~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/arch/arm/mm/init.c

Version: ~ [ linux-5.15-rc5 ] ~ [ linux-5.14.11 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.72 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.152 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.210 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.250 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.286 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.288 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.18.140 ] ~ [ linux-3.16.85 ] ~ [ linux-3.14.79 ] ~ [ linux-3.12.74 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /*
  2  *  linux/arch/arm/mm/init.c
  3  *
  4  *  Copyright (C) 1995-2002 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/config.h>
 11 #include <linux/signal.h>
 12 #include <linux/kernel.h>
 13 #include <linux/errno.h>
 14 #include <linux/string.h>
 15 #include <linux/types.h>
 16 #include <linux/ptrace.h>
 17 #include <linux/mman.h>
 18 #include <linux/mm.h>
 19 #include <linux/swap.h>
 20 #include <linux/smp.h>
 21 #include <linux/init.h>
 22 #include <linux/bootmem.h>
 23 #include <linux/initrd.h>
 24 
 25 #include <asm/segment.h>
 26 #include <asm/mach-types.h>
 27 #include <asm/pgalloc.h>
 28 #include <asm/dma.h>
 29 #include <asm/hardware.h>
 30 #include <asm/setup.h>
 31 #include <asm/tlb.h>
 32 
 33 #include <asm/mach/arch.h>
 34 #include <asm/mach/map.h>
 35 
 36 #ifdef CONFIG_CPU_32
 37 #define TABLE_OFFSET    (PTRS_PER_PTE)
 38 #else
 39 #define TABLE_OFFSET    0
 40 #endif
 41 
 42 #define TABLE_SIZE      ((TABLE_OFFSET + PTRS_PER_PTE) * sizeof(pte_t))
 43 
 44 DEFINE_PER_CPU(struct mmu_gather, mmu_gathers);
 45 
 46 extern pgd_t swapper_pg_dir[PTRS_PER_PGD];
 47 extern char _stext, _text, _etext, _end, __init_begin, __init_end;
 48 extern unsigned long phys_initrd_start;
 49 extern unsigned long phys_initrd_size;
 50 
 51 /*
 52  * The sole use of this is to pass memory configuration
 53  * data from paging_init to mem_init.
 54  */
 55 static struct meminfo meminfo __initdata = { 0, };
 56 
 57 /*
 58  * empty_zero_page is a special page that is used for
 59  * zero-initialized data and COW.
 60  */
 61 struct page *empty_zero_page;
 62 
 63 void show_mem(void)
 64 {
 65         int free = 0, total = 0, reserved = 0;
 66         int shared = 0, cached = 0, slab = 0, node;
 67 
 68         printk("Mem-info:\n");
 69         show_free_areas();
 70         printk("Free swap:       %6dkB\n",nr_swap_pages<<(PAGE_SHIFT-10));
 71 
 72         for (node = 0; node < numnodes; node++) {
 73                 struct page *page, *end;
 74 
 75                 page = NODE_MEM_MAP(node);
 76                 end  = page + NODE_DATA(node)->node_spanned_pages;
 77 
 78                 do {
 79                         total++;
 80                         if (PageReserved(page))
 81                                 reserved++;
 82                         else if (PageSwapCache(page))
 83                                 cached++;
 84                         else if (PageSlab(page))
 85                                 slab++;
 86                         else if (!page_count(page))
 87                                 free++;
 88                         else
 89                                 shared += atomic_read(&page->count) - 1;
 90                         page++;
 91                 } while (page < end);
 92         }
 93 
 94         printk("%d pages of RAM\n", total);
 95         printk("%d free pages\n", free);
 96         printk("%d reserved pages\n", reserved);
 97         printk("%d slab pages\n", slab);
 98         printk("%d pages shared\n", shared);
 99         printk("%d pages swap cached\n", cached);
100 }
101 
102 struct node_info {
103         unsigned int start;
104         unsigned int end;
105         int bootmap_pages;
106 };
107 
108 #define O_PFN_DOWN(x)   ((x) >> PAGE_SHIFT)
109 #define V_PFN_DOWN(x)   O_PFN_DOWN(__pa(x))
110 
111 #define O_PFN_UP(x)     (PAGE_ALIGN(x) >> PAGE_SHIFT)
112 #define V_PFN_UP(x)     O_PFN_UP(__pa(x))
113 
114 #define PFN_SIZE(x)     ((x) >> PAGE_SHIFT)
115 #define PFN_RANGE(s,e)  PFN_SIZE(PAGE_ALIGN((unsigned long)(e)) - \
116                                 (((unsigned long)(s)) & PAGE_MASK))
117 
118 /*
119  * FIXME: We really want to avoid allocating the bootmap bitmap
120  * over the top of the initrd.  Hopefully, this is located towards
121  * the start of a bank, so if we allocate the bootmap bitmap at
122  * the end, we won't clash.
123  */
124 static unsigned int __init
125 find_bootmap_pfn(int node, struct meminfo *mi, unsigned int bootmap_pages)
126 {
127         unsigned int start_pfn, bank, bootmap_pfn;
128 
129         start_pfn   = V_PFN_UP(&_end);
130         bootmap_pfn = 0;
131 
132         for (bank = 0; bank < mi->nr_banks; bank ++) {
133                 unsigned int start, end;
134 
135                 if (mi->bank[bank].node != node)
136                         continue;
137 
138                 start = O_PFN_UP(mi->bank[bank].start);
139                 end   = O_PFN_DOWN(mi->bank[bank].size +
140                                    mi->bank[bank].start);
141 
142                 if (end < start_pfn)
143                         continue;
144 
145                 if (start < start_pfn)
146                         start = start_pfn;
147 
148                 if (end <= start)
149                         continue;
150 
151                 if (end - start >= bootmap_pages) {
152                         bootmap_pfn = start;
153                         break;
154                 }
155         }
156 
157         if (bootmap_pfn == 0)
158                 BUG();
159 
160         return bootmap_pfn;
161 }
162 
163 /*
164  * Scan the memory info structure and pull out:
165  *  - the end of memory
166  *  - the number of nodes
167  *  - the pfn range of each node
168  *  - the number of bootmem bitmap pages
169  */
170 static unsigned int __init
171 find_memend_and_nodes(struct meminfo *mi, struct node_info *np)
172 {
173         unsigned int i, bootmem_pages = 0, memend_pfn = 0;
174 
175         for (i = 0; i < MAX_NUMNODES; i++) {
176                 np[i].start = -1U;
177                 np[i].end = 0;
178                 np[i].bootmap_pages = 0;
179         }
180 
181         for (i = 0; i < mi->nr_banks; i++) {
182                 unsigned long start, end;
183                 int node;
184 
185                 if (mi->bank[i].size == 0) {
186                         /*
187                          * Mark this bank with an invalid node number
188                          */
189                         mi->bank[i].node = -1;
190                         continue;
191                 }
192 
193                 node = mi->bank[i].node;
194 
195                 if (node >= numnodes) {
196                         numnodes = node + 1;
197 
198                         /*
199                          * Make sure we haven't exceeded the maximum number
200                          * of nodes that we have in this configuration.  If
201                          * we have, we're in trouble.  (maybe we ought to
202                          * limit, instead of bugging?)
203                          */
204                         if (numnodes > MAX_NUMNODES)
205                                 BUG();
206                 }
207 
208                 /*
209                  * Get the start and end pfns for this bank
210                  */
211                 start = O_PFN_UP(mi->bank[i].start);
212                 end   = O_PFN_DOWN(mi->bank[i].start + mi->bank[i].size);
213 
214                 if (np[node].start > start)
215                         np[node].start = start;
216 
217                 if (np[node].end < end)
218                         np[node].end = end;
219 
220                 if (memend_pfn < end)
221                         memend_pfn = end;
222         }
223 
224         /*
225          * Calculate the number of pages we require to
226          * store the bootmem bitmaps.
227          */
228         for (i = 0; i < numnodes; i++) {
229                 if (np[i].end == 0)
230                         continue;
231 
232                 np[i].bootmap_pages = bootmem_bootmap_pages(np[i].end -
233                                                             np[i].start);
234                 bootmem_pages += np[i].bootmap_pages;
235         }
236 
237         /*
238          * This doesn't seem to be used by the Linux memory
239          * manager any more.  If we can get rid of it, we
240          * also get rid of some of the stuff above as well.
241          */
242         max_low_pfn = memend_pfn - O_PFN_DOWN(PHYS_OFFSET);
243         max_pfn = memend_pfn - O_PFN_DOWN(PHYS_OFFSET);
244         mi->end = memend_pfn << PAGE_SHIFT;
245 
246         return bootmem_pages;
247 }
248 
249 static int __init check_initrd(struct meminfo *mi)
250 {
251         int initrd_node = -2;
252 #ifdef CONFIG_BLK_DEV_INITRD
253         unsigned long end = phys_initrd_start + phys_initrd_size;
254 
255         /*
256          * Make sure that the initrd is within a valid area of
257          * memory.
258          */
259         if (phys_initrd_size) {
260                 unsigned int i;
261 
262                 initrd_node = -1;
263 
264                 for (i = 0; i < mi->nr_banks; i++) {
265                         unsigned long bank_end;
266 
267                         bank_end = mi->bank[i].start + mi->bank[i].size;
268 
269                         if (mi->bank[i].start <= phys_initrd_start &&
270                             end <= bank_end)
271                                 initrd_node = mi->bank[i].node;
272                 }
273         }
274 
275         if (initrd_node == -1) {
276                 printk(KERN_ERR "initrd (0x%08lx - 0x%08lx) extends beyond "
277                        "physical memory - disabling initrd\n",
278                        phys_initrd_start, end);
279                 phys_initrd_start = phys_initrd_size = 0;
280         }
281 #endif
282 
283         return initrd_node;
284 }
285 
286 /*
287  * Reserve the various regions of node 0
288  */
289 static __init void reserve_node_zero(unsigned int bootmap_pfn, unsigned int bootmap_pages)
290 {
291         pg_data_t *pgdat = NODE_DATA(0);
292 
293         /*
294          * Register the kernel text and data with bootmem.
295          * Note that this can only be in node 0.
296          */
297         reserve_bootmem_node(pgdat, __pa(&_stext), &_end - &_stext);
298 
299 #ifdef CONFIG_CPU_32
300         /*
301          * Reserve the page tables.  These are already in use,
302          * and can only be in node 0.
303          */
304         reserve_bootmem_node(pgdat, __pa(swapper_pg_dir),
305                              PTRS_PER_PGD * sizeof(pgd_t));
306 #endif
307         /*
308          * And don't forget to reserve the allocator bitmap,
309          * which will be freed later.
310          */
311         reserve_bootmem_node(pgdat, bootmap_pfn << PAGE_SHIFT,
312                              bootmap_pages << PAGE_SHIFT);
313 
314         /*
315          * Hmm... This should go elsewhere, but we really really
316          * need to stop things allocating the low memory; we need
317          * a better implementation of GFP_DMA which does not assume
318          * that DMA-able memory starts at zero.
319          */
320         if (machine_is_integrator())
321                 reserve_bootmem_node(pgdat, 0, __pa(swapper_pg_dir));
322         /*
323          * These should likewise go elsewhere.  They pre-reserve
324          * the screen memory region at the start of main system
325          * memory.
326          */
327         if (machine_is_archimedes() || machine_is_a5k())
328                 reserve_bootmem_node(pgdat, 0x02000000, 0x00080000);
329         if (machine_is_edb7211())
330                 reserve_bootmem_node(pgdat, 0xc0000000, 0x00020000);
331         if (machine_is_p720t())
332                 reserve_bootmem_node(pgdat, PHYS_OFFSET, 0x00014000);
333 #ifdef CONFIG_SA1111
334         /*
335          * Because of the SA1111 DMA bug, we want to preserve
336          * our precious DMA-able memory...
337          */
338         reserve_bootmem_node(pgdat, PHYS_OFFSET, __pa(swapper_pg_dir)-PHYS_OFFSET);
339 #endif
340 }
341 
342 /*
343  * Register all available RAM in this node with the bootmem allocator.
344  */
345 static inline void free_bootmem_node_bank(int node, struct meminfo *mi)
346 {
347         pg_data_t *pgdat = NODE_DATA(node);
348         int bank;
349 
350         for (bank = 0; bank < mi->nr_banks; bank++)
351                 if (mi->bank[bank].node == node)
352                         free_bootmem_node(pgdat, mi->bank[bank].start,
353                                           mi->bank[bank].size);
354 }
355 
356 /*
357  * Initialise the bootmem allocator for all nodes.  This is called
358  * early during the architecture specific initialisation.
359  */
360 void __init bootmem_init(struct meminfo *mi)
361 {
362         struct node_info node_info[MAX_NUMNODES], *np = node_info;
363         unsigned int bootmap_pages, bootmap_pfn, map_pg;
364         int node, initrd_node;
365 
366         bootmap_pages = find_memend_and_nodes(mi, np);
367         bootmap_pfn   = find_bootmap_pfn(0, mi, bootmap_pages);
368         initrd_node   = check_initrd(mi);
369 
370         map_pg = bootmap_pfn;
371 
372         /*
373          * Initialise the bootmem nodes.
374          *
375          * What we really want to do is:
376          *
377          *   unmap_all_regions_except_kernel();
378          *   for_each_node_in_reverse_order(node) {
379          *     map_node(node);
380          *     allocate_bootmem_map(node);
381          *     init_bootmem_node(node);
382          *     free_bootmem_node(node);
383          *   }
384          *
385          * but this is a 2.5-type change.  For now, we just set
386          * the nodes up in reverse order.
387          *
388          * (we could also do with rolling bootmem_init and paging_init
389          * into one generic "memory_init" type function).
390          */
391         np += numnodes - 1;
392         for (node = numnodes - 1; node >= 0; node--, np--) {
393                 /*
394                  * If there are no pages in this node, ignore it.
395                  * Note that node 0 must always have some pages.
396                  */
397                 if (np->end == 0) {
398                         if (node == 0)
399                                 BUG();
400                         continue;
401                 }
402 
403                 /*
404                  * Initialise the bootmem allocator.
405                  */
406                 init_bootmem_node(NODE_DATA(node), map_pg, np->start, np->end);
407                 free_bootmem_node_bank(node, mi);
408                 map_pg += np->bootmap_pages;
409 
410                 /*
411                  * If this is node 0, we need to reserve some areas ASAP -
412                  * we may use bootmem on node 0 to setup the other nodes.
413                  */
414                 if (node == 0)
415                         reserve_node_zero(bootmap_pfn, bootmap_pages);
416         }
417 
418 
419 #ifdef CONFIG_BLK_DEV_INITRD
420         if (phys_initrd_size && initrd_node >= 0) {
421                 reserve_bootmem_node(NODE_DATA(initrd_node), phys_initrd_start,
422                                      phys_initrd_size);
423                 initrd_start = __phys_to_virt(phys_initrd_start);
424                 initrd_end = initrd_start + phys_initrd_size;
425         }
426 #endif
427 
428         if (map_pg != bootmap_pfn + bootmap_pages)
429                 BUG();
430 
431 }
432 
433 /*
434  * paging_init() sets up the page tables, initialises the zone memory
435  * maps, and sets up the zero page, bad page and bad page tables.
436  */
437 void __init paging_init(struct meminfo *mi, struct machine_desc *mdesc)
438 {
439         void *zero_page;
440         int node;
441 
442         memcpy(&meminfo, mi, sizeof(meminfo));
443 
444         /*
445          * allocate the zero page.  Note that we count on this going ok.
446          */
447         zero_page = alloc_bootmem_low_pages(PAGE_SIZE);
448 
449         /*
450          * initialise the page tables.
451          */
452         memtable_init(mi);
453         if (mdesc->map_io)
454                 mdesc->map_io();
455         flush_tlb_all();
456 
457         /*
458          * initialise the zones within each node
459          */
460         for (node = 0; node < numnodes; node++) {
461                 unsigned long zone_size[MAX_NR_ZONES];
462                 unsigned long zhole_size[MAX_NR_ZONES];
463                 struct bootmem_data *bdata;
464                 pg_data_t *pgdat;
465                 int i;
466 
467                 /*
468                  * Initialise the zone size information.
469                  */
470                 for (i = 0; i < MAX_NR_ZONES; i++) {
471                         zone_size[i]  = 0;
472                         zhole_size[i] = 0;
473                 }
474 
475                 pgdat = NODE_DATA(node);
476                 bdata = pgdat->bdata;
477 
478                 /*
479                  * The size of this node has already been determined.
480                  * If we need to do anything fancy with the allocation
481                  * of this memory to the zones, now is the time to do
482                  * it.
483                  */
484                 zone_size[0] = bdata->node_low_pfn -
485                                 (bdata->node_boot_start >> PAGE_SHIFT);
486 
487                 /*
488                  * If this zone has zero size, skip it.
489                  */
490                 if (!zone_size[0])
491                         continue;
492 
493                 /*
494                  * For each bank in this node, calculate the size of the
495                  * holes.  holes = node_size - sum(bank_sizes_in_node)
496                  */
497                 zhole_size[0] = zone_size[0];
498                 for (i = 0; i < mi->nr_banks; i++) {
499                         if (mi->bank[i].node != node)
500                                 continue;
501 
502                         zhole_size[0] -= mi->bank[i].size >> PAGE_SHIFT;
503                 }
504 
505                 /*
506                  * Adjust the sizes according to any special
507                  * requirements for this machine type.
508                  */
509                 arch_adjust_zones(node, zone_size, zhole_size);
510 
511                 free_area_init_node(node, pgdat, 0, zone_size,
512                                 bdata->node_boot_start >> PAGE_SHIFT, zhole_size);
513         }
514 
515 #ifndef CONFIG_DISCONTIGMEM
516         mem_map = contig_page_data.node_mem_map;
517 #endif
518 
519         /*
520          * finish off the bad pages once
521          * the mem_map is initialised
522          */
523         memzero(zero_page, PAGE_SIZE);
524         empty_zero_page = virt_to_page(zero_page);
525         flush_dcache_page(empty_zero_page);
526 }
527 
528 static inline void free_area(unsigned long addr, unsigned long end, char *s)
529 {
530         unsigned int size = (end - addr) >> 10;
531 
532         for (; addr < end; addr += PAGE_SIZE) {
533                 struct page *page = virt_to_page(addr);
534                 ClearPageReserved(page);
535                 set_page_count(page, 1);
536                 free_page(addr);
537                 totalram_pages++;
538         }
539 
540         if (size && s)
541                 printk(KERN_INFO "Freeing %s memory: %dK\n", s, size);
542 }
543 
544 /*
545  * mem_init() marks the free areas in the mem_map and tells us how much
546  * memory is free.  This is done after various parts of the system have
547  * claimed their memory after the kernel image.
548  */
549 void __init mem_init(void)
550 {
551         unsigned int codepages, datapages, initpages;
552         int i, node;
553 
554         codepages = &_etext - &_text;
555         datapages = &_end - &_etext;
556         initpages = &__init_end - &__init_begin;
557 
558         high_memory = (void *)__va(meminfo.end);
559 #ifndef CONFIG_DISCONTIGMEM
560         max_mapnr   = virt_to_page(high_memory) - mem_map;
561 #endif
562 
563         /*
564          * We may have non-contiguous memory.
565          */
566         if (meminfo.nr_banks != 1)
567                 create_memmap_holes(&meminfo);
568 
569         /* this will put all unused low memory onto the freelists */
570         for (node = 0; node < numnodes; node++) {
571                 pg_data_t *pgdat = NODE_DATA(node);
572 
573                 if (pgdat->node_spanned_pages != 0)
574                         totalram_pages += free_all_bootmem_node(pgdat);
575         }
576 
577 #ifdef CONFIG_SA1111
578         /* now that our DMA memory is actually so designated, we can free it */
579         free_area(PAGE_OFFSET, (unsigned long)swapper_pg_dir, NULL);
580 #endif
581 
582         /*
583          * Since our memory may not be contiguous, calculate the
584          * real number of pages we have in this system
585          */
586         printk(KERN_INFO "Memory:");
587 
588         num_physpages = 0;
589         for (i = 0; i < meminfo.nr_banks; i++) {
590                 num_physpages += meminfo.bank[i].size >> PAGE_SHIFT;
591                 printk(" %ldMB", meminfo.bank[i].size >> 20);
592         }
593 
594         printk(" = %luMB total\n", num_physpages >> (20 - PAGE_SHIFT));
595         printk(KERN_NOTICE "Memory: %luKB available (%dK code, "
596                 "%dK data, %dK init)\n",
597                 (unsigned long) nr_free_pages() << (PAGE_SHIFT-10),
598                 codepages >> 10, datapages >> 10, initpages >> 10);
599 
600         if (PAGE_SIZE >= 16384 && num_physpages <= 128) {
601                 extern int sysctl_overcommit_memory;
602                 /*
603                  * On a machine this small we won't get
604                  * anywhere without overcommit, so turn
605                  * it on by default.
606                  */
607                 sysctl_overcommit_memory = 1;
608         }
609 }
610 
611 void free_initmem(void)
612 {
613         if (!machine_is_integrator()) {
614                 free_area((unsigned long)(&__init_begin),
615                           (unsigned long)(&__init_end),
616                           "init");
617         }
618 }
619 
620 #ifdef CONFIG_BLK_DEV_INITRD
621 
622 static int keep_initrd;
623 
624 void free_initrd_mem(unsigned long start, unsigned long end)
625 {
626         if (!keep_initrd)
627                 free_area(start, end, "initrd");
628 }
629 
630 static int __init keepinitrd_setup(char *__unused)
631 {
632         keep_initrd = 1;
633         return 1;
634 }
635 
636 __setup("keepinitrd", keepinitrd_setup);
637 #endif
638 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp