drm/radeon: re-organize the acpi notifier callback
[platform/kernel/linux-arm64.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 "drmP.h"
33 #include "drm.h"
34 #include "drm_sarea.h"
35 #include "drm_crtc_helper.h"
36 #include "radeon.h"
37 #include "radeon_acpi.h"
38 #include "atom.h"
39
40 #include <linux/vga_switcheroo.h>
41
42 #define ACPI_AC_CLASS           "ac_adapter"
43
44 extern void radeon_pm_acpi_event_handler(struct radeon_device *rdev);
45
46 struct atif_verify_interface {
47         u16 size;               /* structure size in bytes (includes size field) */
48         u16 version;            /* version */
49         u32 notification_mask;  /* supported notifications mask */
50         u32 function_bits;      /* supported functions bit vector */
51 } __packed;
52
53 struct atif_system_params {
54         u16 size;               /* structure size in bytes (includes size field) */
55         u32 valid_mask;         /* valid flags mask */
56         u32 flags;              /* flags */
57         u8 command_code;        /* notify command code */
58 } __packed;
59
60 struct atif_sbios_requests {
61         u16 size;               /* structure size in bytes (includes size field) */
62         u32 pending;            /* pending sbios requests */
63         u8 panel_exp_mode;      /* panel expansion mode */
64         u8 thermal_gfx;         /* thermal state: target gfx controller */
65         u8 thermal_state;       /* thermal state: state id (0: exit state, non-0: state) */
66         u8 forced_power_gfx;    /* forced power state: target gfx controller */
67         u8 forced_power_state;  /* forced power state: state id */
68         u8 system_power_src;    /* system power source */
69         u8 backlight_level;     /* panel backlight level (0-255) */
70 } __packed;
71
72 #define ATIF_NOTIFY_MASK        0x3
73 #define ATIF_NOTIFY_NONE        0
74 #define ATIF_NOTIFY_81          1
75 #define ATIF_NOTIFY_N           2
76
77 /* Call the ATIF method
78  */
79 static union acpi_object *radeon_atif_call(acpi_handle handle, int function,
80                 struct acpi_buffer *params)
81 {
82         acpi_status status;
83         union acpi_object atif_arg_elements[2];
84         struct acpi_object_list atif_arg;
85         struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
86
87         atif_arg.count = 2;
88         atif_arg.pointer = &atif_arg_elements[0];
89
90         atif_arg_elements[0].type = ACPI_TYPE_INTEGER;
91         atif_arg_elements[0].integer.value = function;
92
93         if (params) {
94                 atif_arg_elements[1].type = ACPI_TYPE_BUFFER;
95                 atif_arg_elements[1].buffer.length = params->length;
96                 atif_arg_elements[1].buffer.pointer = params->pointer;
97         } else {
98                 /* We need a second fake parameter */
99                 atif_arg_elements[1].type = ACPI_TYPE_INTEGER;
100                 atif_arg_elements[1].integer.value = 0;
101         }
102
103         status = acpi_evaluate_object(handle, "ATIF", &atif_arg, &buffer);
104
105         /* Fail only if calling the method fails and ATIF is supported */
106         if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
107                 DRM_DEBUG_DRIVER("failed to evaluate ATIF got %s\n",
108                                  acpi_format_exception(status));
109                 kfree(buffer.pointer);
110                 return NULL;
111         }
112
113         return buffer.pointer;
114 }
115
116 static void radeon_atif_parse_notification(struct radeon_atif_notifications *n, u32 mask)
117 {
118         n->display_switch = mask & ATIF_DISPLAY_SWITCH_REQUEST_SUPPORTED;
119         n->expansion_mode_change = mask & ATIF_EXPANSION_MODE_CHANGE_REQUEST_SUPPORTED;
120         n->thermal_state = mask & ATIF_THERMAL_STATE_CHANGE_REQUEST_SUPPORTED;
121         n->forced_power_state = mask & ATIF_FORCED_POWER_STATE_CHANGE_REQUEST_SUPPORTED;
122         n->system_power_state = mask & ATIF_SYSTEM_POWER_SOURCE_CHANGE_REQUEST_SUPPORTED;
123         n->display_conf_change = mask & ATIF_DISPLAY_CONF_CHANGE_REQUEST_SUPPORTED;
124         n->px_gfx_switch = mask & ATIF_PX_GFX_SWITCH_REQUEST_SUPPORTED;
125         n->brightness_change = mask & ATIF_PANEL_BRIGHTNESS_CHANGE_REQUEST_SUPPORTED;
126         n->dgpu_display_event = mask & ATIF_DGPU_DISPLAY_EVENT_SUPPORTED;
127 }
128
129 static void radeon_atif_parse_functions(struct radeon_atif_functions *f, u32 mask)
130 {
131         f->system_params = mask & ATIF_GET_SYSTEM_PARAMETERS_SUPPORTED;
132         f->sbios_requests = mask & ATIF_GET_SYSTEM_BIOS_REQUESTS_SUPPORTED;
133         f->select_active_disp = mask & ATIF_SELECT_ACTIVE_DISPLAYS_SUPPORTED;
134         f->lid_state = mask & ATIF_GET_LID_STATE_SUPPORTED;
135         f->get_tv_standard = mask & ATIF_GET_TV_STANDARD_FROM_CMOS_SUPPORTED;
136         f->set_tv_standard = mask & ATIF_SET_TV_STANDARD_IN_CMOS_SUPPORTED;
137         f->get_panel_expansion_mode = mask & ATIF_GET_PANEL_EXPANSION_MODE_FROM_CMOS_SUPPORTED;
138         f->set_panel_expansion_mode = mask & ATIF_SET_PANEL_EXPANSION_MODE_IN_CMOS_SUPPORTED;
139         f->temperature_change = mask & ATIF_TEMPERATURE_CHANGE_NOTIFICATION_SUPPORTED;
140         f->graphics_device_types = mask & ATIF_GET_GRAPHICS_DEVICE_TYPES_SUPPORTED;
141 }
142
143 static int radeon_atif_verify_interface(acpi_handle handle,
144                 struct radeon_atif *atif)
145 {
146         union acpi_object *info;
147         struct atif_verify_interface output;
148         size_t size;
149         int err = 0;
150
151         info = radeon_atif_call(handle, ATIF_FUNCTION_VERIFY_INTERFACE, NULL);
152         if (!info)
153                 return -EIO;
154
155         memset(&output, 0, sizeof(output));
156
157         size = *(u16 *) info->buffer.pointer;
158         if (size < 12) {
159                 DRM_INFO("ATIF buffer is too small: %lu\n", size);
160                 err = -EINVAL;
161                 goto out;
162         }
163         size = min(sizeof(output), size);
164
165         memcpy(&output, info->buffer.pointer, size);
166
167         /* TODO: check version? */
168         DRM_DEBUG_DRIVER("ATIF version %u\n", output.version);
169
170         radeon_atif_parse_notification(&atif->notifications, output.notification_mask);
171         radeon_atif_parse_functions(&atif->functions, output.function_bits);
172
173 out:
174         kfree(info);
175         return err;
176 }
177
178 static int radeon_atif_get_notification_params(acpi_handle handle,
179                 struct radeon_atif_notification_cfg *n)
180 {
181         union acpi_object *info;
182         struct atif_system_params params;
183         size_t size;
184         int err = 0;
185
186         info = radeon_atif_call(handle, ATIF_FUNCTION_GET_SYSTEM_PARAMETERS, NULL);
187         if (!info) {
188                 err = -EIO;
189                 goto out;
190         }
191
192         size = *(u16 *) info->buffer.pointer;
193         if (size < 10) {
194                 err = -EINVAL;
195                 goto out;
196         }
197
198         memset(&params, 0, sizeof(params));
199         size = min(sizeof(params), size);
200         memcpy(&params, info->buffer.pointer, size);
201
202         DRM_DEBUG_DRIVER("SYSTEM_PARAMS: mask = %#x, flags = %#x\n",
203                         params.flags, params.valid_mask);
204         params.flags = params.flags & params.valid_mask;
205
206         if ((params.flags & ATIF_NOTIFY_MASK) == ATIF_NOTIFY_NONE) {
207                 n->enabled = false;
208                 n->command_code = 0;
209         } else if ((params.flags & ATIF_NOTIFY_MASK) == ATIF_NOTIFY_81) {
210                 n->enabled = true;
211                 n->command_code = 0x81;
212         } else {
213                 if (size < 11) {
214                         err = -EINVAL;
215                         goto out;
216                 }
217                 n->enabled = true;
218                 n->command_code = params.command_code;
219         }
220
221 out:
222         DRM_DEBUG_DRIVER("Notification %s, command code = %#x\n",
223                         (n->enabled ? "enabled" : "disabled"),
224                         n->command_code);
225         kfree(info);
226         return err;
227 }
228
229 static int radeon_atif_get_sbios_requests(acpi_handle handle,
230                 struct atif_sbios_requests *req)
231 {
232         union acpi_object *info;
233         size_t size;
234         int count = 0;
235
236         info = radeon_atif_call(handle, ATIF_FUNCTION_GET_SYSTEM_BIOS_REQUESTS, NULL);
237         if (!info)
238                 return -EIO;
239
240         size = *(u16 *)info->buffer.pointer;
241         if (size < 0xd) {
242                 count = -EINVAL;
243                 goto out;
244         }
245         memset(req, 0, sizeof(*req));
246
247         size = min(sizeof(*req), size);
248         memcpy(req, info->buffer.pointer, size);
249         DRM_DEBUG_DRIVER("SBIOS pending requests: %#x\n", req->pending);
250
251         count = hweight32(req->pending);
252
253 out:
254         kfree(info);
255         return count;
256 }
257
258 int radeon_atif_handler(struct radeon_device *rdev,
259                 struct acpi_bus_event *event)
260 {
261         struct radeon_atif *atif = &rdev->atif;
262         struct atif_sbios_requests req;
263         acpi_handle handle;
264         int count;
265
266         DRM_DEBUG_DRIVER("event, device_class = %s, type = %#x\n",
267                         event->device_class, event->type);
268
269         if (strcmp(event->device_class, ACPI_VIDEO_CLASS) != 0)
270                 return NOTIFY_DONE;
271
272         if (!atif->notification_cfg.enabled ||
273                         event->type != atif->notification_cfg.command_code)
274                 /* Not our event */
275                 return NOTIFY_DONE;
276
277         /* Check pending SBIOS requests */
278         handle = DEVICE_ACPI_HANDLE(&rdev->pdev->dev);
279         count = radeon_atif_get_sbios_requests(handle, &req);
280
281         if (count <= 0)
282                 return NOTIFY_DONE;
283
284         DRM_DEBUG_DRIVER("ATIF: %d pending SBIOS requests\n", count);
285
286         if (req.pending & ATIF_PANEL_BRIGHTNESS_CHANGE_REQUEST) {
287                 struct radeon_encoder *enc = atif->backlight_ctl;
288
289                 if (enc) {
290                         struct radeon_encoder_atom_dig *dig = enc->enc_priv;
291                         dig->backlight_level = req.backlight_level;
292
293                         DRM_DEBUG_DRIVER("Changing brightness to %d\n",
294                                         req.backlight_level);
295
296                         atombios_set_panel_brightness(enc);
297
298                         backlight_force_update(dig->bl_dev,
299                                         BACKLIGHT_UPDATE_HOTKEY);
300                 }
301         }
302         /* TODO: check other events */
303
304         return NOTIFY_OK;
305 }
306
307 static int radeon_acpi_event(struct notifier_block *nb,
308                              unsigned long val,
309                              void *data)
310 {
311         struct radeon_device *rdev = container_of(nb, struct radeon_device, acpi_nb);
312         struct acpi_bus_event *entry = (struct acpi_bus_event *)data;
313
314         if (strcmp(entry->device_class, ACPI_AC_CLASS) == 0) {
315                 if (power_supply_is_system_supplied() > 0)
316                         DRM_DEBUG_DRIVER("pm: AC\n");
317                 else
318                         DRM_DEBUG_DRIVER("pm: DC\n");
319
320                 radeon_pm_acpi_event_handler(rdev);
321         }
322
323         /* Check for pending SBIOS requests */
324         return radeon_atif_handler(rdev, entry);
325 }
326
327 /* Call all ACPI methods here */
328 int radeon_acpi_init(struct radeon_device *rdev)
329 {
330         acpi_handle handle;
331         struct radeon_atif *atif = &rdev->atif;
332         int ret;
333
334         /* Get the device handle */
335         handle = DEVICE_ACPI_HANDLE(&rdev->pdev->dev);
336
337         /* No need to proceed if we're sure that ATIF is not supported */
338         if (!ASIC_IS_AVIVO(rdev) || !rdev->bios || !handle)
339                 return 0;
340
341         /* Call the ATIF method */
342         ret = radeon_atif_verify_interface(handle, atif);
343         if (ret) {
344                 DRM_DEBUG_DRIVER("Call to verify_interface failed: %d\n", ret);
345                 goto out;
346         }
347
348         if (atif->notifications.brightness_change) {
349                 struct drm_encoder *tmp;
350                 struct radeon_encoder *target = NULL;
351
352                 /* Find the encoder controlling the brightness */
353                 list_for_each_entry(tmp, &rdev->ddev->mode_config.encoder_list,
354                                 head) {
355                         struct radeon_encoder *enc = to_radeon_encoder(tmp);
356                         struct radeon_encoder_atom_dig *dig = enc->enc_priv;
357
358                         if ((enc->devices & (ATOM_DEVICE_LCD_SUPPORT)) &&
359                                         dig->bl_dev != NULL) {
360                                 target = enc;
361                                 break;
362                         }
363                 }
364
365                 atif->backlight_ctl = target;
366                 if (!target) {
367                         /* Brightness change notification is enabled, but we
368                          * didn't find a backlight controller, this should
369                          * never happen.
370                          */
371                         DRM_ERROR("Cannot find a backlight controller\n");
372                 }
373         }
374
375         if (atif->functions.sbios_requests && !atif->functions.system_params) {
376                 /* XXX check this workraround, if sbios request function is
377                  * present we have to see how it's configured in the system
378                  * params
379                  */
380                 atif->functions.system_params = true;
381         }
382
383         if (atif->functions.system_params) {
384                 ret = radeon_atif_get_notification_params(handle,
385                                 &atif->notification_cfg);
386                 if (ret) {
387                         DRM_DEBUG_DRIVER("Call to GET_SYSTEM_PARAMS failed: %d\n",
388                                         ret);
389                         /* Disable notification */
390                         atif->notification_cfg.enabled = false;
391                 }
392         }
393
394 out:
395         rdev->acpi_nb.notifier_call = radeon_acpi_event;
396         register_acpi_notifier(&rdev->acpi_nb);
397
398         return ret;
399 }
400
401 void radeon_acpi_fini(struct radeon_device *rdev)
402 {
403         unregister_acpi_notifier(&rdev->acpi_nb);
404 }