saa7164-buffer.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Driver for the NXP SAA7164 PCIe bridge
  4. *
  5. * Copyright (c) 2010-2015 Steven Toth <[email protected]>
  6. */
  7. #include <linux/slab.h>
  8. #include "saa7164.h"
  9. /* The PCI address space for buffer handling looks like this:
  10. *
  11. * +-u32 wide-------------+
  12. * | +
  13. * +-u64 wide------------------------------------+
  14. * + +
  15. * +----------------------+
  16. * | CurrentBufferPtr + Pointer to current PCI buffer >-+
  17. * +----------------------+ |
  18. * | Unused + |
  19. * +----------------------+ |
  20. * | Pitch + = 188 (bytes) |
  21. * +----------------------+ |
  22. * | PCI buffer size + = pitch * number of lines (312) |
  23. * +----------------------+ |
  24. * |0| Buf0 Write Offset + |
  25. * +----------------------+ v
  26. * |1| Buf1 Write Offset + |
  27. * +----------------------+ |
  28. * |2| Buf2 Write Offset + |
  29. * +----------------------+ |
  30. * |3| Buf3 Write Offset + |
  31. * +----------------------+ |
  32. * ... More write offsets |
  33. * +---------------------------------------------+ |
  34. * +0| set of ptrs to PCI pagetables + |
  35. * +---------------------------------------------+ |
  36. * +1| set of ptrs to PCI pagetables + <--------+
  37. * +---------------------------------------------+
  38. * +2| set of ptrs to PCI pagetables +
  39. * +---------------------------------------------+
  40. * +3| set of ptrs to PCI pagetables + >--+
  41. * +---------------------------------------------+ |
  42. * ... More buffer pointers | +----------------+
  43. * +->| pt[0] TS data |
  44. * | +----------------+
  45. * |
  46. * | +----------------+
  47. * +->| pt[1] TS data |
  48. * | +----------------+
  49. * | etc
  50. */
  51. void saa7164_buffer_display(struct saa7164_buffer *buf)
  52. {
  53. struct saa7164_dev *dev = buf->port->dev;
  54. int i;
  55. dprintk(DBGLVL_BUF, "%s() buffer @ 0x%p nr=%d\n",
  56. __func__, buf, buf->idx);
  57. dprintk(DBGLVL_BUF, " pci_cpu @ 0x%p dma @ 0x%08llx len = 0x%x\n",
  58. buf->cpu, (long long)buf->dma, buf->pci_size);
  59. dprintk(DBGLVL_BUF, " pt_cpu @ 0x%p pt_dma @ 0x%08llx len = 0x%x\n",
  60. buf->pt_cpu, (long long)buf->pt_dma, buf->pt_size);
  61. /* Format the Page Table Entries to point into the data buffer */
  62. for (i = 0 ; i < SAA7164_PT_ENTRIES; i++) {
  63. dprintk(DBGLVL_BUF, " pt[%02d] = 0x%p -> 0x%llx\n",
  64. i, buf->pt_cpu, (u64)*(buf->pt_cpu));
  65. }
  66. }
  67. /* Allocate a new buffer structure and associated PCI space in bytes.
  68. * len must be a multiple of sizeof(u64)
  69. */
  70. struct saa7164_buffer *saa7164_buffer_alloc(struct saa7164_port *port,
  71. u32 len)
  72. {
  73. struct tmHWStreamParameters *params = &port->hw_streamingparams;
  74. struct saa7164_buffer *buf = NULL;
  75. struct saa7164_dev *dev = port->dev;
  76. int i;
  77. if ((len == 0) || (len >= 65536) || (len % sizeof(u64))) {
  78. log_warn("%s() SAA_ERR_BAD_PARAMETER\n", __func__);
  79. goto ret;
  80. }
  81. buf = kzalloc(sizeof(*buf), GFP_KERNEL);
  82. if (!buf)
  83. goto ret;
  84. buf->idx = -1;
  85. buf->port = port;
  86. buf->flags = SAA7164_BUFFER_FREE;
  87. buf->pos = 0;
  88. buf->actual_size = params->pitch * params->numberoflines;
  89. buf->crc = 0;
  90. /* TODO: arg len is being ignored */
  91. buf->pci_size = SAA7164_PT_ENTRIES * 0x1000;
  92. buf->pt_size = (SAA7164_PT_ENTRIES * sizeof(u64)) + 0x1000;
  93. /* Allocate contiguous memory */
  94. buf->cpu = dma_alloc_coherent(&port->dev->pci->dev, buf->pci_size,
  95. &buf->dma, GFP_KERNEL);
  96. if (!buf->cpu)
  97. goto fail1;
  98. buf->pt_cpu = dma_alloc_coherent(&port->dev->pci->dev, buf->pt_size,
  99. &buf->pt_dma, GFP_KERNEL);
  100. if (!buf->pt_cpu)
  101. goto fail2;
  102. /* init the buffers to a known pattern, easier during debugging */
  103. memset(buf->cpu, 0xff, buf->pci_size);
  104. buf->crc = crc32(0, buf->cpu, buf->actual_size);
  105. memset(buf->pt_cpu, 0xff, buf->pt_size);
  106. dprintk(DBGLVL_BUF, "%s() allocated buffer @ 0x%p (%d pageptrs)\n",
  107. __func__, buf, params->numpagetables);
  108. dprintk(DBGLVL_BUF, " pci_cpu @ 0x%p dma @ 0x%08lx len = 0x%x\n",
  109. buf->cpu, (long)buf->dma, buf->pci_size);
  110. dprintk(DBGLVL_BUF, " pt_cpu @ 0x%p pt_dma @ 0x%08lx len = 0x%x\n",
  111. buf->pt_cpu, (long)buf->pt_dma, buf->pt_size);
  112. /* Format the Page Table Entries to point into the data buffer */
  113. for (i = 0 ; i < params->numpagetables; i++) {
  114. *(buf->pt_cpu + i) = buf->dma + (i * 0x1000); /* TODO */
  115. dprintk(DBGLVL_BUF, " pt[%02d] = 0x%p -> 0x%llx\n",
  116. i, buf->pt_cpu, (u64)*(buf->pt_cpu));
  117. }
  118. goto ret;
  119. fail2:
  120. dma_free_coherent(&port->dev->pci->dev, buf->pci_size, buf->cpu,
  121. buf->dma);
  122. fail1:
  123. kfree(buf);
  124. buf = NULL;
  125. ret:
  126. return buf;
  127. }
  128. int saa7164_buffer_dealloc(struct saa7164_buffer *buf)
  129. {
  130. struct saa7164_dev *dev;
  131. if (!buf || !buf->port)
  132. return SAA_ERR_BAD_PARAMETER;
  133. dev = buf->port->dev;
  134. dprintk(DBGLVL_BUF, "%s() deallocating buffer @ 0x%p\n",
  135. __func__, buf);
  136. if (buf->flags != SAA7164_BUFFER_FREE)
  137. log_warn(" freeing a non-free buffer\n");
  138. dma_free_coherent(&dev->pci->dev, buf->pci_size, buf->cpu, buf->dma);
  139. dma_free_coherent(&dev->pci->dev, buf->pt_size, buf->pt_cpu,
  140. buf->pt_dma);
  141. kfree(buf);
  142. return SAA_OK;
  143. }
  144. int saa7164_buffer_zero_offsets(struct saa7164_port *port, int i)
  145. {
  146. struct saa7164_dev *dev = port->dev;
  147. if ((i < 0) || (i >= port->hwcfg.buffercount))
  148. return -EINVAL;
  149. dprintk(DBGLVL_BUF, "%s(idx = %d)\n", __func__, i);
  150. saa7164_writel(port->bufoffset + (sizeof(u32) * i), 0);
  151. return 0;
  152. }
  153. /* Write a buffer into the hardware */
  154. int saa7164_buffer_activate(struct saa7164_buffer *buf, int i)
  155. {
  156. struct saa7164_port *port = buf->port;
  157. struct saa7164_dev *dev = port->dev;
  158. if ((i < 0) || (i >= port->hwcfg.buffercount))
  159. return -EINVAL;
  160. dprintk(DBGLVL_BUF, "%s(idx = %d)\n", __func__, i);
  161. buf->idx = i; /* Note of which buffer list index position we occupy */
  162. buf->flags = SAA7164_BUFFER_BUSY;
  163. buf->pos = 0;
  164. /* TODO: Review this in light of 32v64 assignments */
  165. saa7164_writel(port->bufoffset + (sizeof(u32) * i), 0);
  166. saa7164_writel(port->bufptr32h + ((sizeof(u32) * 2) * i), buf->pt_dma);
  167. saa7164_writel(port->bufptr32l + ((sizeof(u32) * 2) * i), 0);
  168. dprintk(DBGLVL_BUF, " buf[%d] offset 0x%llx (0x%x) buf 0x%llx/%llx (0x%x/%x) nr=%d\n",
  169. buf->idx,
  170. (u64)port->bufoffset + (i * sizeof(u32)),
  171. saa7164_readl(port->bufoffset + (sizeof(u32) * i)),
  172. (u64)port->bufptr32h + ((sizeof(u32) * 2) * i),
  173. (u64)port->bufptr32l + ((sizeof(u32) * 2) * i),
  174. saa7164_readl(port->bufptr32h + ((sizeof(u32) * i) * 2)),
  175. saa7164_readl(port->bufptr32l + ((sizeof(u32) * i) * 2)),
  176. buf->idx);
  177. return 0;
  178. }
  179. int saa7164_buffer_cfg_port(struct saa7164_port *port)
  180. {
  181. struct tmHWStreamParameters *params = &port->hw_streamingparams;
  182. struct saa7164_dev *dev = port->dev;
  183. struct saa7164_buffer *buf;
  184. struct list_head *c, *n;
  185. int i = 0;
  186. dprintk(DBGLVL_BUF, "%s(port=%d)\n", __func__, port->nr);
  187. saa7164_writel(port->bufcounter, 0);
  188. saa7164_writel(port->pitch, params->pitch);
  189. saa7164_writel(port->bufsize, params->pitch * params->numberoflines);
  190. dprintk(DBGLVL_BUF, " configured:\n");
  191. dprintk(DBGLVL_BUF, " lmmio 0x%p\n", dev->lmmio);
  192. dprintk(DBGLVL_BUF, " bufcounter 0x%x = 0x%x\n", port->bufcounter,
  193. saa7164_readl(port->bufcounter));
  194. dprintk(DBGLVL_BUF, " pitch 0x%x = %d\n", port->pitch,
  195. saa7164_readl(port->pitch));
  196. dprintk(DBGLVL_BUF, " bufsize 0x%x = %d\n", port->bufsize,
  197. saa7164_readl(port->bufsize));
  198. dprintk(DBGLVL_BUF, " buffercount = %d\n", port->hwcfg.buffercount);
  199. dprintk(DBGLVL_BUF, " bufoffset = 0x%x\n", port->bufoffset);
  200. dprintk(DBGLVL_BUF, " bufptr32h = 0x%x\n", port->bufptr32h);
  201. dprintk(DBGLVL_BUF, " bufptr32l = 0x%x\n", port->bufptr32l);
  202. /* Poke the buffers and offsets into PCI space */
  203. mutex_lock(&port->dmaqueue_lock);
  204. list_for_each_safe(c, n, &port->dmaqueue.list) {
  205. buf = list_entry(c, struct saa7164_buffer, list);
  206. BUG_ON(buf->flags != SAA7164_BUFFER_FREE);
  207. /* Place the buffer in the h/w queue */
  208. saa7164_buffer_activate(buf, i);
  209. /* Don't exceed the device maximum # bufs */
  210. BUG_ON(i > port->hwcfg.buffercount);
  211. i++;
  212. }
  213. mutex_unlock(&port->dmaqueue_lock);
  214. return 0;
  215. }
  216. struct saa7164_user_buffer *saa7164_buffer_alloc_user(struct saa7164_dev *dev,
  217. u32 len)
  218. {
  219. struct saa7164_user_buffer *buf;
  220. buf = kzalloc(sizeof(*buf), GFP_KERNEL);
  221. if (!buf)
  222. return NULL;
  223. buf->data = kzalloc(len, GFP_KERNEL);
  224. if (!buf->data) {
  225. kfree(buf);
  226. return NULL;
  227. }
  228. buf->actual_size = len;
  229. buf->pos = 0;
  230. buf->crc = 0;
  231. dprintk(DBGLVL_BUF, "%s() allocated user buffer @ 0x%p\n",
  232. __func__, buf);
  233. return buf;
  234. }
  235. void saa7164_buffer_dealloc_user(struct saa7164_user_buffer *buf)
  236. {
  237. if (!buf)
  238. return;
  239. kfree(buf->data);
  240. buf->data = NULL;
  241. kfree(buf);
  242. }