xref: /openbmc/linux/drivers/media/platform/renesas/vsp1/vsp1_dl.c (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
1*ee4a77a3SMauro Carvalho Chehab // SPDX-License-Identifier: GPL-2.0+
2*ee4a77a3SMauro Carvalho Chehab /*
3*ee4a77a3SMauro Carvalho Chehab  * vsp1_dl.c  --  R-Car VSP1 Display List
4*ee4a77a3SMauro Carvalho Chehab  *
5*ee4a77a3SMauro Carvalho Chehab  * Copyright (C) 2015 Renesas Corporation
6*ee4a77a3SMauro Carvalho Chehab  *
7*ee4a77a3SMauro Carvalho Chehab  * Contact: Laurent Pinchart (laurent.pinchart@ideasonboard.com)
8*ee4a77a3SMauro Carvalho Chehab  */
9*ee4a77a3SMauro Carvalho Chehab 
10*ee4a77a3SMauro Carvalho Chehab #include <linux/device.h>
11*ee4a77a3SMauro Carvalho Chehab #include <linux/dma-mapping.h>
12*ee4a77a3SMauro Carvalho Chehab #include <linux/gfp.h>
13*ee4a77a3SMauro Carvalho Chehab #include <linux/refcount.h>
14*ee4a77a3SMauro Carvalho Chehab #include <linux/slab.h>
15*ee4a77a3SMauro Carvalho Chehab #include <linux/workqueue.h>
16*ee4a77a3SMauro Carvalho Chehab 
17*ee4a77a3SMauro Carvalho Chehab #include "vsp1.h"
18*ee4a77a3SMauro Carvalho Chehab #include "vsp1_dl.h"
19*ee4a77a3SMauro Carvalho Chehab 
20*ee4a77a3SMauro Carvalho Chehab #define VSP1_DL_NUM_ENTRIES		256
21*ee4a77a3SMauro Carvalho Chehab 
22*ee4a77a3SMauro Carvalho Chehab #define VSP1_DLH_INT_ENABLE		(1 << 1)
23*ee4a77a3SMauro Carvalho Chehab #define VSP1_DLH_AUTO_START		(1 << 0)
24*ee4a77a3SMauro Carvalho Chehab 
25*ee4a77a3SMauro Carvalho Chehab #define VSP1_DLH_EXT_PRE_CMD_EXEC	(1 << 9)
26*ee4a77a3SMauro Carvalho Chehab #define VSP1_DLH_EXT_POST_CMD_EXEC	(1 << 8)
27*ee4a77a3SMauro Carvalho Chehab 
28*ee4a77a3SMauro Carvalho Chehab struct vsp1_dl_header_list {
29*ee4a77a3SMauro Carvalho Chehab 	u32 num_bytes;
30*ee4a77a3SMauro Carvalho Chehab 	u32 addr;
31*ee4a77a3SMauro Carvalho Chehab } __packed;
32*ee4a77a3SMauro Carvalho Chehab 
33*ee4a77a3SMauro Carvalho Chehab struct vsp1_dl_header {
34*ee4a77a3SMauro Carvalho Chehab 	u32 num_lists;
35*ee4a77a3SMauro Carvalho Chehab 	struct vsp1_dl_header_list lists[8];
36*ee4a77a3SMauro Carvalho Chehab 	u32 next_header;
37*ee4a77a3SMauro Carvalho Chehab 	u32 flags;
38*ee4a77a3SMauro Carvalho Chehab } __packed;
39*ee4a77a3SMauro Carvalho Chehab 
40*ee4a77a3SMauro Carvalho Chehab /**
41*ee4a77a3SMauro Carvalho Chehab  * struct vsp1_dl_ext_header - Extended display list header
42*ee4a77a3SMauro Carvalho Chehab  * @padding: padding zero bytes for alignment
43*ee4a77a3SMauro Carvalho Chehab  * @pre_ext_dl_num_cmd: number of pre-extended command bodies to parse
44*ee4a77a3SMauro Carvalho Chehab  * @flags: enables or disables execution of the pre and post command
45*ee4a77a3SMauro Carvalho Chehab  * @pre_ext_dl_plist: start address of pre-extended display list bodies
46*ee4a77a3SMauro Carvalho Chehab  * @post_ext_dl_num_cmd: number of post-extended command bodies to parse
47*ee4a77a3SMauro Carvalho Chehab  * @post_ext_dl_plist: start address of post-extended display list bodies
48*ee4a77a3SMauro Carvalho Chehab  */
49*ee4a77a3SMauro Carvalho Chehab struct vsp1_dl_ext_header {
50*ee4a77a3SMauro Carvalho Chehab 	u32 padding;
51*ee4a77a3SMauro Carvalho Chehab 
52*ee4a77a3SMauro Carvalho Chehab 	/*
53*ee4a77a3SMauro Carvalho Chehab 	 * The datasheet represents flags as stored before pre_ext_dl_num_cmd,
54*ee4a77a3SMauro Carvalho Chehab 	 * expecting 32-bit accesses. The flags are appropriate to the whole
55*ee4a77a3SMauro Carvalho Chehab 	 * header, not just the pre_ext command, and thus warrant being
56*ee4a77a3SMauro Carvalho Chehab 	 * separated out. Due to byte ordering, and representing as 16 bit
57*ee4a77a3SMauro Carvalho Chehab 	 * values here, the flags must be positioned after the
58*ee4a77a3SMauro Carvalho Chehab 	 * pre_ext_dl_num_cmd.
59*ee4a77a3SMauro Carvalho Chehab 	 */
60*ee4a77a3SMauro Carvalho Chehab 	u16 pre_ext_dl_num_cmd;
61*ee4a77a3SMauro Carvalho Chehab 	u16 flags;
62*ee4a77a3SMauro Carvalho Chehab 	u32 pre_ext_dl_plist;
63*ee4a77a3SMauro Carvalho Chehab 
64*ee4a77a3SMauro Carvalho Chehab 	u32 post_ext_dl_num_cmd;
65*ee4a77a3SMauro Carvalho Chehab 	u32 post_ext_dl_plist;
66*ee4a77a3SMauro Carvalho Chehab } __packed;
67*ee4a77a3SMauro Carvalho Chehab 
68*ee4a77a3SMauro Carvalho Chehab struct vsp1_dl_header_extended {
69*ee4a77a3SMauro Carvalho Chehab 	struct vsp1_dl_header header;
70*ee4a77a3SMauro Carvalho Chehab 	struct vsp1_dl_ext_header ext;
71*ee4a77a3SMauro Carvalho Chehab } __packed;
72*ee4a77a3SMauro Carvalho Chehab 
73*ee4a77a3SMauro Carvalho Chehab struct vsp1_dl_entry {
74*ee4a77a3SMauro Carvalho Chehab 	u32 addr;
75*ee4a77a3SMauro Carvalho Chehab 	u32 data;
76*ee4a77a3SMauro Carvalho Chehab } __packed;
77*ee4a77a3SMauro Carvalho Chehab 
78*ee4a77a3SMauro Carvalho Chehab /**
79*ee4a77a3SMauro Carvalho Chehab  * struct vsp1_pre_ext_dl_body - Pre Extended Display List Body
80*ee4a77a3SMauro Carvalho Chehab  * @opcode: Extended display list command operation code
81*ee4a77a3SMauro Carvalho Chehab  * @flags: Pre-extended command flags. These are specific to each command
82*ee4a77a3SMauro Carvalho Chehab  * @address_set: Source address set pointer. Must have 16-byte alignment
83*ee4a77a3SMauro Carvalho Chehab  * @reserved: Zero bits for alignment.
84*ee4a77a3SMauro Carvalho Chehab  */
85*ee4a77a3SMauro Carvalho Chehab struct vsp1_pre_ext_dl_body {
86*ee4a77a3SMauro Carvalho Chehab 	u32 opcode;
87*ee4a77a3SMauro Carvalho Chehab 	u32 flags;
88*ee4a77a3SMauro Carvalho Chehab 	u32 address_set;
89*ee4a77a3SMauro Carvalho Chehab 	u32 reserved;
90*ee4a77a3SMauro Carvalho Chehab } __packed;
91*ee4a77a3SMauro Carvalho Chehab 
92*ee4a77a3SMauro Carvalho Chehab /**
93*ee4a77a3SMauro Carvalho Chehab  * struct vsp1_dl_body - Display list body
94*ee4a77a3SMauro Carvalho Chehab  * @list: entry in the display list list of bodies
95*ee4a77a3SMauro Carvalho Chehab  * @free: entry in the pool free body list
96*ee4a77a3SMauro Carvalho Chehab  * @refcnt: reference tracking for the body
97*ee4a77a3SMauro Carvalho Chehab  * @pool: pool to which this body belongs
98*ee4a77a3SMauro Carvalho Chehab  * @entries: array of entries
99*ee4a77a3SMauro Carvalho Chehab  * @dma: DMA address of the entries
100*ee4a77a3SMauro Carvalho Chehab  * @size: size of the DMA memory in bytes
101*ee4a77a3SMauro Carvalho Chehab  * @num_entries: number of stored entries
102*ee4a77a3SMauro Carvalho Chehab  * @max_entries: number of entries available
103*ee4a77a3SMauro Carvalho Chehab  */
104*ee4a77a3SMauro Carvalho Chehab struct vsp1_dl_body {
105*ee4a77a3SMauro Carvalho Chehab 	struct list_head list;
106*ee4a77a3SMauro Carvalho Chehab 	struct list_head free;
107*ee4a77a3SMauro Carvalho Chehab 
108*ee4a77a3SMauro Carvalho Chehab 	refcount_t refcnt;
109*ee4a77a3SMauro Carvalho Chehab 
110*ee4a77a3SMauro Carvalho Chehab 	struct vsp1_dl_body_pool *pool;
111*ee4a77a3SMauro Carvalho Chehab 
112*ee4a77a3SMauro Carvalho Chehab 	struct vsp1_dl_entry *entries;
113*ee4a77a3SMauro Carvalho Chehab 	dma_addr_t dma;
114*ee4a77a3SMauro Carvalho Chehab 	size_t size;
115*ee4a77a3SMauro Carvalho Chehab 
116*ee4a77a3SMauro Carvalho Chehab 	unsigned int num_entries;
117*ee4a77a3SMauro Carvalho Chehab 	unsigned int max_entries;
118*ee4a77a3SMauro Carvalho Chehab };
119*ee4a77a3SMauro Carvalho Chehab 
120*ee4a77a3SMauro Carvalho Chehab /**
121*ee4a77a3SMauro Carvalho Chehab  * struct vsp1_dl_body_pool - display list body pool
122*ee4a77a3SMauro Carvalho Chehab  * @dma: DMA address of the entries
123*ee4a77a3SMauro Carvalho Chehab  * @size: size of the full DMA memory pool in bytes
124*ee4a77a3SMauro Carvalho Chehab  * @mem: CPU memory pointer for the pool
125*ee4a77a3SMauro Carvalho Chehab  * @bodies: Array of DLB structures for the pool
126*ee4a77a3SMauro Carvalho Chehab  * @free: List of free DLB entries
127*ee4a77a3SMauro Carvalho Chehab  * @lock: Protects the free list
128*ee4a77a3SMauro Carvalho Chehab  * @vsp1: the VSP1 device
129*ee4a77a3SMauro Carvalho Chehab  */
130*ee4a77a3SMauro Carvalho Chehab struct vsp1_dl_body_pool {
131*ee4a77a3SMauro Carvalho Chehab 	/* DMA allocation */
132*ee4a77a3SMauro Carvalho Chehab 	dma_addr_t dma;
133*ee4a77a3SMauro Carvalho Chehab 	size_t size;
134*ee4a77a3SMauro Carvalho Chehab 	void *mem;
135*ee4a77a3SMauro Carvalho Chehab 
136*ee4a77a3SMauro Carvalho Chehab 	/* Body management */
137*ee4a77a3SMauro Carvalho Chehab 	struct vsp1_dl_body *bodies;
138*ee4a77a3SMauro Carvalho Chehab 	struct list_head free;
139*ee4a77a3SMauro Carvalho Chehab 	spinlock_t lock;
140*ee4a77a3SMauro Carvalho Chehab 
141*ee4a77a3SMauro Carvalho Chehab 	struct vsp1_device *vsp1;
142*ee4a77a3SMauro Carvalho Chehab };
143*ee4a77a3SMauro Carvalho Chehab 
144*ee4a77a3SMauro Carvalho Chehab /**
145*ee4a77a3SMauro Carvalho Chehab  * struct vsp1_dl_cmd_pool - Display List commands pool
146*ee4a77a3SMauro Carvalho Chehab  * @dma: DMA address of the entries
147*ee4a77a3SMauro Carvalho Chehab  * @size: size of the full DMA memory pool in bytes
148*ee4a77a3SMauro Carvalho Chehab  * @mem: CPU memory pointer for the pool
149*ee4a77a3SMauro Carvalho Chehab  * @cmds: Array of command structures for the pool
150*ee4a77a3SMauro Carvalho Chehab  * @free: Free pool entries
151*ee4a77a3SMauro Carvalho Chehab  * @lock: Protects the free list
152*ee4a77a3SMauro Carvalho Chehab  * @vsp1: the VSP1 device
153*ee4a77a3SMauro Carvalho Chehab  */
154*ee4a77a3SMauro Carvalho Chehab struct vsp1_dl_cmd_pool {
155*ee4a77a3SMauro Carvalho Chehab 	/* DMA allocation */
156*ee4a77a3SMauro Carvalho Chehab 	dma_addr_t dma;
157*ee4a77a3SMauro Carvalho Chehab 	size_t size;
158*ee4a77a3SMauro Carvalho Chehab 	void *mem;
159*ee4a77a3SMauro Carvalho Chehab 
160*ee4a77a3SMauro Carvalho Chehab 	struct vsp1_dl_ext_cmd *cmds;
161*ee4a77a3SMauro Carvalho Chehab 	struct list_head free;
162*ee4a77a3SMauro Carvalho Chehab 
163*ee4a77a3SMauro Carvalho Chehab 	spinlock_t lock;
164*ee4a77a3SMauro Carvalho Chehab 
165*ee4a77a3SMauro Carvalho Chehab 	struct vsp1_device *vsp1;
166*ee4a77a3SMauro Carvalho Chehab };
167*ee4a77a3SMauro Carvalho Chehab 
168*ee4a77a3SMauro Carvalho Chehab /**
169*ee4a77a3SMauro Carvalho Chehab  * struct vsp1_dl_list - Display list
170*ee4a77a3SMauro Carvalho Chehab  * @list: entry in the display list manager lists
171*ee4a77a3SMauro Carvalho Chehab  * @dlm: the display list manager
172*ee4a77a3SMauro Carvalho Chehab  * @header: display list header
173*ee4a77a3SMauro Carvalho Chehab  * @extension: extended display list header. NULL for normal lists
174*ee4a77a3SMauro Carvalho Chehab  * @dma: DMA address for the header
175*ee4a77a3SMauro Carvalho Chehab  * @body0: first display list body
176*ee4a77a3SMauro Carvalho Chehab  * @bodies: list of extra display list bodies
177*ee4a77a3SMauro Carvalho Chehab  * @pre_cmd: pre command to be issued through extended dl header
178*ee4a77a3SMauro Carvalho Chehab  * @post_cmd: post command to be issued through extended dl header
179*ee4a77a3SMauro Carvalho Chehab  * @has_chain: if true, indicates that there's a partition chain
180*ee4a77a3SMauro Carvalho Chehab  * @chain: entry in the display list partition chain
181*ee4a77a3SMauro Carvalho Chehab  * @flags: display list flags, a combination of VSP1_DL_FRAME_END_*
182*ee4a77a3SMauro Carvalho Chehab  */
183*ee4a77a3SMauro Carvalho Chehab struct vsp1_dl_list {
184*ee4a77a3SMauro Carvalho Chehab 	struct list_head list;
185*ee4a77a3SMauro Carvalho Chehab 	struct vsp1_dl_manager *dlm;
186*ee4a77a3SMauro Carvalho Chehab 
187*ee4a77a3SMauro Carvalho Chehab 	struct vsp1_dl_header *header;
188*ee4a77a3SMauro Carvalho Chehab 	struct vsp1_dl_ext_header *extension;
189*ee4a77a3SMauro Carvalho Chehab 	dma_addr_t dma;
190*ee4a77a3SMauro Carvalho Chehab 
191*ee4a77a3SMauro Carvalho Chehab 	struct vsp1_dl_body *body0;
192*ee4a77a3SMauro Carvalho Chehab 	struct list_head bodies;
193*ee4a77a3SMauro Carvalho Chehab 
194*ee4a77a3SMauro Carvalho Chehab 	struct vsp1_dl_ext_cmd *pre_cmd;
195*ee4a77a3SMauro Carvalho Chehab 	struct vsp1_dl_ext_cmd *post_cmd;
196*ee4a77a3SMauro Carvalho Chehab 
197*ee4a77a3SMauro Carvalho Chehab 	bool has_chain;
198*ee4a77a3SMauro Carvalho Chehab 	struct list_head chain;
199*ee4a77a3SMauro Carvalho Chehab 
200*ee4a77a3SMauro Carvalho Chehab 	unsigned int flags;
201*ee4a77a3SMauro Carvalho Chehab };
202*ee4a77a3SMauro Carvalho Chehab 
203*ee4a77a3SMauro Carvalho Chehab /**
204*ee4a77a3SMauro Carvalho Chehab  * struct vsp1_dl_manager - Display List manager
205*ee4a77a3SMauro Carvalho Chehab  * @index: index of the related WPF
206*ee4a77a3SMauro Carvalho Chehab  * @singleshot: execute the display list in single-shot mode
207*ee4a77a3SMauro Carvalho Chehab  * @vsp1: the VSP1 device
208*ee4a77a3SMauro Carvalho Chehab  * @lock: protects the free, active, queued, and pending lists
209*ee4a77a3SMauro Carvalho Chehab  * @free: array of all free display lists
210*ee4a77a3SMauro Carvalho Chehab  * @active: list currently being processed (loaded) by hardware
211*ee4a77a3SMauro Carvalho Chehab  * @queued: list queued to the hardware (written to the DL registers)
212*ee4a77a3SMauro Carvalho Chehab  * @pending: list waiting to be queued to the hardware
213*ee4a77a3SMauro Carvalho Chehab  * @pool: body pool for the display list bodies
214*ee4a77a3SMauro Carvalho Chehab  * @cmdpool: commands pool for extended display list
215*ee4a77a3SMauro Carvalho Chehab  */
216*ee4a77a3SMauro Carvalho Chehab struct vsp1_dl_manager {
217*ee4a77a3SMauro Carvalho Chehab 	unsigned int index;
218*ee4a77a3SMauro Carvalho Chehab 	bool singleshot;
219*ee4a77a3SMauro Carvalho Chehab 	struct vsp1_device *vsp1;
220*ee4a77a3SMauro Carvalho Chehab 
221*ee4a77a3SMauro Carvalho Chehab 	spinlock_t lock;
222*ee4a77a3SMauro Carvalho Chehab 	struct list_head free;
223*ee4a77a3SMauro Carvalho Chehab 	struct vsp1_dl_list *active;
224*ee4a77a3SMauro Carvalho Chehab 	struct vsp1_dl_list *queued;
225*ee4a77a3SMauro Carvalho Chehab 	struct vsp1_dl_list *pending;
226*ee4a77a3SMauro Carvalho Chehab 
227*ee4a77a3SMauro Carvalho Chehab 	struct vsp1_dl_body_pool *pool;
228*ee4a77a3SMauro Carvalho Chehab 	struct vsp1_dl_cmd_pool *cmdpool;
229*ee4a77a3SMauro Carvalho Chehab };
230*ee4a77a3SMauro Carvalho Chehab 
231*ee4a77a3SMauro Carvalho Chehab /* -----------------------------------------------------------------------------
232*ee4a77a3SMauro Carvalho Chehab  * Display List Body Management
233*ee4a77a3SMauro Carvalho Chehab  */
234*ee4a77a3SMauro Carvalho Chehab 
235*ee4a77a3SMauro Carvalho Chehab /**
236*ee4a77a3SMauro Carvalho Chehab  * vsp1_dl_body_pool_create - Create a pool of bodies from a single allocation
237*ee4a77a3SMauro Carvalho Chehab  * @vsp1: The VSP1 device
238*ee4a77a3SMauro Carvalho Chehab  * @num_bodies: The number of bodies to allocate
239*ee4a77a3SMauro Carvalho Chehab  * @num_entries: The maximum number of entries that a body can contain
240*ee4a77a3SMauro Carvalho Chehab  * @extra_size: Extra allocation provided for the bodies
241*ee4a77a3SMauro Carvalho Chehab  *
242*ee4a77a3SMauro Carvalho Chehab  * Allocate a pool of display list bodies each with enough memory to contain the
243*ee4a77a3SMauro Carvalho Chehab  * requested number of entries plus the @extra_size.
244*ee4a77a3SMauro Carvalho Chehab  *
245*ee4a77a3SMauro Carvalho Chehab  * Return a pointer to a pool on success or NULL if memory can't be allocated.
246*ee4a77a3SMauro Carvalho Chehab  */
247*ee4a77a3SMauro Carvalho Chehab struct vsp1_dl_body_pool *
vsp1_dl_body_pool_create(struct vsp1_device * vsp1,unsigned int num_bodies,unsigned int num_entries,size_t extra_size)248*ee4a77a3SMauro Carvalho Chehab vsp1_dl_body_pool_create(struct vsp1_device *vsp1, unsigned int num_bodies,
249*ee4a77a3SMauro Carvalho Chehab 			 unsigned int num_entries, size_t extra_size)
250*ee4a77a3SMauro Carvalho Chehab {
251*ee4a77a3SMauro Carvalho Chehab 	struct vsp1_dl_body_pool *pool;
252*ee4a77a3SMauro Carvalho Chehab 	size_t dlb_size;
253*ee4a77a3SMauro Carvalho Chehab 	unsigned int i;
254*ee4a77a3SMauro Carvalho Chehab 
255*ee4a77a3SMauro Carvalho Chehab 	pool = kzalloc(sizeof(*pool), GFP_KERNEL);
256*ee4a77a3SMauro Carvalho Chehab 	if (!pool)
257*ee4a77a3SMauro Carvalho Chehab 		return NULL;
258*ee4a77a3SMauro Carvalho Chehab 
259*ee4a77a3SMauro Carvalho Chehab 	pool->vsp1 = vsp1;
260*ee4a77a3SMauro Carvalho Chehab 
261*ee4a77a3SMauro Carvalho Chehab 	/*
262*ee4a77a3SMauro Carvalho Chehab 	 * TODO: 'extra_size' is only used by vsp1_dlm_create(), to allocate
263*ee4a77a3SMauro Carvalho Chehab 	 * extra memory for the display list header. We need only one header per
264*ee4a77a3SMauro Carvalho Chehab 	 * display list, not per display list body, thus this allocation is
265*ee4a77a3SMauro Carvalho Chehab 	 * extraneous and should be reworked in the future.
266*ee4a77a3SMauro Carvalho Chehab 	 */
267*ee4a77a3SMauro Carvalho Chehab 	dlb_size = num_entries * sizeof(struct vsp1_dl_entry) + extra_size;
268*ee4a77a3SMauro Carvalho Chehab 	pool->size = dlb_size * num_bodies;
269*ee4a77a3SMauro Carvalho Chehab 
270*ee4a77a3SMauro Carvalho Chehab 	pool->bodies = kcalloc(num_bodies, sizeof(*pool->bodies), GFP_KERNEL);
271*ee4a77a3SMauro Carvalho Chehab 	if (!pool->bodies) {
272*ee4a77a3SMauro Carvalho Chehab 		kfree(pool);
273*ee4a77a3SMauro Carvalho Chehab 		return NULL;
274*ee4a77a3SMauro Carvalho Chehab 	}
275*ee4a77a3SMauro Carvalho Chehab 
276*ee4a77a3SMauro Carvalho Chehab 	pool->mem = dma_alloc_wc(vsp1->bus_master, pool->size, &pool->dma,
277*ee4a77a3SMauro Carvalho Chehab 				 GFP_KERNEL);
278*ee4a77a3SMauro Carvalho Chehab 	if (!pool->mem) {
279*ee4a77a3SMauro Carvalho Chehab 		kfree(pool->bodies);
280*ee4a77a3SMauro Carvalho Chehab 		kfree(pool);
281*ee4a77a3SMauro Carvalho Chehab 		return NULL;
282*ee4a77a3SMauro Carvalho Chehab 	}
283*ee4a77a3SMauro Carvalho Chehab 
284*ee4a77a3SMauro Carvalho Chehab 	spin_lock_init(&pool->lock);
285*ee4a77a3SMauro Carvalho Chehab 	INIT_LIST_HEAD(&pool->free);
286*ee4a77a3SMauro Carvalho Chehab 
287*ee4a77a3SMauro Carvalho Chehab 	for (i = 0; i < num_bodies; ++i) {
288*ee4a77a3SMauro Carvalho Chehab 		struct vsp1_dl_body *dlb = &pool->bodies[i];
289*ee4a77a3SMauro Carvalho Chehab 
290*ee4a77a3SMauro Carvalho Chehab 		dlb->pool = pool;
291*ee4a77a3SMauro Carvalho Chehab 		dlb->max_entries = num_entries;
292*ee4a77a3SMauro Carvalho Chehab 
293*ee4a77a3SMauro Carvalho Chehab 		dlb->dma = pool->dma + i * dlb_size;
294*ee4a77a3SMauro Carvalho Chehab 		dlb->entries = pool->mem + i * dlb_size;
295*ee4a77a3SMauro Carvalho Chehab 
296*ee4a77a3SMauro Carvalho Chehab 		list_add_tail(&dlb->free, &pool->free);
297*ee4a77a3SMauro Carvalho Chehab 	}
298*ee4a77a3SMauro Carvalho Chehab 
299*ee4a77a3SMauro Carvalho Chehab 	return pool;
300*ee4a77a3SMauro Carvalho Chehab }
301*ee4a77a3SMauro Carvalho Chehab 
302*ee4a77a3SMauro Carvalho Chehab /**
303*ee4a77a3SMauro Carvalho Chehab  * vsp1_dl_body_pool_destroy - Release a body pool
304*ee4a77a3SMauro Carvalho Chehab  * @pool: The body pool
305*ee4a77a3SMauro Carvalho Chehab  *
306*ee4a77a3SMauro Carvalho Chehab  * Release all components of a pool allocation.
307*ee4a77a3SMauro Carvalho Chehab  */
vsp1_dl_body_pool_destroy(struct vsp1_dl_body_pool * pool)308*ee4a77a3SMauro Carvalho Chehab void vsp1_dl_body_pool_destroy(struct vsp1_dl_body_pool *pool)
309*ee4a77a3SMauro Carvalho Chehab {
310*ee4a77a3SMauro Carvalho Chehab 	if (!pool)
311*ee4a77a3SMauro Carvalho Chehab 		return;
312*ee4a77a3SMauro Carvalho Chehab 
313*ee4a77a3SMauro Carvalho Chehab 	if (pool->mem)
314*ee4a77a3SMauro Carvalho Chehab 		dma_free_wc(pool->vsp1->bus_master, pool->size, pool->mem,
315*ee4a77a3SMauro Carvalho Chehab 			    pool->dma);
316*ee4a77a3SMauro Carvalho Chehab 
317*ee4a77a3SMauro Carvalho Chehab 	kfree(pool->bodies);
318*ee4a77a3SMauro Carvalho Chehab 	kfree(pool);
319*ee4a77a3SMauro Carvalho Chehab }
320*ee4a77a3SMauro Carvalho Chehab 
321*ee4a77a3SMauro Carvalho Chehab /**
322*ee4a77a3SMauro Carvalho Chehab  * vsp1_dl_body_get - Obtain a body from a pool
323*ee4a77a3SMauro Carvalho Chehab  * @pool: The body pool
324*ee4a77a3SMauro Carvalho Chehab  *
325*ee4a77a3SMauro Carvalho Chehab  * Obtain a body from the pool without blocking.
326*ee4a77a3SMauro Carvalho Chehab  *
327*ee4a77a3SMauro Carvalho Chehab  * Returns a display list body or NULL if there are none available.
328*ee4a77a3SMauro Carvalho Chehab  */
vsp1_dl_body_get(struct vsp1_dl_body_pool * pool)329*ee4a77a3SMauro Carvalho Chehab struct vsp1_dl_body *vsp1_dl_body_get(struct vsp1_dl_body_pool *pool)
330*ee4a77a3SMauro Carvalho Chehab {
331*ee4a77a3SMauro Carvalho Chehab 	struct vsp1_dl_body *dlb = NULL;
332*ee4a77a3SMauro Carvalho Chehab 	unsigned long flags;
333*ee4a77a3SMauro Carvalho Chehab 
334*ee4a77a3SMauro Carvalho Chehab 	spin_lock_irqsave(&pool->lock, flags);
335*ee4a77a3SMauro Carvalho Chehab 
336*ee4a77a3SMauro Carvalho Chehab 	if (!list_empty(&pool->free)) {
337*ee4a77a3SMauro Carvalho Chehab 		dlb = list_first_entry(&pool->free, struct vsp1_dl_body, free);
338*ee4a77a3SMauro Carvalho Chehab 		list_del(&dlb->free);
339*ee4a77a3SMauro Carvalho Chehab 		refcount_set(&dlb->refcnt, 1);
340*ee4a77a3SMauro Carvalho Chehab 	}
341*ee4a77a3SMauro Carvalho Chehab 
342*ee4a77a3SMauro Carvalho Chehab 	spin_unlock_irqrestore(&pool->lock, flags);
343*ee4a77a3SMauro Carvalho Chehab 
344*ee4a77a3SMauro Carvalho Chehab 	return dlb;
345*ee4a77a3SMauro Carvalho Chehab }
346*ee4a77a3SMauro Carvalho Chehab 
347*ee4a77a3SMauro Carvalho Chehab /**
348*ee4a77a3SMauro Carvalho Chehab  * vsp1_dl_body_put - Return a body back to its pool
349*ee4a77a3SMauro Carvalho Chehab  * @dlb: The display list body
350*ee4a77a3SMauro Carvalho Chehab  *
351*ee4a77a3SMauro Carvalho Chehab  * Return a body back to the pool, and reset the num_entries to clear the list.
352*ee4a77a3SMauro Carvalho Chehab  */
vsp1_dl_body_put(struct vsp1_dl_body * dlb)353*ee4a77a3SMauro Carvalho Chehab void vsp1_dl_body_put(struct vsp1_dl_body *dlb)
354*ee4a77a3SMauro Carvalho Chehab {
355*ee4a77a3SMauro Carvalho Chehab 	unsigned long flags;
356*ee4a77a3SMauro Carvalho Chehab 
357*ee4a77a3SMauro Carvalho Chehab 	if (!dlb)
358*ee4a77a3SMauro Carvalho Chehab 		return;
359*ee4a77a3SMauro Carvalho Chehab 
360*ee4a77a3SMauro Carvalho Chehab 	if (!refcount_dec_and_test(&dlb->refcnt))
361*ee4a77a3SMauro Carvalho Chehab 		return;
362*ee4a77a3SMauro Carvalho Chehab 
363*ee4a77a3SMauro Carvalho Chehab 	dlb->num_entries = 0;
364*ee4a77a3SMauro Carvalho Chehab 
365*ee4a77a3SMauro Carvalho Chehab 	spin_lock_irqsave(&dlb->pool->lock, flags);
366*ee4a77a3SMauro Carvalho Chehab 	list_add_tail(&dlb->free, &dlb->pool->free);
367*ee4a77a3SMauro Carvalho Chehab 	spin_unlock_irqrestore(&dlb->pool->lock, flags);
368*ee4a77a3SMauro Carvalho Chehab }
369*ee4a77a3SMauro Carvalho Chehab 
370*ee4a77a3SMauro Carvalho Chehab /**
371*ee4a77a3SMauro Carvalho Chehab  * vsp1_dl_body_write - Write a register to a display list body
372*ee4a77a3SMauro Carvalho Chehab  * @dlb: The body
373*ee4a77a3SMauro Carvalho Chehab  * @reg: The register address
374*ee4a77a3SMauro Carvalho Chehab  * @data: The register value
375*ee4a77a3SMauro Carvalho Chehab  *
376*ee4a77a3SMauro Carvalho Chehab  * Write the given register and value to the display list body. The maximum
377*ee4a77a3SMauro Carvalho Chehab  * number of entries that can be written in a body is specified when the body is
378*ee4a77a3SMauro Carvalho Chehab  * allocated by vsp1_dl_body_alloc().
379*ee4a77a3SMauro Carvalho Chehab  */
vsp1_dl_body_write(struct vsp1_dl_body * dlb,u32 reg,u32 data)380*ee4a77a3SMauro Carvalho Chehab void vsp1_dl_body_write(struct vsp1_dl_body *dlb, u32 reg, u32 data)
381*ee4a77a3SMauro Carvalho Chehab {
382*ee4a77a3SMauro Carvalho Chehab 	if (WARN_ONCE(dlb->num_entries >= dlb->max_entries,
383*ee4a77a3SMauro Carvalho Chehab 		      "DLB size exceeded (max %u)", dlb->max_entries))
384*ee4a77a3SMauro Carvalho Chehab 		return;
385*ee4a77a3SMauro Carvalho Chehab 
386*ee4a77a3SMauro Carvalho Chehab 	dlb->entries[dlb->num_entries].addr = reg;
387*ee4a77a3SMauro Carvalho Chehab 	dlb->entries[dlb->num_entries].data = data;
388*ee4a77a3SMauro Carvalho Chehab 	dlb->num_entries++;
389*ee4a77a3SMauro Carvalho Chehab }
390*ee4a77a3SMauro Carvalho Chehab 
391*ee4a77a3SMauro Carvalho Chehab /* -----------------------------------------------------------------------------
392*ee4a77a3SMauro Carvalho Chehab  * Display List Extended Command Management
393*ee4a77a3SMauro Carvalho Chehab  */
394*ee4a77a3SMauro Carvalho Chehab 
395*ee4a77a3SMauro Carvalho Chehab enum vsp1_extcmd_type {
396*ee4a77a3SMauro Carvalho Chehab 	VSP1_EXTCMD_AUTODISP,
397*ee4a77a3SMauro Carvalho Chehab 	VSP1_EXTCMD_AUTOFLD,
398*ee4a77a3SMauro Carvalho Chehab };
399*ee4a77a3SMauro Carvalho Chehab 
400*ee4a77a3SMauro Carvalho Chehab struct vsp1_extended_command_info {
401*ee4a77a3SMauro Carvalho Chehab 	u16 opcode;
402*ee4a77a3SMauro Carvalho Chehab 	size_t body_size;
403*ee4a77a3SMauro Carvalho Chehab };
404*ee4a77a3SMauro Carvalho Chehab 
405*ee4a77a3SMauro Carvalho Chehab static const struct vsp1_extended_command_info vsp1_extended_commands[] = {
406*ee4a77a3SMauro Carvalho Chehab 	[VSP1_EXTCMD_AUTODISP] = { 0x02, 96 },
407*ee4a77a3SMauro Carvalho Chehab 	[VSP1_EXTCMD_AUTOFLD]  = { 0x03, 160 },
408*ee4a77a3SMauro Carvalho Chehab };
409*ee4a77a3SMauro Carvalho Chehab 
410*ee4a77a3SMauro Carvalho Chehab /**
411*ee4a77a3SMauro Carvalho Chehab  * vsp1_dl_cmd_pool_create - Create a pool of commands from a single allocation
412*ee4a77a3SMauro Carvalho Chehab  * @vsp1: The VSP1 device
413*ee4a77a3SMauro Carvalho Chehab  * @type: The command pool type
414*ee4a77a3SMauro Carvalho Chehab  * @num_cmds: The number of commands to allocate
415*ee4a77a3SMauro Carvalho Chehab  *
416*ee4a77a3SMauro Carvalho Chehab  * Allocate a pool of commands each with enough memory to contain the private
417*ee4a77a3SMauro Carvalho Chehab  * data of each command. The allocation sizes are dependent upon the command
418*ee4a77a3SMauro Carvalho Chehab  * type.
419*ee4a77a3SMauro Carvalho Chehab  *
420*ee4a77a3SMauro Carvalho Chehab  * Return a pointer to the pool on success or NULL if memory can't be allocated.
421*ee4a77a3SMauro Carvalho Chehab  */
422*ee4a77a3SMauro Carvalho Chehab static struct vsp1_dl_cmd_pool *
vsp1_dl_cmd_pool_create(struct vsp1_device * vsp1,enum vsp1_extcmd_type type,unsigned int num_cmds)423*ee4a77a3SMauro Carvalho Chehab vsp1_dl_cmd_pool_create(struct vsp1_device *vsp1, enum vsp1_extcmd_type type,
424*ee4a77a3SMauro Carvalho Chehab 			unsigned int num_cmds)
425*ee4a77a3SMauro Carvalho Chehab {
426*ee4a77a3SMauro Carvalho Chehab 	struct vsp1_dl_cmd_pool *pool;
427*ee4a77a3SMauro Carvalho Chehab 	unsigned int i;
428*ee4a77a3SMauro Carvalho Chehab 	size_t cmd_size;
429*ee4a77a3SMauro Carvalho Chehab 
430*ee4a77a3SMauro Carvalho Chehab 	pool = kzalloc(sizeof(*pool), GFP_KERNEL);
431*ee4a77a3SMauro Carvalho Chehab 	if (!pool)
432*ee4a77a3SMauro Carvalho Chehab 		return NULL;
433*ee4a77a3SMauro Carvalho Chehab 
434*ee4a77a3SMauro Carvalho Chehab 	pool->vsp1 = vsp1;
435*ee4a77a3SMauro Carvalho Chehab 
436*ee4a77a3SMauro Carvalho Chehab 	spin_lock_init(&pool->lock);
437*ee4a77a3SMauro Carvalho Chehab 	INIT_LIST_HEAD(&pool->free);
438*ee4a77a3SMauro Carvalho Chehab 
439*ee4a77a3SMauro Carvalho Chehab 	pool->cmds = kcalloc(num_cmds, sizeof(*pool->cmds), GFP_KERNEL);
440*ee4a77a3SMauro Carvalho Chehab 	if (!pool->cmds) {
441*ee4a77a3SMauro Carvalho Chehab 		kfree(pool);
442*ee4a77a3SMauro Carvalho Chehab 		return NULL;
443*ee4a77a3SMauro Carvalho Chehab 	}
444*ee4a77a3SMauro Carvalho Chehab 
445*ee4a77a3SMauro Carvalho Chehab 	cmd_size = sizeof(struct vsp1_pre_ext_dl_body) +
446*ee4a77a3SMauro Carvalho Chehab 		   vsp1_extended_commands[type].body_size;
447*ee4a77a3SMauro Carvalho Chehab 	cmd_size = ALIGN(cmd_size, 16);
448*ee4a77a3SMauro Carvalho Chehab 
449*ee4a77a3SMauro Carvalho Chehab 	pool->size = cmd_size * num_cmds;
450*ee4a77a3SMauro Carvalho Chehab 	pool->mem = dma_alloc_wc(vsp1->bus_master, pool->size, &pool->dma,
451*ee4a77a3SMauro Carvalho Chehab 				 GFP_KERNEL);
452*ee4a77a3SMauro Carvalho Chehab 	if (!pool->mem) {
453*ee4a77a3SMauro Carvalho Chehab 		kfree(pool->cmds);
454*ee4a77a3SMauro Carvalho Chehab 		kfree(pool);
455*ee4a77a3SMauro Carvalho Chehab 		return NULL;
456*ee4a77a3SMauro Carvalho Chehab 	}
457*ee4a77a3SMauro Carvalho Chehab 
458*ee4a77a3SMauro Carvalho Chehab 	for (i = 0; i < num_cmds; ++i) {
459*ee4a77a3SMauro Carvalho Chehab 		struct vsp1_dl_ext_cmd *cmd = &pool->cmds[i];
460*ee4a77a3SMauro Carvalho Chehab 		size_t cmd_offset = i * cmd_size;
461*ee4a77a3SMauro Carvalho Chehab 		/* data_offset must be 16 byte aligned for DMA. */
462*ee4a77a3SMauro Carvalho Chehab 		size_t data_offset = sizeof(struct vsp1_pre_ext_dl_body) +
463*ee4a77a3SMauro Carvalho Chehab 				     cmd_offset;
464*ee4a77a3SMauro Carvalho Chehab 
465*ee4a77a3SMauro Carvalho Chehab 		cmd->pool = pool;
466*ee4a77a3SMauro Carvalho Chehab 		cmd->opcode = vsp1_extended_commands[type].opcode;
467*ee4a77a3SMauro Carvalho Chehab 
468*ee4a77a3SMauro Carvalho Chehab 		/*
469*ee4a77a3SMauro Carvalho Chehab 		 * TODO: Auto-disp can utilise more than one extended body
470*ee4a77a3SMauro Carvalho Chehab 		 * command per cmd.
471*ee4a77a3SMauro Carvalho Chehab 		 */
472*ee4a77a3SMauro Carvalho Chehab 		cmd->num_cmds = 1;
473*ee4a77a3SMauro Carvalho Chehab 		cmd->cmds = pool->mem + cmd_offset;
474*ee4a77a3SMauro Carvalho Chehab 		cmd->cmd_dma = pool->dma + cmd_offset;
475*ee4a77a3SMauro Carvalho Chehab 
476*ee4a77a3SMauro Carvalho Chehab 		cmd->data = pool->mem + data_offset;
477*ee4a77a3SMauro Carvalho Chehab 		cmd->data_dma = pool->dma + data_offset;
478*ee4a77a3SMauro Carvalho Chehab 
479*ee4a77a3SMauro Carvalho Chehab 		list_add_tail(&cmd->free, &pool->free);
480*ee4a77a3SMauro Carvalho Chehab 	}
481*ee4a77a3SMauro Carvalho Chehab 
482*ee4a77a3SMauro Carvalho Chehab 	return pool;
483*ee4a77a3SMauro Carvalho Chehab }
484*ee4a77a3SMauro Carvalho Chehab 
485*ee4a77a3SMauro Carvalho Chehab static
vsp1_dl_ext_cmd_get(struct vsp1_dl_cmd_pool * pool)486*ee4a77a3SMauro Carvalho Chehab struct vsp1_dl_ext_cmd *vsp1_dl_ext_cmd_get(struct vsp1_dl_cmd_pool *pool)
487*ee4a77a3SMauro Carvalho Chehab {
488*ee4a77a3SMauro Carvalho Chehab 	struct vsp1_dl_ext_cmd *cmd = NULL;
489*ee4a77a3SMauro Carvalho Chehab 	unsigned long flags;
490*ee4a77a3SMauro Carvalho Chehab 
491*ee4a77a3SMauro Carvalho Chehab 	spin_lock_irqsave(&pool->lock, flags);
492*ee4a77a3SMauro Carvalho Chehab 
493*ee4a77a3SMauro Carvalho Chehab 	if (!list_empty(&pool->free)) {
494*ee4a77a3SMauro Carvalho Chehab 		cmd = list_first_entry(&pool->free, struct vsp1_dl_ext_cmd,
495*ee4a77a3SMauro Carvalho Chehab 				       free);
496*ee4a77a3SMauro Carvalho Chehab 		list_del(&cmd->free);
497*ee4a77a3SMauro Carvalho Chehab 	}
498*ee4a77a3SMauro Carvalho Chehab 
499*ee4a77a3SMauro Carvalho Chehab 	spin_unlock_irqrestore(&pool->lock, flags);
500*ee4a77a3SMauro Carvalho Chehab 
501*ee4a77a3SMauro Carvalho Chehab 	return cmd;
502*ee4a77a3SMauro Carvalho Chehab }
503*ee4a77a3SMauro Carvalho Chehab 
vsp1_dl_ext_cmd_put(struct vsp1_dl_ext_cmd * cmd)504*ee4a77a3SMauro Carvalho Chehab static void vsp1_dl_ext_cmd_put(struct vsp1_dl_ext_cmd *cmd)
505*ee4a77a3SMauro Carvalho Chehab {
506*ee4a77a3SMauro Carvalho Chehab 	unsigned long flags;
507*ee4a77a3SMauro Carvalho Chehab 
508*ee4a77a3SMauro Carvalho Chehab 	if (!cmd)
509*ee4a77a3SMauro Carvalho Chehab 		return;
510*ee4a77a3SMauro Carvalho Chehab 
511*ee4a77a3SMauro Carvalho Chehab 	/* Reset flags, these mark data usage. */
512*ee4a77a3SMauro Carvalho Chehab 	cmd->flags = 0;
513*ee4a77a3SMauro Carvalho Chehab 
514*ee4a77a3SMauro Carvalho Chehab 	spin_lock_irqsave(&cmd->pool->lock, flags);
515*ee4a77a3SMauro Carvalho Chehab 	list_add_tail(&cmd->free, &cmd->pool->free);
516*ee4a77a3SMauro Carvalho Chehab 	spin_unlock_irqrestore(&cmd->pool->lock, flags);
517*ee4a77a3SMauro Carvalho Chehab }
518*ee4a77a3SMauro Carvalho Chehab 
vsp1_dl_ext_cmd_pool_destroy(struct vsp1_dl_cmd_pool * pool)519*ee4a77a3SMauro Carvalho Chehab static void vsp1_dl_ext_cmd_pool_destroy(struct vsp1_dl_cmd_pool *pool)
520*ee4a77a3SMauro Carvalho Chehab {
521*ee4a77a3SMauro Carvalho Chehab 	if (!pool)
522*ee4a77a3SMauro Carvalho Chehab 		return;
523*ee4a77a3SMauro Carvalho Chehab 
524*ee4a77a3SMauro Carvalho Chehab 	if (pool->mem)
525*ee4a77a3SMauro Carvalho Chehab 		dma_free_wc(pool->vsp1->bus_master, pool->size, pool->mem,
526*ee4a77a3SMauro Carvalho Chehab 			    pool->dma);
527*ee4a77a3SMauro Carvalho Chehab 
528*ee4a77a3SMauro Carvalho Chehab 	kfree(pool->cmds);
529*ee4a77a3SMauro Carvalho Chehab 	kfree(pool);
530*ee4a77a3SMauro Carvalho Chehab }
531*ee4a77a3SMauro Carvalho Chehab 
vsp1_dl_get_pre_cmd(struct vsp1_dl_list * dl)532*ee4a77a3SMauro Carvalho Chehab struct vsp1_dl_ext_cmd *vsp1_dl_get_pre_cmd(struct vsp1_dl_list *dl)
533*ee4a77a3SMauro Carvalho Chehab {
534*ee4a77a3SMauro Carvalho Chehab 	struct vsp1_dl_manager *dlm = dl->dlm;
535*ee4a77a3SMauro Carvalho Chehab 
536*ee4a77a3SMauro Carvalho Chehab 	if (dl->pre_cmd)
537*ee4a77a3SMauro Carvalho Chehab 		return dl->pre_cmd;
538*ee4a77a3SMauro Carvalho Chehab 
539*ee4a77a3SMauro Carvalho Chehab 	dl->pre_cmd = vsp1_dl_ext_cmd_get(dlm->cmdpool);
540*ee4a77a3SMauro Carvalho Chehab 
541*ee4a77a3SMauro Carvalho Chehab 	return dl->pre_cmd;
542*ee4a77a3SMauro Carvalho Chehab }
543*ee4a77a3SMauro Carvalho Chehab 
544*ee4a77a3SMauro Carvalho Chehab /* ----------------------------------------------------------------------------
545*ee4a77a3SMauro Carvalho Chehab  * Display List Transaction Management
546*ee4a77a3SMauro Carvalho Chehab  */
547*ee4a77a3SMauro Carvalho Chehab 
vsp1_dl_list_alloc(struct vsp1_dl_manager * dlm)548*ee4a77a3SMauro Carvalho Chehab static struct vsp1_dl_list *vsp1_dl_list_alloc(struct vsp1_dl_manager *dlm)
549*ee4a77a3SMauro Carvalho Chehab {
550*ee4a77a3SMauro Carvalho Chehab 	struct vsp1_dl_list *dl;
551*ee4a77a3SMauro Carvalho Chehab 	size_t header_offset;
552*ee4a77a3SMauro Carvalho Chehab 
553*ee4a77a3SMauro Carvalho Chehab 	dl = kzalloc(sizeof(*dl), GFP_KERNEL);
554*ee4a77a3SMauro Carvalho Chehab 	if (!dl)
555*ee4a77a3SMauro Carvalho Chehab 		return NULL;
556*ee4a77a3SMauro Carvalho Chehab 
557*ee4a77a3SMauro Carvalho Chehab 	INIT_LIST_HEAD(&dl->bodies);
558*ee4a77a3SMauro Carvalho Chehab 	dl->dlm = dlm;
559*ee4a77a3SMauro Carvalho Chehab 
560*ee4a77a3SMauro Carvalho Chehab 	/* Get a default body for our list. */
561*ee4a77a3SMauro Carvalho Chehab 	dl->body0 = vsp1_dl_body_get(dlm->pool);
562*ee4a77a3SMauro Carvalho Chehab 	if (!dl->body0) {
563*ee4a77a3SMauro Carvalho Chehab 		kfree(dl);
564*ee4a77a3SMauro Carvalho Chehab 		return NULL;
565*ee4a77a3SMauro Carvalho Chehab 	}
566*ee4a77a3SMauro Carvalho Chehab 
567*ee4a77a3SMauro Carvalho Chehab 	header_offset = dl->body0->max_entries * sizeof(*dl->body0->entries);
568*ee4a77a3SMauro Carvalho Chehab 
569*ee4a77a3SMauro Carvalho Chehab 	dl->header = ((void *)dl->body0->entries) + header_offset;
570*ee4a77a3SMauro Carvalho Chehab 	dl->dma = dl->body0->dma + header_offset;
571*ee4a77a3SMauro Carvalho Chehab 
572*ee4a77a3SMauro Carvalho Chehab 	memset(dl->header, 0, sizeof(*dl->header));
573*ee4a77a3SMauro Carvalho Chehab 	dl->header->lists[0].addr = dl->body0->dma;
574*ee4a77a3SMauro Carvalho Chehab 
575*ee4a77a3SMauro Carvalho Chehab 	return dl;
576*ee4a77a3SMauro Carvalho Chehab }
577*ee4a77a3SMauro Carvalho Chehab 
vsp1_dl_list_bodies_put(struct vsp1_dl_list * dl)578*ee4a77a3SMauro Carvalho Chehab static void vsp1_dl_list_bodies_put(struct vsp1_dl_list *dl)
579*ee4a77a3SMauro Carvalho Chehab {
580*ee4a77a3SMauro Carvalho Chehab 	struct vsp1_dl_body *dlb, *tmp;
581*ee4a77a3SMauro Carvalho Chehab 
582*ee4a77a3SMauro Carvalho Chehab 	list_for_each_entry_safe(dlb, tmp, &dl->bodies, list) {
583*ee4a77a3SMauro Carvalho Chehab 		list_del(&dlb->list);
584*ee4a77a3SMauro Carvalho Chehab 		vsp1_dl_body_put(dlb);
585*ee4a77a3SMauro Carvalho Chehab 	}
586*ee4a77a3SMauro Carvalho Chehab }
587*ee4a77a3SMauro Carvalho Chehab 
vsp1_dl_list_free(struct vsp1_dl_list * dl)588*ee4a77a3SMauro Carvalho Chehab static void vsp1_dl_list_free(struct vsp1_dl_list *dl)
589*ee4a77a3SMauro Carvalho Chehab {
590*ee4a77a3SMauro Carvalho Chehab 	vsp1_dl_body_put(dl->body0);
591*ee4a77a3SMauro Carvalho Chehab 	vsp1_dl_list_bodies_put(dl);
592*ee4a77a3SMauro Carvalho Chehab 
593*ee4a77a3SMauro Carvalho Chehab 	kfree(dl);
594*ee4a77a3SMauro Carvalho Chehab }
595*ee4a77a3SMauro Carvalho Chehab 
596*ee4a77a3SMauro Carvalho Chehab /**
597*ee4a77a3SMauro Carvalho Chehab  * vsp1_dl_list_get - Get a free display list
598*ee4a77a3SMauro Carvalho Chehab  * @dlm: The display list manager
599*ee4a77a3SMauro Carvalho Chehab  *
600*ee4a77a3SMauro Carvalho Chehab  * Get a display list from the pool of free lists and return it.
601*ee4a77a3SMauro Carvalho Chehab  *
602*ee4a77a3SMauro Carvalho Chehab  * This function must be called without the display list manager lock held.
603*ee4a77a3SMauro Carvalho Chehab  */
vsp1_dl_list_get(struct vsp1_dl_manager * dlm)604*ee4a77a3SMauro Carvalho Chehab struct vsp1_dl_list *vsp1_dl_list_get(struct vsp1_dl_manager *dlm)
605*ee4a77a3SMauro Carvalho Chehab {
606*ee4a77a3SMauro Carvalho Chehab 	struct vsp1_dl_list *dl = NULL;
607*ee4a77a3SMauro Carvalho Chehab 	unsigned long flags;
608*ee4a77a3SMauro Carvalho Chehab 
609*ee4a77a3SMauro Carvalho Chehab 	spin_lock_irqsave(&dlm->lock, flags);
610*ee4a77a3SMauro Carvalho Chehab 
611*ee4a77a3SMauro Carvalho Chehab 	if (!list_empty(&dlm->free)) {
612*ee4a77a3SMauro Carvalho Chehab 		dl = list_first_entry(&dlm->free, struct vsp1_dl_list, list);
613*ee4a77a3SMauro Carvalho Chehab 		list_del(&dl->list);
614*ee4a77a3SMauro Carvalho Chehab 
615*ee4a77a3SMauro Carvalho Chehab 		/*
616*ee4a77a3SMauro Carvalho Chehab 		 * The display list chain must be initialised to ensure every
617*ee4a77a3SMauro Carvalho Chehab 		 * display list can assert list_empty() if it is not in a chain.
618*ee4a77a3SMauro Carvalho Chehab 		 */
619*ee4a77a3SMauro Carvalho Chehab 		INIT_LIST_HEAD(&dl->chain);
620*ee4a77a3SMauro Carvalho Chehab 	}
621*ee4a77a3SMauro Carvalho Chehab 
622*ee4a77a3SMauro Carvalho Chehab 	spin_unlock_irqrestore(&dlm->lock, flags);
623*ee4a77a3SMauro Carvalho Chehab 
624*ee4a77a3SMauro Carvalho Chehab 	return dl;
625*ee4a77a3SMauro Carvalho Chehab }
626*ee4a77a3SMauro Carvalho Chehab 
627*ee4a77a3SMauro Carvalho Chehab /* This function must be called with the display list manager lock held.*/
__vsp1_dl_list_put(struct vsp1_dl_list * dl)628*ee4a77a3SMauro Carvalho Chehab static void __vsp1_dl_list_put(struct vsp1_dl_list *dl)
629*ee4a77a3SMauro Carvalho Chehab {
630*ee4a77a3SMauro Carvalho Chehab 	struct vsp1_dl_list *dl_next;
631*ee4a77a3SMauro Carvalho Chehab 
632*ee4a77a3SMauro Carvalho Chehab 	if (!dl)
633*ee4a77a3SMauro Carvalho Chehab 		return;
634*ee4a77a3SMauro Carvalho Chehab 
635*ee4a77a3SMauro Carvalho Chehab 	/*
636*ee4a77a3SMauro Carvalho Chehab 	 * Release any linked display-lists which were chained for a single
637*ee4a77a3SMauro Carvalho Chehab 	 * hardware operation.
638*ee4a77a3SMauro Carvalho Chehab 	 */
639*ee4a77a3SMauro Carvalho Chehab 	if (dl->has_chain) {
640*ee4a77a3SMauro Carvalho Chehab 		list_for_each_entry(dl_next, &dl->chain, chain)
641*ee4a77a3SMauro Carvalho Chehab 			__vsp1_dl_list_put(dl_next);
642*ee4a77a3SMauro Carvalho Chehab 	}
643*ee4a77a3SMauro Carvalho Chehab 
644*ee4a77a3SMauro Carvalho Chehab 	dl->has_chain = false;
645*ee4a77a3SMauro Carvalho Chehab 
646*ee4a77a3SMauro Carvalho Chehab 	vsp1_dl_list_bodies_put(dl);
647*ee4a77a3SMauro Carvalho Chehab 
648*ee4a77a3SMauro Carvalho Chehab 	vsp1_dl_ext_cmd_put(dl->pre_cmd);
649*ee4a77a3SMauro Carvalho Chehab 	vsp1_dl_ext_cmd_put(dl->post_cmd);
650*ee4a77a3SMauro Carvalho Chehab 
651*ee4a77a3SMauro Carvalho Chehab 	dl->pre_cmd = NULL;
652*ee4a77a3SMauro Carvalho Chehab 	dl->post_cmd = NULL;
653*ee4a77a3SMauro Carvalho Chehab 
654*ee4a77a3SMauro Carvalho Chehab 	/*
655*ee4a77a3SMauro Carvalho Chehab 	 * body0 is reused as as an optimisation as presently every display list
656*ee4a77a3SMauro Carvalho Chehab 	 * has at least one body, thus we reinitialise the entries list.
657*ee4a77a3SMauro Carvalho Chehab 	 */
658*ee4a77a3SMauro Carvalho Chehab 	dl->body0->num_entries = 0;
659*ee4a77a3SMauro Carvalho Chehab 
660*ee4a77a3SMauro Carvalho Chehab 	list_add_tail(&dl->list, &dl->dlm->free);
661*ee4a77a3SMauro Carvalho Chehab }
662*ee4a77a3SMauro Carvalho Chehab 
663*ee4a77a3SMauro Carvalho Chehab /**
664*ee4a77a3SMauro Carvalho Chehab  * vsp1_dl_list_put - Release a display list
665*ee4a77a3SMauro Carvalho Chehab  * @dl: The display list
666*ee4a77a3SMauro Carvalho Chehab  *
667*ee4a77a3SMauro Carvalho Chehab  * Release the display list and return it to the pool of free lists.
668*ee4a77a3SMauro Carvalho Chehab  *
669*ee4a77a3SMauro Carvalho Chehab  * Passing a NULL pointer to this function is safe, in that case no operation
670*ee4a77a3SMauro Carvalho Chehab  * will be performed.
671*ee4a77a3SMauro Carvalho Chehab  */
vsp1_dl_list_put(struct vsp1_dl_list * dl)672*ee4a77a3SMauro Carvalho Chehab void vsp1_dl_list_put(struct vsp1_dl_list *dl)
673*ee4a77a3SMauro Carvalho Chehab {
674*ee4a77a3SMauro Carvalho Chehab 	unsigned long flags;
675*ee4a77a3SMauro Carvalho Chehab 
676*ee4a77a3SMauro Carvalho Chehab 	if (!dl)
677*ee4a77a3SMauro Carvalho Chehab 		return;
678*ee4a77a3SMauro Carvalho Chehab 
679*ee4a77a3SMauro Carvalho Chehab 	spin_lock_irqsave(&dl->dlm->lock, flags);
680*ee4a77a3SMauro Carvalho Chehab 	__vsp1_dl_list_put(dl);
681*ee4a77a3SMauro Carvalho Chehab 	spin_unlock_irqrestore(&dl->dlm->lock, flags);
682*ee4a77a3SMauro Carvalho Chehab }
683*ee4a77a3SMauro Carvalho Chehab 
684*ee4a77a3SMauro Carvalho Chehab /**
685*ee4a77a3SMauro Carvalho Chehab  * vsp1_dl_list_get_body0 - Obtain the default body for the display list
686*ee4a77a3SMauro Carvalho Chehab  * @dl: The display list
687*ee4a77a3SMauro Carvalho Chehab  *
688*ee4a77a3SMauro Carvalho Chehab  * Obtain a pointer to the internal display list body allowing this to be passed
689*ee4a77a3SMauro Carvalho Chehab  * directly to configure operations.
690*ee4a77a3SMauro Carvalho Chehab  */
vsp1_dl_list_get_body0(struct vsp1_dl_list * dl)691*ee4a77a3SMauro Carvalho Chehab struct vsp1_dl_body *vsp1_dl_list_get_body0(struct vsp1_dl_list *dl)
692*ee4a77a3SMauro Carvalho Chehab {
693*ee4a77a3SMauro Carvalho Chehab 	return dl->body0;
694*ee4a77a3SMauro Carvalho Chehab }
695*ee4a77a3SMauro Carvalho Chehab 
696*ee4a77a3SMauro Carvalho Chehab /**
697*ee4a77a3SMauro Carvalho Chehab  * vsp1_dl_list_add_body - Add a body to the display list
698*ee4a77a3SMauro Carvalho Chehab  * @dl: The display list
699*ee4a77a3SMauro Carvalho Chehab  * @dlb: The body
700*ee4a77a3SMauro Carvalho Chehab  *
701*ee4a77a3SMauro Carvalho Chehab  * Add a display list body to a display list. Registers contained in bodies are
702*ee4a77a3SMauro Carvalho Chehab  * processed after registers contained in the main display list, in the order in
703*ee4a77a3SMauro Carvalho Chehab  * which bodies are added.
704*ee4a77a3SMauro Carvalho Chehab  *
705*ee4a77a3SMauro Carvalho Chehab  * Adding a body to a display list passes ownership of the body to the list. The
706*ee4a77a3SMauro Carvalho Chehab  * caller retains its reference to the body when adding it to the display list,
707*ee4a77a3SMauro Carvalho Chehab  * but is not allowed to add new entries to the body.
708*ee4a77a3SMauro Carvalho Chehab  *
709*ee4a77a3SMauro Carvalho Chehab  * The reference must be explicitly released by a call to vsp1_dl_body_put()
710*ee4a77a3SMauro Carvalho Chehab  * when the body isn't needed anymore.
711*ee4a77a3SMauro Carvalho Chehab  */
vsp1_dl_list_add_body(struct vsp1_dl_list * dl,struct vsp1_dl_body * dlb)712*ee4a77a3SMauro Carvalho Chehab int vsp1_dl_list_add_body(struct vsp1_dl_list *dl, struct vsp1_dl_body *dlb)
713*ee4a77a3SMauro Carvalho Chehab {
714*ee4a77a3SMauro Carvalho Chehab 	refcount_inc(&dlb->refcnt);
715*ee4a77a3SMauro Carvalho Chehab 
716*ee4a77a3SMauro Carvalho Chehab 	list_add_tail(&dlb->list, &dl->bodies);
717*ee4a77a3SMauro Carvalho Chehab 
718*ee4a77a3SMauro Carvalho Chehab 	return 0;
719*ee4a77a3SMauro Carvalho Chehab }
720*ee4a77a3SMauro Carvalho Chehab 
721*ee4a77a3SMauro Carvalho Chehab /**
722*ee4a77a3SMauro Carvalho Chehab  * vsp1_dl_list_add_chain - Add a display list to a chain
723*ee4a77a3SMauro Carvalho Chehab  * @head: The head display list
724*ee4a77a3SMauro Carvalho Chehab  * @dl: The new display list
725*ee4a77a3SMauro Carvalho Chehab  *
726*ee4a77a3SMauro Carvalho Chehab  * Add a display list to an existing display list chain. The chained lists
727*ee4a77a3SMauro Carvalho Chehab  * will be automatically processed by the hardware without intervention from
728*ee4a77a3SMauro Carvalho Chehab  * the CPU. A display list end interrupt will only complete after the last
729*ee4a77a3SMauro Carvalho Chehab  * display list in the chain has completed processing.
730*ee4a77a3SMauro Carvalho Chehab  *
731*ee4a77a3SMauro Carvalho Chehab  * Adding a display list to a chain passes ownership of the display list to
732*ee4a77a3SMauro Carvalho Chehab  * the head display list item. The chain is released when the head dl item is
733*ee4a77a3SMauro Carvalho Chehab  * put back with __vsp1_dl_list_put().
734*ee4a77a3SMauro Carvalho Chehab  */
vsp1_dl_list_add_chain(struct vsp1_dl_list * head,struct vsp1_dl_list * dl)735*ee4a77a3SMauro Carvalho Chehab int vsp1_dl_list_add_chain(struct vsp1_dl_list *head,
736*ee4a77a3SMauro Carvalho Chehab 			   struct vsp1_dl_list *dl)
737*ee4a77a3SMauro Carvalho Chehab {
738*ee4a77a3SMauro Carvalho Chehab 	head->has_chain = true;
739*ee4a77a3SMauro Carvalho Chehab 	list_add_tail(&dl->chain, &head->chain);
740*ee4a77a3SMauro Carvalho Chehab 	return 0;
741*ee4a77a3SMauro Carvalho Chehab }
742*ee4a77a3SMauro Carvalho Chehab 
vsp1_dl_ext_cmd_fill_header(struct vsp1_dl_ext_cmd * cmd)743*ee4a77a3SMauro Carvalho Chehab static void vsp1_dl_ext_cmd_fill_header(struct vsp1_dl_ext_cmd *cmd)
744*ee4a77a3SMauro Carvalho Chehab {
745*ee4a77a3SMauro Carvalho Chehab 	cmd->cmds[0].opcode = cmd->opcode;
746*ee4a77a3SMauro Carvalho Chehab 	cmd->cmds[0].flags = cmd->flags;
747*ee4a77a3SMauro Carvalho Chehab 	cmd->cmds[0].address_set = cmd->data_dma;
748*ee4a77a3SMauro Carvalho Chehab 	cmd->cmds[0].reserved = 0;
749*ee4a77a3SMauro Carvalho Chehab }
750*ee4a77a3SMauro Carvalho Chehab 
vsp1_dl_list_fill_header(struct vsp1_dl_list * dl,bool is_last)751*ee4a77a3SMauro Carvalho Chehab static void vsp1_dl_list_fill_header(struct vsp1_dl_list *dl, bool is_last)
752*ee4a77a3SMauro Carvalho Chehab {
753*ee4a77a3SMauro Carvalho Chehab 	struct vsp1_dl_manager *dlm = dl->dlm;
754*ee4a77a3SMauro Carvalho Chehab 	struct vsp1_dl_header_list *hdr = dl->header->lists;
755*ee4a77a3SMauro Carvalho Chehab 	struct vsp1_dl_body *dlb;
756*ee4a77a3SMauro Carvalho Chehab 	unsigned int num_lists = 0;
757*ee4a77a3SMauro Carvalho Chehab 
758*ee4a77a3SMauro Carvalho Chehab 	/*
759*ee4a77a3SMauro Carvalho Chehab 	 * Fill the header with the display list bodies addresses and sizes. The
760*ee4a77a3SMauro Carvalho Chehab 	 * address of the first body has already been filled when the display
761*ee4a77a3SMauro Carvalho Chehab 	 * list was allocated.
762*ee4a77a3SMauro Carvalho Chehab 	 */
763*ee4a77a3SMauro Carvalho Chehab 
764*ee4a77a3SMauro Carvalho Chehab 	hdr->num_bytes = dl->body0->num_entries
765*ee4a77a3SMauro Carvalho Chehab 		       * sizeof(*dl->header->lists);
766*ee4a77a3SMauro Carvalho Chehab 
767*ee4a77a3SMauro Carvalho Chehab 	list_for_each_entry(dlb, &dl->bodies, list) {
768*ee4a77a3SMauro Carvalho Chehab 		num_lists++;
769*ee4a77a3SMauro Carvalho Chehab 		hdr++;
770*ee4a77a3SMauro Carvalho Chehab 
771*ee4a77a3SMauro Carvalho Chehab 		hdr->addr = dlb->dma;
772*ee4a77a3SMauro Carvalho Chehab 		hdr->num_bytes = dlb->num_entries
773*ee4a77a3SMauro Carvalho Chehab 			       * sizeof(*dl->header->lists);
774*ee4a77a3SMauro Carvalho Chehab 	}
775*ee4a77a3SMauro Carvalho Chehab 
776*ee4a77a3SMauro Carvalho Chehab 	dl->header->num_lists = num_lists;
777*ee4a77a3SMauro Carvalho Chehab 	dl->header->flags = 0;
778*ee4a77a3SMauro Carvalho Chehab 
779*ee4a77a3SMauro Carvalho Chehab 	/*
780*ee4a77a3SMauro Carvalho Chehab 	 * Enable the interrupt for the end of each frame. In continuous mode
781*ee4a77a3SMauro Carvalho Chehab 	 * chained lists are used with one list per frame, so enable the
782*ee4a77a3SMauro Carvalho Chehab 	 * interrupt for each list. In singleshot mode chained lists are used
783*ee4a77a3SMauro Carvalho Chehab 	 * to partition a single frame, so enable the interrupt for the last
784*ee4a77a3SMauro Carvalho Chehab 	 * list only.
785*ee4a77a3SMauro Carvalho Chehab 	 */
786*ee4a77a3SMauro Carvalho Chehab 	if (!dlm->singleshot || is_last)
787*ee4a77a3SMauro Carvalho Chehab 		dl->header->flags |= VSP1_DLH_INT_ENABLE;
788*ee4a77a3SMauro Carvalho Chehab 
789*ee4a77a3SMauro Carvalho Chehab 	/*
790*ee4a77a3SMauro Carvalho Chehab 	 * In continuous mode enable auto-start for all lists, as the VSP must
791*ee4a77a3SMauro Carvalho Chehab 	 * loop on the same list until a new one is queued. In singleshot mode
792*ee4a77a3SMauro Carvalho Chehab 	 * enable auto-start for all lists but the last to chain processing of
793*ee4a77a3SMauro Carvalho Chehab 	 * partitions without software intervention.
794*ee4a77a3SMauro Carvalho Chehab 	 */
795*ee4a77a3SMauro Carvalho Chehab 	if (!dlm->singleshot || !is_last)
796*ee4a77a3SMauro Carvalho Chehab 		dl->header->flags |= VSP1_DLH_AUTO_START;
797*ee4a77a3SMauro Carvalho Chehab 
798*ee4a77a3SMauro Carvalho Chehab 	if (!is_last) {
799*ee4a77a3SMauro Carvalho Chehab 		/*
800*ee4a77a3SMauro Carvalho Chehab 		 * If this is not the last display list in the chain, queue the
801*ee4a77a3SMauro Carvalho Chehab 		 * next item for automatic processing by the hardware.
802*ee4a77a3SMauro Carvalho Chehab 		 */
803*ee4a77a3SMauro Carvalho Chehab 		struct vsp1_dl_list *next = list_next_entry(dl, chain);
804*ee4a77a3SMauro Carvalho Chehab 
805*ee4a77a3SMauro Carvalho Chehab 		dl->header->next_header = next->dma;
806*ee4a77a3SMauro Carvalho Chehab 	} else if (!dlm->singleshot) {
807*ee4a77a3SMauro Carvalho Chehab 		/*
808*ee4a77a3SMauro Carvalho Chehab 		 * if the display list manager works in continuous mode, the VSP
809*ee4a77a3SMauro Carvalho Chehab 		 * should loop over the display list continuously until
810*ee4a77a3SMauro Carvalho Chehab 		 * instructed to do otherwise.
811*ee4a77a3SMauro Carvalho Chehab 		 */
812*ee4a77a3SMauro Carvalho Chehab 		dl->header->next_header = dl->dma;
813*ee4a77a3SMauro Carvalho Chehab 	}
814*ee4a77a3SMauro Carvalho Chehab 
815*ee4a77a3SMauro Carvalho Chehab 	if (!dl->extension)
816*ee4a77a3SMauro Carvalho Chehab 		return;
817*ee4a77a3SMauro Carvalho Chehab 
818*ee4a77a3SMauro Carvalho Chehab 	dl->extension->flags = 0;
819*ee4a77a3SMauro Carvalho Chehab 
820*ee4a77a3SMauro Carvalho Chehab 	if (dl->pre_cmd) {
821*ee4a77a3SMauro Carvalho Chehab 		dl->extension->pre_ext_dl_plist = dl->pre_cmd->cmd_dma;
822*ee4a77a3SMauro Carvalho Chehab 		dl->extension->pre_ext_dl_num_cmd = dl->pre_cmd->num_cmds;
823*ee4a77a3SMauro Carvalho Chehab 		dl->extension->flags |= VSP1_DLH_EXT_PRE_CMD_EXEC;
824*ee4a77a3SMauro Carvalho Chehab 
825*ee4a77a3SMauro Carvalho Chehab 		vsp1_dl_ext_cmd_fill_header(dl->pre_cmd);
826*ee4a77a3SMauro Carvalho Chehab 	}
827*ee4a77a3SMauro Carvalho Chehab 
828*ee4a77a3SMauro Carvalho Chehab 	if (dl->post_cmd) {
829*ee4a77a3SMauro Carvalho Chehab 		dl->extension->post_ext_dl_plist = dl->post_cmd->cmd_dma;
830*ee4a77a3SMauro Carvalho Chehab 		dl->extension->post_ext_dl_num_cmd = dl->post_cmd->num_cmds;
831*ee4a77a3SMauro Carvalho Chehab 		dl->extension->flags |= VSP1_DLH_EXT_POST_CMD_EXEC;
832*ee4a77a3SMauro Carvalho Chehab 
833*ee4a77a3SMauro Carvalho Chehab 		vsp1_dl_ext_cmd_fill_header(dl->post_cmd);
834*ee4a77a3SMauro Carvalho Chehab 	}
835*ee4a77a3SMauro Carvalho Chehab }
836*ee4a77a3SMauro Carvalho Chehab 
vsp1_dl_list_hw_update_pending(struct vsp1_dl_manager * dlm)837*ee4a77a3SMauro Carvalho Chehab static bool vsp1_dl_list_hw_update_pending(struct vsp1_dl_manager *dlm)
838*ee4a77a3SMauro Carvalho Chehab {
839*ee4a77a3SMauro Carvalho Chehab 	struct vsp1_device *vsp1 = dlm->vsp1;
840*ee4a77a3SMauro Carvalho Chehab 
841*ee4a77a3SMauro Carvalho Chehab 	if (!dlm->queued)
842*ee4a77a3SMauro Carvalho Chehab 		return false;
843*ee4a77a3SMauro Carvalho Chehab 
844*ee4a77a3SMauro Carvalho Chehab 	/*
845*ee4a77a3SMauro Carvalho Chehab 	 * Check whether the VSP1 has taken the update. The hardware indicates
846*ee4a77a3SMauro Carvalho Chehab 	 * this by clearing the UPDHDR bit in the CMD register.
847*ee4a77a3SMauro Carvalho Chehab 	 */
848*ee4a77a3SMauro Carvalho Chehab 	return !!(vsp1_read(vsp1, VI6_CMD(dlm->index)) & VI6_CMD_UPDHDR);
849*ee4a77a3SMauro Carvalho Chehab }
850*ee4a77a3SMauro Carvalho Chehab 
vsp1_dl_list_hw_enqueue(struct vsp1_dl_list * dl)851*ee4a77a3SMauro Carvalho Chehab static void vsp1_dl_list_hw_enqueue(struct vsp1_dl_list *dl)
852*ee4a77a3SMauro Carvalho Chehab {
853*ee4a77a3SMauro Carvalho Chehab 	struct vsp1_dl_manager *dlm = dl->dlm;
854*ee4a77a3SMauro Carvalho Chehab 	struct vsp1_device *vsp1 = dlm->vsp1;
855*ee4a77a3SMauro Carvalho Chehab 
856*ee4a77a3SMauro Carvalho Chehab 	/*
857*ee4a77a3SMauro Carvalho Chehab 	 * Program the display list header address. If the hardware is idle
858*ee4a77a3SMauro Carvalho Chehab 	 * (single-shot mode or first frame in continuous mode) it will then be
859*ee4a77a3SMauro Carvalho Chehab 	 * started independently. If the hardware is operating, the
860*ee4a77a3SMauro Carvalho Chehab 	 * VI6_DL_HDR_REF_ADDR register will be updated with the display list
861*ee4a77a3SMauro Carvalho Chehab 	 * address.
862*ee4a77a3SMauro Carvalho Chehab 	 */
863*ee4a77a3SMauro Carvalho Chehab 	vsp1_write(vsp1, VI6_DL_HDR_ADDR(dlm->index), dl->dma);
864*ee4a77a3SMauro Carvalho Chehab }
865*ee4a77a3SMauro Carvalho Chehab 
vsp1_dl_list_commit_continuous(struct vsp1_dl_list * dl)866*ee4a77a3SMauro Carvalho Chehab static void vsp1_dl_list_commit_continuous(struct vsp1_dl_list *dl)
867*ee4a77a3SMauro Carvalho Chehab {
868*ee4a77a3SMauro Carvalho Chehab 	struct vsp1_dl_manager *dlm = dl->dlm;
869*ee4a77a3SMauro Carvalho Chehab 
870*ee4a77a3SMauro Carvalho Chehab 	/*
871*ee4a77a3SMauro Carvalho Chehab 	 * If a previous display list has been queued to the hardware but not
872*ee4a77a3SMauro Carvalho Chehab 	 * processed yet, the VSP can start processing it at any time. In that
873*ee4a77a3SMauro Carvalho Chehab 	 * case we can't replace the queued list by the new one, as we could
874*ee4a77a3SMauro Carvalho Chehab 	 * race with the hardware. We thus mark the update as pending, it will
875*ee4a77a3SMauro Carvalho Chehab 	 * be queued up to the hardware by the frame end interrupt handler.
876*ee4a77a3SMauro Carvalho Chehab 	 *
877*ee4a77a3SMauro Carvalho Chehab 	 * If a display list is already pending we simply drop it as the new
878*ee4a77a3SMauro Carvalho Chehab 	 * display list is assumed to contain a more recent configuration. It is
879*ee4a77a3SMauro Carvalho Chehab 	 * an error if the already pending list has the
880*ee4a77a3SMauro Carvalho Chehab 	 * VSP1_DL_FRAME_END_INTERNAL flag set, as there is then a process
881*ee4a77a3SMauro Carvalho Chehab 	 * waiting for that list to complete. This shouldn't happen as the
882*ee4a77a3SMauro Carvalho Chehab 	 * waiting process should perform proper locking, but warn just in
883*ee4a77a3SMauro Carvalho Chehab 	 * case.
884*ee4a77a3SMauro Carvalho Chehab 	 */
885*ee4a77a3SMauro Carvalho Chehab 	if (vsp1_dl_list_hw_update_pending(dlm)) {
886*ee4a77a3SMauro Carvalho Chehab 		WARN_ON(dlm->pending &&
887*ee4a77a3SMauro Carvalho Chehab 			(dlm->pending->flags & VSP1_DL_FRAME_END_INTERNAL));
888*ee4a77a3SMauro Carvalho Chehab 		__vsp1_dl_list_put(dlm->pending);
889*ee4a77a3SMauro Carvalho Chehab 		dlm->pending = dl;
890*ee4a77a3SMauro Carvalho Chehab 		return;
891*ee4a77a3SMauro Carvalho Chehab 	}
892*ee4a77a3SMauro Carvalho Chehab 
893*ee4a77a3SMauro Carvalho Chehab 	/*
894*ee4a77a3SMauro Carvalho Chehab 	 * Pass the new display list to the hardware and mark it as queued. It
895*ee4a77a3SMauro Carvalho Chehab 	 * will become active when the hardware starts processing it.
896*ee4a77a3SMauro Carvalho Chehab 	 */
897*ee4a77a3SMauro Carvalho Chehab 	vsp1_dl_list_hw_enqueue(dl);
898*ee4a77a3SMauro Carvalho Chehab 
899*ee4a77a3SMauro Carvalho Chehab 	__vsp1_dl_list_put(dlm->queued);
900*ee4a77a3SMauro Carvalho Chehab 	dlm->queued = dl;
901*ee4a77a3SMauro Carvalho Chehab }
902*ee4a77a3SMauro Carvalho Chehab 
vsp1_dl_list_commit_singleshot(struct vsp1_dl_list * dl)903*ee4a77a3SMauro Carvalho Chehab static void vsp1_dl_list_commit_singleshot(struct vsp1_dl_list *dl)
904*ee4a77a3SMauro Carvalho Chehab {
905*ee4a77a3SMauro Carvalho Chehab 	struct vsp1_dl_manager *dlm = dl->dlm;
906*ee4a77a3SMauro Carvalho Chehab 
907*ee4a77a3SMauro Carvalho Chehab 	/*
908*ee4a77a3SMauro Carvalho Chehab 	 * When working in single-shot mode, the caller guarantees that the
909*ee4a77a3SMauro Carvalho Chehab 	 * hardware is idle at this point. Just commit the head display list
910*ee4a77a3SMauro Carvalho Chehab 	 * to hardware. Chained lists will be started automatically.
911*ee4a77a3SMauro Carvalho Chehab 	 */
912*ee4a77a3SMauro Carvalho Chehab 	vsp1_dl_list_hw_enqueue(dl);
913*ee4a77a3SMauro Carvalho Chehab 
914*ee4a77a3SMauro Carvalho Chehab 	dlm->active = dl;
915*ee4a77a3SMauro Carvalho Chehab }
916*ee4a77a3SMauro Carvalho Chehab 
vsp1_dl_list_commit(struct vsp1_dl_list * dl,unsigned int dl_flags)917*ee4a77a3SMauro Carvalho Chehab void vsp1_dl_list_commit(struct vsp1_dl_list *dl, unsigned int dl_flags)
918*ee4a77a3SMauro Carvalho Chehab {
919*ee4a77a3SMauro Carvalho Chehab 	struct vsp1_dl_manager *dlm = dl->dlm;
920*ee4a77a3SMauro Carvalho Chehab 	struct vsp1_dl_list *dl_next;
921*ee4a77a3SMauro Carvalho Chehab 	unsigned long flags;
922*ee4a77a3SMauro Carvalho Chehab 
923*ee4a77a3SMauro Carvalho Chehab 	/* Fill the header for the head and chained display lists. */
924*ee4a77a3SMauro Carvalho Chehab 	vsp1_dl_list_fill_header(dl, list_empty(&dl->chain));
925*ee4a77a3SMauro Carvalho Chehab 
926*ee4a77a3SMauro Carvalho Chehab 	list_for_each_entry(dl_next, &dl->chain, chain) {
927*ee4a77a3SMauro Carvalho Chehab 		bool last = list_is_last(&dl_next->chain, &dl->chain);
928*ee4a77a3SMauro Carvalho Chehab 
929*ee4a77a3SMauro Carvalho Chehab 		vsp1_dl_list_fill_header(dl_next, last);
930*ee4a77a3SMauro Carvalho Chehab 	}
931*ee4a77a3SMauro Carvalho Chehab 
932*ee4a77a3SMauro Carvalho Chehab 	dl->flags = dl_flags & ~VSP1_DL_FRAME_END_COMPLETED;
933*ee4a77a3SMauro Carvalho Chehab 
934*ee4a77a3SMauro Carvalho Chehab 	spin_lock_irqsave(&dlm->lock, flags);
935*ee4a77a3SMauro Carvalho Chehab 
936*ee4a77a3SMauro Carvalho Chehab 	if (dlm->singleshot)
937*ee4a77a3SMauro Carvalho Chehab 		vsp1_dl_list_commit_singleshot(dl);
938*ee4a77a3SMauro Carvalho Chehab 	else
939*ee4a77a3SMauro Carvalho Chehab 		vsp1_dl_list_commit_continuous(dl);
940*ee4a77a3SMauro Carvalho Chehab 
941*ee4a77a3SMauro Carvalho Chehab 	spin_unlock_irqrestore(&dlm->lock, flags);
942*ee4a77a3SMauro Carvalho Chehab }
943*ee4a77a3SMauro Carvalho Chehab 
944*ee4a77a3SMauro Carvalho Chehab /* -----------------------------------------------------------------------------
945*ee4a77a3SMauro Carvalho Chehab  * Display List Manager
946*ee4a77a3SMauro Carvalho Chehab  */
947*ee4a77a3SMauro Carvalho Chehab 
948*ee4a77a3SMauro Carvalho Chehab /**
949*ee4a77a3SMauro Carvalho Chehab  * vsp1_dlm_irq_frame_end - Display list handler for the frame end interrupt
950*ee4a77a3SMauro Carvalho Chehab  * @dlm: the display list manager
951*ee4a77a3SMauro Carvalho Chehab  *
952*ee4a77a3SMauro Carvalho Chehab  * Return a set of flags that indicates display list completion status.
953*ee4a77a3SMauro Carvalho Chehab  *
954*ee4a77a3SMauro Carvalho Chehab  * The VSP1_DL_FRAME_END_COMPLETED flag indicates that the previous display list
955*ee4a77a3SMauro Carvalho Chehab  * has completed at frame end. If the flag is not returned display list
956*ee4a77a3SMauro Carvalho Chehab  * completion has been delayed by one frame because the display list commit
957*ee4a77a3SMauro Carvalho Chehab  * raced with the frame end interrupt. The function always returns with the flag
958*ee4a77a3SMauro Carvalho Chehab  * set in single-shot mode as display list processing is then not continuous and
959*ee4a77a3SMauro Carvalho Chehab  * races never occur.
960*ee4a77a3SMauro Carvalho Chehab  *
961*ee4a77a3SMauro Carvalho Chehab  * The following flags are only supported for continuous mode.
962*ee4a77a3SMauro Carvalho Chehab  *
963*ee4a77a3SMauro Carvalho Chehab  * The VSP1_DL_FRAME_END_INTERNAL flag indicates that the display list that just
964*ee4a77a3SMauro Carvalho Chehab  * became active had been queued with the internal notification flag.
965*ee4a77a3SMauro Carvalho Chehab  *
966*ee4a77a3SMauro Carvalho Chehab  * The VSP1_DL_FRAME_END_WRITEBACK flag indicates that the previously active
967*ee4a77a3SMauro Carvalho Chehab  * display list had been queued with the writeback flag.
968*ee4a77a3SMauro Carvalho Chehab  */
vsp1_dlm_irq_frame_end(struct vsp1_dl_manager * dlm)969*ee4a77a3SMauro Carvalho Chehab unsigned int vsp1_dlm_irq_frame_end(struct vsp1_dl_manager *dlm)
970*ee4a77a3SMauro Carvalho Chehab {
971*ee4a77a3SMauro Carvalho Chehab 	struct vsp1_device *vsp1 = dlm->vsp1;
972*ee4a77a3SMauro Carvalho Chehab 	u32 status = vsp1_read(vsp1, VI6_STATUS);
973*ee4a77a3SMauro Carvalho Chehab 	unsigned int flags = 0;
974*ee4a77a3SMauro Carvalho Chehab 
975*ee4a77a3SMauro Carvalho Chehab 	spin_lock(&dlm->lock);
976*ee4a77a3SMauro Carvalho Chehab 
977*ee4a77a3SMauro Carvalho Chehab 	/*
978*ee4a77a3SMauro Carvalho Chehab 	 * The mem-to-mem pipelines work in single-shot mode. No new display
979*ee4a77a3SMauro Carvalho Chehab 	 * list can be queued, we don't have to do anything.
980*ee4a77a3SMauro Carvalho Chehab 	 */
981*ee4a77a3SMauro Carvalho Chehab 	if (dlm->singleshot) {
982*ee4a77a3SMauro Carvalho Chehab 		__vsp1_dl_list_put(dlm->active);
983*ee4a77a3SMauro Carvalho Chehab 		dlm->active = NULL;
984*ee4a77a3SMauro Carvalho Chehab 		flags |= VSP1_DL_FRAME_END_COMPLETED;
985*ee4a77a3SMauro Carvalho Chehab 		goto done;
986*ee4a77a3SMauro Carvalho Chehab 	}
987*ee4a77a3SMauro Carvalho Chehab 
988*ee4a77a3SMauro Carvalho Chehab 	/*
989*ee4a77a3SMauro Carvalho Chehab 	 * If the commit operation raced with the interrupt and occurred after
990*ee4a77a3SMauro Carvalho Chehab 	 * the frame end event but before interrupt processing, the hardware
991*ee4a77a3SMauro Carvalho Chehab 	 * hasn't taken the update into account yet. We have to skip one frame
992*ee4a77a3SMauro Carvalho Chehab 	 * and retry.
993*ee4a77a3SMauro Carvalho Chehab 	 */
994*ee4a77a3SMauro Carvalho Chehab 	if (vsp1_dl_list_hw_update_pending(dlm))
995*ee4a77a3SMauro Carvalho Chehab 		goto done;
996*ee4a77a3SMauro Carvalho Chehab 
997*ee4a77a3SMauro Carvalho Chehab 	/*
998*ee4a77a3SMauro Carvalho Chehab 	 * Progressive streams report only TOP fields. If we have a BOTTOM
999*ee4a77a3SMauro Carvalho Chehab 	 * field, we are interlaced, and expect the frame to complete on the
1000*ee4a77a3SMauro Carvalho Chehab 	 * next frame end interrupt.
1001*ee4a77a3SMauro Carvalho Chehab 	 */
1002*ee4a77a3SMauro Carvalho Chehab 	if (status & VI6_STATUS_FLD_STD(dlm->index))
1003*ee4a77a3SMauro Carvalho Chehab 		goto done;
1004*ee4a77a3SMauro Carvalho Chehab 
1005*ee4a77a3SMauro Carvalho Chehab 	/*
1006*ee4a77a3SMauro Carvalho Chehab 	 * If the active display list has the writeback flag set, the frame
1007*ee4a77a3SMauro Carvalho Chehab 	 * completion marks the end of the writeback capture. Return the
1008*ee4a77a3SMauro Carvalho Chehab 	 * VSP1_DL_FRAME_END_WRITEBACK flag and reset the display list's
1009*ee4a77a3SMauro Carvalho Chehab 	 * writeback flag.
1010*ee4a77a3SMauro Carvalho Chehab 	 */
1011*ee4a77a3SMauro Carvalho Chehab 	if (dlm->active && (dlm->active->flags & VSP1_DL_FRAME_END_WRITEBACK)) {
1012*ee4a77a3SMauro Carvalho Chehab 		flags |= VSP1_DL_FRAME_END_WRITEBACK;
1013*ee4a77a3SMauro Carvalho Chehab 		dlm->active->flags &= ~VSP1_DL_FRAME_END_WRITEBACK;
1014*ee4a77a3SMauro Carvalho Chehab 	}
1015*ee4a77a3SMauro Carvalho Chehab 
1016*ee4a77a3SMauro Carvalho Chehab 	/*
1017*ee4a77a3SMauro Carvalho Chehab 	 * The device starts processing the queued display list right after the
1018*ee4a77a3SMauro Carvalho Chehab 	 * frame end interrupt. The display list thus becomes active.
1019*ee4a77a3SMauro Carvalho Chehab 	 */
1020*ee4a77a3SMauro Carvalho Chehab 	if (dlm->queued) {
1021*ee4a77a3SMauro Carvalho Chehab 		if (dlm->queued->flags & VSP1_DL_FRAME_END_INTERNAL)
1022*ee4a77a3SMauro Carvalho Chehab 			flags |= VSP1_DL_FRAME_END_INTERNAL;
1023*ee4a77a3SMauro Carvalho Chehab 		dlm->queued->flags &= ~VSP1_DL_FRAME_END_INTERNAL;
1024*ee4a77a3SMauro Carvalho Chehab 
1025*ee4a77a3SMauro Carvalho Chehab 		__vsp1_dl_list_put(dlm->active);
1026*ee4a77a3SMauro Carvalho Chehab 		dlm->active = dlm->queued;
1027*ee4a77a3SMauro Carvalho Chehab 		dlm->queued = NULL;
1028*ee4a77a3SMauro Carvalho Chehab 		flags |= VSP1_DL_FRAME_END_COMPLETED;
1029*ee4a77a3SMauro Carvalho Chehab 	}
1030*ee4a77a3SMauro Carvalho Chehab 
1031*ee4a77a3SMauro Carvalho Chehab 	/*
1032*ee4a77a3SMauro Carvalho Chehab 	 * Now that the VSP has started processing the queued display list, we
1033*ee4a77a3SMauro Carvalho Chehab 	 * can queue the pending display list to the hardware if one has been
1034*ee4a77a3SMauro Carvalho Chehab 	 * prepared.
1035*ee4a77a3SMauro Carvalho Chehab 	 */
1036*ee4a77a3SMauro Carvalho Chehab 	if (dlm->pending) {
1037*ee4a77a3SMauro Carvalho Chehab 		vsp1_dl_list_hw_enqueue(dlm->pending);
1038*ee4a77a3SMauro Carvalho Chehab 		dlm->queued = dlm->pending;
1039*ee4a77a3SMauro Carvalho Chehab 		dlm->pending = NULL;
1040*ee4a77a3SMauro Carvalho Chehab 	}
1041*ee4a77a3SMauro Carvalho Chehab 
1042*ee4a77a3SMauro Carvalho Chehab done:
1043*ee4a77a3SMauro Carvalho Chehab 	spin_unlock(&dlm->lock);
1044*ee4a77a3SMauro Carvalho Chehab 
1045*ee4a77a3SMauro Carvalho Chehab 	return flags;
1046*ee4a77a3SMauro Carvalho Chehab }
1047*ee4a77a3SMauro Carvalho Chehab 
1048*ee4a77a3SMauro Carvalho Chehab /* Hardware Setup */
vsp1_dlm_setup(struct vsp1_device * vsp1)1049*ee4a77a3SMauro Carvalho Chehab void vsp1_dlm_setup(struct vsp1_device *vsp1)
1050*ee4a77a3SMauro Carvalho Chehab {
1051*ee4a77a3SMauro Carvalho Chehab 	unsigned int i;
1052*ee4a77a3SMauro Carvalho Chehab 	u32 ctrl = (256 << VI6_DL_CTRL_AR_WAIT_SHIFT)
1053*ee4a77a3SMauro Carvalho Chehab 		 | VI6_DL_CTRL_DC2 | VI6_DL_CTRL_DC1 | VI6_DL_CTRL_DC0
1054*ee4a77a3SMauro Carvalho Chehab 		 | VI6_DL_CTRL_DLE;
1055*ee4a77a3SMauro Carvalho Chehab 	u32 ext_dl = (0x02 << VI6_DL_EXT_CTRL_POLINT_SHIFT)
1056*ee4a77a3SMauro Carvalho Chehab 		   | VI6_DL_EXT_CTRL_DLPRI | VI6_DL_EXT_CTRL_EXT;
1057*ee4a77a3SMauro Carvalho Chehab 
1058*ee4a77a3SMauro Carvalho Chehab 	if (vsp1_feature(vsp1, VSP1_HAS_EXT_DL)) {
1059*ee4a77a3SMauro Carvalho Chehab 		for (i = 0; i < vsp1->info->wpf_count; ++i)
1060*ee4a77a3SMauro Carvalho Chehab 			vsp1_write(vsp1, VI6_DL_EXT_CTRL(i), ext_dl);
1061*ee4a77a3SMauro Carvalho Chehab 	}
1062*ee4a77a3SMauro Carvalho Chehab 
1063*ee4a77a3SMauro Carvalho Chehab 	vsp1_write(vsp1, VI6_DL_CTRL, ctrl);
1064*ee4a77a3SMauro Carvalho Chehab 	vsp1_write(vsp1, VI6_DL_SWAP, VI6_DL_SWAP_LWS);
1065*ee4a77a3SMauro Carvalho Chehab }
1066*ee4a77a3SMauro Carvalho Chehab 
vsp1_dlm_reset(struct vsp1_dl_manager * dlm)1067*ee4a77a3SMauro Carvalho Chehab void vsp1_dlm_reset(struct vsp1_dl_manager *dlm)
1068*ee4a77a3SMauro Carvalho Chehab {
1069*ee4a77a3SMauro Carvalho Chehab 	unsigned long flags;
1070*ee4a77a3SMauro Carvalho Chehab 
1071*ee4a77a3SMauro Carvalho Chehab 	spin_lock_irqsave(&dlm->lock, flags);
1072*ee4a77a3SMauro Carvalho Chehab 
1073*ee4a77a3SMauro Carvalho Chehab 	__vsp1_dl_list_put(dlm->active);
1074*ee4a77a3SMauro Carvalho Chehab 	__vsp1_dl_list_put(dlm->queued);
1075*ee4a77a3SMauro Carvalho Chehab 	__vsp1_dl_list_put(dlm->pending);
1076*ee4a77a3SMauro Carvalho Chehab 
1077*ee4a77a3SMauro Carvalho Chehab 	spin_unlock_irqrestore(&dlm->lock, flags);
1078*ee4a77a3SMauro Carvalho Chehab 
1079*ee4a77a3SMauro Carvalho Chehab 	dlm->active = NULL;
1080*ee4a77a3SMauro Carvalho Chehab 	dlm->queued = NULL;
1081*ee4a77a3SMauro Carvalho Chehab 	dlm->pending = NULL;
1082*ee4a77a3SMauro Carvalho Chehab }
1083*ee4a77a3SMauro Carvalho Chehab 
vsp1_dlm_dl_body_get(struct vsp1_dl_manager * dlm)1084*ee4a77a3SMauro Carvalho Chehab struct vsp1_dl_body *vsp1_dlm_dl_body_get(struct vsp1_dl_manager *dlm)
1085*ee4a77a3SMauro Carvalho Chehab {
1086*ee4a77a3SMauro Carvalho Chehab 	return vsp1_dl_body_get(dlm->pool);
1087*ee4a77a3SMauro Carvalho Chehab }
1088*ee4a77a3SMauro Carvalho Chehab 
vsp1_dlm_create(struct vsp1_device * vsp1,unsigned int index,unsigned int prealloc)1089*ee4a77a3SMauro Carvalho Chehab struct vsp1_dl_manager *vsp1_dlm_create(struct vsp1_device *vsp1,
1090*ee4a77a3SMauro Carvalho Chehab 					unsigned int index,
1091*ee4a77a3SMauro Carvalho Chehab 					unsigned int prealloc)
1092*ee4a77a3SMauro Carvalho Chehab {
1093*ee4a77a3SMauro Carvalho Chehab 	struct vsp1_dl_manager *dlm;
1094*ee4a77a3SMauro Carvalho Chehab 	size_t header_size;
1095*ee4a77a3SMauro Carvalho Chehab 	unsigned int i;
1096*ee4a77a3SMauro Carvalho Chehab 
1097*ee4a77a3SMauro Carvalho Chehab 	dlm = devm_kzalloc(vsp1->dev, sizeof(*dlm), GFP_KERNEL);
1098*ee4a77a3SMauro Carvalho Chehab 	if (!dlm)
1099*ee4a77a3SMauro Carvalho Chehab 		return NULL;
1100*ee4a77a3SMauro Carvalho Chehab 
1101*ee4a77a3SMauro Carvalho Chehab 	dlm->index = index;
1102*ee4a77a3SMauro Carvalho Chehab 	dlm->singleshot = vsp1->info->uapi;
1103*ee4a77a3SMauro Carvalho Chehab 	dlm->vsp1 = vsp1;
1104*ee4a77a3SMauro Carvalho Chehab 
1105*ee4a77a3SMauro Carvalho Chehab 	spin_lock_init(&dlm->lock);
1106*ee4a77a3SMauro Carvalho Chehab 	INIT_LIST_HEAD(&dlm->free);
1107*ee4a77a3SMauro Carvalho Chehab 
1108*ee4a77a3SMauro Carvalho Chehab 	/*
1109*ee4a77a3SMauro Carvalho Chehab 	 * Initialize the display list body and allocate DMA memory for the body
1110*ee4a77a3SMauro Carvalho Chehab 	 * and the header. Both are allocated together to avoid memory
1111*ee4a77a3SMauro Carvalho Chehab 	 * fragmentation, with the header located right after the body in
1112*ee4a77a3SMauro Carvalho Chehab 	 * memory. An extra body is allocated on top of the prealloc to account
1113*ee4a77a3SMauro Carvalho Chehab 	 * for the cached body used by the vsp1_pipeline object.
1114*ee4a77a3SMauro Carvalho Chehab 	 */
1115*ee4a77a3SMauro Carvalho Chehab 	header_size = vsp1_feature(vsp1, VSP1_HAS_EXT_DL) ?
1116*ee4a77a3SMauro Carvalho Chehab 			sizeof(struct vsp1_dl_header_extended) :
1117*ee4a77a3SMauro Carvalho Chehab 			sizeof(struct vsp1_dl_header);
1118*ee4a77a3SMauro Carvalho Chehab 
1119*ee4a77a3SMauro Carvalho Chehab 	header_size = ALIGN(header_size, 8);
1120*ee4a77a3SMauro Carvalho Chehab 
1121*ee4a77a3SMauro Carvalho Chehab 	dlm->pool = vsp1_dl_body_pool_create(vsp1, prealloc + 1,
1122*ee4a77a3SMauro Carvalho Chehab 					     VSP1_DL_NUM_ENTRIES, header_size);
1123*ee4a77a3SMauro Carvalho Chehab 	if (!dlm->pool)
1124*ee4a77a3SMauro Carvalho Chehab 		return NULL;
1125*ee4a77a3SMauro Carvalho Chehab 
1126*ee4a77a3SMauro Carvalho Chehab 	for (i = 0; i < prealloc; ++i) {
1127*ee4a77a3SMauro Carvalho Chehab 		struct vsp1_dl_list *dl;
1128*ee4a77a3SMauro Carvalho Chehab 
1129*ee4a77a3SMauro Carvalho Chehab 		dl = vsp1_dl_list_alloc(dlm);
1130*ee4a77a3SMauro Carvalho Chehab 		if (!dl) {
1131*ee4a77a3SMauro Carvalho Chehab 			vsp1_dlm_destroy(dlm);
1132*ee4a77a3SMauro Carvalho Chehab 			return NULL;
1133*ee4a77a3SMauro Carvalho Chehab 		}
1134*ee4a77a3SMauro Carvalho Chehab 
1135*ee4a77a3SMauro Carvalho Chehab 		/* The extended header immediately follows the header. */
1136*ee4a77a3SMauro Carvalho Chehab 		if (vsp1_feature(vsp1, VSP1_HAS_EXT_DL))
1137*ee4a77a3SMauro Carvalho Chehab 			dl->extension = (void *)dl->header
1138*ee4a77a3SMauro Carvalho Chehab 				      + sizeof(*dl->header);
1139*ee4a77a3SMauro Carvalho Chehab 
1140*ee4a77a3SMauro Carvalho Chehab 		list_add_tail(&dl->list, &dlm->free);
1141*ee4a77a3SMauro Carvalho Chehab 	}
1142*ee4a77a3SMauro Carvalho Chehab 
1143*ee4a77a3SMauro Carvalho Chehab 	if (vsp1_feature(vsp1, VSP1_HAS_EXT_DL)) {
1144*ee4a77a3SMauro Carvalho Chehab 		dlm->cmdpool = vsp1_dl_cmd_pool_create(vsp1,
1145*ee4a77a3SMauro Carvalho Chehab 					VSP1_EXTCMD_AUTOFLD, prealloc);
1146*ee4a77a3SMauro Carvalho Chehab 		if (!dlm->cmdpool) {
1147*ee4a77a3SMauro Carvalho Chehab 			vsp1_dlm_destroy(dlm);
1148*ee4a77a3SMauro Carvalho Chehab 			return NULL;
1149*ee4a77a3SMauro Carvalho Chehab 		}
1150*ee4a77a3SMauro Carvalho Chehab 	}
1151*ee4a77a3SMauro Carvalho Chehab 
1152*ee4a77a3SMauro Carvalho Chehab 	return dlm;
1153*ee4a77a3SMauro Carvalho Chehab }
1154*ee4a77a3SMauro Carvalho Chehab 
vsp1_dlm_destroy(struct vsp1_dl_manager * dlm)1155*ee4a77a3SMauro Carvalho Chehab void vsp1_dlm_destroy(struct vsp1_dl_manager *dlm)
1156*ee4a77a3SMauro Carvalho Chehab {
1157*ee4a77a3SMauro Carvalho Chehab 	struct vsp1_dl_list *dl, *next;
1158*ee4a77a3SMauro Carvalho Chehab 
1159*ee4a77a3SMauro Carvalho Chehab 	if (!dlm)
1160*ee4a77a3SMauro Carvalho Chehab 		return;
1161*ee4a77a3SMauro Carvalho Chehab 
1162*ee4a77a3SMauro Carvalho Chehab 	list_for_each_entry_safe(dl, next, &dlm->free, list) {
1163*ee4a77a3SMauro Carvalho Chehab 		list_del(&dl->list);
1164*ee4a77a3SMauro Carvalho Chehab 		vsp1_dl_list_free(dl);
1165*ee4a77a3SMauro Carvalho Chehab 	}
1166*ee4a77a3SMauro Carvalho Chehab 
1167*ee4a77a3SMauro Carvalho Chehab 	vsp1_dl_body_pool_destroy(dlm->pool);
1168*ee4a77a3SMauro Carvalho Chehab 	vsp1_dl_ext_cmd_pool_destroy(dlm->cmdpool);
1169*ee4a77a3SMauro Carvalho Chehab }
1170