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