9a8a29339d2d674cbbb27d300ae5e6c6ba204199
[deliverable/linux.git] / arch / ia64 / sn / kernel / sn2 / sn_hwperf.c
1 /*
2 * This file is subject to the terms and conditions of the GNU General Public
3 * License. See the file "COPYING" in the main directory of this archive
4 * for more details.
5 *
6 * Copyright (C) 2004-2006 Silicon Graphics, Inc. All rights reserved.
7 *
8 * SGI Altix topology and hardware performance monitoring API.
9 * Mark Goodwin <markgw@sgi.com>.
10 *
11 * Creates /proc/sgi_sn/sn_topology (read-only) to export
12 * info about Altix nodes, routers, CPUs and NumaLink
13 * interconnection/topology.
14 *
15 * Also creates a dynamic misc device named "sn_hwperf"
16 * that supports an ioctl interface to call down into SAL
17 * to discover hw objects, topology and to read/write
18 * memory mapped registers, e.g. for performance monitoring.
19 * The "sn_hwperf" device is registered only after the procfs
20 * file is first opened, i.e. only if/when it's needed.
21 *
22 * This API is used by SGI Performance Co-Pilot and other
23 * tools, see http://oss.sgi.com/projects/pcp
24 */
25
26 #include <linux/fs.h>
27 #include <linux/slab.h>
28 #include <linux/vmalloc.h>
29 #include <linux/seq_file.h>
30 #include <linux/miscdevice.h>
31 #include <linux/utsname.h>
32 #include <linux/cpumask.h>
33 #include <linux/smp_lock.h>
34 #include <linux/nodemask.h>
35 #include <asm/processor.h>
36 #include <asm/topology.h>
37 #include <asm/smp.h>
38 #include <asm/semaphore.h>
39 #include <asm/uaccess.h>
40 #include <asm/sal.h>
41 #include <asm/sn/io.h>
42 #include <asm/sn/sn_sal.h>
43 #include <asm/sn/module.h>
44 #include <asm/sn/geo.h>
45 #include <asm/sn/sn2/sn_hwperf.h>
46 #include <asm/sn/addrs.h>
47
48 static void *sn_hwperf_salheap = NULL;
49 static int sn_hwperf_obj_cnt = 0;
50 static nasid_t sn_hwperf_master_nasid = INVALID_NASID;
51 static int sn_hwperf_init(void);
52 static DECLARE_MUTEX(sn_hwperf_init_mutex);
53
54 #define cnode_possible(n) ((n) < num_cnodes)
55
56 static int sn_hwperf_enum_objects(int *nobj, struct sn_hwperf_object_info **ret)
57 {
58 int e;
59 u64 sz;
60 struct sn_hwperf_object_info *objbuf = NULL;
61
62 if ((e = sn_hwperf_init()) < 0) {
63 printk(KERN_ERR "sn_hwperf_init failed: err %d\n", e);
64 goto out;
65 }
66
67 sz = sn_hwperf_obj_cnt * sizeof(struct sn_hwperf_object_info);
68 if ((objbuf = (struct sn_hwperf_object_info *) vmalloc(sz)) == NULL) {
69 printk("sn_hwperf_enum_objects: vmalloc(%d) failed\n", (int)sz);
70 e = -ENOMEM;
71 goto out;
72 }
73
74 e = ia64_sn_hwperf_op(sn_hwperf_master_nasid, SN_HWPERF_ENUM_OBJECTS,
75 0, sz, (u64) objbuf, 0, 0, NULL);
76 if (e != SN_HWPERF_OP_OK) {
77 e = -EINVAL;
78 vfree(objbuf);
79 }
80
81 out:
82 *nobj = sn_hwperf_obj_cnt;
83 *ret = objbuf;
84 return e;
85 }
86
87 static int sn_hwperf_location_to_bpos(char *location,
88 int *rack, int *bay, int *slot, int *slab)
89 {
90 char type;
91
92 /* first scan for an old style geoid string */
93 if (sscanf(location, "%03d%c%02d#%d",
94 rack, &type, bay, slab) == 4)
95 *slot = 0;
96 else /* scan for a new bladed geoid string */
97 if (sscanf(location, "%03d%c%02d^%02d#%d",
98 rack, &type, bay, slot, slab) != 5)
99 return -1;
100 /* success */
101 return 0;
102 }
103
104 static int sn_hwperf_geoid_to_cnode(char *location)
105 {
106 int cnode;
107 geoid_t geoid;
108 moduleid_t module_id;
109 int rack, bay, slot, slab;
110 int this_rack, this_bay, this_slot, this_slab;
111
112 if (sn_hwperf_location_to_bpos(location, &rack, &bay, &slot, &slab))
113 return -1;
114
115 /*
116 * FIXME: replace with cleaner for_each_XXX macro which addresses
117 * both compute and IO nodes once ACPI3.0 is available.
118 */
119 for (cnode = 0; cnode < num_cnodes; cnode++) {
120 geoid = cnodeid_get_geoid(cnode);
121 module_id = geo_module(geoid);
122 this_rack = MODULE_GET_RACK(module_id);
123 this_bay = MODULE_GET_BPOS(module_id);
124 this_slot = geo_slot(geoid);
125 this_slab = geo_slab(geoid);
126 if (rack == this_rack && bay == this_bay &&
127 slot == this_slot && slab == this_slab) {
128 break;
129 }
130 }
131
132 return cnode_possible(cnode) ? cnode : -1;
133 }
134
135 static int sn_hwperf_obj_to_cnode(struct sn_hwperf_object_info * obj)
136 {
137 if (!SN_HWPERF_IS_NODE(obj) && !SN_HWPERF_IS_IONODE(obj))
138 BUG();
139 if (SN_HWPERF_FOREIGN(obj))
140 return -1;
141 return sn_hwperf_geoid_to_cnode(obj->location);
142 }
143
144 static int sn_hwperf_generic_ordinal(struct sn_hwperf_object_info *obj,
145 struct sn_hwperf_object_info *objs)
146 {
147 int ordinal;
148 struct sn_hwperf_object_info *p;
149
150 for (ordinal=0, p=objs; p != obj; p++) {
151 if (SN_HWPERF_FOREIGN(p))
152 continue;
153 if (SN_HWPERF_SAME_OBJTYPE(p, obj))
154 ordinal++;
155 }
156
157 return ordinal;
158 }
159
160 static const char *slabname_node = "node"; /* SHub asic */
161 static const char *slabname_ionode = "ionode"; /* TIO asic */
162 static const char *slabname_router = "router"; /* NL3R or NL4R */
163 static const char *slabname_other = "other"; /* unknown asic */
164
165 static const char *sn_hwperf_get_slabname(struct sn_hwperf_object_info *obj,
166 struct sn_hwperf_object_info *objs, int *ordinal)
167 {
168 int isnode;
169 const char *slabname = slabname_other;
170
171 if ((isnode = SN_HWPERF_IS_NODE(obj)) || SN_HWPERF_IS_IONODE(obj)) {
172 slabname = isnode ? slabname_node : slabname_ionode;
173 *ordinal = sn_hwperf_obj_to_cnode(obj);
174 }
175 else {
176 *ordinal = sn_hwperf_generic_ordinal(obj, objs);
177 if (SN_HWPERF_IS_ROUTER(obj))
178 slabname = slabname_router;
179 }
180
181 return slabname;
182 }
183
184 static void print_pci_topology(struct seq_file *s)
185 {
186 char *p;
187 size_t sz;
188 int e;
189
190 for (sz = PAGE_SIZE; sz < 16 * PAGE_SIZE; sz += PAGE_SIZE) {
191 if (!(p = (char *)kmalloc(sz, GFP_KERNEL)))
192 break;
193 e = ia64_sn_ioif_get_pci_topology(__pa(p), sz);
194 if (e == SALRET_OK)
195 seq_puts(s, p);
196 kfree(p);
197 if (e == SALRET_OK || e == SALRET_NOT_IMPLEMENTED)
198 break;
199 }
200 }
201
202 static inline int sn_hwperf_has_cpus(cnodeid_t node)
203 {
204 return node < MAX_NUMNODES && node_online(node) && nr_cpus_node(node);
205 }
206
207 static inline int sn_hwperf_has_mem(cnodeid_t node)
208 {
209 return node < MAX_NUMNODES && node_online(node) && NODE_DATA(node)->node_present_pages;
210 }
211
212 static struct sn_hwperf_object_info *
213 sn_hwperf_findobj_id(struct sn_hwperf_object_info *objbuf,
214 int nobj, int id)
215 {
216 int i;
217 struct sn_hwperf_object_info *p = objbuf;
218
219 for (i=0; i < nobj; i++, p++) {
220 if (p->id == id)
221 return p;
222 }
223
224 return NULL;
225
226 }
227
228 static int sn_hwperf_get_nearest_node_objdata(struct sn_hwperf_object_info *objbuf,
229 int nobj, cnodeid_t node, cnodeid_t *near_mem_node, cnodeid_t *near_cpu_node)
230 {
231 int e;
232 struct sn_hwperf_object_info *nodeobj = NULL;
233 struct sn_hwperf_object_info *op;
234 struct sn_hwperf_object_info *dest;
235 struct sn_hwperf_object_info *router;
236 struct sn_hwperf_port_info ptdata[16];
237 int sz, i, j;
238 cnodeid_t c;
239 int found_mem = 0;
240 int found_cpu = 0;
241
242 if (!cnode_possible(node))
243 return -EINVAL;
244
245 if (sn_hwperf_has_cpus(node)) {
246 if (near_cpu_node)
247 *near_cpu_node = node;
248 found_cpu++;
249 }
250
251 if (sn_hwperf_has_mem(node)) {
252 if (near_mem_node)
253 *near_mem_node = node;
254 found_mem++;
255 }
256
257 if (found_cpu && found_mem)
258 return 0; /* trivially successful */
259
260 /* find the argument node object */
261 for (i=0, op=objbuf; i < nobj; i++, op++) {
262 if (!SN_HWPERF_IS_NODE(op) && !SN_HWPERF_IS_IONODE(op))
263 continue;
264 if (node == sn_hwperf_obj_to_cnode(op)) {
265 nodeobj = op;
266 break;
267 }
268 }
269 if (!nodeobj) {
270 e = -ENOENT;
271 goto err;
272 }
273
274 /* get it's interconnect topology */
275 sz = op->ports * sizeof(struct sn_hwperf_port_info);
276 if (sz > sizeof(ptdata))
277 BUG();
278 e = ia64_sn_hwperf_op(sn_hwperf_master_nasid,
279 SN_HWPERF_ENUM_PORTS, nodeobj->id, sz,
280 (u64)&ptdata, 0, 0, NULL);
281 if (e != SN_HWPERF_OP_OK) {
282 e = -EINVAL;
283 goto err;
284 }
285
286 /* find nearest node with cpus and nearest memory */
287 for (router=NULL, j=0; j < op->ports; j++) {
288 dest = sn_hwperf_findobj_id(objbuf, nobj, ptdata[j].conn_id);
289 if (dest && SN_HWPERF_IS_ROUTER(dest))
290 router = dest;
291 if (!dest || SN_HWPERF_FOREIGN(dest) ||
292 !SN_HWPERF_IS_NODE(dest) || SN_HWPERF_IS_IONODE(dest)) {
293 continue;
294 }
295 c = sn_hwperf_obj_to_cnode(dest);
296 if (!found_cpu && sn_hwperf_has_cpus(c)) {
297 if (near_cpu_node)
298 *near_cpu_node = c;
299 found_cpu++;
300 }
301 if (!found_mem && sn_hwperf_has_mem(c)) {
302 if (near_mem_node)
303 *near_mem_node = c;
304 found_mem++;
305 }
306 }
307
308 if (router && (!found_cpu || !found_mem)) {
309 /* search for a node connected to the same router */
310 sz = router->ports * sizeof(struct sn_hwperf_port_info);
311 if (sz > sizeof(ptdata))
312 BUG();
313 e = ia64_sn_hwperf_op(sn_hwperf_master_nasid,
314 SN_HWPERF_ENUM_PORTS, router->id, sz,
315 (u64)&ptdata, 0, 0, NULL);
316 if (e != SN_HWPERF_OP_OK) {
317 e = -EINVAL;
318 goto err;
319 }
320 for (j=0; j < router->ports; j++) {
321 dest = sn_hwperf_findobj_id(objbuf, nobj,
322 ptdata[j].conn_id);
323 if (!dest || dest->id == node ||
324 SN_HWPERF_FOREIGN(dest) ||
325 !SN_HWPERF_IS_NODE(dest) ||
326 SN_HWPERF_IS_IONODE(dest)) {
327 continue;
328 }
329 c = sn_hwperf_obj_to_cnode(dest);
330 if (!found_cpu && sn_hwperf_has_cpus(c)) {
331 if (near_cpu_node)
332 *near_cpu_node = c;
333 found_cpu++;
334 }
335 if (!found_mem && sn_hwperf_has_mem(c)) {
336 if (near_mem_node)
337 *near_mem_node = c;
338 found_mem++;
339 }
340 if (found_cpu && found_mem)
341 break;
342 }
343 }
344
345 if (!found_cpu || !found_mem) {
346 /* resort to _any_ node with CPUs and memory */
347 for (i=0, op=objbuf; i < nobj; i++, op++) {
348 if (SN_HWPERF_FOREIGN(op) ||
349 SN_HWPERF_IS_IONODE(op) ||
350 !SN_HWPERF_IS_NODE(op)) {
351 continue;
352 }
353 c = sn_hwperf_obj_to_cnode(op);
354 if (!found_cpu && sn_hwperf_has_cpus(c)) {
355 if (near_cpu_node)
356 *near_cpu_node = c;
357 found_cpu++;
358 }
359 if (!found_mem && sn_hwperf_has_mem(c)) {
360 if (near_mem_node)
361 *near_mem_node = c;
362 found_mem++;
363 }
364 if (found_cpu && found_mem)
365 break;
366 }
367 }
368
369 if (!found_cpu || !found_mem)
370 e = -ENODATA;
371
372 err:
373 return e;
374 }
375
376
377 static int sn_topology_show(struct seq_file *s, void *d)
378 {
379 int sz;
380 int pt;
381 int e = 0;
382 int i;
383 int j;
384 const char *slabname;
385 int ordinal;
386 cpumask_t cpumask;
387 char slice;
388 struct cpuinfo_ia64 *c;
389 struct sn_hwperf_port_info *ptdata;
390 struct sn_hwperf_object_info *p;
391 struct sn_hwperf_object_info *obj = d; /* this object */
392 struct sn_hwperf_object_info *objs = s->private; /* all objects */
393 u8 shubtype;
394 u8 system_size;
395 u8 sharing_size;
396 u8 partid;
397 u8 coher;
398 u8 nasid_shift;
399 u8 region_size;
400 u16 nasid_mask;
401 int nasid_msb;
402
403 if (obj == objs) {
404 seq_printf(s, "# sn_topology version 2\n");
405 seq_printf(s, "# objtype ordinal location partition"
406 " [attribute value [, ...]]\n");
407
408 if (ia64_sn_get_sn_info(0,
409 &shubtype, &nasid_mask, &nasid_shift, &system_size,
410 &sharing_size, &partid, &coher, &region_size))
411 BUG();
412 for (nasid_msb=63; nasid_msb > 0; nasid_msb--) {
413 if (((u64)nasid_mask << nasid_shift) & (1ULL << nasid_msb))
414 break;
415 }
416 seq_printf(s, "partition %u %s local "
417 "shubtype %s, "
418 "nasid_mask 0x%016lx, "
419 "nasid_bits %d:%d, "
420 "system_size %d, "
421 "sharing_size %d, "
422 "coherency_domain %d, "
423 "region_size %d\n",
424
425 partid, system_utsname.nodename,
426 shubtype ? "shub2" : "shub1",
427 (u64)nasid_mask << nasid_shift, nasid_msb, nasid_shift,
428 system_size, sharing_size, coher, region_size);
429
430 print_pci_topology(s);
431 }
432
433 if (SN_HWPERF_FOREIGN(obj)) {
434 /* private in another partition: not interesting */
435 return 0;
436 }
437
438 for (i = 0; i < SN_HWPERF_MAXSTRING && obj->name[i]; i++) {
439 if (obj->name[i] == ' ')
440 obj->name[i] = '_';
441 }
442
443 slabname = sn_hwperf_get_slabname(obj, objs, &ordinal);
444 seq_printf(s, "%s %d %s %s asic %s", slabname, ordinal, obj->location,
445 obj->sn_hwp_this_part ? "local" : "shared", obj->name);
446
447 if (ordinal < 0 || (!SN_HWPERF_IS_NODE(obj) && !SN_HWPERF_IS_IONODE(obj)))
448 seq_putc(s, '\n');
449 else {
450 cnodeid_t near_mem = -1;
451 cnodeid_t near_cpu = -1;
452
453 seq_printf(s, ", nasid 0x%x", cnodeid_to_nasid(ordinal));
454
455 if (sn_hwperf_get_nearest_node_objdata(objs, sn_hwperf_obj_cnt,
456 ordinal, &near_mem, &near_cpu) == 0) {
457 seq_printf(s, ", near_mem_nodeid %d, near_cpu_nodeid %d",
458 near_mem, near_cpu);
459 }
460
461 if (!SN_HWPERF_IS_IONODE(obj)) {
462 for_each_online_node(i) {
463 seq_printf(s, i ? ":%d" : ", dist %d",
464 node_distance(ordinal, i));
465 }
466 }
467
468 seq_putc(s, '\n');
469
470 /*
471 * CPUs on this node, if any
472 */
473 if (!SN_HWPERF_IS_IONODE(obj)) {
474 cpumask = node_to_cpumask(ordinal);
475 for_each_online_cpu(i) {
476 if (cpu_isset(i, cpumask)) {
477 slice = 'a' + cpuid_to_slice(i);
478 c = cpu_data(i);
479 seq_printf(s, "cpu %d %s%c local"
480 " freq %luMHz, arch ia64",
481 i, obj->location, slice,
482 c->proc_freq / 1000000);
483 for_each_online_cpu(j) {
484 seq_printf(s, j ? ":%d" : ", dist %d",
485 node_distance(
486 cpu_to_node(i),
487 cpu_to_node(j)));
488 }
489 seq_putc(s, '\n');
490 }
491 }
492 }
493 }
494
495 if (obj->ports) {
496 /*
497 * numalink ports
498 */
499 sz = obj->ports * sizeof(struct sn_hwperf_port_info);
500 if ((ptdata = kmalloc(sz, GFP_KERNEL)) == NULL)
501 return -ENOMEM;
502 e = ia64_sn_hwperf_op(sn_hwperf_master_nasid,
503 SN_HWPERF_ENUM_PORTS, obj->id, sz,
504 (u64) ptdata, 0, 0, NULL);
505 if (e != SN_HWPERF_OP_OK)
506 return -EINVAL;
507 for (ordinal=0, p=objs; p != obj; p++) {
508 if (!SN_HWPERF_FOREIGN(p))
509 ordinal += p->ports;
510 }
511 for (pt = 0; pt < obj->ports; pt++) {
512 for (p = objs, i = 0; i < sn_hwperf_obj_cnt; i++, p++) {
513 if (ptdata[pt].conn_id == p->id) {
514 break;
515 }
516 }
517 seq_printf(s, "numalink %d %s-%d",
518 ordinal+pt, obj->location, ptdata[pt].port);
519
520 if (i >= sn_hwperf_obj_cnt) {
521 /* no connection */
522 seq_puts(s, " local endpoint disconnected"
523 ", protocol unknown\n");
524 continue;
525 }
526
527 if (obj->sn_hwp_this_part && p->sn_hwp_this_part)
528 /* both ends local to this partition */
529 seq_puts(s, " local");
530 else if (SN_HWPERF_FOREIGN(p))
531 /* both ends of the link in foreign partiton */
532 seq_puts(s, " foreign");
533 else
534 /* link straddles a partition */
535 seq_puts(s, " shared");
536
537 /*
538 * Unlikely, but strictly should query the LLP config
539 * registers because an NL4R can be configured to run
540 * NL3 protocol, even when not talking to an NL3 router.
541 * Ditto for node-node.
542 */
543 seq_printf(s, " endpoint %s-%d, protocol %s\n",
544 p->location, ptdata[pt].conn_port,
545 (SN_HWPERF_IS_NL3ROUTER(obj) ||
546 SN_HWPERF_IS_NL3ROUTER(p)) ? "LLP3" : "LLP4");
547 }
548 kfree(ptdata);
549 }
550
551 return 0;
552 }
553
554 static void *sn_topology_start(struct seq_file *s, loff_t * pos)
555 {
556 struct sn_hwperf_object_info *objs = s->private;
557
558 if (*pos < sn_hwperf_obj_cnt)
559 return (void *)(objs + *pos);
560
561 return NULL;
562 }
563
564 static void *sn_topology_next(struct seq_file *s, void *v, loff_t * pos)
565 {
566 ++*pos;
567 return sn_topology_start(s, pos);
568 }
569
570 static void sn_topology_stop(struct seq_file *m, void *v)
571 {
572 return;
573 }
574
575 /*
576 * /proc/sgi_sn/sn_topology, read-only using seq_file
577 */
578 static struct seq_operations sn_topology_seq_ops = {
579 .start = sn_topology_start,
580 .next = sn_topology_next,
581 .stop = sn_topology_stop,
582 .show = sn_topology_show
583 };
584
585 struct sn_hwperf_op_info {
586 u64 op;
587 struct sn_hwperf_ioctl_args *a;
588 void *p;
589 int *v0;
590 int ret;
591 };
592
593 static void sn_hwperf_call_sal(void *info)
594 {
595 struct sn_hwperf_op_info *op_info = info;
596 int r;
597
598 r = ia64_sn_hwperf_op(sn_hwperf_master_nasid, op_info->op,
599 op_info->a->arg, op_info->a->sz,
600 (u64) op_info->p, 0, 0, op_info->v0);
601 op_info->ret = r;
602 }
603
604 static int sn_hwperf_op_cpu(struct sn_hwperf_op_info *op_info)
605 {
606 u32 cpu;
607 u32 use_ipi;
608 int r = 0;
609 cpumask_t save_allowed;
610
611 cpu = (op_info->a->arg & SN_HWPERF_ARG_CPU_MASK) >> 32;
612 use_ipi = op_info->a->arg & SN_HWPERF_ARG_USE_IPI_MASK;
613 op_info->a->arg &= SN_HWPERF_ARG_OBJID_MASK;
614
615 if (cpu != SN_HWPERF_ARG_ANY_CPU) {
616 if (cpu >= NR_CPUS || !cpu_online(cpu)) {
617 r = -EINVAL;
618 goto out;
619 }
620 }
621
622 if (cpu == SN_HWPERF_ARG_ANY_CPU || cpu == get_cpu()) {
623 /* don't care, or already on correct cpu */
624 sn_hwperf_call_sal(op_info);
625 }
626 else {
627 if (use_ipi) {
628 /* use an interprocessor interrupt to call SAL */
629 smp_call_function_single(cpu, sn_hwperf_call_sal,
630 op_info, 1, 1);
631 }
632 else {
633 /* migrate the task before calling SAL */
634 save_allowed = current->cpus_allowed;
635 set_cpus_allowed(current, cpumask_of_cpu(cpu));
636 sn_hwperf_call_sal(op_info);
637 set_cpus_allowed(current, save_allowed);
638 }
639 }
640 r = op_info->ret;
641
642 out:
643 return r;
644 }
645
646 /* map SAL hwperf error code to system error code */
647 static int sn_hwperf_map_err(int hwperf_err)
648 {
649 int e;
650
651 switch(hwperf_err) {
652 case SN_HWPERF_OP_OK:
653 e = 0;
654 break;
655
656 case SN_HWPERF_OP_NOMEM:
657 e = -ENOMEM;
658 break;
659
660 case SN_HWPERF_OP_NO_PERM:
661 e = -EPERM;
662 break;
663
664 case SN_HWPERF_OP_IO_ERROR:
665 e = -EIO;
666 break;
667
668 case SN_HWPERF_OP_BUSY:
669 e = -EBUSY;
670 break;
671
672 case SN_HWPERF_OP_RECONFIGURE:
673 e = -EAGAIN;
674 break;
675
676 case SN_HWPERF_OP_INVAL:
677 default:
678 e = -EINVAL;
679 break;
680 }
681
682 return e;
683 }
684
685 /*
686 * ioctl for "sn_hwperf" misc device
687 */
688 static int
689 sn_hwperf_ioctl(struct inode *in, struct file *fp, u32 op, u64 arg)
690 {
691 struct sn_hwperf_ioctl_args a;
692 struct cpuinfo_ia64 *cdata;
693 struct sn_hwperf_object_info *objs;
694 struct sn_hwperf_object_info *cpuobj;
695 struct sn_hwperf_op_info op_info;
696 void *p = NULL;
697 int nobj;
698 char slice;
699 int node;
700 int r;
701 int v0;
702 int i;
703 int j;
704
705 unlock_kernel();
706
707 /* only user requests are allowed here */
708 if ((op & SN_HWPERF_OP_MASK) < 10) {
709 r = -EINVAL;
710 goto error;
711 }
712 r = copy_from_user(&a, (const void __user *)arg,
713 sizeof(struct sn_hwperf_ioctl_args));
714 if (r != 0) {
715 r = -EFAULT;
716 goto error;
717 }
718
719 /*
720 * Allocate memory to hold a kernel copy of the user buffer. The
721 * buffer contents are either copied in or out (or both) of user
722 * space depending on the flags encoded in the requested operation.
723 */
724 if (a.ptr) {
725 p = vmalloc(a.sz);
726 if (!p) {
727 r = -ENOMEM;
728 goto error;
729 }
730 }
731
732 if (op & SN_HWPERF_OP_MEM_COPYIN) {
733 r = copy_from_user(p, (const void __user *)a.ptr, a.sz);
734 if (r != 0) {
735 r = -EFAULT;
736 goto error;
737 }
738 }
739
740 switch (op) {
741 case SN_HWPERF_GET_CPU_INFO:
742 if (a.sz == sizeof(u64)) {
743 /* special case to get size needed */
744 *(u64 *) p = (u64) num_online_cpus() *
745 sizeof(struct sn_hwperf_object_info);
746 } else
747 if (a.sz < num_online_cpus() * sizeof(struct sn_hwperf_object_info)) {
748 r = -ENOMEM;
749 goto error;
750 } else
751 if ((r = sn_hwperf_enum_objects(&nobj, &objs)) == 0) {
752 memset(p, 0, a.sz);
753 for (i = 0; i < nobj; i++) {
754 int cpuobj_index = 0;
755 if (!SN_HWPERF_IS_NODE(objs + i))
756 continue;
757 node = sn_hwperf_obj_to_cnode(objs + i);
758 for_each_online_cpu(j) {
759 if (node != cpu_to_node(j))
760 continue;
761 cpuobj = (struct sn_hwperf_object_info *) p + cpuobj_index++;
762 slice = 'a' + cpuid_to_slice(j);
763 cdata = cpu_data(j);
764 cpuobj->id = j;
765 snprintf(cpuobj->name,
766 sizeof(cpuobj->name),
767 "CPU %luMHz %s",
768 cdata->proc_freq / 1000000,
769 cdata->vendor);
770 snprintf(cpuobj->location,
771 sizeof(cpuobj->location),
772 "%s%c", objs[i].location,
773 slice);
774 }
775 }
776
777 vfree(objs);
778 }
779 break;
780
781 case SN_HWPERF_GET_NODE_NASID:
782 if (a.sz != sizeof(u64) ||
783 (node = a.arg) < 0 || !cnode_possible(node)) {
784 r = -EINVAL;
785 goto error;
786 }
787 *(u64 *)p = (u64)cnodeid_to_nasid(node);
788 break;
789
790 case SN_HWPERF_GET_OBJ_NODE:
791 if (a.sz != sizeof(u64) || a.arg < 0) {
792 r = -EINVAL;
793 goto error;
794 }
795 if ((r = sn_hwperf_enum_objects(&nobj, &objs)) == 0) {
796 if (a.arg >= nobj) {
797 r = -EINVAL;
798 vfree(objs);
799 goto error;
800 }
801 if (objs[(i = a.arg)].id != a.arg) {
802 for (i = 0; i < nobj; i++) {
803 if (objs[i].id == a.arg)
804 break;
805 }
806 }
807 if (i == nobj) {
808 r = -EINVAL;
809 vfree(objs);
810 goto error;
811 }
812
813 if (!SN_HWPERF_IS_NODE(objs + i) &&
814 !SN_HWPERF_IS_IONODE(objs + i)) {
815 r = -ENOENT;
816 vfree(objs);
817 goto error;
818 }
819
820 *(u64 *)p = (u64)sn_hwperf_obj_to_cnode(objs + i);
821 vfree(objs);
822 }
823 break;
824
825 case SN_HWPERF_GET_MMRS:
826 case SN_HWPERF_SET_MMRS:
827 case SN_HWPERF_OBJECT_DISTANCE:
828 op_info.p = p;
829 op_info.a = &a;
830 op_info.v0 = &v0;
831 op_info.op = op;
832 r = sn_hwperf_op_cpu(&op_info);
833 if (r) {
834 r = sn_hwperf_map_err(r);
835 a.v0 = v0;
836 goto error;
837 }
838 break;
839
840 default:
841 /* all other ops are a direct SAL call */
842 r = ia64_sn_hwperf_op(sn_hwperf_master_nasid, op,
843 a.arg, a.sz, (u64) p, 0, 0, &v0);
844 if (r) {
845 r = sn_hwperf_map_err(r);
846 goto error;
847 }
848 a.v0 = v0;
849 break;
850 }
851
852 if (op & SN_HWPERF_OP_MEM_COPYOUT) {
853 r = copy_to_user((void __user *)a.ptr, p, a.sz);
854 if (r != 0) {
855 r = -EFAULT;
856 goto error;
857 }
858 }
859
860 error:
861 vfree(p);
862
863 lock_kernel();
864 return r;
865 }
866
867 static struct file_operations sn_hwperf_fops = {
868 .ioctl = sn_hwperf_ioctl,
869 };
870
871 static struct miscdevice sn_hwperf_dev = {
872 MISC_DYNAMIC_MINOR,
873 "sn_hwperf",
874 &sn_hwperf_fops
875 };
876
877 static int sn_hwperf_init(void)
878 {
879 u64 v;
880 int salr;
881 int e = 0;
882
883 /* single threaded, once-only initialization */
884 down(&sn_hwperf_init_mutex);
885
886 if (sn_hwperf_salheap) {
887 up(&sn_hwperf_init_mutex);
888 return e;
889 }
890
891 /*
892 * The PROM code needs a fixed reference node. For convenience the
893 * same node as the console I/O is used.
894 */
895 sn_hwperf_master_nasid = (nasid_t) ia64_sn_get_console_nasid();
896
897 /*
898 * Request the needed size and install the PROM scratch area.
899 * The PROM keeps various tracking bits in this memory area.
900 */
901 salr = ia64_sn_hwperf_op(sn_hwperf_master_nasid,
902 (u64) SN_HWPERF_GET_HEAPSIZE, 0,
903 (u64) sizeof(u64), (u64) &v, 0, 0, NULL);
904 if (salr != SN_HWPERF_OP_OK) {
905 e = -EINVAL;
906 goto out;
907 }
908
909 if ((sn_hwperf_salheap = vmalloc(v)) == NULL) {
910 e = -ENOMEM;
911 goto out;
912 }
913 salr = ia64_sn_hwperf_op(sn_hwperf_master_nasid,
914 SN_HWPERF_INSTALL_HEAP, 0, v,
915 (u64) sn_hwperf_salheap, 0, 0, NULL);
916 if (salr != SN_HWPERF_OP_OK) {
917 e = -EINVAL;
918 goto out;
919 }
920
921 salr = ia64_sn_hwperf_op(sn_hwperf_master_nasid,
922 SN_HWPERF_OBJECT_COUNT, 0,
923 sizeof(u64), (u64) &v, 0, 0, NULL);
924 if (salr != SN_HWPERF_OP_OK) {
925 e = -EINVAL;
926 goto out;
927 }
928 sn_hwperf_obj_cnt = (int)v;
929
930 out:
931 if (e < 0 && sn_hwperf_salheap) {
932 vfree(sn_hwperf_salheap);
933 sn_hwperf_salheap = NULL;
934 sn_hwperf_obj_cnt = 0;
935 }
936 up(&sn_hwperf_init_mutex);
937 return e;
938 }
939
940 int sn_topology_open(struct inode *inode, struct file *file)
941 {
942 int e;
943 struct seq_file *seq;
944 struct sn_hwperf_object_info *objbuf;
945 int nobj;
946
947 if ((e = sn_hwperf_enum_objects(&nobj, &objbuf)) == 0) {
948 e = seq_open(file, &sn_topology_seq_ops);
949 seq = file->private_data;
950 seq->private = objbuf;
951 }
952
953 return e;
954 }
955
956 int sn_topology_release(struct inode *inode, struct file *file)
957 {
958 struct seq_file *seq = file->private_data;
959
960 vfree(seq->private);
961 return seq_release(inode, file);
962 }
963
964 int sn_hwperf_get_nearest_node(cnodeid_t node,
965 cnodeid_t *near_mem_node, cnodeid_t *near_cpu_node)
966 {
967 int e;
968 int nobj;
969 struct sn_hwperf_object_info *objbuf;
970
971 if ((e = sn_hwperf_enum_objects(&nobj, &objbuf)) == 0) {
972 e = sn_hwperf_get_nearest_node_objdata(objbuf, nobj,
973 node, near_mem_node, near_cpu_node);
974 vfree(objbuf);
975 }
976
977 return e;
978 }
979
980 static int __devinit sn_hwperf_misc_register_init(void)
981 {
982 int e;
983
984 if (!ia64_platform_is("sn2"))
985 return 0;
986
987 sn_hwperf_init();
988
989 /*
990 * Register a dynamic misc device for hwperf ioctls. Platforms
991 * supporting hotplug will create /dev/sn_hwperf, else user
992 * can to look up the minor number in /proc/misc.
993 */
994 if ((e = misc_register(&sn_hwperf_dev)) != 0) {
995 printk(KERN_ERR "sn_hwperf_misc_register_init: failed to "
996 "register misc device for \"%s\"\n", sn_hwperf_dev.name);
997 }
998
999 return e;
1000 }
1001
1002 device_initcall(sn_hwperf_misc_register_init); /* after misc_init() */
1003 EXPORT_SYMBOL(sn_hwperf_get_nearest_node);
This page took 0.051009 seconds and 4 git commands to generate.