perf_event.c 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277
  1. #undef DEBUG
  2. /*
  3. * ARM performance counter support.
  4. *
  5. * Copyright (C) 2009 picoChip Designs, Ltd., Jamie Iles
  6. *
  7. * ARMv7 support: Jean Pihet <jpihet@mvista.com>
  8. * 2010 (c) MontaVista Software, LLC.
  9. *
  10. * This code is based on the sparc64 perf event code, which is in turn based
  11. * on the x86 code. Callchain code is based on the ARM OProfile backtrace
  12. * code.
  13. */
  14. #define pr_fmt(fmt) "hw perfevents: " fmt
  15. #include <linux/interrupt.h>
  16. #include <linux/kernel.h>
  17. #include <linux/perf_event.h>
  18. #include <linux/spinlock.h>
  19. #include <linux/uaccess.h>
  20. #include <asm/cputype.h>
  21. #include <asm/irq.h>
  22. #include <asm/irq_regs.h>
  23. #include <asm/pmu.h>
  24. #include <asm/stacktrace.h>
  25. static const struct pmu_irqs *pmu_irqs;
  26. /*
  27. * Hardware lock to serialize accesses to PMU registers. Needed for the
  28. * read/modify/write sequences.
  29. */
  30. DEFINE_SPINLOCK(pmu_lock);
  31. /*
  32. * ARMv6 supports a maximum of 3 events, starting from index 1. If we add
  33. * another platform that supports more, we need to increase this to be the
  34. * largest of all platforms.
  35. *
  36. * ARMv7 supports up to 32 events:
  37. * cycle counter CCNT + 31 events counters CNT0..30.
  38. * Cortex-A8 has 1+4 counters, Cortex-A9 has 1+6 counters.
  39. */
  40. #define ARMPMU_MAX_HWEVENTS 33
  41. /* The events for a given CPU. */
  42. struct cpu_hw_events {
  43. /*
  44. * The events that are active on the CPU for the given index. Index 0
  45. * is reserved.
  46. */
  47. struct perf_event *events[ARMPMU_MAX_HWEVENTS];
  48. /*
  49. * A 1 bit for an index indicates that the counter is being used for
  50. * an event. A 0 means that the counter can be used.
  51. */
  52. unsigned long used_mask[BITS_TO_LONGS(ARMPMU_MAX_HWEVENTS)];
  53. /*
  54. * A 1 bit for an index indicates that the counter is actively being
  55. * used.
  56. */
  57. unsigned long active_mask[BITS_TO_LONGS(ARMPMU_MAX_HWEVENTS)];
  58. };
  59. DEFINE_PER_CPU(struct cpu_hw_events, cpu_hw_events);
  60. struct arm_pmu {
  61. char *name;
  62. irqreturn_t (*handle_irq)(int irq_num, void *dev);
  63. void (*enable)(struct hw_perf_event *evt, int idx);
  64. void (*disable)(struct hw_perf_event *evt, int idx);
  65. int (*event_map)(int evt);
  66. u64 (*raw_event)(u64);
  67. int (*get_event_idx)(struct cpu_hw_events *cpuc,
  68. struct hw_perf_event *hwc);
  69. u32 (*read_counter)(int idx);
  70. void (*write_counter)(int idx, u32 val);
  71. void (*start)(void);
  72. void (*stop)(void);
  73. int num_events;
  74. u64 max_period;
  75. };
  76. /* Set at runtime when we know what CPU type we are. */
  77. static const struct arm_pmu *armpmu;
  78. #define HW_OP_UNSUPPORTED 0xFFFF
  79. #define C(_x) \
  80. PERF_COUNT_HW_CACHE_##_x
  81. #define CACHE_OP_UNSUPPORTED 0xFFFF
  82. static unsigned armpmu_perf_cache_map[PERF_COUNT_HW_CACHE_MAX]
  83. [PERF_COUNT_HW_CACHE_OP_MAX]
  84. [PERF_COUNT_HW_CACHE_RESULT_MAX];
  85. static int
  86. armpmu_map_cache_event(u64 config)
  87. {
  88. unsigned int cache_type, cache_op, cache_result, ret;
  89. cache_type = (config >> 0) & 0xff;
  90. if (cache_type >= PERF_COUNT_HW_CACHE_MAX)
  91. return -EINVAL;
  92. cache_op = (config >> 8) & 0xff;
  93. if (cache_op >= PERF_COUNT_HW_CACHE_OP_MAX)
  94. return -EINVAL;
  95. cache_result = (config >> 16) & 0xff;
  96. if (cache_result >= PERF_COUNT_HW_CACHE_RESULT_MAX)
  97. return -EINVAL;
  98. ret = (int)armpmu_perf_cache_map[cache_type][cache_op][cache_result];
  99. if (ret == CACHE_OP_UNSUPPORTED)
  100. return -ENOENT;
  101. return ret;
  102. }
  103. static int
  104. armpmu_event_set_period(struct perf_event *event,
  105. struct hw_perf_event *hwc,
  106. int idx)
  107. {
  108. s64 left = atomic64_read(&hwc->period_left);
  109. s64 period = hwc->sample_period;
  110. int ret = 0;
  111. if (unlikely(left <= -period)) {
  112. left = period;
  113. atomic64_set(&hwc->period_left, left);
  114. hwc->last_period = period;
  115. ret = 1;
  116. }
  117. if (unlikely(left <= 0)) {
  118. left += period;
  119. atomic64_set(&hwc->period_left, left);
  120. hwc->last_period = period;
  121. ret = 1;
  122. }
  123. if (left > (s64)armpmu->max_period)
  124. left = armpmu->max_period;
  125. atomic64_set(&hwc->prev_count, (u64)-left);
  126. armpmu->write_counter(idx, (u64)(-left) & 0xffffffff);
  127. perf_event_update_userpage(event);
  128. return ret;
  129. }
  130. static u64
  131. armpmu_event_update(struct perf_event *event,
  132. struct hw_perf_event *hwc,
  133. int idx)
  134. {
  135. int shift = 64 - 32;
  136. s64 prev_raw_count, new_raw_count;
  137. s64 delta;
  138. again:
  139. prev_raw_count = atomic64_read(&hwc->prev_count);
  140. new_raw_count = armpmu->read_counter(idx);
  141. if (atomic64_cmpxchg(&hwc->prev_count, prev_raw_count,
  142. new_raw_count) != prev_raw_count)
  143. goto again;
  144. delta = (new_raw_count << shift) - (prev_raw_count << shift);
  145. delta >>= shift;
  146. atomic64_add(delta, &event->count);
  147. atomic64_sub(delta, &hwc->period_left);
  148. return new_raw_count;
  149. }
  150. static void
  151. armpmu_disable(struct perf_event *event)
  152. {
  153. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  154. struct hw_perf_event *hwc = &event->hw;
  155. int idx = hwc->idx;
  156. WARN_ON(idx < 0);
  157. clear_bit(idx, cpuc->active_mask);
  158. armpmu->disable(hwc, idx);
  159. barrier();
  160. armpmu_event_update(event, hwc, idx);
  161. cpuc->events[idx] = NULL;
  162. clear_bit(idx, cpuc->used_mask);
  163. perf_event_update_userpage(event);
  164. }
  165. static void
  166. armpmu_read(struct perf_event *event)
  167. {
  168. struct hw_perf_event *hwc = &event->hw;
  169. /* Don't read disabled counters! */
  170. if (hwc->idx < 0)
  171. return;
  172. armpmu_event_update(event, hwc, hwc->idx);
  173. }
  174. static void
  175. armpmu_unthrottle(struct perf_event *event)
  176. {
  177. struct hw_perf_event *hwc = &event->hw;
  178. /*
  179. * Set the period again. Some counters can't be stopped, so when we
  180. * were throttled we simply disabled the IRQ source and the counter
  181. * may have been left counting. If we don't do this step then we may
  182. * get an interrupt too soon or *way* too late if the overflow has
  183. * happened since disabling.
  184. */
  185. armpmu_event_set_period(event, hwc, hwc->idx);
  186. armpmu->enable(hwc, hwc->idx);
  187. }
  188. static int
  189. armpmu_enable(struct perf_event *event)
  190. {
  191. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  192. struct hw_perf_event *hwc = &event->hw;
  193. int idx;
  194. int err = 0;
  195. /* If we don't have a space for the counter then finish early. */
  196. idx = armpmu->get_event_idx(cpuc, hwc);
  197. if (idx < 0) {
  198. err = idx;
  199. goto out;
  200. }
  201. /*
  202. * If there is an event in the counter we are going to use then make
  203. * sure it is disabled.
  204. */
  205. event->hw.idx = idx;
  206. armpmu->disable(hwc, idx);
  207. cpuc->events[idx] = event;
  208. set_bit(idx, cpuc->active_mask);
  209. /* Set the period for the event. */
  210. armpmu_event_set_period(event, hwc, idx);
  211. /* Enable the event. */
  212. armpmu->enable(hwc, idx);
  213. /* Propagate our changes to the userspace mapping. */
  214. perf_event_update_userpage(event);
  215. out:
  216. return err;
  217. }
  218. static struct pmu pmu = {
  219. .enable = armpmu_enable,
  220. .disable = armpmu_disable,
  221. .unthrottle = armpmu_unthrottle,
  222. .read = armpmu_read,
  223. };
  224. static int
  225. validate_event(struct cpu_hw_events *cpuc,
  226. struct perf_event *event)
  227. {
  228. struct hw_perf_event fake_event = event->hw;
  229. if (event->pmu && event->pmu != &pmu)
  230. return 0;
  231. return armpmu->get_event_idx(cpuc, &fake_event) >= 0;
  232. }
  233. static int
  234. validate_group(struct perf_event *event)
  235. {
  236. struct perf_event *sibling, *leader = event->group_leader;
  237. struct cpu_hw_events fake_pmu;
  238. memset(&fake_pmu, 0, sizeof(fake_pmu));
  239. if (!validate_event(&fake_pmu, leader))
  240. return -ENOSPC;
  241. list_for_each_entry(sibling, &leader->sibling_list, group_entry) {
  242. if (!validate_event(&fake_pmu, sibling))
  243. return -ENOSPC;
  244. }
  245. if (!validate_event(&fake_pmu, event))
  246. return -ENOSPC;
  247. return 0;
  248. }
  249. static int
  250. armpmu_reserve_hardware(void)
  251. {
  252. int i;
  253. int err;
  254. pmu_irqs = reserve_pmu();
  255. if (IS_ERR(pmu_irqs)) {
  256. pr_warning("unable to reserve pmu\n");
  257. return PTR_ERR(pmu_irqs);
  258. }
  259. init_pmu();
  260. if (pmu_irqs->num_irqs < 1) {
  261. pr_err("no irqs for PMUs defined\n");
  262. return -ENODEV;
  263. }
  264. for (i = 0; i < pmu_irqs->num_irqs; ++i) {
  265. err = request_irq(pmu_irqs->irqs[i], armpmu->handle_irq,
  266. IRQF_DISABLED | IRQF_NOBALANCING,
  267. "armpmu", NULL);
  268. if (err) {
  269. pr_warning("unable to request IRQ%d for ARM "
  270. "perf counters\n", pmu_irqs->irqs[i]);
  271. break;
  272. }
  273. }
  274. if (err) {
  275. for (i = i - 1; i >= 0; --i)
  276. free_irq(pmu_irqs->irqs[i], NULL);
  277. release_pmu(pmu_irqs);
  278. pmu_irqs = NULL;
  279. }
  280. return err;
  281. }
  282. static void
  283. armpmu_release_hardware(void)
  284. {
  285. int i;
  286. for (i = pmu_irqs->num_irqs - 1; i >= 0; --i)
  287. free_irq(pmu_irqs->irqs[i], NULL);
  288. armpmu->stop();
  289. release_pmu(pmu_irqs);
  290. pmu_irqs = NULL;
  291. }
  292. static atomic_t active_events = ATOMIC_INIT(0);
  293. static DEFINE_MUTEX(pmu_reserve_mutex);
  294. static void
  295. hw_perf_event_destroy(struct perf_event *event)
  296. {
  297. if (atomic_dec_and_mutex_lock(&active_events, &pmu_reserve_mutex)) {
  298. armpmu_release_hardware();
  299. mutex_unlock(&pmu_reserve_mutex);
  300. }
  301. }
  302. static int
  303. __hw_perf_event_init(struct perf_event *event)
  304. {
  305. struct hw_perf_event *hwc = &event->hw;
  306. int mapping, err;
  307. /* Decode the generic type into an ARM event identifier. */
  308. if (PERF_TYPE_HARDWARE == event->attr.type) {
  309. mapping = armpmu->event_map(event->attr.config);
  310. } else if (PERF_TYPE_HW_CACHE == event->attr.type) {
  311. mapping = armpmu_map_cache_event(event->attr.config);
  312. } else if (PERF_TYPE_RAW == event->attr.type) {
  313. mapping = armpmu->raw_event(event->attr.config);
  314. } else {
  315. pr_debug("event type %x not supported\n", event->attr.type);
  316. return -EOPNOTSUPP;
  317. }
  318. if (mapping < 0) {
  319. pr_debug("event %x:%llx not supported\n", event->attr.type,
  320. event->attr.config);
  321. return mapping;
  322. }
  323. /*
  324. * Check whether we need to exclude the counter from certain modes.
  325. * The ARM performance counters are on all of the time so if someone
  326. * has asked us for some excludes then we have to fail.
  327. */
  328. if (event->attr.exclude_kernel || event->attr.exclude_user ||
  329. event->attr.exclude_hv || event->attr.exclude_idle) {
  330. pr_debug("ARM performance counters do not support "
  331. "mode exclusion\n");
  332. return -EPERM;
  333. }
  334. /*
  335. * We don't assign an index until we actually place the event onto
  336. * hardware. Use -1 to signify that we haven't decided where to put it
  337. * yet. For SMP systems, each core has it's own PMU so we can't do any
  338. * clever allocation or constraints checking at this point.
  339. */
  340. hwc->idx = -1;
  341. /*
  342. * Store the event encoding into the config_base field. config and
  343. * event_base are unused as the only 2 things we need to know are
  344. * the event mapping and the counter to use. The counter to use is
  345. * also the indx and the config_base is the event type.
  346. */
  347. hwc->config_base = (unsigned long)mapping;
  348. hwc->config = 0;
  349. hwc->event_base = 0;
  350. if (!hwc->sample_period) {
  351. hwc->sample_period = armpmu->max_period;
  352. hwc->last_period = hwc->sample_period;
  353. atomic64_set(&hwc->period_left, hwc->sample_period);
  354. }
  355. err = 0;
  356. if (event->group_leader != event) {
  357. err = validate_group(event);
  358. if (err)
  359. return -EINVAL;
  360. }
  361. return err;
  362. }
  363. const struct pmu *
  364. hw_perf_event_init(struct perf_event *event)
  365. {
  366. int err = 0;
  367. if (!armpmu)
  368. return ERR_PTR(-ENODEV);
  369. event->destroy = hw_perf_event_destroy;
  370. if (!atomic_inc_not_zero(&active_events)) {
  371. if (atomic_read(&active_events) > perf_max_events) {
  372. atomic_dec(&active_events);
  373. return ERR_PTR(-ENOSPC);
  374. }
  375. mutex_lock(&pmu_reserve_mutex);
  376. if (atomic_read(&active_events) == 0) {
  377. err = armpmu_reserve_hardware();
  378. }
  379. if (!err)
  380. atomic_inc(&active_events);
  381. mutex_unlock(&pmu_reserve_mutex);
  382. }
  383. if (err)
  384. return ERR_PTR(err);
  385. err = __hw_perf_event_init(event);
  386. if (err)
  387. hw_perf_event_destroy(event);
  388. return err ? ERR_PTR(err) : &pmu;
  389. }
  390. void
  391. hw_perf_enable(void)
  392. {
  393. /* Enable all of the perf events on hardware. */
  394. int idx;
  395. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  396. if (!armpmu)
  397. return;
  398. for (idx = 0; idx <= armpmu->num_events; ++idx) {
  399. struct perf_event *event = cpuc->events[idx];
  400. if (!event)
  401. continue;
  402. armpmu->enable(&event->hw, idx);
  403. }
  404. armpmu->start();
  405. }
  406. void
  407. hw_perf_disable(void)
  408. {
  409. if (armpmu)
  410. armpmu->stop();
  411. }
  412. /*
  413. * ARMv6 Performance counter handling code.
  414. *
  415. * ARMv6 has 2 configurable performance counters and a single cycle counter.
  416. * They all share a single reset bit but can be written to zero so we can use
  417. * that for a reset.
  418. *
  419. * The counters can't be individually enabled or disabled so when we remove
  420. * one event and replace it with another we could get spurious counts from the
  421. * wrong event. However, we can take advantage of the fact that the
  422. * performance counters can export events to the event bus, and the event bus
  423. * itself can be monitored. This requires that we *don't* export the events to
  424. * the event bus. The procedure for disabling a configurable counter is:
  425. * - change the counter to count the ETMEXTOUT[0] signal (0x20). This
  426. * effectively stops the counter from counting.
  427. * - disable the counter's interrupt generation (each counter has it's
  428. * own interrupt enable bit).
  429. * Once stopped, the counter value can be written as 0 to reset.
  430. *
  431. * To enable a counter:
  432. * - enable the counter's interrupt generation.
  433. * - set the new event type.
  434. *
  435. * Note: the dedicated cycle counter only counts cycles and can't be
  436. * enabled/disabled independently of the others. When we want to disable the
  437. * cycle counter, we have to just disable the interrupt reporting and start
  438. * ignoring that counter. When re-enabling, we have to reset the value and
  439. * enable the interrupt.
  440. */
  441. enum armv6_perf_types {
  442. ARMV6_PERFCTR_ICACHE_MISS = 0x0,
  443. ARMV6_PERFCTR_IBUF_STALL = 0x1,
  444. ARMV6_PERFCTR_DDEP_STALL = 0x2,
  445. ARMV6_PERFCTR_ITLB_MISS = 0x3,
  446. ARMV6_PERFCTR_DTLB_MISS = 0x4,
  447. ARMV6_PERFCTR_BR_EXEC = 0x5,
  448. ARMV6_PERFCTR_BR_MISPREDICT = 0x6,
  449. ARMV6_PERFCTR_INSTR_EXEC = 0x7,
  450. ARMV6_PERFCTR_DCACHE_HIT = 0x9,
  451. ARMV6_PERFCTR_DCACHE_ACCESS = 0xA,
  452. ARMV6_PERFCTR_DCACHE_MISS = 0xB,
  453. ARMV6_PERFCTR_DCACHE_WBACK = 0xC,
  454. ARMV6_PERFCTR_SW_PC_CHANGE = 0xD,
  455. ARMV6_PERFCTR_MAIN_TLB_MISS = 0xF,
  456. ARMV6_PERFCTR_EXPL_D_ACCESS = 0x10,
  457. ARMV6_PERFCTR_LSU_FULL_STALL = 0x11,
  458. ARMV6_PERFCTR_WBUF_DRAINED = 0x12,
  459. ARMV6_PERFCTR_CPU_CYCLES = 0xFF,
  460. ARMV6_PERFCTR_NOP = 0x20,
  461. };
  462. enum armv6_counters {
  463. ARMV6_CYCLE_COUNTER = 1,
  464. ARMV6_COUNTER0,
  465. ARMV6_COUNTER1,
  466. };
  467. /*
  468. * The hardware events that we support. We do support cache operations but
  469. * we have harvard caches and no way to combine instruction and data
  470. * accesses/misses in hardware.
  471. */
  472. static const unsigned armv6_perf_map[PERF_COUNT_HW_MAX] = {
  473. [PERF_COUNT_HW_CPU_CYCLES] = ARMV6_PERFCTR_CPU_CYCLES,
  474. [PERF_COUNT_HW_INSTRUCTIONS] = ARMV6_PERFCTR_INSTR_EXEC,
  475. [PERF_COUNT_HW_CACHE_REFERENCES] = HW_OP_UNSUPPORTED,
  476. [PERF_COUNT_HW_CACHE_MISSES] = HW_OP_UNSUPPORTED,
  477. [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = ARMV6_PERFCTR_BR_EXEC,
  478. [PERF_COUNT_HW_BRANCH_MISSES] = ARMV6_PERFCTR_BR_MISPREDICT,
  479. [PERF_COUNT_HW_BUS_CYCLES] = HW_OP_UNSUPPORTED,
  480. };
  481. static const unsigned armv6_perf_cache_map[PERF_COUNT_HW_CACHE_MAX]
  482. [PERF_COUNT_HW_CACHE_OP_MAX]
  483. [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
  484. [C(L1D)] = {
  485. /*
  486. * The performance counters don't differentiate between read
  487. * and write accesses/misses so this isn't strictly correct,
  488. * but it's the best we can do. Writes and reads get
  489. * combined.
  490. */
  491. [C(OP_READ)] = {
  492. [C(RESULT_ACCESS)] = ARMV6_PERFCTR_DCACHE_ACCESS,
  493. [C(RESULT_MISS)] = ARMV6_PERFCTR_DCACHE_MISS,
  494. },
  495. [C(OP_WRITE)] = {
  496. [C(RESULT_ACCESS)] = ARMV6_PERFCTR_DCACHE_ACCESS,
  497. [C(RESULT_MISS)] = ARMV6_PERFCTR_DCACHE_MISS,
  498. },
  499. [C(OP_PREFETCH)] = {
  500. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  501. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  502. },
  503. },
  504. [C(L1I)] = {
  505. [C(OP_READ)] = {
  506. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  507. [C(RESULT_MISS)] = ARMV6_PERFCTR_ICACHE_MISS,
  508. },
  509. [C(OP_WRITE)] = {
  510. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  511. [C(RESULT_MISS)] = ARMV6_PERFCTR_ICACHE_MISS,
  512. },
  513. [C(OP_PREFETCH)] = {
  514. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  515. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  516. },
  517. },
  518. [C(LL)] = {
  519. [C(OP_READ)] = {
  520. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  521. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  522. },
  523. [C(OP_WRITE)] = {
  524. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  525. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  526. },
  527. [C(OP_PREFETCH)] = {
  528. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  529. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  530. },
  531. },
  532. [C(DTLB)] = {
  533. /*
  534. * The ARM performance counters can count micro DTLB misses,
  535. * micro ITLB misses and main TLB misses. There isn't an event
  536. * for TLB misses, so use the micro misses here and if users
  537. * want the main TLB misses they can use a raw counter.
  538. */
  539. [C(OP_READ)] = {
  540. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  541. [C(RESULT_MISS)] = ARMV6_PERFCTR_DTLB_MISS,
  542. },
  543. [C(OP_WRITE)] = {
  544. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  545. [C(RESULT_MISS)] = ARMV6_PERFCTR_DTLB_MISS,
  546. },
  547. [C(OP_PREFETCH)] = {
  548. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  549. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  550. },
  551. },
  552. [C(ITLB)] = {
  553. [C(OP_READ)] = {
  554. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  555. [C(RESULT_MISS)] = ARMV6_PERFCTR_ITLB_MISS,
  556. },
  557. [C(OP_WRITE)] = {
  558. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  559. [C(RESULT_MISS)] = ARMV6_PERFCTR_ITLB_MISS,
  560. },
  561. [C(OP_PREFETCH)] = {
  562. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  563. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  564. },
  565. },
  566. [C(BPU)] = {
  567. [C(OP_READ)] = {
  568. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  569. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  570. },
  571. [C(OP_WRITE)] = {
  572. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  573. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  574. },
  575. [C(OP_PREFETCH)] = {
  576. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  577. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  578. },
  579. },
  580. };
  581. enum armv6mpcore_perf_types {
  582. ARMV6MPCORE_PERFCTR_ICACHE_MISS = 0x0,
  583. ARMV6MPCORE_PERFCTR_IBUF_STALL = 0x1,
  584. ARMV6MPCORE_PERFCTR_DDEP_STALL = 0x2,
  585. ARMV6MPCORE_PERFCTR_ITLB_MISS = 0x3,
  586. ARMV6MPCORE_PERFCTR_DTLB_MISS = 0x4,
  587. ARMV6MPCORE_PERFCTR_BR_EXEC = 0x5,
  588. ARMV6MPCORE_PERFCTR_BR_NOTPREDICT = 0x6,
  589. ARMV6MPCORE_PERFCTR_BR_MISPREDICT = 0x7,
  590. ARMV6MPCORE_PERFCTR_INSTR_EXEC = 0x8,
  591. ARMV6MPCORE_PERFCTR_DCACHE_RDACCESS = 0xA,
  592. ARMV6MPCORE_PERFCTR_DCACHE_RDMISS = 0xB,
  593. ARMV6MPCORE_PERFCTR_DCACHE_WRACCESS = 0xC,
  594. ARMV6MPCORE_PERFCTR_DCACHE_WRMISS = 0xD,
  595. ARMV6MPCORE_PERFCTR_DCACHE_EVICTION = 0xE,
  596. ARMV6MPCORE_PERFCTR_SW_PC_CHANGE = 0xF,
  597. ARMV6MPCORE_PERFCTR_MAIN_TLB_MISS = 0x10,
  598. ARMV6MPCORE_PERFCTR_EXPL_MEM_ACCESS = 0x11,
  599. ARMV6MPCORE_PERFCTR_LSU_FULL_STALL = 0x12,
  600. ARMV6MPCORE_PERFCTR_WBUF_DRAINED = 0x13,
  601. ARMV6MPCORE_PERFCTR_CPU_CYCLES = 0xFF,
  602. };
  603. /*
  604. * The hardware events that we support. We do support cache operations but
  605. * we have harvard caches and no way to combine instruction and data
  606. * accesses/misses in hardware.
  607. */
  608. static const unsigned armv6mpcore_perf_map[PERF_COUNT_HW_MAX] = {
  609. [PERF_COUNT_HW_CPU_CYCLES] = ARMV6MPCORE_PERFCTR_CPU_CYCLES,
  610. [PERF_COUNT_HW_INSTRUCTIONS] = ARMV6MPCORE_PERFCTR_INSTR_EXEC,
  611. [PERF_COUNT_HW_CACHE_REFERENCES] = HW_OP_UNSUPPORTED,
  612. [PERF_COUNT_HW_CACHE_MISSES] = HW_OP_UNSUPPORTED,
  613. [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = ARMV6MPCORE_PERFCTR_BR_EXEC,
  614. [PERF_COUNT_HW_BRANCH_MISSES] = ARMV6MPCORE_PERFCTR_BR_MISPREDICT,
  615. [PERF_COUNT_HW_BUS_CYCLES] = HW_OP_UNSUPPORTED,
  616. };
  617. static const unsigned armv6mpcore_perf_cache_map[PERF_COUNT_HW_CACHE_MAX]
  618. [PERF_COUNT_HW_CACHE_OP_MAX]
  619. [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
  620. [C(L1D)] = {
  621. [C(OP_READ)] = {
  622. [C(RESULT_ACCESS)] =
  623. ARMV6MPCORE_PERFCTR_DCACHE_RDACCESS,
  624. [C(RESULT_MISS)] =
  625. ARMV6MPCORE_PERFCTR_DCACHE_RDMISS,
  626. },
  627. [C(OP_WRITE)] = {
  628. [C(RESULT_ACCESS)] =
  629. ARMV6MPCORE_PERFCTR_DCACHE_WRACCESS,
  630. [C(RESULT_MISS)] =
  631. ARMV6MPCORE_PERFCTR_DCACHE_WRMISS,
  632. },
  633. [C(OP_PREFETCH)] = {
  634. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  635. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  636. },
  637. },
  638. [C(L1I)] = {
  639. [C(OP_READ)] = {
  640. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  641. [C(RESULT_MISS)] = ARMV6MPCORE_PERFCTR_ICACHE_MISS,
  642. },
  643. [C(OP_WRITE)] = {
  644. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  645. [C(RESULT_MISS)] = ARMV6MPCORE_PERFCTR_ICACHE_MISS,
  646. },
  647. [C(OP_PREFETCH)] = {
  648. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  649. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  650. },
  651. },
  652. [C(LL)] = {
  653. [C(OP_READ)] = {
  654. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  655. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  656. },
  657. [C(OP_WRITE)] = {
  658. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  659. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  660. },
  661. [C(OP_PREFETCH)] = {
  662. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  663. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  664. },
  665. },
  666. [C(DTLB)] = {
  667. /*
  668. * The ARM performance counters can count micro DTLB misses,
  669. * micro ITLB misses and main TLB misses. There isn't an event
  670. * for TLB misses, so use the micro misses here and if users
  671. * want the main TLB misses they can use a raw counter.
  672. */
  673. [C(OP_READ)] = {
  674. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  675. [C(RESULT_MISS)] = ARMV6MPCORE_PERFCTR_DTLB_MISS,
  676. },
  677. [C(OP_WRITE)] = {
  678. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  679. [C(RESULT_MISS)] = ARMV6MPCORE_PERFCTR_DTLB_MISS,
  680. },
  681. [C(OP_PREFETCH)] = {
  682. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  683. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  684. },
  685. },
  686. [C(ITLB)] = {
  687. [C(OP_READ)] = {
  688. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  689. [C(RESULT_MISS)] = ARMV6MPCORE_PERFCTR_ITLB_MISS,
  690. },
  691. [C(OP_WRITE)] = {
  692. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  693. [C(RESULT_MISS)] = ARMV6MPCORE_PERFCTR_ITLB_MISS,
  694. },
  695. [C(OP_PREFETCH)] = {
  696. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  697. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  698. },
  699. },
  700. [C(BPU)] = {
  701. [C(OP_READ)] = {
  702. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  703. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  704. },
  705. [C(OP_WRITE)] = {
  706. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  707. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  708. },
  709. [C(OP_PREFETCH)] = {
  710. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  711. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  712. },
  713. },
  714. };
  715. static inline unsigned long
  716. armv6_pmcr_read(void)
  717. {
  718. u32 val;
  719. asm volatile("mrc p15, 0, %0, c15, c12, 0" : "=r"(val));
  720. return val;
  721. }
  722. static inline void
  723. armv6_pmcr_write(unsigned long val)
  724. {
  725. asm volatile("mcr p15, 0, %0, c15, c12, 0" : : "r"(val));
  726. }
  727. #define ARMV6_PMCR_ENABLE (1 << 0)
  728. #define ARMV6_PMCR_CTR01_RESET (1 << 1)
  729. #define ARMV6_PMCR_CCOUNT_RESET (1 << 2)
  730. #define ARMV6_PMCR_CCOUNT_DIV (1 << 3)
  731. #define ARMV6_PMCR_COUNT0_IEN (1 << 4)
  732. #define ARMV6_PMCR_COUNT1_IEN (1 << 5)
  733. #define ARMV6_PMCR_CCOUNT_IEN (1 << 6)
  734. #define ARMV6_PMCR_COUNT0_OVERFLOW (1 << 8)
  735. #define ARMV6_PMCR_COUNT1_OVERFLOW (1 << 9)
  736. #define ARMV6_PMCR_CCOUNT_OVERFLOW (1 << 10)
  737. #define ARMV6_PMCR_EVT_COUNT0_SHIFT 20
  738. #define ARMV6_PMCR_EVT_COUNT0_MASK (0xFF << ARMV6_PMCR_EVT_COUNT0_SHIFT)
  739. #define ARMV6_PMCR_EVT_COUNT1_SHIFT 12
  740. #define ARMV6_PMCR_EVT_COUNT1_MASK (0xFF << ARMV6_PMCR_EVT_COUNT1_SHIFT)
  741. #define ARMV6_PMCR_OVERFLOWED_MASK \
  742. (ARMV6_PMCR_COUNT0_OVERFLOW | ARMV6_PMCR_COUNT1_OVERFLOW | \
  743. ARMV6_PMCR_CCOUNT_OVERFLOW)
  744. static inline int
  745. armv6_pmcr_has_overflowed(unsigned long pmcr)
  746. {
  747. return (pmcr & ARMV6_PMCR_OVERFLOWED_MASK);
  748. }
  749. static inline int
  750. armv6_pmcr_counter_has_overflowed(unsigned long pmcr,
  751. enum armv6_counters counter)
  752. {
  753. int ret = 0;
  754. if (ARMV6_CYCLE_COUNTER == counter)
  755. ret = pmcr & ARMV6_PMCR_CCOUNT_OVERFLOW;
  756. else if (ARMV6_COUNTER0 == counter)
  757. ret = pmcr & ARMV6_PMCR_COUNT0_OVERFLOW;
  758. else if (ARMV6_COUNTER1 == counter)
  759. ret = pmcr & ARMV6_PMCR_COUNT1_OVERFLOW;
  760. else
  761. WARN_ONCE(1, "invalid counter number (%d)\n", counter);
  762. return ret;
  763. }
  764. static inline u32
  765. armv6pmu_read_counter(int counter)
  766. {
  767. unsigned long value = 0;
  768. if (ARMV6_CYCLE_COUNTER == counter)
  769. asm volatile("mrc p15, 0, %0, c15, c12, 1" : "=r"(value));
  770. else if (ARMV6_COUNTER0 == counter)
  771. asm volatile("mrc p15, 0, %0, c15, c12, 2" : "=r"(value));
  772. else if (ARMV6_COUNTER1 == counter)
  773. asm volatile("mrc p15, 0, %0, c15, c12, 3" : "=r"(value));
  774. else
  775. WARN_ONCE(1, "invalid counter number (%d)\n", counter);
  776. return value;
  777. }
  778. static inline void
  779. armv6pmu_write_counter(int counter,
  780. u32 value)
  781. {
  782. if (ARMV6_CYCLE_COUNTER == counter)
  783. asm volatile("mcr p15, 0, %0, c15, c12, 1" : : "r"(value));
  784. else if (ARMV6_COUNTER0 == counter)
  785. asm volatile("mcr p15, 0, %0, c15, c12, 2" : : "r"(value));
  786. else if (ARMV6_COUNTER1 == counter)
  787. asm volatile("mcr p15, 0, %0, c15, c12, 3" : : "r"(value));
  788. else
  789. WARN_ONCE(1, "invalid counter number (%d)\n", counter);
  790. }
  791. void
  792. armv6pmu_enable_event(struct hw_perf_event *hwc,
  793. int idx)
  794. {
  795. unsigned long val, mask, evt, flags;
  796. if (ARMV6_CYCLE_COUNTER == idx) {
  797. mask = 0;
  798. evt = ARMV6_PMCR_CCOUNT_IEN;
  799. } else if (ARMV6_COUNTER0 == idx) {
  800. mask = ARMV6_PMCR_EVT_COUNT0_MASK;
  801. evt = (hwc->config_base << ARMV6_PMCR_EVT_COUNT0_SHIFT) |
  802. ARMV6_PMCR_COUNT0_IEN;
  803. } else if (ARMV6_COUNTER1 == idx) {
  804. mask = ARMV6_PMCR_EVT_COUNT1_MASK;
  805. evt = (hwc->config_base << ARMV6_PMCR_EVT_COUNT1_SHIFT) |
  806. ARMV6_PMCR_COUNT1_IEN;
  807. } else {
  808. WARN_ONCE(1, "invalid counter number (%d)\n", idx);
  809. return;
  810. }
  811. /*
  812. * Mask out the current event and set the counter to count the event
  813. * that we're interested in.
  814. */
  815. spin_lock_irqsave(&pmu_lock, flags);
  816. val = armv6_pmcr_read();
  817. val &= ~mask;
  818. val |= evt;
  819. armv6_pmcr_write(val);
  820. spin_unlock_irqrestore(&pmu_lock, flags);
  821. }
  822. static irqreturn_t
  823. armv6pmu_handle_irq(int irq_num,
  824. void *dev)
  825. {
  826. unsigned long pmcr = armv6_pmcr_read();
  827. struct perf_sample_data data;
  828. struct cpu_hw_events *cpuc;
  829. struct pt_regs *regs;
  830. int idx;
  831. if (!armv6_pmcr_has_overflowed(pmcr))
  832. return IRQ_NONE;
  833. regs = get_irq_regs();
  834. /*
  835. * The interrupts are cleared by writing the overflow flags back to
  836. * the control register. All of the other bits don't have any effect
  837. * if they are rewritten, so write the whole value back.
  838. */
  839. armv6_pmcr_write(pmcr);
  840. perf_sample_data_init(&data, 0);
  841. cpuc = &__get_cpu_var(cpu_hw_events);
  842. for (idx = 0; idx <= armpmu->num_events; ++idx) {
  843. struct perf_event *event = cpuc->events[idx];
  844. struct hw_perf_event *hwc;
  845. if (!test_bit(idx, cpuc->active_mask))
  846. continue;
  847. /*
  848. * We have a single interrupt for all counters. Check that
  849. * each counter has overflowed before we process it.
  850. */
  851. if (!armv6_pmcr_counter_has_overflowed(pmcr, idx))
  852. continue;
  853. hwc = &event->hw;
  854. armpmu_event_update(event, hwc, idx);
  855. data.period = event->hw.last_period;
  856. if (!armpmu_event_set_period(event, hwc, idx))
  857. continue;
  858. if (perf_event_overflow(event, 0, &data, regs))
  859. armpmu->disable(hwc, idx);
  860. }
  861. /*
  862. * Handle the pending perf events.
  863. *
  864. * Note: this call *must* be run with interrupts enabled. For
  865. * platforms that can have the PMU interrupts raised as a PMI, this
  866. * will not work.
  867. */
  868. perf_event_do_pending();
  869. return IRQ_HANDLED;
  870. }
  871. static void
  872. armv6pmu_start(void)
  873. {
  874. unsigned long flags, val;
  875. spin_lock_irqsave(&pmu_lock, flags);
  876. val = armv6_pmcr_read();
  877. val |= ARMV6_PMCR_ENABLE;
  878. armv6_pmcr_write(val);
  879. spin_unlock_irqrestore(&pmu_lock, flags);
  880. }
  881. void
  882. armv6pmu_stop(void)
  883. {
  884. unsigned long flags, val;
  885. spin_lock_irqsave(&pmu_lock, flags);
  886. val = armv6_pmcr_read();
  887. val &= ~ARMV6_PMCR_ENABLE;
  888. armv6_pmcr_write(val);
  889. spin_unlock_irqrestore(&pmu_lock, flags);
  890. }
  891. static inline int
  892. armv6pmu_event_map(int config)
  893. {
  894. int mapping = armv6_perf_map[config];
  895. if (HW_OP_UNSUPPORTED == mapping)
  896. mapping = -EOPNOTSUPP;
  897. return mapping;
  898. }
  899. static inline int
  900. armv6mpcore_pmu_event_map(int config)
  901. {
  902. int mapping = armv6mpcore_perf_map[config];
  903. if (HW_OP_UNSUPPORTED == mapping)
  904. mapping = -EOPNOTSUPP;
  905. return mapping;
  906. }
  907. static u64
  908. armv6pmu_raw_event(u64 config)
  909. {
  910. return config & 0xff;
  911. }
  912. static int
  913. armv6pmu_get_event_idx(struct cpu_hw_events *cpuc,
  914. struct hw_perf_event *event)
  915. {
  916. /* Always place a cycle counter into the cycle counter. */
  917. if (ARMV6_PERFCTR_CPU_CYCLES == event->config_base) {
  918. if (test_and_set_bit(ARMV6_CYCLE_COUNTER, cpuc->used_mask))
  919. return -EAGAIN;
  920. return ARMV6_CYCLE_COUNTER;
  921. } else {
  922. /*
  923. * For anything other than a cycle counter, try and use
  924. * counter0 and counter1.
  925. */
  926. if (!test_and_set_bit(ARMV6_COUNTER1, cpuc->used_mask)) {
  927. return ARMV6_COUNTER1;
  928. }
  929. if (!test_and_set_bit(ARMV6_COUNTER0, cpuc->used_mask)) {
  930. return ARMV6_COUNTER0;
  931. }
  932. /* The counters are all in use. */
  933. return -EAGAIN;
  934. }
  935. }
  936. static void
  937. armv6pmu_disable_event(struct hw_perf_event *hwc,
  938. int idx)
  939. {
  940. unsigned long val, mask, evt, flags;
  941. if (ARMV6_CYCLE_COUNTER == idx) {
  942. mask = ARMV6_PMCR_CCOUNT_IEN;
  943. evt = 0;
  944. } else if (ARMV6_COUNTER0 == idx) {
  945. mask = ARMV6_PMCR_COUNT0_IEN | ARMV6_PMCR_EVT_COUNT0_MASK;
  946. evt = ARMV6_PERFCTR_NOP << ARMV6_PMCR_EVT_COUNT0_SHIFT;
  947. } else if (ARMV6_COUNTER1 == idx) {
  948. mask = ARMV6_PMCR_COUNT1_IEN | ARMV6_PMCR_EVT_COUNT1_MASK;
  949. evt = ARMV6_PERFCTR_NOP << ARMV6_PMCR_EVT_COUNT1_SHIFT;
  950. } else {
  951. WARN_ONCE(1, "invalid counter number (%d)\n", idx);
  952. return;
  953. }
  954. /*
  955. * Mask out the current event and set the counter to count the number
  956. * of ETM bus signal assertion cycles. The external reporting should
  957. * be disabled and so this should never increment.
  958. */
  959. spin_lock_irqsave(&pmu_lock, flags);
  960. val = armv6_pmcr_read();
  961. val &= ~mask;
  962. val |= evt;
  963. armv6_pmcr_write(val);
  964. spin_unlock_irqrestore(&pmu_lock, flags);
  965. }
  966. static void
  967. armv6mpcore_pmu_disable_event(struct hw_perf_event *hwc,
  968. int idx)
  969. {
  970. unsigned long val, mask, flags, evt = 0;
  971. if (ARMV6_CYCLE_COUNTER == idx) {
  972. mask = ARMV6_PMCR_CCOUNT_IEN;
  973. } else if (ARMV6_COUNTER0 == idx) {
  974. mask = ARMV6_PMCR_COUNT0_IEN;
  975. } else if (ARMV6_COUNTER1 == idx) {
  976. mask = ARMV6_PMCR_COUNT1_IEN;
  977. } else {
  978. WARN_ONCE(1, "invalid counter number (%d)\n", idx);
  979. return;
  980. }
  981. /*
  982. * Unlike UP ARMv6, we don't have a way of stopping the counters. We
  983. * simply disable the interrupt reporting.
  984. */
  985. spin_lock_irqsave(&pmu_lock, flags);
  986. val = armv6_pmcr_read();
  987. val &= ~mask;
  988. val |= evt;
  989. armv6_pmcr_write(val);
  990. spin_unlock_irqrestore(&pmu_lock, flags);
  991. }
  992. static const struct arm_pmu armv6pmu = {
  993. .name = "v6",
  994. .handle_irq = armv6pmu_handle_irq,
  995. .enable = armv6pmu_enable_event,
  996. .disable = armv6pmu_disable_event,
  997. .event_map = armv6pmu_event_map,
  998. .raw_event = armv6pmu_raw_event,
  999. .read_counter = armv6pmu_read_counter,
  1000. .write_counter = armv6pmu_write_counter,
  1001. .get_event_idx = armv6pmu_get_event_idx,
  1002. .start = armv6pmu_start,
  1003. .stop = armv6pmu_stop,
  1004. .num_events = 3,
  1005. .max_period = (1LLU << 32) - 1,
  1006. };
  1007. /*
  1008. * ARMv6mpcore is almost identical to single core ARMv6 with the exception
  1009. * that some of the events have different enumerations and that there is no
  1010. * *hack* to stop the programmable counters. To stop the counters we simply
  1011. * disable the interrupt reporting and update the event. When unthrottling we
  1012. * reset the period and enable the interrupt reporting.
  1013. */
  1014. static const struct arm_pmu armv6mpcore_pmu = {
  1015. .name = "v6mpcore",
  1016. .handle_irq = armv6pmu_handle_irq,
  1017. .enable = armv6pmu_enable_event,
  1018. .disable = armv6mpcore_pmu_disable_event,
  1019. .event_map = armv6mpcore_pmu_event_map,
  1020. .raw_event = armv6pmu_raw_event,
  1021. .read_counter = armv6pmu_read_counter,
  1022. .write_counter = armv6pmu_write_counter,
  1023. .get_event_idx = armv6pmu_get_event_idx,
  1024. .start = armv6pmu_start,
  1025. .stop = armv6pmu_stop,
  1026. .num_events = 3,
  1027. .max_period = (1LLU << 32) - 1,
  1028. };
  1029. /*
  1030. * ARMv7 Cortex-A8 and Cortex-A9 Performance Events handling code.
  1031. *
  1032. * Copied from ARMv6 code, with the low level code inspired
  1033. * by the ARMv7 Oprofile code.
  1034. *
  1035. * Cortex-A8 has up to 4 configurable performance counters and
  1036. * a single cycle counter.
  1037. * Cortex-A9 has up to 31 configurable performance counters and
  1038. * a single cycle counter.
  1039. *
  1040. * All counters can be enabled/disabled and IRQ masked separately. The cycle
  1041. * counter and all 4 performance counters together can be reset separately.
  1042. */
  1043. #define ARMV7_PMU_CORTEX_A8_NAME "ARMv7 Cortex-A8"
  1044. #define ARMV7_PMU_CORTEX_A9_NAME "ARMv7 Cortex-A9"
  1045. /* Common ARMv7 event types */
  1046. enum armv7_perf_types {
  1047. ARMV7_PERFCTR_PMNC_SW_INCR = 0x00,
  1048. ARMV7_PERFCTR_IFETCH_MISS = 0x01,
  1049. ARMV7_PERFCTR_ITLB_MISS = 0x02,
  1050. ARMV7_PERFCTR_DCACHE_REFILL = 0x03,
  1051. ARMV7_PERFCTR_DCACHE_ACCESS = 0x04,
  1052. ARMV7_PERFCTR_DTLB_REFILL = 0x05,
  1053. ARMV7_PERFCTR_DREAD = 0x06,
  1054. ARMV7_PERFCTR_DWRITE = 0x07,
  1055. ARMV7_PERFCTR_EXC_TAKEN = 0x09,
  1056. ARMV7_PERFCTR_EXC_EXECUTED = 0x0A,
  1057. ARMV7_PERFCTR_CID_WRITE = 0x0B,
  1058. /* ARMV7_PERFCTR_PC_WRITE is equivalent to HW_BRANCH_INSTRUCTIONS.
  1059. * It counts:
  1060. * - all branch instructions,
  1061. * - instructions that explicitly write the PC,
  1062. * - exception generating instructions.
  1063. */
  1064. ARMV7_PERFCTR_PC_WRITE = 0x0C,
  1065. ARMV7_PERFCTR_PC_IMM_BRANCH = 0x0D,
  1066. ARMV7_PERFCTR_UNALIGNED_ACCESS = 0x0F,
  1067. ARMV7_PERFCTR_PC_BRANCH_MIS_PRED = 0x10,
  1068. ARMV7_PERFCTR_CLOCK_CYCLES = 0x11,
  1069. ARMV7_PERFCTR_PC_BRANCH_MIS_USED = 0x12,
  1070. ARMV7_PERFCTR_CPU_CYCLES = 0xFF
  1071. };
  1072. /* ARMv7 Cortex-A8 specific event types */
  1073. enum armv7_a8_perf_types {
  1074. ARMV7_PERFCTR_INSTR_EXECUTED = 0x08,
  1075. ARMV7_PERFCTR_PC_PROC_RETURN = 0x0E,
  1076. ARMV7_PERFCTR_WRITE_BUFFER_FULL = 0x40,
  1077. ARMV7_PERFCTR_L2_STORE_MERGED = 0x41,
  1078. ARMV7_PERFCTR_L2_STORE_BUFF = 0x42,
  1079. ARMV7_PERFCTR_L2_ACCESS = 0x43,
  1080. ARMV7_PERFCTR_L2_CACH_MISS = 0x44,
  1081. ARMV7_PERFCTR_AXI_READ_CYCLES = 0x45,
  1082. ARMV7_PERFCTR_AXI_WRITE_CYCLES = 0x46,
  1083. ARMV7_PERFCTR_MEMORY_REPLAY = 0x47,
  1084. ARMV7_PERFCTR_UNALIGNED_ACCESS_REPLAY = 0x48,
  1085. ARMV7_PERFCTR_L1_DATA_MISS = 0x49,
  1086. ARMV7_PERFCTR_L1_INST_MISS = 0x4A,
  1087. ARMV7_PERFCTR_L1_DATA_COLORING = 0x4B,
  1088. ARMV7_PERFCTR_L1_NEON_DATA = 0x4C,
  1089. ARMV7_PERFCTR_L1_NEON_CACH_DATA = 0x4D,
  1090. ARMV7_PERFCTR_L2_NEON = 0x4E,
  1091. ARMV7_PERFCTR_L2_NEON_HIT = 0x4F,
  1092. ARMV7_PERFCTR_L1_INST = 0x50,
  1093. ARMV7_PERFCTR_PC_RETURN_MIS_PRED = 0x51,
  1094. ARMV7_PERFCTR_PC_BRANCH_FAILED = 0x52,
  1095. ARMV7_PERFCTR_PC_BRANCH_TAKEN = 0x53,
  1096. ARMV7_PERFCTR_PC_BRANCH_EXECUTED = 0x54,
  1097. ARMV7_PERFCTR_OP_EXECUTED = 0x55,
  1098. ARMV7_PERFCTR_CYCLES_INST_STALL = 0x56,
  1099. ARMV7_PERFCTR_CYCLES_INST = 0x57,
  1100. ARMV7_PERFCTR_CYCLES_NEON_DATA_STALL = 0x58,
  1101. ARMV7_PERFCTR_CYCLES_NEON_INST_STALL = 0x59,
  1102. ARMV7_PERFCTR_NEON_CYCLES = 0x5A,
  1103. ARMV7_PERFCTR_PMU0_EVENTS = 0x70,
  1104. ARMV7_PERFCTR_PMU1_EVENTS = 0x71,
  1105. ARMV7_PERFCTR_PMU_EVENTS = 0x72,
  1106. };
  1107. /* ARMv7 Cortex-A9 specific event types */
  1108. enum armv7_a9_perf_types {
  1109. ARMV7_PERFCTR_JAVA_HW_BYTECODE_EXEC = 0x40,
  1110. ARMV7_PERFCTR_JAVA_SW_BYTECODE_EXEC = 0x41,
  1111. ARMV7_PERFCTR_JAZELLE_BRANCH_EXEC = 0x42,
  1112. ARMV7_PERFCTR_COHERENT_LINE_MISS = 0x50,
  1113. ARMV7_PERFCTR_COHERENT_LINE_HIT = 0x51,
  1114. ARMV7_PERFCTR_ICACHE_DEP_STALL_CYCLES = 0x60,
  1115. ARMV7_PERFCTR_DCACHE_DEP_STALL_CYCLES = 0x61,
  1116. ARMV7_PERFCTR_TLB_MISS_DEP_STALL_CYCLES = 0x62,
  1117. ARMV7_PERFCTR_STREX_EXECUTED_PASSED = 0x63,
  1118. ARMV7_PERFCTR_STREX_EXECUTED_FAILED = 0x64,
  1119. ARMV7_PERFCTR_DATA_EVICTION = 0x65,
  1120. ARMV7_PERFCTR_ISSUE_STAGE_NO_INST = 0x66,
  1121. ARMV7_PERFCTR_ISSUE_STAGE_EMPTY = 0x67,
  1122. ARMV7_PERFCTR_INST_OUT_OF_RENAME_STAGE = 0x68,
  1123. ARMV7_PERFCTR_PREDICTABLE_FUNCT_RETURNS = 0x6E,
  1124. ARMV7_PERFCTR_MAIN_UNIT_EXECUTED_INST = 0x70,
  1125. ARMV7_PERFCTR_SECOND_UNIT_EXECUTED_INST = 0x71,
  1126. ARMV7_PERFCTR_LD_ST_UNIT_EXECUTED_INST = 0x72,
  1127. ARMV7_PERFCTR_FP_EXECUTED_INST = 0x73,
  1128. ARMV7_PERFCTR_NEON_EXECUTED_INST = 0x74,
  1129. ARMV7_PERFCTR_PLD_FULL_DEP_STALL_CYCLES = 0x80,
  1130. ARMV7_PERFCTR_DATA_WR_DEP_STALL_CYCLES = 0x81,
  1131. ARMV7_PERFCTR_ITLB_MISS_DEP_STALL_CYCLES = 0x82,
  1132. ARMV7_PERFCTR_DTLB_MISS_DEP_STALL_CYCLES = 0x83,
  1133. ARMV7_PERFCTR_MICRO_ITLB_MISS_DEP_STALL_CYCLES = 0x84,
  1134. ARMV7_PERFCTR_MICRO_DTLB_MISS_DEP_STALL_CYCLES = 0x85,
  1135. ARMV7_PERFCTR_DMB_DEP_STALL_CYCLES = 0x86,
  1136. ARMV7_PERFCTR_INTGR_CLK_ENABLED_CYCLES = 0x8A,
  1137. ARMV7_PERFCTR_DATA_ENGINE_CLK_EN_CYCLES = 0x8B,
  1138. ARMV7_PERFCTR_ISB_INST = 0x90,
  1139. ARMV7_PERFCTR_DSB_INST = 0x91,
  1140. ARMV7_PERFCTR_DMB_INST = 0x92,
  1141. ARMV7_PERFCTR_EXT_INTERRUPTS = 0x93,
  1142. ARMV7_PERFCTR_PLE_CACHE_LINE_RQST_COMPLETED = 0xA0,
  1143. ARMV7_PERFCTR_PLE_CACHE_LINE_RQST_SKIPPED = 0xA1,
  1144. ARMV7_PERFCTR_PLE_FIFO_FLUSH = 0xA2,
  1145. ARMV7_PERFCTR_PLE_RQST_COMPLETED = 0xA3,
  1146. ARMV7_PERFCTR_PLE_FIFO_OVERFLOW = 0xA4,
  1147. ARMV7_PERFCTR_PLE_RQST_PROG = 0xA5
  1148. };
  1149. /*
  1150. * Cortex-A8 HW events mapping
  1151. *
  1152. * The hardware events that we support. We do support cache operations but
  1153. * we have harvard caches and no way to combine instruction and data
  1154. * accesses/misses in hardware.
  1155. */
  1156. static const unsigned armv7_a8_perf_map[PERF_COUNT_HW_MAX] = {
  1157. [PERF_COUNT_HW_CPU_CYCLES] = ARMV7_PERFCTR_CPU_CYCLES,
  1158. [PERF_COUNT_HW_INSTRUCTIONS] = ARMV7_PERFCTR_INSTR_EXECUTED,
  1159. [PERF_COUNT_HW_CACHE_REFERENCES] = HW_OP_UNSUPPORTED,
  1160. [PERF_COUNT_HW_CACHE_MISSES] = HW_OP_UNSUPPORTED,
  1161. [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = ARMV7_PERFCTR_PC_WRITE,
  1162. [PERF_COUNT_HW_BRANCH_MISSES] = ARMV7_PERFCTR_PC_BRANCH_MIS_PRED,
  1163. [PERF_COUNT_HW_BUS_CYCLES] = ARMV7_PERFCTR_CLOCK_CYCLES,
  1164. };
  1165. static const unsigned armv7_a8_perf_cache_map[PERF_COUNT_HW_CACHE_MAX]
  1166. [PERF_COUNT_HW_CACHE_OP_MAX]
  1167. [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
  1168. [C(L1D)] = {
  1169. /*
  1170. * The performance counters don't differentiate between read
  1171. * and write accesses/misses so this isn't strictly correct,
  1172. * but it's the best we can do. Writes and reads get
  1173. * combined.
  1174. */
  1175. [C(OP_READ)] = {
  1176. [C(RESULT_ACCESS)] = ARMV7_PERFCTR_DCACHE_ACCESS,
  1177. [C(RESULT_MISS)] = ARMV7_PERFCTR_DCACHE_REFILL,
  1178. },
  1179. [C(OP_WRITE)] = {
  1180. [C(RESULT_ACCESS)] = ARMV7_PERFCTR_DCACHE_ACCESS,
  1181. [C(RESULT_MISS)] = ARMV7_PERFCTR_DCACHE_REFILL,
  1182. },
  1183. [C(OP_PREFETCH)] = {
  1184. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  1185. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  1186. },
  1187. },
  1188. [C(L1I)] = {
  1189. [C(OP_READ)] = {
  1190. [C(RESULT_ACCESS)] = ARMV7_PERFCTR_L1_INST,
  1191. [C(RESULT_MISS)] = ARMV7_PERFCTR_L1_INST_MISS,
  1192. },
  1193. [C(OP_WRITE)] = {
  1194. [C(RESULT_ACCESS)] = ARMV7_PERFCTR_L1_INST,
  1195. [C(RESULT_MISS)] = ARMV7_PERFCTR_L1_INST_MISS,
  1196. },
  1197. [C(OP_PREFETCH)] = {
  1198. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  1199. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  1200. },
  1201. },
  1202. [C(LL)] = {
  1203. [C(OP_READ)] = {
  1204. [C(RESULT_ACCESS)] = ARMV7_PERFCTR_L2_ACCESS,
  1205. [C(RESULT_MISS)] = ARMV7_PERFCTR_L2_CACH_MISS,
  1206. },
  1207. [C(OP_WRITE)] = {
  1208. [C(RESULT_ACCESS)] = ARMV7_PERFCTR_L2_ACCESS,
  1209. [C(RESULT_MISS)] = ARMV7_PERFCTR_L2_CACH_MISS,
  1210. },
  1211. [C(OP_PREFETCH)] = {
  1212. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  1213. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  1214. },
  1215. },
  1216. [C(DTLB)] = {
  1217. /*
  1218. * Only ITLB misses and DTLB refills are supported.
  1219. * If users want the DTLB refills misses a raw counter
  1220. * must be used.
  1221. */
  1222. [C(OP_READ)] = {
  1223. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  1224. [C(RESULT_MISS)] = ARMV7_PERFCTR_DTLB_REFILL,
  1225. },
  1226. [C(OP_WRITE)] = {
  1227. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  1228. [C(RESULT_MISS)] = ARMV7_PERFCTR_DTLB_REFILL,
  1229. },
  1230. [C(OP_PREFETCH)] = {
  1231. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  1232. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  1233. },
  1234. },
  1235. [C(ITLB)] = {
  1236. [C(OP_READ)] = {
  1237. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  1238. [C(RESULT_MISS)] = ARMV7_PERFCTR_ITLB_MISS,
  1239. },
  1240. [C(OP_WRITE)] = {
  1241. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  1242. [C(RESULT_MISS)] = ARMV7_PERFCTR_ITLB_MISS,
  1243. },
  1244. [C(OP_PREFETCH)] = {
  1245. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  1246. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  1247. },
  1248. },
  1249. [C(BPU)] = {
  1250. [C(OP_READ)] = {
  1251. [C(RESULT_ACCESS)] = ARMV7_PERFCTR_PC_WRITE,
  1252. [C(RESULT_MISS)]
  1253. = ARMV7_PERFCTR_PC_BRANCH_MIS_PRED,
  1254. },
  1255. [C(OP_WRITE)] = {
  1256. [C(RESULT_ACCESS)] = ARMV7_PERFCTR_PC_WRITE,
  1257. [C(RESULT_MISS)]
  1258. = ARMV7_PERFCTR_PC_BRANCH_MIS_PRED,
  1259. },
  1260. [C(OP_PREFETCH)] = {
  1261. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  1262. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  1263. },
  1264. },
  1265. };
  1266. /*
  1267. * Cortex-A9 HW events mapping
  1268. */
  1269. static const unsigned armv7_a9_perf_map[PERF_COUNT_HW_MAX] = {
  1270. [PERF_COUNT_HW_CPU_CYCLES] = ARMV7_PERFCTR_CPU_CYCLES,
  1271. [PERF_COUNT_HW_INSTRUCTIONS] =
  1272. ARMV7_PERFCTR_INST_OUT_OF_RENAME_STAGE,
  1273. [PERF_COUNT_HW_CACHE_REFERENCES] = ARMV7_PERFCTR_COHERENT_LINE_HIT,
  1274. [PERF_COUNT_HW_CACHE_MISSES] = ARMV7_PERFCTR_COHERENT_LINE_MISS,
  1275. [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = ARMV7_PERFCTR_PC_WRITE,
  1276. [PERF_COUNT_HW_BRANCH_MISSES] = ARMV7_PERFCTR_PC_BRANCH_MIS_PRED,
  1277. [PERF_COUNT_HW_BUS_CYCLES] = ARMV7_PERFCTR_CLOCK_CYCLES,
  1278. };
  1279. static const unsigned armv7_a9_perf_cache_map[PERF_COUNT_HW_CACHE_MAX]
  1280. [PERF_COUNT_HW_CACHE_OP_MAX]
  1281. [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
  1282. [C(L1D)] = {
  1283. /*
  1284. * The performance counters don't differentiate between read
  1285. * and write accesses/misses so this isn't strictly correct,
  1286. * but it's the best we can do. Writes and reads get
  1287. * combined.
  1288. */
  1289. [C(OP_READ)] = {
  1290. [C(RESULT_ACCESS)] = ARMV7_PERFCTR_DCACHE_ACCESS,
  1291. [C(RESULT_MISS)] = ARMV7_PERFCTR_DCACHE_REFILL,
  1292. },
  1293. [C(OP_WRITE)] = {
  1294. [C(RESULT_ACCESS)] = ARMV7_PERFCTR_DCACHE_ACCESS,
  1295. [C(RESULT_MISS)] = ARMV7_PERFCTR_DCACHE_REFILL,
  1296. },
  1297. [C(OP_PREFETCH)] = {
  1298. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  1299. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  1300. },
  1301. },
  1302. [C(L1I)] = {
  1303. [C(OP_READ)] = {
  1304. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  1305. [C(RESULT_MISS)] = ARMV7_PERFCTR_IFETCH_MISS,
  1306. },
  1307. [C(OP_WRITE)] = {
  1308. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  1309. [C(RESULT_MISS)] = ARMV7_PERFCTR_IFETCH_MISS,
  1310. },
  1311. [C(OP_PREFETCH)] = {
  1312. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  1313. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  1314. },
  1315. },
  1316. [C(LL)] = {
  1317. [C(OP_READ)] = {
  1318. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  1319. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  1320. },
  1321. [C(OP_WRITE)] = {
  1322. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  1323. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  1324. },
  1325. [C(OP_PREFETCH)] = {
  1326. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  1327. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  1328. },
  1329. },
  1330. [C(DTLB)] = {
  1331. /*
  1332. * Only ITLB misses and DTLB refills are supported.
  1333. * If users want the DTLB refills misses a raw counter
  1334. * must be used.
  1335. */
  1336. [C(OP_READ)] = {
  1337. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  1338. [C(RESULT_MISS)] = ARMV7_PERFCTR_DTLB_REFILL,
  1339. },
  1340. [C(OP_WRITE)] = {
  1341. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  1342. [C(RESULT_MISS)] = ARMV7_PERFCTR_DTLB_REFILL,
  1343. },
  1344. [C(OP_PREFETCH)] = {
  1345. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  1346. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  1347. },
  1348. },
  1349. [C(ITLB)] = {
  1350. [C(OP_READ)] = {
  1351. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  1352. [C(RESULT_MISS)] = ARMV7_PERFCTR_ITLB_MISS,
  1353. },
  1354. [C(OP_WRITE)] = {
  1355. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  1356. [C(RESULT_MISS)] = ARMV7_PERFCTR_ITLB_MISS,
  1357. },
  1358. [C(OP_PREFETCH)] = {
  1359. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  1360. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  1361. },
  1362. },
  1363. [C(BPU)] = {
  1364. [C(OP_READ)] = {
  1365. [C(RESULT_ACCESS)] = ARMV7_PERFCTR_PC_WRITE,
  1366. [C(RESULT_MISS)]
  1367. = ARMV7_PERFCTR_PC_BRANCH_MIS_PRED,
  1368. },
  1369. [C(OP_WRITE)] = {
  1370. [C(RESULT_ACCESS)] = ARMV7_PERFCTR_PC_WRITE,
  1371. [C(RESULT_MISS)]
  1372. = ARMV7_PERFCTR_PC_BRANCH_MIS_PRED,
  1373. },
  1374. [C(OP_PREFETCH)] = {
  1375. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  1376. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  1377. },
  1378. },
  1379. };
  1380. /*
  1381. * Perf Events counters
  1382. */
  1383. enum armv7_counters {
  1384. ARMV7_CYCLE_COUNTER = 1, /* Cycle counter */
  1385. ARMV7_COUNTER0 = 2, /* First event counter */
  1386. };
  1387. /*
  1388. * The cycle counter is ARMV7_CYCLE_COUNTER.
  1389. * The first event counter is ARMV7_COUNTER0.
  1390. * The last event counter is (ARMV7_COUNTER0 + armpmu->num_events - 1).
  1391. */
  1392. #define ARMV7_COUNTER_LAST (ARMV7_COUNTER0 + armpmu->num_events - 1)
  1393. /*
  1394. * ARMv7 low level PMNC access
  1395. */
  1396. /*
  1397. * Per-CPU PMNC: config reg
  1398. */
  1399. #define ARMV7_PMNC_E (1 << 0) /* Enable all counters */
  1400. #define ARMV7_PMNC_P (1 << 1) /* Reset all counters */
  1401. #define ARMV7_PMNC_C (1 << 2) /* Cycle counter reset */
  1402. #define ARMV7_PMNC_D (1 << 3) /* CCNT counts every 64th cpu cycle */
  1403. #define ARMV7_PMNC_X (1 << 4) /* Export to ETM */
  1404. #define ARMV7_PMNC_DP (1 << 5) /* Disable CCNT if non-invasive debug*/
  1405. #define ARMV7_PMNC_N_SHIFT 11 /* Number of counters supported */
  1406. #define ARMV7_PMNC_N_MASK 0x1f
  1407. #define ARMV7_PMNC_MASK 0x3f /* Mask for writable bits */
  1408. /*
  1409. * Available counters
  1410. */
  1411. #define ARMV7_CNT0 0 /* First event counter */
  1412. #define ARMV7_CCNT 31 /* Cycle counter */
  1413. /* Perf Event to low level counters mapping */
  1414. #define ARMV7_EVENT_CNT_TO_CNTx (ARMV7_COUNTER0 - ARMV7_CNT0)
  1415. /*
  1416. * CNTENS: counters enable reg
  1417. */
  1418. #define ARMV7_CNTENS_P(idx) (1 << (idx - ARMV7_EVENT_CNT_TO_CNTx))
  1419. #define ARMV7_CNTENS_C (1 << ARMV7_CCNT)
  1420. /*
  1421. * CNTENC: counters disable reg
  1422. */
  1423. #define ARMV7_CNTENC_P(idx) (1 << (idx - ARMV7_EVENT_CNT_TO_CNTx))
  1424. #define ARMV7_CNTENC_C (1 << ARMV7_CCNT)
  1425. /*
  1426. * INTENS: counters overflow interrupt enable reg
  1427. */
  1428. #define ARMV7_INTENS_P(idx) (1 << (idx - ARMV7_EVENT_CNT_TO_CNTx))
  1429. #define ARMV7_INTENS_C (1 << ARMV7_CCNT)
  1430. /*
  1431. * INTENC: counters overflow interrupt disable reg
  1432. */
  1433. #define ARMV7_INTENC_P(idx) (1 << (idx - ARMV7_EVENT_CNT_TO_CNTx))
  1434. #define ARMV7_INTENC_C (1 << ARMV7_CCNT)
  1435. /*
  1436. * EVTSEL: Event selection reg
  1437. */
  1438. #define ARMV7_EVTSEL_MASK 0xff /* Mask for writable bits */
  1439. /*
  1440. * SELECT: Counter selection reg
  1441. */
  1442. #define ARMV7_SELECT_MASK 0x1f /* Mask for writable bits */
  1443. /*
  1444. * FLAG: counters overflow flag status reg
  1445. */
  1446. #define ARMV7_FLAG_P(idx) (1 << (idx - ARMV7_EVENT_CNT_TO_CNTx))
  1447. #define ARMV7_FLAG_C (1 << ARMV7_CCNT)
  1448. #define ARMV7_FLAG_MASK 0xffffffff /* Mask for writable bits */
  1449. #define ARMV7_OVERFLOWED_MASK ARMV7_FLAG_MASK
  1450. static inline unsigned long armv7_pmnc_read(void)
  1451. {
  1452. u32 val;
  1453. asm volatile("mrc p15, 0, %0, c9, c12, 0" : "=r"(val));
  1454. return val;
  1455. }
  1456. static inline void armv7_pmnc_write(unsigned long val)
  1457. {
  1458. val &= ARMV7_PMNC_MASK;
  1459. asm volatile("mcr p15, 0, %0, c9, c12, 0" : : "r"(val));
  1460. }
  1461. static inline int armv7_pmnc_has_overflowed(unsigned long pmnc)
  1462. {
  1463. return pmnc & ARMV7_OVERFLOWED_MASK;
  1464. }
  1465. static inline int armv7_pmnc_counter_has_overflowed(unsigned long pmnc,
  1466. enum armv7_counters counter)
  1467. {
  1468. int ret;
  1469. if (counter == ARMV7_CYCLE_COUNTER)
  1470. ret = pmnc & ARMV7_FLAG_C;
  1471. else if ((counter >= ARMV7_COUNTER0) && (counter <= ARMV7_COUNTER_LAST))
  1472. ret = pmnc & ARMV7_FLAG_P(counter);
  1473. else
  1474. pr_err("CPU%u checking wrong counter %d overflow status\n",
  1475. smp_processor_id(), counter);
  1476. return ret;
  1477. }
  1478. static inline int armv7_pmnc_select_counter(unsigned int idx)
  1479. {
  1480. u32 val;
  1481. if ((idx < ARMV7_COUNTER0) || (idx > ARMV7_COUNTER_LAST)) {
  1482. pr_err("CPU%u selecting wrong PMNC counter"
  1483. " %d\n", smp_processor_id(), idx);
  1484. return -1;
  1485. }
  1486. val = (idx - ARMV7_EVENT_CNT_TO_CNTx) & ARMV7_SELECT_MASK;
  1487. asm volatile("mcr p15, 0, %0, c9, c12, 5" : : "r" (val));
  1488. return idx;
  1489. }
  1490. static inline u32 armv7pmu_read_counter(int idx)
  1491. {
  1492. unsigned long value = 0;
  1493. if (idx == ARMV7_CYCLE_COUNTER)
  1494. asm volatile("mrc p15, 0, %0, c9, c13, 0" : "=r" (value));
  1495. else if ((idx >= ARMV7_COUNTER0) && (idx <= ARMV7_COUNTER_LAST)) {
  1496. if (armv7_pmnc_select_counter(idx) == idx)
  1497. asm volatile("mrc p15, 0, %0, c9, c13, 2"
  1498. : "=r" (value));
  1499. } else
  1500. pr_err("CPU%u reading wrong counter %d\n",
  1501. smp_processor_id(), idx);
  1502. return value;
  1503. }
  1504. static inline void armv7pmu_write_counter(int idx, u32 value)
  1505. {
  1506. if (idx == ARMV7_CYCLE_COUNTER)
  1507. asm volatile("mcr p15, 0, %0, c9, c13, 0" : : "r" (value));
  1508. else if ((idx >= ARMV7_COUNTER0) && (idx <= ARMV7_COUNTER_LAST)) {
  1509. if (armv7_pmnc_select_counter(idx) == idx)
  1510. asm volatile("mcr p15, 0, %0, c9, c13, 2"
  1511. : : "r" (value));
  1512. } else
  1513. pr_err("CPU%u writing wrong counter %d\n",
  1514. smp_processor_id(), idx);
  1515. }
  1516. static inline void armv7_pmnc_write_evtsel(unsigned int idx, u32 val)
  1517. {
  1518. if (armv7_pmnc_select_counter(idx) == idx) {
  1519. val &= ARMV7_EVTSEL_MASK;
  1520. asm volatile("mcr p15, 0, %0, c9, c13, 1" : : "r" (val));
  1521. }
  1522. }
  1523. static inline u32 armv7_pmnc_enable_counter(unsigned int idx)
  1524. {
  1525. u32 val;
  1526. if ((idx != ARMV7_CYCLE_COUNTER) &&
  1527. ((idx < ARMV7_COUNTER0) || (idx > ARMV7_COUNTER_LAST))) {
  1528. pr_err("CPU%u enabling wrong PMNC counter"
  1529. " %d\n", smp_processor_id(), idx);
  1530. return -1;
  1531. }
  1532. if (idx == ARMV7_CYCLE_COUNTER)
  1533. val = ARMV7_CNTENS_C;
  1534. else
  1535. val = ARMV7_CNTENS_P(idx);
  1536. asm volatile("mcr p15, 0, %0, c9, c12, 1" : : "r" (val));
  1537. return idx;
  1538. }
  1539. static inline u32 armv7_pmnc_disable_counter(unsigned int idx)
  1540. {
  1541. u32 val;
  1542. if ((idx != ARMV7_CYCLE_COUNTER) &&
  1543. ((idx < ARMV7_COUNTER0) || (idx > ARMV7_COUNTER_LAST))) {
  1544. pr_err("CPU%u disabling wrong PMNC counter"
  1545. " %d\n", smp_processor_id(), idx);
  1546. return -1;
  1547. }
  1548. if (idx == ARMV7_CYCLE_COUNTER)
  1549. val = ARMV7_CNTENC_C;
  1550. else
  1551. val = ARMV7_CNTENC_P(idx);
  1552. asm volatile("mcr p15, 0, %0, c9, c12, 2" : : "r" (val));
  1553. return idx;
  1554. }
  1555. static inline u32 armv7_pmnc_enable_intens(unsigned int idx)
  1556. {
  1557. u32 val;
  1558. if ((idx != ARMV7_CYCLE_COUNTER) &&
  1559. ((idx < ARMV7_COUNTER0) || (idx > ARMV7_COUNTER_LAST))) {
  1560. pr_err("CPU%u enabling wrong PMNC counter"
  1561. " interrupt enable %d\n", smp_processor_id(), idx);
  1562. return -1;
  1563. }
  1564. if (idx == ARMV7_CYCLE_COUNTER)
  1565. val = ARMV7_INTENS_C;
  1566. else
  1567. val = ARMV7_INTENS_P(idx);
  1568. asm volatile("mcr p15, 0, %0, c9, c14, 1" : : "r" (val));
  1569. return idx;
  1570. }
  1571. static inline u32 armv7_pmnc_disable_intens(unsigned int idx)
  1572. {
  1573. u32 val;
  1574. if ((idx != ARMV7_CYCLE_COUNTER) &&
  1575. ((idx < ARMV7_COUNTER0) || (idx > ARMV7_COUNTER_LAST))) {
  1576. pr_err("CPU%u disabling wrong PMNC counter"
  1577. " interrupt enable %d\n", smp_processor_id(), idx);
  1578. return -1;
  1579. }
  1580. if (idx == ARMV7_CYCLE_COUNTER)
  1581. val = ARMV7_INTENC_C;
  1582. else
  1583. val = ARMV7_INTENC_P(idx);
  1584. asm volatile("mcr p15, 0, %0, c9, c14, 2" : : "r" (val));
  1585. return idx;
  1586. }
  1587. static inline u32 armv7_pmnc_getreset_flags(void)
  1588. {
  1589. u32 val;
  1590. /* Read */
  1591. asm volatile("mrc p15, 0, %0, c9, c12, 3" : "=r" (val));
  1592. /* Write to clear flags */
  1593. val &= ARMV7_FLAG_MASK;
  1594. asm volatile("mcr p15, 0, %0, c9, c12, 3" : : "r" (val));
  1595. return val;
  1596. }
  1597. #ifdef DEBUG
  1598. static void armv7_pmnc_dump_regs(void)
  1599. {
  1600. u32 val;
  1601. unsigned int cnt;
  1602. printk(KERN_INFO "PMNC registers dump:\n");
  1603. asm volatile("mrc p15, 0, %0, c9, c12, 0" : "=r" (val));
  1604. printk(KERN_INFO "PMNC =0x%08x\n", val);
  1605. asm volatile("mrc p15, 0, %0, c9, c12, 1" : "=r" (val));
  1606. printk(KERN_INFO "CNTENS=0x%08x\n", val);
  1607. asm volatile("mrc p15, 0, %0, c9, c14, 1" : "=r" (val));
  1608. printk(KERN_INFO "INTENS=0x%08x\n", val);
  1609. asm volatile("mrc p15, 0, %0, c9, c12, 3" : "=r" (val));
  1610. printk(KERN_INFO "FLAGS =0x%08x\n", val);
  1611. asm volatile("mrc p15, 0, %0, c9, c12, 5" : "=r" (val));
  1612. printk(KERN_INFO "SELECT=0x%08x\n", val);
  1613. asm volatile("mrc p15, 0, %0, c9, c13, 0" : "=r" (val));
  1614. printk(KERN_INFO "CCNT =0x%08x\n", val);
  1615. for (cnt = ARMV7_COUNTER0; cnt < ARMV7_COUNTER_LAST; cnt++) {
  1616. armv7_pmnc_select_counter(cnt);
  1617. asm volatile("mrc p15, 0, %0, c9, c13, 2" : "=r" (val));
  1618. printk(KERN_INFO "CNT[%d] count =0x%08x\n",
  1619. cnt-ARMV7_EVENT_CNT_TO_CNTx, val);
  1620. asm volatile("mrc p15, 0, %0, c9, c13, 1" : "=r" (val));
  1621. printk(KERN_INFO "CNT[%d] evtsel=0x%08x\n",
  1622. cnt-ARMV7_EVENT_CNT_TO_CNTx, val);
  1623. }
  1624. }
  1625. #endif
  1626. void armv7pmu_enable_event(struct hw_perf_event *hwc, int idx)
  1627. {
  1628. unsigned long flags;
  1629. /*
  1630. * Enable counter and interrupt, and set the counter to count
  1631. * the event that we're interested in.
  1632. */
  1633. spin_lock_irqsave(&pmu_lock, flags);
  1634. /*
  1635. * Disable counter
  1636. */
  1637. armv7_pmnc_disable_counter(idx);
  1638. /*
  1639. * Set event (if destined for PMNx counters)
  1640. * We don't need to set the event if it's a cycle count
  1641. */
  1642. if (idx != ARMV7_CYCLE_COUNTER)
  1643. armv7_pmnc_write_evtsel(idx, hwc->config_base);
  1644. /*
  1645. * Enable interrupt for this counter
  1646. */
  1647. armv7_pmnc_enable_intens(idx);
  1648. /*
  1649. * Enable counter
  1650. */
  1651. armv7_pmnc_enable_counter(idx);
  1652. spin_unlock_irqrestore(&pmu_lock, flags);
  1653. }
  1654. static void armv7pmu_disable_event(struct hw_perf_event *hwc, int idx)
  1655. {
  1656. unsigned long flags;
  1657. /*
  1658. * Disable counter and interrupt
  1659. */
  1660. spin_lock_irqsave(&pmu_lock, flags);
  1661. /*
  1662. * Disable counter
  1663. */
  1664. armv7_pmnc_disable_counter(idx);
  1665. /*
  1666. * Disable interrupt for this counter
  1667. */
  1668. armv7_pmnc_disable_intens(idx);
  1669. spin_unlock_irqrestore(&pmu_lock, flags);
  1670. }
  1671. static irqreturn_t armv7pmu_handle_irq(int irq_num, void *dev)
  1672. {
  1673. unsigned long pmnc;
  1674. struct perf_sample_data data;
  1675. struct cpu_hw_events *cpuc;
  1676. struct pt_regs *regs;
  1677. int idx;
  1678. /*
  1679. * Get and reset the IRQ flags
  1680. */
  1681. pmnc = armv7_pmnc_getreset_flags();
  1682. /*
  1683. * Did an overflow occur?
  1684. */
  1685. if (!armv7_pmnc_has_overflowed(pmnc))
  1686. return IRQ_NONE;
  1687. /*
  1688. * Handle the counter(s) overflow(s)
  1689. */
  1690. regs = get_irq_regs();
  1691. perf_sample_data_init(&data, 0);
  1692. cpuc = &__get_cpu_var(cpu_hw_events);
  1693. for (idx = 0; idx <= armpmu->num_events; ++idx) {
  1694. struct perf_event *event = cpuc->events[idx];
  1695. struct hw_perf_event *hwc;
  1696. if (!test_bit(idx, cpuc->active_mask))
  1697. continue;
  1698. /*
  1699. * We have a single interrupt for all counters. Check that
  1700. * each counter has overflowed before we process it.
  1701. */
  1702. if (!armv7_pmnc_counter_has_overflowed(pmnc, idx))
  1703. continue;
  1704. hwc = &event->hw;
  1705. armpmu_event_update(event, hwc, idx);
  1706. data.period = event->hw.last_period;
  1707. if (!armpmu_event_set_period(event, hwc, idx))
  1708. continue;
  1709. if (perf_event_overflow(event, 0, &data, regs))
  1710. armpmu->disable(hwc, idx);
  1711. }
  1712. /*
  1713. * Handle the pending perf events.
  1714. *
  1715. * Note: this call *must* be run with interrupts enabled. For
  1716. * platforms that can have the PMU interrupts raised as a PMI, this
  1717. * will not work.
  1718. */
  1719. perf_event_do_pending();
  1720. return IRQ_HANDLED;
  1721. }
  1722. static void armv7pmu_start(void)
  1723. {
  1724. unsigned long flags;
  1725. spin_lock_irqsave(&pmu_lock, flags);
  1726. /* Enable all counters */
  1727. armv7_pmnc_write(armv7_pmnc_read() | ARMV7_PMNC_E);
  1728. spin_unlock_irqrestore(&pmu_lock, flags);
  1729. }
  1730. static void armv7pmu_stop(void)
  1731. {
  1732. unsigned long flags;
  1733. spin_lock_irqsave(&pmu_lock, flags);
  1734. /* Disable all counters */
  1735. armv7_pmnc_write(armv7_pmnc_read() & ~ARMV7_PMNC_E);
  1736. spin_unlock_irqrestore(&pmu_lock, flags);
  1737. }
  1738. static inline int armv7_a8_pmu_event_map(int config)
  1739. {
  1740. int mapping = armv7_a8_perf_map[config];
  1741. if (HW_OP_UNSUPPORTED == mapping)
  1742. mapping = -EOPNOTSUPP;
  1743. return mapping;
  1744. }
  1745. static inline int armv7_a9_pmu_event_map(int config)
  1746. {
  1747. int mapping = armv7_a9_perf_map[config];
  1748. if (HW_OP_UNSUPPORTED == mapping)
  1749. mapping = -EOPNOTSUPP;
  1750. return mapping;
  1751. }
  1752. static u64 armv7pmu_raw_event(u64 config)
  1753. {
  1754. return config & 0xff;
  1755. }
  1756. static int armv7pmu_get_event_idx(struct cpu_hw_events *cpuc,
  1757. struct hw_perf_event *event)
  1758. {
  1759. int idx;
  1760. /* Always place a cycle counter into the cycle counter. */
  1761. if (event->config_base == ARMV7_PERFCTR_CPU_CYCLES) {
  1762. if (test_and_set_bit(ARMV7_CYCLE_COUNTER, cpuc->used_mask))
  1763. return -EAGAIN;
  1764. return ARMV7_CYCLE_COUNTER;
  1765. } else {
  1766. /*
  1767. * For anything other than a cycle counter, try and use
  1768. * the events counters
  1769. */
  1770. for (idx = ARMV7_COUNTER0; idx <= armpmu->num_events; ++idx) {
  1771. if (!test_and_set_bit(idx, cpuc->used_mask))
  1772. return idx;
  1773. }
  1774. /* The counters are all in use. */
  1775. return -EAGAIN;
  1776. }
  1777. }
  1778. static struct arm_pmu armv7pmu = {
  1779. .handle_irq = armv7pmu_handle_irq,
  1780. .enable = armv7pmu_enable_event,
  1781. .disable = armv7pmu_disable_event,
  1782. .raw_event = armv7pmu_raw_event,
  1783. .read_counter = armv7pmu_read_counter,
  1784. .write_counter = armv7pmu_write_counter,
  1785. .get_event_idx = armv7pmu_get_event_idx,
  1786. .start = armv7pmu_start,
  1787. .stop = armv7pmu_stop,
  1788. .max_period = (1LLU << 32) - 1,
  1789. };
  1790. static u32 __init armv7_reset_read_pmnc(void)
  1791. {
  1792. u32 nb_cnt;
  1793. /* Initialize & Reset PMNC: C and P bits */
  1794. armv7_pmnc_write(ARMV7_PMNC_P | ARMV7_PMNC_C);
  1795. /* Read the nb of CNTx counters supported from PMNC */
  1796. nb_cnt = (armv7_pmnc_read() >> ARMV7_PMNC_N_SHIFT) & ARMV7_PMNC_N_MASK;
  1797. /* Add the CPU cycles counter and return */
  1798. return nb_cnt + 1;
  1799. }
  1800. static int __init
  1801. init_hw_perf_events(void)
  1802. {
  1803. unsigned long cpuid = read_cpuid_id();
  1804. unsigned long implementor = (cpuid & 0xFF000000) >> 24;
  1805. unsigned long part_number = (cpuid & 0xFFF0);
  1806. /* We only support ARM CPUs implemented by ARM at the moment. */
  1807. if (0x41 == implementor) {
  1808. switch (part_number) {
  1809. case 0xB360: /* ARM1136 */
  1810. case 0xB560: /* ARM1156 */
  1811. case 0xB760: /* ARM1176 */
  1812. armpmu = &armv6pmu;
  1813. memcpy(armpmu_perf_cache_map, armv6_perf_cache_map,
  1814. sizeof(armv6_perf_cache_map));
  1815. perf_max_events = armv6pmu.num_events;
  1816. break;
  1817. case 0xB020: /* ARM11mpcore */
  1818. armpmu = &armv6mpcore_pmu;
  1819. memcpy(armpmu_perf_cache_map,
  1820. armv6mpcore_perf_cache_map,
  1821. sizeof(armv6mpcore_perf_cache_map));
  1822. perf_max_events = armv6mpcore_pmu.num_events;
  1823. break;
  1824. case 0xC080: /* Cortex-A8 */
  1825. armv7pmu.name = ARMV7_PMU_CORTEX_A8_NAME;
  1826. memcpy(armpmu_perf_cache_map, armv7_a8_perf_cache_map,
  1827. sizeof(armv7_a8_perf_cache_map));
  1828. armv7pmu.event_map = armv7_a8_pmu_event_map;
  1829. armpmu = &armv7pmu;
  1830. /* Reset PMNC and read the nb of CNTx counters
  1831. supported */
  1832. armv7pmu.num_events = armv7_reset_read_pmnc();
  1833. perf_max_events = armv7pmu.num_events;
  1834. break;
  1835. case 0xC090: /* Cortex-A9 */
  1836. armv7pmu.name = ARMV7_PMU_CORTEX_A9_NAME;
  1837. memcpy(armpmu_perf_cache_map, armv7_a9_perf_cache_map,
  1838. sizeof(armv7_a9_perf_cache_map));
  1839. armv7pmu.event_map = armv7_a9_pmu_event_map;
  1840. armpmu = &armv7pmu;
  1841. /* Reset PMNC and read the nb of CNTx counters
  1842. supported */
  1843. armv7pmu.num_events = armv7_reset_read_pmnc();
  1844. perf_max_events = armv7pmu.num_events;
  1845. break;
  1846. default:
  1847. pr_info("no hardware support available\n");
  1848. perf_max_events = -1;
  1849. }
  1850. }
  1851. if (armpmu)
  1852. pr_info("enabled with %s PMU driver, %d counters available\n",
  1853. armpmu->name, armpmu->num_events);
  1854. return 0;
  1855. }
  1856. arch_initcall(init_hw_perf_events);
  1857. /*
  1858. * Callchain handling code.
  1859. */
  1860. static inline void
  1861. callchain_store(struct perf_callchain_entry *entry,
  1862. u64 ip)
  1863. {
  1864. if (entry->nr < PERF_MAX_STACK_DEPTH)
  1865. entry->ip[entry->nr++] = ip;
  1866. }
  1867. /*
  1868. * The registers we're interested in are at the end of the variable
  1869. * length saved register structure. The fp points at the end of this
  1870. * structure so the address of this struct is:
  1871. * (struct frame_tail *)(xxx->fp)-1
  1872. *
  1873. * This code has been adapted from the ARM OProfile support.
  1874. */
  1875. struct frame_tail {
  1876. struct frame_tail *fp;
  1877. unsigned long sp;
  1878. unsigned long lr;
  1879. } __attribute__((packed));
  1880. /*
  1881. * Get the return address for a single stackframe and return a pointer to the
  1882. * next frame tail.
  1883. */
  1884. static struct frame_tail *
  1885. user_backtrace(struct frame_tail *tail,
  1886. struct perf_callchain_entry *entry)
  1887. {
  1888. struct frame_tail buftail;
  1889. /* Also check accessibility of one struct frame_tail beyond */
  1890. if (!access_ok(VERIFY_READ, tail, sizeof(buftail)))
  1891. return NULL;
  1892. if (__copy_from_user_inatomic(&buftail, tail, sizeof(buftail)))
  1893. return NULL;
  1894. callchain_store(entry, buftail.lr);
  1895. /*
  1896. * Frame pointers should strictly progress back up the stack
  1897. * (towards higher addresses).
  1898. */
  1899. if (tail >= buftail.fp)
  1900. return NULL;
  1901. return buftail.fp - 1;
  1902. }
  1903. static void
  1904. perf_callchain_user(struct pt_regs *regs,
  1905. struct perf_callchain_entry *entry)
  1906. {
  1907. struct frame_tail *tail;
  1908. callchain_store(entry, PERF_CONTEXT_USER);
  1909. if (!user_mode(regs))
  1910. regs = task_pt_regs(current);
  1911. tail = (struct frame_tail *)regs->ARM_fp - 1;
  1912. while (tail && !((unsigned long)tail & 0x3))
  1913. tail = user_backtrace(tail, entry);
  1914. }
  1915. /*
  1916. * Gets called by walk_stackframe() for every stackframe. This will be called
  1917. * whist unwinding the stackframe and is like a subroutine return so we use
  1918. * the PC.
  1919. */
  1920. static int
  1921. callchain_trace(struct stackframe *fr,
  1922. void *data)
  1923. {
  1924. struct perf_callchain_entry *entry = data;
  1925. callchain_store(entry, fr->pc);
  1926. return 0;
  1927. }
  1928. static void
  1929. perf_callchain_kernel(struct pt_regs *regs,
  1930. struct perf_callchain_entry *entry)
  1931. {
  1932. struct stackframe fr;
  1933. callchain_store(entry, PERF_CONTEXT_KERNEL);
  1934. fr.fp = regs->ARM_fp;
  1935. fr.sp = regs->ARM_sp;
  1936. fr.lr = regs->ARM_lr;
  1937. fr.pc = regs->ARM_pc;
  1938. walk_stackframe(&fr, callchain_trace, entry);
  1939. }
  1940. static void
  1941. perf_do_callchain(struct pt_regs *regs,
  1942. struct perf_callchain_entry *entry)
  1943. {
  1944. int is_user;
  1945. if (!regs)
  1946. return;
  1947. is_user = user_mode(regs);
  1948. if (!current || !current->pid)
  1949. return;
  1950. if (is_user && current->state != TASK_RUNNING)
  1951. return;
  1952. if (!is_user)
  1953. perf_callchain_kernel(regs, entry);
  1954. if (current->mm)
  1955. perf_callchain_user(regs, entry);
  1956. }
  1957. static DEFINE_PER_CPU(struct perf_callchain_entry, pmc_irq_entry);
  1958. struct perf_callchain_entry *
  1959. perf_callchain(struct pt_regs *regs)
  1960. {
  1961. struct perf_callchain_entry *entry = &__get_cpu_var(pmc_irq_entry);
  1962. entry->nr = 0;
  1963. perf_do_callchain(regs, entry);
  1964. return entry;
  1965. }