hwsampler.c 25 KB

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