dma-mapping.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. /*
  2. * PowerPC version derived from arch/arm/mm/consistent.c
  3. * Copyright (C) 2001 Dan Malek (dmalek@jlc.net)
  4. *
  5. * Copyright (C) 2000 Russell King
  6. *
  7. * Consistent memory allocators. Used for DMA devices that want to
  8. * share uncached memory with the processor core. The function return
  9. * is the virtual address and 'dma_handle' is the physical address.
  10. * Mostly stolen from the ARM port, with some changes for PowerPC.
  11. * -- Dan
  12. *
  13. * Reorganized to get rid of the arch-specific consistent_* functions
  14. * and provide non-coherent implementations for the DMA API. -Matt
  15. *
  16. * Added in_interrupt() safe dma_alloc_coherent()/dma_free_coherent()
  17. * implementation. This is pulled straight from ARM and barely
  18. * modified. -Matt
  19. *
  20. * This program is free software; you can redistribute it and/or modify
  21. * it under the terms of the GNU General Public License version 2 as
  22. * published by the Free Software Foundation.
  23. */
  24. #include <linux/config.h>
  25. #include <linux/module.h>
  26. #include <linux/signal.h>
  27. #include <linux/sched.h>
  28. #include <linux/kernel.h>
  29. #include <linux/errno.h>
  30. #include <linux/string.h>
  31. #include <linux/types.h>
  32. #include <linux/ptrace.h>
  33. #include <linux/mman.h>
  34. #include <linux/mm.h>
  35. #include <linux/swap.h>
  36. #include <linux/stddef.h>
  37. #include <linux/vmalloc.h>
  38. #include <linux/init.h>
  39. #include <linux/delay.h>
  40. #include <linux/bootmem.h>
  41. #include <linux/highmem.h>
  42. #include <linux/dma-mapping.h>
  43. #include <linux/hardirq.h>
  44. #include <asm/pgalloc.h>
  45. #include <asm/prom.h>
  46. #include <asm/io.h>
  47. #include <asm/mmu_context.h>
  48. #include <asm/pgtable.h>
  49. #include <asm/mmu.h>
  50. #include <asm/uaccess.h>
  51. #include <asm/smp.h>
  52. #include <asm/machdep.h>
  53. int map_page(unsigned long va, phys_addr_t pa, int flags);
  54. #include <asm/tlbflush.h>
  55. /*
  56. * This address range defaults to a value that is safe for all
  57. * platforms which currently set CONFIG_NOT_COHERENT_CACHE. It
  58. * can be further configured for specific applications under
  59. * the "Advanced Setup" menu. -Matt
  60. */
  61. #define CONSISTENT_BASE (CONFIG_CONSISTENT_START)
  62. #define CONSISTENT_END (CONFIG_CONSISTENT_START + CONFIG_CONSISTENT_SIZE)
  63. #define CONSISTENT_OFFSET(x) (((unsigned long)(x) - CONSISTENT_BASE) >> PAGE_SHIFT)
  64. /*
  65. * This is the page table (2MB) covering uncached, DMA consistent allocations
  66. */
  67. static pte_t *consistent_pte;
  68. static DEFINE_SPINLOCK(consistent_lock);
  69. /*
  70. * VM region handling support.
  71. *
  72. * This should become something generic, handling VM region allocations for
  73. * vmalloc and similar (ioremap, module space, etc).
  74. *
  75. * I envisage vmalloc()'s supporting vm_struct becoming:
  76. *
  77. * struct vm_struct {
  78. * struct vm_region region;
  79. * unsigned long flags;
  80. * struct page **pages;
  81. * unsigned int nr_pages;
  82. * unsigned long phys_addr;
  83. * };
  84. *
  85. * get_vm_area() would then call vm_region_alloc with an appropriate
  86. * struct vm_region head (eg):
  87. *
  88. * struct vm_region vmalloc_head = {
  89. * .vm_list = LIST_HEAD_INIT(vmalloc_head.vm_list),
  90. * .vm_start = VMALLOC_START,
  91. * .vm_end = VMALLOC_END,
  92. * };
  93. *
  94. * However, vmalloc_head.vm_start is variable (typically, it is dependent on
  95. * the amount of RAM found at boot time.) I would imagine that get_vm_area()
  96. * would have to initialise this each time prior to calling vm_region_alloc().
  97. */
  98. struct vm_region {
  99. struct list_head vm_list;
  100. unsigned long vm_start;
  101. unsigned long vm_end;
  102. };
  103. static struct vm_region consistent_head = {
  104. .vm_list = LIST_HEAD_INIT(consistent_head.vm_list),
  105. .vm_start = CONSISTENT_BASE,
  106. .vm_end = CONSISTENT_END,
  107. };
  108. static struct vm_region *
  109. vm_region_alloc(struct vm_region *head, size_t size, int gfp)
  110. {
  111. unsigned long addr = head->vm_start, end = head->vm_end - size;
  112. unsigned long flags;
  113. struct vm_region *c, *new;
  114. new = kmalloc(sizeof(struct vm_region), gfp);
  115. if (!new)
  116. goto out;
  117. spin_lock_irqsave(&consistent_lock, flags);
  118. list_for_each_entry(c, &head->vm_list, vm_list) {
  119. if ((addr + size) < addr)
  120. goto nospc;
  121. if ((addr + size) <= c->vm_start)
  122. goto found;
  123. addr = c->vm_end;
  124. if (addr > end)
  125. goto nospc;
  126. }
  127. found:
  128. /*
  129. * Insert this entry _before_ the one we found.
  130. */
  131. list_add_tail(&new->vm_list, &c->vm_list);
  132. new->vm_start = addr;
  133. new->vm_end = addr + size;
  134. spin_unlock_irqrestore(&consistent_lock, flags);
  135. return new;
  136. nospc:
  137. spin_unlock_irqrestore(&consistent_lock, flags);
  138. kfree(new);
  139. out:
  140. return NULL;
  141. }
  142. static struct vm_region *vm_region_find(struct vm_region *head, unsigned long addr)
  143. {
  144. struct vm_region *c;
  145. list_for_each_entry(c, &head->vm_list, vm_list) {
  146. if (c->vm_start == addr)
  147. goto out;
  148. }
  149. c = NULL;
  150. out:
  151. return c;
  152. }
  153. /*
  154. * Allocate DMA-coherent memory space and return both the kernel remapped
  155. * virtual and bus address for that space.
  156. */
  157. void *
  158. __dma_alloc_coherent(size_t size, dma_addr_t *handle, int gfp)
  159. {
  160. struct page *page;
  161. struct vm_region *c;
  162. unsigned long order;
  163. u64 mask = 0x00ffffff, limit; /* ISA default */
  164. if (!consistent_pte) {
  165. printk(KERN_ERR "%s: not initialised\n", __func__);
  166. dump_stack();
  167. return NULL;
  168. }
  169. size = PAGE_ALIGN(size);
  170. limit = (mask + 1) & ~mask;
  171. if ((limit && size >= limit) || size >= (CONSISTENT_END - CONSISTENT_BASE)) {
  172. printk(KERN_WARNING "coherent allocation too big (requested %#x mask %#Lx)\n",
  173. size, mask);
  174. return NULL;
  175. }
  176. order = get_order(size);
  177. if (mask != 0xffffffff)
  178. gfp |= GFP_DMA;
  179. page = alloc_pages(gfp, order);
  180. if (!page)
  181. goto no_page;
  182. /*
  183. * Invalidate any data that might be lurking in the
  184. * kernel direct-mapped region for device DMA.
  185. */
  186. {
  187. unsigned long kaddr = (unsigned long)page_address(page);
  188. memset(page_address(page), 0, size);
  189. flush_dcache_range(kaddr, kaddr + size);
  190. }
  191. /*
  192. * Allocate a virtual address in the consistent mapping region.
  193. */
  194. c = vm_region_alloc(&consistent_head, size,
  195. gfp & ~(__GFP_DMA | __GFP_HIGHMEM));
  196. if (c) {
  197. unsigned long vaddr = c->vm_start;
  198. pte_t *pte = consistent_pte + CONSISTENT_OFFSET(vaddr);
  199. struct page *end = page + (1 << order);
  200. /*
  201. * Set the "dma handle"
  202. */
  203. *handle = page_to_bus(page);
  204. do {
  205. BUG_ON(!pte_none(*pte));
  206. set_page_count(page, 1);
  207. SetPageReserved(page);
  208. set_pte_at(&init_mm, vaddr,
  209. pte, mk_pte(page, pgprot_noncached(PAGE_KERNEL)));
  210. page++;
  211. pte++;
  212. vaddr += PAGE_SIZE;
  213. } while (size -= PAGE_SIZE);
  214. /*
  215. * Free the otherwise unused pages.
  216. */
  217. while (page < end) {
  218. set_page_count(page, 1);
  219. __free_page(page);
  220. page++;
  221. }
  222. return (void *)c->vm_start;
  223. }
  224. if (page)
  225. __free_pages(page, order);
  226. no_page:
  227. return NULL;
  228. }
  229. EXPORT_SYMBOL(__dma_alloc_coherent);
  230. /*
  231. * free a page as defined by the above mapping.
  232. */
  233. void __dma_free_coherent(size_t size, void *vaddr)
  234. {
  235. struct vm_region *c;
  236. unsigned long flags, addr;
  237. pte_t *ptep;
  238. size = PAGE_ALIGN(size);
  239. spin_lock_irqsave(&consistent_lock, flags);
  240. c = vm_region_find(&consistent_head, (unsigned long)vaddr);
  241. if (!c)
  242. goto no_area;
  243. if ((c->vm_end - c->vm_start) != size) {
  244. printk(KERN_ERR "%s: freeing wrong coherent size (%ld != %d)\n",
  245. __func__, c->vm_end - c->vm_start, size);
  246. dump_stack();
  247. size = c->vm_end - c->vm_start;
  248. }
  249. ptep = consistent_pte + CONSISTENT_OFFSET(c->vm_start);
  250. addr = c->vm_start;
  251. do {
  252. pte_t pte = ptep_get_and_clear(&init_mm, addr, ptep);
  253. unsigned long pfn;
  254. ptep++;
  255. addr += PAGE_SIZE;
  256. if (!pte_none(pte) && pte_present(pte)) {
  257. pfn = pte_pfn(pte);
  258. if (pfn_valid(pfn)) {
  259. struct page *page = pfn_to_page(pfn);
  260. ClearPageReserved(page);
  261. __free_page(page);
  262. continue;
  263. }
  264. }
  265. printk(KERN_CRIT "%s: bad page in kernel page table\n",
  266. __func__);
  267. } while (size -= PAGE_SIZE);
  268. flush_tlb_kernel_range(c->vm_start, c->vm_end);
  269. list_del(&c->vm_list);
  270. spin_unlock_irqrestore(&consistent_lock, flags);
  271. kfree(c);
  272. return;
  273. no_area:
  274. spin_unlock_irqrestore(&consistent_lock, flags);
  275. printk(KERN_ERR "%s: trying to free invalid coherent area: %p\n",
  276. __func__, vaddr);
  277. dump_stack();
  278. }
  279. EXPORT_SYMBOL(__dma_free_coherent);
  280. /*
  281. * Initialise the consistent memory allocation.
  282. */
  283. static int __init dma_alloc_init(void)
  284. {
  285. pgd_t *pgd;
  286. pmd_t *pmd;
  287. pte_t *pte;
  288. int ret = 0;
  289. spin_lock(&init_mm.page_table_lock);
  290. do {
  291. pgd = pgd_offset(&init_mm, CONSISTENT_BASE);
  292. pmd = pmd_alloc(&init_mm, pgd, CONSISTENT_BASE);
  293. if (!pmd) {
  294. printk(KERN_ERR "%s: no pmd tables\n", __func__);
  295. ret = -ENOMEM;
  296. break;
  297. }
  298. WARN_ON(!pmd_none(*pmd));
  299. pte = pte_alloc_kernel(&init_mm, pmd, CONSISTENT_BASE);
  300. if (!pte) {
  301. printk(KERN_ERR "%s: no pte tables\n", __func__);
  302. ret = -ENOMEM;
  303. break;
  304. }
  305. consistent_pte = pte;
  306. } while (0);
  307. spin_unlock(&init_mm.page_table_lock);
  308. return ret;
  309. }
  310. core_initcall(dma_alloc_init);
  311. /*
  312. * make an area consistent.
  313. */
  314. void __dma_sync(void *vaddr, size_t size, int direction)
  315. {
  316. unsigned long start = (unsigned long)vaddr;
  317. unsigned long end = start + size;
  318. switch (direction) {
  319. case DMA_NONE:
  320. BUG();
  321. case DMA_FROM_DEVICE: /* invalidate only */
  322. invalidate_dcache_range(start, end);
  323. break;
  324. case DMA_TO_DEVICE: /* writeback only */
  325. clean_dcache_range(start, end);
  326. break;
  327. case DMA_BIDIRECTIONAL: /* writeback and invalidate */
  328. flush_dcache_range(start, end);
  329. break;
  330. }
  331. }
  332. EXPORT_SYMBOL(__dma_sync);
  333. #ifdef CONFIG_HIGHMEM
  334. /*
  335. * __dma_sync_page() implementation for systems using highmem.
  336. * In this case, each page of a buffer must be kmapped/kunmapped
  337. * in order to have a virtual address for __dma_sync(). This must
  338. * not sleep so kmap_atmomic()/kunmap_atomic() are used.
  339. *
  340. * Note: yes, it is possible and correct to have a buffer extend
  341. * beyond the first page.
  342. */
  343. static inline void __dma_sync_page_highmem(struct page *page,
  344. unsigned long offset, size_t size, int direction)
  345. {
  346. size_t seg_size = min((size_t)PAGE_SIZE, size) - offset;
  347. size_t cur_size = seg_size;
  348. unsigned long flags, start, seg_offset = offset;
  349. int nr_segs = PAGE_ALIGN(size + (PAGE_SIZE - offset))/PAGE_SIZE;
  350. int seg_nr = 0;
  351. local_irq_save(flags);
  352. do {
  353. start = (unsigned long)kmap_atomic(page + seg_nr,
  354. KM_PPC_SYNC_PAGE) + seg_offset;
  355. /* Sync this buffer segment */
  356. __dma_sync((void *)start, seg_size, direction);
  357. kunmap_atomic((void *)start, KM_PPC_SYNC_PAGE);
  358. seg_nr++;
  359. /* Calculate next buffer segment size */
  360. seg_size = min((size_t)PAGE_SIZE, size - cur_size);
  361. /* Add the segment size to our running total */
  362. cur_size += seg_size;
  363. seg_offset = 0;
  364. } while (seg_nr < nr_segs);
  365. local_irq_restore(flags);
  366. }
  367. #endif /* CONFIG_HIGHMEM */
  368. /*
  369. * __dma_sync_page makes memory consistent. identical to __dma_sync, but
  370. * takes a struct page instead of a virtual address
  371. */
  372. void __dma_sync_page(struct page *page, unsigned long offset,
  373. size_t size, int direction)
  374. {
  375. #ifdef CONFIG_HIGHMEM
  376. __dma_sync_page_highmem(page, offset, size, direction);
  377. #else
  378. unsigned long start = (unsigned long)page_address(page) + offset;
  379. __dma_sync((void *)start, size, direction);
  380. #endif
  381. }
  382. EXPORT_SYMBOL(__dma_sync_page);