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