4 * Privileged Space Mapping Buffer (PMB) Support.
6 * Copyright (C) 2005 - 2010 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 inline unsigned long mk_pmb_entry(unsigned int entry)
43 return (entry & PMB_E_MASK) << PMB_E_SHIFT;
46 static inline unsigned long mk_pmb_addr(unsigned int entry)
48 return mk_pmb_entry(entry) | PMB_ADDR;
51 static inline unsigned long mk_pmb_data(unsigned int entry)
53 return mk_pmb_entry(entry) | PMB_DATA;
56 static int pmb_alloc_entry(void)
61 pos = find_first_zero_bit(&pmb_map, NR_PMB_ENTRIES);
63 if (unlikely(pos > NR_PMB_ENTRIES))
66 if (test_and_set_bit(pos, &pmb_map))
72 static struct pmb_entry *pmb_alloc(unsigned long vpn, unsigned long ppn,
73 unsigned long flags, int entry)
75 struct pmb_entry *pmbe;
78 if (entry == PMB_NO_ENTRY) {
79 pos = pmb_alloc_entry();
83 if (test_bit(entry, &pmb_map))
84 return ERR_PTR(-ENOSPC);
88 pmbe = &pmb_entry_list[pos];
90 return ERR_PTR(-ENOMEM);
100 static void pmb_free(struct pmb_entry *pmbe)
102 int pos = pmbe->entry;
109 clear_bit(pos, &pmb_map);
113 * Must be in P2 for __set_pmb_entry()
115 static void __set_pmb_entry(unsigned long vpn, unsigned long ppn,
116 unsigned long flags, int pos)
118 ctrl_outl(vpn | PMB_V, mk_pmb_addr(pos));
120 #ifdef CONFIG_CACHE_WRITETHROUGH
122 * When we are in 32-bit address extended mode, CCR.CB becomes
123 * invalid, so care must be taken to manually adjust cacheable
126 if (likely(flags & PMB_C))
130 ctrl_outl(ppn | flags | PMB_V, mk_pmb_data(pos));
133 static void __uses_jump_to_uncached set_pmb_entry(struct pmb_entry *pmbe)
136 __set_pmb_entry(pmbe->vpn, pmbe->ppn, pmbe->flags, pmbe->entry);
140 static void __uses_jump_to_uncached clear_pmb_entry(struct pmb_entry *pmbe)
142 unsigned int entry = pmbe->entry;
145 if (unlikely(entry >= NR_PMB_ENTRIES))
151 addr = mk_pmb_addr(entry);
152 ctrl_outl(ctrl_inl(addr) & ~PMB_V, addr);
154 addr = mk_pmb_data(entry);
155 ctrl_outl(ctrl_inl(addr) & ~PMB_V, addr);
165 { .size = 0x20000000, .flag = PMB_SZ_512M, },
166 { .size = 0x08000000, .flag = PMB_SZ_128M, },
167 { .size = 0x04000000, .flag = PMB_SZ_64M, },
168 { .size = 0x01000000, .flag = PMB_SZ_16M, },
171 long pmb_remap(unsigned long vaddr, unsigned long phys,
172 unsigned long size, unsigned long flags)
174 struct pmb_entry *pmbp, *pmbe;
175 unsigned long wanted;
179 /* Convert typical pgprot value to the PMB equivalent */
180 if (flags & _PAGE_CACHABLE) {
181 if (flags & _PAGE_WT)
186 pmb_flags = PMB_WT | PMB_UB;
192 for (i = 0; i < ARRAY_SIZE(pmb_sizes); i++) {
193 if (size < pmb_sizes[i].size)
196 pmbe = pmb_alloc(vaddr, phys, pmb_flags | pmb_sizes[i].flag,
205 phys += pmb_sizes[i].size;
206 vaddr += pmb_sizes[i].size;
207 size -= pmb_sizes[i].size;
210 * Link adjacent entries that span multiple PMB entries
211 * for easier tear-down.
219 * Instead of trying smaller sizes on every iteration
220 * (even if we succeed in allocating space), try using
221 * pmb_sizes[i].size again.
226 if (size >= 0x1000000)
229 return wanted - size;
238 void pmb_unmap(unsigned long addr)
240 struct pmb_entry *pmbe = NULL;
243 for (i = 0; i < ARRAY_SIZE(pmb_entry_list); i++) {
244 if (test_bit(i, &pmb_map)) {
245 pmbe = &pmb_entry_list[i];
246 if (pmbe->vpn == addr)
257 static void __pmb_unmap(struct pmb_entry *pmbe)
259 BUG_ON(!test_bit(pmbe->entry, &pmb_map));
262 struct pmb_entry *pmblink = pmbe;
265 * We may be called before this pmb_entry has been
266 * entered into the PMB table via set_pmb_entry(), but
267 * that's OK because we've allocated a unique slot for
268 * this entry in pmb_alloc() (even if we haven't filled
271 * Therefore, calling clear_pmb_entry() is safe as no
272 * other mapping can be using that slot.
274 clear_pmb_entry(pmbe);
276 pmbe = pmblink->link;
282 #ifdef CONFIG_PMB_LEGACY
283 static int pmb_apply_legacy_mappings(void)
286 unsigned long addr, data;
287 unsigned int applied = 0;
289 for (i = 0; i < PMB_ENTRY_MAX; i++) {
290 struct pmb_entry *pmbe;
291 unsigned long vpn, ppn, flags;
293 addr = PMB_DATA + (i << PMB_E_SHIFT);
294 data = ctrl_inl(addr);
299 #if defined(CONFIG_CACHE_WRITETHROUGH)
301 #elif defined(CONFIG_CACHE_WRITEBACK)
304 data &= ~(PMB_C | PMB_WT);
307 ctrl_outl(data, addr);
309 ppn = data & PMB_PFN_MASK;
311 flags = data & (PMB_C | PMB_WT | PMB_UB);
312 flags |= data & PMB_SZ_MASK;
314 addr = PMB_ADDR + (i << PMB_E_SHIFT);
315 data = ctrl_inl(addr);
317 vpn = data & PMB_PFN_MASK;
319 pmbe = pmb_alloc(vpn, ppn, flags, i);
320 WARN_ON(IS_ERR(pmbe));
325 return (applied == 0);
328 static inline int pmb_apply_legacy_mappings(void)
334 int __uses_jump_to_uncached pmb_init(void)
337 unsigned long size, ret;
342 * Attempt to apply the legacy boot mappings if configured. If
343 * this is successful then we simply carry on with those and
344 * don't bother establishing additional memory mappings. Dynamic
345 * device mappings through pmb_remap() can still be bolted on
348 ret = pmb_apply_legacy_mappings();
355 * Insert PMB entries for the P1 and P2 areas so that, after
356 * we've switched the MMU to 32-bit mode, the semantics of P1
357 * and P2 are the same as in 29-bit mode, e.g.
359 * P1 - provides a cached window onto physical memory
360 * P2 - provides an uncached window onto physical memory
362 size = (unsigned long)__MEMORY_START + __MEMORY_SIZE;
364 ret = pmb_remap(P1SEG, 0x00000000, size, PMB_C);
367 ret = pmb_remap(P2SEG, 0x00000000, size, PMB_WT | PMB_UB);
370 ctrl_outl(0, PMB_IRMCR);
372 /* PMB.SE and UB[7] */
373 ctrl_outl(PASCR_SE | (1 << 7), PMB_PASCR);
375 /* Flush out the TLB */
385 static int pmb_seq_show(struct seq_file *file, void *iter)
389 seq_printf(file, "V: Valid, C: Cacheable, WT: Write-Through\n"
390 "CB: Copy-Back, B: Buffered, UB: Unbuffered\n");
391 seq_printf(file, "ety vpn ppn size flags\n");
393 for (i = 0; i < NR_PMB_ENTRIES; i++) {
394 unsigned long addr, data;
398 addr = ctrl_inl(mk_pmb_addr(i));
399 data = ctrl_inl(mk_pmb_data(i));
401 size = data & PMB_SZ_MASK;
402 sz_str = (size == PMB_SZ_16M) ? " 16MB":
403 (size == PMB_SZ_64M) ? " 64MB":
404 (size == PMB_SZ_128M) ? "128MB":
407 /* 02: V 0x88 0x08 128MB C CB B */
408 seq_printf(file, "%02d: %c 0x%02lx 0x%02lx %s %c %s %s\n",
409 i, ((addr & PMB_V) && (data & PMB_V)) ? 'V' : ' ',
410 (addr >> 24) & 0xff, (data >> 24) & 0xff,
411 sz_str, (data & PMB_C) ? 'C' : ' ',
412 (data & PMB_WT) ? "WT" : "CB",
413 (data & PMB_UB) ? "UB" : " B");
419 static int pmb_debugfs_open(struct inode *inode, struct file *file)
421 return single_open(file, pmb_seq_show, NULL);
424 static const struct file_operations pmb_debugfs_fops = {
425 .owner = THIS_MODULE,
426 .open = pmb_debugfs_open,
429 .release = single_release,
432 static int __init pmb_debugfs_init(void)
434 struct dentry *dentry;
436 dentry = debugfs_create_file("pmb", S_IFREG | S_IRUGO,
437 sh_debugfs_root, NULL, &pmb_debugfs_fops);
441 return PTR_ERR(dentry);
445 postcore_initcall(pmb_debugfs_init);
448 static int pmb_sysdev_suspend(struct sys_device *dev, pm_message_t state)
450 static pm_message_t prev_state;
453 /* Restore the PMB after a resume from hibernation */
454 if (state.event == PM_EVENT_ON &&
455 prev_state.event == PM_EVENT_FREEZE) {
456 struct pmb_entry *pmbe;
457 for (i = 0; i < ARRAY_SIZE(pmb_entry_list); i++) {
458 if (test_bit(i, &pmb_map)) {
459 pmbe = &pmb_entry_list[i];
468 static int pmb_sysdev_resume(struct sys_device *dev)
470 return pmb_sysdev_suspend(dev, PMSG_ON);
473 static struct sysdev_driver pmb_sysdev_driver = {
474 .suspend = pmb_sysdev_suspend,
475 .resume = pmb_sysdev_resume,
478 static int __init pmb_sysdev_init(void)
480 return sysdev_driver_register(&cpu_sysdev_class, &pmb_sysdev_driver);
482 subsys_initcall(pmb_sysdev_init);