sn_hwperf.c 23 KB

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