Loading...
1/*
2 * Copyright 2012 Advanced Micro Devices, Inc.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
21 *
22 */
23
24#include <linux/pci.h>
25#include <linux/acpi.h>
26#include <linux/slab.h>
27#include <linux/power_supply.h>
28#include <acpi/video.h>
29#include <drm/drmP.h>
30#include <drm/drm_crtc_helper.h>
31#include "radeon.h"
32#include "radeon_acpi.h"
33#include "atom.h"
34
35#define ACPI_AC_CLASS "ac_adapter"
36
37extern void radeon_pm_acpi_event_handler(struct radeon_device *rdev);
38
39struct atif_verify_interface {
40 u16 size; /* structure size in bytes (includes size field) */
41 u16 version; /* version */
42 u32 notification_mask; /* supported notifications mask */
43 u32 function_bits; /* supported functions bit vector */
44} __packed;
45
46struct atif_system_params {
47 u16 size; /* structure size in bytes (includes size field) */
48 u32 valid_mask; /* valid flags mask */
49 u32 flags; /* flags */
50 u8 command_code; /* notify command code */
51} __packed;
52
53struct atif_sbios_requests {
54 u16 size; /* structure size in bytes (includes size field) */
55 u32 pending; /* pending sbios requests */
56 u8 panel_exp_mode; /* panel expansion mode */
57 u8 thermal_gfx; /* thermal state: target gfx controller */
58 u8 thermal_state; /* thermal state: state id (0: exit state, non-0: state) */
59 u8 forced_power_gfx; /* forced power state: target gfx controller */
60 u8 forced_power_state; /* forced power state: state id */
61 u8 system_power_src; /* system power source */
62 u8 backlight_level; /* panel backlight level (0-255) */
63} __packed;
64
65#define ATIF_NOTIFY_MASK 0x3
66#define ATIF_NOTIFY_NONE 0
67#define ATIF_NOTIFY_81 1
68#define ATIF_NOTIFY_N 2
69
70struct atcs_verify_interface {
71 u16 size; /* structure size in bytes (includes size field) */
72 u16 version; /* version */
73 u32 function_bits; /* supported functions bit vector */
74} __packed;
75
76#define ATCS_VALID_FLAGS_MASK 0x3
77
78struct atcs_pref_req_input {
79 u16 size; /* structure size in bytes (includes size field) */
80 u16 client_id; /* client id (bit 2-0: func num, 7-3: dev num, 15-8: bus num) */
81 u16 valid_flags_mask; /* valid flags mask */
82 u16 flags; /* flags */
83 u8 req_type; /* request type */
84 u8 perf_req; /* performance request */
85} __packed;
86
87struct atcs_pref_req_output {
88 u16 size; /* structure size in bytes (includes size field) */
89 u8 ret_val; /* return value */
90} __packed;
91
92/* Call the ATIF method
93 */
94/**
95 * radeon_atif_call - call an ATIF method
96 *
97 * @handle: acpi handle
98 * @function: the ATIF function to execute
99 * @params: ATIF function params
100 *
101 * Executes the requested ATIF function (all asics).
102 * Returns a pointer to the acpi output buffer.
103 */
104static union acpi_object *radeon_atif_call(acpi_handle handle, int function,
105 struct acpi_buffer *params)
106{
107 acpi_status status;
108 union acpi_object atif_arg_elements[2];
109 struct acpi_object_list atif_arg;
110 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
111
112 atif_arg.count = 2;
113 atif_arg.pointer = &atif_arg_elements[0];
114
115 atif_arg_elements[0].type = ACPI_TYPE_INTEGER;
116 atif_arg_elements[0].integer.value = function;
117
118 if (params) {
119 atif_arg_elements[1].type = ACPI_TYPE_BUFFER;
120 atif_arg_elements[1].buffer.length = params->length;
121 atif_arg_elements[1].buffer.pointer = params->pointer;
122 } else {
123 /* We need a second fake parameter */
124 atif_arg_elements[1].type = ACPI_TYPE_INTEGER;
125 atif_arg_elements[1].integer.value = 0;
126 }
127
128 status = acpi_evaluate_object(handle, "ATIF", &atif_arg, &buffer);
129
130 /* Fail only if calling the method fails and ATIF is supported */
131 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
132 DRM_DEBUG_DRIVER("failed to evaluate ATIF got %s\n",
133 acpi_format_exception(status));
134 kfree(buffer.pointer);
135 return NULL;
136 }
137
138 return buffer.pointer;
139}
140
141/**
142 * radeon_atif_parse_notification - parse supported notifications
143 *
144 * @n: supported notifications struct
145 * @mask: supported notifications mask from ATIF
146 *
147 * Use the supported notifications mask from ATIF function
148 * ATIF_FUNCTION_VERIFY_INTERFACE to determine what notifications
149 * are supported (all asics).
150 */
151static void radeon_atif_parse_notification(struct radeon_atif_notifications *n, u32 mask)
152{
153 n->display_switch = mask & ATIF_DISPLAY_SWITCH_REQUEST_SUPPORTED;
154 n->expansion_mode_change = mask & ATIF_EXPANSION_MODE_CHANGE_REQUEST_SUPPORTED;
155 n->thermal_state = mask & ATIF_THERMAL_STATE_CHANGE_REQUEST_SUPPORTED;
156 n->forced_power_state = mask & ATIF_FORCED_POWER_STATE_CHANGE_REQUEST_SUPPORTED;
157 n->system_power_state = mask & ATIF_SYSTEM_POWER_SOURCE_CHANGE_REQUEST_SUPPORTED;
158 n->display_conf_change = mask & ATIF_DISPLAY_CONF_CHANGE_REQUEST_SUPPORTED;
159 n->px_gfx_switch = mask & ATIF_PX_GFX_SWITCH_REQUEST_SUPPORTED;
160 n->brightness_change = mask & ATIF_PANEL_BRIGHTNESS_CHANGE_REQUEST_SUPPORTED;
161 n->dgpu_display_event = mask & ATIF_DGPU_DISPLAY_EVENT_SUPPORTED;
162}
163
164/**
165 * radeon_atif_parse_functions - parse supported functions
166 *
167 * @f: supported functions struct
168 * @mask: supported functions mask from ATIF
169 *
170 * Use the supported functions mask from ATIF function
171 * ATIF_FUNCTION_VERIFY_INTERFACE to determine what functions
172 * are supported (all asics).
173 */
174static void radeon_atif_parse_functions(struct radeon_atif_functions *f, u32 mask)
175{
176 f->system_params = mask & ATIF_GET_SYSTEM_PARAMETERS_SUPPORTED;
177 f->sbios_requests = mask & ATIF_GET_SYSTEM_BIOS_REQUESTS_SUPPORTED;
178 f->select_active_disp = mask & ATIF_SELECT_ACTIVE_DISPLAYS_SUPPORTED;
179 f->lid_state = mask & ATIF_GET_LID_STATE_SUPPORTED;
180 f->get_tv_standard = mask & ATIF_GET_TV_STANDARD_FROM_CMOS_SUPPORTED;
181 f->set_tv_standard = mask & ATIF_SET_TV_STANDARD_IN_CMOS_SUPPORTED;
182 f->get_panel_expansion_mode = mask & ATIF_GET_PANEL_EXPANSION_MODE_FROM_CMOS_SUPPORTED;
183 f->set_panel_expansion_mode = mask & ATIF_SET_PANEL_EXPANSION_MODE_IN_CMOS_SUPPORTED;
184 f->temperature_change = mask & ATIF_TEMPERATURE_CHANGE_NOTIFICATION_SUPPORTED;
185 f->graphics_device_types = mask & ATIF_GET_GRAPHICS_DEVICE_TYPES_SUPPORTED;
186}
187
188/**
189 * radeon_atif_verify_interface - verify ATIF
190 *
191 * @handle: acpi handle
192 * @atif: radeon atif struct
193 *
194 * Execute the ATIF_FUNCTION_VERIFY_INTERFACE ATIF function
195 * to initialize ATIF and determine what features are supported
196 * (all asics).
197 * returns 0 on success, error on failure.
198 */
199static int radeon_atif_verify_interface(acpi_handle handle,
200 struct radeon_atif *atif)
201{
202 union acpi_object *info;
203 struct atif_verify_interface output;
204 size_t size;
205 int err = 0;
206
207 info = radeon_atif_call(handle, ATIF_FUNCTION_VERIFY_INTERFACE, NULL);
208 if (!info)
209 return -EIO;
210
211 memset(&output, 0, sizeof(output));
212
213 size = *(u16 *) info->buffer.pointer;
214 if (size < 12) {
215 DRM_INFO("ATIF buffer is too small: %zu\n", size);
216 err = -EINVAL;
217 goto out;
218 }
219 size = min(sizeof(output), size);
220
221 memcpy(&output, info->buffer.pointer, size);
222
223 /* TODO: check version? */
224 DRM_DEBUG_DRIVER("ATIF version %u\n", output.version);
225
226 radeon_atif_parse_notification(&atif->notifications, output.notification_mask);
227 radeon_atif_parse_functions(&atif->functions, output.function_bits);
228
229out:
230 kfree(info);
231 return err;
232}
233
234/**
235 * radeon_atif_get_notification_params - determine notify configuration
236 *
237 * @handle: acpi handle
238 * @n: atif notification configuration struct
239 *
240 * Execute the ATIF_FUNCTION_GET_SYSTEM_PARAMETERS ATIF function
241 * to determine if a notifier is used and if so which one
242 * (all asics). This is either Notify(VGA, 0x81) or Notify(VGA, n)
243 * where n is specified in the result if a notifier is used.
244 * Returns 0 on success, error on failure.
245 */
246static int radeon_atif_get_notification_params(acpi_handle handle,
247 struct radeon_atif_notification_cfg *n)
248{
249 union acpi_object *info;
250 struct atif_system_params params;
251 size_t size;
252 int err = 0;
253
254 info = radeon_atif_call(handle, ATIF_FUNCTION_GET_SYSTEM_PARAMETERS, NULL);
255 if (!info) {
256 err = -EIO;
257 goto out;
258 }
259
260 size = *(u16 *) info->buffer.pointer;
261 if (size < 10) {
262 err = -EINVAL;
263 goto out;
264 }
265
266 memset(¶ms, 0, sizeof(params));
267 size = min(sizeof(params), size);
268 memcpy(¶ms, info->buffer.pointer, size);
269
270 DRM_DEBUG_DRIVER("SYSTEM_PARAMS: mask = %#x, flags = %#x\n",
271 params.flags, params.valid_mask);
272 params.flags = params.flags & params.valid_mask;
273
274 if ((params.flags & ATIF_NOTIFY_MASK) == ATIF_NOTIFY_NONE) {
275 n->enabled = false;
276 n->command_code = 0;
277 } else if ((params.flags & ATIF_NOTIFY_MASK) == ATIF_NOTIFY_81) {
278 n->enabled = true;
279 n->command_code = 0x81;
280 } else {
281 if (size < 11) {
282 err = -EINVAL;
283 goto out;
284 }
285 n->enabled = true;
286 n->command_code = params.command_code;
287 }
288
289out:
290 DRM_DEBUG_DRIVER("Notification %s, command code = %#x\n",
291 (n->enabled ? "enabled" : "disabled"),
292 n->command_code);
293 kfree(info);
294 return err;
295}
296
297/**
298 * radeon_atif_get_sbios_requests - get requested sbios event
299 *
300 * @handle: acpi handle
301 * @req: atif sbios request struct
302 *
303 * Execute the ATIF_FUNCTION_GET_SYSTEM_BIOS_REQUESTS ATIF function
304 * to determine what requests the sbios is making to the driver
305 * (all asics).
306 * Returns 0 on success, error on failure.
307 */
308static int radeon_atif_get_sbios_requests(acpi_handle handle,
309 struct atif_sbios_requests *req)
310{
311 union acpi_object *info;
312 size_t size;
313 int count = 0;
314
315 info = radeon_atif_call(handle, ATIF_FUNCTION_GET_SYSTEM_BIOS_REQUESTS, NULL);
316 if (!info)
317 return -EIO;
318
319 size = *(u16 *)info->buffer.pointer;
320 if (size < 0xd) {
321 count = -EINVAL;
322 goto out;
323 }
324 memset(req, 0, sizeof(*req));
325
326 size = min(sizeof(*req), size);
327 memcpy(req, info->buffer.pointer, size);
328 DRM_DEBUG_DRIVER("SBIOS pending requests: %#x\n", req->pending);
329
330 count = hweight32(req->pending);
331
332out:
333 kfree(info);
334 return count;
335}
336
337/**
338 * radeon_atif_handler - handle ATIF notify requests
339 *
340 * @rdev: radeon_device pointer
341 * @event: atif sbios request struct
342 *
343 * Checks the acpi event and if it matches an atif event,
344 * handles it.
345 * Returns NOTIFY code
346 */
347int radeon_atif_handler(struct radeon_device *rdev,
348 struct acpi_bus_event *event)
349{
350 struct radeon_atif *atif = &rdev->atif;
351 struct atif_sbios_requests req;
352 acpi_handle handle;
353 int count;
354
355 DRM_DEBUG_DRIVER("event, device_class = %s, type = %#x\n",
356 event->device_class, event->type);
357
358 if (strcmp(event->device_class, ACPI_VIDEO_CLASS) != 0)
359 return NOTIFY_DONE;
360
361 if (!atif->notification_cfg.enabled ||
362 event->type != atif->notification_cfg.command_code)
363 /* Not our event */
364 return NOTIFY_DONE;
365
366 /* Check pending SBIOS requests */
367 handle = ACPI_HANDLE(&rdev->pdev->dev);
368 count = radeon_atif_get_sbios_requests(handle, &req);
369
370 if (count <= 0)
371 return NOTIFY_DONE;
372
373 DRM_DEBUG_DRIVER("ATIF: %d pending SBIOS requests\n", count);
374
375 if (req.pending & ATIF_PANEL_BRIGHTNESS_CHANGE_REQUEST) {
376 struct radeon_encoder *enc = atif->encoder_for_bl;
377
378 if (enc) {
379 DRM_DEBUG_DRIVER("Changing brightness to %d\n",
380 req.backlight_level);
381
382 radeon_set_backlight_level(rdev, enc, req.backlight_level);
383
384#if defined(CONFIG_BACKLIGHT_CLASS_DEVICE) || defined(CONFIG_BACKLIGHT_CLASS_DEVICE_MODULE)
385 if (rdev->is_atom_bios) {
386 struct radeon_encoder_atom_dig *dig = enc->enc_priv;
387 backlight_force_update(dig->bl_dev,
388 BACKLIGHT_UPDATE_HOTKEY);
389 } else {
390 struct radeon_encoder_lvds *dig = enc->enc_priv;
391 backlight_force_update(dig->bl_dev,
392 BACKLIGHT_UPDATE_HOTKEY);
393 }
394#endif
395 }
396 }
397 /* TODO: check other events */
398
399 /* We've handled the event, stop the notifier chain. The ACPI interface
400 * overloads ACPI_VIDEO_NOTIFY_PROBE, we don't want to send that to
401 * userspace if the event was generated only to signal a SBIOS
402 * request.
403 */
404 return NOTIFY_BAD;
405}
406
407/* Call the ATCS method
408 */
409/**
410 * radeon_atcs_call - call an ATCS method
411 *
412 * @handle: acpi handle
413 * @function: the ATCS function to execute
414 * @params: ATCS function params
415 *
416 * Executes the requested ATCS function (all asics).
417 * Returns a pointer to the acpi output buffer.
418 */
419static union acpi_object *radeon_atcs_call(acpi_handle handle, int function,
420 struct acpi_buffer *params)
421{
422 acpi_status status;
423 union acpi_object atcs_arg_elements[2];
424 struct acpi_object_list atcs_arg;
425 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
426
427 atcs_arg.count = 2;
428 atcs_arg.pointer = &atcs_arg_elements[0];
429
430 atcs_arg_elements[0].type = ACPI_TYPE_INTEGER;
431 atcs_arg_elements[0].integer.value = function;
432
433 if (params) {
434 atcs_arg_elements[1].type = ACPI_TYPE_BUFFER;
435 atcs_arg_elements[1].buffer.length = params->length;
436 atcs_arg_elements[1].buffer.pointer = params->pointer;
437 } else {
438 /* We need a second fake parameter */
439 atcs_arg_elements[1].type = ACPI_TYPE_INTEGER;
440 atcs_arg_elements[1].integer.value = 0;
441 }
442
443 status = acpi_evaluate_object(handle, "ATCS", &atcs_arg, &buffer);
444
445 /* Fail only if calling the method fails and ATIF is supported */
446 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
447 DRM_DEBUG_DRIVER("failed to evaluate ATCS got %s\n",
448 acpi_format_exception(status));
449 kfree(buffer.pointer);
450 return NULL;
451 }
452
453 return buffer.pointer;
454}
455
456/**
457 * radeon_atcs_parse_functions - parse supported functions
458 *
459 * @f: supported functions struct
460 * @mask: supported functions mask from ATCS
461 *
462 * Use the supported functions mask from ATCS function
463 * ATCS_FUNCTION_VERIFY_INTERFACE to determine what functions
464 * are supported (all asics).
465 */
466static void radeon_atcs_parse_functions(struct radeon_atcs_functions *f, u32 mask)
467{
468 f->get_ext_state = mask & ATCS_GET_EXTERNAL_STATE_SUPPORTED;
469 f->pcie_perf_req = mask & ATCS_PCIE_PERFORMANCE_REQUEST_SUPPORTED;
470 f->pcie_dev_rdy = mask & ATCS_PCIE_DEVICE_READY_NOTIFICATION_SUPPORTED;
471 f->pcie_bus_width = mask & ATCS_SET_PCIE_BUS_WIDTH_SUPPORTED;
472}
473
474/**
475 * radeon_atcs_verify_interface - verify ATCS
476 *
477 * @handle: acpi handle
478 * @atcs: radeon atcs struct
479 *
480 * Execute the ATCS_FUNCTION_VERIFY_INTERFACE ATCS function
481 * to initialize ATCS and determine what features are supported
482 * (all asics).
483 * returns 0 on success, error on failure.
484 */
485static int radeon_atcs_verify_interface(acpi_handle handle,
486 struct radeon_atcs *atcs)
487{
488 union acpi_object *info;
489 struct atcs_verify_interface output;
490 size_t size;
491 int err = 0;
492
493 info = radeon_atcs_call(handle, ATCS_FUNCTION_VERIFY_INTERFACE, NULL);
494 if (!info)
495 return -EIO;
496
497 memset(&output, 0, sizeof(output));
498
499 size = *(u16 *) info->buffer.pointer;
500 if (size < 8) {
501 DRM_INFO("ATCS buffer is too small: %zu\n", size);
502 err = -EINVAL;
503 goto out;
504 }
505 size = min(sizeof(output), size);
506
507 memcpy(&output, info->buffer.pointer, size);
508
509 /* TODO: check version? */
510 DRM_DEBUG_DRIVER("ATCS version %u\n", output.version);
511
512 radeon_atcs_parse_functions(&atcs->functions, output.function_bits);
513
514out:
515 kfree(info);
516 return err;
517}
518
519/**
520 * radeon_acpi_is_pcie_performance_request_supported
521 *
522 * @rdev: radeon_device pointer
523 *
524 * Check if the ATCS pcie_perf_req and pcie_dev_rdy methods
525 * are supported (all asics).
526 * returns true if supported, false if not.
527 */
528bool radeon_acpi_is_pcie_performance_request_supported(struct radeon_device *rdev)
529{
530 struct radeon_atcs *atcs = &rdev->atcs;
531
532 if (atcs->functions.pcie_perf_req && atcs->functions.pcie_dev_rdy)
533 return true;
534
535 return false;
536}
537
538/**
539 * radeon_acpi_pcie_notify_device_ready
540 *
541 * @rdev: radeon_device pointer
542 *
543 * Executes the PCIE_DEVICE_READY_NOTIFICATION method
544 * (all asics).
545 * returns 0 on success, error on failure.
546 */
547int radeon_acpi_pcie_notify_device_ready(struct radeon_device *rdev)
548{
549 acpi_handle handle;
550 union acpi_object *info;
551 struct radeon_atcs *atcs = &rdev->atcs;
552
553 /* Get the device handle */
554 handle = ACPI_HANDLE(&rdev->pdev->dev);
555 if (!handle)
556 return -EINVAL;
557
558 if (!atcs->functions.pcie_dev_rdy)
559 return -EINVAL;
560
561 info = radeon_atcs_call(handle, ATCS_FUNCTION_PCIE_DEVICE_READY_NOTIFICATION, NULL);
562 if (!info)
563 return -EIO;
564
565 kfree(info);
566
567 return 0;
568}
569
570/**
571 * radeon_acpi_pcie_performance_request
572 *
573 * @rdev: radeon_device pointer
574 * @perf_req: requested perf level (pcie gen speed)
575 * @advertise: set advertise caps flag if set
576 *
577 * Executes the PCIE_PERFORMANCE_REQUEST method to
578 * change the pcie gen speed (all asics).
579 * returns 0 on success, error on failure.
580 */
581int radeon_acpi_pcie_performance_request(struct radeon_device *rdev,
582 u8 perf_req, bool advertise)
583{
584 acpi_handle handle;
585 union acpi_object *info;
586 struct radeon_atcs *atcs = &rdev->atcs;
587 struct atcs_pref_req_input atcs_input;
588 struct atcs_pref_req_output atcs_output;
589 struct acpi_buffer params;
590 size_t size;
591 u32 retry = 3;
592
593 /* Get the device handle */
594 handle = ACPI_HANDLE(&rdev->pdev->dev);
595 if (!handle)
596 return -EINVAL;
597
598 if (!atcs->functions.pcie_perf_req)
599 return -EINVAL;
600
601 atcs_input.size = sizeof(struct atcs_pref_req_input);
602 /* client id (bit 2-0: func num, 7-3: dev num, 15-8: bus num) */
603 atcs_input.client_id = rdev->pdev->devfn | (rdev->pdev->bus->number << 8);
604 atcs_input.valid_flags_mask = ATCS_VALID_FLAGS_MASK;
605 atcs_input.flags = ATCS_WAIT_FOR_COMPLETION;
606 if (advertise)
607 atcs_input.flags |= ATCS_ADVERTISE_CAPS;
608 atcs_input.req_type = ATCS_PCIE_LINK_SPEED;
609 atcs_input.perf_req = perf_req;
610
611 params.length = sizeof(struct atcs_pref_req_input);
612 params.pointer = &atcs_input;
613
614 while (retry--) {
615 info = radeon_atcs_call(handle, ATCS_FUNCTION_PCIE_PERFORMANCE_REQUEST, ¶ms);
616 if (!info)
617 return -EIO;
618
619 memset(&atcs_output, 0, sizeof(atcs_output));
620
621 size = *(u16 *) info->buffer.pointer;
622 if (size < 3) {
623 DRM_INFO("ATCS buffer is too small: %zu\n", size);
624 kfree(info);
625 return -EINVAL;
626 }
627 size = min(sizeof(atcs_output), size);
628
629 memcpy(&atcs_output, info->buffer.pointer, size);
630
631 kfree(info);
632
633 switch (atcs_output.ret_val) {
634 case ATCS_REQUEST_REFUSED:
635 default:
636 return -EINVAL;
637 case ATCS_REQUEST_COMPLETE:
638 return 0;
639 case ATCS_REQUEST_IN_PROGRESS:
640 udelay(10);
641 break;
642 }
643 }
644
645 return 0;
646}
647
648/**
649 * radeon_acpi_event - handle notify events
650 *
651 * @nb: notifier block
652 * @val: val
653 * @data: acpi event
654 *
655 * Calls relevant radeon functions in response to various
656 * acpi events.
657 * Returns NOTIFY code
658 */
659static int radeon_acpi_event(struct notifier_block *nb,
660 unsigned long val,
661 void *data)
662{
663 struct radeon_device *rdev = container_of(nb, struct radeon_device, acpi_nb);
664 struct acpi_bus_event *entry = (struct acpi_bus_event *)data;
665
666 if (strcmp(entry->device_class, ACPI_AC_CLASS) == 0) {
667 if (power_supply_is_system_supplied() > 0)
668 DRM_DEBUG_DRIVER("pm: AC\n");
669 else
670 DRM_DEBUG_DRIVER("pm: DC\n");
671
672 radeon_pm_acpi_event_handler(rdev);
673 }
674
675 /* Check for pending SBIOS requests */
676 return radeon_atif_handler(rdev, entry);
677}
678
679/* Call all ACPI methods here */
680/**
681 * radeon_acpi_init - init driver acpi support
682 *
683 * @rdev: radeon_device pointer
684 *
685 * Verifies the AMD ACPI interfaces and registers with the acpi
686 * notifier chain (all asics).
687 * Returns 0 on success, error on failure.
688 */
689int radeon_acpi_init(struct radeon_device *rdev)
690{
691 acpi_handle handle;
692 struct radeon_atif *atif = &rdev->atif;
693 struct radeon_atcs *atcs = &rdev->atcs;
694 int ret;
695
696 /* Get the device handle */
697 handle = ACPI_HANDLE(&rdev->pdev->dev);
698
699 /* No need to proceed if we're sure that ATIF is not supported */
700 if (!ASIC_IS_AVIVO(rdev) || !rdev->bios || !handle)
701 return 0;
702
703 /* Call the ATCS method */
704 ret = radeon_atcs_verify_interface(handle, atcs);
705 if (ret) {
706 DRM_DEBUG_DRIVER("Call to ATCS verify_interface failed: %d\n", ret);
707 }
708
709 /* Call the ATIF method */
710 ret = radeon_atif_verify_interface(handle, atif);
711 if (ret) {
712 DRM_DEBUG_DRIVER("Call to ATIF verify_interface failed: %d\n", ret);
713 goto out;
714 }
715
716 if (atif->notifications.brightness_change) {
717 struct drm_encoder *tmp;
718 struct radeon_encoder *target = NULL;
719
720 /* Find the encoder controlling the brightness */
721 list_for_each_entry(tmp, &rdev->ddev->mode_config.encoder_list,
722 head) {
723 struct radeon_encoder *enc = to_radeon_encoder(tmp);
724
725 if ((enc->devices & (ATOM_DEVICE_LCD_SUPPORT)) &&
726 enc->enc_priv) {
727 if (rdev->is_atom_bios) {
728 struct radeon_encoder_atom_dig *dig = enc->enc_priv;
729 if (dig->bl_dev) {
730 target = enc;
731 break;
732 }
733 } else {
734 struct radeon_encoder_lvds *dig = enc->enc_priv;
735 if (dig->bl_dev) {
736 target = enc;
737 break;
738 }
739 }
740 }
741 }
742
743 atif->encoder_for_bl = target;
744 if (!target) {
745 /* Brightness change notification is enabled, but we
746 * didn't find a backlight controller, this should
747 * never happen.
748 */
749 DRM_ERROR("Cannot find a backlight controller\n");
750 }
751 }
752
753 if (atif->functions.sbios_requests && !atif->functions.system_params) {
754 /* XXX check this workraround, if sbios request function is
755 * present we have to see how it's configured in the system
756 * params
757 */
758 atif->functions.system_params = true;
759 }
760
761 if (atif->functions.system_params) {
762 ret = radeon_atif_get_notification_params(handle,
763 &atif->notification_cfg);
764 if (ret) {
765 DRM_DEBUG_DRIVER("Call to GET_SYSTEM_PARAMS failed: %d\n",
766 ret);
767 /* Disable notification */
768 atif->notification_cfg.enabled = false;
769 }
770 }
771
772out:
773 rdev->acpi_nb.notifier_call = radeon_acpi_event;
774 register_acpi_notifier(&rdev->acpi_nb);
775
776 return ret;
777}
778
779/**
780 * radeon_acpi_fini - tear down driver acpi support
781 *
782 * @rdev: radeon_device pointer
783 *
784 * Unregisters with the acpi notifier chain (all asics).
785 */
786void radeon_acpi_fini(struct radeon_device *rdev)
787{
788 unregister_acpi_notifier(&rdev->acpi_nb);
789}
1/*
2 * Copyright 2012 Advanced Micro Devices, Inc.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
21 *
22 */
23
24#include <linux/pci.h>
25#include <linux/acpi.h>
26#include <linux/slab.h>
27#include <linux/power_supply.h>
28#include <linux/pm_runtime.h>
29#include <acpi/video.h>
30#include <drm/drmP.h>
31#include <drm/drm_crtc_helper.h>
32#include "radeon.h"
33#include "radeon_acpi.h"
34#include "atom.h"
35
36#if defined(CONFIG_VGA_SWITCHEROO)
37bool radeon_atpx_dgpu_req_power_for_displays(void);
38#else
39static inline bool radeon_atpx_dgpu_req_power_for_displays(void) { return false; }
40#endif
41
42#define ACPI_AC_CLASS "ac_adapter"
43
44extern void radeon_pm_acpi_event_handler(struct radeon_device *rdev);
45
46struct atif_verify_interface {
47 u16 size; /* structure size in bytes (includes size field) */
48 u16 version; /* version */
49 u32 notification_mask; /* supported notifications mask */
50 u32 function_bits; /* supported functions bit vector */
51} __packed;
52
53struct atif_system_params {
54 u16 size; /* structure size in bytes (includes size field) */
55 u32 valid_mask; /* valid flags mask */
56 u32 flags; /* flags */
57 u8 command_code; /* notify command code */
58} __packed;
59
60struct atif_sbios_requests {
61 u16 size; /* structure size in bytes (includes size field) */
62 u32 pending; /* pending sbios requests */
63 u8 panel_exp_mode; /* panel expansion mode */
64 u8 thermal_gfx; /* thermal state: target gfx controller */
65 u8 thermal_state; /* thermal state: state id (0: exit state, non-0: state) */
66 u8 forced_power_gfx; /* forced power state: target gfx controller */
67 u8 forced_power_state; /* forced power state: state id */
68 u8 system_power_src; /* system power source */
69 u8 backlight_level; /* panel backlight level (0-255) */
70} __packed;
71
72#define ATIF_NOTIFY_MASK 0x3
73#define ATIF_NOTIFY_NONE 0
74#define ATIF_NOTIFY_81 1
75#define ATIF_NOTIFY_N 2
76
77struct atcs_verify_interface {
78 u16 size; /* structure size in bytes (includes size field) */
79 u16 version; /* version */
80 u32 function_bits; /* supported functions bit vector */
81} __packed;
82
83#define ATCS_VALID_FLAGS_MASK 0x3
84
85struct atcs_pref_req_input {
86 u16 size; /* structure size in bytes (includes size field) */
87 u16 client_id; /* client id (bit 2-0: func num, 7-3: dev num, 15-8: bus num) */
88 u16 valid_flags_mask; /* valid flags mask */
89 u16 flags; /* flags */
90 u8 req_type; /* request type */
91 u8 perf_req; /* performance request */
92} __packed;
93
94struct atcs_pref_req_output {
95 u16 size; /* structure size in bytes (includes size field) */
96 u8 ret_val; /* return value */
97} __packed;
98
99/* Call the ATIF method
100 */
101/**
102 * radeon_atif_call - call an ATIF method
103 *
104 * @handle: acpi handle
105 * @function: the ATIF function to execute
106 * @params: ATIF function params
107 *
108 * Executes the requested ATIF function (all asics).
109 * Returns a pointer to the acpi output buffer.
110 */
111static union acpi_object *radeon_atif_call(acpi_handle handle, int function,
112 struct acpi_buffer *params)
113{
114 acpi_status status;
115 union acpi_object atif_arg_elements[2];
116 struct acpi_object_list atif_arg;
117 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
118
119 atif_arg.count = 2;
120 atif_arg.pointer = &atif_arg_elements[0];
121
122 atif_arg_elements[0].type = ACPI_TYPE_INTEGER;
123 atif_arg_elements[0].integer.value = function;
124
125 if (params) {
126 atif_arg_elements[1].type = ACPI_TYPE_BUFFER;
127 atif_arg_elements[1].buffer.length = params->length;
128 atif_arg_elements[1].buffer.pointer = params->pointer;
129 } else {
130 /* We need a second fake parameter */
131 atif_arg_elements[1].type = ACPI_TYPE_INTEGER;
132 atif_arg_elements[1].integer.value = 0;
133 }
134
135 status = acpi_evaluate_object(handle, "ATIF", &atif_arg, &buffer);
136
137 /* Fail only if calling the method fails and ATIF is supported */
138 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
139 DRM_DEBUG_DRIVER("failed to evaluate ATIF got %s\n",
140 acpi_format_exception(status));
141 kfree(buffer.pointer);
142 return NULL;
143 }
144
145 return buffer.pointer;
146}
147
148/**
149 * radeon_atif_parse_notification - parse supported notifications
150 *
151 * @n: supported notifications struct
152 * @mask: supported notifications mask from ATIF
153 *
154 * Use the supported notifications mask from ATIF function
155 * ATIF_FUNCTION_VERIFY_INTERFACE to determine what notifications
156 * are supported (all asics).
157 */
158static void radeon_atif_parse_notification(struct radeon_atif_notifications *n, u32 mask)
159{
160 n->display_switch = mask & ATIF_DISPLAY_SWITCH_REQUEST_SUPPORTED;
161 n->expansion_mode_change = mask & ATIF_EXPANSION_MODE_CHANGE_REQUEST_SUPPORTED;
162 n->thermal_state = mask & ATIF_THERMAL_STATE_CHANGE_REQUEST_SUPPORTED;
163 n->forced_power_state = mask & ATIF_FORCED_POWER_STATE_CHANGE_REQUEST_SUPPORTED;
164 n->system_power_state = mask & ATIF_SYSTEM_POWER_SOURCE_CHANGE_REQUEST_SUPPORTED;
165 n->display_conf_change = mask & ATIF_DISPLAY_CONF_CHANGE_REQUEST_SUPPORTED;
166 n->px_gfx_switch = mask & ATIF_PX_GFX_SWITCH_REQUEST_SUPPORTED;
167 n->brightness_change = mask & ATIF_PANEL_BRIGHTNESS_CHANGE_REQUEST_SUPPORTED;
168 n->dgpu_display_event = mask & ATIF_DGPU_DISPLAY_EVENT_SUPPORTED;
169}
170
171/**
172 * radeon_atif_parse_functions - parse supported functions
173 *
174 * @f: supported functions struct
175 * @mask: supported functions mask from ATIF
176 *
177 * Use the supported functions mask from ATIF function
178 * ATIF_FUNCTION_VERIFY_INTERFACE to determine what functions
179 * are supported (all asics).
180 */
181static void radeon_atif_parse_functions(struct radeon_atif_functions *f, u32 mask)
182{
183 f->system_params = mask & ATIF_GET_SYSTEM_PARAMETERS_SUPPORTED;
184 f->sbios_requests = mask & ATIF_GET_SYSTEM_BIOS_REQUESTS_SUPPORTED;
185 f->select_active_disp = mask & ATIF_SELECT_ACTIVE_DISPLAYS_SUPPORTED;
186 f->lid_state = mask & ATIF_GET_LID_STATE_SUPPORTED;
187 f->get_tv_standard = mask & ATIF_GET_TV_STANDARD_FROM_CMOS_SUPPORTED;
188 f->set_tv_standard = mask & ATIF_SET_TV_STANDARD_IN_CMOS_SUPPORTED;
189 f->get_panel_expansion_mode = mask & ATIF_GET_PANEL_EXPANSION_MODE_FROM_CMOS_SUPPORTED;
190 f->set_panel_expansion_mode = mask & ATIF_SET_PANEL_EXPANSION_MODE_IN_CMOS_SUPPORTED;
191 f->temperature_change = mask & ATIF_TEMPERATURE_CHANGE_NOTIFICATION_SUPPORTED;
192 f->graphics_device_types = mask & ATIF_GET_GRAPHICS_DEVICE_TYPES_SUPPORTED;
193}
194
195/**
196 * radeon_atif_verify_interface - verify ATIF
197 *
198 * @handle: acpi handle
199 * @atif: radeon atif struct
200 *
201 * Execute the ATIF_FUNCTION_VERIFY_INTERFACE ATIF function
202 * to initialize ATIF and determine what features are supported
203 * (all asics).
204 * returns 0 on success, error on failure.
205 */
206static int radeon_atif_verify_interface(acpi_handle handle,
207 struct radeon_atif *atif)
208{
209 union acpi_object *info;
210 struct atif_verify_interface output;
211 size_t size;
212 int err = 0;
213
214 info = radeon_atif_call(handle, ATIF_FUNCTION_VERIFY_INTERFACE, NULL);
215 if (!info)
216 return -EIO;
217
218 memset(&output, 0, sizeof(output));
219
220 size = *(u16 *) info->buffer.pointer;
221 if (size < 12) {
222 DRM_INFO("ATIF buffer is too small: %zu\n", size);
223 err = -EINVAL;
224 goto out;
225 }
226 size = min(sizeof(output), size);
227
228 memcpy(&output, info->buffer.pointer, size);
229
230 /* TODO: check version? */
231 DRM_DEBUG_DRIVER("ATIF version %u\n", output.version);
232
233 radeon_atif_parse_notification(&atif->notifications, output.notification_mask);
234 radeon_atif_parse_functions(&atif->functions, output.function_bits);
235
236out:
237 kfree(info);
238 return err;
239}
240
241/**
242 * radeon_atif_get_notification_params - determine notify configuration
243 *
244 * @handle: acpi handle
245 * @n: atif notification configuration struct
246 *
247 * Execute the ATIF_FUNCTION_GET_SYSTEM_PARAMETERS ATIF function
248 * to determine if a notifier is used and if so which one
249 * (all asics). This is either Notify(VGA, 0x81) or Notify(VGA, n)
250 * where n is specified in the result if a notifier is used.
251 * Returns 0 on success, error on failure.
252 */
253static int radeon_atif_get_notification_params(acpi_handle handle,
254 struct radeon_atif_notification_cfg *n)
255{
256 union acpi_object *info;
257 struct atif_system_params params;
258 size_t size;
259 int err = 0;
260
261 info = radeon_atif_call(handle, ATIF_FUNCTION_GET_SYSTEM_PARAMETERS, NULL);
262 if (!info) {
263 err = -EIO;
264 goto out;
265 }
266
267 size = *(u16 *) info->buffer.pointer;
268 if (size < 10) {
269 err = -EINVAL;
270 goto out;
271 }
272
273 memset(¶ms, 0, sizeof(params));
274 size = min(sizeof(params), size);
275 memcpy(¶ms, info->buffer.pointer, size);
276
277 DRM_DEBUG_DRIVER("SYSTEM_PARAMS: mask = %#x, flags = %#x\n",
278 params.flags, params.valid_mask);
279 params.flags = params.flags & params.valid_mask;
280
281 if ((params.flags & ATIF_NOTIFY_MASK) == ATIF_NOTIFY_NONE) {
282 n->enabled = false;
283 n->command_code = 0;
284 } else if ((params.flags & ATIF_NOTIFY_MASK) == ATIF_NOTIFY_81) {
285 n->enabled = true;
286 n->command_code = 0x81;
287 } else {
288 if (size < 11) {
289 err = -EINVAL;
290 goto out;
291 }
292 n->enabled = true;
293 n->command_code = params.command_code;
294 }
295
296out:
297 DRM_DEBUG_DRIVER("Notification %s, command code = %#x\n",
298 (n->enabled ? "enabled" : "disabled"),
299 n->command_code);
300 kfree(info);
301 return err;
302}
303
304/**
305 * radeon_atif_get_sbios_requests - get requested sbios event
306 *
307 * @handle: acpi handle
308 * @req: atif sbios request struct
309 *
310 * Execute the ATIF_FUNCTION_GET_SYSTEM_BIOS_REQUESTS ATIF function
311 * to determine what requests the sbios is making to the driver
312 * (all asics).
313 * Returns 0 on success, error on failure.
314 */
315static int radeon_atif_get_sbios_requests(acpi_handle handle,
316 struct atif_sbios_requests *req)
317{
318 union acpi_object *info;
319 size_t size;
320 int count = 0;
321
322 info = radeon_atif_call(handle, ATIF_FUNCTION_GET_SYSTEM_BIOS_REQUESTS, NULL);
323 if (!info)
324 return -EIO;
325
326 size = *(u16 *)info->buffer.pointer;
327 if (size < 0xd) {
328 count = -EINVAL;
329 goto out;
330 }
331 memset(req, 0, sizeof(*req));
332
333 size = min(sizeof(*req), size);
334 memcpy(req, info->buffer.pointer, size);
335 DRM_DEBUG_DRIVER("SBIOS pending requests: %#x\n", req->pending);
336
337 count = hweight32(req->pending);
338
339out:
340 kfree(info);
341 return count;
342}
343
344/**
345 * radeon_atif_handler - handle ATIF notify requests
346 *
347 * @rdev: radeon_device pointer
348 * @event: atif sbios request struct
349 *
350 * Checks the acpi event and if it matches an atif event,
351 * handles it.
352 * Returns NOTIFY code
353 */
354static int radeon_atif_handler(struct radeon_device *rdev,
355 struct acpi_bus_event *event)
356{
357 struct radeon_atif *atif = &rdev->atif;
358 struct atif_sbios_requests req;
359 acpi_handle handle;
360 int count;
361
362 DRM_DEBUG_DRIVER("event, device_class = %s, type = %#x\n",
363 event->device_class, event->type);
364
365 if (strcmp(event->device_class, ACPI_VIDEO_CLASS) != 0)
366 return NOTIFY_DONE;
367
368 if (!atif->notification_cfg.enabled ||
369 event->type != atif->notification_cfg.command_code)
370 /* Not our event */
371 return NOTIFY_DONE;
372
373 /* Check pending SBIOS requests */
374 handle = ACPI_HANDLE(&rdev->pdev->dev);
375 count = radeon_atif_get_sbios_requests(handle, &req);
376
377 if (count <= 0)
378 return NOTIFY_DONE;
379
380 DRM_DEBUG_DRIVER("ATIF: %d pending SBIOS requests\n", count);
381
382 if (req.pending & ATIF_PANEL_BRIGHTNESS_CHANGE_REQUEST) {
383 struct radeon_encoder *enc = atif->encoder_for_bl;
384
385 if (enc) {
386 DRM_DEBUG_DRIVER("Changing brightness to %d\n",
387 req.backlight_level);
388
389 radeon_set_backlight_level(rdev, enc, req.backlight_level);
390
391#if defined(CONFIG_BACKLIGHT_CLASS_DEVICE) || defined(CONFIG_BACKLIGHT_CLASS_DEVICE_MODULE)
392 if (rdev->is_atom_bios) {
393 struct radeon_encoder_atom_dig *dig = enc->enc_priv;
394 backlight_force_update(dig->bl_dev,
395 BACKLIGHT_UPDATE_HOTKEY);
396 } else {
397 struct radeon_encoder_lvds *dig = enc->enc_priv;
398 backlight_force_update(dig->bl_dev,
399 BACKLIGHT_UPDATE_HOTKEY);
400 }
401#endif
402 }
403 }
404 if (req.pending & ATIF_DGPU_DISPLAY_EVENT) {
405 if ((rdev->flags & RADEON_IS_PX) &&
406 radeon_atpx_dgpu_req_power_for_displays()) {
407 pm_runtime_get_sync(rdev->ddev->dev);
408 /* Just fire off a uevent and let userspace tell us what to do */
409 drm_helper_hpd_irq_event(rdev->ddev);
410 pm_runtime_mark_last_busy(rdev->ddev->dev);
411 pm_runtime_put_autosuspend(rdev->ddev->dev);
412 }
413 }
414 /* TODO: check other events */
415
416 /* We've handled the event, stop the notifier chain. The ACPI interface
417 * overloads ACPI_VIDEO_NOTIFY_PROBE, we don't want to send that to
418 * userspace if the event was generated only to signal a SBIOS
419 * request.
420 */
421 return NOTIFY_BAD;
422}
423
424/* Call the ATCS method
425 */
426/**
427 * radeon_atcs_call - call an ATCS method
428 *
429 * @handle: acpi handle
430 * @function: the ATCS function to execute
431 * @params: ATCS function params
432 *
433 * Executes the requested ATCS function (all asics).
434 * Returns a pointer to the acpi output buffer.
435 */
436static union acpi_object *radeon_atcs_call(acpi_handle handle, int function,
437 struct acpi_buffer *params)
438{
439 acpi_status status;
440 union acpi_object atcs_arg_elements[2];
441 struct acpi_object_list atcs_arg;
442 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
443
444 atcs_arg.count = 2;
445 atcs_arg.pointer = &atcs_arg_elements[0];
446
447 atcs_arg_elements[0].type = ACPI_TYPE_INTEGER;
448 atcs_arg_elements[0].integer.value = function;
449
450 if (params) {
451 atcs_arg_elements[1].type = ACPI_TYPE_BUFFER;
452 atcs_arg_elements[1].buffer.length = params->length;
453 atcs_arg_elements[1].buffer.pointer = params->pointer;
454 } else {
455 /* We need a second fake parameter */
456 atcs_arg_elements[1].type = ACPI_TYPE_INTEGER;
457 atcs_arg_elements[1].integer.value = 0;
458 }
459
460 status = acpi_evaluate_object(handle, "ATCS", &atcs_arg, &buffer);
461
462 /* Fail only if calling the method fails and ATIF is supported */
463 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
464 DRM_DEBUG_DRIVER("failed to evaluate ATCS got %s\n",
465 acpi_format_exception(status));
466 kfree(buffer.pointer);
467 return NULL;
468 }
469
470 return buffer.pointer;
471}
472
473/**
474 * radeon_atcs_parse_functions - parse supported functions
475 *
476 * @f: supported functions struct
477 * @mask: supported functions mask from ATCS
478 *
479 * Use the supported functions mask from ATCS function
480 * ATCS_FUNCTION_VERIFY_INTERFACE to determine what functions
481 * are supported (all asics).
482 */
483static void radeon_atcs_parse_functions(struct radeon_atcs_functions *f, u32 mask)
484{
485 f->get_ext_state = mask & ATCS_GET_EXTERNAL_STATE_SUPPORTED;
486 f->pcie_perf_req = mask & ATCS_PCIE_PERFORMANCE_REQUEST_SUPPORTED;
487 f->pcie_dev_rdy = mask & ATCS_PCIE_DEVICE_READY_NOTIFICATION_SUPPORTED;
488 f->pcie_bus_width = mask & ATCS_SET_PCIE_BUS_WIDTH_SUPPORTED;
489}
490
491/**
492 * radeon_atcs_verify_interface - verify ATCS
493 *
494 * @handle: acpi handle
495 * @atcs: radeon atcs struct
496 *
497 * Execute the ATCS_FUNCTION_VERIFY_INTERFACE ATCS function
498 * to initialize ATCS and determine what features are supported
499 * (all asics).
500 * returns 0 on success, error on failure.
501 */
502static int radeon_atcs_verify_interface(acpi_handle handle,
503 struct radeon_atcs *atcs)
504{
505 union acpi_object *info;
506 struct atcs_verify_interface output;
507 size_t size;
508 int err = 0;
509
510 info = radeon_atcs_call(handle, ATCS_FUNCTION_VERIFY_INTERFACE, NULL);
511 if (!info)
512 return -EIO;
513
514 memset(&output, 0, sizeof(output));
515
516 size = *(u16 *) info->buffer.pointer;
517 if (size < 8) {
518 DRM_INFO("ATCS buffer is too small: %zu\n", size);
519 err = -EINVAL;
520 goto out;
521 }
522 size = min(sizeof(output), size);
523
524 memcpy(&output, info->buffer.pointer, size);
525
526 /* TODO: check version? */
527 DRM_DEBUG_DRIVER("ATCS version %u\n", output.version);
528
529 radeon_atcs_parse_functions(&atcs->functions, output.function_bits);
530
531out:
532 kfree(info);
533 return err;
534}
535
536/**
537 * radeon_acpi_is_pcie_performance_request_supported
538 *
539 * @rdev: radeon_device pointer
540 *
541 * Check if the ATCS pcie_perf_req and pcie_dev_rdy methods
542 * are supported (all asics).
543 * returns true if supported, false if not.
544 */
545bool radeon_acpi_is_pcie_performance_request_supported(struct radeon_device *rdev)
546{
547 struct radeon_atcs *atcs = &rdev->atcs;
548
549 if (atcs->functions.pcie_perf_req && atcs->functions.pcie_dev_rdy)
550 return true;
551
552 return false;
553}
554
555/**
556 * radeon_acpi_pcie_notify_device_ready
557 *
558 * @rdev: radeon_device pointer
559 *
560 * Executes the PCIE_DEVICE_READY_NOTIFICATION method
561 * (all asics).
562 * returns 0 on success, error on failure.
563 */
564int radeon_acpi_pcie_notify_device_ready(struct radeon_device *rdev)
565{
566 acpi_handle handle;
567 union acpi_object *info;
568 struct radeon_atcs *atcs = &rdev->atcs;
569
570 /* Get the device handle */
571 handle = ACPI_HANDLE(&rdev->pdev->dev);
572 if (!handle)
573 return -EINVAL;
574
575 if (!atcs->functions.pcie_dev_rdy)
576 return -EINVAL;
577
578 info = radeon_atcs_call(handle, ATCS_FUNCTION_PCIE_DEVICE_READY_NOTIFICATION, NULL);
579 if (!info)
580 return -EIO;
581
582 kfree(info);
583
584 return 0;
585}
586
587/**
588 * radeon_acpi_pcie_performance_request
589 *
590 * @rdev: radeon_device pointer
591 * @perf_req: requested perf level (pcie gen speed)
592 * @advertise: set advertise caps flag if set
593 *
594 * Executes the PCIE_PERFORMANCE_REQUEST method to
595 * change the pcie gen speed (all asics).
596 * returns 0 on success, error on failure.
597 */
598int radeon_acpi_pcie_performance_request(struct radeon_device *rdev,
599 u8 perf_req, bool advertise)
600{
601 acpi_handle handle;
602 union acpi_object *info;
603 struct radeon_atcs *atcs = &rdev->atcs;
604 struct atcs_pref_req_input atcs_input;
605 struct atcs_pref_req_output atcs_output;
606 struct acpi_buffer params;
607 size_t size;
608 u32 retry = 3;
609
610 /* Get the device handle */
611 handle = ACPI_HANDLE(&rdev->pdev->dev);
612 if (!handle)
613 return -EINVAL;
614
615 if (!atcs->functions.pcie_perf_req)
616 return -EINVAL;
617
618 atcs_input.size = sizeof(struct atcs_pref_req_input);
619 /* client id (bit 2-0: func num, 7-3: dev num, 15-8: bus num) */
620 atcs_input.client_id = rdev->pdev->devfn | (rdev->pdev->bus->number << 8);
621 atcs_input.valid_flags_mask = ATCS_VALID_FLAGS_MASK;
622 atcs_input.flags = ATCS_WAIT_FOR_COMPLETION;
623 if (advertise)
624 atcs_input.flags |= ATCS_ADVERTISE_CAPS;
625 atcs_input.req_type = ATCS_PCIE_LINK_SPEED;
626 atcs_input.perf_req = perf_req;
627
628 params.length = sizeof(struct atcs_pref_req_input);
629 params.pointer = &atcs_input;
630
631 while (retry--) {
632 info = radeon_atcs_call(handle, ATCS_FUNCTION_PCIE_PERFORMANCE_REQUEST, ¶ms);
633 if (!info)
634 return -EIO;
635
636 memset(&atcs_output, 0, sizeof(atcs_output));
637
638 size = *(u16 *) info->buffer.pointer;
639 if (size < 3) {
640 DRM_INFO("ATCS buffer is too small: %zu\n", size);
641 kfree(info);
642 return -EINVAL;
643 }
644 size = min(sizeof(atcs_output), size);
645
646 memcpy(&atcs_output, info->buffer.pointer, size);
647
648 kfree(info);
649
650 switch (atcs_output.ret_val) {
651 case ATCS_REQUEST_REFUSED:
652 default:
653 return -EINVAL;
654 case ATCS_REQUEST_COMPLETE:
655 return 0;
656 case ATCS_REQUEST_IN_PROGRESS:
657 udelay(10);
658 break;
659 }
660 }
661
662 return 0;
663}
664
665/**
666 * radeon_acpi_event - handle notify events
667 *
668 * @nb: notifier block
669 * @val: val
670 * @data: acpi event
671 *
672 * Calls relevant radeon functions in response to various
673 * acpi events.
674 * Returns NOTIFY code
675 */
676static int radeon_acpi_event(struct notifier_block *nb,
677 unsigned long val,
678 void *data)
679{
680 struct radeon_device *rdev = container_of(nb, struct radeon_device, acpi_nb);
681 struct acpi_bus_event *entry = (struct acpi_bus_event *)data;
682
683 if (strcmp(entry->device_class, ACPI_AC_CLASS) == 0) {
684 if (power_supply_is_system_supplied() > 0)
685 DRM_DEBUG_DRIVER("pm: AC\n");
686 else
687 DRM_DEBUG_DRIVER("pm: DC\n");
688
689 radeon_pm_acpi_event_handler(rdev);
690 }
691
692 /* Check for pending SBIOS requests */
693 return radeon_atif_handler(rdev, entry);
694}
695
696/* Call all ACPI methods here */
697/**
698 * radeon_acpi_init - init driver acpi support
699 *
700 * @rdev: radeon_device pointer
701 *
702 * Verifies the AMD ACPI interfaces and registers with the acpi
703 * notifier chain (all asics).
704 * Returns 0 on success, error on failure.
705 */
706int radeon_acpi_init(struct radeon_device *rdev)
707{
708 acpi_handle handle;
709 struct radeon_atif *atif = &rdev->atif;
710 struct radeon_atcs *atcs = &rdev->atcs;
711 int ret;
712
713 /* Get the device handle */
714 handle = ACPI_HANDLE(&rdev->pdev->dev);
715
716 /* No need to proceed if we're sure that ATIF is not supported */
717 if (!ASIC_IS_AVIVO(rdev) || !rdev->bios || !handle)
718 return 0;
719
720 /* Call the ATCS method */
721 ret = radeon_atcs_verify_interface(handle, atcs);
722 if (ret) {
723 DRM_DEBUG_DRIVER("Call to ATCS verify_interface failed: %d\n", ret);
724 }
725
726 /* Call the ATIF method */
727 ret = radeon_atif_verify_interface(handle, atif);
728 if (ret) {
729 DRM_DEBUG_DRIVER("Call to ATIF verify_interface failed: %d\n", ret);
730 goto out;
731 }
732
733 if (atif->notifications.brightness_change) {
734 struct drm_encoder *tmp;
735 struct radeon_encoder *target = NULL;
736
737 /* Find the encoder controlling the brightness */
738 list_for_each_entry(tmp, &rdev->ddev->mode_config.encoder_list,
739 head) {
740 struct radeon_encoder *enc = to_radeon_encoder(tmp);
741
742 if ((enc->devices & (ATOM_DEVICE_LCD_SUPPORT)) &&
743 enc->enc_priv) {
744 if (rdev->is_atom_bios) {
745 struct radeon_encoder_atom_dig *dig = enc->enc_priv;
746 if (dig->bl_dev) {
747 target = enc;
748 break;
749 }
750 } else {
751 struct radeon_encoder_lvds *dig = enc->enc_priv;
752 if (dig->bl_dev) {
753 target = enc;
754 break;
755 }
756 }
757 }
758 }
759
760 atif->encoder_for_bl = target;
761 }
762
763 if (atif->functions.sbios_requests && !atif->functions.system_params) {
764 /* XXX check this workraround, if sbios request function is
765 * present we have to see how it's configured in the system
766 * params
767 */
768 atif->functions.system_params = true;
769 }
770
771 if (atif->functions.system_params) {
772 ret = radeon_atif_get_notification_params(handle,
773 &atif->notification_cfg);
774 if (ret) {
775 DRM_DEBUG_DRIVER("Call to GET_SYSTEM_PARAMS failed: %d\n",
776 ret);
777 /* Disable notification */
778 atif->notification_cfg.enabled = false;
779 }
780 }
781
782out:
783 rdev->acpi_nb.notifier_call = radeon_acpi_event;
784 register_acpi_notifier(&rdev->acpi_nb);
785
786 return ret;
787}
788
789/**
790 * radeon_acpi_fini - tear down driver acpi support
791 *
792 * @rdev: radeon_device pointer
793 *
794 * Unregisters with the acpi notifier chain (all asics).
795 */
796void radeon_acpi_fini(struct radeon_device *rdev)
797{
798 unregister_acpi_notifier(&rdev->acpi_nb);
799}