hwsampler.c 25 KB

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