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);
50 * Motorola 680x0 user's manual recommends using uncached memory for address
53 * Seeing how the MMU can be external on (some of) these chips, that seems like
54 * a very important recommendation to follow. Provide some helpers to combat
55 * 'variation' amongst the users of this.
58 void mmu_page_ctor(void *page)
60 __flush_page_to_ram(page);
61 flush_tlb_kernel_page(page);
65 void mmu_page_dtor(void *page)
70 /* ++andreas: {get,free}_pointer_table rewritten to use unused fields from
71 struct page instead of separately kmalloced struct. Stolen from
72 arch/sparc/mm/srmmu.c ... */
74 typedef struct list_head ptable_desc;
76 static struct list_head ptable_list[2] = {
77 LIST_HEAD_INIT(ptable_list[0]),
78 LIST_HEAD_INIT(ptable_list[1]),
81 #define PD_PTABLE(page) ((ptable_desc *)&(virt_to_page(page)->lru))
82 #define PD_PAGE(ptable) (list_entry(ptable, struct page, lru))
83 #define PD_MARKBITS(dp) (*(unsigned int *)&PD_PAGE(dp)->index)
85 static const int ptable_shift[2] = {
90 #define ptable_size(type) (1U << ptable_shift[type])
91 #define ptable_mask(type) ((1U << (PAGE_SIZE / ptable_size(type))) - 1)
93 void __init init_pointer_table(void *table, int type)
96 unsigned long ptable = (unsigned long)table;
97 unsigned long page = ptable & PAGE_MASK;
98 unsigned int mask = 1U << ((ptable - page)/ptable_size(type));
100 dp = PD_PTABLE(page);
101 if (!(PD_MARKBITS(dp) & mask)) {
102 PD_MARKBITS(dp) = ptable_mask(type);
103 list_add(dp, &ptable_list[type]);
106 PD_MARKBITS(dp) &= ~mask;
107 pr_debug("init_pointer_table: %lx, %x\n", ptable, PD_MARKBITS(dp));
109 /* unreserve the page so it's possible to free that page */
110 __ClearPageReserved(PD_PAGE(dp));
111 init_page_count(PD_PAGE(dp));
116 void *get_pointer_table(int type)
118 ptable_desc *dp = ptable_list[type].next;
119 unsigned int mask = list_empty(&ptable_list[type]) ? 0 : PD_MARKBITS(dp);
120 unsigned int tmp, off;
123 * For a pointer table for a user process address space, a
124 * table is taken from a page allocated for the purpose. Each
125 * page can hold 8 pointer tables. The page is remapped in
126 * virtual address space to be noncacheable.
132 if (!(page = (void *)get_zeroed_page(GFP_KERNEL)))
135 if (type == TABLE_PTE) {
137 * m68k doesn't have SPLIT_PTE_PTLOCKS for not having
140 pgtable_pte_page_ctor(virt_to_page(page));
145 new = PD_PTABLE(page);
146 PD_MARKBITS(new) = ptable_mask(type) - 1;
147 list_add_tail(new, dp);
149 return (pmd_t *)page;
152 for (tmp = 1, off = 0; (mask & tmp) == 0; tmp <<= 1, off += ptable_size(type))
154 PD_MARKBITS(dp) = mask & ~tmp;
155 if (!PD_MARKBITS(dp)) {
156 /* move to end of list */
157 list_move_tail(dp, &ptable_list[type]);
159 return page_address(PD_PAGE(dp)) + off;
162 int free_pointer_table(void *table, int type)
165 unsigned long ptable = (unsigned long)table;
166 unsigned long page = ptable & PAGE_MASK;
167 unsigned int mask = 1U << ((ptable - page)/ptable_size(type));
169 dp = PD_PTABLE(page);
170 if (PD_MARKBITS (dp) & mask)
171 panic ("table already free!");
173 PD_MARKBITS (dp) |= mask;
175 if (PD_MARKBITS(dp) == ptable_mask(type)) {
176 /* all tables in page are free, free page */
178 mmu_page_dtor((void *)page);
179 if (type == TABLE_PTE)
180 pgtable_pte_page_dtor(virt_to_page(page));
183 } else if (ptable_list[type].next != dp) {
185 * move this descriptor to the front of the list, since
186 * it has one or more free tables.
188 list_move(dp, &ptable_list[type]);
193 /* size of memory already mapped in head.S */
194 extern __initdata unsigned long m68k_init_mapped_size;
196 extern unsigned long availmem;
198 static pte_t *last_pte_table __initdata = NULL;
200 static pte_t * __init kernel_page_table(void)
202 pte_t *pte_table = last_pte_table;
204 if (((unsigned long)last_pte_table & ~PAGE_MASK) == 0) {
205 pte_table = (pte_t *)memblock_alloc_low(PAGE_SIZE, PAGE_SIZE);
207 panic("%s: Failed to allocate %lu bytes align=%lx\n",
208 __func__, PAGE_SIZE, PAGE_SIZE);
211 clear_page(pte_table);
212 mmu_page_ctor(pte_table);
214 last_pte_table = pte_table;
217 last_pte_table += PTRS_PER_PTE;
222 static pmd_t *last_pmd_table __initdata = NULL;
224 static pmd_t * __init kernel_ptr_table(void)
226 if (!last_pmd_table) {
227 unsigned long pmd, last;
230 /* Find the last ptr table that was used in head.S and
231 * reuse the remaining space in that page for further
234 last = (unsigned long)kernel_pg_dir;
235 for (i = 0; i < PTRS_PER_PGD; i++) {
236 pud_t *pud = (pud_t *)(&kernel_pg_dir[i]);
238 if (!pud_present(*pud))
240 pmd = pgd_page_vaddr(kernel_pg_dir[i]);
245 last_pmd_table = (pmd_t *)last;
247 printk("kernel_ptr_init: %p\n", last_pmd_table);
251 last_pmd_table += PTRS_PER_PMD;
252 if (((unsigned long)last_pmd_table & ~PAGE_MASK) == 0) {
253 last_pmd_table = (pmd_t *)memblock_alloc_low(PAGE_SIZE,
256 panic("%s: Failed to allocate %lu bytes align=%lx\n",
257 __func__, PAGE_SIZE, PAGE_SIZE);
259 clear_page(last_pmd_table);
260 mmu_page_ctor(last_pmd_table);
263 return last_pmd_table;
266 static void __init map_node(int node)
268 unsigned long physaddr, virtaddr, size;
275 size = m68k_memory[node].size;
276 physaddr = m68k_memory[node].addr;
277 virtaddr = (unsigned long)phys_to_virt(physaddr);
278 physaddr |= m68k_supervisor_cachemode |
279 _PAGE_PRESENT | _PAGE_ACCESSED | _PAGE_DIRTY;
280 if (CPU_IS_040_OR_060)
281 physaddr |= _PAGE_GLOBAL040;
285 if (!(virtaddr & (PMD_SIZE-1)))
286 printk ("\npa=%#lx va=%#lx ", physaddr & PAGE_MASK,
289 pgd_dir = pgd_offset_k(virtaddr);
290 if (virtaddr && CPU_IS_020_OR_030) {
291 if (!(virtaddr & (PGDIR_SIZE-1)) &&
292 size >= PGDIR_SIZE) {
294 printk ("[very early term]");
296 pgd_val(*pgd_dir) = physaddr;
298 virtaddr += PGDIR_SIZE;
299 physaddr += PGDIR_SIZE;
303 p4d_dir = p4d_offset(pgd_dir, virtaddr);
304 pud_dir = pud_offset(p4d_dir, virtaddr);
305 if (!pud_present(*pud_dir)) {
306 pmd_dir = kernel_ptr_table();
308 printk ("[new pointer %p]", pmd_dir);
310 pud_set(pud_dir, pmd_dir);
312 pmd_dir = pmd_offset(pud_dir, virtaddr);
314 if (CPU_IS_020_OR_030) {
317 printk ("[early term]");
319 pmd_val(*pmd_dir) = physaddr;
320 physaddr += PMD_SIZE;
324 printk ("[zero map]");
326 pte_dir = kernel_page_table();
327 pmd_set(pmd_dir, pte_dir);
329 pte_val(*pte_dir++) = 0;
330 physaddr += PAGE_SIZE;
331 for (i = 1; i < PTRS_PER_PTE; physaddr += PAGE_SIZE, i++)
332 pte_val(*pte_dir++) = physaddr;
335 virtaddr += PMD_SIZE;
337 if (!pmd_present(*pmd_dir)) {
339 printk ("[new table]");
341 pte_dir = kernel_page_table();
342 pmd_set(pmd_dir, pte_dir);
344 pte_dir = pte_offset_kernel(pmd_dir, virtaddr);
347 if (!pte_present(*pte_dir))
348 pte_val(*pte_dir) = physaddr;
350 pte_val(*pte_dir) = 0;
352 virtaddr += PAGE_SIZE;
353 physaddr += PAGE_SIZE;
363 * paging_init() continues the virtual memory environment setup which
364 * was begun by the code in arch/head.S.
366 void __init paging_init(void)
368 unsigned long max_zone_pfn[MAX_NR_ZONES] = { 0, };
369 unsigned long min_addr, max_addr;
374 printk ("start of paging_init (%p, %lx)\n", kernel_pg_dir, availmem);
377 /* Fix the cache mode in the page descriptors for the 680[46]0. */
378 if (CPU_IS_040_OR_060) {
381 mm_cachebits = _PAGE_CACHE040;
383 for (i = 0; i < 16; i++)
384 pgprot_val(protection_map[i]) |= _PAGE_CACHE040;
387 min_addr = m68k_memory[0].addr;
388 max_addr = min_addr + m68k_memory[0].size;
389 memblock_add_node(m68k_memory[0].addr, m68k_memory[0].size, 0);
390 for (i = 1; i < m68k_num_memory;) {
391 if (m68k_memory[i].addr < min_addr) {
392 printk("Ignoring memory chunk at 0x%lx:0x%lx before the first chunk\n",
393 m68k_memory[i].addr, m68k_memory[i].size);
394 printk("Fix your bootloader or use a memfile to make use of this area!\n");
396 memmove(m68k_memory + i, m68k_memory + i + 1,
397 (m68k_num_memory - i) * sizeof(struct m68k_mem_info));
400 memblock_add_node(m68k_memory[i].addr, m68k_memory[i].size, i);
401 addr = m68k_memory[i].addr + m68k_memory[i].size;
406 m68k_memoffset = min_addr - PAGE_OFFSET;
407 m68k_virt_to_node_shift = fls(max_addr - min_addr - 1) - 6;
409 module_fixup(NULL, __start_fixup, __stop_fixup);
412 high_memory = phys_to_virt(max_addr);
414 min_low_pfn = availmem >> PAGE_SHIFT;
415 max_pfn = max_low_pfn = max_addr >> PAGE_SHIFT;
417 /* Reserve kernel text/data/bss and the memory allocated in head.S */
418 memblock_reserve(m68k_memory[0].addr, availmem - m68k_memory[0].addr);
421 * Map the physical memory available into the kernel virtual
422 * address space. Make sure memblock will not try to allocate
423 * pages beyond the memory we already mapped in head.S
425 memblock_set_bottom_up(true);
427 for (i = 0; i < m68k_num_memory; i++) {
435 * initialize the bad page table and bad page to point
436 * to a couple of allocated pages
438 empty_zero_page = memblock_alloc(PAGE_SIZE, PAGE_SIZE);
439 if (!empty_zero_page)
440 panic("%s: Failed to allocate %lu bytes align=0x%lx\n",
441 __func__, PAGE_SIZE, PAGE_SIZE);
444 * Set up SFC/DFC registers
449 printk ("before free_area_init\n");
451 for (i = 0; i < m68k_num_memory; i++)
452 if (node_present_pages(i))
453 node_set_state(i, N_NORMAL_MEMORY);
455 max_zone_pfn[ZONE_DMA] = memblock_end_of_DRAM();
456 free_area_init(max_zone_pfn);