1 // SPDX-License-Identifier: MIT
3 * Copyright 2012 Advanced Micro Devices, Inc.
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
19 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21 * OTHER DEALINGS IN THE SOFTWARE.
25 #include <linux/pci.h>
26 #include <linux/acpi.h>
27 #include <linux/backlight.h>
28 #include <linux/slab.h>
29 #include <linux/power_supply.h>
30 #include <linux/pm_runtime.h>
31 #include <linux/suspend.h>
32 #include <acpi/video.h>
33 #include <acpi/actbl.h>
36 #include "amdgpu_pm.h"
37 #include "amdgpu_display.h"
41 struct amdgpu_atif_notification_cfg {
46 struct amdgpu_atif_notifications {
48 bool forced_power_state;
49 bool system_power_state;
50 bool brightness_change;
51 bool dgpu_display_event;
52 bool gpu_package_power_limit;
55 struct amdgpu_atif_functions {
58 bool temperature_change;
59 bool query_backlight_transfer_characteristics;
61 bool external_gpu_information;
67 struct amdgpu_atif_notifications notifications;
68 struct amdgpu_atif_functions functions;
69 struct amdgpu_atif_notification_cfg notification_cfg;
70 struct backlight_device *bd;
71 struct amdgpu_dm_backlight_caps backlight_caps;
74 struct amdgpu_atcs_functions {
79 bool power_shift_control;
85 struct amdgpu_atcs_functions functions;
88 static struct amdgpu_acpi_priv {
89 struct amdgpu_atif atif;
90 struct amdgpu_atcs atcs;
93 /* Call the ATIF method
96 * amdgpu_atif_call - call an ATIF method
98 * @atif: atif structure
99 * @function: the ATIF function to execute
100 * @params: ATIF function params
102 * Executes the requested ATIF function (all asics).
103 * Returns a pointer to the acpi output buffer.
105 static union acpi_object *amdgpu_atif_call(struct amdgpu_atif *atif,
107 struct acpi_buffer *params)
110 union acpi_object atif_arg_elements[2];
111 struct acpi_object_list atif_arg;
112 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
115 atif_arg.pointer = &atif_arg_elements[0];
117 atif_arg_elements[0].type = ACPI_TYPE_INTEGER;
118 atif_arg_elements[0].integer.value = function;
121 atif_arg_elements[1].type = ACPI_TYPE_BUFFER;
122 atif_arg_elements[1].buffer.length = params->length;
123 atif_arg_elements[1].buffer.pointer = params->pointer;
125 /* We need a second fake parameter */
126 atif_arg_elements[1].type = ACPI_TYPE_INTEGER;
127 atif_arg_elements[1].integer.value = 0;
130 status = acpi_evaluate_object(atif->handle, NULL, &atif_arg,
133 /* Fail only if calling the method fails and ATIF is supported */
134 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
135 DRM_DEBUG_DRIVER("failed to evaluate ATIF got %s\n",
136 acpi_format_exception(status));
137 kfree(buffer.pointer);
141 return buffer.pointer;
145 * amdgpu_atif_parse_notification - parse supported notifications
147 * @n: supported notifications struct
148 * @mask: supported notifications mask from ATIF
150 * Use the supported notifications mask from ATIF function
151 * ATIF_FUNCTION_VERIFY_INTERFACE to determine what notifications
152 * are supported (all asics).
154 static void amdgpu_atif_parse_notification(struct amdgpu_atif_notifications *n, u32 mask)
156 n->thermal_state = mask & ATIF_THERMAL_STATE_CHANGE_REQUEST_SUPPORTED;
157 n->forced_power_state = mask & ATIF_FORCED_POWER_STATE_CHANGE_REQUEST_SUPPORTED;
158 n->system_power_state = mask & ATIF_SYSTEM_POWER_SOURCE_CHANGE_REQUEST_SUPPORTED;
159 n->brightness_change = mask & ATIF_PANEL_BRIGHTNESS_CHANGE_REQUEST_SUPPORTED;
160 n->dgpu_display_event = mask & ATIF_DGPU_DISPLAY_EVENT_SUPPORTED;
161 n->gpu_package_power_limit = mask & ATIF_GPU_PACKAGE_POWER_LIMIT_REQUEST_SUPPORTED;
165 * amdgpu_atif_parse_functions - parse supported functions
167 * @f: supported functions struct
168 * @mask: supported functions mask from ATIF
170 * Use the supported functions mask from ATIF function
171 * ATIF_FUNCTION_VERIFY_INTERFACE to determine what functions
172 * are supported (all asics).
174 static void amdgpu_atif_parse_functions(struct amdgpu_atif_functions *f, u32 mask)
176 f->system_params = mask & ATIF_GET_SYSTEM_PARAMETERS_SUPPORTED;
177 f->sbios_requests = mask & ATIF_GET_SYSTEM_BIOS_REQUESTS_SUPPORTED;
178 f->temperature_change = mask & ATIF_TEMPERATURE_CHANGE_NOTIFICATION_SUPPORTED;
179 f->query_backlight_transfer_characteristics =
180 mask & ATIF_QUERY_BACKLIGHT_TRANSFER_CHARACTERISTICS_SUPPORTED;
181 f->ready_to_undock = mask & ATIF_READY_TO_UNDOCK_NOTIFICATION_SUPPORTED;
182 f->external_gpu_information = mask & ATIF_GET_EXTERNAL_GPU_INFORMATION_SUPPORTED;
186 * amdgpu_atif_verify_interface - verify ATIF
188 * @atif: amdgpu atif struct
190 * Execute the ATIF_FUNCTION_VERIFY_INTERFACE ATIF function
191 * to initialize ATIF and determine what features are supported
193 * returns 0 on success, error on failure.
195 static int amdgpu_atif_verify_interface(struct amdgpu_atif *atif)
197 union acpi_object *info;
198 struct atif_verify_interface output;
202 info = amdgpu_atif_call(atif, ATIF_FUNCTION_VERIFY_INTERFACE, NULL);
206 memset(&output, 0, sizeof(output));
208 size = *(u16 *) info->buffer.pointer;
210 DRM_INFO("ATIF buffer is too small: %zu\n", size);
214 size = min(sizeof(output), size);
216 memcpy(&output, info->buffer.pointer, size);
218 /* TODO: check version? */
219 DRM_DEBUG_DRIVER("ATIF version %u\n", output.version);
221 amdgpu_atif_parse_notification(&atif->notifications, output.notification_mask);
222 amdgpu_atif_parse_functions(&atif->functions, output.function_bits);
230 * amdgpu_atif_get_notification_params - determine notify configuration
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 amdgpu_atif_get_notification_params(struct amdgpu_atif *atif)
242 union acpi_object *info;
243 struct amdgpu_atif_notification_cfg *n = &atif->notification_cfg;
244 struct atif_system_params params;
248 info = amdgpu_atif_call(atif, ATIF_FUNCTION_GET_SYSTEM_PARAMETERS,
255 size = *(u16 *) info->buffer.pointer;
261 memset(¶ms, 0, sizeof(params));
262 size = min(sizeof(params), size);
263 memcpy(¶ms, info->buffer.pointer, size);
265 DRM_DEBUG_DRIVER("SYSTEM_PARAMS: mask = %#x, flags = %#x\n",
266 params.flags, params.valid_mask);
267 params.flags = params.flags & params.valid_mask;
269 if ((params.flags & ATIF_NOTIFY_MASK) == ATIF_NOTIFY_NONE) {
272 } else if ((params.flags & ATIF_NOTIFY_MASK) == ATIF_NOTIFY_81) {
274 n->command_code = 0x81;
281 n->command_code = params.command_code;
285 DRM_DEBUG_DRIVER("Notification %s, command code = %#x\n",
286 (n->enabled ? "enabled" : "disabled"),
293 * amdgpu_atif_query_backlight_caps - get min and max backlight input signal
297 * Execute the QUERY_BRIGHTNESS_TRANSFER_CHARACTERISTICS ATIF function
298 * to determine the acceptable range of backlight values
300 * Backlight_caps.caps_valid will be set to true if the query is successful
302 * The input signals are in range 0-255
304 * This function assumes the display with backlight is the first LCD
306 * Returns 0 on success, error on failure.
308 static int amdgpu_atif_query_backlight_caps(struct amdgpu_atif *atif)
310 union acpi_object *info;
311 struct atif_qbtc_output characteristics;
312 struct atif_qbtc_arguments arguments;
313 struct acpi_buffer params;
317 arguments.size = sizeof(arguments);
318 arguments.requested_display = ATIF_QBTC_REQUEST_LCD1;
320 params.length = sizeof(arguments);
321 params.pointer = (void *)&arguments;
323 info = amdgpu_atif_call(atif,
324 ATIF_FUNCTION_QUERY_BRIGHTNESS_TRANSFER_CHARACTERISTICS,
331 size = *(u16 *) info->buffer.pointer;
337 memset(&characteristics, 0, sizeof(characteristics));
338 size = min(sizeof(characteristics), size);
339 memcpy(&characteristics, info->buffer.pointer, size);
341 atif->backlight_caps.caps_valid = true;
342 atif->backlight_caps.min_input_signal =
343 characteristics.min_input_signal;
344 atif->backlight_caps.max_input_signal =
345 characteristics.max_input_signal;
352 * amdgpu_atif_get_sbios_requests - get requested sbios event
355 * @req: atif sbios request struct
357 * Execute the ATIF_FUNCTION_GET_SYSTEM_BIOS_REQUESTS ATIF function
358 * to determine what requests the sbios is making to the driver
360 * Returns 0 on success, error on failure.
362 static int amdgpu_atif_get_sbios_requests(struct amdgpu_atif *atif,
363 struct atif_sbios_requests *req)
365 union acpi_object *info;
369 info = amdgpu_atif_call(atif, ATIF_FUNCTION_GET_SYSTEM_BIOS_REQUESTS,
374 size = *(u16 *)info->buffer.pointer;
379 memset(req, 0, sizeof(*req));
381 size = min(sizeof(*req), size);
382 memcpy(req, info->buffer.pointer, size);
383 DRM_DEBUG_DRIVER("SBIOS pending requests: %#x\n", req->pending);
385 count = hweight32(req->pending);
393 * amdgpu_atif_handler - handle ATIF notify requests
395 * @adev: amdgpu_device pointer
396 * @event: atif sbios request struct
398 * Checks the acpi event and if it matches an atif event,
402 * NOTIFY_BAD or NOTIFY_DONE, depending on the event.
404 static int amdgpu_atif_handler(struct amdgpu_device *adev,
405 struct acpi_bus_event *event)
407 struct amdgpu_atif *atif = &amdgpu_acpi_priv.atif;
410 DRM_DEBUG_DRIVER("event, device_class = %s, type = %#x\n",
411 event->device_class, event->type);
413 if (strcmp(event->device_class, ACPI_VIDEO_CLASS) != 0)
416 /* Is this actually our event? */
417 if (!atif->notification_cfg.enabled ||
418 event->type != atif->notification_cfg.command_code) {
419 /* These events will generate keypresses otherwise */
420 if (event->type == ACPI_VIDEO_NOTIFY_PROBE)
426 if (atif->functions.sbios_requests) {
427 struct atif_sbios_requests req;
429 /* Check pending SBIOS requests */
430 count = amdgpu_atif_get_sbios_requests(atif, &req);
435 DRM_DEBUG_DRIVER("ATIF: %d pending SBIOS requests\n", count);
437 if (req.pending & ATIF_PANEL_BRIGHTNESS_CHANGE_REQUEST) {
439 DRM_DEBUG_DRIVER("Changing brightness to %d\n",
440 req.backlight_level);
442 * XXX backlight_device_set_brightness() is
443 * hardwired to post BACKLIGHT_UPDATE_SYSFS.
444 * It probably should accept 'reason' parameter.
446 backlight_device_set_brightness(atif->bd, req.backlight_level);
450 if (req.pending & ATIF_DGPU_DISPLAY_EVENT) {
451 if (adev->flags & AMD_IS_PX) {
452 pm_runtime_get_sync(adev_to_drm(adev)->dev);
453 /* Just fire off a uevent and let userspace tell us what to do */
454 drm_helper_hpd_irq_event(adev_to_drm(adev));
455 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
456 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
459 /* TODO: check other events */
462 /* We've handled the event, stop the notifier chain. The ACPI interface
463 * overloads ACPI_VIDEO_NOTIFY_PROBE, we don't want to send that to
464 * userspace if the event was generated only to signal a SBIOS
470 /* Call the ATCS method
473 * amdgpu_atcs_call - call an ATCS method
475 * @atcs: atcs structure
476 * @function: the ATCS function to execute
477 * @params: ATCS function params
479 * Executes the requested ATCS function (all asics).
480 * Returns a pointer to the acpi output buffer.
482 static union acpi_object *amdgpu_atcs_call(struct amdgpu_atcs *atcs,
484 struct acpi_buffer *params)
487 union acpi_object atcs_arg_elements[2];
488 struct acpi_object_list atcs_arg;
489 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
492 atcs_arg.pointer = &atcs_arg_elements[0];
494 atcs_arg_elements[0].type = ACPI_TYPE_INTEGER;
495 atcs_arg_elements[0].integer.value = function;
498 atcs_arg_elements[1].type = ACPI_TYPE_BUFFER;
499 atcs_arg_elements[1].buffer.length = params->length;
500 atcs_arg_elements[1].buffer.pointer = params->pointer;
502 /* We need a second fake parameter */
503 atcs_arg_elements[1].type = ACPI_TYPE_INTEGER;
504 atcs_arg_elements[1].integer.value = 0;
507 status = acpi_evaluate_object(atcs->handle, NULL, &atcs_arg, &buffer);
509 /* Fail only if calling the method fails and ATIF is supported */
510 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
511 DRM_DEBUG_DRIVER("failed to evaluate ATCS got %s\n",
512 acpi_format_exception(status));
513 kfree(buffer.pointer);
517 return buffer.pointer;
521 * amdgpu_atcs_parse_functions - parse supported functions
523 * @f: supported functions struct
524 * @mask: supported functions mask from ATCS
526 * Use the supported functions mask from ATCS function
527 * ATCS_FUNCTION_VERIFY_INTERFACE to determine what functions
528 * are supported (all asics).
530 static void amdgpu_atcs_parse_functions(struct amdgpu_atcs_functions *f, u32 mask)
532 f->get_ext_state = mask & ATCS_GET_EXTERNAL_STATE_SUPPORTED;
533 f->pcie_perf_req = mask & ATCS_PCIE_PERFORMANCE_REQUEST_SUPPORTED;
534 f->pcie_dev_rdy = mask & ATCS_PCIE_DEVICE_READY_NOTIFICATION_SUPPORTED;
535 f->pcie_bus_width = mask & ATCS_SET_PCIE_BUS_WIDTH_SUPPORTED;
536 f->power_shift_control = mask & ATCS_SET_POWER_SHIFT_CONTROL_SUPPORTED;
540 * amdgpu_atcs_verify_interface - verify ATCS
542 * @atcs: amdgpu atcs struct
544 * Execute the ATCS_FUNCTION_VERIFY_INTERFACE ATCS function
545 * to initialize ATCS and determine what features are supported
547 * returns 0 on success, error on failure.
549 static int amdgpu_atcs_verify_interface(struct amdgpu_atcs *atcs)
551 union acpi_object *info;
552 struct atcs_verify_interface output;
556 info = amdgpu_atcs_call(atcs, ATCS_FUNCTION_VERIFY_INTERFACE, NULL);
560 memset(&output, 0, sizeof(output));
562 size = *(u16 *) info->buffer.pointer;
564 DRM_INFO("ATCS buffer is too small: %zu\n", size);
568 size = min(sizeof(output), size);
570 memcpy(&output, info->buffer.pointer, size);
572 /* TODO: check version? */
573 DRM_DEBUG_DRIVER("ATCS version %u\n", output.version);
575 amdgpu_atcs_parse_functions(&atcs->functions, output.function_bits);
583 * amdgpu_acpi_is_pcie_performance_request_supported
585 * @adev: amdgpu_device pointer
587 * Check if the ATCS pcie_perf_req and pcie_dev_rdy methods
588 * are supported (all asics).
589 * returns true if supported, false if not.
591 bool amdgpu_acpi_is_pcie_performance_request_supported(struct amdgpu_device *adev)
593 struct amdgpu_atcs *atcs = &amdgpu_acpi_priv.atcs;
595 if (atcs->functions.pcie_perf_req && atcs->functions.pcie_dev_rdy)
602 * amdgpu_acpi_is_power_shift_control_supported
604 * Check if the ATCS power shift control method
606 * returns true if supported, false if not.
608 bool amdgpu_acpi_is_power_shift_control_supported(void)
610 return amdgpu_acpi_priv.atcs.functions.power_shift_control;
614 * amdgpu_acpi_pcie_notify_device_ready
616 * @adev: amdgpu_device pointer
618 * Executes the PCIE_DEVICE_READY_NOTIFICATION method
620 * returns 0 on success, error on failure.
622 int amdgpu_acpi_pcie_notify_device_ready(struct amdgpu_device *adev)
624 union acpi_object *info;
625 struct amdgpu_atcs *atcs = &amdgpu_acpi_priv.atcs;
627 if (!atcs->functions.pcie_dev_rdy)
630 info = amdgpu_atcs_call(atcs, ATCS_FUNCTION_PCIE_DEVICE_READY_NOTIFICATION, NULL);
640 * amdgpu_acpi_pcie_performance_request
642 * @adev: amdgpu_device pointer
643 * @perf_req: requested perf level (pcie gen speed)
644 * @advertise: set advertise caps flag if set
646 * Executes the PCIE_PERFORMANCE_REQUEST method to
647 * change the pcie gen speed (all asics).
648 * returns 0 on success, error on failure.
650 int amdgpu_acpi_pcie_performance_request(struct amdgpu_device *adev,
651 u8 perf_req, bool advertise)
653 union acpi_object *info;
654 struct amdgpu_atcs *atcs = &amdgpu_acpi_priv.atcs;
655 struct atcs_pref_req_input atcs_input;
656 struct atcs_pref_req_output atcs_output;
657 struct acpi_buffer params;
661 if (amdgpu_acpi_pcie_notify_device_ready(adev))
664 if (!atcs->functions.pcie_perf_req)
667 atcs_input.size = sizeof(struct atcs_pref_req_input);
668 /* client id (bit 2-0: func num, 7-3: dev num, 15-8: bus num) */
669 atcs_input.client_id = adev->pdev->devfn | (adev->pdev->bus->number << 8);
670 atcs_input.valid_flags_mask = ATCS_VALID_FLAGS_MASK;
671 atcs_input.flags = ATCS_WAIT_FOR_COMPLETION;
673 atcs_input.flags |= ATCS_ADVERTISE_CAPS;
674 atcs_input.req_type = ATCS_PCIE_LINK_SPEED;
675 atcs_input.perf_req = perf_req;
677 params.length = sizeof(struct atcs_pref_req_input);
678 params.pointer = &atcs_input;
681 info = amdgpu_atcs_call(atcs, ATCS_FUNCTION_PCIE_PERFORMANCE_REQUEST, ¶ms);
685 memset(&atcs_output, 0, sizeof(atcs_output));
687 size = *(u16 *) info->buffer.pointer;
689 DRM_INFO("ATCS buffer is too small: %zu\n", size);
693 size = min(sizeof(atcs_output), size);
695 memcpy(&atcs_output, info->buffer.pointer, size);
699 switch (atcs_output.ret_val) {
700 case ATCS_REQUEST_REFUSED:
703 case ATCS_REQUEST_COMPLETE:
705 case ATCS_REQUEST_IN_PROGRESS:
715 * amdgpu_acpi_power_shift_control
717 * @adev: amdgpu_device pointer
718 * @dev_state: device acpi state
719 * @drv_state: driver state
721 * Executes the POWER_SHIFT_CONTROL method to
722 * communicate current dGPU device state and
723 * driver state to APU/SBIOS.
724 * returns 0 on success, error on failure.
726 int amdgpu_acpi_power_shift_control(struct amdgpu_device *adev,
727 u8 dev_state, bool drv_state)
729 union acpi_object *info;
730 struct amdgpu_atcs *atcs = &amdgpu_acpi_priv.atcs;
731 struct atcs_pwr_shift_input atcs_input;
732 struct acpi_buffer params;
734 if (!amdgpu_acpi_is_power_shift_control_supported())
737 atcs_input.size = sizeof(struct atcs_pwr_shift_input);
738 /* dGPU id (bit 2-0: func num, 7-3: dev num, 15-8: bus num) */
739 atcs_input.dgpu_id = adev->pdev->devfn | (adev->pdev->bus->number << 8);
740 atcs_input.dev_acpi_state = dev_state;
741 atcs_input.drv_state = drv_state;
743 params.length = sizeof(struct atcs_pwr_shift_input);
744 params.pointer = &atcs_input;
746 info = amdgpu_atcs_call(atcs, ATCS_FUNCTION_POWER_SHIFT_CONTROL, ¶ms);
748 DRM_ERROR("ATCS PSC update failed\n");
756 * amdgpu_acpi_smart_shift_update - update dGPU device state to SBIOS
758 * @dev: drm_device pointer
759 * @ss_state: current smart shift event
761 * returns 0 on success,
762 * otherwise return error number.
764 int amdgpu_acpi_smart_shift_update(struct drm_device *dev, enum amdgpu_ss ss_state)
766 struct amdgpu_device *adev = drm_to_adev(dev);
769 if (!amdgpu_device_supports_smart_shift(dev))
773 /* SBIOS trigger “stop”, “enable” and “start” at D0, Driver Operational.
774 * SBIOS trigger “stop” at D3, Driver Not Operational.
775 * SBIOS trigger “stop” and “disable” at D0, Driver NOT operational.
777 case AMDGPU_SS_DRV_LOAD:
778 r = amdgpu_acpi_power_shift_control(adev,
779 AMDGPU_ATCS_PSC_DEV_STATE_D0,
780 AMDGPU_ATCS_PSC_DRV_STATE_OPR);
782 case AMDGPU_SS_DEV_D0:
783 r = amdgpu_acpi_power_shift_control(adev,
784 AMDGPU_ATCS_PSC_DEV_STATE_D0,
785 AMDGPU_ATCS_PSC_DRV_STATE_OPR);
787 case AMDGPU_SS_DEV_D3:
788 r = amdgpu_acpi_power_shift_control(adev,
789 AMDGPU_ATCS_PSC_DEV_STATE_D3_HOT,
790 AMDGPU_ATCS_PSC_DRV_STATE_NOT_OPR);
792 case AMDGPU_SS_DRV_UNLOAD:
793 r = amdgpu_acpi_power_shift_control(adev,
794 AMDGPU_ATCS_PSC_DEV_STATE_D0,
795 AMDGPU_ATCS_PSC_DRV_STATE_NOT_OPR);
805 * amdgpu_acpi_event - handle notify events
807 * @nb: notifier block
811 * Calls relevant amdgpu functions in response to various
813 * Returns NOTIFY code
815 static int amdgpu_acpi_event(struct notifier_block *nb,
819 struct amdgpu_device *adev = container_of(nb, struct amdgpu_device, acpi_nb);
820 struct acpi_bus_event *entry = (struct acpi_bus_event *)data;
822 if (strcmp(entry->device_class, ACPI_AC_CLASS) == 0) {
823 if (power_supply_is_system_supplied() > 0)
824 DRM_DEBUG_DRIVER("pm: AC\n");
826 DRM_DEBUG_DRIVER("pm: DC\n");
828 amdgpu_pm_acpi_event_handler(adev);
831 /* Check for pending SBIOS requests */
832 return amdgpu_atif_handler(adev, entry);
835 /* Call all ACPI methods here */
837 * amdgpu_acpi_init - init driver acpi support
839 * @adev: amdgpu_device pointer
841 * Verifies the AMD ACPI interfaces and registers with the acpi
842 * notifier chain (all asics).
843 * Returns 0 on success, error on failure.
845 int amdgpu_acpi_init(struct amdgpu_device *adev)
847 struct amdgpu_atif *atif = &amdgpu_acpi_priv.atif;
849 if (atif->notifications.brightness_change) {
850 if (adev->dc_enabled) {
851 #if defined(CONFIG_DRM_AMD_DC)
852 struct amdgpu_display_manager *dm = &adev->dm;
854 if (dm->backlight_dev[0])
855 atif->bd = dm->backlight_dev[0];
858 struct drm_encoder *tmp;
860 /* Find the encoder controlling the brightness */
861 list_for_each_entry(tmp, &adev_to_drm(adev)->mode_config.encoder_list,
863 struct amdgpu_encoder *enc = to_amdgpu_encoder(tmp);
865 if ((enc->devices & (ATOM_DEVICE_LCD_SUPPORT)) &&
867 struct amdgpu_encoder_atom_dig *dig = enc->enc_priv;
870 atif->bd = dig->bl_dev;
877 adev->acpi_nb.notifier_call = amdgpu_acpi_event;
878 register_acpi_notifier(&adev->acpi_nb);
883 void amdgpu_acpi_get_backlight_caps(struct amdgpu_dm_backlight_caps *caps)
885 struct amdgpu_atif *atif = &amdgpu_acpi_priv.atif;
887 caps->caps_valid = atif->backlight_caps.caps_valid;
888 caps->min_input_signal = atif->backlight_caps.min_input_signal;
889 caps->max_input_signal = atif->backlight_caps.max_input_signal;
893 * amdgpu_acpi_fini - tear down driver acpi support
895 * @adev: amdgpu_device pointer
897 * Unregisters with the acpi notifier chain (all asics).
899 void amdgpu_acpi_fini(struct amdgpu_device *adev)
901 unregister_acpi_notifier(&adev->acpi_nb);
905 * amdgpu_atif_pci_probe_handle - look up the ATIF handle
909 * Look up the ATIF handles (all asics).
910 * Returns true if the handle is found, false if not.
912 static bool amdgpu_atif_pci_probe_handle(struct pci_dev *pdev)
914 char acpi_method_name[255] = { 0 };
915 struct acpi_buffer buffer = {sizeof(acpi_method_name), acpi_method_name};
916 acpi_handle dhandle, atif_handle;
920 dhandle = ACPI_HANDLE(&pdev->dev);
924 status = acpi_get_handle(dhandle, "ATIF", &atif_handle);
925 if (ACPI_FAILURE(status))
928 amdgpu_acpi_priv.atif.handle = atif_handle;
929 acpi_get_name(amdgpu_acpi_priv.atif.handle, ACPI_FULL_PATHNAME, &buffer);
930 DRM_DEBUG_DRIVER("Found ATIF handle %s\n", acpi_method_name);
931 ret = amdgpu_atif_verify_interface(&amdgpu_acpi_priv.atif);
933 amdgpu_acpi_priv.atif.handle = 0;
940 * amdgpu_atcs_pci_probe_handle - look up the ATCS handle
944 * Look up the ATCS handles (all asics).
945 * Returns true if the handle is found, false if not.
947 static bool amdgpu_atcs_pci_probe_handle(struct pci_dev *pdev)
949 char acpi_method_name[255] = { 0 };
950 struct acpi_buffer buffer = { sizeof(acpi_method_name), acpi_method_name };
951 acpi_handle dhandle, atcs_handle;
955 dhandle = ACPI_HANDLE(&pdev->dev);
959 status = acpi_get_handle(dhandle, "ATCS", &atcs_handle);
960 if (ACPI_FAILURE(status))
963 amdgpu_acpi_priv.atcs.handle = atcs_handle;
964 acpi_get_name(amdgpu_acpi_priv.atcs.handle, ACPI_FULL_PATHNAME, &buffer);
965 DRM_DEBUG_DRIVER("Found ATCS handle %s\n", acpi_method_name);
966 ret = amdgpu_atcs_verify_interface(&amdgpu_acpi_priv.atcs);
968 amdgpu_acpi_priv.atcs.handle = 0;
976 * amdgpu_acpi_should_gpu_reset
978 * @adev: amdgpu_device_pointer
980 * returns true if should reset GPU, false if not
982 bool amdgpu_acpi_should_gpu_reset(struct amdgpu_device *adev)
984 if ((adev->flags & AMD_IS_APU) &&
985 adev->gfx.imu.funcs) /* Not need to do mode2 reset for IMU enabled APUs */
988 if ((adev->flags & AMD_IS_APU) &&
989 amdgpu_acpi_is_s3_active(adev))
992 if (amdgpu_sriov_vf(adev))
995 #if IS_ENABLED(CONFIG_SUSPEND)
996 return pm_suspend_target_state != PM_SUSPEND_TO_IDLE;
1003 * amdgpu_acpi_detect - detect ACPI ATIF/ATCS methods
1005 * Check if we have the ATIF/ATCS methods and populate
1006 * the structures in the driver.
1008 void amdgpu_acpi_detect(void)
1010 struct amdgpu_atif *atif = &amdgpu_acpi_priv.atif;
1011 struct amdgpu_atcs *atcs = &amdgpu_acpi_priv.atcs;
1012 struct pci_dev *pdev = NULL;
1015 while ((pdev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, pdev)) != NULL) {
1017 amdgpu_atif_pci_probe_handle(pdev);
1019 amdgpu_atcs_pci_probe_handle(pdev);
1022 while ((pdev = pci_get_class(PCI_CLASS_DISPLAY_OTHER << 8, pdev)) != NULL) {
1024 amdgpu_atif_pci_probe_handle(pdev);
1026 amdgpu_atcs_pci_probe_handle(pdev);
1029 if (atif->functions.sbios_requests && !atif->functions.system_params) {
1030 /* XXX check this workraround, if sbios request function is
1031 * present we have to see how it's configured in the system
1034 atif->functions.system_params = true;
1037 if (atif->functions.system_params) {
1038 ret = amdgpu_atif_get_notification_params(atif);
1040 DRM_DEBUG_DRIVER("Call to GET_SYSTEM_PARAMS failed: %d\n",
1042 /* Disable notification */
1043 atif->notification_cfg.enabled = false;
1047 if (atif->functions.query_backlight_transfer_characteristics) {
1048 ret = amdgpu_atif_query_backlight_caps(atif);
1050 DRM_DEBUG_DRIVER("Call to QUERY_BACKLIGHT_TRANSFER_CHARACTERISTICS failed: %d\n",
1052 atif->backlight_caps.caps_valid = false;
1055 atif->backlight_caps.caps_valid = false;
1059 #if IS_ENABLED(CONFIG_SUSPEND)
1061 * amdgpu_acpi_is_s3_active
1063 * @adev: amdgpu_device_pointer
1065 * returns true if supported, false if not.
1067 bool amdgpu_acpi_is_s3_active(struct amdgpu_device *adev)
1069 return !(adev->flags & AMD_IS_APU) ||
1070 (pm_suspend_target_state == PM_SUSPEND_MEM);
1074 * amdgpu_acpi_is_s0ix_active
1076 * @adev: amdgpu_device_pointer
1078 * returns true if supported, false if not.
1080 bool amdgpu_acpi_is_s0ix_active(struct amdgpu_device *adev)
1082 if (!(adev->flags & AMD_IS_APU) ||
1083 (pm_suspend_target_state != PM_SUSPEND_TO_IDLE))
1086 if (adev->asic_type < CHIP_RAVEN)
1090 * If ACPI_FADT_LOW_POWER_S0 is not set in the FADT, it is generally
1091 * risky to do any special firmware-related preparations for entering
1092 * S0ix even though the system is suspending to idle, so return false
1095 if (!(acpi_gbl_FADT.flags & ACPI_FADT_LOW_POWER_S0))
1096 dev_warn_once(adev->dev,
1097 "Power consumption will be higher as BIOS has not been configured for suspend-to-idle.\n"
1098 "To use suspend-to-idle change the sleep mode in BIOS setup.\n");
1100 #if !IS_ENABLED(CONFIG_AMD_PMC)
1101 dev_warn_once(adev->dev,
1102 "Power consumption will be higher as the kernel has not been compiled with CONFIG_AMD_PMC.\n");
1103 #endif /* CONFIG_AMD_PMC */
1107 #endif /* CONFIG_SUSPEND */