consistent.c 10 KB

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