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 /* Declare GUID for AMD _DSM method for XCCs */
42 static const guid_t amd_xcc_dsm_guid = GUID_INIT(0x8267f5d5, 0xa556, 0x44f2,
43 0xb8, 0xb4, 0x45, 0x56, 0x2e,
46 #define AMD_XCC_HID_START 3000
47 #define AMD_XCC_DSM_GET_NUM_FUNCS 0
48 #define AMD_XCC_DSM_GET_SUPP_MODE 1
49 #define AMD_XCC_DSM_GET_XCP_MODE 2
50 #define AMD_XCC_DSM_GET_VF_XCC_MAPPING 4
51 #define AMD_XCC_DSM_GET_TMR_INFO 5
52 #define AMD_XCC_DSM_NUM_FUNCS 5
54 #define AMD_XCC_MAX_HID 24
56 /* Encapsulates the XCD acpi object information */
57 struct amdgpu_acpi_xcc_info {
58 struct list_head list;
65 struct amdgpu_acpi_dev_info {
66 struct list_head list;
67 struct list_head xcc_list;
69 uint16_t supp_xcp_mode;
76 struct list_head amdgpu_acpi_dev_list;
78 struct amdgpu_atif_notification_cfg {
83 struct amdgpu_atif_notifications {
85 bool forced_power_state;
86 bool system_power_state;
87 bool brightness_change;
88 bool dgpu_display_event;
89 bool gpu_package_power_limit;
92 struct amdgpu_atif_functions {
95 bool temperature_change;
96 bool query_backlight_transfer_characteristics;
98 bool external_gpu_information;
104 struct amdgpu_atif_notifications notifications;
105 struct amdgpu_atif_functions functions;
106 struct amdgpu_atif_notification_cfg notification_cfg;
107 struct backlight_device *bd;
108 struct amdgpu_dm_backlight_caps backlight_caps;
111 struct amdgpu_atcs_functions {
116 bool power_shift_control;
122 struct amdgpu_atcs_functions functions;
125 static struct amdgpu_acpi_priv {
126 struct amdgpu_atif atif;
127 struct amdgpu_atcs atcs;
130 /* Call the ATIF method
133 * amdgpu_atif_call - call an ATIF method
135 * @atif: atif structure
136 * @function: the ATIF function to execute
137 * @params: ATIF function params
139 * Executes the requested ATIF function (all asics).
140 * Returns a pointer to the acpi output buffer.
142 static union acpi_object *amdgpu_atif_call(struct amdgpu_atif *atif,
144 struct acpi_buffer *params)
147 union acpi_object atif_arg_elements[2];
148 struct acpi_object_list atif_arg;
149 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
152 atif_arg.pointer = &atif_arg_elements[0];
154 atif_arg_elements[0].type = ACPI_TYPE_INTEGER;
155 atif_arg_elements[0].integer.value = function;
158 atif_arg_elements[1].type = ACPI_TYPE_BUFFER;
159 atif_arg_elements[1].buffer.length = params->length;
160 atif_arg_elements[1].buffer.pointer = params->pointer;
162 /* We need a second fake parameter */
163 atif_arg_elements[1].type = ACPI_TYPE_INTEGER;
164 atif_arg_elements[1].integer.value = 0;
167 status = acpi_evaluate_object(atif->handle, NULL, &atif_arg,
170 /* Fail only if calling the method fails and ATIF is supported */
171 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
172 DRM_DEBUG_DRIVER("failed to evaluate ATIF got %s\n",
173 acpi_format_exception(status));
174 kfree(buffer.pointer);
178 return buffer.pointer;
182 * amdgpu_atif_parse_notification - parse supported notifications
184 * @n: supported notifications struct
185 * @mask: supported notifications mask from ATIF
187 * Use the supported notifications mask from ATIF function
188 * ATIF_FUNCTION_VERIFY_INTERFACE to determine what notifications
189 * are supported (all asics).
191 static void amdgpu_atif_parse_notification(struct amdgpu_atif_notifications *n, u32 mask)
193 n->thermal_state = mask & ATIF_THERMAL_STATE_CHANGE_REQUEST_SUPPORTED;
194 n->forced_power_state = mask & ATIF_FORCED_POWER_STATE_CHANGE_REQUEST_SUPPORTED;
195 n->system_power_state = mask & ATIF_SYSTEM_POWER_SOURCE_CHANGE_REQUEST_SUPPORTED;
196 n->brightness_change = mask & ATIF_PANEL_BRIGHTNESS_CHANGE_REQUEST_SUPPORTED;
197 n->dgpu_display_event = mask & ATIF_DGPU_DISPLAY_EVENT_SUPPORTED;
198 n->gpu_package_power_limit = mask & ATIF_GPU_PACKAGE_POWER_LIMIT_REQUEST_SUPPORTED;
202 * amdgpu_atif_parse_functions - parse supported functions
204 * @f: supported functions struct
205 * @mask: supported functions mask from ATIF
207 * Use the supported functions mask from ATIF function
208 * ATIF_FUNCTION_VERIFY_INTERFACE to determine what functions
209 * are supported (all asics).
211 static void amdgpu_atif_parse_functions(struct amdgpu_atif_functions *f, u32 mask)
213 f->system_params = mask & ATIF_GET_SYSTEM_PARAMETERS_SUPPORTED;
214 f->sbios_requests = mask & ATIF_GET_SYSTEM_BIOS_REQUESTS_SUPPORTED;
215 f->temperature_change = mask & ATIF_TEMPERATURE_CHANGE_NOTIFICATION_SUPPORTED;
216 f->query_backlight_transfer_characteristics =
217 mask & ATIF_QUERY_BACKLIGHT_TRANSFER_CHARACTERISTICS_SUPPORTED;
218 f->ready_to_undock = mask & ATIF_READY_TO_UNDOCK_NOTIFICATION_SUPPORTED;
219 f->external_gpu_information = mask & ATIF_GET_EXTERNAL_GPU_INFORMATION_SUPPORTED;
223 * amdgpu_atif_verify_interface - verify ATIF
225 * @atif: amdgpu atif struct
227 * Execute the ATIF_FUNCTION_VERIFY_INTERFACE ATIF function
228 * to initialize ATIF and determine what features are supported
230 * returns 0 on success, error on failure.
232 static int amdgpu_atif_verify_interface(struct amdgpu_atif *atif)
234 union acpi_object *info;
235 struct atif_verify_interface output;
239 info = amdgpu_atif_call(atif, ATIF_FUNCTION_VERIFY_INTERFACE, NULL);
243 memset(&output, 0, sizeof(output));
245 size = *(u16 *) info->buffer.pointer;
247 DRM_INFO("ATIF buffer is too small: %zu\n", size);
251 size = min(sizeof(output), size);
253 memcpy(&output, info->buffer.pointer, size);
255 /* TODO: check version? */
256 DRM_DEBUG_DRIVER("ATIF version %u\n", output.version);
258 amdgpu_atif_parse_notification(&atif->notifications, output.notification_mask);
259 amdgpu_atif_parse_functions(&atif->functions, output.function_bits);
267 * amdgpu_atif_get_notification_params - determine notify configuration
271 * Execute the ATIF_FUNCTION_GET_SYSTEM_PARAMETERS ATIF function
272 * to determine if a notifier is used and if so which one
273 * (all asics). This is either Notify(VGA, 0x81) or Notify(VGA, n)
274 * where n is specified in the result if a notifier is used.
275 * Returns 0 on success, error on failure.
277 static int amdgpu_atif_get_notification_params(struct amdgpu_atif *atif)
279 union acpi_object *info;
280 struct amdgpu_atif_notification_cfg *n = &atif->notification_cfg;
281 struct atif_system_params params;
285 info = amdgpu_atif_call(atif, ATIF_FUNCTION_GET_SYSTEM_PARAMETERS,
292 size = *(u16 *) info->buffer.pointer;
298 memset(¶ms, 0, sizeof(params));
299 size = min(sizeof(params), size);
300 memcpy(¶ms, info->buffer.pointer, size);
302 DRM_DEBUG_DRIVER("SYSTEM_PARAMS: mask = %#x, flags = %#x\n",
303 params.flags, params.valid_mask);
304 params.flags = params.flags & params.valid_mask;
306 if ((params.flags & ATIF_NOTIFY_MASK) == ATIF_NOTIFY_NONE) {
309 } else if ((params.flags & ATIF_NOTIFY_MASK) == ATIF_NOTIFY_81) {
311 n->command_code = 0x81;
318 n->command_code = params.command_code;
322 DRM_DEBUG_DRIVER("Notification %s, command code = %#x\n",
323 (n->enabled ? "enabled" : "disabled"),
330 * amdgpu_atif_query_backlight_caps - get min and max backlight input signal
334 * Execute the QUERY_BRIGHTNESS_TRANSFER_CHARACTERISTICS ATIF function
335 * to determine the acceptable range of backlight values
337 * Backlight_caps.caps_valid will be set to true if the query is successful
339 * The input signals are in range 0-255
341 * This function assumes the display with backlight is the first LCD
343 * Returns 0 on success, error on failure.
345 static int amdgpu_atif_query_backlight_caps(struct amdgpu_atif *atif)
347 union acpi_object *info;
348 struct atif_qbtc_output characteristics;
349 struct atif_qbtc_arguments arguments;
350 struct acpi_buffer params;
354 arguments.size = sizeof(arguments);
355 arguments.requested_display = ATIF_QBTC_REQUEST_LCD1;
357 params.length = sizeof(arguments);
358 params.pointer = (void *)&arguments;
360 info = amdgpu_atif_call(atif,
361 ATIF_FUNCTION_QUERY_BRIGHTNESS_TRANSFER_CHARACTERISTICS,
368 size = *(u16 *) info->buffer.pointer;
374 memset(&characteristics, 0, sizeof(characteristics));
375 size = min(sizeof(characteristics), size);
376 memcpy(&characteristics, info->buffer.pointer, size);
378 atif->backlight_caps.caps_valid = true;
379 atif->backlight_caps.min_input_signal =
380 characteristics.min_input_signal;
381 atif->backlight_caps.max_input_signal =
382 characteristics.max_input_signal;
389 * amdgpu_atif_get_sbios_requests - get requested sbios event
392 * @req: atif sbios request struct
394 * Execute the ATIF_FUNCTION_GET_SYSTEM_BIOS_REQUESTS ATIF function
395 * to determine what requests the sbios is making to the driver
397 * Returns 0 on success, error on failure.
399 static int amdgpu_atif_get_sbios_requests(struct amdgpu_atif *atif,
400 struct atif_sbios_requests *req)
402 union acpi_object *info;
406 info = amdgpu_atif_call(atif, ATIF_FUNCTION_GET_SYSTEM_BIOS_REQUESTS,
411 size = *(u16 *)info->buffer.pointer;
416 memset(req, 0, sizeof(*req));
418 size = min(sizeof(*req), size);
419 memcpy(req, info->buffer.pointer, size);
420 DRM_DEBUG_DRIVER("SBIOS pending requests: %#x\n", req->pending);
422 count = hweight32(req->pending);
430 * amdgpu_atif_handler - handle ATIF notify requests
432 * @adev: amdgpu_device pointer
433 * @event: atif sbios request struct
435 * Checks the acpi event and if it matches an atif event,
439 * NOTIFY_BAD or NOTIFY_DONE, depending on the event.
441 static int amdgpu_atif_handler(struct amdgpu_device *adev,
442 struct acpi_bus_event *event)
444 struct amdgpu_atif *atif = &amdgpu_acpi_priv.atif;
447 DRM_DEBUG_DRIVER("event, device_class = %s, type = %#x\n",
448 event->device_class, event->type);
450 if (strcmp(event->device_class, ACPI_VIDEO_CLASS) != 0)
453 /* Is this actually our event? */
454 if (!atif->notification_cfg.enabled ||
455 event->type != atif->notification_cfg.command_code) {
456 /* These events will generate keypresses otherwise */
457 if (event->type == ACPI_VIDEO_NOTIFY_PROBE)
463 if (atif->functions.sbios_requests) {
464 struct atif_sbios_requests req;
466 /* Check pending SBIOS requests */
467 count = amdgpu_atif_get_sbios_requests(atif, &req);
472 DRM_DEBUG_DRIVER("ATIF: %d pending SBIOS requests\n", count);
474 if (req.pending & ATIF_PANEL_BRIGHTNESS_CHANGE_REQUEST) {
476 DRM_DEBUG_DRIVER("Changing brightness to %d\n",
477 req.backlight_level);
479 * XXX backlight_device_set_brightness() is
480 * hardwired to post BACKLIGHT_UPDATE_SYSFS.
481 * It probably should accept 'reason' parameter.
483 backlight_device_set_brightness(atif->bd, req.backlight_level);
487 if (req.pending & ATIF_DGPU_DISPLAY_EVENT) {
488 if (adev->flags & AMD_IS_PX) {
489 pm_runtime_get_sync(adev_to_drm(adev)->dev);
490 /* Just fire off a uevent and let userspace tell us what to do */
491 drm_helper_hpd_irq_event(adev_to_drm(adev));
492 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
493 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
496 /* TODO: check other events */
499 /* We've handled the event, stop the notifier chain. The ACPI interface
500 * overloads ACPI_VIDEO_NOTIFY_PROBE, we don't want to send that to
501 * userspace if the event was generated only to signal a SBIOS
507 /* Call the ATCS method
510 * amdgpu_atcs_call - call an ATCS method
512 * @atcs: atcs structure
513 * @function: the ATCS function to execute
514 * @params: ATCS function params
516 * Executes the requested ATCS function (all asics).
517 * Returns a pointer to the acpi output buffer.
519 static union acpi_object *amdgpu_atcs_call(struct amdgpu_atcs *atcs,
521 struct acpi_buffer *params)
524 union acpi_object atcs_arg_elements[2];
525 struct acpi_object_list atcs_arg;
526 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
529 atcs_arg.pointer = &atcs_arg_elements[0];
531 atcs_arg_elements[0].type = ACPI_TYPE_INTEGER;
532 atcs_arg_elements[0].integer.value = function;
535 atcs_arg_elements[1].type = ACPI_TYPE_BUFFER;
536 atcs_arg_elements[1].buffer.length = params->length;
537 atcs_arg_elements[1].buffer.pointer = params->pointer;
539 /* We need a second fake parameter */
540 atcs_arg_elements[1].type = ACPI_TYPE_INTEGER;
541 atcs_arg_elements[1].integer.value = 0;
544 status = acpi_evaluate_object(atcs->handle, NULL, &atcs_arg, &buffer);
546 /* Fail only if calling the method fails and ATIF is supported */
547 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
548 DRM_DEBUG_DRIVER("failed to evaluate ATCS got %s\n",
549 acpi_format_exception(status));
550 kfree(buffer.pointer);
554 return buffer.pointer;
558 * amdgpu_atcs_parse_functions - parse supported functions
560 * @f: supported functions struct
561 * @mask: supported functions mask from ATCS
563 * Use the supported functions mask from ATCS function
564 * ATCS_FUNCTION_VERIFY_INTERFACE to determine what functions
565 * are supported (all asics).
567 static void amdgpu_atcs_parse_functions(struct amdgpu_atcs_functions *f, u32 mask)
569 f->get_ext_state = mask & ATCS_GET_EXTERNAL_STATE_SUPPORTED;
570 f->pcie_perf_req = mask & ATCS_PCIE_PERFORMANCE_REQUEST_SUPPORTED;
571 f->pcie_dev_rdy = mask & ATCS_PCIE_DEVICE_READY_NOTIFICATION_SUPPORTED;
572 f->pcie_bus_width = mask & ATCS_SET_PCIE_BUS_WIDTH_SUPPORTED;
573 f->power_shift_control = mask & ATCS_SET_POWER_SHIFT_CONTROL_SUPPORTED;
577 * amdgpu_atcs_verify_interface - verify ATCS
579 * @atcs: amdgpu atcs struct
581 * Execute the ATCS_FUNCTION_VERIFY_INTERFACE ATCS function
582 * to initialize ATCS and determine what features are supported
584 * returns 0 on success, error on failure.
586 static int amdgpu_atcs_verify_interface(struct amdgpu_atcs *atcs)
588 union acpi_object *info;
589 struct atcs_verify_interface output;
593 info = amdgpu_atcs_call(atcs, ATCS_FUNCTION_VERIFY_INTERFACE, NULL);
597 memset(&output, 0, sizeof(output));
599 size = *(u16 *) info->buffer.pointer;
601 DRM_INFO("ATCS buffer is too small: %zu\n", size);
605 size = min(sizeof(output), size);
607 memcpy(&output, info->buffer.pointer, size);
609 /* TODO: check version? */
610 DRM_DEBUG_DRIVER("ATCS version %u\n", output.version);
612 amdgpu_atcs_parse_functions(&atcs->functions, output.function_bits);
620 * amdgpu_acpi_is_pcie_performance_request_supported
622 * @adev: amdgpu_device pointer
624 * Check if the ATCS pcie_perf_req and pcie_dev_rdy methods
625 * are supported (all asics).
626 * returns true if supported, false if not.
628 bool amdgpu_acpi_is_pcie_performance_request_supported(struct amdgpu_device *adev)
630 struct amdgpu_atcs *atcs = &amdgpu_acpi_priv.atcs;
632 if (atcs->functions.pcie_perf_req && atcs->functions.pcie_dev_rdy)
639 * amdgpu_acpi_is_power_shift_control_supported
641 * Check if the ATCS power shift control method
643 * returns true if supported, false if not.
645 bool amdgpu_acpi_is_power_shift_control_supported(void)
647 return amdgpu_acpi_priv.atcs.functions.power_shift_control;
651 * amdgpu_acpi_pcie_notify_device_ready
653 * @adev: amdgpu_device pointer
655 * Executes the PCIE_DEVICE_READY_NOTIFICATION method
657 * returns 0 on success, error on failure.
659 int amdgpu_acpi_pcie_notify_device_ready(struct amdgpu_device *adev)
661 union acpi_object *info;
662 struct amdgpu_atcs *atcs = &amdgpu_acpi_priv.atcs;
664 if (!atcs->functions.pcie_dev_rdy)
667 info = amdgpu_atcs_call(atcs, ATCS_FUNCTION_PCIE_DEVICE_READY_NOTIFICATION, NULL);
677 * amdgpu_acpi_pcie_performance_request
679 * @adev: amdgpu_device pointer
680 * @perf_req: requested perf level (pcie gen speed)
681 * @advertise: set advertise caps flag if set
683 * Executes the PCIE_PERFORMANCE_REQUEST method to
684 * change the pcie gen speed (all asics).
685 * returns 0 on success, error on failure.
687 int amdgpu_acpi_pcie_performance_request(struct amdgpu_device *adev,
688 u8 perf_req, bool advertise)
690 union acpi_object *info;
691 struct amdgpu_atcs *atcs = &amdgpu_acpi_priv.atcs;
692 struct atcs_pref_req_input atcs_input;
693 struct atcs_pref_req_output atcs_output;
694 struct acpi_buffer params;
698 if (amdgpu_acpi_pcie_notify_device_ready(adev))
701 if (!atcs->functions.pcie_perf_req)
704 atcs_input.size = sizeof(struct atcs_pref_req_input);
705 /* client id (bit 2-0: func num, 7-3: dev num, 15-8: bus num) */
706 atcs_input.client_id = adev->pdev->devfn | (adev->pdev->bus->number << 8);
707 atcs_input.valid_flags_mask = ATCS_VALID_FLAGS_MASK;
708 atcs_input.flags = ATCS_WAIT_FOR_COMPLETION;
710 atcs_input.flags |= ATCS_ADVERTISE_CAPS;
711 atcs_input.req_type = ATCS_PCIE_LINK_SPEED;
712 atcs_input.perf_req = perf_req;
714 params.length = sizeof(struct atcs_pref_req_input);
715 params.pointer = &atcs_input;
718 info = amdgpu_atcs_call(atcs, ATCS_FUNCTION_PCIE_PERFORMANCE_REQUEST, ¶ms);
722 memset(&atcs_output, 0, sizeof(atcs_output));
724 size = *(u16 *) info->buffer.pointer;
726 DRM_INFO("ATCS buffer is too small: %zu\n", size);
730 size = min(sizeof(atcs_output), size);
732 memcpy(&atcs_output, info->buffer.pointer, size);
736 switch (atcs_output.ret_val) {
737 case ATCS_REQUEST_REFUSED:
740 case ATCS_REQUEST_COMPLETE:
742 case ATCS_REQUEST_IN_PROGRESS:
752 * amdgpu_acpi_power_shift_control
754 * @adev: amdgpu_device pointer
755 * @dev_state: device acpi state
756 * @drv_state: driver state
758 * Executes the POWER_SHIFT_CONTROL method to
759 * communicate current dGPU device state and
760 * driver state to APU/SBIOS.
761 * returns 0 on success, error on failure.
763 int amdgpu_acpi_power_shift_control(struct amdgpu_device *adev,
764 u8 dev_state, bool drv_state)
766 union acpi_object *info;
767 struct amdgpu_atcs *atcs = &amdgpu_acpi_priv.atcs;
768 struct atcs_pwr_shift_input atcs_input;
769 struct acpi_buffer params;
771 if (!amdgpu_acpi_is_power_shift_control_supported())
774 atcs_input.size = sizeof(struct atcs_pwr_shift_input);
775 /* dGPU id (bit 2-0: func num, 7-3: dev num, 15-8: bus num) */
776 atcs_input.dgpu_id = adev->pdev->devfn | (adev->pdev->bus->number << 8);
777 atcs_input.dev_acpi_state = dev_state;
778 atcs_input.drv_state = drv_state;
780 params.length = sizeof(struct atcs_pwr_shift_input);
781 params.pointer = &atcs_input;
783 info = amdgpu_atcs_call(atcs, ATCS_FUNCTION_POWER_SHIFT_CONTROL, ¶ms);
785 DRM_ERROR("ATCS PSC update failed\n");
793 * amdgpu_acpi_smart_shift_update - update dGPU device state to SBIOS
795 * @dev: drm_device pointer
796 * @ss_state: current smart shift event
798 * returns 0 on success,
799 * otherwise return error number.
801 int amdgpu_acpi_smart_shift_update(struct drm_device *dev, enum amdgpu_ss ss_state)
803 struct amdgpu_device *adev = drm_to_adev(dev);
806 if (!amdgpu_device_supports_smart_shift(dev))
810 /* SBIOS trigger “stop”, “enable” and “start” at D0, Driver Operational.
811 * SBIOS trigger “stop” at D3, Driver Not Operational.
812 * SBIOS trigger “stop” and “disable” at D0, Driver NOT operational.
814 case AMDGPU_SS_DRV_LOAD:
815 r = amdgpu_acpi_power_shift_control(adev,
816 AMDGPU_ATCS_PSC_DEV_STATE_D0,
817 AMDGPU_ATCS_PSC_DRV_STATE_OPR);
819 case AMDGPU_SS_DEV_D0:
820 r = amdgpu_acpi_power_shift_control(adev,
821 AMDGPU_ATCS_PSC_DEV_STATE_D0,
822 AMDGPU_ATCS_PSC_DRV_STATE_OPR);
824 case AMDGPU_SS_DEV_D3:
825 r = amdgpu_acpi_power_shift_control(adev,
826 AMDGPU_ATCS_PSC_DEV_STATE_D3_HOT,
827 AMDGPU_ATCS_PSC_DRV_STATE_NOT_OPR);
829 case AMDGPU_SS_DRV_UNLOAD:
830 r = amdgpu_acpi_power_shift_control(adev,
831 AMDGPU_ATCS_PSC_DEV_STATE_D0,
832 AMDGPU_ATCS_PSC_DRV_STATE_NOT_OPR);
842 * amdgpu_acpi_get_node_id - obtain the NUMA node id for corresponding amdgpu
845 * @handle: acpi handle
846 * @nid: NUMA Node id returned by the platform firmware
848 * Queries the ACPI interface to fetch the corresponding NUMA Node ID for a
849 * given amdgpu acpi device.
851 * Returns ACPI STATUS OK with Node ID on success or the corresponding failure reason
853 acpi_status amdgpu_acpi_get_node_id(acpi_handle handle, int *nid)
855 #ifdef CONFIG_ACPI_NUMA
859 status = acpi_evaluate_integer(handle, "_PXM", NULL, &pxm);
861 if (ACPI_FAILURE(status))
864 *nid = pxm_to_node(pxm);
866 return_ACPI_STATUS(AE_OK);
868 return_ACPI_STATUS(AE_NOT_EXIST);
872 struct amdgpu_acpi_dev_info *amdgpu_acpi_get_dev(u16 bdf)
874 struct amdgpu_acpi_dev_info *acpi_dev;
876 if (list_empty(&amdgpu_acpi_dev_list))
879 list_for_each_entry(acpi_dev, &amdgpu_acpi_dev_list, list)
880 if (acpi_dev->bdf == bdf)
886 static int amdgpu_acpi_dev_init(struct amdgpu_acpi_dev_info **dev_info,
887 struct amdgpu_acpi_xcc_info *xcc_info, u16 bdf)
889 struct amdgpu_acpi_dev_info *tmp;
890 union acpi_object *obj;
894 tmp = kzalloc(sizeof(struct amdgpu_acpi_dev_info), GFP_KERNEL);
898 INIT_LIST_HEAD(&tmp->xcc_list);
899 INIT_LIST_HEAD(&tmp->list);
902 obj = acpi_evaluate_dsm_typed(xcc_info->handle, &amd_xcc_dsm_guid, 0,
903 AMD_XCC_DSM_GET_SUPP_MODE, NULL,
907 acpi_handle_debug(xcc_info->handle,
908 "_DSM function %d evaluation failed",
909 AMD_XCC_DSM_GET_SUPP_MODE);
914 tmp->supp_xcp_mode = obj->integer.value & 0xFFFF;
917 obj = acpi_evaluate_dsm_typed(xcc_info->handle, &amd_xcc_dsm_guid, 0,
918 AMD_XCC_DSM_GET_XCP_MODE, NULL,
922 acpi_handle_debug(xcc_info->handle,
923 "_DSM function %d evaluation failed",
924 AMD_XCC_DSM_GET_XCP_MODE);
929 tmp->xcp_mode = obj->integer.value & 0xFFFF;
930 tmp->mem_mode = (obj->integer.value >> 32) & 0xFFFF;
933 /* Evaluate DSMs and fill XCC information */
934 obj = acpi_evaluate_dsm_typed(xcc_info->handle, &amd_xcc_dsm_guid, 0,
935 AMD_XCC_DSM_GET_TMR_INFO, NULL,
938 if (!obj || obj->package.count < 2) {
939 acpi_handle_debug(xcc_info->handle,
940 "_DSM function %d evaluation failed",
941 AMD_XCC_DSM_GET_TMR_INFO);
946 tmp->tmr_base = obj->package.elements[0].integer.value;
947 tmp->tmr_size = obj->package.elements[1].integer.value;
951 "New dev(%x): Supported xcp mode: %x curr xcp_mode : %x mem mode : %x, tmr base: %llx tmr size: %llx ",
952 tmp->bdf, tmp->supp_xcp_mode, tmp->xcp_mode, tmp->mem_mode,
953 tmp->tmr_base, tmp->tmr_size);
954 list_add_tail(&tmp->list, &amdgpu_acpi_dev_list);
967 static int amdgpu_acpi_get_xcc_info(struct amdgpu_acpi_xcc_info *xcc_info,
970 union acpi_object *obj;
974 obj = acpi_evaluate_dsm_typed(xcc_info->handle, &amd_xcc_dsm_guid, 0,
975 AMD_XCC_DSM_GET_NUM_FUNCS, NULL,
978 if (!obj || obj->integer.value != AMD_XCC_DSM_NUM_FUNCS)
982 /* Evaluate DSMs and fill XCC information */
983 obj = acpi_evaluate_dsm_typed(xcc_info->handle, &amd_xcc_dsm_guid, 0,
984 AMD_XCC_DSM_GET_VF_XCC_MAPPING, NULL,
988 acpi_handle_debug(xcc_info->handle,
989 "_DSM function %d evaluation failed",
990 AMD_XCC_DSM_GET_VF_XCC_MAPPING);
995 /* PF xcc id [39:32] */
996 xcc_info->phy_id = (obj->integer.value >> 32) & 0xFF;
997 /* xcp node of this xcc [47:40] */
998 xcc_info->xcp_node = (obj->integer.value >> 40) & 0xFF;
999 /* PF bus/dev/fn of this xcc [63:48] */
1000 *bdf = (obj->integer.value >> 48) & 0xFFFF;
1004 status = amdgpu_acpi_get_node_id(xcc_info->handle, &xcc_info->mem_node);
1006 /* TODO: check if this check is required */
1007 if (ACPI_SUCCESS(status))
1016 static int amdgpu_acpi_enumerate_xcc(void)
1018 struct amdgpu_acpi_dev_info *dev_info = NULL;
1019 struct amdgpu_acpi_xcc_info *xcc_info;
1020 struct acpi_device *acpi_dev;
1021 char hid[ACPI_ID_LEN];
1025 INIT_LIST_HEAD(&amdgpu_acpi_dev_list);
1027 for (id = 0; id < AMD_XCC_MAX_HID; id++) {
1028 sprintf(hid, "%s%d", "AMD", AMD_XCC_HID_START + id);
1029 acpi_dev = acpi_dev_get_first_match_dev(hid, NULL, -1);
1030 /* These ACPI objects are expected to be in sequential order. If
1031 * one is not found, no need to check the rest.
1034 DRM_DEBUG_DRIVER("No matching acpi device found for %s",
1039 xcc_info = kzalloc(sizeof(struct amdgpu_acpi_xcc_info),
1042 DRM_ERROR("Failed to allocate memory for xcc info\n");
1046 INIT_LIST_HEAD(&xcc_info->list);
1047 xcc_info->handle = acpi_device_handle(acpi_dev);
1048 acpi_dev_put(acpi_dev);
1050 ret = amdgpu_acpi_get_xcc_info(xcc_info, &bdf);
1056 dev_info = amdgpu_acpi_get_dev(bdf);
1059 ret = amdgpu_acpi_dev_init(&dev_info, xcc_info, bdf);
1069 list_add_tail(&xcc_info->list, &dev_info->xcc_list);
1075 int amdgpu_acpi_get_tmr_info(struct amdgpu_device *adev, u64 *tmr_offset,
1078 struct amdgpu_acpi_dev_info *dev_info;
1081 if (!tmr_offset || !tmr_size)
1084 bdf = (adev->pdev->bus->number << 8) | adev->pdev->devfn;
1085 dev_info = amdgpu_acpi_get_dev(bdf);
1089 *tmr_offset = dev_info->tmr_base;
1090 *tmr_size = dev_info->tmr_size;
1096 * amdgpu_acpi_event - handle notify events
1098 * @nb: notifier block
1102 * Calls relevant amdgpu functions in response to various
1104 * Returns NOTIFY code
1106 static int amdgpu_acpi_event(struct notifier_block *nb,
1110 struct amdgpu_device *adev = container_of(nb, struct amdgpu_device, acpi_nb);
1111 struct acpi_bus_event *entry = (struct acpi_bus_event *)data;
1113 if (strcmp(entry->device_class, ACPI_AC_CLASS) == 0) {
1114 if (power_supply_is_system_supplied() > 0)
1115 DRM_DEBUG_DRIVER("pm: AC\n");
1117 DRM_DEBUG_DRIVER("pm: DC\n");
1119 amdgpu_pm_acpi_event_handler(adev);
1122 /* Check for pending SBIOS requests */
1123 return amdgpu_atif_handler(adev, entry);
1126 /* Call all ACPI methods here */
1128 * amdgpu_acpi_init - init driver acpi support
1130 * @adev: amdgpu_device pointer
1132 * Verifies the AMD ACPI interfaces and registers with the acpi
1133 * notifier chain (all asics).
1134 * Returns 0 on success, error on failure.
1136 int amdgpu_acpi_init(struct amdgpu_device *adev)
1138 struct amdgpu_atif *atif = &amdgpu_acpi_priv.atif;
1140 if (atif->notifications.brightness_change) {
1141 if (adev->dc_enabled) {
1142 #if defined(CONFIG_DRM_AMD_DC)
1143 struct amdgpu_display_manager *dm = &adev->dm;
1145 if (dm->backlight_dev[0])
1146 atif->bd = dm->backlight_dev[0];
1149 struct drm_encoder *tmp;
1151 /* Find the encoder controlling the brightness */
1152 list_for_each_entry(tmp, &adev_to_drm(adev)->mode_config.encoder_list,
1154 struct amdgpu_encoder *enc = to_amdgpu_encoder(tmp);
1156 if ((enc->devices & (ATOM_DEVICE_LCD_SUPPORT)) &&
1158 struct amdgpu_encoder_atom_dig *dig = enc->enc_priv;
1161 atif->bd = dig->bl_dev;
1168 adev->acpi_nb.notifier_call = amdgpu_acpi_event;
1169 register_acpi_notifier(&adev->acpi_nb);
1174 void amdgpu_acpi_get_backlight_caps(struct amdgpu_dm_backlight_caps *caps)
1176 struct amdgpu_atif *atif = &amdgpu_acpi_priv.atif;
1178 caps->caps_valid = atif->backlight_caps.caps_valid;
1179 caps->min_input_signal = atif->backlight_caps.min_input_signal;
1180 caps->max_input_signal = atif->backlight_caps.max_input_signal;
1184 * amdgpu_acpi_fini - tear down driver acpi support
1186 * @adev: amdgpu_device pointer
1188 * Unregisters with the acpi notifier chain (all asics).
1190 void amdgpu_acpi_fini(struct amdgpu_device *adev)
1192 unregister_acpi_notifier(&adev->acpi_nb);
1196 * amdgpu_atif_pci_probe_handle - look up the ATIF handle
1200 * Look up the ATIF handles (all asics).
1201 * Returns true if the handle is found, false if not.
1203 static bool amdgpu_atif_pci_probe_handle(struct pci_dev *pdev)
1205 char acpi_method_name[255] = { 0 };
1206 struct acpi_buffer buffer = {sizeof(acpi_method_name), acpi_method_name};
1207 acpi_handle dhandle, atif_handle;
1211 dhandle = ACPI_HANDLE(&pdev->dev);
1215 status = acpi_get_handle(dhandle, "ATIF", &atif_handle);
1216 if (ACPI_FAILURE(status))
1219 amdgpu_acpi_priv.atif.handle = atif_handle;
1220 acpi_get_name(amdgpu_acpi_priv.atif.handle, ACPI_FULL_PATHNAME, &buffer);
1221 DRM_DEBUG_DRIVER("Found ATIF handle %s\n", acpi_method_name);
1222 ret = amdgpu_atif_verify_interface(&amdgpu_acpi_priv.atif);
1224 amdgpu_acpi_priv.atif.handle = 0;
1231 * amdgpu_atcs_pci_probe_handle - look up the ATCS handle
1235 * Look up the ATCS handles (all asics).
1236 * Returns true if the handle is found, false if not.
1238 static bool amdgpu_atcs_pci_probe_handle(struct pci_dev *pdev)
1240 char acpi_method_name[255] = { 0 };
1241 struct acpi_buffer buffer = { sizeof(acpi_method_name), acpi_method_name };
1242 acpi_handle dhandle, atcs_handle;
1246 dhandle = ACPI_HANDLE(&pdev->dev);
1250 status = acpi_get_handle(dhandle, "ATCS", &atcs_handle);
1251 if (ACPI_FAILURE(status))
1254 amdgpu_acpi_priv.atcs.handle = atcs_handle;
1255 acpi_get_name(amdgpu_acpi_priv.atcs.handle, ACPI_FULL_PATHNAME, &buffer);
1256 DRM_DEBUG_DRIVER("Found ATCS handle %s\n", acpi_method_name);
1257 ret = amdgpu_atcs_verify_interface(&amdgpu_acpi_priv.atcs);
1259 amdgpu_acpi_priv.atcs.handle = 0;
1267 * amdgpu_acpi_should_gpu_reset
1269 * @adev: amdgpu_device_pointer
1271 * returns true if should reset GPU, false if not
1273 bool amdgpu_acpi_should_gpu_reset(struct amdgpu_device *adev)
1275 if ((adev->flags & AMD_IS_APU) &&
1276 adev->gfx.imu.funcs) /* Not need to do mode2 reset for IMU enabled APUs */
1279 if ((adev->flags & AMD_IS_APU) &&
1280 amdgpu_acpi_is_s3_active(adev))
1283 if (amdgpu_sriov_vf(adev))
1286 #if IS_ENABLED(CONFIG_SUSPEND)
1287 return pm_suspend_target_state != PM_SUSPEND_TO_IDLE;
1294 * amdgpu_acpi_detect - detect ACPI ATIF/ATCS methods
1296 * Check if we have the ATIF/ATCS methods and populate
1297 * the structures in the driver.
1299 void amdgpu_acpi_detect(void)
1301 struct amdgpu_atif *atif = &amdgpu_acpi_priv.atif;
1302 struct amdgpu_atcs *atcs = &amdgpu_acpi_priv.atcs;
1303 struct pci_dev *pdev = NULL;
1306 while ((pdev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, pdev)) != NULL) {
1308 amdgpu_atif_pci_probe_handle(pdev);
1310 amdgpu_atcs_pci_probe_handle(pdev);
1313 while ((pdev = pci_get_class(PCI_CLASS_DISPLAY_OTHER << 8, pdev)) != NULL) {
1315 amdgpu_atif_pci_probe_handle(pdev);
1317 amdgpu_atcs_pci_probe_handle(pdev);
1320 if (atif->functions.sbios_requests && !atif->functions.system_params) {
1321 /* XXX check this workraround, if sbios request function is
1322 * present we have to see how it's configured in the system
1325 atif->functions.system_params = true;
1328 if (atif->functions.system_params) {
1329 ret = amdgpu_atif_get_notification_params(atif);
1331 DRM_DEBUG_DRIVER("Call to GET_SYSTEM_PARAMS failed: %d\n",
1333 /* Disable notification */
1334 atif->notification_cfg.enabled = false;
1338 if (atif->functions.query_backlight_transfer_characteristics) {
1339 ret = amdgpu_atif_query_backlight_caps(atif);
1341 DRM_DEBUG_DRIVER("Call to QUERY_BACKLIGHT_TRANSFER_CHARACTERISTICS failed: %d\n",
1343 atif->backlight_caps.caps_valid = false;
1346 atif->backlight_caps.caps_valid = false;
1349 amdgpu_acpi_enumerate_xcc();
1352 void amdgpu_acpi_release(void)
1354 struct amdgpu_acpi_dev_info *dev_info, *dev_tmp;
1355 struct amdgpu_acpi_xcc_info *xcc_info, *xcc_tmp;
1357 if (list_empty(&amdgpu_acpi_dev_list))
1360 list_for_each_entry_safe(dev_info, dev_tmp, &amdgpu_acpi_dev_list,
1362 list_for_each_entry_safe(xcc_info, xcc_tmp, &dev_info->xcc_list,
1364 list_del(&xcc_info->list);
1368 list_del(&dev_info->list);
1373 #if IS_ENABLED(CONFIG_SUSPEND)
1375 * amdgpu_acpi_is_s3_active
1377 * @adev: amdgpu_device_pointer
1379 * returns true if supported, false if not.
1381 bool amdgpu_acpi_is_s3_active(struct amdgpu_device *adev)
1383 return !(adev->flags & AMD_IS_APU) ||
1384 (pm_suspend_target_state == PM_SUSPEND_MEM);
1388 * amdgpu_acpi_is_s0ix_active
1390 * @adev: amdgpu_device_pointer
1392 * returns true if supported, false if not.
1394 bool amdgpu_acpi_is_s0ix_active(struct amdgpu_device *adev)
1396 if (!(adev->flags & AMD_IS_APU) ||
1397 (pm_suspend_target_state != PM_SUSPEND_TO_IDLE))
1400 if (adev->asic_type < CHIP_RAVEN)
1404 * If ACPI_FADT_LOW_POWER_S0 is not set in the FADT, it is generally
1405 * risky to do any special firmware-related preparations for entering
1406 * S0ix even though the system is suspending to idle, so return false
1409 if (!(acpi_gbl_FADT.flags & ACPI_FADT_LOW_POWER_S0))
1410 dev_warn_once(adev->dev,
1411 "Power consumption will be higher as BIOS has not been configured for suspend-to-idle.\n"
1412 "To use suspend-to-idle change the sleep mode in BIOS setup.\n");
1414 #if !IS_ENABLED(CONFIG_AMD_PMC)
1415 dev_warn_once(adev->dev,
1416 "Power consumption will be higher as the kernel has not been compiled with CONFIG_AMD_PMC.\n");
1417 #endif /* CONFIG_AMD_PMC */
1421 #endif /* CONFIG_SUSPEND */