blob: 3cc21933063bf7485a8b0cf2836ef560b54da74d [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 Mundt281983d2010-03-04 16:44:20 +090027#include <asm/cacheflush.h>
Paul Mundt51becfd2010-02-17 15:33:30 +090028#include <asm/sizes.h>
Paul Mundt0c7b1df2006-09-27 15:08:07 +090029#include <asm/system.h>
30#include <asm/uaccess.h>
Paul Mundtd7cdc9e2006-09-27 15:16:42 +090031#include <asm/pgtable.h>
Paul Mundt7bdda622010-02-17 13:23:00 +090032#include <asm/page.h>
Paul Mundt0c7b1df2006-09-27 15:08:07 +090033#include <asm/mmu.h>
Stuart Menefyeddeeb32007-11-26 21:32:40 +090034#include <asm/mmu_context.h>
Paul Mundt0c7b1df2006-09-27 15:08:07 +090035
Paul Mundtd53a0d32010-02-17 21:17:02 +090036struct pmb_entry;
37
38struct pmb_entry {
39 unsigned long vpn;
40 unsigned long ppn;
41 unsigned long flags;
42 unsigned long size;
43
44 spinlock_t lock;
45
46 /*
47 * 0 .. NR_PMB_ENTRIES for specific entry selection, or
48 * PMB_NO_ENTRY to search for a free one
49 */
50 int entry;
51
52 /* Adjacent entry link for contiguous multi-entry mappings */
53 struct pmb_entry *link;
54};
55
Paul Mundt90e7d642010-02-23 16:20:53 +090056static struct {
57 unsigned long size;
58 int flag;
59} pmb_sizes[] = {
60 { .size = SZ_512M, .flag = PMB_SZ_512M, },
61 { .size = SZ_128M, .flag = PMB_SZ_128M, },
62 { .size = SZ_64M, .flag = PMB_SZ_64M, },
63 { .size = SZ_16M, .flag = PMB_SZ_16M, },
64};
65
Paul Mundtd01447b2010-02-18 18:13:51 +090066static void pmb_unmap_entry(struct pmb_entry *, int depth);
Matt Flemingfc2bdef2009-10-06 21:22:22 +000067
Paul Mundtd53a0d32010-02-17 21:17:02 +090068static DEFINE_RWLOCK(pmb_rwlock);
Matt Flemingedd7de82009-10-06 21:22:29 +000069static struct pmb_entry pmb_entry_list[NR_PMB_ENTRIES];
Paul Mundt51becfd2010-02-17 15:33:30 +090070static DECLARE_BITMAP(pmb_map, NR_PMB_ENTRIES);
Paul Mundt0c7b1df2006-09-27 15:08:07 +090071
Paul Mundt4cfa8e72010-03-02 16:49:50 +090072static unsigned int pmb_iomapping_enabled;
73
Paul Mundt51becfd2010-02-17 15:33:30 +090074static __always_inline unsigned long mk_pmb_entry(unsigned int entry)
Paul Mundt0c7b1df2006-09-27 15:08:07 +090075{
76 return (entry & PMB_E_MASK) << PMB_E_SHIFT;
77}
78
Paul Mundt51becfd2010-02-17 15:33:30 +090079static __always_inline unsigned long mk_pmb_addr(unsigned int entry)
Paul Mundt0c7b1df2006-09-27 15:08:07 +090080{
81 return mk_pmb_entry(entry) | PMB_ADDR;
82}
83
Paul Mundt51becfd2010-02-17 15:33:30 +090084static __always_inline unsigned long mk_pmb_data(unsigned int entry)
Paul Mundt0c7b1df2006-09-27 15:08:07 +090085{
86 return mk_pmb_entry(entry) | PMB_DATA;
87}
88
Paul Mundt90e7d642010-02-23 16:20:53 +090089static __always_inline unsigned int pmb_ppn_in_range(unsigned long ppn)
90{
91 return ppn >= __pa(memory_start) && ppn < __pa(memory_end);
92}
93
94/*
95 * Ensure that the PMB entries match our cache configuration.
96 *
97 * When we are in 32-bit address extended mode, CCR.CB becomes
98 * invalid, so care must be taken to manually adjust cacheable
99 * translations.
100 */
101static __always_inline unsigned long pmb_cache_flags(void)
102{
103 unsigned long flags = 0;
104
105#if defined(CONFIG_CACHE_OFF)
106 flags |= PMB_WT | PMB_UB;
107#elif defined(CONFIG_CACHE_WRITETHROUGH)
108 flags |= PMB_C | PMB_WT | PMB_UB;
109#elif defined(CONFIG_CACHE_WRITEBACK)
110 flags |= PMB_C;
111#endif
112
113 return flags;
114}
115
116/*
117 * Convert typical pgprot value to the PMB equivalent
118 */
119static inline unsigned long pgprot_to_pmb_flags(pgprot_t prot)
120{
121 unsigned long pmb_flags = 0;
122 u64 flags = pgprot_val(prot);
123
124 if (flags & _PAGE_CACHABLE)
125 pmb_flags |= PMB_C;
126 if (flags & _PAGE_WT)
127 pmb_flags |= PMB_WT | PMB_UB;
128
129 return pmb_flags;
130}
131
Paul Mundta1042aa2010-03-03 13:13:25 +0900132static inline bool pmb_can_merge(struct pmb_entry *a, struct pmb_entry *b)
Paul Mundt90e7d642010-02-23 16:20:53 +0900133{
134 return (b->vpn == (a->vpn + a->size)) &&
135 (b->ppn == (a->ppn + a->size)) &&
136 (b->flags == a->flags);
137}
138
Paul Mundta1042aa2010-03-03 13:13:25 +0900139static bool pmb_mapping_exists(unsigned long vaddr, phys_addr_t phys,
140 unsigned long size)
141{
142 int i;
143
144 read_lock(&pmb_rwlock);
145
146 for (i = 0; i < ARRAY_SIZE(pmb_entry_list); i++) {
147 struct pmb_entry *pmbe, *iter;
148 unsigned long span;
149
150 if (!test_bit(i, pmb_map))
151 continue;
152
153 pmbe = &pmb_entry_list[i];
154
155 /*
156 * See if VPN and PPN are bounded by an existing mapping.
157 */
158 if ((vaddr < pmbe->vpn) || (vaddr >= (pmbe->vpn + pmbe->size)))
159 continue;
160 if ((phys < pmbe->ppn) || (phys >= (pmbe->ppn + pmbe->size)))
161 continue;
162
163 /*
164 * Now see if we're in range of a simple mapping.
165 */
166 if (size <= pmbe->size) {
167 read_unlock(&pmb_rwlock);
168 return true;
169 }
170
171 span = pmbe->size;
172
173 /*
174 * Finally for sizes that involve compound mappings, walk
175 * the chain.
176 */
177 for (iter = pmbe->link; iter; iter = iter->link)
178 span += iter->size;
179
180 /*
181 * Nothing else to do if the range requirements are met.
182 */
183 if (size <= span) {
184 read_unlock(&pmb_rwlock);
185 return true;
186 }
187 }
188
189 read_unlock(&pmb_rwlock);
190 return false;
191}
192
Paul Mundt90e7d642010-02-23 16:20:53 +0900193static bool pmb_size_valid(unsigned long size)
194{
195 int i;
196
197 for (i = 0; i < ARRAY_SIZE(pmb_sizes); i++)
198 if (pmb_sizes[i].size == size)
199 return true;
200
201 return false;
202}
203
204static inline bool pmb_addr_valid(unsigned long addr, unsigned long size)
205{
206 return (addr >= P1SEG && (addr + size - 1) < P3SEG);
207}
208
209static inline bool pmb_prot_valid(pgprot_t prot)
210{
211 return (pgprot_val(prot) & _PAGE_USER) == 0;
212}
213
214static int pmb_size_to_flags(unsigned long size)
215{
216 int i;
217
218 for (i = 0; i < ARRAY_SIZE(pmb_sizes); i++)
219 if (pmb_sizes[i].size == size)
220 return pmb_sizes[i].flag;
221
222 return 0;
223}
224
Matt Fleming067784f2009-10-06 21:22:23 +0000225static int pmb_alloc_entry(void)
226{
Paul Mundtd53a0d32010-02-17 21:17:02 +0900227 int pos;
Matt Fleming067784f2009-10-06 21:22:23 +0000228
Paul Mundt51becfd2010-02-17 15:33:30 +0900229 pos = find_first_zero_bit(pmb_map, NR_PMB_ENTRIES);
Paul Mundtd53a0d32010-02-17 21:17:02 +0900230 if (pos >= 0 && pos < NR_PMB_ENTRIES)
231 __set_bit(pos, pmb_map);
232 else
233 pos = -ENOSPC;
Matt Fleming067784f2009-10-06 21:22:23 +0000234
235 return pos;
236}
237
Matt Fleming8386aeb2009-10-06 21:22:28 +0000238static struct pmb_entry *pmb_alloc(unsigned long vpn, unsigned long ppn,
Matt Fleming20b50142009-10-06 21:22:33 +0000239 unsigned long flags, int entry)
Paul Mundt0c7b1df2006-09-27 15:08:07 +0900240{
241 struct pmb_entry *pmbe;
Paul Mundtd53a0d32010-02-17 21:17:02 +0900242 unsigned long irqflags;
243 void *ret = NULL;
Matt Fleming067784f2009-10-06 21:22:23 +0000244 int pos;
245
Paul Mundtd53a0d32010-02-17 21:17:02 +0900246 write_lock_irqsave(&pmb_rwlock, irqflags);
247
Matt Fleming20b50142009-10-06 21:22:33 +0000248 if (entry == PMB_NO_ENTRY) {
249 pos = pmb_alloc_entry();
Paul Mundtd53a0d32010-02-17 21:17:02 +0900250 if (unlikely(pos < 0)) {
251 ret = ERR_PTR(pos);
252 goto out;
253 }
Matt Fleming20b50142009-10-06 21:22:33 +0000254 } else {
Paul Mundtd53a0d32010-02-17 21:17:02 +0900255 if (__test_and_set_bit(entry, pmb_map)) {
256 ret = ERR_PTR(-ENOSPC);
257 goto out;
258 }
259
Matt Fleming20b50142009-10-06 21:22:33 +0000260 pos = entry;
261 }
Paul Mundt0c7b1df2006-09-27 15:08:07 +0900262
Paul Mundtd53a0d32010-02-17 21:17:02 +0900263 write_unlock_irqrestore(&pmb_rwlock, irqflags);
264
Matt Flemingedd7de82009-10-06 21:22:29 +0000265 pmbe = &pmb_entry_list[pos];
Paul Mundtd53a0d32010-02-17 21:17:02 +0900266
Paul Mundtd01447b2010-02-18 18:13:51 +0900267 memset(pmbe, 0, sizeof(struct pmb_entry));
268
Paul Mundtd53a0d32010-02-17 21:17:02 +0900269 spin_lock_init(&pmbe->lock);
Paul Mundt0c7b1df2006-09-27 15:08:07 +0900270
271 pmbe->vpn = vpn;
272 pmbe->ppn = ppn;
273 pmbe->flags = flags;
Matt Fleming067784f2009-10-06 21:22:23 +0000274 pmbe->entry = pos;
Paul Mundt0c7b1df2006-09-27 15:08:07 +0900275
276 return pmbe;
Paul Mundtd53a0d32010-02-17 21:17:02 +0900277
278out:
279 write_unlock_irqrestore(&pmb_rwlock, irqflags);
280 return ret;
Paul Mundt0c7b1df2006-09-27 15:08:07 +0900281}
282
Matt Fleming8386aeb2009-10-06 21:22:28 +0000283static void pmb_free(struct pmb_entry *pmbe)
Paul Mundt0c7b1df2006-09-27 15:08:07 +0900284{
Paul Mundtd53a0d32010-02-17 21:17:02 +0900285 __clear_bit(pmbe->entry, pmb_map);
Paul Mundtd01447b2010-02-18 18:13:51 +0900286
287 pmbe->entry = PMB_NO_ENTRY;
288 pmbe->link = NULL;
Paul Mundt0c7b1df2006-09-27 15:08:07 +0900289}
290
291/*
Paul Mundt51becfd2010-02-17 15:33:30 +0900292 * Must be run uncached.
Paul Mundt0c7b1df2006-09-27 15:08:07 +0900293 */
Paul Mundtd53a0d32010-02-17 21:17:02 +0900294static void __set_pmb_entry(struct pmb_entry *pmbe)
Paul Mundt0c7b1df2006-09-27 15:08:07 +0900295{
Paul Mundt281983d2010-03-04 16:44:20 +0900296 unsigned long addr, data;
297
298 addr = mk_pmb_addr(pmbe->entry);
299 data = mk_pmb_data(pmbe->entry);
300
301 jump_to_uncached();
302
Paul Mundt90e7d642010-02-23 16:20:53 +0900303 /* Set V-bit */
Paul Mundt281983d2010-03-04 16:44:20 +0900304 __raw_writel(pmbe->vpn | PMB_V, addr);
305 __raw_writel(pmbe->ppn | pmbe->flags | PMB_V, data);
306
307 back_to_cached();
Paul Mundt0c7b1df2006-09-27 15:08:07 +0900308}
309
Paul Mundtd53a0d32010-02-17 21:17:02 +0900310static void __clear_pmb_entry(struct pmb_entry *pmbe)
Paul Mundt0c7b1df2006-09-27 15:08:07 +0900311{
Paul Mundt2e450642010-02-18 13:26:05 +0900312 unsigned long addr, data;
313 unsigned long addr_val, data_val;
Paul Mundt0c7b1df2006-09-27 15:08:07 +0900314
Paul Mundt2e450642010-02-18 13:26:05 +0900315 addr = mk_pmb_addr(pmbe->entry);
316 data = mk_pmb_data(pmbe->entry);
317
318 addr_val = __raw_readl(addr);
319 data_val = __raw_readl(data);
Paul Mundt0c7b1df2006-09-27 15:08:07 +0900320
321 /* Clear V-bit */
Paul Mundt2e450642010-02-18 13:26:05 +0900322 writel_uncached(addr_val & ~PMB_V, addr);
323 writel_uncached(data_val & ~PMB_V, data);
Paul Mundt0c7b1df2006-09-27 15:08:07 +0900324}
325
Matt Fleming3fe0f362010-03-22 22:09:58 +0000326#ifdef CONFIG_PM
Paul Mundtd53a0d32010-02-17 21:17:02 +0900327static void set_pmb_entry(struct pmb_entry *pmbe)
328{
329 unsigned long flags;
330
331 spin_lock_irqsave(&pmbe->lock, flags);
332 __set_pmb_entry(pmbe);
333 spin_unlock_irqrestore(&pmbe->lock, flags);
334}
Matt Fleming3fe0f362010-03-22 22:09:58 +0000335#endif /* CONFIG_PM */
Paul Mundtd53a0d32010-02-17 21:17:02 +0900336
Paul Mundt90e7d642010-02-23 16:20:53 +0900337int pmb_bolt_mapping(unsigned long vaddr, phys_addr_t phys,
338 unsigned long size, pgprot_t prot)
339{
Matt Flemingfc2bdef2009-10-06 21:22:22 +0000340 struct pmb_entry *pmbp, *pmbe;
Paul Mundt281983d2010-03-04 16:44:20 +0900341 unsigned long orig_addr, orig_size;
Paul Mundta1042aa2010-03-03 13:13:25 +0900342 unsigned long flags, pmb_flags;
Paul Mundt90e7d642010-02-23 16:20:53 +0900343 int i, mapped;
Paul Mundt7bdda622010-02-17 13:23:00 +0900344
Paul Mundt6eb3c732010-03-02 17:22:29 +0900345 if (!pmb_addr_valid(vaddr, size))
346 return -EFAULT;
Paul Mundta1042aa2010-03-03 13:13:25 +0900347 if (pmb_mapping_exists(vaddr, phys, size))
348 return 0;
Paul Mundt4cfa8e72010-03-02 16:49:50 +0900349
Paul Mundt281983d2010-03-04 16:44:20 +0900350 orig_addr = vaddr;
351 orig_size = size;
352
353 flush_tlb_kernel_range(vaddr, vaddr + size);
354
Paul Mundt90e7d642010-02-23 16:20:53 +0900355 pmb_flags = pgprot_to_pmb_flags(prot);
Paul Mundt6eb3c732010-03-02 17:22:29 +0900356 pmbp = NULL;
Paul Mundtd7cdc9e2006-09-27 15:16:42 +0900357
Paul Mundta1042aa2010-03-03 13:13:25 +0900358 do {
359 for (i = mapped = 0; i < ARRAY_SIZE(pmb_sizes); i++) {
360 if (size < pmb_sizes[i].size)
361 continue;
Paul Mundtd53a0d32010-02-17 21:17:02 +0900362
Paul Mundta1042aa2010-03-03 13:13:25 +0900363 pmbe = pmb_alloc(vaddr, phys, pmb_flags |
364 pmb_sizes[i].flag, PMB_NO_ENTRY);
365 if (IS_ERR(pmbe)) {
366 pmb_unmap_entry(pmbp, mapped);
367 return PTR_ERR(pmbe);
368 }
Paul Mundtd7cdc9e2006-09-27 15:16:42 +0900369
Paul Mundta1042aa2010-03-03 13:13:25 +0900370 spin_lock_irqsave(&pmbe->lock, flags);
371
372 pmbe->size = pmb_sizes[i].size;
373
374 __set_pmb_entry(pmbe);
375
376 phys += pmbe->size;
377 vaddr += pmbe->size;
378 size -= pmbe->size;
379
380 /*
381 * Link adjacent entries that span multiple PMB
382 * entries for easier tear-down.
383 */
384 if (likely(pmbp)) {
385 spin_lock(&pmbp->lock);
386 pmbp->link = pmbe;
387 spin_unlock(&pmbp->lock);
388 }
389
390 pmbp = pmbe;
391
392 /*
393 * Instead of trying smaller sizes on every
394 * iteration (even if we succeed in allocating
395 * space), try using pmb_sizes[i].size again.
396 */
397 i--;
398 mapped++;
399
400 spin_unlock_irqrestore(&pmbe->lock, flags);
Matt Flemingfc2bdef2009-10-06 21:22:22 +0000401 }
Paul Mundta1042aa2010-03-03 13:13:25 +0900402 } while (size >= SZ_16M);
Paul Mundtd7cdc9e2006-09-27 15:16:42 +0900403
Paul Mundt281983d2010-03-04 16:44:20 +0900404 flush_cache_vmap(orig_addr, orig_addr + orig_size);
405
Paul Mundt6eb3c732010-03-02 17:22:29 +0900406 return 0;
407}
408
409void __iomem *pmb_remap_caller(phys_addr_t phys, unsigned long size,
410 pgprot_t prot, void *caller)
411{
Paul Mundt281983d2010-03-04 16:44:20 +0900412 unsigned long vaddr;
Paul Mundt6eb3c732010-03-02 17:22:29 +0900413 phys_addr_t offset, last_addr;
414 phys_addr_t align_mask;
415 unsigned long aligned;
416 struct vm_struct *area;
417 int i, ret;
418
419 if (!pmb_iomapping_enabled)
420 return NULL;
421
422 /*
423 * Small mappings need to go through the TLB.
424 */
425 if (size < SZ_16M)
426 return ERR_PTR(-EINVAL);
427 if (!pmb_prot_valid(prot))
428 return ERR_PTR(-EINVAL);
429
430 for (i = 0; i < ARRAY_SIZE(pmb_sizes); i++)
431 if (size >= pmb_sizes[i].size)
432 break;
433
434 last_addr = phys + size;
435 align_mask = ~(pmb_sizes[i].size - 1);
436 offset = phys & ~align_mask;
437 phys &= align_mask;
438 aligned = ALIGN(last_addr, pmb_sizes[i].size) - phys;
439
Paul Mundt281983d2010-03-04 16:44:20 +0900440 /*
441 * XXX: This should really start from uncached_end, but this
442 * causes the MMU to reset, so for now we restrict it to the
443 * 0xb000...0xc000 range.
444 */
445 area = __get_vm_area_caller(aligned, VM_IOREMAP, 0xb0000000,
Paul Mundt6eb3c732010-03-02 17:22:29 +0900446 P3SEG, caller);
447 if (!area)
448 return NULL;
449
450 area->phys_addr = phys;
Paul Mundt281983d2010-03-04 16:44:20 +0900451 vaddr = (unsigned long)area->addr;
Paul Mundt6eb3c732010-03-02 17:22:29 +0900452
453 ret = pmb_bolt_mapping(vaddr, phys, size, prot);
Paul Mundta1042aa2010-03-03 13:13:25 +0900454 if (unlikely(ret != 0))
Paul Mundt6eb3c732010-03-02 17:22:29 +0900455 return ERR_PTR(ret);
456
Paul Mundt281983d2010-03-04 16:44:20 +0900457 return (void __iomem *)(offset + (char *)vaddr);
Paul Mundtd7cdc9e2006-09-27 15:16:42 +0900458}
459
Paul Mundt90e7d642010-02-23 16:20:53 +0900460int pmb_unmap(void __iomem *addr)
Paul Mundtd7cdc9e2006-09-27 15:16:42 +0900461{
Paul Mundtd53a0d32010-02-17 21:17:02 +0900462 struct pmb_entry *pmbe = NULL;
Paul Mundt90e7d642010-02-23 16:20:53 +0900463 unsigned long vaddr = (unsigned long __force)addr;
464 int i, found = 0;
Paul Mundtd7cdc9e2006-09-27 15:16:42 +0900465
Paul Mundtd53a0d32010-02-17 21:17:02 +0900466 read_lock(&pmb_rwlock);
467
Matt Flemingedd7de82009-10-06 21:22:29 +0000468 for (i = 0; i < ARRAY_SIZE(pmb_entry_list); i++) {
Paul Mundt51becfd2010-02-17 15:33:30 +0900469 if (test_bit(i, pmb_map)) {
Matt Flemingedd7de82009-10-06 21:22:29 +0000470 pmbe = &pmb_entry_list[i];
Paul Mundt90e7d642010-02-23 16:20:53 +0900471 if (pmbe->vpn == vaddr) {
472 found = 1;
Matt Flemingedd7de82009-10-06 21:22:29 +0000473 break;
Paul Mundt90e7d642010-02-23 16:20:53 +0900474 }
Matt Flemingedd7de82009-10-06 21:22:29 +0000475 }
476 }
Paul Mundtd53a0d32010-02-17 21:17:02 +0900477
478 read_unlock(&pmb_rwlock);
479
Paul Mundt90e7d642010-02-23 16:20:53 +0900480 if (found) {
481 pmb_unmap_entry(pmbe, NR_PMB_ENTRIES);
482 return 0;
483 }
Paul Mundtd7cdc9e2006-09-27 15:16:42 +0900484
Paul Mundt90e7d642010-02-23 16:20:53 +0900485 return -EINVAL;
Paul Mundtd01447b2010-02-18 18:13:51 +0900486}
487
488static void __pmb_unmap_entry(struct pmb_entry *pmbe, int depth)
489{
Paul Mundtd7cdc9e2006-09-27 15:16:42 +0900490 do {
491 struct pmb_entry *pmblink = pmbe;
492
Matt Fleming067784f2009-10-06 21:22:23 +0000493 /*
494 * We may be called before this pmb_entry has been
495 * entered into the PMB table via set_pmb_entry(), but
496 * that's OK because we've allocated a unique slot for
497 * this entry in pmb_alloc() (even if we haven't filled
498 * it yet).
499 *
Paul Mundtd53a0d32010-02-17 21:17:02 +0900500 * Therefore, calling __clear_pmb_entry() is safe as no
Matt Fleming067784f2009-10-06 21:22:23 +0000501 * other mapping can be using that slot.
502 */
Paul Mundtd53a0d32010-02-17 21:17:02 +0900503 __clear_pmb_entry(pmbe);
Matt Flemingfc2bdef2009-10-06 21:22:22 +0000504
Paul Mundt281983d2010-03-04 16:44:20 +0900505 flush_cache_vunmap(pmbe->vpn, pmbe->vpn + pmbe->size);
506
Paul Mundtd7cdc9e2006-09-27 15:16:42 +0900507 pmbe = pmblink->link;
508
509 pmb_free(pmblink);
Paul Mundtd01447b2010-02-18 18:13:51 +0900510 } while (pmbe && --depth);
511}
Paul Mundtd53a0d32010-02-17 21:17:02 +0900512
Paul Mundtd01447b2010-02-18 18:13:51 +0900513static void pmb_unmap_entry(struct pmb_entry *pmbe, int depth)
514{
515 unsigned long flags;
516
517 if (unlikely(!pmbe))
518 return;
519
520 write_lock_irqsave(&pmb_rwlock, flags);
521 __pmb_unmap_entry(pmbe, depth);
Paul Mundtd53a0d32010-02-17 21:17:02 +0900522 write_unlock_irqrestore(&pmb_rwlock, flags);
Paul Mundtd7cdc9e2006-09-27 15:16:42 +0900523}
524
Paul Mundtd01447b2010-02-18 18:13:51 +0900525static void __init pmb_notify(void)
Matt Fleming20b50142009-10-06 21:22:33 +0000526{
Paul Mundtd01447b2010-02-18 18:13:51 +0900527 int i;
Matt Fleming3d467672010-01-18 19:33:10 +0900528
Paul Mundtefd54ea2010-02-16 18:39:30 +0900529 pr_info("PMB: boot mappings:\n");
Matt Fleming3d467672010-01-18 19:33:10 +0900530
Paul Mundtd01447b2010-02-18 18:13:51 +0900531 read_lock(&pmb_rwlock);
532
533 for (i = 0; i < ARRAY_SIZE(pmb_entry_list); i++) {
534 struct pmb_entry *pmbe;
535
536 if (!test_bit(i, pmb_map))
537 continue;
538
539 pmbe = &pmb_entry_list[i];
540
541 pr_info(" 0x%08lx -> 0x%08lx [ %4ldMB %2scached ]\n",
542 pmbe->vpn >> PAGE_SHIFT, pmbe->ppn >> PAGE_SHIFT,
543 pmbe->size >> 20, (pmbe->flags & PMB_C) ? "" : "un");
544 }
545
546 read_unlock(&pmb_rwlock);
547}
548
549/*
550 * Sync our software copy of the PMB mappings with those in hardware. The
551 * mappings in the hardware PMB were either set up by the bootloader or
552 * very early on by the kernel.
553 */
554static void __init pmb_synchronize(void)
555{
556 struct pmb_entry *pmbp = NULL;
557 int i, j;
558
Matt Fleming3d467672010-01-18 19:33:10 +0900559 /*
Paul Mundtefd54ea2010-02-16 18:39:30 +0900560 * Run through the initial boot mappings, log the established
561 * ones, and blow away anything that falls outside of the valid
562 * PPN range. Specifically, we only care about existing mappings
563 * that impact the cached/uncached sections.
Matt Fleming3d467672010-01-18 19:33:10 +0900564 *
Paul Mundtefd54ea2010-02-16 18:39:30 +0900565 * Note that touching these can be a bit of a minefield; the boot
566 * loader can establish multi-page mappings with the same caching
567 * attributes, so we need to ensure that we aren't modifying a
568 * mapping that we're presently executing from, or may execute
569 * from in the case of straddling page boundaries.
Matt Fleming3d467672010-01-18 19:33:10 +0900570 *
Paul Mundtefd54ea2010-02-16 18:39:30 +0900571 * In the future we will have to tidy up after the boot loader by
572 * jumping between the cached and uncached mappings and tearing
573 * down alternating mappings while executing from the other.
Matt Fleming3d467672010-01-18 19:33:10 +0900574 */
Paul Mundt51becfd2010-02-17 15:33:30 +0900575 for (i = 0; i < NR_PMB_ENTRIES; i++) {
Matt Fleming3d467672010-01-18 19:33:10 +0900576 unsigned long addr, data;
577 unsigned long addr_val, data_val;
Paul Mundtefd54ea2010-02-16 18:39:30 +0900578 unsigned long ppn, vpn, flags;
Paul Mundtd53a0d32010-02-17 21:17:02 +0900579 unsigned long irqflags;
Paul Mundtd7813bc2010-02-17 17:56:38 +0900580 unsigned int size;
Paul Mundtefd54ea2010-02-16 18:39:30 +0900581 struct pmb_entry *pmbe;
Matt Fleming3d467672010-01-18 19:33:10 +0900582
583 addr = mk_pmb_addr(i);
584 data = mk_pmb_data(i);
585
586 addr_val = __raw_readl(addr);
587 data_val = __raw_readl(data);
588
589 /*
590 * Skip over any bogus entries
591 */
592 if (!(data_val & PMB_V) || !(addr_val & PMB_V))
593 continue;
594
595 ppn = data_val & PMB_PFN_MASK;
596 vpn = addr_val & PMB_PFN_MASK;
597
598 /*
599 * Only preserve in-range mappings.
600 */
Paul Mundtefd54ea2010-02-16 18:39:30 +0900601 if (!pmb_ppn_in_range(ppn)) {
Matt Fleming3d467672010-01-18 19:33:10 +0900602 /*
603 * Invalidate anything out of bounds.
604 */
Paul Mundt2e450642010-02-18 13:26:05 +0900605 writel_uncached(addr_val & ~PMB_V, addr);
606 writel_uncached(data_val & ~PMB_V, data);
Paul Mundtefd54ea2010-02-16 18:39:30 +0900607 continue;
Matt Fleming3d467672010-01-18 19:33:10 +0900608 }
Paul Mundtefd54ea2010-02-16 18:39:30 +0900609
610 /*
611 * Update the caching attributes if necessary
612 */
613 if (data_val & PMB_C) {
Paul Mundt0065b962010-02-17 18:05:23 +0900614 data_val &= ~PMB_CACHE_MASK;
615 data_val |= pmb_cache_flags();
Paul Mundt2e450642010-02-18 13:26:05 +0900616
617 writel_uncached(data_val, data);
Paul Mundtefd54ea2010-02-16 18:39:30 +0900618 }
619
Paul Mundtd7813bc2010-02-17 17:56:38 +0900620 size = data_val & PMB_SZ_MASK;
621 flags = size | (data_val & PMB_CACHE_MASK);
Paul Mundtefd54ea2010-02-16 18:39:30 +0900622
623 pmbe = pmb_alloc(vpn, ppn, flags, i);
624 if (IS_ERR(pmbe)) {
625 WARN_ON_ONCE(1);
626 continue;
627 }
628
Paul Mundtd53a0d32010-02-17 21:17:02 +0900629 spin_lock_irqsave(&pmbe->lock, irqflags);
630
Paul Mundtd7813bc2010-02-17 17:56:38 +0900631 for (j = 0; j < ARRAY_SIZE(pmb_sizes); j++)
632 if (pmb_sizes[j].flag == size)
633 pmbe->size = pmb_sizes[j].size;
634
Paul Mundtd53a0d32010-02-17 21:17:02 +0900635 if (pmbp) {
636 spin_lock(&pmbp->lock);
637
638 /*
639 * Compare the previous entry against the current one to
640 * see if the entries span a contiguous mapping. If so,
Paul Mundtd01447b2010-02-18 18:13:51 +0900641 * setup the entry links accordingly. Compound mappings
642 * are later coalesced.
Paul Mundtd53a0d32010-02-17 21:17:02 +0900643 */
Paul Mundtd01447b2010-02-18 18:13:51 +0900644 if (pmb_can_merge(pmbp, pmbe))
Paul Mundtd53a0d32010-02-17 21:17:02 +0900645 pmbp->link = pmbe;
646
647 spin_unlock(&pmbp->lock);
648 }
Paul Mundtd7813bc2010-02-17 17:56:38 +0900649
650 pmbp = pmbe;
651
Paul Mundtd53a0d32010-02-17 21:17:02 +0900652 spin_unlock_irqrestore(&pmbe->lock, irqflags);
Matt Fleming3d467672010-01-18 19:33:10 +0900653 }
Matt Fleming3d467672010-01-18 19:33:10 +0900654}
Matt Fleming3d467672010-01-18 19:33:10 +0900655
Paul Mundtd01447b2010-02-18 18:13:51 +0900656static void __init pmb_merge(struct pmb_entry *head)
Matt Fleming3d467672010-01-18 19:33:10 +0900657{
Paul Mundtd01447b2010-02-18 18:13:51 +0900658 unsigned long span, newsize;
659 struct pmb_entry *tail;
660 int i = 1, depth = 0;
661
662 span = newsize = head->size;
663
664 tail = head->link;
665 while (tail) {
666 span += tail->size;
667
668 if (pmb_size_valid(span)) {
669 newsize = span;
670 depth = i;
671 }
672
673 /* This is the end of the line.. */
674 if (!tail->link)
675 break;
676
677 tail = tail->link;
678 i++;
679 }
Matt Fleming20b50142009-10-06 21:22:33 +0000680
Matt Fleming3d467672010-01-18 19:33:10 +0900681 /*
Paul Mundtd01447b2010-02-18 18:13:51 +0900682 * The merged page size must be valid.
Matt Fleming3d467672010-01-18 19:33:10 +0900683 */
Paul Mundtd01447b2010-02-18 18:13:51 +0900684 if (!pmb_size_valid(newsize))
685 return;
686
687 head->flags &= ~PMB_SZ_MASK;
688 head->flags |= pmb_size_to_flags(newsize);
689
690 head->size = newsize;
691
692 __pmb_unmap_entry(head->link, depth);
693 __set_pmb_entry(head);
694}
695
696static void __init pmb_coalesce(void)
697{
698 unsigned long flags;
699 int i;
700
701 write_lock_irqsave(&pmb_rwlock, flags);
702
703 for (i = 0; i < ARRAY_SIZE(pmb_entry_list); i++) {
704 struct pmb_entry *pmbe;
705
706 if (!test_bit(i, pmb_map))
707 continue;
708
709 pmbe = &pmb_entry_list[i];
710
711 /*
712 * We're only interested in compound mappings
713 */
714 if (!pmbe->link)
715 continue;
716
717 /*
718 * Nothing to do if it already uses the largest possible
719 * page size.
720 */
721 if (pmbe->size == SZ_512M)
722 continue;
723
724 pmb_merge(pmbe);
725 }
726
727 write_unlock_irqrestore(&pmb_rwlock, flags);
728}
729
730#ifdef CONFIG_UNCACHED_MAPPING
731static void __init pmb_resize(void)
732{
733 int i;
734
735 /*
736 * If the uncached mapping was constructed by the kernel, it will
737 * already be a reasonable size.
738 */
739 if (uncached_size == SZ_16M)
740 return;
741
742 read_lock(&pmb_rwlock);
743
744 for (i = 0; i < ARRAY_SIZE(pmb_entry_list); i++) {
745 struct pmb_entry *pmbe;
746 unsigned long flags;
747
748 if (!test_bit(i, pmb_map))
749 continue;
750
751 pmbe = &pmb_entry_list[i];
752
753 if (pmbe->vpn != uncached_start)
754 continue;
755
756 /*
757 * Found it, now resize it.
758 */
759 spin_lock_irqsave(&pmbe->lock, flags);
760
761 pmbe->size = SZ_16M;
762 pmbe->flags &= ~PMB_SZ_MASK;
763 pmbe->flags |= pmb_size_to_flags(pmbe->size);
764
765 uncached_resize(pmbe->size);
766
767 __set_pmb_entry(pmbe);
768
769 spin_unlock_irqrestore(&pmbe->lock, flags);
770 }
771
772 read_lock(&pmb_rwlock);
773}
774#endif
775
Paul Mundt4cfa8e72010-03-02 16:49:50 +0900776static int __init early_pmb(char *p)
777{
778 if (!p)
779 return 0;
780
781 if (strstr(p, "iomap"))
782 pmb_iomapping_enabled = 1;
783
784 return 0;
785}
786early_param("pmb", early_pmb);
787
Paul Mundtd01447b2010-02-18 18:13:51 +0900788void __init pmb_init(void)
789{
790 /* Synchronize software state */
791 pmb_synchronize();
792
793 /* Attempt to combine compound mappings */
794 pmb_coalesce();
795
796#ifdef CONFIG_UNCACHED_MAPPING
797 /* Resize initial mappings, if necessary */
798 pmb_resize();
799#endif
800
801 /* Log them */
802 pmb_notify();
Matt Fleming20b50142009-10-06 21:22:33 +0000803
Paul Mundt2e450642010-02-18 13:26:05 +0900804 writel_uncached(0, PMB_IRMCR);
Paul Mundta0ab3662010-01-13 18:31:48 +0900805
Paul Mundta0ab3662010-01-13 18:31:48 +0900806 /* Flush out the TLB */
Matt Flemingb5b6c7e2010-03-21 19:51:52 +0000807 local_flush_tlb_all();
Paul Mundt2e450642010-02-18 13:26:05 +0900808 ctrl_barrier();
Matt Fleming20b50142009-10-06 21:22:33 +0000809}
Paul Mundt0c7b1df2006-09-27 15:08:07 +0900810
Paul Mundt2efa53b2010-01-20 16:40:48 +0900811bool __in_29bit_mode(void)
812{
813 return (__raw_readl(PMB_PASCR) & PASCR_SE) == 0;
814}
815
Paul Mundt0c7b1df2006-09-27 15:08:07 +0900816static int pmb_seq_show(struct seq_file *file, void *iter)
817{
818 int i;
819
820 seq_printf(file, "V: Valid, C: Cacheable, WT: Write-Through\n"
821 "CB: Copy-Back, B: Buffered, UB: Unbuffered\n");
822 seq_printf(file, "ety vpn ppn size flags\n");
823
824 for (i = 0; i < NR_PMB_ENTRIES; i++) {
825 unsigned long addr, data;
826 unsigned int size;
827 char *sz_str = NULL;
828
Paul Mundt9d56dd32010-01-26 12:58:40 +0900829 addr = __raw_readl(mk_pmb_addr(i));
830 data = __raw_readl(mk_pmb_data(i));
Paul Mundt0c7b1df2006-09-27 15:08:07 +0900831
832 size = data & PMB_SZ_MASK;
833 sz_str = (size == PMB_SZ_16M) ? " 16MB":
834 (size == PMB_SZ_64M) ? " 64MB":
835 (size == PMB_SZ_128M) ? "128MB":
836 "512MB";
837
838 /* 02: V 0x88 0x08 128MB C CB B */
839 seq_printf(file, "%02d: %c 0x%02lx 0x%02lx %s %c %s %s\n",
840 i, ((addr & PMB_V) && (data & PMB_V)) ? 'V' : ' ',
841 (addr >> 24) & 0xff, (data >> 24) & 0xff,
842 sz_str, (data & PMB_C) ? 'C' : ' ',
843 (data & PMB_WT) ? "WT" : "CB",
844 (data & PMB_UB) ? "UB" : " B");
845 }
846
847 return 0;
848}
849
850static int pmb_debugfs_open(struct inode *inode, struct file *file)
851{
852 return single_open(file, pmb_seq_show, NULL);
853}
854
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800855static const struct file_operations pmb_debugfs_fops = {
Paul Mundt0c7b1df2006-09-27 15:08:07 +0900856 .owner = THIS_MODULE,
857 .open = pmb_debugfs_open,
858 .read = seq_read,
859 .llseek = seq_lseek,
Li Zefan45dabf12008-06-24 13:30:23 +0800860 .release = single_release,
Paul Mundt0c7b1df2006-09-27 15:08:07 +0900861};
862
863static int __init pmb_debugfs_init(void)
864{
865 struct dentry *dentry;
866
867 dentry = debugfs_create_file("pmb", S_IFREG | S_IRUGO,
Paul Mundtb9e393c2008-03-07 17:19:58 +0900868 sh_debugfs_root, NULL, &pmb_debugfs_fops);
Zhaolei25627c72008-10-17 19:25:09 +0800869 if (!dentry)
870 return -ENOMEM;
Paul Mundt0c7b1df2006-09-27 15:08:07 +0900871 if (IS_ERR(dentry))
872 return PTR_ERR(dentry);
873
874 return 0;
875}
Pawel Moll62c8cbb2010-02-19 10:26:31 +0000876subsys_initcall(pmb_debugfs_init);
Francesco VIRLINZIa83c0b72009-03-11 10:39:02 +0000877
878#ifdef CONFIG_PM
879static int pmb_sysdev_suspend(struct sys_device *dev, pm_message_t state)
880{
881 static pm_message_t prev_state;
Matt Flemingedd7de82009-10-06 21:22:29 +0000882 int i;
Francesco VIRLINZIa83c0b72009-03-11 10:39:02 +0000883
884 /* Restore the PMB after a resume from hibernation */
885 if (state.event == PM_EVENT_ON &&
886 prev_state.event == PM_EVENT_FREEZE) {
887 struct pmb_entry *pmbe;
Paul Mundtd53a0d32010-02-17 21:17:02 +0900888
889 read_lock(&pmb_rwlock);
890
Matt Flemingedd7de82009-10-06 21:22:29 +0000891 for (i = 0; i < ARRAY_SIZE(pmb_entry_list); i++) {
Paul Mundt51becfd2010-02-17 15:33:30 +0900892 if (test_bit(i, pmb_map)) {
Matt Flemingedd7de82009-10-06 21:22:29 +0000893 pmbe = &pmb_entry_list[i];
894 set_pmb_entry(pmbe);
895 }
896 }
Paul Mundtd53a0d32010-02-17 21:17:02 +0900897
898 read_unlock(&pmb_rwlock);
Francesco VIRLINZIa83c0b72009-03-11 10:39:02 +0000899 }
Paul Mundtd53a0d32010-02-17 21:17:02 +0900900
Francesco VIRLINZIa83c0b72009-03-11 10:39:02 +0000901 prev_state = state;
Paul Mundtd53a0d32010-02-17 21:17:02 +0900902
Francesco VIRLINZIa83c0b72009-03-11 10:39:02 +0000903 return 0;
904}
905
906static int pmb_sysdev_resume(struct sys_device *dev)
907{
908 return pmb_sysdev_suspend(dev, PMSG_ON);
909}
910
911static struct sysdev_driver pmb_sysdev_driver = {
912 .suspend = pmb_sysdev_suspend,
913 .resume = pmb_sysdev_resume,
914};
915
916static int __init pmb_sysdev_init(void)
917{
918 return sysdev_driver_register(&cpu_sysdev_class, &pmb_sysdev_driver);
919}
Francesco VIRLINZIa83c0b72009-03-11 10:39:02 +0000920subsys_initcall(pmb_sysdev_init);
921#endif