Merge branch 'for-4.20-fixes' into for-4.21
[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 <drm/drmP.h>
31 #include <drm/drm_crtc_helper.h>
32 #include "amdgpu.h"
33 #include "amdgpu_pm.h"
34 #include "amdgpu_display.h"
35 #include "amd_acpi.h"
36 #include "atom.h"
37
38 struct amdgpu_atif_notification_cfg {
39         bool enabled;
40         int command_code;
41 };
42
43 struct amdgpu_atif_notifications {
44         bool display_switch;
45         bool expansion_mode_change;
46         bool thermal_state;
47         bool forced_power_state;
48         bool system_power_state;
49         bool display_conf_change;
50         bool px_gfx_switch;
51         bool brightness_change;
52         bool dgpu_display_event;
53 };
54
55 struct amdgpu_atif_functions {
56         bool system_params;
57         bool sbios_requests;
58         bool select_active_disp;
59         bool lid_state;
60         bool get_tv_standard;
61         bool set_tv_standard;
62         bool get_panel_expansion_mode;
63         bool set_panel_expansion_mode;
64         bool temperature_change;
65         bool graphics_device_types;
66 };
67
68 struct amdgpu_atif {
69         acpi_handle handle;
70
71         struct amdgpu_atif_notifications notifications;
72         struct amdgpu_atif_functions functions;
73         struct amdgpu_atif_notification_cfg notification_cfg;
74         struct amdgpu_encoder *encoder_for_bl;
75 };
76
77 /* Call the ATIF method
78  */
79 /**
80  * amdgpu_atif_call - call an ATIF method
81  *
82  * @handle: acpi handle
83  * @function: the ATIF function to execute
84  * @params: ATIF function params
85  *
86  * Executes the requested ATIF function (all asics).
87  * Returns a pointer to the acpi output buffer.
88  */
89 static union acpi_object *amdgpu_atif_call(struct amdgpu_atif *atif,
90                                            int function,
91                                            struct acpi_buffer *params)
92 {
93         acpi_status status;
94         union acpi_object atif_arg_elements[2];
95         struct acpi_object_list atif_arg;
96         struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
97
98         atif_arg.count = 2;
99         atif_arg.pointer = &atif_arg_elements[0];
100
101         atif_arg_elements[0].type = ACPI_TYPE_INTEGER;
102         atif_arg_elements[0].integer.value = function;
103
104         if (params) {
105                 atif_arg_elements[1].type = ACPI_TYPE_BUFFER;
106                 atif_arg_elements[1].buffer.length = params->length;
107                 atif_arg_elements[1].buffer.pointer = params->pointer;
108         } else {
109                 /* We need a second fake parameter */
110                 atif_arg_elements[1].type = ACPI_TYPE_INTEGER;
111                 atif_arg_elements[1].integer.value = 0;
112         }
113
114         status = acpi_evaluate_object(atif->handle, NULL, &atif_arg,
115                                       &buffer);
116
117         /* Fail only if calling the method fails and ATIF is supported */
118         if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
119                 DRM_DEBUG_DRIVER("failed to evaluate ATIF got %s\n",
120                                  acpi_format_exception(status));
121                 kfree(buffer.pointer);
122                 return NULL;
123         }
124
125         return buffer.pointer;
126 }
127
128 /**
129  * amdgpu_atif_parse_notification - parse supported notifications
130  *
131  * @n: supported notifications struct
132  * @mask: supported notifications mask from ATIF
133  *
134  * Use the supported notifications mask from ATIF function
135  * ATIF_FUNCTION_VERIFY_INTERFACE to determine what notifications
136  * are supported (all asics).
137  */
138 static void amdgpu_atif_parse_notification(struct amdgpu_atif_notifications *n, u32 mask)
139 {
140         n->display_switch = mask & ATIF_DISPLAY_SWITCH_REQUEST_SUPPORTED;
141         n->expansion_mode_change = mask & ATIF_EXPANSION_MODE_CHANGE_REQUEST_SUPPORTED;
142         n->thermal_state = mask & ATIF_THERMAL_STATE_CHANGE_REQUEST_SUPPORTED;
143         n->forced_power_state = mask & ATIF_FORCED_POWER_STATE_CHANGE_REQUEST_SUPPORTED;
144         n->system_power_state = mask & ATIF_SYSTEM_POWER_SOURCE_CHANGE_REQUEST_SUPPORTED;
145         n->display_conf_change = mask & ATIF_DISPLAY_CONF_CHANGE_REQUEST_SUPPORTED;
146         n->px_gfx_switch = mask & ATIF_PX_GFX_SWITCH_REQUEST_SUPPORTED;
147         n->brightness_change = mask & ATIF_PANEL_BRIGHTNESS_CHANGE_REQUEST_SUPPORTED;
148         n->dgpu_display_event = mask & ATIF_DGPU_DISPLAY_EVENT_SUPPORTED;
149 }
150
151 /**
152  * amdgpu_atif_parse_functions - parse supported functions
153  *
154  * @f: supported functions struct
155  * @mask: supported functions mask from ATIF
156  *
157  * Use the supported functions mask from ATIF function
158  * ATIF_FUNCTION_VERIFY_INTERFACE to determine what functions
159  * are supported (all asics).
160  */
161 static void amdgpu_atif_parse_functions(struct amdgpu_atif_functions *f, u32 mask)
162 {
163         f->system_params = mask & ATIF_GET_SYSTEM_PARAMETERS_SUPPORTED;
164         f->sbios_requests = mask & ATIF_GET_SYSTEM_BIOS_REQUESTS_SUPPORTED;
165         f->select_active_disp = mask & ATIF_SELECT_ACTIVE_DISPLAYS_SUPPORTED;
166         f->lid_state = mask & ATIF_GET_LID_STATE_SUPPORTED;
167         f->get_tv_standard = mask & ATIF_GET_TV_STANDARD_FROM_CMOS_SUPPORTED;
168         f->set_tv_standard = mask & ATIF_SET_TV_STANDARD_IN_CMOS_SUPPORTED;
169         f->get_panel_expansion_mode = mask & ATIF_GET_PANEL_EXPANSION_MODE_FROM_CMOS_SUPPORTED;
170         f->set_panel_expansion_mode = mask & ATIF_SET_PANEL_EXPANSION_MODE_IN_CMOS_SUPPORTED;
171         f->temperature_change = mask & ATIF_TEMPERATURE_CHANGE_NOTIFICATION_SUPPORTED;
172         f->graphics_device_types = mask & ATIF_GET_GRAPHICS_DEVICE_TYPES_SUPPORTED;
173 }
174
175 /**
176  * amdgpu_atif_verify_interface - verify ATIF
177  *
178  * @handle: acpi handle
179  * @atif: amdgpu atif struct
180  *
181  * Execute the ATIF_FUNCTION_VERIFY_INTERFACE ATIF function
182  * to initialize ATIF and determine what features are supported
183  * (all asics).
184  * returns 0 on success, error on failure.
185  */
186 static int amdgpu_atif_verify_interface(struct amdgpu_atif *atif)
187 {
188         union acpi_object *info;
189         struct atif_verify_interface output;
190         size_t size;
191         int err = 0;
192
193         info = amdgpu_atif_call(atif, ATIF_FUNCTION_VERIFY_INTERFACE, NULL);
194         if (!info)
195                 return -EIO;
196
197         memset(&output, 0, sizeof(output));
198
199         size = *(u16 *) info->buffer.pointer;
200         if (size < 12) {
201                 DRM_INFO("ATIF buffer is too small: %zu\n", size);
202                 err = -EINVAL;
203                 goto out;
204         }
205         size = min(sizeof(output), size);
206
207         memcpy(&output, info->buffer.pointer, size);
208
209         /* TODO: check version? */
210         DRM_DEBUG_DRIVER("ATIF version %u\n", output.version);
211
212         amdgpu_atif_parse_notification(&atif->notifications, output.notification_mask);
213         amdgpu_atif_parse_functions(&atif->functions, output.function_bits);
214
215 out:
216         kfree(info);
217         return err;
218 }
219
220 static acpi_handle amdgpu_atif_probe_handle(acpi_handle dhandle)
221 {
222         acpi_handle handle = NULL;
223         char acpi_method_name[255] = { 0 };
224         struct acpi_buffer buffer = { sizeof(acpi_method_name), acpi_method_name };
225         acpi_status status;
226
227         /* For PX/HG systems, ATIF and ATPX are in the iGPU's namespace, on dGPU only
228          * systems, ATIF is in the dGPU's namespace.
229          */
230         status = acpi_get_handle(dhandle, "ATIF", &handle);
231         if (ACPI_SUCCESS(status))
232                 goto out;
233
234         if (amdgpu_has_atpx()) {
235                 status = acpi_get_handle(amdgpu_atpx_get_dhandle(), "ATIF",
236                                          &handle);
237                 if (ACPI_SUCCESS(status))
238                         goto out;
239         }
240
241         DRM_DEBUG_DRIVER("No ATIF handle found\n");
242         return NULL;
243 out:
244         acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
245         DRM_DEBUG_DRIVER("Found ATIF handle %s\n", acpi_method_name);
246         return handle;
247 }
248
249 /**
250  * amdgpu_atif_get_notification_params - determine notify configuration
251  *
252  * @handle: acpi handle
253  * @n: atif notification configuration struct
254  *
255  * Execute the ATIF_FUNCTION_GET_SYSTEM_PARAMETERS ATIF function
256  * to determine if a notifier is used and if so which one
257  * (all asics).  This is either Notify(VGA, 0x81) or Notify(VGA, n)
258  * where n is specified in the result if a notifier is used.
259  * Returns 0 on success, error on failure.
260  */
261 static int amdgpu_atif_get_notification_params(struct amdgpu_atif *atif)
262 {
263         union acpi_object *info;
264         struct amdgpu_atif_notification_cfg *n = &atif->notification_cfg;
265         struct atif_system_params params;
266         size_t size;
267         int err = 0;
268
269         info = amdgpu_atif_call(atif, ATIF_FUNCTION_GET_SYSTEM_PARAMETERS,
270                                 NULL);
271         if (!info) {
272                 err = -EIO;
273                 goto out;
274         }
275
276         size = *(u16 *) info->buffer.pointer;
277         if (size < 10) {
278                 err = -EINVAL;
279                 goto out;
280         }
281
282         memset(&params, 0, sizeof(params));
283         size = min(sizeof(params), size);
284         memcpy(&params, info->buffer.pointer, size);
285
286         DRM_DEBUG_DRIVER("SYSTEM_PARAMS: mask = %#x, flags = %#x\n",
287                         params.flags, params.valid_mask);
288         params.flags = params.flags & params.valid_mask;
289
290         if ((params.flags & ATIF_NOTIFY_MASK) == ATIF_NOTIFY_NONE) {
291                 n->enabled = false;
292                 n->command_code = 0;
293         } else if ((params.flags & ATIF_NOTIFY_MASK) == ATIF_NOTIFY_81) {
294                 n->enabled = true;
295                 n->command_code = 0x81;
296         } else {
297                 if (size < 11) {
298                         err = -EINVAL;
299                         goto out;
300                 }
301                 n->enabled = true;
302                 n->command_code = params.command_code;
303         }
304
305 out:
306         DRM_DEBUG_DRIVER("Notification %s, command code = %#x\n",
307                         (n->enabled ? "enabled" : "disabled"),
308                         n->command_code);
309         kfree(info);
310         return err;
311 }
312
313 /**
314  * amdgpu_atif_get_sbios_requests - get requested sbios event
315  *
316  * @handle: acpi handle
317  * @req: atif sbios request struct
318  *
319  * Execute the ATIF_FUNCTION_GET_SYSTEM_BIOS_REQUESTS ATIF function
320  * to determine what requests the sbios is making to the driver
321  * (all asics).
322  * Returns 0 on success, error on failure.
323  */
324 static int amdgpu_atif_get_sbios_requests(struct amdgpu_atif *atif,
325                                           struct atif_sbios_requests *req)
326 {
327         union acpi_object *info;
328         size_t size;
329         int count = 0;
330
331         info = amdgpu_atif_call(atif, ATIF_FUNCTION_GET_SYSTEM_BIOS_REQUESTS,
332                                 NULL);
333         if (!info)
334                 return -EIO;
335
336         size = *(u16 *)info->buffer.pointer;
337         if (size < 0xd) {
338                 count = -EINVAL;
339                 goto out;
340         }
341         memset(req, 0, sizeof(*req));
342
343         size = min(sizeof(*req), size);
344         memcpy(req, info->buffer.pointer, size);
345         DRM_DEBUG_DRIVER("SBIOS pending requests: %#x\n", req->pending);
346
347         count = hweight32(req->pending);
348
349 out:
350         kfree(info);
351         return count;
352 }
353
354 /**
355  * amdgpu_atif_handler - handle ATIF notify requests
356  *
357  * @adev: amdgpu_device pointer
358  * @event: atif sbios request struct
359  *
360  * Checks the acpi event and if it matches an atif event,
361  * handles it.
362  *
363  * Returns:
364  * NOTIFY_BAD or NOTIFY_DONE, depending on the event.
365  */
366 static int amdgpu_atif_handler(struct amdgpu_device *adev,
367                                struct acpi_bus_event *event)
368 {
369         struct amdgpu_atif *atif = adev->atif;
370         int count;
371
372         DRM_DEBUG_DRIVER("event, device_class = %s, type = %#x\n",
373                         event->device_class, event->type);
374
375         if (strcmp(event->device_class, ACPI_VIDEO_CLASS) != 0)
376                 return NOTIFY_DONE;
377
378         /* Is this actually our event? */
379         if (!atif ||
380             !atif->notification_cfg.enabled ||
381             event->type != atif->notification_cfg.command_code) {
382                 /* These events will generate keypresses otherwise */
383                 if (event->type == ACPI_VIDEO_NOTIFY_PROBE)
384                         return NOTIFY_BAD;
385                 else
386                         return NOTIFY_DONE;
387         }
388
389         if (atif->functions.sbios_requests) {
390                 struct atif_sbios_requests req;
391
392                 /* Check pending SBIOS requests */
393                 count = amdgpu_atif_get_sbios_requests(atif, &req);
394
395                 if (count <= 0)
396                         return NOTIFY_BAD;
397
398                 DRM_DEBUG_DRIVER("ATIF: %d pending SBIOS requests\n", count);
399
400                 /* todo: add DC handling */
401                 if ((req.pending & ATIF_PANEL_BRIGHTNESS_CHANGE_REQUEST) &&
402                     !amdgpu_device_has_dc_support(adev)) {
403                         struct amdgpu_encoder *enc = atif->encoder_for_bl;
404
405                         if (enc) {
406                                 struct amdgpu_encoder_atom_dig *dig = enc->enc_priv;
407
408                                 DRM_DEBUG_DRIVER("Changing brightness to %d\n",
409                                                  req.backlight_level);
410
411                                 amdgpu_display_backlight_set_level(adev, enc, req.backlight_level);
412
413 #if defined(CONFIG_BACKLIGHT_CLASS_DEVICE) || defined(CONFIG_BACKLIGHT_CLASS_DEVICE_MODULE)
414                                 backlight_force_update(dig->bl_dev,
415                                                        BACKLIGHT_UPDATE_HOTKEY);
416 #endif
417                         }
418                 }
419                 if (req.pending & ATIF_DGPU_DISPLAY_EVENT) {
420                         if ((adev->flags & AMD_IS_PX) &&
421                             amdgpu_atpx_dgpu_req_power_for_displays()) {
422                                 pm_runtime_get_sync(adev->ddev->dev);
423                                 /* Just fire off a uevent and let userspace tell us what to do */
424                                 drm_helper_hpd_irq_event(adev->ddev);
425                                 pm_runtime_mark_last_busy(adev->ddev->dev);
426                                 pm_runtime_put_autosuspend(adev->ddev->dev);
427                         }
428                 }
429                 /* TODO: check other events */
430         }
431
432         /* We've handled the event, stop the notifier chain. The ACPI interface
433          * overloads ACPI_VIDEO_NOTIFY_PROBE, we don't want to send that to
434          * userspace if the event was generated only to signal a SBIOS
435          * request.
436          */
437         return NOTIFY_BAD;
438 }
439
440 /* Call the ATCS method
441  */
442 /**
443  * amdgpu_atcs_call - call an ATCS method
444  *
445  * @handle: acpi handle
446  * @function: the ATCS function to execute
447  * @params: ATCS function params
448  *
449  * Executes the requested ATCS function (all asics).
450  * Returns a pointer to the acpi output buffer.
451  */
452 static union acpi_object *amdgpu_atcs_call(acpi_handle handle, int function,
453                                            struct acpi_buffer *params)
454 {
455         acpi_status status;
456         union acpi_object atcs_arg_elements[2];
457         struct acpi_object_list atcs_arg;
458         struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
459
460         atcs_arg.count = 2;
461         atcs_arg.pointer = &atcs_arg_elements[0];
462
463         atcs_arg_elements[0].type = ACPI_TYPE_INTEGER;
464         atcs_arg_elements[0].integer.value = function;
465
466         if (params) {
467                 atcs_arg_elements[1].type = ACPI_TYPE_BUFFER;
468                 atcs_arg_elements[1].buffer.length = params->length;
469                 atcs_arg_elements[1].buffer.pointer = params->pointer;
470         } else {
471                 /* We need a second fake parameter */
472                 atcs_arg_elements[1].type = ACPI_TYPE_INTEGER;
473                 atcs_arg_elements[1].integer.value = 0;
474         }
475
476         status = acpi_evaluate_object(handle, "ATCS", &atcs_arg, &buffer);
477
478         /* Fail only if calling the method fails and ATIF is supported */
479         if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
480                 DRM_DEBUG_DRIVER("failed to evaluate ATCS got %s\n",
481                                  acpi_format_exception(status));
482                 kfree(buffer.pointer);
483                 return NULL;
484         }
485
486         return buffer.pointer;
487 }
488
489 /**
490  * amdgpu_atcs_parse_functions - parse supported functions
491  *
492  * @f: supported functions struct
493  * @mask: supported functions mask from ATCS
494  *
495  * Use the supported functions mask from ATCS function
496  * ATCS_FUNCTION_VERIFY_INTERFACE to determine what functions
497  * are supported (all asics).
498  */
499 static void amdgpu_atcs_parse_functions(struct amdgpu_atcs_functions *f, u32 mask)
500 {
501         f->get_ext_state = mask & ATCS_GET_EXTERNAL_STATE_SUPPORTED;
502         f->pcie_perf_req = mask & ATCS_PCIE_PERFORMANCE_REQUEST_SUPPORTED;
503         f->pcie_dev_rdy = mask & ATCS_PCIE_DEVICE_READY_NOTIFICATION_SUPPORTED;
504         f->pcie_bus_width = mask & ATCS_SET_PCIE_BUS_WIDTH_SUPPORTED;
505 }
506
507 /**
508  * amdgpu_atcs_verify_interface - verify ATCS
509  *
510  * @handle: acpi handle
511  * @atcs: amdgpu atcs struct
512  *
513  * Execute the ATCS_FUNCTION_VERIFY_INTERFACE ATCS function
514  * to initialize ATCS and determine what features are supported
515  * (all asics).
516  * returns 0 on success, error on failure.
517  */
518 static int amdgpu_atcs_verify_interface(acpi_handle handle,
519                                         struct amdgpu_atcs *atcs)
520 {
521         union acpi_object *info;
522         struct atcs_verify_interface output;
523         size_t size;
524         int err = 0;
525
526         info = amdgpu_atcs_call(handle, ATCS_FUNCTION_VERIFY_INTERFACE, NULL);
527         if (!info)
528                 return -EIO;
529
530         memset(&output, 0, sizeof(output));
531
532         size = *(u16 *) info->buffer.pointer;
533         if (size < 8) {
534                 DRM_INFO("ATCS buffer is too small: %zu\n", size);
535                 err = -EINVAL;
536                 goto out;
537         }
538         size = min(sizeof(output), size);
539
540         memcpy(&output, info->buffer.pointer, size);
541
542         /* TODO: check version? */
543         DRM_DEBUG_DRIVER("ATCS version %u\n", output.version);
544
545         amdgpu_atcs_parse_functions(&atcs->functions, output.function_bits);
546
547 out:
548         kfree(info);
549         return err;
550 }
551
552 /**
553  * amdgpu_acpi_is_pcie_performance_request_supported
554  *
555  * @adev: amdgpu_device pointer
556  *
557  * Check if the ATCS pcie_perf_req and pcie_dev_rdy methods
558  * are supported (all asics).
559  * returns true if supported, false if not.
560  */
561 bool amdgpu_acpi_is_pcie_performance_request_supported(struct amdgpu_device *adev)
562 {
563         struct amdgpu_atcs *atcs = &adev->atcs;
564
565         if (atcs->functions.pcie_perf_req && atcs->functions.pcie_dev_rdy)
566                 return true;
567
568         return false;
569 }
570
571 /**
572  * amdgpu_acpi_pcie_notify_device_ready
573  *
574  * @adev: amdgpu_device pointer
575  *
576  * Executes the PCIE_DEVICE_READY_NOTIFICATION method
577  * (all asics).
578  * returns 0 on success, error on failure.
579  */
580 int amdgpu_acpi_pcie_notify_device_ready(struct amdgpu_device *adev)
581 {
582         acpi_handle handle;
583         union acpi_object *info;
584         struct amdgpu_atcs *atcs = &adev->atcs;
585
586         /* Get the device handle */
587         handle = ACPI_HANDLE(&adev->pdev->dev);
588         if (!handle)
589                 return -EINVAL;
590
591         if (!atcs->functions.pcie_dev_rdy)
592                 return -EINVAL;
593
594         info = amdgpu_atcs_call(handle, ATCS_FUNCTION_PCIE_DEVICE_READY_NOTIFICATION, NULL);
595         if (!info)
596                 return -EIO;
597
598         kfree(info);
599
600         return 0;
601 }
602
603 /**
604  * amdgpu_acpi_pcie_performance_request
605  *
606  * @adev: amdgpu_device pointer
607  * @perf_req: requested perf level (pcie gen speed)
608  * @advertise: set advertise caps flag if set
609  *
610  * Executes the PCIE_PERFORMANCE_REQUEST method to
611  * change the pcie gen speed (all asics).
612  * returns 0 on success, error on failure.
613  */
614 int amdgpu_acpi_pcie_performance_request(struct amdgpu_device *adev,
615                                          u8 perf_req, bool advertise)
616 {
617         acpi_handle handle;
618         union acpi_object *info;
619         struct amdgpu_atcs *atcs = &adev->atcs;
620         struct atcs_pref_req_input atcs_input;
621         struct atcs_pref_req_output atcs_output;
622         struct acpi_buffer params;
623         size_t size;
624         u32 retry = 3;
625
626         if (amdgpu_acpi_pcie_notify_device_ready(adev))
627                 return -EINVAL;
628
629         /* Get the device handle */
630         handle = ACPI_HANDLE(&adev->pdev->dev);
631         if (!handle)
632                 return -EINVAL;
633
634         if (!atcs->functions.pcie_perf_req)
635                 return -EINVAL;
636
637         atcs_input.size = sizeof(struct atcs_pref_req_input);
638         /* client id (bit 2-0: func num, 7-3: dev num, 15-8: bus num) */
639         atcs_input.client_id = adev->pdev->devfn | (adev->pdev->bus->number << 8);
640         atcs_input.valid_flags_mask = ATCS_VALID_FLAGS_MASK;
641         atcs_input.flags = ATCS_WAIT_FOR_COMPLETION;
642         if (advertise)
643                 atcs_input.flags |= ATCS_ADVERTISE_CAPS;
644         atcs_input.req_type = ATCS_PCIE_LINK_SPEED;
645         atcs_input.perf_req = perf_req;
646
647         params.length = sizeof(struct atcs_pref_req_input);
648         params.pointer = &atcs_input;
649
650         while (retry--) {
651                 info = amdgpu_atcs_call(handle, ATCS_FUNCTION_PCIE_PERFORMANCE_REQUEST, &params);
652                 if (!info)
653                         return -EIO;
654
655                 memset(&atcs_output, 0, sizeof(atcs_output));
656
657                 size = *(u16 *) info->buffer.pointer;
658                 if (size < 3) {
659                         DRM_INFO("ATCS buffer is too small: %zu\n", size);
660                         kfree(info);
661                         return -EINVAL;
662                 }
663                 size = min(sizeof(atcs_output), size);
664
665                 memcpy(&atcs_output, info->buffer.pointer, size);
666
667                 kfree(info);
668
669                 switch (atcs_output.ret_val) {
670                 case ATCS_REQUEST_REFUSED:
671                 default:
672                         return -EINVAL;
673                 case ATCS_REQUEST_COMPLETE:
674                         return 0;
675                 case ATCS_REQUEST_IN_PROGRESS:
676                         udelay(10);
677                         break;
678                 }
679         }
680
681         return 0;
682 }
683
684 /**
685  * amdgpu_acpi_event - handle notify events
686  *
687  * @nb: notifier block
688  * @val: val
689  * @data: acpi event
690  *
691  * Calls relevant amdgpu functions in response to various
692  * acpi events.
693  * Returns NOTIFY code
694  */
695 static int amdgpu_acpi_event(struct notifier_block *nb,
696                              unsigned long val,
697                              void *data)
698 {
699         struct amdgpu_device *adev = container_of(nb, struct amdgpu_device, acpi_nb);
700         struct acpi_bus_event *entry = (struct acpi_bus_event *)data;
701
702         if (strcmp(entry->device_class, ACPI_AC_CLASS) == 0) {
703                 if (power_supply_is_system_supplied() > 0)
704                         DRM_DEBUG_DRIVER("pm: AC\n");
705                 else
706                         DRM_DEBUG_DRIVER("pm: DC\n");
707
708                 amdgpu_pm_acpi_event_handler(adev);
709         }
710
711         /* Check for pending SBIOS requests */
712         return amdgpu_atif_handler(adev, entry);
713 }
714
715 /* Call all ACPI methods here */
716 /**
717  * amdgpu_acpi_init - init driver acpi support
718  *
719  * @adev: amdgpu_device pointer
720  *
721  * Verifies the AMD ACPI interfaces and registers with the acpi
722  * notifier chain (all asics).
723  * Returns 0 on success, error on failure.
724  */
725 int amdgpu_acpi_init(struct amdgpu_device *adev)
726 {
727         acpi_handle handle, atif_handle;
728         struct amdgpu_atif *atif;
729         struct amdgpu_atcs *atcs = &adev->atcs;
730         int ret;
731
732         /* Get the device handle */
733         handle = ACPI_HANDLE(&adev->pdev->dev);
734
735         if (!adev->bios || !handle)
736                 return 0;
737
738         /* Call the ATCS method */
739         ret = amdgpu_atcs_verify_interface(handle, atcs);
740         if (ret) {
741                 DRM_DEBUG_DRIVER("Call to ATCS verify_interface failed: %d\n", ret);
742         }
743
744         /* Probe for ATIF, and initialize it if found */
745         atif_handle = amdgpu_atif_probe_handle(handle);
746         if (!atif_handle)
747                 goto out;
748
749         atif = kzalloc(sizeof(*atif), GFP_KERNEL);
750         if (!atif) {
751                 DRM_WARN("Not enough memory to initialize ATIF\n");
752                 goto out;
753         }
754         atif->handle = atif_handle;
755
756         /* Call the ATIF method */
757         ret = amdgpu_atif_verify_interface(atif);
758         if (ret) {
759                 DRM_DEBUG_DRIVER("Call to ATIF verify_interface failed: %d\n", ret);
760                 kfree(atif);
761                 goto out;
762         }
763         adev->atif = atif;
764
765         if (atif->notifications.brightness_change) {
766                 struct drm_encoder *tmp;
767
768                 /* Find the encoder controlling the brightness */
769                 list_for_each_entry(tmp, &adev->ddev->mode_config.encoder_list,
770                                 head) {
771                         struct amdgpu_encoder *enc = to_amdgpu_encoder(tmp);
772
773                         if ((enc->devices & (ATOM_DEVICE_LCD_SUPPORT)) &&
774                             enc->enc_priv) {
775                                 struct amdgpu_encoder_atom_dig *dig = enc->enc_priv;
776                                 if (dig->bl_dev) {
777                                         atif->encoder_for_bl = enc;
778                                         break;
779                                 }
780                         }
781                 }
782         }
783
784         if (atif->functions.sbios_requests && !atif->functions.system_params) {
785                 /* XXX check this workraround, if sbios request function is
786                  * present we have to see how it's configured in the system
787                  * params
788                  */
789                 atif->functions.system_params = true;
790         }
791
792         if (atif->functions.system_params) {
793                 ret = amdgpu_atif_get_notification_params(atif);
794                 if (ret) {
795                         DRM_DEBUG_DRIVER("Call to GET_SYSTEM_PARAMS failed: %d\n",
796                                         ret);
797                         /* Disable notification */
798                         atif->notification_cfg.enabled = false;
799                 }
800         }
801
802 out:
803         adev->acpi_nb.notifier_call = amdgpu_acpi_event;
804         register_acpi_notifier(&adev->acpi_nb);
805
806         return ret;
807 }
808
809 /**
810  * amdgpu_acpi_fini - tear down driver acpi support
811  *
812  * @adev: amdgpu_device pointer
813  *
814  * Unregisters with the acpi notifier chain (all asics).
815  */
816 void amdgpu_acpi_fini(struct amdgpu_device *adev)
817 {
818         unregister_acpi_notifier(&adev->acpi_nb);
819         if (adev->atif)
820                 kfree(adev->atif);
821 }