1 // SPDX-License-Identifier: GPL-2.0
3 ** PARISC 1.1 Dynamic DMA mapping support.
4 ** This implementation is for PA-RISC platforms that do not support
5 ** I/O TLBs (aka DMA address translation hardware).
6 ** See Documentation/DMA-API-HOWTO.txt for interface definitions.
8 ** (c) Copyright 1999,2000 Hewlett-Packard Company
9 ** (c) Copyright 2000 Grant Grundler
10 ** (c) Copyright 2000 Philipp Rumpf <prumpf@tux.org>
11 ** (c) Copyright 2000 John Marvin
13 ** "leveraged" from 2.3.47: arch/ia64/kernel/pci-dma.c.
14 ** (I assume it's from David Mosberger-Tang but there was no Copyright)
16 ** AFAIK, all PA7100LC and PA7300LC platforms can use this code.
21 #include <linux/init.h>
22 #include <linux/gfp.h>
24 #include <linux/proc_fs.h>
25 #include <linux/seq_file.h>
26 #include <linux/string.h>
27 #include <linux/types.h>
28 #include <linux/dma-direct.h>
29 #include <linux/dma-noncoherent.h>
31 #include <asm/cacheflush.h>
32 #include <asm/dma.h> /* for DMA_CHUNK_SIZE */
34 #include <asm/page.h> /* get_order */
35 #include <asm/pgalloc.h>
36 #include <linux/uaccess.h>
37 #include <asm/tlbflush.h> /* for purge_tlb_*() macros */
39 static struct proc_dir_entry
* proc_gsc_root __read_mostly
= NULL
;
40 static unsigned long pcxl_used_bytes __read_mostly
= 0;
41 static unsigned long pcxl_used_pages __read_mostly
= 0;
43 extern unsigned long pcxl_dma_start
; /* Start of pcxl dma mapping area */
44 static DEFINE_SPINLOCK(pcxl_res_lock
);
45 static char *pcxl_res_map
;
46 static int pcxl_res_hint
;
47 static int pcxl_res_size
;
49 #ifdef DEBUG_PCXL_RESOURCE
50 #define DBG_RES(x...) printk(x)
57 ** Dump a hex representation of the resource map.
62 void dump_resmap(void)
64 u_long
*res_ptr
= (unsigned long *)pcxl_res_map
;
68 for(; i
< (pcxl_res_size
/ sizeof(unsigned long)); ++i
, ++res_ptr
)
69 printk("%08lx ", *res_ptr
);
74 static inline void dump_resmap(void) {;}
77 static inline int map_pte_uncached(pte_t
* pte
,
79 unsigned long size
, unsigned long *paddr_ptr
)
82 unsigned long orig_vaddr
= vaddr
;
92 printk(KERN_ERR
"map_pte_uncached: page already exists\n");
93 purge_tlb_start(flags
);
94 set_pte(pte
, __mk_pte(*paddr_ptr
, PAGE_KERNEL_UNC
));
95 pdtlb_kernel(orig_vaddr
);
98 orig_vaddr
+= PAGE_SIZE
;
99 (*paddr_ptr
) += PAGE_SIZE
;
101 } while (vaddr
< end
);
105 static inline int map_pmd_uncached(pmd_t
* pmd
, unsigned long vaddr
,
106 unsigned long size
, unsigned long *paddr_ptr
)
109 unsigned long orig_vaddr
= vaddr
;
111 vaddr
&= ~PGDIR_MASK
;
113 if (end
> PGDIR_SIZE
)
116 pte_t
* pte
= pte_alloc_kernel(pmd
, vaddr
);
119 if (map_pte_uncached(pte
, orig_vaddr
, end
- vaddr
, paddr_ptr
))
121 vaddr
= (vaddr
+ PMD_SIZE
) & PMD_MASK
;
122 orig_vaddr
+= PMD_SIZE
;
124 } while (vaddr
< end
);
128 static inline int map_uncached_pages(unsigned long vaddr
, unsigned long size
,
132 unsigned long end
= vaddr
+ size
;
134 dir
= pgd_offset_k(vaddr
);
140 p4d
= p4d_offset(dir
, vaddr
);
141 pud
= pud_offset(p4d
, vaddr
);
142 pmd
= pmd_alloc(NULL
, pud
, vaddr
);
146 if (map_pmd_uncached(pmd
, vaddr
, end
- vaddr
, &paddr
))
148 vaddr
= vaddr
+ PGDIR_SIZE
;
150 } while (vaddr
&& (vaddr
< end
));
154 static inline void unmap_uncached_pte(pmd_t
* pmd
, unsigned long vaddr
,
159 unsigned long orig_vaddr
= vaddr
;
168 pte
= pte_offset_map(pmd
, vaddr
);
177 pte_clear(&init_mm
, vaddr
, pte
);
178 purge_tlb_start(flags
);
179 pdtlb_kernel(orig_vaddr
);
180 purge_tlb_end(flags
);
182 orig_vaddr
+= PAGE_SIZE
;
184 if (pte_none(page
) || pte_present(page
))
186 printk(KERN_CRIT
"Whee.. Swapped out page in kernel page table\n");
187 } while (vaddr
< end
);
190 static inline void unmap_uncached_pmd(pgd_t
* dir
, unsigned long vaddr
,
195 unsigned long orig_vaddr
= vaddr
;
204 pmd
= pmd_offset(dir
, vaddr
);
205 vaddr
&= ~PGDIR_MASK
;
207 if (end
> PGDIR_SIZE
)
210 unmap_uncached_pte(pmd
, orig_vaddr
, end
- vaddr
);
211 vaddr
= (vaddr
+ PMD_SIZE
) & PMD_MASK
;
212 orig_vaddr
+= PMD_SIZE
;
214 } while (vaddr
< end
);
217 static void unmap_uncached_pages(unsigned long vaddr
, unsigned long size
)
220 unsigned long end
= vaddr
+ size
;
222 dir
= pgd_offset_k(vaddr
);
224 unmap_uncached_pmd(dir
, vaddr
, end
- vaddr
);
225 vaddr
= vaddr
+ PGDIR_SIZE
;
227 } while (vaddr
&& (vaddr
< end
));
230 #define PCXL_SEARCH_LOOP(idx, mask, size) \
231 for(; res_ptr < res_end; ++res_ptr) \
233 if(0 == ((*res_ptr) & mask)) { \
235 idx = (int)((u_long)res_ptr - (u_long)pcxl_res_map); \
236 pcxl_res_hint = idx + (size >> 3); \
237 goto resource_found; \
241 #define PCXL_FIND_FREE_MAPPING(idx, mask, size) { \
242 u##size *res_ptr = (u##size *)&(pcxl_res_map[pcxl_res_hint & ~((size >> 3) - 1)]); \
243 u##size *res_end = (u##size *)&pcxl_res_map[pcxl_res_size]; \
244 PCXL_SEARCH_LOOP(idx, mask, size); \
245 res_ptr = (u##size *)&pcxl_res_map[0]; \
246 PCXL_SEARCH_LOOP(idx, mask, size); \
250 pcxl_alloc_range(size_t size
)
254 unsigned int pages_needed
= size
>> PAGE_SHIFT
;
257 mask
>>= BITS_PER_LONG
- pages_needed
;
259 DBG_RES("pcxl_alloc_range() size: %d pages_needed %d pages_mask 0x%08lx\n",
260 size
, pages_needed
, mask
);
262 spin_lock_irqsave(&pcxl_res_lock
, flags
);
264 if(pages_needed
<= 8) {
265 PCXL_FIND_FREE_MAPPING(res_idx
, mask
, 8);
266 } else if(pages_needed
<= 16) {
267 PCXL_FIND_FREE_MAPPING(res_idx
, mask
, 16);
268 } else if(pages_needed
<= 32) {
269 PCXL_FIND_FREE_MAPPING(res_idx
, mask
, 32);
271 panic("%s: pcxl_alloc_range() Too many pages to map.\n",
276 panic("%s: pcxl_alloc_range() out of dma mapping resources\n",
281 DBG_RES("pcxl_alloc_range() res_idx %d mask 0x%08lx res_hint: %d\n",
282 res_idx
, mask
, pcxl_res_hint
);
284 pcxl_used_pages
+= pages_needed
;
285 pcxl_used_bytes
+= ((pages_needed
>> 3) ? (pages_needed
>> 3) : 1);
287 spin_unlock_irqrestore(&pcxl_res_lock
, flags
);
292 ** return the corresponding vaddr in the pcxl dma map
294 return (pcxl_dma_start
+ (res_idx
<< (PAGE_SHIFT
+ 3)));
297 #define PCXL_FREE_MAPPINGS(idx, m, size) \
298 u##size *res_ptr = (u##size *)&(pcxl_res_map[(idx) + (((size >> 3) - 1) & (~((size >> 3) - 1)))]); \
299 /* BUG_ON((*res_ptr & m) != m); */ \
303 ** clear bits in the pcxl resource map
306 pcxl_free_range(unsigned long vaddr
, size_t size
)
309 unsigned int res_idx
= (vaddr
- pcxl_dma_start
) >> (PAGE_SHIFT
+ 3);
310 unsigned int pages_mapped
= size
>> PAGE_SHIFT
;
313 mask
>>= BITS_PER_LONG
- pages_mapped
;
315 DBG_RES("pcxl_free_range() res_idx: %d size: %d pages_mapped %d mask 0x%08lx\n",
316 res_idx
, size
, pages_mapped
, mask
);
318 spin_lock_irqsave(&pcxl_res_lock
, flags
);
320 if(pages_mapped
<= 8) {
321 PCXL_FREE_MAPPINGS(res_idx
, mask
, 8);
322 } else if(pages_mapped
<= 16) {
323 PCXL_FREE_MAPPINGS(res_idx
, mask
, 16);
324 } else if(pages_mapped
<= 32) {
325 PCXL_FREE_MAPPINGS(res_idx
, mask
, 32);
327 panic("%s: pcxl_free_range() Too many pages to unmap.\n",
331 pcxl_used_pages
-= (pages_mapped
? pages_mapped
: 1);
332 pcxl_used_bytes
-= ((pages_mapped
>> 3) ? (pages_mapped
>> 3) : 1);
334 spin_unlock_irqrestore(&pcxl_res_lock
, flags
);
339 static int proc_pcxl_dma_show(struct seq_file
*m
, void *v
)
343 unsigned long *res_ptr
= (u_long
*)pcxl_res_map
;
345 unsigned long total_pages
= pcxl_res_size
<< 3; /* 8 bits per byte */
347 seq_printf(m
, "\nDMA Mapping Area size : %d bytes (%ld pages)\n",
348 PCXL_DMA_MAP_SIZE
, total_pages
);
350 seq_printf(m
, "Resource bitmap : %d bytes\n", pcxl_res_size
);
352 seq_puts(m
, " total: free: used: % used:\n");
353 seq_printf(m
, "blocks %8d %8ld %8ld %8ld%%\n", pcxl_res_size
,
354 pcxl_res_size
- pcxl_used_bytes
, pcxl_used_bytes
,
355 (pcxl_used_bytes
* 100) / pcxl_res_size
);
357 seq_printf(m
, "pages %8ld %8ld %8ld %8ld%%\n", total_pages
,
358 total_pages
- pcxl_used_pages
, pcxl_used_pages
,
359 (pcxl_used_pages
* 100 / total_pages
));
362 seq_puts(m
, "\nResource bitmap:");
364 for(; i
< (pcxl_res_size
/ sizeof(u_long
)); ++i
, ++res_ptr
) {
367 seq_printf(m
, "%s %08lx", buf
, *res_ptr
);
377 if (pcxl_dma_start
== 0)
380 pcxl_res_size
= PCXL_DMA_MAP_SIZE
>> (PAGE_SHIFT
+ 3);
382 pcxl_res_map
= (char *)__get_free_pages(GFP_KERNEL
,
383 get_order(pcxl_res_size
));
384 memset(pcxl_res_map
, 0, pcxl_res_size
);
385 proc_gsc_root
= proc_mkdir("gsc", NULL
);
388 "pcxl_dma_init: Unable to create gsc /proc dir entry\n");
390 struct proc_dir_entry
* ent
;
391 ent
= proc_create_single("pcxl_dma", 0, proc_gsc_root
,
395 "pci-dma.c: Unable to create pcxl_dma /proc entry.\n");
400 __initcall(pcxl_dma_init
);
402 void *arch_dma_alloc(struct device
*dev
, size_t size
,
403 dma_addr_t
*dma_handle
, gfp_t gfp
, unsigned long attrs
)
409 if (boot_cpu_data
.cpu_type
!= pcxl2
&& boot_cpu_data
.cpu_type
!= pcxl
)
412 order
= get_order(size
);
413 size
= 1 << (order
+ PAGE_SHIFT
);
414 vaddr
= pcxl_alloc_range(size
);
415 paddr
= __get_free_pages(gfp
| __GFP_ZERO
, order
);
416 flush_kernel_dcache_range(paddr
, size
);
418 map_uncached_pages(vaddr
, size
, paddr
);
419 *dma_handle
= (dma_addr_t
) paddr
;
422 /* This probably isn't needed to support EISA cards.
423 ** ISA cards will certainly only support 24-bit DMA addressing.
424 ** Not clear if we can, want, or need to support ISA.
426 if (!dev
|| *dev
->coherent_dma_mask
< 0xffffffff)
429 return (void *)vaddr
;
432 void arch_dma_free(struct device
*dev
, size_t size
, void *vaddr
,
433 dma_addr_t dma_handle
, unsigned long attrs
)
435 int order
= get_order(size
);
437 WARN_ON_ONCE(boot_cpu_data
.cpu_type
!= pcxl2
&&
438 boot_cpu_data
.cpu_type
!= pcxl
);
440 size
= 1 << (order
+ PAGE_SHIFT
);
441 unmap_uncached_pages((unsigned long)vaddr
, size
);
442 pcxl_free_range((unsigned long)vaddr
, size
);
444 free_pages((unsigned long)__va(dma_handle
), order
);
447 void arch_sync_dma_for_device(phys_addr_t paddr
, size_t size
,
448 enum dma_data_direction dir
)
450 flush_kernel_dcache_range((unsigned long)phys_to_virt(paddr
), size
);
453 void arch_sync_dma_for_cpu(phys_addr_t paddr
, size_t size
,
454 enum dma_data_direction dir
)
456 flush_kernel_dcache_range((unsigned long)phys_to_virt(paddr
), size
);
459 void arch_dma_cache_sync(struct device
*dev
, void *vaddr
, size_t size
,
460 enum dma_data_direction direction
)
462 flush_kernel_dcache_range((unsigned long)vaddr
, size
);