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