sq.c 9.5 KB

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