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