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