blob: e43ec600afcfbc9b853522ea4df330d1991de589 [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>
Paul Mundt0c7b1df2006-09-27 15:08:07 +090018#include <linux/bitops.h>
19#include <linux/debugfs.h>
20#include <linux/fs.h>
21#include <linux/seq_file.h>
22#include <linux/err.h>
Paul Mundt51becfd2010-02-17 15:33:30 +090023#include <linux/io.h>
Paul Mundtd53a0d32010-02-17 21:17:02 +090024#include <linux/spinlock.h>
Paul Mundt90e7d642010-02-23 16:20:53 +090025#include <linux/vmalloc.h>
Paul Mundt281983d2010-03-04 16:44:20 +090026#include <asm/cacheflush.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 Mundt4cfa8e72010-03-02 16:49:50 +090071static unsigned int pmb_iomapping_enabled;
72
Paul Mundt51becfd2010-02-17 15:33:30 +090073static __always_inline unsigned long mk_pmb_entry(unsigned int entry)
Paul Mundt0c7b1df2006-09-27 15:08:07 +090074{
75 return (entry & PMB_E_MASK) << PMB_E_SHIFT;
76}
77
Paul Mundt51becfd2010-02-17 15:33:30 +090078static __always_inline unsigned long mk_pmb_addr(unsigned int entry)
Paul Mundt0c7b1df2006-09-27 15:08:07 +090079{
80 return mk_pmb_entry(entry) | PMB_ADDR;
81}
82
Paul Mundt51becfd2010-02-17 15:33:30 +090083static __always_inline unsigned long mk_pmb_data(unsigned int entry)
Paul Mundt0c7b1df2006-09-27 15:08:07 +090084{
85 return mk_pmb_entry(entry) | PMB_DATA;
86}
87
Paul Mundt90e7d642010-02-23 16:20:53 +090088static __always_inline unsigned int pmb_ppn_in_range(unsigned long ppn)
89{
90 return ppn >= __pa(memory_start) && ppn < __pa(memory_end);
91}
92
93/*
94 * Ensure that the PMB entries match our cache configuration.
95 *
96 * When we are in 32-bit address extended mode, CCR.CB becomes
97 * invalid, so care must be taken to manually adjust cacheable
98 * translations.
99 */
100static __always_inline unsigned long pmb_cache_flags(void)
101{
102 unsigned long flags = 0;
103
104#if defined(CONFIG_CACHE_OFF)
105 flags |= PMB_WT | PMB_UB;
106#elif defined(CONFIG_CACHE_WRITETHROUGH)
107 flags |= PMB_C | PMB_WT | PMB_UB;
108#elif defined(CONFIG_CACHE_WRITEBACK)
109 flags |= PMB_C;
110#endif
111
112 return flags;
113}
114
115/*
116 * Convert typical pgprot value to the PMB equivalent
117 */
118static inline unsigned long pgprot_to_pmb_flags(pgprot_t prot)
119{
120 unsigned long pmb_flags = 0;
121 u64 flags = pgprot_val(prot);
122
123 if (flags & _PAGE_CACHABLE)
124 pmb_flags |= PMB_C;
125 if (flags & _PAGE_WT)
126 pmb_flags |= PMB_WT | PMB_UB;
127
128 return pmb_flags;
129}
130
Paul Mundta1042aa2010-03-03 13:13:25 +0900131static inline bool pmb_can_merge(struct pmb_entry *a, struct pmb_entry *b)
Paul Mundt90e7d642010-02-23 16:20:53 +0900132{
133 return (b->vpn == (a->vpn + a->size)) &&
134 (b->ppn == (a->ppn + a->size)) &&
135 (b->flags == a->flags);
136}
137
Paul Mundta1042aa2010-03-03 13:13:25 +0900138static bool pmb_mapping_exists(unsigned long vaddr, phys_addr_t phys,
139 unsigned long size)
140{
141 int i;
142
143 read_lock(&pmb_rwlock);
144
145 for (i = 0; i < ARRAY_SIZE(pmb_entry_list); i++) {
146 struct pmb_entry *pmbe, *iter;
147 unsigned long span;
148
149 if (!test_bit(i, pmb_map))
150 continue;
151
152 pmbe = &pmb_entry_list[i];
153
154 /*
155 * See if VPN and PPN are bounded by an existing mapping.
156 */
157 if ((vaddr < pmbe->vpn) || (vaddr >= (pmbe->vpn + pmbe->size)))
158 continue;
159 if ((phys < pmbe->ppn) || (phys >= (pmbe->ppn + pmbe->size)))
160 continue;
161
162 /*
163 * Now see if we're in range of a simple mapping.
164 */
165 if (size <= pmbe->size) {
166 read_unlock(&pmb_rwlock);
167 return true;
168 }
169
170 span = pmbe->size;
171
172 /*
173 * Finally for sizes that involve compound mappings, walk
174 * the chain.
175 */
176 for (iter = pmbe->link; iter; iter = iter->link)
177 span += iter->size;
178
179 /*
180 * Nothing else to do if the range requirements are met.
181 */
182 if (size <= span) {
183 read_unlock(&pmb_rwlock);
184 return true;
185 }
186 }
187
188 read_unlock(&pmb_rwlock);
189 return false;
190}
191
Paul Mundt90e7d642010-02-23 16:20:53 +0900192static bool pmb_size_valid(unsigned long size)
193{
194 int i;
195
196 for (i = 0; i < ARRAY_SIZE(pmb_sizes); i++)
197 if (pmb_sizes[i].size == size)
198 return true;
199
200 return false;
201}
202
203static inline bool pmb_addr_valid(unsigned long addr, unsigned long size)
204{
205 return (addr >= P1SEG && (addr + size - 1) < P3SEG);
206}
207
208static inline bool pmb_prot_valid(pgprot_t prot)
209{
210 return (pgprot_val(prot) & _PAGE_USER) == 0;
211}
212
213static int pmb_size_to_flags(unsigned long size)
214{
215 int i;
216
217 for (i = 0; i < ARRAY_SIZE(pmb_sizes); i++)
218 if (pmb_sizes[i].size == size)
219 return pmb_sizes[i].flag;
220
221 return 0;
222}
223
Matt Fleming067784f2009-10-06 21:22:23 +0000224static int pmb_alloc_entry(void)
225{
Paul Mundtd53a0d32010-02-17 21:17:02 +0900226 int pos;
Matt Fleming067784f2009-10-06 21:22:23 +0000227
Paul Mundt51becfd2010-02-17 15:33:30 +0900228 pos = find_first_zero_bit(pmb_map, NR_PMB_ENTRIES);
Paul Mundtd53a0d32010-02-17 21:17:02 +0900229 if (pos >= 0 && pos < NR_PMB_ENTRIES)
230 __set_bit(pos, pmb_map);
231 else
232 pos = -ENOSPC;
Matt Fleming067784f2009-10-06 21:22:23 +0000233
234 return pos;
235}
236
Matt Fleming8386aeb2009-10-06 21:22:28 +0000237static struct pmb_entry *pmb_alloc(unsigned long vpn, unsigned long ppn,
Matt Fleming20b50142009-10-06 21:22:33 +0000238 unsigned long flags, int entry)
Paul Mundt0c7b1df2006-09-27 15:08:07 +0900239{
240 struct pmb_entry *pmbe;
Paul Mundtd53a0d32010-02-17 21:17:02 +0900241 unsigned long irqflags;
242 void *ret = NULL;
Matt Fleming067784f2009-10-06 21:22:23 +0000243 int pos;
244
Paul Mundtd53a0d32010-02-17 21:17:02 +0900245 write_lock_irqsave(&pmb_rwlock, irqflags);
246
Matt Fleming20b50142009-10-06 21:22:33 +0000247 if (entry == PMB_NO_ENTRY) {
248 pos = pmb_alloc_entry();
Paul Mundtd53a0d32010-02-17 21:17:02 +0900249 if (unlikely(pos < 0)) {
250 ret = ERR_PTR(pos);
251 goto out;
252 }
Matt Fleming20b50142009-10-06 21:22:33 +0000253 } else {
Paul Mundtd53a0d32010-02-17 21:17:02 +0900254 if (__test_and_set_bit(entry, pmb_map)) {
255 ret = ERR_PTR(-ENOSPC);
256 goto out;
257 }
258
Matt Fleming20b50142009-10-06 21:22:33 +0000259 pos = entry;
260 }
Paul Mundt0c7b1df2006-09-27 15:08:07 +0900261
Paul Mundtd53a0d32010-02-17 21:17:02 +0900262 write_unlock_irqrestore(&pmb_rwlock, irqflags);
263
Matt Flemingedd7de82009-10-06 21:22:29 +0000264 pmbe = &pmb_entry_list[pos];
Paul Mundtd53a0d32010-02-17 21:17:02 +0900265
Paul Mundtd01447b2010-02-18 18:13:51 +0900266 memset(pmbe, 0, sizeof(struct pmb_entry));
267
Paul Mundtd53a0d32010-02-17 21:17:02 +0900268 spin_lock_init(&pmbe->lock);
Paul Mundt0c7b1df2006-09-27 15:08:07 +0900269
270 pmbe->vpn = vpn;
271 pmbe->ppn = ppn;
272 pmbe->flags = flags;
Matt Fleming067784f2009-10-06 21:22:23 +0000273 pmbe->entry = pos;
Paul Mundt0c7b1df2006-09-27 15:08:07 +0900274
275 return pmbe;
Paul Mundtd53a0d32010-02-17 21:17:02 +0900276
277out:
278 write_unlock_irqrestore(&pmb_rwlock, irqflags);
279 return ret;
Paul Mundt0c7b1df2006-09-27 15:08:07 +0900280}
281
Matt Fleming8386aeb2009-10-06 21:22:28 +0000282static void pmb_free(struct pmb_entry *pmbe)
Paul Mundt0c7b1df2006-09-27 15:08:07 +0900283{
Paul Mundtd53a0d32010-02-17 21:17:02 +0900284 __clear_bit(pmbe->entry, pmb_map);
Paul Mundtd01447b2010-02-18 18:13:51 +0900285
286 pmbe->entry = PMB_NO_ENTRY;
287 pmbe->link = NULL;
Paul Mundt0c7b1df2006-09-27 15:08:07 +0900288}
289
290/*
Paul Mundt51becfd2010-02-17 15:33:30 +0900291 * Must be run uncached.
Paul Mundt0c7b1df2006-09-27 15:08:07 +0900292 */
Paul Mundtd53a0d32010-02-17 21:17:02 +0900293static void __set_pmb_entry(struct pmb_entry *pmbe)
Paul Mundt0c7b1df2006-09-27 15:08:07 +0900294{
Paul Mundt281983d2010-03-04 16:44:20 +0900295 unsigned long addr, data;
296
297 addr = mk_pmb_addr(pmbe->entry);
298 data = mk_pmb_data(pmbe->entry);
299
300 jump_to_uncached();
301
Paul Mundt90e7d642010-02-23 16:20:53 +0900302 /* Set V-bit */
Paul Mundt281983d2010-03-04 16:44:20 +0900303 __raw_writel(pmbe->vpn | PMB_V, addr);
304 __raw_writel(pmbe->ppn | pmbe->flags | PMB_V, data);
305
306 back_to_cached();
Paul Mundt0c7b1df2006-09-27 15:08:07 +0900307}
308
Paul Mundtd53a0d32010-02-17 21:17:02 +0900309static void __clear_pmb_entry(struct pmb_entry *pmbe)
Paul Mundt0c7b1df2006-09-27 15:08:07 +0900310{
Paul Mundt2e450642010-02-18 13:26:05 +0900311 unsigned long addr, data;
312 unsigned long addr_val, data_val;
Paul Mundt0c7b1df2006-09-27 15:08:07 +0900313
Paul Mundt2e450642010-02-18 13:26:05 +0900314 addr = mk_pmb_addr(pmbe->entry);
315 data = mk_pmb_data(pmbe->entry);
316
317 addr_val = __raw_readl(addr);
318 data_val = __raw_readl(data);
Paul Mundt0c7b1df2006-09-27 15:08:07 +0900319
320 /* Clear V-bit */
Paul Mundt2e450642010-02-18 13:26:05 +0900321 writel_uncached(addr_val & ~PMB_V, addr);
322 writel_uncached(data_val & ~PMB_V, data);
Paul Mundt0c7b1df2006-09-27 15:08:07 +0900323}
324
Matt Fleming3fe0f362010-03-22 22:09:58 +0000325#ifdef CONFIG_PM
Paul Mundtd53a0d32010-02-17 21:17:02 +0900326static void set_pmb_entry(struct pmb_entry *pmbe)
327{
328 unsigned long flags;
329
330 spin_lock_irqsave(&pmbe->lock, flags);
331 __set_pmb_entry(pmbe);
332 spin_unlock_irqrestore(&pmbe->lock, flags);
333}
Matt Fleming3fe0f362010-03-22 22:09:58 +0000334#endif /* CONFIG_PM */
Paul Mundtd53a0d32010-02-17 21:17:02 +0900335
Paul Mundt90e7d642010-02-23 16:20:53 +0900336int pmb_bolt_mapping(unsigned long vaddr, phys_addr_t phys,
337 unsigned long size, pgprot_t prot)
338{
Matt Flemingfc2bdef2009-10-06 21:22:22 +0000339 struct pmb_entry *pmbp, *pmbe;
Paul Mundt281983d2010-03-04 16:44:20 +0900340 unsigned long orig_addr, orig_size;
Paul Mundta1042aa2010-03-03 13:13:25 +0900341 unsigned long flags, pmb_flags;
Paul Mundt90e7d642010-02-23 16:20:53 +0900342 int i, mapped;
Paul Mundt7bdda622010-02-17 13:23:00 +0900343
Paul Mundt6eb3c732010-03-02 17:22:29 +0900344 if (!pmb_addr_valid(vaddr, size))
345 return -EFAULT;
Paul Mundta1042aa2010-03-03 13:13:25 +0900346 if (pmb_mapping_exists(vaddr, phys, size))
347 return 0;
Paul Mundt4cfa8e72010-03-02 16:49:50 +0900348
Paul Mundt281983d2010-03-04 16:44:20 +0900349 orig_addr = vaddr;
350 orig_size = size;
351
352 flush_tlb_kernel_range(vaddr, vaddr + size);
353
Paul Mundt90e7d642010-02-23 16:20:53 +0900354 pmb_flags = pgprot_to_pmb_flags(prot);
Paul Mundt6eb3c732010-03-02 17:22:29 +0900355 pmbp = NULL;
Paul Mundtd7cdc9e2006-09-27 15:16:42 +0900356
Paul Mundta1042aa2010-03-03 13:13:25 +0900357 do {
358 for (i = mapped = 0; i < ARRAY_SIZE(pmb_sizes); i++) {
359 if (size < pmb_sizes[i].size)
360 continue;
Paul Mundtd53a0d32010-02-17 21:17:02 +0900361
Paul Mundta1042aa2010-03-03 13:13:25 +0900362 pmbe = pmb_alloc(vaddr, phys, pmb_flags |
363 pmb_sizes[i].flag, PMB_NO_ENTRY);
364 if (IS_ERR(pmbe)) {
365 pmb_unmap_entry(pmbp, mapped);
366 return PTR_ERR(pmbe);
367 }
Paul Mundtd7cdc9e2006-09-27 15:16:42 +0900368
Paul Mundta1042aa2010-03-03 13:13:25 +0900369 spin_lock_irqsave(&pmbe->lock, flags);
370
371 pmbe->size = pmb_sizes[i].size;
372
373 __set_pmb_entry(pmbe);
374
375 phys += pmbe->size;
376 vaddr += pmbe->size;
377 size -= pmbe->size;
378
379 /*
380 * Link adjacent entries that span multiple PMB
381 * entries for easier tear-down.
382 */
383 if (likely(pmbp)) {
384 spin_lock(&pmbp->lock);
385 pmbp->link = pmbe;
386 spin_unlock(&pmbp->lock);
387 }
388
389 pmbp = pmbe;
390
391 /*
392 * Instead of trying smaller sizes on every
393 * iteration (even if we succeed in allocating
394 * space), try using pmb_sizes[i].size again.
395 */
396 i--;
397 mapped++;
398
399 spin_unlock_irqrestore(&pmbe->lock, flags);
Matt Flemingfc2bdef2009-10-06 21:22:22 +0000400 }
Paul Mundta1042aa2010-03-03 13:13:25 +0900401 } while (size >= SZ_16M);
Paul Mundtd7cdc9e2006-09-27 15:16:42 +0900402
Paul Mundt281983d2010-03-04 16:44:20 +0900403 flush_cache_vmap(orig_addr, orig_addr + orig_size);
404
Paul Mundt6eb3c732010-03-02 17:22:29 +0900405 return 0;
406}
407
408void __iomem *pmb_remap_caller(phys_addr_t phys, unsigned long size,
409 pgprot_t prot, void *caller)
410{
Paul Mundt281983d2010-03-04 16:44:20 +0900411 unsigned long vaddr;
Paul Mundt6eb3c732010-03-02 17:22:29 +0900412 phys_addr_t offset, last_addr;
413 phys_addr_t align_mask;
414 unsigned long aligned;
415 struct vm_struct *area;
416 int i, ret;
417
418 if (!pmb_iomapping_enabled)
419 return NULL;
420
421 /*
422 * Small mappings need to go through the TLB.
423 */
424 if (size < SZ_16M)
425 return ERR_PTR(-EINVAL);
426 if (!pmb_prot_valid(prot))
427 return ERR_PTR(-EINVAL);
428
429 for (i = 0; i < ARRAY_SIZE(pmb_sizes); i++)
430 if (size >= pmb_sizes[i].size)
431 break;
432
433 last_addr = phys + size;
434 align_mask = ~(pmb_sizes[i].size - 1);
435 offset = phys & ~align_mask;
436 phys &= align_mask;
437 aligned = ALIGN(last_addr, pmb_sizes[i].size) - phys;
438
Paul Mundt281983d2010-03-04 16:44:20 +0900439 /*
440 * XXX: This should really start from uncached_end, but this
441 * causes the MMU to reset, so for now we restrict it to the
442 * 0xb000...0xc000 range.
443 */
444 area = __get_vm_area_caller(aligned, VM_IOREMAP, 0xb0000000,
Paul Mundt6eb3c732010-03-02 17:22:29 +0900445 P3SEG, caller);
446 if (!area)
447 return NULL;
448
449 area->phys_addr = phys;
Paul Mundt281983d2010-03-04 16:44:20 +0900450 vaddr = (unsigned long)area->addr;
Paul Mundt6eb3c732010-03-02 17:22:29 +0900451
452 ret = pmb_bolt_mapping(vaddr, phys, size, prot);
Paul Mundta1042aa2010-03-03 13:13:25 +0900453 if (unlikely(ret != 0))
Paul Mundt6eb3c732010-03-02 17:22:29 +0900454 return ERR_PTR(ret);
455
Paul Mundt281983d2010-03-04 16:44:20 +0900456 return (void __iomem *)(offset + (char *)vaddr);
Paul Mundtd7cdc9e2006-09-27 15:16:42 +0900457}
458
Paul Mundt90e7d642010-02-23 16:20:53 +0900459int pmb_unmap(void __iomem *addr)
Paul Mundtd7cdc9e2006-09-27 15:16:42 +0900460{
Paul Mundtd53a0d32010-02-17 21:17:02 +0900461 struct pmb_entry *pmbe = NULL;
Paul Mundt90e7d642010-02-23 16:20:53 +0900462 unsigned long vaddr = (unsigned long __force)addr;
463 int i, found = 0;
Paul Mundtd7cdc9e2006-09-27 15:16:42 +0900464
Paul Mundtd53a0d32010-02-17 21:17:02 +0900465 read_lock(&pmb_rwlock);
466
Matt Flemingedd7de82009-10-06 21:22:29 +0000467 for (i = 0; i < ARRAY_SIZE(pmb_entry_list); i++) {
Paul Mundt51becfd2010-02-17 15:33:30 +0900468 if (test_bit(i, pmb_map)) {
Matt Flemingedd7de82009-10-06 21:22:29 +0000469 pmbe = &pmb_entry_list[i];
Paul Mundt90e7d642010-02-23 16:20:53 +0900470 if (pmbe->vpn == vaddr) {
471 found = 1;
Matt Flemingedd7de82009-10-06 21:22:29 +0000472 break;
Paul Mundt90e7d642010-02-23 16:20:53 +0900473 }
Matt Flemingedd7de82009-10-06 21:22:29 +0000474 }
475 }
Paul Mundtd53a0d32010-02-17 21:17:02 +0900476
477 read_unlock(&pmb_rwlock);
478
Paul Mundt90e7d642010-02-23 16:20:53 +0900479 if (found) {
480 pmb_unmap_entry(pmbe, NR_PMB_ENTRIES);
481 return 0;
482 }
Paul Mundtd7cdc9e2006-09-27 15:16:42 +0900483
Paul Mundt90e7d642010-02-23 16:20:53 +0900484 return -EINVAL;
Paul Mundtd01447b2010-02-18 18:13:51 +0900485}
486
487static void __pmb_unmap_entry(struct pmb_entry *pmbe, int depth)
488{
Paul Mundtd7cdc9e2006-09-27 15:16:42 +0900489 do {
490 struct pmb_entry *pmblink = pmbe;
491
Matt Fleming067784f2009-10-06 21:22:23 +0000492 /*
493 * We may be called before this pmb_entry has been
494 * entered into the PMB table via set_pmb_entry(), but
495 * that's OK because we've allocated a unique slot for
496 * this entry in pmb_alloc() (even if we haven't filled
497 * it yet).
498 *
Paul Mundtd53a0d32010-02-17 21:17:02 +0900499 * Therefore, calling __clear_pmb_entry() is safe as no
Matt Fleming067784f2009-10-06 21:22:23 +0000500 * other mapping can be using that slot.
501 */
Paul Mundtd53a0d32010-02-17 21:17:02 +0900502 __clear_pmb_entry(pmbe);
Matt Flemingfc2bdef2009-10-06 21:22:22 +0000503
Paul Mundt281983d2010-03-04 16:44:20 +0900504 flush_cache_vunmap(pmbe->vpn, pmbe->vpn + pmbe->size);
505
Paul Mundtd7cdc9e2006-09-27 15:16:42 +0900506 pmbe = pmblink->link;
507
508 pmb_free(pmblink);
Paul Mundtd01447b2010-02-18 18:13:51 +0900509 } while (pmbe && --depth);
510}
Paul Mundtd53a0d32010-02-17 21:17:02 +0900511
Paul Mundtd01447b2010-02-18 18:13:51 +0900512static void pmb_unmap_entry(struct pmb_entry *pmbe, int depth)
513{
514 unsigned long flags;
515
516 if (unlikely(!pmbe))
517 return;
518
519 write_lock_irqsave(&pmb_rwlock, flags);
520 __pmb_unmap_entry(pmbe, depth);
Paul Mundtd53a0d32010-02-17 21:17:02 +0900521 write_unlock_irqrestore(&pmb_rwlock, flags);
Paul Mundtd7cdc9e2006-09-27 15:16:42 +0900522}
523
Paul Mundtd01447b2010-02-18 18:13:51 +0900524static void __init pmb_notify(void)
Matt Fleming20b50142009-10-06 21:22:33 +0000525{
Paul Mundtd01447b2010-02-18 18:13:51 +0900526 int i;
Matt Fleming3d467672010-01-18 19:33:10 +0900527
Paul Mundtefd54ea2010-02-16 18:39:30 +0900528 pr_info("PMB: boot mappings:\n");
Matt Fleming3d467672010-01-18 19:33:10 +0900529
Paul Mundtd01447b2010-02-18 18:13:51 +0900530 read_lock(&pmb_rwlock);
531
532 for (i = 0; i < ARRAY_SIZE(pmb_entry_list); i++) {
533 struct pmb_entry *pmbe;
534
535 if (!test_bit(i, pmb_map))
536 continue;
537
538 pmbe = &pmb_entry_list[i];
539
540 pr_info(" 0x%08lx -> 0x%08lx [ %4ldMB %2scached ]\n",
541 pmbe->vpn >> PAGE_SHIFT, pmbe->ppn >> PAGE_SHIFT,
542 pmbe->size >> 20, (pmbe->flags & PMB_C) ? "" : "un");
543 }
544
545 read_unlock(&pmb_rwlock);
546}
547
548/*
549 * Sync our software copy of the PMB mappings with those in hardware. The
550 * mappings in the hardware PMB were either set up by the bootloader or
551 * very early on by the kernel.
552 */
553static void __init pmb_synchronize(void)
554{
555 struct pmb_entry *pmbp = NULL;
556 int i, j;
557
Matt Fleming3d467672010-01-18 19:33:10 +0900558 /*
Paul Mundtefd54ea2010-02-16 18:39:30 +0900559 * Run through the initial boot mappings, log the established
560 * ones, and blow away anything that falls outside of the valid
561 * PPN range. Specifically, we only care about existing mappings
562 * that impact the cached/uncached sections.
Matt Fleming3d467672010-01-18 19:33:10 +0900563 *
Paul Mundtefd54ea2010-02-16 18:39:30 +0900564 * Note that touching these can be a bit of a minefield; the boot
565 * loader can establish multi-page mappings with the same caching
566 * attributes, so we need to ensure that we aren't modifying a
567 * mapping that we're presently executing from, or may execute
568 * from in the case of straddling page boundaries.
Matt Fleming3d467672010-01-18 19:33:10 +0900569 *
Paul Mundtefd54ea2010-02-16 18:39:30 +0900570 * In the future we will have to tidy up after the boot loader by
571 * jumping between the cached and uncached mappings and tearing
572 * down alternating mappings while executing from the other.
Matt Fleming3d467672010-01-18 19:33:10 +0900573 */
Paul Mundt51becfd2010-02-17 15:33:30 +0900574 for (i = 0; i < NR_PMB_ENTRIES; i++) {
Matt Fleming3d467672010-01-18 19:33:10 +0900575 unsigned long addr, data;
576 unsigned long addr_val, data_val;
Paul Mundtefd54ea2010-02-16 18:39:30 +0900577 unsigned long ppn, vpn, flags;
Paul Mundtd53a0d32010-02-17 21:17:02 +0900578 unsigned long irqflags;
Paul Mundtd7813bc2010-02-17 17:56:38 +0900579 unsigned int size;
Paul Mundtefd54ea2010-02-16 18:39:30 +0900580 struct pmb_entry *pmbe;
Matt Fleming3d467672010-01-18 19:33:10 +0900581
582 addr = mk_pmb_addr(i);
583 data = mk_pmb_data(i);
584
585 addr_val = __raw_readl(addr);
586 data_val = __raw_readl(data);
587
588 /*
589 * Skip over any bogus entries
590 */
591 if (!(data_val & PMB_V) || !(addr_val & PMB_V))
592 continue;
593
594 ppn = data_val & PMB_PFN_MASK;
595 vpn = addr_val & PMB_PFN_MASK;
596
597 /*
598 * Only preserve in-range mappings.
599 */
Paul Mundtefd54ea2010-02-16 18:39:30 +0900600 if (!pmb_ppn_in_range(ppn)) {
Matt Fleming3d467672010-01-18 19:33:10 +0900601 /*
602 * Invalidate anything out of bounds.
603 */
Paul Mundt2e450642010-02-18 13:26:05 +0900604 writel_uncached(addr_val & ~PMB_V, addr);
605 writel_uncached(data_val & ~PMB_V, data);
Paul Mundtefd54ea2010-02-16 18:39:30 +0900606 continue;
Matt Fleming3d467672010-01-18 19:33:10 +0900607 }
Paul Mundtefd54ea2010-02-16 18:39:30 +0900608
609 /*
610 * Update the caching attributes if necessary
611 */
612 if (data_val & PMB_C) {
Paul Mundt0065b962010-02-17 18:05:23 +0900613 data_val &= ~PMB_CACHE_MASK;
614 data_val |= pmb_cache_flags();
Paul Mundt2e450642010-02-18 13:26:05 +0900615
616 writel_uncached(data_val, data);
Paul Mundtefd54ea2010-02-16 18:39:30 +0900617 }
618
Paul Mundtd7813bc2010-02-17 17:56:38 +0900619 size = data_val & PMB_SZ_MASK;
620 flags = size | (data_val & PMB_CACHE_MASK);
Paul Mundtefd54ea2010-02-16 18:39:30 +0900621
622 pmbe = pmb_alloc(vpn, ppn, flags, i);
623 if (IS_ERR(pmbe)) {
624 WARN_ON_ONCE(1);
625 continue;
626 }
627
Paul Mundtd53a0d32010-02-17 21:17:02 +0900628 spin_lock_irqsave(&pmbe->lock, irqflags);
629
Paul Mundtd7813bc2010-02-17 17:56:38 +0900630 for (j = 0; j < ARRAY_SIZE(pmb_sizes); j++)
631 if (pmb_sizes[j].flag == size)
632 pmbe->size = pmb_sizes[j].size;
633
Paul Mundtd53a0d32010-02-17 21:17:02 +0900634 if (pmbp) {
635 spin_lock(&pmbp->lock);
636
637 /*
638 * Compare the previous entry against the current one to
639 * see if the entries span a contiguous mapping. If so,
Paul Mundtd01447b2010-02-18 18:13:51 +0900640 * setup the entry links accordingly. Compound mappings
641 * are later coalesced.
Paul Mundtd53a0d32010-02-17 21:17:02 +0900642 */
Paul Mundtd01447b2010-02-18 18:13:51 +0900643 if (pmb_can_merge(pmbp, pmbe))
Paul Mundtd53a0d32010-02-17 21:17:02 +0900644 pmbp->link = pmbe;
645
646 spin_unlock(&pmbp->lock);
647 }
Paul Mundtd7813bc2010-02-17 17:56:38 +0900648
649 pmbp = pmbe;
650
Paul Mundtd53a0d32010-02-17 21:17:02 +0900651 spin_unlock_irqrestore(&pmbe->lock, irqflags);
Matt Fleming3d467672010-01-18 19:33:10 +0900652 }
Matt Fleming3d467672010-01-18 19:33:10 +0900653}
Matt Fleming3d467672010-01-18 19:33:10 +0900654
Paul Mundtd01447b2010-02-18 18:13:51 +0900655static void __init pmb_merge(struct pmb_entry *head)
Matt Fleming3d467672010-01-18 19:33:10 +0900656{
Paul Mundtd01447b2010-02-18 18:13:51 +0900657 unsigned long span, newsize;
658 struct pmb_entry *tail;
659 int i = 1, depth = 0;
660
661 span = newsize = head->size;
662
663 tail = head->link;
664 while (tail) {
665 span += tail->size;
666
667 if (pmb_size_valid(span)) {
668 newsize = span;
669 depth = i;
670 }
671
672 /* This is the end of the line.. */
673 if (!tail->link)
674 break;
675
676 tail = tail->link;
677 i++;
678 }
Matt Fleming20b50142009-10-06 21:22:33 +0000679
Matt Fleming3d467672010-01-18 19:33:10 +0900680 /*
Paul Mundtd01447b2010-02-18 18:13:51 +0900681 * The merged page size must be valid.
Matt Fleming3d467672010-01-18 19:33:10 +0900682 */
Paul Mundtd01447b2010-02-18 18:13:51 +0900683 if (!pmb_size_valid(newsize))
684 return;
685
686 head->flags &= ~PMB_SZ_MASK;
687 head->flags |= pmb_size_to_flags(newsize);
688
689 head->size = newsize;
690
691 __pmb_unmap_entry(head->link, depth);
692 __set_pmb_entry(head);
693}
694
695static void __init pmb_coalesce(void)
696{
697 unsigned long flags;
698 int i;
699
700 write_lock_irqsave(&pmb_rwlock, flags);
701
702 for (i = 0; i < ARRAY_SIZE(pmb_entry_list); i++) {
703 struct pmb_entry *pmbe;
704
705 if (!test_bit(i, pmb_map))
706 continue;
707
708 pmbe = &pmb_entry_list[i];
709
710 /*
711 * We're only interested in compound mappings
712 */
713 if (!pmbe->link)
714 continue;
715
716 /*
717 * Nothing to do if it already uses the largest possible
718 * page size.
719 */
720 if (pmbe->size == SZ_512M)
721 continue;
722
723 pmb_merge(pmbe);
724 }
725
726 write_unlock_irqrestore(&pmb_rwlock, flags);
727}
728
729#ifdef CONFIG_UNCACHED_MAPPING
730static void __init pmb_resize(void)
731{
732 int i;
733
734 /*
735 * If the uncached mapping was constructed by the kernel, it will
736 * already be a reasonable size.
737 */
738 if (uncached_size == SZ_16M)
739 return;
740
741 read_lock(&pmb_rwlock);
742
743 for (i = 0; i < ARRAY_SIZE(pmb_entry_list); i++) {
744 struct pmb_entry *pmbe;
745 unsigned long flags;
746
747 if (!test_bit(i, pmb_map))
748 continue;
749
750 pmbe = &pmb_entry_list[i];
751
752 if (pmbe->vpn != uncached_start)
753 continue;
754
755 /*
756 * Found it, now resize it.
757 */
758 spin_lock_irqsave(&pmbe->lock, flags);
759
760 pmbe->size = SZ_16M;
761 pmbe->flags &= ~PMB_SZ_MASK;
762 pmbe->flags |= pmb_size_to_flags(pmbe->size);
763
764 uncached_resize(pmbe->size);
765
766 __set_pmb_entry(pmbe);
767
768 spin_unlock_irqrestore(&pmbe->lock, flags);
769 }
770
771 read_lock(&pmb_rwlock);
772}
773#endif
774
Paul Mundt4cfa8e72010-03-02 16:49:50 +0900775static int __init early_pmb(char *p)
776{
777 if (!p)
778 return 0;
779
780 if (strstr(p, "iomap"))
781 pmb_iomapping_enabled = 1;
782
783 return 0;
784}
785early_param("pmb", early_pmb);
786
Paul Mundtd01447b2010-02-18 18:13:51 +0900787void __init pmb_init(void)
788{
789 /* Synchronize software state */
790 pmb_synchronize();
791
792 /* Attempt to combine compound mappings */
793 pmb_coalesce();
794
795#ifdef CONFIG_UNCACHED_MAPPING
796 /* Resize initial mappings, if necessary */
797 pmb_resize();
798#endif
799
800 /* Log them */
801 pmb_notify();
Matt Fleming20b50142009-10-06 21:22:33 +0000802
Paul Mundt2e450642010-02-18 13:26:05 +0900803 writel_uncached(0, PMB_IRMCR);
Paul Mundta0ab3662010-01-13 18:31:48 +0900804
Paul Mundta0ab3662010-01-13 18:31:48 +0900805 /* Flush out the TLB */
Matt Flemingb5b6c7e2010-03-21 19:51:52 +0000806 local_flush_tlb_all();
Paul Mundt2e450642010-02-18 13:26:05 +0900807 ctrl_barrier();
Matt Fleming20b50142009-10-06 21:22:33 +0000808}
Paul Mundt0c7b1df2006-09-27 15:08:07 +0900809
Paul Mundt2efa53b2010-01-20 16:40:48 +0900810bool __in_29bit_mode(void)
811{
812 return (__raw_readl(PMB_PASCR) & PASCR_SE) == 0;
813}
814
Paul Mundt0c7b1df2006-09-27 15:08:07 +0900815static int pmb_seq_show(struct seq_file *file, void *iter)
816{
817 int i;
818
819 seq_printf(file, "V: Valid, C: Cacheable, WT: Write-Through\n"
820 "CB: Copy-Back, B: Buffered, UB: Unbuffered\n");
821 seq_printf(file, "ety vpn ppn size flags\n");
822
823 for (i = 0; i < NR_PMB_ENTRIES; i++) {
824 unsigned long addr, data;
825 unsigned int size;
826 char *sz_str = NULL;
827
Paul Mundt9d56dd32010-01-26 12:58:40 +0900828 addr = __raw_readl(mk_pmb_addr(i));
829 data = __raw_readl(mk_pmb_data(i));
Paul Mundt0c7b1df2006-09-27 15:08:07 +0900830
831 size = data & PMB_SZ_MASK;
832 sz_str = (size == PMB_SZ_16M) ? " 16MB":
833 (size == PMB_SZ_64M) ? " 64MB":
834 (size == PMB_SZ_128M) ? "128MB":
835 "512MB";
836
837 /* 02: V 0x88 0x08 128MB C CB B */
838 seq_printf(file, "%02d: %c 0x%02lx 0x%02lx %s %c %s %s\n",
839 i, ((addr & PMB_V) && (data & PMB_V)) ? 'V' : ' ',
840 (addr >> 24) & 0xff, (data >> 24) & 0xff,
841 sz_str, (data & PMB_C) ? 'C' : ' ',
842 (data & PMB_WT) ? "WT" : "CB",
843 (data & PMB_UB) ? "UB" : " B");
844 }
845
846 return 0;
847}
848
849static int pmb_debugfs_open(struct inode *inode, struct file *file)
850{
851 return single_open(file, pmb_seq_show, NULL);
852}
853
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800854static const struct file_operations pmb_debugfs_fops = {
Paul Mundt0c7b1df2006-09-27 15:08:07 +0900855 .owner = THIS_MODULE,
856 .open = pmb_debugfs_open,
857 .read = seq_read,
858 .llseek = seq_lseek,
Li Zefan45dabf12008-06-24 13:30:23 +0800859 .release = single_release,
Paul Mundt0c7b1df2006-09-27 15:08:07 +0900860};
861
862static int __init pmb_debugfs_init(void)
863{
864 struct dentry *dentry;
865
866 dentry = debugfs_create_file("pmb", S_IFREG | S_IRUGO,
Paul Mundtb9e393c2008-03-07 17:19:58 +0900867 sh_debugfs_root, NULL, &pmb_debugfs_fops);
Zhaolei25627c72008-10-17 19:25:09 +0800868 if (!dentry)
869 return -ENOMEM;
Paul Mundt0c7b1df2006-09-27 15:08:07 +0900870 if (IS_ERR(dentry))
871 return PTR_ERR(dentry);
872
873 return 0;
874}
Pawel Moll62c8cbb2010-02-19 10:26:31 +0000875subsys_initcall(pmb_debugfs_init);
Francesco VIRLINZIa83c0b72009-03-11 10:39:02 +0000876
877#ifdef CONFIG_PM
878static int pmb_sysdev_suspend(struct sys_device *dev, pm_message_t state)
879{
880 static pm_message_t prev_state;
Matt Flemingedd7de82009-10-06 21:22:29 +0000881 int i;
Francesco VIRLINZIa83c0b72009-03-11 10:39:02 +0000882
883 /* Restore the PMB after a resume from hibernation */
884 if (state.event == PM_EVENT_ON &&
885 prev_state.event == PM_EVENT_FREEZE) {
886 struct pmb_entry *pmbe;
Paul Mundtd53a0d32010-02-17 21:17:02 +0900887
888 read_lock(&pmb_rwlock);
889
Matt Flemingedd7de82009-10-06 21:22:29 +0000890 for (i = 0; i < ARRAY_SIZE(pmb_entry_list); i++) {
Paul Mundt51becfd2010-02-17 15:33:30 +0900891 if (test_bit(i, pmb_map)) {
Matt Flemingedd7de82009-10-06 21:22:29 +0000892 pmbe = &pmb_entry_list[i];
893 set_pmb_entry(pmbe);
894 }
895 }
Paul Mundtd53a0d32010-02-17 21:17:02 +0900896
897 read_unlock(&pmb_rwlock);
Francesco VIRLINZIa83c0b72009-03-11 10:39:02 +0000898 }
Paul Mundtd53a0d32010-02-17 21:17:02 +0900899
Francesco VIRLINZIa83c0b72009-03-11 10:39:02 +0000900 prev_state = state;
Paul Mundtd53a0d32010-02-17 21:17:02 +0900901
Francesco VIRLINZIa83c0b72009-03-11 10:39:02 +0000902 return 0;
903}
904
905static int pmb_sysdev_resume(struct sys_device *dev)
906{
907 return pmb_sysdev_suspend(dev, PMSG_ON);
908}
909
910static struct sysdev_driver pmb_sysdev_driver = {
911 .suspend = pmb_sysdev_suspend,
912 .resume = pmb_sysdev_resume,
913};
914
915static int __init pmb_sysdev_init(void)
916{
917 return sysdev_driver_register(&cpu_sysdev_class, &pmb_sysdev_driver);
918}
Francesco VIRLINZIa83c0b72009-03-11 10:39:02 +0000919subsys_initcall(pmb_sysdev_init);
920#endif