blob: bfc88d1b2a5bcae8c738537ad3ce78cd865ea73b [file] [log] [blame]
Jack Steiner78cf1de2008-07-29 22:33:56 -07001/*
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
26#include <linux/module.h>
27#include <linux/kernel.h>
28#include <linux/errno.h>
29#include <linux/slab.h>
30#include <linux/mm.h>
31#include <linux/io.h>
32#include <linux/smp_lock.h>
33#include <linux/spinlock.h>
34#include <linux/device.h>
35#include <linux/miscdevice.h>
36#include <linux/interrupt.h>
37#include <linux/proc_fs.h>
38#include <linux/uaccess.h>
Dean Nelson1c004002009-02-09 10:25:20 -060039#include <asm/uv/uv.h>
Jack Steiner78cf1de2008-07-29 22:33:56 -070040#include "gru.h"
41#include "grulib.h"
42#include "grutables.h"
43
Jack Steiner78cf1de2008-07-29 22:33:56 -070044#include <asm/uv/uv_hub.h>
45#include <asm/uv/uv_mmrs.h>
46
47struct gru_blade_state *gru_base[GRU_MAX_BLADES] __read_mostly;
Jack Steinerfe5bb6b2009-04-02 16:59:04 -070048unsigned long gru_start_paddr __read_mostly;
Jack Steiner1a2c09e2009-06-17 16:28:28 -070049void *gru_start_vaddr __read_mostly;
Jack Steinerfe5bb6b2009-04-02 16:59:04 -070050unsigned long gru_end_paddr __read_mostly;
Jack Steinere1c32192009-04-02 16:59:10 -070051unsigned int gru_max_gids __read_mostly;
Jack Steiner78cf1de2008-07-29 22:33:56 -070052struct gru_stats_s gru_stats;
53
54/* Guaranteed user available resources on each node */
55static int max_user_cbrs, max_user_dsr_bytes;
56
57static struct file_operations gru_fops;
58static struct miscdevice gru_miscdev;
59
60
61/*
62 * gru_vma_close
63 *
64 * Called when unmapping a device mapping. Frees all gru resources
65 * and tables belonging to the vma.
66 */
67static void gru_vma_close(struct vm_area_struct *vma)
68{
69 struct gru_vma_data *vdata;
70 struct gru_thread_state *gts;
71 struct list_head *entry, *next;
72
73 if (!vma->vm_private_data)
74 return;
75
76 vdata = vma->vm_private_data;
77 vma->vm_private_data = NULL;
78 gru_dbg(grudev, "vma %p, file %p, vdata %p\n", vma, vma->vm_file,
79 vdata);
80 list_for_each_safe(entry, next, &vdata->vd_head) {
81 gts =
82 list_entry(entry, struct gru_thread_state, ts_next);
83 list_del(&gts->ts_next);
84 mutex_lock(&gts->ts_ctxlock);
85 if (gts->ts_gru)
86 gru_unload_context(gts, 0);
87 mutex_unlock(&gts->ts_ctxlock);
88 gts_drop(gts);
89 }
90 kfree(vdata);
91 STAT(vdata_free);
92}
93
94/*
95 * gru_file_mmap
96 *
97 * Called when mmaping the device. Initializes the vma with a fault handler
98 * and private data structure necessary to allocate, track, and free the
99 * underlying pages.
100 */
101static int gru_file_mmap(struct file *file, struct vm_area_struct *vma)
102{
103 if ((vma->vm_flags & (VM_SHARED | VM_WRITE)) != (VM_SHARED | VM_WRITE))
104 return -EPERM;
105
Jack Steiner9ca8e40c12008-07-29 22:34:02 -0700106 if (vma->vm_start & (GRU_GSEG_PAGESIZE - 1) ||
Jack Steinerfe5bb6b2009-04-02 16:59:04 -0700107 vma->vm_end & (GRU_GSEG_PAGESIZE - 1))
Jack Steiner9ca8e40c12008-07-29 22:34:02 -0700108 return -EINVAL;
109
Jack Steiner78cf1de2008-07-29 22:33:56 -0700110 vma->vm_flags |=
111 (VM_IO | VM_DONTCOPY | VM_LOCKED | VM_DONTEXPAND | VM_PFNMAP |
112 VM_RESERVED);
113 vma->vm_page_prot = PAGE_SHARED;
114 vma->vm_ops = &gru_vm_ops;
115
116 vma->vm_private_data = gru_alloc_vma_data(vma, 0);
117 if (!vma->vm_private_data)
118 return -ENOMEM;
119
120 gru_dbg(grudev, "file %p, vaddr 0x%lx, vma %p, vdata %p\n",
121 file, vma->vm_start, vma, vma->vm_private_data);
122 return 0;
123}
124
125/*
126 * Create a new GRU context
127 */
128static int gru_create_new_context(unsigned long arg)
129{
130 struct gru_create_context_req req;
131 struct vm_area_struct *vma;
132 struct gru_vma_data *vdata;
133 int ret = -EINVAL;
134
135
136 if (copy_from_user(&req, (void __user *)arg, sizeof(req)))
137 return -EFAULT;
138
Jack Steiner3eac2e92009-06-17 16:28:23 -0700139 if (req.data_segment_bytes > max_user_dsr_bytes)
Jack Steiner78cf1de2008-07-29 22:33:56 -0700140 return -EINVAL;
Jack Steiner3eac2e92009-06-17 16:28:23 -0700141 if (req.control_blocks > max_user_cbrs || !req.maximum_thread_count)
Jack Steiner78cf1de2008-07-29 22:33:56 -0700142 return -EINVAL;
143
144 if (!(req.options & GRU_OPT_MISS_MASK))
145 req.options |= GRU_OPT_MISS_FMM_INTR;
146
147 down_write(&current->mm->mmap_sem);
148 vma = gru_find_vma(req.gseg);
149 if (vma) {
150 vdata = vma->vm_private_data;
151 vdata->vd_user_options = req.options;
152 vdata->vd_dsr_au_count =
153 GRU_DS_BYTES_TO_AU(req.data_segment_bytes);
154 vdata->vd_cbr_au_count = GRU_CB_COUNT_TO_AU(req.control_blocks);
155 ret = 0;
156 }
157 up_write(&current->mm->mmap_sem);
158
159 return ret;
160}
161
162/*
163 * Get GRU configuration info (temp - for emulator testing)
164 */
165static long gru_get_config_info(unsigned long arg)
166{
167 struct gru_config_info info;
168 int nodesperblade;
169
170 if (num_online_nodes() > 1 &&
171 (uv_node_to_blade_id(1) == uv_node_to_blade_id(0)))
172 nodesperblade = 2;
173 else
174 nodesperblade = 1;
175 info.cpus = num_online_cpus();
176 info.nodes = num_online_nodes();
177 info.blades = info.nodes / nodesperblade;
178 info.chiplets = GRU_CHIPLETS_PER_BLADE * info.blades;
179
180 if (copy_to_user((void __user *)arg, &info, sizeof(info)))
181 return -EFAULT;
182 return 0;
183}
184
185/*
186 * Get GRU chiplet status
187 */
188static long gru_get_chiplet_status(unsigned long arg)
189{
190 struct gru_state *gru;
191 struct gru_chiplet_info info;
192
193 if (copy_from_user(&info, (void __user *)arg, sizeof(info)))
194 return -EFAULT;
195
196 if (info.node == -1)
197 info.node = numa_node_id();
198 if (info.node >= num_possible_nodes() ||
199 info.chiplet >= GRU_CHIPLETS_PER_HUB ||
200 info.node < 0 || info.chiplet < 0)
201 return -EINVAL;
202
203 info.blade = uv_node_to_blade_id(info.node);
204 gru = get_gru(info.blade, info.chiplet);
205
206 info.total_dsr_bytes = GRU_NUM_DSR_BYTES;
207 info.total_cbr = GRU_NUM_CB;
208 info.total_user_dsr_bytes = GRU_NUM_DSR_BYTES -
209 gru->gs_reserved_dsr_bytes;
210 info.total_user_cbr = GRU_NUM_CB - gru->gs_reserved_cbrs;
211 info.free_user_dsr_bytes = hweight64(gru->gs_dsr_map) *
212 GRU_DSR_AU_BYTES;
213 info.free_user_cbr = hweight64(gru->gs_cbr_map) * GRU_CBR_AU_SIZE;
214
215 if (copy_to_user((void __user *)arg, &info, sizeof(info)))
216 return -EFAULT;
217 return 0;
218}
219
220/*
221 * gru_file_unlocked_ioctl
222 *
223 * Called to update file attributes via IOCTL calls.
224 */
225static long gru_file_unlocked_ioctl(struct file *file, unsigned int req,
226 unsigned long arg)
227{
228 int err = -EBADRQC;
229
230 gru_dbg(grudev, "file %p\n", file);
231
232 switch (req) {
233 case GRU_CREATE_CONTEXT:
234 err = gru_create_new_context(arg);
235 break;
236 case GRU_SET_TASK_SLICE:
237 err = gru_set_task_slice(arg);
238 break;
239 case GRU_USER_GET_EXCEPTION_DETAIL:
240 err = gru_get_exception_detail(arg);
241 break;
242 case GRU_USER_UNLOAD_CONTEXT:
243 err = gru_user_unload_context(arg);
244 break;
245 case GRU_GET_CHIPLET_STATUS:
246 err = gru_get_chiplet_status(arg);
247 break;
248 case GRU_USER_FLUSH_TLB:
249 err = gru_user_flush_tlb(arg);
250 break;
251 case GRU_USER_CALL_OS:
252 err = gru_handle_user_call_os(arg);
253 break;
Jack Steinereb5bd5e2009-06-17 16:28:26 -0700254 case GRU_KTEST:
255 err = gru_ktest(arg);
256 break;
Jack Steiner78cf1de2008-07-29 22:33:56 -0700257 case GRU_GET_CONFIG_INFO:
258 err = gru_get_config_info(arg);
259 break;
Jack Steiner9cc9b052009-06-17 16:28:19 -0700260 case GRU_DUMP_CHIPLET_STATE:
261 err = gru_dump_chiplet_request(arg);
262 break;
Jack Steiner78cf1de2008-07-29 22:33:56 -0700263 }
264 return err;
265}
266
267/*
268 * Called at init time to build tables for all GRUs that are present in the
269 * system.
270 */
271static void gru_init_chiplet(struct gru_state *gru, unsigned long paddr,
272 void *vaddr, int nid, int bid, int grunum)
273{
274 spin_lock_init(&gru->gs_lock);
275 spin_lock_init(&gru->gs_asid_lock);
276 gru->gs_gru_base_paddr = paddr;
277 gru->gs_gru_base_vaddr = vaddr;
278 gru->gs_gid = bid * GRU_CHIPLETS_PER_BLADE + grunum;
279 gru->gs_blade = gru_base[bid];
280 gru->gs_blade_id = bid;
281 gru->gs_cbr_map = (GRU_CBR_AU == 64) ? ~0 : (1UL << GRU_CBR_AU) - 1;
282 gru->gs_dsr_map = (1UL << GRU_DSR_AU) - 1;
Jack Steiner87419412009-04-02 16:59:08 -0700283 gru->gs_asid_limit = MAX_ASID;
Jack Steiner78cf1de2008-07-29 22:33:56 -0700284 gru_tgh_flush_init(gru);
Jack Steinere1c32192009-04-02 16:59:10 -0700285 if (gru->gs_gid >= gru_max_gids)
286 gru_max_gids = gru->gs_gid + 1;
Jack Steiner43884602009-04-02 16:59:05 -0700287 gru_dbg(grudev, "bid %d, nid %d, gid %d, vaddr %p (0x%lx)\n",
Jack Steiner78cf1de2008-07-29 22:33:56 -0700288 bid, nid, gru->gs_gid, gru->gs_gru_base_vaddr,
289 gru->gs_gru_base_paddr);
290 gru_kservices_init(gru);
291}
292
293static int gru_init_tables(unsigned long gru_base_paddr, void *gru_base_vaddr)
294{
295 int pnode, nid, bid, chip;
296 int cbrs, dsrbytes, n;
297 int order = get_order(sizeof(struct gru_blade_state));
298 struct page *page;
299 struct gru_state *gru;
300 unsigned long paddr;
301 void *vaddr;
302
303 max_user_cbrs = GRU_NUM_CB;
304 max_user_dsr_bytes = GRU_NUM_DSR_BYTES;
305 for_each_online_node(nid) {
306 bid = uv_node_to_blade_id(nid);
307 pnode = uv_node_to_pnode(nid);
Jack Steinerfe5bb6b2009-04-02 16:59:04 -0700308 if (bid < 0 || gru_base[bid])
Jack Steiner78cf1de2008-07-29 22:33:56 -0700309 continue;
Mel Gorman6484eb32009-06-16 15:31:54 -0700310 page = alloc_pages_exact_node(nid, GFP_KERNEL, order);
Jack Steiner78cf1de2008-07-29 22:33:56 -0700311 if (!page)
312 goto fail;
313 gru_base[bid] = page_address(page);
314 memset(gru_base[bid], 0, sizeof(struct gru_blade_state));
315 gru_base[bid]->bs_lru_gru = &gru_base[bid]->bs_grus[0];
316 spin_lock_init(&gru_base[bid]->bs_lock);
317
318 dsrbytes = 0;
319 cbrs = 0;
320 for (gru = gru_base[bid]->bs_grus, chip = 0;
Jack Steinerfe5bb6b2009-04-02 16:59:04 -0700321 chip < GRU_CHIPLETS_PER_BLADE;
Jack Steiner78cf1de2008-07-29 22:33:56 -0700322 chip++, gru++) {
323 paddr = gru_chiplet_paddr(gru_base_paddr, pnode, chip);
324 vaddr = gru_chiplet_vaddr(gru_base_vaddr, pnode, chip);
Jack Steinerfe5bb6b2009-04-02 16:59:04 -0700325 gru_init_chiplet(gru, paddr, vaddr, nid, bid, chip);
Jack Steiner78cf1de2008-07-29 22:33:56 -0700326 n = hweight64(gru->gs_cbr_map) * GRU_CBR_AU_SIZE;
327 cbrs = max(cbrs, n);
328 n = hweight64(gru->gs_dsr_map) * GRU_DSR_AU_BYTES;
329 dsrbytes = max(dsrbytes, n);
330 }
331 max_user_cbrs = min(max_user_cbrs, cbrs);
332 max_user_dsr_bytes = min(max_user_dsr_bytes, dsrbytes);
333 }
334
335 return 0;
336
337fail:
338 for (nid--; nid >= 0; nid--)
339 free_pages((unsigned long)gru_base[nid], order);
340 return -ENOMEM;
341}
342
343#ifdef CONFIG_IA64
344
345static int get_base_irq(void)
346{
347 return IRQ_GRU;
348}
349
350#elif defined CONFIG_X86_64
351
352static void noop(unsigned int irq)
353{
354}
355
356static struct irq_chip gru_chip = {
357 .name = "gru",
358 .mask = noop,
359 .unmask = noop,
360 .ack = noop,
361};
362
363static int get_base_irq(void)
364{
365 set_irq_chip(IRQ_GRU, &gru_chip);
366 set_irq_chip(IRQ_GRU + 1, &gru_chip);
367 return IRQ_GRU;
368}
369#endif
370
371/*
372 * gru_init
373 *
374 * Called at boot or module load time to initialize the GRUs.
375 */
376static int __init gru_init(void)
377{
378 int ret, irq, chip;
379 char id[10];
Jack Steiner78cf1de2008-07-29 22:33:56 -0700380
Dean Nelson1c004002009-02-09 10:25:20 -0600381 if (!is_uv_system())
Robin Holte873cff2009-04-21 12:24:53 -0700382 return 0;
Jack Steiner78cf1de2008-07-29 22:33:56 -0700383
384#if defined CONFIG_IA64
385 gru_start_paddr = 0xd000000000UL; /* ZZZZZZZZZZZZZZZZZZZ fixme */
386#else
387 gru_start_paddr = uv_read_local_mmr(UVH_RH_GAM_GRU_OVERLAY_CONFIG_MMR) &
388 0x7fffffffffffUL;
Jack Steiner78cf1de2008-07-29 22:33:56 -0700389#endif
390 gru_start_vaddr = __va(gru_start_paddr);
Jack Steinerfe5bb6b2009-04-02 16:59:04 -0700391 gru_end_paddr = gru_start_paddr + GRU_MAX_BLADES * GRU_SIZE;
Jack Steiner78cf1de2008-07-29 22:33:56 -0700392 printk(KERN_INFO "GRU space: 0x%lx - 0x%lx\n",
393 gru_start_paddr, gru_end_paddr);
394 irq = get_base_irq();
395 for (chip = 0; chip < GRU_CHIPLETS_PER_BLADE; chip++) {
396 ret = request_irq(irq + chip, gru_intr, 0, id, NULL);
Jack Steinerfe5bb6b2009-04-02 16:59:04 -0700397 /* TODO: fix irq handling on x86. For now ignore failure because
Jack Steiner923f7f62008-10-15 22:05:13 -0700398 * interrupts are not required & not yet fully supported */
399 if (ret) {
Jack Steinerfe5bb6b2009-04-02 16:59:04 -0700400 printk(KERN_WARNING
401 "!!!WARNING: GRU ignoring request failure!!!\n");
Jack Steiner923f7f62008-10-15 22:05:13 -0700402 ret = 0;
403 }
Jack Steiner78cf1de2008-07-29 22:33:56 -0700404 if (ret) {
405 printk(KERN_ERR "%s: request_irq failed\n",
406 GRU_DRIVER_ID_STR);
407 goto exit1;
408 }
409 }
410
411 ret = misc_register(&gru_miscdev);
412 if (ret) {
413 printk(KERN_ERR "%s: misc_register failed\n",
414 GRU_DRIVER_ID_STR);
415 goto exit1;
416 }
417
418 ret = gru_proc_init();
419 if (ret) {
420 printk(KERN_ERR "%s: proc init failed\n", GRU_DRIVER_ID_STR);
421 goto exit2;
422 }
423
424 ret = gru_init_tables(gru_start_paddr, gru_start_vaddr);
425 if (ret) {
426 printk(KERN_ERR "%s: init tables failed\n", GRU_DRIVER_ID_STR);
427 goto exit3;
428 }
429
430 printk(KERN_INFO "%s: v%s\n", GRU_DRIVER_ID_STR,
431 GRU_DRIVER_VERSION_STR);
432 return 0;
433
434exit3:
435 gru_proc_exit();
436exit2:
437 misc_deregister(&gru_miscdev);
438exit1:
439 for (--chip; chip >= 0; chip--)
440 free_irq(irq + chip, NULL);
441 return ret;
442
443}
444
445static void __exit gru_exit(void)
446{
Jack Steiner27ca8a72009-04-02 16:59:11 -0700447 int i, bid, gid;
Jack Steiner78cf1de2008-07-29 22:33:56 -0700448 int order = get_order(sizeof(struct gru_state) *
449 GRU_CHIPLETS_PER_BLADE);
450
Dean Nelson1c004002009-02-09 10:25:20 -0600451 if (!is_uv_system())
Robin Holt8275d102008-09-13 02:33:22 -0700452 return;
453
Jack Steiner78cf1de2008-07-29 22:33:56 -0700454 for (i = 0; i < GRU_CHIPLETS_PER_BLADE; i++)
455 free_irq(IRQ_GRU + i, NULL);
456
Jack Steiner27ca8a72009-04-02 16:59:11 -0700457 foreach_gid(gid)
458 gru_kservices_exit(GID_TO_GRU(gid));
459
Jack Steiner78cf1de2008-07-29 22:33:56 -0700460 for (bid = 0; bid < GRU_MAX_BLADES; bid++)
461 free_pages((unsigned long)gru_base[bid], order);
462
463 misc_deregister(&gru_miscdev);
464 gru_proc_exit();
465}
466
467static struct file_operations gru_fops = {
468 .owner = THIS_MODULE,
469 .unlocked_ioctl = gru_file_unlocked_ioctl,
470 .mmap = gru_file_mmap,
471};
472
473static struct miscdevice gru_miscdev = {
474 .minor = MISC_DYNAMIC_MINOR,
475 .name = "gru",
476 .fops = &gru_fops,
477};
478
479struct vm_operations_struct gru_vm_ops = {
480 .close = gru_vma_close,
481 .fault = gru_fault,
482};
483
Jack Steinerfe5bb6b2009-04-02 16:59:04 -0700484#ifndef MODULE
Dean Nelson026bde12008-12-02 08:06:01 -0600485fs_initcall(gru_init);
Jack Steinerfe5bb6b2009-04-02 16:59:04 -0700486#else
487module_init(gru_init);
488#endif
Jack Steiner78cf1de2008-07-29 22:33:56 -0700489module_exit(gru_exit);
490
Jack Steiner9ca8e40c12008-07-29 22:34:02 -0700491module_param(gru_options, ulong, 0644);
492MODULE_PARM_DESC(gru_options, "Various debug options");
Jack Steiner78cf1de2008-07-29 22:33:56 -0700493
494MODULE_AUTHOR("Silicon Graphics, Inc.");
495MODULE_LICENSE("GPL");
496MODULE_DESCRIPTION(GRU_DRIVER_ID_STR GRU_DRIVER_VERSION_STR);
497MODULE_VERSION(GRU_DRIVER_VERSION_STR);
498