sh: Add flag for MMU PTEA capability.
[pandora-kernel.git] / arch / sh / kernel / cpu / sh4 / sq.c
1 /*
2  * arch/sh/kernel/cpu/sq.c
3  *
4  * General management API for SH-4 integrated Store Queues
5  *
6  * Copyright (C) 2001, 2002, 2003, 2004  Paul Mundt
7  * Copyright (C) 2001, 2002  M. R. Brown
8  *
9  * Some of this code has been adopted directly from the old arch/sh/mm/sq.c
10  * hack that was part of the LinuxDC project. For all intents and purposes,
11  * this is a completely new interface that really doesn't have much in common
12  * with the old zone-based approach at all. In fact, it's only listed here for
13  * general completeness.
14  *
15  * This file is subject to the terms and conditions of the GNU General Public
16  * License.  See the file "COPYING" in the main directory of this archive
17  * for more details.
18  */
19 #include <linux/init.h>
20 #include <linux/kernel.h>
21 #include <linux/module.h>
22 #include <linux/slab.h>
23 #include <linux/list.h>
24 #include <linux/proc_fs.h>
25 #include <linux/miscdevice.h>
26 #include <linux/vmalloc.h>
27 #include <linux/mm.h>
28 #include <asm/io.h>
29 #include <asm/page.h>
30 #include <asm/cacheflush.h>
31 #include <asm/mmu_context.h>
32 #include <asm/cpu/sq.h>
33
34 static LIST_HEAD(sq_mapping_list);
35 static DEFINE_SPINLOCK(sq_mapping_lock);
36
37 /**
38  * sq_flush - Flush (prefetch) the store queue cache
39  * @addr: the store queue address to flush
40  *
41  * Executes a prefetch instruction on the specified store queue cache,
42  * so that the cached data is written to physical memory.
43  */
44 inline void sq_flush(void *addr)
45 {
46         __asm__ __volatile__ ("pref @%0" : : "r" (addr) : "memory");
47 }
48
49 /**
50  * sq_flush_range - Flush (prefetch) a specific SQ range
51  * @start: the store queue address to start flushing from
52  * @len: the length to flush
53  *
54  * Flushes the store queue cache from @start to @start + @len in a
55  * linear fashion.
56  */
57 void sq_flush_range(unsigned long start, unsigned int len)
58 {
59         volatile unsigned long *sq = (unsigned long *)start;
60         unsigned long dummy;
61
62         /* Flush the queues */
63         for (len >>= 5; len--; sq += 8)
64                 sq_flush((void *)sq);
65
66         /* Wait for completion */
67         dummy = ctrl_inl(P4SEG_STORE_QUE);
68
69         ctrl_outl(0, P4SEG_STORE_QUE + 0);
70         ctrl_outl(0, P4SEG_STORE_QUE + 8);
71 }
72
73 static struct sq_mapping *__sq_alloc_mapping(unsigned long virt, unsigned long phys, unsigned long size, const char *name)
74 {
75         struct sq_mapping *map;
76
77         if (virt + size > SQ_ADDRMAX)
78                 return ERR_PTR(-ENOSPC);
79
80         map = kmalloc(sizeof(struct sq_mapping), GFP_KERNEL);
81         if (!map)
82                 return ERR_PTR(-ENOMEM);
83
84         INIT_LIST_HEAD(&map->list);
85
86         map->sq_addr    = virt;
87         map->addr       = phys;
88         map->size       = size + 1;
89         map->name       = name;
90
91         list_add(&map->list, &sq_mapping_list);
92
93         return map;
94 }
95
96 static unsigned long __sq_get_next_addr(void)
97 {
98         if (!list_empty(&sq_mapping_list)) {
99                 struct list_head *pos, *tmp;
100
101                 /*
102                  * Read one off the list head, as it will have the highest
103                  * mapped allocation. Set the next one up right above it.
104                  *
105                  * This is somewhat sub-optimal, as we don't look at
106                  * gaps between allocations or anything lower then the
107                  * highest-level allocation.
108                  *
109                  * However, in the interest of performance and the general
110                  * lack of desire to do constant list rebalancing, we don't
111                  * worry about it.
112                  */
113                 list_for_each_safe(pos, tmp, &sq_mapping_list) {
114                         struct sq_mapping *entry;
115
116                         entry = list_entry(pos, typeof(*entry), list);
117
118                         return entry->sq_addr + entry->size;
119                 }
120         }
121
122         return P4SEG_STORE_QUE;
123 }
124
125 /**
126  * __sq_remap - Perform a translation from the SQ to a phys addr
127  * @map: sq mapping containing phys and store queue addresses.
128  *
129  * Maps the store queue address specified in the mapping to the physical
130  * address specified in the mapping.
131  */
132 static struct sq_mapping *__sq_remap(struct sq_mapping *map)
133 {
134         unsigned long flags, pteh, ptel;
135         struct vm_struct *vma;
136         pgprot_t pgprot;
137
138         /*
139          * Without an MMU (or with it turned off), this is much more
140          * straightforward, as we can just load up each queue's QACR with
141          * the physical address appropriately masked.
142          */
143
144         ctrl_outl(((map->addr >> 26) << 2) & 0x1c, SQ_QACR0);
145         ctrl_outl(((map->addr >> 26) << 2) & 0x1c, SQ_QACR1);
146
147 #ifdef CONFIG_MMU
148         /*
149          * With an MMU on the other hand, things are slightly more involved.
150          * Namely, we have to have a direct mapping between the SQ addr and
151          * the associated physical address in the UTLB by way of setting up
152          * a virt<->phys translation by hand. We do this by simply specifying
153          * the SQ addr in UTLB.VPN and the associated physical address in
154          * UTLB.PPN.
155          *
156          * Notably, even though this is a special case translation, and some
157          * of the configuration bits are meaningless, we're still required
158          * to have a valid ASID context in PTEH.
159          *
160          * We could also probably get by without explicitly setting PTEA, but
161          * we do it here just for good measure.
162          */
163         spin_lock_irqsave(&sq_mapping_lock, flags);
164
165         pteh = map->sq_addr;
166         ctrl_outl((pteh & MMU_VPN_MASK) | get_asid(), MMU_PTEH);
167
168         ptel = map->addr & PAGE_MASK;
169
170         if (cpu_data->flags & CPU_HAS_PTEA)
171                 ctrl_outl(((ptel >> 28) & 0xe) | (ptel & 0x1), MMU_PTEA);
172
173         pgprot = pgprot_noncached(PAGE_KERNEL);
174
175         ptel &= _PAGE_FLAGS_HARDWARE_MASK;
176         ptel |= pgprot_val(pgprot);
177         ctrl_outl(ptel, MMU_PTEL);
178
179         __asm__ __volatile__ ("ldtlb" : : : "memory");
180
181         spin_unlock_irqrestore(&sq_mapping_lock, flags);
182
183         /*
184          * Next, we need to map ourselves in the kernel page table, so that
185          * future accesses after a TLB flush will be handled when we take a
186          * page fault.
187          *
188          * Theoretically we could just do this directly and not worry about
189          * setting up the translation by hand ahead of time, but for the
190          * cases where we want a one-shot SQ mapping followed by a quick
191          * writeout before we hit the TLB flush, we do it anyways. This way
192          * we at least save ourselves the initial page fault overhead.
193          */
194         vma = __get_vm_area(map->size, VM_ALLOC, map->sq_addr, SQ_ADDRMAX);
195         if (!vma)
196                 return ERR_PTR(-ENOMEM);
197
198         vma->phys_addr = map->addr;
199
200         if (remap_area_pages((unsigned long)vma->addr, vma->phys_addr,
201                              map->size, pgprot_val(pgprot))) {
202                 vunmap(vma->addr);
203                 return NULL;
204         }
205 #endif /* CONFIG_MMU */
206
207         return map;
208 }
209
210 /**
211  * sq_remap - Map a physical address through the Store Queues
212  * @phys: Physical address of mapping.
213  * @size: Length of mapping.
214  * @name: User invoking mapping.
215  *
216  * Remaps the physical address @phys through the next available store queue
217  * address of @size length. @name is logged at boot time as well as through
218  * the procfs interface.
219  *
220  * A pre-allocated and filled sq_mapping pointer is returned, and must be
221  * cleaned up with a call to sq_unmap() when the user is done with the
222  * mapping.
223  */
224 struct sq_mapping *sq_remap(unsigned long phys, unsigned int size, const char *name)
225 {
226         struct sq_mapping *map;
227         unsigned long virt, end;
228         unsigned int psz;
229
230         /* Don't allow wraparound or zero size */
231         end = phys + size - 1;
232         if (!size || end < phys)
233                 return NULL;
234         /* Don't allow anyone to remap normal memory.. */
235         if (phys < virt_to_phys(high_memory))
236                 return NULL;
237
238         phys &= PAGE_MASK;
239
240         size  = PAGE_ALIGN(end + 1) - phys;
241         virt  = __sq_get_next_addr();
242         psz   = (size + (PAGE_SIZE - 1)) / PAGE_SIZE;
243         map   = __sq_alloc_mapping(virt, phys, size, name);
244
245         printk("sqremap: %15s  [%4d page%s]  va 0x%08lx   pa 0x%08lx\n",
246                map->name ? map->name : "???",
247                psz, psz == 1 ? " " : "s",
248                map->sq_addr, map->addr);
249
250         return __sq_remap(map);
251 }
252
253 /**
254  * sq_unmap - Unmap a Store Queue allocation
255  * @map: Pre-allocated Store Queue mapping.
256  *
257  * Unmaps the store queue allocation @map that was previously created by
258  * sq_remap(). Also frees up the pte that was previously inserted into
259  * the kernel page table and discards the UTLB translation.
260  */
261 void sq_unmap(struct sq_mapping *map)
262 {
263         if (map->sq_addr > (unsigned long)high_memory)
264                 vfree((void *)(map->sq_addr & PAGE_MASK));
265
266         list_del(&map->list);
267         kfree(map);
268 }
269
270 /**
271  * sq_clear - Clear a store queue range
272  * @addr: Address to start clearing from.
273  * @len: Length to clear.
274  *
275  * A quick zero-fill implementation for clearing out memory that has been
276  * remapped through the store queues.
277  */
278 void sq_clear(unsigned long addr, unsigned int len)
279 {
280         int i;
281
282         /* Clear out both queues linearly */
283         for (i = 0; i < 8; i++) {
284                 ctrl_outl(0, addr + i + 0);
285                 ctrl_outl(0, addr + i + 8);
286         }
287
288         sq_flush_range(addr, len);
289 }
290
291 /**
292  * sq_vma_unmap - Unmap a VMA range
293  * @area: VMA containing range.
294  * @addr: Start of range.
295  * @len: Length of range.
296  *
297  * Searches the sq_mapping_list for a mapping matching the sq addr @addr,
298  * and subsequently frees up the entry. Further cleanup is done by generic
299  * code.
300  */
301 static void sq_vma_unmap(struct vm_area_struct *area,
302                          unsigned long addr, size_t len)
303 {
304         struct list_head *pos, *tmp;
305
306         list_for_each_safe(pos, tmp, &sq_mapping_list) {
307                 struct sq_mapping *entry;
308
309                 entry = list_entry(pos, typeof(*entry), list);
310
311                 if (entry->sq_addr == addr) {
312                         /*
313                          * We could probably get away without doing the tlb flush
314                          * here, as generic code should take care of most of this
315                          * when unmapping the rest of the VMA range for us. Leave
316                          * it in for added sanity for the time being..
317                          */
318                         __flush_tlb_page(get_asid(), entry->sq_addr & PAGE_MASK);
319
320                         list_del(&entry->list);
321                         kfree(entry);
322
323                         return;
324                 }
325         }
326 }
327
328 /**
329  * sq_vma_sync - Sync a VMA range
330  * @area: VMA containing range.
331  * @start: Start of range.
332  * @len: Length of range.
333  * @flags: Additional flags.
334  *
335  * Synchronizes an sq mapped range by flushing the store queue cache for
336  * the duration of the mapping.
337  *
338  * Used internally for user mappings, which must use msync() to prefetch
339  * the store queue cache.
340  */
341 static int sq_vma_sync(struct vm_area_struct *area,
342                        unsigned long start, size_t len, unsigned int flags)
343 {
344         sq_flush_range(start, len);
345
346         return 0;
347 }
348
349 static struct vm_operations_struct sq_vma_ops = {
350         .unmap  = sq_vma_unmap,
351         .sync   = sq_vma_sync,
352 };
353
354 /**
355  * sq_mmap - mmap() for /dev/cpu/sq
356  * @file: unused.
357  * @vma: VMA to remap.
358  *
359  * Remap the specified vma @vma through the store queues, and setup associated
360  * information for the new mapping. Also build up the page tables for the new
361  * area.
362  */
363 static int sq_mmap(struct file *file, struct vm_area_struct *vma)
364 {
365         unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
366         unsigned long size = vma->vm_end - vma->vm_start;
367         struct sq_mapping *map;
368
369         /*
370          * We're not interested in any arbitrary virtual address that has
371          * been stuck in the VMA, as we already know what addresses we
372          * want. Save off the size, and reposition the VMA to begin at
373          * the next available sq address.
374          */
375         vma->vm_start = __sq_get_next_addr();
376         vma->vm_end   = vma->vm_start + size;
377
378         vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
379
380         vma->vm_flags |= VM_IO | VM_RESERVED;
381
382         map = __sq_alloc_mapping(vma->vm_start, offset, size, "Userspace");
383
384         if (io_remap_pfn_range(vma, map->sq_addr, map->addr >> PAGE_SHIFT,
385                                 size, vma->vm_page_prot))
386                 return -EAGAIN;
387
388         vma->vm_ops = &sq_vma_ops;
389
390         return 0;
391 }
392
393 #ifdef CONFIG_PROC_FS
394 static int sq_mapping_read_proc(char *buf, char **start, off_t off,
395                                 int len, int *eof, void *data)
396 {
397         struct list_head *pos;
398         char *p = buf;
399
400         list_for_each_prev(pos, &sq_mapping_list) {
401                 struct sq_mapping *entry;
402
403                 entry = list_entry(pos, typeof(*entry), list);
404
405                 p += sprintf(p, "%08lx-%08lx [%08lx]: %s\n", entry->sq_addr,
406                              entry->sq_addr + entry->size - 1, entry->addr,
407                              entry->name);
408         }
409
410         return p - buf;
411 }
412 #endif
413
414 static struct file_operations sq_fops = {
415         .owner          = THIS_MODULE,
416         .mmap           = sq_mmap,
417 };
418
419 static struct miscdevice sq_dev = {
420         .minor          = STORE_QUEUE_MINOR,
421         .name           = "sq",
422         .fops           = &sq_fops,
423 };
424
425 static int __init sq_api_init(void)
426 {
427         int ret;
428         printk(KERN_NOTICE "sq: Registering store queue API.\n");
429
430         create_proc_read_entry("sq_mapping", 0, 0, sq_mapping_read_proc, 0);
431
432         ret = misc_register(&sq_dev);
433         if (ret)
434                 remove_proc_entry("sq_mapping", NULL);
435
436         return ret;
437 }
438
439 static void __exit sq_api_exit(void)
440 {
441         misc_deregister(&sq_dev);
442         remove_proc_entry("sq_mapping", NULL);
443 }
444
445 module_init(sq_api_init);
446 module_exit(sq_api_exit);
447
448 MODULE_AUTHOR("Paul Mundt <lethal@linux-sh.org>, M. R. Brown <mrbrown@0xd6.org>");
449 MODULE_DESCRIPTION("Simple API for SH-4 integrated Store Queues");
450 MODULE_LICENSE("GPL");
451 MODULE_ALIAS_MISCDEV(STORE_QUEUE_MINOR);
452
453 EXPORT_SYMBOL(sq_remap);
454 EXPORT_SYMBOL(sq_unmap);
455 EXPORT_SYMBOL(sq_clear);
456 EXPORT_SYMBOL(sq_flush);
457 EXPORT_SYMBOL(sq_flush_range);
458