1 // SPDX-License-Identifier: MIT
2 /*
3  * Copyright 2012 Advanced Micro Devices, Inc.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
19  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21  * OTHER DEALINGS IN THE SOFTWARE.
22  *
23  */
24 
25 #include <linux/pci.h>
26 #include <linux/acpi.h>
27 #include <linux/backlight.h>
28 #include <linux/slab.h>
29 #include <linux/xarray.h>
30 #include <linux/power_supply.h>
31 #include <linux/pm_runtime.h>
32 #include <linux/suspend.h>
33 #include <acpi/video.h>
34 #include <acpi/actbl.h>
35 
36 #include "amdgpu.h"
37 #include "amdgpu_pm.h"
38 #include "amdgpu_display.h"
39 #include "amd_acpi.h"
40 #include "atom.h"
41 
42 /* Declare GUID for AMD _DSM method for XCCs */
43 #ifdef notyet
44 static const guid_t amd_xcc_dsm_guid = GUID_INIT(0x8267f5d5, 0xa556, 0x44f2,
45 						 0xb8, 0xb4, 0x45, 0x56, 0x2e,
46 						 0x8c, 0x5b, 0xec);
47 #endif
48 
49 #define AMD_XCC_HID_START 3000
50 #define AMD_XCC_DSM_GET_NUM_FUNCS 0
51 #define AMD_XCC_DSM_GET_SUPP_MODE 1
52 #define AMD_XCC_DSM_GET_XCP_MODE 2
53 #define AMD_XCC_DSM_GET_VF_XCC_MAPPING 4
54 #define AMD_XCC_DSM_GET_TMR_INFO 5
55 #define AMD_XCC_DSM_NUM_FUNCS 5
56 
57 #define AMD_XCC_MAX_HID 24
58 
59 struct xarray numa_info_xa;
60 
61 /* Encapsulates the XCD acpi object information */
62 struct amdgpu_acpi_xcc_info {
63 	struct list_head list;
64 	struct amdgpu_numa_info *numa_info;
65 	uint8_t xcp_node;
66 	uint8_t phy_id;
67 	acpi_handle handle;
68 };
69 
70 struct amdgpu_acpi_dev_info {
71 	struct list_head list;
72 	struct list_head xcc_list;
73 	uint32_t sbdf;
74 	uint16_t supp_xcp_mode;
75 	uint16_t xcp_mode;
76 	uint16_t mem_mode;
77 	uint64_t tmr_base;
78 	uint64_t tmr_size;
79 };
80 
81 struct list_head amdgpu_acpi_dev_list;
82 
83 struct amdgpu_atif_notification_cfg {
84 	bool enabled;
85 	int command_code;
86 };
87 
88 struct amdgpu_atif_notifications {
89 	bool thermal_state;
90 	bool forced_power_state;
91 	bool system_power_state;
92 	bool brightness_change;
93 	bool dgpu_display_event;
94 	bool gpu_package_power_limit;
95 };
96 
97 struct amdgpu_atif_functions {
98 	bool system_params;
99 	bool sbios_requests;
100 	bool temperature_change;
101 	bool query_backlight_transfer_characteristics;
102 	bool ready_to_undock;
103 	bool external_gpu_information;
104 };
105 
106 struct amdgpu_atif {
107 	acpi_handle handle;
108 
109 	struct amdgpu_atif_notifications notifications;
110 	struct amdgpu_atif_functions functions;
111 	struct amdgpu_atif_notification_cfg notification_cfg;
112 	struct backlight_device *bd;
113 	struct amdgpu_dm_backlight_caps backlight_caps;
114 };
115 
116 struct amdgpu_atcs_functions {
117 	bool get_ext_state;
118 	bool pcie_perf_req;
119 	bool pcie_dev_rdy;
120 	bool pcie_bus_width;
121 	bool power_shift_control;
122 };
123 
124 struct amdgpu_atcs {
125 	acpi_handle handle;
126 
127 	struct amdgpu_atcs_functions functions;
128 };
129 
130 static struct amdgpu_acpi_priv {
131 	struct amdgpu_atif atif;
132 	struct amdgpu_atcs atcs;
133 } amdgpu_acpi_priv;
134 
135 /* Call the ATIF method
136  */
137 /**
138  * amdgpu_atif_call - call an ATIF method
139  *
140  * @atif: atif structure
141  * @function: the ATIF function to execute
142  * @params: ATIF function params
143  *
144  * Executes the requested ATIF function (all asics).
145  * Returns a pointer to the acpi output buffer.
146  */
amdgpu_atif_call(struct amdgpu_atif * atif,int function,struct acpi_buffer * params)147 static union acpi_object *amdgpu_atif_call(struct amdgpu_atif *atif,
148 					   int function,
149 					   struct acpi_buffer *params)
150 {
151 	acpi_status status;
152 	union acpi_object *obj;
153 	union acpi_object atif_arg_elements[2];
154 	struct acpi_object_list atif_arg;
155 	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
156 
157 	atif_arg.count = 2;
158 	atif_arg.pointer = &atif_arg_elements[0];
159 
160 	atif_arg_elements[0].type = ACPI_TYPE_INTEGER;
161 	atif_arg_elements[0].integer.value = function;
162 
163 	if (params) {
164 		atif_arg_elements[1].type = ACPI_TYPE_BUFFER;
165 		atif_arg_elements[1].buffer.length = params->length;
166 		atif_arg_elements[1].buffer.pointer = params->pointer;
167 	} else {
168 		/* We need a second fake parameter */
169 		atif_arg_elements[1].type = ACPI_TYPE_INTEGER;
170 		atif_arg_elements[1].integer.value = 0;
171 	}
172 
173 	status = acpi_evaluate_object(atif->handle, NULL, &atif_arg,
174 				      &buffer);
175 	obj = (union acpi_object *)buffer.pointer;
176 
177 	/* Fail if calling the method fails */
178 	if (ACPI_FAILURE(status)) {
179 		DRM_DEBUG_DRIVER("failed to evaluate ATIF got %s\n",
180 				 acpi_format_exception(status));
181 		kfree(obj);
182 		return NULL;
183 	}
184 
185 	if (obj->type != ACPI_TYPE_BUFFER) {
186 		DRM_DEBUG_DRIVER("bad object returned from ATIF: %d\n",
187 				 obj->type);
188 		kfree(obj);
189 		return NULL;
190 	}
191 
192 	return obj;
193 }
194 
195 /**
196  * amdgpu_atif_parse_notification - parse supported notifications
197  *
198  * @n: supported notifications struct
199  * @mask: supported notifications mask from ATIF
200  *
201  * Use the supported notifications mask from ATIF function
202  * ATIF_FUNCTION_VERIFY_INTERFACE to determine what notifications
203  * are supported (all asics).
204  */
amdgpu_atif_parse_notification(struct amdgpu_atif_notifications * n,u32 mask)205 static void amdgpu_atif_parse_notification(struct amdgpu_atif_notifications *n, u32 mask)
206 {
207 	n->thermal_state = mask & ATIF_THERMAL_STATE_CHANGE_REQUEST_SUPPORTED;
208 	n->forced_power_state = mask & ATIF_FORCED_POWER_STATE_CHANGE_REQUEST_SUPPORTED;
209 	n->system_power_state = mask & ATIF_SYSTEM_POWER_SOURCE_CHANGE_REQUEST_SUPPORTED;
210 	n->brightness_change = mask & ATIF_PANEL_BRIGHTNESS_CHANGE_REQUEST_SUPPORTED;
211 	n->dgpu_display_event = mask & ATIF_DGPU_DISPLAY_EVENT_SUPPORTED;
212 	n->gpu_package_power_limit = mask & ATIF_GPU_PACKAGE_POWER_LIMIT_REQUEST_SUPPORTED;
213 }
214 
215 /**
216  * amdgpu_atif_parse_functions - parse supported functions
217  *
218  * @f: supported functions struct
219  * @mask: supported functions mask from ATIF
220  *
221  * Use the supported functions mask from ATIF function
222  * ATIF_FUNCTION_VERIFY_INTERFACE to determine what functions
223  * are supported (all asics).
224  */
amdgpu_atif_parse_functions(struct amdgpu_atif_functions * f,u32 mask)225 static void amdgpu_atif_parse_functions(struct amdgpu_atif_functions *f, u32 mask)
226 {
227 	f->system_params = mask & ATIF_GET_SYSTEM_PARAMETERS_SUPPORTED;
228 	f->sbios_requests = mask & ATIF_GET_SYSTEM_BIOS_REQUESTS_SUPPORTED;
229 	f->temperature_change = mask & ATIF_TEMPERATURE_CHANGE_NOTIFICATION_SUPPORTED;
230 	f->query_backlight_transfer_characteristics =
231 		mask & ATIF_QUERY_BACKLIGHT_TRANSFER_CHARACTERISTICS_SUPPORTED;
232 	f->ready_to_undock = mask & ATIF_READY_TO_UNDOCK_NOTIFICATION_SUPPORTED;
233 	f->external_gpu_information = mask & ATIF_GET_EXTERNAL_GPU_INFORMATION_SUPPORTED;
234 }
235 
236 /**
237  * amdgpu_atif_verify_interface - verify ATIF
238  *
239  * @atif: amdgpu atif struct
240  *
241  * Execute the ATIF_FUNCTION_VERIFY_INTERFACE ATIF function
242  * to initialize ATIF and determine what features are supported
243  * (all asics).
244  * returns 0 on success, error on failure.
245  */
amdgpu_atif_verify_interface(struct amdgpu_atif * atif)246 static int amdgpu_atif_verify_interface(struct amdgpu_atif *atif)
247 {
248 	union acpi_object *info;
249 	struct atif_verify_interface output;
250 	size_t size;
251 	int err = 0;
252 
253 	info = amdgpu_atif_call(atif, ATIF_FUNCTION_VERIFY_INTERFACE, NULL);
254 	if (!info)
255 		return -EIO;
256 
257 	memset(&output, 0, sizeof(output));
258 
259 	size = *(u16 *) info->buffer.pointer;
260 	if (size < 12) {
261 		DRM_INFO("ATIF buffer is too small: %zu\n", size);
262 		err = -EINVAL;
263 		goto out;
264 	}
265 	size = min(sizeof(output), size);
266 
267 	memcpy(&output, info->buffer.pointer, size);
268 
269 	/* TODO: check version? */
270 	DRM_DEBUG_DRIVER("ATIF version %u\n", output.version);
271 
272 	amdgpu_atif_parse_notification(&atif->notifications, output.notification_mask);
273 	amdgpu_atif_parse_functions(&atif->functions, output.function_bits);
274 
275 out:
276 	kfree(info);
277 	return err;
278 }
279 
280 /**
281  * amdgpu_atif_get_notification_params - determine notify configuration
282  *
283  * @atif: acpi handle
284  *
285  * Execute the ATIF_FUNCTION_GET_SYSTEM_PARAMETERS ATIF function
286  * to determine if a notifier is used and if so which one
287  * (all asics).  This is either Notify(VGA, 0x81) or Notify(VGA, n)
288  * where n is specified in the result if a notifier is used.
289  * Returns 0 on success, error on failure.
290  */
amdgpu_atif_get_notification_params(struct amdgpu_atif * atif)291 static int amdgpu_atif_get_notification_params(struct amdgpu_atif *atif)
292 {
293 	union acpi_object *info;
294 	struct amdgpu_atif_notification_cfg *n = &atif->notification_cfg;
295 	struct atif_system_params params;
296 	size_t size;
297 	int err = 0;
298 
299 	info = amdgpu_atif_call(atif, ATIF_FUNCTION_GET_SYSTEM_PARAMETERS,
300 				NULL);
301 	if (!info) {
302 		err = -EIO;
303 		goto out;
304 	}
305 
306 	size = *(u16 *) info->buffer.pointer;
307 	if (size < 10) {
308 		err = -EINVAL;
309 		goto out;
310 	}
311 
312 	memset(&params, 0, sizeof(params));
313 	size = min(sizeof(params), size);
314 	memcpy(&params, info->buffer.pointer, size);
315 
316 	DRM_DEBUG_DRIVER("SYSTEM_PARAMS: mask = %#x, flags = %#x\n",
317 			params.flags, params.valid_mask);
318 	params.flags = params.flags & params.valid_mask;
319 
320 	if ((params.flags & ATIF_NOTIFY_MASK) == ATIF_NOTIFY_NONE) {
321 		n->enabled = false;
322 		n->command_code = 0;
323 	} else if ((params.flags & ATIF_NOTIFY_MASK) == ATIF_NOTIFY_81) {
324 		n->enabled = true;
325 		n->command_code = 0x81;
326 	} else {
327 		if (size < 11) {
328 			err = -EINVAL;
329 			goto out;
330 		}
331 		n->enabled = true;
332 		n->command_code = params.command_code;
333 	}
334 
335 out:
336 	DRM_DEBUG_DRIVER("Notification %s, command code = %#x\n",
337 			(n->enabled ? "enabled" : "disabled"),
338 			n->command_code);
339 	kfree(info);
340 	return err;
341 }
342 
343 /**
344  * amdgpu_atif_query_backlight_caps - get min and max backlight input signal
345  *
346  * @atif: acpi handle
347  *
348  * Execute the QUERY_BRIGHTNESS_TRANSFER_CHARACTERISTICS ATIF function
349  * to determine the acceptable range of backlight values
350  *
351  * Backlight_caps.caps_valid will be set to true if the query is successful
352  *
353  * The input signals are in range 0-255
354  *
355  * This function assumes the display with backlight is the first LCD
356  *
357  * Returns 0 on success, error on failure.
358  */
amdgpu_atif_query_backlight_caps(struct amdgpu_atif * atif)359 static int amdgpu_atif_query_backlight_caps(struct amdgpu_atif *atif)
360 {
361 	union acpi_object *info;
362 	struct atif_qbtc_output characteristics;
363 	struct atif_qbtc_arguments arguments;
364 	struct acpi_buffer params;
365 	size_t size;
366 	int err = 0;
367 
368 	arguments.size = sizeof(arguments);
369 	arguments.requested_display = ATIF_QBTC_REQUEST_LCD1;
370 
371 	params.length = sizeof(arguments);
372 	params.pointer = (void *)&arguments;
373 
374 	info = amdgpu_atif_call(atif,
375 		ATIF_FUNCTION_QUERY_BRIGHTNESS_TRANSFER_CHARACTERISTICS,
376 		&params);
377 	if (!info) {
378 		err = -EIO;
379 		goto out;
380 	}
381 
382 	size = *(u16 *) info->buffer.pointer;
383 	if (size < 10) {
384 		err = -EINVAL;
385 		goto out;
386 	}
387 
388 	memset(&characteristics, 0, sizeof(characteristics));
389 	size = min(sizeof(characteristics), size);
390 	memcpy(&characteristics, info->buffer.pointer, size);
391 
392 	atif->backlight_caps.caps_valid = true;
393 	atif->backlight_caps.min_input_signal =
394 			characteristics.min_input_signal;
395 	atif->backlight_caps.max_input_signal =
396 			characteristics.max_input_signal;
397 	atif->backlight_caps.ac_level = characteristics.ac_level;
398 	atif->backlight_caps.dc_level = characteristics.dc_level;
399 out:
400 	kfree(info);
401 	return err;
402 }
403 
404 /**
405  * amdgpu_atif_get_sbios_requests - get requested sbios event
406  *
407  * @atif: acpi handle
408  * @req: atif sbios request struct
409  *
410  * Execute the ATIF_FUNCTION_GET_SYSTEM_BIOS_REQUESTS ATIF function
411  * to determine what requests the sbios is making to the driver
412  * (all asics).
413  * Returns 0 on success, error on failure.
414  */
amdgpu_atif_get_sbios_requests(struct amdgpu_atif * atif,struct atif_sbios_requests * req)415 static int amdgpu_atif_get_sbios_requests(struct amdgpu_atif *atif,
416 					  struct atif_sbios_requests *req)
417 {
418 	union acpi_object *info;
419 	size_t size;
420 	int count = 0;
421 
422 	info = amdgpu_atif_call(atif, ATIF_FUNCTION_GET_SYSTEM_BIOS_REQUESTS,
423 				NULL);
424 	if (!info)
425 		return -EIO;
426 
427 	size = *(u16 *)info->buffer.pointer;
428 	if (size < 0xd) {
429 		count = -EINVAL;
430 		goto out;
431 	}
432 	memset(req, 0, sizeof(*req));
433 
434 	size = min(sizeof(*req), size);
435 	memcpy(req, info->buffer.pointer, size);
436 	DRM_DEBUG_DRIVER("SBIOS pending requests: %#x\n", req->pending);
437 
438 	count = hweight32(req->pending);
439 
440 out:
441 	kfree(info);
442 	return count;
443 }
444 
445 /**
446  * amdgpu_atif_handler - handle ATIF notify requests
447  *
448  * @adev: amdgpu_device pointer
449  * @event: atif sbios request struct
450  *
451  * Checks the acpi event and if it matches an atif event,
452  * handles it.
453  *
454  * Returns:
455  * NOTIFY_BAD or NOTIFY_DONE, depending on the event.
456  */
amdgpu_atif_handler(struct amdgpu_device * adev,struct acpi_bus_event * event)457 static int amdgpu_atif_handler(struct amdgpu_device *adev,
458 			       struct acpi_bus_event *event)
459 {
460 	struct amdgpu_atif *atif = &amdgpu_acpi_priv.atif;
461 	int count;
462 
463 	DRM_DEBUG_DRIVER("event, device_class = %s, type = %#x\n",
464 			event->device_class, event->type);
465 
466 	if (strcmp(event->device_class, ACPI_VIDEO_CLASS) != 0)
467 		return NOTIFY_DONE;
468 
469 	/* Is this actually our event? */
470 	if (!atif->notification_cfg.enabled ||
471 	    event->type != atif->notification_cfg.command_code) {
472 		/* These events will generate keypresses otherwise */
473 		if (event->type == ACPI_VIDEO_NOTIFY_PROBE)
474 			return NOTIFY_BAD;
475 		else
476 			return NOTIFY_DONE;
477 	}
478 
479 	if (atif->functions.sbios_requests) {
480 		struct atif_sbios_requests req;
481 
482 		/* Check pending SBIOS requests */
483 		count = amdgpu_atif_get_sbios_requests(atif, &req);
484 
485 		if (count <= 0)
486 			return NOTIFY_BAD;
487 
488 		DRM_DEBUG_DRIVER("ATIF: %d pending SBIOS requests\n", count);
489 
490 		if (req.pending & ATIF_PANEL_BRIGHTNESS_CHANGE_REQUEST) {
491 			if (atif->bd) {
492 				DRM_DEBUG_DRIVER("Changing brightness to %d\n",
493 						 req.backlight_level);
494 				/*
495 				 * XXX backlight_device_set_brightness() is
496 				 * hardwired to post BACKLIGHT_UPDATE_SYSFS.
497 				 * It probably should accept 'reason' parameter.
498 				 */
499 				backlight_device_set_brightness(atif->bd, req.backlight_level);
500 			}
501 		}
502 
503 		if (req.pending & ATIF_DGPU_DISPLAY_EVENT) {
504 			if (adev->flags & AMD_IS_PX) {
505 				pm_runtime_get_sync(adev_to_drm(adev)->dev);
506 				/* Just fire off a uevent and let userspace tell us what to do */
507 				drm_helper_hpd_irq_event(adev_to_drm(adev));
508 				pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
509 				pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
510 			}
511 		}
512 		/* TODO: check other events */
513 	}
514 
515 	/* We've handled the event, stop the notifier chain. The ACPI interface
516 	 * overloads ACPI_VIDEO_NOTIFY_PROBE, we don't want to send that to
517 	 * userspace if the event was generated only to signal a SBIOS
518 	 * request.
519 	 */
520 	return NOTIFY_BAD;
521 }
522 
523 /* Call the ATCS method
524  */
525 /**
526  * amdgpu_atcs_call - call an ATCS method
527  *
528  * @atcs: atcs structure
529  * @function: the ATCS function to execute
530  * @params: ATCS function params
531  *
532  * Executes the requested ATCS function (all asics).
533  * Returns a pointer to the acpi output buffer.
534  */
amdgpu_atcs_call(struct amdgpu_atcs * atcs,int function,struct acpi_buffer * params)535 static union acpi_object *amdgpu_atcs_call(struct amdgpu_atcs *atcs,
536 					   int function,
537 					   struct acpi_buffer *params)
538 {
539 	acpi_status status;
540 	union acpi_object atcs_arg_elements[2];
541 	struct acpi_object_list atcs_arg;
542 	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
543 
544 	atcs_arg.count = 2;
545 	atcs_arg.pointer = &atcs_arg_elements[0];
546 
547 	atcs_arg_elements[0].type = ACPI_TYPE_INTEGER;
548 	atcs_arg_elements[0].integer.value = function;
549 
550 	if (params) {
551 		atcs_arg_elements[1].type = ACPI_TYPE_BUFFER;
552 		atcs_arg_elements[1].buffer.length = params->length;
553 		atcs_arg_elements[1].buffer.pointer = params->pointer;
554 	} else {
555 		/* We need a second fake parameter */
556 		atcs_arg_elements[1].type = ACPI_TYPE_INTEGER;
557 		atcs_arg_elements[1].integer.value = 0;
558 	}
559 
560 	status = acpi_evaluate_object(atcs->handle, NULL, &atcs_arg, &buffer);
561 
562 	/* Fail only if calling the method fails and ATIF is supported */
563 	if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
564 		DRM_DEBUG_DRIVER("failed to evaluate ATCS got %s\n",
565 				 acpi_format_exception(status));
566 		kfree(buffer.pointer);
567 		return NULL;
568 	}
569 
570 	return buffer.pointer;
571 }
572 
573 /**
574  * amdgpu_atcs_parse_functions - parse supported functions
575  *
576  * @f: supported functions struct
577  * @mask: supported functions mask from ATCS
578  *
579  * Use the supported functions mask from ATCS function
580  * ATCS_FUNCTION_VERIFY_INTERFACE to determine what functions
581  * are supported (all asics).
582  */
amdgpu_atcs_parse_functions(struct amdgpu_atcs_functions * f,u32 mask)583 static void amdgpu_atcs_parse_functions(struct amdgpu_atcs_functions *f, u32 mask)
584 {
585 	f->get_ext_state = mask & ATCS_GET_EXTERNAL_STATE_SUPPORTED;
586 	f->pcie_perf_req = mask & ATCS_PCIE_PERFORMANCE_REQUEST_SUPPORTED;
587 	f->pcie_dev_rdy = mask & ATCS_PCIE_DEVICE_READY_NOTIFICATION_SUPPORTED;
588 	f->pcie_bus_width = mask & ATCS_SET_PCIE_BUS_WIDTH_SUPPORTED;
589 	f->power_shift_control = mask & ATCS_SET_POWER_SHIFT_CONTROL_SUPPORTED;
590 }
591 
592 /**
593  * amdgpu_atcs_verify_interface - verify ATCS
594  *
595  * @atcs: amdgpu atcs struct
596  *
597  * Execute the ATCS_FUNCTION_VERIFY_INTERFACE ATCS function
598  * to initialize ATCS and determine what features are supported
599  * (all asics).
600  * returns 0 on success, error on failure.
601  */
amdgpu_atcs_verify_interface(struct amdgpu_atcs * atcs)602 static int amdgpu_atcs_verify_interface(struct amdgpu_atcs *atcs)
603 {
604 	union acpi_object *info;
605 	struct atcs_verify_interface output;
606 	size_t size;
607 	int err = 0;
608 
609 	info = amdgpu_atcs_call(atcs, ATCS_FUNCTION_VERIFY_INTERFACE, NULL);
610 	if (!info)
611 		return -EIO;
612 
613 	memset(&output, 0, sizeof(output));
614 
615 	size = *(u16 *) info->buffer.pointer;
616 	if (size < 8) {
617 		DRM_INFO("ATCS buffer is too small: %zu\n", size);
618 		err = -EINVAL;
619 		goto out;
620 	}
621 	size = min(sizeof(output), size);
622 
623 	memcpy(&output, info->buffer.pointer, size);
624 
625 	/* TODO: check version? */
626 	DRM_DEBUG_DRIVER("ATCS version %u\n", output.version);
627 
628 	amdgpu_atcs_parse_functions(&atcs->functions, output.function_bits);
629 
630 out:
631 	kfree(info);
632 	return err;
633 }
634 
635 /**
636  * amdgpu_acpi_is_pcie_performance_request_supported
637  *
638  * @adev: amdgpu_device pointer
639  *
640  * Check if the ATCS pcie_perf_req and pcie_dev_rdy methods
641  * are supported (all asics).
642  * returns true if supported, false if not.
643  */
amdgpu_acpi_is_pcie_performance_request_supported(struct amdgpu_device * adev)644 bool amdgpu_acpi_is_pcie_performance_request_supported(struct amdgpu_device *adev)
645 {
646 	struct amdgpu_atcs *atcs = &amdgpu_acpi_priv.atcs;
647 
648 	if (atcs->functions.pcie_perf_req && atcs->functions.pcie_dev_rdy)
649 		return true;
650 
651 	return false;
652 }
653 
654 /**
655  * amdgpu_acpi_is_power_shift_control_supported
656  *
657  * Check if the ATCS power shift control method
658  * is supported.
659  * returns true if supported, false if not.
660  */
amdgpu_acpi_is_power_shift_control_supported(void)661 bool amdgpu_acpi_is_power_shift_control_supported(void)
662 {
663 	return amdgpu_acpi_priv.atcs.functions.power_shift_control;
664 }
665 
666 /**
667  * amdgpu_acpi_pcie_notify_device_ready
668  *
669  * @adev: amdgpu_device pointer
670  *
671  * Executes the PCIE_DEVICE_READY_NOTIFICATION method
672  * (all asics).
673  * returns 0 on success, error on failure.
674  */
amdgpu_acpi_pcie_notify_device_ready(struct amdgpu_device * adev)675 int amdgpu_acpi_pcie_notify_device_ready(struct amdgpu_device *adev)
676 {
677 	union acpi_object *info;
678 	struct amdgpu_atcs *atcs = &amdgpu_acpi_priv.atcs;
679 
680 	if (!atcs->functions.pcie_dev_rdy)
681 		return -EINVAL;
682 
683 	info = amdgpu_atcs_call(atcs, ATCS_FUNCTION_PCIE_DEVICE_READY_NOTIFICATION, NULL);
684 	if (!info)
685 		return -EIO;
686 
687 	kfree(info);
688 
689 	return 0;
690 }
691 
692 /**
693  * amdgpu_acpi_pcie_performance_request
694  *
695  * @adev: amdgpu_device pointer
696  * @perf_req: requested perf level (pcie gen speed)
697  * @advertise: set advertise caps flag if set
698  *
699  * Executes the PCIE_PERFORMANCE_REQUEST method to
700  * change the pcie gen speed (all asics).
701  * returns 0 on success, error on failure.
702  */
amdgpu_acpi_pcie_performance_request(struct amdgpu_device * adev,u8 perf_req,bool advertise)703 int amdgpu_acpi_pcie_performance_request(struct amdgpu_device *adev,
704 					 u8 perf_req, bool advertise)
705 {
706 	union acpi_object *info;
707 	struct amdgpu_atcs *atcs = &amdgpu_acpi_priv.atcs;
708 	struct atcs_pref_req_input atcs_input;
709 	struct atcs_pref_req_output atcs_output;
710 	struct acpi_buffer params;
711 	size_t size;
712 	u32 retry = 3;
713 
714 	if (amdgpu_acpi_pcie_notify_device_ready(adev))
715 		return -EINVAL;
716 
717 	if (!atcs->functions.pcie_perf_req)
718 		return -EINVAL;
719 
720 	atcs_input.size = sizeof(struct atcs_pref_req_input);
721 	/* client id (bit 2-0: func num, 7-3: dev num, 15-8: bus num) */
722 	atcs_input.client_id = pci_dev_id(adev->pdev);
723 	atcs_input.valid_flags_mask = ATCS_VALID_FLAGS_MASK;
724 	atcs_input.flags = ATCS_WAIT_FOR_COMPLETION;
725 	if (advertise)
726 		atcs_input.flags |= ATCS_ADVERTISE_CAPS;
727 	atcs_input.req_type = ATCS_PCIE_LINK_SPEED;
728 	atcs_input.perf_req = perf_req;
729 
730 	params.length = sizeof(struct atcs_pref_req_input);
731 	params.pointer = &atcs_input;
732 
733 	while (retry--) {
734 		info = amdgpu_atcs_call(atcs, ATCS_FUNCTION_PCIE_PERFORMANCE_REQUEST, &params);
735 		if (!info)
736 			return -EIO;
737 
738 		memset(&atcs_output, 0, sizeof(atcs_output));
739 
740 		size = *(u16 *) info->buffer.pointer;
741 		if (size < 3) {
742 			DRM_INFO("ATCS buffer is too small: %zu\n", size);
743 			kfree(info);
744 			return -EINVAL;
745 		}
746 		size = min(sizeof(atcs_output), size);
747 
748 		memcpy(&atcs_output, info->buffer.pointer, size);
749 
750 		kfree(info);
751 
752 		switch (atcs_output.ret_val) {
753 		case ATCS_REQUEST_REFUSED:
754 		default:
755 			return -EINVAL;
756 		case ATCS_REQUEST_COMPLETE:
757 			return 0;
758 		case ATCS_REQUEST_IN_PROGRESS:
759 			udelay(10);
760 			break;
761 		}
762 	}
763 
764 	return 0;
765 }
766 
767 /**
768  * amdgpu_acpi_power_shift_control
769  *
770  * @adev: amdgpu_device pointer
771  * @dev_state: device acpi state
772  * @drv_state: driver state
773  *
774  * Executes the POWER_SHIFT_CONTROL method to
775  * communicate current dGPU device state and
776  * driver state to APU/SBIOS.
777  * returns 0 on success, error on failure.
778  */
amdgpu_acpi_power_shift_control(struct amdgpu_device * adev,u8 dev_state,bool drv_state)779 int amdgpu_acpi_power_shift_control(struct amdgpu_device *adev,
780 				    u8 dev_state, bool drv_state)
781 {
782 	union acpi_object *info;
783 	struct amdgpu_atcs *atcs = &amdgpu_acpi_priv.atcs;
784 	struct atcs_pwr_shift_input atcs_input;
785 	struct acpi_buffer params;
786 
787 	if (!amdgpu_acpi_is_power_shift_control_supported())
788 		return -EINVAL;
789 
790 	atcs_input.size = sizeof(struct atcs_pwr_shift_input);
791 	/* dGPU id (bit 2-0: func num, 7-3: dev num, 15-8: bus num) */
792 	atcs_input.dgpu_id = pci_dev_id(adev->pdev);
793 	atcs_input.dev_acpi_state = dev_state;
794 	atcs_input.drv_state = drv_state;
795 
796 	params.length = sizeof(struct atcs_pwr_shift_input);
797 	params.pointer = &atcs_input;
798 
799 	info = amdgpu_atcs_call(atcs, ATCS_FUNCTION_POWER_SHIFT_CONTROL, &params);
800 	if (!info) {
801 		DRM_ERROR("ATCS PSC update failed\n");
802 		return -EIO;
803 	}
804 
805 	kfree(info);
806 	return 0;
807 }
808 
809 /**
810  * amdgpu_acpi_smart_shift_update - update dGPU device state to SBIOS
811  *
812  * @dev: drm_device pointer
813  * @ss_state: current smart shift event
814  *
815  * returns 0 on success,
816  * otherwise return error number.
817  */
amdgpu_acpi_smart_shift_update(struct drm_device * dev,enum amdgpu_ss ss_state)818 int amdgpu_acpi_smart_shift_update(struct drm_device *dev, enum amdgpu_ss ss_state)
819 {
820 	struct amdgpu_device *adev = drm_to_adev(dev);
821 	int r;
822 
823 	if (!amdgpu_device_supports_smart_shift(dev))
824 		return 0;
825 
826 	switch (ss_state) {
827 	/* SBIOS trigger “stop”, “enable” and “start” at D0, Driver Operational.
828 	 * SBIOS trigger “stop” at D3, Driver Not Operational.
829 	 * SBIOS trigger “stop” and “disable” at D0, Driver NOT operational.
830 	 */
831 	case AMDGPU_SS_DRV_LOAD:
832 		r = amdgpu_acpi_power_shift_control(adev,
833 						    AMDGPU_ATCS_PSC_DEV_STATE_D0,
834 						    AMDGPU_ATCS_PSC_DRV_STATE_OPR);
835 		break;
836 	case AMDGPU_SS_DEV_D0:
837 		r = amdgpu_acpi_power_shift_control(adev,
838 						    AMDGPU_ATCS_PSC_DEV_STATE_D0,
839 						    AMDGPU_ATCS_PSC_DRV_STATE_OPR);
840 		break;
841 	case AMDGPU_SS_DEV_D3:
842 		r = amdgpu_acpi_power_shift_control(adev,
843 						    AMDGPU_ATCS_PSC_DEV_STATE_D3_HOT,
844 						    AMDGPU_ATCS_PSC_DRV_STATE_NOT_OPR);
845 		break;
846 	case AMDGPU_SS_DRV_UNLOAD:
847 		r = amdgpu_acpi_power_shift_control(adev,
848 						    AMDGPU_ATCS_PSC_DEV_STATE_D0,
849 						    AMDGPU_ATCS_PSC_DRV_STATE_NOT_OPR);
850 		break;
851 	default:
852 		return -EINVAL;
853 	}
854 
855 	return r;
856 }
857 
858 #ifdef CONFIG_ACPI_NUMA
amdgpu_acpi_get_numa_size(int nid)859 static inline uint64_t amdgpu_acpi_get_numa_size(int nid)
860 {
861 	/* This is directly using si_meminfo_node implementation as the
862 	 * function is not exported.
863 	 */
864 	int zone_type;
865 	uint64_t managed_pages = 0;
866 
867 	pg_data_t *pgdat = NODE_DATA(nid);
868 
869 	for (zone_type = 0; zone_type < MAX_NR_ZONES; zone_type++)
870 		managed_pages +=
871 			zone_managed_pages(&pgdat->node_zones[zone_type]);
872 	return managed_pages * PAGE_SIZE;
873 }
874 
amdgpu_acpi_get_numa_info(uint32_t pxm)875 static struct amdgpu_numa_info *amdgpu_acpi_get_numa_info(uint32_t pxm)
876 {
877 	struct amdgpu_numa_info *numa_info;
878 	int nid;
879 
880 	numa_info = xa_load(&numa_info_xa, pxm);
881 
882 	if (!numa_info) {
883 		struct sysinfo info;
884 
885 		numa_info = kzalloc(sizeof(*numa_info), GFP_KERNEL);
886 		if (!numa_info)
887 			return NULL;
888 
889 		nid = pxm_to_node(pxm);
890 		numa_info->pxm = pxm;
891 		numa_info->nid = nid;
892 
893 		if (numa_info->nid == NUMA_NO_NODE) {
894 			si_meminfo(&info);
895 			numa_info->size = info.totalram * info.mem_unit;
896 		} else {
897 			numa_info->size = amdgpu_acpi_get_numa_size(nid);
898 		}
899 		xa_store(&numa_info_xa, numa_info->pxm, numa_info, GFP_KERNEL);
900 	}
901 
902 	return numa_info;
903 }
904 #endif
905 
906 /**
907  * amdgpu_acpi_get_node_id - obtain the NUMA node id for corresponding amdgpu
908  * acpi device handle
909  *
910  * @handle: acpi handle
911  * @numa_info: amdgpu_numa_info structure holding numa information
912  *
913  * Queries the ACPI interface to fetch the corresponding NUMA Node ID for a
914  * given amdgpu acpi device.
915  *
916  * Returns ACPI STATUS OK with Node ID on success or the corresponding failure reason
917  */
amdgpu_acpi_get_node_id(acpi_handle handle,struct amdgpu_numa_info ** numa_info)918 static acpi_status amdgpu_acpi_get_node_id(acpi_handle handle,
919 				    struct amdgpu_numa_info **numa_info)
920 {
921 #ifdef CONFIG_ACPI_NUMA
922 	u64 pxm;
923 	acpi_status status;
924 
925 	if (!numa_info)
926 		return_ACPI_STATUS(AE_ERROR);
927 
928 	status = acpi_evaluate_integer(handle, "_PXM", NULL, &pxm);
929 
930 	if (ACPI_FAILURE(status))
931 		return status;
932 
933 	*numa_info = amdgpu_acpi_get_numa_info(pxm);
934 
935 	if (!*numa_info)
936 		return_ACPI_STATUS(AE_ERROR);
937 
938 	return_ACPI_STATUS(AE_OK);
939 #else
940 	return_ACPI_STATUS(AE_NOT_EXIST);
941 #endif
942 }
943 
amdgpu_acpi_get_dev(u32 sbdf)944 static struct amdgpu_acpi_dev_info *amdgpu_acpi_get_dev(u32 sbdf)
945 {
946 	struct amdgpu_acpi_dev_info *acpi_dev;
947 
948 	if (list_empty(&amdgpu_acpi_dev_list))
949 		return NULL;
950 
951 	list_for_each_entry(acpi_dev, &amdgpu_acpi_dev_list, list)
952 		if (acpi_dev->sbdf == sbdf)
953 			return acpi_dev;
954 
955 	return NULL;
956 }
957 
amdgpu_acpi_dev_init(struct amdgpu_acpi_dev_info ** dev_info,struct amdgpu_acpi_xcc_info * xcc_info,u32 sbdf)958 static int amdgpu_acpi_dev_init(struct amdgpu_acpi_dev_info **dev_info,
959 				struct amdgpu_acpi_xcc_info *xcc_info, u32 sbdf)
960 {
961 	struct amdgpu_acpi_dev_info *tmp;
962 	union acpi_object *obj;
963 	int ret = -ENOENT;
964 
965 	*dev_info = NULL;
966 	tmp = kzalloc(sizeof(struct amdgpu_acpi_dev_info), GFP_KERNEL);
967 	if (!tmp)
968 		return -ENOMEM;
969 
970 	INIT_LIST_HEAD(&tmp->xcc_list);
971 	INIT_LIST_HEAD(&tmp->list);
972 	tmp->sbdf = sbdf;
973 
974 	STUB();
975 	return -ENOSYS;
976 #ifdef notyet
977 
978 	obj = acpi_evaluate_dsm_typed(xcc_info->handle, &amd_xcc_dsm_guid, 0,
979 				      AMD_XCC_DSM_GET_SUPP_MODE, NULL,
980 				      ACPI_TYPE_INTEGER);
981 
982 	if (!obj) {
983 		acpi_handle_debug(xcc_info->handle,
984 				  "_DSM function %d evaluation failed",
985 				  AMD_XCC_DSM_GET_SUPP_MODE);
986 		ret = -ENOENT;
987 		goto out;
988 	}
989 
990 	tmp->supp_xcp_mode = obj->integer.value & 0xFFFF;
991 	ACPI_FREE(obj);
992 
993 	obj = acpi_evaluate_dsm_typed(xcc_info->handle, &amd_xcc_dsm_guid, 0,
994 				      AMD_XCC_DSM_GET_XCP_MODE, NULL,
995 				      ACPI_TYPE_INTEGER);
996 
997 	if (!obj) {
998 		acpi_handle_debug(xcc_info->handle,
999 				  "_DSM function %d evaluation failed",
1000 				  AMD_XCC_DSM_GET_XCP_MODE);
1001 		ret = -ENOENT;
1002 		goto out;
1003 	}
1004 
1005 	tmp->xcp_mode = obj->integer.value & 0xFFFF;
1006 	tmp->mem_mode = (obj->integer.value >> 32) & 0xFFFF;
1007 	ACPI_FREE(obj);
1008 
1009 	/* Evaluate DSMs and fill XCC information */
1010 	obj = acpi_evaluate_dsm_typed(xcc_info->handle, &amd_xcc_dsm_guid, 0,
1011 				      AMD_XCC_DSM_GET_TMR_INFO, NULL,
1012 				      ACPI_TYPE_PACKAGE);
1013 
1014 	if (!obj || obj->package.count < 2) {
1015 		acpi_handle_debug(xcc_info->handle,
1016 				  "_DSM function %d evaluation failed",
1017 				  AMD_XCC_DSM_GET_TMR_INFO);
1018 		ret = -ENOENT;
1019 		goto out;
1020 	}
1021 
1022 	tmp->tmr_base = obj->package.elements[0].integer.value;
1023 	tmp->tmr_size = obj->package.elements[1].integer.value;
1024 	ACPI_FREE(obj);
1025 
1026 	DRM_DEBUG_DRIVER(
1027 		"New dev(%x): Supported xcp mode: %x curr xcp_mode : %x mem mode : %x, tmr base: %llx tmr size: %llx  ",
1028 		tmp->sbdf, tmp->supp_xcp_mode, tmp->xcp_mode, tmp->mem_mode,
1029 		tmp->tmr_base, tmp->tmr_size);
1030 	list_add_tail(&tmp->list, &amdgpu_acpi_dev_list);
1031 	*dev_info = tmp;
1032 
1033 	return 0;
1034 
1035 out:
1036 	if (obj)
1037 		ACPI_FREE(obj);
1038 	kfree(tmp);
1039 
1040 	return ret;
1041 #endif
1042 }
1043 
amdgpu_acpi_get_xcc_info(struct amdgpu_acpi_xcc_info * xcc_info,u32 * sbdf)1044 static int amdgpu_acpi_get_xcc_info(struct amdgpu_acpi_xcc_info *xcc_info,
1045 				    u32 *sbdf)
1046 {
1047 	STUB();
1048 	return -ENOSYS;
1049 #ifdef notyet
1050 	union acpi_object *obj;
1051 	acpi_status status;
1052 	int ret = -ENOENT;
1053 
1054 	obj = acpi_evaluate_dsm_typed(xcc_info->handle, &amd_xcc_dsm_guid, 0,
1055 				      AMD_XCC_DSM_GET_NUM_FUNCS, NULL,
1056 				      ACPI_TYPE_INTEGER);
1057 
1058 	if (!obj || obj->integer.value != AMD_XCC_DSM_NUM_FUNCS)
1059 		goto out;
1060 	ACPI_FREE(obj);
1061 
1062 	/* Evaluate DSMs and fill XCC information */
1063 	obj = acpi_evaluate_dsm_typed(xcc_info->handle, &amd_xcc_dsm_guid, 0,
1064 				      AMD_XCC_DSM_GET_VF_XCC_MAPPING, NULL,
1065 				      ACPI_TYPE_INTEGER);
1066 
1067 	if (!obj) {
1068 		acpi_handle_debug(xcc_info->handle,
1069 				  "_DSM function %d evaluation failed",
1070 				  AMD_XCC_DSM_GET_VF_XCC_MAPPING);
1071 		ret = -EINVAL;
1072 		goto out;
1073 	}
1074 
1075 	/* PF xcc id [39:32] */
1076 	xcc_info->phy_id = (obj->integer.value >> 32) & 0xFF;
1077 	/* xcp node of this xcc [47:40] */
1078 	xcc_info->xcp_node = (obj->integer.value >> 40) & 0xFF;
1079 	/* PF domain of this xcc [31:16] */
1080 	*sbdf = (obj->integer.value) & 0xFFFF0000;
1081 	/* PF bus/dev/fn of this xcc [63:48] */
1082 	*sbdf |= (obj->integer.value >> 48) & 0xFFFF;
1083 	ACPI_FREE(obj);
1084 	obj = NULL;
1085 
1086 	status =
1087 		amdgpu_acpi_get_node_id(xcc_info->handle, &xcc_info->numa_info);
1088 
1089 	/* TODO: check if this check is required */
1090 	if (ACPI_SUCCESS(status))
1091 		ret = 0;
1092 out:
1093 	if (obj)
1094 		ACPI_FREE(obj);
1095 
1096 	return ret;
1097 #endif
1098 }
1099 
amdgpu_acpi_enumerate_xcc(void)1100 static int amdgpu_acpi_enumerate_xcc(void)
1101 {
1102 	struct amdgpu_acpi_dev_info *dev_info = NULL;
1103 	struct amdgpu_acpi_xcc_info *xcc_info;
1104 	struct acpi_device *acpi_dev;
1105 	char hid[ACPI_ID_LEN];
1106 	int ret, id;
1107 	u32 sbdf;
1108 
1109 	INIT_LIST_HEAD(&amdgpu_acpi_dev_list);
1110 	xa_init(&numa_info_xa);
1111 
1112 #ifdef notyet
1113 	for (id = 0; id < AMD_XCC_MAX_HID; id++) {
1114 		snprintf(hid, sizeof(hid), "%s%d", "AMD", AMD_XCC_HID_START + id);
1115 		acpi_dev = acpi_dev_get_first_match_dev(hid, NULL, -1);
1116 		/* These ACPI objects are expected to be in sequential order. If
1117 		 * one is not found, no need to check the rest.
1118 		 */
1119 		if (!acpi_dev) {
1120 			DRM_DEBUG_DRIVER("No matching acpi device found for %s",
1121 					 hid);
1122 			break;
1123 		}
1124 
1125 		xcc_info = kzalloc(sizeof(struct amdgpu_acpi_xcc_info),
1126 				   GFP_KERNEL);
1127 		if (!xcc_info) {
1128 			DRM_ERROR("Failed to allocate memory for xcc info\n");
1129 			return -ENOMEM;
1130 		}
1131 
1132 		INIT_LIST_HEAD(&xcc_info->list);
1133 		xcc_info->handle = acpi_device_handle(acpi_dev);
1134 		acpi_dev_put(acpi_dev);
1135 
1136 		ret = amdgpu_acpi_get_xcc_info(xcc_info, &sbdf);
1137 		if (ret) {
1138 			kfree(xcc_info);
1139 			continue;
1140 		}
1141 
1142 		dev_info = amdgpu_acpi_get_dev(sbdf);
1143 
1144 		if (!dev_info)
1145 			ret = amdgpu_acpi_dev_init(&dev_info, xcc_info, sbdf);
1146 
1147 		if (ret == -ENOMEM)
1148 			return ret;
1149 
1150 		if (!dev_info) {
1151 			kfree(xcc_info);
1152 			continue;
1153 		}
1154 
1155 		list_add_tail(&xcc_info->list, &dev_info->xcc_list);
1156 	}
1157 #endif
1158 
1159 	return 0;
1160 }
1161 
amdgpu_acpi_get_tmr_info(struct amdgpu_device * adev,u64 * tmr_offset,u64 * tmr_size)1162 int amdgpu_acpi_get_tmr_info(struct amdgpu_device *adev, u64 *tmr_offset,
1163 			     u64 *tmr_size)
1164 {
1165 	struct amdgpu_acpi_dev_info *dev_info;
1166 	u32 sbdf;
1167 
1168 	if (!tmr_offset || !tmr_size)
1169 		return -EINVAL;
1170 
1171 	sbdf = (pci_domain_nr(adev->pdev->bus) << 16);
1172 	sbdf |= pci_dev_id(adev->pdev);
1173 	dev_info = amdgpu_acpi_get_dev(sbdf);
1174 	if (!dev_info)
1175 		return -ENOENT;
1176 
1177 	*tmr_offset = dev_info->tmr_base;
1178 	*tmr_size = dev_info->tmr_size;
1179 
1180 	return 0;
1181 }
1182 
amdgpu_acpi_get_mem_info(struct amdgpu_device * adev,int xcc_id,struct amdgpu_numa_info * numa_info)1183 int amdgpu_acpi_get_mem_info(struct amdgpu_device *adev, int xcc_id,
1184 			     struct amdgpu_numa_info *numa_info)
1185 {
1186 	struct amdgpu_acpi_dev_info *dev_info;
1187 	struct amdgpu_acpi_xcc_info *xcc_info;
1188 	u32 sbdf;
1189 
1190 	if (!numa_info)
1191 		return -EINVAL;
1192 
1193 	sbdf = (pci_domain_nr(adev->pdev->bus) << 16);
1194 	sbdf |= pci_dev_id(adev->pdev);
1195 	dev_info = amdgpu_acpi_get_dev(sbdf);
1196 	if (!dev_info)
1197 		return -ENOENT;
1198 
1199 	list_for_each_entry(xcc_info, &dev_info->xcc_list, list) {
1200 		if (xcc_info->phy_id == xcc_id) {
1201 			memcpy(numa_info, xcc_info->numa_info,
1202 			       sizeof(*numa_info));
1203 			return 0;
1204 		}
1205 	}
1206 
1207 	return -ENOENT;
1208 }
1209 
1210 /**
1211  * amdgpu_acpi_event - handle notify events
1212  *
1213  * @nb: notifier block
1214  * @val: val
1215  * @data: acpi event
1216  *
1217  * Calls relevant amdgpu functions in response to various
1218  * acpi events.
1219  * Returns NOTIFY code
1220  */
amdgpu_acpi_event(struct notifier_block * nb,unsigned long val,void * data)1221 static int amdgpu_acpi_event(struct notifier_block *nb,
1222 			     unsigned long val,
1223 			     void *data)
1224 {
1225 	struct amdgpu_device *adev = container_of(nb, struct amdgpu_device, acpi_nb);
1226 	struct acpi_bus_event *entry = (struct acpi_bus_event *)data;
1227 
1228 	if (strcmp(entry->device_class, ACPI_AC_CLASS) == 0) {
1229 		if (power_supply_is_system_supplied() > 0)
1230 			DRM_DEBUG_DRIVER("pm: AC\n");
1231 		else
1232 			DRM_DEBUG_DRIVER("pm: DC\n");
1233 
1234 		amdgpu_pm_acpi_event_handler(adev);
1235 	}
1236 
1237 	/* Check for pending SBIOS requests */
1238 	return amdgpu_atif_handler(adev, entry);
1239 }
1240 
1241 /* Call all ACPI methods here */
1242 /**
1243  * amdgpu_acpi_init - init driver acpi support
1244  *
1245  * @adev: amdgpu_device pointer
1246  *
1247  * Verifies the AMD ACPI interfaces and registers with the acpi
1248  * notifier chain (all asics).
1249  * Returns 0 on success, error on failure.
1250  */
amdgpu_acpi_init(struct amdgpu_device * adev)1251 int amdgpu_acpi_init(struct amdgpu_device *adev)
1252 {
1253 	struct amdgpu_atif *atif = &amdgpu_acpi_priv.atif;
1254 
1255 	if (atif->notifications.brightness_change) {
1256 		if (adev->dc_enabled) {
1257 #if defined(CONFIG_DRM_AMD_DC)
1258 			struct amdgpu_display_manager *dm = &adev->dm;
1259 
1260 			if (dm->backlight_dev[0])
1261 				atif->bd = dm->backlight_dev[0];
1262 #endif
1263 		} else {
1264 			struct drm_encoder *tmp;
1265 
1266 			/* Find the encoder controlling the brightness */
1267 			list_for_each_entry(tmp, &adev_to_drm(adev)->mode_config.encoder_list,
1268 					    head) {
1269 				struct amdgpu_encoder *enc = to_amdgpu_encoder(tmp);
1270 
1271 				if ((enc->devices & (ATOM_DEVICE_LCD_SUPPORT)) &&
1272 				    enc->enc_priv) {
1273 					struct amdgpu_encoder_atom_dig *dig = enc->enc_priv;
1274 
1275 					if (dig->bl_dev) {
1276 						atif->bd = dig->bl_dev;
1277 						break;
1278 					}
1279 				}
1280 			}
1281 		}
1282 	}
1283 	adev->acpi_nb.notifier_call = amdgpu_acpi_event;
1284 	register_acpi_notifier(&adev->acpi_nb);
1285 
1286 	return 0;
1287 }
1288 
amdgpu_acpi_get_backlight_caps(struct amdgpu_dm_backlight_caps * caps)1289 void amdgpu_acpi_get_backlight_caps(struct amdgpu_dm_backlight_caps *caps)
1290 {
1291 	struct amdgpu_atif *atif = &amdgpu_acpi_priv.atif;
1292 
1293 	caps->caps_valid = atif->backlight_caps.caps_valid;
1294 	caps->min_input_signal = atif->backlight_caps.min_input_signal;
1295 	caps->max_input_signal = atif->backlight_caps.max_input_signal;
1296 	caps->ac_level = atif->backlight_caps.ac_level;
1297 	caps->dc_level = atif->backlight_caps.dc_level;
1298 }
1299 
1300 /**
1301  * amdgpu_acpi_fini - tear down driver acpi support
1302  *
1303  * @adev: amdgpu_device pointer
1304  *
1305  * Unregisters with the acpi notifier chain (all asics).
1306  */
amdgpu_acpi_fini(struct amdgpu_device * adev)1307 void amdgpu_acpi_fini(struct amdgpu_device *adev)
1308 {
1309 	unregister_acpi_notifier(&adev->acpi_nb);
1310 }
1311 
1312 /**
1313  * amdgpu_atif_pci_probe_handle - look up the ATIF handle
1314  *
1315  * @pdev: pci device
1316  *
1317  * Look up the ATIF handles (all asics).
1318  * Returns true if the handle is found, false if not.
1319  */
amdgpu_atif_pci_probe_handle(struct pci_dev * pdev)1320 static bool amdgpu_atif_pci_probe_handle(struct pci_dev *pdev)
1321 {
1322 	char acpi_method_name[255] = { 0 };
1323 	struct acpi_buffer buffer = {sizeof(acpi_method_name), acpi_method_name};
1324 	acpi_handle dhandle, atif_handle;
1325 	acpi_status status;
1326 	int ret;
1327 
1328 	dhandle = ACPI_HANDLE(&pdev->dev);
1329 	if (!dhandle)
1330 		return false;
1331 
1332 	status = acpi_get_handle(dhandle, "ATIF", &atif_handle);
1333 	if (ACPI_FAILURE(status))
1334 		return false;
1335 
1336 	amdgpu_acpi_priv.atif.handle = atif_handle;
1337 	acpi_get_name(amdgpu_acpi_priv.atif.handle, ACPI_FULL_PATHNAME, &buffer);
1338 	DRM_DEBUG_DRIVER("Found ATIF handle %s\n", acpi_method_name);
1339 	ret = amdgpu_atif_verify_interface(&amdgpu_acpi_priv.atif);
1340 	if (ret) {
1341 		amdgpu_acpi_priv.atif.handle = 0;
1342 		return false;
1343 	}
1344 	return true;
1345 }
1346 
1347 /**
1348  * amdgpu_atcs_pci_probe_handle - look up the ATCS handle
1349  *
1350  * @pdev: pci device
1351  *
1352  * Look up the ATCS handles (all asics).
1353  * Returns true if the handle is found, false if not.
1354  */
amdgpu_atcs_pci_probe_handle(struct pci_dev * pdev)1355 static bool amdgpu_atcs_pci_probe_handle(struct pci_dev *pdev)
1356 {
1357 	char acpi_method_name[255] = { 0 };
1358 	struct acpi_buffer buffer = { sizeof(acpi_method_name), acpi_method_name };
1359 	acpi_handle dhandle, atcs_handle;
1360 	acpi_status status;
1361 	int ret;
1362 
1363 	dhandle = ACPI_HANDLE(&pdev->dev);
1364 	if (!dhandle)
1365 		return false;
1366 
1367 	status = acpi_get_handle(dhandle, "ATCS", &atcs_handle);
1368 	if (ACPI_FAILURE(status))
1369 		return false;
1370 
1371 	amdgpu_acpi_priv.atcs.handle = atcs_handle;
1372 	acpi_get_name(amdgpu_acpi_priv.atcs.handle, ACPI_FULL_PATHNAME, &buffer);
1373 	DRM_DEBUG_DRIVER("Found ATCS handle %s\n", acpi_method_name);
1374 	ret = amdgpu_atcs_verify_interface(&amdgpu_acpi_priv.atcs);
1375 	if (ret) {
1376 		amdgpu_acpi_priv.atcs.handle = 0;
1377 		return false;
1378 	}
1379 	return true;
1380 }
1381 
1382 extern struct cfdriver amdgpu_cd;
1383 
1384 
1385 /**
1386  * amdgpu_acpi_should_gpu_reset
1387  *
1388  * @adev: amdgpu_device_pointer
1389  *
1390  * returns true if should reset GPU, false if not
1391  */
amdgpu_acpi_should_gpu_reset(struct amdgpu_device * adev)1392 bool amdgpu_acpi_should_gpu_reset(struct amdgpu_device *adev)
1393 {
1394 	if ((adev->flags & AMD_IS_APU) &&
1395 	    adev->gfx.imu.funcs) /* Not need to do mode2 reset for IMU enabled APUs */
1396 		return false;
1397 
1398 	if ((adev->flags & AMD_IS_APU) &&
1399 	    amdgpu_acpi_is_s3_active(adev))
1400 		return false;
1401 
1402 	if (amdgpu_sriov_vf(adev))
1403 		return false;
1404 
1405 #ifdef __OpenBSD__
1406 	/* XXX VEGA10 S3 fails if reset is done */
1407 	if (pm_suspend_target_state == PM_SUSPEND_MEM)
1408 		return false;
1409 #endif
1410 
1411 #if IS_ENABLED(CONFIG_SUSPEND)
1412 	return pm_suspend_target_state != PM_SUSPEND_TO_IDLE;
1413 #else
1414 	return true;
1415 #endif
1416 }
1417 
1418 /*
1419  * amdgpu_acpi_detect - detect ACPI ATIF/ATCS methods
1420  *
1421  * Check if we have the ATIF/ATCS methods and populate
1422  * the structures in the driver.
1423  */
amdgpu_acpi_detect(void)1424 void amdgpu_acpi_detect(void)
1425 {
1426 	struct amdgpu_atif *atif = &amdgpu_acpi_priv.atif;
1427 	struct amdgpu_atcs *atcs = &amdgpu_acpi_priv.atcs;
1428 	struct pci_dev *pdev = NULL;
1429 	int ret;
1430 
1431 #ifdef notyet
1432 	while ((pdev = pci_get_base_class(PCI_BASE_CLASS_DISPLAY, pdev))) {
1433 		if ((pdev->class != PCI_CLASS_DISPLAY_VGA << 8) &&
1434 		    (pdev->class != PCI_CLASS_DISPLAY_OTHER << 8))
1435 			continue;
1436 
1437 		if (!atif->handle)
1438 			amdgpu_atif_pci_probe_handle(pdev);
1439 		if (!atcs->handle)
1440 			amdgpu_atcs_pci_probe_handle(pdev);
1441 	}
1442 #else
1443 	{
1444 		struct amdgpu_device *adev = (void *)amdgpu_cd.cd_devs[0];
1445 		pdev = adev->pdev;
1446 		if (!atif->handle)
1447 			amdgpu_atif_pci_probe_handle(pdev);
1448 		if (!atcs->handle)
1449 			amdgpu_atcs_pci_probe_handle(pdev);
1450 	}
1451 #endif
1452 
1453 	if (atif->functions.sbios_requests && !atif->functions.system_params) {
1454 		/* XXX check this workraround, if sbios request function is
1455 		 * present we have to see how it's configured in the system
1456 		 * params
1457 		 */
1458 		atif->functions.system_params = true;
1459 	}
1460 
1461 	if (atif->functions.system_params) {
1462 		ret = amdgpu_atif_get_notification_params(atif);
1463 		if (ret) {
1464 			DRM_DEBUG_DRIVER("Call to GET_SYSTEM_PARAMS failed: %d\n",
1465 					ret);
1466 			/* Disable notification */
1467 			atif->notification_cfg.enabled = false;
1468 		}
1469 	}
1470 
1471 	if (atif->functions.query_backlight_transfer_characteristics) {
1472 		ret = amdgpu_atif_query_backlight_caps(atif);
1473 		if (ret) {
1474 			DRM_DEBUG_DRIVER("Call to QUERY_BACKLIGHT_TRANSFER_CHARACTERISTICS failed: %d\n",
1475 					ret);
1476 			atif->backlight_caps.caps_valid = false;
1477 		}
1478 	} else {
1479 		atif->backlight_caps.caps_valid = false;
1480 	}
1481 
1482 	amdgpu_acpi_enumerate_xcc();
1483 }
1484 
amdgpu_acpi_release(void)1485 void amdgpu_acpi_release(void)
1486 {
1487 	struct amdgpu_acpi_dev_info *dev_info, *dev_tmp;
1488 	struct amdgpu_acpi_xcc_info *xcc_info, *xcc_tmp;
1489 	struct amdgpu_numa_info *numa_info;
1490 	unsigned long index;
1491 
1492 	xa_for_each(&numa_info_xa, index, numa_info) {
1493 		kfree(numa_info);
1494 		xa_erase(&numa_info_xa, index);
1495 	}
1496 
1497 	if (list_empty(&amdgpu_acpi_dev_list))
1498 		return;
1499 
1500 	list_for_each_entry_safe(dev_info, dev_tmp, &amdgpu_acpi_dev_list,
1501 				 list) {
1502 		list_for_each_entry_safe(xcc_info, xcc_tmp, &dev_info->xcc_list,
1503 					 list) {
1504 			list_del(&xcc_info->list);
1505 			kfree(xcc_info);
1506 		}
1507 
1508 		list_del(&dev_info->list);
1509 		kfree(dev_info);
1510 	}
1511 }
1512 
1513 #if IS_ENABLED(CONFIG_SUSPEND)
1514 /**
1515  * amdgpu_acpi_is_s3_active
1516  *
1517  * @adev: amdgpu_device_pointer
1518  *
1519  * returns true if supported, false if not.
1520  */
amdgpu_acpi_is_s3_active(struct amdgpu_device * adev)1521 bool amdgpu_acpi_is_s3_active(struct amdgpu_device *adev)
1522 {
1523 	return !(adev->flags & AMD_IS_APU) ||
1524 		(pm_suspend_target_state == PM_SUSPEND_MEM);
1525 }
1526 
1527 /**
1528  * amdgpu_acpi_is_s0ix_active
1529  *
1530  * @adev: amdgpu_device_pointer
1531  *
1532  * returns true if supported, false if not.
1533  */
amdgpu_acpi_is_s0ix_active(struct amdgpu_device * adev)1534 bool amdgpu_acpi_is_s0ix_active(struct amdgpu_device *adev)
1535 {
1536 	if (!(adev->flags & AMD_IS_APU) ||
1537 	    (pm_suspend_target_state != PM_SUSPEND_TO_IDLE))
1538 		return false;
1539 
1540 	if (adev->asic_type < CHIP_RAVEN)
1541 		return false;
1542 
1543 	if (!(adev->pm.pp_feature & PP_GFXOFF_MASK))
1544 		return false;
1545 
1546 #ifdef __linux__
1547 	/*
1548 	 * If ACPI_FADT_LOW_POWER_S0 is not set in the FADT, it is generally
1549 	 * risky to do any special firmware-related preparations for entering
1550 	 * S0ix even though the system is suspending to idle, so return false
1551 	 * in that case.
1552 	 */
1553 	if (!(acpi_gbl_FADT.flags & ACPI_FADT_LOW_POWER_S0)) {
1554 		dev_err_once(adev->dev,
1555 			      "Power consumption will be higher as BIOS has not been configured for suspend-to-idle.\n"
1556 			      "To use suspend-to-idle change the sleep mode in BIOS setup.\n");
1557 		return false;
1558 	}
1559 #endif
1560 
1561 #if !IS_ENABLED(CONFIG_AMD_PMC)
1562 	dev_err_once(adev->dev,
1563 		      "Power consumption will be higher as the kernel has not been compiled with CONFIG_AMD_PMC.\n");
1564 	return false;
1565 #else
1566 	return true;
1567 #endif /* CONFIG_AMD_PMC */
1568 }
1569 
1570 /**
1571  * amdgpu_choose_low_power_state
1572  *
1573  * @adev: amdgpu_device_pointer
1574  *
1575  * Choose the target low power state for the GPU
1576  */
amdgpu_choose_low_power_state(struct amdgpu_device * adev)1577 void amdgpu_choose_low_power_state(struct amdgpu_device *adev)
1578 {
1579 	if (adev->in_runpm)
1580 		return;
1581 
1582 	if (amdgpu_acpi_is_s0ix_active(adev))
1583 		adev->in_s0ix = true;
1584 	else if (amdgpu_acpi_is_s3_active(adev))
1585 		adev->in_s3 = true;
1586 }
1587 
1588 #endif /* CONFIG_SUSPEND */
1589