sn_hwperf.c 23 KB

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