Merge branch 'topic/asoc' into for-linus
[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         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                         for_each_cpu_and(i, cpu_online_mask,
476                                          cpumask_of_node(ordinal)) {
477                                 slice = 'a' + cpuid_to_slice(i);
478                                 c = cpu_data(i);
479                                 seq_printf(s, "cpu %d %s%c local"
480                                            " freq %luMHz, arch ia64",
481                                            i, obj->location, slice,
482                                            c->proc_freq / 1000000);
483                                 for_each_online_cpu(j) {
484                                         seq_printf(s, j ? ":%d" : ", dist %d",
485                                                    node_distance(
486                                                         cpu_to_node(i),
487                                                         cpu_to_node(j)));
488                                 }
489                                 seq_putc(s, '\n');
490                         }
491                 }
492         }
493
494         if (obj->ports) {
495                 /*
496                  * numalink ports
497                  */
498                 sz = obj->ports * sizeof(struct sn_hwperf_port_info);
499                 if ((ptdata = kmalloc(sz, GFP_KERNEL)) == NULL)
500                         return -ENOMEM;
501                 e = ia64_sn_hwperf_op(sn_hwperf_master_nasid,
502                                       SN_HWPERF_ENUM_PORTS, obj->id, sz,
503                                       (u64) ptdata, 0, 0, NULL);
504                 if (e != SN_HWPERF_OP_OK)
505                         return -EINVAL;
506                 for (ordinal=0, p=objs; p != obj; p++) {
507                         if (!SN_HWPERF_FOREIGN(p))
508                                 ordinal += p->ports;
509                 }
510                 for (pt = 0; pt < obj->ports; pt++) {
511                         for (p = objs, i = 0; i < sn_hwperf_obj_cnt; i++, p++) {
512                                 if (ptdata[pt].conn_id == p->id) {
513                                         break;
514                                 }
515                         }
516                         seq_printf(s, "numalink %d %s-%d",
517                             ordinal+pt, obj->location, ptdata[pt].port);
518
519                         if (i >= sn_hwperf_obj_cnt) {
520                                 /* no connection */
521                                 seq_puts(s, " local endpoint disconnected"
522                                             ", protocol unknown\n");
523                                 continue;
524                         }
525
526                         if (obj->sn_hwp_this_part && p->sn_hwp_this_part)
527                                 /* both ends local to this partition */
528                                 seq_puts(s, " local");
529                         else if (SN_HWPERF_FOREIGN(p))
530                                 /* both ends of the link in foreign partiton */
531                                 seq_puts(s, " foreign");
532                         else
533                                 /* link straddles a partition */
534                                 seq_puts(s, " shared");
535
536                         /*
537                          * Unlikely, but strictly should query the LLP config
538                          * registers because an NL4R can be configured to run
539                          * NL3 protocol, even when not talking to an NL3 router.
540                          * Ditto for node-node.
541                          */
542                         seq_printf(s, " endpoint %s-%d, protocol %s\n",
543                                 p->location, ptdata[pt].conn_port,
544                                 (SN_HWPERF_IS_NL3ROUTER(obj) ||
545                                 SN_HWPERF_IS_NL3ROUTER(p)) ?  "LLP3" : "LLP4");
546                 }
547                 kfree(ptdata);
548         }
549
550         return 0;
551 }
552
553 static void *sn_topology_start(struct seq_file *s, loff_t * pos)
554 {
555         struct sn_hwperf_object_info *objs = s->private;
556
557         if (*pos < sn_hwperf_obj_cnt)
558                 return (void *)(objs + *pos);
559
560         return NULL;
561 }
562
563 static void *sn_topology_next(struct seq_file *s, void *v, loff_t * pos)
564 {
565         ++*pos;
566         return sn_topology_start(s, pos);
567 }
568
569 static void sn_topology_stop(struct seq_file *m, void *v)
570 {
571         return;
572 }
573
574 /*
575  * /proc/sgi_sn/sn_topology, read-only using seq_file
576  */
577 static const struct seq_operations sn_topology_seq_ops = {
578         .start = sn_topology_start,
579         .next = sn_topology_next,
580         .stop = sn_topology_stop,
581         .show = sn_topology_show
582 };
583
584 struct sn_hwperf_op_info {
585         u64 op;
586         struct sn_hwperf_ioctl_args *a;
587         void *p;
588         int *v0;
589         int ret;
590 };
591
592 static void sn_hwperf_call_sal(void *info)
593 {
594         struct sn_hwperf_op_info *op_info = info;
595         int r;
596
597         r = ia64_sn_hwperf_op(sn_hwperf_master_nasid, op_info->op,
598                       op_info->a->arg, op_info->a->sz,
599                       (u64) op_info->p, 0, 0, op_info->v0);
600         op_info->ret = r;
601 }
602
603 static int sn_hwperf_op_cpu(struct sn_hwperf_op_info *op_info)
604 {
605         u32 cpu;
606         u32 use_ipi;
607         int r = 0;
608         cpumask_t save_allowed;
609         
610         cpu = (op_info->a->arg & SN_HWPERF_ARG_CPU_MASK) >> 32;
611         use_ipi = op_info->a->arg & SN_HWPERF_ARG_USE_IPI_MASK;
612         op_info->a->arg &= SN_HWPERF_ARG_OBJID_MASK;
613
614         if (cpu != SN_HWPERF_ARG_ANY_CPU) {
615                 if (cpu >= NR_CPUS || !cpu_online(cpu)) {
616                         r = -EINVAL;
617                         goto out;
618                 }
619         }
620
621         if (cpu == SN_HWPERF_ARG_ANY_CPU || cpu == get_cpu()) {
622                 /* don't care, or already on correct cpu */
623                 sn_hwperf_call_sal(op_info);
624         }
625         else {
626                 if (use_ipi) {
627                         /* use an interprocessor interrupt to call SAL */
628                         smp_call_function_single(cpu, sn_hwperf_call_sal,
629                                 op_info, 1);
630                 }
631                 else {
632                         /* migrate the task before calling SAL */ 
633                         save_allowed = current->cpus_allowed;
634                         set_cpus_allowed(current, cpumask_of_cpu(cpu));
635                         sn_hwperf_call_sal(op_info);
636                         set_cpus_allowed(current, save_allowed);
637                 }
638         }
639         r = op_info->ret;
640
641 out:
642         return r;
643 }
644
645 /* map SAL hwperf error code to system error code */
646 static int sn_hwperf_map_err(int hwperf_err)
647 {
648         int e;
649
650         switch(hwperf_err) {
651         case SN_HWPERF_OP_OK:
652                 e = 0;
653                 break;
654
655         case SN_HWPERF_OP_NOMEM:
656                 e = -ENOMEM;
657                 break;
658
659         case SN_HWPERF_OP_NO_PERM:
660                 e = -EPERM;
661                 break;
662
663         case SN_HWPERF_OP_IO_ERROR:
664                 e = -EIO;
665                 break;
666
667         case SN_HWPERF_OP_BUSY:
668                 e = -EBUSY;
669                 break;
670
671         case SN_HWPERF_OP_RECONFIGURE:
672                 e = -EAGAIN;
673                 break;
674
675         case SN_HWPERF_OP_INVAL:
676         default:
677                 e = -EINVAL;
678                 break;
679         }
680
681         return e;
682 }
683
684 /*
685  * ioctl for "sn_hwperf" misc device
686  */
687 static int
688 sn_hwperf_ioctl(struct inode *in, struct file *fp, u32 op, u64 arg)
689 {
690         struct sn_hwperf_ioctl_args a;
691         struct cpuinfo_ia64 *cdata;
692         struct sn_hwperf_object_info *objs;
693         struct sn_hwperf_object_info *cpuobj;
694         struct sn_hwperf_op_info op_info;
695         void *p = NULL;
696         int nobj;
697         char slice;
698         int node;
699         int r;
700         int v0;
701         int i;
702         int j;
703
704         unlock_kernel();
705
706         /* only user requests are allowed here */
707         if ((op & SN_HWPERF_OP_MASK) < 10) {
708                 r = -EINVAL;
709                 goto error;
710         }
711         r = copy_from_user(&a, (const void __user *)arg,
712                 sizeof(struct sn_hwperf_ioctl_args));
713         if (r != 0) {
714                 r = -EFAULT;
715                 goto error;
716         }
717
718         /*
719          * Allocate memory to hold a kernel copy of the user buffer. The
720          * buffer contents are either copied in or out (or both) of user
721          * space depending on the flags encoded in the requested operation.
722          */
723         if (a.ptr) {
724                 p = vmalloc(a.sz);
725                 if (!p) {
726                         r = -ENOMEM;
727                         goto error;
728                 }
729         }
730
731         if (op & SN_HWPERF_OP_MEM_COPYIN) {
732                 r = copy_from_user(p, (const void __user *)a.ptr, a.sz);
733                 if (r != 0) {
734                         r = -EFAULT;
735                         goto error;
736                 }
737         }
738
739         switch (op) {
740         case SN_HWPERF_GET_CPU_INFO:
741                 if (a.sz == sizeof(u64)) {
742                         /* special case to get size needed */
743                         *(u64 *) p = (u64) num_online_cpus() *
744                                 sizeof(struct sn_hwperf_object_info);
745                 } else
746                 if (a.sz < num_online_cpus() * sizeof(struct sn_hwperf_object_info)) {
747                         r = -ENOMEM;
748                         goto error;
749                 } else
750                 if ((r = sn_hwperf_enum_objects(&nobj, &objs)) == 0) {
751                         int cpuobj_index = 0;
752
753                         memset(p, 0, a.sz);
754                         for (i = 0; i < nobj; i++) {
755                                 if (!SN_HWPERF_IS_NODE(objs + i))
756                                         continue;
757                                 node = sn_hwperf_obj_to_cnode(objs + i);
758                                 for_each_online_cpu(j) {
759                                         if (node != cpu_to_node(j))
760                                                 continue;
761                                         cpuobj = (struct sn_hwperf_object_info *) p + cpuobj_index++;
762                                         slice = 'a' + cpuid_to_slice(j);
763                                         cdata = cpu_data(j);
764                                         cpuobj->id = j;
765                                         snprintf(cpuobj->name,
766                                                  sizeof(cpuobj->name),
767                                                  "CPU %luMHz %s",
768                                                  cdata->proc_freq / 1000000,
769                                                  cdata->vendor);
770                                         snprintf(cpuobj->location,
771                                                  sizeof(cpuobj->location),
772                                                  "%s%c", objs[i].location,
773                                                  slice);
774                                 }
775                         }
776
777                         vfree(objs);
778                 }
779                 break;
780
781         case SN_HWPERF_GET_NODE_NASID:
782                 if (a.sz != sizeof(u64) ||
783                    (node = a.arg) < 0 || !cnode_possible(node)) {
784                         r = -EINVAL;
785                         goto error;
786                 }
787                 *(u64 *)p = (u64)cnodeid_to_nasid(node);
788                 break;
789
790         case SN_HWPERF_GET_OBJ_NODE:
791                 if (a.sz != sizeof(u64) || a.arg < 0) {
792                         r = -EINVAL;
793                         goto error;
794                 }
795                 if ((r = sn_hwperf_enum_objects(&nobj, &objs)) == 0) {
796                         if (a.arg >= nobj) {
797                                 r = -EINVAL;
798                                 vfree(objs);
799                                 goto error;
800                         }
801                         if (objs[(i = a.arg)].id != a.arg) {
802                                 for (i = 0; i < nobj; i++) {
803                                         if (objs[i].id == a.arg)
804                                                 break;
805                                 }
806                         }
807                         if (i == nobj) {
808                                 r = -EINVAL;
809                                 vfree(objs);
810                                 goto error;
811                         }
812
813                         if (!SN_HWPERF_IS_NODE(objs + i) &&
814                             !SN_HWPERF_IS_IONODE(objs + i)) {
815                                 r = -ENOENT;
816                                 vfree(objs);
817                                 goto error;
818                         }
819
820                         *(u64 *)p = (u64)sn_hwperf_obj_to_cnode(objs + i);
821                         vfree(objs);
822                 }
823                 break;
824
825         case SN_HWPERF_GET_MMRS:
826         case SN_HWPERF_SET_MMRS:
827         case SN_HWPERF_OBJECT_DISTANCE:
828                 op_info.p = p;
829                 op_info.a = &a;
830                 op_info.v0 = &v0;
831                 op_info.op = op;
832                 r = sn_hwperf_op_cpu(&op_info);
833                 if (r) {
834                         r = sn_hwperf_map_err(r);
835                         a.v0 = v0;
836                         goto error;
837                 }
838                 break;
839
840         default:
841                 /* all other ops are a direct SAL call */
842                 r = ia64_sn_hwperf_op(sn_hwperf_master_nasid, op,
843                               a.arg, a.sz, (u64) p, 0, 0, &v0);
844                 if (r) {
845                         r = sn_hwperf_map_err(r);
846                         goto error;
847                 }
848                 a.v0 = v0;
849                 break;
850         }
851
852         if (op & SN_HWPERF_OP_MEM_COPYOUT) {
853                 r = copy_to_user((void __user *)a.ptr, p, a.sz);
854                 if (r != 0) {
855                         r = -EFAULT;
856                         goto error;
857                 }
858         }
859
860 error:
861         vfree(p);
862
863         lock_kernel();
864         return r;
865 }
866
867 static const struct file_operations sn_hwperf_fops = {
868         .ioctl = sn_hwperf_ioctl,
869 };
870
871 static struct miscdevice sn_hwperf_dev = {
872         MISC_DYNAMIC_MINOR,
873         "sn_hwperf",
874         &sn_hwperf_fops
875 };
876
877 static int sn_hwperf_init(void)
878 {
879         u64 v;
880         int salr;
881         int e = 0;
882
883         /* single threaded, once-only initialization */
884         mutex_lock(&sn_hwperf_init_mutex);
885
886         if (sn_hwperf_salheap) {
887                 mutex_unlock(&sn_hwperf_init_mutex);
888                 return e;
889         }
890
891         /*
892          * The PROM code needs a fixed reference node. For convenience the
893          * same node as the console I/O is used.
894          */
895         sn_hwperf_master_nasid = (nasid_t) ia64_sn_get_console_nasid();
896
897         /*
898          * Request the needed size and install the PROM scratch area.
899          * The PROM keeps various tracking bits in this memory area.
900          */
901         salr = ia64_sn_hwperf_op(sn_hwperf_master_nasid,
902                                  (u64) SN_HWPERF_GET_HEAPSIZE, 0,
903                                  (u64) sizeof(u64), (u64) &v, 0, 0, NULL);
904         if (salr != SN_HWPERF_OP_OK) {
905                 e = -EINVAL;
906                 goto out;
907         }
908
909         if ((sn_hwperf_salheap = vmalloc(v)) == NULL) {
910                 e = -ENOMEM;
911                 goto out;
912         }
913         salr = ia64_sn_hwperf_op(sn_hwperf_master_nasid,
914                                  SN_HWPERF_INSTALL_HEAP, 0, v,
915                                  (u64) sn_hwperf_salheap, 0, 0, NULL);
916         if (salr != SN_HWPERF_OP_OK) {
917                 e = -EINVAL;
918                 goto out;
919         }
920
921         salr = ia64_sn_hwperf_op(sn_hwperf_master_nasid,
922                                  SN_HWPERF_OBJECT_COUNT, 0,
923                                  sizeof(u64), (u64) &v, 0, 0, NULL);
924         if (salr != SN_HWPERF_OP_OK) {
925                 e = -EINVAL;
926                 goto out;
927         }
928         sn_hwperf_obj_cnt = (int)v;
929
930 out:
931         if (e < 0 && sn_hwperf_salheap) {
932                 vfree(sn_hwperf_salheap);
933                 sn_hwperf_salheap = NULL;
934                 sn_hwperf_obj_cnt = 0;
935         }
936         mutex_unlock(&sn_hwperf_init_mutex);
937         return e;
938 }
939
940 int sn_topology_open(struct inode *inode, struct file *file)
941 {
942         int e;
943         struct seq_file *seq;
944         struct sn_hwperf_object_info *objbuf;
945         int nobj;
946
947         if ((e = sn_hwperf_enum_objects(&nobj, &objbuf)) == 0) {
948                 e = seq_open(file, &sn_topology_seq_ops);
949                 seq = file->private_data;
950                 seq->private = objbuf;
951         }
952
953         return e;
954 }
955
956 int sn_topology_release(struct inode *inode, struct file *file)
957 {
958         struct seq_file *seq = file->private_data;
959
960         vfree(seq->private);
961         return seq_release(inode, file);
962 }
963
964 int sn_hwperf_get_nearest_node(cnodeid_t node,
965         cnodeid_t *near_mem_node, cnodeid_t *near_cpu_node)
966 {
967         int e;
968         int nobj;
969         struct sn_hwperf_object_info *objbuf;
970
971         if ((e = sn_hwperf_enum_objects(&nobj, &objbuf)) == 0) {
972                 e = sn_hwperf_get_nearest_node_objdata(objbuf, nobj,
973                         node, near_mem_node, near_cpu_node);
974                 vfree(objbuf);
975         }
976
977         return e;
978 }
979
980 static int __devinit sn_hwperf_misc_register_init(void)
981 {
982         int e;
983
984         if (!ia64_platform_is("sn2"))
985                 return 0;
986
987         sn_hwperf_init();
988
989         /*
990          * Register a dynamic misc device for hwperf ioctls. Platforms
991          * supporting hotplug will create /dev/sn_hwperf, else user
992          * can to look up the minor number in /proc/misc.
993          */
994         if ((e = misc_register(&sn_hwperf_dev)) != 0) {
995                 printk(KERN_ERR "sn_hwperf_misc_register_init: failed to "
996                 "register misc device for \"%s\"\n", sn_hwperf_dev.name);
997         }
998
999         return e;
1000 }
1001
1002 device_initcall(sn_hwperf_misc_register_init); /* after misc_init() */
1003 EXPORT_SYMBOL(sn_hwperf_get_nearest_node);