sn_hwperf.c 23 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006
  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 <asm/processor.h>
  36. #include <asm/topology.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. #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. cpumask_t cpumask;
  344. char slice;
  345. struct cpuinfo_ia64 *c;
  346. struct sn_hwperf_port_info *ptdata;
  347. struct sn_hwperf_object_info *p;
  348. struct sn_hwperf_object_info *obj = d; /* this object */
  349. struct sn_hwperf_object_info *objs = s->private; /* all objects */
  350. u8 shubtype;
  351. u8 system_size;
  352. u8 sharing_size;
  353. u8 partid;
  354. u8 coher;
  355. u8 nasid_shift;
  356. u8 region_size;
  357. u16 nasid_mask;
  358. int nasid_msb;
  359. if (obj == objs) {
  360. seq_printf(s, "# sn_topology version 2\n");
  361. seq_printf(s, "# objtype ordinal location partition"
  362. " [attribute value [, ...]]\n");
  363. if (ia64_sn_get_sn_info(0,
  364. &shubtype, &nasid_mask, &nasid_shift, &system_size,
  365. &sharing_size, &partid, &coher, &region_size))
  366. BUG();
  367. for (nasid_msb=63; nasid_msb > 0; nasid_msb--) {
  368. if (((u64)nasid_mask << nasid_shift) & (1ULL << nasid_msb))
  369. break;
  370. }
  371. seq_printf(s, "partition %u %s local "
  372. "shubtype %s, "
  373. "nasid_mask 0x%016lx, "
  374. "nasid_bits %d:%d, "
  375. "system_size %d, "
  376. "sharing_size %d, "
  377. "coherency_domain %d, "
  378. "region_size %d\n",
  379. partid, utsname()->nodename,
  380. shubtype ? "shub2" : "shub1",
  381. (u64)nasid_mask << nasid_shift, nasid_msb, nasid_shift,
  382. system_size, sharing_size, coher, region_size);
  383. print_pci_topology(s);
  384. }
  385. if (SN_HWPERF_FOREIGN(obj)) {
  386. /* private in another partition: not interesting */
  387. return 0;
  388. }
  389. for (i = 0; i < SN_HWPERF_MAXSTRING && obj->name[i]; i++) {
  390. if (obj->name[i] == ' ')
  391. obj->name[i] = '_';
  392. }
  393. slabname = sn_hwperf_get_slabname(obj, objs, &ordinal);
  394. seq_printf(s, "%s %d %s %s asic %s", slabname, ordinal, obj->location,
  395. obj->sn_hwp_this_part ? "local" : "shared", obj->name);
  396. if (ordinal < 0 || (!SN_HWPERF_IS_NODE(obj) && !SN_HWPERF_IS_IONODE(obj)))
  397. seq_putc(s, '\n');
  398. else {
  399. cnodeid_t near_mem = -1;
  400. cnodeid_t near_cpu = -1;
  401. seq_printf(s, ", nasid 0x%x", cnodeid_to_nasid(ordinal));
  402. if (sn_hwperf_get_nearest_node_objdata(objs, sn_hwperf_obj_cnt,
  403. ordinal, &near_mem, &near_cpu) == 0) {
  404. seq_printf(s, ", near_mem_nodeid %d, near_cpu_nodeid %d",
  405. near_mem, near_cpu);
  406. }
  407. if (!SN_HWPERF_IS_IONODE(obj)) {
  408. for_each_online_node(i) {
  409. seq_printf(s, i ? ":%d" : ", dist %d",
  410. node_distance(ordinal, i));
  411. }
  412. }
  413. seq_putc(s, '\n');
  414. /*
  415. * CPUs on this node, if any
  416. */
  417. if (!SN_HWPERF_IS_IONODE(obj)) {
  418. cpumask = node_to_cpumask(ordinal);
  419. for_each_online_cpu(i) {
  420. if (cpu_isset(i, cpumask)) {
  421. slice = 'a' + cpuid_to_slice(i);
  422. c = cpu_data(i);
  423. seq_printf(s, "cpu %d %s%c local"
  424. " freq %luMHz, arch ia64",
  425. i, obj->location, slice,
  426. c->proc_freq / 1000000);
  427. for_each_online_cpu(j) {
  428. seq_printf(s, j ? ":%d" : ", dist %d",
  429. node_distance(
  430. cpu_to_node(i),
  431. cpu_to_node(j)));
  432. }
  433. seq_putc(s, '\n');
  434. }
  435. }
  436. }
  437. }
  438. if (obj->ports) {
  439. /*
  440. * numalink ports
  441. */
  442. sz = obj->ports * sizeof(struct sn_hwperf_port_info);
  443. if ((ptdata = kmalloc(sz, GFP_KERNEL)) == NULL)
  444. return -ENOMEM;
  445. e = ia64_sn_hwperf_op(sn_hwperf_master_nasid,
  446. SN_HWPERF_ENUM_PORTS, obj->id, sz,
  447. (u64) ptdata, 0, 0, NULL);
  448. if (e != SN_HWPERF_OP_OK)
  449. return -EINVAL;
  450. for (ordinal=0, p=objs; p != obj; p++) {
  451. if (!SN_HWPERF_FOREIGN(p))
  452. ordinal += p->ports;
  453. }
  454. for (pt = 0; pt < obj->ports; pt++) {
  455. for (p = objs, i = 0; i < sn_hwperf_obj_cnt; i++, p++) {
  456. if (ptdata[pt].conn_id == p->id) {
  457. break;
  458. }
  459. }
  460. seq_printf(s, "numalink %d %s-%d",
  461. ordinal+pt, obj->location, ptdata[pt].port);
  462. if (i >= sn_hwperf_obj_cnt) {
  463. /* no connection */
  464. seq_puts(s, " local endpoint disconnected"
  465. ", protocol unknown\n");
  466. continue;
  467. }
  468. if (obj->sn_hwp_this_part && p->sn_hwp_this_part)
  469. /* both ends local to this partition */
  470. seq_puts(s, " local");
  471. else if (SN_HWPERF_FOREIGN(p))
  472. /* both ends of the link in foreign partiton */
  473. seq_puts(s, " foreign");
  474. else
  475. /* link straddles a partition */
  476. seq_puts(s, " shared");
  477. /*
  478. * Unlikely, but strictly should query the LLP config
  479. * registers because an NL4R can be configured to run
  480. * NL3 protocol, even when not talking to an NL3 router.
  481. * Ditto for node-node.
  482. */
  483. seq_printf(s, " endpoint %s-%d, protocol %s\n",
  484. p->location, ptdata[pt].conn_port,
  485. (SN_HWPERF_IS_NL3ROUTER(obj) ||
  486. SN_HWPERF_IS_NL3ROUTER(p)) ? "LLP3" : "LLP4");
  487. }
  488. kfree(ptdata);
  489. }
  490. return 0;
  491. }
  492. static void *sn_topology_start(struct seq_file *s, loff_t * pos)
  493. {
  494. struct sn_hwperf_object_info *objs = s->private;
  495. if (*pos < sn_hwperf_obj_cnt)
  496. return (void *)(objs + *pos);
  497. return NULL;
  498. }
  499. static void *sn_topology_next(struct seq_file *s, void *v, loff_t * pos)
  500. {
  501. ++*pos;
  502. return sn_topology_start(s, pos);
  503. }
  504. static void sn_topology_stop(struct seq_file *m, void *v)
  505. {
  506. return;
  507. }
  508. /*
  509. * /proc/sgi_sn/sn_topology, read-only using seq_file
  510. */
  511. static struct seq_operations sn_topology_seq_ops = {
  512. .start = sn_topology_start,
  513. .next = sn_topology_next,
  514. .stop = sn_topology_stop,
  515. .show = sn_topology_show
  516. };
  517. struct sn_hwperf_op_info {
  518. u64 op;
  519. struct sn_hwperf_ioctl_args *a;
  520. void *p;
  521. int *v0;
  522. int ret;
  523. };
  524. static void sn_hwperf_call_sal(void *info)
  525. {
  526. struct sn_hwperf_op_info *op_info = info;
  527. int r;
  528. r = ia64_sn_hwperf_op(sn_hwperf_master_nasid, op_info->op,
  529. op_info->a->arg, op_info->a->sz,
  530. (u64) op_info->p, 0, 0, op_info->v0);
  531. op_info->ret = r;
  532. }
  533. static int sn_hwperf_op_cpu(struct sn_hwperf_op_info *op_info)
  534. {
  535. u32 cpu;
  536. u32 use_ipi;
  537. int r = 0;
  538. cpumask_t save_allowed;
  539. cpu = (op_info->a->arg & SN_HWPERF_ARG_CPU_MASK) >> 32;
  540. use_ipi = op_info->a->arg & SN_HWPERF_ARG_USE_IPI_MASK;
  541. op_info->a->arg &= SN_HWPERF_ARG_OBJID_MASK;
  542. if (cpu != SN_HWPERF_ARG_ANY_CPU) {
  543. if (cpu >= NR_CPUS || !cpu_online(cpu)) {
  544. r = -EINVAL;
  545. goto out;
  546. }
  547. }
  548. if (cpu == SN_HWPERF_ARG_ANY_CPU || cpu == get_cpu()) {
  549. /* don't care, or already on correct cpu */
  550. sn_hwperf_call_sal(op_info);
  551. }
  552. else {
  553. if (use_ipi) {
  554. /* use an interprocessor interrupt to call SAL */
  555. smp_call_function_single(cpu, sn_hwperf_call_sal,
  556. op_info, 1, 1);
  557. }
  558. else {
  559. /* migrate the task before calling SAL */
  560. save_allowed = current->cpus_allowed;
  561. set_cpus_allowed(current, cpumask_of_cpu(cpu));
  562. sn_hwperf_call_sal(op_info);
  563. set_cpus_allowed(current, save_allowed);
  564. }
  565. }
  566. r = op_info->ret;
  567. out:
  568. return r;
  569. }
  570. /* map SAL hwperf error code to system error code */
  571. static int sn_hwperf_map_err(int hwperf_err)
  572. {
  573. int e;
  574. switch(hwperf_err) {
  575. case SN_HWPERF_OP_OK:
  576. e = 0;
  577. break;
  578. case SN_HWPERF_OP_NOMEM:
  579. e = -ENOMEM;
  580. break;
  581. case SN_HWPERF_OP_NO_PERM:
  582. e = -EPERM;
  583. break;
  584. case SN_HWPERF_OP_IO_ERROR:
  585. e = -EIO;
  586. break;
  587. case SN_HWPERF_OP_BUSY:
  588. e = -EBUSY;
  589. break;
  590. case SN_HWPERF_OP_RECONFIGURE:
  591. e = -EAGAIN;
  592. break;
  593. case SN_HWPERF_OP_INVAL:
  594. default:
  595. e = -EINVAL;
  596. break;
  597. }
  598. return e;
  599. }
  600. /*
  601. * ioctl for "sn_hwperf" misc device
  602. */
  603. static int
  604. sn_hwperf_ioctl(struct inode *in, struct file *fp, u32 op, u64 arg)
  605. {
  606. struct sn_hwperf_ioctl_args a;
  607. struct cpuinfo_ia64 *cdata;
  608. struct sn_hwperf_object_info *objs;
  609. struct sn_hwperf_object_info *cpuobj;
  610. struct sn_hwperf_op_info op_info;
  611. void *p = NULL;
  612. int nobj;
  613. char slice;
  614. int node;
  615. int r;
  616. int v0;
  617. int i;
  618. int j;
  619. unlock_kernel();
  620. /* only user requests are allowed here */
  621. if ((op & SN_HWPERF_OP_MASK) < 10) {
  622. r = -EINVAL;
  623. goto error;
  624. }
  625. r = copy_from_user(&a, (const void __user *)arg,
  626. sizeof(struct sn_hwperf_ioctl_args));
  627. if (r != 0) {
  628. r = -EFAULT;
  629. goto error;
  630. }
  631. /*
  632. * Allocate memory to hold a kernel copy of the user buffer. The
  633. * buffer contents are either copied in or out (or both) of user
  634. * space depending on the flags encoded in the requested operation.
  635. */
  636. if (a.ptr) {
  637. p = vmalloc(a.sz);
  638. if (!p) {
  639. r = -ENOMEM;
  640. goto error;
  641. }
  642. }
  643. if (op & SN_HWPERF_OP_MEM_COPYIN) {
  644. r = copy_from_user(p, (const void __user *)a.ptr, a.sz);
  645. if (r != 0) {
  646. r = -EFAULT;
  647. goto error;
  648. }
  649. }
  650. switch (op) {
  651. case SN_HWPERF_GET_CPU_INFO:
  652. if (a.sz == sizeof(u64)) {
  653. /* special case to get size needed */
  654. *(u64 *) p = (u64) num_online_cpus() *
  655. sizeof(struct sn_hwperf_object_info);
  656. } else
  657. if (a.sz < num_online_cpus() * sizeof(struct sn_hwperf_object_info)) {
  658. r = -ENOMEM;
  659. goto error;
  660. } else
  661. if ((r = sn_hwperf_enum_objects(&nobj, &objs)) == 0) {
  662. int cpuobj_index = 0;
  663. memset(p, 0, a.sz);
  664. for (i = 0; i < nobj; i++) {
  665. if (!SN_HWPERF_IS_NODE(objs + i))
  666. continue;
  667. node = sn_hwperf_obj_to_cnode(objs + i);
  668. for_each_online_cpu(j) {
  669. if (node != cpu_to_node(j))
  670. continue;
  671. cpuobj = (struct sn_hwperf_object_info *) p + cpuobj_index++;
  672. slice = 'a' + cpuid_to_slice(j);
  673. cdata = cpu_data(j);
  674. cpuobj->id = j;
  675. snprintf(cpuobj->name,
  676. sizeof(cpuobj->name),
  677. "CPU %luMHz %s",
  678. cdata->proc_freq / 1000000,
  679. cdata->vendor);
  680. snprintf(cpuobj->location,
  681. sizeof(cpuobj->location),
  682. "%s%c", objs[i].location,
  683. slice);
  684. }
  685. }
  686. vfree(objs);
  687. }
  688. break;
  689. case SN_HWPERF_GET_NODE_NASID:
  690. if (a.sz != sizeof(u64) ||
  691. (node = a.arg) < 0 || !cnode_possible(node)) {
  692. r = -EINVAL;
  693. goto error;
  694. }
  695. *(u64 *)p = (u64)cnodeid_to_nasid(node);
  696. break;
  697. case SN_HWPERF_GET_OBJ_NODE:
  698. if (a.sz != sizeof(u64) || a.arg < 0) {
  699. r = -EINVAL;
  700. goto error;
  701. }
  702. if ((r = sn_hwperf_enum_objects(&nobj, &objs)) == 0) {
  703. if (a.arg >= nobj) {
  704. r = -EINVAL;
  705. vfree(objs);
  706. goto error;
  707. }
  708. if (objs[(i = a.arg)].id != a.arg) {
  709. for (i = 0; i < nobj; i++) {
  710. if (objs[i].id == a.arg)
  711. break;
  712. }
  713. }
  714. if (i == nobj) {
  715. r = -EINVAL;
  716. vfree(objs);
  717. goto error;
  718. }
  719. if (!SN_HWPERF_IS_NODE(objs + i) &&
  720. !SN_HWPERF_IS_IONODE(objs + i)) {
  721. r = -ENOENT;
  722. vfree(objs);
  723. goto error;
  724. }
  725. *(u64 *)p = (u64)sn_hwperf_obj_to_cnode(objs + i);
  726. vfree(objs);
  727. }
  728. break;
  729. case SN_HWPERF_GET_MMRS:
  730. case SN_HWPERF_SET_MMRS:
  731. case SN_HWPERF_OBJECT_DISTANCE:
  732. op_info.p = p;
  733. op_info.a = &a;
  734. op_info.v0 = &v0;
  735. op_info.op = op;
  736. r = sn_hwperf_op_cpu(&op_info);
  737. if (r) {
  738. r = sn_hwperf_map_err(r);
  739. a.v0 = v0;
  740. goto error;
  741. }
  742. break;
  743. default:
  744. /* all other ops are a direct SAL call */
  745. r = ia64_sn_hwperf_op(sn_hwperf_master_nasid, op,
  746. a.arg, a.sz, (u64) p, 0, 0, &v0);
  747. if (r) {
  748. r = sn_hwperf_map_err(r);
  749. goto error;
  750. }
  751. a.v0 = v0;
  752. break;
  753. }
  754. if (op & SN_HWPERF_OP_MEM_COPYOUT) {
  755. r = copy_to_user((void __user *)a.ptr, p, a.sz);
  756. if (r != 0) {
  757. r = -EFAULT;
  758. goto error;
  759. }
  760. }
  761. error:
  762. vfree(p);
  763. lock_kernel();
  764. return r;
  765. }
  766. static const struct file_operations sn_hwperf_fops = {
  767. .ioctl = sn_hwperf_ioctl,
  768. };
  769. static struct miscdevice sn_hwperf_dev = {
  770. MISC_DYNAMIC_MINOR,
  771. "sn_hwperf",
  772. &sn_hwperf_fops
  773. };
  774. static int sn_hwperf_init(void)
  775. {
  776. u64 v;
  777. int salr;
  778. int e = 0;
  779. /* single threaded, once-only initialization */
  780. down(&sn_hwperf_init_mutex);
  781. if (sn_hwperf_salheap) {
  782. up(&sn_hwperf_init_mutex);
  783. return e;
  784. }
  785. /*
  786. * The PROM code needs a fixed reference node. For convenience the
  787. * same node as the console I/O is used.
  788. */
  789. sn_hwperf_master_nasid = (nasid_t) ia64_sn_get_console_nasid();
  790. /*
  791. * Request the needed size and install the PROM scratch area.
  792. * The PROM keeps various tracking bits in this memory area.
  793. */
  794. salr = ia64_sn_hwperf_op(sn_hwperf_master_nasid,
  795. (u64) SN_HWPERF_GET_HEAPSIZE, 0,
  796. (u64) sizeof(u64), (u64) &v, 0, 0, NULL);
  797. if (salr != SN_HWPERF_OP_OK) {
  798. e = -EINVAL;
  799. goto out;
  800. }
  801. if ((sn_hwperf_salheap = vmalloc(v)) == NULL) {
  802. e = -ENOMEM;
  803. goto out;
  804. }
  805. salr = ia64_sn_hwperf_op(sn_hwperf_master_nasid,
  806. SN_HWPERF_INSTALL_HEAP, 0, v,
  807. (u64) sn_hwperf_salheap, 0, 0, NULL);
  808. if (salr != SN_HWPERF_OP_OK) {
  809. e = -EINVAL;
  810. goto out;
  811. }
  812. salr = ia64_sn_hwperf_op(sn_hwperf_master_nasid,
  813. SN_HWPERF_OBJECT_COUNT, 0,
  814. sizeof(u64), (u64) &v, 0, 0, NULL);
  815. if (salr != SN_HWPERF_OP_OK) {
  816. e = -EINVAL;
  817. goto out;
  818. }
  819. sn_hwperf_obj_cnt = (int)v;
  820. out:
  821. if (e < 0 && sn_hwperf_salheap) {
  822. vfree(sn_hwperf_salheap);
  823. sn_hwperf_salheap = NULL;
  824. sn_hwperf_obj_cnt = 0;
  825. }
  826. up(&sn_hwperf_init_mutex);
  827. return e;
  828. }
  829. int sn_topology_open(struct inode *inode, struct file *file)
  830. {
  831. int e;
  832. struct seq_file *seq;
  833. struct sn_hwperf_object_info *objbuf;
  834. int nobj;
  835. if ((e = sn_hwperf_enum_objects(&nobj, &objbuf)) == 0) {
  836. e = seq_open(file, &sn_topology_seq_ops);
  837. seq = file->private_data;
  838. seq->private = objbuf;
  839. }
  840. return e;
  841. }
  842. int sn_topology_release(struct inode *inode, struct file *file)
  843. {
  844. struct seq_file *seq = file->private_data;
  845. vfree(seq->private);
  846. return seq_release(inode, file);
  847. }
  848. int sn_hwperf_get_nearest_node(cnodeid_t node,
  849. cnodeid_t *near_mem_node, cnodeid_t *near_cpu_node)
  850. {
  851. int e;
  852. int nobj;
  853. struct sn_hwperf_object_info *objbuf;
  854. if ((e = sn_hwperf_enum_objects(&nobj, &objbuf)) == 0) {
  855. e = sn_hwperf_get_nearest_node_objdata(objbuf, nobj,
  856. node, near_mem_node, near_cpu_node);
  857. vfree(objbuf);
  858. }
  859. return e;
  860. }
  861. static int __devinit sn_hwperf_misc_register_init(void)
  862. {
  863. int e;
  864. if (!ia64_platform_is("sn2"))
  865. return 0;
  866. sn_hwperf_init();
  867. /*
  868. * Register a dynamic misc device for hwperf ioctls. Platforms
  869. * supporting hotplug will create /dev/sn_hwperf, else user
  870. * can to look up the minor number in /proc/misc.
  871. */
  872. if ((e = misc_register(&sn_hwperf_dev)) != 0) {
  873. printk(KERN_ERR "sn_hwperf_misc_register_init: failed to "
  874. "register misc device for \"%s\"\n", sn_hwperf_dev.name);
  875. }
  876. return e;
  877. }
  878. device_initcall(sn_hwperf_misc_register_init); /* after misc_init() */
  879. EXPORT_SYMBOL(sn_hwperf_get_nearest_node);