35b364f931eafecb75daaa754207627e13fce8a9
[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 <asm/sizes.h>
27 #include <asm/system.h>
28 #include <asm/uaccess.h>
29 #include <asm/pgtable.h>
30 #include <asm/page.h>
31 #include <asm/mmu.h>
32 #include <asm/mmu_context.h>
33
34 struct pmb_entry;
35
36 struct pmb_entry {
37         unsigned long vpn;
38         unsigned long ppn;
39         unsigned long flags;
40         unsigned long size;
41
42         spinlock_t lock;
43
44         /*
45          * 0 .. NR_PMB_ENTRIES for specific entry selection, or
46          * PMB_NO_ENTRY to search for a free one
47          */
48         int entry;
49
50         /* Adjacent entry link for contiguous multi-entry mappings */
51         struct pmb_entry *link;
52 };
53
54 static void pmb_unmap_entry(struct pmb_entry *, int depth);
55
56 static DEFINE_RWLOCK(pmb_rwlock);
57 static struct pmb_entry pmb_entry_list[NR_PMB_ENTRIES];
58 static DECLARE_BITMAP(pmb_map, NR_PMB_ENTRIES);
59
60 static __always_inline unsigned long mk_pmb_entry(unsigned int entry)
61 {
62         return (entry & PMB_E_MASK) << PMB_E_SHIFT;
63 }
64
65 static __always_inline unsigned long mk_pmb_addr(unsigned int entry)
66 {
67         return mk_pmb_entry(entry) | PMB_ADDR;
68 }
69
70 static __always_inline unsigned long mk_pmb_data(unsigned int entry)
71 {
72         return mk_pmb_entry(entry) | PMB_DATA;
73 }
74
75 static int pmb_alloc_entry(void)
76 {
77         int pos;
78
79         pos = find_first_zero_bit(pmb_map, NR_PMB_ENTRIES);
80         if (pos >= 0 && pos < NR_PMB_ENTRIES)
81                 __set_bit(pos, pmb_map);
82         else
83                 pos = -ENOSPC;
84
85         return pos;
86 }
87
88 static struct pmb_entry *pmb_alloc(unsigned long vpn, unsigned long ppn,
89                                    unsigned long flags, int entry)
90 {
91         struct pmb_entry *pmbe;
92         unsigned long irqflags;
93         void *ret = NULL;
94         int pos;
95
96         write_lock_irqsave(&pmb_rwlock, irqflags);
97
98         if (entry == PMB_NO_ENTRY) {
99                 pos = pmb_alloc_entry();
100                 if (unlikely(pos < 0)) {
101                         ret = ERR_PTR(pos);
102                         goto out;
103                 }
104         } else {
105                 if (__test_and_set_bit(entry, pmb_map)) {
106                         ret = ERR_PTR(-ENOSPC);
107                         goto out;
108                 }
109
110                 pos = entry;
111         }
112
113         write_unlock_irqrestore(&pmb_rwlock, irqflags);
114
115         pmbe = &pmb_entry_list[pos];
116
117         memset(pmbe, 0, sizeof(struct pmb_entry));
118
119         spin_lock_init(&pmbe->lock);
120
121         pmbe->vpn       = vpn;
122         pmbe->ppn       = ppn;
123         pmbe->flags     = flags;
124         pmbe->entry     = pos;
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
137         pmbe->entry     = PMB_NO_ENTRY;
138         pmbe->link      = NULL;
139 }
140
141 /*
142  * Ensure that the PMB entries match our cache configuration.
143  *
144  * When we are in 32-bit address extended mode, CCR.CB becomes
145  * invalid, so care must be taken to manually adjust cacheable
146  * translations.
147  */
148 static __always_inline unsigned long pmb_cache_flags(void)
149 {
150         unsigned long flags = 0;
151
152 #if defined(CONFIG_CACHE_WRITETHROUGH)
153         flags |= PMB_C | PMB_WT | PMB_UB;
154 #elif defined(CONFIG_CACHE_WRITEBACK)
155         flags |= PMB_C;
156 #endif
157
158         return flags;
159 }
160
161 /*
162  * Must be run uncached.
163  */
164 static void __set_pmb_entry(struct pmb_entry *pmbe)
165 {
166         writel_uncached(pmbe->vpn | PMB_V, mk_pmb_addr(pmbe->entry));
167         writel_uncached(pmbe->ppn | pmbe->flags | PMB_V,
168                         mk_pmb_data(pmbe->entry));
169 }
170
171 static void __clear_pmb_entry(struct pmb_entry *pmbe)
172 {
173         unsigned long addr, data;
174         unsigned long addr_val, data_val;
175
176         addr = mk_pmb_addr(pmbe->entry);
177         data = mk_pmb_data(pmbe->entry);
178
179         addr_val = __raw_readl(addr);
180         data_val = __raw_readl(data);
181
182         /* Clear V-bit */
183         writel_uncached(addr_val & ~PMB_V, addr);
184         writel_uncached(data_val & ~PMB_V, data);
185 }
186
187 static void set_pmb_entry(struct pmb_entry *pmbe)
188 {
189         unsigned long flags;
190
191         spin_lock_irqsave(&pmbe->lock, flags);
192         __set_pmb_entry(pmbe);
193         spin_unlock_irqrestore(&pmbe->lock, flags);
194 }
195
196 static struct {
197         unsigned long size;
198         int flag;
199 } pmb_sizes[] = {
200         { .size = SZ_512M, .flag = PMB_SZ_512M, },
201         { .size = SZ_128M, .flag = PMB_SZ_128M, },
202         { .size = SZ_64M,  .flag = PMB_SZ_64M,  },
203         { .size = SZ_16M,  .flag = PMB_SZ_16M,  },
204 };
205
206 long pmb_remap(unsigned long vaddr, unsigned long phys,
207                unsigned long size, pgprot_t prot)
208 {
209         struct pmb_entry *pmbp, *pmbe;
210         unsigned long wanted;
211         int pmb_flags, i;
212         long err;
213         u64 flags;
214
215         flags = pgprot_val(prot);
216
217         pmb_flags = PMB_WT | PMB_UB;
218
219         /* Convert typical pgprot value to the PMB equivalent */
220         if (flags & _PAGE_CACHABLE) {
221                 pmb_flags |= PMB_C;
222
223                 if ((flags & _PAGE_WT) == 0)
224                         pmb_flags &= ~(PMB_WT | PMB_UB);
225         }
226
227         pmbp = NULL;
228         wanted = size;
229
230 again:
231         for (i = 0; i < ARRAY_SIZE(pmb_sizes); i++) {
232                 unsigned long flags;
233
234                 if (size < pmb_sizes[i].size)
235                         continue;
236
237                 pmbe = pmb_alloc(vaddr, phys, pmb_flags | pmb_sizes[i].flag,
238                                  PMB_NO_ENTRY);
239                 if (IS_ERR(pmbe)) {
240                         err = PTR_ERR(pmbe);
241                         goto out;
242                 }
243
244                 spin_lock_irqsave(&pmbe->lock, flags);
245
246                 __set_pmb_entry(pmbe);
247
248                 phys    += pmb_sizes[i].size;
249                 vaddr   += pmb_sizes[i].size;
250                 size    -= pmb_sizes[i].size;
251
252                 pmbe->size = pmb_sizes[i].size;
253
254                 /*
255                  * Link adjacent entries that span multiple PMB entries
256                  * for easier tear-down.
257                  */
258                 if (likely(pmbp)) {
259                         spin_lock(&pmbp->lock);
260                         pmbp->link = pmbe;
261                         spin_unlock(&pmbp->lock);
262                 }
263
264                 pmbp = pmbe;
265
266                 /*
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.
270                  */
271                 i--;
272
273                 spin_unlock_irqrestore(&pmbe->lock, flags);
274         }
275
276         if (size >= SZ_16M)
277                 goto again;
278
279         return wanted - size;
280
281 out:
282         pmb_unmap_entry(pmbp, NR_PMB_ENTRIES);
283
284         return err;
285 }
286
287 void pmb_unmap(unsigned long addr)
288 {
289         struct pmb_entry *pmbe = NULL;
290         int i;
291
292         read_lock(&pmb_rwlock);
293
294         for (i = 0; i < ARRAY_SIZE(pmb_entry_list); i++) {
295                 if (test_bit(i, pmb_map)) {
296                         pmbe = &pmb_entry_list[i];
297                         if (pmbe->vpn == addr)
298                                 break;
299                 }
300         }
301
302         read_unlock(&pmb_rwlock);
303
304         pmb_unmap_entry(pmbe, NR_PMB_ENTRIES);
305 }
306
307 static bool pmb_can_merge(struct pmb_entry *a, struct pmb_entry *b)
308 {
309         return (b->vpn == (a->vpn + a->size)) &&
310                (b->ppn == (a->ppn + a->size)) &&
311                (b->flags == a->flags);
312 }
313
314 static bool pmb_size_valid(unsigned long size)
315 {
316         int i;
317
318         for (i = 0; i < ARRAY_SIZE(pmb_sizes); i++)
319                 if (pmb_sizes[i].size == size)
320                         return true;
321
322         return false;
323 }
324
325 static int pmb_size_to_flags(unsigned long size)
326 {
327         int i;
328
329         for (i = 0; i < ARRAY_SIZE(pmb_sizes); i++)
330                 if (pmb_sizes[i].size == size)
331                         return pmb_sizes[i].flag;
332
333         return 0;
334 }
335
336 static void __pmb_unmap_entry(struct pmb_entry *pmbe, int depth)
337 {
338         do {
339                 struct pmb_entry *pmblink = pmbe;
340
341                 /*
342                  * We may be called before this pmb_entry has been
343                  * entered into the PMB table via set_pmb_entry(), but
344                  * that's OK because we've allocated a unique slot for
345                  * this entry in pmb_alloc() (even if we haven't filled
346                  * it yet).
347                  *
348                  * Therefore, calling __clear_pmb_entry() is safe as no
349                  * other mapping can be using that slot.
350                  */
351                 __clear_pmb_entry(pmbe);
352
353                 pmbe = pmblink->link;
354
355                 pmb_free(pmblink);
356         } while (pmbe && --depth);
357 }
358
359 static void pmb_unmap_entry(struct pmb_entry *pmbe, int depth)
360 {
361         unsigned long flags;
362
363         if (unlikely(!pmbe))
364                 return;
365
366         write_lock_irqsave(&pmb_rwlock, flags);
367         __pmb_unmap_entry(pmbe, depth);
368         write_unlock_irqrestore(&pmb_rwlock, flags);
369 }
370
371 static __always_inline unsigned int pmb_ppn_in_range(unsigned long ppn)
372 {
373         return ppn >= __pa(memory_start) && ppn < __pa(memory_end);
374 }
375
376 static void __init pmb_notify(void)
377 {
378         int i;
379
380         pr_info("PMB: boot mappings:\n");
381
382         read_lock(&pmb_rwlock);
383
384         for (i = 0; i < ARRAY_SIZE(pmb_entry_list); i++) {
385                 struct pmb_entry *pmbe;
386
387                 if (!test_bit(i, pmb_map))
388                         continue;
389
390                 pmbe = &pmb_entry_list[i];
391
392                 pr_info("       0x%08lx -> 0x%08lx [ %4ldMB %2scached ]\n",
393                         pmbe->vpn >> PAGE_SHIFT, pmbe->ppn >> PAGE_SHIFT,
394                         pmbe->size >> 20, (pmbe->flags & PMB_C) ? "" : "un");
395         }
396
397         read_unlock(&pmb_rwlock);
398 }
399
400 /*
401  * Sync our software copy of the PMB mappings with those in hardware. The
402  * mappings in the hardware PMB were either set up by the bootloader or
403  * very early on by the kernel.
404  */
405 static void __init pmb_synchronize(void)
406 {
407         struct pmb_entry *pmbp = NULL;
408         int i, j;
409
410         /*
411          * Run through the initial boot mappings, log the established
412          * ones, and blow away anything that falls outside of the valid
413          * PPN range. Specifically, we only care about existing mappings
414          * that impact the cached/uncached sections.
415          *
416          * Note that touching these can be a bit of a minefield; the boot
417          * loader can establish multi-page mappings with the same caching
418          * attributes, so we need to ensure that we aren't modifying a
419          * mapping that we're presently executing from, or may execute
420          * from in the case of straddling page boundaries.
421          *
422          * In the future we will have to tidy up after the boot loader by
423          * jumping between the cached and uncached mappings and tearing
424          * down alternating mappings while executing from the other.
425          */
426         for (i = 0; i < NR_PMB_ENTRIES; i++) {
427                 unsigned long addr, data;
428                 unsigned long addr_val, data_val;
429                 unsigned long ppn, vpn, flags;
430                 unsigned long irqflags;
431                 unsigned int size;
432                 struct pmb_entry *pmbe;
433
434                 addr = mk_pmb_addr(i);
435                 data = mk_pmb_data(i);
436
437                 addr_val = __raw_readl(addr);
438                 data_val = __raw_readl(data);
439
440                 /*
441                  * Skip over any bogus entries
442                  */
443                 if (!(data_val & PMB_V) || !(addr_val & PMB_V))
444                         continue;
445
446                 ppn = data_val & PMB_PFN_MASK;
447                 vpn = addr_val & PMB_PFN_MASK;
448
449                 /*
450                  * Only preserve in-range mappings.
451                  */
452                 if (!pmb_ppn_in_range(ppn)) {
453                         /*
454                          * Invalidate anything out of bounds.
455                          */
456                         writel_uncached(addr_val & ~PMB_V, addr);
457                         writel_uncached(data_val & ~PMB_V, data);
458                         continue;
459                 }
460
461                 /*
462                  * Update the caching attributes if necessary
463                  */
464                 if (data_val & PMB_C) {
465                         data_val &= ~PMB_CACHE_MASK;
466                         data_val |= pmb_cache_flags();
467
468                         writel_uncached(data_val, data);
469                 }
470
471                 size = data_val & PMB_SZ_MASK;
472                 flags = size | (data_val & PMB_CACHE_MASK);
473
474                 pmbe = pmb_alloc(vpn, ppn, flags, i);
475                 if (IS_ERR(pmbe)) {
476                         WARN_ON_ONCE(1);
477                         continue;
478                 }
479
480                 spin_lock_irqsave(&pmbe->lock, irqflags);
481
482                 for (j = 0; j < ARRAY_SIZE(pmb_sizes); j++)
483                         if (pmb_sizes[j].flag == size)
484                                 pmbe->size = pmb_sizes[j].size;
485
486                 if (pmbp) {
487                         spin_lock(&pmbp->lock);
488
489                         /*
490                          * Compare the previous entry against the current one to
491                          * see if the entries span a contiguous mapping. If so,
492                          * setup the entry links accordingly. Compound mappings
493                          * are later coalesced.
494                          */
495                         if (pmb_can_merge(pmbp, pmbe))
496                                 pmbp->link = pmbe;
497
498                         spin_unlock(&pmbp->lock);
499                 }
500
501                 pmbp = pmbe;
502
503                 spin_unlock_irqrestore(&pmbe->lock, irqflags);
504         }
505 }
506
507 static void __init pmb_merge(struct pmb_entry *head)
508 {
509         unsigned long span, newsize;
510         struct pmb_entry *tail;
511         int i = 1, depth = 0;
512
513         span = newsize = head->size;
514
515         tail = head->link;
516         while (tail) {
517                 span += tail->size;
518
519                 if (pmb_size_valid(span)) {
520                         newsize = span;
521                         depth = i;
522                 }
523
524                 /* This is the end of the line.. */
525                 if (!tail->link)
526                         break;
527
528                 tail = tail->link;
529                 i++;
530         }
531
532         /*
533          * The merged page size must be valid.
534          */
535         if (!pmb_size_valid(newsize))
536                 return;
537
538         head->flags &= ~PMB_SZ_MASK;
539         head->flags |= pmb_size_to_flags(newsize);
540
541         head->size = newsize;
542
543         __pmb_unmap_entry(head->link, depth);
544         __set_pmb_entry(head);
545 }
546
547 static void __init pmb_coalesce(void)
548 {
549         unsigned long flags;
550         int i;
551
552         write_lock_irqsave(&pmb_rwlock, flags);
553
554         for (i = 0; i < ARRAY_SIZE(pmb_entry_list); i++) {
555                 struct pmb_entry *pmbe;
556
557                 if (!test_bit(i, pmb_map))
558                         continue;
559
560                 pmbe = &pmb_entry_list[i];
561
562                 /*
563                  * We're only interested in compound mappings
564                  */
565                 if (!pmbe->link)
566                         continue;
567
568                 /*
569                  * Nothing to do if it already uses the largest possible
570                  * page size.
571                  */
572                 if (pmbe->size == SZ_512M)
573                         continue;
574
575                 pmb_merge(pmbe);
576         }
577
578         write_unlock_irqrestore(&pmb_rwlock, flags);
579 }
580
581 #ifdef CONFIG_UNCACHED_MAPPING
582 static void __init pmb_resize(void)
583 {
584         int i;
585
586         /*
587          * If the uncached mapping was constructed by the kernel, it will
588          * already be a reasonable size.
589          */
590         if (uncached_size == SZ_16M)
591                 return;
592
593         read_lock(&pmb_rwlock);
594
595         for (i = 0; i < ARRAY_SIZE(pmb_entry_list); i++) {
596                 struct pmb_entry *pmbe;
597                 unsigned long flags;
598
599                 if (!test_bit(i, pmb_map))
600                         continue;
601
602                 pmbe = &pmb_entry_list[i];
603
604                 if (pmbe->vpn != uncached_start)
605                         continue;
606
607                 /*
608                  * Found it, now resize it.
609                  */
610                 spin_lock_irqsave(&pmbe->lock, flags);
611
612                 pmbe->size = SZ_16M;
613                 pmbe->flags &= ~PMB_SZ_MASK;
614                 pmbe->flags |= pmb_size_to_flags(pmbe->size);
615
616                 uncached_resize(pmbe->size);
617
618                 __set_pmb_entry(pmbe);
619
620                 spin_unlock_irqrestore(&pmbe->lock, flags);
621         }
622
623         read_lock(&pmb_rwlock);
624 }
625 #endif
626
627 void __init pmb_init(void)
628 {
629         /* Synchronize software state */
630         pmb_synchronize();
631
632         /* Attempt to combine compound mappings */
633         pmb_coalesce();
634
635 #ifdef CONFIG_UNCACHED_MAPPING
636         /* Resize initial mappings, if necessary */
637         pmb_resize();
638 #endif
639
640         /* Log them */
641         pmb_notify();
642
643         writel_uncached(0, PMB_IRMCR);
644
645         /* Flush out the TLB */
646         __raw_writel(__raw_readl(MMUCR) | MMUCR_TI, MMUCR);
647         ctrl_barrier();
648 }
649
650 bool __in_29bit_mode(void)
651 {
652         return (__raw_readl(PMB_PASCR) & PASCR_SE) == 0;
653 }
654
655 static int pmb_seq_show(struct seq_file *file, void *iter)
656 {
657         int i;
658
659         seq_printf(file, "V: Valid, C: Cacheable, WT: Write-Through\n"
660                          "CB: Copy-Back, B: Buffered, UB: Unbuffered\n");
661         seq_printf(file, "ety   vpn  ppn  size   flags\n");
662
663         for (i = 0; i < NR_PMB_ENTRIES; i++) {
664                 unsigned long addr, data;
665                 unsigned int size;
666                 char *sz_str = NULL;
667
668                 addr = __raw_readl(mk_pmb_addr(i));
669                 data = __raw_readl(mk_pmb_data(i));
670
671                 size = data & PMB_SZ_MASK;
672                 sz_str = (size == PMB_SZ_16M)  ? " 16MB":
673                          (size == PMB_SZ_64M)  ? " 64MB":
674                          (size == PMB_SZ_128M) ? "128MB":
675                                                  "512MB";
676
677                 /* 02: V 0x88 0x08 128MB C CB  B */
678                 seq_printf(file, "%02d: %c 0x%02lx 0x%02lx %s %c %s %s\n",
679                            i, ((addr & PMB_V) && (data & PMB_V)) ? 'V' : ' ',
680                            (addr >> 24) & 0xff, (data >> 24) & 0xff,
681                            sz_str, (data & PMB_C) ? 'C' : ' ',
682                            (data & PMB_WT) ? "WT" : "CB",
683                            (data & PMB_UB) ? "UB" : " B");
684         }
685
686         return 0;
687 }
688
689 static int pmb_debugfs_open(struct inode *inode, struct file *file)
690 {
691         return single_open(file, pmb_seq_show, NULL);
692 }
693
694 static const struct file_operations pmb_debugfs_fops = {
695         .owner          = THIS_MODULE,
696         .open           = pmb_debugfs_open,
697         .read           = seq_read,
698         .llseek         = seq_lseek,
699         .release        = single_release,
700 };
701
702 static int __init pmb_debugfs_init(void)
703 {
704         struct dentry *dentry;
705
706         dentry = debugfs_create_file("pmb", S_IFREG | S_IRUGO,
707                                      sh_debugfs_root, NULL, &pmb_debugfs_fops);
708         if (!dentry)
709                 return -ENOMEM;
710         if (IS_ERR(dentry))
711                 return PTR_ERR(dentry);
712
713         return 0;
714 }
715 postcore_initcall(pmb_debugfs_init);
716
717 #ifdef CONFIG_PM
718 static int pmb_sysdev_suspend(struct sys_device *dev, pm_message_t state)
719 {
720         static pm_message_t prev_state;
721         int i;
722
723         /* Restore the PMB after a resume from hibernation */
724         if (state.event == PM_EVENT_ON &&
725             prev_state.event == PM_EVENT_FREEZE) {
726                 struct pmb_entry *pmbe;
727
728                 read_lock(&pmb_rwlock);
729
730                 for (i = 0; i < ARRAY_SIZE(pmb_entry_list); i++) {
731                         if (test_bit(i, pmb_map)) {
732                                 pmbe = &pmb_entry_list[i];
733                                 set_pmb_entry(pmbe);
734                         }
735                 }
736
737                 read_unlock(&pmb_rwlock);
738         }
739
740         prev_state = state;
741
742         return 0;
743 }
744
745 static int pmb_sysdev_resume(struct sys_device *dev)
746 {
747         return pmb_sysdev_suspend(dev, PMSG_ON);
748 }
749
750 static struct sysdev_driver pmb_sysdev_driver = {
751         .suspend = pmb_sysdev_suspend,
752         .resume = pmb_sysdev_resume,
753 };
754
755 static int __init pmb_sysdev_init(void)
756 {
757         return sysdev_driver_register(&cpu_sysdev_class, &pmb_sysdev_driver);
758 }
759 subsys_initcall(pmb_sysdev_init);
760 #endif