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 pud_t
*pud
= (pud_t
*)(&kernel_pg_dir
[i
]);
87 if (!pud_present(*pud
))
89 pmd
= pgd_page_vaddr(kernel_pg_dir
[i
]);
94 last_pgtable
= (pmd_t
*)last
;
96 printk("kernel_ptr_init: %p\n", last_pgtable
);
100 last_pgtable
+= PTRS_PER_PMD
;
101 if (((unsigned long)last_pgtable
& ~PAGE_MASK
) == 0) {
102 last_pgtable
= (pmd_t
*)memblock_alloc_low(PAGE_SIZE
,
105 panic("%s: Failed to allocate %lu bytes align=%lx\n",
106 __func__
, PAGE_SIZE
, PAGE_SIZE
);
108 clear_page(last_pgtable
);
109 __flush_page_to_ram(last_pgtable
);
110 flush_tlb_kernel_page(last_pgtable
);
111 nocache_page(last_pgtable
);
117 static void __init
map_node(int node
)
119 #define PTRTREESIZE (256*1024)
120 #define ROOTTREESIZE (32*1024*1024)
121 unsigned long physaddr
, virtaddr
, size
;
128 size
= m68k_memory
[node
].size
;
129 physaddr
= m68k_memory
[node
].addr
;
130 virtaddr
= (unsigned long)phys_to_virt(physaddr
);
131 physaddr
|= m68k_supervisor_cachemode
|
132 _PAGE_PRESENT
| _PAGE_ACCESSED
| _PAGE_DIRTY
;
133 if (CPU_IS_040_OR_060
)
134 physaddr
|= _PAGE_GLOBAL040
;
138 if (!(virtaddr
& (PTRTREESIZE
-1)))
139 printk ("\npa=%#lx va=%#lx ", physaddr
& PAGE_MASK
,
142 pgd_dir
= pgd_offset_k(virtaddr
);
143 if (virtaddr
&& CPU_IS_020_OR_030
) {
144 if (!(virtaddr
& (ROOTTREESIZE
-1)) &&
145 size
>= ROOTTREESIZE
) {
147 printk ("[very early term]");
149 pgd_val(*pgd_dir
) = physaddr
;
150 size
-= ROOTTREESIZE
;
151 virtaddr
+= ROOTTREESIZE
;
152 physaddr
+= ROOTTREESIZE
;
156 p4d_dir
= p4d_offset(pgd_dir
, virtaddr
);
157 pud_dir
= pud_offset(p4d_dir
, virtaddr
);
158 if (!pud_present(*pud_dir
)) {
159 pmd_dir
= kernel_ptr_table();
161 printk ("[new pointer %p]", pmd_dir
);
163 pud_set(pud_dir
, pmd_dir
);
165 pmd_dir
= pmd_offset(pud_dir
, virtaddr
);
167 if (CPU_IS_020_OR_030
) {
170 printk ("[early term]");
172 pmd_dir
->pmd
[(virtaddr
/PTRTREESIZE
) & 15] = physaddr
;
173 physaddr
+= PTRTREESIZE
;
177 printk ("[zero map]");
179 zero_pgtable
= kernel_ptr_table();
180 pte_dir
= (pte_t
*)zero_pgtable
;
181 pmd_dir
->pmd
[0] = virt_to_phys(pte_dir
) |
182 _PAGE_TABLE
| _PAGE_ACCESSED
;
183 pte_val(*pte_dir
++) = 0;
184 physaddr
+= PAGE_SIZE
;
185 for (i
= 1; i
< 64; physaddr
+= PAGE_SIZE
, i
++)
186 pte_val(*pte_dir
++) = physaddr
;
189 virtaddr
+= PTRTREESIZE
;
191 if (!pmd_present(*pmd_dir
)) {
193 printk ("[new table]");
195 pte_dir
= kernel_page_table();
196 pmd_set(pmd_dir
, pte_dir
);
198 pte_dir
= pte_offset_kernel(pmd_dir
, virtaddr
);
201 if (!pte_present(*pte_dir
))
202 pte_val(*pte_dir
) = physaddr
;
204 pte_val(*pte_dir
) = 0;
206 virtaddr
+= PAGE_SIZE
;
207 physaddr
+= PAGE_SIZE
;
217 * paging_init() continues the virtual memory environment setup which
218 * was begun by the code in arch/head.S.
220 void __init
paging_init(void)
222 unsigned long zones_size
[MAX_NR_ZONES
] = { 0, };
223 unsigned long min_addr
, max_addr
;
228 printk ("start of paging_init (%p, %lx)\n", kernel_pg_dir
, availmem
);
231 /* Fix the cache mode in the page descriptors for the 680[46]0. */
232 if (CPU_IS_040_OR_060
) {
235 mm_cachebits
= _PAGE_CACHE040
;
237 for (i
= 0; i
< 16; i
++)
238 pgprot_val(protection_map
[i
]) |= _PAGE_CACHE040
;
241 min_addr
= m68k_memory
[0].addr
;
242 max_addr
= min_addr
+ m68k_memory
[0].size
;
243 memblock_add(m68k_memory
[0].addr
, m68k_memory
[0].size
);
244 for (i
= 1; i
< m68k_num_memory
;) {
245 if (m68k_memory
[i
].addr
< min_addr
) {
246 printk("Ignoring memory chunk at 0x%lx:0x%lx before the first chunk\n",
247 m68k_memory
[i
].addr
, m68k_memory
[i
].size
);
248 printk("Fix your bootloader or use a memfile to make use of this area!\n");
250 memmove(m68k_memory
+ i
, m68k_memory
+ i
+ 1,
251 (m68k_num_memory
- i
) * sizeof(struct m68k_mem_info
));
254 memblock_add(m68k_memory
[i
].addr
, m68k_memory
[i
].size
);
255 addr
= m68k_memory
[i
].addr
+ m68k_memory
[i
].size
;
260 m68k_memoffset
= min_addr
- PAGE_OFFSET
;
261 m68k_virt_to_node_shift
= fls(max_addr
- min_addr
- 1) - 6;
263 module_fixup(NULL
, __start_fixup
, __stop_fixup
);
266 high_memory
= phys_to_virt(max_addr
);
268 min_low_pfn
= availmem
>> PAGE_SHIFT
;
269 max_pfn
= max_low_pfn
= max_addr
>> PAGE_SHIFT
;
271 /* Reserve kernel text/data/bss and the memory allocated in head.S */
272 memblock_reserve(m68k_memory
[0].addr
, availmem
- m68k_memory
[0].addr
);
275 * Map the physical memory available into the kernel virtual
276 * address space. Make sure memblock will not try to allocate
277 * pages beyond the memory we already mapped in head.S
279 memblock_set_bottom_up(true);
281 for (i
= 0; i
< m68k_num_memory
; i
++) {
289 * initialize the bad page table and bad page to point
290 * to a couple of allocated pages
292 empty_zero_page
= memblock_alloc(PAGE_SIZE
, PAGE_SIZE
);
293 if (!empty_zero_page
)
294 panic("%s: Failed to allocate %lu bytes align=0x%lx\n",
295 __func__
, PAGE_SIZE
, PAGE_SIZE
);
298 * Set up SFC/DFC registers
303 printk ("before free_area_init\n");
305 for (i
= 0; i
< m68k_num_memory
; i
++) {
306 zones_size
[ZONE_DMA
] = m68k_memory
[i
].size
>> PAGE_SHIFT
;
307 free_area_init_node(i
, zones_size
,
308 m68k_memory
[i
].addr
>> PAGE_SHIFT
, NULL
);
309 if (node_present_pages(i
))
310 node_set_state(i
, N_NORMAL_MEMORY
);