dma-noncoherent.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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/sched.h>
  25. #include <linux/slab.h>
  26. #include <linux/kernel.h>
  27. #include <linux/errno.h>
  28. #include <linux/string.h>
  29. #include <linux/types.h>
  30. #include <linux/highmem.h>
  31. #include <linux/dma-mapping.h>
  32. #include <linux/export.h>
  33. #include <asm/tlbflush.h>
  34. #include <asm/dma.h>
  35. #include "mmu_decl.h"
  36. /*
  37. * This address range defaults to a value that is safe for all
  38. * platforms which currently set CONFIG_NOT_COHERENT_CACHE. It
  39. * can be further configured for specific applications under
  40. * the "Advanced Setup" menu. -Matt
  41. */
  42. #define CONSISTENT_BASE (IOREMAP_TOP)
  43. #define CONSISTENT_END (CONSISTENT_BASE + CONFIG_CONSISTENT_SIZE)
  44. #define CONSISTENT_OFFSET(x) (((unsigned long)(x) - CONSISTENT_BASE) >> PAGE_SHIFT)
  45. /*
  46. * This is the page table (2MB) covering uncached, DMA consistent allocations
  47. */
  48. static DEFINE_SPINLOCK(consistent_lock);
  49. /*
  50. * VM region handling support.
  51. *
  52. * This should become something generic, handling VM region allocations for
  53. * vmalloc and similar (ioremap, module space, etc).
  54. *
  55. * I envisage vmalloc()'s supporting vm_struct becoming:
  56. *
  57. * struct vm_struct {
  58. * struct vm_region region;
  59. * unsigned long flags;
  60. * struct page **pages;
  61. * unsigned int nr_pages;
  62. * unsigned long phys_addr;
  63. * };
  64. *
  65. * get_vm_area() would then call vm_region_alloc with an appropriate
  66. * struct vm_region head (eg):
  67. *
  68. * struct vm_region vmalloc_head = {
  69. * .vm_list = LIST_HEAD_INIT(vmalloc_head.vm_list),
  70. * .vm_start = VMALLOC_START,
  71. * .vm_end = VMALLOC_END,
  72. * };
  73. *
  74. * However, vmalloc_head.vm_start is variable (typically, it is dependent on
  75. * the amount of RAM found at boot time.) I would imagine that get_vm_area()
  76. * would have to initialise this each time prior to calling vm_region_alloc().
  77. */
  78. struct ppc_vm_region {
  79. struct list_head vm_list;
  80. unsigned long vm_start;
  81. unsigned long vm_end;
  82. };
  83. static struct ppc_vm_region consistent_head = {
  84. .vm_list = LIST_HEAD_INIT(consistent_head.vm_list),
  85. .vm_start = CONSISTENT_BASE,
  86. .vm_end = CONSISTENT_END,
  87. };
  88. static struct ppc_vm_region *
  89. ppc_vm_region_alloc(struct ppc_vm_region *head, size_t size, gfp_t gfp)
  90. {
  91. unsigned long addr = head->vm_start, end = head->vm_end - size;
  92. unsigned long flags;
  93. struct ppc_vm_region *c, *new;
  94. new = kmalloc(sizeof(struct ppc_vm_region), gfp);
  95. if (!new)
  96. goto out;
  97. spin_lock_irqsave(&consistent_lock, flags);
  98. list_for_each_entry(c, &head->vm_list, vm_list) {
  99. if ((addr + size) < addr)
  100. goto nospc;
  101. if ((addr + size) <= c->vm_start)
  102. goto found;
  103. addr = c->vm_end;
  104. if (addr > end)
  105. goto nospc;
  106. }
  107. found:
  108. /*
  109. * Insert this entry _before_ the one we found.
  110. */
  111. list_add_tail(&new->vm_list, &c->vm_list);
  112. new->vm_start = addr;
  113. new->vm_end = addr + size;
  114. spin_unlock_irqrestore(&consistent_lock, flags);
  115. return new;
  116. nospc:
  117. spin_unlock_irqrestore(&consistent_lock, flags);
  118. kfree(new);
  119. out:
  120. return NULL;
  121. }
  122. static struct ppc_vm_region *ppc_vm_region_find(struct ppc_vm_region *head, unsigned long addr)
  123. {
  124. struct ppc_vm_region *c;
  125. list_for_each_entry(c, &head->vm_list, vm_list) {
  126. if (c->vm_start == addr)
  127. goto out;
  128. }
  129. c = NULL;
  130. out:
  131. return c;
  132. }
  133. /*
  134. * Allocate DMA-coherent memory space and return both the kernel remapped
  135. * virtual and bus address for that space.
  136. */
  137. void *
  138. __dma_alloc_coherent(struct device *dev, size_t size, dma_addr_t *handle, gfp_t gfp)
  139. {
  140. struct page *page;
  141. struct ppc_vm_region *c;
  142. unsigned long order;
  143. u64 mask = ISA_DMA_THRESHOLD, limit;
  144. if (dev) {
  145. mask = dev->coherent_dma_mask;
  146. /*
  147. * Sanity check the DMA mask - it must be non-zero, and
  148. * must be able to be satisfied by a DMA allocation.
  149. */
  150. if (mask == 0) {
  151. dev_warn(dev, "coherent DMA mask is unset\n");
  152. goto no_page;
  153. }
  154. if ((~mask) & ISA_DMA_THRESHOLD) {
  155. dev_warn(dev, "coherent DMA mask %#llx is smaller "
  156. "than system GFP_DMA mask %#llx\n",
  157. mask, (unsigned long long)ISA_DMA_THRESHOLD);
  158. goto no_page;
  159. }
  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 (requested %#x mask %#Lx)\n",
  166. size, mask);
  167. return NULL;
  168. }
  169. order = get_order(size);
  170. /* Might be useful if we ever have a real legacy DMA zone... */
  171. if (mask != 0xffffffff)
  172. gfp |= GFP_DMA;
  173. page = alloc_pages(gfp, order);
  174. if (!page)
  175. goto no_page;
  176. /*
  177. * Invalidate any data that might be lurking in the
  178. * kernel direct-mapped region for device DMA.
  179. */
  180. {
  181. unsigned long kaddr = (unsigned long)page_address(page);
  182. memset(page_address(page), 0, size);
  183. flush_dcache_range(kaddr, kaddr + size);
  184. }
  185. /*
  186. * Allocate a virtual address in the consistent mapping region.
  187. */
  188. c = ppc_vm_region_alloc(&consistent_head, size,
  189. gfp & ~(__GFP_DMA | __GFP_HIGHMEM));
  190. if (c) {
  191. unsigned long vaddr = c->vm_start;
  192. struct page *end = page + (1 << order);
  193. split_page(page, order);
  194. /*
  195. * Set the "dma handle"
  196. */
  197. *handle = page_to_phys(page);
  198. do {
  199. SetPageReserved(page);
  200. map_page(vaddr, page_to_phys(page),
  201. pgprot_val(pgprot_noncached(PAGE_KERNEL)));
  202. page++;
  203. vaddr += PAGE_SIZE;
  204. } while (size -= PAGE_SIZE);
  205. /*
  206. * Free the otherwise unused pages.
  207. */
  208. while (page < end) {
  209. __free_page(page);
  210. page++;
  211. }
  212. return (void *)c->vm_start;
  213. }
  214. if (page)
  215. __free_pages(page, order);
  216. no_page:
  217. return NULL;
  218. }
  219. EXPORT_SYMBOL(__dma_alloc_coherent);
  220. /*
  221. * free a page as defined by the above mapping.
  222. */
  223. void __dma_free_coherent(size_t size, void *vaddr)
  224. {
  225. struct ppc_vm_region *c;
  226. unsigned long flags, addr;
  227. size = PAGE_ALIGN(size);
  228. spin_lock_irqsave(&consistent_lock, flags);
  229. c = ppc_vm_region_find(&consistent_head, (unsigned long)vaddr);
  230. if (!c)
  231. goto no_area;
  232. if ((c->vm_end - c->vm_start) != size) {
  233. printk(KERN_ERR "%s: freeing wrong coherent size (%ld != %d)\n",
  234. __func__, c->vm_end - c->vm_start, size);
  235. dump_stack();
  236. size = c->vm_end - c->vm_start;
  237. }
  238. addr = c->vm_start;
  239. do {
  240. pte_t *ptep;
  241. unsigned long pfn;
  242. ptep = pte_offset_kernel(pmd_offset(pud_offset(pgd_offset_k(addr),
  243. addr),
  244. addr),
  245. addr);
  246. if (!pte_none(*ptep) && pte_present(*ptep)) {
  247. pfn = pte_pfn(*ptep);
  248. pte_clear(&init_mm, addr, ptep);
  249. if (pfn_valid(pfn)) {
  250. struct page *page = pfn_to_page(pfn);
  251. __free_reserved_page(page);
  252. }
  253. }
  254. addr += PAGE_SIZE;
  255. } while (size -= PAGE_SIZE);
  256. flush_tlb_kernel_range(c->vm_start, c->vm_end);
  257. list_del(&c->vm_list);
  258. spin_unlock_irqrestore(&consistent_lock, flags);
  259. kfree(c);
  260. return;
  261. no_area:
  262. spin_unlock_irqrestore(&consistent_lock, flags);
  263. printk(KERN_ERR "%s: trying to free invalid coherent area: %p\n",
  264. __func__, vaddr);
  265. dump_stack();
  266. }
  267. EXPORT_SYMBOL(__dma_free_coherent);
  268. /*
  269. * make an area consistent.
  270. */
  271. void __dma_sync(void *vaddr, size_t size, int direction)
  272. {
  273. unsigned long start = (unsigned long)vaddr;
  274. unsigned long end = start + size;
  275. switch (direction) {
  276. case DMA_NONE:
  277. BUG();
  278. case DMA_FROM_DEVICE:
  279. /*
  280. * invalidate only when cache-line aligned otherwise there is
  281. * the potential for discarding uncommitted data from the cache
  282. */
  283. if ((start & (L1_CACHE_BYTES - 1)) || (size & (L1_CACHE_BYTES - 1)))
  284. flush_dcache_range(start, end);
  285. else
  286. invalidate_dcache_range(start, end);
  287. break;
  288. case DMA_TO_DEVICE: /* writeback only */
  289. clean_dcache_range(start, end);
  290. break;
  291. case DMA_BIDIRECTIONAL: /* writeback and invalidate */
  292. flush_dcache_range(start, end);
  293. break;
  294. }
  295. }
  296. EXPORT_SYMBOL(__dma_sync);
  297. #ifdef CONFIG_HIGHMEM
  298. /*
  299. * __dma_sync_page() implementation for systems using highmem.
  300. * In this case, each page of a buffer must be kmapped/kunmapped
  301. * in order to have a virtual address for __dma_sync(). This must
  302. * not sleep so kmap_atomic()/kunmap_atomic() are used.
  303. *
  304. * Note: yes, it is possible and correct to have a buffer extend
  305. * beyond the first page.
  306. */
  307. static inline void __dma_sync_page_highmem(struct page *page,
  308. unsigned long offset, size_t size, int direction)
  309. {
  310. size_t seg_size = min((size_t)(PAGE_SIZE - offset), size);
  311. size_t cur_size = seg_size;
  312. unsigned long flags, start, seg_offset = offset;
  313. int nr_segs = 1 + ((size - seg_size) + PAGE_SIZE - 1)/PAGE_SIZE;
  314. int seg_nr = 0;
  315. local_irq_save(flags);
  316. do {
  317. start = (unsigned long)kmap_atomic(page + seg_nr) + seg_offset;
  318. /* Sync this buffer segment */
  319. __dma_sync((void *)start, seg_size, direction);
  320. kunmap_atomic((void *)start);
  321. seg_nr++;
  322. /* Calculate next buffer segment size */
  323. seg_size = min((size_t)PAGE_SIZE, size - cur_size);
  324. /* Add the segment size to our running total */
  325. cur_size += seg_size;
  326. seg_offset = 0;
  327. } while (seg_nr < nr_segs);
  328. local_irq_restore(flags);
  329. }
  330. #endif /* CONFIG_HIGHMEM */
  331. /*
  332. * __dma_sync_page makes memory consistent. identical to __dma_sync, but
  333. * takes a struct page instead of a virtual address
  334. */
  335. void __dma_sync_page(struct page *page, unsigned long offset,
  336. size_t size, int direction)
  337. {
  338. #ifdef CONFIG_HIGHMEM
  339. __dma_sync_page_highmem(page, offset, size, direction);
  340. #else
  341. unsigned long start = (unsigned long)page_address(page) + offset;
  342. __dma_sync((void *)start, size, direction);
  343. #endif
  344. }
  345. EXPORT_SYMBOL(__dma_sync_page);
  346. /*
  347. * Return the PFN for a given cpu virtual address returned by
  348. * __dma_alloc_coherent. This is used by dma_mmap_coherent()
  349. */
  350. unsigned long __dma_get_coherent_pfn(unsigned long cpu_addr)
  351. {
  352. /* This should always be populated, so we don't test every
  353. * level. If that fails, we'll have a nice crash which
  354. * will be as good as a BUG_ON()
  355. */
  356. pgd_t *pgd = pgd_offset_k(cpu_addr);
  357. pud_t *pud = pud_offset(pgd, cpu_addr);
  358. pmd_t *pmd = pmd_offset(pud, cpu_addr);
  359. pte_t *ptep = pte_offset_kernel(pmd, cpu_addr);
  360. if (pte_none(*ptep) || !pte_present(*ptep))
  361. return 0;
  362. return pte_pfn(*ptep);
  363. }