mspec.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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);
  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_nopfn
  175. *
  176. * Creates a mspec page and maps it to user space.
  177. */
  178. static unsigned long
  179. mspec_nopfn(struct vm_area_struct *vma, unsigned long address)
  180. {
  181. unsigned long paddr, maddr;
  182. unsigned long pfn;
  183. int index;
  184. struct vma_data *vdata = vma->vm_private_data;
  185. BUG_ON(address < vdata->vm_start || address >= vdata->vm_end);
  186. index = (address - vdata->vm_start) >> PAGE_SHIFT;
  187. maddr = (volatile unsigned long) vdata->maddr[index];
  188. if (maddr == 0) {
  189. maddr = uncached_alloc_page(numa_node_id());
  190. if (maddr == 0)
  191. return NOPFN_OOM;
  192. spin_lock(&vdata->lock);
  193. if (vdata->maddr[index] == 0) {
  194. vdata->count++;
  195. vdata->maddr[index] = maddr;
  196. } else {
  197. uncached_free_page(maddr);
  198. maddr = vdata->maddr[index];
  199. }
  200. spin_unlock(&vdata->lock);
  201. }
  202. if (vdata->type == MSPEC_FETCHOP)
  203. paddr = TO_AMO(maddr);
  204. else
  205. paddr = maddr & ~__IA64_UNCACHED_OFFSET;
  206. pfn = paddr >> PAGE_SHIFT;
  207. return pfn;
  208. }
  209. static struct vm_operations_struct mspec_vm_ops = {
  210. .open = mspec_open,
  211. .close = mspec_close,
  212. .nopfn = mspec_nopfn
  213. };
  214. /*
  215. * mspec_mmap
  216. *
  217. * Called when mmaping the device. Initializes the vma with a fault handler
  218. * and private data structure necessary to allocate, track, and free the
  219. * underlying pages.
  220. */
  221. static int
  222. mspec_mmap(struct file *file, struct vm_area_struct *vma,
  223. enum mspec_page_type type)
  224. {
  225. struct vma_data *vdata;
  226. int pages, vdata_size, flags = 0;
  227. if (vma->vm_pgoff != 0)
  228. return -EINVAL;
  229. if ((vma->vm_flags & VM_SHARED) == 0)
  230. return -EINVAL;
  231. if ((vma->vm_flags & VM_WRITE) == 0)
  232. return -EPERM;
  233. pages = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
  234. vdata_size = sizeof(struct vma_data) + pages * sizeof(long);
  235. if (vdata_size <= PAGE_SIZE)
  236. vdata = kmalloc(vdata_size, GFP_KERNEL);
  237. else {
  238. vdata = vmalloc(vdata_size);
  239. flags = VMD_VMALLOCED;
  240. }
  241. if (!vdata)
  242. return -ENOMEM;
  243. memset(vdata, 0, vdata_size);
  244. vdata->vm_start = vma->vm_start;
  245. vdata->vm_end = vma->vm_end;
  246. vdata->flags = flags;
  247. vdata->type = type;
  248. spin_lock_init(&vdata->lock);
  249. vdata->refcnt = ATOMIC_INIT(1);
  250. vma->vm_private_data = vdata;
  251. vma->vm_flags |= (VM_IO | VM_RESERVED | VM_PFNMAP);
  252. if (vdata->type == MSPEC_FETCHOP || vdata->type == MSPEC_UNCACHED)
  253. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  254. vma->vm_ops = &mspec_vm_ops;
  255. return 0;
  256. }
  257. static int
  258. fetchop_mmap(struct file *file, struct vm_area_struct *vma)
  259. {
  260. return mspec_mmap(file, vma, MSPEC_FETCHOP);
  261. }
  262. static int
  263. cached_mmap(struct file *file, struct vm_area_struct *vma)
  264. {
  265. return mspec_mmap(file, vma, MSPEC_CACHED);
  266. }
  267. static int
  268. uncached_mmap(struct file *file, struct vm_area_struct *vma)
  269. {
  270. return mspec_mmap(file, vma, MSPEC_UNCACHED);
  271. }
  272. static const struct file_operations fetchop_fops = {
  273. .owner = THIS_MODULE,
  274. .mmap = fetchop_mmap
  275. };
  276. static struct miscdevice fetchop_miscdev = {
  277. .minor = MISC_DYNAMIC_MINOR,
  278. .name = "sgi_fetchop",
  279. .fops = &fetchop_fops
  280. };
  281. static const struct file_operations cached_fops = {
  282. .owner = THIS_MODULE,
  283. .mmap = cached_mmap
  284. };
  285. static struct miscdevice cached_miscdev = {
  286. .minor = MISC_DYNAMIC_MINOR,
  287. .name = "mspec_cached",
  288. .fops = &cached_fops
  289. };
  290. static const struct file_operations uncached_fops = {
  291. .owner = THIS_MODULE,
  292. .mmap = uncached_mmap
  293. };
  294. static struct miscdevice uncached_miscdev = {
  295. .minor = MISC_DYNAMIC_MINOR,
  296. .name = "mspec_uncached",
  297. .fops = &uncached_fops
  298. };
  299. /*
  300. * mspec_init
  301. *
  302. * Called at boot time to initialize the mspec facility.
  303. */
  304. static int __init
  305. mspec_init(void)
  306. {
  307. int ret;
  308. int nid;
  309. /*
  310. * The fetchop device only works on SN2 hardware, uncached and cached
  311. * memory drivers should both be valid on all ia64 hardware
  312. */
  313. #ifdef CONFIG_SGI_SN
  314. if (ia64_platform_is("sn2")) {
  315. is_sn2 = 1;
  316. if (is_shub2()) {
  317. ret = -ENOMEM;
  318. for_each_node_state(nid, N_ONLINE) {
  319. int actual_nid;
  320. int nasid;
  321. unsigned long phys;
  322. scratch_page[nid] = uncached_alloc_page(nid);
  323. if (scratch_page[nid] == 0)
  324. goto free_scratch_pages;
  325. phys = __pa(scratch_page[nid]);
  326. nasid = get_node_number(phys);
  327. actual_nid = nasid_to_cnodeid(nasid);
  328. if (actual_nid != nid)
  329. goto free_scratch_pages;
  330. }
  331. }
  332. ret = misc_register(&fetchop_miscdev);
  333. if (ret) {
  334. printk(KERN_ERR
  335. "%s: failed to register device %i\n",
  336. FETCHOP_ID, ret);
  337. goto free_scratch_pages;
  338. }
  339. }
  340. #endif
  341. ret = misc_register(&cached_miscdev);
  342. if (ret) {
  343. printk(KERN_ERR "%s: failed to register device %i\n",
  344. CACHED_ID, ret);
  345. if (is_sn2)
  346. misc_deregister(&fetchop_miscdev);
  347. goto free_scratch_pages;
  348. }
  349. ret = misc_register(&uncached_miscdev);
  350. if (ret) {
  351. printk(KERN_ERR "%s: failed to register device %i\n",
  352. UNCACHED_ID, ret);
  353. misc_deregister(&cached_miscdev);
  354. if (is_sn2)
  355. misc_deregister(&fetchop_miscdev);
  356. goto free_scratch_pages;
  357. }
  358. printk(KERN_INFO "%s %s initialized devices: %s %s %s\n",
  359. MSPEC_BASENAME, REVISION, is_sn2 ? FETCHOP_ID : "",
  360. CACHED_ID, UNCACHED_ID);
  361. return 0;
  362. free_scratch_pages:
  363. for_each_node(nid) {
  364. if (scratch_page[nid] != 0)
  365. uncached_free_page(scratch_page[nid]);
  366. }
  367. return ret;
  368. }
  369. static void __exit
  370. mspec_exit(void)
  371. {
  372. int nid;
  373. misc_deregister(&uncached_miscdev);
  374. misc_deregister(&cached_miscdev);
  375. if (is_sn2) {
  376. misc_deregister(&fetchop_miscdev);
  377. for_each_node(nid) {
  378. if (scratch_page[nid] != 0)
  379. uncached_free_page(scratch_page[nid]);
  380. }
  381. }
  382. }
  383. module_init(mspec_init);
  384. module_exit(mspec_exit);
  385. MODULE_AUTHOR("Silicon Graphics, Inc. <linux-altix@sgi.com>");
  386. MODULE_DESCRIPTION("Driver for SGI SN special memory operations");
  387. MODULE_LICENSE("GPL");