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