grufile.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. /*
  2. * SN Platform GRU Driver
  3. *
  4. * FILE OPERATIONS & DRIVER INITIALIZATION
  5. *
  6. * This file supports the user system call for file open, close, mmap, etc.
  7. * This also incudes the driver initialization code.
  8. *
  9. * Copyright (c) 2008 Silicon Graphics, Inc. All Rights Reserved.
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  24. */
  25. #include <linux/module.h>
  26. #include <linux/kernel.h>
  27. #include <linux/errno.h>
  28. #include <linux/slab.h>
  29. #include <linux/mm.h>
  30. #include <linux/io.h>
  31. #include <linux/smp_lock.h>
  32. #include <linux/spinlock.h>
  33. #include <linux/device.h>
  34. #include <linux/miscdevice.h>
  35. #include <linux/interrupt.h>
  36. #include <linux/proc_fs.h>
  37. #include <linux/uaccess.h>
  38. #include <asm/uv/uv.h>
  39. #include "gru.h"
  40. #include "grulib.h"
  41. #include "grutables.h"
  42. #include <asm/uv/uv_hub.h>
  43. #include <asm/uv/uv_mmrs.h>
  44. struct gru_blade_state *gru_base[GRU_MAX_BLADES] __read_mostly;
  45. unsigned long gru_start_paddr __read_mostly;
  46. unsigned long gru_end_paddr __read_mostly;
  47. unsigned int gru_max_gids __read_mostly;
  48. struct gru_stats_s gru_stats;
  49. /* Guaranteed user available resources on each node */
  50. static int max_user_cbrs, max_user_dsr_bytes;
  51. static struct file_operations gru_fops;
  52. static struct miscdevice gru_miscdev;
  53. /*
  54. * gru_vma_close
  55. *
  56. * Called when unmapping a device mapping. Frees all gru resources
  57. * and tables belonging to the vma.
  58. */
  59. static void gru_vma_close(struct vm_area_struct *vma)
  60. {
  61. struct gru_vma_data *vdata;
  62. struct gru_thread_state *gts;
  63. struct list_head *entry, *next;
  64. if (!vma->vm_private_data)
  65. return;
  66. vdata = vma->vm_private_data;
  67. vma->vm_private_data = NULL;
  68. gru_dbg(grudev, "vma %p, file %p, vdata %p\n", vma, vma->vm_file,
  69. vdata);
  70. list_for_each_safe(entry, next, &vdata->vd_head) {
  71. gts =
  72. list_entry(entry, struct gru_thread_state, ts_next);
  73. list_del(&gts->ts_next);
  74. mutex_lock(&gts->ts_ctxlock);
  75. if (gts->ts_gru)
  76. gru_unload_context(gts, 0);
  77. mutex_unlock(&gts->ts_ctxlock);
  78. gts_drop(gts);
  79. }
  80. kfree(vdata);
  81. STAT(vdata_free);
  82. }
  83. /*
  84. * gru_file_mmap
  85. *
  86. * Called when mmaping the device. Initializes the vma with a fault handler
  87. * and private data structure necessary to allocate, track, and free the
  88. * underlying pages.
  89. */
  90. static int gru_file_mmap(struct file *file, struct vm_area_struct *vma)
  91. {
  92. if ((vma->vm_flags & (VM_SHARED | VM_WRITE)) != (VM_SHARED | VM_WRITE))
  93. return -EPERM;
  94. if (vma->vm_start & (GRU_GSEG_PAGESIZE - 1) ||
  95. vma->vm_end & (GRU_GSEG_PAGESIZE - 1))
  96. return -EINVAL;
  97. vma->vm_flags |=
  98. (VM_IO | VM_DONTCOPY | VM_LOCKED | VM_DONTEXPAND | VM_PFNMAP |
  99. VM_RESERVED);
  100. vma->vm_page_prot = PAGE_SHARED;
  101. vma->vm_ops = &gru_vm_ops;
  102. vma->vm_private_data = gru_alloc_vma_data(vma, 0);
  103. if (!vma->vm_private_data)
  104. return -ENOMEM;
  105. gru_dbg(grudev, "file %p, vaddr 0x%lx, vma %p, vdata %p\n",
  106. file, vma->vm_start, vma, vma->vm_private_data);
  107. return 0;
  108. }
  109. /*
  110. * Create a new GRU context
  111. */
  112. static int gru_create_new_context(unsigned long arg)
  113. {
  114. struct gru_create_context_req req;
  115. struct vm_area_struct *vma;
  116. struct gru_vma_data *vdata;
  117. int ret = -EINVAL;
  118. if (copy_from_user(&req, (void __user *)arg, sizeof(req)))
  119. return -EFAULT;
  120. if (req.data_segment_bytes > max_user_dsr_bytes)
  121. return -EINVAL;
  122. if (req.control_blocks > max_user_cbrs || !req.maximum_thread_count)
  123. return -EINVAL;
  124. if (!(req.options & GRU_OPT_MISS_MASK))
  125. req.options |= GRU_OPT_MISS_FMM_INTR;
  126. down_write(&current->mm->mmap_sem);
  127. vma = gru_find_vma(req.gseg);
  128. if (vma) {
  129. vdata = vma->vm_private_data;
  130. vdata->vd_user_options = req.options;
  131. vdata->vd_dsr_au_count =
  132. GRU_DS_BYTES_TO_AU(req.data_segment_bytes);
  133. vdata->vd_cbr_au_count = GRU_CB_COUNT_TO_AU(req.control_blocks);
  134. ret = 0;
  135. }
  136. up_write(&current->mm->mmap_sem);
  137. return ret;
  138. }
  139. /*
  140. * Get GRU configuration info (temp - for emulator testing)
  141. */
  142. static long gru_get_config_info(unsigned long arg)
  143. {
  144. struct gru_config_info info;
  145. int nodesperblade;
  146. if (num_online_nodes() > 1 &&
  147. (uv_node_to_blade_id(1) == uv_node_to_blade_id(0)))
  148. nodesperblade = 2;
  149. else
  150. nodesperblade = 1;
  151. info.cpus = num_online_cpus();
  152. info.nodes = num_online_nodes();
  153. info.blades = info.nodes / nodesperblade;
  154. info.chiplets = GRU_CHIPLETS_PER_BLADE * info.blades;
  155. if (copy_to_user((void __user *)arg, &info, sizeof(info)))
  156. return -EFAULT;
  157. return 0;
  158. }
  159. /*
  160. * Get GRU chiplet status
  161. */
  162. static long gru_get_chiplet_status(unsigned long arg)
  163. {
  164. struct gru_state *gru;
  165. struct gru_chiplet_info info;
  166. if (copy_from_user(&info, (void __user *)arg, sizeof(info)))
  167. return -EFAULT;
  168. if (info.node == -1)
  169. info.node = numa_node_id();
  170. if (info.node >= num_possible_nodes() ||
  171. info.chiplet >= GRU_CHIPLETS_PER_HUB ||
  172. info.node < 0 || info.chiplet < 0)
  173. return -EINVAL;
  174. info.blade = uv_node_to_blade_id(info.node);
  175. gru = get_gru(info.blade, info.chiplet);
  176. info.total_dsr_bytes = GRU_NUM_DSR_BYTES;
  177. info.total_cbr = GRU_NUM_CB;
  178. info.total_user_dsr_bytes = GRU_NUM_DSR_BYTES -
  179. gru->gs_reserved_dsr_bytes;
  180. info.total_user_cbr = GRU_NUM_CB - gru->gs_reserved_cbrs;
  181. info.free_user_dsr_bytes = hweight64(gru->gs_dsr_map) *
  182. GRU_DSR_AU_BYTES;
  183. info.free_user_cbr = hweight64(gru->gs_cbr_map) * GRU_CBR_AU_SIZE;
  184. if (copy_to_user((void __user *)arg, &info, sizeof(info)))
  185. return -EFAULT;
  186. return 0;
  187. }
  188. /*
  189. * gru_file_unlocked_ioctl
  190. *
  191. * Called to update file attributes via IOCTL calls.
  192. */
  193. static long gru_file_unlocked_ioctl(struct file *file, unsigned int req,
  194. unsigned long arg)
  195. {
  196. int err = -EBADRQC;
  197. gru_dbg(grudev, "file %p\n", file);
  198. switch (req) {
  199. case GRU_CREATE_CONTEXT:
  200. err = gru_create_new_context(arg);
  201. break;
  202. case GRU_SET_TASK_SLICE:
  203. err = gru_set_task_slice(arg);
  204. break;
  205. case GRU_USER_GET_EXCEPTION_DETAIL:
  206. err = gru_get_exception_detail(arg);
  207. break;
  208. case GRU_USER_UNLOAD_CONTEXT:
  209. err = gru_user_unload_context(arg);
  210. break;
  211. case GRU_GET_CHIPLET_STATUS:
  212. err = gru_get_chiplet_status(arg);
  213. break;
  214. case GRU_USER_FLUSH_TLB:
  215. err = gru_user_flush_tlb(arg);
  216. break;
  217. case GRU_USER_CALL_OS:
  218. err = gru_handle_user_call_os(arg);
  219. break;
  220. case GRU_KTEST:
  221. err = gru_ktest(arg);
  222. break;
  223. case GRU_GET_CONFIG_INFO:
  224. err = gru_get_config_info(arg);
  225. break;
  226. case GRU_DUMP_CHIPLET_STATE:
  227. err = gru_dump_chiplet_request(arg);
  228. break;
  229. }
  230. return err;
  231. }
  232. /*
  233. * Called at init time to build tables for all GRUs that are present in the
  234. * system.
  235. */
  236. static void gru_init_chiplet(struct gru_state *gru, unsigned long paddr,
  237. void *vaddr, int nid, int bid, int grunum)
  238. {
  239. spin_lock_init(&gru->gs_lock);
  240. spin_lock_init(&gru->gs_asid_lock);
  241. gru->gs_gru_base_paddr = paddr;
  242. gru->gs_gru_base_vaddr = vaddr;
  243. gru->gs_gid = bid * GRU_CHIPLETS_PER_BLADE + grunum;
  244. gru->gs_blade = gru_base[bid];
  245. gru->gs_blade_id = bid;
  246. gru->gs_cbr_map = (GRU_CBR_AU == 64) ? ~0 : (1UL << GRU_CBR_AU) - 1;
  247. gru->gs_dsr_map = (1UL << GRU_DSR_AU) - 1;
  248. gru->gs_asid_limit = MAX_ASID;
  249. gru_tgh_flush_init(gru);
  250. if (gru->gs_gid >= gru_max_gids)
  251. gru_max_gids = gru->gs_gid + 1;
  252. gru_dbg(grudev, "bid %d, nid %d, gid %d, vaddr %p (0x%lx)\n",
  253. bid, nid, gru->gs_gid, gru->gs_gru_base_vaddr,
  254. gru->gs_gru_base_paddr);
  255. gru_kservices_init(gru);
  256. }
  257. static int gru_init_tables(unsigned long gru_base_paddr, void *gru_base_vaddr)
  258. {
  259. int pnode, nid, bid, chip;
  260. int cbrs, dsrbytes, n;
  261. int order = get_order(sizeof(struct gru_blade_state));
  262. struct page *page;
  263. struct gru_state *gru;
  264. unsigned long paddr;
  265. void *vaddr;
  266. max_user_cbrs = GRU_NUM_CB;
  267. max_user_dsr_bytes = GRU_NUM_DSR_BYTES;
  268. for_each_online_node(nid) {
  269. bid = uv_node_to_blade_id(nid);
  270. pnode = uv_node_to_pnode(nid);
  271. if (bid < 0 || gru_base[bid])
  272. continue;
  273. page = alloc_pages_exact_node(nid, GFP_KERNEL, order);
  274. if (!page)
  275. goto fail;
  276. gru_base[bid] = page_address(page);
  277. memset(gru_base[bid], 0, sizeof(struct gru_blade_state));
  278. gru_base[bid]->bs_lru_gru = &gru_base[bid]->bs_grus[0];
  279. spin_lock_init(&gru_base[bid]->bs_lock);
  280. dsrbytes = 0;
  281. cbrs = 0;
  282. for (gru = gru_base[bid]->bs_grus, chip = 0;
  283. chip < GRU_CHIPLETS_PER_BLADE;
  284. chip++, gru++) {
  285. paddr = gru_chiplet_paddr(gru_base_paddr, pnode, chip);
  286. vaddr = gru_chiplet_vaddr(gru_base_vaddr, pnode, chip);
  287. gru_init_chiplet(gru, paddr, vaddr, nid, bid, chip);
  288. n = hweight64(gru->gs_cbr_map) * GRU_CBR_AU_SIZE;
  289. cbrs = max(cbrs, n);
  290. n = hweight64(gru->gs_dsr_map) * GRU_DSR_AU_BYTES;
  291. dsrbytes = max(dsrbytes, n);
  292. }
  293. max_user_cbrs = min(max_user_cbrs, cbrs);
  294. max_user_dsr_bytes = min(max_user_dsr_bytes, dsrbytes);
  295. }
  296. return 0;
  297. fail:
  298. for (nid--; nid >= 0; nid--)
  299. free_pages((unsigned long)gru_base[nid], order);
  300. return -ENOMEM;
  301. }
  302. #ifdef CONFIG_IA64
  303. static int get_base_irq(void)
  304. {
  305. return IRQ_GRU;
  306. }
  307. #elif defined CONFIG_X86_64
  308. static void noop(unsigned int irq)
  309. {
  310. }
  311. static struct irq_chip gru_chip = {
  312. .name = "gru",
  313. .mask = noop,
  314. .unmask = noop,
  315. .ack = noop,
  316. };
  317. static int get_base_irq(void)
  318. {
  319. set_irq_chip(IRQ_GRU, &gru_chip);
  320. set_irq_chip(IRQ_GRU + 1, &gru_chip);
  321. return IRQ_GRU;
  322. }
  323. #endif
  324. /*
  325. * gru_init
  326. *
  327. * Called at boot or module load time to initialize the GRUs.
  328. */
  329. static int __init gru_init(void)
  330. {
  331. int ret, irq, chip;
  332. char id[10];
  333. void *gru_start_vaddr;
  334. if (!is_uv_system())
  335. return 0;
  336. #if defined CONFIG_IA64
  337. gru_start_paddr = 0xd000000000UL; /* ZZZZZZZZZZZZZZZZZZZ fixme */
  338. #else
  339. gru_start_paddr = uv_read_local_mmr(UVH_RH_GAM_GRU_OVERLAY_CONFIG_MMR) &
  340. 0x7fffffffffffUL;
  341. #endif
  342. gru_start_vaddr = __va(gru_start_paddr);
  343. gru_end_paddr = gru_start_paddr + GRU_MAX_BLADES * GRU_SIZE;
  344. printk(KERN_INFO "GRU space: 0x%lx - 0x%lx\n",
  345. gru_start_paddr, gru_end_paddr);
  346. irq = get_base_irq();
  347. for (chip = 0; chip < GRU_CHIPLETS_PER_BLADE; chip++) {
  348. ret = request_irq(irq + chip, gru_intr, 0, id, NULL);
  349. /* TODO: fix irq handling on x86. For now ignore failure because
  350. * interrupts are not required & not yet fully supported */
  351. if (ret) {
  352. printk(KERN_WARNING
  353. "!!!WARNING: GRU ignoring request failure!!!\n");
  354. ret = 0;
  355. }
  356. if (ret) {
  357. printk(KERN_ERR "%s: request_irq failed\n",
  358. GRU_DRIVER_ID_STR);
  359. goto exit1;
  360. }
  361. }
  362. ret = misc_register(&gru_miscdev);
  363. if (ret) {
  364. printk(KERN_ERR "%s: misc_register failed\n",
  365. GRU_DRIVER_ID_STR);
  366. goto exit1;
  367. }
  368. ret = gru_proc_init();
  369. if (ret) {
  370. printk(KERN_ERR "%s: proc init failed\n", GRU_DRIVER_ID_STR);
  371. goto exit2;
  372. }
  373. ret = gru_init_tables(gru_start_paddr, gru_start_vaddr);
  374. if (ret) {
  375. printk(KERN_ERR "%s: init tables failed\n", GRU_DRIVER_ID_STR);
  376. goto exit3;
  377. }
  378. printk(KERN_INFO "%s: v%s\n", GRU_DRIVER_ID_STR,
  379. GRU_DRIVER_VERSION_STR);
  380. return 0;
  381. exit3:
  382. gru_proc_exit();
  383. exit2:
  384. misc_deregister(&gru_miscdev);
  385. exit1:
  386. for (--chip; chip >= 0; chip--)
  387. free_irq(irq + chip, NULL);
  388. return ret;
  389. }
  390. static void __exit gru_exit(void)
  391. {
  392. int i, bid, gid;
  393. int order = get_order(sizeof(struct gru_state) *
  394. GRU_CHIPLETS_PER_BLADE);
  395. if (!is_uv_system())
  396. return;
  397. for (i = 0; i < GRU_CHIPLETS_PER_BLADE; i++)
  398. free_irq(IRQ_GRU + i, NULL);
  399. foreach_gid(gid)
  400. gru_kservices_exit(GID_TO_GRU(gid));
  401. for (bid = 0; bid < GRU_MAX_BLADES; bid++)
  402. free_pages((unsigned long)gru_base[bid], order);
  403. misc_deregister(&gru_miscdev);
  404. gru_proc_exit();
  405. }
  406. static struct file_operations gru_fops = {
  407. .owner = THIS_MODULE,
  408. .unlocked_ioctl = gru_file_unlocked_ioctl,
  409. .mmap = gru_file_mmap,
  410. };
  411. static struct miscdevice gru_miscdev = {
  412. .minor = MISC_DYNAMIC_MINOR,
  413. .name = "gru",
  414. .fops = &gru_fops,
  415. };
  416. struct vm_operations_struct gru_vm_ops = {
  417. .close = gru_vma_close,
  418. .fault = gru_fault,
  419. };
  420. #ifndef MODULE
  421. fs_initcall(gru_init);
  422. #else
  423. module_init(gru_init);
  424. #endif
  425. module_exit(gru_exit);
  426. module_param(gru_options, ulong, 0644);
  427. MODULE_PARM_DESC(gru_options, "Various debug options");
  428. MODULE_AUTHOR("Silicon Graphics, Inc.");
  429. MODULE_LICENSE("GPL");
  430. MODULE_DESCRIPTION(GRU_DRIVER_ID_STR GRU_DRIVER_VERSION_STR);
  431. MODULE_VERSION(GRU_DRIVER_VERSION_STR);