GNU Linux-libre 4.19.295-gnu1
[releases.git] / drivers / gpu / drm / vc4 / vc4_kms.c
1 /*
2  * Copyright (C) 2015 Broadcom
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  */
8
9 /**
10  * DOC: VC4 KMS
11  *
12  * This is the general code for implementing KMS mode setting that
13  * doesn't clearly associate with any of the other objects (plane,
14  * crtc, HDMI encoder).
15  */
16
17 #include <drm/drm_crtc.h>
18 #include <drm/drm_atomic.h>
19 #include <drm/drm_atomic_helper.h>
20 #include <drm/drm_crtc_helper.h>
21 #include <drm/drm_plane_helper.h>
22 #include <drm/drm_fb_helper.h>
23 #include <drm/drm_fb_cma_helper.h>
24 #include <drm/drm_gem_framebuffer_helper.h>
25 #include "vc4_drv.h"
26 #include "vc4_regs.h"
27
28 struct vc4_ctm_state {
29         struct drm_private_state base;
30         struct drm_color_ctm *ctm;
31         int fifo;
32 };
33
34 static struct vc4_ctm_state *to_vc4_ctm_state(struct drm_private_state *priv)
35 {
36         return container_of(priv, struct vc4_ctm_state, base);
37 }
38
39 static struct vc4_ctm_state *vc4_get_ctm_state(struct drm_atomic_state *state,
40                                                struct drm_private_obj *manager)
41 {
42         struct drm_device *dev = state->dev;
43         struct vc4_dev *vc4 = dev->dev_private;
44         struct drm_private_state *priv_state;
45         int ret;
46
47         ret = drm_modeset_lock(&vc4->ctm_state_lock, state->acquire_ctx);
48         if (ret)
49                 return ERR_PTR(ret);
50
51         priv_state = drm_atomic_get_private_obj_state(state, manager);
52         if (IS_ERR(priv_state))
53                 return ERR_CAST(priv_state);
54
55         return to_vc4_ctm_state(priv_state);
56 }
57
58 static struct drm_private_state *
59 vc4_ctm_duplicate_state(struct drm_private_obj *obj)
60 {
61         struct vc4_ctm_state *state;
62
63         state = kmemdup(obj->state, sizeof(*state), GFP_KERNEL);
64         if (!state)
65                 return NULL;
66
67         __drm_atomic_helper_private_obj_duplicate_state(obj, &state->base);
68
69         return &state->base;
70 }
71
72 static void vc4_ctm_destroy_state(struct drm_private_obj *obj,
73                                   struct drm_private_state *state)
74 {
75         struct vc4_ctm_state *ctm_state = to_vc4_ctm_state(state);
76
77         kfree(ctm_state);
78 }
79
80 static const struct drm_private_state_funcs vc4_ctm_state_funcs = {
81         .atomic_duplicate_state = vc4_ctm_duplicate_state,
82         .atomic_destroy_state = vc4_ctm_destroy_state,
83 };
84
85 /* Converts a DRM S31.32 value to the HW S0.9 format. */
86 static u16 vc4_ctm_s31_32_to_s0_9(u64 in)
87 {
88         u16 r;
89
90         /* Sign bit. */
91         r = in & BIT_ULL(63) ? BIT(9) : 0;
92
93         if ((in & GENMASK_ULL(62, 32)) > 0) {
94                 /* We have zero integer bits so we can only saturate here. */
95                 r |= GENMASK(8, 0);
96         } else {
97                 /* Otherwise take the 9 most important fractional bits. */
98                 r |= (in >> 23) & GENMASK(8, 0);
99         }
100
101         return r;
102 }
103
104 static void
105 vc4_ctm_commit(struct vc4_dev *vc4, struct drm_atomic_state *state)
106 {
107         struct vc4_ctm_state *ctm_state = to_vc4_ctm_state(vc4->ctm_manager.state);
108         struct drm_color_ctm *ctm = ctm_state->ctm;
109
110         if (ctm_state->fifo) {
111                 HVS_WRITE(SCALER_OLEDCOEF2,
112                           VC4_SET_FIELD(vc4_ctm_s31_32_to_s0_9(ctm->matrix[0]),
113                                         SCALER_OLEDCOEF2_R_TO_R) |
114                           VC4_SET_FIELD(vc4_ctm_s31_32_to_s0_9(ctm->matrix[3]),
115                                         SCALER_OLEDCOEF2_R_TO_G) |
116                           VC4_SET_FIELD(vc4_ctm_s31_32_to_s0_9(ctm->matrix[6]),
117                                         SCALER_OLEDCOEF2_R_TO_B));
118                 HVS_WRITE(SCALER_OLEDCOEF1,
119                           VC4_SET_FIELD(vc4_ctm_s31_32_to_s0_9(ctm->matrix[1]),
120                                         SCALER_OLEDCOEF1_G_TO_R) |
121                           VC4_SET_FIELD(vc4_ctm_s31_32_to_s0_9(ctm->matrix[4]),
122                                         SCALER_OLEDCOEF1_G_TO_G) |
123                           VC4_SET_FIELD(vc4_ctm_s31_32_to_s0_9(ctm->matrix[7]),
124                                         SCALER_OLEDCOEF1_G_TO_B));
125                 HVS_WRITE(SCALER_OLEDCOEF0,
126                           VC4_SET_FIELD(vc4_ctm_s31_32_to_s0_9(ctm->matrix[2]),
127                                         SCALER_OLEDCOEF0_B_TO_R) |
128                           VC4_SET_FIELD(vc4_ctm_s31_32_to_s0_9(ctm->matrix[5]),
129                                         SCALER_OLEDCOEF0_B_TO_G) |
130                           VC4_SET_FIELD(vc4_ctm_s31_32_to_s0_9(ctm->matrix[8]),
131                                         SCALER_OLEDCOEF0_B_TO_B));
132         }
133
134         HVS_WRITE(SCALER_OLEDOFFS,
135                   VC4_SET_FIELD(ctm_state->fifo, SCALER_OLEDOFFS_DISPFIFO));
136 }
137
138 static void
139 vc4_atomic_complete_commit(struct drm_atomic_state *state)
140 {
141         struct drm_device *dev = state->dev;
142         struct vc4_dev *vc4 = to_vc4_dev(dev);
143
144         drm_atomic_helper_wait_for_fences(dev, state, false);
145
146         drm_atomic_helper_wait_for_dependencies(state);
147
148         drm_atomic_helper_commit_modeset_disables(dev, state);
149
150         vc4_ctm_commit(vc4, state);
151
152         drm_atomic_helper_commit_planes(dev, state, 0);
153
154         drm_atomic_helper_commit_modeset_enables(dev, state);
155
156         drm_atomic_helper_fake_vblank(state);
157
158         drm_atomic_helper_commit_hw_done(state);
159
160         drm_atomic_helper_wait_for_flip_done(dev, state);
161
162         drm_atomic_helper_cleanup_planes(dev, state);
163
164         drm_atomic_helper_commit_cleanup_done(state);
165
166         drm_atomic_state_put(state);
167
168         up(&vc4->async_modeset);
169 }
170
171 static void commit_work(struct work_struct *work)
172 {
173         struct drm_atomic_state *state = container_of(work,
174                                                       struct drm_atomic_state,
175                                                       commit_work);
176         vc4_atomic_complete_commit(state);
177 }
178
179 /**
180  * vc4_atomic_commit - commit validated state object
181  * @dev: DRM device
182  * @state: the driver state object
183  * @nonblock: nonblocking commit
184  *
185  * This function commits a with drm_atomic_helper_check() pre-validated state
186  * object. This can still fail when e.g. the framebuffer reservation fails. For
187  * now this doesn't implement asynchronous commits.
188  *
189  * RETURNS
190  * Zero for success or -errno.
191  */
192 static int vc4_atomic_commit(struct drm_device *dev,
193                              struct drm_atomic_state *state,
194                              bool nonblock)
195 {
196         struct vc4_dev *vc4 = to_vc4_dev(dev);
197         int ret;
198
199         if (state->async_update) {
200                 ret = down_interruptible(&vc4->async_modeset);
201                 if (ret)
202                         return ret;
203
204                 ret = drm_atomic_helper_prepare_planes(dev, state);
205                 if (ret) {
206                         up(&vc4->async_modeset);
207                         return ret;
208                 }
209
210                 drm_atomic_helper_async_commit(dev, state);
211
212                 drm_atomic_helper_cleanup_planes(dev, state);
213
214                 up(&vc4->async_modeset);
215
216                 return 0;
217         }
218
219         /* We know for sure we don't want an async update here. Set
220          * state->legacy_cursor_update to false to prevent
221          * drm_atomic_helper_setup_commit() from auto-completing
222          * commit->flip_done.
223          */
224         state->legacy_cursor_update = false;
225         ret = drm_atomic_helper_setup_commit(state, nonblock);
226         if (ret)
227                 return ret;
228
229         INIT_WORK(&state->commit_work, commit_work);
230
231         ret = down_interruptible(&vc4->async_modeset);
232         if (ret)
233                 return ret;
234
235         ret = drm_atomic_helper_prepare_planes(dev, state);
236         if (ret) {
237                 up(&vc4->async_modeset);
238                 return ret;
239         }
240
241         if (!nonblock) {
242                 ret = drm_atomic_helper_wait_for_fences(dev, state, true);
243                 if (ret) {
244                         drm_atomic_helper_cleanup_planes(dev, state);
245                         up(&vc4->async_modeset);
246                         return ret;
247                 }
248         }
249
250         /*
251          * This is the point of no return - everything below never fails except
252          * when the hw goes bonghits. Which means we can commit the new state on
253          * the software side now.
254          */
255
256         BUG_ON(drm_atomic_helper_swap_state(state, false) < 0);
257
258         /*
259          * Everything below can be run asynchronously without the need to grab
260          * any modeset locks at all under one condition: It must be guaranteed
261          * that the asynchronous work has either been cancelled (if the driver
262          * supports it, which at least requires that the framebuffers get
263          * cleaned up with drm_atomic_helper_cleanup_planes()) or completed
264          * before the new state gets committed on the software side with
265          * drm_atomic_helper_swap_state().
266          *
267          * This scheme allows new atomic state updates to be prepared and
268          * checked in parallel to the asynchronous completion of the previous
269          * update. Which is important since compositors need to figure out the
270          * composition of the next frame right after having submitted the
271          * current layout.
272          */
273
274         drm_atomic_state_get(state);
275         if (nonblock)
276                 queue_work(system_unbound_wq, &state->commit_work);
277         else
278                 vc4_atomic_complete_commit(state);
279
280         return 0;
281 }
282
283 static struct drm_framebuffer *vc4_fb_create(struct drm_device *dev,
284                                              struct drm_file *file_priv,
285                                              const struct drm_mode_fb_cmd2 *mode_cmd)
286 {
287         struct drm_mode_fb_cmd2 mode_cmd_local;
288
289         /* If the user didn't specify a modifier, use the
290          * vc4_set_tiling_ioctl() state for the BO.
291          */
292         if (!(mode_cmd->flags & DRM_MODE_FB_MODIFIERS)) {
293                 struct drm_gem_object *gem_obj;
294                 struct vc4_bo *bo;
295
296                 gem_obj = drm_gem_object_lookup(file_priv,
297                                                 mode_cmd->handles[0]);
298                 if (!gem_obj) {
299                         DRM_DEBUG("Failed to look up GEM BO %d\n",
300                                   mode_cmd->handles[0]);
301                         return ERR_PTR(-ENOENT);
302                 }
303                 bo = to_vc4_bo(gem_obj);
304
305                 mode_cmd_local = *mode_cmd;
306
307                 if (bo->t_format) {
308                         mode_cmd_local.modifier[0] =
309                                 DRM_FORMAT_MOD_BROADCOM_VC4_T_TILED;
310                 } else {
311                         mode_cmd_local.modifier[0] = DRM_FORMAT_MOD_NONE;
312                 }
313
314                 drm_gem_object_put_unlocked(gem_obj);
315
316                 mode_cmd = &mode_cmd_local;
317         }
318
319         return drm_gem_fb_create(dev, file_priv, mode_cmd);
320 }
321
322 /* Our CTM has some peculiar limitations: we can only enable it for one CRTC
323  * at a time and the HW only supports S0.9 scalars. To account for the latter,
324  * we don't allow userland to set a CTM that we have no hope of approximating.
325  */
326 static int
327 vc4_ctm_atomic_check(struct drm_device *dev, struct drm_atomic_state *state)
328 {
329         struct vc4_dev *vc4 = to_vc4_dev(dev);
330         struct vc4_ctm_state *ctm_state = NULL;
331         struct drm_crtc *crtc;
332         struct drm_crtc_state *old_crtc_state, *new_crtc_state;
333         struct drm_color_ctm *ctm;
334         int i;
335
336         for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
337                 /* CTM is being disabled. */
338                 if (!new_crtc_state->ctm && old_crtc_state->ctm) {
339                         ctm_state = vc4_get_ctm_state(state, &vc4->ctm_manager);
340                         if (IS_ERR(ctm_state))
341                                 return PTR_ERR(ctm_state);
342                         ctm_state->fifo = 0;
343                 }
344         }
345
346         for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
347                 if (new_crtc_state->ctm == old_crtc_state->ctm)
348                         continue;
349
350                 if (!ctm_state) {
351                         ctm_state = vc4_get_ctm_state(state, &vc4->ctm_manager);
352                         if (IS_ERR(ctm_state))
353                                 return PTR_ERR(ctm_state);
354                 }
355
356                 /* CTM is being enabled or the matrix changed. */
357                 if (new_crtc_state->ctm) {
358                         /* fifo is 1-based since 0 disables CTM. */
359                         int fifo = to_vc4_crtc(crtc)->channel + 1;
360
361                         /* Check userland isn't trying to turn on CTM for more
362                          * than one CRTC at a time.
363                          */
364                         if (ctm_state->fifo && ctm_state->fifo != fifo) {
365                                 DRM_DEBUG_DRIVER("Too many CTM configured\n");
366                                 return -EINVAL;
367                         }
368
369                         /* Check we can approximate the specified CTM.
370                          * We disallow scalars |c| > 1.0 since the HW has
371                          * no integer bits.
372                          */
373                         ctm = new_crtc_state->ctm->data;
374                         for (i = 0; i < ARRAY_SIZE(ctm->matrix); i++) {
375                                 u64 val = ctm->matrix[i];
376
377                                 val &= ~BIT_ULL(63);
378                                 if (val > BIT_ULL(32))
379                                         return -EINVAL;
380                         }
381
382                         ctm_state->fifo = fifo;
383                         ctm_state->ctm = ctm;
384                 }
385         }
386
387         return 0;
388 }
389
390 static int
391 vc4_atomic_check(struct drm_device *dev, struct drm_atomic_state *state)
392 {
393         int ret;
394
395         ret = vc4_ctm_atomic_check(dev, state);
396         if (ret < 0)
397                 return ret;
398
399         return drm_atomic_helper_check(dev, state);
400 }
401
402 static const struct drm_mode_config_funcs vc4_mode_funcs = {
403         .output_poll_changed = drm_fb_helper_output_poll_changed,
404         .atomic_check = vc4_atomic_check,
405         .atomic_commit = vc4_atomic_commit,
406         .fb_create = vc4_fb_create,
407 };
408
409 int vc4_kms_load(struct drm_device *dev)
410 {
411         struct vc4_dev *vc4 = to_vc4_dev(dev);
412         struct vc4_ctm_state *ctm_state;
413         int ret;
414
415         sema_init(&vc4->async_modeset, 1);
416
417         /* Set support for vblank irq fast disable, before drm_vblank_init() */
418         dev->vblank_disable_immediate = true;
419
420         ret = drm_vblank_init(dev, dev->mode_config.num_crtc);
421         if (ret < 0) {
422                 dev_err(dev->dev, "failed to initialize vblank\n");
423                 return ret;
424         }
425
426         dev->mode_config.max_width = 2048;
427         dev->mode_config.max_height = 2048;
428         dev->mode_config.funcs = &vc4_mode_funcs;
429         dev->mode_config.preferred_depth = 24;
430         dev->mode_config.async_page_flip = true;
431         dev->mode_config.allow_fb_modifiers = true;
432
433         drm_modeset_lock_init(&vc4->ctm_state_lock);
434
435         ctm_state = kzalloc(sizeof(*ctm_state), GFP_KERNEL);
436         if (!ctm_state)
437                 return -ENOMEM;
438         drm_atomic_private_obj_init(&vc4->ctm_manager, &ctm_state->base,
439                                     &vc4_ctm_state_funcs);
440
441         drm_mode_config_reset(dev);
442
443         if (dev->mode_config.num_connector)
444                 drm_fb_cma_fbdev_init(dev, 32, 0);
445
446         drm_kms_helper_poll_init(dev);
447
448         return 0;
449 }