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