1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (C) 2005-2017 Andes Technology Corporation
4 #include <linux/types.h>
6 #include <linux/string.h>
7 #include <linux/dma-noncoherent.h>
9 #include <linux/cache.h>
10 #include <linux/highmem.h>
11 #include <linux/slab.h>
12 #include <asm/cacheflush.h>
13 #include <asm/tlbflush.h>
14 #include <asm/proc-fns.h>
17 * This is the page table (2MB) covering uncached, DMA consistent allocations
19 static pte_t
*consistent_pte
;
20 static DEFINE_RAW_SPINLOCK(consistent_lock
);
23 * VM region handling support.
25 * This should become something generic, handling VM region allocations for
26 * vmalloc and similar (ioremap, module space, etc).
28 * I envisage vmalloc()'s supporting vm_struct becoming:
31 * struct vm_region region;
32 * unsigned long flags;
33 * struct page **pages;
34 * unsigned int nr_pages;
35 * unsigned long phys_addr;
38 * get_vm_area() would then call vm_region_alloc with an appropriate
39 * struct vm_region head (eg):
41 * struct vm_region vmalloc_head = {
42 * .vm_list = LIST_HEAD_INIT(vmalloc_head.vm_list),
43 * .vm_start = VMALLOC_START,
44 * .vm_end = VMALLOC_END,
47 * However, vmalloc_head.vm_start is variable (typically, it is dependent on
48 * the amount of RAM found at boot time.) I would imagine that get_vm_area()
49 * would have to initialise this each time prior to calling vm_region_alloc().
51 struct arch_vm_region
{
52 struct list_head vm_list
;
53 unsigned long vm_start
;
55 struct page
*vm_pages
;
58 static struct arch_vm_region consistent_head
= {
59 .vm_list
= LIST_HEAD_INIT(consistent_head
.vm_list
),
60 .vm_start
= CONSISTENT_BASE
,
61 .vm_end
= CONSISTENT_END
,
64 static struct arch_vm_region
*vm_region_alloc(struct arch_vm_region
*head
,
67 unsigned long addr
= head
->vm_start
, end
= head
->vm_end
- size
;
69 struct arch_vm_region
*c
, *new;
71 new = kmalloc(sizeof(struct arch_vm_region
), gfp
);
75 raw_spin_lock_irqsave(&consistent_lock
, flags
);
77 list_for_each_entry(c
, &head
->vm_list
, vm_list
) {
78 if ((addr
+ size
) < addr
)
80 if ((addr
+ size
) <= c
->vm_start
)
89 * Insert this entry _before_ the one we found.
91 list_add_tail(&new->vm_list
, &c
->vm_list
);
93 new->vm_end
= addr
+ size
;
95 raw_spin_unlock_irqrestore(&consistent_lock
, flags
);
99 raw_spin_unlock_irqrestore(&consistent_lock
, flags
);
105 static struct arch_vm_region
*vm_region_find(struct arch_vm_region
*head
,
108 struct arch_vm_region
*c
;
110 list_for_each_entry(c
, &head
->vm_list
, vm_list
) {
111 if (c
->vm_start
== addr
)
119 void *arch_dma_alloc(struct device
*dev
, size_t size
, dma_addr_t
*handle
,
120 gfp_t gfp
, unsigned long attrs
)
123 struct arch_vm_region
*c
;
125 u64 mask
= ~0ULL, limit
;
126 pgprot_t prot
= pgprot_noncached(PAGE_KERNEL
);
128 if (!consistent_pte
) {
129 pr_err("%s: not initialized\n", __func__
);
135 mask
= dev
->coherent_dma_mask
;
138 * Sanity check the DMA mask - it must be non-zero, and
139 * must be able to be satisfied by a DMA allocation.
142 dev_warn(dev
, "coherent DMA mask is unset\n");
149 * Sanity check the allocation size.
151 size
= PAGE_ALIGN(size
);
152 limit
= (mask
+ 1) & ~mask
;
153 if ((limit
&& size
>= limit
) ||
154 size
>= (CONSISTENT_END
- CONSISTENT_BASE
)) {
155 pr_warn("coherent allocation too big "
156 "(requested %#x mask %#llx)\n", size
, mask
);
160 order
= get_order(size
);
162 if (mask
!= 0xffffffff)
165 page
= alloc_pages(gfp
, order
);
170 * Invalidate any data that might be lurking in the
171 * kernel direct-mapped region for device DMA.
174 unsigned long kaddr
= (unsigned long)page_address(page
);
175 memset(page_address(page
), 0, size
);
176 cpu_dma_wbinval_range(kaddr
, kaddr
+ size
);
180 * Allocate a virtual address in the consistent mapping region.
182 c
= vm_region_alloc(&consistent_head
, size
,
183 gfp
& ~(__GFP_DMA
| __GFP_HIGHMEM
));
185 pte_t
*pte
= consistent_pte
+ CONSISTENT_OFFSET(c
->vm_start
);
186 struct page
*end
= page
+ (1 << order
);
191 * Set the "dma handle"
193 *handle
= page_to_phys(page
);
196 BUG_ON(!pte_none(*pte
));
199 * x86 does not mark the pages reserved...
201 SetPageReserved(page
);
202 set_pte(pte
, mk_pte(page
, prot
));
205 } while (size
-= PAGE_SIZE
);
208 * Free the otherwise unused pages.
215 return (void *)c
->vm_start
;
219 __free_pages(page
, order
);
225 void arch_dma_free(struct device
*dev
, size_t size
, void *cpu_addr
,
226 dma_addr_t handle
, unsigned long attrs
)
228 struct arch_vm_region
*c
;
229 unsigned long flags
, addr
;
232 size
= PAGE_ALIGN(size
);
234 raw_spin_lock_irqsave(&consistent_lock
, flags
);
236 c
= vm_region_find(&consistent_head
, (unsigned long)cpu_addr
);
240 if ((c
->vm_end
- c
->vm_start
) != size
) {
241 pr_err("%s: freeing wrong coherent size (%ld != %d)\n",
242 __func__
, c
->vm_end
- c
->vm_start
, size
);
244 size
= c
->vm_end
- c
->vm_start
;
247 ptep
= consistent_pte
+ CONSISTENT_OFFSET(c
->vm_start
);
250 pte_t pte
= ptep_get_and_clear(&init_mm
, addr
, ptep
);
256 if (!pte_none(pte
) && pte_present(pte
)) {
259 if (pfn_valid(pfn
)) {
260 struct page
*page
= pfn_to_page(pfn
);
263 * x86 does not mark the pages reserved...
265 ClearPageReserved(page
);
272 pr_crit("%s: bad page in kernel page table\n", __func__
);
273 } while (size
-= PAGE_SIZE
);
275 flush_tlb_kernel_range(c
->vm_start
, c
->vm_end
);
277 list_del(&c
->vm_list
);
279 raw_spin_unlock_irqrestore(&consistent_lock
, flags
);
285 raw_spin_unlock_irqrestore(&consistent_lock
, flags
);
286 pr_err("%s: trying to free invalid coherent area: %p\n",
292 * Initialise the consistent memory allocation.
294 static int __init
consistent_init(void)
302 pgd
= pgd_offset(&init_mm
, CONSISTENT_BASE
);
303 pmd
= pmd_alloc(&init_mm
, pgd
, CONSISTENT_BASE
);
305 pr_err("%s: no pmd tables\n", __func__
);
309 /* The first level mapping may be created in somewhere.
310 * It's not necessary to warn here. */
311 /* WARN_ON(!pmd_none(*pmd)); */
313 pte
= pte_alloc_kernel(pmd
, CONSISTENT_BASE
);
319 consistent_pte
= pte
;
325 core_initcall(consistent_init
);
327 static inline void cache_op(phys_addr_t paddr
, size_t size
,
328 void (*fn
)(unsigned long start
, unsigned long end
))
330 struct page
*page
= pfn_to_page(paddr
>> PAGE_SHIFT
);
331 unsigned offset
= paddr
& ~PAGE_MASK
;
338 if (PageHighMem(page
)) {
341 if (offset
+ len
> PAGE_SIZE
) {
342 if (offset
>= PAGE_SIZE
) {
343 page
+= offset
>> PAGE_SHIFT
;
344 offset
&= ~PAGE_MASK
;
346 len
= PAGE_SIZE
- offset
;
349 addr
= kmap_atomic(page
);
350 start
= (unsigned long)(addr
+ offset
);
351 fn(start
, start
+ len
);
354 start
= (unsigned long)phys_to_virt(paddr
);
355 fn(start
, start
+ size
);
363 void arch_sync_dma_for_device(struct device
*dev
, phys_addr_t paddr
,
364 size_t size
, enum dma_data_direction dir
)
367 case DMA_FROM_DEVICE
:
370 case DMA_BIDIRECTIONAL
:
371 cache_op(paddr
, size
, cpu_dma_wb_range
);
378 void arch_sync_dma_for_cpu(struct device
*dev
, phys_addr_t paddr
,
379 size_t size
, enum dma_data_direction dir
)
384 case DMA_FROM_DEVICE
:
385 case DMA_BIDIRECTIONAL
:
386 cache_op(paddr
, size
, cpu_dma_inval_range
);