perf_event.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842
  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 = local64_read(&hwc->period_left);
  211. long period = hwc->sample_period;
  212. int ret = 0;
  213. if (unlikely(left <= -period)) {
  214. left = period;
  215. local64_set(&hwc->period_left, left);
  216. hwc->last_period = period;
  217. ret = 1;
  218. }
  219. if (unlikely(left <= 0)) {
  220. left += period;
  221. local64_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. local64_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 = local64_read(&hwc->prev_count);
  259. new_raw_count = alpha_read_pmc(idx);
  260. if (local64_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. local64_add(delta, &event->count);
  271. local64_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. cpuc->idx_mask |= (1<<cpuc->current_idx[j]);
  341. continue;
  342. }
  343. alpha_perf_event_set_period(pe, hwc, idx);
  344. cpuc->current_idx[j] = idx;
  345. cpuc->idx_mask |= (1<<cpuc->current_idx[j]);
  346. }
  347. cpuc->config = cpuc->event[0]->hw.config_base;
  348. }
  349. /* Schedule perf HW event on to PMU.
  350. * - this function is called from outside this module via the pmu struct
  351. * returned from perf event initialisation.
  352. */
  353. static int alpha_pmu_enable(struct perf_event *event)
  354. {
  355. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  356. int n0;
  357. int ret;
  358. unsigned long flags;
  359. /*
  360. * The Sparc code has the IRQ disable first followed by the perf
  361. * disable, however this can lead to an overflowed counter with the
  362. * PMI disabled on rare occasions. The alpha_perf_event_update()
  363. * routine should detect this situation by noting a negative delta,
  364. * nevertheless we disable the PMCs first to enable a potential
  365. * final PMI to occur before we disable interrupts.
  366. */
  367. perf_disable();
  368. local_irq_save(flags);
  369. /* Default to error to be returned */
  370. ret = -EAGAIN;
  371. /* Insert event on to PMU and if successful modify ret to valid return */
  372. n0 = cpuc->n_events;
  373. if (n0 < alpha_pmu->num_pmcs) {
  374. cpuc->event[n0] = event;
  375. cpuc->evtype[n0] = event->hw.event_base;
  376. cpuc->current_idx[n0] = PMC_NO_INDEX;
  377. if (!alpha_check_constraints(cpuc->event, cpuc->evtype, n0+1)) {
  378. cpuc->n_events++;
  379. cpuc->n_added++;
  380. ret = 0;
  381. }
  382. }
  383. local_irq_restore(flags);
  384. perf_enable();
  385. return ret;
  386. }
  387. /* Disable performance monitoring unit
  388. * - this function is called from outside this module via the pmu struct
  389. * returned from perf event initialisation.
  390. */
  391. static void alpha_pmu_disable(struct perf_event *event)
  392. {
  393. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  394. struct hw_perf_event *hwc = &event->hw;
  395. unsigned long flags;
  396. int j;
  397. perf_disable();
  398. local_irq_save(flags);
  399. for (j = 0; j < cpuc->n_events; j++) {
  400. if (event == cpuc->event[j]) {
  401. int idx = cpuc->current_idx[j];
  402. /* Shift remaining entries down into the existing
  403. * slot.
  404. */
  405. while (++j < cpuc->n_events) {
  406. cpuc->event[j - 1] = cpuc->event[j];
  407. cpuc->evtype[j - 1] = cpuc->evtype[j];
  408. cpuc->current_idx[j - 1] =
  409. cpuc->current_idx[j];
  410. }
  411. /* Absorb the final count and turn off the event. */
  412. alpha_perf_event_update(event, hwc, idx, 0);
  413. perf_event_update_userpage(event);
  414. cpuc->idx_mask &= ~(1UL<<idx);
  415. cpuc->n_events--;
  416. break;
  417. }
  418. }
  419. local_irq_restore(flags);
  420. perf_enable();
  421. }
  422. static void alpha_pmu_read(struct perf_event *event)
  423. {
  424. struct hw_perf_event *hwc = &event->hw;
  425. alpha_perf_event_update(event, hwc, hwc->idx, 0);
  426. }
  427. static void alpha_pmu_unthrottle(struct perf_event *event)
  428. {
  429. struct hw_perf_event *hwc = &event->hw;
  430. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  431. cpuc->idx_mask |= 1UL<<hwc->idx;
  432. wrperfmon(PERFMON_CMD_ENABLE, (1UL<<hwc->idx));
  433. }
  434. /*
  435. * Check that CPU performance counters are supported.
  436. * - currently support EV67 and later CPUs.
  437. * - actually some later revisions of the EV6 have the same PMC model as the
  438. * EV67 but we don't do suffiently deep CPU detection to detect them.
  439. * Bad luck to the very few people who might have one, I guess.
  440. */
  441. static int supported_cpu(void)
  442. {
  443. struct percpu_struct *cpu;
  444. unsigned long cputype;
  445. /* Get cpu type from HW */
  446. cpu = (struct percpu_struct *)((char *)hwrpb + hwrpb->processor_offset);
  447. cputype = cpu->type & 0xffffffff;
  448. /* Include all of EV67, EV68, EV7, EV79 and EV69 as supported. */
  449. return (cputype >= EV67_CPU) && (cputype <= EV69_CPU);
  450. }
  451. static void hw_perf_event_destroy(struct perf_event *event)
  452. {
  453. /* Nothing to be done! */
  454. return;
  455. }
  456. static int __hw_perf_event_init(struct perf_event *event)
  457. {
  458. struct perf_event_attr *attr = &event->attr;
  459. struct hw_perf_event *hwc = &event->hw;
  460. struct perf_event *evts[MAX_HWEVENTS];
  461. unsigned long evtypes[MAX_HWEVENTS];
  462. int idx_rubbish_bin[MAX_HWEVENTS];
  463. int ev;
  464. int n;
  465. /* We only support a limited range of HARDWARE event types with one
  466. * only programmable via a RAW event type.
  467. */
  468. if (attr->type == PERF_TYPE_HARDWARE) {
  469. if (attr->config >= alpha_pmu->max_events)
  470. return -EINVAL;
  471. ev = alpha_pmu->event_map[attr->config];
  472. } else if (attr->type == PERF_TYPE_HW_CACHE) {
  473. return -EOPNOTSUPP;
  474. } else if (attr->type == PERF_TYPE_RAW) {
  475. ev = attr->config & 0xff;
  476. } else {
  477. return -EOPNOTSUPP;
  478. }
  479. if (ev < 0) {
  480. return ev;
  481. }
  482. /* The EV67 does not support mode exclusion */
  483. if (attr->exclude_kernel || attr->exclude_user
  484. || attr->exclude_hv || attr->exclude_idle) {
  485. return -EPERM;
  486. }
  487. /*
  488. * We place the event type in event_base here and leave calculation
  489. * of the codes to programme the PMU for alpha_pmu_enable() because
  490. * it is only then we will know what HW events are actually
  491. * scheduled on to the PMU. At that point the code to programme the
  492. * PMU is put into config_base and the PMC to use is placed into
  493. * idx. We initialise idx (below) to PMC_NO_INDEX to indicate that
  494. * it is yet to be determined.
  495. */
  496. hwc->event_base = ev;
  497. /* Collect events in a group together suitable for calling
  498. * alpha_check_constraints() to verify that the group as a whole can
  499. * be scheduled on to the PMU.
  500. */
  501. n = 0;
  502. if (event->group_leader != event) {
  503. n = collect_events(event->group_leader,
  504. alpha_pmu->num_pmcs - 1,
  505. evts, evtypes, idx_rubbish_bin);
  506. if (n < 0)
  507. return -EINVAL;
  508. }
  509. evtypes[n] = hwc->event_base;
  510. evts[n] = event;
  511. if (alpha_check_constraints(evts, evtypes, n + 1))
  512. return -EINVAL;
  513. /* Indicate that PMU config and idx are yet to be determined. */
  514. hwc->config_base = 0;
  515. hwc->idx = PMC_NO_INDEX;
  516. event->destroy = hw_perf_event_destroy;
  517. /*
  518. * Most architectures reserve the PMU for their use at this point.
  519. * As there is no existing mechanism to arbitrate usage and there
  520. * appears to be no other user of the Alpha PMU we just assume
  521. * that we can just use it, hence a NO-OP here.
  522. *
  523. * Maybe an alpha_reserve_pmu() routine should be implemented but is
  524. * anything else ever going to use it?
  525. */
  526. if (!hwc->sample_period) {
  527. hwc->sample_period = alpha_pmu->pmc_max_period[0];
  528. hwc->last_period = hwc->sample_period;
  529. local64_set(&hwc->period_left, hwc->sample_period);
  530. }
  531. return 0;
  532. }
  533. static const struct pmu pmu = {
  534. .enable = alpha_pmu_enable,
  535. .disable = alpha_pmu_disable,
  536. .read = alpha_pmu_read,
  537. .unthrottle = alpha_pmu_unthrottle,
  538. };
  539. /*
  540. * Main entry point to initialise a HW performance event.
  541. */
  542. const struct pmu *hw_perf_event_init(struct perf_event *event)
  543. {
  544. int err;
  545. if (!alpha_pmu)
  546. return ERR_PTR(-ENODEV);
  547. /* Do the real initialisation work. */
  548. err = __hw_perf_event_init(event);
  549. if (err)
  550. return ERR_PTR(err);
  551. return &pmu;
  552. }
  553. /*
  554. * Main entry point - enable HW performance counters.
  555. */
  556. void hw_perf_enable(void)
  557. {
  558. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  559. if (cpuc->enabled)
  560. return;
  561. cpuc->enabled = 1;
  562. barrier();
  563. if (cpuc->n_events > 0) {
  564. /* Update cpuc with information from any new scheduled events. */
  565. maybe_change_configuration(cpuc);
  566. /* Start counting the desired events. */
  567. wrperfmon(PERFMON_CMD_LOGGING_OPTIONS, EV67_PCTR_MODE_AGGREGATE);
  568. wrperfmon(PERFMON_CMD_DESIRED_EVENTS, cpuc->config);
  569. wrperfmon(PERFMON_CMD_ENABLE, cpuc->idx_mask);
  570. }
  571. }
  572. /*
  573. * Main entry point - disable HW performance counters.
  574. */
  575. void hw_perf_disable(void)
  576. {
  577. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  578. if (!cpuc->enabled)
  579. return;
  580. cpuc->enabled = 0;
  581. cpuc->n_added = 0;
  582. wrperfmon(PERFMON_CMD_DISABLE, cpuc->idx_mask);
  583. }
  584. /*
  585. * Main entry point - don't know when this is called but it
  586. * obviously dumps debug info.
  587. */
  588. void perf_event_print_debug(void)
  589. {
  590. unsigned long flags;
  591. unsigned long pcr;
  592. int pcr0, pcr1;
  593. int cpu;
  594. if (!supported_cpu())
  595. return;
  596. local_irq_save(flags);
  597. cpu = smp_processor_id();
  598. pcr = wrperfmon(PERFMON_CMD_READ, 0);
  599. pcr0 = (pcr >> alpha_pmu->pmc_count_shift[0]) & alpha_pmu->pmc_count_mask[0];
  600. pcr1 = (pcr >> alpha_pmu->pmc_count_shift[1]) & alpha_pmu->pmc_count_mask[1];
  601. pr_info("CPU#%d: PCTR0[%06x] PCTR1[%06x]\n", cpu, pcr0, pcr1);
  602. local_irq_restore(flags);
  603. }
  604. /*
  605. * Performance Monitoring Interrupt Service Routine called when a PMC
  606. * overflows. The PMC that overflowed is passed in la_ptr.
  607. */
  608. static void alpha_perf_event_irq_handler(unsigned long la_ptr,
  609. struct pt_regs *regs)
  610. {
  611. struct cpu_hw_events *cpuc;
  612. struct perf_sample_data data;
  613. struct perf_event *event;
  614. struct hw_perf_event *hwc;
  615. int idx, j;
  616. __get_cpu_var(irq_pmi_count)++;
  617. cpuc = &__get_cpu_var(cpu_hw_events);
  618. /* Completely counting through the PMC's period to trigger a new PMC
  619. * overflow interrupt while in this interrupt routine is utterly
  620. * disastrous! The EV6 and EV67 counters are sufficiently large to
  621. * prevent this but to be really sure disable the PMCs.
  622. */
  623. wrperfmon(PERFMON_CMD_DISABLE, cpuc->idx_mask);
  624. /* la_ptr is the counter that overflowed. */
  625. if (unlikely(la_ptr >= perf_max_events)) {
  626. /* This should never occur! */
  627. irq_err_count++;
  628. pr_warning("PMI: silly index %ld\n", la_ptr);
  629. wrperfmon(PERFMON_CMD_ENABLE, cpuc->idx_mask);
  630. return;
  631. }
  632. idx = la_ptr;
  633. perf_sample_data_init(&data, 0);
  634. for (j = 0; j < cpuc->n_events; j++) {
  635. if (cpuc->current_idx[j] == idx)
  636. break;
  637. }
  638. if (unlikely(j == cpuc->n_events)) {
  639. /* This can occur if the event is disabled right on a PMC overflow. */
  640. wrperfmon(PERFMON_CMD_ENABLE, cpuc->idx_mask);
  641. return;
  642. }
  643. event = cpuc->event[j];
  644. if (unlikely(!event)) {
  645. /* This should never occur! */
  646. irq_err_count++;
  647. pr_warning("PMI: No event at index %d!\n", idx);
  648. wrperfmon(PERFMON_CMD_ENABLE, cpuc->idx_mask);
  649. return;
  650. }
  651. hwc = &event->hw;
  652. alpha_perf_event_update(event, hwc, idx, alpha_pmu->pmc_max_period[idx]+1);
  653. data.period = event->hw.last_period;
  654. if (alpha_perf_event_set_period(event, hwc, idx)) {
  655. if (perf_event_overflow(event, 1, &data, regs)) {
  656. /* Interrupts coming too quickly; "throttle" the
  657. * counter, i.e., disable it for a little while.
  658. */
  659. cpuc->idx_mask &= ~(1UL<<idx);
  660. }
  661. }
  662. wrperfmon(PERFMON_CMD_ENABLE, cpuc->idx_mask);
  663. return;
  664. }
  665. /*
  666. * Init call to initialise performance events at kernel startup.
  667. */
  668. void __init init_hw_perf_events(void)
  669. {
  670. pr_info("Performance events: ");
  671. if (!supported_cpu()) {
  672. pr_cont("No support for your CPU.\n");
  673. return;
  674. }
  675. pr_cont("Supported CPU type!\n");
  676. /* Override performance counter IRQ vector */
  677. perf_irq = alpha_perf_event_irq_handler;
  678. /* And set up PMU specification */
  679. alpha_pmu = &ev67_pmu;
  680. perf_max_events = alpha_pmu->num_pmcs;
  681. }