saa7164-buffer.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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(*buf), GFP_KERNEL);
  92. if (!buf)
  93. goto ret;
  94. buf->idx = -1;
  95. buf->port = port;
  96. buf->flags = SAA7164_BUFFER_FREE;
  97. buf->pos = 0;
  98. buf->actual_size = params->pitch * params->numberoflines;
  99. buf->crc = 0;
  100. /* TODO: arg len is being ignored */
  101. buf->pci_size = SAA7164_PT_ENTRIES * 0x1000;
  102. buf->pt_size = (SAA7164_PT_ENTRIES * sizeof(u64)) + 0x1000;
  103. /* Allocate contiguous memory */
  104. buf->cpu = pci_alloc_consistent(port->dev->pci, buf->pci_size,
  105. &buf->dma);
  106. if (!buf->cpu)
  107. goto fail1;
  108. buf->pt_cpu = pci_alloc_consistent(port->dev->pci, buf->pt_size,
  109. &buf->pt_dma);
  110. if (!buf->pt_cpu)
  111. goto fail2;
  112. /* init the buffers to a known pattern, easier during debugging */
  113. memset(buf->cpu, 0xff, buf->pci_size);
  114. buf->crc = crc32(0, buf->cpu, buf->actual_size);
  115. memset(buf->pt_cpu, 0xff, buf->pt_size);
  116. dprintk(DBGLVL_BUF, "%s() allocated buffer @ 0x%p (%d pageptrs)\n",
  117. __func__, buf, params->numpagetables);
  118. dprintk(DBGLVL_BUF, " pci_cpu @ 0x%p dma @ 0x%08lx len = 0x%x\n",
  119. buf->cpu, (long)buf->dma, buf->pci_size);
  120. dprintk(DBGLVL_BUF, " pt_cpu @ 0x%p pt_dma @ 0x%08lx len = 0x%x\n",
  121. buf->pt_cpu, (long)buf->pt_dma, buf->pt_size);
  122. /* Format the Page Table Entries to point into the data buffer */
  123. for (i = 0 ; i < params->numpagetables; i++) {
  124. *(buf->pt_cpu + i) = buf->dma + (i * 0x1000); /* TODO */
  125. dprintk(DBGLVL_BUF, " pt[%02d] = 0x%p -> 0x%llx\n",
  126. i, buf->pt_cpu, (u64)*(buf->pt_cpu));
  127. }
  128. goto ret;
  129. fail2:
  130. pci_free_consistent(port->dev->pci, buf->pci_size, buf->cpu, buf->dma);
  131. fail1:
  132. kfree(buf);
  133. buf = NULL;
  134. ret:
  135. return buf;
  136. }
  137. int saa7164_buffer_dealloc(struct saa7164_buffer *buf)
  138. {
  139. struct saa7164_dev *dev;
  140. if (!buf || !buf->port)
  141. return SAA_ERR_BAD_PARAMETER;
  142. dev = buf->port->dev;
  143. dprintk(DBGLVL_BUF, "%s() deallocating buffer @ 0x%p\n",
  144. __func__, buf);
  145. if (buf->flags != SAA7164_BUFFER_FREE)
  146. log_warn(" freeing a non-free buffer\n");
  147. pci_free_consistent(dev->pci, buf->pci_size, buf->cpu, buf->dma);
  148. pci_free_consistent(dev->pci, buf->pt_size, buf->pt_cpu, buf->pt_dma);
  149. kfree(buf);
  150. return SAA_OK;
  151. }
  152. int saa7164_buffer_zero_offsets(struct saa7164_port *port, int i)
  153. {
  154. struct saa7164_dev *dev = port->dev;
  155. if ((i < 0) || (i >= port->hwcfg.buffercount))
  156. return -EINVAL;
  157. dprintk(DBGLVL_BUF, "%s(idx = %d)\n", __func__, i);
  158. saa7164_writel(port->bufoffset + (sizeof(u32) * i), 0);
  159. return 0;
  160. }
  161. /* Write a buffer into the hardware */
  162. int saa7164_buffer_activate(struct saa7164_buffer *buf, int i)
  163. {
  164. struct saa7164_port *port = buf->port;
  165. struct saa7164_dev *dev = port->dev;
  166. if ((i < 0) || (i >= port->hwcfg.buffercount))
  167. return -EINVAL;
  168. dprintk(DBGLVL_BUF, "%s(idx = %d)\n", __func__, i);
  169. buf->idx = i; /* Note of which buffer list index position we occupy */
  170. buf->flags = SAA7164_BUFFER_BUSY;
  171. buf->pos = 0;
  172. /* TODO: Review this in light of 32v64 assignments */
  173. saa7164_writel(port->bufoffset + (sizeof(u32) * i), 0);
  174. saa7164_writel(port->bufptr32h + ((sizeof(u32) * 2) * i), buf->pt_dma);
  175. saa7164_writel(port->bufptr32l + ((sizeof(u32) * 2) * i), 0);
  176. dprintk(DBGLVL_BUF, " buf[%d] offset 0x%llx (0x%x) buf 0x%llx/%llx (0x%x/%x) nr=%d\n",
  177. buf->idx,
  178. (u64)port->bufoffset + (i * sizeof(u32)),
  179. saa7164_readl(port->bufoffset + (sizeof(u32) * i)),
  180. (u64)port->bufptr32h + ((sizeof(u32) * 2) * i),
  181. (u64)port->bufptr32l + ((sizeof(u32) * 2) * i),
  182. saa7164_readl(port->bufptr32h + ((sizeof(u32) * i) * 2)),
  183. saa7164_readl(port->bufptr32l + ((sizeof(u32) * i) * 2)),
  184. buf->idx);
  185. return 0;
  186. }
  187. int saa7164_buffer_cfg_port(struct saa7164_port *port)
  188. {
  189. struct tmHWStreamParameters *params = &port->hw_streamingparams;
  190. struct saa7164_dev *dev = port->dev;
  191. struct saa7164_buffer *buf;
  192. struct list_head *c, *n;
  193. int i = 0;
  194. dprintk(DBGLVL_BUF, "%s(port=%d)\n", __func__, port->nr);
  195. saa7164_writel(port->bufcounter, 0);
  196. saa7164_writel(port->pitch, params->pitch);
  197. saa7164_writel(port->bufsize, params->pitch * params->numberoflines);
  198. dprintk(DBGLVL_BUF, " configured:\n");
  199. dprintk(DBGLVL_BUF, " lmmio 0x%p\n", dev->lmmio);
  200. dprintk(DBGLVL_BUF, " bufcounter 0x%x = 0x%x\n", port->bufcounter,
  201. saa7164_readl(port->bufcounter));
  202. dprintk(DBGLVL_BUF, " pitch 0x%x = %d\n", port->pitch,
  203. saa7164_readl(port->pitch));
  204. dprintk(DBGLVL_BUF, " bufsize 0x%x = %d\n", port->bufsize,
  205. saa7164_readl(port->bufsize));
  206. dprintk(DBGLVL_BUF, " buffercount = %d\n", port->hwcfg.buffercount);
  207. dprintk(DBGLVL_BUF, " bufoffset = 0x%x\n", port->bufoffset);
  208. dprintk(DBGLVL_BUF, " bufptr32h = 0x%x\n", port->bufptr32h);
  209. dprintk(DBGLVL_BUF, " bufptr32l = 0x%x\n", port->bufptr32l);
  210. /* Poke the buffers and offsets into PCI space */
  211. mutex_lock(&port->dmaqueue_lock);
  212. list_for_each_safe(c, n, &port->dmaqueue.list) {
  213. buf = list_entry(c, struct saa7164_buffer, list);
  214. if (buf->flags != SAA7164_BUFFER_FREE)
  215. BUG();
  216. /* Place the buffer in the h/w queue */
  217. saa7164_buffer_activate(buf, i);
  218. /* Don't exceed the device maximum # bufs */
  219. if (i++ > port->hwcfg.buffercount)
  220. BUG();
  221. }
  222. mutex_unlock(&port->dmaqueue_lock);
  223. return 0;
  224. }
  225. struct saa7164_user_buffer *saa7164_buffer_alloc_user(struct saa7164_dev *dev,
  226. u32 len)
  227. {
  228. struct saa7164_user_buffer *buf;
  229. buf = kzalloc(sizeof(*buf), GFP_KERNEL);
  230. if (!buf)
  231. return NULL;
  232. buf->data = kzalloc(len, GFP_KERNEL);
  233. if (!buf->data) {
  234. kfree(buf);
  235. return NULL;
  236. }
  237. buf->actual_size = len;
  238. buf->pos = 0;
  239. buf->crc = 0;
  240. dprintk(DBGLVL_BUF, "%s() allocated user buffer @ 0x%p\n",
  241. __func__, buf);
  242. return buf;
  243. }
  244. void saa7164_buffer_dealloc_user(struct saa7164_user_buffer *buf)
  245. {
  246. if (!buf)
  247. return;
  248. kfree(buf->data);
  249. buf->data = NULL;
  250. kfree(buf);
  251. }