sh: Use uncached I/O helpers in PMB setup.
[safe/jmp/linux-2.6] / 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         pmbe->flags &= ~PMB_CACHE_MASK;
165         pmbe->flags |= pmb_cache_flags();
166
167         writel_uncached(pmbe->vpn | PMB_V, mk_pmb_addr(pmbe->entry));
168         writel_uncached(pmbe->ppn | pmbe->flags | PMB_V,
169                         mk_pmb_data(pmbe->entry));
170 }
171
172 static void __clear_pmb_entry(struct pmb_entry *pmbe)
173 {
174         unsigned long addr, data;
175         unsigned long addr_val, data_val;
176
177         addr = mk_pmb_addr(pmbe->entry);
178         data = mk_pmb_data(pmbe->entry);
179
180         addr_val = __raw_readl(addr);
181         data_val = __raw_readl(data);
182
183         /* Clear V-bit */
184         writel_uncached(addr_val & ~PMB_V, addr);
185         writel_uncached(data_val & ~PMB_V, data);
186 }
187
188 static void set_pmb_entry(struct pmb_entry *pmbe)
189 {
190         unsigned long flags;
191
192         spin_lock_irqsave(&pmbe->lock, flags);
193         __set_pmb_entry(pmbe);
194         spin_unlock_irqrestore(&pmbe->lock, flags);
195 }
196
197 static struct {
198         unsigned long size;
199         int flag;
200 } pmb_sizes[] = {
201         { .size = SZ_512M, .flag = PMB_SZ_512M, },
202         { .size = SZ_128M, .flag = PMB_SZ_128M, },
203         { .size = SZ_64M,  .flag = PMB_SZ_64M,  },
204         { .size = SZ_16M,  .flag = PMB_SZ_16M,  },
205 };
206
207 long pmb_remap(unsigned long vaddr, unsigned long phys,
208                unsigned long size, pgprot_t prot)
209 {
210         struct pmb_entry *pmbp, *pmbe;
211         unsigned long wanted;
212         int pmb_flags, i;
213         long err;
214         u64 flags;
215
216         flags = pgprot_val(prot);
217
218         pmb_flags = PMB_WT | PMB_UB;
219
220         /* Convert typical pgprot value to the PMB equivalent */
221         if (flags & _PAGE_CACHABLE) {
222                 pmb_flags |= PMB_C;
223
224                 if ((flags & _PAGE_WT) == 0)
225                         pmb_flags &= ~(PMB_WT | PMB_UB);
226         }
227
228         pmbp = NULL;
229         wanted = size;
230
231 again:
232         for (i = 0; i < ARRAY_SIZE(pmb_sizes); i++) {
233                 unsigned long flags;
234
235                 if (size < pmb_sizes[i].size)
236                         continue;
237
238                 pmbe = pmb_alloc(vaddr, phys, pmb_flags | pmb_sizes[i].flag,
239                                  PMB_NO_ENTRY);
240                 if (IS_ERR(pmbe)) {
241                         err = PTR_ERR(pmbe);
242                         goto out;
243                 }
244
245                 spin_lock_irqsave(&pmbe->lock, flags);
246
247                 __set_pmb_entry(pmbe);
248
249                 phys    += pmb_sizes[i].size;
250                 vaddr   += pmb_sizes[i].size;
251                 size    -= pmb_sizes[i].size;
252
253                 pmbe->size = pmb_sizes[i].size;
254
255                 /*
256                  * Link adjacent entries that span multiple PMB entries
257                  * for easier tear-down.
258                  */
259                 if (likely(pmbp)) {
260                         spin_lock(&pmbp->lock);
261                         pmbp->link = pmbe;
262                         spin_unlock(&pmbp->lock);
263                 }
264
265                 pmbp = pmbe;
266
267                 /*
268                  * Instead of trying smaller sizes on every iteration
269                  * (even if we succeed in allocating space), try using
270                  * pmb_sizes[i].size again.
271                  */
272                 i--;
273
274                 spin_unlock_irqrestore(&pmbe->lock, flags);
275         }
276
277         if (size >= SZ_16M)
278                 goto again;
279
280         return wanted - size;
281
282 out:
283         pmb_unmap_entry(pmbp);
284
285         return err;
286 }
287
288 void pmb_unmap(unsigned long addr)
289 {
290         struct pmb_entry *pmbe = NULL;
291         int i;
292
293         read_lock(&pmb_rwlock);
294
295         for (i = 0; i < ARRAY_SIZE(pmb_entry_list); i++) {
296                 if (test_bit(i, pmb_map)) {
297                         pmbe = &pmb_entry_list[i];
298                         if (pmbe->vpn == addr)
299                                 break;
300                 }
301         }
302
303         read_unlock(&pmb_rwlock);
304
305         pmb_unmap_entry(pmbe);
306 }
307
308 static void pmb_unmap_entry(struct pmb_entry *pmbe)
309 {
310         unsigned long flags;
311
312         if (unlikely(!pmbe))
313                 return;
314
315         write_lock_irqsave(&pmb_rwlock, flags);
316
317         do {
318                 struct pmb_entry *pmblink = pmbe;
319
320                 /*
321                  * We may be called before this pmb_entry has been
322                  * entered into the PMB table via set_pmb_entry(), but
323                  * that's OK because we've allocated a unique slot for
324                  * this entry in pmb_alloc() (even if we haven't filled
325                  * it yet).
326                  *
327                  * Therefore, calling __clear_pmb_entry() is safe as no
328                  * other mapping can be using that slot.
329                  */
330                 __clear_pmb_entry(pmbe);
331
332                 pmbe = pmblink->link;
333
334                 pmb_free(pmblink);
335         } while (pmbe);
336
337         write_unlock_irqrestore(&pmb_rwlock, flags);
338 }
339
340 static __always_inline unsigned int pmb_ppn_in_range(unsigned long ppn)
341 {
342         return ppn >= __pa(memory_start) && ppn < __pa(memory_end);
343 }
344
345 static int pmb_synchronize_mappings(void)
346 {
347         unsigned int applied = 0;
348         struct pmb_entry *pmbp = NULL;
349         int i, j;
350
351         pr_info("PMB: boot mappings:\n");
352
353         /*
354          * Run through the initial boot mappings, log the established
355          * ones, and blow away anything that falls outside of the valid
356          * PPN range. Specifically, we only care about existing mappings
357          * that impact the cached/uncached sections.
358          *
359          * Note that touching these can be a bit of a minefield; the boot
360          * loader can establish multi-page mappings with the same caching
361          * attributes, so we need to ensure that we aren't modifying a
362          * mapping that we're presently executing from, or may execute
363          * from in the case of straddling page boundaries.
364          *
365          * In the future we will have to tidy up after the boot loader by
366          * jumping between the cached and uncached mappings and tearing
367          * down alternating mappings while executing from the other.
368          */
369         for (i = 0; i < NR_PMB_ENTRIES; i++) {
370                 unsigned long addr, data;
371                 unsigned long addr_val, data_val;
372                 unsigned long ppn, vpn, flags;
373                 unsigned long irqflags;
374                 unsigned int size;
375                 struct pmb_entry *pmbe;
376
377                 addr = mk_pmb_addr(i);
378                 data = mk_pmb_data(i);
379
380                 addr_val = __raw_readl(addr);
381                 data_val = __raw_readl(data);
382
383                 /*
384                  * Skip over any bogus entries
385                  */
386                 if (!(data_val & PMB_V) || !(addr_val & PMB_V))
387                         continue;
388
389                 ppn = data_val & PMB_PFN_MASK;
390                 vpn = addr_val & PMB_PFN_MASK;
391
392                 /*
393                  * Only preserve in-range mappings.
394                  */
395                 if (!pmb_ppn_in_range(ppn)) {
396                         /*
397                          * Invalidate anything out of bounds.
398                          */
399                         writel_uncached(addr_val & ~PMB_V, addr);
400                         writel_uncached(data_val & ~PMB_V, data);
401                         continue;
402                 }
403
404                 /*
405                  * Update the caching attributes if necessary
406                  */
407                 if (data_val & PMB_C) {
408                         data_val &= ~PMB_CACHE_MASK;
409                         data_val |= pmb_cache_flags();
410
411                         writel_uncached(data_val, data);
412                 }
413
414                 size = data_val & PMB_SZ_MASK;
415                 flags = size | (data_val & PMB_CACHE_MASK);
416
417                 pmbe = pmb_alloc(vpn, ppn, flags, i);
418                 if (IS_ERR(pmbe)) {
419                         WARN_ON_ONCE(1);
420                         continue;
421                 }
422
423                 spin_lock_irqsave(&pmbe->lock, irqflags);
424
425                 for (j = 0; j < ARRAY_SIZE(pmb_sizes); j++)
426                         if (pmb_sizes[j].flag == size)
427                                 pmbe->size = pmb_sizes[j].size;
428
429                 if (pmbp) {
430                         spin_lock(&pmbp->lock);
431
432                         /*
433                          * Compare the previous entry against the current one to
434                          * see if the entries span a contiguous mapping. If so,
435                          * setup the entry links accordingly.
436                          */
437                         if ((pmbe->vpn == (pmbp->vpn + pmbp->size)) &&
438                             (pmbe->ppn == (pmbp->ppn + pmbp->size)))
439                                 pmbp->link = pmbe;
440
441                         spin_unlock(&pmbp->lock);
442                 }
443
444                 pmbp = pmbe;
445
446                 spin_unlock_irqrestore(&pmbe->lock, irqflags);
447
448                 pr_info("\t0x%08lx -> 0x%08lx [ %ldMB %scached ]\n",
449                         vpn >> PAGE_SHIFT, ppn >> PAGE_SHIFT, pmbe->size >> 20,
450                         (data_val & PMB_C) ? "" : "un");
451
452                 applied++;
453         }
454
455         return (applied == 0);
456 }
457
458 int pmb_init(void)
459 {
460         int ret;
461
462         /*
463          * Sync our software copy of the PMB mappings with those in
464          * hardware. The mappings in the hardware PMB were either set up
465          * by the bootloader or very early on by the kernel.
466          */
467         ret = pmb_synchronize_mappings();
468         if (unlikely(ret == 0))
469                 return 0;
470
471         writel_uncached(0, PMB_IRMCR);
472
473         /* Flush out the TLB */
474         __raw_writel(__raw_readl(MMUCR) | MMUCR_TI, MMUCR);
475         ctrl_barrier();
476
477         return 0;
478 }
479
480 bool __in_29bit_mode(void)
481 {
482         return (__raw_readl(PMB_PASCR) & PASCR_SE) == 0;
483 }
484
485 static int pmb_seq_show(struct seq_file *file, void *iter)
486 {
487         int i;
488
489         seq_printf(file, "V: Valid, C: Cacheable, WT: Write-Through\n"
490                          "CB: Copy-Back, B: Buffered, UB: Unbuffered\n");
491         seq_printf(file, "ety   vpn  ppn  size   flags\n");
492
493         for (i = 0; i < NR_PMB_ENTRIES; i++) {
494                 unsigned long addr, data;
495                 unsigned int size;
496                 char *sz_str = NULL;
497
498                 addr = __raw_readl(mk_pmb_addr(i));
499                 data = __raw_readl(mk_pmb_data(i));
500
501                 size = data & PMB_SZ_MASK;
502                 sz_str = (size == PMB_SZ_16M)  ? " 16MB":
503                          (size == PMB_SZ_64M)  ? " 64MB":
504                          (size == PMB_SZ_128M) ? "128MB":
505                                                  "512MB";
506
507                 /* 02: V 0x88 0x08 128MB C CB  B */
508                 seq_printf(file, "%02d: %c 0x%02lx 0x%02lx %s %c %s %s\n",
509                            i, ((addr & PMB_V) && (data & PMB_V)) ? 'V' : ' ',
510                            (addr >> 24) & 0xff, (data >> 24) & 0xff,
511                            sz_str, (data & PMB_C) ? 'C' : ' ',
512                            (data & PMB_WT) ? "WT" : "CB",
513                            (data & PMB_UB) ? "UB" : " B");
514         }
515
516         return 0;
517 }
518
519 static int pmb_debugfs_open(struct inode *inode, struct file *file)
520 {
521         return single_open(file, pmb_seq_show, NULL);
522 }
523
524 static const struct file_operations pmb_debugfs_fops = {
525         .owner          = THIS_MODULE,
526         .open           = pmb_debugfs_open,
527         .read           = seq_read,
528         .llseek         = seq_lseek,
529         .release        = single_release,
530 };
531
532 static int __init pmb_debugfs_init(void)
533 {
534         struct dentry *dentry;
535
536         dentry = debugfs_create_file("pmb", S_IFREG | S_IRUGO,
537                                      sh_debugfs_root, NULL, &pmb_debugfs_fops);
538         if (!dentry)
539                 return -ENOMEM;
540         if (IS_ERR(dentry))
541                 return PTR_ERR(dentry);
542
543         return 0;
544 }
545 postcore_initcall(pmb_debugfs_init);
546
547 #ifdef CONFIG_PM
548 static int pmb_sysdev_suspend(struct sys_device *dev, pm_message_t state)
549 {
550         static pm_message_t prev_state;
551         int i;
552
553         /* Restore the PMB after a resume from hibernation */
554         if (state.event == PM_EVENT_ON &&
555             prev_state.event == PM_EVENT_FREEZE) {
556                 struct pmb_entry *pmbe;
557
558                 read_lock(&pmb_rwlock);
559
560                 for (i = 0; i < ARRAY_SIZE(pmb_entry_list); i++) {
561                         if (test_bit(i, pmb_map)) {
562                                 pmbe = &pmb_entry_list[i];
563                                 set_pmb_entry(pmbe);
564                         }
565                 }
566
567                 read_unlock(&pmb_rwlock);
568         }
569
570         prev_state = state;
571
572         return 0;
573 }
574
575 static int pmb_sysdev_resume(struct sys_device *dev)
576 {
577         return pmb_sysdev_suspend(dev, PMSG_ON);
578 }
579
580 static struct sysdev_driver pmb_sysdev_driver = {
581         .suspend = pmb_sysdev_suspend,
582         .resume = pmb_sysdev_resume,
583 };
584
585 static int __init pmb_sysdev_init(void)
586 {
587         return sysdev_driver_register(&cpu_sysdev_class, &pmb_sysdev_driver);
588 }
589 subsys_initcall(pmb_sysdev_init);
590 #endif