mspec.c 11 KB

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