4 * Privileged Space Mapping Buffer (PMB) Support.
6 * Copyright (C) 2005 - 2010 Paul Mundt
7 * Copyright (C) 2010 Matt Fleming
9 * This file is subject to the terms and conditions of the GNU General Public
10 * License. See the file "COPYING" in the main directory of this archive
13 #include <linux/init.h>
14 #include <linux/kernel.h>
15 #include <linux/sysdev.h>
16 #include <linux/cpu.h>
17 #include <linux/module.h>
18 #include <linux/slab.h>
19 #include <linux/bitops.h>
20 #include <linux/debugfs.h>
22 #include <linux/seq_file.h>
23 #include <linux/err.h>
24 #include <asm/system.h>
25 #include <asm/uaccess.h>
26 #include <asm/pgtable.h>
29 #include <asm/mmu_context.h>
31 #define NR_PMB_ENTRIES 16
33 static void __pmb_unmap(struct pmb_entry *);
35 static struct pmb_entry pmb_entry_list[NR_PMB_ENTRIES];
36 static unsigned long pmb_map;
38 static inline unsigned long mk_pmb_entry(unsigned int entry)
40 return (entry & PMB_E_MASK) << PMB_E_SHIFT;
43 static inline unsigned long mk_pmb_addr(unsigned int entry)
45 return mk_pmb_entry(entry) | PMB_ADDR;
48 static inline unsigned long mk_pmb_data(unsigned int entry)
50 return mk_pmb_entry(entry) | PMB_DATA;
53 static int pmb_alloc_entry(void)
58 pos = find_first_zero_bit(&pmb_map, NR_PMB_ENTRIES);
60 if (unlikely(pos > NR_PMB_ENTRIES))
63 if (test_and_set_bit(pos, &pmb_map))
69 static struct pmb_entry *pmb_alloc(unsigned long vpn, unsigned long ppn,
70 unsigned long flags, int entry)
72 struct pmb_entry *pmbe;
75 if (entry == PMB_NO_ENTRY) {
76 pos = pmb_alloc_entry();
80 if (test_and_set_bit(entry, &pmb_map))
81 return ERR_PTR(-ENOSPC);
85 pmbe = &pmb_entry_list[pos];
87 return ERR_PTR(-ENOMEM);
97 static void pmb_free(struct pmb_entry *pmbe)
99 int pos = pmbe->entry;
106 clear_bit(pos, &pmb_map);
110 * Must be in P2 for __set_pmb_entry()
112 static void __set_pmb_entry(unsigned long vpn, unsigned long ppn,
113 unsigned long flags, int pos)
115 __raw_writel(vpn | PMB_V, mk_pmb_addr(pos));
117 #ifdef CONFIG_CACHE_WRITETHROUGH
119 * When we are in 32-bit address extended mode, CCR.CB becomes
120 * invalid, so care must be taken to manually adjust cacheable
123 if (likely(flags & PMB_C))
127 __raw_writel(ppn | flags | PMB_V, mk_pmb_data(pos));
130 static void set_pmb_entry(struct pmb_entry *pmbe)
133 __set_pmb_entry(pmbe->vpn, pmbe->ppn, pmbe->flags, pmbe->entry);
137 static void clear_pmb_entry(struct pmb_entry *pmbe)
139 unsigned int entry = pmbe->entry;
142 if (unlikely(entry >= NR_PMB_ENTRIES))
148 addr = mk_pmb_addr(entry);
149 __raw_writel(__raw_readl(addr) & ~PMB_V, addr);
151 addr = mk_pmb_data(entry);
152 __raw_writel(__raw_readl(addr) & ~PMB_V, addr);
162 { .size = 0x20000000, .flag = PMB_SZ_512M, },
163 { .size = 0x08000000, .flag = PMB_SZ_128M, },
164 { .size = 0x04000000, .flag = PMB_SZ_64M, },
165 { .size = 0x01000000, .flag = PMB_SZ_16M, },
168 long pmb_remap(unsigned long vaddr, unsigned long phys,
169 unsigned long size, unsigned long flags)
171 struct pmb_entry *pmbp, *pmbe;
172 unsigned long wanted;
176 /* Convert typical pgprot value to the PMB equivalent */
177 if (flags & _PAGE_CACHABLE) {
178 if (flags & _PAGE_WT)
183 pmb_flags = PMB_WT | PMB_UB;
189 for (i = 0; i < ARRAY_SIZE(pmb_sizes); i++) {
190 if (size < pmb_sizes[i].size)
193 pmbe = pmb_alloc(vaddr, phys, pmb_flags | pmb_sizes[i].flag,
202 phys += pmb_sizes[i].size;
203 vaddr += pmb_sizes[i].size;
204 size -= pmb_sizes[i].size;
207 * Link adjacent entries that span multiple PMB entries
208 * for easier tear-down.
216 * Instead of trying smaller sizes on every iteration
217 * (even if we succeed in allocating space), try using
218 * pmb_sizes[i].size again.
223 if (size >= 0x1000000)
226 return wanted - size;
235 void pmb_unmap(unsigned long addr)
237 struct pmb_entry *pmbe = NULL;
240 for (i = 0; i < ARRAY_SIZE(pmb_entry_list); i++) {
241 if (test_bit(i, &pmb_map)) {
242 pmbe = &pmb_entry_list[i];
243 if (pmbe->vpn == addr)
254 static void __pmb_unmap(struct pmb_entry *pmbe)
256 BUG_ON(!test_bit(pmbe->entry, &pmb_map));
259 struct pmb_entry *pmblink = pmbe;
262 * We may be called before this pmb_entry has been
263 * entered into the PMB table via set_pmb_entry(), but
264 * that's OK because we've allocated a unique slot for
265 * this entry in pmb_alloc() (even if we haven't filled
268 * Therefore, calling clear_pmb_entry() is safe as no
269 * other mapping can be using that slot.
271 clear_pmb_entry(pmbe);
273 pmbe = pmblink->link;
280 pmb_log_mapping(unsigned long data_val, unsigned long vpn, unsigned long ppn)
285 size = data_val & PMB_SZ_MASK;
287 sz_str = (size == PMB_SZ_16M) ? " 16MB":
288 (size == PMB_SZ_64M) ? " 64MB":
289 (size == PMB_SZ_128M) ? "128MB":
292 pr_info("\t0x%08lx -> 0x%08lx [ %s %scached ]\n",
293 vpn >> PAGE_SHIFT, ppn >> PAGE_SHIFT, sz_str,
294 (data_val & PMB_C) ? "" : "un");
297 static inline unsigned int pmb_ppn_in_range(unsigned long ppn)
299 return ppn >= __pa(memory_start) && ppn < __pa(memory_end);
302 static int pmb_synchronize_mappings(void)
304 unsigned int applied = 0;
307 pr_info("PMB: boot mappings:\n");
310 * Run through the initial boot mappings, log the established
311 * ones, and blow away anything that falls outside of the valid
312 * PPN range. Specifically, we only care about existing mappings
313 * that impact the cached/uncached sections.
315 * Note that touching these can be a bit of a minefield; the boot
316 * loader can establish multi-page mappings with the same caching
317 * attributes, so we need to ensure that we aren't modifying a
318 * mapping that we're presently executing from, or may execute
319 * from in the case of straddling page boundaries.
321 * In the future we will have to tidy up after the boot loader by
322 * jumping between the cached and uncached mappings and tearing
323 * down alternating mappings while executing from the other.
325 for (i = 0; i < PMB_ENTRY_MAX; i++) {
326 unsigned long addr, data;
327 unsigned long addr_val, data_val;
328 unsigned long ppn, vpn, flags;
329 struct pmb_entry *pmbe;
331 addr = mk_pmb_addr(i);
332 data = mk_pmb_data(i);
334 addr_val = __raw_readl(addr);
335 data_val = __raw_readl(data);
338 * Skip over any bogus entries
340 if (!(data_val & PMB_V) || !(addr_val & PMB_V))
343 ppn = data_val & PMB_PFN_MASK;
344 vpn = addr_val & PMB_PFN_MASK;
347 * Only preserve in-range mappings.
349 if (!pmb_ppn_in_range(ppn)) {
351 * Invalidate anything out of bounds.
353 __raw_writel(addr_val & ~PMB_V, addr);
354 __raw_writel(data_val & ~PMB_V, data);
359 * Update the caching attributes if necessary
361 if (data_val & PMB_C) {
362 #if defined(CONFIG_CACHE_WRITETHROUGH)
364 #elif defined(CONFIG_CACHE_WRITEBACK)
367 data_val &= ~(PMB_C | PMB_WT);
369 __raw_writel(data_val, data);
372 flags = data_val & (PMB_SZ_MASK | PMB_CACHE_MASK);
374 pmbe = pmb_alloc(vpn, ppn, flags, i);
380 pmb_log_mapping(data_val, vpn, ppn);
385 return (applied == 0);
395 * Sync our software copy of the PMB mappings with those in
396 * hardware. The mappings in the hardware PMB were either set up
397 * by the bootloader or very early on by the kernel.
399 ret = pmb_synchronize_mappings();
400 if (unlikely(ret == 0)) {
405 __raw_writel(0, PMB_IRMCR);
407 /* Flush out the TLB */
408 __raw_writel(__raw_readl(MMUCR) | MMUCR_TI, MMUCR);
415 bool __in_29bit_mode(void)
417 return (__raw_readl(PMB_PASCR) & PASCR_SE) == 0;
420 static int pmb_seq_show(struct seq_file *file, void *iter)
424 seq_printf(file, "V: Valid, C: Cacheable, WT: Write-Through\n"
425 "CB: Copy-Back, B: Buffered, UB: Unbuffered\n");
426 seq_printf(file, "ety vpn ppn size flags\n");
428 for (i = 0; i < NR_PMB_ENTRIES; i++) {
429 unsigned long addr, data;
433 addr = __raw_readl(mk_pmb_addr(i));
434 data = __raw_readl(mk_pmb_data(i));
436 size = data & PMB_SZ_MASK;
437 sz_str = (size == PMB_SZ_16M) ? " 16MB":
438 (size == PMB_SZ_64M) ? " 64MB":
439 (size == PMB_SZ_128M) ? "128MB":
442 /* 02: V 0x88 0x08 128MB C CB B */
443 seq_printf(file, "%02d: %c 0x%02lx 0x%02lx %s %c %s %s\n",
444 i, ((addr & PMB_V) && (data & PMB_V)) ? 'V' : ' ',
445 (addr >> 24) & 0xff, (data >> 24) & 0xff,
446 sz_str, (data & PMB_C) ? 'C' : ' ',
447 (data & PMB_WT) ? "WT" : "CB",
448 (data & PMB_UB) ? "UB" : " B");
454 static int pmb_debugfs_open(struct inode *inode, struct file *file)
456 return single_open(file, pmb_seq_show, NULL);
459 static const struct file_operations pmb_debugfs_fops = {
460 .owner = THIS_MODULE,
461 .open = pmb_debugfs_open,
464 .release = single_release,
467 static int __init pmb_debugfs_init(void)
469 struct dentry *dentry;
471 dentry = debugfs_create_file("pmb", S_IFREG | S_IRUGO,
472 sh_debugfs_root, NULL, &pmb_debugfs_fops);
476 return PTR_ERR(dentry);
480 postcore_initcall(pmb_debugfs_init);
483 static int pmb_sysdev_suspend(struct sys_device *dev, pm_message_t state)
485 static pm_message_t prev_state;
488 /* Restore the PMB after a resume from hibernation */
489 if (state.event == PM_EVENT_ON &&
490 prev_state.event == PM_EVENT_FREEZE) {
491 struct pmb_entry *pmbe;
492 for (i = 0; i < ARRAY_SIZE(pmb_entry_list); i++) {
493 if (test_bit(i, &pmb_map)) {
494 pmbe = &pmb_entry_list[i];
503 static int pmb_sysdev_resume(struct sys_device *dev)
505 return pmb_sysdev_suspend(dev, PMSG_ON);
508 static struct sysdev_driver pmb_sysdev_driver = {
509 .suspend = pmb_sysdev_suspend,
510 .resume = pmb_sysdev_resume,
513 static int __init pmb_sysdev_init(void)
515 return sysdev_driver_register(&cpu_sysdev_class, &pmb_sysdev_driver);
517 subsys_initcall(pmb_sysdev_init);