sn_hwperf.c 18 KB

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