sh: Merge the legacy PMB mapping and entry synchronization code.
[deliverable/linux.git] / arch / sh / mm / pmb.c
index 280f6a166035fe9c418dd831e1f188eb6a923e6b..f822f83418e40723327f87dd9a70d14e0cda06df 100644 (file)
@@ -3,11 +3,8 @@
  *
  * Privileged Space Mapping Buffer (PMB) Support.
  *
- * Copyright (C) 2005, 2006, 2007 Paul Mundt
- *
- * P1/P2 Section mapping definitions from map32.h, which was:
- *
- *     Copyright 2003 (c) Lineo Solutions,Inc.
+ * Copyright (C) 2005 - 2010  Paul Mundt
+ * Copyright (C) 2010  Matt Fleming
  *
  * This file is subject to the terms and conditions of the GNU General Public
  * License.  See the file "COPYING" in the main directory of this archive
@@ -80,7 +77,7 @@ static struct pmb_entry *pmb_alloc(unsigned long vpn, unsigned long ppn,
                if (pos < 0)
                        return ERR_PTR(pos);
        } else {
-               if (test_bit(entry, &pmb_map))
+               if (test_and_set_bit(entry, &pmb_map))
                        return ERR_PTR(-ENOSPC);
                pos = entry;
        }
@@ -115,7 +112,7 @@ static void pmb_free(struct pmb_entry *pmbe)
 static void __set_pmb_entry(unsigned long vpn, unsigned long ppn,
                            unsigned long flags, int pos)
 {
-       ctrl_outl(vpn | PMB_V, mk_pmb_addr(pos));
+       __raw_writel(vpn | PMB_V, mk_pmb_addr(pos));
 
 #ifdef CONFIG_CACHE_WRITETHROUGH
        /*
@@ -127,17 +124,17 @@ static void __set_pmb_entry(unsigned long vpn, unsigned long ppn,
                flags |= PMB_WT;
 #endif
 
-       ctrl_outl(ppn | flags | PMB_V, mk_pmb_data(pos));
+       __raw_writel(ppn | flags | PMB_V, mk_pmb_data(pos));
 }
 
-static void __uses_jump_to_uncached set_pmb_entry(struct pmb_entry *pmbe)
+static void set_pmb_entry(struct pmb_entry *pmbe)
 {
        jump_to_uncached();
        __set_pmb_entry(pmbe->vpn, pmbe->ppn, pmbe->flags, pmbe->entry);
        back_to_cached();
 }
 
-static void __uses_jump_to_uncached clear_pmb_entry(struct pmb_entry *pmbe)
+static void clear_pmb_entry(struct pmb_entry *pmbe)
 {
        unsigned int entry = pmbe->entry;
        unsigned long addr;
@@ -149,10 +146,10 @@ static void __uses_jump_to_uncached clear_pmb_entry(struct pmb_entry *pmbe)
 
        /* Clear V-bit */
        addr = mk_pmb_addr(entry);
-       ctrl_outl(ctrl_inl(addr) & ~PMB_V, addr);
+       __raw_writel(__raw_readl(addr) & ~PMB_V, addr);
 
        addr = mk_pmb_data(entry);
-       ctrl_outl(ctrl_inl(addr) & ~PMB_V, addr);
+       __raw_writel(__raw_readl(addr) & ~PMB_V, addr);
 
        back_to_cached();
 }
@@ -279,91 +276,146 @@ static void __pmb_unmap(struct pmb_entry *pmbe)
        } while (pmbe);
 }
 
-#ifdef CONFIG_PMB
-int __uses_jump_to_uncached pmb_init(void)
+static inline void
+pmb_log_mapping(unsigned long data_val, unsigned long vpn, unsigned long ppn)
 {
-       unsigned int i;
-       long size, ret;
-
-       jump_to_uncached();
-
-       /*
-        * Insert PMB entries for the P1 and P2 areas so that, after
-        * we've switched the MMU to 32-bit mode, the semantics of P1
-        * and P2 are the same as in 29-bit mode, e.g.
-        *
-        *      P1 - provides a cached window onto physical memory
-        *      P2 - provides an uncached window onto physical memory
-        */
-       size = __MEMORY_START + __MEMORY_SIZE;
+       unsigned int size;
+       const char *sz_str;
 
-       ret = pmb_remap(P1SEG, 0x00000000, size, PMB_C);
-       BUG_ON(ret != size);
+       size = data_val & PMB_SZ_MASK;
 
-       ret = pmb_remap(P2SEG, 0x00000000, size, PMB_WT | PMB_UB);
-       BUG_ON(ret != size);
+       sz_str = (size == PMB_SZ_16M)  ? " 16MB":
+                (size == PMB_SZ_64M)  ? " 64MB":
+                (size == PMB_SZ_128M) ? "128MB":
+                                        "512MB";
 
-       ctrl_outl(0, PMB_IRMCR);
-
-       /* PMB.SE and UB[7] */
-       ctrl_outl(PASCR_SE | (1 << 7), PMB_PASCR);
-
-       /* Flush out the TLB */
-       i =  ctrl_inl(MMUCR);
-       i |= MMUCR_TI;
-       ctrl_outl(i, MMUCR);
-
-       back_to_cached();
+       pr_info("\t0x%08lx -> 0x%08lx [ %s %scached ]\n",
+               vpn >> PAGE_SHIFT, ppn >> PAGE_SHIFT, sz_str,
+               (data_val & PMB_C) ? "" : "un");
+}
 
-       return 0;
+static inline unsigned int pmb_ppn_in_range(unsigned long ppn)
+{
+       return ppn >= __pa(memory_start) && ppn < __pa(memory_end);
 }
