saa7164-buffer.c 9.7 KB

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