saa7164-buffer.c 9.5 KB

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