drm/radeon: reorganize ATPX support (v2)
[linux-2.6-microblaze.git] / drivers / gpu / drm / radeon / radeon_atpx_handler.c
index 98724fc..331db7a 100644 (file)
 #include <acpi/acpi_bus.h>
 #include <linux/pci.h>
 
-#define ATPX_VERSION 0
-#define ATPX_GPU_PWR 2
-#define ATPX_MUX_SELECT 3
-#define ATPX_I2C_MUX_SELECT 4
-#define ATPX_SWITCH_START 5
-#define ATPX_SWITCH_END 6
-
-#define ATPX_INTEGRATED 0
-#define ATPX_DISCRETE 1
+#include "radeon_acpi.h"
+
+struct radeon_atpx_functions {
+       bool px_params;
+       bool power_cntl;
+       bool disp_mux_cntl;
+       bool i2c_mux_cntl;
+       bool switch_start;
+       bool switch_end;
+       bool disp_connectors_mapping;
+       bool disp_detetion_ports;
+};
 
-#define ATPX_MUX_IGD 0
-#define ATPX_MUX_DISCRETE 1
+struct radeon_atpx {
+       acpi_handle handle;
+       struct radeon_atpx_functions functions;
+};
 
 static struct radeon_atpx_priv {
        bool atpx_detected;
        /* handle for device - and atpx */
        acpi_handle dhandle;
-       acpi_handle atpx_handle;
-       acpi_handle atrm_handle;
+       struct radeon_atpx atpx;
 } radeon_atpx_priv;
 
-/* retrieve the ROM in 4k blocks */
-static int radeon_atrm_call(acpi_handle atrm_handle, uint8_t *bios,
-                           int offset, int len)
-{
-       acpi_status status;
-       union acpi_object atrm_arg_elements[2], *obj;
-       struct acpi_object_list atrm_arg;
-       struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL};
-
-       atrm_arg.count = 2;
-       atrm_arg.pointer = &atrm_arg_elements[0];
-
-       atrm_arg_elements[0].type = ACPI_TYPE_INTEGER;
-       atrm_arg_elements[0].integer.value = offset;
-
-       atrm_arg_elements[1].type = ACPI_TYPE_INTEGER;
-       atrm_arg_elements[1].integer.value = len;
-
-       status = acpi_evaluate_object(atrm_handle, NULL, &atrm_arg, &buffer);
-       if (ACPI_FAILURE(status)) {
-               printk("failed to evaluate ATRM got %s\n", acpi_format_exception(status));
-               return -ENODEV;
-       }
-
-       obj = (union acpi_object *)buffer.pointer;
-       memcpy(bios+offset, obj->buffer.pointer, obj->buffer.length);
-       len = obj->buffer.length;
-       kfree(buffer.pointer);
-       return len;
-}
-
-bool radeon_atrm_supported(struct pci_dev *pdev)
-{
-       /* get the discrete ROM only via ATRM */
-       if (!radeon_atpx_priv.atpx_detected)
-               return false;
-
-       if (radeon_atpx_priv.dhandle == DEVICE_ACPI_HANDLE(&pdev->dev))
-               return false;
-       return true;
-}
+struct atpx_verify_interface {
+       u16 size;               /* structure size in bytes (includes size field) */
+       u16 version;            /* version */
+       u32 function_bits;      /* supported functions bit vector */
+} __packed;
 
+struct atpx_power_control {
+       u16 size;
+       u8 dgpu_state;
+} __packed;
 
-int radeon_atrm_get_bios_chunk(uint8_t *bios, int offset, int len)
-{
-       return radeon_atrm_call(radeon_atpx_priv.atrm_handle, bios, offset, len);
-}
+struct atpx_mux {
+       u16 size;
+       u16 mux;
+} __packed;
 
