Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * memory.c: memory initialisation code. |
| 3 | * |
| 4 | * Copyright (C) 1998 Harald Koerfgen, Frieder Streffer and Paul M. Antoine |
| 5 | * Copyright (C) 2000, 2002 Maciej W. Rozycki |
| 6 | */ |
| 7 | #include <linux/config.h> |
| 8 | #include <linux/init.h> |
| 9 | #include <linux/kernel.h> |
| 10 | #include <linux/mm.h> |
| 11 | #include <linux/bootmem.h> |
| 12 | #include <linux/types.h> |
| 13 | |
| 14 | #include <asm/addrspace.h> |
| 15 | #include <asm/bootinfo.h> |
| 16 | #include <asm/dec/machtype.h> |
| 17 | #include <asm/dec/prom.h> |
| 18 | #include <asm/page.h> |
| 19 | #include <asm/sections.h> |
| 20 | |
| 21 | |
| 22 | volatile unsigned long mem_err = 0; /* So we know an error occurred */ |
| 23 | |
| 24 | /* |
| 25 | * Probe memory in 4MB chunks, waiting for an error to tell us we've fallen |
| 26 | * off the end of real memory. Only suitable for the 2100/3100's (PMAX). |
| 27 | */ |
| 28 | |
| 29 | #define CHUNK_SIZE 0x400000 |
| 30 | |
| 31 | static inline void pmax_setup_memory_region(void) |
| 32 | { |
| 33 | volatile unsigned char *memory_page, dummy; |
| 34 | char old_handler[0x80]; |
| 35 | extern char genexcept_early; |
| 36 | |
| 37 | /* Install exception handler */ |
| 38 | memcpy(&old_handler, (void *)(KSEG0 + 0x80), 0x80); |
| 39 | memcpy((void *)(KSEG0 + 0x80), &genexcept_early, 0x80); |
| 40 | |
| 41 | /* read unmapped and uncached (KSEG1) |
| 42 | * DECstations have at least 4MB RAM |
| 43 | * Assume less than 480MB of RAM, as this is max for 5000/2xx |
| 44 | * FIXME this should be replaced by the first free page! |
| 45 | */ |
| 46 | for (memory_page = (unsigned char *) KSEG1 + CHUNK_SIZE; |
| 47 | (mem_err== 0) && (memory_page < ((unsigned char *) KSEG1+0x1E000000)); |
| 48 | memory_page += CHUNK_SIZE) { |
| 49 | dummy = *memory_page; |
| 50 | } |
| 51 | memcpy((void *)(KSEG0 + 0x80), &old_handler, 0x80); |
| 52 | |
| 53 | add_memory_region(0, (unsigned long)memory_page - KSEG1 - CHUNK_SIZE, |
| 54 | BOOT_MEM_RAM); |
| 55 | } |
| 56 | |
| 57 | /* |
| 58 | * Use the REX prom calls to get hold of the memory bitmap, and thence |
| 59 | * determine memory size. |
| 60 | */ |
| 61 | static inline void rex_setup_memory_region(void) |
| 62 | { |
| 63 | int i, bitmap_size; |
| 64 | unsigned long mem_start = 0, mem_size = 0; |
| 65 | memmap *bm; |
| 66 | |
| 67 | /* some free 64k */ |
| 68 | bm = (memmap *)KSEG0ADDR(0x28000); |
| 69 | |
| 70 | bitmap_size = rex_getbitmap(bm); |
| 71 | |
| 72 | for (i = 0; i < bitmap_size; i++) { |
| 73 | /* FIXME: very simplistically only add full sets of pages */ |
| 74 | if (bm->bitmap[i] == 0xff) |
| 75 | mem_size += (8 * bm->pagesize); |
| 76 | else if (!mem_size) |
| 77 | mem_start += (8 * bm->pagesize); |
| 78 | else { |
| 79 | add_memory_region(mem_start, mem_size, BOOT_MEM_RAM); |
| 80 | mem_start += mem_size + (8 * bm->pagesize); |
| 81 | mem_size = 0; |
| 82 | } |
| 83 | } |
| 84 | if (mem_size) |
| 85 | add_memory_region(mem_start, mem_size, BOOT_MEM_RAM); |
| 86 | } |
| 87 | |
| 88 | void __init prom_meminit(u32 magic) |
| 89 | { |
| 90 | if (!prom_is_rex(magic)) |
| 91 | pmax_setup_memory_region(); |
| 92 | else |
| 93 | rex_setup_memory_region(); |
| 94 | } |
| 95 | |
| 96 | unsigned long __init prom_free_prom_memory(void) |
| 97 | { |
| 98 | unsigned long addr, end; |
| 99 | |
| 100 | /* |
| 101 | * Free everything below the kernel itself but leave |
| 102 | * the first page reserved for the exception handlers. |
| 103 | */ |
| 104 | |
| 105 | #if defined(CONFIG_DECLANCE) || defined(CONFIG_DECLANCE_MODULE) |
| 106 | /* |
| 107 | * Leave 128 KB reserved for Lance memory for |
| 108 | * IOASIC DECstations. |
| 109 | * |
| 110 | * XXX: save this address for use in dec_lance.c? |
| 111 | */ |
| 112 | if (IOASIC) |
| 113 | end = __pa(&_text) - 0x00020000; |
| 114 | else |
| 115 | #endif |
| 116 | end = __pa(&_text); |
| 117 | |
| 118 | addr = PAGE_SIZE; |
| 119 | while (addr < end) { |
| 120 | ClearPageReserved(virt_to_page(__va(addr))); |
| 121 | set_page_count(virt_to_page(__va(addr)), 1); |
| 122 | free_page((unsigned long)__va(addr)); |
| 123 | addr += PAGE_SIZE; |
| 124 | } |
| 125 | |
| 126 | printk("Freeing unused PROM memory: %ldk freed\n", |
| 127 | (end - PAGE_SIZE) >> 10); |
| 128 | |
| 129 | return end - PAGE_SIZE; |
| 130 | } |