1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (C) 2005-2017 Andes Technology Corporation
4 #include <linux/types.h>
6 #include <linux/export.h>
7 #include <linux/string.h>
8 #include <linux/scatterlist.h>
9 #include <linux/dma-mapping.h>
11 #include <linux/cache.h>
12 #include <linux/highmem.h>
13 #include <linux/slab.h>
14 #include <asm/cacheflush.h>
15 #include <asm/tlbflush.h>
16 #include <asm/dma-mapping.h>
17 #include <asm/proc-fns.h>
20 * This is the page table (2MB) covering uncached, DMA consistent allocations
22 static pte_t *consistent_pte;
23 static DEFINE_RAW_SPINLOCK(consistent_lock);
26 * VM region handling support.
28 * This should become something generic, handling VM region allocations for
29 * vmalloc and similar (ioremap, module space, etc).
31 * I envisage vmalloc()'s supporting vm_struct becoming:
34 * struct vm_region region;
35 * unsigned long flags;
36 * struct page **pages;
37 * unsigned int nr_pages;
38 * unsigned long phys_addr;
41 * get_vm_area() would then call vm_region_alloc with an appropriate
42 * struct vm_region head (eg):
44 * struct vm_region vmalloc_head = {
45 * .vm_list = LIST_HEAD_INIT(vmalloc_head.vm_list),
46 * .vm_start = VMALLOC_START,
47 * .vm_end = VMALLOC_END,
50 * However, vmalloc_head.vm_start is variable (typically, it is dependent on
51 * the amount of RAM found at boot time.) I would imagine that get_vm_area()
52 * would have to initialise this each time prior to calling vm_region_alloc().
54 struct arch_vm_region {
55 struct list_head vm_list;
56 unsigned long vm_start;
58 struct page *vm_pages;
61 static struct arch_vm_region consistent_head = {
62 .vm_list = LIST_HEAD_INIT(consistent_head.vm_list),
63 .vm_start = CONSISTENT_BASE,
64 .vm_end = CONSISTENT_END,
67 static struct arch_vm_region *vm_region_alloc(struct arch_vm_region *head,
70 unsigned long addr = head->vm_start, end = head->vm_end - size;
72 struct arch_vm_region *c, *new;
74 new = kmalloc(sizeof(struct arch_vm_region), gfp);
78 raw_spin_lock_irqsave(&consistent_lock, flags);
80 list_for_each_entry(c, &head->vm_list, vm_list) {
81 if ((addr + size) < addr)
83 if ((addr + size) <= c->vm_start)
92 * Insert this entry _before_ the one we found.
94 list_add_tail(&new->vm_list, &c->vm_list);
96 new->vm_end = addr + size;
98 raw_spin_unlock_irqrestore(&consistent_lock, flags);
102 raw_spin_unlock_irqrestore(&consistent_lock, flags);
108 static struct arch_vm_region *vm_region_find(struct arch_vm_region *head,
111 struct arch_vm_region *c;
113 list_for_each_entry(c, &head->vm_list, vm_list) {
114 if (c->vm_start == addr)
122 /* FIXME: attrs is not used. */
123 static void *nds32_dma_alloc_coherent(struct device *dev, size_t size,
124 dma_addr_t * handle, gfp_t gfp,
128 struct arch_vm_region *c;
130 u64 mask = ~0ULL, limit;
131 pgprot_t prot = pgprot_noncached(PAGE_KERNEL);
133 if (!consistent_pte) {
134 pr_err("%s: not initialized\n", __func__);
140 mask = dev->coherent_dma_mask;
143 * Sanity check the DMA mask - it must be non-zero, and
144 * must be able to be satisfied by a DMA allocation.
147 dev_warn(dev, "coherent DMA mask is unset\n");
154 * Sanity check the allocation size.
156 size = PAGE_ALIGN(size);
157 limit = (mask + 1) & ~mask;
158 if ((limit && size >= limit) ||
159 size >= (CONSISTENT_END - CONSISTENT_BASE)) {
160 pr_warn("coherent allocation too big "
161 "(requested %#x mask %#llx)\n", size, mask);
165 order = get_order(size);
167 if (mask != 0xffffffff)
170 page = alloc_pages(gfp, order);
175 * Invalidate any data that might be lurking in the
176 * kernel direct-mapped region for device DMA.
179 unsigned long kaddr = (unsigned long)page_address(page);
180 memset(page_address(page), 0, size);
181 cpu_dma_wbinval_range(kaddr, kaddr + size);
185 * Allocate a virtual address in the consistent mapping region.
187 c = vm_region_alloc(&consistent_head, size,
188 gfp & ~(__GFP_DMA | __GFP_HIGHMEM));
190 pte_t *pte = consistent_pte + CONSISTENT_OFFSET(c->vm_start);
191 struct page *end = page + (1 << order);
196 * Set the "dma handle"
198 *handle = page_to_phys(page);
201 BUG_ON(!pte_none(*pte));
204 * x86 does not mark the pages reserved...
206 SetPageReserved(page);
207 set_pte(pte, mk_pte(page, prot));
210 } while (size -= PAGE_SIZE);
213 * Free the otherwise unused pages.
220 return (void *)c->vm_start;
224 __free_pages(page, order);
230 static void nds32_dma_free(struct device *dev, size_t size, void *cpu_addr,
231 dma_addr_t handle, unsigned long attrs)
233 struct arch_vm_region *c;
234 unsigned long flags, addr;
237 size = PAGE_ALIGN(size);
239 raw_spin_lock_irqsave(&consistent_lock, flags);
241 c = vm_region_find(&consistent_head, (unsigned long)cpu_addr);
245 if ((c->vm_end - c->vm_start) != size) {
246 pr_err("%s: freeing wrong coherent size (%ld != %d)\n",
247 __func__, c->vm_end - c->vm_start, size);
249 size = c->vm_end - c->vm_start;
252 ptep = consistent_pte + CONSISTENT_OFFSET(c->vm_start);
255 pte_t pte = ptep_get_and_clear(&init_mm, addr, ptep);
261 if (!pte_none(pte) && pte_present(pte)) {
264 if (pfn_valid(pfn)) {
265 struct page *page = pfn_to_page(pfn);
268 * x86 does not mark the pages reserved...
270 ClearPageReserved(page);
277 pr_crit("%s: bad page in kernel page table\n", __func__);
278 } while (size -= PAGE_SIZE);
280 flush_tlb_kernel_range(c->vm_start, c->vm_end);
282 list_del(&c->vm_list);
284 raw_spin_unlock_irqrestore(&consistent_lock, flags);
290 raw_spin_unlock_irqrestore(&consistent_lock, flags);
291 pr_err("%s: trying to free invalid coherent area: %p\n",
297 * Initialise the consistent memory allocation.
299 static int __init consistent_init(void)
307 pgd = pgd_offset(&init_mm, CONSISTENT_BASE);
308 pmd = pmd_alloc(&init_mm, pgd, CONSISTENT_BASE);
310 pr_err("%s: no pmd tables\n", __func__);
314 /* The first level mapping may be created in somewhere.
315 * It's not necessary to warn here. */
316 /* WARN_ON(!pmd_none(*pmd)); */
318 pte = pte_alloc_kernel(pmd, CONSISTENT_BASE);
324 consistent_pte = pte;
330 core_initcall(consistent_init);
332 static inline void cache_op(phys_addr_t paddr, size_t size,
333 void (*fn)(unsigned long start, unsigned long end))
335 struct page *page = pfn_to_page(paddr >> PAGE_SHIFT);
336 unsigned offset = paddr & ~PAGE_MASK;
343 if (PageHighMem(page)) {
346 if (offset + len > PAGE_SIZE) {
347 if (offset >= PAGE_SIZE) {
348 page += offset >> PAGE_SHIFT;
349 offset &= ~PAGE_MASK;
351 len = PAGE_SIZE - offset;
354 addr = kmap_atomic(page);
355 start = (unsigned long)(addr + offset);
356 fn(start, start + len);
359 start = (unsigned long)phys_to_virt(paddr);
360 fn(start, start + size);
369 nds32_dma_sync_single_for_device(struct device *dev, dma_addr_t handle,
370 size_t size, enum dma_data_direction dir)
373 case DMA_FROM_DEVICE:
376 case DMA_BIDIRECTIONAL:
377 cache_op(handle, size, cpu_dma_wb_range);
385 nds32_dma_sync_single_for_cpu(struct device *dev, dma_addr_t handle,
386 size_t size, enum dma_data_direction dir)
391 case DMA_FROM_DEVICE:
392 case DMA_BIDIRECTIONAL:
393 cache_op(handle, size, cpu_dma_inval_range);
400 static dma_addr_t nds32_dma_map_page(struct device *dev, struct page *page,
401 unsigned long offset, size_t size,
402 enum dma_data_direction dir,
405 dma_addr_t dma_addr = page_to_phys(page) + offset;
407 if (!(attrs & DMA_ATTR_SKIP_CPU_SYNC))
408 nds32_dma_sync_single_for_device(dev, handle, size, dir);
412 static void nds32_dma_unmap_page(struct device *dev, dma_addr_t handle,
413 size_t size, enum dma_data_direction dir,
416 if (!(attrs & DMA_ATTR_SKIP_CPU_SYNC))
417 nds32_dma_sync_single_for_cpu(dev, handle, size, dir);
421 nds32_dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
422 int nents, enum dma_data_direction dir)
426 for (i = 0; i < nents; i++, sg++) {
427 nds32_dma_sync_single_for_device(dev, sg_dma_address(sg),
433 nds32_dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg, int nents,
434 enum dma_data_direction dir)
438 for (i = 0; i < nents; i++, sg++) {
439 nds32_dma_sync_single_for_cpu(dev, sg_dma_address(sg),
444 static int nds32_dma_map_sg(struct device *dev, struct scatterlist *sg,
445 int nents, enum dma_data_direction dir,
450 for (i = 0; i < nents; i++, sg++) {
451 nds32_dma_sync_single_for_device(dev, sg_dma_address(sg),
457 static void nds32_dma_unmap_sg(struct device *dev, struct scatterlist *sg,
458 int nhwentries, enum dma_data_direction dir,
463 for (i = 0; i < nhwentries; i++, sg++) {
464 nds32_dma_sync_single_for_cpu(dev, sg_dma_address(sg),
469 struct dma_map_ops nds32_dma_ops = {
470 .alloc = nds32_dma_alloc_coherent,
471 .free = nds32_dma_free,
472 .map_page = nds32_dma_map_page,
473 .unmap_page = nds32_dma_unmap_page,
474 .map_sg = nds32_dma_map_sg,
475 .unmap_sg = nds32_dma_unmap_sg,
476 .sync_single_for_device = nds32_dma_sync_single_for_device,
477 .sync_single_for_cpu = nds32_dma_sync_single_for_cpu,
478 .sync_sg_for_cpu = nds32_dma_sync_sg_for_cpu,
479 .sync_sg_for_device = nds32_dma_sync_sg_for_device,
482 EXPORT_SYMBOL(nds32_dma_ops);