mspec.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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/vmalloc.h>
  40. #include <linux/string.h>
  41. #include <linux/slab.h>
  42. #include <linux/numa.h>
  43. #include <asm/page.h>
  44. #include <asm/system.h>
  45. #include <asm/pgtable.h>
  46. #include <asm/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 {
  65. MSPEC_FETCHOP = 1,
  66. MSPEC_CACHED,
  67. MSPEC_UNCACHED
  68. };
  69. static int is_sn2;
  70. /*
  71. * One of these structures is allocated when an mspec region is mmaped. The
  72. * structure is pointed to by the vma->vm_private_data field in the vma struct.
  73. * This structure is used to record the addresses of the mspec pages.
  74. */
  75. struct vma_data {
  76. atomic_t refcnt; /* Number of vmas sharing the data. */
  77. spinlock_t lock; /* Serialize access to the vma. */
  78. int count; /* Number of pages allocated. */
  79. int type; /* Type of pages allocated. */
  80. unsigned long maddr[0]; /* Array of MSPEC addresses. */
  81. };
  82. /* used on shub2 to clear FOP cache in the HUB */
  83. static unsigned long scratch_page[MAX_NUMNODES];
  84. #define SH2_AMO_CACHE_ENTRIES 4
  85. static inline int
  86. mspec_zero_block(unsigned long addr, int len)
  87. {
  88. int status;
  89. if (is_sn2) {
  90. if (is_shub2()) {
  91. int nid;
  92. void *p;
  93. int i;
  94. nid = nasid_to_cnodeid(get_node_number(__pa(addr)));
  95. p = (void *)TO_AMO(scratch_page[nid]);
  96. for (i=0; i < SH2_AMO_CACHE_ENTRIES; i++) {
  97. FETCHOP_LOAD_OP(p, FETCHOP_LOAD);
  98. p += FETCHOP_VAR_SIZE;
  99. }
  100. }
  101. status = bte_copy(0, addr & ~__IA64_UNCACHED_OFFSET, len,
  102. BTE_WACQUIRE | BTE_ZERO_FILL, NULL);
  103. } else {
  104. memset((char *) addr, 0, len);
  105. status = 0;
  106. }
  107. return status;
  108. }
  109. /*
  110. * mspec_open
  111. *
  112. * Called when a device mapping is created by a means other than mmap
  113. * (via fork, etc.). Increments the reference count on the underlying
  114. * mspec data so it is not freed prematurely.
  115. */
  116. static void
  117. mspec_open(struct vm_area_struct *vma)
  118. {
  119. struct vma_data *vdata;
  120. vdata = vma->vm_private_data;
  121. atomic_inc(&vdata->refcnt);
  122. }
  123. /*
  124. * mspec_close
  125. *
  126. * Called when unmapping a device mapping. Frees all mspec pages
  127. * belonging to the vma.
  128. */
  129. static void
  130. mspec_close(struct vm_area_struct *vma)
  131. {
  132. struct vma_data *vdata;
  133. int i, pages, result, vdata_size;
  134. vdata = vma->vm_private_data;
  135. if (!atomic_dec_and_test(&vdata->refcnt))
  136. return;
  137. pages = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
  138. vdata_size = sizeof(struct vma_data) + pages * sizeof(long);
  139. for (i = 0; i < pages; i++) {
  140. if (vdata->maddr[i] == 0)
  141. continue;
  142. /*
  143. * Clear the page before sticking it back
  144. * into the pool.
  145. */
  146. result = mspec_zero_block(vdata->maddr[i], PAGE_SIZE);
  147. if (!result)
  148. uncached_free_page(vdata->maddr[i]);
  149. else
  150. printk(KERN_WARNING "mspec_close(): "
  151. "failed to zero page %i\n",
  152. result);
  153. }
  154. if (vdata_size <= PAGE_SIZE)
  155. kfree(vdata);
  156. else
  157. vfree(vdata);
  158. }
  159. /*
  160. * mspec_nopfn
  161. *
  162. * Creates a mspec page and maps it to user space.
  163. */
  164. static unsigned long
  165. mspec_nopfn(struct vm_area_struct *vma, unsigned long address)
  166. {
  167. unsigned long paddr, maddr;
  168. unsigned long pfn;
  169. int index;
  170. struct vma_data *vdata = vma->vm_private_data;
  171. index = (address - vma->vm_start) >> PAGE_SHIFT;
  172. maddr = (volatile unsigned long) vdata->maddr[index];
  173. if (maddr == 0) {
  174. maddr = uncached_alloc_page(numa_node_id());
  175. if (maddr == 0)
  176. return NOPFN_OOM;
  177. spin_lock(&vdata->lock);
  178. if (vdata->maddr[index] == 0) {
  179. vdata->count++;
  180. vdata->maddr[index] = maddr;
  181. } else {
  182. uncached_free_page(maddr);
  183. maddr = vdata->maddr[index];
  184. }
  185. spin_unlock(&vdata->lock);
  186. }
  187. if (vdata->type == MSPEC_FETCHOP)
  188. paddr = TO_AMO(maddr);
  189. else
  190. paddr = __pa(TO_CAC(maddr));
  191. pfn = paddr >> PAGE_SHIFT;
  192. return pfn;
  193. }
  194. static struct vm_operations_struct mspec_vm_ops = {
  195. .open = mspec_open,
  196. .close = mspec_close,
  197. .nopfn = mspec_nopfn
  198. };
  199. /*
  200. * mspec_mmap
  201. *
  202. * Called when mmaping the device. Initializes the vma with a fault handler
  203. * and private data structure necessary to allocate, track, and free the
  204. * underlying pages.
  205. */
  206. static int
  207. mspec_mmap(struct file *file, struct vm_area_struct *vma, int type)
  208. {
  209. struct vma_data *vdata;
  210. int pages, vdata_size;
  211. if (vma->vm_pgoff != 0)
  212. return -EINVAL;
  213. if ((vma->vm_flags & VM_SHARED) == 0)
  214. return -EINVAL;
  215. if ((vma->vm_flags & VM_WRITE) == 0)
  216. return -EPERM;
  217. pages = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
  218. vdata_size = sizeof(struct vma_data) + pages * sizeof(long);
  219. if (vdata_size <= PAGE_SIZE)
  220. vdata = kmalloc(vdata_size, GFP_KERNEL);
  221. else
  222. vdata = vmalloc(vdata_size);
  223. if (!vdata)
  224. return -ENOMEM;
  225. memset(vdata, 0, vdata_size);
  226. vdata->type = type;
  227. spin_lock_init(&vdata->lock);
  228. vdata->refcnt = ATOMIC_INIT(1);
  229. vma->vm_private_data = vdata;
  230. vma->vm_flags |= (VM_IO | VM_LOCKED | VM_RESERVED | VM_PFNMAP);
  231. if (vdata->type == MSPEC_FETCHOP || vdata->type == MSPEC_UNCACHED)
  232. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  233. vma->vm_ops = &mspec_vm_ops;
  234. return 0;
  235. }
  236. static int
  237. fetchop_mmap(struct file *file, struct vm_area_struct *vma)
  238. {
  239. return mspec_mmap(file, vma, MSPEC_FETCHOP);
  240. }
  241. static int
  242. cached_mmap(struct file *file, struct vm_area_struct *vma)
  243. {
  244. return mspec_mmap(file, vma, MSPEC_CACHED);
  245. }
  246. static int
  247. uncached_mmap(struct file *file, struct vm_area_struct *vma)
  248. {
  249. return mspec_mmap(file, vma, MSPEC_UNCACHED);
  250. }
  251. static struct file_operations fetchop_fops = {
  252. .owner = THIS_MODULE,
  253. .mmap = fetchop_mmap
  254. };
  255. static struct miscdevice fetchop_miscdev = {
  256. .minor = MISC_DYNAMIC_MINOR,
  257. .name = "sgi_fetchop",
  258. .fops = &fetchop_fops
  259. };
  260. static struct file_operations cached_fops = {
  261. .owner = THIS_MODULE,
  262. .mmap = cached_mmap
  263. };
  264. static struct miscdevice cached_miscdev = {
  265. .minor = MISC_DYNAMIC_MINOR,
  266. .name = "mspec_cached",
  267. .fops = &cached_fops
  268. };
  269. static struct file_operations uncached_fops = {
  270. .owner = THIS_MODULE,
  271. .mmap = uncached_mmap
  272. };
  273. static struct miscdevice uncached_miscdev = {
  274. .minor = MISC_DYNAMIC_MINOR,
  275. .name = "mspec_uncached",
  276. .fops = &uncached_fops
  277. };
  278. /*
  279. * mspec_init
  280. *
  281. * Called at boot time to initialize the mspec facility.
  282. */
  283. static int __init
  284. mspec_init(void)
  285. {
  286. int ret;
  287. int nid;
  288. /*
  289. * The fetchop device only works on SN2 hardware, uncached and cached
  290. * memory drivers should both be valid on all ia64 hardware
  291. */
  292. if (ia64_platform_is("sn2")) {
  293. is_sn2 = 1;
  294. if (is_shub2()) {
  295. ret = -ENOMEM;
  296. for_each_online_node(nid) {
  297. int actual_nid;
  298. int nasid;
  299. unsigned long phys;
  300. scratch_page[nid] = uncached_alloc_page(nid);
  301. if (scratch_page[nid] == 0)
  302. goto free_scratch_pages;
  303. phys = __pa(scratch_page[nid]);
  304. nasid = get_node_number(phys);
  305. actual_nid = nasid_to_cnodeid(nasid);
  306. if (actual_nid != nid)
  307. goto free_scratch_pages;
  308. }
  309. }
  310. ret = misc_register(&fetchop_miscdev);
  311. if (ret) {
  312. printk(KERN_ERR
  313. "%s: failed to register device %i\n",
  314. FETCHOP_ID, ret);
  315. goto free_scratch_pages;
  316. }
  317. }
  318. ret = misc_register(&cached_miscdev);
  319. if (ret) {
  320. printk(KERN_ERR "%s: failed to register device %i\n",
  321. CACHED_ID, ret);
  322. if (is_sn2)
  323. misc_deregister(&fetchop_miscdev);
  324. goto free_scratch_pages;
  325. }
  326. ret = misc_register(&uncached_miscdev);
  327. if (ret) {
  328. printk(KERN_ERR "%s: failed to register device %i\n",
  329. UNCACHED_ID, ret);
  330. misc_deregister(&cached_miscdev);
  331. if (is_sn2)
  332. misc_deregister(&fetchop_miscdev);
  333. goto free_scratch_pages;
  334. }
  335. printk(KERN_INFO "%s %s initialized devices: %s %s %s\n",
  336. MSPEC_BASENAME, REVISION, is_sn2 ? FETCHOP_ID : "",
  337. CACHED_ID, UNCACHED_ID);
  338. return 0;
  339. free_scratch_pages:
  340. for_each_node(nid) {
  341. if (scratch_page[nid] != 0)
  342. uncached_free_page(scratch_page[nid]);
  343. }
  344. return ret;
  345. }
  346. static void __exit
  347. mspec_exit(void)
  348. {
  349. int nid;
  350. misc_deregister(&uncached_miscdev);
  351. misc_deregister(&cached_miscdev);
  352. if (is_sn2) {
  353. misc_deregister(&fetchop_miscdev);
  354. for_each_node(nid) {
  355. if (scratch_page[nid] != 0)
  356. uncached_free_page(scratch_page[nid]);
  357. }
  358. }
  359. }
  360. module_init(mspec_init);
  361. module_exit(mspec_exit);
  362. MODULE_AUTHOR("Silicon Graphics, Inc. <linux-altix@sgi.com>");
  363. MODULE_DESCRIPTION("Driver for SGI SN special memory operations");
  364. MODULE_LICENSE("GPL");