-static int radeon_atpx_get_version(acpi_handle handle)
+/**
+ * radeon_atpx_call - call an ATPX method
+ *
+ * @handle: acpi handle
+ * @function: the ATPX function to execute
+ * @params: ATPX function params
+ *
+ * Executes the requested ATPX function (all asics).
+ * Returns a pointer to the acpi output buffer.
+ */
+static union acpi_object *radeon_atpx_call(acpi_handle handle, int function,
+                                          struct acpi_buffer *params)
 {
        acpi_status status;
-       union acpi_object atpx_arg_elements[2], *obj;
+       union acpi_object atpx_arg_elements[2];
        struct acpi_object_list atpx_arg;
        struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
 
@@ -92,95 +75,215 @@ static int radeon_atpx_get_version(acpi_handle handle)
        atpx_arg.pointer = &atpx_arg_elements[0];
 
        atpx_arg_elements[0].type = ACPI_TYPE_INTEGER;
-       atpx_arg_elements[0].integer.value = ATPX_VERSION;
+       atpx_arg_elements[0].integer.value = function;
+
+       if (params) {
+               atpx_arg_elements[1].type = ACPI_TYPE_BUFFER;
+               atpx_arg_elements[1].buffer.length = params->length;
+               atpx_arg_elements[1].buffer.pointer = params->pointer;
+       } else {
+               /* We need a second fake parameter */
+               atpx_arg_elements[1].type = ACPI_TYPE_INTEGER;
+               atpx_arg_elements[1].integer.value = 0;
+       }
 
-       atpx_arg_elements[1].type = ACPI_TYPE_INTEGER;
-       atpx_arg_elements[1].integer.value = ATPX_VERSION;
+       status = acpi_evaluate_object(handle, "ATPX", &atpx_arg, &buffer);
 
-       status = acpi_evaluate_object(handle, NULL, &atpx_arg, &buffer);
-       if (ACPI_FAILURE(status)) {
-               printk("%s: failed to call ATPX: %s\n", __func__, acpi_format_exception(status));
-               return -ENOSYS;
+       /* Fail only if calling the method fails and ATPX is supported */
+       if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
+               printk("failed to evaluate ATPX got %s\n",
+                      acpi_format_exception(status));
+               kfree(buffer.pointer);
+               return NULL;
        }
-       obj = (union acpi_object *)buffer.pointer;
-       if (obj && (obj->type == ACPI_TYPE_BUFFER))
-               printk(KERN_INFO "radeon atpx: version is %d\n", *((u8 *)(obj->buffer.pointer) + 2));
-       kfree(buffer.pointer);
-       return 0;
+
+       return buffer.pointer;
 }
 
-static int radeon_atpx_execute(acpi_handle handle, int cmd_id, u16 value)
+/**
+ * radeon_atpx_parse_functions - parse supported functions
+ *
+ * @f: supported functions struct
+ * @mask: supported functions mask from ATPX
+ *
+ * Use the supported functions mask from ATPX function
+ * ATPX_FUNCTION_VERIFY_INTERFACE to determine what functions
+ * are supported (all asics).
+ */
+static void radeon_atpx_parse_functions(struct radeon_atpx_functions *f, u32 mask)
 {
-       acpi_status status;
-       union acpi_object atpx_arg_elements[2];
-       struct acpi_object_list atpx_arg;
-       struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
-       uint8_t buf[4] = {0};
-
-       if (!handle)
-               return -EINVAL;
-
-       atpx_arg.count = 2;
-       atpx_arg.pointer = &atpx_arg_elements[0];
+       f->px_params = mask & ATPX_GET_PX_PARAMETERS_SUPPORTED;
+       f->power_cntl = mask & ATPX_POWER_CONTROL_SUPPORTED;
+       f->disp_mux_cntl = mask & ATPX_DISPLAY_MUX_CONTROL_SUPPORTED;
+       f->i2c_mux_cntl = mask & ATPX_I2C_MUX_CONTROL_SUPPORTED;
+       f->switch_start = mask & ATPX_GRAPHICS_DEVICE_SWITCH_START_NOTIFICATION_SUPPORTED;
+       f->switch_end = mask & ATPX_GRAPHICS_DEVICE_SWITCH_END_NOTIFICATION_SUPPORTED;
+       f->disp_connectors_mapping = mask & ATPX_GET_DISPLAY_CONNECTORS_MAPPING_SUPPORTED;
+       f->disp_detetion_ports = mask & ATPX_GET_DISPLAY_DETECTION_PORTS_SUPPORTED;
+}
 
