perf_event.c 22 KB

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