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