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