xref: /linux/drivers/gpu/drm/amd/amdgpu/amdgpu_acpi.c (revision 7ba7655701c5cad5bfa043d464d1e24873e24aab)
1 // SPDX-License-Identifier: MIT
2 /*
3  * Copyright 2012 Advanced Micro Devices, Inc.
4  *
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:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
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.
22  *
23  */
24 
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/numa.h>
32 #include <linux/pm_runtime.h>
33 #include <linux/suspend.h>
34 #include <acpi/video.h>
35 #include <acpi/actbl.h>
36 
37 #include "amdgpu.h"
38 #include "amdgpu_pm.h"
39 #include "amdgpu_display.h"
40 #include "amd_acpi.h"
41 #include "atom.h"
42 
43 /* Declare GUID for AMD _DSM method for XCCs */
44 static const guid_t amd_xcc_dsm_guid = GUID_INIT(0x8267f5d5, 0xa556, 0x44f2,
45 						 0xb8, 0xb4, 0x45, 0x56, 0x2e,
46 						 0x8c, 0x5b, 0xec);
47 
48 #define AMD_XCC_HID_START 3000
49 #define AMD_XCC_DSM_GET_NUM_FUNCS 0
50 #define AMD_XCC_DSM_GET_SUPP_MODE 1
51 #define AMD_XCC_DSM_GET_XCP_MODE 2
52 #define AMD_XCC_DSM_GET_VF_XCC_MAPPING 4
53 #define AMD_XCC_DSM_GET_TMR_INFO 5
54 #define AMD_XCC_DSM_NUM_FUNCS 5
55 
56 #define AMD_XCC_MAX_HID 24
57 
58 struct xarray numa_info_xa;
59 
60 /* Encapsulates the XCD acpi object information */
61 struct amdgpu_acpi_xcc_info {
62 	struct list_head list;
63 	struct amdgpu_numa_info *numa_info;
64 	uint8_t xcp_node;
65 	uint8_t phy_id;
66 	acpi_handle handle;
67 };
68 
69 struct amdgpu_acpi_dev_info {
70 	struct list_head list;
71 	struct list_head xcc_list;
72 	uint32_t sbdf;
73 	uint16_t supp_xcp_mode;
74 	uint16_t xcp_mode;
75 	uint16_t mem_mode;
76 	uint64_t tmr_base;
77 	uint64_t tmr_size;
78 };
79 
80 struct list_head amdgpu_acpi_dev_list;
81 
82 struct amdgpu_atif_notification_cfg {
83 	bool enabled;
84 	int command_code;
85 };
86 
87 struct amdgpu_atif_notifications {
88 	bool thermal_state;
89 	bool forced_power_state;
90 	bool system_power_state;
91 	bool brightness_change;
92 	bool dgpu_display_event;
93 	bool gpu_package_power_limit;
94 };
95 
96 struct amdgpu_atif_functions {
97 	bool system_params;
98 	bool sbios_requests;
99 	bool temperature_change;
100 	bool query_backlight_transfer_characteristics;
101 	bool ready_to_undock;
102 	bool external_gpu_information;
103 };
104 
105 struct amdgpu_atif {
106 	acpi_handle handle;
107 
108 	struct amdgpu_atif_notifications notifications;
109 	struct amdgpu_atif_functions functions;
110 	struct amdgpu_atif_notification_cfg notification_cfg;
111 	struct backlight_device *bd;
112 	struct amdgpu_dm_backlight_caps backlight_caps;
113 };
114 
115 struct amdgpu_atcs_functions {
116 	bool get_ext_state;
117 	bool pcie_perf_req;
118 	bool pcie_dev_rdy;
119 	bool pcie_bus_width;
120 	bool get_uma_size;
121 	bool power_shift_control;
122 	bool set_uma_allocation_size;
123 };
124 
125 struct amdgpu_atcs {
126 	acpi_handle handle;
127 
128 	struct amdgpu_atcs_functions functions;
129 };
130 
131 static struct amdgpu_acpi_priv {
132 	struct amdgpu_atif atif;
133 	struct amdgpu_atcs atcs;
134 } amdgpu_acpi_priv;
135 
136 /* Call the ATIF method
137  */
138 /**
139  * amdgpu_atif_call - call an ATIF method
140  *
141  * @atif: atif structure
142  * @function: the ATIF function to execute
143  * @params: ATIF function params
144  * @min_size: minimum size of the expected output buffer in bytes
145  *
146  * Executes the requested ATIF function (all asics).
147  * Returns a pointer to the acpi output buffer.
148  */
149 static union acpi_object *amdgpu_atif_call(struct amdgpu_atif *atif,
150 					   int function,
151 					   struct acpi_buffer *params,
152 					   size_t min_size)
153 {
154 	acpi_status status;
155 	union acpi_object *obj;
156 	union acpi_object atif_arg_elements[2];
157 	struct acpi_object_list atif_arg;
158 	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
159 
160 	atif_arg.count = 2;
161 	atif_arg.pointer = &atif_arg_elements[0];
162 
163 	atif_arg_elements[0].type = ACPI_TYPE_INTEGER;
164 	atif_arg_elements[0].integer.value = function;
165 
166 	if (params) {
167 		atif_arg_elements[1].type = ACPI_TYPE_BUFFER;
168 		atif_arg_elements[1].buffer.length = params->length;
169 		atif_arg_elements[1].buffer.pointer = params->pointer;
170 	} else {
171 		/* We need a second fake parameter */
172 		atif_arg_elements[1].type = ACPI_TYPE_INTEGER;
173 		atif_arg_elements[1].integer.value = 0;
174 	}
175 
176 	status = acpi_evaluate_object(atif->handle, NULL, &atif_arg,
177 				      &buffer);
178 	obj = (union acpi_object *)buffer.pointer;
179 
180 	/* Fail if calling the method fails */
181 	if (ACPI_FAILURE(status)) {
182 		DRM_DEBUG_DRIVER("failed to evaluate ATIF got %s\n",
183 				 acpi_format_exception(status));
184 		kfree(obj);
185 		return NULL;
186 	}
187 
188 	if (obj->type != ACPI_TYPE_BUFFER) {
189 		DRM_DEBUG_DRIVER("bad object returned from ATIF: %d\n",
190 				 obj->type);
191 		kfree(obj);
192 		return NULL;
193 	}
194 
195 	if (obj->buffer.length < sizeof(u16)) {
196 		DRM_DEBUG_DRIVER("ATIF buffer too small to hold size field: %u\n",
197 				 obj->buffer.length);
198 		kfree(obj);
199 		return NULL;
200 	}
201 
202 	if (obj->buffer.length < *(u16 *)obj->buffer.pointer) {
203 		DRM_DEBUG_DRIVER("ATIF buffer length mismatch: reported %u, actual %u\n",
204 				 *(u16 *)obj->buffer.pointer,
205 				 obj->buffer.length);
206 		kfree(obj);
207 		return NULL;
208 	}
209 
210 	if (*(u16 *)obj->buffer.pointer < min_size) {
211 		DRM_DEBUG_DRIVER("ATIF buffer too small: expected %zu, got %u\n",
212 				 min_size, *(u16 *)obj->buffer.pointer);
213 		kfree(obj);
214 		return NULL;
215 	}
216 
217 	return obj;
218 }
219 
220 /**
221  * amdgpu_atif_parse_notification - parse supported notifications
222  *
223  * @n: supported notifications struct
224  * @mask: supported notifications mask from ATIF
225  *
226  * Use the supported notifications mask from ATIF function
227  * ATIF_FUNCTION_VERIFY_INTERFACE to determine what notifications
228  * are supported (all asics).
229  */
230 static void amdgpu_atif_parse_notification(struct amdgpu_atif_notifications *n, u32 mask)
231 {
232 	n->thermal_state = mask & ATIF_THERMAL_STATE_CHANGE_REQUEST_SUPPORTED;
233 	n->forced_power_state = mask & ATIF_FORCED_POWER_STATE_CHANGE_REQUEST_SUPPORTED;
234 	n->system_power_state = mask & ATIF_SYSTEM_POWER_SOURCE_CHANGE_REQUEST_SUPPORTED;
235 	n->brightness_change = mask & ATIF_PANEL_BRIGHTNESS_CHANGE_REQUEST_SUPPORTED;
236 	n->dgpu_display_event = mask & ATIF_DGPU_DISPLAY_EVENT_SUPPORTED;
237 	n->gpu_package_power_limit = mask & ATIF_GPU_PACKAGE_POWER_LIMIT_REQUEST_SUPPORTED;
238 }
239 
240 /**
241  * amdgpu_atif_parse_functions - parse supported functions
242  *
243  * @f: supported functions struct
244  * @mask: supported functions mask from ATIF
245  *
246  * Use the supported functions mask from ATIF function
247  * ATIF_FUNCTION_VERIFY_INTERFACE to determine what functions
248  * are supported (all asics).
249  */
250 static void amdgpu_atif_parse_functions(struct amdgpu_atif_functions *f, u32 mask)
251 {
252 	f->system_params = mask & ATIF_GET_SYSTEM_PARAMETERS_SUPPORTED;
253 	f->sbios_requests = mask & ATIF_GET_SYSTEM_BIOS_REQUESTS_SUPPORTED;
254 	f->temperature_change = mask & ATIF_TEMPERATURE_CHANGE_NOTIFICATION_SUPPORTED;
255 	f->query_backlight_transfer_characteristics =
256 		mask & ATIF_QUERY_BACKLIGHT_TRANSFER_CHARACTERISTICS_SUPPORTED;
257 	f->ready_to_undock = mask & ATIF_READY_TO_UNDOCK_NOTIFICATION_SUPPORTED;
258 	f->external_gpu_information = mask & ATIF_GET_EXTERNAL_GPU_INFORMATION_SUPPORTED;
259 }
260 
261 /**
262  * amdgpu_atif_verify_interface - verify ATIF
263  *
264  * @atif: amdgpu atif struct
265  *
266  * Execute the ATIF_FUNCTION_VERIFY_INTERFACE ATIF function
267  * to initialize ATIF and determine what features are supported
268  * (all asics).
269  * returns 0 on success, error on failure.
270  */
271 static noinline_for_stack
272 int amdgpu_atif_verify_interface(struct amdgpu_atif *atif)
273 {
274 	union acpi_object *info;
275 	struct atif_verify_interface output;
276 	size_t size;
277 	int err = 0;
278 
279 	info = amdgpu_atif_call(atif, ATIF_FUNCTION_VERIFY_INTERFACE, NULL,
280 				sizeof(output));
281 	if (!info)
282 		return -EIO;
283 
284 	memset(&output, 0, sizeof(output));
285 
286 	size = min(sizeof(output), (size_t)*(u16 *)info->buffer.pointer);
287 
288 	memcpy(&output, info->buffer.pointer, size);
289 
290 	/* TODO: check version? */
291 	DRM_DEBUG_DRIVER("ATIF version %u\n", output.version);
292 
293 	amdgpu_atif_parse_notification(&atif->notifications, output.notification_mask);
294 	amdgpu_atif_parse_functions(&atif->functions, output.function_bits);
295 
296 	kfree(info);
297 	return err;
298 }
299 
300 /**
301  * amdgpu_atif_get_notification_params - determine notify configuration
302  *
303  * @atif: acpi handle
304  *
305  * Execute the ATIF_FUNCTION_GET_SYSTEM_PARAMETERS ATIF function
306  * to determine if a notifier is used and if so which one
307  * (all asics).  This is either Notify(VGA, 0x81) or Notify(VGA, n)
308  * where n is specified in the result if a notifier is used.
309  * Returns 0 on success, error on failure.
310  */
311 static noinline_for_stack
312 int amdgpu_atif_get_notification_params(struct amdgpu_atif *atif)
313 {
314 	union acpi_object *info;
315 	struct amdgpu_atif_notification_cfg *n = &atif->notification_cfg;
316 	struct atif_system_params params;
317 	size_t size;
318 	int err = 0;
319 
320 	info = amdgpu_atif_call(atif, ATIF_FUNCTION_GET_SYSTEM_PARAMETERS,
321 				NULL, offsetof(struct atif_system_params, command_code));
322 	if (!info) {
323 		err = -EIO;
324 		goto out;
325 	}
326 
327 	memset(&params, 0, sizeof(params));
328 	size = min(sizeof(params), (size_t)*(u16 *)info->buffer.pointer);
329 	memcpy(&params, info->buffer.pointer, size);
330 
331 	DRM_DEBUG_DRIVER("SYSTEM_PARAMS: mask = %#x, flags = %#x\n",
332 			params.flags, params.valid_mask);
333 	params.flags = params.flags & params.valid_mask;
334 
335 	if ((params.flags & ATIF_NOTIFY_MASK) == ATIF_NOTIFY_NONE) {
336 		n->enabled = false;
337 		n->command_code = 0;
338 	} else if ((params.flags & ATIF_NOTIFY_MASK) == ATIF_NOTIFY_81) {
339 		n->enabled = true;
340 		n->command_code = 0x81;
341 	} else {
342 		if (size < 11) {
343 			err = -EINVAL;
344 			goto out;
345 		}
346 		n->enabled = true;
347 		n->command_code = params.command_code;
348 	}
349 
350 out:
351 	DRM_DEBUG_DRIVER("Notification %s, command code = %#x\n",
352 			(n->enabled ? "enabled" : "disabled"),
353 			n->command_code);
354 	kfree(info);
355 	return err;
356 }
357 
358 /**
359  * amdgpu_atif_query_backlight_caps - get min and max backlight input signal
360  *
361  * @atif: acpi handle
362  *
363  * Execute the QUERY_BRIGHTNESS_TRANSFER_CHARACTERISTICS ATIF function
364  * to determine the acceptable range of backlight values
365  *
366  * Backlight_caps.caps_valid will be set to true if the query is successful
367  *
368  * The input signals are in range 0-255
369  *
370  * This function assumes the display with backlight is the first LCD
371  *
372  * Returns 0 on success, error on failure.
373  */
374 static noinline_for_stack
375 int amdgpu_atif_query_backlight_caps(struct amdgpu_atif *atif)
376 {
377 	union acpi_object *info;
378 	struct atif_qbtc_output characteristics;
379 	struct atif_qbtc_arguments arguments;
380 	struct acpi_buffer params;
381 	size_t size;
382 	int err = 0;
383 
384 	arguments.size = sizeof(arguments);
385 	arguments.requested_display = ATIF_QBTC_REQUEST_LCD1;
386 
387 	params.length = sizeof(arguments);
388 	params.pointer = (void *)&arguments;
389 
390 	info = amdgpu_atif_call(atif,
391 		ATIF_FUNCTION_QUERY_BRIGHTNESS_TRANSFER_CHARACTERISTICS,
392 		&params, offsetof(struct atif_qbtc_output, data_points));
393 	if (!info) {
394 		err = -EIO;
395 		goto out;
396 	}
397 
398 	memset(&characteristics, 0, sizeof(characteristics));
399 	size = min(sizeof(characteristics), (size_t)*(u16 *)info->buffer.pointer);
400 	memcpy(&characteristics, info->buffer.pointer, size);
401 
402 	atif->backlight_caps.caps_valid = true;
403 	atif->backlight_caps.min_input_signal =
404 			characteristics.min_input_signal;
405 	atif->backlight_caps.max_input_signal =
406 			characteristics.max_input_signal;
407 	atif->backlight_caps.ac_level = characteristics.ac_level;
408 	atif->backlight_caps.dc_level = characteristics.dc_level;
409 	if (characteristics.number_of_points > ATIF_QBTC_MAX_DATA_POINTS)
410 		pr_warn(FW_BUG "ATIF: backlight curve has %u points, using the first %u\n",
411 			characteristics.number_of_points, ATIF_QBTC_MAX_DATA_POINTS);
412 	atif->backlight_caps.data_points = min_t(u8, characteristics.number_of_points,
413 						 ATIF_QBTC_MAX_DATA_POINTS);
414 	memcpy(atif->backlight_caps.luminance_data,
415 	       characteristics.data_points,
416 	       sizeof(atif->backlight_caps.luminance_data));
417 out:
418 	kfree(info);
419 	return err;
420 }
421 
422 /**
423  * amdgpu_atif_get_sbios_requests - get requested sbios event
424  *
425  * @atif: acpi handle
426  * @req: atif sbios request struct
427  *
428  * Execute the ATIF_FUNCTION_GET_SYSTEM_BIOS_REQUESTS ATIF function
429  * to determine what requests the sbios is making to the driver
430  * (all asics).
431  * Returns 0 on success, error on failure.
432  */
433 static int amdgpu_atif_get_sbios_requests(struct amdgpu_atif *atif,
434 					  struct atif_sbios_requests *req)
435 {
436 	union acpi_object *info;
437 	size_t size;
438 	int count = 0;
439 
440 	info = amdgpu_atif_call(atif, ATIF_FUNCTION_GET_SYSTEM_BIOS_REQUESTS,
441 				NULL, sizeof(*req));
442 	if (!info)
443 		return -EIO;
444 
445 	memset(req, 0, sizeof(*req));
446 
447 	size = min(sizeof(*req), (size_t)*(u16 *)info->buffer.pointer);
448 	memcpy(req, info->buffer.pointer, size);
449 	DRM_DEBUG_DRIVER("SBIOS pending requests: %#x\n", req->pending);
450 
451 	count = hweight32(req->pending);
452 
453 	kfree(info);
454 	return count;
455 }
456 
457 /**
458  * amdgpu_atif_handler - handle ATIF notify requests
459  *
460  * @adev: amdgpu_device pointer
461  * @event: atif sbios request struct
462  *
463  * Checks the acpi event and if it matches an atif event,
464  * handles it.
465  *
466  * Returns:
467  * NOTIFY_BAD or NOTIFY_DONE, depending on the event.
468  */
469 static int amdgpu_atif_handler(struct amdgpu_device *adev,
470 			       struct acpi_bus_event *event)
471 {
472 	struct amdgpu_atif *atif = &amdgpu_acpi_priv.atif;
473 	int count;
474 
475 	DRM_DEBUG_DRIVER("event, device_class = %s, type = %#x\n",
476 			event->device_class, event->type);
477 
478 	if (strcmp(event->device_class, ACPI_VIDEO_CLASS) != 0)
479 		return NOTIFY_DONE;
480 
481 	/* Is this actually our event? */
482 	if (!atif->notification_cfg.enabled ||
483 	    event->type != atif->notification_cfg.command_code) {
484 		/* These events will generate keypresses otherwise */
485 		if (event->type == ACPI_VIDEO_NOTIFY_PROBE)
486 			return NOTIFY_BAD;
487 		else
488 			return NOTIFY_DONE;
489 	}
490 
491 	if (atif->functions.sbios_requests) {
492 		struct atif_sbios_requests req;
493 
494 		/* Check pending SBIOS requests */
495 		count = amdgpu_atif_get_sbios_requests(atif, &req);
496 
497 		if (count <= 0)
498 			return NOTIFY_BAD;
499 
500 		DRM_DEBUG_DRIVER("ATIF: %d pending SBIOS requests\n", count);
501 
502 		if (req.pending & ATIF_PANEL_BRIGHTNESS_CHANGE_REQUEST) {
503 			if (atif->bd) {
504 				DRM_DEBUG_DRIVER("Changing brightness to %d\n",
505 						 req.backlight_level);
506 				/*
507 				 * XXX backlight_device_set_brightness() is
508 				 * hardwired to post BACKLIGHT_UPDATE_SYSFS.
509 				 * It probably should accept 'reason' parameter.
510 				 */
511 				backlight_device_set_brightness(atif->bd, req.backlight_level);
512 			}
513 		}
514 
515 		if (req.pending & ATIF_DGPU_DISPLAY_EVENT) {
516 			if (adev->flags & AMD_IS_PX) {
517 				pm_runtime_get_sync(adev_to_drm(adev)->dev);
518 				/* Just fire off a uevent and let userspace tell us what to do */
519 				drm_helper_hpd_irq_event(adev_to_drm(adev));
520 				pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
521 			}
522 		}
523 		/* TODO: check other events */
524 	}
525 
526 	/* We've handled the event, stop the notifier chain. The ACPI interface
527 	 * overloads ACPI_VIDEO_NOTIFY_PROBE, we don't want to send that to
528 	 * userspace if the event was generated only to signal a SBIOS
529 	 * request.
530 	 */
531 	return NOTIFY_BAD;
532 }
533 
534 /* Call the ATCS method
535  */
536 /**
537  * amdgpu_atcs_call - call an ATCS method
538  *
539  * @atcs: atcs structure
540  * @function: the ATCS function to execute
541  * @params: ATCS function params
542  *
543  * Executes the requested ATCS function (all asics).
544  * Returns a pointer to the acpi output buffer.
545  */
546 static union acpi_object *amdgpu_atcs_call(struct amdgpu_atcs *atcs,
547 					   int function,
548 					   struct acpi_buffer *params)
549 {
550 	acpi_status status;
551 	union acpi_object atcs_arg_elements[2];
552 	struct acpi_object_list atcs_arg;
553 	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
554 
555 	atcs_arg.count = 2;
556 	atcs_arg.pointer = &atcs_arg_elements[0];
557 
558 	atcs_arg_elements[0].type = ACPI_TYPE_INTEGER;
559 	atcs_arg_elements[0].integer.value = function;
560 
561 	if (params) {
562 		atcs_arg_elements[1].type = ACPI_TYPE_BUFFER;
563 		atcs_arg_elements[1].buffer.length = params->length;
564 		atcs_arg_elements[1].buffer.pointer = params->pointer;
565 	} else {
566 		/* We need a second fake parameter */
567 		atcs_arg_elements[1].type = ACPI_TYPE_INTEGER;
568 		atcs_arg_elements[1].integer.value = 0;
569 	}
570 
571 	status = acpi_evaluate_object(atcs->handle, NULL, &atcs_arg, &buffer);
572 
573 	/* Fail only if calling the method fails and ATIF is supported */
574 	if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
575 		DRM_DEBUG_DRIVER("failed to evaluate ATCS got %s\n",
576 				 acpi_format_exception(status));
577 		kfree(buffer.pointer);
578 		return NULL;
579 	}
580 
581 	return buffer.pointer;
582 }
583 
584 /**
585  * amdgpu_atcs_parse_functions - parse supported functions
586  *
587  * @f: supported functions struct
588  * @mask: supported functions mask from ATCS
589  *
590  * Use the supported functions mask from ATCS function
591  * ATCS_FUNCTION_VERIFY_INTERFACE to determine what functions
592  * are supported (all asics).
593  */
594 static void amdgpu_atcs_parse_functions(struct amdgpu_atcs_functions *f, u32 mask)
595 {
596 	f->get_ext_state = mask & ATCS_GET_EXTERNAL_STATE_SUPPORTED;
597 	f->pcie_perf_req = mask & ATCS_PCIE_PERFORMANCE_REQUEST_SUPPORTED;
598 	f->pcie_dev_rdy = mask & ATCS_PCIE_DEVICE_READY_NOTIFICATION_SUPPORTED;
599 	f->pcie_bus_width = mask & ATCS_SET_PCIE_BUS_WIDTH_SUPPORTED;
600 	f->get_uma_size = mask & ACPI_ATCS_GET_UMA_SIZE_SUPPORTED;
601 	f->power_shift_control = mask & ATCS_SET_POWER_SHIFT_CONTROL_SUPPORTED;
602 	f->set_uma_allocation_size = mask & ACPI_ATCS_SET_UMA_ALLOCATION_SIZE_SUPPORTED;
603 }
604 
605 /**
606  * amdgpu_atcs_verify_interface - verify ATCS
607  *
608  * @atcs: amdgpu atcs struct
609  *
610  * Execute the ATCS_FUNCTION_VERIFY_INTERFACE ATCS function
611  * to initialize ATCS and determine what features are supported
612  * (all asics).
613  * returns 0 on success, error on failure.
614  */
615 static noinline_for_stack
616 int amdgpu_atcs_verify_interface(struct amdgpu_atcs *atcs)
617 {
618 	union acpi_object *info;
619 	struct atcs_verify_interface output;
620 	size_t size;
621 	int err = 0;
622 
623 	info = amdgpu_atcs_call(atcs, ATCS_FUNCTION_VERIFY_INTERFACE, NULL);
624 	if (!info)
625 		return -EIO;
626 
627 	memset(&output, 0, sizeof(output));
628 
629 	size = *(u16 *) info->buffer.pointer;
630 	if (size < 8) {
631 		DRM_INFO("ATCS buffer is too small: %zu\n", size);
632 		err = -EINVAL;
633 		goto out;
634 	}
635 	size = min(sizeof(output), size);
636 
637 	memcpy(&output, info->buffer.pointer, size);
638 
639 	/* TODO: check version? */
640 	DRM_DEBUG_DRIVER("ATCS version %u\n", output.version);
641 
642 	amdgpu_atcs_parse_functions(&atcs->functions, output.function_bits);
643 
644 out:
645 	kfree(info);
646 	return err;
647 }
648 
649 /**
650  * amdgpu_acpi_is_pcie_performance_request_supported
651  *
652  * @adev: amdgpu_device pointer
653  *
654  * Check if the ATCS pcie_perf_req and pcie_dev_rdy methods
655  * are supported (all asics).
656  * returns true if supported, false if not.
657  */
658 bool amdgpu_acpi_is_pcie_performance_request_supported(struct amdgpu_device *adev)
659 {
660 	struct amdgpu_atcs *atcs = &amdgpu_acpi_priv.atcs;
661 
662 	if (atcs->functions.pcie_perf_req && atcs->functions.pcie_dev_rdy)
663 		return true;
664 
665 	return false;
666 }
667 
668 /**
669  * amdgpu_acpi_is_power_shift_control_supported
670  *
671  * Check if the ATCS power shift control method
672  * is supported.
673  * returns true if supported, false if not.
674  */
675 bool amdgpu_acpi_is_power_shift_control_supported(void)
676 {
677 	return amdgpu_acpi_priv.atcs.functions.power_shift_control;
678 }
679 
680 bool amdgpu_acpi_is_set_uma_allocation_size_supported(void)
681 {
682 	return amdgpu_acpi_priv.atcs.functions.set_uma_allocation_size;
683 }
684 
685 /**
686  * amdgpu_acpi_pcie_notify_device_ready
687  *
688  * @adev: amdgpu_device pointer
689  *
690  * Executes the PCIE_DEVICE_READY_NOTIFICATION method
691  * (all asics).
692  * returns 0 on success, error on failure.
693  */
694 int amdgpu_acpi_pcie_notify_device_ready(struct amdgpu_device *adev)
695 {
696 	union acpi_object *info;
697 	struct amdgpu_atcs *atcs = &amdgpu_acpi_priv.atcs;
698 
699 	if (!atcs->functions.pcie_dev_rdy)
700 		return -EINVAL;
701 
702 	info = amdgpu_atcs_call(atcs, ATCS_FUNCTION_PCIE_DEVICE_READY_NOTIFICATION, NULL);
703 	if (!info)
704 		return -EIO;
705 
706 	kfree(info);
707 
708 	return 0;
709 }
710 
711 /**
712  * amdgpu_acpi_pcie_performance_request
713  *
714  * @adev: amdgpu_device pointer
715  * @perf_req: requested perf level (pcie gen speed)
716  * @advertise: set advertise caps flag if set
717  *
718  * Executes the PCIE_PERFORMANCE_REQUEST method to
719  * change the pcie gen speed (all asics).
720  * returns 0 on success, error on failure.
721  */
722 int amdgpu_acpi_pcie_performance_request(struct amdgpu_device *adev,
723 					 u8 perf_req, bool advertise)
724 {
725 	union acpi_object *info;
726 	struct amdgpu_atcs *atcs = &amdgpu_acpi_priv.atcs;
727 	struct atcs_pref_req_input atcs_input;
728 	struct atcs_pref_req_output atcs_output;
729 	struct acpi_buffer params;
730 	size_t size;
731 	u32 retry = 3;
732 
733 	if (amdgpu_acpi_pcie_notify_device_ready(adev))
734 		return -EINVAL;
735 
736 	if (!atcs->functions.pcie_perf_req)
737 		return -EINVAL;
738 
739 	atcs_input.size = sizeof(struct atcs_pref_req_input);
740 	/* client id (bit 2-0: func num, 7-3: dev num, 15-8: bus num) */
741 	atcs_input.client_id = pci_dev_id(adev->pdev);
742 	atcs_input.valid_flags_mask = ATCS_VALID_FLAGS_MASK;
743 	atcs_input.flags = ATCS_WAIT_FOR_COMPLETION;
744 	if (advertise)
745 		atcs_input.flags |= ATCS_ADVERTISE_CAPS;
746 	atcs_input.req_type = ATCS_PCIE_LINK_SPEED;
747 	atcs_input.perf_req = perf_req;
748 
749 	params.length = sizeof(struct atcs_pref_req_input);
750 	params.pointer = &atcs_input;
751 
752 	while (retry--) {
753 		info = amdgpu_atcs_call(atcs, ATCS_FUNCTION_PCIE_PERFORMANCE_REQUEST, &params);
754 		if (!info)
755 			return -EIO;
756 
757 		memset(&atcs_output, 0, sizeof(atcs_output));
758 
759 		size = *(u16 *) info->buffer.pointer;
760 		if (size < 3) {
761 			drm_info(adev_to_drm(adev),
762 				"ATCS buffer is too small: %zu\n", size);
763 			kfree(info);
764 			return -EINVAL;
765 		}
766 		size = min(sizeof(atcs_output), size);
767 
768 		memcpy(&atcs_output, info->buffer.pointer, size);
769 
770 		kfree(info);
771 
772 		switch (atcs_output.ret_val) {
773 		case ATCS_REQUEST_REFUSED:
774 		default:
775 			return -EINVAL;
776 		case ATCS_REQUEST_COMPLETE:
777 			return 0;
778 		case ATCS_REQUEST_IN_PROGRESS:
779 			udelay(10);
780 			break;
781 		}
782 	}
783 
784 	return 0;
785 }
786 
787 /**
788  * amdgpu_acpi_power_shift_control
789  *
790  * @adev: amdgpu_device pointer
791  * @dev_state: device acpi state
792  * @drv_state: driver state
793  *
794  * Executes the POWER_SHIFT_CONTROL method to
795  * communicate current dGPU device state and
796  * driver state to APU/SBIOS.
797  * returns 0 on success, error on failure.
798  */
799 int amdgpu_acpi_power_shift_control(struct amdgpu_device *adev,
800 				    u8 dev_state, bool drv_state)
801 {
802 	union acpi_object *info;
803 	struct amdgpu_atcs *atcs = &amdgpu_acpi_priv.atcs;
804 	struct atcs_pwr_shift_input atcs_input;
805 	struct acpi_buffer params;
806 
807 	if (!amdgpu_acpi_is_power_shift_control_supported())
808 		return -EINVAL;
809 
810 	atcs_input.size = sizeof(struct atcs_pwr_shift_input);
811 	/* dGPU id (bit 2-0: func num, 7-3: dev num, 15-8: bus num) */
812 	atcs_input.dgpu_id = pci_dev_id(adev->pdev);
813 	atcs_input.dev_acpi_state = dev_state;
814 	atcs_input.drv_state = drv_state;
815 
816 	params.length = sizeof(struct atcs_pwr_shift_input);
817 	params.pointer = &atcs_input;
818 
819 	info = amdgpu_atcs_call(atcs, ATCS_FUNCTION_POWER_SHIFT_CONTROL, &params);
820 	if (!info) {
821 		drm_err(adev_to_drm(adev), "ATCS PSC call failed\n");
822 		return -EIO;
823 	}
824 
825 	kfree(info);
826 	return 0;
827 }
828 
829 /**
830  * amdgpu_acpi_smart_shift_update - update dGPU device state to SBIOS
831  *
832  * @adev: amdgpu device pointer
833  * @ss_state: current smart shift event
834  *
835  * returns 0 on success,
836  * otherwise return error number.
837  */
838 int amdgpu_acpi_smart_shift_update(struct amdgpu_device *adev,
839 				   enum amdgpu_ss ss_state)
840 {
841 	int r;
842 
843 	if (!amdgpu_device_supports_smart_shift(adev))
844 		return 0;
845 
846 	switch (ss_state) {
847 	/* SBIOS trigger “stop”, “enable” and “start” at D0, Driver Operational.
848 	 * SBIOS trigger “stop” at D3, Driver Not Operational.
849 	 * SBIOS trigger “stop” and “disable” at D0, Driver NOT operational.
850 	 */
851 	case AMDGPU_SS_DRV_LOAD:
852 		r = amdgpu_acpi_power_shift_control(adev,
853 						    AMDGPU_ATCS_PSC_DEV_STATE_D0,
854 						    AMDGPU_ATCS_PSC_DRV_STATE_OPR);
855 		break;
856 	case AMDGPU_SS_DEV_D0:
857 		r = amdgpu_acpi_power_shift_control(adev,
858 						    AMDGPU_ATCS_PSC_DEV_STATE_D0,
859 						    AMDGPU_ATCS_PSC_DRV_STATE_OPR);
860 		break;
861 	case AMDGPU_SS_DEV_D3:
862 		r = amdgpu_acpi_power_shift_control(adev,
863 						    AMDGPU_ATCS_PSC_DEV_STATE_D3_HOT,
864 						    AMDGPU_ATCS_PSC_DRV_STATE_NOT_OPR);
865 		break;
866 	case AMDGPU_SS_DRV_UNLOAD:
867 		r = amdgpu_acpi_power_shift_control(adev,
868 						    AMDGPU_ATCS_PSC_DEV_STATE_D0,
869 						    AMDGPU_ATCS_PSC_DRV_STATE_NOT_OPR);
870 		break;
871 	default:
872 		return -EINVAL;
873 	}
874 
875 	return r;
876 }
877 
878 #ifdef CONFIG_ACPI_NUMA
879 static inline uint64_t amdgpu_acpi_get_numa_size(int nid)
880 {
881 	/* This is directly using si_meminfo_node implementation as the
882 	 * function is not exported.
883 	 */
884 	int zone_type;
885 	uint64_t managed_pages = 0;
886 
887 	pg_data_t *pgdat = NODE_DATA(nid);
888 
889 	for (zone_type = 0; zone_type < MAX_NR_ZONES; zone_type++)
890 		managed_pages +=
891 			zone_managed_pages(&pgdat->node_zones[zone_type]);
892 	return managed_pages * PAGE_SIZE;
893 }
894 
895 static struct amdgpu_numa_info *amdgpu_acpi_get_numa_info(uint32_t pxm)
896 {
897 	struct amdgpu_numa_info *numa_info;
898 	int nid;
899 
900 	numa_info = xa_load(&numa_info_xa, pxm);
901 
902 	if (!numa_info) {
903 		struct sysinfo info;
904 
905 		numa_info = kzalloc_obj(*numa_info);
906 		if (!numa_info)
907 			return NULL;
908 
909 		nid = pxm_to_node(pxm);
910 		numa_info->pxm = pxm;
911 		numa_info->nid = nid;
912 
913 		if (numa_info->nid == NUMA_NO_NODE) {
914 			si_meminfo(&info);
915 			numa_info->size = info.totalram * info.mem_unit;
916 		} else {
917 			numa_info->size = amdgpu_acpi_get_numa_size(nid);
918 		}
919 		xa_store(&numa_info_xa, numa_info->pxm, numa_info, GFP_KERNEL);
920 	}
921 
922 	return numa_info;
923 }
924 #endif
925 
926 /**
927  * amdgpu_acpi_set_uma_allocation_size - Set Unified Memory Architecture allocation size via ACPI
928  * @adev: Pointer to the amdgpu_device structure
929  * @index: Index specifying the UMA allocation
930  * @type: Type of UMA allocation
931  *
932  * This function configures the UMA allocation size for the specified device
933  * using ACPI methods. The allocation is determined by the provided index and type.
934  * Returns 0 on success or a negative error code on failure.
935  */
936 int amdgpu_acpi_set_uma_allocation_size(struct amdgpu_device *adev, u8 index, u8 type)
937 {
938 	struct atcs_set_uma_allocation_size_input atcs_input;
939 	struct amdgpu_atcs *atcs = &amdgpu_acpi_priv.atcs;
940 	struct acpi_buffer params;
941 	union acpi_object *info;
942 
943 	if (!amdgpu_acpi_is_set_uma_allocation_size_supported())
944 		return -EINVAL;
945 
946 	atcs_input.size = sizeof(struct atcs_set_uma_allocation_size_input);
947 	atcs_input.uma_size_index = index;
948 	atcs_input.uma_size_type = type;
949 
950 	params.length = sizeof(struct atcs_set_uma_allocation_size_input);
951 	params.pointer = &atcs_input;
952 
953 	info = amdgpu_atcs_call(atcs, ATCS_FUNCTION_SET_UMA_ALLOCATION_SIZE, &params);
954 	if (!info) {
955 		drm_err(adev_to_drm(adev), "ATCS UMA allocation size update failed\n");
956 		return -EIO;
957 	}
958 
959 	kfree(info);
960 
961 	return 0;
962 }
963 
964 /**
965  * amdgpu_acpi_get_node_id - obtain the NUMA node id for corresponding amdgpu
966  * acpi device handle
967  *
968  * @handle: acpi handle
969  * @numa_info: amdgpu_numa_info structure holding numa information
970  *
971  * Queries the ACPI interface to fetch the corresponding NUMA Node ID for a
972  * given amdgpu acpi device.
973  *
974  * Returns ACPI STATUS OK with Node ID on success or the corresponding failure reason
975  */
976 static acpi_status amdgpu_acpi_get_node_id(acpi_handle handle,
977 				    struct amdgpu_numa_info **numa_info)
978 {
979 #ifdef CONFIG_ACPI_NUMA
980 	u64 pxm;
981 	acpi_status status;
982 
983 	if (!numa_info)
984 		return_ACPI_STATUS(AE_ERROR);
985 
986 	status = acpi_evaluate_integer(handle, "_PXM", NULL, &pxm);
987 
988 	if (ACPI_FAILURE(status))
989 		return status;
990 
991 	*numa_info = amdgpu_acpi_get_numa_info(pxm);
992 
993 	if (!*numa_info)
994 		return_ACPI_STATUS(AE_ERROR);
995 
996 	return_ACPI_STATUS(AE_OK);
997 #else
998 	return_ACPI_STATUS(AE_NOT_EXIST);
999 #endif
1000 }
1001 
1002 static struct amdgpu_acpi_dev_info *amdgpu_acpi_get_dev(u32 sbdf)
1003 {
1004 	struct amdgpu_acpi_dev_info *acpi_dev;
1005 
1006 	if (list_empty(&amdgpu_acpi_dev_list))
1007 		return NULL;
1008 
1009 	list_for_each_entry(acpi_dev, &amdgpu_acpi_dev_list, list)
1010 		if (acpi_dev->sbdf == sbdf)
1011 			return acpi_dev;
1012 
1013 	return NULL;
1014 }
1015 
1016 static int amdgpu_acpi_dev_init(struct amdgpu_acpi_dev_info **dev_info,
1017 				struct amdgpu_acpi_xcc_info *xcc_info, u32 sbdf)
1018 {
1019 	struct amdgpu_acpi_dev_info *tmp;
1020 	union acpi_object *obj;
1021 	int ret = -ENOENT;
1022 
1023 	*dev_info = NULL;
1024 	tmp = kzalloc_obj(struct amdgpu_acpi_dev_info);
1025 	if (!tmp)
1026 		return -ENOMEM;
1027 
1028 	INIT_LIST_HEAD(&tmp->xcc_list);
1029 	INIT_LIST_HEAD(&tmp->list);
1030 	tmp->sbdf = sbdf;
1031 
1032 	obj = acpi_evaluate_dsm_typed(xcc_info->handle, &amd_xcc_dsm_guid, 0,
1033 				      AMD_XCC_DSM_GET_SUPP_MODE, NULL,
1034 				      ACPI_TYPE_INTEGER);
1035 
1036 	if (!obj) {
1037 		acpi_handle_debug(xcc_info->handle,
1038 				  "_DSM function %d evaluation failed",
1039 				  AMD_XCC_DSM_GET_SUPP_MODE);
1040 		ret = -ENOENT;
1041 		goto out;
1042 	}
1043 
1044 	tmp->supp_xcp_mode = obj->integer.value & 0xFFFF;
1045 	ACPI_FREE(obj);
1046 
1047 	obj = acpi_evaluate_dsm_typed(xcc_info->handle, &amd_xcc_dsm_guid, 0,
1048 				      AMD_XCC_DSM_GET_XCP_MODE, NULL,
1049 				      ACPI_TYPE_INTEGER);
1050 
1051 	if (!obj) {
1052 		acpi_handle_debug(xcc_info->handle,
1053 				  "_DSM function %d evaluation failed",
1054 				  AMD_XCC_DSM_GET_XCP_MODE);
1055 		ret = -ENOENT;
1056 		goto out;
1057 	}
1058 
1059 	tmp->xcp_mode = obj->integer.value & 0xFFFF;
1060 	tmp->mem_mode = (obj->integer.value >> 32) & 0xFFFF;
1061 	ACPI_FREE(obj);
1062 
1063 	/* Evaluate DSMs and fill XCC information */
1064 	obj = acpi_evaluate_dsm_typed(xcc_info->handle, &amd_xcc_dsm_guid, 0,
1065 				      AMD_XCC_DSM_GET_TMR_INFO, NULL,
1066 				      ACPI_TYPE_PACKAGE);
1067 
1068 	if (!obj || obj->package.count < 2) {
1069 		acpi_handle_debug(xcc_info->handle,
1070 				  "_DSM function %d evaluation failed",
1071 				  AMD_XCC_DSM_GET_TMR_INFO);
1072 		ret = -ENOENT;
1073 		goto out;
1074 	}
1075 
1076 	tmp->tmr_base = obj->package.elements[0].integer.value;
1077 	tmp->tmr_size = obj->package.elements[1].integer.value;
1078 	ACPI_FREE(obj);
1079 
1080 	DRM_DEBUG_DRIVER(
1081 		"New dev(%x): Supported xcp mode: %x curr xcp_mode : %x mem mode : %x, tmr base: %llx tmr size: %llx  ",
1082 		tmp->sbdf, tmp->supp_xcp_mode, tmp->xcp_mode, tmp->mem_mode,
1083 		tmp->tmr_base, tmp->tmr_size);
1084 	list_add_tail(&tmp->list, &amdgpu_acpi_dev_list);
1085 	*dev_info = tmp;
1086 
1087 	return 0;
1088 
1089 out:
1090 	if (obj)
1091 		ACPI_FREE(obj);
1092 	kfree(tmp);
1093 
1094 	return ret;
1095 }
1096 
1097 static int amdgpu_acpi_get_xcc_info(struct amdgpu_acpi_xcc_info *xcc_info,
1098 				    u32 *sbdf)
1099 {
1100 	union acpi_object *obj;
1101 	acpi_status status;
1102 	int ret = -ENOENT;
1103 
1104 	obj = acpi_evaluate_dsm_typed(xcc_info->handle, &amd_xcc_dsm_guid, 0,
1105 				      AMD_XCC_DSM_GET_NUM_FUNCS, NULL,
1106 				      ACPI_TYPE_INTEGER);
1107 
1108 	if (!obj || obj->integer.value != AMD_XCC_DSM_NUM_FUNCS)
1109 		goto out;
1110 	ACPI_FREE(obj);
1111 
1112 	/* Evaluate DSMs and fill XCC information */
1113 	obj = acpi_evaluate_dsm_typed(xcc_info->handle, &amd_xcc_dsm_guid, 0,
1114 				      AMD_XCC_DSM_GET_VF_XCC_MAPPING, NULL,
1115 				      ACPI_TYPE_INTEGER);
1116 
1117 	if (!obj) {
1118 		acpi_handle_debug(xcc_info->handle,
1119 				  "_DSM function %d evaluation failed",
1120 				  AMD_XCC_DSM_GET_VF_XCC_MAPPING);
1121 		ret = -EINVAL;
1122 		goto out;
1123 	}
1124 
1125 	/* PF xcc id [39:32] */
1126 	xcc_info->phy_id = (obj->integer.value >> 32) & 0xFF;
1127 	/* xcp node of this xcc [47:40] */
1128 	xcc_info->xcp_node = (obj->integer.value >> 40) & 0xFF;
1129 	/* PF domain of this xcc [31:16] */
1130 	*sbdf = (obj->integer.value) & 0xFFFF0000;
1131 	/* PF bus/dev/fn of this xcc [63:48] */
1132 	*sbdf |= (obj->integer.value >> 48) & 0xFFFF;
1133 	ACPI_FREE(obj);
1134 	obj = NULL;
1135 
1136 	status =
1137 		amdgpu_acpi_get_node_id(xcc_info->handle, &xcc_info->numa_info);
1138 
1139 	/* TODO: check if this check is required */
1140 	if (ACPI_SUCCESS(status))
1141 		ret = 0;
1142 out:
1143 	if (obj)
1144 		ACPI_FREE(obj);
1145 
1146 	return ret;
1147 }
1148 
1149 static noinline_for_stack
1150 int amdgpu_acpi_enumerate_xcc(void)
1151 {
1152 	struct amdgpu_acpi_dev_info *dev_info = NULL;
1153 	struct amdgpu_acpi_xcc_info *xcc_info;
1154 	struct acpi_device *acpi_dev;
1155 	char hid[ACPI_ID_LEN];
1156 	int ret, id;
1157 	u32 sbdf;
1158 
1159 	INIT_LIST_HEAD(&amdgpu_acpi_dev_list);
1160 	xa_init(&numa_info_xa);
1161 
1162 	for (id = 0; id < AMD_XCC_MAX_HID; id++) {
1163 		sprintf(hid, "%s%d", "AMD", AMD_XCC_HID_START + id);
1164 		acpi_dev = acpi_dev_get_first_match_dev(hid, NULL, -1);
1165 		/* These ACPI objects are expected to be in sequential order. If
1166 		 * one is not found, no need to check the rest.
1167 		 */
1168 		if (!acpi_dev) {
1169 			DRM_DEBUG_DRIVER("No matching acpi device found for %s\n",
1170 					 hid);
1171 			break;
1172 		}
1173 
1174 		xcc_info = kzalloc_obj(struct amdgpu_acpi_xcc_info);
1175 		if (!xcc_info) {
1176 			acpi_dev_put(acpi_dev);
1177 			return -ENOMEM;
1178 		}
1179 
1180 		INIT_LIST_HEAD(&xcc_info->list);
1181 		xcc_info->handle = acpi_device_handle(acpi_dev);
1182 		acpi_dev_put(acpi_dev);
1183 
1184 		ret = amdgpu_acpi_get_xcc_info(xcc_info, &sbdf);
1185 		if (ret) {
1186 			kfree(xcc_info);
1187 			continue;
1188 		}
1189 
1190 		dev_info = amdgpu_acpi_get_dev(sbdf);
1191 
1192 		if (!dev_info)
1193 			ret = amdgpu_acpi_dev_init(&dev_info, xcc_info, sbdf);
1194 
1195 		if (ret == -ENOMEM) {
1196 			kfree(xcc_info);
1197 			return ret;
1198 		}
1199 
1200 		if (!dev_info) {
1201 			kfree(xcc_info);
1202 			continue;
1203 		}
1204 
1205 		list_add_tail(&xcc_info->list, &dev_info->xcc_list);
1206 	}
1207 
1208 	return 0;
1209 }
1210 
1211 int amdgpu_acpi_get_tmr_info(struct amdgpu_device *adev, u64 *tmr_offset,
1212 			     u64 *tmr_size)
1213 {
1214 	struct amdgpu_acpi_dev_info *dev_info;
1215 	u32 sbdf;
1216 
1217 	if (!tmr_offset || !tmr_size)
1218 		return -EINVAL;
1219 
1220 	sbdf = (pci_domain_nr(adev->pdev->bus) << 16);
1221 	sbdf |= pci_dev_id(adev->pdev);
1222 	dev_info = amdgpu_acpi_get_dev(sbdf);
1223 	if (!dev_info)
1224 		return -ENOENT;
1225 
1226 	*tmr_offset = dev_info->tmr_base;
1227 	*tmr_size = dev_info->tmr_size;
1228 
1229 	return 0;
1230 }
1231 
1232 int amdgpu_acpi_get_mem_info(struct amdgpu_device *adev, int xcc_id,
1233 			     struct amdgpu_numa_info *numa_info)
1234 {
1235 	struct amdgpu_acpi_dev_info *dev_info;
1236 	struct amdgpu_acpi_xcc_info *xcc_info;
1237 	u32 sbdf;
1238 
1239 	if (!numa_info)
1240 		return -EINVAL;
1241 
1242 	sbdf = (pci_domain_nr(adev->pdev->bus) << 16);
1243 	sbdf |= pci_dev_id(adev->pdev);
1244 	dev_info = amdgpu_acpi_get_dev(sbdf);
1245 	if (!dev_info)
1246 		return -ENOENT;
1247 
1248 	list_for_each_entry(xcc_info, &dev_info->xcc_list, list) {
1249 		if (xcc_info->phy_id == xcc_id) {
1250 			memcpy(numa_info, xcc_info->numa_info,
1251 			       sizeof(*numa_info));
1252 			return 0;
1253 		}
1254 	}
1255 
1256 	return -ENOENT;
1257 }
1258 
1259 /**
1260  * amdgpu_acpi_event - handle notify events
1261  *
1262  * @nb: notifier block
1263  * @val: val
1264  * @data: acpi event
1265  *
1266  * Calls relevant amdgpu functions in response to various
1267  * acpi events.
1268  * Returns NOTIFY code
1269  */
1270 static int amdgpu_acpi_event(struct notifier_block *nb,
1271 			     unsigned long val,
1272 			     void *data)
1273 {
1274 	struct amdgpu_device *adev = container_of(nb, struct amdgpu_device, acpi_nb);
1275 	struct acpi_bus_event *entry = (struct acpi_bus_event *)data;
1276 
1277 	if (strcmp(entry->device_class, ACPI_AC_CLASS) == 0) {
1278 		if (power_supply_is_system_supplied() > 0)
1279 			DRM_DEBUG_DRIVER("pm: AC\n");
1280 		else
1281 			DRM_DEBUG_DRIVER("pm: DC\n");
1282 
1283 		amdgpu_pm_acpi_event_handler(adev);
1284 	}
1285 
1286 	/* Check for pending SBIOS requests */
1287 	return amdgpu_atif_handler(adev, entry);
1288 }
1289 
1290 /* Call all ACPI methods here */
1291 /**
1292  * amdgpu_acpi_init - init driver acpi support
1293  *
1294  * @adev: amdgpu_device pointer
1295  *
1296  * Verifies the AMD ACPI interfaces and registers with the acpi
1297  * notifier chain (all asics).
1298  * Returns 0 on success, error on failure.
1299  */
1300 int amdgpu_acpi_init(struct amdgpu_device *adev)
1301 {
1302 	struct amdgpu_atif *atif = &amdgpu_acpi_priv.atif;
1303 
1304 	if (atif->notifications.brightness_change) {
1305 		if (adev->dc_enabled) {
1306 #if defined(CONFIG_DRM_AMD_DC)
1307 			struct amdgpu_display_manager *dm = &adev->dm;
1308 
1309 			if (dm->backlight_dev[0])
1310 				atif->bd = dm->backlight_dev[0];
1311 #endif
1312 		} else {
1313 			struct drm_encoder *tmp;
1314 
1315 			/* Find the encoder controlling the brightness */
1316 			list_for_each_entry(tmp, &adev_to_drm(adev)->mode_config.encoder_list,
1317 					    head) {
1318 				struct amdgpu_encoder *enc = to_amdgpu_encoder(tmp);
1319 
1320 				if ((enc->devices & (ATOM_DEVICE_LCD_SUPPORT)) &&
1321 				    enc->enc_priv) {
1322 					struct amdgpu_encoder_atom_dig *dig = enc->enc_priv;
1323 
1324 					if (dig->bl_dev) {
1325 						atif->bd = dig->bl_dev;
1326 						break;
1327 					}
1328 				}
1329 			}
1330 		}
1331 	}
1332 	adev->acpi_nb.notifier_call = amdgpu_acpi_event;
1333 	register_acpi_notifier(&adev->acpi_nb);
1334 
1335 	return 0;
1336 }
1337 
1338 void amdgpu_acpi_get_backlight_caps(struct amdgpu_dm_backlight_caps *caps)
1339 {
1340 	struct amdgpu_atif *atif = &amdgpu_acpi_priv.atif;
1341 
1342 	memcpy(caps, &atif->backlight_caps, sizeof(*caps));
1343 }
1344 
1345 /**
1346  * amdgpu_acpi_fini - tear down driver acpi support
1347  *
1348  * @adev: amdgpu_device pointer
1349  *
1350  * Unregisters with the acpi notifier chain (all asics).
1351  */
1352 void amdgpu_acpi_fini(struct amdgpu_device *adev)
1353 {
1354 	unregister_acpi_notifier(&adev->acpi_nb);
1355 }
1356 
1357 /**
1358  * amdgpu_atif_pci_probe_handle - look up the ATIF handle
1359  *
1360  * @pdev: pci device
1361  *
1362  * Look up the ATIF handles (all asics).
1363  * Returns true if the handle is found, false if not.
1364  */
1365 static bool amdgpu_atif_pci_probe_handle(struct pci_dev *pdev)
1366 {
1367 	char acpi_method_name[255] = { 0 };
1368 	struct acpi_buffer buffer = {sizeof(acpi_method_name), acpi_method_name};
1369 	acpi_handle dhandle, atif_handle;
1370 	acpi_status status;
1371 	int ret;
1372 
1373 	dhandle = ACPI_HANDLE(&pdev->dev);
1374 	if (!dhandle)
1375 		return false;
1376 
1377 	status = acpi_get_handle(dhandle, "ATIF", &atif_handle);
1378 	if (ACPI_FAILURE(status))
1379 		return false;
1380 
1381 	amdgpu_acpi_priv.atif.handle = atif_handle;
1382 	acpi_get_name(amdgpu_acpi_priv.atif.handle, ACPI_FULL_PATHNAME, &buffer);
1383 	DRM_DEBUG_DRIVER("Found ATIF handle %s\n", acpi_method_name);
1384 	ret = amdgpu_atif_verify_interface(&amdgpu_acpi_priv.atif);
1385 	if (ret) {
1386 		amdgpu_acpi_priv.atif.handle = 0;
1387 		return false;
1388 	}
1389 	return true;
1390 }
1391 
1392 /**
1393  * amdgpu_atcs_pci_probe_handle - look up the ATCS handle
1394  *
1395  * @pdev: pci device
1396  *
1397  * Look up the ATCS handles (all asics).
1398  * Returns true if the handle is found, false if not.
1399  */
1400 static bool amdgpu_atcs_pci_probe_handle(struct pci_dev *pdev)
1401 {
1402 	char acpi_method_name[255] = { 0 };
1403 	struct acpi_buffer buffer = { sizeof(acpi_method_name), acpi_method_name };
1404 	acpi_handle dhandle, atcs_handle;
1405 	acpi_status status;
1406 	int ret;
1407 
1408 	dhandle = ACPI_HANDLE(&pdev->dev);
1409 	if (!dhandle)
1410 		return false;
1411 
1412 	status = acpi_get_handle(dhandle, "ATCS", &atcs_handle);
1413 	if (ACPI_FAILURE(status))
1414 		return false;
1415 
1416 	amdgpu_acpi_priv.atcs.handle = atcs_handle;
1417 	acpi_get_name(amdgpu_acpi_priv.atcs.handle, ACPI_FULL_PATHNAME, &buffer);
1418 	DRM_DEBUG_DRIVER("Found ATCS handle %s\n", acpi_method_name);
1419 	ret = amdgpu_atcs_verify_interface(&amdgpu_acpi_priv.atcs);
1420 	if (ret) {
1421 		amdgpu_acpi_priv.atcs.handle = 0;
1422 		return false;
1423 	}
1424 	return true;
1425 }
1426 
1427 
1428 /**
1429  * amdgpu_acpi_should_gpu_reset
1430  *
1431  * @adev: amdgpu_device_pointer
1432  *
1433  * returns true if should reset GPU, false if not
1434  */
1435 bool amdgpu_acpi_should_gpu_reset(struct amdgpu_device *adev)
1436 {
1437 	if ((adev->flags & AMD_IS_APU) &&
1438 	    adev->gfx.imu.funcs) /* Not need to do mode2 reset for IMU enabled APUs */
1439 		return false;
1440 
1441 	if ((adev->flags & AMD_IS_APU) &&
1442 	    amdgpu_acpi_is_s3_active(adev))
1443 		return false;
1444 
1445 	if (amdgpu_sriov_vf(adev))
1446 		return false;
1447 
1448 #if IS_ENABLED(CONFIG_SUSPEND)
1449 	return pm_suspend_target_state != PM_SUSPEND_TO_IDLE;
1450 #else
1451 	return true;
1452 #endif
1453 }
1454 
1455 #ifdef HAVE_ACPI_SRAT_GENERIC_AFFINITY
1456 static bool amdgpu_acpi_srat_pci_handle_match(struct pci_dev *pdev,
1457 					      struct acpi_srat_generic_affinity *gi)
1458 {
1459 	u16 segment = *(u16 *)&gi->device_handle[0];
1460 	u8 bus = gi->device_handle[2];
1461 	u8 devfn = gi->device_handle[3];
1462 
1463 	/* Only PCI device handles (ACPI 6.3: type 1; some firmware used 0) */
1464 	if (gi->device_handle_type != 0 && gi->device_handle_type != 1)
1465 		return false;
1466 
1467 	if (!(gi->flags & ACPI_SRAT_GENERIC_AFFINITY_ENABLED))
1468 		return false;
1469 
1470 	/* ACPI 6.3 Table 5-80: segment + bus + devfn bytes */
1471 	return segment == pci_domain_nr(pdev->bus) &&
1472 	       bus == pdev->bus->number &&
1473 	       devfn == pdev->devfn;
1474 }
1475 
1476 int amdgpu_acpi_find_gpu_memory_in_srat(struct amdgpu_device *adev,
1477 					uint64_t *base_addr, uint64_t *length)
1478 {
1479 	struct acpi_srat_generic_affinity *gpu;
1480 	struct acpi_subtable_header *sub_header;
1481 	struct acpi_table_header *table_header;
1482 	struct acpi_srat_mem_affinity *mem;
1483 	struct pci_dev *pdev = adev->pdev;
1484 	unsigned long subtable_len;
1485 	unsigned long table_end;
1486 	acpi_status status;
1487 	bool pxm_found = false;
1488 	bool mem_found = false;
1489 	u64 mem_base = 0;
1490 	u64 mem_len = 0;
1491 	u32 gpu_pxm = 0;
1492 
1493 	if (!base_addr || !length)
1494 		return -EINVAL;
1495 
1496 	status = acpi_get_table(ACPI_SIG_SRAT, 0, &table_header);
1497 	if (ACPI_FAILURE(status))
1498 		return -ENOENT;
1499 
1500 	table_end = (unsigned long)table_header + table_header->length;
1501 
1502 	/* Pass 1: find GPU PXM from GENERIC_AFFINITY */
1503 	sub_header = (struct acpi_subtable_header *)
1504 		((unsigned long)table_header + sizeof(struct acpi_table_srat));
1505 	subtable_len = sub_header->length;
1506 
1507 	while (((unsigned long)sub_header) + subtable_len <= table_end) {
1508 		if (subtable_len == 0)
1509 			break;
1510 
1511 		if (sub_header->type == ACPI_SRAT_TYPE_GENERIC_AFFINITY) {
1512 			gpu = (struct acpi_srat_generic_affinity *)sub_header;
1513 			if (amdgpu_acpi_srat_pci_handle_match(pdev, gpu)) {
1514 				gpu_pxm = gpu->proximity_domain;
1515 				pxm_found = true;
1516 				break;
1517 			}
1518 		}
1519 
1520 		sub_header = (struct acpi_subtable_header *)
1521 			((unsigned long)sub_header + subtable_len);
1522 		subtable_len = sub_header->length;
1523 	}
1524 
1525 	if (!pxm_found) {
1526 		acpi_put_table(table_header);
1527 		dev_err(adev->dev, "SRAT: no acpi generic initiator found\n");
1528 		return -ENOENT;
1529 	}
1530 
1531 	/* Pass 2: find SPM memory for gpu_pxm */
1532 	sub_header = (struct acpi_subtable_header *)
1533 		((unsigned long)table_header + sizeof(struct acpi_table_srat));
1534 	subtable_len = sub_header->length;
1535 
1536 	while (((unsigned long)sub_header) + subtable_len <= table_end) {
1537 		if (subtable_len == 0)
1538 			break;
1539 
1540 		if (sub_header->type == ACPI_SRAT_TYPE_MEMORY_AFFINITY) {
1541 			bool enabled, hotplug;
1542 
1543 			mem = (struct acpi_srat_mem_affinity *)sub_header;
1544 			enabled = mem->flags & ACPI_SRAT_MEM_ENABLED;
1545 			hotplug = mem->flags & ACPI_SRAT_MEM_HOT_PLUGGABLE;
1546 
1547 			/* MUST filter ENABLED && !HOTPLUGGABLE */
1548 			if (enabled && !hotplug &&
1549 			    mem->proximity_domain == gpu_pxm) {
1550 				/* [mem->base_address, mem->length] is the SPM range */
1551 				mem_base = mem->base_address;
1552 				mem_len = mem->length;
1553 				mem_found = true;
1554 				break;
1555 			}
1556 		}
1557 
1558 		sub_header = (struct acpi_subtable_header *)
1559 			((unsigned long)sub_header + subtable_len);
1560 		subtable_len = sub_header->length;
1561 	}
1562 
1563 	acpi_put_table(table_header);
1564 
1565 	if (!mem_found) {
1566 		dev_err(adev->dev, "SRAT: no memory affinity for pxm %u\n",
1567 			 gpu_pxm);
1568 		return -ENOENT;
1569 	}
1570 
1571 	dev_info(adev->dev, "SRAT: gpu pxm=%u base=%llx length=%llx\n",
1572 		 gpu_pxm, mem_base, mem_len);
1573 
1574 	*base_addr = mem_base;
1575 	*length = mem_len;
1576 
1577 	return 0;
1578 }
1579 #else
1580 int amdgpu_acpi_find_gpu_memory_in_srat(struct amdgpu_device *adev,
1581 					uint64_t *base_addr, uint64_t *length)
1582 {
1583 	if (!base_addr || !length)
1584 		return -EINVAL;
1585 
1586 	return -ENOENT;
1587 }
1588 #endif
1589 
1590 /*
1591  * amdgpu_acpi_detect - detect ACPI ATIF/ATCS methods
1592  *
1593  * Check if we have the ATIF/ATCS methods and populate
1594  * the structures in the driver.
1595  */
1596 void amdgpu_acpi_detect(void)
1597 {
1598 	struct amdgpu_atif *atif = &amdgpu_acpi_priv.atif;
1599 	struct amdgpu_atcs *atcs = &amdgpu_acpi_priv.atcs;
1600 	struct pci_dev *pdev = NULL;
1601 	int ret;
1602 
1603 	while ((pdev = pci_get_base_class(PCI_BASE_CLASS_DISPLAY, pdev))) {
1604 		if ((pdev->class != PCI_CLASS_DISPLAY_VGA << 8) &&
1605 		    (pdev->class != PCI_CLASS_DISPLAY_OTHER << 8))
1606 			continue;
1607 
1608 		if (!atif->handle)
1609 			amdgpu_atif_pci_probe_handle(pdev);
1610 		if (!atcs->handle)
1611 			amdgpu_atcs_pci_probe_handle(pdev);
1612 	}
1613 
1614 	if (atif->functions.sbios_requests && !atif->functions.system_params) {
1615 		/* XXX check this workraround, if sbios request function is
1616 		 * present we have to see how it's configured in the system
1617 		 * params
1618 		 */
1619 		atif->functions.system_params = true;
1620 	}
1621 
1622 	if (atif->functions.system_params) {
1623 		ret = amdgpu_atif_get_notification_params(atif);
1624 		if (ret) {
1625 			DRM_DEBUG_DRIVER("Call to GET_SYSTEM_PARAMS failed: %d\n",
1626 					ret);
1627 			/* Disable notification */
1628 			atif->notification_cfg.enabled = false;
1629 		}
1630 	}
1631 
1632 	if (atif->functions.query_backlight_transfer_characteristics) {
1633 		ret = amdgpu_atif_query_backlight_caps(atif);
1634 		if (ret) {
1635 			DRM_DEBUG_DRIVER("Call to QUERY_BACKLIGHT_TRANSFER_CHARACTERISTICS failed: %d\n",
1636 					ret);
1637 			atif->backlight_caps.caps_valid = false;
1638 		}
1639 	} else {
1640 		atif->backlight_caps.caps_valid = false;
1641 	}
1642 
1643 	amdgpu_acpi_enumerate_xcc();
1644 }
1645 
1646 void amdgpu_acpi_release(void)
1647 {
1648 	struct amdgpu_acpi_dev_info *dev_info, *dev_tmp;
1649 	struct amdgpu_acpi_xcc_info *xcc_info, *xcc_tmp;
1650 	struct amdgpu_numa_info *numa_info;
1651 	unsigned long index;
1652 
1653 	xa_for_each(&numa_info_xa, index, numa_info) {
1654 		kfree(numa_info);
1655 		xa_erase(&numa_info_xa, index);
1656 	}
1657 
1658 	if (list_empty(&amdgpu_acpi_dev_list))
1659 		return;
1660 
1661 	list_for_each_entry_safe(dev_info, dev_tmp, &amdgpu_acpi_dev_list,
1662 				 list) {
1663 		list_for_each_entry_safe(xcc_info, xcc_tmp, &dev_info->xcc_list,
1664 					 list) {
1665 			list_del(&xcc_info->list);
1666 			kfree(xcc_info);
1667 		}
1668 
1669 		list_del(&dev_info->list);
1670 		kfree(dev_info);
1671 	}
1672 }
1673 
1674 #if IS_ENABLED(CONFIG_SUSPEND)
1675 /**
1676  * amdgpu_acpi_is_s3_active
1677  *
1678  * @adev: amdgpu_device_pointer
1679  *
1680  * returns true if supported, false if not.
1681  */
1682 bool amdgpu_acpi_is_s3_active(struct amdgpu_device *adev)
1683 {
1684 	return !(adev->flags & AMD_IS_APU) ||
1685 		(pm_suspend_target_state == PM_SUSPEND_MEM);
1686 }
1687 
1688 /**
1689  * amdgpu_acpi_is_s0ix_active
1690  *
1691  * @adev: amdgpu_device_pointer
1692  *
1693  * returns true if supported, false if not.
1694  */
1695 bool amdgpu_acpi_is_s0ix_active(struct amdgpu_device *adev)
1696 {
1697 	if (!(adev->flags & AMD_IS_APU) ||
1698 	    (pm_suspend_target_state != PM_SUSPEND_TO_IDLE))
1699 		return false;
1700 
1701 	if (adev->asic_type < CHIP_RAVEN)
1702 		return false;
1703 
1704 	if (!(adev->pm.pp_feature & PP_GFXOFF_MASK))
1705 		return false;
1706 
1707 	/*
1708 	 * If ACPI_FADT_LOW_POWER_S0 is not set in the FADT, it is generally
1709 	 * risky to do any special firmware-related preparations for entering
1710 	 * S0ix even though the system is suspending to idle, so return false
1711 	 * in that case.
1712 	 */
1713 	if (!(acpi_gbl_FADT.flags & ACPI_FADT_LOW_POWER_S0)) {
1714 		dev_err_once(adev->dev,
1715 			      "Power consumption will be higher as BIOS has not been configured for suspend-to-idle.\n"
1716 			      "To use suspend-to-idle change the sleep mode in BIOS setup.\n");
1717 		return false;
1718 	}
1719 
1720 #if !IS_ENABLED(CONFIG_AMD_PMC)
1721 	dev_err_once(adev->dev,
1722 		      "Power consumption will be higher as the kernel has not been compiled with CONFIG_AMD_PMC.\n");
1723 	return false;
1724 #else
1725 	return true;
1726 #endif /* CONFIG_AMD_PMC */
1727 }
1728 #endif /* CONFIG_SUSPEND */
1729 
1730 #if IS_ENABLED(CONFIG_DRM_AMD_ISP)
1731 static const struct acpi_device_id isp_sensor_ids[] = {
1732 	{ "OMNI5C10" },
1733 	{ }
1734 };
1735 
1736 static int isp_match_acpi_device_ids(struct device *dev, const void *data)
1737 {
1738 	return acpi_match_device(data, dev) ? 1 : 0;
1739 }
1740 
1741 int amdgpu_acpi_get_isp4_dev(struct acpi_device **dev)
1742 {
1743 	struct device *pdev __free(put_device) = NULL;
1744 	struct acpi_device *acpi_pdev;
1745 
1746 	pdev = bus_find_device(&platform_bus_type, NULL, isp_sensor_ids,
1747 			       isp_match_acpi_device_ids);
1748 	if (!pdev)
1749 		return -EINVAL;
1750 
1751 	acpi_pdev = ACPI_COMPANION(pdev);
1752 	if (!acpi_pdev)
1753 		return -ENODEV;
1754 
1755 	*dev = acpi_pdev;
1756 
1757 	return 0;
1758 }
1759 #endif /* CONFIG_DRM_AMD_ISP */
1760