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 struct kmem_cache *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();
70 EXPORT_SYMBOL(sq_flush_range);
72 static inline void sq_mapping_list_add(struct sq_mapping *map)
74 struct sq_mapping **p, *tmp;
76 spin_lock_irq(&sq_mapping_lock);
79 while ((tmp = *p) != NULL)
85 spin_unlock_irq(&sq_mapping_lock);
88 static inline void sq_mapping_list_del(struct sq_mapping *map)
90 struct sq_mapping **p, *tmp;
92 spin_lock_irq(&sq_mapping_lock);
94 for (p = &sq_mapping_list; (tmp = *p); p = &tmp->next)
100 spin_unlock_irq(&sq_mapping_lock);
103 static int __sq_remap(struct sq_mapping *map, unsigned long flags)
105 #if defined(CONFIG_MMU)
106 struct vm_struct *vma;
108 vma = __get_vm_area(map->size, VM_ALLOC, map->sq_addr, SQ_ADDRMAX);
112 vma->phys_addr = map->addr;
114 if (ioremap_page_range((unsigned long)vma->addr,
115 (unsigned long)vma->addr + map->size,
116 vma->phys_addr, __pgprot(flags))) {
122 * Without an MMU (or with it turned off), this is much more
123 * straightforward, as we can just load up each queue's QACR with
124 * the physical address appropriately masked.
126 ctrl_outl(((map->addr >> 26) << 2) & 0x1c, SQ_QACR0);
127 ctrl_outl(((map->addr >> 26) << 2) & 0x1c, SQ_QACR1);
134 * sq_remap - Map a physical address through the Store Queues
135 * @phys: Physical address of mapping.
136 * @size: Length of mapping.
137 * @name: User invoking mapping.
138 * @flags: Protection flags.
140 * Remaps the physical address @phys through the next available store queue
141 * address of @size length. @name is logged at boot time as well as through
142 * the sysfs interface.
144 unsigned long sq_remap(unsigned long phys, unsigned int size,
145 const char *name, unsigned long flags)
147 struct sq_mapping *map;
152 /* Don't allow wraparound or zero size */
153 end = phys + size - 1;
154 if (unlikely(!size || end < phys))
156 /* Don't allow anyone to remap normal memory.. */
157 if (unlikely(phys < virt_to_phys(high_memory)))
161 size = PAGE_ALIGN(end + 1) - phys;
163 map = kmem_cache_alloc(sq_cache, GFP_KERNEL);
171 page = bitmap_find_free_region(sq_bitmap, 0x04000000 >> PAGE_SHIFT,
172 get_order(map->size));
173 if (unlikely(page < 0)) {
178 map->sq_addr = P4SEG_STORE_QUE + (page << PAGE_SHIFT);
180 ret = __sq_remap(map, pgprot_val(PAGE_KERNEL_NOCACHE) | flags);
181 if (unlikely(ret != 0))
184 psz = (size + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
185 pr_info("sqremap: %15s [%4d page%s] va 0x%08lx pa 0x%08lx\n",
186 likely(map->name) ? map->name : "???",
187 psz, psz == 1 ? " " : "s",
188 map->sq_addr, map->addr);
190 sq_mapping_list_add(map);
195 kmem_cache_free(sq_cache, map);
198 EXPORT_SYMBOL(sq_remap);
201 * sq_unmap - Unmap a Store Queue allocation
202 * @map: Pre-allocated Store Queue mapping.
204 * Unmaps the store queue allocation @map that was previously created by
205 * sq_remap(). Also frees up the pte that was previously inserted into
206 * the kernel page table and discards the UTLB translation.
208 void sq_unmap(unsigned long vaddr)
210 struct sq_mapping **p, *map;
211 struct vm_struct *vma;
214 for (p = &sq_mapping_list; (map = *p); p = &map->next)
215 if (map->sq_addr == vaddr)
218 if (unlikely(!map)) {
219 printk("%s: bad store queue address 0x%08lx\n",
220 __FUNCTION__, vaddr);
224 page = (map->sq_addr - P4SEG_STORE_QUE) >> PAGE_SHIFT;
225 bitmap_release_region(sq_bitmap, page, get_order(map->size));
228 vma = remove_vm_area((void *)(map->sq_addr & PAGE_MASK));
230 printk(KERN_ERR "%s: bad address 0x%08lx\n",
231 __FUNCTION__, map->sq_addr);
236 sq_mapping_list_del(map);
238 kmem_cache_free(sq_cache, map);
240 EXPORT_SYMBOL(sq_unmap);
243 * Needlessly complex sysfs interface. Unfortunately it doesn't seem like
244 * there is any other easy way to add things on a per-cpu basis without
245 * putting the directory entries somewhere stupid and having to create
246 * links in sysfs by hand back in to the per-cpu directories.
248 * Some day we may want to have an additional abstraction per store
249 * queue, but considering the kobject hell we already have to deal with,
250 * it's simply not worth the trouble.
252 static struct kobject *sq_kobject[NR_CPUS];
254 struct sq_sysfs_attr {
255 struct attribute attr;
256 ssize_t (*show)(char *buf);
257 ssize_t (*store)(const char *buf, size_t count);
260 #define to_sq_sysfs_attr(attr) container_of(attr, struct sq_sysfs_attr, attr)
262 static ssize_t sq_sysfs_show(struct kobject *kobj, struct attribute *attr,
265 struct sq_sysfs_attr *sattr = to_sq_sysfs_attr(attr);
267 if (likely(sattr->show))
268 return sattr->show(buf);
273 static ssize_t sq_sysfs_store(struct kobject *kobj, struct attribute *attr,
274 const char *buf, size_t count)
276 struct sq_sysfs_attr *sattr = to_sq_sysfs_attr(attr);
278 if (likely(sattr->store))
279 return sattr->store(buf, count);
284 static ssize_t mapping_show(char *buf)
286 struct sq_mapping **list, *entry;
289 for (list = &sq_mapping_list; (entry = *list); list = &entry->next)
290 p += sprintf(p, "%08lx-%08lx [%08lx]: %s\n",
291 entry->sq_addr, entry->sq_addr + entry->size,
292 entry->addr, entry->name);
297 static ssize_t mapping_store(const char *buf, size_t count)
299 unsigned long base = 0, len = 0;
301 sscanf(buf, "%lx %lx", &base, &len);
306 int ret = sq_remap(base, len, "Userspace",
307 pgprot_val(PAGE_SHARED));
316 static struct sq_sysfs_attr mapping_attr =
317 __ATTR(mapping, 0644, mapping_show, mapping_store);
319 static struct attribute *sq_sysfs_attrs[] = {
324 static struct sysfs_ops sq_sysfs_ops = {
325 .show = sq_sysfs_show,
326 .store = sq_sysfs_store,
329 static struct kobj_type ktype_percpu_entry = {
330 .sysfs_ops = &sq_sysfs_ops,
331 .default_attrs = sq_sysfs_attrs,
334 static int __devinit sq_sysdev_add(struct sys_device *sysdev)
336 unsigned int cpu = sysdev->id;
337 struct kobject *kobj;
339 sq_kobject[cpu] = kzalloc(sizeof(struct kobject), GFP_KERNEL);
340 if (unlikely(!sq_kobject[cpu]))
343 kobj = sq_kobject[cpu];
344 kobj->parent = &sysdev->kobj;
345 kobject_set_name(kobj, "%s", "sq");
346 kobj->ktype = &ktype_percpu_entry;
348 return kobject_register(kobj);
351 static int __devexit sq_sysdev_remove(struct sys_device *sysdev)
353 unsigned int cpu = sysdev->id;
354 struct kobject *kobj = sq_kobject[cpu];
356 kobject_unregister(kobj);
360 static struct sysdev_driver sq_sysdev_driver = {
361 .add = sq_sysdev_add,
362 .remove = __devexit_p(sq_sysdev_remove),
365 static int __init sq_api_init(void)
367 unsigned int nr_pages = 0x04000000 >> PAGE_SHIFT;
368 unsigned int size = (nr_pages + (BITS_PER_LONG - 1)) / BITS_PER_LONG;
371 printk(KERN_NOTICE "sq: Registering store queue API.\n");
373 sq_cache = kmem_cache_create("store_queue_cache",
374 sizeof(struct sq_mapping), 0, 0,
376 if (unlikely(!sq_cache))
379 sq_bitmap = kzalloc(size, GFP_KERNEL);
380 if (unlikely(!sq_bitmap))
383 ret = sysdev_driver_register(&cpu_sysdev_class, &sq_sysdev_driver);
384 if (unlikely(ret != 0))
391 kmem_cache_destroy(sq_cache);
396 static void __exit sq_api_exit(void)
398 sysdev_driver_unregister(&cpu_sysdev_class, &sq_sysdev_driver);
400 kmem_cache_destroy(sq_cache);
403 module_init(sq_api_init);
404 module_exit(sq_api_exit);
406 MODULE_AUTHOR("Paul Mundt <lethal@linux-sh.org>, M. R. Brown <mrbrown@0xd6.org>");
407 MODULE_DESCRIPTION("Simple API for SH-4 integrated Store Queues");
408 MODULE_LICENSE("GPL");