sq.c 9.5 KB

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