blob: 9a516b89839a6085afc6644f2d5ad571f0aa3cf9 [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>
Paul Mundt90e7d642010-02-23 16:20:53 +090026#include <linux/vmalloc.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 Mundt90e7d642010-02-23 16:20:53 +090055static struct {
56 unsigned long size;
57 int flag;
58} pmb_sizes[] = {
59 { .size = SZ_512M, .flag = PMB_SZ_512M, },
60 { .size = SZ_128M, .flag = PMB_SZ_128M, },
61 { .size = SZ_64M, .flag = PMB_SZ_64M, },
62 { .size = SZ_16M, .flag = PMB_SZ_16M, },
63};
64
Paul Mundtd01447b2010-02-18 18:13:51 +090065static void pmb_unmap_entry(struct pmb_entry *, int depth);
Matt Flemingfc2bdef2009-10-06 21:22:22 +000066
Paul Mundtd53a0d32010-02-17 21:17:02 +090067static DEFINE_RWLOCK(pmb_rwlock);
Matt Flemingedd7de82009-10-06 21:22:29 +000068static struct pmb_entry pmb_entry_list[NR_PMB_ENTRIES];
Paul Mundt51becfd2010-02-17 15:33:30 +090069static DECLARE_BITMAP(pmb_map, NR_PMB_ENTRIES);
Paul Mundt0c7b1df2006-09-27 15:08:07 +090070
Paul Mundt51becfd2010-02-17 15:33:30 +090071static __always_inline unsigned long mk_pmb_entry(unsigned int entry)
Paul Mundt0c7b1df2006-09-27 15:08:07 +090072{
73 return (entry & PMB_E_MASK) << PMB_E_SHIFT;
74}
75
Paul Mundt51becfd2010-02-17 15:33:30 +090076static __always_inline unsigned long mk_pmb_addr(unsigned int entry)
Paul Mundt0c7b1df2006-09-27 15:08:07 +090077{
78 return mk_pmb_entry(entry) | PMB_ADDR;
79}
80
Paul Mundt51becfd2010-02-17 15:33:30 +090081static __always_inline unsigned long mk_pmb_data(unsigned int entry)
Paul Mundt0c7b1df2006-09-27 15:08:07 +090082{
83 return mk_pmb_entry(entry) | PMB_DATA;
84}
85
Paul Mundt90e7d642010-02-23 16:20:53 +090086static __always_inline unsigned int pmb_ppn_in_range(unsigned long ppn)
87{
88 return ppn >= __pa(memory_start) && ppn < __pa(memory_end);
89}
90
91/*
92 * Ensure that the PMB entries match our cache configuration.
93 *
94 * When we are in 32-bit address extended mode, CCR.CB becomes
95 * invalid, so care must be taken to manually adjust cacheable
96 * translations.
97 */
98static __always_inline unsigned long pmb_cache_flags(void)
99{
100 unsigned long flags = 0;
101
102#if defined(CONFIG_CACHE_OFF)
103 flags |= PMB_WT | PMB_UB;
104#elif defined(CONFIG_CACHE_WRITETHROUGH)
105 flags |= PMB_C | PMB_WT | PMB_UB;
106#elif defined(CONFIG_CACHE_WRITEBACK)
107 flags |= PMB_C;
108#endif
109
110 return flags;
111}
112
113/*
114 * Convert typical pgprot value to the PMB equivalent
115 */
116static inline unsigned long pgprot_to_pmb_flags(pgprot_t prot)
117{
118 unsigned long pmb_flags = 0;
119 u64 flags = pgprot_val(prot);
120
121 if (flags & _PAGE_CACHABLE)
122 pmb_flags |= PMB_C;
123 if (flags & _PAGE_WT)
124 pmb_flags |= PMB_WT | PMB_UB;
125
126 return pmb_flags;
127}
128
129static bool pmb_can_merge(struct pmb_entry *a, struct pmb_entry *b)
130{
131 return (b->vpn == (a->vpn + a->size)) &&
132 (b->ppn == (a->ppn + a->size)) &&
133 (b->flags == a->flags);
134}
135
136static bool pmb_size_valid(unsigned long size)
137{
138 int i;
139
140 for (i = 0; i < ARRAY_SIZE(pmb_sizes); i++)
141 if (pmb_sizes[i].size == size)
142 return true;
143
144 return false;
145}
146
147static inline bool pmb_addr_valid(unsigned long addr, unsigned long size)
148{
149 return (addr >= P1SEG && (addr + size - 1) < P3SEG);
150}
151
152static inline bool pmb_prot_valid(pgprot_t prot)
153{
154 return (pgprot_val(prot) & _PAGE_USER) == 0;
155}
156
157static int pmb_size_to_flags(unsigned long size)
158{
159 int i;
160
161 for (i = 0; i < ARRAY_SIZE(pmb_sizes); i++)
162 if (pmb_sizes[i].size == size)
163 return pmb_sizes[i].flag;
164
165 return 0;
166}
167
Matt Fleming067784f2009-10-06 21:22:23 +0000168static int pmb_alloc_entry(void)
169{
Paul Mundtd53a0d32010-02-17 21:17:02 +0900170 int pos;
Matt Fleming067784f2009-10-06 21:22:23 +0000171
Paul Mundt51becfd2010-02-17 15:33:30 +0900172 pos = find_first_zero_bit(pmb_map, NR_PMB_ENTRIES);
Paul Mundtd53a0d32010-02-17 21:17:02 +0900173 if (pos >= 0 && pos < NR_PMB_ENTRIES)
174 __set_bit(pos, pmb_map);
175 else
176 pos = -ENOSPC;
Matt Fleming067784f2009-10-06 21:22:23 +0000177
178 return pos;
179}
180
Matt Fleming8386aeb2009-10-06 21:22:28 +0000181static struct pmb_entry *pmb_alloc(unsigned long vpn, unsigned long ppn,
Matt Fleming20b50142009-10-06 21:22:33 +0000182 unsigned long flags, int entry)
Paul Mundt0c7b1df2006-09-27 15:08:07 +0900183{
184 struct pmb_entry *pmbe;
Paul Mundtd53a0d32010-02-17 21:17:02 +0900185 unsigned long irqflags;
186 void *ret = NULL;
Matt Fleming067784f2009-10-06 21:22:23 +0000187 int pos;
188
Paul Mundtd53a0d32010-02-17 21:17:02 +0900189 write_lock_irqsave(&pmb_rwlock, irqflags);
190
Matt Fleming20b50142009-10-06 21:22:33 +0000191 if (entry == PMB_NO_ENTRY) {
192 pos = pmb_alloc_entry();
Paul Mundtd53a0d32010-02-17 21:17:02 +0900193 if (unlikely(pos < 0)) {
194 ret = ERR_PTR(pos);
195 goto out;
196 }
Matt Fleming20b50142009-10-06 21:22:33 +0000197 } else {
Paul Mundtd53a0d32010-02-17 21:17:02 +0900198 if (__test_and_set_bit(entry, pmb_map)) {
199 ret = ERR_PTR(-ENOSPC);
200 goto out;
201 }
202
Matt Fleming20b50142009-10-06 21:22:33 +0000203 pos = entry;
204 }
Paul Mundt0c7b1df2006-09-27 15:08:07 +0900205
Paul Mundtd53a0d32010-02-17 21:17:02 +0900206 write_unlock_irqrestore(&pmb_rwlock, irqflags);
207
Matt Flemingedd7de82009-10-06 21:22:29 +0000208 pmbe = &pmb_entry_list[pos];
Paul Mundtd53a0d32010-02-17 21:17:02 +0900209
Paul Mundtd01447b2010-02-18 18:13:51 +0900210 memset(pmbe, 0, sizeof(struct pmb_entry));
211
Paul Mundtd53a0d32010-02-17 21:17:02 +0900212 spin_lock_init(&pmbe->lock);
Paul Mundt0c7b1df2006-09-27 15:08:07 +0900213
214 pmbe->vpn = vpn;
215 pmbe->ppn = ppn;
216 pmbe->flags = flags;
Matt Fleming067784f2009-10-06 21:22:23 +0000217 pmbe->entry = pos;
Paul Mundt0c7b1df2006-09-27 15:08:07 +0900218
219 return pmbe;
Paul Mundtd53a0d32010-02-17 21:17:02 +0900220
221out:
222 write_unlock_irqrestore(&pmb_rwlock, irqflags);
223 return ret;
Paul Mundt0c7b1df2006-09-27 15:08:07 +0900224}
225
Matt Fleming8386aeb2009-10-06 21:22:28 +0000226static void pmb_free(struct pmb_entry *pmbe)
Paul Mundt0c7b1df2006-09-27 15:08:07 +0900227{
Paul Mundtd53a0d32010-02-17 21:17:02 +0900228 __clear_bit(pmbe->entry, pmb_map);
Paul Mundtd01447b2010-02-18 18:13:51 +0900229
230 pmbe->entry = PMB_NO_ENTRY;
231 pmbe->link = NULL;
Paul Mundt0c7b1df2006-09-27 15:08:07 +0900232}
233
234/*
Paul Mundt51becfd2010-02-17 15:33:30 +0900235 * Must be run uncached.
Paul Mundt0c7b1df2006-09-27 15:08:07 +0900236 */
Paul Mundtd53a0d32010-02-17 21:17:02 +0900237static void __set_pmb_entry(struct pmb_entry *pmbe)
Paul Mundt0c7b1df2006-09-27 15:08:07 +0900238{
Paul Mundt90e7d642010-02-23 16:20:53 +0900239 /* Set V-bit */
240 __raw_writel(pmbe->ppn | pmbe->flags | PMB_V, mk_pmb_data(pmbe->entry));
241 __raw_writel(pmbe->vpn | PMB_V, mk_pmb_addr(pmbe->entry));
Paul Mundt0c7b1df2006-09-27 15:08:07 +0900242}
243
Paul Mundtd53a0d32010-02-17 21:17:02 +0900244static void __clear_pmb_entry(struct pmb_entry *pmbe)
Paul Mundt0c7b1df2006-09-27 15:08:07 +0900245{
Paul Mundt2e450642010-02-18 13:26:05 +0900246 unsigned long addr, data;
247 unsigned long addr_val, data_val;
Paul Mundt0c7b1df2006-09-27 15:08:07 +0900248
Paul Mundt2e450642010-02-18 13:26:05 +0900249 addr = mk_pmb_addr(pmbe->entry);
250 data = mk_pmb_data(pmbe->entry);
251
252 addr_val = __raw_readl(addr);
253 data_val = __raw_readl(data);
Paul Mundt0c7b1df2006-09-27 15:08:07 +0900254
255 /* Clear V-bit */
Paul Mundt2e450642010-02-18 13:26:05 +0900256 writel_uncached(addr_val & ~PMB_V, addr);
257 writel_uncached(data_val & ~PMB_V, data);
Paul Mundt0c7b1df2006-09-27 15:08:07 +0900258}
259
Paul Mundtd53a0d32010-02-17 21:17:02 +0900260static void set_pmb_entry(struct pmb_entry *pmbe)
261{
262 unsigned long flags;
263
264 spin_lock_irqsave(&pmbe->lock, flags);
265 __set_pmb_entry(pmbe);
266 spin_unlock_irqrestore(&pmbe->lock, flags);
267}
268
Paul Mundt90e7d642010-02-23 16:20:53 +0900269int pmb_bolt_mapping(unsigned long vaddr, phys_addr_t phys,
270 unsigned long size, pgprot_t prot)
271{
272 return 0;
273}
Paul Mundtd7cdc9e2006-09-27 15:16:42 +0900274
Paul Mundt90e7d642010-02-23 16:20:53 +0900275void __iomem *pmb_remap_caller(phys_addr_t phys, unsigned long size,
276 pgprot_t prot, void *caller)
Paul Mundtd7cdc9e2006-09-27 15:16:42 +0900277{
Matt Flemingfc2bdef2009-10-06 21:22:22 +0000278 struct pmb_entry *pmbp, *pmbe;
Paul Mundt90e7d642010-02-23 16:20:53 +0900279 unsigned long pmb_flags;
280 int i, mapped;
281 unsigned long orig_addr, vaddr;
282 phys_addr_t offset, last_addr;
283 phys_addr_t align_mask;
284 unsigned long aligned;
285 struct vm_struct *area;
Paul Mundt7bdda622010-02-17 13:23:00 +0900286
Paul Mundt90e7d642010-02-23 16:20:53 +0900287 /*
288 * Small mappings need to go through the TLB.
289 */
290 if (size < SZ_16M)
291 return ERR_PTR(-EINVAL);
292 if (!pmb_prot_valid(prot))
293 return ERR_PTR(-EINVAL);
Paul Mundtd7cdc9e2006-09-27 15:16:42 +0900294
295 pmbp = NULL;
Paul Mundt90e7d642010-02-23 16:20:53 +0900296 pmb_flags = pgprot_to_pmb_flags(prot);
297 mapped = 0;
298
299 for (i = 0; i < ARRAY_SIZE(pmb_sizes); i++)
300 if (size >= pmb_sizes[i].size)
301 break;
302
303 last_addr = phys + size;
304 align_mask = ~(pmb_sizes[i].size - 1);
305 offset = phys & ~align_mask;
306 phys &= align_mask;
307 aligned = ALIGN(last_addr, pmb_sizes[i].size) - phys;
308
309 area = __get_vm_area_caller(aligned, VM_IOREMAP, uncached_end,
310 P3SEG, caller);
311 if (!area)
312 return NULL;
313
314 area->phys_addr = phys;
315 orig_addr = vaddr = (unsigned long)area->addr;
316
317 if (!pmb_addr_valid(vaddr, aligned))
318 return ERR_PTR(-EFAULT);
Paul Mundtd7cdc9e2006-09-27 15:16:42 +0900319
320again:
321 for (i = 0; i < ARRAY_SIZE(pmb_sizes); i++) {
Paul Mundtd53a0d32010-02-17 21:17:02 +0900322 unsigned long flags;
323
Paul Mundtd7cdc9e2006-09-27 15:16:42 +0900324 if (size < pmb_sizes[i].size)
325 continue;
326
Matt Fleming20b50142009-10-06 21:22:33 +0000327 pmbe = pmb_alloc(vaddr, phys, pmb_flags | pmb_sizes[i].flag,
328 PMB_NO_ENTRY);
Matt Flemingfc2bdef2009-10-06 21:22:22 +0000329 if (IS_ERR(pmbe)) {
Paul Mundt90e7d642010-02-23 16:20:53 +0900330 pmb_unmap_entry(pmbp, mapped);
331 return pmbe;
Matt Flemingfc2bdef2009-10-06 21:22:22 +0000332 }
Paul Mundtd7cdc9e2006-09-27 15:16:42 +0900333
Paul Mundtd53a0d32010-02-17 21:17:02 +0900334 spin_lock_irqsave(&pmbe->lock, flags);
335
Paul Mundt90e7d642010-02-23 16:20:53 +0900336 pmbe->size = pmb_sizes[i].size;
337
Paul Mundtd53a0d32010-02-17 21:17:02 +0900338 __set_pmb_entry(pmbe);
Paul Mundtd7cdc9e2006-09-27 15:16:42 +0900339
Paul Mundt90e7d642010-02-23 16:20:53 +0900340 phys += pmbe->size;
341 vaddr += pmbe->size;
342 size -= pmbe->size;
Paul Mundtd7813bc2010-02-17 17:56:38 +0900343
Paul Mundtd7cdc9e2006-09-27 15:16:42 +0900344 /*
345 * Link adjacent entries that span multiple PMB entries
346 * for easier tear-down.
347 */
Paul Mundtd53a0d32010-02-17 21:17:02 +0900348 if (likely(pmbp)) {
349 spin_lock(&pmbp->lock);
Paul Mundtd7cdc9e2006-09-27 15:16:42 +0900350 pmbp->link = pmbe;
Paul Mundtd53a0d32010-02-17 21:17:02 +0900351 spin_unlock(&pmbp->lock);
352 }
Paul Mundtd7cdc9e2006-09-27 15:16:42 +0900353
354 pmbp = pmbe;
Matt Fleminga2767cf2009-10-06 21:22:34 +0000355
356 /*
357 * Instead of trying smaller sizes on every iteration
358 * (even if we succeed in allocating space), try using
359 * pmb_sizes[i].size again.
360 */
361 i--;
Paul Mundt90e7d642010-02-23 16:20:53 +0900362 mapped++;
Paul Mundtd53a0d32010-02-17 21:17:02 +0900363
364 spin_unlock_irqrestore(&pmbe->lock, flags);
Paul Mundtd7cdc9e2006-09-27 15:16:42 +0900365 }
366
Paul Mundtd53a0d32010-02-17 21:17:02 +0900367 if (size >= SZ_16M)
Paul Mundtd7cdc9e2006-09-27 15:16:42 +0900368 goto again;
369
Paul Mundt90e7d642010-02-23 16:20:53 +0900370 return (void __iomem *)(offset + (char *)orig_addr);
Paul Mundtd7cdc9e2006-09-27 15:16:42 +0900371}
372
Paul Mundt90e7d642010-02-23 16:20:53 +0900373int pmb_unmap(void __iomem *addr)
Paul Mundtd7cdc9e2006-09-27 15:16:42 +0900374{
Paul Mundtd53a0d32010-02-17 21:17:02 +0900375 struct pmb_entry *pmbe = NULL;
Paul Mundt90e7d642010-02-23 16:20:53 +0900376 unsigned long vaddr = (unsigned long __force)addr;
377 int i, found = 0;
Paul Mundtd7cdc9e2006-09-27 15:16:42 +0900378
Paul Mundtd53a0d32010-02-17 21:17:02 +0900379 read_lock(&pmb_rwlock);
380
Matt Flemingedd7de82009-10-06 21:22:29 +0000381 for (i = 0; i < ARRAY_SIZE(pmb_entry_list); i++) {
Paul Mundt51becfd2010-02-17 15:33:30 +0900382 if (test_bit(i, pmb_map)) {
Matt Flemingedd7de82009-10-06 21:22:29 +0000383 pmbe = &pmb_entry_list[i];
Paul Mundt90e7d642010-02-23 16:20:53 +0900384 if (pmbe->vpn == vaddr) {
385 found = 1;
Matt Flemingedd7de82009-10-06 21:22:29 +0000386 break;
Paul Mundt90e7d642010-02-23 16:20:53 +0900387 }
Matt Flemingedd7de82009-10-06 21:22:29 +0000388 }
389 }
Paul Mundtd53a0d32010-02-17 21:17:02 +0900390
391 read_unlock(&pmb_rwlock);
392
Paul Mundt90e7d642010-02-23 16:20:53 +0900393 if (found) {
394 pmb_unmap_entry(pmbe, NR_PMB_ENTRIES);
395 return 0;
396 }
Paul Mundtd7cdc9e2006-09-27 15:16:42 +0900397
Paul Mundt90e7d642010-02-23 16:20:53 +0900398 return -EINVAL;
Paul Mundtd01447b2010-02-18 18:13:51 +0900399}
400
401static void __pmb_unmap_entry(struct pmb_entry *pmbe, int depth)
402{
Paul Mundtd7cdc9e2006-09-27 15:16:42 +0900403 do {
404 struct pmb_entry *pmblink = pmbe;
405
Matt Fleming067784f2009-10-06 21:22:23 +0000406 /*
407 * We may be called before this pmb_entry has been
408 * entered into the PMB table via set_pmb_entry(), but
409 * that's OK because we've allocated a unique slot for
410 * this entry in pmb_alloc() (even if we haven't filled
411 * it yet).
412 *
Paul Mundtd53a0d32010-02-17 21:17:02 +0900413 * Therefore, calling __clear_pmb_entry() is safe as no
Matt Fleming067784f2009-10-06 21:22:23 +0000414 * other mapping can be using that slot.
415 */
Paul Mundtd53a0d32010-02-17 21:17:02 +0900416 __clear_pmb_entry(pmbe);
Matt Flemingfc2bdef2009-10-06 21:22:22 +0000417
Paul Mundtd7cdc9e2006-09-27 15:16:42 +0900418 pmbe = pmblink->link;
419
420 pmb_free(pmblink);
Paul Mundtd01447b2010-02-18 18:13:51 +0900421 } while (pmbe && --depth);
422}
Paul Mundtd53a0d32010-02-17 21:17:02 +0900423
Paul Mundtd01447b2010-02-18 18:13:51 +0900424static void pmb_unmap_entry(struct pmb_entry *pmbe, int depth)
425{
426 unsigned long flags;
427
428 if (unlikely(!pmbe))
429 return;
430
431 write_lock_irqsave(&pmb_rwlock, flags);
432 __pmb_unmap_entry(pmbe, depth);
Paul Mundtd53a0d32010-02-17 21:17:02 +0900433 write_unlock_irqrestore(&pmb_rwlock, flags);
Paul Mundtd7cdc9e2006-09-27 15:16:42 +0900434}
435
Paul Mundtd01447b2010-02-18 18:13:51 +0900436static void __init pmb_notify(void)
Matt Fleming20b50142009-10-06 21:22:33 +0000437{
Paul Mundtd01447b2010-02-18 18:13:51 +0900438 int i;
Matt Fleming3d467672010-01-18 19:33:10 +0900439
Paul Mundtefd54ea2010-02-16 18:39:30 +0900440 pr_info("PMB: boot mappings:\n");
Matt Fleming3d467672010-01-18 19:33:10 +0900441
Paul Mundtd01447b2010-02-18 18:13:51 +0900442 read_lock(&pmb_rwlock);
443
444 for (i = 0; i < ARRAY_SIZE(pmb_entry_list); i++) {
445 struct pmb_entry *pmbe;
446
447 if (!test_bit(i, pmb_map))
448 continue;
449
450 pmbe = &pmb_entry_list[i];
451
452 pr_info(" 0x%08lx -> 0x%08lx [ %4ldMB %2scached ]\n",
453 pmbe->vpn >> PAGE_SHIFT, pmbe->ppn >> PAGE_SHIFT,
454 pmbe->size >> 20, (pmbe->flags & PMB_C) ? "" : "un");
455 }
456
457 read_unlock(&pmb_rwlock);
458}
459
460/*
461 * Sync our software copy of the PMB mappings with those in hardware. The
462 * mappings in the hardware PMB were either set up by the bootloader or
463 * very early on by the kernel.
464 */
465static void __init pmb_synchronize(void)
466{
467 struct pmb_entry *pmbp = NULL;
468 int i, j;
469
Matt Fleming3d467672010-01-18 19:33:10 +0900470 /*
Paul Mundtefd54ea2010-02-16 18:39:30 +0900471 * Run through the initial boot mappings, log the established
472 * ones, and blow away anything that falls outside of the valid
473 * PPN range. Specifically, we only care about existing mappings
474 * that impact the cached/uncached sections.
Matt Fleming3d467672010-01-18 19:33:10 +0900475 *
Paul Mundtefd54ea2010-02-16 18:39:30 +0900476 * Note that touching these can be a bit of a minefield; the boot
477 * loader can establish multi-page mappings with the same caching
478 * attributes, so we need to ensure that we aren't modifying a
479 * mapping that we're presently executing from, or may execute
480 * from in the case of straddling page boundaries.
Matt Fleming3d467672010-01-18 19:33:10 +0900481 *
Paul Mundtefd54ea2010-02-16 18:39:30 +0900482 * In the future we will have to tidy up after the boot loader by
483 * jumping between the cached and uncached mappings and tearing
484 * down alternating mappings while executing from the other.
Matt Fleming3d467672010-01-18 19:33:10 +0900485 */
Paul Mundt51becfd2010-02-17 15:33:30 +0900486 for (i = 0; i < NR_PMB_ENTRIES; i++) {
Matt Fleming3d467672010-01-18 19:33:10 +0900487 unsigned long addr, data;
488 unsigned long addr_val, data_val;
Paul Mundtefd54ea2010-02-16 18:39:30 +0900489 unsigned long ppn, vpn, flags;
Paul Mundtd53a0d32010-02-17 21:17:02 +0900490 unsigned long irqflags;
Paul Mundtd7813bc2010-02-17 17:56:38 +0900491 unsigned int size;
Paul Mundtefd54ea2010-02-16 18:39:30 +0900492 struct pmb_entry *pmbe;
Matt Fleming3d467672010-01-18 19:33:10 +0900493
494 addr = mk_pmb_addr(i);
495 data = mk_pmb_data(i);
496
497 addr_val = __raw_readl(addr);
498 data_val = __raw_readl(data);
499
500 /*
501 * Skip over any bogus entries
502 */
503 if (!(data_val & PMB_V) || !(addr_val & PMB_V))
504 continue;
505
506 ppn = data_val & PMB_PFN_MASK;
507 vpn = addr_val & PMB_PFN_MASK;
508
509 /*
510 * Only preserve in-range mappings.
511 */
Paul Mundtefd54ea2010-02-16 18:39:30 +0900512 if (!pmb_ppn_in_range(ppn)) {
Matt Fleming3d467672010-01-18 19:33:10 +0900513 /*
514 * Invalidate anything out of bounds.
515 */
Paul Mundt2e450642010-02-18 13:26:05 +0900516 writel_uncached(addr_val & ~PMB_V, addr);
517 writel_uncached(data_val & ~PMB_V, data);
Paul Mundtefd54ea2010-02-16 18:39:30 +0900518 continue;
Matt Fleming3d467672010-01-18 19:33:10 +0900519 }
Paul Mundtefd54ea2010-02-16 18:39:30 +0900520
521 /*
522 * Update the caching attributes if necessary
523 */
524 if (data_val & PMB_C) {
Paul Mundt0065b962010-02-17 18:05:23 +0900525 data_val &= ~PMB_CACHE_MASK;
526 data_val |= pmb_cache_flags();
Paul Mundt2e450642010-02-18 13:26:05 +0900527
528 writel_uncached(data_val, data);
Paul Mundtefd54ea2010-02-16 18:39:30 +0900529 }
530
Paul Mundtd7813bc2010-02-17 17:56:38 +0900531 size = data_val & PMB_SZ_MASK;
532 flags = size | (data_val & PMB_CACHE_MASK);
Paul Mundtefd54ea2010-02-16 18:39:30 +0900533
534 pmbe = pmb_alloc(vpn, ppn, flags, i);
535 if (IS_ERR(pmbe)) {
536 WARN_ON_ONCE(1);
537 continue;
538 }
539
Paul Mundtd53a0d32010-02-17 21:17:02 +0900540 spin_lock_irqsave(&pmbe->lock, irqflags);
541
Paul Mundtd7813bc2010-02-17 17:56:38 +0900542 for (j = 0; j < ARRAY_SIZE(pmb_sizes); j++)
543 if (pmb_sizes[j].flag == size)
544 pmbe->size = pmb_sizes[j].size;
545
Paul Mundtd53a0d32010-02-17 21:17:02 +0900546 if (pmbp) {
547 spin_lock(&pmbp->lock);
548
549 /*
550 * Compare the previous entry against the current one to
551 * see if the entries span a contiguous mapping. If so,
Paul Mundtd01447b2010-02-18 18:13:51 +0900552 * setup the entry links accordingly. Compound mappings
553 * are later coalesced.
Paul Mundtd53a0d32010-02-17 21:17:02 +0900554 */
Paul Mundtd01447b2010-02-18 18:13:51 +0900555 if (pmb_can_merge(pmbp, pmbe))
Paul Mundtd53a0d32010-02-17 21:17:02 +0900556 pmbp->link = pmbe;
557
558 spin_unlock(&pmbp->lock);
559 }
Paul Mundtd7813bc2010-02-17 17:56:38 +0900560
561 pmbp = pmbe;
562
Paul Mundtd53a0d32010-02-17 21:17:02 +0900563 spin_unlock_irqrestore(&pmbe->lock, irqflags);
Matt Fleming3d467672010-01-18 19:33:10 +0900564 }
Matt Fleming3d467672010-01-18 19:33:10 +0900565}
Matt Fleming3d467672010-01-18 19:33:10 +0900566
Paul Mundtd01447b2010-02-18 18:13:51 +0900567static void __init pmb_merge(struct pmb_entry *head)
Matt Fleming3d467672010-01-18 19:33:10 +0900568{
Paul Mundtd01447b2010-02-18 18:13:51 +0900569 unsigned long span, newsize;
570 struct pmb_entry *tail;
571 int i = 1, depth = 0;
572
573 span = newsize = head->size;
574
575 tail = head->link;
576 while (tail) {
577 span += tail->size;
578
579 if (pmb_size_valid(span)) {
580 newsize = span;
581 depth = i;
582 }
583
584 /* This is the end of the line.. */
585 if (!tail->link)
586 break;
587
588 tail = tail->link;
589 i++;
590 }
Matt Fleming20b50142009-10-06 21:22:33 +0000591
Matt Fleming3d467672010-01-18 19:33:10 +0900592 /*
Paul Mundtd01447b2010-02-18 18:13:51 +0900593 * The merged page size must be valid.
Matt Fleming3d467672010-01-18 19:33:10 +0900594 */
Paul Mundtd01447b2010-02-18 18:13:51 +0900595 if (!pmb_size_valid(newsize))
596 return;
597
598 head->flags &= ~PMB_SZ_MASK;
599 head->flags |= pmb_size_to_flags(newsize);
600
601 head->size = newsize;
602
603 __pmb_unmap_entry(head->link, depth);
604 __set_pmb_entry(head);
605}
606
607static void __init pmb_coalesce(void)
608{
609 unsigned long flags;
610 int i;
611
612 write_lock_irqsave(&pmb_rwlock, flags);
613
614 for (i = 0; i < ARRAY_SIZE(pmb_entry_list); i++) {
615 struct pmb_entry *pmbe;
616
617 if (!test_bit(i, pmb_map))
618 continue;
619
620 pmbe = &pmb_entry_list[i];
621
622 /*
623 * We're only interested in compound mappings
624 */
625 if (!pmbe->link)
626 continue;
627
628 /*
629 * Nothing to do if it already uses the largest possible
630 * page size.
631 */
632 if (pmbe->size == SZ_512M)
633 continue;
634
635 pmb_merge(pmbe);
636 }
637
638 write_unlock_irqrestore(&pmb_rwlock, flags);
639}
640
641#ifdef CONFIG_UNCACHED_MAPPING
642static void __init pmb_resize(void)
643{
644 int i;
645
646 /*
647 * If the uncached mapping was constructed by the kernel, it will
648 * already be a reasonable size.
649 */
650 if (uncached_size == SZ_16M)
651 return;
652
653 read_lock(&pmb_rwlock);
654
655 for (i = 0; i < ARRAY_SIZE(pmb_entry_list); i++) {
656 struct pmb_entry *pmbe;
657 unsigned long flags;
658
659 if (!test_bit(i, pmb_map))
660 continue;
661
662 pmbe = &pmb_entry_list[i];
663
664 if (pmbe->vpn != uncached_start)
665 continue;
666
667 /*
668 * Found it, now resize it.
669 */
670 spin_lock_irqsave(&pmbe->lock, flags);
671
672 pmbe->size = SZ_16M;
673 pmbe->flags &= ~PMB_SZ_MASK;
674 pmbe->flags |= pmb_size_to_flags(pmbe->size);
675
676 uncached_resize(pmbe->size);
677
678 __set_pmb_entry(pmbe);
679
680 spin_unlock_irqrestore(&pmbe->lock, flags);
681 }
682
683 read_lock(&pmb_rwlock);
684}
685#endif
686
687void __init pmb_init(void)
688{
689 /* Synchronize software state */
690 pmb_synchronize();
691
692 /* Attempt to combine compound mappings */
693 pmb_coalesce();
694
695#ifdef CONFIG_UNCACHED_MAPPING
696 /* Resize initial mappings, if necessary */
697 pmb_resize();
698#endif
699
700 /* Log them */
701 pmb_notify();
Matt Fleming20b50142009-10-06 21:22:33 +0000702
Paul Mundt2e450642010-02-18 13:26:05 +0900703 writel_uncached(0, PMB_IRMCR);
Paul Mundta0ab3662010-01-13 18:31:48 +0900704
Paul Mundta0ab3662010-01-13 18:31:48 +0900705 /* Flush out the TLB */
Paul Mundtefd54ea2010-02-16 18:39:30 +0900706 __raw_writel(__raw_readl(MMUCR) | MMUCR_TI, MMUCR);
Paul Mundt2e450642010-02-18 13:26:05 +0900707 ctrl_barrier();
Matt Fleming20b50142009-10-06 21:22:33 +0000708}
Paul Mundt0c7b1df2006-09-27 15:08:07 +0900709
Paul Mundt2efa53b2010-01-20 16:40:48 +0900710bool __in_29bit_mode(void)
711{
712 return (__raw_readl(PMB_PASCR) & PASCR_SE) == 0;
713}
714
Paul Mundt0c7b1df2006-09-27 15:08:07 +0900715static int pmb_seq_show(struct seq_file *file, void *iter)
716{
717 int i;
718
719 seq_printf(file, "V: Valid, C: Cacheable, WT: Write-Through\n"
720 "CB: Copy-Back, B: Buffered, UB: Unbuffered\n");
721 seq_printf(file, "ety vpn ppn size flags\n");
722
723 for (i = 0; i < NR_PMB_ENTRIES; i++) {
724 unsigned long addr, data;
725 unsigned int size;
726 char *sz_str = NULL;
727
Paul Mundt9d56dd32010-01-26 12:58:40 +0900728 addr = __raw_readl(mk_pmb_addr(i));
729 data = __raw_readl(mk_pmb_data(i));
Paul Mundt0c7b1df2006-09-27 15:08:07 +0900730
731 size = data & PMB_SZ_MASK;
732 sz_str = (size == PMB_SZ_16M) ? " 16MB":
733 (size == PMB_SZ_64M) ? " 64MB":
734 (size == PMB_SZ_128M) ? "128MB":
735 "512MB";
736
737 /* 02: V 0x88 0x08 128MB C CB B */
738 seq_printf(file, "%02d: %c 0x%02lx 0x%02lx %s %c %s %s\n",
739 i, ((addr & PMB_V) && (data & PMB_V)) ? 'V' : ' ',
740 (addr >> 24) & 0xff, (data >> 24) & 0xff,
741 sz_str, (data & PMB_C) ? 'C' : ' ',
742 (data & PMB_WT) ? "WT" : "CB",
743 (data & PMB_UB) ? "UB" : " B");
744 }
745
746 return 0;
747}
748
749static int pmb_debugfs_open(struct inode *inode, struct file *file)
750{
751 return single_open(file, pmb_seq_show, NULL);
752}
753
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800754static const struct file_operations pmb_debugfs_fops = {
Paul Mundt0c7b1df2006-09-27 15:08:07 +0900755 .owner = THIS_MODULE,
756 .open = pmb_debugfs_open,
757 .read = seq_read,
758 .llseek = seq_lseek,
Li Zefan45dabf12008-06-24 13:30:23 +0800759 .release = single_release,
Paul Mundt0c7b1df2006-09-27 15:08:07 +0900760};
761
762static int __init pmb_debugfs_init(void)
763{
764 struct dentry *dentry;
765
766 dentry = debugfs_create_file("pmb", S_IFREG | S_IRUGO,
Paul Mundtb9e393c2008-03-07 17:19:58 +0900767 sh_debugfs_root, NULL, &pmb_debugfs_fops);
Zhaolei25627c72008-10-17 19:25:09 +0800768 if (!dentry)
769 return -ENOMEM;
Paul Mundt0c7b1df2006-09-27 15:08:07 +0900770 if (IS_ERR(dentry))
771 return PTR_ERR(dentry);
772
773 return 0;
774}
Paul Mundt0c7b1df2006-09-27 15:08:07 +0900775postcore_initcall(pmb_debugfs_init);
Francesco VIRLINZIa83c0b72009-03-11 10:39:02 +0000776
777#ifdef CONFIG_PM
778static int pmb_sysdev_suspend(struct sys_device *dev, pm_message_t state)
779{
780 static pm_message_t prev_state;
Matt Flemingedd7de82009-10-06 21:22:29 +0000781 int i;
Francesco VIRLINZIa83c0b72009-03-11 10:39:02 +0000782
783 /* Restore the PMB after a resume from hibernation */
784 if (state.event == PM_EVENT_ON &&
785 prev_state.event == PM_EVENT_FREEZE) {
786 struct pmb_entry *pmbe;
Paul Mundtd53a0d32010-02-17 21:17:02 +0900787
788 read_lock(&pmb_rwlock);
789
Matt Flemingedd7de82009-10-06 21:22:29 +0000790 for (i = 0; i < ARRAY_SIZE(pmb_entry_list); i++) {
Paul Mundt51becfd2010-02-17 15:33:30 +0900791 if (test_bit(i, pmb_map)) {
Matt Flemingedd7de82009-10-06 21:22:29 +0000792 pmbe = &pmb_entry_list[i];
793 set_pmb_entry(pmbe);
794 }
795 }
Paul Mundtd53a0d32010-02-17 21:17:02 +0900796
797 read_unlock(&pmb_rwlock);
Francesco VIRLINZIa83c0b72009-03-11 10:39:02 +0000798 }
Paul Mundtd53a0d32010-02-17 21:17:02 +0900799
Francesco VIRLINZIa83c0b72009-03-11 10:39:02 +0000800 prev_state = state;
Paul Mundtd53a0d32010-02-17 21:17:02 +0900801
Francesco VIRLINZIa83c0b72009-03-11 10:39:02 +0000802 return 0;
803}
804
805static int pmb_sysdev_resume(struct sys_device *dev)
806{
807 return pmb_sysdev_suspend(dev, PMSG_ON);
808}
809
810static struct sysdev_driver pmb_sysdev_driver = {
811 .suspend = pmb_sysdev_suspend,
812 .resume = pmb_sysdev_resume,
813};
814
815static int __init pmb_sysdev_init(void)
816{
817 return sysdev_driver_register(&cpu_sysdev_class, &pmb_sysdev_driver);
818}
Francesco VIRLINZIa83c0b72009-03-11 10:39:02 +0000819subsys_initcall(pmb_sysdev_init);
820#endif