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