xref: /openbmc/linux/drivers/gpu/drm/amd/amdgpu/amdgpu_acpi.c (revision 8ef9ea1503d0a129cc6f5cf48fb63633efa5d766)
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 	kfree(info);
802 	return 0;
803 }
804 
805 /**
806  * amdgpu_acpi_smart_shift_update - update dGPU device state to SBIOS
807  *
808  * @dev: drm_device pointer
809  * @ss_state: current smart shift event
810  *
811  * returns 0 on success,
812  * otherwise return error number.
813  */
814 int amdgpu_acpi_smart_shift_update(struct drm_device *dev, enum amdgpu_ss ss_state)
815 {
816 	struct amdgpu_device *adev = drm_to_adev(dev);
817 	int r;
818 
819 	if (!amdgpu_device_supports_smart_shift(dev))
820 		return 0;
821 
822 	switch (ss_state) {
823 	/* SBIOS trigger “stop”, “enable” and “start” at D0, Driver Operational.
824 	 * SBIOS trigger “stop” at D3, Driver Not Operational.
825 	 * SBIOS trigger “stop” and “disable” at D0, Driver NOT operational.
826 	 */
827 	case AMDGPU_SS_DRV_LOAD:
828 		r = amdgpu_acpi_power_shift_control(adev,
829 						    AMDGPU_ATCS_PSC_DEV_STATE_D0,
830 						    AMDGPU_ATCS_PSC_DRV_STATE_OPR);
831 		break;
832 	case AMDGPU_SS_DEV_D0:
833 		r = amdgpu_acpi_power_shift_control(adev,
834 						    AMDGPU_ATCS_PSC_DEV_STATE_D0,
835 						    AMDGPU_ATCS_PSC_DRV_STATE_OPR);
836 		break;
837 	case AMDGPU_SS_DEV_D3:
838 		r = amdgpu_acpi_power_shift_control(adev,
839 						    AMDGPU_ATCS_PSC_DEV_STATE_D3_HOT,
840 						    AMDGPU_ATCS_PSC_DRV_STATE_NOT_OPR);
841 		break;
842 	case AMDGPU_SS_DRV_UNLOAD:
843 		r = amdgpu_acpi_power_shift_control(adev,
844 						    AMDGPU_ATCS_PSC_DEV_STATE_D0,
845 						    AMDGPU_ATCS_PSC_DRV_STATE_NOT_OPR);
846 		break;
847 	default:
848 		return -EINVAL;
849 	}
850 
851 	return r;
852 }
853 
854 #ifdef CONFIG_ACPI_NUMA
855 static inline uint64_t amdgpu_acpi_get_numa_size(int nid)
856 {
857 	/* This is directly using si_meminfo_node implementation as the
858 	 * function is not exported.
859 	 */
860 	int zone_type;
861 	uint64_t managed_pages = 0;
862 
863 	pg_data_t *pgdat = NODE_DATA(nid);
864 
865 	for (zone_type = 0; zone_type < MAX_NR_ZONES; zone_type++)
866 		managed_pages +=
867 			zone_managed_pages(&pgdat->node_zones[zone_type]);
868 	return managed_pages * PAGE_SIZE;
869 }
870 
871 static struct amdgpu_numa_info *amdgpu_acpi_get_numa_info(uint32_t pxm)
872 {
873 	struct amdgpu_numa_info *numa_info;
874 	int nid;
875 
876 	numa_info = xa_load(&numa_info_xa, pxm);
877 
878 	if (!numa_info) {
879 		struct sysinfo info;
880 
881 		numa_info = kzalloc(sizeof(*numa_info), GFP_KERNEL);
882 		if (!numa_info)
883 			return NULL;
884 
885 		nid = pxm_to_node(pxm);
886 		numa_info->pxm = pxm;
887 		numa_info->nid = nid;
888 
889 		if (numa_info->nid == NUMA_NO_NODE) {
890 			si_meminfo(&info);
891 			numa_info->size = info.totalram * info.mem_unit;
892 		} else {
893 			numa_info->size = amdgpu_acpi_get_numa_size(nid);
894 		}
895 		xa_store(&numa_info_xa, numa_info->pxm, numa_info, GFP_KERNEL);
896 	}
897 
898 	return numa_info;
899 }
900 #endif
901 
902 /**
903  * amdgpu_acpi_get_node_id - obtain the NUMA node id for corresponding amdgpu
904  * acpi device handle
905  *
906  * @handle: acpi handle
907  * @numa_info: amdgpu_numa_info structure holding numa information
908  *
909  * Queries the ACPI interface to fetch the corresponding NUMA Node ID for a
910  * given amdgpu acpi device.
911  *
912  * Returns ACPI STATUS OK with Node ID on success or the corresponding failure reason
913  */
914 static acpi_status amdgpu_acpi_get_node_id(acpi_handle handle,
915 				    struct amdgpu_numa_info **numa_info)
916 {
917 #ifdef CONFIG_ACPI_NUMA
918 	u64 pxm;
919 	acpi_status status;
920 
921 	if (!numa_info)
922 		return_ACPI_STATUS(AE_ERROR);
923 
924 	status = acpi_evaluate_integer(handle, "_PXM", NULL, &pxm);
925 
926 	if (ACPI_FAILURE(status))
927 		return status;
928 
929 	*numa_info = amdgpu_acpi_get_numa_info(pxm);
930 
931 	if (!*numa_info)
932 		return_ACPI_STATUS(AE_ERROR);
933 
934 	return_ACPI_STATUS(AE_OK);
935 #else
936 	return_ACPI_STATUS(AE_NOT_EXIST);
937 #endif
938 }
939 
940 static struct amdgpu_acpi_dev_info *amdgpu_acpi_get_dev(u16 bdf)
941 {
942 	struct amdgpu_acpi_dev_info *acpi_dev;
943 
944 	if (list_empty(&amdgpu_acpi_dev_list))
945 		return NULL;
946 
947 	list_for_each_entry(acpi_dev, &amdgpu_acpi_dev_list, list)
948 		if (acpi_dev->bdf == bdf)
949 			return acpi_dev;
950 
951 	return NULL;
952 }
953 
954 static int amdgpu_acpi_dev_init(struct amdgpu_acpi_dev_info **dev_info,
955 				struct amdgpu_acpi_xcc_info *xcc_info, u16 bdf)
956 {
957 	struct amdgpu_acpi_dev_info *tmp;
958 	union acpi_object *obj;
959 	int ret = -ENOENT;
960 
961 	*dev_info = NULL;
962 	tmp = kzalloc(sizeof(struct amdgpu_acpi_dev_info), GFP_KERNEL);
963 	if (!tmp)
964 		return -ENOMEM;
965 
966 	INIT_LIST_HEAD(&tmp->xcc_list);
967 	INIT_LIST_HEAD(&tmp->list);
968 	tmp->bdf = bdf;
969 
970 	obj = acpi_evaluate_dsm_typed(xcc_info->handle, &amd_xcc_dsm_guid, 0,
971 				      AMD_XCC_DSM_GET_SUPP_MODE, NULL,
972 				      ACPI_TYPE_INTEGER);
973 
974 	if (!obj) {
975 		acpi_handle_debug(xcc_info->handle,
976 				  "_DSM function %d evaluation failed",
977 				  AMD_XCC_DSM_GET_SUPP_MODE);
978 		ret = -ENOENT;
979 		goto out;
980 	}
981 
982 	tmp->supp_xcp_mode = obj->integer.value & 0xFFFF;
983 	ACPI_FREE(obj);
984 
985 	obj = acpi_evaluate_dsm_typed(xcc_info->handle, &amd_xcc_dsm_guid, 0,
986 				      AMD_XCC_DSM_GET_XCP_MODE, NULL,
987 				      ACPI_TYPE_INTEGER);
988 
989 	if (!obj) {
990 		acpi_handle_debug(xcc_info->handle,
991 				  "_DSM function %d evaluation failed",
992 				  AMD_XCC_DSM_GET_XCP_MODE);
993 		ret = -ENOENT;
994 		goto out;
995 	}
996 
997 	tmp->xcp_mode = obj->integer.value & 0xFFFF;
998 	tmp->mem_mode = (obj->integer.value >> 32) & 0xFFFF;
999 	ACPI_FREE(obj);
1000 
1001 	/* Evaluate DSMs and fill XCC information */
1002 	obj = acpi_evaluate_dsm_typed(xcc_info->handle, &amd_xcc_dsm_guid, 0,
1003 				      AMD_XCC_DSM_GET_TMR_INFO, NULL,
1004 				      ACPI_TYPE_PACKAGE);
1005 
1006 	if (!obj || obj->package.count < 2) {
1007 		acpi_handle_debug(xcc_info->handle,
1008 				  "_DSM function %d evaluation failed",
1009 				  AMD_XCC_DSM_GET_TMR_INFO);
1010 		ret = -ENOENT;
1011 		goto out;
1012 	}
1013 
1014 	tmp->tmr_base = obj->package.elements[0].integer.value;
1015 	tmp->tmr_size = obj->package.elements[1].integer.value;
1016 	ACPI_FREE(obj);
1017 
1018 	DRM_DEBUG_DRIVER(
1019 		"New dev(%x): Supported xcp mode: %x curr xcp_mode : %x mem mode : %x, tmr base: %llx tmr size: %llx  ",
1020 		tmp->bdf, tmp->supp_xcp_mode, tmp->xcp_mode, tmp->mem_mode,
1021 		tmp->tmr_base, tmp->tmr_size);
1022 	list_add_tail(&tmp->list, &amdgpu_acpi_dev_list);
1023 	*dev_info = tmp;
1024 
1025 	return 0;
1026 
1027 out:
1028 	if (obj)
1029 		ACPI_FREE(obj);
1030 	kfree(tmp);
1031 
1032 	return ret;
1033 }
1034 
1035 static int amdgpu_acpi_get_xcc_info(struct amdgpu_acpi_xcc_info *xcc_info,
1036 				    u16 *bdf)
1037 {
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 }
1084 
1085 static int amdgpu_acpi_enumerate_xcc(void)
1086 {
1087 	struct amdgpu_acpi_dev_info *dev_info = NULL;
1088 	struct amdgpu_acpi_xcc_info *xcc_info;
1089 	struct acpi_device *acpi_dev;
1090 	char hid[ACPI_ID_LEN];
1091 	int ret, id;
1092 	u16 bdf;
1093 
1094 	INIT_LIST_HEAD(&amdgpu_acpi_dev_list);
1095 	xa_init(&numa_info_xa);
1096 
1097 	for (id = 0; id < AMD_XCC_MAX_HID; id++) {
1098 		sprintf(hid, "%s%d", "AMD", AMD_XCC_HID_START + id);
1099 		acpi_dev = acpi_dev_get_first_match_dev(hid, NULL, -1);
1100 		/* These ACPI objects are expected to be in sequential order. If
1101 		 * one is not found, no need to check the rest.
1102 		 */
1103 		if (!acpi_dev) {
1104 			DRM_DEBUG_DRIVER("No matching acpi device found for %s",
1105 					 hid);
1106 			break;
1107 		}
1108 
1109 		xcc_info = kzalloc(sizeof(struct amdgpu_acpi_xcc_info),
1110 				   GFP_KERNEL);
1111 		if (!xcc_info) {
1112 			DRM_ERROR("Failed to allocate memory for xcc info\n");
1113 			return -ENOMEM;
1114 		}
1115 
1116 		INIT_LIST_HEAD(&xcc_info->list);
1117 		xcc_info->handle = acpi_device_handle(acpi_dev);
1118 		acpi_dev_put(acpi_dev);
1119 
1120 		ret = amdgpu_acpi_get_xcc_info(xcc_info, &bdf);
1121 		if (ret) {
1122 			kfree(xcc_info);
1123 			continue;
1124 		}
1125 
1126 		dev_info = amdgpu_acpi_get_dev(bdf);
1127 
1128 		if (!dev_info)
1129 			ret = amdgpu_acpi_dev_init(&dev_info, xcc_info, bdf);
1130 
1131 		if (ret == -ENOMEM)
1132 			return ret;
1133 
1134 		if (!dev_info) {
1135 			kfree(xcc_info);
1136 			continue;
1137 		}
1138 
1139 		list_add_tail(&xcc_info->list, &dev_info->xcc_list);
1140 	}
1141 
1142 	return 0;
1143 }
1144 
1145 int amdgpu_acpi_get_tmr_info(struct amdgpu_device *adev, u64 *tmr_offset,
1146 			     u64 *tmr_size)
1147 {
1148 	struct amdgpu_acpi_dev_info *dev_info;
1149 	u16 bdf;
1150 
1151 	if (!tmr_offset || !tmr_size)
1152 		return -EINVAL;
1153 
1154 	bdf = pci_dev_id(adev->pdev);
1155 	dev_info = amdgpu_acpi_get_dev(bdf);
1156 	if (!dev_info)
1157 		return -ENOENT;
1158 
1159 	*tmr_offset = dev_info->tmr_base;
1160 	*tmr_size = dev_info->tmr_size;
1161 
1162 	return 0;
1163 }
1164 
1165 int amdgpu_acpi_get_mem_info(struct amdgpu_device *adev, int xcc_id,
1166 			     struct amdgpu_numa_info *numa_info)
1167 {
1168 	struct amdgpu_acpi_dev_info *dev_info;
1169 	struct amdgpu_acpi_xcc_info *xcc_info;
1170 	u16 bdf;
1171 
1172 	if (!numa_info)
1173 		return -EINVAL;
1174 
1175 	bdf = pci_dev_id(adev->pdev);
1176 	dev_info = amdgpu_acpi_get_dev(bdf);
1177 	if (!dev_info)
1178 		return -ENOENT;
1179 
1180 	list_for_each_entry(xcc_info, &dev_info->xcc_list, list) {
1181 		if (xcc_info->phy_id == xcc_id) {
1182 			memcpy(numa_info, xcc_info->numa_info,
1183 			       sizeof(*numa_info));
1184 			return 0;
1185 		}
1186 	}
1187 
1188 	return -ENOENT;
1189 }
1190 
1191 /**
1192  * amdgpu_acpi_event - handle notify events
1193  *
1194  * @nb: notifier block
1195  * @val: val
1196  * @data: acpi event
1197  *
1198  * Calls relevant amdgpu functions in response to various
1199  * acpi events.
1200  * Returns NOTIFY code
1201  */
1202 static int amdgpu_acpi_event(struct notifier_block *nb,
1203 			     unsigned long val,
1204 			     void *data)
1205 {
1206 	struct amdgpu_device *adev = container_of(nb, struct amdgpu_device, acpi_nb);
1207 	struct acpi_bus_event *entry = (struct acpi_bus_event *)data;
1208 
1209 	if (strcmp(entry->device_class, ACPI_AC_CLASS) == 0) {
1210 		if (power_supply_is_system_supplied() > 0)
1211 			DRM_DEBUG_DRIVER("pm: AC\n");
1212 		else
1213 			DRM_DEBUG_DRIVER("pm: DC\n");
1214 
1215 		amdgpu_pm_acpi_event_handler(adev);
1216 	}
1217 
1218 	/* Check for pending SBIOS requests */
1219 	return amdgpu_atif_handler(adev, entry);
1220 }
1221 
1222 /* Call all ACPI methods here */
1223 /**
1224  * amdgpu_acpi_init - init driver acpi support
1225  *
1226  * @adev: amdgpu_device pointer
1227  *
1228  * Verifies the AMD ACPI interfaces and registers with the acpi
1229  * notifier chain (all asics).
1230  * Returns 0 on success, error on failure.
1231  */
1232 int amdgpu_acpi_init(struct amdgpu_device *adev)
1233 {
1234 	struct amdgpu_atif *atif = &amdgpu_acpi_priv.atif;
1235 
1236 	if (atif->notifications.brightness_change) {
1237 		if (adev->dc_enabled) {
1238 #if defined(CONFIG_DRM_AMD_DC)
1239 			struct amdgpu_display_manager *dm = &adev->dm;
1240 
1241 			if (dm->backlight_dev[0])
1242 				atif->bd = dm->backlight_dev[0];
1243 #endif
1244 		} else {
1245 			struct drm_encoder *tmp;
1246 
1247 			/* Find the encoder controlling the brightness */
1248 			list_for_each_entry(tmp, &adev_to_drm(adev)->mode_config.encoder_list,
1249 					    head) {
1250 				struct amdgpu_encoder *enc = to_amdgpu_encoder(tmp);
1251 
1252 				if ((enc->devices & (ATOM_DEVICE_LCD_SUPPORT)) &&
1253 				    enc->enc_priv) {
1254 					struct amdgpu_encoder_atom_dig *dig = enc->enc_priv;
1255 
1256 					if (dig->bl_dev) {
1257 						atif->bd = dig->bl_dev;
1258 						break;
1259 					}
1260 				}
1261 			}
1262 		}
1263 	}
1264 	adev->acpi_nb.notifier_call = amdgpu_acpi_event;
1265 	register_acpi_notifier(&adev->acpi_nb);
1266 
1267 	return 0;
1268 }
1269 
1270 void amdgpu_acpi_get_backlight_caps(struct amdgpu_dm_backlight_caps *caps)
1271 {
1272 	struct amdgpu_atif *atif = &amdgpu_acpi_priv.atif;
1273 
1274 	caps->caps_valid = atif->backlight_caps.caps_valid;
1275 	caps->min_input_signal = atif->backlight_caps.min_input_signal;
1276 	caps->max_input_signal = atif->backlight_caps.max_input_signal;
1277 }
1278 
1279 /**
1280  * amdgpu_acpi_fini - tear down driver acpi support
1281  *
1282  * @adev: amdgpu_device pointer
1283  *
1284  * Unregisters with the acpi notifier chain (all asics).
1285  */
1286 void amdgpu_acpi_fini(struct amdgpu_device *adev)
1287 {
1288 	unregister_acpi_notifier(&adev->acpi_nb);
1289 }
1290 
1291 /**
1292  * amdgpu_atif_pci_probe_handle - look up the ATIF handle
1293  *
1294  * @pdev: pci device
1295  *
1296  * Look up the ATIF handles (all asics).
1297  * Returns true if the handle is found, false if not.
1298  */
1299 static bool amdgpu_atif_pci_probe_handle(struct pci_dev *pdev)
1300 {
1301 	char acpi_method_name[255] = { 0 };
1302 	struct acpi_buffer buffer = {sizeof(acpi_method_name), acpi_method_name};
1303 	acpi_handle dhandle, atif_handle;
1304 	acpi_status status;
1305 	int ret;
1306 
1307 	dhandle = ACPI_HANDLE(&pdev->dev);
1308 	if (!dhandle)
1309 		return false;
1310 
1311 	status = acpi_get_handle(dhandle, "ATIF", &atif_handle);
1312 	if (ACPI_FAILURE(status))
1313 		return false;
1314 
1315 	amdgpu_acpi_priv.atif.handle = atif_handle;
1316 	acpi_get_name(amdgpu_acpi_priv.atif.handle, ACPI_FULL_PATHNAME, &buffer);
1317 	DRM_DEBUG_DRIVER("Found ATIF handle %s\n", acpi_method_name);
1318 	ret = amdgpu_atif_verify_interface(&amdgpu_acpi_priv.atif);
1319 	if (ret) {
1320 		amdgpu_acpi_priv.atif.handle = 0;
1321 		return false;
1322 	}
1323 	return true;
1324 }
1325 
1326 /**
1327  * amdgpu_atcs_pci_probe_handle - look up the ATCS handle
1328  *
1329  * @pdev: pci device
1330  *
1331  * Look up the ATCS handles (all asics).
1332  * Returns true if the handle is found, false if not.
1333  */
1334 static bool amdgpu_atcs_pci_probe_handle(struct pci_dev *pdev)
1335 {
1336 	char acpi_method_name[255] = { 0 };
1337 	struct acpi_buffer buffer = { sizeof(acpi_method_name), acpi_method_name };
1338 	acpi_handle dhandle, atcs_handle;
1339 	acpi_status status;
1340 	int ret;
1341 
1342 	dhandle = ACPI_HANDLE(&pdev->dev);
1343 	if (!dhandle)
1344 		return false;
1345 
1346 	status = acpi_get_handle(dhandle, "ATCS", &atcs_handle);
1347 	if (ACPI_FAILURE(status))
1348 		return false;
1349 
1350 	amdgpu_acpi_priv.atcs.handle = atcs_handle;
1351 	acpi_get_name(amdgpu_acpi_priv.atcs.handle, ACPI_FULL_PATHNAME, &buffer);
1352 	DRM_DEBUG_DRIVER("Found ATCS handle %s\n", acpi_method_name);
1353 	ret = amdgpu_atcs_verify_interface(&amdgpu_acpi_priv.atcs);
1354 	if (ret) {
1355 		amdgpu_acpi_priv.atcs.handle = 0;
1356 		return false;
1357 	}
1358 	return true;
1359 }
1360 
1361 
1362 /**
1363  * amdgpu_acpi_should_gpu_reset
1364  *
1365  * @adev: amdgpu_device_pointer
1366  *
1367  * returns true if should reset GPU, false if not
1368  */
1369 bool amdgpu_acpi_should_gpu_reset(struct amdgpu_device *adev)
1370 {
1371 	if ((adev->flags & AMD_IS_APU) &&
1372 	    adev->gfx.imu.funcs) /* Not need to do mode2 reset for IMU enabled APUs */
1373 		return false;
1374 
1375 	if ((adev->flags & AMD_IS_APU) &&
1376 	    amdgpu_acpi_is_s3_active(adev))
1377 		return false;
1378 
1379 	if (amdgpu_sriov_vf(adev))
1380 		return false;
1381 
1382 #if IS_ENABLED(CONFIG_SUSPEND)
1383 	return pm_suspend_target_state != PM_SUSPEND_TO_IDLE;
1384 #else
1385 	return true;
1386 #endif
1387 }
1388 
1389 /*
1390  * amdgpu_acpi_detect - detect ACPI ATIF/ATCS methods
1391  *
1392  * Check if we have the ATIF/ATCS methods and populate
1393  * the structures in the driver.
1394  */
1395 void amdgpu_acpi_detect(void)
1396 {
1397 	struct amdgpu_atif *atif = &amdgpu_acpi_priv.atif;
1398 	struct amdgpu_atcs *atcs = &amdgpu_acpi_priv.atcs;
1399 	struct pci_dev *pdev = NULL;
1400 	int ret;
1401 
1402 	while ((pdev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, pdev)) != NULL) {
1403 		if (!atif->handle)
1404 			amdgpu_atif_pci_probe_handle(pdev);
1405 		if (!atcs->handle)
1406 			amdgpu_atcs_pci_probe_handle(pdev);
1407 	}
1408 
1409 	while ((pdev = pci_get_class(PCI_CLASS_DISPLAY_OTHER << 8, pdev)) != NULL) {
1410 		if (!atif->handle)
1411 			amdgpu_atif_pci_probe_handle(pdev);
1412 		if (!atcs->handle)
1413 			amdgpu_atcs_pci_probe_handle(pdev);
1414 	}
1415 
1416 	if (atif->functions.sbios_requests && !atif->functions.system_params) {
1417 		/* XXX check this workraround, if sbios request function is
1418 		 * present we have to see how it's configured in the system
1419 		 * params
1420 		 */
1421 		atif->functions.system_params = true;
1422 	}
1423 
1424 	if (atif->functions.system_params) {
1425 		ret = amdgpu_atif_get_notification_params(atif);
1426 		if (ret) {
1427 			DRM_DEBUG_DRIVER("Call to GET_SYSTEM_PARAMS failed: %d\n",
1428 					ret);
1429 			/* Disable notification */
1430 			atif->notification_cfg.enabled = false;
1431 		}
1432 	}
1433 
1434 	if (atif->functions.query_backlight_transfer_characteristics) {
1435 		ret = amdgpu_atif_query_backlight_caps(atif);
1436 		if (ret) {
1437 			DRM_DEBUG_DRIVER("Call to QUERY_BACKLIGHT_TRANSFER_CHARACTERISTICS failed: %d\n",
1438 					ret);
1439 			atif->backlight_caps.caps_valid = false;
1440 		}
1441 	} else {
1442 		atif->backlight_caps.caps_valid = false;
1443 	}
1444 
1445 	amdgpu_acpi_enumerate_xcc();
1446 }
1447 
1448 void amdgpu_acpi_release(void)
1449 {
1450 	struct amdgpu_acpi_dev_info *dev_info, *dev_tmp;
1451 	struct amdgpu_acpi_xcc_info *xcc_info, *xcc_tmp;
1452 	struct amdgpu_numa_info *numa_info;
1453 	unsigned long index;
1454 
1455 	xa_for_each(&numa_info_xa, index, numa_info) {
1456 		kfree(numa_info);
1457 		xa_erase(&numa_info_xa, index);
1458 	}
1459 
1460 	if (list_empty(&amdgpu_acpi_dev_list))
1461 		return;
1462 
1463 	list_for_each_entry_safe(dev_info, dev_tmp, &amdgpu_acpi_dev_list,
1464 				 list) {
1465 		list_for_each_entry_safe(xcc_info, xcc_tmp, &dev_info->xcc_list,
1466 					 list) {
1467 			list_del(&xcc_info->list);
1468 			kfree(xcc_info);
1469 		}
1470 
1471 		list_del(&dev_info->list);
1472 		kfree(dev_info);
1473 	}
1474 }
1475 
1476 #if IS_ENABLED(CONFIG_SUSPEND)
1477 /**
1478  * amdgpu_acpi_is_s3_active
1479  *
1480  * @adev: amdgpu_device_pointer
1481  *
1482  * returns true if supported, false if not.
1483  */
1484 bool amdgpu_acpi_is_s3_active(struct amdgpu_device *adev)
1485 {
1486 	return !(adev->flags & AMD_IS_APU) ||
1487 		(pm_suspend_target_state == PM_SUSPEND_MEM);
1488 }
1489 
1490 /**
1491  * amdgpu_acpi_is_s0ix_active
1492  *
1493  * @adev: amdgpu_device_pointer
1494  *
1495  * returns true if supported, false if not.
1496  */
1497 bool amdgpu_acpi_is_s0ix_active(struct amdgpu_device *adev)
1498 {
1499 	if (!(adev->flags & AMD_IS_APU) ||
1500 	    (pm_suspend_target_state != PM_SUSPEND_TO_IDLE))
1501 		return false;
1502 
1503 	if (adev->asic_type < CHIP_RAVEN)
1504 		return false;
1505 
1506 	/*
1507 	 * If ACPI_FADT_LOW_POWER_S0 is not set in the FADT, it is generally
1508 	 * risky to do any special firmware-related preparations for entering
1509 	 * S0ix even though the system is suspending to idle, so return false
1510 	 * in that case.
1511 	 */
1512 	if (!(acpi_gbl_FADT.flags & ACPI_FADT_LOW_POWER_S0)) {
1513 		dev_err_once(adev->dev,
1514 			      "Power consumption will be higher as BIOS has not been configured for suspend-to-idle.\n"
1515 			      "To use suspend-to-idle change the sleep mode in BIOS setup.\n");
1516 		return false;
1517 	}
1518 
1519 #if !IS_ENABLED(CONFIG_AMD_PMC)
1520 	dev_err_once(adev->dev,
1521 		      "Power consumption will be higher as the kernel has not been compiled with CONFIG_AMD_PMC.\n");
1522 	return false;
1523 #else
1524 	return true;
1525 #endif /* CONFIG_AMD_PMC */
1526 }
1527 
1528 #endif /* CONFIG_SUSPEND */
1529