hwsampler.c 25 KB

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