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