hwsampler.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248
  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/lowcore.h>
  20. #include <asm/s390_ext.h>
  21. #include "hwsampler.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. #define EI_IEA (1 << 31) /* invalid entry address */
  27. #define EI_ISE (1 << 30) /* incorrect SDBT entry */
  28. #define EI_PRA (1 << 29) /* program request alert */
  29. #define EI_SACA (1 << 23) /* sampler authorization change alert */
  30. #define EI_LSDA (1 << 22) /* loss of sample data alert */
  31. DECLARE_PER_CPU(struct hws_cpu_buffer, sampler_cpu_buffer);
  32. struct hws_execute_parms {
  33. void *buffer;
  34. signed int rc;
  35. };
  36. DEFINE_PER_CPU(struct hws_cpu_buffer, sampler_cpu_buffer);
  37. EXPORT_PER_CPU_SYMBOL(sampler_cpu_buffer);
  38. static DEFINE_MUTEX(hws_sem);
  39. static DEFINE_MUTEX(hws_sem_oom);
  40. static unsigned char hws_flush_all;
  41. static unsigned int hws_oom;
  42. static struct workqueue_struct *hws_wq;
  43. static unsigned int hws_state;
  44. enum {
  45. HWS_INIT = 1,
  46. HWS_DEALLOCATED,
  47. HWS_STOPPED,
  48. HWS_STARTED,
  49. HWS_STOPPING };
  50. /* set to 1 if called by kernel during memory allocation */
  51. static unsigned char oom_killer_was_active;
  52. /* size of SDBT and SDB as of allocate API */
  53. static unsigned long num_sdbt = 100;
  54. static unsigned long num_sdb = 511;
  55. /* sampling interval (machine cycles) */
  56. static unsigned long interval;
  57. static unsigned long min_sampler_rate;
  58. static unsigned long max_sampler_rate;
  59. static int ssctl(void *buffer)
  60. {
  61. int cc;
  62. /* set in order to detect a program check */
  63. cc = 1;
  64. asm volatile(
  65. "0: .insn s,0xB2870000,0(%1)\n"
  66. "1: ipm %0\n"
  67. " srl %0,28\n"
  68. "2:\n"
  69. EX_TABLE(0b, 2b) EX_TABLE(1b, 2b)
  70. : "+d" (cc), "+a" (buffer)
  71. : "m" (*((struct hws_ssctl_request_block *)buffer))
  72. : "cc", "memory");
  73. return cc ? -EINVAL : 0 ;
  74. }
  75. static int qsi(void *buffer)
  76. {
  77. int cc;
  78. cc = 1;
  79. asm volatile(
  80. "0: .insn s,0xB2860000,0(%1)\n"
  81. "1: lhi %0,0\n"
  82. "2:\n"
  83. EX_TABLE(0b, 2b) EX_TABLE(1b, 2b)
  84. : "=d" (cc), "+a" (buffer)
  85. : "m" (*((struct hws_qsi_info_block *)buffer))
  86. : "cc", "memory");
  87. return cc ? -EINVAL : 0;
  88. }
  89. static void execute_qsi(void *parms)
  90. {
  91. struct hws_execute_parms *ep = parms;
  92. ep->rc = qsi(ep->buffer);
  93. }
  94. static void execute_ssctl(void *parms)
  95. {
  96. struct hws_execute_parms *ep = parms;
  97. ep->rc = ssctl(ep->buffer);
  98. }
  99. static int smp_ctl_ssctl_stop(int cpu)
  100. {
  101. int rc;
  102. struct hws_execute_parms ep;
  103. struct hws_cpu_buffer *cb;
  104. cb = &per_cpu(sampler_cpu_buffer, cpu);
  105. cb->ssctl.es = 0;
  106. cb->ssctl.cs = 0;
  107. ep.buffer = &cb->ssctl;
  108. smp_call_function_single(cpu, execute_ssctl, &ep, 1);
  109. rc = ep.rc;
  110. if (rc) {
  111. printk(KERN_ERR "hwsampler: CPU %d CPUMF SSCTL failed.\n", cpu);
  112. dump_stack();
  113. }
  114. ep.buffer = &cb->qsi;
  115. smp_call_function_single(cpu, execute_qsi, &ep, 1);
  116. if (cb->qsi.es || cb->qsi.cs) {
  117. printk(KERN_EMERG "CPUMF sampling did not stop properly.\n");
  118. dump_stack();
  119. }
  120. return rc;
  121. }
  122. static int smp_ctl_ssctl_deactivate(int cpu)
  123. {
  124. int rc;
  125. struct hws_execute_parms ep;
  126. struct hws_cpu_buffer *cb;
  127. cb = &per_cpu(sampler_cpu_buffer, cpu);
  128. cb->ssctl.es = 1;
  129. cb->ssctl.cs = 0;
  130. ep.buffer = &cb->ssctl;
  131. smp_call_function_single(cpu, execute_ssctl, &ep, 1);
  132. rc = ep.rc;
  133. if (rc)
  134. printk(KERN_ERR "hwsampler: CPU %d CPUMF SSCTL failed.\n", cpu);
  135. ep.buffer = &cb->qsi;
  136. smp_call_function_single(cpu, execute_qsi, &ep, 1);
  137. if (cb->qsi.cs)
  138. printk(KERN_EMERG "CPUMF sampling was not set inactive.\n");
  139. return rc;
  140. }
  141. static int smp_ctl_ssctl_enable_activate(int cpu, unsigned long interval)
  142. {
  143. int rc;
  144. struct hws_execute_parms ep;
  145. struct hws_cpu_buffer *cb;
  146. cb = &per_cpu(sampler_cpu_buffer, cpu);
  147. cb->ssctl.h = 1;
  148. cb->ssctl.tear = cb->first_sdbt;
  149. cb->ssctl.dear = *(unsigned long *) cb->first_sdbt;
  150. cb->ssctl.interval = interval;
  151. cb->ssctl.es = 1;
  152. cb->ssctl.cs = 1;
  153. ep.buffer = &cb->ssctl;
  154. smp_call_function_single(cpu, execute_ssctl, &ep, 1);
  155. rc = ep.rc;
  156. if (rc)
  157. printk(KERN_ERR "hwsampler: CPU %d CPUMF SSCTL failed.\n", cpu);
  158. ep.buffer = &cb->qsi;
  159. smp_call_function_single(cpu, execute_qsi, &ep, 1);
  160. if (ep.rc)
  161. printk(KERN_ERR "hwsampler: CPU %d CPUMF QSI failed.\n", cpu);
  162. return rc;
  163. }
  164. static int smp_ctl_qsi(int cpu)
  165. {
  166. struct hws_execute_parms ep;
  167. struct hws_cpu_buffer *cb;
  168. cb = &per_cpu(sampler_cpu_buffer, cpu);
  169. ep.buffer = &cb->qsi;
  170. smp_call_function_single(cpu, execute_qsi, &ep, 1);
  171. return ep.rc;
  172. }
  173. static inline unsigned long *trailer_entry_ptr(unsigned long v)
  174. {
  175. void *ret;
  176. ret = (void *)v;
  177. ret += PAGE_SIZE;
  178. ret -= sizeof(struct hws_trailer_entry);
  179. return (unsigned long *) ret;
  180. }
  181. /* prototypes for external interrupt handler and worker */
  182. static void hws_ext_handler(unsigned int ext_int_code,
  183. unsigned int param32, unsigned long param64);
  184. static void worker(struct work_struct *work);
  185. static void add_samples_to_oprofile(unsigned cpu, unsigned long *,
  186. unsigned long *dear);
  187. static void init_all_cpu_buffers(void)
  188. {
  189. int cpu;
  190. struct hws_cpu_buffer *cb;
  191. for_each_online_cpu(cpu) {
  192. cb = &per_cpu(sampler_cpu_buffer, cpu);
  193. memset(cb, 0, sizeof(struct hws_cpu_buffer));
  194. }
  195. }
  196. static int is_link_entry(unsigned long *s)
  197. {
  198. return *s & 0x1ul ? 1 : 0;
  199. }
  200. static unsigned long *get_next_sdbt(unsigned long *s)
  201. {
  202. return (unsigned long *) (*s & ~0x1ul);
  203. }
  204. static int prepare_cpu_buffers(void)
  205. {
  206. int cpu;
  207. int rc;
  208. struct hws_cpu_buffer *cb;
  209. rc = 0;
  210. for_each_online_cpu(cpu) {
  211. cb = &per_cpu(sampler_cpu_buffer, cpu);
  212. atomic_set(&cb->ext_params, 0);
  213. cb->worker_entry = 0;
  214. cb->sample_overflow = 0;
  215. cb->req_alert = 0;
  216. cb->incorrect_sdbt_entry = 0;
  217. cb->invalid_entry_address = 0;
  218. cb->loss_of_sample_data = 0;
  219. cb->sample_auth_change_alert = 0;
  220. cb->finish = 0;
  221. cb->oom = 0;
  222. cb->stop_mode = 0;
  223. }
  224. return rc;
  225. }
  226. /*
  227. * allocate_sdbt() - allocate sampler memory
  228. * @cpu: the cpu for which sampler memory is allocated
  229. *
  230. * A 4K page is allocated for each requested SDBT.
  231. * A maximum of 511 4K pages are allocated for the SDBs in each of the SDBTs.
  232. * Set ALERT_REQ mask in each SDBs trailer.
  233. * Returns zero if successful, <0 otherwise.
  234. */
  235. static int allocate_sdbt(int cpu)
  236. {
  237. int j, k, rc;
  238. unsigned long *sdbt;
  239. unsigned long sdb;
  240. unsigned long *tail;
  241. unsigned long *trailer;
  242. struct hws_cpu_buffer *cb;
  243. cb = &per_cpu(sampler_cpu_buffer, cpu);
  244. if (cb->first_sdbt)
  245. return -EINVAL;
  246. sdbt = NULL;
  247. tail = sdbt;
  248. for (j = 0; j < num_sdbt; j++) {
  249. sdbt = (unsigned long *)get_zeroed_page(GFP_KERNEL);
  250. mutex_lock(&hws_sem_oom);
  251. /* OOM killer might have been activated */
  252. barrier();
  253. if (oom_killer_was_active || !sdbt) {
  254. if (sdbt)
  255. free_page((unsigned long)sdbt);
  256. goto allocate_sdbt_error;
  257. }
  258. if (cb->first_sdbt == 0)
  259. cb->first_sdbt = (unsigned long)sdbt;
  260. /* link current page to tail of chain */
  261. if (tail)
  262. *tail = (unsigned long)(void *)sdbt + 1;
  263. mutex_unlock(&hws_sem_oom);
  264. for (k = 0; k < num_sdb; k++) {
  265. /* get and set SDB page */
  266. sdb = get_zeroed_page(GFP_KERNEL);
  267. mutex_lock(&hws_sem_oom);
  268. /* OOM killer might have been activated */
  269. barrier();
  270. if (oom_killer_was_active || !sdb) {
  271. if (sdb)
  272. free_page(sdb);
  273. goto allocate_sdbt_error;
  274. }
  275. *sdbt = sdb;
  276. trailer = trailer_entry_ptr(*sdbt);
  277. *trailer = ALERT_REQ_MASK;
  278. sdbt++;
  279. mutex_unlock(&hws_sem_oom);
  280. }
  281. tail = sdbt;
  282. }
  283. mutex_lock(&hws_sem_oom);
  284. if (oom_killer_was_active)
  285. goto allocate_sdbt_error;
  286. rc = 0;
  287. if (tail)
  288. *tail = (unsigned long)
  289. ((void *)cb->first_sdbt) + 1;
  290. allocate_sdbt_exit:
  291. mutex_unlock(&hws_sem_oom);
  292. return rc;
  293. allocate_sdbt_error:
  294. rc = -ENOMEM;
  295. goto allocate_sdbt_exit;
  296. }
  297. /*
  298. * deallocate_sdbt() - deallocate all sampler memory
  299. *
  300. * For each online CPU all SDBT trees are deallocated.
  301. * Returns the number of freed pages.
  302. */
  303. static int deallocate_sdbt(void)
  304. {
  305. int cpu;
  306. int counter;
  307. counter = 0;
  308. for_each_online_cpu(cpu) {
  309. unsigned long start;
  310. unsigned long sdbt;
  311. unsigned long *curr;
  312. struct hws_cpu_buffer *cb;
  313. cb = &per_cpu(sampler_cpu_buffer, cpu);
  314. if (!cb->first_sdbt)
  315. continue;
  316. sdbt = cb->first_sdbt;
  317. curr = (unsigned long *) sdbt;
  318. start = sdbt;
  319. /* we'll free the SDBT after all SDBs are processed... */
  320. while (1) {
  321. if (!*curr || !sdbt)
  322. break;
  323. /* watch for link entry reset if found */
  324. if (is_link_entry(curr)) {
  325. curr = get_next_sdbt(curr);
  326. if (sdbt)
  327. free_page(sdbt);
  328. /* we are done if we reach the start */
  329. if ((unsigned long) curr == start)
  330. break;
  331. else
  332. sdbt = (unsigned long) curr;
  333. } else {
  334. /* process SDB pointer */
  335. if (*curr) {
  336. free_page(*curr);
  337. curr++;
  338. }
  339. }
  340. counter++;
  341. }
  342. cb->first_sdbt = 0;
  343. }
  344. return counter;
  345. }
  346. static int start_sampling(int cpu)
  347. {
  348. int rc;
  349. struct hws_cpu_buffer *cb;
  350. cb = &per_cpu(sampler_cpu_buffer, cpu);
  351. rc = smp_ctl_ssctl_enable_activate(cpu, interval);
  352. if (rc) {
  353. printk(KERN_INFO "hwsampler: CPU %d ssctl failed.\n", cpu);
  354. goto start_exit;
  355. }
  356. rc = -EINVAL;
  357. if (!cb->qsi.es) {
  358. printk(KERN_INFO "hwsampler: CPU %d ssctl not enabled.\n", cpu);
  359. goto start_exit;
  360. }
  361. if (!cb->qsi.cs) {
  362. printk(KERN_INFO "hwsampler: CPU %d ssctl not active.\n", cpu);
  363. goto start_exit;
  364. }
  365. printk(KERN_INFO
  366. "hwsampler: CPU %d, CPUMF Sampling started, interval %lu.\n",
  367. cpu, interval);
  368. rc = 0;
  369. start_exit:
  370. return rc;
  371. }
  372. static int stop_sampling(int cpu)
  373. {
  374. unsigned long v;
  375. int rc;
  376. struct hws_cpu_buffer *cb;
  377. rc = smp_ctl_qsi(cpu);
  378. WARN_ON(rc);
  379. cb = &per_cpu(sampler_cpu_buffer, cpu);
  380. if (!rc && !cb->qsi.es)
  381. printk(KERN_INFO "hwsampler: CPU %d, already stopped.\n", cpu);
  382. rc = smp_ctl_ssctl_stop(cpu);
  383. if (rc) {
  384. printk(KERN_INFO "hwsampler: CPU %d, ssctl stop error %d.\n",
  385. cpu, rc);
  386. goto stop_exit;
  387. }
  388. printk(KERN_INFO "hwsampler: CPU %d, CPUMF Sampling stopped.\n", cpu);
  389. stop_exit:
  390. v = cb->req_alert;
  391. if (v)
  392. printk(KERN_ERR "hwsampler: CPU %d CPUMF Request alert,"
  393. " count=%lu.\n", cpu, v);
  394. v = cb->loss_of_sample_data;
  395. if (v)
  396. printk(KERN_ERR "hwsampler: CPU %d CPUMF Loss of sample data,"
  397. " count=%lu.\n", cpu, v);
  398. v = cb->invalid_entry_address;
  399. if (v)
  400. printk(KERN_ERR "hwsampler: CPU %d CPUMF Invalid entry address,"
  401. " count=%lu.\n", cpu, v);
  402. v = cb->incorrect_sdbt_entry;
  403. if (v)
  404. printk(KERN_ERR
  405. "hwsampler: CPU %d CPUMF Incorrect SDBT address,"
  406. " count=%lu.\n", cpu, v);
  407. v = cb->sample_auth_change_alert;
  408. if (v)
  409. printk(KERN_ERR
  410. "hwsampler: CPU %d CPUMF Sample authorization change,"
  411. " count=%lu.\n", cpu, v);
  412. return rc;
  413. }
  414. static int check_hardware_prerequisites(void)
  415. {
  416. if (!test_facility(68))
  417. return -EOPNOTSUPP;
  418. return 0;
  419. }
  420. /*
  421. * hws_oom_callback() - the OOM callback function
  422. *
  423. * In case the callback is invoked during memory allocation for the
  424. * hw sampler, all obtained memory is deallocated and a flag is set
  425. * so main sampler memory allocation can exit with a failure code.
  426. * In case the callback is invoked during sampling the hw sampler
  427. * is deactivated for all CPUs.
  428. */
  429. static int hws_oom_callback(struct notifier_block *nfb,
  430. unsigned long dummy, void *parm)
  431. {
  432. unsigned long *freed;
  433. int cpu;
  434. struct hws_cpu_buffer *cb;
  435. freed = parm;
  436. mutex_lock(&hws_sem_oom);
  437. if (hws_state == HWS_DEALLOCATED) {
  438. /* during memory allocation */
  439. if (oom_killer_was_active == 0) {
  440. oom_killer_was_active = 1;
  441. *freed += deallocate_sdbt();
  442. }
  443. } else {
  444. int i;
  445. cpu = get_cpu();
  446. cb = &per_cpu(sampler_cpu_buffer, cpu);
  447. if (!cb->oom) {
  448. for_each_online_cpu(i) {
  449. smp_ctl_ssctl_deactivate(i);
  450. cb->oom = 1;
  451. }
  452. cb->finish = 1;
  453. printk(KERN_INFO
  454. "hwsampler: CPU %d, OOM notify during CPUMF Sampling.\n",
  455. cpu);
  456. }
  457. }
  458. mutex_unlock(&hws_sem_oom);
  459. return NOTIFY_OK;
  460. }
  461. static struct notifier_block hws_oom_notifier = {
  462. .notifier_call = hws_oom_callback
  463. };
  464. static int hws_cpu_callback(struct notifier_block *nfb,
  465. unsigned long action, void *hcpu)
  466. {
  467. /* We do not have sampler space available for all possible CPUs.
  468. All CPUs should be online when hw sampling is activated. */
  469. return NOTIFY_BAD;
  470. }
  471. static struct notifier_block hws_cpu_notifier = {
  472. .notifier_call = hws_cpu_callback
  473. };
  474. /**
  475. * hwsampler_deactivate() - set hardware sampling temporarily inactive
  476. * @cpu: specifies the CPU to be set inactive.
  477. *
  478. * Returns 0 on success, !0 on failure.
  479. */
  480. int hwsampler_deactivate(unsigned int cpu)
  481. {
  482. /*
  483. * Deactivate hw sampling temporarily and flush the buffer
  484. * by pushing all the pending samples to oprofile buffer.
  485. *
  486. * This function can be called under one of the following conditions:
  487. * Memory unmap, task is exiting.
  488. */
  489. int rc;
  490. struct hws_cpu_buffer *cb;
  491. rc = 0;
  492. mutex_lock(&hws_sem);
  493. cb = &per_cpu(sampler_cpu_buffer, cpu);
  494. if (hws_state == HWS_STARTED) {
  495. rc = smp_ctl_qsi(cpu);
  496. WARN_ON(rc);
  497. if (cb->qsi.cs) {
  498. rc = smp_ctl_ssctl_deactivate(cpu);
  499. if (rc) {
  500. printk(KERN_INFO
  501. "hwsampler: CPU %d, CPUMF Deactivation failed.\n", cpu);
  502. cb->finish = 1;
  503. hws_state = HWS_STOPPING;
  504. } else {
  505. hws_flush_all = 1;
  506. /* Add work to queue to read pending samples.*/
  507. queue_work_on(cpu, hws_wq, &cb->worker);
  508. }
  509. }
  510. }
  511. mutex_unlock(&hws_sem);
  512. if (hws_wq)
  513. flush_workqueue(hws_wq);
  514. return rc;
  515. }
  516. /**
  517. * hwsampler_activate() - activate/resume hardware sampling which was deactivated
  518. * @cpu: specifies the CPU to be set active.
  519. *
  520. * Returns 0 on success, !0 on failure.
  521. */
  522. int hwsampler_activate(unsigned int cpu)
  523. {
  524. /*
  525. * Re-activate hw sampling. This should be called in pair with
  526. * hwsampler_deactivate().
  527. */
  528. int rc;
  529. struct hws_cpu_buffer *cb;
  530. rc = 0;
  531. mutex_lock(&hws_sem);
  532. cb = &per_cpu(sampler_cpu_buffer, cpu);
  533. if (hws_state == HWS_STARTED) {
  534. rc = smp_ctl_qsi(cpu);
  535. WARN_ON(rc);
  536. if (!cb->qsi.cs) {
  537. hws_flush_all = 0;
  538. rc = smp_ctl_ssctl_enable_activate(cpu, interval);
  539. if (rc) {
  540. printk(KERN_ERR
  541. "CPU %d, CPUMF activate sampling failed.\n",
  542. cpu);
  543. }
  544. }
  545. }
  546. mutex_unlock(&hws_sem);
  547. return rc;
  548. }
  549. static void hws_ext_handler(unsigned int ext_int_code,
  550. unsigned int param32, unsigned long param64)
  551. {
  552. int cpu;
  553. struct hws_cpu_buffer *cb;
  554. kstat_cpu(smp_processor_id()).irqs[EXTINT_CPM]++;
  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_PRA)
  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. 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. unsigned long hwsampler_query_min_interval(void)
  831. {
  832. return min_sampler_rate;
  833. }
  834. unsigned long hwsampler_query_max_interval(void)
  835. {
  836. return max_sampler_rate;
  837. }
  838. unsigned long hwsampler_get_sample_overflow_count(unsigned int cpu)
  839. {
  840. struct hws_cpu_buffer *cb;
  841. cb = &per_cpu(sampler_cpu_buffer, cpu);
  842. return cb->sample_overflow;
  843. }
  844. int hwsampler_setup()
  845. {
  846. int rc;
  847. int cpu;
  848. struct hws_cpu_buffer *cb;
  849. mutex_lock(&hws_sem);
  850. rc = -EINVAL;
  851. if (hws_state)
  852. goto setup_exit;
  853. hws_state = HWS_INIT;
  854. init_all_cpu_buffers();
  855. rc = check_hardware_prerequisites();
  856. if (rc)
  857. goto setup_exit;
  858. rc = check_qsi_on_setup();
  859. if (rc)
  860. goto setup_exit;
  861. rc = -EINVAL;
  862. hws_wq = create_workqueue("hwsampler");
  863. if (!hws_wq)
  864. goto setup_exit;
  865. register_cpu_notifier(&hws_cpu_notifier);
  866. for_each_online_cpu(cpu) {
  867. cb = &per_cpu(sampler_cpu_buffer, cpu);
  868. INIT_WORK(&cb->worker, worker);
  869. rc = smp_ctl_qsi(cpu);
  870. WARN_ON(rc);
  871. if (min_sampler_rate != cb->qsi.min_sampl_rate) {
  872. if (min_sampler_rate) {
  873. printk(KERN_WARNING
  874. "hwsampler: different min sampler rate values.\n");
  875. if (min_sampler_rate < cb->qsi.min_sampl_rate)
  876. min_sampler_rate =
  877. cb->qsi.min_sampl_rate;
  878. } else
  879. min_sampler_rate = cb->qsi.min_sampl_rate;
  880. }
  881. if (max_sampler_rate != cb->qsi.max_sampl_rate) {
  882. if (max_sampler_rate) {
  883. printk(KERN_WARNING
  884. "hwsampler: different max sampler rate values.\n");
  885. if (max_sampler_rate > cb->qsi.max_sampl_rate)
  886. max_sampler_rate =
  887. cb->qsi.max_sampl_rate;
  888. } else
  889. max_sampler_rate = cb->qsi.max_sampl_rate;
  890. }
  891. }
  892. register_external_interrupt(0x1407, hws_ext_handler);
  893. hws_state = HWS_DEALLOCATED;
  894. rc = 0;
  895. setup_exit:
  896. mutex_unlock(&hws_sem);
  897. return rc;
  898. }
  899. int hwsampler_shutdown()
  900. {
  901. int rc;
  902. mutex_lock(&hws_sem);
  903. rc = -EINVAL;
  904. if (hws_state == HWS_DEALLOCATED || hws_state == HWS_STOPPED) {
  905. mutex_unlock(&hws_sem);
  906. if (hws_wq)
  907. flush_workqueue(hws_wq);
  908. mutex_lock(&hws_sem);
  909. if (hws_state == HWS_STOPPED) {
  910. ctl_clear_bit(0, 5); /* set bit 58 CR0 off */
  911. deallocate_sdbt();
  912. }
  913. if (hws_wq) {
  914. destroy_workqueue(hws_wq);
  915. hws_wq = NULL;
  916. }
  917. unregister_external_interrupt(0x1407, hws_ext_handler);
  918. hws_state = HWS_INIT;
  919. rc = 0;
  920. }
  921. mutex_unlock(&hws_sem);
  922. unregister_cpu_notifier(&hws_cpu_notifier);
  923. return rc;
  924. }
  925. /**
  926. * hwsampler_start_all() - start hardware sampling on all online CPUs
  927. * @rate: specifies the used interval when samples are taken
  928. *
  929. * Returns 0 on success, !0 on failure.
  930. */
  931. int hwsampler_start_all(unsigned long rate)
  932. {
  933. int rc, cpu;
  934. mutex_lock(&hws_sem);
  935. hws_oom = 0;
  936. rc = -EINVAL;
  937. if (hws_state != HWS_STOPPED)
  938. goto start_all_exit;
  939. interval = rate;
  940. /* fail if rate is not valid */
  941. if (interval < min_sampler_rate || interval > max_sampler_rate)
  942. goto start_all_exit;
  943. rc = check_qsi_on_start();
  944. if (rc)
  945. goto start_all_exit;
  946. rc = prepare_cpu_buffers();
  947. if (rc)
  948. goto start_all_exit;
  949. for_each_online_cpu(cpu) {
  950. rc = start_sampling(cpu);
  951. if (rc)
  952. break;
  953. }
  954. if (rc) {
  955. for_each_online_cpu(cpu) {
  956. stop_sampling(cpu);
  957. }
  958. goto start_all_exit;
  959. }
  960. hws_state = HWS_STARTED;
  961. rc = 0;
  962. start_all_exit:
  963. mutex_unlock(&hws_sem);
  964. if (rc)
  965. return rc;
  966. register_oom_notifier(&hws_oom_notifier);
  967. hws_oom = 1;
  968. hws_flush_all = 0;
  969. /* now let them in, 1407 CPUMF external interrupts */
  970. ctl_set_bit(0, 5); /* set CR0 bit 58 */
  971. return 0;
  972. }
  973. /**
  974. * hwsampler_stop_all() - stop hardware sampling on all online CPUs
  975. *
  976. * Returns 0 on success, !0 on failure.
  977. */
  978. int hwsampler_stop_all()
  979. {
  980. int tmp_rc, rc, cpu;
  981. struct hws_cpu_buffer *cb;
  982. mutex_lock(&hws_sem);
  983. rc = 0;
  984. if (hws_state == HWS_INIT) {
  985. mutex_unlock(&hws_sem);
  986. return rc;
  987. }
  988. hws_state = HWS_STOPPING;
  989. mutex_unlock(&hws_sem);
  990. for_each_online_cpu(cpu) {
  991. cb = &per_cpu(sampler_cpu_buffer, cpu);
  992. cb->stop_mode = 1;
  993. tmp_rc = stop_sampling(cpu);
  994. if (tmp_rc)
  995. rc = tmp_rc;
  996. }
  997. if (hws_wq)
  998. flush_workqueue(hws_wq);
  999. mutex_lock(&hws_sem);
  1000. if (hws_oom) {
  1001. unregister_oom_notifier(&hws_oom_notifier);
  1002. hws_oom = 0;
  1003. }
  1004. hws_state = HWS_STOPPED;
  1005. mutex_unlock(&hws_sem);
  1006. return rc;
  1007. }