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