[IA64-SGI] fix for-loop in sn_hwperf_geoid_to_cnode()
[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-2006 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(KERN_ERR "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         /*
114          * FIXME: replace with cleaner for_each_XXX macro which addresses
115          * both compute and IO nodes once ACPI3.0 is available.
116          */
117         for (cnode = 0; cnode < num_cnodes; cnode++) {
118                 geoid = cnodeid_get_geoid(cnode);
119                 module_id = geo_module(geoid);
120                 this_rack = MODULE_GET_RACK(module_id);
121                 this_bay = MODULE_GET_BPOS(module_id);
122                 this_slot = geo_slot(geoid);
123                 this_slab = geo_slab(geoid);
124                 if (rack == this_rack && bay == this_bay &&
125                         slot == this_slot && slab == this_slab) {
126                         break;
127                 }
128         }
129
130         return node_possible(cnode) ? cnode : -1;
131 }
132
133 static int sn_hwperf_obj_to_cnode(struct sn_hwperf_object_info * obj)
134 {
135         if (!SN_HWPERF_IS_NODE(obj) && !SN_HWPERF_IS_IONODE(obj))
136                 BUG();
137         if (!obj->sn_hwp_this_part)
138                 return -1;
139         return sn_hwperf_geoid_to_cnode(obj->location);
140 }
141
142 static int sn_hwperf_generic_ordinal(struct sn_hwperf_object_info *obj,
143                                 struct sn_hwperf_object_info *objs)
144 {
145         int ordinal;
146         struct sn_hwperf_object_info *p;
147
148         for (ordinal=0, p=objs; p != obj; p++) {
149                 if (SN_HWPERF_FOREIGN(p))
150                         continue;
151                 if (SN_HWPERF_SAME_OBJTYPE(p, obj))
152                         ordinal++;
153         }
154
155         return ordinal;
156 }
157
158 static const char *slabname_node =      "node"; /* SHub asic */
159 static const char *slabname_ionode =    "ionode"; /* TIO asic */
160 static const char *slabname_router =    "router"; /* NL3R or NL4R */
161 static const char *slabname_other =     "other"; /* unknown asic */
162
163 static const char *sn_hwperf_get_slabname(struct sn_hwperf_object_info *obj,
164                         struct sn_hwperf_object_info *objs, int *ordinal)
165 {
166         int isnode;
167         const char *slabname = slabname_other;
168
169         if ((isnode = SN_HWPERF_IS_NODE(obj)) || SN_HWPERF_IS_IONODE(obj)) {
170                 slabname = isnode ? slabname_node : slabname_ionode;
171                 *ordinal = sn_hwperf_obj_to_cnode(obj);
172         }
173         else {
174                 *ordinal = sn_hwperf_generic_ordinal(obj, objs);
175                 if (SN_HWPERF_IS_ROUTER(obj))
176                         slabname = slabname_router;
177         }
178
179         return slabname;
180 }
181
182 static void print_pci_topology(struct seq_file *s)
183 {
184         char *p;
185         size_t sz;
186         int e;
187
188         for (sz = PAGE_SIZE; sz < 16 * PAGE_SIZE; sz += PAGE_SIZE) {
189                 if (!(p = (char *)kmalloc(sz, GFP_KERNEL)))
190                         break;
191                 e = ia64_sn_ioif_get_pci_topology(__pa(p), sz);
192                 if (e == SALRET_OK)
193                         seq_puts(s, p);
194                 kfree(p);
195                 if (e == SALRET_OK || e == SALRET_NOT_IMPLEMENTED)
196                         break;
197         }
198 }
199
200 static inline int sn_hwperf_has_cpus(cnodeid_t node)
201 {
202         return node_online(node) && nr_cpus_node(node);
203 }
204
205 static inline int sn_hwperf_has_mem(cnodeid_t node)
206 {
207         return node_online(node) && NODE_DATA(node)->node_present_pages;
208 }
209
210 static struct sn_hwperf_object_info *
211 sn_hwperf_findobj_id(struct sn_hwperf_object_info *objbuf,
212         int nobj, int id)
213 {
214         int i;
215         struct sn_hwperf_object_info *p = objbuf;
216
217         for (i=0; i < nobj; i++, p++) {
218                 if (p->id == id)
219                         return p;
220         }
221
222         return NULL;
223
224 }
225
226 static int sn_hwperf_get_nearest_node_objdata(struct sn_hwperf_object_info *objbuf,
227         int nobj, cnodeid_t node, cnodeid_t *near_mem_node, cnodeid_t *near_cpu_node)
228 {
229         int e;
230         struct sn_hwperf_object_info *nodeobj = NULL;
231         struct sn_hwperf_object_info *op;
232         struct sn_hwperf_object_info *dest;
233         struct sn_hwperf_object_info *router;
234         struct sn_hwperf_port_info ptdata[16];
235         int sz, i, j;
236         cnodeid_t c;
237         int found_mem = 0;
238         int found_cpu = 0;
239
240         if (!node_possible(node))
241                 return -EINVAL;
242
243         if (sn_hwperf_has_cpus(node)) {
244                 if (near_cpu_node)
245                         *near_cpu_node = node;
246                 found_cpu++;
247         }
248
249         if (sn_hwperf_has_mem(node)) {
250                 if (near_mem_node)
251                         *near_mem_node = node;
252                 found_mem++;
253         }
254
255         if (found_cpu && found_mem)
256                 return 0; /* trivially successful */
257
258         /* find the argument node object */
259         for (i=0, op=objbuf; i < nobj; i++, op++) {
260                 if (!SN_HWPERF_IS_NODE(op) && !SN_HWPERF_IS_IONODE(op))
261                         continue;
262                 if (node == sn_hwperf_obj_to_cnode(op)) {
263                         nodeobj = op;
264                         break;
265                 }
266         }
267         if (!nodeobj) {
268                 e = -ENOENT;
269                 goto err;
270         }
271
272         /* get it's interconnect topology */
273         sz = op->ports * sizeof(struct sn_hwperf_port_info);
274         if (sz > sizeof(ptdata))
275                 BUG();
276         e = ia64_sn_hwperf_op(sn_hwperf_master_nasid,
277                               SN_HWPERF_ENUM_PORTS, nodeobj->id, sz,
278                               (u64)&ptdata, 0, 0, NULL);
279         if (e != SN_HWPERF_OP_OK) {
280                 e = -EINVAL;
281                 goto err;
282         }
283
284         /* find nearest node with cpus and nearest memory */
285         for (router=NULL, j=0; j < op->ports; j++) {
286                 dest = sn_hwperf_findobj_id(objbuf, nobj, ptdata[j].conn_id);
287                 if (!dest || SN_HWPERF_FOREIGN(dest) ||
288                     !SN_HWPERF_IS_NODE(dest) || SN_HWPERF_IS_IONODE(dest)) {
289                         continue;
290                 }
291                 c = sn_hwperf_obj_to_cnode(dest);
292                 if (!found_cpu && sn_hwperf_has_cpus(c)) {
293                         if (near_cpu_node)
294                                 *near_cpu_node = c;
295                         found_cpu++;
296                 }
297                 if (!found_mem && sn_hwperf_has_mem(c)) {
298                         if (near_mem_node)
299                                 *near_mem_node = c;
300                         found_mem++;
301                 }
302                 if (SN_HWPERF_IS_ROUTER(dest))
303                         router = dest;
304         }
305
306         if (router && (!found_cpu || !found_mem)) {
307                 /* search for a node connected to the same router */
308                 sz = router->ports * sizeof(struct sn_hwperf_port_info);
309                 if (sz > sizeof(ptdata))
310                         BUG();
311                 e = ia64_sn_hwperf_op(sn_hwperf_master_nasid,
312                                       SN_HWPERF_ENUM_PORTS, router->id, sz,
313                                       (u64)&ptdata, 0, 0, NULL);
314                 if (e != SN_HWPERF_OP_OK) {
315                         e = -EINVAL;
316                         goto err;
317                 }
318                 for (j=0; j < router->ports; j++) {
319                         dest = sn_hwperf_findobj_id(objbuf, nobj,
320                                 ptdata[j].conn_id);
321                         if (!dest || dest->id == node ||
322                             SN_HWPERF_FOREIGN(dest) ||
323                             !SN_HWPERF_IS_NODE(dest) ||
324                             SN_HWPERF_IS_IONODE(dest)) {
325                                 continue;
326                         }
327                         c = sn_hwperf_obj_to_cnode(dest);
328                         if (!found_cpu && sn_hwperf_has_cpus(c)) {
329                                 if (near_cpu_node)
330                                         *near_cpu_node = c;
331                                 found_cpu++;
332                         }
333                         if (!found_mem && sn_hwperf_has_mem(c)) {
334                                 if (near_mem_node)
335                                         *near_mem_node = c;
336                                 found_mem++;
337                         }
338                         if (found_cpu && found_mem)
339                                 break;
340                 }
341         }
342
343         if (!found_cpu || !found_mem) {
344                 /* resort to _any_ node with CPUs and memory */
345                 for (i=0, op=objbuf; i < nobj; i++, op++) {
346                         if (SN_HWPERF_FOREIGN(op) ||
347                             SN_HWPERF_IS_IONODE(op) ||
348                             !SN_HWPERF_IS_NODE(op)) {
349                                 continue;
350                         }
351                         c = sn_hwperf_obj_to_cnode(op);
352                         if (!found_cpu && sn_hwperf_has_cpus(c)) {
353                                 if (near_cpu_node)
354                                         *near_cpu_node = c;
355                                 found_cpu++;
356                         }
357                         if (!found_mem && sn_hwperf_has_mem(c)) {
358                                 if (near_mem_node)
359                                         *near_mem_node = c;
360                                 found_mem++;
361                         }
362                         if (found_cpu && found_mem)
363                                 break;
364                 }
365         }
366
367         if (!found_cpu || !found_mem)
368                 e = -ENODATA;
369
370 err:
371         return e;
372 }
373
374
375 static int sn_topology_show(struct seq_file *s, void *d)
376 {
377         int sz;
378         int pt;
379         int e = 0;
380         int i;
381         int j;
382         const char *slabname;
383         int ordinal;
384         cpumask_t cpumask;
385         char slice;
386         struct cpuinfo_ia64 *c;
387         struct sn_hwperf_port_info *ptdata;
388         struct sn_hwperf_object_info *p;
389         struct sn_hwperf_object_info *obj = d;  /* this object */
390         struct sn_hwperf_object_info *objs = s->private; /* all objects */
391         u8 shubtype;
392         u8 system_size;
393         u8 sharing_size;
394         u8 partid;
395         u8 coher;
396         u8 nasid_shift;
397         u8 region_size;
398         u16 nasid_mask;
399         int nasid_msb;
400
401         if (obj == objs) {
402                 seq_printf(s, "# sn_topology version 2\n");
403                 seq_printf(s, "# objtype ordinal location partition"
404                         " [attribute value [, ...]]\n");
405
406                 if (ia64_sn_get_sn_info(0,
407                         &shubtype, &nasid_mask, &nasid_shift, &system_size,
408                         &sharing_size, &partid, &coher, &region_size))
409                         BUG();
410                 for (nasid_msb=63; nasid_msb > 0; nasid_msb--) {
411                         if (((u64)nasid_mask << nasid_shift) & (1ULL << nasid_msb))
412                                 break;
413                 }
414                 seq_printf(s, "partition %u %s local "
415                         "shubtype %s, "
416                         "nasid_mask 0x%016lx, "
417                         "nasid_bits %d:%d, "
418                         "system_size %d, "
419                         "sharing_size %d, "
420                         "coherency_domain %d, "
421                         "region_size %d\n",
422
423                         partid, system_utsname.nodename,
424                         shubtype ? "shub2" : "shub1", 
425                         (u64)nasid_mask << nasid_shift, nasid_msb, nasid_shift,
426                         system_size, sharing_size, coher, region_size);
427
428                 print_pci_topology(s);
429         }
430
431         if (SN_HWPERF_FOREIGN(obj)) {
432                 /* private in another partition: not interesting */
433                 return 0;
434         }
435
436         for (i = 0; i < SN_HWPERF_MAXSTRING && obj->name[i]; i++) {
437                 if (obj->name[i] == ' ')
438                         obj->name[i] = '_';
439         }
440
441         slabname = sn_hwperf_get_slabname(obj, objs, &ordinal);
442         seq_printf(s, "%s %d %s %s asic %s", slabname, ordinal, obj->location,
443                 obj->sn_hwp_this_part ? "local" : "shared", obj->name);
444
445         if (!SN_HWPERF_IS_NODE(obj) && !SN_HWPERF_IS_IONODE(obj))
446                 seq_putc(s, '\n');
447         else {
448                 cnodeid_t near_mem = -1;
449                 cnodeid_t near_cpu = -1;
450
451                 seq_printf(s, ", nasid 0x%x", cnodeid_to_nasid(ordinal));
452
453                 if (sn_hwperf_get_nearest_node_objdata(objs, sn_hwperf_obj_cnt,
454                         ordinal, &near_mem, &near_cpu) == 0) {
455                         seq_printf(s, ", near_mem_nodeid %d, near_cpu_nodeid %d",
456                                 near_mem, near_cpu);
457                 }
458
459                 if (!SN_HWPERF_IS_IONODE(obj)) {
460                         for_each_online_node(i) {
461                                 seq_printf(s, i ? ":%d" : ", dist %d",
462                                         node_distance(ordinal, i));
463                         }
464                 }
465
466                 seq_putc(s, '\n');
467
468                 /*
469                  * CPUs on this node, if any
470                  */
471                 cpumask = node_to_cpumask(ordinal);
472                 for_each_online_cpu(i) {
473                         if (cpu_isset(i, cpumask)) {
474                                 slice = 'a' + cpuid_to_slice(i);
475                                 c = cpu_data(i);
476                                 seq_printf(s, "cpu %d %s%c local"
477                                         " freq %luMHz, arch ia64",
478                                         i, obj->location, slice,
479                                         c->proc_freq / 1000000);
480                                 for_each_online_cpu(j) {
481                                         seq_printf(s, j ? ":%d" : ", dist %d",
482                                                 node_distance(
483                                                     cpu_to_node(i),
484                                                     cpu_to_node(j)));
485                                 }
486                                 seq_putc(s, '\n');
487                         }
488                 }
489         }
490
491         if (obj->ports) {
492                 /*
493                  * numalink ports
494                  */
495                 sz = obj->ports * sizeof(struct sn_hwperf_port_info);
496                 if ((ptdata = vmalloc(sz)) == NULL)
497                         return -ENOMEM;
498                 e = ia64_sn_hwperf_op(sn_hwperf_master_nasid,
499                                       SN_HWPERF_ENUM_PORTS, obj->id, sz,
500                                       (u64) ptdata, 0, 0, NULL);
501                 if (e != SN_HWPERF_OP_OK)
502                         return -EINVAL;
503                 for (ordinal=0, p=objs; p != obj; p++) {
504                         if (!SN_HWPERF_FOREIGN(p))
505                                 ordinal += p->ports;
506                 }
507                 for (pt = 0; pt < obj->ports; pt++) {
508                         for (p = objs, i = 0; i < sn_hwperf_obj_cnt; i++, p++) {
509                                 if (ptdata[pt].conn_id == p->id) {
510                                         break;
511                                 }
512                         }
513                         seq_printf(s, "numalink %d %s-%d",
514                             ordinal+pt, obj->location, ptdata[pt].port);
515
516                         if (i >= sn_hwperf_obj_cnt) {
517                                 /* no connection */
518                                 seq_puts(s, " local endpoint disconnected"
519                                             ", protocol unknown\n");
520                                 continue;
521                         }
522
523                         if (obj->sn_hwp_this_part && p->sn_hwp_this_part)
524                                 /* both ends local to this partition */
525                                 seq_puts(s, " local");
526                         else if (!obj->sn_hwp_this_part && !p->sn_hwp_this_part)
527                                 /* both ends of the link in foreign partiton */
528                                 seq_puts(s, " foreign");
529                         else
530                                 /* link straddles a partition */
531                                 seq_puts(s, " shared");
532
533                         /*
534                          * Unlikely, but strictly should query the LLP config
535                          * registers because an NL4R can be configured to run
536                          * NL3 protocol, even when not talking to an NL3 router.
537                          * Ditto for node-node.
538                          */
539                         seq_printf(s, " endpoint %s-%d, protocol %s\n",
540                                 p->location, ptdata[pt].conn_port,
541                                 (SN_HWPERF_IS_NL3ROUTER(obj) ||
542                                 SN_HWPERF_IS_NL3ROUTER(p)) ?  "LLP3" : "LLP4");
543                 }
544                 vfree(ptdata);
545         }
546
547         return 0;
548 }
549
550 static void *sn_topology_start(struct seq_file *s, loff_t * pos)
551 {
552         struct sn_hwperf_object_info *objs = s->private;
553
554         if (*pos < sn_hwperf_obj_cnt)
555                 return (void *)(objs + *pos);
556
557         return NULL;
558 }
559
560 static void *sn_topology_next(struct seq_file *s, void *v, loff_t * pos)
561 {
562         ++*pos;
563         return sn_topology_start(s, pos);
564 }
565
566 static void sn_topology_stop(struct seq_file *m, void *v)
567 {
568         return;
569 }
570
571 /*
572  * /proc/sgi_sn/sn_topology, read-only using seq_file
573  */
574 static struct seq_operations sn_topology_seq_ops = {
575         .start = sn_topology_start,
576         .next = sn_topology_next,
577         .stop = sn_topology_stop,
578         .show = sn_topology_show
579 };
580
581 struct sn_hwperf_op_info {
582         u64 op;
583         struct sn_hwperf_ioctl_args *a;
584         void *p;
585         int *v0;
586         int ret;
587 };
588
589 static void sn_hwperf_call_sal(void *info)
590 {
591         struct sn_hwperf_op_info *op_info = info;
592         int r;
593
594         r = ia64_sn_hwperf_op(sn_hwperf_master_nasid, op_info->op,
595                       op_info->a->arg, op_info->a->sz,
596                       (u64) op_info->p, 0, 0, op_info->v0);
597         op_info->ret = r;
598 }
599
600 static int sn_hwperf_op_cpu(struct sn_hwperf_op_info *op_info)
601 {
602         u32 cpu;
603         u32 use_ipi;
604         int r = 0;
605         cpumask_t save_allowed;
606         
607         cpu = (op_info->a->arg & SN_HWPERF_ARG_CPU_MASK) >> 32;
608         use_ipi = op_info->a->arg & SN_HWPERF_ARG_USE_IPI_MASK;
609         op_info->a->arg &= SN_HWPERF_ARG_OBJID_MASK;
610
611         if (cpu != SN_HWPERF_ARG_ANY_CPU) {
612                 if (cpu >= NR_CPUS || !cpu_online(cpu)) {
613                         r = -EINVAL;
614                         goto out;
615                 }
616         }
617
618         if (cpu == SN_HWPERF_ARG_ANY_CPU || cpu == get_cpu()) {
619                 /* don't care, or already on correct cpu */
620                 sn_hwperf_call_sal(op_info);
621         }
622         else {
623                 if (use_ipi) {
624                         /* use an interprocessor interrupt to call SAL */
625                         smp_call_function_single(cpu, sn_hwperf_call_sal,
626                                 op_info, 1, 1);
627                 }
628                 else {
629                         /* migrate the task before calling SAL */ 
630                         save_allowed = current->cpus_allowed;
631                         set_cpus_allowed(current, cpumask_of_cpu(cpu));
632                         sn_hwperf_call_sal(op_info);
633                         set_cpus_allowed(current, save_allowed);
634                 }
635         }
636         r = op_info->ret;
637
638 out:
639         return r;
640 }
641
642 /* map SAL hwperf error code to system error code */
643 static int sn_hwperf_map_err(int hwperf_err)
644 {
645         int e;
646
647         switch(hwperf_err) {
648         case SN_HWPERF_OP_OK:
649                 e = 0;
650                 break;
651
652         case SN_HWPERF_OP_NOMEM:
653                 e = -ENOMEM;
654                 break;
655
656         case SN_HWPERF_OP_NO_PERM:
657                 e = -EPERM;
658                 break;
659
660         case SN_HWPERF_OP_IO_ERROR:
661                 e = -EIO;
662                 break;
663
664         case SN_HWPERF_OP_BUSY:
665                 e = -EBUSY;
666                 break;
667
668         case SN_HWPERF_OP_RECONFIGURE:
669                 e = -EAGAIN;
670                 break;
671
672         case SN_HWPERF_OP_INVAL:
673         default:
674                 e = -EINVAL;
675                 break;
676         }
677
678         return e;
679 }
680
681 /*
682  * ioctl for "sn_hwperf" misc device
683  */
684 static int
685 sn_hwperf_ioctl(struct inode *in, struct file *fp, u32 op, u64 arg)
686 {
687         struct sn_hwperf_ioctl_args a;
688         struct cpuinfo_ia64 *cdata;
689         struct sn_hwperf_object_info *objs;
690         struct sn_hwperf_object_info *cpuobj;
691         struct sn_hwperf_op_info op_info;
692         void *p = NULL;
693         int nobj;
694         char slice;
695         int node;
696         int r;
697         int v0;
698         int i;
699         int j;
700
701         unlock_kernel();
702
703         /* only user requests are allowed here */
704         if ((op & SN_HWPERF_OP_MASK) < 10) {
705                 r = -EINVAL;
706                 goto error;
707         }
708         r = copy_from_user(&a, (const void __user *)arg,
709                 sizeof(struct sn_hwperf_ioctl_args));
710         if (r != 0) {
711                 r = -EFAULT;
712                 goto error;
713         }
714
715         /*
716          * Allocate memory to hold a kernel copy of the user buffer. The
717          * buffer contents are either copied in or out (or both) of user
718          * space depending on the flags encoded in the requested operation.
719          */
720         if (a.ptr) {
721                 p = vmalloc(a.sz);
722                 if (!p) {
723                         r = -ENOMEM;
724                         goto error;
725                 }
726         }
727
728         if (op & SN_HWPERF_OP_MEM_COPYIN) {
729                 r = copy_from_user(p, (const void __user *)a.ptr, a.sz);
730                 if (r != 0) {
731                         r = -EFAULT;
732                         goto error;
733                 }
734         }
735
736         switch (op) {
737         case SN_HWPERF_GET_CPU_INFO:
738                 if (a.sz == sizeof(u64)) {
739                         /* special case to get size needed */
740                         *(u64 *) p = (u64) num_online_cpus() *
741                                 sizeof(struct sn_hwperf_object_info);
742                 } else
743                 if (a.sz < num_online_cpus() * sizeof(struct sn_hwperf_object_info)) {
744                         r = -ENOMEM;
745                         goto error;
746                 } else
747                 if ((r = sn_hwperf_enum_objects(&nobj, &objs)) == 0) {
748                         memset(p, 0, a.sz);
749                         for (i = 0; i < nobj; i++) {
750                                 int cpuobj_index = 0;
751                                 if (!SN_HWPERF_IS_NODE(objs + i))
752                                         continue;
753                                 node = sn_hwperf_obj_to_cnode(objs + i);
754                                 for_each_online_cpu(j) {
755                                         if (node != cpu_to_node(j))
756                                                 continue;
757                                         cpuobj = (struct sn_hwperf_object_info *) p + cpuobj_index++;
758                                         slice = 'a' + cpuid_to_slice(j);
759                                         cdata = cpu_data(j);
760                                         cpuobj->id = j;
761                                         snprintf(cpuobj->name,
762                                                  sizeof(cpuobj->name),
763                                                  "CPU %luMHz %s",
764                                                  cdata->proc_freq / 1000000,
765                                                  cdata->vendor);
766                                         snprintf(cpuobj->location,
767                                                  sizeof(cpuobj->location),
768                                                  "%s%c", objs[i].location,
769                                                  slice);
770                                 }
771                         }
772
773                         vfree(objs);
774                 }
775                 break;
776
777         case SN_HWPERF_GET_NODE_NASID:
778                 if (a.sz != sizeof(u64) ||
779                    (node = a.arg) < 0 || !node_possible(node)) {
780                         r = -EINVAL;
781                         goto error;
782                 }
783                 *(u64 *)p = (u64)cnodeid_to_nasid(node);
784                 break;
785
786         case SN_HWPERF_GET_OBJ_NODE:
787                 if (a.sz != sizeof(u64) || a.arg < 0) {
788                         r = -EINVAL;
789                         goto error;
790                 }
791                 if ((r = sn_hwperf_enum_objects(&nobj, &objs)) == 0) {
792                         if (a.arg >= nobj) {
793                                 r = -EINVAL;
794                                 vfree(objs);
795                                 goto error;
796                         }
797                         if (objs[(i = a.arg)].id != a.arg) {
798                                 for (i = 0; i < nobj; i++) {
799                                         if (objs[i].id == a.arg)
800                                                 break;
801                                 }
802                         }
803                         if (i == nobj) {
804                                 r = -EINVAL;
805                                 vfree(objs);
806                                 goto error;
807                         }
808
809                         if (!SN_HWPERF_IS_NODE(objs + i) &&
810                             !SN_HWPERF_IS_IONODE(objs + i)) {
811                                 r = -ENOENT;
812                                 vfree(objs);
813                                 goto error;
814                         }
815
816                         *(u64 *)p = (u64)sn_hwperf_obj_to_cnode(objs + i);
817                         vfree(objs);
818                 }
819                 break;
820
821         case SN_HWPERF_GET_MMRS:
822         case SN_HWPERF_SET_MMRS:
823         case SN_HWPERF_OBJECT_DISTANCE:
824                 op_info.p = p;
825                 op_info.a = &a;
826                 op_info.v0 = &v0;
827                 op_info.op = op;
828                 r = sn_hwperf_op_cpu(&op_info);
829                 if (r) {
830                         r = sn_hwperf_map_err(r);
831                         a.v0 = v0;
832                         goto error;
833                 }
834                 break;
835
836         default:
837                 /* all other ops are a direct SAL call */
838                 r = ia64_sn_hwperf_op(sn_hwperf_master_nasid, op,
839                               a.arg, a.sz, (u64) p, 0, 0, &v0);
840                 if (r) {
841                         r = sn_hwperf_map_err(r);
842                         goto error;
843                 }
844                 a.v0 = v0;
845                 break;
846         }
847
848         if (op & SN_HWPERF_OP_MEM_COPYOUT) {
849                 r = copy_to_user((void __user *)a.ptr, p, a.sz);
850                 if (r != 0) {
851                         r = -EFAULT;
852                         goto error;
853                 }
854         }
855
856 error:
857         vfree(p);
858
859         lock_kernel();
860         return r;
861 }
862
863 static struct file_operations sn_hwperf_fops = {
864         .ioctl = sn_hwperf_ioctl,
865 };
866
867 static struct miscdevice sn_hwperf_dev = {
868         MISC_DYNAMIC_MINOR,
869         "sn_hwperf",
870         &sn_hwperf_fops
871 };
872
873 static int sn_hwperf_init(void)
874 {
875         u64 v;
876         int salr;
877         int e = 0;
878
879         /* single threaded, once-only initialization */
880         down(&sn_hwperf_init_mutex);
881
882         if (sn_hwperf_salheap) {
883                 up(&sn_hwperf_init_mutex);
884                 return e;
885         }
886
887         /*
888          * The PROM code needs a fixed reference node. For convenience the
889          * same node as the console I/O is used.
890          */
891         sn_hwperf_master_nasid = (nasid_t) ia64_sn_get_console_nasid();
892
893         /*
894          * Request the needed size and install the PROM scratch area.
895          * The PROM keeps various tracking bits in this memory area.
896          */
897         salr = ia64_sn_hwperf_op(sn_hwperf_master_nasid,
898                                  (u64) SN_HWPERF_GET_HEAPSIZE, 0,
899                                  (u64) sizeof(u64), (u64) &v, 0, 0, NULL);
900         if (salr != SN_HWPERF_OP_OK) {
901                 e = -EINVAL;
902                 goto out;
903         }
904
905         if ((sn_hwperf_salheap = vmalloc(v)) == NULL) {
906                 e = -ENOMEM;
907                 goto out;
908         }
909         salr = ia64_sn_hwperf_op(sn_hwperf_master_nasid,
910                                  SN_HWPERF_INSTALL_HEAP, 0, v,
911                                  (u64) sn_hwperf_salheap, 0, 0, NULL);
912         if (salr != SN_HWPERF_OP_OK) {
913                 e = -EINVAL;
914                 goto out;
915         }
916
917         salr = ia64_sn_hwperf_op(sn_hwperf_master_nasid,
918                                  SN_HWPERF_OBJECT_COUNT, 0,
919                                  sizeof(u64), (u64) &v, 0, 0, NULL);
920         if (salr != SN_HWPERF_OP_OK) {
921                 e = -EINVAL;
922                 goto out;
923         }
924         sn_hwperf_obj_cnt = (int)v;
925
926 out:
927         if (e < 0 && sn_hwperf_salheap) {
928                 vfree(sn_hwperf_salheap);
929                 sn_hwperf_salheap = NULL;
930                 sn_hwperf_obj_cnt = 0;
931         }
932         up(&sn_hwperf_init_mutex);
933         return e;
934 }
935
936 int sn_topology_open(struct inode *inode, struct file *file)
937 {
938         int e;
939         struct seq_file *seq;
940         struct sn_hwperf_object_info *objbuf;
941         int nobj;
942
943         if ((e = sn_hwperf_enum_objects(&nobj, &objbuf)) == 0) {
944                 e = seq_open(file, &sn_topology_seq_ops);
945                 seq = file->private_data;
946                 seq->private = objbuf;
947         }
948
949         return e;
950 }
951
952 int sn_topology_release(struct inode *inode, struct file *file)
953 {
954         struct seq_file *seq = file->private_data;
955
956         vfree(seq->private);
957         return seq_release(inode, file);
958 }
959
960 int sn_hwperf_get_nearest_node(cnodeid_t node,
961         cnodeid_t *near_mem_node, cnodeid_t *near_cpu_node)
962 {
963         int e;
964         int nobj;
965         struct sn_hwperf_object_info *objbuf;
966
967         if ((e = sn_hwperf_enum_objects(&nobj, &objbuf)) == 0) {
968                 e = sn_hwperf_get_nearest_node_objdata(objbuf, nobj,
969                         node, near_mem_node, near_cpu_node);
970                 vfree(objbuf);
971         }
972
973         return e;
974 }
975
976 static int __devinit sn_hwperf_misc_register_init(void)
977 {
978         int e;
979
980         if (!ia64_platform_is("sn2"))
981                 return 0;
982
983         sn_hwperf_init();
984
985         /*
986          * Register a dynamic misc device for hwperf ioctls. Platforms
987          * supporting hotplug will create /dev/sn_hwperf, else user
988          * can to look up the minor number in /proc/misc.
989          */
990         if ((e = misc_register(&sn_hwperf_dev)) != 0) {
991                 printk(KERN_ERR "sn_hwperf_misc_register_init: failed to "
992                 "register misc device for \"%s\"\n", sn_hwperf_dev.name);
993         }
994
995         return e;
996 }
997
998 device_initcall(sn_hwperf_misc_register_init); /* after misc_init() */
999 EXPORT_SYMBOL(sn_hwperf_get_nearest_node);