ivtv-udma.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. User DMA
  3. Copyright (C) 2003-2004 Kevin Thayer <nufan_wfk at yahoo.com>
  4. Copyright (C) 2004 Chris Kennedy <c@groovy.org>
  5. Copyright (C) 2005-2007 Hans Verkuil <hverkuil@xs4all.nl>
  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. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. */
  18. #include "ivtv-driver.h"
  19. #include "ivtv-udma.h"
  20. void ivtv_udma_get_page_info(struct ivtv_dma_page_info *dma_page, unsigned long first, unsigned long size)
  21. {
  22. dma_page->uaddr = first & PAGE_MASK;
  23. dma_page->offset = first & ~PAGE_MASK;
  24. dma_page->tail = 1 + ((first+size-1) & ~PAGE_MASK);
  25. dma_page->first = (first & PAGE_MASK) >> PAGE_SHIFT;
  26. dma_page->last = ((first+size-1) & PAGE_MASK) >> PAGE_SHIFT;
  27. dma_page->page_count = dma_page->last - dma_page->first + 1;
  28. if (dma_page->page_count == 1) dma_page->tail -= dma_page->offset;
  29. }
  30. int ivtv_udma_fill_sg_list (struct ivtv_user_dma *dma, struct ivtv_dma_page_info *dma_page, int map_offset)
  31. {
  32. int i, offset;
  33. unsigned long flags;
  34. if (map_offset < 0)
  35. return map_offset;
  36. offset = dma_page->offset;
  37. /* Fill SG Array with new values */
  38. for (i = 0; i < dma_page->page_count; i++) {
  39. unsigned int len = (i == dma_page->page_count - 1) ?
  40. dma_page->tail : PAGE_SIZE - offset;
  41. if (PageHighMem(dma->map[map_offset])) {
  42. void *src;
  43. if (dma->bouncemap[map_offset] == NULL)
  44. dma->bouncemap[map_offset] = alloc_page(GFP_KERNEL);
  45. if (dma->bouncemap[map_offset] == NULL)
  46. return -1;
  47. local_irq_save(flags);
  48. src = kmap_atomic(dma->map[map_offset]) + offset;
  49. memcpy(page_address(dma->bouncemap[map_offset]) + offset, src, len);
  50. kunmap_atomic(src);
  51. local_irq_restore(flags);
  52. sg_set_page(&dma->SGlist[map_offset], dma->bouncemap[map_offset], len, offset);
  53. }
  54. else {
  55. sg_set_page(&dma->SGlist[map_offset], dma->map[map_offset], len, offset);
  56. }
  57. offset = 0;
  58. map_offset++;
  59. }
  60. return map_offset;
  61. }
  62. void ivtv_udma_fill_sg_array (struct ivtv_user_dma *dma, u32 buffer_offset, u32 buffer_offset_2, u32 split) {
  63. int i;
  64. struct scatterlist *sg;
  65. for (i = 0, sg = dma->SGlist; i < dma->SG_length; i++, sg = sg_next(sg)) {
  66. dma->SGarray[i].size = cpu_to_le32(sg_dma_len(sg));
  67. dma->SGarray[i].src = cpu_to_le32(sg_dma_address(sg));
  68. dma->SGarray[i].dst = cpu_to_le32(buffer_offset);
  69. buffer_offset += sg_dma_len(sg);
  70. split -= sg_dma_len(sg);
  71. if (split == 0)
  72. buffer_offset = buffer_offset_2;
  73. }
  74. }
  75. /* User DMA Buffers */
  76. void ivtv_udma_alloc(struct ivtv *itv)
  77. {
  78. if (itv->udma.SG_handle == 0) {
  79. /* Map DMA Page Array Buffer */
  80. itv->udma.SG_handle = pci_map_single(itv->pdev, itv->udma.SGarray,
  81. sizeof(itv->udma.SGarray), PCI_DMA_TODEVICE);
  82. ivtv_udma_sync_for_cpu(itv);
  83. }
  84. }
  85. int ivtv_udma_setup(struct ivtv *itv, unsigned long ivtv_dest_addr,
  86. void __user *userbuf, int size_in_bytes)
  87. {
  88. struct ivtv_dma_page_info user_dma;
  89. struct ivtv_user_dma *dma = &itv->udma;
  90. int i, err;
  91. IVTV_DEBUG_DMA("ivtv_udma_setup, dst: 0x%08x\n", (unsigned int)ivtv_dest_addr);
  92. /* Still in USE */
  93. if (dma->SG_length || dma->page_count) {
  94. IVTV_DEBUG_WARN("ivtv_udma_setup: SG_length %d page_count %d still full?\n",
  95. dma->SG_length, dma->page_count);
  96. return -EBUSY;
  97. }
  98. ivtv_udma_get_page_info(&user_dma, (unsigned long)userbuf, size_in_bytes);
  99. if (user_dma.page_count <= 0) {
  100. IVTV_DEBUG_WARN("ivtv_udma_setup: Error %d page_count from %d bytes %d offset\n",
  101. user_dma.page_count, size_in_bytes, user_dma.offset);
  102. return -EINVAL;
  103. }
  104. /* Get user pages for DMA Xfer */
  105. err = get_user_pages_unlocked(current, current->mm,
  106. user_dma.uaddr, user_dma.page_count, dma->map,
  107. FOLL_FORCE);
  108. if (user_dma.page_count != err) {
  109. IVTV_DEBUG_WARN("failed to map user pages, returned %d instead of %d\n",
  110. err, user_dma.page_count);
  111. if (err >= 0) {
  112. for (i = 0; i < err; i++)
  113. put_page(dma->map[i]);
  114. return -EINVAL;
  115. }
  116. return err;
  117. }
  118. dma->page_count = user_dma.page_count;
  119. /* Fill SG List with new values */
  120. if (ivtv_udma_fill_sg_list(dma, &user_dma, 0) < 0) {
  121. for (i = 0; i < dma->page_count; i++) {
  122. put_page(dma->map[i]);
  123. }
  124. dma->page_count = 0;
  125. return -ENOMEM;
  126. }
  127. /* Map SG List */
  128. dma->SG_length = pci_map_sg(itv->pdev, dma->SGlist, dma->page_count, PCI_DMA_TODEVICE);
  129. /* Fill SG Array with new values */
  130. ivtv_udma_fill_sg_array (dma, ivtv_dest_addr, 0, -1);
  131. /* Tag SG Array with Interrupt Bit */
  132. dma->SGarray[dma->SG_length - 1].size |= cpu_to_le32(0x80000000);
  133. ivtv_udma_sync_for_device(itv);
  134. return dma->page_count;
  135. }
  136. void ivtv_udma_unmap(struct ivtv *itv)
  137. {
  138. struct ivtv_user_dma *dma = &itv->udma;
  139. int i;
  140. IVTV_DEBUG_INFO("ivtv_unmap_user_dma\n");
  141. /* Nothing to free */
  142. if (dma->page_count == 0)
  143. return;
  144. /* Unmap Scatterlist */
  145. if (dma->SG_length) {
  146. pci_unmap_sg(itv->pdev, dma->SGlist, dma->page_count, PCI_DMA_TODEVICE);
  147. dma->SG_length = 0;
  148. }
  149. /* sync DMA */
  150. ivtv_udma_sync_for_cpu(itv);
  151. /* Release User Pages */
  152. for (i = 0; i < dma->page_count; i++) {
  153. put_page(dma->map[i]);
  154. }
  155. dma->page_count = 0;
  156. }
  157. void ivtv_udma_free(struct ivtv *itv)
  158. {
  159. int i;
  160. /* Unmap SG Array */
  161. if (itv->udma.SG_handle) {
  162. pci_unmap_single(itv->pdev, itv->udma.SG_handle,
  163. sizeof(itv->udma.SGarray), PCI_DMA_TODEVICE);
  164. }
  165. /* Unmap Scatterlist */
  166. if (itv->udma.SG_length) {
  167. pci_unmap_sg(itv->pdev, itv->udma.SGlist, itv->udma.page_count, PCI_DMA_TODEVICE);
  168. }
  169. for (i = 0; i < IVTV_DMA_SG_OSD_ENT; i++) {
  170. if (itv->udma.bouncemap[i])
  171. __free_page(itv->udma.bouncemap[i]);
  172. }
  173. }
  174. void ivtv_udma_start(struct ivtv *itv)
  175. {
  176. IVTV_DEBUG_DMA("start UDMA\n");
  177. write_reg(itv->udma.SG_handle, IVTV_REG_DECDMAADDR);
  178. write_reg_sync(read_reg(IVTV_REG_DMAXFER) | 0x01, IVTV_REG_DMAXFER);
  179. set_bit(IVTV_F_I_DMA, &itv->i_flags);
  180. set_bit(IVTV_F_I_UDMA, &itv->i_flags);
  181. clear_bit(IVTV_F_I_UDMA_PENDING, &itv->i_flags);
  182. }
  183. void ivtv_udma_prepare(struct ivtv *itv)
  184. {
  185. unsigned long flags;
  186. spin_lock_irqsave(&itv->dma_reg_lock, flags);
  187. if (!test_bit(IVTV_F_I_DMA, &itv->i_flags))
  188. ivtv_udma_start(itv);
  189. else
  190. set_bit(IVTV_F_I_UDMA_PENDING, &itv->i_flags);
  191. spin_unlock_irqrestore(&itv->dma_reg_lock, flags);
  192. }