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