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