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