Linux Audio

Check our new training course

Loading...
v5.4
  1// SPDX-License-Identifier: GPL-2.0
  2/*
  3 * arch/sh/kernel/cpu/sh4/sq.c
  4 *
  5 * General management API for SH-4 integrated Store Queues
  6 *
  7 * Copyright (C) 2001 - 2006  Paul Mundt
  8 * Copyright (C) 2001, 2002  M. R. Brown
 
 
 
 
  9 */
 10#include <linux/init.h>
 11#include <linux/cpu.h>
 12#include <linux/bitmap.h>
 13#include <linux/device.h>
 14#include <linux/kernel.h>
 15#include <linux/module.h>
 16#include <linux/slab.h>
 17#include <linux/vmalloc.h>
 18#include <linux/mm.h>
 19#include <linux/io.h>
 20#include <linux/prefetch.h>
 21#include <asm/page.h>
 22#include <asm/cacheflush.h>
 23#include <cpu/sq.h>
 24
 25struct sq_mapping;
 26
 27struct sq_mapping {
 28	const char *name;
 29
 30	unsigned long sq_addr;
 31	unsigned long addr;
 32	unsigned int size;
 33
 34	struct sq_mapping *next;
 35};
 36
 37static struct sq_mapping *sq_mapping_list;
 38static DEFINE_SPINLOCK(sq_mapping_lock);
 39static struct kmem_cache *sq_cache;
 40static unsigned long *sq_bitmap;
 41
 42#define store_queue_barrier()			\
 43do {						\
 44	(void)__raw_readl(P4SEG_STORE_QUE);	\
 45	__raw_writel(0, P4SEG_STORE_QUE + 0);	\
 46	__raw_writel(0, P4SEG_STORE_QUE + 8);	\
 47} while (0);
 48
 49/**
 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
 53 *
 54 * Flushes the store queue cache from @start to @start + @len in a
 55 * linear fashion.
 56 */
 57void sq_flush_range(unsigned long start, unsigned int len)
 58{
 59	unsigned long *sq = (unsigned long *)start;
 60
 61	/* Flush the queues */
 62	for (len >>= 5; len--; sq += 8)
 63		prefetchw(sq);
 64
 65	/* Wait for completion */
 66	store_queue_barrier();
 67}
 68EXPORT_SYMBOL(sq_flush_range);
 69
 70static inline void sq_mapping_list_add(struct sq_mapping *map)
 71{
 72	struct sq_mapping **p, *tmp;
 73
 74	spin_lock_irq(&sq_mapping_lock);
 75
 76	p = &sq_mapping_list;
 77	while ((tmp = *p) != NULL)
 78		p = &tmp->next;
 79
 80	map->next = tmp;
 81	*p = map;
 82
 83	spin_unlock_irq(&sq_mapping_lock);
 84}
 85
 86static inline void sq_mapping_list_del(struct sq_mapping *map)
 87{
 88	struct sq_mapping **p, *tmp;
 89
 90	spin_lock_irq(&sq_mapping_lock);
 91
 92	for (p = &sq_mapping_list; (tmp = *p); p = &tmp->next)
 93		if (tmp == map) {
 94			*p = tmp->next;
 95			break;
 96		}
 97
 98	spin_unlock_irq(&sq_mapping_lock);
 99}
