blob: 4497b5cb2f669e14e029593eb096d122fce0000f [file] [log] [blame]
Simon Glass9fb9e9b2020-04-09 10:27:38 -06001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Core driver model support for ACPI table generation
4 *
5 * Copyright 2019 Google LLC
6 * Written by Simon Glass <sjg@chromium.org>
7 */
8
9#define LOG_CATEOGRY LOGC_ACPI
10
11#include <common.h>
12#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -060013#include <log.h>
Simon Glass9fb9e9b2020-04-09 10:27:38 -060014#include <dm/acpi.h>
Simon Glass17968c32020-04-26 09:19:46 -060015#include <dm/device-internal.h>
Simon Glass9fb9e9b2020-04-09 10:27:38 -060016#include <dm/root.h>
17
Simon Glass17968c32020-04-26 09:19:46 -060018/* Type of method to call */
19enum method_t {
20 METHOD_WRITE_TABLES,
Simon Glassd43e0ba2020-07-07 13:12:03 -060021 METHOD_FILL_SSDT,
Simon Glass17968c32020-04-26 09:19:46 -060022};
23
24/* Prototype for all methods */
25typedef int (*acpi_method)(const struct udevice *dev, struct acpi_ctx *ctx);
26
Simon Glass9fb9e9b2020-04-09 10:27:38 -060027int acpi_copy_name(char *out_name, const char *name)
28{
29 strncpy(out_name, name, ACPI_NAME_LEN);
30 out_name[ACPI_NAME_LEN] = '\0';
31
32 return 0;
33}
34
35int acpi_get_name(const struct udevice *dev, char *out_name)
36{
37 struct acpi_ops *aops;
38
39 aops = device_get_acpi_ops(dev);
40 if (aops && aops->get_name)
41 return aops->get_name(dev, out_name);
42
43 return -ENOSYS;
44}
Simon Glass17968c32020-04-26 09:19:46 -060045
46acpi_method acpi_get_method(struct udevice *dev, enum method_t method)
47{
48 struct acpi_ops *aops;
49
50 aops = device_get_acpi_ops(dev);
51 if (aops) {
52 switch (method) {
53 case METHOD_WRITE_TABLES:
54 return aops->write_tables;
Simon Glassd43e0ba2020-07-07 13:12:03 -060055 case METHOD_FILL_SSDT:
56 return aops->fill_ssdt;
Simon Glass17968c32020-04-26 09:19:46 -060057 }
58 }
59
60 return NULL;
61}
62
63int acpi_recurse_method(struct acpi_ctx *ctx, struct udevice *parent,
64 enum method_t method)
65{
66 struct udevice *dev;
67 acpi_method func;
68 int ret;
69
70 func = acpi_get_method(parent, method);
71 if (func) {
72 log_debug("\n");
73 log_debug("- %s %p\n", parent->name, func);
74 ret = device_ofdata_to_platdata(parent);
75 if (ret)
76 return log_msg_ret("ofdata", ret);
77 ret = func(parent, ctx);
78 if (ret)
79 return log_msg_ret("func", ret);
80 }
81 device_foreach_child(dev, parent) {
82 ret = acpi_recurse_method(ctx, dev, method);
83 if (ret)
84 return log_msg_ret("recurse", ret);
85 }
86
87 return 0;
88}
89
Simon Glassd43e0ba2020-07-07 13:12:03 -060090int acpi_fill_ssdt(struct acpi_ctx *ctx)
91{
92 int ret;
93
94 log_debug("Writing SSDT tables\n");
95 ret = acpi_recurse_method(ctx, dm_root(), METHOD_FILL_SSDT);
96 log_debug("Writing SSDT finished, err=%d\n", ret);
97
98 return ret;
99}
100
Simon Glass17968c32020-04-26 09:19:46 -0600101int acpi_write_dev_tables(struct acpi_ctx *ctx)
102{
103 int ret;
104
105 log_debug("Writing device tables\n");
106 ret = acpi_recurse_method(ctx, dm_root(), METHOD_WRITE_TABLES);
107 log_debug("Writing finished, err=%d\n", ret);
108
109 return ret;
110}