blob: b9d5476e1284ca74dbcea0eb3364b5475440e355 [file] [log] [blame]
Paul Mundt0c7b1df2006-09-27 15:08:07 +09001/*
2 * arch/sh/mm/pmb.c
3 *
4 * Privileged Space Mapping Buffer (PMB) Support.
5 *
Matt Fleming3d467672010-01-18 19:33:10 +09006 * Copyright (C) 2005 - 2010 Paul Mundt
7 * Copyright (C) 2010 Matt Fleming
Paul Mundt0c7b1df2006-09-27 15:08:07 +09008 *
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
11 * for more details.
12 */
13#include <linux/init.h>
14#include <linux/kernel.h>
Francesco VIRLINZIa83c0b72009-03-11 10:39:02 +000015#include <linux/sysdev.h>
16#include <linux/cpu.h>
Paul Mundt0c7b1df2006-09-27 15:08:07 +090017#include <linux/module.h>
18#include <linux/slab.h>
19#include <linux/bitops.h>
20#include <linux/debugfs.h>
21#include <linux/fs.h>
22#include <linux/seq_file.h>
23#include <linux/err.h>
Paul Mundt51becfd2010-02-17 15:33:30 +090024#include <linux/io.h>
Paul Mundtd53a0d32010-02-17 21:17:02 +090025#include <linux/spinlock.h>
26#include <linux/rwlock.h>
Paul Mundt51becfd2010-02-17 15:33:30 +090027#include <asm/sizes.h>
Paul Mundt0c7b1df2006-09-27 15:08:07 +090028#include <asm/system.h>
29#include <asm/uaccess.h>
Paul Mundtd7cdc9e2006-09-27 15:16:42 +090030#include <asm/pgtable.h>
Paul Mundt7bdda622010-02-17 13:23:00 +090031#include <asm/page.h>
Paul Mundt0c7b1df2006-09-27 15:08:07 +090032#include <asm/mmu.h>
Stuart Menefyeddeeb32007-11-26 21:32:40 +090033#include <asm/mmu_context.h>
Paul Mundt0c7b1df2006-09-27 15:08:07 +090034
Paul Mundtd53a0d32010-02-17 21:17:02 +090035struct pmb_entry;
36
37struct pmb_entry {
38 unsigned long vpn;
39 unsigned long ppn;
40 unsigned long flags;
41 unsigned long size;
42
43 spinlock_t lock;
44
45 /*
46 * 0 .. NR_PMB_ENTRIES for specific entry selection, or
47 * PMB_NO_ENTRY to search for a free one
48 */
49 int entry;
50
51 /* Adjacent entry link for contiguous multi-entry mappings */
52 struct pmb_entry *link;
53};
54
Paul Mundt51becfd2010-02-17 15:33:30 +090055static void pmb_unmap_entry(struct pmb_entry *);
Matt Flemingfc2bdef2009-10-06 21:22:22 +000056
Paul Mundtd53a0d32010-02-17 21:17:02 +090057static DEFINE_RWLOCK(pmb_rwlock);
Matt Flemingedd7de82009-10-06 21:22:29 +000058static struct pmb_entry pmb_entry_list[NR_PMB_ENTRIES];
Paul Mundt51becfd2010-02-17 15:33:30 +090059static DECLARE_BITMAP(pmb_map, NR_PMB_ENTRIES);
Paul Mundt0c7b1df2006-09-27 15:08:07 +090060
Paul Mundt51becfd2010-02-17 15:33:30 +090061static __always_inline unsigned long mk_pmb_entry(unsigned int entry)
Paul Mundt0c7b1df2006-09-27 15:08:07 +090062{
63 return (entry & PMB_E_MASK) << PMB_E_SHIFT;
64}
65
Paul Mundt51becfd2010-02-17 15:33:30 +090066static __always_inline unsigned long mk_pmb_addr(unsigned int entry)
Paul Mundt0c7b1df2006-09-27 15:08:07 +090067{
68 return mk_pmb_entry(entry) | PMB_ADDR;
69}
70
Paul Mundt51becfd2010-02-17 15:33:30 +090071static __always_inline unsigned long mk_pmb_data(unsigned int entry)
Paul Mundt0c7b1df2006-09-27 15:08:07 +090072{
73 return mk_pmb_entry(entry) | PMB_DATA;
74}
75
Matt Fleming067784f2009-10-06 21:22:23 +000076static int pmb_alloc_entry(void)
77{
Paul Mundtd53a0d32010-02-17 21:17:02 +090078 int pos;
Matt Fleming067784f2009-10-06 21:22:23 +000079
Paul Mundt51becfd2010-02-17 15:33:30 +090080 pos = find_first_zero_bit(pmb_map, NR_PMB_ENTRIES);
Paul Mundtd53a0d32010-02-17 21:17:02 +090081 if (pos >= 0 && pos < NR_PMB_ENTRIES)
82 __set_bit(pos, pmb_map);
83 else
84 pos = -ENOSPC;
Matt Fleming067784f2009-10-06 21:22:23 +000085
86 return pos;
87}
88
Matt Fleming8386aeb2009-10-06 21:22:28 +000089static struct pmb_entry *pmb_alloc(unsigned long vpn, unsigned long ppn,
Matt Fleming20b50142009-10-06 21:22:33 +000090 unsigned long flags, int entry)
Paul Mundt0c7b1df2006-09-27 15:08:07 +090091{
92 struct pmb_entry *pmbe;
Paul Mundtd53a0d32010-02-17 21:17:02 +090093 unsigned long irqflags;
94 void *ret = NULL;
Matt Fleming067784f2009-10-06 21:22:23 +000095 int pos;
96
Paul Mundtd53a0d32010-02-17 21:17:02 +090097 write_lock_irqsave(&pmb_rwlock, irqflags);
98
Matt Fleming20b50142009-10-06 21:22:33 +000099 if (entry == PMB_NO_ENTRY) {
100 pos = pmb_alloc_entry();
Paul Mundtd53a0d32010-02-17 21:17:02 +0900101 if (unlikely(pos < 0)) {
102 ret = ERR_PTR(pos);
103 goto out;
104 }
Matt Fleming20b50142009-10-06 21:22:33 +0000105 } else {
Paul Mundtd53a0d32010-02-17 21:17:02 +0900106 if (__test_and_set_bit(entry, pmb_map)) {
107 ret = ERR_PTR(-ENOSPC);
108 goto out;
109 }
110
Matt Fleming20b50142009-10-06 21:22:33 +0000111 pos = entry;
112 }
Paul Mundt0c7b1df2006-09-27 15:08:07 +0900113
Paul Mundtd53a0d32010-02-17 21:17:02 +0900114 write_unlock_irqrestore(&pmb_rwlock, irqflags);
115
Matt Flemingedd7de82009-10-06 21:22:29 +0000116 pmbe = &pmb_entry_list[pos];
Paul Mundtd53a0d32010-02-17 21:17:02 +0900117
118 spin_lock_init(&pmbe->lock);
Paul Mundt0c7b1df2006-09-27 15:08:07 +0900119
120 pmbe->vpn = vpn;
121 pmbe->ppn = ppn;
122 pmbe->flags = flags;
Matt Fleming067784f2009-10-06 21:22:23 +0000123 pmbe->entry = pos;
Paul Mundtd7813bc2010-02-17 17:56:38 +0900124 pmbe->size = 0;
Paul Mundt0c7b1df2006-09-27 15:08:07 +0900125
126 return pmbe;
Paul Mundtd53a0d32010-02-17 21:17:02 +0900127
128out:
129 write_unlock_irqrestore(&pmb_rwlock, irqflags);
130 return ret;
Paul Mundt0c7b1df2006-09-27 15:08:07 +0900131}
132
Matt Fleming8386aeb2009-10-06 21:22:28 +0000133static void pmb_free(struct pmb_entry *pmbe)
Paul Mundt0c7b1df2006-09-27 15:08:07 +0900134{
Paul Mundtd53a0d32010-02-17 21:17:02 +0900135 __clear_bit(pmbe->entry, pmb_map);
Paul Mundtd7813bc2010-02-17 17:56:38 +0900136 pmbe->entry = PMB_NO_ENTRY;
Paul Mundt0c7b1df2006-09-27 15:08:07 +0900137}
138
139/*
Paul Mundt0065b962010-02-17 18:05:23 +0900140 * Ensure that the PMB entries match our cache configuration.
141 *
142 * When we are in 32-bit address extended mode, CCR.CB becomes
143 * invalid, so care must be taken to manually adjust cacheable
144 * translations.
145 */
146static __always_inline unsigned long pmb_cache_flags(void)
147{
148 unsigned long flags = 0;
149
150#if defined(CONFIG_CACHE_WRITETHROUGH)
151 flags |= PMB_C | PMB_WT | PMB_UB;
152#elif defined(CONFIG_CACHE_WRITEBACK)
153 flags |= PMB_C;
154#endif
155
156 return flags;
157}
158
159/*
Paul Mundt51becfd2010-02-17 15:33:30 +0900160 * Must be run uncached.
Paul Mundt0c7b1df2006-09-27 15:08:07 +0900161 */
Paul Mundtd53a0d32010-02-17 21:17:02 +0900162static void __set_pmb_entry(struct pmb_entry *pmbe)
Paul Mundt0c7b1df2006-09-27 15:08:07 +0900163{
Paul Mundt0065b962010-02-17 18:05:23 +0900164 pmbe->flags &= ~PMB_CACHE_MASK;
165 pmbe->flags |= pmb_cache_flags();
166
Paul Mundt2e450642010-02-18 13:26:05 +0900167 writel_uncached(pmbe->vpn | PMB_V, mk_pmb_addr(pmbe->entry));
168 writel_uncached(pmbe->ppn | pmbe->flags | PMB_V,
169 mk_pmb_data(pmbe->entry));
Paul Mundt0c7b1df2006-09-27 15:08:07 +0900170}
171
Paul Mundtd53a0d32010-02-17 21:17:02 +0900172static void __clear_pmb_entry(struct pmb_entry *pmbe)
Paul Mundt0c7b1df2006-09-27 15:08:07 +0900173{
Paul Mundt2e450642010-02-18 13:26:05 +0900174 unsigned long addr, data;
175 unsigned long addr_val, data_val;
Paul Mundt0c7b1df2006-09-27 15:08:07 +0900176
Paul Mundt2e450642010-02-18 13:26:05 +0900177 addr = mk_pmb_addr(pmbe->entry);
178 data = mk_pmb_data(pmbe->entry);
179
180 addr_val = __raw_readl(addr);
181 data_val = __raw_readl(data);
Paul Mundt0c7b1df2006-09-27 15:08:07 +0900182
183 /* Clear V-bit */
Paul Mundt2e450642010-02-18 13:26:05 +0900184 writel_uncached(addr_val & ~PMB_V, addr);
185 writel_uncached(data_val & ~PMB_V, data);
Paul Mundt0c7b1df2006-09-27 15:08:07 +0900186}
187
Paul Mundtd53a0d32010-02-17 21:17:02 +0900188static void set_pmb_entry(struct pmb_entry *pmbe)
189{
190 unsigned long flags;
191
192 spin_lock_irqsave(&pmbe->lock, flags);
193 __set_pmb_entry(pmbe);
194 spin_unlock_irqrestore(&pmbe->lock, flags);
195}
196
Paul Mundtd7cdc9e2006-09-27 15:16:42 +0900197static struct {
198 unsigned long size;
199 int flag;
200} pmb_sizes[] = {
Paul Mundt51becfd2010-02-17 15:33:30 +0900201 { .size = SZ_512M, .flag = PMB_SZ_512M, },
202 { .size = SZ_128M, .flag = PMB_SZ_128M, },
203 { .size = SZ_64M, .flag = PMB_SZ_64M, },
204 { .size = SZ_16M, .flag = PMB_SZ_16M, },
Paul Mundtd7cdc9e2006-09-27 15:16:42 +0900205};
206
207long pmb_remap(unsigned long vaddr, unsigned long phys,
Paul Mundt7bdda622010-02-17 13:23:00 +0900208 unsigned long size, pgprot_t prot)
Paul Mundtd7cdc9e2006-09-27 15:16:42 +0900209{
Matt Flemingfc2bdef2009-10-06 21:22:22 +0000210 struct pmb_entry *pmbp, *pmbe;
Paul Mundtd7cdc9e2006-09-27 15:16:42 +0900211 unsigned long wanted;
212 int pmb_flags, i;
Matt Flemingfc2bdef2009-10-06 21:22:22 +0000213 long err;
Paul Mundt7bdda622010-02-17 13:23:00 +0900214 u64 flags;
215
216 flags = pgprot_val(prot);
Paul Mundtd7cdc9e2006-09-27 15:16:42 +0900217
Paul Mundt0065b962010-02-17 18:05:23 +0900218 pmb_flags = PMB_WT | PMB_UB;
219
Paul Mundtd7cdc9e2006-09-27 15:16:42 +0900220 /* Convert typical pgprot value to the PMB equivalent */
221 if (flags & _PAGE_CACHABLE) {
Paul Mundt0065b962010-02-17 18:05:23 +0900222 pmb_flags |= PMB_C;
223
224 if ((flags & _PAGE_WT) == 0)
225 pmb_flags &= ~(PMB_WT | PMB_UB);
226 }
Paul Mundtd7cdc9e2006-09-27 15:16:42 +0900227
228 pmbp = NULL;
229 wanted = size;
230
231again:
232 for (i = 0; i < ARRAY_SIZE(pmb_sizes); i++) {
Paul Mundtd53a0d32010-02-17 21:17:02 +0900233 unsigned long flags;
234
Paul Mundtd7cdc9e2006-09-27 15:16:42 +0900235 if (size < pmb_sizes[i].size)
236 continue;
237
Matt Fleming20b50142009-10-06 21:22:33 +0000238 pmbe = pmb_alloc(vaddr, phys, pmb_flags | pmb_sizes[i].flag,
239 PMB_NO_ENTRY);
Matt Flemingfc2bdef2009-10-06 21:22:22 +0000240 if (IS_ERR(pmbe)) {
241 err = PTR_ERR(pmbe);
242 goto out;
243 }
Paul Mundtd7cdc9e2006-09-27 15:16:42 +0900244
Paul Mundtd53a0d32010-02-17 21:17:02 +0900245 spin_lock_irqsave(&pmbe->lock, flags);
246
247 __set_pmb_entry(pmbe);
Paul Mundtd7cdc9e2006-09-27 15:16:42 +0900248
249 phys += pmb_sizes[i].size;
250 vaddr += pmb_sizes[i].size;
251 size -= pmb_sizes[i].size;
252
Paul Mundtd7813bc2010-02-17 17:56:38 +0900253 pmbe->size = pmb_sizes[i].size;
254
Paul Mundtd7cdc9e2006-09-27 15:16:42 +0900255 /*
256 * Link adjacent entries that span multiple PMB entries
257 * for easier tear-down.
258 */
Paul Mundtd53a0d32010-02-17 21:17:02 +0900259 if (likely(pmbp)) {
260 spin_lock(&pmbp->lock);
Paul Mundtd7cdc9e2006-09-27 15:16:42 +0900261 pmbp->link = pmbe;
Paul Mundtd53a0d32010-02-17 21:17:02 +0900262 spin_unlock(&pmbp->lock);
263 }
Paul Mundtd7cdc9e2006-09-27 15:16:42 +0900264
265 pmbp = pmbe;
Matt Fleminga2767cf2009-10-06 21:22:34 +0000266
267 /*
268 * Instead of trying smaller sizes on every iteration
269 * (even if we succeed in allocating space), try using
270 * pmb_sizes[i].size again.
271 */
272 i--;
Paul Mundtd53a0d32010-02-17 21:17:02 +0900273
274 spin_unlock_irqrestore(&pmbe->lock, flags);
Paul Mundtd7cdc9e2006-09-27 15:16:42 +0900275 }
276
Paul Mundtd53a0d32010-02-17 21:17:02 +0900277 if (size >= SZ_16M)
Paul Mundtd7cdc9e2006-09-27 15:16:42 +0900278 goto again;
279
280 return wanted - size;
Matt Flemingfc2bdef2009-10-06 21:22:22 +0000281
282out:
Paul Mundt51becfd2010-02-17 15:33:30 +0900283 pmb_unmap_entry(pmbp);
Matt Flemingfc2bdef2009-10-06 21:22:22 +0000284
285 return err;
Paul Mundtd7cdc9e2006-09-27 15:16:42 +0900286}
287
288void pmb_unmap(unsigned long addr)
289{
Paul Mundtd53a0d32010-02-17 21:17:02 +0900290 struct pmb_entry *pmbe = NULL;
Matt Flemingedd7de82009-10-06 21:22:29 +0000291 int i;
Paul Mundtd7cdc9e2006-09-27 15:16:42 +0900292
Paul Mundtd53a0d32010-02-17 21:17:02 +0900293 read_lock(&pmb_rwlock);
294
Matt Flemingedd7de82009-10-06 21:22:29 +0000295 for (i = 0; i < ARRAY_SIZE(pmb_entry_list); i++) {
Paul Mundt51becfd2010-02-17 15:33:30 +0900296 if (test_bit(i, pmb_map)) {
Matt Flemingedd7de82009-10-06 21:22:29 +0000297 pmbe = &pmb_entry_list[i];
Paul Mundtd53a0d32010-02-17 21:17:02 +0900298 if (pmbe->vpn == addr)
Matt Flemingedd7de82009-10-06 21:22:29 +0000299 break;
300 }
301 }
Paul Mundtd53a0d32010-02-17 21:17:02 +0900302
303 read_unlock(&pmb_rwlock);
304
305 pmb_unmap_entry(pmbe);
Paul Mundt51becfd2010-02-17 15:33:30 +0900306}
Paul Mundtd7cdc9e2006-09-27 15:16:42 +0900307
Paul Mundt51becfd2010-02-17 15:33:30 +0900308static void pmb_unmap_entry(struct pmb_entry *pmbe)
309{
Paul Mundtd53a0d32010-02-17 21:17:02 +0900310 unsigned long flags;
311
Paul Mundtd7cdc9e2006-09-27 15:16:42 +0900312 if (unlikely(!pmbe))
313 return;
314
Paul Mundtd53a0d32010-02-17 21:17:02 +0900315 write_lock_irqsave(&pmb_rwlock, flags);
Paul Mundtd7cdc9e2006-09-27 15:16:42 +0900316
317 do {
318 struct pmb_entry *pmblink = pmbe;
319
Matt Fleming067784f2009-10-06 21:22:23 +0000320 /*
321 * We may be called before this pmb_entry has been
322 * entered into the PMB table via set_pmb_entry(), but
323 * that's OK because we've allocated a unique slot for
324 * this entry in pmb_alloc() (even if we haven't filled
325 * it yet).
326 *
Paul Mundtd53a0d32010-02-17 21:17:02 +0900327 * Therefore, calling __clear_pmb_entry() is safe as no
Matt Fleming067784f2009-10-06 21:22:23 +0000328 * other mapping can be using that slot.
329 */
Paul Mundtd53a0d32010-02-17 21:17:02 +0900330 __clear_pmb_entry(pmbe);
Matt Flemingfc2bdef2009-10-06 21:22:22 +0000331
Paul Mundtd7cdc9e2006-09-27 15:16:42 +0900332 pmbe = pmblink->link;
333
334 pmb_free(pmblink);
335 } while (pmbe);
Paul Mundtd53a0d32010-02-17 21:17:02 +0900336
337 write_unlock_irqrestore(&pmb_rwlock, flags);
Paul Mundtd7cdc9e2006-09-27 15:16:42 +0900338}
339
Paul Mundtd7813bc2010-02-17 17:56:38 +0900340static __always_inline unsigned int pmb_ppn_in_range(unsigned long ppn)
Paul Mundtefd54ea2010-02-16 18:39:30 +0900341{
342 return ppn >= __pa(memory_start) && ppn < __pa(memory_end);
343}
344
345static int pmb_synchronize_mappings(void)
Matt Fleming20b50142009-10-06 21:22:33 +0000346{
Matt Fleming3d467672010-01-18 19:33:10 +0900347 unsigned int applied = 0;
Paul Mundtd7813bc2010-02-17 17:56:38 +0900348 struct pmb_entry *pmbp = NULL;
349 int i, j;
Matt Fleming3d467672010-01-18 19:33:10 +0900350
Paul Mundtefd54ea2010-02-16 18:39:30 +0900351 pr_info("PMB: boot mappings:\n");
Matt Fleming3d467672010-01-18 19:33:10 +0900352
353 /*
Paul Mundtefd54ea2010-02-16 18:39:30 +0900354 * Run through the initial boot mappings, log the established
355 * ones, and blow away anything that falls outside of the valid
356 * PPN range. Specifically, we only care about existing mappings
357 * that impact the cached/uncached sections.
Matt Fleming3d467672010-01-18 19:33:10 +0900358 *
Paul Mundtefd54ea2010-02-16 18:39:30 +0900359 * Note that touching these can be a bit of a minefield; the boot
360 * loader can establish multi-page mappings with the same caching
361 * attributes, so we need to ensure that we aren't modifying a
362 * mapping that we're presently executing from, or may execute
363 * from in the case of straddling page boundaries.
Matt Fleming3d467672010-01-18 19:33:10 +0900364 *
Paul Mundtefd54ea2010-02-16 18:39:30 +0900365 * In the future we will have to tidy up after the boot loader by
366 * jumping between the cached and uncached mappings and tearing
367 * down alternating mappings while executing from the other.
Matt Fleming3d467672010-01-18 19:33:10 +0900368 */
Paul Mundt51becfd2010-02-17 15:33:30 +0900369 for (i = 0; i < NR_PMB_ENTRIES; i++) {
Matt Fleming3d467672010-01-18 19:33:10 +0900370 unsigned long addr, data;
371 unsigned long addr_val, data_val;
Paul Mundtefd54ea2010-02-16 18:39:30 +0900372 unsigned long ppn, vpn, flags;
Paul Mundtd53a0d32010-02-17 21:17:02 +0900373 unsigned long irqflags;
Paul Mundtd7813bc2010-02-17 17:56:38 +0900374 unsigned int size;
Paul Mundtefd54ea2010-02-16 18:39:30 +0900375 struct pmb_entry *pmbe;
Matt Fleming3d467672010-01-18 19:33:10 +0900376
377 addr = mk_pmb_addr(i);
378 data = mk_pmb_data(i);
379
380 addr_val = __raw_readl(addr);
381 data_val = __raw_readl(data);
382
383 /*
384 * Skip over any bogus entries
385 */
386 if (!(data_val & PMB_V) || !(addr_val & PMB_V))
387 continue;
388
389 ppn = data_val & PMB_PFN_MASK;
390 vpn = addr_val & PMB_PFN_MASK;
391
392 /*
393 * Only preserve in-range mappings.
394 */
Paul Mundtefd54ea2010-02-16 18:39:30 +0900395 if (!pmb_ppn_in_range(ppn)) {
Matt Fleming3d467672010-01-18 19:33:10 +0900396 /*
397 * Invalidate anything out of bounds.
398 */
Paul Mundt2e450642010-02-18 13:26:05 +0900399 writel_uncached(addr_val & ~PMB_V, addr);
400 writel_uncached(data_val & ~PMB_V, data);
Paul Mundtefd54ea2010-02-16 18:39:30 +0900401 continue;
Matt Fleming3d467672010-01-18 19:33:10 +0900402 }
Paul Mundtefd54ea2010-02-16 18:39:30 +0900403
404 /*
405 * Update the caching attributes if necessary
406 */
407 if (data_val & PMB_C) {
Paul Mundt0065b962010-02-17 18:05:23 +0900408 data_val &= ~PMB_CACHE_MASK;
409 data_val |= pmb_cache_flags();
Paul Mundt2e450642010-02-18 13:26:05 +0900410
411 writel_uncached(data_val, data);
Paul Mundtefd54ea2010-02-16 18:39:30 +0900412 }
413
Paul Mundtd7813bc2010-02-17 17:56:38 +0900414 size = data_val & PMB_SZ_MASK;
415 flags = size | (data_val & PMB_CACHE_MASK);
Paul Mundtefd54ea2010-02-16 18:39:30 +0900416
417 pmbe = pmb_alloc(vpn, ppn, flags, i);
418 if (IS_ERR(pmbe)) {
419 WARN_ON_ONCE(1);
420 continue;
421 }
422
Paul Mundtd53a0d32010-02-17 21:17:02 +0900423 spin_lock_irqsave(&pmbe->lock, irqflags);
424
Paul Mundtd7813bc2010-02-17 17:56:38 +0900425 for (j = 0; j < ARRAY_SIZE(pmb_sizes); j++)
426 if (pmb_sizes[j].flag == size)
427 pmbe->size = pmb_sizes[j].size;
428
Paul Mundtd53a0d32010-02-17 21:17:02 +0900429 if (pmbp) {
430 spin_lock(&pmbp->lock);
431
432 /*
433 * Compare the previous entry against the current one to
434 * see if the entries span a contiguous mapping. If so,
435 * setup the entry links accordingly.
436 */
437 if ((pmbe->vpn == (pmbp->vpn + pmbp->size)) &&
438 (pmbe->ppn == (pmbp->ppn + pmbp->size)))
439 pmbp->link = pmbe;
440
441 spin_unlock(&pmbp->lock);
442 }
Paul Mundtd7813bc2010-02-17 17:56:38 +0900443
444 pmbp = pmbe;
445
Paul Mundtd53a0d32010-02-17 21:17:02 +0900446 spin_unlock_irqrestore(&pmbe->lock, irqflags);
447
Paul Mundtd7813bc2010-02-17 17:56:38 +0900448 pr_info("\t0x%08lx -> 0x%08lx [ %ldMB %scached ]\n",
449 vpn >> PAGE_SHIFT, ppn >> PAGE_SHIFT, pmbe->size >> 20,
450 (data_val & PMB_C) ? "" : "un");
Paul Mundtefd54ea2010-02-16 18:39:30 +0900451
452 applied++;
Matt Fleming3d467672010-01-18 19:33:10 +0900453 }
454
455 return (applied == 0);
456}
Matt Fleming3d467672010-01-18 19:33:10 +0900457
Paul Mundt2dc2f8e2010-01-21 16:05:25 +0900458int pmb_init(void)
Matt Fleming3d467672010-01-18 19:33:10 +0900459{
Paul Mundtefd54ea2010-02-16 18:39:30 +0900460 int ret;
Matt Fleming20b50142009-10-06 21:22:33 +0000461
Matt Fleming3d467672010-01-18 19:33:10 +0900462 /*
Matt Fleming3d467672010-01-18 19:33:10 +0900463 * Sync our software copy of the PMB mappings with those in
464 * hardware. The mappings in the hardware PMB were either set up
465 * by the bootloader or very early on by the kernel.
466 */
Paul Mundtefd54ea2010-02-16 18:39:30 +0900467 ret = pmb_synchronize_mappings();
Paul Mundt2e450642010-02-18 13:26:05 +0900468 if (unlikely(ret == 0))
Paul Mundtefd54ea2010-02-16 18:39:30 +0900469 return 0;
Matt Fleming20b50142009-10-06 21:22:33 +0000470
Paul Mundt2e450642010-02-18 13:26:05 +0900471 writel_uncached(0, PMB_IRMCR);
Paul Mundta0ab3662010-01-13 18:31:48 +0900472
Paul Mundta0ab3662010-01-13 18:31:48 +0900473 /* Flush out the TLB */
Paul Mundtefd54ea2010-02-16 18:39:30 +0900474 __raw_writel(__raw_readl(MMUCR) | MMUCR_TI, MMUCR);
Paul Mundt2e450642010-02-18 13:26:05 +0900475 ctrl_barrier();
Matt Fleming20b50142009-10-06 21:22:33 +0000476
477 return 0;
478}
Paul Mundt0c7b1df2006-09-27 15:08:07 +0900479
Paul Mundt2efa53b2010-01-20 16:40:48 +0900480bool __in_29bit_mode(void)
481{
482 return (__raw_readl(PMB_PASCR) & PASCR_SE) == 0;
483}
484
Paul Mundt0c7b1df2006-09-27 15:08:07 +0900485static int pmb_seq_show(struct seq_file *file, void *iter)
486{
487 int i;
488
489 seq_printf(file, "V: Valid, C: Cacheable, WT: Write-Through\n"
490 "CB: Copy-Back, B: Buffered, UB: Unbuffered\n");
491 seq_printf(file, "ety vpn ppn size flags\n");
492
493 for (i = 0; i < NR_PMB_ENTRIES; i++) {
494 unsigned long addr, data;
495 unsigned int size;
496 char *sz_str = NULL;
497
Paul Mundt9d56dd32010-01-26 12:58:40 +0900498 addr = __raw_readl(mk_pmb_addr(i));
499 data = __raw_readl(mk_pmb_data(i));
Paul Mundt0c7b1df2006-09-27 15:08:07 +0900500
501 size = data & PMB_SZ_MASK;
502 sz_str = (size == PMB_SZ_16M) ? " 16MB":
503 (size == PMB_SZ_64M) ? " 64MB":
504 (size == PMB_SZ_128M) ? "128MB":
505 "512MB";
506
507 /* 02: V 0x88 0x08 128MB C CB B */
508 seq_printf(file, "%02d: %c 0x%02lx 0x%02lx %s %c %s %s\n",
509 i, ((addr & PMB_V) && (data & PMB_V)) ? 'V' : ' ',
510 (addr >> 24) & 0xff, (data >> 24) & 0xff,
511 sz_str, (data & PMB_C) ? 'C' : ' ',
512 (data & PMB_WT) ? "WT" : "CB",
513 (data & PMB_UB) ? "UB" : " B");
514 }
515
516 return 0;
517}
518
519static int pmb_debugfs_open(struct inode *inode, struct file *file)
520{
521 return single_open(file, pmb_seq_show, NULL);
522}
523
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800524static const struct file_operations pmb_debugfs_fops = {
Paul Mundt0c7b1df2006-09-27 15:08:07 +0900525 .owner = THIS_MODULE,
526 .open = pmb_debugfs_open,
527 .read = seq_read,
528 .llseek = seq_lseek,
Li Zefan45dabf12008-06-24 13:30:23 +0800529 .release = single_release,
Paul Mundt0c7b1df2006-09-27 15:08:07 +0900530};
531
532static int __init pmb_debugfs_init(void)
533{
534 struct dentry *dentry;
535
536 dentry = debugfs_create_file("pmb", S_IFREG | S_IRUGO,
Paul Mundtb9e393c2008-03-07 17:19:58 +0900537 sh_debugfs_root, NULL, &pmb_debugfs_fops);
Zhaolei25627c72008-10-17 19:25:09 +0800538 if (!dentry)
539 return -ENOMEM;
Paul Mundt0c7b1df2006-09-27 15:08:07 +0900540 if (IS_ERR(dentry))
541 return PTR_ERR(dentry);
542
543 return 0;
544}
Paul Mundt0c7b1df2006-09-27 15:08:07 +0900545postcore_initcall(pmb_debugfs_init);
Francesco VIRLINZIa83c0b72009-03-11 10:39:02 +0000546
547#ifdef CONFIG_PM
548static int pmb_sysdev_suspend(struct sys_device *dev, pm_message_t state)
549{
550 static pm_message_t prev_state;
Matt Flemingedd7de82009-10-06 21:22:29 +0000551 int i;
Francesco VIRLINZIa83c0b72009-03-11 10:39:02 +0000552
553 /* Restore the PMB after a resume from hibernation */
554 if (state.event == PM_EVENT_ON &&
555 prev_state.event == PM_EVENT_FREEZE) {
556 struct pmb_entry *pmbe;
Paul Mundtd53a0d32010-02-17 21:17:02 +0900557
558 read_lock(&pmb_rwlock);
559
Matt Flemingedd7de82009-10-06 21:22:29 +0000560 for (i = 0; i < ARRAY_SIZE(pmb_entry_list); i++) {
Paul Mundt51becfd2010-02-17 15:33:30 +0900561 if (test_bit(i, pmb_map)) {
Matt Flemingedd7de82009-10-06 21:22:29 +0000562 pmbe = &pmb_entry_list[i];
563 set_pmb_entry(pmbe);
564 }
565 }
Paul Mundtd53a0d32010-02-17 21:17:02 +0900566
567 read_unlock(&pmb_rwlock);
Francesco VIRLINZIa83c0b72009-03-11 10:39:02 +0000568 }
Paul Mundtd53a0d32010-02-17 21:17:02 +0900569
Francesco VIRLINZIa83c0b72009-03-11 10:39:02 +0000570 prev_state = state;
Paul Mundtd53a0d32010-02-17 21:17:02 +0900571
Francesco VIRLINZIa83c0b72009-03-11 10:39:02 +0000572 return 0;
573}
574
575static int pmb_sysdev_resume(struct sys_device *dev)
576{
577 return pmb_sysdev_suspend(dev, PMSG_ON);
578}
579
580static struct sysdev_driver pmb_sysdev_driver = {
581 .suspend = pmb_sysdev_suspend,
582 .resume = pmb_sysdev_resume,
583};
584
585static int __init pmb_sysdev_init(void)
586{
587 return sysdev_driver_register(&cpu_sysdev_class, &pmb_sysdev_driver);
588}
Francesco VIRLINZIa83c0b72009-03-11 10:39:02 +0000589subsys_initcall(pmb_sysdev_init);
590#endif