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.
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 <linux/suspend.h>
30 #include <acpi/video.h>
31 #include <acpi/actbl.h>
33 #include <drm/drm_crtc_helper.h>
35 #include "amdgpu_pm.h"
36 #include "amdgpu_display.h"
40 struct amdgpu_atif_notification_cfg {
45 struct amdgpu_atif_notifications {
47 bool forced_power_state;
48 bool system_power_state;
49 bool brightness_change;
50 bool dgpu_display_event;
51 bool gpu_package_power_limit;
54 struct amdgpu_atif_functions {
57 bool temperature_change;
58 bool query_backlight_transfer_characteristics;
60 bool external_gpu_information;
66 struct amdgpu_atif_notifications notifications;
67 struct amdgpu_atif_functions functions;
68 struct amdgpu_atif_notification_cfg notification_cfg;
69 #if defined(CONFIG_BACKLIGHT_CLASS_DEVICE) || defined(CONFIG_BACKLIGHT_CLASS_DEVICE_MODULE)
70 struct backlight_device *bd;
72 struct amdgpu_dm_backlight_caps backlight_caps;
75 struct amdgpu_atcs_functions {
80 bool power_shift_control;
86 struct amdgpu_atcs_functions functions;
89 static struct amdgpu_acpi_priv {
90 struct amdgpu_atif atif;
91 struct amdgpu_atcs atcs;
94 /* Call the ATIF method
97 * amdgpu_atif_call - call an ATIF method
99 * @atif: atif structure
100 * @function: the ATIF function to execute
101 * @params: ATIF function params
103 * Executes the requested ATIF function (all asics).
104 * Returns a pointer to the acpi output buffer.
106 static union acpi_object *amdgpu_atif_call(struct amdgpu_atif *atif,
108 struct acpi_buffer *params)
111 union acpi_object atif_arg_elements[2];
112 struct acpi_object_list atif_arg;
113 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
116 atif_arg.pointer = &atif_arg_elements[0];
118 atif_arg_elements[0].type = ACPI_TYPE_INTEGER;
119 atif_arg_elements[0].integer.value = function;
122 atif_arg_elements[1].type = ACPI_TYPE_BUFFER;
123 atif_arg_elements[1].buffer.length = params->length;
124 atif_arg_elements[1].buffer.pointer = params->pointer;
126 /* We need a second fake parameter */
127 atif_arg_elements[1].type = ACPI_TYPE_INTEGER;
128 atif_arg_elements[1].integer.value = 0;
131 status = acpi_evaluate_object(atif->handle, NULL, &atif_arg,
134 /* Fail only if calling the method fails and ATIF is supported */
135 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
136 DRM_DEBUG_DRIVER("failed to evaluate ATIF got %s\n",
137 acpi_format_exception(status));
138 kfree(buffer.pointer);
142 return buffer.pointer;
146 * amdgpu_atif_parse_notification - parse supported notifications
148 * @n: supported notifications struct
149 * @mask: supported notifications mask from ATIF
151 * Use the supported notifications mask from ATIF function
152 * ATIF_FUNCTION_VERIFY_INTERFACE to determine what notifications
153 * are supported (all asics).
155 static void amdgpu_atif_parse_notification(struct amdgpu_atif_notifications *n, u32 mask)
157 n->thermal_state = mask & ATIF_THERMAL_STATE_CHANGE_REQUEST_SUPPORTED;
158 n->forced_power_state = mask & ATIF_FORCED_POWER_STATE_CHANGE_REQUEST_SUPPORTED;
159 n->system_power_state = mask & ATIF_SYSTEM_POWER_SOURCE_CHANGE_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 n->gpu_package_power_limit = mask & ATIF_GPU_PACKAGE_POWER_LIMIT_REQUEST_SUPPORTED;
166 * amdgpu_atif_parse_functions - parse supported functions
168 * @f: supported functions struct
169 * @mask: supported functions mask from ATIF
171 * Use the supported functions mask from ATIF function
172 * ATIF_FUNCTION_VERIFY_INTERFACE to determine what functions
173 * are supported (all asics).
175 static void amdgpu_atif_parse_functions(struct amdgpu_atif_functions *f, u32 mask)
177 f->system_params = mask & ATIF_GET_SYSTEM_PARAMETERS_SUPPORTED;
178 f->sbios_requests = mask & ATIF_GET_SYSTEM_BIOS_REQUESTS_SUPPORTED;
179 f->temperature_change = mask & ATIF_TEMPERATURE_CHANGE_NOTIFICATION_SUPPORTED;
180 f->query_backlight_transfer_characteristics =
181 mask & ATIF_QUERY_BACKLIGHT_TRANSFER_CHARACTERISTICS_SUPPORTED;
182 f->ready_to_undock = mask & ATIF_READY_TO_UNDOCK_NOTIFICATION_SUPPORTED;
183 f->external_gpu_information = mask & ATIF_GET_EXTERNAL_GPU_INFORMATION_SUPPORTED;
187 * amdgpu_atif_verify_interface - verify ATIF
189 * @atif: amdgpu atif struct
191 * Execute the ATIF_FUNCTION_VERIFY_INTERFACE ATIF function
192 * to initialize ATIF and determine what features are supported
194 * returns 0 on success, error on failure.
196 static int amdgpu_atif_verify_interface(struct amdgpu_atif *atif)
198 union acpi_object *info;
199 struct atif_verify_interface output;
203 info = amdgpu_atif_call(atif, ATIF_FUNCTION_VERIFY_INTERFACE, NULL);
207 memset(&output, 0, sizeof(output));
209 size = *(u16 *) info->buffer.pointer;
211 DRM_INFO("ATIF buffer is too small: %zu\n", size);
215 size = min(sizeof(output), size);
217 memcpy(&output, info->buffer.pointer, size);
219 /* TODO: check version? */
220 DRM_DEBUG_DRIVER("ATIF version %u\n", output.version);
222 amdgpu_atif_parse_notification(&atif->notifications, output.notification_mask);
223 amdgpu_atif_parse_functions(&atif->functions, output.function_bits);
231 * amdgpu_atif_get_notification_params - determine notify configuration
235 * Execute the ATIF_FUNCTION_GET_SYSTEM_PARAMETERS ATIF function
236 * to determine if a notifier is used and if so which one
237 * (all asics). This is either Notify(VGA, 0x81) or Notify(VGA, n)
238 * where n is specified in the result if a notifier is used.
239 * Returns 0 on success, error on failure.
241 static int amdgpu_atif_get_notification_params(struct amdgpu_atif *atif)
243 union acpi_object *info;
244 struct amdgpu_atif_notification_cfg *n = &atif->notification_cfg;
245 struct atif_system_params params;
249 info = amdgpu_atif_call(atif, ATIF_FUNCTION_GET_SYSTEM_PARAMETERS,
256 size = *(u16 *) info->buffer.pointer;
262 memset(¶ms, 0, sizeof(params));
263 size = min(sizeof(params), size);
264 memcpy(¶ms, info->buffer.pointer, size);
266 DRM_DEBUG_DRIVER("SYSTEM_PARAMS: mask = %#x, flags = %#x\n",
267 params.flags, params.valid_mask);
268 params.flags = params.flags & params.valid_mask;
270 if ((params.flags & ATIF_NOTIFY_MASK) == ATIF_NOTIFY_NONE) {
273 } else if ((params.flags & ATIF_NOTIFY_MASK) == ATIF_NOTIFY_81) {
275 n->command_code = 0x81;
282 n->command_code = params.command_code;
286 DRM_DEBUG_DRIVER("Notification %s, command code = %#x\n",
287 (n->enabled ? "enabled" : "disabled"),
294 * amdgpu_atif_query_backlight_caps - get min and max backlight input signal
298 * Execute the QUERY_BRIGHTNESS_TRANSFER_CHARACTERISTICS ATIF function
299 * to determine the acceptable range of backlight values
301 * Backlight_caps.caps_valid will be set to true if the query is successful
303 * The input signals are in range 0-255
305 * This function assumes the display with backlight is the first LCD
307 * Returns 0 on success, error on failure.
309 static int amdgpu_atif_query_backlight_caps(struct amdgpu_atif *atif)
311 union acpi_object *info;
312 struct atif_qbtc_output characteristics;
313 struct atif_qbtc_arguments arguments;
314 struct acpi_buffer params;
318 arguments.size = sizeof(arguments);
319 arguments.requested_display = ATIF_QBTC_REQUEST_LCD1;
321 params.length = sizeof(arguments);
322 params.pointer = (void *)&arguments;
324 info = amdgpu_atif_call(atif,
325 ATIF_FUNCTION_QUERY_BRIGHTNESS_TRANSFER_CHARACTERISTICS,
332 size = *(u16 *) info->buffer.pointer;
338 memset(&characteristics, 0, sizeof(characteristics));
339 size = min(sizeof(characteristics), size);
340 memcpy(&characteristics, info->buffer.pointer, size);
342 atif->backlight_caps.caps_valid = true;
343 atif->backlight_caps.min_input_signal =
344 characteristics.min_input_signal;
345 atif->backlight_caps.max_input_signal =
346 characteristics.max_input_signal;
353 * amdgpu_atif_get_sbios_requests - get requested sbios event
356 * @req: atif sbios request struct
358 * Execute the ATIF_FUNCTION_GET_SYSTEM_BIOS_REQUESTS ATIF function
359 * to determine what requests the sbios is making to the driver
361 * Returns 0 on success, error on failure.
363 static int amdgpu_atif_get_sbios_requests(struct amdgpu_atif *atif,
364 struct atif_sbios_requests *req)
366 union acpi_object *info;
370 info = amdgpu_atif_call(atif, ATIF_FUNCTION_GET_SYSTEM_BIOS_REQUESTS,
375 size = *(u16 *)info->buffer.pointer;
380 memset(req, 0, sizeof(*req));
382 size = min(sizeof(*req), size);
383 memcpy(req, info->buffer.pointer, size);
384 DRM_DEBUG_DRIVER("SBIOS pending requests: %#x\n", req->pending);
386 count = hweight32(req->pending);
394 * amdgpu_atif_handler - handle ATIF notify requests
396 * @adev: amdgpu_device pointer
397 * @event: atif sbios request struct
399 * Checks the acpi event and if it matches an atif event,
403 * NOTIFY_BAD or NOTIFY_DONE, depending on the event.
405 static int amdgpu_atif_handler(struct amdgpu_device *adev,
406 struct acpi_bus_event *event)
408 struct amdgpu_atif *atif = &amdgpu_acpi_priv.atif;
411 DRM_DEBUG_DRIVER("event, device_class = %s, type = %#x\n",
412 event->device_class, event->type);
414 if (strcmp(event->device_class, ACPI_VIDEO_CLASS) != 0)
417 /* Is this actually our event? */
418 if (!atif->notification_cfg.enabled ||
419 event->type != atif->notification_cfg.command_code) {
420 /* These events will generate keypresses otherwise */
421 if (event->type == ACPI_VIDEO_NOTIFY_PROBE)
427 if (atif->functions.sbios_requests) {
428 struct atif_sbios_requests req;
430 /* Check pending SBIOS requests */
431 count = amdgpu_atif_get_sbios_requests(atif, &req);
436 DRM_DEBUG_DRIVER("ATIF: %d pending SBIOS requests\n", count);
438 if (req.pending & ATIF_PANEL_BRIGHTNESS_CHANGE_REQUEST) {
439 #if defined(CONFIG_BACKLIGHT_CLASS_DEVICE) || defined(CONFIG_BACKLIGHT_CLASS_DEVICE_MODULE)
441 DRM_DEBUG_DRIVER("Changing brightness to %d\n",
442 req.backlight_level);
444 * XXX backlight_device_set_brightness() is
445 * hardwired to post BACKLIGHT_UPDATE_SYSFS.
446 * It probably should accept 'reason' parameter.
448 backlight_device_set_brightness(atif->bd, req.backlight_level);
453 if (req.pending & ATIF_DGPU_DISPLAY_EVENT) {
454 if (adev->flags & AMD_IS_PX) {
455 pm_runtime_get_sync(adev_to_drm(adev)->dev);
456 /* Just fire off a uevent and let userspace tell us what to do */
457 drm_helper_hpd_irq_event(adev_to_drm(adev));
458 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
459 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
462 /* TODO: check other events */
465 /* We've handled the event, stop the notifier chain. The ACPI interface
466 * overloads ACPI_VIDEO_NOTIFY_PROBE, we don't want to send that to
467 * userspace if the event was generated only to signal a SBIOS
473 /* Call the ATCS method
476 * amdgpu_atcs_call - call an ATCS method
478 * @atcs: atcs structure
479 * @function: the ATCS function to execute
480 * @params: ATCS function params
482 * Executes the requested ATCS function (all asics).
483 * Returns a pointer to the acpi output buffer.
485 static union acpi_object *amdgpu_atcs_call(struct amdgpu_atcs *atcs,
487 struct acpi_buffer *params)
490 union acpi_object atcs_arg_elements[2];
491 struct acpi_object_list atcs_arg;
492 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
495 atcs_arg.pointer = &atcs_arg_elements[0];
497 atcs_arg_elements[0].type = ACPI_TYPE_INTEGER;
498 atcs_arg_elements[0].integer.value = function;
501 atcs_arg_elements[1].type = ACPI_TYPE_BUFFER;
502 atcs_arg_elements[1].buffer.length = params->length;
503 atcs_arg_elements[1].buffer.pointer = params->pointer;
505 /* We need a second fake parameter */
506 atcs_arg_elements[1].type = ACPI_TYPE_INTEGER;
507 atcs_arg_elements[1].integer.value = 0;
510 status = acpi_evaluate_object(atcs->handle, NULL, &atcs_arg, &buffer);
512 /* Fail only if calling the method fails and ATIF is supported */
513 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
514 DRM_DEBUG_DRIVER("failed to evaluate ATCS got %s\n",
515 acpi_format_exception(status));
516 kfree(buffer.pointer);
520 return buffer.pointer;
524 * amdgpu_atcs_parse_functions - parse supported functions
526 * @f: supported functions struct
527 * @mask: supported functions mask from ATCS
529 * Use the supported functions mask from ATCS function
530 * ATCS_FUNCTION_VERIFY_INTERFACE to determine what functions
531 * are supported (all asics).
533 static void amdgpu_atcs_parse_functions(struct amdgpu_atcs_functions *f, u32 mask)
535 f->get_ext_state = mask & ATCS_GET_EXTERNAL_STATE_SUPPORTED;
536 f->pcie_perf_req = mask & ATCS_PCIE_PERFORMANCE_REQUEST_SUPPORTED;
537 f->pcie_dev_rdy = mask & ATCS_PCIE_DEVICE_READY_NOTIFICATION_SUPPORTED;
538 f->pcie_bus_width = mask & ATCS_SET_PCIE_BUS_WIDTH_SUPPORTED;
539 f->power_shift_control = mask & ATCS_SET_POWER_SHIFT_CONTROL_SUPPORTED;
543 * amdgpu_atcs_verify_interface - verify ATCS
545 * @atcs: amdgpu atcs struct
547 * Execute the ATCS_FUNCTION_VERIFY_INTERFACE ATCS function
548 * to initialize ATCS and determine what features are supported
550 * returns 0 on success, error on failure.
552 static int amdgpu_atcs_verify_interface(struct amdgpu_atcs *atcs)
554 union acpi_object *info;
555 struct atcs_verify_interface output;
559 info = amdgpu_atcs_call(atcs, ATCS_FUNCTION_VERIFY_INTERFACE, NULL);
563 memset(&output, 0, sizeof(output));
565 size = *(u16 *) info->buffer.pointer;
567 DRM_INFO("ATCS buffer is too small: %zu\n", size);
571 size = min(sizeof(output), size);
573 memcpy(&output, info->buffer.pointer, size);
575 /* TODO: check version? */
576 DRM_DEBUG_DRIVER("ATCS version %u\n", output.version);
578 amdgpu_atcs_parse_functions(&atcs->functions, output.function_bits);
586 * amdgpu_acpi_is_pcie_performance_request_supported
588 * @adev: amdgpu_device pointer
590 * Check if the ATCS pcie_perf_req and pcie_dev_rdy methods
591 * are supported (all asics).
592 * returns true if supported, false if not.
594 bool amdgpu_acpi_is_pcie_performance_request_supported(struct amdgpu_device *adev)
596 struct amdgpu_atcs *atcs = &amdgpu_acpi_priv.atcs;
598 if (atcs->functions.pcie_perf_req && atcs->functions.pcie_dev_rdy)
605 * amdgpu_acpi_is_power_shift_control_supported
607 * Check if the ATCS power shift control method
609 * returns true if supported, false if not.
611 bool amdgpu_acpi_is_power_shift_control_supported(void)
613 return amdgpu_acpi_priv.atcs.functions.power_shift_control;
617 * amdgpu_acpi_pcie_notify_device_ready
619 * @adev: amdgpu_device pointer
621 * Executes the PCIE_DEVICE_READY_NOTIFICATION method
623 * returns 0 on success, error on failure.
625 int amdgpu_acpi_pcie_notify_device_ready(struct amdgpu_device *adev)
627 union acpi_object *info;
628 struct amdgpu_atcs *atcs = &amdgpu_acpi_priv.atcs;
630 if (!atcs->functions.pcie_dev_rdy)
633 info = amdgpu_atcs_call(atcs, ATCS_FUNCTION_PCIE_DEVICE_READY_NOTIFICATION, NULL);
643 * amdgpu_acpi_pcie_performance_request
645 * @adev: amdgpu_device pointer
646 * @perf_req: requested perf level (pcie gen speed)
647 * @advertise: set advertise caps flag if set
649 * Executes the PCIE_PERFORMANCE_REQUEST method to
650 * change the pcie gen speed (all asics).
651 * returns 0 on success, error on failure.
653 int amdgpu_acpi_pcie_performance_request(struct amdgpu_device *adev,
654 u8 perf_req, bool advertise)
656 union acpi_object *info;
657 struct amdgpu_atcs *atcs = &amdgpu_acpi_priv.atcs;
658 struct atcs_pref_req_input atcs_input;
659 struct atcs_pref_req_output atcs_output;
660 struct acpi_buffer params;
664 if (amdgpu_acpi_pcie_notify_device_ready(adev))
667 if (!atcs->functions.pcie_perf_req)
670 atcs_input.size = sizeof(struct atcs_pref_req_input);
671 /* client id (bit 2-0: func num, 7-3: dev num, 15-8: bus num) */
672 atcs_input.client_id = adev->pdev->devfn | (adev->pdev->bus->number << 8);
673 atcs_input.valid_flags_mask = ATCS_VALID_FLAGS_MASK;
674 atcs_input.flags = ATCS_WAIT_FOR_COMPLETION;
676 atcs_input.flags |= ATCS_ADVERTISE_CAPS;
677 atcs_input.req_type = ATCS_PCIE_LINK_SPEED;
678 atcs_input.perf_req = perf_req;
680 params.length = sizeof(struct atcs_pref_req_input);
681 params.pointer = &atcs_input;
684 info = amdgpu_atcs_call(atcs, ATCS_FUNCTION_PCIE_PERFORMANCE_REQUEST, ¶ms);
688 memset(&atcs_output, 0, sizeof(atcs_output));
690 size = *(u16 *) info->buffer.pointer;
692 DRM_INFO("ATCS buffer is too small: %zu\n", size);
696 size = min(sizeof(atcs_output), size);
698 memcpy(&atcs_output, info->buffer.pointer, size);
702 switch (atcs_output.ret_val) {
703 case ATCS_REQUEST_REFUSED:
706 case ATCS_REQUEST_COMPLETE:
708 case ATCS_REQUEST_IN_PROGRESS:
718 * amdgpu_acpi_power_shift_control
720 * @adev: amdgpu_device pointer
721 * @dev_state: device acpi state
722 * @drv_state: driver state
724 * Executes the POWER_SHIFT_CONTROL method to
725 * communicate current dGPU device state and
726 * driver state to APU/SBIOS.
727 * returns 0 on success, error on failure.
729 int amdgpu_acpi_power_shift_control(struct amdgpu_device *adev,
730 u8 dev_state, bool drv_state)
732 union acpi_object *info;
733 struct amdgpu_atcs *atcs = &amdgpu_acpi_priv.atcs;
734 struct atcs_pwr_shift_input atcs_input;
735 struct acpi_buffer params;
737 if (!amdgpu_acpi_is_power_shift_control_supported())
740 atcs_input.size = sizeof(struct atcs_pwr_shift_input);
741 /* dGPU id (bit 2-0: func num, 7-3: dev num, 15-8: bus num) */
742 atcs_input.dgpu_id = adev->pdev->devfn | (adev->pdev->bus->number << 8);
743 atcs_input.dev_acpi_state = dev_state;
744 atcs_input.drv_state = drv_state;
746 params.length = sizeof(struct atcs_pwr_shift_input);
747 params.pointer = &atcs_input;
749 info = amdgpu_atcs_call(atcs, ATCS_FUNCTION_POWER_SHIFT_CONTROL, ¶ms);
751 DRM_ERROR("ATCS PSC update failed\n");
759 * amdgpu_acpi_smart_shift_update - update dGPU device state to SBIOS
761 * @dev: drm_device pointer
762 * @ss_state: current smart shift event
764 * returns 0 on success,
765 * otherwise return error number.
767 int amdgpu_acpi_smart_shift_update(struct drm_device *dev, enum amdgpu_ss ss_state)
769 struct amdgpu_device *adev = drm_to_adev(dev);
772 if (!amdgpu_device_supports_smart_shift(dev))
776 /* SBIOS trigger “stop”, “enable” and “start” at D0, Driver Operational.
777 * SBIOS trigger “stop” at D3, Driver Not Operational.
778 * SBIOS trigger “stop” and “disable” at D0, Driver NOT operational.
780 case AMDGPU_SS_DRV_LOAD:
781 r = amdgpu_acpi_power_shift_control(adev,
782 AMDGPU_ATCS_PSC_DEV_STATE_D0,
783 AMDGPU_ATCS_PSC_DRV_STATE_OPR);
785 case AMDGPU_SS_DEV_D0:
786 r = amdgpu_acpi_power_shift_control(adev,
787 AMDGPU_ATCS_PSC_DEV_STATE_D0,
788 AMDGPU_ATCS_PSC_DRV_STATE_OPR);
790 case AMDGPU_SS_DEV_D3:
791 r = amdgpu_acpi_power_shift_control(adev,
792 AMDGPU_ATCS_PSC_DEV_STATE_D3_HOT,
793 AMDGPU_ATCS_PSC_DRV_STATE_NOT_OPR);
795 case AMDGPU_SS_DRV_UNLOAD:
796 r = amdgpu_acpi_power_shift_control(adev,
797 AMDGPU_ATCS_PSC_DEV_STATE_D0,
798 AMDGPU_ATCS_PSC_DRV_STATE_NOT_OPR);
808 * amdgpu_acpi_event - handle notify events
810 * @nb: notifier block
814 * Calls relevant amdgpu functions in response to various
816 * Returns NOTIFY code
818 static int amdgpu_acpi_event(struct notifier_block *nb,
822 struct amdgpu_device *adev = container_of(nb, struct amdgpu_device, acpi_nb);
823 struct acpi_bus_event *entry = (struct acpi_bus_event *)data;
825 if (strcmp(entry->device_class, ACPI_AC_CLASS) == 0) {
826 if (power_supply_is_system_supplied() > 0)
827 DRM_DEBUG_DRIVER("pm: AC\n");
829 DRM_DEBUG_DRIVER("pm: DC\n");
831 amdgpu_pm_acpi_event_handler(adev);
834 /* Check for pending SBIOS requests */
835 return amdgpu_atif_handler(adev, entry);
838 /* Call all ACPI methods here */
840 * amdgpu_acpi_init - init driver acpi support
842 * @adev: amdgpu_device pointer
844 * Verifies the AMD ACPI interfaces and registers with the acpi
845 * notifier chain (all asics).
846 * Returns 0 on success, error on failure.
848 int amdgpu_acpi_init(struct amdgpu_device *adev)
850 struct amdgpu_atif *atif = &amdgpu_acpi_priv.atif;
852 #if defined(CONFIG_BACKLIGHT_CLASS_DEVICE) || defined(CONFIG_BACKLIGHT_CLASS_DEVICE_MODULE)
853 if (atif->notifications.brightness_change) {
854 if (amdgpu_device_has_dc_support(adev)) {
855 #if defined(CONFIG_DRM_AMD_DC)
856 struct amdgpu_display_manager *dm = &adev->dm;
857 if (dm->backlight_dev[0])
858 atif->bd = dm->backlight_dev[0];
861 struct drm_encoder *tmp;
863 /* Find the encoder controlling the brightness */
864 list_for_each_entry(tmp, &adev_to_drm(adev)->mode_config.encoder_list,
866 struct amdgpu_encoder *enc = to_amdgpu_encoder(tmp);
868 if ((enc->devices & (ATOM_DEVICE_LCD_SUPPORT)) &&
870 struct amdgpu_encoder_atom_dig *dig = enc->enc_priv;
872 atif->bd = dig->bl_dev;
880 adev->acpi_nb.notifier_call = amdgpu_acpi_event;
881 register_acpi_notifier(&adev->acpi_nb);
886 void amdgpu_acpi_get_backlight_caps(struct amdgpu_dm_backlight_caps *caps)
888 struct amdgpu_atif *atif = &amdgpu_acpi_priv.atif;
890 caps->caps_valid = atif->backlight_caps.caps_valid;
891 caps->min_input_signal = atif->backlight_caps.min_input_signal;
892 caps->max_input_signal = atif->backlight_caps.max_input_signal;
896 * amdgpu_acpi_fini - tear down driver acpi support
898 * @adev: amdgpu_device pointer
900 * Unregisters with the acpi notifier chain (all asics).
902 void amdgpu_acpi_fini(struct amdgpu_device *adev)
904 unregister_acpi_notifier(&adev->acpi_nb);
908 * amdgpu_atif_pci_probe_handle - look up the ATIF handle
912 * Look up the ATIF handles (all asics).
913 * Returns true if the handle is found, false if not.
915 static bool amdgpu_atif_pci_probe_handle(struct pci_dev *pdev)
917 char acpi_method_name[255] = { 0 };
918 struct acpi_buffer buffer = {sizeof(acpi_method_name), acpi_method_name};
919 acpi_handle dhandle, atif_handle;
923 dhandle = ACPI_HANDLE(&pdev->dev);
927 status = acpi_get_handle(dhandle, "ATIF", &atif_handle);
928 if (ACPI_FAILURE(status)) {
931 amdgpu_acpi_priv.atif.handle = atif_handle;
932 acpi_get_name(amdgpu_acpi_priv.atif.handle, ACPI_FULL_PATHNAME, &buffer);
933 DRM_DEBUG_DRIVER("Found ATIF handle %s\n", acpi_method_name);
934 ret = amdgpu_atif_verify_interface(&amdgpu_acpi_priv.atif);
936 amdgpu_acpi_priv.atif.handle = 0;
943 * amdgpu_atcs_pci_probe_handle - look up the ATCS handle
947 * Look up the ATCS handles (all asics).
948 * Returns true if the handle is found, false if not.
950 static bool amdgpu_atcs_pci_probe_handle(struct pci_dev *pdev)
952 char acpi_method_name[255] = { 0 };
953 struct acpi_buffer buffer = { sizeof(acpi_method_name), acpi_method_name };
954 acpi_handle dhandle, atcs_handle;
958 dhandle = ACPI_HANDLE(&pdev->dev);
962 status = acpi_get_handle(dhandle, "ATCS", &atcs_handle);
963 if (ACPI_FAILURE(status)) {
966 amdgpu_acpi_priv.atcs.handle = atcs_handle;
967 acpi_get_name(amdgpu_acpi_priv.atcs.handle, ACPI_FULL_PATHNAME, &buffer);
968 DRM_DEBUG_DRIVER("Found ATCS handle %s\n", acpi_method_name);
969 ret = amdgpu_atcs_verify_interface(&amdgpu_acpi_priv.atcs);
971 amdgpu_acpi_priv.atcs.handle = 0;
978 * amdgpu_acpi_detect - detect ACPI ATIF/ATCS methods
980 * Check if we have the ATIF/ATCS methods and populate
981 * the structures in the driver.
983 void amdgpu_acpi_detect(void)
985 struct amdgpu_atif *atif = &amdgpu_acpi_priv.atif;
986 struct amdgpu_atcs *atcs = &amdgpu_acpi_priv.atcs;
987 struct pci_dev *pdev = NULL;
990 while ((pdev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, pdev)) != NULL) {
992 amdgpu_atif_pci_probe_handle(pdev);
994 amdgpu_atcs_pci_probe_handle(pdev);
997 while ((pdev = pci_get_class(PCI_CLASS_DISPLAY_OTHER << 8, pdev)) != NULL) {
999 amdgpu_atif_pci_probe_handle(pdev);
1001 amdgpu_atcs_pci_probe_handle(pdev);
1004 if (atif->functions.sbios_requests && !atif->functions.system_params) {
1005 /* XXX check this workraround, if sbios request function is
1006 * present we have to see how it's configured in the system
1009 atif->functions.system_params = true;
1012 if (atif->functions.system_params) {
1013 ret = amdgpu_atif_get_notification_params(atif);
1015 DRM_DEBUG_DRIVER("Call to GET_SYSTEM_PARAMS failed: %d\n",
1017 /* Disable notification */
1018 atif->notification_cfg.enabled = false;
1022 if (atif->functions.query_backlight_transfer_characteristics) {
1023 ret = amdgpu_atif_query_backlight_caps(atif);
1025 DRM_DEBUG_DRIVER("Call to QUERY_BACKLIGHT_TRANSFER_CHARACTERISTICS failed: %d\n",
1027 atif->backlight_caps.caps_valid = false;
1030 atif->backlight_caps.caps_valid = false;
1034 #if IS_ENABLED(CONFIG_SUSPEND)
1036 * amdgpu_acpi_is_s3_active
1038 * @adev: amdgpu_device_pointer
1040 * returns true if supported, false if not.
1042 bool amdgpu_acpi_is_s3_active(struct amdgpu_device *adev)
1044 return !(adev->flags & AMD_IS_APU) ||
1045 (pm_suspend_target_state == PM_SUSPEND_MEM);
1049 * amdgpu_acpi_should_gpu_reset
1051 * @adev: amdgpu_device_pointer
1053 * returns true if should reset GPU, false if not
1055 bool amdgpu_acpi_should_gpu_reset(struct amdgpu_device *adev)
1057 if (adev->flags & AMD_IS_APU)
1059 return pm_suspend_target_state != PM_SUSPEND_TO_IDLE;
1063 * amdgpu_acpi_is_s0ix_active
1065 * @adev: amdgpu_device_pointer
1067 * returns true if supported, false if not.
1069 bool amdgpu_acpi_is_s0ix_active(struct amdgpu_device *adev)
1071 if (!(adev->flags & AMD_IS_APU) ||
1072 (pm_suspend_target_state != PM_SUSPEND_TO_IDLE))
1075 if (!(acpi_gbl_FADT.flags & ACPI_FADT_LOW_POWER_S0)) {
1076 dev_warn_once(adev->dev,
1077 "Power consumption will be higher as BIOS has not been configured for suspend-to-idle.\n"
1078 "To use suspend-to-idle change the sleep mode in BIOS setup.\n");
1082 #if !IS_ENABLED(CONFIG_AMD_PMC)
1083 dev_warn_once(adev->dev,
1084 "Power consumption will be higher as the kernel has not been compiled with CONFIG_AMD_PMC.\n");
1088 #endif /* CONFIG_AMD_PMC */
1091 #endif /* CONFIG_SUSPEND */