dma.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. // SPDX-License-Identifier: GPL-2.0
  2. // Copyright (C) 2005-2017 Andes Technology Corporation
  3. #include <linux/types.h>
  4. #include <linux/mm.h>
  5. #include <linux/string.h>
  6. #include <linux/dma-noncoherent.h>
  7. #include <linux/io.h>
  8. #include <linux/cache.h>
  9. #include <linux/highmem.h>
  10. #include <linux/slab.h>
  11. #include <asm/cacheflush.h>
  12. #include <asm/tlbflush.h>
  13. #include <asm/proc-fns.h>
  14. /*
  15. * This is the page table (2MB) covering uncached, DMA consistent allocations
  16. */
  17. static pte_t *consistent_pte;
  18. static DEFINE_RAW_SPINLOCK(consistent_lock);
  19. /*
  20. * VM region handling support.
  21. *
  22. * This should become something generic, handling VM region allocations for
  23. * vmalloc and similar (ioremap, module space, etc).
  24. *
  25. * I envisage vmalloc()'s supporting vm_struct becoming:
  26. *
  27. * struct vm_struct {
  28. * struct vm_region region;
  29. * unsigned long flags;
  30. * struct page **pages;
  31. * unsigned int nr_pages;
  32. * unsigned long phys_addr;
  33. * };
  34. *
  35. * get_vm_area() would then call vm_region_alloc with an appropriate
  36. * struct vm_region head (eg):
  37. *
  38. * struct vm_region vmalloc_head = {
  39. * .vm_list = LIST_HEAD_INIT(vmalloc_head.vm_list),
  40. * .vm_start = VMALLOC_START,
  41. * .vm_end = VMALLOC_END,
  42. * };
  43. *
  44. * However, vmalloc_head.vm_start is variable (typically, it is dependent on
  45. * the amount of RAM found at boot time.) I would imagine that get_vm_area()
  46. * would have to initialise this each time prior to calling vm_region_alloc().
  47. */
  48. struct arch_vm_region {
  49. struct list_head vm_list;
  50. unsigned long vm_start;
  51. unsigned long vm_end;
  52. struct page *vm_pages;
  53. };
  54. static struct arch_vm_region consistent_head = {
  55. .vm_list = LIST_HEAD_INIT(consistent_head.vm_list),
  56. .vm_start = CONSISTENT_BASE,
  57. .vm_end = CONSISTENT_END,
  58. };
  59. static struct arch_vm_region *vm_region_alloc(struct arch_vm_region *head,
  60. size_t size, int gfp)
  61. {
  62. unsigned long addr = head->vm_start, end = head->vm_end - size;
  63. unsigned long flags;
  64. struct arch_vm_region *c, *new;
  65. new = kmalloc(sizeof(struct arch_vm_region), gfp);
  66. if (!new)
  67. goto out;
  68. raw_spin_lock_irqsave(&consistent_lock, flags);
  69. list_for_each_entry(c, &head->vm_list, vm_list) {
  70. if ((addr + size) < addr)
  71. goto nospc;
  72. if ((addr + size) <= c->vm_start)
  73. goto found;
  74. addr = c->vm_end;
  75. if (addr > end)
  76. goto nospc;
  77. }
  78. found:
  79. /*
  80. * Insert this entry _before_ the one we found.
  81. */
  82. list_add_tail(&new->vm_list, &c->vm_list);
  83. new->vm_start = addr;
  84. new->vm_end = addr + size;
  85. raw_spin_unlock_irqrestore(&consistent_lock, flags);
  86. return new;
  87. nospc:
  88. raw_spin_unlock_irqrestore(&consistent_lock, flags);
  89. kfree(new);
  90. out:
  91. return NULL;
  92. }
  93. static struct arch_vm_region *vm_region_find(struct arch_vm_region *head,
  94. unsigned long addr)
  95. {
  96. struct arch_vm_region *c;
  97. list_for_each_entry(c, &head->vm_list, vm_list) {
  98. if (c->vm_start == addr)
  99. goto out;
  100. }
  101. c = NULL;
  102. out:
  103. return c;
  104. }
  105. void *arch_dma_alloc(struct device *dev, size_t size, dma_addr_t *handle,
  106. gfp_t gfp, unsigned long attrs)
  107. {
  108. struct page *page;
  109. struct arch_vm_region *c;
  110. unsigned long order;
  111. u64 mask = ~0ULL, limit;
  112. pgprot_t prot = pgprot_noncached(PAGE_KERNEL);
  113. if (!consistent_pte) {
  114. pr_err("%s: not initialized\n", __func__);
  115. dump_stack();
  116. return NULL;
  117. }
  118. if (dev) {
  119. mask = dev->coherent_dma_mask;
  120. /*
  121. * Sanity check the DMA mask - it must be non-zero, and
  122. * must be able to be satisfied by a DMA allocation.
  123. */
  124. if (mask == 0) {
  125. dev_warn(dev, "coherent DMA mask is unset\n");
  126. goto no_page;
  127. }
  128. }
  129. /*
  130. * Sanity check the allocation size.
  131. */
  132. size = PAGE_ALIGN(size);
  133. limit = (mask + 1) & ~mask;
  134. if ((limit && size >= limit) ||
  135. size >= (CONSISTENT_END - CONSISTENT_BASE)) {
  136. pr_warn("coherent allocation too big "
  137. "(requested %#x mask %#llx)\n", size, mask);
  138. goto no_page;
  139. }
  140. order = get_order(size);
  141. if (mask != 0xffffffff)
  142. gfp |= GFP_DMA;
  143. page = alloc_pages(gfp, order);
  144. if (!page)
  145. goto no_page;
  146. /*
  147. * Invalidate any data that might be lurking in the
  148. * kernel direct-mapped region for device DMA.
  149. */
  150. {
  151. unsigned long kaddr = (unsigned long)page_address(page);
  152. memset(page_address(page), 0, size);
  153. cpu_dma_wbinval_range(kaddr, kaddr + size);
  154. }
  155. /*
  156. * Allocate a virtual address in the consistent mapping region.
  157. */
  158. c = vm_region_alloc(&consistent_head, size,
  159. gfp & ~(__GFP_DMA | __GFP_HIGHMEM));
  160. if (c) {
  161. pte_t *pte = consistent_pte + CONSISTENT_OFFSET(c->vm_start);
  162. struct page *end = page + (1 << order);
  163. c->vm_pages = page;
  164. /*
  165. * Set the "dma handle"
  166. */
  167. *handle = page_to_phys(page);
  168. do {
  169. BUG_ON(!pte_none(*pte));
  170. /*
  171. * x86 does not mark the pages reserved...
  172. */
  173. SetPageReserved(page);
  174. set_pte(pte, mk_pte(page, prot));
  175. page++;
  176. pte++;
  177. } while (size -= PAGE_SIZE);
  178. /*
  179. * Free the otherwise unused pages.
  180. */
  181. while (page < end) {
  182. __free_page(page);
  183. page++;
  184. }
  185. return (void *)c->vm_start;
  186. }
  187. if (page)
  188. __free_pages(page, order);
  189. no_page:
  190. *handle = ~0;
  191. return NULL;
  192. }
  193. void arch_dma_free(struct device *dev, size_t size, void *cpu_addr,
  194. dma_addr_t handle, unsigned long attrs)
  195. {
  196. struct arch_vm_region *c;
  197. unsigned long flags, addr;
  198. pte_t *ptep;
  199. size = PAGE_ALIGN(size);
  200. raw_spin_lock_irqsave(&consistent_lock, flags);
  201. c = vm_region_find(&consistent_head, (unsigned long)cpu_addr);
  202. if (!c)
  203. goto no_area;
  204. if ((c->vm_end - c->vm_start) != size) {
  205. pr_err("%s: freeing wrong coherent size (%ld != %d)\n",
  206. __func__, c->vm_end - c->vm_start, size);
  207. dump_stack();
  208. size = c->vm_end - c->vm_start;
  209. }
  210. ptep = consistent_pte + CONSISTENT_OFFSET(c->vm_start);
  211. addr = c->vm_start;
  212. do {
  213. pte_t pte = ptep_get_and_clear(&init_mm, addr, ptep);
  214. unsigned long pfn;
  215. ptep++;
  216. addr += PAGE_SIZE;
  217. if (!pte_none(pte) && pte_present(pte)) {
  218. pfn = pte_pfn(pte);
  219. if (pfn_valid(pfn)) {
  220. struct page *page = pfn_to_page(pfn);
  221. /*
  222. * x86 does not mark the pages reserved...
  223. */
  224. ClearPageReserved(page);
  225. __free_page(page);
  226. continue;
  227. }
  228. }
  229. pr_crit("%s: bad page in kernel page table\n", __func__);
  230. } while (size -= PAGE_SIZE);
  231. flush_tlb_kernel_range(c->vm_start, c->vm_end);
  232. list_del(&c->vm_list);
  233. raw_spin_unlock_irqrestore(&consistent_lock, flags);
  234. kfree(c);
  235. return;
  236. no_area:
  237. raw_spin_unlock_irqrestore(&consistent_lock, flags);
  238. pr_err("%s: trying to free invalid coherent area: %p\n",
  239. __func__, cpu_addr);
  240. dump_stack();
  241. }
  242. /*
  243. * Initialise the consistent memory allocation.
  244. */
  245. static int __init consistent_init(void)
  246. {
  247. pgd_t *pgd;
  248. pmd_t *pmd;
  249. pte_t *pte;
  250. int ret = 0;
  251. do {
  252. pgd = pgd_offset(&init_mm, CONSISTENT_BASE);
  253. pmd = pmd_alloc(&init_mm, pgd, CONSISTENT_BASE);
  254. if (!pmd) {
  255. pr_err("%s: no pmd tables\n", __func__);
  256. ret = -ENOMEM;
  257. break;
  258. }
  259. /* The first level mapping may be created in somewhere.
  260. * It's not necessary to warn here. */
  261. /* WARN_ON(!pmd_none(*pmd)); */
  262. pte = pte_alloc_kernel(pmd, CONSISTENT_BASE);
  263. if (!pte) {
  264. ret = -ENOMEM;
  265. break;
  266. }
  267. consistent_pte = pte;
  268. } while (0);
  269. return ret;
  270. }
  271. core_initcall(consistent_init);
  272. static inline void cache_op(phys_addr_t paddr, size_t size,
  273. void (*fn)(unsigned long start, unsigned long end))
  274. {
  275. struct page *page = pfn_to_page(paddr >> PAGE_SHIFT);
  276. unsigned offset = paddr & ~PAGE_MASK;
  277. size_t left = size;
  278. unsigned long start;
  279. do {
  280. size_t len = left;
  281. if (PageHighMem(page)) {
  282. void *addr;
  283. if (offset + len > PAGE_SIZE) {
  284. if (offset >= PAGE_SIZE) {
  285. page += offset >> PAGE_SHIFT;
  286. offset &= ~PAGE_MASK;
  287. }
  288. len = PAGE_SIZE - offset;
  289. }
  290. addr = kmap_atomic(page);
  291. start = (unsigned long)(addr + offset);
  292. fn(start, start + len);
  293. kunmap_atomic(addr);
  294. } else {
  295. start = (unsigned long)phys_to_virt(paddr);
  296. fn(start, start + size);
  297. }
  298. offset = 0;
  299. page++;
  300. left -= len;
  301. } while (left);
  302. }
  303. void arch_sync_dma_for_device(struct device *dev, phys_addr_t paddr,
  304. size_t size, enum dma_data_direction dir)
  305. {
  306. switch (dir) {
  307. case DMA_FROM_DEVICE:
  308. break;
  309. case DMA_TO_DEVICE:
  310. case DMA_BIDIRECTIONAL:
  311. cache_op(paddr, size, cpu_dma_wb_range);
  312. break;
  313. default:
  314. BUG();
  315. }
  316. }
  317. void arch_sync_dma_for_cpu(struct device *dev, phys_addr_t paddr,
  318. size_t size, enum dma_data_direction dir)
  319. {
  320. switch (dir) {
  321. case DMA_TO_DEVICE:
  322. break;
  323. case DMA_FROM_DEVICE:
  324. case DMA_BIDIRECTIONAL:
  325. cache_op(paddr, size, cpu_dma_inval_range);
  326. break;
  327. default:
  328. BUG();
  329. }
  330. }