blob: 58f935896b44ff643aa06a2541ea48e3fbe7b8cc [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 *
Paul Mundt38c425f2007-05-11 11:26:10 +09006 * Copyright (C) 2005, 2006, 2007 Paul Mundt
Paul Mundt0c7b1df2006-09-27 15:08:07 +09007 *
8 * P1/P2 Section mapping definitions from map32.h, which was:
9 *
10 * Copyright 2003 (c) Lineo Solutions,Inc.
11 *
12 * This file is subject to the terms and conditions of the GNU General Public
13 * License. See the file "COPYING" in the main directory of this archive
14 * for more details.
15 */
16#include <linux/init.h>
17#include <linux/kernel.h>
Francesco VIRLINZIa83c0b72009-03-11 10:39:02 +000018#include <linux/sysdev.h>
19#include <linux/cpu.h>
Paul Mundt0c7b1df2006-09-27 15:08:07 +090020#include <linux/module.h>
21#include <linux/slab.h>
22#include <linux/bitops.h>
23#include <linux/debugfs.h>
24#include <linux/fs.h>
25#include <linux/seq_file.h>
26#include <linux/err.h>
27#include <asm/system.h>
28#include <asm/uaccess.h>
Paul Mundtd7cdc9e2006-09-27 15:16:42 +090029#include <asm/pgtable.h>
Paul Mundt0c7b1df2006-09-27 15:08:07 +090030#include <asm/mmu.h>
31#include <asm/io.h>
Stuart Menefyeddeeb32007-11-26 21:32:40 +090032#include <asm/mmu_context.h>
Paul Mundt0c7b1df2006-09-27 15:08:07 +090033
34#define NR_PMB_ENTRIES 16
35
Matt Flemingfc2bdef2009-10-06 21:22:22 +000036static void __pmb_unmap(struct pmb_entry *);
37
Christoph Lametere18b8902006-12-06 20:33:20 -080038static struct kmem_cache *pmb_cache;
Paul Mundt0c7b1df2006-09-27 15:08:07 +090039static unsigned long pmb_map;
40
41static struct pmb_entry pmb_init_map[] = {
42 /* vpn ppn flags (ub/sz/c/wt) */
43
44 /* P1 Section Mappings */
45 { 0x80000000, 0x00000000, PMB_SZ_64M | PMB_C, },
46 { 0x84000000, 0x04000000, PMB_SZ_64M | PMB_C, },
47 { 0x88000000, 0x08000000, PMB_SZ_128M | PMB_C, },
48 { 0x90000000, 0x10000000, PMB_SZ_64M | PMB_C, },
49 { 0x94000000, 0x14000000, PMB_SZ_64M | PMB_C, },
50 { 0x98000000, 0x18000000, PMB_SZ_64M | PMB_C, },
51
52 /* P2 Section Mappings */
53 { 0xa0000000, 0x00000000, PMB_UB | PMB_SZ_64M | PMB_WT, },
54 { 0xa4000000, 0x04000000, PMB_UB | PMB_SZ_64M | PMB_WT, },
55 { 0xa8000000, 0x08000000, PMB_UB | PMB_SZ_128M | PMB_WT, },
56 { 0xb0000000, 0x10000000, PMB_UB | PMB_SZ_64M | PMB_WT, },
57 { 0xb4000000, 0x14000000, PMB_UB | PMB_SZ_64M | PMB_WT, },
58 { 0xb8000000, 0x18000000, PMB_UB | PMB_SZ_64M | PMB_WT, },
59};
60
61static inline unsigned long mk_pmb_entry(unsigned int entry)
62{
63 return (entry & PMB_E_MASK) << PMB_E_SHIFT;
64}
65
66static inline unsigned long mk_pmb_addr(unsigned int entry)
67{
68 return mk_pmb_entry(entry) | PMB_ADDR;
69}
70
71static inline unsigned long mk_pmb_data(unsigned int entry)
72{
73 return mk_pmb_entry(entry) | PMB_DATA;
74}
75
Paul Mundt38c425f2007-05-11 11:26:10 +090076static DEFINE_SPINLOCK(pmb_list_lock);
77static struct pmb_entry *pmb_list;
78
79static inline void pmb_list_add(struct pmb_entry *pmbe)
80{
81 struct pmb_entry **p, *tmp;
82
83 p = &pmb_list;
84 while ((tmp = *p) != NULL)
85 p = &tmp->next;
86
87 pmbe->next = tmp;
88 *p = pmbe;
89}
90
91static inline void pmb_list_del(struct pmb_entry *pmbe)
92{
93 struct pmb_entry **p, *tmp;
94
95 for (p = &pmb_list; (tmp = *p); p = &tmp->next)
96 if (tmp == pmbe) {
97 *p = tmp->next;
98 return;
99 }
100}
101
Paul Mundt0c7b1df2006-09-27 15:08:07 +0900102struct pmb_entry *pmb_alloc(unsigned long vpn, unsigned long ppn,
103 unsigned long flags)
104{
105 struct pmb_entry *pmbe;
106
107 pmbe = kmem_cache_alloc(pmb_cache, GFP_KERNEL);
108 if (!pmbe)
109 return ERR_PTR(-ENOMEM);
110
111 pmbe->vpn = vpn;
112 pmbe->ppn = ppn;
113 pmbe->flags = flags;
114
Paul Mundt38c425f2007-05-11 11:26:10 +0900115 spin_lock_irq(&pmb_list_lock);
116 pmb_list_add(pmbe);
117 spin_unlock_irq(&pmb_list_lock);
118
Paul Mundt0c7b1df2006-09-27 15:08:07 +0900119 return pmbe;
120}
121
122void pmb_free(struct pmb_entry *pmbe)
123{
Paul Mundt38c425f2007-05-11 11:26:10 +0900124 spin_lock_irq(&pmb_list_lock);
125 pmb_list_del(pmbe);
126 spin_unlock_irq(&pmb_list_lock);
127
Paul Mundt0c7b1df2006-09-27 15:08:07 +0900128 kmem_cache_free(pmb_cache, pmbe);
129}
130
131/*
132 * Must be in P2 for __set_pmb_entry()
133 */
134int __set_pmb_entry(unsigned long vpn, unsigned long ppn,
135 unsigned long flags, int *entry)
136{
137 unsigned int pos = *entry;
138
139 if (unlikely(pos == PMB_NO_ENTRY))
140 pos = find_first_zero_bit(&pmb_map, NR_PMB_ENTRIES);
141
142repeat:
143 if (unlikely(pos > NR_PMB_ENTRIES))
144 return -ENOSPC;
145
146 if (test_and_set_bit(pos, &pmb_map)) {
147 pos = find_first_zero_bit(&pmb_map, NR_PMB_ENTRIES);
148 goto repeat;
149 }
150
151 ctrl_outl(vpn | PMB_V, mk_pmb_addr(pos));
152
Paul Mundte7bd34a2007-07-31 17:07:28 +0900153#ifdef CONFIG_CACHE_WRITETHROUGH
Paul Mundt0c7b1df2006-09-27 15:08:07 +0900154 /*
155 * When we are in 32-bit address extended mode, CCR.CB becomes
156 * invalid, so care must be taken to manually adjust cacheable
157 * translations.
158 */
159 if (likely(flags & PMB_C))
160 flags |= PMB_WT;
161#endif
162
163 ctrl_outl(ppn | flags | PMB_V, mk_pmb_data(pos));
164
165 *entry = pos;
166
167 return 0;
168}
169
Stuart Menefycbaa1182007-11-30 17:06:36 +0900170int __uses_jump_to_uncached set_pmb_entry(struct pmb_entry *pmbe)
Paul Mundt0c7b1df2006-09-27 15:08:07 +0900171{
Paul Mundtd7cdc9e2006-09-27 15:16:42 +0900172 int ret;
173
Stuart Menefycbaa1182007-11-30 17:06:36 +0900174 jump_to_uncached();
Paul Mundtd7cdc9e2006-09-27 15:16:42 +0900175 ret = __set_pmb_entry(pmbe->vpn, pmbe->ppn, pmbe->flags, &pmbe->entry);
Stuart Menefycbaa1182007-11-30 17:06:36 +0900176 back_to_cached();
Paul Mundtd7cdc9e2006-09-27 15:16:42 +0900177
178 return ret;
Paul Mundt0c7b1df2006-09-27 15:08:07 +0900179}
180
Stuart Menefycbaa1182007-11-30 17:06:36 +0900181void __uses_jump_to_uncached clear_pmb_entry(struct pmb_entry *pmbe)
Paul Mundt0c7b1df2006-09-27 15:08:07 +0900182{
183 unsigned int entry = pmbe->entry;
184 unsigned long addr;
185
186 /*
187 * Don't allow clearing of wired init entries, P1 or P2 access
188 * without a corresponding mapping in the PMB will lead to reset
189 * by the TLB.
190 */
191 if (unlikely(entry < ARRAY_SIZE(pmb_init_map) ||
192 entry >= NR_PMB_ENTRIES))
193 return;
194
Stuart Menefycbaa1182007-11-30 17:06:36 +0900195 jump_to_uncached();
Paul Mundt0c7b1df2006-09-27 15:08:07 +0900196
197 /* Clear V-bit */
198 addr = mk_pmb_addr(entry);
199 ctrl_outl(ctrl_inl(addr) & ~PMB_V, addr);
200
201 addr = mk_pmb_data(entry);
202 ctrl_outl(ctrl_inl(addr) & ~PMB_V, addr);
203
Stuart Menefycbaa1182007-11-30 17:06:36 +0900204 back_to_cached();
Paul Mundt0c7b1df2006-09-27 15:08:07 +0900205
206 clear_bit(entry, &pmb_map);
207}
208
Paul Mundtd7cdc9e2006-09-27 15:16:42 +0900209
210static struct {
211 unsigned long size;
212 int flag;
213} pmb_sizes[] = {
214 { .size = 0x20000000, .flag = PMB_SZ_512M, },
215 { .size = 0x08000000, .flag = PMB_SZ_128M, },
216 { .size = 0x04000000, .flag = PMB_SZ_64M, },
217 { .size = 0x01000000, .flag = PMB_SZ_16M, },
218};
219
220long pmb_remap(unsigned long vaddr, unsigned long phys,
221 unsigned long size, unsigned long flags)
222{
Matt Flemingfc2bdef2009-10-06 21:22:22 +0000223 struct pmb_entry *pmbp, *pmbe;
Paul Mundtd7cdc9e2006-09-27 15:16:42 +0900224 unsigned long wanted;
225 int pmb_flags, i;
Matt Flemingfc2bdef2009-10-06 21:22:22 +0000226 long err;
Paul Mundtd7cdc9e2006-09-27 15:16:42 +0900227
228 /* Convert typical pgprot value to the PMB equivalent */
229 if (flags & _PAGE_CACHABLE) {
230 if (flags & _PAGE_WT)
231 pmb_flags = PMB_WT;
232 else
233 pmb_flags = PMB_C;
234 } else
235 pmb_flags = PMB_WT | PMB_UB;
236
237 pmbp = NULL;
238 wanted = size;
239
240again:
241 for (i = 0; i < ARRAY_SIZE(pmb_sizes); i++) {
Paul Mundtd7cdc9e2006-09-27 15:16:42 +0900242 int ret;
243
244 if (size < pmb_sizes[i].size)
245 continue;
246
247 pmbe = pmb_alloc(vaddr, phys, pmb_flags | pmb_sizes[i].flag);
Matt Flemingfc2bdef2009-10-06 21:22:22 +0000248 if (IS_ERR(pmbe)) {
249 err = PTR_ERR(pmbe);
250 goto out;
251 }
Paul Mundtd7cdc9e2006-09-27 15:16:42 +0900252
253 ret = set_pmb_entry(pmbe);
254 if (ret != 0) {
255 pmb_free(pmbe);
Matt Flemingfc2bdef2009-10-06 21:22:22 +0000256 err = -EBUSY;
257 goto out;
Paul Mundtd7cdc9e2006-09-27 15:16:42 +0900258 }
259
260 phys += pmb_sizes[i].size;
261 vaddr += pmb_sizes[i].size;
262 size -= pmb_sizes[i].size;
263
264 /*
265 * Link adjacent entries that span multiple PMB entries
266 * for easier tear-down.
267 */
268 if (likely(pmbp))
269 pmbp->link = pmbe;
270
271 pmbp = pmbe;
272 }
273
274 if (size >= 0x1000000)
275 goto again;
276
277 return wanted - size;
Matt Flemingfc2bdef2009-10-06 21:22:22 +0000278
279out:
280 if (pmbp)
281 __pmb_unmap(pmbp);
282
283 return err;
Paul Mundtd7cdc9e2006-09-27 15:16:42 +0900284}
285
286void pmb_unmap(unsigned long addr)
287{
288 struct pmb_entry **p, *pmbe;
289
290 for (p = &pmb_list; (pmbe = *p); p = &pmbe->next)
291 if (pmbe->vpn == addr)
292 break;
293
294 if (unlikely(!pmbe))
295 return;
296
Matt Flemingfc2bdef2009-10-06 21:22:22 +0000297 __pmb_unmap(pmbe);
298}
299
300static void __pmb_unmap(struct pmb_entry *pmbe)
301{
Paul Mundtd7cdc9e2006-09-27 15:16:42 +0900302 WARN_ON(!test_bit(pmbe->entry, &pmb_map));
303
304 do {
305 struct pmb_entry *pmblink = pmbe;
306
Matt Flemingfc2bdef2009-10-06 21:22:22 +0000307 if (pmbe->entry != PMB_NO_ENTRY)
308 clear_pmb_entry(pmbe);
309
Paul Mundtd7cdc9e2006-09-27 15:16:42 +0900310 pmbe = pmblink->link;
311
312 pmb_free(pmblink);
313 } while (pmbe);
314}
315
Alexey Dobriyan51cc5062008-07-25 19:45:34 -0700316static void pmb_cache_ctor(void *pmb)
Paul Mundt0c7b1df2006-09-27 15:08:07 +0900317{
Paul Mundtd7cdc9e2006-09-27 15:16:42 +0900318 struct pmb_entry *pmbe = pmb;
319
Paul Mundt0c7b1df2006-09-27 15:08:07 +0900320 memset(pmb, 0, sizeof(struct pmb_entry));
321
Paul Mundtd7cdc9e2006-09-27 15:16:42 +0900322 pmbe->entry = PMB_NO_ENTRY;
Paul Mundt0c7b1df2006-09-27 15:08:07 +0900323}
324
Stuart Menefycbaa1182007-11-30 17:06:36 +0900325static int __uses_jump_to_uncached pmb_init(void)
Paul Mundt0c7b1df2006-09-27 15:08:07 +0900326{
327 unsigned int nr_entries = ARRAY_SIZE(pmb_init_map);
Nobuhiro Iwamatsu53ff0942007-11-30 12:33:17 +0900328 unsigned int entry, i;
Paul Mundt0c7b1df2006-09-27 15:08:07 +0900329
330 BUG_ON(unlikely(nr_entries >= NR_PMB_ENTRIES));
331
Akinobu Mita0e6b9c92007-05-08 00:23:13 -0700332 pmb_cache = kmem_cache_create("pmb", sizeof(struct pmb_entry), 0,
Paul Mundt20c2df82007-07-20 10:11:58 +0900333 SLAB_PANIC, pmb_cache_ctor);
Paul Mundt0c7b1df2006-09-27 15:08:07 +0900334
Stuart Menefycbaa1182007-11-30 17:06:36 +0900335 jump_to_uncached();
Paul Mundt0c7b1df2006-09-27 15:08:07 +0900336
337 /*
338 * Ordering is important, P2 must be mapped in the PMB before we
339 * can set PMB.SE, and P1 must be mapped before we jump back to
340 * P1 space.
341 */
342 for (entry = 0; entry < nr_entries; entry++) {
343 struct pmb_entry *pmbe = pmb_init_map + entry;
344
345 __set_pmb_entry(pmbe->vpn, pmbe->ppn, pmbe->flags, &entry);
346 }
347
348 ctrl_outl(0, PMB_IRMCR);
349
350 /* PMB.SE and UB[7] */
351 ctrl_outl((1 << 31) | (1 << 7), PMB_PASCR);
352
Stuart Menefyeddeeb32007-11-26 21:32:40 +0900353 /* Flush out the TLB */
354 i = ctrl_inl(MMUCR);
355 i |= MMUCR_TI;
356 ctrl_outl(i, MMUCR);
357
Stuart Menefycbaa1182007-11-30 17:06:36 +0900358 back_to_cached();
Paul Mundt0c7b1df2006-09-27 15:08:07 +0900359
360 return 0;
361}
Paul Mundt0c7b1df2006-09-27 15:08:07 +0900362arch_initcall(pmb_init);
363
Paul Mundt0c7b1df2006-09-27 15:08:07 +0900364static int pmb_seq_show(struct seq_file *file, void *iter)
365{
366 int i;
367
368 seq_printf(file, "V: Valid, C: Cacheable, WT: Write-Through\n"
369 "CB: Copy-Back, B: Buffered, UB: Unbuffered\n");
370 seq_printf(file, "ety vpn ppn size flags\n");
371
372 for (i = 0; i < NR_PMB_ENTRIES; i++) {
373 unsigned long addr, data;
374 unsigned int size;
375 char *sz_str = NULL;
376
377 addr = ctrl_inl(mk_pmb_addr(i));
378 data = ctrl_inl(mk_pmb_data(i));
379
380 size = data & PMB_SZ_MASK;
381 sz_str = (size == PMB_SZ_16M) ? " 16MB":
382 (size == PMB_SZ_64M) ? " 64MB":
383 (size == PMB_SZ_128M) ? "128MB":
384 "512MB";
385
386 /* 02: V 0x88 0x08 128MB C CB B */
387 seq_printf(file, "%02d: %c 0x%02lx 0x%02lx %s %c %s %s\n",
388 i, ((addr & PMB_V) && (data & PMB_V)) ? 'V' : ' ',
389 (addr >> 24) & 0xff, (data >> 24) & 0xff,
390 sz_str, (data & PMB_C) ? 'C' : ' ',
391 (data & PMB_WT) ? "WT" : "CB",
392 (data & PMB_UB) ? "UB" : " B");
393 }
394
395 return 0;
396}
397
398static int pmb_debugfs_open(struct inode *inode, struct file *file)
399{
400 return single_open(file, pmb_seq_show, NULL);
401}
402
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800403static const struct file_operations pmb_debugfs_fops = {
Paul Mundt0c7b1df2006-09-27 15:08:07 +0900404 .owner = THIS_MODULE,
405 .open = pmb_debugfs_open,
406 .read = seq_read,
407 .llseek = seq_lseek,
Li Zefan45dabf12008-06-24 13:30:23 +0800408 .release = single_release,
Paul Mundt0c7b1df2006-09-27 15:08:07 +0900409};
410
411static int __init pmb_debugfs_init(void)
412{
413 struct dentry *dentry;
414
415 dentry = debugfs_create_file("pmb", S_IFREG | S_IRUGO,
Paul Mundtb9e393c2008-03-07 17:19:58 +0900416 sh_debugfs_root, NULL, &pmb_debugfs_fops);
Zhaolei25627c72008-10-17 19:25:09 +0800417 if (!dentry)
418 return -ENOMEM;
Paul Mundt0c7b1df2006-09-27 15:08:07 +0900419 if (IS_ERR(dentry))
420 return PTR_ERR(dentry);
421
422 return 0;
423}
Paul Mundt0c7b1df2006-09-27 15:08:07 +0900424postcore_initcall(pmb_debugfs_init);
Francesco VIRLINZIa83c0b72009-03-11 10:39:02 +0000425
426#ifdef CONFIG_PM
427static int pmb_sysdev_suspend(struct sys_device *dev, pm_message_t state)
428{
429 static pm_message_t prev_state;
430
431 /* Restore the PMB after a resume from hibernation */
432 if (state.event == PM_EVENT_ON &&
433 prev_state.event == PM_EVENT_FREEZE) {
434 struct pmb_entry *pmbe;
435 spin_lock_irq(&pmb_list_lock);
436 for (pmbe = pmb_list; pmbe; pmbe = pmbe->next)
437 set_pmb_entry(pmbe);
438 spin_unlock_irq(&pmb_list_lock);
439 }
440 prev_state = state;
441 return 0;
442}
443
444static int pmb_sysdev_resume(struct sys_device *dev)
445{
446 return pmb_sysdev_suspend(dev, PMSG_ON);
447}
448
449static struct sysdev_driver pmb_sysdev_driver = {
450 .suspend = pmb_sysdev_suspend,
451 .resume = pmb_sysdev_resume,
452};
453
454static int __init pmb_sysdev_init(void)
455{
456 return sysdev_driver_register(&cpu_sysdev_class, &pmb_sysdev_driver);
457}
458
459subsys_initcall(pmb_sysdev_init);
460#endif