consistent.c 10 KB

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