blob: 2113eec8dbf9b7466a64e905c1f1e3c3a02a2878 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Uwe Zeisbergerf30c2262006-10-03 23:01:26 +02003 * linux/arch/m68k/mm/motorola.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#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/bootmem.h>
Mike Rapoport1008a112018-07-04 09:28:16 +030022#include <linux/memblock.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
25#include <asm/setup.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080026#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <asm/page.h>
28#include <asm/pgalloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <asm/machdep.h>
30#include <asm/io.h>
31#include <asm/dma.h>
32#ifdef CONFIG_ATARI
33#include <asm/atari_stram.h>
34#endif
Geert Uytterhoevenc85627f2008-12-21 12:03:37 +010035#include <asm/sections.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
37#undef DEBUG
38
39#ifndef mm_cachebits
40/*
41 * Bits to add to page descriptors for "normal" caching mode.
42 * For 68020/030 this is 0.
43 * For 68040, this is _PAGE_CACHE040 (cachable, copyback)
44 */
45unsigned long mm_cachebits;
46EXPORT_SYMBOL(mm_cachebits);
47#endif
48
Roman Zippel12d810c2007-05-31 00:40:54 -070049/* size of memory already mapped in head.S */
Andreas Schwab486df8b2014-04-24 12:24:48 +020050extern __initdata unsigned long m68k_init_mapped_size;
Roman Zippel12d810c2007-05-31 00:40:54 -070051
52extern unsigned long availmem;
53
Linus Torvalds1da177e2005-04-16 15:20:36 -070054static pte_t * __init kernel_page_table(void)
55{
56 pte_t *ptablep;
57
Mike Rapoporte8625dc2018-10-30 15:08:54 -070058 ptablep = (pte_t *)memblock_alloc_low(PAGE_SIZE, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
60 clear_page(ptablep);
61 __flush_page_to_ram(ptablep);
62 flush_tlb_kernel_page(ptablep);
63 nocache_page(ptablep);
64
65 return ptablep;
66}
67
68static pmd_t *last_pgtable __initdata = NULL;
69pmd_t *zero_pgtable __initdata = NULL;
70
71static pmd_t * __init kernel_ptr_table(void)
72{
73 if (!last_pgtable) {
74 unsigned long pmd, last;
75 int i;
76
77 /* Find the last ptr table that was used in head.S and
78 * reuse the remaining space in that page for further
79 * ptr tables.
80 */
81 last = (unsigned long)kernel_pg_dir;
82 for (i = 0; i < PTRS_PER_PGD; i++) {
83 if (!pgd_present(kernel_pg_dir[i]))
84 continue;
85 pmd = __pgd_page(kernel_pg_dir[i]);
86 if (pmd > last)
87 last = pmd;
88 }
89
90 last_pgtable = (pmd_t *)last;
91#ifdef DEBUG
92 printk("kernel_ptr_init: %p\n", last_pgtable);
93#endif
94 }
95
96 last_pgtable += PTRS_PER_PMD;
97 if (((unsigned long)last_pgtable & ~PAGE_MASK) == 0) {
Mike Rapoporte8625dc2018-10-30 15:08:54 -070098 last_pgtable = (pmd_t *)memblock_alloc_low(PAGE_SIZE,
99 PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
101 clear_page(last_pgtable);
102 __flush_page_to_ram(last_pgtable);
103 flush_tlb_kernel_page(last_pgtable);
104 nocache_page(last_pgtable);
105 }
106
107 return last_pgtable;
108}
109
Roman Zippel12d810c2007-05-31 00:40:54 -0700110static void __init map_node(int node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111{
112#define PTRTREESIZE (256*1024)
113#define ROOTTREESIZE (32*1024*1024)
Roman Zippel12d810c2007-05-31 00:40:54 -0700114 unsigned long physaddr, virtaddr, size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 pgd_t *pgd_dir;
116 pmd_t *pmd_dir;
117 pte_t *pte_dir;
118
Roman Zippel12d810c2007-05-31 00:40:54 -0700119 size = m68k_memory[node].size;
120 physaddr = m68k_memory[node].addr;
121 virtaddr = (unsigned long)phys_to_virt(physaddr);
122 physaddr |= m68k_supervisor_cachemode |
123 _PAGE_PRESENT | _PAGE_ACCESSED | _PAGE_DIRTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 if (CPU_IS_040_OR_060)
125 physaddr |= _PAGE_GLOBAL040;
126
127 while (size > 0) {
128#ifdef DEBUG
129 if (!(virtaddr & (PTRTREESIZE-1)))
130 printk ("\npa=%#lx va=%#lx ", physaddr & PAGE_MASK,
131 virtaddr);
132#endif
133 pgd_dir = pgd_offset_k(virtaddr);
134 if (virtaddr && CPU_IS_020_OR_030) {
135 if (!(virtaddr & (ROOTTREESIZE-1)) &&
136 size >= ROOTTREESIZE) {
137#ifdef DEBUG
138 printk ("[very early term]");
139#endif
140 pgd_val(*pgd_dir) = physaddr;
141 size -= ROOTTREESIZE;
142 virtaddr += ROOTTREESIZE;
143 physaddr += ROOTTREESIZE;
144 continue;
145 }
146 }
147 if (!pgd_present(*pgd_dir)) {
148 pmd_dir = kernel_ptr_table();
149#ifdef DEBUG
150 printk ("[new pointer %p]", pmd_dir);
151#endif
152 pgd_set(pgd_dir, pmd_dir);
153 } else
154 pmd_dir = pmd_offset(pgd_dir, virtaddr);
155
156 if (CPU_IS_020_OR_030) {
157 if (virtaddr) {
158#ifdef DEBUG
159 printk ("[early term]");
160#endif
161 pmd_dir->pmd[(virtaddr/PTRTREESIZE) & 15] = physaddr;
162 physaddr += PTRTREESIZE;
163 } else {
164 int i;
165#ifdef DEBUG
166 printk ("[zero map]");
167#endif
168 zero_pgtable = kernel_ptr_table();
169 pte_dir = (pte_t *)zero_pgtable;
170 pmd_dir->pmd[0] = virt_to_phys(pte_dir) |
171 _PAGE_TABLE | _PAGE_ACCESSED;
172 pte_val(*pte_dir++) = 0;
173 physaddr += PAGE_SIZE;
174 for (i = 1; i < 64; physaddr += PAGE_SIZE, i++)
175 pte_val(*pte_dir++) = physaddr;
176 }
177 size -= PTRTREESIZE;
178 virtaddr += PTRTREESIZE;
179 } else {
180 if (!pmd_present(*pmd_dir)) {
181#ifdef DEBUG
182 printk ("[new table]");
183#endif
184 pte_dir = kernel_page_table();
185 pmd_set(pmd_dir, pte_dir);
186 }
187 pte_dir = pte_offset_kernel(pmd_dir, virtaddr);
188
189 if (virtaddr) {
190 if (!pte_present(*pte_dir))
191 pte_val(*pte_dir) = physaddr;
192 } else
193 pte_val(*pte_dir) = 0;
194 size -= PAGE_SIZE;
195 virtaddr += PAGE_SIZE;
196 physaddr += PAGE_SIZE;
197 }
198
199 }
200#ifdef DEBUG
201 printk("\n");
202#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203}
204
205/*
206 * paging_init() continues the virtual memory environment setup which
207 * was begun by the code in arch/head.S.
208 */
209void __init paging_init(void)
210{
Roman Zippel2dcf15b2006-06-23 02:04:58 -0700211 unsigned long zones_size[MAX_NR_ZONES] = { 0, };
Roman Zippel12d810c2007-05-31 00:40:54 -0700212 unsigned long min_addr, max_addr;
Mike Rapoport1008a112018-07-04 09:28:16 +0300213 unsigned long addr;
Roman Zippel12d810c2007-05-31 00:40:54 -0700214 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
216#ifdef DEBUG
Geert Uytterhoevenfb425d02007-08-22 14:01:30 -0700217 printk ("start of paging_init (%p, %lx)\n", kernel_pg_dir, availmem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218#endif
219
220 /* Fix the cache mode in the page descriptors for the 680[46]0. */
221 if (CPU_IS_040_OR_060) {
222 int i;
223#ifndef mm_cachebits
224 mm_cachebits = _PAGE_CACHE040;
225#endif
226 for (i = 0; i < 16; i++)
227 pgprot_val(protection_map[i]) |= _PAGE_CACHE040;
228 }
229
Roman Zippel12d810c2007-05-31 00:40:54 -0700230 min_addr = m68k_memory[0].addr;
231 max_addr = min_addr + m68k_memory[0].size;
232 for (i = 1; i < m68k_num_memory;) {
233 if (m68k_memory[i].addr < min_addr) {
234 printk("Ignoring memory chunk at 0x%lx:0x%lx before the first chunk\n",
235 m68k_memory[i].addr, m68k_memory[i].size);
236 printk("Fix your bootloader or use a memfile to make use of this area!\n");
237 m68k_num_memory--;
238 memmove(m68k_memory + i, m68k_memory + i + 1,
Geert Uytterhoeven79930082013-10-02 21:50:56 +0200239 (m68k_num_memory - i) * sizeof(struct m68k_mem_info));
Roman Zippel12d810c2007-05-31 00:40:54 -0700240 continue;
241 }
242 addr = m68k_memory[i].addr + m68k_memory[i].size;
243 if (addr > max_addr)
244 max_addr = addr;
245 i++;
246 }
247 m68k_memoffset = min_addr - PAGE_OFFSET;
248 m68k_virt_to_node_shift = fls(max_addr - min_addr - 1) - 6;
249
Roman Zippelfbe9c962007-05-31 00:40:50 -0700250 module_fixup(NULL, __start_fixup, __stop_fixup);
251 flush_icache();
252
Roman Zippel12d810c2007-05-31 00:40:54 -0700253 high_memory = phys_to_virt(max_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
Roman Zippel12d810c2007-05-31 00:40:54 -0700255 min_low_pfn = availmem >> PAGE_SHIFT;
Geert Uytterhoeven79ae4fa2015-11-15 11:34:20 +0100256 max_pfn = max_low_pfn = max_addr >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
Mike Rapoport1008a112018-07-04 09:28:16 +0300258 /* Reserve kernel text/data/bss and the memory allocated in head.S */
259 memblock_reserve(m68k_memory[0].addr, availmem - m68k_memory[0].addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
Roman Zippel12d810c2007-05-31 00:40:54 -0700261 /*
262 * Map the physical memory available into the kernel virtual
Mike Rapoport1008a112018-07-04 09:28:16 +0300263 * address space. Make sure memblock will not try to allocate
264 * pages beyond the memory we already mapped in head.S
Roman Zippel12d810c2007-05-31 00:40:54 -0700265 */
Mike Rapoport1008a112018-07-04 09:28:16 +0300266 memblock_set_bottom_up(true);
Roman Zippel12d810c2007-05-31 00:40:54 -0700267
Mike Rapoport1008a112018-07-04 09:28:16 +0300268 for (i = 0; i < m68k_num_memory; i++) {
269 m68k_setup_node(i);
Roman Zippel12d810c2007-05-31 00:40:54 -0700270 map_node(i);
Mike Rapoport1008a112018-07-04 09:28:16 +0300271 }
Roman Zippel12d810c2007-05-31 00:40:54 -0700272
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 flush_tlb_all();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
275 /*
276 * initialize the bad page table and bad page to point
277 * to a couple of allocated pages
278 */
Mike Rapoport15c3c112018-10-30 15:08:58 -0700279 empty_zero_page = memblock_alloc(PAGE_SIZE, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
281 /*
282 * Set up SFC/DFC registers
283 */
284 set_fs(KERNEL_DS);
285
286#ifdef DEBUG
287 printk ("before free_area_init\n");
288#endif
Roman Zippel12d810c2007-05-31 00:40:54 -0700289 for (i = 0; i < m68k_num_memory; i++) {
290 zones_size[ZONE_DMA] = m68k_memory[i].size >> PAGE_SHIFT;
Johannes Weiner9109fb72008-07-23 21:27:20 -0700291 free_area_init_node(i, zones_size,
Roman Zippel12d810c2007-05-31 00:40:54 -0700292 m68k_memory[i].addr >> PAGE_SHIFT, NULL);
Michael Schmitz4aac0b482011-04-26 14:51:53 +1200293 if (node_present_pages(i))
294 node_set_state(i, N_NORMAL_MEMORY);
Roman Zippel12d810c2007-05-31 00:40:54 -0700295 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296}
297