swiotlb.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:14k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /*
  2.  * Dynamic DMA mapping support.
  3.  *
  4.  * This implementation is for IA-64 platforms that do not support
  5.  * I/O TLBs (aka DMA address translation hardware).
  6.  * Copyright (C) 2000 Asit Mallick <Asit.K.Mallick@intel.com>
  7.  * Copyright (C) 2000 Goutham Rao <goutham.rao@intel.com>
  8.  *
  9.  * 00/12/13 davidm Rename to swiotlb.c and add mark_clean() to avoid
  10.  * unnecessary i-cache flushing.
  11.  */
  12. #include <linux/mm.h>
  13. #include <linux/module.h>
  14. #include <linux/pci.h>
  15. #include <linux/spinlock.h>
  16. #include <linux/string.h>
  17. #include <linux/types.h>
  18. #include <asm/io.h>
  19. #include <asm/pci.h>
  20. #include <asm/dma.h>
  21. #include <linux/init.h>
  22. #include <linux/bootmem.h>
  23. #define ALIGN(val, align) ((unsigned long)
  24. (((unsigned long) (val) + ((align) - 1)) & ~((align) - 1)))
  25. #define OFFSET(val,align) ((unsigned long)
  26.                    ( (val) & ( (align) - 1)))
  27. #define SG_ENT_VIRT_ADDRESS(sg) ((sg)->address ? (sg)->address
  28.  : page_address((sg)->page) + (sg)->offset)
  29. #define SG_ENT_PHYS_ADDRESS(SG) virt_to_phys(SG_ENT_VIRT_ADDRESS(SG))
  30. /*
  31.  * Maximum allowable number of contiguous slabs to map,
  32.  * must be a power of 2.  What is the appropriate value ?
  33.  * The complexity of {map,unmap}_single is linearly dependent on this value.
  34.  */
  35. #define IO_TLB_SEGSIZE 128
  36. /*
  37.  * log of the size of each IO TLB slab.  The number of slabs is command line controllable.
  38.  */
  39. #define IO_TLB_SHIFT 11
  40. /*
  41.  * Used to do a quick range check in swiotlb_unmap_single and swiotlb_sync_single, to see
  42.  * if the memory was in fact allocated by this API.
  43.  */
  44. static char *io_tlb_start, *io_tlb_end;
  45. /*
  46.  * The number of IO TLB blocks (in groups of 64) betweeen io_tlb_start and io_tlb_end.
  47.  * This is command line adjustable via setup_io_tlb_npages.
  48.  */
  49. static unsigned long io_tlb_nslabs = 1024;
  50. /*
  51.  * This is a free list describing the number of free entries available from each index
  52.  */
  53. static unsigned int *io_tlb_list;
  54. static unsigned int io_tlb_index;
  55. /*
  56.  * We need to save away the original address corresponding to a mapped entry for the sync
  57.  * operations.
  58.  */
  59. static unsigned char **io_tlb_orig_addr;
  60. /*
  61.  * Protect the above data structures in the map and unmap calls
  62.  */
  63. static spinlock_t io_tlb_lock = SPIN_LOCK_UNLOCKED;
  64. static int __init
  65. setup_io_tlb_npages (char *str)
  66. {
  67. io_tlb_nslabs = simple_strtoul(str, NULL, 0) << (PAGE_SHIFT - IO_TLB_SHIFT);
  68. /* avoid tail segment of size < IO_TLB_SEGSIZE */
  69. io_tlb_nslabs = ALIGN(io_tlb_nslabs, IO_TLB_SEGSIZE);
  70. return 1;
  71. }
  72. __setup("swiotlb=", setup_io_tlb_npages);
  73. /*
  74.  * Statically reserve bounce buffer space and initialize bounce buffer data structures for
  75.  * the software IO TLB used to implement the PCI DMA API.
  76.  */
  77. void
  78. swiotlb_init (void)
  79. {
  80. int i;
  81. /*
  82.  * Get IO TLB memory from the low pages
  83.  */
  84. io_tlb_start = alloc_bootmem_low_pages(io_tlb_nslabs * (1 << IO_TLB_SHIFT));
  85. if (!io_tlb_start)
  86. BUG();
  87. io_tlb_end = io_tlb_start + io_tlb_nslabs * (1 << IO_TLB_SHIFT);
  88. /*
  89.  * Allocate and initialize the free list array.  This array is used
  90.  * to find contiguous free memory regions of size up to IO_TLB_SEGSIZE
  91.  * between io_tlb_start and io_tlb_end.
  92.  */
  93. io_tlb_list = alloc_bootmem(io_tlb_nslabs * sizeof(int));
  94. for (i = 0; i < io_tlb_nslabs; i++)
  95.   io_tlb_list[i] = IO_TLB_SEGSIZE - OFFSET(i, IO_TLB_SEGSIZE);
  96. io_tlb_index = 0;
  97. io_tlb_orig_addr = alloc_bootmem(io_tlb_nslabs * sizeof(char *));
  98. printk("Placing software IO TLB between 0x%p - 0x%pn",
  99.        (void *) io_tlb_start, (void *) io_tlb_end);
  100. }
  101. /*
  102.  * Allocates bounce buffer and returns its kernel virtual address.
  103.  */
  104. static void *
  105. map_single (struct pci_dev *hwdev, char *buffer, size_t size, int direction)
  106. {
  107. unsigned long flags;
  108. char *dma_addr;
  109. unsigned int nslots, stride, index, wrap;
  110. int i;
  111. /*
  112.  * For mappings greater than a page size, we limit the stride (and hence alignment)
  113.  * to a page size.
  114.  */
  115. nslots = ALIGN(size, 1 << IO_TLB_SHIFT) >> IO_TLB_SHIFT;
  116. if (size > (1 << PAGE_SHIFT))
  117. stride = (1 << (PAGE_SHIFT - IO_TLB_SHIFT));
  118. else
  119. stride = 1;
  120. if (!nslots)
  121. BUG();
  122. /*
  123.  * Find suitable number of IO TLB entries size that will fit this request and
  124.  * allocate a buffer from that IO TLB pool.
  125.  */
  126. spin_lock_irqsave(&io_tlb_lock, flags);
  127. {
  128. wrap = index = ALIGN(io_tlb_index, stride);
  129. if (index >= io_tlb_nslabs)
  130. wrap = index = 0;
  131. do {
  132. /*
  133.  * If we find a slot that indicates we have 'nslots' number of
  134.  * contiguous buffers, we allocate the buffers from that slot and
  135.  * mark the entries as '0' indicating unavailable.
  136.  */
  137. if (io_tlb_list[index] >= nslots) {
  138. int count = 0;
  139. for (i = index; i < index + nslots; i++)
  140. io_tlb_list[i] = 0;
  141. for (i = index - 1; (OFFSET(i, IO_TLB_SEGSIZE) != IO_TLB_SEGSIZE -1)
  142.        && io_tlb_list[i]; i--)
  143. io_tlb_list[i] = ++count;
  144. dma_addr = io_tlb_start + (index << IO_TLB_SHIFT);
  145. /*
  146.  * Update the indices to avoid searching in the next round.
  147.  */
  148. io_tlb_index = ((index + nslots) < io_tlb_nslabs
  149. ? (index + nslots) : 0);
  150. goto found;
  151. }
  152. index += stride;
  153. if (index >= io_tlb_nslabs)
  154. index = 0;
  155. } while (index != wrap);
  156. /*
  157.  * XXX What is a suitable recovery mechanism here?  We cannot
  158.  * sleep because we are called from with in interrupts!
  159.  */
  160. panic("map_single: could not allocate software IO TLB (%ld bytes)", size);
  161. }
  162.   found:
  163. spin_unlock_irqrestore(&io_tlb_lock, flags);
  164. /*
  165.  * Save away the mapping from the original address to the DMA address.  This is
  166.  * needed when we sync the memory.  Then we sync the buffer if needed.
  167.  */
  168. io_tlb_orig_addr[index] = buffer;
  169. if (direction == PCI_DMA_TODEVICE || direction == PCI_DMA_BIDIRECTIONAL)
  170. memcpy(dma_addr, buffer, size);
  171. return dma_addr;
  172. }
  173. /*
  174.  * dma_addr is the kernel virtual address of the bounce buffer to unmap.
  175.  */
  176. static void
  177. unmap_single (struct pci_dev *hwdev, char *dma_addr, size_t size, int direction)
  178. {
  179. unsigned long flags;
  180. int i, nslots = ALIGN(size, 1 << IO_TLB_SHIFT) >> IO_TLB_SHIFT;
  181. int index = (dma_addr - io_tlb_start) >> IO_TLB_SHIFT;
  182. char *buffer = io_tlb_orig_addr[index];
  183. /*
  184.  * First, sync the memory before unmapping the entry
  185.  */
  186. if ((direction == PCI_DMA_FROMDEVICE) || (direction == PCI_DMA_BIDIRECTIONAL))
  187. /*
  188.  * bounce... copy the data back into the original buffer * and delete the
  189.  * bounce buffer.
  190.  */
  191. memcpy(buffer, dma_addr, size);
  192. /*
  193.  * Return the buffer to the free list by setting the corresponding entries to
  194.  * indicate the number of contigous entries available.  While returning the
  195.  * entries to the free list, we merge the entries with slots below and above the
  196.  * pool being returned.
  197.  */
  198. spin_lock_irqsave(&io_tlb_lock, flags);
  199. {
  200. int count = ((index + nslots) < ALIGN(index + 1, IO_TLB_SEGSIZE) ?
  201.      io_tlb_list[index + nslots] : 0);
  202. /*
  203.  * Step 1: return the slots to the free list, merging the slots with
  204.  * superceeding slots
  205.  */
  206. for (i = index + nslots - 1; i >= index; i--)
  207. io_tlb_list[i] = ++count;
  208. /*
  209.  * Step 2: merge the returned slots with the preceeding slots, if
  210.  * available (non zero)
  211.  */
  212. for (i = index - 1;  (OFFSET(i, IO_TLB_SEGSIZE) != IO_TLB_SEGSIZE -1) &&
  213.        io_tlb_list[i]; i--)
  214. io_tlb_list[i] = ++count;
  215. }
  216. spin_unlock_irqrestore(&io_tlb_lock, flags);
  217. }
  218. static void
  219. sync_single (struct pci_dev *hwdev, char *dma_addr, size_t size, int direction)
  220. {
  221. int index = (dma_addr - io_tlb_start) >> IO_TLB_SHIFT;
  222. char *buffer = io_tlb_orig_addr[index];
  223. /*
  224.  * bounce... copy the data back into/from the original buffer
  225.  * XXX How do you handle PCI_DMA_BIDIRECTIONAL here ?
  226.  */
  227. if (direction == PCI_DMA_FROMDEVICE)
  228. memcpy(buffer, dma_addr, size);
  229. else if (direction == PCI_DMA_TODEVICE)
  230. memcpy(dma_addr, buffer, size);
  231. else
  232. BUG();
  233. }
  234. void *
  235. swiotlb_alloc_consistent (struct pci_dev *hwdev, size_t size, dma_addr_t *dma_handle)
  236. {
  237. unsigned long pci_addr;
  238. int gfp = GFP_ATOMIC;
  239. void *ret;
  240. /*
  241.  * Alloc_consistent() is defined to return memory < 4GB, no matter what the DMA
  242.  * mask says.
  243.  */
  244. gfp |= GFP_DMA; /* XXX fix me: should change this to GFP_32BIT or ZONE_32BIT */
  245. ret = (void *)__get_free_pages(gfp, get_order(size));
  246. if (!ret)
  247. return NULL;
  248. memset(ret, 0, size);
  249. pci_addr = virt_to_phys(ret);
  250. if (hwdev && (pci_addr & ~hwdev->dma_mask) != 0)
  251. panic("swiotlb_alloc_consistent: allocated memory is out of range for PCI device");
  252. *dma_handle = pci_addr;
  253. return ret;
  254. }
  255. void
  256. swiotlb_free_consistent (struct pci_dev *hwdev, size_t size, void *vaddr, dma_addr_t dma_handle)
  257. {
  258. free_pages((unsigned long) vaddr, get_order(size));
  259. }
  260. /*
  261.  * Map a single buffer of the indicated size for DMA in streaming mode.  The PCI address
  262.  * to use is returned.
  263.  *
  264.  * Once the device is given the dma address, the device owns this memory until either
  265.  * swiotlb_unmap_single or swiotlb_dma_sync_single is performed.
  266.  */
  267. dma_addr_t
  268. swiotlb_map_single (struct pci_dev *hwdev, void *ptr, size_t size, int direction)
  269. {
  270. unsigned long pci_addr = virt_to_phys(ptr);
  271. if (direction == PCI_DMA_NONE)
  272. BUG();
  273. /*
  274.  * Check if the PCI device can DMA to ptr... if so, just return ptr
  275.  */
  276. if ((pci_addr & ~hwdev->dma_mask) == 0)
  277. /*
  278.  * Device is bit capable of DMA'ing to the buffer... just return the PCI
  279.  * address of ptr
  280.  */
  281. return pci_addr;
  282. /*
  283.  * get a bounce buffer:
  284.  */
  285. pci_addr = virt_to_phys(map_single(hwdev, ptr, size, direction));
  286. /*
  287.  * Ensure that the address returned is DMA'ble:
  288.  */
  289. if ((pci_addr & ~hwdev->dma_mask) != 0)
  290. panic("map_single: bounce buffer is not DMA'ble");
  291. return pci_addr;
  292. }
  293. /*
  294.  * Since DMA is i-cache coherent, any (complete) pages that were written via
  295.  * DMA can be marked as "clean" so that update_mmu_cache() doesn't have to
  296.  * flush them when they get mapped into an executable vm-area.
  297.  */
  298. static void
  299. mark_clean (void *addr, size_t size)
  300. {
  301. unsigned long pg_addr, end;
  302. pg_addr = PAGE_ALIGN((unsigned long) addr);
  303. end = (unsigned long) addr + size;
  304. while (pg_addr + PAGE_SIZE <= end) {
  305. struct page *page = virt_to_page(pg_addr);
  306. set_bit(PG_arch_1, &page->flags);
  307. pg_addr += PAGE_SIZE;
  308. }
  309. }
  310. /*
  311.  * Unmap a single streaming mode DMA translation.  The dma_addr and size must match what
  312.  * was provided for in a previous swiotlb_map_single call.  All other usages are
  313.  * undefined.
  314.  *
  315.  * After this call, reads by the cpu to the buffer are guarenteed to see whatever the
  316.  * device wrote there.
  317.  */
  318. void
  319. swiotlb_unmap_single (struct pci_dev *hwdev, dma_addr_t pci_addr, size_t size, int direction)
  320. {
  321. char *dma_addr = phys_to_virt(pci_addr);
  322. if (direction == PCI_DMA_NONE)
  323. BUG();
  324. if (dma_addr >= io_tlb_start && dma_addr < io_tlb_end)
  325. unmap_single(hwdev, dma_addr, size, direction);
  326. else if (direction == PCI_DMA_FROMDEVICE)
  327. mark_clean(dma_addr, size);
  328. }
  329. /*
  330.  * Make physical memory consistent for a single streaming mode DMA translation after a
  331.  * transfer.
  332.  *
  333.  * If you perform a swiotlb_map_single() but wish to interrogate the buffer using the cpu,
  334.  * yet do not wish to teardown the PCI dma mapping, you must call this function before
  335.  * doing so.  At the next point you give the PCI dma address back to the card, the device
  336.  * again owns the buffer.
  337.  */
  338. void
  339. swiotlb_sync_single (struct pci_dev *hwdev, dma_addr_t pci_addr, size_t size, int direction)
  340. {
  341. char *dma_addr = phys_to_virt(pci_addr);
  342. if (direction == PCI_DMA_NONE)
  343. BUG();
  344. if (dma_addr >= io_tlb_start && dma_addr < io_tlb_end)
  345. sync_single(hwdev, dma_addr, size, direction);
  346. else if (direction == PCI_DMA_FROMDEVICE)
  347. mark_clean(dma_addr, size);
  348. }
  349. /*
  350.  * Map a set of buffers described by scatterlist in streaming mode for DMA.  This is the
  351.  * scather-gather version of the above swiotlb_map_single interface.  Here the scatter
  352.  * gather list elements are each tagged with the appropriate dma address and length.  They
  353.  * are obtained via sg_dma_{address,length}(SG).
  354.  *
  355.  * NOTE: An implementation may be able to use a smaller number of
  356.  *       DMA address/length pairs than there are SG table elements.
  357.  *       (for example via virtual mapping capabilities)
  358.  *       The routine returns the number of addr/length pairs actually
  359.  *       used, at most nents.
  360.  *
  361.  * Device ownership issues as mentioned above for swiotlb_map_single are the same here.
  362.  */
  363. int
  364. swiotlb_map_sg (struct pci_dev *hwdev, struct scatterlist *sg, int nelems, int direction)
  365. {
  366. void *addr;
  367. int i;
  368. if (direction == PCI_DMA_NONE)
  369. BUG();
  370. for (i = 0; i < nelems; i++, sg++) {
  371. sg->orig_address = SG_ENT_VIRT_ADDRESS(sg);
  372. if ((SG_ENT_PHYS_ADDRESS(sg) & ~hwdev->dma_mask) != 0) {
  373. addr = map_single(hwdev, sg->orig_address, sg->length, direction);
  374. if (sg->address)
  375. sg->address = addr;
  376. else {
  377. sg->page = virt_to_page(addr);
  378. sg->offset = (u64) addr & ~PAGE_MASK;
  379. }
  380. }
  381. }
  382. return nelems;
  383. }
  384. /*
  385.  * Unmap a set of streaming mode DMA translations.  Again, cpu read rules concerning calls
  386.  * here are the same as for swiotlb_unmap_single() above.
  387.  */
  388. void
  389. swiotlb_unmap_sg (struct pci_dev *hwdev, struct scatterlist *sg, int nelems, int direction)
  390. {
  391. int i;
  392. if (direction == PCI_DMA_NONE)
  393. BUG();
  394. for (i = 0; i < nelems; i++, sg++)
  395. if (sg->orig_address != SG_ENT_VIRT_ADDRESS(sg)) {
  396. unmap_single(hwdev, SG_ENT_VIRT_ADDRESS(sg), sg->length, direction);
  397. if (sg->address)
  398. sg->address = sg->orig_address;
  399. else {
  400. sg->page = virt_to_page(sg->orig_address);
  401. sg->offset = (u64) sg->orig_address & ~PAGE_MASK;
  402. }
  403. } else if (direction == PCI_DMA_FROMDEVICE)
  404. mark_clean(SG_ENT_VIRT_ADDRESS(sg), sg->length);
  405. }
  406. /*
  407.  * Make physical memory consistent for a set of streaming mode DMA translations after a
  408.  * transfer.
  409.  *
  410.  * The same as swiotlb_dma_sync_single but for a scatter-gather list, same rules and
  411.  * usage.
  412.  */
  413. void
  414. swiotlb_sync_sg (struct pci_dev *hwdev, struct scatterlist *sg, int nelems, int direction)
  415. {
  416. int i;
  417. if (direction == PCI_DMA_NONE)
  418. BUG();
  419. for (i = 0; i < nelems; i++, sg++)
  420. if (sg->orig_address != SG_ENT_VIRT_ADDRESS(sg))
  421. sync_single(hwdev, SG_ENT_VIRT_ADDRESS(sg), sg->length, direction);
  422. }
  423. unsigned long
  424. swiotlb_dma_address (struct scatterlist *sg)
  425. {
  426. return SG_ENT_PHYS_ADDRESS(sg);
  427. }
  428. /*
  429.  * Return whether the given PCI device DMA address mask can be supported properly.  For
  430.  * example, if your device can only drive the low 24-bits during PCI bus mastering, then
  431.  * you would pass 0x00ffffff as the mask to this function.
  432.  */
  433. int
  434. swiotlb_pci_dma_supported (struct pci_dev *hwdev, u64 mask)
  435. {
  436. return 1;
  437. }
  438. EXPORT_SYMBOL(swiotlb_init);
  439. EXPORT_SYMBOL(swiotlb_map_single);
  440. EXPORT_SYMBOL(swiotlb_unmap_single);
  441. EXPORT_SYMBOL(swiotlb_map_sg);
  442. EXPORT_SYMBOL(swiotlb_unmap_sg);
  443. EXPORT_SYMBOL(swiotlb_sync_single);
  444. EXPORT_SYMBOL(swiotlb_sync_sg);
  445. EXPORT_SYMBOL(swiotlb_dma_address);
  446. EXPORT_SYMBOL(swiotlb_alloc_consistent);
  447. EXPORT_SYMBOL(swiotlb_free_consistent);
  448. EXPORT_SYMBOL(swiotlb_pci_dma_supported);