1 // SPDX-License-Identifier: GPL-2.0
3 * linux/arch/m68k/mm/motorola.c
5 * Routines specific to the Motorola MMU, originally from:
6 * linux/arch/m68k/init.c
7 * which are Copyright (C) 1995 Hamish Macdonald
9 * Moved 8/20/1999 Sam Creasey
12 #include <linux/module.h>
13 #include <linux/signal.h>
14 #include <linux/sched.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>
21 #include <linux/memblock.h>
22 #include <linux/gfp.h>
24 #include <asm/setup.h>
25 #include <linux/uaccess.h>
27 #include <asm/pgalloc.h>
28 #include <asm/machdep.h>
32 #include <asm/atari_stram.h>
34 #include <asm/sections.h>
40 * Bits to add to page descriptors for "normal" caching mode.
41 * For 68020/030 this is 0.
42 * For 68040, this is _PAGE_CACHE040 (cachable, copyback)
44 unsigned long mm_cachebits
;
45 EXPORT_SYMBOL(mm_cachebits
);
48 /* size of memory already mapped in head.S */
49 extern __initdata
unsigned long m68k_init_mapped_size
;
51 extern unsigned long availmem
;
53 static pte_t
* __init
kernel_page_table(void)
57 ptablep
= (pte_t
*)memblock_alloc_low(PAGE_SIZE
, PAGE_SIZE
);
59 panic("%s: Failed to allocate %lu bytes align=%lx\n",
60 __func__
, PAGE_SIZE
, PAGE_SIZE
);
63 __flush_page_to_ram(ptablep
);
64 flush_tlb_kernel_page(ptablep
);
65 nocache_page(ptablep
);
70 static pmd_t
*last_pgtable __initdata
= NULL
;
71 pmd_t
*zero_pgtable __initdata
= NULL
;
73 static pmd_t
* __init
kernel_ptr_table(void)
76 unsigned long pmd
, last
;
79 /* Find the last ptr table that was used in head.S and
80 * reuse the remaining space in that page for further
83 last
= (unsigned long)kernel_pg_dir
;
84 for (i
= 0; i
< PTRS_PER_PGD
; i
++) {
85 if (!pgd_present(kernel_pg_dir
[i
]))
87 pmd
= __pgd_page(kernel_pg_dir
[i
]);
92 last_pgtable
= (pmd_t
*)last
;
94 printk("kernel_ptr_init: %p\n", last_pgtable
);
98 last_pgtable
+= PTRS_PER_PMD
;
99 if (((unsigned long)last_pgtable
& ~PAGE_MASK
) == 0) {
100 last_pgtable
= (pmd_t
*)memblock_alloc_low(PAGE_SIZE
,
103 panic("%s: Failed to allocate %lu bytes align=%lx\n",
104 __func__
, PAGE_SIZE
, PAGE_SIZE
);
106 clear_page(last_pgtable
);
107 __flush_page_to_ram(last_pgtable
);
108 flush_tlb_kernel_page(last_pgtable
);
109 nocache_page(last_pgtable
);
115 static void __init
map_node(int node
)
117 #define PTRTREESIZE (256*1024)
118 #define ROOTTREESIZE (32*1024*1024)
119 unsigned long physaddr
, virtaddr
, size
;
124 size
= m68k_memory
[node
].size
;
125 physaddr
= m68k_memory
[node
].addr
;
126 virtaddr
= (unsigned long)phys_to_virt(physaddr
);
127 physaddr
|= m68k_supervisor_cachemode
|
128 _PAGE_PRESENT
| _PAGE_ACCESSED
| _PAGE_DIRTY
;
129 if (CPU_IS_040_OR_060
)
130 physaddr
|= _PAGE_GLOBAL040
;
134 if (!(virtaddr
& (PTRTREESIZE
-1)))
135 printk ("\npa=%#lx va=%#lx ", physaddr
& PAGE_MASK
,
138 pgd_dir
= pgd_offset_k(virtaddr
);
139 if (virtaddr
&& CPU_IS_020_OR_030
) {
140 if (!(virtaddr
& (ROOTTREESIZE
-1)) &&
141 size
>= ROOTTREESIZE
) {
143 printk ("[very early term]");
145 pgd_val(*pgd_dir
) = physaddr
;
146 size
-= ROOTTREESIZE
;
147 virtaddr
+= ROOTTREESIZE
;
148 physaddr
+= ROOTTREESIZE
;
152 if (!pgd_present(*pgd_dir
)) {
153 pmd_dir
= kernel_ptr_table();
155 printk ("[new pointer %p]", pmd_dir
);
157 pgd_set(pgd_dir
, pmd_dir
);
159 pmd_dir
= pmd_offset(pgd_dir
, virtaddr
);
161 if (CPU_IS_020_OR_030
) {
164 printk ("[early term]");
166 pmd_dir
->pmd
[(virtaddr
/PTRTREESIZE
) & 15] = physaddr
;
167 physaddr
+= PTRTREESIZE
;
171 printk ("[zero map]");
173 zero_pgtable
= kernel_ptr_table();
174 pte_dir
= (pte_t
*)zero_pgtable
;
175 pmd_dir
->pmd
[0] = virt_to_phys(pte_dir
) |
176 _PAGE_TABLE
| _PAGE_ACCESSED
;
177 pte_val(*pte_dir
++) = 0;
178 physaddr
+= PAGE_SIZE
;
179 for (i
= 1; i
< 64; physaddr
+= PAGE_SIZE
, i
++)
180 pte_val(*pte_dir
++) = physaddr
;
183 virtaddr
+= PTRTREESIZE
;
185 if (!pmd_present(*pmd_dir
)) {
187 printk ("[new table]");
189 pte_dir
= kernel_page_table();
190 pmd_set(pmd_dir
, pte_dir
);
192 pte_dir
= pte_offset_kernel(pmd_dir
, virtaddr
);
195 if (!pte_present(*pte_dir
))
196 pte_val(*pte_dir
) = physaddr
;
198 pte_val(*pte_dir
) = 0;
200 virtaddr
+= PAGE_SIZE
;
201 physaddr
+= PAGE_SIZE
;
211 * paging_init() continues the virtual memory environment setup which
212 * was begun by the code in arch/head.S.
214 void __init
paging_init(void)
216 unsigned long zones_size
[MAX_NR_ZONES
] = { 0, };
217 unsigned long min_addr
, max_addr
;
222 printk ("start of paging_init (%p, %lx)\n", kernel_pg_dir
, availmem
);
225 /* Fix the cache mode in the page descriptors for the 680[46]0. */
226 if (CPU_IS_040_OR_060
) {
229 mm_cachebits
= _PAGE_CACHE040
;
231 for (i
= 0; i
< 16; i
++)
232 pgprot_val(protection_map
[i
]) |= _PAGE_CACHE040
;
235 min_addr
= m68k_memory
[0].addr
;
236 max_addr
= min_addr
+ m68k_memory
[0].size
;
237 memblock_add(m68k_memory
[0].addr
, m68k_memory
[0].size
);
238 for (i
= 1; i
< m68k_num_memory
;) {
239 if (m68k_memory
[i
].addr
< min_addr
) {
240 printk("Ignoring memory chunk at 0x%lx:0x%lx before the first chunk\n",
241 m68k_memory
[i
].addr
, m68k_memory
[i
].size
);
242 printk("Fix your bootloader or use a memfile to make use of this area!\n");
244 memmove(m68k_memory
+ i
, m68k_memory
+ i
+ 1,
245 (m68k_num_memory
- i
) * sizeof(struct m68k_mem_info
));
248 memblock_add(m68k_memory
[i
].addr
, m68k_memory
[i
].size
);
249 addr
= m68k_memory
[i
].addr
+ m68k_memory
[i
].size
;
254 m68k_memoffset
= min_addr
- PAGE_OFFSET
;
255 m68k_virt_to_node_shift
= fls(max_addr
- min_addr
- 1) - 6;
257 module_fixup(NULL
, __start_fixup
, __stop_fixup
);
260 high_memory
= phys_to_virt(max_addr
);
262 min_low_pfn
= availmem
>> PAGE_SHIFT
;
263 max_pfn
= max_low_pfn
= max_addr
>> PAGE_SHIFT
;
265 /* Reserve kernel text/data/bss and the memory allocated in head.S */
266 memblock_reserve(m68k_memory
[0].addr
, availmem
- m68k_memory
[0].addr
);
269 * Map the physical memory available into the kernel virtual
270 * address space. Make sure memblock will not try to allocate
271 * pages beyond the memory we already mapped in head.S
273 memblock_set_bottom_up(true);
275 for (i
= 0; i
< m68k_num_memory
; i
++) {
283 * initialize the bad page table and bad page to point
284 * to a couple of allocated pages
286 empty_zero_page
= memblock_alloc(PAGE_SIZE
, PAGE_SIZE
);
287 if (!empty_zero_page
)
288 panic("%s: Failed to allocate %lu bytes align=0x%lx\n",
289 __func__
, PAGE_SIZE
, PAGE_SIZE
);
292 * Set up SFC/DFC registers
297 printk ("before free_area_init\n");
299 for (i
= 0; i
< m68k_num_memory
; i
++) {
300 zones_size
[ZONE_DMA
] = m68k_memory
[i
].size
>> PAGE_SHIFT
;
301 free_area_init_node(i
, zones_size
,
302 m68k_memory
[i
].addr
>> PAGE_SHIFT
, NULL
);
303 if (node_present_pages(i
))
304 node_set_state(i
, N_NORMAL_MEMORY
);