xref: /linux/drivers/gpu/drm/amd/amdgpu/amdgpu_acpi.c (revision 18bf400530ca06e4bffcc9ce4741cf7a3526b85f)
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>
291cc82301SLijo Lazar #include <linux/xarray.h>
30d38ceaf9SAlex Deucher #include <linux/power_supply.h>
311b0f568dSAlex Deucher #include <linux/pm_runtime.h>
3291b03fc6SPratik Vishwakarma #include <linux/suspend.h>
33d38ceaf9SAlex Deucher #include <acpi/video.h>
344cd078dcSPrike Liang #include <acpi/actbl.h>
35fdf2f6c5SSam Ravnborg 
36d38ceaf9SAlex Deucher #include "amdgpu.h"
3798c65108SJean Delvare #include "amdgpu_pm.h"
385df58525SHuang Rui #include "amdgpu_display.h"
3966dc0dddSRex Zhu #include "amd_acpi.h"
40d38ceaf9SAlex Deucher #include "atom.h"
41d38ceaf9SAlex Deucher 
424d5275abSLijo Lazar /* Declare GUID for AMD _DSM method for XCCs */
434d5275abSLijo Lazar static const guid_t amd_xcc_dsm_guid = GUID_INIT(0x8267f5d5, 0xa556, 0x44f2,
444d5275abSLijo Lazar 						 0xb8, 0xb4, 0x45, 0x56, 0x2e,
454d5275abSLijo Lazar 						 0x8c, 0x5b, 0xec);
464d5275abSLijo Lazar 
474d5275abSLijo Lazar #define AMD_XCC_HID_START 3000
484d5275abSLijo Lazar #define AMD_XCC_DSM_GET_NUM_FUNCS 0
494d5275abSLijo Lazar #define AMD_XCC_DSM_GET_SUPP_MODE 1
504d5275abSLijo Lazar #define AMD_XCC_DSM_GET_XCP_MODE 2
514d5275abSLijo Lazar #define AMD_XCC_DSM_GET_VF_XCC_MAPPING 4
524d5275abSLijo Lazar #define AMD_XCC_DSM_GET_TMR_INFO 5
534d5275abSLijo Lazar #define AMD_XCC_DSM_NUM_FUNCS 5
544d5275abSLijo Lazar 
554d5275abSLijo Lazar #define AMD_XCC_MAX_HID 24
564d5275abSLijo Lazar 
571cc82301SLijo Lazar struct xarray numa_info_xa;
581cc82301SLijo Lazar 
594d5275abSLijo Lazar /* Encapsulates the XCD acpi object information */
604d5275abSLijo Lazar struct amdgpu_acpi_xcc_info {
614d5275abSLijo Lazar 	struct list_head list;
621cc82301SLijo Lazar 	struct amdgpu_numa_info *numa_info;
634d5275abSLijo Lazar 	uint8_t xcp_node;
644d5275abSLijo Lazar 	uint8_t phy_id;
654d5275abSLijo Lazar 	acpi_handle handle;
664d5275abSLijo Lazar };
674d5275abSLijo Lazar 
684d5275abSLijo Lazar struct amdgpu_acpi_dev_info {
694d5275abSLijo Lazar 	struct list_head list;
704d5275abSLijo Lazar 	struct list_head xcc_list;
714d5275abSLijo Lazar 	uint16_t bdf;
724d5275abSLijo Lazar 	uint16_t supp_xcp_mode;
734d5275abSLijo Lazar 	uint16_t xcp_mode;
744d5275abSLijo Lazar 	uint16_t mem_mode;
754d5275abSLijo Lazar 	uint64_t tmr_base;
764d5275abSLijo Lazar 	uint64_t tmr_size;
774d5275abSLijo Lazar };
784d5275abSLijo Lazar 
794d5275abSLijo Lazar struct list_head amdgpu_acpi_dev_list;
804d5275abSLijo Lazar 
81102c16a0SLyude Paul struct amdgpu_atif_notification_cfg {
82102c16a0SLyude Paul 	bool enabled;
83102c16a0SLyude Paul 	int command_code;
84102c16a0SLyude Paul };
85102c16a0SLyude Paul 
86102c16a0SLyude Paul struct amdgpu_atif_notifications {
87102c16a0SLyude Paul 	bool thermal_state;
88102c16a0SLyude Paul 	bool forced_power_state;
89102c16a0SLyude Paul 	bool system_power_state;
90102c16a0SLyude Paul 	bool brightness_change;
91102c16a0SLyude Paul 	bool dgpu_display_event;
927349a3afSDavid Francis 	bool gpu_package_power_limit;
93102c16a0SLyude Paul };
94102c16a0SLyude Paul 
95102c16a0SLyude Paul struct amdgpu_atif_functions {
96102c16a0SLyude Paul 	bool system_params;
97102c16a0SLyude Paul 	bool sbios_requests;
98102c16a0SLyude Paul 	bool temperature_change;
997349a3afSDavid Francis 	bool query_backlight_transfer_characteristics;
1007349a3afSDavid Francis 	bool ready_to_undock;
1017349a3afSDavid Francis 	bool external_gpu_information;
102102c16a0SLyude Paul };
103102c16a0SLyude Paul 
104102c16a0SLyude Paul struct amdgpu_atif {
105280cf1a9SLyude Paul 	acpi_handle handle;
106280cf1a9SLyude Paul 
107102c16a0SLyude Paul 	struct amdgpu_atif_notifications notifications;
108102c16a0SLyude Paul 	struct amdgpu_atif_functions functions;
109102c16a0SLyude Paul 	struct amdgpu_atif_notification_cfg notification_cfg;
11097d798b2SAlex Deucher 	struct backlight_device *bd;
111206bbafeSDavid Francis 	struct amdgpu_dm_backlight_caps backlight_caps;
112102c16a0SLyude Paul };
113102c16a0SLyude Paul 
11477bf762fSAlex Deucher struct amdgpu_atcs_functions {
11577bf762fSAlex Deucher 	bool get_ext_state;
11677bf762fSAlex Deucher 	bool pcie_perf_req;
11777bf762fSAlex Deucher 	bool pcie_dev_rdy;
11877bf762fSAlex Deucher 	bool pcie_bus_width;
11916eb48c6SSathishkumar S 	bool power_shift_control;
12077bf762fSAlex Deucher };
12177bf762fSAlex Deucher 
12277bf762fSAlex Deucher struct amdgpu_atcs {
12377bf762fSAlex Deucher 	acpi_handle handle;
12477bf762fSAlex Deucher 
12577bf762fSAlex Deucher 	struct amdgpu_atcs_functions functions;
12677bf762fSAlex Deucher };
12777bf762fSAlex Deucher 
128f9b7f370SAlex Deucher static struct amdgpu_acpi_priv {
129f9b7f370SAlex Deucher 	struct amdgpu_atif atif;
130f9b7f370SAlex Deucher 	struct amdgpu_atcs atcs;
131f9b7f370SAlex Deucher } amdgpu_acpi_priv;
132f9b7f370SAlex Deucher 
133d38ceaf9SAlex Deucher /* Call the ATIF method
134d38ceaf9SAlex Deucher  */
135d38ceaf9SAlex Deucher /**
136d38ceaf9SAlex Deucher  * amdgpu_atif_call - call an ATIF method
137d38ceaf9SAlex Deucher  *
13877bf762fSAlex Deucher  * @atif: atif structure
139d38ceaf9SAlex Deucher  * @function: the ATIF function to execute
140d38ceaf9SAlex Deucher  * @params: ATIF function params
141d38ceaf9SAlex Deucher  *
142d38ceaf9SAlex Deucher  * Executes the requested ATIF function (all asics).
143d38ceaf9SAlex Deucher  * Returns a pointer to the acpi output buffer.
144d38ceaf9SAlex Deucher  */
145280cf1a9SLyude Paul static union acpi_object *amdgpu_atif_call(struct amdgpu_atif *atif,
146280cf1a9SLyude Paul 					   int function,
147d38ceaf9SAlex Deucher 					   struct acpi_buffer *params)
148d38ceaf9SAlex Deucher {
149d38ceaf9SAlex Deucher 	acpi_status status;
150d38ceaf9SAlex Deucher 	union acpi_object atif_arg_elements[2];
151d38ceaf9SAlex Deucher 	struct acpi_object_list atif_arg;
152d38ceaf9SAlex Deucher 	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
153d38ceaf9SAlex Deucher 
154d38ceaf9SAlex Deucher 	atif_arg.count = 2;
155d38ceaf9SAlex Deucher 	atif_arg.pointer = &atif_arg_elements[0];
156d38ceaf9SAlex Deucher 
157d38ceaf9SAlex Deucher 	atif_arg_elements[0].type = ACPI_TYPE_INTEGER;
158d38ceaf9SAlex Deucher 	atif_arg_elements[0].integer.value = function;
159d38ceaf9SAlex Deucher 
160d38ceaf9SAlex Deucher 	if (params) {
161d38ceaf9SAlex Deucher 		atif_arg_elements[1].type = ACPI_TYPE_BUFFER;
162d38ceaf9SAlex Deucher 		atif_arg_elements[1].buffer.length = params->length;
163d38ceaf9SAlex Deucher 		atif_arg_elements[1].buffer.pointer = params->pointer;
164d38ceaf9SAlex Deucher 	} else {
165d38ceaf9SAlex Deucher 		/* We need a second fake parameter */
166d38ceaf9SAlex Deucher 		atif_arg_elements[1].type = ACPI_TYPE_INTEGER;
167d38ceaf9SAlex Deucher 		atif_arg_elements[1].integer.value = 0;
168d38ceaf9SAlex Deucher 	}
169d38ceaf9SAlex Deucher 
170280cf1a9SLyude Paul 	status = acpi_evaluate_object(atif->handle, NULL, &atif_arg,
171280cf1a9SLyude Paul 				      &buffer);
172d38ceaf9SAlex Deucher 
173d38ceaf9SAlex Deucher 	/* Fail only if calling the method fails and ATIF is supported */
174d38ceaf9SAlex Deucher 	if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
175d38ceaf9SAlex Deucher 		DRM_DEBUG_DRIVER("failed to evaluate ATIF got %s\n",
176d38ceaf9SAlex Deucher 				 acpi_format_exception(status));
177d38ceaf9SAlex Deucher 		kfree(buffer.pointer);
178d38ceaf9SAlex Deucher 		return NULL;
179d38ceaf9SAlex Deucher 	}
180d38ceaf9SAlex Deucher 
181d38ceaf9SAlex Deucher 	return buffer.pointer;
182d38ceaf9SAlex Deucher }
183d38ceaf9SAlex Deucher 
184d38ceaf9SAlex Deucher /**
185d38ceaf9SAlex Deucher  * amdgpu_atif_parse_notification - parse supported notifications
186d38ceaf9SAlex Deucher  *
187d38ceaf9SAlex Deucher  * @n: supported notifications struct
188d38ceaf9SAlex Deucher  * @mask: supported notifications mask from ATIF
189d38ceaf9SAlex Deucher  *
190d38ceaf9SAlex Deucher  * Use the supported notifications mask from ATIF function
191d38ceaf9SAlex Deucher  * ATIF_FUNCTION_VERIFY_INTERFACE to determine what notifications
192d38ceaf9SAlex Deucher  * are supported (all asics).
193d38ceaf9SAlex Deucher  */
194d38ceaf9SAlex Deucher static void amdgpu_atif_parse_notification(struct amdgpu_atif_notifications *n, u32 mask)
195d38ceaf9SAlex Deucher {
196d38ceaf9SAlex Deucher 	n->thermal_state = mask & ATIF_THERMAL_STATE_CHANGE_REQUEST_SUPPORTED;
197d38ceaf9SAlex Deucher 	n->forced_power_state = mask & ATIF_FORCED_POWER_STATE_CHANGE_REQUEST_SUPPORTED;
198d38ceaf9SAlex Deucher 	n->system_power_state = mask & ATIF_SYSTEM_POWER_SOURCE_CHANGE_REQUEST_SUPPORTED;
199d38ceaf9SAlex Deucher 	n->brightness_change = mask & ATIF_PANEL_BRIGHTNESS_CHANGE_REQUEST_SUPPORTED;
200d38ceaf9SAlex Deucher 	n->dgpu_display_event = mask & ATIF_DGPU_DISPLAY_EVENT_SUPPORTED;
2017349a3afSDavid Francis 	n->gpu_package_power_limit = mask & ATIF_GPU_PACKAGE_POWER_LIMIT_REQUEST_SUPPORTED;
202d38ceaf9SAlex Deucher }
203d38ceaf9SAlex Deucher 
204d38ceaf9SAlex Deucher /**
205d38ceaf9SAlex Deucher  * amdgpu_atif_parse_functions - parse supported functions
206d38ceaf9SAlex Deucher  *
207d38ceaf9SAlex Deucher  * @f: supported functions struct
208d38ceaf9SAlex Deucher  * @mask: supported functions mask from ATIF
209d38ceaf9SAlex Deucher  *
210d38ceaf9SAlex Deucher  * Use the supported functions mask from ATIF function
211d38ceaf9SAlex Deucher  * ATIF_FUNCTION_VERIFY_INTERFACE to determine what functions
212d38ceaf9SAlex Deucher  * are supported (all asics).
213d38ceaf9SAlex Deucher  */
214d38ceaf9SAlex Deucher static void amdgpu_atif_parse_functions(struct amdgpu_atif_functions *f, u32 mask)
215d38ceaf9SAlex Deucher {
216d38ceaf9SAlex Deucher 	f->system_params = mask & ATIF_GET_SYSTEM_PARAMETERS_SUPPORTED;
217d38ceaf9SAlex Deucher 	f->sbios_requests = mask & ATIF_GET_SYSTEM_BIOS_REQUESTS_SUPPORTED;
218d38ceaf9SAlex Deucher 	f->temperature_change = mask & ATIF_TEMPERATURE_CHANGE_NOTIFICATION_SUPPORTED;
2197349a3afSDavid Francis 	f->query_backlight_transfer_characteristics =
2207349a3afSDavid Francis 		mask & ATIF_QUERY_BACKLIGHT_TRANSFER_CHARACTERISTICS_SUPPORTED;
2217349a3afSDavid Francis 	f->ready_to_undock = mask & ATIF_READY_TO_UNDOCK_NOTIFICATION_SUPPORTED;
2227349a3afSDavid Francis 	f->external_gpu_information = mask & ATIF_GET_EXTERNAL_GPU_INFORMATION_SUPPORTED;
223d38ceaf9SAlex Deucher }
224d38ceaf9SAlex Deucher 
225d38ceaf9SAlex Deucher /**
226d38ceaf9SAlex Deucher  * amdgpu_atif_verify_interface - verify ATIF
227d38ceaf9SAlex Deucher  *
228d38ceaf9SAlex Deucher  * @atif: amdgpu atif struct
229d38ceaf9SAlex Deucher  *
230d38ceaf9SAlex Deucher  * Execute the ATIF_FUNCTION_VERIFY_INTERFACE ATIF function
231d38ceaf9SAlex Deucher  * to initialize ATIF and determine what features are supported
232d38ceaf9SAlex Deucher  * (all asics).
233d38ceaf9SAlex Deucher  * returns 0 on success, error on failure.
234d38ceaf9SAlex Deucher  */
235280cf1a9SLyude Paul static int amdgpu_atif_verify_interface(struct amdgpu_atif *atif)
236d38ceaf9SAlex Deucher {
237d38ceaf9SAlex Deucher 	union acpi_object *info;
238d38ceaf9SAlex Deucher 	struct atif_verify_interface output;
239d38ceaf9SAlex Deucher 	size_t size;
240d38ceaf9SAlex Deucher 	int err = 0;
241d38ceaf9SAlex Deucher 
242280cf1a9SLyude Paul 	info = amdgpu_atif_call(atif, ATIF_FUNCTION_VERIFY_INTERFACE, NULL);
243d38ceaf9SAlex Deucher 	if (!info)
244d38ceaf9SAlex Deucher 		return -EIO;
245d38ceaf9SAlex Deucher 
246d38ceaf9SAlex Deucher 	memset(&output, 0, sizeof(output));
247d38ceaf9SAlex Deucher 
248d38ceaf9SAlex Deucher 	size = *(u16 *) info->buffer.pointer;
249d38ceaf9SAlex Deucher 	if (size < 12) {
250d38ceaf9SAlex Deucher 		DRM_INFO("ATIF buffer is too small: %zu\n", size);
251d38ceaf9SAlex Deucher 		err = -EINVAL;
252d38ceaf9SAlex Deucher 		goto out;
253d38ceaf9SAlex Deucher 	}
254d38ceaf9SAlex Deucher 	size = min(sizeof(output), size);
255d38ceaf9SAlex Deucher 
256d38ceaf9SAlex Deucher 	memcpy(&output, info->buffer.pointer, size);
257d38ceaf9SAlex Deucher 
258d38ceaf9SAlex Deucher 	/* TODO: check version? */
259d38ceaf9SAlex Deucher 	DRM_DEBUG_DRIVER("ATIF version %u\n", output.version);
260d38ceaf9SAlex Deucher 
261d38ceaf9SAlex Deucher 	amdgpu_atif_parse_notification(&atif->notifications, output.notification_mask);
262d38ceaf9SAlex Deucher 	amdgpu_atif_parse_functions(&atif->functions, output.function_bits);
263d38ceaf9SAlex Deucher 
264d38ceaf9SAlex Deucher out:
265d38ceaf9SAlex Deucher 	kfree(info);
266d38ceaf9SAlex Deucher 	return err;
267d38ceaf9SAlex Deucher }
268d38ceaf9SAlex Deucher 
269d38ceaf9SAlex Deucher /**
270d38ceaf9SAlex Deucher  * amdgpu_atif_get_notification_params - determine notify configuration
271d38ceaf9SAlex Deucher  *
2721fdbbc12SFabio M. De Francesco  * @atif: acpi handle
273d38ceaf9SAlex Deucher  *
274d38ceaf9SAlex Deucher  * Execute the ATIF_FUNCTION_GET_SYSTEM_PARAMETERS ATIF function
275d38ceaf9SAlex Deucher  * to determine if a notifier is used and if so which one
276d38ceaf9SAlex Deucher  * (all asics).  This is either Notify(VGA, 0x81) or Notify(VGA, n)
277d38ceaf9SAlex Deucher  * where n is specified in the result if a notifier is used.
278d38ceaf9SAlex Deucher  * Returns 0 on success, error on failure.
279d38ceaf9SAlex Deucher  */
280280cf1a9SLyude Paul static int amdgpu_atif_get_notification_params(struct amdgpu_atif *atif)
281d38ceaf9SAlex Deucher {
282d38ceaf9SAlex Deucher 	union acpi_object *info;
283280cf1a9SLyude Paul 	struct amdgpu_atif_notification_cfg *n = &atif->notification_cfg;
284d38ceaf9SAlex Deucher 	struct atif_system_params params;
285d38ceaf9SAlex Deucher 	size_t size;
286d38ceaf9SAlex Deucher 	int err = 0;
287d38ceaf9SAlex Deucher 
288280cf1a9SLyude Paul 	info = amdgpu_atif_call(atif, ATIF_FUNCTION_GET_SYSTEM_PARAMETERS,
289280cf1a9SLyude Paul 				NULL);
290d38ceaf9SAlex Deucher 	if (!info) {
291d38ceaf9SAlex Deucher 		err = -EIO;
292d38ceaf9SAlex Deucher 		goto out;
293d38ceaf9SAlex Deucher 	}
294d38ceaf9SAlex Deucher 
295d38ceaf9SAlex Deucher 	size = *(u16 *) info->buffer.pointer;
296d38ceaf9SAlex Deucher 	if (size < 10) {
297d38ceaf9SAlex Deucher 		err = -EINVAL;
298d38ceaf9SAlex Deucher 		goto out;
299d38ceaf9SAlex Deucher 	}
300d38ceaf9SAlex Deucher 
301d38ceaf9SAlex Deucher 	memset(&params, 0, sizeof(params));
302d38ceaf9SAlex Deucher 	size = min(sizeof(params), size);
303d38ceaf9SAlex Deucher 	memcpy(&params, info->buffer.pointer, size);
304d38ceaf9SAlex Deucher 
305d38ceaf9SAlex Deucher 	DRM_DEBUG_DRIVER("SYSTEM_PARAMS: mask = %#x, flags = %#x\n",
306d38ceaf9SAlex Deucher 			params.flags, params.valid_mask);
307d38ceaf9SAlex Deucher 	params.flags = params.flags & params.valid_mask;
308d38ceaf9SAlex Deucher 
309d38ceaf9SAlex Deucher 	if ((params.flags & ATIF_NOTIFY_MASK) == ATIF_NOTIFY_NONE) {
310d38ceaf9SAlex Deucher 		n->enabled = false;
311d38ceaf9SAlex Deucher 		n->command_code = 0;
312d38ceaf9SAlex Deucher 	} else if ((params.flags & ATIF_NOTIFY_MASK) == ATIF_NOTIFY_81) {
313d38ceaf9SAlex Deucher 		n->enabled = true;
314d38ceaf9SAlex Deucher 		n->command_code = 0x81;
315d38ceaf9SAlex Deucher 	} else {
316d38ceaf9SAlex Deucher 		if (size < 11) {
317d38ceaf9SAlex Deucher 			err = -EINVAL;
318d38ceaf9SAlex Deucher 			goto out;
319d38ceaf9SAlex Deucher 		}
320d38ceaf9SAlex Deucher 		n->enabled = true;
321d38ceaf9SAlex Deucher 		n->command_code = params.command_code;
322d38ceaf9SAlex Deucher 	}
323d38ceaf9SAlex Deucher 
324d38ceaf9SAlex Deucher out:
325d38ceaf9SAlex Deucher 	DRM_DEBUG_DRIVER("Notification %s, command code = %#x\n",
326d38ceaf9SAlex Deucher 			(n->enabled ? "enabled" : "disabled"),
327d38ceaf9SAlex Deucher 			n->command_code);
328d38ceaf9SAlex Deucher 	kfree(info);
329d38ceaf9SAlex Deucher 	return err;
330d38ceaf9SAlex Deucher }
331d38ceaf9SAlex Deucher 
332d38ceaf9SAlex Deucher /**
333206bbafeSDavid Francis  * amdgpu_atif_query_backlight_caps - get min and max backlight input signal
334206bbafeSDavid Francis  *
3351fdbbc12SFabio M. De Francesco  * @atif: acpi handle
336206bbafeSDavid Francis  *
337206bbafeSDavid Francis  * Execute the QUERY_BRIGHTNESS_TRANSFER_CHARACTERISTICS ATIF function
338206bbafeSDavid Francis  * to determine the acceptable range of backlight values
339206bbafeSDavid Francis  *
340206bbafeSDavid Francis  * Backlight_caps.caps_valid will be set to true if the query is successful
341206bbafeSDavid Francis  *
342206bbafeSDavid Francis  * The input signals are in range 0-255
343206bbafeSDavid Francis  *
344206bbafeSDavid Francis  * This function assumes the display with backlight is the first LCD
345206bbafeSDavid Francis  *
346206bbafeSDavid Francis  * Returns 0 on success, error on failure.
347206bbafeSDavid Francis  */
348206bbafeSDavid Francis static int amdgpu_atif_query_backlight_caps(struct amdgpu_atif *atif)
349206bbafeSDavid Francis {
350206bbafeSDavid Francis 	union acpi_object *info;
351206bbafeSDavid Francis 	struct atif_qbtc_output characteristics;
352206bbafeSDavid Francis 	struct atif_qbtc_arguments arguments;
353206bbafeSDavid Francis 	struct acpi_buffer params;
354206bbafeSDavid Francis 	size_t size;
355206bbafeSDavid Francis 	int err = 0;
356206bbafeSDavid Francis 
357206bbafeSDavid Francis 	arguments.size = sizeof(arguments);
358206bbafeSDavid Francis 	arguments.requested_display = ATIF_QBTC_REQUEST_LCD1;
359206bbafeSDavid Francis 
360206bbafeSDavid Francis 	params.length = sizeof(arguments);
361206bbafeSDavid Francis 	params.pointer = (void *)&arguments;
362206bbafeSDavid Francis 
363206bbafeSDavid Francis 	info = amdgpu_atif_call(atif,
364206bbafeSDavid Francis 		ATIF_FUNCTION_QUERY_BRIGHTNESS_TRANSFER_CHARACTERISTICS,
365206bbafeSDavid Francis 		&params);
366206bbafeSDavid Francis 	if (!info) {
367206bbafeSDavid Francis 		err = -EIO;
368206bbafeSDavid Francis 		goto out;
369206bbafeSDavid Francis 	}
370206bbafeSDavid Francis 
371206bbafeSDavid Francis 	size = *(u16 *) info->buffer.pointer;
372206bbafeSDavid Francis 	if (size < 10) {
373206bbafeSDavid Francis 		err = -EINVAL;
374206bbafeSDavid Francis 		goto out;
375206bbafeSDavid Francis 	}
376206bbafeSDavid Francis 
377206bbafeSDavid Francis 	memset(&characteristics, 0, sizeof(characteristics));
378206bbafeSDavid Francis 	size = min(sizeof(characteristics), size);
379206bbafeSDavid Francis 	memcpy(&characteristics, info->buffer.pointer, size);
380206bbafeSDavid Francis 
381206bbafeSDavid Francis 	atif->backlight_caps.caps_valid = true;
382206bbafeSDavid Francis 	atif->backlight_caps.min_input_signal =
383206bbafeSDavid Francis 			characteristics.min_input_signal;
384206bbafeSDavid Francis 	atif->backlight_caps.max_input_signal =
385206bbafeSDavid Francis 			characteristics.max_input_signal;
386206bbafeSDavid Francis out:
387206bbafeSDavid Francis 	kfree(info);
388206bbafeSDavid Francis 	return err;
389206bbafeSDavid Francis }
390206bbafeSDavid Francis 
391206bbafeSDavid Francis /**
392d38ceaf9SAlex Deucher  * amdgpu_atif_get_sbios_requests - get requested sbios event
393d38ceaf9SAlex Deucher  *
3941fdbbc12SFabio M. De Francesco  * @atif: acpi handle
395d38ceaf9SAlex Deucher  * @req: atif sbios request struct
396d38ceaf9SAlex Deucher  *
397d38ceaf9SAlex Deucher  * Execute the ATIF_FUNCTION_GET_SYSTEM_BIOS_REQUESTS ATIF function
398d38ceaf9SAlex Deucher  * to determine what requests the sbios is making to the driver
399d38ceaf9SAlex Deucher  * (all asics).
400d38ceaf9SAlex Deucher  * Returns 0 on success, error on failure.
401d38ceaf9SAlex Deucher  */
402280cf1a9SLyude Paul static int amdgpu_atif_get_sbios_requests(struct amdgpu_atif *atif,
403d38ceaf9SAlex Deucher 					  struct atif_sbios_requests *req)
404d38ceaf9SAlex Deucher {
405d38ceaf9SAlex Deucher 	union acpi_object *info;
406d38ceaf9SAlex Deucher 	size_t size;
407d38ceaf9SAlex Deucher 	int count = 0;
408d38ceaf9SAlex Deucher 
409280cf1a9SLyude Paul 	info = amdgpu_atif_call(atif, ATIF_FUNCTION_GET_SYSTEM_BIOS_REQUESTS,
410280cf1a9SLyude Paul 				NULL);
411d38ceaf9SAlex Deucher 	if (!info)
412d38ceaf9SAlex Deucher 		return -EIO;
413d38ceaf9SAlex Deucher 
414d38ceaf9SAlex Deucher 	size = *(u16 *)info->buffer.pointer;
415d38ceaf9SAlex Deucher 	if (size < 0xd) {
416d38ceaf9SAlex Deucher 		count = -EINVAL;
417d38ceaf9SAlex Deucher 		goto out;
418d38ceaf9SAlex Deucher 	}
419d38ceaf9SAlex Deucher 	memset(req, 0, sizeof(*req));
420d38ceaf9SAlex Deucher 
421d38ceaf9SAlex Deucher 	size = min(sizeof(*req), size);
422d38ceaf9SAlex Deucher 	memcpy(req, info->buffer.pointer, size);
423d38ceaf9SAlex Deucher 	DRM_DEBUG_DRIVER("SBIOS pending requests: %#x\n", req->pending);
424d38ceaf9SAlex Deucher 
425d38ceaf9SAlex Deucher 	count = hweight32(req->pending);
426d38ceaf9SAlex Deucher 
427d38ceaf9SAlex Deucher out:
428d38ceaf9SAlex Deucher 	kfree(info);
429d38ceaf9SAlex Deucher 	return count;
430d38ceaf9SAlex Deucher }
431d38ceaf9SAlex Deucher 
432d38ceaf9SAlex Deucher /**
433d38ceaf9SAlex Deucher  * amdgpu_atif_handler - handle ATIF notify requests
434d38ceaf9SAlex Deucher  *
435d38ceaf9SAlex Deucher  * @adev: amdgpu_device pointer
436d38ceaf9SAlex Deucher  * @event: atif sbios request struct
437d38ceaf9SAlex Deucher  *
438d38ceaf9SAlex Deucher  * Checks the acpi event and if it matches an atif event,
439d38ceaf9SAlex Deucher  * handles it.
440582f58deSLyude Paul  *
441582f58deSLyude Paul  * Returns:
442582f58deSLyude Paul  * NOTIFY_BAD or NOTIFY_DONE, depending on the event.
443d38ceaf9SAlex Deucher  */
444b7cecbe8SJean Delvare static int amdgpu_atif_handler(struct amdgpu_device *adev,
445d38ceaf9SAlex Deucher 			       struct acpi_bus_event *event)
446d38ceaf9SAlex Deucher {
447f9b7f370SAlex Deucher 	struct amdgpu_atif *atif = &amdgpu_acpi_priv.atif;
448d38ceaf9SAlex Deucher 	int count;
449d38ceaf9SAlex Deucher 
450d38ceaf9SAlex Deucher 	DRM_DEBUG_DRIVER("event, device_class = %s, type = %#x\n",
451d38ceaf9SAlex Deucher 			event->device_class, event->type);
452d38ceaf9SAlex Deucher 
453d38ceaf9SAlex Deucher 	if (strcmp(event->device_class, ACPI_VIDEO_CLASS) != 0)
454d38ceaf9SAlex Deucher 		return NOTIFY_DONE;
455d38ceaf9SAlex Deucher 
456582f58deSLyude Paul 	/* Is this actually our event? */
457f9b7f370SAlex Deucher 	if (!atif->notification_cfg.enabled ||
458582f58deSLyude Paul 	    event->type != atif->notification_cfg.command_code) {
459582f58deSLyude Paul 		/* These events will generate keypresses otherwise */
460582f58deSLyude Paul 		if (event->type == ACPI_VIDEO_NOTIFY_PROBE)
461582f58deSLyude Paul 			return NOTIFY_BAD;
462582f58deSLyude Paul 		else
463d38ceaf9SAlex Deucher 			return NOTIFY_DONE;
464582f58deSLyude Paul 	}
465d38ceaf9SAlex Deucher 
4669e7204beSAlex Deucher 	if (atif->functions.sbios_requests) {
4679e7204beSAlex Deucher 		struct atif_sbios_requests req;
4689e7204beSAlex Deucher 
469d38ceaf9SAlex Deucher 		/* Check pending SBIOS requests */
470280cf1a9SLyude Paul 		count = amdgpu_atif_get_sbios_requests(atif, &req);
471d38ceaf9SAlex Deucher 
472d38ceaf9SAlex Deucher 		if (count <= 0)
473582f58deSLyude Paul 			return NOTIFY_BAD;
474d38ceaf9SAlex Deucher 
475d38ceaf9SAlex Deucher 		DRM_DEBUG_DRIVER("ATIF: %d pending SBIOS requests\n", count);
476d38ceaf9SAlex Deucher 
47797d798b2SAlex Deucher 		if (req.pending & ATIF_PANEL_BRIGHTNESS_CHANGE_REQUEST) {
47897d798b2SAlex Deucher 			if (atif->bd) {
479d38ceaf9SAlex Deucher 				DRM_DEBUG_DRIVER("Changing brightness to %d\n",
480d38ceaf9SAlex Deucher 						 req.backlight_level);
481bcb7b0efSAndriy Gapon 				/*
482bcb7b0efSAndriy Gapon 				 * XXX backlight_device_set_brightness() is
483bcb7b0efSAndriy Gapon 				 * hardwired to post BACKLIGHT_UPDATE_SYSFS.
484bcb7b0efSAndriy Gapon 				 * It probably should accept 'reason' parameter.
485bcb7b0efSAndriy Gapon 				 */
48697d798b2SAlex Deucher 				backlight_device_set_brightness(atif->bd, req.backlight_level);
487bcb7b0efSAndriy Gapon 			}
48897d798b2SAlex Deucher 		}
48997d798b2SAlex Deucher 
4901b0f568dSAlex Deucher 		if (req.pending & ATIF_DGPU_DISPLAY_EVENT) {
491bdb1ccb0SAaron Liu 			if (adev->flags & AMD_IS_PX) {
4924a580877SLuben Tuikov 				pm_runtime_get_sync(adev_to_drm(adev)->dev);
4931b0f568dSAlex Deucher 				/* Just fire off a uevent and let userspace tell us what to do */
4944a580877SLuben Tuikov 				drm_helper_hpd_irq_event(adev_to_drm(adev));
4954a580877SLuben Tuikov 				pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
4964a580877SLuben Tuikov 				pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
4971b0f568dSAlex Deucher 			}
4981b0f568dSAlex Deucher 		}
499d38ceaf9SAlex Deucher 		/* TODO: check other events */
5009e7204beSAlex Deucher 	}
501d38ceaf9SAlex Deucher 
502d38ceaf9SAlex Deucher 	/* We've handled the event, stop the notifier chain. The ACPI interface
503d38ceaf9SAlex Deucher 	 * overloads ACPI_VIDEO_NOTIFY_PROBE, we don't want to send that to
504d38ceaf9SAlex Deucher 	 * userspace if the event was generated only to signal a SBIOS
505d38ceaf9SAlex Deucher 	 * request.
506d38ceaf9SAlex Deucher 	 */
507d38ceaf9SAlex Deucher 	return NOTIFY_BAD;
508d38ceaf9SAlex Deucher }
509d38ceaf9SAlex Deucher 
510d38ceaf9SAlex Deucher /* Call the ATCS method
511d38ceaf9SAlex Deucher  */
512d38ceaf9SAlex Deucher /**
513d38ceaf9SAlex Deucher  * amdgpu_atcs_call - call an ATCS method
514d38ceaf9SAlex Deucher  *
51577bf762fSAlex Deucher  * @atcs: atcs structure
516d38ceaf9SAlex Deucher  * @function: the ATCS function to execute
517d38ceaf9SAlex Deucher  * @params: ATCS function params
518d38ceaf9SAlex Deucher  *
519d38ceaf9SAlex Deucher  * Executes the requested ATCS function (all asics).
520d38ceaf9SAlex Deucher  * Returns a pointer to the acpi output buffer.
521d38ceaf9SAlex Deucher  */
52277bf762fSAlex Deucher static union acpi_object *amdgpu_atcs_call(struct amdgpu_atcs *atcs,
52377bf762fSAlex Deucher 					   int function,
524d38ceaf9SAlex Deucher 					   struct acpi_buffer *params)
525d38ceaf9SAlex Deucher {
526d38ceaf9SAlex Deucher 	acpi_status status;
527d38ceaf9SAlex Deucher 	union acpi_object atcs_arg_elements[2];
528d38ceaf9SAlex Deucher 	struct acpi_object_list atcs_arg;
529d38ceaf9SAlex Deucher 	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
530d38ceaf9SAlex Deucher 
531d38ceaf9SAlex Deucher 	atcs_arg.count = 2;
532d38ceaf9SAlex Deucher 	atcs_arg.pointer = &atcs_arg_elements[0];
533d38ceaf9SAlex Deucher 
534d38ceaf9SAlex Deucher 	atcs_arg_elements[0].type = ACPI_TYPE_INTEGER;
535d38ceaf9SAlex Deucher 	atcs_arg_elements[0].integer.value = function;
536d38ceaf9SAlex Deucher 
537d38ceaf9SAlex Deucher 	if (params) {
538d38ceaf9SAlex Deucher 		atcs_arg_elements[1].type = ACPI_TYPE_BUFFER;
539d38ceaf9SAlex Deucher 		atcs_arg_elements[1].buffer.length = params->length;
540d38ceaf9SAlex Deucher 		atcs_arg_elements[1].buffer.pointer = params->pointer;
541d38ceaf9SAlex Deucher 	} else {
542d38ceaf9SAlex Deucher 		/* We need a second fake parameter */
543d38ceaf9SAlex Deucher 		atcs_arg_elements[1].type = ACPI_TYPE_INTEGER;
544d38ceaf9SAlex Deucher 		atcs_arg_elements[1].integer.value = 0;
545d38ceaf9SAlex Deucher 	}
546d38ceaf9SAlex Deucher 
5474965257fSAlex Deucher 	status = acpi_evaluate_object(atcs->handle, NULL, &atcs_arg, &buffer);
548d38ceaf9SAlex Deucher 
549d38ceaf9SAlex Deucher 	/* Fail only if calling the method fails and ATIF is supported */
550d38ceaf9SAlex Deucher 	if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
551d38ceaf9SAlex Deucher 		DRM_DEBUG_DRIVER("failed to evaluate ATCS got %s\n",
552d38ceaf9SAlex Deucher 				 acpi_format_exception(status));
553d38ceaf9SAlex Deucher 		kfree(buffer.pointer);
554d38ceaf9SAlex Deucher 		return NULL;
555d38ceaf9SAlex Deucher 	}
556d38ceaf9SAlex Deucher 
557d38ceaf9SAlex Deucher 	return buffer.pointer;
558d38ceaf9SAlex Deucher }
559d38ceaf9SAlex Deucher 
560d38ceaf9SAlex Deucher /**
561d38ceaf9SAlex Deucher  * amdgpu_atcs_parse_functions - parse supported functions
562d38ceaf9SAlex Deucher  *
563d38ceaf9SAlex Deucher  * @f: supported functions struct
564d38ceaf9SAlex Deucher  * @mask: supported functions mask from ATCS
565d38ceaf9SAlex Deucher  *
566d38ceaf9SAlex Deucher  * Use the supported functions mask from ATCS function
567d38ceaf9SAlex Deucher  * ATCS_FUNCTION_VERIFY_INTERFACE to determine what functions
568d38ceaf9SAlex Deucher  * are supported (all asics).
569d38ceaf9SAlex Deucher  */
570d38ceaf9SAlex Deucher static void amdgpu_atcs_parse_functions(struct amdgpu_atcs_functions *f, u32 mask)
571d38ceaf9SAlex Deucher {
572d38ceaf9SAlex Deucher 	f->get_ext_state = mask & ATCS_GET_EXTERNAL_STATE_SUPPORTED;
573d38ceaf9SAlex Deucher 	f->pcie_perf_req = mask & ATCS_PCIE_PERFORMANCE_REQUEST_SUPPORTED;
574d38ceaf9SAlex Deucher 	f->pcie_dev_rdy = mask & ATCS_PCIE_DEVICE_READY_NOTIFICATION_SUPPORTED;
575d38ceaf9SAlex Deucher 	f->pcie_bus_width = mask & ATCS_SET_PCIE_BUS_WIDTH_SUPPORTED;
57616eb48c6SSathishkumar S 	f->power_shift_control = mask & ATCS_SET_POWER_SHIFT_CONTROL_SUPPORTED;
577d38ceaf9SAlex Deucher }
578d38ceaf9SAlex Deucher 
579d38ceaf9SAlex Deucher /**
580d38ceaf9SAlex Deucher  * amdgpu_atcs_verify_interface - verify ATCS
581d38ceaf9SAlex Deucher  *
582d38ceaf9SAlex Deucher  * @atcs: amdgpu atcs struct
583d38ceaf9SAlex Deucher  *
584d38ceaf9SAlex Deucher  * Execute the ATCS_FUNCTION_VERIFY_INTERFACE ATCS function
585d38ceaf9SAlex Deucher  * to initialize ATCS and determine what features are supported
586d38ceaf9SAlex Deucher  * (all asics).
587d38ceaf9SAlex Deucher  * returns 0 on success, error on failure.
588d38ceaf9SAlex Deucher  */
58977bf762fSAlex Deucher static int amdgpu_atcs_verify_interface(struct amdgpu_atcs *atcs)
590d38ceaf9SAlex Deucher {
591d38ceaf9SAlex Deucher 	union acpi_object *info;
592d38ceaf9SAlex Deucher 	struct atcs_verify_interface output;
593d38ceaf9SAlex Deucher 	size_t size;
594d38ceaf9SAlex Deucher 	int err = 0;
595d38ceaf9SAlex Deucher 
59677bf762fSAlex Deucher 	info = amdgpu_atcs_call(atcs, ATCS_FUNCTION_VERIFY_INTERFACE, NULL);
597d38ceaf9SAlex Deucher 	if (!info)
598d38ceaf9SAlex Deucher 		return -EIO;
599d38ceaf9SAlex Deucher 
600d38ceaf9SAlex Deucher 	memset(&output, 0, sizeof(output));
601d38ceaf9SAlex Deucher 
602d38ceaf9SAlex Deucher 	size = *(u16 *) info->buffer.pointer;
603d38ceaf9SAlex Deucher 	if (size < 8) {
604d38ceaf9SAlex Deucher 		DRM_INFO("ATCS buffer is too small: %zu\n", size);
605d38ceaf9SAlex Deucher 		err = -EINVAL;
606d38ceaf9SAlex Deucher 		goto out;
607d38ceaf9SAlex Deucher 	}
608d38ceaf9SAlex Deucher 	size = min(sizeof(output), size);
609d38ceaf9SAlex Deucher 
610d38ceaf9SAlex Deucher 	memcpy(&output, info->buffer.pointer, size);
611d38ceaf9SAlex Deucher 
612d38ceaf9SAlex Deucher 	/* TODO: check version? */
613d38ceaf9SAlex Deucher 	DRM_DEBUG_DRIVER("ATCS version %u\n", output.version);
614d38ceaf9SAlex Deucher 
615d38ceaf9SAlex Deucher 	amdgpu_atcs_parse_functions(&atcs->functions, output.function_bits);
616d38ceaf9SAlex Deucher 
617d38ceaf9SAlex Deucher out:
618d38ceaf9SAlex Deucher 	kfree(info);
619d38ceaf9SAlex Deucher 	return err;
620d38ceaf9SAlex Deucher }
621d38ceaf9SAlex Deucher 
622d38ceaf9SAlex Deucher /**
623d38ceaf9SAlex Deucher  * amdgpu_acpi_is_pcie_performance_request_supported
624d38ceaf9SAlex Deucher  *
625d38ceaf9SAlex Deucher  * @adev: amdgpu_device pointer
626d38ceaf9SAlex Deucher  *
627d38ceaf9SAlex Deucher  * Check if the ATCS pcie_perf_req and pcie_dev_rdy methods
628d38ceaf9SAlex Deucher  * are supported (all asics).
629d38ceaf9SAlex Deucher  * returns true if supported, false if not.
630d38ceaf9SAlex Deucher  */
631d38ceaf9SAlex Deucher bool amdgpu_acpi_is_pcie_performance_request_supported(struct amdgpu_device *adev)
632d38ceaf9SAlex Deucher {
633f9b7f370SAlex Deucher 	struct amdgpu_atcs *atcs = &amdgpu_acpi_priv.atcs;
634d38ceaf9SAlex Deucher 
635d38ceaf9SAlex Deucher 	if (atcs->functions.pcie_perf_req && atcs->functions.pcie_dev_rdy)
636d38ceaf9SAlex Deucher 		return true;
637d38ceaf9SAlex Deucher 
638d38ceaf9SAlex Deucher 	return false;
639d38ceaf9SAlex Deucher }
640d38ceaf9SAlex Deucher 
641d38ceaf9SAlex Deucher /**
64216eb48c6SSathishkumar S  * amdgpu_acpi_is_power_shift_control_supported
64316eb48c6SSathishkumar S  *
64416eb48c6SSathishkumar S  * Check if the ATCS power shift control method
64516eb48c6SSathishkumar S  * is supported.
64616eb48c6SSathishkumar S  * returns true if supported, false if not.
64716eb48c6SSathishkumar S  */
64816eb48c6SSathishkumar S bool amdgpu_acpi_is_power_shift_control_supported(void)
64916eb48c6SSathishkumar S {
65016eb48c6SSathishkumar S 	return amdgpu_acpi_priv.atcs.functions.power_shift_control;
65116eb48c6SSathishkumar S }
65216eb48c6SSathishkumar S 
65316eb48c6SSathishkumar S /**
654d38ceaf9SAlex Deucher  * amdgpu_acpi_pcie_notify_device_ready
655d38ceaf9SAlex Deucher  *
656d38ceaf9SAlex Deucher  * @adev: amdgpu_device pointer
657d38ceaf9SAlex Deucher  *
658d38ceaf9SAlex Deucher  * Executes the PCIE_DEVICE_READY_NOTIFICATION method
659d38ceaf9SAlex Deucher  * (all asics).
660d38ceaf9SAlex Deucher  * returns 0 on success, error on failure.
661d38ceaf9SAlex Deucher  */
662d38ceaf9SAlex Deucher int amdgpu_acpi_pcie_notify_device_ready(struct amdgpu_device *adev)
663d38ceaf9SAlex Deucher {
664d38ceaf9SAlex Deucher 	union acpi_object *info;
665f9b7f370SAlex Deucher 	struct amdgpu_atcs *atcs = &amdgpu_acpi_priv.atcs;
666d38ceaf9SAlex Deucher 
667d38ceaf9SAlex Deucher 	if (!atcs->functions.pcie_dev_rdy)
668d38ceaf9SAlex Deucher 		return -EINVAL;
669d38ceaf9SAlex Deucher 
67077bf762fSAlex Deucher 	info = amdgpu_atcs_call(atcs, ATCS_FUNCTION_PCIE_DEVICE_READY_NOTIFICATION, NULL);
671d38ceaf9SAlex Deucher 	if (!info)
672d38ceaf9SAlex Deucher 		return -EIO;
673d38ceaf9SAlex Deucher 
674d38ceaf9SAlex Deucher 	kfree(info);
675d38ceaf9SAlex Deucher 
676d38ceaf9SAlex Deucher 	return 0;
677d38ceaf9SAlex Deucher }
678d38ceaf9SAlex Deucher 
679d38ceaf9SAlex Deucher /**
680d38ceaf9SAlex Deucher  * amdgpu_acpi_pcie_performance_request
681d38ceaf9SAlex Deucher  *
682d38ceaf9SAlex Deucher  * @adev: amdgpu_device pointer
683d38ceaf9SAlex Deucher  * @perf_req: requested perf level (pcie gen speed)
684d38ceaf9SAlex Deucher  * @advertise: set advertise caps flag if set
685d38ceaf9SAlex Deucher  *
686d38ceaf9SAlex Deucher  * Executes the PCIE_PERFORMANCE_REQUEST method to
687d38ceaf9SAlex Deucher  * change the pcie gen speed (all asics).
688d38ceaf9SAlex Deucher  * returns 0 on success, error on failure.
689d38ceaf9SAlex Deucher  */
690d38ceaf9SAlex Deucher int amdgpu_acpi_pcie_performance_request(struct amdgpu_device *adev,
691d38ceaf9SAlex Deucher 					 u8 perf_req, bool advertise)
692d38ceaf9SAlex Deucher {
693d38ceaf9SAlex Deucher 	union acpi_object *info;
694f9b7f370SAlex Deucher 	struct amdgpu_atcs *atcs = &amdgpu_acpi_priv.atcs;
695d38ceaf9SAlex Deucher 	struct atcs_pref_req_input atcs_input;
696d38ceaf9SAlex Deucher 	struct atcs_pref_req_output atcs_output;
697d38ceaf9SAlex Deucher 	struct acpi_buffer params;
698d38ceaf9SAlex Deucher 	size_t size;
699d38ceaf9SAlex Deucher 	u32 retry = 3;
700d38ceaf9SAlex Deucher 
70177bf762fSAlex Deucher 	if (amdgpu_acpi_pcie_notify_device_ready(adev))
702d38ceaf9SAlex Deucher 		return -EINVAL;
703d38ceaf9SAlex Deucher 
704d38ceaf9SAlex Deucher 	if (!atcs->functions.pcie_perf_req)
705d38ceaf9SAlex Deucher 		return -EINVAL;
706d38ceaf9SAlex Deucher 
707d38ceaf9SAlex Deucher 	atcs_input.size = sizeof(struct atcs_pref_req_input);
708d38ceaf9SAlex Deucher 	/* client id (bit 2-0: func num, 7-3: dev num, 15-8: bus num) */
709bdacd16aSXiongfeng Wang 	atcs_input.client_id = pci_dev_id(adev->pdev);
710d38ceaf9SAlex Deucher 	atcs_input.valid_flags_mask = ATCS_VALID_FLAGS_MASK;
711d38ceaf9SAlex Deucher 	atcs_input.flags = ATCS_WAIT_FOR_COMPLETION;
712d38ceaf9SAlex Deucher 	if (advertise)
713d38ceaf9SAlex Deucher 		atcs_input.flags |= ATCS_ADVERTISE_CAPS;
714d38ceaf9SAlex Deucher 	atcs_input.req_type = ATCS_PCIE_LINK_SPEED;
715d38ceaf9SAlex Deucher 	atcs_input.perf_req = perf_req;
716d38ceaf9SAlex Deucher 
717d38ceaf9SAlex Deucher 	params.length = sizeof(struct atcs_pref_req_input);
718d38ceaf9SAlex Deucher 	params.pointer = &atcs_input;
719d38ceaf9SAlex Deucher 
720d38ceaf9SAlex Deucher 	while (retry--) {
72177bf762fSAlex Deucher 		info = amdgpu_atcs_call(atcs, ATCS_FUNCTION_PCIE_PERFORMANCE_REQUEST, &params);
722d38ceaf9SAlex Deucher 		if (!info)
723d38ceaf9SAlex Deucher 			return -EIO;
724d38ceaf9SAlex Deucher 
725d38ceaf9SAlex Deucher 		memset(&atcs_output, 0, sizeof(atcs_output));
726d38ceaf9SAlex Deucher 
727d38ceaf9SAlex Deucher 		size = *(u16 *) info->buffer.pointer;
728d38ceaf9SAlex Deucher 		if (size < 3) {
729d38ceaf9SAlex Deucher 			DRM_INFO("ATCS buffer is too small: %zu\n", size);
730d38ceaf9SAlex Deucher 			kfree(info);
731d38ceaf9SAlex Deucher 			return -EINVAL;
732d38ceaf9SAlex Deucher 		}
733d38ceaf9SAlex Deucher 		size = min(sizeof(atcs_output), size);
734d38ceaf9SAlex Deucher 
735d38ceaf9SAlex Deucher 		memcpy(&atcs_output, info->buffer.pointer, size);
736d38ceaf9SAlex Deucher 
737d38ceaf9SAlex Deucher 		kfree(info);
738d38ceaf9SAlex Deucher 
739d38ceaf9SAlex Deucher 		switch (atcs_output.ret_val) {
740d38ceaf9SAlex Deucher 		case ATCS_REQUEST_REFUSED:
741d38ceaf9SAlex Deucher 		default:
742d38ceaf9SAlex Deucher 			return -EINVAL;
743d38ceaf9SAlex Deucher 		case ATCS_REQUEST_COMPLETE:
744d38ceaf9SAlex Deucher 			return 0;
745d38ceaf9SAlex Deucher 		case ATCS_REQUEST_IN_PROGRESS:
746d38ceaf9SAlex Deucher 			udelay(10);
747d38ceaf9SAlex Deucher 			break;
748d38ceaf9SAlex Deucher 		}
749d38ceaf9SAlex Deucher 	}
750d38ceaf9SAlex Deucher 
751d38ceaf9SAlex Deucher 	return 0;
752d38ceaf9SAlex Deucher }
753d38ceaf9SAlex Deucher 
754d38ceaf9SAlex Deucher /**
75516eb48c6SSathishkumar S  * amdgpu_acpi_power_shift_control
75616eb48c6SSathishkumar S  *
75716eb48c6SSathishkumar S  * @adev: amdgpu_device pointer
75816eb48c6SSathishkumar S  * @dev_state: device acpi state
75916eb48c6SSathishkumar S  * @drv_state: driver state
76016eb48c6SSathishkumar S  *
76116eb48c6SSathishkumar S  * Executes the POWER_SHIFT_CONTROL method to
76216eb48c6SSathishkumar S  * communicate current dGPU device state and
76316eb48c6SSathishkumar S  * driver state to APU/SBIOS.
76416eb48c6SSathishkumar S  * returns 0 on success, error on failure.
76516eb48c6SSathishkumar S  */
76616eb48c6SSathishkumar S int amdgpu_acpi_power_shift_control(struct amdgpu_device *adev,
76716eb48c6SSathishkumar S 				    u8 dev_state, bool drv_state)
76816eb48c6SSathishkumar S {
76916eb48c6SSathishkumar S 	union acpi_object *info;
77016eb48c6SSathishkumar S 	struct amdgpu_atcs *atcs = &amdgpu_acpi_priv.atcs;
77116eb48c6SSathishkumar S 	struct atcs_pwr_shift_input atcs_input;
77216eb48c6SSathishkumar S 	struct acpi_buffer params;
77316eb48c6SSathishkumar S 
77416eb48c6SSathishkumar S 	if (!amdgpu_acpi_is_power_shift_control_supported())
77516eb48c6SSathishkumar S 		return -EINVAL;
77616eb48c6SSathishkumar S 
77716eb48c6SSathishkumar S 	atcs_input.size = sizeof(struct atcs_pwr_shift_input);
77816eb48c6SSathishkumar S 	/* dGPU id (bit 2-0: func num, 7-3: dev num, 15-8: bus num) */
779bdacd16aSXiongfeng Wang 	atcs_input.dgpu_id = pci_dev_id(adev->pdev);
78016eb48c6SSathishkumar S 	atcs_input.dev_acpi_state = dev_state;
78116eb48c6SSathishkumar S 	atcs_input.drv_state = drv_state;
78216eb48c6SSathishkumar S 
78316eb48c6SSathishkumar S 	params.length = sizeof(struct atcs_pwr_shift_input);
78416eb48c6SSathishkumar S 	params.pointer = &atcs_input;
78516eb48c6SSathishkumar S 
78616eb48c6SSathishkumar S 	info = amdgpu_atcs_call(atcs, ATCS_FUNCTION_POWER_SHIFT_CONTROL, &params);
78716eb48c6SSathishkumar S 	if (!info) {
78816eb48c6SSathishkumar S 		DRM_ERROR("ATCS PSC update failed\n");
78916eb48c6SSathishkumar S 		return -EIO;
79016eb48c6SSathishkumar S 	}
79116eb48c6SSathishkumar S 
79216eb48c6SSathishkumar S 	return 0;
79316eb48c6SSathishkumar S }
79416eb48c6SSathishkumar S 
79516eb48c6SSathishkumar S /**
7963fa8f89dSSathishkumar S  * amdgpu_acpi_smart_shift_update - update dGPU device state to SBIOS
7973fa8f89dSSathishkumar S  *
7983fa8f89dSSathishkumar S  * @dev: drm_device pointer
7993fa8f89dSSathishkumar S  * @ss_state: current smart shift event
8003fa8f89dSSathishkumar S  *
8013fa8f89dSSathishkumar S  * returns 0 on success,
8023fa8f89dSSathishkumar S  * otherwise return error number.
8033fa8f89dSSathishkumar S  */
8043fa8f89dSSathishkumar S int amdgpu_acpi_smart_shift_update(struct drm_device *dev, enum amdgpu_ss ss_state)
8053fa8f89dSSathishkumar S {
8063fa8f89dSSathishkumar S 	struct amdgpu_device *adev = drm_to_adev(dev);
8073fa8f89dSSathishkumar S 	int r;
8083fa8f89dSSathishkumar S 
8093fa8f89dSSathishkumar S 	if (!amdgpu_device_supports_smart_shift(dev))
8103fa8f89dSSathishkumar S 		return 0;
8113fa8f89dSSathishkumar S 
8123fa8f89dSSathishkumar S 	switch (ss_state) {
8133fa8f89dSSathishkumar S 	/* SBIOS trigger “stop”, “enable” and “start” at D0, Driver Operational.
8143fa8f89dSSathishkumar S 	 * SBIOS trigger “stop” at D3, Driver Not Operational.
8153fa8f89dSSathishkumar S 	 * SBIOS trigger “stop” and “disable” at D0, Driver NOT operational.
8163fa8f89dSSathishkumar S 	 */
8173fa8f89dSSathishkumar S 	case AMDGPU_SS_DRV_LOAD:
8183fa8f89dSSathishkumar S 		r = amdgpu_acpi_power_shift_control(adev,
8193fa8f89dSSathishkumar S 						    AMDGPU_ATCS_PSC_DEV_STATE_D0,
8203fa8f89dSSathishkumar S 						    AMDGPU_ATCS_PSC_DRV_STATE_OPR);
8213fa8f89dSSathishkumar S 		break;
8223fa8f89dSSathishkumar S 	case AMDGPU_SS_DEV_D0:
8233fa8f89dSSathishkumar S 		r = amdgpu_acpi_power_shift_control(adev,
8243fa8f89dSSathishkumar S 						    AMDGPU_ATCS_PSC_DEV_STATE_D0,
8253fa8f89dSSathishkumar S 						    AMDGPU_ATCS_PSC_DRV_STATE_OPR);
8263fa8f89dSSathishkumar S 		break;
8273fa8f89dSSathishkumar S 	case AMDGPU_SS_DEV_D3:
8283fa8f89dSSathishkumar S 		r = amdgpu_acpi_power_shift_control(adev,
8293fa8f89dSSathishkumar S 						    AMDGPU_ATCS_PSC_DEV_STATE_D3_HOT,
8303fa8f89dSSathishkumar S 						    AMDGPU_ATCS_PSC_DRV_STATE_NOT_OPR);
8313fa8f89dSSathishkumar S 		break;
8323fa8f89dSSathishkumar S 	case AMDGPU_SS_DRV_UNLOAD:
8333fa8f89dSSathishkumar S 		r = amdgpu_acpi_power_shift_control(adev,
8343fa8f89dSSathishkumar S 						    AMDGPU_ATCS_PSC_DEV_STATE_D0,
8353fa8f89dSSathishkumar S 						    AMDGPU_ATCS_PSC_DRV_STATE_NOT_OPR);
8363fa8f89dSSathishkumar S 		break;
8373fa8f89dSSathishkumar S 	default:
8383fa8f89dSSathishkumar S 		return -EINVAL;
8393fa8f89dSSathishkumar S 	}
8403fa8f89dSSathishkumar S 
8413fa8f89dSSathishkumar S 	return r;
8423fa8f89dSSathishkumar S }
8433fa8f89dSSathishkumar S 
84440e39d72SSrinivasan Shanmugam #ifdef CONFIG_ACPI_NUMA
8451cc82301SLijo Lazar static inline uint64_t amdgpu_acpi_get_numa_size(int nid)
8461cc82301SLijo Lazar {
8471cc82301SLijo Lazar 	/* This is directly using si_meminfo_node implementation as the
8481cc82301SLijo Lazar 	 * function is not exported.
8491cc82301SLijo Lazar 	 */
8501cc82301SLijo Lazar 	int zone_type;
8511cc82301SLijo Lazar 	uint64_t managed_pages = 0;
8521cc82301SLijo Lazar 
8531cc82301SLijo Lazar 	pg_data_t *pgdat = NODE_DATA(nid);
8541cc82301SLijo Lazar 
8551cc82301SLijo Lazar 	for (zone_type = 0; zone_type < MAX_NR_ZONES; zone_type++)
8561cc82301SLijo Lazar 		managed_pages +=
8571cc82301SLijo Lazar 			zone_managed_pages(&pgdat->node_zones[zone_type]);
8581cc82301SLijo Lazar 	return managed_pages * PAGE_SIZE;
8591cc82301SLijo Lazar }
8601cc82301SLijo Lazar 
8611cc82301SLijo Lazar static struct amdgpu_numa_info *amdgpu_acpi_get_numa_info(uint32_t pxm)
8621cc82301SLijo Lazar {
8631cc82301SLijo Lazar 	struct amdgpu_numa_info *numa_info;
8641cc82301SLijo Lazar 	int nid;
8651cc82301SLijo Lazar 
8661cc82301SLijo Lazar 	numa_info = xa_load(&numa_info_xa, pxm);
8671cc82301SLijo Lazar 
8681cc82301SLijo Lazar 	if (!numa_info) {
8691cc82301SLijo Lazar 		struct sysinfo info;
8701cc82301SLijo Lazar 
8717db36fe9SSrinivasan Shanmugam 		numa_info = kzalloc(sizeof(*numa_info), GFP_KERNEL);
8721cc82301SLijo Lazar 		if (!numa_info)
8731cc82301SLijo Lazar 			return NULL;
8741cc82301SLijo Lazar 
8751cc82301SLijo Lazar 		nid = pxm_to_node(pxm);
8761cc82301SLijo Lazar 		numa_info->pxm = pxm;
8771cc82301SLijo Lazar 		numa_info->nid = nid;
8781cc82301SLijo Lazar 
8791cc82301SLijo Lazar 		if (numa_info->nid == NUMA_NO_NODE) {
8801cc82301SLijo Lazar 			si_meminfo(&info);
8811cc82301SLijo Lazar 			numa_info->size = info.totalram * info.mem_unit;
8821cc82301SLijo Lazar 		} else {
8831cc82301SLijo Lazar 			numa_info->size = amdgpu_acpi_get_numa_size(nid);
8841cc82301SLijo Lazar 		}
8851cc82301SLijo Lazar 		xa_store(&numa_info_xa, numa_info->pxm, numa_info, GFP_KERNEL);
8861cc82301SLijo Lazar 	}
8871cc82301SLijo Lazar 
8881cc82301SLijo Lazar 	return numa_info;
8891cc82301SLijo Lazar }
89040e39d72SSrinivasan Shanmugam #endif
8911cc82301SLijo Lazar 
8923fa8f89dSSathishkumar S /**
8934d5275abSLijo Lazar  * amdgpu_acpi_get_node_id - obtain the NUMA node id for corresponding amdgpu
8944d5275abSLijo Lazar  * acpi device handle
8954d5275abSLijo Lazar  *
8964d5275abSLijo Lazar  * @handle: acpi handle
89766dadf1aSSrinivasan Shanmugam  * @numa_info: amdgpu_numa_info structure holding numa information
8984d5275abSLijo Lazar  *
8994d5275abSLijo Lazar  * Queries the ACPI interface to fetch the corresponding NUMA Node ID for a
9004d5275abSLijo Lazar  * given amdgpu acpi device.
9014d5275abSLijo Lazar  *
9024d5275abSLijo Lazar  * Returns ACPI STATUS OK with Node ID on success or the corresponding failure reason
9034d5275abSLijo Lazar  */
9041501fe94SArnd Bergmann static acpi_status amdgpu_acpi_get_node_id(acpi_handle handle,
9051cc82301SLijo Lazar 				    struct amdgpu_numa_info **numa_info)
9064d5275abSLijo Lazar {
9074d5275abSLijo Lazar #ifdef CONFIG_ACPI_NUMA
9084d5275abSLijo Lazar 	u64 pxm;
9094d5275abSLijo Lazar 	acpi_status status;
9104d5275abSLijo Lazar 
9111cc82301SLijo Lazar 	if (!numa_info)
9121cc82301SLijo Lazar 		return_ACPI_STATUS(AE_ERROR);
9131cc82301SLijo Lazar 
9144d5275abSLijo Lazar 	status = acpi_evaluate_integer(handle, "_PXM", NULL, &pxm);
9154d5275abSLijo Lazar 
9164d5275abSLijo Lazar 	if (ACPI_FAILURE(status))
9174d5275abSLijo Lazar 		return status;
9184d5275abSLijo Lazar 
9191cc82301SLijo Lazar 	*numa_info = amdgpu_acpi_get_numa_info(pxm);
9201cc82301SLijo Lazar 
9211cc82301SLijo Lazar 	if (!*numa_info)
9221cc82301SLijo Lazar 		return_ACPI_STATUS(AE_ERROR);
9234d5275abSLijo Lazar 
9244d5275abSLijo Lazar 	return_ACPI_STATUS(AE_OK);
9254d5275abSLijo Lazar #else
9264d5275abSLijo Lazar 	return_ACPI_STATUS(AE_NOT_EXIST);
9274d5275abSLijo Lazar #endif
9284d5275abSLijo Lazar }
9294d5275abSLijo Lazar 
9301501fe94SArnd Bergmann static struct amdgpu_acpi_dev_info *amdgpu_acpi_get_dev(u16 bdf)
9314d5275abSLijo Lazar {
9324d5275abSLijo Lazar 	struct amdgpu_acpi_dev_info *acpi_dev;
9334d5275abSLijo Lazar 
9344d5275abSLijo Lazar 	if (list_empty(&amdgpu_acpi_dev_list))
9354d5275abSLijo Lazar 		return NULL;
9364d5275abSLijo Lazar 
9374d5275abSLijo Lazar 	list_for_each_entry(acpi_dev, &amdgpu_acpi_dev_list, list)
9384d5275abSLijo Lazar 		if (acpi_dev->bdf == bdf)
9394d5275abSLijo Lazar 			return acpi_dev;
9404d5275abSLijo Lazar 
9414d5275abSLijo Lazar 	return NULL;
9424d5275abSLijo Lazar }
9434d5275abSLijo Lazar 
9444d5275abSLijo Lazar static int amdgpu_acpi_dev_init(struct amdgpu_acpi_dev_info **dev_info,
9454d5275abSLijo Lazar 				struct amdgpu_acpi_xcc_info *xcc_info, u16 bdf)
9464d5275abSLijo Lazar {
9474d5275abSLijo Lazar 	struct amdgpu_acpi_dev_info *tmp;
9484d5275abSLijo Lazar 	union acpi_object *obj;
9494d5275abSLijo Lazar 	int ret = -ENOENT;
9504d5275abSLijo Lazar 
9514d5275abSLijo Lazar 	*dev_info = NULL;
9524d5275abSLijo Lazar 	tmp = kzalloc(sizeof(struct amdgpu_acpi_dev_info), GFP_KERNEL);
9534d5275abSLijo Lazar 	if (!tmp)
9544d5275abSLijo Lazar 		return -ENOMEM;
9554d5275abSLijo Lazar 
9564d5275abSLijo Lazar 	INIT_LIST_HEAD(&tmp->xcc_list);
9574d5275abSLijo Lazar 	INIT_LIST_HEAD(&tmp->list);
9584d5275abSLijo Lazar 	tmp->bdf = bdf;
9594d5275abSLijo Lazar 
9604d5275abSLijo Lazar 	obj = acpi_evaluate_dsm_typed(xcc_info->handle, &amd_xcc_dsm_guid, 0,
9614d5275abSLijo Lazar 				      AMD_XCC_DSM_GET_SUPP_MODE, NULL,
9624d5275abSLijo Lazar 				      ACPI_TYPE_INTEGER);
9634d5275abSLijo Lazar 
9644d5275abSLijo Lazar 	if (!obj) {
9654d5275abSLijo Lazar 		acpi_handle_debug(xcc_info->handle,
9664d5275abSLijo Lazar 				  "_DSM function %d evaluation failed",
9674d5275abSLijo Lazar 				  AMD_XCC_DSM_GET_SUPP_MODE);
9684d5275abSLijo Lazar 		ret = -ENOENT;
9694d5275abSLijo Lazar 		goto out;
9704d5275abSLijo Lazar 	}
9714d5275abSLijo Lazar 
9724d5275abSLijo Lazar 	tmp->supp_xcp_mode = obj->integer.value & 0xFFFF;
9734d5275abSLijo Lazar 	ACPI_FREE(obj);
9744d5275abSLijo Lazar 
9754d5275abSLijo Lazar 	obj = acpi_evaluate_dsm_typed(xcc_info->handle, &amd_xcc_dsm_guid, 0,
9764d5275abSLijo Lazar 				      AMD_XCC_DSM_GET_XCP_MODE, NULL,
9774d5275abSLijo Lazar 				      ACPI_TYPE_INTEGER);
9784d5275abSLijo Lazar 
9794d5275abSLijo Lazar 	if (!obj) {
9804d5275abSLijo Lazar 		acpi_handle_debug(xcc_info->handle,
9814d5275abSLijo Lazar 				  "_DSM function %d evaluation failed",
9824d5275abSLijo Lazar 				  AMD_XCC_DSM_GET_XCP_MODE);
9834d5275abSLijo Lazar 		ret = -ENOENT;
9844d5275abSLijo Lazar 		goto out;
9854d5275abSLijo Lazar 	}
9864d5275abSLijo Lazar 
9874d5275abSLijo Lazar 	tmp->xcp_mode = obj->integer.value & 0xFFFF;
9884d5275abSLijo Lazar 	tmp->mem_mode = (obj->integer.value >> 32) & 0xFFFF;
9894d5275abSLijo Lazar 	ACPI_FREE(obj);
9904d5275abSLijo Lazar 
9914d5275abSLijo Lazar 	/* Evaluate DSMs and fill XCC information */
9924d5275abSLijo Lazar 	obj = acpi_evaluate_dsm_typed(xcc_info->handle, &amd_xcc_dsm_guid, 0,
9934d5275abSLijo Lazar 				      AMD_XCC_DSM_GET_TMR_INFO, NULL,
9944d5275abSLijo Lazar 				      ACPI_TYPE_PACKAGE);
9954d5275abSLijo Lazar 
9964d5275abSLijo Lazar 	if (!obj || obj->package.count < 2) {
9974d5275abSLijo Lazar 		acpi_handle_debug(xcc_info->handle,
9984d5275abSLijo Lazar 				  "_DSM function %d evaluation failed",
9994d5275abSLijo Lazar 				  AMD_XCC_DSM_GET_TMR_INFO);
10004d5275abSLijo Lazar 		ret = -ENOENT;
10014d5275abSLijo Lazar 		goto out;
10024d5275abSLijo Lazar 	}
10034d5275abSLijo Lazar 
10044d5275abSLijo Lazar 	tmp->tmr_base = obj->package.elements[0].integer.value;
10054d5275abSLijo Lazar 	tmp->tmr_size = obj->package.elements[1].integer.value;
10064d5275abSLijo Lazar 	ACPI_FREE(obj);
10074d5275abSLijo Lazar 
10084d5275abSLijo Lazar 	DRM_DEBUG_DRIVER(
10094d5275abSLijo Lazar 		"New dev(%x): Supported xcp mode: %x curr xcp_mode : %x mem mode : %x, tmr base: %llx tmr size: %llx  ",
10104d5275abSLijo Lazar 		tmp->bdf, tmp->supp_xcp_mode, tmp->xcp_mode, tmp->mem_mode,
10114d5275abSLijo Lazar 		tmp->tmr_base, tmp->tmr_size);
10124d5275abSLijo Lazar 	list_add_tail(&tmp->list, &amdgpu_acpi_dev_list);
10134d5275abSLijo Lazar 	*dev_info = tmp;
10144d5275abSLijo Lazar 
10154d5275abSLijo Lazar 	return 0;
10164d5275abSLijo Lazar 
10174d5275abSLijo Lazar out:
10184d5275abSLijo Lazar 	if (obj)
10194d5275abSLijo Lazar 		ACPI_FREE(obj);
10204d5275abSLijo Lazar 	kfree(tmp);
10214d5275abSLijo Lazar 
10224d5275abSLijo Lazar 	return ret;
10234d5275abSLijo Lazar }
10244d5275abSLijo Lazar 
10254d5275abSLijo Lazar static int amdgpu_acpi_get_xcc_info(struct amdgpu_acpi_xcc_info *xcc_info,
10264d5275abSLijo Lazar 				    u16 *bdf)
10274d5275abSLijo Lazar {
10284d5275abSLijo Lazar 	union acpi_object *obj;
10294d5275abSLijo Lazar 	acpi_status status;
10304d5275abSLijo Lazar 	int ret = -ENOENT;
10314d5275abSLijo Lazar 
10324d5275abSLijo Lazar 	obj = acpi_evaluate_dsm_typed(xcc_info->handle, &amd_xcc_dsm_guid, 0,
10334d5275abSLijo Lazar 				      AMD_XCC_DSM_GET_NUM_FUNCS, NULL,
10344d5275abSLijo Lazar 				      ACPI_TYPE_INTEGER);
10354d5275abSLijo Lazar 
10364d5275abSLijo Lazar 	if (!obj || obj->integer.value != AMD_XCC_DSM_NUM_FUNCS)
10374d5275abSLijo Lazar 		goto out;
10384d5275abSLijo Lazar 	ACPI_FREE(obj);
10394d5275abSLijo Lazar 
10404d5275abSLijo Lazar 	/* Evaluate DSMs and fill XCC information */
10414d5275abSLijo Lazar 	obj = acpi_evaluate_dsm_typed(xcc_info->handle, &amd_xcc_dsm_guid, 0,
10424d5275abSLijo Lazar 				      AMD_XCC_DSM_GET_VF_XCC_MAPPING, NULL,
10434d5275abSLijo Lazar 				      ACPI_TYPE_INTEGER);
10444d5275abSLijo Lazar 
10454d5275abSLijo Lazar 	if (!obj) {
10464d5275abSLijo Lazar 		acpi_handle_debug(xcc_info->handle,
10474d5275abSLijo Lazar 				  "_DSM function %d evaluation failed",
10484d5275abSLijo Lazar 				  AMD_XCC_DSM_GET_VF_XCC_MAPPING);
10494d5275abSLijo Lazar 		ret = -EINVAL;
10504d5275abSLijo Lazar 		goto out;
10514d5275abSLijo Lazar 	}
10524d5275abSLijo Lazar 
10534d5275abSLijo Lazar 	/* PF xcc id [39:32] */
10544d5275abSLijo Lazar 	xcc_info->phy_id = (obj->integer.value >> 32) & 0xFF;
10554d5275abSLijo Lazar 	/* xcp node of this xcc [47:40] */
10564d5275abSLijo Lazar 	xcc_info->xcp_node = (obj->integer.value >> 40) & 0xFF;
10574d5275abSLijo Lazar 	/* PF bus/dev/fn of this xcc [63:48] */
10584d5275abSLijo Lazar 	*bdf = (obj->integer.value >> 48) & 0xFFFF;
10594d5275abSLijo Lazar 	ACPI_FREE(obj);
10604d5275abSLijo Lazar 	obj = NULL;
10614d5275abSLijo Lazar 
10621cc82301SLijo Lazar 	status =
10631cc82301SLijo Lazar 		amdgpu_acpi_get_node_id(xcc_info->handle, &xcc_info->numa_info);
10644d5275abSLijo Lazar 
10654d5275abSLijo Lazar 	/* TODO: check if this check is required */
10664d5275abSLijo Lazar 	if (ACPI_SUCCESS(status))
10674d5275abSLijo Lazar 		ret = 0;
10684d5275abSLijo Lazar out:
10694d5275abSLijo Lazar 	if (obj)
10704d5275abSLijo Lazar 		ACPI_FREE(obj);
10714d5275abSLijo Lazar 
10724d5275abSLijo Lazar 	return ret;
10734d5275abSLijo Lazar }
10744d5275abSLijo Lazar 
10754d5275abSLijo Lazar static int amdgpu_acpi_enumerate_xcc(void)
10764d5275abSLijo Lazar {
10774d5275abSLijo Lazar 	struct amdgpu_acpi_dev_info *dev_info = NULL;
10784d5275abSLijo Lazar 	struct amdgpu_acpi_xcc_info *xcc_info;
10794d5275abSLijo Lazar 	struct acpi_device *acpi_dev;
10804d5275abSLijo Lazar 	char hid[ACPI_ID_LEN];
10814d5275abSLijo Lazar 	int ret, id;
10824d5275abSLijo Lazar 	u16 bdf;
10834d5275abSLijo Lazar 
10844d5275abSLijo Lazar 	INIT_LIST_HEAD(&amdgpu_acpi_dev_list);
10851cc82301SLijo Lazar 	xa_init(&numa_info_xa);
10864d5275abSLijo Lazar 
10874d5275abSLijo Lazar 	for (id = 0; id < AMD_XCC_MAX_HID; id++) {
10884d5275abSLijo Lazar 		sprintf(hid, "%s%d", "AMD", AMD_XCC_HID_START + id);
10894d5275abSLijo Lazar 		acpi_dev = acpi_dev_get_first_match_dev(hid, NULL, -1);
10904d5275abSLijo Lazar 		/* These ACPI objects are expected to be in sequential order. If
10914d5275abSLijo Lazar 		 * one is not found, no need to check the rest.
10924d5275abSLijo Lazar 		 */
10934d5275abSLijo Lazar 		if (!acpi_dev) {
10944d5275abSLijo Lazar 			DRM_DEBUG_DRIVER("No matching acpi device found for %s",
10954d5275abSLijo Lazar 					 hid);
10964d5275abSLijo Lazar 			break;
10974d5275abSLijo Lazar 		}
10984d5275abSLijo Lazar 
10994d5275abSLijo Lazar 		xcc_info = kzalloc(sizeof(struct amdgpu_acpi_xcc_info),
11004d5275abSLijo Lazar 				   GFP_KERNEL);
11014d5275abSLijo Lazar 		if (!xcc_info) {
11024d5275abSLijo Lazar 			DRM_ERROR("Failed to allocate memory for xcc info\n");
11034d5275abSLijo Lazar 			return -ENOMEM;
11044d5275abSLijo Lazar 		}
11054d5275abSLijo Lazar 
11064d5275abSLijo Lazar 		INIT_LIST_HEAD(&xcc_info->list);
11074d5275abSLijo Lazar 		xcc_info->handle = acpi_device_handle(acpi_dev);
11084d5275abSLijo Lazar 		acpi_dev_put(acpi_dev);
11094d5275abSLijo Lazar 
11104d5275abSLijo Lazar 		ret = amdgpu_acpi_get_xcc_info(xcc_info, &bdf);
11114d5275abSLijo Lazar 		if (ret) {
11124d5275abSLijo Lazar 			kfree(xcc_info);
11134d5275abSLijo Lazar 			continue;
11144d5275abSLijo Lazar 		}
11154d5275abSLijo Lazar 
11164d5275abSLijo Lazar 		dev_info = amdgpu_acpi_get_dev(bdf);
11174d5275abSLijo Lazar 
11184d5275abSLijo Lazar 		if (!dev_info)
11194d5275abSLijo Lazar 			ret = amdgpu_acpi_dev_init(&dev_info, xcc_info, bdf);
11204d5275abSLijo Lazar 
11214d5275abSLijo Lazar 		if (ret == -ENOMEM)
11224d5275abSLijo Lazar 			return ret;
11234d5275abSLijo Lazar 
11244d5275abSLijo Lazar 		if (!dev_info) {
11254d5275abSLijo Lazar 			kfree(xcc_info);
11264d5275abSLijo Lazar 			continue;
11274d5275abSLijo Lazar 		}
11284d5275abSLijo Lazar 
11294d5275abSLijo Lazar 		list_add_tail(&xcc_info->list, &dev_info->xcc_list);
11304d5275abSLijo Lazar 	}
11314d5275abSLijo Lazar 
11324d5275abSLijo Lazar 	return 0;
11334d5275abSLijo Lazar }
11344d5275abSLijo Lazar 
11356e018822SLijo Lazar int amdgpu_acpi_get_tmr_info(struct amdgpu_device *adev, u64 *tmr_offset,
11366e018822SLijo Lazar 			     u64 *tmr_size)
11376e018822SLijo Lazar {
11386e018822SLijo Lazar 	struct amdgpu_acpi_dev_info *dev_info;
11396e018822SLijo Lazar 	u16 bdf;
11406e018822SLijo Lazar 
11416e018822SLijo Lazar 	if (!tmr_offset || !tmr_size)
11426e018822SLijo Lazar 		return -EINVAL;
11436e018822SLijo Lazar 
1144bdacd16aSXiongfeng Wang 	bdf = pci_dev_id(adev->pdev);
11456e018822SLijo Lazar 	dev_info = amdgpu_acpi_get_dev(bdf);
11466e018822SLijo Lazar 	if (!dev_info)
11476e018822SLijo Lazar 		return -ENOENT;
11486e018822SLijo Lazar 
11496e018822SLijo Lazar 	*tmr_offset = dev_info->tmr_base;
11506e018822SLijo Lazar 	*tmr_size = dev_info->tmr_size;
11516e018822SLijo Lazar 
11526e018822SLijo Lazar 	return 0;
11536e018822SLijo Lazar }
11546e018822SLijo Lazar 
1155fa0497c3SLijo Lazar int amdgpu_acpi_get_mem_info(struct amdgpu_device *adev, int xcc_id,
1156fa0497c3SLijo Lazar 			     struct amdgpu_numa_info *numa_info)
1157fa0497c3SLijo Lazar {
1158fa0497c3SLijo Lazar 	struct amdgpu_acpi_dev_info *dev_info;
1159fa0497c3SLijo Lazar 	struct amdgpu_acpi_xcc_info *xcc_info;
1160fa0497c3SLijo Lazar 	u16 bdf;
1161fa0497c3SLijo Lazar 
1162fa0497c3SLijo Lazar 	if (!numa_info)
1163fa0497c3SLijo Lazar 		return -EINVAL;
1164fa0497c3SLijo Lazar 
1165bdacd16aSXiongfeng Wang 	bdf = pci_dev_id(adev->pdev);
1166fa0497c3SLijo Lazar 	dev_info = amdgpu_acpi_get_dev(bdf);
1167fa0497c3SLijo Lazar 	if (!dev_info)
1168fa0497c3SLijo Lazar 		return -ENOENT;
1169fa0497c3SLijo Lazar 
1170fa0497c3SLijo Lazar 	list_for_each_entry(xcc_info, &dev_info->xcc_list, list) {
1171fa0497c3SLijo Lazar 		if (xcc_info->phy_id == xcc_id) {
1172fa0497c3SLijo Lazar 			memcpy(numa_info, xcc_info->numa_info,
1173fa0497c3SLijo Lazar 			       sizeof(*numa_info));
1174fa0497c3SLijo Lazar 			return 0;
1175fa0497c3SLijo Lazar 		}
1176fa0497c3SLijo Lazar 	}
1177fa0497c3SLijo Lazar 
1178fa0497c3SLijo Lazar 	return -ENOENT;
1179fa0497c3SLijo Lazar }
1180fa0497c3SLijo Lazar 
11814d5275abSLijo Lazar /**
1182d38ceaf9SAlex Deucher  * amdgpu_acpi_event - handle notify events
1183d38ceaf9SAlex Deucher  *
1184d38ceaf9SAlex Deucher  * @nb: notifier block
1185d38ceaf9SAlex Deucher  * @val: val
1186d38ceaf9SAlex Deucher  * @data: acpi event
1187d38ceaf9SAlex Deucher  *
1188d38ceaf9SAlex Deucher  * Calls relevant amdgpu functions in response to various
1189d38ceaf9SAlex Deucher  * acpi events.
1190d38ceaf9SAlex Deucher  * Returns NOTIFY code
1191d38ceaf9SAlex Deucher  */
1192d38ceaf9SAlex Deucher static int amdgpu_acpi_event(struct notifier_block *nb,
1193d38ceaf9SAlex Deucher 			     unsigned long val,
1194d38ceaf9SAlex Deucher 			     void *data)
1195d38ceaf9SAlex Deucher {
1196d38ceaf9SAlex Deucher 	struct amdgpu_device *adev = container_of(nb, struct amdgpu_device, acpi_nb);
1197d38ceaf9SAlex Deucher 	struct acpi_bus_event *entry = (struct acpi_bus_event *)data;
1198d38ceaf9SAlex Deucher 
1199d38ceaf9SAlex Deucher 	if (strcmp(entry->device_class, ACPI_AC_CLASS) == 0) {
1200d38ceaf9SAlex Deucher 		if (power_supply_is_system_supplied() > 0)
1201d38ceaf9SAlex Deucher 			DRM_DEBUG_DRIVER("pm: AC\n");
1202d38ceaf9SAlex Deucher 		else
1203d38ceaf9SAlex Deucher 			DRM_DEBUG_DRIVER("pm: DC\n");
1204d38ceaf9SAlex Deucher 
1205d38ceaf9SAlex Deucher 		amdgpu_pm_acpi_event_handler(adev);
1206d38ceaf9SAlex Deucher 	}
1207d38ceaf9SAlex Deucher 
1208d38ceaf9SAlex Deucher 	/* Check for pending SBIOS requests */
1209d38ceaf9SAlex Deucher 	return amdgpu_atif_handler(adev, entry);
1210d38ceaf9SAlex Deucher }
1211d38ceaf9SAlex Deucher 
1212d38ceaf9SAlex Deucher /* Call all ACPI methods here */
1213d38ceaf9SAlex Deucher /**
1214d38ceaf9SAlex Deucher  * amdgpu_acpi_init - init driver acpi support
1215d38ceaf9SAlex Deucher  *
1216d38ceaf9SAlex Deucher  * @adev: amdgpu_device pointer
1217d38ceaf9SAlex Deucher  *
1218d38ceaf9SAlex Deucher  * Verifies the AMD ACPI interfaces and registers with the acpi
1219d38ceaf9SAlex Deucher  * notifier chain (all asics).
1220d38ceaf9SAlex Deucher  * Returns 0 on success, error on failure.
1221d38ceaf9SAlex Deucher  */
1222d38ceaf9SAlex Deucher int amdgpu_acpi_init(struct amdgpu_device *adev)
1223d38ceaf9SAlex Deucher {
1224f9b7f370SAlex Deucher 	struct amdgpu_atif *atif = &amdgpu_acpi_priv.atif;
1225d38ceaf9SAlex Deucher 
12269c27bc97SYe Bin 	if (atif->notifications.brightness_change) {
1227d09ef243SAlex Deucher 		if (adev->dc_enabled) {
122897d798b2SAlex Deucher #if defined(CONFIG_DRM_AMD_DC)
122997d798b2SAlex Deucher 			struct amdgpu_display_manager *dm = &adev->dm;
1230556bdae3SJingyu Wang 
12317fd13baeSAlex Deucher 			if (dm->backlight_dev[0])
12327fd13baeSAlex Deucher 				atif->bd = dm->backlight_dev[0];
123397d798b2SAlex Deucher #endif
123497d798b2SAlex Deucher 		} else {
1235d38ceaf9SAlex Deucher 			struct drm_encoder *tmp;
1236d38ceaf9SAlex Deucher 
1237d38ceaf9SAlex Deucher 			/* Find the encoder controlling the brightness */
12384a580877SLuben Tuikov 			list_for_each_entry(tmp, &adev_to_drm(adev)->mode_config.encoder_list,
1239d38ceaf9SAlex Deucher 					    head) {
1240d38ceaf9SAlex Deucher 				struct amdgpu_encoder *enc = to_amdgpu_encoder(tmp);
1241d38ceaf9SAlex Deucher 
1242d38ceaf9SAlex Deucher 				if ((enc->devices & (ATOM_DEVICE_LCD_SUPPORT)) &&
1243d38ceaf9SAlex Deucher 				    enc->enc_priv) {
1244d38ceaf9SAlex Deucher 					struct amdgpu_encoder_atom_dig *dig = enc->enc_priv;
1245556bdae3SJingyu Wang 
1246d38ceaf9SAlex Deucher 					if (dig->bl_dev) {
124797d798b2SAlex Deucher 						atif->bd = dig->bl_dev;
1248d38ceaf9SAlex Deucher 						break;
1249d38ceaf9SAlex Deucher 					}
1250d38ceaf9SAlex Deucher 				}
1251d38ceaf9SAlex Deucher 			}
1252d38ceaf9SAlex Deucher 		}
125397d798b2SAlex Deucher 	}
1254f9b7f370SAlex Deucher 	adev->acpi_nb.notifier_call = amdgpu_acpi_event;
1255f9b7f370SAlex Deucher 	register_acpi_notifier(&adev->acpi_nb);
1256f9b7f370SAlex Deucher 
1257f9b7f370SAlex Deucher 	return 0;
1258f9b7f370SAlex Deucher }
1259f9b7f370SAlex Deucher 
1260f9b7f370SAlex Deucher void amdgpu_acpi_get_backlight_caps(struct amdgpu_dm_backlight_caps *caps)
1261f9b7f370SAlex Deucher {
1262f9b7f370SAlex Deucher 	struct amdgpu_atif *atif = &amdgpu_acpi_priv.atif;
1263f9b7f370SAlex Deucher 
1264f9b7f370SAlex Deucher 	caps->caps_valid = atif->backlight_caps.caps_valid;
1265f9b7f370SAlex Deucher 	caps->min_input_signal = atif->backlight_caps.min_input_signal;
1266f9b7f370SAlex Deucher 	caps->max_input_signal = atif->backlight_caps.max_input_signal;
1267f9b7f370SAlex Deucher }
1268f9b7f370SAlex Deucher 
1269f9b7f370SAlex Deucher /**
1270f9b7f370SAlex Deucher  * amdgpu_acpi_fini - tear down driver acpi support
1271f9b7f370SAlex Deucher  *
1272f9b7f370SAlex Deucher  * @adev: amdgpu_device pointer
1273f9b7f370SAlex Deucher  *
1274f9b7f370SAlex Deucher  * Unregisters with the acpi notifier chain (all asics).
1275f9b7f370SAlex Deucher  */
1276f9b7f370SAlex Deucher void amdgpu_acpi_fini(struct amdgpu_device *adev)
1277f9b7f370SAlex Deucher {
1278f9b7f370SAlex Deucher 	unregister_acpi_notifier(&adev->acpi_nb);
1279f9b7f370SAlex Deucher }
1280f9b7f370SAlex Deucher 
1281f9b7f370SAlex Deucher /**
1282f9b7f370SAlex Deucher  * amdgpu_atif_pci_probe_handle - look up the ATIF handle
1283f9b7f370SAlex Deucher  *
1284f9b7f370SAlex Deucher  * @pdev: pci device
1285f9b7f370SAlex Deucher  *
1286f9b7f370SAlex Deucher  * Look up the ATIF handles (all asics).
1287f9b7f370SAlex Deucher  * Returns true if the handle is found, false if not.
1288f9b7f370SAlex Deucher  */
1289f9b7f370SAlex Deucher static bool amdgpu_atif_pci_probe_handle(struct pci_dev *pdev)
1290f9b7f370SAlex Deucher {
1291f9b7f370SAlex Deucher 	char acpi_method_name[255] = { 0 };
1292f9b7f370SAlex Deucher 	struct acpi_buffer buffer = {sizeof(acpi_method_name), acpi_method_name};
1293f9b7f370SAlex Deucher 	acpi_handle dhandle, atif_handle;
1294f9b7f370SAlex Deucher 	acpi_status status;
1295f9b7f370SAlex Deucher 	int ret;
1296f9b7f370SAlex Deucher 
1297f9b7f370SAlex Deucher 	dhandle = ACPI_HANDLE(&pdev->dev);
1298f9b7f370SAlex Deucher 	if (!dhandle)
1299f9b7f370SAlex Deucher 		return false;
1300f9b7f370SAlex Deucher 
1301f9b7f370SAlex Deucher 	status = acpi_get_handle(dhandle, "ATIF", &atif_handle);
1302556bdae3SJingyu Wang 	if (ACPI_FAILURE(status))
1303f9b7f370SAlex Deucher 		return false;
1304556bdae3SJingyu Wang 
1305f9b7f370SAlex Deucher 	amdgpu_acpi_priv.atif.handle = atif_handle;
1306f9b7f370SAlex Deucher 	acpi_get_name(amdgpu_acpi_priv.atif.handle, ACPI_FULL_PATHNAME, &buffer);
1307f9b7f370SAlex Deucher 	DRM_DEBUG_DRIVER("Found ATIF handle %s\n", acpi_method_name);
1308f9b7f370SAlex Deucher 	ret = amdgpu_atif_verify_interface(&amdgpu_acpi_priv.atif);
1309f9b7f370SAlex Deucher 	if (ret) {
1310f9b7f370SAlex Deucher 		amdgpu_acpi_priv.atif.handle = 0;
1311f9b7f370SAlex Deucher 		return false;
1312f9b7f370SAlex Deucher 	}
1313f9b7f370SAlex Deucher 	return true;
1314f9b7f370SAlex Deucher }
1315f9b7f370SAlex Deucher 
1316f9b7f370SAlex Deucher /**
1317f9b7f370SAlex Deucher  * amdgpu_atcs_pci_probe_handle - look up the ATCS handle
1318f9b7f370SAlex Deucher  *
1319f9b7f370SAlex Deucher  * @pdev: pci device
1320f9b7f370SAlex Deucher  *
1321f9b7f370SAlex Deucher  * Look up the ATCS handles (all asics).
1322f9b7f370SAlex Deucher  * Returns true if the handle is found, false if not.
1323f9b7f370SAlex Deucher  */
1324f9b7f370SAlex Deucher static bool amdgpu_atcs_pci_probe_handle(struct pci_dev *pdev)
1325f9b7f370SAlex Deucher {
1326f9b7f370SAlex Deucher 	char acpi_method_name[255] = { 0 };
1327f9b7f370SAlex Deucher 	struct acpi_buffer buffer = { sizeof(acpi_method_name), acpi_method_name };
1328f9b7f370SAlex Deucher 	acpi_handle dhandle, atcs_handle;
1329f9b7f370SAlex Deucher 	acpi_status status;
1330f9b7f370SAlex Deucher 	int ret;
1331f9b7f370SAlex Deucher 
1332f9b7f370SAlex Deucher 	dhandle = ACPI_HANDLE(&pdev->dev);
1333f9b7f370SAlex Deucher 	if (!dhandle)
1334f9b7f370SAlex Deucher 		return false;
1335f9b7f370SAlex Deucher 
1336f9b7f370SAlex Deucher 	status = acpi_get_handle(dhandle, "ATCS", &atcs_handle);
1337556bdae3SJingyu Wang 	if (ACPI_FAILURE(status))
1338f9b7f370SAlex Deucher 		return false;
1339556bdae3SJingyu Wang 
1340f9b7f370SAlex Deucher 	amdgpu_acpi_priv.atcs.handle = atcs_handle;
1341f9b7f370SAlex Deucher 	acpi_get_name(amdgpu_acpi_priv.atcs.handle, ACPI_FULL_PATHNAME, &buffer);
1342f9b7f370SAlex Deucher 	DRM_DEBUG_DRIVER("Found ATCS handle %s\n", acpi_method_name);
1343f9b7f370SAlex Deucher 	ret = amdgpu_atcs_verify_interface(&amdgpu_acpi_priv.atcs);
1344f9b7f370SAlex Deucher 	if (ret) {
1345f9b7f370SAlex Deucher 		amdgpu_acpi_priv.atcs.handle = 0;
1346f9b7f370SAlex Deucher 		return false;
1347f9b7f370SAlex Deucher 	}
1348f9b7f370SAlex Deucher 	return true;
1349f9b7f370SAlex Deucher }
1350f9b7f370SAlex Deucher 
1351aaee0ce4STim Huang 
1352aaee0ce4STim Huang /**
1353aaee0ce4STim Huang  * amdgpu_acpi_should_gpu_reset
1354aaee0ce4STim Huang  *
1355aaee0ce4STim Huang  * @adev: amdgpu_device_pointer
1356aaee0ce4STim Huang  *
1357aaee0ce4STim Huang  * returns true if should reset GPU, false if not
1358aaee0ce4STim Huang  */
1359aaee0ce4STim Huang bool amdgpu_acpi_should_gpu_reset(struct amdgpu_device *adev)
1360aaee0ce4STim Huang {
1361980d5baeSTim Huang 	if ((adev->flags & AMD_IS_APU) &&
1362980d5baeSTim Huang 	    adev->gfx.imu.funcs) /* Not need to do mode2 reset for IMU enabled APUs */
1363980d5baeSTim Huang 		return false;
1364980d5baeSTim Huang 
1365980d5baeSTim Huang 	if ((adev->flags & AMD_IS_APU) &&
1366980d5baeSTim Huang 	    amdgpu_acpi_is_s3_active(adev))
1367aaee0ce4STim Huang 		return false;
1368aaee0ce4STim Huang 
1369aaee0ce4STim Huang 	if (amdgpu_sriov_vf(adev))
1370aaee0ce4STim Huang 		return false;
1371aaee0ce4STim Huang 
1372aaee0ce4STim Huang #if IS_ENABLED(CONFIG_SUSPEND)
1373aaee0ce4STim Huang 	return pm_suspend_target_state != PM_SUSPEND_TO_IDLE;
1374aaee0ce4STim Huang #else
1375aaee0ce4STim Huang 	return true;
1376aaee0ce4STim Huang #endif
1377aaee0ce4STim Huang }
1378aaee0ce4STim Huang 
1379f9b7f370SAlex Deucher /*
1380f9b7f370SAlex Deucher  * amdgpu_acpi_detect - detect ACPI ATIF/ATCS methods
1381f9b7f370SAlex Deucher  *
1382f9b7f370SAlex Deucher  * Check if we have the ATIF/ATCS methods and populate
1383f9b7f370SAlex Deucher  * the structures in the driver.
1384f9b7f370SAlex Deucher  */
1385f9b7f370SAlex Deucher void amdgpu_acpi_detect(void)
1386f9b7f370SAlex Deucher {
1387f9b7f370SAlex Deucher 	struct amdgpu_atif *atif = &amdgpu_acpi_priv.atif;
1388f9b7f370SAlex Deucher 	struct amdgpu_atcs *atcs = &amdgpu_acpi_priv.atcs;
1389f9b7f370SAlex Deucher 	struct pci_dev *pdev = NULL;
1390f9b7f370SAlex Deucher 	int ret;
1391f9b7f370SAlex Deucher 
1392*18bf4005SSui Jingfeng 	while ((pdev = pci_get_base_class(PCI_BASE_CLASS_DISPLAY, pdev))) {
1393*18bf4005SSui Jingfeng 		if ((pdev->class != PCI_CLASS_DISPLAY_VGA << 8) &&
1394*18bf4005SSui Jingfeng 		    (pdev->class != PCI_CLASS_DISPLAY_OTHER << 8))
1395*18bf4005SSui Jingfeng 			continue;
1396f9b7f370SAlex Deucher 
1397f9b7f370SAlex Deucher 		if (!atif->handle)
1398f9b7f370SAlex Deucher 			amdgpu_atif_pci_probe_handle(pdev);
1399f9b7f370SAlex Deucher 		if (!atcs->handle)
1400f9b7f370SAlex Deucher 			amdgpu_atcs_pci_probe_handle(pdev);
1401f9b7f370SAlex Deucher 	}
1402d38ceaf9SAlex Deucher 
1403d38ceaf9SAlex Deucher 	if (atif->functions.sbios_requests && !atif->functions.system_params) {
1404d38ceaf9SAlex Deucher 		/* XXX check this workraround, if sbios request function is
1405d38ceaf9SAlex Deucher 		 * present we have to see how it's configured in the system
1406d38ceaf9SAlex Deucher 		 * params
1407d38ceaf9SAlex Deucher 		 */
1408d38ceaf9SAlex Deucher 		atif->functions.system_params = true;
1409d38ceaf9SAlex Deucher 	}
1410d38ceaf9SAlex Deucher 
1411d38ceaf9SAlex Deucher 	if (atif->functions.system_params) {
1412280cf1a9SLyude Paul 		ret = amdgpu_atif_get_notification_params(atif);
1413d38ceaf9SAlex Deucher 		if (ret) {
1414d38ceaf9SAlex Deucher 			DRM_DEBUG_DRIVER("Call to GET_SYSTEM_PARAMS failed: %d\n",
1415d38ceaf9SAlex Deucher 					ret);
1416d38ceaf9SAlex Deucher 			/* Disable notification */
1417d38ceaf9SAlex Deucher 			atif->notification_cfg.enabled = false;
1418d38ceaf9SAlex Deucher 		}
1419d38ceaf9SAlex Deucher 	}
1420d38ceaf9SAlex Deucher 
1421206bbafeSDavid Francis 	if (atif->functions.query_backlight_transfer_characteristics) {
1422206bbafeSDavid Francis 		ret = amdgpu_atif_query_backlight_caps(atif);
1423206bbafeSDavid Francis 		if (ret) {
1424206bbafeSDavid Francis 			DRM_DEBUG_DRIVER("Call to QUERY_BACKLIGHT_TRANSFER_CHARACTERISTICS failed: %d\n",
1425206bbafeSDavid Francis 					ret);
1426206bbafeSDavid Francis 			atif->backlight_caps.caps_valid = false;
1427206bbafeSDavid Francis 		}
1428206bbafeSDavid Francis 	} else {
1429206bbafeSDavid Francis 		atif->backlight_caps.caps_valid = false;
1430206bbafeSDavid Francis 	}
14314d5275abSLijo Lazar 
14324d5275abSLijo Lazar 	amdgpu_acpi_enumerate_xcc();
14334d5275abSLijo Lazar }
14344d5275abSLijo Lazar 
14354d5275abSLijo Lazar void amdgpu_acpi_release(void)
14364d5275abSLijo Lazar {
14374d5275abSLijo Lazar 	struct amdgpu_acpi_dev_info *dev_info, *dev_tmp;
14384d5275abSLijo Lazar 	struct amdgpu_acpi_xcc_info *xcc_info, *xcc_tmp;
14391cc82301SLijo Lazar 	struct amdgpu_numa_info *numa_info;
14401cc82301SLijo Lazar 	unsigned long index;
14411cc82301SLijo Lazar 
14421cc82301SLijo Lazar 	xa_for_each(&numa_info_xa, index, numa_info) {
14431cc82301SLijo Lazar 		kfree(numa_info);
14441cc82301SLijo Lazar 		xa_erase(&numa_info_xa, index);
14451cc82301SLijo Lazar 	}
14464d5275abSLijo Lazar 
14474d5275abSLijo Lazar 	if (list_empty(&amdgpu_acpi_dev_list))
14484d5275abSLijo Lazar 		return;
14494d5275abSLijo Lazar 
14504d5275abSLijo Lazar 	list_for_each_entry_safe(dev_info, dev_tmp, &amdgpu_acpi_dev_list,
14514d5275abSLijo Lazar 				 list) {
14524d5275abSLijo Lazar 		list_for_each_entry_safe(xcc_info, xcc_tmp, &dev_info->xcc_list,
14534d5275abSLijo Lazar 					 list) {
14544d5275abSLijo Lazar 			list_del(&xcc_info->list);
14554d5275abSLijo Lazar 			kfree(xcc_info);
14564d5275abSLijo Lazar 		}
14574d5275abSLijo Lazar 
14584d5275abSLijo Lazar 		list_del(&dev_info->list);
14594d5275abSLijo Lazar 		kfree(dev_info);
14604d5275abSLijo Lazar 	}
1461d38ceaf9SAlex Deucher }
14624cd078dcSPrike Liang 
1463f588a1bbSMario Limonciello #if IS_ENABLED(CONFIG_SUSPEND)
14644cd078dcSPrike Liang /**
146518b66aceSMario Limonciello  * amdgpu_acpi_is_s3_active
146618b66aceSMario Limonciello  *
146718b66aceSMario Limonciello  * @adev: amdgpu_device_pointer
146818b66aceSMario Limonciello  *
146918b66aceSMario Limonciello  * returns true if supported, false if not.
147018b66aceSMario Limonciello  */
147118b66aceSMario Limonciello bool amdgpu_acpi_is_s3_active(struct amdgpu_device *adev)
147218b66aceSMario Limonciello {
147318b66aceSMario Limonciello 	return !(adev->flags & AMD_IS_APU) ||
147418b66aceSMario Limonciello 		(pm_suspend_target_state == PM_SUSPEND_MEM);
147518b66aceSMario Limonciello }
147618b66aceSMario Limonciello 
147718b66aceSMario Limonciello /**
1478d0260f62SPratik Vishwakarma  * amdgpu_acpi_is_s0ix_active
14794cd078dcSPrike Liang  *
14801fdbbc12SFabio M. De Francesco  * @adev: amdgpu_device_pointer
14811fdbbc12SFabio M. De Francesco  *
14824cd078dcSPrike Liang  * returns true if supported, false if not.
14834cd078dcSPrike Liang  */
1484d0260f62SPratik Vishwakarma bool amdgpu_acpi_is_s0ix_active(struct amdgpu_device *adev)
14854cd078dcSPrike Liang {
1486f588a1bbSMario Limonciello 	if (!(adev->flags & AMD_IS_APU) ||
1487f588a1bbSMario Limonciello 	    (pm_suspend_target_state != PM_SUSPEND_TO_IDLE))
1488f588a1bbSMario Limonciello 		return false;
1489f588a1bbSMario Limonciello 
1490ca475186SMario Limonciello 	if (adev->asic_type < CHIP_RAVEN)
1491ca475186SMario Limonciello 		return false;
1492ca475186SMario Limonciello 
149361ebd2feSRafael J. Wysocki 	/*
149461ebd2feSRafael J. Wysocki 	 * If ACPI_FADT_LOW_POWER_S0 is not set in the FADT, it is generally
149561ebd2feSRafael J. Wysocki 	 * risky to do any special firmware-related preparations for entering
149661ebd2feSRafael J. Wysocki 	 * S0ix even though the system is suspending to idle, so return false
149761ebd2feSRafael J. Wysocki 	 * in that case.
149861ebd2feSRafael J. Wysocki 	 */
149909521b5dSMario Limonciello 	if (!(acpi_gbl_FADT.flags & ACPI_FADT_LOW_POWER_S0)) {
1500257d7b7bSMario Limonciello 		dev_err_once(adev->dev,
1501f588a1bbSMario Limonciello 			      "Power consumption will be higher as BIOS has not been configured for suspend-to-idle.\n"
1502f588a1bbSMario Limonciello 			      "To use suspend-to-idle change the sleep mode in BIOS setup.\n");
150309521b5dSMario Limonciello 		return false;
150409521b5dSMario Limonciello 	}
1505f588a1bbSMario Limonciello 
1506f588a1bbSMario Limonciello #if !IS_ENABLED(CONFIG_AMD_PMC)
1507257d7b7bSMario Limonciello 	dev_err_once(adev->dev,
1508f588a1bbSMario Limonciello 		      "Power consumption will be higher as the kernel has not been compiled with CONFIG_AMD_PMC.\n");
150909521b5dSMario Limonciello 	return false;
151009521b5dSMario Limonciello #else
1511cf488dcdSMario Limonciello 	return true;
151209521b5dSMario Limonciello #endif /* CONFIG_AMD_PMC */
1513f588a1bbSMario Limonciello }
1514f588a1bbSMario Limonciello 
1515f588a1bbSMario Limonciello #endif /* CONFIG_SUSPEND */
1516