saa7164-buffer.c 9.5 KB

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