]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - arch/sh/mm/init.c
sh: stack debugging support.
[mirror_ubuntu-jammy-kernel.git] / arch / sh / mm / init.c
CommitLineData
1da177e4
LT
1/* $Id: init.c,v 1.19 2004/02/21 04:42:16 kkojima Exp $
2 *
3 * linux/arch/sh/mm/init.c
4 *
5 * Copyright (C) 1999 Niibe Yutaka
6 * Copyright (C) 2002, 2004 Paul Mundt
7 *
8 * Based on linux/arch/i386/mm/init.c:
9 * Copyright (C) 1995 Linus Torvalds
10 */
11
1da177e4
LT
12#include <linux/signal.h>
13#include <linux/sched.h>
14#include <linux/kernel.h>
15#include <linux/errno.h>
16#include <linux/string.h>
17#include <linux/types.h>
18#include <linux/ptrace.h>
19#include <linux/mman.h>
20#include <linux/mm.h>
21#include <linux/swap.h>
22#include <linux/smp.h>
23#include <linux/init.h>
24#include <linux/highmem.h>
25#include <linux/bootmem.h>
26#include <linux/pagemap.h>
27
28#include <asm/processor.h>
29#include <asm/system.h>
30#include <asm/uaccess.h>
31#include <asm/pgtable.h>
32#include <asm/pgalloc.h>
33#include <asm/mmu_context.h>
34#include <asm/io.h>
35#include <asm/tlb.h>
36#include <asm/cacheflush.h>
37#include <asm/cache.h>
38
39DEFINE_PER_CPU(struct mmu_gather, mmu_gathers);
40pgd_t swapper_pg_dir[PTRS_PER_PGD];
41
42/*
43 * Cache of MMU context last used.
44 */
45unsigned long mmu_context_cache = NO_CONTEXT;
46
47#ifdef CONFIG_MMU
48/* It'd be good if these lines were in the standard header file. */
49#define START_PFN (NODE_DATA(0)->bdata->node_boot_start >> PAGE_SHIFT)
50#define MAX_LOW_PFN (NODE_DATA(0)->bdata->node_low_pfn)
51#endif
52
1da177e4
LT
53void (*copy_page)(void *from, void *to);
54void (*clear_page)(void *to);
55
56void show_mem(void)
57{
58 int i, total = 0, reserved = 0;
59 int shared = 0, cached = 0;
60
61 printk("Mem-info:\n");
62 show_free_areas();
63 printk("Free swap: %6ldkB\n", nr_swap_pages<<(PAGE_SHIFT-10));
64 i = max_mapnr;
65 while (i-- > 0) {
66 total++;
67 if (PageReserved(mem_map+i))
68 reserved++;
69 else if (PageSwapCache(mem_map+i))
70 cached++;
71 else if (page_count(mem_map+i))
72 shared += page_count(mem_map+i) - 1;
73 }
74 printk("%d pages of RAM\n",total);
75 printk("%d reserved pages\n",reserved);
76 printk("%d pages shared\n",shared);
77 printk("%d pages swap cached\n",cached);
78}
79
80static void set_pte_phys(unsigned long addr, unsigned long phys, pgprot_t prot)
81{
82 pgd_t *pgd;
26ff6c11 83 pud_t *pud;
1da177e4
LT
84 pmd_t *pmd;
85 pte_t *pte;
86
87 pgd = swapper_pg_dir + pgd_index(addr);
88 if (pgd_none(*pgd)) {
89 pgd_ERROR(*pgd);
90 return;
91 }
92
26ff6c11
PM
93 pud = pud_offset(pgd, addr);
94 if (pud_none(*pud)) {
95 pmd = (pmd_t *)get_zeroed_page(GFP_ATOMIC);
96 set_pud(pud, __pud(__pa(pmd) | _KERNPG_TABLE | _PAGE_USER));
97 if (pmd != pmd_offset(pud, 0)) {
98 pud_ERROR(*pud);
99 return;
100 }
101 }
102
103 pmd = pmd_offset(pud, addr);
1da177e4
LT
104 if (pmd_none(*pmd)) {
105 pte = (pte_t *)get_zeroed_page(GFP_ATOMIC);
106 set_pmd(pmd, __pmd(__pa(pte) | _KERNPG_TABLE | _PAGE_USER));
107 if (pte != pte_offset_kernel(pmd, 0)) {
108 pmd_ERROR(*pmd);
109 return;
110 }
111 }
112
113 pte = pte_offset_kernel(pmd, addr);
114 if (!pte_none(*pte)) {
115 pte_ERROR(*pte);
116 return;
117 }
118
119 set_pte(pte, pfn_pte(phys >> PAGE_SHIFT, prot));
120
121 __flush_tlb_page(get_asid(), addr);
122}
123
124/*
125 * As a performance optimization, other platforms preserve the fixmap mapping
126 * across a context switch, we don't presently do this, but this could be done
127 * in a similar fashion as to the wired TLB interface that sh64 uses (by way
128 * of the memorry mapped UTLB configuration) -- this unfortunately forces us to
129 * give up a TLB entry for each mapping we want to preserve. While this may be
130 * viable for a small number of fixmaps, it's not particularly useful for
131 * everything and needs to be carefully evaluated. (ie, we may want this for
132 * the vsyscall page).
133 *
134 * XXX: Perhaps add a _PAGE_WIRED flag or something similar that we can pass
135 * in at __set_fixmap() time to determine the appropriate behavior to follow.
136 *
137 * -- PFM.
138 */
139void __set_fixmap(enum fixed_addresses idx, unsigned long phys, pgprot_t prot)
140{
141 unsigned long address = __fix_to_virt(idx);
142
143 if (idx >= __end_of_fixed_addresses) {
144 BUG();
145 return;
146 }
147
148 set_pte_phys(address, phys, prot);
149}
150
151/* References to section boundaries */
152
153extern char _text, _etext, _edata, __bss_start, _end;
154extern char __init_begin, __init_end;
155
156/*
157 * paging_init() sets up the page tables
158 *
159 * This routines also unmaps the page at virtual kernel address 0, so
160 * that we can trap those pesky NULL-reference errors in the kernel.
161 */
162void __init paging_init(void)
163{
164 unsigned long zones_size[MAX_NR_ZONES] = { 0, };
165
166 /*
167 * Setup some defaults for the zone sizes.. these should be safe
168 * regardless of distcontiguous memory or MMU settings.
169 */
170 zones_size[ZONE_DMA] = 0 >> PAGE_SHIFT;
171 zones_size[ZONE_NORMAL] = __MEMORY_SIZE >> PAGE_SHIFT;
172#ifdef CONFIG_HIGHMEM
173 zones_size[ZONE_HIGHMEM] = 0 >> PAGE_SHIFT;
174#endif
175
176#ifdef CONFIG_MMU
177 /*
178 * If we have an MMU, and want to be using it .. we need to adjust
179 * the zone sizes accordingly, in addition to turning it on.
180 */
181 {
182 unsigned long max_dma, low, start_pfn;
183 pgd_t *pg_dir;
184 int i;
185
186 /* We don't need kernel mapping as hardware support that. */
187 pg_dir = swapper_pg_dir;
188
189 for (i = 0; i < PTRS_PER_PGD; i++)
190 pgd_val(pg_dir[i]) = 0;
191
192 /* Turn on the MMU */
193 enable_mmu();
194
195 /* Fixup the zone sizes */
196 start_pfn = START_PFN;
197 max_dma = virt_to_phys((char *)MAX_DMA_ADDRESS) >> PAGE_SHIFT;
198 low = MAX_LOW_PFN;
199
200 if (low < max_dma) {
201 zones_size[ZONE_DMA] = low - start_pfn;
202 zones_size[ZONE_NORMAL] = 0;
203 } else {
204 zones_size[ZONE_DMA] = max_dma - start_pfn;
205 zones_size[ZONE_NORMAL] = low - max_dma;
206 }
207 }
208
209#elif defined(CONFIG_CPU_SH3) || defined(CONFIG_CPU_SH4)
210 /*
211 * If we don't have CONFIG_MMU set and the processor in question
212 * still has an MMU, care needs to be taken to make sure it doesn't
213 * stay on.. Since the boot loader could have potentially already
214 * turned it on, and we clearly don't want it, we simply turn it off.
215 *
216 * We don't need to do anything special for the zone sizes, since the
217 * default values that were already configured up above should be
218 * satisfactory.
219 */
220 disable_mmu();
221#endif
222 NODE_DATA(0)->node_mem_map = NULL;
223 free_area_init_node(0, NODE_DATA(0), zones_size, __MEMORY_START >> PAGE_SHIFT, 0);
1da177e4
LT
224}
225
226void __init mem_init(void)
227{
228 extern unsigned long empty_zero_page[1024];
229 int codesize, reservedpages, datasize, initsize;
230 int tmp;
231 extern unsigned long memory_start;
232
233#ifdef CONFIG_MMU
234 high_memory = (void *)__va(MAX_LOW_PFN * PAGE_SIZE);
235#else
236 extern unsigned long memory_end;
237
238 high_memory = (void *)(memory_end & PAGE_MASK);
239#endif
240
241 max_mapnr = num_physpages = MAP_NR(high_memory) - MAP_NR(memory_start);
242
243 /* clear the zero-page */
244 memset(empty_zero_page, 0, PAGE_SIZE);
245 __flush_wback_region(empty_zero_page, PAGE_SIZE);
246
65463b73 247 /*
1da177e4
LT
248 * Setup wrappers for copy/clear_page(), these will get overridden
249 * later in the boot process if a better method is available.
250 */
e96636cc 251#ifdef CONFIG_MMU
1da177e4
LT
252 copy_page = copy_page_slow;
253 clear_page = clear_page_slow;
e96636cc
YS
254#else
255 copy_page = copy_page_nommu;
256 clear_page = clear_page_nommu;
257#endif
1da177e4
LT
258
259 /* this will put all low memory onto the freelists */
260 totalram_pages += free_all_bootmem_node(NODE_DATA(0));
1da177e4
LT
261 reservedpages = 0;
262 for (tmp = 0; tmp < num_physpages; tmp++)
263 /*
264 * Only count reserved RAM pages
265 */
266 if (PageReserved(mem_map+tmp))
267 reservedpages++;
268
269 codesize = (unsigned long) &_etext - (unsigned long) &_text;
270 datasize = (unsigned long) &_edata - (unsigned long) &_etext;
271 initsize = (unsigned long) &__init_end - (unsigned long) &__init_begin;
272
273 printk("Memory: %luk/%luk available (%dk kernel code, %dk reserved, %dk data, %dk init)\n",
274 (unsigned long) nr_free_pages() << (PAGE_SHIFT-10),
275 max_mapnr << (PAGE_SHIFT-10),
276 codesize >> 10,
277 reservedpages << (PAGE_SHIFT-10),
278 datasize >> 10,
279 initsize >> 10);
280
281 p3_cache_init();
282}
283
284void free_initmem(void)
285{
286 unsigned long addr;
65463b73 287
1da177e4
LT
288 addr = (unsigned long)(&__init_begin);
289 for (; addr < (unsigned long)(&__init_end); addr += PAGE_SIZE) {
290 ClearPageReserved(virt_to_page(addr));
7835e98b 291 init_page_count(virt_to_page(addr));
1da177e4
LT
292 free_page(addr);
293 totalram_pages++;
294 }
295 printk ("Freeing unused kernel memory: %dk freed\n", (&__init_end - &__init_begin) >> 10);
296}
297
298#ifdef CONFIG_BLK_DEV_INITRD
299void free_initrd_mem(unsigned long start, unsigned long end)
300{
301 unsigned long p;
302 for (p = start; p < end; p += PAGE_SIZE) {
303 ClearPageReserved(virt_to_page(p));
7835e98b 304 init_page_count(virt_to_page(p));
1da177e4
LT
305 free_page(p);
306 totalram_pages++;
307 }
308 printk ("Freeing initrd memory: %ldk freed\n", (end - start) >> 10);
309}
310#endif
311