sn_hwperf.c 23 KB

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