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