Path: blob/master/arch/ia64/sn/kernel/sn2/sn_hwperf.c
17602 views
/*1* This file is subject to the terms and conditions of the GNU General Public2* License. See the file "COPYING" in the main directory of this archive3* for more details.4*5* Copyright (C) 2004-2006 Silicon Graphics, Inc. All rights reserved.6*7* SGI Altix topology and hardware performance monitoring API.8* Mark Goodwin <[email protected]>.9*10* Creates /proc/sgi_sn/sn_topology (read-only) to export11* info about Altix nodes, routers, CPUs and NumaLink12* interconnection/topology.13*14* Also creates a dynamic misc device named "sn_hwperf"15* that supports an ioctl interface to call down into SAL16* to discover hw objects, topology and to read/write17* memory mapped registers, e.g. for performance monitoring.18* The "sn_hwperf" device is registered only after the procfs19* file is first opened, i.e. only if/when it's needed.20*21* This API is used by SGI Performance Co-Pilot and other22* tools, see http://oss.sgi.com/projects/pcp23*/2425#include <linux/fs.h>26#include <linux/slab.h>27#include <linux/vmalloc.h>28#include <linux/seq_file.h>29#include <linux/miscdevice.h>30#include <linux/utsname.h>31#include <linux/cpumask.h>32#include <linux/nodemask.h>33#include <linux/smp.h>34#include <linux/mutex.h>3536#include <asm/processor.h>37#include <asm/topology.h>38#include <asm/uaccess.h>39#include <asm/sal.h>40#include <asm/sn/io.h>41#include <asm/sn/sn_sal.h>42#include <asm/sn/module.h>43#include <asm/sn/geo.h>44#include <asm/sn/sn2/sn_hwperf.h>45#include <asm/sn/addrs.h>4647static void *sn_hwperf_salheap = NULL;48static int sn_hwperf_obj_cnt = 0;49static nasid_t sn_hwperf_master_nasid = INVALID_NASID;50static int sn_hwperf_init(void);51static DEFINE_MUTEX(sn_hwperf_init_mutex);5253#define cnode_possible(n) ((n) < num_cnodes)5455static int sn_hwperf_enum_objects(int *nobj, struct sn_hwperf_object_info **ret)56{57int e;58u64 sz;59struct sn_hwperf_object_info *objbuf = NULL;6061if ((e = sn_hwperf_init()) < 0) {62printk(KERN_ERR "sn_hwperf_init failed: err %d\n", e);63goto out;64}6566sz = sn_hwperf_obj_cnt * sizeof(struct sn_hwperf_object_info);67objbuf = vmalloc(sz);68if (objbuf == NULL) {69printk("sn_hwperf_enum_objects: vmalloc(%d) failed\n", (int)sz);70e = -ENOMEM;71goto out;72}7374e = ia64_sn_hwperf_op(sn_hwperf_master_nasid, SN_HWPERF_ENUM_OBJECTS,750, sz, (u64) objbuf, 0, 0, NULL);76if (e != SN_HWPERF_OP_OK) {77e = -EINVAL;78vfree(objbuf);79}8081out:82*nobj = sn_hwperf_obj_cnt;83*ret = objbuf;84return e;85}8687static int sn_hwperf_location_to_bpos(char *location,88int *rack, int *bay, int *slot, int *slab)89{90char type;9192/* first scan for an old style geoid string */93if (sscanf(location, "%03d%c%02d#%d",94rack, &type, bay, slab) == 4)95*slot = 0;96else /* scan for a new bladed geoid string */97if (sscanf(location, "%03d%c%02d^%02d#%d",98rack, &type, bay, slot, slab) != 5)99return -1;100/* success */101return 0;102}103104static int sn_hwperf_geoid_to_cnode(char *location)105{106int cnode;107geoid_t geoid;108moduleid_t module_id;109int rack, bay, slot, slab;110int this_rack, this_bay, this_slot, this_slab;111112if (sn_hwperf_location_to_bpos(location, &rack, &bay, &slot, &slab))113return -1;114115/*116* FIXME: replace with cleaner for_each_XXX macro which addresses117* both compute and IO nodes once ACPI3.0 is available.118*/119for (cnode = 0; cnode < num_cnodes; cnode++) {120geoid = cnodeid_get_geoid(cnode);121module_id = geo_module(geoid);122this_rack = MODULE_GET_RACK(module_id);123this_bay = MODULE_GET_BPOS(module_id);124this_slot = geo_slot(geoid);125this_slab = geo_slab(geoid);126if (rack == this_rack && bay == this_bay &&127slot == this_slot && slab == this_slab) {128break;129}130}131132return cnode_possible(cnode) ? cnode : -1;133}134135static int sn_hwperf_obj_to_cnode(struct sn_hwperf_object_info * obj)136{137if (!SN_HWPERF_IS_NODE(obj) && !SN_HWPERF_IS_IONODE(obj))138BUG();139if (SN_HWPERF_FOREIGN(obj))140return -1;141return sn_hwperf_geoid_to_cnode(obj->location);142}143144static int sn_hwperf_generic_ordinal(struct sn_hwperf_object_info *obj,145struct sn_hwperf_object_info *objs)146{147int ordinal;148struct sn_hwperf_object_info *p;149150for (ordinal=0, p=objs; p != obj; p++) {151if (SN_HWPERF_FOREIGN(p))152continue;153if (SN_HWPERF_SAME_OBJTYPE(p, obj))154ordinal++;155}156157return ordinal;158}159160static const char *slabname_node = "node"; /* SHub asic */161static const char *slabname_ionode = "ionode"; /* TIO asic */162static const char *slabname_router = "router"; /* NL3R or NL4R */163static const char *slabname_other = "other"; /* unknown asic */164165static const char *sn_hwperf_get_slabname(struct sn_hwperf_object_info *obj,166struct sn_hwperf_object_info *objs, int *ordinal)167{168int isnode;169const char *slabname = slabname_other;170171if ((isnode = SN_HWPERF_IS_NODE(obj)) || SN_HWPERF_IS_IONODE(obj)) {172slabname = isnode ? slabname_node : slabname_ionode;173*ordinal = sn_hwperf_obj_to_cnode(obj);174}175else {176*ordinal = sn_hwperf_generic_ordinal(obj, objs);177if (SN_HWPERF_IS_ROUTER(obj))178slabname = slabname_router;179}180181return slabname;182}183184static void print_pci_topology(struct seq_file *s)185{186char *p;187size_t sz;188int e;189190for (sz = PAGE_SIZE; sz < 16 * PAGE_SIZE; sz += PAGE_SIZE) {191if (!(p = kmalloc(sz, GFP_KERNEL)))192break;193e = ia64_sn_ioif_get_pci_topology(__pa(p), sz);194if (e == SALRET_OK)195seq_puts(s, p);196kfree(p);197if (e == SALRET_OK || e == SALRET_NOT_IMPLEMENTED)198break;199}200}201202static inline int sn_hwperf_has_cpus(cnodeid_t node)203{204return node < MAX_NUMNODES && node_online(node) && nr_cpus_node(node);205}206207static inline int sn_hwperf_has_mem(cnodeid_t node)208{209return node < MAX_NUMNODES && node_online(node) && NODE_DATA(node)->node_present_pages;210}211212static struct sn_hwperf_object_info *213sn_hwperf_findobj_id(struct sn_hwperf_object_info *objbuf,214int nobj, int id)215{216int i;217struct sn_hwperf_object_info *p = objbuf;218219for (i=0; i < nobj; i++, p++) {220if (p->id == id)221return p;222}223224return NULL;225226}227228static int sn_hwperf_get_nearest_node_objdata(struct sn_hwperf_object_info *objbuf,229int nobj, cnodeid_t node, cnodeid_t *near_mem_node, cnodeid_t *near_cpu_node)230{231int e;232struct sn_hwperf_object_info *nodeobj = NULL;233struct sn_hwperf_object_info *op;234struct sn_hwperf_object_info *dest;235struct sn_hwperf_object_info *router;236struct sn_hwperf_port_info ptdata[16];237int sz, i, j;238cnodeid_t c;239int found_mem = 0;240int found_cpu = 0;241242if (!cnode_possible(node))243return -EINVAL;244245if (sn_hwperf_has_cpus(node)) {246if (near_cpu_node)247*near_cpu_node = node;248found_cpu++;249}250251if (sn_hwperf_has_mem(node)) {252if (near_mem_node)253*near_mem_node = node;254found_mem++;255}256257if (found_cpu && found_mem)258return 0; /* trivially successful */259260/* find the argument node object */261for (i=0, op=objbuf; i < nobj; i++, op++) {262if (!SN_HWPERF_IS_NODE(op) && !SN_HWPERF_IS_IONODE(op))263continue;264if (node == sn_hwperf_obj_to_cnode(op)) {265nodeobj = op;266break;267}268}269if (!nodeobj) {270e = -ENOENT;271goto err;272}273274/* get it's interconnect topology */275sz = op->ports * sizeof(struct sn_hwperf_port_info);276BUG_ON(sz > sizeof(ptdata));277e = ia64_sn_hwperf_op(sn_hwperf_master_nasid,278SN_HWPERF_ENUM_PORTS, nodeobj->id, sz,279(u64)&ptdata, 0, 0, NULL);280if (e != SN_HWPERF_OP_OK) {281e = -EINVAL;282goto err;283}284285/* find nearest node with cpus and nearest memory */286for (router=NULL, j=0; j < op->ports; j++) {287dest = sn_hwperf_findobj_id(objbuf, nobj, ptdata[j].conn_id);288if (dest && SN_HWPERF_IS_ROUTER(dest))289router = dest;290if (!dest || SN_HWPERF_FOREIGN(dest) ||291!SN_HWPERF_IS_NODE(dest) || SN_HWPERF_IS_IONODE(dest)) {292continue;293}294c = sn_hwperf_obj_to_cnode(dest);295if (!found_cpu && sn_hwperf_has_cpus(c)) {296if (near_cpu_node)297*near_cpu_node = c;298found_cpu++;299}300if (!found_mem && sn_hwperf_has_mem(c)) {301if (near_mem_node)302*near_mem_node = c;303found_mem++;304}305}306307if (router && (!found_cpu || !found_mem)) {308/* search for a node connected to the same router */309sz = router->ports * sizeof(struct sn_hwperf_port_info);310BUG_ON(sz > sizeof(ptdata));311e = ia64_sn_hwperf_op(sn_hwperf_master_nasid,312SN_HWPERF_ENUM_PORTS, router->id, sz,313(u64)&ptdata, 0, 0, NULL);314if (e != SN_HWPERF_OP_OK) {315e = -EINVAL;316goto err;317}318for (j=0; j < router->ports; j++) {319dest = sn_hwperf_findobj_id(objbuf, nobj,320ptdata[j].conn_id);321if (!dest || dest->id == node ||322SN_HWPERF_FOREIGN(dest) ||323!SN_HWPERF_IS_NODE(dest) ||324SN_HWPERF_IS_IONODE(dest)) {325continue;326}327c = sn_hwperf_obj_to_cnode(dest);328if (!found_cpu && sn_hwperf_has_cpus(c)) {329if (near_cpu_node)330*near_cpu_node = c;331found_cpu++;332}333if (!found_mem && sn_hwperf_has_mem(c)) {334if (near_mem_node)335*near_mem_node = c;336found_mem++;337}338if (found_cpu && found_mem)339break;340}341}342343if (!found_cpu || !found_mem) {344/* resort to _any_ node with CPUs and memory */345for (i=0, op=objbuf; i < nobj; i++, op++) {346if (SN_HWPERF_FOREIGN(op) ||347SN_HWPERF_IS_IONODE(op) ||348!SN_HWPERF_IS_NODE(op)) {349continue;350}351c = sn_hwperf_obj_to_cnode(op);352if (!found_cpu && sn_hwperf_has_cpus(c)) {353if (near_cpu_node)354*near_cpu_node = c;355found_cpu++;356}357if (!found_mem && sn_hwperf_has_mem(c)) {358if (near_mem_node)359*near_mem_node = c;360found_mem++;361}362if (found_cpu && found_mem)363break;364}365}366367if (!found_cpu || !found_mem)368e = -ENODATA;369370err:371return e;372}373374375static int sn_topology_show(struct seq_file *s, void *d)376{377int sz;378int pt;379int e = 0;380int i;381int j;382const char *slabname;383int ordinal;384char slice;385struct cpuinfo_ia64 *c;386struct sn_hwperf_port_info *ptdata;387struct sn_hwperf_object_info *p;388struct sn_hwperf_object_info *obj = d; /* this object */389struct sn_hwperf_object_info *objs = s->private; /* all objects */390u8 shubtype;391u8 system_size;392u8 sharing_size;393u8 partid;394u8 coher;395u8 nasid_shift;396u8 region_size;397u16 nasid_mask;398int nasid_msb;399400if (obj == objs) {401seq_printf(s, "# sn_topology version 2\n");402seq_printf(s, "# objtype ordinal location partition"403" [attribute value [, ...]]\n");404405if (ia64_sn_get_sn_info(0,406&shubtype, &nasid_mask, &nasid_shift, &system_size,407&sharing_size, &partid, &coher, ®ion_size))408BUG();409for (nasid_msb=63; nasid_msb > 0; nasid_msb--) {410if (((u64)nasid_mask << nasid_shift) & (1ULL << nasid_msb))411break;412}413seq_printf(s, "partition %u %s local "414"shubtype %s, "415"nasid_mask 0x%016llx, "416"nasid_bits %d:%d, "417"system_size %d, "418"sharing_size %d, "419"coherency_domain %d, "420"region_size %d\n",421422partid, utsname()->nodename,423shubtype ? "shub2" : "shub1",424(u64)nasid_mask << nasid_shift, nasid_msb, nasid_shift,425system_size, sharing_size, coher, region_size);426427print_pci_topology(s);428}429430if (SN_HWPERF_FOREIGN(obj)) {431/* private in another partition: not interesting */432return 0;433}434435for (i = 0; i < SN_HWPERF_MAXSTRING && obj->name[i]; i++) {436if (obj->name[i] == ' ')437obj->name[i] = '_';438}439440slabname = sn_hwperf_get_slabname(obj, objs, &ordinal);441seq_printf(s, "%s %d %s %s asic %s", slabname, ordinal, obj->location,442obj->sn_hwp_this_part ? "local" : "shared", obj->name);443444if (ordinal < 0 || (!SN_HWPERF_IS_NODE(obj) && !SN_HWPERF_IS_IONODE(obj)))445seq_putc(s, '\n');446else {447cnodeid_t near_mem = -1;448cnodeid_t near_cpu = -1;449450seq_printf(s, ", nasid 0x%x", cnodeid_to_nasid(ordinal));451452if (sn_hwperf_get_nearest_node_objdata(objs, sn_hwperf_obj_cnt,453ordinal, &near_mem, &near_cpu) == 0) {454seq_printf(s, ", near_mem_nodeid %d, near_cpu_nodeid %d",455near_mem, near_cpu);456}457458if (!SN_HWPERF_IS_IONODE(obj)) {459for_each_online_node(i) {460seq_printf(s, i ? ":%d" : ", dist %d",461node_distance(ordinal, i));462}463}464465seq_putc(s, '\n');466467/*468* CPUs on this node, if any469*/470if (!SN_HWPERF_IS_IONODE(obj)) {471for_each_cpu_and(i, cpu_online_mask,472cpumask_of_node(ordinal)) {473slice = 'a' + cpuid_to_slice(i);474c = cpu_data(i);475seq_printf(s, "cpu %d %s%c local"476" freq %luMHz, arch ia64",477i, obj->location, slice,478c->proc_freq / 1000000);479for_each_online_cpu(j) {480seq_printf(s, j ? ":%d" : ", dist %d",481node_distance(482cpu_to_node(i),483cpu_to_node(j)));484}485seq_putc(s, '\n');486}487}488}489490if (obj->ports) {491/*492* numalink ports493*/494sz = obj->ports * sizeof(struct sn_hwperf_port_info);495if ((ptdata = kmalloc(sz, GFP_KERNEL)) == NULL)496return -ENOMEM;497e = ia64_sn_hwperf_op(sn_hwperf_master_nasid,498SN_HWPERF_ENUM_PORTS, obj->id, sz,499(u64) ptdata, 0, 0, NULL);500if (e != SN_HWPERF_OP_OK)501return -EINVAL;502for (ordinal=0, p=objs; p != obj; p++) {503if (!SN_HWPERF_FOREIGN(p))504ordinal += p->ports;505}506for (pt = 0; pt < obj->ports; pt++) {507for (p = objs, i = 0; i < sn_hwperf_obj_cnt; i++, p++) {508if (ptdata[pt].conn_id == p->id) {509break;510}511}512seq_printf(s, "numalink %d %s-%d",513ordinal+pt, obj->location, ptdata[pt].port);514515if (i >= sn_hwperf_obj_cnt) {516/* no connection */517seq_puts(s, " local endpoint disconnected"518", protocol unknown\n");519continue;520}521522if (obj->sn_hwp_this_part && p->sn_hwp_this_part)523/* both ends local to this partition */524seq_puts(s, " local");525else if (SN_HWPERF_FOREIGN(p))526/* both ends of the link in foreign partiton */527seq_puts(s, " foreign");528else529/* link straddles a partition */530seq_puts(s, " shared");531532/*533* Unlikely, but strictly should query the LLP config534* registers because an NL4R can be configured to run535* NL3 protocol, even when not talking to an NL3 router.536* Ditto for node-node.537*/538seq_printf(s, " endpoint %s-%d, protocol %s\n",539p->location, ptdata[pt].conn_port,540(SN_HWPERF_IS_NL3ROUTER(obj) ||541SN_HWPERF_IS_NL3ROUTER(p)) ? "LLP3" : "LLP4");542}543kfree(ptdata);544}545546return 0;547}548549static void *sn_topology_start(struct seq_file *s, loff_t * pos)550{551struct sn_hwperf_object_info *objs = s->private;552553if (*pos < sn_hwperf_obj_cnt)554return (void *)(objs + *pos);555556return NULL;557}558559static void *sn_topology_next(struct seq_file *s, void *v, loff_t * pos)560{561++*pos;562return sn_topology_start(s, pos);563}564565static void sn_topology_stop(struct seq_file *m, void *v)566{567return;568}569570/*571* /proc/sgi_sn/sn_topology, read-only using seq_file572*/573static const struct seq_operations sn_topology_seq_ops = {574.start = sn_topology_start,575.next = sn_topology_next,576.stop = sn_topology_stop,577.show = sn_topology_show578};579580struct sn_hwperf_op_info {581u64 op;582struct sn_hwperf_ioctl_args *a;583void *p;584int *v0;585int ret;586};587588static void sn_hwperf_call_sal(void *info)589{590struct sn_hwperf_op_info *op_info = info;591int r;592593r = ia64_sn_hwperf_op(sn_hwperf_master_nasid, op_info->op,594op_info->a->arg, op_info->a->sz,595(u64) op_info->p, 0, 0, op_info->v0);596op_info->ret = r;597}598599static int sn_hwperf_op_cpu(struct sn_hwperf_op_info *op_info)600{601u32 cpu;602u32 use_ipi;603int r = 0;604cpumask_t save_allowed;605606cpu = (op_info->a->arg & SN_HWPERF_ARG_CPU_MASK) >> 32;607use_ipi = op_info->a->arg & SN_HWPERF_ARG_USE_IPI_MASK;608op_info->a->arg &= SN_HWPERF_ARG_OBJID_MASK;609610if (cpu != SN_HWPERF_ARG_ANY_CPU) {611if (cpu >= nr_cpu_ids || !cpu_online(cpu)) {612r = -EINVAL;613goto out;614}615}616617if (cpu == SN_HWPERF_ARG_ANY_CPU || cpu == get_cpu()) {618/* don't care, or already on correct cpu */619sn_hwperf_call_sal(op_info);620}621else {622if (use_ipi) {623/* use an interprocessor interrupt to call SAL */624smp_call_function_single(cpu, sn_hwperf_call_sal,625op_info, 1);626}627else {628/* migrate the task before calling SAL */629save_allowed = current->cpus_allowed;630set_cpus_allowed_ptr(current, cpumask_of(cpu));631sn_hwperf_call_sal(op_info);632set_cpus_allowed_ptr(current, &save_allowed);633}634}635r = op_info->ret;636637out:638return r;639}640641/* map SAL hwperf error code to system error code */642static int sn_hwperf_map_err(int hwperf_err)643{644int e;645646switch(hwperf_err) {647case SN_HWPERF_OP_OK:648e = 0;649break;650651case SN_HWPERF_OP_NOMEM:652e = -ENOMEM;653break;654655case SN_HWPERF_OP_NO_PERM:656e = -EPERM;657break;658659case SN_HWPERF_OP_IO_ERROR:660e = -EIO;661break;662663case SN_HWPERF_OP_BUSY:664e = -EBUSY;665break;666667case SN_HWPERF_OP_RECONFIGURE:668e = -EAGAIN;669break;670671case SN_HWPERF_OP_INVAL:672default:673e = -EINVAL;674break;675}676677return e;678}679680/*681* ioctl for "sn_hwperf" misc device682*/683static long sn_hwperf_ioctl(struct file *fp, u32 op, unsigned long arg)684{685struct sn_hwperf_ioctl_args a;686struct cpuinfo_ia64 *cdata;687struct sn_hwperf_object_info *objs;688struct sn_hwperf_object_info *cpuobj;689struct sn_hwperf_op_info op_info;690void *p = NULL;691int nobj;692char slice;693int node;694int r;695int v0;696int i;697int j;698699/* only user requests are allowed here */700if ((op & SN_HWPERF_OP_MASK) < 10) {701r = -EINVAL;702goto error;703}704r = copy_from_user(&a, (const void __user *)arg,705sizeof(struct sn_hwperf_ioctl_args));706if (r != 0) {707r = -EFAULT;708goto error;709}710711/*712* Allocate memory to hold a kernel copy of the user buffer. The713* buffer contents are either copied in or out (or both) of user714* space depending on the flags encoded in the requested operation.715*/716if (a.ptr) {717p = vmalloc(a.sz);718if (!p) {719r = -ENOMEM;720goto error;721}722}723724if (op & SN_HWPERF_OP_MEM_COPYIN) {725r = copy_from_user(p, (const void __user *)a.ptr, a.sz);726if (r != 0) {727r = -EFAULT;728goto error;729}730}731732switch (op) {733case SN_HWPERF_GET_CPU_INFO:734if (a.sz == sizeof(u64)) {735/* special case to get size needed */736*(u64 *) p = (u64) num_online_cpus() *737sizeof(struct sn_hwperf_object_info);738} else739if (a.sz < num_online_cpus() * sizeof(struct sn_hwperf_object_info)) {740r = -ENOMEM;741goto error;742} else743if ((r = sn_hwperf_enum_objects(&nobj, &objs)) == 0) {744int cpuobj_index = 0;745746memset(p, 0, a.sz);747for (i = 0; i < nobj; i++) {748if (!SN_HWPERF_IS_NODE(objs + i))749continue;750node = sn_hwperf_obj_to_cnode(objs + i);751for_each_online_cpu(j) {752if (node != cpu_to_node(j))753continue;754cpuobj = (struct sn_hwperf_object_info *) p + cpuobj_index++;755slice = 'a' + cpuid_to_slice(j);756cdata = cpu_data(j);757cpuobj->id = j;758snprintf(cpuobj->name,759sizeof(cpuobj->name),760"CPU %luMHz %s",761cdata->proc_freq / 1000000,762cdata->vendor);763snprintf(cpuobj->location,764sizeof(cpuobj->location),765"%s%c", objs[i].location,766slice);767}768}769770vfree(objs);771}772break;773774case SN_HWPERF_GET_NODE_NASID:775if (a.sz != sizeof(u64) ||776(node = a.arg) < 0 || !cnode_possible(node)) {777r = -EINVAL;778goto error;779}780*(u64 *)p = (u64)cnodeid_to_nasid(node);781break;782783case SN_HWPERF_GET_OBJ_NODE:784i = a.arg;785if (a.sz != sizeof(u64) || i < 0) {786r = -EINVAL;787goto error;788}789if ((r = sn_hwperf_enum_objects(&nobj, &objs)) == 0) {790if (i >= nobj) {791r = -EINVAL;792vfree(objs);793goto error;794}795if (objs[i].id != a.arg) {796for (i = 0; i < nobj; i++) {797if (objs[i].id == a.arg)798break;799}800}801if (i == nobj) {802r = -EINVAL;803vfree(objs);804goto error;805}806807if (!SN_HWPERF_IS_NODE(objs + i) &&808!SN_HWPERF_IS_IONODE(objs + i)) {809r = -ENOENT;810vfree(objs);811goto error;812}813814*(u64 *)p = (u64)sn_hwperf_obj_to_cnode(objs + i);815vfree(objs);816}817break;818819case SN_HWPERF_GET_MMRS:820case SN_HWPERF_SET_MMRS:821case SN_HWPERF_OBJECT_DISTANCE:822op_info.p = p;823op_info.a = &a;824op_info.v0 = &v0;825op_info.op = op;826r = sn_hwperf_op_cpu(&op_info);827if (r) {828r = sn_hwperf_map_err(r);829a.v0 = v0;830goto error;831}832break;833834default:835/* all other ops are a direct SAL call */836r = ia64_sn_hwperf_op(sn_hwperf_master_nasid, op,837a.arg, a.sz, (u64) p, 0, 0, &v0);838if (r) {839r = sn_hwperf_map_err(r);840goto error;841}842a.v0 = v0;843break;844}845846if (op & SN_HWPERF_OP_MEM_COPYOUT) {847r = copy_to_user((void __user *)a.ptr, p, a.sz);848if (r != 0) {849r = -EFAULT;850goto error;851}852}853854error:855vfree(p);856857return r;858}859860static const struct file_operations sn_hwperf_fops = {861.unlocked_ioctl = sn_hwperf_ioctl,862.llseek = noop_llseek,863};864865static struct miscdevice sn_hwperf_dev = {866MISC_DYNAMIC_MINOR,867"sn_hwperf",868&sn_hwperf_fops869};870871static int sn_hwperf_init(void)872{873u64 v;874int salr;875int e = 0;876877/* single threaded, once-only initialization */878mutex_lock(&sn_hwperf_init_mutex);879880if (sn_hwperf_salheap) {881mutex_unlock(&sn_hwperf_init_mutex);882return e;883}884885/*886* The PROM code needs a fixed reference node. For convenience the887* same node as the console I/O is used.888*/889sn_hwperf_master_nasid = (nasid_t) ia64_sn_get_console_nasid();890891/*892* Request the needed size and install the PROM scratch area.893* The PROM keeps various tracking bits in this memory area.894*/895salr = ia64_sn_hwperf_op(sn_hwperf_master_nasid,896(u64) SN_HWPERF_GET_HEAPSIZE, 0,897(u64) sizeof(u64), (u64) &v, 0, 0, NULL);898if (salr != SN_HWPERF_OP_OK) {899e = -EINVAL;900goto out;901}902903if ((sn_hwperf_salheap = vmalloc(v)) == NULL) {904e = -ENOMEM;905goto out;906}907salr = ia64_sn_hwperf_op(sn_hwperf_master_nasid,908SN_HWPERF_INSTALL_HEAP, 0, v,909(u64) sn_hwperf_salheap, 0, 0, NULL);910if (salr != SN_HWPERF_OP_OK) {911e = -EINVAL;912goto out;913}914915salr = ia64_sn_hwperf_op(sn_hwperf_master_nasid,916SN_HWPERF_OBJECT_COUNT, 0,917sizeof(u64), (u64) &v, 0, 0, NULL);918if (salr != SN_HWPERF_OP_OK) {919e = -EINVAL;920goto out;921}922sn_hwperf_obj_cnt = (int)v;923924out:925if (e < 0 && sn_hwperf_salheap) {926vfree(sn_hwperf_salheap);927sn_hwperf_salheap = NULL;928sn_hwperf_obj_cnt = 0;929}930mutex_unlock(&sn_hwperf_init_mutex);931return e;932}933934int sn_topology_open(struct inode *inode, struct file *file)935{936int e;937struct seq_file *seq;938struct sn_hwperf_object_info *objbuf;939int nobj;940941if ((e = sn_hwperf_enum_objects(&nobj, &objbuf)) == 0) {942e = seq_open(file, &sn_topology_seq_ops);943seq = file->private_data;944seq->private = objbuf;945}946947return e;948}949950int sn_topology_release(struct inode *inode, struct file *file)951{952struct seq_file *seq = file->private_data;953954vfree(seq->private);955return seq_release(inode, file);956}957958int sn_hwperf_get_nearest_node(cnodeid_t node,959cnodeid_t *near_mem_node, cnodeid_t *near_cpu_node)960{961int e;962int nobj;963struct sn_hwperf_object_info *objbuf;964965if ((e = sn_hwperf_enum_objects(&nobj, &objbuf)) == 0) {966e = sn_hwperf_get_nearest_node_objdata(objbuf, nobj,967node, near_mem_node, near_cpu_node);968vfree(objbuf);969}970971return e;972}973974static int __devinit sn_hwperf_misc_register_init(void)975{976int e;977978if (!ia64_platform_is("sn2"))979return 0;980981sn_hwperf_init();982983/*984* Register a dynamic misc device for hwperf ioctls. Platforms985* supporting hotplug will create /dev/sn_hwperf, else user986* can to look up the minor number in /proc/misc.987*/988if ((e = misc_register(&sn_hwperf_dev)) != 0) {989printk(KERN_ERR "sn_hwperf_misc_register_init: failed to "990"register misc device for \"%s\"\n", sn_hwperf_dev.name);991}992993return e;994}995996device_initcall(sn_hwperf_misc_register_init); /* after misc_init() */997EXPORT_SYMBOL(sn_hwperf_get_nearest_node);9989991000