hwsampler.c 25 KB

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