sn_hwperf.c 23 KB

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