sh: Merge the legacy PMB mapping and entry synchronization code.
[deliverable/linux.git] / arch / sh / mm / pmb.c
index b796b6c021b4095c793bd4edddba2a912b8ad126..f822f83418e40723327f87dd9a70d14e0cda06df 100644 (file)
@@ -77,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;
        }
@@ -112,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
        /*
@@ -124,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;
@@ -146,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();
 }
@@ -276,41 +276,57 @@ static void __pmb_unmap(struct pmb_entry *pmbe)
        } while (pmbe);
 }
 
-#ifdef CONFIG_PMB_LEGACY
+static inline void
+pmb_log_mapping(unsigned long data_val, unsigned long vpn, unsigned long ppn)
+{
+       unsigned int size;
+       const char *sz_str;
+
+       size = data_val & PMB_SZ_MASK;
+
+       sz_str = (size == PMB_SZ_16M)  ? " 16MB":
+                (size == PMB_SZ_64M)  ? " 64MB":
+                (size == PMB_SZ_128M) ? "128MB":
+                                        "512MB";
+
+       pr_info("\t0x%08lx -> 0x%08lx [ %s %scached ]\n",
+               vpn >> PAGE_SHIFT, ppn >> PAGE_SHIFT, sz_str,
+               (data_val & PMB_C) ? "" : "un");
+}
+
 static inline unsigned int pmb_ppn_in_range(unsigned long ppn)
 {
-       return ppn >= __MEMORY_START && ppn < __MEMORY_START + __MEMORY_SIZE;
+       return ppn >= __pa(memory_start) && ppn < __pa(memory_end);
 }
 
-static int pmb_apply_legacy_mappings(void)
+static int pmb_synchronize_mappings(void)
 {
        unsigned int applied = 0;
        int i;
 
-       pr_info("PMB: Preserving legacy mappings:\n");
+       pr_info("PMB: boot mappings:\n");
 
        /*
-        * The following entries are setup by the bootloader.
+        * 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.
         *
-        * Entry       VPN         PPN      V   SZ      C       UB
-        * --------------------------------------------------------
-        *   0      0xA0000000 0x00000000   1   64MB    0       0
-        *   1      0xA4000000 0x04000000   1   16MB    0       0
-        *   2      0xA6000000 0x08000000   1   16MB    0       0
-        *   9      0x88000000 0x48000000   1  128MB    1       1
-        *  10      0x90000000 0x50000000   1  128MB    1       1
-        *  11      0x98000000 0x58000000   1  128MB    1       1
-        *  13      0xA8000000 0x48000000   1  128MB    0       0
-        *  14      0xB0000000 0x50000000   1  128MB    0       0
-        *  15      0xB8000000 0x58000000   1  128MB    0       0
+        * 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.
         *
-        * The only entries the we need are the ones that map the kernel
-        * at the cached and uncached addresses.
+        * 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;
+               unsigned long ppn, vpn, flags;
+               struct pmb_entry *pmbe;
 
                addr = mk_pmb_addr(i);
                data = mk_pmb_data(i);
@@ -330,112 +346,77 @@ static int pmb_apply_legacy_mappings(void)
                /*
                 * Only preserve in-range mappings.
                 */
-               if (pmb_ppn_in_range(ppn)) {
-                       unsigned int size;
-                       char *sz_str = NULL;
-
-                       size = data_val & PMB_SZ_MASK;
-
-                       sz_str = (size == PMB_SZ_16M)  ? " 16MB":
-                                (size == PMB_SZ_64M)  ? " 64MB":
-                                (size == PMB_SZ_128M) ? "128MB":
-                                                        "512MB";
-
-                       pr_info("\t0x%08lx -> 0x%08lx [ %s %scached ]\n",
-                               vpn >> PAGE_SHIFT, ppn >> PAGE_SHIFT, sz_str,
-                               (data_val & PMB_C) ? "" : "un");
-
-                       applied++;
-               } else {
+               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_val |= PMB_WT;
+#elif defined(CONFIG_CACHE_WRITEBACK)
+                       data_val &= ~PMB_WT;
+#else
+                       data_val &= ~(PMB_C | PMB_WT);
+#endif
+                       __raw_writel(data_val, data);
                }
+
+               flags = data_val & (PMB_SZ_MASK | PMB_CACHE_MASK);
+
+               pmbe = pmb_alloc(vpn, ppn, flags, i);
+               if (IS_ERR(pmbe)) {
+                       WARN_ON_ONCE(1);
+                       continue;
+               }
+
+               pmb_log_mapping(data_val, vpn, ppn);
+
+               applied++;
        }
 
        return (applied == 0);
 }
-#else
-static inline int pmb_apply_legacy_mappings(void)
-{
-       return 1;
-}
-#endif
 
-int __uses_jump_to_uncached pmb_init(void)
+int pmb_init(void)
 {
-       int i;
-       unsigned long addr, data;
-       unsigned long ret;
+       int ret;
 
        jump_to_uncached();
 
-       /*
-        * Attempt to apply the legacy boot mappings if configured. If
-        * this is successful then we simply carry on with those and
-        * don't bother establishing additional memory mappings. Dynamic
-        * device mappings through pmb_remap() can still be bolted on
-        * after this.
-        */
-       ret = pmb_apply_legacy_mappings();
-       if (ret == 0) {
-               back_to_cached();
-               return 0;
-       }
-
        /*
         * 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.
         */
-       for (i = 0; i < PMB_ENTRY_MAX; i++) {
-               struct pmb_entry *pmbe;
-               unsigned long vpn, ppn, flags;
-
-               addr = PMB_DATA + (i << PMB_E_SHIFT);
-               data = ctrl_inl(addr);
-               if (!(data & PMB_V))
-                       continue;
-
-               if (data & PMB_C) {
-#if defined(CONFIG_CACHE_WRITETHROUGH)
-                       data |= PMB_WT;
-#elif defined(CONFIG_CACHE_WRITEBACK)
-                       data &= ~PMB_WT;
-#else
-                       data &= ~(PMB_C | PMB_WT);
-#endif
-               }
-               ctrl_outl(data, addr);
-
-               ppn = data & PMB_PFN_MASK;
-
-               flags = data & (PMB_C | PMB_WT | PMB_UB);
-               flags |= data & PMB_SZ_MASK;
-
-               addr = PMB_ADDR + (i << PMB_E_SHIFT);
-               data = ctrl_inl(addr);
-
-               vpn = data & PMB_PFN_MASK;
-
-               pmbe = pmb_alloc(vpn, ppn, flags, i);
-               WARN_ON(IS_ERR(pmbe));
+       ret = pmb_synchronize_mappings();
+       if (unlikely(ret == 0)) {
+               back_to_cached();
+               return 0;
        }
 
-       ctrl_outl(0, PMB_IRMCR);
+       __raw_writel(0, PMB_IRMCR);
 
        /* Flush out the TLB */
-       i =  ctrl_inl(MMUCR);
-       i |= MMUCR_TI;
-       ctrl_outl(i, MMUCR);
+       __raw_writel(__raw_readl(MMUCR) | MMUCR_TI, MMUCR);
 
        back_to_cached();
 
        return 0;
 }
 
+bool __in_29bit_mode(void)
+{
+        return (__raw_readl(PMB_PASCR) & PASCR_SE) == 0;
+}
+
 static int pmb_seq_show(struct seq_file *file, void *iter)
 {
        int i;
@@ -449,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":
This page took 0.030553 seconds and 5 git commands to generate.