100
101static int __sq_remap(struct sq_mapping *map, pgprot_t prot)
102{
103#if defined(CONFIG_MMU)
104	struct vm_struct *vma;
105
106	vma = __get_vm_area(map->size, VM_ALLOC, map->sq_addr, SQ_ADDRMAX);
107	if (!vma)
108		return -ENOMEM;
109
110	vma->phys_addr = map->addr;
111
112	if (ioremap_page_range((unsigned long)vma->addr,
113			       (unsigned long)vma->addr + map->size,
114			       vma->phys_addr, prot)) {
115		vunmap(vma->addr);
116		return -EAGAIN;
117	}
118#else
119	/*
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.
123	 */
124	__raw_writel(((map->addr >> 26) << 2) & 0x1c, SQ_QACR0);
125	__raw_writel(((map->addr >> 26) << 2) & 0x1c, SQ_QACR1);
126#endif
127
128	return 0;
129}
130
131/**
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 * @prot: Protection bits.
137 *
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.
141 */
142unsigned long sq_remap(unsigned long phys, unsigned int size,
143		       const char *name, pgprot_t prot)
144{
145	struct sq_mapping *map;
146	unsigned long end;
147	unsigned int psz;
148	int ret, page;
149
150	/* Don't allow wraparound or zero size */
151	end = phys + size - 1;
152	if (unlikely(!size || end < phys))
153		return -EINVAL;
154	/* Don't allow anyone to remap normal memory.. */
155	if (unlikely(phys < virt_to_phys(high_memory)))
156		return -EINVAL;
157
158	phys &= PAGE_MASK;
159	size = PAGE_ALIGN(end + 1) - phys;
160
161	map = kmem_cache_alloc(sq_cache, GFP_KERNEL);
162	if (unlikely(!map))
163		return -ENOMEM;
164
165	map->addr = phys;
166	map->size = size;
167	map->name = name;
168
169	page = bitmap_find_free_region(sq_bitmap, 0x04000000 >> PAGE_SHIFT,
170				       get_order(map->size));
171	if (unlikely(page < 0)) {
172		ret = -ENOSPC;
173		goto out;
174	}
175
176	map->sq_addr = P4SEG_STORE_QUE + (page << PAGE_SHIFT);
177
178	ret = __sq_remap(map, prot);
179	if (unlikely(ret != 0))
180		goto out;
181
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);
187
188	sq_mapping_list_add(map);
189
190	return map->sq_addr;
191
192out:
193	kmem_cache_free(sq_cache, map);
194	return ret;
195}
196EXPORT_SYMBOL(sq_remap);
197
198/**
199 * sq_unmap - Unmap a Store Queue allocation
200 * @vaddr: Pre-allocated Store Queue mapping.
201 *
202 * Unmaps the store queue allocation @map that was previously created by
203 * sq_remap(). Also frees up the pte that was previously inserted into
204 * the kernel page table and discards the UTLB translation.
205 */
206void sq_unmap(unsigned long vaddr)
207{
208	struct sq_mapping **p, *map;
209	int page;
210
211	for (p = &sq_mapping_list; (map = *p); p = &map->next)
212		if (map->sq_addr == vaddr)
213			break;
214
215	if (unlikely(!map)) {
216		printk("%s: bad store queue address 0x%08lx\n",
217		       __func__, vaddr);
218		return;
219	}
220
221	page = (map->sq_addr - P4SEG_STORE_QUE) >> PAGE_SHIFT;
222	bitmap_release_region(sq_bitmap, page, get_order(map->size));
223
224#ifdef CONFIG_MMU
225	{
226		/*
227		 * Tear down the VMA in the MMU case.
228		 */
229		struct vm_struct *vma;
230
231		vma = remove_vm_area((void *)(map->sq_addr & PAGE_MASK));
232		if (!vma) {
233			printk(KERN_ERR "%s: bad address 0x%08lx\n",
234			       __func__, map->sq_addr);
235			return;
236		}
237	}
238#endif
239
240	sq_mapping_list_del(map);
241
242	kmem_cache_free(sq_cache, map);
243}
244EXPORT_SYMBOL(sq_unmap);
245
246/*
247 * Needlessly complex sysfs interface. Unfortunately it doesn't seem like
248 * there is any other easy way to add things on a per-cpu basis without
249 * putting the directory entries somewhere stupid and having to create
250 * links in sysfs by hand back in to the per-cpu directories.
251 *
252 * Some day we may want to have an additional abstraction per store
253 * queue, but considering the kobject hell we already have to deal with,
254 * it's simply not worth the trouble.
255 */
256static struct kobject *sq_kobject[NR_CPUS];
257
258struct sq_sysfs_attr {
259	struct attribute attr;
260	ssize_t (*show)(char *buf);
261	ssize_t (*store)(const char *buf, size_t count);
262};
263
264#define to_sq_sysfs_attr(a)	container_of(a, struct sq_sysfs_attr, attr)
265
266static ssize_t sq_sysfs_show(struct kobject *kobj, struct attribute *attr,
267			     char *buf)
268{
269	struct sq_sysfs_attr *sattr = to_sq_sysfs_attr(attr);
270
271	if (likely(sattr->show))
272		return sattr->show(buf);
273
274	return -EIO;
275}
276
277static ssize_t sq_sysfs_store(struct kobject *kobj, struct attribute *attr,
278			      const char *buf, size_t count)
279{
280	struct sq_sysfs_attr *sattr = to_sq_sysfs_attr(attr);
281
282	if (likely(sattr->store))
283		return sattr->store(buf, count);
284
285	return -EIO;
286}
287
288static ssize_t mapping_show(char *buf)
289{
290	struct sq_mapping **list, *entry;
291	char *p = buf;
292
293	for (list = &sq_mapping_list; (entry = *list); list = &entry->next)
294		p += sprintf(p, "%08lx-%08lx [%08lx]: %s\n",
295			     entry->sq_addr, entry->sq_addr + entry->size,
296			     entry->addr, entry->name);
297
298	return p - buf;
299}
300
301static ssize_t mapping_store(const char *buf, size_t count)
302{
303	unsigned long base = 0, len = 0;
304
305	sscanf(buf, "%lx %lx", &base, &len);
306	if (!base)
307		return -EIO;
308
309	if (likely(len)) {
310		int ret = sq_remap(base, len, "Userspace", PAGE_SHARED);
311		if (ret < 0)
312			return ret;
313	} else
314		sq_unmap(base);
315
316	return count;
317}
318
319static struct sq_sysfs_attr mapping_attr =
320	__ATTR(mapping, 0644, mapping_show, mapping_store);
321
322static struct attribute *sq_sysfs_attrs[] = {
323	&mapping_attr.attr,
324	NULL,
325};
326
327static const struct sysfs_ops sq_sysfs_ops = {
328	.show	= sq_sysfs_show,
329	.store	= sq_sysfs_store,
330};
331
332static struct kobj_type ktype_percpu_entry = {
333	.sysfs_ops	= &sq_sysfs_ops,
334	.default_attrs	= sq_sysfs_attrs,
335};
336
337static int sq_dev_add(struct device *dev, struct subsys_interface *sif)
338{
339	unsigned int cpu = dev->id;
340	struct kobject *kobj;
341	int error;
342
343	sq_kobject[cpu] = kzalloc(sizeof(struct kobject), GFP_KERNEL);
344	if (unlikely(!sq_kobject[cpu]))
345		return -ENOMEM;
346
347	kobj = sq_kobject[cpu];
348	error = kobject_init_and_add(kobj, &ktype_percpu_entry, &dev->kobj,
349				     "%s", "sq");
350	if (!error)
351		kobject_uevent(kobj, KOBJ_ADD);
352	return error;
353}
354
355static void sq_dev_remove(struct device *dev, struct subsys_interface *sif)
356{
357	unsigned int cpu = dev->id;
358	struct kobject *kobj = sq_kobject[cpu];
359
360	kobject_put(kobj);
 
361}
362
363static struct subsys_interface sq_interface = {
364	.name		= "sq",
365	.subsys		= &cpu_subsys,
366	.add_dev	= sq_dev_add,
367	.remove_dev	= sq_dev_remove,
368};
369
370static int __init sq_api_init(void)
371{
372	unsigned int nr_pages = 0x04000000 >> PAGE_SHIFT;
373	unsigned int size = (nr_pages + (BITS_PER_LONG - 1)) / BITS_PER_LONG;
374	int ret = -ENOMEM;
375
376	printk(KERN_NOTICE "sq: Registering store queue API.\n");
377
378	sq_cache = kmem_cache_create("store_queue_cache",
379				sizeof(struct sq_mapping), 0, 0, NULL);
380	if (unlikely(!sq_cache))
381		return ret;
382
383	sq_bitmap = kzalloc(size, GFP_KERNEL);
384	if (unlikely(!sq_bitmap))
385		goto out;
386
387	ret = subsys_interface_register(&sq_interface);
388	if (unlikely(ret != 0))
389		goto out;
390
391	return 0;
392
393out:
394	kfree(sq_bitmap);
395	kmem_cache_destroy(sq_cache);
396
397	return ret;
398}
399
400static void __exit sq_api_exit(void)
401{
402	subsys_interface_unregister(&sq_interface);
403	kfree(sq_bitmap);
404	kmem_cache_destroy(sq_cache);
405}
406
407module_init(sq_api_init);
408module_exit(sq_api_exit);
409
410MODULE_AUTHOR("Paul Mundt <lethal@linux-sh.org>, M. R. Brown <mrbrown@0xd6.org>");
411MODULE_DESCRIPTION("Simple API for SH-4 integrated Store Queues");
412MODULE_LICENSE("GPL");
v3.15
 
  1/*
  2 * arch/sh/kernel/cpu/sh4/sq.c
  3 *
  4 * General management API for SH-4 integrated Store Queues
  5 *
  6 * Copyright (C) 2001 - 2006  Paul Mundt
  7 * Copyright (C) 2001, 2002  M. R. Brown
  8 *
  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
 11 * for more details.
 12 */
 13#include <linux/init.h>
 14#include <linux/cpu.h>
 15#include <linux/bitmap.h>
 16#include <linux/device.h>
 17#include <linux/kernel.h>
 18#include <linux/module.h>
 19#include <linux/slab.h>
 20#include <linux/vmalloc.h>
 21#include <linux/mm.h>
 22#include <linux/io.h>
 23#include <linux/prefetch.h>
 24#include <asm/page.h>
 25#include <asm/cacheflush.h>
 26#include <cpu/sq.h>
 27
 28struct sq_mapping;
 29
 30struct sq_mapping {
 31	const char *name;
 32
 33	unsigned long sq_addr;
 34	unsigned long addr;
 35	unsigned int size;
 36
 37	struct sq_mapping *next;
 38};
 39
 40static struct sq_mapping *sq_mapping_list;
 41static DEFINE_SPINLOCK(sq_mapping_lock);
 42static struct kmem_cache *sq_cache;
 43static unsigned long *sq_bitmap;
 44
 45#define store_queue_barrier()			\
 46do {						\
 47	(void)__raw_readl(P4SEG_STORE_QUE);	\
 48	__raw_writel(0, P4SEG_STORE_QUE + 0);	\
 49	__raw_writel(0, P4SEG_STORE_QUE + 8);	\
 50} while (0);
 51
 52/**
 53 * sq_flush_range - Flush (prefetch) a specific SQ range
 54 * @start: the store queue address to start flushing from
 55 * @len: the length to flush
 56 *
 57 * Flushes the store queue cache from @start to @start + @len in a
 58 * linear fashion.
 59 */
 60void sq_flush_range(unsigned long start, unsigned int len)
 61{
 62	unsigned long *sq = (unsigned long *)start;
 63
 64	/* Flush the queues */
 65	for (len >>= 5; len--; sq += 8)
 66		prefetchw(sq);
 67
 68	/* Wait for completion */
 69	store_queue_barrier();
 70}
 71EXPORT_SYMBOL(sq_flush_range);
 72
 73static inline void sq_mapping_list_add(struct sq_mapping *map)
 74{
 75	struct sq_mapping **p, *tmp;
 76
 77	spin_lock_irq(&sq_mapping_lock);
 78
 79	p = &sq_mapping_list;
 80	while ((tmp = *p) != NULL)
 81		p = &tmp->next;
 82
 83	map->next = tmp;
 84	*p = map;
 85
 86	spin_unlock_irq(&sq_mapping_lock);
 87}
 88
 89static inline void sq_mapping_list_del(struct sq_mapping *map)
 90{
 91	struct sq_mapping **p, *tmp;
 92
 93	spin_lock_irq(&sq_mapping_lock);
 94
 95	for (p = &sq_mapping_list; (tmp = *p); p = &tmp->next)
 96		if (tmp == map) {
 97			*p = tmp->next;
 98			break;
 99		}
100
101	spin_unlock_irq(&sq_mapping_lock);
102}
103
104static int __sq_remap(struct sq_mapping *map, pgprot_t prot)
105{
106#if defined(CONFIG_MMU)
107	struct vm_struct *vma;
108
109	vma = __get_vm_area(map->size, VM_ALLOC, map->sq_addr, SQ_ADDRMAX);
110	if (!vma)
111		return -ENOMEM;
112
113	vma->phys_addr = map->addr;
114
115	if (ioremap_page_range((unsigned long)vma->addr,
116			       (unsigned long)vma->addr + map->size,
117			       vma->phys_addr, prot)) {
118		vunmap(vma->addr);
119		return -EAGAIN;
120	}
121#else
122	/*
123	 * Without an MMU (or with it turned off), this is much more
124	 * straightforward, as we can just load up each queue's QACR with
125	 * the physical address appropriately masked.
126	 */
127	__raw_writel(((map->addr >> 26) << 2) & 0x1c, SQ_QACR0);
128	__raw_writel(((map->addr >> 26) << 2) & 0x1c, SQ_QACR1);
129#endif
130
131	return 0;
132}
133
134/**
135 * sq_remap - Map a physical address through the Store Queues
136 * @phys: Physical address of mapping.
137 * @size: Length of mapping.
138 * @name: User invoking mapping.
139 * @prot: Protection bits.
140 *
141 * Remaps the physical address @phys through the next available store queue
142 * address of @size length. @name is logged at boot time as well as through
143 * the sysfs interface.
144 */
145unsigned long sq_remap(unsigned long phys, unsigned int size,
146		       const char *name, pgprot_t prot)
147{
148	struct sq_mapping *map;
149	unsigned long end;
150	unsigned int psz;
151	int ret, page;
152
153	/* Don't allow wraparound or zero size */
154	end = phys + size - 1;
155	if (unlikely(!size || end < phys))
156		return -EINVAL;
157	/* Don't allow anyone to remap normal memory.. */
158	if (unlikely(phys < virt_to_phys(high_memory)))
159		return -EINVAL;
160
161	phys &= PAGE_MASK;
162	size = PAGE_ALIGN(end + 1) - phys;
163
164	map = kmem_cache_alloc(sq_cache, GFP_KERNEL);
165	if (unlikely(!map))
166		return -ENOMEM;
167
168	map->addr = phys;
169	map->size = size;
170	map->name = name;
171
172	page = bitmap_find_free_region(sq_bitmap, 0x04000000 >> PAGE_SHIFT,
173				       get_order(map->size));
174	if (unlikely(page < 0)) {
175		ret = -ENOSPC;
176		goto out;
177	}
178
179	map->sq_addr = P4SEG_STORE_QUE + (page << PAGE_SHIFT);
180
181	ret = __sq_remap(map, prot);
182	if (unlikely(ret != 0))
183		goto out;
184
185	psz = (size + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
186	pr_info("sqremap: %15s  [%4d page%s]  va 0x%08lx   pa 0x%08lx\n",
187		likely(map->name) ? map->name : "???",
188		psz, psz == 1 ? " " : "s",
189		map->sq_addr, map->addr);
190
191	sq_mapping_list_add(map);
192
193	return map->sq_addr;
194
195out:
196	kmem_cache_free(sq_cache, map);
197	return ret;
198}
199EXPORT_SYMBOL(sq_remap);
200
201/**
202 * sq_unmap - Unmap a Store Queue allocation
203 * @vaddr: Pre-allocated Store Queue mapping.
204 *
205 * Unmaps the store queue allocation @map that was previously created by
206 * sq_remap(). Also frees up the pte that was previously inserted into
207 * the kernel page table and discards the UTLB translation.
208 */
209void sq_unmap(unsigned long vaddr)
210{
211	struct sq_mapping **p, *map;
212	int page;
213
214	for (p = &sq_mapping_list; (map = *p); p = &map->next)
215		if (map->sq_addr == vaddr)
216			break;
217
218	if (unlikely(!map)) {
219		printk("%s: bad store queue address 0x%08lx\n",
220		       __func__, vaddr);
221		return;
222	}
223
224	page = (map->sq_addr - P4SEG_STORE_QUE) >> PAGE_SHIFT;
225	bitmap_release_region(sq_bitmap, page, get_order(map->size));
226
227#ifdef CONFIG_MMU
228	{
229		/*
230		 * Tear down the VMA in the MMU case.
231		 */
232		struct vm_struct *vma;
233
234		vma = remove_vm_area((void *)(map->sq_addr & PAGE_MASK));
235		if (!vma) {
236			printk(KERN_ERR "%s: bad address 0x%08lx\n",
237			       __func__, map->sq_addr);
238			return;
239		}
240	}
241#endif
242
243	sq_mapping_list_del(map);
244
245	kmem_cache_free(sq_cache, map);
246}
247EXPORT_SYMBOL(sq_unmap);
248
249/*
250 * Needlessly complex sysfs interface. Unfortunately it doesn't seem like
251 * there is any other easy way to add things on a per-cpu basis without
252 * putting the directory entries somewhere stupid and having to create
253 * links in sysfs by hand back in to the per-cpu directories.
254 *
255 * Some day we may want to have an additional abstraction per store
256 * queue, but considering the kobject hell we already have to deal with,
257 * it's simply not worth the trouble.
258 */
259static struct kobject *sq_kobject[NR_CPUS];
260
261struct sq_sysfs_attr {
262	struct attribute attr;
263	ssize_t (*show)(char *buf);
264	ssize_t (*store)(const char *buf, size_t count);
265};
266
267#define to_sq_sysfs_attr(a)	container_of(a, struct sq_sysfs_attr, attr)
268
269static ssize_t sq_sysfs_show(struct kobject *kobj, struct attribute *attr,
270			     char *buf)
271{
272	struct sq_sysfs_attr *sattr = to_sq_sysfs_attr(attr);
273
274	if (likely(sattr->show))
275		return sattr->show(buf);
276
277	return -EIO;
278}
279
280static ssize_t sq_sysfs_store(struct kobject *kobj, struct attribute *attr,
281			      const char *buf, size_t count)
282{
283	struct sq_sysfs_attr *sattr = to_sq_sysfs_attr(attr);
284
285	if (likely(sattr->store))
286		return sattr->store(buf, count);
287
288	return -EIO;
289}
290
291static ssize_t mapping_show(char *buf)
292{
293	struct sq_mapping **list, *entry;
294	char *p = buf;
295
296	for (list = &sq_mapping_list; (entry = *list); list = &entry->next)
297		p += sprintf(p, "%08lx-%08lx [%08lx]: %s\n",
298			     entry->sq_addr, entry->sq_addr + entry->size,
299			     entry->addr, entry->name);
300
301	return p - buf;
302}
303
304static ssize_t mapping_store(const char *buf, size_t count)
305{
306	unsigned long base = 0, len = 0;
307
308	sscanf(buf, "%lx %lx", &base, &len);
309	if (!base)
310		return -EIO;
311
312	if (likely(len)) {
313		int ret = sq_remap(base, len, "Userspace", PAGE_SHARED);
314		if (ret < 0)
315			return ret;
316	} else
317		sq_unmap(base);
318
319	return count;
320}
321
322static struct sq_sysfs_attr mapping_attr =
323	__ATTR(mapping, 0644, mapping_show, mapping_store);
324
325static struct attribute *sq_sysfs_attrs[] = {
326	&mapping_attr.attr,
327	NULL,
328};
329
330static const struct sysfs_ops sq_sysfs_ops = {
331	.show	= sq_sysfs_show,
332	.store	= sq_sysfs_store,
333};
334
335static struct kobj_type ktype_percpu_entry = {
336	.sysfs_ops	= &sq_sysfs_ops,
337	.default_attrs	= sq_sysfs_attrs,
338};
339
340static int sq_dev_add(struct device *dev, struct subsys_interface *sif)
341{
342	unsigned int cpu = dev->id;
343	struct kobject *kobj;
344	int error;
345
346	sq_kobject[cpu] = kzalloc(sizeof(struct kobject), GFP_KERNEL);
347	if (unlikely(!sq_kobject[cpu]))
348		return -ENOMEM;
349
350	kobj = sq_kobject[cpu];
351	error = kobject_init_and_add(kobj, &ktype_percpu_entry, &dev->kobj,
352				     "%s", "sq");
353	if (!error)
354		kobject_uevent(kobj, KOBJ_ADD);
355	return error;
356}
357
358static int sq_dev_remove(struct device *dev, struct subsys_interface *sif)
359{
360	unsigned int cpu = dev->id;
361	struct kobject *kobj = sq_kobject[cpu];
362
363	kobject_put(kobj);
364	return 0;
365}
366
367static struct subsys_interface sq_interface = {
368	.name		= "sq",
369	.subsys		= &cpu_subsys,
370	.add_dev	= sq_dev_add,
371	.remove_dev	= sq_dev_remove,
372};
373
374static int __init sq_api_init(void)
375{
376	unsigned int nr_pages = 0x04000000 >> PAGE_SHIFT;
377	unsigned int size = (nr_pages + (BITS_PER_LONG - 1)) / BITS_PER_LONG;
378	int ret = -ENOMEM;
379
380	printk(KERN_NOTICE "sq: Registering store queue API.\n");
381
382	sq_cache = kmem_cache_create("store_queue_cache",
383				sizeof(struct sq_mapping), 0, 0, NULL);
384	if (unlikely(!sq_cache))
385		return ret;
386
387	sq_bitmap = kzalloc(size, GFP_KERNEL);
388	if (unlikely(!sq_bitmap))
389		goto out;
390
391	ret = subsys_interface_register(&sq_interface);
392	if (unlikely(ret != 0))
393		goto out;
394
395	return 0;
396
397out:
398	kfree(sq_bitmap);
399	kmem_cache_destroy(sq_cache);
400
401	return ret;
402}
403
404static void __exit sq_api_exit(void)
405{
406	subsys_interface_unregister(&sq_interface);
407	kfree(sq_bitmap);
408	kmem_cache_destroy(sq_cache);
409}
410
411module_init(sq_api_init);
412module_exit(sq_api_exit);
413
414MODULE_AUTHOR("Paul Mundt <lethal@linux-sh.org>, M. R. Brown <mrbrown@0xd6.org>");
415MODULE_DESCRIPTION("Simple API for SH-4 integrated Store Queues");
416MODULE_LICENSE("GPL");