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