sq.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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 <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. unsigned long *sq = (unsigned long *)start;
  55. /* Flush the queues */
  56. for (len >>= 5; len--; sq += 8)
  57. prefetchw(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. * @vaddr: 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. int page;
  176. for (p = &sq_mapping_list; (map = *p); p = &map->next)
  177. if (map->sq_addr == vaddr)
  178. break;
  179. if (unlikely(!map)) {
  180. printk("%s: bad store queue address 0x%08lx\n",
  181. __func__, vaddr);
  182. return;
  183. }
  184. page = (map->sq_addr - P4SEG_STORE_QUE) >> PAGE_SHIFT;
  185. bitmap_release_region(sq_bitmap, page, get_order(map->size));
  186. #ifdef CONFIG_MMU
  187. {
  188. /*
  189. * Tear down the VMA in the MMU case.
  190. */
  191. struct vm_struct *vma;
  192. vma = remove_vm_area((void *)(map->sq_addr & PAGE_MASK));
  193. if (!vma) {
  194. printk(KERN_ERR "%s: bad address 0x%08lx\n",
  195. __func__, map->sq_addr);
  196. return;
  197. }
  198. }
  199. #endif
  200. sq_mapping_list_del(map);
  201. kmem_cache_free(sq_cache, map);
  202. }
  203. EXPORT_SYMBOL(sq_unmap);
  204. /*
  205. * Needlessly complex sysfs interface. Unfortunately it doesn't seem like
  206. * there is any other easy way to add things on a per-cpu basis without
  207. * putting the directory entries somewhere stupid and having to create
  208. * links in sysfs by hand back in to the per-cpu directories.
  209. *
  210. * Some day we may want to have an additional abstraction per store
  211. * queue, but considering the kobject hell we already have to deal with,
  212. * it's simply not worth the trouble.
  213. */
  214. static struct kobject *sq_kobject[NR_CPUS];
  215. struct sq_sysfs_attr {
  216. struct attribute attr;
  217. ssize_t (*show)(char *buf);
  218. ssize_t (*store)(const char *buf, size_t count);
  219. };
  220. #define to_sq_sysfs_attr(a) container_of(a, struct sq_sysfs_attr, attr)
  221. static ssize_t sq_sysfs_show(struct kobject *kobj, struct attribute *attr,
  222. char *buf)
  223. {
  224. struct sq_sysfs_attr *sattr = to_sq_sysfs_attr(attr);
  225. if (likely(sattr->show))
  226. return sattr->show(buf);
  227. return -EIO;
  228. }
  229. static ssize_t sq_sysfs_store(struct kobject *kobj, struct attribute *attr,
  230. const char *buf, size_t count)
  231. {
  232. struct sq_sysfs_attr *sattr = to_sq_sysfs_attr(attr);
  233. if (likely(sattr->store))
  234. return sattr->store(buf, count);
  235. return -EIO;
  236. }
  237. static ssize_t mapping_show(char *buf)
  238. {
  239. struct sq_mapping **list, *entry;
  240. char *p = buf;
  241. for (list = &sq_mapping_list; (entry = *list); list = &entry->next)
  242. p += sprintf(p, "%08lx-%08lx [%08lx]: %s\n",
  243. entry->sq_addr, entry->sq_addr + entry->size,
  244. entry->addr, entry->name);
  245. return p - buf;
  246. }
  247. static ssize_t mapping_store(const char *buf, size_t count)
  248. {
  249. unsigned long base = 0, len = 0;
  250. sscanf(buf, "%lx %lx", &base, &len);
  251. if (!base)
  252. return -EIO;
  253. if (likely(len)) {
  254. int ret = sq_remap(base, len, "Userspace",
  255. pgprot_val(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 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 __devinit sq_sysdev_add(struct sys_device *sysdev)
  277. {
  278. unsigned int cpu = sysdev->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, &sysdev->kobj,
  286. "%s", "sq");
  287. if (!error)
  288. kobject_uevent(kobj, KOBJ_ADD);
  289. return error;
  290. }
  291. static int __devexit sq_sysdev_remove(struct sys_device *sysdev)
  292. {
  293. unsigned int cpu = sysdev->id;
  294. struct kobject *kobj = sq_kobject[cpu];
  295. kobject_put(kobj);
  296. return 0;
  297. }
  298. static struct sysdev_driver sq_sysdev_driver = {
  299. .add = sq_sysdev_add,
  300. .remove = __devexit_p(sq_sysdev_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 = kzalloc(size, GFP_KERNEL);
  313. if (unlikely(!sq_bitmap))
  314. goto out;
  315. ret = sysdev_driver_register(&cpu_sysdev_class, &sq_sysdev_driver);
  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. sysdev_driver_unregister(&cpu_sysdev_class, &sq_sysdev_driver);
  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 <lethal@linux-sh.org>, M. R. Brown <mrbrown@0xd6.org>");
  333. MODULE_DESCRIPTION("Simple API for SH-4 integrated Store Queues");
  334. MODULE_LICENSE("GPL");