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