xref: /linux/drivers/gpu/drm/amd/amdgpu/amdgpu_acpi.c (revision bdb1ccb080dafc1b4224873a5b759ff85a7d1c10)
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"
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;
67102c16a0SLyude Paul 	struct amdgpu_encoder *encoder_for_bl;
68206bbafeSDavid Francis 	struct amdgpu_dm_backlight_caps backlight_caps;
69102c16a0SLyude Paul };
70102c16a0SLyude Paul 
71d38ceaf9SAlex Deucher /* Call the ATIF method
72d38ceaf9SAlex Deucher  */
73d38ceaf9SAlex Deucher /**
74d38ceaf9SAlex Deucher  * amdgpu_atif_call - call an ATIF method
75d38ceaf9SAlex Deucher  *
76d38ceaf9SAlex Deucher  * @handle: acpi handle
77d38ceaf9SAlex Deucher  * @function: the ATIF function to execute
78d38ceaf9SAlex Deucher  * @params: ATIF function params
79d38ceaf9SAlex Deucher  *
80d38ceaf9SAlex Deucher  * Executes the requested ATIF function (all asics).
81d38ceaf9SAlex Deucher  * Returns a pointer to the acpi output buffer.
82d38ceaf9SAlex Deucher  */
83280cf1a9SLyude Paul static union acpi_object *amdgpu_atif_call(struct amdgpu_atif *atif,
84280cf1a9SLyude Paul 					   int function,
85d38ceaf9SAlex Deucher 					   struct acpi_buffer *params)
86d38ceaf9SAlex Deucher {
87d38ceaf9SAlex Deucher 	acpi_status status;
88d38ceaf9SAlex Deucher 	union acpi_object atif_arg_elements[2];
89d38ceaf9SAlex Deucher 	struct acpi_object_list atif_arg;
90d38ceaf9SAlex Deucher 	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
91d38ceaf9SAlex Deucher 
92d38ceaf9SAlex Deucher 	atif_arg.count = 2;
93d38ceaf9SAlex Deucher 	atif_arg.pointer = &atif_arg_elements[0];
94d38ceaf9SAlex Deucher 
95d38ceaf9SAlex Deucher 	atif_arg_elements[0].type = ACPI_TYPE_INTEGER;
96d38ceaf9SAlex Deucher 	atif_arg_elements[0].integer.value = function;
97d38ceaf9SAlex Deucher 
98d38ceaf9SAlex Deucher 	if (params) {
99d38ceaf9SAlex Deucher 		atif_arg_elements[1].type = ACPI_TYPE_BUFFER;
100d38ceaf9SAlex Deucher 		atif_arg_elements[1].buffer.length = params->length;
101d38ceaf9SAlex Deucher 		atif_arg_elements[1].buffer.pointer = params->pointer;
102d38ceaf9SAlex Deucher 	} else {
103d38ceaf9SAlex Deucher 		/* We need a second fake parameter */
104d38ceaf9SAlex Deucher 		atif_arg_elements[1].type = ACPI_TYPE_INTEGER;
105d38ceaf9SAlex Deucher 		atif_arg_elements[1].integer.value = 0;
106d38ceaf9SAlex Deucher 	}
107d38ceaf9SAlex Deucher 
108280cf1a9SLyude Paul 	status = acpi_evaluate_object(atif->handle, NULL, &atif_arg,
109280cf1a9SLyude Paul 				      &buffer);
110d38ceaf9SAlex Deucher 
111d38ceaf9SAlex Deucher 	/* Fail only if calling the method fails and ATIF is supported */
112d38ceaf9SAlex Deucher 	if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
113d38ceaf9SAlex Deucher 		DRM_DEBUG_DRIVER("failed to evaluate ATIF got %s\n",
114d38ceaf9SAlex Deucher 				 acpi_format_exception(status));
115d38ceaf9SAlex Deucher 		kfree(buffer.pointer);
116d38ceaf9SAlex Deucher 		return NULL;
117d38ceaf9SAlex Deucher 	}
118d38ceaf9SAlex Deucher 
119d38ceaf9SAlex Deucher 	return buffer.pointer;
120d38ceaf9SAlex Deucher }
121d38ceaf9SAlex Deucher 
122d38ceaf9SAlex Deucher /**
123d38ceaf9SAlex Deucher  * amdgpu_atif_parse_notification - parse supported notifications
124d38ceaf9SAlex Deucher  *
125d38ceaf9SAlex Deucher  * @n: supported notifications struct
126d38ceaf9SAlex Deucher  * @mask: supported notifications mask from ATIF
127d38ceaf9SAlex Deucher  *
128d38ceaf9SAlex Deucher  * Use the supported notifications mask from ATIF function
129d38ceaf9SAlex Deucher  * ATIF_FUNCTION_VERIFY_INTERFACE to determine what notifications
130d38ceaf9SAlex Deucher  * are supported (all asics).
131d38ceaf9SAlex Deucher  */
132d38ceaf9SAlex Deucher static void amdgpu_atif_parse_notification(struct amdgpu_atif_notifications *n, u32 mask)
133d38ceaf9SAlex Deucher {
134d38ceaf9SAlex Deucher 	n->thermal_state = mask & ATIF_THERMAL_STATE_CHANGE_REQUEST_SUPPORTED;
135d38ceaf9SAlex Deucher 	n->forced_power_state = mask & ATIF_FORCED_POWER_STATE_CHANGE_REQUEST_SUPPORTED;
136d38ceaf9SAlex Deucher 	n->system_power_state = mask & ATIF_SYSTEM_POWER_SOURCE_CHANGE_REQUEST_SUPPORTED;
137d38ceaf9SAlex Deucher 	n->brightness_change = mask & ATIF_PANEL_BRIGHTNESS_CHANGE_REQUEST_SUPPORTED;
138d38ceaf9SAlex Deucher 	n->dgpu_display_event = mask & ATIF_DGPU_DISPLAY_EVENT_SUPPORTED;
1397349a3afSDavid Francis 	n->gpu_package_power_limit = mask & ATIF_GPU_PACKAGE_POWER_LIMIT_REQUEST_SUPPORTED;
140d38ceaf9SAlex Deucher }
141d38ceaf9SAlex Deucher 
142d38ceaf9SAlex Deucher /**
143d38ceaf9SAlex Deucher  * amdgpu_atif_parse_functions - parse supported functions
144d38ceaf9SAlex Deucher  *
145d38ceaf9SAlex Deucher  * @f: supported functions struct
146d38ceaf9SAlex Deucher  * @mask: supported functions mask from ATIF
147d38ceaf9SAlex Deucher  *
148d38ceaf9SAlex Deucher  * Use the supported functions mask from ATIF function
149d38ceaf9SAlex Deucher  * ATIF_FUNCTION_VERIFY_INTERFACE to determine what functions
150d38ceaf9SAlex Deucher  * are supported (all asics).
151d38ceaf9SAlex Deucher  */
152d38ceaf9SAlex Deucher static void amdgpu_atif_parse_functions(struct amdgpu_atif_functions *f, u32 mask)
153d38ceaf9SAlex Deucher {
154d38ceaf9SAlex Deucher 	f->system_params = mask & ATIF_GET_SYSTEM_PARAMETERS_SUPPORTED;
155d38ceaf9SAlex Deucher 	f->sbios_requests = mask & ATIF_GET_SYSTEM_BIOS_REQUESTS_SUPPORTED;
156d38ceaf9SAlex Deucher 	f->temperature_change = mask & ATIF_TEMPERATURE_CHANGE_NOTIFICATION_SUPPORTED;
1577349a3afSDavid Francis 	f->query_backlight_transfer_characteristics =
1587349a3afSDavid Francis 		mask & ATIF_QUERY_BACKLIGHT_TRANSFER_CHARACTERISTICS_SUPPORTED;
1597349a3afSDavid Francis 	f->ready_to_undock = mask & ATIF_READY_TO_UNDOCK_NOTIFICATION_SUPPORTED;
1607349a3afSDavid Francis 	f->external_gpu_information = mask & ATIF_GET_EXTERNAL_GPU_INFORMATION_SUPPORTED;
161d38ceaf9SAlex Deucher }
162d38ceaf9SAlex Deucher 
163d38ceaf9SAlex Deucher /**
164d38ceaf9SAlex Deucher  * amdgpu_atif_verify_interface - verify ATIF
165d38ceaf9SAlex Deucher  *
166d38ceaf9SAlex Deucher  * @handle: acpi handle
167d38ceaf9SAlex Deucher  * @atif: amdgpu atif struct
168d38ceaf9SAlex Deucher  *
169d38ceaf9SAlex Deucher  * Execute the ATIF_FUNCTION_VERIFY_INTERFACE ATIF function
170d38ceaf9SAlex Deucher  * to initialize ATIF and determine what features are supported
171d38ceaf9SAlex Deucher  * (all asics).
172d38ceaf9SAlex Deucher  * returns 0 on success, error on failure.
173d38ceaf9SAlex Deucher  */
174280cf1a9SLyude Paul static int amdgpu_atif_verify_interface(struct amdgpu_atif *atif)
175d38ceaf9SAlex Deucher {
176d38ceaf9SAlex Deucher 	union acpi_object *info;
177d38ceaf9SAlex Deucher 	struct atif_verify_interface output;
178d38ceaf9SAlex Deucher 	size_t size;
179d38ceaf9SAlex Deucher 	int err = 0;
180d38ceaf9SAlex Deucher 
181280cf1a9SLyude Paul 	info = amdgpu_atif_call(atif, ATIF_FUNCTION_VERIFY_INTERFACE, NULL);
182d38ceaf9SAlex Deucher 	if (!info)
183d38ceaf9SAlex Deucher 		return -EIO;
184d38ceaf9SAlex Deucher 
185d38ceaf9SAlex Deucher 	memset(&output, 0, sizeof(output));
186d38ceaf9SAlex Deucher 
187d38ceaf9SAlex Deucher 	size = *(u16 *) info->buffer.pointer;
188d38ceaf9SAlex Deucher 	if (size < 12) {
189d38ceaf9SAlex Deucher 		DRM_INFO("ATIF buffer is too small: %zu\n", size);
190d38ceaf9SAlex Deucher 		err = -EINVAL;
191d38ceaf9SAlex Deucher 		goto out;
192d38ceaf9SAlex Deucher 	}
193d38ceaf9SAlex Deucher 	size = min(sizeof(output), size);
194d38ceaf9SAlex Deucher 
195d38ceaf9SAlex Deucher 	memcpy(&output, info->buffer.pointer, size);
196d38ceaf9SAlex Deucher 
197d38ceaf9SAlex Deucher 	/* TODO: check version? */
198d38ceaf9SAlex Deucher 	DRM_DEBUG_DRIVER("ATIF version %u\n", output.version);
199d38ceaf9SAlex Deucher 
200d38ceaf9SAlex Deucher 	amdgpu_atif_parse_notification(&atif->notifications, output.notification_mask);
201d38ceaf9SAlex Deucher 	amdgpu_atif_parse_functions(&atif->functions, output.function_bits);
202d38ceaf9SAlex Deucher 
203d38ceaf9SAlex Deucher out:
204d38ceaf9SAlex Deucher 	kfree(info);
205d38ceaf9SAlex Deucher 	return err;
206d38ceaf9SAlex Deucher }
207d38ceaf9SAlex Deucher 
208280cf1a9SLyude Paul static acpi_handle amdgpu_atif_probe_handle(acpi_handle dhandle)
209280cf1a9SLyude Paul {
210280cf1a9SLyude Paul 	acpi_handle handle = NULL;
211280cf1a9SLyude Paul 	char acpi_method_name[255] = { 0 };
212280cf1a9SLyude Paul 	struct acpi_buffer buffer = { sizeof(acpi_method_name), acpi_method_name };
213280cf1a9SLyude Paul 	acpi_status status;
214280cf1a9SLyude Paul 
215280cf1a9SLyude Paul 	/* For PX/HG systems, ATIF and ATPX are in the iGPU's namespace, on dGPU only
216280cf1a9SLyude Paul 	 * systems, ATIF is in the dGPU's namespace.
217280cf1a9SLyude Paul 	 */
218280cf1a9SLyude Paul 	status = acpi_get_handle(dhandle, "ATIF", &handle);
219280cf1a9SLyude Paul 	if (ACPI_SUCCESS(status))
220280cf1a9SLyude Paul 		goto out;
221280cf1a9SLyude Paul 
222280cf1a9SLyude Paul 	if (amdgpu_has_atpx()) {
223280cf1a9SLyude Paul 		status = acpi_get_handle(amdgpu_atpx_get_dhandle(), "ATIF",
224280cf1a9SLyude Paul 					 &handle);
225280cf1a9SLyude Paul 		if (ACPI_SUCCESS(status))
226280cf1a9SLyude Paul 			goto out;
227280cf1a9SLyude Paul 	}
228280cf1a9SLyude Paul 
229280cf1a9SLyude Paul 	DRM_DEBUG_DRIVER("No ATIF handle found\n");
230280cf1a9SLyude Paul 	return NULL;
231280cf1a9SLyude Paul out:
232280cf1a9SLyude Paul 	acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
233280cf1a9SLyude Paul 	DRM_DEBUG_DRIVER("Found ATIF handle %s\n", acpi_method_name);
234280cf1a9SLyude Paul 	return handle;
235280cf1a9SLyude Paul }
236280cf1a9SLyude Paul 
237d38ceaf9SAlex Deucher /**
238d38ceaf9SAlex Deucher  * amdgpu_atif_get_notification_params - determine notify configuration
239d38ceaf9SAlex Deucher  *
240d38ceaf9SAlex Deucher  * @handle: acpi handle
241d38ceaf9SAlex Deucher  * @n: atif notification configuration struct
242d38ceaf9SAlex Deucher  *
243d38ceaf9SAlex Deucher  * Execute the ATIF_FUNCTION_GET_SYSTEM_PARAMETERS ATIF function
244d38ceaf9SAlex Deucher  * to determine if a notifier is used and if so which one
245d38ceaf9SAlex Deucher  * (all asics).  This is either Notify(VGA, 0x81) or Notify(VGA, n)
246d38ceaf9SAlex Deucher  * where n is specified in the result if a notifier is used.
247d38ceaf9SAlex Deucher  * Returns 0 on success, error on failure.
248d38ceaf9SAlex Deucher  */
249280cf1a9SLyude Paul static int amdgpu_atif_get_notification_params(struct amdgpu_atif *atif)
250d38ceaf9SAlex Deucher {
251d38ceaf9SAlex Deucher 	union acpi_object *info;
252280cf1a9SLyude Paul 	struct amdgpu_atif_notification_cfg *n = &atif->notification_cfg;
253d38ceaf9SAlex Deucher 	struct atif_system_params params;
254d38ceaf9SAlex Deucher 	size_t size;
255d38ceaf9SAlex Deucher 	int err = 0;
256d38ceaf9SAlex Deucher 
257280cf1a9SLyude Paul 	info = amdgpu_atif_call(atif, ATIF_FUNCTION_GET_SYSTEM_PARAMETERS,
258280cf1a9SLyude Paul 				NULL);
259d38ceaf9SAlex Deucher 	if (!info) {
260d38ceaf9SAlex Deucher 		err = -EIO;
261d38ceaf9SAlex Deucher 		goto out;
262d38ceaf9SAlex Deucher 	}
263d38ceaf9SAlex Deucher 
264d38ceaf9SAlex Deucher 	size = *(u16 *) info->buffer.pointer;
265d38ceaf9SAlex Deucher 	if (size < 10) {
266d38ceaf9SAlex Deucher 		err = -EINVAL;
267d38ceaf9SAlex Deucher 		goto out;
268d38ceaf9SAlex Deucher 	}
269d38ceaf9SAlex Deucher 
270d38ceaf9SAlex Deucher 	memset(&params, 0, sizeof(params));
271d38ceaf9SAlex Deucher 	size = min(sizeof(params), size);
272d38ceaf9SAlex Deucher 	memcpy(&params, info->buffer.pointer, size);
273d38ceaf9SAlex Deucher 
274d38ceaf9SAlex Deucher 	DRM_DEBUG_DRIVER("SYSTEM_PARAMS: mask = %#x, flags = %#x\n",
275d38ceaf9SAlex Deucher 			params.flags, params.valid_mask);
276d38ceaf9SAlex Deucher 	params.flags = params.flags & params.valid_mask;
277d38ceaf9SAlex Deucher 
278d38ceaf9SAlex Deucher 	if ((params.flags & ATIF_NOTIFY_MASK) == ATIF_NOTIFY_NONE) {
279d38ceaf9SAlex Deucher 		n->enabled = false;
280d38ceaf9SAlex Deucher 		n->command_code = 0;
281d38ceaf9SAlex Deucher 	} else if ((params.flags & ATIF_NOTIFY_MASK) == ATIF_NOTIFY_81) {
282d38ceaf9SAlex Deucher 		n->enabled = true;
283d38ceaf9SAlex Deucher 		n->command_code = 0x81;
284d38ceaf9SAlex Deucher 	} else {
285d38ceaf9SAlex Deucher 		if (size < 11) {
286d38ceaf9SAlex Deucher 			err = -EINVAL;
287d38ceaf9SAlex Deucher 			goto out;
288d38ceaf9SAlex Deucher 		}
289d38ceaf9SAlex Deucher 		n->enabled = true;
290d38ceaf9SAlex Deucher 		n->command_code = params.command_code;
291d38ceaf9SAlex Deucher 	}
292d38ceaf9SAlex Deucher 
293d38ceaf9SAlex Deucher out:
294d38ceaf9SAlex Deucher 	DRM_DEBUG_DRIVER("Notification %s, command code = %#x\n",
295d38ceaf9SAlex Deucher 			(n->enabled ? "enabled" : "disabled"),
296d38ceaf9SAlex Deucher 			n->command_code);
297d38ceaf9SAlex Deucher 	kfree(info);
298d38ceaf9SAlex Deucher 	return err;
299d38ceaf9SAlex Deucher }
300d38ceaf9SAlex Deucher 
301d38ceaf9SAlex Deucher /**
302206bbafeSDavid Francis  * amdgpu_atif_query_backlight_caps - get min and max backlight input signal
303206bbafeSDavid Francis  *
304206bbafeSDavid Francis  * @handle: acpi handle
305206bbafeSDavid Francis  *
306206bbafeSDavid Francis  * Execute the QUERY_BRIGHTNESS_TRANSFER_CHARACTERISTICS ATIF function
307206bbafeSDavid Francis  * to determine the acceptable range of backlight values
308206bbafeSDavid Francis  *
309206bbafeSDavid Francis  * Backlight_caps.caps_valid will be set to true if the query is successful
310206bbafeSDavid Francis  *
311206bbafeSDavid Francis  * The input signals are in range 0-255
312206bbafeSDavid Francis  *
313206bbafeSDavid Francis  * This function assumes the display with backlight is the first LCD
314206bbafeSDavid Francis  *
315206bbafeSDavid Francis  * Returns 0 on success, error on failure.
316206bbafeSDavid Francis  */
317206bbafeSDavid Francis static int amdgpu_atif_query_backlight_caps(struct amdgpu_atif *atif)
318206bbafeSDavid Francis {
319206bbafeSDavid Francis 	union acpi_object *info;
320206bbafeSDavid Francis 	struct atif_qbtc_output characteristics;
321206bbafeSDavid Francis 	struct atif_qbtc_arguments arguments;
322206bbafeSDavid Francis 	struct acpi_buffer params;
323206bbafeSDavid Francis 	size_t size;
324206bbafeSDavid Francis 	int err = 0;
325206bbafeSDavid Francis 
326206bbafeSDavid Francis 	arguments.size = sizeof(arguments);
327206bbafeSDavid Francis 	arguments.requested_display = ATIF_QBTC_REQUEST_LCD1;
328206bbafeSDavid Francis 
329206bbafeSDavid Francis 	params.length = sizeof(arguments);
330206bbafeSDavid Francis 	params.pointer = (void *)&arguments;
331206bbafeSDavid Francis 
332206bbafeSDavid Francis 	info = amdgpu_atif_call(atif,
333206bbafeSDavid Francis 		ATIF_FUNCTION_QUERY_BRIGHTNESS_TRANSFER_CHARACTERISTICS,
334206bbafeSDavid Francis 		&params);
335206bbafeSDavid Francis 	if (!info) {
336206bbafeSDavid Francis 		err = -EIO;
337206bbafeSDavid Francis 		goto out;
338206bbafeSDavid Francis 	}
339206bbafeSDavid Francis 
340206bbafeSDavid Francis 	size = *(u16 *) info->buffer.pointer;
341206bbafeSDavid Francis 	if (size < 10) {
342206bbafeSDavid Francis 		err = -EINVAL;
343206bbafeSDavid Francis 		goto out;
344206bbafeSDavid Francis 	}
345206bbafeSDavid Francis 
346206bbafeSDavid Francis 	memset(&characteristics, 0, sizeof(characteristics));
347206bbafeSDavid Francis 	size = min(sizeof(characteristics), size);
348206bbafeSDavid Francis 	memcpy(&characteristics, info->buffer.pointer, size);
349206bbafeSDavid Francis 
350206bbafeSDavid Francis 	atif->backlight_caps.caps_valid = true;
351206bbafeSDavid Francis 	atif->backlight_caps.min_input_signal =
352206bbafeSDavid Francis 			characteristics.min_input_signal;
353206bbafeSDavid Francis 	atif->backlight_caps.max_input_signal =
354206bbafeSDavid Francis 			characteristics.max_input_signal;
355206bbafeSDavid Francis out:
356206bbafeSDavid Francis 	kfree(info);
357206bbafeSDavid Francis 	return err;
358206bbafeSDavid Francis }
359206bbafeSDavid Francis 
360206bbafeSDavid Francis /**
361d38ceaf9SAlex Deucher  * amdgpu_atif_get_sbios_requests - get requested sbios event
362d38ceaf9SAlex Deucher  *
363d38ceaf9SAlex Deucher  * @handle: acpi handle
364d38ceaf9SAlex Deucher  * @req: atif sbios request struct
365d38ceaf9SAlex Deucher  *
366d38ceaf9SAlex Deucher  * Execute the ATIF_FUNCTION_GET_SYSTEM_BIOS_REQUESTS ATIF function
367d38ceaf9SAlex Deucher  * to determine what requests the sbios is making to the driver
368d38ceaf9SAlex Deucher  * (all asics).
369d38ceaf9SAlex Deucher  * Returns 0 on success, error on failure.
370d38ceaf9SAlex Deucher  */
371280cf1a9SLyude Paul static int amdgpu_atif_get_sbios_requests(struct amdgpu_atif *atif,
372d38ceaf9SAlex Deucher 					  struct atif_sbios_requests *req)
373d38ceaf9SAlex Deucher {
374d38ceaf9SAlex Deucher 	union acpi_object *info;
375d38ceaf9SAlex Deucher 	size_t size;
376d38ceaf9SAlex Deucher 	int count = 0;
377d38ceaf9SAlex Deucher 
378280cf1a9SLyude Paul 	info = amdgpu_atif_call(atif, ATIF_FUNCTION_GET_SYSTEM_BIOS_REQUESTS,
379280cf1a9SLyude Paul 				NULL);
380d38ceaf9SAlex Deucher 	if (!info)
381d38ceaf9SAlex Deucher 		return -EIO;
382d38ceaf9SAlex Deucher 
383d38ceaf9SAlex Deucher 	size = *(u16 *)info->buffer.pointer;
384d38ceaf9SAlex Deucher 	if (size < 0xd) {
385d38ceaf9SAlex Deucher 		count = -EINVAL;
386d38ceaf9SAlex Deucher 		goto out;
387d38ceaf9SAlex Deucher 	}
388d38ceaf9SAlex Deucher 	memset(req, 0, sizeof(*req));
389d38ceaf9SAlex Deucher 
390d38ceaf9SAlex Deucher 	size = min(sizeof(*req), size);
391d38ceaf9SAlex Deucher 	memcpy(req, info->buffer.pointer, size);
392d38ceaf9SAlex Deucher 	DRM_DEBUG_DRIVER("SBIOS pending requests: %#x\n", req->pending);
393d38ceaf9SAlex Deucher 
394d38ceaf9SAlex Deucher 	count = hweight32(req->pending);
395d38ceaf9SAlex Deucher 
396d38ceaf9SAlex Deucher out:
397d38ceaf9SAlex Deucher 	kfree(info);
398d38ceaf9SAlex Deucher 	return count;
399d38ceaf9SAlex Deucher }
400d38ceaf9SAlex Deucher 
401d38ceaf9SAlex Deucher /**
402d38ceaf9SAlex Deucher  * amdgpu_atif_handler - handle ATIF notify requests
403d38ceaf9SAlex Deucher  *
404d38ceaf9SAlex Deucher  * @adev: amdgpu_device pointer
405d38ceaf9SAlex Deucher  * @event: atif sbios request struct
406d38ceaf9SAlex Deucher  *
407d38ceaf9SAlex Deucher  * Checks the acpi event and if it matches an atif event,
408d38ceaf9SAlex Deucher  * handles it.
409582f58deSLyude Paul  *
410582f58deSLyude Paul  * Returns:
411582f58deSLyude Paul  * NOTIFY_BAD or NOTIFY_DONE, depending on the event.
412d38ceaf9SAlex Deucher  */
413b7cecbe8SJean Delvare static int amdgpu_atif_handler(struct amdgpu_device *adev,
414d38ceaf9SAlex Deucher 			       struct acpi_bus_event *event)
415d38ceaf9SAlex Deucher {
416102c16a0SLyude Paul 	struct amdgpu_atif *atif = adev->atif;
417d38ceaf9SAlex Deucher 	int count;
418d38ceaf9SAlex Deucher 
419d38ceaf9SAlex Deucher 	DRM_DEBUG_DRIVER("event, device_class = %s, type = %#x\n",
420d38ceaf9SAlex Deucher 			event->device_class, event->type);
421d38ceaf9SAlex Deucher 
422d38ceaf9SAlex Deucher 	if (strcmp(event->device_class, ACPI_VIDEO_CLASS) != 0)
423d38ceaf9SAlex Deucher 		return NOTIFY_DONE;
424d38ceaf9SAlex Deucher 
425582f58deSLyude Paul 	/* Is this actually our event? */
426102c16a0SLyude Paul 	if (!atif ||
427102c16a0SLyude Paul 	    !atif->notification_cfg.enabled ||
428582f58deSLyude Paul 	    event->type != atif->notification_cfg.command_code) {
429582f58deSLyude Paul 		/* These events will generate keypresses otherwise */
430582f58deSLyude Paul 		if (event->type == ACPI_VIDEO_NOTIFY_PROBE)
431582f58deSLyude Paul 			return NOTIFY_BAD;
432582f58deSLyude Paul 		else
433d38ceaf9SAlex Deucher 			return NOTIFY_DONE;
434582f58deSLyude Paul 	}
435d38ceaf9SAlex Deucher 
4369e7204beSAlex Deucher 	if (atif->functions.sbios_requests) {
4379e7204beSAlex Deucher 		struct atif_sbios_requests req;
4389e7204beSAlex Deucher 
439d38ceaf9SAlex Deucher 		/* Check pending SBIOS requests */
440280cf1a9SLyude Paul 		count = amdgpu_atif_get_sbios_requests(atif, &req);
441d38ceaf9SAlex Deucher 
442d38ceaf9SAlex Deucher 		if (count <= 0)
443582f58deSLyude Paul 			return NOTIFY_BAD;
444d38ceaf9SAlex Deucher 
445d38ceaf9SAlex Deucher 		DRM_DEBUG_DRIVER("ATIF: %d pending SBIOS requests\n", count);
446d38ceaf9SAlex Deucher 
4475b8eb0edSAlex Deucher 		/* todo: add DC handling */
4485b8eb0edSAlex Deucher 		if ((req.pending & ATIF_PANEL_BRIGHTNESS_CHANGE_REQUEST) &&
4495b8eb0edSAlex Deucher 		    !amdgpu_device_has_dc_support(adev)) {
450d38ceaf9SAlex Deucher 			struct amdgpu_encoder *enc = atif->encoder_for_bl;
451d38ceaf9SAlex Deucher 
452d38ceaf9SAlex Deucher 			if (enc) {
453d38ceaf9SAlex Deucher 				struct amdgpu_encoder_atom_dig *dig = enc->enc_priv;
454d38ceaf9SAlex Deucher 
455d38ceaf9SAlex Deucher 				DRM_DEBUG_DRIVER("Changing brightness to %d\n",
456d38ceaf9SAlex Deucher 						 req.backlight_level);
457d38ceaf9SAlex Deucher 
458d38ceaf9SAlex Deucher 				amdgpu_display_backlight_set_level(adev, enc, req.backlight_level);
459d38ceaf9SAlex Deucher 
460d38ceaf9SAlex Deucher #if defined(CONFIG_BACKLIGHT_CLASS_DEVICE) || defined(CONFIG_BACKLIGHT_CLASS_DEVICE_MODULE)
461d38ceaf9SAlex Deucher 				backlight_force_update(dig->bl_dev,
462d38ceaf9SAlex Deucher 						       BACKLIGHT_UPDATE_HOTKEY);
463d38ceaf9SAlex Deucher #endif
464d38ceaf9SAlex Deucher 			}
465d38ceaf9SAlex Deucher 		}
4661b0f568dSAlex Deucher 		if (req.pending & ATIF_DGPU_DISPLAY_EVENT) {
467*bdb1ccb0SAaron Liu 			if (adev->flags & AMD_IS_PX) {
4681b0f568dSAlex Deucher 				pm_runtime_get_sync(adev->ddev->dev);
4691b0f568dSAlex Deucher 				/* Just fire off a uevent and let userspace tell us what to do */
4701b0f568dSAlex Deucher 				drm_helper_hpd_irq_event(adev->ddev);
4711b0f568dSAlex Deucher 				pm_runtime_mark_last_busy(adev->ddev->dev);
4721b0f568dSAlex Deucher 				pm_runtime_put_autosuspend(adev->ddev->dev);
4731b0f568dSAlex Deucher 			}
4741b0f568dSAlex Deucher 		}
475d38ceaf9SAlex Deucher 		/* TODO: check other events */
4769e7204beSAlex Deucher 	}
477d38ceaf9SAlex Deucher 
478d38ceaf9SAlex Deucher 	/* We've handled the event, stop the notifier chain. The ACPI interface
479d38ceaf9SAlex Deucher 	 * overloads ACPI_VIDEO_NOTIFY_PROBE, we don't want to send that to
480d38ceaf9SAlex Deucher 	 * userspace if the event was generated only to signal a SBIOS
481d38ceaf9SAlex Deucher 	 * request.
482d38ceaf9SAlex Deucher 	 */
483d38ceaf9SAlex Deucher 	return NOTIFY_BAD;
484d38ceaf9SAlex Deucher }
485d38ceaf9SAlex Deucher 
486d38ceaf9SAlex Deucher /* Call the ATCS method
487d38ceaf9SAlex Deucher  */
488d38ceaf9SAlex Deucher /**
489d38ceaf9SAlex Deucher  * amdgpu_atcs_call - call an ATCS method
490d38ceaf9SAlex Deucher  *
491d38ceaf9SAlex Deucher  * @handle: acpi handle
492d38ceaf9SAlex Deucher  * @function: the ATCS function to execute
493d38ceaf9SAlex Deucher  * @params: ATCS function params
494d38ceaf9SAlex Deucher  *
495d38ceaf9SAlex Deucher  * Executes the requested ATCS function (all asics).
496d38ceaf9SAlex Deucher  * Returns a pointer to the acpi output buffer.
497d38ceaf9SAlex Deucher  */
498d38ceaf9SAlex Deucher static union acpi_object *amdgpu_atcs_call(acpi_handle handle, int function,
499d38ceaf9SAlex Deucher 					   struct acpi_buffer *params)
500d38ceaf9SAlex Deucher {
501d38ceaf9SAlex Deucher 	acpi_status status;
502d38ceaf9SAlex Deucher 	union acpi_object atcs_arg_elements[2];
503d38ceaf9SAlex Deucher 	struct acpi_object_list atcs_arg;
504d38ceaf9SAlex Deucher 	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
505d38ceaf9SAlex Deucher 
506d38ceaf9SAlex Deucher 	atcs_arg.count = 2;
507d38ceaf9SAlex Deucher 	atcs_arg.pointer = &atcs_arg_elements[0];
508d38ceaf9SAlex Deucher 
509d38ceaf9SAlex Deucher 	atcs_arg_elements[0].type = ACPI_TYPE_INTEGER;
510d38ceaf9SAlex Deucher 	atcs_arg_elements[0].integer.value = function;
511d38ceaf9SAlex Deucher 
512d38ceaf9SAlex Deucher 	if (params) {
513d38ceaf9SAlex Deucher 		atcs_arg_elements[1].type = ACPI_TYPE_BUFFER;
514d38ceaf9SAlex Deucher 		atcs_arg_elements[1].buffer.length = params->length;
515d38ceaf9SAlex Deucher 		atcs_arg_elements[1].buffer.pointer = params->pointer;
516d38ceaf9SAlex Deucher 	} else {
517d38ceaf9SAlex Deucher 		/* We need a second fake parameter */
518d38ceaf9SAlex Deucher 		atcs_arg_elements[1].type = ACPI_TYPE_INTEGER;
519d38ceaf9SAlex Deucher 		atcs_arg_elements[1].integer.value = 0;
520d38ceaf9SAlex Deucher 	}
521d38ceaf9SAlex Deucher 
522d38ceaf9SAlex Deucher 	status = acpi_evaluate_object(handle, "ATCS", &atcs_arg, &buffer);
523d38ceaf9SAlex Deucher 
524d38ceaf9SAlex Deucher 	/* Fail only if calling the method fails and ATIF is supported */
525d38ceaf9SAlex Deucher 	if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
526d38ceaf9SAlex Deucher 		DRM_DEBUG_DRIVER("failed to evaluate ATCS got %s\n",
527d38ceaf9SAlex Deucher 				 acpi_format_exception(status));
528d38ceaf9SAlex Deucher 		kfree(buffer.pointer);
529d38ceaf9SAlex Deucher 		return NULL;
530d38ceaf9SAlex Deucher 	}
531d38ceaf9SAlex Deucher 
532d38ceaf9SAlex Deucher 	return buffer.pointer;
533d38ceaf9SAlex Deucher }
534d38ceaf9SAlex Deucher 
535d38ceaf9SAlex Deucher /**
536d38ceaf9SAlex Deucher  * amdgpu_atcs_parse_functions - parse supported functions
537d38ceaf9SAlex Deucher  *
538d38ceaf9SAlex Deucher  * @f: supported functions struct
539d38ceaf9SAlex Deucher  * @mask: supported functions mask from ATCS
540d38ceaf9SAlex Deucher  *
541d38ceaf9SAlex Deucher  * Use the supported functions mask from ATCS function
542d38ceaf9SAlex Deucher  * ATCS_FUNCTION_VERIFY_INTERFACE to determine what functions
543d38ceaf9SAlex Deucher  * are supported (all asics).
544d38ceaf9SAlex Deucher  */
545d38ceaf9SAlex Deucher static void amdgpu_atcs_parse_functions(struct amdgpu_atcs_functions *f, u32 mask)
546d38ceaf9SAlex Deucher {
547d38ceaf9SAlex Deucher 	f->get_ext_state = mask & ATCS_GET_EXTERNAL_STATE_SUPPORTED;
548d38ceaf9SAlex Deucher 	f->pcie_perf_req = mask & ATCS_PCIE_PERFORMANCE_REQUEST_SUPPORTED;
549d38ceaf9SAlex Deucher 	f->pcie_dev_rdy = mask & ATCS_PCIE_DEVICE_READY_NOTIFICATION_SUPPORTED;
550d38ceaf9SAlex Deucher 	f->pcie_bus_width = mask & ATCS_SET_PCIE_BUS_WIDTH_SUPPORTED;
551d38ceaf9SAlex Deucher }
552d38ceaf9SAlex Deucher 
553d38ceaf9SAlex Deucher /**
554d38ceaf9SAlex Deucher  * amdgpu_atcs_verify_interface - verify ATCS
555d38ceaf9SAlex Deucher  *
556d38ceaf9SAlex Deucher  * @handle: acpi handle
557d38ceaf9SAlex Deucher  * @atcs: amdgpu atcs struct
558d38ceaf9SAlex Deucher  *
559d38ceaf9SAlex Deucher  * Execute the ATCS_FUNCTION_VERIFY_INTERFACE ATCS function
560d38ceaf9SAlex Deucher  * to initialize ATCS and determine what features are supported
561d38ceaf9SAlex Deucher  * (all asics).
562d38ceaf9SAlex Deucher  * returns 0 on success, error on failure.
563d38ceaf9SAlex Deucher  */
564d38ceaf9SAlex Deucher static int amdgpu_atcs_verify_interface(acpi_handle handle,
565d38ceaf9SAlex Deucher 					struct amdgpu_atcs *atcs)
566d38ceaf9SAlex Deucher {
567d38ceaf9SAlex Deucher 	union acpi_object *info;
568d38ceaf9SAlex Deucher 	struct atcs_verify_interface output;
569d38ceaf9SAlex Deucher 	size_t size;
570d38ceaf9SAlex Deucher 	int err = 0;
571d38ceaf9SAlex Deucher 
572d38ceaf9SAlex Deucher 	info = amdgpu_atcs_call(handle, ATCS_FUNCTION_VERIFY_INTERFACE, NULL);
573d38ceaf9SAlex Deucher 	if (!info)
574d38ceaf9SAlex Deucher 		return -EIO;
575d38ceaf9SAlex Deucher 
576d38ceaf9SAlex Deucher 	memset(&output, 0, sizeof(output));
577d38ceaf9SAlex Deucher 
578d38ceaf9SAlex Deucher 	size = *(u16 *) info->buffer.pointer;
579d38ceaf9SAlex Deucher 	if (size < 8) {
580d38ceaf9SAlex Deucher 		DRM_INFO("ATCS buffer is too small: %zu\n", size);
581d38ceaf9SAlex Deucher 		err = -EINVAL;
582d38ceaf9SAlex Deucher 		goto out;
583d38ceaf9SAlex Deucher 	}
584d38ceaf9SAlex Deucher 	size = min(sizeof(output), size);
585d38ceaf9SAlex Deucher 
586d38ceaf9SAlex Deucher 	memcpy(&output, info->buffer.pointer, size);
587d38ceaf9SAlex Deucher 
588d38ceaf9SAlex Deucher 	/* TODO: check version? */
589d38ceaf9SAlex Deucher 	DRM_DEBUG_DRIVER("ATCS version %u\n", output.version);
590d38ceaf9SAlex Deucher 
591d38ceaf9SAlex Deucher 	amdgpu_atcs_parse_functions(&atcs->functions, output.function_bits);
592d38ceaf9SAlex Deucher 
593d38ceaf9SAlex Deucher out:
594d38ceaf9SAlex Deucher 	kfree(info);
595d38ceaf9SAlex Deucher 	return err;
596d38ceaf9SAlex Deucher }
597d38ceaf9SAlex Deucher 
598d38ceaf9SAlex Deucher /**
599d38ceaf9SAlex Deucher  * amdgpu_acpi_is_pcie_performance_request_supported
600d38ceaf9SAlex Deucher  *
601d38ceaf9SAlex Deucher  * @adev: amdgpu_device pointer
602d38ceaf9SAlex Deucher  *
603d38ceaf9SAlex Deucher  * Check if the ATCS pcie_perf_req and pcie_dev_rdy methods
604d38ceaf9SAlex Deucher  * are supported (all asics).
605d38ceaf9SAlex Deucher  * returns true if supported, false if not.
606d38ceaf9SAlex Deucher  */
607d38ceaf9SAlex Deucher bool amdgpu_acpi_is_pcie_performance_request_supported(struct amdgpu_device *adev)
608d38ceaf9SAlex Deucher {
609d38ceaf9SAlex Deucher 	struct amdgpu_atcs *atcs = &adev->atcs;
610d38ceaf9SAlex Deucher 
611d38ceaf9SAlex Deucher 	if (atcs->functions.pcie_perf_req && atcs->functions.pcie_dev_rdy)
612d38ceaf9SAlex Deucher 		return true;
613d38ceaf9SAlex Deucher 
614d38ceaf9SAlex Deucher 	return false;
615d38ceaf9SAlex Deucher }
616d38ceaf9SAlex Deucher 
617d38ceaf9SAlex Deucher /**
618d38ceaf9SAlex Deucher  * amdgpu_acpi_pcie_notify_device_ready
619d38ceaf9SAlex Deucher  *
620d38ceaf9SAlex Deucher  * @adev: amdgpu_device pointer
621d38ceaf9SAlex Deucher  *
622d38ceaf9SAlex Deucher  * Executes the PCIE_DEVICE_READY_NOTIFICATION method
623d38ceaf9SAlex Deucher  * (all asics).
624d38ceaf9SAlex Deucher  * returns 0 on success, error on failure.
625d38ceaf9SAlex Deucher  */
626d38ceaf9SAlex Deucher int amdgpu_acpi_pcie_notify_device_ready(struct amdgpu_device *adev)
627d38ceaf9SAlex Deucher {
628d38ceaf9SAlex Deucher 	acpi_handle handle;
629d38ceaf9SAlex Deucher 	union acpi_object *info;
630d38ceaf9SAlex Deucher 	struct amdgpu_atcs *atcs = &adev->atcs;
631d38ceaf9SAlex Deucher 
632d38ceaf9SAlex Deucher 	/* Get the device handle */
633d38ceaf9SAlex Deucher 	handle = ACPI_HANDLE(&adev->pdev->dev);
634d38ceaf9SAlex Deucher 	if (!handle)
635d38ceaf9SAlex Deucher 		return -EINVAL;
636d38ceaf9SAlex Deucher 
637d38ceaf9SAlex Deucher 	if (!atcs->functions.pcie_dev_rdy)
638d38ceaf9SAlex Deucher 		return -EINVAL;
639d38ceaf9SAlex Deucher 
640d38ceaf9SAlex Deucher 	info = amdgpu_atcs_call(handle, ATCS_FUNCTION_PCIE_DEVICE_READY_NOTIFICATION, NULL);
641d38ceaf9SAlex Deucher 	if (!info)
642d38ceaf9SAlex Deucher 		return -EIO;
643d38ceaf9SAlex Deucher 
644d38ceaf9SAlex Deucher 	kfree(info);
645d38ceaf9SAlex Deucher 
646d38ceaf9SAlex Deucher 	return 0;
647d38ceaf9SAlex Deucher }
648d38ceaf9SAlex Deucher 
649d38ceaf9SAlex Deucher /**
650d38ceaf9SAlex Deucher  * amdgpu_acpi_pcie_performance_request
651d38ceaf9SAlex Deucher  *
652d38ceaf9SAlex Deucher  * @adev: amdgpu_device pointer
653d38ceaf9SAlex Deucher  * @perf_req: requested perf level (pcie gen speed)
654d38ceaf9SAlex Deucher  * @advertise: set advertise caps flag if set
655d38ceaf9SAlex Deucher  *
656d38ceaf9SAlex Deucher  * Executes the PCIE_PERFORMANCE_REQUEST method to
657d38ceaf9SAlex Deucher  * change the pcie gen speed (all asics).
658d38ceaf9SAlex Deucher  * returns 0 on success, error on failure.
659d38ceaf9SAlex Deucher  */
660d38ceaf9SAlex Deucher int amdgpu_acpi_pcie_performance_request(struct amdgpu_device *adev,
661d38ceaf9SAlex Deucher 					 u8 perf_req, bool advertise)
662d38ceaf9SAlex Deucher {
663d38ceaf9SAlex Deucher 	acpi_handle handle;
664d38ceaf9SAlex Deucher 	union acpi_object *info;
665d38ceaf9SAlex Deucher 	struct amdgpu_atcs *atcs = &adev->atcs;
666d38ceaf9SAlex Deucher 	struct atcs_pref_req_input atcs_input;
667d38ceaf9SAlex Deucher 	struct atcs_pref_req_output atcs_output;
668d38ceaf9SAlex Deucher 	struct acpi_buffer params;
669d38ceaf9SAlex Deucher 	size_t size;
670d38ceaf9SAlex Deucher 	u32 retry = 3;
671d38ceaf9SAlex Deucher 
6721bced75fSRex Zhu 	if (amdgpu_acpi_pcie_notify_device_ready(adev))
6731bced75fSRex Zhu 		return -EINVAL;
6741bced75fSRex Zhu 
675d38ceaf9SAlex Deucher 	/* Get the device handle */
676d38ceaf9SAlex Deucher 	handle = ACPI_HANDLE(&adev->pdev->dev);
677d38ceaf9SAlex Deucher 	if (!handle)
678d38ceaf9SAlex Deucher 		return -EINVAL;
679d38ceaf9SAlex Deucher 
680d38ceaf9SAlex Deucher 	if (!atcs->functions.pcie_perf_req)
681d38ceaf9SAlex Deucher 		return -EINVAL;
682d38ceaf9SAlex Deucher 
683d38ceaf9SAlex Deucher 	atcs_input.size = sizeof(struct atcs_pref_req_input);
684d38ceaf9SAlex Deucher 	/* client id (bit 2-0: func num, 7-3: dev num, 15-8: bus num) */
685d38ceaf9SAlex Deucher 	atcs_input.client_id = adev->pdev->devfn | (adev->pdev->bus->number << 8);
686d38ceaf9SAlex Deucher 	atcs_input.valid_flags_mask = ATCS_VALID_FLAGS_MASK;
687d38ceaf9SAlex Deucher 	atcs_input.flags = ATCS_WAIT_FOR_COMPLETION;
688d38ceaf9SAlex Deucher 	if (advertise)
689d38ceaf9SAlex Deucher 		atcs_input.flags |= ATCS_ADVERTISE_CAPS;
690d38ceaf9SAlex Deucher 	atcs_input.req_type = ATCS_PCIE_LINK_SPEED;
691d38ceaf9SAlex Deucher 	atcs_input.perf_req = perf_req;
692d38ceaf9SAlex Deucher 
693d38ceaf9SAlex Deucher 	params.length = sizeof(struct atcs_pref_req_input);
694d38ceaf9SAlex Deucher 	params.pointer = &atcs_input;
695d38ceaf9SAlex Deucher 
696d38ceaf9SAlex Deucher 	while (retry--) {
697d38ceaf9SAlex Deucher 		info = amdgpu_atcs_call(handle, ATCS_FUNCTION_PCIE_PERFORMANCE_REQUEST, &params);
698d38ceaf9SAlex Deucher 		if (!info)
699d38ceaf9SAlex Deucher 			return -EIO;
700d38ceaf9SAlex Deucher 
701d38ceaf9SAlex Deucher 		memset(&atcs_output, 0, sizeof(atcs_output));
702d38ceaf9SAlex Deucher 
703d38ceaf9SAlex Deucher 		size = *(u16 *) info->buffer.pointer;
704d38ceaf9SAlex Deucher 		if (size < 3) {
705d38ceaf9SAlex Deucher 			DRM_INFO("ATCS buffer is too small: %zu\n", size);
706d38ceaf9SAlex Deucher 			kfree(info);
707d38ceaf9SAlex Deucher 			return -EINVAL;
708d38ceaf9SAlex Deucher 		}
709d38ceaf9SAlex Deucher 		size = min(sizeof(atcs_output), size);
710d38ceaf9SAlex Deucher 
711d38ceaf9SAlex Deucher 		memcpy(&atcs_output, info->buffer.pointer, size);
712d38ceaf9SAlex Deucher 
713d38ceaf9SAlex Deucher 		kfree(info);
714d38ceaf9SAlex Deucher 
715d38ceaf9SAlex Deucher 		switch (atcs_output.ret_val) {
716d38ceaf9SAlex Deucher 		case ATCS_REQUEST_REFUSED:
717d38ceaf9SAlex Deucher 		default:
718d38ceaf9SAlex Deucher 			return -EINVAL;
719d38ceaf9SAlex Deucher 		case ATCS_REQUEST_COMPLETE:
720d38ceaf9SAlex Deucher 			return 0;
721d38ceaf9SAlex Deucher 		case ATCS_REQUEST_IN_PROGRESS:
722d38ceaf9SAlex Deucher 			udelay(10);
723d38ceaf9SAlex Deucher 			break;
724d38ceaf9SAlex Deucher 		}
725d38ceaf9SAlex Deucher 	}
726d38ceaf9SAlex Deucher 
727d38ceaf9SAlex Deucher 	return 0;
728d38ceaf9SAlex Deucher }
729d38ceaf9SAlex Deucher 
730d38ceaf9SAlex Deucher /**
731d38ceaf9SAlex Deucher  * amdgpu_acpi_event - handle notify events
732d38ceaf9SAlex Deucher  *
733d38ceaf9SAlex Deucher  * @nb: notifier block
734d38ceaf9SAlex Deucher  * @val: val
735d38ceaf9SAlex Deucher  * @data: acpi event
736d38ceaf9SAlex Deucher  *
737d38ceaf9SAlex Deucher  * Calls relevant amdgpu functions in response to various
738d38ceaf9SAlex Deucher  * acpi events.
739d38ceaf9SAlex Deucher  * Returns NOTIFY code
740d38ceaf9SAlex Deucher  */
741d38ceaf9SAlex Deucher static int amdgpu_acpi_event(struct notifier_block *nb,
742d38ceaf9SAlex Deucher 			     unsigned long val,
743d38ceaf9SAlex Deucher 			     void *data)
744d38ceaf9SAlex Deucher {
745d38ceaf9SAlex Deucher 	struct amdgpu_device *adev = container_of(nb, struct amdgpu_device, acpi_nb);
746d38ceaf9SAlex Deucher 	struct acpi_bus_event *entry = (struct acpi_bus_event *)data;
747d38ceaf9SAlex Deucher 
748d38ceaf9SAlex Deucher 	if (strcmp(entry->device_class, ACPI_AC_CLASS) == 0) {
749d38ceaf9SAlex Deucher 		if (power_supply_is_system_supplied() > 0)
750d38ceaf9SAlex Deucher 			DRM_DEBUG_DRIVER("pm: AC\n");
751d38ceaf9SAlex Deucher 		else
752d38ceaf9SAlex Deucher 			DRM_DEBUG_DRIVER("pm: DC\n");
753d38ceaf9SAlex Deucher 
754d38ceaf9SAlex Deucher 		amdgpu_pm_acpi_event_handler(adev);
755d38ceaf9SAlex Deucher 	}
756d38ceaf9SAlex Deucher 
757d38ceaf9SAlex Deucher 	/* Check for pending SBIOS requests */
758d38ceaf9SAlex Deucher 	return amdgpu_atif_handler(adev, entry);
759d38ceaf9SAlex Deucher }
760d38ceaf9SAlex Deucher 
761d38ceaf9SAlex Deucher /* Call all ACPI methods here */
762d38ceaf9SAlex Deucher /**
763d38ceaf9SAlex Deucher  * amdgpu_acpi_init - init driver acpi support
764d38ceaf9SAlex Deucher  *
765d38ceaf9SAlex Deucher  * @adev: amdgpu_device pointer
766d38ceaf9SAlex Deucher  *
767d38ceaf9SAlex Deucher  * Verifies the AMD ACPI interfaces and registers with the acpi
768d38ceaf9SAlex Deucher  * notifier chain (all asics).
769d38ceaf9SAlex Deucher  * Returns 0 on success, error on failure.
770d38ceaf9SAlex Deucher  */
771d38ceaf9SAlex Deucher int amdgpu_acpi_init(struct amdgpu_device *adev)
772d38ceaf9SAlex Deucher {
773280cf1a9SLyude Paul 	acpi_handle handle, atif_handle;
774102c16a0SLyude Paul 	struct amdgpu_atif *atif;
775d38ceaf9SAlex Deucher 	struct amdgpu_atcs *atcs = &adev->atcs;
776d38ceaf9SAlex Deucher 	int ret;
777d38ceaf9SAlex Deucher 
778d38ceaf9SAlex Deucher 	/* Get the device handle */
779d38ceaf9SAlex Deucher 	handle = ACPI_HANDLE(&adev->pdev->dev);
780d38ceaf9SAlex Deucher 
781d38ceaf9SAlex Deucher 	if (!adev->bios || !handle)
782d38ceaf9SAlex Deucher 		return 0;
783d38ceaf9SAlex Deucher 
784d38ceaf9SAlex Deucher 	/* Call the ATCS method */
785d38ceaf9SAlex Deucher 	ret = amdgpu_atcs_verify_interface(handle, atcs);
786d38ceaf9SAlex Deucher 	if (ret) {
787d38ceaf9SAlex Deucher 		DRM_DEBUG_DRIVER("Call to ATCS verify_interface failed: %d\n", ret);
788d38ceaf9SAlex Deucher 	}
789d38ceaf9SAlex Deucher 
790280cf1a9SLyude Paul 	/* Probe for ATIF, and initialize it if found */
791280cf1a9SLyude Paul 	atif_handle = amdgpu_atif_probe_handle(handle);
792280cf1a9SLyude Paul 	if (!atif_handle)
793280cf1a9SLyude Paul 		goto out;
794280cf1a9SLyude Paul 
795102c16a0SLyude Paul 	atif = kzalloc(sizeof(*atif), GFP_KERNEL);
796102c16a0SLyude Paul 	if (!atif) {
797102c16a0SLyude Paul 		DRM_WARN("Not enough memory to initialize ATIF\n");
798102c16a0SLyude Paul 		goto out;
799102c16a0SLyude Paul 	}
800280cf1a9SLyude Paul 	atif->handle = atif_handle;
801102c16a0SLyude Paul 
802280cf1a9SLyude Paul 	/* Call the ATIF method */
803280cf1a9SLyude Paul 	ret = amdgpu_atif_verify_interface(atif);
804d38ceaf9SAlex Deucher 	if (ret) {
805d38ceaf9SAlex Deucher 		DRM_DEBUG_DRIVER("Call to ATIF verify_interface failed: %d\n", ret);
806102c16a0SLyude Paul 		kfree(atif);
807d38ceaf9SAlex Deucher 		goto out;
808d38ceaf9SAlex Deucher 	}
809102c16a0SLyude Paul 	adev->atif = atif;
810d38ceaf9SAlex Deucher 
811d38ceaf9SAlex Deucher 	if (atif->notifications.brightness_change) {
812d38ceaf9SAlex Deucher 		struct drm_encoder *tmp;
813d38ceaf9SAlex Deucher 
814d38ceaf9SAlex Deucher 		/* Find the encoder controlling the brightness */
815d38ceaf9SAlex Deucher 		list_for_each_entry(tmp, &adev->ddev->mode_config.encoder_list,
816d38ceaf9SAlex Deucher 				head) {
817d38ceaf9SAlex Deucher 			struct amdgpu_encoder *enc = to_amdgpu_encoder(tmp);
818d38ceaf9SAlex Deucher 
819d38ceaf9SAlex Deucher 			if ((enc->devices & (ATOM_DEVICE_LCD_SUPPORT)) &&
820d38ceaf9SAlex Deucher 			    enc->enc_priv) {
821d38ceaf9SAlex Deucher 				struct amdgpu_encoder_atom_dig *dig = enc->enc_priv;
822d38ceaf9SAlex Deucher 				if (dig->bl_dev) {
823d38ceaf9SAlex Deucher 					atif->encoder_for_bl = enc;
824d38ceaf9SAlex Deucher 					break;
825d38ceaf9SAlex Deucher 				}
826d38ceaf9SAlex Deucher 			}
827d38ceaf9SAlex Deucher 		}
828d38ceaf9SAlex Deucher 	}
829d38ceaf9SAlex Deucher 
830d38ceaf9SAlex Deucher 	if (atif->functions.sbios_requests && !atif->functions.system_params) {
831d38ceaf9SAlex Deucher 		/* XXX check this workraround, if sbios request function is
832d38ceaf9SAlex Deucher 		 * present we have to see how it's configured in the system
833d38ceaf9SAlex Deucher 		 * params
834d38ceaf9SAlex Deucher 		 */
835d38ceaf9SAlex Deucher 		atif->functions.system_params = true;
836d38ceaf9SAlex Deucher 	}
837d38ceaf9SAlex Deucher 
838d38ceaf9SAlex Deucher 	if (atif->functions.system_params) {
839280cf1a9SLyude Paul 		ret = amdgpu_atif_get_notification_params(atif);
840d38ceaf9SAlex Deucher 		if (ret) {
841d38ceaf9SAlex Deucher 			DRM_DEBUG_DRIVER("Call to GET_SYSTEM_PARAMS failed: %d\n",
842d38ceaf9SAlex Deucher 					ret);
843d38ceaf9SAlex Deucher 			/* Disable notification */
844d38ceaf9SAlex Deucher 			atif->notification_cfg.enabled = false;
845d38ceaf9SAlex Deucher 		}
846d38ceaf9SAlex Deucher 	}
847d38ceaf9SAlex Deucher 
848206bbafeSDavid Francis 	if (atif->functions.query_backlight_transfer_characteristics) {
849206bbafeSDavid Francis 		ret = amdgpu_atif_query_backlight_caps(atif);
850206bbafeSDavid Francis 		if (ret) {
851206bbafeSDavid Francis 			DRM_DEBUG_DRIVER("Call to QUERY_BACKLIGHT_TRANSFER_CHARACTERISTICS failed: %d\n",
852206bbafeSDavid Francis 					ret);
853206bbafeSDavid Francis 			atif->backlight_caps.caps_valid = false;
854206bbafeSDavid Francis 		}
855206bbafeSDavid Francis 	} else {
856206bbafeSDavid Francis 		atif->backlight_caps.caps_valid = false;
857206bbafeSDavid Francis 	}
858206bbafeSDavid Francis 
859d38ceaf9SAlex Deucher out:
860d38ceaf9SAlex Deucher 	adev->acpi_nb.notifier_call = amdgpu_acpi_event;
861d38ceaf9SAlex Deucher 	register_acpi_notifier(&adev->acpi_nb);
862d38ceaf9SAlex Deucher 
863d38ceaf9SAlex Deucher 	return ret;
864d38ceaf9SAlex Deucher }
865d38ceaf9SAlex Deucher 
866206bbafeSDavid Francis void amdgpu_acpi_get_backlight_caps(struct amdgpu_device *adev,
867206bbafeSDavid Francis 		struct amdgpu_dm_backlight_caps *caps)
868206bbafeSDavid Francis {
869206bbafeSDavid Francis 	if (!adev->atif) {
870206bbafeSDavid Francis 		caps->caps_valid = false;
871206bbafeSDavid Francis 		return;
872206bbafeSDavid Francis 	}
873206bbafeSDavid Francis 	caps->caps_valid = adev->atif->backlight_caps.caps_valid;
874206bbafeSDavid Francis 	caps->min_input_signal = adev->atif->backlight_caps.min_input_signal;
875206bbafeSDavid Francis 	caps->max_input_signal = adev->atif->backlight_caps.max_input_signal;
876206bbafeSDavid Francis }
877206bbafeSDavid Francis 
878d38ceaf9SAlex Deucher /**
879d38ceaf9SAlex Deucher  * amdgpu_acpi_fini - tear down driver acpi support
880d38ceaf9SAlex Deucher  *
881d38ceaf9SAlex Deucher  * @adev: amdgpu_device pointer
882d38ceaf9SAlex Deucher  *
883d38ceaf9SAlex Deucher  * Unregisters with the acpi notifier chain (all asics).
884d38ceaf9SAlex Deucher  */
885d38ceaf9SAlex Deucher void amdgpu_acpi_fini(struct amdgpu_device *adev)
886d38ceaf9SAlex Deucher {
887d38ceaf9SAlex Deucher 	unregister_acpi_notifier(&adev->acpi_nb);
888102c16a0SLyude Paul 	kfree(adev->atif);
889d38ceaf9SAlex Deucher }
890