sn_hwperf.c 18 KB

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