Fedora kernel-2.6.17-1.2142_FC4 patched with stable patch-2.6.17.4-vs2.0.2-rc26.diff
[linux-2.6.git] / arch / ia64 / sn / kernel / sn2 / sn_hwperf.c
index 8a810d3..739c948 100644 (file)
@@ -3,7 +3,7 @@
  * License.  See the file "COPYING" in the main directory of this archive
  * for more details.
  *
- * Copyright (C) 2004 Silicon Graphics, Inc. All rights reserved.
+ * Copyright (C) 2004-2006 Silicon Graphics, Inc. All rights reserved.
  *
  * SGI Altix topology and hardware performance monitoring API.
  * Mark Goodwin <markgw@sgi.com>. 
 #include <linux/vmalloc.h>
 #include <linux/seq_file.h>
 #include <linux/miscdevice.h>
+#include <linux/utsname.h>
 #include <linux/cpumask.h>
 #include <linux/smp_lock.h>
+#include <linux/nodemask.h>
 #include <asm/processor.h>
 #include <asm/topology.h>
 #include <asm/smp.h>
 #include <asm/semaphore.h>
-#include <asm/segment.h>
 #include <asm/uaccess.h>
-#include <asm-ia64/sal.h>
-#include <asm-ia64/sn/sn_sal.h>
-#include <asm-ia64/sn/sn2/sn_hwperf.h>
+#include <asm/sal.h>
+#include <asm/sn/io.h>
+#include <asm/sn/sn_sal.h>
+#include <asm/sn/module.h>
+#include <asm/sn/geo.h>
+#include <asm/sn/sn2/sn_hwperf.h>
+#include <asm/sn/addrs.h>
 
 static void *sn_hwperf_salheap = NULL;
 static int sn_hwperf_obj_cnt = 0;
@@ -53,7 +58,7 @@ static int sn_hwperf_enum_objects(int *nobj, struct sn_hwperf_object_info **ret)
        struct sn_hwperf_object_info *objbuf = NULL;
 
        if ((e = sn_hwperf_init()) < 0) {
-               printk("sn_hwperf_init failed: err %d\n", e);
+               printk(KERN_ERR "sn_hwperf_init failed: err %d\n", e);
                goto out;
        }
 
@@ -77,28 +82,58 @@ out:
        return e;
 }
 
