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