]> Git Repo - linux.git/blob - arch/m68k/mm/motorola.c
Linux 6.14-rc3
[linux.git] / arch / m68k / mm / motorola.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * linux/arch/m68k/mm/motorola.c
4  *
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
12 #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>
21 #include <linux/memblock.h>
22 #include <linux/gfp.h>
23
24 #include <asm/setup.h>
25 #include <linux/uaccess.h>
26 #include <asm/page.h>
27 #include <asm/pgalloc.h>
28 #include <asm/machdep.h>
29 #include <asm/io.h>
30 #ifdef CONFIG_ATARI
31 #include <asm/atari_stram.h>
32 #endif
33 #include <asm/sections.h>
34
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  */
43 unsigned long mm_cachebits;
44 EXPORT_SYMBOL(mm_cachebits);
45 #endif
46
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
50  */
51 static inline void nocache_page(void *vaddr)
52 {
53         unsigned long addr = (unsigned long)vaddr;
54
55         if (CPU_IS_040_OR_060) {
56                 pte_t *ptep = virt_to_kpte(addr);
57
58                 *ptep = pte_mknocache(*ptep);
59         }
60 }
61
62 static inline void cache_page(void *vaddr)
63 {
64         unsigned long addr = (unsigned long)vaddr;
65
66         if (CPU_IS_040_OR_060) {
67                 pte_t *ptep = virt_to_kpte(addr);
68
69                 *ptep = pte_mkcache(*ptep);
70         }
71 }
72
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
82 void mmu_page_ctor(void *page)
83 {
84         __flush_pages_to_ram(page, 1);
85         flush_tlb_kernel_page(page);
86         nocache_page(page);
87 }
88
89 void mmu_page_dtor(void *page)
90 {
91         cache_page(page);
92 }
93
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 ... */
97
98 typedef struct list_head ptable_desc;
99
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]),
104 };
105
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)
109
110 static const int ptable_shift[3] = {
111         7+2, /* PGD */
112         7+2, /* PMD */
113         6+2, /* PTE */
114 };
115
116 #define ptable_size(type) (1U << ptable_shift[type])
117 #define ptable_mask(type) ((1U << (PAGE_SIZE / ptable_size(type))) - 1)
118
119 void __init init_pointer_table(void *table, int type)
120 {
121         ptable_desc *dp;
122         unsigned long ptable = (unsigned long)table;
123         unsigned long page = ptable & PAGE_MASK;
124         unsigned int mask = 1U << ((ptable - page)/ptable_size(type));
125
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]);
130         }
131
132         PD_MARKBITS(dp) &= ~mask;
133         pr_debug("init_pointer_table: %lx, %x\n", ptable, PD_MARKBITS(dp));
134
135         /* unreserve the page so it's possible to free that page */
136         __ClearPageReserved(PD_PAGE(dp));
137         init_page_count(PD_PAGE(dp));
138
139         return;
140 }
141
142 void *get_pointer_table(int type)
143 {
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;
147
148         /*
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.
153          */
154         if (mask == 0) {
155                 void *page;
156                 ptable_desc *new;
157
158                 if (!(page = (void *)get_zeroed_page(GFP_KERNEL)))
159                         return NULL;
160
161                 switch (type) {
162                 case TABLE_PTE:
163                         /*
164                          * m68k doesn't have SPLIT_PTE_PTLOCKS for not having
165                          * SMP.
166                          */
167                         pagetable_pte_ctor(virt_to_ptdesc(page));
168                         break;
169                 case TABLE_PMD:
170                         pagetable_pmd_ctor(virt_to_ptdesc(page));
171                         break;
172                 case TABLE_PGD:
173                         pagetable_pgd_ctor(virt_to_ptdesc(page));
174                         break;
175                 }
176
177                 mmu_page_ctor(page);
178
179                 new = PD_PTABLE(page);
180                 PD_MARKBITS(new) = ptable_mask(type) - 1;
181                 list_add_tail(new, dp);
182
183                 return (pmd_t *)page;
184         }
185
186         for (tmp = 1, off = 0; (mask & tmp) == 0; tmp <<= 1, off += ptable_size(type))
187                 ;
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]);
192         }
193         return page_address(PD_PAGE(dp)) + off;
194 }
195
196 int free_pointer_table(void *table, int type)
197 {
198         ptable_desc *dp;
199         unsigned long ptable = (unsigned long)table;
200         unsigned long page = ptable & PAGE_MASK;
201         unsigned int mask = 1U << ((ptable - page)/ptable_size(type));
202
203         dp = PD_PTABLE(page);
204         if (PD_MARKBITS (dp) & mask)
205                 panic ("table already free!");
206
207         PD_MARKBITS (dp) |= mask;
208
209         if (PD_MARKBITS(dp) == ptable_mask(type)) {
210                 /* all tables in page are free, free page */
211                 list_del(dp);
212                 mmu_page_dtor((void *)page);
213                 pagetable_dtor(virt_to_ptdesc((void *)page));
214                 free_page (page);
215                 return 1;
216         } else if (ptable_list[type].next != dp) {
217                 /*
218                  * move this descriptor to the front of the list, since
219                  * it has one or more free tables.
220                  */
221                 list_move(dp, &ptable_list[type]);
222         }
223         return 0;
224 }
225
226 /* size of memory already mapped in head.S */
227 extern __initdata unsigned long m68k_init_mapped_size;
228
229 extern unsigned long availmem;
230
231 static pte_t *last_pte_table __initdata = NULL;
232
233 static pte_t * __init kernel_page_table(void)
234 {
235         pte_t *pte_table = last_pte_table;
236
237         if (PAGE_ALIGNED(last_pte_table)) {
238                 pte_table = memblock_alloc_low(PAGE_SIZE, PAGE_SIZE);
239                 if (!pte_table) {
240                         panic("%s: Failed to allocate %lu bytes align=%lx\n",
241                                         __func__, PAGE_SIZE, PAGE_SIZE);
242                 }
243
244                 clear_page(pte_table);
245                 mmu_page_ctor(pte_table);
246
247                 last_pte_table = pte_table;
248         }
249
250         last_pte_table += PTRS_PER_PTE;
251
252         return pte_table;
253 }
254
255 static pmd_t *last_pmd_table __initdata = NULL;
256
257 static pmd_t * __init kernel_ptr_table(void)
258 {
259         if (!last_pmd_table) {
260                 unsigned long pmd, last;
261                 int i;
262
263                 /* Find the last ptr table that was used in head.S and
264                  * reuse the remaining space in that page for further
265                  * ptr tables.
266                  */
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]);
270
271                         if (!pud_present(*pud))
272                                 continue;
273                         pmd = pgd_page_vaddr(kernel_pg_dir[i]);
274                         if (pmd > last)
275                                 last = pmd;
276                 }
277
278                 last_pmd_table = (pmd_t *)last;
279 #ifdef DEBUG
280                 printk("kernel_ptr_init: %p\n", last_pmd_table);
281 #endif
282         }
283
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);
287                 if (!last_pmd_table)
288                         panic("%s: Failed to allocate %lu bytes align=%lx\n",
289                               __func__, PAGE_SIZE, PAGE_SIZE);
290
291                 clear_page(last_pmd_table);
292                 mmu_page_ctor(last_pmd_table);
293         }
294
295         return last_pmd_table;
296 }
297
298 static void __init map_node(int node)
299 {
300         unsigned long physaddr, virtaddr, size;
301         pgd_t *pgd_dir;
302         p4d_t *p4d_dir;
303         pud_t *pud_dir;
304         pmd_t *pmd_dir;
305         pte_t *pte_dir;
306
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;
314
315         while (size > 0) {
316 #ifdef DEBUG
317                 if (!(virtaddr & (PMD_SIZE-1)))
318                         printk ("\npa=%#lx va=%#lx ", physaddr & PAGE_MASK,
319                                 virtaddr);
320 #endif
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) {
325 #ifdef DEBUG
326                                 printk ("[very early term]");
327 #endif
328                                 pgd_val(*pgd_dir) = physaddr;
329                                 size -= PGDIR_SIZE;
330                                 virtaddr += PGDIR_SIZE;
331                                 physaddr += PGDIR_SIZE;
332                                 continue;
333                         }
334                 }
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();
339 #ifdef DEBUG
340                         printk ("[new pointer %p]", pmd_dir);
341 #endif
342                         pud_set(pud_dir, pmd_dir);
343                 } else
344                         pmd_dir = pmd_offset(pud_dir, virtaddr);
345
346                 if (CPU_IS_020_OR_030) {
347                         if (virtaddr) {
348 #ifdef DEBUG
349                                 printk ("[early term]");
350 #endif
351                                 pmd_val(*pmd_dir) = physaddr;
352                                 physaddr += PMD_SIZE;
353                         } else {
354                                 int i;
355 #ifdef DEBUG
356                                 printk ("[zero map]");
357 #endif
358                                 pte_dir = kernel_page_table();
359                                 pmd_set(pmd_dir, pte_dir);
360
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;
365                         }
366                         size -= PMD_SIZE;
367                         virtaddr += PMD_SIZE;
368                 } else {
369                         if (!pmd_present(*pmd_dir)) {
370 #ifdef DEBUG
371                                 printk ("[new table]");
372 #endif
373                                 pte_dir = kernel_page_table();
374                                 pmd_set(pmd_dir, pte_dir);
375                         }
376                         pte_dir = pte_offset_kernel(pmd_dir, virtaddr);
377
378                         if (virtaddr) {
379                                 if (!pte_present(*pte_dir))
380                                         pte_val(*pte_dir) = physaddr;
381                         } else
382                                 pte_val(*pte_dir) = 0;
383                         size -= PAGE_SIZE;
384                         virtaddr += PAGE_SIZE;
385                         physaddr += PAGE_SIZE;
386                 }
387
388         }
389 #ifdef DEBUG
390         printk("\n");
391 #endif
392 }
393
394 /*
395  * Alternate definitions that are compile time constants, for
396  * initializing protection_map.  The cachebits are fixed later.
397  */
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)
402
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
420 };
421 DECLARE_VM_GET_PAGE_PROT
422
423 /*
424  * paging_init() continues the virtual memory environment setup which
425  * was begun by the code in arch/head.S.
426  */
427 void __init paging_init(void)
428 {
429         unsigned long max_zone_pfn[MAX_NR_ZONES] = { 0, };
430         unsigned long min_addr, max_addr;
431         unsigned long addr;
432         int i;
433
434 #ifdef DEBUG
435         printk ("start of paging_init (%p, %lx)\n", kernel_pg_dir, availmem);
436 #endif
437
438         /* Fix the cache mode in the page descriptors for the 680[46]0.  */
439         if (CPU_IS_040_OR_060) {
440                 int i;
441 #ifndef mm_cachebits
442                 mm_cachebits = _PAGE_CACHE040;
443 #endif
444                 for (i = 0; i < 16; i++)
445                         pgprot_val(protection_map[i]) |= _PAGE_CACHE040;
446         }
447
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,
451                           MEMBLOCK_NONE);
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");
457                         m68k_num_memory--;
458                         memmove(m68k_memory + i, m68k_memory + i + 1,
459                                 (m68k_num_memory - i) * sizeof(struct m68k_mem_info));
460                         continue;
461                 }
462                 memblock_add_node(m68k_memory[i].addr, m68k_memory[i].size, i,
463                                   MEMBLOCK_NONE);
464                 addr = m68k_memory[i].addr + m68k_memory[i].size - 1;
465                 if (addr > max_addr)
466                         max_addr = addr;
467                 i++;
468         }
469         m68k_memoffset = min_addr - PAGE_OFFSET;
470         m68k_virt_to_node_shift = fls(max_addr - min_addr) - 6;
471
472         module_fixup(NULL, __start_fixup, __stop_fixup);
473         flush_icache();
474
475         high_memory = phys_to_virt(max_addr) + 1;
476
477         min_low_pfn = availmem >> PAGE_SHIFT;
478         max_pfn = max_low_pfn = (max_addr >> PAGE_SHIFT) + 1;
479
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);
482
483         /*
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
487          */
488         memblock_set_bottom_up(true);
489
490         for (i = 0; i < m68k_num_memory; i++) {
491                 m68k_setup_node(i);
492                 map_node(i);
493         }
494
495         flush_tlb_all();
496
497         early_memtest(min_addr, max_addr);
498
499         /*
500          * initialize the bad page table and bad page to point
501          * to a couple of allocated pages
502          */
503         empty_zero_page = memblock_alloc_or_panic(PAGE_SIZE, PAGE_SIZE);
504
505         /*
506          * Set up SFC/DFC registers
507          */
508         set_fc(USER_DATA);
509
510 #ifdef DEBUG
511         printk ("before free_area_init\n");
512 #endif
513         for (i = 0; i < m68k_num_memory; i++)
514                 if (node_present_pages(i))
515                         node_set_state(i, N_NORMAL_MEMORY);
516
517         max_zone_pfn[ZONE_DMA] = memblock_end_of_DRAM();
518         free_area_init(max_zone_pfn);
519 }
This page took 0.054528 seconds and 4 git commands to generate.