-       atpx_arg_elements[0].type = ACPI_TYPE_INTEGER;
-       atpx_arg_elements[0].integer.value = cmd_id;
+/**
+ * radeon_atpx_verify_interface - verify ATPX
+ *
+ * @handle: acpi handle
+ * @atpx: radeon atpx struct
+ *
+ * Execute the ATPX_FUNCTION_VERIFY_INTERFACE ATPX function
+ * to initialize ATPX and determine what features are supported
+ * (all asics).
+ * returns 0 on success, error on failure.
+ */
+static int radeon_atpx_verify_interface(struct radeon_atpx *atpx)
+{
+       union acpi_object *info;
+       struct atpx_verify_interface output;
+       size_t size;
+       int err = 0;
+
+       info = radeon_atpx_call(atpx->handle, ATPX_FUNCTION_VERIFY_INTERFACE, NULL);
+       if (!info)
+               return -EIO;
+
+       memset(&output, 0, sizeof(output));
+
+       size = *(u16 *) info->buffer.pointer;
+       if (size < 8) {
+               printk("ATPX buffer is too small: %lu\n", size);
+               err = -EINVAL;
+               goto out;
+       }
+       size = min(sizeof(output), size);
 
-       buf[2] = value & 0xff;
-       buf[3] = (value >> 8) & 0xff;
+       memcpy(&output, info->buffer.pointer, size);
 
-       atpx_arg_elements[1].type = ACPI_TYPE_BUFFER;
-       atpx_arg_elements[1].buffer.length = 4;
-       atpx_arg_elements[1].buffer.pointer = buf;
+       /* TODO: check version? */
+       printk("ATPX version %u\n", output.version);
 
-       status = acpi_evaluate_object(handle, NULL, &atpx_arg, &buffer);
-       if (ACPI_FAILURE(status)) {
-               printk("%s: failed to call ATPX: %s\n", __func__, acpi_format_exception(status));
-               return -ENOSYS;
-       }
-       kfree(buffer.pointer);
+       radeon_atpx_parse_functions(&atpx->functions, output.function_bits);
 
-       return 0;
+out:
+       kfree(info);
+       return err;
 }
 
-static int radeon_atpx_set_discrete_state(acpi_handle handle, int state)
+static int radeon_atpx_set_discrete_state(struct radeon_atpx *atpx, u8 state)
 {
-       return radeon_atpx_execute(handle, ATPX_GPU_PWR, state);
+       struct acpi_buffer params;
+       union acpi_object *info;
+       struct atpx_power_control input;
+
+       if (atpx->functions.power_cntl) {
+               input.size = 3;
+               input.dgpu_state = state;
+               params.length = input.size;
+               params.pointer = &input;
+               info = radeon_atpx_call(atpx->handle,
+                                       ATPX_FUNCTION_POWER_CONTROL,
+                                       &params);
+               if (!info)
+                       return -EIO;
+               kfree(info);
+       }
+       return 0;
 }
 
-static int radeon_atpx_switch_mux(acpi_handle handle, int mux_id)
+static int radeon_atpx_switch_disp_mux(struct radeon_atpx *atpx, u16 mux_id)
 {
-       return radeon_atpx_execute(handle, ATPX_MUX_SELECT, mux_id);
+       struct acpi_buffer params;
+       union acpi_object *info;
+       struct atpx_mux input;
+
+       if (atpx->functions.disp_mux_cntl) {
+               input.size = 4;
+               input.mux = mux_id;
+               params.length = input.size;
+               params.pointer = &input;
+               info = radeon_atpx_call(atpx->handle,
+                                       ATPX_FUNCTION_DISPLAY_MUX_CONTROL,
+                                       &params);
+               if (!info)
+                       return -EIO;
+               kfree(info);
+       }
+       return 0;
 }
 
-static int radeon_atpx_switch_i2c_mux(acpi_handle handle, int mux_id)
+static int radeon_atpx_switch_i2c_mux(struct radeon_atpx *atpx, u16 mux_id)
 {
-       return radeon_atpx_execute(handle, ATPX_I2C_MUX_SELECT, mux_id);
+       struct acpi_buffer params;
+       union acpi_object *info;
+       struct atpx_mux input;
+
+       if (atpx->functions.i2c_mux_cntl) {
+               input.size = 4;
+               input.mux = mux_id;
+               params.length = input.size;
+               params.pointer = &input;
+               info = radeon_atpx_call(atpx->handle,
+                                       ATPX_FUNCTION_I2C_MUX_CONTROL,
+                                       &params);
+               if (!info)
+                       return -EIO;
+               kfree(info);
+       }
+       return 0;
 }
 
-static int radeon_atpx_switch_start(acpi_handle handle, int gpu_id)
+static int radeon_atpx_switch_start(struct radeon_atpx *atpx, u16 mux_id)
 {
-       return radeon_atpx_execute(handle, ATPX_SWITCH_START, gpu_id);
+       struct acpi_buffer params;
+       union acpi_object *info;
+       struct atpx_mux input;
+
+       if (atpx->functions.switch_start) {
+               input.size = 4;
+               input.mux = mux_id;
+               params.length = input.size;
+               params.pointer = &input;
+               info = radeon_atpx_call(atpx->handle,
+                                       ATPX_FUNCTION_GRAPHICS_DEVICE_SWITCH_START_NOTIFICATION,
+                                       &params);
+               if (!info)
+                       return -EIO;
+               kfree(info);
+       }
+       return 0;
 }
 
