consistent.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. /*
  2. * linux/arch/arm/mm/consistent.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/cacheflush.h>
  21. #include <asm/tlbflush.h>
  22. #include <asm/sizes.h>
  23. /* Sanity check size */
  24. #if (CONSISTENT_DMA_SIZE % SZ_2M)
  25. #error "CONSISTENT_DMA_SIZE must be multiple of 2MiB"
  26. #endif
  27. #define CONSISTENT_END (0xffe00000)
  28. #define CONSISTENT_BASE (CONSISTENT_END - CONSISTENT_DMA_SIZE)
  29. #define CONSISTENT_OFFSET(x) (((unsigned long)(x) - CONSISTENT_BASE) >> PAGE_SHIFT)
  30. #define CONSISTENT_PTE_INDEX(x) (((unsigned long)(x) - CONSISTENT_BASE) >> PGDIR_SHIFT)
  31. #define NUM_CONSISTENT_PTES (CONSISTENT_DMA_SIZE >> PGDIR_SHIFT)
  32. /*
  33. * These are the page tables (2MB each) covering uncached, DMA consistent allocations
  34. */
  35. static pte_t *consistent_pte[NUM_CONSISTENT_PTES];
  36. static DEFINE_SPINLOCK(consistent_lock);
  37. /*
  38. * VM region handling support.
  39. *
  40. * This should become something generic, handling VM region allocations for
  41. * vmalloc and similar (ioremap, module space, etc).
  42. *
  43. * I envisage vmalloc()'s supporting vm_struct becoming:
  44. *
  45. * struct vm_struct {
  46. * struct vm_region region;
  47. * unsigned long flags;
  48. * struct page **pages;
  49. * unsigned int nr_pages;
  50. * unsigned long phys_addr;
  51. * };
  52. *
  53. * get_vm_area() would then call vm_region_alloc with an appropriate
  54. * struct vm_region head (eg):
  55. *
  56. * struct vm_region vmalloc_head = {
  57. * .vm_list = LIST_HEAD_INIT(vmalloc_head.vm_list),
  58. * .vm_start = VMALLOC_START,
  59. * .vm_end = VMALLOC_END,
  60. * };
  61. *
  62. * However, vmalloc_head.vm_start is variable (typically, it is dependent on
  63. * the amount of RAM found at boot time.) I would imagine that get_vm_area()
  64. * would have to initialise this each time prior to calling vm_region_alloc().
  65. */
  66. struct vm_region {
  67. struct list_head vm_list;
  68. unsigned long vm_start;
  69. unsigned long vm_end;
  70. struct page *vm_pages;
  71. int vm_active;
  72. };
  73. static struct vm_region consistent_head = {
  74. .vm_list = LIST_HEAD_INIT(consistent_head.vm_list),
  75. .vm_start = CONSISTENT_BASE,
  76. .vm_end = CONSISTENT_END,
  77. };
  78. static struct vm_region *
  79. vm_region_alloc(struct vm_region *head, size_t size, gfp_t gfp)
  80. {
  81. unsigned long addr = head->vm_start, end = head->vm_end - size;
  82. unsigned long flags;
  83. struct vm_region *c, *new;
  84. new = kmalloc(sizeof(struct vm_region), gfp);
  85. if (!new)
  86. goto out;
  87. spin_lock_irqsave(&consistent_lock, flags);
  88. list_for_each_entry(c, &head->vm_list, vm_list) {
  89. if ((addr + size) < addr)
  90. goto nospc;
  91. if ((addr + size) <= c->vm_start)
  92. goto found;
  93. addr = c->vm_end;
  94. if (addr > end)
  95. goto nospc;
  96. }
  97. found:
  98. /*
  99. * Insert this entry _before_ the one we found.
  100. */
  101. list_add_tail(&new->vm_list, &c->vm_list);
  102. new->vm_start = addr;
  103. new->vm_end = addr + size;
  104. new->vm_active = 1;
  105. spin_unlock_irqrestore(&consistent_lock, flags);
  106. return new;
  107. nospc:
  108. spin_unlock_irqrestore(&consistent_lock, flags);
  109. kfree(new);
  110. out:
  111. return NULL;
  112. }
  113. static struct vm_region *vm_region_find(struct vm_region *head, unsigned long addr)
  114. {
  115. struct vm_region *c;
  116. list_for_each_entry(c, &head->vm_list, vm_list) {
  117. if (c->vm_active && c->vm_start == addr)
  118. goto out;
  119. }
  120. c = NULL;
  121. out:
  122. return c;
  123. }
  124. #ifdef CONFIG_HUGETLB_PAGE
  125. #error ARM Coherent DMA allocator does not (yet) support huge TLB
  126. #endif
  127. static void *
  128. __dma_alloc(struct device *dev, size_t size, dma_addr_t *handle, gfp_t gfp,
  129. pgprot_t prot)
  130. {
  131. struct page *page;
  132. struct vm_region *c;
  133. unsigned long order;
  134. u64 mask = ISA_DMA_THRESHOLD, limit;
  135. if (!consistent_pte[0]) {
  136. printk(KERN_ERR "%s: not initialised\n", __func__);
  137. dump_stack();
  138. return NULL;
  139. }
  140. if (dev) {
  141. mask = dev->coherent_dma_mask;
  142. /*
  143. * Sanity check the DMA mask - it must be non-zero, and
  144. * must be able to be satisfied by a DMA allocation.
  145. */
  146. if (mask == 0) {
  147. dev_warn(dev, "coherent DMA mask is unset\n");
  148. goto no_page;
  149. }
  150. if ((~mask) & ISA_DMA_THRESHOLD) {
  151. dev_warn(dev, "coherent DMA mask %#llx is smaller "
  152. "than system GFP_DMA mask %#llx\n",
  153. mask, (unsigned long long)ISA_DMA_THRESHOLD);
  154. goto no_page;
  155. }
  156. }
  157. /*
  158. * Sanity check the allocation size.
  159. */
  160. size = PAGE_ALIGN(size);
  161. limit = (mask + 1) & ~mask;
  162. if ((limit && size >= limit) ||
  163. size >= (CONSISTENT_END - CONSISTENT_BASE)) {
  164. printk(KERN_WARNING "coherent allocation too big "
  165. "(requested %#x mask %#llx)\n", size, mask);
  166. goto no_page;
  167. }
  168. order = get_order(size);
  169. if (mask != 0xffffffff)
  170. gfp |= GFP_DMA;
  171. page = alloc_pages(gfp, order);
  172. if (!page)
  173. goto no_page;
  174. /*
  175. * Invalidate any data that might be lurking in the
  176. * kernel direct-mapped region for device DMA.
  177. */
  178. {
  179. unsigned long kaddr = (unsigned long)page_address(page);
  180. memset(page_address(page), 0, size);
  181. dmac_flush_range(kaddr, kaddr + size);
  182. }
  183. /*
  184. * Allocate a virtual address in the consistent mapping region.
  185. */
  186. c = vm_region_alloc(&consistent_head, size,
  187. gfp & ~(__GFP_DMA | __GFP_HIGHMEM));
  188. if (c) {
  189. pte_t *pte;
  190. struct page *end = page + (1 << order);
  191. int idx = CONSISTENT_PTE_INDEX(c->vm_start);
  192. u32 off = CONSISTENT_OFFSET(c->vm_start) & (PTRS_PER_PTE-1);
  193. pte = consistent_pte[idx] + off;
  194. c->vm_pages = page;
  195. /*
  196. * Set the "dma handle"
  197. */
  198. *handle = page_to_dma(dev, page);
  199. do {
  200. BUG_ON(!pte_none(*pte));
  201. set_page_count(page, 1);
  202. /*
  203. * x86 does not mark the pages reserved...
  204. */
  205. SetPageReserved(page);
  206. set_pte(pte, mk_pte(page, prot));
  207. page++;
  208. pte++;
  209. off++;
  210. if (off >= PTRS_PER_PTE) {
  211. off = 0;
  212. pte = consistent_pte[++idx];
  213. }
  214. } while (size -= PAGE_SIZE);
  215. /*
  216. * Free the otherwise unused pages.
  217. */
  218. while (page < end) {
  219. set_page_count(page, 1);
  220. __free_page(page);
  221. page++;
  222. }
  223. return (void *)c->vm_start;
  224. }
  225. if (page)
  226. __free_pages(page, order);
  227. no_page:
  228. *handle = ~0;
  229. return NULL;
  230. }
  231. /*
  232. * Allocate DMA-coherent memory space and return both the kernel remapped
  233. * virtual and bus address for that space.
  234. */
  235. void *
  236. dma_alloc_coherent(struct device *dev, size_t size, dma_addr_t *handle, gfp_t gfp)
  237. {
  238. return __dma_alloc(dev, size, handle, gfp,
  239. pgprot_noncached(pgprot_kernel));
  240. }
  241. EXPORT_SYMBOL(dma_alloc_coherent);
  242. /*
  243. * Allocate a writecombining region, in much the same way as
  244. * dma_alloc_coherent above.
  245. */
  246. void *
  247. dma_alloc_writecombine(struct device *dev, size_t size, dma_addr_t *handle, gfp_t gfp)
  248. {
  249. return __dma_alloc(dev, size, handle, gfp,
  250. pgprot_writecombine(pgprot_kernel));
  251. }
  252. EXPORT_SYMBOL(dma_alloc_writecombine);
  253. static int dma_mmap(struct device *dev, struct vm_area_struct *vma,
  254. void *cpu_addr, dma_addr_t dma_addr, size_t size)
  255. {
  256. unsigned long flags, user_size, kern_size;
  257. struct vm_region *c;
  258. int ret = -ENXIO;
  259. user_size = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
  260. spin_lock_irqsave(&consistent_lock, flags);
  261. c = vm_region_find(&consistent_head, (unsigned long)cpu_addr);
  262. spin_unlock_irqrestore(&consistent_lock, flags);
  263. if (c) {
  264. unsigned long off = vma->vm_pgoff;
  265. kern_size = (c->vm_end - c->vm_start) >> PAGE_SHIFT;
  266. if (off < kern_size &&
  267. user_size <= (kern_size - off)) {
  268. vma->vm_flags |= VM_RESERVED;
  269. ret = remap_pfn_range(vma, vma->vm_start,
  270. page_to_pfn(c->vm_pages) + off,
  271. user_size << PAGE_SHIFT,
  272. vma->vm_page_prot);
  273. }
  274. }
  275. return ret;
  276. }
  277. int dma_mmap_coherent(struct device *dev, struct vm_area_struct *vma,
  278. void *cpu_addr, dma_addr_t dma_addr, size_t size)
  279. {
  280. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  281. return dma_mmap(dev, vma, cpu_addr, dma_addr, size);
  282. }
  283. EXPORT_SYMBOL(dma_mmap_coherent);
  284. int dma_mmap_writecombine(struct device *dev, struct vm_area_struct *vma,
  285. void *cpu_addr, dma_addr_t dma_addr, size_t size)
  286. {
  287. vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
  288. return dma_mmap(dev, vma, cpu_addr, dma_addr, size);
  289. }
  290. EXPORT_SYMBOL(dma_mmap_writecombine);
  291. /*
  292. * free a page as defined by the above mapping.
  293. * Must not be called with IRQs disabled.
  294. */
  295. void dma_free_coherent(struct device *dev, size_t size, void *cpu_addr, dma_addr_t handle)
  296. {
  297. struct vm_region *c;
  298. unsigned long flags, addr;
  299. pte_t *ptep;
  300. int idx;
  301. u32 off;
  302. WARN_ON(irqs_disabled());
  303. size = PAGE_ALIGN(size);
  304. spin_lock_irqsave(&consistent_lock, flags);
  305. c = vm_region_find(&consistent_head, (unsigned long)cpu_addr);
  306. if (!c)
  307. goto no_area;
  308. c->vm_active = 0;
  309. spin_unlock_irqrestore(&consistent_lock, flags);
  310. if ((c->vm_end - c->vm_start) != size) {
  311. printk(KERN_ERR "%s: freeing wrong coherent size (%ld != %d)\n",
  312. __func__, c->vm_end - c->vm_start, size);
  313. dump_stack();
  314. size = c->vm_end - c->vm_start;
  315. }
  316. idx = CONSISTENT_PTE_INDEX(c->vm_start);
  317. off = CONSISTENT_OFFSET(c->vm_start) & (PTRS_PER_PTE-1);
  318. ptep = consistent_pte[idx] + off;
  319. addr = c->vm_start;
  320. do {
  321. pte_t pte = ptep_get_and_clear(&init_mm, addr, ptep);
  322. unsigned long pfn;
  323. ptep++;
  324. addr += PAGE_SIZE;
  325. off++;
  326. if (off >= PTRS_PER_PTE) {
  327. off = 0;
  328. ptep = consistent_pte[++idx];
  329. }
  330. if (!pte_none(pte) && pte_present(pte)) {
  331. pfn = pte_pfn(pte);
  332. if (pfn_valid(pfn)) {
  333. struct page *page = pfn_to_page(pfn);
  334. /*
  335. * x86 does not mark the pages reserved...
  336. */
  337. ClearPageReserved(page);
  338. __free_page(page);
  339. continue;
  340. }
  341. }
  342. printk(KERN_CRIT "%s: bad page in kernel page table\n",
  343. __func__);
  344. } while (size -= PAGE_SIZE);
  345. flush_tlb_kernel_range(c->vm_start, c->vm_end);
  346. spin_lock_irqsave(&consistent_lock, flags);
  347. list_del(&c->vm_list);
  348. spin_unlock_irqrestore(&consistent_lock, flags);
  349. kfree(c);
  350. return;
  351. no_area:
  352. spin_unlock_irqrestore(&consistent_lock, flags);
  353. printk(KERN_ERR "%s: trying to free invalid coherent area: %p\n",
  354. __func__, cpu_addr);
  355. dump_stack();
  356. }
  357. EXPORT_SYMBOL(dma_free_coherent);
  358. /*
  359. * Initialise the consistent memory allocation.
  360. */
  361. static int __init consistent_init(void)
  362. {
  363. pgd_t *pgd;
  364. pmd_t *pmd;
  365. pte_t *pte;
  366. int ret = 0, i = 0;
  367. u32 base = CONSISTENT_BASE;
  368. do {
  369. pgd = pgd_offset(&init_mm, base);
  370. pmd = pmd_alloc(&init_mm, pgd, base);
  371. if (!pmd) {
  372. printk(KERN_ERR "%s: no pmd tables\n", __func__);
  373. ret = -ENOMEM;
  374. break;
  375. }
  376. WARN_ON(!pmd_none(*pmd));
  377. pte = pte_alloc_kernel(pmd, base);
  378. if (!pte) {
  379. printk(KERN_ERR "%s: no pte tables\n", __func__);
  380. ret = -ENOMEM;
  381. break;
  382. }
  383. consistent_pte[i++] = pte;
  384. base += (1 << PGDIR_SHIFT);
  385. } while (base < CONSISTENT_END);
  386. return ret;
  387. }
  388. core_initcall(consistent_init);
  389. /*
  390. * Make an area consistent for devices.
  391. */
  392. void consistent_sync(void *vaddr, size_t size, int direction)
  393. {
  394. unsigned long start = (unsigned long)vaddr;
  395. unsigned long end = start + size;
  396. switch (direction) {
  397. case DMA_FROM_DEVICE: /* invalidate only */
  398. dmac_inv_range(start, end);
  399. break;
  400. case DMA_TO_DEVICE: /* writeback only */
  401. dmac_clean_range(start, end);
  402. break;
  403. case DMA_BIDIRECTIONAL: /* writeback and invalidate */
  404. dmac_flush_range(start, end);
  405. break;
  406. default:
  407. BUG();
  408. }
  409. }
  410. EXPORT_SYMBOL(consistent_sync);