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 atif_arg_elements[2];
151 struct acpi_object_list atif_arg;
152 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
155 atif_arg.pointer = &atif_arg_elements[0];
157 atif_arg_elements[0].type = ACPI_TYPE_INTEGER;
158 atif_arg_elements[0].integer.value = function;
161 atif_arg_elements[1].type = ACPI_TYPE_BUFFER;
162 atif_arg_elements[1].buffer.length = params->length;
163 atif_arg_elements[1].buffer.pointer = params->pointer;
165 /* We need a second fake parameter */
166 atif_arg_elements[1].type = ACPI_TYPE_INTEGER;
167 atif_arg_elements[1].integer.value = 0;
170 status = acpi_evaluate_object(atif->handle, NULL, &atif_arg,
173 /* Fail only if calling the method fails and ATIF is supported */
174 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
175 DRM_DEBUG_DRIVER("failed to evaluate ATIF got %s\n",
176 acpi_format_exception(status));
177 kfree(buffer.pointer);
181 return buffer.pointer;
185 * amdgpu_atif_parse_notification - parse supported notifications
187 * @n: supported notifications struct
188 * @mask: supported notifications mask from ATIF
190 * Use the supported notifications mask from ATIF function
191 * ATIF_FUNCTION_VERIFY_INTERFACE to determine what notifications
192 * are supported (all asics).
194 static void amdgpu_atif_parse_notification(struct amdgpu_atif_notifications *n, u32 mask)
196 n->thermal_state = mask & ATIF_THERMAL_STATE_CHANGE_REQUEST_SUPPORTED;
197 n->forced_power_state = mask & ATIF_FORCED_POWER_STATE_CHANGE_REQUEST_SUPPORTED;
198 n->system_power_state = mask & ATIF_SYSTEM_POWER_SOURCE_CHANGE_REQUEST_SUPPORTED;
199 n->brightness_change = mask & ATIF_PANEL_BRIGHTNESS_CHANGE_REQUEST_SUPPORTED;
200 n->dgpu_display_event = mask & ATIF_DGPU_DISPLAY_EVENT_SUPPORTED;
201 n->gpu_package_power_limit = mask & ATIF_GPU_PACKAGE_POWER_LIMIT_REQUEST_SUPPORTED;
205 * amdgpu_atif_parse_functions - parse supported functions
207 * @f: supported functions struct
208 * @mask: supported functions mask from ATIF
210 * Use the supported functions mask from ATIF function
211 * ATIF_FUNCTION_VERIFY_INTERFACE to determine what functions
212 * are supported (all asics).
214 static void amdgpu_atif_parse_functions(struct amdgpu_atif_functions *f, u32 mask)
216 f->system_params = mask & ATIF_GET_SYSTEM_PARAMETERS_SUPPORTED;
217 f->sbios_requests = mask & ATIF_GET_SYSTEM_BIOS_REQUESTS_SUPPORTED;
218 f->temperature_change = mask & ATIF_TEMPERATURE_CHANGE_NOTIFICATION_SUPPORTED;
219 f->query_backlight_transfer_characteristics =
220 mask & ATIF_QUERY_BACKLIGHT_TRANSFER_CHARACTERISTICS_SUPPORTED;
221 f->ready_to_undock = mask & ATIF_READY_TO_UNDOCK_NOTIFICATION_SUPPORTED;
222 f->external_gpu_information = mask & ATIF_GET_EXTERNAL_GPU_INFORMATION_SUPPORTED;
226 * amdgpu_atif_verify_interface - verify ATIF
228 * @atif: amdgpu atif struct
230 * Execute the ATIF_FUNCTION_VERIFY_INTERFACE ATIF function
231 * to initialize ATIF and determine what features are supported
233 * returns 0 on success, error on failure.
235 static int amdgpu_atif_verify_interface(struct amdgpu_atif *atif)
237 union acpi_object *info;
238 struct atif_verify_interface output;
242 info = amdgpu_atif_call(atif, ATIF_FUNCTION_VERIFY_INTERFACE, NULL);
246 memset(&output, 0, sizeof(output));
248 size = *(u16 *) info->buffer.pointer;
250 DRM_INFO("ATIF buffer is too small: %zu\n", size);
254 size = min(sizeof(output), size);
256 memcpy(&output, info->buffer.pointer, size);
258 /* TODO: check version? */
259 DRM_DEBUG_DRIVER("ATIF version %u\n", output.version);
261 amdgpu_atif_parse_notification(&atif->notifications, output.notification_mask);
262 amdgpu_atif_parse_functions(&atif->functions, output.function_bits);
270 * amdgpu_atif_get_notification_params - determine notify configuration
274 * Execute the ATIF_FUNCTION_GET_SYSTEM_PARAMETERS ATIF function
275 * to determine if a notifier is used and if so which one
276 * (all asics). This is either Notify(VGA, 0x81) or Notify(VGA, n)
277 * where n is specified in the result if a notifier is used.
278 * Returns 0 on success, error on failure.
280 static int amdgpu_atif_get_notification_params(struct amdgpu_atif *atif)
282 union acpi_object *info;
283 struct amdgpu_atif_notification_cfg *n = &atif->notification_cfg;
284 struct atif_system_params params;
288 info = amdgpu_atif_call(atif, ATIF_FUNCTION_GET_SYSTEM_PARAMETERS,
295 size = *(u16 *) info->buffer.pointer;
301 memset(¶ms, 0, sizeof(params));
302 size = min(sizeof(params), size);
303 memcpy(¶ms, info->buffer.pointer, size);
305 DRM_DEBUG_DRIVER("SYSTEM_PARAMS: mask = %#x, flags = %#x\n",
306 params.flags, params.valid_mask);
307 params.flags = params.flags & params.valid_mask;
309 if ((params.flags & ATIF_NOTIFY_MASK) == ATIF_NOTIFY_NONE) {
312 } else if ((params.flags & ATIF_NOTIFY_MASK) == ATIF_NOTIFY_81) {
314 n->command_code = 0x81;
321 n->command_code = params.command_code;
325 DRM_DEBUG_DRIVER("Notification %s, command code = %#x\n",
326 (n->enabled ? "enabled" : "disabled"),
333 * amdgpu_atif_query_backlight_caps - get min and max backlight input signal
337 * Execute the QUERY_BRIGHTNESS_TRANSFER_CHARACTERISTICS ATIF function
338 * to determine the acceptable range of backlight values
340 * Backlight_caps.caps_valid will be set to true if the query is successful
342 * The input signals are in range 0-255
344 * This function assumes the display with backlight is the first LCD
346 * Returns 0 on success, error on failure.
348 static int amdgpu_atif_query_backlight_caps(struct amdgpu_atif *atif)
350 union acpi_object *info;
351 struct atif_qbtc_output characteristics;
352 struct atif_qbtc_arguments arguments;
353 struct acpi_buffer params;
357 arguments.size = sizeof(arguments);
358 arguments.requested_display = ATIF_QBTC_REQUEST_LCD1;
360 params.length = sizeof(arguments);
361 params.pointer = (void *)&arguments;
363 info = amdgpu_atif_call(atif,
364 ATIF_FUNCTION_QUERY_BRIGHTNESS_TRANSFER_CHARACTERISTICS,
371 size = *(u16 *) info->buffer.pointer;
377 memset(&characteristics, 0, sizeof(characteristics));
378 size = min(sizeof(characteristics), size);
379 memcpy(&characteristics, info->buffer.pointer, size);
381 atif->backlight_caps.caps_valid = true;
382 atif->backlight_caps.min_input_signal =
383 characteristics.min_input_signal;
384 atif->backlight_caps.max_input_signal =
385 characteristics.max_input_signal;
392 * amdgpu_atif_get_sbios_requests - get requested sbios event
395 * @req: atif sbios request struct
397 * Execute the ATIF_FUNCTION_GET_SYSTEM_BIOS_REQUESTS ATIF function
398 * to determine what requests the sbios is making to the driver
400 * Returns 0 on success, error on failure.
402 static int amdgpu_atif_get_sbios_requests(struct amdgpu_atif *atif,
403 struct atif_sbios_requests *req)
405 union acpi_object *info;
409 info = amdgpu_atif_call(atif, ATIF_FUNCTION_GET_SYSTEM_BIOS_REQUESTS,
414 size = *(u16 *)info->buffer.pointer;
419 memset(req, 0, sizeof(*req));
421 size = min(sizeof(*req), size);
422 memcpy(req, info->buffer.pointer, size);
423 DRM_DEBUG_DRIVER("SBIOS pending requests: %#x\n", req->pending);
425 count = hweight32(req->pending);
433 * amdgpu_atif_handler - handle ATIF notify requests
435 * @adev: amdgpu_device pointer
436 * @event: atif sbios request struct
438 * Checks the acpi event and if it matches an atif event,
442 * NOTIFY_BAD or NOTIFY_DONE, depending on the event.
444 static int amdgpu_atif_handler(struct amdgpu_device *adev,
445 struct acpi_bus_event *event)
447 struct amdgpu_atif *atif = &amdgpu_acpi_priv.atif;
450 DRM_DEBUG_DRIVER("event, device_class = %s, type = %#x\n",
451 event->device_class, event->type);
453 if (strcmp(event->device_class, ACPI_VIDEO_CLASS) != 0)
456 /* Is this actually our event? */
457 if (!atif->notification_cfg.enabled ||
458 event->type != atif->notification_cfg.command_code) {
459 /* These events will generate keypresses otherwise */
460 if (event->type == ACPI_VIDEO_NOTIFY_PROBE)
466 if (atif->functions.sbios_requests) {
467 struct atif_sbios_requests req;
469 /* Check pending SBIOS requests */
470 count = amdgpu_atif_get_sbios_requests(atif, &req);
475 DRM_DEBUG_DRIVER("ATIF: %d pending SBIOS requests\n", count);
477 if (req.pending & ATIF_PANEL_BRIGHTNESS_CHANGE_REQUEST) {
479 DRM_DEBUG_DRIVER("Changing brightness to %d\n",
480 req.backlight_level);
482 * XXX backlight_device_set_brightness() is
483 * hardwired to post BACKLIGHT_UPDATE_SYSFS.
484 * It probably should accept 'reason' parameter.
486 backlight_device_set_brightness(atif->bd, req.backlight_level);
490 if (req.pending & ATIF_DGPU_DISPLAY_EVENT) {
491 if (adev->flags & AMD_IS_PX) {
492 pm_runtime_get_sync(adev_to_drm(adev)->dev);
493 /* Just fire off a uevent and let userspace tell us what to do */
494 drm_helper_hpd_irq_event(adev_to_drm(adev));
495 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
496 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
499 /* TODO: check other events */
502 /* We've handled the event, stop the notifier chain. The ACPI interface
503 * overloads ACPI_VIDEO_NOTIFY_PROBE, we don't want to send that to
504 * userspace if the event was generated only to signal a SBIOS
510 /* Call the ATCS method
513 * amdgpu_atcs_call - call an ATCS method
515 * @atcs: atcs structure
516 * @function: the ATCS function to execute
517 * @params: ATCS function params
519 * Executes the requested ATCS function (all asics).
520 * Returns a pointer to the acpi output buffer.
522 static union acpi_object *amdgpu_atcs_call(struct amdgpu_atcs *atcs,
524 struct acpi_buffer *params)
527 union acpi_object atcs_arg_elements[2];
528 struct acpi_object_list atcs_arg;
529 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
532 atcs_arg.pointer = &atcs_arg_elements[0];
534 atcs_arg_elements[0].type = ACPI_TYPE_INTEGER;
535 atcs_arg_elements[0].integer.value = function;
538 atcs_arg_elements[1].type = ACPI_TYPE_BUFFER;
539 atcs_arg_elements[1].buffer.length = params->length;
540 atcs_arg_elements[1].buffer.pointer = params->pointer;
542 /* We need a second fake parameter */
543 atcs_arg_elements[1].type = ACPI_TYPE_INTEGER;
544 atcs_arg_elements[1].integer.value = 0;
547 status = acpi_evaluate_object(atcs->handle, NULL, &atcs_arg, &buffer);
549 /* Fail only if calling the method fails and ATIF is supported */
550 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
551 DRM_DEBUG_DRIVER("failed to evaluate ATCS got %s\n",
552 acpi_format_exception(status));
553 kfree(buffer.pointer);
557 return buffer.pointer;
561 * amdgpu_atcs_parse_functions - parse supported functions
563 * @f: supported functions struct
564 * @mask: supported functions mask from ATCS
566 * Use the supported functions mask from ATCS function
567 * ATCS_FUNCTION_VERIFY_INTERFACE to determine what functions
568 * are supported (all asics).
570 static void amdgpu_atcs_parse_functions(struct amdgpu_atcs_functions *f, u32 mask)
572 f->get_ext_state = mask & ATCS_GET_EXTERNAL_STATE_SUPPORTED;
573 f->pcie_perf_req = mask & ATCS_PCIE_PERFORMANCE_REQUEST_SUPPORTED;
574 f->pcie_dev_rdy = mask & ATCS_PCIE_DEVICE_READY_NOTIFICATION_SUPPORTED;
575 f->pcie_bus_width = mask & ATCS_SET_PCIE_BUS_WIDTH_SUPPORTED;
576 f->power_shift_control = mask & ATCS_SET_POWER_SHIFT_CONTROL_SUPPORTED;
580 * amdgpu_atcs_verify_interface - verify ATCS
582 * @atcs: amdgpu atcs struct
584 * Execute the ATCS_FUNCTION_VERIFY_INTERFACE ATCS function
585 * to initialize ATCS and determine what features are supported
587 * returns 0 on success, error on failure.
589 static int amdgpu_atcs_verify_interface(struct amdgpu_atcs *atcs)
591 union acpi_object *info;
592 struct atcs_verify_interface output;
596 info = amdgpu_atcs_call(atcs, ATCS_FUNCTION_VERIFY_INTERFACE, NULL);
600 memset(&output, 0, sizeof(output));
602 size = *(u16 *) info->buffer.pointer;
604 DRM_INFO("ATCS buffer is too small: %zu\n", size);
608 size = min(sizeof(output), size);
610 memcpy(&output, info->buffer.pointer, size);
612 /* TODO: check version? */
613 DRM_DEBUG_DRIVER("ATCS version %u\n", output.version);
615 amdgpu_atcs_parse_functions(&atcs->functions, output.function_bits);
623 * amdgpu_acpi_is_pcie_performance_request_supported
625 * @adev: amdgpu_device pointer
627 * Check if the ATCS pcie_perf_req and pcie_dev_rdy methods
628 * are supported (all asics).
629 * returns true if supported, false if not.
631 bool amdgpu_acpi_is_pcie_performance_request_supported(struct amdgpu_device *adev)
633 struct amdgpu_atcs *atcs = &amdgpu_acpi_priv.atcs;
635 if (atcs->functions.pcie_perf_req && atcs->functions.pcie_dev_rdy)
642 * amdgpu_acpi_is_power_shift_control_supported
644 * Check if the ATCS power shift control method
646 * returns true if supported, false if not.
648 bool amdgpu_acpi_is_power_shift_control_supported(void)
650 return amdgpu_acpi_priv.atcs.functions.power_shift_control;
654 * amdgpu_acpi_pcie_notify_device_ready
656 * @adev: amdgpu_device pointer
658 * Executes the PCIE_DEVICE_READY_NOTIFICATION method
660 * returns 0 on success, error on failure.
662 int amdgpu_acpi_pcie_notify_device_ready(struct amdgpu_device *adev)
664 union acpi_object *info;
665 struct amdgpu_atcs *atcs = &amdgpu_acpi_priv.atcs;
667 if (!atcs->functions.pcie_dev_rdy)
670 info = amdgpu_atcs_call(atcs, ATCS_FUNCTION_PCIE_DEVICE_READY_NOTIFICATION, NULL);
680 * amdgpu_acpi_pcie_performance_request
682 * @adev: amdgpu_device pointer
683 * @perf_req: requested perf level (pcie gen speed)
684 * @advertise: set advertise caps flag if set
686 * Executes the PCIE_PERFORMANCE_REQUEST method to
687 * change the pcie gen speed (all asics).
688 * returns 0 on success, error on failure.
690 int amdgpu_acpi_pcie_performance_request(struct amdgpu_device *adev,
691 u8 perf_req, bool advertise)
693 union acpi_object *info;
694 struct amdgpu_atcs *atcs = &amdgpu_acpi_priv.atcs;
695 struct atcs_pref_req_input atcs_input;
696 struct atcs_pref_req_output atcs_output;
697 struct acpi_buffer params;
701 if (amdgpu_acpi_pcie_notify_device_ready(adev))
704 if (!atcs->functions.pcie_perf_req)
707 atcs_input.size = sizeof(struct atcs_pref_req_input);
708 /* client id (bit 2-0: func num, 7-3: dev num, 15-8: bus num) */
709 atcs_input.client_id = adev->pdev->devfn | (adev->pdev->bus->number << 8);
710 atcs_input.valid_flags_mask = ATCS_VALID_FLAGS_MASK;
711 atcs_input.flags = ATCS_WAIT_FOR_COMPLETION;
713 atcs_input.flags |= ATCS_ADVERTISE_CAPS;
714 atcs_input.req_type = ATCS_PCIE_LINK_SPEED;
715 atcs_input.perf_req = perf_req;
717 params.length = sizeof(struct atcs_pref_req_input);
718 params.pointer = &atcs_input;
721 info = amdgpu_atcs_call(atcs, ATCS_FUNCTION_PCIE_PERFORMANCE_REQUEST, ¶ms);
725 memset(&atcs_output, 0, sizeof(atcs_output));
727 size = *(u16 *) info->buffer.pointer;
729 DRM_INFO("ATCS buffer is too small: %zu\n", size);
733 size = min(sizeof(atcs_output), size);
735 memcpy(&atcs_output, info->buffer.pointer, size);
739 switch (atcs_output.ret_val) {
740 case ATCS_REQUEST_REFUSED:
743 case ATCS_REQUEST_COMPLETE:
745 case ATCS_REQUEST_IN_PROGRESS:
755 * amdgpu_acpi_power_shift_control
757 * @adev: amdgpu_device pointer
758 * @dev_state: device acpi state
759 * @drv_state: driver state
761 * Executes the POWER_SHIFT_CONTROL method to
762 * communicate current dGPU device state and
763 * driver state to APU/SBIOS.
764 * returns 0 on success, error on failure.
766 int amdgpu_acpi_power_shift_control(struct amdgpu_device *adev,
767 u8 dev_state, bool drv_state)
769 union acpi_object *info;
770 struct amdgpu_atcs *atcs = &amdgpu_acpi_priv.atcs;
771 struct atcs_pwr_shift_input atcs_input;
772 struct acpi_buffer params;
774 if (!amdgpu_acpi_is_power_shift_control_supported())
777 atcs_input.size = sizeof(struct atcs_pwr_shift_input);
778 /* dGPU id (bit 2-0: func num, 7-3: dev num, 15-8: bus num) */
779 atcs_input.dgpu_id = adev->pdev->devfn | (adev->pdev->bus->number << 8);
780 atcs_input.dev_acpi_state = dev_state;
781 atcs_input.drv_state = drv_state;
783 params.length = sizeof(struct atcs_pwr_shift_input);
784 params.pointer = &atcs_input;
786 info = amdgpu_atcs_call(atcs, ATCS_FUNCTION_POWER_SHIFT_CONTROL, ¶ms);
788 DRM_ERROR("ATCS PSC update failed\n");
796 * amdgpu_acpi_smart_shift_update - update dGPU device state to SBIOS
798 * @dev: drm_device pointer
799 * @ss_state: current smart shift event
801 * returns 0 on success,
802 * otherwise return error number.
804 int amdgpu_acpi_smart_shift_update(struct drm_device *dev, enum amdgpu_ss ss_state)
806 struct amdgpu_device *adev = drm_to_adev(dev);
809 if (!amdgpu_device_supports_smart_shift(dev))
813 /* SBIOS trigger “stop”, “enable” and “start” at D0, Driver Operational.
814 * SBIOS trigger “stop” at D3, Driver Not Operational.
815 * SBIOS trigger “stop” and “disable” at D0, Driver NOT operational.
817 case AMDGPU_SS_DRV_LOAD:
818 r = amdgpu_acpi_power_shift_control(adev,
819 AMDGPU_ATCS_PSC_DEV_STATE_D0,
820 AMDGPU_ATCS_PSC_DRV_STATE_OPR);
822 case AMDGPU_SS_DEV_D0:
823 r = amdgpu_acpi_power_shift_control(adev,
824 AMDGPU_ATCS_PSC_DEV_STATE_D0,
825 AMDGPU_ATCS_PSC_DRV_STATE_OPR);
827 case AMDGPU_SS_DEV_D3:
828 r = amdgpu_acpi_power_shift_control(adev,
829 AMDGPU_ATCS_PSC_DEV_STATE_D3_HOT,
830 AMDGPU_ATCS_PSC_DRV_STATE_NOT_OPR);
832 case AMDGPU_SS_DRV_UNLOAD:
833 r = amdgpu_acpi_power_shift_control(adev,
834 AMDGPU_ATCS_PSC_DEV_STATE_D0,
835 AMDGPU_ATCS_PSC_DRV_STATE_NOT_OPR);
844 #ifdef CONFIG_ACPI_NUMA
845 static inline uint64_t amdgpu_acpi_get_numa_size(int nid)
847 /* This is directly using si_meminfo_node implementation as the
848 * function is not exported.
851 uint64_t managed_pages = 0;
853 pg_data_t *pgdat = NODE_DATA(nid);
855 for (zone_type = 0; zone_type < MAX_NR_ZONES; zone_type++)
857 zone_managed_pages(&pgdat->node_zones[zone_type]);
858 return managed_pages * PAGE_SIZE;
861 static struct amdgpu_numa_info *amdgpu_acpi_get_numa_info(uint32_t pxm)
863 struct amdgpu_numa_info *numa_info;
866 numa_info = xa_load(&numa_info_xa, pxm);
871 numa_info = kzalloc(sizeof *numa_info, GFP_KERNEL);
875 nid = pxm_to_node(pxm);
876 numa_info->pxm = pxm;
877 numa_info->nid = nid;
879 if (numa_info->nid == NUMA_NO_NODE) {
881 numa_info->size = info.totalram * info.mem_unit;
883 numa_info->size = amdgpu_acpi_get_numa_size(nid);
885 xa_store(&numa_info_xa, numa_info->pxm, numa_info, GFP_KERNEL);
893 * amdgpu_acpi_get_node_id - obtain the NUMA node id for corresponding amdgpu
896 * @handle: acpi handle
897 * @numa_info: amdgpu_numa_info structure holding numa information
899 * Queries the ACPI interface to fetch the corresponding NUMA Node ID for a
900 * given amdgpu acpi device.
902 * Returns ACPI STATUS OK with Node ID on success or the corresponding failure reason
904 static acpi_status amdgpu_acpi_get_node_id(acpi_handle handle,
905 struct amdgpu_numa_info **numa_info)
907 #ifdef CONFIG_ACPI_NUMA
912 return_ACPI_STATUS(AE_ERROR);
914 status = acpi_evaluate_integer(handle, "_PXM", NULL, &pxm);
916 if (ACPI_FAILURE(status))
919 *numa_info = amdgpu_acpi_get_numa_info(pxm);
922 return_ACPI_STATUS(AE_ERROR);
924 return_ACPI_STATUS(AE_OK);
926 return_ACPI_STATUS(AE_NOT_EXIST);
930 static struct amdgpu_acpi_dev_info *amdgpu_acpi_get_dev(u16 bdf)
932 struct amdgpu_acpi_dev_info *acpi_dev;
934 if (list_empty(&amdgpu_acpi_dev_list))
937 list_for_each_entry(acpi_dev, &amdgpu_acpi_dev_list, list)
938 if (acpi_dev->bdf == bdf)
944 static int amdgpu_acpi_dev_init(struct amdgpu_acpi_dev_info **dev_info,
945 struct amdgpu_acpi_xcc_info *xcc_info, u16 bdf)
947 struct amdgpu_acpi_dev_info *tmp;
948 union acpi_object *obj;
952 tmp = kzalloc(sizeof(struct amdgpu_acpi_dev_info), GFP_KERNEL);
956 INIT_LIST_HEAD(&tmp->xcc_list);
957 INIT_LIST_HEAD(&tmp->list);
960 obj = acpi_evaluate_dsm_typed(xcc_info->handle, &amd_xcc_dsm_guid, 0,
961 AMD_XCC_DSM_GET_SUPP_MODE, NULL,
965 acpi_handle_debug(xcc_info->handle,
966 "_DSM function %d evaluation failed",
967 AMD_XCC_DSM_GET_SUPP_MODE);
972 tmp->supp_xcp_mode = obj->integer.value & 0xFFFF;
975 obj = acpi_evaluate_dsm_typed(xcc_info->handle, &amd_xcc_dsm_guid, 0,
976 AMD_XCC_DSM_GET_XCP_MODE, NULL,
980 acpi_handle_debug(xcc_info->handle,
981 "_DSM function %d evaluation failed",
982 AMD_XCC_DSM_GET_XCP_MODE);
987 tmp->xcp_mode = obj->integer.value & 0xFFFF;
988 tmp->mem_mode = (obj->integer.value >> 32) & 0xFFFF;
991 /* Evaluate DSMs and fill XCC information */
992 obj = acpi_evaluate_dsm_typed(xcc_info->handle, &amd_xcc_dsm_guid, 0,
993 AMD_XCC_DSM_GET_TMR_INFO, NULL,
996 if (!obj || obj->package.count < 2) {
997 acpi_handle_debug(xcc_info->handle,
998 "_DSM function %d evaluation failed",
999 AMD_XCC_DSM_GET_TMR_INFO);
1004 tmp->tmr_base = obj->package.elements[0].integer.value;
1005 tmp->tmr_size = obj->package.elements[1].integer.value;
1009 "New dev(%x): Supported xcp mode: %x curr xcp_mode : %x mem mode : %x, tmr base: %llx tmr size: %llx ",
1010 tmp->bdf, tmp->supp_xcp_mode, tmp->xcp_mode, tmp->mem_mode,
1011 tmp->tmr_base, tmp->tmr_size);
1012 list_add_tail(&tmp->list, &amdgpu_acpi_dev_list);
1025 static int amdgpu_acpi_get_xcc_info(struct amdgpu_acpi_xcc_info *xcc_info,
1028 union acpi_object *obj;
1032 obj = acpi_evaluate_dsm_typed(xcc_info->handle, &amd_xcc_dsm_guid, 0,
1033 AMD_XCC_DSM_GET_NUM_FUNCS, NULL,
1036 if (!obj || obj->integer.value != AMD_XCC_DSM_NUM_FUNCS)
1040 /* Evaluate DSMs and fill XCC information */
1041 obj = acpi_evaluate_dsm_typed(xcc_info->handle, &amd_xcc_dsm_guid, 0,
1042 AMD_XCC_DSM_GET_VF_XCC_MAPPING, NULL,
1046 acpi_handle_debug(xcc_info->handle,
1047 "_DSM function %d evaluation failed",
1048 AMD_XCC_DSM_GET_VF_XCC_MAPPING);
1053 /* PF xcc id [39:32] */
1054 xcc_info->phy_id = (obj->integer.value >> 32) & 0xFF;
1055 /* xcp node of this xcc [47:40] */
1056 xcc_info->xcp_node = (obj->integer.value >> 40) & 0xFF;
1057 /* PF bus/dev/fn of this xcc [63:48] */
1058 *bdf = (obj->integer.value >> 48) & 0xFFFF;
1063 amdgpu_acpi_get_node_id(xcc_info->handle, &xcc_info->numa_info);
1065 /* TODO: check if this check is required */
1066 if (ACPI_SUCCESS(status))
1075 static int amdgpu_acpi_enumerate_xcc(void)
1077 struct amdgpu_acpi_dev_info *dev_info = NULL;
1078 struct amdgpu_acpi_xcc_info *xcc_info;
1079 struct acpi_device *acpi_dev;
1080 char hid[ACPI_ID_LEN];
1084 INIT_LIST_HEAD(&amdgpu_acpi_dev_list);
1085 xa_init(&numa_info_xa);
1087 for (id = 0; id < AMD_XCC_MAX_HID; id++) {
1088 sprintf(hid, "%s%d", "AMD", AMD_XCC_HID_START + id);
1089 acpi_dev = acpi_dev_get_first_match_dev(hid, NULL, -1);
1090 /* These ACPI objects are expected to be in sequential order. If
1091 * one is not found, no need to check the rest.
1094 DRM_DEBUG_DRIVER("No matching acpi device found for %s",
1099 xcc_info = kzalloc(sizeof(struct amdgpu_acpi_xcc_info),
1102 DRM_ERROR("Failed to allocate memory for xcc info\n");
1106 INIT_LIST_HEAD(&xcc_info->list);
1107 xcc_info->handle = acpi_device_handle(acpi_dev);
1108 acpi_dev_put(acpi_dev);
1110 ret = amdgpu_acpi_get_xcc_info(xcc_info, &bdf);
1116 dev_info = amdgpu_acpi_get_dev(bdf);
1119 ret = amdgpu_acpi_dev_init(&dev_info, xcc_info, bdf);
1129 list_add_tail(&xcc_info->list, &dev_info->xcc_list);
1135 int amdgpu_acpi_get_tmr_info(struct amdgpu_device *adev, u64 *tmr_offset,
1138 struct amdgpu_acpi_dev_info *dev_info;
1141 if (!tmr_offset || !tmr_size)
1144 bdf = (adev->pdev->bus->number << 8) | adev->pdev->devfn;
1145 dev_info = amdgpu_acpi_get_dev(bdf);
1149 *tmr_offset = dev_info->tmr_base;
1150 *tmr_size = dev_info->tmr_size;
1155 int amdgpu_acpi_get_mem_info(struct amdgpu_device *adev, int xcc_id,
1156 struct amdgpu_numa_info *numa_info)
1158 struct amdgpu_acpi_dev_info *dev_info;
1159 struct amdgpu_acpi_xcc_info *xcc_info;
1165 bdf = (adev->pdev->bus->number << 8) | adev->pdev->devfn;
1166 dev_info = amdgpu_acpi_get_dev(bdf);
1170 list_for_each_entry(xcc_info, &dev_info->xcc_list, list) {
1171 if (xcc_info->phy_id == xcc_id) {
1172 memcpy(numa_info, xcc_info->numa_info,
1173 sizeof(*numa_info));
1182 * amdgpu_acpi_event - handle notify events
1184 * @nb: notifier block
1188 * Calls relevant amdgpu functions in response to various
1190 * Returns NOTIFY code
1192 static int amdgpu_acpi_event(struct notifier_block *nb,
1196 struct amdgpu_device *adev = container_of(nb, struct amdgpu_device, acpi_nb);
1197 struct acpi_bus_event *entry = (struct acpi_bus_event *)data;
1199 if (strcmp(entry->device_class, ACPI_AC_CLASS) == 0) {
1200 if (power_supply_is_system_supplied() > 0)
1201 DRM_DEBUG_DRIVER("pm: AC\n");
1203 DRM_DEBUG_DRIVER("pm: DC\n");
1205 amdgpu_pm_acpi_event_handler(adev);
1208 /* Check for pending SBIOS requests */
1209 return amdgpu_atif_handler(adev, entry);
1212 /* Call all ACPI methods here */
1214 * amdgpu_acpi_init - init driver acpi support
1216 * @adev: amdgpu_device pointer
1218 * Verifies the AMD ACPI interfaces and registers with the acpi
1219 * notifier chain (all asics).
1220 * Returns 0 on success, error on failure.
1222 int amdgpu_acpi_init(struct amdgpu_device *adev)
1224 struct amdgpu_atif *atif = &amdgpu_acpi_priv.atif;
1226 if (atif->notifications.brightness_change) {
1227 if (adev->dc_enabled) {
1228 #if defined(CONFIG_DRM_AMD_DC)
1229 struct amdgpu_display_manager *dm = &adev->dm;
1231 if (dm->backlight_dev[0])
1232 atif->bd = dm->backlight_dev[0];
1235 struct drm_encoder *tmp;
1237 /* Find the encoder controlling the brightness */
1238 list_for_each_entry(tmp, &adev_to_drm(adev)->mode_config.encoder_list,
1240 struct amdgpu_encoder *enc = to_amdgpu_encoder(tmp);
1242 if ((enc->devices & (ATOM_DEVICE_LCD_SUPPORT)) &&
1244 struct amdgpu_encoder_atom_dig *dig = enc->enc_priv;
1247 atif->bd = dig->bl_dev;
1254 adev->acpi_nb.notifier_call = amdgpu_acpi_event;
1255 register_acpi_notifier(&adev->acpi_nb);
1260 void amdgpu_acpi_get_backlight_caps(struct amdgpu_dm_backlight_caps *caps)
1262 struct amdgpu_atif *atif = &amdgpu_acpi_priv.atif;
1264 caps->caps_valid = atif->backlight_caps.caps_valid;
1265 caps->min_input_signal = atif->backlight_caps.min_input_signal;
1266 caps->max_input_signal = atif->backlight_caps.max_input_signal;
1270 * amdgpu_acpi_fini - tear down driver acpi support
1272 * @adev: amdgpu_device pointer
1274 * Unregisters with the acpi notifier chain (all asics).
1276 void amdgpu_acpi_fini(struct amdgpu_device *adev)
1278 unregister_acpi_notifier(&adev->acpi_nb);
1282 * amdgpu_atif_pci_probe_handle - look up the ATIF handle
1286 * Look up the ATIF handles (all asics).
1287 * Returns true if the handle is found, false if not.
1289 static bool amdgpu_atif_pci_probe_handle(struct pci_dev *pdev)
1291 char acpi_method_name[255] = { 0 };
1292 struct acpi_buffer buffer = {sizeof(acpi_method_name), acpi_method_name};
1293 acpi_handle dhandle, atif_handle;
1297 dhandle = ACPI_HANDLE(&pdev->dev);
1301 status = acpi_get_handle(dhandle, "ATIF", &atif_handle);
1302 if (ACPI_FAILURE(status))
1305 amdgpu_acpi_priv.atif.handle = atif_handle;
1306 acpi_get_name(amdgpu_acpi_priv.atif.handle, ACPI_FULL_PATHNAME, &buffer);
1307 DRM_DEBUG_DRIVER("Found ATIF handle %s\n", acpi_method_name);
1308 ret = amdgpu_atif_verify_interface(&amdgpu_acpi_priv.atif);
1310 amdgpu_acpi_priv.atif.handle = 0;
1317 * amdgpu_atcs_pci_probe_handle - look up the ATCS handle
1321 * Look up the ATCS handles (all asics).
1322 * Returns true if the handle is found, false if not.
1324 static bool amdgpu_atcs_pci_probe_handle(struct pci_dev *pdev)
1326 char acpi_method_name[255] = { 0 };
1327 struct acpi_buffer buffer = { sizeof(acpi_method_name), acpi_method_name };
1328 acpi_handle dhandle, atcs_handle;
1332 dhandle = ACPI_HANDLE(&pdev->dev);
1336 status = acpi_get_handle(dhandle, "ATCS", &atcs_handle);
1337 if (ACPI_FAILURE(status))
1340 amdgpu_acpi_priv.atcs.handle = atcs_handle;
1341 acpi_get_name(amdgpu_acpi_priv.atcs.handle, ACPI_FULL_PATHNAME, &buffer);
1342 DRM_DEBUG_DRIVER("Found ATCS handle %s\n", acpi_method_name);
1343 ret = amdgpu_atcs_verify_interface(&amdgpu_acpi_priv.atcs);
1345 amdgpu_acpi_priv.atcs.handle = 0;
1353 * amdgpu_acpi_should_gpu_reset
1355 * @adev: amdgpu_device_pointer
1357 * returns true if should reset GPU, false if not
1359 bool amdgpu_acpi_should_gpu_reset(struct amdgpu_device *adev)
1361 if ((adev->flags & AMD_IS_APU) &&
1362 adev->gfx.imu.funcs) /* Not need to do mode2 reset for IMU enabled APUs */
1365 if ((adev->flags & AMD_IS_APU) &&
1366 amdgpu_acpi_is_s3_active(adev))
1369 if (amdgpu_sriov_vf(adev))
1372 #if IS_ENABLED(CONFIG_SUSPEND)
1373 return pm_suspend_target_state != PM_SUSPEND_TO_IDLE;
1380 * amdgpu_acpi_detect - detect ACPI ATIF/ATCS methods
1382 * Check if we have the ATIF/ATCS methods and populate
1383 * the structures in the driver.
1385 void amdgpu_acpi_detect(void)
1387 struct amdgpu_atif *atif = &amdgpu_acpi_priv.atif;
1388 struct amdgpu_atcs *atcs = &amdgpu_acpi_priv.atcs;
1389 struct pci_dev *pdev = NULL;
1392 while ((pdev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, pdev)) != NULL) {
1394 amdgpu_atif_pci_probe_handle(pdev);
1396 amdgpu_atcs_pci_probe_handle(pdev);
1399 while ((pdev = pci_get_class(PCI_CLASS_DISPLAY_OTHER << 8, pdev)) != NULL) {
1401 amdgpu_atif_pci_probe_handle(pdev);
1403 amdgpu_atcs_pci_probe_handle(pdev);
1406 if (atif->functions.sbios_requests && !atif->functions.system_params) {
1407 /* XXX check this workraround, if sbios request function is
1408 * present we have to see how it's configured in the system
1411 atif->functions.system_params = true;
1414 if (atif->functions.system_params) {
1415 ret = amdgpu_atif_get_notification_params(atif);
1417 DRM_DEBUG_DRIVER("Call to GET_SYSTEM_PARAMS failed: %d\n",
1419 /* Disable notification */
1420 atif->notification_cfg.enabled = false;
1424 if (atif->functions.query_backlight_transfer_characteristics) {
1425 ret = amdgpu_atif_query_backlight_caps(atif);
1427 DRM_DEBUG_DRIVER("Call to QUERY_BACKLIGHT_TRANSFER_CHARACTERISTICS failed: %d\n",
1429 atif->backlight_caps.caps_valid = false;
1432 atif->backlight_caps.caps_valid = false;
1435 amdgpu_acpi_enumerate_xcc();
1438 void amdgpu_acpi_release(void)
1440 struct amdgpu_acpi_dev_info *dev_info, *dev_tmp;
1441 struct amdgpu_acpi_xcc_info *xcc_info, *xcc_tmp;
1442 struct amdgpu_numa_info *numa_info;
1443 unsigned long index;
1445 xa_for_each(&numa_info_xa, index, numa_info) {
1447 xa_erase(&numa_info_xa, index);
1450 if (list_empty(&amdgpu_acpi_dev_list))
1453 list_for_each_entry_safe(dev_info, dev_tmp, &amdgpu_acpi_dev_list,
1455 list_for_each_entry_safe(xcc_info, xcc_tmp, &dev_info->xcc_list,
1457 list_del(&xcc_info->list);
1461 list_del(&dev_info->list);
1466 #if IS_ENABLED(CONFIG_SUSPEND)
1468 * amdgpu_acpi_is_s3_active
1470 * @adev: amdgpu_device_pointer
1472 * returns true if supported, false if not.
1474 bool amdgpu_acpi_is_s3_active(struct amdgpu_device *adev)
1476 return !(adev->flags & AMD_IS_APU) ||
1477 (pm_suspend_target_state == PM_SUSPEND_MEM);
1481 * amdgpu_acpi_is_s0ix_active
1483 * @adev: amdgpu_device_pointer
1485 * returns true if supported, false if not.
1487 bool amdgpu_acpi_is_s0ix_active(struct amdgpu_device *adev)
1489 if (!(adev->flags & AMD_IS_APU) ||
1490 (pm_suspend_target_state != PM_SUSPEND_TO_IDLE))
1493 if (adev->asic_type < CHIP_RAVEN)
1497 * If ACPI_FADT_LOW_POWER_S0 is not set in the FADT, it is generally
1498 * risky to do any special firmware-related preparations for entering
1499 * S0ix even though the system is suspending to idle, so return false
1502 if (!(acpi_gbl_FADT.flags & ACPI_FADT_LOW_POWER_S0)) {
1503 dev_err_once(adev->dev,
1504 "Power consumption will be higher as BIOS has not been configured for suspend-to-idle.\n"
1505 "To use suspend-to-idle change the sleep mode in BIOS setup.\n");
1509 #if !IS_ENABLED(CONFIG_AMD_PMC)
1510 dev_err_once(adev->dev,
1511 "Power consumption will be higher as the kernel has not been compiled with CONFIG_AMD_PMC.\n");
1515 #endif /* CONFIG_AMD_PMC */
1518 #endif /* CONFIG_SUSPEND */