xref: /linux/drivers/gpu/drm/amd/amdgpu/amdgpu_acpi.c (revision 4d5275ab0b18d17697392aafd93e206e6b9de647)
1556bdae3SJingyu Wang // SPDX-License-Identifier: MIT
2d38ceaf9SAlex Deucher /*
3d38ceaf9SAlex Deucher  * Copyright 2012 Advanced Micro Devices, Inc.
4d38ceaf9SAlex Deucher  *
5d38ceaf9SAlex Deucher  * Permission is hereby granted, free of charge, to any person obtaining a
6d38ceaf9SAlex Deucher  * copy of this software and associated documentation files (the "Software"),
7d38ceaf9SAlex Deucher  * to deal in the Software without restriction, including without limitation
8d38ceaf9SAlex Deucher  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9d38ceaf9SAlex Deucher  * and/or sell copies of the Software, and to permit persons to whom the
10d38ceaf9SAlex Deucher  * Software is furnished to do so, subject to the following conditions:
11d38ceaf9SAlex Deucher  *
12d38ceaf9SAlex Deucher  * The above copyright notice and this permission notice shall be included in
13d38ceaf9SAlex Deucher  * all copies or substantial portions of the Software.
14d38ceaf9SAlex Deucher  *
15d38ceaf9SAlex Deucher  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16d38ceaf9SAlex Deucher  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17d38ceaf9SAlex Deucher  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18d38ceaf9SAlex Deucher  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
19d38ceaf9SAlex Deucher  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20d38ceaf9SAlex Deucher  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21d38ceaf9SAlex Deucher  * OTHER DEALINGS IN THE SOFTWARE.
22d38ceaf9SAlex Deucher  *
23d38ceaf9SAlex Deucher  */
24d38ceaf9SAlex Deucher 
25d38ceaf9SAlex Deucher #include <linux/pci.h>
26d38ceaf9SAlex Deucher #include <linux/acpi.h>
27a6276e92SThomas Zimmermann #include <linux/backlight.h>
28d38ceaf9SAlex Deucher #include <linux/slab.h>
29d38ceaf9SAlex Deucher #include <linux/power_supply.h>
301b0f568dSAlex Deucher #include <linux/pm_runtime.h>
3191b03fc6SPratik Vishwakarma #include <linux/suspend.h>
32d38ceaf9SAlex Deucher #include <acpi/video.h>
334cd078dcSPrike Liang #include <acpi/actbl.h>
34fdf2f6c5SSam Ravnborg 
35d38ceaf9SAlex Deucher #include "amdgpu.h"
3698c65108SJean Delvare #include "amdgpu_pm.h"
375df58525SHuang Rui #include "amdgpu_display.h"
3866dc0dddSRex Zhu #include "amd_acpi.h"
39d38ceaf9SAlex Deucher #include "atom.h"
40d38ceaf9SAlex Deucher 
41*4d5275abSLijo Lazar /* Declare GUID for AMD _DSM method for XCCs */
42*4d5275abSLijo Lazar static const guid_t amd_xcc_dsm_guid = GUID_INIT(0x8267f5d5, 0xa556, 0x44f2,
43*4d5275abSLijo Lazar 						 0xb8, 0xb4, 0x45, 0x56, 0x2e,
44*4d5275abSLijo Lazar 						 0x8c, 0x5b, 0xec);
45*4d5275abSLijo Lazar 
46*4d5275abSLijo Lazar #define AMD_XCC_HID_START 3000
47*4d5275abSLijo Lazar #define AMD_XCC_DSM_GET_NUM_FUNCS 0
48*4d5275abSLijo Lazar #define AMD_XCC_DSM_GET_SUPP_MODE 1
49*4d5275abSLijo Lazar #define AMD_XCC_DSM_GET_XCP_MODE 2
50*4d5275abSLijo Lazar #define AMD_XCC_DSM_GET_VF_XCC_MAPPING 4
51*4d5275abSLijo Lazar #define AMD_XCC_DSM_GET_TMR_INFO 5
52*4d5275abSLijo Lazar #define AMD_XCC_DSM_NUM_FUNCS 5
53*4d5275abSLijo Lazar 
54*4d5275abSLijo Lazar #define AMD_XCC_MAX_HID 24
55*4d5275abSLijo Lazar 
56*4d5275abSLijo Lazar /* Encapsulates the XCD acpi object information */
57*4d5275abSLijo Lazar struct amdgpu_acpi_xcc_info {
58*4d5275abSLijo Lazar 	struct list_head list;
59*4d5275abSLijo Lazar 	int mem_node;
60*4d5275abSLijo Lazar 	uint8_t xcp_node;
61*4d5275abSLijo Lazar 	uint8_t phy_id;
62*4d5275abSLijo Lazar 	acpi_handle handle;
63*4d5275abSLijo Lazar };
64*4d5275abSLijo Lazar 
65*4d5275abSLijo Lazar struct amdgpu_acpi_dev_info {
66*4d5275abSLijo Lazar 	struct list_head list;
67*4d5275abSLijo Lazar 	struct list_head xcc_list;
68*4d5275abSLijo Lazar 	uint16_t bdf;
69*4d5275abSLijo Lazar 	uint16_t supp_xcp_mode;
70*4d5275abSLijo Lazar 	uint16_t xcp_mode;
71*4d5275abSLijo Lazar 	uint16_t mem_mode;
72*4d5275abSLijo Lazar 	uint64_t tmr_base;
73*4d5275abSLijo Lazar 	uint64_t tmr_size;
74*4d5275abSLijo Lazar };
75*4d5275abSLijo Lazar 
76*4d5275abSLijo Lazar struct list_head amdgpu_acpi_dev_list;
77*4d5275abSLijo Lazar 
78102c16a0SLyude Paul struct amdgpu_atif_notification_cfg {
79102c16a0SLyude Paul 	bool enabled;
80102c16a0SLyude Paul 	int command_code;
81102c16a0SLyude Paul };
82102c16a0SLyude Paul 
83102c16a0SLyude Paul struct amdgpu_atif_notifications {
84102c16a0SLyude Paul 	bool thermal_state;
85102c16a0SLyude Paul 	bool forced_power_state;
86102c16a0SLyude Paul 	bool system_power_state;
87102c16a0SLyude Paul 	bool brightness_change;
88102c16a0SLyude Paul 	bool dgpu_display_event;
897349a3afSDavid Francis 	bool gpu_package_power_limit;
90102c16a0SLyude Paul };
91102c16a0SLyude Paul 
92102c16a0SLyude Paul struct amdgpu_atif_functions {
93102c16a0SLyude Paul 	bool system_params;
94102c16a0SLyude Paul 	bool sbios_requests;
95102c16a0SLyude Paul 	bool temperature_change;
967349a3afSDavid Francis 	bool query_backlight_transfer_characteristics;
977349a3afSDavid Francis 	bool ready_to_undock;
987349a3afSDavid Francis 	bool external_gpu_information;
99102c16a0SLyude Paul };
100102c16a0SLyude Paul 
101102c16a0SLyude Paul struct amdgpu_atif {
102280cf1a9SLyude Paul 	acpi_handle handle;
103280cf1a9SLyude Paul 
104102c16a0SLyude Paul 	struct amdgpu_atif_notifications notifications;
105102c16a0SLyude Paul 	struct amdgpu_atif_functions functions;
106102c16a0SLyude Paul 	struct amdgpu_atif_notification_cfg notification_cfg;
10797d798b2SAlex Deucher 	struct backlight_device *bd;
108206bbafeSDavid Francis 	struct amdgpu_dm_backlight_caps backlight_caps;
109102c16a0SLyude Paul };
110102c16a0SLyude Paul 
11177bf762fSAlex Deucher struct amdgpu_atcs_functions {
11277bf762fSAlex Deucher 	bool get_ext_state;
11377bf762fSAlex Deucher 	bool pcie_perf_req;
11477bf762fSAlex Deucher 	bool pcie_dev_rdy;
11577bf762fSAlex Deucher 	bool pcie_bus_width;
11616eb48c6SSathishkumar S 	bool power_shift_control;
11777bf762fSAlex Deucher };
11877bf762fSAlex Deucher 
11977bf762fSAlex Deucher struct amdgpu_atcs {
12077bf762fSAlex Deucher 	acpi_handle handle;
12177bf762fSAlex Deucher 
12277bf762fSAlex Deucher 	struct amdgpu_atcs_functions functions;
12377bf762fSAlex Deucher };
12477bf762fSAlex Deucher 
125f9b7f370SAlex Deucher static struct amdgpu_acpi_priv {
126f9b7f370SAlex Deucher 	struct amdgpu_atif atif;
127f9b7f370SAlex Deucher 	struct amdgpu_atcs atcs;
128f9b7f370SAlex Deucher } amdgpu_acpi_priv;
129f9b7f370SAlex Deucher 
130d38ceaf9SAlex Deucher /* Call the ATIF method
131d38ceaf9SAlex Deucher  */
132d38ceaf9SAlex Deucher /**
133d38ceaf9SAlex Deucher  * amdgpu_atif_call - call an ATIF method
134d38ceaf9SAlex Deucher  *
13577bf762fSAlex Deucher  * @atif: atif structure
136d38ceaf9SAlex Deucher  * @function: the ATIF function to execute
137d38ceaf9SAlex Deucher  * @params: ATIF function params
138d38ceaf9SAlex Deucher  *
139d38ceaf9SAlex Deucher  * Executes the requested ATIF function (all asics).
140d38ceaf9SAlex Deucher  * Returns a pointer to the acpi output buffer.
141d38ceaf9SAlex Deucher  */
142280cf1a9SLyude Paul static union acpi_object *amdgpu_atif_call(struct amdgpu_atif *atif,
143280cf1a9SLyude Paul 					   int function,
144d38ceaf9SAlex Deucher 					   struct acpi_buffer *params)
145d38ceaf9SAlex Deucher {
146d38ceaf9SAlex Deucher 	acpi_status status;
147d38ceaf9SAlex Deucher 	union acpi_object atif_arg_elements[2];
148d38ceaf9SAlex Deucher 	struct acpi_object_list atif_arg;
149d38ceaf9SAlex Deucher 	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
150d38ceaf9SAlex Deucher 
151d38ceaf9SAlex Deucher 	atif_arg.count = 2;
152d38ceaf9SAlex Deucher 	atif_arg.pointer = &atif_arg_elements[0];
153d38ceaf9SAlex Deucher 
154d38ceaf9SAlex Deucher 	atif_arg_elements[0].type = ACPI_TYPE_INTEGER;
155d38ceaf9SAlex Deucher 	atif_arg_elements[0].integer.value = function;
156d38ceaf9SAlex Deucher 
157d38ceaf9SAlex Deucher 	if (params) {
158d38ceaf9SAlex Deucher 		atif_arg_elements[1].type = ACPI_TYPE_BUFFER;
159d38ceaf9SAlex Deucher 		atif_arg_elements[1].buffer.length = params->length;
160d38ceaf9SAlex Deucher 		atif_arg_elements[1].buffer.pointer = params->pointer;
161d38ceaf9SAlex Deucher 	} else {
162d38ceaf9SAlex Deucher 		/* We need a second fake parameter */
163d38ceaf9SAlex Deucher 		atif_arg_elements[1].type = ACPI_TYPE_INTEGER;
164d38ceaf9SAlex Deucher 		atif_arg_elements[1].integer.value = 0;
165d38ceaf9SAlex Deucher 	}
166d38ceaf9SAlex Deucher 
167280cf1a9SLyude Paul 	status = acpi_evaluate_object(atif->handle, NULL, &atif_arg,
168280cf1a9SLyude Paul 				      &buffer);
169d38ceaf9SAlex Deucher 
170d38ceaf9SAlex Deucher 	/* Fail only if calling the method fails and ATIF is supported */
171d38ceaf9SAlex Deucher 	if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
172d38ceaf9SAlex Deucher 		DRM_DEBUG_DRIVER("failed to evaluate ATIF got %s\n",
173d38ceaf9SAlex Deucher 				 acpi_format_exception(status));
174d38ceaf9SAlex Deucher 		kfree(buffer.pointer);
175d38ceaf9SAlex Deucher 		return NULL;
176d38ceaf9SAlex Deucher 	}
177d38ceaf9SAlex Deucher 
178d38ceaf9SAlex Deucher 	return buffer.pointer;
179d38ceaf9SAlex Deucher }
180d38ceaf9SAlex Deucher 
181d38ceaf9SAlex Deucher /**
182d38ceaf9SAlex Deucher  * amdgpu_atif_parse_notification - parse supported notifications
183d38ceaf9SAlex Deucher  *
184d38ceaf9SAlex Deucher  * @n: supported notifications struct
185d38ceaf9SAlex Deucher  * @mask: supported notifications mask from ATIF
186d38ceaf9SAlex Deucher  *
187d38ceaf9SAlex Deucher  * Use the supported notifications mask from ATIF function
188d38ceaf9SAlex Deucher  * ATIF_FUNCTION_VERIFY_INTERFACE to determine what notifications
189d38ceaf9SAlex Deucher  * are supported (all asics).
190d38ceaf9SAlex Deucher  */
191d38ceaf9SAlex Deucher static void amdgpu_atif_parse_notification(struct amdgpu_atif_notifications *n, u32 mask)
192d38ceaf9SAlex Deucher {
193d38ceaf9SAlex Deucher 	n->thermal_state = mask & ATIF_THERMAL_STATE_CHANGE_REQUEST_SUPPORTED;
194d38ceaf9SAlex Deucher 	n->forced_power_state = mask & ATIF_FORCED_POWER_STATE_CHANGE_REQUEST_SUPPORTED;
195d38ceaf9SAlex Deucher 	n->system_power_state = mask & ATIF_SYSTEM_POWER_SOURCE_CHANGE_REQUEST_SUPPORTED;
196d38ceaf9SAlex Deucher 	n->brightness_change = mask & ATIF_PANEL_BRIGHTNESS_CHANGE_REQUEST_SUPPORTED;
197d38ceaf9SAlex Deucher 	n->dgpu_display_event = mask & ATIF_DGPU_DISPLAY_EVENT_SUPPORTED;
1987349a3afSDavid Francis 	n->gpu_package_power_limit = mask & ATIF_GPU_PACKAGE_POWER_LIMIT_REQUEST_SUPPORTED;
199d38ceaf9SAlex Deucher }
200d38ceaf9SAlex Deucher 
201d38ceaf9SAlex Deucher /**
202d38ceaf9SAlex Deucher  * amdgpu_atif_parse_functions - parse supported functions
203d38ceaf9SAlex Deucher  *
204d38ceaf9SAlex Deucher  * @f: supported functions struct
205d38ceaf9SAlex Deucher  * @mask: supported functions mask from ATIF
206d38ceaf9SAlex Deucher  *
207d38ceaf9SAlex Deucher  * Use the supported functions mask from ATIF function
208d38ceaf9SAlex Deucher  * ATIF_FUNCTION_VERIFY_INTERFACE to determine what functions
209d38ceaf9SAlex Deucher  * are supported (all asics).
210d38ceaf9SAlex Deucher  */
211d38ceaf9SAlex Deucher static void amdgpu_atif_parse_functions(struct amdgpu_atif_functions *f, u32 mask)
212d38ceaf9SAlex Deucher {
213d38ceaf9SAlex Deucher 	f->system_params = mask & ATIF_GET_SYSTEM_PARAMETERS_SUPPORTED;
214d38ceaf9SAlex Deucher 	f->sbios_requests = mask & ATIF_GET_SYSTEM_BIOS_REQUESTS_SUPPORTED;
215d38ceaf9SAlex Deucher 	f->temperature_change = mask & ATIF_TEMPERATURE_CHANGE_NOTIFICATION_SUPPORTED;
2167349a3afSDavid Francis 	f->query_backlight_transfer_characteristics =
2177349a3afSDavid Francis 		mask & ATIF_QUERY_BACKLIGHT_TRANSFER_CHARACTERISTICS_SUPPORTED;
2187349a3afSDavid Francis 	f->ready_to_undock = mask & ATIF_READY_TO_UNDOCK_NOTIFICATION_SUPPORTED;
2197349a3afSDavid Francis 	f->external_gpu_information = mask & ATIF_GET_EXTERNAL_GPU_INFORMATION_SUPPORTED;
220d38ceaf9SAlex Deucher }
221d38ceaf9SAlex Deucher 
222d38ceaf9SAlex Deucher /**
223d38ceaf9SAlex Deucher  * amdgpu_atif_verify_interface - verify ATIF
224d38ceaf9SAlex Deucher  *
225d38ceaf9SAlex Deucher  * @atif: amdgpu atif struct
226d38ceaf9SAlex Deucher  *
227d38ceaf9SAlex Deucher  * Execute the ATIF_FUNCTION_VERIFY_INTERFACE ATIF function
228d38ceaf9SAlex Deucher  * to initialize ATIF and determine what features are supported
229d38ceaf9SAlex Deucher  * (all asics).
230d38ceaf9SAlex Deucher  * returns 0 on success, error on failure.
231d38ceaf9SAlex Deucher  */
232280cf1a9SLyude Paul static int amdgpu_atif_verify_interface(struct amdgpu_atif *atif)
233d38ceaf9SAlex Deucher {
234d38ceaf9SAlex Deucher 	union acpi_object *info;
235d38ceaf9SAlex Deucher 	struct atif_verify_interface output;
236d38ceaf9SAlex Deucher 	size_t size;
237d38ceaf9SAlex Deucher 	int err = 0;
238d38ceaf9SAlex Deucher 
239280cf1a9SLyude Paul 	info = amdgpu_atif_call(atif, ATIF_FUNCTION_VERIFY_INTERFACE, NULL);
240d38ceaf9SAlex Deucher 	if (!info)
241d38ceaf9SAlex Deucher 		return -EIO;
242d38ceaf9SAlex Deucher 
243d38ceaf9SAlex Deucher 	memset(&output, 0, sizeof(output));
244d38ceaf9SAlex Deucher 
245d38ceaf9SAlex Deucher 	size = *(u16 *) info->buffer.pointer;
246d38ceaf9SAlex Deucher 	if (size < 12) {
247d38ceaf9SAlex Deucher 		DRM_INFO("ATIF buffer is too small: %zu\n", size);
248d38ceaf9SAlex Deucher 		err = -EINVAL;
249d38ceaf9SAlex Deucher 		goto out;
250d38ceaf9SAlex Deucher 	}
251d38ceaf9SAlex Deucher 	size = min(sizeof(output), size);
252d38ceaf9SAlex Deucher 
253d38ceaf9SAlex Deucher 	memcpy(&output, info->buffer.pointer, size);
254d38ceaf9SAlex Deucher 
255d38ceaf9SAlex Deucher 	/* TODO: check version? */
256d38ceaf9SAlex Deucher 	DRM_DEBUG_DRIVER("ATIF version %u\n", output.version);
257d38ceaf9SAlex Deucher 
258d38ceaf9SAlex Deucher 	amdgpu_atif_parse_notification(&atif->notifications, output.notification_mask);
259d38ceaf9SAlex Deucher 	amdgpu_atif_parse_functions(&atif->functions, output.function_bits);
260d38ceaf9SAlex Deucher 
261d38ceaf9SAlex Deucher out:
262d38ceaf9SAlex Deucher 	kfree(info);
263d38ceaf9SAlex Deucher 	return err;
264d38ceaf9SAlex Deucher }
265d38ceaf9SAlex Deucher 
266d38ceaf9SAlex Deucher /**
267d38ceaf9SAlex Deucher  * amdgpu_atif_get_notification_params - determine notify configuration
268d38ceaf9SAlex Deucher  *
2691fdbbc12SFabio M. De Francesco  * @atif: acpi handle
270d38ceaf9SAlex Deucher  *
271d38ceaf9SAlex Deucher  * Execute the ATIF_FUNCTION_GET_SYSTEM_PARAMETERS ATIF function
272d38ceaf9SAlex Deucher  * to determine if a notifier is used and if so which one
273d38ceaf9SAlex Deucher  * (all asics).  This is either Notify(VGA, 0x81) or Notify(VGA, n)
274d38ceaf9SAlex Deucher  * where n is specified in the result if a notifier is used.
275d38ceaf9SAlex Deucher  * Returns 0 on success, error on failure.
276d38ceaf9SAlex Deucher  */
277280cf1a9SLyude Paul static int amdgpu_atif_get_notification_params(struct amdgpu_atif *atif)
278d38ceaf9SAlex Deucher {
279d38ceaf9SAlex Deucher 	union acpi_object *info;
280280cf1a9SLyude Paul 	struct amdgpu_atif_notification_cfg *n = &atif->notification_cfg;
281d38ceaf9SAlex Deucher 	struct atif_system_params params;
282d38ceaf9SAlex Deucher 	size_t size;
283d38ceaf9SAlex Deucher 	int err = 0;
284d38ceaf9SAlex Deucher 
285280cf1a9SLyude Paul 	info = amdgpu_atif_call(atif, ATIF_FUNCTION_GET_SYSTEM_PARAMETERS,
286280cf1a9SLyude Paul 				NULL);
287d38ceaf9SAlex Deucher 	if (!info) {
288d38ceaf9SAlex Deucher 		err = -EIO;
289d38ceaf9SAlex Deucher 		goto out;
290d38ceaf9SAlex Deucher 	}
291d38ceaf9SAlex Deucher 
292d38ceaf9SAlex Deucher 	size = *(u16 *) info->buffer.pointer;
293d38ceaf9SAlex Deucher 	if (size < 10) {
294d38ceaf9SAlex Deucher 		err = -EINVAL;
295d38ceaf9SAlex Deucher 		goto out;
296d38ceaf9SAlex Deucher 	}
297d38ceaf9SAlex Deucher 
298d38ceaf9SAlex Deucher 	memset(&params, 0, sizeof(params));
299d38ceaf9SAlex Deucher 	size = min(sizeof(params), size);
300d38ceaf9SAlex Deucher 	memcpy(&params, info->buffer.pointer, size);
301d38ceaf9SAlex Deucher 
302d38ceaf9SAlex Deucher 	DRM_DEBUG_DRIVER("SYSTEM_PARAMS: mask = %#x, flags = %#x\n",
303d38ceaf9SAlex Deucher 			params.flags, params.valid_mask);
304d38ceaf9SAlex Deucher 	params.flags = params.flags & params.valid_mask;
305d38ceaf9SAlex Deucher 
306d38ceaf9SAlex Deucher 	if ((params.flags & ATIF_NOTIFY_MASK) == ATIF_NOTIFY_NONE) {
307d38ceaf9SAlex Deucher 		n->enabled = false;
308d38ceaf9SAlex Deucher 		n->command_code = 0;
309d38ceaf9SAlex Deucher 	} else if ((params.flags & ATIF_NOTIFY_MASK) == ATIF_NOTIFY_81) {
310d38ceaf9SAlex Deucher 		n->enabled = true;
311d38ceaf9SAlex Deucher 		n->command_code = 0x81;
312d38ceaf9SAlex Deucher 	} else {
313d38ceaf9SAlex Deucher 		if (size < 11) {
314d38ceaf9SAlex Deucher 			err = -EINVAL;
315d38ceaf9SAlex Deucher 			goto out;
316d38ceaf9SAlex Deucher 		}
317d38ceaf9SAlex Deucher 		n->enabled = true;
318d38ceaf9SAlex Deucher 		n->command_code = params.command_code;
319d38ceaf9SAlex Deucher 	}
320d38ceaf9SAlex Deucher 
321d38ceaf9SAlex Deucher out:
322d38ceaf9SAlex Deucher 	DRM_DEBUG_DRIVER("Notification %s, command code = %#x\n",
323d38ceaf9SAlex Deucher 			(n->enabled ? "enabled" : "disabled"),
324d38ceaf9SAlex Deucher 			n->command_code);
325d38ceaf9SAlex Deucher 	kfree(info);
326d38ceaf9SAlex Deucher 	return err;
327d38ceaf9SAlex Deucher }
328d38ceaf9SAlex Deucher 
329d38ceaf9SAlex Deucher /**
330206bbafeSDavid Francis  * amdgpu_atif_query_backlight_caps - get min and max backlight input signal
331206bbafeSDavid Francis  *
3321fdbbc12SFabio M. De Francesco  * @atif: acpi handle
333206bbafeSDavid Francis  *
334206bbafeSDavid Francis  * Execute the QUERY_BRIGHTNESS_TRANSFER_CHARACTERISTICS ATIF function
335206bbafeSDavid Francis  * to determine the acceptable range of backlight values
336206bbafeSDavid Francis  *
337206bbafeSDavid Francis  * Backlight_caps.caps_valid will be set to true if the query is successful
338206bbafeSDavid Francis  *
339206bbafeSDavid Francis  * The input signals are in range 0-255
340206bbafeSDavid Francis  *
341206bbafeSDavid Francis  * This function assumes the display with backlight is the first LCD
342206bbafeSDavid Francis  *
343206bbafeSDavid Francis  * Returns 0 on success, error on failure.
344206bbafeSDavid Francis  */
345206bbafeSDavid Francis static int amdgpu_atif_query_backlight_caps(struct amdgpu_atif *atif)
346206bbafeSDavid Francis {
347206bbafeSDavid Francis 	union acpi_object *info;
348206bbafeSDavid Francis 	struct atif_qbtc_output characteristics;
349206bbafeSDavid Francis 	struct atif_qbtc_arguments arguments;
350206bbafeSDavid Francis 	struct acpi_buffer params;
351206bbafeSDavid Francis 	size_t size;
352206bbafeSDavid Francis 	int err = 0;
353206bbafeSDavid Francis 
354206bbafeSDavid Francis 	arguments.size = sizeof(arguments);
355206bbafeSDavid Francis 	arguments.requested_display = ATIF_QBTC_REQUEST_LCD1;
356206bbafeSDavid Francis 
357206bbafeSDavid Francis 	params.length = sizeof(arguments);
358206bbafeSDavid Francis 	params.pointer = (void *)&arguments;
359206bbafeSDavid Francis 
360206bbafeSDavid Francis 	info = amdgpu_atif_call(atif,
361206bbafeSDavid Francis 		ATIF_FUNCTION_QUERY_BRIGHTNESS_TRANSFER_CHARACTERISTICS,
362206bbafeSDavid Francis 		&params);
363206bbafeSDavid Francis 	if (!info) {
364206bbafeSDavid Francis 		err = -EIO;
365206bbafeSDavid Francis 		goto out;
366206bbafeSDavid Francis 	}
367206bbafeSDavid Francis 
368206bbafeSDavid Francis 	size = *(u16 *) info->buffer.pointer;
369206bbafeSDavid Francis 	if (size < 10) {
370206bbafeSDavid Francis 		err = -EINVAL;
371206bbafeSDavid Francis 		goto out;
372206bbafeSDavid Francis 	}
373206bbafeSDavid Francis 
374206bbafeSDavid Francis 	memset(&characteristics, 0, sizeof(characteristics));
375206bbafeSDavid Francis 	size = min(sizeof(characteristics), size);
376206bbafeSDavid Francis 	memcpy(&characteristics, info->buffer.pointer, size);
377206bbafeSDavid Francis 
378206bbafeSDavid Francis 	atif->backlight_caps.caps_valid = true;
379206bbafeSDavid Francis 	atif->backlight_caps.min_input_signal =
380206bbafeSDavid Francis 			characteristics.min_input_signal;
381206bbafeSDavid Francis 	atif->backlight_caps.max_input_signal =
382206bbafeSDavid Francis 			characteristics.max_input_signal;
383206bbafeSDavid Francis out:
384206bbafeSDavid Francis 	kfree(info);
385206bbafeSDavid Francis 	return err;
386206bbafeSDavid Francis }
387206bbafeSDavid Francis 
388206bbafeSDavid Francis /**
389d38ceaf9SAlex Deucher  * amdgpu_atif_get_sbios_requests - get requested sbios event
390d38ceaf9SAlex Deucher  *
3911fdbbc12SFabio M. De Francesco  * @atif: acpi handle
392d38ceaf9SAlex Deucher  * @req: atif sbios request struct
393d38ceaf9SAlex Deucher  *
394d38ceaf9SAlex Deucher  * Execute the ATIF_FUNCTION_GET_SYSTEM_BIOS_REQUESTS ATIF function
395d38ceaf9SAlex Deucher  * to determine what requests the sbios is making to the driver
396d38ceaf9SAlex Deucher  * (all asics).
397d38ceaf9SAlex Deucher  * Returns 0 on success, error on failure.
398d38ceaf9SAlex Deucher  */
399280cf1a9SLyude Paul static int amdgpu_atif_get_sbios_requests(struct amdgpu_atif *atif,
400d38ceaf9SAlex Deucher 					  struct atif_sbios_requests *req)
401d38ceaf9SAlex Deucher {
402d38ceaf9SAlex Deucher 	union acpi_object *info;
403d38ceaf9SAlex Deucher 	size_t size;
404d38ceaf9SAlex Deucher 	int count = 0;
405d38ceaf9SAlex Deucher 
406280cf1a9SLyude Paul 	info = amdgpu_atif_call(atif, ATIF_FUNCTION_GET_SYSTEM_BIOS_REQUESTS,
407280cf1a9SLyude Paul 				NULL);
408d38ceaf9SAlex Deucher 	if (!info)
409d38ceaf9SAlex Deucher 		return -EIO;
410d38ceaf9SAlex Deucher 
411d38ceaf9SAlex Deucher 	size = *(u16 *)info->buffer.pointer;
412d38ceaf9SAlex Deucher 	if (size < 0xd) {
413d38ceaf9SAlex Deucher 		count = -EINVAL;
414d38ceaf9SAlex Deucher 		goto out;
415d38ceaf9SAlex Deucher 	}
416d38ceaf9SAlex Deucher 	memset(req, 0, sizeof(*req));
417d38ceaf9SAlex Deucher 
418d38ceaf9SAlex Deucher 	size = min(sizeof(*req), size);
419d38ceaf9SAlex Deucher 	memcpy(req, info->buffer.pointer, size);
420d38ceaf9SAlex Deucher 	DRM_DEBUG_DRIVER("SBIOS pending requests: %#x\n", req->pending);
421d38ceaf9SAlex Deucher 
422d38ceaf9SAlex Deucher 	count = hweight32(req->pending);
423d38ceaf9SAlex Deucher 
424d38ceaf9SAlex Deucher out:
425d38ceaf9SAlex Deucher 	kfree(info);
426d38ceaf9SAlex Deucher 	return count;
427d38ceaf9SAlex Deucher }
428d38ceaf9SAlex Deucher 
429d38ceaf9SAlex Deucher /**
430d38ceaf9SAlex Deucher  * amdgpu_atif_handler - handle ATIF notify requests
431d38ceaf9SAlex Deucher  *
432d38ceaf9SAlex Deucher  * @adev: amdgpu_device pointer
433d38ceaf9SAlex Deucher  * @event: atif sbios request struct
434d38ceaf9SAlex Deucher  *
435d38ceaf9SAlex Deucher  * Checks the acpi event and if it matches an atif event,
436d38ceaf9SAlex Deucher  * handles it.
437582f58deSLyude Paul  *
438582f58deSLyude Paul  * Returns:
439582f58deSLyude Paul  * NOTIFY_BAD or NOTIFY_DONE, depending on the event.
440d38ceaf9SAlex Deucher  */
441b7cecbe8SJean Delvare static int amdgpu_atif_handler(struct amdgpu_device *adev,
442d38ceaf9SAlex Deucher 			       struct acpi_bus_event *event)
443d38ceaf9SAlex Deucher {
444f9b7f370SAlex Deucher 	struct amdgpu_atif *atif = &amdgpu_acpi_priv.atif;
445d38ceaf9SAlex Deucher 	int count;
446d38ceaf9SAlex Deucher 
447d38ceaf9SAlex Deucher 	DRM_DEBUG_DRIVER("event, device_class = %s, type = %#x\n",
448d38ceaf9SAlex Deucher 			event->device_class, event->type);
449d38ceaf9SAlex Deucher 
450d38ceaf9SAlex Deucher 	if (strcmp(event->device_class, ACPI_VIDEO_CLASS) != 0)
451d38ceaf9SAlex Deucher 		return NOTIFY_DONE;
452d38ceaf9SAlex Deucher 
453582f58deSLyude Paul 	/* Is this actually our event? */
454f9b7f370SAlex Deucher 	if (!atif->notification_cfg.enabled ||
455582f58deSLyude Paul 	    event->type != atif->notification_cfg.command_code) {
456582f58deSLyude Paul 		/* These events will generate keypresses otherwise */
457582f58deSLyude Paul 		if (event->type == ACPI_VIDEO_NOTIFY_PROBE)
458582f58deSLyude Paul 			return NOTIFY_BAD;
459582f58deSLyude Paul 		else
460d38ceaf9SAlex Deucher 			return NOTIFY_DONE;
461582f58deSLyude Paul 	}
462d38ceaf9SAlex Deucher 
4639e7204beSAlex Deucher 	if (atif->functions.sbios_requests) {
4649e7204beSAlex Deucher 		struct atif_sbios_requests req;
4659e7204beSAlex Deucher 
466d38ceaf9SAlex Deucher 		/* Check pending SBIOS requests */
467280cf1a9SLyude Paul 		count = amdgpu_atif_get_sbios_requests(atif, &req);
468d38ceaf9SAlex Deucher 
469d38ceaf9SAlex Deucher 		if (count <= 0)
470582f58deSLyude Paul 			return NOTIFY_BAD;
471d38ceaf9SAlex Deucher 
472d38ceaf9SAlex Deucher 		DRM_DEBUG_DRIVER("ATIF: %d pending SBIOS requests\n", count);
473d38ceaf9SAlex Deucher 
47497d798b2SAlex Deucher 		if (req.pending & ATIF_PANEL_BRIGHTNESS_CHANGE_REQUEST) {
47597d798b2SAlex Deucher 			if (atif->bd) {
476d38ceaf9SAlex Deucher 				DRM_DEBUG_DRIVER("Changing brightness to %d\n",
477d38ceaf9SAlex Deucher 						 req.backlight_level);
478bcb7b0efSAndriy Gapon 				/*
479bcb7b0efSAndriy Gapon 				 * XXX backlight_device_set_brightness() is
480bcb7b0efSAndriy Gapon 				 * hardwired to post BACKLIGHT_UPDATE_SYSFS.
481bcb7b0efSAndriy Gapon 				 * It probably should accept 'reason' parameter.
482bcb7b0efSAndriy Gapon 				 */
48397d798b2SAlex Deucher 				backlight_device_set_brightness(atif->bd, req.backlight_level);
484bcb7b0efSAndriy Gapon 			}
48597d798b2SAlex Deucher 		}
48697d798b2SAlex Deucher 
4871b0f568dSAlex Deucher 		if (req.pending & ATIF_DGPU_DISPLAY_EVENT) {
488bdb1ccb0SAaron Liu 			if (adev->flags & AMD_IS_PX) {
4894a580877SLuben Tuikov 				pm_runtime_get_sync(adev_to_drm(adev)->dev);
4901b0f568dSAlex Deucher 				/* Just fire off a uevent and let userspace tell us what to do */
4914a580877SLuben Tuikov 				drm_helper_hpd_irq_event(adev_to_drm(adev));
4924a580877SLuben Tuikov 				pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
4934a580877SLuben Tuikov 				pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
4941b0f568dSAlex Deucher 			}
4951b0f568dSAlex Deucher 		}
496d38ceaf9SAlex Deucher 		/* TODO: check other events */
4979e7204beSAlex Deucher 	}
498d38ceaf9SAlex Deucher 
499d38ceaf9SAlex Deucher 	/* We've handled the event, stop the notifier chain. The ACPI interface
500d38ceaf9SAlex Deucher 	 * overloads ACPI_VIDEO_NOTIFY_PROBE, we don't want to send that to
501d38ceaf9SAlex Deucher 	 * userspace if the event was generated only to signal a SBIOS
502d38ceaf9SAlex Deucher 	 * request.
503d38ceaf9SAlex Deucher 	 */
504d38ceaf9SAlex Deucher 	return NOTIFY_BAD;
505d38ceaf9SAlex Deucher }
506d38ceaf9SAlex Deucher 
507d38ceaf9SAlex Deucher /* Call the ATCS method
508d38ceaf9SAlex Deucher  */
509d38ceaf9SAlex Deucher /**
510d38ceaf9SAlex Deucher  * amdgpu_atcs_call - call an ATCS method
511d38ceaf9SAlex Deucher  *
51277bf762fSAlex Deucher  * @atcs: atcs structure
513d38ceaf9SAlex Deucher  * @function: the ATCS function to execute
514d38ceaf9SAlex Deucher  * @params: ATCS function params
515d38ceaf9SAlex Deucher  *
516d38ceaf9SAlex Deucher  * Executes the requested ATCS function (all asics).
517d38ceaf9SAlex Deucher  * Returns a pointer to the acpi output buffer.
518d38ceaf9SAlex Deucher  */
51977bf762fSAlex Deucher static union acpi_object *amdgpu_atcs_call(struct amdgpu_atcs *atcs,
52077bf762fSAlex Deucher 					   int function,
521d38ceaf9SAlex Deucher 					   struct acpi_buffer *params)
522d38ceaf9SAlex Deucher {
523d38ceaf9SAlex Deucher 	acpi_status status;
524d38ceaf9SAlex Deucher 	union acpi_object atcs_arg_elements[2];
525d38ceaf9SAlex Deucher 	struct acpi_object_list atcs_arg;
526d38ceaf9SAlex Deucher 	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
527d38ceaf9SAlex Deucher 
528d38ceaf9SAlex Deucher 	atcs_arg.count = 2;
529d38ceaf9SAlex Deucher 	atcs_arg.pointer = &atcs_arg_elements[0];
530d38ceaf9SAlex Deucher 
531d38ceaf9SAlex Deucher 	atcs_arg_elements[0].type = ACPI_TYPE_INTEGER;
532d38ceaf9SAlex Deucher 	atcs_arg_elements[0].integer.value = function;
533d38ceaf9SAlex Deucher 
534d38ceaf9SAlex Deucher 	if (params) {
535d38ceaf9SAlex Deucher 		atcs_arg_elements[1].type = ACPI_TYPE_BUFFER;
536d38ceaf9SAlex Deucher 		atcs_arg_elements[1].buffer.length = params->length;
537d38ceaf9SAlex Deucher 		atcs_arg_elements[1].buffer.pointer = params->pointer;
538d38ceaf9SAlex Deucher 	} else {
539d38ceaf9SAlex Deucher 		/* We need a second fake parameter */
540d38ceaf9SAlex Deucher 		atcs_arg_elements[1].type = ACPI_TYPE_INTEGER;
541d38ceaf9SAlex Deucher 		atcs_arg_elements[1].integer.value = 0;
542d38ceaf9SAlex Deucher 	}
543d38ceaf9SAlex Deucher 
5444965257fSAlex Deucher 	status = acpi_evaluate_object(atcs->handle, NULL, &atcs_arg, &buffer);
545d38ceaf9SAlex Deucher 
546d38ceaf9SAlex Deucher 	/* Fail only if calling the method fails and ATIF is supported */
547d38ceaf9SAlex Deucher 	if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
548d38ceaf9SAlex Deucher 		DRM_DEBUG_DRIVER("failed to evaluate ATCS got %s\n",
549d38ceaf9SAlex Deucher 				 acpi_format_exception(status));
550d38ceaf9SAlex Deucher 		kfree(buffer.pointer);
551d38ceaf9SAlex Deucher 		return NULL;
552d38ceaf9SAlex Deucher 	}
553d38ceaf9SAlex Deucher 
554d38ceaf9SAlex Deucher 	return buffer.pointer;
555d38ceaf9SAlex Deucher }
556d38ceaf9SAlex Deucher 
557d38ceaf9SAlex Deucher /**
558d38ceaf9SAlex Deucher  * amdgpu_atcs_parse_functions - parse supported functions
559d38ceaf9SAlex Deucher  *
560d38ceaf9SAlex Deucher  * @f: supported functions struct
561d38ceaf9SAlex Deucher  * @mask: supported functions mask from ATCS
562d38ceaf9SAlex Deucher  *
563d38ceaf9SAlex Deucher  * Use the supported functions mask from ATCS function
564d38ceaf9SAlex Deucher  * ATCS_FUNCTION_VERIFY_INTERFACE to determine what functions
565d38ceaf9SAlex Deucher  * are supported (all asics).
566d38ceaf9SAlex Deucher  */
567d38ceaf9SAlex Deucher static void amdgpu_atcs_parse_functions(struct amdgpu_atcs_functions *f, u32 mask)
568d38ceaf9SAlex Deucher {
569d38ceaf9SAlex Deucher 	f->get_ext_state = mask & ATCS_GET_EXTERNAL_STATE_SUPPORTED;
570d38ceaf9SAlex Deucher 	f->pcie_perf_req = mask & ATCS_PCIE_PERFORMANCE_REQUEST_SUPPORTED;
571d38ceaf9SAlex Deucher 	f->pcie_dev_rdy = mask & ATCS_PCIE_DEVICE_READY_NOTIFICATION_SUPPORTED;
572d38ceaf9SAlex Deucher 	f->pcie_bus_width = mask & ATCS_SET_PCIE_BUS_WIDTH_SUPPORTED;
57316eb48c6SSathishkumar S 	f->power_shift_control = mask & ATCS_SET_POWER_SHIFT_CONTROL_SUPPORTED;
574d38ceaf9SAlex Deucher }
575d38ceaf9SAlex Deucher 
576d38ceaf9SAlex Deucher /**
577d38ceaf9SAlex Deucher  * amdgpu_atcs_verify_interface - verify ATCS
578d38ceaf9SAlex Deucher  *
579d38ceaf9SAlex Deucher  * @atcs: amdgpu atcs struct
580d38ceaf9SAlex Deucher  *
581d38ceaf9SAlex Deucher  * Execute the ATCS_FUNCTION_VERIFY_INTERFACE ATCS function
582d38ceaf9SAlex Deucher  * to initialize ATCS and determine what features are supported
583d38ceaf9SAlex Deucher  * (all asics).
584d38ceaf9SAlex Deucher  * returns 0 on success, error on failure.
585d38ceaf9SAlex Deucher  */
58677bf762fSAlex Deucher static int amdgpu_atcs_verify_interface(struct amdgpu_atcs *atcs)
587d38ceaf9SAlex Deucher {
588d38ceaf9SAlex Deucher 	union acpi_object *info;
589d38ceaf9SAlex Deucher 	struct atcs_verify_interface output;
590d38ceaf9SAlex Deucher 	size_t size;
591d38ceaf9SAlex Deucher 	int err = 0;
592d38ceaf9SAlex Deucher 
59377bf762fSAlex Deucher 	info = amdgpu_atcs_call(atcs, ATCS_FUNCTION_VERIFY_INTERFACE, NULL);
594d38ceaf9SAlex Deucher 	if (!info)
595d38ceaf9SAlex Deucher 		return -EIO;
596d38ceaf9SAlex Deucher 
597d38ceaf9SAlex Deucher 	memset(&output, 0, sizeof(output));
598d38ceaf9SAlex Deucher 
599d38ceaf9SAlex Deucher 	size = *(u16 *) info->buffer.pointer;
600d38ceaf9SAlex Deucher 	if (size < 8) {
601d38ceaf9SAlex Deucher 		DRM_INFO("ATCS buffer is too small: %zu\n", size);
602d38ceaf9SAlex Deucher 		err = -EINVAL;
603d38ceaf9SAlex Deucher 		goto out;
604d38ceaf9SAlex Deucher 	}
605d38ceaf9SAlex Deucher 	size = min(sizeof(output), size);
606d38ceaf9SAlex Deucher 
607d38ceaf9SAlex Deucher 	memcpy(&output, info->buffer.pointer, size);
608d38ceaf9SAlex Deucher 
609d38ceaf9SAlex Deucher 	/* TODO: check version? */
610d38ceaf9SAlex Deucher 	DRM_DEBUG_DRIVER("ATCS version %u\n", output.version);
611d38ceaf9SAlex Deucher 
612d38ceaf9SAlex Deucher 	amdgpu_atcs_parse_functions(&atcs->functions, output.function_bits);
613d38ceaf9SAlex Deucher 
614d38ceaf9SAlex Deucher out:
615d38ceaf9SAlex Deucher 	kfree(info);
616d38ceaf9SAlex Deucher 	return err;
617d38ceaf9SAlex Deucher }
618d38ceaf9SAlex Deucher 
619d38ceaf9SAlex Deucher /**
620d38ceaf9SAlex Deucher  * amdgpu_acpi_is_pcie_performance_request_supported
621d38ceaf9SAlex Deucher  *
622d38ceaf9SAlex Deucher  * @adev: amdgpu_device pointer
623d38ceaf9SAlex Deucher  *
624d38ceaf9SAlex Deucher  * Check if the ATCS pcie_perf_req and pcie_dev_rdy methods
625d38ceaf9SAlex Deucher  * are supported (all asics).
626d38ceaf9SAlex Deucher  * returns true if supported, false if not.
627d38ceaf9SAlex Deucher  */
628d38ceaf9SAlex Deucher bool amdgpu_acpi_is_pcie_performance_request_supported(struct amdgpu_device *adev)
629d38ceaf9SAlex Deucher {
630f9b7f370SAlex Deucher 	struct amdgpu_atcs *atcs = &amdgpu_acpi_priv.atcs;
631d38ceaf9SAlex Deucher 
632d38ceaf9SAlex Deucher 	if (atcs->functions.pcie_perf_req && atcs->functions.pcie_dev_rdy)
633d38ceaf9SAlex Deucher 		return true;
634d38ceaf9SAlex Deucher 
635d38ceaf9SAlex Deucher 	return false;
636d38ceaf9SAlex Deucher }
637d38ceaf9SAlex Deucher 
638d38ceaf9SAlex Deucher /**
63916eb48c6SSathishkumar S  * amdgpu_acpi_is_power_shift_control_supported
64016eb48c6SSathishkumar S  *
64116eb48c6SSathishkumar S  * Check if the ATCS power shift control method
64216eb48c6SSathishkumar S  * is supported.
64316eb48c6SSathishkumar S  * returns true if supported, false if not.
64416eb48c6SSathishkumar S  */
64516eb48c6SSathishkumar S bool amdgpu_acpi_is_power_shift_control_supported(void)
64616eb48c6SSathishkumar S {
64716eb48c6SSathishkumar S 	return amdgpu_acpi_priv.atcs.functions.power_shift_control;
64816eb48c6SSathishkumar S }
64916eb48c6SSathishkumar S 
65016eb48c6SSathishkumar S /**
651d38ceaf9SAlex Deucher  * amdgpu_acpi_pcie_notify_device_ready
652d38ceaf9SAlex Deucher  *
653d38ceaf9SAlex Deucher  * @adev: amdgpu_device pointer
654d38ceaf9SAlex Deucher  *
655d38ceaf9SAlex Deucher  * Executes the PCIE_DEVICE_READY_NOTIFICATION method
656d38ceaf9SAlex Deucher  * (all asics).
657d38ceaf9SAlex Deucher  * returns 0 on success, error on failure.
658d38ceaf9SAlex Deucher  */
659d38ceaf9SAlex Deucher int amdgpu_acpi_pcie_notify_device_ready(struct amdgpu_device *adev)
660d38ceaf9SAlex Deucher {
661d38ceaf9SAlex Deucher 	union acpi_object *info;
662f9b7f370SAlex Deucher 	struct amdgpu_atcs *atcs = &amdgpu_acpi_priv.atcs;
663d38ceaf9SAlex Deucher 
664d38ceaf9SAlex Deucher 	if (!atcs->functions.pcie_dev_rdy)
665d38ceaf9SAlex Deucher 		return -EINVAL;
666d38ceaf9SAlex Deucher 
66777bf762fSAlex Deucher 	info = amdgpu_atcs_call(atcs, ATCS_FUNCTION_PCIE_DEVICE_READY_NOTIFICATION, NULL);
668d38ceaf9SAlex Deucher 	if (!info)
669d38ceaf9SAlex Deucher 		return -EIO;
670d38ceaf9SAlex Deucher 
671d38ceaf9SAlex Deucher 	kfree(info);
672d38ceaf9SAlex Deucher 
673d38ceaf9SAlex Deucher 	return 0;
674d38ceaf9SAlex Deucher }
675d38ceaf9SAlex Deucher 
676d38ceaf9SAlex Deucher /**
677d38ceaf9SAlex Deucher  * amdgpu_acpi_pcie_performance_request
678d38ceaf9SAlex Deucher  *
679d38ceaf9SAlex Deucher  * @adev: amdgpu_device pointer
680d38ceaf9SAlex Deucher  * @perf_req: requested perf level (pcie gen speed)
681d38ceaf9SAlex Deucher  * @advertise: set advertise caps flag if set
682d38ceaf9SAlex Deucher  *
683d38ceaf9SAlex Deucher  * Executes the PCIE_PERFORMANCE_REQUEST method to
684d38ceaf9SAlex Deucher  * change the pcie gen speed (all asics).
685d38ceaf9SAlex Deucher  * returns 0 on success, error on failure.
686d38ceaf9SAlex Deucher  */
687d38ceaf9SAlex Deucher int amdgpu_acpi_pcie_performance_request(struct amdgpu_device *adev,
688d38ceaf9SAlex Deucher 					 u8 perf_req, bool advertise)
689d38ceaf9SAlex Deucher {
690d38ceaf9SAlex Deucher 	union acpi_object *info;
691f9b7f370SAlex Deucher 	struct amdgpu_atcs *atcs = &amdgpu_acpi_priv.atcs;
692d38ceaf9SAlex Deucher 	struct atcs_pref_req_input atcs_input;
693d38ceaf9SAlex Deucher 	struct atcs_pref_req_output atcs_output;
694d38ceaf9SAlex Deucher 	struct acpi_buffer params;
695d38ceaf9SAlex Deucher 	size_t size;
696d38ceaf9SAlex Deucher 	u32 retry = 3;
697d38ceaf9SAlex Deucher 
69877bf762fSAlex Deucher 	if (amdgpu_acpi_pcie_notify_device_ready(adev))
699d38ceaf9SAlex Deucher 		return -EINVAL;
700d38ceaf9SAlex Deucher 
701d38ceaf9SAlex Deucher 	if (!atcs->functions.pcie_perf_req)
702d38ceaf9SAlex Deucher 		return -EINVAL;
703d38ceaf9SAlex Deucher 
704d38ceaf9SAlex Deucher 	atcs_input.size = sizeof(struct atcs_pref_req_input);
705d38ceaf9SAlex Deucher 	/* client id (bit 2-0: func num, 7-3: dev num, 15-8: bus num) */
706d38ceaf9SAlex Deucher 	atcs_input.client_id = adev->pdev->devfn | (adev->pdev->bus->number << 8);
707d38ceaf9SAlex Deucher 	atcs_input.valid_flags_mask = ATCS_VALID_FLAGS_MASK;
708d38ceaf9SAlex Deucher 	atcs_input.flags = ATCS_WAIT_FOR_COMPLETION;
709d38ceaf9SAlex Deucher 	if (advertise)
710d38ceaf9SAlex Deucher 		atcs_input.flags |= ATCS_ADVERTISE_CAPS;
711d38ceaf9SAlex Deucher 	atcs_input.req_type = ATCS_PCIE_LINK_SPEED;
712d38ceaf9SAlex Deucher 	atcs_input.perf_req = perf_req;
713d38ceaf9SAlex Deucher 
714d38ceaf9SAlex Deucher 	params.length = sizeof(struct atcs_pref_req_input);
715d38ceaf9SAlex Deucher 	params.pointer = &atcs_input;
716d38ceaf9SAlex Deucher 
717d38ceaf9SAlex Deucher 	while (retry--) {
71877bf762fSAlex Deucher 		info = amdgpu_atcs_call(atcs, ATCS_FUNCTION_PCIE_PERFORMANCE_REQUEST, &params);
719d38ceaf9SAlex Deucher 		if (!info)
720d38ceaf9SAlex Deucher 			return -EIO;
721d38ceaf9SAlex Deucher 
722d38ceaf9SAlex Deucher 		memset(&atcs_output, 0, sizeof(atcs_output));
723d38ceaf9SAlex Deucher 
724d38ceaf9SAlex Deucher 		size = *(u16 *) info->buffer.pointer;
725d38ceaf9SAlex Deucher 		if (size < 3) {
726d38ceaf9SAlex Deucher 			DRM_INFO("ATCS buffer is too small: %zu\n", size);
727d38ceaf9SAlex Deucher 			kfree(info);
728d38ceaf9SAlex Deucher 			return -EINVAL;
729d38ceaf9SAlex Deucher 		}
730d38ceaf9SAlex Deucher 		size = min(sizeof(atcs_output), size);
731d38ceaf9SAlex Deucher 
732d38ceaf9SAlex Deucher 		memcpy(&atcs_output, info->buffer.pointer, size);
733d38ceaf9SAlex Deucher 
734d38ceaf9SAlex Deucher 		kfree(info);
735d38ceaf9SAlex Deucher 
736d38ceaf9SAlex Deucher 		switch (atcs_output.ret_val) {
737d38ceaf9SAlex Deucher 		case ATCS_REQUEST_REFUSED:
738d38ceaf9SAlex Deucher 		default:
739d38ceaf9SAlex Deucher 			return -EINVAL;
740d38ceaf9SAlex Deucher 		case ATCS_REQUEST_COMPLETE:
741d38ceaf9SAlex Deucher 			return 0;
742d38ceaf9SAlex Deucher 		case ATCS_REQUEST_IN_PROGRESS:
743d38ceaf9SAlex Deucher 			udelay(10);
744d38ceaf9SAlex Deucher 			break;
745d38ceaf9SAlex Deucher 		}
746d38ceaf9SAlex Deucher 	}
747d38ceaf9SAlex Deucher 
748d38ceaf9SAlex Deucher 	return 0;
749d38ceaf9SAlex Deucher }
750d38ceaf9SAlex Deucher 
751d38ceaf9SAlex Deucher /**
75216eb48c6SSathishkumar S  * amdgpu_acpi_power_shift_control
75316eb48c6SSathishkumar S  *
75416eb48c6SSathishkumar S  * @adev: amdgpu_device pointer
75516eb48c6SSathishkumar S  * @dev_state: device acpi state
75616eb48c6SSathishkumar S  * @drv_state: driver state
75716eb48c6SSathishkumar S  *
75816eb48c6SSathishkumar S  * Executes the POWER_SHIFT_CONTROL method to
75916eb48c6SSathishkumar S  * communicate current dGPU device state and
76016eb48c6SSathishkumar S  * driver state to APU/SBIOS.
76116eb48c6SSathishkumar S  * returns 0 on success, error on failure.
76216eb48c6SSathishkumar S  */
76316eb48c6SSathishkumar S int amdgpu_acpi_power_shift_control(struct amdgpu_device *adev,
76416eb48c6SSathishkumar S 				    u8 dev_state, bool drv_state)
76516eb48c6SSathishkumar S {
76616eb48c6SSathishkumar S 	union acpi_object *info;
76716eb48c6SSathishkumar S 	struct amdgpu_atcs *atcs = &amdgpu_acpi_priv.atcs;
76816eb48c6SSathishkumar S 	struct atcs_pwr_shift_input atcs_input;
76916eb48c6SSathishkumar S 	struct acpi_buffer params;
77016eb48c6SSathishkumar S 
77116eb48c6SSathishkumar S 	if (!amdgpu_acpi_is_power_shift_control_supported())
77216eb48c6SSathishkumar S 		return -EINVAL;
77316eb48c6SSathishkumar S 
77416eb48c6SSathishkumar S 	atcs_input.size = sizeof(struct atcs_pwr_shift_input);
77516eb48c6SSathishkumar S 	/* dGPU id (bit 2-0: func num, 7-3: dev num, 15-8: bus num) */
77616eb48c6SSathishkumar S 	atcs_input.dgpu_id = adev->pdev->devfn | (adev->pdev->bus->number << 8);
77716eb48c6SSathishkumar S 	atcs_input.dev_acpi_state = dev_state;
77816eb48c6SSathishkumar S 	atcs_input.drv_state = drv_state;
77916eb48c6SSathishkumar S 
78016eb48c6SSathishkumar S 	params.length = sizeof(struct atcs_pwr_shift_input);
78116eb48c6SSathishkumar S 	params.pointer = &atcs_input;
78216eb48c6SSathishkumar S 
78316eb48c6SSathishkumar S 	info = amdgpu_atcs_call(atcs, ATCS_FUNCTION_POWER_SHIFT_CONTROL, &params);
78416eb48c6SSathishkumar S 	if (!info) {
78516eb48c6SSathishkumar S 		DRM_ERROR("ATCS PSC update failed\n");
78616eb48c6SSathishkumar S 		return -EIO;
78716eb48c6SSathishkumar S 	}
78816eb48c6SSathishkumar S 
78916eb48c6SSathishkumar S 	return 0;
79016eb48c6SSathishkumar S }
79116eb48c6SSathishkumar S 
79216eb48c6SSathishkumar S /**
7933fa8f89dSSathishkumar S  * amdgpu_acpi_smart_shift_update - update dGPU device state to SBIOS
7943fa8f89dSSathishkumar S  *
7953fa8f89dSSathishkumar S  * @dev: drm_device pointer
7963fa8f89dSSathishkumar S  * @ss_state: current smart shift event
7973fa8f89dSSathishkumar S  *
7983fa8f89dSSathishkumar S  * returns 0 on success,
7993fa8f89dSSathishkumar S  * otherwise return error number.
8003fa8f89dSSathishkumar S  */
8013fa8f89dSSathishkumar S int amdgpu_acpi_smart_shift_update(struct drm_device *dev, enum amdgpu_ss ss_state)
8023fa8f89dSSathishkumar S {
8033fa8f89dSSathishkumar S 	struct amdgpu_device *adev = drm_to_adev(dev);
8043fa8f89dSSathishkumar S 	int r;
8053fa8f89dSSathishkumar S 
8063fa8f89dSSathishkumar S 	if (!amdgpu_device_supports_smart_shift(dev))
8073fa8f89dSSathishkumar S 		return 0;
8083fa8f89dSSathishkumar S 
8093fa8f89dSSathishkumar S 	switch (ss_state) {
8103fa8f89dSSathishkumar S 	/* SBIOS trigger “stop”, “enable” and “start” at D0, Driver Operational.
8113fa8f89dSSathishkumar S 	 * SBIOS trigger “stop” at D3, Driver Not Operational.
8123fa8f89dSSathishkumar S 	 * SBIOS trigger “stop” and “disable” at D0, Driver NOT operational.
8133fa8f89dSSathishkumar S 	 */
8143fa8f89dSSathishkumar S 	case AMDGPU_SS_DRV_LOAD:
8153fa8f89dSSathishkumar S 		r = amdgpu_acpi_power_shift_control(adev,
8163fa8f89dSSathishkumar S 						    AMDGPU_ATCS_PSC_DEV_STATE_D0,
8173fa8f89dSSathishkumar S 						    AMDGPU_ATCS_PSC_DRV_STATE_OPR);
8183fa8f89dSSathishkumar S 		break;
8193fa8f89dSSathishkumar S 	case AMDGPU_SS_DEV_D0:
8203fa8f89dSSathishkumar S 		r = amdgpu_acpi_power_shift_control(adev,
8213fa8f89dSSathishkumar S 						    AMDGPU_ATCS_PSC_DEV_STATE_D0,
8223fa8f89dSSathishkumar S 						    AMDGPU_ATCS_PSC_DRV_STATE_OPR);
8233fa8f89dSSathishkumar S 		break;
8243fa8f89dSSathishkumar S 	case AMDGPU_SS_DEV_D3:
8253fa8f89dSSathishkumar S 		r = amdgpu_acpi_power_shift_control(adev,
8263fa8f89dSSathishkumar S 						    AMDGPU_ATCS_PSC_DEV_STATE_D3_HOT,
8273fa8f89dSSathishkumar S 						    AMDGPU_ATCS_PSC_DRV_STATE_NOT_OPR);
8283fa8f89dSSathishkumar S 		break;
8293fa8f89dSSathishkumar S 	case AMDGPU_SS_DRV_UNLOAD:
8303fa8f89dSSathishkumar S 		r = amdgpu_acpi_power_shift_control(adev,
8313fa8f89dSSathishkumar S 						    AMDGPU_ATCS_PSC_DEV_STATE_D0,
8323fa8f89dSSathishkumar S 						    AMDGPU_ATCS_PSC_DRV_STATE_NOT_OPR);
8333fa8f89dSSathishkumar S 		break;
8343fa8f89dSSathishkumar S 	default:
8353fa8f89dSSathishkumar S 		return -EINVAL;
8363fa8f89dSSathishkumar S 	}
8373fa8f89dSSathishkumar S 
8383fa8f89dSSathishkumar S 	return r;
8393fa8f89dSSathishkumar S }
8403fa8f89dSSathishkumar S 
8413fa8f89dSSathishkumar S /**
842*4d5275abSLijo Lazar  * amdgpu_acpi_get_node_id - obtain the NUMA node id for corresponding amdgpu
843*4d5275abSLijo Lazar  * acpi device handle
844*4d5275abSLijo Lazar  *
845*4d5275abSLijo Lazar  * @handle: acpi handle
846*4d5275abSLijo Lazar  * @nid: NUMA Node id returned by the platform firmware
847*4d5275abSLijo Lazar  *
848*4d5275abSLijo Lazar  * Queries the ACPI interface to fetch the corresponding NUMA Node ID for a
849*4d5275abSLijo Lazar  * given amdgpu acpi device.
850*4d5275abSLijo Lazar  *
851*4d5275abSLijo Lazar  * Returns ACPI STATUS OK with Node ID on success or the corresponding failure reason
852*4d5275abSLijo Lazar  */
853*4d5275abSLijo Lazar acpi_status amdgpu_acpi_get_node_id(acpi_handle handle, int *nid)
854*4d5275abSLijo Lazar {
855*4d5275abSLijo Lazar #ifdef CONFIG_ACPI_NUMA
856*4d5275abSLijo Lazar 	u64 pxm;
857*4d5275abSLijo Lazar 	acpi_status status;
858*4d5275abSLijo Lazar 
859*4d5275abSLijo Lazar 	status = acpi_evaluate_integer(handle, "_PXM", NULL, &pxm);
860*4d5275abSLijo Lazar 
861*4d5275abSLijo Lazar 	if (ACPI_FAILURE(status))
862*4d5275abSLijo Lazar 		return status;
863*4d5275abSLijo Lazar 
864*4d5275abSLijo Lazar 	*nid = pxm_to_node(pxm);
865*4d5275abSLijo Lazar 
866*4d5275abSLijo Lazar 	return_ACPI_STATUS(AE_OK);
867*4d5275abSLijo Lazar #else
868*4d5275abSLijo Lazar 	return_ACPI_STATUS(AE_NOT_EXIST);
869*4d5275abSLijo Lazar #endif
870*4d5275abSLijo Lazar }
871*4d5275abSLijo Lazar 
872*4d5275abSLijo Lazar struct amdgpu_acpi_dev_info *amdgpu_acpi_get_dev(u16 bdf)
873*4d5275abSLijo Lazar {
874*4d5275abSLijo Lazar 	struct amdgpu_acpi_dev_info *acpi_dev;
875*4d5275abSLijo Lazar 
876*4d5275abSLijo Lazar 	if (list_empty(&amdgpu_acpi_dev_list))
877*4d5275abSLijo Lazar 		return NULL;
878*4d5275abSLijo Lazar 
879*4d5275abSLijo Lazar 	list_for_each_entry(acpi_dev, &amdgpu_acpi_dev_list, list)
880*4d5275abSLijo Lazar 		if (acpi_dev->bdf == bdf)
881*4d5275abSLijo Lazar 			return acpi_dev;
882*4d5275abSLijo Lazar 
883*4d5275abSLijo Lazar 	return NULL;
884*4d5275abSLijo Lazar }
885*4d5275abSLijo Lazar 
886*4d5275abSLijo Lazar static int amdgpu_acpi_dev_init(struct amdgpu_acpi_dev_info **dev_info,
887*4d5275abSLijo Lazar 				struct amdgpu_acpi_xcc_info *xcc_info, u16 bdf)
888*4d5275abSLijo Lazar {
889*4d5275abSLijo Lazar 	struct amdgpu_acpi_dev_info *tmp;
890*4d5275abSLijo Lazar 	union acpi_object *obj;
891*4d5275abSLijo Lazar 	int ret = -ENOENT;
892*4d5275abSLijo Lazar 
893*4d5275abSLijo Lazar 	*dev_info = NULL;
894*4d5275abSLijo Lazar 	tmp = kzalloc(sizeof(struct amdgpu_acpi_dev_info), GFP_KERNEL);
895*4d5275abSLijo Lazar 	if (!tmp)
896*4d5275abSLijo Lazar 		return -ENOMEM;
897*4d5275abSLijo Lazar 
898*4d5275abSLijo Lazar 	INIT_LIST_HEAD(&tmp->xcc_list);
899*4d5275abSLijo Lazar 	INIT_LIST_HEAD(&tmp->list);
900*4d5275abSLijo Lazar 	tmp->bdf = bdf;
901*4d5275abSLijo Lazar 
902*4d5275abSLijo Lazar 	obj = acpi_evaluate_dsm_typed(xcc_info->handle, &amd_xcc_dsm_guid, 0,
903*4d5275abSLijo Lazar 				      AMD_XCC_DSM_GET_SUPP_MODE, NULL,
904*4d5275abSLijo Lazar 				      ACPI_TYPE_INTEGER);
905*4d5275abSLijo Lazar 
906*4d5275abSLijo Lazar 	if (!obj) {
907*4d5275abSLijo Lazar 		acpi_handle_debug(xcc_info->handle,
908*4d5275abSLijo Lazar 				  "_DSM function %d evaluation failed",
909*4d5275abSLijo Lazar 				  AMD_XCC_DSM_GET_SUPP_MODE);
910*4d5275abSLijo Lazar 		ret = -ENOENT;
911*4d5275abSLijo Lazar 		goto out;
912*4d5275abSLijo Lazar 	}
913*4d5275abSLijo Lazar 
914*4d5275abSLijo Lazar 	tmp->supp_xcp_mode = obj->integer.value & 0xFFFF;
915*4d5275abSLijo Lazar 	ACPI_FREE(obj);
916*4d5275abSLijo Lazar 
917*4d5275abSLijo Lazar 	obj = acpi_evaluate_dsm_typed(xcc_info->handle, &amd_xcc_dsm_guid, 0,
918*4d5275abSLijo Lazar 				      AMD_XCC_DSM_GET_XCP_MODE, NULL,
919*4d5275abSLijo Lazar 				      ACPI_TYPE_INTEGER);
920*4d5275abSLijo Lazar 
921*4d5275abSLijo Lazar 	if (!obj) {
922*4d5275abSLijo Lazar 		acpi_handle_debug(xcc_info->handle,
923*4d5275abSLijo Lazar 				  "_DSM function %d evaluation failed",
924*4d5275abSLijo Lazar 				  AMD_XCC_DSM_GET_XCP_MODE);
925*4d5275abSLijo Lazar 		ret = -ENOENT;
926*4d5275abSLijo Lazar 		goto out;
927*4d5275abSLijo Lazar 	}
928*4d5275abSLijo Lazar 
929*4d5275abSLijo Lazar 	tmp->xcp_mode = obj->integer.value & 0xFFFF;
930*4d5275abSLijo Lazar 	tmp->mem_mode = (obj->integer.value >> 32) & 0xFFFF;
931*4d5275abSLijo Lazar 	ACPI_FREE(obj);
932*4d5275abSLijo Lazar 
933*4d5275abSLijo Lazar 	/* Evaluate DSMs and fill XCC information */
934*4d5275abSLijo Lazar 	obj = acpi_evaluate_dsm_typed(xcc_info->handle, &amd_xcc_dsm_guid, 0,
935*4d5275abSLijo Lazar 				      AMD_XCC_DSM_GET_TMR_INFO, NULL,
936*4d5275abSLijo Lazar 				      ACPI_TYPE_PACKAGE);
937*4d5275abSLijo Lazar 
938*4d5275abSLijo Lazar 	if (!obj || obj->package.count < 2) {
939*4d5275abSLijo Lazar 		acpi_handle_debug(xcc_info->handle,
940*4d5275abSLijo Lazar 				  "_DSM function %d evaluation failed",
941*4d5275abSLijo Lazar 				  AMD_XCC_DSM_GET_TMR_INFO);
942*4d5275abSLijo Lazar 		ret = -ENOENT;
943*4d5275abSLijo Lazar 		goto out;
944*4d5275abSLijo Lazar 	}
945*4d5275abSLijo Lazar 
946*4d5275abSLijo Lazar 	tmp->tmr_base = obj->package.elements[0].integer.value;
947*4d5275abSLijo Lazar 	tmp->tmr_size = obj->package.elements[1].integer.value;
948*4d5275abSLijo Lazar 	ACPI_FREE(obj);
949*4d5275abSLijo Lazar 
950*4d5275abSLijo Lazar 	DRM_DEBUG_DRIVER(
951*4d5275abSLijo Lazar 		"New dev(%x): Supported xcp mode: %x curr xcp_mode : %x mem mode : %x, tmr base: %llx tmr size: %llx  ",
952*4d5275abSLijo Lazar 		tmp->bdf, tmp->supp_xcp_mode, tmp->xcp_mode, tmp->mem_mode,
953*4d5275abSLijo Lazar 		tmp->tmr_base, tmp->tmr_size);
954*4d5275abSLijo Lazar 	list_add_tail(&tmp->list, &amdgpu_acpi_dev_list);
955*4d5275abSLijo Lazar 	*dev_info = tmp;
956*4d5275abSLijo Lazar 
957*4d5275abSLijo Lazar 	return 0;
958*4d5275abSLijo Lazar 
959*4d5275abSLijo Lazar out:
960*4d5275abSLijo Lazar 	if (obj)
961*4d5275abSLijo Lazar 		ACPI_FREE(obj);
962*4d5275abSLijo Lazar 	kfree(tmp);
963*4d5275abSLijo Lazar 
964*4d5275abSLijo Lazar 	return ret;
965*4d5275abSLijo Lazar }
966*4d5275abSLijo Lazar 
967*4d5275abSLijo Lazar static int amdgpu_acpi_get_xcc_info(struct amdgpu_acpi_xcc_info *xcc_info,
968*4d5275abSLijo Lazar 				    u16 *bdf)
969*4d5275abSLijo Lazar {
970*4d5275abSLijo Lazar 	union acpi_object *obj;
971*4d5275abSLijo Lazar 	acpi_status status;
972*4d5275abSLijo Lazar 	int ret = -ENOENT;
973*4d5275abSLijo Lazar 
974*4d5275abSLijo Lazar 	obj = acpi_evaluate_dsm_typed(xcc_info->handle, &amd_xcc_dsm_guid, 0,
975*4d5275abSLijo Lazar 				      AMD_XCC_DSM_GET_NUM_FUNCS, NULL,
976*4d5275abSLijo Lazar 				      ACPI_TYPE_INTEGER);
977*4d5275abSLijo Lazar 
978*4d5275abSLijo Lazar 	if (!obj || obj->integer.value != AMD_XCC_DSM_NUM_FUNCS)
979*4d5275abSLijo Lazar 		goto out;
980*4d5275abSLijo Lazar 	ACPI_FREE(obj);
981*4d5275abSLijo Lazar 
982*4d5275abSLijo Lazar 	/* Evaluate DSMs and fill XCC information */
983*4d5275abSLijo Lazar 	obj = acpi_evaluate_dsm_typed(xcc_info->handle, &amd_xcc_dsm_guid, 0,
984*4d5275abSLijo Lazar 				      AMD_XCC_DSM_GET_VF_XCC_MAPPING, NULL,
985*4d5275abSLijo Lazar 				      ACPI_TYPE_INTEGER);
986*4d5275abSLijo Lazar 
987*4d5275abSLijo Lazar 	if (!obj) {
988*4d5275abSLijo Lazar 		acpi_handle_debug(xcc_info->handle,
989*4d5275abSLijo Lazar 				  "_DSM function %d evaluation failed",
990*4d5275abSLijo Lazar 				  AMD_XCC_DSM_GET_VF_XCC_MAPPING);
991*4d5275abSLijo Lazar 		ret = -EINVAL;
992*4d5275abSLijo Lazar 		goto out;
993*4d5275abSLijo Lazar 	}
994*4d5275abSLijo Lazar 
995*4d5275abSLijo Lazar 	/* PF xcc id [39:32] */
996*4d5275abSLijo Lazar 	xcc_info->phy_id = (obj->integer.value >> 32) & 0xFF;
997*4d5275abSLijo Lazar 	/* xcp node of this xcc [47:40] */
998*4d5275abSLijo Lazar 	xcc_info->xcp_node = (obj->integer.value >> 40) & 0xFF;
999*4d5275abSLijo Lazar 	/* PF bus/dev/fn of this xcc [63:48] */
1000*4d5275abSLijo Lazar 	*bdf = (obj->integer.value >> 48) & 0xFFFF;
1001*4d5275abSLijo Lazar 	ACPI_FREE(obj);
1002*4d5275abSLijo Lazar 	obj = NULL;
1003*4d5275abSLijo Lazar 
1004*4d5275abSLijo Lazar 	status = amdgpu_acpi_get_node_id(xcc_info->handle, &xcc_info->mem_node);
1005*4d5275abSLijo Lazar 
1006*4d5275abSLijo Lazar 	/* TODO: check if this check is required */
1007*4d5275abSLijo Lazar 	if (ACPI_SUCCESS(status))
1008*4d5275abSLijo Lazar 		ret = 0;
1009*4d5275abSLijo Lazar out:
1010*4d5275abSLijo Lazar 	if (obj)
1011*4d5275abSLijo Lazar 		ACPI_FREE(obj);
1012*4d5275abSLijo Lazar 
1013*4d5275abSLijo Lazar 	return ret;
1014*4d5275abSLijo Lazar }
1015*4d5275abSLijo Lazar 
1016*4d5275abSLijo Lazar static int amdgpu_acpi_enumerate_xcc(void)
1017*4d5275abSLijo Lazar {
1018*4d5275abSLijo Lazar 	struct amdgpu_acpi_dev_info *dev_info = NULL;
1019*4d5275abSLijo Lazar 	struct amdgpu_acpi_xcc_info *xcc_info;
1020*4d5275abSLijo Lazar 	struct acpi_device *acpi_dev;
1021*4d5275abSLijo Lazar 	char hid[ACPI_ID_LEN];
1022*4d5275abSLijo Lazar 	int ret, id;
1023*4d5275abSLijo Lazar 	u16 bdf;
1024*4d5275abSLijo Lazar 
1025*4d5275abSLijo Lazar 	INIT_LIST_HEAD(&amdgpu_acpi_dev_list);
1026*4d5275abSLijo Lazar 
1027*4d5275abSLijo Lazar 	for (id = 0; id < AMD_XCC_MAX_HID; id++) {
1028*4d5275abSLijo Lazar 		sprintf(hid, "%s%d", "AMD", AMD_XCC_HID_START + id);
1029*4d5275abSLijo Lazar 		acpi_dev = acpi_dev_get_first_match_dev(hid, NULL, -1);
1030*4d5275abSLijo Lazar 		/* These ACPI objects are expected to be in sequential order. If
1031*4d5275abSLijo Lazar 		 * one is not found, no need to check the rest.
1032*4d5275abSLijo Lazar 		 */
1033*4d5275abSLijo Lazar 		if (!acpi_dev) {
1034*4d5275abSLijo Lazar 			DRM_DEBUG_DRIVER("No matching acpi device found for %s",
1035*4d5275abSLijo Lazar 					 hid);
1036*4d5275abSLijo Lazar 			break;
1037*4d5275abSLijo Lazar 		}
1038*4d5275abSLijo Lazar 
1039*4d5275abSLijo Lazar 		xcc_info = kzalloc(sizeof(struct amdgpu_acpi_xcc_info),
1040*4d5275abSLijo Lazar 				   GFP_KERNEL);
1041*4d5275abSLijo Lazar 		if (!xcc_info) {
1042*4d5275abSLijo Lazar 			DRM_ERROR("Failed to allocate memory for xcc info\n");
1043*4d5275abSLijo Lazar 			return -ENOMEM;
1044*4d5275abSLijo Lazar 		}
1045*4d5275abSLijo Lazar 
1046*4d5275abSLijo Lazar 		INIT_LIST_HEAD(&xcc_info->list);
1047*4d5275abSLijo Lazar 		xcc_info->handle = acpi_device_handle(acpi_dev);
1048*4d5275abSLijo Lazar 		acpi_dev_put(acpi_dev);
1049*4d5275abSLijo Lazar 
1050*4d5275abSLijo Lazar 		ret = amdgpu_acpi_get_xcc_info(xcc_info, &bdf);
1051*4d5275abSLijo Lazar 		if (ret) {
1052*4d5275abSLijo Lazar 			kfree(xcc_info);
1053*4d5275abSLijo Lazar 			continue;
1054*4d5275abSLijo Lazar 		}
1055*4d5275abSLijo Lazar 
1056*4d5275abSLijo Lazar 		dev_info = amdgpu_acpi_get_dev(bdf);
1057*4d5275abSLijo Lazar 
1058*4d5275abSLijo Lazar 		if (!dev_info)
1059*4d5275abSLijo Lazar 			ret = amdgpu_acpi_dev_init(&dev_info, xcc_info, bdf);
1060*4d5275abSLijo Lazar 
1061*4d5275abSLijo Lazar 		if (ret == -ENOMEM)
1062*4d5275abSLijo Lazar 			return ret;
1063*4d5275abSLijo Lazar 
1064*4d5275abSLijo Lazar 		if (!dev_info) {
1065*4d5275abSLijo Lazar 			kfree(xcc_info);
1066*4d5275abSLijo Lazar 			continue;
1067*4d5275abSLijo Lazar 		}
1068*4d5275abSLijo Lazar 
1069*4d5275abSLijo Lazar 		list_add_tail(&xcc_info->list, &dev_info->xcc_list);
1070*4d5275abSLijo Lazar 	}
1071*4d5275abSLijo Lazar 
1072*4d5275abSLijo Lazar 	return 0;
1073*4d5275abSLijo Lazar }
1074*4d5275abSLijo Lazar 
1075*4d5275abSLijo Lazar /**
1076d38ceaf9SAlex Deucher  * amdgpu_acpi_event - handle notify events
1077d38ceaf9SAlex Deucher  *
1078d38ceaf9SAlex Deucher  * @nb: notifier block
1079d38ceaf9SAlex Deucher  * @val: val
1080d38ceaf9SAlex Deucher  * @data: acpi event
1081d38ceaf9SAlex Deucher  *
1082d38ceaf9SAlex Deucher  * Calls relevant amdgpu functions in response to various
1083d38ceaf9SAlex Deucher  * acpi events.
1084d38ceaf9SAlex Deucher  * Returns NOTIFY code
1085d38ceaf9SAlex Deucher  */
1086d38ceaf9SAlex Deucher static int amdgpu_acpi_event(struct notifier_block *nb,
1087d38ceaf9SAlex Deucher 			     unsigned long val,
1088d38ceaf9SAlex Deucher 			     void *data)
1089d38ceaf9SAlex Deucher {
1090d38ceaf9SAlex Deucher 	struct amdgpu_device *adev = container_of(nb, struct amdgpu_device, acpi_nb);
1091d38ceaf9SAlex Deucher 	struct acpi_bus_event *entry = (struct acpi_bus_event *)data;
1092d38ceaf9SAlex Deucher 
1093d38ceaf9SAlex Deucher 	if (strcmp(entry->device_class, ACPI_AC_CLASS) == 0) {
1094d38ceaf9SAlex Deucher 		if (power_supply_is_system_supplied() > 0)
1095d38ceaf9SAlex Deucher 			DRM_DEBUG_DRIVER("pm: AC\n");
1096d38ceaf9SAlex Deucher 		else
1097d38ceaf9SAlex Deucher 			DRM_DEBUG_DRIVER("pm: DC\n");
1098d38ceaf9SAlex Deucher 
1099d38ceaf9SAlex Deucher 		amdgpu_pm_acpi_event_handler(adev);
1100d38ceaf9SAlex Deucher 	}
1101d38ceaf9SAlex Deucher 
1102d38ceaf9SAlex Deucher 	/* Check for pending SBIOS requests */
1103d38ceaf9SAlex Deucher 	return amdgpu_atif_handler(adev, entry);
1104d38ceaf9SAlex Deucher }
1105d38ceaf9SAlex Deucher 
1106d38ceaf9SAlex Deucher /* Call all ACPI methods here */
1107d38ceaf9SAlex Deucher /**
1108d38ceaf9SAlex Deucher  * amdgpu_acpi_init - init driver acpi support
1109d38ceaf9SAlex Deucher  *
1110d38ceaf9SAlex Deucher  * @adev: amdgpu_device pointer
1111d38ceaf9SAlex Deucher  *
1112d38ceaf9SAlex Deucher  * Verifies the AMD ACPI interfaces and registers with the acpi
1113d38ceaf9SAlex Deucher  * notifier chain (all asics).
1114d38ceaf9SAlex Deucher  * Returns 0 on success, error on failure.
1115d38ceaf9SAlex Deucher  */
1116d38ceaf9SAlex Deucher int amdgpu_acpi_init(struct amdgpu_device *adev)
1117d38ceaf9SAlex Deucher {
1118f9b7f370SAlex Deucher 	struct amdgpu_atif *atif = &amdgpu_acpi_priv.atif;
1119d38ceaf9SAlex Deucher 
11209c27bc97SYe Bin 	if (atif->notifications.brightness_change) {
1121d09ef243SAlex Deucher 		if (adev->dc_enabled) {
112297d798b2SAlex Deucher #if defined(CONFIG_DRM_AMD_DC)
112397d798b2SAlex Deucher 			struct amdgpu_display_manager *dm = &adev->dm;
1124556bdae3SJingyu Wang 
11257fd13baeSAlex Deucher 			if (dm->backlight_dev[0])
11267fd13baeSAlex Deucher 				atif->bd = dm->backlight_dev[0];
112797d798b2SAlex Deucher #endif
112897d798b2SAlex Deucher 		} else {
1129d38ceaf9SAlex Deucher 			struct drm_encoder *tmp;
1130d38ceaf9SAlex Deucher 
1131d38ceaf9SAlex Deucher 			/* Find the encoder controlling the brightness */
11324a580877SLuben Tuikov 			list_for_each_entry(tmp, &adev_to_drm(adev)->mode_config.encoder_list,
1133d38ceaf9SAlex Deucher 					    head) {
1134d38ceaf9SAlex Deucher 				struct amdgpu_encoder *enc = to_amdgpu_encoder(tmp);
1135d38ceaf9SAlex Deucher 
1136d38ceaf9SAlex Deucher 				if ((enc->devices & (ATOM_DEVICE_LCD_SUPPORT)) &&
1137d38ceaf9SAlex Deucher 				    enc->enc_priv) {
1138d38ceaf9SAlex Deucher 					struct amdgpu_encoder_atom_dig *dig = enc->enc_priv;
1139556bdae3SJingyu Wang 
1140d38ceaf9SAlex Deucher 					if (dig->bl_dev) {
114197d798b2SAlex Deucher 						atif->bd = dig->bl_dev;
1142d38ceaf9SAlex Deucher 						break;
1143d38ceaf9SAlex Deucher 					}
1144d38ceaf9SAlex Deucher 				}
1145d38ceaf9SAlex Deucher 			}
1146d38ceaf9SAlex Deucher 		}
114797d798b2SAlex Deucher 	}
1148f9b7f370SAlex Deucher 	adev->acpi_nb.notifier_call = amdgpu_acpi_event;
1149f9b7f370SAlex Deucher 	register_acpi_notifier(&adev->acpi_nb);
1150f9b7f370SAlex Deucher 
1151f9b7f370SAlex Deucher 	return 0;
1152f9b7f370SAlex Deucher }
1153f9b7f370SAlex Deucher 
1154f9b7f370SAlex Deucher void amdgpu_acpi_get_backlight_caps(struct amdgpu_dm_backlight_caps *caps)
1155f9b7f370SAlex Deucher {
1156f9b7f370SAlex Deucher 	struct amdgpu_atif *atif = &amdgpu_acpi_priv.atif;
1157f9b7f370SAlex Deucher 
1158f9b7f370SAlex Deucher 	caps->caps_valid = atif->backlight_caps.caps_valid;
1159f9b7f370SAlex Deucher 	caps->min_input_signal = atif->backlight_caps.min_input_signal;
1160f9b7f370SAlex Deucher 	caps->max_input_signal = atif->backlight_caps.max_input_signal;
1161f9b7f370SAlex Deucher }
1162f9b7f370SAlex Deucher 
1163f9b7f370SAlex Deucher /**
1164f9b7f370SAlex Deucher  * amdgpu_acpi_fini - tear down driver acpi support
1165f9b7f370SAlex Deucher  *
1166f9b7f370SAlex Deucher  * @adev: amdgpu_device pointer
1167f9b7f370SAlex Deucher  *
1168f9b7f370SAlex Deucher  * Unregisters with the acpi notifier chain (all asics).
1169f9b7f370SAlex Deucher  */
1170f9b7f370SAlex Deucher void amdgpu_acpi_fini(struct amdgpu_device *adev)
1171f9b7f370SAlex Deucher {
1172f9b7f370SAlex Deucher 	unregister_acpi_notifier(&adev->acpi_nb);
1173f9b7f370SAlex Deucher }
1174f9b7f370SAlex Deucher 
1175f9b7f370SAlex Deucher /**
1176f9b7f370SAlex Deucher  * amdgpu_atif_pci_probe_handle - look up the ATIF handle
1177f9b7f370SAlex Deucher  *
1178f9b7f370SAlex Deucher  * @pdev: pci device
1179f9b7f370SAlex Deucher  *
1180f9b7f370SAlex Deucher  * Look up the ATIF handles (all asics).
1181f9b7f370SAlex Deucher  * Returns true if the handle is found, false if not.
1182f9b7f370SAlex Deucher  */
1183f9b7f370SAlex Deucher static bool amdgpu_atif_pci_probe_handle(struct pci_dev *pdev)
1184f9b7f370SAlex Deucher {
1185f9b7f370SAlex Deucher 	char acpi_method_name[255] = { 0 };
1186f9b7f370SAlex Deucher 	struct acpi_buffer buffer = {sizeof(acpi_method_name), acpi_method_name};
1187f9b7f370SAlex Deucher 	acpi_handle dhandle, atif_handle;
1188f9b7f370SAlex Deucher 	acpi_status status;
1189f9b7f370SAlex Deucher 	int ret;
1190f9b7f370SAlex Deucher 
1191f9b7f370SAlex Deucher 	dhandle = ACPI_HANDLE(&pdev->dev);
1192f9b7f370SAlex Deucher 	if (!dhandle)
1193f9b7f370SAlex Deucher 		return false;
1194f9b7f370SAlex Deucher 
1195f9b7f370SAlex Deucher 	status = acpi_get_handle(dhandle, "ATIF", &atif_handle);
1196556bdae3SJingyu Wang 	if (ACPI_FAILURE(status))
1197f9b7f370SAlex Deucher 		return false;
1198556bdae3SJingyu Wang 
1199f9b7f370SAlex Deucher 	amdgpu_acpi_priv.atif.handle = atif_handle;
1200f9b7f370SAlex Deucher 	acpi_get_name(amdgpu_acpi_priv.atif.handle, ACPI_FULL_PATHNAME, &buffer);
1201f9b7f370SAlex Deucher 	DRM_DEBUG_DRIVER("Found ATIF handle %s\n", acpi_method_name);
1202f9b7f370SAlex Deucher 	ret = amdgpu_atif_verify_interface(&amdgpu_acpi_priv.atif);
1203f9b7f370SAlex Deucher 	if (ret) {
1204f9b7f370SAlex Deucher 		amdgpu_acpi_priv.atif.handle = 0;
1205f9b7f370SAlex Deucher 		return false;
1206f9b7f370SAlex Deucher 	}
1207f9b7f370SAlex Deucher 	return true;
1208f9b7f370SAlex Deucher }
1209f9b7f370SAlex Deucher 
1210f9b7f370SAlex Deucher /**
1211f9b7f370SAlex Deucher  * amdgpu_atcs_pci_probe_handle - look up the ATCS handle
1212f9b7f370SAlex Deucher  *
1213f9b7f370SAlex Deucher  * @pdev: pci device
1214f9b7f370SAlex Deucher  *
1215f9b7f370SAlex Deucher  * Look up the ATCS handles (all asics).
1216f9b7f370SAlex Deucher  * Returns true if the handle is found, false if not.
1217f9b7f370SAlex Deucher  */
1218f9b7f370SAlex Deucher static bool amdgpu_atcs_pci_probe_handle(struct pci_dev *pdev)
1219f9b7f370SAlex Deucher {
1220f9b7f370SAlex Deucher 	char acpi_method_name[255] = { 0 };
1221f9b7f370SAlex Deucher 	struct acpi_buffer buffer = { sizeof(acpi_method_name), acpi_method_name };
1222f9b7f370SAlex Deucher 	acpi_handle dhandle, atcs_handle;
1223f9b7f370SAlex Deucher 	acpi_status status;
1224f9b7f370SAlex Deucher 	int ret;
1225f9b7f370SAlex Deucher 
1226f9b7f370SAlex Deucher 	dhandle = ACPI_HANDLE(&pdev->dev);
1227f9b7f370SAlex Deucher 	if (!dhandle)
1228f9b7f370SAlex Deucher 		return false;
1229f9b7f370SAlex Deucher 
1230f9b7f370SAlex Deucher 	status = acpi_get_handle(dhandle, "ATCS", &atcs_handle);
1231556bdae3SJingyu Wang 	if (ACPI_FAILURE(status))
1232f9b7f370SAlex Deucher 		return false;
1233556bdae3SJingyu Wang 
1234f9b7f370SAlex Deucher 	amdgpu_acpi_priv.atcs.handle = atcs_handle;
1235f9b7f370SAlex Deucher 	acpi_get_name(amdgpu_acpi_priv.atcs.handle, ACPI_FULL_PATHNAME, &buffer);
1236f9b7f370SAlex Deucher 	DRM_DEBUG_DRIVER("Found ATCS handle %s\n", acpi_method_name);
1237f9b7f370SAlex Deucher 	ret = amdgpu_atcs_verify_interface(&amdgpu_acpi_priv.atcs);
1238f9b7f370SAlex Deucher 	if (ret) {
1239f9b7f370SAlex Deucher 		amdgpu_acpi_priv.atcs.handle = 0;
1240f9b7f370SAlex Deucher 		return false;
1241f9b7f370SAlex Deucher 	}
1242f9b7f370SAlex Deucher 	return true;
1243f9b7f370SAlex Deucher }
1244f9b7f370SAlex Deucher 
1245aaee0ce4STim Huang 
1246aaee0ce4STim Huang /**
1247aaee0ce4STim Huang  * amdgpu_acpi_should_gpu_reset
1248aaee0ce4STim Huang  *
1249aaee0ce4STim Huang  * @adev: amdgpu_device_pointer
1250aaee0ce4STim Huang  *
1251aaee0ce4STim Huang  * returns true if should reset GPU, false if not
1252aaee0ce4STim Huang  */
1253aaee0ce4STim Huang bool amdgpu_acpi_should_gpu_reset(struct amdgpu_device *adev)
1254aaee0ce4STim Huang {
1255980d5baeSTim Huang 	if ((adev->flags & AMD_IS_APU) &&
1256980d5baeSTim Huang 	    adev->gfx.imu.funcs) /* Not need to do mode2 reset for IMU enabled APUs */
1257980d5baeSTim Huang 		return false;
1258980d5baeSTim Huang 
1259980d5baeSTim Huang 	if ((adev->flags & AMD_IS_APU) &&
1260980d5baeSTim Huang 	    amdgpu_acpi_is_s3_active(adev))
1261aaee0ce4STim Huang 		return false;
1262aaee0ce4STim Huang 
1263aaee0ce4STim Huang 	if (amdgpu_sriov_vf(adev))
1264aaee0ce4STim Huang 		return false;
1265aaee0ce4STim Huang 
1266aaee0ce4STim Huang #if IS_ENABLED(CONFIG_SUSPEND)
1267aaee0ce4STim Huang 	return pm_suspend_target_state != PM_SUSPEND_TO_IDLE;
1268aaee0ce4STim Huang #else
1269aaee0ce4STim Huang 	return true;
1270aaee0ce4STim Huang #endif
1271aaee0ce4STim Huang }
1272aaee0ce4STim Huang 
1273f9b7f370SAlex Deucher /*
1274f9b7f370SAlex Deucher  * amdgpu_acpi_detect - detect ACPI ATIF/ATCS methods
1275f9b7f370SAlex Deucher  *
1276f9b7f370SAlex Deucher  * Check if we have the ATIF/ATCS methods and populate
1277f9b7f370SAlex Deucher  * the structures in the driver.
1278f9b7f370SAlex Deucher  */
1279f9b7f370SAlex Deucher void amdgpu_acpi_detect(void)
1280f9b7f370SAlex Deucher {
1281f9b7f370SAlex Deucher 	struct amdgpu_atif *atif = &amdgpu_acpi_priv.atif;
1282f9b7f370SAlex Deucher 	struct amdgpu_atcs *atcs = &amdgpu_acpi_priv.atcs;
1283f9b7f370SAlex Deucher 	struct pci_dev *pdev = NULL;
1284f9b7f370SAlex Deucher 	int ret;
1285f9b7f370SAlex Deucher 
1286f9b7f370SAlex Deucher 	while ((pdev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, pdev)) != NULL) {
1287f9b7f370SAlex Deucher 		if (!atif->handle)
1288f9b7f370SAlex Deucher 			amdgpu_atif_pci_probe_handle(pdev);
1289f9b7f370SAlex Deucher 		if (!atcs->handle)
1290f9b7f370SAlex Deucher 			amdgpu_atcs_pci_probe_handle(pdev);
1291f9b7f370SAlex Deucher 	}
1292f9b7f370SAlex Deucher 
1293f9b7f370SAlex Deucher 	while ((pdev = pci_get_class(PCI_CLASS_DISPLAY_OTHER << 8, pdev)) != NULL) {
1294f9b7f370SAlex Deucher 		if (!atif->handle)
1295f9b7f370SAlex Deucher 			amdgpu_atif_pci_probe_handle(pdev);
1296f9b7f370SAlex Deucher 		if (!atcs->handle)
1297f9b7f370SAlex Deucher 			amdgpu_atcs_pci_probe_handle(pdev);
1298f9b7f370SAlex Deucher 	}
1299d38ceaf9SAlex Deucher 
1300d38ceaf9SAlex Deucher 	if (atif->functions.sbios_requests && !atif->functions.system_params) {
1301d38ceaf9SAlex Deucher 		/* XXX check this workraround, if sbios request function is
1302d38ceaf9SAlex Deucher 		 * present we have to see how it's configured in the system
1303d38ceaf9SAlex Deucher 		 * params
1304d38ceaf9SAlex Deucher 		 */
1305d38ceaf9SAlex Deucher 		atif->functions.system_params = true;
1306d38ceaf9SAlex Deucher 	}
1307d38ceaf9SAlex Deucher 
1308d38ceaf9SAlex Deucher 	if (atif->functions.system_params) {
1309280cf1a9SLyude Paul 		ret = amdgpu_atif_get_notification_params(atif);
1310d38ceaf9SAlex Deucher 		if (ret) {
1311d38ceaf9SAlex Deucher 			DRM_DEBUG_DRIVER("Call to GET_SYSTEM_PARAMS failed: %d\n",
1312d38ceaf9SAlex Deucher 					ret);
1313d38ceaf9SAlex Deucher 			/* Disable notification */
1314d38ceaf9SAlex Deucher 			atif->notification_cfg.enabled = false;
1315d38ceaf9SAlex Deucher 		}
1316d38ceaf9SAlex Deucher 	}
1317d38ceaf9SAlex Deucher 
1318206bbafeSDavid Francis 	if (atif->functions.query_backlight_transfer_characteristics) {
1319206bbafeSDavid Francis 		ret = amdgpu_atif_query_backlight_caps(atif);
1320206bbafeSDavid Francis 		if (ret) {
1321206bbafeSDavid Francis 			DRM_DEBUG_DRIVER("Call to QUERY_BACKLIGHT_TRANSFER_CHARACTERISTICS failed: %d\n",
1322206bbafeSDavid Francis 					ret);
1323206bbafeSDavid Francis 			atif->backlight_caps.caps_valid = false;
1324206bbafeSDavid Francis 		}
1325206bbafeSDavid Francis 	} else {
1326206bbafeSDavid Francis 		atif->backlight_caps.caps_valid = false;
1327206bbafeSDavid Francis 	}
1328*4d5275abSLijo Lazar 
1329*4d5275abSLijo Lazar 	amdgpu_acpi_enumerate_xcc();
1330*4d5275abSLijo Lazar }
1331*4d5275abSLijo Lazar 
1332*4d5275abSLijo Lazar void amdgpu_acpi_release(void)
1333*4d5275abSLijo Lazar {
1334*4d5275abSLijo Lazar 	struct amdgpu_acpi_dev_info *dev_info, *dev_tmp;
1335*4d5275abSLijo Lazar 	struct amdgpu_acpi_xcc_info *xcc_info, *xcc_tmp;
1336*4d5275abSLijo Lazar 
1337*4d5275abSLijo Lazar 	if (list_empty(&amdgpu_acpi_dev_list))
1338*4d5275abSLijo Lazar 		return;
1339*4d5275abSLijo Lazar 
1340*4d5275abSLijo Lazar 	list_for_each_entry_safe(dev_info, dev_tmp, &amdgpu_acpi_dev_list,
1341*4d5275abSLijo Lazar 				 list) {
1342*4d5275abSLijo Lazar 		list_for_each_entry_safe(xcc_info, xcc_tmp, &dev_info->xcc_list,
1343*4d5275abSLijo Lazar 					 list) {
1344*4d5275abSLijo Lazar 			list_del(&xcc_info->list);
1345*4d5275abSLijo Lazar 			kfree(xcc_info);
1346*4d5275abSLijo Lazar 		}
1347*4d5275abSLijo Lazar 
1348*4d5275abSLijo Lazar 		list_del(&dev_info->list);
1349*4d5275abSLijo Lazar 		kfree(dev_info);
1350*4d5275abSLijo Lazar 	}
1351d38ceaf9SAlex Deucher }
13524cd078dcSPrike Liang 
1353f588a1bbSMario Limonciello #if IS_ENABLED(CONFIG_SUSPEND)
13544cd078dcSPrike Liang /**
135518b66aceSMario Limonciello  * amdgpu_acpi_is_s3_active
135618b66aceSMario Limonciello  *
135718b66aceSMario Limonciello  * @adev: amdgpu_device_pointer
135818b66aceSMario Limonciello  *
135918b66aceSMario Limonciello  * returns true if supported, false if not.
136018b66aceSMario Limonciello  */
136118b66aceSMario Limonciello bool amdgpu_acpi_is_s3_active(struct amdgpu_device *adev)
136218b66aceSMario Limonciello {
136318b66aceSMario Limonciello 	return !(adev->flags & AMD_IS_APU) ||
136418b66aceSMario Limonciello 		(pm_suspend_target_state == PM_SUSPEND_MEM);
136518b66aceSMario Limonciello }
136618b66aceSMario Limonciello 
136718b66aceSMario Limonciello /**
1368d0260f62SPratik Vishwakarma  * amdgpu_acpi_is_s0ix_active
13694cd078dcSPrike Liang  *
13701fdbbc12SFabio M. De Francesco  * @adev: amdgpu_device_pointer
13711fdbbc12SFabio M. De Francesco  *
13724cd078dcSPrike Liang  * returns true if supported, false if not.
13734cd078dcSPrike Liang  */
1374d0260f62SPratik Vishwakarma bool amdgpu_acpi_is_s0ix_active(struct amdgpu_device *adev)
13754cd078dcSPrike Liang {
1376f588a1bbSMario Limonciello 	if (!(adev->flags & AMD_IS_APU) ||
1377f588a1bbSMario Limonciello 	    (pm_suspend_target_state != PM_SUSPEND_TO_IDLE))
1378f588a1bbSMario Limonciello 		return false;
1379f588a1bbSMario Limonciello 
1380ca475186SMario Limonciello 	if (adev->asic_type < CHIP_RAVEN)
1381ca475186SMario Limonciello 		return false;
1382ca475186SMario Limonciello 
138361ebd2feSRafael J. Wysocki 	/*
138461ebd2feSRafael J. Wysocki 	 * If ACPI_FADT_LOW_POWER_S0 is not set in the FADT, it is generally
138561ebd2feSRafael J. Wysocki 	 * risky to do any special firmware-related preparations for entering
138661ebd2feSRafael J. Wysocki 	 * S0ix even though the system is suspending to idle, so return false
138761ebd2feSRafael J. Wysocki 	 * in that case.
138861ebd2feSRafael J. Wysocki 	 */
1389cf488dcdSMario Limonciello 	if (!(acpi_gbl_FADT.flags & ACPI_FADT_LOW_POWER_S0))
1390f588a1bbSMario Limonciello 		dev_warn_once(adev->dev,
1391f588a1bbSMario Limonciello 			      "Power consumption will be higher as BIOS has not been configured for suspend-to-idle.\n"
1392f588a1bbSMario Limonciello 			      "To use suspend-to-idle change the sleep mode in BIOS setup.\n");
1393f588a1bbSMario Limonciello 
1394f588a1bbSMario Limonciello #if !IS_ENABLED(CONFIG_AMD_PMC)
1395f588a1bbSMario Limonciello 	dev_warn_once(adev->dev,
1396f588a1bbSMario Limonciello 		      "Power consumption will be higher as the kernel has not been compiled with CONFIG_AMD_PMC.\n");
1397f588a1bbSMario Limonciello #endif /* CONFIG_AMD_PMC */
1398cf488dcdSMario Limonciello 	return true;
1399f588a1bbSMario Limonciello }
1400f588a1bbSMario Limonciello 
1401f588a1bbSMario Limonciello #endif /* CONFIG_SUSPEND */
1402