2 * arch/sh/kernel/cpu/sq.c
4 * General management API for SH-4 integrated Store Queues
6 * Copyright (C) 2001, 2002, 2003, 2004 Paul Mundt
7 * Copyright (C) 2001, 2002 M. R. Brown
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.
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
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>
31 #include <asm/mmu_context.h>
32 #include <asm/cpu/sq.h>
34 static LIST_HEAD(sq_mapping_list);
35 static spinlock_t sq_mapping_lock = SPIN_LOCK_UNLOCKED;
37 extern struct vm_struct *__get_vm_area(unsigned long size, unsigned long flags, unsigned long start, unsigned long end);
40 * sq_flush - Flush (prefetch) the store queue cache
42 * @addr: the store queue address to flush
44 * Executes a prefetch instruction on the specified store queue cache,
45 * so that the cached data is written to physical memory.
47 inline void sq_flush(void *addr)
49 __asm__ __volatile__ ("pref @%0" : : "r" (addr) : "memory");
53 * sq_flush_range - Flush (prefetch) a specific SQ range
55 * @start: the store queue address to start flushing from
56 * @len: the length to flush
58 * Flushes the store queue cache from @start to @start + @len in a
61 void sq_flush_range(unsigned long start, unsigned int len)
63 volatile unsigned long *sq = (unsigned long *)start;
66 /* Flush the queues */
67 for (len >>= 5; len--; sq += 8)
70 /* Wait for completion */
71 dummy = ctrl_inl(P4SEG_STORE_QUE);
73 ctrl_outl(0, P4SEG_STORE_QUE + 0);
74 ctrl_outl(0, P4SEG_STORE_QUE + 8);
77 static struct sq_mapping *__sq_alloc_mapping(unsigned long virt, unsigned long phys, unsigned long size, const char *name)
79 struct sq_mapping *map;
81 if (virt + size > SQ_ADDRMAX)
82 return ERR_PTR(-ENOSPC);
84 map = kmalloc(sizeof(struct sq_mapping), GFP_KERNEL);
86 return ERR_PTR(-ENOMEM);
88 INIT_LIST_HEAD(&map->list);
95 list_add(&map->list, &sq_mapping_list);
100 static unsigned long __sq_get_next_addr(void)
102 if (!list_empty(&sq_mapping_list)) {
103 struct list_head *pos, *tmp;
106 * Read one off the list head, as it will have the highest
107 * mapped allocation. Set the next one up right above it.
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.
113 * However, in the interest of performance and the general
114 * lack of desire to do constant list rebalancing, we don't
117 list_for_each_safe(pos, tmp, &sq_mapping_list) {
118 struct sq_mapping *entry;
120 entry = list_entry(pos, typeof(*entry), list);
122 return entry->sq_addr + entry->size;
126 return P4SEG_STORE_QUE;
130 * __sq_remap - Perform a translation from the SQ to a phys addr
132 * @phys: Physical address to map store queues too.
133 * @virt: Associated store queue address.
135 * Maps the store queue address @virt to the physical address @phys.
137 static struct sq_mapping *__sq_remap(struct sq_mapping *map)
139 unsigned long flags, pteh, ptel;
140 struct vm_struct *vma;
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.
149 ctrl_outl(((map->addr >> 26) << 2) & 0x1c, SQ_QACR0);
150 ctrl_outl(((map->addr >> 26) << 2) & 0x1c, SQ_QACR1);
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
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.
165 * We could also probably get by without explicitly setting PTEA, but
166 * we do it here just for good measure.
168 spin_lock_irqsave(&sq_mapping_lock, flags);
171 ctrl_outl((pteh & MMU_VPN_MASK) | get_asid(), MMU_PTEH);
173 ptel = map->addr & PAGE_MASK;
174 ctrl_outl(((ptel >> 28) & 0xe) | (ptel & 0x1), MMU_PTEA);
176 pgprot = pgprot_noncached(PAGE_KERNEL);
178 ptel &= _PAGE_FLAGS_HARDWARE_MASK;
179 ptel |= pgprot_val(pgprot);
180 ctrl_outl(ptel, MMU_PTEL);
182 __asm__ __volatile__ ("ldtlb" : : : "memory");
184 spin_unlock_irqrestore(&sq_mapping_lock, flags);
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
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.
197 vma = __get_vm_area(map->size, VM_ALLOC, map->sq_addr, SQ_ADDRMAX);
199 return ERR_PTR(-ENOMEM);
201 vma->phys_addr = map->addr;
203 if (remap_area_pages((unsigned long)vma->addr, vma->phys_addr,
204 map->size, pgprot_val(pgprot))) {
208 #endif /* CONFIG_MMU */
214 * sq_remap - Map a physical address through the Store Queues
216 * @phys: Physical address of mapping.
217 * @size: Length of mapping.
218 * @name: User invoking mapping.
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.
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
228 struct sq_mapping *sq_remap(unsigned long phys, unsigned int size, const char *name)
230 struct sq_mapping *map;
231 unsigned long virt, end;
234 /* Don't allow wraparound or zero size */
235 end = phys + size - 1;
236 if (!size || end < phys)
238 /* Don't allow anyone to remap normal memory.. */
239 if (phys < virt_to_phys(high_memory))
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);
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);
254 return __sq_remap(map);
258 * sq_unmap - Unmap a Store Queue allocation
260 * @map: Pre-allocated Store Queue mapping.
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.
266 void sq_unmap(struct sq_mapping *map)
268 if (map->sq_addr > (unsigned long)high_memory)
269 vfree((void *)(map->sq_addr & PAGE_MASK));
271 list_del(&map->list);
276 * sq_clear - Clear a store queue range
278 * @addr: Address to start clearing from.
279 * @len: Length to clear.
281 * A quick zero-fill implementation for clearing out memory that has been
282 * remapped through the store queues.
284 void sq_clear(unsigned long addr, unsigned int len)
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);
294 sq_flush_range(addr, len);
298 * sq_vma_unmap - Unmap a VMA range
300 * @area: VMA containing range.
301 * @addr: Start of range.
302 * @len: Length of range.
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
308 static void sq_vma_unmap(struct vm_area_struct *area,
309 unsigned long addr, size_t len)
311 struct list_head *pos, *tmp;
313 list_for_each_safe(pos, tmp, &sq_mapping_list) {
314 struct sq_mapping *entry;
316 entry = list_entry(pos, typeof(*entry), list);
318 if (entry->sq_addr == addr) {
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..
325 __flush_tlb_page(get_asid(), entry->sq_addr & PAGE_MASK);
327 list_del(&entry->list);
336 * sq_vma_sync - Sync a VMA range
338 * @area: VMA containing range.
339 * @start: Start of range.
340 * @len: Length of range.
341 * @flags: Additional flags.
343 * Synchronizes an sq mapped range by flushing the store queue cache for
344 * the duration of the mapping.
346 * Used internally for user mappings, which must use msync() to prefetch
347 * the store queue cache.
349 static int sq_vma_sync(struct vm_area_struct *area,
350 unsigned long start, size_t len, unsigned int flags)
352 sq_flush_range(start, len);
357 static struct vm_operations_struct sq_vma_ops = {
358 .unmap = sq_vma_unmap,
363 * sq_mmap - mmap() for /dev/cpu/sq
366 * @vma: VMA to remap.
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
372 static int sq_mmap(struct file *file, struct vm_area_struct *vma)
374 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
375 unsigned long size = vma->vm_end - vma->vm_start;
376 struct sq_mapping *map;
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.
384 vma->vm_start = __sq_get_next_addr();
385 vma->vm_end = vma->vm_start + size;
387 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
389 vma->vm_flags |= VM_IO | VM_RESERVED;
391 map = __sq_alloc_mapping(vma->vm_start, offset, size, "Userspace");
393 if (io_remap_page_range(vma, map->sq_addr, map->addr,
394 size, vma->vm_page_prot))
397 vma->vm_ops = &sq_vma_ops;
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)
406 struct list_head *pos;
409 list_for_each_prev(pos, &sq_mapping_list) {
410 struct sq_mapping *entry;
412 entry = list_entry(pos, typeof(*entry), list);
414 p += sprintf(p, "%08lx-%08lx [%08lx]: %s\n", entry->sq_addr,
415 entry->sq_addr + entry->size - 1, entry->addr,
423 static struct file_operations sq_fops = {
424 .owner = THIS_MODULE,
428 static struct miscdevice sq_dev = {
429 .minor = STORE_QUEUE_MINOR,
431 .devfs_name = "cpu/sq",
435 static int __init sq_api_init(void)
437 printk(KERN_NOTICE "sq: Registering store queue API.\n");
439 #ifdef CONFIG_PROC_FS
440 create_proc_read_entry("sq_mapping", 0, 0, sq_mapping_read_proc, 0);
443 return misc_register(&sq_dev);
446 static void __exit sq_api_exit(void)
448 misc_deregister(&sq_dev);
451 module_init(sq_api_init);
452 module_exit(sq_api_exit);
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);
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);