sq.c 9.37 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1
/*
Paul Mundt's avatar
Paul Mundt committed
2
 * arch/sh/kernel/cpu/sh4/sq.c
Linus Torvalds's avatar
Linus Torvalds committed
3 4 5
 *
 * General management API for SH-4 integrated Store Queues
 *
Paul Mundt's avatar
Paul Mundt committed
6
 * Copyright (C) 2001 - 2006  Paul Mundt
Linus Torvalds's avatar
Linus Torvalds committed
7 8 9 10 11 12 13
 * Copyright (C) 2001, 2002  M. R. Brown
 *
 * This file is subject to the terms and conditions of the GNU General Public
 * License.  See the file "COPYING" in the main directory of this archive
 * for more details.
 */
#include <linux/init.h>
Paul Mundt's avatar
Paul Mundt committed
14 15 16
#include <linux/cpu.h>
#include <linux/bitmap.h>
#include <linux/sysdev.h>
Linus Torvalds's avatar
Linus Torvalds committed
17 18 19 20
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/vmalloc.h>
Paul Mundt's avatar
Paul Mundt committed
21
#include <linux/mm.h>
Paul Mundt's avatar
Paul Mundt committed
22
#include <linux/io.h>
Linus Torvalds's avatar
Linus Torvalds committed
23
#include <asm/page.h>
Paul Mundt's avatar
Paul Mundt committed
24
#include <asm/cacheflush.h>
Linus Torvalds's avatar
Linus Torvalds committed
25 26
#include <asm/cpu/sq.h>

Paul Mundt's avatar
Paul Mundt committed
27 28 29 30 31 32 33 34 35 36 37 38 39
struct sq_mapping;

struct sq_mapping {
	const char *name;

	unsigned long sq_addr;
	unsigned long addr;
	unsigned int size;

	struct sq_mapping *next;
};

static struct sq_mapping *sq_mapping_list;
Linus Torvalds's avatar
Linus Torvalds committed
40
static DEFINE_SPINLOCK(sq_mapping_lock);
41
static struct kmem_cache *sq_cache;
Paul Mundt's avatar
Paul Mundt committed
42
static unsigned long *sq_bitmap;
Linus Torvalds's avatar
Linus Torvalds committed
43

Paul Mundt's avatar
Paul Mundt committed
44 45 46 47 48 49
#define store_queue_barrier()			\
do {						\
	(void)ctrl_inl(P4SEG_STORE_QUE);	\
	ctrl_outl(0, P4SEG_STORE_QUE + 0);	\
	ctrl_outl(0, P4SEG_STORE_QUE + 8);	\
} while (0);
Linus Torvalds's avatar
Linus Torvalds committed
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64

/**
 * sq_flush_range - Flush (prefetch) a specific SQ range
 * @start: the store queue address to start flushing from
 * @len: the length to flush
 *
 * Flushes the store queue cache from @start to @start + @len in a
 * linear fashion.
 */
void sq_flush_range(unsigned long start, unsigned int len)
{
	volatile unsigned long *sq = (unsigned long *)start;

	/* Flush the queues */
	for (len >>= 5; len--; sq += 8)
Paul Mundt's avatar
Paul Mundt committed
65
		prefetchw((void *)sq);
Linus Torvalds's avatar
Linus Torvalds committed
66 67

	/* Wait for completion */
Paul Mundt's avatar
Paul Mundt committed
68
	store_queue_barrier();
Linus Torvalds's avatar
Linus Torvalds committed
69
}
Paul Mundt's avatar
Paul Mundt committed
70
EXPORT_SYMBOL(sq_flush_range);
Linus Torvalds's avatar
Linus Torvalds committed
71

