15292e5f74a14315b9cedc4da28de32e071d86e7
[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/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>
39 #include <asm/uv/uv.h>
40 #include "gru.h"
41 #include "grulib.h"
42 #include "grutables.h"
43
44 #include <asm/uv/uv_hub.h>
45 #include <asm/uv/uv_mmrs.h>
46
47 struct gru_blade_state *gru_base[GRU_MAX_BLADES] __read_mostly;
48 unsigned long gru_start_paddr __read_mostly;
49 unsigned long gru_end_paddr __read_mostly;
50 struct gru_stats_s gru_stats;
51
52 /* Guaranteed user available resources on each node */
53 static int max_user_cbrs, max_user_dsr_bytes;
54
55 static struct file_operations gru_fops;
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 == 0 ||
138                                 req.data_segment_bytes > max_user_dsr_bytes)
139                 return -EINVAL;
140         if (!req.control_blocks || !req.maximum_thread_count ||
141                                 req.control_blocks > max_user_cbrs)
142                 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  */
165 static 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  */
188 static 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  */
225 static 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;
254         case GRU_GET_CONFIG_INFO:
255                 err = gru_get_config_info(arg);
256                 break;
257         }
258         return err;
259 }
260
261 /*
262  * Called at init time to build tables for all GRUs that are present in the
263  * system.
264  */
265 static void gru_init_chiplet(struct gru_state *gru, unsigned long paddr,
266                              void *vaddr, int nid, int bid, int grunum)
267 {
268         spin_lock_init(&gru->gs_lock);
269         spin_lock_init(&gru->gs_asid_lock);
270         gru->gs_gru_base_paddr = paddr;
271         gru->gs_gru_base_vaddr = vaddr;
272         gru->gs_gid = bid * GRU_CHIPLETS_PER_BLADE + grunum;
273         gru->gs_blade = gru_base[bid];
274         gru->gs_blade_id = bid;
275         gru->gs_cbr_map = (GRU_CBR_AU == 64) ? ~0 : (1UL << GRU_CBR_AU) - 1;
276         gru->gs_dsr_map = (1UL << GRU_DSR_AU) - 1;
277         gru_tgh_flush_init(gru);
278         gru_dbg(grudev, "bid %d, nid %d, gru %x, vaddr %p (0x%lx)\n",
279                 bid, nid, gru->gs_gid, gru->gs_gru_base_vaddr,
280                 gru->gs_gru_base_paddr);
281         gru_kservices_init(gru);
282 }
283
284 static int gru_init_tables(unsigned long gru_base_paddr, void *gru_base_vaddr)
285 {
286         int pnode, nid, bid, chip;
287         int cbrs, dsrbytes, n;
288         int order = get_order(sizeof(struct gru_blade_state));
289         struct page *page;
290         struct gru_state *gru;
291         unsigned long paddr;
292         void *vaddr;
293
294         max_user_cbrs = GRU_NUM_CB;
295         max_user_dsr_bytes = GRU_NUM_DSR_BYTES;
296         for_each_online_node(nid) {
297                 bid = uv_node_to_blade_id(nid);
298                 pnode = uv_node_to_pnode(nid);
299                 if (bid < 0 || gru_base[bid])
300                         continue;
301                 page = alloc_pages_node(nid, GFP_KERNEL, order);
302                 if (!page)
303                         goto fail;
304                 gru_base[bid] = page_address(page);
305                 memset(gru_base[bid], 0, sizeof(struct gru_blade_state));
306                 gru_base[bid]->bs_lru_gru = &gru_base[bid]->bs_grus[0];
307                 spin_lock_init(&gru_base[bid]->bs_lock);
308
309                 dsrbytes = 0;
310                 cbrs = 0;
311                 for (gru = gru_base[bid]->bs_grus, chip = 0;
312                                 chip < GRU_CHIPLETS_PER_BLADE;
313                                 chip++, gru++) {
314                         paddr = gru_chiplet_paddr(gru_base_paddr, pnode, chip);
315                         vaddr = gru_chiplet_vaddr(gru_base_vaddr, pnode, chip);
316                         gru_init_chiplet(gru, paddr, vaddr, nid, bid, chip);
317                         n = hweight64(gru->gs_cbr_map) * GRU_CBR_AU_SIZE;
318                         cbrs = max(cbrs, n);
319                         n = hweight64(gru->gs_dsr_map) * GRU_DSR_AU_BYTES;
320                         dsrbytes = max(dsrbytes, n);
321                 }
322                 max_user_cbrs = min(max_user_cbrs, cbrs);
323                 max_user_dsr_bytes = min(max_user_dsr_bytes, dsrbytes);
324         }
325
326         return 0;
327
328 fail:
329         for (nid--; nid >= 0; nid--)
330                 free_pages((unsigned long)gru_base[nid], order);
331         return -ENOMEM;
332 }
333
334 #ifdef CONFIG_IA64
335
336 static int get_base_irq(void)
337 {
338         return IRQ_GRU;
339 }
340
341 #elif defined CONFIG_X86_64
342
343 static void noop(unsigned int irq)
344 {
345 }
346
347 static struct irq_chip gru_chip = {
348         .name           = "gru",
349         .mask           = noop,
350         .unmask         = noop,
351         .ack            = noop,
352 };
353
354 static int get_base_irq(void)
355 {
356         set_irq_chip(IRQ_GRU, &gru_chip);
357         set_irq_chip(IRQ_GRU + 1, &gru_chip);
358         return IRQ_GRU;
359 }
360 #endif
361
362 /*
363  * gru_init
364  *
365  * Called at boot or module load time to initialize the GRUs.
366  */
367 static int __init gru_init(void)
368 {
369         int ret, irq, chip;
370         char id[10];
371         void *gru_start_vaddr;
372
373         if (!is_uv_system())
374                 return -ENODEV;
375
376 #if defined CONFIG_IA64
377         gru_start_paddr = 0xd000000000UL; /* ZZZZZZZZZZZZZZZZZZZ fixme */
378 #else
379         gru_start_paddr = uv_read_local_mmr(UVH_RH_GAM_GRU_OVERLAY_CONFIG_MMR) &
380                                 0x7fffffffffffUL;
381 #endif
382         gru_start_vaddr = __va(gru_start_paddr);
383         gru_end_paddr = gru_start_paddr + GRU_MAX_BLADES * GRU_SIZE;
384         printk(KERN_INFO "GRU space: 0x%lx - 0x%lx\n",
385                gru_start_paddr, gru_end_paddr);
386         irq = get_base_irq();
387         for (chip = 0; chip < GRU_CHIPLETS_PER_BLADE; chip++) {
388                 ret = request_irq(irq + chip, gru_intr, 0, id, NULL);
389                 /* TODO: fix irq handling on x86. For now ignore failure because
390                  * interrupts are not required & not yet fully supported */
391                 if (ret) {
392                         printk(KERN_WARNING
393                                "!!!WARNING: GRU ignoring request failure!!!\n");
394                         ret = 0;
395                 }
396                 if (ret) {
397                         printk(KERN_ERR "%s: request_irq failed\n",
398                                GRU_DRIVER_ID_STR);
399                         goto exit1;
400                 }
401         }
402
403         ret = misc_register(&gru_miscdev);
404         if (ret) {
405                 printk(KERN_ERR "%s: misc_register failed\n",
406                        GRU_DRIVER_ID_STR);
407                 goto exit1;
408         }
409
410         ret = gru_proc_init();
411         if (ret) {
412                 printk(KERN_ERR "%s: proc init failed\n", GRU_DRIVER_ID_STR);
413                 goto exit2;
414         }
415
416         ret = gru_init_tables(gru_start_paddr, gru_start_vaddr);
417         if (ret) {
418                 printk(KERN_ERR "%s: init tables failed\n", GRU_DRIVER_ID_STR);
419                 goto exit3;
420         }
421
422         printk(KERN_INFO "%s: v%s\n", GRU_DRIVER_ID_STR,
423                GRU_DRIVER_VERSION_STR);
424         return 0;
425
426 exit3:
427         gru_proc_exit();
428 exit2:
429         misc_deregister(&gru_miscdev);
430 exit1:
431         for (--chip; chip >= 0; chip--)
432                 free_irq(irq + chip, NULL);
433         return ret;
434
435 }
436
437 static void __exit gru_exit(void)
438 {
439         int i, bid;
440         int order = get_order(sizeof(struct gru_state) *
441                               GRU_CHIPLETS_PER_BLADE);
442
443         if (!is_uv_system())
444                 return;
445
446         for (i = 0; i < GRU_CHIPLETS_PER_BLADE; i++)
447                 free_irq(IRQ_GRU + i, NULL);
448
449         for (bid = 0; bid < GRU_MAX_BLADES; bid++)
450                 free_pages((unsigned long)gru_base[bid], order);
451
452         misc_deregister(&gru_miscdev);
453         gru_proc_exit();
454 }
455
456 static struct file_operations gru_fops = {
457         .owner          = THIS_MODULE,
458         .unlocked_ioctl = gru_file_unlocked_ioctl,
459         .mmap           = gru_file_mmap,
460 };
461
462 static struct miscdevice gru_miscdev = {
463         .minor          = MISC_DYNAMIC_MINOR,
464         .name           = "gru",
465         .fops           = &gru_fops,
466 };
467
468 struct vm_operations_struct gru_vm_ops = {
469         .close          = gru_vma_close,
470         .fault          = gru_fault,
471 };
472
473 #ifndef MODULE
474 fs_initcall(gru_init);
475 #else
476 module_init(gru_init);
477 #endif
478 module_exit(gru_exit);
479
480 module_param(gru_options, ulong, 0644);
481 MODULE_PARM_DESC(gru_options, "Various debug options");
482
483 MODULE_AUTHOR("Silicon Graphics, Inc.");
484 MODULE_LICENSE("GPL");
485 MODULE_DESCRIPTION(GRU_DRIVER_ID_STR GRU_DRIVER_VERSION_STR);
486 MODULE_VERSION(GRU_DRIVER_VERSION_STR);
487