xref: /openbmc/linux/arch/microblaze/mm/pgtable.c (revision 97da55fc)
1 /*
2  *  This file contains the routines setting up the linux page tables.
3  *
4  * Copyright (C) 2008 Michal Simek
5  * Copyright (C) 2008 PetaLogix
6  *
7  *    Copyright (C) 2007 Xilinx, Inc.  All rights reserved.
8  *
9  *  Derived from arch/ppc/mm/pgtable.c:
10  *    -- paulus
11  *
12  *  Derived from arch/ppc/mm/init.c:
13  *    Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
14  *
15  *  Modifications by Paul Mackerras (PowerMac) (paulus@cs.anu.edu.au)
16  *  and Cort Dougan (PReP) (cort@cs.nmt.edu)
17  *    Copyright (C) 1996 Paul Mackerras
18  *  Amiga/APUS changes by Jesper Skov (jskov@cygnus.co.uk).
19  *
20  *  Derived from "arch/i386/mm/init.c"
21  *    Copyright (C) 1991, 1992, 1993, 1994  Linus Torvalds
22  *
23  *  This file is subject to the terms and conditions of the GNU General
24  *  Public License.  See the file COPYING in the main directory of this
25  *  archive for more details.
26  *
27  */
28 
29 #include <linux/export.h>
30 #include <linux/kernel.h>
31 #include <linux/types.h>
32 #include <linux/vmalloc.h>
33 #include <linux/init.h>
34 
35 #include <asm/pgtable.h>
36 #include <asm/pgalloc.h>
37 #include <linux/io.h>
38 #include <asm/mmu.h>
39 #include <asm/sections.h>
40 #include <asm/fixmap.h>
41 
42 unsigned long ioremap_base;
43 unsigned long ioremap_bot;
44 EXPORT_SYMBOL(ioremap_bot);
45 
46 #ifndef CONFIG_SMP
47 struct pgtable_cache_struct quicklists;
48 #endif
49 
50 static void __iomem *__ioremap(phys_addr_t addr, unsigned long size,
51 		unsigned long flags)
52 {
53 	unsigned long v, i;
54 	phys_addr_t p;
55 	int err;
56 
57 	/*
58 	 * Choose an address to map it to.
59 	 * Once the vmalloc system is running, we use it.
60 	 * Before then, we use space going down from ioremap_base
61 	 * (ioremap_bot records where we're up to).
62 	 */
63 	p = addr & PAGE_MASK;
64 	size = PAGE_ALIGN(addr + size) - p;
65 
66 	/*
67 	 * Don't allow anybody to remap normal RAM that we're using.
68 	 * mem_init() sets high_memory so only do the check after that.
69 	 *
70 	 * However, allow remap of rootfs: TBD
71 	 */
72 	if (mem_init_done &&
73 		p >= memory_start && p < virt_to_phys(high_memory) &&
74 		!(p >= virt_to_phys((unsigned long)&__bss_stop) &&
75 		p < virt_to_phys((unsigned long)__bss_stop))) {
76 		pr_warn("__ioremap(): phys addr "PTE_FMT" is RAM lr %pf\n",
77 			(unsigned long)p, __builtin_return_address(0));
78 		return NULL;
79 	}
80 
81 	if (size == 0)
82 		return NULL;
83 
84 	/*
85 	 * Is it already mapped? If the whole area is mapped then we're
86 	 * done, otherwise remap it since we want to keep the virt addrs for
87 	 * each request contiguous.
88 	 *
89 	 * We make the assumption here that if the bottom and top
90 	 * of the range we want are mapped then it's mapped to the
91 	 * same virt address (and this is contiguous).
92 	 *  -- Cort
93 	 */
94 
95 	if (mem_init_done) {
96 		struct vm_struct *area;
97 		area = get_vm_area(size, VM_IOREMAP);
98 		if (area == NULL)
99 			return NULL;
100 		v = (unsigned long) area->addr;
101 	} else {
102 		v = (ioremap_bot -= size);
103 	}
104 
105 	if ((flags & _PAGE_PRESENT) == 0)
106 		flags |= _PAGE_KERNEL;
107 	if (flags & _PAGE_NO_CACHE)
108 		flags |= _PAGE_GUARDED;
109 
110 	err = 0;
111 	for (i = 0; i < size && err == 0; i += PAGE_SIZE)
112 		err = map_page(v + i, p + i, flags);
113 	if (err) {
114 		if (mem_init_done)
115 			vfree((void *)v);
116 		return NULL;
117 	}
118 
119 	return (void __iomem *) (v + ((unsigned long)addr & ~PAGE_MASK));
120 }
121 
122 void __iomem *ioremap(phys_addr_t addr, unsigned long size)
123 {
124 	return __ioremap(addr, size, _PAGE_NO_CACHE);
125 }
126 EXPORT_SYMBOL(ioremap);
127 
128 void iounmap(void __iomem *addr)
129 {
130 	if ((__force void *)addr > high_memory &&
131 					(unsigned long) addr < ioremap_bot)
132 		vfree((void *) (PAGE_MASK & (unsigned long) addr));
133 }
134 EXPORT_SYMBOL(iounmap);
135 
136 
137 int map_page(unsigned long va, phys_addr_t pa, int flags)
138 {
139 	pmd_t *pd;
140 	pte_t *pg;
141 	int err = -ENOMEM;
142 	/* Use upper 10 bits of VA to index the first level map */
143 	pd = pmd_offset(pgd_offset_k(va), va);
144 	/* Use middle 10 bits of VA to index the second-level map */
145 	pg = pte_alloc_kernel(pd, va); /* from powerpc - pgtable.c */
146 	/* pg = pte_alloc_kernel(&init_mm, pd, va); */
147 
148 	if (pg != NULL) {
149 		err = 0;
150 		set_pte_at(&init_mm, va, pg, pfn_pte(pa >> PAGE_SHIFT,
151 				__pgprot(flags)));
152 		if (unlikely(mem_init_done))
153 			_tlbie(va);
154 	}
155 	return err;
156 }
157 
158 /*
159  * Map in all of physical memory starting at CONFIG_KERNEL_START.
160  */
161 void __init mapin_ram(void)
162 {
163 	unsigned long v, p, s, f;
164 
165 	v = CONFIG_KERNEL_START;
166 	p = memory_start;
167 	for (s = 0; s < lowmem_size; s += PAGE_SIZE) {
168 		f = _PAGE_PRESENT | _PAGE_ACCESSED |
169 				_PAGE_SHARED | _PAGE_HWEXEC;
170 		if ((char *) v < _stext || (char *) v >= _etext)
171 			f |= _PAGE_WRENABLE;
172 		else
173 			/* On the MicroBlaze, no user access
174 			   forces R/W kernel access */
175 			f |= _PAGE_USER;
176 		map_page(v, p, f);
177 		v += PAGE_SIZE;
178 		p += PAGE_SIZE;
179 	}
180 }
181 
182 /* is x a power of 2? */
183 #define is_power_of_2(x)	((x) != 0 && (((x) & ((x) - 1)) == 0))
184 
185 /* Scan the real Linux page tables and return a PTE pointer for
186  * a virtual address in a context.
187  * Returns true (1) if PTE was found, zero otherwise.  The pointer to
188  * the PTE pointer is unmodified if PTE is not found.
189  */
190 static int get_pteptr(struct mm_struct *mm, unsigned long addr, pte_t **ptep)
191 {
192 	pgd_t	*pgd;
193 	pmd_t	*pmd;
194 	pte_t	*pte;
195 	int     retval = 0;
196 
197 	pgd = pgd_offset(mm, addr & PAGE_MASK);
198 	if (pgd) {
199 		pmd = pmd_offset(pgd, addr & PAGE_MASK);
200 		if (pmd_present(*pmd)) {
201 			pte = pte_offset_kernel(pmd, addr & PAGE_MASK);
202 			if (pte) {
203 				retval = 1;
204 				*ptep = pte;
205 			}
206 		}
207 	}
208 	return retval;
209 }
210 
211 /* Find physical address for this virtual address.  Normally used by
212  * I/O functions, but anyone can call it.
213  */
214 unsigned long iopa(unsigned long addr)
215 {
216 	unsigned long pa;
217 
218 	pte_t *pte;
219 	struct mm_struct *mm;
220 
221 	/* Allow mapping of user addresses (within the thread)
222 	 * for DMA if necessary.
223 	 */
224 	if (addr < TASK_SIZE)
225 		mm = current->mm;
226 	else
227 		mm = &init_mm;
228 
229 	pa = 0;
230 	if (get_pteptr(mm, addr, &pte))
231 		pa = (pte_val(*pte) & PAGE_MASK) | (addr & ~PAGE_MASK);
232 
233 	return pa;
234 }
235 
236 __init_refok pte_t *pte_alloc_one_kernel(struct mm_struct *mm,
237 		unsigned long address)
238 {
239 	pte_t *pte;
240 	if (mem_init_done) {
241 		pte = (pte_t *)__get_free_page(GFP_KERNEL |
242 					__GFP_REPEAT | __GFP_ZERO);
243 	} else {
244 		pte = (pte_t *)early_get_page();
245 		if (pte)
246 			clear_page(pte);
247 	}
248 	return pte;
249 }
250 
251 void __set_fixmap(enum fixed_addresses idx, phys_addr_t phys, pgprot_t flags)
252 {
253 	unsigned long address = __fix_to_virt(idx);
254 
255 	if (idx >= __end_of_fixed_addresses)
256 		BUG();
257 
258 	map_page(address, phys, pgprot_val(flags));
259 }
260