xref: /linux/drivers/gpu/drm/amd/amdgpu/amdgpu_acpi.c (revision a1ff5a7d78a036d6c2178ee5acd6ba4946243800)
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;
71d055714aSLijo Lazar 	uint32_t sbdf;
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  */
amdgpu_atif_call(struct amdgpu_atif * atif,int function,struct acpi_buffer * params)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  */
amdgpu_atif_parse_notification(struct amdgpu_atif_notifications * n,u32 mask)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  */
amdgpu_atif_parse_functions(struct amdgpu_atif_functions * f,u32 mask)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  */
amdgpu_atif_verify_interface(struct amdgpu_atif * atif)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  */
amdgpu_atif_get_notification_params(struct amdgpu_atif * atif)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  */
amdgpu_atif_query_backlight_caps(struct amdgpu_atif * atif)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;
386*2fe87f54SMario Limonciello 	atif->backlight_caps.ac_level = characteristics.ac_level;
387*2fe87f54SMario Limonciello 	atif->backlight_caps.dc_level = characteristics.dc_level;
388206bbafeSDavid Francis out:
389206bbafeSDavid Francis 	kfree(info);
390206bbafeSDavid Francis 	return err;
391206bbafeSDavid Francis }
392206bbafeSDavid Francis 
393206bbafeSDavid Francis /**
394d38ceaf9SAlex Deucher  * amdgpu_atif_get_sbios_requests - get requested sbios event
395d38ceaf9SAlex Deucher  *
3961fdbbc12SFabio M. De Francesco  * @atif: acpi handle
397d38ceaf9SAlex Deucher  * @req: atif sbios request struct
398d38ceaf9SAlex Deucher  *
399d38ceaf9SAlex Deucher  * Execute the ATIF_FUNCTION_GET_SYSTEM_BIOS_REQUESTS ATIF function
400d38ceaf9SAlex Deucher  * to determine what requests the sbios is making to the driver
401d38ceaf9SAlex Deucher  * (all asics).
402d38ceaf9SAlex Deucher  * Returns 0 on success, error on failure.
403d38ceaf9SAlex Deucher  */
amdgpu_atif_get_sbios_requests(struct amdgpu_atif * atif,struct atif_sbios_requests * req)404280cf1a9SLyude Paul static int amdgpu_atif_get_sbios_requests(struct amdgpu_atif *atif,
405d38ceaf9SAlex Deucher 					  struct atif_sbios_requests *req)
406d38ceaf9SAlex Deucher {
407d38ceaf9SAlex Deucher 	union acpi_object *info;
408d38ceaf9SAlex Deucher 	size_t size;
409d38ceaf9SAlex Deucher 	int count = 0;
410d38ceaf9SAlex Deucher 
411280cf1a9SLyude Paul 	info = amdgpu_atif_call(atif, ATIF_FUNCTION_GET_SYSTEM_BIOS_REQUESTS,
412280cf1a9SLyude Paul 				NULL);
413d38ceaf9SAlex Deucher 	if (!info)
414d38ceaf9SAlex Deucher 		return -EIO;
415d38ceaf9SAlex Deucher 
416d38ceaf9SAlex Deucher 	size = *(u16 *)info->buffer.pointer;
417d38ceaf9SAlex Deucher 	if (size < 0xd) {
418d38ceaf9SAlex Deucher 		count = -EINVAL;
419d38ceaf9SAlex Deucher 		goto out;
420d38ceaf9SAlex Deucher 	}
421d38ceaf9SAlex Deucher 	memset(req, 0, sizeof(*req));
422d38ceaf9SAlex Deucher 
423d38ceaf9SAlex Deucher 	size = min(sizeof(*req), size);
424d38ceaf9SAlex Deucher 	memcpy(req, info->buffer.pointer, size);
425d38ceaf9SAlex Deucher 	DRM_DEBUG_DRIVER("SBIOS pending requests: %#x\n", req->pending);
426d38ceaf9SAlex Deucher 
427d38ceaf9SAlex Deucher 	count = hweight32(req->pending);
428d38ceaf9SAlex Deucher 
429d38ceaf9SAlex Deucher out:
430d38ceaf9SAlex Deucher 	kfree(info);
431d38ceaf9SAlex Deucher 	return count;
432d38ceaf9SAlex Deucher }
433d38ceaf9SAlex Deucher 
434d38ceaf9SAlex Deucher /**
435d38ceaf9SAlex Deucher  * amdgpu_atif_handler - handle ATIF notify requests
436d38ceaf9SAlex Deucher  *
437d38ceaf9SAlex Deucher  * @adev: amdgpu_device pointer
438d38ceaf9SAlex Deucher  * @event: atif sbios request struct
439d38ceaf9SAlex Deucher  *
440d38ceaf9SAlex Deucher  * Checks the acpi event and if it matches an atif event,
441d38ceaf9SAlex Deucher  * handles it.
442582f58deSLyude Paul  *
443582f58deSLyude Paul  * Returns:
444582f58deSLyude Paul  * NOTIFY_BAD or NOTIFY_DONE, depending on the event.
445d38ceaf9SAlex Deucher  */
amdgpu_atif_handler(struct amdgpu_device * adev,struct acpi_bus_event * event)446b7cecbe8SJean Delvare static int amdgpu_atif_handler(struct amdgpu_device *adev,
447d38ceaf9SAlex Deucher 			       struct acpi_bus_event *event)
448d38ceaf9SAlex Deucher {
449f9b7f370SAlex Deucher 	struct amdgpu_atif *atif = &amdgpu_acpi_priv.atif;
450d38ceaf9SAlex Deucher 	int count;
451d38ceaf9SAlex Deucher 
452d38ceaf9SAlex Deucher 	DRM_DEBUG_DRIVER("event, device_class = %s, type = %#x\n",
453d38ceaf9SAlex Deucher 			event->device_class, event->type);
454d38ceaf9SAlex Deucher 
455d38ceaf9SAlex Deucher 	if (strcmp(event->device_class, ACPI_VIDEO_CLASS) != 0)
456d38ceaf9SAlex Deucher 		return NOTIFY_DONE;
457d38ceaf9SAlex Deucher 
458582f58deSLyude Paul 	/* Is this actually our event? */
459f9b7f370SAlex Deucher 	if (!atif->notification_cfg.enabled ||
460582f58deSLyude Paul 	    event->type != atif->notification_cfg.command_code) {
461582f58deSLyude Paul 		/* These events will generate keypresses otherwise */
462582f58deSLyude Paul 		if (event->type == ACPI_VIDEO_NOTIFY_PROBE)
463582f58deSLyude Paul 			return NOTIFY_BAD;
464582f58deSLyude Paul 		else
465d38ceaf9SAlex Deucher 			return NOTIFY_DONE;
466582f58deSLyude Paul 	}
467d38ceaf9SAlex Deucher 
4689e7204beSAlex Deucher 	if (atif->functions.sbios_requests) {
4699e7204beSAlex Deucher 		struct atif_sbios_requests req;
4709e7204beSAlex Deucher 
471d38ceaf9SAlex Deucher 		/* Check pending SBIOS requests */
472280cf1a9SLyude Paul 		count = amdgpu_atif_get_sbios_requests(atif, &req);
473d38ceaf9SAlex Deucher 
474d38ceaf9SAlex Deucher 		if (count <= 0)
475582f58deSLyude Paul 			return NOTIFY_BAD;
476d38ceaf9SAlex Deucher 
477d38ceaf9SAlex Deucher 		DRM_DEBUG_DRIVER("ATIF: %d pending SBIOS requests\n", count);
478d38ceaf9SAlex Deucher 
47997d798b2SAlex Deucher 		if (req.pending & ATIF_PANEL_BRIGHTNESS_CHANGE_REQUEST) {
48097d798b2SAlex Deucher 			if (atif->bd) {
481d38ceaf9SAlex Deucher 				DRM_DEBUG_DRIVER("Changing brightness to %d\n",
482d38ceaf9SAlex Deucher 						 req.backlight_level);
483bcb7b0efSAndriy Gapon 				/*
484bcb7b0efSAndriy Gapon 				 * XXX backlight_device_set_brightness() is
485bcb7b0efSAndriy Gapon 				 * hardwired to post BACKLIGHT_UPDATE_SYSFS.
486bcb7b0efSAndriy Gapon 				 * It probably should accept 'reason' parameter.
487bcb7b0efSAndriy Gapon 				 */
48897d798b2SAlex Deucher 				backlight_device_set_brightness(atif->bd, req.backlight_level);
489bcb7b0efSAndriy Gapon 			}
49097d798b2SAlex Deucher 		}
49197d798b2SAlex Deucher 
4921b0f568dSAlex Deucher 		if (req.pending & ATIF_DGPU_DISPLAY_EVENT) {
493bdb1ccb0SAaron Liu 			if (adev->flags & AMD_IS_PX) {
4944a580877SLuben Tuikov 				pm_runtime_get_sync(adev_to_drm(adev)->dev);
4951b0f568dSAlex Deucher 				/* Just fire off a uevent and let userspace tell us what to do */
4964a580877SLuben Tuikov 				drm_helper_hpd_irq_event(adev_to_drm(adev));
4974a580877SLuben Tuikov 				pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
4984a580877SLuben Tuikov 				pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
4991b0f568dSAlex Deucher 			}
5001b0f568dSAlex Deucher 		}
501d38ceaf9SAlex Deucher 		/* TODO: check other events */
5029e7204beSAlex Deucher 	}
503d38ceaf9SAlex Deucher 
504d38ceaf9SAlex Deucher 	/* We've handled the event, stop the notifier chain. The ACPI interface
505d38ceaf9SAlex Deucher 	 * overloads ACPI_VIDEO_NOTIFY_PROBE, we don't want to send that to
506d38ceaf9SAlex Deucher 	 * userspace if the event was generated only to signal a SBIOS
507d38ceaf9SAlex Deucher 	 * request.
508d38ceaf9SAlex Deucher 	 */
509d38ceaf9SAlex Deucher 	return NOTIFY_BAD;
510d38ceaf9SAlex Deucher }
511d38ceaf9SAlex Deucher 
512d38ceaf9SAlex Deucher /* Call the ATCS method
513d38ceaf9SAlex Deucher  */
514d38ceaf9SAlex Deucher /**
515d38ceaf9SAlex Deucher  * amdgpu_atcs_call - call an ATCS method
516d38ceaf9SAlex Deucher  *
51777bf762fSAlex Deucher  * @atcs: atcs structure
518d38ceaf9SAlex Deucher  * @function: the ATCS function to execute
519d38ceaf9SAlex Deucher  * @params: ATCS function params
520d38ceaf9SAlex Deucher  *
521d38ceaf9SAlex Deucher  * Executes the requested ATCS function (all asics).
522d38ceaf9SAlex Deucher  * Returns a pointer to the acpi output buffer.
523d38ceaf9SAlex Deucher  */
amdgpu_atcs_call(struct amdgpu_atcs * atcs,int function,struct acpi_buffer * params)52477bf762fSAlex Deucher static union acpi_object *amdgpu_atcs_call(struct amdgpu_atcs *atcs,
52577bf762fSAlex Deucher 					   int function,
526d38ceaf9SAlex Deucher 					   struct acpi_buffer *params)
527d38ceaf9SAlex Deucher {
528d38ceaf9SAlex Deucher 	acpi_status status;
529d38ceaf9SAlex Deucher 	union acpi_object atcs_arg_elements[2];
530d38ceaf9SAlex Deucher 	struct acpi_object_list atcs_arg;
531d38ceaf9SAlex Deucher 	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
532d38ceaf9SAlex Deucher 
533d38ceaf9SAlex Deucher 	atcs_arg.count = 2;
534d38ceaf9SAlex Deucher 	atcs_arg.pointer = &atcs_arg_elements[0];
535d38ceaf9SAlex Deucher 
536d38ceaf9SAlex Deucher 	atcs_arg_elements[0].type = ACPI_TYPE_INTEGER;
537d38ceaf9SAlex Deucher 	atcs_arg_elements[0].integer.value = function;
538d38ceaf9SAlex Deucher 
539d38ceaf9SAlex Deucher 	if (params) {
540d38ceaf9SAlex Deucher 		atcs_arg_elements[1].type = ACPI_TYPE_BUFFER;
541d38ceaf9SAlex Deucher 		atcs_arg_elements[1].buffer.length = params->length;
542d38ceaf9SAlex Deucher 		atcs_arg_elements[1].buffer.pointer = params->pointer;
543d38ceaf9SAlex Deucher 	} else {
544d38ceaf9SAlex Deucher 		/* We need a second fake parameter */
545d38ceaf9SAlex Deucher 		atcs_arg_elements[1].type = ACPI_TYPE_INTEGER;
546d38ceaf9SAlex Deucher 		atcs_arg_elements[1].integer.value = 0;
547d38ceaf9SAlex Deucher 	}
548d38ceaf9SAlex Deucher 
5494965257fSAlex Deucher 	status = acpi_evaluate_object(atcs->handle, NULL, &atcs_arg, &buffer);
550d38ceaf9SAlex Deucher 
551d38ceaf9SAlex Deucher 	/* Fail only if calling the method fails and ATIF is supported */
552d38ceaf9SAlex Deucher 	if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
553d38ceaf9SAlex Deucher 		DRM_DEBUG_DRIVER("failed to evaluate ATCS got %s\n",
554d38ceaf9SAlex Deucher 				 acpi_format_exception(status));
555d38ceaf9SAlex Deucher 		kfree(buffer.pointer);
556d38ceaf9SAlex Deucher 		return NULL;
557d38ceaf9SAlex Deucher 	}
558d38ceaf9SAlex Deucher 
559d38ceaf9SAlex Deucher 	return buffer.pointer;
560d38ceaf9SAlex Deucher }
561d38ceaf9SAlex Deucher 
562d38ceaf9SAlex Deucher /**
563d38ceaf9SAlex Deucher  * amdgpu_atcs_parse_functions - parse supported functions
564d38ceaf9SAlex Deucher  *
565d38ceaf9SAlex Deucher  * @f: supported functions struct
566d38ceaf9SAlex Deucher  * @mask: supported functions mask from ATCS
567d38ceaf9SAlex Deucher  *
568d38ceaf9SAlex Deucher  * Use the supported functions mask from ATCS function
569d38ceaf9SAlex Deucher  * ATCS_FUNCTION_VERIFY_INTERFACE to determine what functions
570d38ceaf9SAlex Deucher  * are supported (all asics).
571d38ceaf9SAlex Deucher  */
amdgpu_atcs_parse_functions(struct amdgpu_atcs_functions * f,u32 mask)572d38ceaf9SAlex Deucher static void amdgpu_atcs_parse_functions(struct amdgpu_atcs_functions *f, u32 mask)
573d38ceaf9SAlex Deucher {
574d38ceaf9SAlex Deucher 	f->get_ext_state = mask & ATCS_GET_EXTERNAL_STATE_SUPPORTED;
575d38ceaf9SAlex Deucher 	f->pcie_perf_req = mask & ATCS_PCIE_PERFORMANCE_REQUEST_SUPPORTED;
576d38ceaf9SAlex Deucher 	f->pcie_dev_rdy = mask & ATCS_PCIE_DEVICE_READY_NOTIFICATION_SUPPORTED;
577d38ceaf9SAlex Deucher 	f->pcie_bus_width = mask & ATCS_SET_PCIE_BUS_WIDTH_SUPPORTED;
57816eb48c6SSathishkumar S 	f->power_shift_control = mask & ATCS_SET_POWER_SHIFT_CONTROL_SUPPORTED;
579d38ceaf9SAlex Deucher }
580d38ceaf9SAlex Deucher 
581d38ceaf9SAlex Deucher /**
582d38ceaf9SAlex Deucher  * amdgpu_atcs_verify_interface - verify ATCS
583d38ceaf9SAlex Deucher  *
584d38ceaf9SAlex Deucher  * @atcs: amdgpu atcs struct
585d38ceaf9SAlex Deucher  *
586d38ceaf9SAlex Deucher  * Execute the ATCS_FUNCTION_VERIFY_INTERFACE ATCS function
587d38ceaf9SAlex Deucher  * to initialize ATCS and determine what features are supported
588d38ceaf9SAlex Deucher  * (all asics).
589d38ceaf9SAlex Deucher  * returns 0 on success, error on failure.
590d38ceaf9SAlex Deucher  */
amdgpu_atcs_verify_interface(struct amdgpu_atcs * atcs)59177bf762fSAlex Deucher static int amdgpu_atcs_verify_interface(struct amdgpu_atcs *atcs)
592d38ceaf9SAlex Deucher {
593d38ceaf9SAlex Deucher 	union acpi_object *info;
594d38ceaf9SAlex Deucher 	struct atcs_verify_interface output;
595d38ceaf9SAlex Deucher 	size_t size;
596d38ceaf9SAlex Deucher 	int err = 0;
597d38ceaf9SAlex Deucher 
59877bf762fSAlex Deucher 	info = amdgpu_atcs_call(atcs, ATCS_FUNCTION_VERIFY_INTERFACE, NULL);
599d38ceaf9SAlex Deucher 	if (!info)
600d38ceaf9SAlex Deucher 		return -EIO;
601d38ceaf9SAlex Deucher 
602d38ceaf9SAlex Deucher 	memset(&output, 0, sizeof(output));
603d38ceaf9SAlex Deucher 
604d38ceaf9SAlex Deucher 	size = *(u16 *) info->buffer.pointer;
605d38ceaf9SAlex Deucher 	if (size < 8) {
606d38ceaf9SAlex Deucher 		DRM_INFO("ATCS buffer is too small: %zu\n", size);
607d38ceaf9SAlex Deucher 		err = -EINVAL;
608d38ceaf9SAlex Deucher 		goto out;
609d38ceaf9SAlex Deucher 	}
610d38ceaf9SAlex Deucher 	size = min(sizeof(output), size);
611d38ceaf9SAlex Deucher 
612d38ceaf9SAlex Deucher 	memcpy(&output, info->buffer.pointer, size);
613d38ceaf9SAlex Deucher 
614d38ceaf9SAlex Deucher 	/* TODO: check version? */
615d38ceaf9SAlex Deucher 	DRM_DEBUG_DRIVER("ATCS version %u\n", output.version);
616d38ceaf9SAlex Deucher 
617d38ceaf9SAlex Deucher 	amdgpu_atcs_parse_functions(&atcs->functions, output.function_bits);
618d38ceaf9SAlex Deucher 
619d38ceaf9SAlex Deucher out:
620d38ceaf9SAlex Deucher 	kfree(info);
621d38ceaf9SAlex Deucher 	return err;
622d38ceaf9SAlex Deucher }
623d38ceaf9SAlex Deucher 
624d38ceaf9SAlex Deucher /**
625d38ceaf9SAlex Deucher  * amdgpu_acpi_is_pcie_performance_request_supported
626d38ceaf9SAlex Deucher  *
627d38ceaf9SAlex Deucher  * @adev: amdgpu_device pointer
628d38ceaf9SAlex Deucher  *
629d38ceaf9SAlex Deucher  * Check if the ATCS pcie_perf_req and pcie_dev_rdy methods
630d38ceaf9SAlex Deucher  * are supported (all asics).
631d38ceaf9SAlex Deucher  * returns true if supported, false if not.
632d38ceaf9SAlex Deucher  */
amdgpu_acpi_is_pcie_performance_request_supported(struct amdgpu_device * adev)633d38ceaf9SAlex Deucher bool amdgpu_acpi_is_pcie_performance_request_supported(struct amdgpu_device *adev)
634d38ceaf9SAlex Deucher {
635f9b7f370SAlex Deucher 	struct amdgpu_atcs *atcs = &amdgpu_acpi_priv.atcs;
636d38ceaf9SAlex Deucher 
637d38ceaf9SAlex Deucher 	if (atcs->functions.pcie_perf_req && atcs->functions.pcie_dev_rdy)
638d38ceaf9SAlex Deucher 		return true;
639d38ceaf9SAlex Deucher 
640d38ceaf9SAlex Deucher 	return false;
641d38ceaf9SAlex Deucher }
642d38ceaf9SAlex Deucher 
643d38ceaf9SAlex Deucher /**
64416eb48c6SSathishkumar S  * amdgpu_acpi_is_power_shift_control_supported
64516eb48c6SSathishkumar S  *
64616eb48c6SSathishkumar S  * Check if the ATCS power shift control method
64716eb48c6SSathishkumar S  * is supported.
64816eb48c6SSathishkumar S  * returns true if supported, false if not.
64916eb48c6SSathishkumar S  */
amdgpu_acpi_is_power_shift_control_supported(void)65016eb48c6SSathishkumar S bool amdgpu_acpi_is_power_shift_control_supported(void)
65116eb48c6SSathishkumar S {
65216eb48c6SSathishkumar S 	return amdgpu_acpi_priv.atcs.functions.power_shift_control;
65316eb48c6SSathishkumar S }
65416eb48c6SSathishkumar S 
65516eb48c6SSathishkumar S /**
656d38ceaf9SAlex Deucher  * amdgpu_acpi_pcie_notify_device_ready
657d38ceaf9SAlex Deucher  *
658d38ceaf9SAlex Deucher  * @adev: amdgpu_device pointer
659d38ceaf9SAlex Deucher  *
660d38ceaf9SAlex Deucher  * Executes the PCIE_DEVICE_READY_NOTIFICATION method
661d38ceaf9SAlex Deucher  * (all asics).
662d38ceaf9SAlex Deucher  * returns 0 on success, error on failure.
663d38ceaf9SAlex Deucher  */
amdgpu_acpi_pcie_notify_device_ready(struct amdgpu_device * adev)664d38ceaf9SAlex Deucher int amdgpu_acpi_pcie_notify_device_ready(struct amdgpu_device *adev)
665d38ceaf9SAlex Deucher {
666d38ceaf9SAlex Deucher 	union acpi_object *info;
667f9b7f370SAlex Deucher 	struct amdgpu_atcs *atcs = &amdgpu_acpi_priv.atcs;
668d38ceaf9SAlex Deucher 
669d38ceaf9SAlex Deucher 	if (!atcs->functions.pcie_dev_rdy)
670d38ceaf9SAlex Deucher 		return -EINVAL;
671d38ceaf9SAlex Deucher 
67277bf762fSAlex Deucher 	info = amdgpu_atcs_call(atcs, ATCS_FUNCTION_PCIE_DEVICE_READY_NOTIFICATION, NULL);
673d38ceaf9SAlex Deucher 	if (!info)
674d38ceaf9SAlex Deucher 		return -EIO;
675d38ceaf9SAlex Deucher 
676d38ceaf9SAlex Deucher 	kfree(info);
677d38ceaf9SAlex Deucher 
678d38ceaf9SAlex Deucher 	return 0;
679d38ceaf9SAlex Deucher }
680d38ceaf9SAlex Deucher 
681d38ceaf9SAlex Deucher /**
682d38ceaf9SAlex Deucher  * amdgpu_acpi_pcie_performance_request
683d38ceaf9SAlex Deucher  *
684d38ceaf9SAlex Deucher  * @adev: amdgpu_device pointer
685d38ceaf9SAlex Deucher  * @perf_req: requested perf level (pcie gen speed)
686d38ceaf9SAlex Deucher  * @advertise: set advertise caps flag if set
687d38ceaf9SAlex Deucher  *
688d38ceaf9SAlex Deucher  * Executes the PCIE_PERFORMANCE_REQUEST method to
689d38ceaf9SAlex Deucher  * change the pcie gen speed (all asics).
690d38ceaf9SAlex Deucher  * returns 0 on success, error on failure.
691d38ceaf9SAlex Deucher  */
amdgpu_acpi_pcie_performance_request(struct amdgpu_device * adev,u8 perf_req,bool advertise)692d38ceaf9SAlex Deucher int amdgpu_acpi_pcie_performance_request(struct amdgpu_device *adev,
693d38ceaf9SAlex Deucher 					 u8 perf_req, bool advertise)
694d38ceaf9SAlex Deucher {
695d38ceaf9SAlex Deucher 	union acpi_object *info;
696f9b7f370SAlex Deucher 	struct amdgpu_atcs *atcs = &amdgpu_acpi_priv.atcs;
697d38ceaf9SAlex Deucher 	struct atcs_pref_req_input atcs_input;
698d38ceaf9SAlex Deucher 	struct atcs_pref_req_output atcs_output;
699d38ceaf9SAlex Deucher 	struct acpi_buffer params;
700d38ceaf9SAlex Deucher 	size_t size;
701d38ceaf9SAlex Deucher 	u32 retry = 3;
702d38ceaf9SAlex Deucher 
70377bf762fSAlex Deucher 	if (amdgpu_acpi_pcie_notify_device_ready(adev))
704d38ceaf9SAlex Deucher 		return -EINVAL;
705d38ceaf9SAlex Deucher 
706d38ceaf9SAlex Deucher 	if (!atcs->functions.pcie_perf_req)
707d38ceaf9SAlex Deucher 		return -EINVAL;
708d38ceaf9SAlex Deucher 
709d38ceaf9SAlex Deucher 	atcs_input.size = sizeof(struct atcs_pref_req_input);
710d38ceaf9SAlex Deucher 	/* client id (bit 2-0: func num, 7-3: dev num, 15-8: bus num) */
711bdacd16aSXiongfeng Wang 	atcs_input.client_id = pci_dev_id(adev->pdev);
712d38ceaf9SAlex Deucher 	atcs_input.valid_flags_mask = ATCS_VALID_FLAGS_MASK;
713d38ceaf9SAlex Deucher 	atcs_input.flags = ATCS_WAIT_FOR_COMPLETION;
714d38ceaf9SAlex Deucher 	if (advertise)
715d38ceaf9SAlex Deucher 		atcs_input.flags |= ATCS_ADVERTISE_CAPS;
716d38ceaf9SAlex Deucher 	atcs_input.req_type = ATCS_PCIE_LINK_SPEED;
717d38ceaf9SAlex Deucher 	atcs_input.perf_req = perf_req;
718d38ceaf9SAlex Deucher 
719d38ceaf9SAlex Deucher 	params.length = sizeof(struct atcs_pref_req_input);
720d38ceaf9SAlex Deucher 	params.pointer = &atcs_input;
721d38ceaf9SAlex Deucher 
722d38ceaf9SAlex Deucher 	while (retry--) {
72377bf762fSAlex Deucher 		info = amdgpu_atcs_call(atcs, ATCS_FUNCTION_PCIE_PERFORMANCE_REQUEST, &params);
724d38ceaf9SAlex Deucher 		if (!info)
725d38ceaf9SAlex Deucher 			return -EIO;
726d38ceaf9SAlex Deucher 
727d38ceaf9SAlex Deucher 		memset(&atcs_output, 0, sizeof(atcs_output));
728d38ceaf9SAlex Deucher 
729d38ceaf9SAlex Deucher 		size = *(u16 *) info->buffer.pointer;
730d38ceaf9SAlex Deucher 		if (size < 3) {
731d38ceaf9SAlex Deucher 			DRM_INFO("ATCS buffer is too small: %zu\n", size);
732d38ceaf9SAlex Deucher 			kfree(info);
733d38ceaf9SAlex Deucher 			return -EINVAL;
734d38ceaf9SAlex Deucher 		}
735d38ceaf9SAlex Deucher 		size = min(sizeof(atcs_output), size);
736d38ceaf9SAlex Deucher 
737d38ceaf9SAlex Deucher 		memcpy(&atcs_output, info->buffer.pointer, size);
738d38ceaf9SAlex Deucher 
739d38ceaf9SAlex Deucher 		kfree(info);
740d38ceaf9SAlex Deucher 
741d38ceaf9SAlex Deucher 		switch (atcs_output.ret_val) {
742d38ceaf9SAlex Deucher 		case ATCS_REQUEST_REFUSED:
743d38ceaf9SAlex Deucher 		default:
744d38ceaf9SAlex Deucher 			return -EINVAL;
745d38ceaf9SAlex Deucher 		case ATCS_REQUEST_COMPLETE:
746d38ceaf9SAlex Deucher 			return 0;
747d38ceaf9SAlex Deucher 		case ATCS_REQUEST_IN_PROGRESS:
748d38ceaf9SAlex Deucher 			udelay(10);
749d38ceaf9SAlex Deucher 			break;
750d38ceaf9SAlex Deucher 		}
751d38ceaf9SAlex Deucher 	}
752d38ceaf9SAlex Deucher 
753d38ceaf9SAlex Deucher 	return 0;
754d38ceaf9SAlex Deucher }
755d38ceaf9SAlex Deucher 
756d38ceaf9SAlex Deucher /**
75716eb48c6SSathishkumar S  * amdgpu_acpi_power_shift_control
75816eb48c6SSathishkumar S  *
75916eb48c6SSathishkumar S  * @adev: amdgpu_device pointer
76016eb48c6SSathishkumar S  * @dev_state: device acpi state
76116eb48c6SSathishkumar S  * @drv_state: driver state
76216eb48c6SSathishkumar S  *
76316eb48c6SSathishkumar S  * Executes the POWER_SHIFT_CONTROL method to
76416eb48c6SSathishkumar S  * communicate current dGPU device state and
76516eb48c6SSathishkumar S  * driver state to APU/SBIOS.
76616eb48c6SSathishkumar S  * returns 0 on success, error on failure.
76716eb48c6SSathishkumar S  */
amdgpu_acpi_power_shift_control(struct amdgpu_device * adev,u8 dev_state,bool drv_state)76816eb48c6SSathishkumar S int amdgpu_acpi_power_shift_control(struct amdgpu_device *adev,
76916eb48c6SSathishkumar S 				    u8 dev_state, bool drv_state)
77016eb48c6SSathishkumar S {
77116eb48c6SSathishkumar S 	union acpi_object *info;
77216eb48c6SSathishkumar S 	struct amdgpu_atcs *atcs = &amdgpu_acpi_priv.atcs;
77316eb48c6SSathishkumar S 	struct atcs_pwr_shift_input atcs_input;
77416eb48c6SSathishkumar S 	struct acpi_buffer params;
77516eb48c6SSathishkumar S 
77616eb48c6SSathishkumar S 	if (!amdgpu_acpi_is_power_shift_control_supported())
77716eb48c6SSathishkumar S 		return -EINVAL;
77816eb48c6SSathishkumar S 
77916eb48c6SSathishkumar S 	atcs_input.size = sizeof(struct atcs_pwr_shift_input);
78016eb48c6SSathishkumar S 	/* dGPU id (bit 2-0: func num, 7-3: dev num, 15-8: bus num) */
781bdacd16aSXiongfeng Wang 	atcs_input.dgpu_id = pci_dev_id(adev->pdev);
78216eb48c6SSathishkumar S 	atcs_input.dev_acpi_state = dev_state;
78316eb48c6SSathishkumar S 	atcs_input.drv_state = drv_state;
78416eb48c6SSathishkumar S 
78516eb48c6SSathishkumar S 	params.length = sizeof(struct atcs_pwr_shift_input);
78616eb48c6SSathishkumar S 	params.pointer = &atcs_input;
78716eb48c6SSathishkumar S 
78816eb48c6SSathishkumar S 	info = amdgpu_atcs_call(atcs, ATCS_FUNCTION_POWER_SHIFT_CONTROL, &params);
78916eb48c6SSathishkumar S 	if (!info) {
79016eb48c6SSathishkumar S 		DRM_ERROR("ATCS PSC update failed\n");
79116eb48c6SSathishkumar S 		return -EIO;
79216eb48c6SSathishkumar S 	}
79316eb48c6SSathishkumar S 
79416eb48c6SSathishkumar S 	return 0;
79516eb48c6SSathishkumar S }
79616eb48c6SSathishkumar S 
79716eb48c6SSathishkumar S /**
7983fa8f89dSSathishkumar S  * amdgpu_acpi_smart_shift_update - update dGPU device state to SBIOS
7993fa8f89dSSathishkumar S  *
8003fa8f89dSSathishkumar S  * @dev: drm_device pointer
8013fa8f89dSSathishkumar S  * @ss_state: current smart shift event
8023fa8f89dSSathishkumar S  *
8033fa8f89dSSathishkumar S  * returns 0 on success,
8043fa8f89dSSathishkumar S  * otherwise return error number.
8053fa8f89dSSathishkumar S  */
amdgpu_acpi_smart_shift_update(struct drm_device * dev,enum amdgpu_ss ss_state)8063fa8f89dSSathishkumar S int amdgpu_acpi_smart_shift_update(struct drm_device *dev, enum amdgpu_ss ss_state)
8073fa8f89dSSathishkumar S {
8083fa8f89dSSathishkumar S 	struct amdgpu_device *adev = drm_to_adev(dev);
8093fa8f89dSSathishkumar S 	int r;
8103fa8f89dSSathishkumar S 
8113fa8f89dSSathishkumar S 	if (!amdgpu_device_supports_smart_shift(dev))
8123fa8f89dSSathishkumar S 		return 0;
8133fa8f89dSSathishkumar S 
8143fa8f89dSSathishkumar S 	switch (ss_state) {
8153fa8f89dSSathishkumar S 	/* SBIOS trigger “stop”, “enable” and “start” at D0, Driver Operational.
8163fa8f89dSSathishkumar S 	 * SBIOS trigger “stop” at D3, Driver Not Operational.
8173fa8f89dSSathishkumar S 	 * SBIOS trigger “stop” and “disable” at D0, Driver NOT operational.
8183fa8f89dSSathishkumar S 	 */
8193fa8f89dSSathishkumar S 	case AMDGPU_SS_DRV_LOAD:
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_D0:
8253fa8f89dSSathishkumar S 		r = amdgpu_acpi_power_shift_control(adev,
8263fa8f89dSSathishkumar S 						    AMDGPU_ATCS_PSC_DEV_STATE_D0,
8273fa8f89dSSathishkumar S 						    AMDGPU_ATCS_PSC_DRV_STATE_OPR);
8283fa8f89dSSathishkumar S 		break;
8293fa8f89dSSathishkumar S 	case AMDGPU_SS_DEV_D3:
8303fa8f89dSSathishkumar S 		r = amdgpu_acpi_power_shift_control(adev,
8313fa8f89dSSathishkumar S 						    AMDGPU_ATCS_PSC_DEV_STATE_D3_HOT,
8323fa8f89dSSathishkumar S 						    AMDGPU_ATCS_PSC_DRV_STATE_NOT_OPR);
8333fa8f89dSSathishkumar S 		break;
8343fa8f89dSSathishkumar S 	case AMDGPU_SS_DRV_UNLOAD:
8353fa8f89dSSathishkumar S 		r = amdgpu_acpi_power_shift_control(adev,
8363fa8f89dSSathishkumar S 						    AMDGPU_ATCS_PSC_DEV_STATE_D0,
8373fa8f89dSSathishkumar S 						    AMDGPU_ATCS_PSC_DRV_STATE_NOT_OPR);
8383fa8f89dSSathishkumar S 		break;
8393fa8f89dSSathishkumar S 	default:
8403fa8f89dSSathishkumar S 		return -EINVAL;
8413fa8f89dSSathishkumar S 	}
8423fa8f89dSSathishkumar S 
8433fa8f89dSSathishkumar S 	return r;
8443fa8f89dSSathishkumar S }
8453fa8f89dSSathishkumar S 
84640e39d72SSrinivasan Shanmugam #ifdef CONFIG_ACPI_NUMA
amdgpu_acpi_get_numa_size(int nid)8471cc82301SLijo Lazar static inline uint64_t amdgpu_acpi_get_numa_size(int nid)
8481cc82301SLijo Lazar {
8491cc82301SLijo Lazar 	/* This is directly using si_meminfo_node implementation as the
8501cc82301SLijo Lazar 	 * function is not exported.
8511cc82301SLijo Lazar 	 */
8521cc82301SLijo Lazar 	int zone_type;
8531cc82301SLijo Lazar 	uint64_t managed_pages = 0;
8541cc82301SLijo Lazar 
8551cc82301SLijo Lazar 	pg_data_t *pgdat = NODE_DATA(nid);
8561cc82301SLijo Lazar 
8571cc82301SLijo Lazar 	for (zone_type = 0; zone_type < MAX_NR_ZONES; zone_type++)
8581cc82301SLijo Lazar 		managed_pages +=
8591cc82301SLijo Lazar 			zone_managed_pages(&pgdat->node_zones[zone_type]);
8601cc82301SLijo Lazar 	return managed_pages * PAGE_SIZE;
8611cc82301SLijo Lazar }
8621cc82301SLijo Lazar 
amdgpu_acpi_get_numa_info(uint32_t pxm)8631cc82301SLijo Lazar static struct amdgpu_numa_info *amdgpu_acpi_get_numa_info(uint32_t pxm)
8641cc82301SLijo Lazar {
8651cc82301SLijo Lazar 	struct amdgpu_numa_info *numa_info;
8661cc82301SLijo Lazar 	int nid;
8671cc82301SLijo Lazar 
8681cc82301SLijo Lazar 	numa_info = xa_load(&numa_info_xa, pxm);
8691cc82301SLijo Lazar 
8701cc82301SLijo Lazar 	if (!numa_info) {
8711cc82301SLijo Lazar 		struct sysinfo info;
8721cc82301SLijo Lazar 
8737db36fe9SSrinivasan Shanmugam 		numa_info = kzalloc(sizeof(*numa_info), GFP_KERNEL);
8741cc82301SLijo Lazar 		if (!numa_info)
8751cc82301SLijo Lazar 			return NULL;
8761cc82301SLijo Lazar 
8771cc82301SLijo Lazar 		nid = pxm_to_node(pxm);
8781cc82301SLijo Lazar 		numa_info->pxm = pxm;
8791cc82301SLijo Lazar 		numa_info->nid = nid;
8801cc82301SLijo Lazar 
8811cc82301SLijo Lazar 		if (numa_info->nid == NUMA_NO_NODE) {
8821cc82301SLijo Lazar 			si_meminfo(&info);
8831cc82301SLijo Lazar 			numa_info->size = info.totalram * info.mem_unit;
8841cc82301SLijo Lazar 		} else {
8851cc82301SLijo Lazar 			numa_info->size = amdgpu_acpi_get_numa_size(nid);
8861cc82301SLijo Lazar 		}
8871cc82301SLijo Lazar 		xa_store(&numa_info_xa, numa_info->pxm, numa_info, GFP_KERNEL);
8881cc82301SLijo Lazar 	}
8891cc82301SLijo Lazar 
8901cc82301SLijo Lazar 	return numa_info;
8911cc82301SLijo Lazar }
89240e39d72SSrinivasan Shanmugam #endif
8931cc82301SLijo Lazar 
8943fa8f89dSSathishkumar S /**
8954d5275abSLijo Lazar  * amdgpu_acpi_get_node_id - obtain the NUMA node id for corresponding amdgpu
8964d5275abSLijo Lazar  * acpi device handle
8974d5275abSLijo Lazar  *
8984d5275abSLijo Lazar  * @handle: acpi handle
89966dadf1aSSrinivasan Shanmugam  * @numa_info: amdgpu_numa_info structure holding numa information
9004d5275abSLijo Lazar  *
9014d5275abSLijo Lazar  * Queries the ACPI interface to fetch the corresponding NUMA Node ID for a
9024d5275abSLijo Lazar  * given amdgpu acpi device.
9034d5275abSLijo Lazar  *
9044d5275abSLijo Lazar  * Returns ACPI STATUS OK with Node ID on success or the corresponding failure reason
9054d5275abSLijo Lazar  */
amdgpu_acpi_get_node_id(acpi_handle handle,struct amdgpu_numa_info ** numa_info)9061501fe94SArnd Bergmann static acpi_status amdgpu_acpi_get_node_id(acpi_handle handle,
9071cc82301SLijo Lazar 				    struct amdgpu_numa_info **numa_info)
9084d5275abSLijo Lazar {
9094d5275abSLijo Lazar #ifdef CONFIG_ACPI_NUMA
9104d5275abSLijo Lazar 	u64 pxm;
9114d5275abSLijo Lazar 	acpi_status status;
9124d5275abSLijo Lazar 
9131cc82301SLijo Lazar 	if (!numa_info)
9141cc82301SLijo Lazar 		return_ACPI_STATUS(AE_ERROR);
9151cc82301SLijo Lazar 
9164d5275abSLijo Lazar 	status = acpi_evaluate_integer(handle, "_PXM", NULL, &pxm);
9174d5275abSLijo Lazar 
9184d5275abSLijo Lazar 	if (ACPI_FAILURE(status))
9194d5275abSLijo Lazar 		return status;
9204d5275abSLijo Lazar 
9211cc82301SLijo Lazar 	*numa_info = amdgpu_acpi_get_numa_info(pxm);
9221cc82301SLijo Lazar 
9231cc82301SLijo Lazar 	if (!*numa_info)
9241cc82301SLijo Lazar 		return_ACPI_STATUS(AE_ERROR);
9254d5275abSLijo Lazar 
9264d5275abSLijo Lazar 	return_ACPI_STATUS(AE_OK);
9274d5275abSLijo Lazar #else
9284d5275abSLijo Lazar 	return_ACPI_STATUS(AE_NOT_EXIST);
9294d5275abSLijo Lazar #endif
9304d5275abSLijo Lazar }
9314d5275abSLijo Lazar 
amdgpu_acpi_get_dev(u32 sbdf)932d055714aSLijo Lazar static struct amdgpu_acpi_dev_info *amdgpu_acpi_get_dev(u32 sbdf)
9334d5275abSLijo Lazar {
9344d5275abSLijo Lazar 	struct amdgpu_acpi_dev_info *acpi_dev;
9354d5275abSLijo Lazar 
9364d5275abSLijo Lazar 	if (list_empty(&amdgpu_acpi_dev_list))
9374d5275abSLijo Lazar 		return NULL;
9384d5275abSLijo Lazar 
9394d5275abSLijo Lazar 	list_for_each_entry(acpi_dev, &amdgpu_acpi_dev_list, list)
940d055714aSLijo Lazar 		if (acpi_dev->sbdf == sbdf)
9414d5275abSLijo Lazar 			return acpi_dev;
9424d5275abSLijo Lazar 
9434d5275abSLijo Lazar 	return NULL;
9444d5275abSLijo Lazar }
9454d5275abSLijo Lazar 
amdgpu_acpi_dev_init(struct amdgpu_acpi_dev_info ** dev_info,struct amdgpu_acpi_xcc_info * xcc_info,u32 sbdf)9464d5275abSLijo Lazar static int amdgpu_acpi_dev_init(struct amdgpu_acpi_dev_info **dev_info,
947d055714aSLijo Lazar 				struct amdgpu_acpi_xcc_info *xcc_info, u32 sbdf)
9484d5275abSLijo Lazar {
9494d5275abSLijo Lazar 	struct amdgpu_acpi_dev_info *tmp;
9504d5275abSLijo Lazar 	union acpi_object *obj;
9514d5275abSLijo Lazar 	int ret = -ENOENT;
9524d5275abSLijo Lazar 
9534d5275abSLijo Lazar 	*dev_info = NULL;
9544d5275abSLijo Lazar 	tmp = kzalloc(sizeof(struct amdgpu_acpi_dev_info), GFP_KERNEL);
9554d5275abSLijo Lazar 	if (!tmp)
9564d5275abSLijo Lazar 		return -ENOMEM;
9574d5275abSLijo Lazar 
9584d5275abSLijo Lazar 	INIT_LIST_HEAD(&tmp->xcc_list);
9594d5275abSLijo Lazar 	INIT_LIST_HEAD(&tmp->list);
960d055714aSLijo Lazar 	tmp->sbdf = sbdf;
9614d5275abSLijo Lazar 
9624d5275abSLijo Lazar 	obj = acpi_evaluate_dsm_typed(xcc_info->handle, &amd_xcc_dsm_guid, 0,
9634d5275abSLijo Lazar 				      AMD_XCC_DSM_GET_SUPP_MODE, NULL,
9644d5275abSLijo Lazar 				      ACPI_TYPE_INTEGER);
9654d5275abSLijo Lazar 
9664d5275abSLijo Lazar 	if (!obj) {
9674d5275abSLijo Lazar 		acpi_handle_debug(xcc_info->handle,
9684d5275abSLijo Lazar 				  "_DSM function %d evaluation failed",
9694d5275abSLijo Lazar 				  AMD_XCC_DSM_GET_SUPP_MODE);
9704d5275abSLijo Lazar 		ret = -ENOENT;
9714d5275abSLijo Lazar 		goto out;
9724d5275abSLijo Lazar 	}
9734d5275abSLijo Lazar 
9744d5275abSLijo Lazar 	tmp->supp_xcp_mode = obj->integer.value & 0xFFFF;
9754d5275abSLijo Lazar 	ACPI_FREE(obj);
9764d5275abSLijo Lazar 
9774d5275abSLijo Lazar 	obj = acpi_evaluate_dsm_typed(xcc_info->handle, &amd_xcc_dsm_guid, 0,
9784d5275abSLijo Lazar 				      AMD_XCC_DSM_GET_XCP_MODE, NULL,
9794d5275abSLijo Lazar 				      ACPI_TYPE_INTEGER);
9804d5275abSLijo Lazar 
9814d5275abSLijo Lazar 	if (!obj) {
9824d5275abSLijo Lazar 		acpi_handle_debug(xcc_info->handle,
9834d5275abSLijo Lazar 				  "_DSM function %d evaluation failed",
9844d5275abSLijo Lazar 				  AMD_XCC_DSM_GET_XCP_MODE);
9854d5275abSLijo Lazar 		ret = -ENOENT;
9864d5275abSLijo Lazar 		goto out;
9874d5275abSLijo Lazar 	}
9884d5275abSLijo Lazar 
9894d5275abSLijo Lazar 	tmp->xcp_mode = obj->integer.value & 0xFFFF;
9904d5275abSLijo Lazar 	tmp->mem_mode = (obj->integer.value >> 32) & 0xFFFF;
9914d5275abSLijo Lazar 	ACPI_FREE(obj);
9924d5275abSLijo Lazar 
9934d5275abSLijo Lazar 	/* Evaluate DSMs and fill XCC information */
9944d5275abSLijo Lazar 	obj = acpi_evaluate_dsm_typed(xcc_info->handle, &amd_xcc_dsm_guid, 0,
9954d5275abSLijo Lazar 				      AMD_XCC_DSM_GET_TMR_INFO, NULL,
9964d5275abSLijo Lazar 				      ACPI_TYPE_PACKAGE);
9974d5275abSLijo Lazar 
9984d5275abSLijo Lazar 	if (!obj || obj->package.count < 2) {
9994d5275abSLijo Lazar 		acpi_handle_debug(xcc_info->handle,
10004d5275abSLijo Lazar 				  "_DSM function %d evaluation failed",
10014d5275abSLijo Lazar 				  AMD_XCC_DSM_GET_TMR_INFO);
10024d5275abSLijo Lazar 		ret = -ENOENT;
10034d5275abSLijo Lazar 		goto out;
10044d5275abSLijo Lazar 	}
10054d5275abSLijo Lazar 
10064d5275abSLijo Lazar 	tmp->tmr_base = obj->package.elements[0].integer.value;
10074d5275abSLijo Lazar 	tmp->tmr_size = obj->package.elements[1].integer.value;
10084d5275abSLijo Lazar 	ACPI_FREE(obj);
10094d5275abSLijo Lazar 
10104d5275abSLijo Lazar 	DRM_DEBUG_DRIVER(
10114d5275abSLijo Lazar 		"New dev(%x): Supported xcp mode: %x curr xcp_mode : %x mem mode : %x, tmr base: %llx tmr size: %llx  ",
1012d055714aSLijo Lazar 		tmp->sbdf, tmp->supp_xcp_mode, tmp->xcp_mode, tmp->mem_mode,
10134d5275abSLijo Lazar 		tmp->tmr_base, tmp->tmr_size);
10144d5275abSLijo Lazar 	list_add_tail(&tmp->list, &amdgpu_acpi_dev_list);
10154d5275abSLijo Lazar 	*dev_info = tmp;
10164d5275abSLijo Lazar 
10174d5275abSLijo Lazar 	return 0;
10184d5275abSLijo Lazar 
10194d5275abSLijo Lazar out:
10204d5275abSLijo Lazar 	if (obj)
10214d5275abSLijo Lazar 		ACPI_FREE(obj);
10224d5275abSLijo Lazar 	kfree(tmp);
10234d5275abSLijo Lazar 
10244d5275abSLijo Lazar 	return ret;
10254d5275abSLijo Lazar }
10264d5275abSLijo Lazar 
amdgpu_acpi_get_xcc_info(struct amdgpu_acpi_xcc_info * xcc_info,u32 * sbdf)10274d5275abSLijo Lazar static int amdgpu_acpi_get_xcc_info(struct amdgpu_acpi_xcc_info *xcc_info,
1028d055714aSLijo Lazar 				    u32 *sbdf)
10294d5275abSLijo Lazar {
10304d5275abSLijo Lazar 	union acpi_object *obj;
10314d5275abSLijo Lazar 	acpi_status status;
10324d5275abSLijo Lazar 	int ret = -ENOENT;
10334d5275abSLijo Lazar 
10344d5275abSLijo Lazar 	obj = acpi_evaluate_dsm_typed(xcc_info->handle, &amd_xcc_dsm_guid, 0,
10354d5275abSLijo Lazar 				      AMD_XCC_DSM_GET_NUM_FUNCS, NULL,
10364d5275abSLijo Lazar 				      ACPI_TYPE_INTEGER);
10374d5275abSLijo Lazar 
10384d5275abSLijo Lazar 	if (!obj || obj->integer.value != AMD_XCC_DSM_NUM_FUNCS)
10394d5275abSLijo Lazar 		goto out;
10404d5275abSLijo Lazar 	ACPI_FREE(obj);
10414d5275abSLijo Lazar 
10424d5275abSLijo Lazar 	/* Evaluate DSMs and fill XCC information */
10434d5275abSLijo Lazar 	obj = acpi_evaluate_dsm_typed(xcc_info->handle, &amd_xcc_dsm_guid, 0,
10444d5275abSLijo Lazar 				      AMD_XCC_DSM_GET_VF_XCC_MAPPING, NULL,
10454d5275abSLijo Lazar 				      ACPI_TYPE_INTEGER);
10464d5275abSLijo Lazar 
10474d5275abSLijo Lazar 	if (!obj) {
10484d5275abSLijo Lazar 		acpi_handle_debug(xcc_info->handle,
10494d5275abSLijo Lazar 				  "_DSM function %d evaluation failed",
10504d5275abSLijo Lazar 				  AMD_XCC_DSM_GET_VF_XCC_MAPPING);
10514d5275abSLijo Lazar 		ret = -EINVAL;
10524d5275abSLijo Lazar 		goto out;
10534d5275abSLijo Lazar 	}
10544d5275abSLijo Lazar 
10554d5275abSLijo Lazar 	/* PF xcc id [39:32] */
10564d5275abSLijo Lazar 	xcc_info->phy_id = (obj->integer.value >> 32) & 0xFF;
10574d5275abSLijo Lazar 	/* xcp node of this xcc [47:40] */
10584d5275abSLijo Lazar 	xcc_info->xcp_node = (obj->integer.value >> 40) & 0xFF;
1059d055714aSLijo Lazar 	/* PF domain of this xcc [31:16] */
1060d055714aSLijo Lazar 	*sbdf = (obj->integer.value) & 0xFFFF0000;
10614d5275abSLijo Lazar 	/* PF bus/dev/fn of this xcc [63:48] */
1062d055714aSLijo Lazar 	*sbdf |= (obj->integer.value >> 48) & 0xFFFF;
10634d5275abSLijo Lazar 	ACPI_FREE(obj);
10644d5275abSLijo Lazar 	obj = NULL;
10654d5275abSLijo Lazar 
10661cc82301SLijo Lazar 	status =
10671cc82301SLijo Lazar 		amdgpu_acpi_get_node_id(xcc_info->handle, &xcc_info->numa_info);
10684d5275abSLijo Lazar 
10694d5275abSLijo Lazar 	/* TODO: check if this check is required */
10704d5275abSLijo Lazar 	if (ACPI_SUCCESS(status))
10714d5275abSLijo Lazar 		ret = 0;
10724d5275abSLijo Lazar out:
10734d5275abSLijo Lazar 	if (obj)
10744d5275abSLijo Lazar 		ACPI_FREE(obj);
10754d5275abSLijo Lazar 
10764d5275abSLijo Lazar 	return ret;
10774d5275abSLijo Lazar }
10784d5275abSLijo Lazar 
amdgpu_acpi_enumerate_xcc(void)10794d5275abSLijo Lazar static int amdgpu_acpi_enumerate_xcc(void)
10804d5275abSLijo Lazar {
10814d5275abSLijo Lazar 	struct amdgpu_acpi_dev_info *dev_info = NULL;
10824d5275abSLijo Lazar 	struct amdgpu_acpi_xcc_info *xcc_info;
10834d5275abSLijo Lazar 	struct acpi_device *acpi_dev;
10844d5275abSLijo Lazar 	char hid[ACPI_ID_LEN];
10854d5275abSLijo Lazar 	int ret, id;
1086d055714aSLijo Lazar 	u32 sbdf;
10874d5275abSLijo Lazar 
10884d5275abSLijo Lazar 	INIT_LIST_HEAD(&amdgpu_acpi_dev_list);
10891cc82301SLijo Lazar 	xa_init(&numa_info_xa);
10904d5275abSLijo Lazar 
10914d5275abSLijo Lazar 	for (id = 0; id < AMD_XCC_MAX_HID; id++) {
10924d5275abSLijo Lazar 		sprintf(hid, "%s%d", "AMD", AMD_XCC_HID_START + id);
10934d5275abSLijo Lazar 		acpi_dev = acpi_dev_get_first_match_dev(hid, NULL, -1);
10944d5275abSLijo Lazar 		/* These ACPI objects are expected to be in sequential order. If
10954d5275abSLijo Lazar 		 * one is not found, no need to check the rest.
10964d5275abSLijo Lazar 		 */
10974d5275abSLijo Lazar 		if (!acpi_dev) {
10984d5275abSLijo Lazar 			DRM_DEBUG_DRIVER("No matching acpi device found for %s",
10994d5275abSLijo Lazar 					 hid);
11004d5275abSLijo Lazar 			break;
11014d5275abSLijo Lazar 		}
11024d5275abSLijo Lazar 
11034d5275abSLijo Lazar 		xcc_info = kzalloc(sizeof(struct amdgpu_acpi_xcc_info),
11044d5275abSLijo Lazar 				   GFP_KERNEL);
11054d5275abSLijo Lazar 		if (!xcc_info) {
11064d5275abSLijo Lazar 			DRM_ERROR("Failed to allocate memory for xcc info\n");
11074d5275abSLijo Lazar 			return -ENOMEM;
11084d5275abSLijo Lazar 		}
11094d5275abSLijo Lazar 
11104d5275abSLijo Lazar 		INIT_LIST_HEAD(&xcc_info->list);
11114d5275abSLijo Lazar 		xcc_info->handle = acpi_device_handle(acpi_dev);
11124d5275abSLijo Lazar 		acpi_dev_put(acpi_dev);
11134d5275abSLijo Lazar 
1114d055714aSLijo Lazar 		ret = amdgpu_acpi_get_xcc_info(xcc_info, &sbdf);
11154d5275abSLijo Lazar 		if (ret) {
11164d5275abSLijo Lazar 			kfree(xcc_info);
11174d5275abSLijo Lazar 			continue;
11184d5275abSLijo Lazar 		}
11194d5275abSLijo Lazar 
1120d055714aSLijo Lazar 		dev_info = amdgpu_acpi_get_dev(sbdf);
11214d5275abSLijo Lazar 
11224d5275abSLijo Lazar 		if (!dev_info)
1123d055714aSLijo Lazar 			ret = amdgpu_acpi_dev_init(&dev_info, xcc_info, sbdf);
11244d5275abSLijo Lazar 
11254d5275abSLijo Lazar 		if (ret == -ENOMEM)
11264d5275abSLijo Lazar 			return ret;
11274d5275abSLijo Lazar 
11284d5275abSLijo Lazar 		if (!dev_info) {
11294d5275abSLijo Lazar 			kfree(xcc_info);
11304d5275abSLijo Lazar 			continue;
11314d5275abSLijo Lazar 		}
11324d5275abSLijo Lazar 
11334d5275abSLijo Lazar 		list_add_tail(&xcc_info->list, &dev_info->xcc_list);
11344d5275abSLijo Lazar 	}
11354d5275abSLijo Lazar 
11364d5275abSLijo Lazar 	return 0;
11374d5275abSLijo Lazar }
11384d5275abSLijo Lazar 
amdgpu_acpi_get_tmr_info(struct amdgpu_device * adev,u64 * tmr_offset,u64 * tmr_size)11396e018822SLijo Lazar int amdgpu_acpi_get_tmr_info(struct amdgpu_device *adev, u64 *tmr_offset,
11406e018822SLijo Lazar 			     u64 *tmr_size)
11416e018822SLijo Lazar {
11426e018822SLijo Lazar 	struct amdgpu_acpi_dev_info *dev_info;
1143d055714aSLijo Lazar 	u32 sbdf;
11446e018822SLijo Lazar 
11456e018822SLijo Lazar 	if (!tmr_offset || !tmr_size)
11466e018822SLijo Lazar 		return -EINVAL;
11476e018822SLijo Lazar 
1148d055714aSLijo Lazar 	sbdf = (pci_domain_nr(adev->pdev->bus) << 16);
1149d055714aSLijo Lazar 	sbdf |= pci_dev_id(adev->pdev);
1150d055714aSLijo Lazar 	dev_info = amdgpu_acpi_get_dev(sbdf);
11516e018822SLijo Lazar 	if (!dev_info)
11526e018822SLijo Lazar 		return -ENOENT;
11536e018822SLijo Lazar 
11546e018822SLijo Lazar 	*tmr_offset = dev_info->tmr_base;
11556e018822SLijo Lazar 	*tmr_size = dev_info->tmr_size;
11566e018822SLijo Lazar 
11576e018822SLijo Lazar 	return 0;
11586e018822SLijo Lazar }
11596e018822SLijo Lazar 
amdgpu_acpi_get_mem_info(struct amdgpu_device * adev,int xcc_id,struct amdgpu_numa_info * numa_info)1160fa0497c3SLijo Lazar int amdgpu_acpi_get_mem_info(struct amdgpu_device *adev, int xcc_id,
1161fa0497c3SLijo Lazar 			     struct amdgpu_numa_info *numa_info)
1162fa0497c3SLijo Lazar {
1163fa0497c3SLijo Lazar 	struct amdgpu_acpi_dev_info *dev_info;
1164fa0497c3SLijo Lazar 	struct amdgpu_acpi_xcc_info *xcc_info;
1165d055714aSLijo Lazar 	u32 sbdf;
1166fa0497c3SLijo Lazar 
1167fa0497c3SLijo Lazar 	if (!numa_info)
1168fa0497c3SLijo Lazar 		return -EINVAL;
1169fa0497c3SLijo Lazar 
1170d055714aSLijo Lazar 	sbdf = (pci_domain_nr(adev->pdev->bus) << 16);
1171d055714aSLijo Lazar 	sbdf |= pci_dev_id(adev->pdev);
1172d055714aSLijo Lazar 	dev_info = amdgpu_acpi_get_dev(sbdf);
1173fa0497c3SLijo Lazar 	if (!dev_info)
1174fa0497c3SLijo Lazar 		return -ENOENT;
1175fa0497c3SLijo Lazar 
1176fa0497c3SLijo Lazar 	list_for_each_entry(xcc_info, &dev_info->xcc_list, list) {
1177fa0497c3SLijo Lazar 		if (xcc_info->phy_id == xcc_id) {
1178fa0497c3SLijo Lazar 			memcpy(numa_info, xcc_info->numa_info,
1179fa0497c3SLijo Lazar 			       sizeof(*numa_info));
1180fa0497c3SLijo Lazar 			return 0;
1181fa0497c3SLijo Lazar 		}
1182fa0497c3SLijo Lazar 	}
1183fa0497c3SLijo Lazar 
1184fa0497c3SLijo Lazar 	return -ENOENT;
1185fa0497c3SLijo Lazar }
1186fa0497c3SLijo Lazar 
11874d5275abSLijo Lazar /**
1188d38ceaf9SAlex Deucher  * amdgpu_acpi_event - handle notify events
1189d38ceaf9SAlex Deucher  *
1190d38ceaf9SAlex Deucher  * @nb: notifier block
1191d38ceaf9SAlex Deucher  * @val: val
1192d38ceaf9SAlex Deucher  * @data: acpi event
1193d38ceaf9SAlex Deucher  *
1194d38ceaf9SAlex Deucher  * Calls relevant amdgpu functions in response to various
1195d38ceaf9SAlex Deucher  * acpi events.
1196d38ceaf9SAlex Deucher  * Returns NOTIFY code
1197d38ceaf9SAlex Deucher  */
amdgpu_acpi_event(struct notifier_block * nb,unsigned long val,void * data)1198d38ceaf9SAlex Deucher static int amdgpu_acpi_event(struct notifier_block *nb,
1199d38ceaf9SAlex Deucher 			     unsigned long val,
1200d38ceaf9SAlex Deucher 			     void *data)
1201d38ceaf9SAlex Deucher {
1202d38ceaf9SAlex Deucher 	struct amdgpu_device *adev = container_of(nb, struct amdgpu_device, acpi_nb);
1203d38ceaf9SAlex Deucher 	struct acpi_bus_event *entry = (struct acpi_bus_event *)data;
1204d38ceaf9SAlex Deucher 
1205d38ceaf9SAlex Deucher 	if (strcmp(entry->device_class, ACPI_AC_CLASS) == 0) {
1206d38ceaf9SAlex Deucher 		if (power_supply_is_system_supplied() > 0)
1207d38ceaf9SAlex Deucher 			DRM_DEBUG_DRIVER("pm: AC\n");
1208d38ceaf9SAlex Deucher 		else
1209d38ceaf9SAlex Deucher 			DRM_DEBUG_DRIVER("pm: DC\n");
1210d38ceaf9SAlex Deucher 
1211d38ceaf9SAlex Deucher 		amdgpu_pm_acpi_event_handler(adev);
1212d38ceaf9SAlex Deucher 	}
1213d38ceaf9SAlex Deucher 
1214d38ceaf9SAlex Deucher 	/* Check for pending SBIOS requests */
1215d38ceaf9SAlex Deucher 	return amdgpu_atif_handler(adev, entry);
1216d38ceaf9SAlex Deucher }
1217d38ceaf9SAlex Deucher 
1218d38ceaf9SAlex Deucher /* Call all ACPI methods here */
1219d38ceaf9SAlex Deucher /**
1220d38ceaf9SAlex Deucher  * amdgpu_acpi_init - init driver acpi support
1221d38ceaf9SAlex Deucher  *
1222d38ceaf9SAlex Deucher  * @adev: amdgpu_device pointer
1223d38ceaf9SAlex Deucher  *
1224d38ceaf9SAlex Deucher  * Verifies the AMD ACPI interfaces and registers with the acpi
1225d38ceaf9SAlex Deucher  * notifier chain (all asics).
1226d38ceaf9SAlex Deucher  * Returns 0 on success, error on failure.
1227d38ceaf9SAlex Deucher  */
amdgpu_acpi_init(struct amdgpu_device * adev)1228d38ceaf9SAlex Deucher int amdgpu_acpi_init(struct amdgpu_device *adev)
1229d38ceaf9SAlex Deucher {
1230f9b7f370SAlex Deucher 	struct amdgpu_atif *atif = &amdgpu_acpi_priv.atif;
1231d38ceaf9SAlex Deucher 
12329c27bc97SYe Bin 	if (atif->notifications.brightness_change) {
1233d09ef243SAlex Deucher 		if (adev->dc_enabled) {
123497d798b2SAlex Deucher #if defined(CONFIG_DRM_AMD_DC)
123597d798b2SAlex Deucher 			struct amdgpu_display_manager *dm = &adev->dm;
1236556bdae3SJingyu Wang 
12377fd13baeSAlex Deucher 			if (dm->backlight_dev[0])
12387fd13baeSAlex Deucher 				atif->bd = dm->backlight_dev[0];
123997d798b2SAlex Deucher #endif
124097d798b2SAlex Deucher 		} else {
1241d38ceaf9SAlex Deucher 			struct drm_encoder *tmp;
1242d38ceaf9SAlex Deucher 
1243d38ceaf9SAlex Deucher 			/* Find the encoder controlling the brightness */
12444a580877SLuben Tuikov 			list_for_each_entry(tmp, &adev_to_drm(adev)->mode_config.encoder_list,
1245d38ceaf9SAlex Deucher 					    head) {
1246d38ceaf9SAlex Deucher 				struct amdgpu_encoder *enc = to_amdgpu_encoder(tmp);
1247d38ceaf9SAlex Deucher 
1248d38ceaf9SAlex Deucher 				if ((enc->devices & (ATOM_DEVICE_LCD_SUPPORT)) &&
1249d38ceaf9SAlex Deucher 				    enc->enc_priv) {
1250d38ceaf9SAlex Deucher 					struct amdgpu_encoder_atom_dig *dig = enc->enc_priv;
1251556bdae3SJingyu Wang 
1252d38ceaf9SAlex Deucher 					if (dig->bl_dev) {
125397d798b2SAlex Deucher 						atif->bd = dig->bl_dev;
1254d38ceaf9SAlex Deucher 						break;
1255d38ceaf9SAlex Deucher 					}
1256d38ceaf9SAlex Deucher 				}
1257d38ceaf9SAlex Deucher 			}
1258d38ceaf9SAlex Deucher 		}
125997d798b2SAlex Deucher 	}
1260f9b7f370SAlex Deucher 	adev->acpi_nb.notifier_call = amdgpu_acpi_event;
1261f9b7f370SAlex Deucher 	register_acpi_notifier(&adev->acpi_nb);
1262f9b7f370SAlex Deucher 
1263f9b7f370SAlex Deucher 	return 0;
1264f9b7f370SAlex Deucher }
1265f9b7f370SAlex Deucher 
amdgpu_acpi_get_backlight_caps(struct amdgpu_dm_backlight_caps * caps)1266f9b7f370SAlex Deucher void amdgpu_acpi_get_backlight_caps(struct amdgpu_dm_backlight_caps *caps)
1267f9b7f370SAlex Deucher {
1268f9b7f370SAlex Deucher 	struct amdgpu_atif *atif = &amdgpu_acpi_priv.atif;
1269f9b7f370SAlex Deucher 
1270f9b7f370SAlex Deucher 	caps->caps_valid = atif->backlight_caps.caps_valid;
1271f9b7f370SAlex Deucher 	caps->min_input_signal = atif->backlight_caps.min_input_signal;
1272f9b7f370SAlex Deucher 	caps->max_input_signal = atif->backlight_caps.max_input_signal;
1273*2fe87f54SMario Limonciello 	caps->ac_level = atif->backlight_caps.ac_level;
1274*2fe87f54SMario Limonciello 	caps->dc_level = atif->backlight_caps.dc_level;
1275f9b7f370SAlex Deucher }
1276f9b7f370SAlex Deucher 
1277f9b7f370SAlex Deucher /**
1278f9b7f370SAlex Deucher  * amdgpu_acpi_fini - tear down driver acpi support
1279f9b7f370SAlex Deucher  *
1280f9b7f370SAlex Deucher  * @adev: amdgpu_device pointer
1281f9b7f370SAlex Deucher  *
1282f9b7f370SAlex Deucher  * Unregisters with the acpi notifier chain (all asics).
1283f9b7f370SAlex Deucher  */
amdgpu_acpi_fini(struct amdgpu_device * adev)1284f9b7f370SAlex Deucher void amdgpu_acpi_fini(struct amdgpu_device *adev)
1285f9b7f370SAlex Deucher {
1286f9b7f370SAlex Deucher 	unregister_acpi_notifier(&adev->acpi_nb);
1287f9b7f370SAlex Deucher }
1288f9b7f370SAlex Deucher 
1289f9b7f370SAlex Deucher /**
1290f9b7f370SAlex Deucher  * amdgpu_atif_pci_probe_handle - look up the ATIF handle
1291f9b7f370SAlex Deucher  *
1292f9b7f370SAlex Deucher  * @pdev: pci device
1293f9b7f370SAlex Deucher  *
1294f9b7f370SAlex Deucher  * Look up the ATIF handles (all asics).
1295f9b7f370SAlex Deucher  * Returns true if the handle is found, false if not.
1296f9b7f370SAlex Deucher  */
amdgpu_atif_pci_probe_handle(struct pci_dev * pdev)1297f9b7f370SAlex Deucher static bool amdgpu_atif_pci_probe_handle(struct pci_dev *pdev)
1298f9b7f370SAlex Deucher {
1299f9b7f370SAlex Deucher 	char acpi_method_name[255] = { 0 };
1300f9b7f370SAlex Deucher 	struct acpi_buffer buffer = {sizeof(acpi_method_name), acpi_method_name};
1301f9b7f370SAlex Deucher 	acpi_handle dhandle, atif_handle;
1302f9b7f370SAlex Deucher 	acpi_status status;
1303f9b7f370SAlex Deucher 	int ret;
1304f9b7f370SAlex Deucher 
1305f9b7f370SAlex Deucher 	dhandle = ACPI_HANDLE(&pdev->dev);
1306f9b7f370SAlex Deucher 	if (!dhandle)
1307f9b7f370SAlex Deucher 		return false;
1308f9b7f370SAlex Deucher 
1309f9b7f370SAlex Deucher 	status = acpi_get_handle(dhandle, "ATIF", &atif_handle);
1310556bdae3SJingyu Wang 	if (ACPI_FAILURE(status))
1311f9b7f370SAlex Deucher 		return false;
1312556bdae3SJingyu Wang 
1313f9b7f370SAlex Deucher 	amdgpu_acpi_priv.atif.handle = atif_handle;
1314f9b7f370SAlex Deucher 	acpi_get_name(amdgpu_acpi_priv.atif.handle, ACPI_FULL_PATHNAME, &buffer);
1315f9b7f370SAlex Deucher 	DRM_DEBUG_DRIVER("Found ATIF handle %s\n", acpi_method_name);
1316f9b7f370SAlex Deucher 	ret = amdgpu_atif_verify_interface(&amdgpu_acpi_priv.atif);
1317f9b7f370SAlex Deucher 	if (ret) {
1318f9b7f370SAlex Deucher 		amdgpu_acpi_priv.atif.handle = 0;
1319f9b7f370SAlex Deucher 		return false;
1320f9b7f370SAlex Deucher 	}
1321f9b7f370SAlex Deucher 	return true;
1322f9b7f370SAlex Deucher }
1323f9b7f370SAlex Deucher 
1324f9b7f370SAlex Deucher /**
1325f9b7f370SAlex Deucher  * amdgpu_atcs_pci_probe_handle - look up the ATCS handle
1326f9b7f370SAlex Deucher  *
1327f9b7f370SAlex Deucher  * @pdev: pci device
1328f9b7f370SAlex Deucher  *
1329f9b7f370SAlex Deucher  * Look up the ATCS handles (all asics).
1330f9b7f370SAlex Deucher  * Returns true if the handle is found, false if not.
1331f9b7f370SAlex Deucher  */
amdgpu_atcs_pci_probe_handle(struct pci_dev * pdev)1332f9b7f370SAlex Deucher static bool amdgpu_atcs_pci_probe_handle(struct pci_dev *pdev)
1333f9b7f370SAlex Deucher {
1334f9b7f370SAlex Deucher 	char acpi_method_name[255] = { 0 };
1335f9b7f370SAlex Deucher 	struct acpi_buffer buffer = { sizeof(acpi_method_name), acpi_method_name };
1336f9b7f370SAlex Deucher 	acpi_handle dhandle, atcs_handle;
1337f9b7f370SAlex Deucher 	acpi_status status;
1338f9b7f370SAlex Deucher 	int ret;
1339f9b7f370SAlex Deucher 
1340f9b7f370SAlex Deucher 	dhandle = ACPI_HANDLE(&pdev->dev);
1341f9b7f370SAlex Deucher 	if (!dhandle)
1342f9b7f370SAlex Deucher 		return false;
1343f9b7f370SAlex Deucher 
1344f9b7f370SAlex Deucher 	status = acpi_get_handle(dhandle, "ATCS", &atcs_handle);
1345556bdae3SJingyu Wang 	if (ACPI_FAILURE(status))
1346f9b7f370SAlex Deucher 		return false;
1347556bdae3SJingyu Wang 
1348f9b7f370SAlex Deucher 	amdgpu_acpi_priv.atcs.handle = atcs_handle;
1349f9b7f370SAlex Deucher 	acpi_get_name(amdgpu_acpi_priv.atcs.handle, ACPI_FULL_PATHNAME, &buffer);
1350f9b7f370SAlex Deucher 	DRM_DEBUG_DRIVER("Found ATCS handle %s\n", acpi_method_name);
1351f9b7f370SAlex Deucher 	ret = amdgpu_atcs_verify_interface(&amdgpu_acpi_priv.atcs);
1352f9b7f370SAlex Deucher 	if (ret) {
1353f9b7f370SAlex Deucher 		amdgpu_acpi_priv.atcs.handle = 0;
1354f9b7f370SAlex Deucher 		return false;
1355f9b7f370SAlex Deucher 	}
1356f9b7f370SAlex Deucher 	return true;
1357f9b7f370SAlex Deucher }
1358f9b7f370SAlex Deucher 
1359aaee0ce4STim Huang 
1360aaee0ce4STim Huang /**
1361aaee0ce4STim Huang  * amdgpu_acpi_should_gpu_reset
1362aaee0ce4STim Huang  *
1363aaee0ce4STim Huang  * @adev: amdgpu_device_pointer
1364aaee0ce4STim Huang  *
1365aaee0ce4STim Huang  * returns true if should reset GPU, false if not
1366aaee0ce4STim Huang  */
amdgpu_acpi_should_gpu_reset(struct amdgpu_device * adev)1367aaee0ce4STim Huang bool amdgpu_acpi_should_gpu_reset(struct amdgpu_device *adev)
1368aaee0ce4STim Huang {
1369980d5baeSTim Huang 	if ((adev->flags & AMD_IS_APU) &&
1370980d5baeSTim Huang 	    adev->gfx.imu.funcs) /* Not need to do mode2 reset for IMU enabled APUs */
1371980d5baeSTim Huang 		return false;
1372980d5baeSTim Huang 
1373980d5baeSTim Huang 	if ((adev->flags & AMD_IS_APU) &&
1374980d5baeSTim Huang 	    amdgpu_acpi_is_s3_active(adev))
1375aaee0ce4STim Huang 		return false;
1376aaee0ce4STim Huang 
1377aaee0ce4STim Huang 	if (amdgpu_sriov_vf(adev))
1378aaee0ce4STim Huang 		return false;
1379aaee0ce4STim Huang 
1380aaee0ce4STim Huang #if IS_ENABLED(CONFIG_SUSPEND)
1381aaee0ce4STim Huang 	return pm_suspend_target_state != PM_SUSPEND_TO_IDLE;
1382aaee0ce4STim Huang #else
1383aaee0ce4STim Huang 	return true;
1384aaee0ce4STim Huang #endif
1385aaee0ce4STim Huang }
1386aaee0ce4STim Huang 
1387f9b7f370SAlex Deucher /*
1388f9b7f370SAlex Deucher  * amdgpu_acpi_detect - detect ACPI ATIF/ATCS methods
1389f9b7f370SAlex Deucher  *
1390f9b7f370SAlex Deucher  * Check if we have the ATIF/ATCS methods and populate
1391f9b7f370SAlex Deucher  * the structures in the driver.
1392f9b7f370SAlex Deucher  */
amdgpu_acpi_detect(void)1393f9b7f370SAlex Deucher void amdgpu_acpi_detect(void)
1394f9b7f370SAlex Deucher {
1395f9b7f370SAlex Deucher 	struct amdgpu_atif *atif = &amdgpu_acpi_priv.atif;
1396f9b7f370SAlex Deucher 	struct amdgpu_atcs *atcs = &amdgpu_acpi_priv.atcs;
1397f9b7f370SAlex Deucher 	struct pci_dev *pdev = NULL;
1398f9b7f370SAlex Deucher 	int ret;
1399f9b7f370SAlex Deucher 
140018bf4005SSui Jingfeng 	while ((pdev = pci_get_base_class(PCI_BASE_CLASS_DISPLAY, pdev))) {
140118bf4005SSui Jingfeng 		if ((pdev->class != PCI_CLASS_DISPLAY_VGA << 8) &&
140218bf4005SSui Jingfeng 		    (pdev->class != PCI_CLASS_DISPLAY_OTHER << 8))
140318bf4005SSui Jingfeng 			continue;
1404f9b7f370SAlex Deucher 
1405f9b7f370SAlex Deucher 		if (!atif->handle)
1406f9b7f370SAlex Deucher 			amdgpu_atif_pci_probe_handle(pdev);
1407f9b7f370SAlex Deucher 		if (!atcs->handle)
1408f9b7f370SAlex Deucher 			amdgpu_atcs_pci_probe_handle(pdev);
1409f9b7f370SAlex Deucher 	}
1410d38ceaf9SAlex Deucher 
1411d38ceaf9SAlex Deucher 	if (atif->functions.sbios_requests && !atif->functions.system_params) {
1412d38ceaf9SAlex Deucher 		/* XXX check this workraround, if sbios request function is
1413d38ceaf9SAlex Deucher 		 * present we have to see how it's configured in the system
1414d38ceaf9SAlex Deucher 		 * params
1415d38ceaf9SAlex Deucher 		 */
1416d38ceaf9SAlex Deucher 		atif->functions.system_params = true;
1417d38ceaf9SAlex Deucher 	}
1418d38ceaf9SAlex Deucher 
1419d38ceaf9SAlex Deucher 	if (atif->functions.system_params) {
1420280cf1a9SLyude Paul 		ret = amdgpu_atif_get_notification_params(atif);
1421d38ceaf9SAlex Deucher 		if (ret) {
1422d38ceaf9SAlex Deucher 			DRM_DEBUG_DRIVER("Call to GET_SYSTEM_PARAMS failed: %d\n",
1423d38ceaf9SAlex Deucher 					ret);
1424d38ceaf9SAlex Deucher 			/* Disable notification */
1425d38ceaf9SAlex Deucher 			atif->notification_cfg.enabled = false;
1426d38ceaf9SAlex Deucher 		}
1427d38ceaf9SAlex Deucher 	}
1428d38ceaf9SAlex Deucher 
1429206bbafeSDavid Francis 	if (atif->functions.query_backlight_transfer_characteristics) {
1430206bbafeSDavid Francis 		ret = amdgpu_atif_query_backlight_caps(atif);
1431206bbafeSDavid Francis 		if (ret) {
1432206bbafeSDavid Francis 			DRM_DEBUG_DRIVER("Call to QUERY_BACKLIGHT_TRANSFER_CHARACTERISTICS failed: %d\n",
1433206bbafeSDavid Francis 					ret);
1434206bbafeSDavid Francis 			atif->backlight_caps.caps_valid = false;
1435206bbafeSDavid Francis 		}
1436206bbafeSDavid Francis 	} else {
1437206bbafeSDavid Francis 		atif->backlight_caps.caps_valid = false;
1438206bbafeSDavid Francis 	}
14394d5275abSLijo Lazar 
14404d5275abSLijo Lazar 	amdgpu_acpi_enumerate_xcc();
14414d5275abSLijo Lazar }
14424d5275abSLijo Lazar 
amdgpu_acpi_release(void)14434d5275abSLijo Lazar void amdgpu_acpi_release(void)
14444d5275abSLijo Lazar {
14454d5275abSLijo Lazar 	struct amdgpu_acpi_dev_info *dev_info, *dev_tmp;
14464d5275abSLijo Lazar 	struct amdgpu_acpi_xcc_info *xcc_info, *xcc_tmp;
14471cc82301SLijo Lazar 	struct amdgpu_numa_info *numa_info;
14481cc82301SLijo Lazar 	unsigned long index;
14491cc82301SLijo Lazar 
14501cc82301SLijo Lazar 	xa_for_each(&numa_info_xa, index, numa_info) {
14511cc82301SLijo Lazar 		kfree(numa_info);
14521cc82301SLijo Lazar 		xa_erase(&numa_info_xa, index);
14531cc82301SLijo Lazar 	}
14544d5275abSLijo Lazar 
14554d5275abSLijo Lazar 	if (list_empty(&amdgpu_acpi_dev_list))
14564d5275abSLijo Lazar 		return;
14574d5275abSLijo Lazar 
14584d5275abSLijo Lazar 	list_for_each_entry_safe(dev_info, dev_tmp, &amdgpu_acpi_dev_list,
14594d5275abSLijo Lazar 				 list) {
14604d5275abSLijo Lazar 		list_for_each_entry_safe(xcc_info, xcc_tmp, &dev_info->xcc_list,
14614d5275abSLijo Lazar 					 list) {
14624d5275abSLijo Lazar 			list_del(&xcc_info->list);
14634d5275abSLijo Lazar 			kfree(xcc_info);
14644d5275abSLijo Lazar 		}
14654d5275abSLijo Lazar 
14664d5275abSLijo Lazar 		list_del(&dev_info->list);
14674d5275abSLijo Lazar 		kfree(dev_info);
14684d5275abSLijo Lazar 	}
1469d38ceaf9SAlex Deucher }
14704cd078dcSPrike Liang 
1471f588a1bbSMario Limonciello #if IS_ENABLED(CONFIG_SUSPEND)
14724cd078dcSPrike Liang /**
147318b66aceSMario Limonciello  * amdgpu_acpi_is_s3_active
147418b66aceSMario Limonciello  *
147518b66aceSMario Limonciello  * @adev: amdgpu_device_pointer
147618b66aceSMario Limonciello  *
147718b66aceSMario Limonciello  * returns true if supported, false if not.
147818b66aceSMario Limonciello  */
amdgpu_acpi_is_s3_active(struct amdgpu_device * adev)147918b66aceSMario Limonciello bool amdgpu_acpi_is_s3_active(struct amdgpu_device *adev)
148018b66aceSMario Limonciello {
148118b66aceSMario Limonciello 	return !(adev->flags & AMD_IS_APU) ||
148218b66aceSMario Limonciello 		(pm_suspend_target_state == PM_SUSPEND_MEM);
148318b66aceSMario Limonciello }
148418b66aceSMario Limonciello 
148518b66aceSMario Limonciello /**
1486d0260f62SPratik Vishwakarma  * amdgpu_acpi_is_s0ix_active
14874cd078dcSPrike Liang  *
14881fdbbc12SFabio M. De Francesco  * @adev: amdgpu_device_pointer
14891fdbbc12SFabio M. De Francesco  *
14904cd078dcSPrike Liang  * returns true if supported, false if not.
14914cd078dcSPrike Liang  */
amdgpu_acpi_is_s0ix_active(struct amdgpu_device * adev)1492d0260f62SPratik Vishwakarma bool amdgpu_acpi_is_s0ix_active(struct amdgpu_device *adev)
14934cd078dcSPrike Liang {
1494f588a1bbSMario Limonciello 	if (!(adev->flags & AMD_IS_APU) ||
1495f588a1bbSMario Limonciello 	    (pm_suspend_target_state != PM_SUSPEND_TO_IDLE))
1496f588a1bbSMario Limonciello 		return false;
1497f588a1bbSMario Limonciello 
1498ca475186SMario Limonciello 	if (adev->asic_type < CHIP_RAVEN)
1499ca475186SMario Limonciello 		return false;
1500ca475186SMario Limonciello 
1501e4c44b1aSMario Limonciello 	if (!(adev->pm.pp_feature & PP_GFXOFF_MASK))
1502e4c44b1aSMario Limonciello 		return false;
1503e4c44b1aSMario Limonciello 
150461ebd2feSRafael J. Wysocki 	/*
150561ebd2feSRafael J. Wysocki 	 * If ACPI_FADT_LOW_POWER_S0 is not set in the FADT, it is generally
150661ebd2feSRafael J. Wysocki 	 * risky to do any special firmware-related preparations for entering
150761ebd2feSRafael J. Wysocki 	 * S0ix even though the system is suspending to idle, so return false
150861ebd2feSRafael J. Wysocki 	 * in that case.
150961ebd2feSRafael J. Wysocki 	 */
151009521b5dSMario Limonciello 	if (!(acpi_gbl_FADT.flags & ACPI_FADT_LOW_POWER_S0)) {
1511257d7b7bSMario Limonciello 		dev_err_once(adev->dev,
1512f588a1bbSMario Limonciello 			      "Power consumption will be higher as BIOS has not been configured for suspend-to-idle.\n"
1513f588a1bbSMario Limonciello 			      "To use suspend-to-idle change the sleep mode in BIOS setup.\n");
151409521b5dSMario Limonciello 		return false;
151509521b5dSMario Limonciello 	}
1516f588a1bbSMario Limonciello 
1517f588a1bbSMario Limonciello #if !IS_ENABLED(CONFIG_AMD_PMC)
1518257d7b7bSMario Limonciello 	dev_err_once(adev->dev,
1519f588a1bbSMario Limonciello 		      "Power consumption will be higher as the kernel has not been compiled with CONFIG_AMD_PMC.\n");
152009521b5dSMario Limonciello 	return false;
152109521b5dSMario Limonciello #else
1522cf488dcdSMario Limonciello 	return true;
152309521b5dSMario Limonciello #endif /* CONFIG_AMD_PMC */
1524f588a1bbSMario Limonciello }
1525f588a1bbSMario Limonciello 
15263a9626c8SMario Limonciello /**
15273a9626c8SMario Limonciello  * amdgpu_choose_low_power_state
15283a9626c8SMario Limonciello  *
15293a9626c8SMario Limonciello  * @adev: amdgpu_device_pointer
15303a9626c8SMario Limonciello  *
15313a9626c8SMario Limonciello  * Choose the target low power state for the GPU
15323a9626c8SMario Limonciello  */
amdgpu_choose_low_power_state(struct amdgpu_device * adev)15333a9626c8SMario Limonciello void amdgpu_choose_low_power_state(struct amdgpu_device *adev)
15343a9626c8SMario Limonciello {
1535bbfaf2aeSMa Jun 	if (adev->in_runpm)
1536bbfaf2aeSMa Jun 		return;
1537bbfaf2aeSMa Jun 
15383a9626c8SMario Limonciello 	if (amdgpu_acpi_is_s0ix_active(adev))
15393a9626c8SMario Limonciello 		adev->in_s0ix = true;
15403a9626c8SMario Limonciello 	else if (amdgpu_acpi_is_s3_active(adev))
15413a9626c8SMario Limonciello 		adev->in_s3 = true;
15423a9626c8SMario Limonciello }
15433a9626c8SMario Limonciello 
1544f588a1bbSMario Limonciello #endif /* CONFIG_SUSPEND */
1545