+static int sn_hwperf_location_to_bpos(char *location,
+       int *rack, int *bay, int *slot, int *slab)
+{
+       char type;
+
+       /* first scan for an old style geoid string */
+       if (sscanf(location, "%03d%c%02d#%d",
+               rack, &type, bay, slab) == 4)
+               *slot = 0; 
+       else /* scan for a new bladed geoid string */
+       if (sscanf(location, "%03d%c%02d^%02d#%d",
+               rack, &type, bay, slot, slab) != 5)
+               return -1; 
+       /* success */
+       return 0;
+}
+
 static int sn_hwperf_geoid_to_cnode(char *location)
 {
        int cnode;
-       int mod, slot, slab;
-       int cmod, cslot, cslab;
+       geoid_t geoid;
+       moduleid_t module_id;
+       int rack, bay, slot, slab;
+       int this_rack, this_bay, this_slot, this_slab;
 
-       if (sscanf(location, "%03dc%02d#%d", &mod, &slot, &slab) != 3)
+       if (sn_hwperf_location_to_bpos(location, &rack, &bay, &slot, &slab))
                return -1;
-       for (cnode = 0; cnode < numnodes; cnode++) {
-               /* XXX: need a better way than this ... */
-               if (sscanf(NODEPDA(cnode)->hwg_node_name,
-                  "hw/module/%03dc%02d/slab/%d", &cmod, &cslot, &cslab) == 3) {
-                       if (mod == cmod && slot == cslot && slab == cslab)
-                               break;
+
+       /*
+        * FIXME: replace with cleaner for_each_XXX macro which addresses
+        * both compute and IO nodes once ACPI3.0 is available.
+        */
+       for (cnode = 0; cnode < num_cnodes; cnode++) {
+               geoid = cnodeid_get_geoid(cnode);
+               module_id = geo_module(geoid);
+               this_rack = MODULE_GET_RACK(module_id);
+               this_bay = MODULE_GET_BPOS(module_id);
+               this_slot = geo_slot(geoid);
+               this_slab = geo_slab(geoid);
+               if (rack == this_rack && bay == this_bay &&
+                       slot == this_slot && slab == this_slab) {
+                       break;
                }
        }
 
-       return cnode < numnodes ? cnode : -1;
+       return node_possible(cnode) ? cnode : -1;
 }
 
 static int sn_hwperf_obj_to_cnode(struct sn_hwperf_object_info * obj)
 {
+       if (!SN_HWPERF_IS_NODE(obj) && !SN_HWPERF_IS_IONODE(obj))
+               BUG();
        if (!obj->sn_hwp_this_part)
                return -1;
        return sn_hwperf_geoid_to_cnode(obj->location);
@@ -113,62 +148,238 @@ static int sn_hwperf_generic_ordinal(struct sn_hwperf_object_info *obj,
        for (ordinal=0, p=objs; p != obj; p++) {
                if (SN_HWPERF_FOREIGN(p))
                        continue;
-               if (p->location[3] == obj->location[3])
+               if (SN_HWPERF_SAME_OBJTYPE(p, obj))
                        ordinal++;
        }
 
        return ordinal;
 }
 
-#ifndef MODULE_IOBRICK 
-/* this will be available when ioif TIO support is added */
-#define MODULE_IOBRICK (MODULE_OPUSBRICK+1)
-#endif
+static const char *slabname_node =     "node"; /* SHub asic */
+static const char *slabname_ionode =   "ionode"; /* TIO asic */
+static const char *slabname_router =   "router"; /* NL3R or NL4R */
+static const char *slabname_other =    "other"; /* unknown asic */
+
+static const char *sn_hwperf_get_slabname(struct sn_hwperf_object_info *obj,
+                       struct sn_hwperf_object_info *objs, int *ordinal)
+{
+       int isnode;
+       const char *slabname = slabname_other;
+
+       if ((isnode = SN_HWPERF_IS_NODE(obj)) || SN_HWPERF_IS_IONODE(obj)) {
+               slabname = isnode ? slabname_node : slabname_ionode;
+               *ordinal = sn_hwperf_obj_to_cnode(obj);
+       }
+       else {
+               *ordinal = sn_hwperf_generic_ordinal(obj, objs);
+               if (SN_HWPERF_IS_ROUTER(obj))
+                       slabname = slabname_router;
+       }
+
+       return slabname;
+}
 
-static const char *sn_hwperf_get_brickname(struct sn_hwperf_object_info *obj,
-                               struct sn_hwperf_object_info *objs, int *ordinal)
+static void print_pci_topology(struct seq_file *s)
+{
+       char *p;
+       size_t sz;
+       int e;
+
+       for (sz = PAGE_SIZE; sz < 16 * PAGE_SIZE; sz += PAGE_SIZE) {
+               if (!(p = (char *)kmalloc(sz, GFP_KERNEL)))
+                       break;
+               e = ia64_sn_ioif_get_pci_topology(__pa(p), sz);
+               if (e == SALRET_OK)
+                       seq_puts(s, p);
+               kfree(p);
+               if (e == SALRET_OK || e == SALRET_NOT_IMPLEMENTED)
+                       break;
+       }
+}
+
+static inline int sn_hwperf_has_cpus(cnodeid_t node)
+{
+       return node_online(node) && nr_cpus_node(node);
+}
+
+static inline int sn_hwperf_has_mem(cnodeid_t node)
+{
+       return node_online(node) && NODE_DATA(node)->node_present_pages;
+}
+
+static struct sn_hwperf_object_info *
+sn_hwperf_findobj_id(struct sn_hwperf_object_info *objbuf,
+       int nobj, int id)
 {
        int i;
-       const char *objtype = NULL;
+       struct sn_hwperf_object_info *p = objbuf;
+
+       for (i=0; i < nobj; i++, p++) {
+               if (p->id == id)
+                       return p;
+       }
+
+       return NULL;
+
+}
+
+static int sn_hwperf_get_nearest_node_objdata(struct sn_hwperf_object_info *objbuf,
+       int nobj, cnodeid_t node, cnodeid_t *near_mem_node, cnodeid_t *near_cpu_node)
+{
+       int e;
+       struct sn_hwperf_object_info *nodeobj = NULL;
+       struct sn_hwperf_object_info *op;
+       struct sn_hwperf_object_info *dest;
+       struct sn_hwperf_object_info *router;
+       struct sn_hwperf_port_info ptdata[16];
+       int sz, i, j;
+       cnodeid_t c;
+       int found_mem = 0;
+       int found_cpu = 0;
+
+       if (!node_possible(node))
+               return -EINVAL;
+
+       if (sn_hwperf_has_cpus(node)) {
+               if (near_cpu_node)
+                       *near_cpu_node = node;
+               found_cpu++;
+       }
 
-       for (i=0; i < MAX_BRICK_TYPES; i++) {
-               if (brick_types[i] != obj->location[3])
+       if (sn_hwperf_has_mem(node)) {
+               if (near_mem_node)
+                       *near_mem_node = node;
+               found_mem++;
+       }
+
+       if (found_cpu && found_mem)
+               return 0; /* trivially successful */
+
+       /* find the argument node object */
+       for (i=0, op=objbuf; i < nobj; i++, op++) {
+               if (!SN_HWPERF_IS_NODE(op) && !SN_HWPERF_IS_IONODE(op))
                        continue;
-               switch (i) {
-               case MODULE_CBRICK:
-                   objtype = "node";
-                   *ordinal = sn_hwperf_obj_to_cnode(obj); /* cnodeid */
-                   break;
-
-               case MODULE_RBRICK:
-                   objtype = "router";
-                   *ordinal = sn_hwperf_generic_ordinal(obj, objs);
-                   break;
-
-               case MODULE_IOBRICK:
-                   objtype = "ionode";
-                   *ordinal = sn_hwperf_generic_ordinal(obj, objs);
-                   break;
+               if (node == sn_hwperf_obj_to_cnode(op)) {
+                       nodeobj = op;
+                       break;
                }
-               break;
+       }
+       if (!nodeobj) {
+               e = -ENOENT;
+               goto err;
        }
 
-       if (i == MAX_BRICK_TYPES) {
-               objtype = "other";
-               *ordinal = sn_hwperf_generic_ordinal(obj, objs);
+       /* get it's interconnect topology */
+       sz = op->ports * sizeof(struct sn_hwperf_port_info);
+       if (sz > sizeof(ptdata))
+               BUG();
+       e = ia64_sn_hwperf_op(sn_hwperf_master_nasid,
+                             SN_HWPERF_ENUM_PORTS, nodeobj->id, sz,
+                             (u64)&ptdata, 0, 0, NULL);
+       if (e != SN_HWPERF_OP_OK) {
+               e = -EINVAL;
+               goto err;
+       }
+
+       /* find nearest node with cpus and nearest memory */
+       for (router=NULL, j=0; j < op->ports; j++) {
+               dest = sn_hwperf_findobj_id(objbuf, nobj, ptdata[j].conn_id);
+               if (dest && SN_HWPERF_IS_ROUTER(dest))
+                       router = dest;
+               if (!dest || SN_HWPERF_FOREIGN(dest) ||
+                   !SN_HWPERF_IS_NODE(dest) || SN_HWPERF_IS_IONODE(dest)) {
+                       continue;
+               }
+               c = sn_hwperf_obj_to_cnode(dest);
+               if (!found_cpu && sn_hwperf_has_cpus(c)) {
+                       if (near_cpu_node)
+                               *near_cpu_node = c;
+                       found_cpu++;
+               }
+               if (!found_mem && sn_hwperf_has_mem(c)) {
+                       if (near_mem_node)
+                               *near_mem_node = c;
+                       found_mem++;
+               }
        }
 
-       return objtype;
+       if (router && (!found_cpu || !found_mem)) {
+               /* search for a node connected to the same router */
+               sz = router->ports * sizeof(struct sn_hwperf_port_info);
+               if (sz > sizeof(ptdata))
+                       BUG();
+               e = ia64_sn_hwperf_op(sn_hwperf_master_nasid,
+                                     SN_HWPERF_ENUM_PORTS, router->id, sz,
+                                     (u64)&ptdata, 0, 0, NULL);
+               if (e != SN_HWPERF_OP_OK) {
+                       e = -EINVAL;
+                       goto err;
+               }
+               for (j=0; j < router->ports; j++) {
+                       dest = sn_hwperf_findobj_id(objbuf, nobj,
+                               ptdata[j].conn_id);
+                       if (!dest || dest->id == node ||
+                           SN_HWPERF_FOREIGN(dest) ||
+                           !SN_HWPERF_IS_NODE(dest) ||
+                           SN_HWPERF_IS_IONODE(dest)) {
+                               continue;
+                       }
+                       c = sn_hwperf_obj_to_cnode(dest);
+                       if (!found_cpu && sn_hwperf_has_cpus(c)) {
+                               if (near_cpu_node)
+                                       *near_cpu_node = c;
+                               found_cpu++;
+                       }
+                       if (!found_mem && sn_hwperf_has_mem(c)) {
+                               if (near_mem_node)
+                                       *near_mem_node = c;
+                               found_mem++;
+                       }
+                       if (found_cpu && found_mem)
+                               break;
+               }
+       }
+
+       if (!found_cpu || !found_mem) {
+               /* resort to _any_ node with CPUs and memory */
+               for (i=0, op=objbuf; i < nobj; i++, op++) {
+                       if (SN_HWPERF_FOREIGN(op) ||
+                           SN_HWPERF_IS_IONODE(op) ||
+                           !SN_HWPERF_IS_NODE(op)) {
+                               continue;
+                       }
+                       c = sn_hwperf_obj_to_cnode(op);
+                       if (!found_cpu && sn_hwperf_has_cpus(c)) {
+                               if (near_cpu_node)
+                                       *near_cpu_node = c;
+                               found_cpu++;
+                       }
+                       if (!found_mem && sn_hwperf_has_mem(c)) {
+                               if (near_mem_node)
+                                       *near_mem_node = c;
+                               found_mem++;
+                       }
+                       if (found_cpu && found_mem)
+                               break;
+               }
+       }
+
+       if (!found_cpu || !found_mem)
+               e = -ENODATA;
+
+err:
+       return e;
 }
 
+
 static int sn_topology_show(struct seq_file *s, void *d)
 {
        int sz;
        int pt;
-       int e;
+       int e = 0;
        int i;
        int j;
-       const char *brickname;
+       const char *slabname;
        int ordinal;
        cpumask_t cpumask;
        char slice;
@@ -177,11 +388,44 @@ static int sn_topology_show(struct seq_file *s, void *d)
        struct sn_hwperf_object_info *p;
        struct sn_hwperf_object_info *obj = d;  /* this object */
        struct sn_hwperf_object_info *objs = s->private; /* all objects */
+       u8 shubtype;
+       u8 system_size;
+       u8 sharing_size;
+       u8 partid;
+       u8 coher;
+       u8 nasid_shift;
+       u8 region_size;
+       u16 nasid_mask;
+       int nasid_msb;
 
        if (obj == objs) {
-               seq_printf(s, "# sn_topology version 1\n");
+               seq_printf(s, "# sn_topology version 2\n");
                seq_printf(s, "# objtype ordinal location partition"
                        " [attribute value [, ...]]\n");
+
+               if (ia64_sn_get_sn_info(0,
+                       &shubtype, &nasid_mask, &nasid_shift, &system_size,
+                       &sharing_size, &partid, &coher, &region_size))
+                       BUG();
+               for (nasid_msb=63; nasid_msb > 0; nasid_msb--) {
+                       if (((u64)nasid_mask << nasid_shift) & (1ULL << nasid_msb))
+                               break;
+               }
+               seq_printf(s, "partition %u %s local "
+                       "shubtype %s, "
+                       "nasid_mask 0x%016lx, "
+                       "nasid_bits %d:%d, "
+                       "system_size %d, "
+                       "sharing_size %d, "
+                       "coherency_domain %d, "
+                       "region_size %d\n",
+
+                       partid, system_utsname.nodename,
+                       shubtype ? "shub2" : "shub1", 
+                       (u64)nasid_mask << nasid_shift, nasid_msb, nasid_shift,
+                       system_size, sharing_size, coher, region_size);
+
+               print_pci_topology(s);
        }
 
        if (SN_HWPERF_FOREIGN(obj)) {
@@ -189,27 +433,40 @@ static int sn_topology_show(struct seq_file *s, void *d)
                return 0;
        }
 
-       for (i = 0; obj->name[i]; i++) {
+       for (i = 0; i < SN_HWPERF_MAXSTRING && obj->name[i]; i++) {
                if (obj->name[i] == ' ')
                        obj->name[i] = '_';
        }
 
-       brickname = sn_hwperf_get_brickname(obj, objs, &ordinal);
-       seq_printf(s, "%s %d %s %s asic %s", brickname, ordinal, obj->location,
+       slabname = sn_hwperf_get_slabname(obj, objs, &ordinal);
+       seq_printf(s, "%s %d %s %s asic %s", slabname, ordinal, obj->location,
                obj->sn_hwp_this_part ? "local" : "shared", obj->name);
 
-       if (obj->location[3] != 'c')
+       if (!SN_HWPERF_IS_NODE(obj) && !SN_HWPERF_IS_IONODE(obj))
                seq_putc(s, '\n');
        else {
+               cnodeid_t near_mem = -1;
+               cnodeid_t near_cpu = -1;
+
                seq_printf(s, ", nasid 0x%x", cnodeid_to_nasid(ordinal));
-               for (i=0; i < numnodes; i++) {
-                       seq_printf(s, i ? ":%d" : ", dist %d",
-                               node_distance(ordinal, i));
+
+               if (sn_hwperf_get_nearest_node_objdata(objs, sn_hwperf_obj_cnt,
+                       ordinal, &near_mem, &near_cpu) == 0) {
+                       seq_printf(s, ", near_mem_nodeid %d, near_cpu_nodeid %d",
+                               near_mem, near_cpu);
+               }
+
+               if (!SN_HWPERF_IS_IONODE(obj)) {
+                       for_each_online_node(i) {
+                               seq_printf(s, i ? ":%d" : ", dist %d",
+                                       node_distance(ordinal, i));
+                       }
                }
+
                seq_putc(s, '\n');
 
                /*
-                * CPUs on this node
+                * CPUs on this node, if any
                 */
                cpumask = node_to_cpumask(ordinal);
                for_each_online_cpu(i) {
@@ -223,8 +480,8 @@ static int sn_topology_show(struct seq_file *s, void *d)
                                for_each_online_cpu(j) {
                                        seq_printf(s, j ? ":%d" : ", dist %d",
                                                node_distance(
-                                                   cpuid_to_cnodeid(i),
-                                                   cpuid_to_cnodeid(j)));
+                                                   cpu_to_node(i),
+                                                   cpu_to_node(j)));
                                }
                                seq_putc(s, '\n');
                        }
@@ -236,7 +493,7 @@ static int sn_topology_show(struct seq_file *s, void *d)
                 * numalink ports
                 */
                sz = obj->ports * sizeof(struct sn_hwperf_port_info);
-               if ((ptdata = vmalloc(sz)) == NULL)
+               if ((ptdata = kmalloc(sz, GFP_KERNEL)) == NULL)
                        return -ENOMEM;
                e = ia64_sn_hwperf_op(sn_hwperf_master_nasid,
                                      SN_HWPERF_ENUM_PORTS, obj->id, sz,
@@ -253,11 +510,16 @@ static int sn_topology_show(struct seq_file *s, void *d)
                                        break;
                                }
                        }
-                       if (i >= sn_hwperf_obj_cnt)
-                               continue;
                        seq_printf(s, "numalink %d %s-%d",
                            ordinal+pt, obj->location, ptdata[pt].port);
 
+                       if (i >= sn_hwperf_obj_cnt) {
+                               /* no connection */
+                               seq_puts(s, " local endpoint disconnected"
+                                           ", protocol unknown\n");
+                               continue;
+                       }
+
                        if (obj->sn_hwp_this_part && p->sn_hwp_this_part)
                                /* both ends local to this partition */
                                seq_puts(s, " local");
@@ -276,11 +538,10 @@ static int sn_topology_show(struct seq_file *s, void *d)
                         */
                        seq_printf(s, " endpoint %s-%d, protocol %s\n",
                                p->location, ptdata[pt].conn_port,
-                               strcmp(obj->name, "NL3Router") == 0 ||
-                               strcmp(p->name, "NL3Router") == 0 ?
-                               "LLP3" : "LLP4");
+                               (SN_HWPERF_IS_NL3ROUTER(obj) ||
+                               SN_HWPERF_IS_NL3ROUTER(p)) ?  "LLP3" : "LLP4");
                }
-               vfree(ptdata);
+               kfree(ptdata);
        }
 
        return 0;
@@ -348,7 +609,7 @@ static int sn_hwperf_op_cpu(struct sn_hwperf_op_info *op_info)
        op_info->a->arg &= SN_HWPERF_ARG_OBJID_MASK;
 
        if (cpu != SN_HWPERF_ARG_ANY_CPU) {
-               if (cpu >= num_online_cpus() || !cpu_online(cpu)) {
+               if (cpu >= NR_CPUS || !cpu_online(cpu)) {
                        r = -EINVAL;
                        goto out;
                }
@@ -378,6 +639,45 @@ out:
        return r;
 }
 
+/* map SAL hwperf error code to system error code */
+static int sn_hwperf_map_err(int hwperf_err)
+{
+       int e;
+
+       switch(hwperf_err) {
+       case SN_HWPERF_OP_OK:
+               e = 0;
+               break;
+
+       case SN_HWPERF_OP_NOMEM:
+               e = -ENOMEM;
+               break;
+
+       case SN_HWPERF_OP_NO_PERM:
+               e = -EPERM;
+               break;
+
+       case SN_HWPERF_OP_IO_ERROR:
+               e = -EIO;
+               break;
+
+       case SN_HWPERF_OP_BUSY:
+               e = -EBUSY;
+               break;
+
+       case SN_HWPERF_OP_RECONFIGURE:
+               e = -EAGAIN;
+               break;
+
+       case SN_HWPERF_OP_INVAL:
+       default:
+               e = -EINVAL;
+               break;
+       }
+
+       return e;
+}
+
 /*
  * ioctl for "sn_hwperf" misc device
  */
@@ -405,7 +705,7 @@ sn_hwperf_ioctl(struct inode *in, struct file *fp, u32 op, u64 arg)
                r = -EINVAL;
                goto error;
        }
-       r = copy_from_user(&a, (const void *)arg,
+       r = copy_from_user(&a, (const void __user *)arg,
                sizeof(struct sn_hwperf_ioctl_args));
        if (r != 0) {
                r = -EFAULT;
@@ -426,7 +726,7 @@ sn_hwperf_ioctl(struct inode *in, struct file *fp, u32 op, u64 arg)
        }
 
        if (op & SN_HWPERF_OP_MEM_COPYIN) {
-               r = copy_from_user(p, (const void *)a.ptr, a.sz);
+               r = copy_from_user(p, (const void __user *)a.ptr, a.sz);
                if (r != 0) {
                        r = -EFAULT;
                        goto error;
@@ -447,11 +747,14 @@ sn_hwperf_ioctl(struct inode *in, struct file *fp, u32 op, u64 arg)
                if ((r = sn_hwperf_enum_objects(&nobj, &objs)) == 0) {
                        memset(p, 0, a.sz);
                        for (i = 0; i < nobj; i++) {
+                               int cpuobj_index = 0;
+                               if (!SN_HWPERF_IS_NODE(objs + i))
+                                       continue;
                                node = sn_hwperf_obj_to_cnode(objs + i);
                                for_each_online_cpu(j) {
                                        if (node != cpu_to_node(j))
                                                continue;
-                                       cpuobj = (struct sn_hwperf_object_info *) p + j;
+                                       cpuobj = (struct sn_hwperf_object_info *) p + cpuobj_index++;
                                        slice = 'a' + cpuid_to_slice(j);
                                        cdata = cpu_data(j);
                                        cpuobj->id = j;
@@ -473,7 +776,7 @@ sn_hwperf_ioctl(struct inode *in, struct file *fp, u32 op, u64 arg)
 
        case SN_HWPERF_GET_NODE_NASID:
                if (a.sz != sizeof(u64) ||
-                  (node = a.arg) < 0 || node >= numnodes) {
+                  (node = a.arg) < 0 || !node_possible(node)) {
                        r = -EINVAL;
                        goto error;
                }
@@ -502,6 +805,14 @@ sn_hwperf_ioctl(struct inode *in, struct file *fp, u32 op, u64 arg)
                                vfree(objs);
                                goto error;
                        }
+
+                       if (!SN_HWPERF_IS_NODE(objs + i) &&
+                           !SN_HWPERF_IS_IONODE(objs + i)) {
+                               r = -ENOENT;
+                               vfree(objs);
+                               goto error;
+                       }
+
                        *(u64 *)p = (u64)sn_hwperf_obj_to_cnode(objs + i);
                        vfree(objs);
                }
@@ -515,18 +826,27 @@ sn_hwperf_ioctl(struct inode *in, struct file *fp, u32 op, u64 arg)
                op_info.v0 = &v0;
                op_info.op = op;
                r = sn_hwperf_op_cpu(&op_info);
+               if (r) {
+                       r = sn_hwperf_map_err(r);
+                       a.v0 = v0;
+                       goto error;
+               }
                break;
 
        default:
                /* all other ops are a direct SAL call */
                r = ia64_sn_hwperf_op(sn_hwperf_master_nasid, op,
                              a.arg, a.sz, (u64) p, 0, 0, &v0);
+               if (r) {
+                       r = sn_hwperf_map_err(r);
+                       goto error;
+               }
                a.v0 = v0;
                break;
        }
 
        if (op & SN_HWPERF_OP_MEM_COPYOUT) {
-               r = copy_to_user((void *)a.ptr, p, a.sz);
+               r = copy_to_user((void __user *)a.ptr, p, a.sz);
                if (r != 0) {
                        r = -EFAULT;
                        goto error;
@@ -534,8 +854,7 @@ sn_hwperf_ioctl(struct inode *in, struct file *fp, u32 op, u64 arg)
        }
 
 error:
-       if (p)
-               vfree(p);
+       vfree(p);
 
        lock_kernel();
        return r;
@@ -559,6 +878,7 @@ static int sn_hwperf_init(void)
 
        /* single threaded, once-only initialization */
        down(&sn_hwperf_init_mutex);
+
        if (sn_hwperf_salheap) {
                up(&sn_hwperf_init_mutex);
                return e;
@@ -609,19 +929,6 @@ out:
                sn_hwperf_salheap = NULL;
                sn_hwperf_obj_cnt = 0;
        }
-
-       if (!e) {
-               /*
-                * Register a dynamic misc device for ioctl. Platforms
-                * supporting hotplug will create /dev/sn_hwperf, else
-                * user can to look up the minor number in /proc/misc.
-                */
-               if ((e = misc_register(&sn_hwperf_dev)) != 0) {
-                       printk(KERN_ERR "sn_hwperf_init: misc register "
-                              "for \"sn_hwperf\" failed, err %d\n", e);
-               }
-       }
-
        up(&sn_hwperf_init_mutex);
        return e;
 }
@@ -646,7 +953,47 @@ int sn_topology_release(struct inode *inode, struct file *file)
 {
        struct seq_file *seq = file->private_data;
 
-       if (seq->private)
-               vfree(seq->private);
+       vfree(seq->private);
        return seq_release(inode, file);
 }
+
+int sn_hwperf_get_nearest_node(cnodeid_t node,
+       cnodeid_t *near_mem_node, cnodeid_t *near_cpu_node)
+{
+       int e;
+       int nobj;
+       struct sn_hwperf_object_info *objbuf;
+
+       if ((e = sn_hwperf_enum_objects(&nobj, &objbuf)) == 0) {
+               e = sn_hwperf_get_nearest_node_objdata(objbuf, nobj,
+                       node, near_mem_node, near_cpu_node);
+               vfree(objbuf);
+       }
+
+       return e;
+}
+
+static int __devinit sn_hwperf_misc_register_init(void)
+{
+       int e;
+
+       if (!ia64_platform_is("sn2"))
+               return 0;
+
+       sn_hwperf_init();
+
+       /*
+        * Register a dynamic misc device for hwperf ioctls. Platforms
+        * supporting hotplug will create /dev/sn_hwperf, else user
+        * can to look up the minor number in /proc/misc.
+        */
+       if ((e = misc_register(&sn_hwperf_dev)) != 0) {
+               printk(KERN_ERR "sn_hwperf_misc_register_init: failed to "
+               "register misc device for \"%s\"\n", sn_hwperf_dev.name);
+       }
+
+       return e;
+}
+
+device_initcall(sn_hwperf_misc_register_init); /* after misc_init() */
+EXPORT_SYMBOL(sn_hwperf_get_nearest_node);