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