Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / arch / ia64 / sn / kernel / sn2 / sn_hwperf.c
blob4b0d1538e7e5586450fd2fe39c363af2768bdc35
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/smp_lock.h>
34 #include <linux/nodemask.h>
35 #include <linux/smp.h>
36 #include <linux/mutex.h>
38 #include <asm/processor.h>
39 #include <asm/topology.h>
40 #include <asm/semaphore.h>
41 #include <asm/uaccess.h>
42 #include <asm/sal.h>
43 #include <asm/sn/io.h>
44 #include <asm/sn/sn_sal.h>
45 #include <asm/sn/module.h>
46 #include <asm/sn/geo.h>
47 #include <asm/sn/sn2/sn_hwperf.h>
48 #include <asm/sn/addrs.h>
50 static void *sn_hwperf_salheap = NULL;
51 static int sn_hwperf_obj_cnt = 0;
52 static nasid_t sn_hwperf_master_nasid = INVALID_NASID;
53 static int sn_hwperf_init(void);
54 static DEFINE_MUTEX(sn_hwperf_init_mutex);
56 #define cnode_possible(n) ((n) < num_cnodes)
58 static int sn_hwperf_enum_objects(int *nobj, struct sn_hwperf_object_info **ret)
60 int e;
61 u64 sz;
62 struct sn_hwperf_object_info *objbuf = NULL;
64 if ((e = sn_hwperf_init()) < 0) {
65 printk(KERN_ERR "sn_hwperf_init failed: err %d\n", e);
66 goto out;
69 sz = sn_hwperf_obj_cnt * sizeof(struct sn_hwperf_object_info);
70 objbuf = vmalloc(sz);
71 if (objbuf == NULL) {
72 printk("sn_hwperf_enum_objects: vmalloc(%d) failed\n", (int)sz);
73 e = -ENOMEM;
74 goto out;
77 e = ia64_sn_hwperf_op(sn_hwperf_master_nasid, SN_HWPERF_ENUM_OBJECTS,
78 0, sz, (u64) objbuf, 0, 0, NULL);
79 if (e != SN_HWPERF_OP_OK) {
80 e = -EINVAL;
81 vfree(objbuf);
84 out:
85 *nobj = sn_hwperf_obj_cnt;
86 *ret = objbuf;
87 return e;
90 static int sn_hwperf_location_to_bpos(char *location,
91 int *rack, int *bay, int *slot, int *slab)
93 char type;
95 /* first scan for an old style geoid string */
96 if (sscanf(location, "%03d%c%02d#%d",
97 rack, &type, bay, slab) == 4)
98 *slot = 0;
99 else /* scan for a new bladed geoid string */
100 if (sscanf(location, "%03d%c%02d^%02d#%d",
101 rack, &type, bay, slot, slab) != 5)
102 return -1;
103 /* success */
104 return 0;
107 static int sn_hwperf_geoid_to_cnode(char *location)
109 int cnode;
110 geoid_t geoid;
111 moduleid_t module_id;
112 int rack, bay, slot, slab;
113 int this_rack, this_bay, this_slot, this_slab;
115 if (sn_hwperf_location_to_bpos(location, &rack, &bay, &slot, &slab))
116 return -1;
119 * FIXME: replace with cleaner for_each_XXX macro which addresses
120 * both compute and IO nodes once ACPI3.0 is available.
122 for (cnode = 0; cnode < num_cnodes; cnode++) {
123 geoid = cnodeid_get_geoid(cnode);
124 module_id = geo_module(geoid);
125 this_rack = MODULE_GET_RACK(module_id);
126 this_bay = MODULE_GET_BPOS(module_id);
127 this_slot = geo_slot(geoid);
128 this_slab = geo_slab(geoid);
129 if (rack == this_rack && bay == this_bay &&
130 slot == this_slot && slab == this_slab) {
131 break;
135 return cnode_possible(cnode) ? cnode : -1;
138 static int sn_hwperf_obj_to_cnode(struct sn_hwperf_object_info * obj)
140 if (!SN_HWPERF_IS_NODE(obj) && !SN_HWPERF_IS_IONODE(obj))
141 BUG();
142 if (SN_HWPERF_FOREIGN(obj))
143 return -1;
144 return sn_hwperf_geoid_to_cnode(obj->location);
147 static int sn_hwperf_generic_ordinal(struct sn_hwperf_object_info *obj,
148 struct sn_hwperf_object_info *objs)
150 int ordinal;
151 struct sn_hwperf_object_info *p;
153 for (ordinal=0, p=objs; p != obj; p++) {
154 if (SN_HWPERF_FOREIGN(p))
155 continue;
156 if (SN_HWPERF_SAME_OBJTYPE(p, obj))
157 ordinal++;
160 return ordinal;
163 static const char *slabname_node = "node"; /* SHub asic */
164 static const char *slabname_ionode = "ionode"; /* TIO asic */
165 static const char *slabname_router = "router"; /* NL3R or NL4R */
166 static const char *slabname_other = "other"; /* unknown asic */
168 static const char *sn_hwperf_get_slabname(struct sn_hwperf_object_info *obj,
169 struct sn_hwperf_object_info *objs, int *ordinal)
171 int isnode;
172 const char *slabname = slabname_other;
174 if ((isnode = SN_HWPERF_IS_NODE(obj)) || SN_HWPERF_IS_IONODE(obj)) {
175 slabname = isnode ? slabname_node : slabname_ionode;
176 *ordinal = sn_hwperf_obj_to_cnode(obj);
178 else {
179 *ordinal = sn_hwperf_generic_ordinal(obj, objs);
180 if (SN_HWPERF_IS_ROUTER(obj))
181 slabname = slabname_router;
184 return slabname;
187 static void print_pci_topology(struct seq_file *s)
189 char *p;
190 size_t sz;
191 int e;
193 for (sz = PAGE_SIZE; sz < 16 * PAGE_SIZE; sz += PAGE_SIZE) {
194 if (!(p = kmalloc(sz, GFP_KERNEL)))
195 break;
196 e = ia64_sn_ioif_get_pci_topology(__pa(p), sz);
197 if (e == SALRET_OK)
198 seq_puts(s, p);
199 kfree(p);
200 if (e == SALRET_OK || e == SALRET_NOT_IMPLEMENTED)
201 break;
205 static inline int sn_hwperf_has_cpus(cnodeid_t node)
207 return node < MAX_NUMNODES && node_online(node) && nr_cpus_node(node);
210 static inline int sn_hwperf_has_mem(cnodeid_t node)
212 return node < MAX_NUMNODES && node_online(node) && NODE_DATA(node)->node_present_pages;
215 static struct sn_hwperf_object_info *
216 sn_hwperf_findobj_id(struct sn_hwperf_object_info *objbuf,
217 int nobj, int id)
219 int i;
220 struct sn_hwperf_object_info *p = objbuf;
222 for (i=0; i < nobj; i++, p++) {
223 if (p->id == id)
224 return p;
227 return NULL;
231 static int sn_hwperf_get_nearest_node_objdata(struct sn_hwperf_object_info *objbuf,
232 int nobj, cnodeid_t node, cnodeid_t *near_mem_node, cnodeid_t *near_cpu_node)
234 int e;
235 struct sn_hwperf_object_info *nodeobj = NULL;
236 struct sn_hwperf_object_info *op;
237 struct sn_hwperf_object_info *dest;
238 struct sn_hwperf_object_info *router;
239 struct sn_hwperf_port_info ptdata[16];
240 int sz, i, j;
241 cnodeid_t c;
242 int found_mem = 0;
243 int found_cpu = 0;
245 if (!cnode_possible(node))
246 return -EINVAL;
248 if (sn_hwperf_has_cpus(node)) {
249 if (near_cpu_node)
250 *near_cpu_node = node;
251 found_cpu++;
254 if (sn_hwperf_has_mem(node)) {
255 if (near_mem_node)
256 *near_mem_node = node;
257 found_mem++;
260 if (found_cpu && found_mem)
261 return 0; /* trivially successful */
263 /* find the argument node object */
264 for (i=0, op=objbuf; i < nobj; i++, op++) {
265 if (!SN_HWPERF_IS_NODE(op) && !SN_HWPERF_IS_IONODE(op))
266 continue;
267 if (node == sn_hwperf_obj_to_cnode(op)) {
268 nodeobj = op;
269 break;
272 if (!nodeobj) {
273 e = -ENOENT;
274 goto err;
277 /* get it's interconnect topology */
278 sz = op->ports * sizeof(struct sn_hwperf_port_info);
279 if (sz > sizeof(ptdata))
280 BUG();
281 e = ia64_sn_hwperf_op(sn_hwperf_master_nasid,
282 SN_HWPERF_ENUM_PORTS, nodeobj->id, sz,
283 (u64)&ptdata, 0, 0, NULL);
284 if (e != SN_HWPERF_OP_OK) {
285 e = -EINVAL;
286 goto err;
289 /* find nearest node with cpus and nearest memory */
290 for (router=NULL, j=0; j < op->ports; j++) {
291 dest = sn_hwperf_findobj_id(objbuf, nobj, ptdata[j].conn_id);
292 if (dest && SN_HWPERF_IS_ROUTER(dest))
293 router = dest;
294 if (!dest || SN_HWPERF_FOREIGN(dest) ||
295 !SN_HWPERF_IS_NODE(dest) || SN_HWPERF_IS_IONODE(dest)) {
296 continue;
298 c = sn_hwperf_obj_to_cnode(dest);
299 if (!found_cpu && sn_hwperf_has_cpus(c)) {
300 if (near_cpu_node)
301 *near_cpu_node = c;
302 found_cpu++;
304 if (!found_mem && sn_hwperf_has_mem(c)) {
305 if (near_mem_node)
306 *near_mem_node = c;
307 found_mem++;
311 if (router && (!found_cpu || !found_mem)) {
312 /* search for a node connected to the same router */
313 sz = router->ports * sizeof(struct sn_hwperf_port_info);
314 if (sz > sizeof(ptdata))
315 BUG();
316 e = ia64_sn_hwperf_op(sn_hwperf_master_nasid,
317 SN_HWPERF_ENUM_PORTS, router->id, sz,
318 (u64)&ptdata, 0, 0, NULL);
319 if (e != SN_HWPERF_OP_OK) {
320 e = -EINVAL;
321 goto err;
323 for (j=0; j < router->ports; j++) {
324 dest = sn_hwperf_findobj_id(objbuf, nobj,
325 ptdata[j].conn_id);
326 if (!dest || dest->id == node ||
327 SN_HWPERF_FOREIGN(dest) ||
328 !SN_HWPERF_IS_NODE(dest) ||
329 SN_HWPERF_IS_IONODE(dest)) {
330 continue;
332 c = sn_hwperf_obj_to_cnode(dest);
333 if (!found_cpu && sn_hwperf_has_cpus(c)) {
334 if (near_cpu_node)
335 *near_cpu_node = c;
336 found_cpu++;
338 if (!found_mem && sn_hwperf_has_mem(c)) {
339 if (near_mem_node)
340 *near_mem_node = c;
341 found_mem++;
343 if (found_cpu && found_mem)
344 break;
348 if (!found_cpu || !found_mem) {
349 /* resort to _any_ node with CPUs and memory */
350 for (i=0, op=objbuf; i < nobj; i++, op++) {
351 if (SN_HWPERF_FOREIGN(op) ||
352 SN_HWPERF_IS_IONODE(op) ||
353 !SN_HWPERF_IS_NODE(op)) {
354 continue;
356 c = sn_hwperf_obj_to_cnode(op);
357 if (!found_cpu && sn_hwperf_has_cpus(c)) {
358 if (near_cpu_node)
359 *near_cpu_node = c;
360 found_cpu++;
362 if (!found_mem && sn_hwperf_has_mem(c)) {
363 if (near_mem_node)
364 *near_mem_node = c;
365 found_mem++;
367 if (found_cpu && found_mem)
368 break;
372 if (!found_cpu || !found_mem)
373 e = -ENODATA;
375 err:
376 return e;
380 static int sn_topology_show(struct seq_file *s, void *d)
382 int sz;
383 int pt;
384 int e = 0;
385 int i;
386 int j;
387 const char *slabname;
388 int ordinal;
389 cpumask_t cpumask;
390 char slice;
391 struct cpuinfo_ia64 *c;
392 struct sn_hwperf_port_info *ptdata;
393 struct sn_hwperf_object_info *p;
394 struct sn_hwperf_object_info *obj = d; /* this object */
395 struct sn_hwperf_object_info *objs = s->private; /* all objects */
396 u8 shubtype;
397 u8 system_size;
398 u8 sharing_size;
399 u8 partid;
400 u8 coher;
401 u8 nasid_shift;
402 u8 region_size;
403 u16 nasid_mask;
404 int nasid_msb;
406 if (obj == objs) {
407 seq_printf(s, "# sn_topology version 2\n");
408 seq_printf(s, "# objtype ordinal location partition"
409 " [attribute value [, ...]]\n");
411 if (ia64_sn_get_sn_info(0,
412 &shubtype, &nasid_mask, &nasid_shift, &system_size,
413 &sharing_size, &partid, &coher, &region_size))
414 BUG();
415 for (nasid_msb=63; nasid_msb > 0; nasid_msb--) {
416 if (((u64)nasid_mask << nasid_shift) & (1ULL << nasid_msb))
417 break;
419 seq_printf(s, "partition %u %s local "
420 "shubtype %s, "
421 "nasid_mask 0x%016lx, "
422 "nasid_bits %d:%d, "
423 "system_size %d, "
424 "sharing_size %d, "
425 "coherency_domain %d, "
426 "region_size %d\n",
428 partid, utsname()->nodename,
429 shubtype ? "shub2" : "shub1",
430 (u64)nasid_mask << nasid_shift, nasid_msb, nasid_shift,
431 system_size, sharing_size, coher, region_size);
433 print_pci_topology(s);
436 if (SN_HWPERF_FOREIGN(obj)) {
437 /* private in another partition: not interesting */
438 return 0;
441 for (i = 0; i < SN_HWPERF_MAXSTRING && obj->name[i]; i++) {
442 if (obj->name[i] == ' ')
443 obj->name[i] = '_';
446 slabname = sn_hwperf_get_slabname(obj, objs, &ordinal);
447 seq_printf(s, "%s %d %s %s asic %s", slabname, ordinal, obj->location,
448 obj->sn_hwp_this_part ? "local" : "shared", obj->name);
450 if (ordinal < 0 || (!SN_HWPERF_IS_NODE(obj) && !SN_HWPERF_IS_IONODE(obj)))
451 seq_putc(s, '\n');
452 else {
453 cnodeid_t near_mem = -1;
454 cnodeid_t near_cpu = -1;
456 seq_printf(s, ", nasid 0x%x", cnodeid_to_nasid(ordinal));
458 if (sn_hwperf_get_nearest_node_objdata(objs, sn_hwperf_obj_cnt,
459 ordinal, &near_mem, &near_cpu) == 0) {
460 seq_printf(s, ", near_mem_nodeid %d, near_cpu_nodeid %d",
461 near_mem, near_cpu);
464 if (!SN_HWPERF_IS_IONODE(obj)) {
465 for_each_online_node(i) {
466 seq_printf(s, i ? ":%d" : ", dist %d",
467 node_distance(ordinal, i));
471 seq_putc(s, '\n');
474 * CPUs on this node, if any
476 if (!SN_HWPERF_IS_IONODE(obj)) {
477 cpumask = node_to_cpumask(ordinal);
478 for_each_online_cpu(i) {
479 if (cpu_isset(i, cpumask)) {
480 slice = 'a' + cpuid_to_slice(i);
481 c = cpu_data(i);
482 seq_printf(s, "cpu %d %s%c local"
483 " freq %luMHz, arch ia64",
484 i, obj->location, slice,
485 c->proc_freq / 1000000);
486 for_each_online_cpu(j) {
487 seq_printf(s, j ? ":%d" : ", dist %d",
488 node_distance(
489 cpu_to_node(i),
490 cpu_to_node(j)));
492 seq_putc(s, '\n');
498 if (obj->ports) {
500 * numalink ports
502 sz = obj->ports * sizeof(struct sn_hwperf_port_info);
503 if ((ptdata = kmalloc(sz, GFP_KERNEL)) == NULL)
504 return -ENOMEM;
505 e = ia64_sn_hwperf_op(sn_hwperf_master_nasid,
506 SN_HWPERF_ENUM_PORTS, obj->id, sz,
507 (u64) ptdata, 0, 0, NULL);
508 if (e != SN_HWPERF_OP_OK)
509 return -EINVAL;
510 for (ordinal=0, p=objs; p != obj; p++) {
511 if (!SN_HWPERF_FOREIGN(p))
512 ordinal += p->ports;
514 for (pt = 0; pt < obj->ports; pt++) {
515 for (p = objs, i = 0; i < sn_hwperf_obj_cnt; i++, p++) {
516 if (ptdata[pt].conn_id == p->id) {
517 break;
520 seq_printf(s, "numalink %d %s-%d",
521 ordinal+pt, obj->location, ptdata[pt].port);
523 if (i >= sn_hwperf_obj_cnt) {
524 /* no connection */
525 seq_puts(s, " local endpoint disconnected"
526 ", protocol unknown\n");
527 continue;
530 if (obj->sn_hwp_this_part && p->sn_hwp_this_part)
531 /* both ends local to this partition */
532 seq_puts(s, " local");
533 else if (SN_HWPERF_FOREIGN(p))
534 /* both ends of the link in foreign partiton */
535 seq_puts(s, " foreign");
536 else
537 /* link straddles a partition */
538 seq_puts(s, " shared");
541 * Unlikely, but strictly should query the LLP config
542 * registers because an NL4R can be configured to run
543 * NL3 protocol, even when not talking to an NL3 router.
544 * Ditto for node-node.
546 seq_printf(s, " endpoint %s-%d, protocol %s\n",
547 p->location, ptdata[pt].conn_port,
548 (SN_HWPERF_IS_NL3ROUTER(obj) ||
549 SN_HWPERF_IS_NL3ROUTER(p)) ? "LLP3" : "LLP4");
551 kfree(ptdata);
554 return 0;
557 static void *sn_topology_start(struct seq_file *s, loff_t * pos)
559 struct sn_hwperf_object_info *objs = s->private;
561 if (*pos < sn_hwperf_obj_cnt)
562 return (void *)(objs + *pos);
564 return NULL;
567 static void *sn_topology_next(struct seq_file *s, void *v, loff_t * pos)
569 ++*pos;
570 return sn_topology_start(s, pos);
573 static void sn_topology_stop(struct seq_file *m, void *v)
575 return;
579 * /proc/sgi_sn/sn_topology, read-only using seq_file
581 static const struct seq_operations sn_topology_seq_ops = {
582 .start = sn_topology_start,
583 .next = sn_topology_next,
584 .stop = sn_topology_stop,
585 .show = sn_topology_show
588 struct sn_hwperf_op_info {
589 u64 op;
590 struct sn_hwperf_ioctl_args *a;
591 void *p;
592 int *v0;
593 int ret;
596 static void sn_hwperf_call_sal(void *info)
598 struct sn_hwperf_op_info *op_info = info;
599 int r;
601 r = ia64_sn_hwperf_op(sn_hwperf_master_nasid, op_info->op,
602 op_info->a->arg, op_info->a->sz,
603 (u64) op_info->p, 0, 0, op_info->v0);
604 op_info->ret = r;
607 static int sn_hwperf_op_cpu(struct sn_hwperf_op_info *op_info)
609 u32 cpu;
610 u32 use_ipi;
611 int r = 0;
612 cpumask_t save_allowed;
614 cpu = (op_info->a->arg & SN_HWPERF_ARG_CPU_MASK) >> 32;
615 use_ipi = op_info->a->arg & SN_HWPERF_ARG_USE_IPI_MASK;
616 op_info->a->arg &= SN_HWPERF_ARG_OBJID_MASK;
618 if (cpu != SN_HWPERF_ARG_ANY_CPU) {
619 if (cpu >= NR_CPUS || !cpu_online(cpu)) {
620 r = -EINVAL;
621 goto out;
625 if (cpu == SN_HWPERF_ARG_ANY_CPU || cpu == get_cpu()) {
626 /* don't care, or already on correct cpu */
627 sn_hwperf_call_sal(op_info);
629 else {
630 if (use_ipi) {
631 /* use an interprocessor interrupt to call SAL */
632 smp_call_function_single(cpu, sn_hwperf_call_sal,
633 op_info, 1, 1);
635 else {
636 /* migrate the task before calling SAL */
637 save_allowed = current->cpus_allowed;
638 set_cpus_allowed(current, cpumask_of_cpu(cpu));
639 sn_hwperf_call_sal(op_info);
640 set_cpus_allowed(current, save_allowed);
643 r = op_info->ret;
645 out:
646 return r;
649 /* map SAL hwperf error code to system error code */
650 static int sn_hwperf_map_err(int hwperf_err)
652 int e;
654 switch(hwperf_err) {
655 case SN_HWPERF_OP_OK:
656 e = 0;
657 break;
659 case SN_HWPERF_OP_NOMEM:
660 e = -ENOMEM;
661 break;
663 case SN_HWPERF_OP_NO_PERM:
664 e = -EPERM;
665 break;
667 case SN_HWPERF_OP_IO_ERROR:
668 e = -EIO;
669 break;
671 case SN_HWPERF_OP_BUSY:
672 e = -EBUSY;
673 break;
675 case SN_HWPERF_OP_RECONFIGURE:
676 e = -EAGAIN;
677 break;
679 case SN_HWPERF_OP_INVAL:
680 default:
681 e = -EINVAL;
682 break;
685 return e;
689 * ioctl for "sn_hwperf" misc device
691 static int
692 sn_hwperf_ioctl(struct inode *in, struct file *fp, u32 op, u64 arg)
694 struct sn_hwperf_ioctl_args a;
695 struct cpuinfo_ia64 *cdata;
696 struct sn_hwperf_object_info *objs;
697 struct sn_hwperf_object_info *cpuobj;
698 struct sn_hwperf_op_info op_info;
699 void *p = NULL;
700 int nobj;
701 char slice;
702 int node;
703 int r;
704 int v0;
705 int i;
706 int j;
708 unlock_kernel();
710 /* only user requests are allowed here */
711 if ((op & SN_HWPERF_OP_MASK) < 10) {
712 r = -EINVAL;
713 goto error;
715 r = copy_from_user(&a, (const void __user *)arg,
716 sizeof(struct sn_hwperf_ioctl_args));
717 if (r != 0) {
718 r = -EFAULT;
719 goto error;
723 * Allocate memory to hold a kernel copy of the user buffer. The
724 * buffer contents are either copied in or out (or both) of user
725 * space depending on the flags encoded in the requested operation.
727 if (a.ptr) {
728 p = vmalloc(a.sz);
729 if (!p) {
730 r = -ENOMEM;
731 goto error;
735 if (op & SN_HWPERF_OP_MEM_COPYIN) {
736 r = copy_from_user(p, (const void __user *)a.ptr, a.sz);
737 if (r != 0) {
738 r = -EFAULT;
739 goto error;
743 switch (op) {
744 case SN_HWPERF_GET_CPU_INFO:
745 if (a.sz == sizeof(u64)) {
746 /* special case to get size needed */
747 *(u64 *) p = (u64) num_online_cpus() *
748 sizeof(struct sn_hwperf_object_info);
749 } else
750 if (a.sz < num_online_cpus() * sizeof(struct sn_hwperf_object_info)) {
751 r = -ENOMEM;
752 goto error;
753 } else
754 if ((r = sn_hwperf_enum_objects(&nobj, &objs)) == 0) {
755 int cpuobj_index = 0;
757 memset(p, 0, a.sz);
758 for (i = 0; i < nobj; i++) {
759 if (!SN_HWPERF_IS_NODE(objs + i))
760 continue;
761 node = sn_hwperf_obj_to_cnode(objs + i);
762 for_each_online_cpu(j) {
763 if (node != cpu_to_node(j))
764 continue;
765 cpuobj = (struct sn_hwperf_object_info *) p + cpuobj_index++;
766 slice = 'a' + cpuid_to_slice(j);
767 cdata = cpu_data(j);
768 cpuobj->id = j;
769 snprintf(cpuobj->name,
770 sizeof(cpuobj->name),
771 "CPU %luMHz %s",
772 cdata->proc_freq / 1000000,
773 cdata->vendor);
774 snprintf(cpuobj->location,
775 sizeof(cpuobj->location),
776 "%s%c", objs[i].location,
777 slice);
781 vfree(objs);
783 break;
785 case SN_HWPERF_GET_NODE_NASID:
786 if (a.sz != sizeof(u64) ||
787 (node = a.arg) < 0 || !cnode_possible(node)) {
788 r = -EINVAL;
789 goto error;
791 *(u64 *)p = (u64)cnodeid_to_nasid(node);
792 break;
794 case SN_HWPERF_GET_OBJ_NODE:
795 if (a.sz != sizeof(u64) || a.arg < 0) {
796 r = -EINVAL;
797 goto error;
799 if ((r = sn_hwperf_enum_objects(&nobj, &objs)) == 0) {
800 if (a.arg >= nobj) {
801 r = -EINVAL;
802 vfree(objs);
803 goto error;
805 if (objs[(i = a.arg)].id != a.arg) {
806 for (i = 0; i < nobj; i++) {
807 if (objs[i].id == a.arg)
808 break;
811 if (i == nobj) {
812 r = -EINVAL;
813 vfree(objs);
814 goto error;
817 if (!SN_HWPERF_IS_NODE(objs + i) &&
818 !SN_HWPERF_IS_IONODE(objs + i)) {
819 r = -ENOENT;
820 vfree(objs);
821 goto error;
824 *(u64 *)p = (u64)sn_hwperf_obj_to_cnode(objs + i);
825 vfree(objs);
827 break;
829 case SN_HWPERF_GET_MMRS:
830 case SN_HWPERF_SET_MMRS:
831 case SN_HWPERF_OBJECT_DISTANCE:
832 op_info.p = p;
833 op_info.a = &a;
834 op_info.v0 = &v0;
835 op_info.op = op;
836 r = sn_hwperf_op_cpu(&op_info);
837 if (r) {
838 r = sn_hwperf_map_err(r);
839 a.v0 = v0;
840 goto error;
842 break;
844 default:
845 /* all other ops are a direct SAL call */
846 r = ia64_sn_hwperf_op(sn_hwperf_master_nasid, op,
847 a.arg, a.sz, (u64) p, 0, 0, &v0);
848 if (r) {
849 r = sn_hwperf_map_err(r);
850 goto error;
852 a.v0 = v0;
853 break;
856 if (op & SN_HWPERF_OP_MEM_COPYOUT) {
857 r = copy_to_user((void __user *)a.ptr, p, a.sz);
858 if (r != 0) {
859 r = -EFAULT;
860 goto error;
864 error:
865 vfree(p);
867 lock_kernel();
868 return r;
871 static const struct file_operations sn_hwperf_fops = {
872 .ioctl = sn_hwperf_ioctl,
875 static struct miscdevice sn_hwperf_dev = {
876 MISC_DYNAMIC_MINOR,
877 "sn_hwperf",
878 &sn_hwperf_fops
881 static int sn_hwperf_init(void)
883 u64 v;
884 int salr;
885 int e = 0;
887 /* single threaded, once-only initialization */
888 mutex_lock(&sn_hwperf_init_mutex);
890 if (sn_hwperf_salheap) {
891 mutex_unlock(&sn_hwperf_init_mutex);
892 return e;
896 * The PROM code needs a fixed reference node. For convenience the
897 * same node as the console I/O is used.
899 sn_hwperf_master_nasid = (nasid_t) ia64_sn_get_console_nasid();
902 * Request the needed size and install the PROM scratch area.
903 * The PROM keeps various tracking bits in this memory area.
905 salr = ia64_sn_hwperf_op(sn_hwperf_master_nasid,
906 (u64) SN_HWPERF_GET_HEAPSIZE, 0,
907 (u64) sizeof(u64), (u64) &v, 0, 0, NULL);
908 if (salr != SN_HWPERF_OP_OK) {
909 e = -EINVAL;
910 goto out;
913 if ((sn_hwperf_salheap = vmalloc(v)) == NULL) {
914 e = -ENOMEM;
915 goto out;
917 salr = ia64_sn_hwperf_op(sn_hwperf_master_nasid,
918 SN_HWPERF_INSTALL_HEAP, 0, v,
919 (u64) sn_hwperf_salheap, 0, 0, NULL);
920 if (salr != SN_HWPERF_OP_OK) {
921 e = -EINVAL;
922 goto out;
925 salr = ia64_sn_hwperf_op(sn_hwperf_master_nasid,
926 SN_HWPERF_OBJECT_COUNT, 0,
927 sizeof(u64), (u64) &v, 0, 0, NULL);
928 if (salr != SN_HWPERF_OP_OK) {
929 e = -EINVAL;
930 goto out;
932 sn_hwperf_obj_cnt = (int)v;
934 out:
935 if (e < 0 && sn_hwperf_salheap) {
936 vfree(sn_hwperf_salheap);
937 sn_hwperf_salheap = NULL;
938 sn_hwperf_obj_cnt = 0;
940 mutex_unlock(&sn_hwperf_init_mutex);
941 return e;
944 int sn_topology_open(struct inode *inode, struct file *file)
946 int e;
947 struct seq_file *seq;
948 struct sn_hwperf_object_info *objbuf;
949 int nobj;
951 if ((e = sn_hwperf_enum_objects(&nobj, &objbuf)) == 0) {
952 e = seq_open(file, &sn_topology_seq_ops);
953 seq = file->private_data;
954 seq->private = objbuf;
957 return e;
960 int sn_topology_release(struct inode *inode, struct file *file)
962 struct seq_file *seq = file->private_data;
964 vfree(seq->private);
965 return seq_release(inode, file);
968 int sn_hwperf_get_nearest_node(cnodeid_t node,
969 cnodeid_t *near_mem_node, cnodeid_t *near_cpu_node)
971 int e;
972 int nobj;
973 struct sn_hwperf_object_info *objbuf;
975 if ((e = sn_hwperf_enum_objects(&nobj, &objbuf)) == 0) {
976 e = sn_hwperf_get_nearest_node_objdata(objbuf, nobj,
977 node, near_mem_node, near_cpu_node);
978 vfree(objbuf);
981 return e;
984 static int __devinit sn_hwperf_misc_register_init(void)
986 int e;
988 if (!ia64_platform_is("sn2"))
989 return 0;
991 sn_hwperf_init();
994 * Register a dynamic misc device for hwperf ioctls. Platforms
995 * supporting hotplug will create /dev/sn_hwperf, else user
996 * can to look up the minor number in /proc/misc.
998 if ((e = misc_register(&sn_hwperf_dev)) != 0) {
999 printk(KERN_ERR "sn_hwperf_misc_register_init: failed to "
1000 "register misc device for \"%s\"\n", sn_hwperf_dev.name);
1003 return e;
1006 device_initcall(sn_hwperf_misc_register_init); /* after misc_init() */
1007 EXPORT_SYMBOL(sn_hwperf_get_nearest_node);