2 * Copyright 2012 Advanced Micro Devices, Inc.
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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.
22 * $FreeBSD: head/sys/dev/drm2/radeon/radeon_acpi.c 254885 2013-08-25 19:37:15Z dumbbell $
25 #include <linux/pci.h>
26 #include <linux/acpi.h>
27 #include <linux/power_supply.h>
28 #include <linux/pm_runtime.h>
29 #include <acpi/video.h>
31 #include <drm/drm_crtc_helper.h>
33 #include "radeon_acpi.h"
36 #define ACPI_AC_CLASS "ac_adapter"
38 struct atif_verify_interface {
39 u16 size; /* structure size in bytes (includes size field) */
40 u16 version; /* version */
41 u32 notification_mask; /* supported notifications mask */
42 u32 function_bits; /* supported functions bit vector */
45 struct atif_system_params {
46 u16 size; /* structure size in bytes (includes size field) */
47 u32 valid_mask; /* valid flags mask */
48 u32 flags; /* flags */
49 u8 command_code; /* notify command code */
52 struct atif_sbios_requests {
53 u16 size; /* structure size in bytes (includes size field) */
54 u32 pending; /* pending sbios requests */
55 u8 panel_exp_mode; /* panel expansion mode */
56 u8 thermal_gfx; /* thermal state: target gfx controller */
57 u8 thermal_state; /* thermal state: state id (0: exit state, non-0: state) */
58 u8 forced_power_gfx; /* forced power state: target gfx controller */
59 u8 forced_power_state; /* forced power state: state id */
60 u8 system_power_src; /* system power source */
61 u8 backlight_level; /* panel backlight level (0-255) */
64 #define ATIF_NOTIFY_MASK 0x3
65 #define ATIF_NOTIFY_NONE 0
66 #define ATIF_NOTIFY_81 1
67 #define ATIF_NOTIFY_N 2
69 struct atcs_verify_interface {
70 u16 size; /* structure size in bytes (includes size field) */
71 u16 version; /* version */
72 u32 function_bits; /* supported functions bit vector */
75 #define ATCS_VALID_FLAGS_MASK 0x3
77 struct atcs_pref_req_input {
78 u16 size; /* structure size in bytes (includes size field) */
79 u16 client_id; /* client id (bit 2-0: func num, 7-3: dev num, 15-8: bus num) */
80 u16 valid_flags_mask; /* valid flags mask */
81 u16 flags; /* flags */
82 u8 req_type; /* request type */
83 u8 perf_req; /* performance request */
86 struct atcs_pref_req_output {
87 u16 size; /* structure size in bytes (includes size field) */
88 u8 ret_val; /* return value */
91 /* Call the ATIF method
94 * radeon_atif_call - call an ATIF method
96 * @handle: acpi handle
97 * @function: the ATIF function to execute
98 * @params: ATIF function params
100 * Executes the requested ATIF function (all asics).
101 * Returns a pointer to the acpi output buffer.
103 static ACPI_OBJECT *radeon_atif_call(ACPI_HANDLE handle, int function,
107 ACPI_OBJECT atif_arg_elements[2];
108 ACPI_OBJECT_LIST atif_arg;
109 ACPI_BUFFER buffer = { ACPI_ALLOCATE_BUFFER, NULL };
112 atif_arg.Pointer = &atif_arg_elements[0];
114 atif_arg_elements[0].Type = ACPI_TYPE_INTEGER;
115 atif_arg_elements[0].Integer.Value = function;
118 atif_arg_elements[1].Type = ACPI_TYPE_BUFFER;
119 atif_arg_elements[1].Buffer.Length = params->Length;
120 atif_arg_elements[1].Buffer.Pointer = params->Pointer;
122 /* We need a second fake parameter */
123 atif_arg_elements[1].Type = ACPI_TYPE_INTEGER;
124 atif_arg_elements[1].Integer.Value = 0;
127 status = AcpiEvaluateObject(handle, "ATIF", &atif_arg, &buffer);
129 /* Fail only if calling the method fails and ATIF is supported */
130 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
131 DRM_DEBUG_DRIVER("failed to evaluate ATIF got %s\n",
132 AcpiFormatException(status));
133 AcpiOsFree(buffer.Pointer);
137 return buffer.Pointer;
141 * radeon_atif_parse_notification - parse supported notifications
143 * @n: supported notifications struct
144 * @mask: supported notifications mask from ATIF
146 * Use the supported notifications mask from ATIF function
147 * ATIF_FUNCTION_VERIFY_INTERFACE to determine what notifications
148 * are supported (all asics).
150 static void radeon_atif_parse_notification(struct radeon_atif_notifications *n, u32 mask)
152 n->display_switch = mask & ATIF_DISPLAY_SWITCH_REQUEST_SUPPORTED;
153 n->expansion_mode_change = mask & ATIF_EXPANSION_MODE_CHANGE_REQUEST_SUPPORTED;
154 n->thermal_state = mask & ATIF_THERMAL_STATE_CHANGE_REQUEST_SUPPORTED;
155 n->forced_power_state = mask & ATIF_FORCED_POWER_STATE_CHANGE_REQUEST_SUPPORTED;
156 n->system_power_state = mask & ATIF_SYSTEM_POWER_SOURCE_CHANGE_REQUEST_SUPPORTED;
157 n->display_conf_change = mask & ATIF_DISPLAY_CONF_CHANGE_REQUEST_SUPPORTED;
158 n->px_gfx_switch = mask & ATIF_PX_GFX_SWITCH_REQUEST_SUPPORTED;
159 n->brightness_change = mask & ATIF_PANEL_BRIGHTNESS_CHANGE_REQUEST_SUPPORTED;
160 n->dgpu_display_event = mask & ATIF_DGPU_DISPLAY_EVENT_SUPPORTED;
164 * radeon_atif_parse_functions - parse supported functions
166 * @f: supported functions struct
167 * @mask: supported functions mask from ATIF
169 * Use the supported functions mask from ATIF function
170 * ATIF_FUNCTION_VERIFY_INTERFACE to determine what functions
171 * are supported (all asics).
173 static void radeon_atif_parse_functions(struct radeon_atif_functions *f, u32 mask)
175 f->system_params = mask & ATIF_GET_SYSTEM_PARAMETERS_SUPPORTED;
176 f->sbios_requests = mask & ATIF_GET_SYSTEM_BIOS_REQUESTS_SUPPORTED;
177 f->select_active_disp = mask & ATIF_SELECT_ACTIVE_DISPLAYS_SUPPORTED;
178 f->lid_state = mask & ATIF_GET_LID_STATE_SUPPORTED;
179 f->get_tv_standard = mask & ATIF_GET_TV_STANDARD_FROM_CMOS_SUPPORTED;
180 f->set_tv_standard = mask & ATIF_SET_TV_STANDARD_IN_CMOS_SUPPORTED;
181 f->get_panel_expansion_mode = mask & ATIF_GET_PANEL_EXPANSION_MODE_FROM_CMOS_SUPPORTED;
182 f->set_panel_expansion_mode = mask & ATIF_SET_PANEL_EXPANSION_MODE_IN_CMOS_SUPPORTED;
183 f->temperature_change = mask & ATIF_TEMPERATURE_CHANGE_NOTIFICATION_SUPPORTED;
184 f->graphics_device_types = mask & ATIF_GET_GRAPHICS_DEVICE_TYPES_SUPPORTED;
188 * radeon_atif_verify_interface - verify ATIF
190 * @handle: acpi handle
191 * @atif: radeon atif struct
193 * Execute the ATIF_FUNCTION_VERIFY_INTERFACE ATIF function
194 * to initialize ATIF and determine what features are supported
196 * returns 0 on success, error on failure.
198 static int radeon_atif_verify_interface(ACPI_HANDLE handle,
199 struct radeon_atif *atif)
202 struct atif_verify_interface output;
206 info = radeon_atif_call(handle, ATIF_FUNCTION_VERIFY_INTERFACE, NULL);
210 memset(&output, 0, sizeof(output));
212 size = *(u16 *) info->Buffer.Pointer;
214 DRM_INFO("ATIF buffer is too small: %zu\n", size);
218 size = min(sizeof(output), size);
220 memcpy(&output, info->Buffer.Pointer, size);
222 /* TODO: check version? */
223 DRM_DEBUG_DRIVER("ATIF version %u\n", output.version);
225 radeon_atif_parse_notification(&atif->notifications, output.notification_mask);
226 radeon_atif_parse_functions(&atif->functions, output.function_bits);
234 * radeon_atif_get_notification_params - determine notify configuration
236 * @handle: acpi handle
237 * @n: atif notification configuration struct
239 * Execute the ATIF_FUNCTION_GET_SYSTEM_PARAMETERS ATIF function
240 * to determine if a notifier is used and if so which one
241 * (all asics). This is either Notify(VGA, 0x81) or Notify(VGA, n)
242 * where n is specified in the result if a notifier is used.
243 * Returns 0 on success, error on failure.
245 static int radeon_atif_get_notification_params(ACPI_HANDLE handle,
246 struct radeon_atif_notification_cfg *n)
249 struct atif_system_params params;
253 info = radeon_atif_call(handle, ATIF_FUNCTION_GET_SYSTEM_PARAMETERS, NULL);
259 size = *(u16 *) info->Buffer.Pointer;
265 memset(¶ms, 0, sizeof(params));
266 size = min(sizeof(params), size);
267 memcpy(¶ms, info->Buffer.Pointer, size);
269 DRM_DEBUG_DRIVER("SYSTEM_PARAMS: mask = %#x, flags = %#x\n",
270 params.flags, params.valid_mask);
271 params.flags = params.flags & params.valid_mask;
273 if ((params.flags & ATIF_NOTIFY_MASK) == ATIF_NOTIFY_NONE) {
276 } else if ((params.flags & ATIF_NOTIFY_MASK) == ATIF_NOTIFY_81) {
278 n->command_code = 0x81;
285 n->command_code = params.command_code;
289 DRM_DEBUG_DRIVER("Notification %s, command code = %#x\n",
290 (n->enabled ? "enabled" : "disabled"),
297 * radeon_atif_get_sbios_requests - get requested sbios event
299 * @handle: acpi handle
300 * @req: atif sbios request struct
302 * Execute the ATIF_FUNCTION_GET_SYSTEM_BIOS_REQUESTS ATIF function
303 * to determine what requests the sbios is making to the driver
305 * Returns 0 on success, error on failure.
307 static int radeon_atif_get_sbios_requests(ACPI_HANDLE handle,
308 struct atif_sbios_requests *req)
314 info = radeon_atif_call(handle, ATIF_FUNCTION_GET_SYSTEM_BIOS_REQUESTS, NULL);
318 size = *(u16 *)info->Buffer.Pointer;
323 memset(req, 0, sizeof(*req));
325 size = min(sizeof(*req), size);
326 memcpy(req, info->Buffer.Pointer, size);
327 DRM_DEBUG_DRIVER("SBIOS pending requests: %#x\n", req->pending);
329 count = hweight32(req->pending);
337 * radeon_atif_handler - handle ATIF notify requests
339 * @rdev: radeon_device pointer
340 * @event: atif sbios request struct
342 * Checks the acpi event and if it matches an atif event,
344 * Returns NOTIFY code
346 void radeon_atif_handler(struct radeon_device *rdev,
349 struct radeon_atif *atif = &rdev->atif;
350 struct atif_sbios_requests req;
354 DRM_DEBUG_DRIVER("event, type = %#x\n",
357 if (!atif->notification_cfg.enabled ||
358 type != atif->notification_cfg.command_code)
362 /* Check pending SBIOS requests */
363 handle = rdev->acpi.handle;
364 count = radeon_atif_get_sbios_requests(handle, &req);
369 DRM_DEBUG_DRIVER("ATIF: %d pending SBIOS requests\n", count);
371 if (req.pending & ATIF_PANEL_BRIGHTNESS_CHANGE_REQUEST) {
372 struct radeon_encoder *enc = atif->encoder_for_bl;
375 DRM_DEBUG_DRIVER("Changing brightness to %d\n",
376 req.backlight_level);
378 radeon_set_backlight_level(rdev, enc, req.backlight_level);
381 if (rdev->is_atom_bios) {
382 struct radeon_encoder_atom_dig *dig = enc->enc_priv;
383 backlight_force_update(dig->bl_dev,
384 BACKLIGHT_UPDATE_HOTKEY);
386 struct radeon_encoder_lvds *dig = enc->enc_priv;
387 backlight_force_update(dig->bl_dev,
388 BACKLIGHT_UPDATE_HOTKEY);
390 #endif /* DUMBBELL_WIP */
393 /* TODO: check other events */
395 /* We've handled the event, stop the notifier chain. The ACPI interface
396 * overloads ACPI_VIDEO_NOTIFY_PROBE, we don't want to send that to
397 * userspace if the event was generated only to signal a SBIOS
402 /* Call the ATCS method
405 * radeon_atcs_call - call an ATCS method
407 * @handle: acpi handle
408 * @function: the ATCS function to execute
409 * @params: ATCS function params
411 * Executes the requested ATCS function (all asics).
412 * Returns a pointer to the acpi output buffer.
414 static union acpi_object *radeon_atcs_call(ACPI_HANDLE handle, int function,
418 ACPI_OBJECT atcs_arg_elements[2];
419 ACPI_OBJECT_LIST atcs_arg;
420 ACPI_BUFFER buffer = { ACPI_ALLOCATE_BUFFER, NULL };
423 atcs_arg.Pointer = &atcs_arg_elements[0];
425 atcs_arg_elements[0].Type = ACPI_TYPE_INTEGER;
426 atcs_arg_elements[0].Integer.Value = function;
429 atcs_arg_elements[1].Type = ACPI_TYPE_BUFFER;
430 atcs_arg_elements[1].Buffer.Length = params->Length;
431 atcs_arg_elements[1].Buffer.Pointer = params->Pointer;
433 /* We need a second fake parameter */
434 atcs_arg_elements[1].Type = ACPI_TYPE_INTEGER;
435 atcs_arg_elements[1].Integer.Value = 0;
438 status = AcpiEvaluateObject(handle, "ATCS", &atcs_arg, &buffer);
440 /* Fail only if calling the method fails and ATIF is supported */
441 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
442 DRM_DEBUG_DRIVER("failed to evaluate ATCS got %s\n",
443 AcpiFormatException(status));
444 AcpiOsFree(buffer.Pointer);
448 return buffer.Pointer;
452 * radeon_atcs_parse_functions - parse supported functions
454 * @f: supported functions struct
455 * @mask: supported functions mask from ATCS
457 * Use the supported functions mask from ATCS function
458 * ATCS_FUNCTION_VERIFY_INTERFACE to determine what functions
459 * are supported (all asics).
461 static void radeon_atcs_parse_functions(struct radeon_atcs_functions *f, u32 mask)
463 f->get_ext_state = mask & ATCS_GET_EXTERNAL_STATE_SUPPORTED;
464 f->pcie_perf_req = mask & ATCS_PCIE_PERFORMANCE_REQUEST_SUPPORTED;
465 f->pcie_dev_rdy = mask & ATCS_PCIE_DEVICE_READY_NOTIFICATION_SUPPORTED;
466 f->pcie_bus_width = mask & ATCS_SET_PCIE_BUS_WIDTH_SUPPORTED;
470 * radeon_atcs_verify_interface - verify ATCS
472 * @handle: acpi handle
473 * @atcs: radeon atcs struct
475 * Execute the ATCS_FUNCTION_VERIFY_INTERFACE ATCS function
476 * to initialize ATCS and determine what features are supported
478 * returns 0 on success, error on failure.
480 static int radeon_atcs_verify_interface(ACPI_HANDLE handle,
481 struct radeon_atcs *atcs)
484 struct atcs_verify_interface output;
488 info = radeon_atcs_call(handle, ATCS_FUNCTION_VERIFY_INTERFACE, NULL);
492 memset(&output, 0, sizeof(output));
494 size = *(u16 *) info->Buffer.Pointer;
496 DRM_INFO("ATCS buffer is too small: %zu\n", size);
500 size = min(sizeof(output), size);
502 memcpy(&output, info->Buffer.Pointer, size);
504 /* TODO: check version? */
505 DRM_DEBUG_DRIVER("ATCS version %u\n", output.version);
507 radeon_atcs_parse_functions(&atcs->functions, output.function_bits);
515 * radeon_acpi_is_pcie_performance_request_supported
517 * @rdev: radeon_device pointer
519 * Check if the ATCS pcie_perf_req and pcie_dev_rdy methods
520 * are supported (all asics).
521 * returns true if supported, false if not.
523 bool radeon_acpi_is_pcie_performance_request_supported(struct radeon_device *rdev)
525 struct radeon_atcs *atcs = &rdev->atcs;
527 if (atcs->functions.pcie_perf_req && atcs->functions.pcie_dev_rdy)
534 * radeon_acpi_pcie_notify_device_ready
536 * @rdev: radeon_device pointer
538 * Executes the PCIE_DEVICE_READY_NOTIFICATION method
540 * returns 0 on success, error on failure.
542 int radeon_acpi_pcie_notify_device_ready(struct radeon_device *rdev)
546 union acpi_object *info;
547 struct radeon_atcs *atcs = &rdev->atcs;
549 /* Get the device handle */
550 handle = DEVICE_ACPI_HANDLE(&rdev->pdev->dev);
554 if (!atcs->functions.pcie_dev_rdy)
557 info = radeon_atcs_call(handle, ATCS_FUNCTION_PCIE_DEVICE_READY_NOTIFICATION, NULL);
570 * radeon_acpi_pcie_performance_request
572 * @rdev: radeon_device pointer
573 * @perf_req: requested perf level (pcie gen speed)
574 * @advertise: set advertise caps flag if set
576 * Executes the PCIE_PERFORMANCE_REQUEST method to
577 * change the pcie gen speed (all asics).
578 * returns 0 on success, error on failure.
580 int radeon_acpi_pcie_performance_request(struct radeon_device *rdev,
581 u8 perf_req, bool advertise)
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;
593 /* Get the device handle */
594 handle = DEVICE_ACPI_HANDLE(&rdev->pdev->dev);
598 if (!atcs->functions.pcie_perf_req)
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;
607 atcs_input.flags |= ATCS_ADVERTISE_CAPS;
608 atcs_input.req_type = ATCS_PCIE_LINK_SPEED;
609 atcs_input.perf_req = perf_req;
611 params.length = sizeof(struct atcs_pref_req_input);
612 params.pointer = &atcs_input;
615 info = radeon_atcs_call(handle, ATCS_FUNCTION_PCIE_PERFORMANCE_REQUEST, ¶ms);
619 memset(&atcs_output, 0, sizeof(atcs_output));
621 size = *(u16 *) info->buffer.pointer;
623 DRM_INFO("ATCS buffer is too small: %zu\n", size);
627 size = min(sizeof(atcs_output), size);
629 memcpy(&atcs_output, info->buffer.pointer, size);
633 switch (atcs_output.ret_val) {
634 case ATCS_REQUEST_REFUSED:
637 case ATCS_REQUEST_COMPLETE:
639 case ATCS_REQUEST_IN_PROGRESS:
652 * radeon_acpi_event - handle notify events
654 * @nb: notifier block
658 * Calls relevant radeon functions in response to various
660 * Returns NOTIFY code
662 static void radeon_acpi_event(ACPI_HANDLE handle, UINT32 type,
665 struct radeon_device *rdev = (struct radeon_device *)context;
668 if (strcmp(entry->device_class, ACPI_AC_CLASS) == 0) {
669 if (power_supply_is_system_supplied() > 0)
670 DRM_DEBUG_DRIVER("pm: AC\n");
672 DRM_DEBUG_DRIVER("pm: DC\n");
674 radeon_pm_acpi_event_handler(rdev);
676 #endif /* DUMBBELL_WIP */
678 /* Check for pending SBIOS requests */
679 radeon_atif_handler(rdev, type);
682 /* Call all ACPI methods here */
684 * radeon_acpi_init - init driver acpi support
686 * @rdev: radeon_device pointer
688 * Verifies the AMD ACPI interfaces and registers with the acpi
689 * notifier chain (all asics).
690 * Returns 0 on success, error on failure.
692 int radeon_acpi_init(struct radeon_device *rdev)
695 struct radeon_atif *atif = &rdev->atif;
696 struct radeon_atcs *atcs = &rdev->atcs;
699 /* Get the device handle */
700 handle = acpi_get_handle(rdev->dev->bsddev);
702 /* No need to proceed if we're sure that ATIF is not supported */
703 if (!ASIC_IS_AVIVO(rdev) || !rdev->bios || !handle)
706 /* Call the ATCS method */
707 ret = radeon_atcs_verify_interface(handle, atcs);
709 DRM_DEBUG_DRIVER("Call to ATCS verify_interface failed: %d\n", ret);
712 /* Call the ATIF method */
713 ret = radeon_atif_verify_interface(handle, atif);
715 DRM_DEBUG_DRIVER("Call to ATIF verify_interface failed: %d\n", ret);
719 if (atif->notifications.brightness_change) {
720 struct drm_encoder *tmp;
721 struct radeon_encoder *target = NULL;
723 /* Find the encoder controlling the brightness */
724 list_for_each_entry(tmp, &rdev->ddev->mode_config.encoder_list,
726 struct radeon_encoder *enc = to_radeon_encoder(tmp);
728 if ((enc->devices & (ATOM_DEVICE_LCD_SUPPORT)) &&
730 if (rdev->is_atom_bios) {
731 struct radeon_encoder_atom_dig *dig = enc->enc_priv;
737 struct radeon_encoder_lvds *dig = enc->enc_priv;
746 atif->encoder_for_bl = target;
749 if (atif->functions.sbios_requests && !atif->functions.system_params) {
750 /* XXX check this workraround, if sbios request function is
751 * present we have to see how it's configured in the system
754 atif->functions.system_params = true;
757 if (atif->functions.system_params) {
758 ret = radeon_atif_get_notification_params(handle,
759 &atif->notification_cfg);
761 DRM_DEBUG_DRIVER("Call to GET_SYSTEM_PARAMS failed: %d\n",
763 /* Disable notification */
764 atif->notification_cfg.enabled = false;
769 rdev->acpi.handle = handle;
770 rdev->acpi.notifier_call = radeon_acpi_event;
771 AcpiInstallNotifyHandler(handle, ACPI_DEVICE_NOTIFY,
772 rdev->acpi.notifier_call, rdev);
778 * radeon_acpi_fini - tear down driver acpi support
780 * @rdev: radeon_device pointer
782 * Unregisters with the acpi notifier chain (all asics).
784 void radeon_acpi_fini(struct radeon_device *rdev)
786 AcpiRemoveNotifyHandler(rdev->acpi.handle, ACPI_DEVICE_NOTIFY,
787 rdev->acpi.notifier_call);