mspec.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. /*
  2. * Copyright (C) 2001-2006 Silicon Graphics, Inc. All rights
  3. * reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of version 2 of the GNU General Public License
  7. * as published by the Free Software Foundation.
  8. */
  9. /*
  10. * SN Platform Special Memory (mspec) Support
  11. *
  12. * This driver exports the SN special memory (mspec) facility to user
  13. * processes.
  14. * There are three types of memory made available thru this driver:
  15. * fetchops, uncached and cached.
  16. *
  17. * Fetchops are atomic memory operations that are implemented in the
  18. * memory controller on SGI SN hardware.
  19. *
  20. * Uncached are used for memory write combining feature of the ia64
  21. * cpu.
  22. *
  23. * Cached are used for areas of memory that are used as cached addresses
  24. * on our partition and used as uncached addresses from other partitions.
  25. * Due to a design constraint of the SN2 Shub, you can not have processors
  26. * on the same FSB perform both a cached and uncached reference to the
  27. * same cache line. These special memory cached regions prevent the
  28. * kernel from ever dropping in a TLB entry and therefore prevent the
  29. * processor from ever speculating a cache line from this page.
  30. */
  31. #include <linux/types.h>
  32. #include <linux/kernel.h>
  33. #include <linux/module.h>
  34. #include <linux/init.h>
  35. #include <linux/errno.h>
  36. #include <linux/miscdevice.h>
  37. #include <linux/spinlock.h>
  38. #include <linux/mm.h>
  39. #include <linux/fs.h>
  40. #include <linux/vmalloc.h>
  41. #include <linux/string.h>
  42. #include <linux/slab.h>
  43. #include <linux/numa.h>
  44. #include <linux/refcount.h>
  45. #include <asm/page.h>
  46. #include <asm/pgtable.h>
  47. #include <linux/atomic.h>
  48. #include <asm/tlbflush.h>
  49. #include <asm/uncached.h>
  50. #include <asm/sn/addrs.h>
  51. #include <asm/sn/arch.h>
  52. #include <asm/sn/mspec.h>
  53. #include <asm/sn/sn_cpuid.h>
  54. #include <asm/sn/io.h>
  55. #include <asm/sn/bte.h>
  56. #include <asm/sn/shubio.h>
  57. #define FETCHOP_ID "SGI Fetchop,"
  58. #define CACHED_ID "Cached,"
  59. #define UNCACHED_ID "Uncached"
  60. #define REVISION "4.0"
  61. #define MSPEC_BASENAME "mspec"
  62. /*
  63. * Page types allocated by the device.
  64. */
  65. enum mspec_page_type {
  66. MSPEC_FETCHOP = 1,
  67. MSPEC_CACHED,
  68. MSPEC_UNCACHED
  69. };
  70. #ifdef CONFIG_SGI_SN
  71. static int is_sn2;
  72. #else
  73. #define is_sn2 0
  74. #endif
  75. /*
  76. * One of these structures is allocated when an mspec region is mmaped. The
  77. * structure is pointed to by the vma->vm_private_data field in the vma struct.
  78. * This structure is used to record the addresses of the mspec pages.
  79. * This structure is shared by all vma's that are split off from the
  80. * original vma when split_vma()'s are done.
  81. *
  82. * The refcnt is incremented atomically because mm->mmap_sem does not
  83. * protect in fork case where multiple tasks share the vma_data.
  84. */
  85. struct vma_data {
  86. refcount_t refcnt; /* Number of vmas sharing the data. */
  87. spinlock_t lock; /* Serialize access to this structure. */
  88. int count; /* Number of pages allocated. */
  89. enum mspec_page_type type; /* Type of pages allocated. */
  90. unsigned long vm_start; /* Original (unsplit) base. */
  91. unsigned long vm_end; /* Original (unsplit) end. */
  92. unsigned long maddr[0]; /* Array of MSPEC addresses. */
  93. };
  94. /* used on shub2 to clear FOP cache in the HUB */
  95. static unsigned long scratch_page[MAX_NUMNODES];
  96. #define SH2_AMO_CACHE_ENTRIES 4
  97. static inline int
  98. mspec_zero_block(unsigned long addr, int len)
  99. {
  100. int status;
  101. if (is_sn2) {
  102. if (is_shub2()) {
  103. int nid;
  104. void *p;
  105. int i;
  106. nid = nasid_to_cnodeid(get_node_number(__pa(addr)));
  107. p = (void *)TO_AMO(scratch_page[nid]);
  108. for (i=0; i < SH2_AMO_CACHE_ENTRIES; i++) {
  109. FETCHOP_LOAD_OP(p, FETCHOP_LOAD);
  110. p += FETCHOP_VAR_SIZE;
  111. }
  112. }
  113. status = bte_copy(0, addr & ~__IA64_UNCACHED_OFFSET, len,
  114. BTE_WACQUIRE | BTE_ZERO_FILL, NULL);
  115. } else {
  116. memset((char *) addr, 0, len);
  117. status = 0;
  118. }
  119. return status;
  120. }
  121. /*
  122. * mspec_open
  123. *
  124. * Called when a device mapping is created by a means other than mmap
  125. * (via fork, munmap, etc.). Increments the reference count on the
  126. * underlying mspec data so it is not freed prematurely.
  127. */
  128. static void
  129. mspec_open(struct vm_area_struct *vma)
  130. {
  131. struct vma_data *vdata;
  132. vdata = vma->vm_private_data;
  133. refcount_inc(&vdata->refcnt);
  134. }
  135. /*
  136. * mspec_close
  137. *
  138. * Called when unmapping a device mapping. Frees all mspec pages
  139. * belonging to all the vma's sharing this vma_data structure.
  140. */
  141. static void
  142. mspec_close(struct vm_area_struct *vma)
  143. {
  144. struct vma_data *vdata;
  145. int index, last_index;
  146. unsigned long my_page;
  147. vdata = vma->vm_private_data;
  148. if (!refcount_dec_and_test(&vdata->refcnt))
  149. return;
  150. last_index = (vdata->vm_end - vdata->vm_start) >> PAGE_SHIFT;
  151. for (index = 0; index < last_index; index++) {
  152. if (vdata->maddr[index] == 0)
  153. continue;
  154. /*
  155. * Clear the page before sticking it back
  156. * into the pool.
  157. */
  158. my_page = vdata->maddr[index];
  159. vdata->maddr[index] = 0;
  160. if (!mspec_zero_block(my_page, PAGE_SIZE))
  161. uncached_free_page(my_page, 1);
  162. else
  163. printk(KERN_WARNING "mspec_close(): "
  164. "failed to zero page %ld\n", my_page);
  165. }
  166. kvfree(vdata);
  167. }
  168. /*
  169. * mspec_fault
  170. *
  171. * Creates a mspec page and maps it to user space.
  172. */
  173. static vm_fault_t
  174. mspec_fault(struct vm_fault *vmf)
  175. {
  176. unsigned long paddr, maddr;
  177. unsigned long pfn;
  178. pgoff_t index = vmf->pgoff;
  179. struct vma_data *vdata = vmf->vma->vm_private_data;
  180. maddr = (volatile unsigned long) vdata->maddr[index];
  181. if (maddr == 0) {
  182. maddr = uncached_alloc_page(numa_node_id(), 1);
  183. if (maddr == 0)
  184. return VM_FAULT_OOM;
  185. spin_lock(&vdata->lock);
  186. if (vdata->maddr[index] == 0) {
  187. vdata->count++;
  188. vdata->maddr[index] = maddr;
  189. } else {
  190. uncached_free_page(maddr, 1);
  191. maddr = vdata->maddr[index];
  192. }
  193. spin_unlock(&vdata->lock);
  194. }
  195. if (vdata->type == MSPEC_FETCHOP)
  196. paddr = TO_AMO(maddr);
  197. else
  198. paddr = maddr & ~__IA64_UNCACHED_OFFSET;
  199. pfn = paddr >> PAGE_SHIFT;
  200. return vmf_insert_pfn(vmf->vma, vmf->address, pfn);
  201. }
  202. static const struct vm_operations_struct mspec_vm_ops = {
  203. .open = mspec_open,
  204. .close = mspec_close,
  205. .fault = mspec_fault,
  206. };
  207. /*
  208. * mspec_mmap
  209. *
  210. * Called when mmapping the device. Initializes the vma with a fault handler
  211. * and private data structure necessary to allocate, track, and free the
  212. * underlying pages.
  213. */
  214. static int
  215. mspec_mmap(struct file *file, struct vm_area_struct *vma,
  216. enum mspec_page_type type)
  217. {
  218. struct vma_data *vdata;
  219. int pages, vdata_size;
  220. if (vma->vm_pgoff != 0)
  221. return -EINVAL;
  222. if ((vma->vm_flags & VM_SHARED) == 0)
  223. return -EINVAL;
  224. if ((vma->vm_flags & VM_WRITE) == 0)
  225. return -EPERM;
  226. pages = vma_pages(vma);
  227. vdata_size = sizeof(struct vma_data) + pages * sizeof(long);
  228. if (vdata_size <= PAGE_SIZE)
  229. vdata = kzalloc(vdata_size, GFP_KERNEL);
  230. else
  231. vdata = vzalloc(vdata_size);
  232. if (!vdata)
  233. return -ENOMEM;
  234. vdata->vm_start = vma->vm_start;
  235. vdata->vm_end = vma->vm_end;
  236. vdata->type = type;
  237. spin_lock_init(&vdata->lock);
  238. refcount_set(&vdata->refcnt, 1);
  239. vma->vm_private_data = vdata;
  240. vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP;
  241. if (vdata->type == MSPEC_FETCHOP || vdata->type == MSPEC_UNCACHED)
  242. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  243. vma->vm_ops = &mspec_vm_ops;
  244. return 0;
  245. }
  246. static int
  247. fetchop_mmap(struct file *file, struct vm_area_struct *vma)
  248. {
  249. return mspec_mmap(file, vma, MSPEC_FETCHOP);
  250. }
  251. static int
  252. cached_mmap(struct file *file, struct vm_area_struct *vma)
  253. {
  254. return mspec_mmap(file, vma, MSPEC_CACHED);
  255. }
  256. static int
  257. uncached_mmap(struct file *file, struct vm_area_struct *vma)
  258. {
  259. return mspec_mmap(file, vma, MSPEC_UNCACHED);
  260. }
  261. static const struct file_operations fetchop_fops = {
  262. .owner = THIS_MODULE,
  263. .mmap = fetchop_mmap,
  264. .llseek = noop_llseek,
  265. };
  266. static struct miscdevice fetchop_miscdev = {
  267. .minor = MISC_DYNAMIC_MINOR,
  268. .name = "sgi_fetchop",
  269. .fops = &fetchop_fops
  270. };
  271. static const struct file_operations cached_fops = {
  272. .owner = THIS_MODULE,
  273. .mmap = cached_mmap,
  274. .llseek = noop_llseek,
  275. };
  276. static struct miscdevice cached_miscdev = {
  277. .minor = MISC_DYNAMIC_MINOR,
  278. .name = "mspec_cached",
  279. .fops = &cached_fops
  280. };
  281. static const struct file_operations uncached_fops = {
  282. .owner = THIS_MODULE,
  283. .mmap = uncached_mmap,
  284. .llseek = noop_llseek,
  285. };
  286. static struct miscdevice uncached_miscdev = {
  287. .minor = MISC_DYNAMIC_MINOR,
  288. .name = "mspec_uncached",
  289. .fops = &uncached_fops
  290. };
  291. /*
  292. * mspec_init
  293. *
  294. * Called at boot time to initialize the mspec facility.
  295. */
  296. static int __init
  297. mspec_init(void)
  298. {
  299. int ret;
  300. int nid;
  301. /*
  302. * The fetchop device only works on SN2 hardware, uncached and cached
  303. * memory drivers should both be valid on all ia64 hardware
  304. */
  305. #ifdef CONFIG_SGI_SN
  306. if (ia64_platform_is("sn2")) {
  307. is_sn2 = 1;
  308. if (is_shub2()) {
  309. ret = -ENOMEM;
  310. for_each_node_state(nid, N_ONLINE) {
  311. int actual_nid;
  312. int nasid;
  313. unsigned long phys;
  314. scratch_page[nid] = uncached_alloc_page(nid, 1);
  315. if (scratch_page[nid] == 0)
  316. goto free_scratch_pages;
  317. phys = __pa(scratch_page[nid]);
  318. nasid = get_node_number(phys);
  319. actual_nid = nasid_to_cnodeid(nasid);
  320. if (actual_nid != nid)
  321. goto free_scratch_pages;
  322. }
  323. }
  324. ret = misc_register(&fetchop_miscdev);
  325. if (ret) {
  326. printk(KERN_ERR
  327. "%s: failed to register device %i\n",
  328. FETCHOP_ID, ret);
  329. goto free_scratch_pages;
  330. }
  331. }
  332. #endif
  333. ret = misc_register(&cached_miscdev);
  334. if (ret) {
  335. printk(KERN_ERR "%s: failed to register device %i\n",
  336. CACHED_ID, ret);
  337. if (is_sn2)
  338. misc_deregister(&fetchop_miscdev);
  339. goto free_scratch_pages;
  340. }
  341. ret = misc_register(&uncached_miscdev);
  342. if (ret) {
  343. printk(KERN_ERR "%s: failed to register device %i\n",
  344. UNCACHED_ID, ret);
  345. misc_deregister(&cached_miscdev);
  346. if (is_sn2)
  347. misc_deregister(&fetchop_miscdev);
  348. goto free_scratch_pages;
  349. }
  350. printk(KERN_INFO "%s %s initialized devices: %s %s %s\n",
  351. MSPEC_BASENAME, REVISION, is_sn2 ? FETCHOP_ID : "",
  352. CACHED_ID, UNCACHED_ID);
  353. return 0;
  354. free_scratch_pages:
  355. for_each_node(nid) {
  356. if (scratch_page[nid] != 0)
  357. uncached_free_page(scratch_page[nid], 1);
  358. }
  359. return ret;
  360. }
  361. static void __exit
  362. mspec_exit(void)
  363. {
  364. int nid;
  365. misc_deregister(&uncached_miscdev);
  366. misc_deregister(&cached_miscdev);
  367. if (is_sn2) {
  368. misc_deregister(&fetchop_miscdev);
  369. for_each_node(nid) {
  370. if (scratch_page[nid] != 0)
  371. uncached_free_page(scratch_page[nid], 1);
  372. }
  373. }
  374. }
  375. module_init(mspec_init);
  376. module_exit(mspec_exit);
  377. MODULE_AUTHOR("Silicon Graphics, Inc. <linux-altix@sgi.com>");
  378. MODULE_DESCRIPTION("Driver for SGI SN special memory operations");
  379. MODULE_LICENSE("GPL");