2 * arch/sh/kernel/cpu/sh4/sq.c
4 * General management API for SH-4 integrated Store Queues
6 * Copyright (C) 2001 - 2006 Paul Mundt
7 * Copyright (C) 2001, 2002 M. R. Brown
9 * This file is subject to the terms and conditions of the GNU General Public
10 * License. See the file "COPYING" in the main directory of this archive
13 #include <linux/init.h>
14 #include <linux/cpu.h>
15 #include <linux/bitmap.h>
16 #include <linux/sysdev.h>
17 #include <linux/kernel.h>
18 #include <linux/module.h>
19 #include <linux/slab.h>
20 #include <linux/vmalloc.h>
24 #include <asm/cacheflush.h>
25 #include <asm/cpu/sq.h>
32 unsigned long sq_addr;
36 struct sq_mapping *next;
39 static struct sq_mapping *sq_mapping_list;
40 static DEFINE_SPINLOCK(sq_mapping_lock);
41 static kmem_cache_t *sq_cache;
42 static unsigned long *sq_bitmap;
44 #define store_queue_barrier() \
46 (void)ctrl_inl(P4SEG_STORE_QUE); \
47 ctrl_outl(0, P4SEG_STORE_QUE + 0); \
48 ctrl_outl(0, P4SEG_STORE_QUE + 8); \
52 * sq_flush_range - Flush (prefetch) a specific SQ range
53 * @start: the store queue address to start flushing from
54 * @len: the length to flush
56 * Flushes the store queue cache from @start to @start + @len in a
59 void sq_flush_range(unsigned long start, unsigned int len)
61 volatile unsigned long *sq = (unsigned long *)start;
63 /* Flush the queues */
64 for (len >>= 5; len--; sq += 8)
65 prefetchw((void *)sq);
67 /* Wait for completion */
68 store_queue_barrier();
71 static inline void sq_mapping_list_add(struct sq_mapping *map)
73 struct sq_mapping **p, *tmp;
75 spin_lock_irq(&sq_mapping_lock);
78 while ((tmp = *p) != NULL)
84 spin_unlock_irq(&sq_mapping_lock);
87 static inline void sq_mapping_list_del(struct sq_mapping *map)
89 struct sq_mapping **p, *tmp;
91 spin_lock_irq(&sq_mapping_lock);
93 for (p = &sq_mapping_list; (tmp = *p); p = &tmp->next)
99 spin_unlock_irq(&sq_mapping_lock);
102 static int __sq_remap(struct sq_mapping *map, unsigned long flags)
104 #if defined(CONFIG_MMU)
105 struct vm_struct *vma;
107 vma = __get_vm_area(map->size, VM_ALLOC, map->sq_addr, SQ_ADDRMAX);
111 vma->phys_addr = map->addr;
113 if (remap_area_pages((unsigned long)vma->addr, vma->phys_addr,
120 * Without an MMU (or with it turned off), this is much more
121 * straightforward, as we can just load up each queue's QACR with
122 * the physical address appropriately masked.
124 ctrl_outl(((map->addr >> 26) << 2) & 0x1c, SQ_QACR0);
125 ctrl_outl(((map->addr >> 26) << 2) & 0x1c, SQ_QACR1);
132 * sq_remap - Map a physical address through the Store Queues
133 * @phys: Physical address of mapping.
134 * @size: Length of mapping.
135 * @name: User invoking mapping.
136 * @flags: Protection flags.
138 * Remaps the physical address @phys through the next available store queue
139 * address of @size length. @name is logged at boot time as well as through
140 * the sysfs interface.
142 unsigned long sq_remap(unsigned long phys, unsigned int size,
143 const char *name, unsigned long flags)
145 struct sq_mapping *map;
150 /* Don't allow wraparound or zero size */
151 end = phys + size - 1;
152 if (unlikely(!size || end < phys))
154 /* Don't allow anyone to remap normal memory.. */
155 if (unlikely(phys < virt_to_phys(high_memory)))
159 size = PAGE_ALIGN(end + 1) - phys;
161 map = kmem_cache_alloc(sq_cache, GFP_KERNEL);
169 page = bitmap_find_free_region(sq_bitmap, 0x04000000,
170 get_order(map->size));
171 if (unlikely(page < 0)) {
176 map->sq_addr = P4SEG_STORE_QUE + (page << PAGE_SHIFT);
178 ret = __sq_remap(map, flags);
179 if (unlikely(ret != 0))
182 psz = (size + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
183 pr_info("sqremap: %15s [%4d page%s] va 0x%08lx pa 0x%08lx\n",
184 likely(map->name) ? map->name : "???",
185 psz, psz == 1 ? " " : "s",
186 map->sq_addr, map->addr);
188 sq_mapping_list_add(map);
193 kmem_cache_free(sq_cache, map);
198 * sq_unmap - Unmap a Store Queue allocation
199 * @map: Pre-allocated Store Queue mapping.
201 * Unmaps the store queue allocation @map that was previously created by
202 * sq_remap(). Also frees up the pte that was previously inserted into
203 * the kernel page table and discards the UTLB translation.
205 void sq_unmap(unsigned long vaddr)
207 struct sq_mapping **p, *map;
208 struct vm_struct *vma;
211 for (p = &sq_mapping_list; (map = *p); p = &map->next)
212 if (map->sq_addr == vaddr)
215 if (unlikely(!map)) {
216 printk("%s: bad store queue address 0x%08lx\n",
217 __FUNCTION__, vaddr);
221 page = (map->sq_addr - P4SEG_STORE_QUE) >> PAGE_SHIFT;
222 bitmap_release_region(sq_bitmap, page, get_order(map->size));
225 vma = remove_vm_area((void *)(map->sq_addr & PAGE_MASK));
227 printk(KERN_ERR "%s: bad address 0x%08lx\n",
228 __FUNCTION__, map->sq_addr);
233 sq_mapping_list_del(map);
235 kmem_cache_free(sq_cache, map);
239 * Needlessly complex sysfs interface. Unfortunately it doesn't seem like
240 * there is any other easy way to add things on a per-cpu basis without
241 * putting the directory entries somewhere stupid and having to create
242 * links in sysfs by hand back in to the per-cpu directories.
244 * Some day we may want to have an additional abstraction per store
245 * queue, but considering the kobject hell we already have to deal with,
246 * it's simply not worth the trouble.
248 static struct kobject *sq_kobject[NR_CPUS];
250 struct sq_sysfs_attr {
251 struct attribute attr;
252 ssize_t (*show)(char *buf);
253 ssize_t (*store)(const char *buf, size_t count);
256 #define to_sq_sysfs_attr(attr) container_of(attr, struct sq_sysfs_attr, attr)
258 static ssize_t sq_sysfs_show(struct kobject *kobj, struct attribute *attr,
261 struct sq_sysfs_attr *sattr = to_sq_sysfs_attr(attr);
263 if (likely(sattr->show))
264 return sattr->show(buf);
269 static ssize_t sq_sysfs_store(struct kobject *kobj, struct attribute *attr,
270 const char *buf, size_t count)
272 struct sq_sysfs_attr *sattr = to_sq_sysfs_attr(attr);
274 if (likely(sattr->store))
275 return sattr->store(buf, count);
280 static ssize_t mapping_show(char *buf)
282 struct sq_mapping **list, *entry;
285 for (list = &sq_mapping_list; (entry = *list); list = &entry->next)
286 p += sprintf(p, "%08lx-%08lx [%08lx]: %s\n",
287 entry->sq_addr, entry->sq_addr + entry->size,
288 entry->addr, entry->name);
293 static ssize_t mapping_store(const char *buf, size_t count)
295 unsigned long base = 0, len = 0;
297 sscanf(buf, "%lx %lx", &base, &len);
302 int ret = sq_remap(base, len, "Userspace",
303 pgprot_val(PAGE_SHARED));
312 static struct sq_sysfs_attr mapping_attr =
313 __ATTR(mapping, 0644, mapping_show, mapping_store);
315 static struct attribute *sq_sysfs_attrs[] = {
320 static struct sysfs_ops sq_sysfs_ops = {
321 .show = sq_sysfs_show,
322 .store = sq_sysfs_store,
325 static struct kobj_type ktype_percpu_entry = {
326 .sysfs_ops = &sq_sysfs_ops,
327 .default_attrs = sq_sysfs_attrs,
330 static int __devinit sq_sysdev_add(struct sys_device *sysdev)
332 unsigned int cpu = sysdev->id;
333 struct kobject *kobj;
335 sq_kobject[cpu] = kzalloc(sizeof(struct kobject), GFP_KERNEL);
336 if (unlikely(!sq_kobject[cpu]))
339 kobj = sq_kobject[cpu];
340 kobj->parent = &sysdev->kobj;
341 kobject_set_name(kobj, "%s", "sq");
342 kobj->ktype = &ktype_percpu_entry;
344 return kobject_register(kobj);
347 static int __devexit sq_sysdev_remove(struct sys_device *sysdev)
349 unsigned int cpu = sysdev->id;
350 struct kobject *kobj = sq_kobject[cpu];
352 kobject_unregister(kobj);
356 static struct sysdev_driver sq_sysdev_driver = {
357 .add = sq_sysdev_add,
358 .remove = __devexit_p(sq_sysdev_remove),
361 static int __init sq_api_init(void)
363 unsigned int nr_pages = 0x04000000 >> PAGE_SHIFT;
364 unsigned int size = (nr_pages + (BITS_PER_LONG - 1)) / BITS_PER_LONG;
367 printk(KERN_NOTICE "sq: Registering store queue API.\n");
369 sq_cache = kmem_cache_create("store_queue_cache",
370 sizeof(struct sq_mapping), 0, 0,
372 if (unlikely(!sq_cache))
375 sq_bitmap = kzalloc(size, GFP_KERNEL);
376 if (unlikely(!sq_bitmap))
379 ret = sysdev_driver_register(&cpu_sysdev_class, &sq_sysdev_driver);
380 if (unlikely(ret != 0))
387 kmem_cache_destroy(sq_cache);
392 static void __exit sq_api_exit(void)
394 sysdev_driver_unregister(&cpu_sysdev_class, &sq_sysdev_driver);
396 kmem_cache_destroy(sq_cache);
399 module_init(sq_api_init);
400 module_exit(sq_api_exit);
402 MODULE_AUTHOR("Paul Mundt <lethal@linux-sh.org>, M. R. Brown <mrbrown@0xd6.org>");
403 MODULE_DESCRIPTION("Simple API for SH-4 integrated Store Queues");
404 MODULE_LICENSE("GPL");
406 EXPORT_SYMBOL(sq_remap);
407 EXPORT_SYMBOL(sq_unmap);
408 EXPORT_SYMBOL(sq_flush_range);