sh: PMB locking overhaul.
[pandora-kernel.git] / arch / sh / mm / pmb.c
1 /*
2  * arch/sh/mm/pmb.c
3  *
4  * Privileged Space Mapping Buffer (PMB) Support.
5  *
6  * Copyright (C) 2005 - 2010  Paul Mundt
7  * Copyright (C) 2010  Matt Fleming
8  *
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>
15 #include <linux/sysdev.h>
16 #include <linux/cpu.h>
17 #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>
24 #include <linux/io.h>
25 #include <linux/spinlock.h>
26 #include <linux/rwlock.h>
27 #include <asm/sizes.h>
28 #include <asm/system.h>
29 #include <asm/uaccess.h>
30 #include <asm/pgtable.h>
31 #include <asm/page.h>
32 #include <asm/mmu.h>
33 #include <asm/mmu_context.h>
34
35 struct pmb_entry;
36
37 struct 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
55 static void pmb_unmap_entry(struct pmb_entry *);
56
57 static DEFINE_RWLOCK(pmb_rwlock);
58 static struct pmb_entry pmb_entry_list[NR_PMB_ENTRIES];
59 static DECLARE_BITMAP(pmb_map, NR_PMB_ENTRIES);
60
61 static __always_inline unsigned long mk_pmb_entry(unsigned int entry)
62 {
63         return (entry & PMB_E_MASK) << PMB_E_SHIFT;
64 }
65
66 static __always_inline unsigned long mk_pmb_addr(unsigned int entry)
67 {
68         return mk_pmb_entry(entry) | PMB_ADDR;
69 }
70
71 static __always_inline unsigned long mk_pmb_data(unsigned int entry)
72 {
73         return mk_pmb_entry(entry) | PMB_DATA;
74 }
75
76 static int pmb_alloc_entry(void)
77 {
78         int pos;
79
80         pos = find_first_zero_bit(pmb_map, NR_PMB_ENTRIES);
81         if (pos >= 0 && pos < NR_PMB_ENTRIES)
82                 __set_bit(pos, pmb_map);
83         else
84                 pos = -ENOSPC;
85
86         return pos;
87 }
88
89 static struct pmb_entry *pmb_alloc(unsigned long vpn, unsigned long ppn,
90                                    unsigned long flags, int entry)
91 {
92         struct pmb_entry *pmbe;
93         unsigned long irqflags;
94         void *ret = NULL;
95         int pos;
96
97         write_lock_irqsave(&pmb_rwlock, irqflags);
98
99         if (entry == PMB_NO_ENTRY) {
100                 pos = pmb_alloc_entry();
101                 if (unlikely(pos < 0)) {
102                         ret = ERR_PTR(pos);
103                         goto out;
104                 }
105         } else {
106                 if (__test_and_set_bit(entry, pmb_map)) {
107                         ret = ERR_PTR(-ENOSPC);
108                         goto out;
109                 }
110
111                 pos = entry;
112         }
113
114         write_unlock_irqrestore(&pmb_rwlock, irqflags);
115
116         pmbe = &pmb_entry_list[pos];
117
118         spin_lock_init(&pmbe->lock);
119
120         pmbe->vpn       = vpn;
121         pmbe->ppn       = ppn;
122         pmbe->flags     = flags;
123         pmbe->entry     = pos;
124         pmbe->size      = 0;
125
126         return pmbe;
127
128 out:
129         write_unlock_irqrestore(&pmb_rwlock, irqflags);
130         return ret;
131 }
132
133 static void pmb_free(struct pmb_entry *pmbe)
134 {
135         __clear_bit(pmbe->entry, pmb_map);
136         pmbe->entry = PMB_NO_ENTRY;
137 }
138
139 /*
140  * Ensure that the PMB entries match our cache configuration.
141  *
142  * When we are in 32-bit address extended mode, CCR.CB becomes
143  * invalid, so care must be taken to manually adjust cacheable
144  * translations.
145  */
146 static __always_inline unsigned long pmb_cache_flags(void)
147 {
148         unsigned long flags = 0;
149
150 #if defined(CONFIG_CACHE_WRITETHROUGH)
151         flags |= PMB_C | PMB_WT | PMB_UB;
152 #elif defined(CONFIG_CACHE_WRITEBACK)
153         flags |= PMB_C;
154 #endif
155
156         return flags;
157 }
158
159 /*
160  * Must be run uncached.
161  */
162 static void __set_pmb_entry(struct pmb_entry *pmbe)
163 {
164         jump_to_uncached();
165
166         pmbe->flags &= ~PMB_CACHE_MASK;
167         pmbe->flags |= pmb_cache_flags();
168
169         __raw_writel(pmbe->vpn | PMB_V, mk_pmb_addr(pmbe->entry));
170         __raw_writel(pmbe->ppn | pmbe->flags | PMB_V, mk_pmb_data(pmbe->entry));
171
172         back_to_cached();
173 }
174
175 static void __clear_pmb_entry(struct pmb_entry *pmbe)
176 {
177         unsigned int entry = pmbe->entry;
178         unsigned long addr;
179
180         jump_to_uncached();
181
182         /* Clear V-bit */
183         addr = mk_pmb_addr(entry);
184         __raw_writel(__raw_readl(addr) & ~PMB_V, addr);
185
186         addr = mk_pmb_data(entry);
187         __raw_writel(__raw_readl(addr) & ~PMB_V, addr);
188
189         back_to_cached();
190 }
191
192 static void set_pmb_entry(struct pmb_entry *pmbe)
193 {
194         unsigned long flags;
195
196         spin_lock_irqsave(&pmbe->lock, flags);
197         __set_pmb_entry(pmbe);
198         spin_unlock_irqrestore(&pmbe->lock, flags);
199 }
200
201 static struct {
202         unsigned long size;
203         int flag;
204 } pmb_sizes[] = {
205         { .size = SZ_512M, .flag = PMB_SZ_512M, },
206         { .size = SZ_128M, .flag = PMB_SZ_128M, },
207         { .size = SZ_64M,  .flag = PMB_SZ_64M,  },
208         { .size = SZ_16M,  .flag = PMB_SZ_16M,  },
209 };
210
211 long pmb_remap(unsigned long vaddr, unsigned long phys,
212                unsigned long size, pgprot_t prot)
213 {
214         struct pmb_entry *pmbp, *pmbe;
215         unsigned long wanted;
216         int pmb_flags, i;
217         long err;
218         u64 flags;
219
220         flags = pgprot_val(prot);
221
222         pmb_flags = PMB_WT | PMB_UB;
223
224         /* Convert typical pgprot value to the PMB equivalent */
225         if (flags & _PAGE_CACHABLE) {
226                 pmb_flags |= PMB_C;
227
228                 if ((flags & _PAGE_WT) == 0)
229                         pmb_flags &= ~(PMB_WT | PMB_UB);
230         }
231
232         pmbp = NULL;
233         wanted = size;
234
235 again:
236         for (i = 0; i < ARRAY_SIZE(pmb_sizes); i++) {
237                 unsigned long flags;
238
239                 if (size < pmb_sizes[i].size)
240                         continue;
241
242                 pmbe = pmb_alloc(vaddr, phys, pmb_flags | pmb_sizes[i].flag,
243                                  PMB_NO_ENTRY);
244                 if (IS_ERR(pmbe)) {
245                         err = PTR_ERR(pmbe);
246                         goto out;
247                 }
248
249                 spin_lock_irqsave(&pmbe->lock, flags);
250
251                 __set_pmb_entry(pmbe);
252
253                 phys    += pmb_sizes[i].size;
254                 vaddr   += pmb_sizes[i].size;
255                 size    -= pmb_sizes[i].size;
256
257                 pmbe->size = pmb_sizes[i].size;
258
259                 /*
260                  * Link adjacent entries that span multiple PMB entries
261                  * for easier tear-down.
262                  */
263                 if (likely(pmbp)) {
264                         spin_lock(&pmbp->lock);
265                         pmbp->link = pmbe;
266                         spin_unlock(&pmbp->lock);
267                 }
268
269                 pmbp = pmbe;
270
271                 /*
272                  * Instead of trying smaller sizes on every iteration
273                  * (even if we succeed in allocating space), try using
274                  * pmb_sizes[i].size again.
275                  */
276                 i--;
277
278                 spin_unlock_irqrestore(&pmbe->lock, flags);
279         }
280
281         if (size >= SZ_16M)
282                 goto again;
283
284         return wanted - size;
285
286 out:
287         pmb_unmap_entry(pmbp);
288
289         return err;
290 }
291
292 void pmb_unmap(unsigned long addr)
293 {
294         struct pmb_entry *pmbe = NULL;
295         int i;
296
297         read_lock(&pmb_rwlock);
298
299         for (i = 0; i < ARRAY_SIZE(pmb_entry_list); i++) {
300                 if (test_bit(i, pmb_map)) {
301                         pmbe = &pmb_entry_list[i];
302                         if (pmbe->vpn == addr)
303                                 break;
304                 }
305         }
306
307         read_unlock(&pmb_rwlock);
308
309         pmb_unmap_entry(pmbe);
310 }
311
312 static void pmb_unmap_entry(struct pmb_entry *pmbe)
313 {
314         unsigned long flags;
315
316         if (unlikely(!pmbe))
317                 return;
318
319         write_lock_irqsave(&pmb_rwlock, flags);
320
321         do {
322                 struct pmb_entry *pmblink = pmbe;
323
324                 /*
325                  * We may be called before this pmb_entry has been
326                  * entered into the PMB table via set_pmb_entry(), but
327                  * that's OK because we've allocated a unique slot for
328                  * this entry in pmb_alloc() (even if we haven't filled
329                  * it yet).
330                  *
331                  * Therefore, calling __clear_pmb_entry() is safe as no
332                  * other mapping can be using that slot.
333                  */
334                 __clear_pmb_entry(pmbe);
335
336                 pmbe = pmblink->link;
337
338                 pmb_free(pmblink);
339         } while (pmbe);
340
341         write_unlock_irqrestore(&pmb_rwlock, flags);
342 }
343
344 static __always_inline unsigned int pmb_ppn_in_range(unsigned long ppn)
345 {
346         return ppn >= __pa(memory_start) && ppn < __pa(memory_end);
347 }
348
349 static int pmb_synchronize_mappings(void)
350 {
351         unsigned int applied = 0;
352         struct pmb_entry *pmbp = NULL;
353         int i, j;
354
355         pr_info("PMB: boot mappings:\n");
356
357         /*
358          * Run through the initial boot mappings, log the established
359          * ones, and blow away anything that falls outside of the valid
360          * PPN range. Specifically, we only care about existing mappings
361          * that impact the cached/uncached sections.
362          *
363          * Note that touching these can be a bit of a minefield; the boot
364          * loader can establish multi-page mappings with the same caching
365          * attributes, so we need to ensure that we aren't modifying a
366          * mapping that we're presently executing from, or may execute
367          * from in the case of straddling page boundaries.
368          *
369          * In the future we will have to tidy up after the boot loader by
370          * jumping between the cached and uncached mappings and tearing
371          * down alternating mappings while executing from the other.
372          */
373         for (i = 0; i < NR_PMB_ENTRIES; i++) {
374                 unsigned long addr, data;
375                 unsigned long addr_val, data_val;
376                 unsigned long ppn, vpn, flags;
377                 unsigned long irqflags;
378                 unsigned int size;
379                 struct pmb_entry *pmbe;
380
381                 addr = mk_pmb_addr(i);
382                 data = mk_pmb_data(i);
383
384                 addr_val = __raw_readl(addr);
385                 data_val = __raw_readl(data);
386
387                 /*
388                  * Skip over any bogus entries
389                  */
390                 if (!(data_val & PMB_V) || !(addr_val & PMB_V))
391                         continue;
392
393                 ppn = data_val & PMB_PFN_MASK;
394                 vpn = addr_val & PMB_PFN_MASK;
395
396                 /*
397                  * Only preserve in-range mappings.
398                  */
399                 if (!pmb_ppn_in_range(ppn)) {
400                         /*
401                          * Invalidate anything out of bounds.
402                          */
403                         __raw_writel(addr_val & ~PMB_V, addr);
404                         __raw_writel(data_val & ~PMB_V, data);
405                         continue;
406                 }
407
408                 /*
409                  * Update the caching attributes if necessary
410                  */
411                 if (data_val & PMB_C) {
412                         data_val &= ~PMB_CACHE_MASK;
413                         data_val |= pmb_cache_flags();
414                         __raw_writel(data_val, data);
415                 }
416
417                 size = data_val & PMB_SZ_MASK;
418                 flags = size | (data_val & PMB_CACHE_MASK);
419
420                 pmbe = pmb_alloc(vpn, ppn, flags, i);
421                 if (IS_ERR(pmbe)) {
422                         WARN_ON_ONCE(1);
423                         continue;
424                 }
425
426                 spin_lock_irqsave(&pmbe->lock, irqflags);
427
428                 for (j = 0; j < ARRAY_SIZE(pmb_sizes); j++)
429                         if (pmb_sizes[j].flag == size)
430                                 pmbe->size = pmb_sizes[j].size;
431
432                 if (pmbp) {
433                         spin_lock(&pmbp->lock);
434
435                         /*
436                          * Compare the previous entry against the current one to
437                          * see if the entries span a contiguous mapping. If so,
438                          * setup the entry links accordingly.
439                          */
440                         if ((pmbe->vpn == (pmbp->vpn + pmbp->size)) &&
441                             (pmbe->ppn == (pmbp->ppn + pmbp->size)))
442                                 pmbp->link = pmbe;
443
444                         spin_unlock(&pmbp->lock);
445                 }
446
447                 pmbp = pmbe;
448
449                 spin_unlock_irqrestore(&pmbe->lock, irqflags);
450
451                 pr_info("\t0x%08lx -> 0x%08lx [ %ldMB %scached ]\n",
452                         vpn >> PAGE_SHIFT, ppn >> PAGE_SHIFT, pmbe->size >> 20,
453                         (data_val & PMB_C) ? "" : "un");
454
455                 applied++;
456         }
457
458         return (applied == 0);
459 }
460
461 int pmb_init(void)
462 {
463         int ret;
464
465         jump_to_uncached();
466
467         /*
468          * Sync our software copy of the PMB mappings with those in
469          * hardware. The mappings in the hardware PMB were either set up
470          * by the bootloader or very early on by the kernel.
471          */
472         ret = pmb_synchronize_mappings();
473         if (unlikely(ret == 0)) {
474                 back_to_cached();
475                 return 0;
476         }
477
478         __raw_writel(0, PMB_IRMCR);
479
480         /* Flush out the TLB */
481         __raw_writel(__raw_readl(MMUCR) | MMUCR_TI, MMUCR);
482
483         back_to_cached();
484
485         return 0;
486 }
487
488 bool __in_29bit_mode(void)
489 {
490         return (__raw_readl(PMB_PASCR) & PASCR_SE) == 0;
491 }
492
493 static int pmb_seq_show(struct seq_file *file, void *iter)
494 {
495         int i;
496
497         seq_printf(file, "V: Valid, C: Cacheable, WT: Write-Through\n"
498                          "CB: Copy-Back, B: Buffered, UB: Unbuffered\n");
499         seq_printf(file, "ety   vpn  ppn  size   flags\n");
500
501         for (i = 0; i < NR_PMB_ENTRIES; i++) {
502                 unsigned long addr, data;
503                 unsigned int size;
504                 char *sz_str = NULL;
505
506                 addr = __raw_readl(mk_pmb_addr(i));
507                 data = __raw_readl(mk_pmb_data(i));
508
509                 size = data & PMB_SZ_MASK;
510                 sz_str = (size == PMB_SZ_16M)  ? " 16MB":
511                          (size == PMB_SZ_64M)  ? " 64MB":
512                          (size == PMB_SZ_128M) ? "128MB":
513                                                  "512MB";
514
515                 /* 02: V 0x88 0x08 128MB C CB  B */
516                 seq_printf(file, "%02d: %c 0x%02lx 0x%02lx %s %c %s %s\n",
517                            i, ((addr & PMB_V) && (data & PMB_V)) ? 'V' : ' ',
518                            (addr >> 24) & 0xff, (data >> 24) & 0xff,
519                            sz_str, (data & PMB_C) ? 'C' : ' ',
520                            (data & PMB_WT) ? "WT" : "CB",
521                            (data & PMB_UB) ? "UB" : " B");
522         }
523
524         return 0;
525 }
526
527 static int pmb_debugfs_open(struct inode *inode, struct file *file)
528 {
529         return single_open(file, pmb_seq_show, NULL);
530 }
531
532 static const struct file_operations pmb_debugfs_fops = {
533         .owner          = THIS_MODULE,
534         .open           = pmb_debugfs_open,
535         .read           = seq_read,
536         .llseek         = seq_lseek,
537         .release        = single_release,
538 };
539
540 static int __init pmb_debugfs_init(void)
541 {
542         struct dentry *dentry;
543
544         dentry = debugfs_create_file("pmb", S_IFREG | S_IRUGO,
545                                      sh_debugfs_root, NULL, &pmb_debugfs_fops);
546         if (!dentry)
547                 return -ENOMEM;
548         if (IS_ERR(dentry))
549                 return PTR_ERR(dentry);
550
551         return 0;
552 }
553 postcore_initcall(pmb_debugfs_init);
554
555 #ifdef CONFIG_PM
556 static int pmb_sysdev_suspend(struct sys_device *dev, pm_message_t state)
557 {
558         static pm_message_t prev_state;
559         int i;
560
561         /* Restore the PMB after a resume from hibernation */
562         if (state.event == PM_EVENT_ON &&
563             prev_state.event == PM_EVENT_FREEZE) {
564                 struct pmb_entry *pmbe;
565
566                 read_lock(&pmb_rwlock);
567
568                 for (i = 0; i < ARRAY_SIZE(pmb_entry_list); i++) {
569                         if (test_bit(i, pmb_map)) {
570                                 pmbe = &pmb_entry_list[i];
571                                 set_pmb_entry(pmbe);
572                         }
573                 }
574
575                 read_unlock(&pmb_rwlock);
576         }
577
578         prev_state = state;
579
580         return 0;
581 }
582
583 static int pmb_sysdev_resume(struct sys_device *dev)
584 {
585         return pmb_sysdev_suspend(dev, PMSG_ON);
586 }
587
588 static struct sysdev_driver pmb_sysdev_driver = {
589         .suspend = pmb_sysdev_suspend,
590         .resume = pmb_sysdev_resume,
591 };
592
593 static int __init pmb_sysdev_init(void)
594 {
595         return sysdev_driver_register(&cpu_sysdev_class, &pmb_sysdev_driver);
596 }
597 subsys_initcall(pmb_sysdev_init);
598 #endif