xref: /linux/drivers/gpu/drm/amd/amdgpu/amdgpu_acpi.c (revision 9c27bc97aff8bbe62b5b29ebf528291dd85d9c86)
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>
30fdf2f6c5SSam Ravnborg 
31d38ceaf9SAlex Deucher #include <drm/drm_crtc_helper.h>
32d38ceaf9SAlex Deucher #include "amdgpu.h"
3398c65108SJean Delvare #include "amdgpu_pm.h"
345df58525SHuang Rui #include "amdgpu_display.h"
3566dc0dddSRex Zhu #include "amd_acpi.h"
36d38ceaf9SAlex Deucher #include "atom.h"
37d38ceaf9SAlex Deucher 
38102c16a0SLyude Paul struct amdgpu_atif_notification_cfg {
39102c16a0SLyude Paul 	bool enabled;
40102c16a0SLyude Paul 	int command_code;
41102c16a0SLyude Paul };
42102c16a0SLyude Paul 
43102c16a0SLyude Paul struct amdgpu_atif_notifications {
44102c16a0SLyude Paul 	bool thermal_state;
45102c16a0SLyude Paul 	bool forced_power_state;
46102c16a0SLyude Paul 	bool system_power_state;
47102c16a0SLyude Paul 	bool brightness_change;
48102c16a0SLyude Paul 	bool dgpu_display_event;
497349a3afSDavid Francis 	bool gpu_package_power_limit;
50102c16a0SLyude Paul };
51102c16a0SLyude Paul 
52102c16a0SLyude Paul struct amdgpu_atif_functions {
53102c16a0SLyude Paul 	bool system_params;
54102c16a0SLyude Paul 	bool sbios_requests;
55102c16a0SLyude Paul 	bool temperature_change;
567349a3afSDavid Francis 	bool query_backlight_transfer_characteristics;
577349a3afSDavid Francis 	bool ready_to_undock;
587349a3afSDavid Francis 	bool external_gpu_information;
59102c16a0SLyude Paul };
60102c16a0SLyude Paul 
61102c16a0SLyude Paul struct amdgpu_atif {
62280cf1a9SLyude Paul 	acpi_handle handle;
63280cf1a9SLyude Paul 
64102c16a0SLyude Paul 	struct amdgpu_atif_notifications notifications;
65102c16a0SLyude Paul 	struct amdgpu_atif_functions functions;
66102c16a0SLyude Paul 	struct amdgpu_atif_notification_cfg notification_cfg;
6797d798b2SAlex Deucher #if defined(CONFIG_BACKLIGHT_CLASS_DEVICE) || defined(CONFIG_BACKLIGHT_CLASS_DEVICE_MODULE)
6897d798b2SAlex Deucher 	struct backlight_device *bd;
6997d798b2SAlex Deucher #endif
70206bbafeSDavid Francis 	struct amdgpu_dm_backlight_caps backlight_caps;
71102c16a0SLyude Paul };
72102c16a0SLyude Paul 
73d38ceaf9SAlex Deucher /* Call the ATIF method
74d38ceaf9SAlex Deucher  */
75d38ceaf9SAlex Deucher /**
76d38ceaf9SAlex Deucher  * amdgpu_atif_call - call an ATIF method
77d38ceaf9SAlex Deucher  *
78d38ceaf9SAlex Deucher  * @handle: acpi handle
79d38ceaf9SAlex Deucher  * @function: the ATIF function to execute
80d38ceaf9SAlex Deucher  * @params: ATIF function params
81d38ceaf9SAlex Deucher  *
82d38ceaf9SAlex Deucher  * Executes the requested ATIF function (all asics).
83d38ceaf9SAlex Deucher  * Returns a pointer to the acpi output buffer.
84d38ceaf9SAlex Deucher  */
85280cf1a9SLyude Paul static union acpi_object *amdgpu_atif_call(struct amdgpu_atif *atif,
86280cf1a9SLyude Paul 					   int function,
87d38ceaf9SAlex Deucher 					   struct acpi_buffer *params)
88d38ceaf9SAlex Deucher {
89d38ceaf9SAlex Deucher 	acpi_status status;
90d38ceaf9SAlex Deucher 	union acpi_object atif_arg_elements[2];
91d38ceaf9SAlex Deucher 	struct acpi_object_list atif_arg;
92d38ceaf9SAlex Deucher 	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
93d38ceaf9SAlex Deucher 
94d38ceaf9SAlex Deucher 	atif_arg.count = 2;
95d38ceaf9SAlex Deucher 	atif_arg.pointer = &atif_arg_elements[0];
96d38ceaf9SAlex Deucher 
97d38ceaf9SAlex Deucher 	atif_arg_elements[0].type = ACPI_TYPE_INTEGER;
98d38ceaf9SAlex Deucher 	atif_arg_elements[0].integer.value = function;
99d38ceaf9SAlex Deucher 
100d38ceaf9SAlex Deucher 	if (params) {
101d38ceaf9SAlex Deucher 		atif_arg_elements[1].type = ACPI_TYPE_BUFFER;
102d38ceaf9SAlex Deucher 		atif_arg_elements[1].buffer.length = params->length;
103d38ceaf9SAlex Deucher 		atif_arg_elements[1].buffer.pointer = params->pointer;
104d38ceaf9SAlex Deucher 	} else {
105d38ceaf9SAlex Deucher 		/* We need a second fake parameter */
106d38ceaf9SAlex Deucher 		atif_arg_elements[1].type = ACPI_TYPE_INTEGER;
107d38ceaf9SAlex Deucher 		atif_arg_elements[1].integer.value = 0;
108d38ceaf9SAlex Deucher 	}
109d38ceaf9SAlex Deucher 
110280cf1a9SLyude Paul 	status = acpi_evaluate_object(atif->handle, NULL, &atif_arg,
111280cf1a9SLyude Paul 				      &buffer);
112d38ceaf9SAlex Deucher 
113d38ceaf9SAlex Deucher 	/* Fail only if calling the method fails and ATIF is supported */
114d38ceaf9SAlex Deucher 	if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
115d38ceaf9SAlex Deucher 		DRM_DEBUG_DRIVER("failed to evaluate ATIF got %s\n",
116d38ceaf9SAlex Deucher 				 acpi_format_exception(status));
117d38ceaf9SAlex Deucher 		kfree(buffer.pointer);
118d38ceaf9SAlex Deucher 		return NULL;
119d38ceaf9SAlex Deucher 	}
120d38ceaf9SAlex Deucher 
121d38ceaf9SAlex Deucher 	return buffer.pointer;
122d38ceaf9SAlex Deucher }
123d38ceaf9SAlex Deucher 
124d38ceaf9SAlex Deucher /**
125d38ceaf9SAlex Deucher  * amdgpu_atif_parse_notification - parse supported notifications
126d38ceaf9SAlex Deucher  *
127d38ceaf9SAlex Deucher  * @n: supported notifications struct
128d38ceaf9SAlex Deucher  * @mask: supported notifications mask from ATIF
129d38ceaf9SAlex Deucher  *
130d38ceaf9SAlex Deucher  * Use the supported notifications mask from ATIF function
131d38ceaf9SAlex Deucher  * ATIF_FUNCTION_VERIFY_INTERFACE to determine what notifications
132d38ceaf9SAlex Deucher  * are supported (all asics).
133d38ceaf9SAlex Deucher  */
134d38ceaf9SAlex Deucher static void amdgpu_atif_parse_notification(struct amdgpu_atif_notifications *n, u32 mask)
135d38ceaf9SAlex Deucher {
136d38ceaf9SAlex Deucher 	n->thermal_state = mask & ATIF_THERMAL_STATE_CHANGE_REQUEST_SUPPORTED;
137d38ceaf9SAlex Deucher 	n->forced_power_state = mask & ATIF_FORCED_POWER_STATE_CHANGE_REQUEST_SUPPORTED;
138d38ceaf9SAlex Deucher 	n->system_power_state = mask & ATIF_SYSTEM_POWER_SOURCE_CHANGE_REQUEST_SUPPORTED;
139d38ceaf9SAlex Deucher 	n->brightness_change = mask & ATIF_PANEL_BRIGHTNESS_CHANGE_REQUEST_SUPPORTED;
140d38ceaf9SAlex Deucher 	n->dgpu_display_event = mask & ATIF_DGPU_DISPLAY_EVENT_SUPPORTED;
1417349a3afSDavid Francis 	n->gpu_package_power_limit = mask & ATIF_GPU_PACKAGE_POWER_LIMIT_REQUEST_SUPPORTED;
142d38ceaf9SAlex Deucher }
143d38ceaf9SAlex Deucher 
144d38ceaf9SAlex Deucher /**
145d38ceaf9SAlex Deucher  * amdgpu_atif_parse_functions - parse supported functions
146d38ceaf9SAlex Deucher  *
147d38ceaf9SAlex Deucher  * @f: supported functions struct
148d38ceaf9SAlex Deucher  * @mask: supported functions mask from ATIF
149d38ceaf9SAlex Deucher  *
150d38ceaf9SAlex Deucher  * Use the supported functions mask from ATIF function
151d38ceaf9SAlex Deucher  * ATIF_FUNCTION_VERIFY_INTERFACE to determine what functions
152d38ceaf9SAlex Deucher  * are supported (all asics).
153d38ceaf9SAlex Deucher  */
154d38ceaf9SAlex Deucher static void amdgpu_atif_parse_functions(struct amdgpu_atif_functions *f, u32 mask)
155d38ceaf9SAlex Deucher {
156d38ceaf9SAlex Deucher 	f->system_params = mask & ATIF_GET_SYSTEM_PARAMETERS_SUPPORTED;
157d38ceaf9SAlex Deucher 	f->sbios_requests = mask & ATIF_GET_SYSTEM_BIOS_REQUESTS_SUPPORTED;
158d38ceaf9SAlex Deucher 	f->temperature_change = mask & ATIF_TEMPERATURE_CHANGE_NOTIFICATION_SUPPORTED;
1597349a3afSDavid Francis 	f->query_backlight_transfer_characteristics =
1607349a3afSDavid Francis 		mask & ATIF_QUERY_BACKLIGHT_TRANSFER_CHARACTERISTICS_SUPPORTED;
1617349a3afSDavid Francis 	f->ready_to_undock = mask & ATIF_READY_TO_UNDOCK_NOTIFICATION_SUPPORTED;
1627349a3afSDavid Francis 	f->external_gpu_information = mask & ATIF_GET_EXTERNAL_GPU_INFORMATION_SUPPORTED;
163d38ceaf9SAlex Deucher }
164d38ceaf9SAlex Deucher 
165d38ceaf9SAlex Deucher /**
166d38ceaf9SAlex Deucher  * amdgpu_atif_verify_interface - verify ATIF
167d38ceaf9SAlex Deucher  *
168d38ceaf9SAlex Deucher  * @handle: acpi handle
169d38ceaf9SAlex Deucher  * @atif: amdgpu atif struct
170d38ceaf9SAlex Deucher  *
171d38ceaf9SAlex Deucher  * Execute the ATIF_FUNCTION_VERIFY_INTERFACE ATIF function
172d38ceaf9SAlex Deucher  * to initialize ATIF and determine what features are supported
173d38ceaf9SAlex Deucher  * (all asics).
174d38ceaf9SAlex Deucher  * returns 0 on success, error on failure.
175d38ceaf9SAlex Deucher  */
176280cf1a9SLyude Paul static int amdgpu_atif_verify_interface(struct amdgpu_atif *atif)
177d38ceaf9SAlex Deucher {
178d38ceaf9SAlex Deucher 	union acpi_object *info;
179d38ceaf9SAlex Deucher 	struct atif_verify_interface output;
180d38ceaf9SAlex Deucher 	size_t size;
181d38ceaf9SAlex Deucher 	int err = 0;
182d38ceaf9SAlex Deucher 
183280cf1a9SLyude Paul 	info = amdgpu_atif_call(atif, ATIF_FUNCTION_VERIFY_INTERFACE, NULL);
184d38ceaf9SAlex Deucher 	if (!info)
185d38ceaf9SAlex Deucher 		return -EIO;
186d38ceaf9SAlex Deucher 
187d38ceaf9SAlex Deucher 	memset(&output, 0, sizeof(output));
188d38ceaf9SAlex Deucher 
189d38ceaf9SAlex Deucher 	size = *(u16 *) info->buffer.pointer;
190d38ceaf9SAlex Deucher 	if (size < 12) {
191d38ceaf9SAlex Deucher 		DRM_INFO("ATIF buffer is too small: %zu\n", size);
192d38ceaf9SAlex Deucher 		err = -EINVAL;
193d38ceaf9SAlex Deucher 		goto out;
194d38ceaf9SAlex Deucher 	}
195d38ceaf9SAlex Deucher 	size = min(sizeof(output), size);
196d38ceaf9SAlex Deucher 
197d38ceaf9SAlex Deucher 	memcpy(&output, info->buffer.pointer, size);
198d38ceaf9SAlex Deucher 
199d38ceaf9SAlex Deucher 	/* TODO: check version? */
200d38ceaf9SAlex Deucher 	DRM_DEBUG_DRIVER("ATIF version %u\n", output.version);
201d38ceaf9SAlex Deucher 
202d38ceaf9SAlex Deucher 	amdgpu_atif_parse_notification(&atif->notifications, output.notification_mask);
203d38ceaf9SAlex Deucher 	amdgpu_atif_parse_functions(&atif->functions, output.function_bits);
204d38ceaf9SAlex Deucher 
205d38ceaf9SAlex Deucher out:
206d38ceaf9SAlex Deucher 	kfree(info);
207d38ceaf9SAlex Deucher 	return err;
208d38ceaf9SAlex Deucher }
209d38ceaf9SAlex Deucher 
210280cf1a9SLyude Paul static acpi_handle amdgpu_atif_probe_handle(acpi_handle dhandle)
211280cf1a9SLyude Paul {
212280cf1a9SLyude Paul 	acpi_handle handle = NULL;
213280cf1a9SLyude Paul 	char acpi_method_name[255] = { 0 };
214280cf1a9SLyude Paul 	struct acpi_buffer buffer = { sizeof(acpi_method_name), acpi_method_name };
215280cf1a9SLyude Paul 	acpi_status status;
216280cf1a9SLyude Paul 
217280cf1a9SLyude Paul 	/* For PX/HG systems, ATIF and ATPX are in the iGPU's namespace, on dGPU only
218280cf1a9SLyude Paul 	 * systems, ATIF is in the dGPU's namespace.
219280cf1a9SLyude Paul 	 */
220280cf1a9SLyude Paul 	status = acpi_get_handle(dhandle, "ATIF", &handle);
221280cf1a9SLyude Paul 	if (ACPI_SUCCESS(status))
222280cf1a9SLyude Paul 		goto out;
223280cf1a9SLyude Paul 
224280cf1a9SLyude Paul 	if (amdgpu_has_atpx()) {
225280cf1a9SLyude Paul 		status = acpi_get_handle(amdgpu_atpx_get_dhandle(), "ATIF",
226280cf1a9SLyude Paul 					 &handle);
227280cf1a9SLyude Paul 		if (ACPI_SUCCESS(status))
228280cf1a9SLyude Paul 			goto out;
229280cf1a9SLyude Paul 	}
230280cf1a9SLyude Paul 
231280cf1a9SLyude Paul 	DRM_DEBUG_DRIVER("No ATIF handle found\n");
232280cf1a9SLyude Paul 	return NULL;
233280cf1a9SLyude Paul out:
234280cf1a9SLyude Paul 	acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
235280cf1a9SLyude Paul 	DRM_DEBUG_DRIVER("Found ATIF handle %s\n", acpi_method_name);
236280cf1a9SLyude Paul 	return handle;
237280cf1a9SLyude Paul }
238280cf1a9SLyude Paul 
239d38ceaf9SAlex Deucher /**
240d38ceaf9SAlex Deucher  * amdgpu_atif_get_notification_params - determine notify configuration
241d38ceaf9SAlex Deucher  *
242d38ceaf9SAlex Deucher  * @handle: acpi handle
243d38ceaf9SAlex Deucher  * @n: atif notification configuration struct
244d38ceaf9SAlex Deucher  *
245d38ceaf9SAlex Deucher  * Execute the ATIF_FUNCTION_GET_SYSTEM_PARAMETERS ATIF function
246d38ceaf9SAlex Deucher  * to determine if a notifier is used and if so which one
247d38ceaf9SAlex Deucher  * (all asics).  This is either Notify(VGA, 0x81) or Notify(VGA, n)
248d38ceaf9SAlex Deucher  * where n is specified in the result if a notifier is used.
249d38ceaf9SAlex Deucher  * Returns 0 on success, error on failure.
250d38ceaf9SAlex Deucher  */
251280cf1a9SLyude Paul static int amdgpu_atif_get_notification_params(struct amdgpu_atif *atif)
252d38ceaf9SAlex Deucher {
253d38ceaf9SAlex Deucher 	union acpi_object *info;
254280cf1a9SLyude Paul 	struct amdgpu_atif_notification_cfg *n = &atif->notification_cfg;
255d38ceaf9SAlex Deucher 	struct atif_system_params params;
256d38ceaf9SAlex Deucher 	size_t size;
257d38ceaf9SAlex Deucher 	int err = 0;
258d38ceaf9SAlex Deucher 
259280cf1a9SLyude Paul 	info = amdgpu_atif_call(atif, ATIF_FUNCTION_GET_SYSTEM_PARAMETERS,
260280cf1a9SLyude Paul 				NULL);
261d38ceaf9SAlex Deucher 	if (!info) {
262d38ceaf9SAlex Deucher 		err = -EIO;
263d38ceaf9SAlex Deucher 		goto out;
264d38ceaf9SAlex Deucher 	}
265d38ceaf9SAlex Deucher 
266d38ceaf9SAlex Deucher 	size = *(u16 *) info->buffer.pointer;
267d38ceaf9SAlex Deucher 	if (size < 10) {
268d38ceaf9SAlex Deucher 		err = -EINVAL;
269d38ceaf9SAlex Deucher 		goto out;
270d38ceaf9SAlex Deucher 	}
271d38ceaf9SAlex Deucher 
272d38ceaf9SAlex Deucher 	memset(&params, 0, sizeof(params));
273d38ceaf9SAlex Deucher 	size = min(sizeof(params), size);
274d38ceaf9SAlex Deucher 	memcpy(&params, info->buffer.pointer, size);
275d38ceaf9SAlex Deucher 
276d38ceaf9SAlex Deucher 	DRM_DEBUG_DRIVER("SYSTEM_PARAMS: mask = %#x, flags = %#x\n",
277d38ceaf9SAlex Deucher 			params.flags, params.valid_mask);
278d38ceaf9SAlex Deucher 	params.flags = params.flags & params.valid_mask;
279d38ceaf9SAlex Deucher 
280d38ceaf9SAlex Deucher 	if ((params.flags & ATIF_NOTIFY_MASK) == ATIF_NOTIFY_NONE) {
281d38ceaf9SAlex Deucher 		n->enabled = false;
282d38ceaf9SAlex Deucher 		n->command_code = 0;
283d38ceaf9SAlex Deucher 	} else if ((params.flags & ATIF_NOTIFY_MASK) == ATIF_NOTIFY_81) {
284d38ceaf9SAlex Deucher 		n->enabled = true;
285d38ceaf9SAlex Deucher 		n->command_code = 0x81;
286d38ceaf9SAlex Deucher 	} else {
287d38ceaf9SAlex Deucher 		if (size < 11) {
288d38ceaf9SAlex Deucher 			err = -EINVAL;
289d38ceaf9SAlex Deucher 			goto out;
290d38ceaf9SAlex Deucher 		}
291d38ceaf9SAlex Deucher 		n->enabled = true;
292d38ceaf9SAlex Deucher 		n->command_code = params.command_code;
293d38ceaf9SAlex Deucher 	}
294d38ceaf9SAlex Deucher 
295d38ceaf9SAlex Deucher out:
296d38ceaf9SAlex Deucher 	DRM_DEBUG_DRIVER("Notification %s, command code = %#x\n",
297d38ceaf9SAlex Deucher 			(n->enabled ? "enabled" : "disabled"),
298d38ceaf9SAlex Deucher 			n->command_code);
299d38ceaf9SAlex Deucher 	kfree(info);
300d38ceaf9SAlex Deucher 	return err;
301d38ceaf9SAlex Deucher }
302d38ceaf9SAlex Deucher 
303d38ceaf9SAlex Deucher /**
304206bbafeSDavid Francis  * amdgpu_atif_query_backlight_caps - get min and max backlight input signal
305206bbafeSDavid Francis  *
306206bbafeSDavid Francis  * @handle: acpi handle
307206bbafeSDavid Francis  *
308206bbafeSDavid Francis  * Execute the QUERY_BRIGHTNESS_TRANSFER_CHARACTERISTICS ATIF function
309206bbafeSDavid Francis  * to determine the acceptable range of backlight values
310206bbafeSDavid Francis  *
311206bbafeSDavid Francis  * Backlight_caps.caps_valid will be set to true if the query is successful
312206bbafeSDavid Francis  *
313206bbafeSDavid Francis  * The input signals are in range 0-255
314206bbafeSDavid Francis  *
315206bbafeSDavid Francis  * This function assumes the display with backlight is the first LCD
316206bbafeSDavid Francis  *
317206bbafeSDavid Francis  * Returns 0 on success, error on failure.
318206bbafeSDavid Francis  */
319206bbafeSDavid Francis static int amdgpu_atif_query_backlight_caps(struct amdgpu_atif *atif)
320206bbafeSDavid Francis {
321206bbafeSDavid Francis 	union acpi_object *info;
322206bbafeSDavid Francis 	struct atif_qbtc_output characteristics;
323206bbafeSDavid Francis 	struct atif_qbtc_arguments arguments;
324206bbafeSDavid Francis 	struct acpi_buffer params;
325206bbafeSDavid Francis 	size_t size;
326206bbafeSDavid Francis 	int err = 0;
327206bbafeSDavid Francis 
328206bbafeSDavid Francis 	arguments.size = sizeof(arguments);
329206bbafeSDavid Francis 	arguments.requested_display = ATIF_QBTC_REQUEST_LCD1;
330206bbafeSDavid Francis 
331206bbafeSDavid Francis 	params.length = sizeof(arguments);
332206bbafeSDavid Francis 	params.pointer = (void *)&arguments;
333206bbafeSDavid Francis 
334206bbafeSDavid Francis 	info = amdgpu_atif_call(atif,
335206bbafeSDavid Francis 		ATIF_FUNCTION_QUERY_BRIGHTNESS_TRANSFER_CHARACTERISTICS,
336206bbafeSDavid Francis 		&params);
337206bbafeSDavid Francis 	if (!info) {
338206bbafeSDavid Francis 		err = -EIO;
339206bbafeSDavid Francis 		goto out;
340206bbafeSDavid Francis 	}
341206bbafeSDavid Francis 
342206bbafeSDavid Francis 	size = *(u16 *) info->buffer.pointer;
343206bbafeSDavid Francis 	if (size < 10) {
344206bbafeSDavid Francis 		err = -EINVAL;
345206bbafeSDavid Francis 		goto out;
346206bbafeSDavid Francis 	}
347206bbafeSDavid Francis 
348206bbafeSDavid Francis 	memset(&characteristics, 0, sizeof(characteristics));
349206bbafeSDavid Francis 	size = min(sizeof(characteristics), size);
350206bbafeSDavid Francis 	memcpy(&characteristics, info->buffer.pointer, size);
351206bbafeSDavid Francis 
352206bbafeSDavid Francis 	atif->backlight_caps.caps_valid = true;
353206bbafeSDavid Francis 	atif->backlight_caps.min_input_signal =
354206bbafeSDavid Francis 			characteristics.min_input_signal;
355206bbafeSDavid Francis 	atif->backlight_caps.max_input_signal =
356206bbafeSDavid Francis 			characteristics.max_input_signal;
357206bbafeSDavid Francis out:
358206bbafeSDavid Francis 	kfree(info);
359206bbafeSDavid Francis 	return err;
360206bbafeSDavid Francis }
361206bbafeSDavid Francis 
362206bbafeSDavid Francis /**
363d38ceaf9SAlex Deucher  * amdgpu_atif_get_sbios_requests - get requested sbios event
364d38ceaf9SAlex Deucher  *
365d38ceaf9SAlex Deucher  * @handle: acpi handle
366d38ceaf9SAlex Deucher  * @req: atif sbios request struct
367d38ceaf9SAlex Deucher  *
368d38ceaf9SAlex Deucher  * Execute the ATIF_FUNCTION_GET_SYSTEM_BIOS_REQUESTS ATIF function
369d38ceaf9SAlex Deucher  * to determine what requests the sbios is making to the driver
370d38ceaf9SAlex Deucher  * (all asics).
371d38ceaf9SAlex Deucher  * Returns 0 on success, error on failure.
372d38ceaf9SAlex Deucher  */
373280cf1a9SLyude Paul static int amdgpu_atif_get_sbios_requests(struct amdgpu_atif *atif,
374d38ceaf9SAlex Deucher 					  struct atif_sbios_requests *req)
375d38ceaf9SAlex Deucher {
376d38ceaf9SAlex Deucher 	union acpi_object *info;
377d38ceaf9SAlex Deucher 	size_t size;
378d38ceaf9SAlex Deucher 	int count = 0;
379d38ceaf9SAlex Deucher 
380280cf1a9SLyude Paul 	info = amdgpu_atif_call(atif, ATIF_FUNCTION_GET_SYSTEM_BIOS_REQUESTS,
381280cf1a9SLyude Paul 				NULL);
382d38ceaf9SAlex Deucher 	if (!info)
383d38ceaf9SAlex Deucher 		return -EIO;
384d38ceaf9SAlex Deucher 
385d38ceaf9SAlex Deucher 	size = *(u16 *)info->buffer.pointer;
386d38ceaf9SAlex Deucher 	if (size < 0xd) {
387d38ceaf9SAlex Deucher 		count = -EINVAL;
388d38ceaf9SAlex Deucher 		goto out;
389d38ceaf9SAlex Deucher 	}
390d38ceaf9SAlex Deucher 	memset(req, 0, sizeof(*req));
391d38ceaf9SAlex Deucher 
392d38ceaf9SAlex Deucher 	size = min(sizeof(*req), size);
393d38ceaf9SAlex Deucher 	memcpy(req, info->buffer.pointer, size);
394d38ceaf9SAlex Deucher 	DRM_DEBUG_DRIVER("SBIOS pending requests: %#x\n", req->pending);
395d38ceaf9SAlex Deucher 
396d38ceaf9SAlex Deucher 	count = hweight32(req->pending);
397d38ceaf9SAlex Deucher 
398d38ceaf9SAlex Deucher out:
399d38ceaf9SAlex Deucher 	kfree(info);
400d38ceaf9SAlex Deucher 	return count;
401d38ceaf9SAlex Deucher }
402d38ceaf9SAlex Deucher 
403d38ceaf9SAlex Deucher /**
404d38ceaf9SAlex Deucher  * amdgpu_atif_handler - handle ATIF notify requests
405d38ceaf9SAlex Deucher  *
406d38ceaf9SAlex Deucher  * @adev: amdgpu_device pointer
407d38ceaf9SAlex Deucher  * @event: atif sbios request struct
408d38ceaf9SAlex Deucher  *
409d38ceaf9SAlex Deucher  * Checks the acpi event and if it matches an atif event,
410d38ceaf9SAlex Deucher  * handles it.
411582f58deSLyude Paul  *
412582f58deSLyude Paul  * Returns:
413582f58deSLyude Paul  * NOTIFY_BAD or NOTIFY_DONE, depending on the event.
414d38ceaf9SAlex Deucher  */
415b7cecbe8SJean Delvare static int amdgpu_atif_handler(struct amdgpu_device *adev,
416d38ceaf9SAlex Deucher 			       struct acpi_bus_event *event)
417d38ceaf9SAlex Deucher {
418102c16a0SLyude Paul 	struct amdgpu_atif *atif = adev->atif;
419d38ceaf9SAlex Deucher 	int count;
420d38ceaf9SAlex Deucher 
421d38ceaf9SAlex Deucher 	DRM_DEBUG_DRIVER("event, device_class = %s, type = %#x\n",
422d38ceaf9SAlex Deucher 			event->device_class, event->type);
423d38ceaf9SAlex Deucher 
424d38ceaf9SAlex Deucher 	if (strcmp(event->device_class, ACPI_VIDEO_CLASS) != 0)
425d38ceaf9SAlex Deucher 		return NOTIFY_DONE;
426d38ceaf9SAlex Deucher 
427582f58deSLyude Paul 	/* Is this actually our event? */
428102c16a0SLyude Paul 	if (!atif ||
429102c16a0SLyude Paul 	    !atif->notification_cfg.enabled ||
430582f58deSLyude Paul 	    event->type != atif->notification_cfg.command_code) {
431582f58deSLyude Paul 		/* These events will generate keypresses otherwise */
432582f58deSLyude Paul 		if (event->type == ACPI_VIDEO_NOTIFY_PROBE)
433582f58deSLyude Paul 			return NOTIFY_BAD;
434582f58deSLyude Paul 		else
435d38ceaf9SAlex Deucher 			return NOTIFY_DONE;
436582f58deSLyude Paul 	}
437d38ceaf9SAlex Deucher 
4389e7204beSAlex Deucher 	if (atif->functions.sbios_requests) {
4399e7204beSAlex Deucher 		struct atif_sbios_requests req;
4409e7204beSAlex Deucher 
441d38ceaf9SAlex Deucher 		/* Check pending SBIOS requests */
442280cf1a9SLyude Paul 		count = amdgpu_atif_get_sbios_requests(atif, &req);
443d38ceaf9SAlex Deucher 
444d38ceaf9SAlex Deucher 		if (count <= 0)
445582f58deSLyude Paul 			return NOTIFY_BAD;
446d38ceaf9SAlex Deucher 
447d38ceaf9SAlex Deucher 		DRM_DEBUG_DRIVER("ATIF: %d pending SBIOS requests\n", count);
448d38ceaf9SAlex Deucher 
44997d798b2SAlex Deucher 		if (req.pending & ATIF_PANEL_BRIGHTNESS_CHANGE_REQUEST) {
45097d798b2SAlex Deucher #if defined(CONFIG_BACKLIGHT_CLASS_DEVICE) || defined(CONFIG_BACKLIGHT_CLASS_DEVICE_MODULE)
45197d798b2SAlex Deucher 			if (atif->bd) {
452d38ceaf9SAlex Deucher 				DRM_DEBUG_DRIVER("Changing brightness to %d\n",
453d38ceaf9SAlex Deucher 						 req.backlight_level);
454bcb7b0efSAndriy Gapon 				/*
455bcb7b0efSAndriy Gapon 				 * XXX backlight_device_set_brightness() is
456bcb7b0efSAndriy Gapon 				 * hardwired to post BACKLIGHT_UPDATE_SYSFS.
457bcb7b0efSAndriy Gapon 				 * It probably should accept 'reason' parameter.
458bcb7b0efSAndriy Gapon 				 */
45997d798b2SAlex Deucher 				backlight_device_set_brightness(atif->bd, req.backlight_level);
460bcb7b0efSAndriy Gapon 			}
461bcb7b0efSAndriy Gapon #endif
46297d798b2SAlex Deucher 		}
46397d798b2SAlex Deucher 
4641b0f568dSAlex Deucher 		if (req.pending & ATIF_DGPU_DISPLAY_EVENT) {
465bdb1ccb0SAaron Liu 			if (adev->flags & AMD_IS_PX) {
4664a580877SLuben Tuikov 				pm_runtime_get_sync(adev_to_drm(adev)->dev);
4671b0f568dSAlex Deucher 				/* Just fire off a uevent and let userspace tell us what to do */
4684a580877SLuben Tuikov 				drm_helper_hpd_irq_event(adev_to_drm(adev));
4694a580877SLuben Tuikov 				pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
4704a580877SLuben Tuikov 				pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
4711b0f568dSAlex Deucher 			}
4721b0f568dSAlex Deucher 		}
473d38ceaf9SAlex Deucher 		/* TODO: check other events */
4749e7204beSAlex Deucher 	}
475d38ceaf9SAlex Deucher 
476d38ceaf9SAlex Deucher 	/* We've handled the event, stop the notifier chain. The ACPI interface
477d38ceaf9SAlex Deucher 	 * overloads ACPI_VIDEO_NOTIFY_PROBE, we don't want to send that to
478d38ceaf9SAlex Deucher 	 * userspace if the event was generated only to signal a SBIOS
479d38ceaf9SAlex Deucher 	 * request.
480d38ceaf9SAlex Deucher 	 */
481d38ceaf9SAlex Deucher 	return NOTIFY_BAD;
482d38ceaf9SAlex Deucher }
483d38ceaf9SAlex Deucher 
484d38ceaf9SAlex Deucher /* Call the ATCS method
485d38ceaf9SAlex Deucher  */
486d38ceaf9SAlex Deucher /**
487d38ceaf9SAlex Deucher  * amdgpu_atcs_call - call an ATCS method
488d38ceaf9SAlex Deucher  *
489d38ceaf9SAlex Deucher  * @handle: acpi handle
490d38ceaf9SAlex Deucher  * @function: the ATCS function to execute
491d38ceaf9SAlex Deucher  * @params: ATCS function params
492d38ceaf9SAlex Deucher  *
493d38ceaf9SAlex Deucher  * Executes the requested ATCS function (all asics).
494d38ceaf9SAlex Deucher  * Returns a pointer to the acpi output buffer.
495d38ceaf9SAlex Deucher  */
496d38ceaf9SAlex Deucher static union acpi_object *amdgpu_atcs_call(acpi_handle handle, int function,
497d38ceaf9SAlex Deucher 					   struct acpi_buffer *params)
498d38ceaf9SAlex Deucher {
499d38ceaf9SAlex Deucher 	acpi_status status;
500d38ceaf9SAlex Deucher 	union acpi_object atcs_arg_elements[2];
501d38ceaf9SAlex Deucher 	struct acpi_object_list atcs_arg;
502d38ceaf9SAlex Deucher 	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
503d38ceaf9SAlex Deucher 
504d38ceaf9SAlex Deucher 	atcs_arg.count = 2;
505d38ceaf9SAlex Deucher 	atcs_arg.pointer = &atcs_arg_elements[0];
506d38ceaf9SAlex Deucher 
507d38ceaf9SAlex Deucher 	atcs_arg_elements[0].type = ACPI_TYPE_INTEGER;
508d38ceaf9SAlex Deucher 	atcs_arg_elements[0].integer.value = function;
509d38ceaf9SAlex Deucher 
510d38ceaf9SAlex Deucher 	if (params) {
511d38ceaf9SAlex Deucher 		atcs_arg_elements[1].type = ACPI_TYPE_BUFFER;
512d38ceaf9SAlex Deucher 		atcs_arg_elements[1].buffer.length = params->length;
513d38ceaf9SAlex Deucher 		atcs_arg_elements[1].buffer.pointer = params->pointer;
514d38ceaf9SAlex Deucher 	} else {
515d38ceaf9SAlex Deucher 		/* We need a second fake parameter */
516d38ceaf9SAlex Deucher 		atcs_arg_elements[1].type = ACPI_TYPE_INTEGER;
517d38ceaf9SAlex Deucher 		atcs_arg_elements[1].integer.value = 0;
518d38ceaf9SAlex Deucher 	}
519d38ceaf9SAlex Deucher 
520d38ceaf9SAlex Deucher 	status = acpi_evaluate_object(handle, "ATCS", &atcs_arg, &buffer);
521d38ceaf9SAlex Deucher 
522d38ceaf9SAlex Deucher 	/* Fail only if calling the method fails and ATIF is supported */
523d38ceaf9SAlex Deucher 	if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
524d38ceaf9SAlex Deucher 		DRM_DEBUG_DRIVER("failed to evaluate ATCS got %s\n",
525d38ceaf9SAlex Deucher 				 acpi_format_exception(status));
526d38ceaf9SAlex Deucher 		kfree(buffer.pointer);
527d38ceaf9SAlex Deucher 		return NULL;
528d38ceaf9SAlex Deucher 	}
529d38ceaf9SAlex Deucher 
530d38ceaf9SAlex Deucher 	return buffer.pointer;
531d38ceaf9SAlex Deucher }
532d38ceaf9SAlex Deucher 
533d38ceaf9SAlex Deucher /**
534d38ceaf9SAlex Deucher  * amdgpu_atcs_parse_functions - parse supported functions
535d38ceaf9SAlex Deucher  *
536d38ceaf9SAlex Deucher  * @f: supported functions struct
537d38ceaf9SAlex Deucher  * @mask: supported functions mask from ATCS
538d38ceaf9SAlex Deucher  *
539d38ceaf9SAlex Deucher  * Use the supported functions mask from ATCS function
540d38ceaf9SAlex Deucher  * ATCS_FUNCTION_VERIFY_INTERFACE to determine what functions
541d38ceaf9SAlex Deucher  * are supported (all asics).
542d38ceaf9SAlex Deucher  */
543d38ceaf9SAlex Deucher static void amdgpu_atcs_parse_functions(struct amdgpu_atcs_functions *f, u32 mask)
544d38ceaf9SAlex Deucher {
545d38ceaf9SAlex Deucher 	f->get_ext_state = mask & ATCS_GET_EXTERNAL_STATE_SUPPORTED;
546d38ceaf9SAlex Deucher 	f->pcie_perf_req = mask & ATCS_PCIE_PERFORMANCE_REQUEST_SUPPORTED;
547d38ceaf9SAlex Deucher 	f->pcie_dev_rdy = mask & ATCS_PCIE_DEVICE_READY_NOTIFICATION_SUPPORTED;
548d38ceaf9SAlex Deucher 	f->pcie_bus_width = mask & ATCS_SET_PCIE_BUS_WIDTH_SUPPORTED;
549d38ceaf9SAlex Deucher }
550d38ceaf9SAlex Deucher 
551d38ceaf9SAlex Deucher /**
552d38ceaf9SAlex Deucher  * amdgpu_atcs_verify_interface - verify ATCS
553d38ceaf9SAlex Deucher  *
554d38ceaf9SAlex Deucher  * @handle: acpi handle
555d38ceaf9SAlex Deucher  * @atcs: amdgpu atcs struct
556d38ceaf9SAlex Deucher  *
557d38ceaf9SAlex Deucher  * Execute the ATCS_FUNCTION_VERIFY_INTERFACE ATCS function
558d38ceaf9SAlex Deucher  * to initialize ATCS and determine what features are supported
559d38ceaf9SAlex Deucher  * (all asics).
560d38ceaf9SAlex Deucher  * returns 0 on success, error on failure.
561d38ceaf9SAlex Deucher  */
562d38ceaf9SAlex Deucher static int amdgpu_atcs_verify_interface(acpi_handle handle,
563d38ceaf9SAlex Deucher 					struct amdgpu_atcs *atcs)
564d38ceaf9SAlex Deucher {
565d38ceaf9SAlex Deucher 	union acpi_object *info;
566d38ceaf9SAlex Deucher 	struct atcs_verify_interface output;
567d38ceaf9SAlex Deucher 	size_t size;
568d38ceaf9SAlex Deucher 	int err = 0;
569d38ceaf9SAlex Deucher 
570d38ceaf9SAlex Deucher 	info = amdgpu_atcs_call(handle, ATCS_FUNCTION_VERIFY_INTERFACE, NULL);
571d38ceaf9SAlex Deucher 	if (!info)
572d38ceaf9SAlex Deucher 		return -EIO;
573d38ceaf9SAlex Deucher 
574d38ceaf9SAlex Deucher 	memset(&output, 0, sizeof(output));
575d38ceaf9SAlex Deucher 
576d38ceaf9SAlex Deucher 	size = *(u16 *) info->buffer.pointer;
577d38ceaf9SAlex Deucher 	if (size < 8) {
578d38ceaf9SAlex Deucher 		DRM_INFO("ATCS buffer is too small: %zu\n", size);
579d38ceaf9SAlex Deucher 		err = -EINVAL;
580d38ceaf9SAlex Deucher 		goto out;
581d38ceaf9SAlex Deucher 	}
582d38ceaf9SAlex Deucher 	size = min(sizeof(output), size);
583d38ceaf9SAlex Deucher 
584d38ceaf9SAlex Deucher 	memcpy(&output, info->buffer.pointer, size);
585d38ceaf9SAlex Deucher 
586d38ceaf9SAlex Deucher 	/* TODO: check version? */
587d38ceaf9SAlex Deucher 	DRM_DEBUG_DRIVER("ATCS version %u\n", output.version);
588d38ceaf9SAlex Deucher 
589d38ceaf9SAlex Deucher 	amdgpu_atcs_parse_functions(&atcs->functions, output.function_bits);
590d38ceaf9SAlex Deucher 
591d38ceaf9SAlex Deucher out:
592d38ceaf9SAlex Deucher 	kfree(info);
593d38ceaf9SAlex Deucher 	return err;
594d38ceaf9SAlex Deucher }
595d38ceaf9SAlex Deucher 
596d38ceaf9SAlex Deucher /**
597d38ceaf9SAlex Deucher  * amdgpu_acpi_is_pcie_performance_request_supported
598d38ceaf9SAlex Deucher  *
599d38ceaf9SAlex Deucher  * @adev: amdgpu_device pointer
600d38ceaf9SAlex Deucher  *
601d38ceaf9SAlex Deucher  * Check if the ATCS pcie_perf_req and pcie_dev_rdy methods
602d38ceaf9SAlex Deucher  * are supported (all asics).
603d38ceaf9SAlex Deucher  * returns true if supported, false if not.
604d38ceaf9SAlex Deucher  */
605d38ceaf9SAlex Deucher bool amdgpu_acpi_is_pcie_performance_request_supported(struct amdgpu_device *adev)
606d38ceaf9SAlex Deucher {
607d38ceaf9SAlex Deucher 	struct amdgpu_atcs *atcs = &adev->atcs;
608d38ceaf9SAlex Deucher 
609d38ceaf9SAlex Deucher 	if (atcs->functions.pcie_perf_req && atcs->functions.pcie_dev_rdy)
610d38ceaf9SAlex Deucher 		return true;
611d38ceaf9SAlex Deucher 
612d38ceaf9SAlex Deucher 	return false;
613d38ceaf9SAlex Deucher }
614d38ceaf9SAlex Deucher 
615d38ceaf9SAlex Deucher /**
616d38ceaf9SAlex Deucher  * amdgpu_acpi_pcie_notify_device_ready
617d38ceaf9SAlex Deucher  *
618d38ceaf9SAlex Deucher  * @adev: amdgpu_device pointer
619d38ceaf9SAlex Deucher  *
620d38ceaf9SAlex Deucher  * Executes the PCIE_DEVICE_READY_NOTIFICATION method
621d38ceaf9SAlex Deucher  * (all asics).
622d38ceaf9SAlex Deucher  * returns 0 on success, error on failure.
623d38ceaf9SAlex Deucher  */
624d38ceaf9SAlex Deucher int amdgpu_acpi_pcie_notify_device_ready(struct amdgpu_device *adev)
625d38ceaf9SAlex Deucher {
626d38ceaf9SAlex Deucher 	acpi_handle handle;
627d38ceaf9SAlex Deucher 	union acpi_object *info;
628d38ceaf9SAlex Deucher 	struct amdgpu_atcs *atcs = &adev->atcs;
629d38ceaf9SAlex Deucher 
630d38ceaf9SAlex Deucher 	/* Get the device handle */
631d38ceaf9SAlex Deucher 	handle = ACPI_HANDLE(&adev->pdev->dev);
632d38ceaf9SAlex Deucher 	if (!handle)
633d38ceaf9SAlex Deucher 		return -EINVAL;
634d38ceaf9SAlex Deucher 
635d38ceaf9SAlex Deucher 	if (!atcs->functions.pcie_dev_rdy)
636d38ceaf9SAlex Deucher 		return -EINVAL;
637d38ceaf9SAlex Deucher 
638d38ceaf9SAlex Deucher 	info = amdgpu_atcs_call(handle, ATCS_FUNCTION_PCIE_DEVICE_READY_NOTIFICATION, NULL);
639d38ceaf9SAlex Deucher 	if (!info)
640d38ceaf9SAlex Deucher 		return -EIO;
641d38ceaf9SAlex Deucher 
642d38ceaf9SAlex Deucher 	kfree(info);
643d38ceaf9SAlex Deucher 
644d38ceaf9SAlex Deucher 	return 0;
645d38ceaf9SAlex Deucher }
646d38ceaf9SAlex Deucher 
647d38ceaf9SAlex Deucher /**
648d38ceaf9SAlex Deucher  * amdgpu_acpi_pcie_performance_request
649d38ceaf9SAlex Deucher  *
650d38ceaf9SAlex Deucher  * @adev: amdgpu_device pointer
651d38ceaf9SAlex Deucher  * @perf_req: requested perf level (pcie gen speed)
652d38ceaf9SAlex Deucher  * @advertise: set advertise caps flag if set
653d38ceaf9SAlex Deucher  *
654d38ceaf9SAlex Deucher  * Executes the PCIE_PERFORMANCE_REQUEST method to
655d38ceaf9SAlex Deucher  * change the pcie gen speed (all asics).
656d38ceaf9SAlex Deucher  * returns 0 on success, error on failure.
657d38ceaf9SAlex Deucher  */
658d38ceaf9SAlex Deucher int amdgpu_acpi_pcie_performance_request(struct amdgpu_device *adev,
659d38ceaf9SAlex Deucher 					 u8 perf_req, bool advertise)
660d38ceaf9SAlex Deucher {
661d38ceaf9SAlex Deucher 	acpi_handle handle;
662d38ceaf9SAlex Deucher 	union acpi_object *info;
663d38ceaf9SAlex Deucher 	struct amdgpu_atcs *atcs = &adev->atcs;
664d38ceaf9SAlex Deucher 	struct atcs_pref_req_input atcs_input;
665d38ceaf9SAlex Deucher 	struct atcs_pref_req_output atcs_output;
666d38ceaf9SAlex Deucher 	struct acpi_buffer params;
667d38ceaf9SAlex Deucher 	size_t size;
668d38ceaf9SAlex Deucher 	u32 retry = 3;
669d38ceaf9SAlex Deucher 
6701bced75fSRex Zhu 	if (amdgpu_acpi_pcie_notify_device_ready(adev))
6711bced75fSRex Zhu 		return -EINVAL;
6721bced75fSRex Zhu 
673d38ceaf9SAlex Deucher 	/* Get the device handle */
674d38ceaf9SAlex Deucher 	handle = ACPI_HANDLE(&adev->pdev->dev);
675d38ceaf9SAlex Deucher 	if (!handle)
676d38ceaf9SAlex Deucher 		return -EINVAL;
677d38ceaf9SAlex Deucher 
678d38ceaf9SAlex Deucher 	if (!atcs->functions.pcie_perf_req)
679d38ceaf9SAlex Deucher 		return -EINVAL;
680d38ceaf9SAlex Deucher 
681d38ceaf9SAlex Deucher 	atcs_input.size = sizeof(struct atcs_pref_req_input);
682d38ceaf9SAlex Deucher 	/* client id (bit 2-0: func num, 7-3: dev num, 15-8: bus num) */
683d38ceaf9SAlex Deucher 	atcs_input.client_id = adev->pdev->devfn | (adev->pdev->bus->number << 8);
684d38ceaf9SAlex Deucher 	atcs_input.valid_flags_mask = ATCS_VALID_FLAGS_MASK;
685d38ceaf9SAlex Deucher 	atcs_input.flags = ATCS_WAIT_FOR_COMPLETION;
686d38ceaf9SAlex Deucher 	if (advertise)
687d38ceaf9SAlex Deucher 		atcs_input.flags |= ATCS_ADVERTISE_CAPS;
688d38ceaf9SAlex Deucher 	atcs_input.req_type = ATCS_PCIE_LINK_SPEED;
689d38ceaf9SAlex Deucher 	atcs_input.perf_req = perf_req;
690d38ceaf9SAlex Deucher 
691d38ceaf9SAlex Deucher 	params.length = sizeof(struct atcs_pref_req_input);
692d38ceaf9SAlex Deucher 	params.pointer = &atcs_input;
693d38ceaf9SAlex Deucher 
694d38ceaf9SAlex Deucher 	while (retry--) {
695d38ceaf9SAlex Deucher 		info = amdgpu_atcs_call(handle, ATCS_FUNCTION_PCIE_PERFORMANCE_REQUEST, &params);
696d38ceaf9SAlex Deucher 		if (!info)
697d38ceaf9SAlex Deucher 			return -EIO;
698d38ceaf9SAlex Deucher 
699d38ceaf9SAlex Deucher 		memset(&atcs_output, 0, sizeof(atcs_output));
700d38ceaf9SAlex Deucher 
701d38ceaf9SAlex Deucher 		size = *(u16 *) info->buffer.pointer;
702d38ceaf9SAlex Deucher 		if (size < 3) {
703d38ceaf9SAlex Deucher 			DRM_INFO("ATCS buffer is too small: %zu\n", size);
704d38ceaf9SAlex Deucher 			kfree(info);
705d38ceaf9SAlex Deucher 			return -EINVAL;
706d38ceaf9SAlex Deucher 		}
707d38ceaf9SAlex Deucher 		size = min(sizeof(atcs_output), size);
708d38ceaf9SAlex Deucher 
709d38ceaf9SAlex Deucher 		memcpy(&atcs_output, info->buffer.pointer, size);
710d38ceaf9SAlex Deucher 
711d38ceaf9SAlex Deucher 		kfree(info);
712d38ceaf9SAlex Deucher 
713d38ceaf9SAlex Deucher 		switch (atcs_output.ret_val) {
714d38ceaf9SAlex Deucher 		case ATCS_REQUEST_REFUSED:
715d38ceaf9SAlex Deucher 		default:
716d38ceaf9SAlex Deucher 			return -EINVAL;
717d38ceaf9SAlex Deucher 		case ATCS_REQUEST_COMPLETE:
718d38ceaf9SAlex Deucher 			return 0;
719d38ceaf9SAlex Deucher 		case ATCS_REQUEST_IN_PROGRESS:
720d38ceaf9SAlex Deucher 			udelay(10);
721d38ceaf9SAlex Deucher 			break;
722d38ceaf9SAlex Deucher 		}
723d38ceaf9SAlex Deucher 	}
724d38ceaf9SAlex Deucher 
725d38ceaf9SAlex Deucher 	return 0;
726d38ceaf9SAlex Deucher }
727d38ceaf9SAlex Deucher 
728d38ceaf9SAlex Deucher /**
729d38ceaf9SAlex Deucher  * amdgpu_acpi_event - handle notify events
730d38ceaf9SAlex Deucher  *
731d38ceaf9SAlex Deucher  * @nb: notifier block
732d38ceaf9SAlex Deucher  * @val: val
733d38ceaf9SAlex Deucher  * @data: acpi event
734d38ceaf9SAlex Deucher  *
735d38ceaf9SAlex Deucher  * Calls relevant amdgpu functions in response to various
736d38ceaf9SAlex Deucher  * acpi events.
737d38ceaf9SAlex Deucher  * Returns NOTIFY code
738d38ceaf9SAlex Deucher  */
739d38ceaf9SAlex Deucher static int amdgpu_acpi_event(struct notifier_block *nb,
740d38ceaf9SAlex Deucher 			     unsigned long val,
741d38ceaf9SAlex Deucher 			     void *data)
742d38ceaf9SAlex Deucher {
743d38ceaf9SAlex Deucher 	struct amdgpu_device *adev = container_of(nb, struct amdgpu_device, acpi_nb);
744d38ceaf9SAlex Deucher 	struct acpi_bus_event *entry = (struct acpi_bus_event *)data;
745d38ceaf9SAlex Deucher 
746d38ceaf9SAlex Deucher 	if (strcmp(entry->device_class, ACPI_AC_CLASS) == 0) {
747d38ceaf9SAlex Deucher 		if (power_supply_is_system_supplied() > 0)
748d38ceaf9SAlex Deucher 			DRM_DEBUG_DRIVER("pm: AC\n");
749d38ceaf9SAlex Deucher 		else
750d38ceaf9SAlex Deucher 			DRM_DEBUG_DRIVER("pm: DC\n");
751d38ceaf9SAlex Deucher 
752d38ceaf9SAlex Deucher 		amdgpu_pm_acpi_event_handler(adev);
753d38ceaf9SAlex Deucher 	}
754d38ceaf9SAlex Deucher 
755d38ceaf9SAlex Deucher 	/* Check for pending SBIOS requests */
756d38ceaf9SAlex Deucher 	return amdgpu_atif_handler(adev, entry);
757d38ceaf9SAlex Deucher }
758d38ceaf9SAlex Deucher 
759d38ceaf9SAlex Deucher /* Call all ACPI methods here */
760d38ceaf9SAlex Deucher /**
761d38ceaf9SAlex Deucher  * amdgpu_acpi_init - init driver acpi support
762d38ceaf9SAlex Deucher  *
763d38ceaf9SAlex Deucher  * @adev: amdgpu_device pointer
764d38ceaf9SAlex Deucher  *
765d38ceaf9SAlex Deucher  * Verifies the AMD ACPI interfaces and registers with the acpi
766d38ceaf9SAlex Deucher  * notifier chain (all asics).
767d38ceaf9SAlex Deucher  * Returns 0 on success, error on failure.
768d38ceaf9SAlex Deucher  */
769d38ceaf9SAlex Deucher int amdgpu_acpi_init(struct amdgpu_device *adev)
770d38ceaf9SAlex Deucher {
771280cf1a9SLyude Paul 	acpi_handle handle, atif_handle;
772102c16a0SLyude Paul 	struct amdgpu_atif *atif;
773d38ceaf9SAlex Deucher 	struct amdgpu_atcs *atcs = &adev->atcs;
774d38ceaf9SAlex Deucher 	int ret;
775d38ceaf9SAlex Deucher 
776d38ceaf9SAlex Deucher 	/* Get the device handle */
777d38ceaf9SAlex Deucher 	handle = ACPI_HANDLE(&adev->pdev->dev);
778d38ceaf9SAlex Deucher 
779d38ceaf9SAlex Deucher 	if (!adev->bios || !handle)
780d38ceaf9SAlex Deucher 		return 0;
781d38ceaf9SAlex Deucher 
782d38ceaf9SAlex Deucher 	/* Call the ATCS method */
783d38ceaf9SAlex Deucher 	ret = amdgpu_atcs_verify_interface(handle, atcs);
784d38ceaf9SAlex Deucher 	if (ret) {
785d38ceaf9SAlex Deucher 		DRM_DEBUG_DRIVER("Call to ATCS verify_interface failed: %d\n", ret);
786d38ceaf9SAlex Deucher 	}
787d38ceaf9SAlex Deucher 
788280cf1a9SLyude Paul 	/* Probe for ATIF, and initialize it if found */
789280cf1a9SLyude Paul 	atif_handle = amdgpu_atif_probe_handle(handle);
790280cf1a9SLyude Paul 	if (!atif_handle)
791280cf1a9SLyude Paul 		goto out;
792280cf1a9SLyude Paul 
793102c16a0SLyude Paul 	atif = kzalloc(sizeof(*atif), GFP_KERNEL);
794102c16a0SLyude Paul 	if (!atif) {
795102c16a0SLyude Paul 		DRM_WARN("Not enough memory to initialize ATIF\n");
796102c16a0SLyude Paul 		goto out;
797102c16a0SLyude Paul 	}
798280cf1a9SLyude Paul 	atif->handle = atif_handle;
799102c16a0SLyude Paul 
800280cf1a9SLyude Paul 	/* Call the ATIF method */
801280cf1a9SLyude Paul 	ret = amdgpu_atif_verify_interface(atif);
802d38ceaf9SAlex Deucher 	if (ret) {
803d38ceaf9SAlex Deucher 		DRM_DEBUG_DRIVER("Call to ATIF verify_interface failed: %d\n", ret);
804102c16a0SLyude Paul 		kfree(atif);
805d38ceaf9SAlex Deucher 		goto out;
806d38ceaf9SAlex Deucher 	}
807102c16a0SLyude Paul 	adev->atif = atif;
808d38ceaf9SAlex Deucher 
80997d798b2SAlex Deucher #if defined(CONFIG_BACKLIGHT_CLASS_DEVICE) || defined(CONFIG_BACKLIGHT_CLASS_DEVICE_MODULE)
810*9c27bc97SYe Bin 	if (atif->notifications.brightness_change) {
81197d798b2SAlex Deucher 		if (amdgpu_device_has_dc_support(adev)) {
81297d798b2SAlex Deucher #if defined(CONFIG_DRM_AMD_DC)
81397d798b2SAlex Deucher 			struct amdgpu_display_manager *dm = &adev->dm;
81497d798b2SAlex Deucher 			atif->bd = dm->backlight_dev;
81597d798b2SAlex Deucher #endif
81697d798b2SAlex Deucher 		} else {
817d38ceaf9SAlex Deucher 			struct drm_encoder *tmp;
818d38ceaf9SAlex Deucher 
819d38ceaf9SAlex Deucher 			/* Find the encoder controlling the brightness */
8204a580877SLuben Tuikov 			list_for_each_entry(tmp, &adev_to_drm(adev)->mode_config.encoder_list,
821d38ceaf9SAlex Deucher 					    head) {
822d38ceaf9SAlex Deucher 				struct amdgpu_encoder *enc = to_amdgpu_encoder(tmp);
823d38ceaf9SAlex Deucher 
824d38ceaf9SAlex Deucher 				if ((enc->devices & (ATOM_DEVICE_LCD_SUPPORT)) &&
825d38ceaf9SAlex Deucher 				    enc->enc_priv) {
826d38ceaf9SAlex Deucher 					struct amdgpu_encoder_atom_dig *dig = enc->enc_priv;
827d38ceaf9SAlex Deucher 					if (dig->bl_dev) {
82897d798b2SAlex Deucher 						atif->bd = dig->bl_dev;
829d38ceaf9SAlex Deucher 						break;
830d38ceaf9SAlex Deucher 					}
831d38ceaf9SAlex Deucher 				}
832d38ceaf9SAlex Deucher 			}
833d38ceaf9SAlex Deucher 		}
83497d798b2SAlex Deucher 	}
83597d798b2SAlex Deucher #endif
836d38ceaf9SAlex Deucher 
837d38ceaf9SAlex Deucher 	if (atif->functions.sbios_requests && !atif->functions.system_params) {
838d38ceaf9SAlex Deucher 		/* XXX check this workraround, if sbios request function is
839d38ceaf9SAlex Deucher 		 * present we have to see how it's configured in the system
840d38ceaf9SAlex Deucher 		 * params
841d38ceaf9SAlex Deucher 		 */
842d38ceaf9SAlex Deucher 		atif->functions.system_params = true;
843d38ceaf9SAlex Deucher 	}
844d38ceaf9SAlex Deucher 
845d38ceaf9SAlex Deucher 	if (atif->functions.system_params) {
846280cf1a9SLyude Paul 		ret = amdgpu_atif_get_notification_params(atif);
847d38ceaf9SAlex Deucher 		if (ret) {
848d38ceaf9SAlex Deucher 			DRM_DEBUG_DRIVER("Call to GET_SYSTEM_PARAMS failed: %d\n",
849d38ceaf9SAlex Deucher 					ret);
850d38ceaf9SAlex Deucher 			/* Disable notification */
851d38ceaf9SAlex Deucher 			atif->notification_cfg.enabled = false;
852d38ceaf9SAlex Deucher 		}
853d38ceaf9SAlex Deucher 	}
854d38ceaf9SAlex Deucher 
855206bbafeSDavid Francis 	if (atif->functions.query_backlight_transfer_characteristics) {
856206bbafeSDavid Francis 		ret = amdgpu_atif_query_backlight_caps(atif);
857206bbafeSDavid Francis 		if (ret) {
858206bbafeSDavid Francis 			DRM_DEBUG_DRIVER("Call to QUERY_BACKLIGHT_TRANSFER_CHARACTERISTICS failed: %d\n",
859206bbafeSDavid Francis 					ret);
860206bbafeSDavid Francis 			atif->backlight_caps.caps_valid = false;
861206bbafeSDavid Francis 		}
862206bbafeSDavid Francis 	} else {
863206bbafeSDavid Francis 		atif->backlight_caps.caps_valid = false;
864206bbafeSDavid Francis 	}
865206bbafeSDavid Francis 
866d38ceaf9SAlex Deucher out:
867d38ceaf9SAlex Deucher 	adev->acpi_nb.notifier_call = amdgpu_acpi_event;
868d38ceaf9SAlex Deucher 	register_acpi_notifier(&adev->acpi_nb);
869d38ceaf9SAlex Deucher 
870d38ceaf9SAlex Deucher 	return ret;
871d38ceaf9SAlex Deucher }
872d38ceaf9SAlex Deucher 
873206bbafeSDavid Francis void amdgpu_acpi_get_backlight_caps(struct amdgpu_device *adev,
874206bbafeSDavid Francis 		struct amdgpu_dm_backlight_caps *caps)
875206bbafeSDavid Francis {
876206bbafeSDavid Francis 	if (!adev->atif) {
877206bbafeSDavid Francis 		caps->caps_valid = false;
878206bbafeSDavid Francis 		return;
879206bbafeSDavid Francis 	}
880206bbafeSDavid Francis 	caps->caps_valid = adev->atif->backlight_caps.caps_valid;
881206bbafeSDavid Francis 	caps->min_input_signal = adev->atif->backlight_caps.min_input_signal;
882206bbafeSDavid Francis 	caps->max_input_signal = adev->atif->backlight_caps.max_input_signal;
883206bbafeSDavid Francis }
884206bbafeSDavid Francis 
885d38ceaf9SAlex Deucher /**
886d38ceaf9SAlex Deucher  * amdgpu_acpi_fini - tear down driver acpi support
887d38ceaf9SAlex Deucher  *
888d38ceaf9SAlex Deucher  * @adev: amdgpu_device pointer
889d38ceaf9SAlex Deucher  *
890d38ceaf9SAlex Deucher  * Unregisters with the acpi notifier chain (all asics).
891d38ceaf9SAlex Deucher  */
892d38ceaf9SAlex Deucher void amdgpu_acpi_fini(struct amdgpu_device *adev)
893d38ceaf9SAlex Deucher {
894d38ceaf9SAlex Deucher 	unregister_acpi_notifier(&adev->acpi_nb);
895102c16a0SLyude Paul 	kfree(adev->atif);
896d38ceaf9SAlex Deucher }
897