-static int radeon_atpx_switch_end(acpi_handle handle, int gpu_id)
+static int radeon_atpx_switch_end(struct radeon_atpx *atpx, u16 mux_id)
 {
-       return radeon_atpx_execute(handle, ATPX_SWITCH_END, gpu_id);
+       struct acpi_buffer params;
+       union acpi_object *info;
+       struct atpx_mux input;
+
+       if (atpx->functions.switch_end) {
+               input.size = 4;
+               input.mux = mux_id;
+               params.length = input.size;
+               params.pointer = &input;
+               info = radeon_atpx_call(atpx->handle,
+                                       ATPX_FUNCTION_GRAPHICS_DEVICE_SWITCH_END_NOTIFICATION,
+                                       &params);
+               if (!info)
+                       return -EIO;
+               kfree(info);
+       }
+       return 0;
 }
 
 static int radeon_atpx_switchto(enum vga_switcheroo_client_id id)
 {
-       int gpu_id;
+       u16 gpu_id;
 
        if (id == VGA_SWITCHEROO_IGD)
-               gpu_id = ATPX_INTEGRATED;
+               gpu_id = ATPX_INTEGRATED_GPU;
        else
-               gpu_id = ATPX_DISCRETE;
+               gpu_id = ATPX_DISCRETE_GPU;
 
-       radeon_atpx_switch_start(radeon_atpx_priv.atpx_handle, gpu_id);
-       radeon_atpx_switch_mux(radeon_atpx_priv.atpx_handle, gpu_id);
-       radeon_atpx_switch_i2c_mux(radeon_atpx_priv.atpx_handle, gpu_id);
-       radeon_atpx_switch_end(radeon_atpx_priv.atpx_handle, gpu_id);
+       radeon_atpx_switch_start(&radeon_atpx_priv.atpx, gpu_id);
+       radeon_atpx_switch_disp_mux(&radeon_atpx_priv.atpx, gpu_id);
+       radeon_atpx_switch_i2c_mux(&radeon_atpx_priv.atpx, gpu_id);
+       radeon_atpx_switch_end(&radeon_atpx_priv.atpx, gpu_id);
 
        return 0;
 }
@@ -192,13 +295,13 @@ static int radeon_atpx_power_state(enum vga_switcheroo_client_id id,
        if (id == VGA_SWITCHEROO_IGD)
                return 0;
 
-       radeon_atpx_set_discrete_state(radeon_atpx_priv.atpx_handle, state);
+       radeon_atpx_set_discrete_state(&radeon_atpx_priv.atpx, state);
        return 0;
 }
 
 static bool radeon_atpx_pci_probe_handle(struct pci_dev *pdev)
 {
-       acpi_handle dhandle, atpx_handle, atrm_handle;
+       acpi_handle dhandle, atpx_handle;
        acpi_status status;
 
        dhandle = DEVICE_ACPI_HANDLE(&pdev->dev);
@@ -209,22 +312,15 @@ static bool radeon_atpx_pci_probe_handle(struct pci_dev *pdev)
        if (ACPI_FAILURE(status))
                return false;
 
-       status = acpi_get_handle(dhandle, "ATRM", &atrm_handle);
-       if (ACPI_FAILURE(status))
-               return false;
-
        radeon_atpx_priv.dhandle = dhandle;
-       radeon_atpx_priv.atpx_handle = atpx_handle;
-       radeon_atpx_priv.atrm_handle = atrm_handle;
+       radeon_atpx_priv.atpx.handle = atpx_handle;
        return true;
 }
 
 static int radeon_atpx_init(void)
 {
        /* set up the ATPX handle */
-
-       radeon_atpx_get_version(radeon_atpx_priv.atpx_handle);
-       return 0;
+       return radeon_atpx_verify_interface(&radeon_atpx_priv.atpx);
 }
 
 static int radeon_atpx_get_client_id(struct pci_dev *pdev)
@@ -257,7 +353,7 @@ static bool radeon_atpx_detect(void)
        }
 
        if (has_atpx && vga_count == 2) {
-               acpi_get_name(radeon_atpx_priv.atpx_handle, ACPI_FULL_PATHNAME, &buffer);
+               acpi_get_name(radeon_atpx_priv.atpx.handle, ACPI_FULL_PATHNAME, &buffer);
                printk(KERN_INFO "VGA switcheroo: detected switching method %s handle\n",
                       acpi_method_name);
                radeon_atpx_priv.atpx_detected = true;