sn_hwperf.c 18 KB

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