1 /*
2  * Carsten Langgaard, carstenl@mips.com
3  * Copyright (C) 1999,2000 MIPS Technologies, Inc.  All rights reserved.
4  *
5  * ########################################################################
6  *
7  *  This program is free software; you can distribute it and/or modify it
8  *  under the terms of the GNU General Public License (Version 2) as
9  *  published by the Free Software Foundation.
10  *
11  *  This program is distributed in the hope it will be useful, but WITHOUT
12  *  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  *  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  *  for more details.
15  *
16  *  You should have received a copy of the GNU General Public License along
17  *  with this program; if not, write to the Free Software Foundation, Inc.,
18  *  59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
19  *
20  * ########################################################################
21  *
22  * PROM library functions for acquiring/using memory descriptors given to
23  * us from the YAMON.
24  *
25  */
26 #include <linux/config.h>
27 #include <linux/init.h>
28 #include <linux/mm.h>
29 #include <linux/bootmem.h>
30 
31 #include <asm/bootinfo.h>
32 #include <asm/page.h>
33 
34 #include <asm/mips-boards/prom.h>
35 
36 /*#define DEBUG*/
37 
38 enum yamon_memtypes {
39 	yamon_dontuse,
40 	yamon_prom,
41 	yamon_free,
42 };
43 struct prom_pmemblock mdesc[PROM_MAX_PMEMBLOCKS];
44 
45 #ifdef DEBUG
46 static char *mtypes[3] = {
47 	"Dont use memory",
48 	"YAMON PROM memory",
49 	"Free memmory",
50 };
51 #endif
52 
53 /* References to section boundaries */
54 extern char _end;
55 
56 #define PFN_ALIGN(x)    (((unsigned long)(x) + (PAGE_SIZE - 1)) & PAGE_MASK)
57 
58 
prom_getmdesc(void)59 struct prom_pmemblock * __init prom_getmdesc(void)
60 {
61 	char *memsize_str;
62 	unsigned int memsize;
63 
64 	memsize_str = prom_getenv("memsize");
65 	if (!memsize_str) {
66 		prom_printf("memsize not set in boot prom, set to default (32Mb)\n");
67 		memsize = 0x02000000;
68 	} else {
69 #ifdef DEBUG
70 		prom_printf("prom_memsize = %s\n", memsize_str);
71 #endif
72 		memsize = simple_strtol(memsize_str, NULL, 0);
73 	}
74 
75 	memset(mdesc, 0, sizeof(mdesc));
76 
77 	mdesc[0].type = yamon_dontuse;
78 	mdesc[0].base = 0x00000000;
79 	mdesc[0].size = 0x00001000;
80 
81 	mdesc[1].type = yamon_prom;
82 	mdesc[1].base = 0x00001000;
83 	mdesc[1].size = 0x000ef000;
84 
85 #if (CONFIG_MIPS_MALTA)
86 	/*
87 	 * The area 0x000f0000-0x000fffff is allocated for BIOS memory by the
88 	 * south bridge and PCI access always forwarded to the ISA Bus and
89 	 * BIOSCS# is always generated.
90 	 * This mean that this area can't be used as DMA memory for PCI
91 	 * devices.
92 	 */
93 	mdesc[2].type = yamon_dontuse;
94 	mdesc[2].base = 0x000f0000;
95 	mdesc[2].size = 0x00010000;
96 #else
97 	mdesc[2].type = yamon_prom;
98 	mdesc[2].base = 0x000f0000;
99 	mdesc[2].size = 0x00010000;
100 #endif
101 
102 	mdesc[3].type = yamon_dontuse;
103 	mdesc[3].base = 0x00100000;
104 	mdesc[3].size = PHYSADDR(PFN_ALIGN(&_end)) - mdesc[3].base;
105 
106 	mdesc[4].type = yamon_free;
107 	mdesc[4].base = PHYSADDR(PFN_ALIGN(&_end));
108 	mdesc[4].size = memsize - mdesc[4].base;
109 
110 	return &mdesc[0];
111 }
112 
prom_memtype_classify(unsigned int type)113 static int __init prom_memtype_classify (unsigned int type)
114 {
115 	switch (type) {
116 	case yamon_free:
117 		return BOOT_MEM_RAM;
118 	case yamon_prom:
119 		return BOOT_MEM_ROM_DATA;
120 	default:
121 		return BOOT_MEM_RESERVED;
122 	}
123 }
124 
prom_meminit(void)125 void __init prom_meminit(void)
126 {
127 	struct prom_pmemblock *p;
128 
129 #ifdef DEBUG
130 	prom_printf("YAMON MEMORY DESCRIPTOR dump:\n");
131 	p = prom_getmdesc();
132 	while (p->size) {
133 		int i = 0;
134 		prom_printf("[%d,%p]: base<%08lx> size<%08lx> type<%s>\n",
135 			    i, p, p->base, p->size, mtypes[p->type]);
136 		p++;
137 		i++;
138 	}
139 #endif
140 	p = prom_getmdesc();
141 
142 	while (p->size) {
143 		long type;
144 		unsigned long base, size;
145 
146 		type = prom_memtype_classify (p->type);
147 		base = p->base;
148 		size = p->size;
149 
150 		add_memory_region(base, size, type);
151                 p++;
152 	}
153 }
154 
155 void __init
prom_free_prom_memory(void)156 prom_free_prom_memory (void)
157 {
158 	int i;
159 	unsigned long freed = 0;
160 	unsigned long addr;
161 
162 	for (i = 0; i < boot_mem_map.nr_map; i++) {
163 		if (boot_mem_map.map[i].type != BOOT_MEM_ROM_DATA)
164 			continue;
165 
166 		addr = boot_mem_map.map[i].addr;
167 		while (addr < boot_mem_map.map[i].addr
168 			      + boot_mem_map.map[i].size) {
169 			ClearPageReserved(virt_to_page(__va(addr)));
170 			set_page_count(virt_to_page(__va(addr)), 1);
171 			free_page((unsigned long)__va(addr));
172 			addr += PAGE_SIZE;
173 			freed += PAGE_SIZE;
174 		}
175 	}
176 	printk("Freeing prom memory: %ldkb freed\n", freed >> 10);
177 }
178