blob: 9113012240789469e20f7360583e861d4d3c3c65 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Uwe Zeisbergerf30c2262006-10-03 23:01:26 +02003 * linux/arch/m68k/mm/motorola.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 * Routines specific to the Motorola MMU, originally from:
6 * linux/arch/m68k/init.c
7 * which are Copyright (C) 1995 Hamish Macdonald
8 *
9 * Moved 8/20/1999 Sam Creasey
10 */
11
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/module.h>
13#include <linux/signal.h>
14#include <linux/sched.h>
15#include <linux/mm.h>
16#include <linux/swap.h>
17#include <linux/kernel.h>
18#include <linux/string.h>
19#include <linux/types.h>
20#include <linux/init.h>
Mike Rapoport1008a112018-07-04 09:28:16 +030021#include <linux/memblock.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
24#include <asm/setup.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080025#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <asm/page.h>
27#include <asm/pgalloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <asm/machdep.h>
29#include <asm/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#ifdef CONFIG_ATARI
31#include <asm/atari_stram.h>
32#endif
Geert Uytterhoevenc85627f2008-12-21 12:03:37 +010033#include <asm/sections.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
35#undef DEBUG
36
37#ifndef mm_cachebits
38/*
39 * Bits to add to page descriptors for "normal" caching mode.
40 * For 68020/030 this is 0.
41 * For 68040, this is _PAGE_CACHE040 (cachable, copyback)
42 */
43unsigned long mm_cachebits;
44EXPORT_SYMBOL(mm_cachebits);
45#endif
46
Mike Rapoport1bcdc682020-06-08 21:32:56 -070047/* Prior to calling these routines, the page should have been flushed
48 * from both the cache and ATC, or the CPU might not notice that the
49 * cache setting for the page has been changed. -jskov
50 */
51static inline void nocache_page(void *vaddr)
52{
53 unsigned long addr = (unsigned long)vaddr;
54
55 if (CPU_IS_040_OR_060) {
Mike Rapoporte05c7b12020-06-08 21:33:05 -070056 pte_t *ptep = virt_to_kpte(addr);
Mike Rapoport1bcdc682020-06-08 21:32:56 -070057
Mike Rapoport1bcdc682020-06-08 21:32:56 -070058 *ptep = pte_mknocache(*ptep);
59 }
60}
61
62static inline void cache_page(void *vaddr)
63{
64 unsigned long addr = (unsigned long)vaddr;
65
66 if (CPU_IS_040_OR_060) {
Mike Rapoporte05c7b12020-06-08 21:33:05 -070067 pte_t *ptep = virt_to_kpte(addr);
Mike Rapoport1bcdc682020-06-08 21:32:56 -070068
Mike Rapoport1bcdc682020-06-08 21:32:56 -070069 *ptep = pte_mkcache(*ptep);
70 }
71}
Peter Zijlstra13076a22020-01-31 13:45:34 +010072
73/*
74 * Motorola 680x0 user's manual recommends using uncached memory for address
75 * translation tables.
76 *
77 * Seeing how the MMU can be external on (some of) these chips, that seems like
78 * a very important recommendation to follow. Provide some helpers to combat
79 * 'variation' amongst the users of this.
80 */
81
82void mmu_page_ctor(void *page)
83{
84 __flush_page_to_ram(page);
85 flush_tlb_kernel_page(page);
86 nocache_page(page);
87}
88
89void mmu_page_dtor(void *page)
90{
91 cache_page(page);
92}
93
Peter Zijlstra5ad272a2020-01-31 13:45:35 +010094/* ++andreas: {get,free}_pointer_table rewritten to use unused fields from
95 struct page instead of separately kmalloced struct. Stolen from
96 arch/sparc/mm/srmmu.c ... */
97
98typedef struct list_head ptable_desc;
Peter Zijlstra0e071ee2020-01-31 13:45:39 +010099
100static struct list_head ptable_list[2] = {
101 LIST_HEAD_INIT(ptable_list[0]),
102 LIST_HEAD_INIT(ptable_list[1]),
103};
Peter Zijlstra5ad272a2020-01-31 13:45:35 +0100104
105#define PD_PTABLE(page) ((ptable_desc *)&(virt_to_page(page)->lru))
106#define PD_PAGE(ptable) (list_entry(ptable, struct page, lru))
Peter Zijlstra0e071ee2020-01-31 13:45:39 +0100107#define PD_MARKBITS(dp) (*(unsigned int *)&PD_PAGE(dp)->index)
Peter Zijlstra5ad272a2020-01-31 13:45:35 +0100108
Peter Zijlstra0e071ee2020-01-31 13:45:39 +0100109static const int ptable_shift[2] = {
110 7+2, /* PGD, PMD */
111 6+2, /* PTE */
112};
Peter Zijlstra5ad272a2020-01-31 13:45:35 +0100113
Peter Zijlstra0e071ee2020-01-31 13:45:39 +0100114#define ptable_size(type) (1U << ptable_shift[type])
115#define ptable_mask(type) ((1U << (PAGE_SIZE / ptable_size(type))) - 1)
116
117void __init init_pointer_table(void *table, int type)
Peter Zijlstra5ad272a2020-01-31 13:45:35 +0100118{
119 ptable_desc *dp;
Peter Zijlstra0e071ee2020-01-31 13:45:39 +0100120 unsigned long ptable = (unsigned long)table;
Peter Zijlstra5ad272a2020-01-31 13:45:35 +0100121 unsigned long page = ptable & PAGE_MASK;
Peter Zijlstra0e071ee2020-01-31 13:45:39 +0100122 unsigned int mask = 1U << ((ptable - page)/ptable_size(type));
Peter Zijlstra5ad272a2020-01-31 13:45:35 +0100123
124 dp = PD_PTABLE(page);
125 if (!(PD_MARKBITS(dp) & mask)) {
Peter Zijlstra0e071ee2020-01-31 13:45:39 +0100126 PD_MARKBITS(dp) = ptable_mask(type);
127 list_add(dp, &ptable_list[type]);
Peter Zijlstra5ad272a2020-01-31 13:45:35 +0100128 }
129
130 PD_MARKBITS(dp) &= ~mask;
131 pr_debug("init_pointer_table: %lx, %x\n", ptable, PD_MARKBITS(dp));
132
133 /* unreserve the page so it's possible to free that page */
134 __ClearPageReserved(PD_PAGE(dp));
135 init_page_count(PD_PAGE(dp));
136
137 return;
138}
139
Peter Zijlstra0e071ee2020-01-31 13:45:39 +0100140void *get_pointer_table(int type)
Peter Zijlstra5ad272a2020-01-31 13:45:35 +0100141{
Peter Zijlstra0e071ee2020-01-31 13:45:39 +0100142 ptable_desc *dp = ptable_list[type].next;
143 unsigned int mask = list_empty(&ptable_list[type]) ? 0 : PD_MARKBITS(dp);
144 unsigned int tmp, off;
Peter Zijlstra5ad272a2020-01-31 13:45:35 +0100145
146 /*
147 * For a pointer table for a user process address space, a
148 * table is taken from a page allocated for the purpose. Each
149 * page can hold 8 pointer tables. The page is remapped in
150 * virtual address space to be noncacheable.
151 */
152 if (mask == 0) {
153 void *page;
154 ptable_desc *new;
155
156 if (!(page = (void *)get_zeroed_page(GFP_KERNEL)))
157 return NULL;
158
Peter Zijlstra0e071ee2020-01-31 13:45:39 +0100159 if (type == TABLE_PTE) {
160 /*
161 * m68k doesn't have SPLIT_PTE_PTLOCKS for not having
162 * SMP.
163 */
164 pgtable_pte_page_ctor(virt_to_page(page));
165 }
166
Peter Zijlstra5ad272a2020-01-31 13:45:35 +0100167 mmu_page_ctor(page);
168
169 new = PD_PTABLE(page);
Peter Zijlstra0e071ee2020-01-31 13:45:39 +0100170 PD_MARKBITS(new) = ptable_mask(type) - 1;
Peter Zijlstra5ad272a2020-01-31 13:45:35 +0100171 list_add_tail(new, dp);
172
173 return (pmd_t *)page;
174 }
175
Peter Zijlstra0e071ee2020-01-31 13:45:39 +0100176 for (tmp = 1, off = 0; (mask & tmp) == 0; tmp <<= 1, off += ptable_size(type))
Peter Zijlstra5ad272a2020-01-31 13:45:35 +0100177 ;
178 PD_MARKBITS(dp) = mask & ~tmp;
179 if (!PD_MARKBITS(dp)) {
180 /* move to end of list */
Peter Zijlstra0e071ee2020-01-31 13:45:39 +0100181 list_move_tail(dp, &ptable_list[type]);
Peter Zijlstra5ad272a2020-01-31 13:45:35 +0100182 }
Peter Zijlstra0e071ee2020-01-31 13:45:39 +0100183 return page_address(PD_PAGE(dp)) + off;
Peter Zijlstra5ad272a2020-01-31 13:45:35 +0100184}
185
Peter Zijlstra0e071ee2020-01-31 13:45:39 +0100186int free_pointer_table(void *table, int type)
Peter Zijlstra5ad272a2020-01-31 13:45:35 +0100187{
188 ptable_desc *dp;
Peter Zijlstra0e071ee2020-01-31 13:45:39 +0100189 unsigned long ptable = (unsigned long)table;
190 unsigned long page = ptable & PAGE_MASK;
191 unsigned int mask = 1U << ((ptable - page)/ptable_size(type));
Peter Zijlstra5ad272a2020-01-31 13:45:35 +0100192
193 dp = PD_PTABLE(page);
194 if (PD_MARKBITS (dp) & mask)
195 panic ("table already free!");
196
197 PD_MARKBITS (dp) |= mask;
198
Peter Zijlstra0e071ee2020-01-31 13:45:39 +0100199 if (PD_MARKBITS(dp) == ptable_mask(type)) {
Peter Zijlstra5ad272a2020-01-31 13:45:35 +0100200 /* all tables in page are free, free page */
201 list_del(dp);
202 mmu_page_dtor((void *)page);
Peter Zijlstra0e071ee2020-01-31 13:45:39 +0100203 if (type == TABLE_PTE)
204 pgtable_pte_page_dtor(virt_to_page(page));
Peter Zijlstra5ad272a2020-01-31 13:45:35 +0100205 free_page (page);
206 return 1;
Peter Zijlstra0e071ee2020-01-31 13:45:39 +0100207 } else if (ptable_list[type].next != dp) {
Peter Zijlstra5ad272a2020-01-31 13:45:35 +0100208 /*
209 * move this descriptor to the front of the list, since
210 * it has one or more free tables.
211 */
Peter Zijlstra0e071ee2020-01-31 13:45:39 +0100212 list_move(dp, &ptable_list[type]);
Peter Zijlstra5ad272a2020-01-31 13:45:35 +0100213 }
214 return 0;
215}
216
Roman Zippel12d810c2007-05-31 00:40:54 -0700217/* size of memory already mapped in head.S */
Andreas Schwab486df8b2014-04-24 12:24:48 +0200218extern __initdata unsigned long m68k_init_mapped_size;
Roman Zippel12d810c2007-05-31 00:40:54 -0700219
220extern unsigned long availmem;
221
Peter Zijlstraef9285f2020-01-31 13:45:37 +0100222static pte_t *last_pte_table __initdata = NULL;
223
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224static pte_t * __init kernel_page_table(void)
225{
Peter Zijlstraef9285f2020-01-31 13:45:37 +0100226 pte_t *pte_table = last_pte_table;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
Geert Uytterhoeven41f1bf32020-08-26 15:01:03 +0200228 if (PAGE_ALIGNED(last_pte_table)) {
Geert Uytterhoeven7e158822020-08-26 15:04:44 +0200229 pte_table = memblock_alloc_low(PAGE_SIZE, PAGE_SIZE);
Peter Zijlstraef9285f2020-01-31 13:45:37 +0100230 if (!pte_table) {
231 panic("%s: Failed to allocate %lu bytes align=%lx\n",
232 __func__, PAGE_SIZE, PAGE_SIZE);
233 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
Peter Zijlstraef9285f2020-01-31 13:45:37 +0100235 clear_page(pte_table);
236 mmu_page_ctor(pte_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
Peter Zijlstraef9285f2020-01-31 13:45:37 +0100238 last_pte_table = pte_table;
239 }
240
241 last_pte_table += PTRS_PER_PTE;
242
243 return pte_table;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244}
245
Peter Zijlstraef9285f2020-01-31 13:45:37 +0100246static pmd_t *last_pmd_table __initdata = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
248static pmd_t * __init kernel_ptr_table(void)
249{
Peter Zijlstraef9285f2020-01-31 13:45:37 +0100250 if (!last_pmd_table) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 unsigned long pmd, last;
252 int i;
253
254 /* Find the last ptr table that was used in head.S and
255 * reuse the remaining space in that page for further
256 * ptr tables.
257 */
258 last = (unsigned long)kernel_pg_dir;
259 for (i = 0; i < PTRS_PER_PGD; i++) {
Mike Rapoport60e50f32019-12-04 16:53:59 -0800260 pud_t *pud = (pud_t *)(&kernel_pg_dir[i]);
261
262 if (!pud_present(*pud))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 continue;
Mike Rapoport60e50f32019-12-04 16:53:59 -0800264 pmd = pgd_page_vaddr(kernel_pg_dir[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 if (pmd > last)
266 last = pmd;
267 }
268
Peter Zijlstraef9285f2020-01-31 13:45:37 +0100269 last_pmd_table = (pmd_t *)last;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270#ifdef DEBUG
Peter Zijlstraef9285f2020-01-31 13:45:37 +0100271 printk("kernel_ptr_init: %p\n", last_pmd_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272#endif
273 }
274
Peter Zijlstraef9285f2020-01-31 13:45:37 +0100275 last_pmd_table += PTRS_PER_PMD;
Geert Uytterhoeven41f1bf32020-08-26 15:01:03 +0200276 if (PAGE_ALIGNED(last_pmd_table)) {
Geert Uytterhoeven7e158822020-08-26 15:04:44 +0200277 last_pmd_table = memblock_alloc_low(PAGE_SIZE, PAGE_SIZE);
Peter Zijlstraef9285f2020-01-31 13:45:37 +0100278 if (!last_pmd_table)
Mike Rapoport8a7f97b2019-03-11 23:30:31 -0700279 panic("%s: Failed to allocate %lu bytes align=%lx\n",
280 __func__, PAGE_SIZE, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
Peter Zijlstraef9285f2020-01-31 13:45:37 +0100282 clear_page(last_pmd_table);
283 mmu_page_ctor(last_pmd_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 }
285
Peter Zijlstraef9285f2020-01-31 13:45:37 +0100286 return last_pmd_table;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287}
288
Roman Zippel12d810c2007-05-31 00:40:54 -0700289static void __init map_node(int node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290{
Roman Zippel12d810c2007-05-31 00:40:54 -0700291 unsigned long physaddr, virtaddr, size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 pgd_t *pgd_dir;
Mike Rapoport60e50f32019-12-04 16:53:59 -0800293 p4d_t *p4d_dir;
294 pud_t *pud_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 pmd_t *pmd_dir;
296 pte_t *pte_dir;
297
Roman Zippel12d810c2007-05-31 00:40:54 -0700298 size = m68k_memory[node].size;
299 physaddr = m68k_memory[node].addr;
300 virtaddr = (unsigned long)phys_to_virt(physaddr);
301 physaddr |= m68k_supervisor_cachemode |
302 _PAGE_PRESENT | _PAGE_ACCESSED | _PAGE_DIRTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 if (CPU_IS_040_OR_060)
304 physaddr |= _PAGE_GLOBAL040;
305
306 while (size > 0) {
307#ifdef DEBUG
Peter Zijlstraef22d8a2020-01-31 13:45:36 +0100308 if (!(virtaddr & (PMD_SIZE-1)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 printk ("\npa=%#lx va=%#lx ", physaddr & PAGE_MASK,
310 virtaddr);
311#endif
312 pgd_dir = pgd_offset_k(virtaddr);
313 if (virtaddr && CPU_IS_020_OR_030) {
Peter Zijlstraef22d8a2020-01-31 13:45:36 +0100314 if (!(virtaddr & (PGDIR_SIZE-1)) &&
315 size >= PGDIR_SIZE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316#ifdef DEBUG
317 printk ("[very early term]");
318#endif
319 pgd_val(*pgd_dir) = physaddr;
Peter Zijlstraef22d8a2020-01-31 13:45:36 +0100320 size -= PGDIR_SIZE;
321 virtaddr += PGDIR_SIZE;
322 physaddr += PGDIR_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 continue;
324 }
325 }
Mike Rapoport60e50f32019-12-04 16:53:59 -0800326 p4d_dir = p4d_offset(pgd_dir, virtaddr);
327 pud_dir = pud_offset(p4d_dir, virtaddr);
328 if (!pud_present(*pud_dir)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 pmd_dir = kernel_ptr_table();
330#ifdef DEBUG
331 printk ("[new pointer %p]", pmd_dir);
332#endif
Mike Rapoport60e50f32019-12-04 16:53:59 -0800333 pud_set(pud_dir, pmd_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 } else
Mike Rapoport60e50f32019-12-04 16:53:59 -0800335 pmd_dir = pmd_offset(pud_dir, virtaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
337 if (CPU_IS_020_OR_030) {
338 if (virtaddr) {
339#ifdef DEBUG
340 printk ("[early term]");
341#endif
Peter Zijlstraef22d8a2020-01-31 13:45:36 +0100342 pmd_val(*pmd_dir) = physaddr;
343 physaddr += PMD_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 } else {
345 int i;
346#ifdef DEBUG
347 printk ("[zero map]");
348#endif
Peter Zijlstraef9285f2020-01-31 13:45:37 +0100349 pte_dir = kernel_page_table();
Peter Zijlstraef22d8a2020-01-31 13:45:36 +0100350 pmd_set(pmd_dir, pte_dir);
351
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 pte_val(*pte_dir++) = 0;
353 physaddr += PAGE_SIZE;
Peter Zijlstraef22d8a2020-01-31 13:45:36 +0100354 for (i = 1; i < PTRS_PER_PTE; physaddr += PAGE_SIZE, i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 pte_val(*pte_dir++) = physaddr;
356 }
Peter Zijlstraef22d8a2020-01-31 13:45:36 +0100357 size -= PMD_SIZE;
358 virtaddr += PMD_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 } else {
360 if (!pmd_present(*pmd_dir)) {
361#ifdef DEBUG
362 printk ("[new table]");
363#endif
364 pte_dir = kernel_page_table();
365 pmd_set(pmd_dir, pte_dir);
366 }
367 pte_dir = pte_offset_kernel(pmd_dir, virtaddr);
368
369 if (virtaddr) {
370 if (!pte_present(*pte_dir))
371 pte_val(*pte_dir) = physaddr;
372 } else
373 pte_val(*pte_dir) = 0;
374 size -= PAGE_SIZE;
375 virtaddr += PAGE_SIZE;
376 physaddr += PAGE_SIZE;
377 }
378
379 }
380#ifdef DEBUG
381 printk("\n");
382#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383}
384
385/*
Anshuman Khandual6d0b9222022-07-11 12:35:55 +0530386 * Alternate definitions that are compile time constants, for
387 * initializing protection_map. The cachebits are fixed later.
388 */
389#define PAGE_NONE_C __pgprot(_PAGE_PROTNONE | _PAGE_ACCESSED)
390#define PAGE_SHARED_C __pgprot(_PAGE_PRESENT | _PAGE_ACCESSED)
391#define PAGE_COPY_C __pgprot(_PAGE_PRESENT | _PAGE_RONLY | _PAGE_ACCESSED)
392#define PAGE_READONLY_C __pgprot(_PAGE_PRESENT | _PAGE_RONLY | _PAGE_ACCESSED)
393
394static pgprot_t protection_map[16] __ro_after_init = {
395 [VM_NONE] = PAGE_NONE_C,
396 [VM_READ] = PAGE_READONLY_C,
397 [VM_WRITE] = PAGE_COPY_C,
398 [VM_WRITE | VM_READ] = PAGE_COPY_C,
399 [VM_EXEC] = PAGE_READONLY_C,
400 [VM_EXEC | VM_READ] = PAGE_READONLY_C,
401 [VM_EXEC | VM_WRITE] = PAGE_COPY_C,
402 [VM_EXEC | VM_WRITE | VM_READ] = PAGE_COPY_C,
403 [VM_SHARED] = PAGE_NONE_C,
404 [VM_SHARED | VM_READ] = PAGE_READONLY_C,
405 [VM_SHARED | VM_WRITE] = PAGE_SHARED_C,
406 [VM_SHARED | VM_WRITE | VM_READ] = PAGE_SHARED_C,
407 [VM_SHARED | VM_EXEC] = PAGE_READONLY_C,
408 [VM_SHARED | VM_EXEC | VM_READ] = PAGE_READONLY_C,
409 [VM_SHARED | VM_EXEC | VM_WRITE] = PAGE_SHARED_C,
410 [VM_SHARED | VM_EXEC | VM_WRITE | VM_READ] = PAGE_SHARED_C
411};
412DECLARE_VM_GET_PAGE_PROT
413
414/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 * paging_init() continues the virtual memory environment setup which
416 * was begun by the code in arch/head.S.
417 */
418void __init paging_init(void)
419{
Mike Rapoport5d2ee1a2020-06-03 15:57:32 -0700420 unsigned long max_zone_pfn[MAX_NR_ZONES] = { 0, };
Roman Zippel12d810c2007-05-31 00:40:54 -0700421 unsigned long min_addr, max_addr;
Mike Rapoport1008a112018-07-04 09:28:16 +0300422 unsigned long addr;
Roman Zippel12d810c2007-05-31 00:40:54 -0700423 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
425#ifdef DEBUG
Geert Uytterhoevenfb425d02007-08-22 14:01:30 -0700426 printk ("start of paging_init (%p, %lx)\n", kernel_pg_dir, availmem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427#endif
428
429 /* Fix the cache mode in the page descriptors for the 680[46]0. */
430 if (CPU_IS_040_OR_060) {
431 int i;
432#ifndef mm_cachebits
433 mm_cachebits = _PAGE_CACHE040;
434#endif
435 for (i = 0; i < 16; i++)
436 pgprot_val(protection_map[i]) |= _PAGE_CACHE040;
437 }
438
Roman Zippel12d810c2007-05-31 00:40:54 -0700439 min_addr = m68k_memory[0].addr;
Kars de Jong0d9fad92023-02-23 12:23:49 +0100440 max_addr = min_addr + m68k_memory[0].size - 1;
David Hildenbrand952eea92021-11-05 13:44:49 -0700441 memblock_add_node(m68k_memory[0].addr, m68k_memory[0].size, 0,
442 MEMBLOCK_NONE);
Roman Zippel12d810c2007-05-31 00:40:54 -0700443 for (i = 1; i < m68k_num_memory;) {
444 if (m68k_memory[i].addr < min_addr) {
445 printk("Ignoring memory chunk at 0x%lx:0x%lx before the first chunk\n",
446 m68k_memory[i].addr, m68k_memory[i].size);
447 printk("Fix your bootloader or use a memfile to make use of this area!\n");
448 m68k_num_memory--;
449 memmove(m68k_memory + i, m68k_memory + i + 1,
Geert Uytterhoeven79930082013-10-02 21:50:56 +0200450 (m68k_num_memory - i) * sizeof(struct m68k_mem_info));
Roman Zippel12d810c2007-05-31 00:40:54 -0700451 continue;
452 }
David Hildenbrand952eea92021-11-05 13:44:49 -0700453 memblock_add_node(m68k_memory[i].addr, m68k_memory[i].size, i,
454 MEMBLOCK_NONE);
Kars de Jong0d9fad92023-02-23 12:23:49 +0100455 addr = m68k_memory[i].addr + m68k_memory[i].size - 1;
Roman Zippel12d810c2007-05-31 00:40:54 -0700456 if (addr > max_addr)
457 max_addr = addr;
458 i++;
459 }
460 m68k_memoffset = min_addr - PAGE_OFFSET;
Kars de Jong0d9fad92023-02-23 12:23:49 +0100461 m68k_virt_to_node_shift = fls(max_addr - min_addr) - 6;
Roman Zippel12d810c2007-05-31 00:40:54 -0700462
Roman Zippelfbe9c962007-05-31 00:40:50 -0700463 module_fixup(NULL, __start_fixup, __stop_fixup);
464 flush_icache();
465
Kars de Jong0d9fad92023-02-23 12:23:49 +0100466 high_memory = phys_to_virt(max_addr) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
Roman Zippel12d810c2007-05-31 00:40:54 -0700468 min_low_pfn = availmem >> PAGE_SHIFT;
Kars de Jong0d9fad92023-02-23 12:23:49 +0100469 max_pfn = max_low_pfn = (max_addr >> PAGE_SHIFT) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
Mike Rapoport1008a112018-07-04 09:28:16 +0300471 /* Reserve kernel text/data/bss and the memory allocated in head.S */
472 memblock_reserve(m68k_memory[0].addr, availmem - m68k_memory[0].addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
Roman Zippel12d810c2007-05-31 00:40:54 -0700474 /*
475 * Map the physical memory available into the kernel virtual
Mike Rapoport1008a112018-07-04 09:28:16 +0300476 * address space. Make sure memblock will not try to allocate
477 * pages beyond the memory we already mapped in head.S
Roman Zippel12d810c2007-05-31 00:40:54 -0700478 */
Mike Rapoport1008a112018-07-04 09:28:16 +0300479 memblock_set_bottom_up(true);
Roman Zippel12d810c2007-05-31 00:40:54 -0700480
Mike Rapoport1008a112018-07-04 09:28:16 +0300481 for (i = 0; i < m68k_num_memory; i++) {
482 m68k_setup_node(i);
Roman Zippel12d810c2007-05-31 00:40:54 -0700483 map_node(i);
Mike Rapoport1008a112018-07-04 09:28:16 +0300484 }
Roman Zippel12d810c2007-05-31 00:40:54 -0700485
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 flush_tlb_all();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
Finn Thain376e3fd2021-11-19 09:29:38 +1100488 early_memtest(min_addr, max_addr);
489
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 /*
491 * initialize the bad page table and bad page to point
492 * to a couple of allocated pages
493 */
Mike Rapoport15c3c112018-10-30 15:08:58 -0700494 empty_zero_page = memblock_alloc(PAGE_SIZE, PAGE_SIZE);
Mike Rapoport8a7f97b2019-03-11 23:30:31 -0700495 if (!empty_zero_page)
496 panic("%s: Failed to allocate %lu bytes align=0x%lx\n",
497 __func__, PAGE_SIZE, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
499 /*
500 * Set up SFC/DFC registers
501 */
Christoph Hellwig9fde03482021-09-16 09:04:05 +0200502 set_fc(USER_DATA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
504#ifdef DEBUG
505 printk ("before free_area_init\n");
506#endif
Mike Rapoport5d2ee1a2020-06-03 15:57:32 -0700507 for (i = 0; i < m68k_num_memory; i++)
Michael Schmitz4aac0b482011-04-26 14:51:53 +1200508 if (node_present_pages(i))
509 node_set_state(i, N_NORMAL_MEMORY);
Mike Rapoport5d2ee1a2020-06-03 15:57:32 -0700510
511 max_zone_pfn[ZONE_DMA] = memblock_end_of_DRAM();
512 free_area_init(max_zone_pfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513}