58e48fdf5b475364da38ddea69c4e03e35ddc10a
[linux-2.6.git] / arch / ia64 / sn / kernel / sn2 / sn_hwperf.c
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  *
6  * Copyright (C) 2004-2005 Silicon Graphics, Inc. All rights reserved.
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>
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>
37 #include <asm/smp.h>
38 #include <asm/semaphore.h>
39 #include <asm/segment.h>
40 #include <asm/uaccess.h>
41 #include <asm/sal.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>
48
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);
54
55 static int sn_hwperf_enum_objects(int *nobj, struct sn_hwperf_object_info **ret)
56 {
57         int e;
58         u64 sz;
59         struct sn_hwperf_object_info *objbuf = NULL;
60
61         if ((e = sn_hwperf_init()) < 0) {
62                 printk("sn_hwperf_init failed: err %d\n", e);
63                 goto out;
64         }
65
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);
69                 e = -ENOMEM;
70                 goto out;
71         }
72
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) {
76                 e = -EINVAL;
77                 vfree(objbuf);
78         }
79
80 out:
81         *nobj = sn_hwperf_obj_cnt;
82         *ret = objbuf;
83         return e;
84 }
85
86 static int sn_hwperf_location_to_bpos(char *location,
87         int *rack, int *bay, int *slot, int *slab)
88 {
89         char type;
90
91         /* first scan for an old style geoid string */
92         if (sscanf(location, "%03d%c%02d#%d",
93                 rack, &type, bay, slab) == 4)
94                 *slot = 0; 
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)
98                 return -1; 
99         /* success */
100         return 0;
101 }
102
103 static int sn_hwperf_geoid_to_cnode(char *location)
104 {
105         int cnode;
106         geoid_t geoid;
107         moduleid_t module_id;
108         int rack, bay, slot, slab;
109         int this_rack, this_bay, this_slot, this_slab;
110
111         if (sn_hwperf_location_to_bpos(location, &rack, &bay, &slot, &slab))
112                 return -1;
113
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 = geo_slot(geoid);
120                 this_slab = geo_slab(geoid);
121                 if (rack == this_rack && bay == this_bay &&
122                         slot == this_slot && slab == this_slab) {
123                         break;
124                 }
125         }
126
127         return cnode < numionodes ? cnode : -1;
128 }
129
130 static int sn_hwperf_obj_to_cnode(struct sn_hwperf_object_info * obj)
131 {
132         if (!obj->sn_hwp_this_part)
133                 return -1;
134         return sn_hwperf_geoid_to_cnode(obj->location);
135 }
136
137 static int sn_hwperf_generic_ordinal(struct sn_hwperf_object_info *obj,
138                                 struct sn_hwperf_object_info *objs)
139 {
140         int ordinal;
141         struct sn_hwperf_object_info *p;
142
143         for (ordinal=0, p=objs; p != obj; p++) {
144                 if (SN_HWPERF_FOREIGN(p))
145                         continue;
146                 if (SN_HWPERF_SAME_OBJTYPE(p, obj))
147                         ordinal++;
148         }
149
150         return ordinal;
151 }
152
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 */
157
158 static const char *sn_hwperf_get_slabname(struct sn_hwperf_object_info *obj,
159                         struct sn_hwperf_object_info *objs, int *ordinal)
160 {
161         int isnode;
162         const char *slabname = slabname_other;
163
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);
167         }
168         else {
169                 *ordinal = sn_hwperf_generic_ordinal(obj, objs);
170                 if (SN_HWPERF_IS_ROUTER(obj))
171                         slabname = slabname_router;
172         }
173
174         return slabname;
175 }
176
177 static void print_pci_topology(struct seq_file *s)
178 {
179         char *p;
180         size_t sz;
181         int e;
182
183         for (sz = PAGE_SIZE; sz < 16 * PAGE_SIZE; sz += PAGE_SIZE) {
184                 if (!(p = (char *)kmalloc(sz, GFP_KERNEL)))
185                         break;
186                 e = ia64_sn_ioif_get_pci_topology(__pa(p), sz);
187                 if (e == SALRET_OK)
188                         seq_puts(s, p);
189                 kfree(p);
190                 if (e == SALRET_OK || e == SALRET_NOT_IMPLEMENTED)
191                         break;
192         }
193 }
194
195 static int sn_topology_show(struct seq_file *s, void *d)
196 {
197         int sz;
198         int pt;
199         int e = 0;
200         int i;
201         int j;
202         const char *slabname;
203         int ordinal;
204         cpumask_t cpumask;
205         char slice;
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         u8 shubtype;
212         u8 system_size;
213         u8 sharing_size;
214         u8 partid;
215         u8 coher;
216         u8 nasid_shift;
217         u8 region_size;
218         u16 nasid_mask;
219         int nasid_msb;
220
221         if (obj == objs) {
222                 seq_printf(s, "# sn_topology version 2\n");
223                 seq_printf(s, "# objtype ordinal location partition"
224                         " [attribute value [, ...]]\n");
225
226                 if (ia64_sn_get_sn_info(0,
227                         &shubtype, &nasid_mask, &nasid_shift, &system_size,
228                         &sharing_size, &partid, &coher, &region_size))
229                         BUG();
230                 for (nasid_msb=63; nasid_msb > 0; nasid_msb--) {
231                         if (((u64)nasid_mask << nasid_shift) & (1ULL << nasid_msb))
232                                 break;
233                 }
234                 seq_printf(s, "partition %u %s local "
235                         "shubtype %s, "
236                         "nasid_mask 0x%016lx, "
237                         "nasid_bits %d:%d, "
238                         "system_size %d, "
239                         "sharing_size %d, "
240                         "coherency_domain %d, "
241                         "region_size %d\n",
242
243                         partid, system_utsname.nodename,
244                         shubtype ? "shub2" : "shub1", 
245                         (u64)nasid_mask << nasid_shift, nasid_msb, nasid_shift,
246                         system_size, sharing_size, coher, region_size);
247
248                 print_pci_topology(s);
249         }
250
251         if (SN_HWPERF_FOREIGN(obj)) {
252                 /* private in another partition: not interesting */
253                 return 0;
254         }
255
256         for (i = 0; i < SN_HWPERF_MAXSTRING && obj->name[i]; i++) {
257                 if (obj->name[i] == ' ')
258                         obj->name[i] = '_';
259         }
260
261         slabname = sn_hwperf_get_slabname(obj, objs, &ordinal);
262         seq_printf(s, "%s %d %s %s asic %s", slabname, ordinal, obj->location,
263                 obj->sn_hwp_this_part ? "local" : "shared", obj->name);
264
265         if (!SN_HWPERF_IS_NODE(obj) && !SN_HWPERF_IS_IONODE(obj))
266                 seq_putc(s, '\n');
267         else {
268                 seq_printf(s, ", nasid 0x%x", cnodeid_to_nasid(ordinal));
269                 for (i=0; i < numionodes; i++) {
270                         seq_printf(s, i ? ":%d" : ", dist %d",
271                                 node_distance(ordinal, i));
272                 }
273                 seq_putc(s, '\n');
274
275                 /*
276                  * CPUs on this node, if any
277                  */
278                 cpumask = node_to_cpumask(ordinal);
279                 for_each_online_cpu(i) {
280                         if (cpu_isset(i, cpumask)) {
281                                 slice = 'a' + cpuid_to_slice(i);
282                                 c = cpu_data(i);
283                                 seq_printf(s, "cpu %d %s%c local"
284                                         " freq %luMHz, arch ia64",
285                                         i, obj->location, slice,
286                                         c->proc_freq / 1000000);
287                                 for_each_online_cpu(j) {
288                                         seq_printf(s, j ? ":%d" : ", dist %d",
289                                                 node_distance(
290                                                     cpuid_to_cnodeid(i),
291                                                     cpuid_to_cnodeid(j)));
292                                 }
293                                 seq_putc(s, '\n');
294                         }
295                 }
296         }
297
298         if (obj->ports) {
299                 /*
300                  * numalink ports
301                  */
302                 sz = obj->ports * sizeof(struct sn_hwperf_port_info);
303                 if ((ptdata = vmalloc(sz)) == NULL)
304                         return -ENOMEM;
305                 e = ia64_sn_hwperf_op(sn_hwperf_master_nasid,
306                                       SN_HWPERF_ENUM_PORTS, obj->id, sz,
307                                       (u64) ptdata, 0, 0, NULL);
308                 if (e != SN_HWPERF_OP_OK)
309                         return -EINVAL;
310                 for (ordinal=0, p=objs; p != obj; p++) {
311                         if (!SN_HWPERF_FOREIGN(p))
312                                 ordinal += p->ports;
313                 }
314                 for (pt = 0; pt < obj->ports; pt++) {
315                         for (p = objs, i = 0; i < sn_hwperf_obj_cnt; i++, p++) {
316                                 if (ptdata[pt].conn_id == p->id) {
317                                         break;
318                                 }
319                         }
320                         seq_printf(s, "numalink %d %s-%d",
321                             ordinal+pt, obj->location, ptdata[pt].port);
322
323                         if (i >= sn_hwperf_obj_cnt) {
324                                 /* no connection */
325                                 seq_puts(s, " local endpoint disconnected"
326                                             ", protocol unknown\n");
327                                 continue;
328                         }
329
330                         if (obj->sn_hwp_this_part && p->sn_hwp_this_part)
331                                 /* both ends local to this partition */
332                                 seq_puts(s, " local");
333                         else if (!obj->sn_hwp_this_part && !p->sn_hwp_this_part)
334                                 /* both ends of the link in foreign partiton */
335                                 seq_puts(s, " foreign");
336                         else
337                                 /* link straddles a partition */
338                                 seq_puts(s, " shared");
339
340                         /*
341                          * Unlikely, but strictly should query the LLP config
342                          * registers because an NL4R can be configured to run
343                          * NL3 protocol, even when not talking to an NL3 router.
344                          * Ditto for node-node.
345                          */
346                         seq_printf(s, " endpoint %s-%d, protocol %s\n",
347                                 p->location, ptdata[pt].conn_port,
348                                 (SN_HWPERF_IS_NL3ROUTER(obj) ||
349                                 SN_HWPERF_IS_NL3ROUTER(p)) ?  "LLP3" : "LLP4");
350                 }
351                 vfree(ptdata);
352         }
353
354         return 0;
355 }
356
357 static void *sn_topology_start(struct seq_file *s, loff_t * pos)
358 {
359         struct sn_hwperf_object_info *objs = s->private;
360
361         if (*pos < sn_hwperf_obj_cnt)
362                 return (void *)(objs + *pos);
363
364         return NULL;
365 }
366
367 static void *sn_topology_next(struct seq_file *s, void *v, loff_t * pos)
368 {
369         ++*pos;
370         return sn_topology_start(s, pos);
371 }
372
373 static void sn_topology_stop(struct seq_file *m, void *v)
374 {
375         return;
376 }
377
378 /*
379  * /proc/sgi_sn/sn_topology, read-only using seq_file
380  */
381 static struct seq_operations sn_topology_seq_ops = {
382         .start = sn_topology_start,
383         .next = sn_topology_next,
384         .stop = sn_topology_stop,
385         .show = sn_topology_show
386 };
387
388 struct sn_hwperf_op_info {
389         u64 op;
390         struct sn_hwperf_ioctl_args *a;
391         void *p;
392         int *v0;
393         int ret;
394 };
395
396 static void sn_hwperf_call_sal(void *info)
397 {
398         struct sn_hwperf_op_info *op_info = info;
399         int r;
400
401         r = ia64_sn_hwperf_op(sn_hwperf_master_nasid, op_info->op,
402                       op_info->a->arg, op_info->a->sz,
403                       (u64) op_info->p, 0, 0, op_info->v0);
404         op_info->ret = r;
405 }
406
407 static int sn_hwperf_op_cpu(struct sn_hwperf_op_info *op_info)
408 {
409         u32 cpu;
410         u32 use_ipi;
411         int r = 0;
412         cpumask_t save_allowed;
413         
414         cpu = (op_info->a->arg & SN_HWPERF_ARG_CPU_MASK) >> 32;
415         use_ipi = op_info->a->arg & SN_HWPERF_ARG_USE_IPI_MASK;
416         op_info->a->arg &= SN_HWPERF_ARG_OBJID_MASK;
417
418         if (cpu != SN_HWPERF_ARG_ANY_CPU) {
419                 if (cpu >= num_online_cpus() || !cpu_online(cpu)) {
420                         r = -EINVAL;
421                         goto out;
422                 }
423         }
424
425         if (cpu == SN_HWPERF_ARG_ANY_CPU || cpu == get_cpu()) {
426                 /* don't care, or already on correct cpu */
427                 sn_hwperf_call_sal(op_info);
428         }
429         else {
430                 if (use_ipi) {
431                         /* use an interprocessor interrupt to call SAL */
432                         smp_call_function_single(cpu, sn_hwperf_call_sal,
433                                 op_info, 1, 1);
434                 }
435                 else {
436                         /* migrate the task before calling SAL */ 
437                         save_allowed = current->cpus_allowed;
438                         set_cpus_allowed(current, cpumask_of_cpu(cpu));
439                         sn_hwperf_call_sal(op_info);
440                         set_cpus_allowed(current, save_allowed);
441                 }
442         }
443         r = op_info->ret;
444
445 out:
446         return r;
447 }
448
449 /* map SAL hwperf error code to system error code */
450 static int sn_hwperf_map_err(int hwperf_err)
451 {
452         int e;
453
454         switch(hwperf_err) {
455         case SN_HWPERF_OP_OK:
456                 e = 0;
457                 break;
458
459         case SN_HWPERF_OP_NOMEM:
460                 e = -ENOMEM;
461                 break;
462
463         case SN_HWPERF_OP_NO_PERM:
464                 e = -EPERM;
465                 break;
466
467         case SN_HWPERF_OP_IO_ERROR:
468                 e = -EIO;
469                 break;
470
471         case SN_HWPERF_OP_BUSY:
472                 e = -EBUSY;
473                 break;
474
475         case SN_HWPERF_OP_RECONFIGURE:
476                 e = -EAGAIN;
477                 break;
478
479         case SN_HWPERF_OP_INVAL:
480         default:
481                 e = -EINVAL;
482                 break;
483         }
484
485         return e;
486 }
487
488 /*
489  * ioctl for "sn_hwperf" misc device
490  */
491 static int
492 sn_hwperf_ioctl(struct inode *in, struct file *fp, u32 op, u64 arg)
493 {
494         struct sn_hwperf_ioctl_args a;
495         struct cpuinfo_ia64 *cdata;
496         struct sn_hwperf_object_info *objs;
497         struct sn_hwperf_object_info *cpuobj;
498         struct sn_hwperf_op_info op_info;
499         void *p = NULL;
500         int nobj;
501         char slice;
502         int node;
503         int r;
504         int v0;
505         int i;
506         int j;
507
508         unlock_kernel();
509
510         /* only user requests are allowed here */
511         if ((op & SN_HWPERF_OP_MASK) < 10) {
512                 r = -EINVAL;
513                 goto error;
514         }
515         r = copy_from_user(&a, (const void __user *)arg,
516                 sizeof(struct sn_hwperf_ioctl_args));
517         if (r != 0) {
518                 r = -EFAULT;
519                 goto error;
520         }
521
522         /*
523          * Allocate memory to hold a kernel copy of the user buffer. The
524          * buffer contents are either copied in or out (or both) of user
525          * space depending on the flags encoded in the requested operation.
526          */
527         if (a.ptr) {
528                 p = vmalloc(a.sz);
529                 if (!p) {
530                         r = -ENOMEM;
531                         goto error;
532                 }
533         }
534
535         if (op & SN_HWPERF_OP_MEM_COPYIN) {
536                 r = copy_from_user(p, (const void __user *)a.ptr, a.sz);
537                 if (r != 0) {
538                         r = -EFAULT;
539                         goto error;
540                 }
541         }
542
543         switch (op) {
544         case SN_HWPERF_GET_CPU_INFO:
545                 if (a.sz == sizeof(u64)) {
546                         /* special case to get size needed */
547                         *(u64 *) p = (u64) num_online_cpus() *
548                                 sizeof(struct sn_hwperf_object_info);
549                 } else
550                 if (a.sz < num_online_cpus() * sizeof(struct sn_hwperf_object_info)) {
551                         r = -ENOMEM;
552                         goto error;
553                 } else
554                 if ((r = sn_hwperf_enum_objects(&nobj, &objs)) == 0) {
555                         memset(p, 0, a.sz);
556                         for (i = 0; i < nobj; i++) {
557                                 node = sn_hwperf_obj_to_cnode(objs + i);
558                                 for_each_online_cpu(j) {
559                                         if (node != cpu_to_node(j))
560                                                 continue;
561                                         cpuobj = (struct sn_hwperf_object_info *) p + j;
562                                         slice = 'a' + cpuid_to_slice(j);
563                                         cdata = cpu_data(j);
564                                         cpuobj->id = j;
565                                         snprintf(cpuobj->name,
566                                                  sizeof(cpuobj->name),
567                                                  "CPU %luMHz %s",
568                                                  cdata->proc_freq / 1000000,
569                                                  cdata->vendor);
570                                         snprintf(cpuobj->location,
571                                                  sizeof(cpuobj->location),
572                                                  "%s%c", objs[i].location,
573                                                  slice);
574                                 }
575                         }
576
577                         vfree(objs);
578                 }
579                 break;
580
581         case SN_HWPERF_GET_NODE_NASID:
582                 if (a.sz != sizeof(u64) ||
583                    (node = a.arg) < 0 || node >= numionodes) {
584                         r = -EINVAL;
585                         goto error;
586                 }
587                 *(u64 *)p = (u64)cnodeid_to_nasid(node);
588                 break;
589
590         case SN_HWPERF_GET_OBJ_NODE:
591                 if (a.sz != sizeof(u64) || a.arg < 0) {
592                         r = -EINVAL;
593                         goto error;
594                 }
595                 if ((r = sn_hwperf_enum_objects(&nobj, &objs)) == 0) {
596                         if (a.arg >= nobj) {
597                                 r = -EINVAL;
598                                 vfree(objs);
599                                 goto error;
600                         }
601                         if (objs[(i = a.arg)].id != a.arg) {
602                                 for (i = 0; i < nobj; i++) {
603                                         if (objs[i].id == a.arg)
604                                                 break;
605                                 }
606                         }
607                         if (i == nobj) {
608                                 r = -EINVAL;
609                                 vfree(objs);
610                                 goto error;
611                         }
612                         *(u64 *)p = (u64)sn_hwperf_obj_to_cnode(objs + i);
613                         vfree(objs);
614                 }
615                 break;
616
617         case SN_HWPERF_GET_MMRS:
618         case SN_HWPERF_SET_MMRS:
619         case SN_HWPERF_OBJECT_DISTANCE:
620                 op_info.p = p;
621                 op_info.a = &a;
622                 op_info.v0 = &v0;
623                 op_info.op = op;
624                 r = sn_hwperf_op_cpu(&op_info);
625                 if (r) {
626                         r = sn_hwperf_map_err(r);
627                         a.v0 = v0;
628                         goto error;
629                 }
630                 break;
631
632         default:
633                 /* all other ops are a direct SAL call */
634                 r = ia64_sn_hwperf_op(sn_hwperf_master_nasid, op,
635                               a.arg, a.sz, (u64) p, 0, 0, &v0);
636                 if (r) {
637                         r = sn_hwperf_map_err(r);
638                         goto error;
639                 }
640                 a.v0 = v0;
641                 break;
642         }
643
644         if (op & SN_HWPERF_OP_MEM_COPYOUT) {
645                 r = copy_to_user((void __user *)a.ptr, p, a.sz);
646                 if (r != 0) {
647                         r = -EFAULT;
648                         goto error;
649                 }
650         }
651
652 error:
653         vfree(p);
654
655         lock_kernel();
656         return r;
657 }
658
659 static struct file_operations sn_hwperf_fops = {
660         .ioctl = sn_hwperf_ioctl,
661 };
662
663 static struct miscdevice sn_hwperf_dev = {
664         MISC_DYNAMIC_MINOR,
665         "sn_hwperf",
666         &sn_hwperf_fops
667 };
668
669 static int sn_hwperf_init(void)
670 {
671         u64 v;
672         int salr;
673         int e = 0;
674
675         /* single threaded, once-only initialization */
676         down(&sn_hwperf_init_mutex);
677         if (sn_hwperf_salheap) {
678                 up(&sn_hwperf_init_mutex);
679                 return e;
680         }
681
682         /*
683          * The PROM code needs a fixed reference node. For convenience the
684          * same node as the console I/O is used.
685          */
686         sn_hwperf_master_nasid = (nasid_t) ia64_sn_get_console_nasid();
687
688         /*
689          * Request the needed size and install the PROM scratch area.
690          * The PROM keeps various tracking bits in this memory area.
691          */
692         salr = ia64_sn_hwperf_op(sn_hwperf_master_nasid,
693                                  (u64) SN_HWPERF_GET_HEAPSIZE, 0,
694                                  (u64) sizeof(u64), (u64) &v, 0, 0, NULL);
695         if (salr != SN_HWPERF_OP_OK) {
696                 e = -EINVAL;
697                 goto out;
698         }
699
700         if ((sn_hwperf_salheap = vmalloc(v)) == NULL) {
701                 e = -ENOMEM;
702                 goto out;
703         }
704         salr = ia64_sn_hwperf_op(sn_hwperf_master_nasid,
705                                  SN_HWPERF_INSTALL_HEAP, 0, v,
706                                  (u64) sn_hwperf_salheap, 0, 0, NULL);
707         if (salr != SN_HWPERF_OP_OK) {
708                 e = -EINVAL;
709                 goto out;
710         }
711
712         salr = ia64_sn_hwperf_op(sn_hwperf_master_nasid,
713                                  SN_HWPERF_OBJECT_COUNT, 0,
714                                  sizeof(u64), (u64) &v, 0, 0, NULL);
715         if (salr != SN_HWPERF_OP_OK) {
716                 e = -EINVAL;
717                 goto out;
718         }
719         sn_hwperf_obj_cnt = (int)v;
720
721 out:
722         if (e < 0 && sn_hwperf_salheap) {
723                 vfree(sn_hwperf_salheap);
724                 sn_hwperf_salheap = NULL;
725                 sn_hwperf_obj_cnt = 0;
726         }
727
728         if (!e) {
729                 /*
730                  * Register a dynamic misc device for ioctl. Platforms
731                  * supporting hotplug will create /dev/sn_hwperf, else
732                  * user can to look up the minor number in /proc/misc.
733                  */
734                 if ((e = misc_register(&sn_hwperf_dev)) != 0) {
735                         printk(KERN_ERR "sn_hwperf_init: misc register "
736                                "for \"sn_hwperf\" failed, err %d\n", e);
737                 }
738         }
739
740         up(&sn_hwperf_init_mutex);
741         return e;
742 }
743
744 int sn_topology_open(struct inode *inode, struct file *file)
745 {
746         int e;
747         struct seq_file *seq;
748         struct sn_hwperf_object_info *objbuf;
749         int nobj;
750
751         if ((e = sn_hwperf_enum_objects(&nobj, &objbuf)) == 0) {
752                 e = seq_open(file, &sn_topology_seq_ops);
753                 seq = file->private_data;
754                 seq->private = objbuf;
755         }
756
757         return e;
758 }
759
760 int sn_topology_release(struct inode *inode, struct file *file)
761 {
762         struct seq_file *seq = file->private_data;
763
764         vfree(seq->private);
765         return seq_release(inode, file);
766 }