Commit | Line | Data |
---|---|---|
6a9ee8af DA |
1 | /* |
2 | * Copyright (c) 2010 Red Hat Inc. | |
3 | * Author : Dave Airlie <airlied@redhat.com> | |
4 | * | |
5 | * Licensed under GPLv2 | |
6 | * | |
7 | * ATPX support for both Intel/ATI | |
8 | */ | |
6a9ee8af DA |
9 | #include <linux/vga_switcheroo.h> |
10 | #include <acpi/acpi.h> | |
11 | #include <acpi/acpi_bus.h> | |
12 | #include <linux/pci.h> | |
13 | ||
14 | #define ATPX_VERSION 0 | |
15 | #define ATPX_GPU_PWR 2 | |
16 | #define ATPX_MUX_SELECT 3 | |
17 | ||
18 | #define ATPX_INTEGRATED 0 | |
19 | #define ATPX_DISCRETE 1 | |
20 | ||
21 | #define ATPX_MUX_IGD 0 | |
22 | #define ATPX_MUX_DISCRETE 1 | |
23 | ||
24 | static struct radeon_atpx_priv { | |
25 | bool atpx_detected; | |
26 | /* handle for device - and atpx */ | |
27 | acpi_handle dhandle; | |
28 | acpi_handle atpx_handle; | |
29 | acpi_handle atrm_handle; | |
30 | } radeon_atpx_priv; | |
31 | ||
32 | /* retrieve the ROM in 4k blocks */ | |
33 | static int radeon_atrm_call(acpi_handle atrm_handle, uint8_t *bios, | |
34 | int offset, int len) | |
35 | { | |
36 | acpi_status status; | |
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}; | |
40 | ||
41 | atrm_arg.count = 2; | |
42 | atrm_arg.pointer = &atrm_arg_elements[0]; | |
43 | ||
44 | atrm_arg_elements[0].type = ACPI_TYPE_INTEGER; | |
45 | atrm_arg_elements[0].integer.value = offset; | |
46 | ||
47 | atrm_arg_elements[1].type = ACPI_TYPE_INTEGER; | |
48 | atrm_arg_elements[1].integer.value = len; | |
49 | ||
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)); | |
53 | return -ENODEV; | |
54 | } | |
55 | ||
56 | obj = (union acpi_object *)buffer.pointer; | |
57 | memcpy(bios+offset, obj->buffer.pointer, len); | |
58 | kfree(buffer.pointer); | |
59 | return len; | |
60 | } | |
61 | ||
62 | bool radeon_atrm_supported(struct pci_dev *pdev) | |
63 | { | |
64 | /* get the discrete ROM only via ATRM */ | |
65 | if (!radeon_atpx_priv.atpx_detected) | |
66 | return false; | |
67 | ||
68 | if (radeon_atpx_priv.dhandle == DEVICE_ACPI_HANDLE(&pdev->dev)) | |
69 | return false; | |
70 | return true; | |
71 | } | |
72 | ||
73 | ||
74 | int radeon_atrm_get_bios_chunk(uint8_t *bios, int offset, int len) | |
75 | { | |
76 | return radeon_atrm_call(radeon_atpx_priv.atrm_handle, bios, offset, len); | |
77 | } | |
78 | ||
79 | static int radeon_atpx_get_version(acpi_handle handle) | |
80 | { | |
81 | acpi_status status; | |
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 }; | |
85 | ||
86 | atpx_arg.count = 2; | |
87 | atpx_arg.pointer = &atpx_arg_elements[0]; | |
88 | ||
89 | atpx_arg_elements[0].type = ACPI_TYPE_INTEGER; | |
90 | atpx_arg_elements[0].integer.value = ATPX_VERSION; | |
91 | ||
92 | atpx_arg_elements[1].type = ACPI_TYPE_INTEGER; | |
93 | atpx_arg_elements[1].integer.value = ATPX_VERSION; | |
94 | ||
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)); | |
98 | return -ENOSYS; | |
99 | } | |
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); | |
104 | return 0; | |
105 | } | |
106 | ||
107 | static int radeon_atpx_execute(acpi_handle handle, int cmd_id, u16 value) | |
108 | { | |
109 | acpi_status status; | |
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}; | |
114 | ||
115 | if (!handle) | |
116 | return -EINVAL; | |
117 | ||
118 | atpx_arg.count = 2; | |
119 | atpx_arg.pointer = &atpx_arg_elements[0]; | |
120 | ||
121 | atpx_arg_elements[0].type = ACPI_TYPE_INTEGER; | |
122 | atpx_arg_elements[0].integer.value = cmd_id; | |
123 | ||
124 | buf[2] = value & 0xff; | |
125 | buf[3] = (value >> 8) & 0xff; | |
126 | ||
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; | |
130 | ||
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)); | |
134 | return -ENOSYS; | |
135 | } | |
136 | kfree(buffer.pointer); | |
137 | ||
138 | return 0; | |
139 | } | |
140 | ||
141 | static int radeon_atpx_set_discrete_state(acpi_handle handle, int state) | |
142 | { | |
143 | return radeon_atpx_execute(handle, ATPX_GPU_PWR, state); | |
144 | } | |
145 | ||
146 | static int radeon_atpx_switch_mux(acpi_handle handle, int mux_id) | |
147 | { | |
148 | return radeon_atpx_execute(handle, ATPX_MUX_SELECT, mux_id); | |
149 | } | |
150 | ||
151 | ||
152 | static int radeon_atpx_switchto(enum vga_switcheroo_client_id id) | |
153 | { | |
154 | if (id == VGA_SWITCHEROO_IGD) | |
155 | radeon_atpx_switch_mux(radeon_atpx_priv.atpx_handle, 0); | |
156 | else | |
157 | radeon_atpx_switch_mux(radeon_atpx_priv.atpx_handle, 1); | |
158 | return 0; | |
159 | } | |
160 | ||
161 | static int radeon_atpx_power_state(enum vga_switcheroo_client_id id, | |
162 | enum vga_switcheroo_state state) | |
163 | { | |
164 | /* on w500 ACPI can't change intel gpu state */ | |
165 | if (id == VGA_SWITCHEROO_IGD) | |
166 | return 0; | |
167 | ||
168 | radeon_atpx_set_discrete_state(radeon_atpx_priv.atpx_handle, state); | |
169 | return 0; | |
170 | } | |
171 | ||
172 | static bool radeon_atpx_pci_probe_handle(struct pci_dev *pdev) | |
173 | { | |
174 | acpi_handle dhandle, atpx_handle, atrm_handle; | |
175 | acpi_status status; | |
176 | ||
177 | dhandle = DEVICE_ACPI_HANDLE(&pdev->dev); | |
178 | if (!dhandle) | |
179 | return false; | |
180 | ||
181 | status = acpi_get_handle(dhandle, "ATPX", &atpx_handle); | |
182 | if (ACPI_FAILURE(status)) | |
183 | return false; | |
184 | ||
185 | status = acpi_get_handle(dhandle, "ATRM", &atrm_handle); | |
186 | if (ACPI_FAILURE(status)) | |
187 | return false; | |
188 | ||
189 | radeon_atpx_priv.dhandle = dhandle; | |
190 | radeon_atpx_priv.atpx_handle = atpx_handle; | |
191 | radeon_atpx_priv.atrm_handle = atrm_handle; | |
192 | return true; | |
193 | } | |
194 | ||
195 | static int radeon_atpx_init(void) | |
196 | { | |
197 | /* set up the ATPX handle */ | |
198 | ||
199 | radeon_atpx_get_version(radeon_atpx_priv.atpx_handle); | |
200 | return 0; | |
201 | } | |
202 | ||
203 | static int radeon_atpx_get_client_id(struct pci_dev *pdev) | |
204 | { | |
205 | if (radeon_atpx_priv.dhandle == DEVICE_ACPI_HANDLE(&pdev->dev)) | |
206 | return VGA_SWITCHEROO_IGD; | |
207 | else | |
208 | return VGA_SWITCHEROO_DIS; | |
209 | } | |
210 | ||
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, | |
216 | }; | |
217 | ||
218 | static bool radeon_atpx_detect(void) | |
219 | { | |
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; | |
224 | int vga_count = 0; | |
225 | ||
226 | while ((pdev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, pdev)) != NULL) { | |
227 | vga_count++; | |
228 | ||
229 | has_atpx |= (radeon_atpx_pci_probe_handle(pdev) == true); | |
230 | } | |
231 | ||
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", | |
235 | acpi_method_name); | |
236 | radeon_atpx_priv.atpx_detected = true; | |
237 | return true; | |
238 | } | |
239 | return false; | |
240 | } | |
241 | ||
242 | void radeon_register_atpx_handler(void) | |
243 | { | |
244 | bool r; | |
245 | ||
246 | /* detect if we have any ATPX + 2 VGA in the system */ | |
247 | r = radeon_atpx_detect(); | |
248 | if (!r) | |
249 | return; | |
250 | ||
251 | vga_switcheroo_register_handler(&radeon_atpx_handler); | |
252 | } | |
253 | ||
254 | void radeon_unregister_atpx_handler(void) | |
255 | { | |
256 | vga_switcheroo_unregister_handler(); | |
257 | } |