hwsampler.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246
  1. /**
  2. * arch/s390/oprofile/hwsampler.c
  3. *
  4. * Copyright IBM Corp. 2010
  5. * Author: Heinz Graalfs <graalfs@de.ibm.com>
  6. */
  7. #include <linux/kernel_stat.h>
  8. #include <linux/kernel.h>
  9. #include <linux/module.h>
  10. #include <linux/smp.h>
  11. #include <linux/errno.h>
  12. #include <linux/workqueue.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/notifier.h>
  15. #include <linux/cpu.h>
  16. #include <linux/semaphore.h>
  17. #include <linux/oom.h>
  18. #include <linux/oprofile.h>
  19. #include <asm/lowcore.h>
  20. #include <asm/irq.h>
  21. #include "hwsampler.h"
  22. #include "op_counter.h"
  23. #define MAX_NUM_SDB 511
  24. #define MIN_NUM_SDB 1
  25. #define ALERT_REQ_MASK 0x4000000000000000ul
  26. #define BUFFER_FULL_MASK 0x8000000000000000ul
  27. #define EI_IEA (1 << 31) /* invalid entry address */
  28. #define EI_ISE (1 << 30) /* incorrect SDBT entry */
  29. #define EI_PRA (1 << 29) /* program request alert */
  30. #define EI_SACA (1 << 23) /* sampler authorization change alert */
  31. #define EI_LSDA (1 << 22) /* loss of sample data alert */
  32. DECLARE_PER_CPU(struct hws_cpu_buffer, sampler_cpu_buffer);
  33. struct hws_execute_parms {
  34. void *buffer;
  35. signed int rc;
  36. };
  37. DEFINE_PER_CPU(struct hws_cpu_buffer, sampler_cpu_buffer);
  38. EXPORT_PER_CPU_SYMBOL(sampler_cpu_buffer);
  39. static DEFINE_MUTEX(hws_sem);
  40. static DEFINE_MUTEX(hws_sem_oom);
  41. static unsigned char hws_flush_all;
  42. static unsigned int hws_oom;
  43. static struct workqueue_struct *hws_wq;
  44. static unsigned int hws_state;
  45. enum {
  46. HWS_INIT = 1,
  47. HWS_DEALLOCATED,
  48. HWS_STOPPED,
  49. HWS_STARTED,
  50. HWS_STOPPING };
  51. /* set to 1 if called by kernel during memory allocation */
  52. static unsigned char oom_killer_was_active;
  53. /* size of SDBT and SDB as of allocate API */
  54. static unsigned long num_sdbt = 100;
  55. static unsigned long num_sdb = 511;
  56. /* sampling interval (machine cycles) */
  57. static unsigned long interval;
  58. static unsigned long min_sampler_rate;
  59. static unsigned long max_sampler_rate;
  60. static int ssctl(void *buffer)
  61. {
  62. int cc;
  63. /* set in order to detect a program check */
  64. cc = 1;
  65. asm volatile(
  66. "0: .insn s,0xB2870000,0(%1)\n"
  67. "1: ipm %0\n"
  68. " srl %0,28\n"
  69. "2:\n"
  70. EX_TABLE(0b, 2b) EX_TABLE(1b, 2b)
  71. : "+d" (cc), "+a" (buffer)
  72. : "m" (*((struct hws_ssctl_request_block *)buffer))
  73. : "cc", "memory");
  74. return cc ? -EINVAL : 0 ;
  75. }
  76. static int qsi(void *buffer)
  77. {
  78. int cc;
  79. cc = 1;
  80. asm volatile(
  81. "0: .insn s,0xB2860000,0(%1)\n"
  82. "1: lhi %0,0\n"
  83. "2:\n"
  84. EX_TABLE(0b, 2b) EX_TABLE(1b, 2b)
  85. : "=d" (cc), "+a" (buffer)
  86. : "m" (*((struct hws_qsi_info_block *)buffer))
  87. : "cc", "memory");
  88. return cc ? -EINVAL : 0;
  89. }
  90. static void execute_qsi(void *parms)
  91. {
  92. struct hws_execute_parms *ep = parms;
  93. ep->rc = qsi(ep->buffer);
  94. }
  95. static void execute_ssctl(void *parms)
  96. {
  97. struct hws_execute_parms *ep = parms;
  98. ep->rc = ssctl(ep->buffer);
  99. }
  100. static int smp_ctl_ssctl_stop(int cpu)
  101. {
  102. int rc;
  103. struct hws_execute_parms ep;
  104. struct hws_cpu_buffer *cb;
  105. cb = &per_cpu(sampler_cpu_buffer, cpu);
  106. cb->ssctl.es = 0;
  107. cb->ssctl.cs = 0;
  108. ep.buffer = &cb->ssctl;
  109. smp_call_function_single(cpu, execute_ssctl, &ep, 1);
  110. rc = ep.rc;
  111. if (rc) {
  112. printk(KERN_ERR "hwsampler: CPU %d CPUMF SSCTL failed.\n", cpu);
  113. dump_stack();
  114. }
  115. ep.buffer = &cb->qsi;
  116. smp_call_function_single(cpu, execute_qsi, &ep, 1);
  117. if (cb->qsi.es || cb->qsi.cs) {
  118. printk(KERN_EMERG "CPUMF sampling did not stop properly.\n");
  119. dump_stack();
  120. }
  121. return rc;
  122. }
  123. static int smp_ctl_ssctl_deactivate(int cpu)
  124. {
  125. int rc;
  126. struct hws_execute_parms ep;
  127. struct hws_cpu_buffer *cb;
  128. cb = &per_cpu(sampler_cpu_buffer, cpu);
  129. cb->ssctl.es = 1;
  130. cb->ssctl.cs = 0;
  131. ep.buffer = &cb->ssctl;
  132. smp_call_function_single(cpu, execute_ssctl, &ep, 1);
  133. rc = ep.rc;
  134. if (rc)
  135. printk(KERN_ERR "hwsampler: CPU %d CPUMF SSCTL failed.\n", cpu);
  136. ep.buffer = &cb->qsi;
  137. smp_call_function_single(cpu, execute_qsi, &ep, 1);
  138. if (cb->qsi.cs)
  139. printk(KERN_EMERG "CPUMF sampling was not set inactive.\n");
  140. return rc;
  141. }
  142. static int smp_ctl_ssctl_enable_activate(int cpu, unsigned long interval)
  143. {
  144. int rc;
  145. struct hws_execute_parms ep;
  146. struct hws_cpu_buffer *cb;
  147. cb = &per_cpu(sampler_cpu_buffer, cpu);
  148. cb->ssctl.h = 1;
  149. cb->ssctl.tear = cb->first_sdbt;
  150. cb->ssctl.dear = *(unsigned long *) cb->first_sdbt;
  151. cb->ssctl.interval = interval;
  152. cb->ssctl.es = 1;
  153. cb->ssctl.cs = 1;
  154. ep.buffer = &cb->ssctl;
  155. smp_call_function_single(cpu, execute_ssctl, &ep, 1);
  156. rc = ep.rc;
  157. if (rc)
  158. printk(KERN_ERR "hwsampler: CPU %d CPUMF SSCTL failed.\n", cpu);
  159. ep.buffer = &cb->qsi;
  160. smp_call_function_single(cpu, execute_qsi, &ep, 1);
  161. if (ep.rc)
  162. printk(KERN_ERR "hwsampler: CPU %d CPUMF QSI failed.\n", cpu);
  163. return rc;
  164. }
  165. static int smp_ctl_qsi(int cpu)
  166. {
  167. struct hws_execute_parms ep;
  168. struct hws_cpu_buffer *cb;
  169. cb = &per_cpu(sampler_cpu_buffer, cpu);
  170. ep.buffer = &cb->qsi;
  171. smp_call_function_single(cpu, execute_qsi, &ep, 1);
  172. return ep.rc;
  173. }
  174. static inline unsigned long *trailer_entry_ptr(unsigned long v)
  175. {
  176. void *ret;
  177. ret = (void *)v;
  178. ret += PAGE_SIZE;
  179. ret -= sizeof(struct hws_trailer_entry);
  180. return (unsigned long *) ret;
  181. }
  182. /* prototypes for external interrupt handler and worker */
  183. static void hws_ext_handler(unsigned int ext_int_code,
  184. unsigned int param32, unsigned long param64);
  185. static void worker(struct work_struct *work);
  186. static void add_samples_to_oprofile(unsigned cpu, unsigned long *,
  187. unsigned long *dear);
  188. static void init_all_cpu_buffers(void)
  189. {
  190. int cpu;
  191. struct hws_cpu_buffer *cb;
  192. for_each_online_cpu(cpu) {
  193. cb = &per_cpu(sampler_cpu_buffer, cpu);
  194. memset(cb, 0, sizeof(struct hws_cpu_buffer));
  195. }
  196. }
  197. static int is_link_entry(unsigned long *s)
  198. {
  199. return *s & 0x1ul ? 1 : 0;
  200. }
  201. static unsigned long *get_next_sdbt(unsigned long *s)
  202. {
  203. return (unsigned long *) (*s & ~0x1ul);
  204. }
  205. static int prepare_cpu_buffers(void)
  206. {
  207. int cpu;
  208. int rc;
  209. struct hws_cpu_buffer *cb;
  210. rc = 0;
  211. for_each_online_cpu(cpu) {
  212. cb = &per_cpu(sampler_cpu_buffer, cpu);
  213. atomic_set(&cb->ext_params, 0);
  214. cb->worker_entry = 0;
  215. cb->sample_overflow = 0;
  216. cb->req_alert = 0;
  217. cb->incorrect_sdbt_entry = 0;
  218. cb->invalid_entry_address = 0;
  219. cb->loss_of_sample_data = 0;
  220. cb->sample_auth_change_alert = 0;
  221. cb->finish = 0;
  222. cb->oom = 0;
  223. cb->stop_mode = 0;
  224. }
  225. return rc;
  226. }
  227. /*
  228. * allocate_sdbt() - allocate sampler memory
  229. * @cpu: the cpu for which sampler memory is allocated
  230. *
  231. * A 4K page is allocated for each requested SDBT.
  232. * A maximum of 511 4K pages are allocated for the SDBs in each of the SDBTs.
  233. * Set ALERT_REQ mask in each SDBs trailer.
  234. * Returns zero if successful, <0 otherwise.
  235. */
  236. static int allocate_sdbt(int cpu)
  237. {
  238. int j, k, rc;
  239. unsigned long *sdbt;
  240. unsigned long sdb;
  241. unsigned long *tail;
  242. unsigned long *trailer;
  243. struct hws_cpu_buffer *cb;
  244. cb = &per_cpu(sampler_cpu_buffer, cpu);
  245. if (cb->first_sdbt)
  246. return -EINVAL;
  247. sdbt = NULL;
  248. tail = sdbt;
  249. for (j = 0; j < num_sdbt; j++) {
  250. sdbt = (unsigned long *)get_zeroed_page(GFP_KERNEL);
  251. mutex_lock(&hws_sem_oom);
  252. /* OOM killer might have been activated */
  253. barrier();
  254. if (oom_killer_was_active || !sdbt) {
  255. if (sdbt)
  256. free_page((unsigned long)sdbt);
  257. goto allocate_sdbt_error;
  258. }
  259. if (cb->first_sdbt == 0)
  260. cb->first_sdbt = (unsigned long)sdbt;
  261. /* link current page to tail of chain */
  262. if (tail)
  263. *tail = (unsigned long)(void *)sdbt + 1;
  264. mutex_unlock(&hws_sem_oom);
  265. for (k = 0; k < num_sdb; k++) {
  266. /* get and set SDB page */
  267. sdb = get_zeroed_page(GFP_KERNEL);
  268. mutex_lock(&hws_sem_oom);
  269. /* OOM killer might have been activated */
  270. barrier();
  271. if (oom_killer_was_active || !sdb) {
  272. if (sdb)
  273. free_page(sdb);
  274. goto allocate_sdbt_error;
  275. }
  276. *sdbt = sdb;
  277. trailer = trailer_entry_ptr(*sdbt);
  278. *trailer = ALERT_REQ_MASK;
  279. sdbt++;
  280. mutex_unlock(&hws_sem_oom);
  281. }
  282. tail = sdbt;
  283. }
  284. mutex_lock(&hws_sem_oom);
  285. if (oom_killer_was_active)
  286. goto allocate_sdbt_error;
  287. rc = 0;
  288. if (tail)
  289. *tail = (unsigned long)
  290. ((void *)cb->first_sdbt) + 1;
  291. allocate_sdbt_exit:
  292. mutex_unlock(&hws_sem_oom);
  293. return rc;
  294. allocate_sdbt_error:
  295. rc = -ENOMEM;
  296. goto allocate_sdbt_exit;
  297. }
  298. /*
  299. * deallocate_sdbt() - deallocate all sampler memory
  300. *
  301. * For each online CPU all SDBT trees are deallocated.
  302. * Returns the number of freed pages.
  303. */
  304. static int deallocate_sdbt(void)
  305. {
  306. int cpu;
  307. int counter;
  308. counter = 0;
  309. for_each_online_cpu(cpu) {
  310. unsigned long start;
  311. unsigned long sdbt;
  312. unsigned long *curr;
  313. struct hws_cpu_buffer *cb;
  314. cb = &per_cpu(sampler_cpu_buffer, cpu);
  315. if (!cb->first_sdbt)
  316. continue;
  317. sdbt = cb->first_sdbt;
  318. curr = (unsigned long *) sdbt;
  319. start = sdbt;
  320. /* we'll free the SDBT after all SDBs are processed... */
  321. while (1) {
  322. if (!*curr || !sdbt)
  323. break;
  324. /* watch for link entry reset if found */
  325. if (is_link_entry(curr)) {
  326. curr = get_next_sdbt(curr);
  327. if (sdbt)
  328. free_page(sdbt);
  329. /* we are done if we reach the start */
  330. if ((unsigned long) curr == start)
  331. break;
  332. else
  333. sdbt = (unsigned long) curr;
  334. } else {
  335. /* process SDB pointer */
  336. if (*curr) {
  337. free_page(*curr);
  338. curr++;
  339. }
  340. }
  341. counter++;
  342. }
  343. cb->first_sdbt = 0;
  344. }
  345. return counter;
  346. }
  347. static int start_sampling(int cpu)
  348. {
  349. int rc;
  350. struct hws_cpu_buffer *cb;
  351. cb = &per_cpu(sampler_cpu_buffer, cpu);
  352. rc = smp_ctl_ssctl_enable_activate(cpu, interval);
  353. if (rc) {
  354. printk(KERN_INFO "hwsampler: CPU %d ssctl failed.\n", cpu);
  355. goto start_exit;
  356. }
  357. rc = -EINVAL;
  358. if (!cb->qsi.es) {
  359. printk(KERN_INFO "hwsampler: CPU %d ssctl not enabled.\n", cpu);
  360. goto start_exit;
  361. }
  362. if (!cb->qsi.cs) {
  363. printk(KERN_INFO "hwsampler: CPU %d ssctl not active.\n", cpu);
  364. goto start_exit;
  365. }
  366. printk(KERN_INFO
  367. "hwsampler: CPU %d, CPUMF Sampling started, interval %lu.\n",
  368. cpu, interval);
  369. rc = 0;
  370. start_exit:
  371. return rc;
  372. }
  373. static int stop_sampling(int cpu)
  374. {
  375. unsigned long v;
  376. int rc;
  377. struct hws_cpu_buffer *cb;
  378. rc = smp_ctl_qsi(cpu);
  379. WARN_ON(rc);
  380. cb = &per_cpu(sampler_cpu_buffer, cpu);
  381. if (!rc && !cb->qsi.es)
  382. printk(KERN_INFO "hwsampler: CPU %d, already stopped.\n", cpu);
  383. rc = smp_ctl_ssctl_stop(cpu);
  384. if (rc) {
  385. printk(KERN_INFO "hwsampler: CPU %d, ssctl stop error %d.\n",
  386. cpu, rc);
  387. goto stop_exit;
  388. }
  389. printk(KERN_INFO "hwsampler: CPU %d, CPUMF Sampling stopped.\n", cpu);
  390. stop_exit:
  391. v = cb->req_alert;
  392. if (v)
  393. printk(KERN_ERR "hwsampler: CPU %d CPUMF Request alert,"
  394. " count=%lu.\n", cpu, v);
  395. v = cb->loss_of_sample_data;
  396. if (v)
  397. printk(KERN_ERR "hwsampler: CPU %d CPUMF Loss of sample data,"
  398. " count=%lu.\n", cpu, v);
  399. v = cb->invalid_entry_address;
  400. if (v)
  401. printk(KERN_ERR "hwsampler: CPU %d CPUMF Invalid entry address,"
  402. " count=%lu.\n", cpu, v);
  403. v = cb->incorrect_sdbt_entry;
  404. if (v)
  405. printk(KERN_ERR
  406. "hwsampler: CPU %d CPUMF Incorrect SDBT address,"
  407. " count=%lu.\n", cpu, v);
  408. v = cb->sample_auth_change_alert;
  409. if (v)
  410. printk(KERN_ERR
  411. "hwsampler: CPU %d CPUMF Sample authorization change,"
  412. " count=%lu.\n", cpu, v);
  413. return rc;
  414. }
  415. static int check_hardware_prerequisites(void)
  416. {
  417. if (!test_facility(68))
  418. return -EOPNOTSUPP;
  419. return 0;
  420. }
  421. /*
  422. * hws_oom_callback() - the OOM callback function
  423. *
  424. * In case the callback is invoked during memory allocation for the
  425. * hw sampler, all obtained memory is deallocated and a flag is set
  426. * so main sampler memory allocation can exit with a failure code.
  427. * In case the callback is invoked during sampling the hw sampler
  428. * is deactivated for all CPUs.
  429. */
  430. static int hws_oom_callback(struct notifier_block *nfb,
  431. unsigned long dummy, void *parm)
  432. {
  433. unsigned long *freed;
  434. int cpu;
  435. struct hws_cpu_buffer *cb;
  436. freed = parm;
  437. mutex_lock(&hws_sem_oom);
  438. if (hws_state == HWS_DEALLOCATED) {
  439. /* during memory allocation */
  440. if (oom_killer_was_active == 0) {
  441. oom_killer_was_active = 1;
  442. *freed += deallocate_sdbt();
  443. }
  444. } else {
  445. int i;
  446. cpu = get_cpu();
  447. cb = &per_cpu(sampler_cpu_buffer, cpu);
  448. if (!cb->oom) {
  449. for_each_online_cpu(i) {
  450. smp_ctl_ssctl_deactivate(i);
  451. cb->oom = 1;
  452. }
  453. cb->finish = 1;
  454. printk(KERN_INFO
  455. "hwsampler: CPU %d, OOM notify during CPUMF Sampling.\n",
  456. cpu);
  457. }
  458. }
  459. mutex_unlock(&hws_sem_oom);
  460. return NOTIFY_OK;
  461. }
  462. static struct notifier_block hws_oom_notifier = {
  463. .notifier_call = hws_oom_callback
  464. };
  465. static int hws_cpu_callback(struct notifier_block *nfb,
  466. unsigned long action, void *hcpu)
  467. {
  468. /* We do not have sampler space available for all possible CPUs.
  469. All CPUs should be online when hw sampling is activated. */
  470. return (hws_state <= HWS_DEALLOCATED) ? NOTIFY_OK : NOTIFY_BAD;
  471. }
  472. static struct notifier_block hws_cpu_notifier = {
  473. .notifier_call = hws_cpu_callback
  474. };
  475. /**
  476. * hwsampler_deactivate() - set hardware sampling temporarily inactive
  477. * @cpu: specifies the CPU to be set inactive.
  478. *
  479. * Returns 0 on success, !0 on failure.
  480. */
  481. int hwsampler_deactivate(unsigned int cpu)
  482. {
  483. /*
  484. * Deactivate hw sampling temporarily and flush the buffer
  485. * by pushing all the pending samples to oprofile buffer.
  486. *
  487. * This function can be called under one of the following conditions:
  488. * Memory unmap, task is exiting.
  489. */
  490. int rc;
  491. struct hws_cpu_buffer *cb;
  492. rc = 0;
  493. mutex_lock(&hws_sem);
  494. cb = &per_cpu(sampler_cpu_buffer, cpu);
  495. if (hws_state == HWS_STARTED) {
  496. rc = smp_ctl_qsi(cpu);
  497. WARN_ON(rc);
  498. if (cb->qsi.cs) {
  499. rc = smp_ctl_ssctl_deactivate(cpu);
  500. if (rc) {
  501. printk(KERN_INFO
  502. "hwsampler: CPU %d, CPUMF Deactivation failed.\n", cpu);
  503. cb->finish = 1;
  504. hws_state = HWS_STOPPING;
  505. } else {
  506. hws_flush_all = 1;
  507. /* Add work to queue to read pending samples.*/
  508. queue_work_on(cpu, hws_wq, &cb->worker);
  509. }
  510. }
  511. }
  512. mutex_unlock(&hws_sem);
  513. if (hws_wq)
  514. flush_workqueue(hws_wq);
  515. return rc;
  516. }
  517. /**
  518. * hwsampler_activate() - activate/resume hardware sampling which was deactivated
  519. * @cpu: specifies the CPU to be set active.
  520. *
  521. * Returns 0 on success, !0 on failure.
  522. */
  523. int hwsampler_activate(unsigned int cpu)
  524. {
  525. /*
  526. * Re-activate hw sampling. This should be called in pair with
  527. * hwsampler_deactivate().
  528. */
  529. int rc;
  530. struct hws_cpu_buffer *cb;
  531. rc = 0;
  532. mutex_lock(&hws_sem);
  533. cb = &per_cpu(sampler_cpu_buffer, cpu);
  534. if (hws_state == HWS_STARTED) {
  535. rc = smp_ctl_qsi(cpu);
  536. WARN_ON(rc);
  537. if (!cb->qsi.cs) {
  538. hws_flush_all = 0;
  539. rc = smp_ctl_ssctl_enable_activate(cpu, interval);
  540. if (rc) {
  541. printk(KERN_ERR
  542. "CPU %d, CPUMF activate sampling failed.\n",
  543. cpu);
  544. }
  545. }
  546. }
  547. mutex_unlock(&hws_sem);
  548. return rc;
  549. }
  550. static void hws_ext_handler(unsigned int ext_int_code,
  551. unsigned int param32, unsigned long param64)
  552. {
  553. struct hws_cpu_buffer *cb;
  554. kstat_cpu(smp_processor_id()).irqs[EXTINT_CPM]++;
  555. cb = &__get_cpu_var(sampler_cpu_buffer);
  556. atomic_xchg(&cb->ext_params, atomic_read(&cb->ext_params) | param32);
  557. if (hws_wq)
  558. queue_work(hws_wq, &cb->worker);
  559. }
  560. static int check_qsi_on_setup(void)
  561. {
  562. int rc;
  563. unsigned int cpu;
  564. struct hws_cpu_buffer *cb;
  565. for_each_online_cpu(cpu) {
  566. cb = &per_cpu(sampler_cpu_buffer, cpu);
  567. rc = smp_ctl_qsi(cpu);
  568. WARN_ON(rc);
  569. if (rc)
  570. return -EOPNOTSUPP;
  571. if (!cb->qsi.as) {
  572. printk(KERN_INFO "hwsampler: CPUMF sampling is not authorized.\n");
  573. return -EINVAL;
  574. }
  575. if (cb->qsi.es) {
  576. printk(KERN_WARNING "hwsampler: CPUMF is still enabled.\n");
  577. rc = smp_ctl_ssctl_stop(cpu);
  578. if (rc)
  579. return -EINVAL;
  580. printk(KERN_INFO
  581. "CPU %d, CPUMF Sampling stopped now.\n", cpu);
  582. }
  583. }
  584. return 0;
  585. }
  586. static int check_qsi_on_start(void)
  587. {
  588. unsigned int cpu;
  589. int rc;
  590. struct hws_cpu_buffer *cb;
  591. for_each_online_cpu(cpu) {
  592. cb = &per_cpu(sampler_cpu_buffer, cpu);
  593. rc = smp_ctl_qsi(cpu);
  594. WARN_ON(rc);
  595. if (!cb->qsi.as)
  596. return -EINVAL;
  597. if (cb->qsi.es)
  598. return -EINVAL;
  599. if (cb->qsi.cs)
  600. return -EINVAL;
  601. }
  602. return 0;
  603. }
  604. static void worker_on_start(unsigned int cpu)
  605. {
  606. struct hws_cpu_buffer *cb;
  607. cb = &per_cpu(sampler_cpu_buffer, cpu);
  608. cb->worker_entry = cb->first_sdbt;
  609. }
  610. static int worker_check_error(unsigned int cpu, int ext_params)
  611. {
  612. int rc;
  613. unsigned long *sdbt;
  614. struct hws_cpu_buffer *cb;
  615. rc = 0;
  616. cb = &per_cpu(sampler_cpu_buffer, cpu);
  617. sdbt = (unsigned long *) cb->worker_entry;
  618. if (!sdbt || !*sdbt)
  619. return -EINVAL;
  620. if (ext_params & EI_PRA)
  621. cb->req_alert++;
  622. if (ext_params & EI_LSDA)
  623. cb->loss_of_sample_data++;
  624. if (ext_params & EI_IEA) {
  625. cb->invalid_entry_address++;
  626. rc = -EINVAL;
  627. }
  628. if (ext_params & EI_ISE) {
  629. cb->incorrect_sdbt_entry++;
  630. rc = -EINVAL;
  631. }
  632. if (ext_params & EI_SACA) {
  633. cb->sample_auth_change_alert++;
  634. rc = -EINVAL;
  635. }
  636. return rc;
  637. }
  638. static void worker_on_finish(unsigned int cpu)
  639. {
  640. int rc, i;
  641. struct hws_cpu_buffer *cb;
  642. cb = &per_cpu(sampler_cpu_buffer, cpu);
  643. if (cb->finish) {
  644. rc = smp_ctl_qsi(cpu);
  645. WARN_ON(rc);
  646. if (cb->qsi.es) {
  647. printk(KERN_INFO
  648. "hwsampler: CPU %d, CPUMF Stop/Deactivate sampling.\n",
  649. cpu);
  650. rc = smp_ctl_ssctl_stop(cpu);
  651. if (rc)
  652. printk(KERN_INFO
  653. "hwsampler: CPU %d, CPUMF Deactivation failed.\n",
  654. cpu);
  655. for_each_online_cpu(i) {
  656. if (i == cpu)
  657. continue;
  658. if (!cb->finish) {
  659. cb->finish = 1;
  660. queue_work_on(i, hws_wq,
  661. &cb->worker);
  662. }
  663. }
  664. }
  665. }
  666. }
  667. static void worker_on_interrupt(unsigned int cpu)
  668. {
  669. unsigned long *sdbt;
  670. unsigned char done;
  671. struct hws_cpu_buffer *cb;
  672. cb = &per_cpu(sampler_cpu_buffer, cpu);
  673. sdbt = (unsigned long *) cb->worker_entry;
  674. done = 0;
  675. /* do not proceed if stop was entered,
  676. * forget the buffers not yet processed */
  677. while (!done && !cb->stop_mode) {
  678. unsigned long *trailer;
  679. struct hws_trailer_entry *te;
  680. unsigned long *dear = 0;
  681. trailer = trailer_entry_ptr(*sdbt);
  682. /* leave loop if no more work to do */
  683. if (!(*trailer & BUFFER_FULL_MASK)) {
  684. done = 1;
  685. if (!hws_flush_all)
  686. continue;
  687. }
  688. te = (struct hws_trailer_entry *)trailer;
  689. cb->sample_overflow += te->overflow;
  690. add_samples_to_oprofile(cpu, sdbt, dear);
  691. /* reset trailer */
  692. xchg((unsigned char *) te, 0x40);
  693. /* advance to next sdb slot in current sdbt */
  694. sdbt++;
  695. /* in case link bit is set use address w/o link bit */
  696. if (is_link_entry(sdbt))
  697. sdbt = get_next_sdbt(sdbt);
  698. cb->worker_entry = (unsigned long)sdbt;
  699. }
  700. }
  701. static void add_samples_to_oprofile(unsigned int cpu, unsigned long *sdbt,
  702. unsigned long *dear)
  703. {
  704. struct hws_data_entry *sample_data_ptr;
  705. unsigned long *trailer;
  706. trailer = trailer_entry_ptr(*sdbt);
  707. if (dear) {
  708. if (dear > trailer)
  709. return;
  710. trailer = dear;
  711. }
  712. sample_data_ptr = (struct hws_data_entry *)(*sdbt);
  713. while ((unsigned long *)sample_data_ptr < trailer) {
  714. struct pt_regs *regs = NULL;
  715. struct task_struct *tsk = NULL;
  716. /*
  717. * Check sampling mode, 1 indicates basic (=customer) sampling
  718. * mode.
  719. */
  720. if (sample_data_ptr->def != 1) {
  721. /* sample slot is not yet written */
  722. break;
  723. } else {
  724. /* make sure we don't use it twice,
  725. * the next time the sampler will set it again */
  726. sample_data_ptr->def = 0;
  727. }
  728. /* Get pt_regs. */
  729. if (sample_data_ptr->P == 1) {
  730. /* userspace sample */
  731. unsigned int pid = sample_data_ptr->prim_asn;
  732. if (!counter_config.user)
  733. goto skip_sample;
  734. rcu_read_lock();
  735. tsk = pid_task(find_vpid(pid), PIDTYPE_PID);
  736. if (tsk)
  737. regs = task_pt_regs(tsk);
  738. rcu_read_unlock();
  739. } else {
  740. /* kernelspace sample */
  741. if (!counter_config.kernel)
  742. goto skip_sample;
  743. regs = task_pt_regs(current);
  744. }
  745. mutex_lock(&hws_sem);
  746. oprofile_add_ext_hw_sample(sample_data_ptr->ia, regs, 0,
  747. !sample_data_ptr->P, tsk);
  748. mutex_unlock(&hws_sem);
  749. skip_sample:
  750. sample_data_ptr++;
  751. }
  752. }
  753. static void worker(struct work_struct *work)
  754. {
  755. unsigned int cpu;
  756. int ext_params;
  757. struct hws_cpu_buffer *cb;
  758. cb = container_of(work, struct hws_cpu_buffer, worker);
  759. cpu = smp_processor_id();
  760. ext_params = atomic_xchg(&cb->ext_params, 0);
  761. if (!cb->worker_entry)
  762. worker_on_start(cpu);
  763. if (worker_check_error(cpu, ext_params))
  764. return;
  765. if (!cb->finish)
  766. worker_on_interrupt(cpu);
  767. if (cb->finish)
  768. worker_on_finish(cpu);
  769. }
  770. /**
  771. * hwsampler_allocate() - allocate memory for the hardware sampler
  772. * @sdbt: number of SDBTs per online CPU (must be > 0)
  773. * @sdb: number of SDBs per SDBT (minimum 1, maximum 511)
  774. *
  775. * Returns 0 on success, !0 on failure.
  776. */
  777. int hwsampler_allocate(unsigned long sdbt, unsigned long sdb)
  778. {
  779. int cpu, rc;
  780. mutex_lock(&hws_sem);
  781. rc = -EINVAL;
  782. if (hws_state != HWS_DEALLOCATED)
  783. goto allocate_exit;
  784. if (sdbt < 1)
  785. goto allocate_exit;
  786. if (sdb > MAX_NUM_SDB || sdb < MIN_NUM_SDB)
  787. goto allocate_exit;
  788. num_sdbt = sdbt;
  789. num_sdb = sdb;
  790. oom_killer_was_active = 0;
  791. register_oom_notifier(&hws_oom_notifier);
  792. for_each_online_cpu(cpu) {
  793. if (allocate_sdbt(cpu)) {
  794. unregister_oom_notifier(&hws_oom_notifier);
  795. goto allocate_error;
  796. }
  797. }
  798. unregister_oom_notifier(&hws_oom_notifier);
  799. if (oom_killer_was_active)
  800. goto allocate_error;
  801. hws_state = HWS_STOPPED;
  802. rc = 0;
  803. allocate_exit:
  804. mutex_unlock(&hws_sem);
  805. return rc;
  806. allocate_error:
  807. rc = -ENOMEM;
  808. printk(KERN_ERR "hwsampler: CPUMF Memory allocation failed.\n");
  809. goto allocate_exit;
  810. }
  811. /**
  812. * hwsampler_deallocate() - deallocate hardware sampler memory
  813. *
  814. * Returns 0 on success, !0 on failure.
  815. */
  816. int hwsampler_deallocate(void)
  817. {
  818. int rc;
  819. mutex_lock(&hws_sem);
  820. rc = -EINVAL;
  821. if (hws_state != HWS_STOPPED)
  822. goto deallocate_exit;
  823. ctl_clear_bit(0, 5); /* set bit 58 CR0 off */
  824. deallocate_sdbt();
  825. hws_state = HWS_DEALLOCATED;
  826. rc = 0;
  827. deallocate_exit:
  828. mutex_unlock(&hws_sem);
  829. return rc;
  830. }
  831. unsigned long hwsampler_query_min_interval(void)
  832. {
  833. return min_sampler_rate;
  834. }
  835. unsigned long hwsampler_query_max_interval(void)
  836. {
  837. return max_sampler_rate;
  838. }
  839. unsigned long hwsampler_get_sample_overflow_count(unsigned int cpu)
  840. {
  841. struct hws_cpu_buffer *cb;
  842. cb = &per_cpu(sampler_cpu_buffer, cpu);
  843. return cb->sample_overflow;
  844. }
  845. int hwsampler_setup(void)
  846. {
  847. int rc;
  848. int cpu;
  849. struct hws_cpu_buffer *cb;
  850. mutex_lock(&hws_sem);
  851. rc = -EINVAL;
  852. if (hws_state)
  853. goto setup_exit;
  854. hws_state = HWS_INIT;
  855. init_all_cpu_buffers();
  856. rc = check_hardware_prerequisites();
  857. if (rc)
  858. goto setup_exit;
  859. rc = check_qsi_on_setup();
  860. if (rc)
  861. goto setup_exit;
  862. rc = -EINVAL;
  863. hws_wq = create_workqueue("hwsampler");
  864. if (!hws_wq)
  865. goto setup_exit;
  866. register_cpu_notifier(&hws_cpu_notifier);
  867. for_each_online_cpu(cpu) {
  868. cb = &per_cpu(sampler_cpu_buffer, cpu);
  869. INIT_WORK(&cb->worker, worker);
  870. rc = smp_ctl_qsi(cpu);
  871. WARN_ON(rc);
  872. if (min_sampler_rate != cb->qsi.min_sampl_rate) {
  873. if (min_sampler_rate) {
  874. printk(KERN_WARNING
  875. "hwsampler: different min sampler rate values.\n");
  876. if (min_sampler_rate < cb->qsi.min_sampl_rate)
  877. min_sampler_rate =
  878. cb->qsi.min_sampl_rate;
  879. } else
  880. min_sampler_rate = cb->qsi.min_sampl_rate;
  881. }
  882. if (max_sampler_rate != cb->qsi.max_sampl_rate) {
  883. if (max_sampler_rate) {
  884. printk(KERN_WARNING
  885. "hwsampler: different max sampler rate values.\n");
  886. if (max_sampler_rate > cb->qsi.max_sampl_rate)
  887. max_sampler_rate =
  888. cb->qsi.max_sampl_rate;
  889. } else
  890. max_sampler_rate = cb->qsi.max_sampl_rate;
  891. }
  892. }
  893. register_external_interrupt(0x1407, hws_ext_handler);
  894. hws_state = HWS_DEALLOCATED;
  895. rc = 0;
  896. setup_exit:
  897. mutex_unlock(&hws_sem);
  898. return rc;
  899. }
  900. int hwsampler_shutdown(void)
  901. {
  902. int rc;
  903. mutex_lock(&hws_sem);
  904. rc = -EINVAL;
  905. if (hws_state == HWS_DEALLOCATED || hws_state == HWS_STOPPED) {
  906. mutex_unlock(&hws_sem);
  907. if (hws_wq)
  908. flush_workqueue(hws_wq);
  909. mutex_lock(&hws_sem);
  910. if (hws_state == HWS_STOPPED) {
  911. ctl_clear_bit(0, 5); /* set bit 58 CR0 off */
  912. deallocate_sdbt();
  913. }
  914. if (hws_wq) {
  915. destroy_workqueue(hws_wq);
  916. hws_wq = NULL;
  917. }
  918. unregister_external_interrupt(0x1407, hws_ext_handler);
  919. hws_state = HWS_INIT;
  920. rc = 0;
  921. }
  922. mutex_unlock(&hws_sem);
  923. unregister_cpu_notifier(&hws_cpu_notifier);
  924. return rc;
  925. }
  926. /**
  927. * hwsampler_start_all() - start hardware sampling on all online CPUs
  928. * @rate: specifies the used interval when samples are taken
  929. *
  930. * Returns 0 on success, !0 on failure.
  931. */
  932. int hwsampler_start_all(unsigned long rate)
  933. {
  934. int rc, cpu;
  935. mutex_lock(&hws_sem);
  936. hws_oom = 0;
  937. rc = -EINVAL;
  938. if (hws_state != HWS_STOPPED)
  939. goto start_all_exit;
  940. interval = rate;
  941. /* fail if rate is not valid */
  942. if (interval < min_sampler_rate || interval > max_sampler_rate)
  943. goto start_all_exit;
  944. rc = check_qsi_on_start();
  945. if (rc)
  946. goto start_all_exit;
  947. rc = prepare_cpu_buffers();
  948. if (rc)
  949. goto start_all_exit;
  950. for_each_online_cpu(cpu) {
  951. rc = start_sampling(cpu);
  952. if (rc)
  953. break;
  954. }
  955. if (rc) {
  956. for_each_online_cpu(cpu) {
  957. stop_sampling(cpu);
  958. }
  959. goto start_all_exit;
  960. }
  961. hws_state = HWS_STARTED;
  962. rc = 0;
  963. start_all_exit:
  964. mutex_unlock(&hws_sem);
  965. if (rc)
  966. return rc;
  967. register_oom_notifier(&hws_oom_notifier);
  968. hws_oom = 1;
  969. hws_flush_all = 0;
  970. /* now let them in, 1407 CPUMF external interrupts */
  971. ctl_set_bit(0, 5); /* set CR0 bit 58 */
  972. return 0;
  973. }
  974. /**
  975. * hwsampler_stop_all() - stop hardware sampling on all online CPUs
  976. *
  977. * Returns 0 on success, !0 on failure.
  978. */
  979. int hwsampler_stop_all(void)
  980. {
  981. int tmp_rc, rc, cpu;
  982. struct hws_cpu_buffer *cb;
  983. mutex_lock(&hws_sem);
  984. rc = 0;
  985. if (hws_state == HWS_INIT) {
  986. mutex_unlock(&hws_sem);
  987. return rc;
  988. }
  989. hws_state = HWS_STOPPING;
  990. mutex_unlock(&hws_sem);
  991. for_each_online_cpu(cpu) {
  992. cb = &per_cpu(sampler_cpu_buffer, cpu);
  993. cb->stop_mode = 1;
  994. tmp_rc = stop_sampling(cpu);
  995. if (tmp_rc)
  996. rc = tmp_rc;
  997. }
  998. if (hws_wq)
  999. flush_workqueue(hws_wq);
  1000. mutex_lock(&hws_sem);
  1001. if (hws_oom) {
  1002. unregister_oom_notifier(&hws_oom_notifier);
  1003. hws_oom = 0;
  1004. }
  1005. hws_state = HWS_STOPPED;
  1006. mutex_unlock(&hws_sem);
  1007. return rc;
  1008. }