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