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