perf_event.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  1. /* Performance event support for sparc64.
  2. *
  3. * Copyright (C) 2009 David S. Miller <davem@davemloft.net>
  4. *
  5. * This code is based almost entirely upon the x86 perf event
  6. * code, which is:
  7. *
  8. * Copyright (C) 2008 Thomas Gleixner <tglx@linutronix.de>
  9. * Copyright (C) 2008-2009 Red Hat, Inc., Ingo Molnar
  10. * Copyright (C) 2009 Jaswinder Singh Rajput
  11. * Copyright (C) 2009 Advanced Micro Devices, Inc., Robert Richter
  12. * Copyright (C) 2008-2009 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com>
  13. */
  14. #include <linux/perf_event.h>
  15. #include <linux/kprobes.h>
  16. #include <linux/kernel.h>
  17. #include <linux/kdebug.h>
  18. #include <linux/mutex.h>
  19. #include <asm/cpudata.h>
  20. #include <asm/atomic.h>
  21. #include <asm/nmi.h>
  22. #include <asm/pcr.h>
  23. /* Sparc64 chips have two performance counters, 32-bits each, with
  24. * overflow interrupts generated on transition from 0xffffffff to 0.
  25. * The counters are accessed in one go using a 64-bit register.
  26. *
  27. * Both counters are controlled using a single control register. The
  28. * only way to stop all sampling is to clear all of the context (user,
  29. * supervisor, hypervisor) sampling enable bits. But these bits apply
  30. * to both counters, thus the two counters can't be enabled/disabled
  31. * individually.
  32. *
  33. * The control register has two event fields, one for each of the two
  34. * counters. It's thus nearly impossible to have one counter going
  35. * while keeping the other one stopped. Therefore it is possible to
  36. * get overflow interrupts for counters not currently "in use" and
  37. * that condition must be checked in the overflow interrupt handler.
  38. *
  39. * So we use a hack, in that we program inactive counters with the
  40. * "sw_count0" and "sw_count1" events. These count how many times
  41. * the instruction "sethi %hi(0xfc000), %g0" is executed. It's an
  42. * unusual way to encode a NOP and therefore will not trigger in
  43. * normal code.
  44. */
  45. #define MAX_HWEVENTS 2
  46. #define MAX_PERIOD ((1UL << 32) - 1)
  47. #define PIC_UPPER_INDEX 0
  48. #define PIC_LOWER_INDEX 1
  49. struct cpu_hw_events {
  50. struct perf_event *events[MAX_HWEVENTS];
  51. unsigned long used_mask[BITS_TO_LONGS(MAX_HWEVENTS)];
  52. unsigned long active_mask[BITS_TO_LONGS(MAX_HWEVENTS)];
  53. int enabled;
  54. };
  55. DEFINE_PER_CPU(struct cpu_hw_events, cpu_hw_events) = { .enabled = 1, };
  56. struct perf_event_map {
  57. u16 encoding;
  58. u8 pic_mask;
  59. #define PIC_NONE 0x00
  60. #define PIC_UPPER 0x01
  61. #define PIC_LOWER 0x02
  62. };
  63. #define C(x) PERF_COUNT_HW_CACHE_##x
  64. #define CACHE_OP_UNSUPPORTED 0xfffe
  65. #define CACHE_OP_NONSENSE 0xffff
  66. typedef struct perf_event_map cache_map_t
  67. [PERF_COUNT_HW_CACHE_MAX]
  68. [PERF_COUNT_HW_CACHE_OP_MAX]
  69. [PERF_COUNT_HW_CACHE_RESULT_MAX];
  70. struct sparc_pmu {
  71. const struct perf_event_map *(*event_map)(int);
  72. const cache_map_t *cache_map;
  73. int max_events;
  74. int upper_shift;
  75. int lower_shift;
  76. int event_mask;
  77. int hv_bit;
  78. int irq_bit;
  79. int upper_nop;
  80. int lower_nop;
  81. };
  82. static const struct perf_event_map ultra3_perfmon_event_map[] = {
  83. [PERF_COUNT_HW_CPU_CYCLES] = { 0x0000, PIC_UPPER | PIC_LOWER },
  84. [PERF_COUNT_HW_INSTRUCTIONS] = { 0x0001, PIC_UPPER | PIC_LOWER },
  85. [PERF_COUNT_HW_CACHE_REFERENCES] = { 0x0009, PIC_LOWER },
  86. [PERF_COUNT_HW_CACHE_MISSES] = { 0x0009, PIC_UPPER },
  87. };
  88. static const struct perf_event_map *ultra3_event_map(int event_id)
  89. {
  90. return &ultra3_perfmon_event_map[event_id];
  91. }
  92. static const cache_map_t ultra3_cache_map = {
  93. [C(L1D)] = {
  94. [C(OP_READ)] = {
  95. [C(RESULT_ACCESS)] = { 0x09, PIC_LOWER, },
  96. [C(RESULT_MISS)] = { 0x09, PIC_UPPER, },
  97. },
  98. [C(OP_WRITE)] = {
  99. [C(RESULT_ACCESS)] = { 0x0a, PIC_LOWER },
  100. [C(RESULT_MISS)] = { 0x0a, PIC_UPPER },
  101. },
  102. [C(OP_PREFETCH)] = {
  103. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  104. [C(RESULT_MISS)] = { CACHE_OP_UNSUPPORTED },
  105. },
  106. },
  107. [C(L1I)] = {
  108. [C(OP_READ)] = {
  109. [C(RESULT_ACCESS)] = { 0x09, PIC_LOWER, },
  110. [C(RESULT_MISS)] = { 0x09, PIC_UPPER, },
  111. },
  112. [ C(OP_WRITE) ] = {
  113. [ C(RESULT_ACCESS) ] = { CACHE_OP_NONSENSE },
  114. [ C(RESULT_MISS) ] = { CACHE_OP_NONSENSE },
  115. },
  116. [ C(OP_PREFETCH) ] = {
  117. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  118. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  119. },
  120. },
  121. [C(LL)] = {
  122. [C(OP_READ)] = {
  123. [C(RESULT_ACCESS)] = { 0x0c, PIC_LOWER, },
  124. [C(RESULT_MISS)] = { 0x0c, PIC_UPPER, },
  125. },
  126. [C(OP_WRITE)] = {
  127. [C(RESULT_ACCESS)] = { 0x0c, PIC_LOWER },
  128. [C(RESULT_MISS)] = { 0x0c, PIC_UPPER },
  129. },
  130. [C(OP_PREFETCH)] = {
  131. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  132. [C(RESULT_MISS)] = { CACHE_OP_UNSUPPORTED },
  133. },
  134. },
  135. [C(DTLB)] = {
  136. [C(OP_READ)] = {
  137. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  138. [C(RESULT_MISS)] = { 0x12, PIC_UPPER, },
  139. },
  140. [ C(OP_WRITE) ] = {
  141. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  142. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  143. },
  144. [ C(OP_PREFETCH) ] = {
  145. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  146. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  147. },
  148. },
  149. [C(ITLB)] = {
  150. [C(OP_READ)] = {
  151. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  152. [C(RESULT_MISS)] = { 0x11, PIC_UPPER, },
  153. },
  154. [ C(OP_WRITE) ] = {
  155. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  156. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  157. },
  158. [ C(OP_PREFETCH) ] = {
  159. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  160. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  161. },
  162. },
  163. [C(BPU)] = {
  164. [C(OP_READ)] = {
  165. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  166. [C(RESULT_MISS)] = { CACHE_OP_UNSUPPORTED },
  167. },
  168. [ C(OP_WRITE) ] = {
  169. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  170. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  171. },
  172. [ C(OP_PREFETCH) ] = {
  173. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  174. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  175. },
  176. },
  177. };
  178. static const struct sparc_pmu ultra3_pmu = {
  179. .event_map = ultra3_event_map,
  180. .cache_map = &ultra3_cache_map,
  181. .max_events = ARRAY_SIZE(ultra3_perfmon_event_map),
  182. .upper_shift = 11,
  183. .lower_shift = 4,
  184. .event_mask = 0x3f,
  185. .upper_nop = 0x1c,
  186. .lower_nop = 0x14,
  187. };
  188. static const struct perf_event_map niagara2_perfmon_event_map[] = {
  189. [PERF_COUNT_HW_CPU_CYCLES] = { 0x02ff, PIC_UPPER | PIC_LOWER },
  190. [PERF_COUNT_HW_INSTRUCTIONS] = { 0x02ff, PIC_UPPER | PIC_LOWER },
  191. [PERF_COUNT_HW_CACHE_REFERENCES] = { 0x0208, PIC_UPPER | PIC_LOWER },
  192. [PERF_COUNT_HW_CACHE_MISSES] = { 0x0302, PIC_UPPER | PIC_LOWER },
  193. [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = { 0x0201, PIC_UPPER | PIC_LOWER },
  194. [PERF_COUNT_HW_BRANCH_MISSES] = { 0x0202, PIC_UPPER | PIC_LOWER },
  195. };
  196. static const struct perf_event_map *niagara2_event_map(int event_id)
  197. {
  198. return &niagara2_perfmon_event_map[event_id];
  199. }
  200. static const struct sparc_pmu niagara2_pmu = {
  201. .event_map = niagara2_event_map,
  202. .max_events = ARRAY_SIZE(niagara2_perfmon_event_map),
  203. .upper_shift = 19,
  204. .lower_shift = 6,
  205. .event_mask = 0xfff,
  206. .hv_bit = 0x8,
  207. .irq_bit = 0x03,
  208. .upper_nop = 0x220,
  209. .lower_nop = 0x220,
  210. };
  211. static const struct sparc_pmu *sparc_pmu __read_mostly;
  212. static u64 event_encoding(u64 event_id, int idx)
  213. {
  214. if (idx == PIC_UPPER_INDEX)
  215. event_id <<= sparc_pmu->upper_shift;
  216. else
  217. event_id <<= sparc_pmu->lower_shift;
  218. return event_id;
  219. }
  220. static u64 mask_for_index(int idx)
  221. {
  222. return event_encoding(sparc_pmu->event_mask, idx);
  223. }
  224. static u64 nop_for_index(int idx)
  225. {
  226. return event_encoding(idx == PIC_UPPER_INDEX ?
  227. sparc_pmu->upper_nop :
  228. sparc_pmu->lower_nop, idx);
  229. }
  230. static inline void sparc_pmu_enable_event(struct hw_perf_event *hwc,
  231. int idx)
  232. {
  233. u64 val, mask = mask_for_index(idx);
  234. val = pcr_ops->read();
  235. pcr_ops->write((val & ~mask) | hwc->config);
  236. }
  237. static inline void sparc_pmu_disable_event(struct hw_perf_event *hwc,
  238. int idx)
  239. {
  240. u64 mask = mask_for_index(idx);
  241. u64 nop = nop_for_index(idx);
  242. u64 val = pcr_ops->read();
  243. pcr_ops->write((val & ~mask) | nop);
  244. }
  245. void hw_perf_enable(void)
  246. {
  247. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  248. u64 val;
  249. int i;
  250. if (cpuc->enabled)
  251. return;
  252. cpuc->enabled = 1;
  253. barrier();
  254. val = pcr_ops->read();
  255. for (i = 0; i < MAX_HWEVENTS; i++) {
  256. struct perf_event *cp = cpuc->events[i];
  257. struct hw_perf_event *hwc;
  258. if (!cp)
  259. continue;
  260. hwc = &cp->hw;
  261. val |= hwc->config_base;
  262. }
  263. pcr_ops->write(val);
  264. }
  265. void hw_perf_disable(void)
  266. {
  267. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  268. u64 val;
  269. if (!cpuc->enabled)
  270. return;
  271. cpuc->enabled = 0;
  272. val = pcr_ops->read();
  273. val &= ~(PCR_UTRACE | PCR_STRACE |
  274. sparc_pmu->hv_bit | sparc_pmu->irq_bit);
  275. pcr_ops->write(val);
  276. }
  277. static u32 read_pmc(int idx)
  278. {
  279. u64 val;
  280. read_pic(val);
  281. if (idx == PIC_UPPER_INDEX)
  282. val >>= 32;
  283. return val & 0xffffffff;
  284. }
  285. static void write_pmc(int idx, u64 val)
  286. {
  287. u64 shift, mask, pic;
  288. shift = 0;
  289. if (idx == PIC_UPPER_INDEX)
  290. shift = 32;
  291. mask = ((u64) 0xffffffff) << shift;
  292. val <<= shift;
  293. read_pic(pic);
  294. pic &= ~mask;
  295. pic |= val;
  296. write_pic(pic);
  297. }
  298. static int sparc_perf_event_set_period(struct perf_event *event,
  299. struct hw_perf_event *hwc, int idx)
  300. {
  301. s64 left = atomic64_read(&hwc->period_left);
  302. s64 period = hwc->sample_period;
  303. int ret = 0;
  304. if (unlikely(left <= -period)) {
  305. left = period;
  306. atomic64_set(&hwc->period_left, left);
  307. hwc->last_period = period;
  308. ret = 1;
  309. }
  310. if (unlikely(left <= 0)) {
  311. left += period;
  312. atomic64_set(&hwc->period_left, left);
  313. hwc->last_period = period;
  314. ret = 1;
  315. }
  316. if (left > MAX_PERIOD)
  317. left = MAX_PERIOD;
  318. atomic64_set(&hwc->prev_count, (u64)-left);
  319. write_pmc(idx, (u64)(-left) & 0xffffffff);
  320. perf_event_update_userpage(event);
  321. return ret;
  322. }
  323. static int sparc_pmu_enable(struct perf_event *event)
  324. {
  325. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  326. struct hw_perf_event *hwc = &event->hw;
  327. int idx = hwc->idx;
  328. if (test_and_set_bit(idx, cpuc->used_mask))
  329. return -EAGAIN;
  330. sparc_pmu_disable_event(hwc, idx);
  331. cpuc->events[idx] = event;
  332. set_bit(idx, cpuc->active_mask);
  333. sparc_perf_event_set_period(event, hwc, idx);
  334. sparc_pmu_enable_event(hwc, idx);
  335. perf_event_update_userpage(event);
  336. return 0;
  337. }
  338. static u64 sparc_perf_event_update(struct perf_event *event,
  339. struct hw_perf_event *hwc, int idx)
  340. {
  341. int shift = 64 - 32;
  342. u64 prev_raw_count, new_raw_count;
  343. s64 delta;
  344. again:
  345. prev_raw_count = atomic64_read(&hwc->prev_count);
  346. new_raw_count = read_pmc(idx);
  347. if (atomic64_cmpxchg(&hwc->prev_count, prev_raw_count,
  348. new_raw_count) != prev_raw_count)
  349. goto again;
  350. delta = (new_raw_count << shift) - (prev_raw_count << shift);
  351. delta >>= shift;
  352. atomic64_add(delta, &event->count);
  353. atomic64_sub(delta, &hwc->period_left);
  354. return new_raw_count;
  355. }
  356. static void sparc_pmu_disable(struct perf_event *event)
  357. {
  358. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  359. struct hw_perf_event *hwc = &event->hw;
  360. int idx = hwc->idx;
  361. clear_bit(idx, cpuc->active_mask);
  362. sparc_pmu_disable_event(hwc, idx);
  363. barrier();
  364. sparc_perf_event_update(event, hwc, idx);
  365. cpuc->events[idx] = NULL;
  366. clear_bit(idx, cpuc->used_mask);
  367. perf_event_update_userpage(event);
  368. }
  369. static void sparc_pmu_read(struct perf_event *event)
  370. {
  371. struct hw_perf_event *hwc = &event->hw;
  372. sparc_perf_event_update(event, hwc, hwc->idx);
  373. }
  374. static void sparc_pmu_unthrottle(struct perf_event *event)
  375. {
  376. struct hw_perf_event *hwc = &event->hw;
  377. sparc_pmu_enable_event(hwc, hwc->idx);
  378. }
  379. static atomic_t active_events = ATOMIC_INIT(0);
  380. static DEFINE_MUTEX(pmc_grab_mutex);
  381. void perf_event_grab_pmc(void)
  382. {
  383. if (atomic_inc_not_zero(&active_events))
  384. return;
  385. mutex_lock(&pmc_grab_mutex);
  386. if (atomic_read(&active_events) == 0) {
  387. if (atomic_read(&nmi_active) > 0) {
  388. on_each_cpu(stop_nmi_watchdog, NULL, 1);
  389. BUG_ON(atomic_read(&nmi_active) != 0);
  390. }
  391. atomic_inc(&active_events);
  392. }
  393. mutex_unlock(&pmc_grab_mutex);
  394. }
  395. void perf_event_release_pmc(void)
  396. {
  397. if (atomic_dec_and_mutex_lock(&active_events, &pmc_grab_mutex)) {
  398. if (atomic_read(&nmi_active) == 0)
  399. on_each_cpu(start_nmi_watchdog, NULL, 1);
  400. mutex_unlock(&pmc_grab_mutex);
  401. }
  402. }
  403. static const struct perf_event_map *sparc_map_cache_event(u64 config)
  404. {
  405. unsigned int cache_type, cache_op, cache_result;
  406. const struct perf_event_map *pmap;
  407. if (!sparc_pmu->cache_map)
  408. return ERR_PTR(-ENOENT);
  409. cache_type = (config >> 0) & 0xff;
  410. if (cache_type >= PERF_COUNT_HW_CACHE_MAX)
  411. return ERR_PTR(-EINVAL);
  412. cache_op = (config >> 8) & 0xff;
  413. if (cache_op >= PERF_COUNT_HW_CACHE_OP_MAX)
  414. return ERR_PTR(-EINVAL);
  415. cache_result = (config >> 16) & 0xff;
  416. if (cache_result >= PERF_COUNT_HW_CACHE_RESULT_MAX)
  417. return ERR_PTR(-EINVAL);
  418. pmap = &((*sparc_pmu->cache_map)[cache_type][cache_op][cache_result]);
  419. if (pmap->encoding == CACHE_OP_UNSUPPORTED)
  420. return ERR_PTR(-ENOENT);
  421. if (pmap->encoding == CACHE_OP_NONSENSE)
  422. return ERR_PTR(-EINVAL);
  423. return pmap;
  424. }
  425. static void hw_perf_event_destroy(struct perf_event *event)
  426. {
  427. perf_event_release_pmc();
  428. }
  429. static int __hw_perf_event_init(struct perf_event *event)
  430. {
  431. struct perf_event_attr *attr = &event->attr;
  432. struct hw_perf_event *hwc = &event->hw;
  433. const struct perf_event_map *pmap;
  434. u64 enc;
  435. if (atomic_read(&nmi_active) < 0)
  436. return -ENODEV;
  437. if (attr->type == PERF_TYPE_HARDWARE) {
  438. if (attr->config >= sparc_pmu->max_events)
  439. return -EINVAL;
  440. pmap = sparc_pmu->event_map(attr->config);
  441. } else if (attr->type == PERF_TYPE_HW_CACHE) {
  442. pmap = sparc_map_cache_event(attr->config);
  443. if (IS_ERR(pmap))
  444. return PTR_ERR(pmap);
  445. } else
  446. return -EOPNOTSUPP;
  447. perf_event_grab_pmc();
  448. event->destroy = hw_perf_event_destroy;
  449. /* We save the enable bits in the config_base. So to
  450. * turn off sampling just write 'config', and to enable
  451. * things write 'config | config_base'.
  452. */
  453. hwc->config_base = sparc_pmu->irq_bit;
  454. if (!attr->exclude_user)
  455. hwc->config_base |= PCR_UTRACE;
  456. if (!attr->exclude_kernel)
  457. hwc->config_base |= PCR_STRACE;
  458. if (!attr->exclude_hv)
  459. hwc->config_base |= sparc_pmu->hv_bit;
  460. if (!hwc->sample_period) {
  461. hwc->sample_period = MAX_PERIOD;
  462. hwc->last_period = hwc->sample_period;
  463. atomic64_set(&hwc->period_left, hwc->sample_period);
  464. }
  465. enc = pmap->encoding;
  466. if (pmap->pic_mask & PIC_UPPER) {
  467. hwc->idx = PIC_UPPER_INDEX;
  468. enc <<= sparc_pmu->upper_shift;
  469. } else {
  470. hwc->idx = PIC_LOWER_INDEX;
  471. enc <<= sparc_pmu->lower_shift;
  472. }
  473. hwc->config |= enc;
  474. return 0;
  475. }
  476. static const struct pmu pmu = {
  477. .enable = sparc_pmu_enable,
  478. .disable = sparc_pmu_disable,
  479. .read = sparc_pmu_read,
  480. .unthrottle = sparc_pmu_unthrottle,
  481. };
  482. const struct pmu *hw_perf_event_init(struct perf_event *event)
  483. {
  484. int err = __hw_perf_event_init(event);
  485. if (err)
  486. return ERR_PTR(err);
  487. return &pmu;
  488. }
  489. void perf_event_print_debug(void)
  490. {
  491. unsigned long flags;
  492. u64 pcr, pic;
  493. int cpu;
  494. if (!sparc_pmu)
  495. return;
  496. local_irq_save(flags);
  497. cpu = smp_processor_id();
  498. pcr = pcr_ops->read();
  499. read_pic(pic);
  500. pr_info("\n");
  501. pr_info("CPU#%d: PCR[%016llx] PIC[%016llx]\n",
  502. cpu, pcr, pic);
  503. local_irq_restore(flags);
  504. }
  505. static int __kprobes perf_event_nmi_handler(struct notifier_block *self,
  506. unsigned long cmd, void *__args)
  507. {
  508. struct die_args *args = __args;
  509. struct perf_sample_data data;
  510. struct cpu_hw_events *cpuc;
  511. struct pt_regs *regs;
  512. int idx;
  513. if (!atomic_read(&active_events))
  514. return NOTIFY_DONE;
  515. switch (cmd) {
  516. case DIE_NMI:
  517. break;
  518. default:
  519. return NOTIFY_DONE;
  520. }
  521. regs = args->regs;
  522. data.addr = 0;
  523. cpuc = &__get_cpu_var(cpu_hw_events);
  524. for (idx = 0; idx < MAX_HWEVENTS; idx++) {
  525. struct perf_event *event = cpuc->events[idx];
  526. struct hw_perf_event *hwc;
  527. u64 val;
  528. if (!test_bit(idx, cpuc->active_mask))
  529. continue;
  530. hwc = &event->hw;
  531. val = sparc_perf_event_update(event, hwc, idx);
  532. if (val & (1ULL << 31))
  533. continue;
  534. data.period = event->hw.last_period;
  535. if (!sparc_perf_event_set_period(event, hwc, idx))
  536. continue;
  537. if (perf_event_overflow(event, 1, &data, regs))
  538. sparc_pmu_disable_event(hwc, idx);
  539. }
  540. return NOTIFY_STOP;
  541. }
  542. static __read_mostly struct notifier_block perf_event_nmi_notifier = {
  543. .notifier_call = perf_event_nmi_handler,
  544. };
  545. static bool __init supported_pmu(void)
  546. {
  547. if (!strcmp(sparc_pmu_type, "ultra3") ||
  548. !strcmp(sparc_pmu_type, "ultra3+") ||
  549. !strcmp(sparc_pmu_type, "ultra3i") ||
  550. !strcmp(sparc_pmu_type, "ultra4+")) {
  551. sparc_pmu = &ultra3_pmu;
  552. return true;
  553. }
  554. if (!strcmp(sparc_pmu_type, "niagara2")) {
  555. sparc_pmu = &niagara2_pmu;
  556. return true;
  557. }
  558. return false;
  559. }
  560. void __init init_hw_perf_events(void)
  561. {
  562. pr_info("Performance events: ");
  563. if (!supported_pmu()) {
  564. pr_cont("No support for PMU type '%s'\n", sparc_pmu_type);
  565. return;
  566. }
  567. pr_cont("Supported PMU type is '%s'\n", sparc_pmu_type);
  568. /* All sparc64 PMUs currently have 2 events. But this simple
  569. * driver only supports one active event at a time.
  570. */
  571. perf_max_events = 1;
  572. register_die_notifier(&perf_event_nmi_notifier);
  573. }