xref: /dragonfly/sys/dev/drm/amd/amdgpu/amdgpu_acpi.c (revision 7d3e9a5b)
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 ACPI_OBJECT *amdgpu_atif_call(struct amdgpu_atif *atif,
89 					   int function,
90 					   ACPI_BUFFER *params)
91 {
92 	ACPI_STATUS status;
93 	ACPI_OBJECT atif_arg_elements[2];
94 	ACPI_OBJECT_LIST atif_arg;
95 	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 = AcpiEvaluateObject(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 				 AcpiFormatException(status));
120 		AcpiOsFree(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 	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 	AcpiOsFree(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 	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 = AcpiGetHandle(dhandle, "ATIF", &handle);
230 	if (ACPI_SUCCESS(status))
231 		goto out;
232 
233 	if (amdgpu_has_atpx()) {
234 		status = AcpiGetHandle(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 	AcpiGetName(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 	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 	AcpiOsFree(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 	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 	AcpiOsFree(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 void amdgpu_atif_handler(struct amdgpu_device *adev,
366 			       UINT32 type)
367 {
368 	struct amdgpu_atif *atif = adev->atif;
369 	int count;
370 
371 	DRM_DEBUG_DRIVER("event, type = %#x\n", type);
372 
373 	/* Is this actually our event? */
374 	if (!atif ||
375 	    !atif->notification_cfg.enabled ||
376 	    type != atif->notification_cfg.command_code) {
377 		/* These events will generate keypresses otherwise */
378 #if 0
379                if (event->type == ACPI_VIDEO_NOTIFY_PROBE)
380                        return NOTIFY_BAD;
381 		else
382 			return NOTIFY_DONE;
383 #endif
384 	}
385 
386 	if (atif->functions.sbios_requests) {
387 		struct atif_sbios_requests req;
388 
389 		/* Check pending SBIOS requests */
390 		count = amdgpu_atif_get_sbios_requests(atif, &req);
391 
392 		if (count <= 0)
393 			return;
394 
395 		DRM_DEBUG_DRIVER("ATIF: %d pending SBIOS requests\n", count);
396 
397 		/* todo: add DC handling */
398 		if ((req.pending & ATIF_PANEL_BRIGHTNESS_CHANGE_REQUEST) &&
399 		    !amdgpu_device_has_dc_support(adev)) {
400 			struct amdgpu_encoder *enc = atif->encoder_for_bl;
401 
402 			if (enc) {
403 #if 0
404 				struct amdgpu_encoder_atom_dig *dig = enc->enc_priv;
405 #endif
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 #if 0
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 #endif
417 			}
418 		}
419 		if (req.pending & ATIF_DGPU_DISPLAY_EVENT) {
420 			if (adev->flags & AMD_IS_PX) {
421 				pm_runtime_get_sync(adev->ddev->dev);
422 				/* Just fire off a uevent and let userspace tell us what to do */
423 				drm_helper_hpd_irq_event(adev->ddev);
424 				pm_runtime_mark_last_busy(adev->ddev->dev);
425 				pm_runtime_put_autosuspend(adev->ddev->dev);
426 			}
427 		}
428 		/* TODO: check other events */
429 	}
430 
431 	/* We've handled the event, stop the notifier chain. The ACPI interface
432 	 * overloads ACPI_VIDEO_NOTIFY_PROBE, we don't want to send that to
433 	 * userspace if the event was generated only to signal a SBIOS
434 	 * request.
435 	 */
436 	return;
437 }
438 
439 /* Call the ATCS method
440  */
441 /**
442  * amdgpu_atcs_call - call an ATCS method
443  *
444  * @handle: acpi handle
445  * @function: the ATCS function to execute
446  * @params: ATCS function params
447  *
448  * Executes the requested ATCS function (all asics).
449  * Returns a pointer to the acpi output buffer.
450  */
451 static ACPI_OBJECT *amdgpu_atcs_call(ACPI_HANDLE handle, int function,
452 					   ACPI_BUFFER *params)
453 {
454 	ACPI_STATUS status;
455 	ACPI_OBJECT atcs_arg_elements[2];
456 	ACPI_OBJECT_LIST atcs_arg;
457 	ACPI_BUFFER buffer = { ACPI_ALLOCATE_BUFFER, NULL };
458 
459 	atcs_arg.Count = 2;
460 	atcs_arg.Pointer = &atcs_arg_elements[0];
461 
462 	atcs_arg_elements[0].Type = ACPI_TYPE_INTEGER;
463 	atcs_arg_elements[0].Integer.Value = function;
464 
465 	if (params) {
466 		atcs_arg_elements[1].Type = ACPI_TYPE_BUFFER;
467 		atcs_arg_elements[1].Buffer.Length = params->Length;
468 		atcs_arg_elements[1].Buffer.Pointer = params->Pointer;
469 	} else {
470 		/* We need a second fake parameter */
471 		atcs_arg_elements[1].Type = ACPI_TYPE_INTEGER;
472 		atcs_arg_elements[1].Integer.Value = 0;
473 	}
474 
475 	status = AcpiEvaluateObject(handle, "ATCS", &atcs_arg, &buffer);
476 
477 	/* Fail only if calling the method fails and ATIF is supported */
478 	if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
479 		DRM_DEBUG_DRIVER("failed to evaluate ATCS got %s\n",
480 				 AcpiFormatException(status));
481 		AcpiOsFree(buffer.Pointer);
482 		return NULL;
483 	}
484 
485 	return buffer.Pointer;
486 }
487 
488 /**
489  * amdgpu_atcs_parse_functions - parse supported functions
490  *
491  * @f: supported functions struct
492  * @mask: supported functions mask from ATCS
493  *
494  * Use the supported functions mask from ATCS function
495  * ATCS_FUNCTION_VERIFY_INTERFACE to determine what functions
496  * are supported (all asics).
497  */
498 static void amdgpu_atcs_parse_functions(struct amdgpu_atcs_functions *f, u32 mask)
499 {
500 	f->get_ext_state = mask & ATCS_GET_EXTERNAL_STATE_SUPPORTED;
501 	f->pcie_perf_req = mask & ATCS_PCIE_PERFORMANCE_REQUEST_SUPPORTED;
502 	f->pcie_dev_rdy = mask & ATCS_PCIE_DEVICE_READY_NOTIFICATION_SUPPORTED;
503 	f->pcie_bus_width = mask & ATCS_SET_PCIE_BUS_WIDTH_SUPPORTED;
504 }
505 
506 /**
507  * amdgpu_atcs_verify_interface - verify ATCS
508  *
509  * @handle: acpi handle
510  * @atcs: amdgpu atcs struct
511  *
512  * Execute the ATCS_FUNCTION_VERIFY_INTERFACE ATCS function
513  * to initialize ATCS and determine what features are supported
514  * (all asics).
515  * returns 0 on success, error on failure.
516  */
517 static int amdgpu_atcs_verify_interface(ACPI_HANDLE handle,
518 					struct amdgpu_atcs *atcs)
519 {
520 	ACPI_OBJECT *info;
521 	struct atcs_verify_interface output;
522 	size_t size;
523 	int err = 0;
524 
525 	info = amdgpu_atcs_call(handle, ATCS_FUNCTION_VERIFY_INTERFACE, NULL);
526 	if (!info)
527 		return -EIO;
528 
529 	memset(&output, 0, sizeof(output));
530 
531 	size = *(u16 *) info->Buffer.Pointer;
532 	if (size < 8) {
533 		DRM_INFO("ATCS buffer is too small: %zu\n", size);
534 		err = -EINVAL;
535 		goto out;
536 	}
537 	size = min(sizeof(output), size);
538 
539 	memcpy(&output, info->Buffer.Pointer, size);
540 
541 	/* TODO: check version? */
542 	DRM_DEBUG_DRIVER("ATCS version %u\n", output.version);
543 
544 	amdgpu_atcs_parse_functions(&atcs->functions, output.function_bits);
545 
546 out:
547 	AcpiOsFree(info);
548 	return err;
549 }
550 
551 /**
552  * amdgpu_acpi_is_pcie_performance_request_supported
553  *
554  * @adev: amdgpu_device pointer
555  *
556  * Check if the ATCS pcie_perf_req and pcie_dev_rdy methods
557  * are supported (all asics).
558  * returns true if supported, false if not.
559  */
560 bool amdgpu_acpi_is_pcie_performance_request_supported(struct amdgpu_device *adev)
561 {
562 	struct amdgpu_atcs *atcs = &adev->atcs;
563 
564 	if (atcs->functions.pcie_perf_req && atcs->functions.pcie_dev_rdy)
565 		return true;
566 
567 	return false;
568 }
569 
570 /**
571  * amdgpu_acpi_pcie_notify_device_ready
572  *
573  * @adev: amdgpu_device pointer
574  *
575  * Executes the PCIE_DEVICE_READY_NOTIFICATION method
576  * (all asics).
577  * returns 0 on success, error on failure.
578  */
579 int amdgpu_acpi_pcie_notify_device_ready(struct amdgpu_device *adev)
580 {
581 	ACPI_HANDLE handle;
582 	ACPI_OBJECT *info;
583 	struct amdgpu_atcs *atcs = &adev->atcs;
584 
585 	/* Get the device handle */
586 	handle = acpi_get_handle(adev->dev->bsddev);
587 	if (!handle)
588 		return -EINVAL;
589 
590 	if (!atcs->functions.pcie_dev_rdy)
591 		return -EINVAL;
592 
593 	info = amdgpu_atcs_call(handle, ATCS_FUNCTION_PCIE_DEVICE_READY_NOTIFICATION, NULL);
594 	if (!info)
595 		return -EIO;
596 
597 	AcpiOsFree(info);
598 
599 	return 0;
600 }
601 
602 /**
603  * amdgpu_acpi_pcie_performance_request
604  *
605  * @adev: amdgpu_device pointer
606  * @perf_req: requested perf level (pcie gen speed)
607  * @advertise: set advertise caps flag if set
608  *
609  * Executes the PCIE_PERFORMANCE_REQUEST method to
610  * change the pcie gen speed (all asics).
611  * returns 0 on success, error on failure.
612  */
613 int amdgpu_acpi_pcie_performance_request(struct amdgpu_device *adev,
614 					 u8 perf_req, bool advertise)
615 {
616 #if 0
617 	ACPI_HANDLE handle;
618 	ACPI_OBJECT *info;
619 	struct amdgpu_atcs *atcs = &adev->atcs;
620 	struct atcs_pref_req_input atcs_input;
621 	struct atcs_pref_req_output atcs_output;
622 	ACPI_BUFFER params;
623 	size_t size;
624 	u32 retry = 3;
625 
626 	if (amdgpu_acpi_pcie_notify_device_ready(adev))
627 		return -EINVAL;
628 
629 	/* Get the device handle */
630 	handle = acpi_get_handle(adev->dev->bsddev)
631 	if (!handle)
632 		return -EINVAL;
633 
634 	if (!atcs->functions.pcie_perf_req)
635 		return -EINVAL;
636 
637 	atcs_input.size = sizeof(struct atcs_pref_req_input);
638 	/* client id (bit 2-0: func num, 7-3: dev num, 15-8: bus num) */
639 	atcs_input.client_id = adev->pdev->devfn | (adev->pdev->bus->number << 8);
640 	atcs_input.valid_flags_mask = ATCS_VALID_FLAGS_MASK;
641 	atcs_input.flags = ATCS_WAIT_FOR_COMPLETION;
642 	if (advertise)
643 		atcs_input.flags |= ATCS_ADVERTISE_CAPS;
644 	atcs_input.req_type = ATCS_PCIE_LINK_SPEED;
645 	atcs_input.perf_req = perf_req;
646 
647 	params.Length = sizeof(struct atcs_pref_req_input);
648 	params.Pointer = &atcs_input;
649 
650 	while (retry--) {
651 		info = amdgpu_atcs_call(handle, ATCS_FUNCTION_PCIE_PERFORMANCE_REQUEST, &params);
652 		if (!info)
653 			return -EIO;
654 
655 		memset(&atcs_output, 0, sizeof(atcs_output));
656 
657 		size = *(u16 *) info->Buffer.Pointer;
658 		if (size < 3) {
659 			DRM_INFO("ATCS buffer is too small: %zu\n", size);
660 			AcpiOsFree(info);
661 			return -EINVAL;
662 		}
663 		size = min(sizeof(atcs_output), size);
664 
665 		memcpy(&atcs_output, info->Buffer.Pointer, size);
666 
667 		AcpiOsFree(info);
668 
669 		switch (atcs_output.ret_val) {
670 		case ATCS_REQUEST_REFUSED:
671 		default:
672 			return -EINVAL;
673 		case ATCS_REQUEST_COMPLETE:
674 			return 0;
675 		case ATCS_REQUEST_IN_PROGRESS:
676 			udelay(10);
677 			break;
678 		}
679 	}
680 
681 	return 0;
682 #else
683 	return -EINVAL;
684 #endif
685 }
686 
687 /**
688  * amdgpu_acpi_event - handle notify events
689  *
690  * @nb: notifier block
691  * @val: val
692  * @data: acpi event
693  *
694  * Calls relevant amdgpu functions in response to various
695  * acpi events.
696  * Returns NOTIFY code
697  */
698 static void amdgpu_acpi_event(ACPI_HANDLE handle,
699 			     UINT32 type,
700 			     void *context)
701 {
702 	struct amdgpu_device *adev = (struct amdgpu_device *)context;
703 
704 #if 0
705 	if (strcmp(entry->device_class, ACPI_AC_CLASS) == 0) {
706 		if (power_supply_is_system_supplied() > 0)
707 			DRM_DEBUG_DRIVER("pm: AC\n");
708 		else
709 			DRM_DEBUG_DRIVER("pm: DC\n");
710 
711 		amdgpu_pm_acpi_event_handler(adev);
712 	}
713 #endif
714 
715 	/* Check for pending SBIOS requests */
716 	return amdgpu_atif_handler(adev, type);
717 }
718 
719 /* Call all ACPI methods here */
720 /**
721  * amdgpu_acpi_init - init driver acpi support
722  *
723  * @adev: amdgpu_device pointer
724  *
725  * Verifies the AMD ACPI interfaces and registers with the acpi
726  * notifier chain (all asics).
727  * Returns 0 on success, error on failure.
728  */
729 int amdgpu_acpi_init(struct amdgpu_device *adev)
730 {
731 	ACPI_HANDLE handle, atif_handle;
732 	struct amdgpu_atif *atif;
733 	struct amdgpu_atcs *atcs = &adev->atcs;
734 	int ret;
735 
736 	/* Get the device handle */
737 	handle = acpi_get_handle(adev->dev->bsddev);
738 
739 	if (!adev->bios || !handle)
740 		return 0;
741 
742 	/* Call the ATCS method */
743 	ret = amdgpu_atcs_verify_interface(handle, atcs);
744 	if (ret) {
745 		DRM_DEBUG_DRIVER("Call to ATCS verify_interface failed: %d\n", ret);
746 	}
747 
748 	/* Probe for ATIF, and initialize it if found */
749 	atif_handle = amdgpu_atif_probe_handle(handle);
750 	if (!atif_handle)
751 		goto out;
752 
753 	atif = kzalloc(sizeof(*atif), GFP_KERNEL);
754 	if (!atif) {
755 		DRM_WARN("Not enough memory to initialize ATIF\n");
756 		goto out;
757 	}
758 	atif->handle = atif_handle;
759 
760 	/* Call the ATIF method */
761 	ret = amdgpu_atif_verify_interface(atif);
762 	if (ret) {
763 		DRM_DEBUG_DRIVER("Call to ATIF verify_interface failed: %d\n", ret);
764 		AcpiOsFree(atif);
765 		goto out;
766 	}
767 	adev->atif = atif;
768 
769 	if (atif->notifications.brightness_change) {
770 		struct drm_encoder *tmp;
771 
772 		/* Find the encoder controlling the brightness */
773 		list_for_each_entry(tmp, &adev->ddev->mode_config.encoder_list,
774 				head) {
775 			struct amdgpu_encoder *enc = to_amdgpu_encoder(tmp);
776 
777 			if ((enc->devices & (ATOM_DEVICE_LCD_SUPPORT)) &&
778 			    enc->enc_priv) {
779 				struct amdgpu_encoder_atom_dig *dig = enc->enc_priv;
780 				if (dig->bl_dev) {
781 					atif->encoder_for_bl = enc;
782 					break;
783 				}
784 			}
785 		}
786 	}
787 
788 	if (atif->functions.sbios_requests && !atif->functions.system_params) {
789 		/* XXX check this workraround, if sbios request function is
790 		 * present we have to see how it's configured in the system
791 		 * params
792 		 */
793 		atif->functions.system_params = true;
794 	}
795 
796 	if (atif->functions.system_params) {
797 		ret = amdgpu_atif_get_notification_params(atif);
798 		if (ret) {
799 			DRM_DEBUG_DRIVER("Call to GET_SYSTEM_PARAMS failed: %d\n",
800 					ret);
801 			/* Disable notification */
802 			atif->notification_cfg.enabled = false;
803 		}
804 	}
805 
806 out:
807 	adev->acpi.handle = handle;
808 	adev->acpi.notifier_call = amdgpu_acpi_event;
809 	AcpiInstallNotifyHandler(handle, ACPI_DEVICE_NOTIFY,
810 	    adev->acpi.notifier_call, adev);
811 
812 	return ret;
813 }
814 
815 /**
816  * amdgpu_acpi_fini - tear down driver acpi support
817  *
818  * @adev: amdgpu_device pointer
819  *
820  * Unregisters with the acpi notifier chain (all asics).
821  */
822 void amdgpu_acpi_fini(struct amdgpu_device *adev)
823 {
824 	if (adev->atif)
825 		AcpiOsFree(adev->atif);
826 }
827