dma.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. /*
  2. * Meta version derived from arch/powerpc/lib/dma-noncoherent.c
  3. * Copyright (C) 2008 Imagination Technologies Ltd.
  4. *
  5. * PowerPC version derived from arch/arm/mm/consistent.c
  6. * Copyright (C) 2001 Dan Malek (dmalek@jlc.net)
  7. *
  8. * Copyright (C) 2000 Russell King
  9. *
  10. * Consistent memory allocators. Used for DMA devices that want to
  11. * share uncached memory with the processor core. The function return
  12. * is the virtual address and 'dma_handle' is the physical address.
  13. * Mostly stolen from the ARM port, with some changes for PowerPC.
  14. * -- Dan
  15. *
  16. * Reorganized to get rid of the arch-specific consistent_* functions
  17. * and provide non-coherent implementations for the DMA API. -Matt
  18. *
  19. * Added in_interrupt() safe dma_alloc_coherent()/dma_free_coherent()
  20. * implementation. This is pulled straight from ARM and barely
  21. * modified. -Matt
  22. *
  23. * This program is free software; you can redistribute it and/or modify
  24. * it under the terms of the GNU General Public License version 2 as
  25. * published by the Free Software Foundation.
  26. */
  27. #include <linux/sched.h>
  28. #include <linux/kernel.h>
  29. #include <linux/errno.h>
  30. #include <linux/export.h>
  31. #include <linux/string.h>
  32. #include <linux/types.h>
  33. #include <linux/highmem.h>
  34. #include <linux/dma-mapping.h>
  35. #include <linux/slab.h>
  36. #include <asm/tlbflush.h>
  37. #include <asm/mmu.h>
  38. #define CONSISTENT_OFFSET(x) (((unsigned long)(x) - CONSISTENT_START) \
  39. >> PAGE_SHIFT)
  40. static u64 get_coherent_dma_mask(struct device *dev)
  41. {
  42. u64 mask = ~0ULL;
  43. if (dev) {
  44. mask = dev->coherent_dma_mask;
  45. /*
  46. * Sanity check the DMA mask - it must be non-zero, and
  47. * must be able to be satisfied by a DMA allocation.
  48. */
  49. if (mask == 0) {
  50. dev_warn(dev, "coherent DMA mask is unset\n");
  51. return 0;
  52. }
  53. }
  54. return mask;
  55. }
  56. /*
  57. * This is the page table (2MB) covering uncached, DMA consistent allocations
  58. */
  59. static pte_t *consistent_pte;
  60. static DEFINE_SPINLOCK(consistent_lock);
  61. /*
  62. * VM region handling support.
  63. *
  64. * This should become something generic, handling VM region allocations for
  65. * vmalloc and similar (ioremap, module space, etc).
  66. *
  67. * I envisage vmalloc()'s supporting vm_struct becoming:
  68. *
  69. * struct vm_struct {
  70. * struct metag_vm_region region;
  71. * unsigned long flags;
  72. * struct page **pages;
  73. * unsigned int nr_pages;
  74. * unsigned long phys_addr;
  75. * };
  76. *
  77. * get_vm_area() would then call metag_vm_region_alloc with an appropriate
  78. * struct metag_vm_region head (eg):
  79. *
  80. * struct metag_vm_region vmalloc_head = {
  81. * .vm_list = LIST_HEAD_INIT(vmalloc_head.vm_list),
  82. * .vm_start = VMALLOC_START,
  83. * .vm_end = VMALLOC_END,
  84. * };
  85. *
  86. * However, vmalloc_head.vm_start is variable (typically, it is dependent on
  87. * the amount of RAM found at boot time.) I would imagine that get_vm_area()
  88. * would have to initialise this each time prior to calling
  89. * metag_vm_region_alloc().
  90. */
  91. struct metag_vm_region {
  92. struct list_head vm_list;
  93. unsigned long vm_start;
  94. unsigned long vm_end;
  95. struct page *vm_pages;
  96. int vm_active;
  97. };
  98. static struct metag_vm_region consistent_head = {
  99. .vm_list = LIST_HEAD_INIT(consistent_head.vm_list),
  100. .vm_start = CONSISTENT_START,
  101. .vm_end = CONSISTENT_END,
  102. };
  103. static struct metag_vm_region *metag_vm_region_alloc(struct metag_vm_region
  104. *head, size_t size,
  105. gfp_t gfp)
  106. {
  107. unsigned long addr = head->vm_start, end = head->vm_end - size;
  108. unsigned long flags;
  109. struct metag_vm_region *c, *new;
  110. new = kmalloc(sizeof(struct metag_vm_region), gfp);
  111. if (!new)
  112. goto out;
  113. spin_lock_irqsave(&consistent_lock, flags);
  114. list_for_each_entry(c, &head->vm_list, vm_list) {
  115. if ((addr + size) < addr)
  116. goto nospc;
  117. if ((addr + size) <= c->vm_start)
  118. goto found;
  119. addr = c->vm_end;
  120. if (addr > end)
  121. goto nospc;
  122. }
  123. found:
  124. /*
  125. * Insert this entry _before_ the one we found.
  126. */
  127. list_add_tail(&new->vm_list, &c->vm_list);
  128. new->vm_start = addr;
  129. new->vm_end = addr + size;
  130. new->vm_active = 1;
  131. spin_unlock_irqrestore(&consistent_lock, flags);
  132. return new;
  133. nospc:
  134. spin_unlock_irqrestore(&consistent_lock, flags);
  135. kfree(new);
  136. out:
  137. return NULL;
  138. }
  139. static struct metag_vm_region *metag_vm_region_find(struct metag_vm_region
  140. *head, unsigned long addr)
  141. {
  142. struct metag_vm_region *c;
  143. list_for_each_entry(c, &head->vm_list, vm_list) {
  144. if (c->vm_active && c->vm_start == addr)
  145. goto out;
  146. }
  147. c = NULL;
  148. out:
  149. return c;
  150. }
  151. /*
  152. * Allocate DMA-coherent memory space and return both the kernel remapped
  153. * virtual and bus address for that space.
  154. */
  155. void *dma_alloc_coherent(struct device *dev, size_t size,
  156. dma_addr_t *handle, gfp_t gfp)
  157. {
  158. struct page *page;
  159. struct metag_vm_region *c;
  160. unsigned long order;
  161. u64 mask = get_coherent_dma_mask(dev);
  162. u64 limit;
  163. if (!consistent_pte) {
  164. pr_err("%s: not initialised\n", __func__);
  165. dump_stack();
  166. return NULL;
  167. }
  168. if (!mask)
  169. goto no_page;
  170. size = PAGE_ALIGN(size);
  171. limit = (mask + 1) & ~mask;
  172. if ((limit && size >= limit)
  173. || size >= (CONSISTENT_END - CONSISTENT_START)) {
  174. pr_warn("coherent allocation too big (requested %#x mask %#Lx)\n",
  175. size, mask);
  176. return NULL;
  177. }
  178. order = get_order(size);
  179. if (mask != 0xffffffff)
  180. gfp |= GFP_DMA;
  181. page = alloc_pages(gfp, order);
  182. if (!page)
  183. goto no_page;
  184. /*
  185. * Invalidate any data that might be lurking in the
  186. * kernel direct-mapped region for device DMA.
  187. */
  188. {
  189. void *kaddr = page_address(page);
  190. memset(kaddr, 0, size);
  191. flush_dcache_region(kaddr, size);
  192. }
  193. /*
  194. * Allocate a virtual address in the consistent mapping region.
  195. */
  196. c = metag_vm_region_alloc(&consistent_head, size,
  197. gfp & ~(__GFP_DMA | __GFP_HIGHMEM));
  198. if (c) {
  199. unsigned long vaddr = c->vm_start;
  200. pte_t *pte = consistent_pte + CONSISTENT_OFFSET(vaddr);
  201. struct page *end = page + (1 << order);
  202. c->vm_pages = page;
  203. split_page(page, order);
  204. /*
  205. * Set the "dma handle"
  206. */
  207. *handle = page_to_bus(page);
  208. do {
  209. BUG_ON(!pte_none(*pte));
  210. SetPageReserved(page);
  211. set_pte_at(&init_mm, vaddr,
  212. pte, mk_pte(page,
  213. pgprot_writecombine
  214. (PAGE_KERNEL)));
  215. page++;
  216. pte++;
  217. vaddr += PAGE_SIZE;
  218. } while (size -= PAGE_SIZE);
  219. /*
  220. * Free the otherwise unused pages.
  221. */
  222. while (page < end) {
  223. __free_page(page);
  224. page++;
  225. }
  226. return (void *)c->vm_start;
  227. }
  228. if (page)
  229. __free_pages(page, order);
  230. no_page:
  231. return NULL;
  232. }
  233. EXPORT_SYMBOL(dma_alloc_coherent);
  234. /*
  235. * free a page as defined by the above mapping.
  236. */
  237. void dma_free_coherent(struct device *dev, size_t size,
  238. void *vaddr, dma_addr_t dma_handle)
  239. {
  240. struct metag_vm_region *c;
  241. unsigned long flags, addr;
  242. pte_t *ptep;
  243. size = PAGE_ALIGN(size);
  244. spin_lock_irqsave(&consistent_lock, flags);
  245. c = metag_vm_region_find(&consistent_head, (unsigned long)vaddr);
  246. if (!c)
  247. goto no_area;
  248. c->vm_active = 0;
  249. if ((c->vm_end - c->vm_start) != size) {
  250. pr_err("%s: freeing wrong coherent size (%ld != %d)\n",
  251. __func__, c->vm_end - c->vm_start, size);
  252. dump_stack();
  253. size = c->vm_end - c->vm_start;
  254. }
  255. ptep = consistent_pte + CONSISTENT_OFFSET(c->vm_start);
  256. addr = c->vm_start;
  257. do {
  258. pte_t pte = ptep_get_and_clear(&init_mm, addr, ptep);
  259. unsigned long pfn;
  260. ptep++;
  261. addr += PAGE_SIZE;
  262. if (!pte_none(pte) && pte_present(pte)) {
  263. pfn = pte_pfn(pte);
  264. if (pfn_valid(pfn)) {
  265. struct page *page = pfn_to_page(pfn);
  266. __free_reserved_page(page);
  267. continue;
  268. }
  269. }
  270. pr_crit("%s: bad page in kernel page table\n",
  271. __func__);
  272. } while (size -= PAGE_SIZE);
  273. flush_tlb_kernel_range(c->vm_start, c->vm_end);
  274. list_del(&c->vm_list);
  275. spin_unlock_irqrestore(&consistent_lock, flags);
  276. kfree(c);
  277. return;
  278. no_area:
  279. spin_unlock_irqrestore(&consistent_lock, flags);
  280. pr_err("%s: trying to free invalid coherent area: %p\n",
  281. __func__, vaddr);
  282. dump_stack();
  283. }
  284. EXPORT_SYMBOL(dma_free_coherent);
  285. static int dma_mmap(struct device *dev, struct vm_area_struct *vma,
  286. void *cpu_addr, dma_addr_t dma_addr, size_t size)
  287. {
  288. int ret = -ENXIO;
  289. unsigned long flags, user_size, kern_size;
  290. struct metag_vm_region *c;
  291. user_size = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
  292. spin_lock_irqsave(&consistent_lock, flags);
  293. c = metag_vm_region_find(&consistent_head, (unsigned long)cpu_addr);
  294. spin_unlock_irqrestore(&consistent_lock, flags);
  295. if (c) {
  296. unsigned long off = vma->vm_pgoff;
  297. kern_size = (c->vm_end - c->vm_start) >> PAGE_SHIFT;
  298. if (off < kern_size &&
  299. user_size <= (kern_size - off)) {
  300. ret = remap_pfn_range(vma, vma->vm_start,
  301. page_to_pfn(c->vm_pages) + off,
  302. user_size << PAGE_SHIFT,
  303. vma->vm_page_prot);
  304. }
  305. }
  306. return ret;
  307. }
  308. int dma_mmap_coherent(struct device *dev, struct vm_area_struct *vma,
  309. void *cpu_addr, dma_addr_t dma_addr, size_t size)
  310. {
  311. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  312. return dma_mmap(dev, vma, cpu_addr, dma_addr, size);
  313. }
  314. EXPORT_SYMBOL(dma_mmap_coherent);
  315. int dma_mmap_writecombine(struct device *dev, struct vm_area_struct *vma,
  316. void *cpu_addr, dma_addr_t dma_addr, size_t size)
  317. {
  318. vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
  319. return dma_mmap(dev, vma, cpu_addr, dma_addr, size);
  320. }
  321. EXPORT_SYMBOL(dma_mmap_writecombine);
  322. /*
  323. * Initialise the consistent memory allocation.
  324. */
  325. static int __init dma_alloc_init(void)
  326. {
  327. pgd_t *pgd, *pgd_k;
  328. pud_t *pud, *pud_k;
  329. pmd_t *pmd, *pmd_k;
  330. pte_t *pte;
  331. int ret = 0;
  332. do {
  333. int offset = pgd_index(CONSISTENT_START);
  334. pgd = pgd_offset(&init_mm, CONSISTENT_START);
  335. pud = pud_alloc(&init_mm, pgd, CONSISTENT_START);
  336. pmd = pmd_alloc(&init_mm, pud, CONSISTENT_START);
  337. WARN_ON(!pmd_none(*pmd));
  338. pte = pte_alloc_kernel(pmd, CONSISTENT_START);
  339. if (!pte) {
  340. pr_err("%s: no pte tables\n", __func__);
  341. ret = -ENOMEM;
  342. break;
  343. }
  344. pgd_k = ((pgd_t *) mmu_get_base()) + offset;
  345. pud_k = pud_offset(pgd_k, CONSISTENT_START);
  346. pmd_k = pmd_offset(pud_k, CONSISTENT_START);
  347. set_pmd(pmd_k, *pmd);
  348. consistent_pte = pte;
  349. } while (0);
  350. return ret;
  351. }
  352. early_initcall(dma_alloc_init);
  353. /*
  354. * make an area consistent to devices.
  355. */
  356. void dma_sync_for_device(void *vaddr, size_t size, int dma_direction)
  357. {
  358. /*
  359. * Ensure any writes get through the write combiner. This is necessary
  360. * even with DMA_FROM_DEVICE, or the write may dirty the cache after
  361. * we've invalidated it and get written back during the DMA.
  362. */
  363. barrier();
  364. switch (dma_direction) {
  365. case DMA_BIDIRECTIONAL:
  366. /*
  367. * Writeback to ensure the device can see our latest changes and
  368. * so that we have no dirty lines, and invalidate the cache
  369. * lines too in preparation for receiving the buffer back
  370. * (dma_sync_for_cpu) later.
  371. */
  372. flush_dcache_region(vaddr, size);
  373. break;
  374. case DMA_TO_DEVICE:
  375. /*
  376. * Writeback to ensure the device can see our latest changes.
  377. * There's no need to invalidate as the device shouldn't write
  378. * to the buffer.
  379. */
  380. writeback_dcache_region(vaddr, size);
  381. break;
  382. case DMA_FROM_DEVICE:
  383. /*
  384. * Invalidate to ensure we have no dirty lines that could get
  385. * written back during the DMA. It's also safe to flush
  386. * (writeback) here if necessary.
  387. */
  388. invalidate_dcache_region(vaddr, size);
  389. break;
  390. case DMA_NONE:
  391. BUG();
  392. }
  393. wmb();
  394. }
  395. EXPORT_SYMBOL(dma_sync_for_device);
  396. /*
  397. * make an area consistent to the core.
  398. */
  399. void dma_sync_for_cpu(void *vaddr, size_t size, int dma_direction)
  400. {
  401. /*
  402. * Hardware L2 cache prefetch doesn't occur across 4K physical
  403. * boundaries, however according to Documentation/DMA-API-HOWTO.txt
  404. * kmalloc'd memory is DMA'able, so accesses in nearby memory could
  405. * trigger a cache fill in the DMA buffer.
  406. *
  407. * This should never cause dirty lines, so a flush or invalidate should
  408. * be safe to allow us to see data from the device.
  409. */
  410. if (_meta_l2c_pf_is_enabled()) {
  411. switch (dma_direction) {
  412. case DMA_BIDIRECTIONAL:
  413. case DMA_FROM_DEVICE:
  414. invalidate_dcache_region(vaddr, size);
  415. break;
  416. case DMA_TO_DEVICE:
  417. /* The device shouldn't have written to the buffer */
  418. break;
  419. case DMA_NONE:
  420. BUG();
  421. }
  422. }
  423. rmb();
  424. }
  425. EXPORT_SYMBOL(dma_sync_for_cpu);