blob: bbb9b54786d366eacf049697793e440b0909d347 [file] [log] [blame]
Simon Glass6afa63a2021-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
Simon Glass6afa63a2021-12-01 09:02:47 -070010#include <log.h>
11#include <malloc.h>
12#include <mapmem.h>
13#include <acpi/acpi_table.h>
14#include <asm/global_data.h>
15#include <dm/acpi.h>
Tom Rini467382c2023-12-14 13:16:58 -050016#include <linux/errno.h>
Simon Glass6afa63a2021-12-01 09:02:47 -070017
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 Glass94ba15a2021-12-01 09:02:50 -070038 if (entry->flags & ACPIWF_ALIGN64)
39 acpi_align64(ctx);
40 else
41 acpi_align(ctx);
Simon Glass6afa63a2021-12-01 09:02:47 -070042
Simon Glass2d7c7382021-12-01 09:03:04 -070043 /* Add the item to the internal list */
44 ret = acpi_add_other_item(ctx, entry, ctx->tab_start);
45 if (ret)
46 return log_msg_ret("add", ret);
47
Simon Glass6afa63a2021-12-01 09:02:47 -070048 return 0;
49}
50
Heinrich Schuchardtb2f088c2023-12-19 16:04:00 +010051#ifndef CONFIG_QFW_ACPI
Simon Glass6afa63a2021-12-01 09:02:47 -070052static int acpi_write_all(struct acpi_ctx *ctx)
53{
54 const struct acpi_writer *writer =
55 ll_entry_start(struct acpi_writer, acpi_writer);
56 const int n_ents = ll_entry_count(struct acpi_writer, acpi_writer);
57 const struct acpi_writer *entry;
58 int ret;
59
60 for (entry = writer; entry != writer + n_ents; entry++) {
61 ret = acpi_write_one(ctx, entry);
62 if (ret && ret != -ENOENT)
63 return log_msg_ret("one", ret);
64 }
65
66 return 0;
67}
68
69/*
70 * QEMU's version of write_acpi_tables is defined in drivers/misc/qfw.c
71 */
Simon Glass31c27eb2021-12-01 09:02:49 -070072ulong write_acpi_tables(ulong start_addr)
Simon Glass6afa63a2021-12-01 09:02:47 -070073{
74 struct acpi_ctx *ctx;
75 ulong addr;
Simon Glass6afa63a2021-12-01 09:02:47 -070076 int ret;
77
Simon Glasscc1f8c32021-12-01 09:02:48 -070078 ctx = malloc(sizeof(*ctx));
Simon Glass6afa63a2021-12-01 09:02:47 -070079 if (!ctx)
80 return log_msg_ret("mem", -ENOMEM);
Simon Glass6afa63a2021-12-01 09:02:47 -070081
82 log_debug("ACPI: Writing ACPI tables at %lx\n", start_addr);
83
84 acpi_reset_items();
Simon Glasscc1f8c32021-12-01 09:02:48 -070085 acpi_setup_ctx(ctx, start_addr);
Simon Glass6afa63a2021-12-01 09:02:47 -070086
87 ret = acpi_write_all(ctx);
88 if (ret) {
89 log_err("Failed to write ACPI tables (err=%d)\n", ret);
90 return log_msg_ret("write", -ENOMEM);
91 }
92
93 addr = map_to_sysmem(ctx->current);
94 log_debug("ACPI current = %lx\n", addr);
95
96 return addr;
97}
Simon Glassa7e53b92021-12-01 09:03:02 -070098
99int write_dev_tables(struct acpi_ctx *ctx, const struct acpi_writer *entry)
100{
101 int ret;
102
103 ret = acpi_write_dev_tables(ctx);
104 if (ret)
105 return log_msg_ret("write", ret);
106
107 return 0;
108}
109ACPI_WRITER(8dev, NULL, write_dev_tables, 0);
Simon Glass9d2adca2021-12-01 09:03:03 -0700110
111ulong acpi_get_rsdp_addr(void)
112{
113 if (!gd->acpi_ctx)
114 return 0;
115
116 return map_to_sysmem(gd->acpi_ctx->rsdp);
117}
Heinrich Schuchardtb2f088c2023-12-19 16:04:00 +0100118#endif /* QFW_ACPI */
Simon Glasscc1f8c32021-12-01 09:02:48 -0700119
120void acpi_setup_ctx(struct acpi_ctx *ctx, ulong start)
121{
122 gd->acpi_ctx = ctx;
123 memset(ctx, '\0', sizeof(*ctx));
124
125 /* Align ACPI tables to 16-byte boundary */
126 start = ALIGN(start, 16);
127 ctx->base = map_sysmem(start, 0);
128 ctx->current = ctx->base;
129
130 gd_set_acpi_start(start);
131}