dma-mapping.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629
  1. /*
  2. * linux/arch/arm/mm/dma-mapping.c
  3. *
  4. * Copyright (C) 2000-2004 Russell King
  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 version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * DMA uncached mapping support.
  11. */
  12. #include <linux/module.h>
  13. #include <linux/mm.h>
  14. #include <linux/slab.h>
  15. #include <linux/errno.h>
  16. #include <linux/list.h>
  17. #include <linux/init.h>
  18. #include <linux/device.h>
  19. #include <linux/dma-mapping.h>
  20. #include <asm/memory.h>
  21. #include <asm/highmem.h>
  22. #include <asm/cacheflush.h>
  23. #include <asm/tlbflush.h>
  24. #include <asm/sizes.h>
  25. /* Sanity check size */
  26. #if (CONSISTENT_DMA_SIZE % SZ_2M)
  27. #error "CONSISTENT_DMA_SIZE must be multiple of 2MiB"
  28. #endif
  29. #define CONSISTENT_END (0xffe00000)
  30. #define CONSISTENT_BASE (CONSISTENT_END - CONSISTENT_DMA_SIZE)
  31. #define CONSISTENT_OFFSET(x) (((unsigned long)(x) - CONSISTENT_BASE) >> PAGE_SHIFT)
  32. #define CONSISTENT_PTE_INDEX(x) (((unsigned long)(x) - CONSISTENT_BASE) >> PGDIR_SHIFT)
  33. #define NUM_CONSISTENT_PTES (CONSISTENT_DMA_SIZE >> PGDIR_SHIFT)
  34. static u64 get_coherent_dma_mask(struct device *dev)
  35. {
  36. u64 mask = ISA_DMA_THRESHOLD;
  37. if (dev) {
  38. mask = dev->coherent_dma_mask;
  39. /*
  40. * Sanity check the DMA mask - it must be non-zero, and
  41. * must be able to be satisfied by a DMA allocation.
  42. */
  43. if (mask == 0) {
  44. dev_warn(dev, "coherent DMA mask is unset\n");
  45. return 0;
  46. }
  47. if ((~mask) & ISA_DMA_THRESHOLD) {
  48. dev_warn(dev, "coherent DMA mask %#llx is smaller "
  49. "than system GFP_DMA mask %#llx\n",
  50. mask, (unsigned long long)ISA_DMA_THRESHOLD);
  51. return 0;
  52. }
  53. }
  54. return mask;
  55. }
  56. /*
  57. * Allocate a DMA buffer for 'dev' of size 'size' using the
  58. * specified gfp mask. Note that 'size' must be page aligned.
  59. */
  60. static struct page *__dma_alloc_buffer(struct device *dev, size_t size, gfp_t gfp)
  61. {
  62. unsigned long order = get_order(size);
  63. struct page *page, *p, *e;
  64. void *ptr;
  65. u64 mask = get_coherent_dma_mask(dev);
  66. #ifdef CONFIG_DMA_API_DEBUG
  67. u64 limit = (mask + 1) & ~mask;
  68. if (limit && size >= limit) {
  69. dev_warn(dev, "coherent allocation too big (requested %#x mask %#llx)\n",
  70. size, mask);
  71. return NULL;
  72. }
  73. #endif
  74. if (!mask)
  75. return NULL;
  76. if (mask < 0xffffffffULL)
  77. gfp |= GFP_DMA;
  78. page = alloc_pages(gfp, order);
  79. if (!page)
  80. return NULL;
  81. /*
  82. * Now split the huge page and free the excess pages
  83. */
  84. split_page(page, order);
  85. for (p = page + (size >> PAGE_SHIFT), e = page + (1 << order); p < e; p++)
  86. __free_page(p);
  87. /*
  88. * Ensure that the allocated pages are zeroed, and that any data
  89. * lurking in the kernel direct-mapped region is invalidated.
  90. */
  91. ptr = page_address(page);
  92. memset(ptr, 0, size);
  93. dmac_flush_range(ptr, ptr + size);
  94. outer_flush_range(__pa(ptr), __pa(ptr) + size);
  95. return page;
  96. }
  97. /*
  98. * Free a DMA buffer. 'size' must be page aligned.
  99. */
  100. static void __dma_free_buffer(struct page *page, size_t size)
  101. {
  102. struct page *e = page + (size >> PAGE_SHIFT);
  103. while (page < e) {
  104. __free_page(page);
  105. page++;
  106. }
  107. }
  108. #ifdef CONFIG_MMU
  109. /*
  110. * These are the page tables (2MB each) covering uncached, DMA consistent allocations
  111. */
  112. static pte_t *consistent_pte[NUM_CONSISTENT_PTES];
  113. #include "vmregion.h"
  114. static struct arm_vmregion_head consistent_head = {
  115. .vm_lock = __SPIN_LOCK_UNLOCKED(&consistent_head.vm_lock),
  116. .vm_list = LIST_HEAD_INIT(consistent_head.vm_list),
  117. .vm_start = CONSISTENT_BASE,
  118. .vm_end = CONSISTENT_END,
  119. };
  120. #ifdef CONFIG_HUGETLB_PAGE
  121. #error ARM Coherent DMA allocator does not (yet) support huge TLB
  122. #endif
  123. /*
  124. * Initialise the consistent memory allocation.
  125. */
  126. static int __init consistent_init(void)
  127. {
  128. int ret = 0;
  129. pgd_t *pgd;
  130. pmd_t *pmd;
  131. pte_t *pte;
  132. int i = 0;
  133. u32 base = CONSISTENT_BASE;
  134. do {
  135. pgd = pgd_offset(&init_mm, base);
  136. pmd = pmd_alloc(&init_mm, pgd, base);
  137. if (!pmd) {
  138. printk(KERN_ERR "%s: no pmd tables\n", __func__);
  139. ret = -ENOMEM;
  140. break;
  141. }
  142. WARN_ON(!pmd_none(*pmd));
  143. pte = pte_alloc_kernel(pmd, base);
  144. if (!pte) {
  145. printk(KERN_ERR "%s: no pte tables\n", __func__);
  146. ret = -ENOMEM;
  147. break;
  148. }
  149. consistent_pte[i++] = pte;
  150. base += (1 << PGDIR_SHIFT);
  151. } while (base < CONSISTENT_END);
  152. return ret;
  153. }
  154. core_initcall(consistent_init);
  155. static void *
  156. __dma_alloc_remap(struct page *page, size_t size, gfp_t gfp, pgprot_t prot)
  157. {
  158. struct arm_vmregion *c;
  159. if (!consistent_pte[0]) {
  160. printk(KERN_ERR "%s: not initialised\n", __func__);
  161. dump_stack();
  162. return NULL;
  163. }
  164. /*
  165. * Allocate a virtual address in the consistent mapping region.
  166. */
  167. c = arm_vmregion_alloc(&consistent_head, size,
  168. gfp & ~(__GFP_DMA | __GFP_HIGHMEM));
  169. if (c) {
  170. pte_t *pte;
  171. int idx = CONSISTENT_PTE_INDEX(c->vm_start);
  172. u32 off = CONSISTENT_OFFSET(c->vm_start) & (PTRS_PER_PTE-1);
  173. pte = consistent_pte[idx] + off;
  174. c->vm_pages = page;
  175. do {
  176. BUG_ON(!pte_none(*pte));
  177. set_pte_ext(pte, mk_pte(page, prot), 0);
  178. page++;
  179. pte++;
  180. off++;
  181. if (off >= PTRS_PER_PTE) {
  182. off = 0;
  183. pte = consistent_pte[++idx];
  184. }
  185. } while (size -= PAGE_SIZE);
  186. return (void *)c->vm_start;
  187. }
  188. return NULL;
  189. }
  190. static void __dma_free_remap(void *cpu_addr, size_t size)
  191. {
  192. struct arm_vmregion *c;
  193. unsigned long addr;
  194. pte_t *ptep;
  195. int idx;
  196. u32 off;
  197. c = arm_vmregion_find_remove(&consistent_head, (unsigned long)cpu_addr);
  198. if (!c) {
  199. printk(KERN_ERR "%s: trying to free invalid coherent area: %p\n",
  200. __func__, cpu_addr);
  201. dump_stack();
  202. return;
  203. }
  204. if ((c->vm_end - c->vm_start) != size) {
  205. printk(KERN_ERR "%s: freeing wrong coherent size (%ld != %d)\n",
  206. __func__, c->vm_end - c->vm_start, size);
  207. dump_stack();
  208. size = c->vm_end - c->vm_start;
  209. }
  210. idx = CONSISTENT_PTE_INDEX(c->vm_start);
  211. off = CONSISTENT_OFFSET(c->vm_start) & (PTRS_PER_PTE-1);
  212. ptep = consistent_pte[idx] + off;
  213. addr = c->vm_start;
  214. do {
  215. pte_t pte = ptep_get_and_clear(&init_mm, addr, ptep);
  216. ptep++;
  217. addr += PAGE_SIZE;
  218. off++;
  219. if (off >= PTRS_PER_PTE) {
  220. off = 0;
  221. ptep = consistent_pte[++idx];
  222. }
  223. if (pte_none(pte) || !pte_present(pte))
  224. printk(KERN_CRIT "%s: bad page in kernel page table\n",
  225. __func__);
  226. } while (size -= PAGE_SIZE);
  227. flush_tlb_kernel_range(c->vm_start, c->vm_end);
  228. arm_vmregion_free(&consistent_head, c);
  229. }
  230. #else /* !CONFIG_MMU */
  231. #define __dma_alloc_remap(page, size, gfp, prot) page_address(page)
  232. #define __dma_free_remap(addr, size) do { } while (0)
  233. #endif /* CONFIG_MMU */
  234. static void *
  235. __dma_alloc(struct device *dev, size_t size, dma_addr_t *handle, gfp_t gfp,
  236. pgprot_t prot)
  237. {
  238. struct page *page;
  239. void *addr;
  240. *handle = ~0;
  241. size = PAGE_ALIGN(size);
  242. page = __dma_alloc_buffer(dev, size, gfp);
  243. if (!page)
  244. return NULL;
  245. if (!arch_is_coherent())
  246. addr = __dma_alloc_remap(page, size, gfp, prot);
  247. else
  248. addr = page_address(page);
  249. if (addr)
  250. *handle = page_to_dma(dev, page);
  251. return addr;
  252. }
  253. /*
  254. * Allocate DMA-coherent memory space and return both the kernel remapped
  255. * virtual and bus address for that space.
  256. */
  257. void *
  258. dma_alloc_coherent(struct device *dev, size_t size, dma_addr_t *handle, gfp_t gfp)
  259. {
  260. void *memory;
  261. if (dma_alloc_from_coherent(dev, size, handle, &memory))
  262. return memory;
  263. return __dma_alloc(dev, size, handle, gfp,
  264. pgprot_dmacoherent(pgprot_kernel));
  265. }
  266. EXPORT_SYMBOL(dma_alloc_coherent);
  267. /*
  268. * Allocate a writecombining region, in much the same way as
  269. * dma_alloc_coherent above.
  270. */
  271. void *
  272. dma_alloc_writecombine(struct device *dev, size_t size, dma_addr_t *handle, gfp_t gfp)
  273. {
  274. return __dma_alloc(dev, size, handle, gfp,
  275. pgprot_writecombine(pgprot_kernel));
  276. }
  277. EXPORT_SYMBOL(dma_alloc_writecombine);
  278. static int dma_mmap(struct device *dev, struct vm_area_struct *vma,
  279. void *cpu_addr, dma_addr_t dma_addr, size_t size)
  280. {
  281. int ret = -ENXIO;
  282. #ifdef CONFIG_MMU
  283. unsigned long user_size, kern_size;
  284. struct arm_vmregion *c;
  285. user_size = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
  286. c = arm_vmregion_find(&consistent_head, (unsigned long)cpu_addr);
  287. if (c) {
  288. unsigned long off = vma->vm_pgoff;
  289. kern_size = (c->vm_end - c->vm_start) >> PAGE_SHIFT;
  290. if (off < kern_size &&
  291. user_size <= (kern_size - off)) {
  292. ret = remap_pfn_range(vma, vma->vm_start,
  293. page_to_pfn(c->vm_pages) + off,
  294. user_size << PAGE_SHIFT,
  295. vma->vm_page_prot);
  296. }
  297. }
  298. #endif /* CONFIG_MMU */
  299. return ret;
  300. }
  301. int dma_mmap_coherent(struct device *dev, struct vm_area_struct *vma,
  302. void *cpu_addr, dma_addr_t dma_addr, size_t size)
  303. {
  304. vma->vm_page_prot = pgprot_dmacoherent(vma->vm_page_prot);
  305. return dma_mmap(dev, vma, cpu_addr, dma_addr, size);
  306. }
  307. EXPORT_SYMBOL(dma_mmap_coherent);
  308. int dma_mmap_writecombine(struct device *dev, struct vm_area_struct *vma,
  309. void *cpu_addr, dma_addr_t dma_addr, size_t size)
  310. {
  311. vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
  312. return dma_mmap(dev, vma, cpu_addr, dma_addr, size);
  313. }
  314. EXPORT_SYMBOL(dma_mmap_writecombine);
  315. /*
  316. * free a page as defined by the above mapping.
  317. * Must not be called with IRQs disabled.
  318. */
  319. void dma_free_coherent(struct device *dev, size_t size, void *cpu_addr, dma_addr_t handle)
  320. {
  321. WARN_ON(irqs_disabled());
  322. if (dma_release_from_coherent(dev, get_order(size), cpu_addr))
  323. return;
  324. size = PAGE_ALIGN(size);
  325. if (!arch_is_coherent())
  326. __dma_free_remap(cpu_addr, size);
  327. __dma_free_buffer(dma_to_page(dev, handle), size);
  328. }
  329. EXPORT_SYMBOL(dma_free_coherent);
  330. /*
  331. * Make an area consistent for devices.
  332. * Note: Drivers should NOT use this function directly, as it will break
  333. * platforms with CONFIG_DMABOUNCE.
  334. * Use the driver DMA support - see dma-mapping.h (dma_sync_*)
  335. */
  336. static void dma_cache_maint(const void *start, size_t size, int direction)
  337. {
  338. void (*inner_op)(const void *, const void *);
  339. void (*outer_op)(unsigned long, unsigned long);
  340. BUG_ON(!virt_addr_valid(start) || !virt_addr_valid(start + size - 1));
  341. switch (direction) {
  342. case DMA_FROM_DEVICE: /* invalidate only */
  343. inner_op = dmac_inv_range;
  344. outer_op = outer_inv_range;
  345. break;
  346. case DMA_TO_DEVICE: /* writeback only */
  347. inner_op = dmac_clean_range;
  348. outer_op = outer_clean_range;
  349. break;
  350. case DMA_BIDIRECTIONAL: /* writeback and invalidate */
  351. inner_op = dmac_flush_range;
  352. outer_op = outer_flush_range;
  353. break;
  354. default:
  355. BUG();
  356. }
  357. inner_op(start, start + size);
  358. outer_op(__pa(start), __pa(start) + size);
  359. }
  360. void ___dma_single_cpu_to_dev(const void *kaddr, size_t size,
  361. enum dma_data_direction dir)
  362. {
  363. dma_cache_maint(kaddr, size, dir);
  364. }
  365. EXPORT_SYMBOL(___dma_single_cpu_to_dev);
  366. void ___dma_single_dev_to_cpu(const void *kaddr, size_t size,
  367. enum dma_data_direction dir)
  368. {
  369. /* nothing to do */
  370. }
  371. EXPORT_SYMBOL(___dma_single_dev_to_cpu);
  372. static void dma_cache_maint_page(struct page *page, unsigned long offset,
  373. size_t size, void (*op)(const void *, const void *))
  374. {
  375. /*
  376. * A single sg entry may refer to multiple physically contiguous
  377. * pages. But we still need to process highmem pages individually.
  378. * If highmem is not configured then the bulk of this loop gets
  379. * optimized out.
  380. */
  381. size_t left = size;
  382. do {
  383. size_t len = left;
  384. void *vaddr;
  385. if (PageHighMem(page)) {
  386. if (len + offset > PAGE_SIZE) {
  387. if (offset >= PAGE_SIZE) {
  388. page += offset / PAGE_SIZE;
  389. offset %= PAGE_SIZE;
  390. }
  391. len = PAGE_SIZE - offset;
  392. }
  393. vaddr = kmap_high_get(page);
  394. if (vaddr) {
  395. vaddr += offset;
  396. op(vaddr, vaddr + len);
  397. kunmap_high(page);
  398. }
  399. } else {
  400. vaddr = page_address(page) + offset;
  401. op(vaddr, vaddr + len);
  402. }
  403. offset = 0;
  404. page++;
  405. left -= len;
  406. } while (left);
  407. }
  408. void ___dma_page_cpu_to_dev(struct page *page, unsigned long off,
  409. size_t size, enum dma_data_direction dir)
  410. {
  411. unsigned long paddr;
  412. void (*inner_op)(const void *, const void *);
  413. void (*outer_op)(unsigned long, unsigned long);
  414. switch (direction) {
  415. case DMA_FROM_DEVICE: /* invalidate only */
  416. inner_op = dmac_inv_range;
  417. outer_op = outer_inv_range;
  418. break;
  419. case DMA_TO_DEVICE: /* writeback only */
  420. inner_op = dmac_clean_range;
  421. outer_op = outer_clean_range;
  422. break;
  423. case DMA_BIDIRECTIONAL: /* writeback and invalidate */
  424. inner_op = dmac_flush_range;
  425. outer_op = outer_flush_range;
  426. break;
  427. default:
  428. BUG();
  429. }
  430. dma_cache_maint_page(page, off, size, inner_op);
  431. paddr = page_to_phys(page) + off;
  432. outer_op(paddr, paddr + size);
  433. }
  434. EXPORT_SYMBOL(___dma_page_cpu_to_dev);
  435. void ___dma_page_dev_to_cpu(struct page *page, unsigned long off,
  436. size_t size, enum dma_data_direction dir)
  437. {
  438. /* nothing to do */
  439. }
  440. EXPORT_SYMBOL(___dma_page_dev_to_cpu);
  441. /**
  442. * dma_map_sg - map a set of SG buffers for streaming mode DMA
  443. * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
  444. * @sg: list of buffers
  445. * @nents: number of buffers to map
  446. * @dir: DMA transfer direction
  447. *
  448. * Map a set of buffers described by scatterlist in streaming mode for DMA.
  449. * This is the scatter-gather version of the dma_map_single interface.
  450. * Here the scatter gather list elements are each tagged with the
  451. * appropriate dma address and length. They are obtained via
  452. * sg_dma_{address,length}.
  453. *
  454. * Device ownership issues as mentioned for dma_map_single are the same
  455. * here.
  456. */
  457. int dma_map_sg(struct device *dev, struct scatterlist *sg, int nents,
  458. enum dma_data_direction dir)
  459. {
  460. struct scatterlist *s;
  461. int i, j;
  462. for_each_sg(sg, s, nents, i) {
  463. s->dma_address = dma_map_page(dev, sg_page(s), s->offset,
  464. s->length, dir);
  465. if (dma_mapping_error(dev, s->dma_address))
  466. goto bad_mapping;
  467. }
  468. return nents;
  469. bad_mapping:
  470. for_each_sg(sg, s, i, j)
  471. dma_unmap_page(dev, sg_dma_address(s), sg_dma_len(s), dir);
  472. return 0;
  473. }
  474. EXPORT_SYMBOL(dma_map_sg);
  475. /**
  476. * dma_unmap_sg - unmap a set of SG buffers mapped by dma_map_sg
  477. * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
  478. * @sg: list of buffers
  479. * @nents: number of buffers to unmap (returned from dma_map_sg)
  480. * @dir: DMA transfer direction (same as was passed to dma_map_sg)
  481. *
  482. * Unmap a set of streaming mode DMA translations. Again, CPU access
  483. * rules concerning calls here are the same as for dma_unmap_single().
  484. */
  485. void dma_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,
  486. enum dma_data_direction dir)
  487. {
  488. struct scatterlist *s;
  489. int i;
  490. for_each_sg(sg, s, nents, i)
  491. dma_unmap_page(dev, sg_dma_address(s), sg_dma_len(s), dir);
  492. }
  493. EXPORT_SYMBOL(dma_unmap_sg);
  494. /**
  495. * dma_sync_sg_for_cpu
  496. * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
  497. * @sg: list of buffers
  498. * @nents: number of buffers to map (returned from dma_map_sg)
  499. * @dir: DMA transfer direction (same as was passed to dma_map_sg)
  500. */
  501. void dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
  502. int nents, enum dma_data_direction dir)
  503. {
  504. struct scatterlist *s;
  505. int i;
  506. for_each_sg(sg, s, nents, i) {
  507. if (!dmabounce_sync_for_cpu(dev, sg_dma_address(s), 0,
  508. sg_dma_len(s), dir))
  509. continue;
  510. __dma_page_dev_to_cpu(sg_page(s), s->offset,
  511. s->length, dir);
  512. }
  513. }
  514. EXPORT_SYMBOL(dma_sync_sg_for_cpu);
  515. /**
  516. * dma_sync_sg_for_device
  517. * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
  518. * @sg: list of buffers
  519. * @nents: number of buffers to map (returned from dma_map_sg)
  520. * @dir: DMA transfer direction (same as was passed to dma_map_sg)
  521. */
  522. void dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
  523. int nents, enum dma_data_direction dir)
  524. {
  525. struct scatterlist *s;
  526. int i;
  527. for_each_sg(sg, s, nents, i) {
  528. if (!dmabounce_sync_for_device(dev, sg_dma_address(s), 0,
  529. sg_dma_len(s), dir))
  530. continue;
  531. __dma_page_cpu_to_dev(sg_page(s), s->offset,
  532. s->length, dir);
  533. }
  534. }
  535. EXPORT_SYMBOL(dma_sync_sg_for_device);