mspec.c 9.7 KB

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