perf_event.c 23 KB

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