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