sq.c 9.4 KB

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