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