dma-mapping.c 15 KB

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