RT-AC56 3.0.0.4.374.37 core
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / arch / ia64 / sn / kernel / sn2 / sn_hwperf.c
blob5b794145ab5dbe405df9a5be0fbe0559c980c629
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.
6 * Copyright (C) 2004-2006 Silicon Graphics, Inc. All rights reserved.
8 * SGI Altix topology and hardware performance monitoring API.
9 * Mark Goodwin <markgw@sgi.com>.
11 * Creates /proc/sgi_sn/sn_topology (read-only) to export
12 * info about Altix nodes, routers, CPUs and NumaLink
13 * interconnection/topology.
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.
22 * This API is used by SGI Performance Co-Pilot and other
23 * tools, see http://oss.sgi.com/projects/pcp
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/nodemask.h>
34 #include <linux/smp.h>
35 #include <linux/mutex.h>
37 #include <asm/processor.h>
38 #include <asm/topology.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>
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 DEFINE_MUTEX(sn_hwperf_init_mutex);
54 #define cnode_possible(n) ((n) < num_cnodes)
56 static int sn_hwperf_enum_objects(int *nobj, struct sn_hwperf_object_info **ret)
58 int e;
59 u64 sz;
60 struct sn_hwperf_object_info *objbuf = NULL;
62 if ((e = sn_hwperf_init()) < 0) {
63 printk(KERN_ERR "sn_hwperf_init failed: err %d\n", e);
64 goto out;
67 sz = sn_hwperf_obj_cnt * sizeof(struct sn_hwperf_object_info);
68 objbuf = vmalloc(sz);
69 if (objbuf == NULL) {
70 printk("sn_hwperf_enum_objects: vmalloc(%d) failed\n", (int)sz);
71 e = -ENOMEM;
72 goto out;
75 e = ia64_sn_hwperf_op(sn_hwperf_master_nasid, SN_HWPERF_ENUM_OBJECTS,
76 0, sz, (u64) objbuf, 0, 0, NULL);
77 if (e != SN_HWPERF_OP_OK) {
78 e = -EINVAL;
79 vfree(objbuf);
82 out:
83 *nobj = sn_hwperf_obj_cnt;
84 *ret = objbuf;
85 return e;
88 static int sn_hwperf_location_to_bpos(char *location,
89 int *rack, int *bay, int *slot, int *slab)
91 char type;
93 /* first scan for an old style geoid string */
94 if (sscanf(location, "%03d%c%02d#%d",
95 rack, &type, bay, slab) == 4)
96 *slot = 0;
97 else /* scan for a new bladed geoid string */
98 if (sscanf(location, "%03d%c%02d^%02d#%d",
99 rack, &type, bay, slot, slab) != 5)
100 return -1;
101 /* success */
102 return 0;
105 static int sn_hwperf_geoid_to_cnode(char *location)
107 int cnode;
108 geoid_t geoid;
109 moduleid_t module_id;
110 int rack, bay, slot, slab;
111 int this_rack, this_bay, this_slot, this_slab;
113 if (sn_hwperf_location_to_bpos(location, &rack, &bay, &slot, &slab))
114 return -1;
116 for (cnode = 0; cnode < num_cnodes; cnode++) {
117 geoid = cnodeid_get_geoid(cnode);
118 module_id = geo_module(geoid);
119 this_rack = MODULE_GET_RACK(module_id);
120 this_bay = MODULE_GET_BPOS(module_id);
121 this_slot = geo_slot(geoid);
122 this_slab = geo_slab(geoid);
123 if (rack == this_rack && bay == this_bay &&
124 slot == this_slot && slab == this_slab) {
125 break;
129 return cnode_possible(cnode) ? cnode : -1;
132 static int sn_hwperf_obj_to_cnode(struct sn_hwperf_object_info * obj)
134 if (!SN_HWPERF_IS_NODE(obj) && !SN_HWPERF_IS_IONODE(obj))
135 BUG();
136 if (SN_HWPERF_FOREIGN(obj))
137 return -1;
138 return sn_hwperf_geoid_to_cnode(obj->location);
141 static int sn_hwperf_generic_ordinal(struct sn_hwperf_object_info *obj,
142 struct sn_hwperf_object_info *objs)
144 int ordinal;
145 struct sn_hwperf_object_info *p;
147 for (ordinal=0, p=objs; p != obj; p++) {
148 if (SN_HWPERF_FOREIGN(p))
149 continue;
150 if (SN_HWPERF_SAME_OBJTYPE(p, obj))
151 ordinal++;
154 return ordinal;
157 static const char *slabname_node = "node"; /* SHub asic */
158 static const char *slabname_ionode = "ionode"; /* TIO asic */
159 static const char *slabname_router = "router"; /* NL3R or NL4R */
160 static const char *slabname_other = "other"; /* unknown asic */
162 static const char *sn_hwperf_get_slabname(struct sn_hwperf_object_info *obj,
163 struct sn_hwperf_object_info *objs, int *ordinal)
165 int isnode;
166 const char *slabname = slabname_other;
168 if ((isnode = SN_HWPERF_IS_NODE(obj)) || SN_HWPERF_IS_IONODE(obj)) {
169 slabname = isnode ? slabname_node : slabname_ionode;
170 *ordinal = sn_hwperf_obj_to_cnode(obj);
172 else {
173 *ordinal = sn_hwperf_generic_ordinal(obj, objs);
174 if (SN_HWPERF_IS_ROUTER(obj))
175 slabname = slabname_router;
178 return slabname;
181 static void print_pci_topology(struct seq_file *s)
183 char *p;
184 size_t sz;
185 int e;
187 for (sz = PAGE_SIZE; sz < 16 * PAGE_SIZE; sz += PAGE_SIZE) {
188 if (!(p = kmalloc(sz, GFP_KERNEL)))
189 break;
190 e = ia64_sn_ioif_get_pci_topology(__pa(p), sz);
191 if (e == SALRET_OK)
192 seq_puts(s, p);
193 kfree(p);
194 if (e == SALRET_OK || e == SALRET_NOT_IMPLEMENTED)
195 break;
199 static inline int sn_hwperf_has_cpus(cnodeid_t node)
201 return node < MAX_NUMNODES && node_online(node) && nr_cpus_node(node);
204 static inline int sn_hwperf_has_mem(cnodeid_t node)
206 return node < MAX_NUMNODES && node_online(node) && NODE_DATA(node)->node_present_pages;
209 static struct sn_hwperf_object_info *
210 sn_hwperf_findobj_id(struct sn_hwperf_object_info *objbuf,
211 int nobj, int id)
213 int i;
214 struct sn_hwperf_object_info *p = objbuf;
216 for (i=0; i < nobj; i++, p++) {
217 if (p->id == id)
218 return p;
221 return NULL;
225 static int sn_hwperf_get_nearest_node_objdata(struct sn_hwperf_object_info *objbuf,
226 int nobj, cnodeid_t node, cnodeid_t *near_mem_node, cnodeid_t *near_cpu_node)
228 int e;
229 struct sn_hwperf_object_info *nodeobj = NULL;
230 struct sn_hwperf_object_info *op;
231 struct sn_hwperf_object_info *dest;
232 struct sn_hwperf_object_info *router;
233 struct sn_hwperf_port_info ptdata[16];
234 int sz, i, j;
235 cnodeid_t c;
236 int found_mem = 0;
237 int found_cpu = 0;
239 if (!cnode_possible(node))
240 return -EINVAL;
242 if (sn_hwperf_has_cpus(node)) {
243 if (near_cpu_node)
244 *near_cpu_node = node;
245 found_cpu++;
248 if (sn_hwperf_has_mem(node)) {
249 if (near_mem_node)
250 *near_mem_node = node;
251 found_mem++;
254 if (found_cpu && found_mem)
255 return 0; /* trivially successful */
257 /* find the argument node object */
258 for (i=0, op=objbuf; i < nobj; i++, op++) {
259 if (!SN_HWPERF_IS_NODE(op) && !SN_HWPERF_IS_IONODE(op))
260 continue;
261 if (node == sn_hwperf_obj_to_cnode(op)) {
262 nodeobj = op;
263 break;
266 if (!nodeobj) {
267 e = -ENOENT;
268 goto err;
271 /* get it's interconnect topology */
272 sz = op->ports * sizeof(struct sn_hwperf_port_info);
273 BUG_ON(sz > sizeof(ptdata));
274 e = ia64_sn_hwperf_op(sn_hwperf_master_nasid,
275 SN_HWPERF_ENUM_PORTS, nodeobj->id, sz,
276 (u64)&ptdata, 0, 0, NULL);
277 if (e != SN_HWPERF_OP_OK) {
278 e = -EINVAL;
279 goto err;
282 /* find nearest node with cpus and nearest memory */
283 for (router=NULL, j=0; j < op->ports; j++) {
284 dest = sn_hwperf_findobj_id(objbuf, nobj, ptdata[j].conn_id);
285 if (dest && SN_HWPERF_IS_ROUTER(dest))
286 router = dest;
287 if (!dest || SN_HWPERF_FOREIGN(dest) ||
288 !SN_HWPERF_IS_NODE(dest) || SN_HWPERF_IS_IONODE(dest)) {
289 continue;
291 c = sn_hwperf_obj_to_cnode(dest);
292 if (!found_cpu && sn_hwperf_has_cpus(c)) {
293 if (near_cpu_node)
294 *near_cpu_node = c;
295 found_cpu++;
297 if (!found_mem && sn_hwperf_has_mem(c)) {
298 if (near_mem_node)
299 *near_mem_node = c;
300 found_mem++;
304 if (router && (!found_cpu || !found_mem)) {
305 /* search for a node connected to the same router */
306 sz = router->ports * sizeof(struct sn_hwperf_port_info);
307 BUG_ON(sz > sizeof(ptdata));
308 e = ia64_sn_hwperf_op(sn_hwperf_master_nasid,
309 SN_HWPERF_ENUM_PORTS, router->id, sz,
310 (u64)&ptdata, 0, 0, NULL);
311 if (e != SN_HWPERF_OP_OK) {
312 e = -EINVAL;
313 goto err;
315 for (j=0; j < router->ports; j++) {
316 dest = sn_hwperf_findobj_id(objbuf, nobj,
317 ptdata[j].conn_id);
318 if (!dest || dest->id == node ||
319 SN_HWPERF_FOREIGN(dest) ||
320 !SN_HWPERF_IS_NODE(dest) ||
321 SN_HWPERF_IS_IONODE(dest)) {
322 continue;
324 c = sn_hwperf_obj_to_cnode(dest);
325 if (!found_cpu && sn_hwperf_has_cpus(c)) {
326 if (near_cpu_node)
327 *near_cpu_node = c;
328 found_cpu++;
330 if (!found_mem && sn_hwperf_has_mem(c)) {
331 if (near_mem_node)
332 *near_mem_node = c;
333 found_mem++;
335 if (found_cpu && found_mem)
336 break;
340 if (!found_cpu || !found_mem) {
341 /* resort to _any_ node with CPUs and memory */
342 for (i=0, op=objbuf; i < nobj; i++, op++) {
343 if (SN_HWPERF_FOREIGN(op) ||
344 SN_HWPERF_IS_IONODE(op) ||
345 !SN_HWPERF_IS_NODE(op)) {
346 continue;
348 c = sn_hwperf_obj_to_cnode(op);
349 if (!found_cpu && sn_hwperf_has_cpus(c)) {
350 if (near_cpu_node)
351 *near_cpu_node = c;
352 found_cpu++;
354 if (!found_mem && sn_hwperf_has_mem(c)) {
355 if (near_mem_node)
356 *near_mem_node = c;
357 found_mem++;
359 if (found_cpu && found_mem)
360 break;
364 if (!found_cpu || !found_mem)
365 e = -ENODATA;
367 err:
368 return e;
372 static int sn_topology_show(struct seq_file *s, void *d)
374 int sz;
375 int pt;
376 int e = 0;
377 int i;
378 int j;
379 const char *slabname;
380 int ordinal;
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 */
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;
397 if (obj == objs) {
398 seq_printf(s, "# sn_topology version 2\n");
399 seq_printf(s, "# objtype ordinal location partition"
400 " [attribute value [, ...]]\n");
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;
410 seq_printf(s, "partition %u %s local "
411 "shubtype %s, "
412 "nasid_mask 0x%016llx, "
413 "nasid_bits %d:%d, "
414 "system_size %d, "
415 "sharing_size %d, "
416 "coherency_domain %d, "
417 "region_size %d\n",
419 partid, utsname()->nodename,
420 shubtype ? "shub2" : "shub1",
421 (u64)nasid_mask << nasid_shift, nasid_msb, nasid_shift,
422 system_size, sharing_size, coher, region_size);
424 print_pci_topology(s);
427 if (SN_HWPERF_FOREIGN(obj)) {
428 /* private in another partition: not interesting */
429 return 0;
432 for (i = 0; i < SN_HWPERF_MAXSTRING && obj->name[i]; i++) {
433 if (obj->name[i] == ' ')
434 obj->name[i] = '_';
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);
441 if (ordinal < 0 || (!SN_HWPERF_IS_NODE(obj) && !SN_HWPERF_IS_IONODE(obj)))
442 seq_putc(s, '\n');
443 else {
444 cnodeid_t near_mem = -1;
445 cnodeid_t near_cpu = -1;
447 seq_printf(s, ", nasid 0x%x", cnodeid_to_nasid(ordinal));
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);
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));
462 seq_putc(s, '\n');
465 * CPUs on this node, if any
467 if (!SN_HWPERF_IS_IONODE(obj)) {
468 for_each_cpu_and(i, cpu_online_mask,
469 cpumask_of_node(ordinal)) {
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(
479 cpu_to_node(i),
480 cpu_to_node(j)));
482 seq_putc(s, '\n');
487 if (obj->ports) {
489 * numalink ports
491 sz = obj->ports * sizeof(struct sn_hwperf_port_info);
492 if ((ptdata = kmalloc(sz, GFP_KERNEL)) == 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;
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;
509 seq_printf(s, "numalink %d %s-%d",
510 ordinal+pt, obj->location, ptdata[pt].port);
512 if (i >= sn_hwperf_obj_cnt) {
513 /* no connection */
514 seq_puts(s, " local endpoint disconnected"
515 ", protocol unknown\n");
516 continue;
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 (SN_HWPERF_FOREIGN(p))
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");
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.
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");
540 kfree(ptdata);
543 return 0;
546 static void *sn_topology_start(struct seq_file *s, loff_t * pos)
548 struct sn_hwperf_object_info *objs = s->private;
550 if (*pos < sn_hwperf_obj_cnt)
551 return (void *)(objs + *pos);
553 return NULL;
556 static void *sn_topology_next(struct seq_file *s, void *v, loff_t * pos)
558 ++*pos;
559 return sn_topology_start(s, pos);
562 static void sn_topology_stop(struct seq_file *m, void *v)
564 return;
568 * /proc/sgi_sn/sn_topology, read-only using seq_file
570 static const 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
577 struct sn_hwperf_op_info {
578 u64 op;
579 struct sn_hwperf_ioctl_args *a;
580 void *p;
581 int *v0;
582 int ret;
585 static void sn_hwperf_call_sal(void *info)
587 struct sn_hwperf_op_info *op_info = info;
588 int r;
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;
596 static int sn_hwperf_op_cpu(struct sn_hwperf_op_info *op_info)
598 u32 cpu;
599 u32 use_ipi;
600 int r = 0;
601 cpumask_t save_allowed;
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;
607 if (cpu != SN_HWPERF_ARG_ANY_CPU) {
608 if (cpu >= nr_cpu_ids || !cpu_online(cpu)) {
609 r = -EINVAL;
610 goto out;
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);
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);
624 else {
625 /* migrate the task before calling SAL */
626 save_allowed = current->cpus_allowed;
627 set_cpus_allowed_ptr(current, cpumask_of(cpu));
628 sn_hwperf_call_sal(op_info);
629 set_cpus_allowed_ptr(current, &save_allowed);
632 r = op_info->ret;
634 out:
635 return r;
638 /* map SAL hwperf error code to system error code */
639 static int sn_hwperf_map_err(int hwperf_err)
641 int e;
643 switch(hwperf_err) {
644 case SN_HWPERF_OP_OK:
645 e = 0;
646 break;
648 case SN_HWPERF_OP_NOMEM:
649 e = -ENOMEM;
650 break;
652 case SN_HWPERF_OP_NO_PERM:
653 e = -EPERM;
654 break;
656 case SN_HWPERF_OP_IO_ERROR:
657 e = -EIO;
658 break;
660 case SN_HWPERF_OP_BUSY:
661 e = -EBUSY;
662 break;
664 case SN_HWPERF_OP_RECONFIGURE:
665 e = -EAGAIN;
666 break;
668 case SN_HWPERF_OP_INVAL:
669 default:
670 e = -EINVAL;
671 break;
674 return e;
678 * ioctl for "sn_hwperf" misc device
680 static long sn_hwperf_ioctl(struct file *fp, u32 op, unsigned long arg)
682 struct sn_hwperf_ioctl_args a;
683 struct cpuinfo_ia64 *cdata;
684 struct sn_hwperf_object_info *objs;
685 struct sn_hwperf_object_info *cpuobj;
686 struct sn_hwperf_op_info op_info;
687 void *p = NULL;
688 int nobj;
689 char slice;
690 int node;
691 int r;
692 int v0;
693 int i;
694 int j;
696 /* only user requests are allowed here */
697 if ((op & SN_HWPERF_OP_MASK) < 10) {
698 r = -EINVAL;
699 goto error;
701 r = copy_from_user(&a, (const void __user *)arg,
702 sizeof(struct sn_hwperf_ioctl_args));
703 if (r != 0) {
704 r = -EFAULT;
705 goto error;
709 * Allocate memory to hold a kernel copy of the user buffer. The
710 * buffer contents are either copied in or out (or both) of user
711 * space depending on the flags encoded in the requested operation.
713 if (a.ptr) {
714 p = vmalloc(a.sz);
715 if (!p) {
716 r = -ENOMEM;
717 goto error;
721 if (op & SN_HWPERF_OP_MEM_COPYIN) {
722 r = copy_from_user(p, (const void __user *)a.ptr, a.sz);
723 if (r != 0) {
724 r = -EFAULT;
725 goto error;
729 switch (op) {
730 case SN_HWPERF_GET_CPU_INFO:
731 if (a.sz == sizeof(u64)) {
732 /* special case to get size needed */
733 *(u64 *) p = (u64) num_online_cpus() *
734 sizeof(struct sn_hwperf_object_info);
735 } else
736 if (a.sz < num_online_cpus() * sizeof(struct sn_hwperf_object_info)) {
737 r = -ENOMEM;
738 goto error;
739 } else
740 if ((r = sn_hwperf_enum_objects(&nobj, &objs)) == 0) {
741 int cpuobj_index = 0;
743 memset(p, 0, a.sz);
744 for (i = 0; i < nobj; i++) {
745 if (!SN_HWPERF_IS_NODE(objs + i))
746 continue;
747 node = sn_hwperf_obj_to_cnode(objs + i);
748 for_each_online_cpu(j) {
749 if (node != cpu_to_node(j))
750 continue;
751 cpuobj = (struct sn_hwperf_object_info *) p + cpuobj_index++;
752 slice = 'a' + cpuid_to_slice(j);
753 cdata = cpu_data(j);
754 cpuobj->id = j;
755 snprintf(cpuobj->name,
756 sizeof(cpuobj->name),
757 "CPU %luMHz %s",
758 cdata->proc_freq / 1000000,
759 cdata->vendor);
760 snprintf(cpuobj->location,
761 sizeof(cpuobj->location),
762 "%s%c", objs[i].location,
763 slice);
767 vfree(objs);
769 break;
771 case SN_HWPERF_GET_NODE_NASID:
772 if (a.sz != sizeof(u64) ||
773 (node = a.arg) < 0 || !cnode_possible(node)) {
774 r = -EINVAL;
775 goto error;
777 *(u64 *)p = (u64)cnodeid_to_nasid(node);
778 break;
780 case SN_HWPERF_GET_OBJ_NODE:
781 i = a.arg;
782 if (a.sz != sizeof(u64) || i < 0) {
783 r = -EINVAL;
784 goto error;
786 if ((r = sn_hwperf_enum_objects(&nobj, &objs)) == 0) {
787 if (i >= nobj) {
788 r = -EINVAL;
789 vfree(objs);
790 goto error;
792 if (objs[i].id != a.arg) {
793 for (i = 0; i < nobj; i++) {
794 if (objs[i].id == a.arg)
795 break;
798 if (i == nobj) {
799 r = -EINVAL;
800 vfree(objs);
801 goto error;
804 if (!SN_HWPERF_IS_NODE(objs + i) &&
805 !SN_HWPERF_IS_IONODE(objs + i)) {
806 r = -ENOENT;
807 vfree(objs);
808 goto error;
811 *(u64 *)p = (u64)sn_hwperf_obj_to_cnode(objs + i);
812 vfree(objs);
814 break;
816 case SN_HWPERF_GET_MMRS:
817 case SN_HWPERF_SET_MMRS:
818 case SN_HWPERF_OBJECT_DISTANCE:
819 op_info.p = p;
820 op_info.a = &a;
821 op_info.v0 = &v0;
822 op_info.op = op;
823 r = sn_hwperf_op_cpu(&op_info);
824 if (r) {
825 r = sn_hwperf_map_err(r);
826 a.v0 = v0;
827 goto error;
829 break;
831 default:
832 /* all other ops are a direct SAL call */
833 r = ia64_sn_hwperf_op(sn_hwperf_master_nasid, op,
834 a.arg, a.sz, (u64) p, 0, 0, &v0);
835 if (r) {
836 r = sn_hwperf_map_err(r);
837 goto error;
839 a.v0 = v0;
840 break;
843 if (op & SN_HWPERF_OP_MEM_COPYOUT) {
844 r = copy_to_user((void __user *)a.ptr, p, a.sz);
845 if (r != 0) {
846 r = -EFAULT;
847 goto error;
851 error:
852 vfree(p);
854 return r;
857 static const struct file_operations sn_hwperf_fops = {
858 .unlocked_ioctl = sn_hwperf_ioctl,
861 static struct miscdevice sn_hwperf_dev = {
862 MISC_DYNAMIC_MINOR,
863 "sn_hwperf",
864 &sn_hwperf_fops
867 static int sn_hwperf_init(void)
869 u64 v;
870 int salr;
871 int e = 0;
873 /* single threaded, once-only initialization */
874 mutex_lock(&sn_hwperf_init_mutex);
876 if (sn_hwperf_salheap) {
877 mutex_unlock(&sn_hwperf_init_mutex);
878 return e;
882 * The PROM code needs a fixed reference node. For convenience the
883 * same node as the console I/O is used.
885 sn_hwperf_master_nasid = (nasid_t) ia64_sn_get_console_nasid();
888 * Request the needed size and install the PROM scratch area.
889 * The PROM keeps various tracking bits in this memory area.
891 salr = ia64_sn_hwperf_op(sn_hwperf_master_nasid,
892 (u64) SN_HWPERF_GET_HEAPSIZE, 0,
893 (u64) sizeof(u64), (u64) &v, 0, 0, NULL);
894 if (salr != SN_HWPERF_OP_OK) {
895 e = -EINVAL;
896 goto out;
899 if ((sn_hwperf_salheap = vmalloc(v)) == NULL) {
900 e = -ENOMEM;
901 goto out;
903 salr = ia64_sn_hwperf_op(sn_hwperf_master_nasid,
904 SN_HWPERF_INSTALL_HEAP, 0, v,
905 (u64) sn_hwperf_salheap, 0, 0, NULL);
906 if (salr != SN_HWPERF_OP_OK) {
907 e = -EINVAL;
908 goto out;
911 salr = ia64_sn_hwperf_op(sn_hwperf_master_nasid,
912 SN_HWPERF_OBJECT_COUNT, 0,
913 sizeof(u64), (u64) &v, 0, 0, NULL);
914 if (salr != SN_HWPERF_OP_OK) {
915 e = -EINVAL;
916 goto out;
918 sn_hwperf_obj_cnt = (int)v;
920 out:
921 if (e < 0 && sn_hwperf_salheap) {
922 vfree(sn_hwperf_salheap);
923 sn_hwperf_salheap = NULL;
924 sn_hwperf_obj_cnt = 0;
926 mutex_unlock(&sn_hwperf_init_mutex);
927 return e;
930 int sn_topology_open(struct inode *inode, struct file *file)
932 int e;
933 struct seq_file *seq;
934 struct sn_hwperf_object_info *objbuf;
935 int nobj;
937 if ((e = sn_hwperf_enum_objects(&nobj, &objbuf)) == 0) {
938 e = seq_open(file, &sn_topology_seq_ops);
939 seq = file->private_data;
940 seq->private = objbuf;
943 return e;
946 int sn_topology_release(struct inode *inode, struct file *file)
948 struct seq_file *seq = file->private_data;
950 vfree(seq->private);
951 return seq_release(inode, file);
954 int sn_hwperf_get_nearest_node(cnodeid_t node,
955 cnodeid_t *near_mem_node, cnodeid_t *near_cpu_node)
957 int e;
958 int nobj;
959 struct sn_hwperf_object_info *objbuf;
961 if ((e = sn_hwperf_enum_objects(&nobj, &objbuf)) == 0) {
962 e = sn_hwperf_get_nearest_node_objdata(objbuf, nobj,
963 node, near_mem_node, near_cpu_node);
964 vfree(objbuf);
967 return e;
970 static int __devinit sn_hwperf_misc_register_init(void)
972 int e;
974 if (!ia64_platform_is("sn2"))
975 return 0;
977 sn_hwperf_init();
980 * Register a dynamic misc device for hwperf ioctls. Platforms
981 * supporting hotplug will create /dev/sn_hwperf, else user
982 * can to look up the minor number in /proc/misc.
984 if ((e = misc_register(&sn_hwperf_dev)) != 0) {
985 printk(KERN_ERR "sn_hwperf_misc_register_init: failed to "
986 "register misc device for \"%s\"\n", sn_hwperf_dev.name);
989 return e;
992 device_initcall(sn_hwperf_misc_register_init); /* after misc_init() */
993 EXPORT_SYMBOL(sn_hwperf_get_nearest_node);