dma-noncoherent.c 10 KB

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