2 ** PARISC 1.1 Dynamic DMA mapping support.
3 ** This implementation is for PA-RISC platforms that do not support
4 ** I/O TLBs (aka DMA address translation hardware).
5 ** See Documentation/DMA-mapping.txt for interface definitions.
7 ** (c) Copyright 1999,2000 Hewlett-Packard Company
8 ** (c) Copyright 2000 Grant Grundler
9 ** (c) Copyright 2000 Philipp Rumpf <prumpf@tux.org>
10 ** (c) Copyright 2000 John Marvin
12 ** "leveraged" from 2.3.47: arch/ia64/kernel/pci-dma.c.
13 ** (I assume it's from David Mosberger-Tang but there was no Copyright)
15 ** AFAIK, all PA7100LC and PA7300LC platforms can use this code.
20 #include <linux/init.h>
22 #include <linux/pci.h>
23 #include <linux/proc_fs.h>
24 #include <linux/slab.h>
25 #include <linux/string.h>
26 #include <linux/types.h>
28 #include <asm/cacheflush.h>
29 #include <asm/dma.h> /* for DMA_CHUNK_SIZE */
31 #include <asm/page.h> /* get_order */
32 #include <asm/pgalloc.h>
33 #include <asm/uaccess.h>
34 #include <asm/tlbflush.h> /* for purge_tlb_*() macros */
36 static struct proc_dir_entry * proc_gsc_root = NULL;
37 static int pcxl_proc_info(char *buffer, char **start, off_t offset, int length);
38 static unsigned long pcxl_used_bytes = 0;
39 static unsigned long pcxl_used_pages = 0;
41 extern unsigned long pcxl_dma_start; /* Start of pcxl dma mapping area */
42 static spinlock_t pcxl_res_lock;
43 static char *pcxl_res_map;
44 static int pcxl_res_hint;
45 static int pcxl_res_size;
47 #ifdef DEBUG_PCXL_RESOURCE
48 #define DBG_RES(x...) printk(x)
55 ** Dump a hex representation of the resource map.
60 void dump_resmap(void)
62 u_long *res_ptr = (unsigned long *)pcxl_res_map;
66 for(; i < (pcxl_res_size / sizeof(unsigned long)); ++i, ++res_ptr)
67 printk("%08lx ", *res_ptr);
72 static inline void dump_resmap(void) {;}
75 static int pa11_dma_supported( struct device *dev, u64 mask)
80 static inline int map_pte_uncached(pte_t * pte,
82 unsigned long size, unsigned long *paddr_ptr)
85 unsigned long orig_vaddr = vaddr;
93 printk(KERN_ERR "map_pte_uncached: page already exists\n");
94 set_pte(pte, __mk_pte(*paddr_ptr, PAGE_KERNEL_UNC));
96 pdtlb_kernel(orig_vaddr);
99 orig_vaddr += PAGE_SIZE;
100 (*paddr_ptr) += PAGE_SIZE;
102 } while (vaddr < end);
106 static inline int map_pmd_uncached(pmd_t * pmd, unsigned long vaddr,
107 unsigned long size, unsigned long *paddr_ptr)
110 unsigned long orig_vaddr = vaddr;
112 vaddr &= ~PGDIR_MASK;
114 if (end > PGDIR_SIZE)
117 pte_t * pte = pte_alloc_kernel(pmd, vaddr);
120 if (map_pte_uncached(pte, orig_vaddr, end - vaddr, paddr_ptr))
122 vaddr = (vaddr + PMD_SIZE) & PMD_MASK;
123 orig_vaddr += PMD_SIZE;
125 } while (vaddr < end);
129 static inline int map_uncached_pages(unsigned long vaddr, unsigned long size,
133 unsigned long end = vaddr + size;
135 dir = pgd_offset_k(vaddr);
139 pmd = pmd_alloc(NULL, dir, vaddr);
142 if (map_pmd_uncached(pmd, vaddr, end - vaddr, &paddr))
144 vaddr = vaddr + PGDIR_SIZE;
146 } while (vaddr && (vaddr < end));
150 static inline void unmap_uncached_pte(pmd_t * pmd, unsigned long vaddr,
155 unsigned long orig_vaddr = vaddr;
164 pte = pte_offset_map(pmd, vaddr);
171 pte_clear(&init_mm, vaddr, pte);
173 pdtlb_kernel(orig_vaddr);
176 orig_vaddr += PAGE_SIZE;
178 if (pte_none(page) || pte_present(page))
180 printk(KERN_CRIT "Whee.. Swapped out page in kernel page table\n");
181 } while (vaddr < end);
184 static inline void unmap_uncached_pmd(pgd_t * dir, unsigned long vaddr,
189 unsigned long orig_vaddr = vaddr;
198 pmd = pmd_offset(dir, vaddr);
199 vaddr &= ~PGDIR_MASK;
201 if (end > PGDIR_SIZE)
204 unmap_uncached_pte(pmd, orig_vaddr, end - vaddr);
205 vaddr = (vaddr + PMD_SIZE) & PMD_MASK;
206 orig_vaddr += PMD_SIZE;
208 } while (vaddr < end);
211 static void unmap_uncached_pages(unsigned long vaddr, unsigned long size)
214 unsigned long end = vaddr + size;
216 dir = pgd_offset_k(vaddr);
218 unmap_uncached_pmd(dir, vaddr, end - vaddr);
219 vaddr = vaddr + PGDIR_SIZE;
221 } while (vaddr && (vaddr < end));
224 #define PCXL_SEARCH_LOOP(idx, mask, size) \
225 for(; res_ptr < res_end; ++res_ptr) \
227 if(0 == ((*res_ptr) & mask)) { \
229 idx = (int)((u_long)res_ptr - (u_long)pcxl_res_map); \
230 pcxl_res_hint = idx + (size >> 3); \
231 goto resource_found; \
235 #define PCXL_FIND_FREE_MAPPING(idx, mask, size) { \
236 u##size *res_ptr = (u##size *)&(pcxl_res_map[pcxl_res_hint & ~((size >> 3) - 1)]); \
237 u##size *res_end = (u##size *)&pcxl_res_map[pcxl_res_size]; \
238 PCXL_SEARCH_LOOP(idx, mask, size); \
239 res_ptr = (u##size *)&pcxl_res_map[0]; \
240 PCXL_SEARCH_LOOP(idx, mask, size); \
244 pcxl_alloc_range(size_t size)
248 unsigned int pages_needed = size >> PAGE_SHIFT;
251 mask >>= BITS_PER_LONG - pages_needed;
253 DBG_RES("pcxl_alloc_range() size: %d pages_needed %d pages_mask 0x%08lx\n",
254 size, pages_needed, mask);
256 spin_lock_irqsave(&pcxl_res_lock, flags);
258 if(pages_needed <= 8) {
259 PCXL_FIND_FREE_MAPPING(res_idx, mask, 8);
260 } else if(pages_needed <= 16) {
261 PCXL_FIND_FREE_MAPPING(res_idx, mask, 16);
262 } else if(pages_needed <= 32) {
263 PCXL_FIND_FREE_MAPPING(res_idx, mask, 32);
265 panic("%s: pcxl_alloc_range() Too many pages to map.\n",
270 panic("%s: pcxl_alloc_range() out of dma mapping resources\n",
275 DBG_RES("pcxl_alloc_range() res_idx %d mask 0x%08lx res_hint: %d\n",
276 res_idx, mask, pcxl_res_hint);
278 pcxl_used_pages += pages_needed;
279 pcxl_used_bytes += ((pages_needed >> 3) ? (pages_needed >> 3) : 1);
281 spin_unlock_irqrestore(&pcxl_res_lock, flags);
286 ** return the corresponding vaddr in the pcxl dma map
288 return (pcxl_dma_start + (res_idx << (PAGE_SHIFT + 3)));
291 #define PCXL_FREE_MAPPINGS(idx, m, size) \
292 u##size *res_ptr = (u##size *)&(pcxl_res_map[(idx) + (((size >> 3) - 1) & (~((size >> 3) - 1)))]); \
293 /* BUG_ON((*res_ptr & m) != m); */ \
297 ** clear bits in the pcxl resource map
300 pcxl_free_range(unsigned long vaddr, size_t size)
303 unsigned int res_idx = (vaddr - pcxl_dma_start) >> (PAGE_SHIFT + 3);
304 unsigned int pages_mapped = size >> PAGE_SHIFT;
307 mask >>= BITS_PER_LONG - pages_mapped;
309 DBG_RES("pcxl_free_range() res_idx: %d size: %d pages_mapped %d mask 0x%08lx\n",
310 res_idx, size, pages_mapped, mask);
312 spin_lock_irqsave(&pcxl_res_lock, flags);
314 if(pages_mapped <= 8) {
315 PCXL_FREE_MAPPINGS(res_idx, mask, 8);
316 } else if(pages_mapped <= 16) {
317 PCXL_FREE_MAPPINGS(res_idx, mask, 16);
318 } else if(pages_mapped <= 32) {
319 PCXL_FREE_MAPPINGS(res_idx, mask, 32);
321 panic("%s: pcxl_free_range() Too many pages to unmap.\n",
325 pcxl_used_pages -= (pages_mapped ? pages_mapped : 1);
326 pcxl_used_bytes -= ((pages_mapped >> 3) ? (pages_mapped >> 3) : 1);
328 spin_unlock_irqrestore(&pcxl_res_lock, flags);
336 if (pcxl_dma_start == 0)
339 spin_lock_init(&pcxl_res_lock);
340 pcxl_res_size = PCXL_DMA_MAP_SIZE >> (PAGE_SHIFT + 3);
342 pcxl_res_map = (char *)__get_free_pages(GFP_KERNEL,
343 get_order(pcxl_res_size));
344 memset(pcxl_res_map, 0, pcxl_res_size);
345 proc_gsc_root = proc_mkdir("gsc", 0);
348 "pcxl_dma_init: Unable to create gsc /proc dir entry\n");
350 struct proc_dir_entry* ent;
351 ent = create_proc_info_entry("pcxl_dma", 0,
352 proc_gsc_root, pcxl_proc_info);
355 "pci-dma.c: Unable to create pcxl_dma /proc entry.\n");
360 __initcall(pcxl_dma_init);
362 static void * pa11_dma_alloc_consistent (struct device *dev, size_t size, dma_addr_t *dma_handle, gfp_t flag)
368 order = get_order(size);
369 size = 1 << (order + PAGE_SHIFT);
370 vaddr = pcxl_alloc_range(size);
371 paddr = __get_free_pages(flag, order);
372 flush_kernel_dcache_range(paddr, size);
374 map_uncached_pages(vaddr, size, paddr);
375 *dma_handle = (dma_addr_t) paddr;
378 /* This probably isn't needed to support EISA cards.
379 ** ISA cards will certainly only support 24-bit DMA addressing.
380 ** Not clear if we can, want, or need to support ISA.
382 if (!dev || *dev->coherent_dma_mask < 0xffffffff)
385 return (void *)vaddr;
388 static void pa11_dma_free_consistent (struct device *dev, size_t size, void *vaddr, dma_addr_t dma_handle)
392 order = get_order(size);
393 size = 1 << (order + PAGE_SHIFT);
394 unmap_uncached_pages((unsigned long)vaddr, size);
395 pcxl_free_range((unsigned long)vaddr, size);
396 free_pages((unsigned long)__va(dma_handle), order);
399 static dma_addr_t pa11_dma_map_single(struct device *dev, void *addr, size_t size, enum dma_data_direction direction)
401 if (direction == DMA_NONE) {
402 printk(KERN_ERR "pa11_dma_map_single(PCI_DMA_NONE) called by %p\n", __builtin_return_address(0));
406 flush_kernel_dcache_range((unsigned long) addr, size);
407 return virt_to_phys(addr);
410 static void pa11_dma_unmap_single(struct device *dev, dma_addr_t dma_handle, size_t size, enum dma_data_direction direction)
412 if (direction == DMA_NONE) {
413 printk(KERN_ERR "pa11_dma_unmap_single(PCI_DMA_NONE) called by %p\n", __builtin_return_address(0));
417 if (direction == DMA_TO_DEVICE)
421 * For PCI_DMA_FROMDEVICE this flush is not necessary for the
422 * simple map/unmap case. However, it IS necessary if if
423 * pci_dma_sync_single_* has been called and the buffer reused.
426 flush_kernel_dcache_range((unsigned long) phys_to_virt(dma_handle), size);
430 static int pa11_dma_map_sg(struct device *dev, struct scatterlist *sglist, int nents, enum dma_data_direction direction)
434 if (direction == DMA_NONE)
437 for (i = 0; i < nents; i++, sglist++ ) {
438 unsigned long vaddr = sg_virt_addr(sglist);
439 sg_dma_address(sglist) = (dma_addr_t) virt_to_phys(vaddr);
440 sg_dma_len(sglist) = sglist->length;
441 flush_kernel_dcache_range(vaddr, sglist->length);
446 static void pa11_dma_unmap_sg(struct device *dev, struct scatterlist *sglist, int nents, enum dma_data_direction direction)
450 if (direction == DMA_NONE)
453 if (direction == DMA_TO_DEVICE)
456 /* once we do combining we'll need to use phys_to_virt(sg_dma_address(sglist)) */
458 for (i = 0; i < nents; i++, sglist++ )
459 flush_kernel_dcache_range(sg_virt_addr(sglist), sglist->length);
463 static void pa11_dma_sync_single_for_cpu(struct device *dev, dma_addr_t dma_handle, unsigned long offset, size_t size, enum dma_data_direction direction)
465 if (direction == DMA_NONE)
468 flush_kernel_dcache_range((unsigned long) phys_to_virt(dma_handle) + offset, size);
471 static void pa11_dma_sync_single_for_device(struct device *dev, dma_addr_t dma_handle, unsigned long offset, size_t size, enum dma_data_direction direction)
473 if (direction == DMA_NONE)
476 flush_kernel_dcache_range((unsigned long) phys_to_virt(dma_handle) + offset, size);
479 static void pa11_dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sglist, int nents, enum dma_data_direction direction)
483 /* once we do combining we'll need to use phys_to_virt(sg_dma_address(sglist)) */
485 for (i = 0; i < nents; i++, sglist++ )
486 flush_kernel_dcache_range(sg_virt_addr(sglist), sglist->length);
489 static void pa11_dma_sync_sg_for_device(struct device *dev, struct scatterlist *sglist, int nents, enum dma_data_direction direction)
493 /* once we do combining we'll need to use phys_to_virt(sg_dma_address(sglist)) */
495 for (i = 0; i < nents; i++, sglist++ )
496 flush_kernel_dcache_range(sg_virt_addr(sglist), sglist->length);
499 struct hppa_dma_ops pcxl_dma_ops = {
500 .dma_supported = pa11_dma_supported,
501 .alloc_consistent = pa11_dma_alloc_consistent,
502 .alloc_noncoherent = pa11_dma_alloc_consistent,
503 .free_consistent = pa11_dma_free_consistent,
504 .map_single = pa11_dma_map_single,
505 .unmap_single = pa11_dma_unmap_single,
506 .map_sg = pa11_dma_map_sg,
507 .unmap_sg = pa11_dma_unmap_sg,
508 .dma_sync_single_for_cpu = pa11_dma_sync_single_for_cpu,
509 .dma_sync_single_for_device = pa11_dma_sync_single_for_device,
510 .dma_sync_sg_for_cpu = pa11_dma_sync_sg_for_cpu,
511 .dma_sync_sg_for_device = pa11_dma_sync_sg_for_device,
514 static void *fail_alloc_consistent(struct device *dev, size_t size,
515 dma_addr_t *dma_handle, gfp_t flag)
520 static void *pa11_dma_alloc_noncoherent(struct device *dev, size_t size,
521 dma_addr_t *dma_handle, gfp_t flag)
525 /* rely on kmalloc to be cacheline aligned */
526 addr = kmalloc(size, flag);
528 *dma_handle = (dma_addr_t)virt_to_phys(addr);
533 static void pa11_dma_free_noncoherent(struct device *dev, size_t size,
534 void *vaddr, dma_addr_t iova)
540 struct hppa_dma_ops pcx_dma_ops = {
541 .dma_supported = pa11_dma_supported,
542 .alloc_consistent = fail_alloc_consistent,
543 .alloc_noncoherent = pa11_dma_alloc_noncoherent,
544 .free_consistent = pa11_dma_free_noncoherent,
545 .map_single = pa11_dma_map_single,
546 .unmap_single = pa11_dma_unmap_single,
547 .map_sg = pa11_dma_map_sg,
548 .unmap_sg = pa11_dma_unmap_sg,
549 .dma_sync_single_for_cpu = pa11_dma_sync_single_for_cpu,
550 .dma_sync_single_for_device = pa11_dma_sync_single_for_device,
551 .dma_sync_sg_for_cpu = pa11_dma_sync_sg_for_cpu,
552 .dma_sync_sg_for_device = pa11_dma_sync_sg_for_device,
556 static int pcxl_proc_info(char *buf, char **start, off_t offset, int len)
560 unsigned long *res_ptr = (u_long *)pcxl_res_map;
562 unsigned long total_pages = pcxl_res_size << 3; /* 8 bits per byte */
564 sprintf(buf, "\nDMA Mapping Area size : %d bytes (%ld pages)\n",
565 PCXL_DMA_MAP_SIZE, total_pages);
567 sprintf(buf, "%sResource bitmap : %d bytes\n", buf, pcxl_res_size);
569 strcat(buf, " total: free: used: % used:\n");
570 sprintf(buf, "%sblocks %8d %8ld %8ld %8ld%%\n", buf, pcxl_res_size,
571 pcxl_res_size - pcxl_used_bytes, pcxl_used_bytes,
572 (pcxl_used_bytes * 100) / pcxl_res_size);
574 sprintf(buf, "%spages %8ld %8ld %8ld %8ld%%\n", buf, total_pages,
575 total_pages - pcxl_used_pages, pcxl_used_pages,
576 (pcxl_used_pages * 100 / total_pages));
579 strcat(buf, "\nResource bitmap:");
581 for(; i < (pcxl_res_size / sizeof(u_long)); ++i, ++res_ptr) {
584 sprintf(buf, "%s %08lx", buf, *res_ptr);