Simon Glass | 9fb9e9b | 2020-04-09 10:27:38 -0600 | [diff] [blame] | 1 | // 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 Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 13 | #include <log.h> |
Simon Glass | 9fb9e9b | 2020-04-09 10:27:38 -0600 | [diff] [blame] | 14 | #include <dm/acpi.h> |
Simon Glass | 17968c3 | 2020-04-26 09:19:46 -0600 | [diff] [blame] | 15 | #include <dm/device-internal.h> |
Simon Glass | 9fb9e9b | 2020-04-09 10:27:38 -0600 | [diff] [blame] | 16 | #include <dm/root.h> |
| 17 | |
Simon Glass | 17968c3 | 2020-04-26 09:19:46 -0600 | [diff] [blame] | 18 | /* Type of method to call */ |
| 19 | enum method_t { |
| 20 | METHOD_WRITE_TABLES, |
Simon Glass | d43e0ba | 2020-07-07 13:12:03 -0600 | [diff] [blame^] | 21 | METHOD_FILL_SSDT, |
Simon Glass | 17968c3 | 2020-04-26 09:19:46 -0600 | [diff] [blame] | 22 | }; |
| 23 | |
| 24 | /* Prototype for all methods */ |
| 25 | typedef int (*acpi_method)(const struct udevice *dev, struct acpi_ctx *ctx); |
| 26 | |
Simon Glass | 9fb9e9b | 2020-04-09 10:27:38 -0600 | [diff] [blame] | 27 | int 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 | |
| 35 | int 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 Glass | 17968c3 | 2020-04-26 09:19:46 -0600 | [diff] [blame] | 45 | |
| 46 | acpi_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 Glass | d43e0ba | 2020-07-07 13:12:03 -0600 | [diff] [blame^] | 55 | case METHOD_FILL_SSDT: |
| 56 | return aops->fill_ssdt; |
Simon Glass | 17968c3 | 2020-04-26 09:19:46 -0600 | [diff] [blame] | 57 | } |
| 58 | } |
| 59 | |
| 60 | return NULL; |
| 61 | } |
| 62 | |
| 63 | int 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 Glass | d43e0ba | 2020-07-07 13:12:03 -0600 | [diff] [blame^] | 90 | int 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 Glass | 17968c3 | 2020-04-26 09:19:46 -0600 | [diff] [blame] | 101 | int 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 | } |