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