]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/media/platform/vsp1/vsp1_dl.c
[media] v4l: vsp1: Use pipeline display list to decide how to write to modules
[karo-tx-linux.git] / drivers / media / platform / vsp1 / vsp1_dl.c
1 /*
2  * vsp1_dl.h  --  R-Car VSP1 Display List
3  *
4  * Copyright (C) 2015 Renesas Corporation
5  *
6  * Contact: Laurent Pinchart (laurent.pinchart@ideasonboard.com)
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  */
13
14 #include <linux/device.h>
15 #include <linux/dma-mapping.h>
16 #include <linux/gfp.h>
17 #include <linux/slab.h>
18
19 #include "vsp1.h"
20 #include "vsp1_dl.h"
21
22 /*
23  * Global resources
24  *
25  * - Display-related interrupts (can be used for vblank evasion ?)
26  * - Display-list enable
27  * - Header-less for WPF0
28  * - DL swap
29  */
30
31 #define VSP1_DL_BODY_SIZE               (2 * 4 * 256)
32 #define VSP1_DL_NUM_LISTS               3
33
34 struct vsp1_dl_entry {
35         u32 addr;
36         u32 data;
37 } __attribute__((__packed__));
38
39 struct vsp1_dl_list {
40         size_t size;
41         int reg_count;
42
43         bool in_use;
44
45         struct vsp1_dl_entry *body;
46         dma_addr_t dma;
47 };
48
49 /**
50  * struct vsp1_dl - Display List manager
51  * @vsp1: the VSP1 device
52  * @lock: protects the active, queued and pending lists
53  * @lists.all: array of all allocate display lists
54  * @lists.active: list currently being processed (loaded) by hardware
55  * @lists.queued: list queued to the hardware (written to the DL registers)
56  * @lists.pending: list waiting to be queued to the hardware
57  * @lists.write: list being written to by software
58  */
59 struct vsp1_dl {
60         struct vsp1_device *vsp1;
61
62         spinlock_t lock;
63
64         size_t size;
65         dma_addr_t dma;
66         void *mem;
67
68         struct {
69                 struct vsp1_dl_list all[VSP1_DL_NUM_LISTS];
70
71                 struct vsp1_dl_list *active;
72                 struct vsp1_dl_list *queued;
73                 struct vsp1_dl_list *pending;
74                 struct vsp1_dl_list *write;
75         } lists;
76 };
77
78 /* -----------------------------------------------------------------------------
79  * Display List Transaction Management
80  */
81
82 static void vsp1_dl_free_list(struct vsp1_dl_list *list)
83 {
84         if (!list)
85                 return;
86
87         list->in_use = false;
88 }
89
90 void vsp1_dl_reset(struct vsp1_dl *dl)
91 {
92         unsigned int i;
93
94         dl->lists.active = NULL;
95         dl->lists.queued = NULL;
96         dl->lists.pending = NULL;
97         dl->lists.write = NULL;
98
99         for (i = 0; i < ARRAY_SIZE(dl->lists.all); ++i)
100                 dl->lists.all[i].in_use = false;
101 }
102
103 void vsp1_dl_begin(struct vsp1_dl *dl)
104 {
105         struct vsp1_dl_list *list = NULL;
106         unsigned long flags;
107         unsigned int i;
108
109         spin_lock_irqsave(&dl->lock, flags);
110
111         for (i = 0; i < ARRAY_SIZE(dl->lists.all); ++i) {
112                 if (!dl->lists.all[i].in_use) {
113                         list = &dl->lists.all[i];
114                         break;
115                 }
116         }
117
118         if (!list) {
119                 list = dl->lists.pending;
120                 dl->lists.pending = NULL;
121         }
122
123         spin_unlock_irqrestore(&dl->lock, flags);
124
125         dl->lists.write = list;
126
127         list->in_use = true;
128         list->reg_count = 0;
129 }
130
131 void vsp1_dl_add(struct vsp1_dl *dl, u32 reg, u32 data)
132 {
133         struct vsp1_dl_list *list = dl->lists.write;
134
135         list->body[list->reg_count].addr = reg;
136         list->body[list->reg_count].data = data;
137         list->reg_count++;
138 }
139
140 void vsp1_dl_commit(struct vsp1_dl *dl)
141 {
142         struct vsp1_device *vsp1 = dl->vsp1;
143         struct vsp1_dl_list *list;
144         unsigned long flags;
145         bool update;
146
147         list = dl->lists.write;
148         dl->lists.write = NULL;
149
150         spin_lock_irqsave(&dl->lock, flags);
151
152         /* Once the UPD bit has been set the hardware can start processing the
153          * display list at any time and we can't touch the address and size
154          * registers. In that case mark the update as pending, it will be
155          * queued up to the hardware by the frame end interrupt handler.
156          */
157         update = !!(vsp1_read(vsp1, VI6_DL_BODY_SIZE) & VI6_DL_BODY_SIZE_UPD);
158         if (update) {
159                 vsp1_dl_free_list(dl->lists.pending);
160                 dl->lists.pending = list;
161                 goto done;
162         }
163
164         /* Program the hardware with the display list body address and size.
165          * The UPD bit will be cleared by the device when the display list is
166          * processed.
167          */
168         vsp1_write(vsp1, VI6_DL_HDR_ADDR(0), list->dma);
169         vsp1_write(vsp1, VI6_DL_BODY_SIZE, VI6_DL_BODY_SIZE_UPD |
170                    (list->reg_count * 8));
171
172         vsp1_dl_free_list(dl->lists.queued);
173         dl->lists.queued = list;
174
175 done:
176         spin_unlock_irqrestore(&dl->lock, flags);
177 }
178
179 /* -----------------------------------------------------------------------------
180  * Interrupt Handling
181  */
182
183 void vsp1_dl_irq_display_start(struct vsp1_dl *dl)
184 {
185         spin_lock(&dl->lock);
186
187         /* The display start interrupt signals the end of the display list
188          * processing by the device. The active display list, if any, won't be
189          * accessed anymore and can be reused.
190          */
191         if (dl->lists.active) {
192                 vsp1_dl_free_list(dl->lists.active);
193                 dl->lists.active = NULL;
194         }
195
196         spin_unlock(&dl->lock);
197 }
198
199 void vsp1_dl_irq_frame_end(struct vsp1_dl *dl)
200 {
201         struct vsp1_device *vsp1 = dl->vsp1;
202
203         spin_lock(&dl->lock);
204
205         /* The UPD bit set indicates that the commit operation raced with the
206          * interrupt and occurred after the frame end event and UPD clear but
207          * before interrupt processing. The hardware hasn't taken the update
208          * into account yet, we'll thus skip one frame and retry.
209          */
210         if (vsp1_read(vsp1, VI6_DL_BODY_SIZE) & VI6_DL_BODY_SIZE_UPD)
211                 goto done;
212
213         /* The device starts processing the queued display list right after the
214          * frame end interrupt. The display list thus becomes active.
215          */
216         if (dl->lists.queued) {
217                 WARN_ON(dl->lists.active);
218                 dl->lists.active = dl->lists.queued;
219                 dl->lists.queued = NULL;
220         }
221
222         /* Now that the UPD bit has been cleared we can queue the next display
223          * list to the hardware if one has been prepared.
224          */
225         if (dl->lists.pending) {
226                 struct vsp1_dl_list *list = dl->lists.pending;
227
228                 vsp1_write(vsp1, VI6_DL_HDR_ADDR(0), list->dma);
229                 vsp1_write(vsp1, VI6_DL_BODY_SIZE, VI6_DL_BODY_SIZE_UPD |
230                            (list->reg_count * 8));
231
232                 dl->lists.queued = list;
233                 dl->lists.pending = NULL;
234         }
235
236 done:
237         spin_unlock(&dl->lock);
238 }
239
240 /* -----------------------------------------------------------------------------
241  * Hardware Setup
242  */
243
244 void vsp1_dl_setup(struct vsp1_device *vsp1)
245 {
246         u32 ctrl = (256 << VI6_DL_CTRL_AR_WAIT_SHIFT)
247                  | VI6_DL_CTRL_DC2 | VI6_DL_CTRL_DC1 | VI6_DL_CTRL_DC0
248                  | VI6_DL_CTRL_DLE;
249
250         /* The DRM pipeline operates with header-less display lists in
251          * Continuous Frame Mode.
252          */
253         if (vsp1->drm)
254                 ctrl |= VI6_DL_CTRL_CFM0 | VI6_DL_CTRL_NH0;
255
256         vsp1_write(vsp1, VI6_DL_CTRL, ctrl);
257         vsp1_write(vsp1, VI6_DL_SWAP, VI6_DL_SWAP_LWS);
258 }
259
260 /* -----------------------------------------------------------------------------
261  * Initialization and Cleanup
262  */
263
264 struct vsp1_dl *vsp1_dl_create(struct vsp1_device *vsp1)
265 {
266         struct vsp1_dl *dl;
267         unsigned int i;
268
269         dl = kzalloc(sizeof(*dl), GFP_KERNEL);
270         if (!dl)
271                 return NULL;
272
273         spin_lock_init(&dl->lock);
274
275         dl->vsp1 = vsp1;
276         dl->size = VSP1_DL_BODY_SIZE * ARRAY_SIZE(dl->lists.all);
277
278         dl->mem = dma_alloc_wc(vsp1->dev, dl->size, &dl->dma,
279                                          GFP_KERNEL);
280         if (!dl->mem) {
281                 kfree(dl);
282                 return NULL;
283         }
284
285         for (i = 0; i < ARRAY_SIZE(dl->lists.all); ++i) {
286                 struct vsp1_dl_list *list = &dl->lists.all[i];
287
288                 list->size = VSP1_DL_BODY_SIZE;
289                 list->reg_count = 0;
290                 list->in_use = false;
291                 list->dma = dl->dma + VSP1_DL_BODY_SIZE * i;
292                 list->body = dl->mem + VSP1_DL_BODY_SIZE * i;
293         }
294
295         return dl;
296 }
297
298 void vsp1_dl_destroy(struct vsp1_dl *dl)
299 {
300         dma_free_wc(dl->vsp1->dev, dl->size, dl->mem, dl->dma);
301         kfree(dl);
302 }