xref: /openbsd/sys/dev/pci/drm/amd/amdgpu/amdgpu_acpi.c (revision 9ea232b5)
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 #ifdef notyet
44 static const guid_t amd_xcc_dsm_guid = GUID_INIT(0x8267f5d5, 0xa556, 0x44f2,
45 						 0xb8, 0xb4, 0x45, 0x56, 0x2e,
46 						 0x8c, 0x5b, 0xec);
47 #endif
48 
49 #define AMD_XCC_HID_START 3000
50 #define AMD_XCC_DSM_GET_NUM_FUNCS 0
51 #define AMD_XCC_DSM_GET_SUPP_MODE 1
52 #define AMD_XCC_DSM_GET_XCP_MODE 2
53 #define AMD_XCC_DSM_GET_VF_XCC_MAPPING 4
54 #define AMD_XCC_DSM_GET_TMR_INFO 5
55 #define AMD_XCC_DSM_NUM_FUNCS 5
56 
57 #define AMD_XCC_MAX_HID 24
58 
59 struct xarray numa_info_xa;
60 
61 /* Encapsulates the XCD acpi object information */
62 struct amdgpu_acpi_xcc_info {
63 	struct list_head list;
64 	struct amdgpu_numa_info *numa_info;
65 	uint8_t xcp_node;
66 	uint8_t phy_id;
67 	acpi_handle handle;
68 };
69 
70 struct amdgpu_acpi_dev_info {
71 	struct list_head list;
72 	struct list_head xcc_list;
73 	uint16_t bdf;
74 	uint16_t supp_xcp_mode;
75 	uint16_t xcp_mode;
76 	uint16_t mem_mode;
77 	uint64_t tmr_base;
78 	uint64_t tmr_size;
79 };
80 
81 struct list_head amdgpu_acpi_dev_list;
82 
83 struct amdgpu_atif_notification_cfg {
84 	bool enabled;
85 	int command_code;
86 };
87 
88 struct amdgpu_atif_notifications {
89 	bool thermal_state;
90 	bool forced_power_state;
91 	bool system_power_state;
92 	bool brightness_change;
93 	bool dgpu_display_event;
94 	bool gpu_package_power_limit;
95 };
96 
97 struct amdgpu_atif_functions {
98 	bool system_params;
99 	bool sbios_requests;
100 	bool temperature_change;
101 	bool query_backlight_transfer_characteristics;
102 	bool ready_to_undock;
103 	bool external_gpu_information;
104 };
105 
106 struct amdgpu_atif {
107 	acpi_handle handle;
108 
109 	struct amdgpu_atif_notifications notifications;
110 	struct amdgpu_atif_functions functions;
111 	struct amdgpu_atif_notification_cfg notification_cfg;
112 	struct backlight_device *bd;
113 	struct amdgpu_dm_backlight_caps backlight_caps;
114 };
115 
116 struct amdgpu_atcs_functions {
117 	bool get_ext_state;
118 	bool pcie_perf_req;
119 	bool pcie_dev_rdy;
120 	bool pcie_bus_width;
121 	bool power_shift_control;
122 };
123 
124 struct amdgpu_atcs {
125 	acpi_handle handle;
126 
127 	struct amdgpu_atcs_functions functions;
128 };
129 
130 static struct amdgpu_acpi_priv {
131 	struct amdgpu_atif atif;
132 	struct amdgpu_atcs atcs;
133 } amdgpu_acpi_priv;
134 
135 /* Call the ATIF method
136  */
137 /**
138  * amdgpu_atif_call - call an ATIF method
139  *
140  * @atif: atif structure
141  * @function: the ATIF function to execute
142  * @params: ATIF function params
143  *
144  * Executes the requested ATIF function (all asics).
145  * Returns a pointer to the acpi output buffer.
146  */
147 static union acpi_object *amdgpu_atif_call(struct amdgpu_atif *atif,
148 					   int function,
149 					   struct acpi_buffer *params)
150 {
151 	acpi_status status;
152 	union acpi_object atif_arg_elements[2];
153 	struct acpi_object_list atif_arg;
154 	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
155 
156 	atif_arg.count = 2;
157 	atif_arg.pointer = &atif_arg_elements[0];
158 
159 	atif_arg_elements[0].type = ACPI_TYPE_INTEGER;
160 	atif_arg_elements[0].integer.value = function;
161 
162 	if (params) {
163 		atif_arg_elements[1].type = ACPI_TYPE_BUFFER;
164 		atif_arg_elements[1].buffer.length = params->length;
165 		atif_arg_elements[1].buffer.pointer = params->pointer;
166 	} else {
167 		/* We need a second fake parameter */
168 		atif_arg_elements[1].type = ACPI_TYPE_INTEGER;
169 		atif_arg_elements[1].integer.value = 0;
170 	}
171 
172 	status = acpi_evaluate_object(atif->handle, NULL, &atif_arg,
173 				      &buffer);
174 
175 	/* Fail only if calling the method fails and ATIF is supported */
176 	if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
177 		DRM_DEBUG_DRIVER("failed to evaluate ATIF got %s\n",
178 				 acpi_format_exception(status));
179 		kfree(buffer.pointer);
180 		return NULL;
181 	}
182 
183 	return buffer.pointer;
184 }
185 
186 /**
187  * amdgpu_atif_parse_notification - parse supported notifications
188  *
189  * @n: supported notifications struct
190  * @mask: supported notifications mask from ATIF
191  *
192  * Use the supported notifications mask from ATIF function
193  * ATIF_FUNCTION_VERIFY_INTERFACE to determine what notifications
194  * are supported (all asics).
195  */
196 static void amdgpu_atif_parse_notification(struct amdgpu_atif_notifications *n, u32 mask)
197 {
198 	n->thermal_state = mask & ATIF_THERMAL_STATE_CHANGE_REQUEST_SUPPORTED;
199 	n->forced_power_state = mask & ATIF_FORCED_POWER_STATE_CHANGE_REQUEST_SUPPORTED;
200 	n->system_power_state = mask & ATIF_SYSTEM_POWER_SOURCE_CHANGE_REQUEST_SUPPORTED;
201 	n->brightness_change = mask & ATIF_PANEL_BRIGHTNESS_CHANGE_REQUEST_SUPPORTED;
202 	n->dgpu_display_event = mask & ATIF_DGPU_DISPLAY_EVENT_SUPPORTED;
203 	n->gpu_package_power_limit = mask & ATIF_GPU_PACKAGE_POWER_LIMIT_REQUEST_SUPPORTED;
204 }
205 
206 /**
207  * amdgpu_atif_parse_functions - parse supported functions
208  *
209  * @f: supported functions struct
210  * @mask: supported functions mask from ATIF
211  *
212  * Use the supported functions mask from ATIF function
213  * ATIF_FUNCTION_VERIFY_INTERFACE to determine what functions
214  * are supported (all asics).
215  */
216 static void amdgpu_atif_parse_functions(struct amdgpu_atif_functions *f, u32 mask)
217 {
218 	f->system_params = mask & ATIF_GET_SYSTEM_PARAMETERS_SUPPORTED;
219 	f->sbios_requests = mask & ATIF_GET_SYSTEM_BIOS_REQUESTS_SUPPORTED;
220 	f->temperature_change = mask & ATIF_TEMPERATURE_CHANGE_NOTIFICATION_SUPPORTED;
221 	f->query_backlight_transfer_characteristics =
222 		mask & ATIF_QUERY_BACKLIGHT_TRANSFER_CHARACTERISTICS_SUPPORTED;
223 	f->ready_to_undock = mask & ATIF_READY_TO_UNDOCK_NOTIFICATION_SUPPORTED;
224 	f->external_gpu_information = mask & ATIF_GET_EXTERNAL_GPU_INFORMATION_SUPPORTED;
225 }
226 
227 /**
228  * amdgpu_atif_verify_interface - verify ATIF
229  *
230  * @atif: amdgpu atif struct
231  *
232  * Execute the ATIF_FUNCTION_VERIFY_INTERFACE ATIF function
233  * to initialize ATIF and determine what features are supported
234  * (all asics).
235  * returns 0 on success, error on failure.
236  */
237 static int amdgpu_atif_verify_interface(struct amdgpu_atif *atif)
238 {
239 	union acpi_object *info;
240 	struct atif_verify_interface output;
241 	size_t size;
242 	int err = 0;
243 
244 	info = amdgpu_atif_call(atif, ATIF_FUNCTION_VERIFY_INTERFACE, NULL);
245 	if (!info)
246 		return -EIO;
247 
248 	memset(&output, 0, sizeof(output));
249 
250 	size = *(u16 *) info->buffer.pointer;
251 	if (size < 12) {
252 		DRM_INFO("ATIF buffer is too small: %zu\n", size);
253 		err = -EINVAL;
254 		goto out;
255 	}
256 	size = min(sizeof(output), size);
257 
258 	memcpy(&output, info->buffer.pointer, size);
259 
260 	/* TODO: check version? */
261 	DRM_DEBUG_DRIVER("ATIF version %u\n", output.version);
262 
263 	amdgpu_atif_parse_notification(&atif->notifications, output.notification_mask);
264 	amdgpu_atif_parse_functions(&atif->functions, output.function_bits);
265 
266 out:
267 	kfree(info);
268 	return err;
269 }
270 
271 /**
272  * amdgpu_atif_get_notification_params - determine notify configuration
273  *
274  * @atif: acpi handle
275  *
276  * Execute the ATIF_FUNCTION_GET_SYSTEM_PARAMETERS ATIF function
277  * to determine if a notifier is used and if so which one
278  * (all asics).  This is either Notify(VGA, 0x81) or Notify(VGA, n)
279  * where n is specified in the result if a notifier is used.
280  * Returns 0 on success, error on failure.
281  */
282 static int amdgpu_atif_get_notification_params(struct amdgpu_atif *atif)
283 {
284 	union acpi_object *info;
285 	struct amdgpu_atif_notification_cfg *n = &atif->notification_cfg;
286 	struct atif_system_params params;
287 	size_t size;
288 	int err = 0;
289 
290 	info = amdgpu_atif_call(atif, ATIF_FUNCTION_GET_SYSTEM_PARAMETERS,
291 				NULL);
292 	if (!info) {
293 		err = -EIO;
294 		goto out;
295 	}
296 
297 	size = *(u16 *) info->buffer.pointer;
298 	if (size < 10) {
299 		err = -EINVAL;
300 		goto out;
301 	}
302 
303 	memset(&params, 0, sizeof(params));
304 	size = min(sizeof(params), size);
305 	memcpy(&params, info->buffer.pointer, size);
306 
307 	DRM_DEBUG_DRIVER("SYSTEM_PARAMS: mask = %#x, flags = %#x\n",
308 			params.flags, params.valid_mask);
309 	params.flags = params.flags & params.valid_mask;
310 
311 	if ((params.flags & ATIF_NOTIFY_MASK) == ATIF_NOTIFY_NONE) {
312 		n->enabled = false;
313 		n->command_code = 0;
314 	} else if ((params.flags & ATIF_NOTIFY_MASK) == ATIF_NOTIFY_81) {
315 		n->enabled = true;
316 		n->command_code = 0x81;
317 	} else {
318 		if (size < 11) {
319 			err = -EINVAL;
320 			goto out;
321 		}
322 		n->enabled = true;
323 		n->command_code = params.command_code;
324 	}
325 
326 out:
327 	DRM_DEBUG_DRIVER("Notification %s, command code = %#x\n",
328 			(n->enabled ? "enabled" : "disabled"),
329 			n->command_code);
330 	kfree(info);
331 	return err;
332 }
333 
334 /**
335  * amdgpu_atif_query_backlight_caps - get min and max backlight input signal
336  *
337  * @atif: acpi handle
338  *
339  * Execute the QUERY_BRIGHTNESS_TRANSFER_CHARACTERISTICS ATIF function
340  * to determine the acceptable range of backlight values
341  *
342  * Backlight_caps.caps_valid will be set to true if the query is successful
343  *
344  * The input signals are in range 0-255
345  *
346  * This function assumes the display with backlight is the first LCD
347  *
348  * Returns 0 on success, error on failure.
349  */
350 static int amdgpu_atif_query_backlight_caps(struct amdgpu_atif *atif)
351 {
352 	union acpi_object *info;
353 	struct atif_qbtc_output characteristics;
354 	struct atif_qbtc_arguments arguments;
355 	struct acpi_buffer params;
356 	size_t size;
357 	int err = 0;
358 
359 	arguments.size = sizeof(arguments);
360 	arguments.requested_display = ATIF_QBTC_REQUEST_LCD1;
361 
362 	params.length = sizeof(arguments);
363 	params.pointer = (void *)&arguments;
364 
365 	info = amdgpu_atif_call(atif,
366 		ATIF_FUNCTION_QUERY_BRIGHTNESS_TRANSFER_CHARACTERISTICS,
367 		&params);
368 	if (!info) {
369 		err = -EIO;
370 		goto out;
371 	}
372 
373 	size = *(u16 *) info->buffer.pointer;
374 	if (size < 10) {
375 		err = -EINVAL;
376 		goto out;
377 	}
378 
379 	memset(&characteristics, 0, sizeof(characteristics));
380 	size = min(sizeof(characteristics), size);
381 	memcpy(&characteristics, info->buffer.pointer, size);
382 
383 	atif->backlight_caps.caps_valid = true;
384 	atif->backlight_caps.min_input_signal =
385 			characteristics.min_input_signal;
386 	atif->backlight_caps.max_input_signal =
387 			characteristics.max_input_signal;
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(u16 bdf)
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->bdf == bdf)
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, u16 bdf)
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->bdf = bdf;
961 
962 	STUB();
963 	return -ENOSYS;
964 #ifdef notyet
965 
966 	obj = acpi_evaluate_dsm_typed(xcc_info->handle, &amd_xcc_dsm_guid, 0,
967 				      AMD_XCC_DSM_GET_SUPP_MODE, NULL,
968 				      ACPI_TYPE_INTEGER);
969 
970 	if (!obj) {
971 		acpi_handle_debug(xcc_info->handle,
972 				  "_DSM function %d evaluation failed",
973 				  AMD_XCC_DSM_GET_SUPP_MODE);
974 		ret = -ENOENT;
975 		goto out;
976 	}
977 
978 	tmp->supp_xcp_mode = obj->integer.value & 0xFFFF;
979 	ACPI_FREE(obj);
980 
981 	obj = acpi_evaluate_dsm_typed(xcc_info->handle, &amd_xcc_dsm_guid, 0,
982 				      AMD_XCC_DSM_GET_XCP_MODE, NULL,
983 				      ACPI_TYPE_INTEGER);
984 
985 	if (!obj) {
986 		acpi_handle_debug(xcc_info->handle,
987 				  "_DSM function %d evaluation failed",
988 				  AMD_XCC_DSM_GET_XCP_MODE);
989 		ret = -ENOENT;
990 		goto out;
991 	}
992 
993 	tmp->xcp_mode = obj->integer.value & 0xFFFF;
994 	tmp->mem_mode = (obj->integer.value >> 32) & 0xFFFF;
995 	ACPI_FREE(obj);
996 
997 	/* Evaluate DSMs and fill XCC information */
998 	obj = acpi_evaluate_dsm_typed(xcc_info->handle, &amd_xcc_dsm_guid, 0,
999 				      AMD_XCC_DSM_GET_TMR_INFO, NULL,
1000 				      ACPI_TYPE_PACKAGE);
1001 
1002 	if (!obj || obj->package.count < 2) {
1003 		acpi_handle_debug(xcc_info->handle,
1004 				  "_DSM function %d evaluation failed",
1005 				  AMD_XCC_DSM_GET_TMR_INFO);
1006 		ret = -ENOENT;
1007 		goto out;
1008 	}
1009 
1010 	tmp->tmr_base = obj->package.elements[0].integer.value;
1011 	tmp->tmr_size = obj->package.elements[1].integer.value;
1012 	ACPI_FREE(obj);
1013 
1014 	DRM_DEBUG_DRIVER(
1015 		"New dev(%x): Supported xcp mode: %x curr xcp_mode : %x mem mode : %x, tmr base: %llx tmr size: %llx  ",
1016 		tmp->bdf, tmp->supp_xcp_mode, tmp->xcp_mode, tmp->mem_mode,
1017 		tmp->tmr_base, tmp->tmr_size);
1018 	list_add_tail(&tmp->list, &amdgpu_acpi_dev_list);
1019 	*dev_info = tmp;
1020 
1021 	return 0;
1022 
1023 out:
1024 	if (obj)
1025 		ACPI_FREE(obj);
1026 	kfree(tmp);
1027 
1028 	return ret;
1029 #endif
1030 }
1031 
1032 static int amdgpu_acpi_get_xcc_info(struct amdgpu_acpi_xcc_info *xcc_info,
1033 				    u16 *bdf)
1034 {
1035 	STUB();
1036 	return -ENOSYS;
1037 #ifdef notyet
1038 	union acpi_object *obj;
1039 	acpi_status status;
1040 	int ret = -ENOENT;
1041 
1042 	obj = acpi_evaluate_dsm_typed(xcc_info->handle, &amd_xcc_dsm_guid, 0,
1043 				      AMD_XCC_DSM_GET_NUM_FUNCS, NULL,
1044 				      ACPI_TYPE_INTEGER);
1045 
1046 	if (!obj || obj->integer.value != AMD_XCC_DSM_NUM_FUNCS)
1047 		goto out;
1048 	ACPI_FREE(obj);
1049 
1050 	/* Evaluate DSMs and fill XCC information */
1051 	obj = acpi_evaluate_dsm_typed(xcc_info->handle, &amd_xcc_dsm_guid, 0,
1052 				      AMD_XCC_DSM_GET_VF_XCC_MAPPING, NULL,
1053 				      ACPI_TYPE_INTEGER);
1054 
1055 	if (!obj) {
1056 		acpi_handle_debug(xcc_info->handle,
1057 				  "_DSM function %d evaluation failed",
1058 				  AMD_XCC_DSM_GET_VF_XCC_MAPPING);
1059 		ret = -EINVAL;
1060 		goto out;
1061 	}
1062 
1063 	/* PF xcc id [39:32] */
1064 	xcc_info->phy_id = (obj->integer.value >> 32) & 0xFF;
1065 	/* xcp node of this xcc [47:40] */
1066 	xcc_info->xcp_node = (obj->integer.value >> 40) & 0xFF;
1067 	/* PF bus/dev/fn of this xcc [63:48] */
1068 	*bdf = (obj->integer.value >> 48) & 0xFFFF;
1069 	ACPI_FREE(obj);
1070 	obj = NULL;
1071 
1072 	status =
1073 		amdgpu_acpi_get_node_id(xcc_info->handle, &xcc_info->numa_info);
1074 
1075 	/* TODO: check if this check is required */
1076 	if (ACPI_SUCCESS(status))
1077 		ret = 0;
1078 out:
1079 	if (obj)
1080 		ACPI_FREE(obj);
1081 
1082 	return ret;
1083 #endif
1084 }
1085 
1086 static int amdgpu_acpi_enumerate_xcc(void)
1087 {
1088 	struct amdgpu_acpi_dev_info *dev_info = NULL;
1089 	struct amdgpu_acpi_xcc_info *xcc_info;
1090 	struct acpi_device *acpi_dev;
1091 	char hid[ACPI_ID_LEN];
1092 	int ret, id;
1093 	u16 bdf;
1094 
1095 	INIT_LIST_HEAD(&amdgpu_acpi_dev_list);
1096 	xa_init(&numa_info_xa);
1097 
1098 #ifdef notyet
1099 	for (id = 0; id < AMD_XCC_MAX_HID; id++) {
1100 		snprintf(hid, sizeof(hid), "%s%d", "AMD", AMD_XCC_HID_START + id);
1101 		acpi_dev = acpi_dev_get_first_match_dev(hid, NULL, -1);
1102 		/* These ACPI objects are expected to be in sequential order. If
1103 		 * one is not found, no need to check the rest.
1104 		 */
1105 		if (!acpi_dev) {
1106 			DRM_DEBUG_DRIVER("No matching acpi device found for %s",
1107 					 hid);
1108 			break;
1109 		}
1110 
1111 		xcc_info = kzalloc(sizeof(struct amdgpu_acpi_xcc_info),
1112 				   GFP_KERNEL);
1113 		if (!xcc_info) {
1114 			DRM_ERROR("Failed to allocate memory for xcc info\n");
1115 			return -ENOMEM;
1116 		}
1117 
1118 		INIT_LIST_HEAD(&xcc_info->list);
1119 		xcc_info->handle = acpi_device_handle(acpi_dev);
1120 		acpi_dev_put(acpi_dev);
1121 
1122 		ret = amdgpu_acpi_get_xcc_info(xcc_info, &bdf);
1123 		if (ret) {
1124 			kfree(xcc_info);
1125 			continue;
1126 		}
1127 
1128 		dev_info = amdgpu_acpi_get_dev(bdf);
1129 
1130 		if (!dev_info)
1131 			ret = amdgpu_acpi_dev_init(&dev_info, xcc_info, bdf);
1132 
1133 		if (ret == -ENOMEM)
1134 			return ret;
1135 
1136 		if (!dev_info) {
1137 			kfree(xcc_info);
1138 			continue;
1139 		}
1140 
1141 		list_add_tail(&xcc_info->list, &dev_info->xcc_list);
1142 	}
1143 #endif
1144 
1145 	return 0;
1146 }
1147 
1148 int amdgpu_acpi_get_tmr_info(struct amdgpu_device *adev, u64 *tmr_offset,
1149 			     u64 *tmr_size)
1150 {
1151 	struct amdgpu_acpi_dev_info *dev_info;
1152 	u16 bdf;
1153 
1154 	if (!tmr_offset || !tmr_size)
1155 		return -EINVAL;
1156 
1157 	bdf = pci_dev_id(adev->pdev);
1158 	dev_info = amdgpu_acpi_get_dev(bdf);
1159 	if (!dev_info)
1160 		return -ENOENT;
1161 
1162 	*tmr_offset = dev_info->tmr_base;
1163 	*tmr_size = dev_info->tmr_size;
1164 
1165 	return 0;
1166 }
1167 
1168 int amdgpu_acpi_get_mem_info(struct amdgpu_device *adev, int xcc_id,
1169 			     struct amdgpu_numa_info *numa_info)
1170 {
1171 	struct amdgpu_acpi_dev_info *dev_info;
1172 	struct amdgpu_acpi_xcc_info *xcc_info;
1173 	u16 bdf;
1174 
1175 	if (!numa_info)
1176 		return -EINVAL;
1177 
1178 	bdf = pci_dev_id(adev->pdev);
1179 	dev_info = amdgpu_acpi_get_dev(bdf);
1180 	if (!dev_info)
1181 		return -ENOENT;
1182 
1183 	list_for_each_entry(xcc_info, &dev_info->xcc_list, list) {
1184 		if (xcc_info->phy_id == xcc_id) {
1185 			memcpy(numa_info, xcc_info->numa_info,
1186 			       sizeof(*numa_info));
1187 			return 0;
1188 		}
1189 	}
1190 
1191 	return -ENOENT;
1192 }
1193 
1194 /**
1195  * amdgpu_acpi_event - handle notify events
1196  *
1197  * @nb: notifier block
1198  * @val: val
1199  * @data: acpi event
1200  *
1201  * Calls relevant amdgpu functions in response to various
1202  * acpi events.
1203  * Returns NOTIFY code
1204  */
1205 static int amdgpu_acpi_event(struct notifier_block *nb,
1206 			     unsigned long val,
1207 			     void *data)
1208 {
1209 	struct amdgpu_device *adev = container_of(nb, struct amdgpu_device, acpi_nb);
1210 	struct acpi_bus_event *entry = (struct acpi_bus_event *)data;
1211 
1212 	if (strcmp(entry->device_class, ACPI_AC_CLASS) == 0) {
1213 		if (power_supply_is_system_supplied() > 0)
1214 			DRM_DEBUG_DRIVER("pm: AC\n");
1215 		else
1216 			DRM_DEBUG_DRIVER("pm: DC\n");
1217 
1218 		amdgpu_pm_acpi_event_handler(adev);
1219 	}
1220 
1221 	/* Check for pending SBIOS requests */
1222 	return amdgpu_atif_handler(adev, entry);
1223 }
1224 
1225 /* Call all ACPI methods here */
1226 /**
1227  * amdgpu_acpi_init - init driver acpi support
1228  *
1229  * @adev: amdgpu_device pointer
1230  *
1231  * Verifies the AMD ACPI interfaces and registers with the acpi
1232  * notifier chain (all asics).
1233  * Returns 0 on success, error on failure.
1234  */
1235 int amdgpu_acpi_init(struct amdgpu_device *adev)
1236 {
1237 	struct amdgpu_atif *atif = &amdgpu_acpi_priv.atif;
1238 
1239 	if (atif->notifications.brightness_change) {
1240 		if (adev->dc_enabled) {
1241 #if defined(CONFIG_DRM_AMD_DC)
1242 			struct amdgpu_display_manager *dm = &adev->dm;
1243 
1244 			if (dm->backlight_dev[0])
1245 				atif->bd = dm->backlight_dev[0];
1246 #endif
1247 		} else {
1248 			struct drm_encoder *tmp;
1249 
1250 			/* Find the encoder controlling the brightness */
1251 			list_for_each_entry(tmp, &adev_to_drm(adev)->mode_config.encoder_list,
1252 					    head) {
1253 				struct amdgpu_encoder *enc = to_amdgpu_encoder(tmp);
1254 
1255 				if ((enc->devices & (ATOM_DEVICE_LCD_SUPPORT)) &&
1256 				    enc->enc_priv) {
1257 					struct amdgpu_encoder_atom_dig *dig = enc->enc_priv;
1258 
1259 					if (dig->bl_dev) {
1260 						atif->bd = dig->bl_dev;
1261 						break;
1262 					}
1263 				}
1264 			}
1265 		}
1266 	}
1267 	adev->acpi_nb.notifier_call = amdgpu_acpi_event;
1268 	register_acpi_notifier(&adev->acpi_nb);
1269 
1270 	return 0;
1271 }
1272 
1273 void amdgpu_acpi_get_backlight_caps(struct amdgpu_dm_backlight_caps *caps)
1274 {
1275 	struct amdgpu_atif *atif = &amdgpu_acpi_priv.atif;
1276 
1277 	caps->caps_valid = atif->backlight_caps.caps_valid;
1278 	caps->min_input_signal = atif->backlight_caps.min_input_signal;
1279 	caps->max_input_signal = atif->backlight_caps.max_input_signal;
1280 }
1281 
1282 /**
1283  * amdgpu_acpi_fini - tear down driver acpi support
1284  *
1285  * @adev: amdgpu_device pointer
1286  *
1287  * Unregisters with the acpi notifier chain (all asics).
1288  */
1289 void amdgpu_acpi_fini(struct amdgpu_device *adev)
1290 {
1291 	unregister_acpi_notifier(&adev->acpi_nb);
1292 }
1293 
1294 /**
1295  * amdgpu_atif_pci_probe_handle - look up the ATIF handle
1296  *
1297  * @pdev: pci device
1298  *
1299  * Look up the ATIF handles (all asics).
1300  * Returns true if the handle is found, false if not.
1301  */
1302 static bool amdgpu_atif_pci_probe_handle(struct pci_dev *pdev)
1303 {
1304 	char acpi_method_name[255] = { 0 };
1305 	struct acpi_buffer buffer = {sizeof(acpi_method_name), acpi_method_name};
1306 	acpi_handle dhandle, atif_handle;
1307 	acpi_status status;
1308 	int ret;
1309 
1310 	dhandle = ACPI_HANDLE(&pdev->dev);
1311 	if (!dhandle)
1312 		return false;
1313 
1314 	status = acpi_get_handle(dhandle, "ATIF", &atif_handle);
1315 	if (ACPI_FAILURE(status))
1316 		return false;
1317 
1318 	amdgpu_acpi_priv.atif.handle = atif_handle;
1319 	acpi_get_name(amdgpu_acpi_priv.atif.handle, ACPI_FULL_PATHNAME, &buffer);
1320 	DRM_DEBUG_DRIVER("Found ATIF handle %s\n", acpi_method_name);
1321 	ret = amdgpu_atif_verify_interface(&amdgpu_acpi_priv.atif);
1322 	if (ret) {
1323 		amdgpu_acpi_priv.atif.handle = 0;
1324 		return false;
1325 	}
1326 	return true;
1327 }
1328 
1329 /**
1330  * amdgpu_atcs_pci_probe_handle - look up the ATCS handle
1331  *
1332  * @pdev: pci device
1333  *
1334  * Look up the ATCS handles (all asics).
1335  * Returns true if the handle is found, false if not.
1336  */
1337 static bool amdgpu_atcs_pci_probe_handle(struct pci_dev *pdev)
1338 {
1339 	char acpi_method_name[255] = { 0 };
1340 	struct acpi_buffer buffer = { sizeof(acpi_method_name), acpi_method_name };
1341 	acpi_handle dhandle, atcs_handle;
1342 	acpi_status status;
1343 	int ret;
1344 
1345 	dhandle = ACPI_HANDLE(&pdev->dev);
1346 	if (!dhandle)
1347 		return false;
1348 
1349 	status = acpi_get_handle(dhandle, "ATCS", &atcs_handle);
1350 	if (ACPI_FAILURE(status))
1351 		return false;
1352 
1353 	amdgpu_acpi_priv.atcs.handle = atcs_handle;
1354 	acpi_get_name(amdgpu_acpi_priv.atcs.handle, ACPI_FULL_PATHNAME, &buffer);
1355 	DRM_DEBUG_DRIVER("Found ATCS handle %s\n", acpi_method_name);
1356 	ret = amdgpu_atcs_verify_interface(&amdgpu_acpi_priv.atcs);
1357 	if (ret) {
1358 		amdgpu_acpi_priv.atcs.handle = 0;
1359 		return false;
1360 	}
1361 	return true;
1362 }
1363 
1364 extern struct cfdriver amdgpu_cd;
1365 
1366 
1367 /**
1368  * amdgpu_acpi_should_gpu_reset
1369  *
1370  * @adev: amdgpu_device_pointer
1371  *
1372  * returns true if should reset GPU, false if not
1373  */
1374 bool amdgpu_acpi_should_gpu_reset(struct amdgpu_device *adev)
1375 {
1376 	if ((adev->flags & AMD_IS_APU) &&
1377 	    adev->gfx.imu.funcs) /* Not need to do mode2 reset for IMU enabled APUs */
1378 		return false;
1379 
1380 	if ((adev->flags & AMD_IS_APU) &&
1381 	    amdgpu_acpi_is_s3_active(adev))
1382 		return false;
1383 
1384 	if (amdgpu_sriov_vf(adev))
1385 		return false;
1386 
1387 #if IS_ENABLED(CONFIG_SUSPEND)
1388 	return pm_suspend_target_state != PM_SUSPEND_TO_IDLE;
1389 #else
1390 	return true;
1391 #endif
1392 }
1393 
1394 /*
1395  * amdgpu_acpi_detect - detect ACPI ATIF/ATCS methods
1396  *
1397  * Check if we have the ATIF/ATCS methods and populate
1398  * the structures in the driver.
1399  */
1400 void amdgpu_acpi_detect(void)
1401 {
1402 	struct amdgpu_atif *atif = &amdgpu_acpi_priv.atif;
1403 	struct amdgpu_atcs *atcs = &amdgpu_acpi_priv.atcs;
1404 	struct pci_dev *pdev = NULL;
1405 	int ret;
1406 
1407 #ifdef notyet
1408 	while ((pdev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, pdev)) != NULL) {
1409 		if (!atif->handle)
1410 			amdgpu_atif_pci_probe_handle(pdev);
1411 		if (!atcs->handle)
1412 			amdgpu_atcs_pci_probe_handle(pdev);
1413 	}
1414 
1415 	while ((pdev = pci_get_class(PCI_CLASS_DISPLAY_OTHER << 8, pdev)) != NULL) {
1416 		if (!atif->handle)
1417 			amdgpu_atif_pci_probe_handle(pdev);
1418 		if (!atcs->handle)
1419 			amdgpu_atcs_pci_probe_handle(pdev);
1420 	}
1421 #else
1422 	{
1423 		struct amdgpu_device *adev = (void *)amdgpu_cd.cd_devs[0];
1424 		pdev = adev->pdev;
1425 		if (!atif->handle)
1426 			amdgpu_atif_pci_probe_handle(pdev);
1427 		if (!atcs->handle)
1428 			amdgpu_atcs_pci_probe_handle(pdev);
1429 	}
1430 #endif
1431 
1432 	if (atif->functions.sbios_requests && !atif->functions.system_params) {
1433 		/* XXX check this workraround, if sbios request function is
1434 		 * present we have to see how it's configured in the system
1435 		 * params
1436 		 */
1437 		atif->functions.system_params = true;
1438 	}
1439 
1440 	if (atif->functions.system_params) {
1441 		ret = amdgpu_atif_get_notification_params(atif);
1442 		if (ret) {
1443 			DRM_DEBUG_DRIVER("Call to GET_SYSTEM_PARAMS failed: %d\n",
1444 					ret);
1445 			/* Disable notification */
1446 			atif->notification_cfg.enabled = false;
1447 		}
1448 	}
1449 
1450 	if (atif->functions.query_backlight_transfer_characteristics) {
1451 		ret = amdgpu_atif_query_backlight_caps(atif);
1452 		if (ret) {
1453 			DRM_DEBUG_DRIVER("Call to QUERY_BACKLIGHT_TRANSFER_CHARACTERISTICS failed: %d\n",
1454 					ret);
1455 			atif->backlight_caps.caps_valid = false;
1456 		}
1457 	} else {
1458 		atif->backlight_caps.caps_valid = false;
1459 	}
1460 
1461 	amdgpu_acpi_enumerate_xcc();
1462 }
1463 
1464 void amdgpu_acpi_release(void)
1465 {
1466 	struct amdgpu_acpi_dev_info *dev_info, *dev_tmp;
1467 	struct amdgpu_acpi_xcc_info *xcc_info, *xcc_tmp;
1468 	struct amdgpu_numa_info *numa_info;
1469 	unsigned long index;
1470 
1471 	xa_for_each(&numa_info_xa, index, numa_info) {
1472 		kfree(numa_info);
1473 		xa_erase(&numa_info_xa, index);
1474 	}
1475 
1476 	if (list_empty(&amdgpu_acpi_dev_list))
1477 		return;
1478 
1479 	list_for_each_entry_safe(dev_info, dev_tmp, &amdgpu_acpi_dev_list,
1480 				 list) {
1481 		list_for_each_entry_safe(xcc_info, xcc_tmp, &dev_info->xcc_list,
1482 					 list) {
1483 			list_del(&xcc_info->list);
1484 			kfree(xcc_info);
1485 		}
1486 
1487 		list_del(&dev_info->list);
1488 		kfree(dev_info);
1489 	}
1490 }
1491 
1492 #if IS_ENABLED(CONFIG_SUSPEND)
1493 /**
1494  * amdgpu_acpi_is_s3_active
1495  *
1496  * @adev: amdgpu_device_pointer
1497  *
1498  * returns true if supported, false if not.
1499  */
1500 bool amdgpu_acpi_is_s3_active(struct amdgpu_device *adev)
1501 {
1502 	return !(adev->flags & AMD_IS_APU) ||
1503 		(pm_suspend_target_state == PM_SUSPEND_MEM);
1504 }
1505 
1506 /**
1507  * amdgpu_acpi_is_s0ix_active
1508  *
1509  * @adev: amdgpu_device_pointer
1510  *
1511  * returns true if supported, false if not.
1512  */
1513 bool amdgpu_acpi_is_s0ix_active(struct amdgpu_device *adev)
1514 {
1515 	if (!(adev->flags & AMD_IS_APU) ||
1516 	    (pm_suspend_target_state != PM_SUSPEND_TO_IDLE))
1517 		return false;
1518 
1519 	if (adev->asic_type < CHIP_RAVEN)
1520 		return false;
1521 
1522 	/*
1523 	 * If ACPI_FADT_LOW_POWER_S0 is not set in the FADT, it is generally
1524 	 * risky to do any special firmware-related preparations for entering
1525 	 * S0ix even though the system is suspending to idle, so return false
1526 	 * in that case.
1527 	 */
1528 	if (!(acpi_gbl_FADT.flags & ACPI_FADT_LOW_POWER_S0)) {
1529 		dev_err_once(adev->dev,
1530 			      "Power consumption will be higher as BIOS has not been configured for suspend-to-idle.\n"
1531 			      "To use suspend-to-idle change the sleep mode in BIOS setup.\n");
1532 		return false;
1533 	}
1534 
1535 #if !IS_ENABLED(CONFIG_AMD_PMC)
1536 	dev_err_once(adev->dev,
1537 		      "Power consumption will be higher as the kernel has not been compiled with CONFIG_AMD_PMC.\n");
1538 	return false;
1539 #else
1540 	return true;
1541 #endif /* CONFIG_AMD_PMC */
1542 }
1543 
1544 #endif /* CONFIG_SUSPEND */
1545