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