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