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/xarray.h>
30 #include <linux/power_supply.h>
31 #include <linux/pm_runtime.h>
32 #include <linux/suspend.h>
33 #include <acpi/video.h>
34 #include <acpi/actbl.h>
37 #include "amdgpu_pm.h"
38 #include "amdgpu_display.h"
42 /* Declare GUID for AMD _DSM method for XCCs */
43 static const guid_t amd_xcc_dsm_guid = GUID_INIT(0x8267f5d5, 0xa556, 0x44f2,
44 0xb8, 0xb4, 0x45, 0x56, 0x2e,
47 #define AMD_XCC_HID_START 3000
48 #define AMD_XCC_DSM_GET_NUM_FUNCS 0
49 #define AMD_XCC_DSM_GET_SUPP_MODE 1
50 #define AMD_XCC_DSM_GET_XCP_MODE 2
51 #define AMD_XCC_DSM_GET_VF_XCC_MAPPING 4
52 #define AMD_XCC_DSM_GET_TMR_INFO 5
53 #define AMD_XCC_DSM_NUM_FUNCS 5
55 #define AMD_XCC_MAX_HID 24
57 struct xarray numa_info_xa;
59 /* Encapsulates the XCD acpi object information */
60 struct amdgpu_acpi_xcc_info {
61 struct list_head list;
62 struct amdgpu_numa_info *numa_info;
68 struct amdgpu_acpi_dev_info {
69 struct list_head list;
70 struct list_head xcc_list;
72 uint16_t supp_xcp_mode;
79 struct list_head amdgpu_acpi_dev_list;
81 struct amdgpu_atif_notification_cfg {
86 struct amdgpu_atif_notifications {
88 bool forced_power_state;
89 bool system_power_state;
90 bool brightness_change;
91 bool dgpu_display_event;
92 bool gpu_package_power_limit;
95 struct amdgpu_atif_functions {
98 bool temperature_change;
99 bool query_backlight_transfer_characteristics;
100 bool ready_to_undock;
101 bool external_gpu_information;
107 struct amdgpu_atif_notifications notifications;
108 struct amdgpu_atif_functions functions;
109 struct amdgpu_atif_notification_cfg notification_cfg;
110 struct backlight_device *bd;
111 struct amdgpu_dm_backlight_caps backlight_caps;
114 struct amdgpu_atcs_functions {
119 bool power_shift_control;
125 struct amdgpu_atcs_functions functions;
128 static struct amdgpu_acpi_priv {
129 struct amdgpu_atif atif;
130 struct amdgpu_atcs atcs;
133 /* Call the ATIF method
136 * amdgpu_atif_call - call an ATIF method
138 * @atif: atif structure
139 * @function: the ATIF function to execute
140 * @params: ATIF function params
142 * Executes the requested ATIF function (all asics).
143 * Returns a pointer to the acpi output buffer.
145 static union acpi_object *amdgpu_atif_call(struct amdgpu_atif *atif,
147 struct acpi_buffer *params)
150 union acpi_object *obj;
151 union acpi_object atif_arg_elements[2];
152 struct acpi_object_list atif_arg;
153 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
156 atif_arg.pointer = &atif_arg_elements[0];
158 atif_arg_elements[0].type = ACPI_TYPE_INTEGER;
159 atif_arg_elements[0].integer.value = function;
162 atif_arg_elements[1].type = ACPI_TYPE_BUFFER;
163 atif_arg_elements[1].buffer.length = params->length;
164 atif_arg_elements[1].buffer.pointer = params->pointer;
166 /* We need a second fake parameter */
167 atif_arg_elements[1].type = ACPI_TYPE_INTEGER;
168 atif_arg_elements[1].integer.value = 0;
171 status = acpi_evaluate_object(atif->handle, NULL, &atif_arg,
173 obj = (union acpi_object *)buffer.pointer;
175 /* Fail if calling the method fails */
176 if (ACPI_FAILURE(status)) {
177 DRM_DEBUG_DRIVER("failed to evaluate ATIF got %s\n",
178 acpi_format_exception(status));
183 if (obj->type != ACPI_TYPE_BUFFER) {
184 DRM_DEBUG_DRIVER("bad object returned from ATIF: %d\n",
194 * amdgpu_atif_parse_notification - parse supported notifications
196 * @n: supported notifications struct
197 * @mask: supported notifications mask from ATIF
199 * Use the supported notifications mask from ATIF function
200 * ATIF_FUNCTION_VERIFY_INTERFACE to determine what notifications
201 * are supported (all asics).
203 static void amdgpu_atif_parse_notification(struct amdgpu_atif_notifications *n, u32 mask)
205 n->thermal_state = mask & ATIF_THERMAL_STATE_CHANGE_REQUEST_SUPPORTED;
206 n->forced_power_state = mask & ATIF_FORCED_POWER_STATE_CHANGE_REQUEST_SUPPORTED;
207 n->system_power_state = mask & ATIF_SYSTEM_POWER_SOURCE_CHANGE_REQUEST_SUPPORTED;
208 n->brightness_change = mask & ATIF_PANEL_BRIGHTNESS_CHANGE_REQUEST_SUPPORTED;
209 n->dgpu_display_event = mask & ATIF_DGPU_DISPLAY_EVENT_SUPPORTED;
210 n->gpu_package_power_limit = mask & ATIF_GPU_PACKAGE_POWER_LIMIT_REQUEST_SUPPORTED;
214 * amdgpu_atif_parse_functions - parse supported functions
216 * @f: supported functions struct
217 * @mask: supported functions mask from ATIF
219 * Use the supported functions mask from ATIF function
220 * ATIF_FUNCTION_VERIFY_INTERFACE to determine what functions
221 * are supported (all asics).
223 static void amdgpu_atif_parse_functions(struct amdgpu_atif_functions *f, u32 mask)
225 f->system_params = mask & ATIF_GET_SYSTEM_PARAMETERS_SUPPORTED;
226 f->sbios_requests = mask & ATIF_GET_SYSTEM_BIOS_REQUESTS_SUPPORTED;
227 f->temperature_change = mask & ATIF_TEMPERATURE_CHANGE_NOTIFICATION_SUPPORTED;
228 f->query_backlight_transfer_characteristics =
229 mask & ATIF_QUERY_BACKLIGHT_TRANSFER_CHARACTERISTICS_SUPPORTED;
230 f->ready_to_undock = mask & ATIF_READY_TO_UNDOCK_NOTIFICATION_SUPPORTED;
231 f->external_gpu_information = mask & ATIF_GET_EXTERNAL_GPU_INFORMATION_SUPPORTED;
235 * amdgpu_atif_verify_interface - verify ATIF
237 * @atif: amdgpu atif struct
239 * Execute the ATIF_FUNCTION_VERIFY_INTERFACE ATIF function
240 * to initialize ATIF and determine what features are supported
242 * returns 0 on success, error on failure.
244 static int amdgpu_atif_verify_interface(struct amdgpu_atif *atif)
246 union acpi_object *info;
247 struct atif_verify_interface output;
251 info = amdgpu_atif_call(atif, ATIF_FUNCTION_VERIFY_INTERFACE, NULL);
255 memset(&output, 0, sizeof(output));
257 size = *(u16 *) info->buffer.pointer;
259 DRM_INFO("ATIF buffer is too small: %zu\n", size);
263 size = min(sizeof(output), size);
265 memcpy(&output, info->buffer.pointer, size);
267 /* TODO: check version? */
268 DRM_DEBUG_DRIVER("ATIF version %u\n", output.version);
270 amdgpu_atif_parse_notification(&atif->notifications, output.notification_mask);
271 amdgpu_atif_parse_functions(&atif->functions, output.function_bits);
279 * amdgpu_atif_get_notification_params - determine notify configuration
283 * Execute the ATIF_FUNCTION_GET_SYSTEM_PARAMETERS ATIF function
284 * to determine if a notifier is used and if so which one
285 * (all asics). This is either Notify(VGA, 0x81) or Notify(VGA, n)
286 * where n is specified in the result if a notifier is used.
287 * Returns 0 on success, error on failure.
289 static int amdgpu_atif_get_notification_params(struct amdgpu_atif *atif)
291 union acpi_object *info;
292 struct amdgpu_atif_notification_cfg *n = &atif->notification_cfg;
293 struct atif_system_params params;
297 info = amdgpu_atif_call(atif, ATIF_FUNCTION_GET_SYSTEM_PARAMETERS,
304 size = *(u16 *) info->buffer.pointer;
310 memset(¶ms, 0, sizeof(params));
311 size = min(sizeof(params), size);
312 memcpy(¶ms, info->buffer.pointer, size);
314 DRM_DEBUG_DRIVER("SYSTEM_PARAMS: mask = %#x, flags = %#x\n",
315 params.flags, params.valid_mask);
316 params.flags = params.flags & params.valid_mask;
318 if ((params.flags & ATIF_NOTIFY_MASK) == ATIF_NOTIFY_NONE) {
321 } else if ((params.flags & ATIF_NOTIFY_MASK) == ATIF_NOTIFY_81) {
323 n->command_code = 0x81;
330 n->command_code = params.command_code;
334 DRM_DEBUG_DRIVER("Notification %s, command code = %#x\n",
335 (n->enabled ? "enabled" : "disabled"),
342 * amdgpu_atif_query_backlight_caps - get min and max backlight input signal
346 * Execute the QUERY_BRIGHTNESS_TRANSFER_CHARACTERISTICS ATIF function
347 * to determine the acceptable range of backlight values
349 * Backlight_caps.caps_valid will be set to true if the query is successful
351 * The input signals are in range 0-255
353 * This function assumes the display with backlight is the first LCD
355 * Returns 0 on success, error on failure.
357 static int amdgpu_atif_query_backlight_caps(struct amdgpu_atif *atif)
359 union acpi_object *info;
360 struct atif_qbtc_output characteristics;
361 struct atif_qbtc_arguments arguments;
362 struct acpi_buffer params;
366 arguments.size = sizeof(arguments);
367 arguments.requested_display = ATIF_QBTC_REQUEST_LCD1;
369 params.length = sizeof(arguments);
370 params.pointer = (void *)&arguments;
372 info = amdgpu_atif_call(atif,
373 ATIF_FUNCTION_QUERY_BRIGHTNESS_TRANSFER_CHARACTERISTICS,
380 size = *(u16 *) info->buffer.pointer;
386 memset(&characteristics, 0, sizeof(characteristics));
387 size = min(sizeof(characteristics), size);
388 memcpy(&characteristics, info->buffer.pointer, size);
390 atif->backlight_caps.caps_valid = true;
391 atif->backlight_caps.min_input_signal =
392 characteristics.min_input_signal;
393 atif->backlight_caps.max_input_signal =
394 characteristics.max_input_signal;
395 atif->backlight_caps.ac_level = characteristics.ac_level;
396 atif->backlight_caps.dc_level = characteristics.dc_level;
403 * amdgpu_atif_get_sbios_requests - get requested sbios event
406 * @req: atif sbios request struct
408 * Execute the ATIF_FUNCTION_GET_SYSTEM_BIOS_REQUESTS ATIF function
409 * to determine what requests the sbios is making to the driver
411 * Returns 0 on success, error on failure.
413 static int amdgpu_atif_get_sbios_requests(struct amdgpu_atif *atif,
414 struct atif_sbios_requests *req)
416 union acpi_object *info;
420 info = amdgpu_atif_call(atif, ATIF_FUNCTION_GET_SYSTEM_BIOS_REQUESTS,
425 size = *(u16 *)info->buffer.pointer;
430 memset(req, 0, sizeof(*req));
432 size = min(sizeof(*req), size);
433 memcpy(req, info->buffer.pointer, size);
434 DRM_DEBUG_DRIVER("SBIOS pending requests: %#x\n", req->pending);
436 count = hweight32(req->pending);
444 * amdgpu_atif_handler - handle ATIF notify requests
446 * @adev: amdgpu_device pointer
447 * @event: atif sbios request struct
449 * Checks the acpi event and if it matches an atif event,
453 * NOTIFY_BAD or NOTIFY_DONE, depending on the event.
455 static int amdgpu_atif_handler(struct amdgpu_device *adev,
456 struct acpi_bus_event *event)
458 struct amdgpu_atif *atif = &amdgpu_acpi_priv.atif;
461 DRM_DEBUG_DRIVER("event, device_class = %s, type = %#x\n",
462 event->device_class, event->type);
464 if (strcmp(event->device_class, ACPI_VIDEO_CLASS) != 0)
467 /* Is this actually our event? */
468 if (!atif->notification_cfg.enabled ||
469 event->type != atif->notification_cfg.command_code) {
470 /* These events will generate keypresses otherwise */
471 if (event->type == ACPI_VIDEO_NOTIFY_PROBE)
477 if (atif->functions.sbios_requests) {
478 struct atif_sbios_requests req;
480 /* Check pending SBIOS requests */
481 count = amdgpu_atif_get_sbios_requests(atif, &req);
486 DRM_DEBUG_DRIVER("ATIF: %d pending SBIOS requests\n", count);
488 if (req.pending & ATIF_PANEL_BRIGHTNESS_CHANGE_REQUEST) {
490 DRM_DEBUG_DRIVER("Changing brightness to %d\n",
491 req.backlight_level);
493 * XXX backlight_device_set_brightness() is
494 * hardwired to post BACKLIGHT_UPDATE_SYSFS.
495 * It probably should accept 'reason' parameter.
497 backlight_device_set_brightness(atif->bd, req.backlight_level);
501 if (req.pending & ATIF_DGPU_DISPLAY_EVENT) {
502 if (adev->flags & AMD_IS_PX) {
503 pm_runtime_get_sync(adev_to_drm(adev)->dev);
504 /* Just fire off a uevent and let userspace tell us what to do */
505 drm_helper_hpd_irq_event(adev_to_drm(adev));
506 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
507 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
510 /* TODO: check other events */
513 /* We've handled the event, stop the notifier chain. The ACPI interface
514 * overloads ACPI_VIDEO_NOTIFY_PROBE, we don't want to send that to
515 * userspace if the event was generated only to signal a SBIOS
521 /* Call the ATCS method
524 * amdgpu_atcs_call - call an ATCS method
526 * @atcs: atcs structure
527 * @function: the ATCS function to execute
528 * @params: ATCS function params
530 * Executes the requested ATCS function (all asics).
531 * Returns a pointer to the acpi output buffer.
533 static union acpi_object *amdgpu_atcs_call(struct amdgpu_atcs *atcs,
535 struct acpi_buffer *params)
538 union acpi_object atcs_arg_elements[2];
539 struct acpi_object_list atcs_arg;
540 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
543 atcs_arg.pointer = &atcs_arg_elements[0];
545 atcs_arg_elements[0].type = ACPI_TYPE_INTEGER;
546 atcs_arg_elements[0].integer.value = function;
549 atcs_arg_elements[1].type = ACPI_TYPE_BUFFER;
550 atcs_arg_elements[1].buffer.length = params->length;
551 atcs_arg_elements[1].buffer.pointer = params->pointer;
553 /* We need a second fake parameter */
554 atcs_arg_elements[1].type = ACPI_TYPE_INTEGER;
555 atcs_arg_elements[1].integer.value = 0;
558 status = acpi_evaluate_object(atcs->handle, NULL, &atcs_arg, &buffer);
560 /* Fail only if calling the method fails and ATIF is supported */
561 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
562 DRM_DEBUG_DRIVER("failed to evaluate ATCS got %s\n",
563 acpi_format_exception(status));
564 kfree(buffer.pointer);
568 return buffer.pointer;
572 * amdgpu_atcs_parse_functions - parse supported functions
574 * @f: supported functions struct
575 * @mask: supported functions mask from ATCS
577 * Use the supported functions mask from ATCS function
578 * ATCS_FUNCTION_VERIFY_INTERFACE to determine what functions
579 * are supported (all asics).
581 static void amdgpu_atcs_parse_functions(struct amdgpu_atcs_functions *f, u32 mask)
583 f->get_ext_state = mask & ATCS_GET_EXTERNAL_STATE_SUPPORTED;
584 f->pcie_perf_req = mask & ATCS_PCIE_PERFORMANCE_REQUEST_SUPPORTED;
585 f->pcie_dev_rdy = mask & ATCS_PCIE_DEVICE_READY_NOTIFICATION_SUPPORTED;
586 f->pcie_bus_width = mask & ATCS_SET_PCIE_BUS_WIDTH_SUPPORTED;
587 f->power_shift_control = mask & ATCS_SET_POWER_SHIFT_CONTROL_SUPPORTED;
591 * amdgpu_atcs_verify_interface - verify ATCS
593 * @atcs: amdgpu atcs struct
595 * Execute the ATCS_FUNCTION_VERIFY_INTERFACE ATCS function
596 * to initialize ATCS and determine what features are supported
598 * returns 0 on success, error on failure.
600 static int amdgpu_atcs_verify_interface(struct amdgpu_atcs *atcs)
602 union acpi_object *info;
603 struct atcs_verify_interface output;
607 info = amdgpu_atcs_call(atcs, ATCS_FUNCTION_VERIFY_INTERFACE, NULL);
611 memset(&output, 0, sizeof(output));
613 size = *(u16 *) info->buffer.pointer;
615 DRM_INFO("ATCS buffer is too small: %zu\n", size);
619 size = min(sizeof(output), size);
621 memcpy(&output, info->buffer.pointer, size);
623 /* TODO: check version? */
624 DRM_DEBUG_DRIVER("ATCS version %u\n", output.version);
626 amdgpu_atcs_parse_functions(&atcs->functions, output.function_bits);
634 * amdgpu_acpi_is_pcie_performance_request_supported
636 * @adev: amdgpu_device pointer
638 * Check if the ATCS pcie_perf_req and pcie_dev_rdy methods
639 * are supported (all asics).
640 * returns true if supported, false if not.
642 bool amdgpu_acpi_is_pcie_performance_request_supported(struct amdgpu_device *adev)
644 struct amdgpu_atcs *atcs = &amdgpu_acpi_priv.atcs;
646 if (atcs->functions.pcie_perf_req && atcs->functions.pcie_dev_rdy)
653 * amdgpu_acpi_is_power_shift_control_supported
655 * Check if the ATCS power shift control method
657 * returns true if supported, false if not.
659 bool amdgpu_acpi_is_power_shift_control_supported(void)
661 return amdgpu_acpi_priv.atcs.functions.power_shift_control;
665 * amdgpu_acpi_pcie_notify_device_ready
667 * @adev: amdgpu_device pointer
669 * Executes the PCIE_DEVICE_READY_NOTIFICATION method
671 * returns 0 on success, error on failure.
673 int amdgpu_acpi_pcie_notify_device_ready(struct amdgpu_device *adev)
675 union acpi_object *info;
676 struct amdgpu_atcs *atcs = &amdgpu_acpi_priv.atcs;
678 if (!atcs->functions.pcie_dev_rdy)
681 info = amdgpu_atcs_call(atcs, ATCS_FUNCTION_PCIE_DEVICE_READY_NOTIFICATION, NULL);
691 * amdgpu_acpi_pcie_performance_request
693 * @adev: amdgpu_device pointer
694 * @perf_req: requested perf level (pcie gen speed)
695 * @advertise: set advertise caps flag if set
697 * Executes the PCIE_PERFORMANCE_REQUEST method to
698 * change the pcie gen speed (all asics).
699 * returns 0 on success, error on failure.
701 int amdgpu_acpi_pcie_performance_request(struct amdgpu_device *adev,
702 u8 perf_req, bool advertise)
704 union acpi_object *info;
705 struct amdgpu_atcs *atcs = &amdgpu_acpi_priv.atcs;
706 struct atcs_pref_req_input atcs_input;
707 struct atcs_pref_req_output atcs_output;
708 struct acpi_buffer params;
712 if (amdgpu_acpi_pcie_notify_device_ready(adev))
715 if (!atcs->functions.pcie_perf_req)
718 atcs_input.size = sizeof(struct atcs_pref_req_input);
719 /* client id (bit 2-0: func num, 7-3: dev num, 15-8: bus num) */
720 atcs_input.client_id = pci_dev_id(adev->pdev);
721 atcs_input.valid_flags_mask = ATCS_VALID_FLAGS_MASK;
722 atcs_input.flags = ATCS_WAIT_FOR_COMPLETION;
724 atcs_input.flags |= ATCS_ADVERTISE_CAPS;
725 atcs_input.req_type = ATCS_PCIE_LINK_SPEED;
726 atcs_input.perf_req = perf_req;
728 params.length = sizeof(struct atcs_pref_req_input);
729 params.pointer = &atcs_input;
732 info = amdgpu_atcs_call(atcs, ATCS_FUNCTION_PCIE_PERFORMANCE_REQUEST, ¶ms);
736 memset(&atcs_output, 0, sizeof(atcs_output));
738 size = *(u16 *) info->buffer.pointer;
740 DRM_INFO("ATCS buffer is too small: %zu\n", size);
744 size = min(sizeof(atcs_output), size);
746 memcpy(&atcs_output, info->buffer.pointer, size);
750 switch (atcs_output.ret_val) {
751 case ATCS_REQUEST_REFUSED:
754 case ATCS_REQUEST_COMPLETE:
756 case ATCS_REQUEST_IN_PROGRESS:
766 * amdgpu_acpi_power_shift_control
768 * @adev: amdgpu_device pointer
769 * @dev_state: device acpi state
770 * @drv_state: driver state
772 * Executes the POWER_SHIFT_CONTROL method to
773 * communicate current dGPU device state and
774 * driver state to APU/SBIOS.
775 * returns 0 on success, error on failure.
777 int amdgpu_acpi_power_shift_control(struct amdgpu_device *adev,
778 u8 dev_state, bool drv_state)
780 union acpi_object *info;
781 struct amdgpu_atcs *atcs = &amdgpu_acpi_priv.atcs;
782 struct atcs_pwr_shift_input atcs_input;
783 struct acpi_buffer params;
785 if (!amdgpu_acpi_is_power_shift_control_supported())
788 atcs_input.size = sizeof(struct atcs_pwr_shift_input);
789 /* dGPU id (bit 2-0: func num, 7-3: dev num, 15-8: bus num) */
790 atcs_input.dgpu_id = pci_dev_id(adev->pdev);
791 atcs_input.dev_acpi_state = dev_state;
792 atcs_input.drv_state = drv_state;
794 params.length = sizeof(struct atcs_pwr_shift_input);
795 params.pointer = &atcs_input;
797 info = amdgpu_atcs_call(atcs, ATCS_FUNCTION_POWER_SHIFT_CONTROL, ¶ms);
799 DRM_ERROR("ATCS PSC update failed\n");
807 * amdgpu_acpi_smart_shift_update - update dGPU device state to SBIOS
809 * @dev: drm_device pointer
810 * @ss_state: current smart shift event
812 * returns 0 on success,
813 * otherwise return error number.
815 int amdgpu_acpi_smart_shift_update(struct drm_device *dev, enum amdgpu_ss ss_state)
817 struct amdgpu_device *adev = drm_to_adev(dev);
820 if (!amdgpu_device_supports_smart_shift(dev))
824 /* SBIOS trigger “stop”, “enable” and “start” at D0, Driver Operational.
825 * SBIOS trigger “stop” at D3, Driver Not Operational.
826 * SBIOS trigger “stop” and “disable” at D0, Driver NOT operational.
828 case AMDGPU_SS_DRV_LOAD:
829 r = amdgpu_acpi_power_shift_control(adev,
830 AMDGPU_ATCS_PSC_DEV_STATE_D0,
831 AMDGPU_ATCS_PSC_DRV_STATE_OPR);
833 case AMDGPU_SS_DEV_D0:
834 r = amdgpu_acpi_power_shift_control(adev,
835 AMDGPU_ATCS_PSC_DEV_STATE_D0,
836 AMDGPU_ATCS_PSC_DRV_STATE_OPR);
838 case AMDGPU_SS_DEV_D3:
839 r = amdgpu_acpi_power_shift_control(adev,
840 AMDGPU_ATCS_PSC_DEV_STATE_D3_HOT,
841 AMDGPU_ATCS_PSC_DRV_STATE_NOT_OPR);
843 case AMDGPU_SS_DRV_UNLOAD:
844 r = amdgpu_acpi_power_shift_control(adev,
845 AMDGPU_ATCS_PSC_DEV_STATE_D0,
846 AMDGPU_ATCS_PSC_DRV_STATE_NOT_OPR);
855 #ifdef CONFIG_ACPI_NUMA
856 static inline uint64_t amdgpu_acpi_get_numa_size(int nid)
858 /* This is directly using si_meminfo_node implementation as the
859 * function is not exported.
862 uint64_t managed_pages = 0;
864 pg_data_t *pgdat = NODE_DATA(nid);
866 for (zone_type = 0; zone_type < MAX_NR_ZONES; zone_type++)
868 zone_managed_pages(&pgdat->node_zones[zone_type]);
869 return managed_pages * PAGE_SIZE;
872 static struct amdgpu_numa_info *amdgpu_acpi_get_numa_info(uint32_t pxm)
874 struct amdgpu_numa_info *numa_info;
877 numa_info = xa_load(&numa_info_xa, pxm);
882 numa_info = kzalloc(sizeof(*numa_info), GFP_KERNEL);
886 nid = pxm_to_node(pxm);
887 numa_info->pxm = pxm;
888 numa_info->nid = nid;
890 if (numa_info->nid == NUMA_NO_NODE) {
892 numa_info->size = info.totalram * info.mem_unit;
894 numa_info->size = amdgpu_acpi_get_numa_size(nid);
896 xa_store(&numa_info_xa, numa_info->pxm, numa_info, GFP_KERNEL);
904 * amdgpu_acpi_get_node_id - obtain the NUMA node id for corresponding amdgpu
907 * @handle: acpi handle
908 * @numa_info: amdgpu_numa_info structure holding numa information
910 * Queries the ACPI interface to fetch the corresponding NUMA Node ID for a
911 * given amdgpu acpi device.
913 * Returns ACPI STATUS OK with Node ID on success or the corresponding failure reason
915 static acpi_status amdgpu_acpi_get_node_id(acpi_handle handle,
916 struct amdgpu_numa_info **numa_info)
918 #ifdef CONFIG_ACPI_NUMA
923 return_ACPI_STATUS(AE_ERROR);
925 status = acpi_evaluate_integer(handle, "_PXM", NULL, &pxm);
927 if (ACPI_FAILURE(status))
930 *numa_info = amdgpu_acpi_get_numa_info(pxm);
933 return_ACPI_STATUS(AE_ERROR);
935 return_ACPI_STATUS(AE_OK);
937 return_ACPI_STATUS(AE_NOT_EXIST);
941 static struct amdgpu_acpi_dev_info *amdgpu_acpi_get_dev(u32 sbdf)
943 struct amdgpu_acpi_dev_info *acpi_dev;
945 if (list_empty(&amdgpu_acpi_dev_list))
948 list_for_each_entry(acpi_dev, &amdgpu_acpi_dev_list, list)
949 if (acpi_dev->sbdf == sbdf)
955 static int amdgpu_acpi_dev_init(struct amdgpu_acpi_dev_info **dev_info,
956 struct amdgpu_acpi_xcc_info *xcc_info, u32 sbdf)
958 struct amdgpu_acpi_dev_info *tmp;
959 union acpi_object *obj;
963 tmp = kzalloc(sizeof(struct amdgpu_acpi_dev_info), GFP_KERNEL);
967 INIT_LIST_HEAD(&tmp->xcc_list);
968 INIT_LIST_HEAD(&tmp->list);
971 obj = acpi_evaluate_dsm_typed(xcc_info->handle, &amd_xcc_dsm_guid, 0,
972 AMD_XCC_DSM_GET_SUPP_MODE, NULL,
976 acpi_handle_debug(xcc_info->handle,
977 "_DSM function %d evaluation failed",
978 AMD_XCC_DSM_GET_SUPP_MODE);
983 tmp->supp_xcp_mode = obj->integer.value & 0xFFFF;
986 obj = acpi_evaluate_dsm_typed(xcc_info->handle, &amd_xcc_dsm_guid, 0,
987 AMD_XCC_DSM_GET_XCP_MODE, NULL,
991 acpi_handle_debug(xcc_info->handle,
992 "_DSM function %d evaluation failed",
993 AMD_XCC_DSM_GET_XCP_MODE);
998 tmp->xcp_mode = obj->integer.value & 0xFFFF;
999 tmp->mem_mode = (obj->integer.value >> 32) & 0xFFFF;
1002 /* Evaluate DSMs and fill XCC information */
1003 obj = acpi_evaluate_dsm_typed(xcc_info->handle, &amd_xcc_dsm_guid, 0,
1004 AMD_XCC_DSM_GET_TMR_INFO, NULL,
1007 if (!obj || obj->package.count < 2) {
1008 acpi_handle_debug(xcc_info->handle,
1009 "_DSM function %d evaluation failed",
1010 AMD_XCC_DSM_GET_TMR_INFO);
1015 tmp->tmr_base = obj->package.elements[0].integer.value;
1016 tmp->tmr_size = obj->package.elements[1].integer.value;
1020 "New dev(%x): Supported xcp mode: %x curr xcp_mode : %x mem mode : %x, tmr base: %llx tmr size: %llx ",
1021 tmp->sbdf, tmp->supp_xcp_mode, tmp->xcp_mode, tmp->mem_mode,
1022 tmp->tmr_base, tmp->tmr_size);
1023 list_add_tail(&tmp->list, &amdgpu_acpi_dev_list);
1036 static int amdgpu_acpi_get_xcc_info(struct amdgpu_acpi_xcc_info *xcc_info,
1039 union acpi_object *obj;
1043 obj = acpi_evaluate_dsm_typed(xcc_info->handle, &amd_xcc_dsm_guid, 0,
1044 AMD_XCC_DSM_GET_NUM_FUNCS, NULL,
1047 if (!obj || obj->integer.value != AMD_XCC_DSM_NUM_FUNCS)
1051 /* Evaluate DSMs and fill XCC information */
1052 obj = acpi_evaluate_dsm_typed(xcc_info->handle, &amd_xcc_dsm_guid, 0,
1053 AMD_XCC_DSM_GET_VF_XCC_MAPPING, NULL,
1057 acpi_handle_debug(xcc_info->handle,
1058 "_DSM function %d evaluation failed",
1059 AMD_XCC_DSM_GET_VF_XCC_MAPPING);
1064 /* PF xcc id [39:32] */
1065 xcc_info->phy_id = (obj->integer.value >> 32) & 0xFF;
1066 /* xcp node of this xcc [47:40] */
1067 xcc_info->xcp_node = (obj->integer.value >> 40) & 0xFF;
1068 /* PF domain of this xcc [31:16] */
1069 *sbdf = (obj->integer.value) & 0xFFFF0000;
1070 /* PF bus/dev/fn of this xcc [63:48] */
1071 *sbdf |= (obj->integer.value >> 48) & 0xFFFF;
1076 amdgpu_acpi_get_node_id(xcc_info->handle, &xcc_info->numa_info);
1078 /* TODO: check if this check is required */
1079 if (ACPI_SUCCESS(status))
1088 static int amdgpu_acpi_enumerate_xcc(void)
1090 struct amdgpu_acpi_dev_info *dev_info = NULL;
1091 struct amdgpu_acpi_xcc_info *xcc_info;
1092 struct acpi_device *acpi_dev;
1093 char hid[ACPI_ID_LEN];
1097 INIT_LIST_HEAD(&amdgpu_acpi_dev_list);
1098 xa_init(&numa_info_xa);
1100 for (id = 0; id < AMD_XCC_MAX_HID; id++) {
1101 sprintf(hid, "%s%d", "AMD", AMD_XCC_HID_START + id);
1102 acpi_dev = acpi_dev_get_first_match_dev(hid, NULL, -1);
1103 /* These ACPI objects are expected to be in sequential order. If
1104 * one is not found, no need to check the rest.
1107 DRM_DEBUG_DRIVER("No matching acpi device found for %s",
1112 xcc_info = kzalloc(sizeof(struct amdgpu_acpi_xcc_info),
1115 DRM_ERROR("Failed to allocate memory for xcc info\n");
1119 INIT_LIST_HEAD(&xcc_info->list);
1120 xcc_info->handle = acpi_device_handle(acpi_dev);
1121 acpi_dev_put(acpi_dev);
1123 ret = amdgpu_acpi_get_xcc_info(xcc_info, &sbdf);
1129 dev_info = amdgpu_acpi_get_dev(sbdf);
1132 ret = amdgpu_acpi_dev_init(&dev_info, xcc_info, sbdf);
1142 list_add_tail(&xcc_info->list, &dev_info->xcc_list);
1148 int amdgpu_acpi_get_tmr_info(struct amdgpu_device *adev, u64 *tmr_offset,
1151 struct amdgpu_acpi_dev_info *dev_info;
1154 if (!tmr_offset || !tmr_size)
1157 sbdf = (pci_domain_nr(adev->pdev->bus) << 16);
1158 sbdf |= pci_dev_id(adev->pdev);
1159 dev_info = amdgpu_acpi_get_dev(sbdf);
1163 *tmr_offset = dev_info->tmr_base;
1164 *tmr_size = dev_info->tmr_size;
1169 int amdgpu_acpi_get_mem_info(struct amdgpu_device *adev, int xcc_id,
1170 struct amdgpu_numa_info *numa_info)
1172 struct amdgpu_acpi_dev_info *dev_info;
1173 struct amdgpu_acpi_xcc_info *xcc_info;
1179 sbdf = (pci_domain_nr(adev->pdev->bus) << 16);
1180 sbdf |= pci_dev_id(adev->pdev);
1181 dev_info = amdgpu_acpi_get_dev(sbdf);
1185 list_for_each_entry(xcc_info, &dev_info->xcc_list, list) {
1186 if (xcc_info->phy_id == xcc_id) {
1187 memcpy(numa_info, xcc_info->numa_info,
1188 sizeof(*numa_info));
1197 * amdgpu_acpi_event - handle notify events
1199 * @nb: notifier block
1203 * Calls relevant amdgpu functions in response to various
1205 * Returns NOTIFY code
1207 static int amdgpu_acpi_event(struct notifier_block *nb,
1211 struct amdgpu_device *adev = container_of(nb, struct amdgpu_device, acpi_nb);
1212 struct acpi_bus_event *entry = (struct acpi_bus_event *)data;
1214 if (strcmp(entry->device_class, ACPI_AC_CLASS) == 0) {
1215 if (power_supply_is_system_supplied() > 0)
1216 DRM_DEBUG_DRIVER("pm: AC\n");
1218 DRM_DEBUG_DRIVER("pm: DC\n");
1220 amdgpu_pm_acpi_event_handler(adev);
1223 /* Check for pending SBIOS requests */
1224 return amdgpu_atif_handler(adev, entry);
1227 /* Call all ACPI methods here */
1229 * amdgpu_acpi_init - init driver acpi support
1231 * @adev: amdgpu_device pointer
1233 * Verifies the AMD ACPI interfaces and registers with the acpi
1234 * notifier chain (all asics).
1235 * Returns 0 on success, error on failure.
1237 int amdgpu_acpi_init(struct amdgpu_device *adev)
1239 struct amdgpu_atif *atif = &amdgpu_acpi_priv.atif;
1241 if (atif->notifications.brightness_change) {
1242 if (adev->dc_enabled) {
1243 #if defined(CONFIG_DRM_AMD_DC)
1244 struct amdgpu_display_manager *dm = &adev->dm;
1246 if (dm->backlight_dev[0])
1247 atif->bd = dm->backlight_dev[0];
1250 struct drm_encoder *tmp;
1252 /* Find the encoder controlling the brightness */
1253 list_for_each_entry(tmp, &adev_to_drm(adev)->mode_config.encoder_list,
1255 struct amdgpu_encoder *enc = to_amdgpu_encoder(tmp);
1257 if ((enc->devices & (ATOM_DEVICE_LCD_SUPPORT)) &&
1259 struct amdgpu_encoder_atom_dig *dig = enc->enc_priv;
1262 atif->bd = dig->bl_dev;
1269 adev->acpi_nb.notifier_call = amdgpu_acpi_event;
1270 register_acpi_notifier(&adev->acpi_nb);
1275 void amdgpu_acpi_get_backlight_caps(struct amdgpu_dm_backlight_caps *caps)
1277 struct amdgpu_atif *atif = &amdgpu_acpi_priv.atif;
1279 caps->caps_valid = atif->backlight_caps.caps_valid;
1280 caps->min_input_signal = atif->backlight_caps.min_input_signal;
1281 caps->max_input_signal = atif->backlight_caps.max_input_signal;
1282 caps->ac_level = atif->backlight_caps.ac_level;
1283 caps->dc_level = atif->backlight_caps.dc_level;
1287 * amdgpu_acpi_fini - tear down driver acpi support
1289 * @adev: amdgpu_device pointer
1291 * Unregisters with the acpi notifier chain (all asics).
1293 void amdgpu_acpi_fini(struct amdgpu_device *adev)
1295 unregister_acpi_notifier(&adev->acpi_nb);
1299 * amdgpu_atif_pci_probe_handle - look up the ATIF handle
1303 * Look up the ATIF handles (all asics).
1304 * Returns true if the handle is found, false if not.
1306 static bool amdgpu_atif_pci_probe_handle(struct pci_dev *pdev)
1308 char acpi_method_name[255] = { 0 };
1309 struct acpi_buffer buffer = {sizeof(acpi_method_name), acpi_method_name};
1310 acpi_handle dhandle, atif_handle;
1314 dhandle = ACPI_HANDLE(&pdev->dev);
1318 status = acpi_get_handle(dhandle, "ATIF", &atif_handle);
1319 if (ACPI_FAILURE(status))
1322 amdgpu_acpi_priv.atif.handle = atif_handle;
1323 acpi_get_name(amdgpu_acpi_priv.atif.handle, ACPI_FULL_PATHNAME, &buffer);
1324 DRM_DEBUG_DRIVER("Found ATIF handle %s\n", acpi_method_name);
1325 ret = amdgpu_atif_verify_interface(&amdgpu_acpi_priv.atif);
1327 amdgpu_acpi_priv.atif.handle = 0;
1334 * amdgpu_atcs_pci_probe_handle - look up the ATCS handle
1338 * Look up the ATCS handles (all asics).
1339 * Returns true if the handle is found, false if not.
1341 static bool amdgpu_atcs_pci_probe_handle(struct pci_dev *pdev)
1343 char acpi_method_name[255] = { 0 };
1344 struct acpi_buffer buffer = { sizeof(acpi_method_name), acpi_method_name };
1345 acpi_handle dhandle, atcs_handle;
1349 dhandle = ACPI_HANDLE(&pdev->dev);
1353 status = acpi_get_handle(dhandle, "ATCS", &atcs_handle);
1354 if (ACPI_FAILURE(status))
1357 amdgpu_acpi_priv.atcs.handle = atcs_handle;
1358 acpi_get_name(amdgpu_acpi_priv.atcs.handle, ACPI_FULL_PATHNAME, &buffer);
1359 DRM_DEBUG_DRIVER("Found ATCS handle %s\n", acpi_method_name);
1360 ret = amdgpu_atcs_verify_interface(&amdgpu_acpi_priv.atcs);
1362 amdgpu_acpi_priv.atcs.handle = 0;
1370 * amdgpu_acpi_should_gpu_reset
1372 * @adev: amdgpu_device_pointer
1374 * returns true if should reset GPU, false if not
1376 bool amdgpu_acpi_should_gpu_reset(struct amdgpu_device *adev)
1378 if ((adev->flags & AMD_IS_APU) &&
1379 adev->gfx.imu.funcs) /* Not need to do mode2 reset for IMU enabled APUs */
1382 if ((adev->flags & AMD_IS_APU) &&
1383 amdgpu_acpi_is_s3_active(adev))
1386 if (amdgpu_sriov_vf(adev))
1389 #if IS_ENABLED(CONFIG_SUSPEND)
1390 return pm_suspend_target_state != PM_SUSPEND_TO_IDLE;
1397 * amdgpu_acpi_detect - detect ACPI ATIF/ATCS methods
1399 * Check if we have the ATIF/ATCS methods and populate
1400 * the structures in the driver.
1402 void amdgpu_acpi_detect(void)
1404 struct amdgpu_atif *atif = &amdgpu_acpi_priv.atif;
1405 struct amdgpu_atcs *atcs = &amdgpu_acpi_priv.atcs;
1406 struct pci_dev *pdev = NULL;
1409 while ((pdev = pci_get_base_class(PCI_BASE_CLASS_DISPLAY, pdev))) {
1410 if ((pdev->class != PCI_CLASS_DISPLAY_VGA << 8) &&
1411 (pdev->class != PCI_CLASS_DISPLAY_OTHER << 8))
1415 amdgpu_atif_pci_probe_handle(pdev);
1417 amdgpu_atcs_pci_probe_handle(pdev);
1420 if (atif->functions.sbios_requests && !atif->functions.system_params) {
1421 /* XXX check this workraround, if sbios request function is
1422 * present we have to see how it's configured in the system
1425 atif->functions.system_params = true;
1428 if (atif->functions.system_params) {
1429 ret = amdgpu_atif_get_notification_params(atif);
1431 DRM_DEBUG_DRIVER("Call to GET_SYSTEM_PARAMS failed: %d\n",
1433 /* Disable notification */
1434 atif->notification_cfg.enabled = false;
1438 if (atif->functions.query_backlight_transfer_characteristics) {
1439 ret = amdgpu_atif_query_backlight_caps(atif);
1441 DRM_DEBUG_DRIVER("Call to QUERY_BACKLIGHT_TRANSFER_CHARACTERISTICS failed: %d\n",
1443 atif->backlight_caps.caps_valid = false;
1446 atif->backlight_caps.caps_valid = false;
1449 amdgpu_acpi_enumerate_xcc();
1452 void amdgpu_acpi_release(void)
1454 struct amdgpu_acpi_dev_info *dev_info, *dev_tmp;
1455 struct amdgpu_acpi_xcc_info *xcc_info, *xcc_tmp;
1456 struct amdgpu_numa_info *numa_info;
1457 unsigned long index;
1459 xa_for_each(&numa_info_xa, index, numa_info) {
1461 xa_erase(&numa_info_xa, index);
1464 if (list_empty(&amdgpu_acpi_dev_list))
1467 list_for_each_entry_safe(dev_info, dev_tmp, &amdgpu_acpi_dev_list,
1469 list_for_each_entry_safe(xcc_info, xcc_tmp, &dev_info->xcc_list,
1471 list_del(&xcc_info->list);
1475 list_del(&dev_info->list);
1480 #if IS_ENABLED(CONFIG_SUSPEND)
1482 * amdgpu_acpi_is_s3_active
1484 * @adev: amdgpu_device_pointer
1486 * returns true if supported, false if not.
1488 bool amdgpu_acpi_is_s3_active(struct amdgpu_device *adev)
1490 return !(adev->flags & AMD_IS_APU) ||
1491 (pm_suspend_target_state == PM_SUSPEND_MEM);
1495 * amdgpu_acpi_is_s0ix_active
1497 * @adev: amdgpu_device_pointer
1499 * returns true if supported, false if not.
1501 bool amdgpu_acpi_is_s0ix_active(struct amdgpu_device *adev)
1503 if (!(adev->flags & AMD_IS_APU) ||
1504 (pm_suspend_target_state != PM_SUSPEND_TO_IDLE))
1507 if (adev->asic_type < CHIP_RAVEN)
1510 if (!(adev->pm.pp_feature & PP_GFXOFF_MASK))
1514 * If ACPI_FADT_LOW_POWER_S0 is not set in the FADT, it is generally
1515 * risky to do any special firmware-related preparations for entering
1516 * S0ix even though the system is suspending to idle, so return false
1519 if (!(acpi_gbl_FADT.flags & ACPI_FADT_LOW_POWER_S0)) {
1520 dev_err_once(adev->dev,
1521 "Power consumption will be higher as BIOS has not been configured for suspend-to-idle.\n"
1522 "To use suspend-to-idle change the sleep mode in BIOS setup.\n");
1526 #if !IS_ENABLED(CONFIG_AMD_PMC)
1527 dev_err_once(adev->dev,
1528 "Power consumption will be higher as the kernel has not been compiled with CONFIG_AMD_PMC.\n");
1532 #endif /* CONFIG_AMD_PMC */
1536 * amdgpu_choose_low_power_state
1538 * @adev: amdgpu_device_pointer
1540 * Choose the target low power state for the GPU
1542 void amdgpu_choose_low_power_state(struct amdgpu_device *adev)
1547 if (amdgpu_acpi_is_s0ix_active(adev))
1548 adev->in_s0ix = true;
1549 else if (amdgpu_acpi_is_s3_active(adev))
1553 #endif /* CONFIG_SUSPEND */