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