vsp1_dl.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137
  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. spin_lock_init(&pool->lock);
  366. INIT_LIST_HEAD(&pool->free);
  367. pool->cmds = kcalloc(num_cmds, sizeof(*pool->cmds), GFP_KERNEL);
  368. if (!pool->cmds) {
  369. kfree(pool);
  370. return NULL;
  371. }
  372. cmd_size = sizeof(struct vsp1_pre_ext_dl_body) +
  373. vsp1_extended_commands[type].body_size;
  374. cmd_size = ALIGN(cmd_size, 16);
  375. pool->size = cmd_size * num_cmds;
  376. pool->mem = dma_alloc_wc(vsp1->bus_master, pool->size, &pool->dma,
  377. GFP_KERNEL);
  378. if (!pool->mem) {
  379. kfree(pool->cmds);
  380. kfree(pool);
  381. return NULL;
  382. }
  383. for (i = 0; i < num_cmds; ++i) {
  384. struct vsp1_dl_ext_cmd *cmd = &pool->cmds[i];
  385. size_t cmd_offset = i * cmd_size;
  386. /* data_offset must be 16 byte aligned for DMA. */
  387. size_t data_offset = sizeof(struct vsp1_pre_ext_dl_body) +
  388. cmd_offset;
  389. cmd->pool = pool;
  390. cmd->opcode = vsp1_extended_commands[type].opcode;
  391. /*
  392. * TODO: Auto-disp can utilise more than one extended body
  393. * command per cmd.
  394. */
  395. cmd->num_cmds = 1;
  396. cmd->cmds = pool->mem + cmd_offset;
  397. cmd->cmd_dma = pool->dma + cmd_offset;
  398. cmd->data = pool->mem + data_offset;
  399. cmd->data_dma = pool->dma + data_offset;
  400. list_add_tail(&cmd->free, &pool->free);
  401. }
  402. return pool;
  403. }
  404. static
  405. struct vsp1_dl_ext_cmd *vsp1_dl_ext_cmd_get(struct vsp1_dl_cmd_pool *pool)
  406. {
  407. struct vsp1_dl_ext_cmd *cmd = NULL;
  408. unsigned long flags;
  409. spin_lock_irqsave(&pool->lock, flags);
  410. if (!list_empty(&pool->free)) {
  411. cmd = list_first_entry(&pool->free, struct vsp1_dl_ext_cmd,
  412. free);
  413. list_del(&cmd->free);
  414. }
  415. spin_unlock_irqrestore(&pool->lock, flags);
  416. return cmd;
  417. }
  418. static void vsp1_dl_ext_cmd_put(struct vsp1_dl_ext_cmd *cmd)
  419. {
  420. unsigned long flags;
  421. if (!cmd)
  422. return;
  423. /* Reset flags, these mark data usage. */
  424. cmd->flags = 0;
  425. spin_lock_irqsave(&cmd->pool->lock, flags);
  426. list_add_tail(&cmd->free, &cmd->pool->free);
  427. spin_unlock_irqrestore(&cmd->pool->lock, flags);
  428. }
  429. static void vsp1_dl_ext_cmd_pool_destroy(struct vsp1_dl_cmd_pool *pool)
  430. {
  431. if (!pool)
  432. return;
  433. if (pool->mem)
  434. dma_free_wc(pool->vsp1->bus_master, pool->size, pool->mem,
  435. pool->dma);
  436. kfree(pool->cmds);
  437. kfree(pool);
  438. }
  439. struct vsp1_dl_ext_cmd *vsp1_dl_get_pre_cmd(struct vsp1_dl_list *dl)
  440. {
  441. struct vsp1_dl_manager *dlm = dl->dlm;
  442. if (dl->pre_cmd)
  443. return dl->pre_cmd;
  444. dl->pre_cmd = vsp1_dl_ext_cmd_get(dlm->cmdpool);
  445. return dl->pre_cmd;
  446. }
  447. /* ----------------------------------------------------------------------------
  448. * Display List Transaction Management
  449. */
  450. static struct vsp1_dl_list *vsp1_dl_list_alloc(struct vsp1_dl_manager *dlm)
  451. {
  452. struct vsp1_dl_list *dl;
  453. size_t header_offset;
  454. dl = kzalloc(sizeof(*dl), GFP_KERNEL);
  455. if (!dl)
  456. return NULL;
  457. INIT_LIST_HEAD(&dl->bodies);
  458. dl->dlm = dlm;
  459. /* Get a default body for our list. */
  460. dl->body0 = vsp1_dl_body_get(dlm->pool);
  461. if (!dl->body0)
  462. return NULL;
  463. header_offset = dl->body0->max_entries * sizeof(*dl->body0->entries);
  464. dl->header = ((void *)dl->body0->entries) + header_offset;
  465. dl->dma = dl->body0->dma + header_offset;
  466. memset(dl->header, 0, sizeof(*dl->header));
  467. dl->header->lists[0].addr = dl->body0->dma;
  468. return dl;
  469. }
  470. static void vsp1_dl_list_bodies_put(struct vsp1_dl_list *dl)
  471. {
  472. struct vsp1_dl_body *dlb, *tmp;
  473. list_for_each_entry_safe(dlb, tmp, &dl->bodies, list) {
  474. list_del(&dlb->list);
  475. vsp1_dl_body_put(dlb);
  476. }
  477. }
  478. static void vsp1_dl_list_free(struct vsp1_dl_list *dl)
  479. {
  480. vsp1_dl_body_put(dl->body0);
  481. vsp1_dl_list_bodies_put(dl);
  482. kfree(dl);
  483. }
  484. /**
  485. * vsp1_dl_list_get - Get a free display list
  486. * @dlm: The display list manager
  487. *
  488. * Get a display list from the pool of free lists and return it.
  489. *
  490. * This function must be called without the display list manager lock held.
  491. */
  492. struct vsp1_dl_list *vsp1_dl_list_get(struct vsp1_dl_manager *dlm)
  493. {
  494. struct vsp1_dl_list *dl = NULL;
  495. unsigned long flags;
  496. spin_lock_irqsave(&dlm->lock, flags);
  497. if (!list_empty(&dlm->free)) {
  498. dl = list_first_entry(&dlm->free, struct vsp1_dl_list, list);
  499. list_del(&dl->list);
  500. /*
  501. * The display list chain must be initialised to ensure every
  502. * display list can assert list_empty() if it is not in a chain.
  503. */
  504. INIT_LIST_HEAD(&dl->chain);
  505. }
  506. spin_unlock_irqrestore(&dlm->lock, flags);
  507. return dl;
  508. }
  509. /* This function must be called with the display list manager lock held.*/
  510. static void __vsp1_dl_list_put(struct vsp1_dl_list *dl)
  511. {
  512. struct vsp1_dl_list *dl_next;
  513. if (!dl)
  514. return;
  515. /*
  516. * Release any linked display-lists which were chained for a single
  517. * hardware operation.
  518. */
  519. if (dl->has_chain) {
  520. list_for_each_entry(dl_next, &dl->chain, chain)
  521. __vsp1_dl_list_put(dl_next);
  522. }
  523. dl->has_chain = false;
  524. vsp1_dl_list_bodies_put(dl);
  525. vsp1_dl_ext_cmd_put(dl->pre_cmd);
  526. vsp1_dl_ext_cmd_put(dl->post_cmd);
  527. dl->pre_cmd = NULL;
  528. dl->post_cmd = NULL;
  529. /*
  530. * body0 is reused as as an optimisation as presently every display list
  531. * has at least one body, thus we reinitialise the entries list.
  532. */
  533. dl->body0->num_entries = 0;
  534. list_add_tail(&dl->list, &dl->dlm->free);
  535. }
  536. /**
  537. * vsp1_dl_list_put - Release a display list
  538. * @dl: The display list
  539. *
  540. * Release the display list and return it to the pool of free lists.
  541. *
  542. * Passing a NULL pointer to this function is safe, in that case no operation
  543. * will be performed.
  544. */
  545. void vsp1_dl_list_put(struct vsp1_dl_list *dl)
  546. {
  547. unsigned long flags;
  548. if (!dl)
  549. return;
  550. spin_lock_irqsave(&dl->dlm->lock, flags);
  551. __vsp1_dl_list_put(dl);
  552. spin_unlock_irqrestore(&dl->dlm->lock, flags);
  553. }
  554. /**
  555. * vsp1_dl_list_get_body0 - Obtain the default body for the display list
  556. * @dl: The display list
  557. *
  558. * Obtain a pointer to the internal display list body allowing this to be passed
  559. * directly to configure operations.
  560. */
  561. struct vsp1_dl_body *vsp1_dl_list_get_body0(struct vsp1_dl_list *dl)
  562. {
  563. return dl->body0;
  564. }
  565. /**
  566. * vsp1_dl_list_add_body - Add a body to the display list
  567. * @dl: The display list
  568. * @dlb: The body
  569. *
  570. * Add a display list body to a display list. Registers contained in bodies are
  571. * processed after registers contained in the main display list, in the order in
  572. * which bodies are added.
  573. *
  574. * Adding a body to a display list passes ownership of the body to the list. The
  575. * caller retains its reference to the fragment when adding it to the display
  576. * list, but is not allowed to add new entries to the body.
  577. *
  578. * The reference must be explicitly released by a call to vsp1_dl_body_put()
  579. * when the body isn't needed anymore.
  580. */
  581. int vsp1_dl_list_add_body(struct vsp1_dl_list *dl, struct vsp1_dl_body *dlb)
  582. {
  583. refcount_inc(&dlb->refcnt);
  584. list_add_tail(&dlb->list, &dl->bodies);
  585. return 0;
  586. }
  587. /**
  588. * vsp1_dl_list_add_chain - Add a display list to a chain
  589. * @head: The head display list
  590. * @dl: The new display list
  591. *
  592. * Add a display list to an existing display list chain. The chained lists
  593. * will be automatically processed by the hardware without intervention from
  594. * the CPU. A display list end interrupt will only complete after the last
  595. * display list in the chain has completed processing.
  596. *
  597. * Adding a display list to a chain passes ownership of the display list to
  598. * the head display list item. The chain is released when the head dl item is
  599. * put back with __vsp1_dl_list_put().
  600. */
  601. int vsp1_dl_list_add_chain(struct vsp1_dl_list *head,
  602. struct vsp1_dl_list *dl)
  603. {
  604. head->has_chain = true;
  605. list_add_tail(&dl->chain, &head->chain);
  606. return 0;
  607. }
  608. static void vsp1_dl_ext_cmd_fill_header(struct vsp1_dl_ext_cmd *cmd)
  609. {
  610. cmd->cmds[0].opcode = cmd->opcode;
  611. cmd->cmds[0].flags = cmd->flags;
  612. cmd->cmds[0].address_set = cmd->data_dma;
  613. cmd->cmds[0].reserved = 0;
  614. }
  615. static void vsp1_dl_list_fill_header(struct vsp1_dl_list *dl, bool is_last)
  616. {
  617. struct vsp1_dl_manager *dlm = dl->dlm;
  618. struct vsp1_dl_header_list *hdr = dl->header->lists;
  619. struct vsp1_dl_body *dlb;
  620. unsigned int num_lists = 0;
  621. /*
  622. * Fill the header with the display list bodies addresses and sizes. The
  623. * address of the first body has already been filled when the display
  624. * list was allocated.
  625. */
  626. hdr->num_bytes = dl->body0->num_entries
  627. * sizeof(*dl->header->lists);
  628. list_for_each_entry(dlb, &dl->bodies, list) {
  629. num_lists++;
  630. hdr++;
  631. hdr->addr = dlb->dma;
  632. hdr->num_bytes = dlb->num_entries
  633. * sizeof(*dl->header->lists);
  634. }
  635. dl->header->num_lists = num_lists;
  636. if (!list_empty(&dl->chain) && !is_last) {
  637. /*
  638. * If this display list's chain is not empty, we are on a list,
  639. * and the next item is the display list that we must queue for
  640. * automatic processing by the hardware.
  641. */
  642. struct vsp1_dl_list *next = list_next_entry(dl, chain);
  643. dl->header->next_header = next->dma;
  644. dl->header->flags = VSP1_DLH_AUTO_START;
  645. } else if (!dlm->singleshot) {
  646. /*
  647. * if the display list manager works in continuous mode, the VSP
  648. * should loop over the display list continuously until
  649. * instructed to do otherwise.
  650. */
  651. dl->header->next_header = dl->dma;
  652. dl->header->flags = VSP1_DLH_INT_ENABLE | VSP1_DLH_AUTO_START;
  653. } else {
  654. /*
  655. * Otherwise, in mem-to-mem mode, we work in single-shot mode
  656. * and the next display list must not be started automatically.
  657. */
  658. dl->header->flags = VSP1_DLH_INT_ENABLE;
  659. }
  660. if (!dl->extension)
  661. return;
  662. dl->extension->flags = 0;
  663. if (dl->pre_cmd) {
  664. dl->extension->pre_ext_dl_plist = dl->pre_cmd->cmd_dma;
  665. dl->extension->pre_ext_dl_num_cmd = dl->pre_cmd->num_cmds;
  666. dl->extension->flags |= VSP1_DLH_EXT_PRE_CMD_EXEC;
  667. vsp1_dl_ext_cmd_fill_header(dl->pre_cmd);
  668. }
  669. if (dl->post_cmd) {
  670. dl->extension->post_ext_dl_plist = dl->post_cmd->cmd_dma;
  671. dl->extension->post_ext_dl_num_cmd = dl->post_cmd->num_cmds;
  672. dl->extension->flags |= VSP1_DLH_EXT_POST_CMD_EXEC;
  673. vsp1_dl_ext_cmd_fill_header(dl->post_cmd);
  674. }
  675. }
  676. static bool vsp1_dl_list_hw_update_pending(struct vsp1_dl_manager *dlm)
  677. {
  678. struct vsp1_device *vsp1 = dlm->vsp1;
  679. if (!dlm->queued)
  680. return false;
  681. /*
  682. * Check whether the VSP1 has taken the update. The hardware indicates
  683. * this by clearing the UPDHDR bit in the CMD register.
  684. */
  685. return !!(vsp1_read(vsp1, VI6_CMD(dlm->index)) & VI6_CMD_UPDHDR);
  686. }
  687. static void vsp1_dl_list_hw_enqueue(struct vsp1_dl_list *dl)
  688. {
  689. struct vsp1_dl_manager *dlm = dl->dlm;
  690. struct vsp1_device *vsp1 = dlm->vsp1;
  691. /*
  692. * Program the display list header address. If the hardware is idle
  693. * (single-shot mode or first frame in continuous mode) it will then be
  694. * started independently. If the hardware is operating, the
  695. * VI6_DL_HDR_REF_ADDR register will be updated with the display list
  696. * address.
  697. */
  698. vsp1_write(vsp1, VI6_DL_HDR_ADDR(dlm->index), dl->dma);
  699. }
  700. static void vsp1_dl_list_commit_continuous(struct vsp1_dl_list *dl)
  701. {
  702. struct vsp1_dl_manager *dlm = dl->dlm;
  703. /*
  704. * If a previous display list has been queued to the hardware but not
  705. * processed yet, the VSP can start processing it at any time. In that
  706. * case we can't replace the queued list by the new one, as we could
  707. * race with the hardware. We thus mark the update as pending, it will
  708. * be queued up to the hardware by the frame end interrupt handler.
  709. *
  710. * If a display list is already pending we simply drop it as the new
  711. * display list is assumed to contain a more recent configuration. It is
  712. * an error if the already pending list has the internal flag set, as
  713. * there is then a process waiting for that list to complete. This
  714. * shouldn't happen as the waiting process should perform proper
  715. * locking, but warn just in case.
  716. */
  717. if (vsp1_dl_list_hw_update_pending(dlm)) {
  718. WARN_ON(dlm->pending && dlm->pending->internal);
  719. __vsp1_dl_list_put(dlm->pending);
  720. dlm->pending = dl;
  721. return;
  722. }
  723. /*
  724. * Pass the new display list to the hardware and mark it as queued. It
  725. * will become active when the hardware starts processing it.
  726. */
  727. vsp1_dl_list_hw_enqueue(dl);
  728. __vsp1_dl_list_put(dlm->queued);
  729. dlm->queued = dl;
  730. }
  731. static void vsp1_dl_list_commit_singleshot(struct vsp1_dl_list *dl)
  732. {
  733. struct vsp1_dl_manager *dlm = dl->dlm;
  734. /*
  735. * When working in single-shot mode, the caller guarantees that the
  736. * hardware is idle at this point. Just commit the head display list
  737. * to hardware. Chained lists will be started automatically.
  738. */
  739. vsp1_dl_list_hw_enqueue(dl);
  740. dlm->active = dl;
  741. }
  742. void vsp1_dl_list_commit(struct vsp1_dl_list *dl, bool internal)
  743. {
  744. struct vsp1_dl_manager *dlm = dl->dlm;
  745. struct vsp1_dl_list *dl_next;
  746. unsigned long flags;
  747. /* Fill the header for the head and chained display lists. */
  748. vsp1_dl_list_fill_header(dl, list_empty(&dl->chain));
  749. list_for_each_entry(dl_next, &dl->chain, chain) {
  750. bool last = list_is_last(&dl_next->chain, &dl->chain);
  751. vsp1_dl_list_fill_header(dl_next, last);
  752. }
  753. dl->internal = internal;
  754. spin_lock_irqsave(&dlm->lock, flags);
  755. if (dlm->singleshot)
  756. vsp1_dl_list_commit_singleshot(dl);
  757. else
  758. vsp1_dl_list_commit_continuous(dl);
  759. spin_unlock_irqrestore(&dlm->lock, flags);
  760. }
  761. /* -----------------------------------------------------------------------------
  762. * Display List Manager
  763. */
  764. /**
  765. * vsp1_dlm_irq_frame_end - Display list handler for the frame end interrupt
  766. * @dlm: the display list manager
  767. *
  768. * Return a set of flags that indicates display list completion status.
  769. *
  770. * The VSP1_DL_FRAME_END_COMPLETED flag indicates that the previous display list
  771. * has completed at frame end. If the flag is not returned display list
  772. * completion has been delayed by one frame because the display list commit
  773. * raced with the frame end interrupt. The function always returns with the flag
  774. * set in single-shot mode as display list processing is then not continuous and
  775. * races never occur.
  776. *
  777. * The VSP1_DL_FRAME_END_INTERNAL flag indicates that the previous display list
  778. * has completed and had been queued with the internal notification flag.
  779. * Internal notification is only supported for continuous mode.
  780. */
  781. unsigned int vsp1_dlm_irq_frame_end(struct vsp1_dl_manager *dlm)
  782. {
  783. struct vsp1_device *vsp1 = dlm->vsp1;
  784. u32 status = vsp1_read(vsp1, VI6_STATUS);
  785. unsigned int flags = 0;
  786. spin_lock(&dlm->lock);
  787. /*
  788. * The mem-to-mem pipelines work in single-shot mode. No new display
  789. * list can be queued, we don't have to do anything.
  790. */
  791. if (dlm->singleshot) {
  792. __vsp1_dl_list_put(dlm->active);
  793. dlm->active = NULL;
  794. flags |= VSP1_DL_FRAME_END_COMPLETED;
  795. goto done;
  796. }
  797. /*
  798. * If the commit operation raced with the interrupt and occurred after
  799. * the frame end event but before interrupt processing, the hardware
  800. * hasn't taken the update into account yet. We have to skip one frame
  801. * and retry.
  802. */
  803. if (vsp1_dl_list_hw_update_pending(dlm))
  804. goto done;
  805. /*
  806. * Progressive streams report only TOP fields. If we have a BOTTOM
  807. * field, we are interlaced, and expect the frame to complete on the
  808. * next frame end interrupt.
  809. */
  810. if (status & VI6_STATUS_FLD_STD(dlm->index))
  811. goto done;
  812. /*
  813. * The device starts processing the queued display list right after the
  814. * frame end interrupt. The display list thus becomes active.
  815. */
  816. if (dlm->queued) {
  817. if (dlm->queued->internal)
  818. flags |= VSP1_DL_FRAME_END_INTERNAL;
  819. dlm->queued->internal = false;
  820. __vsp1_dl_list_put(dlm->active);
  821. dlm->active = dlm->queued;
  822. dlm->queued = NULL;
  823. flags |= VSP1_DL_FRAME_END_COMPLETED;
  824. }
  825. /*
  826. * Now that the VSP has started processing the queued display list, we
  827. * can queue the pending display list to the hardware if one has been
  828. * prepared.
  829. */
  830. if (dlm->pending) {
  831. vsp1_dl_list_hw_enqueue(dlm->pending);
  832. dlm->queued = dlm->pending;
  833. dlm->pending = NULL;
  834. }
  835. done:
  836. spin_unlock(&dlm->lock);
  837. return flags;
  838. }
  839. /* Hardware Setup */
  840. void vsp1_dlm_setup(struct vsp1_device *vsp1)
  841. {
  842. unsigned int i;
  843. u32 ctrl = (256 << VI6_DL_CTRL_AR_WAIT_SHIFT)
  844. | VI6_DL_CTRL_DC2 | VI6_DL_CTRL_DC1 | VI6_DL_CTRL_DC0
  845. | VI6_DL_CTRL_DLE;
  846. u32 ext_dl = (0x02 << VI6_DL_EXT_CTRL_POLINT_SHIFT)
  847. | VI6_DL_EXT_CTRL_DLPRI | VI6_DL_EXT_CTRL_EXT;
  848. if (vsp1_feature(vsp1, VSP1_HAS_EXT_DL)) {
  849. for (i = 0; i < vsp1->info->wpf_count; ++i)
  850. vsp1_write(vsp1, VI6_DL_EXT_CTRL(i), ext_dl);
  851. }
  852. vsp1_write(vsp1, VI6_DL_CTRL, ctrl);
  853. vsp1_write(vsp1, VI6_DL_SWAP, VI6_DL_SWAP_LWS);
  854. }
  855. void vsp1_dlm_reset(struct vsp1_dl_manager *dlm)
  856. {
  857. unsigned long flags;
  858. spin_lock_irqsave(&dlm->lock, flags);
  859. __vsp1_dl_list_put(dlm->active);
  860. __vsp1_dl_list_put(dlm->queued);
  861. __vsp1_dl_list_put(dlm->pending);
  862. spin_unlock_irqrestore(&dlm->lock, flags);
  863. dlm->active = NULL;
  864. dlm->queued = NULL;
  865. dlm->pending = NULL;
  866. }
  867. struct vsp1_dl_body *vsp1_dlm_dl_body_get(struct vsp1_dl_manager *dlm)
  868. {
  869. return vsp1_dl_body_get(dlm->pool);
  870. }
  871. struct vsp1_dl_manager *vsp1_dlm_create(struct vsp1_device *vsp1,
  872. unsigned int index,
  873. unsigned int prealloc)
  874. {
  875. struct vsp1_dl_manager *dlm;
  876. size_t header_size;
  877. unsigned int i;
  878. dlm = devm_kzalloc(vsp1->dev, sizeof(*dlm), GFP_KERNEL);
  879. if (!dlm)
  880. return NULL;
  881. dlm->index = index;
  882. dlm->singleshot = vsp1->info->uapi;
  883. dlm->vsp1 = vsp1;
  884. spin_lock_init(&dlm->lock);
  885. INIT_LIST_HEAD(&dlm->free);
  886. /*
  887. * Initialize the display list body and allocate DMA memory for the body
  888. * and the header. Both are allocated together to avoid memory
  889. * fragmentation, with the header located right after the body in
  890. * memory. An extra body is allocated on top of the prealloc to account
  891. * for the cached body used by the vsp1_pipeline object.
  892. */
  893. header_size = vsp1_feature(vsp1, VSP1_HAS_EXT_DL) ?
  894. sizeof(struct vsp1_dl_header_extended) :
  895. sizeof(struct vsp1_dl_header);
  896. header_size = ALIGN(header_size, 8);
  897. dlm->pool = vsp1_dl_body_pool_create(vsp1, prealloc + 1,
  898. VSP1_DL_NUM_ENTRIES, header_size);
  899. if (!dlm->pool)
  900. return NULL;
  901. for (i = 0; i < prealloc; ++i) {
  902. struct vsp1_dl_list *dl;
  903. dl = vsp1_dl_list_alloc(dlm);
  904. if (!dl) {
  905. vsp1_dlm_destroy(dlm);
  906. return NULL;
  907. }
  908. /* The extended header immediately follows the header. */
  909. if (vsp1_feature(vsp1, VSP1_HAS_EXT_DL))
  910. dl->extension = (void *)dl->header
  911. + sizeof(*dl->header);
  912. list_add_tail(&dl->list, &dlm->free);
  913. }
  914. if (vsp1_feature(vsp1, VSP1_HAS_EXT_DL)) {
  915. dlm->cmdpool = vsp1_dl_cmd_pool_create(vsp1,
  916. VSP1_EXTCMD_AUTOFLD, prealloc);
  917. if (!dlm->cmdpool) {
  918. vsp1_dlm_destroy(dlm);
  919. return NULL;
  920. }
  921. }
  922. return dlm;
  923. }
  924. void vsp1_dlm_destroy(struct vsp1_dl_manager *dlm)
  925. {
  926. struct vsp1_dl_list *dl, *next;
  927. if (!dlm)
  928. return;
  929. list_for_each_entry_safe(dl, next, &dlm->free, list) {
  930. list_del(&dl->list);
  931. vsp1_dl_list_free(dl);
  932. }
  933. vsp1_dl_body_pool_destroy(dlm->pool);
  934. vsp1_dl_ext_cmd_pool_destroy(dlm->cmdpool);
  935. }