2 * Copyright (c) 2010 Red Hat Inc.
3 * Author : Dave Airlie <airlied@redhat.com>
7 * ATPX support for both Intel/ATI
9 #include <linux/vga_switcheroo.h>
10 #include <acpi/acpi.h>
11 #include <acpi/acpi_bus.h>
12 #include <linux/pci.h>
14 #define ATPX_VERSION 0
15 #define ATPX_GPU_PWR 2
16 #define ATPX_MUX_SELECT 3
18 #define ATPX_INTEGRATED 0
19 #define ATPX_DISCRETE 1
21 #define ATPX_MUX_IGD 0
22 #define ATPX_MUX_DISCRETE 1
24 static struct radeon_atpx_priv {
26 /* handle for device - and atpx */
28 acpi_handle atpx_handle;
29 acpi_handle atrm_handle;
32 /* retrieve the ROM in 4k blocks */
33 static int radeon_atrm_call(acpi_handle atrm_handle, uint8_t *bios,
37 union acpi_object atrm_arg_elements[2], *obj;
38 struct acpi_object_list atrm_arg;
39 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL};
42 atrm_arg.pointer = &atrm_arg_elements[0];
44 atrm_arg_elements[0].type = ACPI_TYPE_INTEGER;
45 atrm_arg_elements[0].integer.value = offset;
47 atrm_arg_elements[1].type = ACPI_TYPE_INTEGER;
48 atrm_arg_elements[1].integer.value = len;
50 status = acpi_evaluate_object(atrm_handle, NULL, &atrm_arg, &buffer);
51 if (ACPI_FAILURE(status)) {
52 printk("failed to evaluate ATRM got %s\n", acpi_format_exception(status));
56 obj = (union acpi_object *)buffer.pointer;
57 memcpy(bios+offset, obj->buffer.pointer, len);
58 kfree(buffer.pointer);
62 bool radeon_atrm_supported(struct pci_dev *pdev)
64 /* get the discrete ROM only via ATRM */
65 if (!radeon_atpx_priv.atpx_detected)
68 if (radeon_atpx_priv.dhandle == DEVICE_ACPI_HANDLE(&pdev->dev))
74 int radeon_atrm_get_bios_chunk(uint8_t *bios, int offset, int len)
76 return radeon_atrm_call(radeon_atpx_priv.atrm_handle, bios, offset, len);
79 static int radeon_atpx_get_version(acpi_handle handle)
82 union acpi_object atpx_arg_elements[2], *obj;
83 struct acpi_object_list atpx_arg;
84 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
87 atpx_arg.pointer = &atpx_arg_elements[0];
89 atpx_arg_elements[0].type = ACPI_TYPE_INTEGER;
90 atpx_arg_elements[0].integer.value = ATPX_VERSION;
92 atpx_arg_elements[1].type = ACPI_TYPE_INTEGER;
93 atpx_arg_elements[1].integer.value = ATPX_VERSION;
95 status = acpi_evaluate_object(handle, NULL, &atpx_arg, &buffer);
96 if (ACPI_FAILURE(status)) {
97 printk("%s: failed to call ATPX: %s\n", __func__, acpi_format_exception(status));
100 obj = (union acpi_object *)buffer.pointer;
101 if (obj && (obj->type == ACPI_TYPE_BUFFER))
102 printk(KERN_INFO "radeon atpx: version is %d\n", *((u8 *)(obj->buffer.pointer) + 2));
103 kfree(buffer.pointer);
107 static int radeon_atpx_execute(acpi_handle handle, int cmd_id, u16 value)
110 union acpi_object atpx_arg_elements[2];
111 struct acpi_object_list atpx_arg;
112 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
113 uint8_t buf[4] = {0};
119 atpx_arg.pointer = &atpx_arg_elements[0];
121 atpx_arg_elements[0].type = ACPI_TYPE_INTEGER;
122 atpx_arg_elements[0].integer.value = cmd_id;
124 buf[2] = value & 0xff;
125 buf[3] = (value >> 8) & 0xff;
127 atpx_arg_elements[1].type = ACPI_TYPE_BUFFER;
128 atpx_arg_elements[1].buffer.length = 4;
129 atpx_arg_elements[1].buffer.pointer = buf;
131 status = acpi_evaluate_object(handle, NULL, &atpx_arg, &buffer);
132 if (ACPI_FAILURE(status)) {
133 printk("%s: failed to call ATPX: %s\n", __func__, acpi_format_exception(status));
136 kfree(buffer.pointer);
141 static int radeon_atpx_set_discrete_state(acpi_handle handle, int state)
143 return radeon_atpx_execute(handle, ATPX_GPU_PWR, state);
146 static int radeon_atpx_switch_mux(acpi_handle handle, int mux_id)
148 return radeon_atpx_execute(handle, ATPX_MUX_SELECT, mux_id);
152 static int radeon_atpx_switchto(enum vga_switcheroo_client_id id)
154 if (id == VGA_SWITCHEROO_IGD)
155 radeon_atpx_switch_mux(radeon_atpx_priv.atpx_handle, 0);
157 radeon_atpx_switch_mux(radeon_atpx_priv.atpx_handle, 1);
161 static int radeon_atpx_power_state(enum vga_switcheroo_client_id id,
162 enum vga_switcheroo_state state)
164 /* on w500 ACPI can't change intel gpu state */
165 if (id == VGA_SWITCHEROO_IGD)
168 radeon_atpx_set_discrete_state(radeon_atpx_priv.atpx_handle, state);
172 static bool radeon_atpx_pci_probe_handle(struct pci_dev *pdev)
174 acpi_handle dhandle, atpx_handle, atrm_handle;
177 dhandle = DEVICE_ACPI_HANDLE(&pdev->dev);
181 status = acpi_get_handle(dhandle, "ATPX", &atpx_handle);
182 if (ACPI_FAILURE(status))
185 status = acpi_get_handle(dhandle, "ATRM", &atrm_handle);
186 if (ACPI_FAILURE(status))
189 radeon_atpx_priv.dhandle = dhandle;
190 radeon_atpx_priv.atpx_handle = atpx_handle;
191 radeon_atpx_priv.atrm_handle = atrm_handle;
195 static int radeon_atpx_init(void)
197 /* set up the ATPX handle */
199 radeon_atpx_get_version(radeon_atpx_priv.atpx_handle);
203 static int radeon_atpx_get_client_id(struct pci_dev *pdev)
205 if (radeon_atpx_priv.dhandle == DEVICE_ACPI_HANDLE(&pdev->dev))
206 return VGA_SWITCHEROO_IGD;
208 return VGA_SWITCHEROO_DIS;
211 static struct vga_switcheroo_handler radeon_atpx_handler = {
212 .switchto = radeon_atpx_switchto,
213 .power_state = radeon_atpx_power_state,
214 .init = radeon_atpx_init,
215 .get_client_id = radeon_atpx_get_client_id,
218 static bool radeon_atpx_detect(void)
220 char acpi_method_name[255] = { 0 };
221 struct acpi_buffer buffer = {sizeof(acpi_method_name), acpi_method_name};
222 struct pci_dev *pdev = NULL;
223 bool has_atpx = false;
226 while ((pdev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, pdev)) != NULL) {
229 has_atpx |= (radeon_atpx_pci_probe_handle(pdev) == true);
232 if (has_atpx && vga_count == 2) {
233 acpi_get_name(radeon_atpx_priv.atpx_handle, ACPI_FULL_PATHNAME, &buffer);
234 printk(KERN_INFO "VGA switcheroo: detected switching method %s handle\n",
236 radeon_atpx_priv.atpx_detected = true;
242 void radeon_register_atpx_handler(void)
246 /* detect if we have any ATPX + 2 VGA in the system */
247 r = radeon_atpx_detect();
251 vga_switcheroo_register_handler(&radeon_atpx_handler);
254 void radeon_unregister_atpx_handler(void)
256 vga_switcheroo_unregister_handler();