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>
31 #include <asm/atari_stram.h>
33 #include <asm/sections.h>
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)
43 unsigned long mm_cachebits;
44 EXPORT_SYMBOL(mm_cachebits);
47 /* 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
51 static inline void nocache_page(void *vaddr)
53 unsigned long addr = (unsigned long)vaddr;
55 if (CPU_IS_040_OR_060) {
56 pte_t *ptep = virt_to_kpte(addr);
58 *ptep = pte_mknocache(*ptep);
62 static inline void cache_page(void *vaddr)
64 unsigned long addr = (unsigned long)vaddr;
66 if (CPU_IS_040_OR_060) {
67 pte_t *ptep = virt_to_kpte(addr);
69 *ptep = pte_mkcache(*ptep);
74 * Motorola 680x0 user's manual recommends using uncached memory for address
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.
82 void mmu_page_ctor(void *page)
84 __flush_pages_to_ram(page, 1);
85 flush_tlb_kernel_page(page);
89 void mmu_page_dtor(void *page)
94 /* ++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 ... */
98 typedef struct list_head ptable_desc;
100 static struct list_head ptable_list[3] = {
101 LIST_HEAD_INIT(ptable_list[0]),
102 LIST_HEAD_INIT(ptable_list[1]),
103 LIST_HEAD_INIT(ptable_list[2]),
106 #define PD_PTABLE(page) ((ptable_desc *)&(virt_to_page((void *)(page))->lru))
107 #define PD_PAGE(ptable) (list_entry(ptable, struct page, lru))
108 #define PD_MARKBITS(dp) (*(unsigned int *)&PD_PAGE(dp)->index)
110 static const int ptable_shift[3] = {
116 #define ptable_size(type) (1U << ptable_shift[type])
117 #define ptable_mask(type) ((1U << (PAGE_SIZE / ptable_size(type))) - 1)
119 void __init init_pointer_table(void *table, int type)
122 unsigned long ptable = (unsigned long)table;
123 unsigned long page = ptable & PAGE_MASK;
124 unsigned int mask = 1U << ((ptable - page)/ptable_size(type));
126 dp = PD_PTABLE(page);
127 if (!(PD_MARKBITS(dp) & mask)) {
128 PD_MARKBITS(dp) = ptable_mask(type);
129 list_add(dp, &ptable_list[type]);
132 PD_MARKBITS(dp) &= ~mask;
133 pr_debug("init_pointer_table: %lx, %x\n", ptable, PD_MARKBITS(dp));
135 /* unreserve the page so it's possible to free that page */
136 __ClearPageReserved(PD_PAGE(dp));
137 init_page_count(PD_PAGE(dp));
142 void *get_pointer_table(int type)
144 ptable_desc *dp = ptable_list[type].next;
145 unsigned int mask = list_empty(&ptable_list[type]) ? 0 : PD_MARKBITS(dp);
146 unsigned int tmp, off;
149 * For a pointer table for a user process address space, a
150 * table is taken from a page allocated for the purpose. Each
151 * page can hold 8 pointer tables. The page is remapped in
152 * virtual address space to be noncacheable.
158 if (!(page = (void *)get_zeroed_page(GFP_KERNEL)))
164 * m68k doesn't have SPLIT_PTE_PTLOCKS for not having
167 pagetable_pte_ctor(virt_to_ptdesc(page));
170 pagetable_pmd_ctor(virt_to_ptdesc(page));
173 pagetable_pgd_ctor(virt_to_ptdesc(page));
179 new = PD_PTABLE(page);
180 PD_MARKBITS(new) = ptable_mask(type) - 1;
181 list_add_tail(new, dp);
183 return (pmd_t *)page;
186 for (tmp = 1, off = 0; (mask & tmp) == 0; tmp <<= 1, off += ptable_size(type))
188 PD_MARKBITS(dp) = mask & ~tmp;
189 if (!PD_MARKBITS(dp)) {
190 /* move to end of list */
191 list_move_tail(dp, &ptable_list[type]);
193 return page_address(PD_PAGE(dp)) + off;
196 int free_pointer_table(void *table, int type)
199 unsigned long ptable = (unsigned long)table;
200 unsigned long page = ptable & PAGE_MASK;
201 unsigned int mask = 1U << ((ptable - page)/ptable_size(type));
203 dp = PD_PTABLE(page);
204 if (PD_MARKBITS (dp) & mask)
205 panic ("table already free!");
207 PD_MARKBITS (dp) |= mask;
209 if (PD_MARKBITS(dp) == ptable_mask(type)) {
210 /* all tables in page are free, free page */
212 mmu_page_dtor((void *)page);
213 pagetable_dtor(virt_to_ptdesc((void *)page));
216 } else if (ptable_list[type].next != dp) {
218 * move this descriptor to the front of the list, since
219 * it has one or more free tables.
221 list_move(dp, &ptable_list[type]);
226 /* size of memory already mapped in head.S */
227 extern __initdata unsigned long m68k_init_mapped_size;
229 extern unsigned long availmem;
231 static pte_t *last_pte_table __initdata = NULL;
233 static pte_t * __init kernel_page_table(void)
235 pte_t *pte_table = last_pte_table;
237 if (PAGE_ALIGNED(last_pte_table)) {
238 pte_table = memblock_alloc_low(PAGE_SIZE, PAGE_SIZE);
240 panic("%s: Failed to allocate %lu bytes align=%lx\n",
241 __func__, PAGE_SIZE, PAGE_SIZE);
244 clear_page(pte_table);
245 mmu_page_ctor(pte_table);
247 last_pte_table = pte_table;
250 last_pte_table += PTRS_PER_PTE;
255 static pmd_t *last_pmd_table __initdata = NULL;
257 static pmd_t * __init kernel_ptr_table(void)
259 if (!last_pmd_table) {
260 unsigned long pmd, last;
263 /* Find the last ptr table that was used in head.S and
264 * reuse the remaining space in that page for further
267 last = (unsigned long)kernel_pg_dir;
268 for (i = 0; i < PTRS_PER_PGD; i++) {
269 pud_t *pud = (pud_t *)(&kernel_pg_dir[i]);
271 if (!pud_present(*pud))
273 pmd = pgd_page_vaddr(kernel_pg_dir[i]);
278 last_pmd_table = (pmd_t *)last;
280 printk("kernel_ptr_init: %p\n", last_pmd_table);
284 last_pmd_table += PTRS_PER_PMD;
285 if (PAGE_ALIGNED(last_pmd_table)) {
286 last_pmd_table = memblock_alloc_low(PAGE_SIZE, PAGE_SIZE);
288 panic("%s: Failed to allocate %lu bytes align=%lx\n",
289 __func__, PAGE_SIZE, PAGE_SIZE);
291 clear_page(last_pmd_table);
292 mmu_page_ctor(last_pmd_table);
295 return last_pmd_table;
298 static void __init map_node(int node)
300 unsigned long physaddr, virtaddr, size;
307 size = m68k_memory[node].size;
308 physaddr = m68k_memory[node].addr;
309 virtaddr = (unsigned long)phys_to_virt(physaddr);
310 physaddr |= m68k_supervisor_cachemode |
311 _PAGE_PRESENT | _PAGE_ACCESSED | _PAGE_DIRTY;
312 if (CPU_IS_040_OR_060)
313 physaddr |= _PAGE_GLOBAL040;
317 if (!(virtaddr & (PMD_SIZE-1)))
318 printk ("\npa=%#lx va=%#lx ", physaddr & PAGE_MASK,
321 pgd_dir = pgd_offset_k(virtaddr);
322 if (virtaddr && CPU_IS_020_OR_030) {
323 if (!(virtaddr & (PGDIR_SIZE-1)) &&
324 size >= PGDIR_SIZE) {
326 printk ("[very early term]");
328 pgd_val(*pgd_dir) = physaddr;
330 virtaddr += PGDIR_SIZE;
331 physaddr += PGDIR_SIZE;
335 p4d_dir = p4d_offset(pgd_dir, virtaddr);
336 pud_dir = pud_offset(p4d_dir, virtaddr);
337 if (!pud_present(*pud_dir)) {
338 pmd_dir = kernel_ptr_table();
340 printk ("[new pointer %p]", pmd_dir);
342 pud_set(pud_dir, pmd_dir);
344 pmd_dir = pmd_offset(pud_dir, virtaddr);
346 if (CPU_IS_020_OR_030) {
349 printk ("[early term]");
351 pmd_val(*pmd_dir) = physaddr;
352 physaddr += PMD_SIZE;
356 printk ("[zero map]");
358 pte_dir = kernel_page_table();
359 pmd_set(pmd_dir, pte_dir);
361 pte_val(*pte_dir++) = 0;
362 physaddr += PAGE_SIZE;
363 for (i = 1; i < PTRS_PER_PTE; physaddr += PAGE_SIZE, i++)
364 pte_val(*pte_dir++) = physaddr;
367 virtaddr += PMD_SIZE;
369 if (!pmd_present(*pmd_dir)) {
371 printk ("[new table]");
373 pte_dir = kernel_page_table();
374 pmd_set(pmd_dir, pte_dir);
376 pte_dir = pte_offset_kernel(pmd_dir, virtaddr);
379 if (!pte_present(*pte_dir))
380 pte_val(*pte_dir) = physaddr;
382 pte_val(*pte_dir) = 0;
384 virtaddr += PAGE_SIZE;
385 physaddr += PAGE_SIZE;
395 * Alternate definitions that are compile time constants, for
396 * initializing protection_map. The cachebits are fixed later.
398 #define PAGE_NONE_C __pgprot(_PAGE_PROTNONE | _PAGE_ACCESSED)
399 #define PAGE_SHARED_C __pgprot(_PAGE_PRESENT | _PAGE_ACCESSED)
400 #define PAGE_COPY_C __pgprot(_PAGE_PRESENT | _PAGE_RONLY | _PAGE_ACCESSED)
401 #define PAGE_READONLY_C __pgprot(_PAGE_PRESENT | _PAGE_RONLY | _PAGE_ACCESSED)
403 static pgprot_t protection_map[16] __ro_after_init = {
404 [VM_NONE] = PAGE_NONE_C,
405 [VM_READ] = PAGE_READONLY_C,
406 [VM_WRITE] = PAGE_COPY_C,
407 [VM_WRITE | VM_READ] = PAGE_COPY_C,
408 [VM_EXEC] = PAGE_READONLY_C,
409 [VM_EXEC | VM_READ] = PAGE_READONLY_C,
410 [VM_EXEC | VM_WRITE] = PAGE_COPY_C,
411 [VM_EXEC | VM_WRITE | VM_READ] = PAGE_COPY_C,
412 [VM_SHARED] = PAGE_NONE_C,
413 [VM_SHARED | VM_READ] = PAGE_READONLY_C,
414 [VM_SHARED | VM_WRITE] = PAGE_SHARED_C,
415 [VM_SHARED | VM_WRITE | VM_READ] = PAGE_SHARED_C,
416 [VM_SHARED | VM_EXEC] = PAGE_READONLY_C,
417 [VM_SHARED | VM_EXEC | VM_READ] = PAGE_READONLY_C,
418 [VM_SHARED | VM_EXEC | VM_WRITE] = PAGE_SHARED_C,
419 [VM_SHARED | VM_EXEC | VM_WRITE | VM_READ] = PAGE_SHARED_C
421 DECLARE_VM_GET_PAGE_PROT
424 * paging_init() continues the virtual memory environment setup which
425 * was begun by the code in arch/head.S.
427 void __init paging_init(void)
429 unsigned long max_zone_pfn[MAX_NR_ZONES] = { 0, };
430 unsigned long min_addr, max_addr;
435 printk ("start of paging_init (%p, %lx)\n", kernel_pg_dir, availmem);
438 /* Fix the cache mode in the page descriptors for the 680[46]0. */
439 if (CPU_IS_040_OR_060) {
442 mm_cachebits = _PAGE_CACHE040;
444 for (i = 0; i < 16; i++)
445 pgprot_val(protection_map[i]) |= _PAGE_CACHE040;
448 min_addr = m68k_memory[0].addr;
449 max_addr = min_addr + m68k_memory[0].size - 1;
450 memblock_add_node(m68k_memory[0].addr, m68k_memory[0].size, 0,
452 for (i = 1; i < m68k_num_memory;) {
453 if (m68k_memory[i].addr < min_addr) {
454 printk("Ignoring memory chunk at 0x%lx:0x%lx before the first chunk\n",
455 m68k_memory[i].addr, m68k_memory[i].size);
456 printk("Fix your bootloader or use a memfile to make use of this area!\n");
458 memmove(m68k_memory + i, m68k_memory + i + 1,
459 (m68k_num_memory - i) * sizeof(struct m68k_mem_info));
462 memblock_add_node(m68k_memory[i].addr, m68k_memory[i].size, i,
464 addr = m68k_memory[i].addr + m68k_memory[i].size - 1;
469 m68k_memoffset = min_addr - PAGE_OFFSET;
470 m68k_virt_to_node_shift = fls(max_addr - min_addr) - 6;
472 module_fixup(NULL, __start_fixup, __stop_fixup);
475 high_memory = phys_to_virt(max_addr) + 1;
477 min_low_pfn = availmem >> PAGE_SHIFT;
478 max_pfn = max_low_pfn = (max_addr >> PAGE_SHIFT) + 1;
480 /* Reserve kernel text/data/bss and the memory allocated in head.S */
481 memblock_reserve(m68k_memory[0].addr, availmem - m68k_memory[0].addr);
484 * Map the physical memory available into the kernel virtual
485 * address space. Make sure memblock will not try to allocate
486 * pages beyond the memory we already mapped in head.S
488 memblock_set_bottom_up(true);
490 for (i = 0; i < m68k_num_memory; i++) {
497 early_memtest(min_addr, max_addr);
500 * initialize the bad page table and bad page to point
501 * to a couple of allocated pages
503 empty_zero_page = memblock_alloc_or_panic(PAGE_SIZE, PAGE_SIZE);
506 * Set up SFC/DFC registers
511 printk ("before free_area_init\n");
513 for (i = 0; i < m68k_num_memory; i++)
514 if (node_present_pages(i))
515 node_set_state(i, N_NORMAL_MEMORY);
517 max_zone_pfn[ZONE_DMA] = memblock_end_of_DRAM();
518 free_area_init(max_zone_pfn);