sn_hwperf.c 22 KB

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