acpi: Add support for DSDT generation

Some devices need to inject extra code into the Differentiated System
Descriptor Table (DSDT). Add a method to handle this.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Wolfgang Wallner <wolfgang.wallner@br-automation.com>
Reviewed-by: Bin Meng <bmeng.cn@gmail.com>
[bmeng: correct one typo in inject_dsdt() comments]
Signed-off-by: Bin Meng <bmeng.cn@gmail.com>
diff --git a/drivers/core/acpi.c b/drivers/core/acpi.c
index a9b7fc1..7b32694 100644
--- a/drivers/core/acpi.c
+++ b/drivers/core/acpi.c
@@ -22,12 +22,14 @@
 enum gen_type_t {
 	TYPE_NONE,
 	TYPE_SSDT,
+	TYPE_DSDT,
 };
 
 /* Type of method to call */
 enum method_t {
 	METHOD_WRITE_TABLES,
 	METHOD_FILL_SSDT,
+	METHOD_INJECT_DSDT,
 };
 
 /* Prototype for all methods */
@@ -144,7 +146,9 @@
 	void *end = ctx->current;
 
 	ptr = start;
-	order = ofnode_read_chosen_prop("u-boot,acpi-ssdt-order", &size);
+	order = ofnode_read_chosen_prop(type == TYPE_DSDT ?
+					"u-boot,acpi-dsdt-order" :
+					"u-boot,acpi-ssdt-order", &size);
 	if (!order) {
 		log_warning("Failed to find ordering, leaving as is\n");
 		return 0;
@@ -198,6 +202,8 @@
 			return aops->write_tables;
 		case METHOD_FILL_SSDT:
 			return aops->fill_ssdt;
+		case METHOD_INJECT_DSDT:
+			return aops->inject_dsdt;
 		}
 	}
 
@@ -256,6 +262,23 @@
 	return ret;
 }
 
+int acpi_inject_dsdt(struct acpi_ctx *ctx)
+{
+	void *start = ctx->current;
+	int ret;
+
+	log_debug("Writing DSDT tables\n");
+	item_count = 0;
+	ret = acpi_recurse_method(ctx, dm_root(), METHOD_INJECT_DSDT,
+				  TYPE_DSDT);
+	log_debug("Writing DSDT finished, err=%d\n", ret);
+	ret = sort_acpi_item_type(ctx, start, TYPE_DSDT);
+	if (ret)
+		return log_msg_ret("build", ret);
+
+	return ret;
+}
+
 int acpi_write_dev_tables(struct acpi_ctx *ctx)
 {
 	int ret;