sn_hwperf.c 22 KB

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