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/smp_lock.h>
  33. #include <linux/nodemask.h>
  34. #include <linux/smp.h>
  35. #include <linux/mutex.h>
  36. #include <asm/processor.h>
  37. #include <asm/topology.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 DEFINE_MUTEX(sn_hwperf_init_mutex);
  51. #define cnode_possible(n) ((n) < num_cnodes)
  52. static int sn_hwperf_enum_objects(int *nobj, struct sn_hwperf_object_info **ret)
  53. {
  54. int e;
  55. u64 sz;
  56. struct sn_hwperf_object_info *objbuf = NULL;
  57. if ((e = sn_hwperf_init()) < 0) {
  58. printk(KERN_ERR "sn_hwperf_init failed: err %d\n", e);
  59. goto out;
  60. }
  61. sz = sn_hwperf_obj_cnt * sizeof(struct sn_hwperf_object_info);
  62. objbuf = vmalloc(sz);
  63. if (objbuf == NULL) {
  64. printk("sn_hwperf_enum_objects: vmalloc(%d) failed\n", (int)sz);
  65. e = -ENOMEM;
  66. goto out;
  67. }
  68. e = ia64_sn_hwperf_op(sn_hwperf_master_nasid, SN_HWPERF_ENUM_OBJECTS,
  69. 0, sz, (u64) objbuf, 0, 0, NULL);
  70. if (e != SN_HWPERF_OP_OK) {
  71. e = -EINVAL;
  72. vfree(objbuf);
  73. }
  74. out:
  75. *nobj = sn_hwperf_obj_cnt;
  76. *ret = objbuf;
  77. return e;
  78. }
  79. static int sn_hwperf_location_to_bpos(char *location,
  80. int *rack, int *bay, int *slot, int *slab)
  81. {
  82. char type;
  83. /* first scan for an old style geoid string */
  84. if (sscanf(location, "%03d%c%02d#%d",
  85. rack, &type, bay, slab) == 4)
  86. *slot = 0;
  87. else /* scan for a new bladed geoid string */
  88. if (sscanf(location, "%03d%c%02d^%02d#%d",
  89. rack, &type, bay, slot, slab) != 5)
  90. return -1;
  91. /* success */
  92. return 0;
  93. }
  94. static int sn_hwperf_geoid_to_cnode(char *location)
  95. {
  96. int cnode;
  97. geoid_t geoid;
  98. moduleid_t module_id;
  99. int rack, bay, slot, slab;
  100. int this_rack, this_bay, this_slot, this_slab;
  101. if (sn_hwperf_location_to_bpos(location, &rack, &bay, &slot, &slab))
  102. return -1;
  103. /*
  104. * FIXME: replace with cleaner for_each_XXX macro which addresses
  105. * both compute and IO nodes once ACPI3.0 is available.
  106. */
  107. for (cnode = 0; cnode < num_cnodes; cnode++) {
  108. geoid = cnodeid_get_geoid(cnode);
  109. module_id = geo_module(geoid);
  110. this_rack = MODULE_GET_RACK(module_id);
  111. this_bay = MODULE_GET_BPOS(module_id);
  112. this_slot = geo_slot(geoid);
  113. this_slab = geo_slab(geoid);
  114. if (rack == this_rack && bay == this_bay &&
  115. slot == this_slot && slab == this_slab) {
  116. break;
  117. }
  118. }
  119. return cnode_possible(cnode) ? cnode : -1;
  120. }
  121. static int sn_hwperf_obj_to_cnode(struct sn_hwperf_object_info * obj)
  122. {
  123. if (!SN_HWPERF_IS_NODE(obj) && !SN_HWPERF_IS_IONODE(obj))
  124. BUG();
  125. if (SN_HWPERF_FOREIGN(obj))
  126. return -1;
  127. return sn_hwperf_geoid_to_cnode(obj->location);
  128. }
  129. static int sn_hwperf_generic_ordinal(struct sn_hwperf_object_info *obj,
  130. struct sn_hwperf_object_info *objs)
  131. {
  132. int ordinal;
  133. struct sn_hwperf_object_info *p;
  134. for (ordinal=0, p=objs; p != obj; p++) {
  135. if (SN_HWPERF_FOREIGN(p))
  136. continue;
  137. if (SN_HWPERF_SAME_OBJTYPE(p, obj))
  138. ordinal++;
  139. }
  140. return ordinal;
  141. }
  142. static const char *slabname_node = "node"; /* SHub asic */
  143. static const char *slabname_ionode = "ionode"; /* TIO asic */
  144. static const char *slabname_router = "router"; /* NL3R or NL4R */
  145. static const char *slabname_other = "other"; /* unknown asic */
  146. static const char *sn_hwperf_get_slabname(struct sn_hwperf_object_info *obj,
  147. struct sn_hwperf_object_info *objs, int *ordinal)
  148. {
  149. int isnode;
  150. const char *slabname = slabname_other;
  151. if ((isnode = SN_HWPERF_IS_NODE(obj)) || SN_HWPERF_IS_IONODE(obj)) {
  152. slabname = isnode ? slabname_node : slabname_ionode;
  153. *ordinal = sn_hwperf_obj_to_cnode(obj);
  154. }
  155. else {
  156. *ordinal = sn_hwperf_generic_ordinal(obj, objs);
  157. if (SN_HWPERF_IS_ROUTER(obj))
  158. slabname = slabname_router;
  159. }
  160. return slabname;
  161. }
  162. static void print_pci_topology(struct seq_file *s)
  163. {
  164. char *p;
  165. size_t sz;
  166. int e;
  167. for (sz = PAGE_SIZE; sz < 16 * PAGE_SIZE; sz += PAGE_SIZE) {
  168. if (!(p = kmalloc(sz, GFP_KERNEL)))
  169. break;
  170. e = ia64_sn_ioif_get_pci_topology(__pa(p), sz);
  171. if (e == SALRET_OK)
  172. seq_puts(s, p);
  173. kfree(p);
  174. if (e == SALRET_OK || e == SALRET_NOT_IMPLEMENTED)
  175. break;
  176. }
  177. }
  178. static inline int sn_hwperf_has_cpus(cnodeid_t node)
  179. {
  180. return node < MAX_NUMNODES && node_online(node) && nr_cpus_node(node);
  181. }
  182. static inline int sn_hwperf_has_mem(cnodeid_t node)
  183. {
  184. return node < MAX_NUMNODES && node_online(node) && NODE_DATA(node)->node_present_pages;
  185. }
  186. static struct sn_hwperf_object_info *
  187. sn_hwperf_findobj_id(struct sn_hwperf_object_info *objbuf,
  188. int nobj, int id)
  189. {
  190. int i;
  191. struct sn_hwperf_object_info *p = objbuf;
  192. for (i=0; i < nobj; i++, p++) {
  193. if (p->id == id)
  194. return p;
  195. }
  196. return NULL;
  197. }
  198. static int sn_hwperf_get_nearest_node_objdata(struct sn_hwperf_object_info *objbuf,
  199. int nobj, cnodeid_t node, cnodeid_t *near_mem_node, cnodeid_t *near_cpu_node)
  200. {
  201. int e;
  202. struct sn_hwperf_object_info *nodeobj = NULL;
  203. struct sn_hwperf_object_info *op;
  204. struct sn_hwperf_object_info *dest;
  205. struct sn_hwperf_object_info *router;
  206. struct sn_hwperf_port_info ptdata[16];
  207. int sz, i, j;
  208. cnodeid_t c;
  209. int found_mem = 0;
  210. int found_cpu = 0;
  211. if (!cnode_possible(node))
  212. return -EINVAL;
  213. if (sn_hwperf_has_cpus(node)) {
  214. if (near_cpu_node)
  215. *near_cpu_node = node;
  216. found_cpu++;
  217. }
  218. if (sn_hwperf_has_mem(node)) {
  219. if (near_mem_node)
  220. *near_mem_node = node;
  221. found_mem++;
  222. }
  223. if (found_cpu && found_mem)
  224. return 0; /* trivially successful */
  225. /* find the argument node object */
  226. for (i=0, op=objbuf; i < nobj; i++, op++) {
  227. if (!SN_HWPERF_IS_NODE(op) && !SN_HWPERF_IS_IONODE(op))
  228. continue;
  229. if (node == sn_hwperf_obj_to_cnode(op)) {
  230. nodeobj = op;
  231. break;
  232. }
  233. }
  234. if (!nodeobj) {
  235. e = -ENOENT;
  236. goto err;
  237. }
  238. /* get it's interconnect topology */
  239. sz = op->ports * sizeof(struct sn_hwperf_port_info);
  240. BUG_ON(sz > sizeof(ptdata));
  241. e = ia64_sn_hwperf_op(sn_hwperf_master_nasid,
  242. SN_HWPERF_ENUM_PORTS, nodeobj->id, sz,
  243. (u64)&ptdata, 0, 0, NULL);
  244. if (e != SN_HWPERF_OP_OK) {
  245. e = -EINVAL;
  246. goto err;
  247. }
  248. /* find nearest node with cpus and nearest memory */
  249. for (router=NULL, j=0; j < op->ports; j++) {
  250. dest = sn_hwperf_findobj_id(objbuf, nobj, ptdata[j].conn_id);
  251. if (dest && SN_HWPERF_IS_ROUTER(dest))
  252. router = dest;
  253. if (!dest || SN_HWPERF_FOREIGN(dest) ||
  254. !SN_HWPERF_IS_NODE(dest) || SN_HWPERF_IS_IONODE(dest)) {
  255. continue;
  256. }
  257. c = sn_hwperf_obj_to_cnode(dest);
  258. if (!found_cpu && sn_hwperf_has_cpus(c)) {
  259. if (near_cpu_node)
  260. *near_cpu_node = c;
  261. found_cpu++;
  262. }
  263. if (!found_mem && sn_hwperf_has_mem(c)) {
  264. if (near_mem_node)
  265. *near_mem_node = c;
  266. found_mem++;
  267. }
  268. }
  269. if (router && (!found_cpu || !found_mem)) {
  270. /* search for a node connected to the same router */
  271. sz = router->ports * sizeof(struct sn_hwperf_port_info);
  272. BUG_ON(sz > sizeof(ptdata));
  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. char slice;
  342. struct cpuinfo_ia64 *c;
  343. struct sn_hwperf_port_info *ptdata;
  344. struct sn_hwperf_object_info *p;
  345. struct sn_hwperf_object_info *obj = d; /* this object */
  346. struct sn_hwperf_object_info *objs = s->private; /* all objects */
  347. u8 shubtype;
  348. u8 system_size;
  349. u8 sharing_size;
  350. u8 partid;
  351. u8 coher;
  352. u8 nasid_shift;
  353. u8 region_size;
  354. u16 nasid_mask;
  355. int nasid_msb;
  356. if (obj == objs) {
  357. seq_printf(s, "# sn_topology version 2\n");
  358. seq_printf(s, "# objtype ordinal location partition"
  359. " [attribute value [, ...]]\n");
  360. if (ia64_sn_get_sn_info(0,
  361. &shubtype, &nasid_mask, &nasid_shift, &system_size,
  362. &sharing_size, &partid, &coher, &region_size))
  363. BUG();
  364. for (nasid_msb=63; nasid_msb > 0; nasid_msb--) {
  365. if (((u64)nasid_mask << nasid_shift) & (1ULL << nasid_msb))
  366. break;
  367. }
  368. seq_printf(s, "partition %u %s local "
  369. "shubtype %s, "
  370. "nasid_mask 0x%016llx, "
  371. "nasid_bits %d:%d, "
  372. "system_size %d, "
  373. "sharing_size %d, "
  374. "coherency_domain %d, "
  375. "region_size %d\n",
  376. partid, utsname()->nodename,
  377. shubtype ? "shub2" : "shub1",
  378. (u64)nasid_mask << nasid_shift, nasid_msb, nasid_shift,
  379. system_size, sharing_size, coher, region_size);
  380. print_pci_topology(s);
  381. }
  382. if (SN_HWPERF_FOREIGN(obj)) {
  383. /* private in another partition: not interesting */
  384. return 0;
  385. }
  386. for (i = 0; i < SN_HWPERF_MAXSTRING && obj->name[i]; i++) {
  387. if (obj->name[i] == ' ')
  388. obj->name[i] = '_';
  389. }
  390. slabname = sn_hwperf_get_slabname(obj, objs, &ordinal);
  391. seq_printf(s, "%s %d %s %s asic %s", slabname, ordinal, obj->location,
  392. obj->sn_hwp_this_part ? "local" : "shared", obj->name);
  393. if (ordinal < 0 || (!SN_HWPERF_IS_NODE(obj) && !SN_HWPERF_IS_IONODE(obj)))
  394. seq_putc(s, '\n');
  395. else {
  396. cnodeid_t near_mem = -1;
  397. cnodeid_t near_cpu = -1;
  398. seq_printf(s, ", nasid 0x%x", cnodeid_to_nasid(ordinal));
  399. if (sn_hwperf_get_nearest_node_objdata(objs, sn_hwperf_obj_cnt,
  400. ordinal, &near_mem, &near_cpu) == 0) {
  401. seq_printf(s, ", near_mem_nodeid %d, near_cpu_nodeid %d",
  402. near_mem, near_cpu);
  403. }
  404. if (!SN_HWPERF_IS_IONODE(obj)) {
  405. for_each_online_node(i) {
  406. seq_printf(s, i ? ":%d" : ", dist %d",
  407. node_distance(ordinal, i));
  408. }
  409. }
  410. seq_putc(s, '\n');
  411. /*
  412. * CPUs on this node, if any
  413. */
  414. if (!SN_HWPERF_IS_IONODE(obj)) {
  415. for_each_cpu_and(i, cpu_online_mask,
  416. cpumask_of_node(ordinal)) {
  417. slice = 'a' + cpuid_to_slice(i);
  418. c = cpu_data(i);
  419. seq_printf(s, "cpu %d %s%c local"
  420. " freq %luMHz, arch ia64",
  421. i, obj->location, slice,
  422. c->proc_freq / 1000000);
  423. for_each_online_cpu(j) {
  424. seq_printf(s, j ? ":%d" : ", dist %d",
  425. node_distance(
  426. cpu_to_node(i),
  427. cpu_to_node(j)));
  428. }
  429. seq_putc(s, '\n');
  430. }
  431. }
  432. }
  433. if (obj->ports) {
  434. /*
  435. * numalink ports
  436. */
  437. sz = obj->ports * sizeof(struct sn_hwperf_port_info);
  438. if ((ptdata = kmalloc(sz, GFP_KERNEL)) == NULL)
  439. return -ENOMEM;
  440. e = ia64_sn_hwperf_op(sn_hwperf_master_nasid,
  441. SN_HWPERF_ENUM_PORTS, obj->id, sz,
  442. (u64) ptdata, 0, 0, NULL);
  443. if (e != SN_HWPERF_OP_OK)
  444. return -EINVAL;
  445. for (ordinal=0, p=objs; p != obj; p++) {
  446. if (!SN_HWPERF_FOREIGN(p))
  447. ordinal += p->ports;
  448. }
  449. for (pt = 0; pt < obj->ports; pt++) {
  450. for (p = objs, i = 0; i < sn_hwperf_obj_cnt; i++, p++) {
  451. if (ptdata[pt].conn_id == p->id) {
  452. break;
  453. }
  454. }
  455. seq_printf(s, "numalink %d %s-%d",
  456. ordinal+pt, obj->location, ptdata[pt].port);
  457. if (i >= sn_hwperf_obj_cnt) {
  458. /* no connection */
  459. seq_puts(s, " local endpoint disconnected"
  460. ", protocol unknown\n");
  461. continue;
  462. }
  463. if (obj->sn_hwp_this_part && p->sn_hwp_this_part)
  464. /* both ends local to this partition */
  465. seq_puts(s, " local");
  466. else if (SN_HWPERF_FOREIGN(p))
  467. /* both ends of the link in foreign partiton */
  468. seq_puts(s, " foreign");
  469. else
  470. /* link straddles a partition */
  471. seq_puts(s, " shared");
  472. /*
  473. * Unlikely, but strictly should query the LLP config
  474. * registers because an NL4R can be configured to run
  475. * NL3 protocol, even when not talking to an NL3 router.
  476. * Ditto for node-node.
  477. */
  478. seq_printf(s, " endpoint %s-%d, protocol %s\n",
  479. p->location, ptdata[pt].conn_port,
  480. (SN_HWPERF_IS_NL3ROUTER(obj) ||
  481. SN_HWPERF_IS_NL3ROUTER(p)) ? "LLP3" : "LLP4");
  482. }
  483. kfree(ptdata);
  484. }
  485. return 0;
  486. }
  487. static void *sn_topology_start(struct seq_file *s, loff_t * pos)
  488. {
  489. struct sn_hwperf_object_info *objs = s->private;
  490. if (*pos < sn_hwperf_obj_cnt)
  491. return (void *)(objs + *pos);
  492. return NULL;
  493. }
  494. static void *sn_topology_next(struct seq_file *s, void *v, loff_t * pos)
  495. {
  496. ++*pos;
  497. return sn_topology_start(s, pos);
  498. }
  499. static void sn_topology_stop(struct seq_file *m, void *v)
  500. {
  501. return;
  502. }
  503. /*
  504. * /proc/sgi_sn/sn_topology, read-only using seq_file
  505. */
  506. static const struct seq_operations sn_topology_seq_ops = {
  507. .start = sn_topology_start,
  508. .next = sn_topology_next,
  509. .stop = sn_topology_stop,
  510. .show = sn_topology_show
  511. };
  512. struct sn_hwperf_op_info {
  513. u64 op;
  514. struct sn_hwperf_ioctl_args *a;
  515. void *p;
  516. int *v0;
  517. int ret;
  518. };
  519. static void sn_hwperf_call_sal(void *info)
  520. {
  521. struct sn_hwperf_op_info *op_info = info;
  522. int r;
  523. r = ia64_sn_hwperf_op(sn_hwperf_master_nasid, op_info->op,
  524. op_info->a->arg, op_info->a->sz,
  525. (u64) op_info->p, 0, 0, op_info->v0);
  526. op_info->ret = r;
  527. }
  528. static int sn_hwperf_op_cpu(struct sn_hwperf_op_info *op_info)
  529. {
  530. u32 cpu;
  531. u32 use_ipi;
  532. int r = 0;
  533. cpumask_t save_allowed;
  534. cpu = (op_info->a->arg & SN_HWPERF_ARG_CPU_MASK) >> 32;
  535. use_ipi = op_info->a->arg & SN_HWPERF_ARG_USE_IPI_MASK;
  536. op_info->a->arg &= SN_HWPERF_ARG_OBJID_MASK;
  537. if (cpu != SN_HWPERF_ARG_ANY_CPU) {
  538. if (cpu >= nr_cpu_ids || !cpu_online(cpu)) {
  539. r = -EINVAL;
  540. goto out;
  541. }
  542. }
  543. if (cpu == SN_HWPERF_ARG_ANY_CPU || cpu == get_cpu()) {
  544. /* don't care, or already on correct cpu */
  545. sn_hwperf_call_sal(op_info);
  546. }
  547. else {
  548. if (use_ipi) {
  549. /* use an interprocessor interrupt to call SAL */
  550. smp_call_function_single(cpu, sn_hwperf_call_sal,
  551. op_info, 1);
  552. }
  553. else {
  554. /* migrate the task before calling SAL */
  555. save_allowed = current->cpus_allowed;
  556. set_cpus_allowed(current, cpumask_of_cpu(cpu));
  557. sn_hwperf_call_sal(op_info);
  558. set_cpus_allowed(current, save_allowed);
  559. }
  560. }
  561. r = op_info->ret;
  562. out:
  563. return r;
  564. }
  565. /* map SAL hwperf error code to system error code */
  566. static int sn_hwperf_map_err(int hwperf_err)
  567. {
  568. int e;
  569. switch(hwperf_err) {
  570. case SN_HWPERF_OP_OK:
  571. e = 0;
  572. break;
  573. case SN_HWPERF_OP_NOMEM:
  574. e = -ENOMEM;
  575. break;
  576. case SN_HWPERF_OP_NO_PERM:
  577. e = -EPERM;
  578. break;
  579. case SN_HWPERF_OP_IO_ERROR:
  580. e = -EIO;
  581. break;
  582. case SN_HWPERF_OP_BUSY:
  583. e = -EBUSY;
  584. break;
  585. case SN_HWPERF_OP_RECONFIGURE:
  586. e = -EAGAIN;
  587. break;
  588. case SN_HWPERF_OP_INVAL:
  589. default:
  590. e = -EINVAL;
  591. break;
  592. }
  593. return e;
  594. }
  595. /*
  596. * ioctl for "sn_hwperf" misc device
  597. */
  598. static int
  599. sn_hwperf_ioctl(struct inode *in, struct file *fp, u32 op, unsigned long arg)
  600. {
  601. struct sn_hwperf_ioctl_args a;
  602. struct cpuinfo_ia64 *cdata;
  603. struct sn_hwperf_object_info *objs;
  604. struct sn_hwperf_object_info *cpuobj;
  605. struct sn_hwperf_op_info op_info;
  606. void *p = NULL;
  607. int nobj;
  608. char slice;
  609. int node;
  610. int r;
  611. int v0;
  612. int i;
  613. int j;
  614. unlock_kernel();
  615. /* only user requests are allowed here */
  616. if ((op & SN_HWPERF_OP_MASK) < 10) {
  617. r = -EINVAL;
  618. goto error;
  619. }
  620. r = copy_from_user(&a, (const void __user *)arg,
  621. sizeof(struct sn_hwperf_ioctl_args));
  622. if (r != 0) {
  623. r = -EFAULT;
  624. goto error;
  625. }
  626. /*
  627. * Allocate memory to hold a kernel copy of the user buffer. The
  628. * buffer contents are either copied in or out (or both) of user
  629. * space depending on the flags encoded in the requested operation.
  630. */
  631. if (a.ptr) {
  632. p = vmalloc(a.sz);
  633. if (!p) {
  634. r = -ENOMEM;
  635. goto error;
  636. }
  637. }
  638. if (op & SN_HWPERF_OP_MEM_COPYIN) {
  639. r = copy_from_user(p, (const void __user *)a.ptr, a.sz);
  640. if (r != 0) {
  641. r = -EFAULT;
  642. goto error;
  643. }
  644. }
  645. switch (op) {
  646. case SN_HWPERF_GET_CPU_INFO:
  647. if (a.sz == sizeof(u64)) {
  648. /* special case to get size needed */
  649. *(u64 *) p = (u64) num_online_cpus() *
  650. sizeof(struct sn_hwperf_object_info);
  651. } else
  652. if (a.sz < num_online_cpus() * sizeof(struct sn_hwperf_object_info)) {
  653. r = -ENOMEM;
  654. goto error;
  655. } else
  656. if ((r = sn_hwperf_enum_objects(&nobj, &objs)) == 0) {
  657. int cpuobj_index = 0;
  658. memset(p, 0, a.sz);
  659. for (i = 0; i < nobj; i++) {
  660. if (!SN_HWPERF_IS_NODE(objs + i))
  661. continue;
  662. node = sn_hwperf_obj_to_cnode(objs + i);
  663. for_each_online_cpu(j) {
  664. if (node != cpu_to_node(j))
  665. continue;
  666. cpuobj = (struct sn_hwperf_object_info *) p + cpuobj_index++;
  667. slice = 'a' + cpuid_to_slice(j);
  668. cdata = cpu_data(j);
  669. cpuobj->id = j;
  670. snprintf(cpuobj->name,
  671. sizeof(cpuobj->name),
  672. "CPU %luMHz %s",
  673. cdata->proc_freq / 1000000,
  674. cdata->vendor);
  675. snprintf(cpuobj->location,
  676. sizeof(cpuobj->location),
  677. "%s%c", objs[i].location,
  678. slice);
  679. }
  680. }
  681. vfree(objs);
  682. }
  683. break;
  684. case SN_HWPERF_GET_NODE_NASID:
  685. if (a.sz != sizeof(u64) ||
  686. (node = a.arg) < 0 || !cnode_possible(node)) {
  687. r = -EINVAL;
  688. goto error;
  689. }
  690. *(u64 *)p = (u64)cnodeid_to_nasid(node);
  691. break;
  692. case SN_HWPERF_GET_OBJ_NODE:
  693. i = a.arg;
  694. if (a.sz != sizeof(u64) || i < 0) {
  695. r = -EINVAL;
  696. goto error;
  697. }
  698. if ((r = sn_hwperf_enum_objects(&nobj, &objs)) == 0) {
  699. if (i >= nobj) {
  700. r = -EINVAL;
  701. vfree(objs);
  702. goto error;
  703. }
  704. if (objs[i].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 const 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. mutex_lock(&sn_hwperf_init_mutex);
  777. if (sn_hwperf_salheap) {
  778. mutex_unlock(&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. mutex_unlock(&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);