xref: /dflybsd-src/sys/dev/drm/amd/amdgpu/amdgpu_acpi.c (revision 789731325bde747251c28a37e0a00ed4efb88c46)
1b843c749SSergey Zigachev /*
2b843c749SSergey Zigachev  * Copyright 2012 Advanced Micro Devices, Inc.
3b843c749SSergey Zigachev  *
4b843c749SSergey Zigachev  * Permission is hereby granted, free of charge, to any person obtaining a
5b843c749SSergey Zigachev  * copy of this software and associated documentation files (the "Software"),
6b843c749SSergey Zigachev  * to deal in the Software without restriction, including without limitation
7b843c749SSergey Zigachev  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8b843c749SSergey Zigachev  * and/or sell copies of the Software, and to permit persons to whom the
9b843c749SSergey Zigachev  * Software is furnished to do so, subject to the following conditions:
10b843c749SSergey Zigachev  *
11b843c749SSergey Zigachev  * The above copyright notice and this permission notice shall be included in
12b843c749SSergey Zigachev  * all copies or substantial portions of the Software.
13b843c749SSergey Zigachev  *
14b843c749SSergey Zigachev  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15b843c749SSergey Zigachev  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16b843c749SSergey Zigachev  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17b843c749SSergey Zigachev  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18b843c749SSergey Zigachev  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19b843c749SSergey Zigachev  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20b843c749SSergey Zigachev  * OTHER DEALINGS IN THE SOFTWARE.
21b843c749SSergey Zigachev  *
22b843c749SSergey Zigachev  */
23b843c749SSergey Zigachev 
24b843c749SSergey Zigachev #include <linux/pci.h>
25b843c749SSergey Zigachev #include <linux/acpi.h>
26b843c749SSergey Zigachev #include <linux/slab.h>
27b843c749SSergey Zigachev #include <linux/power_supply.h>
28b843c749SSergey Zigachev #include <linux/pm_runtime.h>
29b843c749SSergey Zigachev #include <acpi/video.h>
30b843c749SSergey Zigachev #include <drm/drmP.h>
31b843c749SSergey Zigachev #include <drm/drm_crtc_helper.h>
32b843c749SSergey Zigachev #include "amdgpu.h"
33b843c749SSergey Zigachev #include "amdgpu_pm.h"
34b843c749SSergey Zigachev #include "amd_acpi.h"
35b843c749SSergey Zigachev #include "atom.h"
36b843c749SSergey Zigachev 
37b843c749SSergey Zigachev struct amdgpu_atif_notification_cfg {
38b843c749SSergey Zigachev 	bool enabled;
39b843c749SSergey Zigachev 	int command_code;
40b843c749SSergey Zigachev };
41b843c749SSergey Zigachev 
42b843c749SSergey Zigachev struct amdgpu_atif_notifications {
43b843c749SSergey Zigachev 	bool display_switch;
44b843c749SSergey Zigachev 	bool expansion_mode_change;
45b843c749SSergey Zigachev 	bool thermal_state;
46b843c749SSergey Zigachev 	bool forced_power_state;
47b843c749SSergey Zigachev 	bool system_power_state;
48b843c749SSergey Zigachev 	bool display_conf_change;
49b843c749SSergey Zigachev 	bool px_gfx_switch;
50b843c749SSergey Zigachev 	bool brightness_change;
51b843c749SSergey Zigachev 	bool dgpu_display_event;
52b843c749SSergey Zigachev };
53b843c749SSergey Zigachev 
54b843c749SSergey Zigachev struct amdgpu_atif_functions {
55b843c749SSergey Zigachev 	bool system_params;
56b843c749SSergey Zigachev 	bool sbios_requests;
57b843c749SSergey Zigachev 	bool select_active_disp;
58b843c749SSergey Zigachev 	bool lid_state;
59b843c749SSergey Zigachev 	bool get_tv_standard;
60b843c749SSergey Zigachev 	bool set_tv_standard;
61b843c749SSergey Zigachev 	bool get_panel_expansion_mode;
62b843c749SSergey Zigachev 	bool set_panel_expansion_mode;
63b843c749SSergey Zigachev 	bool temperature_change;
64b843c749SSergey Zigachev 	bool graphics_device_types;
65b843c749SSergey Zigachev };
66b843c749SSergey Zigachev 
67b843c749SSergey Zigachev struct amdgpu_atif {
68*78973132SSergey Zigachev 	ACPI_HANDLE handle;
69b843c749SSergey Zigachev 
70b843c749SSergey Zigachev 	struct amdgpu_atif_notifications notifications;
71b843c749SSergey Zigachev 	struct amdgpu_atif_functions functions;
72b843c749SSergey Zigachev 	struct amdgpu_atif_notification_cfg notification_cfg;
73b843c749SSergey Zigachev 	struct amdgpu_encoder *encoder_for_bl;
74b843c749SSergey Zigachev };
75b843c749SSergey Zigachev 
76b843c749SSergey Zigachev /* Call the ATIF method
77b843c749SSergey Zigachev  */
78b843c749SSergey Zigachev /**
79b843c749SSergey Zigachev  * amdgpu_atif_call - call an ATIF method
80b843c749SSergey Zigachev  *
81b843c749SSergey Zigachev  * @handle: acpi handle
82b843c749SSergey Zigachev  * @function: the ATIF function to execute
83b843c749SSergey Zigachev  * @params: ATIF function params
84b843c749SSergey Zigachev  *
85b843c749SSergey Zigachev  * Executes the requested ATIF function (all asics).
86b843c749SSergey Zigachev  * Returns a pointer to the acpi output buffer.
87b843c749SSergey Zigachev  */
amdgpu_atif_call(struct amdgpu_atif * atif,int function,ACPI_BUFFER * params)88*78973132SSergey Zigachev static ACPI_OBJECT *amdgpu_atif_call(struct amdgpu_atif *atif,
89b843c749SSergey Zigachev 					   int function,
90*78973132SSergey Zigachev 					   ACPI_BUFFER *params)
91b843c749SSergey Zigachev {
92*78973132SSergey Zigachev 	ACPI_STATUS status;
93*78973132SSergey Zigachev 	ACPI_OBJECT atif_arg_elements[2];
94*78973132SSergey Zigachev 	ACPI_OBJECT_LIST atif_arg;
95*78973132SSergey Zigachev 	ACPI_BUFFER buffer = { ACPI_ALLOCATE_BUFFER, NULL };
96b843c749SSergey Zigachev 
97*78973132SSergey Zigachev 	atif_arg.Count = 2;
98*78973132SSergey Zigachev 	atif_arg.Pointer = &atif_arg_elements[0];
99b843c749SSergey Zigachev 
100*78973132SSergey Zigachev 	atif_arg_elements[0].Type = ACPI_TYPE_INTEGER;
101*78973132SSergey Zigachev 	atif_arg_elements[0].Integer.Value = function;
102b843c749SSergey Zigachev 
103b843c749SSergey Zigachev 	if (params) {
104*78973132SSergey Zigachev 		atif_arg_elements[1].Type = ACPI_TYPE_BUFFER;
105*78973132SSergey Zigachev 		atif_arg_elements[1].Buffer.Length = params->Length;
106*78973132SSergey Zigachev 		atif_arg_elements[1].Buffer.Pointer = params->Pointer;
107b843c749SSergey Zigachev 	} else {
108b843c749SSergey Zigachev 		/* We need a second fake parameter */
109*78973132SSergey Zigachev 		atif_arg_elements[1].Type = ACPI_TYPE_INTEGER;
110*78973132SSergey Zigachev 		atif_arg_elements[1].Integer.Value = 0;
111b843c749SSergey Zigachev 	}
112b843c749SSergey Zigachev 
113*78973132SSergey Zigachev 	status = AcpiEvaluateObject(atif->handle, NULL, &atif_arg,
114b843c749SSergey Zigachev 				      &buffer);
115b843c749SSergey Zigachev 
116b843c749SSergey Zigachev 	/* Fail only if calling the method fails and ATIF is supported */
117b843c749SSergey Zigachev 	if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
118b843c749SSergey Zigachev 		DRM_DEBUG_DRIVER("failed to evaluate ATIF got %s\n",
119*78973132SSergey Zigachev 				 AcpiFormatException(status));
120*78973132SSergey Zigachev 		AcpiOsFree(buffer.Pointer);
121b843c749SSergey Zigachev 		return NULL;
122b843c749SSergey Zigachev 	}
123b843c749SSergey Zigachev 
124*78973132SSergey Zigachev 	return buffer.Pointer;
125b843c749SSergey Zigachev }
126b843c749SSergey Zigachev 
127b843c749SSergey Zigachev /**
128b843c749SSergey Zigachev  * amdgpu_atif_parse_notification - parse supported notifications
129b843c749SSergey Zigachev  *
130b843c749SSergey Zigachev  * @n: supported notifications struct
131b843c749SSergey Zigachev  * @mask: supported notifications mask from ATIF
132b843c749SSergey Zigachev  *
133b843c749SSergey Zigachev  * Use the supported notifications mask from ATIF function
134b843c749SSergey Zigachev  * ATIF_FUNCTION_VERIFY_INTERFACE to determine what notifications
135b843c749SSergey Zigachev  * are supported (all asics).
136b843c749SSergey Zigachev  */
amdgpu_atif_parse_notification(struct amdgpu_atif_notifications * n,u32 mask)137b843c749SSergey Zigachev static void amdgpu_atif_parse_notification(struct amdgpu_atif_notifications *n, u32 mask)
138b843c749SSergey Zigachev {
139b843c749SSergey Zigachev 	n->display_switch = mask & ATIF_DISPLAY_SWITCH_REQUEST_SUPPORTED;
140b843c749SSergey Zigachev 	n->expansion_mode_change = mask & ATIF_EXPANSION_MODE_CHANGE_REQUEST_SUPPORTED;
141b843c749SSergey Zigachev 	n->thermal_state = mask & ATIF_THERMAL_STATE_CHANGE_REQUEST_SUPPORTED;
142b843c749SSergey Zigachev 	n->forced_power_state = mask & ATIF_FORCED_POWER_STATE_CHANGE_REQUEST_SUPPORTED;
143b843c749SSergey Zigachev 	n->system_power_state = mask & ATIF_SYSTEM_POWER_SOURCE_CHANGE_REQUEST_SUPPORTED;
144b843c749SSergey Zigachev 	n->display_conf_change = mask & ATIF_DISPLAY_CONF_CHANGE_REQUEST_SUPPORTED;
145b843c749SSergey Zigachev 	n->px_gfx_switch = mask & ATIF_PX_GFX_SWITCH_REQUEST_SUPPORTED;
146b843c749SSergey Zigachev 	n->brightness_change = mask & ATIF_PANEL_BRIGHTNESS_CHANGE_REQUEST_SUPPORTED;
147b843c749SSergey Zigachev 	n->dgpu_display_event = mask & ATIF_DGPU_DISPLAY_EVENT_SUPPORTED;
148b843c749SSergey Zigachev }
149b843c749SSergey Zigachev 
150b843c749SSergey Zigachev /**
151b843c749SSergey Zigachev  * amdgpu_atif_parse_functions - parse supported functions
152b843c749SSergey Zigachev  *
153b843c749SSergey Zigachev  * @f: supported functions struct
154b843c749SSergey Zigachev  * @mask: supported functions mask from ATIF
155b843c749SSergey Zigachev  *
156b843c749SSergey Zigachev  * Use the supported functions mask from ATIF function
157b843c749SSergey Zigachev  * ATIF_FUNCTION_VERIFY_INTERFACE to determine what functions
158b843c749SSergey Zigachev  * are supported (all asics).
159b843c749SSergey Zigachev  */
amdgpu_atif_parse_functions(struct amdgpu_atif_functions * f,u32 mask)160b843c749SSergey Zigachev static void amdgpu_atif_parse_functions(struct amdgpu_atif_functions *f, u32 mask)
161b843c749SSergey Zigachev {
162b843c749SSergey Zigachev 	f->system_params = mask & ATIF_GET_SYSTEM_PARAMETERS_SUPPORTED;
163b843c749SSergey Zigachev 	f->sbios_requests = mask & ATIF_GET_SYSTEM_BIOS_REQUESTS_SUPPORTED;
164b843c749SSergey Zigachev 	f->select_active_disp = mask & ATIF_SELECT_ACTIVE_DISPLAYS_SUPPORTED;
165b843c749SSergey Zigachev 	f->lid_state = mask & ATIF_GET_LID_STATE_SUPPORTED;
166b843c749SSergey Zigachev 	f->get_tv_standard = mask & ATIF_GET_TV_STANDARD_FROM_CMOS_SUPPORTED;
167b843c749SSergey Zigachev 	f->set_tv_standard = mask & ATIF_SET_TV_STANDARD_IN_CMOS_SUPPORTED;
168b843c749SSergey Zigachev 	f->get_panel_expansion_mode = mask & ATIF_GET_PANEL_EXPANSION_MODE_FROM_CMOS_SUPPORTED;
169b843c749SSergey Zigachev 	f->set_panel_expansion_mode = mask & ATIF_SET_PANEL_EXPANSION_MODE_IN_CMOS_SUPPORTED;
170b843c749SSergey Zigachev 	f->temperature_change = mask & ATIF_TEMPERATURE_CHANGE_NOTIFICATION_SUPPORTED;
171b843c749SSergey Zigachev 	f->graphics_device_types = mask & ATIF_GET_GRAPHICS_DEVICE_TYPES_SUPPORTED;
172b843c749SSergey Zigachev }
173b843c749SSergey Zigachev 
174b843c749SSergey Zigachev /**
175b843c749SSergey Zigachev  * amdgpu_atif_verify_interface - verify ATIF
176b843c749SSergey Zigachev  *
177b843c749SSergey Zigachev  * @handle: acpi handle
178b843c749SSergey Zigachev  * @atif: amdgpu atif struct
179b843c749SSergey Zigachev  *
180b843c749SSergey Zigachev  * Execute the ATIF_FUNCTION_VERIFY_INTERFACE ATIF function
181b843c749SSergey Zigachev  * to initialize ATIF and determine what features are supported
182b843c749SSergey Zigachev  * (all asics).
183b843c749SSergey Zigachev  * returns 0 on success, error on failure.
184b843c749SSergey Zigachev  */
amdgpu_atif_verify_interface(struct amdgpu_atif * atif)185b843c749SSergey Zigachev static int amdgpu_atif_verify_interface(struct amdgpu_atif *atif)
186b843c749SSergey Zigachev {
187*78973132SSergey Zigachev 	ACPI_OBJECT *info;
188b843c749SSergey Zigachev 	struct atif_verify_interface output;
189b843c749SSergey Zigachev 	size_t size;
190b843c749SSergey Zigachev 	int err = 0;
191b843c749SSergey Zigachev 
192b843c749SSergey Zigachev 	info = amdgpu_atif_call(atif, ATIF_FUNCTION_VERIFY_INTERFACE, NULL);
193b843c749SSergey Zigachev 	if (!info)
194b843c749SSergey Zigachev 		return -EIO;
195b843c749SSergey Zigachev 
196b843c749SSergey Zigachev 	memset(&output, 0, sizeof(output));
197b843c749SSergey Zigachev 
198*78973132SSergey Zigachev 	size = *(u16 *) info->Buffer.Pointer;
199b843c749SSergey Zigachev 	if (size < 12) {
200b843c749SSergey Zigachev 		DRM_INFO("ATIF buffer is too small: %zu\n", size);
201b843c749SSergey Zigachev 		err = -EINVAL;
202b843c749SSergey Zigachev 		goto out;
203b843c749SSergey Zigachev 	}
204b843c749SSergey Zigachev 	size = min(sizeof(output), size);
205b843c749SSergey Zigachev 
206*78973132SSergey Zigachev 	memcpy(&output, info->Buffer.Pointer, size);
207b843c749SSergey Zigachev 
208b843c749SSergey Zigachev 	/* TODO: check version? */
209b843c749SSergey Zigachev 	DRM_DEBUG_DRIVER("ATIF version %u\n", output.version);
210b843c749SSergey Zigachev 
211b843c749SSergey Zigachev 	amdgpu_atif_parse_notification(&atif->notifications, output.notification_mask);
212b843c749SSergey Zigachev 	amdgpu_atif_parse_functions(&atif->functions, output.function_bits);
213b843c749SSergey Zigachev 
214b843c749SSergey Zigachev out:
215*78973132SSergey Zigachev 	AcpiOsFree(info);
216b843c749SSergey Zigachev 	return err;
217b843c749SSergey Zigachev }
218b843c749SSergey Zigachev 
amdgpu_atif_probe_handle(ACPI_HANDLE dhandle)219*78973132SSergey Zigachev static ACPI_HANDLE amdgpu_atif_probe_handle(ACPI_HANDLE dhandle)
220b843c749SSergey Zigachev {
221*78973132SSergey Zigachev 	ACPI_HANDLE handle = NULL;
222b843c749SSergey Zigachev 	char acpi_method_name[255] = { 0 };
223*78973132SSergey Zigachev 	ACPI_BUFFER buffer = { sizeof(acpi_method_name), acpi_method_name };
224*78973132SSergey Zigachev 	ACPI_STATUS status;
225b843c749SSergey Zigachev 
226b843c749SSergey Zigachev 	/* For PX/HG systems, ATIF and ATPX are in the iGPU's namespace, on dGPU only
227b843c749SSergey Zigachev 	 * systems, ATIF is in the dGPU's namespace.
228b843c749SSergey Zigachev 	 */
229*78973132SSergey Zigachev 	status = AcpiGetHandle(dhandle, "ATIF", &handle);
230b843c749SSergey Zigachev 	if (ACPI_SUCCESS(status))
231b843c749SSergey Zigachev 		goto out;
232b843c749SSergey Zigachev 
233b843c749SSergey Zigachev 	if (amdgpu_has_atpx()) {
234*78973132SSergey Zigachev 		status = AcpiGetHandle(amdgpu_atpx_get_dhandle(), "ATIF",
235b843c749SSergey Zigachev 					 &handle);
236b843c749SSergey Zigachev 		if (ACPI_SUCCESS(status))
237b843c749SSergey Zigachev 			goto out;
238b843c749SSergey Zigachev 	}
239b843c749SSergey Zigachev 
240b843c749SSergey Zigachev 	DRM_DEBUG_DRIVER("No ATIF handle found\n");
241b843c749SSergey Zigachev 	return NULL;
242b843c749SSergey Zigachev out:
243*78973132SSergey Zigachev 	AcpiGetName(handle, ACPI_FULL_PATHNAME, &buffer);
244b843c749SSergey Zigachev 	DRM_DEBUG_DRIVER("Found ATIF handle %s\n", acpi_method_name);
245b843c749SSergey Zigachev 	return handle;
246b843c749SSergey Zigachev }
247b843c749SSergey Zigachev 
248b843c749SSergey Zigachev /**
249b843c749SSergey Zigachev  * amdgpu_atif_get_notification_params - determine notify configuration
250b843c749SSergey Zigachev  *
251b843c749SSergey Zigachev  * @handle: acpi handle
252b843c749SSergey Zigachev  * @n: atif notification configuration struct
253b843c749SSergey Zigachev  *
254b843c749SSergey Zigachev  * Execute the ATIF_FUNCTION_GET_SYSTEM_PARAMETERS ATIF function
255b843c749SSergey Zigachev  * to determine if a notifier is used and if so which one
256b843c749SSergey Zigachev  * (all asics).  This is either Notify(VGA, 0x81) or Notify(VGA, n)
257b843c749SSergey Zigachev  * where n is specified in the result if a notifier is used.
258b843c749SSergey Zigachev  * Returns 0 on success, error on failure.
259b843c749SSergey Zigachev  */
amdgpu_atif_get_notification_params(struct amdgpu_atif * atif)260b843c749SSergey Zigachev static int amdgpu_atif_get_notification_params(struct amdgpu_atif *atif)
261b843c749SSergey Zigachev {
262*78973132SSergey Zigachev 	ACPI_OBJECT *info;
263b843c749SSergey Zigachev 	struct amdgpu_atif_notification_cfg *n = &atif->notification_cfg;
264b843c749SSergey Zigachev 	struct atif_system_params params;
265b843c749SSergey Zigachev 	size_t size;
266b843c749SSergey Zigachev 	int err = 0;
267b843c749SSergey Zigachev 
268b843c749SSergey Zigachev 	info = amdgpu_atif_call(atif, ATIF_FUNCTION_GET_SYSTEM_PARAMETERS,
269b843c749SSergey Zigachev 				NULL);
270b843c749SSergey Zigachev 	if (!info) {
271b843c749SSergey Zigachev 		err = -EIO;
272b843c749SSergey Zigachev 		goto out;
273b843c749SSergey Zigachev 	}
274b843c749SSergey Zigachev 
275*78973132SSergey Zigachev 	size = *(u16 *) info->Buffer.Pointer;
276b843c749SSergey Zigachev 	if (size < 10) {
277b843c749SSergey Zigachev 		err = -EINVAL;
278b843c749SSergey Zigachev 		goto out;
279b843c749SSergey Zigachev 	}
280b843c749SSergey Zigachev 
281b843c749SSergey Zigachev 	memset(&params, 0, sizeof(params));
282b843c749SSergey Zigachev 	size = min(sizeof(params), size);
283*78973132SSergey Zigachev 	memcpy(&params, info->Buffer.Pointer, size);
284b843c749SSergey Zigachev 
285b843c749SSergey Zigachev 	DRM_DEBUG_DRIVER("SYSTEM_PARAMS: mask = %#x, flags = %#x\n",
286b843c749SSergey Zigachev 			params.flags, params.valid_mask);
287b843c749SSergey Zigachev 	params.flags = params.flags & params.valid_mask;
288b843c749SSergey Zigachev 
289b843c749SSergey Zigachev 	if ((params.flags & ATIF_NOTIFY_MASK) == ATIF_NOTIFY_NONE) {
290b843c749SSergey Zigachev 		n->enabled = false;
291b843c749SSergey Zigachev 		n->command_code = 0;
292b843c749SSergey Zigachev 	} else if ((params.flags & ATIF_NOTIFY_MASK) == ATIF_NOTIFY_81) {
293b843c749SSergey Zigachev 		n->enabled = true;
294b843c749SSergey Zigachev 		n->command_code = 0x81;
295b843c749SSergey Zigachev 	} else {
296b843c749SSergey Zigachev 		if (size < 11) {
297b843c749SSergey Zigachev 			err = -EINVAL;
298b843c749SSergey Zigachev 			goto out;
299b843c749SSergey Zigachev 		}
300b843c749SSergey Zigachev 		n->enabled = true;
301b843c749SSergey Zigachev 		n->command_code = params.command_code;
302b843c749SSergey Zigachev 	}
303b843c749SSergey Zigachev 
304b843c749SSergey Zigachev out:
305b843c749SSergey Zigachev 	DRM_DEBUG_DRIVER("Notification %s, command code = %#x\n",
306b843c749SSergey Zigachev 			(n->enabled ? "enabled" : "disabled"),
307b843c749SSergey Zigachev 			n->command_code);
308*78973132SSergey Zigachev 	AcpiOsFree(info);
309b843c749SSergey Zigachev 	return err;
310b843c749SSergey Zigachev }
311b843c749SSergey Zigachev 
312b843c749SSergey Zigachev /**
313b843c749SSergey Zigachev  * amdgpu_atif_get_sbios_requests - get requested sbios event
314b843c749SSergey Zigachev  *
315b843c749SSergey Zigachev  * @handle: acpi handle
316b843c749SSergey Zigachev  * @req: atif sbios request struct
317b843c749SSergey Zigachev  *
318b843c749SSergey Zigachev  * Execute the ATIF_FUNCTION_GET_SYSTEM_BIOS_REQUESTS ATIF function
319b843c749SSergey Zigachev  * to determine what requests the sbios is making to the driver
320b843c749SSergey Zigachev  * (all asics).
321b843c749SSergey Zigachev  * Returns 0 on success, error on failure.
322b843c749SSergey Zigachev  */
amdgpu_atif_get_sbios_requests(struct amdgpu_atif * atif,struct atif_sbios_requests * req)323b843c749SSergey Zigachev static int amdgpu_atif_get_sbios_requests(struct amdgpu_atif *atif,
324b843c749SSergey Zigachev 					  struct atif_sbios_requests *req)
325b843c749SSergey Zigachev {
326*78973132SSergey Zigachev 	ACPI_OBJECT *info;
327b843c749SSergey Zigachev 	size_t size;
328b843c749SSergey Zigachev 	int count = 0;
329b843c749SSergey Zigachev 
330b843c749SSergey Zigachev 	info = amdgpu_atif_call(atif, ATIF_FUNCTION_GET_SYSTEM_BIOS_REQUESTS,
331b843c749SSergey Zigachev 				NULL);
332b843c749SSergey Zigachev 	if (!info)
333b843c749SSergey Zigachev 		return -EIO;
334b843c749SSergey Zigachev 
335*78973132SSergey Zigachev 	size = *(u16 *)info->Buffer.Pointer;
336b843c749SSergey Zigachev 	if (size < 0xd) {
337b843c749SSergey Zigachev 		count = -EINVAL;
338b843c749SSergey Zigachev 		goto out;
339b843c749SSergey Zigachev 	}
340b843c749SSergey Zigachev 	memset(req, 0, sizeof(*req));
341b843c749SSergey Zigachev 
342b843c749SSergey Zigachev 	size = min(sizeof(*req), size);
343*78973132SSergey Zigachev 	memcpy(req, info->Buffer.Pointer, size);
344b843c749SSergey Zigachev 	DRM_DEBUG_DRIVER("SBIOS pending requests: %#x\n", req->pending);
345b843c749SSergey Zigachev 
346b843c749SSergey Zigachev 	count = hweight32(req->pending);
347b843c749SSergey Zigachev 
348b843c749SSergey Zigachev out:
349*78973132SSergey Zigachev 	AcpiOsFree(info);
350b843c749SSergey Zigachev 	return count;
351b843c749SSergey Zigachev }
352b843c749SSergey Zigachev 
353b843c749SSergey Zigachev /**
354b843c749SSergey Zigachev  * amdgpu_atif_handler - handle ATIF notify requests
355b843c749SSergey Zigachev  *
356b843c749SSergey Zigachev  * @adev: amdgpu_device pointer
357b843c749SSergey Zigachev  * @event: atif sbios request struct
358b843c749SSergey Zigachev  *
359b843c749SSergey Zigachev  * Checks the acpi event and if it matches an atif event,
360b843c749SSergey Zigachev  * handles it.
361b843c749SSergey Zigachev  *
362b843c749SSergey Zigachev  * Returns:
363b843c749SSergey Zigachev  * NOTIFY_BAD or NOTIFY_DONE, depending on the event.
364b843c749SSergey Zigachev  */
amdgpu_atif_handler(struct amdgpu_device * adev,UINT32 type)365*78973132SSergey Zigachev static void amdgpu_atif_handler(struct amdgpu_device *adev,
366*78973132SSergey Zigachev 			       UINT32 type)
367b843c749SSergey Zigachev {
368b843c749SSergey Zigachev 	struct amdgpu_atif *atif = adev->atif;
369b843c749SSergey Zigachev 	int count;
370b843c749SSergey Zigachev 
371*78973132SSergey Zigachev 	DRM_DEBUG_DRIVER("event, type = %#x\n", type);
372b843c749SSergey Zigachev 
373b843c749SSergey Zigachev 	/* Is this actually our event? */
374b843c749SSergey Zigachev 	if (!atif ||
375b843c749SSergey Zigachev 	    !atif->notification_cfg.enabled ||
376*78973132SSergey Zigachev 	    type != atif->notification_cfg.command_code) {
377b843c749SSergey Zigachev 		/* These events will generate keypresses otherwise */
378*78973132SSergey Zigachev #if 0
379b843c749SSergey Zigachev                if (event->type == ACPI_VIDEO_NOTIFY_PROBE)
380b843c749SSergey Zigachev                        return NOTIFY_BAD;
381b843c749SSergey Zigachev 		else
382b843c749SSergey Zigachev 			return NOTIFY_DONE;
383*78973132SSergey Zigachev #endif
384b843c749SSergey Zigachev 	}
385b843c749SSergey Zigachev 
386b843c749SSergey Zigachev 	if (atif->functions.sbios_requests) {
387b843c749SSergey Zigachev 		struct atif_sbios_requests req;
388b843c749SSergey Zigachev 
389b843c749SSergey Zigachev 		/* Check pending SBIOS requests */
390b843c749SSergey Zigachev 		count = amdgpu_atif_get_sbios_requests(atif, &req);
391b843c749SSergey Zigachev 
392b843c749SSergey Zigachev 		if (count <= 0)
393*78973132SSergey Zigachev 			return;
394b843c749SSergey Zigachev 
395b843c749SSergey Zigachev 		DRM_DEBUG_DRIVER("ATIF: %d pending SBIOS requests\n", count);
396b843c749SSergey Zigachev 
397b843c749SSergey Zigachev 		/* todo: add DC handling */
398b843c749SSergey Zigachev 		if ((req.pending & ATIF_PANEL_BRIGHTNESS_CHANGE_REQUEST) &&
399b843c749SSergey Zigachev 		    !amdgpu_device_has_dc_support(adev)) {
400b843c749SSergey Zigachev 			struct amdgpu_encoder *enc = atif->encoder_for_bl;
401b843c749SSergey Zigachev 
402b843c749SSergey Zigachev 			if (enc) {
403*78973132SSergey Zigachev #if 0
404b843c749SSergey Zigachev 				struct amdgpu_encoder_atom_dig *dig = enc->enc_priv;
405*78973132SSergey Zigachev #endif
406b843c749SSergey Zigachev 
407b843c749SSergey Zigachev 				DRM_DEBUG_DRIVER("Changing brightness to %d\n",
408b843c749SSergey Zigachev 						 req.backlight_level);
409b843c749SSergey Zigachev 
410b843c749SSergey Zigachev 				amdgpu_display_backlight_set_level(adev, enc, req.backlight_level);
411*78973132SSergey Zigachev #if 0
412b843c749SSergey Zigachev #if defined(CONFIG_BACKLIGHT_CLASS_DEVICE) || defined(CONFIG_BACKLIGHT_CLASS_DEVICE_MODULE)
413b843c749SSergey Zigachev 				backlight_force_update(dig->bl_dev,
414b843c749SSergey Zigachev 						       BACKLIGHT_UPDATE_HOTKEY);
415b843c749SSergey Zigachev #endif
416*78973132SSergey Zigachev #endif
417b843c749SSergey Zigachev 			}
418b843c749SSergey Zigachev 		}
419b843c749SSergey Zigachev 		if (req.pending & ATIF_DGPU_DISPLAY_EVENT) {
420b843c749SSergey Zigachev 			if (adev->flags & AMD_IS_PX) {
421b843c749SSergey Zigachev 				pm_runtime_get_sync(adev->ddev->dev);
422b843c749SSergey Zigachev 				/* Just fire off a uevent and let userspace tell us what to do */
423b843c749SSergey Zigachev 				drm_helper_hpd_irq_event(adev->ddev);
424b843c749SSergey Zigachev 				pm_runtime_mark_last_busy(adev->ddev->dev);
425b843c749SSergey Zigachev 				pm_runtime_put_autosuspend(adev->ddev->dev);
426b843c749SSergey Zigachev 			}
427b843c749SSergey Zigachev 		}
428b843c749SSergey Zigachev 		/* TODO: check other events */
429b843c749SSergey Zigachev 	}
430b843c749SSergey Zigachev 
431b843c749SSergey Zigachev 	/* We've handled the event, stop the notifier chain. The ACPI interface
432b843c749SSergey Zigachev 	 * overloads ACPI_VIDEO_NOTIFY_PROBE, we don't want to send that to
433b843c749SSergey Zigachev 	 * userspace if the event was generated only to signal a SBIOS
434b843c749SSergey Zigachev 	 * request.
435b843c749SSergey Zigachev 	 */
436*78973132SSergey Zigachev 	return;
437b843c749SSergey Zigachev }
438b843c749SSergey Zigachev 
439b843c749SSergey Zigachev /* Call the ATCS method
440b843c749SSergey Zigachev  */
441b843c749SSergey Zigachev /**
442b843c749SSergey Zigachev  * amdgpu_atcs_call - call an ATCS method
443b843c749SSergey Zigachev  *
444b843c749SSergey Zigachev  * @handle: acpi handle
445b843c749SSergey Zigachev  * @function: the ATCS function to execute
446b843c749SSergey Zigachev  * @params: ATCS function params
447b843c749SSergey Zigachev  *
448b843c749SSergey Zigachev  * Executes the requested ATCS function (all asics).
449b843c749SSergey Zigachev  * Returns a pointer to the acpi output buffer.
450b843c749SSergey Zigachev  */
amdgpu_atcs_call(ACPI_HANDLE handle,int function,ACPI_BUFFER * params)451*78973132SSergey Zigachev static ACPI_OBJECT *amdgpu_atcs_call(ACPI_HANDLE handle, int function,
452*78973132SSergey Zigachev 					   ACPI_BUFFER *params)
453b843c749SSergey Zigachev {
454*78973132SSergey Zigachev 	ACPI_STATUS status;
455*78973132SSergey Zigachev 	ACPI_OBJECT atcs_arg_elements[2];
456*78973132SSergey Zigachev 	ACPI_OBJECT_LIST atcs_arg;
457*78973132SSergey Zigachev 	ACPI_BUFFER buffer = { ACPI_ALLOCATE_BUFFER, NULL };
458b843c749SSergey Zigachev 
459*78973132SSergey Zigachev 	atcs_arg.Count = 2;
460*78973132SSergey Zigachev 	atcs_arg.Pointer = &atcs_arg_elements[0];
461b843c749SSergey Zigachev 
462*78973132SSergey Zigachev 	atcs_arg_elements[0].Type = ACPI_TYPE_INTEGER;
463*78973132SSergey Zigachev 	atcs_arg_elements[0].Integer.Value = function;
464b843c749SSergey Zigachev 
465b843c749SSergey Zigachev 	if (params) {
466*78973132SSergey Zigachev 		atcs_arg_elements[1].Type = ACPI_TYPE_BUFFER;
467*78973132SSergey Zigachev 		atcs_arg_elements[1].Buffer.Length = params->Length;
468*78973132SSergey Zigachev 		atcs_arg_elements[1].Buffer.Pointer = params->Pointer;
469b843c749SSergey Zigachev 	} else {
470b843c749SSergey Zigachev 		/* We need a second fake parameter */
471*78973132SSergey Zigachev 		atcs_arg_elements[1].Type = ACPI_TYPE_INTEGER;
472*78973132SSergey Zigachev 		atcs_arg_elements[1].Integer.Value = 0;
473b843c749SSergey Zigachev 	}
474b843c749SSergey Zigachev 
475*78973132SSergey Zigachev 	status = AcpiEvaluateObject(handle, "ATCS", &atcs_arg, &buffer);
476b843c749SSergey Zigachev 
477b843c749SSergey Zigachev 	/* Fail only if calling the method fails and ATIF is supported */
478b843c749SSergey Zigachev 	if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
479b843c749SSergey Zigachev 		DRM_DEBUG_DRIVER("failed to evaluate ATCS got %s\n",
480*78973132SSergey Zigachev 				 AcpiFormatException(status));
481*78973132SSergey Zigachev 		AcpiOsFree(buffer.Pointer);
482b843c749SSergey Zigachev 		return NULL;
483b843c749SSergey Zigachev 	}
484b843c749SSergey Zigachev 
485*78973132SSergey Zigachev 	return buffer.Pointer;
486b843c749SSergey Zigachev }
487b843c749SSergey Zigachev 
488b843c749SSergey Zigachev /**
489b843c749SSergey Zigachev  * amdgpu_atcs_parse_functions - parse supported functions
490b843c749SSergey Zigachev  *
491b843c749SSergey Zigachev  * @f: supported functions struct
492b843c749SSergey Zigachev  * @mask: supported functions mask from ATCS
493b843c749SSergey Zigachev  *
494b843c749SSergey Zigachev  * Use the supported functions mask from ATCS function
495b843c749SSergey Zigachev  * ATCS_FUNCTION_VERIFY_INTERFACE to determine what functions
496b843c749SSergey Zigachev  * are supported (all asics).
497b843c749SSergey Zigachev  */
amdgpu_atcs_parse_functions(struct amdgpu_atcs_functions * f,u32 mask)498b843c749SSergey Zigachev static void amdgpu_atcs_parse_functions(struct amdgpu_atcs_functions *f, u32 mask)
499b843c749SSergey Zigachev {
500b843c749SSergey Zigachev 	f->get_ext_state = mask & ATCS_GET_EXTERNAL_STATE_SUPPORTED;
501b843c749SSergey Zigachev 	f->pcie_perf_req = mask & ATCS_PCIE_PERFORMANCE_REQUEST_SUPPORTED;
502b843c749SSergey Zigachev 	f->pcie_dev_rdy = mask & ATCS_PCIE_DEVICE_READY_NOTIFICATION_SUPPORTED;
503b843c749SSergey Zigachev 	f->pcie_bus_width = mask & ATCS_SET_PCIE_BUS_WIDTH_SUPPORTED;
504b843c749SSergey Zigachev }
505b843c749SSergey Zigachev 
506b843c749SSergey Zigachev /**
507b843c749SSergey Zigachev  * amdgpu_atcs_verify_interface - verify ATCS
508b843c749SSergey Zigachev  *
509b843c749SSergey Zigachev  * @handle: acpi handle
510b843c749SSergey Zigachev  * @atcs: amdgpu atcs struct
511b843c749SSergey Zigachev  *
512b843c749SSergey Zigachev  * Execute the ATCS_FUNCTION_VERIFY_INTERFACE ATCS function
513b843c749SSergey Zigachev  * to initialize ATCS and determine what features are supported
514b843c749SSergey Zigachev  * (all asics).
515b843c749SSergey Zigachev  * returns 0 on success, error on failure.
516b843c749SSergey Zigachev  */
amdgpu_atcs_verify_interface(ACPI_HANDLE handle,struct amdgpu_atcs * atcs)517*78973132SSergey Zigachev static int amdgpu_atcs_verify_interface(ACPI_HANDLE handle,
518b843c749SSergey Zigachev 					struct amdgpu_atcs *atcs)
519b843c749SSergey Zigachev {
520*78973132SSergey Zigachev 	ACPI_OBJECT *info;
521b843c749SSergey Zigachev 	struct atcs_verify_interface output;
522b843c749SSergey Zigachev 	size_t size;
523b843c749SSergey Zigachev 	int err = 0;
524b843c749SSergey Zigachev 
525b843c749SSergey Zigachev 	info = amdgpu_atcs_call(handle, ATCS_FUNCTION_VERIFY_INTERFACE, NULL);
526b843c749SSergey Zigachev 	if (!info)
527b843c749SSergey Zigachev 		return -EIO;
528b843c749SSergey Zigachev 
529b843c749SSergey Zigachev 	memset(&output, 0, sizeof(output));
530b843c749SSergey Zigachev 
531*78973132SSergey Zigachev 	size = *(u16 *) info->Buffer.Pointer;
532b843c749SSergey Zigachev 	if (size < 8) {
533b843c749SSergey Zigachev 		DRM_INFO("ATCS buffer is too small: %zu\n", size);
534b843c749SSergey Zigachev 		err = -EINVAL;
535b843c749SSergey Zigachev 		goto out;
536b843c749SSergey Zigachev 	}
537b843c749SSergey Zigachev 	size = min(sizeof(output), size);
538b843c749SSergey Zigachev 
539*78973132SSergey Zigachev 	memcpy(&output, info->Buffer.Pointer, size);
540b843c749SSergey Zigachev 
541b843c749SSergey Zigachev 	/* TODO: check version? */
542b843c749SSergey Zigachev 	DRM_DEBUG_DRIVER("ATCS version %u\n", output.version);
543b843c749SSergey Zigachev 
544b843c749SSergey Zigachev 	amdgpu_atcs_parse_functions(&atcs->functions, output.function_bits);
545b843c749SSergey Zigachev 
546b843c749SSergey Zigachev out:
547*78973132SSergey Zigachev 	AcpiOsFree(info);
548b843c749SSergey Zigachev 	return err;
549b843c749SSergey Zigachev }
550b843c749SSergey Zigachev 
551b843c749SSergey Zigachev /**
552b843c749SSergey Zigachev  * amdgpu_acpi_is_pcie_performance_request_supported
553b843c749SSergey Zigachev  *
554b843c749SSergey Zigachev  * @adev: amdgpu_device pointer
555b843c749SSergey Zigachev  *
556b843c749SSergey Zigachev  * Check if the ATCS pcie_perf_req and pcie_dev_rdy methods
557b843c749SSergey Zigachev  * are supported (all asics).
558b843c749SSergey Zigachev  * returns true if supported, false if not.
559b843c749SSergey Zigachev  */
amdgpu_acpi_is_pcie_performance_request_supported(struct amdgpu_device * adev)560b843c749SSergey Zigachev bool amdgpu_acpi_is_pcie_performance_request_supported(struct amdgpu_device *adev)
561b843c749SSergey Zigachev {
562b843c749SSergey Zigachev 	struct amdgpu_atcs *atcs = &adev->atcs;
563b843c749SSergey Zigachev 
564b843c749SSergey Zigachev 	if (atcs->functions.pcie_perf_req && atcs->functions.pcie_dev_rdy)
565b843c749SSergey Zigachev 		return true;
566b843c749SSergey Zigachev 
567b843c749SSergey Zigachev 	return false;
568b843c749SSergey Zigachev }
569b843c749SSergey Zigachev 
570b843c749SSergey Zigachev /**
571b843c749SSergey Zigachev  * amdgpu_acpi_pcie_notify_device_ready
572b843c749SSergey Zigachev  *
573b843c749SSergey Zigachev  * @adev: amdgpu_device pointer
574b843c749SSergey Zigachev  *
575b843c749SSergey Zigachev  * Executes the PCIE_DEVICE_READY_NOTIFICATION method
576b843c749SSergey Zigachev  * (all asics).
577b843c749SSergey Zigachev  * returns 0 on success, error on failure.
578b843c749SSergey Zigachev  */
amdgpu_acpi_pcie_notify_device_ready(struct amdgpu_device * adev)579b843c749SSergey Zigachev int amdgpu_acpi_pcie_notify_device_ready(struct amdgpu_device *adev)
580b843c749SSergey Zigachev {
581*78973132SSergey Zigachev 	ACPI_HANDLE handle;
582*78973132SSergey Zigachev 	ACPI_OBJECT *info;
583b843c749SSergey Zigachev 	struct amdgpu_atcs *atcs = &adev->atcs;
584b843c749SSergey Zigachev 
585b843c749SSergey Zigachev 	/* Get the device handle */
586*78973132SSergey Zigachev 	handle = acpi_get_handle(adev->dev->bsddev);
587b843c749SSergey Zigachev 	if (!handle)
588b843c749SSergey Zigachev 		return -EINVAL;
589b843c749SSergey Zigachev 
590b843c749SSergey Zigachev 	if (!atcs->functions.pcie_dev_rdy)
591b843c749SSergey Zigachev 		return -EINVAL;
592b843c749SSergey Zigachev 
593b843c749SSergey Zigachev 	info = amdgpu_atcs_call(handle, ATCS_FUNCTION_PCIE_DEVICE_READY_NOTIFICATION, NULL);
594b843c749SSergey Zigachev 	if (!info)
595b843c749SSergey Zigachev 		return -EIO;
596b843c749SSergey Zigachev 
597*78973132SSergey Zigachev 	AcpiOsFree(info);
598b843c749SSergey Zigachev 
599b843c749SSergey Zigachev 	return 0;
600b843c749SSergey Zigachev }
601b843c749SSergey Zigachev 
602b843c749SSergey Zigachev /**
603b843c749SSergey Zigachev  * amdgpu_acpi_pcie_performance_request
604b843c749SSergey Zigachev  *
605b843c749SSergey Zigachev  * @adev: amdgpu_device pointer
606b843c749SSergey Zigachev  * @perf_req: requested perf level (pcie gen speed)
607b843c749SSergey Zigachev  * @advertise: set advertise caps flag if set
608b843c749SSergey Zigachev  *
609b843c749SSergey Zigachev  * Executes the PCIE_PERFORMANCE_REQUEST method to
610b843c749SSergey Zigachev  * change the pcie gen speed (all asics).
611b843c749SSergey Zigachev  * returns 0 on success, error on failure.
612b843c749SSergey Zigachev  */
amdgpu_acpi_pcie_performance_request(struct amdgpu_device * adev,u8 perf_req,bool advertise)613b843c749SSergey Zigachev int amdgpu_acpi_pcie_performance_request(struct amdgpu_device *adev,
614b843c749SSergey Zigachev 					 u8 perf_req, bool advertise)
615b843c749SSergey Zigachev {
616*78973132SSergey Zigachev #if 0
617*78973132SSergey Zigachev 	ACPI_HANDLE handle;
618*78973132SSergey Zigachev 	ACPI_OBJECT *info;
619b843c749SSergey Zigachev 	struct amdgpu_atcs *atcs = &adev->atcs;
620b843c749SSergey Zigachev 	struct atcs_pref_req_input atcs_input;
621b843c749SSergey Zigachev 	struct atcs_pref_req_output atcs_output;
622*78973132SSergey Zigachev 	ACPI_BUFFER params;
623b843c749SSergey Zigachev 	size_t size;
624b843c749SSergey Zigachev 	u32 retry = 3;
625b843c749SSergey Zigachev 
626b843c749SSergey Zigachev 	if (amdgpu_acpi_pcie_notify_device_ready(adev))
627b843c749SSergey Zigachev 		return -EINVAL;
628b843c749SSergey Zigachev 
629b843c749SSergey Zigachev 	/* Get the device handle */
630*78973132SSergey Zigachev 	handle = acpi_get_handle(adev->dev->bsddev)
631b843c749SSergey Zigachev 	if (!handle)
632b843c749SSergey Zigachev 		return -EINVAL;
633b843c749SSergey Zigachev 
634b843c749SSergey Zigachev 	if (!atcs->functions.pcie_perf_req)
635b843c749SSergey Zigachev 		return -EINVAL;
636b843c749SSergey Zigachev 
637b843c749SSergey Zigachev 	atcs_input.size = sizeof(struct atcs_pref_req_input);
638b843c749SSergey Zigachev 	/* client id (bit 2-0: func num, 7-3: dev num, 15-8: bus num) */
639b843c749SSergey Zigachev 	atcs_input.client_id = adev->pdev->devfn | (adev->pdev->bus->number << 8);
640b843c749SSergey Zigachev 	atcs_input.valid_flags_mask = ATCS_VALID_FLAGS_MASK;
641b843c749SSergey Zigachev 	atcs_input.flags = ATCS_WAIT_FOR_COMPLETION;
642b843c749SSergey Zigachev 	if (advertise)
643b843c749SSergey Zigachev 		atcs_input.flags |= ATCS_ADVERTISE_CAPS;
644b843c749SSergey Zigachev 	atcs_input.req_type = ATCS_PCIE_LINK_SPEED;
645b843c749SSergey Zigachev 	atcs_input.perf_req = perf_req;
646b843c749SSergey Zigachev 
647*78973132SSergey Zigachev 	params.Length = sizeof(struct atcs_pref_req_input);
648*78973132SSergey Zigachev 	params.Pointer = &atcs_input;
649b843c749SSergey Zigachev 
650b843c749SSergey Zigachev 	while (retry--) {
651b843c749SSergey Zigachev 		info = amdgpu_atcs_call(handle, ATCS_FUNCTION_PCIE_PERFORMANCE_REQUEST, &params);
652b843c749SSergey Zigachev 		if (!info)
653b843c749SSergey Zigachev 			return -EIO;
654b843c749SSergey Zigachev 
655b843c749SSergey Zigachev 		memset(&atcs_output, 0, sizeof(atcs_output));
656b843c749SSergey Zigachev 
657*78973132SSergey Zigachev 		size = *(u16 *) info->Buffer.Pointer;
658b843c749SSergey Zigachev 		if (size < 3) {
659b843c749SSergey Zigachev 			DRM_INFO("ATCS buffer is too small: %zu\n", size);
660*78973132SSergey Zigachev 			AcpiOsFree(info);
661b843c749SSergey Zigachev 			return -EINVAL;
662b843c749SSergey Zigachev 		}
663b843c749SSergey Zigachev 		size = min(sizeof(atcs_output), size);
664b843c749SSergey Zigachev 
665*78973132SSergey Zigachev 		memcpy(&atcs_output, info->Buffer.Pointer, size);
666b843c749SSergey Zigachev 
667*78973132SSergey Zigachev 		AcpiOsFree(info);
668b843c749SSergey Zigachev 
669b843c749SSergey Zigachev 		switch (atcs_output.ret_val) {
670b843c749SSergey Zigachev 		case ATCS_REQUEST_REFUSED:
671b843c749SSergey Zigachev 		default:
672b843c749SSergey Zigachev 			return -EINVAL;
673b843c749SSergey Zigachev 		case ATCS_REQUEST_COMPLETE:
674b843c749SSergey Zigachev 			return 0;
675b843c749SSergey Zigachev 		case ATCS_REQUEST_IN_PROGRESS:
676b843c749SSergey Zigachev 			udelay(10);
677b843c749SSergey Zigachev 			break;
678b843c749SSergey Zigachev 		}
679b843c749SSergey Zigachev 	}
680b843c749SSergey Zigachev 
681b843c749SSergey Zigachev 	return 0;
682*78973132SSergey Zigachev #else
683*78973132SSergey Zigachev 	return -EINVAL;
684*78973132SSergey Zigachev #endif
685b843c749SSergey Zigachev }
686b843c749SSergey Zigachev 
687b843c749SSergey Zigachev /**
688b843c749SSergey Zigachev  * amdgpu_acpi_event - handle notify events
689b843c749SSergey Zigachev  *
690b843c749SSergey Zigachev  * @nb: notifier block
691b843c749SSergey Zigachev  * @val: val
692b843c749SSergey Zigachev  * @data: acpi event
693b843c749SSergey Zigachev  *
694b843c749SSergey Zigachev  * Calls relevant amdgpu functions in response to various
695b843c749SSergey Zigachev  * acpi events.
696b843c749SSergey Zigachev  * Returns NOTIFY code
697b843c749SSergey Zigachev  */
amdgpu_acpi_event(ACPI_HANDLE handle,UINT32 type,void * context)698*78973132SSergey Zigachev static void amdgpu_acpi_event(ACPI_HANDLE handle,
699*78973132SSergey Zigachev 			     UINT32 type,
700*78973132SSergey Zigachev 			     void *context)
701b843c749SSergey Zigachev {
702*78973132SSergey Zigachev 	struct amdgpu_device *adev = (struct amdgpu_device *)context;
703b843c749SSergey Zigachev 
704*78973132SSergey Zigachev #if 0
705b843c749SSergey Zigachev 	if (strcmp(entry->device_class, ACPI_AC_CLASS) == 0) {
706b843c749SSergey Zigachev 		if (power_supply_is_system_supplied() > 0)
707b843c749SSergey Zigachev 			DRM_DEBUG_DRIVER("pm: AC\n");
708b843c749SSergey Zigachev 		else
709b843c749SSergey Zigachev 			DRM_DEBUG_DRIVER("pm: DC\n");
710b843c749SSergey Zigachev 
711b843c749SSergey Zigachev 		amdgpu_pm_acpi_event_handler(adev);
712b843c749SSergey Zigachev 	}
713*78973132SSergey Zigachev #endif
714b843c749SSergey Zigachev 
715b843c749SSergey Zigachev 	/* Check for pending SBIOS requests */
716*78973132SSergey Zigachev 	return amdgpu_atif_handler(adev, type);
717b843c749SSergey Zigachev }
718b843c749SSergey Zigachev 
719b843c749SSergey Zigachev /* Call all ACPI methods here */
720b843c749SSergey Zigachev /**
721b843c749SSergey Zigachev  * amdgpu_acpi_init - init driver acpi support
722b843c749SSergey Zigachev  *
723b843c749SSergey Zigachev  * @adev: amdgpu_device pointer
724b843c749SSergey Zigachev  *
725b843c749SSergey Zigachev  * Verifies the AMD ACPI interfaces and registers with the acpi
726b843c749SSergey Zigachev  * notifier chain (all asics).
727b843c749SSergey Zigachev  * Returns 0 on success, error on failure.
728b843c749SSergey Zigachev  */
amdgpu_acpi_init(struct amdgpu_device * adev)729b843c749SSergey Zigachev int amdgpu_acpi_init(struct amdgpu_device *adev)
730b843c749SSergey Zigachev {
731*78973132SSergey Zigachev 	ACPI_HANDLE handle, atif_handle;
732b843c749SSergey Zigachev 	struct amdgpu_atif *atif;
733b843c749SSergey Zigachev 	struct amdgpu_atcs *atcs = &adev->atcs;
734b843c749SSergey Zigachev 	int ret;
735b843c749SSergey Zigachev 
736b843c749SSergey Zigachev 	/* Get the device handle */
737*78973132SSergey Zigachev 	handle = acpi_get_handle(adev->dev->bsddev);
738b843c749SSergey Zigachev 
739b843c749SSergey Zigachev 	if (!adev->bios || !handle)
740b843c749SSergey Zigachev 		return 0;
741b843c749SSergey Zigachev 
742b843c749SSergey Zigachev 	/* Call the ATCS method */
743b843c749SSergey Zigachev 	ret = amdgpu_atcs_verify_interface(handle, atcs);
744b843c749SSergey Zigachev 	if (ret) {
745b843c749SSergey Zigachev 		DRM_DEBUG_DRIVER("Call to ATCS verify_interface failed: %d\n", ret);
746b843c749SSergey Zigachev 	}
747b843c749SSergey Zigachev 
748b843c749SSergey Zigachev 	/* Probe for ATIF, and initialize it if found */
749b843c749SSergey Zigachev 	atif_handle = amdgpu_atif_probe_handle(handle);
750b843c749SSergey Zigachev 	if (!atif_handle)
751b843c749SSergey Zigachev 		goto out;
752b843c749SSergey Zigachev 
753b843c749SSergey Zigachev 	atif = kzalloc(sizeof(*atif), GFP_KERNEL);
754b843c749SSergey Zigachev 	if (!atif) {
755b843c749SSergey Zigachev 		DRM_WARN("Not enough memory to initialize ATIF\n");
756b843c749SSergey Zigachev 		goto out;
757b843c749SSergey Zigachev 	}
758b843c749SSergey Zigachev 	atif->handle = atif_handle;
759b843c749SSergey Zigachev 
760b843c749SSergey Zigachev 	/* Call the ATIF method */
761b843c749SSergey Zigachev 	ret = amdgpu_atif_verify_interface(atif);
762b843c749SSergey Zigachev 	if (ret) {
763b843c749SSergey Zigachev 		DRM_DEBUG_DRIVER("Call to ATIF verify_interface failed: %d\n", ret);
764*78973132SSergey Zigachev 		AcpiOsFree(atif);
765b843c749SSergey Zigachev 		goto out;
766b843c749SSergey Zigachev 	}
767b843c749SSergey Zigachev 	adev->atif = atif;
768b843c749SSergey Zigachev 
769b843c749SSergey Zigachev 	if (atif->notifications.brightness_change) {
770b843c749SSergey Zigachev 		struct drm_encoder *tmp;
771b843c749SSergey Zigachev 
772b843c749SSergey Zigachev 		/* Find the encoder controlling the brightness */
773b843c749SSergey Zigachev 		list_for_each_entry(tmp, &adev->ddev->mode_config.encoder_list,
774b843c749SSergey Zigachev 				head) {
775b843c749SSergey Zigachev 			struct amdgpu_encoder *enc = to_amdgpu_encoder(tmp);
776b843c749SSergey Zigachev 
777b843c749SSergey Zigachev 			if ((enc->devices & (ATOM_DEVICE_LCD_SUPPORT)) &&
778b843c749SSergey Zigachev 			    enc->enc_priv) {
779b843c749SSergey Zigachev 				struct amdgpu_encoder_atom_dig *dig = enc->enc_priv;
780b843c749SSergey Zigachev 				if (dig->bl_dev) {
781b843c749SSergey Zigachev 					atif->encoder_for_bl = enc;
782b843c749SSergey Zigachev 					break;
783b843c749SSergey Zigachev 				}
784b843c749SSergey Zigachev 			}
785b843c749SSergey Zigachev 		}
786b843c749SSergey Zigachev 	}
787b843c749SSergey Zigachev 
788b843c749SSergey Zigachev 	if (atif->functions.sbios_requests && !atif->functions.system_params) {
789b843c749SSergey Zigachev 		/* XXX check this workraround, if sbios request function is
790b843c749SSergey Zigachev 		 * present we have to see how it's configured in the system
791b843c749SSergey Zigachev 		 * params
792b843c749SSergey Zigachev 		 */
793b843c749SSergey Zigachev 		atif->functions.system_params = true;
794b843c749SSergey Zigachev 	}
795b843c749SSergey Zigachev 
796b843c749SSergey Zigachev 	if (atif->functions.system_params) {
797b843c749SSergey Zigachev 		ret = amdgpu_atif_get_notification_params(atif);
798b843c749SSergey Zigachev 		if (ret) {
799b843c749SSergey Zigachev 			DRM_DEBUG_DRIVER("Call to GET_SYSTEM_PARAMS failed: %d\n",
800b843c749SSergey Zigachev 					ret);
801b843c749SSergey Zigachev 			/* Disable notification */
802b843c749SSergey Zigachev 			atif->notification_cfg.enabled = false;
803b843c749SSergey Zigachev 		}
804b843c749SSergey Zigachev 	}
805b843c749SSergey Zigachev 
806b843c749SSergey Zigachev out:
807*78973132SSergey Zigachev 	adev->acpi.handle = handle;
808*78973132SSergey Zigachev 	adev->acpi.notifier_call = amdgpu_acpi_event;
809*78973132SSergey Zigachev 	AcpiInstallNotifyHandler(handle, ACPI_DEVICE_NOTIFY,
810*78973132SSergey Zigachev 	    adev->acpi.notifier_call, adev);
811b843c749SSergey Zigachev 
812b843c749SSergey Zigachev 	return ret;
813b843c749SSergey Zigachev }
814b843c749SSergey Zigachev 
815b843c749SSergey Zigachev /**
816b843c749SSergey Zigachev  * amdgpu_acpi_fini - tear down driver acpi support
817b843c749SSergey Zigachev  *
818b843c749SSergey Zigachev  * @adev: amdgpu_device pointer
819b843c749SSergey Zigachev  *
820b843c749SSergey Zigachev  * Unregisters with the acpi notifier chain (all asics).
821b843c749SSergey Zigachev  */
amdgpu_acpi_fini(struct amdgpu_device * adev)822b843c749SSergey Zigachev void amdgpu_acpi_fini(struct amdgpu_device *adev)
823b843c749SSergey Zigachev {
824b843c749SSergey Zigachev 	if (adev->atif)
825*78973132SSergey Zigachev 		AcpiOsFree(adev->atif);
826b843c749SSergey Zigachev }
827