saa7164-buffer.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. /* TODO: arg len is being ignored */
  105. buf->pci_size = SAA7164_PT_ENTRIES * 0x1000;
  106. buf->pt_size = (SAA7164_PT_ENTRIES * sizeof(u64)) + 0x1000;
  107. /* Allocate contiguous memory */
  108. buf->cpu = pci_alloc_consistent(port->dev->pci, buf->pci_size,
  109. &buf->dma);
  110. if (!buf->cpu)
  111. goto fail1;
  112. buf->pt_cpu = pci_alloc_consistent(port->dev->pci, buf->pt_size,
  113. &buf->pt_dma);
  114. if (!buf->pt_cpu)
  115. goto fail2;
  116. /* init the buffers to a known pattern, easier during debugging */
  117. memset(buf->cpu, 0xff, buf->pci_size);
  118. memset(buf->pt_cpu, 0xff, buf->pt_size);
  119. dprintk(DBGLVL_BUF, "%s() allocated buffer @ 0x%p\n",
  120. __func__, buf);
  121. dprintk(DBGLVL_BUF, " pci_cpu @ 0x%p dma @ 0x%08lx len = 0x%x\n",
  122. buf->cpu, (long)buf->dma, buf->pci_size);
  123. dprintk(DBGLVL_BUF, " pt_cpu @ 0x%p pt_dma @ 0x%08lx len = 0x%x\n",
  124. buf->pt_cpu, (long)buf->pt_dma, buf->pt_size);
  125. /* Format the Page Table Entries to point into the data buffer */
  126. for (i = 0 ; i < SAA7164_PT_ENTRIES; i++) {
  127. *(buf->pt_cpu + i) = buf->dma + (i * 0x1000); /* TODO */
  128. }
  129. goto ret;
  130. fail2:
  131. pci_free_consistent(port->dev->pci, buf->pci_size, buf->cpu, buf->dma);
  132. fail1:
  133. kfree(buf);
  134. buf = 0;
  135. ret:
  136. return buf;
  137. }
  138. int saa7164_buffer_dealloc(struct saa7164_buffer *buf)
  139. {
  140. struct saa7164_dev *dev;
  141. if (!buf || !buf->port)
  142. return SAA_ERR_BAD_PARAMETER;
  143. dev = buf->port->dev;
  144. dprintk(DBGLVL_BUF, "%s() deallocating buffer @ 0x%p\n",
  145. __func__, buf);
  146. if (buf->flags != SAA7164_BUFFER_FREE)
  147. log_warn(" freeing a non-free buffer\n");
  148. pci_free_consistent(dev->pci, buf->pci_size, buf->cpu, buf->dma);
  149. pci_free_consistent(dev->pci, buf->pt_size, buf->pt_cpu, buf->pt_dma);
  150. kfree(buf);
  151. return SAA_OK;
  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) "
  169. "buf 0x%llx/%llx (0x%x/%x) nr=%d\n",
  170. buf->idx,
  171. (u64)port->bufoffset + (i * sizeof(u32)),
  172. saa7164_readl(port->bufoffset + (sizeof(u32) * i)),
  173. (u64)port->bufptr32h + ((sizeof(u32) * 2) * i),
  174. (u64)port->bufptr32l + ((sizeof(u32) * 2) * i),
  175. saa7164_readl(port->bufptr32h + ((sizeof(u32) * i) * 2)),
  176. saa7164_readl(port->bufptr32l + ((sizeof(u32) * i) * 2)),
  177. buf->idx);
  178. return 0;
  179. }
  180. int saa7164_buffer_cfg_port(struct saa7164_port *port)
  181. {
  182. tmHWStreamParameters_t *params = &port->hw_streamingparams;
  183. struct saa7164_dev *dev = port->dev;
  184. struct saa7164_buffer *buf;
  185. struct list_head *c, *n;
  186. int i = 0;
  187. dprintk(DBGLVL_BUF, "%s(port=%d)\n", __func__, port->nr);
  188. saa7164_writel(port->bufcounter, 0);
  189. saa7164_writel(port->pitch, params->pitch);
  190. saa7164_writel(port->bufsize, params->pitch * params->numberoflines);
  191. dprintk(DBGLVL_BUF, " configured:\n");
  192. dprintk(DBGLVL_BUF, " lmmio 0x%p\n", dev->lmmio);
  193. dprintk(DBGLVL_BUF, " bufcounter 0x%x = 0x%x\n", port->bufcounter,
  194. saa7164_readl(port->bufcounter));
  195. dprintk(DBGLVL_BUF, " pitch 0x%x = %d\n", port->pitch,
  196. saa7164_readl(port->pitch));
  197. dprintk(DBGLVL_BUF, " bufsize 0x%x = %d\n", port->bufsize,
  198. saa7164_readl(port->bufsize));
  199. dprintk(DBGLVL_BUF, " buffercount = %d\n", port->hwcfg.buffercount);
  200. dprintk(DBGLVL_BUF, " bufoffset = 0x%x\n", port->bufoffset);
  201. dprintk(DBGLVL_BUF, " bufptr32h = 0x%x\n", port->bufptr32h);
  202. dprintk(DBGLVL_BUF, " bufptr32l = 0x%x\n", port->bufptr32l);
  203. /* Poke the buffers and offsets into PCI space */
  204. mutex_lock(&port->dmaqueue_lock);
  205. list_for_each_safe(c, n, &port->dmaqueue.list) {
  206. buf = list_entry(c, struct saa7164_buffer, list);
  207. if (buf->flags != SAA7164_BUFFER_FREE)
  208. BUG();
  209. /* Place the buffer in the h/w queue */
  210. saa7164_buffer_activate(buf, i);
  211. /* Don't exceed the device maximum # bufs */
  212. if (i++ > port->hwcfg.buffercount)
  213. BUG();
  214. }
  215. mutex_unlock(&port->dmaqueue_lock);
  216. return 0;
  217. }