linux/arch/arm/mm/mmu.c
<<
>>
Prefs
   1/*
   2 *  linux/arch/arm/mm/mmu.c
   3 *
   4 *  Copyright (C) 1995-2005 Russell King
   5 *
   6 * This program is free software; you can redistribute it and/or modify
   7 * it under the terms of the GNU General Public License version 2 as
   8 * published by the Free Software Foundation.
   9 */
  10#include <linux/module.h>
  11#include <linux/kernel.h>
  12#include <linux/errno.h>
  13#include <linux/init.h>
  14#include <linux/mman.h>
  15#include <linux/nodemask.h>
  16#include <linux/memblock.h>
  17#include <linux/fs.h>
  18#include <linux/vmalloc.h>
  19#include <linux/sizes.h>
  20
  21#include <asm/cp15.h>
  22#include <asm/cputype.h>
  23#include <asm/sections.h>
  24#include <asm/cachetype.h>
  25#include <asm/fixmap.h>
  26#include <asm/sections.h>
  27#include <asm/setup.h>
  28#include <asm/smp_plat.h>
  29#include <asm/tlb.h>
  30#include <asm/highmem.h>
  31#include <asm/system_info.h>
  32#include <asm/traps.h>
  33#include <asm/procinfo.h>
  34#include <asm/memory.h>
  35
  36#include <asm/mach/arch.h>
  37#include <asm/mach/map.h>
  38#include <asm/mach/pci.h>
  39#include <asm/fixmap.h>
  40
  41#include "fault.h"
  42#include "mm.h"
  43#include "tcm.h"
  44
  45/*
  46 * empty_zero_page is a special page that is used for
  47 * zero-initialized data and COW.
  48 */
  49struct page *empty_zero_page;
  50EXPORT_SYMBOL(empty_zero_page);
  51
  52/*
  53 * The pmd table for the upper-most set of pages.
  54 */
  55pmd_t *top_pmd;
  56
  57pmdval_t user_pmd_table = _PAGE_USER_TABLE;
  58
  59#define CPOLICY_UNCACHED        0
  60#define CPOLICY_BUFFERED        1
  61#define CPOLICY_WRITETHROUGH    2
  62#define CPOLICY_WRITEBACK       3
  63#define CPOLICY_WRITEALLOC      4
  64
  65static unsigned int cachepolicy __initdata = CPOLICY_WRITEBACK;
  66static unsigned int ecc_mask __initdata = 0;
  67pgprot_t pgprot_user;
  68pgprot_t pgprot_kernel;
  69
  70EXPORT_SYMBOL(pgprot_user);
  71EXPORT_SYMBOL(pgprot_kernel);
  72
  73struct cachepolicy {
  74        const char      policy[16];
  75        unsigned int    cr_mask;
  76        pmdval_t        pmd;
  77        pteval_t        pte;
  78};
  79
  80unsigned long kimage_voffset __ro_after_init;
  81
  82static struct cachepolicy cache_policies[] __initdata = {
  83        {
  84                .policy         = "uncached",
  85                .cr_mask        = CR_W|CR_C,
  86                .pmd            = PMD_SECT_UNCACHED,
  87                .pte            = L_PTE_MT_UNCACHED,
  88        }, {
  89                .policy         = "buffered",
  90                .cr_mask        = CR_C,
  91                .pmd            = PMD_SECT_BUFFERED,
  92                .pte            = L_PTE_MT_BUFFERABLE,
  93        }, {
  94                .policy         = "writethrough",
  95                .cr_mask        = 0,
  96                .pmd            = PMD_SECT_WT,
  97                .pte            = L_PTE_MT_WRITETHROUGH,
  98        }, {
  99                .policy         = "writeback",
 100                .cr_mask        = 0,
 101                .pmd            = PMD_SECT_WB,
 102                .pte            = L_PTE_MT_WRITEBACK,
 103        }, {
 104                .policy         = "writealloc",
 105                .cr_mask        = 0,
 106                .pmd            = PMD_SECT_WBWA,
 107                .pte            = L_PTE_MT_WRITEALLOC,
 108        }
 109};
 110
 111#ifdef CONFIG_CPU_CP15
 112static unsigned long initial_pmd_value __initdata = 0;
 113
 114/*
 115 * Initialise the cache_policy variable with the initial state specified
 116 * via the "pmd" value.  This is used to ensure that on ARMv6 and later,
 117 * the C code sets the page tables up with the same policy as the head
 118 * assembly code, which avoids an illegal state where the TLBs can get
 119 * confused.  See comments in early_cachepolicy() for more information.
 120 */
 121void __init init_default_cache_policy(unsigned long pmd)
 122{
 123        int i;
 124
 125        initial_pmd_value = pmd;
 126
 127        pmd &= PMD_SECT_CACHE_MASK;
 128
 129        for (i = 0; i < ARRAY_SIZE(cache_policies); i++)
 130                if (cache_policies[i].pmd == pmd) {
 131                        cachepolicy = i;
 132                        break;
 133                }
 134
 135        if (i == ARRAY_SIZE(cache_policies))
 136                pr_err("ERROR: could not find cache policy\n");
 137}
 138
 139/*
 140 * These are useful for identifying cache coherency problems by allowing
 141 * the cache or the cache and writebuffer to be turned off.  (Note: the
 142 * write buffer should not be on and the cache off).
 143 */
 144static int __init early_cachepolicy(char *p)
 145{
 146        int i, selected = -1;
 147
 148        for (i = 0; i < ARRAY_SIZE(cache_policies); i++) {
 149                int len = strlen(cache_policies[i].policy);
 150
 151                if (memcmp(p, cache_policies[i].policy, len) == 0) {
 152                        selected = i;
 153                        break;
 154                }
 155        }
 156
 157        if (selected == -1)
 158                pr_err("ERROR: unknown or unsupported cache policy\n");
 159
 160        /*
 161         * This restriction is partly to do with the way we boot; it is
 162         * unpredictable to have memory mapped using two different sets of
 163         * memory attributes (shared, type, and cache attribs).  We can not
 164         * change these attributes once the initial assembly has setup the
 165         * page tables.
 166         */
 167        if (cpu_architecture() >= CPU_ARCH_ARMv6 && selected != cachepolicy) {
 168                pr_warn("Only cachepolicy=%s supported on ARMv6 and later\n",
 169                        cache_policies[cachepolicy].policy);
 170                return 0;
 171        }
 172
 173        if (selected != cachepolicy) {
 174                unsigned long cr = __clear_cr(cache_policies[selected].cr_mask);
 175                cachepolicy = selected;
 176                flush_cache_all();
 177                set_cr(cr);
 178        }
 179        return 0;
 180}
 181early_param("cachepolicy", early_cachepolicy);
 182
 183static int __init early_nocache(char *__unused)
 184{
 185        char *p = "buffered";
 186        pr_warn("nocache is deprecated; use cachepolicy=%s\n", p);
 187        early_cachepolicy(p);
 188        return 0;
 189}
 190early_param("nocache", early_nocache);
 191
 192static int __init early_nowrite(char *__unused)
 193{
 194        char *p = "uncached";
 195        pr_warn("nowb is deprecated; use cachepolicy=%s\n", p);
 196        early_cachepolicy(p);
 197        return 0;
 198}
 199early_param("nowb", early_nowrite);
 200
 201#ifndef CONFIG_ARM_LPAE
 202static int __init early_ecc(char *p)
 203{
 204        if (memcmp(p, "on", 2) == 0)
 205                ecc_mask = PMD_PROTECTION;
 206        else if (memcmp(p, "off", 3) == 0)
 207                ecc_mask = 0;
 208        return 0;
 209}
 210early_param("ecc", early_ecc);
 211#endif
 212
 213#else /* ifdef CONFIG_CPU_CP15 */
 214
 215static int __init early_cachepolicy(char *p)
 216{
 217        pr_warn("cachepolicy kernel parameter not supported without cp15\n");
 218}
 219early_param("cachepolicy", early_cachepolicy);
 220
 221static int __init noalign_setup(char *__unused)
 222{
 223        pr_warn("noalign kernel parameter not supported without cp15\n");
 224}
 225__setup("noalign", noalign_setup);
 226
 227#endif /* ifdef CONFIG_CPU_CP15 / else */
 228
 229#define PROT_PTE_DEVICE         L_PTE_PRESENT|L_PTE_YOUNG|L_PTE_DIRTY|L_PTE_XN
 230#define PROT_PTE_S2_DEVICE      PROT_PTE_DEVICE
 231#define PROT_SECT_DEVICE        PMD_TYPE_SECT|PMD_SECT_AP_WRITE
 232
 233static struct mem_type mem_types[] __ro_after_init = {
 234        [MT_DEVICE] = {           /* Strongly ordered / ARMv6 shared device */
 235                .prot_pte       = PROT_PTE_DEVICE | L_PTE_MT_DEV_SHARED |
 236                                  L_PTE_SHARED,
 237                .prot_l1        = PMD_TYPE_TABLE,
 238                .prot_sect      = PROT_SECT_DEVICE | PMD_SECT_S,
 239                .domain         = DOMAIN_IO,
 240        },
 241        [MT_DEVICE_NONSHARED] = { /* ARMv6 non-shared device */
 242                .prot_pte       = PROT_PTE_DEVICE | L_PTE_MT_DEV_NONSHARED,
 243                .prot_l1        = PMD_TYPE_TABLE,
 244                .prot_sect      = PROT_SECT_DEVICE,
 245                .domain         = DOMAIN_IO,
 246        },
 247        [MT_DEVICE_CACHED] = {    /* ioremap_cached */
 248                .prot_pte       = PROT_PTE_DEVICE | L_PTE_MT_DEV_CACHED,
 249                .prot_l1        = PMD_TYPE_TABLE,
 250                .prot_sect      = PROT_SECT_DEVICE | PMD_SECT_WB,
 251                .domain         = DOMAIN_IO,
 252        },
 253        [MT_DEVICE_WC] = {      /* ioremap_wc */
 254                .prot_pte       = PROT_PTE_DEVICE | L_PTE_MT_DEV_WC,
 255                .prot_l1        = PMD_TYPE_TABLE,
 256                .prot_sect      = PROT_SECT_DEVICE,
 257                .domain         = DOMAIN_IO,
 258        },
 259        [MT_UNCACHED] = {
 260                .prot_pte       = PROT_PTE_DEVICE,
 261                .prot_l1        = PMD_TYPE_TABLE,
 262                .prot_sect      = PMD_TYPE_SECT | PMD_SECT_XN,
 263                .domain         = DOMAIN_IO,
 264        },
 265        [MT_CACHECLEAN] = {
 266                .prot_sect = PMD_TYPE_SECT | PMD_SECT_XN,
 267                .domain    = DOMAIN_KERNEL,
 268        },
 269#ifndef CONFIG_ARM_LPAE
 270        [MT_MINICLEAN] = {
 271                .prot_sect = PMD_TYPE_SECT | PMD_SECT_XN | PMD_SECT_MINICACHE,
 272                .domain    = DOMAIN_KERNEL,
 273        },
 274#endif
 275        [MT_LOW_VECTORS] = {
 276                .prot_pte  = L_PTE_PRESENT | L_PTE_YOUNG | L_PTE_DIRTY |
 277                                L_PTE_RDONLY,
 278                .prot_l1   = PMD_TYPE_TABLE,
 279                .domain    = DOMAIN_VECTORS,
 280        },
 281        [MT_HIGH_VECTORS] = {
 282                .prot_pte  = L_PTE_PRESENT | L_PTE_YOUNG | L_PTE_DIRTY |
 283                                L_PTE_USER | L_PTE_RDONLY,
 284                .prot_l1   = PMD_TYPE_TABLE,
 285                .domain    = DOMAIN_VECTORS,
 286        },
 287        [MT_MEMORY_RWX] = {
 288                .prot_pte  = L_PTE_PRESENT | L_PTE_YOUNG | L_PTE_DIRTY,
 289                .prot_l1   = PMD_TYPE_TABLE,
 290                .prot_sect = PMD_TYPE_SECT | PMD_SECT_AP_WRITE,
 291                .domain    = DOMAIN_KERNEL,
 292        },
 293        [MT_MEMORY_RW] = {
 294                .prot_pte  = L_PTE_PRESENT | L_PTE_YOUNG | L_PTE_DIRTY |
 295                             L_PTE_XN,
 296                .prot_l1   = PMD_TYPE_TABLE,
 297                .prot_sect = PMD_TYPE_SECT | PMD_SECT_AP_WRITE,
 298                .domain    = DOMAIN_KERNEL,
 299        },
 300        [MT_ROM] = {
 301                .prot_sect = PMD_TYPE_SECT,
 302                .domain    = DOMAIN_KERNEL,
 303        },
 304        [MT_MEMORY_RWX_NONCACHED] = {
 305                .prot_pte  = L_PTE_PRESENT | L_PTE_YOUNG | L_PTE_DIRTY |
 306                                L_PTE_MT_BUFFERABLE,
 307                .prot_l1   = PMD_TYPE_TABLE,
 308                .prot_sect = PMD_TYPE_SECT | PMD_SECT_AP_WRITE,
 309                .domain    = DOMAIN_KERNEL,
 310        },
 311        [MT_MEMORY_RW_DTCM] = {
 312                .prot_pte  = L_PTE_PRESENT | L_PTE_YOUNG | L_PTE_DIRTY |
 313                                L_PTE_XN,
 314                .prot_l1   = PMD_TYPE_TABLE,
 315                .prot_sect = PMD_TYPE_SECT | PMD_SECT_XN,
 316                .domain    = DOMAIN_KERNEL,
 317        },
 318        [MT_MEMORY_RWX_ITCM] = {
 319                .prot_pte  = L_PTE_PRESENT | L_PTE_YOUNG | L_PTE_DIRTY,
 320                .prot_l1   = PMD_TYPE_TABLE,
 321                .domain    = DOMAIN_KERNEL,
 322        },
 323        [MT_MEMORY_RW_SO] = {
 324                .prot_pte  = L_PTE_PRESENT | L_PTE_YOUNG | L_PTE_DIRTY |
 325                                L_PTE_MT_UNCACHED | L_PTE_XN,
 326                .prot_l1   = PMD_TYPE_TABLE,
 327                .prot_sect = PMD_TYPE_SECT | PMD_SECT_AP_WRITE | PMD_SECT_S |
 328                                PMD_SECT_UNCACHED | PMD_SECT_XN,
 329                .domain    = DOMAIN_KERNEL,
 330        },
 331        [MT_MEMORY_DMA_READY] = {
 332                .prot_pte  = L_PTE_PRESENT | L_PTE_YOUNG | L_PTE_DIRTY |
 333                                L_PTE_XN,
 334                .prot_l1   = PMD_TYPE_TABLE,
 335                .domain    = DOMAIN_KERNEL,
 336        },
 337};
 338
 339const struct mem_type *get_mem_type(unsigned int type)
 340{
 341        return type < ARRAY_SIZE(mem_types) ? &mem_types[type] : NULL;
 342}
 343EXPORT_SYMBOL(get_mem_type);
 344
 345static pte_t *(*pte_offset_fixmap)(pmd_t *dir, unsigned long addr);
 346
 347static pte_t bm_pte[PTRS_PER_PTE + PTE_HWTABLE_PTRS]
 348        __aligned(PTE_HWTABLE_OFF + PTE_HWTABLE_SIZE) __initdata;
 349
 350static pte_t * __init pte_offset_early_fixmap(pmd_t *dir, unsigned long addr)
 351{
 352        return &bm_pte[pte_index(addr)];
 353}
 354
 355static pte_t *pte_offset_late_fixmap(pmd_t *dir, unsigned long addr)
 356{
 357        return pte_offset_kernel(dir, addr);
 358}
 359
 360static inline pmd_t * __init fixmap_pmd(unsigned long addr)
 361{
 362        pgd_t *pgd = pgd_offset_k(addr);
 363        pud_t *pud = pud_offset(pgd, addr);
 364        pmd_t *pmd = pmd_offset(pud, addr);
 365
 366        return pmd;
 367}
 368
 369void __init early_fixmap_init(void)
 370{
 371        pmd_t *pmd;
 372
 373        /*
 374         * The early fixmap range spans multiple pmds, for which
 375         * we are not prepared:
 376         */
 377        BUILD_BUG_ON((__fix_to_virt(__end_of_early_ioremap_region) >> PMD_SHIFT)
 378                     != FIXADDR_TOP >> PMD_SHIFT);
 379
 380        pmd = fixmap_pmd(FIXADDR_TOP);
 381        pmd_populate_kernel(&init_mm, pmd, bm_pte);
 382
 383        pte_offset_fixmap = pte_offset_early_fixmap;
 384}
 385
 386/*
 387 * To avoid TLB flush broadcasts, this uses local_flush_tlb_kernel_range().
 388 * As a result, this can only be called with preemption disabled, as under
 389 * stop_machine().
 390 */
 391void __set_fixmap(enum fixed_addresses idx, phys_addr_t phys, pgprot_t prot)
 392{
 393        unsigned long vaddr = __fix_to_virt(idx);
 394        pte_t *pte = pte_offset_fixmap(pmd_off_k(vaddr), vaddr);
 395
 396        /* Make sure fixmap region does not exceed available allocation. */
 397        BUILD_BUG_ON(FIXADDR_START + (__end_of_fixed_addresses * PAGE_SIZE) >
 398                     FIXADDR_END);
 399        BUG_ON(idx >= __end_of_fixed_addresses);
 400
 401        /* we only support device mappings until pgprot_kernel has been set */
 402        if (WARN_ON(pgprot_val(prot) != pgprot_val(FIXMAP_PAGE_IO) &&
 403                    pgprot_val(pgprot_kernel) == 0))
 404                return;
 405
 406        if (pgprot_val(prot))
 407                set_pte_at(NULL, vaddr, pte,
 408                        pfn_pte(phys >> PAGE_SHIFT, prot));
 409        else
 410                pte_clear(NULL, vaddr, pte);
 411        local_flush_tlb_kernel_range(vaddr, vaddr + PAGE_SIZE);
 412}
 413
 414/*
 415 * Adjust the PMD section entries according to the CPU in use.
 416 */
 417static void __init build_mem_type_table(void)
 418{
 419        struct cachepolicy *cp;
 420        unsigned int cr = get_cr();
 421        pteval_t user_pgprot, kern_pgprot, vecs_pgprot;
 422        int cpu_arch = cpu_architecture();
 423        int i;
 424
 425        if (cpu_arch < CPU_ARCH_ARMv6) {
 426#if defined(CONFIG_CPU_DCACHE_DISABLE)
 427                if (cachepolicy > CPOLICY_BUFFERED)
 428                        cachepolicy = CPOLICY_BUFFERED;
 429#elif defined(CONFIG_CPU_DCACHE_WRITETHROUGH)
 430                if (cachepolicy > CPOLICY_WRITETHROUGH)
 431                        cachepolicy = CPOLICY_WRITETHROUGH;
 432#endif
 433        }
 434        if (cpu_arch < CPU_ARCH_ARMv5) {
 435                if (cachepolicy >= CPOLICY_WRITEALLOC)
 436                        cachepolicy = CPOLICY_WRITEBACK;
 437                ecc_mask = 0;
 438        }
 439
 440        if (is_smp()) {
 441                if (cachepolicy != CPOLICY_WRITEALLOC) {
 442                        pr_warn("Forcing write-allocate cache policy for SMP\n");
 443                        cachepolicy = CPOLICY_WRITEALLOC;
 444                }
 445                if (!(initial_pmd_value & PMD_SECT_S)) {
 446                        pr_warn("Forcing shared mappings for SMP\n");
 447                        initial_pmd_value |= PMD_SECT_S;
 448                }
 449        }
 450
 451        /*
 452         * Strip out features not present on earlier architectures.
 453         * Pre-ARMv5 CPUs don't have TEX bits.  Pre-ARMv6 CPUs or those
 454         * without extended page tables don't have the 'Shared' bit.
 455         */
 456        if (cpu_arch < CPU_ARCH_ARMv5)
 457                for (i = 0; i < ARRAY_SIZE(mem_types); i++)
 458                        mem_types[i].prot_sect &= ~PMD_SECT_TEX(7);
 459        if ((cpu_arch < CPU_ARCH_ARMv6 || !(cr & CR_XP)) && !cpu_is_xsc3())
 460                for (i = 0; i < ARRAY_SIZE(mem_types); i++)
 461                        mem_types[i].prot_sect &= ~PMD_SECT_S;
 462
 463        /*
 464         * ARMv5 and lower, bit 4 must be set for page tables (was: cache
 465         * "update-able on write" bit on ARM610).  However, Xscale and
 466         * Xscale3 require this bit to be cleared.
 467         */
 468        if (cpu_is_xscale_family()) {
 469                for (i = 0; i < ARRAY_SIZE(mem_types); i++) {
 470                        mem_types[i].prot_sect &= ~PMD_BIT4;
 471                        mem_types[i].prot_l1 &= ~PMD_BIT4;
 472                }
 473        } else if (cpu_arch < CPU_ARCH_ARMv6) {
 474                for (i = 0; i < ARRAY_SIZE(mem_types); i++) {
 475                        if (mem_types[i].prot_l1)
 476                                mem_types[i].prot_l1 |= PMD_BIT4;
 477                        if (mem_types[i].prot_sect)
 478                                mem_types[i].prot_sect |= PMD_BIT4;
 479                }
 480        }
 481
 482        /*
 483         * Mark the device areas according to the CPU/architecture.
 484         */
 485        if (cpu_is_xsc3() || (cpu_arch >= CPU_ARCH_ARMv6 && (cr & CR_XP))) {
 486                if (!cpu_is_xsc3()) {
 487                        /*
 488                         * Mark device regions on ARMv6+ as execute-never
 489                         * to prevent speculative instruction fetches.
 490                         */
 491                        mem_types[MT_DEVICE].prot_sect |= PMD_SECT_XN;
 492                        mem_types[MT_DEVICE_NONSHARED].prot_sect |= PMD_SECT_XN;
 493                        mem_types[MT_DEVICE_CACHED].prot_sect |= PMD_SECT_XN;
 494                        mem_types[MT_DEVICE_WC].prot_sect |= PMD_SECT_XN;
 495
 496                        /* Also setup NX memory mapping */
 497                        mem_types[MT_MEMORY_RW].prot_sect |= PMD_SECT_XN;
 498                }
 499                if (cpu_arch >= CPU_ARCH_ARMv7 && (cr & CR_TRE)) {
 500                        /*
 501                         * For ARMv7 with TEX remapping,
 502                         * - shared device is SXCB=1100
 503                         * - nonshared device is SXCB=0100
 504                         * - write combine device mem is SXCB=0001
 505                         * (Uncached Normal memory)
 506                         */
 507                        mem_types[MT_DEVICE].prot_sect |= PMD_SECT_TEX(1);
 508                        mem_types[MT_DEVICE_NONSHARED].prot_sect |= PMD_SECT_TEX(1);
 509                        mem_types[MT_DEVICE_WC].prot_sect |= PMD_SECT_BUFFERABLE;
 510                } else if (cpu_is_xsc3()) {
 511                        /*
 512                         * For Xscale3,
 513                         * - shared device is TEXCB=00101
 514                         * - nonshared device is TEXCB=01000
 515                         * - write combine device mem is TEXCB=00100
 516                         * (Inner/Outer Uncacheable in xsc3 parlance)
 517                         */
 518                        mem_types[MT_DEVICE].prot_sect |= PMD_SECT_TEX(1) | PMD_SECT_BUFFERED;
 519                        mem_types[MT_DEVICE_NONSHARED].prot_sect |= PMD_SECT_TEX(2);
 520                        mem_types[MT_DEVICE_WC].prot_sect |= PMD_SECT_TEX(1);
 521                } else {
 522                        /*
 523                         * For ARMv6 and ARMv7 without TEX remapping,
 524                         * - shared device is TEXCB=00001
 525                         * - nonshared device is TEXCB=01000
 526                         * - write combine device mem is TEXCB=00100
 527                         * (Uncached Normal in ARMv6 parlance).
 528                         */
 529                        mem_types[MT_DEVICE].prot_sect |= PMD_SECT_BUFFERED;
 530                        mem_types[MT_DEVICE_NONSHARED].prot_sect |= PMD_SECT_TEX(2);
 531                        mem_types[MT_DEVICE_WC].prot_sect |= PMD_SECT_TEX(1);
 532                }
 533        } else {
 534                /*
 535                 * On others, write combining is "Uncached/Buffered"
 536                 */
 537                mem_types[MT_DEVICE_WC].prot_sect |= PMD_SECT_BUFFERABLE;
 538        }
 539
 540        /*
 541         * Now deal with the memory-type mappings
 542         */
 543        cp = &cache_policies[cachepolicy];
 544        vecs_pgprot = kern_pgprot = user_pgprot = cp->pte;
 545
 546#ifndef CONFIG_ARM_LPAE
 547        /*
 548         * We don't use domains on ARMv6 (since this causes problems with
 549         * v6/v7 kernels), so we must use a separate memory type for user
 550         * r/o, kernel r/w to map the vectors page.
 551         */
 552        if (cpu_arch == CPU_ARCH_ARMv6)
 553                vecs_pgprot |= L_PTE_MT_VECTORS;
 554
 555        /*
 556         * Check is it with support for the PXN bit
 557         * in the Short-descriptor translation table format descriptors.
 558         */
 559        if (cpu_arch == CPU_ARCH_ARMv7 &&
 560                (read_cpuid_ext(CPUID_EXT_MMFR0) & 0xF) >= 4) {
 561                user_pmd_table |= PMD_PXNTABLE;
 562        }
 563#endif
 564
 565        /*
 566         * ARMv6 and above have extended page tables.
 567         */
 568        if (cpu_arch >= CPU_ARCH_ARMv6 && (cr & CR_XP)) {
 569#ifndef CONFIG_ARM_LPAE
 570                /*
 571                 * Mark cache clean areas and XIP ROM read only
 572                 * from SVC mode and no access from userspace.
 573                 */
 574                mem_types[MT_ROM].prot_sect |= PMD_SECT_APX|PMD_SECT_AP_WRITE;
 575                mem_types[MT_MINICLEAN].prot_sect |= PMD_SECT_APX|PMD_SECT_AP_WRITE;
 576                mem_types[MT_CACHECLEAN].prot_sect |= PMD_SECT_APX|PMD_SECT_AP_WRITE;
 577#endif
 578
 579                /*
 580                 * If the initial page tables were created with the S bit
 581                 * set, then we need to do the same here for the same
 582                 * reasons given in early_cachepolicy().
 583                 */
 584                if (initial_pmd_value & PMD_SECT_S) {
 585                        user_pgprot |= L_PTE_SHARED;
 586                        kern_pgprot |= L_PTE_SHARED;
 587                        vecs_pgprot |= L_PTE_SHARED;
 588                        mem_types[MT_DEVICE_WC].prot_sect |= PMD_SECT_S;
 589                        mem_types[MT_DEVICE_WC].prot_pte |= L_PTE_SHARED;
 590                        mem_types[MT_DEVICE_CACHED].prot_sect |= PMD_SECT_S;
 591                        mem_types[MT_DEVICE_CACHED].prot_pte |= L_PTE_SHARED;
 592                        mem_types[MT_MEMORY_RWX].prot_sect |= PMD_SECT_S;
 593                        mem_types[MT_MEMORY_RWX].prot_pte |= L_PTE_SHARED;
 594                        mem_types[MT_MEMORY_RW].prot_sect |= PMD_SECT_S;
 595                        mem_types[MT_MEMORY_RW].prot_pte |= L_PTE_SHARED;
 596                        mem_types[MT_MEMORY_DMA_READY].prot_pte |= L_PTE_SHARED;
 597                        mem_types[MT_MEMORY_RWX_NONCACHED].prot_sect |= PMD_SECT_S;
 598                        mem_types[MT_MEMORY_RWX_NONCACHED].prot_pte |= L_PTE_SHARED;
 599                }
 600        }
 601
 602        /*
 603         * Non-cacheable Normal - intended for memory areas that must
 604         * not cause dirty cache line writebacks when used
 605         */
 606        if (cpu_arch >= CPU_ARCH_ARMv6) {
 607                if (cpu_arch >= CPU_ARCH_ARMv7 && (cr & CR_TRE)) {
 608                        /* Non-cacheable Normal is XCB = 001 */
 609                        mem_types[MT_MEMORY_RWX_NONCACHED].prot_sect |=
 610                                PMD_SECT_BUFFERED;
 611                } else {
 612                        /* For both ARMv6 and non-TEX-remapping ARMv7 */
 613                        mem_types[MT_MEMORY_RWX_NONCACHED].prot_sect |=
 614                                PMD_SECT_TEX(1);
 615                }
 616        } else {
 617                mem_types[MT_MEMORY_RWX_NONCACHED].prot_sect |= PMD_SECT_BUFFERABLE;
 618        }
 619
 620#ifdef CONFIG_ARM_LPAE
 621        /*
 622         * Do not generate access flag faults for the kernel mappings.
 623         */
 624        for (i = 0; i < ARRAY_SIZE(mem_types); i++) {
 625                mem_types[i].prot_pte |= PTE_EXT_AF;
 626                if (mem_types[i].prot_sect)
 627                        mem_types[i].prot_sect |= PMD_SECT_AF;
 628        }
 629        kern_pgprot |= PTE_EXT_AF;
 630        vecs_pgprot |= PTE_EXT_AF;
 631
 632        /*
 633         * Set PXN for user mappings
 634         */
 635        user_pgprot |= PTE_EXT_PXN;
 636#endif
 637
 638        for (i = 0; i < 16; i++) {
 639                pteval_t v = pgprot_val(protection_map[i]);
 640                protection_map[i] = __pgprot(v | user_pgprot);
 641        }
 642
 643        mem_types[MT_LOW_VECTORS].prot_pte |= vecs_pgprot;
 644        mem_types[MT_HIGH_VECTORS].prot_pte |= vecs_pgprot;
 645
 646        pgprot_user   = __pgprot(L_PTE_PRESENT | L_PTE_YOUNG | user_pgprot);
 647        pgprot_kernel = __pgprot(L_PTE_PRESENT | L_PTE_YOUNG |
 648                                 L_PTE_DIRTY | kern_pgprot);
 649
 650        mem_types[MT_LOW_VECTORS].prot_l1 |= ecc_mask;
 651        mem_types[MT_HIGH_VECTORS].prot_l1 |= ecc_mask;
 652        mem_types[MT_MEMORY_RWX].prot_sect |= ecc_mask | cp->pmd;
 653        mem_types[MT_MEMORY_RWX].prot_pte |= kern_pgprot;
 654        mem_types[MT_MEMORY_RW].prot_sect |= ecc_mask | cp->pmd;
 655        mem_types[MT_MEMORY_RW].prot_pte |= kern_pgprot;
 656        mem_types[MT_MEMORY_DMA_READY].prot_pte |= kern_pgprot;
 657        mem_types[MT_MEMORY_RWX_NONCACHED].prot_sect |= ecc_mask;
 658        mem_types[MT_ROM].prot_sect |= cp->pmd;
 659
 660        switch (cp->pmd) {
 661        case PMD_SECT_WT:
 662                mem_types[MT_CACHECLEAN].prot_sect |= PMD_SECT_WT;
 663                break;
 664        case PMD_SECT_WB:
 665        case PMD_SECT_WBWA:
 666                mem_types[MT_CACHECLEAN].prot_sect |= PMD_SECT_WB;
 667                break;
 668        }
 669        pr_info("Memory policy: %sData cache %s\n",
 670                ecc_mask ? "ECC enabled, " : "", cp->policy);
 671
 672        for (i = 0; i < ARRAY_SIZE(mem_types); i++) {
 673                struct mem_type *t = &mem_types[i];
 674                if (t->prot_l1)
 675                        t->prot_l1 |= PMD_DOMAIN(t->domain);
 676                if (t->prot_sect)
 677                        t->prot_sect |= PMD_DOMAIN(t->domain);
 678        }
 679}
 680
 681#ifdef CONFIG_ARM_DMA_MEM_BUFFERABLE
 682pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn,
 683                              unsigned long size, pgprot_t vma_prot)
 684{
 685        if (!pfn_valid(pfn))
 686                return pgprot_noncached(vma_prot);
 687        else if (file->f_flags & O_SYNC)
 688                return pgprot_writecombine(vma_prot);
 689        return vma_prot;
 690}
 691EXPORT_SYMBOL(phys_mem_access_prot);
 692#endif
 693
 694#define vectors_base()  (vectors_high() ? 0xffff0000 : 0)
 695
 696static void __init *early_alloc_aligned(unsigned long sz, unsigned long align)
 697{
 698        void *ptr = __va(memblock_phys_alloc(sz, align));
 699        memset(ptr, 0, sz);
 700        return ptr;
 701}
 702
 703static void __init *early_alloc(unsigned long sz)
 704{
 705        return early_alloc_aligned(sz, sz);
 706}
 707
 708static void *__init late_alloc(unsigned long sz)
 709{
 710        void *ptr = (void *)__get_free_pages(PGALLOC_GFP, get_order(sz));
 711
 712        if (!ptr || !pgtable_page_ctor(virt_to_page(ptr)))
 713                BUG();
 714        return ptr;
 715}
 716
 717static pte_t * __init arm_pte_alloc(pmd_t *pmd, unsigned long addr,
 718                                unsigned long prot,
 719                                void *(*alloc)(unsigned long sz))
 720{
 721        if (pmd_none(*pmd)) {
 722                pte_t *pte = alloc(PTE_HWTABLE_OFF + PTE_HWTABLE_SIZE);
 723                __pmd_populate(pmd, __pa(pte), prot);
 724        }
 725        BUG_ON(pmd_bad(*pmd));
 726        return pte_offset_kernel(pmd, addr);
 727}
 728
 729static pte_t * __init early_pte_alloc(pmd_t *pmd, unsigned long addr,
 730                                      unsigned long prot)
 731{
 732        return arm_pte_alloc(pmd, addr, prot, early_alloc);
 733}
 734
 735static void __init alloc_init_pte(pmd_t *pmd, unsigned long addr,
 736                                  unsigned long end, unsigned long pfn,
 737                                  const struct mem_type *type,
 738                                  void *(*alloc)(unsigned long sz),
 739                                  bool ng)
 740{
 741        pte_t *pte = arm_pte_alloc(pmd, addr, type->prot_l1, alloc);
 742        do {
 743                set_pte_ext(pte, pfn_pte(pfn, __pgprot(type->prot_pte)),
 744                            ng ? PTE_EXT_NG : 0);
 745                pfn++;
 746        } while (pte++, addr += PAGE_SIZE, addr != end);
 747}
 748
 749static void __init __map_init_section(pmd_t *pmd, unsigned long addr,
 750                        unsigned long end, phys_addr_t phys,
 751                        const struct mem_type *type, bool ng)
 752{
 753        pmd_t *p = pmd;
 754
 755#ifndef CONFIG_ARM_LPAE
 756        /*
 757         * In classic MMU format, puds and pmds are folded in to
 758         * the pgds. pmd_offset gives the PGD entry. PGDs refer to a
 759         * group of L1 entries making up one logical pointer to
 760         * an L2 table (2MB), where as PMDs refer to the individual
 761         * L1 entries (1MB). Hence increment to get the correct
 762         * offset for odd 1MB sections.
 763         * (See arch/arm/include/asm/pgtable-2level.h)
 764         */
 765        if (addr & SECTION_SIZE)
 766                pmd++;
 767#endif
 768        do {
 769                *pmd = __pmd(phys | type->prot_sect | (ng ? PMD_SECT_nG : 0));
 770                phys += SECTION_SIZE;
 771        } while (pmd++, addr += SECTION_SIZE, addr != end);
 772
 773        flush_pmd_entry(p);
 774}
 775
 776static void __init alloc_init_pmd(pud_t *pud, unsigned long addr,
 777                                      unsigned long end, phys_addr_t phys,
 778                                      const struct mem_type *type,
 779                                      void *(*alloc)(unsigned long sz), bool ng)
 780{
 781        pmd_t *pmd = pmd_offset(pud, addr);
 782        unsigned long next;
 783
 784        do {
 785                /*
 786                 * With LPAE, we must loop over to map
 787                 * all the pmds for the given range.
 788                 */
 789                next = pmd_addr_end(addr, end);
 790
 791                /*
 792                 * Try a section mapping - addr, next and phys must all be
 793                 * aligned to a section boundary.
 794                 */
 795                if (type->prot_sect &&
 796                                ((addr | next | phys) & ~SECTION_MASK) == 0) {
 797                        __map_init_section(pmd, addr, next, phys, type, ng);
 798                } else {
 799                        alloc_init_pte(pmd, addr, next,
 800                                       __phys_to_pfn(phys), type, alloc, ng);
 801                }
 802
 803                phys += next - addr;
 804
 805        } while (pmd++, addr = next, addr != end);
 806}
 807
 808static void __init alloc_init_pud(pgd_t *pgd, unsigned long addr,
 809                                  unsigned long end, phys_addr_t phys,
 810                                  const struct mem_type *type,
 811                                  void *(*alloc)(unsigned long sz), bool ng)
 812{
 813        pud_t *pud = pud_offset(pgd, addr);
 814        unsigned long next;
 815
 816        do {
 817                next = pud_addr_end(addr, end);
 818                alloc_init_pmd(pud, addr, next, phys, type, alloc, ng);
 819                phys += next - addr;
 820        } while (pud++, addr = next, addr != end);
 821}
 822
 823#ifndef CONFIG_ARM_LPAE
 824static void __init create_36bit_mapping(struct mm_struct *mm,
 825                                        struct map_desc *md,
 826                                        const struct mem_type *type,
 827                                        bool ng)
 828{
 829        unsigned long addr, length, end;
 830        phys_addr_t phys;
 831        pgd_t *pgd;
 832
 833        addr = md->virtual;
 834        phys = __pfn_to_phys(md->pfn);
 835        length = PAGE_ALIGN(md->length);
 836
 837        if (!(cpu_architecture() >= CPU_ARCH_ARMv6 || cpu_is_xsc3())) {
 838                pr_err("MM: CPU does not support supersection mapping for 0x%08llx at 0x%08lx\n",
 839                       (long long)__pfn_to_phys((u64)md->pfn), addr);
 840                return;
 841        }
 842
 843        /* N.B. ARMv6 supersections are only defined to work with domain 0.
 844         *      Since domain assignments can in fact be arbitrary, the
 845         *      'domain == 0' check below is required to insure that ARMv6
 846         *      supersections are only allocated for domain 0 regardless
 847         *      of the actual domain assignments in use.
 848         */
 849        if (type->domain) {
 850                pr_err("MM: invalid domain in supersection mapping for 0x%08llx at 0x%08lx\n",
 851                       (long long)__pfn_to_phys((u64)md->pfn), addr);
 852                return;
 853        }
 854
 855        if ((addr | length | __pfn_to_phys(md->pfn)) & ~SUPERSECTION_MASK) {
 856                pr_err("MM: cannot create mapping for 0x%08llx at 0x%08lx invalid alignment\n",
 857                       (long long)__pfn_to_phys((u64)md->pfn), addr);
 858                return;
 859        }
 860
 861        /*
 862         * Shift bits [35:32] of address into bits [23:20] of PMD
 863         * (See ARMv6 spec).
 864         */
 865        phys |= (((md->pfn >> (32 - PAGE_SHIFT)) & 0xF) << 20);
 866
 867        pgd = pgd_offset(mm, addr);
 868        end = addr + length;
 869        do {
 870                pud_t *pud = pud_offset(pgd, addr);
 871                pmd_t *pmd = pmd_offset(pud, addr);
 872                int i;
 873
 874                for (i = 0; i < 16; i++)
 875                        *pmd++ = __pmd(phys | type->prot_sect | PMD_SECT_SUPER |
 876                                       (ng ? PMD_SECT_nG : 0));
 877
 878                addr += SUPERSECTION_SIZE;
 879                phys += SUPERSECTION_SIZE;
 880                pgd += SUPERSECTION_SIZE >> PGDIR_SHIFT;
 881        } while (addr != end);
 882}
 883#endif  /* !CONFIG_ARM_LPAE */
 884
 885static void __init __create_mapping(struct mm_struct *mm, struct map_desc *md,
 886                                    void *(*alloc)(unsigned long sz),
 887                                    bool ng)
 888{
 889        unsigned long addr, length, end;
 890        phys_addr_t phys;
 891        const struct mem_type *type;
 892        pgd_t *pgd;
 893
 894        type = &mem_types[md->type];
 895
 896#ifndef CONFIG_ARM_LPAE
 897        /*
 898         * Catch 36-bit addresses
 899         */
 900        if (md->pfn >= 0x100000) {
 901                create_36bit_mapping(mm, md, type, ng);
 902                return;
 903        }
 904#endif
 905
 906        addr = md->virtual & PAGE_MASK;
 907        phys = __pfn_to_phys(md->pfn);
 908        length = PAGE_ALIGN(md->length + (md->virtual & ~PAGE_MASK));
 909
 910        if (type->prot_l1 == 0 && ((addr | phys | length) & ~SECTION_MASK)) {
 911                pr_warn("BUG: map for 0x%08llx at 0x%08lx can not be mapped using pages, ignoring.\n",
 912                        (long long)__pfn_to_phys(md->pfn), addr);
 913                return;
 914        }
 915
 916        pgd = pgd_offset(mm, addr);
 917        end = addr + length;
 918        do {
 919                unsigned long next = pgd_addr_end(addr, end);
 920
 921                alloc_init_pud(pgd, addr, next, phys, type, alloc, ng);
 922
 923                phys += next - addr;
 924                addr = next;
 925        } while (pgd++, addr != end);
 926}
 927
 928/*
 929 * Create the page directory entries and any necessary
 930 * page tables for the mapping specified by `md'.  We
 931 * are able to cope here with varying sizes and address
 932 * offsets, and we take full advantage of sections and
 933 * supersections.
 934 */
 935static void __init create_mapping(struct map_desc *md)
 936{
 937        if (md->virtual != vectors_base() && md->virtual < TASK_SIZE) {
 938                pr_warn("BUG: not creating mapping for 0x%08llx at 0x%08lx in user region\n",
 939                        (long long)__pfn_to_phys((u64)md->pfn), md->virtual);
 940                return;
 941        }
 942
 943        if ((md->type == MT_DEVICE || md->type == MT_ROM) &&
 944            md->virtual >= PAGE_OFFSET && md->virtual < FIXADDR_START &&
 945            (md->virtual < VMALLOC_START || md->virtual >= VMALLOC_END)) {
 946                pr_warn("BUG: mapping for 0x%08llx at 0x%08lx out of vmalloc space\n",
 947                        (long long)__pfn_to_phys((u64)md->pfn), md->virtual);
 948        }
 949
 950        __create_mapping(&init_mm, md, early_alloc, false);
 951}
 952
 953void __init create_mapping_late(struct mm_struct *mm, struct map_desc *md,
 954                                bool ng)
 955{
 956#ifdef CONFIG_ARM_LPAE
 957        pud_t *pud = pud_alloc(mm, pgd_offset(mm, md->virtual), md->virtual);
 958        if (WARN_ON(!pud))
 959                return;
 960        pmd_alloc(mm, pud, 0);
 961#endif
 962        __create_mapping(mm, md, late_alloc, ng);
 963}
 964
 965/*
 966 * Create the architecture specific mappings
 967 */
 968void __init iotable_init(struct map_desc *io_desc, int nr)
 969{
 970        struct map_desc *md;
 971        struct vm_struct *vm;
 972        struct static_vm *svm;
 973
 974        if (!nr)
 975                return;
 976
 977        svm = early_alloc_aligned(sizeof(*svm) * nr, __alignof__(*svm));
 978
 979        for (md = io_desc; nr; md++, nr--) {
 980                create_mapping(md);
 981
 982                vm = &svm->vm;
 983                vm->addr = (void *)(md->virtual & PAGE_MASK);
 984                vm->size = PAGE_ALIGN(md->length + (md->virtual & ~PAGE_MASK));
 985                vm->phys_addr = __pfn_to_phys(md->pfn);
 986                vm->flags = VM_IOREMAP | VM_ARM_STATIC_MAPPING;
 987                vm->flags |= VM_ARM_MTYPE(md->type);
 988                vm->caller = iotable_init;
 989                add_static_vm_early(svm++);
 990        }
 991}
 992
 993void __init vm_reserve_area_early(unsigned long addr, unsigned long size,
 994                                  void *caller)
 995{
 996        struct vm_struct *vm;
 997        struct static_vm *svm;
 998
 999        svm = early_alloc_aligned(sizeof(*svm), __alignof__(*svm));
1000
1001        vm = &svm->vm;
1002        vm->addr = (void *)addr;
1003        vm->size = size;
1004        vm->flags = VM_IOREMAP | VM_ARM_EMPTY_MAPPING;
1005        vm->caller = caller;
1006        add_static_vm_early(svm);
1007}
1008
1009#ifndef CONFIG_ARM_LPAE
1010
1011/*
1012 * The Linux PMD is made of two consecutive section entries covering 2MB
1013 * (see definition in include/asm/pgtable-2level.h).  However a call to
1014 * create_mapping() may optimize static mappings by using individual
1015 * 1MB section mappings.  This leaves the actual PMD potentially half
1016 * initialized if the top or bottom section entry isn't used, leaving it
1017 * open to problems if a subsequent ioremap() or vmalloc() tries to use
1018 * the virtual space left free by that unused section entry.
1019 *
1020 * Let's avoid the issue by inserting dummy vm entries covering the unused
1021 * PMD halves once the static mappings are in place.
1022 */
1023
1024static void __init pmd_empty_section_gap(unsigned long addr)
1025{
1026        vm_reserve_area_early(addr, SECTION_SIZE, pmd_empty_section_gap);
1027}
1028
1029static void __init fill_pmd_gaps(void)
1030{
1031        struct static_vm *svm;
1032        struct vm_struct *vm;
1033        unsigned long addr, next = 0;
1034        pmd_t *pmd;
1035
1036        list_for_each_entry(svm, &static_vmlist, list) {
1037                vm = &svm->vm;
1038                addr = (unsigned long)vm->addr;
1039                if (addr < next)
1040                        continue;
1041
1042                /*
1043                 * Check if this vm starts on an odd section boundary.
1044                 * If so and the first section entry for this PMD is free
1045                 * then we block the corresponding virtual address.
1046                 */
1047                if ((addr & ~PMD_MASK) == SECTION_SIZE) {
1048                        pmd = pmd_off_k(addr);
1049                        if (pmd_none(*pmd))
1050                                pmd_empty_section_gap(addr & PMD_MASK);
1051                }
1052
1053                /*
1054                 * Then check if this vm ends on an odd section boundary.
1055                 * If so and the second section entry for this PMD is empty
1056                 * then we block the corresponding virtual address.
1057                 */
1058                addr += vm->size;
1059                if ((addr & ~PMD_MASK) == SECTION_SIZE) {
1060                        pmd = pmd_off_k(addr) + 1;
1061                        if (pmd_none(*pmd))
1062                                pmd_empty_section_gap(addr);
1063                }
1064
1065                /* no need to look at any vm entry until we hit the next PMD */
1066                next = (addr + PMD_SIZE - 1) & PMD_MASK;
1067        }
1068}
1069
1070#else
1071#define fill_pmd_gaps() do { } while (0)
1072#endif
1073
1074#if defined(CONFIG_PCI) && !defined(CONFIG_NEED_MACH_IO_H)
1075static void __init pci_reserve_io(void)
1076{
1077        struct static_vm *svm;
1078
1079        svm = find_static_vm_vaddr((void *)PCI_IO_VIRT_BASE);
1080        if (svm)
1081                return;
1082
1083        vm_reserve_area_early(PCI_IO_VIRT_BASE, SZ_2M, pci_reserve_io);
1084}
1085#else
1086#define pci_reserve_io() do { } while (0)
1087#endif
1088
1089#ifdef CONFIG_DEBUG_LL
1090void __init debug_ll_io_init(void)
1091{
1092        struct map_desc map;
1093
1094        debug_ll_addr(&map.pfn, &map.virtual);
1095        if (!map.pfn || !map.virtual)
1096                return;
1097        map.pfn = __phys_to_pfn(map.pfn);
1098        map.virtual &= PAGE_MASK;
1099        map.length = PAGE_SIZE;
1100        map.type = MT_DEVICE;
1101        iotable_init(&map, 1);
1102}
1103#endif
1104
1105static void * __initdata vmalloc_min =
1106        (void *)(VMALLOC_END - (240 << 20) - VMALLOC_OFFSET);
1107
1108/*
1109 * vmalloc=size forces the vmalloc area to be exactly 'size'
1110 * bytes. This can be used to increase (or decrease) the vmalloc
1111 * area - the default is 240m.
1112 */
1113static int __init early_vmalloc(char *arg)
1114{
1115        unsigned long vmalloc_reserve = memparse(arg, NULL);
1116
1117        if (vmalloc_reserve < SZ_16M) {
1118                vmalloc_reserve = SZ_16M;
1119                pr_warn("vmalloc area too small, limiting to %luMB\n",
1120                        vmalloc_reserve >> 20);
1121        }
1122
1123        if (vmalloc_reserve > VMALLOC_END - (PAGE_OFFSET + SZ_32M)) {
1124                vmalloc_reserve = VMALLOC_END - (PAGE_OFFSET + SZ_32M);
1125                pr_warn("vmalloc area is too big, limiting to %luMB\n",
1126                        vmalloc_reserve >> 20);
1127        }
1128
1129        vmalloc_min = (void *)(VMALLOC_END - vmalloc_reserve);
1130        return 0;
1131}
1132early_param("vmalloc", early_vmalloc);
1133
1134phys_addr_t arm_lowmem_limit __initdata = 0;
1135
1136void __init adjust_lowmem_bounds(void)
1137{
1138        phys_addr_t memblock_limit = 0;
1139        u64 vmalloc_limit;
1140        struct memblock_region *reg;
1141        phys_addr_t lowmem_limit = 0;
1142
1143        /*
1144         * Let's use our own (unoptimized) equivalent of __pa() that is
1145         * not affected by wrap-arounds when sizeof(phys_addr_t) == 4.
1146         * The result is used as the upper bound on physical memory address
1147         * and may itself be outside the valid range for which phys_addr_t
1148         * and therefore __pa() is defined.
1149         */
1150        vmalloc_limit = (u64)(uintptr_t)vmalloc_min - PAGE_OFFSET + PHYS_OFFSET;
1151
1152        for_each_memblock(memory, reg) {
1153                phys_addr_t block_start = reg->base;
1154                phys_addr_t block_end = reg->base + reg->size;
1155
1156                if (reg->base < vmalloc_limit) {
1157                        if (block_end > lowmem_limit)
1158                                /*
1159                                 * Compare as u64 to ensure vmalloc_limit does
1160                                 * not get truncated. block_end should always
1161                                 * fit in phys_addr_t so there should be no
1162                                 * issue with assignment.
1163                                 */
1164                                lowmem_limit = min_t(u64,
1165                                                         vmalloc_limit,
1166                                                         block_end);
1167
1168                        /*
1169                         * Find the first non-pmd-aligned page, and point
1170                         * memblock_limit at it. This relies on rounding the
1171                         * limit down to be pmd-aligned, which happens at the
1172                         * end of this function.
1173                         *
1174                         * With this algorithm, the start or end of almost any
1175                         * bank can be non-pmd-aligned. The only exception is
1176                         * that the start of the bank 0 must be section-
1177                         * aligned, since otherwise memory would need to be
1178                         * allocated when mapping the start of bank 0, which
1179                         * occurs before any free memory is mapped.
1180                         */
1181                        if (!memblock_limit) {
1182                                if (!IS_ALIGNED(block_start, PMD_SIZE))
1183                                        memblock_limit = block_start;
1184                                else if (!IS_ALIGNED(block_end, PMD_SIZE))
1185                                        memblock_limit = lowmem_limit;
1186                        }
1187
1188                }
1189        }
1190
1191        arm_lowmem_limit = lowmem_limit;
1192
1193        high_memory = __va(arm_lowmem_limit - 1) + 1;
1194
1195        if (!memblock_limit)
1196                memblock_limit = arm_lowmem_limit;
1197
1198        /*
1199         * Round the memblock limit down to a pmd size.  This
1200         * helps to ensure that we will allocate memory from the
1201         * last full pmd, which should be mapped.
1202         */
1203        memblock_limit = round_down(memblock_limit, PMD_SIZE);
1204
1205        if (!IS_ENABLED(CONFIG_HIGHMEM) || cache_is_vipt_aliasing()) {
1206                if (memblock_end_of_DRAM() > arm_lowmem_limit) {
1207                        phys_addr_t end = memblock_end_of_DRAM();
1208
1209                        pr_notice("Ignoring RAM at %pa-%pa\n",
1210                                  &memblock_limit, &end);
1211                        pr_notice("Consider using a HIGHMEM enabled kernel.\n");
1212
1213                        memblock_remove(memblock_limit, end - memblock_limit);
1214                }
1215        }
1216
1217        memblock_set_current_limit(memblock_limit);
1218}
1219
1220static inline void prepare_page_table(void)
1221{
1222        unsigned long addr;
1223        phys_addr_t end;
1224
1225        /*
1226         * Clear out all the mappings below the kernel image.
1227         */
1228        for (addr = 0; addr < MODULES_VADDR; addr += PMD_SIZE)
1229                pmd_clear(pmd_off_k(addr));
1230
1231#ifdef CONFIG_XIP_KERNEL
1232        /* The XIP kernel is mapped in the module area -- skip over it */
1233        addr = ((unsigned long)_exiprom + PMD_SIZE - 1) & PMD_MASK;
1234#endif
1235        for ( ; addr < PAGE_OFFSET; addr += PMD_SIZE)
1236                pmd_clear(pmd_off_k(addr));
1237
1238        /*
1239         * Find the end of the first block of lowmem.
1240         */
1241        end = memblock.memory.regions[0].base + memblock.memory.regions[0].size;
1242        if (end >= arm_lowmem_limit)
1243                end = arm_lowmem_limit;
1244
1245        /*
1246         * Clear out all the kernel space mappings, except for the first
1247         * memory bank, up to the vmalloc region.
1248         */
1249        for (addr = __phys_to_virt(end);
1250             addr < VMALLOC_START; addr += PMD_SIZE)
1251                pmd_clear(pmd_off_k(addr));
1252}
1253
1254#ifdef CONFIG_ARM_LPAE
1255/* the first page is reserved for pgd */
1256#define SWAPPER_PG_DIR_SIZE     (PAGE_SIZE + \
1257                                 PTRS_PER_PGD * PTRS_PER_PMD * sizeof(pmd_t))
1258#else
1259#define SWAPPER_PG_DIR_SIZE     (PTRS_PER_PGD * sizeof(pgd_t))
1260#endif
1261
1262/*
1263 * Reserve the special regions of memory
1264 */
1265void __init arm_mm_memblock_reserve(void)
1266{
1267        /*
1268         * Reserve the page tables.  These are already in use,
1269         * and can only be in node 0.
1270         */
1271        memblock_reserve(__pa(swapper_pg_dir), SWAPPER_PG_DIR_SIZE);
1272
1273#ifdef CONFIG_SA1111
1274        /*
1275         * Because of the SA1111 DMA bug, we want to preserve our
1276         * precious DMA-able memory...
1277         */
1278        memblock_reserve(PHYS_OFFSET, __pa(swapper_pg_dir) - PHYS_OFFSET);
1279#endif
1280}
1281
1282/*
1283 * Set up the device mappings.  Since we clear out the page tables for all
1284 * mappings above VMALLOC_START, except early fixmap, we might remove debug
1285 * device mappings.  This means earlycon can be used to debug this function
1286 * Any other function or debugging method which may touch any device _will_
1287 * crash the kernel.
1288 */
1289static void __init devicemaps_init(const struct machine_desc *mdesc)
1290{
1291        struct map_desc map;
1292        unsigned long addr;
1293        void *vectors;
1294
1295        /*
1296         * Allocate the vector page early.
1297         */
1298        vectors = early_alloc(PAGE_SIZE * 2);
1299
1300        early_trap_init(vectors);
1301
1302        /*
1303         * Clear page table except top pmd used by early fixmaps
1304         */
1305        for (addr = VMALLOC_START; addr < (FIXADDR_TOP & PMD_MASK); addr += PMD_SIZE)
1306                pmd_clear(pmd_off_k(addr));
1307
1308        /*
1309         * Map the kernel if it is XIP.
1310         * It is always first in the modulearea.
1311         */
1312#ifdef CONFIG_XIP_KERNEL
1313        map.pfn = __phys_to_pfn(CONFIG_XIP_PHYS_ADDR & SECTION_MASK);
1314        map.virtual = MODULES_VADDR;
1315        map.length = ((unsigned long)_exiprom - map.virtual + ~SECTION_MASK) & SECTION_MASK;
1316        map.type = MT_ROM;
1317        create_mapping(&map);
1318#endif
1319
1320        /*
1321         * Map the cache flushing regions.
1322         */
1323#ifdef FLUSH_BASE
1324        map.pfn = __phys_to_pfn(FLUSH_BASE_PHYS);
1325        map.virtual = FLUSH_BASE;
1326        map.length = SZ_1M;
1327        map.type = MT_CACHECLEAN;
1328        create_mapping(&map);
1329#endif
1330#ifdef FLUSH_BASE_MINICACHE
1331        map.pfn = __phys_to_pfn(FLUSH_BASE_PHYS + SZ_1M);
1332        map.virtual = FLUSH_BASE_MINICACHE;
1333        map.length = SZ_1M;
1334        map.type = MT_MINICLEAN;
1335        create_mapping(&map);
1336#endif
1337
1338        /*
1339         * Create a mapping for the machine vectors at the high-vectors
1340         * location (0xffff0000).  If we aren't using high-vectors, also
1341         * create a mapping at the low-vectors virtual address.
1342         */
1343        map.pfn = __phys_to_pfn(virt_to_phys(vectors));
1344        map.virtual = 0xffff0000;
1345        map.length = PAGE_SIZE;
1346#ifdef CONFIG_KUSER_HELPERS
1347        map.type = MT_HIGH_VECTORS;
1348#else
1349        map.type = MT_LOW_VECTORS;
1350#endif
1351        create_mapping(&map);
1352
1353        if (!vectors_high()) {
1354                map.virtual = 0;
1355                map.length = PAGE_SIZE * 2;
1356                map.type = MT_LOW_VECTORS;
1357                create_mapping(&map);
1358        }
1359
1360        /* Now create a kernel read-only mapping */
1361        map.pfn += 1;
1362        map.virtual = 0xffff0000 + PAGE_SIZE;
1363        map.length = PAGE_SIZE;
1364        map.type = MT_LOW_VECTORS;
1365        create_mapping(&map);
1366
1367        /*
1368         * Ask the machine support to map in the statically mapped devices.
1369         */
1370        if (mdesc->map_io)
1371                mdesc->map_io();
1372        else
1373                debug_ll_io_init();
1374        fill_pmd_gaps();
1375
1376        /* Reserve fixed i/o space in VMALLOC region */
1377        pci_reserve_io();
1378
1379        /*
1380         * Finally flush the caches and tlb to ensure that we're in a
1381         * consistent state wrt the writebuffer.  This also ensures that
1382         * any write-allocated cache lines in the vector page are written
1383         * back.  After this point, we can start to touch devices again.
1384         */
1385        local_flush_tlb_all();
1386        flush_cache_all();
1387
1388        /* Enable asynchronous aborts */
1389        early_abt_enable();
1390}
1391
1392static void __init kmap_init(void)
1393{
1394#ifdef CONFIG_HIGHMEM
1395        pkmap_page_table = early_pte_alloc(pmd_off_k(PKMAP_BASE),
1396                PKMAP_BASE, _PAGE_KERNEL_TABLE);
1397#endif
1398
1399        early_pte_alloc(pmd_off_k(FIXADDR_START), FIXADDR_START,
1400                        _PAGE_KERNEL_TABLE);
1401}
1402
1403static void __init map_lowmem(void)
1404{
1405        struct memblock_region *reg;
1406        phys_addr_t kernel_x_start = round_down(__pa(KERNEL_START), SECTION_SIZE);
1407        phys_addr_t kernel_x_end = round_up(__pa(__init_end), SECTION_SIZE);
1408
1409        /* Map all the lowmem memory banks. */
1410        for_each_memblock(memory, reg) {
1411                phys_addr_t start = reg->base;
1412                phys_addr_t end = start + reg->size;
1413                struct map_desc map;
1414
1415                if (memblock_is_nomap(reg))
1416                        continue;
1417
1418                if (end > arm_lowmem_limit)
1419                        end = arm_lowmem_limit;
1420                if (start >= end)
1421                        break;
1422
1423                if (end < kernel_x_start) {
1424                        map.pfn = __phys_to_pfn(start);
1425                        map.virtual = __phys_to_virt(start);
1426                        map.length = end - start;
1427                        map.type = MT_MEMORY_RWX;
1428
1429                        create_mapping(&map);
1430                } else if (start >= kernel_x_end) {
1431                        map.pfn = __phys_to_pfn(start);
1432                        map.virtual = __phys_to_virt(start);
1433                        map.length = end - start;
1434                        map.type = MT_MEMORY_RW;
1435
1436                        create_mapping(&map);
1437                } else {
1438                        /* This better cover the entire kernel */
1439                        if (start < kernel_x_start) {
1440                                map.pfn = __phys_to_pfn(start);
1441                                map.virtual = __phys_to_virt(start);
1442                                map.length = kernel_x_start - start;
1443                                map.type = MT_MEMORY_RW;
1444
1445                                create_mapping(&map);
1446                        }
1447
1448                        map.pfn = __phys_to_pfn(kernel_x_start);
1449                        map.virtual = __phys_to_virt(kernel_x_start);
1450                        map.length = kernel_x_end - kernel_x_start;
1451                        map.type = MT_MEMORY_RWX;
1452
1453                        create_mapping(&map);
1454
1455                        if (kernel_x_end < end) {
1456                                map.pfn = __phys_to_pfn(kernel_x_end);
1457                                map.virtual = __phys_to_virt(kernel_x_end);
1458                                map.length = end - kernel_x_end;
1459                                map.type = MT_MEMORY_RW;
1460
1461                                create_mapping(&map);
1462                        }
1463                }
1464        }
1465}
1466
1467#ifdef CONFIG_ARM_PV_FIXUP
1468extern unsigned long __atags_pointer;
1469typedef void pgtables_remap(long long offset, unsigned long pgd, void *bdata);
1470pgtables_remap lpae_pgtables_remap_asm;
1471
1472/*
1473 * early_paging_init() recreates boot time page table setup, allowing machines
1474 * to switch over to a high (>4G) address space on LPAE systems
1475 */
1476static void __init early_paging_init(const struct machine_desc *mdesc)
1477{
1478        pgtables_remap *lpae_pgtables_remap;
1479        unsigned long pa_pgd;
1480        unsigned int cr, ttbcr;
1481        long long offset;
1482        void *boot_data;
1483
1484        if (!mdesc->pv_fixup)
1485                return;
1486
1487        offset = mdesc->pv_fixup();
1488        if (offset == 0)
1489                return;
1490
1491        /*
1492         * Get the address of the remap function in the 1:1 identity
1493         * mapping setup by the early page table assembly code.  We
1494         * must get this prior to the pv update.  The following barrier
1495         * ensures that this is complete before we fixup any P:V offsets.
1496         */
1497        lpae_pgtables_remap = (pgtables_remap *)(unsigned long)__pa(lpae_pgtables_remap_asm);
1498        pa_pgd = __pa(swapper_pg_dir);
1499        boot_data = __va(__atags_pointer);
1500        barrier();
1501
1502        pr_info("Switching physical address space to 0x%08llx\n",
1503                (u64)PHYS_OFFSET + offset);
1504
1505        /* Re-set the phys pfn offset, and the pv offset */
1506        __pv_offset += offset;
1507        __pv_phys_pfn_offset += PFN_DOWN(offset);
1508
1509        /* Run the patch stub to update the constants */
1510        fixup_pv_table(&__pv_table_begin,
1511                (&__pv_table_end - &__pv_table_begin) << 2);
1512
1513        /*
1514         * We changing not only the virtual to physical mapping, but also
1515         * the physical addresses used to access memory.  We need to flush
1516         * all levels of cache in the system with caching disabled to
1517         * ensure that all data is written back, and nothing is prefetched
1518         * into the caches.  We also need to prevent the TLB walkers
1519         * allocating into the caches too.  Note that this is ARMv7 LPAE
1520         * specific.
1521         */
1522        cr = get_cr();
1523        set_cr(cr & ~(CR_I | CR_C));
1524        asm("mrc p15, 0, %0, c2, c0, 2" : "=r" (ttbcr));
1525        asm volatile("mcr p15, 0, %0, c2, c0, 2"
1526                : : "r" (ttbcr & ~(3 << 8 | 3 << 10)));
1527        flush_cache_all();
1528
1529        /*
1530         * Fixup the page tables - this must be in the idmap region as
1531         * we need to disable the MMU to do this safely, and hence it
1532         * needs to be assembly.  It's fairly simple, as we're using the
1533         * temporary tables setup by the initial assembly code.
1534         */
1535        lpae_pgtables_remap(offset, pa_pgd, boot_data);
1536
1537        /* Re-enable the caches and cacheable TLB walks */
1538        asm volatile("mcr p15, 0, %0, c2, c0, 2" : : "r" (ttbcr));
1539        set_cr(cr);
1540}
1541
1542#else
1543
1544static void __init early_paging_init(const struct machine_desc *mdesc)
1545{
1546        long long offset;
1547
1548        if (!mdesc->pv_fixup)
1549                return;
1550
1551        offset = mdesc->pv_fixup();
1552        if (offset == 0)
1553                return;
1554
1555        pr_crit("Physical address space modification is only to support Keystone2.\n");
1556        pr_crit("Please enable ARM_LPAE and ARM_PATCH_PHYS_VIRT support to use this\n");
1557        pr_crit("feature. Your kernel may crash now, have a good day.\n");
1558        add_taint(TAINT_CPU_OUT_OF_SPEC, LOCKDEP_STILL_OK);
1559}
1560
1561#endif
1562
1563static void __init early_fixmap_shutdown(void)
1564{
1565        int i;
1566        unsigned long va = fix_to_virt(__end_of_permanent_fixed_addresses - 1);
1567
1568        pte_offset_fixmap = pte_offset_late_fixmap;
1569        pmd_clear(fixmap_pmd(va));
1570        local_flush_tlb_kernel_page(va);
1571
1572        for (i = 0; i < __end_of_permanent_fixed_addresses; i++) {
1573                pte_t *pte;
1574                struct map_desc map;
1575
1576                map.virtual = fix_to_virt(i);
1577                pte = pte_offset_early_fixmap(pmd_off_k(map.virtual), map.virtual);
1578
1579                /* Only i/o device mappings are supported ATM */
1580                if (pte_none(*pte) ||
1581                    (pte_val(*pte) & L_PTE_MT_MASK) != L_PTE_MT_DEV_SHARED)
1582                        continue;
1583
1584                map.pfn = pte_pfn(*pte);
1585                map.type = MT_DEVICE;
1586                map.length = PAGE_SIZE;
1587
1588                create_mapping(&map);
1589        }
1590}
1591
1592/*
1593 * paging_init() sets up the page tables, initialises the zone memory
1594 * maps, and sets up the zero page, bad page and bad page tables.
1595 */
1596void __init paging_init(const struct machine_desc *mdesc)
1597{
1598        void *zero_page;
1599
1600        prepare_page_table();
1601        map_lowmem();
1602        memblock_set_current_limit(arm_lowmem_limit);
1603        dma_contiguous_remap();
1604        early_fixmap_shutdown();
1605        devicemaps_init(mdesc);
1606        kmap_init();
1607        tcm_init();
1608
1609        top_pmd = pmd_off_k(0xffff0000);
1610
1611        /* allocate the zero page. */
1612        zero_page = early_alloc(PAGE_SIZE);
1613
1614        bootmem_init();
1615
1616        empty_zero_page = virt_to_page(zero_page);
1617        __flush_dcache_page(NULL, empty_zero_page);
1618
1619        /* Compute the virt/idmap offset, mostly for the sake of KVM */
1620        kimage_voffset = (unsigned long)&kimage_voffset - virt_to_idmap(&kimage_voffset);
1621}
1622
1623void __init early_mm_init(const struct machine_desc *mdesc)
1624{
1625        build_mem_type_table();
1626        early_paging_init(mdesc);
1627}
1628