4 * Privileged Space Mapping Buffer (PMB) Support.
6 * Copyright (C) 2005 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/module.h>
19 #include <linux/slab.h>
20 #include <linux/bitops.h>
21 #include <linux/debugfs.h>
23 #include <linux/seq_file.h>
24 #include <linux/err.h>
25 #include <asm/system.h>
26 #include <asm/uaccess.h>
30 #define NR_PMB_ENTRIES 16
32 static kmem_cache_t *pmb_cache;
33 static unsigned long pmb_map;
35 static struct pmb_entry pmb_init_map[] = {
36 /* vpn ppn flags (ub/sz/c/wt) */
38 /* P1 Section Mappings */
39 { 0x80000000, 0x00000000, PMB_SZ_64M | PMB_C, },
40 { 0x84000000, 0x04000000, PMB_SZ_64M | PMB_C, },
41 { 0x88000000, 0x08000000, PMB_SZ_128M | PMB_C, },
42 { 0x90000000, 0x10000000, PMB_SZ_64M | PMB_C, },
43 { 0x94000000, 0x14000000, PMB_SZ_64M | PMB_C, },
44 { 0x98000000, 0x18000000, PMB_SZ_64M | PMB_C, },
46 /* P2 Section Mappings */
47 { 0xa0000000, 0x00000000, PMB_UB | PMB_SZ_64M | PMB_WT, },
48 { 0xa4000000, 0x04000000, PMB_UB | PMB_SZ_64M | PMB_WT, },
49 { 0xa8000000, 0x08000000, PMB_UB | PMB_SZ_128M | PMB_WT, },
50 { 0xb0000000, 0x10000000, PMB_UB | PMB_SZ_64M | PMB_WT, },
51 { 0xb4000000, 0x14000000, PMB_UB | PMB_SZ_64M | PMB_WT, },
52 { 0xb8000000, 0x18000000, PMB_UB | PMB_SZ_64M | PMB_WT, },
55 static inline unsigned long mk_pmb_entry(unsigned int entry)
57 return (entry & PMB_E_MASK) << PMB_E_SHIFT;
60 static inline unsigned long mk_pmb_addr(unsigned int entry)
62 return mk_pmb_entry(entry) | PMB_ADDR;
65 static inline unsigned long mk_pmb_data(unsigned int entry)
67 return mk_pmb_entry(entry) | PMB_DATA;
70 struct pmb_entry *pmb_alloc(unsigned long vpn, unsigned long ppn,
73 struct pmb_entry *pmbe;
75 pmbe = kmem_cache_alloc(pmb_cache, GFP_KERNEL);
77 return ERR_PTR(-ENOMEM);
86 void pmb_free(struct pmb_entry *pmbe)
88 kmem_cache_free(pmb_cache, pmbe);
92 * Must be in P2 for __set_pmb_entry()
94 int __set_pmb_entry(unsigned long vpn, unsigned long ppn,
95 unsigned long flags, int *entry)
97 unsigned int pos = *entry;
99 if (unlikely(pos == PMB_NO_ENTRY))
100 pos = find_first_zero_bit(&pmb_map, NR_PMB_ENTRIES);
103 if (unlikely(pos > NR_PMB_ENTRIES))
106 if (test_and_set_bit(pos, &pmb_map)) {
107 pos = find_first_zero_bit(&pmb_map, NR_PMB_ENTRIES);
111 ctrl_outl(vpn | PMB_V, mk_pmb_addr(pos));
113 #ifdef CONFIG_SH_WRITETHROUGH
115 * When we are in 32-bit address extended mode, CCR.CB becomes
116 * invalid, so care must be taken to manually adjust cacheable
119 if (likely(flags & PMB_C))
123 ctrl_outl(ppn | flags | PMB_V, mk_pmb_data(pos));
130 void set_pmb_entry(struct pmb_entry *pmbe)
133 __set_pmb_entry(pmbe->vpn, pmbe->ppn, pmbe->flags, &pmbe->entry);
137 void clear_pmb_entry(struct pmb_entry *pmbe)
139 unsigned int entry = pmbe->entry;
143 * Don't allow clearing of wired init entries, P1 or P2 access
144 * without a corresponding mapping in the PMB will lead to reset
147 if (unlikely(entry < ARRAY_SIZE(pmb_init_map) ||
148 entry >= NR_PMB_ENTRIES))
154 addr = mk_pmb_addr(entry);
155 ctrl_outl(ctrl_inl(addr) & ~PMB_V, addr);
157 addr = mk_pmb_data(entry);
158 ctrl_outl(ctrl_inl(addr) & ~PMB_V, addr);
162 clear_bit(entry, &pmb_map);
165 static void pmb_cache_ctor(void *pmb, kmem_cache_t *cachep, unsigned long flags)
167 memset(pmb, 0, sizeof(struct pmb_entry));
169 ((struct pmb_entry *)pmb)->entry = PMB_NO_ENTRY;
172 static int __init pmb_init(void)
174 unsigned int nr_entries = ARRAY_SIZE(pmb_init_map);
177 BUG_ON(unlikely(nr_entries >= NR_PMB_ENTRIES));
179 pmb_cache = kmem_cache_create("pmb", sizeof(struct pmb_entry),
180 0, 0, pmb_cache_ctor, NULL);
186 * Ordering is important, P2 must be mapped in the PMB before we
187 * can set PMB.SE, and P1 must be mapped before we jump back to
190 for (entry = 0; entry < nr_entries; entry++) {
191 struct pmb_entry *pmbe = pmb_init_map + entry;
193 __set_pmb_entry(pmbe->vpn, pmbe->ppn, pmbe->flags, &entry);
196 ctrl_outl(0, PMB_IRMCR);
198 /* PMB.SE and UB[7] */
199 ctrl_outl((1 << 31) | (1 << 7), PMB_PASCR);
206 arch_initcall(pmb_init);
208 #ifdef CONFIG_DEBUG_FS
209 static int pmb_seq_show(struct seq_file *file, void *iter)
213 seq_printf(file, "V: Valid, C: Cacheable, WT: Write-Through\n"
214 "CB: Copy-Back, B: Buffered, UB: Unbuffered\n");
215 seq_printf(file, "ety vpn ppn size flags\n");
217 for (i = 0; i < NR_PMB_ENTRIES; i++) {
218 unsigned long addr, data;
222 addr = ctrl_inl(mk_pmb_addr(i));
223 data = ctrl_inl(mk_pmb_data(i));
225 size = data & PMB_SZ_MASK;
226 sz_str = (size == PMB_SZ_16M) ? " 16MB":
227 (size == PMB_SZ_64M) ? " 64MB":
228 (size == PMB_SZ_128M) ? "128MB":
231 /* 02: V 0x88 0x08 128MB C CB B */
232 seq_printf(file, "%02d: %c 0x%02lx 0x%02lx %s %c %s %s\n",
233 i, ((addr & PMB_V) && (data & PMB_V)) ? 'V' : ' ',
234 (addr >> 24) & 0xff, (data >> 24) & 0xff,
235 sz_str, (data & PMB_C) ? 'C' : ' ',
236 (data & PMB_WT) ? "WT" : "CB",
237 (data & PMB_UB) ? "UB" : " B");
243 static int pmb_debugfs_open(struct inode *inode, struct file *file)
245 return single_open(file, pmb_seq_show, NULL);
248 static struct file_operations pmb_debugfs_fops = {
249 .owner = THIS_MODULE,
250 .open = pmb_debugfs_open,
253 .release = seq_release,
256 static int __init pmb_debugfs_init(void)
258 struct dentry *dentry;
260 dentry = debugfs_create_file("pmb", S_IFREG | S_IRUGO,
261 NULL, NULL, &pmb_debugfs_fops);
263 return PTR_ERR(dentry);
268 postcore_initcall(pmb_debugfs_init);