4 * Privileged Space Mapping Buffer (PMB) Support.
6 * Copyright (C) 2005, 2006, 2007 Paul Mundt
8 * P1/P2 Section mapping definitions from map32.h, which was:
10 * Copyright 2003 (c) Lineo Solutions,Inc.
12 * This file is subject to the terms and conditions of the GNU General Public
13 * License. See the file "COPYING" in the main directory of this archive
16 #include <linux/init.h>
17 #include <linux/kernel.h>
18 #include <linux/sysdev.h>
19 #include <linux/cpu.h>
20 #include <linux/module.h>
21 #include <linux/slab.h>
22 #include <linux/bitops.h>
23 #include <linux/debugfs.h>
25 #include <linux/seq_file.h>
26 #include <linux/err.h>
27 #include <asm/system.h>
28 #include <asm/uaccess.h>
29 #include <asm/pgtable.h>
32 #include <asm/mmu_context.h>
34 #define NR_PMB_ENTRIES 16
36 static void __pmb_unmap(struct pmb_entry *);
38 static struct pmb_entry pmb_entry_list[NR_PMB_ENTRIES];
39 static unsigned long pmb_map;
41 static struct pmb_entry pmb_init_map[] = {
42 /* vpn ppn flags (ub/sz/c/wt) */
44 /* P1 Section Mappings */
45 { 0x80000000, 0x00000000, PMB_SZ_64M | PMB_C, },
46 { 0x84000000, 0x04000000, PMB_SZ_64M | PMB_C, },
47 { 0x88000000, 0x08000000, PMB_SZ_128M | PMB_C, },
48 { 0x90000000, 0x10000000, PMB_SZ_64M | PMB_C, },
49 { 0x94000000, 0x14000000, PMB_SZ_64M | PMB_C, },
50 { 0x98000000, 0x18000000, PMB_SZ_64M | PMB_C, },
52 /* P2 Section Mappings */
53 { 0xa0000000, 0x00000000, PMB_UB | PMB_SZ_64M | PMB_WT, },
54 { 0xa4000000, 0x04000000, PMB_UB | PMB_SZ_64M | PMB_WT, },
55 { 0xa8000000, 0x08000000, PMB_UB | PMB_SZ_128M | PMB_WT, },
56 { 0xb0000000, 0x10000000, PMB_UB | PMB_SZ_64M | PMB_WT, },
57 { 0xb4000000, 0x14000000, PMB_UB | PMB_SZ_64M | PMB_WT, },
58 { 0xb8000000, 0x18000000, PMB_UB | PMB_SZ_64M | PMB_WT, },
61 static inline unsigned long mk_pmb_entry(unsigned int entry)
63 return (entry & PMB_E_MASK) << PMB_E_SHIFT;
66 static inline unsigned long mk_pmb_addr(unsigned int entry)
68 return mk_pmb_entry(entry) | PMB_ADDR;
71 static inline unsigned long mk_pmb_data(unsigned int entry)
73 return mk_pmb_entry(entry) | PMB_DATA;
76 static int pmb_alloc_entry(void)
81 pos = find_first_zero_bit(&pmb_map, NR_PMB_ENTRIES);
83 if (unlikely(pos > NR_PMB_ENTRIES))
86 if (test_and_set_bit(pos, &pmb_map))
92 static struct pmb_entry *pmb_alloc(unsigned long vpn, unsigned long ppn,
95 struct pmb_entry *pmbe;
98 pos = pmb_alloc_entry();
102 pmbe = &pmb_entry_list[pos];
104 return ERR_PTR(-ENOMEM);
114 static void pmb_free(struct pmb_entry *pmbe)
116 int pos = pmbe->entry;
123 clear_bit(pos, &pmb_map);
127 * Must be in P2 for __set_pmb_entry()
129 static void __set_pmb_entry(unsigned long vpn, unsigned long ppn,
130 unsigned long flags, int pos)
132 ctrl_outl(vpn | PMB_V, mk_pmb_addr(pos));
134 #ifdef CONFIG_CACHE_WRITETHROUGH
136 * When we are in 32-bit address extended mode, CCR.CB becomes
137 * invalid, so care must be taken to manually adjust cacheable
140 if (likely(flags & PMB_C))
144 ctrl_outl(ppn | flags | PMB_V, mk_pmb_data(pos));
147 static void __uses_jump_to_uncached set_pmb_entry(struct pmb_entry *pmbe)
150 __set_pmb_entry(pmbe->vpn, pmbe->ppn, pmbe->flags, pmbe->entry);
154 static void __uses_jump_to_uncached clear_pmb_entry(struct pmb_entry *pmbe)
156 unsigned int entry = pmbe->entry;
160 * Don't allow clearing of wired init entries, P1 or P2 access
161 * without a corresponding mapping in the PMB will lead to reset
164 if (unlikely(entry < ARRAY_SIZE(pmb_init_map) ||
165 entry >= NR_PMB_ENTRIES))
171 addr = mk_pmb_addr(entry);
172 ctrl_outl(ctrl_inl(addr) & ~PMB_V, addr);
174 addr = mk_pmb_data(entry);
175 ctrl_outl(ctrl_inl(addr) & ~PMB_V, addr);
185 { .size = 0x20000000, .flag = PMB_SZ_512M, },
186 { .size = 0x08000000, .flag = PMB_SZ_128M, },
187 { .size = 0x04000000, .flag = PMB_SZ_64M, },
188 { .size = 0x01000000, .flag = PMB_SZ_16M, },
191 long pmb_remap(unsigned long vaddr, unsigned long phys,
192 unsigned long size, unsigned long flags)
194 struct pmb_entry *pmbp, *pmbe;
195 unsigned long wanted;
199 /* Convert typical pgprot value to the PMB equivalent */
200 if (flags & _PAGE_CACHABLE) {
201 if (flags & _PAGE_WT)
206 pmb_flags = PMB_WT | PMB_UB;
212 for (i = 0; i < ARRAY_SIZE(pmb_sizes); i++) {
213 if (size < pmb_sizes[i].size)
216 pmbe = pmb_alloc(vaddr, phys, pmb_flags | pmb_sizes[i].flag);
224 phys += pmb_sizes[i].size;
225 vaddr += pmb_sizes[i].size;
226 size -= pmb_sizes[i].size;
229 * Link adjacent entries that span multiple PMB entries
230 * for easier tear-down.
238 * Instead of trying smaller sizes on every iteration
239 * (even if we succeed in allocating space), try using
240 * pmb_sizes[i].size again.
245 if (size >= 0x1000000)
248 return wanted - size;
257 void pmb_unmap(unsigned long addr)
259 struct pmb_entry *pmbe = NULL;
262 for (i = 0; i < ARRAY_SIZE(pmb_entry_list); i++) {
263 if (test_bit(i, &pmb_map)) {
264 pmbe = &pmb_entry_list[i];
265 if (pmbe->vpn == addr)
276 static void __pmb_unmap(struct pmb_entry *pmbe)
278 BUG_ON(!test_bit(pmbe->entry, &pmb_map));
281 struct pmb_entry *pmblink = pmbe;
284 * We may be called before this pmb_entry has been
285 * entered into the PMB table via set_pmb_entry(), but
286 * that's OK because we've allocated a unique slot for
287 * this entry in pmb_alloc() (even if we haven't filled
290 * Therefore, calling clear_pmb_entry() is safe as no
291 * other mapping can be using that slot.
293 clear_pmb_entry(pmbe);
295 pmbe = pmblink->link;
301 int __uses_jump_to_uncached pmb_init(void)
303 unsigned int nr_entries = ARRAY_SIZE(pmb_init_map);
304 unsigned int entry, i;
306 BUG_ON(unlikely(nr_entries >= NR_PMB_ENTRIES));
311 * Ordering is important, P2 must be mapped in the PMB before we
312 * can set PMB.SE, and P1 must be mapped before we jump back to
315 for (entry = 0; entry < nr_entries; entry++) {
316 struct pmb_entry *pmbe = pmb_init_map + entry;
318 __set_pmb_entry(pmbe->vpn, pmbe->ppn, pmbe->flags, entry);
321 ctrl_outl(0, PMB_IRMCR);
323 /* PMB.SE and UB[7] */
324 ctrl_outl((1 << 31) | (1 << 7), PMB_PASCR);
326 /* Flush out the TLB */
336 static int pmb_seq_show(struct seq_file *file, void *iter)
340 seq_printf(file, "V: Valid, C: Cacheable, WT: Write-Through\n"
341 "CB: Copy-Back, B: Buffered, UB: Unbuffered\n");
342 seq_printf(file, "ety vpn ppn size flags\n");
344 for (i = 0; i < NR_PMB_ENTRIES; i++) {
345 unsigned long addr, data;
349 addr = ctrl_inl(mk_pmb_addr(i));
350 data = ctrl_inl(mk_pmb_data(i));
352 size = data & PMB_SZ_MASK;
353 sz_str = (size == PMB_SZ_16M) ? " 16MB":
354 (size == PMB_SZ_64M) ? " 64MB":
355 (size == PMB_SZ_128M) ? "128MB":
358 /* 02: V 0x88 0x08 128MB C CB B */
359 seq_printf(file, "%02d: %c 0x%02lx 0x%02lx %s %c %s %s\n",
360 i, ((addr & PMB_V) && (data & PMB_V)) ? 'V' : ' ',
361 (addr >> 24) & 0xff, (data >> 24) & 0xff,
362 sz_str, (data & PMB_C) ? 'C' : ' ',
363 (data & PMB_WT) ? "WT" : "CB",
364 (data & PMB_UB) ? "UB" : " B");
370 static int pmb_debugfs_open(struct inode *inode, struct file *file)
372 return single_open(file, pmb_seq_show, NULL);
375 static const struct file_operations pmb_debugfs_fops = {
376 .owner = THIS_MODULE,
377 .open = pmb_debugfs_open,
380 .release = single_release,
383 static int __init pmb_debugfs_init(void)
385 struct dentry *dentry;
387 dentry = debugfs_create_file("pmb", S_IFREG | S_IRUGO,
388 sh_debugfs_root, NULL, &pmb_debugfs_fops);
392 return PTR_ERR(dentry);
396 postcore_initcall(pmb_debugfs_init);
399 static int pmb_sysdev_suspend(struct sys_device *dev, pm_message_t state)
401 static pm_message_t prev_state;
404 /* Restore the PMB after a resume from hibernation */
405 if (state.event == PM_EVENT_ON &&
406 prev_state.event == PM_EVENT_FREEZE) {
407 struct pmb_entry *pmbe;
408 for (i = 0; i < ARRAY_SIZE(pmb_entry_list); i++) {
409 if (test_bit(i, &pmb_map)) {
410 pmbe = &pmb_entry_list[i];
419 static int pmb_sysdev_resume(struct sys_device *dev)
421 return pmb_sysdev_suspend(dev, PMSG_ON);
424 static struct sysdev_driver pmb_sysdev_driver = {
425 .suspend = pmb_sysdev_suspend,
426 .resume = pmb_sysdev_resume,
429 static int __init pmb_sysdev_init(void)
431 return sysdev_driver_register(&cpu_sysdev_class, &pmb_sysdev_driver);
434 subsys_initcall(pmb_sysdev_init);