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