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