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