Loading...
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 */
43static const guid_t amd_xcc_dsm_guid = GUID_INIT(0x8267f5d5, 0xa556, 0x44f2,
44 0xb8, 0xb4, 0x45, 0x56, 0x2e,
45 0x8c, 0x5b, 0xec);
46
47#define AMD_XCC_HID_START 3000
48#define AMD_XCC_DSM_GET_NUM_FUNCS 0
49#define AMD_XCC_DSM_GET_SUPP_MODE 1
50#define AMD_XCC_DSM_GET_XCP_MODE 2
51#define AMD_XCC_DSM_GET_VF_XCC_MAPPING 4
52#define AMD_XCC_DSM_GET_TMR_INFO 5
53#define AMD_XCC_DSM_NUM_FUNCS 5
54
55#define AMD_XCC_MAX_HID 24
56
57struct xarray numa_info_xa;
58
59/* Encapsulates the XCD acpi object information */
60struct amdgpu_acpi_xcc_info {
61 struct list_head list;
62 struct amdgpu_numa_info *numa_info;
63 uint8_t xcp_node;
64 uint8_t phy_id;
65 acpi_handle handle;
66};
67
68struct amdgpu_acpi_dev_info {
69 struct list_head list;
70 struct list_head xcc_list;
71 uint32_t sbdf;
72 uint16_t supp_xcp_mode;
73 uint16_t xcp_mode;
74 uint16_t mem_mode;
75 uint64_t tmr_base;
76 uint64_t tmr_size;
77};
78
79struct list_head amdgpu_acpi_dev_list;
80
81struct amdgpu_atif_notification_cfg {
82 bool enabled;
83 int command_code;
84};
85
86struct amdgpu_atif_notifications {
87 bool thermal_state;
88 bool forced_power_state;
89 bool system_power_state;
90 bool brightness_change;
91 bool dgpu_display_event;
92 bool gpu_package_power_limit;
93};
94
95struct amdgpu_atif_functions {
96 bool system_params;
97 bool sbios_requests;
98 bool temperature_change;
99 bool query_backlight_transfer_characteristics;
100 bool ready_to_undock;
101 bool external_gpu_information;
102};
103
104struct amdgpu_atif {
105 acpi_handle handle;
106
107 struct amdgpu_atif_notifications notifications;
108 struct amdgpu_atif_functions functions;
109 struct amdgpu_atif_notification_cfg notification_cfg;
110 struct backlight_device *bd;
111 struct amdgpu_dm_backlight_caps backlight_caps;
112};
113
114struct amdgpu_atcs_functions {
115 bool get_ext_state;
116 bool pcie_perf_req;
117 bool pcie_dev_rdy;
118 bool pcie_bus_width;
119 bool power_shift_control;
120};
121
122struct amdgpu_atcs {
123 acpi_handle handle;
124
125 struct amdgpu_atcs_functions functions;
126};
127
128static struct amdgpu_acpi_priv {
129 struct amdgpu_atif atif;
130 struct amdgpu_atcs atcs;
131} amdgpu_acpi_priv;
132
133/* Call the ATIF method
134 */
135/**
136 * amdgpu_atif_call - call an ATIF method
137 *
138 * @atif: atif structure
139 * @function: the ATIF function to execute
140 * @params: ATIF function params
141 *
142 * Executes the requested ATIF function (all asics).
143 * Returns a pointer to the acpi output buffer.
144 */
145static union acpi_object *amdgpu_atif_call(struct amdgpu_atif *atif,
146 int function,
147 struct acpi_buffer *params)
148{
149 acpi_status status;
150 union acpi_object *obj;
151 union acpi_object atif_arg_elements[2];
152 struct acpi_object_list atif_arg;
153 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
154
155 atif_arg.count = 2;
156 atif_arg.pointer = &atif_arg_elements[0];
157
158 atif_arg_elements[0].type = ACPI_TYPE_INTEGER;
159 atif_arg_elements[0].integer.value = function;
160
161 if (params) {
162 atif_arg_elements[1].type = ACPI_TYPE_BUFFER;
163 atif_arg_elements[1].buffer.length = params->length;
164 atif_arg_elements[1].buffer.pointer = params->pointer;
165 } else {
166 /* We need a second fake parameter */
167 atif_arg_elements[1].type = ACPI_TYPE_INTEGER;
168 atif_arg_elements[1].integer.value = 0;
169 }
170
171 status = acpi_evaluate_object(atif->handle, NULL, &atif_arg,
172 &buffer);
173 obj = (union acpi_object *)buffer.pointer;
174
175 /* Fail if calling the method fails */
176 if (ACPI_FAILURE(status)) {
177 DRM_DEBUG_DRIVER("failed to evaluate ATIF got %s\n",
178 acpi_format_exception(status));
179 kfree(obj);
180 return NULL;
181 }
182
183 if (obj->type != ACPI_TYPE_BUFFER) {
184 DRM_DEBUG_DRIVER("bad object returned from ATIF: %d\n",
185 obj->type);
186 kfree(obj);
187 return NULL;
188 }
189
190 return obj;
191}
192
193/**
194 * amdgpu_atif_parse_notification - parse supported notifications
195 *
196 * @n: supported notifications struct
197 * @mask: supported notifications mask from ATIF
198 *
199 * Use the supported notifications mask from ATIF function
200 * ATIF_FUNCTION_VERIFY_INTERFACE to determine what notifications
201 * are supported (all asics).
202 */
203static void amdgpu_atif_parse_notification(struct amdgpu_atif_notifications *n, u32 mask)
204{
205 n->thermal_state = mask & ATIF_THERMAL_STATE_CHANGE_REQUEST_SUPPORTED;
206 n->forced_power_state = mask & ATIF_FORCED_POWER_STATE_CHANGE_REQUEST_SUPPORTED;
207 n->system_power_state = mask & ATIF_SYSTEM_POWER_SOURCE_CHANGE_REQUEST_SUPPORTED;
208 n->brightness_change = mask & ATIF_PANEL_BRIGHTNESS_CHANGE_REQUEST_SUPPORTED;
209 n->dgpu_display_event = mask & ATIF_DGPU_DISPLAY_EVENT_SUPPORTED;
210 n->gpu_package_power_limit = mask & ATIF_GPU_PACKAGE_POWER_LIMIT_REQUEST_SUPPORTED;
211}
212
213/**
214 * amdgpu_atif_parse_functions - parse supported functions
215 *
216 * @f: supported functions struct
217 * @mask: supported functions mask from ATIF
218 *
219 * Use the supported functions mask from ATIF function
220 * ATIF_FUNCTION_VERIFY_INTERFACE to determine what functions
221 * are supported (all asics).
222 */
223static void amdgpu_atif_parse_functions(struct amdgpu_atif_functions *f, u32 mask)
224{
225 f->system_params = mask & ATIF_GET_SYSTEM_PARAMETERS_SUPPORTED;
226 f->sbios_requests = mask & ATIF_GET_SYSTEM_BIOS_REQUESTS_SUPPORTED;
227 f->temperature_change = mask & ATIF_TEMPERATURE_CHANGE_NOTIFICATION_SUPPORTED;
228 f->query_backlight_transfer_characteristics =
229 mask & ATIF_QUERY_BACKLIGHT_TRANSFER_CHARACTERISTICS_SUPPORTED;
230 f->ready_to_undock = mask & ATIF_READY_TO_UNDOCK_NOTIFICATION_SUPPORTED;
231 f->external_gpu_information = mask & ATIF_GET_EXTERNAL_GPU_INFORMATION_SUPPORTED;
232}
233
234/**
235 * amdgpu_atif_verify_interface - verify ATIF
236 *
237 * @atif: amdgpu atif struct
238 *
239 * Execute the ATIF_FUNCTION_VERIFY_INTERFACE ATIF function
240 * to initialize ATIF and determine what features are supported
241 * (all asics).
242 * returns 0 on success, error on failure.
243 */
244static int amdgpu_atif_verify_interface(struct amdgpu_atif *atif)
245{
246 union acpi_object *info;
247 struct atif_verify_interface output;
248 size_t size;
249 int err = 0;
250
251 info = amdgpu_atif_call(atif, ATIF_FUNCTION_VERIFY_INTERFACE, NULL);
252 if (!info)
253 return -EIO;
254
255 memset(&output, 0, sizeof(output));
256
257 size = *(u16 *) info->buffer.pointer;
258 if (size < 12) {
259 DRM_INFO("ATIF buffer is too small: %zu\n", size);
260 err = -EINVAL;
261 goto out;
262 }
263 size = min(sizeof(output), size);
264
265 memcpy(&output, info->buffer.pointer, size);
266
267 /* TODO: check version? */
268 DRM_DEBUG_DRIVER("ATIF version %u\n", output.version);
269
270 amdgpu_atif_parse_notification(&atif->notifications, output.notification_mask);
271 amdgpu_atif_parse_functions(&atif->functions, output.function_bits);
272
273out:
274 kfree(info);
275 return err;
276}
277
278/**
279 * amdgpu_atif_get_notification_params - determine notify configuration
280 *
281 * @atif: acpi handle
282 *
283 * Execute the ATIF_FUNCTION_GET_SYSTEM_PARAMETERS ATIF function
284 * to determine if a notifier is used and if so which one
285 * (all asics). This is either Notify(VGA, 0x81) or Notify(VGA, n)
286 * where n is specified in the result if a notifier is used.
287 * Returns 0 on success, error on failure.
288 */
289static int amdgpu_atif_get_notification_params(struct amdgpu_atif *atif)
290{
291 union acpi_object *info;
292 struct amdgpu_atif_notification_cfg *n = &atif->notification_cfg;
293 struct atif_system_params params;
294 size_t size;
295 int err = 0;
296
297 info = amdgpu_atif_call(atif, ATIF_FUNCTION_GET_SYSTEM_PARAMETERS,
298 NULL);
299 if (!info) {
300 err = -EIO;
301 goto out;
302 }
303
304 size = *(u16 *) info->buffer.pointer;
305 if (size < 10) {
306 err = -EINVAL;
307 goto out;
308 }
309
310 memset(¶ms, 0, sizeof(params));
311 size = min(sizeof(params), size);
312 memcpy(¶ms, info->buffer.pointer, size);
313
314 DRM_DEBUG_DRIVER("SYSTEM_PARAMS: mask = %#x, flags = %#x\n",
315 params.flags, params.valid_mask);
316 params.flags = params.flags & params.valid_mask;
317
318 if ((params.flags & ATIF_NOTIFY_MASK) == ATIF_NOTIFY_NONE) {
319 n->enabled = false;
320 n->command_code = 0;
321 } else if ((params.flags & ATIF_NOTIFY_MASK) == ATIF_NOTIFY_81) {
322 n->enabled = true;
323 n->command_code = 0x81;
324 } else {
325 if (size < 11) {
326 err = -EINVAL;
327 goto out;
328 }
329 n->enabled = true;
330 n->command_code = params.command_code;
331 }
332
333out:
334 DRM_DEBUG_DRIVER("Notification %s, command code = %#x\n",
335 (n->enabled ? "enabled" : "disabled"),
336 n->command_code);
337 kfree(info);
338 return err;
339}
340
341/**
342 * amdgpu_atif_query_backlight_caps - get min and max backlight input signal
343 *
344 * @atif: acpi handle
345 *
346 * Execute the QUERY_BRIGHTNESS_TRANSFER_CHARACTERISTICS ATIF function
347 * to determine the acceptable range of backlight values
348 *
349 * Backlight_caps.caps_valid will be set to true if the query is successful
350 *
351 * The input signals are in range 0-255
352 *
353 * This function assumes the display with backlight is the first LCD
354 *
355 * Returns 0 on success, error on failure.
356 */
357static int amdgpu_atif_query_backlight_caps(struct amdgpu_atif *atif)
358{
359 union acpi_object *info;
360 struct atif_qbtc_output characteristics;
361 struct atif_qbtc_arguments arguments;
362 struct acpi_buffer params;
363 size_t size;
364 int err = 0;
365
366 arguments.size = sizeof(arguments);
367 arguments.requested_display = ATIF_QBTC_REQUEST_LCD1;
368
369 params.length = sizeof(arguments);
370 params.pointer = (void *)&arguments;
371
372 info = amdgpu_atif_call(atif,
373 ATIF_FUNCTION_QUERY_BRIGHTNESS_TRANSFER_CHARACTERISTICS,
374 ¶ms);
375 if (!info) {
376 err = -EIO;
377 goto out;
378 }
379
380 size = *(u16 *) info->buffer.pointer;
381 if (size < 10) {
382 err = -EINVAL;
383 goto out;
384 }
385
386 memset(&characteristics, 0, sizeof(characteristics));
387 size = min(sizeof(characteristics), size);
388 memcpy(&characteristics, info->buffer.pointer, size);
389
390 atif->backlight_caps.caps_valid = true;
391 atif->backlight_caps.min_input_signal =
392 characteristics.min_input_signal;
393 atif->backlight_caps.max_input_signal =
394 characteristics.max_input_signal;
395 atif->backlight_caps.ac_level = characteristics.ac_level;
396 atif->backlight_caps.dc_level = characteristics.dc_level;
397out:
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 */
413static 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
438out:
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 */
455static 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 */
533static 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 */
581static 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 */
600static 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
628out:
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 */
642bool 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 */
659bool 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 */
673int 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 */
701int 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 */
777int 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 */
816int 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
857static 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
873static 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 */
916static 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
942static struct amdgpu_acpi_dev_info *amdgpu_acpi_get_dev(u32 sbdf)
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->sbdf == sbdf)
951 return acpi_dev;
952
953 return NULL;
954}
955
956static int amdgpu_acpi_dev_init(struct amdgpu_acpi_dev_info **dev_info,
957 struct amdgpu_acpi_xcc_info *xcc_info, u32 sbdf)
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->sbdf = sbdf;
971
972 obj = acpi_evaluate_dsm_typed(xcc_info->handle, &amd_xcc_dsm_guid, 0,
973 AMD_XCC_DSM_GET_SUPP_MODE, NULL,
974 ACPI_TYPE_INTEGER);
975
976 if (!obj) {
977 acpi_handle_debug(xcc_info->handle,
978 "_DSM function %d evaluation failed",
979 AMD_XCC_DSM_GET_SUPP_MODE);
980 ret = -ENOENT;
981 goto out;
982 }
983
984 tmp->supp_xcp_mode = obj->integer.value & 0xFFFF;
985 ACPI_FREE(obj);
986
987 obj = acpi_evaluate_dsm_typed(xcc_info->handle, &amd_xcc_dsm_guid, 0,
988 AMD_XCC_DSM_GET_XCP_MODE, NULL,
989 ACPI_TYPE_INTEGER);
990
991 if (!obj) {
992 acpi_handle_debug(xcc_info->handle,
993 "_DSM function %d evaluation failed",
994 AMD_XCC_DSM_GET_XCP_MODE);
995 ret = -ENOENT;
996 goto out;
997 }
998
999 tmp->xcp_mode = obj->integer.value & 0xFFFF;
1000 tmp->mem_mode = (obj->integer.value >> 32) & 0xFFFF;
1001 ACPI_FREE(obj);
1002
1003 /* Evaluate DSMs and fill XCC information */
1004 obj = acpi_evaluate_dsm_typed(xcc_info->handle, &amd_xcc_dsm_guid, 0,
1005 AMD_XCC_DSM_GET_TMR_INFO, NULL,
1006 ACPI_TYPE_PACKAGE);
1007
1008 if (!obj || obj->package.count < 2) {
1009 acpi_handle_debug(xcc_info->handle,
1010 "_DSM function %d evaluation failed",
1011 AMD_XCC_DSM_GET_TMR_INFO);
1012 ret = -ENOENT;
1013 goto out;
1014 }
1015
1016 tmp->tmr_base = obj->package.elements[0].integer.value;
1017 tmp->tmr_size = obj->package.elements[1].integer.value;
1018 ACPI_FREE(obj);
1019
1020 DRM_DEBUG_DRIVER(
1021 "New dev(%x): Supported xcp mode: %x curr xcp_mode : %x mem mode : %x, tmr base: %llx tmr size: %llx ",
1022 tmp->sbdf, tmp->supp_xcp_mode, tmp->xcp_mode, tmp->mem_mode,
1023 tmp->tmr_base, tmp->tmr_size);
1024 list_add_tail(&tmp->list, &amdgpu_acpi_dev_list);
1025 *dev_info = tmp;
1026
1027 return 0;
1028
1029out:
1030 if (obj)
1031 ACPI_FREE(obj);
1032 kfree(tmp);
1033
1034 return ret;
1035}
1036
1037static int amdgpu_acpi_get_xcc_info(struct amdgpu_acpi_xcc_info *xcc_info,
1038 u32 *sbdf)
1039{
1040 union acpi_object *obj;
1041 acpi_status status;
1042 int ret = -ENOENT;
1043
1044 obj = acpi_evaluate_dsm_typed(xcc_info->handle, &amd_xcc_dsm_guid, 0,
1045 AMD_XCC_DSM_GET_NUM_FUNCS, NULL,
1046 ACPI_TYPE_INTEGER);
1047
1048 if (!obj || obj->integer.value != AMD_XCC_DSM_NUM_FUNCS)
1049 goto out;
1050 ACPI_FREE(obj);
1051
1052 /* Evaluate DSMs and fill XCC information */
1053 obj = acpi_evaluate_dsm_typed(xcc_info->handle, &amd_xcc_dsm_guid, 0,
1054 AMD_XCC_DSM_GET_VF_XCC_MAPPING, NULL,
1055 ACPI_TYPE_INTEGER);
1056
1057 if (!obj) {
1058 acpi_handle_debug(xcc_info->handle,
1059 "_DSM function %d evaluation failed",
1060 AMD_XCC_DSM_GET_VF_XCC_MAPPING);
1061 ret = -EINVAL;
1062 goto out;
1063 }
1064
1065 /* PF xcc id [39:32] */
1066 xcc_info->phy_id = (obj->integer.value >> 32) & 0xFF;
1067 /* xcp node of this xcc [47:40] */
1068 xcc_info->xcp_node = (obj->integer.value >> 40) & 0xFF;
1069 /* PF domain of this xcc [31:16] */
1070 *sbdf = (obj->integer.value) & 0xFFFF0000;
1071 /* PF bus/dev/fn of this xcc [63:48] */
1072 *sbdf |= (obj->integer.value >> 48) & 0xFFFF;
1073 ACPI_FREE(obj);
1074 obj = NULL;
1075
1076 status =
1077 amdgpu_acpi_get_node_id(xcc_info->handle, &xcc_info->numa_info);
1078
1079 /* TODO: check if this check is required */
1080 if (ACPI_SUCCESS(status))
1081 ret = 0;
1082out:
1083 if (obj)
1084 ACPI_FREE(obj);
1085
1086 return ret;
1087}
1088
1089static int amdgpu_acpi_enumerate_xcc(void)
1090{
1091 struct amdgpu_acpi_dev_info *dev_info = NULL;
1092 struct amdgpu_acpi_xcc_info *xcc_info;
1093 struct acpi_device *acpi_dev;
1094 char hid[ACPI_ID_LEN];
1095 int ret, id;
1096 u32 sbdf;
1097
1098 INIT_LIST_HEAD(&amdgpu_acpi_dev_list);
1099 xa_init(&numa_info_xa);
1100
1101 for (id = 0; id < AMD_XCC_MAX_HID; id++) {
1102 sprintf(hid, "%s%d", "AMD", AMD_XCC_HID_START + id);
1103 acpi_dev = acpi_dev_get_first_match_dev(hid, NULL, -1);
1104 /* These ACPI objects are expected to be in sequential order. If
1105 * one is not found, no need to check the rest.
1106 */
1107 if (!acpi_dev) {
1108 DRM_DEBUG_DRIVER("No matching acpi device found for %s",
1109 hid);
1110 break;
1111 }
1112
1113 xcc_info = kzalloc(sizeof(struct amdgpu_acpi_xcc_info),
1114 GFP_KERNEL);
1115 if (!xcc_info) {
1116 DRM_ERROR("Failed to allocate memory for xcc info\n");
1117 return -ENOMEM;
1118 }
1119
1120 INIT_LIST_HEAD(&xcc_info->list);
1121 xcc_info->handle = acpi_device_handle(acpi_dev);
1122 acpi_dev_put(acpi_dev);
1123
1124 ret = amdgpu_acpi_get_xcc_info(xcc_info, &sbdf);
1125 if (ret) {
1126 kfree(xcc_info);
1127 continue;
1128 }
1129
1130 dev_info = amdgpu_acpi_get_dev(sbdf);
1131
1132 if (!dev_info)
1133 ret = amdgpu_acpi_dev_init(&dev_info, xcc_info, sbdf);
1134
1135 if (ret == -ENOMEM)
1136 return ret;
1137
1138 if (!dev_info) {
1139 kfree(xcc_info);
1140 continue;
1141 }
1142
1143 list_add_tail(&xcc_info->list, &dev_info->xcc_list);
1144 }
1145
1146 return 0;
1147}
1148
1149int amdgpu_acpi_get_tmr_info(struct amdgpu_device *adev, u64 *tmr_offset,
1150 u64 *tmr_size)
1151{
1152 struct amdgpu_acpi_dev_info *dev_info;
1153 u32 sbdf;
1154
1155 if (!tmr_offset || !tmr_size)
1156 return -EINVAL;
1157
1158 sbdf = (pci_domain_nr(adev->pdev->bus) << 16);
1159 sbdf |= pci_dev_id(adev->pdev);
1160 dev_info = amdgpu_acpi_get_dev(sbdf);
1161 if (!dev_info)
1162 return -ENOENT;
1163
1164 *tmr_offset = dev_info->tmr_base;
1165 *tmr_size = dev_info->tmr_size;
1166
1167 return 0;
1168}
1169
1170int amdgpu_acpi_get_mem_info(struct amdgpu_device *adev, int xcc_id,
1171 struct amdgpu_numa_info *numa_info)
1172{
1173 struct amdgpu_acpi_dev_info *dev_info;
1174 struct amdgpu_acpi_xcc_info *xcc_info;
1175 u32 sbdf;
1176
1177 if (!numa_info)
1178 return -EINVAL;
1179
1180 sbdf = (pci_domain_nr(adev->pdev->bus) << 16);
1181 sbdf |= pci_dev_id(adev->pdev);
1182 dev_info = amdgpu_acpi_get_dev(sbdf);
1183 if (!dev_info)
1184 return -ENOENT;
1185
1186 list_for_each_entry(xcc_info, &dev_info->xcc_list, list) {
1187 if (xcc_info->phy_id == xcc_id) {
1188 memcpy(numa_info, xcc_info->numa_info,
1189 sizeof(*numa_info));
1190 return 0;
1191 }
1192 }
1193
1194 return -ENOENT;
1195}
1196
1197/**
1198 * amdgpu_acpi_event - handle notify events
1199 *
1200 * @nb: notifier block
1201 * @val: val
1202 * @data: acpi event
1203 *
1204 * Calls relevant amdgpu functions in response to various
1205 * acpi events.
1206 * Returns NOTIFY code
1207 */
1208static int amdgpu_acpi_event(struct notifier_block *nb,
1209 unsigned long val,
1210 void *data)
1211{
1212 struct amdgpu_device *adev = container_of(nb, struct amdgpu_device, acpi_nb);
1213 struct acpi_bus_event *entry = (struct acpi_bus_event *)data;
1214
1215 if (strcmp(entry->device_class, ACPI_AC_CLASS) == 0) {
1216 if (power_supply_is_system_supplied() > 0)
1217 DRM_DEBUG_DRIVER("pm: AC\n");
1218 else
1219 DRM_DEBUG_DRIVER("pm: DC\n");
1220
1221 amdgpu_pm_acpi_event_handler(adev);
1222 }
1223
1224 /* Check for pending SBIOS requests */
1225 return amdgpu_atif_handler(adev, entry);
1226}
1227
1228/* Call all ACPI methods here */
1229/**
1230 * amdgpu_acpi_init - init driver acpi support
1231 *
1232 * @adev: amdgpu_device pointer
1233 *
1234 * Verifies the AMD ACPI interfaces and registers with the acpi
1235 * notifier chain (all asics).
1236 * Returns 0 on success, error on failure.
1237 */
1238int amdgpu_acpi_init(struct amdgpu_device *adev)
1239{
1240 struct amdgpu_atif *atif = &amdgpu_acpi_priv.atif;
1241
1242 if (atif->notifications.brightness_change) {
1243 if (adev->dc_enabled) {
1244#if defined(CONFIG_DRM_AMD_DC)
1245 struct amdgpu_display_manager *dm = &adev->dm;
1246
1247 if (dm->backlight_dev[0])
1248 atif->bd = dm->backlight_dev[0];
1249#endif
1250 } else {
1251 struct drm_encoder *tmp;
1252
1253 /* Find the encoder controlling the brightness */
1254 list_for_each_entry(tmp, &adev_to_drm(adev)->mode_config.encoder_list,
1255 head) {
1256 struct amdgpu_encoder *enc = to_amdgpu_encoder(tmp);
1257
1258 if ((enc->devices & (ATOM_DEVICE_LCD_SUPPORT)) &&
1259 enc->enc_priv) {
1260 struct amdgpu_encoder_atom_dig *dig = enc->enc_priv;
1261
1262 if (dig->bl_dev) {
1263 atif->bd = dig->bl_dev;
1264 break;
1265 }
1266 }
1267 }
1268 }
1269 }
1270 adev->acpi_nb.notifier_call = amdgpu_acpi_event;
1271 register_acpi_notifier(&adev->acpi_nb);
1272
1273 return 0;
1274}
1275
1276void amdgpu_acpi_get_backlight_caps(struct amdgpu_dm_backlight_caps *caps)
1277{
1278 struct amdgpu_atif *atif = &amdgpu_acpi_priv.atif;
1279
1280 caps->caps_valid = atif->backlight_caps.caps_valid;
1281 caps->min_input_signal = atif->backlight_caps.min_input_signal;
1282 caps->max_input_signal = atif->backlight_caps.max_input_signal;
1283 caps->ac_level = atif->backlight_caps.ac_level;
1284 caps->dc_level = atif->backlight_caps.dc_level;
1285}
1286
1287/**
1288 * amdgpu_acpi_fini - tear down driver acpi support
1289 *
1290 * @adev: amdgpu_device pointer
1291 *
1292 * Unregisters with the acpi notifier chain (all asics).
1293 */
1294void amdgpu_acpi_fini(struct amdgpu_device *adev)
1295{
1296 unregister_acpi_notifier(&adev->acpi_nb);
1297}
1298
1299/**
1300 * amdgpu_atif_pci_probe_handle - look up the ATIF handle
1301 *
1302 * @pdev: pci device
1303 *
1304 * Look up the ATIF handles (all asics).
1305 * Returns true if the handle is found, false if not.
1306 */
1307static bool amdgpu_atif_pci_probe_handle(struct pci_dev *pdev)
1308{
1309 char acpi_method_name[255] = { 0 };
1310 struct acpi_buffer buffer = {sizeof(acpi_method_name), acpi_method_name};
1311 acpi_handle dhandle, atif_handle;
1312 acpi_status status;
1313 int ret;
1314
1315 dhandle = ACPI_HANDLE(&pdev->dev);
1316 if (!dhandle)
1317 return false;
1318
1319 status = acpi_get_handle(dhandle, "ATIF", &atif_handle);
1320 if (ACPI_FAILURE(status))
1321 return false;
1322
1323 amdgpu_acpi_priv.atif.handle = atif_handle;
1324 acpi_get_name(amdgpu_acpi_priv.atif.handle, ACPI_FULL_PATHNAME, &buffer);
1325 DRM_DEBUG_DRIVER("Found ATIF handle %s\n", acpi_method_name);
1326 ret = amdgpu_atif_verify_interface(&amdgpu_acpi_priv.atif);
1327 if (ret) {
1328 amdgpu_acpi_priv.atif.handle = 0;
1329 return false;
1330 }
1331 return true;
1332}
1333
1334/**
1335 * amdgpu_atcs_pci_probe_handle - look up the ATCS handle
1336 *
1337 * @pdev: pci device
1338 *
1339 * Look up the ATCS handles (all asics).
1340 * Returns true if the handle is found, false if not.
1341 */
1342static bool amdgpu_atcs_pci_probe_handle(struct pci_dev *pdev)
1343{
1344 char acpi_method_name[255] = { 0 };
1345 struct acpi_buffer buffer = { sizeof(acpi_method_name), acpi_method_name };
1346 acpi_handle dhandle, atcs_handle;
1347 acpi_status status;
1348 int ret;
1349
1350 dhandle = ACPI_HANDLE(&pdev->dev);
1351 if (!dhandle)
1352 return false;
1353
1354 status = acpi_get_handle(dhandle, "ATCS", &atcs_handle);
1355 if (ACPI_FAILURE(status))
1356 return false;
1357
1358 amdgpu_acpi_priv.atcs.handle = atcs_handle;
1359 acpi_get_name(amdgpu_acpi_priv.atcs.handle, ACPI_FULL_PATHNAME, &buffer);
1360 DRM_DEBUG_DRIVER("Found ATCS handle %s\n", acpi_method_name);
1361 ret = amdgpu_atcs_verify_interface(&amdgpu_acpi_priv.atcs);
1362 if (ret) {
1363 amdgpu_acpi_priv.atcs.handle = 0;
1364 return false;
1365 }
1366 return true;
1367}
1368
1369
1370/**
1371 * amdgpu_acpi_should_gpu_reset
1372 *
1373 * @adev: amdgpu_device_pointer
1374 *
1375 * returns true if should reset GPU, false if not
1376 */
1377bool amdgpu_acpi_should_gpu_reset(struct amdgpu_device *adev)
1378{
1379 if ((adev->flags & AMD_IS_APU) &&
1380 adev->gfx.imu.funcs) /* Not need to do mode2 reset for IMU enabled APUs */
1381 return false;
1382
1383 if ((adev->flags & AMD_IS_APU) &&
1384 amdgpu_acpi_is_s3_active(adev))
1385 return false;
1386
1387 if (amdgpu_sriov_vf(adev))
1388 return false;
1389
1390#if IS_ENABLED(CONFIG_SUSPEND)
1391 return pm_suspend_target_state != PM_SUSPEND_TO_IDLE;
1392#else
1393 return true;
1394#endif
1395}
1396
1397/*
1398 * amdgpu_acpi_detect - detect ACPI ATIF/ATCS methods
1399 *
1400 * Check if we have the ATIF/ATCS methods and populate
1401 * the structures in the driver.
1402 */
1403void amdgpu_acpi_detect(void)
1404{
1405 struct amdgpu_atif *atif = &amdgpu_acpi_priv.atif;
1406 struct amdgpu_atcs *atcs = &amdgpu_acpi_priv.atcs;
1407 struct pci_dev *pdev = NULL;
1408 int ret;
1409
1410 while ((pdev = pci_get_base_class(PCI_BASE_CLASS_DISPLAY, pdev))) {
1411 if ((pdev->class != PCI_CLASS_DISPLAY_VGA << 8) &&
1412 (pdev->class != PCI_CLASS_DISPLAY_OTHER << 8))
1413 continue;
1414
1415 if (!atif->handle)
1416 amdgpu_atif_pci_probe_handle(pdev);
1417 if (!atcs->handle)
1418 amdgpu_atcs_pci_probe_handle(pdev);
1419 }
1420
1421 if (atif->functions.sbios_requests && !atif->functions.system_params) {
1422 /* XXX check this workraround, if sbios request function is
1423 * present we have to see how it's configured in the system
1424 * params
1425 */
1426 atif->functions.system_params = true;
1427 }
1428
1429 if (atif->functions.system_params) {
1430 ret = amdgpu_atif_get_notification_params(atif);
1431 if (ret) {
1432 DRM_DEBUG_DRIVER("Call to GET_SYSTEM_PARAMS failed: %d\n",
1433 ret);
1434 /* Disable notification */
1435 atif->notification_cfg.enabled = false;
1436 }
1437 }
1438
1439 if (atif->functions.query_backlight_transfer_characteristics) {
1440 ret = amdgpu_atif_query_backlight_caps(atif);
1441 if (ret) {
1442 DRM_DEBUG_DRIVER("Call to QUERY_BACKLIGHT_TRANSFER_CHARACTERISTICS failed: %d\n",
1443 ret);
1444 atif->backlight_caps.caps_valid = false;
1445 }
1446 } else {
1447 atif->backlight_caps.caps_valid = false;
1448 }
1449
1450 amdgpu_acpi_enumerate_xcc();
1451}
1452
1453void amdgpu_acpi_release(void)
1454{
1455 struct amdgpu_acpi_dev_info *dev_info, *dev_tmp;
1456 struct amdgpu_acpi_xcc_info *xcc_info, *xcc_tmp;
1457 struct amdgpu_numa_info *numa_info;
1458 unsigned long index;
1459
1460 xa_for_each(&numa_info_xa, index, numa_info) {
1461 kfree(numa_info);
1462 xa_erase(&numa_info_xa, index);
1463 }
1464
1465 if (list_empty(&amdgpu_acpi_dev_list))
1466 return;
1467
1468 list_for_each_entry_safe(dev_info, dev_tmp, &amdgpu_acpi_dev_list,
1469 list) {
1470 list_for_each_entry_safe(xcc_info, xcc_tmp, &dev_info->xcc_list,
1471 list) {
1472 list_del(&xcc_info->list);
1473 kfree(xcc_info);
1474 }
1475
1476 list_del(&dev_info->list);
1477 kfree(dev_info);
1478 }
1479}
1480
1481#if IS_ENABLED(CONFIG_SUSPEND)
1482/**
1483 * amdgpu_acpi_is_s3_active
1484 *
1485 * @adev: amdgpu_device_pointer
1486 *
1487 * returns true if supported, false if not.
1488 */
1489bool amdgpu_acpi_is_s3_active(struct amdgpu_device *adev)
1490{
1491 return !(adev->flags & AMD_IS_APU) ||
1492 (pm_suspend_target_state == PM_SUSPEND_MEM);
1493}
1494
1495/**
1496 * amdgpu_acpi_is_s0ix_active
1497 *
1498 * @adev: amdgpu_device_pointer
1499 *
1500 * returns true if supported, false if not.
1501 */
1502bool amdgpu_acpi_is_s0ix_active(struct amdgpu_device *adev)
1503{
1504 if (!(adev->flags & AMD_IS_APU) ||
1505 (pm_suspend_target_state != PM_SUSPEND_TO_IDLE))
1506 return false;
1507
1508 if (adev->asic_type < CHIP_RAVEN)
1509 return false;
1510
1511 if (!(adev->pm.pp_feature & PP_GFXOFF_MASK))
1512 return false;
1513
1514 /*
1515 * If ACPI_FADT_LOW_POWER_S0 is not set in the FADT, it is generally
1516 * risky to do any special firmware-related preparations for entering
1517 * S0ix even though the system is suspending to idle, so return false
1518 * in that case.
1519 */
1520 if (!(acpi_gbl_FADT.flags & ACPI_FADT_LOW_POWER_S0)) {
1521 dev_err_once(adev->dev,
1522 "Power consumption will be higher as BIOS has not been configured for suspend-to-idle.\n"
1523 "To use suspend-to-idle change the sleep mode in BIOS setup.\n");
1524 return false;
1525 }
1526
1527#if !IS_ENABLED(CONFIG_AMD_PMC)
1528 dev_err_once(adev->dev,
1529 "Power consumption will be higher as the kernel has not been compiled with CONFIG_AMD_PMC.\n");
1530 return false;
1531#else
1532 return true;
1533#endif /* CONFIG_AMD_PMC */
1534}
1535
1536/**
1537 * amdgpu_choose_low_power_state
1538 *
1539 * @adev: amdgpu_device_pointer
1540 *
1541 * Choose the target low power state for the GPU
1542 */
1543void amdgpu_choose_low_power_state(struct amdgpu_device *adev)
1544{
1545 if (adev->in_runpm)
1546 return;
1547
1548 if (amdgpu_acpi_is_s0ix_active(adev))
1549 adev->in_s0ix = true;
1550 else if (amdgpu_acpi_is_s3_active(adev))
1551 adev->in_s3 = true;
1552}
1553
1554#endif /* CONFIG_SUSPEND */
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#include <drm/drmP.h>
31#include <drm/drm_crtc_helper.h>
32#include "amdgpu.h"
33#include "amd_acpi.h"
34#include "atom.h"
35
36extern void amdgpu_pm_acpi_event_handler(struct amdgpu_device *adev);
37/* Call the ATIF method
38 */
39/**
40 * amdgpu_atif_call - call an ATIF method
41 *
42 * @handle: acpi handle
43 * @function: the ATIF function to execute
44 * @params: ATIF function params
45 *
46 * Executes the requested ATIF function (all asics).
47 * Returns a pointer to the acpi output buffer.
48 */
49static union acpi_object *amdgpu_atif_call(acpi_handle handle, int function,
50 struct acpi_buffer *params)
51{
52 acpi_status status;
53 union acpi_object atif_arg_elements[2];
54 struct acpi_object_list atif_arg;
55 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
56
57 atif_arg.count = 2;
58 atif_arg.pointer = &atif_arg_elements[0];
59
60 atif_arg_elements[0].type = ACPI_TYPE_INTEGER;
61 atif_arg_elements[0].integer.value = function;
62
63 if (params) {
64 atif_arg_elements[1].type = ACPI_TYPE_BUFFER;
65 atif_arg_elements[1].buffer.length = params->length;
66 atif_arg_elements[1].buffer.pointer = params->pointer;
67 } else {
68 /* We need a second fake parameter */
69 atif_arg_elements[1].type = ACPI_TYPE_INTEGER;
70 atif_arg_elements[1].integer.value = 0;
71 }
72
73 status = acpi_evaluate_object(handle, "ATIF", &atif_arg, &buffer);
74
75 /* Fail only if calling the method fails and ATIF is supported */
76 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
77 DRM_DEBUG_DRIVER("failed to evaluate ATIF got %s\n",
78 acpi_format_exception(status));
79 kfree(buffer.pointer);
80 return NULL;
81 }
82
83 return buffer.pointer;
84}
85
86/**
87 * amdgpu_atif_parse_notification - parse supported notifications
88 *
89 * @n: supported notifications struct
90 * @mask: supported notifications mask from ATIF
91 *
92 * Use the supported notifications mask from ATIF function
93 * ATIF_FUNCTION_VERIFY_INTERFACE to determine what notifications
94 * are supported (all asics).
95 */
96static void amdgpu_atif_parse_notification(struct amdgpu_atif_notifications *n, u32 mask)
97{
98 n->display_switch = mask & ATIF_DISPLAY_SWITCH_REQUEST_SUPPORTED;
99 n->expansion_mode_change = mask & ATIF_EXPANSION_MODE_CHANGE_REQUEST_SUPPORTED;
100 n->thermal_state = mask & ATIF_THERMAL_STATE_CHANGE_REQUEST_SUPPORTED;
101 n->forced_power_state = mask & ATIF_FORCED_POWER_STATE_CHANGE_REQUEST_SUPPORTED;
102 n->system_power_state = mask & ATIF_SYSTEM_POWER_SOURCE_CHANGE_REQUEST_SUPPORTED;
103 n->display_conf_change = mask & ATIF_DISPLAY_CONF_CHANGE_REQUEST_SUPPORTED;
104 n->px_gfx_switch = mask & ATIF_PX_GFX_SWITCH_REQUEST_SUPPORTED;
105 n->brightness_change = mask & ATIF_PANEL_BRIGHTNESS_CHANGE_REQUEST_SUPPORTED;
106 n->dgpu_display_event = mask & ATIF_DGPU_DISPLAY_EVENT_SUPPORTED;
107}
108
109/**
110 * amdgpu_atif_parse_functions - parse supported functions
111 *
112 * @f: supported functions struct
113 * @mask: supported functions mask from ATIF
114 *
115 * Use the supported functions mask from ATIF function
116 * ATIF_FUNCTION_VERIFY_INTERFACE to determine what functions
117 * are supported (all asics).
118 */
119static void amdgpu_atif_parse_functions(struct amdgpu_atif_functions *f, u32 mask)
120{
121 f->system_params = mask & ATIF_GET_SYSTEM_PARAMETERS_SUPPORTED;
122 f->sbios_requests = mask & ATIF_GET_SYSTEM_BIOS_REQUESTS_SUPPORTED;
123 f->select_active_disp = mask & ATIF_SELECT_ACTIVE_DISPLAYS_SUPPORTED;
124 f->lid_state = mask & ATIF_GET_LID_STATE_SUPPORTED;
125 f->get_tv_standard = mask & ATIF_GET_TV_STANDARD_FROM_CMOS_SUPPORTED;
126 f->set_tv_standard = mask & ATIF_SET_TV_STANDARD_IN_CMOS_SUPPORTED;
127 f->get_panel_expansion_mode = mask & ATIF_GET_PANEL_EXPANSION_MODE_FROM_CMOS_SUPPORTED;
128 f->set_panel_expansion_mode = mask & ATIF_SET_PANEL_EXPANSION_MODE_IN_CMOS_SUPPORTED;
129 f->temperature_change = mask & ATIF_TEMPERATURE_CHANGE_NOTIFICATION_SUPPORTED;
130 f->graphics_device_types = mask & ATIF_GET_GRAPHICS_DEVICE_TYPES_SUPPORTED;
131}
132
133/**
134 * amdgpu_atif_verify_interface - verify ATIF
135 *
136 * @handle: acpi handle
137 * @atif: amdgpu atif struct
138 *
139 * Execute the ATIF_FUNCTION_VERIFY_INTERFACE ATIF function
140 * to initialize ATIF and determine what features are supported
141 * (all asics).
142 * returns 0 on success, error on failure.
143 */
144static int amdgpu_atif_verify_interface(acpi_handle handle,
145 struct amdgpu_atif *atif)
146{
147 union acpi_object *info;
148 struct atif_verify_interface output;
149 size_t size;
150 int err = 0;
151
152 info = amdgpu_atif_call(handle, ATIF_FUNCTION_VERIFY_INTERFACE, NULL);
153 if (!info)
154 return -EIO;
155
156 memset(&output, 0, sizeof(output));
157
158 size = *(u16 *) info->buffer.pointer;
159 if (size < 12) {
160 DRM_INFO("ATIF buffer is too small: %zu\n", size);
161 err = -EINVAL;
162 goto out;
163 }
164 size = min(sizeof(output), size);
165
166 memcpy(&output, info->buffer.pointer, size);
167
168 /* TODO: check version? */
169 DRM_DEBUG_DRIVER("ATIF version %u\n", output.version);
170
171 amdgpu_atif_parse_notification(&atif->notifications, output.notification_mask);
172 amdgpu_atif_parse_functions(&atif->functions, output.function_bits);
173
174out:
175 kfree(info);
176 return err;
177}
178
179/**
180 * amdgpu_atif_get_notification_params - determine notify configuration
181 *
182 * @handle: acpi handle
183 * @n: atif notification configuration struct
184 *
185 * Execute the ATIF_FUNCTION_GET_SYSTEM_PARAMETERS ATIF function
186 * to determine if a notifier is used and if so which one
187 * (all asics). This is either Notify(VGA, 0x81) or Notify(VGA, n)
188 * where n is specified in the result if a notifier is used.
189 * Returns 0 on success, error on failure.
190 */
191static int amdgpu_atif_get_notification_params(acpi_handle handle,
192 struct amdgpu_atif_notification_cfg *n)
193{
194 union acpi_object *info;
195 struct atif_system_params params;
196 size_t size;
197 int err = 0;
198
199 info = amdgpu_atif_call(handle, ATIF_FUNCTION_GET_SYSTEM_PARAMETERS, NULL);
200 if (!info) {
201 err = -EIO;
202 goto out;
203 }
204
205 size = *(u16 *) info->buffer.pointer;
206 if (size < 10) {
207 err = -EINVAL;
208 goto out;
209 }
210
211 memset(¶ms, 0, sizeof(params));
212 size = min(sizeof(params), size);
213 memcpy(¶ms, info->buffer.pointer, size);
214
215 DRM_DEBUG_DRIVER("SYSTEM_PARAMS: mask = %#x, flags = %#x\n",
216 params.flags, params.valid_mask);
217 params.flags = params.flags & params.valid_mask;
218
219 if ((params.flags & ATIF_NOTIFY_MASK) == ATIF_NOTIFY_NONE) {
220 n->enabled = false;
221 n->command_code = 0;
222 } else if ((params.flags & ATIF_NOTIFY_MASK) == ATIF_NOTIFY_81) {
223 n->enabled = true;
224 n->command_code = 0x81;
225 } else {
226 if (size < 11) {
227 err = -EINVAL;
228 goto out;
229 }
230 n->enabled = true;
231 n->command_code = params.command_code;
232 }
233
234out:
235 DRM_DEBUG_DRIVER("Notification %s, command code = %#x\n",
236 (n->enabled ? "enabled" : "disabled"),
237 n->command_code);
238 kfree(info);
239 return err;
240}
241
242/**
243 * amdgpu_atif_get_sbios_requests - get requested sbios event
244 *
245 * @handle: acpi handle
246 * @req: atif sbios request struct
247 *
248 * Execute the ATIF_FUNCTION_GET_SYSTEM_BIOS_REQUESTS ATIF function
249 * to determine what requests the sbios is making to the driver
250 * (all asics).
251 * Returns 0 on success, error on failure.
252 */
253static int amdgpu_atif_get_sbios_requests(acpi_handle handle,
254 struct atif_sbios_requests *req)
255{
256 union acpi_object *info;
257 size_t size;
258 int count = 0;
259
260 info = amdgpu_atif_call(handle, ATIF_FUNCTION_GET_SYSTEM_BIOS_REQUESTS, NULL);
261 if (!info)
262 return -EIO;
263
264 size = *(u16 *)info->buffer.pointer;
265 if (size < 0xd) {
266 count = -EINVAL;
267 goto out;
268 }
269 memset(req, 0, sizeof(*req));
270
271 size = min(sizeof(*req), size);
272 memcpy(req, info->buffer.pointer, size);
273 DRM_DEBUG_DRIVER("SBIOS pending requests: %#x\n", req->pending);
274
275 count = hweight32(req->pending);
276
277out:
278 kfree(info);
279 return count;
280}
281
282/**
283 * amdgpu_atif_handler - handle ATIF notify requests
284 *
285 * @adev: amdgpu_device pointer
286 * @event: atif sbios request struct
287 *
288 * Checks the acpi event and if it matches an atif event,
289 * handles it.
290 * Returns NOTIFY code
291 */
292int amdgpu_atif_handler(struct amdgpu_device *adev,
293 struct acpi_bus_event *event)
294{
295 struct amdgpu_atif *atif = &adev->atif;
296 struct atif_sbios_requests req;
297 acpi_handle handle;
298 int count;
299
300 DRM_DEBUG_DRIVER("event, device_class = %s, type = %#x\n",
301 event->device_class, event->type);
302
303 if (strcmp(event->device_class, ACPI_VIDEO_CLASS) != 0)
304 return NOTIFY_DONE;
305
306 if (!atif->notification_cfg.enabled ||
307 event->type != atif->notification_cfg.command_code)
308 /* Not our event */
309 return NOTIFY_DONE;
310
311 /* Check pending SBIOS requests */
312 handle = ACPI_HANDLE(&adev->pdev->dev);
313 count = amdgpu_atif_get_sbios_requests(handle, &req);
314
315 if (count <= 0)
316 return NOTIFY_DONE;
317
318 DRM_DEBUG_DRIVER("ATIF: %d pending SBIOS requests\n", count);
319
320 if (req.pending & ATIF_PANEL_BRIGHTNESS_CHANGE_REQUEST) {
321 struct amdgpu_encoder *enc = atif->encoder_for_bl;
322
323 if (enc) {
324 struct amdgpu_encoder_atom_dig *dig = enc->enc_priv;
325
326 DRM_DEBUG_DRIVER("Changing brightness to %d\n",
327 req.backlight_level);
328
329 amdgpu_display_backlight_set_level(adev, enc, req.backlight_level);
330
331#if defined(CONFIG_BACKLIGHT_CLASS_DEVICE) || defined(CONFIG_BACKLIGHT_CLASS_DEVICE_MODULE)
332 backlight_force_update(dig->bl_dev,
333 BACKLIGHT_UPDATE_HOTKEY);
334#endif
335 }
336 }
337 if (req.pending & ATIF_DGPU_DISPLAY_EVENT) {
338 if ((adev->flags & AMD_IS_PX) &&
339 amdgpu_atpx_dgpu_req_power_for_displays()) {
340 pm_runtime_get_sync(adev->ddev->dev);
341 /* Just fire off a uevent and let userspace tell us what to do */
342 drm_helper_hpd_irq_event(adev->ddev);
343 pm_runtime_mark_last_busy(adev->ddev->dev);
344 pm_runtime_put_autosuspend(adev->ddev->dev);
345 }
346 }
347 /* TODO: check other events */
348
349 /* We've handled the event, stop the notifier chain. The ACPI interface
350 * overloads ACPI_VIDEO_NOTIFY_PROBE, we don't want to send that to
351 * userspace if the event was generated only to signal a SBIOS
352 * request.
353 */
354 return NOTIFY_BAD;
355}
356
357/* Call the ATCS method
358 */
359/**
360 * amdgpu_atcs_call - call an ATCS method
361 *
362 * @handle: acpi handle
363 * @function: the ATCS function to execute
364 * @params: ATCS function params
365 *
366 * Executes the requested ATCS function (all asics).
367 * Returns a pointer to the acpi output buffer.
368 */
369static union acpi_object *amdgpu_atcs_call(acpi_handle handle, int function,
370 struct acpi_buffer *params)
371{
372 acpi_status status;
373 union acpi_object atcs_arg_elements[2];
374 struct acpi_object_list atcs_arg;
375 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
376
377 atcs_arg.count = 2;
378 atcs_arg.pointer = &atcs_arg_elements[0];
379
380 atcs_arg_elements[0].type = ACPI_TYPE_INTEGER;
381 atcs_arg_elements[0].integer.value = function;
382
383 if (params) {
384 atcs_arg_elements[1].type = ACPI_TYPE_BUFFER;
385 atcs_arg_elements[1].buffer.length = params->length;
386 atcs_arg_elements[1].buffer.pointer = params->pointer;
387 } else {
388 /* We need a second fake parameter */
389 atcs_arg_elements[1].type = ACPI_TYPE_INTEGER;
390 atcs_arg_elements[1].integer.value = 0;
391 }
392
393 status = acpi_evaluate_object(handle, "ATCS", &atcs_arg, &buffer);
394
395 /* Fail only if calling the method fails and ATIF is supported */
396 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
397 DRM_DEBUG_DRIVER("failed to evaluate ATCS got %s\n",
398 acpi_format_exception(status));
399 kfree(buffer.pointer);
400 return NULL;
401 }
402
403 return buffer.pointer;
404}
405
406/**
407 * amdgpu_atcs_parse_functions - parse supported functions
408 *
409 * @f: supported functions struct
410 * @mask: supported functions mask from ATCS
411 *
412 * Use the supported functions mask from ATCS function
413 * ATCS_FUNCTION_VERIFY_INTERFACE to determine what functions
414 * are supported (all asics).
415 */
416static void amdgpu_atcs_parse_functions(struct amdgpu_atcs_functions *f, u32 mask)
417{
418 f->get_ext_state = mask & ATCS_GET_EXTERNAL_STATE_SUPPORTED;
419 f->pcie_perf_req = mask & ATCS_PCIE_PERFORMANCE_REQUEST_SUPPORTED;
420 f->pcie_dev_rdy = mask & ATCS_PCIE_DEVICE_READY_NOTIFICATION_SUPPORTED;
421 f->pcie_bus_width = mask & ATCS_SET_PCIE_BUS_WIDTH_SUPPORTED;
422}
423
424/**
425 * amdgpu_atcs_verify_interface - verify ATCS
426 *
427 * @handle: acpi handle
428 * @atcs: amdgpu atcs struct
429 *
430 * Execute the ATCS_FUNCTION_VERIFY_INTERFACE ATCS function
431 * to initialize ATCS and determine what features are supported
432 * (all asics).
433 * returns 0 on success, error on failure.
434 */
435static int amdgpu_atcs_verify_interface(acpi_handle handle,
436 struct amdgpu_atcs *atcs)
437{
438 union acpi_object *info;
439 struct atcs_verify_interface output;
440 size_t size;
441 int err = 0;
442
443 info = amdgpu_atcs_call(handle, ATCS_FUNCTION_VERIFY_INTERFACE, NULL);
444 if (!info)
445 return -EIO;
446
447 memset(&output, 0, sizeof(output));
448
449 size = *(u16 *) info->buffer.pointer;
450 if (size < 8) {
451 DRM_INFO("ATCS buffer is too small: %zu\n", size);
452 err = -EINVAL;
453 goto out;
454 }
455 size = min(sizeof(output), size);
456
457 memcpy(&output, info->buffer.pointer, size);
458
459 /* TODO: check version? */
460 DRM_DEBUG_DRIVER("ATCS version %u\n", output.version);
461
462 amdgpu_atcs_parse_functions(&atcs->functions, output.function_bits);
463
464out:
465 kfree(info);
466 return err;
467}
468
469/**
470 * amdgpu_acpi_is_pcie_performance_request_supported
471 *
472 * @adev: amdgpu_device pointer
473 *
474 * Check if the ATCS pcie_perf_req and pcie_dev_rdy methods
475 * are supported (all asics).
476 * returns true if supported, false if not.
477 */
478bool amdgpu_acpi_is_pcie_performance_request_supported(struct amdgpu_device *adev)
479{
480 struct amdgpu_atcs *atcs = &adev->atcs;
481
482 if (atcs->functions.pcie_perf_req && atcs->functions.pcie_dev_rdy)
483 return true;
484
485 return false;
486}
487
488/**
489 * amdgpu_acpi_pcie_notify_device_ready
490 *
491 * @adev: amdgpu_device pointer
492 *
493 * Executes the PCIE_DEVICE_READY_NOTIFICATION method
494 * (all asics).
495 * returns 0 on success, error on failure.
496 */
497int amdgpu_acpi_pcie_notify_device_ready(struct amdgpu_device *adev)
498{
499 acpi_handle handle;
500 union acpi_object *info;
501 struct amdgpu_atcs *atcs = &adev->atcs;
502
503 /* Get the device handle */
504 handle = ACPI_HANDLE(&adev->pdev->dev);
505 if (!handle)
506 return -EINVAL;
507
508 if (!atcs->functions.pcie_dev_rdy)
509 return -EINVAL;
510
511 info = amdgpu_atcs_call(handle, ATCS_FUNCTION_PCIE_DEVICE_READY_NOTIFICATION, NULL);
512 if (!info)
513 return -EIO;
514
515 kfree(info);
516
517 return 0;
518}
519
520/**
521 * amdgpu_acpi_pcie_performance_request
522 *
523 * @adev: amdgpu_device pointer
524 * @perf_req: requested perf level (pcie gen speed)
525 * @advertise: set advertise caps flag if set
526 *
527 * Executes the PCIE_PERFORMANCE_REQUEST method to
528 * change the pcie gen speed (all asics).
529 * returns 0 on success, error on failure.
530 */
531int amdgpu_acpi_pcie_performance_request(struct amdgpu_device *adev,
532 u8 perf_req, bool advertise)
533{
534 acpi_handle handle;
535 union acpi_object *info;
536 struct amdgpu_atcs *atcs = &adev->atcs;
537 struct atcs_pref_req_input atcs_input;
538 struct atcs_pref_req_output atcs_output;
539 struct acpi_buffer params;
540 size_t size;
541 u32 retry = 3;
542
543 /* Get the device handle */
544 handle = ACPI_HANDLE(&adev->pdev->dev);
545 if (!handle)
546 return -EINVAL;
547
548 if (!atcs->functions.pcie_perf_req)
549 return -EINVAL;
550
551 atcs_input.size = sizeof(struct atcs_pref_req_input);
552 /* client id (bit 2-0: func num, 7-3: dev num, 15-8: bus num) */
553 atcs_input.client_id = adev->pdev->devfn | (adev->pdev->bus->number << 8);
554 atcs_input.valid_flags_mask = ATCS_VALID_FLAGS_MASK;
555 atcs_input.flags = ATCS_WAIT_FOR_COMPLETION;
556 if (advertise)
557 atcs_input.flags |= ATCS_ADVERTISE_CAPS;
558 atcs_input.req_type = ATCS_PCIE_LINK_SPEED;
559 atcs_input.perf_req = perf_req;
560
561 params.length = sizeof(struct atcs_pref_req_input);
562 params.pointer = &atcs_input;
563
564 while (retry--) {
565 info = amdgpu_atcs_call(handle, ATCS_FUNCTION_PCIE_PERFORMANCE_REQUEST, ¶ms);
566 if (!info)
567 return -EIO;
568
569 memset(&atcs_output, 0, sizeof(atcs_output));
570
571 size = *(u16 *) info->buffer.pointer;
572 if (size < 3) {
573 DRM_INFO("ATCS buffer is too small: %zu\n", size);
574 kfree(info);
575 return -EINVAL;
576 }
577 size = min(sizeof(atcs_output), size);
578
579 memcpy(&atcs_output, info->buffer.pointer, size);
580
581 kfree(info);
582
583 switch (atcs_output.ret_val) {
584 case ATCS_REQUEST_REFUSED:
585 default:
586 return -EINVAL;
587 case ATCS_REQUEST_COMPLETE:
588 return 0;
589 case ATCS_REQUEST_IN_PROGRESS:
590 udelay(10);
591 break;
592 }
593 }
594
595 return 0;
596}
597
598/**
599 * amdgpu_acpi_event - handle notify events
600 *
601 * @nb: notifier block
602 * @val: val
603 * @data: acpi event
604 *
605 * Calls relevant amdgpu functions in response to various
606 * acpi events.
607 * Returns NOTIFY code
608 */
609static int amdgpu_acpi_event(struct notifier_block *nb,
610 unsigned long val,
611 void *data)
612{
613 struct amdgpu_device *adev = container_of(nb, struct amdgpu_device, acpi_nb);
614 struct acpi_bus_event *entry = (struct acpi_bus_event *)data;
615
616 if (strcmp(entry->device_class, ACPI_AC_CLASS) == 0) {
617 if (power_supply_is_system_supplied() > 0)
618 DRM_DEBUG_DRIVER("pm: AC\n");
619 else
620 DRM_DEBUG_DRIVER("pm: DC\n");
621
622 amdgpu_pm_acpi_event_handler(adev);
623 }
624
625 /* Check for pending SBIOS requests */
626 return amdgpu_atif_handler(adev, entry);
627}
628
629/* Call all ACPI methods here */
630/**
631 * amdgpu_acpi_init - init driver acpi support
632 *
633 * @adev: amdgpu_device pointer
634 *
635 * Verifies the AMD ACPI interfaces and registers with the acpi
636 * notifier chain (all asics).
637 * Returns 0 on success, error on failure.
638 */
639int amdgpu_acpi_init(struct amdgpu_device *adev)
640{
641 acpi_handle handle;
642 struct amdgpu_atif *atif = &adev->atif;
643 struct amdgpu_atcs *atcs = &adev->atcs;
644 int ret;
645
646 /* Get the device handle */
647 handle = ACPI_HANDLE(&adev->pdev->dev);
648
649 if (!adev->bios || !handle)
650 return 0;
651
652 /* Call the ATCS method */
653 ret = amdgpu_atcs_verify_interface(handle, atcs);
654 if (ret) {
655 DRM_DEBUG_DRIVER("Call to ATCS verify_interface failed: %d\n", ret);
656 }
657
658 /* Call the ATIF method */
659 ret = amdgpu_atif_verify_interface(handle, atif);
660 if (ret) {
661 DRM_DEBUG_DRIVER("Call to ATIF verify_interface failed: %d\n", ret);
662 goto out;
663 }
664
665 if (atif->notifications.brightness_change) {
666 struct drm_encoder *tmp;
667
668 /* Find the encoder controlling the brightness */
669 list_for_each_entry(tmp, &adev->ddev->mode_config.encoder_list,
670 head) {
671 struct amdgpu_encoder *enc = to_amdgpu_encoder(tmp);
672
673 if ((enc->devices & (ATOM_DEVICE_LCD_SUPPORT)) &&
674 enc->enc_priv) {
675 if (adev->is_atom_bios) {
676 struct amdgpu_encoder_atom_dig *dig = enc->enc_priv;
677 if (dig->bl_dev) {
678 atif->encoder_for_bl = enc;
679 break;
680 }
681 }
682 }
683 }
684 }
685
686 if (atif->functions.sbios_requests && !atif->functions.system_params) {
687 /* XXX check this workraround, if sbios request function is
688 * present we have to see how it's configured in the system
689 * params
690 */
691 atif->functions.system_params = true;
692 }
693
694 if (atif->functions.system_params) {
695 ret = amdgpu_atif_get_notification_params(handle,
696 &atif->notification_cfg);
697 if (ret) {
698 DRM_DEBUG_DRIVER("Call to GET_SYSTEM_PARAMS failed: %d\n",
699 ret);
700 /* Disable notification */
701 atif->notification_cfg.enabled = false;
702 }
703 }
704
705out:
706 adev->acpi_nb.notifier_call = amdgpu_acpi_event;
707 register_acpi_notifier(&adev->acpi_nb);
708
709 return ret;
710}
711
712/**
713 * amdgpu_acpi_fini - tear down driver acpi support
714 *
715 * @adev: amdgpu_device pointer
716 *
717 * Unregisters with the acpi notifier chain (all asics).
718 */
719void amdgpu_acpi_fini(struct amdgpu_device *adev)
720{
721 unregister_acpi_notifier(&adev->acpi_nb);
722}