hwsampler.c 24 KB

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