Paul Mundt's avatar
Paul Mundt committed
72
static inline void sq_mapping_list_add(struct sq_mapping *map)
Linus Torvalds's avatar
Linus Torvalds committed
73
{
Paul Mundt's avatar
Paul Mundt committed
74
	struct sq_mapping **p, *tmp;
Linus Torvalds's avatar
Linus Torvalds committed
75

Paul Mundt's avatar
Paul Mundt committed
76
	spin_lock_irq(&sq_mapping_lock);
Linus Torvalds's avatar
Linus Torvalds committed
77

Paul Mundt's avatar
Paul Mundt committed
78 79 80
	p = &sq_mapping_list;
	while ((tmp = *p) != NULL)
		p = &tmp->next;
Linus Torvalds's avatar
Linus Torvalds committed
81

Paul Mundt's avatar
Paul Mundt committed
82 83
	map->next = tmp;
	*p = map;
Linus Torvalds's avatar
Linus Torvalds committed
84

Paul Mundt's avatar
Paul Mundt committed
85
	spin_unlock_irq(&sq_mapping_lock);
Linus Torvalds's avatar
Linus Torvalds committed
86 87
}

Paul Mundt's avatar
Paul Mundt committed
88
static inline void sq_mapping_list_del(struct sq_mapping *map)
Linus Torvalds's avatar
Linus Torvalds committed
89
{
Paul Mundt's avatar
Paul Mundt committed
90 91 92 93 94 95 96 97
	struct sq_mapping **p, *tmp;

	spin_lock_irq(&sq_mapping_lock);

	for (p = &sq_mapping_list; (tmp = *p); p = &tmp->next)
		if (tmp == map) {
			*p = tmp->next;
			break;
Linus Torvalds's avatar
Linus Torvalds committed
98 99
		}

Paul Mundt's avatar
Paul Mundt committed
100
	spin_unlock_irq(&sq_mapping_lock);
Linus Torvalds's avatar
Linus Torvalds committed
101 102
}

Paul Mundt's avatar
Paul Mundt committed
103
static int __sq_remap(struct sq_mapping *map, unsigned long flags)
Linus Torvalds's avatar
Linus Torvalds committed
104
{
Paul Mundt's avatar
Paul Mundt committed
105
#if defined(CONFIG_MMU)
Linus Torvalds's avatar
Linus Torvalds committed
106 107 108 109
	struct vm_struct *vma;

	vma = __get_vm_area(map->size, VM_ALLOC, map->sq_addr, SQ_ADDRMAX);
	if (!vma)
Paul Mundt's avatar
Paul Mundt committed
110
		return -ENOMEM;
Linus Torvalds's avatar
Linus Torvalds committed
111 112 113 114

	vma->phys_addr = map->addr;

	if (remap_area_pages((unsigned long)vma->addr, vma->phys_addr,
Paul Mundt's avatar
Paul Mundt committed
115
			     map->size, flags)) {
Linus Torvalds's avatar
Linus Torvalds committed
116
		vunmap(vma->addr);
Paul Mundt's avatar
Paul Mundt committed
117
		return -EAGAIN;
Linus Torvalds's avatar
Linus Torvalds committed
118
	}
Paul Mundt's avatar
Paul Mundt committed
119 120 121 122 123 124 125 126 127
#else
	/*
	 * Without an MMU (or with it turned off), this is much more
	 * straightforward, as we can just load up each queue's QACR with
	 * the physical address appropriately masked.
	 */
	ctrl_outl(((map->addr >> 26) << 2) & 0x1c, SQ_QACR0);
	ctrl_outl(((map->addr >> 26) << 2) & 0x1c, SQ_QACR1);
#endif
Linus Torvalds's avatar
Linus Torvalds committed
128

Paul Mundt's avatar
Paul Mundt committed
129
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
130 131 132 133 134 135 136
}

/**
 * sq_remap - Map a physical address through the Store Queues
 * @phys: Physical address of mapping.
 * @size: Length of mapping.
 * @name: User invoking mapping.
Paul Mundt's avatar
Paul Mundt committed
137
 * @flags: Protection flags.
Linus Torvalds's avatar
Linus Torvalds committed
138 139 140
 *
 * Remaps the physical address @phys through the next available store queue
 * address of @size length. @name is logged at boot time as well as through
Paul Mundt's avatar
Paul Mundt committed
141
 * the sysfs interface.
Linus Torvalds's avatar
Linus Torvalds committed
142
 */
