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