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 DEFINE_SPINLOCK(sq_mapping_lock
);
38 * sq_flush - Flush (prefetch) the store queue cache
39 * @addr: the store queue address to flush
41 * Executes a prefetch instruction on the specified store queue cache,
42 * so that the cached data is written to physical memory.
44 inline void sq_flush(void *addr
)
46 __asm__
__volatile__ ("pref @%0" : : "r" (addr
) : "memory");
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
54 * Flushes the store queue cache from @start to @start + @len in a
57 void sq_flush_range(unsigned long start
, unsigned int len
)
59 volatile unsigned long *sq
= (unsigned long *)start
;
62 /* Flush the queues */
63 for (len
>>= 5; len
--; sq
+= 8)
66 /* Wait for completion */
67 dummy
= ctrl_inl(P4SEG_STORE_QUE
);
69 ctrl_outl(0, P4SEG_STORE_QUE
+ 0);
70 ctrl_outl(0, P4SEG_STORE_QUE
+ 8);
73 static struct sq_mapping
*__sq_alloc_mapping(unsigned long virt
, unsigned long phys
, unsigned long size
, const char *name
)
75 struct sq_mapping
*map
;
77 if (virt
+ size
> SQ_ADDRMAX
)
78 return ERR_PTR(-ENOSPC
);
80 map
= kmalloc(sizeof(struct sq_mapping
), GFP_KERNEL
);
82 return ERR_PTR(-ENOMEM
);
84 INIT_LIST_HEAD(&map
->list
);
91 list_add(&map
->list
, &sq_mapping_list
);
96 static unsigned long __sq_get_next_addr(void)
98 if (!list_empty(&sq_mapping_list
)) {
99 struct list_head
*pos
, *tmp
;
102 * Read one off the list head, as it will have the highest
103 * mapped allocation. Set the next one up right above it.
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.
109 * However, in the interest of performance and the general
110 * lack of desire to do constant list rebalancing, we don't
113 list_for_each_safe(pos
, tmp
, &sq_mapping_list
) {
114 struct sq_mapping
*entry
;
116 entry
= list_entry(pos
, typeof(*entry
), list
);
118 return entry
->sq_addr
+ entry
->size
;
122 return P4SEG_STORE_QUE
;
126 * __sq_remap - Perform a translation from the SQ to a phys addr
127 * @map: sq mapping containing phys and store queue addresses.
129 * Maps the store queue address specified in the mapping to the physical
130 * address specified in the mapping.
132 static struct sq_mapping
*__sq_remap(struct sq_mapping
*map
)
134 unsigned long flags
, pteh
, ptel
;
135 struct vm_struct
*vma
;
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.
144 ctrl_outl(((map
->addr
>> 26) << 2) & 0x1c, SQ_QACR0
);
145 ctrl_outl(((map
->addr
>> 26) << 2) & 0x1c, SQ_QACR1
);
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
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.
160 * We could also probably get by without explicitly setting PTEA, but
161 * we do it here just for good measure.
163 spin_lock_irqsave(&sq_mapping_lock
, flags
);
166 ctrl_outl((pteh
& MMU_VPN_MASK
) | get_asid(), MMU_PTEH
);
168 ptel
= map
->addr
& PAGE_MASK
;
169 ctrl_outl(((ptel
>> 28) & 0xe) | (ptel
& 0x1), MMU_PTEA
);
171 pgprot
= pgprot_noncached(PAGE_KERNEL
);
173 ptel
&= _PAGE_FLAGS_HARDWARE_MASK
;
174 ptel
|= pgprot_val(pgprot
);
175 ctrl_outl(ptel
, MMU_PTEL
);
177 __asm__
__volatile__ ("ldtlb" : : : "memory");
179 spin_unlock_irqrestore(&sq_mapping_lock
, flags
);
182 * Next, we need to map ourselves in the kernel page table, so that
183 * future accesses after a TLB flush will be handled when we take a
186 * Theoretically we could just do this directly and not worry about
187 * setting up the translation by hand ahead of time, but for the
188 * cases where we want a one-shot SQ mapping followed by a quick
189 * writeout before we hit the TLB flush, we do it anyways. This way
190 * we at least save ourselves the initial page fault overhead.
192 vma
= __get_vm_area(map
->size
, VM_ALLOC
, map
->sq_addr
, SQ_ADDRMAX
);
194 return ERR_PTR(-ENOMEM
);
196 vma
->phys_addr
= map
->addr
;
198 if (remap_area_pages((unsigned long)vma
->addr
, vma
->phys_addr
,
199 map
->size
, pgprot_val(pgprot
))) {
203 #endif /* CONFIG_MMU */
209 * sq_remap - Map a physical address through the Store Queues
210 * @phys: Physical address of mapping.
211 * @size: Length of mapping.
212 * @name: User invoking mapping.
214 * Remaps the physical address @phys through the next available store queue
215 * address of @size length. @name is logged at boot time as well as through
216 * the procfs interface.
218 * A pre-allocated and filled sq_mapping pointer is returned, and must be
219 * cleaned up with a call to sq_unmap() when the user is done with the
222 struct sq_mapping
*sq_remap(unsigned long phys
, unsigned int size
, const char *name
)
224 struct sq_mapping
*map
;
225 unsigned long virt
, end
;
228 /* Don't allow wraparound or zero size */
229 end
= phys
+ size
- 1;
230 if (!size
|| end
< phys
)
232 /* Don't allow anyone to remap normal memory.. */
233 if (phys
< virt_to_phys(high_memory
))
238 size
= PAGE_ALIGN(end
+ 1) - phys
;
239 virt
= __sq_get_next_addr();
240 psz
= (size
+ (PAGE_SIZE
- 1)) / PAGE_SIZE
;
241 map
= __sq_alloc_mapping(virt
, phys
, size
, name
);
243 printk("sqremap: %15s [%4d page%s] va 0x%08lx pa 0x%08lx\n",
244 map
->name
? map
->name
: "???",
245 psz
, psz
== 1 ? " " : "s",
246 map
->sq_addr
, map
->addr
);
248 return __sq_remap(map
);
252 * sq_unmap - Unmap a Store Queue allocation
253 * @map: Pre-allocated Store Queue mapping.
255 * Unmaps the store queue allocation @map that was previously created by
256 * sq_remap(). Also frees up the pte that was previously inserted into
257 * the kernel page table and discards the UTLB translation.
259 void sq_unmap(struct sq_mapping
*map
)
261 if (map
->sq_addr
> (unsigned long)high_memory
)
262 vfree((void *)(map
->sq_addr
& PAGE_MASK
));
264 list_del(&map
->list
);
269 * sq_clear - Clear a store queue range
270 * @addr: Address to start clearing from.
271 * @len: Length to clear.
273 * A quick zero-fill implementation for clearing out memory that has been
274 * remapped through the store queues.
276 void sq_clear(unsigned long addr
, unsigned int len
)
280 /* Clear out both queues linearly */
281 for (i
= 0; i
< 8; i
++) {
282 ctrl_outl(0, addr
+ i
+ 0);
283 ctrl_outl(0, addr
+ i
+ 8);
286 sq_flush_range(addr
, len
);
290 * sq_vma_unmap - Unmap a VMA range
291 * @area: VMA containing range.
292 * @addr: Start of range.
293 * @len: Length of range.
295 * Searches the sq_mapping_list for a mapping matching the sq addr @addr,
296 * and subsequently frees up the entry. Further cleanup is done by generic
299 static void sq_vma_unmap(struct vm_area_struct
*area
,
300 unsigned long addr
, size_t len
)
302 struct list_head
*pos
, *tmp
;
304 list_for_each_safe(pos
, tmp
, &sq_mapping_list
) {
305 struct sq_mapping
*entry
;
307 entry
= list_entry(pos
, typeof(*entry
), list
);
309 if (entry
->sq_addr
== addr
) {
311 * We could probably get away without doing the tlb flush
312 * here, as generic code should take care of most of this
313 * when unmapping the rest of the VMA range for us. Leave
314 * it in for added sanity for the time being..
316 __flush_tlb_page(get_asid(), entry
->sq_addr
& PAGE_MASK
);
318 list_del(&entry
->list
);
327 * sq_vma_sync - Sync a VMA range
328 * @area: VMA containing range.
329 * @start: Start of range.
330 * @len: Length of range.
331 * @flags: Additional flags.
333 * Synchronizes an sq mapped range by flushing the store queue cache for
334 * the duration of the mapping.
336 * Used internally for user mappings, which must use msync() to prefetch
337 * the store queue cache.
339 static int sq_vma_sync(struct vm_area_struct
*area
,
340 unsigned long start
, size_t len
, unsigned int flags
)
342 sq_flush_range(start
, len
);
347 static struct vm_operations_struct sq_vma_ops
= {
348 .unmap
= sq_vma_unmap
,
353 * sq_mmap - mmap() for /dev/cpu/sq
355 * @vma: VMA to remap.
357 * Remap the specified vma @vma through the store queues, and setup associated
358 * information for the new mapping. Also build up the page tables for the new
361 static int sq_mmap(struct file
*file
, struct vm_area_struct
*vma
)
363 unsigned long offset
= vma
->vm_pgoff
<< PAGE_SHIFT
;
364 unsigned long size
= vma
->vm_end
- vma
->vm_start
;
365 struct sq_mapping
*map
;
368 * We're not interested in any arbitrary virtual address that has
369 * been stuck in the VMA, as we already know what addresses we
370 * want. Save off the size, and reposition the VMA to begin at
371 * the next available sq address.
373 vma
->vm_start
= __sq_get_next_addr();
374 vma
->vm_end
= vma
->vm_start
+ size
;
376 vma
->vm_page_prot
= pgprot_noncached(vma
->vm_page_prot
);
378 vma
->vm_flags
|= VM_IO
| VM_RESERVED
;
380 map
= __sq_alloc_mapping(vma
->vm_start
, offset
, size
, "Userspace");
382 if (io_remap_pfn_range(vma
, map
->sq_addr
, map
->addr
>> PAGE_SHIFT
,
383 size
, vma
->vm_page_prot
))
386 vma
->vm_ops
= &sq_vma_ops
;
391 #ifdef CONFIG_PROC_FS
392 static int sq_mapping_read_proc(char *buf
, char **start
, off_t off
,
393 int len
, int *eof
, void *data
)
395 struct list_head
*pos
;
398 list_for_each_prev(pos
, &sq_mapping_list
) {
399 struct sq_mapping
*entry
;
401 entry
= list_entry(pos
, typeof(*entry
), list
);
403 p
+= sprintf(p
, "%08lx-%08lx [%08lx]: %s\n", entry
->sq_addr
,
404 entry
->sq_addr
+ entry
->size
- 1, entry
->addr
,
412 static struct file_operations sq_fops
= {
413 .owner
= THIS_MODULE
,
417 static struct miscdevice sq_dev
= {
418 .minor
= STORE_QUEUE_MINOR
,
420 .devfs_name
= "cpu/sq",
424 static int __init
sq_api_init(void)
426 printk(KERN_NOTICE
"sq: Registering store queue API.\n");
428 #ifdef CONFIG_PROC_FS
429 create_proc_read_entry("sq_mapping", 0, 0, sq_mapping_read_proc
, 0);
432 return misc_register(&sq_dev
);
435 static void __exit
sq_api_exit(void)
437 misc_deregister(&sq_dev
);
440 module_init(sq_api_init
);
441 module_exit(sq_api_exit
);
443 MODULE_AUTHOR("Paul Mundt <lethal@linux-sh.org>, M. R. Brown <mrbrown@0xd6.org>");
444 MODULE_DESCRIPTION("Simple API for SH-4 integrated Store Queues");
445 MODULE_LICENSE("GPL");
446 MODULE_ALIAS_MISCDEV(STORE_QUEUE_MINOR
);
448 EXPORT_SYMBOL(sq_remap
);
449 EXPORT_SYMBOL(sq_unmap
);
450 EXPORT_SYMBOL(sq_clear
);
451 EXPORT_SYMBOL(sq_flush
);
452 EXPORT_SYMBOL(sq_flush_range
);