mspec.c 11 KB

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