]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - drivers/gpu/drm/radeon/radeon_atpx_handler.c
ACPI / driver core: Store an ACPI device pointer in struct acpi_dev_node
[mirror_ubuntu-artful-kernel.git] / drivers / gpu / drm / radeon / radeon_atpx_handler.c
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 */
9 #include <linux/vga_switcheroo.h>
10 #include <linux/slab.h>
11 #include <linux/acpi.h>
12 #include <linux/pci.h>
13
14 #include "radeon_acpi.h"
15
16 struct radeon_atpx_functions {
17 bool px_params;
18 bool power_cntl;
19 bool disp_mux_cntl;
20 bool i2c_mux_cntl;
21 bool switch_start;
22 bool switch_end;
23 bool disp_connectors_mapping;
24 bool disp_detetion_ports;
25 };
26
27 struct radeon_atpx {
28 acpi_handle handle;
29 struct radeon_atpx_functions functions;
30 };
31
32 static struct radeon_atpx_priv {
33 bool atpx_detected;
34 /* handle for device - and atpx */
35 acpi_handle dhandle;
36 struct radeon_atpx atpx;
37 } radeon_atpx_priv;
38
39 struct atpx_verify_interface {
40 u16 size; /* structure size in bytes (includes size field) */
41 u16 version; /* version */
42 u32 function_bits; /* supported functions bit vector */
43 } __packed;
44
45 struct atpx_px_params {
46 u16 size; /* structure size in bytes (includes size field) */
47 u32 valid_flags; /* which flags are valid */
48 u32 flags; /* flags */
49 } __packed;
50
51 struct atpx_power_control {
52 u16 size;
53 u8 dgpu_state;
54 } __packed;
55
56 struct atpx_mux {
57 u16 size;
58 u16 mux;
59 } __packed;
60
61 /**
62 * radeon_atpx_call - call an ATPX method
63 *
64 * @handle: acpi handle
65 * @function: the ATPX function to execute
66 * @params: ATPX function params
67 *
68 * Executes the requested ATPX function (all asics).
69 * Returns a pointer to the acpi output buffer.
70 */
71 static union acpi_object *radeon_atpx_call(acpi_handle handle, int function,
72 struct acpi_buffer *params)
73 {
74 acpi_status status;
75 union acpi_object atpx_arg_elements[2];
76 struct acpi_object_list atpx_arg;
77 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
78
79 atpx_arg.count = 2;
80 atpx_arg.pointer = &atpx_arg_elements[0];
81
82 atpx_arg_elements[0].type = ACPI_TYPE_INTEGER;
83 atpx_arg_elements[0].integer.value = function;
84
85 if (params) {
86 atpx_arg_elements[1].type = ACPI_TYPE_BUFFER;
87 atpx_arg_elements[1].buffer.length = params->length;
88 atpx_arg_elements[1].buffer.pointer = params->pointer;
89 } else {
90 /* We need a second fake parameter */
91 atpx_arg_elements[1].type = ACPI_TYPE_INTEGER;
92 atpx_arg_elements[1].integer.value = 0;
93 }
94
95 status = acpi_evaluate_object(handle, NULL, &atpx_arg, &buffer);
96
97 /* Fail only if calling the method fails and ATPX is supported */
98 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
99 printk("failed to evaluate ATPX got %s\n",
100 acpi_format_exception(status));
101 kfree(buffer.pointer);
102 return NULL;
103 }
104
105 return buffer.pointer;
106 }
107
108 /**
109 * radeon_atpx_parse_functions - parse supported functions
110 *
111 * @f: supported functions struct
112 * @mask: supported functions mask from ATPX
113 *
114 * Use the supported functions mask from ATPX function
115 * ATPX_FUNCTION_VERIFY_INTERFACE to determine what functions
116 * are supported (all asics).
117 */
118 static void radeon_atpx_parse_functions(struct radeon_atpx_functions *f, u32 mask)
119 {
120 f->px_params = mask & ATPX_GET_PX_PARAMETERS_SUPPORTED;
121 f->power_cntl = mask & ATPX_POWER_CONTROL_SUPPORTED;
122 f->disp_mux_cntl = mask & ATPX_DISPLAY_MUX_CONTROL_SUPPORTED;
123 f->i2c_mux_cntl = mask & ATPX_I2C_MUX_CONTROL_SUPPORTED;
124 f->switch_start = mask & ATPX_GRAPHICS_DEVICE_SWITCH_START_NOTIFICATION_SUPPORTED;
125 f->switch_end = mask & ATPX_GRAPHICS_DEVICE_SWITCH_END_NOTIFICATION_SUPPORTED;
126 f->disp_connectors_mapping = mask & ATPX_GET_DISPLAY_CONNECTORS_MAPPING_SUPPORTED;
127 f->disp_detetion_ports = mask & ATPX_GET_DISPLAY_DETECTION_PORTS_SUPPORTED;
128 }
129
130 /**
131 * radeon_atpx_validate_functions - validate ATPX functions
132 *
133 * @atpx: radeon atpx struct
134 *
135 * Validate that required functions are enabled (all asics).
136 * returns 0 on success, error on failure.
137 */
138 static int radeon_atpx_validate(struct radeon_atpx *atpx)
139 {
140 /* make sure required functions are enabled */
141 /* dGPU power control is required */
142 atpx->functions.power_cntl = true;
143
144 if (atpx->functions.px_params) {
145 union acpi_object *info;
146 struct atpx_px_params output;
147 size_t size;
148 u32 valid_bits;
149
150 info = radeon_atpx_call(atpx->handle, ATPX_FUNCTION_GET_PX_PARAMETERS, NULL);
151 if (!info)
152 return -EIO;
153
154 memset(&output, 0, sizeof(output));
155
156 size = *(u16 *) info->buffer.pointer;
157 if (size < 10) {
158 printk("ATPX buffer is too small: %zu\n", size);
159 kfree(info);
160 return -EINVAL;
161 }
162 size = min(sizeof(output), size);
163
164 memcpy(&output, info->buffer.pointer, size);
165
166 valid_bits = output.flags & output.valid_flags;
167 /* if separate mux flag is set, mux controls are required */
168 if (valid_bits & ATPX_SEPARATE_MUX_FOR_I2C) {
169 atpx->functions.i2c_mux_cntl = true;
170 atpx->functions.disp_mux_cntl = true;
171 }
172 /* if any outputs are muxed, mux controls are required */
173 if (valid_bits & (ATPX_CRT1_RGB_SIGNAL_MUXED |
174 ATPX_TV_SIGNAL_MUXED |
175 ATPX_DFP_SIGNAL_MUXED))
176 atpx->functions.disp_mux_cntl = true;
177
178 kfree(info);
179 }
180 return 0;
181 }
182
183 /**
184 * radeon_atpx_verify_interface - verify ATPX
185 *
186 * @atpx: radeon atpx struct
187 *
188 * Execute the ATPX_FUNCTION_VERIFY_INTERFACE ATPX function
189 * to initialize ATPX and determine what features are supported
190 * (all asics).
191 * returns 0 on success, error on failure.
192 */
193 static int radeon_atpx_verify_interface(struct radeon_atpx *atpx)
194 {
195 union acpi_object *info;
196 struct atpx_verify_interface output;
197 size_t size;
198 int err = 0;
199
200 info = radeon_atpx_call(atpx->handle, ATPX_FUNCTION_VERIFY_INTERFACE, NULL);
201 if (!info)
202 return -EIO;
203
204 memset(&output, 0, sizeof(output));
205
206 size = *(u16 *) info->buffer.pointer;
207 if (size < 8) {
208 printk("ATPX buffer is too small: %zu\n", size);
209 err = -EINVAL;
210 goto out;
211 }
212 size = min(sizeof(output), size);
213
214 memcpy(&output, info->buffer.pointer, size);
215
216 /* TODO: check version? */
217 printk("ATPX version %u\n", output.version);
218
219 radeon_atpx_parse_functions(&atpx->functions, output.function_bits);
220
221 out:
222 kfree(info);
223 return err;
224 }
225
226 /**
227 * radeon_atpx_set_discrete_state - power up/down discrete GPU
228 *
229 * @atpx: atpx info struct
230 * @state: discrete GPU state (0 = power down, 1 = power up)
231 *
232 * Execute the ATPX_FUNCTION_POWER_CONTROL ATPX function to
233 * power down/up the discrete GPU (all asics).
234 * Returns 0 on success, error on failure.
235 */
236 static int radeon_atpx_set_discrete_state(struct radeon_atpx *atpx, u8 state)
237 {
238 struct acpi_buffer params;
239 union acpi_object *info;
240 struct atpx_power_control input;
241
242 if (atpx->functions.power_cntl) {
243 input.size = 3;
244 input.dgpu_state = state;
245 params.length = input.size;
246 params.pointer = &input;
247 info = radeon_atpx_call(atpx->handle,
248 ATPX_FUNCTION_POWER_CONTROL,
249 &params);
250 if (!info)
251 return -EIO;
252 kfree(info);
253 }
254 return 0;
255 }
256
257 /**
258 * radeon_atpx_switch_disp_mux - switch display mux
259 *
260 * @atpx: atpx info struct
261 * @mux_id: mux state (0 = integrated GPU, 1 = discrete GPU)
262 *
263 * Execute the ATPX_FUNCTION_DISPLAY_MUX_CONTROL ATPX function to
264 * switch the display mux between the discrete GPU and integrated GPU
265 * (all asics).
266 * Returns 0 on success, error on failure.
267 */
268 static int radeon_atpx_switch_disp_mux(struct radeon_atpx *atpx, u16 mux_id)
269 {
270 struct acpi_buffer params;
271 union acpi_object *info;
272 struct atpx_mux input;
273
274 if (atpx->functions.disp_mux_cntl) {
275 input.size = 4;
276 input.mux = mux_id;
277 params.length = input.size;
278 params.pointer = &input;
279 info = radeon_atpx_call(atpx->handle,
280 ATPX_FUNCTION_DISPLAY_MUX_CONTROL,
281 &params);
282 if (!info)
283 return -EIO;
284 kfree(info);
285 }
286 return 0;
287 }
288
289 /**
290 * radeon_atpx_switch_i2c_mux - switch i2c/hpd mux
291 *
292 * @atpx: atpx info struct
293 * @mux_id: mux state (0 = integrated GPU, 1 = discrete GPU)
294 *
295 * Execute the ATPX_FUNCTION_I2C_MUX_CONTROL ATPX function to
296 * switch the i2c/hpd mux between the discrete GPU and integrated GPU
297 * (all asics).
298 * Returns 0 on success, error on failure.
299 */
300 static int radeon_atpx_switch_i2c_mux(struct radeon_atpx *atpx, u16 mux_id)
301 {
302 struct acpi_buffer params;
303 union acpi_object *info;
304 struct atpx_mux input;
305
306 if (atpx->functions.i2c_mux_cntl) {
307 input.size = 4;
308 input.mux = mux_id;
309 params.length = input.size;
310 params.pointer = &input;
311 info = radeon_atpx_call(atpx->handle,
312 ATPX_FUNCTION_I2C_MUX_CONTROL,
313 &params);
314 if (!info)
315 return -EIO;
316 kfree(info);
317 }
318 return 0;
319 }
320
321 /**
322 * radeon_atpx_switch_start - notify the sbios of a GPU switch
323 *
324 * @atpx: atpx info struct
325 * @mux_id: mux state (0 = integrated GPU, 1 = discrete GPU)
326 *
327 * Execute the ATPX_FUNCTION_GRAPHICS_DEVICE_SWITCH_START_NOTIFICATION ATPX
328 * function to notify the sbios that a switch between the discrete GPU and
329 * integrated GPU has begun (all asics).
330 * Returns 0 on success, error on failure.
331 */
332 static int radeon_atpx_switch_start(struct radeon_atpx *atpx, u16 mux_id)
333 {
334 struct acpi_buffer params;
335 union acpi_object *info;
336 struct atpx_mux input;
337
338 if (atpx->functions.switch_start) {
339 input.size = 4;
340 input.mux = mux_id;
341 params.length = input.size;
342 params.pointer = &input;
343 info = radeon_atpx_call(atpx->handle,
344 ATPX_FUNCTION_GRAPHICS_DEVICE_SWITCH_START_NOTIFICATION,
345 &params);
346 if (!info)
347 return -EIO;
348 kfree(info);
349 }
350 return 0;
351 }
352
353 /**
354 * radeon_atpx_switch_end - notify the sbios of a GPU switch
355 *
356 * @atpx: atpx info struct
357 * @mux_id: mux state (0 = integrated GPU, 1 = discrete GPU)
358 *
359 * Execute the ATPX_FUNCTION_GRAPHICS_DEVICE_SWITCH_END_NOTIFICATION ATPX
360 * function to notify the sbios that a switch between the discrete GPU and
361 * integrated GPU has ended (all asics).
362 * Returns 0 on success, error on failure.
363 */
364 static int radeon_atpx_switch_end(struct radeon_atpx *atpx, u16 mux_id)
365 {
366 struct acpi_buffer params;
367 union acpi_object *info;
368 struct atpx_mux input;
369
370 if (atpx->functions.switch_end) {
371 input.size = 4;
372 input.mux = mux_id;
373 params.length = input.size;
374 params.pointer = &input;
375 info = radeon_atpx_call(atpx->handle,
376 ATPX_FUNCTION_GRAPHICS_DEVICE_SWITCH_END_NOTIFICATION,
377 &params);
378 if (!info)
379 return -EIO;
380 kfree(info);
381 }
382 return 0;
383 }
384
385 /**
386 * radeon_atpx_switchto - switch to the requested GPU
387 *
388 * @id: GPU to switch to
389 *
390 * Execute the necessary ATPX functions to switch between the discrete GPU and
391 * integrated GPU (all asics).
392 * Returns 0 on success, error on failure.
393 */
394 static int radeon_atpx_switchto(enum vga_switcheroo_client_id id)
395 {
396 u16 gpu_id;
397
398 if (id == VGA_SWITCHEROO_IGD)
399 gpu_id = ATPX_INTEGRATED_GPU;
400 else
401 gpu_id = ATPX_DISCRETE_GPU;
402
403 radeon_atpx_switch_start(&radeon_atpx_priv.atpx, gpu_id);
404 radeon_atpx_switch_disp_mux(&radeon_atpx_priv.atpx, gpu_id);
405 radeon_atpx_switch_i2c_mux(&radeon_atpx_priv.atpx, gpu_id);
406 radeon_atpx_switch_end(&radeon_atpx_priv.atpx, gpu_id);
407
408 return 0;
409 }
410
411 /**
412 * radeon_atpx_power_state - power down/up the requested GPU
413 *
414 * @id: GPU to power down/up
415 * @state: requested power state (0 = off, 1 = on)
416 *
417 * Execute the necessary ATPX function to power down/up the discrete GPU
418 * (all asics).
419 * Returns 0 on success, error on failure.
420 */
421 static int radeon_atpx_power_state(enum vga_switcheroo_client_id id,
422 enum vga_switcheroo_state state)
423 {
424 /* on w500 ACPI can't change intel gpu state */
425 if (id == VGA_SWITCHEROO_IGD)
426 return 0;
427
428 radeon_atpx_set_discrete_state(&radeon_atpx_priv.atpx, state);
429 return 0;
430 }
431
432 /**
433 * radeon_atpx_pci_probe_handle - look up the ATPX handle
434 *
435 * @pdev: pci device
436 *
437 * Look up the ATPX handles (all asics).
438 * Returns true if the handles are found, false if not.
439 */
440 static bool radeon_atpx_pci_probe_handle(struct pci_dev *pdev)
441 {
442 acpi_handle dhandle, atpx_handle;
443 acpi_status status;
444
445 dhandle = DEVICE_ACPI_HANDLE(&pdev->dev);
446 if (!dhandle)
447 return false;
448
449 status = acpi_get_handle(dhandle, "ATPX", &atpx_handle);
450 if (ACPI_FAILURE(status))
451 return false;
452
453 radeon_atpx_priv.dhandle = dhandle;
454 radeon_atpx_priv.atpx.handle = atpx_handle;
455 return true;
456 }
457
458 /**
459 * radeon_atpx_init - verify the ATPX interface
460 *
461 * Verify the ATPX interface (all asics).
462 * Returns 0 on success, error on failure.
463 */
464 static int radeon_atpx_init(void)
465 {
466 int r;
467
468 /* set up the ATPX handle */
469 r = radeon_atpx_verify_interface(&radeon_atpx_priv.atpx);
470 if (r)
471 return r;
472
473 /* validate the atpx setup */
474 r = radeon_atpx_validate(&radeon_atpx_priv.atpx);
475 if (r)
476 return r;
477
478 return 0;
479 }
480
481 /**
482 * radeon_atpx_get_client_id - get the client id
483 *
484 * @pdev: pci device
485 *
486 * look up whether we are the integrated or discrete GPU (all asics).
487 * Returns the client id.
488 */
489 static int radeon_atpx_get_client_id(struct pci_dev *pdev)
490 {
491 if (radeon_atpx_priv.dhandle == DEVICE_ACPI_HANDLE(&pdev->dev))
492 return VGA_SWITCHEROO_IGD;
493 else
494 return VGA_SWITCHEROO_DIS;
495 }
496
497 static struct vga_switcheroo_handler radeon_atpx_handler = {
498 .switchto = radeon_atpx_switchto,
499 .power_state = radeon_atpx_power_state,
500 .init = radeon_atpx_init,
501 .get_client_id = radeon_atpx_get_client_id,
502 };
503
504 /**
505 * radeon_atpx_detect - detect whether we have PX
506 *
507 * Check if we have a PX system (all asics).
508 * Returns true if we have a PX system, false if not.
509 */
510 static bool radeon_atpx_detect(void)
511 {
512 char acpi_method_name[255] = { 0 };
513 struct acpi_buffer buffer = {sizeof(acpi_method_name), acpi_method_name};
514 struct pci_dev *pdev = NULL;
515 bool has_atpx = false;
516 int vga_count = 0;
517
518 while ((pdev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, pdev)) != NULL) {
519 vga_count++;
520
521 has_atpx |= (radeon_atpx_pci_probe_handle(pdev) == true);
522 }
523
524 if (has_atpx && vga_count == 2) {
525 acpi_get_name(radeon_atpx_priv.atpx.handle, ACPI_FULL_PATHNAME, &buffer);
526 printk(KERN_INFO "VGA switcheroo: detected switching method %s handle\n",
527 acpi_method_name);
528 radeon_atpx_priv.atpx_detected = true;
529 return true;
530 }
531 return false;
532 }
533
534 /**
535 * radeon_register_atpx_handler - register with vga_switcheroo
536 *
537 * Register the PX callbacks with vga_switcheroo (all asics).
538 */
539 void radeon_register_atpx_handler(void)
540 {
541 bool r;
542
543 /* detect if we have any ATPX + 2 VGA in the system */
544 r = radeon_atpx_detect();
545 if (!r)
546 return;
547
548 vga_switcheroo_register_handler(&radeon_atpx_handler);
549 }
550
551 /**
552 * radeon_unregister_atpx_handler - unregister with vga_switcheroo
553 *
554 * Unregister the PX callbacks with vga_switcheroo (all asics).
555 */
556 void radeon_unregister_atpx_handler(void)
557 {
558 vga_switcheroo_unregister_handler();
559 }