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