41c8fe2a928c6a76967890a7467038e7df703c3c
[linux-2.6.git] / drivers / misc / sgi-gru / grufile.c
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
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/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
43 #include <asm/uv/uv_hub.h>
44 #include <asm/uv/uv_mmrs.h>
45
46 struct gru_blade_state *gru_base[GRU_MAX_BLADES] __read_mostly;
47 unsigned long gru_start_paddr __read_mostly;
48 void *gru_start_vaddr __read_mostly;
49 unsigned long gru_end_paddr __read_mostly;
50 unsigned int gru_max_gids __read_mostly;
51 struct gru_stats_s gru_stats;
52
53 /* Guaranteed user available resources on each node */
54 static int max_user_cbrs, max_user_dsr_bytes;
55
56 static struct miscdevice gru_miscdev;
57
58
59 /*
60  * gru_vma_close
61  *
62  * Called when unmapping a device mapping. Frees all gru resources
63  * and tables belonging to the vma.
64  */
65 static void gru_vma_close(struct vm_area_struct *vma)
66 {
67         struct gru_vma_data *vdata;
68         struct gru_thread_state *gts;
69         struct list_head *entry, *next;
70
71         if (!vma->vm_private_data)
72                 return;
73
74         vdata = vma->vm_private_data;
75         vma->vm_private_data = NULL;
76         gru_dbg(grudev, "vma %p, file %p, vdata %p\n", vma, vma->vm_file,
77                                 vdata);
78         list_for_each_safe(entry, next, &vdata->vd_head) {
79                 gts =
80                     list_entry(entry, struct gru_thread_state, ts_next);
81                 list_del(&gts->ts_next);
82                 mutex_lock(&gts->ts_ctxlock);
83                 if (gts->ts_gru)
84                         gru_unload_context(gts, 0);
85                 mutex_unlock(&gts->ts_ctxlock);
86                 gts_drop(gts);
87         }
88         kfree(vdata);
89         STAT(vdata_free);
90 }
91
92 /*
93  * gru_file_mmap
94  *
95  * Called when mmaping the device.  Initializes the vma with a fault handler
96  * and private data structure necessary to allocate, track, and free the
97  * underlying pages.
98  */
99 static int gru_file_mmap(struct file *file, struct vm_area_struct *vma)
100 {
101         if ((vma->vm_flags & (VM_SHARED | VM_WRITE)) != (VM_SHARED | VM_WRITE))
102                 return -EPERM;
103
104         if (vma->vm_start & (GRU_GSEG_PAGESIZE - 1) ||
105                                 vma->vm_end & (GRU_GSEG_PAGESIZE - 1))
106                 return -EINVAL;
107
108         vma->vm_flags |=
109             (VM_IO | VM_DONTCOPY | VM_LOCKED | VM_DONTEXPAND | VM_PFNMAP |
110                         VM_RESERVED);
111         vma->vm_page_prot = PAGE_SHARED;
112         vma->vm_ops = &gru_vm_ops;
113
114         vma->vm_private_data = gru_alloc_vma_data(vma, 0);
115         if (!vma->vm_private_data)
116                 return -ENOMEM;
117
118         gru_dbg(grudev, "file %p, vaddr 0x%lx, vma %p, vdata %p\n",
119                 file, vma->vm_start, vma, vma->vm_private_data);
120         return 0;
121 }
122
123 /*
124  * Create a new GRU context
125  */
126 static int gru_create_new_context(unsigned long arg)
127 {
128         struct gru_create_context_req req;
129         struct vm_area_struct *vma;
130         struct gru_vma_data *vdata;
131         int ret = -EINVAL;
132
133
134         if (copy_from_user(&req, (void __user *)arg, sizeof(req)))
135                 return -EFAULT;
136
137         if (req.data_segment_bytes > max_user_dsr_bytes)
138                 return -EINVAL;
139         if (req.control_blocks > max_user_cbrs || !req.maximum_thread_count)
140                 return -EINVAL;
141
142         if (!(req.options & GRU_OPT_MISS_MASK))
143                 req.options |= GRU_OPT_MISS_FMM_INTR;
144
145         down_write(&current->mm->mmap_sem);
146         vma = gru_find_vma(req.gseg);
147         if (vma) {
148                 vdata = vma->vm_private_data;
149                 vdata->vd_user_options = req.options;
150                 vdata->vd_dsr_au_count =
151                     GRU_DS_BYTES_TO_AU(req.data_segment_bytes);
152                 vdata->vd_cbr_au_count = GRU_CB_COUNT_TO_AU(req.control_blocks);
153                 ret = 0;
154         }
155         up_write(&current->mm->mmap_sem);
156
157         return ret;
158 }
159
160 /*
161  * Get GRU configuration info (temp - for emulator testing)
162  */
163 static long gru_get_config_info(unsigned long arg)
164 {
165         struct gru_config_info info;
166         int nodesperblade;
167
168         if (num_online_nodes() > 1 &&
169                         (uv_node_to_blade_id(1) == uv_node_to_blade_id(0)))
170                 nodesperblade = 2;
171         else
172                 nodesperblade = 1;
173         info.cpus = num_online_cpus();
174         info.nodes = num_online_nodes();
175         info.blades = info.nodes / nodesperblade;
176         info.chiplets = GRU_CHIPLETS_PER_BLADE * info.blades;
177
178         if (copy_to_user((void __user *)arg, &info, sizeof(info)))
179                 return -EFAULT;
180         return 0;
181 }
182
183 /*
184  * gru_file_unlocked_ioctl
185  *
186  * Called to update file attributes via IOCTL calls.
187  */
188 static long gru_file_unlocked_ioctl(struct file *file, unsigned int req,
189                                     unsigned long arg)
190 {
191         int err = -EBADRQC;
192
193         gru_dbg(grudev, "file %p\n", file);
194
195         switch (req) {
196         case GRU_CREATE_CONTEXT:
197                 err = gru_create_new_context(arg);
198                 break;
199         case GRU_SET_CONTEXT_OPTION:
200                 err = gru_set_context_option(arg);
201                 break;
202         case GRU_USER_GET_EXCEPTION_DETAIL:
203                 err = gru_get_exception_detail(arg);
204                 break;
205         case GRU_USER_UNLOAD_CONTEXT:
206                 err = gru_user_unload_context(arg);
207                 break;
208         case GRU_USER_FLUSH_TLB:
209                 err = gru_user_flush_tlb(arg);
210                 break;
211         case GRU_USER_CALL_OS:
212                 err = gru_handle_user_call_os(arg);
213                 break;
214         case GRU_GET_GSEG_STATISTICS:
215                 err = gru_get_gseg_statistics(arg);
216                 break;
217         case GRU_KTEST:
218                 err = gru_ktest(arg);
219                 break;
220         case GRU_GET_CONFIG_INFO:
221                 err = gru_get_config_info(arg);
222                 break;
223         case GRU_DUMP_CHIPLET_STATE:
224                 err = gru_dump_chiplet_request(arg);
225                 break;
226         }
227         return err;
228 }
229
230 /*
231  * Called at init time to build tables for all GRUs that are present in the
232  * system.
233  */
234 static void gru_init_chiplet(struct gru_state *gru, unsigned long paddr,
235                              void *vaddr, int nid, int bid, int grunum)
236 {
237         spin_lock_init(&gru->gs_lock);
238         spin_lock_init(&gru->gs_asid_lock);
239         gru->gs_gru_base_paddr = paddr;
240         gru->gs_gru_base_vaddr = vaddr;
241         gru->gs_gid = bid * GRU_CHIPLETS_PER_BLADE + grunum;
242         gru->gs_blade = gru_base[bid];
243         gru->gs_blade_id = bid;
244         gru->gs_cbr_map = (GRU_CBR_AU == 64) ? ~0 : (1UL << GRU_CBR_AU) - 1;
245         gru->gs_dsr_map = (1UL << GRU_DSR_AU) - 1;
246         gru->gs_asid_limit = MAX_ASID;
247         gru_tgh_flush_init(gru);
248         if (gru->gs_gid >= gru_max_gids)
249                 gru_max_gids = gru->gs_gid + 1;
250         gru_dbg(grudev, "bid %d, nid %d, gid %d, vaddr %p (0x%lx)\n",
251                 bid, nid, gru->gs_gid, gru->gs_gru_base_vaddr,
252                 gru->gs_gru_base_paddr);
253 }
254
255 static int gru_init_tables(unsigned long gru_base_paddr, void *gru_base_vaddr)
256 {
257         int pnode, nid, bid, chip;
258         int cbrs, dsrbytes, n;
259         int order = get_order(sizeof(struct gru_blade_state));
260         struct page *page;
261         struct gru_state *gru;
262         unsigned long paddr;
263         void *vaddr;
264
265         max_user_cbrs = GRU_NUM_CB;
266         max_user_dsr_bytes = GRU_NUM_DSR_BYTES;
267         for_each_online_node(nid) {
268                 bid = uv_node_to_blade_id(nid);
269                 pnode = uv_node_to_pnode(nid);
270                 if (bid < 0 || gru_base[bid])
271                         continue;
272                 page = alloc_pages_exact_node(nid, GFP_KERNEL, order);
273                 if (!page)
274                         goto fail;
275                 gru_base[bid] = page_address(page);
276                 memset(gru_base[bid], 0, sizeof(struct gru_blade_state));
277                 gru_base[bid]->bs_lru_gru = &gru_base[bid]->bs_grus[0];
278                 spin_lock_init(&gru_base[bid]->bs_lock);
279                 init_rwsem(&gru_base[bid]->bs_kgts_sema);
280
281                 dsrbytes = 0;
282                 cbrs = 0;
283                 for (gru = gru_base[bid]->bs_grus, chip = 0;
284                                 chip < GRU_CHIPLETS_PER_BLADE;
285                                 chip++, gru++) {
286                         paddr = gru_chiplet_paddr(gru_base_paddr, pnode, chip);
287                         vaddr = gru_chiplet_vaddr(gru_base_vaddr, pnode, chip);
288                         gru_init_chiplet(gru, paddr, vaddr, nid, bid, chip);
289                         n = hweight64(gru->gs_cbr_map) * GRU_CBR_AU_SIZE;
290                         cbrs = max(cbrs, n);
291                         n = hweight64(gru->gs_dsr_map) * GRU_DSR_AU_BYTES;
292                         dsrbytes = max(dsrbytes, n);
293                 }
294                 max_user_cbrs = min(max_user_cbrs, cbrs);
295                 max_user_dsr_bytes = min(max_user_dsr_bytes, dsrbytes);
296         }
297
298         return 0;
299
300 fail:
301         for (nid--; nid >= 0; nid--)
302                 free_pages((unsigned long)gru_base[nid], order);
303         return -ENOMEM;
304 }
305
306 #ifdef CONFIG_IA64
307
308 static int get_base_irq(void)
309 {
310         return IRQ_GRU;
311 }
312
313 #elif defined CONFIG_X86_64
314
315 static void noop(unsigned int irq)
316 {
317 }
318
319 static struct irq_chip gru_chip = {
320         .name           = "gru",
321         .mask           = noop,
322         .unmask         = noop,
323         .ack            = noop,
324 };
325
326 static int get_base_irq(void)
327 {
328         set_irq_chip(IRQ_GRU, &gru_chip);
329         set_irq_chip(IRQ_GRU + 1, &gru_chip);
330         return IRQ_GRU;
331 }
332 #endif
333
334 /*
335  * gru_init
336  *
337  * Called at boot or module load time to initialize the GRUs.
338  */
339 static int __init gru_init(void)
340 {
341         int ret, irq, chip;
342         char id[10];
343
344         if (!is_uv_system())
345                 return 0;
346
347 #if defined CONFIG_IA64
348         gru_start_paddr = 0xd000000000UL; /* ZZZZZZZZZZZZZZZZZZZ fixme */
349 #else
350         gru_start_paddr = uv_read_local_mmr(UVH_RH_GAM_GRU_OVERLAY_CONFIG_MMR) &
351                                 0x7fffffffffffUL;
352 #endif
353         gru_start_vaddr = __va(gru_start_paddr);
354         gru_end_paddr = gru_start_paddr + GRU_MAX_BLADES * GRU_SIZE;
355         printk(KERN_INFO "GRU space: 0x%lx - 0x%lx\n",
356                gru_start_paddr, gru_end_paddr);
357         irq = get_base_irq();
358         for (chip = 0; chip < GRU_CHIPLETS_PER_BLADE; chip++) {
359                 ret = request_irq(irq + chip, gru_intr, 0, id, NULL);
360                 /* TODO: fix irq handling on x86. For now ignore failure because
361                  * interrupts are not required & not yet fully supported */
362                 if (ret) {
363                         printk(KERN_WARNING
364                                "!!!WARNING: GRU ignoring request failure!!!\n");
365                         ret = 0;
366                 }
367                 if (ret) {
368                         printk(KERN_ERR "%s: request_irq failed\n",
369                                GRU_DRIVER_ID_STR);
370                         goto exit1;
371                 }
372         }
373
374         ret = misc_register(&gru_miscdev);
375         if (ret) {
376                 printk(KERN_ERR "%s: misc_register failed\n",
377                        GRU_DRIVER_ID_STR);
378                 goto exit1;
379         }
380
381         ret = gru_proc_init();
382         if (ret) {
383                 printk(KERN_ERR "%s: proc init failed\n", GRU_DRIVER_ID_STR);
384                 goto exit2;
385         }
386
387         ret = gru_init_tables(gru_start_paddr, gru_start_vaddr);
388         if (ret) {
389                 printk(KERN_ERR "%s: init tables failed\n", GRU_DRIVER_ID_STR);
390                 goto exit3;
391         }
392         gru_kservices_init();
393
394         printk(KERN_INFO "%s: v%s\n", GRU_DRIVER_ID_STR,
395                GRU_DRIVER_VERSION_STR);
396         return 0;
397
398 exit3:
399         gru_proc_exit();
400 exit2:
401         misc_deregister(&gru_miscdev);
402 exit1:
403         for (--chip; chip >= 0; chip--)
404                 free_irq(irq + chip, NULL);
405         return ret;
406
407 }
408
409 static void __exit gru_exit(void)
410 {
411         int i, bid;
412         int order = get_order(sizeof(struct gru_state) *
413                               GRU_CHIPLETS_PER_BLADE);
414
415         if (!is_uv_system())
416                 return;
417
418         for (i = 0; i < GRU_CHIPLETS_PER_BLADE; i++)
419                 free_irq(IRQ_GRU + i, NULL);
420         gru_kservices_exit();
421         for (bid = 0; bid < GRU_MAX_BLADES; bid++)
422                 free_pages((unsigned long)gru_base[bid], order);
423
424         misc_deregister(&gru_miscdev);
425         gru_proc_exit();
426 }
427
428 static const struct file_operations gru_fops = {
429         .owner          = THIS_MODULE,
430         .unlocked_ioctl = gru_file_unlocked_ioctl,
431         .mmap           = gru_file_mmap,
432 };
433
434 static struct miscdevice gru_miscdev = {
435         .minor          = MISC_DYNAMIC_MINOR,
436         .name           = "gru",
437         .fops           = &gru_fops,
438 };
439
440 const struct vm_operations_struct gru_vm_ops = {
441         .close          = gru_vma_close,
442         .fault          = gru_fault,
443 };
444
445 #ifndef MODULE
446 fs_initcall(gru_init);
447 #else
448 module_init(gru_init);
449 #endif
450 module_exit(gru_exit);
451
452 module_param(gru_options, ulong, 0644);
453 MODULE_PARM_DESC(gru_options, "Various debug options");
454
455 MODULE_AUTHOR("Silicon Graphics, Inc.");
456 MODULE_LICENSE("GPL");
457 MODULE_DESCRIPTION(GRU_DRIVER_ID_STR GRU_DRIVER_VERSION_STR);
458 MODULE_VERSION(GRU_DRIVER_VERSION_STR);
459