drivers/gfx: Add generic graphics with SSDT generator

Adds a generic graphics driver that can be added to a devicetree which
populates graphics-related ACPI table. It will write the _DOD method
(Enumerate All Devices Attached to the Display Adapter) and a device
object for each device defined. The device may optionally have a
connected privacy screen which can be controlled with a _DSM.

Example:
chip drivers/generic/gfx
  register "device_count" = "1"
  register "device[0].name" = ""LCD""
  register "device[0].addr" = "0x0400"
  register "device[0].privacy.enabled" = "1"
  register "device[0].privacy.detect_function" = ""\\_SB.PCI0.PVSC.GPVD""
  register "device[0].privacy.status_function" = ""\\_SB.PCI0.PVSC.GPVX""
  register "device[0].privacy.enable_function" = ""\\_SB.PCI0.PVSC.EPVX""
  register "device[0].privacy.disable_function" = ""\\_SB.PCI0.PVSC.DPVX""
  device generic 0 on end
end

ASL
Scope (\_SB.PCI0.GFX0)
{
    Method (_DOD, 0, NotSerialized)  // _DOD: Display Output Devices
    {
        Return (Package (0x01)
        {
            0x00000400
        })
    }

    Device (LCD)
    {
        Name (_ADR, 0x0400)  // _ADR: Address
        Name (_STA, 0x0F)  // _STA: Status
        Method (_DSM, 4, Serialized)  // _DSM: Device-Specific Method
        {
            ToBuffer (Arg0, Local0)
            If ((Local0 == ToUUID ("c7033113-8720-4ceb-9090-9d52b3e52d73")))
            {
                ToInteger (Arg2, Local1)
                If ((Local1 == Zero))
                {
                    Local2 = \_SB.PCI0.PVSC.GPVD ()
                    If ((Local2 == One))
                    {
                        Return (Buffer (One)
                        {
                                0x0F
                        })
                    }
                }

                If ((Local1 == One))
                {
                    ToBuffer (\_SB.PCI0.PVSC.GPVX (), Local2)
                    Return (Local2)
                }

                If ((Local1 == 0x02))
                {
                    \_SB.PCI0.PVSC.EPVX ()
                }

                If ((Local1 == 0x03))
                {
                    \_SB.PCI0.PVSC.DPVX ()
                }

                Return (Buffer (One)
                {
                        0x00
                })
            }

            Return (Buffer (One)
            {
                    0x00
            })
        }
    }
}

BUG=b:142237145
TEST=Added gfx to devicetree on sarien_cml and correct ASL in SSDT

Change-Id: Ida520dd7aad81ee7c1e5f2d0d3f5cc1a766d78a0
Signed-off-by: Mathew King <mathewk@chromium.org>
Reviewed-on: https://review.coreboot.org/c/coreboot/+/36041
Reviewed-by: Simon Glass <sjg@chromium.org>
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
diff --git a/src/drivers/generic/gfx/gfx.c b/src/drivers/generic/gfx/gfx.c
new file mode 100644
index 0000000..76d311c
--- /dev/null
+++ b/src/drivers/generic/gfx/gfx.c
@@ -0,0 +1,127 @@
+/*
+ * This file is part of the coreboot project.
+ *
+ * Copyright 2019 Google LLC
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+
+#include <arch/acpigen.h>
+#include <console/console.h>
+#include <device/device.h>
+#include <device/pci.h>
+#include <device/pci_ids.h>
+#include <stdint.h>
+#include <string.h>
+
+#include "chip.h"
+
+#define ACPI_DSM_PRIVACY_SCREEN_UUID	"C7033113-8720-4CEB-9090-9D52B3E52D73"
+
+static void privacy_screen_detect_cb(void *arg)
+{
+	struct drivers_generic_gfx_privacy_screen_config *config = arg;
+
+	acpigen_write_store();
+	acpigen_emit_namestring(config->detect_function);
+	acpigen_emit_byte(LOCAL2_OP);
+	acpigen_write_if_lequal_op_int(LOCAL2_OP, 1);
+	acpigen_write_return_singleton_buffer(0xF);
+	acpigen_pop_len();
+}
+static void privacy_screen_get_status_cb(void *arg)
+{
+	struct drivers_generic_gfx_privacy_screen_config *config = arg;
+
+	acpigen_emit_byte(RETURN_OP);
+	acpigen_emit_namestring(config->status_function);
+}
+static void privacy_screen_enable_cb(void *arg)
+{
+	struct drivers_generic_gfx_privacy_screen_config *config = arg;
+
+	acpigen_emit_namestring(config->enable_function);
+}
+static void privacy_screen_disable_cb(void *arg)
+{
+	struct drivers_generic_gfx_privacy_screen_config *config = arg;
+
+	acpigen_emit_namestring(config->disable_function);
+}
+
+static void (*privacy_screen_callbacks[])(void *) = {
+	privacy_screen_detect_cb,
+	privacy_screen_get_status_cb,
+	privacy_screen_enable_cb,
+	privacy_screen_disable_cb,
+};
+
+static void gfx_fill_ssdt_generator(struct device *dev)
+{
+	size_t i;
+	struct drivers_generic_gfx_config *config = dev->chip_info;
+
+	const char *scope = acpi_device_scope(dev);
+
+	acpigen_write_scope(scope);
+
+	/* Method (_DOD, 0) */
+	acpigen_write_method("_DOD", 0);
+	acpigen_emit_byte(RETURN_OP);
+	acpigen_write_package(config->device_count);
+	for (i = 0; i < config->device_count; i++)
+		acpigen_write_dword(config->device[i].addr);
+	acpigen_pop_len(); /* End Package. */
+	acpigen_pop_len(); /* End Method. */
+
+	for (i = 0; i < config->device_count; i++) {
+		acpigen_write_device(config->device[i].name);
+
+		acpigen_write_name_integer("_ADR", config->device[i].addr);
+		acpigen_write_name_integer("_STA", 0xF);
+
+		if (config->device[i].privacy.enabled) {
+			acpigen_write_dsm(ACPI_DSM_PRIVACY_SCREEN_UUID,
+				privacy_screen_callbacks,
+				ARRAY_SIZE(privacy_screen_callbacks),
+				&config->device[i].privacy);
+		}
+
+		acpigen_pop_len(); /* Device */
+	}
+	acpigen_pop_len(); /* Scope */
+}
+
+static const char *gfx_acpi_name(const struct device *dev)
+{
+	struct drivers_generic_gfx_config *config = dev->chip_info;
+
+	return config->name ? : "GFX0";
+}
+
+static struct device_operations gfx_ops = {
+	.acpi_name		  = gfx_acpi_name,
+	.acpi_fill_ssdt_generator = gfx_fill_ssdt_generator,
+};
+
+static void gfx_enable(struct device *dev)
+{
+	struct drivers_generic_gfx_config *config = dev->chip_info;
+
+	if (!config)
+		return;
+
+	dev->ops = &gfx_ops;
+}
+
+struct chip_operations drivers_generic_gfx_ops = {
+	CHIP_NAME("Graphics Device")
+	.enable_dev = gfx_enable
+};