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