sn_hwperf.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002
  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) {
  543. /* don't care which cpu */
  544. sn_hwperf_call_sal(op_info);
  545. } else if (cpu == get_cpu()) {
  546. /* already on correct cpu */
  547. sn_hwperf_call_sal(op_info);
  548. put_cpu();
  549. } else {
  550. put_cpu();
  551. if (use_ipi) {
  552. /* use an interprocessor interrupt to call SAL */
  553. smp_call_function_single(cpu, sn_hwperf_call_sal,
  554. op_info, 1);
  555. }
  556. else {
  557. /* migrate the task before calling SAL */
  558. save_allowed = current->cpus_allowed;
  559. set_cpus_allowed_ptr(current, cpumask_of(cpu));
  560. sn_hwperf_call_sal(op_info);
  561. set_cpus_allowed_ptr(current, &save_allowed);
  562. }
  563. }
  564. r = op_info->ret;
  565. out:
  566. return r;
  567. }
  568. /* map SAL hwperf error code to system error code */
  569. static int sn_hwperf_map_err(int hwperf_err)
  570. {
  571. int e;
  572. switch(hwperf_err) {
  573. case SN_HWPERF_OP_OK:
  574. e = 0;
  575. break;
  576. case SN_HWPERF_OP_NOMEM:
  577. e = -ENOMEM;
  578. break;
  579. case SN_HWPERF_OP_NO_PERM:
  580. e = -EPERM;
  581. break;
  582. case SN_HWPERF_OP_IO_ERROR:
  583. e = -EIO;
  584. break;
  585. case SN_HWPERF_OP_BUSY:
  586. e = -EBUSY;
  587. break;
  588. case SN_HWPERF_OP_RECONFIGURE:
  589. e = -EAGAIN;
  590. break;
  591. case SN_HWPERF_OP_INVAL:
  592. default:
  593. e = -EINVAL;
  594. break;
  595. }
  596. return e;
  597. }
  598. /*
  599. * ioctl for "sn_hwperf" misc device
  600. */
  601. static long sn_hwperf_ioctl(struct file *fp, u32 op, unsigned long arg)
  602. {
  603. struct sn_hwperf_ioctl_args a;
  604. struct cpuinfo_ia64 *cdata;
  605. struct sn_hwperf_object_info *objs;
  606. struct sn_hwperf_object_info *cpuobj;
  607. struct sn_hwperf_op_info op_info;
  608. void *p = NULL;
  609. int nobj;
  610. char slice;
  611. int node;
  612. int r;
  613. int v0;
  614. int i;
  615. int j;
  616. /* only user requests are allowed here */
  617. if ((op & SN_HWPERF_OP_MASK) < 10) {
  618. r = -EINVAL;
  619. goto error;
  620. }
  621. r = copy_from_user(&a, (const void __user *)arg,
  622. sizeof(struct sn_hwperf_ioctl_args));
  623. if (r != 0) {
  624. r = -EFAULT;
  625. goto error;
  626. }
  627. /*
  628. * Allocate memory to hold a kernel copy of the user buffer. The
  629. * buffer contents are either copied in or out (or both) of user
  630. * space depending on the flags encoded in the requested operation.
  631. */
  632. if (a.ptr) {
  633. p = vmalloc(a.sz);
  634. if (!p) {
  635. r = -ENOMEM;
  636. goto error;
  637. }
  638. }
  639. if (op & SN_HWPERF_OP_MEM_COPYIN) {
  640. r = copy_from_user(p, (const void __user *)a.ptr, a.sz);
  641. if (r != 0) {
  642. r = -EFAULT;
  643. goto error;
  644. }
  645. }
  646. switch (op) {
  647. case SN_HWPERF_GET_CPU_INFO:
  648. if (a.sz == sizeof(u64)) {
  649. /* special case to get size needed */
  650. *(u64 *) p = (u64) num_online_cpus() *
  651. sizeof(struct sn_hwperf_object_info);
  652. } else
  653. if (a.sz < num_online_cpus() * sizeof(struct sn_hwperf_object_info)) {
  654. r = -ENOMEM;
  655. goto error;
  656. } else
  657. if ((r = sn_hwperf_enum_objects(&nobj, &objs)) == 0) {
  658. int cpuobj_index = 0;
  659. memset(p, 0, a.sz);
  660. for (i = 0; i < nobj; i++) {
  661. if (!SN_HWPERF_IS_NODE(objs + i))
  662. continue;
  663. node = sn_hwperf_obj_to_cnode(objs + i);
  664. for_each_online_cpu(j) {
  665. if (node != cpu_to_node(j))
  666. continue;
  667. cpuobj = (struct sn_hwperf_object_info *) p + cpuobj_index++;
  668. slice = 'a' + cpuid_to_slice(j);
  669. cdata = cpu_data(j);
  670. cpuobj->id = j;
  671. snprintf(cpuobj->name,
  672. sizeof(cpuobj->name),
  673. "CPU %luMHz %s",
  674. cdata->proc_freq / 1000000,
  675. cdata->vendor);
  676. snprintf(cpuobj->location,
  677. sizeof(cpuobj->location),
  678. "%s%c", objs[i].location,
  679. slice);
  680. }
  681. }
  682. vfree(objs);
  683. }
  684. break;
  685. case SN_HWPERF_GET_NODE_NASID:
  686. if (a.sz != sizeof(u64) ||
  687. (node = a.arg) < 0 || !cnode_possible(node)) {
  688. r = -EINVAL;
  689. goto error;
  690. }
  691. *(u64 *)p = (u64)cnodeid_to_nasid(node);
  692. break;
  693. case SN_HWPERF_GET_OBJ_NODE:
  694. i = a.arg;
  695. if (a.sz != sizeof(u64) || i < 0) {
  696. r = -EINVAL;
  697. goto error;
  698. }
  699. if ((r = sn_hwperf_enum_objects(&nobj, &objs)) == 0) {
  700. if (i >= nobj) {
  701. r = -EINVAL;
  702. vfree(objs);
  703. goto error;
  704. }
  705. if (objs[i].id != a.arg) {
  706. for (i = 0; i < nobj; i++) {
  707. if (objs[i].id == a.arg)
  708. break;
  709. }
  710. }
  711. if (i == nobj) {
  712. r = -EINVAL;
  713. vfree(objs);
  714. goto error;
  715. }
  716. if (!SN_HWPERF_IS_NODE(objs + i) &&
  717. !SN_HWPERF_IS_IONODE(objs + i)) {
  718. r = -ENOENT;
  719. vfree(objs);
  720. goto error;
  721. }
  722. *(u64 *)p = (u64)sn_hwperf_obj_to_cnode(objs + i);
  723. vfree(objs);
  724. }
  725. break;
  726. case SN_HWPERF_GET_MMRS:
  727. case SN_HWPERF_SET_MMRS:
  728. case SN_HWPERF_OBJECT_DISTANCE:
  729. op_info.p = p;
  730. op_info.a = &a;
  731. op_info.v0 = &v0;
  732. op_info.op = op;
  733. r = sn_hwperf_op_cpu(&op_info);
  734. if (r) {
  735. r = sn_hwperf_map_err(r);
  736. a.v0 = v0;
  737. goto error;
  738. }
  739. break;
  740. default:
  741. /* all other ops are a direct SAL call */
  742. r = ia64_sn_hwperf_op(sn_hwperf_master_nasid, op,
  743. a.arg, a.sz, (u64) p, 0, 0, &v0);
  744. if (r) {
  745. r = sn_hwperf_map_err(r);
  746. goto error;
  747. }
  748. a.v0 = v0;
  749. break;
  750. }
  751. if (op & SN_HWPERF_OP_MEM_COPYOUT) {
  752. r = copy_to_user((void __user *)a.ptr, p, a.sz);
  753. if (r != 0) {
  754. r = -EFAULT;
  755. goto error;
  756. }
  757. }
  758. error:
  759. vfree(p);
  760. return r;
  761. }
  762. static const struct file_operations sn_hwperf_fops = {
  763. .unlocked_ioctl = sn_hwperf_ioctl,
  764. .llseek = noop_llseek,
  765. };
  766. static struct miscdevice sn_hwperf_dev = {
  767. MISC_DYNAMIC_MINOR,
  768. "sn_hwperf",
  769. &sn_hwperf_fops
  770. };
  771. static int sn_hwperf_init(void)
  772. {
  773. u64 v;
  774. int salr;
  775. int e = 0;
  776. /* single threaded, once-only initialization */
  777. mutex_lock(&sn_hwperf_init_mutex);
  778. if (sn_hwperf_salheap) {
  779. mutex_unlock(&sn_hwperf_init_mutex);
  780. return e;
  781. }
  782. /*
  783. * The PROM code needs a fixed reference node. For convenience the
  784. * same node as the console I/O is used.
  785. */
  786. sn_hwperf_master_nasid = (nasid_t) ia64_sn_get_console_nasid();
  787. /*
  788. * Request the needed size and install the PROM scratch area.
  789. * The PROM keeps various tracking bits in this memory area.
  790. */
  791. salr = ia64_sn_hwperf_op(sn_hwperf_master_nasid,
  792. (u64) SN_HWPERF_GET_HEAPSIZE, 0,
  793. (u64) sizeof(u64), (u64) &v, 0, 0, NULL);
  794. if (salr != SN_HWPERF_OP_OK) {
  795. e = -EINVAL;
  796. goto out;
  797. }
  798. if ((sn_hwperf_salheap = vmalloc(v)) == NULL) {
  799. e = -ENOMEM;
  800. goto out;
  801. }
  802. salr = ia64_sn_hwperf_op(sn_hwperf_master_nasid,
  803. SN_HWPERF_INSTALL_HEAP, 0, v,
  804. (u64) sn_hwperf_salheap, 0, 0, NULL);
  805. if (salr != SN_HWPERF_OP_OK) {
  806. e = -EINVAL;
  807. goto out;
  808. }
  809. salr = ia64_sn_hwperf_op(sn_hwperf_master_nasid,
  810. SN_HWPERF_OBJECT_COUNT, 0,
  811. sizeof(u64), (u64) &v, 0, 0, NULL);
  812. if (salr != SN_HWPERF_OP_OK) {
  813. e = -EINVAL;
  814. goto out;
  815. }
  816. sn_hwperf_obj_cnt = (int)v;
  817. out:
  818. if (e < 0 && sn_hwperf_salheap) {
  819. vfree(sn_hwperf_salheap);
  820. sn_hwperf_salheap = NULL;
  821. sn_hwperf_obj_cnt = 0;
  822. }
  823. mutex_unlock(&sn_hwperf_init_mutex);
  824. return e;
  825. }
  826. int sn_topology_open(struct inode *inode, struct file *file)
  827. {
  828. int e;
  829. struct seq_file *seq;
  830. struct sn_hwperf_object_info *objbuf;
  831. int nobj;
  832. if ((e = sn_hwperf_enum_objects(&nobj, &objbuf)) == 0) {
  833. e = seq_open(file, &sn_topology_seq_ops);
  834. seq = file->private_data;
  835. seq->private = objbuf;
  836. }
  837. return e;
  838. }
  839. int sn_topology_release(struct inode *inode, struct file *file)
  840. {
  841. struct seq_file *seq = file->private_data;
  842. vfree(seq->private);
  843. return seq_release(inode, file);
  844. }
  845. int sn_hwperf_get_nearest_node(cnodeid_t node,
  846. cnodeid_t *near_mem_node, cnodeid_t *near_cpu_node)
  847. {
  848. int e;
  849. int nobj;
  850. struct sn_hwperf_object_info *objbuf;
  851. if ((e = sn_hwperf_enum_objects(&nobj, &objbuf)) == 0) {
  852. e = sn_hwperf_get_nearest_node_objdata(objbuf, nobj,
  853. node, near_mem_node, near_cpu_node);
  854. vfree(objbuf);
  855. }
  856. return e;
  857. }
  858. static int __devinit sn_hwperf_misc_register_init(void)
  859. {
  860. int e;
  861. if (!ia64_platform_is("sn2"))
  862. return 0;
  863. sn_hwperf_init();
  864. /*
  865. * Register a dynamic misc device for hwperf ioctls. Platforms
  866. * supporting hotplug will create /dev/sn_hwperf, else user
  867. * can to look up the minor number in /proc/misc.
  868. */
  869. if ((e = misc_register(&sn_hwperf_dev)) != 0) {
  870. printk(KERN_ERR "sn_hwperf_misc_register_init: failed to "
  871. "register misc device for \"%s\"\n", sn_hwperf_dev.name);
  872. }
  873. return e;
  874. }
  875. device_initcall(sn_hwperf_misc_register_init); /* after misc_init() */
  876. EXPORT_SYMBOL(sn_hwperf_get_nearest_node);