linux/arch/unicore32/mm/init.c
<<
>>
Prefs
   1/*
   2 *  linux/arch/unicore32/mm/init.c
   3 *
   4 *  Copyright (C) 2010 GUAN Xue-tao
   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/kernel.h>
  11#include <linux/errno.h>
  12#include <linux/swap.h>
  13#include <linux/init.h>
  14#include <linux/memblock.h>
  15#include <linux/mman.h>
  16#include <linux/nodemask.h>
  17#include <linux/initrd.h>
  18#include <linux/highmem.h>
  19#include <linux/gfp.h>
  20#include <linux/sort.h>
  21#include <linux/dma-mapping.h>
  22#include <linux/export.h>
  23
  24#include <asm/sections.h>
  25#include <asm/setup.h>
  26#include <asm/sizes.h>
  27#include <asm/tlb.h>
  28#include <asm/memblock.h>
  29#include <mach/map.h>
  30
  31#include "mm.h"
  32
  33static unsigned long phys_initrd_start __initdata = 0x01000000;
  34static unsigned long phys_initrd_size __initdata = SZ_8M;
  35
  36static int __init early_initrd(char *p)
  37{
  38        unsigned long start, size;
  39        char *endp;
  40
  41        start = memparse(p, &endp);
  42        if (*endp == ',') {
  43                size = memparse(endp + 1, NULL);
  44
  45                phys_initrd_start = start;
  46                phys_initrd_size = size;
  47        }
  48        return 0;
  49}
  50early_param("initrd", early_initrd);
  51
  52/*
  53 * This keeps memory configuration data used by a couple memory
  54 * initialization functions, as well as show_mem() for the skipping
  55 * of holes in the memory map.  It is populated by uc32_add_memory().
  56 */
  57struct meminfo meminfo;
  58
  59static void __init find_limits(unsigned long *min, unsigned long *max_low,
  60        unsigned long *max_high)
  61{
  62        struct meminfo *mi = &meminfo;
  63        int i;
  64
  65        *min = -1UL;
  66        *max_low = *max_high = 0;
  67
  68        for_each_bank(i, mi) {
  69                struct membank *bank = &mi->bank[i];
  70                unsigned long start, end;
  71
  72                start = bank_pfn_start(bank);
  73                end = bank_pfn_end(bank);
  74
  75                if (*min > start)
  76                        *min = start;
  77                if (*max_high < end)
  78                        *max_high = end;
  79                if (bank->highmem)
  80                        continue;
  81                if (*max_low < end)
  82                        *max_low = end;
  83        }
  84}
  85
  86static void __init uc32_bootmem_init(unsigned long start_pfn,
  87        unsigned long end_pfn)
  88{
  89        struct memblock_region *reg;
  90        unsigned int boot_pages;
  91        phys_addr_t bitmap;
  92        pg_data_t *pgdat;
  93
  94        /*
  95         * Allocate the bootmem bitmap page.  This must be in a region
  96         * of memory which has already been mapped.
  97         */
  98        boot_pages = bootmem_bootmap_pages(end_pfn - start_pfn);
  99        bitmap = memblock_alloc_base(boot_pages << PAGE_SHIFT, L1_CACHE_BYTES,
 100                                __pfn_to_phys(end_pfn));
 101
 102        /*
 103         * Initialise the bootmem allocator, handing the
 104         * memory banks over to bootmem.
 105         */
 106        node_set_online(0);
 107        pgdat = NODE_DATA(0);
 108        init_bootmem_node(pgdat, __phys_to_pfn(bitmap), start_pfn, end_pfn);
 109
 110        /* Free the lowmem regions from memblock into bootmem. */
 111        for_each_memblock(memory, reg) {
 112                unsigned long start = memblock_region_memory_base_pfn(reg);
 113                unsigned long end = memblock_region_memory_end_pfn(reg);
 114
 115                if (end >= end_pfn)
 116                        end = end_pfn;
 117                if (start >= end)
 118                        break;
 119
 120                free_bootmem(__pfn_to_phys(start), (end - start) << PAGE_SHIFT);
 121        }
 122
 123        /* Reserve the lowmem memblock reserved regions in bootmem. */
 124        for_each_memblock(reserved, reg) {
 125                unsigned long start = memblock_region_reserved_base_pfn(reg);
 126                unsigned long end = memblock_region_reserved_end_pfn(reg);
 127
 128                if (end >= end_pfn)
 129                        end = end_pfn;
 130                if (start >= end)
 131                        break;
 132
 133                reserve_bootmem(__pfn_to_phys(start),
 134                        (end - start) << PAGE_SHIFT, BOOTMEM_DEFAULT);
 135        }
 136}
 137
 138static void __init uc32_bootmem_free(unsigned long min, unsigned long max_low,
 139        unsigned long max_high)
 140{
 141        unsigned long zone_size[MAX_NR_ZONES], zhole_size[MAX_NR_ZONES];
 142        struct memblock_region *reg;
 143
 144        /*
 145         * initialise the zones.
 146         */
 147        memset(zone_size, 0, sizeof(zone_size));
 148
 149        /*
 150         * The memory size has already been determined.  If we need
 151         * to do anything fancy with the allocation of this memory
 152         * to the zones, now is the time to do it.
 153         */
 154        zone_size[0] = max_low - min;
 155
 156        /*
 157         * Calculate the size of the holes.
 158         *  holes = node_size - sum(bank_sizes)
 159         */
 160        memcpy(zhole_size, zone_size, sizeof(zhole_size));
 161        for_each_memblock(memory, reg) {
 162                unsigned long start = memblock_region_memory_base_pfn(reg);
 163                unsigned long end = memblock_region_memory_end_pfn(reg);
 164
 165                if (start < max_low) {
 166                        unsigned long low_end = min(end, max_low);
 167                        zhole_size[0] -= low_end - start;
 168                }
 169        }
 170
 171        /*
 172         * Adjust the sizes according to any special requirements for
 173         * this machine type.
 174         */
 175        arch_adjust_zones(zone_size, zhole_size);
 176
 177        free_area_init_node(0, zone_size, min, zhole_size);
 178}
 179
 180int pfn_valid(unsigned long pfn)
 181{
 182        return memblock_is_memory(pfn << PAGE_SHIFT);
 183}
 184EXPORT_SYMBOL(pfn_valid);
 185
 186static void uc32_memory_present(void)
 187{
 188}
 189
 190static int __init meminfo_cmp(const void *_a, const void *_b)
 191{
 192        const struct membank *a = _a, *b = _b;
 193        long cmp = bank_pfn_start(a) - bank_pfn_start(b);
 194        return cmp < 0 ? -1 : cmp > 0 ? 1 : 0;
 195}
 196
 197void __init uc32_memblock_init(struct meminfo *mi)
 198{
 199        int i;
 200
 201        sort(&meminfo.bank, meminfo.nr_banks, sizeof(meminfo.bank[0]),
 202                meminfo_cmp, NULL);
 203
 204        for (i = 0; i < mi->nr_banks; i++)
 205                memblock_add(mi->bank[i].start, mi->bank[i].size);
 206
 207        /* Register the kernel text, kernel data and initrd with memblock. */
 208        memblock_reserve(__pa(_text), _end - _text);
 209
 210#ifdef CONFIG_BLK_DEV_INITRD
 211        if (phys_initrd_size) {
 212                memblock_reserve(phys_initrd_start, phys_initrd_size);
 213
 214                /* Now convert initrd to virtual addresses */
 215                initrd_start = __phys_to_virt(phys_initrd_start);
 216                initrd_end = initrd_start + phys_initrd_size;
 217        }
 218#endif
 219
 220        uc32_mm_memblock_reserve();
 221
 222        memblock_allow_resize();
 223        memblock_dump_all();
 224}
 225
 226void __init bootmem_init(void)
 227{
 228        unsigned long min, max_low, max_high;
 229
 230        max_low = max_high = 0;
 231
 232        find_limits(&min, &max_low, &max_high);
 233
 234        uc32_bootmem_init(min, max_low);
 235
 236#ifdef CONFIG_SWIOTLB
 237        swiotlb_init(1);
 238#endif
 239        /*
 240         * Sparsemem tries to allocate bootmem in memory_present(),
 241         * so must be done after the fixed reservations
 242         */
 243        uc32_memory_present();
 244
 245        /*
 246         * sparse_init() needs the bootmem allocator up and running.
 247         */
 248        sparse_init();
 249
 250        /*
 251         * Now free the memory - free_area_init_node needs
 252         * the sparse mem_map arrays initialized by sparse_init()
 253         * for memmap_init_zone(), otherwise all PFNs are invalid.
 254         */
 255        uc32_bootmem_free(min, max_low, max_high);
 256
 257        high_memory = __va((max_low << PAGE_SHIFT) - 1) + 1;
 258
 259        /*
 260         * This doesn't seem to be used by the Linux memory manager any
 261         * more, but is used by ll_rw_block.  If we can get rid of it, we
 262         * also get rid of some of the stuff above as well.
 263         *
 264         * Note: max_low_pfn and max_pfn reflect the number of _pages_ in
 265         * the system, not the maximum PFN.
 266         */
 267        max_low_pfn = max_low - PHYS_PFN_OFFSET;
 268        max_pfn = max_high - PHYS_PFN_OFFSET;
 269}
 270
 271static inline void
 272free_memmap(unsigned long start_pfn, unsigned long end_pfn)
 273{
 274        struct page *start_pg, *end_pg;
 275        unsigned long pg, pgend;
 276
 277        /*
 278         * Convert start_pfn/end_pfn to a struct page pointer.
 279         */
 280        start_pg = pfn_to_page(start_pfn - 1) + 1;
 281        end_pg = pfn_to_page(end_pfn);
 282
 283        /*
 284         * Convert to physical addresses, and
 285         * round start upwards and end downwards.
 286         */
 287        pg = PAGE_ALIGN(__pa(start_pg));
 288        pgend = __pa(end_pg) & PAGE_MASK;
 289
 290        /*
 291         * If there are free pages between these,
 292         * free the section of the memmap array.
 293         */
 294        if (pg < pgend)
 295                memblock_free(pg, pgend - pg);
 296}
 297
 298/*
 299 * The mem_map array can get very big.  Free the unused area of the memory map.
 300 */
 301static void __init free_unused_memmap(struct meminfo *mi)
 302{
 303        unsigned long bank_start, prev_bank_end = 0;
 304        unsigned int i;
 305
 306        /*
 307         * This relies on each bank being in address order.
 308         * The banks are sorted previously in bootmem_init().
 309         */
 310        for_each_bank(i, mi) {
 311                struct membank *bank = &mi->bank[i];
 312
 313                bank_start = bank_pfn_start(bank);
 314
 315                /*
 316                 * If we had a previous bank, and there is a space
 317                 * between the current bank and the previous, free it.
 318                 */
 319                if (prev_bank_end && prev_bank_end < bank_start)
 320                        free_memmap(prev_bank_end, bank_start);
 321
 322                /*
 323                 * Align up here since the VM subsystem insists that the
 324                 * memmap entries are valid from the bank end aligned to
 325                 * MAX_ORDER_NR_PAGES.
 326                 */
 327                prev_bank_end = ALIGN(bank_pfn_end(bank), MAX_ORDER_NR_PAGES);
 328        }
 329}
 330
 331/*
 332 * mem_init() marks the free areas in the mem_map and tells us how much
 333 * memory is free.  This is done after various parts of the system have
 334 * claimed their memory after the kernel image.
 335 */
 336void __init mem_init(void)
 337{
 338        max_mapnr   = pfn_to_page(max_pfn + PHYS_PFN_OFFSET) - mem_map;
 339
 340        free_unused_memmap(&meminfo);
 341
 342        /* this will put all unused low memory onto the freelists */
 343        memblock_free_all();
 344
 345        mem_init_print_info(NULL);
 346        printk(KERN_NOTICE "Virtual kernel memory layout:\n"
 347                "    vector  : 0x%08lx - 0x%08lx   (%4ld kB)\n"
 348                "    vmalloc : 0x%08lx - 0x%08lx   (%4ld MB)\n"
 349                "    lowmem  : 0x%08lx - 0x%08lx   (%4ld MB)\n"
 350                "    modules : 0x%08lx - 0x%08lx   (%4ld MB)\n"
 351                "      .init : 0x%p" " - 0x%p" "   (%4d kB)\n"
 352                "      .text : 0x%p" " - 0x%p" "   (%4d kB)\n"
 353                "      .data : 0x%p" " - 0x%p" "   (%4d kB)\n",
 354
 355                VECTORS_BASE, VECTORS_BASE + PAGE_SIZE,
 356                DIV_ROUND_UP(PAGE_SIZE, SZ_1K),
 357                VMALLOC_START, VMALLOC_END,
 358                DIV_ROUND_UP((VMALLOC_END - VMALLOC_START), SZ_1M),
 359                PAGE_OFFSET, (unsigned long)high_memory,
 360                DIV_ROUND_UP(((unsigned long)high_memory - PAGE_OFFSET), SZ_1M),
 361                MODULES_VADDR, MODULES_END,
 362                DIV_ROUND_UP((MODULES_END - MODULES_VADDR), SZ_1M),
 363
 364                __init_begin, __init_end,
 365                DIV_ROUND_UP((__init_end - __init_begin), SZ_1K),
 366                _stext, _etext,
 367                DIV_ROUND_UP((_etext - _stext), SZ_1K),
 368                _sdata, _edata,
 369                DIV_ROUND_UP((_edata - _sdata), SZ_1K));
 370
 371        BUILD_BUG_ON(TASK_SIZE                          > MODULES_VADDR);
 372        BUG_ON(TASK_SIZE                                > MODULES_VADDR);
 373
 374        if (PAGE_SIZE >= 16384 && get_num_physpages() <= 128) {
 375                /*
 376                 * On a machine this small we won't get
 377                 * anywhere without overcommit, so turn
 378                 * it on by default.
 379                 */
 380                sysctl_overcommit_memory = OVERCOMMIT_ALWAYS;
 381        }
 382}
 383
 384void free_initmem(void)
 385{
 386        free_initmem_default(-1);
 387}
 388
 389#ifdef CONFIG_BLK_DEV_INITRD
 390
 391static int keep_initrd;
 392
 393void free_initrd_mem(unsigned long start, unsigned long end)
 394{
 395        if (!keep_initrd)
 396                free_reserved_area((void *)start, (void *)end, -1, "initrd");
 397}
 398
 399static int __init keepinitrd_setup(char *__unused)
 400{
 401        keep_initrd = 1;
 402        return 1;
 403}
 404
 405__setup("keepinitrd", keepinitrd_setup);
 406#endif
 407