blob: 59f951cba45ccd7144550ecb56d6995d9f2d2ea9 [file] [log] [blame]
Simon Glass4ecb97e2021-12-01 09:02:47 -07001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Handles writing the declared ACPI tables
4 *
5 * Copyright 2021 Google LLC
6 */
7
8#define LOG_CATEGORY LOGC_ACPI
9
10#include <common.h>
11#include <log.h>
12#include <malloc.h>
13#include <mapmem.h>
14#include <acpi/acpi_table.h>
15#include <asm/global_data.h>
16#include <dm/acpi.h>
17
18DECLARE_GLOBAL_DATA_PTR;
19
20int acpi_write_one(struct acpi_ctx *ctx, const struct acpi_writer *entry)
21{
22 int ret;
23
24 log_debug("%s: writing table '%s'\n", entry->name,
25 entry->table);
26 ctx->tab_start = ctx->current;
27 ret = entry->h_write(ctx, entry);
28 if (ret == -ENOENT) {
29 log_debug("%s: Omitted due to being empty\n",
30 entry->name);
31 ret = 0;
32 ctx->current = ctx->tab_start; /* drop the table */
33 return ret;
34 }
35 if (ret)
36 return log_msg_ret("write", ret);
37
Simon Glasse9f6e6a2021-12-01 09:02:50 -070038 if (entry->flags & ACPIWF_ALIGN64)
39 acpi_align64(ctx);
40 else
41 acpi_align(ctx);
Simon Glass4ecb97e2021-12-01 09:02:47 -070042
43 return 0;
44}
45
Simon Glass16170882021-12-01 09:02:49 -070046#ifndef CONFIG_QEMU
Simon Glass4ecb97e2021-12-01 09:02:47 -070047static int acpi_write_all(struct acpi_ctx *ctx)
48{
49 const struct acpi_writer *writer =
50 ll_entry_start(struct acpi_writer, acpi_writer);
51 const int n_ents = ll_entry_count(struct acpi_writer, acpi_writer);
52 const struct acpi_writer *entry;
53 int ret;
54
55 for (entry = writer; entry != writer + n_ents; entry++) {
56 ret = acpi_write_one(ctx, entry);
57 if (ret && ret != -ENOENT)
58 return log_msg_ret("one", ret);
59 }
60
61 return 0;
62}
63
64/*
65 * QEMU's version of write_acpi_tables is defined in drivers/misc/qfw.c
66 */
Simon Glass16170882021-12-01 09:02:49 -070067ulong write_acpi_tables(ulong start_addr)
Simon Glass4ecb97e2021-12-01 09:02:47 -070068{
69 struct acpi_ctx *ctx;
70 ulong addr;
Simon Glass4ecb97e2021-12-01 09:02:47 -070071 int ret;
72
Simon Glass37acc662021-12-01 09:02:48 -070073 ctx = malloc(sizeof(*ctx));
Simon Glass4ecb97e2021-12-01 09:02:47 -070074 if (!ctx)
75 return log_msg_ret("mem", -ENOMEM);
Simon Glass4ecb97e2021-12-01 09:02:47 -070076
77 log_debug("ACPI: Writing ACPI tables at %lx\n", start_addr);
78
79 acpi_reset_items();
Simon Glass37acc662021-12-01 09:02:48 -070080 acpi_setup_ctx(ctx, start_addr);
Simon Glass4ecb97e2021-12-01 09:02:47 -070081
82 ret = acpi_write_all(ctx);
83 if (ret) {
84 log_err("Failed to write ACPI tables (err=%d)\n", ret);
85 return log_msg_ret("write", -ENOMEM);
86 }
87
88 addr = map_to_sysmem(ctx->current);
89 log_debug("ACPI current = %lx\n", addr);
90
91 return addr;
92}
Simon Glass5da1e3c2021-12-01 09:03:02 -070093
94int write_dev_tables(struct acpi_ctx *ctx, const struct acpi_writer *entry)
95{
96 int ret;
97
98 ret = acpi_write_dev_tables(ctx);
99 if (ret)
100 return log_msg_ret("write", ret);
101
102 return 0;
103}
104ACPI_WRITER(8dev, NULL, write_dev_tables, 0);
Simon Glass16170882021-12-01 09:02:49 -0700105#endif /* QEMU */
Simon Glass37acc662021-12-01 09:02:48 -0700106
107void acpi_setup_ctx(struct acpi_ctx *ctx, ulong start)
108{
109 gd->acpi_ctx = ctx;
110 memset(ctx, '\0', sizeof(*ctx));
111
112 /* Align ACPI tables to 16-byte boundary */
113 start = ALIGN(start, 16);
114 ctx->base = map_sysmem(start, 0);
115 ctx->current = ctx->base;
116
117 gd_set_acpi_start(start);
118}