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