perf_event.c 23 KB

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