67675eff1d2a03e90e34cd9cd62dfb94685a3bb9
[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 };
80
81 struct amdgpu_atcs {
82         acpi_handle handle;
83
84         struct amdgpu_atcs_functions functions;
85 };
86
87 /* Call the ATIF method
88  */
89 /**
90  * amdgpu_atif_call - call an ATIF method
91  *
92  * @atif: atif structure
93  * @function: the ATIF function to execute
94  * @params: ATIF function params
95  *
96  * Executes the requested ATIF function (all asics).
97  * Returns a pointer to the acpi output buffer.
98  */
99 static union acpi_object *amdgpu_atif_call(struct amdgpu_atif *atif,
100                                            int function,
101                                            struct acpi_buffer *params)
102 {
103         acpi_status status;
104         union acpi_object atif_arg_elements[2];
105         struct acpi_object_list atif_arg;
106         struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
107
108         atif_arg.count = 2;
109         atif_arg.pointer = &atif_arg_elements[0];
110
111         atif_arg_elements[0].type = ACPI_TYPE_INTEGER;
112         atif_arg_elements[0].integer.value = function;
113
114         if (params) {
115                 atif_arg_elements[1].type = ACPI_TYPE_BUFFER;
116                 atif_arg_elements[1].buffer.length = params->length;
117                 atif_arg_elements[1].buffer.pointer = params->pointer;
118         } else {
119                 /* We need a second fake parameter */
120                 atif_arg_elements[1].type = ACPI_TYPE_INTEGER;
121                 atif_arg_elements[1].integer.value = 0;
122         }
123
124         status = acpi_evaluate_object(atif->handle, NULL, &atif_arg,
125                                       &buffer);
126
127         /* Fail only if calling the method fails and ATIF is supported */
128         if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
129                 DRM_DEBUG_DRIVER("failed to evaluate ATIF got %s\n",
130                                  acpi_format_exception(status));
131                 kfree(buffer.pointer);
132                 return NULL;
133         }
134
135         return buffer.pointer;
136 }
137
138 /**
139  * amdgpu_atif_parse_notification - parse supported notifications
140  *
141  * @n: supported notifications struct
142  * @mask: supported notifications mask from ATIF
143  *
144  * Use the supported notifications mask from ATIF function
145  * ATIF_FUNCTION_VERIFY_INTERFACE to determine what notifications
146  * are supported (all asics).
147  */
148 static void amdgpu_atif_parse_notification(struct amdgpu_atif_notifications *n, u32 mask)
149 {
150         n->thermal_state = mask & ATIF_THERMAL_STATE_CHANGE_REQUEST_SUPPORTED;
151         n->forced_power_state = mask & ATIF_FORCED_POWER_STATE_CHANGE_REQUEST_SUPPORTED;
152         n->system_power_state = mask & ATIF_SYSTEM_POWER_SOURCE_CHANGE_REQUEST_SUPPORTED;
153         n->brightness_change = mask & ATIF_PANEL_BRIGHTNESS_CHANGE_REQUEST_SUPPORTED;
154         n->dgpu_display_event = mask & ATIF_DGPU_DISPLAY_EVENT_SUPPORTED;
155         n->gpu_package_power_limit = mask & ATIF_GPU_PACKAGE_POWER_LIMIT_REQUEST_SUPPORTED;
156 }
157
158 /**
159  * amdgpu_atif_parse_functions - parse supported functions
160  *
161  * @f: supported functions struct
162  * @mask: supported functions mask from ATIF
163  *
164  * Use the supported functions mask from ATIF function
165  * ATIF_FUNCTION_VERIFY_INTERFACE to determine what functions
166  * are supported (all asics).
167  */
168 static void amdgpu_atif_parse_functions(struct amdgpu_atif_functions *f, u32 mask)
169 {
170         f->system_params = mask & ATIF_GET_SYSTEM_PARAMETERS_SUPPORTED;
171         f->sbios_requests = mask & ATIF_GET_SYSTEM_BIOS_REQUESTS_SUPPORTED;
172         f->temperature_change = mask & ATIF_TEMPERATURE_CHANGE_NOTIFICATION_SUPPORTED;
173         f->query_backlight_transfer_characteristics =
174                 mask & ATIF_QUERY_BACKLIGHT_TRANSFER_CHARACTERISTICS_SUPPORTED;
175         f->ready_to_undock = mask & ATIF_READY_TO_UNDOCK_NOTIFICATION_SUPPORTED;
176         f->external_gpu_information = mask & ATIF_GET_EXTERNAL_GPU_INFORMATION_SUPPORTED;
177 }
178
179 /**
180  * amdgpu_atif_verify_interface - verify ATIF
181  *
182  * @atif: amdgpu atif struct
183  *
184  * Execute the ATIF_FUNCTION_VERIFY_INTERFACE ATIF function
185  * to initialize ATIF and determine what features are supported
186  * (all asics).
187  * returns 0 on success, error on failure.
188  */
189 static int amdgpu_atif_verify_interface(struct amdgpu_atif *atif)
190 {
191         union acpi_object *info;
192         struct atif_verify_interface output;
193         size_t size;
194         int err = 0;
195
196         info = amdgpu_atif_call(atif, ATIF_FUNCTION_VERIFY_INTERFACE, NULL);
197         if (!info)
198                 return -EIO;
199
200         memset(&output, 0, sizeof(output));
201
202         size = *(u16 *) info->buffer.pointer;
203         if (size < 12) {
204                 DRM_INFO("ATIF buffer is too small: %zu\n", size);
205                 err = -EINVAL;
206                 goto out;
207         }
208         size = min(sizeof(output), size);
209
210         memcpy(&output, info->buffer.pointer, size);
211
212         /* TODO: check version? */
213         DRM_DEBUG_DRIVER("ATIF version %u\n", output.version);
214
215         amdgpu_atif_parse_notification(&atif->notifications, output.notification_mask);
216         amdgpu_atif_parse_functions(&atif->functions, output.function_bits);
217
218 out:
219         kfree(info);
220         return err;
221 }
222
223 static acpi_handle amdgpu_atif_probe_handle(acpi_handle dhandle)
224 {
225         acpi_handle handle = NULL;
226         char acpi_method_name[255] = { 0 };
227         struct acpi_buffer buffer = { sizeof(acpi_method_name), acpi_method_name };
228         acpi_status status;
229
230         /* For PX/HG systems, ATIF and ATPX are in the iGPU's namespace, on dGPU only
231          * systems, ATIF is in the dGPU's namespace.
232          */
233         status = acpi_get_handle(dhandle, "ATIF", &handle);
234         if (ACPI_SUCCESS(status))
235                 goto out;
236
237         if (amdgpu_has_atpx()) {
238                 status = acpi_get_handle(amdgpu_atpx_get_dhandle(), "ATIF",
239                                          &handle);
240                 if (ACPI_SUCCESS(status))
241                         goto out;
242         }
243
244         DRM_DEBUG_DRIVER("No ATIF handle found\n");
245         return NULL;
246 out:
247         acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
248         DRM_DEBUG_DRIVER("Found ATIF handle %s\n", acpi_method_name);
249         return handle;
250 }
251
252 static acpi_handle amdgpu_atcs_probe_handle(acpi_handle dhandle)
253 {
254         acpi_handle handle = NULL;
255         char acpi_method_name[255] = { 0 };
256         struct acpi_buffer buffer = { sizeof(acpi_method_name), acpi_method_name };
257         acpi_status status;
258
259         /* For PX/HG systems, ATCS and ATPX are in the iGPU's namespace, on dGPU only
260          * systems, ATIF is in the dGPU's namespace.
261          */
262         status = acpi_get_handle(dhandle, "ATCS", &handle);
263         if (ACPI_SUCCESS(status))
264                 goto out;
265
266         if (amdgpu_has_atpx()) {
267                 status = acpi_get_handle(amdgpu_atpx_get_dhandle(), "ATCS",
268                                          &handle);
269                 if (ACPI_SUCCESS(status))
270                         goto out;
271         }
272
273         DRM_DEBUG_DRIVER("No ATCS handle found\n");
274         return NULL;
275 out:
276         acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
277         DRM_DEBUG_DRIVER("Found ATCS handle %s\n", acpi_method_name);
278         return handle;
279 }
280
281 /**
282  * amdgpu_atif_get_notification_params - determine notify configuration
283  *
284  * @atif: acpi handle
285  *
286  * Execute the ATIF_FUNCTION_GET_SYSTEM_PARAMETERS ATIF function
287  * to determine if a notifier is used and if so which one
288  * (all asics).  This is either Notify(VGA, 0x81) or Notify(VGA, n)
289  * where n is specified in the result if a notifier is used.
290  * Returns 0 on success, error on failure.
291  */
292 static int amdgpu_atif_get_notification_params(struct amdgpu_atif *atif)
293 {
294         union acpi_object *info;
295         struct amdgpu_atif_notification_cfg *n = &atif->notification_cfg;
296         struct atif_system_params params;
297         size_t size;
298         int err = 0;
299
300         info = amdgpu_atif_call(atif, ATIF_FUNCTION_GET_SYSTEM_PARAMETERS,
301                                 NULL);
302         if (!info) {
303                 err = -EIO;
304                 goto out;
305         }
306
307         size = *(u16 *) info->buffer.pointer;
308         if (size < 10) {
309                 err = -EINVAL;
310                 goto out;
311         }
312
313         memset(&params, 0, sizeof(params));
314         size = min(sizeof(params), size);
315         memcpy(&params, info->buffer.pointer, size);
316
317         DRM_DEBUG_DRIVER("SYSTEM_PARAMS: mask = %#x, flags = %#x\n",
318                         params.flags, params.valid_mask);
319         params.flags = params.flags & params.valid_mask;
320
321         if ((params.flags & ATIF_NOTIFY_MASK) == ATIF_NOTIFY_NONE) {
322                 n->enabled = false;
323                 n->command_code = 0;
324         } else if ((params.flags & ATIF_NOTIFY_MASK) == ATIF_NOTIFY_81) {
325                 n->enabled = true;
326                 n->command_code = 0x81;
327         } else {
328                 if (size < 11) {
329                         err = -EINVAL;
330                         goto out;
331                 }
332                 n->enabled = true;
333                 n->command_code = params.command_code;
334         }
335
336 out:
337         DRM_DEBUG_DRIVER("Notification %s, command code = %#x\n",
338                         (n->enabled ? "enabled" : "disabled"),
339                         n->command_code);
340         kfree(info);
341         return err;
342 }
343
344 /**
345  * amdgpu_atif_query_backlight_caps - get min and max backlight input signal
346  *
347  * @atif: acpi handle
348  *
349  * Execute the QUERY_BRIGHTNESS_TRANSFER_CHARACTERISTICS ATIF function
350  * to determine the acceptable range of backlight values
351  *
352  * Backlight_caps.caps_valid will be set to true if the query is successful
353  *
354  * The input signals are in range 0-255
355  *
356  * This function assumes the display with backlight is the first LCD
357  *
358  * Returns 0 on success, error on failure.
359  */
360 static int amdgpu_atif_query_backlight_caps(struct amdgpu_atif *atif)
361 {
362         union acpi_object *info;
363         struct atif_qbtc_output characteristics;
364         struct atif_qbtc_arguments arguments;
365         struct acpi_buffer params;
366         size_t size;
367         int err = 0;
368
369         arguments.size = sizeof(arguments);
370         arguments.requested_display = ATIF_QBTC_REQUEST_LCD1;
371
372         params.length = sizeof(arguments);
373         params.pointer = (void *)&arguments;
374
375         info = amdgpu_atif_call(atif,
376                 ATIF_FUNCTION_QUERY_BRIGHTNESS_TRANSFER_CHARACTERISTICS,
377                 &params);
378         if (!info) {
379                 err = -EIO;
380                 goto out;
381         }
382
383         size = *(u16 *) info->buffer.pointer;
384         if (size < 10) {
385                 err = -EINVAL;
386                 goto out;
387         }
388
389         memset(&characteristics, 0, sizeof(characteristics));
390         size = min(sizeof(characteristics), size);
391         memcpy(&characteristics, info->buffer.pointer, size);
392
393         atif->backlight_caps.caps_valid = true;
394         atif->backlight_caps.min_input_signal =
395                         characteristics.min_input_signal;
396         atif->backlight_caps.max_input_signal =
397                         characteristics.max_input_signal;
398 out:
399         kfree(info);
400         return err;
401 }
402
403 /**
404  * amdgpu_atif_get_sbios_requests - get requested sbios event
405  *
406  * @atif: acpi handle
407  * @req: atif sbios request struct
408  *
409  * Execute the ATIF_FUNCTION_GET_SYSTEM_BIOS_REQUESTS ATIF function
410  * to determine what requests the sbios is making to the driver
411  * (all asics).
412  * Returns 0 on success, error on failure.
413  */
414 static int amdgpu_atif_get_sbios_requests(struct amdgpu_atif *atif,
415                                           struct atif_sbios_requests *req)
416 {
417         union acpi_object *info;
418         size_t size;
419         int count = 0;
420
421         info = amdgpu_atif_call(atif, ATIF_FUNCTION_GET_SYSTEM_BIOS_REQUESTS,
422                                 NULL);
423         if (!info)
424                 return -EIO;
425
426         size = *(u16 *)info->buffer.pointer;
427         if (size < 0xd) {
428                 count = -EINVAL;
429                 goto out;
430         }
431         memset(req, 0, sizeof(*req));
432
433         size = min(sizeof(*req), size);
434         memcpy(req, info->buffer.pointer, size);
435         DRM_DEBUG_DRIVER("SBIOS pending requests: %#x\n", req->pending);
436
437         count = hweight32(req->pending);
438
439 out:
440         kfree(info);
441         return count;
442 }
443
444 /**
445  * amdgpu_atif_handler - handle ATIF notify requests
446  *
447  * @adev: amdgpu_device pointer
448  * @event: atif sbios request struct
449  *
450  * Checks the acpi event and if it matches an atif event,
451  * handles it.
452  *
453  * Returns:
454  * NOTIFY_BAD or NOTIFY_DONE, depending on the event.
455  */
456 static int amdgpu_atif_handler(struct amdgpu_device *adev,
457                                struct acpi_bus_event *event)
458 {
459         struct amdgpu_atif *atif = adev->atif;
460         int count;
461
462         DRM_DEBUG_DRIVER("event, device_class = %s, type = %#x\n",
463                         event->device_class, event->type);
464
465         if (strcmp(event->device_class, ACPI_VIDEO_CLASS) != 0)
466                 return NOTIFY_DONE;
467
468         /* Is this actually our event? */
469         if (!atif ||
470             !atif->notification_cfg.enabled ||
471             event->type != atif->notification_cfg.command_code) {
472                 /* These events will generate keypresses otherwise */
473                 if (event->type == ACPI_VIDEO_NOTIFY_PROBE)
474                         return NOTIFY_BAD;
475                 else
476                         return NOTIFY_DONE;
477         }
478
479         if (atif->functions.sbios_requests) {
480                 struct atif_sbios_requests req;
481
482                 /* Check pending SBIOS requests */
483                 count = amdgpu_atif_get_sbios_requests(atif, &req);
484
485                 if (count <= 0)
486                         return NOTIFY_BAD;
487
488                 DRM_DEBUG_DRIVER("ATIF: %d pending SBIOS requests\n", count);
489
490                 if (req.pending & ATIF_PANEL_BRIGHTNESS_CHANGE_REQUEST) {
491 #if defined(CONFIG_BACKLIGHT_CLASS_DEVICE) || defined(CONFIG_BACKLIGHT_CLASS_DEVICE_MODULE)
492                         if (atif->bd) {
493                                 DRM_DEBUG_DRIVER("Changing brightness to %d\n",
494                                                  req.backlight_level);
495                                 /*
496                                  * XXX backlight_device_set_brightness() is
497                                  * hardwired to post BACKLIGHT_UPDATE_SYSFS.
498                                  * It probably should accept 'reason' parameter.
499                                  */
500                                 backlight_device_set_brightness(atif->bd, req.backlight_level);
501                         }
502 #endif
503                 }
504
505                 if (req.pending & ATIF_DGPU_DISPLAY_EVENT) {
506                         if (adev->flags & AMD_IS_PX) {
507                                 pm_runtime_get_sync(adev_to_drm(adev)->dev);
508                                 /* Just fire off a uevent and let userspace tell us what to do */
509                                 drm_helper_hpd_irq_event(adev_to_drm(adev));
510                                 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
511                                 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
512                         }
513                 }
514                 /* TODO: check other events */
515         }
516
517         /* We've handled the event, stop the notifier chain. The ACPI interface
518          * overloads ACPI_VIDEO_NOTIFY_PROBE, we don't want to send that to
519          * userspace if the event was generated only to signal a SBIOS
520          * request.
521          */
522         return NOTIFY_BAD;
523 }
524
525 /* Call the ATCS method
526  */
527 /**
528  * amdgpu_atcs_call - call an ATCS method
529  *
530  * @atcs: atcs structure
531  * @function: the ATCS function to execute
532  * @params: ATCS function params
533  *
534  * Executes the requested ATCS function (all asics).
535  * Returns a pointer to the acpi output buffer.
536  */
537 static union acpi_object *amdgpu_atcs_call(struct amdgpu_atcs *atcs,
538                                            int function,
539                                            struct acpi_buffer *params)
540 {
541         acpi_status status;
542         union acpi_object atcs_arg_elements[2];
543         struct acpi_object_list atcs_arg;
544         struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
545
546         atcs_arg.count = 2;
547         atcs_arg.pointer = &atcs_arg_elements[0];
548
549         atcs_arg_elements[0].type = ACPI_TYPE_INTEGER;
550         atcs_arg_elements[0].integer.value = function;
551
552         if (params) {
553                 atcs_arg_elements[1].type = ACPI_TYPE_BUFFER;
554                 atcs_arg_elements[1].buffer.length = params->length;
555                 atcs_arg_elements[1].buffer.pointer = params->pointer;
556         } else {
557                 /* We need a second fake parameter */
558                 atcs_arg_elements[1].type = ACPI_TYPE_INTEGER;
559                 atcs_arg_elements[1].integer.value = 0;
560         }
561
562         status = acpi_evaluate_object(atcs->handle, "ATCS", &atcs_arg, &buffer);
563
564         /* Fail only if calling the method fails and ATIF is supported */
565         if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
566                 DRM_DEBUG_DRIVER("failed to evaluate ATCS got %s\n",
567                                  acpi_format_exception(status));
568                 kfree(buffer.pointer);
569                 return NULL;
570         }
571
572         return buffer.pointer;
573 }
574
575 /**
576  * amdgpu_atcs_parse_functions - parse supported functions
577  *
578  * @f: supported functions struct
579  * @mask: supported functions mask from ATCS
580  *
581  * Use the supported functions mask from ATCS function
582  * ATCS_FUNCTION_VERIFY_INTERFACE to determine what functions
583  * are supported (all asics).
584  */
585 static void amdgpu_atcs_parse_functions(struct amdgpu_atcs_functions *f, u32 mask)
586 {
587         f->get_ext_state = mask & ATCS_GET_EXTERNAL_STATE_SUPPORTED;
588         f->pcie_perf_req = mask & ATCS_PCIE_PERFORMANCE_REQUEST_SUPPORTED;
589         f->pcie_dev_rdy = mask & ATCS_PCIE_DEVICE_READY_NOTIFICATION_SUPPORTED;
590         f->pcie_bus_width = mask & ATCS_SET_PCIE_BUS_WIDTH_SUPPORTED;
591 }
592
593 /**
594  * amdgpu_atcs_verify_interface - verify ATCS
595  *
596  * @atcs: amdgpu atcs struct
597  *
598  * Execute the ATCS_FUNCTION_VERIFY_INTERFACE ATCS function
599  * to initialize ATCS and determine what features are supported
600  * (all asics).
601  * returns 0 on success, error on failure.
602  */
603 static int amdgpu_atcs_verify_interface(struct amdgpu_atcs *atcs)
604 {
605         union acpi_object *info;
606         struct atcs_verify_interface output;
607         size_t size;
608         int err = 0;
609
610         info = amdgpu_atcs_call(atcs, ATCS_FUNCTION_VERIFY_INTERFACE, NULL);
611         if (!info)
612                 return -EIO;
613
614         memset(&output, 0, sizeof(output));
615
616         size = *(u16 *) info->buffer.pointer;
617         if (size < 8) {
618                 DRM_INFO("ATCS buffer is too small: %zu\n", size);
619                 err = -EINVAL;
620                 goto out;
621         }
622         size = min(sizeof(output), size);
623
624         memcpy(&output, info->buffer.pointer, size);
625
626         /* TODO: check version? */
627         DRM_DEBUG_DRIVER("ATCS version %u\n", output.version);
628
629         amdgpu_atcs_parse_functions(&atcs->functions, output.function_bits);
630
631 out:
632         kfree(info);
633         return err;
634 }
635
636 /**
637  * amdgpu_acpi_is_pcie_performance_request_supported
638  *
639  * @adev: amdgpu_device pointer
640  *
641  * Check if the ATCS pcie_perf_req and pcie_dev_rdy methods
642  * are supported (all asics).
643  * returns true if supported, false if not.
644  */
645 bool amdgpu_acpi_is_pcie_performance_request_supported(struct amdgpu_device *adev)
646 {
647         struct amdgpu_atcs *atcs = adev->atcs;
648
649         if (!atcs)
650                 return false;
651         if (atcs->functions.pcie_perf_req && atcs->functions.pcie_dev_rdy)
652                 return true;
653
654         return false;
655 }
656
657 /**
658  * amdgpu_acpi_pcie_notify_device_ready
659  *
660  * @adev: amdgpu_device pointer
661  *
662  * Executes the PCIE_DEVICE_READY_NOTIFICATION method
663  * (all asics).
664  * returns 0 on success, error on failure.
665  */
666 int amdgpu_acpi_pcie_notify_device_ready(struct amdgpu_device *adev)
667 {
668         union acpi_object *info;
669         struct amdgpu_atcs *atcs = adev->atcs;
670
671         if (!atcs)
672                 return -EINVAL;
673         if (!atcs->functions.pcie_dev_rdy)
674                 return -EINVAL;
675
676         info = amdgpu_atcs_call(atcs, ATCS_FUNCTION_PCIE_DEVICE_READY_NOTIFICATION, NULL);
677         if (!info)
678                 return -EIO;
679
680         kfree(info);
681
682         return 0;
683 }
684
685 /**
686  * amdgpu_acpi_pcie_performance_request
687  *
688  * @adev: amdgpu_device pointer
689  * @perf_req: requested perf level (pcie gen speed)
690  * @advertise: set advertise caps flag if set
691  *
692  * Executes the PCIE_PERFORMANCE_REQUEST method to
693  * change the pcie gen speed (all asics).
694  * returns 0 on success, error on failure.
695  */
696 int amdgpu_acpi_pcie_performance_request(struct amdgpu_device *adev,
697                                          u8 perf_req, bool advertise)
698 {
699         union acpi_object *info;
700         struct amdgpu_atcs *atcs = adev->atcs;
701         struct atcs_pref_req_input atcs_input;
702         struct atcs_pref_req_output atcs_output;
703         struct acpi_buffer params;
704         size_t size;
705         u32 retry = 3;
706
707         if (!atcs)
708                 return -EINVAL;
709
710         if (amdgpu_acpi_pcie_notify_device_ready(adev))
711                 return -EINVAL;
712
713         if (!atcs->functions.pcie_perf_req)
714                 return -EINVAL;
715
716         atcs_input.size = sizeof(struct atcs_pref_req_input);
717         /* client id (bit 2-0: func num, 7-3: dev num, 15-8: bus num) */
718         atcs_input.client_id = adev->pdev->devfn | (adev->pdev->bus->number << 8);
719         atcs_input.valid_flags_mask = ATCS_VALID_FLAGS_MASK;
720         atcs_input.flags = ATCS_WAIT_FOR_COMPLETION;
721         if (advertise)
722                 atcs_input.flags |= ATCS_ADVERTISE_CAPS;
723         atcs_input.req_type = ATCS_PCIE_LINK_SPEED;
724         atcs_input.perf_req = perf_req;
725
726         params.length = sizeof(struct atcs_pref_req_input);
727         params.pointer = &atcs_input;
728
729         while (retry--) {
730                 info = amdgpu_atcs_call(atcs, ATCS_FUNCTION_PCIE_PERFORMANCE_REQUEST, &params);
731                 if (!info)
732                         return -EIO;
733
734                 memset(&atcs_output, 0, sizeof(atcs_output));
735
736                 size = *(u16 *) info->buffer.pointer;
737                 if (size < 3) {
738                         DRM_INFO("ATCS buffer is too small: %zu\n", size);
739                         kfree(info);
740                         return -EINVAL;
741                 }
742                 size = min(sizeof(atcs_output), size);
743
744                 memcpy(&atcs_output, info->buffer.pointer, size);
745
746                 kfree(info);
747
748                 switch (atcs_output.ret_val) {
749                 case ATCS_REQUEST_REFUSED:
750                 default:
751                         return -EINVAL;
752                 case ATCS_REQUEST_COMPLETE:
753                         return 0;
754                 case ATCS_REQUEST_IN_PROGRESS:
755                         udelay(10);
756                         break;
757                 }
758         }
759
760         return 0;
761 }
762
763 /**
764  * amdgpu_acpi_event - handle notify events
765  *
766  * @nb: notifier block
767  * @val: val
768  * @data: acpi event
769  *
770  * Calls relevant amdgpu functions in response to various
771  * acpi events.
772  * Returns NOTIFY code
773  */
774 static int amdgpu_acpi_event(struct notifier_block *nb,
775                              unsigned long val,
776                              void *data)
777 {
778         struct amdgpu_device *adev = container_of(nb, struct amdgpu_device, acpi_nb);
779         struct acpi_bus_event *entry = (struct acpi_bus_event *)data;
780
781         if (strcmp(entry->device_class, ACPI_AC_CLASS) == 0) {
782                 if (power_supply_is_system_supplied() > 0)
783                         DRM_DEBUG_DRIVER("pm: AC\n");
784                 else
785                         DRM_DEBUG_DRIVER("pm: DC\n");
786
787                 amdgpu_pm_acpi_event_handler(adev);
788         }
789
790         /* Check for pending SBIOS requests */
791         return amdgpu_atif_handler(adev, entry);
792 }
793
794 /* Call all ACPI methods here */
795 /**
796  * amdgpu_acpi_init - init driver acpi support
797  *
798  * @adev: amdgpu_device pointer
799  *
800  * Verifies the AMD ACPI interfaces and registers with the acpi
801  * notifier chain (all asics).
802  * Returns 0 on success, error on failure.
803  */
804 int amdgpu_acpi_init(struct amdgpu_device *adev)
805 {
806         acpi_handle handle, atif_handle, atcs_handle;
807         struct amdgpu_atif *atif;
808         struct amdgpu_atcs *atcs;
809         int ret = 0;
810
811         /* Get the device handle */
812         handle = ACPI_HANDLE(&adev->pdev->dev);
813
814         if (!adev->bios || !handle)
815                 return ret;
816
817         /* Probe for ATIF, and initialize it if found */
818         atif_handle = amdgpu_atif_probe_handle(handle);
819         if (!atif_handle)
820                 goto atcs;
821
822         atif = kzalloc(sizeof(*atif), GFP_KERNEL);
823         if (!atif) {
824                 DRM_WARN("Not enough memory to initialize ATIF\n");
825                 goto atcs;
826         }
827         atif->handle = atif_handle;
828
829         /* Call the ATIF method */
830         ret = amdgpu_atif_verify_interface(atif);
831         if (ret) {
832                 DRM_DEBUG_DRIVER("Call to ATIF verify_interface failed: %d\n", ret);
833                 kfree(atif);
834                 goto atcs;
835         }
836         adev->atif = atif;
837
838 #if defined(CONFIG_BACKLIGHT_CLASS_DEVICE) || defined(CONFIG_BACKLIGHT_CLASS_DEVICE_MODULE)
839         if (atif->notifications.brightness_change) {
840                 if (amdgpu_device_has_dc_support(adev)) {
841 #if defined(CONFIG_DRM_AMD_DC)
842                         struct amdgpu_display_manager *dm = &adev->dm;
843                         if (dm->backlight_dev)
844                                 atif->bd = dm->backlight_dev;
845 #endif
846                 } else {
847                         struct drm_encoder *tmp;
848
849                         /* Find the encoder controlling the brightness */
850                         list_for_each_entry(tmp, &adev_to_drm(adev)->mode_config.encoder_list,
851                                             head) {
852                                 struct amdgpu_encoder *enc = to_amdgpu_encoder(tmp);
853
854                                 if ((enc->devices & (ATOM_DEVICE_LCD_SUPPORT)) &&
855                                     enc->enc_priv) {
856                                         struct amdgpu_encoder_atom_dig *dig = enc->enc_priv;
857                                         if (dig->bl_dev) {
858                                                 atif->bd = dig->bl_dev;
859                                                 break;
860                                         }
861                                 }
862                         }
863                 }
864         }
865 #endif
866
867         if (atif->functions.sbios_requests && !atif->functions.system_params) {
868                 /* XXX check this workraround, if sbios request function is
869                  * present we have to see how it's configured in the system
870                  * params
871                  */
872                 atif->functions.system_params = true;
873         }
874
875         if (atif->functions.system_params) {
876                 ret = amdgpu_atif_get_notification_params(atif);
877                 if (ret) {
878                         DRM_DEBUG_DRIVER("Call to GET_SYSTEM_PARAMS failed: %d\n",
879                                         ret);
880                         /* Disable notification */
881                         atif->notification_cfg.enabled = false;
882                 }
883         }
884
885         if (atif->functions.query_backlight_transfer_characteristics) {
886                 ret = amdgpu_atif_query_backlight_caps(atif);
887                 if (ret) {
888                         DRM_DEBUG_DRIVER("Call to QUERY_BACKLIGHT_TRANSFER_CHARACTERISTICS failed: %d\n",
889                                         ret);
890                         atif->backlight_caps.caps_valid = false;
891                 }
892         } else {
893                 atif->backlight_caps.caps_valid = false;
894         }
895
896 atcs:
897         /* Probe for ATCS, and initialize it if found */
898         atcs_handle = amdgpu_atcs_probe_handle(handle);
899         if (!atcs_handle)
900                 goto out;
901
902         atcs = kzalloc(sizeof(*atcs), GFP_KERNEL);
903         if (!atcs) {
904                 DRM_WARN("Not enough memory to initialize ATCS\n");
905                 goto out;
906         }
907         atcs->handle = atcs_handle;
908
909         /* Call the ATCS method */
910         ret = amdgpu_atcs_verify_interface(atcs);
911         if (ret) {
912                 DRM_DEBUG_DRIVER("Call to ATCS verify_interface failed: %d\n", ret);
913                 kfree(atcs);
914                 goto out;
915         }
916         adev->atcs = atcs;
917
918 out:
919         adev->acpi_nb.notifier_call = amdgpu_acpi_event;
920         register_acpi_notifier(&adev->acpi_nb);
921
922         return ret;
923 }
924
925 void amdgpu_acpi_get_backlight_caps(struct amdgpu_device *adev,
926                 struct amdgpu_dm_backlight_caps *caps)
927 {
928         if (!adev->atif) {
929                 caps->caps_valid = false;
930                 return;
931         }
932         caps->caps_valid = adev->atif->backlight_caps.caps_valid;
933         caps->min_input_signal = adev->atif->backlight_caps.min_input_signal;
934         caps->max_input_signal = adev->atif->backlight_caps.max_input_signal;
935 }
936
937 /**
938  * amdgpu_acpi_fini - tear down driver acpi support
939  *
940  * @adev: amdgpu_device pointer
941  *
942  * Unregisters with the acpi notifier chain (all asics).
943  */
944 void amdgpu_acpi_fini(struct amdgpu_device *adev)
945 {
946         unregister_acpi_notifier(&adev->acpi_nb);
947         kfree(adev->atif);
948         kfree(adev->atcs);
949 }
950
951 /**
952  * amdgpu_acpi_is_s0ix_supported
953  *
954  * @adev: amdgpu_device_pointer
955  *
956  * returns true if supported, false if not.
957  */
958 bool amdgpu_acpi_is_s0ix_supported(struct amdgpu_device *adev)
959 {
960 #if defined(CONFIG_AMD_PMC) || defined(CONFIG_AMD_PMC_MODULE)
961         if (acpi_gbl_FADT.flags & ACPI_FADT_LOW_POWER_S0) {
962                 if (adev->flags & AMD_IS_APU)
963                         return true;
964         }
965 #endif
966         return false;
967 }