xref: /dragonfly/sys/dev/drm/amd/amdgpu/amdgpu_acpi.c (revision b843c749)
1 /*
2  * Copyright 2012 Advanced Micro Devices, Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20  * OTHER DEALINGS IN THE SOFTWARE.
21  *
22  */
23 
24 #include <linux/pci.h>
25 #include <linux/acpi.h>
26 #include <linux/slab.h>
27 #include <linux/power_supply.h>
28 #include <linux/pm_runtime.h>
29 #include <acpi/video.h>
30 #include <drm/drmP.h>
31 #include <drm/drm_crtc_helper.h>
32 #include "amdgpu.h"
33 #include "amdgpu_pm.h"
34 #include "amd_acpi.h"
35 #include "atom.h"
36 
37 struct amdgpu_atif_notification_cfg {
38 	bool enabled;
39 	int command_code;
40 };
41 
42 struct amdgpu_atif_notifications {
43 	bool display_switch;
44 	bool expansion_mode_change;
45 	bool thermal_state;
46 	bool forced_power_state;
47 	bool system_power_state;
48 	bool display_conf_change;
49 	bool px_gfx_switch;
50 	bool brightness_change;
51 	bool dgpu_display_event;
52 };
53 
54 struct amdgpu_atif_functions {
55 	bool system_params;
56 	bool sbios_requests;
57 	bool select_active_disp;
58 	bool lid_state;
59 	bool get_tv_standard;
60 	bool set_tv_standard;
61 	bool get_panel_expansion_mode;
62 	bool set_panel_expansion_mode;
63 	bool temperature_change;
64 	bool graphics_device_types;
65 };
66 
67 struct amdgpu_atif {
68 	acpi_handle handle;
69 
70 	struct amdgpu_atif_notifications notifications;
71 	struct amdgpu_atif_functions functions;
72 	struct amdgpu_atif_notification_cfg notification_cfg;
73 	struct amdgpu_encoder *encoder_for_bl;
74 };
75 
76 /* Call the ATIF method
77  */
78 /**
79  * amdgpu_atif_call - call an ATIF method
80  *
81  * @handle: acpi handle
82  * @function: the ATIF function to execute
83  * @params: ATIF function params
84  *
85  * Executes the requested ATIF function (all asics).
86  * Returns a pointer to the acpi output buffer.
87  */
88 static union acpi_object *amdgpu_atif_call(struct amdgpu_atif *atif,
89 					   int function,
90 					   struct acpi_buffer *params)
91 {
92 	acpi_status status;
93 	union acpi_object atif_arg_elements[2];
94 	struct acpi_object_list atif_arg;
95 	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
96 
97 	atif_arg.count = 2;
98 	atif_arg.pointer = &atif_arg_elements[0];
99 
100 	atif_arg_elements[0].type = ACPI_TYPE_INTEGER;
101 	atif_arg_elements[0].integer.value = function;
102 
103 	if (params) {
104 		atif_arg_elements[1].type = ACPI_TYPE_BUFFER;
105 		atif_arg_elements[1].buffer.length = params->length;
106 		atif_arg_elements[1].buffer.pointer = params->pointer;
107 	} else {
108 		/* We need a second fake parameter */
109 		atif_arg_elements[1].type = ACPI_TYPE_INTEGER;
110 		atif_arg_elements[1].integer.value = 0;
111 	}
112 
113 	status = acpi_evaluate_object(atif->handle, NULL, &atif_arg,
114 				      &buffer);
115 
116 	/* Fail only if calling the method fails and ATIF is supported */
117 	if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
118 		DRM_DEBUG_DRIVER("failed to evaluate ATIF got %s\n",
119 				 acpi_format_exception(status));
120 		kfree(buffer.pointer);
121 		return NULL;
122 	}
123 
124 	return buffer.pointer;
125 }
126 
127 /**
128  * amdgpu_atif_parse_notification - parse supported notifications
129  *
130  * @n: supported notifications struct
131  * @mask: supported notifications mask from ATIF
132  *
133  * Use the supported notifications mask from ATIF function
134  * ATIF_FUNCTION_VERIFY_INTERFACE to determine what notifications
135  * are supported (all asics).
136  */
137 static void amdgpu_atif_parse_notification(struct amdgpu_atif_notifications *n, u32 mask)
138 {
139 	n->display_switch = mask & ATIF_DISPLAY_SWITCH_REQUEST_SUPPORTED;
140 	n->expansion_mode_change = mask & ATIF_EXPANSION_MODE_CHANGE_REQUEST_SUPPORTED;
141 	n->thermal_state = mask & ATIF_THERMAL_STATE_CHANGE_REQUEST_SUPPORTED;
142 	n->forced_power_state = mask & ATIF_FORCED_POWER_STATE_CHANGE_REQUEST_SUPPORTED;
143 	n->system_power_state = mask & ATIF_SYSTEM_POWER_SOURCE_CHANGE_REQUEST_SUPPORTED;
144 	n->display_conf_change = mask & ATIF_DISPLAY_CONF_CHANGE_REQUEST_SUPPORTED;
145 	n->px_gfx_switch = mask & ATIF_PX_GFX_SWITCH_REQUEST_SUPPORTED;
146 	n->brightness_change = mask & ATIF_PANEL_BRIGHTNESS_CHANGE_REQUEST_SUPPORTED;
147 	n->dgpu_display_event = mask & ATIF_DGPU_DISPLAY_EVENT_SUPPORTED;
148 }
149 
150 /**
151  * amdgpu_atif_parse_functions - parse supported functions
152  *
153  * @f: supported functions struct
154  * @mask: supported functions mask from ATIF
155  *
156  * Use the supported functions mask from ATIF function
157  * ATIF_FUNCTION_VERIFY_INTERFACE to determine what functions
158  * are supported (all asics).
159  */
160 static void amdgpu_atif_parse_functions(struct amdgpu_atif_functions *f, u32 mask)
161 {
162 	f->system_params = mask & ATIF_GET_SYSTEM_PARAMETERS_SUPPORTED;
163 	f->sbios_requests = mask & ATIF_GET_SYSTEM_BIOS_REQUESTS_SUPPORTED;
164 	f->select_active_disp = mask & ATIF_SELECT_ACTIVE_DISPLAYS_SUPPORTED;
165 	f->lid_state = mask & ATIF_GET_LID_STATE_SUPPORTED;
166 	f->get_tv_standard = mask & ATIF_GET_TV_STANDARD_FROM_CMOS_SUPPORTED;
167 	f->set_tv_standard = mask & ATIF_SET_TV_STANDARD_IN_CMOS_SUPPORTED;
168 	f->get_panel_expansion_mode = mask & ATIF_GET_PANEL_EXPANSION_MODE_FROM_CMOS_SUPPORTED;
169 	f->set_panel_expansion_mode = mask & ATIF_SET_PANEL_EXPANSION_MODE_IN_CMOS_SUPPORTED;
170 	f->temperature_change = mask & ATIF_TEMPERATURE_CHANGE_NOTIFICATION_SUPPORTED;
171 	f->graphics_device_types = mask & ATIF_GET_GRAPHICS_DEVICE_TYPES_SUPPORTED;
172 }
173 
174 /**
175  * amdgpu_atif_verify_interface - verify ATIF
176  *
177  * @handle: acpi handle
178  * @atif: amdgpu atif struct
179  *
180  * Execute the ATIF_FUNCTION_VERIFY_INTERFACE ATIF function
181  * to initialize ATIF and determine what features are supported
182  * (all asics).
183  * returns 0 on success, error on failure.
184  */
185 static int amdgpu_atif_verify_interface(struct amdgpu_atif *atif)
186 {
187 	union acpi_object *info;
188 	struct atif_verify_interface output;
189 	size_t size;
190 	int err = 0;
191 
192 	info = amdgpu_atif_call(atif, ATIF_FUNCTION_VERIFY_INTERFACE, NULL);
193 	if (!info)
194 		return -EIO;
195 
196 	memset(&output, 0, sizeof(output));
197 
198 	size = *(u16 *) info->buffer.pointer;
199 	if (size < 12) {
200 		DRM_INFO("ATIF buffer is too small: %zu\n", size);
201 		err = -EINVAL;
202 		goto out;
203 	}
204 	size = min(sizeof(output), size);
205 
206 	memcpy(&output, info->buffer.pointer, size);
207 
208 	/* TODO: check version? */
209 	DRM_DEBUG_DRIVER("ATIF version %u\n", output.version);
210 
211 	amdgpu_atif_parse_notification(&atif->notifications, output.notification_mask);
212 	amdgpu_atif_parse_functions(&atif->functions, output.function_bits);
213 
214 out:
215 	kfree(info);
216 	return err;
217 }
218 
219 static acpi_handle amdgpu_atif_probe_handle(acpi_handle dhandle)
220 {
221 	acpi_handle handle = NULL;
222 	char acpi_method_name[255] = { 0 };
223 	struct acpi_buffer buffer = { sizeof(acpi_method_name), acpi_method_name };
224 	acpi_status status;
225 
226 	/* For PX/HG systems, ATIF and ATPX are in the iGPU's namespace, on dGPU only
227 	 * systems, ATIF is in the dGPU's namespace.
228 	 */
229 	status = acpi_get_handle(dhandle, "ATIF", &handle);
230 	if (ACPI_SUCCESS(status))
231 		goto out;
232 
233 	if (amdgpu_has_atpx()) {
234 		status = acpi_get_handle(amdgpu_atpx_get_dhandle(), "ATIF",
235 					 &handle);
236 		if (ACPI_SUCCESS(status))
237 			goto out;
238 	}
239 
240 	DRM_DEBUG_DRIVER("No ATIF handle found\n");
241 	return NULL;
242 out:
243 	acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
244 	DRM_DEBUG_DRIVER("Found ATIF handle %s\n", acpi_method_name);
245 	return handle;
246 }
247 
248 /**
249  * amdgpu_atif_get_notification_params - determine notify configuration
250  *
251  * @handle: acpi handle
252  * @n: atif notification configuration struct
253  *
254  * Execute the ATIF_FUNCTION_GET_SYSTEM_PARAMETERS ATIF function
255  * to determine if a notifier is used and if so which one
256  * (all asics).  This is either Notify(VGA, 0x81) or Notify(VGA, n)
257  * where n is specified in the result if a notifier is used.
258  * Returns 0 on success, error on failure.
259  */
260 static int amdgpu_atif_get_notification_params(struct amdgpu_atif *atif)
261 {
262 	union acpi_object *info;
263 	struct amdgpu_atif_notification_cfg *n = &atif->notification_cfg;
264 	struct atif_system_params params;
265 	size_t size;
266 	int err = 0;
267 
268 	info = amdgpu_atif_call(atif, ATIF_FUNCTION_GET_SYSTEM_PARAMETERS,
269 				NULL);
270 	if (!info) {
271 		err = -EIO;
272 		goto out;
273 	}
274 
275 	size = *(u16 *) info->buffer.pointer;
276 	if (size < 10) {
277 		err = -EINVAL;
278 		goto out;
279 	}
280 
281 	memset(&params, 0, sizeof(params));
282 	size = min(sizeof(params), size);
283 	memcpy(&params, info->buffer.pointer, size);
284 
285 	DRM_DEBUG_DRIVER("SYSTEM_PARAMS: mask = %#x, flags = %#x\n",
286 			params.flags, params.valid_mask);
287 	params.flags = params.flags & params.valid_mask;
288 
289 	if ((params.flags & ATIF_NOTIFY_MASK) == ATIF_NOTIFY_NONE) {
290 		n->enabled = false;
291 		n->command_code = 0;
292 	} else if ((params.flags & ATIF_NOTIFY_MASK) == ATIF_NOTIFY_81) {
293 		n->enabled = true;
294 		n->command_code = 0x81;
295 	} else {
296 		if (size < 11) {
297 			err = -EINVAL;
298 			goto out;
299 		}
300 		n->enabled = true;
301 		n->command_code = params.command_code;
302 	}
303 
304 out:
305 	DRM_DEBUG_DRIVER("Notification %s, command code = %#x\n",
306 			(n->enabled ? "enabled" : "disabled"),
307 			n->command_code);
308 	kfree(info);
309 	return err;
310 }
311 
312 /**
313  * amdgpu_atif_get_sbios_requests - get requested sbios event
314  *
315  * @handle: acpi handle
316  * @req: atif sbios request struct
317  *
318  * Execute the ATIF_FUNCTION_GET_SYSTEM_BIOS_REQUESTS ATIF function
319  * to determine what requests the sbios is making to the driver
320  * (all asics).
321  * Returns 0 on success, error on failure.
322  */
323 static int amdgpu_atif_get_sbios_requests(struct amdgpu_atif *atif,
324 					  struct atif_sbios_requests *req)
325 {
326 	union acpi_object *info;
327 	size_t size;
328 	int count = 0;
329 
330 	info = amdgpu_atif_call(atif, ATIF_FUNCTION_GET_SYSTEM_BIOS_REQUESTS,
331 				NULL);
332 	if (!info)
333 		return -EIO;
334 
335 	size = *(u16 *)info->buffer.pointer;
336 	if (size < 0xd) {
337 		count = -EINVAL;
338 		goto out;
339 	}
340 	memset(req, 0, sizeof(*req));
341 
342 	size = min(sizeof(*req), size);
343 	memcpy(req, info->buffer.pointer, size);
344 	DRM_DEBUG_DRIVER("SBIOS pending requests: %#x\n", req->pending);
345 
346 	count = hweight32(req->pending);
347 
348 out:
349 	kfree(info);
350 	return count;
351 }
352 
353 /**
354  * amdgpu_atif_handler - handle ATIF notify requests
355  *
356  * @adev: amdgpu_device pointer
357  * @event: atif sbios request struct
358  *
359  * Checks the acpi event and if it matches an atif event,
360  * handles it.
361  *
362  * Returns:
363  * NOTIFY_BAD or NOTIFY_DONE, depending on the event.
364  */
365 static int amdgpu_atif_handler(struct amdgpu_device *adev,
366 			       struct acpi_bus_event *event)
367 {
368 	struct amdgpu_atif *atif = adev->atif;
369 	int count;
370 
371 	DRM_DEBUG_DRIVER("event, device_class = %s, type = %#x\n",
372 			event->device_class, event->type);
373 
374 	if (strcmp(event->device_class, ACPI_VIDEO_CLASS) != 0)
375 		return NOTIFY_DONE;
376 
377 	/* Is this actually our event? */
378 	if (!atif ||
379 	    !atif->notification_cfg.enabled ||
380 	    event->type != atif->notification_cfg.command_code) {
381 		/* These events will generate keypresses otherwise */
382 		if (event->type == ACPI_VIDEO_NOTIFY_PROBE)
383 			return NOTIFY_BAD;
384 		else
385 			return NOTIFY_DONE;
386 	}
387 
388 	if (atif->functions.sbios_requests) {
389 		struct atif_sbios_requests req;
390 
391 		/* Check pending SBIOS requests */
392 		count = amdgpu_atif_get_sbios_requests(atif, &req);
393 
394 		if (count <= 0)
395 			return NOTIFY_BAD;
396 
397 		DRM_DEBUG_DRIVER("ATIF: %d pending SBIOS requests\n", count);
398 
399 		/* todo: add DC handling */
400 		if ((req.pending & ATIF_PANEL_BRIGHTNESS_CHANGE_REQUEST) &&
401 		    !amdgpu_device_has_dc_support(adev)) {
402 			struct amdgpu_encoder *enc = atif->encoder_for_bl;
403 
404 			if (enc) {
405 				struct amdgpu_encoder_atom_dig *dig = enc->enc_priv;
406 
407 				DRM_DEBUG_DRIVER("Changing brightness to %d\n",
408 						 req.backlight_level);
409 
410 				amdgpu_display_backlight_set_level(adev, enc, req.backlight_level);
411 
412 #if defined(CONFIG_BACKLIGHT_CLASS_DEVICE) || defined(CONFIG_BACKLIGHT_CLASS_DEVICE_MODULE)
413 				backlight_force_update(dig->bl_dev,
414 						       BACKLIGHT_UPDATE_HOTKEY);
415 #endif
416 			}
417 		}
418 		if (req.pending & ATIF_DGPU_DISPLAY_EVENT) {
419 			if (adev->flags & AMD_IS_PX) {
420 				pm_runtime_get_sync(adev->ddev->dev);
421 				/* Just fire off a uevent and let userspace tell us what to do */
422 				drm_helper_hpd_irq_event(adev->ddev);
423 				pm_runtime_mark_last_busy(adev->ddev->dev);
424 				pm_runtime_put_autosuspend(adev->ddev->dev);
425 			}
426 		}
427 		/* TODO: check other events */
428 	}
429 
430 	/* We've handled the event, stop the notifier chain. The ACPI interface
431 	 * overloads ACPI_VIDEO_NOTIFY_PROBE, we don't want to send that to
432 	 * userspace if the event was generated only to signal a SBIOS
433 	 * request.
434 	 */
435 	return NOTIFY_BAD;
436 }
437 
438 /* Call the ATCS method
439  */
440 /**
441  * amdgpu_atcs_call - call an ATCS method
442  *
443  * @handle: acpi handle
444  * @function: the ATCS function to execute
445  * @params: ATCS function params
446  *
447  * Executes the requested ATCS function (all asics).
448  * Returns a pointer to the acpi output buffer.
449  */
450 static union acpi_object *amdgpu_atcs_call(acpi_handle handle, int function,
451 					   struct acpi_buffer *params)
452 {
453 	acpi_status status;
454 	union acpi_object atcs_arg_elements[2];
455 	struct acpi_object_list atcs_arg;
456 	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
457 
458 	atcs_arg.count = 2;
459 	atcs_arg.pointer = &atcs_arg_elements[0];
460 
461 	atcs_arg_elements[0].type = ACPI_TYPE_INTEGER;
462 	atcs_arg_elements[0].integer.value = function;
463 
464 	if (params) {
465 		atcs_arg_elements[1].type = ACPI_TYPE_BUFFER;
466 		atcs_arg_elements[1].buffer.length = params->length;
467 		atcs_arg_elements[1].buffer.pointer = params->pointer;
468 	} else {
469 		/* We need a second fake parameter */
470 		atcs_arg_elements[1].type = ACPI_TYPE_INTEGER;
471 		atcs_arg_elements[1].integer.value = 0;
472 	}
473 
474 	status = acpi_evaluate_object(handle, "ATCS", &atcs_arg, &buffer);
475 
476 	/* Fail only if calling the method fails and ATIF is supported */
477 	if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
478 		DRM_DEBUG_DRIVER("failed to evaluate ATCS got %s\n",
479 				 acpi_format_exception(status));
480 		kfree(buffer.pointer);
481 		return NULL;
482 	}
483 
484 	return buffer.pointer;
485 }
486 
487 /**
488  * amdgpu_atcs_parse_functions - parse supported functions
489  *
490  * @f: supported functions struct
491  * @mask: supported functions mask from ATCS
492  *
493  * Use the supported functions mask from ATCS function
494  * ATCS_FUNCTION_VERIFY_INTERFACE to determine what functions
495  * are supported (all asics).
496  */
497 static void amdgpu_atcs_parse_functions(struct amdgpu_atcs_functions *f, u32 mask)
498 {
499 	f->get_ext_state = mask & ATCS_GET_EXTERNAL_STATE_SUPPORTED;
500 	f->pcie_perf_req = mask & ATCS_PCIE_PERFORMANCE_REQUEST_SUPPORTED;
501 	f->pcie_dev_rdy = mask & ATCS_PCIE_DEVICE_READY_NOTIFICATION_SUPPORTED;
502 	f->pcie_bus_width = mask & ATCS_SET_PCIE_BUS_WIDTH_SUPPORTED;
503 }
504 
505 /**
506  * amdgpu_atcs_verify_interface - verify ATCS
507  *
508  * @handle: acpi handle
509  * @atcs: amdgpu atcs struct
510  *
511  * Execute the ATCS_FUNCTION_VERIFY_INTERFACE ATCS function
512  * to initialize ATCS and determine what features are supported
513  * (all asics).
514  * returns 0 on success, error on failure.
515  */
516 static int amdgpu_atcs_verify_interface(acpi_handle handle,
517 					struct amdgpu_atcs *atcs)
518 {
519 	union acpi_object *info;
520 	struct atcs_verify_interface output;
521 	size_t size;
522 	int err = 0;
523 
524 	info = amdgpu_atcs_call(handle, ATCS_FUNCTION_VERIFY_INTERFACE, NULL);
525 	if (!info)
526 		return -EIO;
527 
528 	memset(&output, 0, sizeof(output));
529 
530 	size = *(u16 *) info->buffer.pointer;
531 	if (size < 8) {
532 		DRM_INFO("ATCS buffer is too small: %zu\n", size);
533 		err = -EINVAL;
534 		goto out;
535 	}
536 	size = min(sizeof(output), size);
537 
538 	memcpy(&output, info->buffer.pointer, size);
539 
540 	/* TODO: check version? */
541 	DRM_DEBUG_DRIVER("ATCS version %u\n", output.version);
542 
543 	amdgpu_atcs_parse_functions(&atcs->functions, output.function_bits);
544 
545 out:
546 	kfree(info);
547 	return err;
548 }
549 
550 /**
551  * amdgpu_acpi_is_pcie_performance_request_supported
552  *
553  * @adev: amdgpu_device pointer
554  *
555  * Check if the ATCS pcie_perf_req and pcie_dev_rdy methods
556  * are supported (all asics).
557  * returns true if supported, false if not.
558  */
559 bool amdgpu_acpi_is_pcie_performance_request_supported(struct amdgpu_device *adev)
560 {
561 	struct amdgpu_atcs *atcs = &adev->atcs;
562 
563 	if (atcs->functions.pcie_perf_req && atcs->functions.pcie_dev_rdy)
564 		return true;
565 
566 	return false;
567 }
568 
569 /**
570  * amdgpu_acpi_pcie_notify_device_ready
571  *
572  * @adev: amdgpu_device pointer
573  *
574  * Executes the PCIE_DEVICE_READY_NOTIFICATION method
575  * (all asics).
576  * returns 0 on success, error on failure.
577  */
578 int amdgpu_acpi_pcie_notify_device_ready(struct amdgpu_device *adev)
579 {
580 	acpi_handle handle;
581 	union acpi_object *info;
582 	struct amdgpu_atcs *atcs = &adev->atcs;
583 
584 	/* Get the device handle */
585 	handle = ACPI_HANDLE(&adev->pdev->dev);
586 	if (!handle)
587 		return -EINVAL;
588 
589 	if (!atcs->functions.pcie_dev_rdy)
590 		return -EINVAL;
591 
592 	info = amdgpu_atcs_call(handle, ATCS_FUNCTION_PCIE_DEVICE_READY_NOTIFICATION, NULL);
593 	if (!info)
594 		return -EIO;
595 
596 	kfree(info);
597 
598 	return 0;
599 }
600 
601 /**
602  * amdgpu_acpi_pcie_performance_request
603  *
604  * @adev: amdgpu_device pointer
605  * @perf_req: requested perf level (pcie gen speed)
606  * @advertise: set advertise caps flag if set
607  *
608  * Executes the PCIE_PERFORMANCE_REQUEST method to
609  * change the pcie gen speed (all asics).
610  * returns 0 on success, error on failure.
611  */
612 int amdgpu_acpi_pcie_performance_request(struct amdgpu_device *adev,
613 					 u8 perf_req, bool advertise)
614 {
615 	acpi_handle handle;
616 	union acpi_object *info;
617 	struct amdgpu_atcs *atcs = &adev->atcs;
618 	struct atcs_pref_req_input atcs_input;
619 	struct atcs_pref_req_output atcs_output;
620 	struct acpi_buffer params;
621 	size_t size;
622 	u32 retry = 3;
623 
624 	if (amdgpu_acpi_pcie_notify_device_ready(adev))
625 		return -EINVAL;
626 
627 	/* Get the device handle */
628 	handle = ACPI_HANDLE(&adev->pdev->dev);
629 	if (!handle)
630 		return -EINVAL;
631 
632 	if (!atcs->functions.pcie_perf_req)
633 		return -EINVAL;
634 
635 	atcs_input.size = sizeof(struct atcs_pref_req_input);
636 	/* client id (bit 2-0: func num, 7-3: dev num, 15-8: bus num) */
637 	atcs_input.client_id = adev->pdev->devfn | (adev->pdev->bus->number << 8);
638 	atcs_input.valid_flags_mask = ATCS_VALID_FLAGS_MASK;
639 	atcs_input.flags = ATCS_WAIT_FOR_COMPLETION;
640 	if (advertise)
641 		atcs_input.flags |= ATCS_ADVERTISE_CAPS;
642 	atcs_input.req_type = ATCS_PCIE_LINK_SPEED;
643 	atcs_input.perf_req = perf_req;
644 
645 	params.length = sizeof(struct atcs_pref_req_input);
646 	params.pointer = &atcs_input;
647 
648 	while (retry--) {
649 		info = amdgpu_atcs_call(handle, ATCS_FUNCTION_PCIE_PERFORMANCE_REQUEST, &params);
650 		if (!info)
651 			return -EIO;
652 
653 		memset(&atcs_output, 0, sizeof(atcs_output));
654 
655 		size = *(u16 *) info->buffer.pointer;
656 		if (size < 3) {
657 			DRM_INFO("ATCS buffer is too small: %zu\n", size);
658 			kfree(info);
659 			return -EINVAL;
660 		}
661 		size = min(sizeof(atcs_output), size);
662 
663 		memcpy(&atcs_output, info->buffer.pointer, size);
664 
665 		kfree(info);
666 
667 		switch (atcs_output.ret_val) {
668 		case ATCS_REQUEST_REFUSED:
669 		default:
670 			return -EINVAL;
671 		case ATCS_REQUEST_COMPLETE:
672 			return 0;
673 		case ATCS_REQUEST_IN_PROGRESS:
674 			udelay(10);
675 			break;
676 		}
677 	}
678 
679 	return 0;
680 }
681 
682 /**
683  * amdgpu_acpi_event - handle notify events
684  *
685  * @nb: notifier block
686  * @val: val
687  * @data: acpi event
688  *
689  * Calls relevant amdgpu functions in response to various
690  * acpi events.
691  * Returns NOTIFY code
692  */
693 static int amdgpu_acpi_event(struct notifier_block *nb,
694 			     unsigned long val,
695 			     void *data)
696 {
697 	struct amdgpu_device *adev = container_of(nb, struct amdgpu_device, acpi_nb);
698 	struct acpi_bus_event *entry = (struct acpi_bus_event *)data;
699 
700 	if (strcmp(entry->device_class, ACPI_AC_CLASS) == 0) {
701 		if (power_supply_is_system_supplied() > 0)
702 			DRM_DEBUG_DRIVER("pm: AC\n");
703 		else
704 			DRM_DEBUG_DRIVER("pm: DC\n");
705 
706 		amdgpu_pm_acpi_event_handler(adev);
707 	}
708 
709 	/* Check for pending SBIOS requests */
710 	return amdgpu_atif_handler(adev, entry);
711 }
712 
713 /* Call all ACPI methods here */
714 /**
715  * amdgpu_acpi_init - init driver acpi support
716  *
717  * @adev: amdgpu_device pointer
718  *
719  * Verifies the AMD ACPI interfaces and registers with the acpi
720  * notifier chain (all asics).
721  * Returns 0 on success, error on failure.
722  */
723 int amdgpu_acpi_init(struct amdgpu_device *adev)
724 {
725 	acpi_handle handle, atif_handle;
726 	struct amdgpu_atif *atif;
727 	struct amdgpu_atcs *atcs = &adev->atcs;
728 	int ret;
729 
730 	/* Get the device handle */
731 	handle = ACPI_HANDLE(&adev->pdev->dev);
732 
733 	if (!adev->bios || !handle)
734 		return 0;
735 
736 	/* Call the ATCS method */
737 	ret = amdgpu_atcs_verify_interface(handle, atcs);
738 	if (ret) {
739 		DRM_DEBUG_DRIVER("Call to ATCS verify_interface failed: %d\n", ret);
740 	}
741 
742 	/* Probe for ATIF, and initialize it if found */
743 	atif_handle = amdgpu_atif_probe_handle(handle);
744 	if (!atif_handle)
745 		goto out;
746 
747 	atif = kzalloc(sizeof(*atif), GFP_KERNEL);
748 	if (!atif) {
749 		DRM_WARN("Not enough memory to initialize ATIF\n");
750 		goto out;
751 	}
752 	atif->handle = atif_handle;
753 
754 	/* Call the ATIF method */
755 	ret = amdgpu_atif_verify_interface(atif);
756 	if (ret) {
757 		DRM_DEBUG_DRIVER("Call to ATIF verify_interface failed: %d\n", ret);
758 		kfree(atif);
759 		goto out;
760 	}
761 	adev->atif = atif;
762 
763 	if (atif->notifications.brightness_change) {
764 		struct drm_encoder *tmp;
765 
766 		/* Find the encoder controlling the brightness */
767 		list_for_each_entry(tmp, &adev->ddev->mode_config.encoder_list,
768 				head) {
769 			struct amdgpu_encoder *enc = to_amdgpu_encoder(tmp);
770 
771 			if ((enc->devices & (ATOM_DEVICE_LCD_SUPPORT)) &&
772 			    enc->enc_priv) {
773 				struct amdgpu_encoder_atom_dig *dig = enc->enc_priv;
774 				if (dig->bl_dev) {
775 					atif->encoder_for_bl = enc;
776 					break;
777 				}
778 			}
779 		}
780 	}
781 
782 	if (atif->functions.sbios_requests && !atif->functions.system_params) {
783 		/* XXX check this workraround, if sbios request function is
784 		 * present we have to see how it's configured in the system
785 		 * params
786 		 */
787 		atif->functions.system_params = true;
788 	}
789 
790 	if (atif->functions.system_params) {
791 		ret = amdgpu_atif_get_notification_params(atif);
792 		if (ret) {
793 			DRM_DEBUG_DRIVER("Call to GET_SYSTEM_PARAMS failed: %d\n",
794 					ret);
795 			/* Disable notification */
796 			atif->notification_cfg.enabled = false;
797 		}
798 	}
799 
800 out:
801 	adev->acpi_nb.notifier_call = amdgpu_acpi_event;
802 	register_acpi_notifier(&adev->acpi_nb);
803 
804 	return ret;
805 }
806 
807 /**
808  * amdgpu_acpi_fini - tear down driver acpi support
809  *
810  * @adev: amdgpu_device pointer
811  *
812  * Unregisters with the acpi notifier chain (all asics).
813  */
814 void amdgpu_acpi_fini(struct amdgpu_device *adev)
815 {
816 	unregister_acpi_notifier(&adev->acpi_nb);
817 	if (adev->atif)
818 		kfree(adev->atif);
819 }
820