hwsampler.c 25 KB

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