mspec.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  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 <asm/page.h>
  45. #include <asm/pgtable.h>
  46. #include <linux/atomic.h>
  47. #include <asm/tlbflush.h>
  48. #include <asm/uncached.h>
  49. #include <asm/sn/addrs.h>
  50. #include <asm/sn/arch.h>
  51. #include <asm/sn/mspec.h>
  52. #include <asm/sn/sn_cpuid.h>
  53. #include <asm/sn/io.h>
  54. #include <asm/sn/bte.h>
  55. #include <asm/sn/shubio.h>
  56. #define FETCHOP_ID "SGI Fetchop,"
  57. #define CACHED_ID "Cached,"
  58. #define UNCACHED_ID "Uncached"
  59. #define REVISION "4.0"
  60. #define MSPEC_BASENAME "mspec"
  61. /*
  62. * Page types allocated by the device.
  63. */
  64. enum mspec_page_type {
  65. MSPEC_FETCHOP = 1,
  66. MSPEC_CACHED,
  67. MSPEC_UNCACHED
  68. };
  69. #ifdef CONFIG_SGI_SN
  70. static int is_sn2;
  71. #else
  72. #define is_sn2 0
  73. #endif
  74. /*
  75. * One of these structures is allocated when an mspec region is mmaped. The
  76. * structure is pointed to by the vma->vm_private_data field in the vma struct.
  77. * This structure is used to record the addresses of the mspec pages.
  78. * This structure is shared by all vma's that are split off from the
  79. * original vma when split_vma()'s are done.
  80. *
  81. * The refcnt is incremented atomically because mm->mmap_sem does not
  82. * protect in fork case where multiple tasks share the vma_data.
  83. */
  84. struct vma_data {
  85. atomic_t refcnt; /* Number of vmas sharing the data. */
  86. spinlock_t lock; /* Serialize access to this structure. */
  87. int count; /* Number of pages allocated. */
  88. enum mspec_page_type type; /* Type of pages allocated. */
  89. int flags; /* See VMD_xxx below. */
  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. #define VMD_VMALLOCED 0x1 /* vmalloc'd rather than kmalloc'd */
  95. /* used on shub2 to clear FOP cache in the HUB */
  96. static unsigned long scratch_page[MAX_NUMNODES];
  97. #define SH2_AMO_CACHE_ENTRIES 4
  98. static inline int
  99. mspec_zero_block(unsigned long addr, int len)
  100. {
  101. int status;
  102. if (is_sn2) {
  103. if (is_shub2()) {
  104. int nid;
  105. void *p;
  106. int i;
  107. nid = nasid_to_cnodeid(get_node_number(__pa(addr)));
  108. p = (void *)TO_AMO(scratch_page[nid]);
  109. for (i=0; i < SH2_AMO_CACHE_ENTRIES; i++) {
  110. FETCHOP_LOAD_OP(p, FETCHOP_LOAD);
  111. p += FETCHOP_VAR_SIZE;
  112. }
  113. }
  114. status = bte_copy(0, addr & ~__IA64_UNCACHED_OFFSET, len,
  115. BTE_WACQUIRE | BTE_ZERO_FILL, NULL);
  116. } else {
  117. memset((char *) addr, 0, len);
  118. status = 0;
  119. }
  120. return status;
  121. }
  122. /*
  123. * mspec_open
  124. *
  125. * Called when a device mapping is created by a means other than mmap
  126. * (via fork, munmap, etc.). Increments the reference count on the
  127. * underlying mspec data so it is not freed prematurely.
  128. */
  129. static void
  130. mspec_open(struct vm_area_struct *vma)
  131. {
  132. struct vma_data *vdata;
  133. vdata = vma->vm_private_data;
  134. atomic_inc(&vdata->refcnt);
  135. }
  136. /*
  137. * mspec_close
  138. *
  139. * Called when unmapping a device mapping. Frees all mspec pages
  140. * belonging to all the vma's sharing this vma_data structure.
  141. */
  142. static void
  143. mspec_close(struct vm_area_struct *vma)
  144. {
  145. struct vma_data *vdata;
  146. int index, last_index;
  147. unsigned long my_page;
  148. vdata = vma->vm_private_data;
  149. if (!atomic_dec_and_test(&vdata->refcnt))
  150. return;
  151. last_index = (vdata->vm_end - vdata->vm_start) >> PAGE_SHIFT;
  152. for (index = 0; index < last_index; index++) {
  153. if (vdata->maddr[index] == 0)
  154. continue;
  155. /*
  156. * Clear the page before sticking it back
  157. * into the pool.
  158. */
  159. my_page = vdata->maddr[index];
  160. vdata->maddr[index] = 0;
  161. if (!mspec_zero_block(my_page, PAGE_SIZE))
  162. uncached_free_page(my_page, 1);
  163. else
  164. printk(KERN_WARNING "mspec_close(): "
  165. "failed to zero page %ld\n", my_page);
  166. }
  167. if (vdata->flags & VMD_VMALLOCED)
  168. vfree(vdata);
  169. else
  170. kfree(vdata);
  171. }
  172. /*
  173. * mspec_fault
  174. *
  175. * Creates a mspec page and maps it to user space.
  176. */
  177. static int
  178. mspec_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  179. {
  180. unsigned long paddr, maddr;
  181. unsigned long pfn;
  182. pgoff_t index = vmf->pgoff;
  183. struct vma_data *vdata = vma->vm_private_data;
  184. maddr = (volatile unsigned long) vdata->maddr[index];
  185. if (maddr == 0) {
  186. maddr = uncached_alloc_page(numa_node_id(), 1);
  187. if (maddr == 0)
  188. return VM_FAULT_OOM;
  189. spin_lock(&vdata->lock);
  190. if (vdata->maddr[index] == 0) {
  191. vdata->count++;
  192. vdata->maddr[index] = maddr;
  193. } else {
  194. uncached_free_page(maddr, 1);
  195. maddr = vdata->maddr[index];
  196. }
  197. spin_unlock(&vdata->lock);
  198. }
  199. if (vdata->type == MSPEC_FETCHOP)
  200. paddr = TO_AMO(maddr);
  201. else
  202. paddr = maddr & ~__IA64_UNCACHED_OFFSET;
  203. pfn = paddr >> PAGE_SHIFT;
  204. /*
  205. * vm_insert_pfn can fail with -EBUSY, but in that case it will
  206. * be because another thread has installed the pte first, so it
  207. * is no problem.
  208. */
  209. vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, pfn);
  210. return VM_FAULT_NOPAGE;
  211. }
  212. static const struct vm_operations_struct mspec_vm_ops = {
  213. .open = mspec_open,
  214. .close = mspec_close,
  215. .fault = mspec_fault,
  216. };
  217. /*
  218. * mspec_mmap
  219. *
  220. * Called when mmapping the device. Initializes the vma with a fault handler
  221. * and private data structure necessary to allocate, track, and free the
  222. * underlying pages.
  223. */
  224. static int
  225. mspec_mmap(struct file *file, struct vm_area_struct *vma,
  226. enum mspec_page_type type)
  227. {
  228. struct vma_data *vdata;
  229. int pages, vdata_size, flags = 0;
  230. if (vma->vm_pgoff != 0)
  231. return -EINVAL;
  232. if ((vma->vm_flags & VM_SHARED) == 0)
  233. return -EINVAL;
  234. if ((vma->vm_flags & VM_WRITE) == 0)
  235. return -EPERM;
  236. pages = vma_pages(vma);
  237. vdata_size = sizeof(struct vma_data) + pages * sizeof(long);
  238. if (vdata_size <= PAGE_SIZE)
  239. vdata = kzalloc(vdata_size, GFP_KERNEL);
  240. else {
  241. vdata = vzalloc(vdata_size);
  242. flags = VMD_VMALLOCED;
  243. }
  244. if (!vdata)
  245. return -ENOMEM;
  246. vdata->vm_start = vma->vm_start;
  247. vdata->vm_end = vma->vm_end;
  248. vdata->flags = flags;
  249. vdata->type = type;
  250. spin_lock_init(&vdata->lock);
  251. atomic_set(&vdata->refcnt, 1);
  252. vma->vm_private_data = vdata;
  253. vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP;
  254. if (vdata->type == MSPEC_FETCHOP || vdata->type == MSPEC_UNCACHED)
  255. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  256. vma->vm_ops = &mspec_vm_ops;
  257. return 0;
  258. }
  259. static int
  260. fetchop_mmap(struct file *file, struct vm_area_struct *vma)
  261. {
  262. return mspec_mmap(file, vma, MSPEC_FETCHOP);
  263. }
  264. static int
  265. cached_mmap(struct file *file, struct vm_area_struct *vma)
  266. {
  267. return mspec_mmap(file, vma, MSPEC_CACHED);
  268. }
  269. static int
  270. uncached_mmap(struct file *file, struct vm_area_struct *vma)
  271. {
  272. return mspec_mmap(file, vma, MSPEC_UNCACHED);
  273. }
  274. static const struct file_operations fetchop_fops = {
  275. .owner = THIS_MODULE,
  276. .mmap = fetchop_mmap,
  277. .llseek = noop_llseek,
  278. };
  279. static struct miscdevice fetchop_miscdev = {
  280. .minor = MISC_DYNAMIC_MINOR,
  281. .name = "sgi_fetchop",
  282. .fops = &fetchop_fops
  283. };
  284. static const struct file_operations cached_fops = {
  285. .owner = THIS_MODULE,
  286. .mmap = cached_mmap,
  287. .llseek = noop_llseek,
  288. };
  289. static struct miscdevice cached_miscdev = {
  290. .minor = MISC_DYNAMIC_MINOR,
  291. .name = "mspec_cached",
  292. .fops = &cached_fops
  293. };
  294. static const struct file_operations uncached_fops = {
  295. .owner = THIS_MODULE,
  296. .mmap = uncached_mmap,
  297. .llseek = noop_llseek,
  298. };
  299. static struct miscdevice uncached_miscdev = {
  300. .minor = MISC_DYNAMIC_MINOR,
  301. .name = "mspec_uncached",
  302. .fops = &uncached_fops
  303. };
  304. /*
  305. * mspec_init
  306. *
  307. * Called at boot time to initialize the mspec facility.
  308. */
  309. static int __init
  310. mspec_init(void)
  311. {
  312. int ret;
  313. int nid;
  314. /*
  315. * The fetchop device only works on SN2 hardware, uncached and cached
  316. * memory drivers should both be valid on all ia64 hardware
  317. */
  318. #ifdef CONFIG_SGI_SN
  319. if (ia64_platform_is("sn2")) {
  320. is_sn2 = 1;
  321. if (is_shub2()) {
  322. ret = -ENOMEM;
  323. for_each_node_state(nid, N_ONLINE) {
  324. int actual_nid;
  325. int nasid;
  326. unsigned long phys;
  327. scratch_page[nid] = uncached_alloc_page(nid, 1);
  328. if (scratch_page[nid] == 0)
  329. goto free_scratch_pages;
  330. phys = __pa(scratch_page[nid]);
  331. nasid = get_node_number(phys);
  332. actual_nid = nasid_to_cnodeid(nasid);
  333. if (actual_nid != nid)
  334. goto free_scratch_pages;
  335. }
  336. }
  337. ret = misc_register(&fetchop_miscdev);
  338. if (ret) {
  339. printk(KERN_ERR
  340. "%s: failed to register device %i\n",
  341. FETCHOP_ID, ret);
  342. goto free_scratch_pages;
  343. }
  344. }
  345. #endif
  346. ret = misc_register(&cached_miscdev);
  347. if (ret) {
  348. printk(KERN_ERR "%s: failed to register device %i\n",
  349. CACHED_ID, ret);
  350. if (is_sn2)
  351. misc_deregister(&fetchop_miscdev);
  352. goto free_scratch_pages;
  353. }
  354. ret = misc_register(&uncached_miscdev);
  355. if (ret) {
  356. printk(KERN_ERR "%s: failed to register device %i\n",
  357. UNCACHED_ID, ret);
  358. misc_deregister(&cached_miscdev);
  359. if (is_sn2)
  360. misc_deregister(&fetchop_miscdev);
  361. goto free_scratch_pages;
  362. }
  363. printk(KERN_INFO "%s %s initialized devices: %s %s %s\n",
  364. MSPEC_BASENAME, REVISION, is_sn2 ? FETCHOP_ID : "",
  365. CACHED_ID, UNCACHED_ID);
  366. return 0;
  367. free_scratch_pages:
  368. for_each_node(nid) {
  369. if (scratch_page[nid] != 0)
  370. uncached_free_page(scratch_page[nid], 1);
  371. }
  372. return ret;
  373. }
  374. static void __exit
  375. mspec_exit(void)
  376. {
  377. int nid;
  378. misc_deregister(&uncached_miscdev);
  379. misc_deregister(&cached_miscdev);
  380. if (is_sn2) {
  381. misc_deregister(&fetchop_miscdev);
  382. for_each_node(nid) {
  383. if (scratch_page[nid] != 0)
  384. uncached_free_page(scratch_page[nid], 1);
  385. }
  386. }
  387. }
  388. module_init(mspec_init);
  389. module_exit(mspec_exit);
  390. MODULE_AUTHOR("Silicon Graphics, Inc. <linux-altix@sgi.com>");
  391. MODULE_DESCRIPTION("Driver for SGI SN special memory operations");
  392. MODULE_LICENSE("GPL");