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");
808 * amdgpu_acpi_smart_shift_update - update dGPU device state to SBIOS
810 * @dev: drm_device pointer
811 * @ss_state: current smart shift event
813 * returns 0 on success,
814 * otherwise return error number.
816 int amdgpu_acpi_smart_shift_update(struct drm_device *dev, enum amdgpu_ss ss_state)
818 struct amdgpu_device *adev = drm_to_adev(dev);
821 if (!amdgpu_device_supports_smart_shift(dev))
825 /* SBIOS trigger “stop”, “enable” and “start” at D0, Driver Operational.
826 * SBIOS trigger “stop” at D3, Driver Not Operational.
827 * SBIOS trigger “stop” and “disable” at D0, Driver NOT operational.
829 case AMDGPU_SS_DRV_LOAD:
830 r = amdgpu_acpi_power_shift_control(adev,
831 AMDGPU_ATCS_PSC_DEV_STATE_D0,
832 AMDGPU_ATCS_PSC_DRV_STATE_OPR);
834 case AMDGPU_SS_DEV_D0:
835 r = amdgpu_acpi_power_shift_control(adev,
836 AMDGPU_ATCS_PSC_DEV_STATE_D0,
837 AMDGPU_ATCS_PSC_DRV_STATE_OPR);
839 case AMDGPU_SS_DEV_D3:
840 r = amdgpu_acpi_power_shift_control(adev,
841 AMDGPU_ATCS_PSC_DEV_STATE_D3_HOT,
842 AMDGPU_ATCS_PSC_DRV_STATE_NOT_OPR);
844 case AMDGPU_SS_DRV_UNLOAD:
845 r = amdgpu_acpi_power_shift_control(adev,
846 AMDGPU_ATCS_PSC_DEV_STATE_D0,
847 AMDGPU_ATCS_PSC_DRV_STATE_NOT_OPR);
856 #ifdef CONFIG_ACPI_NUMA
857 static inline uint64_t amdgpu_acpi_get_numa_size(int nid)
859 /* This is directly using si_meminfo_node implementation as the
860 * function is not exported.
863 uint64_t managed_pages = 0;
865 pg_data_t *pgdat = NODE_DATA(nid);
867 for (zone_type = 0; zone_type < MAX_NR_ZONES; zone_type++)
869 zone_managed_pages(&pgdat->node_zones[zone_type]);
870 return managed_pages * PAGE_SIZE;
873 static struct amdgpu_numa_info *amdgpu_acpi_get_numa_info(uint32_t pxm)
875 struct amdgpu_numa_info *numa_info;
878 numa_info = xa_load(&numa_info_xa, pxm);
883 numa_info = kzalloc(sizeof(*numa_info), GFP_KERNEL);
887 nid = pxm_to_node(pxm);
888 numa_info->pxm = pxm;
889 numa_info->nid = nid;
891 if (numa_info->nid == NUMA_NO_NODE) {
893 numa_info->size = info.totalram * info.mem_unit;
895 numa_info->size = amdgpu_acpi_get_numa_size(nid);
897 xa_store(&numa_info_xa, numa_info->pxm, numa_info, GFP_KERNEL);
905 * amdgpu_acpi_get_node_id - obtain the NUMA node id for corresponding amdgpu
908 * @handle: acpi handle
909 * @numa_info: amdgpu_numa_info structure holding numa information
911 * Queries the ACPI interface to fetch the corresponding NUMA Node ID for a
912 * given amdgpu acpi device.
914 * Returns ACPI STATUS OK with Node ID on success or the corresponding failure reason
916 static acpi_status amdgpu_acpi_get_node_id(acpi_handle handle,
917 struct amdgpu_numa_info **numa_info)
919 #ifdef CONFIG_ACPI_NUMA
924 return_ACPI_STATUS(AE_ERROR);
926 status = acpi_evaluate_integer(handle, "_PXM", NULL, &pxm);
928 if (ACPI_FAILURE(status))
931 *numa_info = amdgpu_acpi_get_numa_info(pxm);
934 return_ACPI_STATUS(AE_ERROR);
936 return_ACPI_STATUS(AE_OK);
938 return_ACPI_STATUS(AE_NOT_EXIST);
942 static struct amdgpu_acpi_dev_info *amdgpu_acpi_get_dev(u32 sbdf)
944 struct amdgpu_acpi_dev_info *acpi_dev;
946 if (list_empty(&amdgpu_acpi_dev_list))
949 list_for_each_entry(acpi_dev, &amdgpu_acpi_dev_list, list)
950 if (acpi_dev->sbdf == sbdf)
956 static int amdgpu_acpi_dev_init(struct amdgpu_acpi_dev_info **dev_info,
957 struct amdgpu_acpi_xcc_info *xcc_info, u32 sbdf)
959 struct amdgpu_acpi_dev_info *tmp;
960 union acpi_object *obj;
964 tmp = kzalloc(sizeof(struct amdgpu_acpi_dev_info), GFP_KERNEL);
968 INIT_LIST_HEAD(&tmp->xcc_list);
969 INIT_LIST_HEAD(&tmp->list);
972 obj = acpi_evaluate_dsm_typed(xcc_info->handle, &amd_xcc_dsm_guid, 0,
973 AMD_XCC_DSM_GET_SUPP_MODE, NULL,
977 acpi_handle_debug(xcc_info->handle,
978 "_DSM function %d evaluation failed",
979 AMD_XCC_DSM_GET_SUPP_MODE);
984 tmp->supp_xcp_mode = obj->integer.value & 0xFFFF;
987 obj = acpi_evaluate_dsm_typed(xcc_info->handle, &amd_xcc_dsm_guid, 0,
988 AMD_XCC_DSM_GET_XCP_MODE, NULL,
992 acpi_handle_debug(xcc_info->handle,
993 "_DSM function %d evaluation failed",
994 AMD_XCC_DSM_GET_XCP_MODE);
999 tmp->xcp_mode = obj->integer.value & 0xFFFF;
1000 tmp->mem_mode = (obj->integer.value >> 32) & 0xFFFF;
1003 /* Evaluate DSMs and fill XCC information */
1004 obj = acpi_evaluate_dsm_typed(xcc_info->handle, &amd_xcc_dsm_guid, 0,
1005 AMD_XCC_DSM_GET_TMR_INFO, NULL,
1008 if (!obj || obj->package.count < 2) {
1009 acpi_handle_debug(xcc_info->handle,
1010 "_DSM function %d evaluation failed",
1011 AMD_XCC_DSM_GET_TMR_INFO);
1016 tmp->tmr_base = obj->package.elements[0].integer.value;
1017 tmp->tmr_size = obj->package.elements[1].integer.value;
1021 "New dev(%x): Supported xcp mode: %x curr xcp_mode : %x mem mode : %x, tmr base: %llx tmr size: %llx ",
1022 tmp->sbdf, tmp->supp_xcp_mode, tmp->xcp_mode, tmp->mem_mode,
1023 tmp->tmr_base, tmp->tmr_size);
1024 list_add_tail(&tmp->list, &amdgpu_acpi_dev_list);
1037 static int amdgpu_acpi_get_xcc_info(struct amdgpu_acpi_xcc_info *xcc_info,
1040 union acpi_object *obj;
1044 obj = acpi_evaluate_dsm_typed(xcc_info->handle, &amd_xcc_dsm_guid, 0,
1045 AMD_XCC_DSM_GET_NUM_FUNCS, NULL,
1048 if (!obj || obj->integer.value != AMD_XCC_DSM_NUM_FUNCS)
1052 /* Evaluate DSMs and fill XCC information */
1053 obj = acpi_evaluate_dsm_typed(xcc_info->handle, &amd_xcc_dsm_guid, 0,
1054 AMD_XCC_DSM_GET_VF_XCC_MAPPING, NULL,
1058 acpi_handle_debug(xcc_info->handle,
1059 "_DSM function %d evaluation failed",
1060 AMD_XCC_DSM_GET_VF_XCC_MAPPING);
1065 /* PF xcc id [39:32] */
1066 xcc_info->phy_id = (obj->integer.value >> 32) & 0xFF;
1067 /* xcp node of this xcc [47:40] */
1068 xcc_info->xcp_node = (obj->integer.value >> 40) & 0xFF;
1069 /* PF domain of this xcc [31:16] */
1070 *sbdf = (obj->integer.value) & 0xFFFF0000;
1071 /* PF bus/dev/fn of this xcc [63:48] */
1072 *sbdf |= (obj->integer.value >> 48) & 0xFFFF;
1077 amdgpu_acpi_get_node_id(xcc_info->handle, &xcc_info->numa_info);
1079 /* TODO: check if this check is required */
1080 if (ACPI_SUCCESS(status))
1089 static int amdgpu_acpi_enumerate_xcc(void)
1091 struct amdgpu_acpi_dev_info *dev_info = NULL;
1092 struct amdgpu_acpi_xcc_info *xcc_info;
1093 struct acpi_device *acpi_dev;
1094 char hid[ACPI_ID_LEN];
1098 INIT_LIST_HEAD(&amdgpu_acpi_dev_list);
1099 xa_init(&numa_info_xa);
1101 for (id = 0; id < AMD_XCC_MAX_HID; id++) {
1102 sprintf(hid, "%s%d", "AMD", AMD_XCC_HID_START + id);
1103 acpi_dev = acpi_dev_get_first_match_dev(hid, NULL, -1);
1104 /* These ACPI objects are expected to be in sequential order. If
1105 * one is not found, no need to check the rest.
1108 DRM_DEBUG_DRIVER("No matching acpi device found for %s",
1113 xcc_info = kzalloc(sizeof(struct amdgpu_acpi_xcc_info),
1116 DRM_ERROR("Failed to allocate memory for xcc info\n");
1120 INIT_LIST_HEAD(&xcc_info->list);
1121 xcc_info->handle = acpi_device_handle(acpi_dev);
1122 acpi_dev_put(acpi_dev);
1124 ret = amdgpu_acpi_get_xcc_info(xcc_info, &sbdf);
1130 dev_info = amdgpu_acpi_get_dev(sbdf);
1133 ret = amdgpu_acpi_dev_init(&dev_info, xcc_info, sbdf);
1143 list_add_tail(&xcc_info->list, &dev_info->xcc_list);
1149 int amdgpu_acpi_get_tmr_info(struct amdgpu_device *adev, u64 *tmr_offset,
1152 struct amdgpu_acpi_dev_info *dev_info;
1155 if (!tmr_offset || !tmr_size)
1158 sbdf = (pci_domain_nr(adev->pdev->bus) << 16);
1159 sbdf |= pci_dev_id(adev->pdev);
1160 dev_info = amdgpu_acpi_get_dev(sbdf);
1164 *tmr_offset = dev_info->tmr_base;
1165 *tmr_size = dev_info->tmr_size;
1170 int amdgpu_acpi_get_mem_info(struct amdgpu_device *adev, int xcc_id,
1171 struct amdgpu_numa_info *numa_info)
1173 struct amdgpu_acpi_dev_info *dev_info;
1174 struct amdgpu_acpi_xcc_info *xcc_info;
1180 sbdf = (pci_domain_nr(adev->pdev->bus) << 16);
1181 sbdf |= pci_dev_id(adev->pdev);
1182 dev_info = amdgpu_acpi_get_dev(sbdf);
1186 list_for_each_entry(xcc_info, &dev_info->xcc_list, list) {
1187 if (xcc_info->phy_id == xcc_id) {
1188 memcpy(numa_info, xcc_info->numa_info,
1189 sizeof(*numa_info));
1198 * amdgpu_acpi_event - handle notify events
1200 * @nb: notifier block
1204 * Calls relevant amdgpu functions in response to various
1206 * Returns NOTIFY code
1208 static int amdgpu_acpi_event(struct notifier_block *nb,
1212 struct amdgpu_device *adev = container_of(nb, struct amdgpu_device, acpi_nb);
1213 struct acpi_bus_event *entry = (struct acpi_bus_event *)data;
1215 if (strcmp(entry->device_class, ACPI_AC_CLASS) == 0) {
1216 if (power_supply_is_system_supplied() > 0)
1217 DRM_DEBUG_DRIVER("pm: AC\n");
1219 DRM_DEBUG_DRIVER("pm: DC\n");
1221 amdgpu_pm_acpi_event_handler(adev);
1224 /* Check for pending SBIOS requests */
1225 return amdgpu_atif_handler(adev, entry);
1228 /* Call all ACPI methods here */
1230 * amdgpu_acpi_init - init driver acpi support
1232 * @adev: amdgpu_device pointer
1234 * Verifies the AMD ACPI interfaces and registers with the acpi
1235 * notifier chain (all asics).
1236 * Returns 0 on success, error on failure.
1238 int amdgpu_acpi_init(struct amdgpu_device *adev)
1240 struct amdgpu_atif *atif = &amdgpu_acpi_priv.atif;
1242 if (atif->notifications.brightness_change) {
1243 if (adev->dc_enabled) {
1244 #if defined(CONFIG_DRM_AMD_DC)
1245 struct amdgpu_display_manager *dm = &adev->dm;
1247 if (dm->backlight_dev[0])
1248 atif->bd = dm->backlight_dev[0];
1251 struct drm_encoder *tmp;
1253 /* Find the encoder controlling the brightness */
1254 list_for_each_entry(tmp, &adev_to_drm(adev)->mode_config.encoder_list,
1256 struct amdgpu_encoder *enc = to_amdgpu_encoder(tmp);
1258 if ((enc->devices & (ATOM_DEVICE_LCD_SUPPORT)) &&
1260 struct amdgpu_encoder_atom_dig *dig = enc->enc_priv;
1263 atif->bd = dig->bl_dev;
1270 adev->acpi_nb.notifier_call = amdgpu_acpi_event;
1271 register_acpi_notifier(&adev->acpi_nb);
1276 void amdgpu_acpi_get_backlight_caps(struct amdgpu_dm_backlight_caps *caps)
1278 struct amdgpu_atif *atif = &amdgpu_acpi_priv.atif;
1280 caps->caps_valid = atif->backlight_caps.caps_valid;
1281 caps->min_input_signal = atif->backlight_caps.min_input_signal;
1282 caps->max_input_signal = atif->backlight_caps.max_input_signal;
1283 caps->ac_level = atif->backlight_caps.ac_level;
1284 caps->dc_level = atif->backlight_caps.dc_level;
1288 * amdgpu_acpi_fini - tear down driver acpi support
1290 * @adev: amdgpu_device pointer
1292 * Unregisters with the acpi notifier chain (all asics).
1294 void amdgpu_acpi_fini(struct amdgpu_device *adev)
1296 unregister_acpi_notifier(&adev->acpi_nb);
1300 * amdgpu_atif_pci_probe_handle - look up the ATIF handle
1304 * Look up the ATIF handles (all asics).
1305 * Returns true if the handle is found, false if not.
1307 static bool amdgpu_atif_pci_probe_handle(struct pci_dev *pdev)
1309 char acpi_method_name[255] = { 0 };
1310 struct acpi_buffer buffer = {sizeof(acpi_method_name), acpi_method_name};
1311 acpi_handle dhandle, atif_handle;
1315 dhandle = ACPI_HANDLE(&pdev->dev);
1319 status = acpi_get_handle(dhandle, "ATIF", &atif_handle);
1320 if (ACPI_FAILURE(status))
1323 amdgpu_acpi_priv.atif.handle = atif_handle;
1324 acpi_get_name(amdgpu_acpi_priv.atif.handle, ACPI_FULL_PATHNAME, &buffer);
1325 DRM_DEBUG_DRIVER("Found ATIF handle %s\n", acpi_method_name);
1326 ret = amdgpu_atif_verify_interface(&amdgpu_acpi_priv.atif);
1328 amdgpu_acpi_priv.atif.handle = 0;
1335 * amdgpu_atcs_pci_probe_handle - look up the ATCS handle
1339 * Look up the ATCS handles (all asics).
1340 * Returns true if the handle is found, false if not.
1342 static bool amdgpu_atcs_pci_probe_handle(struct pci_dev *pdev)
1344 char acpi_method_name[255] = { 0 };
1345 struct acpi_buffer buffer = { sizeof(acpi_method_name), acpi_method_name };
1346 acpi_handle dhandle, atcs_handle;
1350 dhandle = ACPI_HANDLE(&pdev->dev);
1354 status = acpi_get_handle(dhandle, "ATCS", &atcs_handle);
1355 if (ACPI_FAILURE(status))
1358 amdgpu_acpi_priv.atcs.handle = atcs_handle;
1359 acpi_get_name(amdgpu_acpi_priv.atcs.handle, ACPI_FULL_PATHNAME, &buffer);
1360 DRM_DEBUG_DRIVER("Found ATCS handle %s\n", acpi_method_name);
1361 ret = amdgpu_atcs_verify_interface(&amdgpu_acpi_priv.atcs);
1363 amdgpu_acpi_priv.atcs.handle = 0;
1371 * amdgpu_acpi_should_gpu_reset
1373 * @adev: amdgpu_device_pointer
1375 * returns true if should reset GPU, false if not
1377 bool amdgpu_acpi_should_gpu_reset(struct amdgpu_device *adev)
1379 if ((adev->flags & AMD_IS_APU) &&
1380 adev->gfx.imu.funcs) /* Not need to do mode2 reset for IMU enabled APUs */
1383 if ((adev->flags & AMD_IS_APU) &&
1384 amdgpu_acpi_is_s3_active(adev))
1387 if (amdgpu_sriov_vf(adev))
1390 #if IS_ENABLED(CONFIG_SUSPEND)
1391 return pm_suspend_target_state != PM_SUSPEND_TO_IDLE;
1398 * amdgpu_acpi_detect - detect ACPI ATIF/ATCS methods
1400 * Check if we have the ATIF/ATCS methods and populate
1401 * the structures in the driver.
1403 void amdgpu_acpi_detect(void)
1405 struct amdgpu_atif *atif = &amdgpu_acpi_priv.atif;
1406 struct amdgpu_atcs *atcs = &amdgpu_acpi_priv.atcs;
1407 struct pci_dev *pdev = NULL;
1410 while ((pdev = pci_get_base_class(PCI_BASE_CLASS_DISPLAY, pdev))) {
1411 if ((pdev->class != PCI_CLASS_DISPLAY_VGA << 8) &&
1412 (pdev->class != PCI_CLASS_DISPLAY_OTHER << 8))
1416 amdgpu_atif_pci_probe_handle(pdev);
1418 amdgpu_atcs_pci_probe_handle(pdev);
1421 if (atif->functions.sbios_requests && !atif->functions.system_params) {
1422 /* XXX check this workraround, if sbios request function is
1423 * present we have to see how it's configured in the system
1426 atif->functions.system_params = true;
1429 if (atif->functions.system_params) {
1430 ret = amdgpu_atif_get_notification_params(atif);
1432 DRM_DEBUG_DRIVER("Call to GET_SYSTEM_PARAMS failed: %d\n",
1434 /* Disable notification */
1435 atif->notification_cfg.enabled = false;
1439 if (atif->functions.query_backlight_transfer_characteristics) {
1440 ret = amdgpu_atif_query_backlight_caps(atif);
1442 DRM_DEBUG_DRIVER("Call to QUERY_BACKLIGHT_TRANSFER_CHARACTERISTICS failed: %d\n",
1444 atif->backlight_caps.caps_valid = false;
1447 atif->backlight_caps.caps_valid = false;
1450 amdgpu_acpi_enumerate_xcc();
1453 void amdgpu_acpi_release(void)
1455 struct amdgpu_acpi_dev_info *dev_info, *dev_tmp;
1456 struct amdgpu_acpi_xcc_info *xcc_info, *xcc_tmp;
1457 struct amdgpu_numa_info *numa_info;
1458 unsigned long index;
1460 xa_for_each(&numa_info_xa, index, numa_info) {
1462 xa_erase(&numa_info_xa, index);
1465 if (list_empty(&amdgpu_acpi_dev_list))
1468 list_for_each_entry_safe(dev_info, dev_tmp, &amdgpu_acpi_dev_list,
1470 list_for_each_entry_safe(xcc_info, xcc_tmp, &dev_info->xcc_list,
1472 list_del(&xcc_info->list);
1476 list_del(&dev_info->list);
1481 #if IS_ENABLED(CONFIG_SUSPEND)
1483 * amdgpu_acpi_is_s3_active
1485 * @adev: amdgpu_device_pointer
1487 * returns true if supported, false if not.
1489 bool amdgpu_acpi_is_s3_active(struct amdgpu_device *adev)
1491 return !(adev->flags & AMD_IS_APU) ||
1492 (pm_suspend_target_state == PM_SUSPEND_MEM);
1496 * amdgpu_acpi_is_s0ix_active
1498 * @adev: amdgpu_device_pointer
1500 * returns true if supported, false if not.
1502 bool amdgpu_acpi_is_s0ix_active(struct amdgpu_device *adev)
1504 if (!(adev->flags & AMD_IS_APU) ||
1505 (pm_suspend_target_state != PM_SUSPEND_TO_IDLE))
1508 if (adev->asic_type < CHIP_RAVEN)
1511 if (!(adev->pm.pp_feature & PP_GFXOFF_MASK))
1515 * If ACPI_FADT_LOW_POWER_S0 is not set in the FADT, it is generally
1516 * risky to do any special firmware-related preparations for entering
1517 * S0ix even though the system is suspending to idle, so return false
1520 if (!(acpi_gbl_FADT.flags & ACPI_FADT_LOW_POWER_S0)) {
1521 dev_err_once(adev->dev,
1522 "Power consumption will be higher as BIOS has not been configured for suspend-to-idle.\n"
1523 "To use suspend-to-idle change the sleep mode in BIOS setup.\n");
1527 #if !IS_ENABLED(CONFIG_AMD_PMC)
1528 dev_err_once(adev->dev,
1529 "Power consumption will be higher as the kernel has not been compiled with CONFIG_AMD_PMC.\n");
1533 #endif /* CONFIG_AMD_PMC */
1537 * amdgpu_choose_low_power_state
1539 * @adev: amdgpu_device_pointer
1541 * Choose the target low power state for the GPU
1543 void amdgpu_choose_low_power_state(struct amdgpu_device *adev)
1548 if (amdgpu_acpi_is_s0ix_active(adev))
1549 adev->in_s0ix = true;
1550 else if (amdgpu_acpi_is_s3_active(adev))
1554 #endif /* CONFIG_SUSPEND */