sh: Allocate PMB entry slot earlier
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / arch / sh / mm / pmb.c
blobb8a33949296a7645b41b0c18efee19cb716670d2
1 /*
2 * arch/sh/mm/pmb.c
4 * Privileged Space Mapping Buffer (PMB) Support.
6 * Copyright (C) 2005, 2006, 2007 Paul Mundt
8 * P1/P2 Section mapping definitions from map32.h, which was:
10 * Copyright 2003 (c) Lineo Solutions,Inc.
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.
16 #include <linux/init.h>
17 #include <linux/kernel.h>
18 #include <linux/sysdev.h>
19 #include <linux/cpu.h>
20 #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>
29 #include <asm/pgtable.h>
30 #include <asm/mmu.h>
31 #include <asm/io.h>
32 #include <asm/mmu_context.h>
34 #define NR_PMB_ENTRIES 16
36 static void __pmb_unmap(struct pmb_entry *);
38 static struct kmem_cache *pmb_cache;
39 static unsigned long pmb_map;
41 static struct pmb_entry pmb_init_map[] = {
42 /* vpn ppn flags (ub/sz/c/wt) */
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, },
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, },
61 static inline unsigned long mk_pmb_entry(unsigned int entry)
63 return (entry & PMB_E_MASK) << PMB_E_SHIFT;
66 static inline unsigned long mk_pmb_addr(unsigned int entry)
68 return mk_pmb_entry(entry) | PMB_ADDR;
71 static inline unsigned long mk_pmb_data(unsigned int entry)
73 return mk_pmb_entry(entry) | PMB_DATA;
76 static DEFINE_SPINLOCK(pmb_list_lock);
77 static struct pmb_entry *pmb_list;
79 static inline void pmb_list_add(struct pmb_entry *pmbe)
81 struct pmb_entry **p, *tmp;
83 p = &pmb_list;
84 while ((tmp = *p) != NULL)
85 p = &tmp->next;
87 pmbe->next = tmp;
88 *p = pmbe;
91 static inline void pmb_list_del(struct pmb_entry *pmbe)
93 struct pmb_entry **p, *tmp;
95 for (p = &pmb_list; (tmp = *p); p = &tmp->next)
96 if (tmp == pmbe) {
97 *p = tmp->next;
98 return;
102 static int pmb_alloc_entry(void)
104 unsigned int pos;
106 repeat:
107 pos = find_first_zero_bit(&pmb_map, NR_PMB_ENTRIES);
109 if (unlikely(pos > NR_PMB_ENTRIES))
110 return -ENOSPC;
112 if (test_and_set_bit(pos, &pmb_map))
113 goto repeat;
115 return pos;
118 struct pmb_entry *pmb_alloc(unsigned long vpn, unsigned long ppn,
119 unsigned long flags)
121 struct pmb_entry *pmbe;
122 int pos;
124 pos = pmb_alloc_entry();
125 if (pos < 0)
126 return ERR_PTR(pos);
128 pmbe = kmem_cache_alloc(pmb_cache, GFP_KERNEL);
129 if (!pmbe)
130 return ERR_PTR(-ENOMEM);
132 pmbe->vpn = vpn;
133 pmbe->ppn = ppn;
134 pmbe->flags = flags;
135 pmbe->entry = pos;
137 spin_lock_irq(&pmb_list_lock);
138 pmb_list_add(pmbe);
139 spin_unlock_irq(&pmb_list_lock);
141 return pmbe;
144 void pmb_free(struct pmb_entry *pmbe)
146 spin_lock_irq(&pmb_list_lock);
147 pmb_list_del(pmbe);
148 spin_unlock_irq(&pmb_list_lock);
150 kmem_cache_free(pmb_cache, pmbe);
154 * Must be in P2 for __set_pmb_entry()
156 void __set_pmb_entry(unsigned long vpn, unsigned long ppn,
157 unsigned long flags, int pos)
159 ctrl_outl(vpn | PMB_V, mk_pmb_addr(pos));
161 #ifdef CONFIG_CACHE_WRITETHROUGH
163 * When we are in 32-bit address extended mode, CCR.CB becomes
164 * invalid, so care must be taken to manually adjust cacheable
165 * translations.
167 if (likely(flags & PMB_C))
168 flags |= PMB_WT;
169 #endif
171 ctrl_outl(ppn | flags | PMB_V, mk_pmb_data(pos));
174 void __uses_jump_to_uncached set_pmb_entry(struct pmb_entry *pmbe)
176 jump_to_uncached();
177 __set_pmb_entry(pmbe->vpn, pmbe->ppn, pmbe->flags, pmbe->entry);
178 back_to_cached();
181 void __uses_jump_to_uncached clear_pmb_entry(struct pmb_entry *pmbe)
183 unsigned int entry = pmbe->entry;
184 unsigned long addr;
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.
191 if (unlikely(entry < ARRAY_SIZE(pmb_init_map) ||
192 entry >= NR_PMB_ENTRIES))
193 return;
195 jump_to_uncached();
197 /* Clear V-bit */
198 addr = mk_pmb_addr(entry);
199 ctrl_outl(ctrl_inl(addr) & ~PMB_V, addr);
201 addr = mk_pmb_data(entry);
202 ctrl_outl(ctrl_inl(addr) & ~PMB_V, addr);
204 back_to_cached();
206 clear_bit(entry, &pmb_map);
210 static 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, },
220 long pmb_remap(unsigned long vaddr, unsigned long phys,
221 unsigned long size, unsigned long flags)
223 struct pmb_entry *pmbp, *pmbe;
224 unsigned long wanted;
225 int pmb_flags, i;
226 long err;
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;
237 pmbp = NULL;
238 wanted = size;
240 again:
241 for (i = 0; i < ARRAY_SIZE(pmb_sizes); i++) {
242 if (size < pmb_sizes[i].size)
243 continue;
245 pmbe = pmb_alloc(vaddr, phys, pmb_flags | pmb_sizes[i].flag);
246 if (IS_ERR(pmbe)) {
247 err = PTR_ERR(pmbe);
248 goto out;
251 set_pmb_entry(pmbe);
253 phys += pmb_sizes[i].size;
254 vaddr += pmb_sizes[i].size;
255 size -= pmb_sizes[i].size;
258 * Link adjacent entries that span multiple PMB entries
259 * for easier tear-down.
261 if (likely(pmbp))
262 pmbp->link = pmbe;
264 pmbp = pmbe;
267 * Instead of trying smaller sizes on every iteration
268 * (even if we succeed in allocating space), try using
269 * pmb_sizes[i].size again.
271 i--;
274 if (size >= 0x1000000)
275 goto again;
277 return wanted - size;
279 out:
280 if (pmbp)
281 __pmb_unmap(pmbp);
283 return err;
286 void pmb_unmap(unsigned long addr)
288 struct pmb_entry **p, *pmbe;
290 for (p = &pmb_list; (pmbe = *p); p = &pmbe->next)
291 if (pmbe->vpn == addr)
292 break;
294 if (unlikely(!pmbe))
295 return;
297 __pmb_unmap(pmbe);
300 static void __pmb_unmap(struct pmb_entry *pmbe)
302 WARN_ON(!test_bit(pmbe->entry, &pmb_map));
304 do {
305 struct pmb_entry *pmblink = pmbe;
308 * We may be called before this pmb_entry has been
309 * entered into the PMB table via set_pmb_entry(), but
310 * that's OK because we've allocated a unique slot for
311 * this entry in pmb_alloc() (even if we haven't filled
312 * it yet).
314 * Therefore, calling clear_pmb_entry() is safe as no
315 * other mapping can be using that slot.
317 clear_pmb_entry(pmbe);
319 pmbe = pmblink->link;
321 pmb_free(pmblink);
322 } while (pmbe);
325 static void pmb_cache_ctor(void *pmb)
327 memset(pmb, 0, sizeof(struct pmb_entry));
330 static int __uses_jump_to_uncached pmb_init(void)
332 unsigned int nr_entries = ARRAY_SIZE(pmb_init_map);
333 unsigned int entry, i;
335 BUG_ON(unlikely(nr_entries >= NR_PMB_ENTRIES));
337 pmb_cache = kmem_cache_create("pmb", sizeof(struct pmb_entry), 0,
338 SLAB_PANIC, pmb_cache_ctor);
340 jump_to_uncached();
343 * Ordering is important, P2 must be mapped in the PMB before we
344 * can set PMB.SE, and P1 must be mapped before we jump back to
345 * P1 space.
347 for (entry = 0; entry < nr_entries; entry++) {
348 struct pmb_entry *pmbe = pmb_init_map + entry;
350 __set_pmb_entry(pmbe->vpn, pmbe->ppn, pmbe->flags, entry);
353 ctrl_outl(0, PMB_IRMCR);
355 /* PMB.SE and UB[7] */
356 ctrl_outl((1 << 31) | (1 << 7), PMB_PASCR);
358 /* Flush out the TLB */
359 i = ctrl_inl(MMUCR);
360 i |= MMUCR_TI;
361 ctrl_outl(i, MMUCR);
363 back_to_cached();
365 return 0;
367 arch_initcall(pmb_init);
369 static int pmb_seq_show(struct seq_file *file, void *iter)
371 int i;
373 seq_printf(file, "V: Valid, C: Cacheable, WT: Write-Through\n"
374 "CB: Copy-Back, B: Buffered, UB: Unbuffered\n");
375 seq_printf(file, "ety vpn ppn size flags\n");
377 for (i = 0; i < NR_PMB_ENTRIES; i++) {
378 unsigned long addr, data;
379 unsigned int size;
380 char *sz_str = NULL;
382 addr = ctrl_inl(mk_pmb_addr(i));
383 data = ctrl_inl(mk_pmb_data(i));
385 size = data & PMB_SZ_MASK;
386 sz_str = (size == PMB_SZ_16M) ? " 16MB":
387 (size == PMB_SZ_64M) ? " 64MB":
388 (size == PMB_SZ_128M) ? "128MB":
389 "512MB";
391 /* 02: V 0x88 0x08 128MB C CB B */
392 seq_printf(file, "%02d: %c 0x%02lx 0x%02lx %s %c %s %s\n",
393 i, ((addr & PMB_V) && (data & PMB_V)) ? 'V' : ' ',
394 (addr >> 24) & 0xff, (data >> 24) & 0xff,
395 sz_str, (data & PMB_C) ? 'C' : ' ',
396 (data & PMB_WT) ? "WT" : "CB",
397 (data & PMB_UB) ? "UB" : " B");
400 return 0;
403 static int pmb_debugfs_open(struct inode *inode, struct file *file)
405 return single_open(file, pmb_seq_show, NULL);
408 static const struct file_operations pmb_debugfs_fops = {
409 .owner = THIS_MODULE,
410 .open = pmb_debugfs_open,
411 .read = seq_read,
412 .llseek = seq_lseek,
413 .release = single_release,
416 static int __init pmb_debugfs_init(void)
418 struct dentry *dentry;
420 dentry = debugfs_create_file("pmb", S_IFREG | S_IRUGO,
421 sh_debugfs_root, NULL, &pmb_debugfs_fops);
422 if (!dentry)
423 return -ENOMEM;
424 if (IS_ERR(dentry))
425 return PTR_ERR(dentry);
427 return 0;
429 postcore_initcall(pmb_debugfs_init);
431 #ifdef CONFIG_PM
432 static int pmb_sysdev_suspend(struct sys_device *dev, pm_message_t state)
434 static pm_message_t prev_state;
436 /* Restore the PMB after a resume from hibernation */
437 if (state.event == PM_EVENT_ON &&
438 prev_state.event == PM_EVENT_FREEZE) {
439 struct pmb_entry *pmbe;
440 spin_lock_irq(&pmb_list_lock);
441 for (pmbe = pmb_list; pmbe; pmbe = pmbe->next)
442 set_pmb_entry(pmbe);
443 spin_unlock_irq(&pmb_list_lock);
445 prev_state = state;
446 return 0;
449 static int pmb_sysdev_resume(struct sys_device *dev)
451 return pmb_sysdev_suspend(dev, PMSG_ON);
454 static struct sysdev_driver pmb_sysdev_driver = {
455 .suspend = pmb_sysdev_suspend,
456 .resume = pmb_sysdev_resume,
459 static int __init pmb_sysdev_init(void)
461 return sysdev_driver_register(&cpu_sysdev_class, &pmb_sysdev_driver);
464 subsys_initcall(pmb_sysdev_init);
465 #endif