vsp1_dl.c 29 KB

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