perf_event.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886
  1. /*
  2. * Hardware performance events for the Alpha.
  3. *
  4. * We implement HW counts on the EV67 and subsequent CPUs only.
  5. *
  6. * (C) 2010 Michael J. Cree
  7. *
  8. * Somewhat based on the Sparc code, and to a lesser extent the PowerPC and
  9. * ARM code, which are copyright by their respective authors.
  10. */
  11. #include <linux/perf_event.h>
  12. #include <linux/kprobes.h>
  13. #include <linux/kernel.h>
  14. #include <linux/kdebug.h>
  15. #include <linux/mutex.h>
  16. #include <asm/hwrpb.h>
  17. #include <asm/atomic.h>
  18. #include <asm/irq.h>
  19. #include <asm/irq_regs.h>
  20. #include <asm/pal.h>
  21. #include <asm/wrperfmon.h>
  22. #include <asm/hw_irq.h>
  23. /* The maximum number of PMCs on any Alpha CPU whatsoever. */
  24. #define MAX_HWEVENTS 3
  25. #define PMC_NO_INDEX -1
  26. /* For tracking PMCs and the hw events they monitor on each CPU. */
  27. struct cpu_hw_events {
  28. int enabled;
  29. /* Number of events scheduled; also number entries valid in arrays below. */
  30. int n_events;
  31. /* Number events added since last hw_perf_disable(). */
  32. int n_added;
  33. /* Events currently scheduled. */
  34. struct perf_event *event[MAX_HWEVENTS];
  35. /* Event type of each scheduled event. */
  36. unsigned long evtype[MAX_HWEVENTS];
  37. /* Current index of each scheduled event; if not yet determined
  38. * contains PMC_NO_INDEX.
  39. */
  40. int current_idx[MAX_HWEVENTS];
  41. /* The active PMCs' config for easy use with wrperfmon(). */
  42. unsigned long config;
  43. /* The active counters' indices for easy use with wrperfmon(). */
  44. unsigned long idx_mask;
  45. };
  46. DEFINE_PER_CPU(struct cpu_hw_events, cpu_hw_events);
  47. /*
  48. * A structure to hold the description of the PMCs available on a particular
  49. * type of Alpha CPU.
  50. */
  51. struct alpha_pmu_t {
  52. /* Mapping of the perf system hw event types to indigenous event types */
  53. const int *event_map;
  54. /* The number of entries in the event_map */
  55. int max_events;
  56. /* The number of PMCs on this Alpha */
  57. int num_pmcs;
  58. /*
  59. * All PMC counters reside in the IBOX register PCTR. This is the
  60. * LSB of the counter.
  61. */
  62. int pmc_count_shift[MAX_HWEVENTS];
  63. /*
  64. * The mask that isolates the PMC bits when the LSB of the counter
  65. * is shifted to bit 0.
  66. */
  67. unsigned long pmc_count_mask[MAX_HWEVENTS];
  68. /* The maximum period the PMC can count. */
  69. unsigned long pmc_max_period[MAX_HWEVENTS];
  70. /*
  71. * The maximum value that may be written to the counter due to
  72. * hardware restrictions is pmc_max_period - pmc_left.
  73. */
  74. long pmc_left[3];
  75. /* Subroutine for allocation of PMCs. Enforces constraints. */
  76. int (*check_constraints)(struct perf_event **, unsigned long *, int);
  77. };
  78. /*
  79. * The Alpha CPU PMU description currently in operation. This is set during
  80. * the boot process to the specific CPU of the machine.
  81. */
  82. static const struct alpha_pmu_t *alpha_pmu;
  83. #define HW_OP_UNSUPPORTED -1
  84. /*
  85. * The hardware description of the EV67, EV68, EV69, EV7 and EV79 PMUs
  86. * follow. Since they are identical we refer to them collectively as the
  87. * EV67 henceforth.
  88. */
  89. /*
  90. * EV67 PMC event types
  91. *
  92. * There is no one-to-one mapping of the possible hw event types to the
  93. * actual codes that are used to program the PMCs hence we introduce our
  94. * own hw event type identifiers.
  95. */
  96. enum ev67_pmc_event_type {
  97. EV67_CYCLES = 1,
  98. EV67_INSTRUCTIONS,
  99. EV67_BCACHEMISS,
  100. EV67_MBOXREPLAY,
  101. EV67_LAST_ET
  102. };
  103. #define EV67_NUM_EVENT_TYPES (EV67_LAST_ET-EV67_CYCLES)
  104. /* Mapping of the hw event types to the perf tool interface */
  105. static const int ev67_perfmon_event_map[] = {
  106. [PERF_COUNT_HW_CPU_CYCLES] = EV67_CYCLES,
  107. [PERF_COUNT_HW_INSTRUCTIONS] = EV67_INSTRUCTIONS,
  108. [PERF_COUNT_HW_CACHE_REFERENCES] = HW_OP_UNSUPPORTED,
  109. [PERF_COUNT_HW_CACHE_MISSES] = EV67_BCACHEMISS,
  110. };
  111. struct ev67_mapping_t {
  112. int config;
  113. int idx;
  114. };
  115. /*
  116. * The mapping used for one event only - these must be in same order as enum
  117. * ev67_pmc_event_type definition.
  118. */
  119. static const struct ev67_mapping_t ev67_mapping[] = {
  120. {EV67_PCTR_INSTR_CYCLES, 1}, /* EV67_CYCLES, */
  121. {EV67_PCTR_INSTR_CYCLES, 0}, /* EV67_INSTRUCTIONS */
  122. {EV67_PCTR_INSTR_BCACHEMISS, 1}, /* EV67_BCACHEMISS */
  123. {EV67_PCTR_CYCLES_MBOX, 1} /* EV67_MBOXREPLAY */
  124. };
  125. /*
  126. * Check that a group of events can be simultaneously scheduled on to the
  127. * EV67 PMU. Also allocate counter indices and config.
  128. */
  129. static int ev67_check_constraints(struct perf_event **event,
  130. unsigned long *evtype, int n_ev)
  131. {
  132. int idx0;
  133. unsigned long config;
  134. idx0 = ev67_mapping[evtype[0]-1].idx;
  135. config = ev67_mapping[evtype[0]-1].config;
  136. if (n_ev == 1)
  137. goto success;
  138. BUG_ON(n_ev != 2);
  139. if (evtype[0] == EV67_MBOXREPLAY || evtype[1] == EV67_MBOXREPLAY) {
  140. /* MBOX replay traps must be on PMC 1 */
  141. idx0 = (evtype[0] == EV67_MBOXREPLAY) ? 1 : 0;
  142. /* Only cycles can accompany MBOX replay traps */
  143. if (evtype[idx0] == EV67_CYCLES) {
  144. config = EV67_PCTR_CYCLES_MBOX;
  145. goto success;
  146. }
  147. }
  148. if (evtype[0] == EV67_BCACHEMISS || evtype[1] == EV67_BCACHEMISS) {
  149. /* Bcache misses must be on PMC 1 */
  150. idx0 = (evtype[0] == EV67_BCACHEMISS) ? 1 : 0;
  151. /* Only instructions can accompany Bcache misses */
  152. if (evtype[idx0] == EV67_INSTRUCTIONS) {
  153. config = EV67_PCTR_INSTR_BCACHEMISS;
  154. goto success;
  155. }
  156. }
  157. if (evtype[0] == EV67_INSTRUCTIONS || evtype[1] == EV67_INSTRUCTIONS) {
  158. /* Instructions must be on PMC 0 */
  159. idx0 = (evtype[0] == EV67_INSTRUCTIONS) ? 0 : 1;
  160. /* By this point only cycles can accompany instructions */
  161. if (evtype[idx0^1] == EV67_CYCLES) {
  162. config = EV67_PCTR_INSTR_CYCLES;
  163. goto success;
  164. }
  165. }
  166. /* Otherwise, darn it, there is a conflict. */
  167. return -1;
  168. success:
  169. event[0]->hw.idx = idx0;
  170. event[0]->hw.config_base = config;
  171. if (n_ev == 2) {
  172. event[1]->hw.idx = idx0 ^ 1;
  173. event[1]->hw.config_base = config;
  174. }
  175. return 0;
  176. }
  177. static const struct alpha_pmu_t ev67_pmu = {
  178. .event_map = ev67_perfmon_event_map,
  179. .max_events = ARRAY_SIZE(ev67_perfmon_event_map),
  180. .num_pmcs = 2,
  181. .pmc_count_shift = {EV67_PCTR_0_COUNT_SHIFT, EV67_PCTR_1_COUNT_SHIFT, 0},
  182. .pmc_count_mask = {EV67_PCTR_0_COUNT_MASK, EV67_PCTR_1_COUNT_MASK, 0},
  183. .pmc_max_period = {(1UL<<20) - 1, (1UL<<20) - 1, 0},
  184. .pmc_left = {16, 4, 0},
  185. .check_constraints = ev67_check_constraints
  186. };
  187. /*
  188. * Helper routines to ensure that we read/write only the correct PMC bits
  189. * when calling the wrperfmon PALcall.
  190. */
  191. static inline void alpha_write_pmc(int idx, unsigned long val)
  192. {
  193. val &= alpha_pmu->pmc_count_mask[idx];
  194. val <<= alpha_pmu->pmc_count_shift[idx];
  195. val |= (1<<idx);
  196. wrperfmon(PERFMON_CMD_WRITE, val);
  197. }
  198. static inline unsigned long alpha_read_pmc(int idx)
  199. {
  200. unsigned long val;
  201. val = wrperfmon(PERFMON_CMD_READ, 0);
  202. val >>= alpha_pmu->pmc_count_shift[idx];
  203. val &= alpha_pmu->pmc_count_mask[idx];
  204. return val;
  205. }
  206. /* Set a new period to sample over */
  207. static int alpha_perf_event_set_period(struct perf_event *event,
  208. struct hw_perf_event *hwc, int idx)
  209. {
  210. long left = atomic64_read(&hwc->period_left);
  211. long period = hwc->sample_period;
  212. int ret = 0;
  213. if (unlikely(left <= -period)) {
  214. left = period;
  215. atomic64_set(&hwc->period_left, left);
  216. hwc->last_period = period;
  217. ret = 1;
  218. }
  219. if (unlikely(left <= 0)) {
  220. left += period;
  221. atomic64_set(&hwc->period_left, left);
  222. hwc->last_period = period;
  223. ret = 1;
  224. }
  225. /*
  226. * Hardware restrictions require that the counters must not be
  227. * written with values that are too close to the maximum period.
  228. */
  229. if (unlikely(left < alpha_pmu->pmc_left[idx]))
  230. left = alpha_pmu->pmc_left[idx];
  231. if (left > (long)alpha_pmu->pmc_max_period[idx])
  232. left = alpha_pmu->pmc_max_period[idx];
  233. atomic64_set(&hwc->prev_count, (unsigned long)(-left));
  234. alpha_write_pmc(idx, (unsigned long)(-left));
  235. perf_event_update_userpage(event);
  236. return ret;
  237. }
  238. /*
  239. * Calculates the count (the 'delta') since the last time the PMC was read.
  240. *
  241. * As the PMCs' full period can easily be exceeded within the perf system
  242. * sampling period we cannot use any high order bits as a guard bit in the
  243. * PMCs to detect overflow as is done by other architectures. The code here
  244. * calculates the delta on the basis that there is no overflow when ovf is
  245. * zero. The value passed via ovf by the interrupt handler corrects for
  246. * overflow.
  247. *
  248. * This can be racey on rare occasions -- a call to this routine can occur
  249. * with an overflowed counter just before the PMI service routine is called.
  250. * The check for delta negative hopefully always rectifies this situation.
  251. */
  252. static unsigned long alpha_perf_event_update(struct perf_event *event,
  253. struct hw_perf_event *hwc, int idx, long ovf)
  254. {
  255. long prev_raw_count, new_raw_count;
  256. long delta;
  257. again:
  258. prev_raw_count = atomic64_read(&hwc->prev_count);
  259. new_raw_count = alpha_read_pmc(idx);
  260. if (atomic64_cmpxchg(&hwc->prev_count, prev_raw_count,
  261. new_raw_count) != prev_raw_count)
  262. goto again;
  263. delta = (new_raw_count - (prev_raw_count & alpha_pmu->pmc_count_mask[idx])) + ovf;
  264. /* It is possible on very rare occasions that the PMC has overflowed
  265. * but the interrupt is yet to come. Detect and fix this situation.
  266. */
  267. if (unlikely(delta < 0)) {
  268. delta += alpha_pmu->pmc_max_period[idx] + 1;
  269. }
  270. atomic64_add(delta, &event->count);
  271. atomic64_sub(delta, &hwc->period_left);
  272. return new_raw_count;
  273. }
  274. /*
  275. * Collect all HW events into the array event[].
  276. */
  277. static int collect_events(struct perf_event *group, int max_count,
  278. struct perf_event *event[], unsigned long *evtype,
  279. int *current_idx)
  280. {
  281. struct perf_event *pe;
  282. int n = 0;
  283. if (!is_software_event(group)) {
  284. if (n >= max_count)
  285. return -1;
  286. event[n] = group;
  287. evtype[n] = group->hw.event_base;
  288. current_idx[n++] = PMC_NO_INDEX;
  289. }
  290. list_for_each_entry(pe, &group->sibling_list, group_entry) {
  291. if (!is_software_event(pe) && pe->state != PERF_EVENT_STATE_OFF) {
  292. if (n >= max_count)
  293. return -1;
  294. event[n] = pe;
  295. evtype[n] = pe->hw.event_base;
  296. current_idx[n++] = PMC_NO_INDEX;
  297. }
  298. }
  299. return n;
  300. }
  301. /*
  302. * Check that a group of events can be simultaneously scheduled on to the PMU.
  303. */
  304. static int alpha_check_constraints(struct perf_event **events,
  305. unsigned long *evtypes, int n_ev)
  306. {
  307. /* No HW events is possible from hw_perf_group_sched_in(). */
  308. if (n_ev == 0)
  309. return 0;
  310. if (n_ev > alpha_pmu->num_pmcs)
  311. return -1;
  312. return alpha_pmu->check_constraints(events, evtypes, n_ev);
  313. }
  314. /*
  315. * If new events have been scheduled then update cpuc with the new
  316. * configuration. This may involve shifting cycle counts from one PMC to
  317. * another.
  318. */
  319. static void maybe_change_configuration(struct cpu_hw_events *cpuc)
  320. {
  321. int j;
  322. if (cpuc->n_added == 0)
  323. return;
  324. /* Find counters that are moving to another PMC and update */
  325. for (j = 0; j < cpuc->n_events; j++) {
  326. struct perf_event *pe = cpuc->event[j];
  327. if (cpuc->current_idx[j] != PMC_NO_INDEX &&
  328. cpuc->current_idx[j] != pe->hw.idx) {
  329. alpha_perf_event_update(pe, &pe->hw, cpuc->current_idx[j], 0);
  330. cpuc->current_idx[j] = PMC_NO_INDEX;
  331. }
  332. }
  333. /* Assign to counters all unassigned events. */
  334. cpuc->idx_mask = 0;
  335. for (j = 0; j < cpuc->n_events; j++) {
  336. struct perf_event *pe = cpuc->event[j];
  337. struct hw_perf_event *hwc = &pe->hw;
  338. int idx = hwc->idx;
  339. if (cpuc->current_idx[j] == PMC_NO_INDEX) {
  340. alpha_perf_event_set_period(pe, hwc, idx);
  341. cpuc->current_idx[j] = idx;
  342. }
  343. if (!(hwc->state & PERF_HES_STOPPED))
  344. cpuc->idx_mask |= (1<<cpuc->current_idx[j]);
  345. }
  346. cpuc->config = cpuc->event[0]->hw.config_base;
  347. }
  348. /* Schedule perf HW event on to PMU.
  349. * - this function is called from outside this module via the pmu struct
  350. * returned from perf event initialisation.
  351. */
  352. static int alpha_pmu_add(struct perf_event *event, int flags)
  353. {
  354. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  355. int n0;
  356. int ret;
  357. unsigned long flags;
  358. /*
  359. * The Sparc code has the IRQ disable first followed by the perf
  360. * disable, however this can lead to an overflowed counter with the
  361. * PMI disabled on rare occasions. The alpha_perf_event_update()
  362. * routine should detect this situation by noting a negative delta,
  363. * nevertheless we disable the PMCs first to enable a potential
  364. * final PMI to occur before we disable interrupts.
  365. */
  366. perf_pmu_disable(event->pmu);
  367. local_irq_save(flags);
  368. /* Default to error to be returned */
  369. ret = -EAGAIN;
  370. /* Insert event on to PMU and if successful modify ret to valid return */
  371. n0 = cpuc->n_events;
  372. if (n0 < alpha_pmu->num_pmcs) {
  373. cpuc->event[n0] = event;
  374. cpuc->evtype[n0] = event->hw.event_base;
  375. cpuc->current_idx[n0] = PMC_NO_INDEX;
  376. if (!alpha_check_constraints(cpuc->event, cpuc->evtype, n0+1)) {
  377. cpuc->n_events++;
  378. cpuc->n_added++;
  379. ret = 0;
  380. }
  381. }
  382. hwc->state = PERF_HES_UPTODATE;
  383. if (!(flags & PERF_EF_START))
  384. hwc->state |= PERF_HES_STOPPED;
  385. local_irq_restore(flags);
  386. perf_pmu_enable(event->pmu);
  387. return ret;
  388. }
  389. /* Disable performance monitoring unit
  390. * - this function is called from outside this module via the pmu struct
  391. * returned from perf event initialisation.
  392. */
  393. static void alpha_pmu_del(struct perf_event *event, int flags)
  394. {
  395. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  396. struct hw_perf_event *hwc = &event->hw;
  397. unsigned long flags;
  398. int j;
  399. perf_pmu_disable(event->pmu);
  400. local_irq_save(flags);
  401. for (j = 0; j < cpuc->n_events; j++) {
  402. if (event == cpuc->event[j]) {
  403. int idx = cpuc->current_idx[j];
  404. /* Shift remaining entries down into the existing
  405. * slot.
  406. */
  407. while (++j < cpuc->n_events) {
  408. cpuc->event[j - 1] = cpuc->event[j];
  409. cpuc->evtype[j - 1] = cpuc->evtype[j];
  410. cpuc->current_idx[j - 1] =
  411. cpuc->current_idx[j];
  412. }
  413. /* Absorb the final count and turn off the event. */
  414. alpha_perf_event_update(event, hwc, idx, 0);
  415. perf_event_update_userpage(event);
  416. cpuc->idx_mask &= ~(1UL<<idx);
  417. cpuc->n_events--;
  418. break;
  419. }
  420. }
  421. local_irq_restore(flags);
  422. perf_pmu_enable(event->pmu);
  423. }
  424. static void alpha_pmu_read(struct perf_event *event)
  425. {
  426. struct hw_perf_event *hwc = &event->hw;
  427. alpha_perf_event_update(event, hwc, hwc->idx, 0);
  428. }
  429. static void alpha_pmu_stop(struct perf_event *event, int flags)
  430. {
  431. struct hw_perf_event *hwc = &event->hw;
  432. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  433. if (!(hwc->state & PERF_HES_STOPPED)) {
  434. cpuc->idx_mask &= !(1UL<<hwc->idx);
  435. hwc->state |= PERF_HES_STOPPED;
  436. }
  437. if ((flags & PERF_EF_UPDATE) && !(hwc->state & PERF_HES_UPTODATE)) {
  438. alpha_perf_event_update(event, hwc, hwc->idx, 0);
  439. hwc->state |= PERF_HES_UPTODATE;
  440. }
  441. if (cpuc->enabled)
  442. wrperfmon(PERFMON_CMD_ENABLE, (1UL<<hwc->idx));
  443. }
  444. static void alpha_pmu_start(struct perf_event *event, int flags)
  445. {
  446. struct hw_perf_event *hwc = &event->hw;
  447. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  448. if (WARN_ON_ONCE(!(hwc->state & PERF_HES_STOPPED)))
  449. return;
  450. if (flags & PERF_EF_RELOAD) {
  451. WARN_ON_ONCE(!(hwc->state & PERF_HES_UPTODATE));
  452. alpha_perf_event_set_period(event, hwc, hwc->idx);
  453. }
  454. hwc->state = 0;
  455. cpuc->idx_mask |= 1UL<<hwc->idx;
  456. if (cpuc->enabled)
  457. wrperfmon(PERFMON_CMD_ENABLE, (1UL<<hwc->idx));
  458. }
  459. /*
  460. * Check that CPU performance counters are supported.
  461. * - currently support EV67 and later CPUs.
  462. * - actually some later revisions of the EV6 have the same PMC model as the
  463. * EV67 but we don't do suffiently deep CPU detection to detect them.
  464. * Bad luck to the very few people who might have one, I guess.
  465. */
  466. static int supported_cpu(void)
  467. {
  468. struct percpu_struct *cpu;
  469. unsigned long cputype;
  470. /* Get cpu type from HW */
  471. cpu = (struct percpu_struct *)((char *)hwrpb + hwrpb->processor_offset);
  472. cputype = cpu->type & 0xffffffff;
  473. /* Include all of EV67, EV68, EV7, EV79 and EV69 as supported. */
  474. return (cputype >= EV67_CPU) && (cputype <= EV69_CPU);
  475. }
  476. static void hw_perf_event_destroy(struct perf_event *event)
  477. {
  478. /* Nothing to be done! */
  479. return;
  480. }
  481. static int __hw_perf_event_init(struct perf_event *event)
  482. {
  483. struct perf_event_attr *attr = &event->attr;
  484. struct hw_perf_event *hwc = &event->hw;
  485. struct perf_event *evts[MAX_HWEVENTS];
  486. unsigned long evtypes[MAX_HWEVENTS];
  487. int idx_rubbish_bin[MAX_HWEVENTS];
  488. int ev;
  489. int n;
  490. /* We only support a limited range of HARDWARE event types with one
  491. * only programmable via a RAW event type.
  492. */
  493. if (attr->type == PERF_TYPE_HARDWARE) {
  494. if (attr->config >= alpha_pmu->max_events)
  495. return -EINVAL;
  496. ev = alpha_pmu->event_map[attr->config];
  497. } else if (attr->type == PERF_TYPE_HW_CACHE) {
  498. return -EOPNOTSUPP;
  499. } else if (attr->type == PERF_TYPE_RAW) {
  500. ev = attr->config & 0xff;
  501. } else {
  502. return -EOPNOTSUPP;
  503. }
  504. if (ev < 0) {
  505. return ev;
  506. }
  507. /* The EV67 does not support mode exclusion */
  508. if (attr->exclude_kernel || attr->exclude_user
  509. || attr->exclude_hv || attr->exclude_idle) {
  510. return -EPERM;
  511. }
  512. /*
  513. * We place the event type in event_base here and leave calculation
  514. * of the codes to programme the PMU for alpha_pmu_enable() because
  515. * it is only then we will know what HW events are actually
  516. * scheduled on to the PMU. At that point the code to programme the
  517. * PMU is put into config_base and the PMC to use is placed into
  518. * idx. We initialise idx (below) to PMC_NO_INDEX to indicate that
  519. * it is yet to be determined.
  520. */
  521. hwc->event_base = ev;
  522. /* Collect events in a group together suitable for calling
  523. * alpha_check_constraints() to verify that the group as a whole can
  524. * be scheduled on to the PMU.
  525. */
  526. n = 0;
  527. if (event->group_leader != event) {
  528. n = collect_events(event->group_leader,
  529. alpha_pmu->num_pmcs - 1,
  530. evts, evtypes, idx_rubbish_bin);
  531. if (n < 0)
  532. return -EINVAL;
  533. }
  534. evtypes[n] = hwc->event_base;
  535. evts[n] = event;
  536. if (alpha_check_constraints(evts, evtypes, n + 1))
  537. return -EINVAL;
  538. /* Indicate that PMU config and idx are yet to be determined. */
  539. hwc->config_base = 0;
  540. hwc->idx = PMC_NO_INDEX;
  541. event->destroy = hw_perf_event_destroy;
  542. /*
  543. * Most architectures reserve the PMU for their use at this point.
  544. * As there is no existing mechanism to arbitrate usage and there
  545. * appears to be no other user of the Alpha PMU we just assume
  546. * that we can just use it, hence a NO-OP here.
  547. *
  548. * Maybe an alpha_reserve_pmu() routine should be implemented but is
  549. * anything else ever going to use it?
  550. */
  551. if (!hwc->sample_period) {
  552. hwc->sample_period = alpha_pmu->pmc_max_period[0];
  553. hwc->last_period = hwc->sample_period;
  554. atomic64_set(&hwc->period_left, hwc->sample_period);
  555. }
  556. return 0;
  557. }
  558. /*
  559. * Main entry point to initialise a HW performance event.
  560. */
  561. static int alpha_pmu_event_init(struct perf_event *event)
  562. {
  563. int err;
  564. switch (event->attr.type) {
  565. case PERF_TYPE_RAW:
  566. case PERF_TYPE_HARDWARE:
  567. case PERF_TYPE_HW_CACHE:
  568. break;
  569. default:
  570. return -ENOENT;
  571. }
  572. if (!alpha_pmu)
  573. return -ENODEV;
  574. /* Do the real initialisation work. */
  575. err = __hw_perf_event_init(event);
  576. return err;
  577. }
  578. /*
  579. * Main entry point - enable HW performance counters.
  580. */
  581. static void alpha_pmu_enable(struct pmu *pmu)
  582. {
  583. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  584. if (cpuc->enabled)
  585. return;
  586. cpuc->enabled = 1;
  587. barrier();
  588. if (cpuc->n_events > 0) {
  589. /* Update cpuc with information from any new scheduled events. */
  590. maybe_change_configuration(cpuc);
  591. /* Start counting the desired events. */
  592. wrperfmon(PERFMON_CMD_LOGGING_OPTIONS, EV67_PCTR_MODE_AGGREGATE);
  593. wrperfmon(PERFMON_CMD_DESIRED_EVENTS, cpuc->config);
  594. wrperfmon(PERFMON_CMD_ENABLE, cpuc->idx_mask);
  595. }
  596. }
  597. /*
  598. * Main entry point - disable HW performance counters.
  599. */
  600. static void alpha_pmu_disable(struct pmu *pmu)
  601. {
  602. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  603. if (!cpuc->enabled)
  604. return;
  605. cpuc->enabled = 0;
  606. cpuc->n_added = 0;
  607. wrperfmon(PERFMON_CMD_DISABLE, cpuc->idx_mask);
  608. }
  609. static struct pmu pmu = {
  610. .pmu_enable = alpha_pmu_enable,
  611. .pmu_disable = alpha_pmu_disable,
  612. .event_init = alpha_pmu_event_init,
  613. .add = alpha_pmu_add,
  614. .del = alpha_pmu_del,
  615. .start = alpha_pmu_start,
  616. .stop = alpha_pmu_stop,
  617. .read = alpha_pmu_read,
  618. };
  619. /*
  620. * Main entry point - don't know when this is called but it
  621. * obviously dumps debug info.
  622. */
  623. void perf_event_print_debug(void)
  624. {
  625. unsigned long flags;
  626. unsigned long pcr;
  627. int pcr0, pcr1;
  628. int cpu;
  629. if (!supported_cpu())
  630. return;
  631. local_irq_save(flags);
  632. cpu = smp_processor_id();
  633. pcr = wrperfmon(PERFMON_CMD_READ, 0);
  634. pcr0 = (pcr >> alpha_pmu->pmc_count_shift[0]) & alpha_pmu->pmc_count_mask[0];
  635. pcr1 = (pcr >> alpha_pmu->pmc_count_shift[1]) & alpha_pmu->pmc_count_mask[1];
  636. pr_info("CPU#%d: PCTR0[%06x] PCTR1[%06x]\n", cpu, pcr0, pcr1);
  637. local_irq_restore(flags);
  638. }
  639. /*
  640. * Performance Monitoring Interrupt Service Routine called when a PMC
  641. * overflows. The PMC that overflowed is passed in la_ptr.
  642. */
  643. static void alpha_perf_event_irq_handler(unsigned long la_ptr,
  644. struct pt_regs *regs)
  645. {
  646. struct cpu_hw_events *cpuc;
  647. struct perf_sample_data data;
  648. struct perf_event *event;
  649. struct hw_perf_event *hwc;
  650. int idx, j;
  651. __get_cpu_var(irq_pmi_count)++;
  652. cpuc = &__get_cpu_var(cpu_hw_events);
  653. /* Completely counting through the PMC's period to trigger a new PMC
  654. * overflow interrupt while in this interrupt routine is utterly
  655. * disastrous! The EV6 and EV67 counters are sufficiently large to
  656. * prevent this but to be really sure disable the PMCs.
  657. */
  658. wrperfmon(PERFMON_CMD_DISABLE, cpuc->idx_mask);
  659. /* la_ptr is the counter that overflowed. */
  660. if (unlikely(la_ptr >= perf_max_events)) {
  661. /* This should never occur! */
  662. irq_err_count++;
  663. pr_warning("PMI: silly index %ld\n", la_ptr);
  664. wrperfmon(PERFMON_CMD_ENABLE, cpuc->idx_mask);
  665. return;
  666. }
  667. idx = la_ptr;
  668. perf_sample_data_init(&data, 0);
  669. for (j = 0; j < cpuc->n_events; j++) {
  670. if (cpuc->current_idx[j] == idx)
  671. break;
  672. }
  673. if (unlikely(j == cpuc->n_events)) {
  674. /* This can occur if the event is disabled right on a PMC overflow. */
  675. wrperfmon(PERFMON_CMD_ENABLE, cpuc->idx_mask);
  676. return;
  677. }
  678. event = cpuc->event[j];
  679. if (unlikely(!event)) {
  680. /* This should never occur! */
  681. irq_err_count++;
  682. pr_warning("PMI: No event at index %d!\n", idx);
  683. wrperfmon(PERFMON_CMD_ENABLE, cpuc->idx_mask);
  684. return;
  685. }
  686. hwc = &event->hw;
  687. alpha_perf_event_update(event, hwc, idx, alpha_pmu->pmc_max_period[idx]+1);
  688. data.period = event->hw.last_period;
  689. if (alpha_perf_event_set_period(event, hwc, idx)) {
  690. if (perf_event_overflow(event, 1, &data, regs)) {
  691. /* Interrupts coming too quickly; "throttle" the
  692. * counter, i.e., disable it for a little while.
  693. */
  694. cpuc->idx_mask &= ~(1UL<<idx);
  695. }
  696. }
  697. wrperfmon(PERFMON_CMD_ENABLE, cpuc->idx_mask);
  698. return;
  699. }
  700. /*
  701. * Init call to initialise performance events at kernel startup.
  702. */
  703. void __init init_hw_perf_events(void)
  704. {
  705. pr_info("Performance events: ");
  706. if (!supported_cpu()) {
  707. pr_cont("No support for your CPU.\n");
  708. return;
  709. }
  710. pr_cont("Supported CPU type!\n");
  711. /* Override performance counter IRQ vector */
  712. perf_irq = alpha_perf_event_irq_handler;
  713. /* And set up PMU specification */
  714. alpha_pmu = &ev67_pmu;
  715. perf_max_events = alpha_pmu->num_pmcs;
  716. perf_pmu_register(&pmu);
  717. }