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