xref: /linux/arch/m68k/mm/motorola.c (revision e05c7b1f2bc4b7b28199b9a7572f73436d97317e)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
21da177e4SLinus Torvalds /*
3f30c2269SUwe Zeisberger  * linux/arch/m68k/mm/motorola.c
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  * Routines specific to the Motorola MMU, originally from:
61da177e4SLinus Torvalds  * linux/arch/m68k/init.c
71da177e4SLinus Torvalds  * which are Copyright (C) 1995 Hamish Macdonald
81da177e4SLinus Torvalds  *
91da177e4SLinus Torvalds  * Moved 8/20/1999 Sam Creasey
101da177e4SLinus Torvalds  */
111da177e4SLinus Torvalds 
121da177e4SLinus Torvalds #include <linux/module.h>
131da177e4SLinus Torvalds #include <linux/signal.h>
141da177e4SLinus Torvalds #include <linux/sched.h>
151da177e4SLinus Torvalds #include <linux/mm.h>
161da177e4SLinus Torvalds #include <linux/swap.h>
171da177e4SLinus Torvalds #include <linux/kernel.h>
181da177e4SLinus Torvalds #include <linux/string.h>
191da177e4SLinus Torvalds #include <linux/types.h>
201da177e4SLinus Torvalds #include <linux/init.h>
211008a115SMike Rapoport #include <linux/memblock.h>
225a0e3ad6STejun Heo #include <linux/gfp.h>
231da177e4SLinus Torvalds 
241da177e4SLinus Torvalds #include <asm/setup.h>
257c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
261da177e4SLinus Torvalds #include <asm/page.h>
271da177e4SLinus Torvalds #include <asm/pgalloc.h>
281da177e4SLinus Torvalds #include <asm/machdep.h>
291da177e4SLinus Torvalds #include <asm/io.h>
301da177e4SLinus Torvalds #include <asm/dma.h>
311da177e4SLinus Torvalds #ifdef CONFIG_ATARI
321da177e4SLinus Torvalds #include <asm/atari_stram.h>
331da177e4SLinus Torvalds #endif
34c85627fbSGeert Uytterhoeven #include <asm/sections.h>
351da177e4SLinus Torvalds 
361da177e4SLinus Torvalds #undef DEBUG
371da177e4SLinus Torvalds 
381da177e4SLinus Torvalds #ifndef mm_cachebits
391da177e4SLinus Torvalds /*
401da177e4SLinus Torvalds  * Bits to add to page descriptors for "normal" caching mode.
411da177e4SLinus Torvalds  * For 68020/030 this is 0.
421da177e4SLinus Torvalds  * For 68040, this is _PAGE_CACHE040 (cachable, copyback)
431da177e4SLinus Torvalds  */
441da177e4SLinus Torvalds unsigned long mm_cachebits;
451da177e4SLinus Torvalds EXPORT_SYMBOL(mm_cachebits);
461da177e4SLinus Torvalds #endif
471da177e4SLinus Torvalds 
481bcdc68dSMike Rapoport /* Prior to calling these routines, the page should have been flushed
491bcdc68dSMike Rapoport  * from both the cache and ATC, or the CPU might not notice that the
501bcdc68dSMike Rapoport  * cache setting for the page has been changed. -jskov
511bcdc68dSMike Rapoport  */
521bcdc68dSMike Rapoport static inline void nocache_page(void *vaddr)
531bcdc68dSMike Rapoport {
541bcdc68dSMike Rapoport 	unsigned long addr = (unsigned long)vaddr;
551bcdc68dSMike Rapoport 
561bcdc68dSMike Rapoport 	if (CPU_IS_040_OR_060) {
57*e05c7b1fSMike Rapoport 		pte_t *ptep = virt_to_kpte(addr);
581bcdc68dSMike Rapoport 
591bcdc68dSMike Rapoport 		*ptep = pte_mknocache(*ptep);
601bcdc68dSMike Rapoport 	}
611bcdc68dSMike Rapoport }
621bcdc68dSMike Rapoport 
631bcdc68dSMike Rapoport static inline void cache_page(void *vaddr)
641bcdc68dSMike Rapoport {
651bcdc68dSMike Rapoport 	unsigned long addr = (unsigned long)vaddr;
661bcdc68dSMike Rapoport 
671bcdc68dSMike Rapoport 	if (CPU_IS_040_OR_060) {
68*e05c7b1fSMike Rapoport 		pte_t *ptep = virt_to_kpte(addr);
691bcdc68dSMike Rapoport 
701bcdc68dSMike Rapoport 		*ptep = pte_mkcache(*ptep);
711bcdc68dSMike Rapoport 	}
721bcdc68dSMike Rapoport }
7313076a29SPeter Zijlstra 
7413076a29SPeter Zijlstra /*
7513076a29SPeter Zijlstra  * Motorola 680x0 user's manual recommends using uncached memory for address
7613076a29SPeter Zijlstra  * translation tables.
7713076a29SPeter Zijlstra  *
7813076a29SPeter Zijlstra  * Seeing how the MMU can be external on (some of) these chips, that seems like
7913076a29SPeter Zijlstra  * a very important recommendation to follow. Provide some helpers to combat
8013076a29SPeter Zijlstra  * 'variation' amongst the users of this.
8113076a29SPeter Zijlstra  */
8213076a29SPeter Zijlstra 
8313076a29SPeter Zijlstra void mmu_page_ctor(void *page)
8413076a29SPeter Zijlstra {
8513076a29SPeter Zijlstra 	__flush_page_to_ram(page);
8613076a29SPeter Zijlstra 	flush_tlb_kernel_page(page);
8713076a29SPeter Zijlstra 	nocache_page(page);
8813076a29SPeter Zijlstra }
8913076a29SPeter Zijlstra 
9013076a29SPeter Zijlstra void mmu_page_dtor(void *page)
9113076a29SPeter Zijlstra {
9213076a29SPeter Zijlstra 	cache_page(page);
9313076a29SPeter Zijlstra }
9413076a29SPeter Zijlstra 
955ad272abSPeter Zijlstra /* ++andreas: {get,free}_pointer_table rewritten to use unused fields from
965ad272abSPeter Zijlstra    struct page instead of separately kmalloced struct.  Stolen from
975ad272abSPeter Zijlstra    arch/sparc/mm/srmmu.c ... */
985ad272abSPeter Zijlstra 
995ad272abSPeter Zijlstra typedef struct list_head ptable_desc;
1000e071ee6SPeter Zijlstra 
1010e071ee6SPeter Zijlstra static struct list_head ptable_list[2] = {
1020e071ee6SPeter Zijlstra 	LIST_HEAD_INIT(ptable_list[0]),
1030e071ee6SPeter Zijlstra 	LIST_HEAD_INIT(ptable_list[1]),
1040e071ee6SPeter Zijlstra };
1055ad272abSPeter Zijlstra 
1065ad272abSPeter Zijlstra #define PD_PTABLE(page) ((ptable_desc *)&(virt_to_page(page)->lru))
1075ad272abSPeter Zijlstra #define PD_PAGE(ptable) (list_entry(ptable, struct page, lru))
1080e071ee6SPeter Zijlstra #define PD_MARKBITS(dp) (*(unsigned int *)&PD_PAGE(dp)->index)
1095ad272abSPeter Zijlstra 
1100e071ee6SPeter Zijlstra static const int ptable_shift[2] = {
1110e071ee6SPeter Zijlstra 	7+2, /* PGD, PMD */
1120e071ee6SPeter Zijlstra 	6+2, /* PTE */
1130e071ee6SPeter Zijlstra };
1145ad272abSPeter Zijlstra 
1150e071ee6SPeter Zijlstra #define ptable_size(type) (1U << ptable_shift[type])
1160e071ee6SPeter Zijlstra #define ptable_mask(type) ((1U << (PAGE_SIZE / ptable_size(type))) - 1)
1170e071ee6SPeter Zijlstra 
1180e071ee6SPeter Zijlstra void __init init_pointer_table(void *table, int type)
1195ad272abSPeter Zijlstra {
1205ad272abSPeter Zijlstra 	ptable_desc *dp;
1210e071ee6SPeter Zijlstra 	unsigned long ptable = (unsigned long)table;
1225ad272abSPeter Zijlstra 	unsigned long page = ptable & PAGE_MASK;
1230e071ee6SPeter Zijlstra 	unsigned int mask = 1U << ((ptable - page)/ptable_size(type));
1245ad272abSPeter Zijlstra 
1255ad272abSPeter Zijlstra 	dp = PD_PTABLE(page);
1265ad272abSPeter Zijlstra 	if (!(PD_MARKBITS(dp) & mask)) {
1270e071ee6SPeter Zijlstra 		PD_MARKBITS(dp) = ptable_mask(type);
1280e071ee6SPeter Zijlstra 		list_add(dp, &ptable_list[type]);
1295ad272abSPeter Zijlstra 	}
1305ad272abSPeter Zijlstra 
1315ad272abSPeter Zijlstra 	PD_MARKBITS(dp) &= ~mask;
1325ad272abSPeter Zijlstra 	pr_debug("init_pointer_table: %lx, %x\n", ptable, PD_MARKBITS(dp));
1335ad272abSPeter Zijlstra 
1345ad272abSPeter Zijlstra 	/* unreserve the page so it's possible to free that page */
1355ad272abSPeter Zijlstra 	__ClearPageReserved(PD_PAGE(dp));
1365ad272abSPeter Zijlstra 	init_page_count(PD_PAGE(dp));
1375ad272abSPeter Zijlstra 
1385ad272abSPeter Zijlstra 	return;
1395ad272abSPeter Zijlstra }
1405ad272abSPeter Zijlstra 
1410e071ee6SPeter Zijlstra void *get_pointer_table(int type)
1425ad272abSPeter Zijlstra {
1430e071ee6SPeter Zijlstra 	ptable_desc *dp = ptable_list[type].next;
1440e071ee6SPeter Zijlstra 	unsigned int mask = list_empty(&ptable_list[type]) ? 0 : PD_MARKBITS(dp);
1450e071ee6SPeter Zijlstra 	unsigned int tmp, off;
1465ad272abSPeter Zijlstra 
1475ad272abSPeter Zijlstra 	/*
1485ad272abSPeter Zijlstra 	 * For a pointer table for a user process address space, a
1495ad272abSPeter Zijlstra 	 * table is taken from a page allocated for the purpose.  Each
1505ad272abSPeter Zijlstra 	 * page can hold 8 pointer tables.  The page is remapped in
1515ad272abSPeter Zijlstra 	 * virtual address space to be noncacheable.
1525ad272abSPeter Zijlstra 	 */
1535ad272abSPeter Zijlstra 	if (mask == 0) {
1545ad272abSPeter Zijlstra 		void *page;
1555ad272abSPeter Zijlstra 		ptable_desc *new;
1565ad272abSPeter Zijlstra 
1575ad272abSPeter Zijlstra 		if (!(page = (void *)get_zeroed_page(GFP_KERNEL)))
1585ad272abSPeter Zijlstra 			return NULL;
1595ad272abSPeter Zijlstra 
1600e071ee6SPeter Zijlstra 		if (type == TABLE_PTE) {
1610e071ee6SPeter Zijlstra 			/*
1620e071ee6SPeter Zijlstra 			 * m68k doesn't have SPLIT_PTE_PTLOCKS for not having
1630e071ee6SPeter Zijlstra 			 * SMP.
1640e071ee6SPeter Zijlstra 			 */
1650e071ee6SPeter Zijlstra 			pgtable_pte_page_ctor(virt_to_page(page));
1660e071ee6SPeter Zijlstra 		}
1670e071ee6SPeter Zijlstra 
1685ad272abSPeter Zijlstra 		mmu_page_ctor(page);
1695ad272abSPeter Zijlstra 
1705ad272abSPeter Zijlstra 		new = PD_PTABLE(page);
1710e071ee6SPeter Zijlstra 		PD_MARKBITS(new) = ptable_mask(type) - 1;
1725ad272abSPeter Zijlstra 		list_add_tail(new, dp);
1735ad272abSPeter Zijlstra 
1745ad272abSPeter Zijlstra 		return (pmd_t *)page;
1755ad272abSPeter Zijlstra 	}
1765ad272abSPeter Zijlstra 
1770e071ee6SPeter Zijlstra 	for (tmp = 1, off = 0; (mask & tmp) == 0; tmp <<= 1, off += ptable_size(type))
1785ad272abSPeter Zijlstra 		;
1795ad272abSPeter Zijlstra 	PD_MARKBITS(dp) = mask & ~tmp;
1805ad272abSPeter Zijlstra 	if (!PD_MARKBITS(dp)) {
1815ad272abSPeter Zijlstra 		/* move to end of list */
1820e071ee6SPeter Zijlstra 		list_move_tail(dp, &ptable_list[type]);
1835ad272abSPeter Zijlstra 	}
1840e071ee6SPeter Zijlstra 	return page_address(PD_PAGE(dp)) + off;
1855ad272abSPeter Zijlstra }
1865ad272abSPeter Zijlstra 
1870e071ee6SPeter Zijlstra int free_pointer_table(void *table, int type)
1885ad272abSPeter Zijlstra {
1895ad272abSPeter Zijlstra 	ptable_desc *dp;
1900e071ee6SPeter Zijlstra 	unsigned long ptable = (unsigned long)table;
1910e071ee6SPeter Zijlstra 	unsigned long page = ptable & PAGE_MASK;
1920e071ee6SPeter Zijlstra 	unsigned int mask = 1U << ((ptable - page)/ptable_size(type));
1935ad272abSPeter Zijlstra 
1945ad272abSPeter Zijlstra 	dp = PD_PTABLE(page);
1955ad272abSPeter Zijlstra 	if (PD_MARKBITS (dp) & mask)
1965ad272abSPeter Zijlstra 		panic ("table already free!");
1975ad272abSPeter Zijlstra 
1985ad272abSPeter Zijlstra 	PD_MARKBITS (dp) |= mask;
1995ad272abSPeter Zijlstra 
2000e071ee6SPeter Zijlstra 	if (PD_MARKBITS(dp) == ptable_mask(type)) {
2015ad272abSPeter Zijlstra 		/* all tables in page are free, free page */
2025ad272abSPeter Zijlstra 		list_del(dp);
2035ad272abSPeter Zijlstra 		mmu_page_dtor((void *)page);
2040e071ee6SPeter Zijlstra 		if (type == TABLE_PTE)
2050e071ee6SPeter Zijlstra 			pgtable_pte_page_dtor(virt_to_page(page));
2065ad272abSPeter Zijlstra 		free_page (page);
2075ad272abSPeter Zijlstra 		return 1;
2080e071ee6SPeter Zijlstra 	} else if (ptable_list[type].next != dp) {
2095ad272abSPeter Zijlstra 		/*
2105ad272abSPeter Zijlstra 		 * move this descriptor to the front of the list, since
2115ad272abSPeter Zijlstra 		 * it has one or more free tables.
2125ad272abSPeter Zijlstra 		 */
2130e071ee6SPeter Zijlstra 		list_move(dp, &ptable_list[type]);
2145ad272abSPeter Zijlstra 	}
2155ad272abSPeter Zijlstra 	return 0;
2165ad272abSPeter Zijlstra }
2175ad272abSPeter Zijlstra 
21812d810c1SRoman Zippel /* size of memory already mapped in head.S */
219486df8bcSAndreas Schwab extern __initdata unsigned long m68k_init_mapped_size;
22012d810c1SRoman Zippel 
22112d810c1SRoman Zippel extern unsigned long availmem;
22212d810c1SRoman Zippel 
223ef9285f6SPeter Zijlstra static pte_t *last_pte_table __initdata = NULL;
224ef9285f6SPeter Zijlstra 
2251da177e4SLinus Torvalds static pte_t * __init kernel_page_table(void)
2261da177e4SLinus Torvalds {
227ef9285f6SPeter Zijlstra 	pte_t *pte_table = last_pte_table;
2281da177e4SLinus Torvalds 
229ef9285f6SPeter Zijlstra 	if (((unsigned long)last_pte_table & ~PAGE_MASK) == 0) {
230ef9285f6SPeter Zijlstra 		pte_table = (pte_t *)memblock_alloc_low(PAGE_SIZE, PAGE_SIZE);
231ef9285f6SPeter Zijlstra 		if (!pte_table) {
2328a7f97b9SMike Rapoport 			panic("%s: Failed to allocate %lu bytes align=%lx\n",
2338a7f97b9SMike Rapoport 					__func__, PAGE_SIZE, PAGE_SIZE);
2341da177e4SLinus Torvalds 		}
2351da177e4SLinus Torvalds 
236ef9285f6SPeter Zijlstra 		clear_page(pte_table);
237ef9285f6SPeter Zijlstra 		mmu_page_ctor(pte_table);
238ef9285f6SPeter Zijlstra 
239ef9285f6SPeter Zijlstra 		last_pte_table = pte_table;
240ef9285f6SPeter Zijlstra 	}
241ef9285f6SPeter Zijlstra 
242ef9285f6SPeter Zijlstra 	last_pte_table += PTRS_PER_PTE;
243ef9285f6SPeter Zijlstra 
244ef9285f6SPeter Zijlstra 	return pte_table;
245ef9285f6SPeter Zijlstra }
246ef9285f6SPeter Zijlstra 
247ef9285f6SPeter Zijlstra static pmd_t *last_pmd_table __initdata = NULL;
2481da177e4SLinus Torvalds 
2491da177e4SLinus Torvalds static pmd_t * __init kernel_ptr_table(void)
2501da177e4SLinus Torvalds {
251ef9285f6SPeter Zijlstra 	if (!last_pmd_table) {
2521da177e4SLinus Torvalds 		unsigned long pmd, last;
2531da177e4SLinus Torvalds 		int i;
2541da177e4SLinus Torvalds 
2551da177e4SLinus Torvalds 		/* Find the last ptr table that was used in head.S and
2561da177e4SLinus Torvalds 		 * reuse the remaining space in that page for further
2571da177e4SLinus Torvalds 		 * ptr tables.
2581da177e4SLinus Torvalds 		 */
2591da177e4SLinus Torvalds 		last = (unsigned long)kernel_pg_dir;
2601da177e4SLinus Torvalds 		for (i = 0; i < PTRS_PER_PGD; i++) {
26160e50f34SMike Rapoport 			pud_t *pud = (pud_t *)(&kernel_pg_dir[i]);
26260e50f34SMike Rapoport 
26360e50f34SMike Rapoport 			if (!pud_present(*pud))
2641da177e4SLinus Torvalds 				continue;
26560e50f34SMike Rapoport 			pmd = pgd_page_vaddr(kernel_pg_dir[i]);
2661da177e4SLinus Torvalds 			if (pmd > last)
2671da177e4SLinus Torvalds 				last = pmd;
2681da177e4SLinus Torvalds 		}
2691da177e4SLinus Torvalds 
270ef9285f6SPeter Zijlstra 		last_pmd_table = (pmd_t *)last;
2711da177e4SLinus Torvalds #ifdef DEBUG
272ef9285f6SPeter Zijlstra 		printk("kernel_ptr_init: %p\n", last_pmd_table);
2731da177e4SLinus Torvalds #endif
2741da177e4SLinus Torvalds 	}
2751da177e4SLinus Torvalds 
276ef9285f6SPeter Zijlstra 	last_pmd_table += PTRS_PER_PMD;
277ef9285f6SPeter Zijlstra 	if (((unsigned long)last_pmd_table & ~PAGE_MASK) == 0) {
278ef9285f6SPeter Zijlstra 		last_pmd_table = (pmd_t *)memblock_alloc_low(PAGE_SIZE,
279e8625dceSMike Rapoport 							   PAGE_SIZE);
280ef9285f6SPeter Zijlstra 		if (!last_pmd_table)
2818a7f97b9SMike Rapoport 			panic("%s: Failed to allocate %lu bytes align=%lx\n",
2828a7f97b9SMike Rapoport 			      __func__, PAGE_SIZE, PAGE_SIZE);
2831da177e4SLinus Torvalds 
284ef9285f6SPeter Zijlstra 		clear_page(last_pmd_table);
285ef9285f6SPeter Zijlstra 		mmu_page_ctor(last_pmd_table);
2861da177e4SLinus Torvalds 	}
2871da177e4SLinus Torvalds 
288ef9285f6SPeter Zijlstra 	return last_pmd_table;
2891da177e4SLinus Torvalds }
2901da177e4SLinus Torvalds 
29112d810c1SRoman Zippel static void __init map_node(int node)
2921da177e4SLinus Torvalds {
29312d810c1SRoman Zippel 	unsigned long physaddr, virtaddr, size;
2941da177e4SLinus Torvalds 	pgd_t *pgd_dir;
29560e50f34SMike Rapoport 	p4d_t *p4d_dir;
29660e50f34SMike Rapoport 	pud_t *pud_dir;
2971da177e4SLinus Torvalds 	pmd_t *pmd_dir;
2981da177e4SLinus Torvalds 	pte_t *pte_dir;
2991da177e4SLinus Torvalds 
30012d810c1SRoman Zippel 	size = m68k_memory[node].size;
30112d810c1SRoman Zippel 	physaddr = m68k_memory[node].addr;
30212d810c1SRoman Zippel 	virtaddr = (unsigned long)phys_to_virt(physaddr);
30312d810c1SRoman Zippel 	physaddr |= m68k_supervisor_cachemode |
30412d810c1SRoman Zippel 		    _PAGE_PRESENT | _PAGE_ACCESSED | _PAGE_DIRTY;
3051da177e4SLinus Torvalds 	if (CPU_IS_040_OR_060)
3061da177e4SLinus Torvalds 		physaddr |= _PAGE_GLOBAL040;
3071da177e4SLinus Torvalds 
3081da177e4SLinus Torvalds 	while (size > 0) {
3091da177e4SLinus Torvalds #ifdef DEBUG
310ef22d8abSPeter Zijlstra 		if (!(virtaddr & (PMD_SIZE-1)))
3111da177e4SLinus Torvalds 			printk ("\npa=%#lx va=%#lx ", physaddr & PAGE_MASK,
3121da177e4SLinus Torvalds 				virtaddr);
3131da177e4SLinus Torvalds #endif
3141da177e4SLinus Torvalds 		pgd_dir = pgd_offset_k(virtaddr);
3151da177e4SLinus Torvalds 		if (virtaddr && CPU_IS_020_OR_030) {
316ef22d8abSPeter Zijlstra 			if (!(virtaddr & (PGDIR_SIZE-1)) &&
317ef22d8abSPeter Zijlstra 			    size >= PGDIR_SIZE) {
3181da177e4SLinus Torvalds #ifdef DEBUG
3191da177e4SLinus Torvalds 				printk ("[very early term]");
3201da177e4SLinus Torvalds #endif
3211da177e4SLinus Torvalds 				pgd_val(*pgd_dir) = physaddr;
322ef22d8abSPeter Zijlstra 				size -= PGDIR_SIZE;
323ef22d8abSPeter Zijlstra 				virtaddr += PGDIR_SIZE;
324ef22d8abSPeter Zijlstra 				physaddr += PGDIR_SIZE;
3251da177e4SLinus Torvalds 				continue;
3261da177e4SLinus Torvalds 			}
3271da177e4SLinus Torvalds 		}
32860e50f34SMike Rapoport 		p4d_dir = p4d_offset(pgd_dir, virtaddr);
32960e50f34SMike Rapoport 		pud_dir = pud_offset(p4d_dir, virtaddr);
33060e50f34SMike Rapoport 		if (!pud_present(*pud_dir)) {
3311da177e4SLinus Torvalds 			pmd_dir = kernel_ptr_table();
3321da177e4SLinus Torvalds #ifdef DEBUG
3331da177e4SLinus Torvalds 			printk ("[new pointer %p]", pmd_dir);
3341da177e4SLinus Torvalds #endif
33560e50f34SMike Rapoport 			pud_set(pud_dir, pmd_dir);
3361da177e4SLinus Torvalds 		} else
33760e50f34SMike Rapoport 			pmd_dir = pmd_offset(pud_dir, virtaddr);
3381da177e4SLinus Torvalds 
3391da177e4SLinus Torvalds 		if (CPU_IS_020_OR_030) {
3401da177e4SLinus Torvalds 			if (virtaddr) {
3411da177e4SLinus Torvalds #ifdef DEBUG
3421da177e4SLinus Torvalds 				printk ("[early term]");
3431da177e4SLinus Torvalds #endif
344ef22d8abSPeter Zijlstra 				pmd_val(*pmd_dir) = physaddr;
345ef22d8abSPeter Zijlstra 				physaddr += PMD_SIZE;
3461da177e4SLinus Torvalds 			} else {
3471da177e4SLinus Torvalds 				int i;
3481da177e4SLinus Torvalds #ifdef DEBUG
3491da177e4SLinus Torvalds 				printk ("[zero map]");
3501da177e4SLinus Torvalds #endif
351ef9285f6SPeter Zijlstra 				pte_dir = kernel_page_table();
352ef22d8abSPeter Zijlstra 				pmd_set(pmd_dir, pte_dir);
353ef22d8abSPeter Zijlstra 
3541da177e4SLinus Torvalds 				pte_val(*pte_dir++) = 0;
3551da177e4SLinus Torvalds 				physaddr += PAGE_SIZE;
356ef22d8abSPeter Zijlstra 				for (i = 1; i < PTRS_PER_PTE; physaddr += PAGE_SIZE, i++)
3571da177e4SLinus Torvalds 					pte_val(*pte_dir++) = physaddr;
3581da177e4SLinus Torvalds 			}
359ef22d8abSPeter Zijlstra 			size -= PMD_SIZE;
360ef22d8abSPeter Zijlstra 			virtaddr += PMD_SIZE;
3611da177e4SLinus Torvalds 		} else {
3621da177e4SLinus Torvalds 			if (!pmd_present(*pmd_dir)) {
3631da177e4SLinus Torvalds #ifdef DEBUG
3641da177e4SLinus Torvalds 				printk ("[new table]");
3651da177e4SLinus Torvalds #endif
3661da177e4SLinus Torvalds 				pte_dir = kernel_page_table();
3671da177e4SLinus Torvalds 				pmd_set(pmd_dir, pte_dir);
3681da177e4SLinus Torvalds 			}
3691da177e4SLinus Torvalds 			pte_dir = pte_offset_kernel(pmd_dir, virtaddr);
3701da177e4SLinus Torvalds 
3711da177e4SLinus Torvalds 			if (virtaddr) {
3721da177e4SLinus Torvalds 				if (!pte_present(*pte_dir))
3731da177e4SLinus Torvalds 					pte_val(*pte_dir) = physaddr;
3741da177e4SLinus Torvalds 			} else
3751da177e4SLinus Torvalds 				pte_val(*pte_dir) = 0;
3761da177e4SLinus Torvalds 			size -= PAGE_SIZE;
3771da177e4SLinus Torvalds 			virtaddr += PAGE_SIZE;
3781da177e4SLinus Torvalds 			physaddr += PAGE_SIZE;
3791da177e4SLinus Torvalds 		}
3801da177e4SLinus Torvalds 
3811da177e4SLinus Torvalds 	}
3821da177e4SLinus Torvalds #ifdef DEBUG
3831da177e4SLinus Torvalds 	printk("\n");
3841da177e4SLinus Torvalds #endif
3851da177e4SLinus Torvalds }
3861da177e4SLinus Torvalds 
3871da177e4SLinus Torvalds /*
3881da177e4SLinus Torvalds  * paging_init() continues the virtual memory environment setup which
3891da177e4SLinus Torvalds  * was begun by the code in arch/head.S.
3901da177e4SLinus Torvalds  */
3911da177e4SLinus Torvalds void __init paging_init(void)
3921da177e4SLinus Torvalds {
3935d2ee1a1SMike Rapoport 	unsigned long max_zone_pfn[MAX_NR_ZONES] = { 0, };
39412d810c1SRoman Zippel 	unsigned long min_addr, max_addr;
3951008a115SMike Rapoport 	unsigned long addr;
39612d810c1SRoman Zippel 	int i;
3971da177e4SLinus Torvalds 
3981da177e4SLinus Torvalds #ifdef DEBUG
399fb425d0bSGeert Uytterhoeven 	printk ("start of paging_init (%p, %lx)\n", kernel_pg_dir, availmem);
4001da177e4SLinus Torvalds #endif
4011da177e4SLinus Torvalds 
4021da177e4SLinus Torvalds 	/* Fix the cache mode in the page descriptors for the 680[46]0.  */
4031da177e4SLinus Torvalds 	if (CPU_IS_040_OR_060) {
4041da177e4SLinus Torvalds 		int i;
4051da177e4SLinus Torvalds #ifndef mm_cachebits
4061da177e4SLinus Torvalds 		mm_cachebits = _PAGE_CACHE040;
4071da177e4SLinus Torvalds #endif
4081da177e4SLinus Torvalds 		for (i = 0; i < 16; i++)
4091da177e4SLinus Torvalds 			pgprot_val(protection_map[i]) |= _PAGE_CACHE040;
4101da177e4SLinus Torvalds 	}
4111da177e4SLinus Torvalds 
41212d810c1SRoman Zippel 	min_addr = m68k_memory[0].addr;
41312d810c1SRoman Zippel 	max_addr = min_addr + m68k_memory[0].size;
4143f08a302SMike Rapoport 	memblock_add_node(m68k_memory[0].addr, m68k_memory[0].size, 0);
41512d810c1SRoman Zippel 	for (i = 1; i < m68k_num_memory;) {
41612d810c1SRoman Zippel 		if (m68k_memory[i].addr < min_addr) {
41712d810c1SRoman Zippel 			printk("Ignoring memory chunk at 0x%lx:0x%lx before the first chunk\n",
41812d810c1SRoman Zippel 				m68k_memory[i].addr, m68k_memory[i].size);
41912d810c1SRoman Zippel 			printk("Fix your bootloader or use a memfile to make use of this area!\n");
42012d810c1SRoman Zippel 			m68k_num_memory--;
42112d810c1SRoman Zippel 			memmove(m68k_memory + i, m68k_memory + i + 1,
42279930084SGeert Uytterhoeven 				(m68k_num_memory - i) * sizeof(struct m68k_mem_info));
42312d810c1SRoman Zippel 			continue;
42412d810c1SRoman Zippel 		}
4253f08a302SMike Rapoport 		memblock_add_node(m68k_memory[i].addr, m68k_memory[i].size, i);
42612d810c1SRoman Zippel 		addr = m68k_memory[i].addr + m68k_memory[i].size;
42712d810c1SRoman Zippel 		if (addr > max_addr)
42812d810c1SRoman Zippel 			max_addr = addr;
42912d810c1SRoman Zippel 		i++;
43012d810c1SRoman Zippel 	}
43112d810c1SRoman Zippel 	m68k_memoffset = min_addr - PAGE_OFFSET;
43212d810c1SRoman Zippel 	m68k_virt_to_node_shift = fls(max_addr - min_addr - 1) - 6;
43312d810c1SRoman Zippel 
434fbe9c961SRoman Zippel 	module_fixup(NULL, __start_fixup, __stop_fixup);
435fbe9c961SRoman Zippel 	flush_icache();
436fbe9c961SRoman Zippel 
43712d810c1SRoman Zippel 	high_memory = phys_to_virt(max_addr);
4381da177e4SLinus Torvalds 
43912d810c1SRoman Zippel 	min_low_pfn = availmem >> PAGE_SHIFT;
44079ae4fa5SGeert Uytterhoeven 	max_pfn = max_low_pfn = max_addr >> PAGE_SHIFT;
4411da177e4SLinus Torvalds 
4421008a115SMike Rapoport 	/* Reserve kernel text/data/bss and the memory allocated in head.S */
4431008a115SMike Rapoport 	memblock_reserve(m68k_memory[0].addr, availmem - m68k_memory[0].addr);
4441da177e4SLinus Torvalds 
44512d810c1SRoman Zippel 	/*
44612d810c1SRoman Zippel 	 * Map the physical memory available into the kernel virtual
4471008a115SMike Rapoport 	 * address space. Make sure memblock will not try to allocate
4481008a115SMike Rapoport 	 * pages beyond the memory we already mapped in head.S
44912d810c1SRoman Zippel 	 */
4501008a115SMike Rapoport 	memblock_set_bottom_up(true);
45112d810c1SRoman Zippel 
4521008a115SMike Rapoport 	for (i = 0; i < m68k_num_memory; i++) {
4531008a115SMike Rapoport 		m68k_setup_node(i);
45412d810c1SRoman Zippel 		map_node(i);
4551008a115SMike Rapoport 	}
45612d810c1SRoman Zippel 
4571da177e4SLinus Torvalds 	flush_tlb_all();
4581da177e4SLinus Torvalds 
4591da177e4SLinus Torvalds 	/*
4601da177e4SLinus Torvalds 	 * initialize the bad page table and bad page to point
4611da177e4SLinus Torvalds 	 * to a couple of allocated pages
4621da177e4SLinus Torvalds 	 */
46315c3c114SMike Rapoport 	empty_zero_page = memblock_alloc(PAGE_SIZE, PAGE_SIZE);
4648a7f97b9SMike Rapoport 	if (!empty_zero_page)
4658a7f97b9SMike Rapoport 		panic("%s: Failed to allocate %lu bytes align=0x%lx\n",
4668a7f97b9SMike Rapoport 		      __func__, PAGE_SIZE, PAGE_SIZE);
4671da177e4SLinus Torvalds 
4681da177e4SLinus Torvalds 	/*
4691da177e4SLinus Torvalds 	 * Set up SFC/DFC registers
4701da177e4SLinus Torvalds 	 */
4711da177e4SLinus Torvalds 	set_fs(KERNEL_DS);
4721da177e4SLinus Torvalds 
4731da177e4SLinus Torvalds #ifdef DEBUG
4741da177e4SLinus Torvalds 	printk ("before free_area_init\n");
4751da177e4SLinus Torvalds #endif
4765d2ee1a1SMike Rapoport 	for (i = 0; i < m68k_num_memory; i++)
4774aac0b48SMichael Schmitz 		if (node_present_pages(i))
4784aac0b48SMichael Schmitz 			node_set_state(i, N_NORMAL_MEMORY);
4795d2ee1a1SMike Rapoport 
4805d2ee1a1SMike Rapoport 	max_zone_pfn[ZONE_DMA] = memblock_end_of_DRAM();
4815d2ee1a1SMike Rapoport 	free_area_init(max_zone_pfn);
4821da177e4SLinus Torvalds }
483