xref: /linux/drivers/gpu/drm/amd/amdgpu/amdgpu_acpi.c (revision 16eb48c62bd3ff1a523cd1d59591e694bd60277a)
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>
304cd078dcSPrike Liang #include <acpi/actbl.h>
31fdf2f6c5SSam Ravnborg 
32d38ceaf9SAlex Deucher #include <drm/drm_crtc_helper.h>
33d38ceaf9SAlex Deucher #include "amdgpu.h"
3498c65108SJean Delvare #include "amdgpu_pm.h"
355df58525SHuang Rui #include "amdgpu_display.h"
3666dc0dddSRex Zhu #include "amd_acpi.h"
37d38ceaf9SAlex Deucher #include "atom.h"
38d38ceaf9SAlex Deucher 
39102c16a0SLyude Paul struct amdgpu_atif_notification_cfg {
40102c16a0SLyude Paul 	bool enabled;
41102c16a0SLyude Paul 	int command_code;
42102c16a0SLyude Paul };
43102c16a0SLyude Paul 
44102c16a0SLyude Paul struct amdgpu_atif_notifications {
45102c16a0SLyude Paul 	bool thermal_state;
46102c16a0SLyude Paul 	bool forced_power_state;
47102c16a0SLyude Paul 	bool system_power_state;
48102c16a0SLyude Paul 	bool brightness_change;
49102c16a0SLyude Paul 	bool dgpu_display_event;
507349a3afSDavid Francis 	bool gpu_package_power_limit;
51102c16a0SLyude Paul };
52102c16a0SLyude Paul 
53102c16a0SLyude Paul struct amdgpu_atif_functions {
54102c16a0SLyude Paul 	bool system_params;
55102c16a0SLyude Paul 	bool sbios_requests;
56102c16a0SLyude Paul 	bool temperature_change;
577349a3afSDavid Francis 	bool query_backlight_transfer_characteristics;
587349a3afSDavid Francis 	bool ready_to_undock;
597349a3afSDavid Francis 	bool external_gpu_information;
60102c16a0SLyude Paul };
61102c16a0SLyude Paul 
62102c16a0SLyude Paul struct amdgpu_atif {
63280cf1a9SLyude Paul 	acpi_handle handle;
64280cf1a9SLyude Paul 
65102c16a0SLyude Paul 	struct amdgpu_atif_notifications notifications;
66102c16a0SLyude Paul 	struct amdgpu_atif_functions functions;
67102c16a0SLyude Paul 	struct amdgpu_atif_notification_cfg notification_cfg;
6897d798b2SAlex Deucher #if defined(CONFIG_BACKLIGHT_CLASS_DEVICE) || defined(CONFIG_BACKLIGHT_CLASS_DEVICE_MODULE)
6997d798b2SAlex Deucher 	struct backlight_device *bd;
7097d798b2SAlex Deucher #endif
71206bbafeSDavid Francis 	struct amdgpu_dm_backlight_caps backlight_caps;
72102c16a0SLyude Paul };
73102c16a0SLyude Paul 
7477bf762fSAlex Deucher struct amdgpu_atcs_functions {
7577bf762fSAlex Deucher 	bool get_ext_state;
7677bf762fSAlex Deucher 	bool pcie_perf_req;
7777bf762fSAlex Deucher 	bool pcie_dev_rdy;
7877bf762fSAlex Deucher 	bool pcie_bus_width;
79*16eb48c6SSathishkumar S 	bool power_shift_control;
8077bf762fSAlex Deucher };
8177bf762fSAlex Deucher 
8277bf762fSAlex Deucher struct amdgpu_atcs {
8377bf762fSAlex Deucher 	acpi_handle handle;
8477bf762fSAlex Deucher 
8577bf762fSAlex Deucher 	struct amdgpu_atcs_functions functions;
8677bf762fSAlex Deucher };
8777bf762fSAlex Deucher 
88f9b7f370SAlex Deucher static struct amdgpu_acpi_priv {
89f9b7f370SAlex Deucher 	struct amdgpu_atif atif;
90f9b7f370SAlex Deucher 	struct amdgpu_atcs atcs;
91f9b7f370SAlex Deucher } amdgpu_acpi_priv;
92f9b7f370SAlex Deucher 
93d38ceaf9SAlex Deucher /* Call the ATIF method
94d38ceaf9SAlex Deucher  */
95d38ceaf9SAlex Deucher /**
96d38ceaf9SAlex Deucher  * amdgpu_atif_call - call an ATIF method
97d38ceaf9SAlex Deucher  *
9877bf762fSAlex Deucher  * @atif: atif structure
99d38ceaf9SAlex Deucher  * @function: the ATIF function to execute
100d38ceaf9SAlex Deucher  * @params: ATIF function params
101d38ceaf9SAlex Deucher  *
102d38ceaf9SAlex Deucher  * Executes the requested ATIF function (all asics).
103d38ceaf9SAlex Deucher  * Returns a pointer to the acpi output buffer.
104d38ceaf9SAlex Deucher  */
105280cf1a9SLyude Paul static union acpi_object *amdgpu_atif_call(struct amdgpu_atif *atif,
106280cf1a9SLyude Paul 					   int function,
107d38ceaf9SAlex Deucher 					   struct acpi_buffer *params)
108d38ceaf9SAlex Deucher {
109d38ceaf9SAlex Deucher 	acpi_status status;
110d38ceaf9SAlex Deucher 	union acpi_object atif_arg_elements[2];
111d38ceaf9SAlex Deucher 	struct acpi_object_list atif_arg;
112d38ceaf9SAlex Deucher 	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
113d38ceaf9SAlex Deucher 
114d38ceaf9SAlex Deucher 	atif_arg.count = 2;
115d38ceaf9SAlex Deucher 	atif_arg.pointer = &atif_arg_elements[0];
116d38ceaf9SAlex Deucher 
117d38ceaf9SAlex Deucher 	atif_arg_elements[0].type = ACPI_TYPE_INTEGER;
118d38ceaf9SAlex Deucher 	atif_arg_elements[0].integer.value = function;
119d38ceaf9SAlex Deucher 
120d38ceaf9SAlex Deucher 	if (params) {
121d38ceaf9SAlex Deucher 		atif_arg_elements[1].type = ACPI_TYPE_BUFFER;
122d38ceaf9SAlex Deucher 		atif_arg_elements[1].buffer.length = params->length;
123d38ceaf9SAlex Deucher 		atif_arg_elements[1].buffer.pointer = params->pointer;
124d38ceaf9SAlex Deucher 	} else {
125d38ceaf9SAlex Deucher 		/* We need a second fake parameter */
126d38ceaf9SAlex Deucher 		atif_arg_elements[1].type = ACPI_TYPE_INTEGER;
127d38ceaf9SAlex Deucher 		atif_arg_elements[1].integer.value = 0;
128d38ceaf9SAlex Deucher 	}
129d38ceaf9SAlex Deucher 
130280cf1a9SLyude Paul 	status = acpi_evaluate_object(atif->handle, NULL, &atif_arg,
131280cf1a9SLyude Paul 				      &buffer);
132d38ceaf9SAlex Deucher 
133d38ceaf9SAlex Deucher 	/* Fail only if calling the method fails and ATIF is supported */
134d38ceaf9SAlex Deucher 	if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
135d38ceaf9SAlex Deucher 		DRM_DEBUG_DRIVER("failed to evaluate ATIF got %s\n",
136d38ceaf9SAlex Deucher 				 acpi_format_exception(status));
137d38ceaf9SAlex Deucher 		kfree(buffer.pointer);
138d38ceaf9SAlex Deucher 		return NULL;
139d38ceaf9SAlex Deucher 	}
140d38ceaf9SAlex Deucher 
141d38ceaf9SAlex Deucher 	return buffer.pointer;
142d38ceaf9SAlex Deucher }
143d38ceaf9SAlex Deucher 
144d38ceaf9SAlex Deucher /**
145d38ceaf9SAlex Deucher  * amdgpu_atif_parse_notification - parse supported notifications
146d38ceaf9SAlex Deucher  *
147d38ceaf9SAlex Deucher  * @n: supported notifications struct
148d38ceaf9SAlex Deucher  * @mask: supported notifications mask from ATIF
149d38ceaf9SAlex Deucher  *
150d38ceaf9SAlex Deucher  * Use the supported notifications mask from ATIF function
151d38ceaf9SAlex Deucher  * ATIF_FUNCTION_VERIFY_INTERFACE to determine what notifications
152d38ceaf9SAlex Deucher  * are supported (all asics).
153d38ceaf9SAlex Deucher  */
154d38ceaf9SAlex Deucher static void amdgpu_atif_parse_notification(struct amdgpu_atif_notifications *n, u32 mask)
155d38ceaf9SAlex Deucher {
156d38ceaf9SAlex Deucher 	n->thermal_state = mask & ATIF_THERMAL_STATE_CHANGE_REQUEST_SUPPORTED;
157d38ceaf9SAlex Deucher 	n->forced_power_state = mask & ATIF_FORCED_POWER_STATE_CHANGE_REQUEST_SUPPORTED;
158d38ceaf9SAlex Deucher 	n->system_power_state = mask & ATIF_SYSTEM_POWER_SOURCE_CHANGE_REQUEST_SUPPORTED;
159d38ceaf9SAlex Deucher 	n->brightness_change = mask & ATIF_PANEL_BRIGHTNESS_CHANGE_REQUEST_SUPPORTED;
160d38ceaf9SAlex Deucher 	n->dgpu_display_event = mask & ATIF_DGPU_DISPLAY_EVENT_SUPPORTED;
1617349a3afSDavid Francis 	n->gpu_package_power_limit = mask & ATIF_GPU_PACKAGE_POWER_LIMIT_REQUEST_SUPPORTED;
162d38ceaf9SAlex Deucher }
163d38ceaf9SAlex Deucher 
164d38ceaf9SAlex Deucher /**
165d38ceaf9SAlex Deucher  * amdgpu_atif_parse_functions - parse supported functions
166d38ceaf9SAlex Deucher  *
167d38ceaf9SAlex Deucher  * @f: supported functions struct
168d38ceaf9SAlex Deucher  * @mask: supported functions mask from ATIF
169d38ceaf9SAlex Deucher  *
170d38ceaf9SAlex Deucher  * Use the supported functions mask from ATIF function
171d38ceaf9SAlex Deucher  * ATIF_FUNCTION_VERIFY_INTERFACE to determine what functions
172d38ceaf9SAlex Deucher  * are supported (all asics).
173d38ceaf9SAlex Deucher  */
174d38ceaf9SAlex Deucher static void amdgpu_atif_parse_functions(struct amdgpu_atif_functions *f, u32 mask)
175d38ceaf9SAlex Deucher {
176d38ceaf9SAlex Deucher 	f->system_params = mask & ATIF_GET_SYSTEM_PARAMETERS_SUPPORTED;
177d38ceaf9SAlex Deucher 	f->sbios_requests = mask & ATIF_GET_SYSTEM_BIOS_REQUESTS_SUPPORTED;
178d38ceaf9SAlex Deucher 	f->temperature_change = mask & ATIF_TEMPERATURE_CHANGE_NOTIFICATION_SUPPORTED;
1797349a3afSDavid Francis 	f->query_backlight_transfer_characteristics =
1807349a3afSDavid Francis 		mask & ATIF_QUERY_BACKLIGHT_TRANSFER_CHARACTERISTICS_SUPPORTED;
1817349a3afSDavid Francis 	f->ready_to_undock = mask & ATIF_READY_TO_UNDOCK_NOTIFICATION_SUPPORTED;
1827349a3afSDavid Francis 	f->external_gpu_information = mask & ATIF_GET_EXTERNAL_GPU_INFORMATION_SUPPORTED;
183d38ceaf9SAlex Deucher }
184d38ceaf9SAlex Deucher 
185d38ceaf9SAlex Deucher /**
186d38ceaf9SAlex Deucher  * amdgpu_atif_verify_interface - verify ATIF
187d38ceaf9SAlex Deucher  *
188d38ceaf9SAlex Deucher  * @atif: amdgpu atif struct
189d38ceaf9SAlex Deucher  *
190d38ceaf9SAlex Deucher  * Execute the ATIF_FUNCTION_VERIFY_INTERFACE ATIF function
191d38ceaf9SAlex Deucher  * to initialize ATIF and determine what features are supported
192d38ceaf9SAlex Deucher  * (all asics).
193d38ceaf9SAlex Deucher  * returns 0 on success, error on failure.
194d38ceaf9SAlex Deucher  */
195280cf1a9SLyude Paul static int amdgpu_atif_verify_interface(struct amdgpu_atif *atif)
196d38ceaf9SAlex Deucher {
197d38ceaf9SAlex Deucher 	union acpi_object *info;
198d38ceaf9SAlex Deucher 	struct atif_verify_interface output;
199d38ceaf9SAlex Deucher 	size_t size;
200d38ceaf9SAlex Deucher 	int err = 0;
201d38ceaf9SAlex Deucher 
202280cf1a9SLyude Paul 	info = amdgpu_atif_call(atif, ATIF_FUNCTION_VERIFY_INTERFACE, NULL);
203d38ceaf9SAlex Deucher 	if (!info)
204d38ceaf9SAlex Deucher 		return -EIO;
205d38ceaf9SAlex Deucher 
206d38ceaf9SAlex Deucher 	memset(&output, 0, sizeof(output));
207d38ceaf9SAlex Deucher 
208d38ceaf9SAlex Deucher 	size = *(u16 *) info->buffer.pointer;
209d38ceaf9SAlex Deucher 	if (size < 12) {
210d38ceaf9SAlex Deucher 		DRM_INFO("ATIF buffer is too small: %zu\n", size);
211d38ceaf9SAlex Deucher 		err = -EINVAL;
212d38ceaf9SAlex Deucher 		goto out;
213d38ceaf9SAlex Deucher 	}
214d38ceaf9SAlex Deucher 	size = min(sizeof(output), size);
215d38ceaf9SAlex Deucher 
216d38ceaf9SAlex Deucher 	memcpy(&output, info->buffer.pointer, size);
217d38ceaf9SAlex Deucher 
218d38ceaf9SAlex Deucher 	/* TODO: check version? */
219d38ceaf9SAlex Deucher 	DRM_DEBUG_DRIVER("ATIF version %u\n", output.version);
220d38ceaf9SAlex Deucher 
221d38ceaf9SAlex Deucher 	amdgpu_atif_parse_notification(&atif->notifications, output.notification_mask);
222d38ceaf9SAlex Deucher 	amdgpu_atif_parse_functions(&atif->functions, output.function_bits);
223d38ceaf9SAlex Deucher 
224d38ceaf9SAlex Deucher out:
225d38ceaf9SAlex Deucher 	kfree(info);
226d38ceaf9SAlex Deucher 	return err;
227d38ceaf9SAlex Deucher }
228d38ceaf9SAlex Deucher 
229d38ceaf9SAlex Deucher /**
230d38ceaf9SAlex Deucher  * amdgpu_atif_get_notification_params - determine notify configuration
231d38ceaf9SAlex Deucher  *
2321fdbbc12SFabio M. De Francesco  * @atif: acpi handle
233d38ceaf9SAlex Deucher  *
234d38ceaf9SAlex Deucher  * Execute the ATIF_FUNCTION_GET_SYSTEM_PARAMETERS ATIF function
235d38ceaf9SAlex Deucher  * to determine if a notifier is used and if so which one
236d38ceaf9SAlex Deucher  * (all asics).  This is either Notify(VGA, 0x81) or Notify(VGA, n)
237d38ceaf9SAlex Deucher  * where n is specified in the result if a notifier is used.
238d38ceaf9SAlex Deucher  * Returns 0 on success, error on failure.
239d38ceaf9SAlex Deucher  */
240280cf1a9SLyude Paul static int amdgpu_atif_get_notification_params(struct amdgpu_atif *atif)
241d38ceaf9SAlex Deucher {
242d38ceaf9SAlex Deucher 	union acpi_object *info;
243280cf1a9SLyude Paul 	struct amdgpu_atif_notification_cfg *n = &atif->notification_cfg;
244d38ceaf9SAlex Deucher 	struct atif_system_params params;
245d38ceaf9SAlex Deucher 	size_t size;
246d38ceaf9SAlex Deucher 	int err = 0;
247d38ceaf9SAlex Deucher 
248280cf1a9SLyude Paul 	info = amdgpu_atif_call(atif, ATIF_FUNCTION_GET_SYSTEM_PARAMETERS,
249280cf1a9SLyude Paul 				NULL);
250d38ceaf9SAlex Deucher 	if (!info) {
251d38ceaf9SAlex Deucher 		err = -EIO;
252d38ceaf9SAlex Deucher 		goto out;
253d38ceaf9SAlex Deucher 	}
254d38ceaf9SAlex Deucher 
255d38ceaf9SAlex Deucher 	size = *(u16 *) info->buffer.pointer;
256d38ceaf9SAlex Deucher 	if (size < 10) {
257d38ceaf9SAlex Deucher 		err = -EINVAL;
258d38ceaf9SAlex Deucher 		goto out;
259d38ceaf9SAlex Deucher 	}
260d38ceaf9SAlex Deucher 
261d38ceaf9SAlex Deucher 	memset(&params, 0, sizeof(params));
262d38ceaf9SAlex Deucher 	size = min(sizeof(params), size);
263d38ceaf9SAlex Deucher 	memcpy(&params, info->buffer.pointer, size);
264d38ceaf9SAlex Deucher 
265d38ceaf9SAlex Deucher 	DRM_DEBUG_DRIVER("SYSTEM_PARAMS: mask = %#x, flags = %#x\n",
266d38ceaf9SAlex Deucher 			params.flags, params.valid_mask);
267d38ceaf9SAlex Deucher 	params.flags = params.flags & params.valid_mask;
268d38ceaf9SAlex Deucher 
269d38ceaf9SAlex Deucher 	if ((params.flags & ATIF_NOTIFY_MASK) == ATIF_NOTIFY_NONE) {
270d38ceaf9SAlex Deucher 		n->enabled = false;
271d38ceaf9SAlex Deucher 		n->command_code = 0;
272d38ceaf9SAlex Deucher 	} else if ((params.flags & ATIF_NOTIFY_MASK) == ATIF_NOTIFY_81) {
273d38ceaf9SAlex Deucher 		n->enabled = true;
274d38ceaf9SAlex Deucher 		n->command_code = 0x81;
275d38ceaf9SAlex Deucher 	} else {
276d38ceaf9SAlex Deucher 		if (size < 11) {
277d38ceaf9SAlex Deucher 			err = -EINVAL;
278d38ceaf9SAlex Deucher 			goto out;
279d38ceaf9SAlex Deucher 		}
280d38ceaf9SAlex Deucher 		n->enabled = true;
281d38ceaf9SAlex Deucher 		n->command_code = params.command_code;
282d38ceaf9SAlex Deucher 	}
283d38ceaf9SAlex Deucher 
284d38ceaf9SAlex Deucher out:
285d38ceaf9SAlex Deucher 	DRM_DEBUG_DRIVER("Notification %s, command code = %#x\n",
286d38ceaf9SAlex Deucher 			(n->enabled ? "enabled" : "disabled"),
287d38ceaf9SAlex Deucher 			n->command_code);
288d38ceaf9SAlex Deucher 	kfree(info);
289d38ceaf9SAlex Deucher 	return err;
290d38ceaf9SAlex Deucher }
291d38ceaf9SAlex Deucher 
292d38ceaf9SAlex Deucher /**
293206bbafeSDavid Francis  * amdgpu_atif_query_backlight_caps - get min and max backlight input signal
294206bbafeSDavid Francis  *
2951fdbbc12SFabio M. De Francesco  * @atif: acpi handle
296206bbafeSDavid Francis  *
297206bbafeSDavid Francis  * Execute the QUERY_BRIGHTNESS_TRANSFER_CHARACTERISTICS ATIF function
298206bbafeSDavid Francis  * to determine the acceptable range of backlight values
299206bbafeSDavid Francis  *
300206bbafeSDavid Francis  * Backlight_caps.caps_valid will be set to true if the query is successful
301206bbafeSDavid Francis  *
302206bbafeSDavid Francis  * The input signals are in range 0-255
303206bbafeSDavid Francis  *
304206bbafeSDavid Francis  * This function assumes the display with backlight is the first LCD
305206bbafeSDavid Francis  *
306206bbafeSDavid Francis  * Returns 0 on success, error on failure.
307206bbafeSDavid Francis  */
308206bbafeSDavid Francis static int amdgpu_atif_query_backlight_caps(struct amdgpu_atif *atif)
309206bbafeSDavid Francis {
310206bbafeSDavid Francis 	union acpi_object *info;
311206bbafeSDavid Francis 	struct atif_qbtc_output characteristics;
312206bbafeSDavid Francis 	struct atif_qbtc_arguments arguments;
313206bbafeSDavid Francis 	struct acpi_buffer params;
314206bbafeSDavid Francis 	size_t size;
315206bbafeSDavid Francis 	int err = 0;
316206bbafeSDavid Francis 
317206bbafeSDavid Francis 	arguments.size = sizeof(arguments);
318206bbafeSDavid Francis 	arguments.requested_display = ATIF_QBTC_REQUEST_LCD1;
319206bbafeSDavid Francis 
320206bbafeSDavid Francis 	params.length = sizeof(arguments);
321206bbafeSDavid Francis 	params.pointer = (void *)&arguments;
322206bbafeSDavid Francis 
323206bbafeSDavid Francis 	info = amdgpu_atif_call(atif,
324206bbafeSDavid Francis 		ATIF_FUNCTION_QUERY_BRIGHTNESS_TRANSFER_CHARACTERISTICS,
325206bbafeSDavid Francis 		&params);
326206bbafeSDavid Francis 	if (!info) {
327206bbafeSDavid Francis 		err = -EIO;
328206bbafeSDavid Francis 		goto out;
329206bbafeSDavid Francis 	}
330206bbafeSDavid Francis 
331206bbafeSDavid Francis 	size = *(u16 *) info->buffer.pointer;
332206bbafeSDavid Francis 	if (size < 10) {
333206bbafeSDavid Francis 		err = -EINVAL;
334206bbafeSDavid Francis 		goto out;
335206bbafeSDavid Francis 	}
336206bbafeSDavid Francis 
337206bbafeSDavid Francis 	memset(&characteristics, 0, sizeof(characteristics));
338206bbafeSDavid Francis 	size = min(sizeof(characteristics), size);
339206bbafeSDavid Francis 	memcpy(&characteristics, info->buffer.pointer, size);
340206bbafeSDavid Francis 
341206bbafeSDavid Francis 	atif->backlight_caps.caps_valid = true;
342206bbafeSDavid Francis 	atif->backlight_caps.min_input_signal =
343206bbafeSDavid Francis 			characteristics.min_input_signal;
344206bbafeSDavid Francis 	atif->backlight_caps.max_input_signal =
345206bbafeSDavid Francis 			characteristics.max_input_signal;
346206bbafeSDavid Francis out:
347206bbafeSDavid Francis 	kfree(info);
348206bbafeSDavid Francis 	return err;
349206bbafeSDavid Francis }
350206bbafeSDavid Francis 
351206bbafeSDavid Francis /**
352d38ceaf9SAlex Deucher  * amdgpu_atif_get_sbios_requests - get requested sbios event
353d38ceaf9SAlex Deucher  *
3541fdbbc12SFabio M. De Francesco  * @atif: acpi handle
355d38ceaf9SAlex Deucher  * @req: atif sbios request struct
356d38ceaf9SAlex Deucher  *
357d38ceaf9SAlex Deucher  * Execute the ATIF_FUNCTION_GET_SYSTEM_BIOS_REQUESTS ATIF function
358d38ceaf9SAlex Deucher  * to determine what requests the sbios is making to the driver
359d38ceaf9SAlex Deucher  * (all asics).
360d38ceaf9SAlex Deucher  * Returns 0 on success, error on failure.
361d38ceaf9SAlex Deucher  */
362280cf1a9SLyude Paul static int amdgpu_atif_get_sbios_requests(struct amdgpu_atif *atif,
363d38ceaf9SAlex Deucher 					  struct atif_sbios_requests *req)
364d38ceaf9SAlex Deucher {
365d38ceaf9SAlex Deucher 	union acpi_object *info;
366d38ceaf9SAlex Deucher 	size_t size;
367d38ceaf9SAlex Deucher 	int count = 0;
368d38ceaf9SAlex Deucher 
369280cf1a9SLyude Paul 	info = amdgpu_atif_call(atif, ATIF_FUNCTION_GET_SYSTEM_BIOS_REQUESTS,
370280cf1a9SLyude Paul 				NULL);
371d38ceaf9SAlex Deucher 	if (!info)
372d38ceaf9SAlex Deucher 		return -EIO;
373d38ceaf9SAlex Deucher 
374d38ceaf9SAlex Deucher 	size = *(u16 *)info->buffer.pointer;
375d38ceaf9SAlex Deucher 	if (size < 0xd) {
376d38ceaf9SAlex Deucher 		count = -EINVAL;
377d38ceaf9SAlex Deucher 		goto out;
378d38ceaf9SAlex Deucher 	}
379d38ceaf9SAlex Deucher 	memset(req, 0, sizeof(*req));
380d38ceaf9SAlex Deucher 
381d38ceaf9SAlex Deucher 	size = min(sizeof(*req), size);
382d38ceaf9SAlex Deucher 	memcpy(req, info->buffer.pointer, size);
383d38ceaf9SAlex Deucher 	DRM_DEBUG_DRIVER("SBIOS pending requests: %#x\n", req->pending);
384d38ceaf9SAlex Deucher 
385d38ceaf9SAlex Deucher 	count = hweight32(req->pending);
386d38ceaf9SAlex Deucher 
387d38ceaf9SAlex Deucher out:
388d38ceaf9SAlex Deucher 	kfree(info);
389d38ceaf9SAlex Deucher 	return count;
390d38ceaf9SAlex Deucher }
391d38ceaf9SAlex Deucher 
392d38ceaf9SAlex Deucher /**
393d38ceaf9SAlex Deucher  * amdgpu_atif_handler - handle ATIF notify requests
394d38ceaf9SAlex Deucher  *
395d38ceaf9SAlex Deucher  * @adev: amdgpu_device pointer
396d38ceaf9SAlex Deucher  * @event: atif sbios request struct
397d38ceaf9SAlex Deucher  *
398d38ceaf9SAlex Deucher  * Checks the acpi event and if it matches an atif event,
399d38ceaf9SAlex Deucher  * handles it.
400582f58deSLyude Paul  *
401582f58deSLyude Paul  * Returns:
402582f58deSLyude Paul  * NOTIFY_BAD or NOTIFY_DONE, depending on the event.
403d38ceaf9SAlex Deucher  */
404b7cecbe8SJean Delvare static int amdgpu_atif_handler(struct amdgpu_device *adev,
405d38ceaf9SAlex Deucher 			       struct acpi_bus_event *event)
406d38ceaf9SAlex Deucher {
407f9b7f370SAlex Deucher 	struct amdgpu_atif *atif = &amdgpu_acpi_priv.atif;
408d38ceaf9SAlex Deucher 	int count;
409d38ceaf9SAlex Deucher 
410d38ceaf9SAlex Deucher 	DRM_DEBUG_DRIVER("event, device_class = %s, type = %#x\n",
411d38ceaf9SAlex Deucher 			event->device_class, event->type);
412d38ceaf9SAlex Deucher 
413d38ceaf9SAlex Deucher 	if (strcmp(event->device_class, ACPI_VIDEO_CLASS) != 0)
414d38ceaf9SAlex Deucher 		return NOTIFY_DONE;
415d38ceaf9SAlex Deucher 
416582f58deSLyude Paul 	/* Is this actually our event? */
417f9b7f370SAlex Deucher 	if (!atif->notification_cfg.enabled ||
418582f58deSLyude Paul 	    event->type != atif->notification_cfg.command_code) {
419582f58deSLyude Paul 		/* These events will generate keypresses otherwise */
420582f58deSLyude Paul 		if (event->type == ACPI_VIDEO_NOTIFY_PROBE)
421582f58deSLyude Paul 			return NOTIFY_BAD;
422582f58deSLyude Paul 		else
423d38ceaf9SAlex Deucher 			return NOTIFY_DONE;
424582f58deSLyude Paul 	}
425d38ceaf9SAlex Deucher 
4269e7204beSAlex Deucher 	if (atif->functions.sbios_requests) {
4279e7204beSAlex Deucher 		struct atif_sbios_requests req;
4289e7204beSAlex Deucher 
429d38ceaf9SAlex Deucher 		/* Check pending SBIOS requests */
430280cf1a9SLyude Paul 		count = amdgpu_atif_get_sbios_requests(atif, &req);
431d38ceaf9SAlex Deucher 
432d38ceaf9SAlex Deucher 		if (count <= 0)
433582f58deSLyude Paul 			return NOTIFY_BAD;
434d38ceaf9SAlex Deucher 
435d38ceaf9SAlex Deucher 		DRM_DEBUG_DRIVER("ATIF: %d pending SBIOS requests\n", count);
436d38ceaf9SAlex Deucher 
43797d798b2SAlex Deucher 		if (req.pending & ATIF_PANEL_BRIGHTNESS_CHANGE_REQUEST) {
43897d798b2SAlex Deucher #if defined(CONFIG_BACKLIGHT_CLASS_DEVICE) || defined(CONFIG_BACKLIGHT_CLASS_DEVICE_MODULE)
43997d798b2SAlex Deucher 			if (atif->bd) {
440d38ceaf9SAlex Deucher 				DRM_DEBUG_DRIVER("Changing brightness to %d\n",
441d38ceaf9SAlex Deucher 						 req.backlight_level);
442bcb7b0efSAndriy Gapon 				/*
443bcb7b0efSAndriy Gapon 				 * XXX backlight_device_set_brightness() is
444bcb7b0efSAndriy Gapon 				 * hardwired to post BACKLIGHT_UPDATE_SYSFS.
445bcb7b0efSAndriy Gapon 				 * It probably should accept 'reason' parameter.
446bcb7b0efSAndriy Gapon 				 */
44797d798b2SAlex Deucher 				backlight_device_set_brightness(atif->bd, req.backlight_level);
448bcb7b0efSAndriy Gapon 			}
449bcb7b0efSAndriy Gapon #endif
45097d798b2SAlex Deucher 		}
45197d798b2SAlex Deucher 
4521b0f568dSAlex Deucher 		if (req.pending & ATIF_DGPU_DISPLAY_EVENT) {
453bdb1ccb0SAaron Liu 			if (adev->flags & AMD_IS_PX) {
4544a580877SLuben Tuikov 				pm_runtime_get_sync(adev_to_drm(adev)->dev);
4551b0f568dSAlex Deucher 				/* Just fire off a uevent and let userspace tell us what to do */
4564a580877SLuben Tuikov 				drm_helper_hpd_irq_event(adev_to_drm(adev));
4574a580877SLuben Tuikov 				pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
4584a580877SLuben Tuikov 				pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
4591b0f568dSAlex Deucher 			}
4601b0f568dSAlex Deucher 		}
461d38ceaf9SAlex Deucher 		/* TODO: check other events */
4629e7204beSAlex Deucher 	}
463d38ceaf9SAlex Deucher 
464d38ceaf9SAlex Deucher 	/* We've handled the event, stop the notifier chain. The ACPI interface
465d38ceaf9SAlex Deucher 	 * overloads ACPI_VIDEO_NOTIFY_PROBE, we don't want to send that to
466d38ceaf9SAlex Deucher 	 * userspace if the event was generated only to signal a SBIOS
467d38ceaf9SAlex Deucher 	 * request.
468d38ceaf9SAlex Deucher 	 */
469d38ceaf9SAlex Deucher 	return NOTIFY_BAD;
470d38ceaf9SAlex Deucher }
471d38ceaf9SAlex Deucher 
472d38ceaf9SAlex Deucher /* Call the ATCS method
473d38ceaf9SAlex Deucher  */
474d38ceaf9SAlex Deucher /**
475d38ceaf9SAlex Deucher  * amdgpu_atcs_call - call an ATCS method
476d38ceaf9SAlex Deucher  *
47777bf762fSAlex Deucher  * @atcs: atcs structure
478d38ceaf9SAlex Deucher  * @function: the ATCS function to execute
479d38ceaf9SAlex Deucher  * @params: ATCS function params
480d38ceaf9SAlex Deucher  *
481d38ceaf9SAlex Deucher  * Executes the requested ATCS function (all asics).
482d38ceaf9SAlex Deucher  * Returns a pointer to the acpi output buffer.
483d38ceaf9SAlex Deucher  */
48477bf762fSAlex Deucher static union acpi_object *amdgpu_atcs_call(struct amdgpu_atcs *atcs,
48577bf762fSAlex Deucher 					   int function,
486d38ceaf9SAlex Deucher 					   struct acpi_buffer *params)
487d38ceaf9SAlex Deucher {
488d38ceaf9SAlex Deucher 	acpi_status status;
489d38ceaf9SAlex Deucher 	union acpi_object atcs_arg_elements[2];
490d38ceaf9SAlex Deucher 	struct acpi_object_list atcs_arg;
491d38ceaf9SAlex Deucher 	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
492d38ceaf9SAlex Deucher 
493d38ceaf9SAlex Deucher 	atcs_arg.count = 2;
494d38ceaf9SAlex Deucher 	atcs_arg.pointer = &atcs_arg_elements[0];
495d38ceaf9SAlex Deucher 
496d38ceaf9SAlex Deucher 	atcs_arg_elements[0].type = ACPI_TYPE_INTEGER;
497d38ceaf9SAlex Deucher 	atcs_arg_elements[0].integer.value = function;
498d38ceaf9SAlex Deucher 
499d38ceaf9SAlex Deucher 	if (params) {
500d38ceaf9SAlex Deucher 		atcs_arg_elements[1].type = ACPI_TYPE_BUFFER;
501d38ceaf9SAlex Deucher 		atcs_arg_elements[1].buffer.length = params->length;
502d38ceaf9SAlex Deucher 		atcs_arg_elements[1].buffer.pointer = params->pointer;
503d38ceaf9SAlex Deucher 	} else {
504d38ceaf9SAlex Deucher 		/* We need a second fake parameter */
505d38ceaf9SAlex Deucher 		atcs_arg_elements[1].type = ACPI_TYPE_INTEGER;
506d38ceaf9SAlex Deucher 		atcs_arg_elements[1].integer.value = 0;
507d38ceaf9SAlex Deucher 	}
508d38ceaf9SAlex Deucher 
5094965257fSAlex Deucher 	status = acpi_evaluate_object(atcs->handle, NULL, &atcs_arg, &buffer);
510d38ceaf9SAlex Deucher 
511d38ceaf9SAlex Deucher 	/* Fail only if calling the method fails and ATIF is supported */
512d38ceaf9SAlex Deucher 	if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
513d38ceaf9SAlex Deucher 		DRM_DEBUG_DRIVER("failed to evaluate ATCS got %s\n",
514d38ceaf9SAlex Deucher 				 acpi_format_exception(status));
515d38ceaf9SAlex Deucher 		kfree(buffer.pointer);
516d38ceaf9SAlex Deucher 		return NULL;
517d38ceaf9SAlex Deucher 	}
518d38ceaf9SAlex Deucher 
519d38ceaf9SAlex Deucher 	return buffer.pointer;
520d38ceaf9SAlex Deucher }
521d38ceaf9SAlex Deucher 
522d38ceaf9SAlex Deucher /**
523d38ceaf9SAlex Deucher  * amdgpu_atcs_parse_functions - parse supported functions
524d38ceaf9SAlex Deucher  *
525d38ceaf9SAlex Deucher  * @f: supported functions struct
526d38ceaf9SAlex Deucher  * @mask: supported functions mask from ATCS
527d38ceaf9SAlex Deucher  *
528d38ceaf9SAlex Deucher  * Use the supported functions mask from ATCS function
529d38ceaf9SAlex Deucher  * ATCS_FUNCTION_VERIFY_INTERFACE to determine what functions
530d38ceaf9SAlex Deucher  * are supported (all asics).
531d38ceaf9SAlex Deucher  */
532d38ceaf9SAlex Deucher static void amdgpu_atcs_parse_functions(struct amdgpu_atcs_functions *f, u32 mask)
533d38ceaf9SAlex Deucher {
534d38ceaf9SAlex Deucher 	f->get_ext_state = mask & ATCS_GET_EXTERNAL_STATE_SUPPORTED;
535d38ceaf9SAlex Deucher 	f->pcie_perf_req = mask & ATCS_PCIE_PERFORMANCE_REQUEST_SUPPORTED;
536d38ceaf9SAlex Deucher 	f->pcie_dev_rdy = mask & ATCS_PCIE_DEVICE_READY_NOTIFICATION_SUPPORTED;
537d38ceaf9SAlex Deucher 	f->pcie_bus_width = mask & ATCS_SET_PCIE_BUS_WIDTH_SUPPORTED;
538*16eb48c6SSathishkumar S 	f->power_shift_control = mask & ATCS_SET_POWER_SHIFT_CONTROL_SUPPORTED;
539d38ceaf9SAlex Deucher }
540d38ceaf9SAlex Deucher 
541d38ceaf9SAlex Deucher /**
542d38ceaf9SAlex Deucher  * amdgpu_atcs_verify_interface - verify ATCS
543d38ceaf9SAlex Deucher  *
544d38ceaf9SAlex Deucher  * @atcs: amdgpu atcs struct
545d38ceaf9SAlex Deucher  *
546d38ceaf9SAlex Deucher  * Execute the ATCS_FUNCTION_VERIFY_INTERFACE ATCS function
547d38ceaf9SAlex Deucher  * to initialize ATCS and determine what features are supported
548d38ceaf9SAlex Deucher  * (all asics).
549d38ceaf9SAlex Deucher  * returns 0 on success, error on failure.
550d38ceaf9SAlex Deucher  */
55177bf762fSAlex Deucher static int amdgpu_atcs_verify_interface(struct amdgpu_atcs *atcs)
552d38ceaf9SAlex Deucher {
553d38ceaf9SAlex Deucher 	union acpi_object *info;
554d38ceaf9SAlex Deucher 	struct atcs_verify_interface output;
555d38ceaf9SAlex Deucher 	size_t size;
556d38ceaf9SAlex Deucher 	int err = 0;
557d38ceaf9SAlex Deucher 
55877bf762fSAlex Deucher 	info = amdgpu_atcs_call(atcs, ATCS_FUNCTION_VERIFY_INTERFACE, NULL);
559d38ceaf9SAlex Deucher 	if (!info)
560d38ceaf9SAlex Deucher 		return -EIO;
561d38ceaf9SAlex Deucher 
562d38ceaf9SAlex Deucher 	memset(&output, 0, sizeof(output));
563d38ceaf9SAlex Deucher 
564d38ceaf9SAlex Deucher 	size = *(u16 *) info->buffer.pointer;
565d38ceaf9SAlex Deucher 	if (size < 8) {
566d38ceaf9SAlex Deucher 		DRM_INFO("ATCS buffer is too small: %zu\n", size);
567d38ceaf9SAlex Deucher 		err = -EINVAL;
568d38ceaf9SAlex Deucher 		goto out;
569d38ceaf9SAlex Deucher 	}
570d38ceaf9SAlex Deucher 	size = min(sizeof(output), size);
571d38ceaf9SAlex Deucher 
572d38ceaf9SAlex Deucher 	memcpy(&output, info->buffer.pointer, size);
573d38ceaf9SAlex Deucher 
574d38ceaf9SAlex Deucher 	/* TODO: check version? */
575d38ceaf9SAlex Deucher 	DRM_DEBUG_DRIVER("ATCS version %u\n", output.version);
576d38ceaf9SAlex Deucher 
577d38ceaf9SAlex Deucher 	amdgpu_atcs_parse_functions(&atcs->functions, output.function_bits);
578d38ceaf9SAlex Deucher 
579d38ceaf9SAlex Deucher out:
580d38ceaf9SAlex Deucher 	kfree(info);
581d38ceaf9SAlex Deucher 	return err;
582d38ceaf9SAlex Deucher }
583d38ceaf9SAlex Deucher 
584d38ceaf9SAlex Deucher /**
585d38ceaf9SAlex Deucher  * amdgpu_acpi_is_pcie_performance_request_supported
586d38ceaf9SAlex Deucher  *
587d38ceaf9SAlex Deucher  * @adev: amdgpu_device pointer
588d38ceaf9SAlex Deucher  *
589d38ceaf9SAlex Deucher  * Check if the ATCS pcie_perf_req and pcie_dev_rdy methods
590d38ceaf9SAlex Deucher  * are supported (all asics).
591d38ceaf9SAlex Deucher  * returns true if supported, false if not.
592d38ceaf9SAlex Deucher  */
593d38ceaf9SAlex Deucher bool amdgpu_acpi_is_pcie_performance_request_supported(struct amdgpu_device *adev)
594d38ceaf9SAlex Deucher {
595f9b7f370SAlex Deucher 	struct amdgpu_atcs *atcs = &amdgpu_acpi_priv.atcs;
596d38ceaf9SAlex Deucher 
597d38ceaf9SAlex Deucher 	if (atcs->functions.pcie_perf_req && atcs->functions.pcie_dev_rdy)
598d38ceaf9SAlex Deucher 		return true;
599d38ceaf9SAlex Deucher 
600d38ceaf9SAlex Deucher 	return false;
601d38ceaf9SAlex Deucher }
602d38ceaf9SAlex Deucher 
603d38ceaf9SAlex Deucher /**
604*16eb48c6SSathishkumar S  * amdgpu_acpi_is_power_shift_control_supported
605*16eb48c6SSathishkumar S  *
606*16eb48c6SSathishkumar S  * Check if the ATCS power shift control method
607*16eb48c6SSathishkumar S  * is supported.
608*16eb48c6SSathishkumar S  * returns true if supported, false if not.
609*16eb48c6SSathishkumar S  */
610*16eb48c6SSathishkumar S bool amdgpu_acpi_is_power_shift_control_supported(void)
611*16eb48c6SSathishkumar S {
612*16eb48c6SSathishkumar S 	return amdgpu_acpi_priv.atcs.functions.power_shift_control;
613*16eb48c6SSathishkumar S }
614*16eb48c6SSathishkumar S 
615*16eb48c6SSathishkumar S /**
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 	union acpi_object *info;
627f9b7f370SAlex Deucher 	struct amdgpu_atcs *atcs = &amdgpu_acpi_priv.atcs;
628d38ceaf9SAlex Deucher 
629d38ceaf9SAlex Deucher 	if (!atcs->functions.pcie_dev_rdy)
630d38ceaf9SAlex Deucher 		return -EINVAL;
631d38ceaf9SAlex Deucher 
63277bf762fSAlex Deucher 	info = amdgpu_atcs_call(atcs, ATCS_FUNCTION_PCIE_DEVICE_READY_NOTIFICATION, NULL);
633d38ceaf9SAlex Deucher 	if (!info)
634d38ceaf9SAlex Deucher 		return -EIO;
635d38ceaf9SAlex Deucher 
636d38ceaf9SAlex Deucher 	kfree(info);
637d38ceaf9SAlex Deucher 
638d38ceaf9SAlex Deucher 	return 0;
639d38ceaf9SAlex Deucher }
640d38ceaf9SAlex Deucher 
641d38ceaf9SAlex Deucher /**
642d38ceaf9SAlex Deucher  * amdgpu_acpi_pcie_performance_request
643d38ceaf9SAlex Deucher  *
644d38ceaf9SAlex Deucher  * @adev: amdgpu_device pointer
645d38ceaf9SAlex Deucher  * @perf_req: requested perf level (pcie gen speed)
646d38ceaf9SAlex Deucher  * @advertise: set advertise caps flag if set
647d38ceaf9SAlex Deucher  *
648d38ceaf9SAlex Deucher  * Executes the PCIE_PERFORMANCE_REQUEST method to
649d38ceaf9SAlex Deucher  * change the pcie gen speed (all asics).
650d38ceaf9SAlex Deucher  * returns 0 on success, error on failure.
651d38ceaf9SAlex Deucher  */
652d38ceaf9SAlex Deucher int amdgpu_acpi_pcie_performance_request(struct amdgpu_device *adev,
653d38ceaf9SAlex Deucher 					 u8 perf_req, bool advertise)
654d38ceaf9SAlex Deucher {
655d38ceaf9SAlex Deucher 	union acpi_object *info;
656f9b7f370SAlex Deucher 	struct amdgpu_atcs *atcs = &amdgpu_acpi_priv.atcs;
657d38ceaf9SAlex Deucher 	struct atcs_pref_req_input atcs_input;
658d38ceaf9SAlex Deucher 	struct atcs_pref_req_output atcs_output;
659d38ceaf9SAlex Deucher 	struct acpi_buffer params;
660d38ceaf9SAlex Deucher 	size_t size;
661d38ceaf9SAlex Deucher 	u32 retry = 3;
662d38ceaf9SAlex Deucher 
66377bf762fSAlex Deucher 	if (amdgpu_acpi_pcie_notify_device_ready(adev))
664d38ceaf9SAlex Deucher 		return -EINVAL;
665d38ceaf9SAlex Deucher 
666d38ceaf9SAlex Deucher 	if (!atcs->functions.pcie_perf_req)
667d38ceaf9SAlex Deucher 		return -EINVAL;
668d38ceaf9SAlex Deucher 
669d38ceaf9SAlex Deucher 	atcs_input.size = sizeof(struct atcs_pref_req_input);
670d38ceaf9SAlex Deucher 	/* client id (bit 2-0: func num, 7-3: dev num, 15-8: bus num) */
671d38ceaf9SAlex Deucher 	atcs_input.client_id = adev->pdev->devfn | (adev->pdev->bus->number << 8);
672d38ceaf9SAlex Deucher 	atcs_input.valid_flags_mask = ATCS_VALID_FLAGS_MASK;
673d38ceaf9SAlex Deucher 	atcs_input.flags = ATCS_WAIT_FOR_COMPLETION;
674d38ceaf9SAlex Deucher 	if (advertise)
675d38ceaf9SAlex Deucher 		atcs_input.flags |= ATCS_ADVERTISE_CAPS;
676d38ceaf9SAlex Deucher 	atcs_input.req_type = ATCS_PCIE_LINK_SPEED;
677d38ceaf9SAlex Deucher 	atcs_input.perf_req = perf_req;
678d38ceaf9SAlex Deucher 
679d38ceaf9SAlex Deucher 	params.length = sizeof(struct atcs_pref_req_input);
680d38ceaf9SAlex Deucher 	params.pointer = &atcs_input;
681d38ceaf9SAlex Deucher 
682d38ceaf9SAlex Deucher 	while (retry--) {
68377bf762fSAlex Deucher 		info = amdgpu_atcs_call(atcs, ATCS_FUNCTION_PCIE_PERFORMANCE_REQUEST, &params);
684d38ceaf9SAlex Deucher 		if (!info)
685d38ceaf9SAlex Deucher 			return -EIO;
686d38ceaf9SAlex Deucher 
687d38ceaf9SAlex Deucher 		memset(&atcs_output, 0, sizeof(atcs_output));
688d38ceaf9SAlex Deucher 
689d38ceaf9SAlex Deucher 		size = *(u16 *) info->buffer.pointer;
690d38ceaf9SAlex Deucher 		if (size < 3) {
691d38ceaf9SAlex Deucher 			DRM_INFO("ATCS buffer is too small: %zu\n", size);
692d38ceaf9SAlex Deucher 			kfree(info);
693d38ceaf9SAlex Deucher 			return -EINVAL;
694d38ceaf9SAlex Deucher 		}
695d38ceaf9SAlex Deucher 		size = min(sizeof(atcs_output), size);
696d38ceaf9SAlex Deucher 
697d38ceaf9SAlex Deucher 		memcpy(&atcs_output, info->buffer.pointer, size);
698d38ceaf9SAlex Deucher 
699d38ceaf9SAlex Deucher 		kfree(info);
700d38ceaf9SAlex Deucher 
701d38ceaf9SAlex Deucher 		switch (atcs_output.ret_val) {
702d38ceaf9SAlex Deucher 		case ATCS_REQUEST_REFUSED:
703d38ceaf9SAlex Deucher 		default:
704d38ceaf9SAlex Deucher 			return -EINVAL;
705d38ceaf9SAlex Deucher 		case ATCS_REQUEST_COMPLETE:
706d38ceaf9SAlex Deucher 			return 0;
707d38ceaf9SAlex Deucher 		case ATCS_REQUEST_IN_PROGRESS:
708d38ceaf9SAlex Deucher 			udelay(10);
709d38ceaf9SAlex Deucher 			break;
710d38ceaf9SAlex Deucher 		}
711d38ceaf9SAlex Deucher 	}
712d38ceaf9SAlex Deucher 
713d38ceaf9SAlex Deucher 	return 0;
714d38ceaf9SAlex Deucher }
715d38ceaf9SAlex Deucher 
716d38ceaf9SAlex Deucher /**
717*16eb48c6SSathishkumar S  * amdgpu_acpi_power_shift_control
718*16eb48c6SSathishkumar S  *
719*16eb48c6SSathishkumar S  * @adev: amdgpu_device pointer
720*16eb48c6SSathishkumar S  * @dev_state: device acpi state
721*16eb48c6SSathishkumar S  * @drv_state: driver state
722*16eb48c6SSathishkumar S  *
723*16eb48c6SSathishkumar S  * Executes the POWER_SHIFT_CONTROL method to
724*16eb48c6SSathishkumar S  * communicate current dGPU device state and
725*16eb48c6SSathishkumar S  * driver state to APU/SBIOS.
726*16eb48c6SSathishkumar S  * returns 0 on success, error on failure.
727*16eb48c6SSathishkumar S  */
728*16eb48c6SSathishkumar S int amdgpu_acpi_power_shift_control(struct amdgpu_device *adev,
729*16eb48c6SSathishkumar S 				    u8 dev_state, bool drv_state)
730*16eb48c6SSathishkumar S {
731*16eb48c6SSathishkumar S 	union acpi_object *info;
732*16eb48c6SSathishkumar S 	struct amdgpu_atcs *atcs = &amdgpu_acpi_priv.atcs;
733*16eb48c6SSathishkumar S 	struct atcs_pwr_shift_input atcs_input;
734*16eb48c6SSathishkumar S 	struct acpi_buffer params;
735*16eb48c6SSathishkumar S 
736*16eb48c6SSathishkumar S 	if (!amdgpu_acpi_is_power_shift_control_supported())
737*16eb48c6SSathishkumar S 		return -EINVAL;
738*16eb48c6SSathishkumar S 
739*16eb48c6SSathishkumar S 	atcs_input.size = sizeof(struct atcs_pwr_shift_input);
740*16eb48c6SSathishkumar S 	/* dGPU id (bit 2-0: func num, 7-3: dev num, 15-8: bus num) */
741*16eb48c6SSathishkumar S 	atcs_input.dgpu_id = adev->pdev->devfn | (adev->pdev->bus->number << 8);
742*16eb48c6SSathishkumar S 	atcs_input.dev_acpi_state = dev_state;
743*16eb48c6SSathishkumar S 	atcs_input.drv_state = drv_state;
744*16eb48c6SSathishkumar S 
745*16eb48c6SSathishkumar S 	params.length = sizeof(struct atcs_pwr_shift_input);
746*16eb48c6SSathishkumar S 	params.pointer = &atcs_input;
747*16eb48c6SSathishkumar S 
748*16eb48c6SSathishkumar S 	info = amdgpu_atcs_call(atcs, ATCS_FUNCTION_POWER_SHIFT_CONTROL, &params);
749*16eb48c6SSathishkumar S 	if (!info) {
750*16eb48c6SSathishkumar S 		DRM_ERROR("ATCS PSC update failed\n");
751*16eb48c6SSathishkumar S 		return -EIO;
752*16eb48c6SSathishkumar S 	}
753*16eb48c6SSathishkumar S 
754*16eb48c6SSathishkumar S 	return 0;
755*16eb48c6SSathishkumar S }
756*16eb48c6SSathishkumar S 
757*16eb48c6SSathishkumar S /**
758d38ceaf9SAlex Deucher  * amdgpu_acpi_event - handle notify events
759d38ceaf9SAlex Deucher  *
760d38ceaf9SAlex Deucher  * @nb: notifier block
761d38ceaf9SAlex Deucher  * @val: val
762d38ceaf9SAlex Deucher  * @data: acpi event
763d38ceaf9SAlex Deucher  *
764d38ceaf9SAlex Deucher  * Calls relevant amdgpu functions in response to various
765d38ceaf9SAlex Deucher  * acpi events.
766d38ceaf9SAlex Deucher  * Returns NOTIFY code
767d38ceaf9SAlex Deucher  */
768d38ceaf9SAlex Deucher static int amdgpu_acpi_event(struct notifier_block *nb,
769d38ceaf9SAlex Deucher 			     unsigned long val,
770d38ceaf9SAlex Deucher 			     void *data)
771d38ceaf9SAlex Deucher {
772d38ceaf9SAlex Deucher 	struct amdgpu_device *adev = container_of(nb, struct amdgpu_device, acpi_nb);
773d38ceaf9SAlex Deucher 	struct acpi_bus_event *entry = (struct acpi_bus_event *)data;
774d38ceaf9SAlex Deucher 
775d38ceaf9SAlex Deucher 	if (strcmp(entry->device_class, ACPI_AC_CLASS) == 0) {
776d38ceaf9SAlex Deucher 		if (power_supply_is_system_supplied() > 0)
777d38ceaf9SAlex Deucher 			DRM_DEBUG_DRIVER("pm: AC\n");
778d38ceaf9SAlex Deucher 		else
779d38ceaf9SAlex Deucher 			DRM_DEBUG_DRIVER("pm: DC\n");
780d38ceaf9SAlex Deucher 
781d38ceaf9SAlex Deucher 		amdgpu_pm_acpi_event_handler(adev);
782d38ceaf9SAlex Deucher 	}
783d38ceaf9SAlex Deucher 
784d38ceaf9SAlex Deucher 	/* Check for pending SBIOS requests */
785d38ceaf9SAlex Deucher 	return amdgpu_atif_handler(adev, entry);
786d38ceaf9SAlex Deucher }
787d38ceaf9SAlex Deucher 
788d38ceaf9SAlex Deucher /* Call all ACPI methods here */
789d38ceaf9SAlex Deucher /**
790d38ceaf9SAlex Deucher  * amdgpu_acpi_init - init driver acpi support
791d38ceaf9SAlex Deucher  *
792d38ceaf9SAlex Deucher  * @adev: amdgpu_device pointer
793d38ceaf9SAlex Deucher  *
794d38ceaf9SAlex Deucher  * Verifies the AMD ACPI interfaces and registers with the acpi
795d38ceaf9SAlex Deucher  * notifier chain (all asics).
796d38ceaf9SAlex Deucher  * Returns 0 on success, error on failure.
797d38ceaf9SAlex Deucher  */
798d38ceaf9SAlex Deucher int amdgpu_acpi_init(struct amdgpu_device *adev)
799d38ceaf9SAlex Deucher {
800f9b7f370SAlex Deucher 	struct amdgpu_atif *atif = &amdgpu_acpi_priv.atif;
801d38ceaf9SAlex Deucher 
80297d798b2SAlex Deucher #if defined(CONFIG_BACKLIGHT_CLASS_DEVICE) || defined(CONFIG_BACKLIGHT_CLASS_DEVICE_MODULE)
8039c27bc97SYe Bin 	if (atif->notifications.brightness_change) {
80497d798b2SAlex Deucher 		if (amdgpu_device_has_dc_support(adev)) {
80597d798b2SAlex Deucher #if defined(CONFIG_DRM_AMD_DC)
80697d798b2SAlex Deucher 			struct amdgpu_display_manager *dm = &adev->dm;
80777bf762fSAlex Deucher 			if (dm->backlight_dev)
80897d798b2SAlex Deucher 				atif->bd = dm->backlight_dev;
80997d798b2SAlex Deucher #endif
81097d798b2SAlex Deucher 		} else {
811d38ceaf9SAlex Deucher 			struct drm_encoder *tmp;
812d38ceaf9SAlex Deucher 
813d38ceaf9SAlex Deucher 			/* Find the encoder controlling the brightness */
8144a580877SLuben Tuikov 			list_for_each_entry(tmp, &adev_to_drm(adev)->mode_config.encoder_list,
815d38ceaf9SAlex Deucher 					    head) {
816d38ceaf9SAlex Deucher 				struct amdgpu_encoder *enc = to_amdgpu_encoder(tmp);
817d38ceaf9SAlex Deucher 
818d38ceaf9SAlex Deucher 				if ((enc->devices & (ATOM_DEVICE_LCD_SUPPORT)) &&
819d38ceaf9SAlex Deucher 				    enc->enc_priv) {
820d38ceaf9SAlex Deucher 					struct amdgpu_encoder_atom_dig *dig = enc->enc_priv;
821d38ceaf9SAlex Deucher 					if (dig->bl_dev) {
82297d798b2SAlex Deucher 						atif->bd = dig->bl_dev;
823d38ceaf9SAlex Deucher 						break;
824d38ceaf9SAlex Deucher 					}
825d38ceaf9SAlex Deucher 				}
826d38ceaf9SAlex Deucher 			}
827d38ceaf9SAlex Deucher 		}
82897d798b2SAlex Deucher 	}
82997d798b2SAlex Deucher #endif
830f9b7f370SAlex Deucher 	adev->acpi_nb.notifier_call = amdgpu_acpi_event;
831f9b7f370SAlex Deucher 	register_acpi_notifier(&adev->acpi_nb);
832f9b7f370SAlex Deucher 
833f9b7f370SAlex Deucher 	return 0;
834f9b7f370SAlex Deucher }
835f9b7f370SAlex Deucher 
836f9b7f370SAlex Deucher void amdgpu_acpi_get_backlight_caps(struct amdgpu_dm_backlight_caps *caps)
837f9b7f370SAlex Deucher {
838f9b7f370SAlex Deucher 	struct amdgpu_atif *atif = &amdgpu_acpi_priv.atif;
839f9b7f370SAlex Deucher 
840f9b7f370SAlex Deucher 	caps->caps_valid = atif->backlight_caps.caps_valid;
841f9b7f370SAlex Deucher 	caps->min_input_signal = atif->backlight_caps.min_input_signal;
842f9b7f370SAlex Deucher 	caps->max_input_signal = atif->backlight_caps.max_input_signal;
843f9b7f370SAlex Deucher }
844f9b7f370SAlex Deucher 
845f9b7f370SAlex Deucher /**
846f9b7f370SAlex Deucher  * amdgpu_acpi_fini - tear down driver acpi support
847f9b7f370SAlex Deucher  *
848f9b7f370SAlex Deucher  * @adev: amdgpu_device pointer
849f9b7f370SAlex Deucher  *
850f9b7f370SAlex Deucher  * Unregisters with the acpi notifier chain (all asics).
851f9b7f370SAlex Deucher  */
852f9b7f370SAlex Deucher void amdgpu_acpi_fini(struct amdgpu_device *adev)
853f9b7f370SAlex Deucher {
854f9b7f370SAlex Deucher 	unregister_acpi_notifier(&adev->acpi_nb);
855f9b7f370SAlex Deucher }
856f9b7f370SAlex Deucher 
857f9b7f370SAlex Deucher /**
858f9b7f370SAlex Deucher  * amdgpu_atif_pci_probe_handle - look up the ATIF handle
859f9b7f370SAlex Deucher  *
860f9b7f370SAlex Deucher  * @pdev: pci device
861f9b7f370SAlex Deucher  *
862f9b7f370SAlex Deucher  * Look up the ATIF handles (all asics).
863f9b7f370SAlex Deucher  * Returns true if the handle is found, false if not.
864f9b7f370SAlex Deucher  */
865f9b7f370SAlex Deucher static bool amdgpu_atif_pci_probe_handle(struct pci_dev *pdev)
866f9b7f370SAlex Deucher {
867f9b7f370SAlex Deucher 	char acpi_method_name[255] = { 0 };
868f9b7f370SAlex Deucher 	struct acpi_buffer buffer = {sizeof(acpi_method_name), acpi_method_name};
869f9b7f370SAlex Deucher 	acpi_handle dhandle, atif_handle;
870f9b7f370SAlex Deucher 	acpi_status status;
871f9b7f370SAlex Deucher 	int ret;
872f9b7f370SAlex Deucher 
873f9b7f370SAlex Deucher 	dhandle = ACPI_HANDLE(&pdev->dev);
874f9b7f370SAlex Deucher 	if (!dhandle)
875f9b7f370SAlex Deucher 		return false;
876f9b7f370SAlex Deucher 
877f9b7f370SAlex Deucher 	status = acpi_get_handle(dhandle, "ATIF", &atif_handle);
878f9b7f370SAlex Deucher 	if (ACPI_FAILURE(status)) {
879f9b7f370SAlex Deucher 		return false;
880f9b7f370SAlex Deucher 	}
881f9b7f370SAlex Deucher 	amdgpu_acpi_priv.atif.handle = atif_handle;
882f9b7f370SAlex Deucher 	acpi_get_name(amdgpu_acpi_priv.atif.handle, ACPI_FULL_PATHNAME, &buffer);
883f9b7f370SAlex Deucher 	DRM_DEBUG_DRIVER("Found ATIF handle %s\n", acpi_method_name);
884f9b7f370SAlex Deucher 	ret = amdgpu_atif_verify_interface(&amdgpu_acpi_priv.atif);
885f9b7f370SAlex Deucher 	if (ret) {
886f9b7f370SAlex Deucher 		amdgpu_acpi_priv.atif.handle = 0;
887f9b7f370SAlex Deucher 		return false;
888f9b7f370SAlex Deucher 	}
889f9b7f370SAlex Deucher 	return true;
890f9b7f370SAlex Deucher }
891f9b7f370SAlex Deucher 
892f9b7f370SAlex Deucher /**
893f9b7f370SAlex Deucher  * amdgpu_atcs_pci_probe_handle - look up the ATCS handle
894f9b7f370SAlex Deucher  *
895f9b7f370SAlex Deucher  * @pdev: pci device
896f9b7f370SAlex Deucher  *
897f9b7f370SAlex Deucher  * Look up the ATCS handles (all asics).
898f9b7f370SAlex Deucher  * Returns true if the handle is found, false if not.
899f9b7f370SAlex Deucher  */
900f9b7f370SAlex Deucher static bool amdgpu_atcs_pci_probe_handle(struct pci_dev *pdev)
901f9b7f370SAlex Deucher {
902f9b7f370SAlex Deucher 	char acpi_method_name[255] = { 0 };
903f9b7f370SAlex Deucher 	struct acpi_buffer buffer = { sizeof(acpi_method_name), acpi_method_name };
904f9b7f370SAlex Deucher 	acpi_handle dhandle, atcs_handle;
905f9b7f370SAlex Deucher 	acpi_status status;
906f9b7f370SAlex Deucher 	int ret;
907f9b7f370SAlex Deucher 
908f9b7f370SAlex Deucher 	dhandle = ACPI_HANDLE(&pdev->dev);
909f9b7f370SAlex Deucher 	if (!dhandle)
910f9b7f370SAlex Deucher 		return false;
911f9b7f370SAlex Deucher 
912f9b7f370SAlex Deucher 	status = acpi_get_handle(dhandle, "ATCS", &atcs_handle);
913f9b7f370SAlex Deucher 	if (ACPI_FAILURE(status)) {
914f9b7f370SAlex Deucher 		return false;
915f9b7f370SAlex Deucher 	}
916f9b7f370SAlex Deucher 	amdgpu_acpi_priv.atcs.handle = atcs_handle;
917f9b7f370SAlex Deucher 	acpi_get_name(amdgpu_acpi_priv.atcs.handle, ACPI_FULL_PATHNAME, &buffer);
918f9b7f370SAlex Deucher 	DRM_DEBUG_DRIVER("Found ATCS handle %s\n", acpi_method_name);
919f9b7f370SAlex Deucher 	ret = amdgpu_atcs_verify_interface(&amdgpu_acpi_priv.atcs);
920f9b7f370SAlex Deucher 	if (ret) {
921f9b7f370SAlex Deucher 		amdgpu_acpi_priv.atcs.handle = 0;
922f9b7f370SAlex Deucher 		return false;
923f9b7f370SAlex Deucher 	}
924f9b7f370SAlex Deucher 	return true;
925f9b7f370SAlex Deucher }
926f9b7f370SAlex Deucher 
927f9b7f370SAlex Deucher /*
928f9b7f370SAlex Deucher  * amdgpu_acpi_detect - detect ACPI ATIF/ATCS methods
929f9b7f370SAlex Deucher  *
930f9b7f370SAlex Deucher  * Check if we have the ATIF/ATCS methods and populate
931f9b7f370SAlex Deucher  * the structures in the driver.
932f9b7f370SAlex Deucher  */
933f9b7f370SAlex Deucher void amdgpu_acpi_detect(void)
934f9b7f370SAlex Deucher {
935f9b7f370SAlex Deucher 	struct amdgpu_atif *atif = &amdgpu_acpi_priv.atif;
936f9b7f370SAlex Deucher 	struct amdgpu_atcs *atcs = &amdgpu_acpi_priv.atcs;
937f9b7f370SAlex Deucher 	struct pci_dev *pdev = NULL;
938f9b7f370SAlex Deucher 	int ret;
939f9b7f370SAlex Deucher 
940f9b7f370SAlex Deucher 	while ((pdev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, pdev)) != NULL) {
941f9b7f370SAlex Deucher 		if (!atif->handle)
942f9b7f370SAlex Deucher 			amdgpu_atif_pci_probe_handle(pdev);
943f9b7f370SAlex Deucher 		if (!atcs->handle)
944f9b7f370SAlex Deucher 			amdgpu_atcs_pci_probe_handle(pdev);
945f9b7f370SAlex Deucher 	}
946f9b7f370SAlex Deucher 
947f9b7f370SAlex Deucher 	while ((pdev = pci_get_class(PCI_CLASS_DISPLAY_OTHER << 8, pdev)) != NULL) {
948f9b7f370SAlex Deucher 		if (!atif->handle)
949f9b7f370SAlex Deucher 			amdgpu_atif_pci_probe_handle(pdev);
950f9b7f370SAlex Deucher 		if (!atcs->handle)
951f9b7f370SAlex Deucher 			amdgpu_atcs_pci_probe_handle(pdev);
952f9b7f370SAlex Deucher 	}
953d38ceaf9SAlex Deucher 
954d38ceaf9SAlex Deucher 	if (atif->functions.sbios_requests && !atif->functions.system_params) {
955d38ceaf9SAlex Deucher 		/* XXX check this workraround, if sbios request function is
956d38ceaf9SAlex Deucher 		 * present we have to see how it's configured in the system
957d38ceaf9SAlex Deucher 		 * params
958d38ceaf9SAlex Deucher 		 */
959d38ceaf9SAlex Deucher 		atif->functions.system_params = true;
960d38ceaf9SAlex Deucher 	}
961d38ceaf9SAlex Deucher 
962d38ceaf9SAlex Deucher 	if (atif->functions.system_params) {
963280cf1a9SLyude Paul 		ret = amdgpu_atif_get_notification_params(atif);
964d38ceaf9SAlex Deucher 		if (ret) {
965d38ceaf9SAlex Deucher 			DRM_DEBUG_DRIVER("Call to GET_SYSTEM_PARAMS failed: %d\n",
966d38ceaf9SAlex Deucher 					ret);
967d38ceaf9SAlex Deucher 			/* Disable notification */
968d38ceaf9SAlex Deucher 			atif->notification_cfg.enabled = false;
969d38ceaf9SAlex Deucher 		}
970d38ceaf9SAlex Deucher 	}
971d38ceaf9SAlex Deucher 
972206bbafeSDavid Francis 	if (atif->functions.query_backlight_transfer_characteristics) {
973206bbafeSDavid Francis 		ret = amdgpu_atif_query_backlight_caps(atif);
974206bbafeSDavid Francis 		if (ret) {
975206bbafeSDavid Francis 			DRM_DEBUG_DRIVER("Call to QUERY_BACKLIGHT_TRANSFER_CHARACTERISTICS failed: %d\n",
976206bbafeSDavid Francis 					ret);
977206bbafeSDavid Francis 			atif->backlight_caps.caps_valid = false;
978206bbafeSDavid Francis 		}
979206bbafeSDavid Francis 	} else {
980206bbafeSDavid Francis 		atif->backlight_caps.caps_valid = false;
981206bbafeSDavid Francis 	}
982d38ceaf9SAlex Deucher }
9834cd078dcSPrike Liang 
9844cd078dcSPrike Liang /**
9854cd078dcSPrike Liang  * amdgpu_acpi_is_s0ix_supported
9864cd078dcSPrike Liang  *
9871fdbbc12SFabio M. De Francesco  * @adev: amdgpu_device_pointer
9881fdbbc12SFabio M. De Francesco  *
9894cd078dcSPrike Liang  * returns true if supported, false if not.
9904cd078dcSPrike Liang  */
9919ca5b8a1SLikun Gao bool amdgpu_acpi_is_s0ix_supported(struct amdgpu_device *adev)
9924cd078dcSPrike Liang {
993a5cb3c1aSAlex Deucher #if defined(CONFIG_AMD_PMC) || defined(CONFIG_AMD_PMC_MODULE)
9949ca5b8a1SLikun Gao 	if (acpi_gbl_FADT.flags & ACPI_FADT_LOW_POWER_S0) {
9959ca5b8a1SLikun Gao 		if (adev->flags & AMD_IS_APU)
9964cd078dcSPrike Liang 			return true;
9979ca5b8a1SLikun Gao 	}
99831ada99bSAlex Deucher #endif
9994cd078dcSPrike Liang 	return false;
10004cd078dcSPrike Liang }
1001