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 * @min_size: minimum size of the expected output buffer in bytes
144 *
145 * Executes the requested ATIF function (all asics).
146 * Returns a pointer to the acpi output buffer.
147 */
amdgpu_atif_call(struct amdgpu_atif * atif,int function,struct acpi_buffer * params,size_t min_size)148 static union acpi_object *amdgpu_atif_call(struct amdgpu_atif *atif,
149 int function,
150 struct acpi_buffer *params,
151 size_t min_size)
152 {
153 acpi_status status;
154 union acpi_object *obj;
155 union acpi_object atif_arg_elements[2];
156 struct acpi_object_list atif_arg;
157 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
158
159 atif_arg.count = 2;
160 atif_arg.pointer = &atif_arg_elements[0];
161
162 atif_arg_elements[0].type = ACPI_TYPE_INTEGER;
163 atif_arg_elements[0].integer.value = function;
164
165 if (params) {
166 atif_arg_elements[1].type = ACPI_TYPE_BUFFER;
167 atif_arg_elements[1].buffer.length = params->length;
168 atif_arg_elements[1].buffer.pointer = params->pointer;
169 } else {
170 /* We need a second fake parameter */
171 atif_arg_elements[1].type = ACPI_TYPE_INTEGER;
172 atif_arg_elements[1].integer.value = 0;
173 }
174
175 status = acpi_evaluate_object(atif->handle, NULL, &atif_arg,
176 &buffer);
177 obj = (union acpi_object *)buffer.pointer;
178
179 /* Fail if calling the method fails */
180 if (ACPI_FAILURE(status)) {
181 DRM_DEBUG_DRIVER("failed to evaluate ATIF got %s\n",
182 acpi_format_exception(status));
183 kfree(obj);
184 return NULL;
185 }
186
187 if (obj->type != ACPI_TYPE_BUFFER) {
188 DRM_DEBUG_DRIVER("bad object returned from ATIF: %d\n",
189 obj->type);
190 kfree(obj);
191 return NULL;
192 }
193
194 if (obj->buffer.length < sizeof(u16)) {
195 DRM_DEBUG_DRIVER("ATIF buffer too small to hold size field: %u\n",
196 obj->buffer.length);
197 kfree(obj);
198 return NULL;
199 }
200
201 if (obj->buffer.length < *(u16 *)obj->buffer.pointer) {
202 DRM_DEBUG_DRIVER("ATIF buffer length mismatch: reported %u, actual %u\n",
203 *(u16 *)obj->buffer.pointer,
204 obj->buffer.length);
205 kfree(obj);
206 return NULL;
207 }
208
209 if (*(u16 *)obj->buffer.pointer < min_size) {
210 DRM_DEBUG_DRIVER("ATIF buffer too small: expected %zu, got %u\n",
211 min_size, *(u16 *)obj->buffer.pointer);
212 kfree(obj);
213 return NULL;
214 }
215
216 return obj;
217 }
218
219 /**
220 * amdgpu_atif_parse_notification - parse supported notifications
221 *
222 * @n: supported notifications struct
223 * @mask: supported notifications mask from ATIF
224 *
225 * Use the supported notifications mask from ATIF function
226 * ATIF_FUNCTION_VERIFY_INTERFACE to determine what notifications
227 * are supported (all asics).
228 */
amdgpu_atif_parse_notification(struct amdgpu_atif_notifications * n,u32 mask)229 static void amdgpu_atif_parse_notification(struct amdgpu_atif_notifications *n, u32 mask)
230 {
231 n->thermal_state = mask & ATIF_THERMAL_STATE_CHANGE_REQUEST_SUPPORTED;
232 n->forced_power_state = mask & ATIF_FORCED_POWER_STATE_CHANGE_REQUEST_SUPPORTED;
233 n->system_power_state = mask & ATIF_SYSTEM_POWER_SOURCE_CHANGE_REQUEST_SUPPORTED;
234 n->brightness_change = mask & ATIF_PANEL_BRIGHTNESS_CHANGE_REQUEST_SUPPORTED;
235 n->dgpu_display_event = mask & ATIF_DGPU_DISPLAY_EVENT_SUPPORTED;
236 n->gpu_package_power_limit = mask & ATIF_GPU_PACKAGE_POWER_LIMIT_REQUEST_SUPPORTED;
237 }
238
239 /**
240 * amdgpu_atif_parse_functions - parse supported functions
241 *
242 * @f: supported functions struct
243 * @mask: supported functions mask from ATIF
244 *
245 * Use the supported functions mask from ATIF function
246 * ATIF_FUNCTION_VERIFY_INTERFACE to determine what functions
247 * are supported (all asics).
248 */
amdgpu_atif_parse_functions(struct amdgpu_atif_functions * f,u32 mask)249 static void amdgpu_atif_parse_functions(struct amdgpu_atif_functions *f, u32 mask)
250 {
251 f->system_params = mask & ATIF_GET_SYSTEM_PARAMETERS_SUPPORTED;
252 f->sbios_requests = mask & ATIF_GET_SYSTEM_BIOS_REQUESTS_SUPPORTED;
253 f->temperature_change = mask & ATIF_TEMPERATURE_CHANGE_NOTIFICATION_SUPPORTED;
254 f->query_backlight_transfer_characteristics =
255 mask & ATIF_QUERY_BACKLIGHT_TRANSFER_CHARACTERISTICS_SUPPORTED;
256 f->ready_to_undock = mask & ATIF_READY_TO_UNDOCK_NOTIFICATION_SUPPORTED;
257 f->external_gpu_information = mask & ATIF_GET_EXTERNAL_GPU_INFORMATION_SUPPORTED;
258 }
259
260 /**
261 * amdgpu_atif_verify_interface - verify ATIF
262 *
263 * @atif: amdgpu atif struct
264 *
265 * Execute the ATIF_FUNCTION_VERIFY_INTERFACE ATIF function
266 * to initialize ATIF and determine what features are supported
267 * (all asics).
268 * returns 0 on success, error on failure.
269 */
270 static noinline_for_stack
amdgpu_atif_verify_interface(struct amdgpu_atif * atif)271 int amdgpu_atif_verify_interface(struct amdgpu_atif *atif)
272 {
273 union acpi_object *info;
274 struct atif_verify_interface output;
275 size_t size;
276 int err = 0;
277
278 info = amdgpu_atif_call(atif, ATIF_FUNCTION_VERIFY_INTERFACE, NULL,
279 sizeof(output));
280 if (!info)
281 return -EIO;
282
283 memset(&output, 0, sizeof(output));
284
285 size = min(sizeof(output), (size_t)*(u16 *)info->buffer.pointer);
286
287 memcpy(&output, info->buffer.pointer, size);
288
289 /* TODO: check version? */
290 DRM_DEBUG_DRIVER("ATIF version %u\n", output.version);
291
292 amdgpu_atif_parse_notification(&atif->notifications, output.notification_mask);
293 amdgpu_atif_parse_functions(&atif->functions, output.function_bits);
294
295 kfree(info);
296 return err;
297 }
298
299 /**
300 * amdgpu_atif_get_notification_params - determine notify configuration
301 *
302 * @atif: acpi handle
303 *
304 * Execute the ATIF_FUNCTION_GET_SYSTEM_PARAMETERS ATIF function
305 * to determine if a notifier is used and if so which one
306 * (all asics). This is either Notify(VGA, 0x81) or Notify(VGA, n)
307 * where n is specified in the result if a notifier is used.
308 * Returns 0 on success, error on failure.
309 */
310 static noinline_for_stack
amdgpu_atif_get_notification_params(struct amdgpu_atif * atif)311 int amdgpu_atif_get_notification_params(struct amdgpu_atif *atif)
312 {
313 union acpi_object *info;
314 struct amdgpu_atif_notification_cfg *n = &atif->notification_cfg;
315 struct atif_system_params params;
316 size_t size;
317 int err = 0;
318
319 info = amdgpu_atif_call(atif, ATIF_FUNCTION_GET_SYSTEM_PARAMETERS,
320 NULL, offsetof(struct atif_system_params, command_code));
321 if (!info) {
322 err = -EIO;
323 goto out;
324 }
325
326 memset(¶ms, 0, sizeof(params));
327 size = min(sizeof(params), (size_t)*(u16 *)info->buffer.pointer);
328 memcpy(¶ms, info->buffer.pointer, size);
329
330 DRM_DEBUG_DRIVER("SYSTEM_PARAMS: mask = %#x, flags = %#x\n",
331 params.flags, params.valid_mask);
332 params.flags = params.flags & params.valid_mask;
333
334 if ((params.flags & ATIF_NOTIFY_MASK) == ATIF_NOTIFY_NONE) {
335 n->enabled = false;
336 n->command_code = 0;
337 } else if ((params.flags & ATIF_NOTIFY_MASK) == ATIF_NOTIFY_81) {
338 n->enabled = true;
339 n->command_code = 0x81;
340 } else {
341 if (size < 11) {
342 err = -EINVAL;
343 goto out;
344 }
345 n->enabled = true;
346 n->command_code = params.command_code;
347 }
348
349 out:
350 DRM_DEBUG_DRIVER("Notification %s, command code = %#x\n",
351 (n->enabled ? "enabled" : "disabled"),
352 n->command_code);
353 kfree(info);
354 return err;
355 }
356
357 /**
358 * amdgpu_atif_query_backlight_caps - get min and max backlight input signal
359 *
360 * @atif: acpi handle
361 *
362 * Execute the QUERY_BRIGHTNESS_TRANSFER_CHARACTERISTICS ATIF function
363 * to determine the acceptable range of backlight values
364 *
365 * Backlight_caps.caps_valid will be set to true if the query is successful
366 *
367 * The input signals are in range 0-255
368 *
369 * This function assumes the display with backlight is the first LCD
370 *
371 * Returns 0 on success, error on failure.
372 */
373 static noinline_for_stack
amdgpu_atif_query_backlight_caps(struct amdgpu_atif * atif)374 int amdgpu_atif_query_backlight_caps(struct amdgpu_atif *atif)
375 {
376 union acpi_object *info;
377 struct atif_qbtc_output characteristics;
378 struct atif_qbtc_arguments arguments;
379 struct acpi_buffer params;
380 size_t size;
381 int err = 0;
382
383 arguments.size = sizeof(arguments);
384 arguments.requested_display = ATIF_QBTC_REQUEST_LCD1;
385
386 params.length = sizeof(arguments);
387 params.pointer = (void *)&arguments;
388
389 info = amdgpu_atif_call(atif,
390 ATIF_FUNCTION_QUERY_BRIGHTNESS_TRANSFER_CHARACTERISTICS,
391 ¶ms, offsetof(struct atif_qbtc_output, data_points));
392 if (!info) {
393 err = -EIO;
394 goto out;
395 }
396
397 memset(&characteristics, 0, sizeof(characteristics));
398 size = min(sizeof(characteristics), (size_t)*(u16 *)info->buffer.pointer);
399 memcpy(&characteristics, info->buffer.pointer, size);
400
401 atif->backlight_caps.caps_valid = true;
402 atif->backlight_caps.min_input_signal =
403 characteristics.min_input_signal;
404 atif->backlight_caps.max_input_signal =
405 characteristics.max_input_signal;
406 atif->backlight_caps.ac_level = characteristics.ac_level;
407 atif->backlight_caps.dc_level = characteristics.dc_level;
408 atif->backlight_caps.data_points = characteristics.number_of_points;
409 memcpy(atif->backlight_caps.luminance_data,
410 characteristics.data_points,
411 sizeof(atif->backlight_caps.luminance_data));
412 out:
413 kfree(info);
414 return err;
415 }
416
417 /**
418 * amdgpu_atif_get_sbios_requests - get requested sbios event
419 *
420 * @atif: acpi handle
421 * @req: atif sbios request struct
422 *
423 * Execute the ATIF_FUNCTION_GET_SYSTEM_BIOS_REQUESTS ATIF function
424 * to determine what requests the sbios is making to the driver
425 * (all asics).
426 * Returns 0 on success, error on failure.
427 */
amdgpu_atif_get_sbios_requests(struct amdgpu_atif * atif,struct atif_sbios_requests * req)428 static int amdgpu_atif_get_sbios_requests(struct amdgpu_atif *atif,
429 struct atif_sbios_requests *req)
430 {
431 union acpi_object *info;
432 size_t size;
433 int count = 0;
434
435 info = amdgpu_atif_call(atif, ATIF_FUNCTION_GET_SYSTEM_BIOS_REQUESTS,
436 NULL, sizeof(*req));
437 if (!info)
438 return -EIO;
439
440 memset(req, 0, sizeof(*req));
441
442 size = min(sizeof(*req), (size_t)*(u16 *)info->buffer.pointer);
443 memcpy(req, info->buffer.pointer, size);
444 DRM_DEBUG_DRIVER("SBIOS pending requests: %#x\n", req->pending);
445
446 count = hweight32(req->pending);
447
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 acpi_dev_put(acpi_dev);
1172 return -ENOMEM;
1173 }
1174
1175 INIT_LIST_HEAD(&xcc_info->list);
1176 xcc_info->handle = acpi_device_handle(acpi_dev);
1177 acpi_dev_put(acpi_dev);
1178
1179 ret = amdgpu_acpi_get_xcc_info(xcc_info, &sbdf);
1180 if (ret) {
1181 kfree(xcc_info);
1182 continue;
1183 }
1184
1185 dev_info = amdgpu_acpi_get_dev(sbdf);
1186
1187 if (!dev_info)
1188 ret = amdgpu_acpi_dev_init(&dev_info, xcc_info, sbdf);
1189
1190 if (ret == -ENOMEM) {
1191 kfree(xcc_info);
1192 return ret;
1193 }
1194
1195 if (!dev_info) {
1196 kfree(xcc_info);
1197 continue;
1198 }
1199
1200 list_add_tail(&xcc_info->list, &dev_info->xcc_list);
1201 }
1202
1203 return 0;
1204 }
1205
amdgpu_acpi_get_tmr_info(struct amdgpu_device * adev,u64 * tmr_offset,u64 * tmr_size)1206 int amdgpu_acpi_get_tmr_info(struct amdgpu_device *adev, u64 *tmr_offset,
1207 u64 *tmr_size)
1208 {
1209 struct amdgpu_acpi_dev_info *dev_info;
1210 u32 sbdf;
1211
1212 if (!tmr_offset || !tmr_size)
1213 return -EINVAL;
1214
1215 sbdf = (pci_domain_nr(adev->pdev->bus) << 16);
1216 sbdf |= pci_dev_id(adev->pdev);
1217 dev_info = amdgpu_acpi_get_dev(sbdf);
1218 if (!dev_info)
1219 return -ENOENT;
1220
1221 *tmr_offset = dev_info->tmr_base;
1222 *tmr_size = dev_info->tmr_size;
1223
1224 return 0;
1225 }
1226
amdgpu_acpi_get_mem_info(struct amdgpu_device * adev,int xcc_id,struct amdgpu_numa_info * numa_info)1227 int amdgpu_acpi_get_mem_info(struct amdgpu_device *adev, int xcc_id,
1228 struct amdgpu_numa_info *numa_info)
1229 {
1230 struct amdgpu_acpi_dev_info *dev_info;
1231 struct amdgpu_acpi_xcc_info *xcc_info;
1232 u32 sbdf;
1233
1234 if (!numa_info)
1235 return -EINVAL;
1236
1237 sbdf = (pci_domain_nr(adev->pdev->bus) << 16);
1238 sbdf |= pci_dev_id(adev->pdev);
1239 dev_info = amdgpu_acpi_get_dev(sbdf);
1240 if (!dev_info)
1241 return -ENOENT;
1242
1243 list_for_each_entry(xcc_info, &dev_info->xcc_list, list) {
1244 if (xcc_info->phy_id == xcc_id) {
1245 memcpy(numa_info, xcc_info->numa_info,
1246 sizeof(*numa_info));
1247 return 0;
1248 }
1249 }
1250
1251 return -ENOENT;
1252 }
1253
1254 /**
1255 * amdgpu_acpi_event - handle notify events
1256 *
1257 * @nb: notifier block
1258 * @val: val
1259 * @data: acpi event
1260 *
1261 * Calls relevant amdgpu functions in response to various
1262 * acpi events.
1263 * Returns NOTIFY code
1264 */
amdgpu_acpi_event(struct notifier_block * nb,unsigned long val,void * data)1265 static int amdgpu_acpi_event(struct notifier_block *nb,
1266 unsigned long val,
1267 void *data)
1268 {
1269 struct amdgpu_device *adev = container_of(nb, struct amdgpu_device, acpi_nb);
1270 struct acpi_bus_event *entry = (struct acpi_bus_event *)data;
1271
1272 if (strcmp(entry->device_class, ACPI_AC_CLASS) == 0) {
1273 if (power_supply_is_system_supplied() > 0)
1274 DRM_DEBUG_DRIVER("pm: AC\n");
1275 else
1276 DRM_DEBUG_DRIVER("pm: DC\n");
1277
1278 amdgpu_pm_acpi_event_handler(adev);
1279 }
1280
1281 /* Check for pending SBIOS requests */
1282 return amdgpu_atif_handler(adev, entry);
1283 }
1284
1285 /* Call all ACPI methods here */
1286 /**
1287 * amdgpu_acpi_init - init driver acpi support
1288 *
1289 * @adev: amdgpu_device pointer
1290 *
1291 * Verifies the AMD ACPI interfaces and registers with the acpi
1292 * notifier chain (all asics).
1293 * Returns 0 on success, error on failure.
1294 */
amdgpu_acpi_init(struct amdgpu_device * adev)1295 int amdgpu_acpi_init(struct amdgpu_device *adev)
1296 {
1297 struct amdgpu_atif *atif = &amdgpu_acpi_priv.atif;
1298
1299 if (atif->notifications.brightness_change) {
1300 if (adev->dc_enabled) {
1301 #if defined(CONFIG_DRM_AMD_DC)
1302 struct amdgpu_display_manager *dm = &adev->dm;
1303
1304 if (dm->backlight_dev[0])
1305 atif->bd = dm->backlight_dev[0];
1306 #endif
1307 } else {
1308 struct drm_encoder *tmp;
1309
1310 /* Find the encoder controlling the brightness */
1311 list_for_each_entry(tmp, &adev_to_drm(adev)->mode_config.encoder_list,
1312 head) {
1313 struct amdgpu_encoder *enc = to_amdgpu_encoder(tmp);
1314
1315 if ((enc->devices & (ATOM_DEVICE_LCD_SUPPORT)) &&
1316 enc->enc_priv) {
1317 struct amdgpu_encoder_atom_dig *dig = enc->enc_priv;
1318
1319 if (dig->bl_dev) {
1320 atif->bd = dig->bl_dev;
1321 break;
1322 }
1323 }
1324 }
1325 }
1326 }
1327 adev->acpi_nb.notifier_call = amdgpu_acpi_event;
1328 register_acpi_notifier(&adev->acpi_nb);
1329
1330 return 0;
1331 }
1332
amdgpu_acpi_get_backlight_caps(struct amdgpu_dm_backlight_caps * caps)1333 void amdgpu_acpi_get_backlight_caps(struct amdgpu_dm_backlight_caps *caps)
1334 {
1335 struct amdgpu_atif *atif = &amdgpu_acpi_priv.atif;
1336
1337 memcpy(caps, &atif->backlight_caps, sizeof(*caps));
1338 }
1339
1340 /**
1341 * amdgpu_acpi_fini - tear down driver acpi support
1342 *
1343 * @adev: amdgpu_device pointer
1344 *
1345 * Unregisters with the acpi notifier chain (all asics).
1346 */
amdgpu_acpi_fini(struct amdgpu_device * adev)1347 void amdgpu_acpi_fini(struct amdgpu_device *adev)
1348 {
1349 unregister_acpi_notifier(&adev->acpi_nb);
1350 }
1351
1352 /**
1353 * amdgpu_atif_pci_probe_handle - look up the ATIF handle
1354 *
1355 * @pdev: pci device
1356 *
1357 * Look up the ATIF handles (all asics).
1358 * Returns true if the handle is found, false if not.
1359 */
amdgpu_atif_pci_probe_handle(struct pci_dev * pdev)1360 static bool amdgpu_atif_pci_probe_handle(struct pci_dev *pdev)
1361 {
1362 char acpi_method_name[255] = { 0 };
1363 struct acpi_buffer buffer = {sizeof(acpi_method_name), acpi_method_name};
1364 acpi_handle dhandle, atif_handle;
1365 acpi_status status;
1366 int ret;
1367
1368 dhandle = ACPI_HANDLE(&pdev->dev);
1369 if (!dhandle)
1370 return false;
1371
1372 status = acpi_get_handle(dhandle, "ATIF", &atif_handle);
1373 if (ACPI_FAILURE(status))
1374 return false;
1375
1376 amdgpu_acpi_priv.atif.handle = atif_handle;
1377 acpi_get_name(amdgpu_acpi_priv.atif.handle, ACPI_FULL_PATHNAME, &buffer);
1378 DRM_DEBUG_DRIVER("Found ATIF handle %s\n", acpi_method_name);
1379 ret = amdgpu_atif_verify_interface(&amdgpu_acpi_priv.atif);
1380 if (ret) {
1381 amdgpu_acpi_priv.atif.handle = 0;
1382 return false;
1383 }
1384 return true;
1385 }
1386
1387 /**
1388 * amdgpu_atcs_pci_probe_handle - look up the ATCS handle
1389 *
1390 * @pdev: pci device
1391 *
1392 * Look up the ATCS handles (all asics).
1393 * Returns true if the handle is found, false if not.
1394 */
amdgpu_atcs_pci_probe_handle(struct pci_dev * pdev)1395 static bool amdgpu_atcs_pci_probe_handle(struct pci_dev *pdev)
1396 {
1397 char acpi_method_name[255] = { 0 };
1398 struct acpi_buffer buffer = { sizeof(acpi_method_name), acpi_method_name };
1399 acpi_handle dhandle, atcs_handle;
1400 acpi_status status;
1401 int ret;
1402
1403 dhandle = ACPI_HANDLE(&pdev->dev);
1404 if (!dhandle)
1405 return false;
1406
1407 status = acpi_get_handle(dhandle, "ATCS", &atcs_handle);
1408 if (ACPI_FAILURE(status))
1409 return false;
1410
1411 amdgpu_acpi_priv.atcs.handle = atcs_handle;
1412 acpi_get_name(amdgpu_acpi_priv.atcs.handle, ACPI_FULL_PATHNAME, &buffer);
1413 DRM_DEBUG_DRIVER("Found ATCS handle %s\n", acpi_method_name);
1414 ret = amdgpu_atcs_verify_interface(&amdgpu_acpi_priv.atcs);
1415 if (ret) {
1416 amdgpu_acpi_priv.atcs.handle = 0;
1417 return false;
1418 }
1419 return true;
1420 }
1421
1422
1423 /**
1424 * amdgpu_acpi_should_gpu_reset
1425 *
1426 * @adev: amdgpu_device_pointer
1427 *
1428 * returns true if should reset GPU, false if not
1429 */
amdgpu_acpi_should_gpu_reset(struct amdgpu_device * adev)1430 bool amdgpu_acpi_should_gpu_reset(struct amdgpu_device *adev)
1431 {
1432 if ((adev->flags & AMD_IS_APU) &&
1433 adev->gfx.imu.funcs) /* Not need to do mode2 reset for IMU enabled APUs */
1434 return false;
1435
1436 if ((adev->flags & AMD_IS_APU) &&
1437 amdgpu_acpi_is_s3_active(adev))
1438 return false;
1439
1440 if (amdgpu_sriov_vf(adev))
1441 return false;
1442
1443 #if IS_ENABLED(CONFIG_SUSPEND)
1444 return pm_suspend_target_state != PM_SUSPEND_TO_IDLE;
1445 #else
1446 return true;
1447 #endif
1448 }
1449
1450 /*
1451 * amdgpu_acpi_detect - detect ACPI ATIF/ATCS methods
1452 *
1453 * Check if we have the ATIF/ATCS methods and populate
1454 * the structures in the driver.
1455 */
amdgpu_acpi_detect(void)1456 void amdgpu_acpi_detect(void)
1457 {
1458 struct amdgpu_atif *atif = &amdgpu_acpi_priv.atif;
1459 struct amdgpu_atcs *atcs = &amdgpu_acpi_priv.atcs;
1460 struct pci_dev *pdev = NULL;
1461 int ret;
1462
1463 while ((pdev = pci_get_base_class(PCI_BASE_CLASS_DISPLAY, pdev))) {
1464 if ((pdev->class != PCI_CLASS_DISPLAY_VGA << 8) &&
1465 (pdev->class != PCI_CLASS_DISPLAY_OTHER << 8))
1466 continue;
1467
1468 if (!atif->handle)
1469 amdgpu_atif_pci_probe_handle(pdev);
1470 if (!atcs->handle)
1471 amdgpu_atcs_pci_probe_handle(pdev);
1472 }
1473
1474 if (atif->functions.sbios_requests && !atif->functions.system_params) {
1475 /* XXX check this workraround, if sbios request function is
1476 * present we have to see how it's configured in the system
1477 * params
1478 */
1479 atif->functions.system_params = true;
1480 }
1481
1482 if (atif->functions.system_params) {
1483 ret = amdgpu_atif_get_notification_params(atif);
1484 if (ret) {
1485 DRM_DEBUG_DRIVER("Call to GET_SYSTEM_PARAMS failed: %d\n",
1486 ret);
1487 /* Disable notification */
1488 atif->notification_cfg.enabled = false;
1489 }
1490 }
1491
1492 if (atif->functions.query_backlight_transfer_characteristics) {
1493 ret = amdgpu_atif_query_backlight_caps(atif);
1494 if (ret) {
1495 DRM_DEBUG_DRIVER("Call to QUERY_BACKLIGHT_TRANSFER_CHARACTERISTICS failed: %d\n",
1496 ret);
1497 atif->backlight_caps.caps_valid = false;
1498 }
1499 } else {
1500 atif->backlight_caps.caps_valid = false;
1501 }
1502
1503 amdgpu_acpi_enumerate_xcc();
1504 }
1505
amdgpu_acpi_release(void)1506 void amdgpu_acpi_release(void)
1507 {
1508 struct amdgpu_acpi_dev_info *dev_info, *dev_tmp;
1509 struct amdgpu_acpi_xcc_info *xcc_info, *xcc_tmp;
1510 struct amdgpu_numa_info *numa_info;
1511 unsigned long index;
1512
1513 xa_for_each(&numa_info_xa, index, numa_info) {
1514 kfree(numa_info);
1515 xa_erase(&numa_info_xa, index);
1516 }
1517
1518 if (list_empty(&amdgpu_acpi_dev_list))
1519 return;
1520
1521 list_for_each_entry_safe(dev_info, dev_tmp, &amdgpu_acpi_dev_list,
1522 list) {
1523 list_for_each_entry_safe(xcc_info, xcc_tmp, &dev_info->xcc_list,
1524 list) {
1525 list_del(&xcc_info->list);
1526 kfree(xcc_info);
1527 }
1528
1529 list_del(&dev_info->list);
1530 kfree(dev_info);
1531 }
1532 }
1533
1534 #if IS_ENABLED(CONFIG_SUSPEND)
1535 /**
1536 * amdgpu_acpi_is_s3_active
1537 *
1538 * @adev: amdgpu_device_pointer
1539 *
1540 * returns true if supported, false if not.
1541 */
amdgpu_acpi_is_s3_active(struct amdgpu_device * adev)1542 bool amdgpu_acpi_is_s3_active(struct amdgpu_device *adev)
1543 {
1544 return !(adev->flags & AMD_IS_APU) ||
1545 (pm_suspend_target_state == PM_SUSPEND_MEM);
1546 }
1547
1548 /**
1549 * amdgpu_acpi_is_s0ix_active
1550 *
1551 * @adev: amdgpu_device_pointer
1552 *
1553 * returns true if supported, false if not.
1554 */
amdgpu_acpi_is_s0ix_active(struct amdgpu_device * adev)1555 bool amdgpu_acpi_is_s0ix_active(struct amdgpu_device *adev)
1556 {
1557 if (!(adev->flags & AMD_IS_APU) ||
1558 (pm_suspend_target_state != PM_SUSPEND_TO_IDLE))
1559 return false;
1560
1561 if (adev->asic_type < CHIP_RAVEN)
1562 return false;
1563
1564 if (!(adev->pm.pp_feature & PP_GFXOFF_MASK))
1565 return false;
1566
1567 /*
1568 * If ACPI_FADT_LOW_POWER_S0 is not set in the FADT, it is generally
1569 * risky to do any special firmware-related preparations for entering
1570 * S0ix even though the system is suspending to idle, so return false
1571 * in that case.
1572 */
1573 if (!(acpi_gbl_FADT.flags & ACPI_FADT_LOW_POWER_S0)) {
1574 dev_err_once(adev->dev,
1575 "Power consumption will be higher as BIOS has not been configured for suspend-to-idle.\n"
1576 "To use suspend-to-idle change the sleep mode in BIOS setup.\n");
1577 return false;
1578 }
1579
1580 #if !IS_ENABLED(CONFIG_AMD_PMC)
1581 dev_err_once(adev->dev,
1582 "Power consumption will be higher as the kernel has not been compiled with CONFIG_AMD_PMC.\n");
1583 return false;
1584 #else
1585 return true;
1586 #endif /* CONFIG_AMD_PMC */
1587 }
1588 #endif /* CONFIG_SUSPEND */
1589
1590 #if IS_ENABLED(CONFIG_DRM_AMD_ISP)
1591 static const struct acpi_device_id isp_sensor_ids[] = {
1592 { "OMNI5C10" },
1593 { }
1594 };
1595
isp_match_acpi_device_ids(struct device * dev,const void * data)1596 static int isp_match_acpi_device_ids(struct device *dev, const void *data)
1597 {
1598 return acpi_match_device(data, dev) ? 1 : 0;
1599 }
1600
amdgpu_acpi_get_isp4_dev(struct acpi_device ** dev)1601 int amdgpu_acpi_get_isp4_dev(struct acpi_device **dev)
1602 {
1603 struct device *pdev __free(put_device) = NULL;
1604 struct acpi_device *acpi_pdev;
1605
1606 pdev = bus_find_device(&platform_bus_type, NULL, isp_sensor_ids,
1607 isp_match_acpi_device_ids);
1608 if (!pdev)
1609 return -EINVAL;
1610
1611 acpi_pdev = ACPI_COMPANION(pdev);
1612 if (!acpi_pdev)
1613 return -ENODEV;
1614
1615 *dev = acpi_pdev;
1616
1617 return 0;
1618 }
1619 #endif /* CONFIG_DRM_AMD_ISP */
1620