perf_event.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  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. struct sparc_pmu {
  64. const struct perf_event_map *(*event_map)(int);
  65. int max_events;
  66. int upper_shift;
  67. int lower_shift;
  68. int event_mask;
  69. int hv_bit;
  70. int irq_bit;
  71. int upper_nop;
  72. int lower_nop;
  73. };
  74. static const struct perf_event_map ultra3i_perfmon_event_map[] = {
  75. [PERF_COUNT_HW_CPU_CYCLES] = { 0x0000, PIC_UPPER | PIC_LOWER },
  76. [PERF_COUNT_HW_INSTRUCTIONS] = { 0x0001, PIC_UPPER | PIC_LOWER },
  77. [PERF_COUNT_HW_CACHE_REFERENCES] = { 0x0009, PIC_LOWER },
  78. [PERF_COUNT_HW_CACHE_MISSES] = { 0x0009, PIC_UPPER },
  79. };
  80. static const struct perf_event_map *ultra3i_event_map(int event_id)
  81. {
  82. return &ultra3i_perfmon_event_map[event_id];
  83. }
  84. static const struct sparc_pmu ultra3i_pmu = {
  85. .event_map = ultra3i_event_map,
  86. .max_events = ARRAY_SIZE(ultra3i_perfmon_event_map),
  87. .upper_shift = 11,
  88. .lower_shift = 4,
  89. .event_mask = 0x3f,
  90. .upper_nop = 0x1c,
  91. .lower_nop = 0x14,
  92. };
  93. static const struct perf_event_map niagara2_perfmon_event_map[] = {
  94. [PERF_COUNT_HW_CPU_CYCLES] = { 0x02ff, PIC_UPPER | PIC_LOWER },
  95. [PERF_COUNT_HW_INSTRUCTIONS] = { 0x02ff, PIC_UPPER | PIC_LOWER },
  96. [PERF_COUNT_HW_CACHE_REFERENCES] = { 0x0208, PIC_UPPER | PIC_LOWER },
  97. [PERF_COUNT_HW_CACHE_MISSES] = { 0x0302, PIC_UPPER | PIC_LOWER },
  98. [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = { 0x0201, PIC_UPPER | PIC_LOWER },
  99. [PERF_COUNT_HW_BRANCH_MISSES] = { 0x0202, PIC_UPPER | PIC_LOWER },
  100. };
  101. static const struct perf_event_map *niagara2_event_map(int event_id)
  102. {
  103. return &niagara2_perfmon_event_map[event_id];
  104. }
  105. static const struct sparc_pmu niagara2_pmu = {
  106. .event_map = niagara2_event_map,
  107. .max_events = ARRAY_SIZE(niagara2_perfmon_event_map),
  108. .upper_shift = 19,
  109. .lower_shift = 6,
  110. .event_mask = 0xfff,
  111. .hv_bit = 0x8,
  112. .irq_bit = 0x03,
  113. .upper_nop = 0x220,
  114. .lower_nop = 0x220,
  115. };
  116. static const struct sparc_pmu *sparc_pmu __read_mostly;
  117. static u64 event_encoding(u64 event_id, int idx)
  118. {
  119. if (idx == PIC_UPPER_INDEX)
  120. event_id <<= sparc_pmu->upper_shift;
  121. else
  122. event_id <<= sparc_pmu->lower_shift;
  123. return event_id;
  124. }
  125. static u64 mask_for_index(int idx)
  126. {
  127. return event_encoding(sparc_pmu->event_mask, idx);
  128. }
  129. static u64 nop_for_index(int idx)
  130. {
  131. return event_encoding(idx == PIC_UPPER_INDEX ?
  132. sparc_pmu->upper_nop :
  133. sparc_pmu->lower_nop, idx);
  134. }
  135. static inline void sparc_pmu_enable_event(struct hw_perf_event *hwc,
  136. int idx)
  137. {
  138. u64 val, mask = mask_for_index(idx);
  139. val = pcr_ops->read();
  140. pcr_ops->write((val & ~mask) | hwc->config);
  141. }
  142. static inline void sparc_pmu_disable_event(struct hw_perf_event *hwc,
  143. int idx)
  144. {
  145. u64 mask = mask_for_index(idx);
  146. u64 nop = nop_for_index(idx);
  147. u64 val = pcr_ops->read();
  148. pcr_ops->write((val & ~mask) | nop);
  149. }
  150. void hw_perf_enable(void)
  151. {
  152. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  153. u64 val;
  154. int i;
  155. if (cpuc->enabled)
  156. return;
  157. cpuc->enabled = 1;
  158. barrier();
  159. val = pcr_ops->read();
  160. for (i = 0; i < MAX_HWEVENTS; i++) {
  161. struct perf_event *cp = cpuc->events[i];
  162. struct hw_perf_event *hwc;
  163. if (!cp)
  164. continue;
  165. hwc = &cp->hw;
  166. val |= hwc->config_base;
  167. }
  168. pcr_ops->write(val);
  169. }
  170. void hw_perf_disable(void)
  171. {
  172. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  173. u64 val;
  174. if (!cpuc->enabled)
  175. return;
  176. cpuc->enabled = 0;
  177. val = pcr_ops->read();
  178. val &= ~(PCR_UTRACE | PCR_STRACE |
  179. sparc_pmu->hv_bit | sparc_pmu->irq_bit);
  180. pcr_ops->write(val);
  181. }
  182. static u32 read_pmc(int idx)
  183. {
  184. u64 val;
  185. read_pic(val);
  186. if (idx == PIC_UPPER_INDEX)
  187. val >>= 32;
  188. return val & 0xffffffff;
  189. }
  190. static void write_pmc(int idx, u64 val)
  191. {
  192. u64 shift, mask, pic;
  193. shift = 0;
  194. if (idx == PIC_UPPER_INDEX)
  195. shift = 32;
  196. mask = ((u64) 0xffffffff) << shift;
  197. val <<= shift;
  198. read_pic(pic);
  199. pic &= ~mask;
  200. pic |= val;
  201. write_pic(pic);
  202. }
  203. static int sparc_perf_event_set_period(struct perf_event *event,
  204. struct hw_perf_event *hwc, int idx)
  205. {
  206. s64 left = atomic64_read(&hwc->period_left);
  207. s64 period = hwc->sample_period;
  208. int ret = 0;
  209. if (unlikely(left <= -period)) {
  210. left = period;
  211. atomic64_set(&hwc->period_left, left);
  212. hwc->last_period = period;
  213. ret = 1;
  214. }
  215. if (unlikely(left <= 0)) {
  216. left += period;
  217. atomic64_set(&hwc->period_left, left);
  218. hwc->last_period = period;
  219. ret = 1;
  220. }
  221. if (left > MAX_PERIOD)
  222. left = MAX_PERIOD;
  223. atomic64_set(&hwc->prev_count, (u64)-left);
  224. write_pmc(idx, (u64)(-left) & 0xffffffff);
  225. perf_event_update_userpage(event);
  226. return ret;
  227. }
  228. static int sparc_pmu_enable(struct perf_event *event)
  229. {
  230. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  231. struct hw_perf_event *hwc = &event->hw;
  232. int idx = hwc->idx;
  233. if (test_and_set_bit(idx, cpuc->used_mask))
  234. return -EAGAIN;
  235. sparc_pmu_disable_event(hwc, idx);
  236. cpuc->events[idx] = event;
  237. set_bit(idx, cpuc->active_mask);
  238. sparc_perf_event_set_period(event, hwc, idx);
  239. sparc_pmu_enable_event(hwc, idx);
  240. perf_event_update_userpage(event);
  241. return 0;
  242. }
  243. static u64 sparc_perf_event_update(struct perf_event *event,
  244. struct hw_perf_event *hwc, int idx)
  245. {
  246. int shift = 64 - 32;
  247. u64 prev_raw_count, new_raw_count;
  248. s64 delta;
  249. again:
  250. prev_raw_count = atomic64_read(&hwc->prev_count);
  251. new_raw_count = read_pmc(idx);
  252. if (atomic64_cmpxchg(&hwc->prev_count, prev_raw_count,
  253. new_raw_count) != prev_raw_count)
  254. goto again;
  255. delta = (new_raw_count << shift) - (prev_raw_count << shift);
  256. delta >>= shift;
  257. atomic64_add(delta, &event->count);
  258. atomic64_sub(delta, &hwc->period_left);
  259. return new_raw_count;
  260. }
  261. static void sparc_pmu_disable(struct perf_event *event)
  262. {
  263. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  264. struct hw_perf_event *hwc = &event->hw;
  265. int idx = hwc->idx;
  266. clear_bit(idx, cpuc->active_mask);
  267. sparc_pmu_disable_event(hwc, idx);
  268. barrier();
  269. sparc_perf_event_update(event, hwc, idx);
  270. cpuc->events[idx] = NULL;
  271. clear_bit(idx, cpuc->used_mask);
  272. perf_event_update_userpage(event);
  273. }
  274. static void sparc_pmu_read(struct perf_event *event)
  275. {
  276. struct hw_perf_event *hwc = &event->hw;
  277. sparc_perf_event_update(event, hwc, hwc->idx);
  278. }
  279. static void sparc_pmu_unthrottle(struct perf_event *event)
  280. {
  281. struct hw_perf_event *hwc = &event->hw;
  282. sparc_pmu_enable_event(hwc, hwc->idx);
  283. }
  284. static atomic_t active_events = ATOMIC_INIT(0);
  285. static DEFINE_MUTEX(pmc_grab_mutex);
  286. void perf_event_grab_pmc(void)
  287. {
  288. if (atomic_inc_not_zero(&active_events))
  289. return;
  290. mutex_lock(&pmc_grab_mutex);
  291. if (atomic_read(&active_events) == 0) {
  292. if (atomic_read(&nmi_active) > 0) {
  293. on_each_cpu(stop_nmi_watchdog, NULL, 1);
  294. BUG_ON(atomic_read(&nmi_active) != 0);
  295. }
  296. atomic_inc(&active_events);
  297. }
  298. mutex_unlock(&pmc_grab_mutex);
  299. }
  300. void perf_event_release_pmc(void)
  301. {
  302. if (atomic_dec_and_mutex_lock(&active_events, &pmc_grab_mutex)) {
  303. if (atomic_read(&nmi_active) == 0)
  304. on_each_cpu(start_nmi_watchdog, NULL, 1);
  305. mutex_unlock(&pmc_grab_mutex);
  306. }
  307. }
  308. static void hw_perf_event_destroy(struct perf_event *event)
  309. {
  310. perf_event_release_pmc();
  311. }
  312. static int __hw_perf_event_init(struct perf_event *event)
  313. {
  314. struct perf_event_attr *attr = &event->attr;
  315. struct hw_perf_event *hwc = &event->hw;
  316. const struct perf_event_map *pmap;
  317. u64 enc;
  318. if (atomic_read(&nmi_active) < 0)
  319. return -ENODEV;
  320. if (attr->type != PERF_TYPE_HARDWARE)
  321. return -EOPNOTSUPP;
  322. if (attr->config >= sparc_pmu->max_events)
  323. return -EINVAL;
  324. perf_event_grab_pmc();
  325. event->destroy = hw_perf_event_destroy;
  326. /* We save the enable bits in the config_base. So to
  327. * turn off sampling just write 'config', and to enable
  328. * things write 'config | config_base'.
  329. */
  330. hwc->config_base = sparc_pmu->irq_bit;
  331. if (!attr->exclude_user)
  332. hwc->config_base |= PCR_UTRACE;
  333. if (!attr->exclude_kernel)
  334. hwc->config_base |= PCR_STRACE;
  335. if (!attr->exclude_hv)
  336. hwc->config_base |= sparc_pmu->hv_bit;
  337. if (!hwc->sample_period) {
  338. hwc->sample_period = MAX_PERIOD;
  339. hwc->last_period = hwc->sample_period;
  340. atomic64_set(&hwc->period_left, hwc->sample_period);
  341. }
  342. pmap = sparc_pmu->event_map(attr->config);
  343. enc = pmap->encoding;
  344. if (pmap->pic_mask & PIC_UPPER) {
  345. hwc->idx = PIC_UPPER_INDEX;
  346. enc <<= sparc_pmu->upper_shift;
  347. } else {
  348. hwc->idx = PIC_LOWER_INDEX;
  349. enc <<= sparc_pmu->lower_shift;
  350. }
  351. hwc->config |= enc;
  352. return 0;
  353. }
  354. static const struct pmu pmu = {
  355. .enable = sparc_pmu_enable,
  356. .disable = sparc_pmu_disable,
  357. .read = sparc_pmu_read,
  358. .unthrottle = sparc_pmu_unthrottle,
  359. };
  360. const struct pmu *hw_perf_event_init(struct perf_event *event)
  361. {
  362. int err = __hw_perf_event_init(event);
  363. if (err)
  364. return ERR_PTR(err);
  365. return &pmu;
  366. }
  367. void perf_event_print_debug(void)
  368. {
  369. unsigned long flags;
  370. u64 pcr, pic;
  371. int cpu;
  372. if (!sparc_pmu)
  373. return;
  374. local_irq_save(flags);
  375. cpu = smp_processor_id();
  376. pcr = pcr_ops->read();
  377. read_pic(pic);
  378. pr_info("\n");
  379. pr_info("CPU#%d: PCR[%016llx] PIC[%016llx]\n",
  380. cpu, pcr, pic);
  381. local_irq_restore(flags);
  382. }
  383. static int __kprobes perf_event_nmi_handler(struct notifier_block *self,
  384. unsigned long cmd, void *__args)
  385. {
  386. struct die_args *args = __args;
  387. struct perf_sample_data data;
  388. struct cpu_hw_events *cpuc;
  389. struct pt_regs *regs;
  390. int idx;
  391. if (!atomic_read(&active_events))
  392. return NOTIFY_DONE;
  393. switch (cmd) {
  394. case DIE_NMI:
  395. break;
  396. default:
  397. return NOTIFY_DONE;
  398. }
  399. regs = args->regs;
  400. data.addr = 0;
  401. cpuc = &__get_cpu_var(cpu_hw_events);
  402. for (idx = 0; idx < MAX_HWEVENTS; idx++) {
  403. struct perf_event *event = cpuc->events[idx];
  404. struct hw_perf_event *hwc;
  405. u64 val;
  406. if (!test_bit(idx, cpuc->active_mask))
  407. continue;
  408. hwc = &event->hw;
  409. val = sparc_perf_event_update(event, hwc, idx);
  410. if (val & (1ULL << 31))
  411. continue;
  412. data.period = event->hw.last_period;
  413. if (!sparc_perf_event_set_period(event, hwc, idx))
  414. continue;
  415. if (perf_event_overflow(event, 1, &data, regs))
  416. sparc_pmu_disable_event(hwc, idx);
  417. }
  418. return NOTIFY_STOP;
  419. }
  420. static __read_mostly struct notifier_block perf_event_nmi_notifier = {
  421. .notifier_call = perf_event_nmi_handler,
  422. };
  423. static bool __init supported_pmu(void)
  424. {
  425. if (!strcmp(sparc_pmu_type, "ultra3i")) {
  426. sparc_pmu = &ultra3i_pmu;
  427. return true;
  428. }
  429. if (!strcmp(sparc_pmu_type, "niagara2")) {
  430. sparc_pmu = &niagara2_pmu;
  431. return true;
  432. }
  433. return false;
  434. }
  435. void __init init_hw_perf_events(void)
  436. {
  437. pr_info("Performance events: ");
  438. if (!supported_pmu()) {
  439. pr_cont("No support for PMU type '%s'\n", sparc_pmu_type);
  440. return;
  441. }
  442. pr_cont("Supported PMU type is '%s'\n", sparc_pmu_type);
  443. /* All sparc64 PMUs currently have 2 events. But this simple
  444. * driver only supports one active event at a time.
  445. */
  446. perf_max_events = 1;
  447. register_die_notifier(&perf_event_nmi_notifier);
  448. }