xref: /netbsd-src/sys/external/bsd/drm2/dist/drm/radeon/radeon_acpi.c (revision 7d62b00eb9ad855ffcd7da46b41e23feb5476fac)
1 /*	$NetBSD: radeon_acpi.c,v 1.4 2022/02/27 14:24:27 riastradh Exp $	*/
2 
3 /*
4  * Copyright 2012 Advanced Micro Devices, Inc.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
20  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22  * OTHER DEALINGS IN THE SOFTWARE.
23  *
24  */
25 
26 #include <sys/cdefs.h>
27 __KERNEL_RCSID(0, "$NetBSD: radeon_acpi.c,v 1.4 2022/02/27 14:24:27 riastradh Exp $");
28 
29 #include <linux/acpi.h>
30 #include <linux/pci.h>
31 #include <linux/pm_runtime.h>
32 #include <linux/power_supply.h>
33 #include <linux/slab.h>
34 
35 #include <acpi/acpi_bus.h>
36 #include <acpi/video.h>
37 
38 #include <drm/drm_crtc_helper.h>
39 #include <drm/drm_probe_helper.h>
40 
41 #include "atom.h"
42 #include "radeon.h"
43 #include "radeon_acpi.h"
44 
45 #if defined(CONFIG_VGA_SWITCHEROO)
46 bool radeon_atpx_dgpu_req_power_for_displays(void);
47 #else
48 static inline bool radeon_atpx_dgpu_req_power_for_displays(void) { return false; }
49 #endif
50 
51 #define ACPI_AC_CLASS           "ac_adapter"
52 
53 extern void radeon_pm_acpi_event_handler(struct radeon_device *rdev);
54 
55 struct atif_verify_interface {
56 	u16 size;		/* structure size in bytes (includes size field) */
57 	u16 version;		/* version */
58 	u32 notification_mask;	/* supported notifications mask */
59 	u32 function_bits;	/* supported functions bit vector */
60 } __packed;
61 
62 struct atif_system_params {
63 	u16 size;		/* structure size in bytes (includes size field) */
64 	u32 valid_mask;		/* valid flags mask */
65 	u32 flags;		/* flags */
66 	u8 command_code;	/* notify command code */
67 } __packed;
68 
69 struct atif_sbios_requests {
70 	u16 size;		/* structure size in bytes (includes size field) */
71 	u32 pending;		/* pending sbios requests */
72 	u8 panel_exp_mode;	/* panel expansion mode */
73 	u8 thermal_gfx;		/* thermal state: target gfx controller */
74 	u8 thermal_state;	/* thermal state: state id (0: exit state, non-0: state) */
75 	u8 forced_power_gfx;	/* forced power state: target gfx controller */
76 	u8 forced_power_state;	/* forced power state: state id */
77 	u8 system_power_src;	/* system power source */
78 	u8 backlight_level;	/* panel backlight level (0-255) */
79 } __packed;
80 
81 #define ATIF_NOTIFY_MASK	0x3
82 #define ATIF_NOTIFY_NONE	0
83 #define ATIF_NOTIFY_81		1
84 #define ATIF_NOTIFY_N		2
85 
86 struct atcs_verify_interface {
87 	u16 size;		/* structure size in bytes (includes size field) */
88 	u16 version;		/* version */
89 	u32 function_bits;	/* supported functions bit vector */
90 } __packed;
91 
92 #define ATCS_VALID_FLAGS_MASK	0x3
93 
94 struct atcs_pref_req_input {
95 	u16 size;		/* structure size in bytes (includes size field) */
96 	u16 client_id;		/* client id (bit 2-0: func num, 7-3: dev num, 15-8: bus num) */
97 	u16 valid_flags_mask;	/* valid flags mask */
98 	u16 flags;		/* flags */
99 	u8 req_type;		/* request type */
100 	u8 perf_req;		/* performance request */
101 } __packed;
102 
103 struct atcs_pref_req_output {
104 	u16 size;		/* structure size in bytes (includes size field) */
105 	u8 ret_val;		/* return value */
106 } __packed;
107 
108 /* Call the ATIF method
109  */
110 /**
111  * radeon_atif_call - call an ATIF method
112  *
113  * @handle: acpi handle
114  * @function: the ATIF function to execute
115  * @params: ATIF function params
116  *
117  * Executes the requested ATIF function (all asics).
118  * Returns a pointer to the acpi output buffer.
119  */
120 static union acpi_object *radeon_atif_call(acpi_handle handle, int function,
121 		struct acpi_buffer *params)
122 {
123 	acpi_status status;
124 	union acpi_object atif_arg_elements[2];
125 	struct acpi_object_list atif_arg;
126 	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
127 
128 	atif_arg.count = 2;
129 	atif_arg.pointer = &atif_arg_elements[0];
130 
131 	atif_arg_elements[0].type = ACPI_TYPE_INTEGER;
132 	atif_arg_elements[0].integer.value = function;
133 
134 	if (params) {
135 		atif_arg_elements[1].type = ACPI_TYPE_BUFFER;
136 		atif_arg_elements[1].buffer.length = params->length;
137 		atif_arg_elements[1].buffer.pointer = params->pointer;
138 	} else {
139 		/* We need a second fake parameter */
140 		atif_arg_elements[1].type = ACPI_TYPE_INTEGER;
141 		atif_arg_elements[1].integer.value = 0;
142 	}
143 
144 	status = acpi_evaluate_object(handle, "ATIF", &atif_arg, &buffer);
145 
146 	/* Fail only if calling the method fails and ATIF is supported */
147 	if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
148 		DRM_DEBUG_DRIVER("failed to evaluate ATIF got %s\n",
149 				 acpi_format_exception(status));
150 		ACPI_FREE(buffer.pointer);
151 		return NULL;
152 	}
153 
154 	return buffer.pointer;
155 }
156 
157 /**
158  * radeon_atif_parse_notification - parse supported notifications
159  *
160  * @n: supported notifications struct
161  * @mask: supported notifications mask from ATIF
162  *
163  * Use the supported notifications mask from ATIF function
164  * ATIF_FUNCTION_VERIFY_INTERFACE to determine what notifications
165  * are supported (all asics).
166  */
167 static void radeon_atif_parse_notification(struct radeon_atif_notifications *n, u32 mask)
168 {
169 	n->display_switch = mask & ATIF_DISPLAY_SWITCH_REQUEST_SUPPORTED;
170 	n->expansion_mode_change = mask & ATIF_EXPANSION_MODE_CHANGE_REQUEST_SUPPORTED;
171 	n->thermal_state = mask & ATIF_THERMAL_STATE_CHANGE_REQUEST_SUPPORTED;
172 	n->forced_power_state = mask & ATIF_FORCED_POWER_STATE_CHANGE_REQUEST_SUPPORTED;
173 	n->system_power_state = mask & ATIF_SYSTEM_POWER_SOURCE_CHANGE_REQUEST_SUPPORTED;
174 	n->display_conf_change = mask & ATIF_DISPLAY_CONF_CHANGE_REQUEST_SUPPORTED;
175 	n->px_gfx_switch = mask & ATIF_PX_GFX_SWITCH_REQUEST_SUPPORTED;
176 	n->brightness_change = mask & ATIF_PANEL_BRIGHTNESS_CHANGE_REQUEST_SUPPORTED;
177 	n->dgpu_display_event = mask & ATIF_DGPU_DISPLAY_EVENT_SUPPORTED;
178 }
179 
180 /**
181  * radeon_atif_parse_functions - parse supported functions
182  *
183  * @f: supported functions struct
184  * @mask: supported functions mask from ATIF
185  *
186  * Use the supported functions mask from ATIF function
187  * ATIF_FUNCTION_VERIFY_INTERFACE to determine what functions
188  * are supported (all asics).
189  */
190 static void radeon_atif_parse_functions(struct radeon_atif_functions *f, u32 mask)
191 {
192 	f->system_params = mask & ATIF_GET_SYSTEM_PARAMETERS_SUPPORTED;
193 	f->sbios_requests = mask & ATIF_GET_SYSTEM_BIOS_REQUESTS_SUPPORTED;
194 	f->select_active_disp = mask & ATIF_SELECT_ACTIVE_DISPLAYS_SUPPORTED;
195 	f->lid_state = mask & ATIF_GET_LID_STATE_SUPPORTED;
196 	f->get_tv_standard = mask & ATIF_GET_TV_STANDARD_FROM_CMOS_SUPPORTED;
197 	f->set_tv_standard = mask & ATIF_SET_TV_STANDARD_IN_CMOS_SUPPORTED;
198 	f->get_panel_expansion_mode = mask & ATIF_GET_PANEL_EXPANSION_MODE_FROM_CMOS_SUPPORTED;
199 	f->set_panel_expansion_mode = mask & ATIF_SET_PANEL_EXPANSION_MODE_IN_CMOS_SUPPORTED;
200 	f->temperature_change = mask & ATIF_TEMPERATURE_CHANGE_NOTIFICATION_SUPPORTED;
201 	f->graphics_device_types = mask & ATIF_GET_GRAPHICS_DEVICE_TYPES_SUPPORTED;
202 }
203 
204 /**
205  * radeon_atif_verify_interface - verify ATIF
206  *
207  * @handle: acpi handle
208  * @atif: radeon atif struct
209  *
210  * Execute the ATIF_FUNCTION_VERIFY_INTERFACE ATIF function
211  * to initialize ATIF and determine what features are supported
212  * (all asics).
213  * returns 0 on success, error on failure.
214  */
215 static int radeon_atif_verify_interface(acpi_handle handle,
216 		struct radeon_atif *atif)
217 {
218 	union acpi_object *info;
219 	struct atif_verify_interface output;
220 	size_t size;
221 	int err = 0;
222 
223 	info = radeon_atif_call(handle, ATIF_FUNCTION_VERIFY_INTERFACE, NULL);
224 	if (!info)
225 		return -EIO;
226 
227 	memset(&output, 0, sizeof(output));
228 
229 	size = *(u16 *) info->buffer.pointer;
230 	if (size < 12) {
231 		DRM_INFO("ATIF buffer is too small: %zu\n", size);
232 		err = -EINVAL;
233 		goto out;
234 	}
235 	size = min(sizeof(output), size);
236 
237 	memcpy(&output, info->buffer.pointer, size);
238 
239 	/* TODO: check version? */
240 	DRM_DEBUG_DRIVER("ATIF version %u\n", output.version);
241 
242 	radeon_atif_parse_notification(&atif->notifications, output.notification_mask);
243 	radeon_atif_parse_functions(&atif->functions, output.function_bits);
244 
245 out:
246 	ACPI_FREE(info);
247 	return err;
248 }
249 
250 /**
251  * radeon_atif_get_notification_params - determine notify configuration
252  *
253  * @handle: acpi handle
254  * @n: atif notification configuration struct
255  *
256  * Execute the ATIF_FUNCTION_GET_SYSTEM_PARAMETERS ATIF function
257  * to determine if a notifier is used and if so which one
258  * (all asics).  This is either Notify(VGA, 0x81) or Notify(VGA, n)
259  * where n is specified in the result if a notifier is used.
260  * Returns 0 on success, error on failure.
261  */
262 static int radeon_atif_get_notification_params(acpi_handle handle,
263 		struct radeon_atif_notification_cfg *n)
264 {
265 	union acpi_object *info;
266 	struct atif_system_params params;
267 	size_t size;
268 	int err = 0;
269 
270 	info = radeon_atif_call(handle, ATIF_FUNCTION_GET_SYSTEM_PARAMETERS, 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 	ACPI_FREE(info);
310 	return err;
311 }
312 
313 /**
314  * radeon_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 radeon_atif_get_sbios_requests(acpi_handle handle,
325 		struct atif_sbios_requests *req)
326 {
327 	union acpi_object *info;
328 	size_t size;
329 	int count = 0;
330 
331 	info = radeon_atif_call(handle, ATIF_FUNCTION_GET_SYSTEM_BIOS_REQUESTS, 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 	ACPI_FREE(info);
350 	return count;
351 }
352 
353 /**
354  * radeon_atif_handler - handle ATIF notify requests
355  *
356  * @rdev: radeon_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  */
363 static int radeon_atif_handler(struct radeon_device *rdev,
364 		struct acpi_bus_event *event)
365 {
366 	struct radeon_atif *atif = &rdev->atif;
367 	struct atif_sbios_requests req;
368 	acpi_handle handle;
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 	if (!atif->notification_cfg.enabled ||
378 			event->type != atif->notification_cfg.command_code)
379 		/* Not our event */
380 		return NOTIFY_DONE;
381 
382 	/* Check pending SBIOS requests */
383 	handle = ACPI_HANDLE(&rdev->pdev->dev);
384 	count = radeon_atif_get_sbios_requests(handle, &req);
385 
386 	if (count <= 0)
387 		return NOTIFY_DONE;
388 
389 	DRM_DEBUG_DRIVER("ATIF: %d pending SBIOS requests\n", count);
390 
391 	if (req.pending & ATIF_PANEL_BRIGHTNESS_CHANGE_REQUEST) {
392 		struct radeon_encoder *enc = atif->encoder_for_bl;
393 
394 		if (enc) {
395 			DRM_DEBUG_DRIVER("Changing brightness to %d\n",
396 					req.backlight_level);
397 
398 			radeon_set_backlight_level(rdev, enc, req.backlight_level);
399 
400 #if defined(CONFIG_BACKLIGHT_CLASS_DEVICE) || defined(CONFIG_BACKLIGHT_CLASS_DEVICE_MODULE)
401 			if (rdev->is_atom_bios) {
402 				struct radeon_encoder_atom_dig *dig = enc->enc_priv;
403 				backlight_force_update(dig->bl_dev,
404 						       BACKLIGHT_UPDATE_HOTKEY);
405 			} else {
406 				struct radeon_encoder_lvds *dig = enc->enc_priv;
407 				backlight_force_update(dig->bl_dev,
408 						       BACKLIGHT_UPDATE_HOTKEY);
409 			}
410 #endif
411 		}
412 	}
413 	if (req.pending & ATIF_DGPU_DISPLAY_EVENT) {
414 		if ((rdev->flags & RADEON_IS_PX) &&
415 		    radeon_atpx_dgpu_req_power_for_displays()) {
416 			pm_runtime_get_sync(rdev->ddev->dev);
417 			/* Just fire off a uevent and let userspace tell us what to do */
418 			drm_helper_hpd_irq_event(rdev->ddev);
419 			pm_runtime_mark_last_busy(rdev->ddev->dev);
420 			pm_runtime_put_autosuspend(rdev->ddev->dev);
421 		}
422 	}
423 	/* TODO: check other events */
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  * radeon_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 *radeon_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 		ACPI_FREE(buffer.pointer);
476 		return NULL;
477 	}
478 
479 	return buffer.pointer;
480 }
481 
482 /**
483  * radeon_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 radeon_atcs_parse_functions(struct radeon_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  * radeon_atcs_verify_interface - verify ATCS
502  *
503  * @handle: acpi handle
504  * @atcs: radeon 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 radeon_atcs_verify_interface(acpi_handle handle,
512 					struct radeon_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 = radeon_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 	radeon_atcs_parse_functions(&atcs->functions, output.function_bits);
539 
540 out:
541 	ACPI_FREE(info);
542 	return err;
543 }
544 
545 /**
546  * radeon_acpi_is_pcie_performance_request_supported
547  *
548  * @rdev: radeon_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 radeon_acpi_is_pcie_performance_request_supported(struct radeon_device *rdev)
555 {
556 	struct radeon_atcs *atcs = &rdev->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  * radeon_acpi_pcie_notify_device_ready
566  *
567  * @rdev: radeon_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 radeon_acpi_pcie_notify_device_ready(struct radeon_device *rdev)
574 {
575 	acpi_handle handle;
576 	union acpi_object *info;
577 	struct radeon_atcs *atcs = &rdev->atcs;
578 
579 	/* Get the device handle */
580 	handle = ACPI_HANDLE(&rdev->pdev->dev);
581 	if (!handle)
582 		return -EINVAL;
583 
584 	if (!atcs->functions.pcie_dev_rdy)
585 		return -EINVAL;
586 
587 	info = radeon_atcs_call(handle, ATCS_FUNCTION_PCIE_DEVICE_READY_NOTIFICATION, NULL);
588 	if (!info)
589 		return -EIO;
590 
591 	ACPI_FREE(info);
592 
593 	return 0;
594 }
595 
596 /**
597  * radeon_acpi_pcie_performance_request
598  *
599  * @rdev: radeon_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 radeon_acpi_pcie_performance_request(struct radeon_device *rdev,
608 					 u8 perf_req, bool advertise)
609 {
610 	acpi_handle handle;
611 	union acpi_object *info;
612 	struct radeon_atcs *atcs = &rdev->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 	/* Get the device handle */
620 	handle = ACPI_HANDLE(&rdev->pdev->dev);
621 	if (!handle)
622 		return -EINVAL;
623 
624 	if (!atcs->functions.pcie_perf_req)
625 		return -EINVAL;
626 
627 	atcs_input.size = sizeof(struct atcs_pref_req_input);
628 	/* client id (bit 2-0: func num, 7-3: dev num, 15-8: bus num) */
629 	atcs_input.client_id = rdev->pdev->devfn | (rdev->pdev->bus->number << 8);
630 	atcs_input.valid_flags_mask = ATCS_VALID_FLAGS_MASK;
631 	atcs_input.flags = ATCS_WAIT_FOR_COMPLETION;
632 	if (advertise)
633 		atcs_input.flags |= ATCS_ADVERTISE_CAPS;
634 	atcs_input.req_type = ATCS_PCIE_LINK_SPEED;
635 	atcs_input.perf_req = perf_req;
636 
637 	params.length = sizeof(struct atcs_pref_req_input);
638 	params.pointer = &atcs_input;
639 
640 	while (retry--) {
641 		info = radeon_atcs_call(handle, ATCS_FUNCTION_PCIE_PERFORMANCE_REQUEST, &params);
642 		if (!info)
643 			return -EIO;
644 
645 		memset(&atcs_output, 0, sizeof(atcs_output));
646 
647 		size = *(u16 *) info->buffer.pointer;
648 		if (size < 3) {
649 			DRM_INFO("ATCS buffer is too small: %zu\n", size);
650 			ACPI_FREE(info);
651 			return -EINVAL;
652 		}
653 		size = min(sizeof(atcs_output), size);
654 
655 		memcpy(&atcs_output, info->buffer.pointer, size);
656 
657 		ACPI_FREE(info);
658 
659 		switch (atcs_output.ret_val) {
660 		case ATCS_REQUEST_REFUSED:
661 		default:
662 			return -EINVAL;
663 		case ATCS_REQUEST_COMPLETE:
664 			return 0;
665 		case ATCS_REQUEST_IN_PROGRESS:
666 			udelay(10);
667 			break;
668 		}
669 	}
670 
671 	return 0;
672 }
673 
674 /**
675  * radeon_acpi_event - handle notify events
676  *
677  * @nb: notifier block
678  * @val: val
679  * @data: acpi event
680  *
681  * Calls relevant radeon functions in response to various
682  * acpi events.
683  * Returns NOTIFY code
684  */
685 static int radeon_acpi_event(struct notifier_block *nb,
686 			     unsigned long val,
687 			     void *data)
688 {
689 	struct radeon_device *rdev = container_of(nb, struct radeon_device, acpi_nb);
690 	struct acpi_bus_event *entry = (struct acpi_bus_event *)data;
691 
692 	if (strcmp(entry->device_class, ACPI_AC_CLASS) == 0) {
693 		if (power_supply_is_system_supplied() > 0)
694 			DRM_DEBUG_DRIVER("pm: AC\n");
695 		else
696 			DRM_DEBUG_DRIVER("pm: DC\n");
697 
698 		radeon_pm_acpi_event_handler(rdev);
699 	}
700 
701 	/* Check for pending SBIOS requests */
702 	return radeon_atif_handler(rdev, entry);
703 }
704 
705 /* Call all ACPI methods here */
706 /**
707  * radeon_acpi_init - init driver acpi support
708  *
709  * @rdev: radeon_device pointer
710  *
711  * Verifies the AMD ACPI interfaces and registers with the acpi
712  * notifier chain (all asics).
713  * Returns 0 on success, error on failure.
714  */
715 int radeon_acpi_init(struct radeon_device *rdev)
716 {
717 	acpi_handle handle;
718 	struct radeon_atif *atif = &rdev->atif;
719 	struct radeon_atcs *atcs = &rdev->atcs;
720 	int ret;
721 
722 	/* Get the device handle */
723 	handle = ACPI_HANDLE(&rdev->pdev->dev);
724 
725 	/* No need to proceed if we're sure that ATIF is not supported */
726 	if (!ASIC_IS_AVIVO(rdev) || !rdev->bios || !handle)
727 		return 0;
728 
729 	/* Call the ATCS method */
730 	ret = radeon_atcs_verify_interface(handle, atcs);
731 	if (ret) {
732 		DRM_DEBUG_DRIVER("Call to ATCS verify_interface failed: %d\n", ret);
733 	}
734 
735 	/* Call the ATIF method */
736 	ret = radeon_atif_verify_interface(handle, atif);
737 	if (ret) {
738 		DRM_DEBUG_DRIVER("Call to ATIF verify_interface failed: %d\n", ret);
739 		goto out;
740 	}
741 
742 	if (atif->notifications.brightness_change) {
743 		struct drm_encoder *tmp;
744 		struct radeon_encoder *target = NULL;
745 
746 		/* Find the encoder controlling the brightness */
747 		list_for_each_entry(tmp, &rdev->ddev->mode_config.encoder_list,
748 				head) {
749 			struct radeon_encoder *enc = to_radeon_encoder(tmp);
750 
751 			if ((enc->devices & (ATOM_DEVICE_LCD_SUPPORT)) &&
752 			    enc->enc_priv) {
753 				if (rdev->is_atom_bios) {
754 					struct radeon_encoder_atom_dig *dig = enc->enc_priv;
755 					if (dig->bl_dev) {
756 						target = enc;
757 						break;
758 					}
759 				} else {
760 					struct radeon_encoder_lvds *dig = enc->enc_priv;
761 					if (dig->bl_dev) {
762 						target = enc;
763 						break;
764 					}
765 				}
766 			}
767 		}
768 
769 		atif->encoder_for_bl = target;
770 	}
771 
772 	if (atif->functions.sbios_requests && !atif->functions.system_params) {
773 		/* XXX check this workraround, if sbios request function is
774 		 * present we have to see how it's configured in the system
775 		 * params
776 		 */
777 		atif->functions.system_params = true;
778 	}
779 
780 	if (atif->functions.system_params) {
781 		ret = radeon_atif_get_notification_params(handle,
782 				&atif->notification_cfg);
783 		if (ret) {
784 			DRM_DEBUG_DRIVER("Call to GET_SYSTEM_PARAMS failed: %d\n",
785 					ret);
786 			/* Disable notification */
787 			atif->notification_cfg.enabled = false;
788 		}
789 	}
790 
791 out:
792 	rdev->acpi_nb.notifier_call = radeon_acpi_event;
793 	register_acpi_notifier(&rdev->acpi_nb);
794 
795 	return ret;
796 }
797 
798 /**
799  * radeon_acpi_fini - tear down driver acpi support
800  *
801  * @rdev: radeon_device pointer
802  *
803  * Unregisters with the acpi notifier chain (all asics).
804  */
805 void radeon_acpi_fini(struct radeon_device *rdev)
806 {
807 	unregister_acpi_notifier(&rdev->acpi_nb);
808 }
809