]> Git Repo - J-u-boot.git/blob - lib/acpi/acpi_writer.c
x86: Move device-specific ACPI tables to a writer function
[J-u-boot.git] / lib / acpi / acpi_writer.c
1 // 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
18 DECLARE_GLOBAL_DATA_PTR;
19
20 int 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
38         if (entry->flags & ACPIWF_ALIGN64)
39                 acpi_align64(ctx);
40         else
41                 acpi_align(ctx);
42
43         return 0;
44 }
45
46 #ifndef CONFIG_QEMU
47 static 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  */
67 ulong write_acpi_tables(ulong start_addr)
68 {
69         struct acpi_ctx *ctx;
70         ulong addr;
71         int ret;
72
73         ctx = malloc(sizeof(*ctx));
74         if (!ctx)
75                 return log_msg_ret("mem", -ENOMEM);
76
77         log_debug("ACPI: Writing ACPI tables at %lx\n", start_addr);
78
79         acpi_reset_items();
80         acpi_setup_ctx(ctx, start_addr);
81
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 }
93
94 int 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 }
104 ACPI_WRITER(8dev, NULL, write_dev_tables, 0);
105 #endif /* QEMU */
106
107 void 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 }
This page took 0.033501 seconds and 4 git commands to generate.