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 $
26 #include <drm/drm_crtc_helper.h>
28 #include "radeon_acpi.h"
31 #define ACPI_AC_CLASS "ac_adapter"
33 struct atif_verify_interface {
34 u16 size; /* structure size in bytes (includes size field) */
35 u16 version; /* version */
36 u32 notification_mask; /* supported notifications mask */
37 u32 function_bits; /* supported functions bit vector */
40 struct atif_system_params {
41 u16 size; /* structure size in bytes (includes size field) */
42 u32 valid_mask; /* valid flags mask */
43 u32 flags; /* flags */
44 u8 command_code; /* notify command code */
47 struct atif_sbios_requests {
48 u16 size; /* structure size in bytes (includes size field) */
49 u32 pending; /* pending sbios requests */
50 u8 panel_exp_mode; /* panel expansion mode */
51 u8 thermal_gfx; /* thermal state: target gfx controller */
52 u8 thermal_state; /* thermal state: state id (0: exit state, non-0: state) */
53 u8 forced_power_gfx; /* forced power state: target gfx controller */
54 u8 forced_power_state; /* forced power state: state id */
55 u8 system_power_src; /* system power source */
56 u8 backlight_level; /* panel backlight level (0-255) */
59 #define ATIF_NOTIFY_MASK 0x3
60 #define ATIF_NOTIFY_NONE 0
61 #define ATIF_NOTIFY_81 1
62 #define ATIF_NOTIFY_N 2
64 struct atcs_verify_interface {
65 u16 size; /* structure size in bytes (includes size field) */
66 u16 version; /* version */
67 u32 function_bits; /* supported functions bit vector */
70 #define ATCS_VALID_FLAGS_MASK 0x3
72 struct atcs_pref_req_input {
73 u16 size; /* structure size in bytes (includes size field) */
74 u16 client_id; /* client id (bit 2-0: func num, 7-3: dev num, 15-8: bus num) */
75 u16 valid_flags_mask; /* valid flags mask */
76 u16 flags; /* flags */
77 u8 req_type; /* request type */
78 u8 perf_req; /* performance request */
81 struct atcs_pref_req_output {
82 u16 size; /* structure size in bytes (includes size field) */
83 u8 ret_val; /* return value */
86 /* Call the ATIF method
89 * radeon_atif_call - call an ATIF method
91 * @handle: acpi handle
92 * @function: the ATIF function to execute
93 * @params: ATIF function params
95 * Executes the requested ATIF function (all asics).
96 * Returns a pointer to the acpi output buffer.
98 static ACPI_OBJECT *radeon_atif_call(ACPI_HANDLE handle, int function,
102 ACPI_OBJECT atif_arg_elements[2];
103 ACPI_OBJECT_LIST atif_arg;
104 ACPI_BUFFER buffer = { ACPI_ALLOCATE_BUFFER, NULL };
107 atif_arg.Pointer = &atif_arg_elements[0];
109 atif_arg_elements[0].Type = ACPI_TYPE_INTEGER;
110 atif_arg_elements[0].Integer.Value = function;
113 atif_arg_elements[1].Type = ACPI_TYPE_BUFFER;
114 atif_arg_elements[1].Buffer.Length = params->Length;
115 atif_arg_elements[1].Buffer.Pointer = params->Pointer;
117 /* We need a second fake parameter */
118 atif_arg_elements[1].Type = ACPI_TYPE_INTEGER;
119 atif_arg_elements[1].Integer.Value = 0;
122 status = AcpiEvaluateObject(handle, "ATIF", &atif_arg, &buffer);
124 /* Fail only if calling the method fails and ATIF is supported */
125 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
126 DRM_DEBUG_DRIVER("failed to evaluate ATIF got %s\n",
127 AcpiFormatException(status));
128 AcpiOsFree(buffer.Pointer);
132 return buffer.Pointer;
136 * radeon_atif_parse_notification - parse supported notifications
138 * @n: supported notifications struct
139 * @mask: supported notifications mask from ATIF
141 * Use the supported notifications mask from ATIF function
142 * ATIF_FUNCTION_VERIFY_INTERFACE to determine what notifications
143 * are supported (all asics).
145 static void radeon_atif_parse_notification(struct radeon_atif_notifications *n, u32 mask)
147 n->display_switch = mask & ATIF_DISPLAY_SWITCH_REQUEST_SUPPORTED;
148 n->expansion_mode_change = mask & ATIF_EXPANSION_MODE_CHANGE_REQUEST_SUPPORTED;
149 n->thermal_state = mask & ATIF_THERMAL_STATE_CHANGE_REQUEST_SUPPORTED;
150 n->forced_power_state = mask & ATIF_FORCED_POWER_STATE_CHANGE_REQUEST_SUPPORTED;
151 n->system_power_state = mask & ATIF_SYSTEM_POWER_SOURCE_CHANGE_REQUEST_SUPPORTED;
152 n->display_conf_change = mask & ATIF_DISPLAY_CONF_CHANGE_REQUEST_SUPPORTED;
153 n->px_gfx_switch = mask & ATIF_PX_GFX_SWITCH_REQUEST_SUPPORTED;
154 n->brightness_change = mask & ATIF_PANEL_BRIGHTNESS_CHANGE_REQUEST_SUPPORTED;
155 n->dgpu_display_event = mask & ATIF_DGPU_DISPLAY_EVENT_SUPPORTED;
159 * radeon_atif_parse_functions - parse supported functions
161 * @f: supported functions struct
162 * @mask: supported functions mask from ATIF
164 * Use the supported functions mask from ATIF function
165 * ATIF_FUNCTION_VERIFY_INTERFACE to determine what functions
166 * are supported (all asics).
168 static void radeon_atif_parse_functions(struct radeon_atif_functions *f, u32 mask)
170 f->system_params = mask & ATIF_GET_SYSTEM_PARAMETERS_SUPPORTED;
171 f->sbios_requests = mask & ATIF_GET_SYSTEM_BIOS_REQUESTS_SUPPORTED;
172 f->select_active_disp = mask & ATIF_SELECT_ACTIVE_DISPLAYS_SUPPORTED;
173 f->lid_state = mask & ATIF_GET_LID_STATE_SUPPORTED;
174 f->get_tv_standard = mask & ATIF_GET_TV_STANDARD_FROM_CMOS_SUPPORTED;
175 f->set_tv_standard = mask & ATIF_SET_TV_STANDARD_IN_CMOS_SUPPORTED;
176 f->get_panel_expansion_mode = mask & ATIF_GET_PANEL_EXPANSION_MODE_FROM_CMOS_SUPPORTED;
177 f->set_panel_expansion_mode = mask & ATIF_SET_PANEL_EXPANSION_MODE_IN_CMOS_SUPPORTED;
178 f->temperature_change = mask & ATIF_TEMPERATURE_CHANGE_NOTIFICATION_SUPPORTED;
179 f->graphics_device_types = mask & ATIF_GET_GRAPHICS_DEVICE_TYPES_SUPPORTED;
183 * radeon_atif_verify_interface - verify ATIF
185 * @handle: acpi handle
186 * @atif: radeon atif struct
188 * Execute the ATIF_FUNCTION_VERIFY_INTERFACE ATIF function
189 * to initialize ATIF and determine what features are supported
191 * returns 0 on success, error on failure.
193 static int radeon_atif_verify_interface(ACPI_HANDLE handle,
194 struct radeon_atif *atif)
197 struct atif_verify_interface output;
201 info = radeon_atif_call(handle, ATIF_FUNCTION_VERIFY_INTERFACE, NULL);
205 memset(&output, 0, sizeof(output));
207 size = *(u16 *) info->Buffer.Pointer;
209 DRM_INFO("ATIF buffer is too small: %zu\n", size);
213 size = min(sizeof(output), size);
215 memcpy(&output, info->Buffer.Pointer, size);
217 /* TODO: check version? */
218 DRM_DEBUG_DRIVER("ATIF version %u\n", output.version);
220 radeon_atif_parse_notification(&atif->notifications, output.notification_mask);
221 radeon_atif_parse_functions(&atif->functions, output.function_bits);
229 * radeon_atif_get_notification_params - determine notify configuration
231 * @handle: acpi handle
232 * @n: atif notification configuration struct
234 * Execute the ATIF_FUNCTION_GET_SYSTEM_PARAMETERS ATIF function
235 * to determine if a notifier is used and if so which one
236 * (all asics). This is either Notify(VGA, 0x81) or Notify(VGA, n)
237 * where n is specified in the result if a notifier is used.
238 * Returns 0 on success, error on failure.
240 static int radeon_atif_get_notification_params(ACPI_HANDLE handle,
241 struct radeon_atif_notification_cfg *n)
244 struct atif_system_params params;
248 info = radeon_atif_call(handle, ATIF_FUNCTION_GET_SYSTEM_PARAMETERS, NULL);
254 size = *(u16 *) info->Buffer.Pointer;
260 memset(¶ms, 0, sizeof(params));
261 size = min(sizeof(params), size);
262 memcpy(¶ms, info->Buffer.Pointer, size);
264 DRM_DEBUG_DRIVER("SYSTEM_PARAMS: mask = %#x, flags = %#x\n",
265 params.flags, params.valid_mask);
266 params.flags = params.flags & params.valid_mask;
268 if ((params.flags & ATIF_NOTIFY_MASK) == ATIF_NOTIFY_NONE) {
271 } else if ((params.flags & ATIF_NOTIFY_MASK) == ATIF_NOTIFY_81) {
273 n->command_code = 0x81;
280 n->command_code = params.command_code;
284 DRM_DEBUG_DRIVER("Notification %s, command code = %#x\n",
285 (n->enabled ? "enabled" : "disabled"),
292 * radeon_atif_get_sbios_requests - get requested sbios event
294 * @handle: acpi handle
295 * @req: atif sbios request struct
297 * Execute the ATIF_FUNCTION_GET_SYSTEM_BIOS_REQUESTS ATIF function
298 * to determine what requests the sbios is making to the driver
300 * Returns 0 on success, error on failure.
302 static int radeon_atif_get_sbios_requests(ACPI_HANDLE handle,
303 struct atif_sbios_requests *req)
309 info = radeon_atif_call(handle, ATIF_FUNCTION_GET_SYSTEM_BIOS_REQUESTS, NULL);
313 size = *(u16 *)info->Buffer.Pointer;
318 memset(req, 0, sizeof(*req));
320 size = min(sizeof(*req), size);
321 memcpy(req, info->Buffer.Pointer, size);
322 DRM_DEBUG_DRIVER("SBIOS pending requests: %#x\n", req->pending);
324 count = hweight32(req->pending);
332 * radeon_atif_handler - handle ATIF notify requests
334 * @rdev: radeon_device pointer
335 * @event: atif sbios request struct
337 * Checks the acpi event and if it matches an atif event,
339 * Returns NOTIFY code
341 void radeon_atif_handler(struct radeon_device *rdev,
344 struct radeon_atif *atif = &rdev->atif;
345 struct atif_sbios_requests req;
349 DRM_DEBUG_DRIVER("event, type = %#x\n",
352 if (!atif->notification_cfg.enabled ||
353 type != atif->notification_cfg.command_code)
357 /* Check pending SBIOS requests */
358 handle = rdev->acpi.handle;
359 count = radeon_atif_get_sbios_requests(handle, &req);
364 DRM_DEBUG_DRIVER("ATIF: %d pending SBIOS requests\n", count);
366 if (req.pending & ATIF_PANEL_BRIGHTNESS_CHANGE_REQUEST) {
367 struct radeon_encoder *enc = atif->encoder_for_bl;
370 DRM_DEBUG_DRIVER("Changing brightness to %d\n",
371 req.backlight_level);
373 radeon_set_backlight_level(rdev, enc, req.backlight_level);
376 if (rdev->is_atom_bios) {
377 struct radeon_encoder_atom_dig *dig = enc->enc_priv;
378 backlight_force_update(dig->bl_dev,
379 BACKLIGHT_UPDATE_HOTKEY);
381 struct radeon_encoder_lvds *dig = enc->enc_priv;
382 backlight_force_update(dig->bl_dev,
383 BACKLIGHT_UPDATE_HOTKEY);
385 #endif /* DUMBBELL_WIP */
388 /* TODO: check other events */
390 /* We've handled the event, stop the notifier chain. The ACPI interface
391 * overloads ACPI_VIDEO_NOTIFY_PROBE, we don't want to send that to
392 * userspace if the event was generated only to signal a SBIOS
397 /* Call the ATCS method
400 * radeon_atcs_call - call an ATCS method
402 * @handle: acpi handle
403 * @function: the ATCS function to execute
404 * @params: ATCS function params
406 * Executes the requested ATCS function (all asics).
407 * Returns a pointer to the acpi output buffer.
409 static union acpi_object *radeon_atcs_call(ACPI_HANDLE handle, int function,
413 ACPI_OBJECT atcs_arg_elements[2];
414 ACPI_OBJECT_LIST atcs_arg;
415 ACPI_BUFFER buffer = { ACPI_ALLOCATE_BUFFER, NULL };
418 atcs_arg.Pointer = &atcs_arg_elements[0];
420 atcs_arg_elements[0].Type = ACPI_TYPE_INTEGER;
421 atcs_arg_elements[0].Integer.Value = function;
424 atcs_arg_elements[1].Type = ACPI_TYPE_BUFFER;
425 atcs_arg_elements[1].Buffer.Length = params->Length;
426 atcs_arg_elements[1].Buffer.Pointer = params->Pointer;
428 /* We need a second fake parameter */
429 atcs_arg_elements[1].Type = ACPI_TYPE_INTEGER;
430 atcs_arg_elements[1].Integer.Value = 0;
433 status = AcpiEvaluateObject(handle, "ATCS", &atcs_arg, &buffer);
435 /* Fail only if calling the method fails and ATIF is supported */
436 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
437 DRM_DEBUG_DRIVER("failed to evaluate ATCS got %s\n",
438 AcpiFormatException(status));
439 AcpiOsFree(buffer.Pointer);
443 return buffer.Pointer;
447 * radeon_atcs_parse_functions - parse supported functions
449 * @f: supported functions struct
450 * @mask: supported functions mask from ATCS
452 * Use the supported functions mask from ATCS function
453 * ATCS_FUNCTION_VERIFY_INTERFACE to determine what functions
454 * are supported (all asics).
456 static void radeon_atcs_parse_functions(struct radeon_atcs_functions *f, u32 mask)
458 f->get_ext_state = mask & ATCS_GET_EXTERNAL_STATE_SUPPORTED;
459 f->pcie_perf_req = mask & ATCS_PCIE_PERFORMANCE_REQUEST_SUPPORTED;
460 f->pcie_dev_rdy = mask & ATCS_PCIE_DEVICE_READY_NOTIFICATION_SUPPORTED;
461 f->pcie_bus_width = mask & ATCS_SET_PCIE_BUS_WIDTH_SUPPORTED;
465 * radeon_atcs_verify_interface - verify ATCS
467 * @handle: acpi handle
468 * @atcs: radeon atcs struct
470 * Execute the ATCS_FUNCTION_VERIFY_INTERFACE ATCS function
471 * to initialize ATCS and determine what features are supported
473 * returns 0 on success, error on failure.
475 static int radeon_atcs_verify_interface(ACPI_HANDLE handle,
476 struct radeon_atcs *atcs)
479 struct atcs_verify_interface output;
483 info = radeon_atcs_call(handle, ATCS_FUNCTION_VERIFY_INTERFACE, NULL);
487 memset(&output, 0, sizeof(output));
489 size = *(u16 *) info->Buffer.Pointer;
491 DRM_INFO("ATCS buffer is too small: %zu\n", size);
495 size = min(sizeof(output), size);
497 memcpy(&output, info->Buffer.Pointer, size);
499 /* TODO: check version? */
500 DRM_DEBUG_DRIVER("ATCS version %u\n", output.version);
502 radeon_atcs_parse_functions(&atcs->functions, output.function_bits);
510 * radeon_acpi_is_pcie_performance_request_supported
512 * @rdev: radeon_device pointer
514 * Check if the ATCS pcie_perf_req and pcie_dev_rdy methods
515 * are supported (all asics).
516 * returns true if supported, false if not.
518 bool radeon_acpi_is_pcie_performance_request_supported(struct radeon_device *rdev)
520 struct radeon_atcs *atcs = &rdev->atcs;
522 if (atcs->functions.pcie_perf_req && atcs->functions.pcie_dev_rdy)
529 * radeon_acpi_pcie_notify_device_ready
531 * @rdev: radeon_device pointer
533 * Executes the PCIE_DEVICE_READY_NOTIFICATION method
535 * returns 0 on success, error on failure.
537 int radeon_acpi_pcie_notify_device_ready(struct radeon_device *rdev)
541 union acpi_object *info;
542 struct radeon_atcs *atcs = &rdev->atcs;
544 /* Get the device handle */
545 handle = DEVICE_ACPI_HANDLE(&rdev->pdev->dev);
549 if (!atcs->functions.pcie_dev_rdy)
552 info = radeon_atcs_call(handle, ATCS_FUNCTION_PCIE_DEVICE_READY_NOTIFICATION, NULL);
565 * radeon_acpi_pcie_performance_request
567 * @rdev: radeon_device pointer
568 * @perf_req: requested perf level (pcie gen speed)
569 * @advertise: set advertise caps flag if set
571 * Executes the PCIE_PERFORMANCE_REQUEST method to
572 * change the pcie gen speed (all asics).
573 * returns 0 on success, error on failure.
575 int radeon_acpi_pcie_performance_request(struct radeon_device *rdev,
576 u8 perf_req, bool advertise)
580 union acpi_object *info;
581 struct radeon_atcs *atcs = &rdev->atcs;
582 struct atcs_pref_req_input atcs_input;
583 struct atcs_pref_req_output atcs_output;
584 struct acpi_buffer params;
588 /* Get the device handle */
589 handle = DEVICE_ACPI_HANDLE(&rdev->pdev->dev);
593 if (!atcs->functions.pcie_perf_req)
596 atcs_input.size = sizeof(struct atcs_pref_req_input);
597 /* client id (bit 2-0: func num, 7-3: dev num, 15-8: bus num) */
598 atcs_input.client_id = rdev->pdev->devfn | (rdev->pdev->bus->number << 8);
599 atcs_input.valid_flags_mask = ATCS_VALID_FLAGS_MASK;
600 atcs_input.flags = ATCS_WAIT_FOR_COMPLETION;
602 atcs_input.flags |= ATCS_ADVERTISE_CAPS;
603 atcs_input.req_type = ATCS_PCIE_LINK_SPEED;
604 atcs_input.perf_req = perf_req;
606 params.length = sizeof(struct atcs_pref_req_input);
607 params.pointer = &atcs_input;
610 info = radeon_atcs_call(handle, ATCS_FUNCTION_PCIE_PERFORMANCE_REQUEST, ¶ms);
614 memset(&atcs_output, 0, sizeof(atcs_output));
616 size = *(u16 *) info->buffer.pointer;
618 DRM_INFO("ATCS buffer is too small: %zu\n", size);
622 size = min(sizeof(atcs_output), size);
624 memcpy(&atcs_output, info->buffer.pointer, size);
628 switch (atcs_output.ret_val) {
629 case ATCS_REQUEST_REFUSED:
632 case ATCS_REQUEST_COMPLETE:
634 case ATCS_REQUEST_IN_PROGRESS:
647 * radeon_acpi_event - handle notify events
649 * @nb: notifier block
653 * Calls relevant radeon functions in response to various
655 * Returns NOTIFY code
657 static void radeon_acpi_event(ACPI_HANDLE handle, UINT32 type,
660 struct radeon_device *rdev = (struct radeon_device *)context;
663 if (strcmp(entry->device_class, ACPI_AC_CLASS) == 0) {
664 if (power_supply_is_system_supplied() > 0)
665 DRM_DEBUG_DRIVER("pm: AC\n");
667 DRM_DEBUG_DRIVER("pm: DC\n");
669 radeon_pm_acpi_event_handler(rdev);
671 #endif /* DUMBBELL_WIP */
673 /* Check for pending SBIOS requests */
674 radeon_atif_handler(rdev, type);
677 /* Call all ACPI methods here */
679 * radeon_acpi_init - init driver acpi support
681 * @rdev: radeon_device pointer
683 * Verifies the AMD ACPI interfaces and registers with the acpi
684 * notifier chain (all asics).
685 * Returns 0 on success, error on failure.
687 int radeon_acpi_init(struct radeon_device *rdev)
690 struct radeon_atif *atif = &rdev->atif;
691 struct radeon_atcs *atcs = &rdev->atcs;
694 /* Get the device handle */
695 handle = acpi_get_handle(rdev->dev);
697 /* No need to proceed if we're sure that ATIF is not supported */
698 if (!ASIC_IS_AVIVO(rdev) || !rdev->bios || !handle)
701 /* Call the ATCS method */
702 ret = radeon_atcs_verify_interface(handle, atcs);
704 DRM_DEBUG_DRIVER("Call to ATCS verify_interface failed: %d\n", ret);
707 /* Call the ATIF method */
708 ret = radeon_atif_verify_interface(handle, atif);
710 DRM_DEBUG_DRIVER("Call to ATIF verify_interface failed: %d\n", ret);
714 if (atif->notifications.brightness_change) {
715 struct drm_encoder *tmp;
716 struct radeon_encoder *target = NULL;
718 /* Find the encoder controlling the brightness */
719 list_for_each_entry(tmp, &rdev->ddev->mode_config.encoder_list,
721 struct radeon_encoder *enc = to_radeon_encoder(tmp);
723 if ((enc->devices & (ATOM_DEVICE_LCD_SUPPORT)) &&
725 if (rdev->is_atom_bios) {
726 struct radeon_encoder_atom_dig *dig = enc->enc_priv;
732 struct radeon_encoder_lvds *dig = enc->enc_priv;
741 atif->encoder_for_bl = target;
743 /* Brightness change notification is enabled, but we
744 * didn't find a backlight controller, this should
747 DRM_ERROR("Cannot find a backlight controller\n");
751 if (atif->functions.sbios_requests && !atif->functions.system_params) {
752 /* XXX check this workraround, if sbios request function is
753 * present we have to see how it's configured in the system
756 atif->functions.system_params = true;
759 if (atif->functions.system_params) {
760 ret = radeon_atif_get_notification_params(handle,
761 &atif->notification_cfg);
763 DRM_DEBUG_DRIVER("Call to GET_SYSTEM_PARAMS failed: %d\n",
765 /* Disable notification */
766 atif->notification_cfg.enabled = false;
771 rdev->acpi.handle = handle;
772 rdev->acpi.notifier_call = radeon_acpi_event;
773 AcpiInstallNotifyHandler(handle, ACPI_DEVICE_NOTIFY,
774 rdev->acpi.notifier_call, rdev);
780 * radeon_acpi_fini - tear down driver acpi support
782 * @rdev: radeon_device pointer
784 * Unregisters with the acpi notifier chain (all asics).
786 void radeon_acpi_fini(struct radeon_device *rdev)
788 AcpiRemoveNotifyHandler(rdev->acpi.handle, ACPI_DEVICE_NOTIFY,
789 rdev->acpi.notifier_call);