Paul Mundt's avatar
Paul Mundt committed
143 144
unsigned long sq_remap(unsigned long phys, unsigned int size,
		       const char *name, unsigned long flags)
Linus Torvalds's avatar
Linus Torvalds committed
145 146
{
	struct sq_mapping *map;
Paul Mundt's avatar
Paul Mundt committed
147
	unsigned long end;
Linus Torvalds's avatar
Linus Torvalds committed
148
	unsigned int psz;
Paul Mundt's avatar
Paul Mundt committed
149
	int ret, page;
Linus Torvalds's avatar
Linus Torvalds committed
150 151 152

	/* Don't allow wraparound or zero size */
	end = phys + size - 1;
Paul Mundt's avatar
Paul Mundt committed
153 154
	if (unlikely(!size || end < phys))
		return -EINVAL;
Linus Torvalds's avatar
Linus Torvalds committed
155
	/* Don't allow anyone to remap normal memory.. */
Paul Mundt's avatar
Paul Mundt committed
156 157
	if (unlikely(phys < virt_to_phys(high_memory)))
		return -EINVAL;
Linus Torvalds's avatar
Linus Torvalds committed
158 159

	phys &= PAGE_MASK;
Paul Mundt's avatar
Paul Mundt committed
160 161 162 163 164 165 166 167 168 169
	size = PAGE_ALIGN(end + 1) - phys;

	map = kmem_cache_alloc(sq_cache, GFP_KERNEL);
	if (unlikely(!map))
		return -ENOMEM;

	map->addr = phys;
	map->size = size;
	map->name = name;

Paul Mundt's avatar
Paul Mundt committed
170
	page = bitmap_find_free_region(sq_bitmap, 0x04000000 >> PAGE_SHIFT,
Paul Mundt's avatar
Paul Mundt committed
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
				       get_order(map->size));
	if (unlikely(page < 0)) {
		ret = -ENOSPC;
		goto out;
	}

	map->sq_addr = P4SEG_STORE_QUE + (page << PAGE_SHIFT);

	ret = __sq_remap(map, flags);
	if (unlikely(ret != 0))
		goto out;

	psz = (size + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
	pr_info("sqremap: %15s  [%4d page%s]  va 0x%08lx   pa 0x%08lx\n",
		likely(map->name) ? map->name : "???",
		psz, psz == 1 ? " " : "s",
		map->sq_addr, map->addr);
Linus Torvalds's avatar
Linus Torvalds committed
188

Paul Mundt's avatar
Paul Mundt committed
189
	sq_mapping_list_add(map);
Linus Torvalds's avatar
Linus Torvalds committed
190

Paul Mundt's avatar
Paul Mundt committed
191
	return map->sq_addr;
Linus Torvalds's avatar
Linus Torvalds committed
192

Paul Mundt's avatar
Paul Mundt committed
193 194 195
out:
	kmem_cache_free(sq_cache, map);
	return ret;
Linus Torvalds's avatar
Linus Torvalds committed
196
}
Paul Mundt's avatar
Paul Mundt committed
197
EXPORT_SYMBOL(sq_remap);
Linus Torvalds's avatar
Linus Torvalds committed
198 199 200 201 202 203 204 205 206

/**
 * sq_unmap - Unmap a Store Queue allocation
 * @map: Pre-allocated Store Queue mapping.
 *
 * Unmaps the store queue allocation @map that was previously created by
 * sq_remap(). Also frees up the pte that was previously inserted into
 * the kernel page table and discards the UTLB translation.
 */
Paul Mundt's avatar
Paul Mundt committed
207
void sq_unmap(unsigned long vaddr)
Linus Torvalds's avatar
Linus Torvalds committed
208
{
Paul Mundt's avatar
Paul Mundt committed
209 210 211
	struct sq_mapping **p, *map;
	struct vm_struct *vma;
	int page;
Linus Torvalds's avatar
Linus Torvalds committed
212

Paul Mundt's avatar
Paul Mundt committed
213 214 215
	for (p = &sq_mapping_list; (map = *p); p = &map->next)
		if (map->sq_addr == vaddr)
			break;
Linus Torvalds's avatar
Linus Torvalds committed
216

Paul Mundt's avatar
Paul Mundt committed
217 218 219 220 221
	if (unlikely(!map)) {
		printk("%s: bad store queue address 0x%08lx\n",
		       __FUNCTION__, vaddr);
		return;
	}
Linus Torvalds's avatar
Linus Torvalds committed
222

Paul Mundt's avatar
Paul Mundt committed
223 224 225 226 227 228 229 230 231
	page = (map->sq_addr - P4SEG_STORE_QUE) >> PAGE_SHIFT;
	bitmap_release_region(sq_bitmap, page, get_order(map->size));

#ifdef CONFIG_MMU
	vma = remove_vm_area((void *)(map->sq_addr & PAGE_MASK));
	if (!vma) {
		printk(KERN_ERR "%s: bad address 0x%08lx\n",
		       __FUNCTION__, map->sq_addr);
		return;
Linus Torvalds's avatar
Linus Torvalds committed
232
	}
Paul Mundt's avatar
Paul Mundt committed
233 234 235
#endif

	sq_mapping_list_del(map);
Linus Torvalds's avatar
Linus Torvalds committed
236

Paul Mundt's avatar
Paul Mundt committed
237
	kmem_cache_free(sq_cache, map);
Linus Torvalds's avatar
Linus Torvalds committed
238
}
Paul Mundt's avatar
Paul Mundt committed
239
EXPORT_SYMBOL(sq_unmap);
Linus Torvalds's avatar
Linus Torvalds committed
240

Paul Mundt's avatar
Paul Mundt committed
241 242 243 244 245
/*
 * Needlessly complex sysfs interface. Unfortunately it doesn't seem like
 * there is any other easy way to add things on a per-cpu basis without
 * putting the directory entries somewhere stupid and having to create
 * links in sysfs by hand back in to the per-cpu directories.
Linus Torvalds's avatar
Linus Torvalds committed
246
 *
Paul Mundt's avatar
Paul Mundt committed
247 248 249
 * Some day we may want to have an additional abstraction per store
 * queue, but considering the kobject hell we already have to deal with,
 * it's simply not worth the trouble.
Linus Torvalds's avatar
Linus Torvalds committed
250
 */
Paul Mundt's avatar
Paul Mundt committed
251
static struct kobject *sq_kobject[NR_CPUS];
Linus Torvalds's avatar
Linus Torvalds committed
252

Paul Mundt's avatar
Paul Mundt committed
253 254 255 256 257
struct sq_sysfs_attr {
	struct attribute attr;
	ssize_t (*show)(char *buf);
	ssize_t (*store)(const char *buf, size_t count);
};
Linus Torvalds's avatar
Linus Torvalds committed
258

Paul Mundt's avatar
Paul Mundt committed
259
#define to_sq_sysfs_attr(attr)	container_of(attr, struct sq_sysfs_attr, attr)
Linus Torvalds's avatar
Linus Torvalds committed
260

Paul Mundt's avatar
Paul Mundt committed
261 262 263 264
static ssize_t sq_sysfs_show(struct kobject *kobj, struct attribute *attr,
			     char *buf)
{
	struct sq_sysfs_attr *sattr = to_sq_sysfs_attr(attr);
Linus Torvalds's avatar
Linus Torvalds committed
265

Paul Mundt's avatar
Paul Mundt committed
266 267
	if (likely(sattr->show))
		return sattr->show(buf);
Linus Torvalds's avatar
Linus Torvalds committed
268

Paul Mundt's avatar
Paul Mundt committed
269
	return -EIO;
Linus Torvalds's avatar
Linus Torvalds committed
270 271
}

Paul Mundt's avatar
Paul Mundt committed
272 273
static ssize_t sq_sysfs_store(struct kobject *kobj, struct attribute *attr,
			      const char *buf, size_t count)
Linus Torvalds's avatar
Linus Torvalds committed
274
{
Paul Mundt's avatar
Paul Mundt committed
275
	struct sq_sysfs_attr *sattr = to_sq_sysfs_attr(attr);
Linus Torvalds's avatar
Linus Torvalds committed
276

Paul Mundt's avatar
Paul Mundt committed
277 278 279 280
	if (likely(sattr->store))
		return sattr->store(buf, count);

	return -EIO;
Linus Torvalds's avatar
Linus Torvalds committed
281 282
}

Paul Mundt's avatar
Paul Mundt committed
283 284 285 286
static ssize_t mapping_show(char *buf)
{
	struct sq_mapping **list, *entry;
	char *p = buf;
Linus Torvalds's avatar
Linus Torvalds committed
287

Paul Mundt's avatar
Paul Mundt committed
288 289 290 291 292 293 294 295 296
	for (list = &sq_mapping_list; (entry = *list); list = &entry->next)
		p += sprintf(p, "%08lx-%08lx [%08lx]: %s\n",
			     entry->sq_addr, entry->sq_addr + entry->size,
			     entry->addr, entry->name);

	return p - buf;
}

static ssize_t mapping_store(const char *buf, size_t count)
Linus Torvalds's avatar
Linus Torvalds committed
297
{
Paul Mundt's avatar
Paul Mundt committed
298
	unsigned long base = 0, len = 0;
Linus Torvalds's avatar
Linus Torvalds committed
299

Paul Mundt's avatar
Paul Mundt committed
300 301 302
	sscanf(buf, "%lx %lx", &base, &len);
	if (!base)
		return -EIO;
Linus Torvalds's avatar
Linus Torvalds committed
303

Paul Mundt's avatar
Paul Mundt committed
304 305 306 307 308 309 310
	if (likely(len)) {
		int ret = sq_remap(base, len, "Userspace",
				   pgprot_val(PAGE_SHARED));
		if (ret < 0)
			return ret;
	} else
		sq_unmap(base);
Linus Torvalds's avatar
Linus Torvalds committed
311

Paul Mundt's avatar
Paul Mundt committed
312 313
	return count;
}
Linus Torvalds's avatar
Linus Torvalds committed
314

Paul Mundt's avatar
Paul Mundt committed
315 316
static struct sq_sysfs_attr mapping_attr =
	__ATTR(mapping, 0644, mapping_show, mapping_store);
Linus Torvalds's avatar
Linus Torvalds committed
317

Paul Mundt's avatar
Paul Mundt committed
318 319 320 321
static struct attribute *sq_sysfs_attrs[] = {
	&mapping_attr.attr,
	NULL,
};
Linus Torvalds's avatar
Linus Torvalds committed
322

Paul Mundt's avatar
Paul Mundt committed
323 324 325 326
static struct sysfs_ops sq_sysfs_ops = {
	.show	= sq_sysfs_show,
	.store	= sq_sysfs_store,
};
Linus Torvalds's avatar
Linus Torvalds committed
327

Paul Mundt's avatar
Paul Mundt committed
328 329 330 331
static struct kobj_type ktype_percpu_entry = {
	.sysfs_ops	= &sq_sysfs_ops,
	.default_attrs	= sq_sysfs_attrs,
};
Linus Torvalds's avatar
Linus Torvalds committed
332

Paul Mundt's avatar
Paul Mundt committed
333
static int __devinit sq_sysdev_add(struct sys_device *sysdev)
Linus Torvalds's avatar
Linus Torvalds committed
334
{
Paul Mundt's avatar
Paul Mundt committed
335 336
	unsigned int cpu = sysdev->id;
	struct kobject *kobj;
Linus Torvalds's avatar
Linus Torvalds committed
337

Paul Mundt's avatar
Paul Mundt committed
338 339 340
	sq_kobject[cpu] = kzalloc(sizeof(struct kobject), GFP_KERNEL);
	if (unlikely(!sq_kobject[cpu]))
		return -ENOMEM;
Linus Torvalds's avatar
Linus Torvalds committed
341

Paul Mundt's avatar
Paul Mundt committed
342 343 344 345
	kobj = sq_kobject[cpu];
	kobj->parent = &sysdev->kobj;
	kobject_set_name(kobj, "%s", "sq");
	kobj->ktype = &ktype_percpu_entry;
Linus Torvalds's avatar
Linus Torvalds committed
346

Paul Mundt's avatar
Paul Mundt committed
347
	return kobject_register(kobj);
Linus Torvalds's avatar
Linus Torvalds committed
348 349
}

Paul Mundt's avatar
Paul Mundt committed
350 351 352 353
static int __devexit sq_sysdev_remove(struct sys_device *sysdev)
{
	unsigned int cpu = sysdev->id;
	struct kobject *kobj = sq_kobject[cpu];
Linus Torvalds's avatar
Linus Torvalds committed
354

Paul Mundt's avatar
Paul Mundt committed
355 356 357 358 359 360 361
	kobject_unregister(kobj);
	return 0;
}

static struct sysdev_driver sq_sysdev_driver = {
	.add		= sq_sysdev_add,
	.remove		= __devexit_p(sq_sysdev_remove),
Linus Torvalds's avatar
Linus Torvalds committed
362 363 364 365
};

static int __init sq_api_init(void)
{
Paul Mundt's avatar
Paul Mundt committed
366 367 368 369
	unsigned int nr_pages = 0x04000000 >> PAGE_SHIFT;
	unsigned int size = (nr_pages + (BITS_PER_LONG - 1)) / BITS_PER_LONG;
	int ret = -ENOMEM;

Linus Torvalds's avatar
Linus Torvalds committed
370 371
	printk(KERN_NOTICE "sq: Registering store queue API.\n");

Paul Mundt's avatar
Paul Mundt committed
372 373 374 375 376
	sq_cache = kmem_cache_create("store_queue_cache",
				sizeof(struct sq_mapping), 0, 0,
				NULL, NULL);
	if (unlikely(!sq_cache))
		return ret;
Linus Torvalds's avatar
Linus Torvalds committed
377

Paul Mundt's avatar
Paul Mundt committed
378 379 380 381 382 383 384 385 386 387 388 389 390
	sq_bitmap = kzalloc(size, GFP_KERNEL);
	if (unlikely(!sq_bitmap))
		goto out;

	ret = sysdev_driver_register(&cpu_sysdev_class, &sq_sysdev_driver);
	if (unlikely(ret != 0))
		goto out;

	return 0;

out:
	kfree(sq_bitmap);
	kmem_cache_destroy(sq_cache);
391 392

	return ret;
Linus Torvalds's avatar
Linus Torvalds committed
393 394 395 396
}

static void __exit sq_api_exit(void)
{
Paul Mundt's avatar
Paul Mundt committed
397 398 399
	sysdev_driver_unregister(&cpu_sysdev_class, &sq_sysdev_driver);
	kfree(sq_bitmap);
	kmem_cache_destroy(sq_cache);
Linus Torvalds's avatar
Linus Torvalds committed
400 401 402 403 404 405 406 407
}

module_init(sq_api_init);
module_exit(sq_api_exit);

MODULE_AUTHOR("Paul Mundt <lethal@linux-sh.org>, M. R. Brown <mrbrown@0xd6.org>");
MODULE_DESCRIPTION("Simple API for SH-4 integrated Store Queues");
MODULE_LICENSE("GPL");