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