Back to home page

LXR

 
 

    


0001 /*
0002  * Provide common bits of early_ioremap() support for architectures needing
0003  * temporary mappings during boot before ioremap() is available.
0004  *
0005  * This is mostly a direct copy of the x86 early_ioremap implementation.
0006  *
0007  * (C) Copyright 1995 1996, 2014 Linus Torvalds
0008  *
0009  */
0010 #include <linux/kernel.h>
0011 #include <linux/init.h>
0012 #include <linux/io.h>
0013 #include <linux/module.h>
0014 #include <linux/slab.h>
0015 #include <linux/mm.h>
0016 #include <linux/vmalloc.h>
0017 #include <asm/fixmap.h>
0018 #include <asm/early_ioremap.h>
0019 
0020 #ifdef CONFIG_MMU
0021 static int early_ioremap_debug __initdata;
0022 
0023 static int __init early_ioremap_debug_setup(char *str)
0024 {
0025     early_ioremap_debug = 1;
0026 
0027     return 0;
0028 }
0029 early_param("early_ioremap_debug", early_ioremap_debug_setup);
0030 
0031 static int after_paging_init __initdata;
0032 
0033 void __init __weak early_ioremap_shutdown(void)
0034 {
0035 }
0036 
0037 void __init early_ioremap_reset(void)
0038 {
0039     early_ioremap_shutdown();
0040     after_paging_init = 1;
0041 }
0042 
0043 /*
0044  * Generally, ioremap() is available after paging_init() has been called.
0045  * Architectures wanting to allow early_ioremap after paging_init() can
0046  * define __late_set_fixmap and __late_clear_fixmap to do the right thing.
0047  */
0048 #ifndef __late_set_fixmap
0049 static inline void __init __late_set_fixmap(enum fixed_addresses idx,
0050                         phys_addr_t phys, pgprot_t prot)
0051 {
0052     BUG();
0053 }
0054 #endif
0055 
0056 #ifndef __late_clear_fixmap
0057 static inline void __init __late_clear_fixmap(enum fixed_addresses idx)
0058 {
0059     BUG();
0060 }
0061 #endif
0062 
0063 static void __iomem *prev_map[FIX_BTMAPS_SLOTS] __initdata;
0064 static unsigned long prev_size[FIX_BTMAPS_SLOTS] __initdata;
0065 static unsigned long slot_virt[FIX_BTMAPS_SLOTS] __initdata;
0066 
0067 void __init early_ioremap_setup(void)
0068 {
0069     int i;
0070 
0071     for (i = 0; i < FIX_BTMAPS_SLOTS; i++)
0072         if (WARN_ON(prev_map[i]))
0073             break;
0074 
0075     for (i = 0; i < FIX_BTMAPS_SLOTS; i++)
0076         slot_virt[i] = __fix_to_virt(FIX_BTMAP_BEGIN - NR_FIX_BTMAPS*i);
0077 }
0078 
0079 static int __init check_early_ioremap_leak(void)
0080 {
0081     int count = 0;
0082     int i;
0083 
0084     for (i = 0; i < FIX_BTMAPS_SLOTS; i++)
0085         if (prev_map[i])
0086             count++;
0087 
0088     if (WARN(count, KERN_WARNING
0089          "Debug warning: early ioremap leak of %d areas detected.\n"
0090          "please boot with early_ioremap_debug and report the dmesg.\n",
0091          count))
0092         return 1;
0093     return 0;
0094 }
0095 late_initcall(check_early_ioremap_leak);
0096 
0097 static void __init __iomem *
0098 __early_ioremap(resource_size_t phys_addr, unsigned long size, pgprot_t prot)
0099 {
0100     unsigned long offset;
0101     resource_size_t last_addr;
0102     unsigned int nrpages;
0103     enum fixed_addresses idx;
0104     int i, slot;
0105 
0106     WARN_ON(system_state != SYSTEM_BOOTING);
0107 
0108     slot = -1;
0109     for (i = 0; i < FIX_BTMAPS_SLOTS; i++) {
0110         if (!prev_map[i]) {
0111             slot = i;
0112             break;
0113         }
0114     }
0115 
0116     if (WARN(slot < 0, "%s(%08llx, %08lx) not found slot\n",
0117          __func__, (u64)phys_addr, size))
0118         return NULL;
0119 
0120     /* Don't allow wraparound or zero size */
0121     last_addr = phys_addr + size - 1;
0122     if (WARN_ON(!size || last_addr < phys_addr))
0123         return NULL;
0124 
0125     prev_size[slot] = size;
0126     /*
0127      * Mappings have to be page-aligned
0128      */
0129     offset = offset_in_page(phys_addr);
0130     phys_addr &= PAGE_MASK;
0131     size = PAGE_ALIGN(last_addr + 1) - phys_addr;
0132 
0133     /*
0134      * Mappings have to fit in the FIX_BTMAP area.
0135      */
0136     nrpages = size >> PAGE_SHIFT;
0137     if (WARN_ON(nrpages > NR_FIX_BTMAPS))
0138         return NULL;
0139 
0140     /*
0141      * Ok, go for it..
0142      */
0143     idx = FIX_BTMAP_BEGIN - NR_FIX_BTMAPS*slot;
0144     while (nrpages > 0) {
0145         if (after_paging_init)
0146             __late_set_fixmap(idx, phys_addr, prot);
0147         else
0148             __early_set_fixmap(idx, phys_addr, prot);
0149         phys_addr += PAGE_SIZE;
0150         --idx;
0151         --nrpages;
0152     }
0153     WARN(early_ioremap_debug, "%s(%08llx, %08lx) [%d] => %08lx + %08lx\n",
0154          __func__, (u64)phys_addr, size, slot, offset, slot_virt[slot]);
0155 
0156     prev_map[slot] = (void __iomem *)(offset + slot_virt[slot]);
0157     return prev_map[slot];
0158 }
0159 
0160 void __init early_iounmap(void __iomem *addr, unsigned long size)
0161 {
0162     unsigned long virt_addr;
0163     unsigned long offset;
0164     unsigned int nrpages;
0165     enum fixed_addresses idx;
0166     int i, slot;
0167 
0168     slot = -1;
0169     for (i = 0; i < FIX_BTMAPS_SLOTS; i++) {
0170         if (prev_map[i] == addr) {
0171             slot = i;
0172             break;
0173         }
0174     }
0175 
0176     if (WARN(slot < 0, "early_iounmap(%p, %08lx) not found slot\n",
0177          addr, size))
0178         return;
0179 
0180     if (WARN(prev_size[slot] != size,
0181          "early_iounmap(%p, %08lx) [%d] size not consistent %08lx\n",
0182          addr, size, slot, prev_size[slot]))
0183         return;
0184 
0185     WARN(early_ioremap_debug, "early_iounmap(%p, %08lx) [%d]\n",
0186          addr, size, slot);
0187 
0188     virt_addr = (unsigned long)addr;
0189     if (WARN_ON(virt_addr < fix_to_virt(FIX_BTMAP_BEGIN)))
0190         return;
0191 
0192     offset = offset_in_page(virt_addr);
0193     nrpages = PAGE_ALIGN(offset + size) >> PAGE_SHIFT;
0194 
0195     idx = FIX_BTMAP_BEGIN - NR_FIX_BTMAPS*slot;
0196     while (nrpages > 0) {
0197         if (after_paging_init)
0198             __late_clear_fixmap(idx);
0199         else
0200             __early_set_fixmap(idx, 0, FIXMAP_PAGE_CLEAR);
0201         --idx;
0202         --nrpages;
0203     }
0204     prev_map[slot] = NULL;
0205 }
0206 
0207 /* Remap an IO device */
0208 void __init __iomem *
0209 early_ioremap(resource_size_t phys_addr, unsigned long size)
0210 {
0211     return __early_ioremap(phys_addr, size, FIXMAP_PAGE_IO);
0212 }
0213 
0214 /* Remap memory */
0215 void __init *
0216 early_memremap(resource_size_t phys_addr, unsigned long size)
0217 {
0218     return (__force void *)__early_ioremap(phys_addr, size,
0219                            FIXMAP_PAGE_NORMAL);
0220 }
0221 #ifdef FIXMAP_PAGE_RO
0222 void __init *
0223 early_memremap_ro(resource_size_t phys_addr, unsigned long size)
0224 {
0225     return (__force void *)__early_ioremap(phys_addr, size, FIXMAP_PAGE_RO);
0226 }
0227 #endif
0228 
0229 #define MAX_MAP_CHUNK   (NR_FIX_BTMAPS << PAGE_SHIFT)
0230 
0231 void __init copy_from_early_mem(void *dest, phys_addr_t src, unsigned long size)
0232 {
0233     unsigned long slop, clen;
0234     char *p;
0235 
0236     while (size) {
0237         slop = offset_in_page(src);
0238         clen = size;
0239         if (clen > MAX_MAP_CHUNK - slop)
0240             clen = MAX_MAP_CHUNK - slop;
0241         p = early_memremap(src & PAGE_MASK, clen + slop);
0242         memcpy(dest, p + slop, clen);
0243         early_memunmap(p, clen + slop);
0244         dest += clen;
0245         src += clen;
0246         size -= clen;
0247     }
0248 }
0249 
0250 #else /* CONFIG_MMU */
0251 
0252 void __init __iomem *
0253 early_ioremap(resource_size_t phys_addr, unsigned long size)
0254 {
0255     return (__force void __iomem *)phys_addr;
0256 }
0257 
0258 /* Remap memory */
0259 void __init *
0260 early_memremap(resource_size_t phys_addr, unsigned long size)
0261 {
0262     return (void *)phys_addr;
0263 }
0264 void __init *
0265 early_memremap_ro(resource_size_t phys_addr, unsigned long size)
0266 {
0267     return (void *)phys_addr;
0268 }
0269 
0270 void __init early_iounmap(void __iomem *addr, unsigned long size)
0271 {
0272 }
0273 
0274 #endif /* CONFIG_MMU */
0275 
0276 
0277 void __init early_memunmap(void *addr, unsigned long size)
0278 {
0279     early_iounmap((__force void __iomem *)addr, size);
0280 }