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/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 <asm/processor.h>
  36. #include <asm/topology.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. #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. if ((objbuf = (struct sn_hwperf_object_info *) vmalloc(sz)) == 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. if (sz > sizeof(ptdata))
  240. BUG();
  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. if (sz > sizeof(ptdata))
  273. BUG();
  274. e = ia64_sn_hwperf_op(sn_hwperf_master_nasid,
  275. SN_HWPERF_ENUM_PORTS, router->id, sz,
  276. (u64)&ptdata, 0, 0, NULL);
  277. if (e != SN_HWPERF_OP_OK) {
  278. e = -EINVAL;
  279. goto err;
  280. }
  281. for (j=0; j < router->ports; j++) {
  282. dest = sn_hwperf_findobj_id(objbuf, nobj,
  283. ptdata[j].conn_id);
  284. if (!dest || dest->id == node ||
  285. SN_HWPERF_FOREIGN(dest) ||
  286. !SN_HWPERF_IS_NODE(dest) ||
  287. SN_HWPERF_IS_IONODE(dest)) {
  288. continue;
  289. }
  290. c = sn_hwperf_obj_to_cnode(dest);
  291. if (!found_cpu && sn_hwperf_has_cpus(c)) {
  292. if (near_cpu_node)
  293. *near_cpu_node = c;
  294. found_cpu++;
  295. }
  296. if (!found_mem && sn_hwperf_has_mem(c)) {
  297. if (near_mem_node)
  298. *near_mem_node = c;
  299. found_mem++;
  300. }
  301. if (found_cpu && found_mem)
  302. break;
  303. }
  304. }
  305. if (!found_cpu || !found_mem) {
  306. /* resort to _any_ node with CPUs and memory */
  307. for (i=0, op=objbuf; i < nobj; i++, op++) {
  308. if (SN_HWPERF_FOREIGN(op) ||
  309. SN_HWPERF_IS_IONODE(op) ||
  310. !SN_HWPERF_IS_NODE(op)) {
  311. continue;
  312. }
  313. c = sn_hwperf_obj_to_cnode(op);
  314. if (!found_cpu && sn_hwperf_has_cpus(c)) {
  315. if (near_cpu_node)
  316. *near_cpu_node = c;
  317. found_cpu++;
  318. }
  319. if (!found_mem && sn_hwperf_has_mem(c)) {
  320. if (near_mem_node)
  321. *near_mem_node = c;
  322. found_mem++;
  323. }
  324. if (found_cpu && found_mem)
  325. break;
  326. }
  327. }
  328. if (!found_cpu || !found_mem)
  329. e = -ENODATA;
  330. err:
  331. return e;
  332. }
  333. static int sn_topology_show(struct seq_file *s, void *d)
  334. {
  335. int sz;
  336. int pt;
  337. int e = 0;
  338. int i;
  339. int j;
  340. const char *slabname;
  341. int ordinal;
  342. cpumask_t cpumask;
  343. char slice;
  344. struct cpuinfo_ia64 *c;
  345. struct sn_hwperf_port_info *ptdata;
  346. struct sn_hwperf_object_info *p;
  347. struct sn_hwperf_object_info *obj = d; /* this object */
  348. struct sn_hwperf_object_info *objs = s->private; /* all objects */
  349. u8 shubtype;
  350. u8 system_size;
  351. u8 sharing_size;
  352. u8 partid;
  353. u8 coher;
  354. u8 nasid_shift;
  355. u8 region_size;
  356. u16 nasid_mask;
  357. int nasid_msb;
  358. if (obj == objs) {
  359. seq_printf(s, "# sn_topology version 2\n");
  360. seq_printf(s, "# objtype ordinal location partition"
  361. " [attribute value [, ...]]\n");
  362. if (ia64_sn_get_sn_info(0,
  363. &shubtype, &nasid_mask, &nasid_shift, &system_size,
  364. &sharing_size, &partid, &coher, &region_size))
  365. BUG();
  366. for (nasid_msb=63; nasid_msb > 0; nasid_msb--) {
  367. if (((u64)nasid_mask << nasid_shift) & (1ULL << nasid_msb))
  368. break;
  369. }
  370. seq_printf(s, "partition %u %s local "
  371. "shubtype %s, "
  372. "nasid_mask 0x%016lx, "
  373. "nasid_bits %d:%d, "
  374. "system_size %d, "
  375. "sharing_size %d, "
  376. "coherency_domain %d, "
  377. "region_size %d\n",
  378. partid, utsname()->nodename,
  379. shubtype ? "shub2" : "shub1",
  380. (u64)nasid_mask << nasid_shift, nasid_msb, nasid_shift,
  381. system_size, sharing_size, coher, region_size);
  382. print_pci_topology(s);
  383. }
  384. if (SN_HWPERF_FOREIGN(obj)) {
  385. /* private in another partition: not interesting */
  386. return 0;
  387. }
  388. for (i = 0; i < SN_HWPERF_MAXSTRING && obj->name[i]; i++) {
  389. if (obj->name[i] == ' ')
  390. obj->name[i] = '_';
  391. }
  392. slabname = sn_hwperf_get_slabname(obj, objs, &ordinal);
  393. seq_printf(s, "%s %d %s %s asic %s", slabname, ordinal, obj->location,
  394. obj->sn_hwp_this_part ? "local" : "shared", obj->name);
  395. if (ordinal < 0 || (!SN_HWPERF_IS_NODE(obj) && !SN_HWPERF_IS_IONODE(obj)))
  396. seq_putc(s, '\n');
  397. else {
  398. cnodeid_t near_mem = -1;
  399. cnodeid_t near_cpu = -1;
  400. seq_printf(s, ", nasid 0x%x", cnodeid_to_nasid(ordinal));
  401. if (sn_hwperf_get_nearest_node_objdata(objs, sn_hwperf_obj_cnt,
  402. ordinal, &near_mem, &near_cpu) == 0) {
  403. seq_printf(s, ", near_mem_nodeid %d, near_cpu_nodeid %d",
  404. near_mem, near_cpu);
  405. }
  406. if (!SN_HWPERF_IS_IONODE(obj)) {
  407. for_each_online_node(i) {
  408. seq_printf(s, i ? ":%d" : ", dist %d",
  409. node_distance(ordinal, i));
  410. }
  411. }
  412. seq_putc(s, '\n');
  413. /*
  414. * CPUs on this node, if any
  415. */
  416. if (!SN_HWPERF_IS_IONODE(obj)) {
  417. cpumask = node_to_cpumask(ordinal);
  418. for_each_online_cpu(i) {
  419. if (cpu_isset(i, cpumask)) {
  420. slice = 'a' + cpuid_to_slice(i);
  421. c = cpu_data(i);
  422. seq_printf(s, "cpu %d %s%c local"
  423. " freq %luMHz, arch ia64",
  424. i, obj->location, slice,
  425. c->proc_freq / 1000000);
  426. for_each_online_cpu(j) {
  427. seq_printf(s, j ? ":%d" : ", dist %d",
  428. node_distance(
  429. cpu_to_node(i),
  430. cpu_to_node(j)));
  431. }
  432. seq_putc(s, '\n');
  433. }
  434. }
  435. }
  436. }
  437. if (obj->ports) {
  438. /*
  439. * numalink ports
  440. */
  441. sz = obj->ports * sizeof(struct sn_hwperf_port_info);
  442. if ((ptdata = kmalloc(sz, GFP_KERNEL)) == NULL)
  443. return -ENOMEM;
  444. e = ia64_sn_hwperf_op(sn_hwperf_master_nasid,
  445. SN_HWPERF_ENUM_PORTS, obj->id, sz,
  446. (u64) ptdata, 0, 0, NULL);
  447. if (e != SN_HWPERF_OP_OK)
  448. return -EINVAL;
  449. for (ordinal=0, p=objs; p != obj; p++) {
  450. if (!SN_HWPERF_FOREIGN(p))
  451. ordinal += p->ports;
  452. }
  453. for (pt = 0; pt < obj->ports; pt++) {
  454. for (p = objs, i = 0; i < sn_hwperf_obj_cnt; i++, p++) {
  455. if (ptdata[pt].conn_id == p->id) {
  456. break;
  457. }
  458. }
  459. seq_printf(s, "numalink %d %s-%d",
  460. ordinal+pt, obj->location, ptdata[pt].port);
  461. if (i >= sn_hwperf_obj_cnt) {
  462. /* no connection */
  463. seq_puts(s, " local endpoint disconnected"
  464. ", protocol unknown\n");
  465. continue;
  466. }
  467. if (obj->sn_hwp_this_part && p->sn_hwp_this_part)
  468. /* both ends local to this partition */
  469. seq_puts(s, " local");
  470. else if (SN_HWPERF_FOREIGN(p))
  471. /* both ends of the link in foreign partiton */
  472. seq_puts(s, " foreign");
  473. else
  474. /* link straddles a partition */
  475. seq_puts(s, " shared");
  476. /*
  477. * Unlikely, but strictly should query the LLP config
  478. * registers because an NL4R can be configured to run
  479. * NL3 protocol, even when not talking to an NL3 router.
  480. * Ditto for node-node.
  481. */
  482. seq_printf(s, " endpoint %s-%d, protocol %s\n",
  483. p->location, ptdata[pt].conn_port,
  484. (SN_HWPERF_IS_NL3ROUTER(obj) ||
  485. SN_HWPERF_IS_NL3ROUTER(p)) ? "LLP3" : "LLP4");
  486. }
  487. kfree(ptdata);
  488. }
  489. return 0;
  490. }
  491. static void *sn_topology_start(struct seq_file *s, loff_t * pos)
  492. {
  493. struct sn_hwperf_object_info *objs = s->private;
  494. if (*pos < sn_hwperf_obj_cnt)
  495. return (void *)(objs + *pos);
  496. return NULL;
  497. }
  498. static void *sn_topology_next(struct seq_file *s, void *v, loff_t * pos)
  499. {
  500. ++*pos;
  501. return sn_topology_start(s, pos);
  502. }
  503. static void sn_topology_stop(struct seq_file *m, void *v)
  504. {
  505. return;
  506. }
  507. /*
  508. * /proc/sgi_sn/sn_topology, read-only using seq_file
  509. */
  510. static struct seq_operations sn_topology_seq_ops = {
  511. .start = sn_topology_start,
  512. .next = sn_topology_next,
  513. .stop = sn_topology_stop,
  514. .show = sn_topology_show
  515. };
  516. struct sn_hwperf_op_info {
  517. u64 op;
  518. struct sn_hwperf_ioctl_args *a;
  519. void *p;
  520. int *v0;
  521. int ret;
  522. };
  523. static void sn_hwperf_call_sal(void *info)
  524. {
  525. struct sn_hwperf_op_info *op_info = info;
  526. int r;
  527. r = ia64_sn_hwperf_op(sn_hwperf_master_nasid, op_info->op,
  528. op_info->a->arg, op_info->a->sz,
  529. (u64) op_info->p, 0, 0, op_info->v0);
  530. op_info->ret = r;
  531. }
  532. static int sn_hwperf_op_cpu(struct sn_hwperf_op_info *op_info)
  533. {
  534. u32 cpu;
  535. u32 use_ipi;
  536. int r = 0;
  537. cpumask_t save_allowed;
  538. cpu = (op_info->a->arg & SN_HWPERF_ARG_CPU_MASK) >> 32;
  539. use_ipi = op_info->a->arg & SN_HWPERF_ARG_USE_IPI_MASK;
  540. op_info->a->arg &= SN_HWPERF_ARG_OBJID_MASK;
  541. if (cpu != SN_HWPERF_ARG_ANY_CPU) {
  542. if (cpu >= NR_CPUS || !cpu_online(cpu)) {
  543. r = -EINVAL;
  544. goto out;
  545. }
  546. }
  547. if (cpu == SN_HWPERF_ARG_ANY_CPU || cpu == get_cpu()) {
  548. /* don't care, or already on correct cpu */
  549. sn_hwperf_call_sal(op_info);
  550. }
  551. else {
  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, 1);
  556. }
  557. else {
  558. /* migrate the task before calling SAL */
  559. save_allowed = current->cpus_allowed;
  560. set_cpus_allowed(current, cpumask_of_cpu(cpu));
  561. sn_hwperf_call_sal(op_info);
  562. set_cpus_allowed(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 int
  603. sn_hwperf_ioctl(struct inode *in, struct file *fp, u32 op, u64 arg)
  604. {
  605. struct sn_hwperf_ioctl_args a;
  606. struct cpuinfo_ia64 *cdata;
  607. struct sn_hwperf_object_info *objs;
  608. struct sn_hwperf_object_info *cpuobj;
  609. struct sn_hwperf_op_info op_info;
  610. void *p = NULL;
  611. int nobj;
  612. char slice;
  613. int node;
  614. int r;
  615. int v0;
  616. int i;
  617. int j;
  618. unlock_kernel();
  619. /* only user requests are allowed here */
  620. if ((op & SN_HWPERF_OP_MASK) < 10) {
  621. r = -EINVAL;
  622. goto error;
  623. }
  624. r = copy_from_user(&a, (const void __user *)arg,
  625. sizeof(struct sn_hwperf_ioctl_args));
  626. if (r != 0) {
  627. r = -EFAULT;
  628. goto error;
  629. }
  630. /*
  631. * Allocate memory to hold a kernel copy of the user buffer. The
  632. * buffer contents are either copied in or out (or both) of user
  633. * space depending on the flags encoded in the requested operation.
  634. */
  635. if (a.ptr) {
  636. p = vmalloc(a.sz);
  637. if (!p) {
  638. r = -ENOMEM;
  639. goto error;
  640. }
  641. }
  642. if (op & SN_HWPERF_OP_MEM_COPYIN) {
  643. r = copy_from_user(p, (const void __user *)a.ptr, a.sz);
  644. if (r != 0) {
  645. r = -EFAULT;
  646. goto error;
  647. }
  648. }
  649. switch (op) {
  650. case SN_HWPERF_GET_CPU_INFO:
  651. if (a.sz == sizeof(u64)) {
  652. /* special case to get size needed */
  653. *(u64 *) p = (u64) num_online_cpus() *
  654. sizeof(struct sn_hwperf_object_info);
  655. } else
  656. if (a.sz < num_online_cpus() * sizeof(struct sn_hwperf_object_info)) {
  657. r = -ENOMEM;
  658. goto error;
  659. } else
  660. if ((r = sn_hwperf_enum_objects(&nobj, &objs)) == 0) {
  661. memset(p, 0, a.sz);
  662. for (i = 0; i < nobj; i++) {
  663. int cpuobj_index = 0;
  664. if (!SN_HWPERF_IS_NODE(objs + i))
  665. continue;
  666. node = sn_hwperf_obj_to_cnode(objs + i);
  667. for_each_online_cpu(j) {
  668. if (node != cpu_to_node(j))
  669. continue;
  670. cpuobj = (struct sn_hwperf_object_info *) p + cpuobj_index++;
  671. slice = 'a' + cpuid_to_slice(j);
  672. cdata = cpu_data(j);
  673. cpuobj->id = j;
  674. snprintf(cpuobj->name,
  675. sizeof(cpuobj->name),
  676. "CPU %luMHz %s",
  677. cdata->proc_freq / 1000000,
  678. cdata->vendor);
  679. snprintf(cpuobj->location,
  680. sizeof(cpuobj->location),
  681. "%s%c", objs[i].location,
  682. slice);
  683. }
  684. }
  685. vfree(objs);
  686. }
  687. break;
  688. case SN_HWPERF_GET_NODE_NASID:
  689. if (a.sz != sizeof(u64) ||
  690. (node = a.arg) < 0 || !cnode_possible(node)) {
  691. r = -EINVAL;
  692. goto error;
  693. }
  694. *(u64 *)p = (u64)cnodeid_to_nasid(node);
  695. break;
  696. case SN_HWPERF_GET_OBJ_NODE:
  697. if (a.sz != sizeof(u64) || a.arg < 0) {
  698. r = -EINVAL;
  699. goto error;
  700. }
  701. if ((r = sn_hwperf_enum_objects(&nobj, &objs)) == 0) {
  702. if (a.arg >= nobj) {
  703. r = -EINVAL;
  704. vfree(objs);
  705. goto error;
  706. }
  707. if (objs[(i = a.arg)].id != a.arg) {
  708. for (i = 0; i < nobj; i++) {
  709. if (objs[i].id == a.arg)
  710. break;
  711. }
  712. }
  713. if (i == nobj) {
  714. r = -EINVAL;
  715. vfree(objs);
  716. goto error;
  717. }
  718. if (!SN_HWPERF_IS_NODE(objs + i) &&
  719. !SN_HWPERF_IS_IONODE(objs + i)) {
  720. r = -ENOENT;
  721. vfree(objs);
  722. goto error;
  723. }
  724. *(u64 *)p = (u64)sn_hwperf_obj_to_cnode(objs + i);
  725. vfree(objs);
  726. }
  727. break;
  728. case SN_HWPERF_GET_MMRS:
  729. case SN_HWPERF_SET_MMRS:
  730. case SN_HWPERF_OBJECT_DISTANCE:
  731. op_info.p = p;
  732. op_info.a = &a;
  733. op_info.v0 = &v0;
  734. op_info.op = op;
  735. r = sn_hwperf_op_cpu(&op_info);
  736. if (r) {
  737. r = sn_hwperf_map_err(r);
  738. a.v0 = v0;
  739. goto error;
  740. }
  741. break;
  742. default:
  743. /* all other ops are a direct SAL call */
  744. r = ia64_sn_hwperf_op(sn_hwperf_master_nasid, op,
  745. a.arg, a.sz, (u64) p, 0, 0, &v0);
  746. if (r) {
  747. r = sn_hwperf_map_err(r);
  748. goto error;
  749. }
  750. a.v0 = v0;
  751. break;
  752. }
  753. if (op & SN_HWPERF_OP_MEM_COPYOUT) {
  754. r = copy_to_user((void __user *)a.ptr, p, a.sz);
  755. if (r != 0) {
  756. r = -EFAULT;
  757. goto error;
  758. }
  759. }
  760. error:
  761. vfree(p);
  762. lock_kernel();
  763. return r;
  764. }
  765. static const struct file_operations sn_hwperf_fops = {
  766. .ioctl = sn_hwperf_ioctl,
  767. };
  768. static struct miscdevice sn_hwperf_dev = {
  769. MISC_DYNAMIC_MINOR,
  770. "sn_hwperf",
  771. &sn_hwperf_fops
  772. };
  773. static int sn_hwperf_init(void)
  774. {
  775. u64 v;
  776. int salr;
  777. int e = 0;
  778. /* single threaded, once-only initialization */
  779. down(&sn_hwperf_init_mutex);
  780. if (sn_hwperf_salheap) {
  781. up(&sn_hwperf_init_mutex);
  782. return e;
  783. }
  784. /*
  785. * The PROM code needs a fixed reference node. For convenience the
  786. * same node as the console I/O is used.
  787. */
  788. sn_hwperf_master_nasid = (nasid_t) ia64_sn_get_console_nasid();
  789. /*
  790. * Request the needed size and install the PROM scratch area.
  791. * The PROM keeps various tracking bits in this memory area.
  792. */
  793. salr = ia64_sn_hwperf_op(sn_hwperf_master_nasid,
  794. (u64) SN_HWPERF_GET_HEAPSIZE, 0,
  795. (u64) sizeof(u64), (u64) &v, 0, 0, NULL);
  796. if (salr != SN_HWPERF_OP_OK) {
  797. e = -EINVAL;
  798. goto out;
  799. }
  800. if ((sn_hwperf_salheap = vmalloc(v)) == NULL) {
  801. e = -ENOMEM;
  802. goto out;
  803. }
  804. salr = ia64_sn_hwperf_op(sn_hwperf_master_nasid,
  805. SN_HWPERF_INSTALL_HEAP, 0, v,
  806. (u64) sn_hwperf_salheap, 0, 0, NULL);
  807. if (salr != SN_HWPERF_OP_OK) {
  808. e = -EINVAL;
  809. goto out;
  810. }
  811. salr = ia64_sn_hwperf_op(sn_hwperf_master_nasid,
  812. SN_HWPERF_OBJECT_COUNT, 0,
  813. sizeof(u64), (u64) &v, 0, 0, NULL);
  814. if (salr != SN_HWPERF_OP_OK) {
  815. e = -EINVAL;
  816. goto out;
  817. }
  818. sn_hwperf_obj_cnt = (int)v;
  819. out:
  820. if (e < 0 && sn_hwperf_salheap) {
  821. vfree(sn_hwperf_salheap);
  822. sn_hwperf_salheap = NULL;
  823. sn_hwperf_obj_cnt = 0;
  824. }
  825. up(&sn_hwperf_init_mutex);
  826. return e;
  827. }
  828. int sn_topology_open(struct inode *inode, struct file *file)
  829. {
  830. int e;
  831. struct seq_file *seq;
  832. struct sn_hwperf_object_info *objbuf;
  833. int nobj;
  834. if ((e = sn_hwperf_enum_objects(&nobj, &objbuf)) == 0) {
  835. e = seq_open(file, &sn_topology_seq_ops);
  836. seq = file->private_data;
  837. seq->private = objbuf;
  838. }
  839. return e;
  840. }
  841. int sn_topology_release(struct inode *inode, struct file *file)
  842. {
  843. struct seq_file *seq = file->private_data;
  844. vfree(seq->private);
  845. return seq_release(inode, file);
  846. }
  847. int sn_hwperf_get_nearest_node(cnodeid_t node,
  848. cnodeid_t *near_mem_node, cnodeid_t *near_cpu_node)
  849. {
  850. int e;
  851. int nobj;
  852. struct sn_hwperf_object_info *objbuf;
  853. if ((e = sn_hwperf_enum_objects(&nobj, &objbuf)) == 0) {
  854. e = sn_hwperf_get_nearest_node_objdata(objbuf, nobj,
  855. node, near_mem_node, near_cpu_node);
  856. vfree(objbuf);
  857. }
  858. return e;
  859. }
  860. static int __devinit sn_hwperf_misc_register_init(void)
  861. {
  862. int e;
  863. if (!ia64_platform_is("sn2"))
  864. return 0;
  865. sn_hwperf_init();
  866. /*
  867. * Register a dynamic misc device for hwperf ioctls. Platforms
  868. * supporting hotplug will create /dev/sn_hwperf, else user
  869. * can to look up the minor number in /proc/misc.
  870. */
  871. if ((e = misc_register(&sn_hwperf_dev)) != 0) {
  872. printk(KERN_ERR "sn_hwperf_misc_register_init: failed to "
  873. "register misc device for \"%s\"\n", sn_hwperf_dev.name);
  874. }
  875. return e;
  876. }
  877. device_initcall(sn_hwperf_misc_register_init); /* after misc_init() */
  878. EXPORT_SYMBOL(sn_hwperf_get_nearest_node);