-#else
-int __uses_jump_to_uncached pmb_init(void)
+
+static int pmb_synchronize_mappings(void)
 {
+       unsigned int applied = 0;
        int i;
-       unsigned long addr, data;
 
-       jump_to_uncached();
+       pr_info("PMB: boot mappings:\n");
 
+       /*
+        * Run through the initial boot mappings, log the established
+        * ones, and blow away anything that falls outside of the valid
+        * PPN range. Specifically, we only care about existing mappings
+        * that impact the cached/uncached sections.
+        *
+        * Note that touching these can be a bit of a minefield; the boot
+        * loader can establish multi-page mappings with the same caching
+        * attributes, so we need to ensure that we aren't modifying a
+        * mapping that we're presently executing from, or may execute
+        * from in the case of straddling page boundaries.
+        *
+        * In the future we will have to tidy up after the boot loader by
+        * jumping between the cached and uncached mappings and tearing
+        * down alternating mappings while executing from the other.
+        */
        for (i = 0; i < PMB_ENTRY_MAX; i++) {
+               unsigned long addr, data;
+               unsigned long addr_val, data_val;
+               unsigned long ppn, vpn, flags;
                struct pmb_entry *pmbe;
-               unsigned long vpn, ppn, flags;
 
-               addr = PMB_DATA + (i << PMB_E_SHIFT);
-               data = ctrl_inl(addr);
-               if (!(data & PMB_V))
+               addr = mk_pmb_addr(i);
+               data = mk_pmb_data(i);
+
+               addr_val = __raw_readl(addr);
+               data_val = __raw_readl(data);
+
+               /*
+                * Skip over any bogus entries
+                */
+               if (!(data_val & PMB_V) || !(addr_val & PMB_V))
                        continue;
 
-               if (data & PMB_C) {
+               ppn = data_val & PMB_PFN_MASK;
+               vpn = addr_val & PMB_PFN_MASK;
+
+               /*
+                * Only preserve in-range mappings.
+                */
+               if (!pmb_ppn_in_range(ppn)) {
+                       /*
+                        * Invalidate anything out of bounds.
+                        */
+                       __raw_writel(addr_val & ~PMB_V, addr);
+                       __raw_writel(data_val & ~PMB_V, data);
+                       continue;
+               }
+
+               /*
+                * Update the caching attributes if necessary
+                */
+               if (data_val & PMB_C) {
 #if defined(CONFIG_CACHE_WRITETHROUGH)
-                       data |= PMB_WT;
+                       data_val |= PMB_WT;
 #elif defined(CONFIG_CACHE_WRITEBACK)
-                       data &= ~PMB_WT;
+                       data_val &= ~PMB_WT;
 #else
-                       data &= ~(PMB_C | PMB_WT);
+                       data_val &= ~(PMB_C | PMB_WT);
 #endif
+                       __raw_writel(data_val, data);
                }
-               ctrl_outl(data, addr);
 
-               ppn = data & PMB_PFN_MASK;
+               flags = data_val & (PMB_SZ_MASK | PMB_CACHE_MASK);
 
-               flags = data & (PMB_C | PMB_WT | PMB_UB);
-               flags |= data & PMB_SZ_MASK;
+               pmbe = pmb_alloc(vpn, ppn, flags, i);
+               if (IS_ERR(pmbe)) {
+                       WARN_ON_ONCE(1);
+                       continue;
+               }
 
-               addr = PMB_ADDR + (i << PMB_E_SHIFT);
-               data = ctrl_inl(addr);
+               pmb_log_mapping(data_val, vpn, ppn);
 
-               vpn = data & PMB_PFN_MASK;
+               applied++;
+       }
 
-               pmbe = pmb_alloc(vpn, ppn, flags, i);
-               WARN_ON(IS_ERR(pmbe));
+       return (applied == 0);
+}
+
+int pmb_init(void)
+{
+       int ret;
+
+       jump_to_uncached();
+
+       /*
+        * Sync our software copy of the PMB mappings with those in
+        * hardware. The mappings in the hardware PMB were either set up
+        * by the bootloader or very early on by the kernel.
+        */
+       ret = pmb_synchronize_mappings();
+       if (unlikely(ret == 0)) {
+               back_to_cached();
+               return 0;
        }
 
+       __raw_writel(0, PMB_IRMCR);
+
+       /* Flush out the TLB */
+       __raw_writel(__raw_readl(MMUCR) | MMUCR_TI, MMUCR);
+
        back_to_cached();
 
        return 0;
 }
-#endif /* CONFIG_PMB */
+
+bool __in_29bit_mode(void)
+{
+        return (__raw_readl(PMB_PASCR) & PASCR_SE) == 0;
+}
 
 static int pmb_seq_show(struct seq_file *file, void *iter)
 {
@@ -378,8 +430,8 @@ static int pmb_seq_show(struct seq_file *file, void *iter)
                unsigned int size;
                char *sz_str = NULL;
 
-               addr = ctrl_inl(mk_pmb_addr(i));
-               data = ctrl_inl(mk_pmb_data(i));
+               addr = __raw_readl(mk_pmb_addr(i));
+               data = __raw_readl(mk_pmb_data(i));
 
                size = data & PMB_SZ_MASK;
                sz_str = (size == PMB_SZ_16M)  ? " 16MB":
@@ -462,6 +514,5 @@ static int __init pmb_sysdev_init(void)
 {
        return sysdev_driver_register(&cpu_sysdev_class, &pmb_sysdev_driver);
 }
-
 subsys_initcall(pmb_sysdev_init);
 #endif
This page took 0.033073 seconds and 5 git commands to generate.