x86, delay: tsc based udelay should have rdtsc_barrier
[linux-2.6/mini2440.git] / drivers / misc / sgi-gru / grufile.c
blobfa2d93a9fb8d7755de89a2f08badce29a07f8983
1 /*
2 * SN Platform GRU Driver
4 * FILE OPERATIONS & DRIVER INITIALIZATION
6 * This file supports the user system call for file open, close, mmap, etc.
7 * This also incudes the driver initialization code.
9 * Copyright (c) 2008 Silicon Graphics, Inc. All Rights Reserved.
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.
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.
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
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"
44 #include <asm/uv/uv_hub.h>
45 #include <asm/uv/uv_mmrs.h>
47 struct gru_blade_state *gru_base[GRU_MAX_BLADES] __read_mostly;
48 unsigned long gru_start_paddr __read_mostly;
49 void *gru_start_vaddr __read_mostly;
50 unsigned long gru_end_paddr __read_mostly;
51 unsigned int gru_max_gids __read_mostly;
52 struct gru_stats_s gru_stats;
54 /* Guaranteed user available resources on each node */
55 static int max_user_cbrs, max_user_dsr_bytes;
57 static struct file_operations gru_fops;
58 static struct miscdevice gru_miscdev;
62 * gru_vma_close
64 * Called when unmapping a device mapping. Frees all gru resources
65 * and tables belonging to the vma.
67 static void gru_vma_close(struct vm_area_struct *vma)
69 struct gru_vma_data *vdata;
70 struct gru_thread_state *gts;
71 struct list_head *entry, *next;
73 if (!vma->vm_private_data)
74 return;
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);
90 kfree(vdata);
91 STAT(vdata_free);
95 * gru_file_mmap
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.
101 static int gru_file_mmap(struct file *file, struct vm_area_struct *vma)
103 if ((vma->vm_flags & (VM_SHARED | VM_WRITE)) != (VM_SHARED | VM_WRITE))
104 return -EPERM;
106 if (vma->vm_start & (GRU_GSEG_PAGESIZE - 1) ||
107 vma->vm_end & (GRU_GSEG_PAGESIZE - 1))
108 return -EINVAL;
110 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;
116 vma->vm_private_data = gru_alloc_vma_data(vma, 0);
117 if (!vma->vm_private_data)
118 return -ENOMEM;
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;
126 * Create a new GRU context
128 static int gru_create_new_context(unsigned long arg)
130 struct gru_create_context_req req;
131 struct vm_area_struct *vma;
132 struct gru_vma_data *vdata;
133 int ret = -EINVAL;
136 if (copy_from_user(&req, (void __user *)arg, sizeof(req)))
137 return -EFAULT;
139 if (req.data_segment_bytes > max_user_dsr_bytes)
140 return -EINVAL;
141 if (req.control_blocks > max_user_cbrs || !req.maximum_thread_count)
142 return -EINVAL;
144 if (!(req.options & GRU_OPT_MISS_MASK))
145 req.options |= GRU_OPT_MISS_FMM_INTR;
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;
157 up_write(&current->mm->mmap_sem);
159 return ret;
163 * Get GRU configuration info (temp - for emulator testing)
165 static long gru_get_config_info(unsigned long arg)
167 struct gru_config_info info;
168 int nodesperblade;
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;
180 if (copy_to_user((void __user *)arg, &info, sizeof(info)))
181 return -EFAULT;
182 return 0;
186 * gru_file_unlocked_ioctl
188 * Called to update file attributes via IOCTL calls.
190 static long gru_file_unlocked_ioctl(struct file *file, unsigned int req,
191 unsigned long arg)
193 int err = -EBADRQC;
195 gru_dbg(grudev, "file %p\n", file);
197 switch (req) {
198 case GRU_CREATE_CONTEXT:
199 err = gru_create_new_context(arg);
200 break;
201 case GRU_SET_CONTEXT_OPTION:
202 err = gru_set_context_option(arg);
203 break;
204 case GRU_USER_GET_EXCEPTION_DETAIL:
205 err = gru_get_exception_detail(arg);
206 break;
207 case GRU_USER_UNLOAD_CONTEXT:
208 err = gru_user_unload_context(arg);
209 break;
210 case GRU_USER_FLUSH_TLB:
211 err = gru_user_flush_tlb(arg);
212 break;
213 case GRU_USER_CALL_OS:
214 err = gru_handle_user_call_os(arg);
215 break;
216 case GRU_GET_GSEG_STATISTICS:
217 err = gru_get_gseg_statistics(arg);
218 break;
219 case GRU_KTEST:
220 err = gru_ktest(arg);
221 break;
222 case GRU_GET_CONFIG_INFO:
223 err = gru_get_config_info(arg);
224 break;
225 case GRU_DUMP_CHIPLET_STATE:
226 err = gru_dump_chiplet_request(arg);
227 break;
229 return err;
233 * Called at init time to build tables for all GRUs that are present in the
234 * system.
236 static void gru_init_chiplet(struct gru_state *gru, unsigned long paddr,
237 void *vaddr, int nid, int bid, int grunum)
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);
257 static int gru_init_tables(unsigned long gru_base_paddr, void *gru_base_vaddr)
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;
267 max_user_cbrs = GRU_NUM_CB;
268 max_user_dsr_bytes = GRU_NUM_DSR_BYTES;
269 for_each_online_node(nid) {
270 bid = uv_node_to_blade_id(nid);
271 pnode = uv_node_to_pnode(nid);
272 if (bid < 0 || gru_base[bid])
273 continue;
274 page = alloc_pages_exact_node(nid, GFP_KERNEL, order);
275 if (!page)
276 goto fail;
277 gru_base[bid] = page_address(page);
278 memset(gru_base[bid], 0, sizeof(struct gru_blade_state));
279 gru_base[bid]->bs_lru_gru = &gru_base[bid]->bs_grus[0];
280 spin_lock_init(&gru_base[bid]->bs_lock);
281 init_rwsem(&gru_base[bid]->bs_kgts_sema);
283 dsrbytes = 0;
284 cbrs = 0;
285 for (gru = gru_base[bid]->bs_grus, chip = 0;
286 chip < GRU_CHIPLETS_PER_BLADE;
287 chip++, gru++) {
288 paddr = gru_chiplet_paddr(gru_base_paddr, pnode, chip);
289 vaddr = gru_chiplet_vaddr(gru_base_vaddr, pnode, chip);
290 gru_init_chiplet(gru, paddr, vaddr, nid, bid, chip);
291 n = hweight64(gru->gs_cbr_map) * GRU_CBR_AU_SIZE;
292 cbrs = max(cbrs, n);
293 n = hweight64(gru->gs_dsr_map) * GRU_DSR_AU_BYTES;
294 dsrbytes = max(dsrbytes, n);
296 max_user_cbrs = min(max_user_cbrs, cbrs);
297 max_user_dsr_bytes = min(max_user_dsr_bytes, dsrbytes);
300 return 0;
302 fail:
303 for (nid--; nid >= 0; nid--)
304 free_pages((unsigned long)gru_base[nid], order);
305 return -ENOMEM;
308 #ifdef CONFIG_IA64
310 static int get_base_irq(void)
312 return IRQ_GRU;
315 #elif defined CONFIG_X86_64
317 static void noop(unsigned int irq)
321 static struct irq_chip gru_chip = {
322 .name = "gru",
323 .mask = noop,
324 .unmask = noop,
325 .ack = noop,
328 static int get_base_irq(void)
330 set_irq_chip(IRQ_GRU, &gru_chip);
331 set_irq_chip(IRQ_GRU + 1, &gru_chip);
332 return IRQ_GRU;
334 #endif
337 * gru_init
339 * Called at boot or module load time to initialize the GRUs.
341 static int __init gru_init(void)
343 int ret, irq, chip;
344 char id[10];
346 if (!is_uv_system())
347 return 0;
349 #if defined CONFIG_IA64
350 gru_start_paddr = 0xd000000000UL; /* ZZZZZZZZZZZZZZZZZZZ fixme */
351 #else
352 gru_start_paddr = uv_read_local_mmr(UVH_RH_GAM_GRU_OVERLAY_CONFIG_MMR) &
353 0x7fffffffffffUL;
354 #endif
355 gru_start_vaddr = __va(gru_start_paddr);
356 gru_end_paddr = gru_start_paddr + GRU_MAX_BLADES * GRU_SIZE;
357 printk(KERN_INFO "GRU space: 0x%lx - 0x%lx\n",
358 gru_start_paddr, gru_end_paddr);
359 irq = get_base_irq();
360 for (chip = 0; chip < GRU_CHIPLETS_PER_BLADE; chip++) {
361 ret = request_irq(irq + chip, gru_intr, 0, id, NULL);
362 /* TODO: fix irq handling on x86. For now ignore failure because
363 * interrupts are not required & not yet fully supported */
364 if (ret) {
365 printk(KERN_WARNING
366 "!!!WARNING: GRU ignoring request failure!!!\n");
367 ret = 0;
369 if (ret) {
370 printk(KERN_ERR "%s: request_irq failed\n",
371 GRU_DRIVER_ID_STR);
372 goto exit1;
376 ret = misc_register(&gru_miscdev);
377 if (ret) {
378 printk(KERN_ERR "%s: misc_register failed\n",
379 GRU_DRIVER_ID_STR);
380 goto exit1;
383 ret = gru_proc_init();
384 if (ret) {
385 printk(KERN_ERR "%s: proc init failed\n", GRU_DRIVER_ID_STR);
386 goto exit2;
389 ret = gru_init_tables(gru_start_paddr, gru_start_vaddr);
390 if (ret) {
391 printk(KERN_ERR "%s: init tables failed\n", GRU_DRIVER_ID_STR);
392 goto exit3;
394 gru_kservices_init();
396 printk(KERN_INFO "%s: v%s\n", GRU_DRIVER_ID_STR,
397 GRU_DRIVER_VERSION_STR);
398 return 0;
400 exit3:
401 gru_proc_exit();
402 exit2:
403 misc_deregister(&gru_miscdev);
404 exit1:
405 for (--chip; chip >= 0; chip--)
406 free_irq(irq + chip, NULL);
407 return ret;
411 static void __exit gru_exit(void)
413 int i, bid;
414 int order = get_order(sizeof(struct gru_state) *
415 GRU_CHIPLETS_PER_BLADE);
417 if (!is_uv_system())
418 return;
420 for (i = 0; i < GRU_CHIPLETS_PER_BLADE; i++)
421 free_irq(IRQ_GRU + i, NULL);
422 gru_kservices_exit();
423 for (bid = 0; bid < GRU_MAX_BLADES; bid++)
424 free_pages((unsigned long)gru_base[bid], order);
426 misc_deregister(&gru_miscdev);
427 gru_proc_exit();
430 static struct file_operations gru_fops = {
431 .owner = THIS_MODULE,
432 .unlocked_ioctl = gru_file_unlocked_ioctl,
433 .mmap = gru_file_mmap,
436 static struct miscdevice gru_miscdev = {
437 .minor = MISC_DYNAMIC_MINOR,
438 .name = "gru",
439 .fops = &gru_fops,
442 struct vm_operations_struct gru_vm_ops = {
443 .close = gru_vma_close,
444 .fault = gru_fault,
447 #ifndef MODULE
448 fs_initcall(gru_init);
449 #else
450 module_init(gru_init);
451 #endif
452 module_exit(gru_exit);
454 module_param(gru_options, ulong, 0644);
455 MODULE_PARM_DESC(gru_options, "Various debug options");
457 MODULE_AUTHOR("Silicon Graphics, Inc.");
458 MODULE_LICENSE("GPL");
459 MODULE_DESCRIPTION(GRU_DRIVER_ID_STR GRU_DRIVER_VERSION_STR);
460 MODULE_VERSION(GRU_DRIVER_VERSION_STR);