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