Back to home page

LXR

 
 

    


0001 /*
0002  * Re-map IO memory to kernel address space so that we can access it.
0003  * This is needed for high PCI addresses that aren't mapped in the
0004  * 640k-1MB IO memory area on PC's
0005  *
0006  * (C) Copyright 1995 1996 Linus Torvalds
0007  */
0008 #include <linux/vmalloc.h>
0009 #include <linux/mm.h>
0010 #include <linux/sched.h>
0011 #include <linux/io.h>
0012 #include <linux/export.h>
0013 #include <asm/cacheflush.h>
0014 #include <asm/pgtable.h>
0015 
0016 #ifdef CONFIG_HAVE_ARCH_HUGE_VMAP
0017 static int __read_mostly ioremap_pud_capable;
0018 static int __read_mostly ioremap_pmd_capable;
0019 static int __read_mostly ioremap_huge_disabled;
0020 
0021 static int __init set_nohugeiomap(char *str)
0022 {
0023     ioremap_huge_disabled = 1;
0024     return 0;
0025 }
0026 early_param("nohugeiomap", set_nohugeiomap);
0027 
0028 void __init ioremap_huge_init(void)
0029 {
0030     if (!ioremap_huge_disabled) {
0031         if (arch_ioremap_pud_supported())
0032             ioremap_pud_capable = 1;
0033         if (arch_ioremap_pmd_supported())
0034             ioremap_pmd_capable = 1;
0035     }
0036 }
0037 
0038 static inline int ioremap_pud_enabled(void)
0039 {
0040     return ioremap_pud_capable;
0041 }
0042 
0043 static inline int ioremap_pmd_enabled(void)
0044 {
0045     return ioremap_pmd_capable;
0046 }
0047 
0048 #else   /* !CONFIG_HAVE_ARCH_HUGE_VMAP */
0049 static inline int ioremap_pud_enabled(void) { return 0; }
0050 static inline int ioremap_pmd_enabled(void) { return 0; }
0051 #endif  /* CONFIG_HAVE_ARCH_HUGE_VMAP */
0052 
0053 static int ioremap_pte_range(pmd_t *pmd, unsigned long addr,
0054         unsigned long end, phys_addr_t phys_addr, pgprot_t prot)
0055 {
0056     pte_t *pte;
0057     u64 pfn;
0058 
0059     pfn = phys_addr >> PAGE_SHIFT;
0060     pte = pte_alloc_kernel(pmd, addr);
0061     if (!pte)
0062         return -ENOMEM;
0063     do {
0064         BUG_ON(!pte_none(*pte));
0065         set_pte_at(&init_mm, addr, pte, pfn_pte(pfn, prot));
0066         pfn++;
0067     } while (pte++, addr += PAGE_SIZE, addr != end);
0068     return 0;
0069 }
0070 
0071 static inline int ioremap_pmd_range(pud_t *pud, unsigned long addr,
0072         unsigned long end, phys_addr_t phys_addr, pgprot_t prot)
0073 {
0074     pmd_t *pmd;
0075     unsigned long next;
0076 
0077     phys_addr -= addr;
0078     pmd = pmd_alloc(&init_mm, pud, addr);
0079     if (!pmd)
0080         return -ENOMEM;
0081     do {
0082         next = pmd_addr_end(addr, end);
0083 
0084         if (ioremap_pmd_enabled() &&
0085             ((next - addr) == PMD_SIZE) &&
0086             IS_ALIGNED(phys_addr + addr, PMD_SIZE)) {
0087             if (pmd_set_huge(pmd, phys_addr + addr, prot))
0088                 continue;
0089         }
0090 
0091         if (ioremap_pte_range(pmd, addr, next, phys_addr + addr, prot))
0092             return -ENOMEM;
0093     } while (pmd++, addr = next, addr != end);
0094     return 0;
0095 }
0096 
0097 static inline int ioremap_pud_range(pgd_t *pgd, unsigned long addr,
0098         unsigned long end, phys_addr_t phys_addr, pgprot_t prot)
0099 {
0100     pud_t *pud;
0101     unsigned long next;
0102 
0103     phys_addr -= addr;
0104     pud = pud_alloc(&init_mm, pgd, addr);
0105     if (!pud)
0106         return -ENOMEM;
0107     do {
0108         next = pud_addr_end(addr, end);
0109 
0110         if (ioremap_pud_enabled() &&
0111             ((next - addr) == PUD_SIZE) &&
0112             IS_ALIGNED(phys_addr + addr, PUD_SIZE)) {
0113             if (pud_set_huge(pud, phys_addr + addr, prot))
0114                 continue;
0115         }
0116 
0117         if (ioremap_pmd_range(pud, addr, next, phys_addr + addr, prot))
0118             return -ENOMEM;
0119     } while (pud++, addr = next, addr != end);
0120     return 0;
0121 }
0122 
0123 int ioremap_page_range(unsigned long addr,
0124                unsigned long end, phys_addr_t phys_addr, pgprot_t prot)
0125 {
0126     pgd_t *pgd;
0127     unsigned long start;
0128     unsigned long next;
0129     int err;
0130 
0131     BUG_ON(addr >= end);
0132 
0133     start = addr;
0134     phys_addr -= addr;
0135     pgd = pgd_offset_k(addr);
0136     do {
0137         next = pgd_addr_end(addr, end);
0138         err = ioremap_pud_range(pgd, addr, next, phys_addr+addr, prot);
0139         if (err)
0140             break;
0141     } while (pgd++, addr = next, addr != end);
0142 
0143     flush_cache_vmap(start, end);
0144 
0145     return err;
0146 }