sn_hwperf.c 23 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001
  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%016lx, "
  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, u64 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. if (a.sz != sizeof(u64) || a.arg < 0) {
  694. r = -EINVAL;
  695. goto error;
  696. }
  697. if ((r = sn_hwperf_enum_objects(&nobj, &objs)) == 0) {
  698. if (a.arg >= nobj) {
  699. r = -EINVAL;
  700. vfree(objs);
  701. goto error;
  702. }
  703. if (objs[(i = a.arg)].id != a.arg) {
  704. for (i = 0; i < nobj; i++) {
  705. if (objs[i].id == a.arg)
  706. break;
  707. }
  708. }
  709. if (i == nobj) {
  710. r = -EINVAL;
  711. vfree(objs);
  712. goto error;
  713. }
  714. if (!SN_HWPERF_IS_NODE(objs + i) &&
  715. !SN_HWPERF_IS_IONODE(objs + i)) {
  716. r = -ENOENT;
  717. vfree(objs);
  718. goto error;
  719. }
  720. *(u64 *)p = (u64)sn_hwperf_obj_to_cnode(objs + i);
  721. vfree(objs);
  722. }
  723. break;
  724. case SN_HWPERF_GET_MMRS:
  725. case SN_HWPERF_SET_MMRS:
  726. case SN_HWPERF_OBJECT_DISTANCE:
  727. op_info.p = p;
  728. op_info.a = &a;
  729. op_info.v0 = &v0;
  730. op_info.op = op;
  731. r = sn_hwperf_op_cpu(&op_info);
  732. if (r) {
  733. r = sn_hwperf_map_err(r);
  734. a.v0 = v0;
  735. goto error;
  736. }
  737. break;
  738. default:
  739. /* all other ops are a direct SAL call */
  740. r = ia64_sn_hwperf_op(sn_hwperf_master_nasid, op,
  741. a.arg, a.sz, (u64) p, 0, 0, &v0);
  742. if (r) {
  743. r = sn_hwperf_map_err(r);
  744. goto error;
  745. }
  746. a.v0 = v0;
  747. break;
  748. }
  749. if (op & SN_HWPERF_OP_MEM_COPYOUT) {
  750. r = copy_to_user((void __user *)a.ptr, p, a.sz);
  751. if (r != 0) {
  752. r = -EFAULT;
  753. goto error;
  754. }
  755. }
  756. error:
  757. vfree(p);
  758. lock_kernel();
  759. return r;
  760. }
  761. static const struct file_operations sn_hwperf_fops = {
  762. .ioctl = sn_hwperf_ioctl,
  763. };
  764. static struct miscdevice sn_hwperf_dev = {
  765. MISC_DYNAMIC_MINOR,
  766. "sn_hwperf",
  767. &sn_hwperf_fops
  768. };
  769. static int sn_hwperf_init(void)
  770. {
  771. u64 v;
  772. int salr;
  773. int e = 0;
  774. /* single threaded, once-only initialization */
  775. mutex_lock(&sn_hwperf_init_mutex);
  776. if (sn_hwperf_salheap) {
  777. mutex_unlock(&sn_hwperf_init_mutex);
  778. return e;
  779. }
  780. /*
  781. * The PROM code needs a fixed reference node. For convenience the
  782. * same node as the console I/O is used.
  783. */
  784. sn_hwperf_master_nasid = (nasid_t) ia64_sn_get_console_nasid();
  785. /*
  786. * Request the needed size and install the PROM scratch area.
  787. * The PROM keeps various tracking bits in this memory area.
  788. */
  789. salr = ia64_sn_hwperf_op(sn_hwperf_master_nasid,
  790. (u64) SN_HWPERF_GET_HEAPSIZE, 0,
  791. (u64) sizeof(u64), (u64) &v, 0, 0, NULL);
  792. if (salr != SN_HWPERF_OP_OK) {
  793. e = -EINVAL;
  794. goto out;
  795. }
  796. if ((sn_hwperf_salheap = vmalloc(v)) == NULL) {
  797. e = -ENOMEM;
  798. goto out;
  799. }
  800. salr = ia64_sn_hwperf_op(sn_hwperf_master_nasid,
  801. SN_HWPERF_INSTALL_HEAP, 0, v,
  802. (u64) sn_hwperf_salheap, 0, 0, NULL);
  803. if (salr != SN_HWPERF_OP_OK) {
  804. e = -EINVAL;
  805. goto out;
  806. }
  807. salr = ia64_sn_hwperf_op(sn_hwperf_master_nasid,
  808. SN_HWPERF_OBJECT_COUNT, 0,
  809. sizeof(u64), (u64) &v, 0, 0, NULL);
  810. if (salr != SN_HWPERF_OP_OK) {
  811. e = -EINVAL;
  812. goto out;
  813. }
  814. sn_hwperf_obj_cnt = (int)v;
  815. out:
  816. if (e < 0 && sn_hwperf_salheap) {
  817. vfree(sn_hwperf_salheap);
  818. sn_hwperf_salheap = NULL;
  819. sn_hwperf_obj_cnt = 0;
  820. }
  821. mutex_unlock(&sn_hwperf_init_mutex);
  822. return e;
  823. }
  824. int sn_topology_open(struct inode *inode, struct file *file)
  825. {
  826. int e;
  827. struct seq_file *seq;
  828. struct sn_hwperf_object_info *objbuf;
  829. int nobj;
  830. if ((e = sn_hwperf_enum_objects(&nobj, &objbuf)) == 0) {
  831. e = seq_open(file, &sn_topology_seq_ops);
  832. seq = file->private_data;
  833. seq->private = objbuf;
  834. }
  835. return e;
  836. }
  837. int sn_topology_release(struct inode *inode, struct file *file)
  838. {
  839. struct seq_file *seq = file->private_data;
  840. vfree(seq->private);
  841. return seq_release(inode, file);
  842. }
  843. int sn_hwperf_get_nearest_node(cnodeid_t node,
  844. cnodeid_t *near_mem_node, cnodeid_t *near_cpu_node)
  845. {
  846. int e;
  847. int nobj;
  848. struct sn_hwperf_object_info *objbuf;
  849. if ((e = sn_hwperf_enum_objects(&nobj, &objbuf)) == 0) {
  850. e = sn_hwperf_get_nearest_node_objdata(objbuf, nobj,
  851. node, near_mem_node, near_cpu_node);
  852. vfree(objbuf);
  853. }
  854. return e;
  855. }
  856. static int __devinit sn_hwperf_misc_register_init(void)
  857. {
  858. int e;
  859. if (!ia64_platform_is("sn2"))
  860. return 0;
  861. sn_hwperf_init();
  862. /*
  863. * Register a dynamic misc device for hwperf ioctls. Platforms
  864. * supporting hotplug will create /dev/sn_hwperf, else user
  865. * can to look up the minor number in /proc/misc.
  866. */
  867. if ((e = misc_register(&sn_hwperf_dev)) != 0) {
  868. printk(KERN_ERR "sn_hwperf_misc_register_init: failed to "
  869. "register misc device for \"%s\"\n", sn_hwperf_dev.name);
  870. }
  871. return e;
  872. }
  873. device_initcall(sn_hwperf_misc_register_init); /* after misc_init() */
  874. EXPORT_SYMBOL(sn_hwperf_get_nearest_node);