xref: /openbmc/linux/arch/m68k/mm/motorola.c (revision 3fc41476)
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 #include <asm/dma.h>
31 #ifdef CONFIG_ATARI
32 #include <asm/atari_stram.h>
33 #endif
34 #include <asm/sections.h>
35 
36 #undef DEBUG
37 
38 #ifndef mm_cachebits
39 /*
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)
43  */
44 unsigned long mm_cachebits;
45 EXPORT_SYMBOL(mm_cachebits);
46 #endif
47 
48 /* size of memory already mapped in head.S */
49 extern __initdata unsigned long m68k_init_mapped_size;
50 
51 extern unsigned long availmem;
52 
53 static pte_t * __init kernel_page_table(void)
54 {
55 	pte_t *ptablep;
56 
57 	ptablep = (pte_t *)memblock_alloc_low(PAGE_SIZE, PAGE_SIZE);
58 	if (!ptablep)
59 		panic("%s: Failed to allocate %lu bytes align=%lx\n",
60 		      __func__, PAGE_SIZE, PAGE_SIZE);
61 
62 	clear_page(ptablep);
63 	__flush_page_to_ram(ptablep);
64 	flush_tlb_kernel_page(ptablep);
65 	nocache_page(ptablep);
66 
67 	return ptablep;
68 }
69 
70 static pmd_t *last_pgtable __initdata = NULL;
71 pmd_t *zero_pgtable __initdata = NULL;
72 
73 static pmd_t * __init kernel_ptr_table(void)
74 {
75 	if (!last_pgtable) {
76 		unsigned long pmd, last;
77 		int i;
78 
79 		/* Find the last ptr table that was used in head.S and
80 		 * reuse the remaining space in that page for further
81 		 * ptr tables.
82 		 */
83 		last = (unsigned long)kernel_pg_dir;
84 		for (i = 0; i < PTRS_PER_PGD; i++) {
85 			if (!pgd_present(kernel_pg_dir[i]))
86 				continue;
87 			pmd = __pgd_page(kernel_pg_dir[i]);
88 			if (pmd > last)
89 				last = pmd;
90 		}
91 
92 		last_pgtable = (pmd_t *)last;
93 #ifdef DEBUG
94 		printk("kernel_ptr_init: %p\n", last_pgtable);
95 #endif
96 	}
97 
98 	last_pgtable += PTRS_PER_PMD;
99 	if (((unsigned long)last_pgtable & ~PAGE_MASK) == 0) {
100 		last_pgtable = (pmd_t *)memblock_alloc_low(PAGE_SIZE,
101 							   PAGE_SIZE);
102 		if (!last_pgtable)
103 			panic("%s: Failed to allocate %lu bytes align=%lx\n",
104 			      __func__, PAGE_SIZE, PAGE_SIZE);
105 
106 		clear_page(last_pgtable);
107 		__flush_page_to_ram(last_pgtable);
108 		flush_tlb_kernel_page(last_pgtable);
109 		nocache_page(last_pgtable);
110 	}
111 
112 	return last_pgtable;
113 }
114 
115 static void __init map_node(int node)
116 {
117 #define PTRTREESIZE (256*1024)
118 #define ROOTTREESIZE (32*1024*1024)
119 	unsigned long physaddr, virtaddr, size;
120 	pgd_t *pgd_dir;
121 	pmd_t *pmd_dir;
122 	pte_t *pte_dir;
123 
124 	size = m68k_memory[node].size;
125 	physaddr = m68k_memory[node].addr;
126 	virtaddr = (unsigned long)phys_to_virt(physaddr);
127 	physaddr |= m68k_supervisor_cachemode |
128 		    _PAGE_PRESENT | _PAGE_ACCESSED | _PAGE_DIRTY;
129 	if (CPU_IS_040_OR_060)
130 		physaddr |= _PAGE_GLOBAL040;
131 
132 	while (size > 0) {
133 #ifdef DEBUG
134 		if (!(virtaddr & (PTRTREESIZE-1)))
135 			printk ("\npa=%#lx va=%#lx ", physaddr & PAGE_MASK,
136 				virtaddr);
137 #endif
138 		pgd_dir = pgd_offset_k(virtaddr);
139 		if (virtaddr && CPU_IS_020_OR_030) {
140 			if (!(virtaddr & (ROOTTREESIZE-1)) &&
141 			    size >= ROOTTREESIZE) {
142 #ifdef DEBUG
143 				printk ("[very early term]");
144 #endif
145 				pgd_val(*pgd_dir) = physaddr;
146 				size -= ROOTTREESIZE;
147 				virtaddr += ROOTTREESIZE;
148 				physaddr += ROOTTREESIZE;
149 				continue;
150 			}
151 		}
152 		if (!pgd_present(*pgd_dir)) {
153 			pmd_dir = kernel_ptr_table();
154 #ifdef DEBUG
155 			printk ("[new pointer %p]", pmd_dir);
156 #endif
157 			pgd_set(pgd_dir, pmd_dir);
158 		} else
159 			pmd_dir = pmd_offset(pgd_dir, virtaddr);
160 
161 		if (CPU_IS_020_OR_030) {
162 			if (virtaddr) {
163 #ifdef DEBUG
164 				printk ("[early term]");
165 #endif
166 				pmd_dir->pmd[(virtaddr/PTRTREESIZE) & 15] = physaddr;
167 				physaddr += PTRTREESIZE;
168 			} else {
169 				int i;
170 #ifdef DEBUG
171 				printk ("[zero map]");
172 #endif
173 				zero_pgtable = kernel_ptr_table();
174 				pte_dir = (pte_t *)zero_pgtable;
175 				pmd_dir->pmd[0] = virt_to_phys(pte_dir) |
176 					_PAGE_TABLE | _PAGE_ACCESSED;
177 				pte_val(*pte_dir++) = 0;
178 				physaddr += PAGE_SIZE;
179 				for (i = 1; i < 64; physaddr += PAGE_SIZE, i++)
180 					pte_val(*pte_dir++) = physaddr;
181 			}
182 			size -= PTRTREESIZE;
183 			virtaddr += PTRTREESIZE;
184 		} else {
185 			if (!pmd_present(*pmd_dir)) {
186 #ifdef DEBUG
187 				printk ("[new table]");
188 #endif
189 				pte_dir = kernel_page_table();
190 				pmd_set(pmd_dir, pte_dir);
191 			}
192 			pte_dir = pte_offset_kernel(pmd_dir, virtaddr);
193 
194 			if (virtaddr) {
195 				if (!pte_present(*pte_dir))
196 					pte_val(*pte_dir) = physaddr;
197 			} else
198 				pte_val(*pte_dir) = 0;
199 			size -= PAGE_SIZE;
200 			virtaddr += PAGE_SIZE;
201 			physaddr += PAGE_SIZE;
202 		}
203 
204 	}
205 #ifdef DEBUG
206 	printk("\n");
207 #endif
208 }
209 
210 /*
211  * paging_init() continues the virtual memory environment setup which
212  * was begun by the code in arch/head.S.
213  */
214 void __init paging_init(void)
215 {
216 	unsigned long zones_size[MAX_NR_ZONES] = { 0, };
217 	unsigned long min_addr, max_addr;
218 	unsigned long addr;
219 	int i;
220 
221 #ifdef DEBUG
222 	printk ("start of paging_init (%p, %lx)\n", kernel_pg_dir, availmem);
223 #endif
224 
225 	/* Fix the cache mode in the page descriptors for the 680[46]0.  */
226 	if (CPU_IS_040_OR_060) {
227 		int i;
228 #ifndef mm_cachebits
229 		mm_cachebits = _PAGE_CACHE040;
230 #endif
231 		for (i = 0; i < 16; i++)
232 			pgprot_val(protection_map[i]) |= _PAGE_CACHE040;
233 	}
234 
235 	min_addr = m68k_memory[0].addr;
236 	max_addr = min_addr + m68k_memory[0].size;
237 	memblock_add(m68k_memory[0].addr, m68k_memory[0].size);
238 	for (i = 1; i < m68k_num_memory;) {
239 		if (m68k_memory[i].addr < min_addr) {
240 			printk("Ignoring memory chunk at 0x%lx:0x%lx before the first chunk\n",
241 				m68k_memory[i].addr, m68k_memory[i].size);
242 			printk("Fix your bootloader or use a memfile to make use of this area!\n");
243 			m68k_num_memory--;
244 			memmove(m68k_memory + i, m68k_memory + i + 1,
245 				(m68k_num_memory - i) * sizeof(struct m68k_mem_info));
246 			continue;
247 		}
248 		memblock_add(m68k_memory[i].addr, m68k_memory[i].size);
249 		addr = m68k_memory[i].addr + m68k_memory[i].size;
250 		if (addr > max_addr)
251 			max_addr = addr;
252 		i++;
253 	}
254 	m68k_memoffset = min_addr - PAGE_OFFSET;
255 	m68k_virt_to_node_shift = fls(max_addr - min_addr - 1) - 6;
256 
257 	module_fixup(NULL, __start_fixup, __stop_fixup);
258 	flush_icache();
259 
260 	high_memory = phys_to_virt(max_addr);
261 
262 	min_low_pfn = availmem >> PAGE_SHIFT;
263 	max_pfn = max_low_pfn = max_addr >> PAGE_SHIFT;
264 
265 	/* Reserve kernel text/data/bss and the memory allocated in head.S */
266 	memblock_reserve(m68k_memory[0].addr, availmem - m68k_memory[0].addr);
267 
268 	/*
269 	 * Map the physical memory available into the kernel virtual
270 	 * address space. Make sure memblock will not try to allocate
271 	 * pages beyond the memory we already mapped in head.S
272 	 */
273 	memblock_set_bottom_up(true);
274 
275 	for (i = 0; i < m68k_num_memory; i++) {
276 		m68k_setup_node(i);
277 		map_node(i);
278 	}
279 
280 	flush_tlb_all();
281 
282 	/*
283 	 * initialize the bad page table and bad page to point
284 	 * to a couple of allocated pages
285 	 */
286 	empty_zero_page = memblock_alloc(PAGE_SIZE, PAGE_SIZE);
287 	if (!empty_zero_page)
288 		panic("%s: Failed to allocate %lu bytes align=0x%lx\n",
289 		      __func__, PAGE_SIZE, PAGE_SIZE);
290 
291 	/*
292 	 * Set up SFC/DFC registers
293 	 */
294 	set_fs(KERNEL_DS);
295 
296 #ifdef DEBUG
297 	printk ("before free_area_init\n");
298 #endif
299 	for (i = 0; i < m68k_num_memory; i++) {
300 		zones_size[ZONE_DMA] = m68k_memory[i].size >> PAGE_SHIFT;
301 		free_area_init_node(i, zones_size,
302 				    m68k_memory[i].addr >> PAGE_SHIFT, NULL);
303 		if (node_present_pages(i))
304 			node_set_state(i, N_NORMAL_MEMORY);
305 	}
306 }
307 
308