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