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