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