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