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