1 /*
2  * Initialize MMU support.
3  *
4  * Copyright (C) 1998-2002 Hewlett-Packard Co
5  *	David Mosberger-Tang <davidm@hpl.hp.com>
6  */
7 #include <linux/config.h>
8 #include <linux/module.h>
9 #include <linux/kernel.h>
10 #include <linux/init.h>
11 
12 #include <linux/bootmem.h>
13 #include <linux/mm.h>
14 #include <linux/personality.h>
15 #include <linux/reboot.h>
16 #include <linux/slab.h>
17 #include <linux/swap.h>
18 #include <linux/efi.h>
19 #include <linux/mmzone.h>
20 
21 #include <asm/bitops.h>
22 #include <asm/dma.h>
23 #include <asm/ia32.h>
24 #include <asm/io.h>
25 #include <asm/machvec.h>
26 #include <asm/numa.h>
27 #include <asm/pgalloc.h>
28 #include <asm/sal.h>
29 #include <asm/system.h>
30 #include <asm/uaccess.h>
31 #include <asm/mca.h>
32 
33 /* References to section boundaries: */
34 extern char _stext, _etext, _edata, __init_begin, __init_end;
35 
36 extern void ia64_tlb_init (void);
37 extern int  filter_rsvd_memory (unsigned long, unsigned long, void *);
38 
39 /* Note - may be changed by platform_setup */
40 unsigned long MAX_DMA_ADDRESS = PAGE_OFFSET + 0x100000000UL;
41 #define LARGE_GAP 0x40000000 /* Use virtual mem map if a hole is > than this */
42 
43 static unsigned long totalram_pages, reserved_pages;
44 struct page *zero_page_memmap_ptr;		/* map entry for zero page */
45 
46 unsigned long vmalloc_end = VMALLOC_END_INIT;
47 
48 static struct page *vmem_map;
49 static unsigned long num_dma_physpages;
50 
51 int
do_check_pgt_cache(int low,int high)52 do_check_pgt_cache (int low, int high)
53 {
54 	int freed = 0;
55 
56 	if (pgtable_cache_size > high) {
57 		do {
58 			if (pgd_quicklist)
59 				free_page((unsigned long)pgd_alloc_one_fast(0)), ++freed;
60 			if (pmd_quicklist)
61 				free_page((unsigned long)pmd_alloc_one_fast(0, 0)), ++freed;
62 			if (pte_quicklist)
63 				free_page((unsigned long)pte_alloc_one_fast(0, 0)), ++freed;
64 		} while (pgtable_cache_size > low);
65 	}
66 	return freed;
67 }
68 
69 inline void
ia64_set_rbs_bot(void)70 ia64_set_rbs_bot (void)
71 {
72 	unsigned long stack_size = current->rlim[RLIMIT_STACK].rlim_max & -16;
73 
74 	if (stack_size > MAX_USER_STACK_SIZE)
75 		stack_size = MAX_USER_STACK_SIZE;
76 	current->thread.rbs_bot = STACK_TOP - stack_size;
77 }
78 
79 /*
80  * This performs some platform-dependent address space initialization.
81  * On IA-64, we want to setup the VM area for the register backing
82  * store (which grows upwards) and install the gateway page which is
83  * used for signal trampolines, etc.
84  */
85 void
ia64_init_addr_space(void)86 ia64_init_addr_space (void)
87 {
88 	struct vm_area_struct *vma;
89 
90 	ia64_set_rbs_bot();
91 
92 	/*
93 	 * If we're out of memory and kmem_cache_alloc() returns NULL, we simply ignore
94 	 * the problem.  When the process attempts to write to the register backing store
95 	 * for the first time, it will get a SEGFAULT in this case.
96 	 */
97 	vma = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
98 	if (vma) {
99 		vma->vm_mm = current->mm;
100 		vma->vm_start = current->thread.rbs_bot & PAGE_MASK;
101 		vma->vm_end = vma->vm_start + PAGE_SIZE;
102 		vma->vm_page_prot = PAGE_COPY;
103 		vma->vm_flags = VM_READ|VM_WRITE|VM_MAYREAD|VM_MAYWRITE|VM_GROWSUP;
104 		vma->vm_ops = NULL;
105 		vma->vm_pgoff = 0;
106 		vma->vm_file = NULL;
107 		vma->vm_private_data = NULL;
108 		down_write(&current->mm->mmap_sem);
109 		if (insert_vm_struct(current->mm, vma)) {
110 			up_write(&current->mm->mmap_sem);
111 			kmem_cache_free(vm_area_cachep, vma);
112 			return;
113 		}
114 		up_write(&current->mm->mmap_sem);
115 	}
116 
117 	/* map NaT-page at address zero to speed up speculative dereferencing of NULL: */
118 	if (!(current->personality & MMAP_PAGE_ZERO)) {
119 		vma = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
120 		if (vma) {
121 			memset(vma, 0, sizeof(*vma));
122 			vma->vm_mm = current->mm;
123 			vma->vm_end = PAGE_SIZE;
124 			vma->vm_page_prot = __pgprot(pgprot_val(PAGE_READONLY) | _PAGE_MA_NAT);
125 			vma->vm_flags = VM_READ | VM_MAYREAD | VM_IO | VM_RESERVED;
126 			down_write(&current->mm->mmap_sem);
127 			if (insert_vm_struct(current->mm, vma)) {
128 				up_write(&current->mm->mmap_sem);
129 				kmem_cache_free(vm_area_cachep, vma);
130 				return;
131 			}
132 			up_write(&current->mm->mmap_sem);
133 		}
134 	}
135 }
136 
137 void
free_initmem(void)138 free_initmem (void)
139 {
140 	unsigned long addr, eaddr;
141 
142 	addr = (unsigned long) ia64_imva(&__init_begin);
143 	eaddr = (unsigned long) ia64_imva(&__init_end);
144 	for (; addr < eaddr; addr += PAGE_SIZE) {
145 		clear_bit(PG_reserved, &virt_to_page((void *)addr)->flags);
146 		set_page_count(virt_to_page((void *)addr), 1);
147 		free_page(addr);
148 		++totalram_pages;
149 	}
150 	printk(KERN_INFO "Freeing unused kernel memory: %ldkB freed\n",
151 		(&__init_end - &__init_begin) >> 10);
152 }
153 
154 void
free_initrd_mem(unsigned long start,unsigned long end)155 free_initrd_mem(unsigned long start, unsigned long end)
156 {
157 	/*
158 	 * EFI uses 4KB pages while the kernel can use 4KB  or bigger.
159 	 * Thus EFI and the kernel may have different page sizes. It is
160 	 * therefore possible to have the initrd share the same page as
161 	 * the end of the kernel (given current setup).
162 	 *
163 	 * To avoid freeing/using the wrong page (kernel sized) we:
164 	 *	- align up the beginning of initrd
165 	 *	- align down the end of initrd
166 	 *
167 	 *  |             |
168 	 *  |=============| a000
169 	 *  |             |
170 	 *  |             |
171 	 *  |             | 9000
172 	 *  |/////////////|
173 	 *  |/////////////|
174 	 *  |=============| 8000
175 	 *  |///INITRD////|
176 	 *  |/////////////|
177 	 *  |/////////////| 7000
178 	 *  |             |
179 	 *  |KKKKKKKKKKKKK|
180 	 *  |=============| 6000
181 	 *  |KKKKKKKKKKKKK|
182 	 *  |KKKKKKKKKKKKK|
183 	 *  K=kernel using 8KB pages
184 	 *
185 	 * In this example, we must free page 8000 ONLY. So we must align up
186 	 * initrd_start and keep initrd_end as is.
187 	 */
188 	start = PAGE_ALIGN(start);
189 	end = end & PAGE_MASK;
190 
191 	if (start < end)
192 		printk(KERN_INFO "Freeing initrd memory: %ldkB freed\n", (end - start) >> 10);
193 
194 	for (; start < end; start += PAGE_SIZE) {
195 		if (!VALID_PAGE(virt_to_page((void *)start)))
196 			continue;
197 		clear_bit(PG_reserved, &virt_to_page((void *)start)->flags);
198 		set_page_count(virt_to_page((void *)start), 1);
199 		free_page(start);
200 		++totalram_pages;
201 	}
202 }
203 
204 void
si_meminfo(struct sysinfo * val)205 si_meminfo (struct sysinfo *val)
206 {
207 	val->totalram = totalram_pages;
208 	val->sharedram = 0;
209 	val->freeram = nr_free_pages();
210 	val->bufferram = atomic_read(&buffermem_pages);
211 	val->totalhigh = 0;
212 	val->freehigh = 0;
213 	val->mem_unit = PAGE_SIZE;
214 	return;
215 }
216 
217 void
show_mem(void)218 show_mem(void)
219 {
220 	int i, reserved;
221 	int shared, cached;
222 	pg_data_t *pgdat;
223 	char *tchar = (numnodes > 1) ? "\t" : "";
224 
225 	printk("Mem-info:\n");
226 	show_free_areas();
227 
228 	printk("Free swap:       %6dkB\n", nr_swap_pages<<(PAGE_SHIFT-10));
229 	for_each_pgdat(pgdat) {
230 		reserved=0;
231 		cached=0;
232 		shared=0;
233 		if (numnodes > 1)
234 			printk("Node ID: %d\n", pgdat->node_id);
235 		for(i = 0; i < pgdat->node_size; i++) {
236 			if (!VALID_PAGE(pgdat->node_mem_map+i))
237 				continue;
238 			if (PageReserved(pgdat->node_mem_map+i))
239 				reserved++;
240 			else if (PageSwapCache(pgdat->node_mem_map+i))
241 				cached++;
242 			else if (page_count(pgdat->node_mem_map + i))
243 				shared += page_count(pgdat->node_mem_map + i) - 1;
244 		}
245 		printk("%s%ld pages of RAM\n", tchar, pgdat->node_size);
246 		printk("%s%d reserved pages\n", tchar, reserved);
247 		printk("%s%d pages shared\n", tchar, shared);
248 		printk("%s%d pages swap cached\n", tchar, cached);
249 	}
250 	printk("Total of %ld pages in page table cache\n", pgtable_cache_size);
251 	show_buffers();
252 	printk("%d free buffer pages\n", nr_free_buffer_pages());
253 }
254 
255 /*
256  * This is like put_dirty_page() but installs a clean page with PAGE_GATE protection
257  * (execute-only, typically).
258  */
259 struct page *
put_gate_page(struct page * page,unsigned long address)260 put_gate_page (struct page *page, unsigned long address)
261 {
262 	pgd_t *pgd;
263 	pmd_t *pmd;
264 	pte_t *pte;
265 
266 	if (!PageReserved(page))
267 		printk(KERN_ERR "put_gate_page: gate page at 0x%p not in reserved memory\n",
268 		       page_address(page));
269 
270 	pgd = pgd_offset_k(address);		/* note: this is NOT pgd_offset()! */
271 
272 	spin_lock(&init_mm.page_table_lock);
273 	{
274 		pmd = pmd_alloc(&init_mm, pgd, address);
275 		if (!pmd)
276 			goto out;
277 		pte = pte_alloc(&init_mm, pmd, address);
278 		if (!pte)
279 			goto out;
280 		if (!pte_none(*pte)) {
281 			pte_ERROR(*pte);
282 			goto out;
283 		}
284 		flush_page_to_ram(page);
285 		set_pte(pte, mk_pte(page, PAGE_GATE));
286 	}
287   out:	spin_unlock(&init_mm.page_table_lock);
288 	/* no need for flush_tlb */
289 	return page;
290 }
291 
292 void __init
ia64_mmu_init(void * my_cpu_data)293 ia64_mmu_init (void *my_cpu_data)
294 {
295 	unsigned long psr, rid, pta, impl_va_bits;
296 	extern void __init tlb_init (void);
297 #ifdef CONFIG_IA64_MCA
298 	int cpu;
299 #endif
300 
301 #ifdef CONFIG_DISABLE_VHPT
302 #	define VHPT_ENABLE_BIT	0
303 #else
304 #	define VHPT_ENABLE_BIT	1
305 #endif
306 
307 	/*
308 	 * Set up the kernel identity mapping for regions 6 and 5.  The mapping for region
309 	 * 7 is setup up in _start().
310 	 */
311 	psr = ia64_clear_ic();
312 
313 	rid = ia64_rid(IA64_REGION_ID_KERNEL, __IA64_UNCACHED_OFFSET);
314 	ia64_set_rr(__IA64_UNCACHED_OFFSET, (rid << 8) | (IA64_GRANULE_SHIFT << 2));
315 
316 	rid = ia64_rid(IA64_REGION_ID_KERNEL, VMALLOC_START);
317 	ia64_set_rr(VMALLOC_START, (rid << 8) | (PAGE_SHIFT << 2) | 1);
318 
319 	/* ensure rr6 is up-to-date before inserting the PERCPU_ADDR translation: */
320 	ia64_srlz_d();
321 
322 	ia64_itr(0x2, IA64_TR_PERCPU_DATA, PERCPU_ADDR,
323 		 pte_val(mk_pte_phys(__pa(my_cpu_data), PAGE_KERNEL)), PAGE_SHIFT);
324 
325 	ia64_set_psr(psr);
326 	ia64_srlz_i();
327 
328 	/*
329 	 * Check if the virtually mapped linear page table (VMLPT) overlaps with a mapped
330 	 * address space.  The IA-64 architecture guarantees that at least 50 bits of
331 	 * virtual address space are implemented but if we pick a large enough page size
332 	 * (e.g., 64KB), the mapped address space is big enough that it will overlap with
333 	 * VMLPT.  I assume that once we run on machines big enough to warrant 64KB pages,
334 	 * IMPL_VA_MSB will be significantly bigger, so this is unlikely to become a
335 	 * problem in practice.  Alternatively, we could truncate the top of the mapped
336 	 * address space to not permit mappings that would overlap with the VMLPT.
337 	 * --davidm 00/12/06
338 	 */
339 #	define pte_bits			3
340 #	define mapped_space_bits	(3*(PAGE_SHIFT - pte_bits) + PAGE_SHIFT)
341 	/*
342 	 * The virtual page table has to cover the entire implemented address space within
343 	 * a region even though not all of this space may be mappable.  The reason for
344 	 * this is that the Access bit and Dirty bit fault handlers perform
345 	 * non-speculative accesses to the virtual page table, so the address range of the
346 	 * virtual page table itself needs to be covered by virtual page table.
347 	 */
348 #	define vmlpt_bits		(impl_va_bits - PAGE_SHIFT + pte_bits)
349 #	define POW2(n)			(1ULL << (n))
350 
351 	impl_va_bits = ffz(~(local_cpu_data->unimpl_va_mask | (7UL << 61)));
352 
353 	if (impl_va_bits < 51 || impl_va_bits > 61)
354 		panic("CPU has bogus IMPL_VA_MSB value of %lu!\n", impl_va_bits - 1);
355 
356 	/* place the VMLPT at the end of each page-table mapped region: */
357 	pta = POW2(61) - POW2(vmlpt_bits);
358 
359 	if (POW2(mapped_space_bits) >= pta)
360 		panic("mm/init: overlap between virtually mapped linear page table and "
361 		      "mapped kernel space!");
362 	/*
363 	 * Set the (virtually mapped linear) page table address.  Bit
364 	 * 8 selects between the short and long format, bits 2-7 the
365 	 * size of the table, and bit 0 whether the VHPT walker is
366 	 * enabled.
367 	 */
368 	ia64_set_pta(pta | (0 << 8) | (vmlpt_bits << 2) | VHPT_ENABLE_BIT);
369 
370 	ia64_tlb_init();
371 
372 #ifdef	CONFIG_IA64_MCA
373 	cpu = smp_processor_id();
374 
375 	/* mca handler uses cr.lid as key to pick the right entry */
376 	ia64_mca_tlb_list[cpu].cr_lid = ia64_get_lid();
377 
378 	/* insert this percpu data information into our list for MCA recovery purposes */
379 	ia64_mca_tlb_list[cpu].percpu_paddr = pte_val(mk_pte_phys(__pa(my_cpu_data), PAGE_KERNEL));
380 	/* Also save per-cpu tlb flush recipe for use in physical mode mca handler */
381 	ia64_mca_tlb_list[cpu].ptce_base = local_cpu_data->ptce_base;
382 	ia64_mca_tlb_list[cpu].ptce_count[0] = local_cpu_data->ptce_count[0];
383 	ia64_mca_tlb_list[cpu].ptce_count[1] = local_cpu_data->ptce_count[1];
384 	ia64_mca_tlb_list[cpu].ptce_stride[0] = local_cpu_data->ptce_stride[0];
385 	ia64_mca_tlb_list[cpu].ptce_stride[1] = local_cpu_data->ptce_stride[1];
386 #endif
387 }
388 
389 static int
create_mem_map_page_table(u64 start,u64 end,void * arg)390 create_mem_map_page_table (u64 start, u64 end, void *arg)
391 {
392 	unsigned long address, start_page, end_page, next_blk_page;
393 	unsigned long blk_start;
394 	struct page *map_start, *map_end;
395 	int node=0;
396 	pgd_t *pgd;
397 	pmd_t *pmd;
398 	pte_t *pte;
399 
400 	/* should we use platform_map_nr here? */
401 
402 	map_start = vmem_map + MAP_NR_DENSE(start);
403 	map_end   = vmem_map + MAP_NR_DENSE(end);
404 
405 	start_page = (unsigned long) map_start & PAGE_MASK;
406 	end_page = PAGE_ALIGN((unsigned long) map_end);
407 
408 	/* force the first iteration to get node id */
409 	blk_start = start;
410 	next_blk_page = 0;
411 
412 	for (address = start_page; address < end_page; address += PAGE_SIZE) {
413 
414 		/* if we went across a node boundary, get new nid */
415 		if (address >= next_blk_page) {
416 			struct page *map_next_blk;
417 
418 			node = paddr_to_nid(__pa(blk_start));
419 
420 			/* get end addr of this memblk as next blk_start */
421 			blk_start = (unsigned long) __va(min(end, memblk_endpaddr(__pa(blk_start))));
422 			map_next_blk = vmem_map + MAP_NR_DENSE(blk_start);
423 			next_blk_page = PAGE_ALIGN((unsigned long) map_next_blk);
424 		}
425 
426 		pgd = pgd_offset_k(address);
427 		if (pgd_none(*pgd))
428 			pgd_populate(&init_mm, pgd, alloc_bootmem_pages_node(NODE_DATA(node), PAGE_SIZE));
429 		pmd = pmd_offset(pgd, address);
430 
431 		if (pmd_none(*pmd))
432 			pmd_populate(&init_mm, pmd, alloc_bootmem_pages_node(NODE_DATA(node), PAGE_SIZE));
433 		pte = pte_offset(pmd, address);
434 
435 		if (pte_none(*pte))
436 			set_pte(pte, mk_pte_phys(__pa(alloc_bootmem_pages_node(NODE_DATA(node), PAGE_SIZE)),
437 						 PAGE_KERNEL));
438  	}
439  	return 0;
440 }
441 
442 struct memmap_init_callback_data {
443 	memmap_init_callback_t *memmap_init;
444 	struct page *start;
445 	struct page *end;
446 	int zone;
447 	int highmem;
448 };
449 
450 struct memmap_count_callback_data {
451 	int node;
452 	unsigned long num_physpages;
453 	unsigned long num_dma_physpages;
454 	unsigned long min_pfn;
455 	unsigned long max_pfn;
456 } cdata;
457 
458 static int
virtual_memmap_init(u64 start,u64 end,void * arg)459 virtual_memmap_init (u64 start, u64 end, void *arg)
460 {
461 	struct memmap_init_callback_data *args;
462 	struct page *map_start, *map_end;
463 
464 	args = (struct memmap_init_callback_data *) arg;
465 
466 	/* Should we use platform_map_nr here? */
467 
468 	map_start = mem_map + MAP_NR_DENSE(start);
469 	map_end   = mem_map + MAP_NR_DENSE(end);
470 
471 	if (map_start < args->start)
472 		map_start = args->start;
473 	if (map_end > args->end)
474 		map_end = args->end;
475 
476 	/*
477 	 * We have to initialize "out of bounds" struct page elements
478 	 * that fit completely on the same pages that were allocated
479 	 * for the "in bounds" elements because they may be referenced
480 	 * later (and found to be "reserved").
481 	 */
482 	map_start -= ((unsigned long) map_start & (PAGE_SIZE - 1))
483 			/ sizeof(struct page);
484 	map_end += ((PAGE_ALIGN((unsigned long) map_end) -
485 				(unsigned long) map_end)
486 			/ sizeof(struct page));
487 
488 	if (map_start < map_end)
489 		(*args->memmap_init)(map_start, map_end, args->zone,
490 				     page_to_phys(map_start), args->highmem);
491 
492 	return 0;
493 }
494 
495 unsigned long
arch_memmap_init(memmap_init_callback_t * memmap_init,struct page * start,struct page * end,int zone,unsigned long start_paddr,int highmem)496 arch_memmap_init (memmap_init_callback_t *memmap_init, struct page *start,
497 	struct page *end, int zone, unsigned long start_paddr, int highmem)
498 {
499 	if (!vmem_map)
500 		memmap_init(start,end,zone,page_to_phys(start),highmem);
501 	else {
502 		struct memmap_init_callback_data args;
503 
504 		args.memmap_init = memmap_init;
505 		args.start = start;
506 		args.end = end;
507 		args.zone = zone;
508 		args.highmem = highmem;
509 
510 		efi_memmap_walk(virtual_memmap_init, &args);
511 	}
512 
513 	return page_to_phys(end-1) + PAGE_SIZE;;
514 }
515 
516 int
ia64_page_valid(struct page * page)517 ia64_page_valid (struct page *page)
518 {
519 	char byte;
520 
521 	return     (__get_user(byte, (char *) page) == 0)
522 		&& (__get_user(byte, (char *) (page + 1) - 1) == 0);
523 }
524 
525 #define GRANULEROUNDDOWN(n) ((n) & ~(IA64_GRANULE_SIZE-1))
526 #define GRANULEROUNDUP(n) (((n)+IA64_GRANULE_SIZE-1) & ~(IA64_GRANULE_SIZE-1))
527 #define ORDERROUNDDOWN(n) ((n) & ~((PAGE_SIZE<<MAX_ORDER)-1))
528 static int
count_pages(u64 start,u64 end,int node)529 count_pages (u64 start, u64 end, int node)
530 {
531 	start = __pa(start);
532 	end = __pa(end);
533 	if (node == cdata.node) {
534 		cdata.num_physpages += (end - start) >> PAGE_SHIFT;
535 		if (start <= __pa(MAX_DMA_ADDRESS))
536 			cdata.num_dma_physpages += (min(end, __pa(MAX_DMA_ADDRESS)) - start) >> PAGE_SHIFT;
537 		start = GRANULEROUNDDOWN(__pa(start));
538 		start = ORDERROUNDDOWN(start);
539 		end = GRANULEROUNDUP(__pa(end));
540 		cdata.max_pfn = max(cdata.max_pfn, end >> PAGE_SHIFT);
541 		cdata.min_pfn = min(cdata.min_pfn, start >> PAGE_SHIFT);
542 	}
543 	return 0;
544 }
545 
546 static int
find_largest_hole(u64 start,u64 end,void * arg)547 find_largest_hole(u64 start, u64 end, void *arg)
548 {
549 	u64 *max_gap = arg;
550 	static u64 last_end = PAGE_OFFSET;
551 
552 	/* NOTE: this algorithm assumes efi memmap table is ordered */
553 
554 	if (*max_gap < (start - last_end))
555 		*max_gap = start - last_end;
556 	last_end = end;
557 	return 0;
558 }
559 
560 /*
561  * Set up the page tables.
562  */
563 void
paging_init(void)564 paging_init (void)
565 {
566 	unsigned long max_dma;
567 	unsigned long zones_size[MAX_NR_ZONES];
568 	unsigned long zholes_size[MAX_NR_ZONES];
569 	unsigned long max_gap;
570 	int node;
571 
572 	/* initialize mem_map[] */
573 
574 	max_dma = virt_to_phys((void *) MAX_DMA_ADDRESS) >> PAGE_SHIFT;
575 	max_gap = 0;
576 	efi_memmap_walk(find_largest_hole, (u64 *)&max_gap);
577 
578 	for (node=0; node < numnodes; node++) {
579 		memset(zones_size, 0, sizeof(zones_size));
580 		memset(zholes_size, 0, sizeof(zholes_size));
581 		memset(&cdata, 0, sizeof(cdata));
582 
583 		cdata.node = node;
584 		cdata.min_pfn = ~0;
585 
586 		efi_memmap_walk(filter_rsvd_memory, count_pages);
587 		num_dma_physpages += cdata.num_dma_physpages;
588 		num_physpages += cdata.num_physpages;
589 
590 		if (cdata.min_pfn >= max_dma) {
591 			zones_size[ZONE_NORMAL] = cdata.max_pfn - cdata.min_pfn;
592 			zholes_size[ZONE_NORMAL] = cdata.max_pfn - cdata.min_pfn - cdata.num_physpages;
593 		} else if (cdata.max_pfn < max_dma) {
594 			zones_size[ZONE_DMA] = cdata.max_pfn - cdata.min_pfn;
595 			zholes_size[ZONE_DMA] = cdata.max_pfn - cdata.min_pfn - cdata.num_dma_physpages;
596 		} else {
597 			zones_size[ZONE_DMA] = max_dma - cdata.min_pfn;
598 			zholes_size[ZONE_DMA] = zones_size[ZONE_DMA] - cdata.num_dma_physpages;
599 			zones_size[ZONE_NORMAL] = cdata.max_pfn - max_dma;
600 			zholes_size[ZONE_NORMAL] = zones_size[ZONE_NORMAL] - (cdata.num_physpages - cdata.num_dma_physpages);
601 		}
602 
603 		if (numnodes == 1 && max_gap < LARGE_GAP) {
604 			vmem_map = (struct page *)0;
605 			zones_size[ZONE_DMA] += cdata.min_pfn;
606 			zholes_size[ZONE_DMA] += cdata.min_pfn;
607 			free_area_init_core(0, NODE_DATA(node), &mem_map, zones_size, 0, zholes_size, NULL);
608 		} else {
609 
610 			/* allocate virtual mem_map */
611 
612 			if (node == 0) {
613 				unsigned long map_size;
614 				map_size = PAGE_ALIGN(max_low_pfn*sizeof(struct page));
615 				vmalloc_end -= map_size;
616 				mem_map = vmem_map = (struct page *) vmalloc_end;
617 				efi_memmap_walk(create_mem_map_page_table, 0);
618 				printk(KERN_INFO "Virtual mem_map starts at 0x%p\n", mem_map);
619 			}
620 
621 			free_area_init_node(node, NODE_DATA(node), vmem_map+cdata.min_pfn, zones_size,
622 				cdata.min_pfn<<PAGE_SHIFT, zholes_size);
623 		}
624 	}
625 
626 	zero_page_memmap_ptr = virt_to_page(ia64_imva(empty_zero_page));
627 }
628 
629 static int
count_reserved_pages(u64 start,u64 end,void * arg)630 count_reserved_pages (u64 start, u64 end, void *arg)
631 {
632 	unsigned long num_reserved = 0;
633 	struct page *pg;
634 
635 	for (pg = virt_to_page((void *)start); pg < virt_to_page((void *)end); ++pg)
636 		if (PageReserved(pg))
637 			++num_reserved;
638 	reserved_pages += num_reserved;
639 	return 0;
640 }
641 
642 void
mem_init(void)643 mem_init (void)
644 {
645 	extern char __start_gate_section[];
646 	long codesize, datasize, initsize;
647 	unsigned long num_pgt_pages;
648 	pg_data_t *pgdat;
649 
650 
651 #ifdef CONFIG_PCI
652 	/*
653 	 * This needs to be called _after_ the command line has been parsed but _before_
654 	 * any drivers that may need the PCI DMA interface are initialized or bootmem has
655 	 * been freed.
656 	 */
657 	platform_pci_dma_init();
658 #endif
659 
660 	if (!mem_map)
661 		BUG();
662 
663 	max_mapnr = max_low_pfn;
664 	high_memory = __va(max_low_pfn * PAGE_SIZE);
665 
666 	for_each_pgdat(pgdat)
667 		totalram_pages += free_all_bootmem_node(pgdat);
668 
669 	reserved_pages = 0;
670 	efi_memmap_walk(filter_rsvd_memory, count_reserved_pages);
671 
672 	codesize =  (unsigned long) &_etext - (unsigned long) &_stext;
673 	datasize =  (unsigned long) &_edata - (unsigned long) &_etext;
674 	initsize =  (unsigned long) &__init_end - (unsigned long) &__init_begin;
675 
676 	printk(KERN_INFO "Memory: %luk/%luk available (%luk code, %luk reserved, %luk data, %luk init)\n",
677 	       (unsigned long) nr_free_pages() << (PAGE_SHIFT - 10),
678 	       num_physpages << (PAGE_SHIFT - 10), codesize >> 10,
679 	       reserved_pages << (PAGE_SHIFT - 10), datasize >> 10, initsize >> 10);
680 
681 	/*
682 	 * Allow for enough (cached) page table pages so that we can map the entire memory
683 	 * at least once.  Each task also needs a couple of page tables pages, so add in a
684 	 * fudge factor for that (don't use "threads-max" here; that would be wrong!).
685 	 * Don't allow the cache to be more than 10% of total memory, though.
686 	 */
687 #	define NUM_TASKS	500	/* typical number of tasks */
688 	num_pgt_pages = nr_free_pages() / PTRS_PER_PGD + NUM_TASKS;
689 	if (num_pgt_pages > nr_free_pages() / 10)
690 		num_pgt_pages = nr_free_pages() / 10;
691 	if (num_pgt_pages > pgt_cache_water[1])
692 		pgt_cache_water[1] = num_pgt_pages;
693 
694 	/* install the gate page in the global page table: */
695 	put_gate_page(virt_to_page(ia64_imva(__start_gate_section)), GATE_ADDR);
696 
697 #ifdef CONFIG_IA32_SUPPORT
698 	ia32_gdt_init();
699 #endif
700 }
701