sn_hwperf.c 23 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007
  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/semaphore.h>
  39. #include <asm/uaccess.h>
  40. #include <asm/sal.h>
  41. #include <asm/sn/io.h>
  42. #include <asm/sn/sn_sal.h>
  43. #include <asm/sn/module.h>
  44. #include <asm/sn/geo.h>
  45. #include <asm/sn/sn2/sn_hwperf.h>
  46. #include <asm/sn/addrs.h>
  47. static void *sn_hwperf_salheap = NULL;
  48. static int sn_hwperf_obj_cnt = 0;
  49. static nasid_t sn_hwperf_master_nasid = INVALID_NASID;
  50. static int sn_hwperf_init(void);
  51. static DEFINE_MUTEX(sn_hwperf_init_mutex);
  52. #define cnode_possible(n) ((n) < num_cnodes)
  53. static int sn_hwperf_enum_objects(int *nobj, struct sn_hwperf_object_info **ret)
  54. {
  55. int e;
  56. u64 sz;
  57. struct sn_hwperf_object_info *objbuf = NULL;
  58. if ((e = sn_hwperf_init()) < 0) {
  59. printk(KERN_ERR "sn_hwperf_init failed: err %d\n", e);
  60. goto out;
  61. }
  62. sz = sn_hwperf_obj_cnt * sizeof(struct sn_hwperf_object_info);
  63. objbuf = vmalloc(sz);
  64. if (objbuf == NULL) {
  65. printk("sn_hwperf_enum_objects: vmalloc(%d) failed\n", (int)sz);
  66. e = -ENOMEM;
  67. goto out;
  68. }
  69. e = ia64_sn_hwperf_op(sn_hwperf_master_nasid, SN_HWPERF_ENUM_OBJECTS,
  70. 0, sz, (u64) objbuf, 0, 0, NULL);
  71. if (e != SN_HWPERF_OP_OK) {
  72. e = -EINVAL;
  73. vfree(objbuf);
  74. }
  75. out:
  76. *nobj = sn_hwperf_obj_cnt;
  77. *ret = objbuf;
  78. return e;
  79. }
  80. static int sn_hwperf_location_to_bpos(char *location,
  81. int *rack, int *bay, int *slot, int *slab)
  82. {
  83. char type;
  84. /* first scan for an old style geoid string */
  85. if (sscanf(location, "%03d%c%02d#%d",
  86. rack, &type, bay, slab) == 4)
  87. *slot = 0;
  88. else /* scan for a new bladed geoid string */
  89. if (sscanf(location, "%03d%c%02d^%02d#%d",
  90. rack, &type, bay, slot, slab) != 5)
  91. return -1;
  92. /* success */
  93. return 0;
  94. }
  95. static int sn_hwperf_geoid_to_cnode(char *location)
  96. {
  97. int cnode;
  98. geoid_t geoid;
  99. moduleid_t module_id;
  100. int rack, bay, slot, slab;
  101. int this_rack, this_bay, this_slot, this_slab;
  102. if (sn_hwperf_location_to_bpos(location, &rack, &bay, &slot, &slab))
  103. return -1;
  104. /*
  105. * FIXME: replace with cleaner for_each_XXX macro which addresses
  106. * both compute and IO nodes once ACPI3.0 is available.
  107. */
  108. for (cnode = 0; cnode < num_cnodes; cnode++) {
  109. geoid = cnodeid_get_geoid(cnode);
  110. module_id = geo_module(geoid);
  111. this_rack = MODULE_GET_RACK(module_id);
  112. this_bay = MODULE_GET_BPOS(module_id);
  113. this_slot = geo_slot(geoid);
  114. this_slab = geo_slab(geoid);
  115. if (rack == this_rack && bay == this_bay &&
  116. slot == this_slot && slab == this_slab) {
  117. break;
  118. }
  119. }
  120. return cnode_possible(cnode) ? cnode : -1;
  121. }
  122. static int sn_hwperf_obj_to_cnode(struct sn_hwperf_object_info * obj)
  123. {
  124. if (!SN_HWPERF_IS_NODE(obj) && !SN_HWPERF_IS_IONODE(obj))
  125. BUG();
  126. if (SN_HWPERF_FOREIGN(obj))
  127. return -1;
  128. return sn_hwperf_geoid_to_cnode(obj->location);
  129. }
  130. static int sn_hwperf_generic_ordinal(struct sn_hwperf_object_info *obj,
  131. struct sn_hwperf_object_info *objs)
  132. {
  133. int ordinal;
  134. struct sn_hwperf_object_info *p;
  135. for (ordinal=0, p=objs; p != obj; p++) {
  136. if (SN_HWPERF_FOREIGN(p))
  137. continue;
  138. if (SN_HWPERF_SAME_OBJTYPE(p, obj))
  139. ordinal++;
  140. }
  141. return ordinal;
  142. }
  143. static const char *slabname_node = "node"; /* SHub asic */
  144. static const char *slabname_ionode = "ionode"; /* TIO asic */
  145. static const char *slabname_router = "router"; /* NL3R or NL4R */
  146. static const char *slabname_other = "other"; /* unknown asic */
  147. static const char *sn_hwperf_get_slabname(struct sn_hwperf_object_info *obj,
  148. struct sn_hwperf_object_info *objs, int *ordinal)
  149. {
  150. int isnode;
  151. const char *slabname = slabname_other;
  152. if ((isnode = SN_HWPERF_IS_NODE(obj)) || SN_HWPERF_IS_IONODE(obj)) {
  153. slabname = isnode ? slabname_node : slabname_ionode;
  154. *ordinal = sn_hwperf_obj_to_cnode(obj);
  155. }
  156. else {
  157. *ordinal = sn_hwperf_generic_ordinal(obj, objs);
  158. if (SN_HWPERF_IS_ROUTER(obj))
  159. slabname = slabname_router;
  160. }
  161. return slabname;
  162. }
  163. static void print_pci_topology(struct seq_file *s)
  164. {
  165. char *p;
  166. size_t sz;
  167. int e;
  168. for (sz = PAGE_SIZE; sz < 16 * PAGE_SIZE; sz += PAGE_SIZE) {
  169. if (!(p = kmalloc(sz, GFP_KERNEL)))
  170. break;
  171. e = ia64_sn_ioif_get_pci_topology(__pa(p), sz);
  172. if (e == SALRET_OK)
  173. seq_puts(s, p);
  174. kfree(p);
  175. if (e == SALRET_OK || e == SALRET_NOT_IMPLEMENTED)
  176. break;
  177. }
  178. }
  179. static inline int sn_hwperf_has_cpus(cnodeid_t node)
  180. {
  181. return node < MAX_NUMNODES && node_online(node) && nr_cpus_node(node);
  182. }
  183. static inline int sn_hwperf_has_mem(cnodeid_t node)
  184. {
  185. return node < MAX_NUMNODES && node_online(node) && NODE_DATA(node)->node_present_pages;
  186. }
  187. static struct sn_hwperf_object_info *
  188. sn_hwperf_findobj_id(struct sn_hwperf_object_info *objbuf,
  189. int nobj, int id)
  190. {
  191. int i;
  192. struct sn_hwperf_object_info *p = objbuf;
  193. for (i=0; i < nobj; i++, p++) {
  194. if (p->id == id)
  195. return p;
  196. }
  197. return NULL;
  198. }
  199. static int sn_hwperf_get_nearest_node_objdata(struct sn_hwperf_object_info *objbuf,
  200. int nobj, cnodeid_t node, cnodeid_t *near_mem_node, cnodeid_t *near_cpu_node)
  201. {
  202. int e;
  203. struct sn_hwperf_object_info *nodeobj = NULL;
  204. struct sn_hwperf_object_info *op;
  205. struct sn_hwperf_object_info *dest;
  206. struct sn_hwperf_object_info *router;
  207. struct sn_hwperf_port_info ptdata[16];
  208. int sz, i, j;
  209. cnodeid_t c;
  210. int found_mem = 0;
  211. int found_cpu = 0;
  212. if (!cnode_possible(node))
  213. return -EINVAL;
  214. if (sn_hwperf_has_cpus(node)) {
  215. if (near_cpu_node)
  216. *near_cpu_node = node;
  217. found_cpu++;
  218. }
  219. if (sn_hwperf_has_mem(node)) {
  220. if (near_mem_node)
  221. *near_mem_node = node;
  222. found_mem++;
  223. }
  224. if (found_cpu && found_mem)
  225. return 0; /* trivially successful */
  226. /* find the argument node object */
  227. for (i=0, op=objbuf; i < nobj; i++, op++) {
  228. if (!SN_HWPERF_IS_NODE(op) && !SN_HWPERF_IS_IONODE(op))
  229. continue;
  230. if (node == sn_hwperf_obj_to_cnode(op)) {
  231. nodeobj = op;
  232. break;
  233. }
  234. }
  235. if (!nodeobj) {
  236. e = -ENOENT;
  237. goto err;
  238. }
  239. /* get it's interconnect topology */
  240. sz = op->ports * sizeof(struct sn_hwperf_port_info);
  241. if (sz > sizeof(ptdata))
  242. BUG();
  243. e = ia64_sn_hwperf_op(sn_hwperf_master_nasid,
  244. SN_HWPERF_ENUM_PORTS, nodeobj->id, sz,
  245. (u64)&ptdata, 0, 0, NULL);
  246. if (e != SN_HWPERF_OP_OK) {
  247. e = -EINVAL;
  248. goto err;
  249. }
  250. /* find nearest node with cpus and nearest memory */
  251. for (router=NULL, j=0; j < op->ports; j++) {
  252. dest = sn_hwperf_findobj_id(objbuf, nobj, ptdata[j].conn_id);
  253. if (dest && SN_HWPERF_IS_ROUTER(dest))
  254. router = dest;
  255. if (!dest || SN_HWPERF_FOREIGN(dest) ||
  256. !SN_HWPERF_IS_NODE(dest) || SN_HWPERF_IS_IONODE(dest)) {
  257. continue;
  258. }
  259. c = sn_hwperf_obj_to_cnode(dest);
  260. if (!found_cpu && sn_hwperf_has_cpus(c)) {
  261. if (near_cpu_node)
  262. *near_cpu_node = c;
  263. found_cpu++;
  264. }
  265. if (!found_mem && sn_hwperf_has_mem(c)) {
  266. if (near_mem_node)
  267. *near_mem_node = c;
  268. found_mem++;
  269. }
  270. }
  271. if (router && (!found_cpu || !found_mem)) {
  272. /* search for a node connected to the same router */
  273. sz = router->ports * sizeof(struct sn_hwperf_port_info);
  274. if (sz > sizeof(ptdata))
  275. BUG();
  276. e = ia64_sn_hwperf_op(sn_hwperf_master_nasid,
  277. SN_HWPERF_ENUM_PORTS, router->id, sz,
  278. (u64)&ptdata, 0, 0, NULL);
  279. if (e != SN_HWPERF_OP_OK) {
  280. e = -EINVAL;
  281. goto err;
  282. }
  283. for (j=0; j < router->ports; j++) {
  284. dest = sn_hwperf_findobj_id(objbuf, nobj,
  285. ptdata[j].conn_id);
  286. if (!dest || dest->id == node ||
  287. SN_HWPERF_FOREIGN(dest) ||
  288. !SN_HWPERF_IS_NODE(dest) ||
  289. SN_HWPERF_IS_IONODE(dest)) {
  290. continue;
  291. }
  292. c = sn_hwperf_obj_to_cnode(dest);
  293. if (!found_cpu && sn_hwperf_has_cpus(c)) {
  294. if (near_cpu_node)
  295. *near_cpu_node = c;
  296. found_cpu++;
  297. }
  298. if (!found_mem && sn_hwperf_has_mem(c)) {
  299. if (near_mem_node)
  300. *near_mem_node = c;
  301. found_mem++;
  302. }
  303. if (found_cpu && found_mem)
  304. break;
  305. }
  306. }
  307. if (!found_cpu || !found_mem) {
  308. /* resort to _any_ node with CPUs and memory */
  309. for (i=0, op=objbuf; i < nobj; i++, op++) {
  310. if (SN_HWPERF_FOREIGN(op) ||
  311. SN_HWPERF_IS_IONODE(op) ||
  312. !SN_HWPERF_IS_NODE(op)) {
  313. continue;
  314. }
  315. c = sn_hwperf_obj_to_cnode(op);
  316. if (!found_cpu && sn_hwperf_has_cpus(c)) {
  317. if (near_cpu_node)
  318. *near_cpu_node = c;
  319. found_cpu++;
  320. }
  321. if (!found_mem && sn_hwperf_has_mem(c)) {
  322. if (near_mem_node)
  323. *near_mem_node = c;
  324. found_mem++;
  325. }
  326. if (found_cpu && found_mem)
  327. break;
  328. }
  329. }
  330. if (!found_cpu || !found_mem)
  331. e = -ENODATA;
  332. err:
  333. return e;
  334. }
  335. static int sn_topology_show(struct seq_file *s, void *d)
  336. {
  337. int sz;
  338. int pt;
  339. int e = 0;
  340. int i;
  341. int j;
  342. const char *slabname;
  343. int ordinal;
  344. cpumask_t cpumask;
  345. char slice;
  346. struct cpuinfo_ia64 *c;
  347. struct sn_hwperf_port_info *ptdata;
  348. struct sn_hwperf_object_info *p;
  349. struct sn_hwperf_object_info *obj = d; /* this object */
  350. struct sn_hwperf_object_info *objs = s->private; /* all objects */
  351. u8 shubtype;
  352. u8 system_size;
  353. u8 sharing_size;
  354. u8 partid;
  355. u8 coher;
  356. u8 nasid_shift;
  357. u8 region_size;
  358. u16 nasid_mask;
  359. int nasid_msb;
  360. if (obj == objs) {
  361. seq_printf(s, "# sn_topology version 2\n");
  362. seq_printf(s, "# objtype ordinal location partition"
  363. " [attribute value [, ...]]\n");
  364. if (ia64_sn_get_sn_info(0,
  365. &shubtype, &nasid_mask, &nasid_shift, &system_size,
  366. &sharing_size, &partid, &coher, &region_size))
  367. BUG();
  368. for (nasid_msb=63; nasid_msb > 0; nasid_msb--) {
  369. if (((u64)nasid_mask << nasid_shift) & (1ULL << nasid_msb))
  370. break;
  371. }
  372. seq_printf(s, "partition %u %s local "
  373. "shubtype %s, "
  374. "nasid_mask 0x%016lx, "
  375. "nasid_bits %d:%d, "
  376. "system_size %d, "
  377. "sharing_size %d, "
  378. "coherency_domain %d, "
  379. "region_size %d\n",
  380. partid, utsname()->nodename,
  381. shubtype ? "shub2" : "shub1",
  382. (u64)nasid_mask << nasid_shift, nasid_msb, nasid_shift,
  383. system_size, sharing_size, coher, region_size);
  384. print_pci_topology(s);
  385. }
  386. if (SN_HWPERF_FOREIGN(obj)) {
  387. /* private in another partition: not interesting */
  388. return 0;
  389. }
  390. for (i = 0; i < SN_HWPERF_MAXSTRING && obj->name[i]; i++) {
  391. if (obj->name[i] == ' ')
  392. obj->name[i] = '_';
  393. }
  394. slabname = sn_hwperf_get_slabname(obj, objs, &ordinal);
  395. seq_printf(s, "%s %d %s %s asic %s", slabname, ordinal, obj->location,
  396. obj->sn_hwp_this_part ? "local" : "shared", obj->name);
  397. if (ordinal < 0 || (!SN_HWPERF_IS_NODE(obj) && !SN_HWPERF_IS_IONODE(obj)))
  398. seq_putc(s, '\n');
  399. else {
  400. cnodeid_t near_mem = -1;
  401. cnodeid_t near_cpu = -1;
  402. seq_printf(s, ", nasid 0x%x", cnodeid_to_nasid(ordinal));
  403. if (sn_hwperf_get_nearest_node_objdata(objs, sn_hwperf_obj_cnt,
  404. ordinal, &near_mem, &near_cpu) == 0) {
  405. seq_printf(s, ", near_mem_nodeid %d, near_cpu_nodeid %d",
  406. near_mem, near_cpu);
  407. }
  408. if (!SN_HWPERF_IS_IONODE(obj)) {
  409. for_each_online_node(i) {
  410. seq_printf(s, i ? ":%d" : ", dist %d",
  411. node_distance(ordinal, i));
  412. }
  413. }
  414. seq_putc(s, '\n');
  415. /*
  416. * CPUs on this node, if any
  417. */
  418. if (!SN_HWPERF_IS_IONODE(obj)) {
  419. cpumask = node_to_cpumask(ordinal);
  420. for_each_online_cpu(i) {
  421. if (cpu_isset(i, cpumask)) {
  422. slice = 'a' + cpuid_to_slice(i);
  423. c = cpu_data(i);
  424. seq_printf(s, "cpu %d %s%c local"
  425. " freq %luMHz, arch ia64",
  426. i, obj->location, slice,
  427. c->proc_freq / 1000000);
  428. for_each_online_cpu(j) {
  429. seq_printf(s, j ? ":%d" : ", dist %d",
  430. node_distance(
  431. cpu_to_node(i),
  432. cpu_to_node(j)));
  433. }
  434. seq_putc(s, '\n');
  435. }
  436. }
  437. }
  438. }
  439. if (obj->ports) {
  440. /*
  441. * numalink ports
  442. */
  443. sz = obj->ports * sizeof(struct sn_hwperf_port_info);
  444. if ((ptdata = kmalloc(sz, GFP_KERNEL)) == NULL)
  445. return -ENOMEM;
  446. e = ia64_sn_hwperf_op(sn_hwperf_master_nasid,
  447. SN_HWPERF_ENUM_PORTS, obj->id, sz,
  448. (u64) ptdata, 0, 0, NULL);
  449. if (e != SN_HWPERF_OP_OK)
  450. return -EINVAL;
  451. for (ordinal=0, p=objs; p != obj; p++) {
  452. if (!SN_HWPERF_FOREIGN(p))
  453. ordinal += p->ports;
  454. }
  455. for (pt = 0; pt < obj->ports; pt++) {
  456. for (p = objs, i = 0; i < sn_hwperf_obj_cnt; i++, p++) {
  457. if (ptdata[pt].conn_id == p->id) {
  458. break;
  459. }
  460. }
  461. seq_printf(s, "numalink %d %s-%d",
  462. ordinal+pt, obj->location, ptdata[pt].port);
  463. if (i >= sn_hwperf_obj_cnt) {
  464. /* no connection */
  465. seq_puts(s, " local endpoint disconnected"
  466. ", protocol unknown\n");
  467. continue;
  468. }
  469. if (obj->sn_hwp_this_part && p->sn_hwp_this_part)
  470. /* both ends local to this partition */
  471. seq_puts(s, " local");
  472. else if (SN_HWPERF_FOREIGN(p))
  473. /* both ends of the link in foreign partiton */
  474. seq_puts(s, " foreign");
  475. else
  476. /* link straddles a partition */
  477. seq_puts(s, " shared");
  478. /*
  479. * Unlikely, but strictly should query the LLP config
  480. * registers because an NL4R can be configured to run
  481. * NL3 protocol, even when not talking to an NL3 router.
  482. * Ditto for node-node.
  483. */
  484. seq_printf(s, " endpoint %s-%d, protocol %s\n",
  485. p->location, ptdata[pt].conn_port,
  486. (SN_HWPERF_IS_NL3ROUTER(obj) ||
  487. SN_HWPERF_IS_NL3ROUTER(p)) ? "LLP3" : "LLP4");
  488. }
  489. kfree(ptdata);
  490. }
  491. return 0;
  492. }
  493. static void *sn_topology_start(struct seq_file *s, loff_t * pos)
  494. {
  495. struct sn_hwperf_object_info *objs = s->private;
  496. if (*pos < sn_hwperf_obj_cnt)
  497. return (void *)(objs + *pos);
  498. return NULL;
  499. }
  500. static void *sn_topology_next(struct seq_file *s, void *v, loff_t * pos)
  501. {
  502. ++*pos;
  503. return sn_topology_start(s, pos);
  504. }
  505. static void sn_topology_stop(struct seq_file *m, void *v)
  506. {
  507. return;
  508. }
  509. /*
  510. * /proc/sgi_sn/sn_topology, read-only using seq_file
  511. */
  512. static const struct seq_operations sn_topology_seq_ops = {
  513. .start = sn_topology_start,
  514. .next = sn_topology_next,
  515. .stop = sn_topology_stop,
  516. .show = sn_topology_show
  517. };
  518. struct sn_hwperf_op_info {
  519. u64 op;
  520. struct sn_hwperf_ioctl_args *a;
  521. void *p;
  522. int *v0;
  523. int ret;
  524. };
  525. static void sn_hwperf_call_sal(void *info)
  526. {
  527. struct sn_hwperf_op_info *op_info = info;
  528. int r;
  529. r = ia64_sn_hwperf_op(sn_hwperf_master_nasid, op_info->op,
  530. op_info->a->arg, op_info->a->sz,
  531. (u64) op_info->p, 0, 0, op_info->v0);
  532. op_info->ret = r;
  533. }
  534. static int sn_hwperf_op_cpu(struct sn_hwperf_op_info *op_info)
  535. {
  536. u32 cpu;
  537. u32 use_ipi;
  538. int r = 0;
  539. cpumask_t save_allowed;
  540. cpu = (op_info->a->arg & SN_HWPERF_ARG_CPU_MASK) >> 32;
  541. use_ipi = op_info->a->arg & SN_HWPERF_ARG_USE_IPI_MASK;
  542. op_info->a->arg &= SN_HWPERF_ARG_OBJID_MASK;
  543. if (cpu != SN_HWPERF_ARG_ANY_CPU) {
  544. if (cpu >= NR_CPUS || !cpu_online(cpu)) {
  545. r = -EINVAL;
  546. goto out;
  547. }
  548. }
  549. if (cpu == SN_HWPERF_ARG_ANY_CPU || cpu == get_cpu()) {
  550. /* don't care, or already on correct cpu */
  551. sn_hwperf_call_sal(op_info);
  552. }
  553. else {
  554. if (use_ipi) {
  555. /* use an interprocessor interrupt to call SAL */
  556. smp_call_function_single(cpu, sn_hwperf_call_sal,
  557. op_info, 1, 1);
  558. }
  559. else {
  560. /* migrate the task before calling SAL */
  561. save_allowed = current->cpus_allowed;
  562. set_cpus_allowed(current, cpumask_of_cpu(cpu));
  563. sn_hwperf_call_sal(op_info);
  564. set_cpus_allowed(current, save_allowed);
  565. }
  566. }
  567. r = op_info->ret;
  568. out:
  569. return r;
  570. }
  571. /* map SAL hwperf error code to system error code */
  572. static int sn_hwperf_map_err(int hwperf_err)
  573. {
  574. int e;
  575. switch(hwperf_err) {
  576. case SN_HWPERF_OP_OK:
  577. e = 0;
  578. break;
  579. case SN_HWPERF_OP_NOMEM:
  580. e = -ENOMEM;
  581. break;
  582. case SN_HWPERF_OP_NO_PERM:
  583. e = -EPERM;
  584. break;
  585. case SN_HWPERF_OP_IO_ERROR:
  586. e = -EIO;
  587. break;
  588. case SN_HWPERF_OP_BUSY:
  589. e = -EBUSY;
  590. break;
  591. case SN_HWPERF_OP_RECONFIGURE:
  592. e = -EAGAIN;
  593. break;
  594. case SN_HWPERF_OP_INVAL:
  595. default:
  596. e = -EINVAL;
  597. break;
  598. }
  599. return e;
  600. }
  601. /*
  602. * ioctl for "sn_hwperf" misc device
  603. */
  604. static int
  605. sn_hwperf_ioctl(struct inode *in, struct file *fp, u32 op, u64 arg)
  606. {
  607. struct sn_hwperf_ioctl_args a;
  608. struct cpuinfo_ia64 *cdata;
  609. struct sn_hwperf_object_info *objs;
  610. struct sn_hwperf_object_info *cpuobj;
  611. struct sn_hwperf_op_info op_info;
  612. void *p = NULL;
  613. int nobj;
  614. char slice;
  615. int node;
  616. int r;
  617. int v0;
  618. int i;
  619. int j;
  620. unlock_kernel();
  621. /* only user requests are allowed here */
  622. if ((op & SN_HWPERF_OP_MASK) < 10) {
  623. r = -EINVAL;
  624. goto error;
  625. }
  626. r = copy_from_user(&a, (const void __user *)arg,
  627. sizeof(struct sn_hwperf_ioctl_args));
  628. if (r != 0) {
  629. r = -EFAULT;
  630. goto error;
  631. }
  632. /*
  633. * Allocate memory to hold a kernel copy of the user buffer. The
  634. * buffer contents are either copied in or out (or both) of user
  635. * space depending on the flags encoded in the requested operation.
  636. */
  637. if (a.ptr) {
  638. p = vmalloc(a.sz);
  639. if (!p) {
  640. r = -ENOMEM;
  641. goto error;
  642. }
  643. }
  644. if (op & SN_HWPERF_OP_MEM_COPYIN) {
  645. r = copy_from_user(p, (const void __user *)a.ptr, a.sz);
  646. if (r != 0) {
  647. r = -EFAULT;
  648. goto error;
  649. }
  650. }
  651. switch (op) {
  652. case SN_HWPERF_GET_CPU_INFO:
  653. if (a.sz == sizeof(u64)) {
  654. /* special case to get size needed */
  655. *(u64 *) p = (u64) num_online_cpus() *
  656. sizeof(struct sn_hwperf_object_info);
  657. } else
  658. if (a.sz < num_online_cpus() * sizeof(struct sn_hwperf_object_info)) {
  659. r = -ENOMEM;
  660. goto error;
  661. } else
  662. if ((r = sn_hwperf_enum_objects(&nobj, &objs)) == 0) {
  663. int cpuobj_index = 0;
  664. memset(p, 0, a.sz);
  665. for (i = 0; i < nobj; i++) {
  666. if (!SN_HWPERF_IS_NODE(objs + i))
  667. continue;
  668. node = sn_hwperf_obj_to_cnode(objs + i);
  669. for_each_online_cpu(j) {
  670. if (node != cpu_to_node(j))
  671. continue;
  672. cpuobj = (struct sn_hwperf_object_info *) p + cpuobj_index++;
  673. slice = 'a' + cpuid_to_slice(j);
  674. cdata = cpu_data(j);
  675. cpuobj->id = j;
  676. snprintf(cpuobj->name,
  677. sizeof(cpuobj->name),
  678. "CPU %luMHz %s",
  679. cdata->proc_freq / 1000000,
  680. cdata->vendor);
  681. snprintf(cpuobj->location,
  682. sizeof(cpuobj->location),
  683. "%s%c", objs[i].location,
  684. slice);
  685. }
  686. }
  687. vfree(objs);
  688. }
  689. break;
  690. case SN_HWPERF_GET_NODE_NASID:
  691. if (a.sz != sizeof(u64) ||
  692. (node = a.arg) < 0 || !cnode_possible(node)) {
  693. r = -EINVAL;
  694. goto error;
  695. }
  696. *(u64 *)p = (u64)cnodeid_to_nasid(node);
  697. break;
  698. case SN_HWPERF_GET_OBJ_NODE:
  699. if (a.sz != sizeof(u64) || a.arg < 0) {
  700. r = -EINVAL;
  701. goto error;
  702. }
  703. if ((r = sn_hwperf_enum_objects(&nobj, &objs)) == 0) {
  704. if (a.arg >= nobj) {
  705. r = -EINVAL;
  706. vfree(objs);
  707. goto error;
  708. }
  709. if (objs[(i = a.arg)].id != a.arg) {
  710. for (i = 0; i < nobj; i++) {
  711. if (objs[i].id == a.arg)
  712. break;
  713. }
  714. }
  715. if (i == nobj) {
  716. r = -EINVAL;
  717. vfree(objs);
  718. goto error;
  719. }
  720. if (!SN_HWPERF_IS_NODE(objs + i) &&
  721. !SN_HWPERF_IS_IONODE(objs + i)) {
  722. r = -ENOENT;
  723. vfree(objs);
  724. goto error;
  725. }
  726. *(u64 *)p = (u64)sn_hwperf_obj_to_cnode(objs + i);
  727. vfree(objs);
  728. }
  729. break;
  730. case SN_HWPERF_GET_MMRS:
  731. case SN_HWPERF_SET_MMRS:
  732. case SN_HWPERF_OBJECT_DISTANCE:
  733. op_info.p = p;
  734. op_info.a = &a;
  735. op_info.v0 = &v0;
  736. op_info.op = op;
  737. r = sn_hwperf_op_cpu(&op_info);
  738. if (r) {
  739. r = sn_hwperf_map_err(r);
  740. a.v0 = v0;
  741. goto error;
  742. }
  743. break;
  744. default:
  745. /* all other ops are a direct SAL call */
  746. r = ia64_sn_hwperf_op(sn_hwperf_master_nasid, op,
  747. a.arg, a.sz, (u64) p, 0, 0, &v0);
  748. if (r) {
  749. r = sn_hwperf_map_err(r);
  750. goto error;
  751. }
  752. a.v0 = v0;
  753. break;
  754. }
  755. if (op & SN_HWPERF_OP_MEM_COPYOUT) {
  756. r = copy_to_user((void __user *)a.ptr, p, a.sz);
  757. if (r != 0) {
  758. r = -EFAULT;
  759. goto error;
  760. }
  761. }
  762. error:
  763. vfree(p);
  764. lock_kernel();
  765. return r;
  766. }
  767. static const struct file_operations sn_hwperf_fops = {
  768. .ioctl = sn_hwperf_ioctl,
  769. };
  770. static struct miscdevice sn_hwperf_dev = {
  771. MISC_DYNAMIC_MINOR,
  772. "sn_hwperf",
  773. &sn_hwperf_fops
  774. };
  775. static int sn_hwperf_init(void)
  776. {
  777. u64 v;
  778. int salr;
  779. int e = 0;
  780. /* single threaded, once-only initialization */
  781. mutex_lock(&sn_hwperf_init_mutex);
  782. if (sn_hwperf_salheap) {
  783. mutex_unlock(&sn_hwperf_init_mutex);
  784. return e;
  785. }
  786. /*
  787. * The PROM code needs a fixed reference node. For convenience the
  788. * same node as the console I/O is used.
  789. */
  790. sn_hwperf_master_nasid = (nasid_t) ia64_sn_get_console_nasid();
  791. /*
  792. * Request the needed size and install the PROM scratch area.
  793. * The PROM keeps various tracking bits in this memory area.
  794. */
  795. salr = ia64_sn_hwperf_op(sn_hwperf_master_nasid,
  796. (u64) SN_HWPERF_GET_HEAPSIZE, 0,
  797. (u64) sizeof(u64), (u64) &v, 0, 0, NULL);
  798. if (salr != SN_HWPERF_OP_OK) {
  799. e = -EINVAL;
  800. goto out;
  801. }
  802. if ((sn_hwperf_salheap = vmalloc(v)) == NULL) {
  803. e = -ENOMEM;
  804. goto out;
  805. }
  806. salr = ia64_sn_hwperf_op(sn_hwperf_master_nasid,
  807. SN_HWPERF_INSTALL_HEAP, 0, v,
  808. (u64) sn_hwperf_salheap, 0, 0, NULL);
  809. if (salr != SN_HWPERF_OP_OK) {
  810. e = -EINVAL;
  811. goto out;
  812. }
  813. salr = ia64_sn_hwperf_op(sn_hwperf_master_nasid,
  814. SN_HWPERF_OBJECT_COUNT, 0,
  815. sizeof(u64), (u64) &v, 0, 0, NULL);
  816. if (salr != SN_HWPERF_OP_OK) {
  817. e = -EINVAL;
  818. goto out;
  819. }
  820. sn_hwperf_obj_cnt = (int)v;
  821. out:
  822. if (e < 0 && sn_hwperf_salheap) {
  823. vfree(sn_hwperf_salheap);
  824. sn_hwperf_salheap = NULL;
  825. sn_hwperf_obj_cnt = 0;
  826. }
  827. mutex_unlock(&sn_hwperf_init_mutex);
  828. return e;
  829. }
  830. int sn_topology_open(struct inode *inode, struct file *file)
  831. {
  832. int e;
  833. struct seq_file *seq;
  834. struct sn_hwperf_object_info *objbuf;
  835. int nobj;
  836. if ((e = sn_hwperf_enum_objects(&nobj, &objbuf)) == 0) {
  837. e = seq_open(file, &sn_topology_seq_ops);
  838. seq = file->private_data;
  839. seq->private = objbuf;
  840. }
  841. return e;
  842. }
  843. int sn_topology_release(struct inode *inode, struct file *file)
  844. {
  845. struct seq_file *seq = file->private_data;
  846. vfree(seq->private);
  847. return seq_release(inode, file);
  848. }
  849. int sn_hwperf_get_nearest_node(cnodeid_t node,
  850. cnodeid_t *near_mem_node, cnodeid_t *near_cpu_node)
  851. {
  852. int e;
  853. int nobj;
  854. struct sn_hwperf_object_info *objbuf;
  855. if ((e = sn_hwperf_enum_objects(&nobj, &objbuf)) == 0) {
  856. e = sn_hwperf_get_nearest_node_objdata(objbuf, nobj,
  857. node, near_mem_node, near_cpu_node);
  858. vfree(objbuf);
  859. }
  860. return e;
  861. }
  862. static int __devinit sn_hwperf_misc_register_init(void)
  863. {
  864. int e;
  865. if (!ia64_platform_is("sn2"))
  866. return 0;
  867. sn_hwperf_init();
  868. /*
  869. * Register a dynamic misc device for hwperf ioctls. Platforms
  870. * supporting hotplug will create /dev/sn_hwperf, else user
  871. * can to look up the minor number in /proc/misc.
  872. */
  873. if ((e = misc_register(&sn_hwperf_dev)) != 0) {
  874. printk(KERN_ERR "sn_hwperf_misc_register_init: failed to "
  875. "register misc device for \"%s\"\n", sn_hwperf_dev.name);
  876. }
  877. return e;
  878. }
  879. device_initcall(sn_hwperf_misc_register_init); /* after misc_init() */
  880. EXPORT_SYMBOL(sn_hwperf_get_nearest_node);