dma-mapping.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  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. static void *
  124. __dma_alloc(struct device *dev, size_t size, dma_addr_t *handle, gfp_t gfp,
  125. pgprot_t prot)
  126. {
  127. struct page *page;
  128. struct arm_vmregion *c;
  129. if (!consistent_pte[0]) {
  130. printk(KERN_ERR "%s: not initialised\n", __func__);
  131. dump_stack();
  132. return NULL;
  133. }
  134. size = PAGE_ALIGN(size);
  135. page = __dma_alloc_buffer(dev, size, gfp);
  136. if (!page)
  137. goto no_page;
  138. /*
  139. * Allocate a virtual address in the consistent mapping region.
  140. */
  141. c = arm_vmregion_alloc(&consistent_head, size,
  142. gfp & ~(__GFP_DMA | __GFP_HIGHMEM));
  143. if (c) {
  144. pte_t *pte;
  145. int idx = CONSISTENT_PTE_INDEX(c->vm_start);
  146. u32 off = CONSISTENT_OFFSET(c->vm_start) & (PTRS_PER_PTE-1);
  147. pte = consistent_pte[idx] + off;
  148. c->vm_pages = page;
  149. /*
  150. * Set the "dma handle"
  151. */
  152. *handle = page_to_dma(dev, page);
  153. do {
  154. BUG_ON(!pte_none(*pte));
  155. /*
  156. * x86 does not mark the pages reserved...
  157. */
  158. SetPageReserved(page);
  159. set_pte_ext(pte, mk_pte(page, prot), 0);
  160. page++;
  161. pte++;
  162. off++;
  163. if (off >= PTRS_PER_PTE) {
  164. off = 0;
  165. pte = consistent_pte[++idx];
  166. }
  167. } while (size -= PAGE_SIZE);
  168. return (void *)c->vm_start;
  169. }
  170. if (page)
  171. __dma_free_buffer(page, size);
  172. no_page:
  173. *handle = ~0;
  174. return NULL;
  175. }
  176. #else /* !CONFIG_MMU */
  177. static void *
  178. __dma_alloc(struct device *dev, size_t size, dma_addr_t *handle, gfp_t gfp,
  179. pgprot_t prot)
  180. {
  181. void *virt;
  182. u64 mask = get_coherent_dma_mask(dev);
  183. if (!mask)
  184. goto error;
  185. if (mask < 0xffffffffULL)
  186. gfp |= GFP_DMA;
  187. virt = kmalloc(size, gfp);
  188. if (!virt)
  189. goto error;
  190. *handle = virt_to_dma(dev, virt);
  191. return virt;
  192. error:
  193. *handle = ~0;
  194. return NULL;
  195. }
  196. #endif /* CONFIG_MMU */
  197. /*
  198. * Allocate DMA-coherent memory space and return both the kernel remapped
  199. * virtual and bus address for that space.
  200. */
  201. void *
  202. dma_alloc_coherent(struct device *dev, size_t size, dma_addr_t *handle, gfp_t gfp)
  203. {
  204. void *memory;
  205. if (dma_alloc_from_coherent(dev, size, handle, &memory))
  206. return memory;
  207. if (arch_is_coherent()) {
  208. struct page *page;
  209. page = __dma_alloc_buffer(dev, PAGE_ALIGN(size), gfp);
  210. if (!page) {
  211. *handle = ~0;
  212. return NULL;
  213. }
  214. *handle = page_to_dma(dev, page);
  215. return page_address(page);
  216. }
  217. return __dma_alloc(dev, size, handle, gfp,
  218. pgprot_noncached(pgprot_kernel));
  219. }
  220. EXPORT_SYMBOL(dma_alloc_coherent);
  221. /*
  222. * Allocate a writecombining region, in much the same way as
  223. * dma_alloc_coherent above.
  224. */
  225. void *
  226. dma_alloc_writecombine(struct device *dev, size_t size, dma_addr_t *handle, gfp_t gfp)
  227. {
  228. return __dma_alloc(dev, size, handle, gfp,
  229. pgprot_writecombine(pgprot_kernel));
  230. }
  231. EXPORT_SYMBOL(dma_alloc_writecombine);
  232. static int dma_mmap(struct device *dev, struct vm_area_struct *vma,
  233. void *cpu_addr, dma_addr_t dma_addr, size_t size)
  234. {
  235. int ret = -ENXIO;
  236. #ifdef CONFIG_MMU
  237. unsigned long user_size, kern_size;
  238. struct arm_vmregion *c;
  239. user_size = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
  240. c = arm_vmregion_find(&consistent_head, (unsigned long)cpu_addr);
  241. if (c) {
  242. unsigned long off = vma->vm_pgoff;
  243. kern_size = (c->vm_end - c->vm_start) >> PAGE_SHIFT;
  244. if (off < kern_size &&
  245. user_size <= (kern_size - off)) {
  246. ret = remap_pfn_range(vma, vma->vm_start,
  247. page_to_pfn(c->vm_pages) + off,
  248. user_size << PAGE_SHIFT,
  249. vma->vm_page_prot);
  250. }
  251. }
  252. #endif /* CONFIG_MMU */
  253. return ret;
  254. }
  255. int dma_mmap_coherent(struct device *dev, struct vm_area_struct *vma,
  256. void *cpu_addr, dma_addr_t dma_addr, size_t size)
  257. {
  258. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  259. return dma_mmap(dev, vma, cpu_addr, dma_addr, size);
  260. }
  261. EXPORT_SYMBOL(dma_mmap_coherent);
  262. int dma_mmap_writecombine(struct device *dev, struct vm_area_struct *vma,
  263. void *cpu_addr, dma_addr_t dma_addr, size_t size)
  264. {
  265. vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
  266. return dma_mmap(dev, vma, cpu_addr, dma_addr, size);
  267. }
  268. EXPORT_SYMBOL(dma_mmap_writecombine);
  269. /*
  270. * free a page as defined by the above mapping.
  271. * Must not be called with IRQs disabled.
  272. */
  273. #ifdef CONFIG_MMU
  274. void dma_free_coherent(struct device *dev, size_t size, void *cpu_addr, dma_addr_t handle)
  275. {
  276. struct arm_vmregion *c;
  277. unsigned long addr;
  278. pte_t *ptep;
  279. int idx;
  280. u32 off;
  281. WARN_ON(irqs_disabled());
  282. if (dma_release_from_coherent(dev, get_order(size), cpu_addr))
  283. return;
  284. size = PAGE_ALIGN(size);
  285. if (arch_is_coherent()) {
  286. __dma_free_buffer(dma_to_page(dev, handle), size);
  287. return;
  288. }
  289. c = arm_vmregion_find_remove(&consistent_head, (unsigned long)cpu_addr);
  290. if (!c)
  291. goto no_area;
  292. if ((c->vm_end - c->vm_start) != size) {
  293. printk(KERN_ERR "%s: freeing wrong coherent size (%ld != %d)\n",
  294. __func__, c->vm_end - c->vm_start, size);
  295. dump_stack();
  296. size = c->vm_end - c->vm_start;
  297. }
  298. idx = CONSISTENT_PTE_INDEX(c->vm_start);
  299. off = CONSISTENT_OFFSET(c->vm_start) & (PTRS_PER_PTE-1);
  300. ptep = consistent_pte[idx] + off;
  301. addr = c->vm_start;
  302. do {
  303. pte_t pte = ptep_get_and_clear(&init_mm, addr, ptep);
  304. unsigned long pfn;
  305. ptep++;
  306. addr += PAGE_SIZE;
  307. off++;
  308. if (off >= PTRS_PER_PTE) {
  309. off = 0;
  310. ptep = consistent_pte[++idx];
  311. }
  312. if (!pte_none(pte) && pte_present(pte)) {
  313. pfn = pte_pfn(pte);
  314. if (pfn_valid(pfn)) {
  315. struct page *page = pfn_to_page(pfn);
  316. /*
  317. * x86 does not mark the pages reserved...
  318. */
  319. ClearPageReserved(page);
  320. continue;
  321. }
  322. }
  323. printk(KERN_CRIT "%s: bad page in kernel page table\n",
  324. __func__);
  325. } while (size -= PAGE_SIZE);
  326. flush_tlb_kernel_range(c->vm_start, c->vm_end);
  327. arm_vmregion_free(&consistent_head, c);
  328. __dma_free_buffer(dma_to_page(dev, handle), size);
  329. return;
  330. no_area:
  331. printk(KERN_ERR "%s: trying to free invalid coherent area: %p\n",
  332. __func__, cpu_addr);
  333. dump_stack();
  334. }
  335. #else /* !CONFIG_MMU */
  336. void dma_free_coherent(struct device *dev, size_t size, void *cpu_addr, dma_addr_t handle)
  337. {
  338. if (dma_release_from_coherent(dev, get_order(size), cpu_addr))
  339. return;
  340. kfree(cpu_addr);
  341. }
  342. #endif /* CONFIG_MMU */
  343. EXPORT_SYMBOL(dma_free_coherent);
  344. /*
  345. * Initialise the consistent memory allocation.
  346. */
  347. static int __init consistent_init(void)
  348. {
  349. int ret = 0;
  350. #ifdef CONFIG_MMU
  351. pgd_t *pgd;
  352. pmd_t *pmd;
  353. pte_t *pte;
  354. int i = 0;
  355. u32 base = CONSISTENT_BASE;
  356. do {
  357. pgd = pgd_offset(&init_mm, base);
  358. pmd = pmd_alloc(&init_mm, pgd, base);
  359. if (!pmd) {
  360. printk(KERN_ERR "%s: no pmd tables\n", __func__);
  361. ret = -ENOMEM;
  362. break;
  363. }
  364. WARN_ON(!pmd_none(*pmd));
  365. pte = pte_alloc_kernel(pmd, base);
  366. if (!pte) {
  367. printk(KERN_ERR "%s: no pte tables\n", __func__);
  368. ret = -ENOMEM;
  369. break;
  370. }
  371. consistent_pte[i++] = pte;
  372. base += (1 << PGDIR_SHIFT);
  373. } while (base < CONSISTENT_END);
  374. #endif /* !CONFIG_MMU */
  375. return ret;
  376. }
  377. core_initcall(consistent_init);
  378. /*
  379. * Make an area consistent for devices.
  380. * Note: Drivers should NOT use this function directly, as it will break
  381. * platforms with CONFIG_DMABOUNCE.
  382. * Use the driver DMA support - see dma-mapping.h (dma_sync_*)
  383. */
  384. void dma_cache_maint(const void *start, size_t size, int direction)
  385. {
  386. void (*inner_op)(const void *, const void *);
  387. void (*outer_op)(unsigned long, unsigned long);
  388. BUG_ON(!virt_addr_valid(start) || !virt_addr_valid(start + size - 1));
  389. switch (direction) {
  390. case DMA_FROM_DEVICE: /* invalidate only */
  391. inner_op = dmac_inv_range;
  392. outer_op = outer_inv_range;
  393. break;
  394. case DMA_TO_DEVICE: /* writeback only */
  395. inner_op = dmac_clean_range;
  396. outer_op = outer_clean_range;
  397. break;
  398. case DMA_BIDIRECTIONAL: /* writeback and invalidate */
  399. inner_op = dmac_flush_range;
  400. outer_op = outer_flush_range;
  401. break;
  402. default:
  403. BUG();
  404. }
  405. inner_op(start, start + size);
  406. outer_op(__pa(start), __pa(start) + size);
  407. }
  408. EXPORT_SYMBOL(dma_cache_maint);
  409. static void dma_cache_maint_contiguous(struct page *page, unsigned long offset,
  410. size_t size, int direction)
  411. {
  412. void *vaddr;
  413. unsigned long paddr;
  414. void (*inner_op)(const void *, const void *);
  415. void (*outer_op)(unsigned long, unsigned long);
  416. switch (direction) {
  417. case DMA_FROM_DEVICE: /* invalidate only */
  418. inner_op = dmac_inv_range;
  419. outer_op = outer_inv_range;
  420. break;
  421. case DMA_TO_DEVICE: /* writeback only */
  422. inner_op = dmac_clean_range;
  423. outer_op = outer_clean_range;
  424. break;
  425. case DMA_BIDIRECTIONAL: /* writeback and invalidate */
  426. inner_op = dmac_flush_range;
  427. outer_op = outer_flush_range;
  428. break;
  429. default:
  430. BUG();
  431. }
  432. if (!PageHighMem(page)) {
  433. vaddr = page_address(page) + offset;
  434. inner_op(vaddr, vaddr + size);
  435. } else {
  436. vaddr = kmap_high_get(page);
  437. if (vaddr) {
  438. vaddr += offset;
  439. inner_op(vaddr, vaddr + size);
  440. kunmap_high(page);
  441. }
  442. }
  443. paddr = page_to_phys(page) + offset;
  444. outer_op(paddr, paddr + size);
  445. }
  446. void dma_cache_maint_page(struct page *page, unsigned long offset,
  447. size_t size, int dir)
  448. {
  449. /*
  450. * A single sg entry may refer to multiple physically contiguous
  451. * pages. But we still need to process highmem pages individually.
  452. * If highmem is not configured then the bulk of this loop gets
  453. * optimized out.
  454. */
  455. size_t left = size;
  456. do {
  457. size_t len = left;
  458. if (PageHighMem(page) && len + offset > PAGE_SIZE) {
  459. if (offset >= PAGE_SIZE) {
  460. page += offset / PAGE_SIZE;
  461. offset %= PAGE_SIZE;
  462. }
  463. len = PAGE_SIZE - offset;
  464. }
  465. dma_cache_maint_contiguous(page, offset, len, dir);
  466. offset = 0;
  467. page++;
  468. left -= len;
  469. } while (left);
  470. }
  471. EXPORT_SYMBOL(dma_cache_maint_page);
  472. /**
  473. * dma_map_sg - map a set of SG buffers for streaming mode DMA
  474. * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
  475. * @sg: list of buffers
  476. * @nents: number of buffers to map
  477. * @dir: DMA transfer direction
  478. *
  479. * Map a set of buffers described by scatterlist in streaming mode for DMA.
  480. * This is the scatter-gather version of the dma_map_single interface.
  481. * Here the scatter gather list elements are each tagged with the
  482. * appropriate dma address and length. They are obtained via
  483. * sg_dma_{address,length}.
  484. *
  485. * Device ownership issues as mentioned for dma_map_single are the same
  486. * here.
  487. */
  488. int dma_map_sg(struct device *dev, struct scatterlist *sg, int nents,
  489. enum dma_data_direction dir)
  490. {
  491. struct scatterlist *s;
  492. int i, j;
  493. for_each_sg(sg, s, nents, i) {
  494. s->dma_address = dma_map_page(dev, sg_page(s), s->offset,
  495. s->length, dir);
  496. if (dma_mapping_error(dev, s->dma_address))
  497. goto bad_mapping;
  498. }
  499. return nents;
  500. bad_mapping:
  501. for_each_sg(sg, s, i, j)
  502. dma_unmap_page(dev, sg_dma_address(s), sg_dma_len(s), dir);
  503. return 0;
  504. }
  505. EXPORT_SYMBOL(dma_map_sg);
  506. /**
  507. * dma_unmap_sg - unmap a set of SG buffers mapped by dma_map_sg
  508. * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
  509. * @sg: list of buffers
  510. * @nents: number of buffers to unmap (returned from dma_map_sg)
  511. * @dir: DMA transfer direction (same as was passed to dma_map_sg)
  512. *
  513. * Unmap a set of streaming mode DMA translations. Again, CPU access
  514. * rules concerning calls here are the same as for dma_unmap_single().
  515. */
  516. void dma_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,
  517. enum dma_data_direction dir)
  518. {
  519. struct scatterlist *s;
  520. int i;
  521. for_each_sg(sg, s, nents, i)
  522. dma_unmap_page(dev, sg_dma_address(s), sg_dma_len(s), dir);
  523. }
  524. EXPORT_SYMBOL(dma_unmap_sg);
  525. /**
  526. * dma_sync_sg_for_cpu
  527. * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
  528. * @sg: list of buffers
  529. * @nents: number of buffers to map (returned from dma_map_sg)
  530. * @dir: DMA transfer direction (same as was passed to dma_map_sg)
  531. */
  532. void dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
  533. int nents, enum dma_data_direction dir)
  534. {
  535. struct scatterlist *s;
  536. int i;
  537. for_each_sg(sg, s, nents, i) {
  538. dmabounce_sync_for_cpu(dev, sg_dma_address(s), 0,
  539. sg_dma_len(s), dir);
  540. }
  541. }
  542. EXPORT_SYMBOL(dma_sync_sg_for_cpu);
  543. /**
  544. * dma_sync_sg_for_device
  545. * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
  546. * @sg: list of buffers
  547. * @nents: number of buffers to map (returned from dma_map_sg)
  548. * @dir: DMA transfer direction (same as was passed to dma_map_sg)
  549. */
  550. void dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
  551. int nents, enum dma_data_direction dir)
  552. {
  553. struct scatterlist *s;
  554. int i;
  555. for_each_sg(sg, s, nents, i) {
  556. if (!dmabounce_sync_for_device(dev, sg_dma_address(s), 0,
  557. sg_dma_len(s), dir))
  558. continue;
  559. if (!arch_is_coherent())
  560. dma_cache_maint_page(sg_page(s), s->offset,
  561. s->length, dir);
  562. }
  563. }
  564. EXPORT_SYMBOL(dma_sync_sg_for_device);