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