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
6 * Copyright (C) 2004-2005 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
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>
38 #include <asm/semaphore.h>
39 #include <asm/segment.h>
40 #include <asm/uaccess.h>
42 #include <asm/sn/io.h>
43 #include <asm/sn/sn_sal.h>
44 #include <asm/sn/module.h>
45 #include <asm/sn/geo.h>
46 #include <asm/sn/sn2/sn_hwperf.h>
47 #include <asm/sn/addrs.h>
49 static void *sn_hwperf_salheap = NULL;
50 static int sn_hwperf_obj_cnt = 0;
51 static nasid_t sn_hwperf_master_nasid = INVALID_NASID;
52 static int sn_hwperf_init(void);
53 static DECLARE_MUTEX(sn_hwperf_init_mutex);
55 static int sn_hwperf_enum_objects(int *nobj, struct sn_hwperf_object_info **ret)
59 struct sn_hwperf_object_info *objbuf = NULL;
61 if ((e = sn_hwperf_init()) < 0) {
62 printk("sn_hwperf_init failed: err %d\n", e);
66 sz = sn_hwperf_obj_cnt * sizeof(struct sn_hwperf_object_info);
67 if ((objbuf = (struct sn_hwperf_object_info *) vmalloc(sz)) == NULL) {
68 printk("sn_hwperf_enum_objects: vmalloc(%d) failed\n", (int)sz);
73 e = ia64_sn_hwperf_op(sn_hwperf_master_nasid, SN_HWPERF_ENUM_OBJECTS,
74 0, sz, (u64) objbuf, 0, 0, NULL);
75 if (e != SN_HWPERF_OP_OK) {
81 *nobj = sn_hwperf_obj_cnt;
86 static int sn_hwperf_location_to_bpos(char *location,
87 int *rack, int *bay, int *slot, int *slab)
91 /* first scan for an old style geoid string */
92 if (sscanf(location, "%03d%c%02d#%d",
93 rack, &type, bay, slab) == 4)
95 else /* scan for a new bladed geoid string */
96 if (sscanf(location, "%03d%c%02d^%02d#%d",
97 rack, &type, bay, slot, slab) != 5)
103 static int sn_hwperf_geoid_to_cnode(char *location)
107 moduleid_t module_id;
108 int rack, bay, slot, slab;
109 int this_rack, this_bay, this_slot, this_slab;
111 if (sn_hwperf_location_to_bpos(location, &rack, &bay, &slot, &slab))
114 for (cnode = 0; cnode < numionodes; cnode++) {
115 geoid = cnodeid_get_geoid(cnode);
116 module_id = geo_module(geoid);
117 this_rack = MODULE_GET_RACK(module_id);
118 this_bay = MODULE_GET_BPOS(module_id);
119 this_slot = 0; /* XXX */
120 this_slab = geo_slab(geoid);
121 if (rack == this_rack && bay == this_bay &&
122 slot == this_slot && slab == this_slab) {
127 return cnode < numionodes ? cnode : -1;
130 static int sn_hwperf_obj_to_cnode(struct sn_hwperf_object_info * obj)
132 if (!obj->sn_hwp_this_part)
134 return sn_hwperf_geoid_to_cnode(obj->location);
137 static int sn_hwperf_generic_ordinal(struct sn_hwperf_object_info *obj,
138 struct sn_hwperf_object_info *objs)
141 struct sn_hwperf_object_info *p;
143 for (ordinal=0, p=objs; p != obj; p++) {
144 if (SN_HWPERF_FOREIGN(p))
146 if (SN_HWPERF_SAME_OBJTYPE(p, obj))
153 static const char *slabname_node = "node"; /* SHub asic */
154 static const char *slabname_ionode = "ionode"; /* TIO asic */
155 static const char *slabname_router = "router"; /* NL3R or NL4R */
156 static const char *slabname_other = "other"; /* unknown asic */
158 static const char *sn_hwperf_get_slabname(struct sn_hwperf_object_info *obj,
159 struct sn_hwperf_object_info *objs, int *ordinal)
162 const char *slabname = slabname_other;
164 if ((isnode = SN_HWPERF_IS_NODE(obj)) || SN_HWPERF_IS_IONODE(obj)) {
165 slabname = isnode ? slabname_node : slabname_ionode;
166 *ordinal = sn_hwperf_obj_to_cnode(obj);
169 *ordinal = sn_hwperf_generic_ordinal(obj, objs);
170 if (SN_HWPERF_IS_ROUTER(obj))
171 slabname = slabname_router;
177 static void print_pci_topology(struct seq_file *s,
178 struct sn_hwperf_object_info *obj, int *ordinal,
179 char *pci_topo_buf, int len)
184 for (p1=pci_topo_buf; *p1 && p1 < pci_topo_buf + len;) {
185 if (!(p2 = strchr(p1, '\n')))
188 seq_printf(s, "pcibus %d %s-%s\n",
189 *ordinal, obj->location, p1);
195 static int sn_topology_show(struct seq_file *s, void *d)
202 const char *slabname;
206 struct cpuinfo_ia64 *c;
207 struct sn_hwperf_port_info *ptdata;
208 struct sn_hwperf_object_info *p;
209 struct sn_hwperf_object_info *obj = d; /* this object */
210 struct sn_hwperf_object_info *objs = s->private; /* all objects */
211 int rack, bay, slot, slab;
222 int pci_bus_ordinal = 0;
223 static int pci_topo_buf_len = 256;
226 seq_printf(s, "# sn_topology version 2\n");
227 seq_printf(s, "# objtype ordinal location partition"
228 " [attribute value [, ...]]\n");
230 if (ia64_sn_get_sn_info(0,
231 &shubtype, &nasid_mask, &nasid_shift, &system_size,
232 &sharing_size, &partid, &coher, ®ion_size))
234 for (nasid_msb=63; nasid_msb > 0; nasid_msb--) {
235 if (((u64)nasid_mask << nasid_shift) & (1ULL << nasid_msb))
238 seq_printf(s, "partition %u %s local "
240 "nasid_mask 0x%016lx, "
244 "coherency_domain %d, "
247 partid, system_utsname.nodename,
248 shubtype ? "shub2" : "shub1",
249 (u64)nasid_mask << nasid_shift, nasid_msb, nasid_shift,
250 system_size, sharing_size, coher, region_size);
253 if (SN_HWPERF_FOREIGN(obj)) {
254 /* private in another partition: not interesting */
258 for (i = 0; i < SN_HWPERF_MAXSTRING && obj->name[i]; i++) {
259 if (obj->name[i] == ' ')
263 slabname = sn_hwperf_get_slabname(obj, objs, &ordinal);
264 seq_printf(s, "%s %d %s %s asic %s", slabname, ordinal, obj->location,
265 obj->sn_hwp_this_part ? "local" : "shared", obj->name);
267 if (!SN_HWPERF_IS_NODE(obj) && !SN_HWPERF_IS_IONODE(obj))
270 seq_printf(s, ", nasid 0x%x", cnodeid_to_nasid(ordinal));
271 for (i=0; i < numionodes; i++) {
272 seq_printf(s, i ? ":%d" : ", dist %d",
273 node_distance(ordinal, i));
278 * CPUs on this node, if any
280 cpumask = node_to_cpumask(ordinal);
281 for_each_online_cpu(i) {
282 if (cpu_isset(i, cpumask)) {
283 slice = 'a' + cpuid_to_slice(i);
285 seq_printf(s, "cpu %d %s%c local"
286 " freq %luMHz, arch ia64",
287 i, obj->location, slice,
288 c->proc_freq / 1000000);
289 for_each_online_cpu(j) {
290 seq_printf(s, j ? ":%d" : ", dist %d",
293 cpuid_to_cnodeid(j)));
300 * PCI busses attached to this node, if any
303 if (!(pci_topo_buf = vmalloc(pci_topo_buf_len))) {
304 printk("sn_topology_show: kmalloc failed\n");
308 if (sn_hwperf_location_to_bpos(obj->location,
309 &rack, &bay, &slot, &slab) != 0)
312 e = ia64_sn_ioif_get_pci_topology(rack, bay, slot, slab,
313 pci_topo_buf, pci_topo_buf_len);
316 case SALRET_NOT_IMPLEMENTED:
317 case SALRET_INVALID_ARG:
318 /* ignore, don't print anything */
323 /* retry with a bigger buffer */
324 pci_topo_buf_len += 256;
327 case SN_HWPERF_OP_OK:
328 /* export pci bus info */
329 print_pci_topology(s, obj, &pci_bus_ordinal,
330 pci_topo_buf, pci_topo_buf_len);
334 } while (e != SN_HWPERF_OP_OK && pci_topo_buf_len < 0x200000);
341 sz = obj->ports * sizeof(struct sn_hwperf_port_info);
342 if ((ptdata = vmalloc(sz)) == NULL)
344 e = ia64_sn_hwperf_op(sn_hwperf_master_nasid,
345 SN_HWPERF_ENUM_PORTS, obj->id, sz,
346 (u64) ptdata, 0, 0, NULL);
347 if (e != SN_HWPERF_OP_OK)
349 for (ordinal=0, p=objs; p != obj; p++) {
350 if (!SN_HWPERF_FOREIGN(p))
353 for (pt = 0; pt < obj->ports; pt++) {
354 for (p = objs, i = 0; i < sn_hwperf_obj_cnt; i++, p++) {
355 if (ptdata[pt].conn_id == p->id) {
359 seq_printf(s, "numalink %d %s-%d",
360 ordinal+pt, obj->location, ptdata[pt].port);
362 if (i >= sn_hwperf_obj_cnt) {
364 seq_puts(s, " local endpoint disconnected"
365 ", protocol unknown\n");
369 if (obj->sn_hwp_this_part && p->sn_hwp_this_part)
370 /* both ends local to this partition */
371 seq_puts(s, " local");
372 else if (!obj->sn_hwp_this_part && !p->sn_hwp_this_part)
373 /* both ends of the link in foreign partiton */
374 seq_puts(s, " foreign");
376 /* link straddles a partition */
377 seq_puts(s, " shared");
380 * Unlikely, but strictly should query the LLP config
381 * registers because an NL4R can be configured to run
382 * NL3 protocol, even when not talking to an NL3 router.
383 * Ditto for node-node.
385 seq_printf(s, " endpoint %s-%d, protocol %s\n",
386 p->location, ptdata[pt].conn_port,
387 (SN_HWPERF_IS_NL3ROUTER(obj) ||
388 SN_HWPERF_IS_NL3ROUTER(p)) ? "LLP3" : "LLP4");
396 static void *sn_topology_start(struct seq_file *s, loff_t * pos)
398 struct sn_hwperf_object_info *objs = s->private;
400 if (*pos < sn_hwperf_obj_cnt)
401 return (void *)(objs + *pos);
406 static void *sn_topology_next(struct seq_file *s, void *v, loff_t * pos)
409 return sn_topology_start(s, pos);
412 static void sn_topology_stop(struct seq_file *m, void *v)
418 * /proc/sgi_sn/sn_topology, read-only using seq_file
420 static struct seq_operations sn_topology_seq_ops = {
421 .start = sn_topology_start,
422 .next = sn_topology_next,
423 .stop = sn_topology_stop,
424 .show = sn_topology_show
427 struct sn_hwperf_op_info {
429 struct sn_hwperf_ioctl_args *a;
435 static void sn_hwperf_call_sal(void *info)
437 struct sn_hwperf_op_info *op_info = info;
440 r = ia64_sn_hwperf_op(sn_hwperf_master_nasid, op_info->op,
441 op_info->a->arg, op_info->a->sz,
442 (u64) op_info->p, 0, 0, op_info->v0);
446 static int sn_hwperf_op_cpu(struct sn_hwperf_op_info *op_info)
451 cpumask_t save_allowed;
453 cpu = (op_info->a->arg & SN_HWPERF_ARG_CPU_MASK) >> 32;
454 use_ipi = op_info->a->arg & SN_HWPERF_ARG_USE_IPI_MASK;
455 op_info->a->arg &= SN_HWPERF_ARG_OBJID_MASK;
457 if (cpu != SN_HWPERF_ARG_ANY_CPU) {
458 if (cpu >= num_online_cpus() || !cpu_online(cpu)) {
464 if (cpu == SN_HWPERF_ARG_ANY_CPU || cpu == get_cpu()) {
465 /* don't care, or already on correct cpu */
466 sn_hwperf_call_sal(op_info);
470 /* use an interprocessor interrupt to call SAL */
471 smp_call_function_single(cpu, sn_hwperf_call_sal,
475 /* migrate the task before calling SAL */
476 save_allowed = current->cpus_allowed;
477 set_cpus_allowed(current, cpumask_of_cpu(cpu));
478 sn_hwperf_call_sal(op_info);
479 set_cpus_allowed(current, save_allowed);
488 /* map SAL hwperf error code to system error code */
489 static int sn_hwperf_map_err(int hwperf_err)
494 case SN_HWPERF_OP_OK:
498 case SN_HWPERF_OP_NOMEM:
502 case SN_HWPERF_OP_NO_PERM:
506 case SN_HWPERF_OP_IO_ERROR:
510 case SN_HWPERF_OP_BUSY:
514 case SN_HWPERF_OP_RECONFIGURE:
518 case SN_HWPERF_OP_INVAL:
528 * ioctl for "sn_hwperf" misc device
531 sn_hwperf_ioctl(struct inode *in, struct file *fp, u32 op, u64 arg)
533 struct sn_hwperf_ioctl_args a;
534 struct cpuinfo_ia64 *cdata;
535 struct sn_hwperf_object_info *objs;
536 struct sn_hwperf_object_info *cpuobj;
537 struct sn_hwperf_op_info op_info;
549 /* only user requests are allowed here */
550 if ((op & SN_HWPERF_OP_MASK) < 10) {
554 r = copy_from_user(&a, (const void __user *)arg,
555 sizeof(struct sn_hwperf_ioctl_args));
562 * Allocate memory to hold a kernel copy of the user buffer. The
563 * buffer contents are either copied in or out (or both) of user
564 * space depending on the flags encoded in the requested operation.
574 if (op & SN_HWPERF_OP_MEM_COPYIN) {
575 r = copy_from_user(p, (const void __user *)a.ptr, a.sz);
583 case SN_HWPERF_GET_CPU_INFO:
584 if (a.sz == sizeof(u64)) {
585 /* special case to get size needed */
586 *(u64 *) p = (u64) num_online_cpus() *
587 sizeof(struct sn_hwperf_object_info);
589 if (a.sz < num_online_cpus() * sizeof(struct sn_hwperf_object_info)) {
593 if ((r = sn_hwperf_enum_objects(&nobj, &objs)) == 0) {
595 for (i = 0; i < nobj; i++) {
596 node = sn_hwperf_obj_to_cnode(objs + i);
597 for_each_online_cpu(j) {
598 if (node != cpu_to_node(j))
600 cpuobj = (struct sn_hwperf_object_info *) p + j;
601 slice = 'a' + cpuid_to_slice(j);
604 snprintf(cpuobj->name,
605 sizeof(cpuobj->name),
607 cdata->proc_freq / 1000000,
609 snprintf(cpuobj->location,
610 sizeof(cpuobj->location),
611 "%s%c", objs[i].location,
620 case SN_HWPERF_GET_NODE_NASID:
621 if (a.sz != sizeof(u64) ||
622 (node = a.arg) < 0 || node >= numionodes) {
626 *(u64 *)p = (u64)cnodeid_to_nasid(node);
629 case SN_HWPERF_GET_OBJ_NODE:
630 if (a.sz != sizeof(u64) || a.arg < 0) {
634 if ((r = sn_hwperf_enum_objects(&nobj, &objs)) == 0) {
640 if (objs[(i = a.arg)].id != a.arg) {
641 for (i = 0; i < nobj; i++) {
642 if (objs[i].id == a.arg)
651 *(u64 *)p = (u64)sn_hwperf_obj_to_cnode(objs + i);
656 case SN_HWPERF_GET_MMRS:
657 case SN_HWPERF_SET_MMRS:
658 case SN_HWPERF_OBJECT_DISTANCE:
663 r = sn_hwperf_op_cpu(&op_info);
665 r = sn_hwperf_map_err(r);
672 /* all other ops are a direct SAL call */
673 r = ia64_sn_hwperf_op(sn_hwperf_master_nasid, op,
674 a.arg, a.sz, (u64) p, 0, 0, &v0);
676 r = sn_hwperf_map_err(r);
683 if (op & SN_HWPERF_OP_MEM_COPYOUT) {
684 r = copy_to_user((void __user *)a.ptr, p, a.sz);
698 static struct file_operations sn_hwperf_fops = {
699 .ioctl = sn_hwperf_ioctl,
702 static struct miscdevice sn_hwperf_dev = {
708 static int sn_hwperf_init(void)
714 /* single threaded, once-only initialization */
715 down(&sn_hwperf_init_mutex);
716 if (sn_hwperf_salheap) {
717 up(&sn_hwperf_init_mutex);
722 * The PROM code needs a fixed reference node. For convenience the
723 * same node as the console I/O is used.
725 sn_hwperf_master_nasid = (nasid_t) ia64_sn_get_console_nasid();
728 * Request the needed size and install the PROM scratch area.
729 * The PROM keeps various tracking bits in this memory area.
731 salr = ia64_sn_hwperf_op(sn_hwperf_master_nasid,
732 (u64) SN_HWPERF_GET_HEAPSIZE, 0,
733 (u64) sizeof(u64), (u64) &v, 0, 0, NULL);
734 if (salr != SN_HWPERF_OP_OK) {
739 if ((sn_hwperf_salheap = vmalloc(v)) == NULL) {
743 salr = ia64_sn_hwperf_op(sn_hwperf_master_nasid,
744 SN_HWPERF_INSTALL_HEAP, 0, v,
745 (u64) sn_hwperf_salheap, 0, 0, NULL);
746 if (salr != SN_HWPERF_OP_OK) {
751 salr = ia64_sn_hwperf_op(sn_hwperf_master_nasid,
752 SN_HWPERF_OBJECT_COUNT, 0,
753 sizeof(u64), (u64) &v, 0, 0, NULL);
754 if (salr != SN_HWPERF_OP_OK) {
758 sn_hwperf_obj_cnt = (int)v;
761 if (e < 0 && sn_hwperf_salheap) {
762 vfree(sn_hwperf_salheap);
763 sn_hwperf_salheap = NULL;
764 sn_hwperf_obj_cnt = 0;
769 * Register a dynamic misc device for ioctl. Platforms
770 * supporting hotplug will create /dev/sn_hwperf, else
771 * user can to look up the minor number in /proc/misc.
773 if ((e = misc_register(&sn_hwperf_dev)) != 0) {
774 printk(KERN_ERR "sn_hwperf_init: misc register "
775 "for \"sn_hwperf\" failed, err %d\n", e);
779 up(&sn_hwperf_init_mutex);
783 int sn_topology_open(struct inode *inode, struct file *file)
786 struct seq_file *seq;
787 struct sn_hwperf_object_info *objbuf;
790 if ((e = sn_hwperf_enum_objects(&nobj, &objbuf)) == 0) {
791 e = seq_open(file, &sn_topology_seq_ops);
792 seq = file->private_data;
793 seq->private = objbuf;
799 int sn_topology_release(struct inode *inode, struct file *file)
801 struct seq_file *seq = file->private_data;
804 return seq_release(inode, file);