Loading...
1/*
2 * Copyright 2012 Advanced Micro Devices, Inc.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
21 *
22 */
23
24#include <linux/pci.h>
25#include <linux/acpi.h>
26#include <linux/slab.h>
27#include <linux/power_supply.h>
28#include <linux/pm_runtime.h>
29#include <acpi/video.h>
30
31#include <drm/drm_crtc_helper.h>
32#include "amdgpu.h"
33#include "amdgpu_pm.h"
34#include "amdgpu_display.h"
35#include "amd_acpi.h"
36#include "atom.h"
37
38struct amdgpu_atif_notification_cfg {
39 bool enabled;
40 int command_code;
41};
42
43struct amdgpu_atif_notifications {
44 bool thermal_state;
45 bool forced_power_state;
46 bool system_power_state;
47 bool brightness_change;
48 bool dgpu_display_event;
49 bool gpu_package_power_limit;
50};
51
52struct amdgpu_atif_functions {
53 bool system_params;
54 bool sbios_requests;
55 bool temperature_change;
56 bool query_backlight_transfer_characteristics;
57 bool ready_to_undock;
58 bool external_gpu_information;
59};
60
61struct amdgpu_atif {
62 acpi_handle handle;
63
64 struct amdgpu_atif_notifications notifications;
65 struct amdgpu_atif_functions functions;
66 struct amdgpu_atif_notification_cfg notification_cfg;
67#if defined(CONFIG_BACKLIGHT_CLASS_DEVICE) || defined(CONFIG_BACKLIGHT_CLASS_DEVICE_MODULE)
68 struct backlight_device *bd;
69#endif
70 struct amdgpu_dm_backlight_caps backlight_caps;
71};
72
73/* Call the ATIF method
74 */
75/**
76 * amdgpu_atif_call - call an ATIF method
77 *
78 * @handle: acpi handle
79 * @function: the ATIF function to execute
80 * @params: ATIF function params
81 *
82 * Executes the requested ATIF function (all asics).
83 * Returns a pointer to the acpi output buffer.
84 */
85static union acpi_object *amdgpu_atif_call(struct amdgpu_atif *atif,
86 int function,
87 struct acpi_buffer *params)
88{
89 acpi_status status;
90 union acpi_object atif_arg_elements[2];
91 struct acpi_object_list atif_arg;
92 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
93
94 atif_arg.count = 2;
95 atif_arg.pointer = &atif_arg_elements[0];
96
97 atif_arg_elements[0].type = ACPI_TYPE_INTEGER;
98 atif_arg_elements[0].integer.value = function;
99
100 if (params) {
101 atif_arg_elements[1].type = ACPI_TYPE_BUFFER;
102 atif_arg_elements[1].buffer.length = params->length;
103 atif_arg_elements[1].buffer.pointer = params->pointer;
104 } else {
105 /* We need a second fake parameter */
106 atif_arg_elements[1].type = ACPI_TYPE_INTEGER;
107 atif_arg_elements[1].integer.value = 0;
108 }
109
110 status = acpi_evaluate_object(atif->handle, NULL, &atif_arg,
111 &buffer);
112
113 /* Fail only if calling the method fails and ATIF is supported */
114 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
115 DRM_DEBUG_DRIVER("failed to evaluate ATIF got %s\n",
116 acpi_format_exception(status));
117 kfree(buffer.pointer);
118 return NULL;
119 }
120
121 return buffer.pointer;
122}
123
124/**
125 * amdgpu_atif_parse_notification - parse supported notifications
126 *
127 * @n: supported notifications struct
128 * @mask: supported notifications mask from ATIF
129 *
130 * Use the supported notifications mask from ATIF function
131 * ATIF_FUNCTION_VERIFY_INTERFACE to determine what notifications
132 * are supported (all asics).
133 */
134static void amdgpu_atif_parse_notification(struct amdgpu_atif_notifications *n, u32 mask)
135{
136 n->thermal_state = mask & ATIF_THERMAL_STATE_CHANGE_REQUEST_SUPPORTED;
137 n->forced_power_state = mask & ATIF_FORCED_POWER_STATE_CHANGE_REQUEST_SUPPORTED;
138 n->system_power_state = mask & ATIF_SYSTEM_POWER_SOURCE_CHANGE_REQUEST_SUPPORTED;
139 n->brightness_change = mask & ATIF_PANEL_BRIGHTNESS_CHANGE_REQUEST_SUPPORTED;
140 n->dgpu_display_event = mask & ATIF_DGPU_DISPLAY_EVENT_SUPPORTED;
141 n->gpu_package_power_limit = mask & ATIF_GPU_PACKAGE_POWER_LIMIT_REQUEST_SUPPORTED;
142}
143
144/**
145 * amdgpu_atif_parse_functions - parse supported functions
146 *
147 * @f: supported functions struct
148 * @mask: supported functions mask from ATIF
149 *
150 * Use the supported functions mask from ATIF function
151 * ATIF_FUNCTION_VERIFY_INTERFACE to determine what functions
152 * are supported (all asics).
153 */
154static void amdgpu_atif_parse_functions(struct amdgpu_atif_functions *f, u32 mask)
155{
156 f->system_params = mask & ATIF_GET_SYSTEM_PARAMETERS_SUPPORTED;
157 f->sbios_requests = mask & ATIF_GET_SYSTEM_BIOS_REQUESTS_SUPPORTED;
158 f->temperature_change = mask & ATIF_TEMPERATURE_CHANGE_NOTIFICATION_SUPPORTED;
159 f->query_backlight_transfer_characteristics =
160 mask & ATIF_QUERY_BACKLIGHT_TRANSFER_CHARACTERISTICS_SUPPORTED;
161 f->ready_to_undock = mask & ATIF_READY_TO_UNDOCK_NOTIFICATION_SUPPORTED;
162 f->external_gpu_information = mask & ATIF_GET_EXTERNAL_GPU_INFORMATION_SUPPORTED;
163}
164
165/**
166 * amdgpu_atif_verify_interface - verify ATIF
167 *
168 * @handle: acpi handle
169 * @atif: amdgpu atif struct
170 *
171 * Execute the ATIF_FUNCTION_VERIFY_INTERFACE ATIF function
172 * to initialize ATIF and determine what features are supported
173 * (all asics).
174 * returns 0 on success, error on failure.
175 */
176static int amdgpu_atif_verify_interface(struct amdgpu_atif *atif)
177{
178 union acpi_object *info;
179 struct atif_verify_interface output;
180 size_t size;
181 int err = 0;
182
183 info = amdgpu_atif_call(atif, ATIF_FUNCTION_VERIFY_INTERFACE, NULL);
184 if (!info)
185 return -EIO;
186
187 memset(&output, 0, sizeof(output));
188
189 size = *(u16 *) info->buffer.pointer;
190 if (size < 12) {
191 DRM_INFO("ATIF buffer is too small: %zu\n", size);
192 err = -EINVAL;
193 goto out;
194 }
195 size = min(sizeof(output), size);
196
197 memcpy(&output, info->buffer.pointer, size);
198
199 /* TODO: check version? */
200 DRM_DEBUG_DRIVER("ATIF version %u\n", output.version);
201
202 amdgpu_atif_parse_notification(&atif->notifications, output.notification_mask);
203 amdgpu_atif_parse_functions(&atif->functions, output.function_bits);
204
205out:
206 kfree(info);
207 return err;
208}
209
210static acpi_handle amdgpu_atif_probe_handle(acpi_handle dhandle)
211{
212 acpi_handle handle = NULL;
213 char acpi_method_name[255] = { 0 };
214 struct acpi_buffer buffer = { sizeof(acpi_method_name), acpi_method_name };
215 acpi_status status;
216
217 /* For PX/HG systems, ATIF and ATPX are in the iGPU's namespace, on dGPU only
218 * systems, ATIF is in the dGPU's namespace.
219 */
220 status = acpi_get_handle(dhandle, "ATIF", &handle);
221 if (ACPI_SUCCESS(status))
222 goto out;
223
224 if (amdgpu_has_atpx()) {
225 status = acpi_get_handle(amdgpu_atpx_get_dhandle(), "ATIF",
226 &handle);
227 if (ACPI_SUCCESS(status))
228 goto out;
229 }
230
231 DRM_DEBUG_DRIVER("No ATIF handle found\n");
232 return NULL;
233out:
234 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
235 DRM_DEBUG_DRIVER("Found ATIF handle %s\n", acpi_method_name);
236 return handle;
237}
238
239/**
240 * amdgpu_atif_get_notification_params - determine notify configuration
241 *
242 * @handle: acpi handle
243 * @n: atif notification configuration struct
244 *
245 * Execute the ATIF_FUNCTION_GET_SYSTEM_PARAMETERS ATIF function
246 * to determine if a notifier is used and if so which one
247 * (all asics). This is either Notify(VGA, 0x81) or Notify(VGA, n)
248 * where n is specified in the result if a notifier is used.
249 * Returns 0 on success, error on failure.
250 */
251static int amdgpu_atif_get_notification_params(struct amdgpu_atif *atif)
252{
253 union acpi_object *info;
254 struct amdgpu_atif_notification_cfg *n = &atif->notification_cfg;
255 struct atif_system_params params;
256 size_t size;
257 int err = 0;
258
259 info = amdgpu_atif_call(atif, ATIF_FUNCTION_GET_SYSTEM_PARAMETERS,
260 NULL);
261 if (!info) {
262 err = -EIO;
263 goto out;
264 }
265
266 size = *(u16 *) info->buffer.pointer;
267 if (size < 10) {
268 err = -EINVAL;
269 goto out;
270 }
271
272 memset(¶ms, 0, sizeof(params));
273 size = min(sizeof(params), size);
274 memcpy(¶ms, info->buffer.pointer, size);
275
276 DRM_DEBUG_DRIVER("SYSTEM_PARAMS: mask = %#x, flags = %#x\n",
277 params.flags, params.valid_mask);
278 params.flags = params.flags & params.valid_mask;
279
280 if ((params.flags & ATIF_NOTIFY_MASK) == ATIF_NOTIFY_NONE) {
281 n->enabled = false;
282 n->command_code = 0;
283 } else if ((params.flags & ATIF_NOTIFY_MASK) == ATIF_NOTIFY_81) {
284 n->enabled = true;
285 n->command_code = 0x81;
286 } else {
287 if (size < 11) {
288 err = -EINVAL;
289 goto out;
290 }
291 n->enabled = true;
292 n->command_code = params.command_code;
293 }
294
295out:
296 DRM_DEBUG_DRIVER("Notification %s, command code = %#x\n",
297 (n->enabled ? "enabled" : "disabled"),
298 n->command_code);
299 kfree(info);
300 return err;
301}
302
303/**
304 * amdgpu_atif_query_backlight_caps - get min and max backlight input signal
305 *
306 * @handle: acpi handle
307 *
308 * Execute the QUERY_BRIGHTNESS_TRANSFER_CHARACTERISTICS ATIF function
309 * to determine the acceptable range of backlight values
310 *
311 * Backlight_caps.caps_valid will be set to true if the query is successful
312 *
313 * The input signals are in range 0-255
314 *
315 * This function assumes the display with backlight is the first LCD
316 *
317 * Returns 0 on success, error on failure.
318 */
319static int amdgpu_atif_query_backlight_caps(struct amdgpu_atif *atif)
320{
321 union acpi_object *info;
322 struct atif_qbtc_output characteristics;
323 struct atif_qbtc_arguments arguments;
324 struct acpi_buffer params;
325 size_t size;
326 int err = 0;
327
328 arguments.size = sizeof(arguments);
329 arguments.requested_display = ATIF_QBTC_REQUEST_LCD1;
330
331 params.length = sizeof(arguments);
332 params.pointer = (void *)&arguments;
333
334 info = amdgpu_atif_call(atif,
335 ATIF_FUNCTION_QUERY_BRIGHTNESS_TRANSFER_CHARACTERISTICS,
336 ¶ms);
337 if (!info) {
338 err = -EIO;
339 goto out;
340 }
341
342 size = *(u16 *) info->buffer.pointer;
343 if (size < 10) {
344 err = -EINVAL;
345 goto out;
346 }
347
348 memset(&characteristics, 0, sizeof(characteristics));
349 size = min(sizeof(characteristics), size);
350 memcpy(&characteristics, info->buffer.pointer, size);
351
352 atif->backlight_caps.caps_valid = true;
353 atif->backlight_caps.min_input_signal =
354 characteristics.min_input_signal;
355 atif->backlight_caps.max_input_signal =
356 characteristics.max_input_signal;
357out:
358 kfree(info);
359 return err;
360}
361
362/**
363 * amdgpu_atif_get_sbios_requests - get requested sbios event
364 *
365 * @handle: acpi handle
366 * @req: atif sbios request struct
367 *
368 * Execute the ATIF_FUNCTION_GET_SYSTEM_BIOS_REQUESTS ATIF function
369 * to determine what requests the sbios is making to the driver
370 * (all asics).
371 * Returns 0 on success, error on failure.
372 */
373static int amdgpu_atif_get_sbios_requests(struct amdgpu_atif *atif,
374 struct atif_sbios_requests *req)
375{
376 union acpi_object *info;
377 size_t size;
378 int count = 0;
379
380 info = amdgpu_atif_call(atif, ATIF_FUNCTION_GET_SYSTEM_BIOS_REQUESTS,
381 NULL);
382 if (!info)
383 return -EIO;
384
385 size = *(u16 *)info->buffer.pointer;
386 if (size < 0xd) {
387 count = -EINVAL;
388 goto out;
389 }
390 memset(req, 0, sizeof(*req));
391
392 size = min(sizeof(*req), size);
393 memcpy(req, info->buffer.pointer, size);
394 DRM_DEBUG_DRIVER("SBIOS pending requests: %#x\n", req->pending);
395
396 count = hweight32(req->pending);
397
398out:
399 kfree(info);
400 return count;
401}
402
403/**
404 * amdgpu_atif_handler - handle ATIF notify requests
405 *
406 * @adev: amdgpu_device pointer
407 * @event: atif sbios request struct
408 *
409 * Checks the acpi event and if it matches an atif event,
410 * handles it.
411 *
412 * Returns:
413 * NOTIFY_BAD or NOTIFY_DONE, depending on the event.
414 */
415static int amdgpu_atif_handler(struct amdgpu_device *adev,
416 struct acpi_bus_event *event)
417{
418 struct amdgpu_atif *atif = adev->atif;
419 int count;
420
421 DRM_DEBUG_DRIVER("event, device_class = %s, type = %#x\n",
422 event->device_class, event->type);
423
424 if (strcmp(event->device_class, ACPI_VIDEO_CLASS) != 0)
425 return NOTIFY_DONE;
426
427 /* Is this actually our event? */
428 if (!atif ||
429 !atif->notification_cfg.enabled ||
430 event->type != atif->notification_cfg.command_code) {
431 /* These events will generate keypresses otherwise */
432 if (event->type == ACPI_VIDEO_NOTIFY_PROBE)
433 return NOTIFY_BAD;
434 else
435 return NOTIFY_DONE;
436 }
437
438 if (atif->functions.sbios_requests) {
439 struct atif_sbios_requests req;
440
441 /* Check pending SBIOS requests */
442 count = amdgpu_atif_get_sbios_requests(atif, &req);
443
444 if (count <= 0)
445 return NOTIFY_BAD;
446
447 DRM_DEBUG_DRIVER("ATIF: %d pending SBIOS requests\n", count);
448
449 if (req.pending & ATIF_PANEL_BRIGHTNESS_CHANGE_REQUEST) {
450#if defined(CONFIG_BACKLIGHT_CLASS_DEVICE) || defined(CONFIG_BACKLIGHT_CLASS_DEVICE_MODULE)
451 if (atif->bd) {
452 DRM_DEBUG_DRIVER("Changing brightness to %d\n",
453 req.backlight_level);
454 /*
455 * XXX backlight_device_set_brightness() is
456 * hardwired to post BACKLIGHT_UPDATE_SYSFS.
457 * It probably should accept 'reason' parameter.
458 */
459 backlight_device_set_brightness(atif->bd, req.backlight_level);
460 }
461#endif
462 }
463
464 if (req.pending & ATIF_DGPU_DISPLAY_EVENT) {
465 if (adev->flags & AMD_IS_PX) {
466 pm_runtime_get_sync(adev->ddev->dev);
467 /* Just fire off a uevent and let userspace tell us what to do */
468 drm_helper_hpd_irq_event(adev->ddev);
469 pm_runtime_mark_last_busy(adev->ddev->dev);
470 pm_runtime_put_autosuspend(adev->ddev->dev);
471 }
472 }
473 /* TODO: check other events */
474 }
475
476 /* We've handled the event, stop the notifier chain. The ACPI interface
477 * overloads ACPI_VIDEO_NOTIFY_PROBE, we don't want to send that to
478 * userspace if the event was generated only to signal a SBIOS
479 * request.
480 */
481 return NOTIFY_BAD;
482}
483
484/* Call the ATCS method
485 */
486/**
487 * amdgpu_atcs_call - call an ATCS method
488 *
489 * @handle: acpi handle
490 * @function: the ATCS function to execute
491 * @params: ATCS function params
492 *
493 * Executes the requested ATCS function (all asics).
494 * Returns a pointer to the acpi output buffer.
495 */
496static union acpi_object *amdgpu_atcs_call(acpi_handle handle, int function,
497 struct acpi_buffer *params)
498{
499 acpi_status status;
500 union acpi_object atcs_arg_elements[2];
501 struct acpi_object_list atcs_arg;
502 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
503
504 atcs_arg.count = 2;
505 atcs_arg.pointer = &atcs_arg_elements[0];
506
507 atcs_arg_elements[0].type = ACPI_TYPE_INTEGER;
508 atcs_arg_elements[0].integer.value = function;
509
510 if (params) {
511 atcs_arg_elements[1].type = ACPI_TYPE_BUFFER;
512 atcs_arg_elements[1].buffer.length = params->length;
513 atcs_arg_elements[1].buffer.pointer = params->pointer;
514 } else {
515 /* We need a second fake parameter */
516 atcs_arg_elements[1].type = ACPI_TYPE_INTEGER;
517 atcs_arg_elements[1].integer.value = 0;
518 }
519
520 status = acpi_evaluate_object(handle, "ATCS", &atcs_arg, &buffer);
521
522 /* Fail only if calling the method fails and ATIF is supported */
523 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
524 DRM_DEBUG_DRIVER("failed to evaluate ATCS got %s\n",
525 acpi_format_exception(status));
526 kfree(buffer.pointer);
527 return NULL;
528 }
529
530 return buffer.pointer;
531}
532
533/**
534 * amdgpu_atcs_parse_functions - parse supported functions
535 *
536 * @f: supported functions struct
537 * @mask: supported functions mask from ATCS
538 *
539 * Use the supported functions mask from ATCS function
540 * ATCS_FUNCTION_VERIFY_INTERFACE to determine what functions
541 * are supported (all asics).
542 */
543static void amdgpu_atcs_parse_functions(struct amdgpu_atcs_functions *f, u32 mask)
544{
545 f->get_ext_state = mask & ATCS_GET_EXTERNAL_STATE_SUPPORTED;
546 f->pcie_perf_req = mask & ATCS_PCIE_PERFORMANCE_REQUEST_SUPPORTED;
547 f->pcie_dev_rdy = mask & ATCS_PCIE_DEVICE_READY_NOTIFICATION_SUPPORTED;
548 f->pcie_bus_width = mask & ATCS_SET_PCIE_BUS_WIDTH_SUPPORTED;
549}
550
551/**
552 * amdgpu_atcs_verify_interface - verify ATCS
553 *
554 * @handle: acpi handle
555 * @atcs: amdgpu atcs struct
556 *
557 * Execute the ATCS_FUNCTION_VERIFY_INTERFACE ATCS function
558 * to initialize ATCS and determine what features are supported
559 * (all asics).
560 * returns 0 on success, error on failure.
561 */
562static int amdgpu_atcs_verify_interface(acpi_handle handle,
563 struct amdgpu_atcs *atcs)
564{
565 union acpi_object *info;
566 struct atcs_verify_interface output;
567 size_t size;
568 int err = 0;
569
570 info = amdgpu_atcs_call(handle, ATCS_FUNCTION_VERIFY_INTERFACE, NULL);
571 if (!info)
572 return -EIO;
573
574 memset(&output, 0, sizeof(output));
575
576 size = *(u16 *) info->buffer.pointer;
577 if (size < 8) {
578 DRM_INFO("ATCS buffer is too small: %zu\n", size);
579 err = -EINVAL;
580 goto out;
581 }
582 size = min(sizeof(output), size);
583
584 memcpy(&output, info->buffer.pointer, size);
585
586 /* TODO: check version? */
587 DRM_DEBUG_DRIVER("ATCS version %u\n", output.version);
588
589 amdgpu_atcs_parse_functions(&atcs->functions, output.function_bits);
590
591out:
592 kfree(info);
593 return err;
594}
595
596/**
597 * amdgpu_acpi_is_pcie_performance_request_supported
598 *
599 * @adev: amdgpu_device pointer
600 *
601 * Check if the ATCS pcie_perf_req and pcie_dev_rdy methods
602 * are supported (all asics).
603 * returns true if supported, false if not.
604 */
605bool amdgpu_acpi_is_pcie_performance_request_supported(struct amdgpu_device *adev)
606{
607 struct amdgpu_atcs *atcs = &adev->atcs;
608
609 if (atcs->functions.pcie_perf_req && atcs->functions.pcie_dev_rdy)
610 return true;
611
612 return false;
613}
614
615/**
616 * amdgpu_acpi_pcie_notify_device_ready
617 *
618 * @adev: amdgpu_device pointer
619 *
620 * Executes the PCIE_DEVICE_READY_NOTIFICATION method
621 * (all asics).
622 * returns 0 on success, error on failure.
623 */
624int amdgpu_acpi_pcie_notify_device_ready(struct amdgpu_device *adev)
625{
626 acpi_handle handle;
627 union acpi_object *info;
628 struct amdgpu_atcs *atcs = &adev->atcs;
629
630 /* Get the device handle */
631 handle = ACPI_HANDLE(&adev->pdev->dev);
632 if (!handle)
633 return -EINVAL;
634
635 if (!atcs->functions.pcie_dev_rdy)
636 return -EINVAL;
637
638 info = amdgpu_atcs_call(handle, ATCS_FUNCTION_PCIE_DEVICE_READY_NOTIFICATION, NULL);
639 if (!info)
640 return -EIO;
641
642 kfree(info);
643
644 return 0;
645}
646
647/**
648 * amdgpu_acpi_pcie_performance_request
649 *
650 * @adev: amdgpu_device pointer
651 * @perf_req: requested perf level (pcie gen speed)
652 * @advertise: set advertise caps flag if set
653 *
654 * Executes the PCIE_PERFORMANCE_REQUEST method to
655 * change the pcie gen speed (all asics).
656 * returns 0 on success, error on failure.
657 */
658int amdgpu_acpi_pcie_performance_request(struct amdgpu_device *adev,
659 u8 perf_req, bool advertise)
660{
661 acpi_handle handle;
662 union acpi_object *info;
663 struct amdgpu_atcs *atcs = &adev->atcs;
664 struct atcs_pref_req_input atcs_input;
665 struct atcs_pref_req_output atcs_output;
666 struct acpi_buffer params;
667 size_t size;
668 u32 retry = 3;
669
670 if (amdgpu_acpi_pcie_notify_device_ready(adev))
671 return -EINVAL;
672
673 /* Get the device handle */
674 handle = ACPI_HANDLE(&adev->pdev->dev);
675 if (!handle)
676 return -EINVAL;
677
678 if (!atcs->functions.pcie_perf_req)
679 return -EINVAL;
680
681 atcs_input.size = sizeof(struct atcs_pref_req_input);
682 /* client id (bit 2-0: func num, 7-3: dev num, 15-8: bus num) */
683 atcs_input.client_id = adev->pdev->devfn | (adev->pdev->bus->number << 8);
684 atcs_input.valid_flags_mask = ATCS_VALID_FLAGS_MASK;
685 atcs_input.flags = ATCS_WAIT_FOR_COMPLETION;
686 if (advertise)
687 atcs_input.flags |= ATCS_ADVERTISE_CAPS;
688 atcs_input.req_type = ATCS_PCIE_LINK_SPEED;
689 atcs_input.perf_req = perf_req;
690
691 params.length = sizeof(struct atcs_pref_req_input);
692 params.pointer = &atcs_input;
693
694 while (retry--) {
695 info = amdgpu_atcs_call(handle, ATCS_FUNCTION_PCIE_PERFORMANCE_REQUEST, ¶ms);
696 if (!info)
697 return -EIO;
698
699 memset(&atcs_output, 0, sizeof(atcs_output));
700
701 size = *(u16 *) info->buffer.pointer;
702 if (size < 3) {
703 DRM_INFO("ATCS buffer is too small: %zu\n", size);
704 kfree(info);
705 return -EINVAL;
706 }
707 size = min(sizeof(atcs_output), size);
708
709 memcpy(&atcs_output, info->buffer.pointer, size);
710
711 kfree(info);
712
713 switch (atcs_output.ret_val) {
714 case ATCS_REQUEST_REFUSED:
715 default:
716 return -EINVAL;
717 case ATCS_REQUEST_COMPLETE:
718 return 0;
719 case ATCS_REQUEST_IN_PROGRESS:
720 udelay(10);
721 break;
722 }
723 }
724
725 return 0;
726}
727
728/**
729 * amdgpu_acpi_event - handle notify events
730 *
731 * @nb: notifier block
732 * @val: val
733 * @data: acpi event
734 *
735 * Calls relevant amdgpu functions in response to various
736 * acpi events.
737 * Returns NOTIFY code
738 */
739static int amdgpu_acpi_event(struct notifier_block *nb,
740 unsigned long val,
741 void *data)
742{
743 struct amdgpu_device *adev = container_of(nb, struct amdgpu_device, acpi_nb);
744 struct acpi_bus_event *entry = (struct acpi_bus_event *)data;
745
746 if (strcmp(entry->device_class, ACPI_AC_CLASS) == 0) {
747 if (power_supply_is_system_supplied() > 0)
748 DRM_DEBUG_DRIVER("pm: AC\n");
749 else
750 DRM_DEBUG_DRIVER("pm: DC\n");
751
752 amdgpu_pm_acpi_event_handler(adev);
753 }
754
755 /* Check for pending SBIOS requests */
756 return amdgpu_atif_handler(adev, entry);
757}
758
759/* Call all ACPI methods here */
760/**
761 * amdgpu_acpi_init - init driver acpi support
762 *
763 * @adev: amdgpu_device pointer
764 *
765 * Verifies the AMD ACPI interfaces and registers with the acpi
766 * notifier chain (all asics).
767 * Returns 0 on success, error on failure.
768 */
769int amdgpu_acpi_init(struct amdgpu_device *adev)
770{
771 acpi_handle handle, atif_handle;
772 struct amdgpu_atif *atif;
773 struct amdgpu_atcs *atcs = &adev->atcs;
774 int ret;
775
776 /* Get the device handle */
777 handle = ACPI_HANDLE(&adev->pdev->dev);
778
779 if (!adev->bios || !handle)
780 return 0;
781
782 /* Call the ATCS method */
783 ret = amdgpu_atcs_verify_interface(handle, atcs);
784 if (ret) {
785 DRM_DEBUG_DRIVER("Call to ATCS verify_interface failed: %d\n", ret);
786 }
787
788 /* Probe for ATIF, and initialize it if found */
789 atif_handle = amdgpu_atif_probe_handle(handle);
790 if (!atif_handle)
791 goto out;
792
793 atif = kzalloc(sizeof(*atif), GFP_KERNEL);
794 if (!atif) {
795 DRM_WARN("Not enough memory to initialize ATIF\n");
796 goto out;
797 }
798 atif->handle = atif_handle;
799
800 /* Call the ATIF method */
801 ret = amdgpu_atif_verify_interface(atif);
802 if (ret) {
803 DRM_DEBUG_DRIVER("Call to ATIF verify_interface failed: %d\n", ret);
804 kfree(atif);
805 goto out;
806 }
807 adev->atif = atif;
808
809 if (atif->notifications.brightness_change) {
810#if defined(CONFIG_BACKLIGHT_CLASS_DEVICE) || defined(CONFIG_BACKLIGHT_CLASS_DEVICE_MODULE)
811 if (amdgpu_device_has_dc_support(adev)) {
812#if defined(CONFIG_DRM_AMD_DC)
813 struct amdgpu_display_manager *dm = &adev->dm;
814 atif->bd = dm->backlight_dev;
815#endif
816 } else {
817 struct drm_encoder *tmp;
818
819 /* Find the encoder controlling the brightness */
820 list_for_each_entry(tmp, &adev->ddev->mode_config.encoder_list,
821 head) {
822 struct amdgpu_encoder *enc = to_amdgpu_encoder(tmp);
823
824 if ((enc->devices & (ATOM_DEVICE_LCD_SUPPORT)) &&
825 enc->enc_priv) {
826 struct amdgpu_encoder_atom_dig *dig = enc->enc_priv;
827 if (dig->bl_dev) {
828 atif->bd = dig->bl_dev;
829 break;
830 }
831 }
832 }
833 }
834 }
835#endif
836
837 if (atif->functions.sbios_requests && !atif->functions.system_params) {
838 /* XXX check this workraround, if sbios request function is
839 * present we have to see how it's configured in the system
840 * params
841 */
842 atif->functions.system_params = true;
843 }
844
845 if (atif->functions.system_params) {
846 ret = amdgpu_atif_get_notification_params(atif);
847 if (ret) {
848 DRM_DEBUG_DRIVER("Call to GET_SYSTEM_PARAMS failed: %d\n",
849 ret);
850 /* Disable notification */
851 atif->notification_cfg.enabled = false;
852 }
853 }
854
855 if (atif->functions.query_backlight_transfer_characteristics) {
856 ret = amdgpu_atif_query_backlight_caps(atif);
857 if (ret) {
858 DRM_DEBUG_DRIVER("Call to QUERY_BACKLIGHT_TRANSFER_CHARACTERISTICS failed: %d\n",
859 ret);
860 atif->backlight_caps.caps_valid = false;
861 }
862 } else {
863 atif->backlight_caps.caps_valid = false;
864 }
865
866out:
867 adev->acpi_nb.notifier_call = amdgpu_acpi_event;
868 register_acpi_notifier(&adev->acpi_nb);
869
870 return ret;
871}
872
873void amdgpu_acpi_get_backlight_caps(struct amdgpu_device *adev,
874 struct amdgpu_dm_backlight_caps *caps)
875{
876 if (!adev->atif) {
877 caps->caps_valid = false;
878 return;
879 }
880 caps->caps_valid = adev->atif->backlight_caps.caps_valid;
881 caps->min_input_signal = adev->atif->backlight_caps.min_input_signal;
882 caps->max_input_signal = adev->atif->backlight_caps.max_input_signal;
883}
884
885/**
886 * amdgpu_acpi_fini - tear down driver acpi support
887 *
888 * @adev: amdgpu_device pointer
889 *
890 * Unregisters with the acpi notifier chain (all asics).
891 */
892void amdgpu_acpi_fini(struct amdgpu_device *adev)
893{
894 unregister_acpi_notifier(&adev->acpi_nb);
895 kfree(adev->atif);
896}
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/slab.h>
28#include <linux/power_supply.h>
29#include <linux/pm_runtime.h>
30#include <linux/suspend.h>
31#include <acpi/video.h>
32#include <acpi/actbl.h>
33
34#include <drm/drm_crtc_helper.h>
35#include "amdgpu.h"
36#include "amdgpu_pm.h"
37#include "amdgpu_display.h"
38#include "amd_acpi.h"
39#include "atom.h"
40
41struct amdgpu_atif_notification_cfg {
42 bool enabled;
43 int command_code;
44};
45
46struct amdgpu_atif_notifications {
47 bool thermal_state;
48 bool forced_power_state;
49 bool system_power_state;
50 bool brightness_change;
51 bool dgpu_display_event;
52 bool gpu_package_power_limit;
53};
54
55struct amdgpu_atif_functions {
56 bool system_params;
57 bool sbios_requests;
58 bool temperature_change;
59 bool query_backlight_transfer_characteristics;
60 bool ready_to_undock;
61 bool external_gpu_information;
62};
63
64struct amdgpu_atif {
65 acpi_handle handle;
66
67 struct amdgpu_atif_notifications notifications;
68 struct amdgpu_atif_functions functions;
69 struct amdgpu_atif_notification_cfg notification_cfg;
70 struct backlight_device *bd;
71 struct amdgpu_dm_backlight_caps backlight_caps;
72};
73
74struct amdgpu_atcs_functions {
75 bool get_ext_state;
76 bool pcie_perf_req;
77 bool pcie_dev_rdy;
78 bool pcie_bus_width;
79 bool power_shift_control;
80};
81
82struct amdgpu_atcs {
83 acpi_handle handle;
84
85 struct amdgpu_atcs_functions functions;
86};
87
88static struct amdgpu_acpi_priv {
89 struct amdgpu_atif atif;
90 struct amdgpu_atcs atcs;
91} amdgpu_acpi_priv;
92
93/* Call the ATIF method
94 */
95/**
96 * amdgpu_atif_call - call an ATIF method
97 *
98 * @atif: atif structure
99 * @function: the ATIF function to execute
100 * @params: ATIF function params
101 *
102 * Executes the requested ATIF function (all asics).
103 * Returns a pointer to the acpi output buffer.
104 */
105static union acpi_object *amdgpu_atif_call(struct amdgpu_atif *atif,
106 int function,
107 struct acpi_buffer *params)
108{
109 acpi_status status;
110 union acpi_object atif_arg_elements[2];
111 struct acpi_object_list atif_arg;
112 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
113
114 atif_arg.count = 2;
115 atif_arg.pointer = &atif_arg_elements[0];
116
117 atif_arg_elements[0].type = ACPI_TYPE_INTEGER;
118 atif_arg_elements[0].integer.value = function;
119
120 if (params) {
121 atif_arg_elements[1].type = ACPI_TYPE_BUFFER;
122 atif_arg_elements[1].buffer.length = params->length;
123 atif_arg_elements[1].buffer.pointer = params->pointer;
124 } else {
125 /* We need a second fake parameter */
126 atif_arg_elements[1].type = ACPI_TYPE_INTEGER;
127 atif_arg_elements[1].integer.value = 0;
128 }
129
130 status = acpi_evaluate_object(atif->handle, NULL, &atif_arg,
131 &buffer);
132
133 /* Fail only if calling the method fails and ATIF is supported */
134 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
135 DRM_DEBUG_DRIVER("failed to evaluate ATIF got %s\n",
136 acpi_format_exception(status));
137 kfree(buffer.pointer);
138 return NULL;
139 }
140
141 return buffer.pointer;
142}
143
144/**
145 * amdgpu_atif_parse_notification - parse supported notifications
146 *
147 * @n: supported notifications struct
148 * @mask: supported notifications mask from ATIF
149 *
150 * Use the supported notifications mask from ATIF function
151 * ATIF_FUNCTION_VERIFY_INTERFACE to determine what notifications
152 * are supported (all asics).
153 */
154static void amdgpu_atif_parse_notification(struct amdgpu_atif_notifications *n, u32 mask)
155{
156 n->thermal_state = mask & ATIF_THERMAL_STATE_CHANGE_REQUEST_SUPPORTED;
157 n->forced_power_state = mask & ATIF_FORCED_POWER_STATE_CHANGE_REQUEST_SUPPORTED;
158 n->system_power_state = mask & ATIF_SYSTEM_POWER_SOURCE_CHANGE_REQUEST_SUPPORTED;
159 n->brightness_change = mask & ATIF_PANEL_BRIGHTNESS_CHANGE_REQUEST_SUPPORTED;
160 n->dgpu_display_event = mask & ATIF_DGPU_DISPLAY_EVENT_SUPPORTED;
161 n->gpu_package_power_limit = mask & ATIF_GPU_PACKAGE_POWER_LIMIT_REQUEST_SUPPORTED;
162}
163
164/**
165 * amdgpu_atif_parse_functions - parse supported functions
166 *
167 * @f: supported functions struct
168 * @mask: supported functions mask from ATIF
169 *
170 * Use the supported functions mask from ATIF function
171 * ATIF_FUNCTION_VERIFY_INTERFACE to determine what functions
172 * are supported (all asics).
173 */
174static void amdgpu_atif_parse_functions(struct amdgpu_atif_functions *f, u32 mask)
175{
176 f->system_params = mask & ATIF_GET_SYSTEM_PARAMETERS_SUPPORTED;
177 f->sbios_requests = mask & ATIF_GET_SYSTEM_BIOS_REQUESTS_SUPPORTED;
178 f->temperature_change = mask & ATIF_TEMPERATURE_CHANGE_NOTIFICATION_SUPPORTED;
179 f->query_backlight_transfer_characteristics =
180 mask & ATIF_QUERY_BACKLIGHT_TRANSFER_CHARACTERISTICS_SUPPORTED;
181 f->ready_to_undock = mask & ATIF_READY_TO_UNDOCK_NOTIFICATION_SUPPORTED;
182 f->external_gpu_information = mask & ATIF_GET_EXTERNAL_GPU_INFORMATION_SUPPORTED;
183}
184
185/**
186 * amdgpu_atif_verify_interface - verify ATIF
187 *
188 * @atif: amdgpu atif struct
189 *
190 * Execute the ATIF_FUNCTION_VERIFY_INTERFACE ATIF function
191 * to initialize ATIF and determine what features are supported
192 * (all asics).
193 * returns 0 on success, error on failure.
194 */
195static int amdgpu_atif_verify_interface(struct amdgpu_atif *atif)
196{
197 union acpi_object *info;
198 struct atif_verify_interface output;
199 size_t size;
200 int err = 0;
201
202 info = amdgpu_atif_call(atif, ATIF_FUNCTION_VERIFY_INTERFACE, NULL);
203 if (!info)
204 return -EIO;
205
206 memset(&output, 0, sizeof(output));
207
208 size = *(u16 *) info->buffer.pointer;
209 if (size < 12) {
210 DRM_INFO("ATIF buffer is too small: %zu\n", size);
211 err = -EINVAL;
212 goto out;
213 }
214 size = min(sizeof(output), size);
215
216 memcpy(&output, info->buffer.pointer, size);
217
218 /* TODO: check version? */
219 DRM_DEBUG_DRIVER("ATIF version %u\n", output.version);
220
221 amdgpu_atif_parse_notification(&atif->notifications, output.notification_mask);
222 amdgpu_atif_parse_functions(&atif->functions, output.function_bits);
223
224out:
225 kfree(info);
226 return err;
227}
228
229/**
230 * amdgpu_atif_get_notification_params - determine notify configuration
231 *
232 * @atif: acpi handle
233 *
234 * Execute the ATIF_FUNCTION_GET_SYSTEM_PARAMETERS ATIF function
235 * to determine if a notifier is used and if so which one
236 * (all asics). This is either Notify(VGA, 0x81) or Notify(VGA, n)
237 * where n is specified in the result if a notifier is used.
238 * Returns 0 on success, error on failure.
239 */
240static int amdgpu_atif_get_notification_params(struct amdgpu_atif *atif)
241{
242 union acpi_object *info;
243 struct amdgpu_atif_notification_cfg *n = &atif->notification_cfg;
244 struct atif_system_params params;
245 size_t size;
246 int err = 0;
247
248 info = amdgpu_atif_call(atif, ATIF_FUNCTION_GET_SYSTEM_PARAMETERS,
249 NULL);
250 if (!info) {
251 err = -EIO;
252 goto out;
253 }
254
255 size = *(u16 *) info->buffer.pointer;
256 if (size < 10) {
257 err = -EINVAL;
258 goto out;
259 }
260
261 memset(¶ms, 0, sizeof(params));
262 size = min(sizeof(params), size);
263 memcpy(¶ms, info->buffer.pointer, size);
264
265 DRM_DEBUG_DRIVER("SYSTEM_PARAMS: mask = %#x, flags = %#x\n",
266 params.flags, params.valid_mask);
267 params.flags = params.flags & params.valid_mask;
268
269 if ((params.flags & ATIF_NOTIFY_MASK) == ATIF_NOTIFY_NONE) {
270 n->enabled = false;
271 n->command_code = 0;
272 } else if ((params.flags & ATIF_NOTIFY_MASK) == ATIF_NOTIFY_81) {
273 n->enabled = true;
274 n->command_code = 0x81;
275 } else {
276 if (size < 11) {
277 err = -EINVAL;
278 goto out;
279 }
280 n->enabled = true;
281 n->command_code = params.command_code;
282 }
283
284out:
285 DRM_DEBUG_DRIVER("Notification %s, command code = %#x\n",
286 (n->enabled ? "enabled" : "disabled"),
287 n->command_code);
288 kfree(info);
289 return err;
290}
291
292/**
293 * amdgpu_atif_query_backlight_caps - get min and max backlight input signal
294 *
295 * @atif: acpi handle
296 *
297 * Execute the QUERY_BRIGHTNESS_TRANSFER_CHARACTERISTICS ATIF function
298 * to determine the acceptable range of backlight values
299 *
300 * Backlight_caps.caps_valid will be set to true if the query is successful
301 *
302 * The input signals are in range 0-255
303 *
304 * This function assumes the display with backlight is the first LCD
305 *
306 * Returns 0 on success, error on failure.
307 */
308static int amdgpu_atif_query_backlight_caps(struct amdgpu_atif *atif)
309{
310 union acpi_object *info;
311 struct atif_qbtc_output characteristics;
312 struct atif_qbtc_arguments arguments;
313 struct acpi_buffer params;
314 size_t size;
315 int err = 0;
316
317 arguments.size = sizeof(arguments);
318 arguments.requested_display = ATIF_QBTC_REQUEST_LCD1;
319
320 params.length = sizeof(arguments);
321 params.pointer = (void *)&arguments;
322
323 info = amdgpu_atif_call(atif,
324 ATIF_FUNCTION_QUERY_BRIGHTNESS_TRANSFER_CHARACTERISTICS,
325 ¶ms);
326 if (!info) {
327 err = -EIO;
328 goto out;
329 }
330
331 size = *(u16 *) info->buffer.pointer;
332 if (size < 10) {
333 err = -EINVAL;
334 goto out;
335 }
336
337 memset(&characteristics, 0, sizeof(characteristics));
338 size = min(sizeof(characteristics), size);
339 memcpy(&characteristics, info->buffer.pointer, size);
340
341 atif->backlight_caps.caps_valid = true;
342 atif->backlight_caps.min_input_signal =
343 characteristics.min_input_signal;
344 atif->backlight_caps.max_input_signal =
345 characteristics.max_input_signal;
346out:
347 kfree(info);
348 return err;
349}
350
351/**
352 * amdgpu_atif_get_sbios_requests - get requested sbios event
353 *
354 * @atif: acpi handle
355 * @req: atif sbios request struct
356 *
357 * Execute the ATIF_FUNCTION_GET_SYSTEM_BIOS_REQUESTS ATIF function
358 * to determine what requests the sbios is making to the driver
359 * (all asics).
360 * Returns 0 on success, error on failure.
361 */
362static int amdgpu_atif_get_sbios_requests(struct amdgpu_atif *atif,
363 struct atif_sbios_requests *req)
364{
365 union acpi_object *info;
366 size_t size;
367 int count = 0;
368
369 info = amdgpu_atif_call(atif, ATIF_FUNCTION_GET_SYSTEM_BIOS_REQUESTS,
370 NULL);
371 if (!info)
372 return -EIO;
373
374 size = *(u16 *)info->buffer.pointer;
375 if (size < 0xd) {
376 count = -EINVAL;
377 goto out;
378 }
379 memset(req, 0, sizeof(*req));
380
381 size = min(sizeof(*req), size);
382 memcpy(req, info->buffer.pointer, size);
383 DRM_DEBUG_DRIVER("SBIOS pending requests: %#x\n", req->pending);
384
385 count = hweight32(req->pending);
386
387out:
388 kfree(info);
389 return count;
390}
391
392/**
393 * amdgpu_atif_handler - handle ATIF notify requests
394 *
395 * @adev: amdgpu_device pointer
396 * @event: atif sbios request struct
397 *
398 * Checks the acpi event and if it matches an atif event,
399 * handles it.
400 *
401 * Returns:
402 * NOTIFY_BAD or NOTIFY_DONE, depending on the event.
403 */
404static int amdgpu_atif_handler(struct amdgpu_device *adev,
405 struct acpi_bus_event *event)
406{
407 struct amdgpu_atif *atif = &amdgpu_acpi_priv.atif;
408 int count;
409
410 DRM_DEBUG_DRIVER("event, device_class = %s, type = %#x\n",
411 event->device_class, event->type);
412
413 if (strcmp(event->device_class, ACPI_VIDEO_CLASS) != 0)
414 return NOTIFY_DONE;
415
416 /* Is this actually our event? */
417 if (!atif->notification_cfg.enabled ||
418 event->type != atif->notification_cfg.command_code) {
419 /* These events will generate keypresses otherwise */
420 if (event->type == ACPI_VIDEO_NOTIFY_PROBE)
421 return NOTIFY_BAD;
422 else
423 return NOTIFY_DONE;
424 }
425
426 if (atif->functions.sbios_requests) {
427 struct atif_sbios_requests req;
428
429 /* Check pending SBIOS requests */
430 count = amdgpu_atif_get_sbios_requests(atif, &req);
431
432 if (count <= 0)
433 return NOTIFY_BAD;
434
435 DRM_DEBUG_DRIVER("ATIF: %d pending SBIOS requests\n", count);
436
437 if (req.pending & ATIF_PANEL_BRIGHTNESS_CHANGE_REQUEST) {
438 if (atif->bd) {
439 DRM_DEBUG_DRIVER("Changing brightness to %d\n",
440 req.backlight_level);
441 /*
442 * XXX backlight_device_set_brightness() is
443 * hardwired to post BACKLIGHT_UPDATE_SYSFS.
444 * It probably should accept 'reason' parameter.
445 */
446 backlight_device_set_brightness(atif->bd, req.backlight_level);
447 }
448 }
449
450 if (req.pending & ATIF_DGPU_DISPLAY_EVENT) {
451 if (adev->flags & AMD_IS_PX) {
452 pm_runtime_get_sync(adev_to_drm(adev)->dev);
453 /* Just fire off a uevent and let userspace tell us what to do */
454 drm_helper_hpd_irq_event(adev_to_drm(adev));
455 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
456 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
457 }
458 }
459 /* TODO: check other events */
460 }
461
462 /* We've handled the event, stop the notifier chain. The ACPI interface
463 * overloads ACPI_VIDEO_NOTIFY_PROBE, we don't want to send that to
464 * userspace if the event was generated only to signal a SBIOS
465 * request.
466 */
467 return NOTIFY_BAD;
468}
469
470/* Call the ATCS method
471 */
472/**
473 * amdgpu_atcs_call - call an ATCS method
474 *
475 * @atcs: atcs structure
476 * @function: the ATCS function to execute
477 * @params: ATCS function params
478 *
479 * Executes the requested ATCS function (all asics).
480 * Returns a pointer to the acpi output buffer.
481 */
482static union acpi_object *amdgpu_atcs_call(struct amdgpu_atcs *atcs,
483 int function,
484 struct acpi_buffer *params)
485{
486 acpi_status status;
487 union acpi_object atcs_arg_elements[2];
488 struct acpi_object_list atcs_arg;
489 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
490
491 atcs_arg.count = 2;
492 atcs_arg.pointer = &atcs_arg_elements[0];
493
494 atcs_arg_elements[0].type = ACPI_TYPE_INTEGER;
495 atcs_arg_elements[0].integer.value = function;
496
497 if (params) {
498 atcs_arg_elements[1].type = ACPI_TYPE_BUFFER;
499 atcs_arg_elements[1].buffer.length = params->length;
500 atcs_arg_elements[1].buffer.pointer = params->pointer;
501 } else {
502 /* We need a second fake parameter */
503 atcs_arg_elements[1].type = ACPI_TYPE_INTEGER;
504 atcs_arg_elements[1].integer.value = 0;
505 }
506
507 status = acpi_evaluate_object(atcs->handle, NULL, &atcs_arg, &buffer);
508
509 /* Fail only if calling the method fails and ATIF is supported */
510 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
511 DRM_DEBUG_DRIVER("failed to evaluate ATCS got %s\n",
512 acpi_format_exception(status));
513 kfree(buffer.pointer);
514 return NULL;
515 }
516
517 return buffer.pointer;
518}
519
520/**
521 * amdgpu_atcs_parse_functions - parse supported functions
522 *
523 * @f: supported functions struct
524 * @mask: supported functions mask from ATCS
525 *
526 * Use the supported functions mask from ATCS function
527 * ATCS_FUNCTION_VERIFY_INTERFACE to determine what functions
528 * are supported (all asics).
529 */
530static void amdgpu_atcs_parse_functions(struct amdgpu_atcs_functions *f, u32 mask)
531{
532 f->get_ext_state = mask & ATCS_GET_EXTERNAL_STATE_SUPPORTED;
533 f->pcie_perf_req = mask & ATCS_PCIE_PERFORMANCE_REQUEST_SUPPORTED;
534 f->pcie_dev_rdy = mask & ATCS_PCIE_DEVICE_READY_NOTIFICATION_SUPPORTED;
535 f->pcie_bus_width = mask & ATCS_SET_PCIE_BUS_WIDTH_SUPPORTED;
536 f->power_shift_control = mask & ATCS_SET_POWER_SHIFT_CONTROL_SUPPORTED;
537}
538
539/**
540 * amdgpu_atcs_verify_interface - verify ATCS
541 *
542 * @atcs: amdgpu atcs struct
543 *
544 * Execute the ATCS_FUNCTION_VERIFY_INTERFACE ATCS function
545 * to initialize ATCS and determine what features are supported
546 * (all asics).
547 * returns 0 on success, error on failure.
548 */
549static int amdgpu_atcs_verify_interface(struct amdgpu_atcs *atcs)
550{
551 union acpi_object *info;
552 struct atcs_verify_interface output;
553 size_t size;
554 int err = 0;
555
556 info = amdgpu_atcs_call(atcs, ATCS_FUNCTION_VERIFY_INTERFACE, NULL);
557 if (!info)
558 return -EIO;
559
560 memset(&output, 0, sizeof(output));
561
562 size = *(u16 *) info->buffer.pointer;
563 if (size < 8) {
564 DRM_INFO("ATCS buffer is too small: %zu\n", size);
565 err = -EINVAL;
566 goto out;
567 }
568 size = min(sizeof(output), size);
569
570 memcpy(&output, info->buffer.pointer, size);
571
572 /* TODO: check version? */
573 DRM_DEBUG_DRIVER("ATCS version %u\n", output.version);
574
575 amdgpu_atcs_parse_functions(&atcs->functions, output.function_bits);
576
577out:
578 kfree(info);
579 return err;
580}
581
582/**
583 * amdgpu_acpi_is_pcie_performance_request_supported
584 *
585 * @adev: amdgpu_device pointer
586 *
587 * Check if the ATCS pcie_perf_req and pcie_dev_rdy methods
588 * are supported (all asics).
589 * returns true if supported, false if not.
590 */
591bool amdgpu_acpi_is_pcie_performance_request_supported(struct amdgpu_device *adev)
592{
593 struct amdgpu_atcs *atcs = &amdgpu_acpi_priv.atcs;
594
595 if (atcs->functions.pcie_perf_req && atcs->functions.pcie_dev_rdy)
596 return true;
597
598 return false;
599}
600
601/**
602 * amdgpu_acpi_is_power_shift_control_supported
603 *
604 * Check if the ATCS power shift control method
605 * is supported.
606 * returns true if supported, false if not.
607 */
608bool amdgpu_acpi_is_power_shift_control_supported(void)
609{
610 return amdgpu_acpi_priv.atcs.functions.power_shift_control;
611}
612
613/**
614 * amdgpu_acpi_pcie_notify_device_ready
615 *
616 * @adev: amdgpu_device pointer
617 *
618 * Executes the PCIE_DEVICE_READY_NOTIFICATION method
619 * (all asics).
620 * returns 0 on success, error on failure.
621 */
622int amdgpu_acpi_pcie_notify_device_ready(struct amdgpu_device *adev)
623{
624 union acpi_object *info;
625 struct amdgpu_atcs *atcs = &amdgpu_acpi_priv.atcs;
626
627 if (!atcs->functions.pcie_dev_rdy)
628 return -EINVAL;
629
630 info = amdgpu_atcs_call(atcs, ATCS_FUNCTION_PCIE_DEVICE_READY_NOTIFICATION, NULL);
631 if (!info)
632 return -EIO;
633
634 kfree(info);
635
636 return 0;
637}
638
639/**
640 * amdgpu_acpi_pcie_performance_request
641 *
642 * @adev: amdgpu_device pointer
643 * @perf_req: requested perf level (pcie gen speed)
644 * @advertise: set advertise caps flag if set
645 *
646 * Executes the PCIE_PERFORMANCE_REQUEST method to
647 * change the pcie gen speed (all asics).
648 * returns 0 on success, error on failure.
649 */
650int amdgpu_acpi_pcie_performance_request(struct amdgpu_device *adev,
651 u8 perf_req, bool advertise)
652{
653 union acpi_object *info;
654 struct amdgpu_atcs *atcs = &amdgpu_acpi_priv.atcs;
655 struct atcs_pref_req_input atcs_input;
656 struct atcs_pref_req_output atcs_output;
657 struct acpi_buffer params;
658 size_t size;
659 u32 retry = 3;
660
661 if (amdgpu_acpi_pcie_notify_device_ready(adev))
662 return -EINVAL;
663
664 if (!atcs->functions.pcie_perf_req)
665 return -EINVAL;
666
667 atcs_input.size = sizeof(struct atcs_pref_req_input);
668 /* client id (bit 2-0: func num, 7-3: dev num, 15-8: bus num) */
669 atcs_input.client_id = adev->pdev->devfn | (adev->pdev->bus->number << 8);
670 atcs_input.valid_flags_mask = ATCS_VALID_FLAGS_MASK;
671 atcs_input.flags = ATCS_WAIT_FOR_COMPLETION;
672 if (advertise)
673 atcs_input.flags |= ATCS_ADVERTISE_CAPS;
674 atcs_input.req_type = ATCS_PCIE_LINK_SPEED;
675 atcs_input.perf_req = perf_req;
676
677 params.length = sizeof(struct atcs_pref_req_input);
678 params.pointer = &atcs_input;
679
680 while (retry--) {
681 info = amdgpu_atcs_call(atcs, ATCS_FUNCTION_PCIE_PERFORMANCE_REQUEST, ¶ms);
682 if (!info)
683 return -EIO;
684
685 memset(&atcs_output, 0, sizeof(atcs_output));
686
687 size = *(u16 *) info->buffer.pointer;
688 if (size < 3) {
689 DRM_INFO("ATCS buffer is too small: %zu\n", size);
690 kfree(info);
691 return -EINVAL;
692 }
693 size = min(sizeof(atcs_output), size);
694
695 memcpy(&atcs_output, info->buffer.pointer, size);
696
697 kfree(info);
698
699 switch (atcs_output.ret_val) {
700 case ATCS_REQUEST_REFUSED:
701 default:
702 return -EINVAL;
703 case ATCS_REQUEST_COMPLETE:
704 return 0;
705 case ATCS_REQUEST_IN_PROGRESS:
706 udelay(10);
707 break;
708 }
709 }
710
711 return 0;
712}
713
714/**
715 * amdgpu_acpi_power_shift_control
716 *
717 * @adev: amdgpu_device pointer
718 * @dev_state: device acpi state
719 * @drv_state: driver state
720 *
721 * Executes the POWER_SHIFT_CONTROL method to
722 * communicate current dGPU device state and
723 * driver state to APU/SBIOS.
724 * returns 0 on success, error on failure.
725 */
726int amdgpu_acpi_power_shift_control(struct amdgpu_device *adev,
727 u8 dev_state, bool drv_state)
728{
729 union acpi_object *info;
730 struct amdgpu_atcs *atcs = &amdgpu_acpi_priv.atcs;
731 struct atcs_pwr_shift_input atcs_input;
732 struct acpi_buffer params;
733
734 if (!amdgpu_acpi_is_power_shift_control_supported())
735 return -EINVAL;
736
737 atcs_input.size = sizeof(struct atcs_pwr_shift_input);
738 /* dGPU id (bit 2-0: func num, 7-3: dev num, 15-8: bus num) */
739 atcs_input.dgpu_id = adev->pdev->devfn | (adev->pdev->bus->number << 8);
740 atcs_input.dev_acpi_state = dev_state;
741 atcs_input.drv_state = drv_state;
742
743 params.length = sizeof(struct atcs_pwr_shift_input);
744 params.pointer = &atcs_input;
745
746 info = amdgpu_atcs_call(atcs, ATCS_FUNCTION_POWER_SHIFT_CONTROL, ¶ms);
747 if (!info) {
748 DRM_ERROR("ATCS PSC update failed\n");
749 return -EIO;
750 }
751
752 return 0;
753}
754
755/**
756 * amdgpu_acpi_smart_shift_update - update dGPU device state to SBIOS
757 *
758 * @dev: drm_device pointer
759 * @ss_state: current smart shift event
760 *
761 * returns 0 on success,
762 * otherwise return error number.
763 */
764int amdgpu_acpi_smart_shift_update(struct drm_device *dev, enum amdgpu_ss ss_state)
765{
766 struct amdgpu_device *adev = drm_to_adev(dev);
767 int r;
768
769 if (!amdgpu_device_supports_smart_shift(dev))
770 return 0;
771
772 switch (ss_state) {
773 /* SBIOS trigger “stop”, “enable” and “start” at D0, Driver Operational.
774 * SBIOS trigger “stop” at D3, Driver Not Operational.
775 * SBIOS trigger “stop” and “disable” at D0, Driver NOT operational.
776 */
777 case AMDGPU_SS_DRV_LOAD:
778 r = amdgpu_acpi_power_shift_control(adev,
779 AMDGPU_ATCS_PSC_DEV_STATE_D0,
780 AMDGPU_ATCS_PSC_DRV_STATE_OPR);
781 break;
782 case AMDGPU_SS_DEV_D0:
783 r = amdgpu_acpi_power_shift_control(adev,
784 AMDGPU_ATCS_PSC_DEV_STATE_D0,
785 AMDGPU_ATCS_PSC_DRV_STATE_OPR);
786 break;
787 case AMDGPU_SS_DEV_D3:
788 r = amdgpu_acpi_power_shift_control(adev,
789 AMDGPU_ATCS_PSC_DEV_STATE_D3_HOT,
790 AMDGPU_ATCS_PSC_DRV_STATE_NOT_OPR);
791 break;
792 case AMDGPU_SS_DRV_UNLOAD:
793 r = amdgpu_acpi_power_shift_control(adev,
794 AMDGPU_ATCS_PSC_DEV_STATE_D0,
795 AMDGPU_ATCS_PSC_DRV_STATE_NOT_OPR);
796 break;
797 default:
798 return -EINVAL;
799 }
800
801 return r;
802}
803
804/**
805 * amdgpu_acpi_event - handle notify events
806 *
807 * @nb: notifier block
808 * @val: val
809 * @data: acpi event
810 *
811 * Calls relevant amdgpu functions in response to various
812 * acpi events.
813 * Returns NOTIFY code
814 */
815static int amdgpu_acpi_event(struct notifier_block *nb,
816 unsigned long val,
817 void *data)
818{
819 struct amdgpu_device *adev = container_of(nb, struct amdgpu_device, acpi_nb);
820 struct acpi_bus_event *entry = (struct acpi_bus_event *)data;
821
822 if (strcmp(entry->device_class, ACPI_AC_CLASS) == 0) {
823 if (power_supply_is_system_supplied() > 0)
824 DRM_DEBUG_DRIVER("pm: AC\n");
825 else
826 DRM_DEBUG_DRIVER("pm: DC\n");
827
828 amdgpu_pm_acpi_event_handler(adev);
829 }
830
831 /* Check for pending SBIOS requests */
832 return amdgpu_atif_handler(adev, entry);
833}
834
835/* Call all ACPI methods here */
836/**
837 * amdgpu_acpi_init - init driver acpi support
838 *
839 * @adev: amdgpu_device pointer
840 *
841 * Verifies the AMD ACPI interfaces and registers with the acpi
842 * notifier chain (all asics).
843 * Returns 0 on success, error on failure.
844 */
845int amdgpu_acpi_init(struct amdgpu_device *adev)
846{
847 struct amdgpu_atif *atif = &amdgpu_acpi_priv.atif;
848
849 if (atif->notifications.brightness_change) {
850 if (adev->dc_enabled) {
851#if defined(CONFIG_DRM_AMD_DC)
852 struct amdgpu_display_manager *dm = &adev->dm;
853
854 if (dm->backlight_dev[0])
855 atif->bd = dm->backlight_dev[0];
856#endif
857 } else {
858 struct drm_encoder *tmp;
859
860 /* Find the encoder controlling the brightness */
861 list_for_each_entry(tmp, &adev_to_drm(adev)->mode_config.encoder_list,
862 head) {
863 struct amdgpu_encoder *enc = to_amdgpu_encoder(tmp);
864
865 if ((enc->devices & (ATOM_DEVICE_LCD_SUPPORT)) &&
866 enc->enc_priv) {
867 struct amdgpu_encoder_atom_dig *dig = enc->enc_priv;
868
869 if (dig->bl_dev) {
870 atif->bd = dig->bl_dev;
871 break;
872 }
873 }
874 }
875 }
876 }
877 adev->acpi_nb.notifier_call = amdgpu_acpi_event;
878 register_acpi_notifier(&adev->acpi_nb);
879
880 return 0;
881}
882
883void amdgpu_acpi_get_backlight_caps(struct amdgpu_dm_backlight_caps *caps)
884{
885 struct amdgpu_atif *atif = &amdgpu_acpi_priv.atif;
886
887 caps->caps_valid = atif->backlight_caps.caps_valid;
888 caps->min_input_signal = atif->backlight_caps.min_input_signal;
889 caps->max_input_signal = atif->backlight_caps.max_input_signal;
890}
891
892/**
893 * amdgpu_acpi_fini - tear down driver acpi support
894 *
895 * @adev: amdgpu_device pointer
896 *
897 * Unregisters with the acpi notifier chain (all asics).
898 */
899void amdgpu_acpi_fini(struct amdgpu_device *adev)
900{
901 unregister_acpi_notifier(&adev->acpi_nb);
902}
903
904/**
905 * amdgpu_atif_pci_probe_handle - look up the ATIF handle
906 *
907 * @pdev: pci device
908 *
909 * Look up the ATIF handles (all asics).
910 * Returns true if the handle is found, false if not.
911 */
912static bool amdgpu_atif_pci_probe_handle(struct pci_dev *pdev)
913{
914 char acpi_method_name[255] = { 0 };
915 struct acpi_buffer buffer = {sizeof(acpi_method_name), acpi_method_name};
916 acpi_handle dhandle, atif_handle;
917 acpi_status status;
918 int ret;
919
920 dhandle = ACPI_HANDLE(&pdev->dev);
921 if (!dhandle)
922 return false;
923
924 status = acpi_get_handle(dhandle, "ATIF", &atif_handle);
925 if (ACPI_FAILURE(status))
926 return false;
927
928 amdgpu_acpi_priv.atif.handle = atif_handle;
929 acpi_get_name(amdgpu_acpi_priv.atif.handle, ACPI_FULL_PATHNAME, &buffer);
930 DRM_DEBUG_DRIVER("Found ATIF handle %s\n", acpi_method_name);
931 ret = amdgpu_atif_verify_interface(&amdgpu_acpi_priv.atif);
932 if (ret) {
933 amdgpu_acpi_priv.atif.handle = 0;
934 return false;
935 }
936 return true;
937}
938
939/**
940 * amdgpu_atcs_pci_probe_handle - look up the ATCS handle
941 *
942 * @pdev: pci device
943 *
944 * Look up the ATCS handles (all asics).
945 * Returns true if the handle is found, false if not.
946 */
947static bool amdgpu_atcs_pci_probe_handle(struct pci_dev *pdev)
948{
949 char acpi_method_name[255] = { 0 };
950 struct acpi_buffer buffer = { sizeof(acpi_method_name), acpi_method_name };
951 acpi_handle dhandle, atcs_handle;
952 acpi_status status;
953 int ret;
954
955 dhandle = ACPI_HANDLE(&pdev->dev);
956 if (!dhandle)
957 return false;
958
959 status = acpi_get_handle(dhandle, "ATCS", &atcs_handle);
960 if (ACPI_FAILURE(status))
961 return false;
962
963 amdgpu_acpi_priv.atcs.handle = atcs_handle;
964 acpi_get_name(amdgpu_acpi_priv.atcs.handle, ACPI_FULL_PATHNAME, &buffer);
965 DRM_DEBUG_DRIVER("Found ATCS handle %s\n", acpi_method_name);
966 ret = amdgpu_atcs_verify_interface(&amdgpu_acpi_priv.atcs);
967 if (ret) {
968 amdgpu_acpi_priv.atcs.handle = 0;
969 return false;
970 }
971 return true;
972}
973
974/*
975 * amdgpu_acpi_detect - detect ACPI ATIF/ATCS methods
976 *
977 * Check if we have the ATIF/ATCS methods and populate
978 * the structures in the driver.
979 */
980void amdgpu_acpi_detect(void)
981{
982 struct amdgpu_atif *atif = &amdgpu_acpi_priv.atif;
983 struct amdgpu_atcs *atcs = &amdgpu_acpi_priv.atcs;
984 struct pci_dev *pdev = NULL;
985 int ret;
986
987 while ((pdev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, pdev)) != NULL) {
988 if (!atif->handle)
989 amdgpu_atif_pci_probe_handle(pdev);
990 if (!atcs->handle)
991 amdgpu_atcs_pci_probe_handle(pdev);
992 }
993
994 while ((pdev = pci_get_class(PCI_CLASS_DISPLAY_OTHER << 8, pdev)) != NULL) {
995 if (!atif->handle)
996 amdgpu_atif_pci_probe_handle(pdev);
997 if (!atcs->handle)
998 amdgpu_atcs_pci_probe_handle(pdev);
999 }
1000
1001 if (atif->functions.sbios_requests && !atif->functions.system_params) {
1002 /* XXX check this workraround, if sbios request function is
1003 * present we have to see how it's configured in the system
1004 * params
1005 */
1006 atif->functions.system_params = true;
1007 }
1008
1009 if (atif->functions.system_params) {
1010 ret = amdgpu_atif_get_notification_params(atif);
1011 if (ret) {
1012 DRM_DEBUG_DRIVER("Call to GET_SYSTEM_PARAMS failed: %d\n",
1013 ret);
1014 /* Disable notification */
1015 atif->notification_cfg.enabled = false;
1016 }
1017 }
1018
1019 if (atif->functions.query_backlight_transfer_characteristics) {
1020 ret = amdgpu_atif_query_backlight_caps(atif);
1021 if (ret) {
1022 DRM_DEBUG_DRIVER("Call to QUERY_BACKLIGHT_TRANSFER_CHARACTERISTICS failed: %d\n",
1023 ret);
1024 atif->backlight_caps.caps_valid = false;
1025 }
1026 } else {
1027 atif->backlight_caps.caps_valid = false;
1028 }
1029}
1030
1031#if IS_ENABLED(CONFIG_SUSPEND)
1032/**
1033 * amdgpu_acpi_is_s3_active
1034 *
1035 * @adev: amdgpu_device_pointer
1036 *
1037 * returns true if supported, false if not.
1038 */
1039bool amdgpu_acpi_is_s3_active(struct amdgpu_device *adev)
1040{
1041 return !(adev->flags & AMD_IS_APU) ||
1042 (pm_suspend_target_state == PM_SUSPEND_MEM);
1043}
1044
1045/**
1046 * amdgpu_acpi_should_gpu_reset
1047 *
1048 * @adev: amdgpu_device_pointer
1049 *
1050 * returns true if should reset GPU, false if not
1051 */
1052bool amdgpu_acpi_should_gpu_reset(struct amdgpu_device *adev)
1053{
1054 if (adev->flags & AMD_IS_APU)
1055 return false;
1056
1057 if (amdgpu_sriov_vf(adev))
1058 return false;
1059
1060 return pm_suspend_target_state != PM_SUSPEND_TO_IDLE;
1061}
1062
1063/**
1064 * amdgpu_acpi_is_s0ix_active
1065 *
1066 * @adev: amdgpu_device_pointer
1067 *
1068 * returns true if supported, false if not.
1069 */
1070bool amdgpu_acpi_is_s0ix_active(struct amdgpu_device *adev)
1071{
1072 if (!(adev->flags & AMD_IS_APU) ||
1073 (pm_suspend_target_state != PM_SUSPEND_TO_IDLE))
1074 return false;
1075
1076 /*
1077 * If ACPI_FADT_LOW_POWER_S0 is not set in the FADT, it is generally
1078 * risky to do any special firmware-related preparations for entering
1079 * S0ix even though the system is suspending to idle, so return false
1080 * in that case.
1081 */
1082 if (!(acpi_gbl_FADT.flags & ACPI_FADT_LOW_POWER_S0)) {
1083 dev_warn_once(adev->dev,
1084 "Power consumption will be higher as BIOS has not been configured for suspend-to-idle.\n"
1085 "To use suspend-to-idle change the sleep mode in BIOS setup.\n");
1086 return false;
1087 }
1088
1089#if !IS_ENABLED(CONFIG_AMD_PMC)
1090 dev_warn_once(adev->dev,
1091 "Power consumption will be higher as the kernel has not been compiled with CONFIG_AMD_PMC.\n");
1092 return false;
1093#else
1094 return true;
1095#endif /* CONFIG_AMD_PMC */
1096}
1097
1098#endif /* CONFIG_SUSPEND */