hwsampler.c 24 KB

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