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;
150c9b7c809SMario Limonciello 	union acpi_object *obj;
151d38ceaf9SAlex Deucher 	union acpi_object atif_arg_elements[2];
152d38ceaf9SAlex Deucher 	struct acpi_object_list atif_arg;
153d38ceaf9SAlex Deucher 	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
154d38ceaf9SAlex Deucher 
155d38ceaf9SAlex Deucher 	atif_arg.count = 2;
156d38ceaf9SAlex Deucher 	atif_arg.pointer = &atif_arg_elements[0];
157d38ceaf9SAlex Deucher 
158d38ceaf9SAlex Deucher 	atif_arg_elements[0].type = ACPI_TYPE_INTEGER;
159d38ceaf9SAlex Deucher 	atif_arg_elements[0].integer.value = function;
160d38ceaf9SAlex Deucher 
161d38ceaf9SAlex Deucher 	if (params) {
162d38ceaf9SAlex Deucher 		atif_arg_elements[1].type = ACPI_TYPE_BUFFER;
163d38ceaf9SAlex Deucher 		atif_arg_elements[1].buffer.length = params->length;
164d38ceaf9SAlex Deucher 		atif_arg_elements[1].buffer.pointer = params->pointer;
165d38ceaf9SAlex Deucher 	} else {
166d38ceaf9SAlex Deucher 		/* We need a second fake parameter */
167d38ceaf9SAlex Deucher 		atif_arg_elements[1].type = ACPI_TYPE_INTEGER;
168d38ceaf9SAlex Deucher 		atif_arg_elements[1].integer.value = 0;
169d38ceaf9SAlex Deucher 	}
170d38ceaf9SAlex Deucher 
171280cf1a9SLyude Paul 	status = acpi_evaluate_object(atif->handle, NULL, &atif_arg,
172280cf1a9SLyude Paul 				      &buffer);
173c9b7c809SMario Limonciello 	obj = (union acpi_object *)buffer.pointer;
174d38ceaf9SAlex Deucher 
17591c9e221SAntonio Quartulli 	/* Fail if calling the method fails */
17691c9e221SAntonio Quartulli 	if (ACPI_FAILURE(status)) {
177d38ceaf9SAlex Deucher 		DRM_DEBUG_DRIVER("failed to evaluate ATIF got %s\n",
178d38ceaf9SAlex Deucher 				 acpi_format_exception(status));
179c9b7c809SMario Limonciello 		kfree(obj);
180d38ceaf9SAlex Deucher 		return NULL;
181d38ceaf9SAlex Deucher 	}
182d38ceaf9SAlex Deucher 
183c9b7c809SMario Limonciello 	if (obj->type != ACPI_TYPE_BUFFER) {
184c9b7c809SMario Limonciello 		DRM_DEBUG_DRIVER("bad object returned from ATIF: %d\n",
185c9b7c809SMario Limonciello 				 obj->type);
186c9b7c809SMario Limonciello 		kfree(obj);
187c9b7c809SMario Limonciello 		return NULL;
188c9b7c809SMario Limonciello 	}
189c9b7c809SMario Limonciello 
190c9b7c809SMario Limonciello 	return obj;
191d38ceaf9SAlex Deucher }
192d38ceaf9SAlex Deucher 
193d38ceaf9SAlex Deucher /**
194d38ceaf9SAlex Deucher  * amdgpu_atif_parse_notification - parse supported notifications
195d38ceaf9SAlex Deucher  *
196d38ceaf9SAlex Deucher  * @n: supported notifications struct
197d38ceaf9SAlex Deucher  * @mask: supported notifications mask from ATIF
198d38ceaf9SAlex Deucher  *
199d38ceaf9SAlex Deucher  * Use the supported notifications mask from ATIF function
200d38ceaf9SAlex Deucher  * ATIF_FUNCTION_VERIFY_INTERFACE to determine what notifications
201d38ceaf9SAlex Deucher  * are supported (all asics).
202d38ceaf9SAlex Deucher  */
amdgpu_atif_parse_notification(struct amdgpu_atif_notifications * n,u32 mask)203d38ceaf9SAlex Deucher static void amdgpu_atif_parse_notification(struct amdgpu_atif_notifications *n, u32 mask)
204d38ceaf9SAlex Deucher {
205d38ceaf9SAlex Deucher 	n->thermal_state = mask & ATIF_THERMAL_STATE_CHANGE_REQUEST_SUPPORTED;
206d38ceaf9SAlex Deucher 	n->forced_power_state = mask & ATIF_FORCED_POWER_STATE_CHANGE_REQUEST_SUPPORTED;
207d38ceaf9SAlex Deucher 	n->system_power_state = mask & ATIF_SYSTEM_POWER_SOURCE_CHANGE_REQUEST_SUPPORTED;
208d38ceaf9SAlex Deucher 	n->brightness_change = mask & ATIF_PANEL_BRIGHTNESS_CHANGE_REQUEST_SUPPORTED;
209d38ceaf9SAlex Deucher 	n->dgpu_display_event = mask & ATIF_DGPU_DISPLAY_EVENT_SUPPORTED;
2107349a3afSDavid Francis 	n->gpu_package_power_limit = mask & ATIF_GPU_PACKAGE_POWER_LIMIT_REQUEST_SUPPORTED;
211d38ceaf9SAlex Deucher }
212d38ceaf9SAlex Deucher 
213d38ceaf9SAlex Deucher /**
214d38ceaf9SAlex Deucher  * amdgpu_atif_parse_functions - parse supported functions
215d38ceaf9SAlex Deucher  *
216d38ceaf9SAlex Deucher  * @f: supported functions struct
217d38ceaf9SAlex Deucher  * @mask: supported functions mask from ATIF
218d38ceaf9SAlex Deucher  *
219d38ceaf9SAlex Deucher  * Use the supported functions mask from ATIF function
220d38ceaf9SAlex Deucher  * ATIF_FUNCTION_VERIFY_INTERFACE to determine what functions
221d38ceaf9SAlex Deucher  * are supported (all asics).
222d38ceaf9SAlex Deucher  */
amdgpu_atif_parse_functions(struct amdgpu_atif_functions * f,u32 mask)223d38ceaf9SAlex Deucher static void amdgpu_atif_parse_functions(struct amdgpu_atif_functions *f, u32 mask)
224d38ceaf9SAlex Deucher {
225d38ceaf9SAlex Deucher 	f->system_params = mask & ATIF_GET_SYSTEM_PARAMETERS_SUPPORTED;
226d38ceaf9SAlex Deucher 	f->sbios_requests = mask & ATIF_GET_SYSTEM_BIOS_REQUESTS_SUPPORTED;
227d38ceaf9SAlex Deucher 	f->temperature_change = mask & ATIF_TEMPERATURE_CHANGE_NOTIFICATION_SUPPORTED;
2287349a3afSDavid Francis 	f->query_backlight_transfer_characteristics =
2297349a3afSDavid Francis 		mask & ATIF_QUERY_BACKLIGHT_TRANSFER_CHARACTERISTICS_SUPPORTED;
2307349a3afSDavid Francis 	f->ready_to_undock = mask & ATIF_READY_TO_UNDOCK_NOTIFICATION_SUPPORTED;
2317349a3afSDavid Francis 	f->external_gpu_information = mask & ATIF_GET_EXTERNAL_GPU_INFORMATION_SUPPORTED;
232d38ceaf9SAlex Deucher }
233d38ceaf9SAlex Deucher 
234d38ceaf9SAlex Deucher /**
235d38ceaf9SAlex Deucher  * amdgpu_atif_verify_interface - verify ATIF
236d38ceaf9SAlex Deucher  *
237d38ceaf9SAlex Deucher  * @atif: amdgpu atif struct
238d38ceaf9SAlex Deucher  *
239d38ceaf9SAlex Deucher  * Execute the ATIF_FUNCTION_VERIFY_INTERFACE ATIF function
240d38ceaf9SAlex Deucher  * to initialize ATIF and determine what features are supported
241d38ceaf9SAlex Deucher  * (all asics).
242d38ceaf9SAlex Deucher  * returns 0 on success, error on failure.
243d38ceaf9SAlex Deucher  */
amdgpu_atif_verify_interface(struct amdgpu_atif * atif)244280cf1a9SLyude Paul static int amdgpu_atif_verify_interface(struct amdgpu_atif *atif)
245d38ceaf9SAlex Deucher {
246d38ceaf9SAlex Deucher 	union acpi_object *info;
247d38ceaf9SAlex Deucher 	struct atif_verify_interface output;
248d38ceaf9SAlex Deucher 	size_t size;
249d38ceaf9SAlex Deucher 	int err = 0;
250d38ceaf9SAlex Deucher 
251280cf1a9SLyude Paul 	info = amdgpu_atif_call(atif, ATIF_FUNCTION_VERIFY_INTERFACE, NULL);
252d38ceaf9SAlex Deucher 	if (!info)
253d38ceaf9SAlex Deucher 		return -EIO;
254d38ceaf9SAlex Deucher 
255d38ceaf9SAlex Deucher 	memset(&output, 0, sizeof(output));
256d38ceaf9SAlex Deucher 
257d38ceaf9SAlex Deucher 	size = *(u16 *) info->buffer.pointer;
258d38ceaf9SAlex Deucher 	if (size < 12) {
259d38ceaf9SAlex Deucher 		DRM_INFO("ATIF buffer is too small: %zu\n", size);
260d38ceaf9SAlex Deucher 		err = -EINVAL;
261d38ceaf9SAlex Deucher 		goto out;
262d38ceaf9SAlex Deucher 	}
263d38ceaf9SAlex Deucher 	size = min(sizeof(output), size);
264d38ceaf9SAlex Deucher 
265d38ceaf9SAlex Deucher 	memcpy(&output, info->buffer.pointer, size);
266d38ceaf9SAlex Deucher 
267d38ceaf9SAlex Deucher 	/* TODO: check version? */
268d38ceaf9SAlex Deucher 	DRM_DEBUG_DRIVER("ATIF version %u\n", output.version);
269d38ceaf9SAlex Deucher 
270d38ceaf9SAlex Deucher 	amdgpu_atif_parse_notification(&atif->notifications, output.notification_mask);
271d38ceaf9SAlex Deucher 	amdgpu_atif_parse_functions(&atif->functions, output.function_bits);
272d38ceaf9SAlex Deucher 
273d38ceaf9SAlex Deucher out:
274d38ceaf9SAlex Deucher 	kfree(info);
275d38ceaf9SAlex Deucher 	return err;
276d38ceaf9SAlex Deucher }
277d38ceaf9SAlex Deucher 
278d38ceaf9SAlex Deucher /**
279d38ceaf9SAlex Deucher  * amdgpu_atif_get_notification_params - determine notify configuration
280d38ceaf9SAlex Deucher  *
2811fdbbc12SFabio M. De Francesco  * @atif: acpi handle
282d38ceaf9SAlex Deucher  *
283d38ceaf9SAlex Deucher  * Execute the ATIF_FUNCTION_GET_SYSTEM_PARAMETERS ATIF function
284d38ceaf9SAlex Deucher  * to determine if a notifier is used and if so which one
285d38ceaf9SAlex Deucher  * (all asics).  This is either Notify(VGA, 0x81) or Notify(VGA, n)
286d38ceaf9SAlex Deucher  * where n is specified in the result if a notifier is used.
287d38ceaf9SAlex Deucher  * Returns 0 on success, error on failure.
288d38ceaf9SAlex Deucher  */
amdgpu_atif_get_notification_params(struct amdgpu_atif * atif)289280cf1a9SLyude Paul static int amdgpu_atif_get_notification_params(struct amdgpu_atif *atif)
290d38ceaf9SAlex Deucher {
291d38ceaf9SAlex Deucher 	union acpi_object *info;
292280cf1a9SLyude Paul 	struct amdgpu_atif_notification_cfg *n = &atif->notification_cfg;
293d38ceaf9SAlex Deucher 	struct atif_system_params params;
294d38ceaf9SAlex Deucher 	size_t size;
295d38ceaf9SAlex Deucher 	int err = 0;
296d38ceaf9SAlex Deucher 
297280cf1a9SLyude Paul 	info = amdgpu_atif_call(atif, ATIF_FUNCTION_GET_SYSTEM_PARAMETERS,
298280cf1a9SLyude Paul 				NULL);
299d38ceaf9SAlex Deucher 	if (!info) {
300d38ceaf9SAlex Deucher 		err = -EIO;
301d38ceaf9SAlex Deucher 		goto out;
302d38ceaf9SAlex Deucher 	}
303d38ceaf9SAlex Deucher 
304d38ceaf9SAlex Deucher 	size = *(u16 *) info->buffer.pointer;
305d38ceaf9SAlex Deucher 	if (size < 10) {
306d38ceaf9SAlex Deucher 		err = -EINVAL;
307d38ceaf9SAlex Deucher 		goto out;
308d38ceaf9SAlex Deucher 	}
309d38ceaf9SAlex Deucher 
310d38ceaf9SAlex Deucher 	memset(&params, 0, sizeof(params));
311d38ceaf9SAlex Deucher 	size = min(sizeof(params), size);
312d38ceaf9SAlex Deucher 	memcpy(&params, info->buffer.pointer, size);
313d38ceaf9SAlex Deucher 
314d38ceaf9SAlex Deucher 	DRM_DEBUG_DRIVER("SYSTEM_PARAMS: mask = %#x, flags = %#x\n",
315d38ceaf9SAlex Deucher 			params.flags, params.valid_mask);
316d38ceaf9SAlex Deucher 	params.flags = params.flags & params.valid_mask;
317d38ceaf9SAlex Deucher 
318d38ceaf9SAlex Deucher 	if ((params.flags & ATIF_NOTIFY_MASK) == ATIF_NOTIFY_NONE) {
319d38ceaf9SAlex Deucher 		n->enabled = false;
320d38ceaf9SAlex Deucher 		n->command_code = 0;
321d38ceaf9SAlex Deucher 	} else if ((params.flags & ATIF_NOTIFY_MASK) == ATIF_NOTIFY_81) {
322d38ceaf9SAlex Deucher 		n->enabled = true;
323d38ceaf9SAlex Deucher 		n->command_code = 0x81;
324d38ceaf9SAlex Deucher 	} else {
325d38ceaf9SAlex Deucher 		if (size < 11) {
326d38ceaf9SAlex Deucher 			err = -EINVAL;
327d38ceaf9SAlex Deucher 			goto out;
328d38ceaf9SAlex Deucher 		}
329d38ceaf9SAlex Deucher 		n->enabled = true;
330d38ceaf9SAlex Deucher 		n->command_code = params.command_code;
331d38ceaf9SAlex Deucher 	}
332d38ceaf9SAlex Deucher 
333d38ceaf9SAlex Deucher out:
334d38ceaf9SAlex Deucher 	DRM_DEBUG_DRIVER("Notification %s, command code = %#x\n",
335d38ceaf9SAlex Deucher 			(n->enabled ? "enabled" : "disabled"),
336d38ceaf9SAlex Deucher 			n->command_code);
337d38ceaf9SAlex Deucher 	kfree(info);
338d38ceaf9SAlex Deucher 	return err;
339d38ceaf9SAlex Deucher }
340d38ceaf9SAlex Deucher 
341d38ceaf9SAlex Deucher /**
342206bbafeSDavid Francis  * amdgpu_atif_query_backlight_caps - get min and max backlight input signal
343206bbafeSDavid Francis  *
3441fdbbc12SFabio M. De Francesco  * @atif: acpi handle
345206bbafeSDavid Francis  *
346206bbafeSDavid Francis  * Execute the QUERY_BRIGHTNESS_TRANSFER_CHARACTERISTICS ATIF function
347206bbafeSDavid Francis  * to determine the acceptable range of backlight values
348206bbafeSDavid Francis  *
349206bbafeSDavid Francis  * Backlight_caps.caps_valid will be set to true if the query is successful
350206bbafeSDavid Francis  *
351206bbafeSDavid Francis  * The input signals are in range 0-255
352206bbafeSDavid Francis  *
353206bbafeSDavid Francis  * This function assumes the display with backlight is the first LCD
354206bbafeSDavid Francis  *
355206bbafeSDavid Francis  * Returns 0 on success, error on failure.
356206bbafeSDavid Francis  */
amdgpu_atif_query_backlight_caps(struct amdgpu_atif * atif)357206bbafeSDavid Francis static int amdgpu_atif_query_backlight_caps(struct amdgpu_atif *atif)
358206bbafeSDavid Francis {
359206bbafeSDavid Francis 	union acpi_object *info;
360206bbafeSDavid Francis 	struct atif_qbtc_output characteristics;
361206bbafeSDavid Francis 	struct atif_qbtc_arguments arguments;
362206bbafeSDavid Francis 	struct acpi_buffer params;
363206bbafeSDavid Francis 	size_t size;
364206bbafeSDavid Francis 	int err = 0;
365206bbafeSDavid Francis 
366206bbafeSDavid Francis 	arguments.size = sizeof(arguments);
367206bbafeSDavid Francis 	arguments.requested_display = ATIF_QBTC_REQUEST_LCD1;
368206bbafeSDavid Francis 
369206bbafeSDavid Francis 	params.length = sizeof(arguments);
370206bbafeSDavid Francis 	params.pointer = (void *)&arguments;
371206bbafeSDavid Francis 
372206bbafeSDavid Francis 	info = amdgpu_atif_call(atif,
373206bbafeSDavid Francis 		ATIF_FUNCTION_QUERY_BRIGHTNESS_TRANSFER_CHARACTERISTICS,
374206bbafeSDavid Francis 		&params);
375206bbafeSDavid Francis 	if (!info) {
376206bbafeSDavid Francis 		err = -EIO;
377206bbafeSDavid Francis 		goto out;
378206bbafeSDavid Francis 	}
379206bbafeSDavid Francis 
380206bbafeSDavid Francis 	size = *(u16 *) info->buffer.pointer;
381206bbafeSDavid Francis 	if (size < 10) {
382206bbafeSDavid Francis 		err = -EINVAL;
383206bbafeSDavid Francis 		goto out;
384206bbafeSDavid Francis 	}
385206bbafeSDavid Francis 
386206bbafeSDavid Francis 	memset(&characteristics, 0, sizeof(characteristics));
387206bbafeSDavid Francis 	size = min(sizeof(characteristics), size);
388206bbafeSDavid Francis 	memcpy(&characteristics, info->buffer.pointer, size);
389206bbafeSDavid Francis 
390206bbafeSDavid Francis 	atif->backlight_caps.caps_valid = true;
391206bbafeSDavid Francis 	atif->backlight_caps.min_input_signal =
392206bbafeSDavid Francis 			characteristics.min_input_signal;
393206bbafeSDavid Francis 	atif->backlight_caps.max_input_signal =
394206bbafeSDavid Francis 			characteristics.max_input_signal;
3952fe87f54SMario Limonciello 	atif->backlight_caps.ac_level = characteristics.ac_level;
3962fe87f54SMario Limonciello 	atif->backlight_caps.dc_level = characteristics.dc_level;
397*f729e637SMario Limonciello 	atif->backlight_caps.data_points = characteristics.number_of_points;
398*f729e637SMario Limonciello 	memcpy(atif->backlight_caps.luminance_data,
399*f729e637SMario Limonciello 	       characteristics.data_points,
400*f729e637SMario Limonciello 	       sizeof(atif->backlight_caps.luminance_data));
401206bbafeSDavid Francis out:
402206bbafeSDavid Francis 	kfree(info);
403206bbafeSDavid Francis 	return err;
404206bbafeSDavid Francis }
405206bbafeSDavid Francis 
406206bbafeSDavid Francis /**
407d38ceaf9SAlex Deucher  * amdgpu_atif_get_sbios_requests - get requested sbios event
408d38ceaf9SAlex Deucher  *
4091fdbbc12SFabio M. De Francesco  * @atif: acpi handle
410d38ceaf9SAlex Deucher  * @req: atif sbios request struct
411d38ceaf9SAlex Deucher  *
412d38ceaf9SAlex Deucher  * Execute the ATIF_FUNCTION_GET_SYSTEM_BIOS_REQUESTS ATIF function
413d38ceaf9SAlex Deucher  * to determine what requests the sbios is making to the driver
414d38ceaf9SAlex Deucher  * (all asics).
415d38ceaf9SAlex Deucher  * Returns 0 on success, error on failure.
416d38ceaf9SAlex Deucher  */
amdgpu_atif_get_sbios_requests(struct amdgpu_atif * atif,struct atif_sbios_requests * req)417280cf1a9SLyude Paul static int amdgpu_atif_get_sbios_requests(struct amdgpu_atif *atif,
418d38ceaf9SAlex Deucher 					  struct atif_sbios_requests *req)
419d38ceaf9SAlex Deucher {
420d38ceaf9SAlex Deucher 	union acpi_object *info;
421d38ceaf9SAlex Deucher 	size_t size;
422d38ceaf9SAlex Deucher 	int count = 0;
423d38ceaf9SAlex Deucher 
424280cf1a9SLyude Paul 	info = amdgpu_atif_call(atif, ATIF_FUNCTION_GET_SYSTEM_BIOS_REQUESTS,
425280cf1a9SLyude Paul 				NULL);
426d38ceaf9SAlex Deucher 	if (!info)
427d38ceaf9SAlex Deucher 		return -EIO;
428d38ceaf9SAlex Deucher 
429d38ceaf9SAlex Deucher 	size = *(u16 *)info->buffer.pointer;
430d38ceaf9SAlex Deucher 	if (size < 0xd) {
431d38ceaf9SAlex Deucher 		count = -EINVAL;
432d38ceaf9SAlex Deucher 		goto out;
433d38ceaf9SAlex Deucher 	}
434d38ceaf9SAlex Deucher 	memset(req, 0, sizeof(*req));
435d38ceaf9SAlex Deucher 
436d38ceaf9SAlex Deucher 	size = min(sizeof(*req), size);
437d38ceaf9SAlex Deucher 	memcpy(req, info->buffer.pointer, size);
438d38ceaf9SAlex Deucher 	DRM_DEBUG_DRIVER("SBIOS pending requests: %#x\n", req->pending);
439d38ceaf9SAlex Deucher 
440d38ceaf9SAlex Deucher 	count = hweight32(req->pending);
441d38ceaf9SAlex Deucher 
442d38ceaf9SAlex Deucher out:
443d38ceaf9SAlex Deucher 	kfree(info);
444d38ceaf9SAlex Deucher 	return count;
445d38ceaf9SAlex Deucher }
446d38ceaf9SAlex Deucher 
447d38ceaf9SAlex Deucher /**
448d38ceaf9SAlex Deucher  * amdgpu_atif_handler - handle ATIF notify requests
449d38ceaf9SAlex Deucher  *
450d38ceaf9SAlex Deucher  * @adev: amdgpu_device pointer
451d38ceaf9SAlex Deucher  * @event: atif sbios request struct
452d38ceaf9SAlex Deucher  *
453d38ceaf9SAlex Deucher  * Checks the acpi event and if it matches an atif event,
454d38ceaf9SAlex Deucher  * handles it.
455582f58deSLyude Paul  *
456582f58deSLyude Paul  * Returns:
457582f58deSLyude Paul  * NOTIFY_BAD or NOTIFY_DONE, depending on the event.
458d38ceaf9SAlex Deucher  */
amdgpu_atif_handler(struct amdgpu_device * adev,struct acpi_bus_event * event)459b7cecbe8SJean Delvare static int amdgpu_atif_handler(struct amdgpu_device *adev,
460d38ceaf9SAlex Deucher 			       struct acpi_bus_event *event)
461d38ceaf9SAlex Deucher {
462f9b7f370SAlex Deucher 	struct amdgpu_atif *atif = &amdgpu_acpi_priv.atif;
463d38ceaf9SAlex Deucher 	int count;
464d38ceaf9SAlex Deucher 
465d38ceaf9SAlex Deucher 	DRM_DEBUG_DRIVER("event, device_class = %s, type = %#x\n",
466d38ceaf9SAlex Deucher 			event->device_class, event->type);
467d38ceaf9SAlex Deucher 
468d38ceaf9SAlex Deucher 	if (strcmp(event->device_class, ACPI_VIDEO_CLASS) != 0)
469d38ceaf9SAlex Deucher 		return NOTIFY_DONE;
470d38ceaf9SAlex Deucher 
471582f58deSLyude Paul 	/* Is this actually our event? */
472f9b7f370SAlex Deucher 	if (!atif->notification_cfg.enabled ||
473582f58deSLyude Paul 	    event->type != atif->notification_cfg.command_code) {
474582f58deSLyude Paul 		/* These events will generate keypresses otherwise */
475582f58deSLyude Paul 		if (event->type == ACPI_VIDEO_NOTIFY_PROBE)
476582f58deSLyude Paul 			return NOTIFY_BAD;
477582f58deSLyude Paul 		else
478d38ceaf9SAlex Deucher 			return NOTIFY_DONE;
479582f58deSLyude Paul 	}
480d38ceaf9SAlex Deucher 
4819e7204beSAlex Deucher 	if (atif->functions.sbios_requests) {
4829e7204beSAlex Deucher 		struct atif_sbios_requests req;
4839e7204beSAlex Deucher 
484d38ceaf9SAlex Deucher 		/* Check pending SBIOS requests */
485280cf1a9SLyude Paul 		count = amdgpu_atif_get_sbios_requests(atif, &req);
486d38ceaf9SAlex Deucher 
487d38ceaf9SAlex Deucher 		if (count <= 0)
488582f58deSLyude Paul 			return NOTIFY_BAD;
489d38ceaf9SAlex Deucher 
490d38ceaf9SAlex Deucher 		DRM_DEBUG_DRIVER("ATIF: %d pending SBIOS requests\n", count);
491d38ceaf9SAlex Deucher 
49297d798b2SAlex Deucher 		if (req.pending & ATIF_PANEL_BRIGHTNESS_CHANGE_REQUEST) {
49397d798b2SAlex Deucher 			if (atif->bd) {
494d38ceaf9SAlex Deucher 				DRM_DEBUG_DRIVER("Changing brightness to %d\n",
495d38ceaf9SAlex Deucher 						 req.backlight_level);
496bcb7b0efSAndriy Gapon 				/*
497bcb7b0efSAndriy Gapon 				 * XXX backlight_device_set_brightness() is
498bcb7b0efSAndriy Gapon 				 * hardwired to post BACKLIGHT_UPDATE_SYSFS.
499bcb7b0efSAndriy Gapon 				 * It probably should accept 'reason' parameter.
500bcb7b0efSAndriy Gapon 				 */
50197d798b2SAlex Deucher 				backlight_device_set_brightness(atif->bd, req.backlight_level);
502bcb7b0efSAndriy Gapon 			}
50397d798b2SAlex Deucher 		}
50497d798b2SAlex Deucher 
5051b0f568dSAlex Deucher 		if (req.pending & ATIF_DGPU_DISPLAY_EVENT) {
506bdb1ccb0SAaron Liu 			if (adev->flags & AMD_IS_PX) {
5074a580877SLuben Tuikov 				pm_runtime_get_sync(adev_to_drm(adev)->dev);
5081b0f568dSAlex Deucher 				/* Just fire off a uevent and let userspace tell us what to do */
5094a580877SLuben Tuikov 				drm_helper_hpd_irq_event(adev_to_drm(adev));
5104a580877SLuben Tuikov 				pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
5114a580877SLuben Tuikov 				pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
5121b0f568dSAlex Deucher 			}
5131b0f568dSAlex Deucher 		}
514d38ceaf9SAlex Deucher 		/* TODO: check other events */
5159e7204beSAlex Deucher 	}
516d38ceaf9SAlex Deucher 
517d38ceaf9SAlex Deucher 	/* We've handled the event, stop the notifier chain. The ACPI interface
518d38ceaf9SAlex Deucher 	 * overloads ACPI_VIDEO_NOTIFY_PROBE, we don't want to send that to
519d38ceaf9SAlex Deucher 	 * userspace if the event was generated only to signal a SBIOS
520d38ceaf9SAlex Deucher 	 * request.
521d38ceaf9SAlex Deucher 	 */
522d38ceaf9SAlex Deucher 	return NOTIFY_BAD;
523d38ceaf9SAlex Deucher }
524d38ceaf9SAlex Deucher 
525d38ceaf9SAlex Deucher /* Call the ATCS method
526d38ceaf9SAlex Deucher  */
527d38ceaf9SAlex Deucher /**
528d38ceaf9SAlex Deucher  * amdgpu_atcs_call - call an ATCS method
529d38ceaf9SAlex Deucher  *
53077bf762fSAlex Deucher  * @atcs: atcs structure
531d38ceaf9SAlex Deucher  * @function: the ATCS function to execute
532d38ceaf9SAlex Deucher  * @params: ATCS function params
533d38ceaf9SAlex Deucher  *
534d38ceaf9SAlex Deucher  * Executes the requested ATCS function (all asics).
535d38ceaf9SAlex Deucher  * Returns a pointer to the acpi output buffer.
536d38ceaf9SAlex Deucher  */
amdgpu_atcs_call(struct amdgpu_atcs * atcs,int function,struct acpi_buffer * params)53777bf762fSAlex Deucher static union acpi_object *amdgpu_atcs_call(struct amdgpu_atcs *atcs,
53877bf762fSAlex Deucher 					   int function,
539d38ceaf9SAlex Deucher 					   struct acpi_buffer *params)
540d38ceaf9SAlex Deucher {
541d38ceaf9SAlex Deucher 	acpi_status status;
542d38ceaf9SAlex Deucher 	union acpi_object atcs_arg_elements[2];
543d38ceaf9SAlex Deucher 	struct acpi_object_list atcs_arg;
544d38ceaf9SAlex Deucher 	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
545d38ceaf9SAlex Deucher 
546d38ceaf9SAlex Deucher 	atcs_arg.count = 2;
547d38ceaf9SAlex Deucher 	atcs_arg.pointer = &atcs_arg_elements[0];
548d38ceaf9SAlex Deucher 
549d38ceaf9SAlex Deucher 	atcs_arg_elements[0].type = ACPI_TYPE_INTEGER;
550d38ceaf9SAlex Deucher 	atcs_arg_elements[0].integer.value = function;
551d38ceaf9SAlex Deucher 
552d38ceaf9SAlex Deucher 	if (params) {
553d38ceaf9SAlex Deucher 		atcs_arg_elements[1].type = ACPI_TYPE_BUFFER;
554d38ceaf9SAlex Deucher 		atcs_arg_elements[1].buffer.length = params->length;
555d38ceaf9SAlex Deucher 		atcs_arg_elements[1].buffer.pointer = params->pointer;
556d38ceaf9SAlex Deucher 	} else {
557d38ceaf9SAlex Deucher 		/* We need a second fake parameter */
558d38ceaf9SAlex Deucher 		atcs_arg_elements[1].type = ACPI_TYPE_INTEGER;
559d38ceaf9SAlex Deucher 		atcs_arg_elements[1].integer.value = 0;
560d38ceaf9SAlex Deucher 	}
561d38ceaf9SAlex Deucher 
5624965257fSAlex Deucher 	status = acpi_evaluate_object(atcs->handle, NULL, &atcs_arg, &buffer);
563d38ceaf9SAlex Deucher 
564d38ceaf9SAlex Deucher 	/* Fail only if calling the method fails and ATIF is supported */
565d38ceaf9SAlex Deucher 	if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
566d38ceaf9SAlex Deucher 		DRM_DEBUG_DRIVER("failed to evaluate ATCS got %s\n",
567d38ceaf9SAlex Deucher 				 acpi_format_exception(status));
568d38ceaf9SAlex Deucher 		kfree(buffer.pointer);
569d38ceaf9SAlex Deucher 		return NULL;
570d38ceaf9SAlex Deucher 	}
571d38ceaf9SAlex Deucher 
572d38ceaf9SAlex Deucher 	return buffer.pointer;
573d38ceaf9SAlex Deucher }
574d38ceaf9SAlex Deucher 
575d38ceaf9SAlex Deucher /**
576d38ceaf9SAlex Deucher  * amdgpu_atcs_parse_functions - parse supported functions
577d38ceaf9SAlex Deucher  *
578d38ceaf9SAlex Deucher  * @f: supported functions struct
579d38ceaf9SAlex Deucher  * @mask: supported functions mask from ATCS
580d38ceaf9SAlex Deucher  *
581d38ceaf9SAlex Deucher  * Use the supported functions mask from ATCS function
582d38ceaf9SAlex Deucher  * ATCS_FUNCTION_VERIFY_INTERFACE to determine what functions
583d38ceaf9SAlex Deucher  * are supported (all asics).
584d38ceaf9SAlex Deucher  */
amdgpu_atcs_parse_functions(struct amdgpu_atcs_functions * f,u32 mask)585d38ceaf9SAlex Deucher static void amdgpu_atcs_parse_functions(struct amdgpu_atcs_functions *f, u32 mask)
586d38ceaf9SAlex Deucher {
587d38ceaf9SAlex Deucher 	f->get_ext_state = mask & ATCS_GET_EXTERNAL_STATE_SUPPORTED;
588d38ceaf9SAlex Deucher 	f->pcie_perf_req = mask & ATCS_PCIE_PERFORMANCE_REQUEST_SUPPORTED;
589d38ceaf9SAlex Deucher 	f->pcie_dev_rdy = mask & ATCS_PCIE_DEVICE_READY_NOTIFICATION_SUPPORTED;
590d38ceaf9SAlex Deucher 	f->pcie_bus_width = mask & ATCS_SET_PCIE_BUS_WIDTH_SUPPORTED;
59116eb48c6SSathishkumar S 	f->power_shift_control = mask & ATCS_SET_POWER_SHIFT_CONTROL_SUPPORTED;
592d38ceaf9SAlex Deucher }
593d38ceaf9SAlex Deucher 
594d38ceaf9SAlex Deucher /**
595d38ceaf9SAlex Deucher  * amdgpu_atcs_verify_interface - verify ATCS
596d38ceaf9SAlex Deucher  *
597d38ceaf9SAlex Deucher  * @atcs: amdgpu atcs struct
598d38ceaf9SAlex Deucher  *
599d38ceaf9SAlex Deucher  * Execute the ATCS_FUNCTION_VERIFY_INTERFACE ATCS function
600d38ceaf9SAlex Deucher  * to initialize ATCS and determine what features are supported
601d38ceaf9SAlex Deucher  * (all asics).
602d38ceaf9SAlex Deucher  * returns 0 on success, error on failure.
603d38ceaf9SAlex Deucher  */
amdgpu_atcs_verify_interface(struct amdgpu_atcs * atcs)60477bf762fSAlex Deucher static int amdgpu_atcs_verify_interface(struct amdgpu_atcs *atcs)
605d38ceaf9SAlex Deucher {
606d38ceaf9SAlex Deucher 	union acpi_object *info;
607d38ceaf9SAlex Deucher 	struct atcs_verify_interface output;
608d38ceaf9SAlex Deucher 	size_t size;
609d38ceaf9SAlex Deucher 	int err = 0;
610d38ceaf9SAlex Deucher 
61177bf762fSAlex Deucher 	info = amdgpu_atcs_call(atcs, ATCS_FUNCTION_VERIFY_INTERFACE, NULL);
612d38ceaf9SAlex Deucher 	if (!info)
613d38ceaf9SAlex Deucher 		return -EIO;
614d38ceaf9SAlex Deucher 
615d38ceaf9SAlex Deucher 	memset(&output, 0, sizeof(output));
616d38ceaf9SAlex Deucher 
617d38ceaf9SAlex Deucher 	size = *(u16 *) info->buffer.pointer;
618d38ceaf9SAlex Deucher 	if (size < 8) {
619d38ceaf9SAlex Deucher 		DRM_INFO("ATCS buffer is too small: %zu\n", size);
620d38ceaf9SAlex Deucher 		err = -EINVAL;
621d38ceaf9SAlex Deucher 		goto out;
622d38ceaf9SAlex Deucher 	}
623d38ceaf9SAlex Deucher 	size = min(sizeof(output), size);
624d38ceaf9SAlex Deucher 
625d38ceaf9SAlex Deucher 	memcpy(&output, info->buffer.pointer, size);
626d38ceaf9SAlex Deucher 
627d38ceaf9SAlex Deucher 	/* TODO: check version? */
628d38ceaf9SAlex Deucher 	DRM_DEBUG_DRIVER("ATCS version %u\n", output.version);
629d38ceaf9SAlex Deucher 
630d38ceaf9SAlex Deucher 	amdgpu_atcs_parse_functions(&atcs->functions, output.function_bits);
631d38ceaf9SAlex Deucher 
632d38ceaf9SAlex Deucher out:
633d38ceaf9SAlex Deucher 	kfree(info);
634d38ceaf9SAlex Deucher 	return err;
635d38ceaf9SAlex Deucher }
636d38ceaf9SAlex Deucher 
637d38ceaf9SAlex Deucher /**
638d38ceaf9SAlex Deucher  * amdgpu_acpi_is_pcie_performance_request_supported
639d38ceaf9SAlex Deucher  *
640d38ceaf9SAlex Deucher  * @adev: amdgpu_device pointer
641d38ceaf9SAlex Deucher  *
642d38ceaf9SAlex Deucher  * Check if the ATCS pcie_perf_req and pcie_dev_rdy methods
643d38ceaf9SAlex Deucher  * are supported (all asics).
644d38ceaf9SAlex Deucher  * returns true if supported, false if not.
645d38ceaf9SAlex Deucher  */
amdgpu_acpi_is_pcie_performance_request_supported(struct amdgpu_device * adev)646d38ceaf9SAlex Deucher bool amdgpu_acpi_is_pcie_performance_request_supported(struct amdgpu_device *adev)
647d38ceaf9SAlex Deucher {
648f9b7f370SAlex Deucher 	struct amdgpu_atcs *atcs = &amdgpu_acpi_priv.atcs;
649d38ceaf9SAlex Deucher 
650d38ceaf9SAlex Deucher 	if (atcs->functions.pcie_perf_req && atcs->functions.pcie_dev_rdy)
651d38ceaf9SAlex Deucher 		return true;
652d38ceaf9SAlex Deucher 
653d38ceaf9SAlex Deucher 	return false;
654d38ceaf9SAlex Deucher }
655d38ceaf9SAlex Deucher 
656d38ceaf9SAlex Deucher /**
65716eb48c6SSathishkumar S  * amdgpu_acpi_is_power_shift_control_supported
65816eb48c6SSathishkumar S  *
65916eb48c6SSathishkumar S  * Check if the ATCS power shift control method
66016eb48c6SSathishkumar S  * is supported.
66116eb48c6SSathishkumar S  * returns true if supported, false if not.
66216eb48c6SSathishkumar S  */
amdgpu_acpi_is_power_shift_control_supported(void)66316eb48c6SSathishkumar S bool amdgpu_acpi_is_power_shift_control_supported(void)
66416eb48c6SSathishkumar S {
66516eb48c6SSathishkumar S 	return amdgpu_acpi_priv.atcs.functions.power_shift_control;
66616eb48c6SSathishkumar S }
66716eb48c6SSathishkumar S 
66816eb48c6SSathishkumar S /**
669d38ceaf9SAlex Deucher  * amdgpu_acpi_pcie_notify_device_ready
670d38ceaf9SAlex Deucher  *
671d38ceaf9SAlex Deucher  * @adev: amdgpu_device pointer
672d38ceaf9SAlex Deucher  *
673d38ceaf9SAlex Deucher  * Executes the PCIE_DEVICE_READY_NOTIFICATION method
674d38ceaf9SAlex Deucher  * (all asics).
675d38ceaf9SAlex Deucher  * returns 0 on success, error on failure.
676d38ceaf9SAlex Deucher  */
amdgpu_acpi_pcie_notify_device_ready(struct amdgpu_device * adev)677d38ceaf9SAlex Deucher int amdgpu_acpi_pcie_notify_device_ready(struct amdgpu_device *adev)
678d38ceaf9SAlex Deucher {
679d38ceaf9SAlex Deucher 	union acpi_object *info;
680f9b7f370SAlex Deucher 	struct amdgpu_atcs *atcs = &amdgpu_acpi_priv.atcs;
681d38ceaf9SAlex Deucher 
682d38ceaf9SAlex Deucher 	if (!atcs->functions.pcie_dev_rdy)
683d38ceaf9SAlex Deucher 		return -EINVAL;
684d38ceaf9SAlex Deucher 
68577bf762fSAlex Deucher 	info = amdgpu_atcs_call(atcs, ATCS_FUNCTION_PCIE_DEVICE_READY_NOTIFICATION, NULL);
686d38ceaf9SAlex Deucher 	if (!info)
687d38ceaf9SAlex Deucher 		return -EIO;
688d38ceaf9SAlex Deucher 
689d38ceaf9SAlex Deucher 	kfree(info);
690d38ceaf9SAlex Deucher 
691d38ceaf9SAlex Deucher 	return 0;
692d38ceaf9SAlex Deucher }
693d38ceaf9SAlex Deucher 
694d38ceaf9SAlex Deucher /**
695d38ceaf9SAlex Deucher  * amdgpu_acpi_pcie_performance_request
696d38ceaf9SAlex Deucher  *
697d38ceaf9SAlex Deucher  * @adev: amdgpu_device pointer
698d38ceaf9SAlex Deucher  * @perf_req: requested perf level (pcie gen speed)
699d38ceaf9SAlex Deucher  * @advertise: set advertise caps flag if set
700d38ceaf9SAlex Deucher  *
701d38ceaf9SAlex Deucher  * Executes the PCIE_PERFORMANCE_REQUEST method to
702d38ceaf9SAlex Deucher  * change the pcie gen speed (all asics).
703d38ceaf9SAlex Deucher  * returns 0 on success, error on failure.
704d38ceaf9SAlex Deucher  */
amdgpu_acpi_pcie_performance_request(struct amdgpu_device * adev,u8 perf_req,bool advertise)705d38ceaf9SAlex Deucher int amdgpu_acpi_pcie_performance_request(struct amdgpu_device *adev,
706d38ceaf9SAlex Deucher 					 u8 perf_req, bool advertise)
707d38ceaf9SAlex Deucher {
708d38ceaf9SAlex Deucher 	union acpi_object *info;
709f9b7f370SAlex Deucher 	struct amdgpu_atcs *atcs = &amdgpu_acpi_priv.atcs;
710d38ceaf9SAlex Deucher 	struct atcs_pref_req_input atcs_input;
711d38ceaf9SAlex Deucher 	struct atcs_pref_req_output atcs_output;
712d38ceaf9SAlex Deucher 	struct acpi_buffer params;
713d38ceaf9SAlex Deucher 	size_t size;
714d38ceaf9SAlex Deucher 	u32 retry = 3;
715d38ceaf9SAlex Deucher 
71677bf762fSAlex Deucher 	if (amdgpu_acpi_pcie_notify_device_ready(adev))
717d38ceaf9SAlex Deucher 		return -EINVAL;
718d38ceaf9SAlex Deucher 
719d38ceaf9SAlex Deucher 	if (!atcs->functions.pcie_perf_req)
720d38ceaf9SAlex Deucher 		return -EINVAL;
721d38ceaf9SAlex Deucher 
722d38ceaf9SAlex Deucher 	atcs_input.size = sizeof(struct atcs_pref_req_input);
723d38ceaf9SAlex Deucher 	/* client id (bit 2-0: func num, 7-3: dev num, 15-8: bus num) */
724bdacd16aSXiongfeng Wang 	atcs_input.client_id = pci_dev_id(adev->pdev);
725d38ceaf9SAlex Deucher 	atcs_input.valid_flags_mask = ATCS_VALID_FLAGS_MASK;
726d38ceaf9SAlex Deucher 	atcs_input.flags = ATCS_WAIT_FOR_COMPLETION;
727d38ceaf9SAlex Deucher 	if (advertise)
728d38ceaf9SAlex Deucher 		atcs_input.flags |= ATCS_ADVERTISE_CAPS;
729d38ceaf9SAlex Deucher 	atcs_input.req_type = ATCS_PCIE_LINK_SPEED;
730d38ceaf9SAlex Deucher 	atcs_input.perf_req = perf_req;
731d38ceaf9SAlex Deucher 
732d38ceaf9SAlex Deucher 	params.length = sizeof(struct atcs_pref_req_input);
733d38ceaf9SAlex Deucher 	params.pointer = &atcs_input;
734d38ceaf9SAlex Deucher 
735d38ceaf9SAlex Deucher 	while (retry--) {
73677bf762fSAlex Deucher 		info = amdgpu_atcs_call(atcs, ATCS_FUNCTION_PCIE_PERFORMANCE_REQUEST, &params);
737d38ceaf9SAlex Deucher 		if (!info)
738d38ceaf9SAlex Deucher 			return -EIO;
739d38ceaf9SAlex Deucher 
740d38ceaf9SAlex Deucher 		memset(&atcs_output, 0, sizeof(atcs_output));
741d38ceaf9SAlex Deucher 
742d38ceaf9SAlex Deucher 		size = *(u16 *) info->buffer.pointer;
743d38ceaf9SAlex Deucher 		if (size < 3) {
744d38ceaf9SAlex Deucher 			DRM_INFO("ATCS buffer is too small: %zu\n", size);
745d38ceaf9SAlex Deucher 			kfree(info);
746d38ceaf9SAlex Deucher 			return -EINVAL;
747d38ceaf9SAlex Deucher 		}
748d38ceaf9SAlex Deucher 		size = min(sizeof(atcs_output), size);
749d38ceaf9SAlex Deucher 
750d38ceaf9SAlex Deucher 		memcpy(&atcs_output, info->buffer.pointer, size);
751d38ceaf9SAlex Deucher 
752d38ceaf9SAlex Deucher 		kfree(info);
753d38ceaf9SAlex Deucher 
754d38ceaf9SAlex Deucher 		switch (atcs_output.ret_val) {
755d38ceaf9SAlex Deucher 		case ATCS_REQUEST_REFUSED:
756d38ceaf9SAlex Deucher 		default:
757d38ceaf9SAlex Deucher 			return -EINVAL;
758d38ceaf9SAlex Deucher 		case ATCS_REQUEST_COMPLETE:
759d38ceaf9SAlex Deucher 			return 0;
760d38ceaf9SAlex Deucher 		case ATCS_REQUEST_IN_PROGRESS:
761d38ceaf9SAlex Deucher 			udelay(10);
762d38ceaf9SAlex Deucher 			break;
763d38ceaf9SAlex Deucher 		}
764d38ceaf9SAlex Deucher 	}
765d38ceaf9SAlex Deucher 
766d38ceaf9SAlex Deucher 	return 0;
767d38ceaf9SAlex Deucher }
768d38ceaf9SAlex Deucher 
769d38ceaf9SAlex Deucher /**
77016eb48c6SSathishkumar S  * amdgpu_acpi_power_shift_control
77116eb48c6SSathishkumar S  *
77216eb48c6SSathishkumar S  * @adev: amdgpu_device pointer
77316eb48c6SSathishkumar S  * @dev_state: device acpi state
77416eb48c6SSathishkumar S  * @drv_state: driver state
77516eb48c6SSathishkumar S  *
77616eb48c6SSathishkumar S  * Executes the POWER_SHIFT_CONTROL method to
77716eb48c6SSathishkumar S  * communicate current dGPU device state and
77816eb48c6SSathishkumar S  * driver state to APU/SBIOS.
77916eb48c6SSathishkumar S  * returns 0 on success, error on failure.
78016eb48c6SSathishkumar S  */
amdgpu_acpi_power_shift_control(struct amdgpu_device * adev,u8 dev_state,bool drv_state)78116eb48c6SSathishkumar S int amdgpu_acpi_power_shift_control(struct amdgpu_device *adev,
78216eb48c6SSathishkumar S 				    u8 dev_state, bool drv_state)
78316eb48c6SSathishkumar S {
78416eb48c6SSathishkumar S 	union acpi_object *info;
78516eb48c6SSathishkumar S 	struct amdgpu_atcs *atcs = &amdgpu_acpi_priv.atcs;
78616eb48c6SSathishkumar S 	struct atcs_pwr_shift_input atcs_input;
78716eb48c6SSathishkumar S 	struct acpi_buffer params;
78816eb48c6SSathishkumar S 
78916eb48c6SSathishkumar S 	if (!amdgpu_acpi_is_power_shift_control_supported())
79016eb48c6SSathishkumar S 		return -EINVAL;
79116eb48c6SSathishkumar S 
79216eb48c6SSathishkumar S 	atcs_input.size = sizeof(struct atcs_pwr_shift_input);
79316eb48c6SSathishkumar S 	/* dGPU id (bit 2-0: func num, 7-3: dev num, 15-8: bus num) */
794bdacd16aSXiongfeng Wang 	atcs_input.dgpu_id = pci_dev_id(adev->pdev);
79516eb48c6SSathishkumar S 	atcs_input.dev_acpi_state = dev_state;
79616eb48c6SSathishkumar S 	atcs_input.drv_state = drv_state;
79716eb48c6SSathishkumar S 
79816eb48c6SSathishkumar S 	params.length = sizeof(struct atcs_pwr_shift_input);
79916eb48c6SSathishkumar S 	params.pointer = &atcs_input;
80016eb48c6SSathishkumar S 
80116eb48c6SSathishkumar S 	info = amdgpu_atcs_call(atcs, ATCS_FUNCTION_POWER_SHIFT_CONTROL, &params);
80216eb48c6SSathishkumar S 	if (!info) {
80316eb48c6SSathishkumar S 		DRM_ERROR("ATCS PSC update failed\n");
80416eb48c6SSathishkumar S 		return -EIO;
80516eb48c6SSathishkumar S 	}
80616eb48c6SSathishkumar S 
80732e7ee29SPrike Liang 	kfree(info);
80816eb48c6SSathishkumar S 	return 0;
80916eb48c6SSathishkumar S }
81016eb48c6SSathishkumar S 
81116eb48c6SSathishkumar S /**
8123fa8f89dSSathishkumar S  * amdgpu_acpi_smart_shift_update - update dGPU device state to SBIOS
8133fa8f89dSSathishkumar S  *
8143fa8f89dSSathishkumar S  * @dev: drm_device pointer
8153fa8f89dSSathishkumar S  * @ss_state: current smart shift event
8163fa8f89dSSathishkumar S  *
8173fa8f89dSSathishkumar S  * returns 0 on success,
8183fa8f89dSSathishkumar S  * otherwise return error number.
8193fa8f89dSSathishkumar S  */
amdgpu_acpi_smart_shift_update(struct drm_device * dev,enum amdgpu_ss ss_state)8203fa8f89dSSathishkumar S int amdgpu_acpi_smart_shift_update(struct drm_device *dev, enum amdgpu_ss ss_state)
8213fa8f89dSSathishkumar S {
8223fa8f89dSSathishkumar S 	struct amdgpu_device *adev = drm_to_adev(dev);
8233fa8f89dSSathishkumar S 	int r;
8243fa8f89dSSathishkumar S 
8253fa8f89dSSathishkumar S 	if (!amdgpu_device_supports_smart_shift(dev))
8263fa8f89dSSathishkumar S 		return 0;
8273fa8f89dSSathishkumar S 
8283fa8f89dSSathishkumar S 	switch (ss_state) {
8293fa8f89dSSathishkumar S 	/* SBIOS trigger “stop”, “enable” and “start” at D0, Driver Operational.
8303fa8f89dSSathishkumar S 	 * SBIOS trigger “stop” at D3, Driver Not Operational.
8313fa8f89dSSathishkumar S 	 * SBIOS trigger “stop” and “disable” at D0, Driver NOT operational.
8323fa8f89dSSathishkumar S 	 */
8333fa8f89dSSathishkumar S 	case AMDGPU_SS_DRV_LOAD:
8343fa8f89dSSathishkumar S 		r = amdgpu_acpi_power_shift_control(adev,
8353fa8f89dSSathishkumar S 						    AMDGPU_ATCS_PSC_DEV_STATE_D0,
8363fa8f89dSSathishkumar S 						    AMDGPU_ATCS_PSC_DRV_STATE_OPR);
8373fa8f89dSSathishkumar S 		break;
8383fa8f89dSSathishkumar S 	case AMDGPU_SS_DEV_D0:
8393fa8f89dSSathishkumar S 		r = amdgpu_acpi_power_shift_control(adev,
8403fa8f89dSSathishkumar S 						    AMDGPU_ATCS_PSC_DEV_STATE_D0,
8413fa8f89dSSathishkumar S 						    AMDGPU_ATCS_PSC_DRV_STATE_OPR);
8423fa8f89dSSathishkumar S 		break;
8433fa8f89dSSathishkumar S 	case AMDGPU_SS_DEV_D3:
8443fa8f89dSSathishkumar S 		r = amdgpu_acpi_power_shift_control(adev,
8453fa8f89dSSathishkumar S 						    AMDGPU_ATCS_PSC_DEV_STATE_D3_HOT,
8463fa8f89dSSathishkumar S 						    AMDGPU_ATCS_PSC_DRV_STATE_NOT_OPR);
8473fa8f89dSSathishkumar S 		break;
8483fa8f89dSSathishkumar S 	case AMDGPU_SS_DRV_UNLOAD:
8493fa8f89dSSathishkumar S 		r = amdgpu_acpi_power_shift_control(adev,
8503fa8f89dSSathishkumar S 						    AMDGPU_ATCS_PSC_DEV_STATE_D0,
8513fa8f89dSSathishkumar S 						    AMDGPU_ATCS_PSC_DRV_STATE_NOT_OPR);
8523fa8f89dSSathishkumar S 		break;
8533fa8f89dSSathishkumar S 	default:
8543fa8f89dSSathishkumar S 		return -EINVAL;
8553fa8f89dSSathishkumar S 	}
8563fa8f89dSSathishkumar S 
8573fa8f89dSSathishkumar S 	return r;
8583fa8f89dSSathishkumar S }
8593fa8f89dSSathishkumar S 
86040e39d72SSrinivasan Shanmugam #ifdef CONFIG_ACPI_NUMA
amdgpu_acpi_get_numa_size(int nid)8611cc82301SLijo Lazar static inline uint64_t amdgpu_acpi_get_numa_size(int nid)
8621cc82301SLijo Lazar {
8631cc82301SLijo Lazar 	/* This is directly using si_meminfo_node implementation as the
8641cc82301SLijo Lazar 	 * function is not exported.
8651cc82301SLijo Lazar 	 */
8661cc82301SLijo Lazar 	int zone_type;
8671cc82301SLijo Lazar 	uint64_t managed_pages = 0;
8681cc82301SLijo Lazar 
8691cc82301SLijo Lazar 	pg_data_t *pgdat = NODE_DATA(nid);
8701cc82301SLijo Lazar 
8711cc82301SLijo Lazar 	for (zone_type = 0; zone_type < MAX_NR_ZONES; zone_type++)
8721cc82301SLijo Lazar 		managed_pages +=
8731cc82301SLijo Lazar 			zone_managed_pages(&pgdat->node_zones[zone_type]);
8741cc82301SLijo Lazar 	return managed_pages * PAGE_SIZE;
8751cc82301SLijo Lazar }
8761cc82301SLijo Lazar 
amdgpu_acpi_get_numa_info(uint32_t pxm)8771cc82301SLijo Lazar static struct amdgpu_numa_info *amdgpu_acpi_get_numa_info(uint32_t pxm)
8781cc82301SLijo Lazar {
8791cc82301SLijo Lazar 	struct amdgpu_numa_info *numa_info;
8801cc82301SLijo Lazar 	int nid;
8811cc82301SLijo Lazar 
8821cc82301SLijo Lazar 	numa_info = xa_load(&numa_info_xa, pxm);
8831cc82301SLijo Lazar 
8841cc82301SLijo Lazar 	if (!numa_info) {
8851cc82301SLijo Lazar 		struct sysinfo info;
8861cc82301SLijo Lazar 
8877db36fe9SSrinivasan Shanmugam 		numa_info = kzalloc(sizeof(*numa_info), GFP_KERNEL);
8881cc82301SLijo Lazar 		if (!numa_info)
8891cc82301SLijo Lazar 			return NULL;
8901cc82301SLijo Lazar 
8911cc82301SLijo Lazar 		nid = pxm_to_node(pxm);
8921cc82301SLijo Lazar 		numa_info->pxm = pxm;
8931cc82301SLijo Lazar 		numa_info->nid = nid;
8941cc82301SLijo Lazar 
8951cc82301SLijo Lazar 		if (numa_info->nid == NUMA_NO_NODE) {
8961cc82301SLijo Lazar 			si_meminfo(&info);
8971cc82301SLijo Lazar 			numa_info->size = info.totalram * info.mem_unit;
8981cc82301SLijo Lazar 		} else {
8991cc82301SLijo Lazar 			numa_info->size = amdgpu_acpi_get_numa_size(nid);
9001cc82301SLijo Lazar 		}
9011cc82301SLijo Lazar 		xa_store(&numa_info_xa, numa_info->pxm, numa_info, GFP_KERNEL);
9021cc82301SLijo Lazar 	}
9031cc82301SLijo Lazar 
9041cc82301SLijo Lazar 	return numa_info;
9051cc82301SLijo Lazar }
90640e39d72SSrinivasan Shanmugam #endif
9071cc82301SLijo Lazar 
9083fa8f89dSSathishkumar S /**
9094d5275abSLijo Lazar  * amdgpu_acpi_get_node_id - obtain the NUMA node id for corresponding amdgpu
9104d5275abSLijo Lazar  * acpi device handle
9114d5275abSLijo Lazar  *
9124d5275abSLijo Lazar  * @handle: acpi handle
91366dadf1aSSrinivasan Shanmugam  * @numa_info: amdgpu_numa_info structure holding numa information
9144d5275abSLijo Lazar  *
9154d5275abSLijo Lazar  * Queries the ACPI interface to fetch the corresponding NUMA Node ID for a
9164d5275abSLijo Lazar  * given amdgpu acpi device.
9174d5275abSLijo Lazar  *
9184d5275abSLijo Lazar  * Returns ACPI STATUS OK with Node ID on success or the corresponding failure reason
9194d5275abSLijo Lazar  */
amdgpu_acpi_get_node_id(acpi_handle handle,struct amdgpu_numa_info ** numa_info)9201501fe94SArnd Bergmann static acpi_status amdgpu_acpi_get_node_id(acpi_handle handle,
9211cc82301SLijo Lazar 				    struct amdgpu_numa_info **numa_info)
9224d5275abSLijo Lazar {
9234d5275abSLijo Lazar #ifdef CONFIG_ACPI_NUMA
9244d5275abSLijo Lazar 	u64 pxm;
9254d5275abSLijo Lazar 	acpi_status status;
9264d5275abSLijo Lazar 
9271cc82301SLijo Lazar 	if (!numa_info)
9281cc82301SLijo Lazar 		return_ACPI_STATUS(AE_ERROR);
9291cc82301SLijo Lazar 
9304d5275abSLijo Lazar 	status = acpi_evaluate_integer(handle, "_PXM", NULL, &pxm);
9314d5275abSLijo Lazar 
9324d5275abSLijo Lazar 	if (ACPI_FAILURE(status))
9334d5275abSLijo Lazar 		return status;
9344d5275abSLijo Lazar 
9351cc82301SLijo Lazar 	*numa_info = amdgpu_acpi_get_numa_info(pxm);
9361cc82301SLijo Lazar 
9371cc82301SLijo Lazar 	if (!*numa_info)
9381cc82301SLijo Lazar 		return_ACPI_STATUS(AE_ERROR);
9394d5275abSLijo Lazar 
9404d5275abSLijo Lazar 	return_ACPI_STATUS(AE_OK);
9414d5275abSLijo Lazar #else
9424d5275abSLijo Lazar 	return_ACPI_STATUS(AE_NOT_EXIST);
9434d5275abSLijo Lazar #endif
9444d5275abSLijo Lazar }
9454d5275abSLijo Lazar 
amdgpu_acpi_get_dev(u32 sbdf)946d055714aSLijo Lazar static struct amdgpu_acpi_dev_info *amdgpu_acpi_get_dev(u32 sbdf)
9474d5275abSLijo Lazar {
9484d5275abSLijo Lazar 	struct amdgpu_acpi_dev_info *acpi_dev;
9494d5275abSLijo Lazar 
9504d5275abSLijo Lazar 	if (list_empty(&amdgpu_acpi_dev_list))
9514d5275abSLijo Lazar 		return NULL;
9524d5275abSLijo Lazar 
9534d5275abSLijo Lazar 	list_for_each_entry(acpi_dev, &amdgpu_acpi_dev_list, list)
954d055714aSLijo Lazar 		if (acpi_dev->sbdf == sbdf)
9554d5275abSLijo Lazar 			return acpi_dev;
9564d5275abSLijo Lazar 
9574d5275abSLijo Lazar 	return NULL;
9584d5275abSLijo Lazar }
9594d5275abSLijo Lazar 
amdgpu_acpi_dev_init(struct amdgpu_acpi_dev_info ** dev_info,struct amdgpu_acpi_xcc_info * xcc_info,u32 sbdf)9604d5275abSLijo Lazar static int amdgpu_acpi_dev_init(struct amdgpu_acpi_dev_info **dev_info,
961d055714aSLijo Lazar 				struct amdgpu_acpi_xcc_info *xcc_info, u32 sbdf)
9624d5275abSLijo Lazar {
9634d5275abSLijo Lazar 	struct amdgpu_acpi_dev_info *tmp;
9644d5275abSLijo Lazar 	union acpi_object *obj;
9654d5275abSLijo Lazar 	int ret = -ENOENT;
9664d5275abSLijo Lazar 
9674d5275abSLijo Lazar 	*dev_info = NULL;
9684d5275abSLijo Lazar 	tmp = kzalloc(sizeof(struct amdgpu_acpi_dev_info), GFP_KERNEL);
9694d5275abSLijo Lazar 	if (!tmp)
9704d5275abSLijo Lazar 		return -ENOMEM;
9714d5275abSLijo Lazar 
9724d5275abSLijo Lazar 	INIT_LIST_HEAD(&tmp->xcc_list);
9734d5275abSLijo Lazar 	INIT_LIST_HEAD(&tmp->list);
974d055714aSLijo Lazar 	tmp->sbdf = sbdf;
9754d5275abSLijo Lazar 
9764d5275abSLijo Lazar 	obj = acpi_evaluate_dsm_typed(xcc_info->handle, &amd_xcc_dsm_guid, 0,
9774d5275abSLijo Lazar 				      AMD_XCC_DSM_GET_SUPP_MODE, NULL,
9784d5275abSLijo Lazar 				      ACPI_TYPE_INTEGER);
9794d5275abSLijo Lazar 
9804d5275abSLijo Lazar 	if (!obj) {
9814d5275abSLijo Lazar 		acpi_handle_debug(xcc_info->handle,
9824d5275abSLijo Lazar 				  "_DSM function %d evaluation failed",
9834d5275abSLijo Lazar 				  AMD_XCC_DSM_GET_SUPP_MODE);
9844d5275abSLijo Lazar 		ret = -ENOENT;
9854d5275abSLijo Lazar 		goto out;
9864d5275abSLijo Lazar 	}
9874d5275abSLijo Lazar 
9884d5275abSLijo Lazar 	tmp->supp_xcp_mode = obj->integer.value & 0xFFFF;
9894d5275abSLijo Lazar 	ACPI_FREE(obj);
9904d5275abSLijo Lazar 
9914d5275abSLijo Lazar 	obj = acpi_evaluate_dsm_typed(xcc_info->handle, &amd_xcc_dsm_guid, 0,
9924d5275abSLijo Lazar 				      AMD_XCC_DSM_GET_XCP_MODE, NULL,
9934d5275abSLijo Lazar 				      ACPI_TYPE_INTEGER);
9944d5275abSLijo Lazar 
9954d5275abSLijo Lazar 	if (!obj) {
9964d5275abSLijo Lazar 		acpi_handle_debug(xcc_info->handle,
9974d5275abSLijo Lazar 				  "_DSM function %d evaluation failed",
9984d5275abSLijo Lazar 				  AMD_XCC_DSM_GET_XCP_MODE);
9994d5275abSLijo Lazar 		ret = -ENOENT;
10004d5275abSLijo Lazar 		goto out;
10014d5275abSLijo Lazar 	}
10024d5275abSLijo Lazar 
10034d5275abSLijo Lazar 	tmp->xcp_mode = obj->integer.value & 0xFFFF;
10044d5275abSLijo Lazar 	tmp->mem_mode = (obj->integer.value >> 32) & 0xFFFF;
10054d5275abSLijo Lazar 	ACPI_FREE(obj);
10064d5275abSLijo Lazar 
10074d5275abSLijo Lazar 	/* Evaluate DSMs and fill XCC information */
10084d5275abSLijo Lazar 	obj = acpi_evaluate_dsm_typed(xcc_info->handle, &amd_xcc_dsm_guid, 0,
10094d5275abSLijo Lazar 				      AMD_XCC_DSM_GET_TMR_INFO, NULL,
10104d5275abSLijo Lazar 				      ACPI_TYPE_PACKAGE);
10114d5275abSLijo Lazar 
10124d5275abSLijo Lazar 	if (!obj || obj->package.count < 2) {
10134d5275abSLijo Lazar 		acpi_handle_debug(xcc_info->handle,
10144d5275abSLijo Lazar 				  "_DSM function %d evaluation failed",
10154d5275abSLijo Lazar 				  AMD_XCC_DSM_GET_TMR_INFO);
10164d5275abSLijo Lazar 		ret = -ENOENT;
10174d5275abSLijo Lazar 		goto out;
10184d5275abSLijo Lazar 	}
10194d5275abSLijo Lazar 
10204d5275abSLijo Lazar 	tmp->tmr_base = obj->package.elements[0].integer.value;
10214d5275abSLijo Lazar 	tmp->tmr_size = obj->package.elements[1].integer.value;
10224d5275abSLijo Lazar 	ACPI_FREE(obj);
10234d5275abSLijo Lazar 
10244d5275abSLijo Lazar 	DRM_DEBUG_DRIVER(
10254d5275abSLijo Lazar 		"New dev(%x): Supported xcp mode: %x curr xcp_mode : %x mem mode : %x, tmr base: %llx tmr size: %llx  ",
1026d055714aSLijo Lazar 		tmp->sbdf, tmp->supp_xcp_mode, tmp->xcp_mode, tmp->mem_mode,
10274d5275abSLijo Lazar 		tmp->tmr_base, tmp->tmr_size);
10284d5275abSLijo Lazar 	list_add_tail(&tmp->list, &amdgpu_acpi_dev_list);
10294d5275abSLijo Lazar 	*dev_info = tmp;
10304d5275abSLijo Lazar 
10314d5275abSLijo Lazar 	return 0;
10324d5275abSLijo Lazar 
10334d5275abSLijo Lazar out:
10344d5275abSLijo Lazar 	if (obj)
10354d5275abSLijo Lazar 		ACPI_FREE(obj);
10364d5275abSLijo Lazar 	kfree(tmp);
10374d5275abSLijo Lazar 
10384d5275abSLijo Lazar 	return ret;
10394d5275abSLijo Lazar }
10404d5275abSLijo Lazar 
amdgpu_acpi_get_xcc_info(struct amdgpu_acpi_xcc_info * xcc_info,u32 * sbdf)10414d5275abSLijo Lazar static int amdgpu_acpi_get_xcc_info(struct amdgpu_acpi_xcc_info *xcc_info,
1042d055714aSLijo Lazar 				    u32 *sbdf)
10434d5275abSLijo Lazar {
10444d5275abSLijo Lazar 	union acpi_object *obj;
10454d5275abSLijo Lazar 	acpi_status status;
10464d5275abSLijo Lazar 	int ret = -ENOENT;
10474d5275abSLijo Lazar 
10484d5275abSLijo Lazar 	obj = acpi_evaluate_dsm_typed(xcc_info->handle, &amd_xcc_dsm_guid, 0,
10494d5275abSLijo Lazar 				      AMD_XCC_DSM_GET_NUM_FUNCS, NULL,
10504d5275abSLijo Lazar 				      ACPI_TYPE_INTEGER);
10514d5275abSLijo Lazar 
10524d5275abSLijo Lazar 	if (!obj || obj->integer.value != AMD_XCC_DSM_NUM_FUNCS)
10534d5275abSLijo Lazar 		goto out;
10544d5275abSLijo Lazar 	ACPI_FREE(obj);
10554d5275abSLijo Lazar 
10564d5275abSLijo Lazar 	/* Evaluate DSMs and fill XCC information */
10574d5275abSLijo Lazar 	obj = acpi_evaluate_dsm_typed(xcc_info->handle, &amd_xcc_dsm_guid, 0,
10584d5275abSLijo Lazar 				      AMD_XCC_DSM_GET_VF_XCC_MAPPING, NULL,
10594d5275abSLijo Lazar 				      ACPI_TYPE_INTEGER);
10604d5275abSLijo Lazar 
10614d5275abSLijo Lazar 	if (!obj) {
10624d5275abSLijo Lazar 		acpi_handle_debug(xcc_info->handle,
10634d5275abSLijo Lazar 				  "_DSM function %d evaluation failed",
10644d5275abSLijo Lazar 				  AMD_XCC_DSM_GET_VF_XCC_MAPPING);
10654d5275abSLijo Lazar 		ret = -EINVAL;
10664d5275abSLijo Lazar 		goto out;
10674d5275abSLijo Lazar 	}
10684d5275abSLijo Lazar 
10694d5275abSLijo Lazar 	/* PF xcc id [39:32] */
10704d5275abSLijo Lazar 	xcc_info->phy_id = (obj->integer.value >> 32) & 0xFF;
10714d5275abSLijo Lazar 	/* xcp node of this xcc [47:40] */
10724d5275abSLijo Lazar 	xcc_info->xcp_node = (obj->integer.value >> 40) & 0xFF;
1073d055714aSLijo Lazar 	/* PF domain of this xcc [31:16] */
1074d055714aSLijo Lazar 	*sbdf = (obj->integer.value) & 0xFFFF0000;
10754d5275abSLijo Lazar 	/* PF bus/dev/fn of this xcc [63:48] */
1076d055714aSLijo Lazar 	*sbdf |= (obj->integer.value >> 48) & 0xFFFF;
10774d5275abSLijo Lazar 	ACPI_FREE(obj);
10784d5275abSLijo Lazar 	obj = NULL;
10794d5275abSLijo Lazar 
10801cc82301SLijo Lazar 	status =
10811cc82301SLijo Lazar 		amdgpu_acpi_get_node_id(xcc_info->handle, &xcc_info->numa_info);
10824d5275abSLijo Lazar 
10834d5275abSLijo Lazar 	/* TODO: check if this check is required */
10844d5275abSLijo Lazar 	if (ACPI_SUCCESS(status))
10854d5275abSLijo Lazar 		ret = 0;
10864d5275abSLijo Lazar out:
10874d5275abSLijo Lazar 	if (obj)
10884d5275abSLijo Lazar 		ACPI_FREE(obj);
10894d5275abSLijo Lazar 
10904d5275abSLijo Lazar 	return ret;
10914d5275abSLijo Lazar }
10924d5275abSLijo Lazar 
amdgpu_acpi_enumerate_xcc(void)10934d5275abSLijo Lazar static int amdgpu_acpi_enumerate_xcc(void)
10944d5275abSLijo Lazar {
10954d5275abSLijo Lazar 	struct amdgpu_acpi_dev_info *dev_info = NULL;
10964d5275abSLijo Lazar 	struct amdgpu_acpi_xcc_info *xcc_info;
10974d5275abSLijo Lazar 	struct acpi_device *acpi_dev;
10984d5275abSLijo Lazar 	char hid[ACPI_ID_LEN];
10994d5275abSLijo Lazar 	int ret, id;
1100d055714aSLijo Lazar 	u32 sbdf;
11014d5275abSLijo Lazar 
11024d5275abSLijo Lazar 	INIT_LIST_HEAD(&amdgpu_acpi_dev_list);
11031cc82301SLijo Lazar 	xa_init(&numa_info_xa);
11044d5275abSLijo Lazar 
11054d5275abSLijo Lazar 	for (id = 0; id < AMD_XCC_MAX_HID; id++) {
11064d5275abSLijo Lazar 		sprintf(hid, "%s%d", "AMD", AMD_XCC_HID_START + id);
11074d5275abSLijo Lazar 		acpi_dev = acpi_dev_get_first_match_dev(hid, NULL, -1);
11084d5275abSLijo Lazar 		/* These ACPI objects are expected to be in sequential order. If
11094d5275abSLijo Lazar 		 * one is not found, no need to check the rest.
11104d5275abSLijo Lazar 		 */
11114d5275abSLijo Lazar 		if (!acpi_dev) {
11124d5275abSLijo Lazar 			DRM_DEBUG_DRIVER("No matching acpi device found for %s",
11134d5275abSLijo Lazar 					 hid);
11144d5275abSLijo Lazar 			break;
11154d5275abSLijo Lazar 		}
11164d5275abSLijo Lazar 
11174d5275abSLijo Lazar 		xcc_info = kzalloc(sizeof(struct amdgpu_acpi_xcc_info),
11184d5275abSLijo Lazar 				   GFP_KERNEL);
11194d5275abSLijo Lazar 		if (!xcc_info) {
11204d5275abSLijo Lazar 			DRM_ERROR("Failed to allocate memory for xcc info\n");
11214d5275abSLijo Lazar 			return -ENOMEM;
11224d5275abSLijo Lazar 		}
11234d5275abSLijo Lazar 
11244d5275abSLijo Lazar 		INIT_LIST_HEAD(&xcc_info->list);
11254d5275abSLijo Lazar 		xcc_info->handle = acpi_device_handle(acpi_dev);
11264d5275abSLijo Lazar 		acpi_dev_put(acpi_dev);
11274d5275abSLijo Lazar 
1128d055714aSLijo Lazar 		ret = amdgpu_acpi_get_xcc_info(xcc_info, &sbdf);
11294d5275abSLijo Lazar 		if (ret) {
11304d5275abSLijo Lazar 			kfree(xcc_info);
11314d5275abSLijo Lazar 			continue;
11324d5275abSLijo Lazar 		}
11334d5275abSLijo Lazar 
1134d055714aSLijo Lazar 		dev_info = amdgpu_acpi_get_dev(sbdf);
11354d5275abSLijo Lazar 
11364d5275abSLijo Lazar 		if (!dev_info)
1137d055714aSLijo Lazar 			ret = amdgpu_acpi_dev_init(&dev_info, xcc_info, sbdf);
11384d5275abSLijo Lazar 
11394d5275abSLijo Lazar 		if (ret == -ENOMEM)
11404d5275abSLijo Lazar 			return ret;
11414d5275abSLijo Lazar 
11424d5275abSLijo Lazar 		if (!dev_info) {
11434d5275abSLijo Lazar 			kfree(xcc_info);
11444d5275abSLijo Lazar 			continue;
11454d5275abSLijo Lazar 		}
11464d5275abSLijo Lazar 
11474d5275abSLijo Lazar 		list_add_tail(&xcc_info->list, &dev_info->xcc_list);
11484d5275abSLijo Lazar 	}
11494d5275abSLijo Lazar 
11504d5275abSLijo Lazar 	return 0;
11514d5275abSLijo Lazar }
11524d5275abSLijo Lazar 
amdgpu_acpi_get_tmr_info(struct amdgpu_device * adev,u64 * tmr_offset,u64 * tmr_size)11536e018822SLijo Lazar int amdgpu_acpi_get_tmr_info(struct amdgpu_device *adev, u64 *tmr_offset,
11546e018822SLijo Lazar 			     u64 *tmr_size)
11556e018822SLijo Lazar {
11566e018822SLijo Lazar 	struct amdgpu_acpi_dev_info *dev_info;
1157d055714aSLijo Lazar 	u32 sbdf;
11586e018822SLijo Lazar 
11596e018822SLijo Lazar 	if (!tmr_offset || !tmr_size)
11606e018822SLijo Lazar 		return -EINVAL;
11616e018822SLijo Lazar 
1162d055714aSLijo Lazar 	sbdf = (pci_domain_nr(adev->pdev->bus) << 16);
1163d055714aSLijo Lazar 	sbdf |= pci_dev_id(adev->pdev);
1164d055714aSLijo Lazar 	dev_info = amdgpu_acpi_get_dev(sbdf);
11656e018822SLijo Lazar 	if (!dev_info)
11666e018822SLijo Lazar 		return -ENOENT;
11676e018822SLijo Lazar 
11686e018822SLijo Lazar 	*tmr_offset = dev_info->tmr_base;
11696e018822SLijo Lazar 	*tmr_size = dev_info->tmr_size;
11706e018822SLijo Lazar 
11716e018822SLijo Lazar 	return 0;
11726e018822SLijo Lazar }
11736e018822SLijo Lazar 
amdgpu_acpi_get_mem_info(struct amdgpu_device * adev,int xcc_id,struct amdgpu_numa_info * numa_info)1174fa0497c3SLijo Lazar int amdgpu_acpi_get_mem_info(struct amdgpu_device *adev, int xcc_id,
1175fa0497c3SLijo Lazar 			     struct amdgpu_numa_info *numa_info)
1176fa0497c3SLijo Lazar {
1177fa0497c3SLijo Lazar 	struct amdgpu_acpi_dev_info *dev_info;
1178fa0497c3SLijo Lazar 	struct amdgpu_acpi_xcc_info *xcc_info;
1179d055714aSLijo Lazar 	u32 sbdf;
1180fa0497c3SLijo Lazar 
1181fa0497c3SLijo Lazar 	if (!numa_info)
1182fa0497c3SLijo Lazar 		return -EINVAL;
1183fa0497c3SLijo Lazar 
1184d055714aSLijo Lazar 	sbdf = (pci_domain_nr(adev->pdev->bus) << 16);
1185d055714aSLijo Lazar 	sbdf |= pci_dev_id(adev->pdev);
1186d055714aSLijo Lazar 	dev_info = amdgpu_acpi_get_dev(sbdf);
1187fa0497c3SLijo Lazar 	if (!dev_info)
1188fa0497c3SLijo Lazar 		return -ENOENT;
1189fa0497c3SLijo Lazar 
1190fa0497c3SLijo Lazar 	list_for_each_entry(xcc_info, &dev_info->xcc_list, list) {
1191fa0497c3SLijo Lazar 		if (xcc_info->phy_id == xcc_id) {
1192fa0497c3SLijo Lazar 			memcpy(numa_info, xcc_info->numa_info,
1193fa0497c3SLijo Lazar 			       sizeof(*numa_info));
1194fa0497c3SLijo Lazar 			return 0;
1195fa0497c3SLijo Lazar 		}
1196fa0497c3SLijo Lazar 	}
1197fa0497c3SLijo Lazar 
1198fa0497c3SLijo Lazar 	return -ENOENT;
1199fa0497c3SLijo Lazar }
1200fa0497c3SLijo Lazar 
12014d5275abSLijo Lazar /**
1202d38ceaf9SAlex Deucher  * amdgpu_acpi_event - handle notify events
1203d38ceaf9SAlex Deucher  *
1204d38ceaf9SAlex Deucher  * @nb: notifier block
1205d38ceaf9SAlex Deucher  * @val: val
1206d38ceaf9SAlex Deucher  * @data: acpi event
1207d38ceaf9SAlex Deucher  *
1208d38ceaf9SAlex Deucher  * Calls relevant amdgpu functions in response to various
1209d38ceaf9SAlex Deucher  * acpi events.
1210d38ceaf9SAlex Deucher  * Returns NOTIFY code
1211d38ceaf9SAlex Deucher  */
amdgpu_acpi_event(struct notifier_block * nb,unsigned long val,void * data)1212d38ceaf9SAlex Deucher static int amdgpu_acpi_event(struct notifier_block *nb,
1213d38ceaf9SAlex Deucher 			     unsigned long val,
1214d38ceaf9SAlex Deucher 			     void *data)
1215d38ceaf9SAlex Deucher {
1216d38ceaf9SAlex Deucher 	struct amdgpu_device *adev = container_of(nb, struct amdgpu_device, acpi_nb);
1217d38ceaf9SAlex Deucher 	struct acpi_bus_event *entry = (struct acpi_bus_event *)data;
1218d38ceaf9SAlex Deucher 
1219d38ceaf9SAlex Deucher 	if (strcmp(entry->device_class, ACPI_AC_CLASS) == 0) {
1220d38ceaf9SAlex Deucher 		if (power_supply_is_system_supplied() > 0)
1221d38ceaf9SAlex Deucher 			DRM_DEBUG_DRIVER("pm: AC\n");
1222d38ceaf9SAlex Deucher 		else
1223d38ceaf9SAlex Deucher 			DRM_DEBUG_DRIVER("pm: DC\n");
1224d38ceaf9SAlex Deucher 
1225d38ceaf9SAlex Deucher 		amdgpu_pm_acpi_event_handler(adev);
1226d38ceaf9SAlex Deucher 	}
1227d38ceaf9SAlex Deucher 
1228d38ceaf9SAlex Deucher 	/* Check for pending SBIOS requests */
1229d38ceaf9SAlex Deucher 	return amdgpu_atif_handler(adev, entry);
1230d38ceaf9SAlex Deucher }
1231d38ceaf9SAlex Deucher 
1232d38ceaf9SAlex Deucher /* Call all ACPI methods here */
1233d38ceaf9SAlex Deucher /**
1234d38ceaf9SAlex Deucher  * amdgpu_acpi_init - init driver acpi support
1235d38ceaf9SAlex Deucher  *
1236d38ceaf9SAlex Deucher  * @adev: amdgpu_device pointer
1237d38ceaf9SAlex Deucher  *
1238d38ceaf9SAlex Deucher  * Verifies the AMD ACPI interfaces and registers with the acpi
1239d38ceaf9SAlex Deucher  * notifier chain (all asics).
1240d38ceaf9SAlex Deucher  * Returns 0 on success, error on failure.
1241d38ceaf9SAlex Deucher  */
amdgpu_acpi_init(struct amdgpu_device * adev)1242d38ceaf9SAlex Deucher int amdgpu_acpi_init(struct amdgpu_device *adev)
1243d38ceaf9SAlex Deucher {
1244f9b7f370SAlex Deucher 	struct amdgpu_atif *atif = &amdgpu_acpi_priv.atif;
1245d38ceaf9SAlex Deucher 
12469c27bc97SYe Bin 	if (atif->notifications.brightness_change) {
1247d09ef243SAlex Deucher 		if (adev->dc_enabled) {
124897d798b2SAlex Deucher #if defined(CONFIG_DRM_AMD_DC)
124997d798b2SAlex Deucher 			struct amdgpu_display_manager *dm = &adev->dm;
1250556bdae3SJingyu Wang 
12517fd13baeSAlex Deucher 			if (dm->backlight_dev[0])
12527fd13baeSAlex Deucher 				atif->bd = dm->backlight_dev[0];
125397d798b2SAlex Deucher #endif
125497d798b2SAlex Deucher 		} else {
1255d38ceaf9SAlex Deucher 			struct drm_encoder *tmp;
1256d38ceaf9SAlex Deucher 
1257d38ceaf9SAlex Deucher 			/* Find the encoder controlling the brightness */
12584a580877SLuben Tuikov 			list_for_each_entry(tmp, &adev_to_drm(adev)->mode_config.encoder_list,
1259d38ceaf9SAlex Deucher 					    head) {
1260d38ceaf9SAlex Deucher 				struct amdgpu_encoder *enc = to_amdgpu_encoder(tmp);
1261d38ceaf9SAlex Deucher 
1262d38ceaf9SAlex Deucher 				if ((enc->devices & (ATOM_DEVICE_LCD_SUPPORT)) &&
1263d38ceaf9SAlex Deucher 				    enc->enc_priv) {
1264d38ceaf9SAlex Deucher 					struct amdgpu_encoder_atom_dig *dig = enc->enc_priv;
1265556bdae3SJingyu Wang 
1266d38ceaf9SAlex Deucher 					if (dig->bl_dev) {
126797d798b2SAlex Deucher 						atif->bd = dig->bl_dev;
1268d38ceaf9SAlex Deucher 						break;
1269d38ceaf9SAlex Deucher 					}
1270d38ceaf9SAlex Deucher 				}
1271d38ceaf9SAlex Deucher 			}
1272d38ceaf9SAlex Deucher 		}
127397d798b2SAlex Deucher 	}
1274f9b7f370SAlex Deucher 	adev->acpi_nb.notifier_call = amdgpu_acpi_event;
1275f9b7f370SAlex Deucher 	register_acpi_notifier(&adev->acpi_nb);
1276f9b7f370SAlex Deucher 
1277f9b7f370SAlex Deucher 	return 0;
1278f9b7f370SAlex Deucher }
1279f9b7f370SAlex Deucher 
amdgpu_acpi_get_backlight_caps(struct amdgpu_dm_backlight_caps * caps)1280f9b7f370SAlex Deucher void amdgpu_acpi_get_backlight_caps(struct amdgpu_dm_backlight_caps *caps)
1281f9b7f370SAlex Deucher {
1282f9b7f370SAlex Deucher 	struct amdgpu_atif *atif = &amdgpu_acpi_priv.atif;
1283f9b7f370SAlex Deucher 
12841c79b5fcSMario Limonciello 	memcpy(caps, &atif->backlight_caps, sizeof(*caps));
1285f9b7f370SAlex Deucher }
1286f9b7f370SAlex Deucher 
1287f9b7f370SAlex Deucher /**
1288f9b7f370SAlex Deucher  * amdgpu_acpi_fini - tear down driver acpi support
1289f9b7f370SAlex Deucher  *
1290f9b7f370SAlex Deucher  * @adev: amdgpu_device pointer
1291f9b7f370SAlex Deucher  *
1292f9b7f370SAlex Deucher  * Unregisters with the acpi notifier chain (all asics).
1293f9b7f370SAlex Deucher  */
amdgpu_acpi_fini(struct amdgpu_device * adev)1294f9b7f370SAlex Deucher void amdgpu_acpi_fini(struct amdgpu_device *adev)
1295f9b7f370SAlex Deucher {
1296f9b7f370SAlex Deucher 	unregister_acpi_notifier(&adev->acpi_nb);
1297f9b7f370SAlex Deucher }
1298f9b7f370SAlex Deucher 
1299f9b7f370SAlex Deucher /**
1300f9b7f370SAlex Deucher  * amdgpu_atif_pci_probe_handle - look up the ATIF handle
1301f9b7f370SAlex Deucher  *
1302f9b7f370SAlex Deucher  * @pdev: pci device
1303f9b7f370SAlex Deucher  *
1304f9b7f370SAlex Deucher  * Look up the ATIF handles (all asics).
1305f9b7f370SAlex Deucher  * Returns true if the handle is found, false if not.
1306f9b7f370SAlex Deucher  */
amdgpu_atif_pci_probe_handle(struct pci_dev * pdev)1307f9b7f370SAlex Deucher static bool amdgpu_atif_pci_probe_handle(struct pci_dev *pdev)
1308f9b7f370SAlex Deucher {
1309f9b7f370SAlex Deucher 	char acpi_method_name[255] = { 0 };
1310f9b7f370SAlex Deucher 	struct acpi_buffer buffer = {sizeof(acpi_method_name), acpi_method_name};
1311f9b7f370SAlex Deucher 	acpi_handle dhandle, atif_handle;
1312f9b7f370SAlex Deucher 	acpi_status status;
1313f9b7f370SAlex Deucher 	int ret;
1314f9b7f370SAlex Deucher 
1315f9b7f370SAlex Deucher 	dhandle = ACPI_HANDLE(&pdev->dev);
1316f9b7f370SAlex Deucher 	if (!dhandle)
1317f9b7f370SAlex Deucher 		return false;
1318f9b7f370SAlex Deucher 
1319f9b7f370SAlex Deucher 	status = acpi_get_handle(dhandle, "ATIF", &atif_handle);
1320556bdae3SJingyu Wang 	if (ACPI_FAILURE(status))
1321f9b7f370SAlex Deucher 		return false;
1322556bdae3SJingyu Wang 
1323f9b7f370SAlex Deucher 	amdgpu_acpi_priv.atif.handle = atif_handle;
1324f9b7f370SAlex Deucher 	acpi_get_name(amdgpu_acpi_priv.atif.handle, ACPI_FULL_PATHNAME, &buffer);
1325f9b7f370SAlex Deucher 	DRM_DEBUG_DRIVER("Found ATIF handle %s\n", acpi_method_name);
1326f9b7f370SAlex Deucher 	ret = amdgpu_atif_verify_interface(&amdgpu_acpi_priv.atif);
1327f9b7f370SAlex Deucher 	if (ret) {
1328f9b7f370SAlex Deucher 		amdgpu_acpi_priv.atif.handle = 0;
1329f9b7f370SAlex Deucher 		return false;
1330f9b7f370SAlex Deucher 	}
1331f9b7f370SAlex Deucher 	return true;
1332f9b7f370SAlex Deucher }
1333f9b7f370SAlex Deucher 
1334f9b7f370SAlex Deucher /**
1335f9b7f370SAlex Deucher  * amdgpu_atcs_pci_probe_handle - look up the ATCS handle
1336f9b7f370SAlex Deucher  *
1337f9b7f370SAlex Deucher  * @pdev: pci device
1338f9b7f370SAlex Deucher  *
1339f9b7f370SAlex Deucher  * Look up the ATCS handles (all asics).
1340f9b7f370SAlex Deucher  * Returns true if the handle is found, false if not.
1341f9b7f370SAlex Deucher  */
amdgpu_atcs_pci_probe_handle(struct pci_dev * pdev)1342f9b7f370SAlex Deucher static bool amdgpu_atcs_pci_probe_handle(struct pci_dev *pdev)
1343f9b7f370SAlex Deucher {
1344f9b7f370SAlex Deucher 	char acpi_method_name[255] = { 0 };
1345f9b7f370SAlex Deucher 	struct acpi_buffer buffer = { sizeof(acpi_method_name), acpi_method_name };
1346f9b7f370SAlex Deucher 	acpi_handle dhandle, atcs_handle;
1347f9b7f370SAlex Deucher 	acpi_status status;
1348f9b7f370SAlex Deucher 	int ret;
1349f9b7f370SAlex Deucher 
1350f9b7f370SAlex Deucher 	dhandle = ACPI_HANDLE(&pdev->dev);
1351f9b7f370SAlex Deucher 	if (!dhandle)
1352f9b7f370SAlex Deucher 		return false;
1353f9b7f370SAlex Deucher 
1354f9b7f370SAlex Deucher 	status = acpi_get_handle(dhandle, "ATCS", &atcs_handle);
1355556bdae3SJingyu Wang 	if (ACPI_FAILURE(status))
1356f9b7f370SAlex Deucher 		return false;
1357556bdae3SJingyu Wang 
1358f9b7f370SAlex Deucher 	amdgpu_acpi_priv.atcs.handle = atcs_handle;
1359f9b7f370SAlex Deucher 	acpi_get_name(amdgpu_acpi_priv.atcs.handle, ACPI_FULL_PATHNAME, &buffer);
1360f9b7f370SAlex Deucher 	DRM_DEBUG_DRIVER("Found ATCS handle %s\n", acpi_method_name);
1361f9b7f370SAlex Deucher 	ret = amdgpu_atcs_verify_interface(&amdgpu_acpi_priv.atcs);
1362f9b7f370SAlex Deucher 	if (ret) {
1363f9b7f370SAlex Deucher 		amdgpu_acpi_priv.atcs.handle = 0;
1364f9b7f370SAlex Deucher 		return false;
1365f9b7f370SAlex Deucher 	}
1366f9b7f370SAlex Deucher 	return true;
1367f9b7f370SAlex Deucher }
1368f9b7f370SAlex Deucher 
1369aaee0ce4STim Huang 
1370aaee0ce4STim Huang /**
1371aaee0ce4STim Huang  * amdgpu_acpi_should_gpu_reset
1372aaee0ce4STim Huang  *
1373aaee0ce4STim Huang  * @adev: amdgpu_device_pointer
1374aaee0ce4STim Huang  *
1375aaee0ce4STim Huang  * returns true if should reset GPU, false if not
1376aaee0ce4STim Huang  */
amdgpu_acpi_should_gpu_reset(struct amdgpu_device * adev)1377aaee0ce4STim Huang bool amdgpu_acpi_should_gpu_reset(struct amdgpu_device *adev)
1378aaee0ce4STim Huang {
1379980d5baeSTim Huang 	if ((adev->flags & AMD_IS_APU) &&
1380980d5baeSTim Huang 	    adev->gfx.imu.funcs) /* Not need to do mode2 reset for IMU enabled APUs */
1381980d5baeSTim Huang 		return false;
1382980d5baeSTim Huang 
1383980d5baeSTim Huang 	if ((adev->flags & AMD_IS_APU) &&
1384980d5baeSTim Huang 	    amdgpu_acpi_is_s3_active(adev))
1385aaee0ce4STim Huang 		return false;
1386aaee0ce4STim Huang 
1387aaee0ce4STim Huang 	if (amdgpu_sriov_vf(adev))
1388aaee0ce4STim Huang 		return false;
1389aaee0ce4STim Huang 
1390aaee0ce4STim Huang #if IS_ENABLED(CONFIG_SUSPEND)
1391aaee0ce4STim Huang 	return pm_suspend_target_state != PM_SUSPEND_TO_IDLE;
1392aaee0ce4STim Huang #else
1393aaee0ce4STim Huang 	return true;
1394aaee0ce4STim Huang #endif
1395aaee0ce4STim Huang }
1396aaee0ce4STim Huang 
1397f9b7f370SAlex Deucher /*
1398f9b7f370SAlex Deucher  * amdgpu_acpi_detect - detect ACPI ATIF/ATCS methods
1399f9b7f370SAlex Deucher  *
1400f9b7f370SAlex Deucher  * Check if we have the ATIF/ATCS methods and populate
1401f9b7f370SAlex Deucher  * the structures in the driver.
1402f9b7f370SAlex Deucher  */
amdgpu_acpi_detect(void)1403f9b7f370SAlex Deucher void amdgpu_acpi_detect(void)
1404f9b7f370SAlex Deucher {
1405f9b7f370SAlex Deucher 	struct amdgpu_atif *atif = &amdgpu_acpi_priv.atif;
1406f9b7f370SAlex Deucher 	struct amdgpu_atcs *atcs = &amdgpu_acpi_priv.atcs;
1407f9b7f370SAlex Deucher 	struct pci_dev *pdev = NULL;
1408f9b7f370SAlex Deucher 	int ret;
1409f9b7f370SAlex Deucher 
141018bf4005SSui Jingfeng 	while ((pdev = pci_get_base_class(PCI_BASE_CLASS_DISPLAY, pdev))) {
141118bf4005SSui Jingfeng 		if ((pdev->class != PCI_CLASS_DISPLAY_VGA << 8) &&
141218bf4005SSui Jingfeng 		    (pdev->class != PCI_CLASS_DISPLAY_OTHER << 8))
141318bf4005SSui Jingfeng 			continue;
1414f9b7f370SAlex Deucher 
1415f9b7f370SAlex Deucher 		if (!atif->handle)
1416f9b7f370SAlex Deucher 			amdgpu_atif_pci_probe_handle(pdev);
1417f9b7f370SAlex Deucher 		if (!atcs->handle)
1418f9b7f370SAlex Deucher 			amdgpu_atcs_pci_probe_handle(pdev);
1419f9b7f370SAlex Deucher 	}
1420d38ceaf9SAlex Deucher 
1421d38ceaf9SAlex Deucher 	if (atif->functions.sbios_requests && !atif->functions.system_params) {
1422d38ceaf9SAlex Deucher 		/* XXX check this workraround, if sbios request function is
1423d38ceaf9SAlex Deucher 		 * present we have to see how it's configured in the system
1424d38ceaf9SAlex Deucher 		 * params
1425d38ceaf9SAlex Deucher 		 */
1426d38ceaf9SAlex Deucher 		atif->functions.system_params = true;
1427d38ceaf9SAlex Deucher 	}
1428d38ceaf9SAlex Deucher 
1429d38ceaf9SAlex Deucher 	if (atif->functions.system_params) {
1430280cf1a9SLyude Paul 		ret = amdgpu_atif_get_notification_params(atif);
1431d38ceaf9SAlex Deucher 		if (ret) {
1432d38ceaf9SAlex Deucher 			DRM_DEBUG_DRIVER("Call to GET_SYSTEM_PARAMS failed: %d\n",
1433d38ceaf9SAlex Deucher 					ret);
1434d38ceaf9SAlex Deucher 			/* Disable notification */
1435d38ceaf9SAlex Deucher 			atif->notification_cfg.enabled = false;
1436d38ceaf9SAlex Deucher 		}
1437d38ceaf9SAlex Deucher 	}
1438d38ceaf9SAlex Deucher 
1439206bbafeSDavid Francis 	if (atif->functions.query_backlight_transfer_characteristics) {
1440206bbafeSDavid Francis 		ret = amdgpu_atif_query_backlight_caps(atif);
1441206bbafeSDavid Francis 		if (ret) {
1442206bbafeSDavid Francis 			DRM_DEBUG_DRIVER("Call to QUERY_BACKLIGHT_TRANSFER_CHARACTERISTICS failed: %d\n",
1443206bbafeSDavid Francis 					ret);
1444206bbafeSDavid Francis 			atif->backlight_caps.caps_valid = false;
1445206bbafeSDavid Francis 		}
1446206bbafeSDavid Francis 	} else {
1447206bbafeSDavid Francis 		atif->backlight_caps.caps_valid = false;
1448206bbafeSDavid Francis 	}
14494d5275abSLijo Lazar 
14504d5275abSLijo Lazar 	amdgpu_acpi_enumerate_xcc();
14514d5275abSLijo Lazar }
14524d5275abSLijo Lazar 
amdgpu_acpi_release(void)14534d5275abSLijo Lazar void amdgpu_acpi_release(void)
14544d5275abSLijo Lazar {
14554d5275abSLijo Lazar 	struct amdgpu_acpi_dev_info *dev_info, *dev_tmp;
14564d5275abSLijo Lazar 	struct amdgpu_acpi_xcc_info *xcc_info, *xcc_tmp;
14571cc82301SLijo Lazar 	struct amdgpu_numa_info *numa_info;
14581cc82301SLijo Lazar 	unsigned long index;
14591cc82301SLijo Lazar 
14601cc82301SLijo Lazar 	xa_for_each(&numa_info_xa, index, numa_info) {
14611cc82301SLijo Lazar 		kfree(numa_info);
14621cc82301SLijo Lazar 		xa_erase(&numa_info_xa, index);
14631cc82301SLijo Lazar 	}
14644d5275abSLijo Lazar 
14654d5275abSLijo Lazar 	if (list_empty(&amdgpu_acpi_dev_list))
14664d5275abSLijo Lazar 		return;
14674d5275abSLijo Lazar 
14684d5275abSLijo Lazar 	list_for_each_entry_safe(dev_info, dev_tmp, &amdgpu_acpi_dev_list,
14694d5275abSLijo Lazar 				 list) {
14704d5275abSLijo Lazar 		list_for_each_entry_safe(xcc_info, xcc_tmp, &dev_info->xcc_list,
14714d5275abSLijo Lazar 					 list) {
14724d5275abSLijo Lazar 			list_del(&xcc_info->list);
14734d5275abSLijo Lazar 			kfree(xcc_info);
14744d5275abSLijo Lazar 		}
14754d5275abSLijo Lazar 
14764d5275abSLijo Lazar 		list_del(&dev_info->list);
14774d5275abSLijo Lazar 		kfree(dev_info);
14784d5275abSLijo Lazar 	}
1479d38ceaf9SAlex Deucher }
14804cd078dcSPrike Liang 
1481f588a1bbSMario Limonciello #if IS_ENABLED(CONFIG_SUSPEND)
14824cd078dcSPrike Liang /**
148318b66aceSMario Limonciello  * amdgpu_acpi_is_s3_active
148418b66aceSMario Limonciello  *
148518b66aceSMario Limonciello  * @adev: amdgpu_device_pointer
148618b66aceSMario Limonciello  *
148718b66aceSMario Limonciello  * returns true if supported, false if not.
148818b66aceSMario Limonciello  */
amdgpu_acpi_is_s3_active(struct amdgpu_device * adev)148918b66aceSMario Limonciello bool amdgpu_acpi_is_s3_active(struct amdgpu_device *adev)
149018b66aceSMario Limonciello {
149118b66aceSMario Limonciello 	return !(adev->flags & AMD_IS_APU) ||
149218b66aceSMario Limonciello 		(pm_suspend_target_state == PM_SUSPEND_MEM);
149318b66aceSMario Limonciello }
149418b66aceSMario Limonciello 
149518b66aceSMario Limonciello /**
1496d0260f62SPratik Vishwakarma  * amdgpu_acpi_is_s0ix_active
14974cd078dcSPrike Liang  *
14981fdbbc12SFabio M. De Francesco  * @adev: amdgpu_device_pointer
14991fdbbc12SFabio M. De Francesco  *
15004cd078dcSPrike Liang  * returns true if supported, false if not.
15014cd078dcSPrike Liang  */
amdgpu_acpi_is_s0ix_active(struct amdgpu_device * adev)1502d0260f62SPratik Vishwakarma bool amdgpu_acpi_is_s0ix_active(struct amdgpu_device *adev)
15034cd078dcSPrike Liang {
1504f588a1bbSMario Limonciello 	if (!(adev->flags & AMD_IS_APU) ||
1505f588a1bbSMario Limonciello 	    (pm_suspend_target_state != PM_SUSPEND_TO_IDLE))
1506f588a1bbSMario Limonciello 		return false;
1507f588a1bbSMario Limonciello 
1508ca475186SMario Limonciello 	if (adev->asic_type < CHIP_RAVEN)
1509ca475186SMario Limonciello 		return false;
1510ca475186SMario Limonciello 
1511e4c44b1aSMario Limonciello 	if (!(adev->pm.pp_feature & PP_GFXOFF_MASK))
1512e4c44b1aSMario Limonciello 		return false;
1513e4c44b1aSMario Limonciello 
151461ebd2feSRafael J. Wysocki 	/*
151561ebd2feSRafael J. Wysocki 	 * If ACPI_FADT_LOW_POWER_S0 is not set in the FADT, it is generally
151661ebd2feSRafael J. Wysocki 	 * risky to do any special firmware-related preparations for entering
151761ebd2feSRafael J. Wysocki 	 * S0ix even though the system is suspending to idle, so return false
151861ebd2feSRafael J. Wysocki 	 * in that case.
151961ebd2feSRafael J. Wysocki 	 */
152009521b5dSMario Limonciello 	if (!(acpi_gbl_FADT.flags & ACPI_FADT_LOW_POWER_S0)) {
1521257d7b7bSMario Limonciello 		dev_err_once(adev->dev,
1522f588a1bbSMario Limonciello 			      "Power consumption will be higher as BIOS has not been configured for suspend-to-idle.\n"
1523f588a1bbSMario Limonciello 			      "To use suspend-to-idle change the sleep mode in BIOS setup.\n");
152409521b5dSMario Limonciello 		return false;
152509521b5dSMario Limonciello 	}
1526f588a1bbSMario Limonciello 
1527f588a1bbSMario Limonciello #if !IS_ENABLED(CONFIG_AMD_PMC)
1528257d7b7bSMario Limonciello 	dev_err_once(adev->dev,
1529f588a1bbSMario Limonciello 		      "Power consumption will be higher as the kernel has not been compiled with CONFIG_AMD_PMC.\n");
153009521b5dSMario Limonciello 	return false;
153109521b5dSMario Limonciello #else
1532cf488dcdSMario Limonciello 	return true;
153309521b5dSMario Limonciello #endif /* CONFIG_AMD_PMC */
1534f588a1bbSMario Limonciello }
1535f588a1bbSMario Limonciello 
1536f588a1bbSMario Limonciello #endif /* CONFIG_SUSPEND */
1537