dma-mapping.c 15 KB

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