perf_event.c 78 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070
  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/module.h>
  18. #include <linux/perf_event.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/spinlock.h>
  21. #include <linux/uaccess.h>
  22. #include <asm/cputype.h>
  23. #include <asm/irq.h>
  24. #include <asm/irq_regs.h>
  25. #include <asm/pmu.h>
  26. #include <asm/stacktrace.h>
  27. static struct platform_device *pmu_device;
  28. /*
  29. * Hardware lock to serialize accesses to PMU registers. Needed for the
  30. * read/modify/write sequences.
  31. */
  32. DEFINE_SPINLOCK(pmu_lock);
  33. /*
  34. * ARMv6 supports a maximum of 3 events, starting from index 1. If we add
  35. * another platform that supports more, we need to increase this to be the
  36. * largest of all platforms.
  37. *
  38. * ARMv7 supports up to 32 events:
  39. * cycle counter CCNT + 31 events counters CNT0..30.
  40. * Cortex-A8 has 1+4 counters, Cortex-A9 has 1+6 counters.
  41. */
  42. #define ARMPMU_MAX_HWEVENTS 33
  43. /* The events for a given CPU. */
  44. struct cpu_hw_events {
  45. /*
  46. * The events that are active on the CPU for the given index. Index 0
  47. * is reserved.
  48. */
  49. struct perf_event *events[ARMPMU_MAX_HWEVENTS];
  50. /*
  51. * A 1 bit for an index indicates that the counter is being used for
  52. * an event. A 0 means that the counter can be used.
  53. */
  54. unsigned long used_mask[BITS_TO_LONGS(ARMPMU_MAX_HWEVENTS)];
  55. /*
  56. * A 1 bit for an index indicates that the counter is actively being
  57. * used.
  58. */
  59. unsigned long active_mask[BITS_TO_LONGS(ARMPMU_MAX_HWEVENTS)];
  60. };
  61. DEFINE_PER_CPU(struct cpu_hw_events, cpu_hw_events);
  62. /* PMU names. */
  63. static const char *arm_pmu_names[] = {
  64. [ARM_PERF_PMU_ID_XSCALE1] = "xscale1",
  65. [ARM_PERF_PMU_ID_XSCALE2] = "xscale2",
  66. [ARM_PERF_PMU_ID_V6] = "v6",
  67. [ARM_PERF_PMU_ID_V6MP] = "v6mpcore",
  68. [ARM_PERF_PMU_ID_CA8] = "ARMv7 Cortex-A8",
  69. [ARM_PERF_PMU_ID_CA9] = "ARMv7 Cortex-A9",
  70. };
  71. struct arm_pmu {
  72. enum arm_perf_pmu_ids id;
  73. irqreturn_t (*handle_irq)(int irq_num, void *dev);
  74. void (*enable)(struct hw_perf_event *evt, int idx);
  75. void (*disable)(struct hw_perf_event *evt, int idx);
  76. int (*get_event_idx)(struct cpu_hw_events *cpuc,
  77. struct hw_perf_event *hwc);
  78. u32 (*read_counter)(int idx);
  79. void (*write_counter)(int idx, u32 val);
  80. void (*start)(void);
  81. void (*stop)(void);
  82. const unsigned (*cache_map)[PERF_COUNT_HW_CACHE_MAX]
  83. [PERF_COUNT_HW_CACHE_OP_MAX]
  84. [PERF_COUNT_HW_CACHE_RESULT_MAX];
  85. const unsigned (*event_map)[PERF_COUNT_HW_MAX];
  86. u32 raw_event_mask;
  87. int num_events;
  88. u64 max_period;
  89. };
  90. /* Set at runtime when we know what CPU type we are. */
  91. static const struct arm_pmu *armpmu;
  92. enum arm_perf_pmu_ids
  93. armpmu_get_pmu_id(void)
  94. {
  95. int id = -ENODEV;
  96. if (armpmu != NULL)
  97. id = armpmu->id;
  98. return id;
  99. }
  100. EXPORT_SYMBOL_GPL(armpmu_get_pmu_id);
  101. int
  102. armpmu_get_max_events(void)
  103. {
  104. int max_events = 0;
  105. if (armpmu != NULL)
  106. max_events = armpmu->num_events;
  107. return max_events;
  108. }
  109. EXPORT_SYMBOL_GPL(armpmu_get_max_events);
  110. int perf_num_counters(void)
  111. {
  112. return armpmu_get_max_events();
  113. }
  114. EXPORT_SYMBOL_GPL(perf_num_counters);
  115. #define HW_OP_UNSUPPORTED 0xFFFF
  116. #define C(_x) \
  117. PERF_COUNT_HW_CACHE_##_x
  118. #define CACHE_OP_UNSUPPORTED 0xFFFF
  119. static int
  120. armpmu_map_cache_event(u64 config)
  121. {
  122. unsigned int cache_type, cache_op, cache_result, ret;
  123. cache_type = (config >> 0) & 0xff;
  124. if (cache_type >= PERF_COUNT_HW_CACHE_MAX)
  125. return -EINVAL;
  126. cache_op = (config >> 8) & 0xff;
  127. if (cache_op >= PERF_COUNT_HW_CACHE_OP_MAX)
  128. return -EINVAL;
  129. cache_result = (config >> 16) & 0xff;
  130. if (cache_result >= PERF_COUNT_HW_CACHE_RESULT_MAX)
  131. return -EINVAL;
  132. ret = (int)(*armpmu->cache_map)[cache_type][cache_op][cache_result];
  133. if (ret == CACHE_OP_UNSUPPORTED)
  134. return -ENOENT;
  135. return ret;
  136. }
  137. static int
  138. armpmu_map_event(u64 config)
  139. {
  140. int mapping = (*armpmu->event_map)[config];
  141. return mapping == HW_OP_UNSUPPORTED ? -EOPNOTSUPP : mapping;
  142. }
  143. static int
  144. armpmu_map_raw_event(u64 config)
  145. {
  146. return (int)(config & armpmu->raw_event_mask);
  147. }
  148. static int
  149. armpmu_event_set_period(struct perf_event *event,
  150. struct hw_perf_event *hwc,
  151. int idx)
  152. {
  153. s64 left = local64_read(&hwc->period_left);
  154. s64 period = hwc->sample_period;
  155. int ret = 0;
  156. if (unlikely(left <= -period)) {
  157. left = period;
  158. local64_set(&hwc->period_left, left);
  159. hwc->last_period = period;
  160. ret = 1;
  161. }
  162. if (unlikely(left <= 0)) {
  163. left += period;
  164. local64_set(&hwc->period_left, left);
  165. hwc->last_period = period;
  166. ret = 1;
  167. }
  168. if (left > (s64)armpmu->max_period)
  169. left = armpmu->max_period;
  170. local64_set(&hwc->prev_count, (u64)-left);
  171. armpmu->write_counter(idx, (u64)(-left) & 0xffffffff);
  172. perf_event_update_userpage(event);
  173. return ret;
  174. }
  175. static u64
  176. armpmu_event_update(struct perf_event *event,
  177. struct hw_perf_event *hwc,
  178. int idx)
  179. {
  180. int shift = 64 - 32;
  181. s64 prev_raw_count, new_raw_count;
  182. u64 delta;
  183. again:
  184. prev_raw_count = local64_read(&hwc->prev_count);
  185. new_raw_count = armpmu->read_counter(idx);
  186. if (local64_cmpxchg(&hwc->prev_count, prev_raw_count,
  187. new_raw_count) != prev_raw_count)
  188. goto again;
  189. delta = (new_raw_count << shift) - (prev_raw_count << shift);
  190. delta >>= shift;
  191. local64_add(delta, &event->count);
  192. local64_sub(delta, &hwc->period_left);
  193. return new_raw_count;
  194. }
  195. static void
  196. armpmu_read(struct perf_event *event)
  197. {
  198. struct hw_perf_event *hwc = &event->hw;
  199. /* Don't read disabled counters! */
  200. if (hwc->idx < 0)
  201. return;
  202. armpmu_event_update(event, hwc, hwc->idx);
  203. }
  204. static void
  205. armpmu_stop(struct perf_event *event, int flags)
  206. {
  207. struct hw_perf_event *hwc = &event->hw;
  208. if (!armpmu)
  209. return;
  210. /*
  211. * ARM pmu always has to update the counter, so ignore
  212. * PERF_EF_UPDATE, see comments in armpmu_start().
  213. */
  214. if (!(hwc->state & PERF_HES_STOPPED)) {
  215. armpmu->disable(hwc, hwc->idx);
  216. barrier(); /* why? */
  217. armpmu_event_update(event, hwc, hwc->idx);
  218. hwc->state |= PERF_HES_STOPPED | PERF_HES_UPTODATE;
  219. }
  220. }
  221. static void
  222. armpmu_start(struct perf_event *event, int flags)
  223. {
  224. struct hw_perf_event *hwc = &event->hw;
  225. if (!armpmu)
  226. return;
  227. /*
  228. * ARM pmu always has to reprogram the period, so ignore
  229. * PERF_EF_RELOAD, see the comment below.
  230. */
  231. if (flags & PERF_EF_RELOAD)
  232. WARN_ON_ONCE(!(hwc->state & PERF_HES_UPTODATE));
  233. hwc->state = 0;
  234. /*
  235. * Set the period again. Some counters can't be stopped, so when we
  236. * were stopped we simply disabled the IRQ source and the counter
  237. * may have been left counting. If we don't do this step then we may
  238. * get an interrupt too soon or *way* too late if the overflow has
  239. * happened since disabling.
  240. */
  241. armpmu_event_set_period(event, hwc, hwc->idx);
  242. armpmu->enable(hwc, hwc->idx);
  243. }
  244. static void
  245. armpmu_del(struct perf_event *event, int flags)
  246. {
  247. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  248. struct hw_perf_event *hwc = &event->hw;
  249. int idx = hwc->idx;
  250. WARN_ON(idx < 0);
  251. clear_bit(idx, cpuc->active_mask);
  252. armpmu_stop(event, PERF_EF_UPDATE);
  253. cpuc->events[idx] = NULL;
  254. clear_bit(idx, cpuc->used_mask);
  255. perf_event_update_userpage(event);
  256. }
  257. static int
  258. armpmu_add(struct perf_event *event, int flags)
  259. {
  260. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  261. struct hw_perf_event *hwc = &event->hw;
  262. int idx;
  263. int err = 0;
  264. perf_pmu_disable(event->pmu);
  265. /* If we don't have a space for the counter then finish early. */
  266. idx = armpmu->get_event_idx(cpuc, hwc);
  267. if (idx < 0) {
  268. err = idx;
  269. goto out;
  270. }
  271. /*
  272. * If there is an event in the counter we are going to use then make
  273. * sure it is disabled.
  274. */
  275. event->hw.idx = idx;
  276. armpmu->disable(hwc, idx);
  277. cpuc->events[idx] = event;
  278. set_bit(idx, cpuc->active_mask);
  279. hwc->state = PERF_HES_STOPPED | PERF_HES_UPTODATE;
  280. if (flags & PERF_EF_START)
  281. armpmu_start(event, PERF_EF_RELOAD);
  282. /* Propagate our changes to the userspace mapping. */
  283. perf_event_update_userpage(event);
  284. out:
  285. perf_pmu_enable(event->pmu);
  286. return err;
  287. }
  288. static struct pmu pmu;
  289. static int
  290. validate_event(struct cpu_hw_events *cpuc,
  291. struct perf_event *event)
  292. {
  293. struct hw_perf_event fake_event = event->hw;
  294. if (event->pmu != &pmu || event->state <= PERF_EVENT_STATE_OFF)
  295. return 1;
  296. return armpmu->get_event_idx(cpuc, &fake_event) >= 0;
  297. }
  298. static int
  299. validate_group(struct perf_event *event)
  300. {
  301. struct perf_event *sibling, *leader = event->group_leader;
  302. struct cpu_hw_events fake_pmu;
  303. memset(&fake_pmu, 0, sizeof(fake_pmu));
  304. if (!validate_event(&fake_pmu, leader))
  305. return -ENOSPC;
  306. list_for_each_entry(sibling, &leader->sibling_list, group_entry) {
  307. if (!validate_event(&fake_pmu, sibling))
  308. return -ENOSPC;
  309. }
  310. if (!validate_event(&fake_pmu, event))
  311. return -ENOSPC;
  312. return 0;
  313. }
  314. static int
  315. armpmu_reserve_hardware(void)
  316. {
  317. int i, err = -ENODEV, irq;
  318. pmu_device = reserve_pmu(ARM_PMU_DEVICE_CPU);
  319. if (IS_ERR(pmu_device)) {
  320. pr_warning("unable to reserve pmu\n");
  321. return PTR_ERR(pmu_device);
  322. }
  323. init_pmu(ARM_PMU_DEVICE_CPU);
  324. if (pmu_device->num_resources < 1) {
  325. pr_err("no irqs for PMUs defined\n");
  326. return -ENODEV;
  327. }
  328. for (i = 0; i < pmu_device->num_resources; ++i) {
  329. irq = platform_get_irq(pmu_device, i);
  330. if (irq < 0)
  331. continue;
  332. err = request_irq(irq, armpmu->handle_irq,
  333. IRQF_DISABLED | IRQF_NOBALANCING,
  334. "armpmu", NULL);
  335. if (err) {
  336. pr_warning("unable to request IRQ%d for ARM perf "
  337. "counters\n", irq);
  338. break;
  339. }
  340. }
  341. if (err) {
  342. for (i = i - 1; i >= 0; --i) {
  343. irq = platform_get_irq(pmu_device, i);
  344. if (irq >= 0)
  345. free_irq(irq, NULL);
  346. }
  347. release_pmu(pmu_device);
  348. pmu_device = NULL;
  349. }
  350. return err;
  351. }
  352. static void
  353. armpmu_release_hardware(void)
  354. {
  355. int i, irq;
  356. for (i = pmu_device->num_resources - 1; i >= 0; --i) {
  357. irq = platform_get_irq(pmu_device, i);
  358. if (irq >= 0)
  359. free_irq(irq, NULL);
  360. }
  361. armpmu->stop();
  362. release_pmu(pmu_device);
  363. pmu_device = NULL;
  364. }
  365. static atomic_t active_events = ATOMIC_INIT(0);
  366. static DEFINE_MUTEX(pmu_reserve_mutex);
  367. static void
  368. hw_perf_event_destroy(struct perf_event *event)
  369. {
  370. if (atomic_dec_and_mutex_lock(&active_events, &pmu_reserve_mutex)) {
  371. armpmu_release_hardware();
  372. mutex_unlock(&pmu_reserve_mutex);
  373. }
  374. }
  375. static int
  376. __hw_perf_event_init(struct perf_event *event)
  377. {
  378. struct hw_perf_event *hwc = &event->hw;
  379. int mapping, err;
  380. /* Decode the generic type into an ARM event identifier. */
  381. if (PERF_TYPE_HARDWARE == event->attr.type) {
  382. mapping = armpmu_map_event(event->attr.config);
  383. } else if (PERF_TYPE_HW_CACHE == event->attr.type) {
  384. mapping = armpmu_map_cache_event(event->attr.config);
  385. } else if (PERF_TYPE_RAW == event->attr.type) {
  386. mapping = armpmu_map_raw_event(event->attr.config);
  387. } else {
  388. pr_debug("event type %x not supported\n", event->attr.type);
  389. return -EOPNOTSUPP;
  390. }
  391. if (mapping < 0) {
  392. pr_debug("event %x:%llx not supported\n", event->attr.type,
  393. event->attr.config);
  394. return mapping;
  395. }
  396. /*
  397. * Check whether we need to exclude the counter from certain modes.
  398. * The ARM performance counters are on all of the time so if someone
  399. * has asked us for some excludes then we have to fail.
  400. */
  401. if (event->attr.exclude_kernel || event->attr.exclude_user ||
  402. event->attr.exclude_hv || event->attr.exclude_idle) {
  403. pr_debug("ARM performance counters do not support "
  404. "mode exclusion\n");
  405. return -EPERM;
  406. }
  407. /*
  408. * We don't assign an index until we actually place the event onto
  409. * hardware. Use -1 to signify that we haven't decided where to put it
  410. * yet. For SMP systems, each core has it's own PMU so we can't do any
  411. * clever allocation or constraints checking at this point.
  412. */
  413. hwc->idx = -1;
  414. /*
  415. * Store the event encoding into the config_base field. config and
  416. * event_base are unused as the only 2 things we need to know are
  417. * the event mapping and the counter to use. The counter to use is
  418. * also the indx and the config_base is the event type.
  419. */
  420. hwc->config_base = (unsigned long)mapping;
  421. hwc->config = 0;
  422. hwc->event_base = 0;
  423. if (!hwc->sample_period) {
  424. hwc->sample_period = armpmu->max_period;
  425. hwc->last_period = hwc->sample_period;
  426. local64_set(&hwc->period_left, hwc->sample_period);
  427. }
  428. err = 0;
  429. if (event->group_leader != event) {
  430. err = validate_group(event);
  431. if (err)
  432. return -EINVAL;
  433. }
  434. return err;
  435. }
  436. static int armpmu_event_init(struct perf_event *event)
  437. {
  438. int err = 0;
  439. switch (event->attr.type) {
  440. case PERF_TYPE_RAW:
  441. case PERF_TYPE_HARDWARE:
  442. case PERF_TYPE_HW_CACHE:
  443. break;
  444. default:
  445. return -ENOENT;
  446. }
  447. if (!armpmu)
  448. return -ENODEV;
  449. event->destroy = hw_perf_event_destroy;
  450. if (!atomic_inc_not_zero(&active_events)) {
  451. if (atomic_read(&active_events) > armpmu->num_events) {
  452. atomic_dec(&active_events);
  453. return -ENOSPC;
  454. }
  455. mutex_lock(&pmu_reserve_mutex);
  456. if (atomic_read(&active_events) == 0) {
  457. err = armpmu_reserve_hardware();
  458. }
  459. if (!err)
  460. atomic_inc(&active_events);
  461. mutex_unlock(&pmu_reserve_mutex);
  462. }
  463. if (err)
  464. return err;
  465. err = __hw_perf_event_init(event);
  466. if (err)
  467. hw_perf_event_destroy(event);
  468. return err;
  469. }
  470. static void armpmu_enable(struct pmu *pmu)
  471. {
  472. /* Enable all of the perf events on hardware. */
  473. int idx;
  474. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  475. if (!armpmu)
  476. return;
  477. for (idx = 0; idx <= armpmu->num_events; ++idx) {
  478. struct perf_event *event = cpuc->events[idx];
  479. if (!event)
  480. continue;
  481. armpmu->enable(&event->hw, idx);
  482. }
  483. armpmu->start();
  484. }
  485. static void armpmu_disable(struct pmu *pmu)
  486. {
  487. if (armpmu)
  488. armpmu->stop();
  489. }
  490. static struct pmu pmu = {
  491. .pmu_enable = armpmu_enable,
  492. .pmu_disable = armpmu_disable,
  493. .event_init = armpmu_event_init,
  494. .add = armpmu_add,
  495. .del = armpmu_del,
  496. .start = armpmu_start,
  497. .stop = armpmu_stop,
  498. .read = armpmu_read,
  499. };
  500. /*
  501. * ARMv6 Performance counter handling code.
  502. *
  503. * ARMv6 has 2 configurable performance counters and a single cycle counter.
  504. * They all share a single reset bit but can be written to zero so we can use
  505. * that for a reset.
  506. *
  507. * The counters can't be individually enabled or disabled so when we remove
  508. * one event and replace it with another we could get spurious counts from the
  509. * wrong event. However, we can take advantage of the fact that the
  510. * performance counters can export events to the event bus, and the event bus
  511. * itself can be monitored. This requires that we *don't* export the events to
  512. * the event bus. The procedure for disabling a configurable counter is:
  513. * - change the counter to count the ETMEXTOUT[0] signal (0x20). This
  514. * effectively stops the counter from counting.
  515. * - disable the counter's interrupt generation (each counter has it's
  516. * own interrupt enable bit).
  517. * Once stopped, the counter value can be written as 0 to reset.
  518. *
  519. * To enable a counter:
  520. * - enable the counter's interrupt generation.
  521. * - set the new event type.
  522. *
  523. * Note: the dedicated cycle counter only counts cycles and can't be
  524. * enabled/disabled independently of the others. When we want to disable the
  525. * cycle counter, we have to just disable the interrupt reporting and start
  526. * ignoring that counter. When re-enabling, we have to reset the value and
  527. * enable the interrupt.
  528. */
  529. enum armv6_perf_types {
  530. ARMV6_PERFCTR_ICACHE_MISS = 0x0,
  531. ARMV6_PERFCTR_IBUF_STALL = 0x1,
  532. ARMV6_PERFCTR_DDEP_STALL = 0x2,
  533. ARMV6_PERFCTR_ITLB_MISS = 0x3,
  534. ARMV6_PERFCTR_DTLB_MISS = 0x4,
  535. ARMV6_PERFCTR_BR_EXEC = 0x5,
  536. ARMV6_PERFCTR_BR_MISPREDICT = 0x6,
  537. ARMV6_PERFCTR_INSTR_EXEC = 0x7,
  538. ARMV6_PERFCTR_DCACHE_HIT = 0x9,
  539. ARMV6_PERFCTR_DCACHE_ACCESS = 0xA,
  540. ARMV6_PERFCTR_DCACHE_MISS = 0xB,
  541. ARMV6_PERFCTR_DCACHE_WBACK = 0xC,
  542. ARMV6_PERFCTR_SW_PC_CHANGE = 0xD,
  543. ARMV6_PERFCTR_MAIN_TLB_MISS = 0xF,
  544. ARMV6_PERFCTR_EXPL_D_ACCESS = 0x10,
  545. ARMV6_PERFCTR_LSU_FULL_STALL = 0x11,
  546. ARMV6_PERFCTR_WBUF_DRAINED = 0x12,
  547. ARMV6_PERFCTR_CPU_CYCLES = 0xFF,
  548. ARMV6_PERFCTR_NOP = 0x20,
  549. };
  550. enum armv6_counters {
  551. ARMV6_CYCLE_COUNTER = 1,
  552. ARMV6_COUNTER0,
  553. ARMV6_COUNTER1,
  554. };
  555. /*
  556. * The hardware events that we support. We do support cache operations but
  557. * we have harvard caches and no way to combine instruction and data
  558. * accesses/misses in hardware.
  559. */
  560. static const unsigned armv6_perf_map[PERF_COUNT_HW_MAX] = {
  561. [PERF_COUNT_HW_CPU_CYCLES] = ARMV6_PERFCTR_CPU_CYCLES,
  562. [PERF_COUNT_HW_INSTRUCTIONS] = ARMV6_PERFCTR_INSTR_EXEC,
  563. [PERF_COUNT_HW_CACHE_REFERENCES] = HW_OP_UNSUPPORTED,
  564. [PERF_COUNT_HW_CACHE_MISSES] = HW_OP_UNSUPPORTED,
  565. [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = ARMV6_PERFCTR_BR_EXEC,
  566. [PERF_COUNT_HW_BRANCH_MISSES] = ARMV6_PERFCTR_BR_MISPREDICT,
  567. [PERF_COUNT_HW_BUS_CYCLES] = HW_OP_UNSUPPORTED,
  568. };
  569. static const unsigned armv6_perf_cache_map[PERF_COUNT_HW_CACHE_MAX]
  570. [PERF_COUNT_HW_CACHE_OP_MAX]
  571. [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
  572. [C(L1D)] = {
  573. /*
  574. * The performance counters don't differentiate between read
  575. * and write accesses/misses so this isn't strictly correct,
  576. * but it's the best we can do. Writes and reads get
  577. * combined.
  578. */
  579. [C(OP_READ)] = {
  580. [C(RESULT_ACCESS)] = ARMV6_PERFCTR_DCACHE_ACCESS,
  581. [C(RESULT_MISS)] = ARMV6_PERFCTR_DCACHE_MISS,
  582. },
  583. [C(OP_WRITE)] = {
  584. [C(RESULT_ACCESS)] = ARMV6_PERFCTR_DCACHE_ACCESS,
  585. [C(RESULT_MISS)] = ARMV6_PERFCTR_DCACHE_MISS,
  586. },
  587. [C(OP_PREFETCH)] = {
  588. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  589. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  590. },
  591. },
  592. [C(L1I)] = {
  593. [C(OP_READ)] = {
  594. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  595. [C(RESULT_MISS)] = ARMV6_PERFCTR_ICACHE_MISS,
  596. },
  597. [C(OP_WRITE)] = {
  598. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  599. [C(RESULT_MISS)] = ARMV6_PERFCTR_ICACHE_MISS,
  600. },
  601. [C(OP_PREFETCH)] = {
  602. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  603. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  604. },
  605. },
  606. [C(LL)] = {
  607. [C(OP_READ)] = {
  608. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  609. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  610. },
  611. [C(OP_WRITE)] = {
  612. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  613. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  614. },
  615. [C(OP_PREFETCH)] = {
  616. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  617. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  618. },
  619. },
  620. [C(DTLB)] = {
  621. /*
  622. * The ARM performance counters can count micro DTLB misses,
  623. * micro ITLB misses and main TLB misses. There isn't an event
  624. * for TLB misses, so use the micro misses here and if users
  625. * want the main TLB misses they can use a raw counter.
  626. */
  627. [C(OP_READ)] = {
  628. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  629. [C(RESULT_MISS)] = ARMV6_PERFCTR_DTLB_MISS,
  630. },
  631. [C(OP_WRITE)] = {
  632. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  633. [C(RESULT_MISS)] = ARMV6_PERFCTR_DTLB_MISS,
  634. },
  635. [C(OP_PREFETCH)] = {
  636. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  637. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  638. },
  639. },
  640. [C(ITLB)] = {
  641. [C(OP_READ)] = {
  642. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  643. [C(RESULT_MISS)] = ARMV6_PERFCTR_ITLB_MISS,
  644. },
  645. [C(OP_WRITE)] = {
  646. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  647. [C(RESULT_MISS)] = ARMV6_PERFCTR_ITLB_MISS,
  648. },
  649. [C(OP_PREFETCH)] = {
  650. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  651. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  652. },
  653. },
  654. [C(BPU)] = {
  655. [C(OP_READ)] = {
  656. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  657. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  658. },
  659. [C(OP_WRITE)] = {
  660. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  661. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  662. },
  663. [C(OP_PREFETCH)] = {
  664. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  665. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  666. },
  667. },
  668. };
  669. enum armv6mpcore_perf_types {
  670. ARMV6MPCORE_PERFCTR_ICACHE_MISS = 0x0,
  671. ARMV6MPCORE_PERFCTR_IBUF_STALL = 0x1,
  672. ARMV6MPCORE_PERFCTR_DDEP_STALL = 0x2,
  673. ARMV6MPCORE_PERFCTR_ITLB_MISS = 0x3,
  674. ARMV6MPCORE_PERFCTR_DTLB_MISS = 0x4,
  675. ARMV6MPCORE_PERFCTR_BR_EXEC = 0x5,
  676. ARMV6MPCORE_PERFCTR_BR_NOTPREDICT = 0x6,
  677. ARMV6MPCORE_PERFCTR_BR_MISPREDICT = 0x7,
  678. ARMV6MPCORE_PERFCTR_INSTR_EXEC = 0x8,
  679. ARMV6MPCORE_PERFCTR_DCACHE_RDACCESS = 0xA,
  680. ARMV6MPCORE_PERFCTR_DCACHE_RDMISS = 0xB,
  681. ARMV6MPCORE_PERFCTR_DCACHE_WRACCESS = 0xC,
  682. ARMV6MPCORE_PERFCTR_DCACHE_WRMISS = 0xD,
  683. ARMV6MPCORE_PERFCTR_DCACHE_EVICTION = 0xE,
  684. ARMV6MPCORE_PERFCTR_SW_PC_CHANGE = 0xF,
  685. ARMV6MPCORE_PERFCTR_MAIN_TLB_MISS = 0x10,
  686. ARMV6MPCORE_PERFCTR_EXPL_MEM_ACCESS = 0x11,
  687. ARMV6MPCORE_PERFCTR_LSU_FULL_STALL = 0x12,
  688. ARMV6MPCORE_PERFCTR_WBUF_DRAINED = 0x13,
  689. ARMV6MPCORE_PERFCTR_CPU_CYCLES = 0xFF,
  690. };
  691. /*
  692. * The hardware events that we support. We do support cache operations but
  693. * we have harvard caches and no way to combine instruction and data
  694. * accesses/misses in hardware.
  695. */
  696. static const unsigned armv6mpcore_perf_map[PERF_COUNT_HW_MAX] = {
  697. [PERF_COUNT_HW_CPU_CYCLES] = ARMV6MPCORE_PERFCTR_CPU_CYCLES,
  698. [PERF_COUNT_HW_INSTRUCTIONS] = ARMV6MPCORE_PERFCTR_INSTR_EXEC,
  699. [PERF_COUNT_HW_CACHE_REFERENCES] = HW_OP_UNSUPPORTED,
  700. [PERF_COUNT_HW_CACHE_MISSES] = HW_OP_UNSUPPORTED,
  701. [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = ARMV6MPCORE_PERFCTR_BR_EXEC,
  702. [PERF_COUNT_HW_BRANCH_MISSES] = ARMV6MPCORE_PERFCTR_BR_MISPREDICT,
  703. [PERF_COUNT_HW_BUS_CYCLES] = HW_OP_UNSUPPORTED,
  704. };
  705. static const unsigned armv6mpcore_perf_cache_map[PERF_COUNT_HW_CACHE_MAX]
  706. [PERF_COUNT_HW_CACHE_OP_MAX]
  707. [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
  708. [C(L1D)] = {
  709. [C(OP_READ)] = {
  710. [C(RESULT_ACCESS)] =
  711. ARMV6MPCORE_PERFCTR_DCACHE_RDACCESS,
  712. [C(RESULT_MISS)] =
  713. ARMV6MPCORE_PERFCTR_DCACHE_RDMISS,
  714. },
  715. [C(OP_WRITE)] = {
  716. [C(RESULT_ACCESS)] =
  717. ARMV6MPCORE_PERFCTR_DCACHE_WRACCESS,
  718. [C(RESULT_MISS)] =
  719. ARMV6MPCORE_PERFCTR_DCACHE_WRMISS,
  720. },
  721. [C(OP_PREFETCH)] = {
  722. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  723. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  724. },
  725. },
  726. [C(L1I)] = {
  727. [C(OP_READ)] = {
  728. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  729. [C(RESULT_MISS)] = ARMV6MPCORE_PERFCTR_ICACHE_MISS,
  730. },
  731. [C(OP_WRITE)] = {
  732. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  733. [C(RESULT_MISS)] = ARMV6MPCORE_PERFCTR_ICACHE_MISS,
  734. },
  735. [C(OP_PREFETCH)] = {
  736. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  737. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  738. },
  739. },
  740. [C(LL)] = {
  741. [C(OP_READ)] = {
  742. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  743. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  744. },
  745. [C(OP_WRITE)] = {
  746. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  747. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  748. },
  749. [C(OP_PREFETCH)] = {
  750. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  751. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  752. },
  753. },
  754. [C(DTLB)] = {
  755. /*
  756. * The ARM performance counters can count micro DTLB misses,
  757. * micro ITLB misses and main TLB misses. There isn't an event
  758. * for TLB misses, so use the micro misses here and if users
  759. * want the main TLB misses they can use a raw counter.
  760. */
  761. [C(OP_READ)] = {
  762. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  763. [C(RESULT_MISS)] = ARMV6MPCORE_PERFCTR_DTLB_MISS,
  764. },
  765. [C(OP_WRITE)] = {
  766. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  767. [C(RESULT_MISS)] = ARMV6MPCORE_PERFCTR_DTLB_MISS,
  768. },
  769. [C(OP_PREFETCH)] = {
  770. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  771. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  772. },
  773. },
  774. [C(ITLB)] = {
  775. [C(OP_READ)] = {
  776. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  777. [C(RESULT_MISS)] = ARMV6MPCORE_PERFCTR_ITLB_MISS,
  778. },
  779. [C(OP_WRITE)] = {
  780. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  781. [C(RESULT_MISS)] = ARMV6MPCORE_PERFCTR_ITLB_MISS,
  782. },
  783. [C(OP_PREFETCH)] = {
  784. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  785. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  786. },
  787. },
  788. [C(BPU)] = {
  789. [C(OP_READ)] = {
  790. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  791. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  792. },
  793. [C(OP_WRITE)] = {
  794. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  795. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  796. },
  797. [C(OP_PREFETCH)] = {
  798. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  799. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  800. },
  801. },
  802. };
  803. static inline unsigned long
  804. armv6_pmcr_read(void)
  805. {
  806. u32 val;
  807. asm volatile("mrc p15, 0, %0, c15, c12, 0" : "=r"(val));
  808. return val;
  809. }
  810. static inline void
  811. armv6_pmcr_write(unsigned long val)
  812. {
  813. asm volatile("mcr p15, 0, %0, c15, c12, 0" : : "r"(val));
  814. }
  815. #define ARMV6_PMCR_ENABLE (1 << 0)
  816. #define ARMV6_PMCR_CTR01_RESET (1 << 1)
  817. #define ARMV6_PMCR_CCOUNT_RESET (1 << 2)
  818. #define ARMV6_PMCR_CCOUNT_DIV (1 << 3)
  819. #define ARMV6_PMCR_COUNT0_IEN (1 << 4)
  820. #define ARMV6_PMCR_COUNT1_IEN (1 << 5)
  821. #define ARMV6_PMCR_CCOUNT_IEN (1 << 6)
  822. #define ARMV6_PMCR_COUNT0_OVERFLOW (1 << 8)
  823. #define ARMV6_PMCR_COUNT1_OVERFLOW (1 << 9)
  824. #define ARMV6_PMCR_CCOUNT_OVERFLOW (1 << 10)
  825. #define ARMV6_PMCR_EVT_COUNT0_SHIFT 20
  826. #define ARMV6_PMCR_EVT_COUNT0_MASK (0xFF << ARMV6_PMCR_EVT_COUNT0_SHIFT)
  827. #define ARMV6_PMCR_EVT_COUNT1_SHIFT 12
  828. #define ARMV6_PMCR_EVT_COUNT1_MASK (0xFF << ARMV6_PMCR_EVT_COUNT1_SHIFT)
  829. #define ARMV6_PMCR_OVERFLOWED_MASK \
  830. (ARMV6_PMCR_COUNT0_OVERFLOW | ARMV6_PMCR_COUNT1_OVERFLOW | \
  831. ARMV6_PMCR_CCOUNT_OVERFLOW)
  832. static inline int
  833. armv6_pmcr_has_overflowed(unsigned long pmcr)
  834. {
  835. return (pmcr & ARMV6_PMCR_OVERFLOWED_MASK);
  836. }
  837. static inline int
  838. armv6_pmcr_counter_has_overflowed(unsigned long pmcr,
  839. enum armv6_counters counter)
  840. {
  841. int ret = 0;
  842. if (ARMV6_CYCLE_COUNTER == counter)
  843. ret = pmcr & ARMV6_PMCR_CCOUNT_OVERFLOW;
  844. else if (ARMV6_COUNTER0 == counter)
  845. ret = pmcr & ARMV6_PMCR_COUNT0_OVERFLOW;
  846. else if (ARMV6_COUNTER1 == counter)
  847. ret = pmcr & ARMV6_PMCR_COUNT1_OVERFLOW;
  848. else
  849. WARN_ONCE(1, "invalid counter number (%d)\n", counter);
  850. return ret;
  851. }
  852. static inline u32
  853. armv6pmu_read_counter(int counter)
  854. {
  855. unsigned long value = 0;
  856. if (ARMV6_CYCLE_COUNTER == counter)
  857. asm volatile("mrc p15, 0, %0, c15, c12, 1" : "=r"(value));
  858. else if (ARMV6_COUNTER0 == counter)
  859. asm volatile("mrc p15, 0, %0, c15, c12, 2" : "=r"(value));
  860. else if (ARMV6_COUNTER1 == counter)
  861. asm volatile("mrc p15, 0, %0, c15, c12, 3" : "=r"(value));
  862. else
  863. WARN_ONCE(1, "invalid counter number (%d)\n", counter);
  864. return value;
  865. }
  866. static inline void
  867. armv6pmu_write_counter(int counter,
  868. u32 value)
  869. {
  870. if (ARMV6_CYCLE_COUNTER == counter)
  871. asm volatile("mcr p15, 0, %0, c15, c12, 1" : : "r"(value));
  872. else if (ARMV6_COUNTER0 == counter)
  873. asm volatile("mcr p15, 0, %0, c15, c12, 2" : : "r"(value));
  874. else if (ARMV6_COUNTER1 == counter)
  875. asm volatile("mcr p15, 0, %0, c15, c12, 3" : : "r"(value));
  876. else
  877. WARN_ONCE(1, "invalid counter number (%d)\n", counter);
  878. }
  879. void
  880. armv6pmu_enable_event(struct hw_perf_event *hwc,
  881. int idx)
  882. {
  883. unsigned long val, mask, evt, flags;
  884. if (ARMV6_CYCLE_COUNTER == idx) {
  885. mask = 0;
  886. evt = ARMV6_PMCR_CCOUNT_IEN;
  887. } else if (ARMV6_COUNTER0 == idx) {
  888. mask = ARMV6_PMCR_EVT_COUNT0_MASK;
  889. evt = (hwc->config_base << ARMV6_PMCR_EVT_COUNT0_SHIFT) |
  890. ARMV6_PMCR_COUNT0_IEN;
  891. } else if (ARMV6_COUNTER1 == idx) {
  892. mask = ARMV6_PMCR_EVT_COUNT1_MASK;
  893. evt = (hwc->config_base << ARMV6_PMCR_EVT_COUNT1_SHIFT) |
  894. ARMV6_PMCR_COUNT1_IEN;
  895. } else {
  896. WARN_ONCE(1, "invalid counter number (%d)\n", idx);
  897. return;
  898. }
  899. /*
  900. * Mask out the current event and set the counter to count the event
  901. * that we're interested in.
  902. */
  903. spin_lock_irqsave(&pmu_lock, flags);
  904. val = armv6_pmcr_read();
  905. val &= ~mask;
  906. val |= evt;
  907. armv6_pmcr_write(val);
  908. spin_unlock_irqrestore(&pmu_lock, flags);
  909. }
  910. static irqreturn_t
  911. armv6pmu_handle_irq(int irq_num,
  912. void *dev)
  913. {
  914. unsigned long pmcr = armv6_pmcr_read();
  915. struct perf_sample_data data;
  916. struct cpu_hw_events *cpuc;
  917. struct pt_regs *regs;
  918. int idx;
  919. if (!armv6_pmcr_has_overflowed(pmcr))
  920. return IRQ_NONE;
  921. regs = get_irq_regs();
  922. /*
  923. * The interrupts are cleared by writing the overflow flags back to
  924. * the control register. All of the other bits don't have any effect
  925. * if they are rewritten, so write the whole value back.
  926. */
  927. armv6_pmcr_write(pmcr);
  928. perf_sample_data_init(&data, 0);
  929. cpuc = &__get_cpu_var(cpu_hw_events);
  930. for (idx = 0; idx <= armpmu->num_events; ++idx) {
  931. struct perf_event *event = cpuc->events[idx];
  932. struct hw_perf_event *hwc;
  933. if (!test_bit(idx, cpuc->active_mask))
  934. continue;
  935. /*
  936. * We have a single interrupt for all counters. Check that
  937. * each counter has overflowed before we process it.
  938. */
  939. if (!armv6_pmcr_counter_has_overflowed(pmcr, idx))
  940. continue;
  941. hwc = &event->hw;
  942. armpmu_event_update(event, hwc, idx);
  943. data.period = event->hw.last_period;
  944. if (!armpmu_event_set_period(event, hwc, idx))
  945. continue;
  946. if (perf_event_overflow(event, 0, &data, regs))
  947. armpmu->disable(hwc, idx);
  948. }
  949. /*
  950. * Handle the pending perf events.
  951. *
  952. * Note: this call *must* be run with interrupts disabled. For
  953. * platforms that can have the PMU interrupts raised as an NMI, this
  954. * will not work.
  955. */
  956. irq_work_run();
  957. return IRQ_HANDLED;
  958. }
  959. static void
  960. armv6pmu_start(void)
  961. {
  962. unsigned long flags, val;
  963. spin_lock_irqsave(&pmu_lock, flags);
  964. val = armv6_pmcr_read();
  965. val |= ARMV6_PMCR_ENABLE;
  966. armv6_pmcr_write(val);
  967. spin_unlock_irqrestore(&pmu_lock, flags);
  968. }
  969. static void
  970. armv6pmu_stop(void)
  971. {
  972. unsigned long flags, val;
  973. spin_lock_irqsave(&pmu_lock, flags);
  974. val = armv6_pmcr_read();
  975. val &= ~ARMV6_PMCR_ENABLE;
  976. armv6_pmcr_write(val);
  977. spin_unlock_irqrestore(&pmu_lock, flags);
  978. }
  979. static int
  980. armv6pmu_get_event_idx(struct cpu_hw_events *cpuc,
  981. struct hw_perf_event *event)
  982. {
  983. /* Always place a cycle counter into the cycle counter. */
  984. if (ARMV6_PERFCTR_CPU_CYCLES == event->config_base) {
  985. if (test_and_set_bit(ARMV6_CYCLE_COUNTER, cpuc->used_mask))
  986. return -EAGAIN;
  987. return ARMV6_CYCLE_COUNTER;
  988. } else {
  989. /*
  990. * For anything other than a cycle counter, try and use
  991. * counter0 and counter1.
  992. */
  993. if (!test_and_set_bit(ARMV6_COUNTER1, cpuc->used_mask)) {
  994. return ARMV6_COUNTER1;
  995. }
  996. if (!test_and_set_bit(ARMV6_COUNTER0, cpuc->used_mask)) {
  997. return ARMV6_COUNTER0;
  998. }
  999. /* The counters are all in use. */
  1000. return -EAGAIN;
  1001. }
  1002. }
  1003. static void
  1004. armv6pmu_disable_event(struct hw_perf_event *hwc,
  1005. int idx)
  1006. {
  1007. unsigned long val, mask, evt, flags;
  1008. if (ARMV6_CYCLE_COUNTER == idx) {
  1009. mask = ARMV6_PMCR_CCOUNT_IEN;
  1010. evt = 0;
  1011. } else if (ARMV6_COUNTER0 == idx) {
  1012. mask = ARMV6_PMCR_COUNT0_IEN | ARMV6_PMCR_EVT_COUNT0_MASK;
  1013. evt = ARMV6_PERFCTR_NOP << ARMV6_PMCR_EVT_COUNT0_SHIFT;
  1014. } else if (ARMV6_COUNTER1 == idx) {
  1015. mask = ARMV6_PMCR_COUNT1_IEN | ARMV6_PMCR_EVT_COUNT1_MASK;
  1016. evt = ARMV6_PERFCTR_NOP << ARMV6_PMCR_EVT_COUNT1_SHIFT;
  1017. } else {
  1018. WARN_ONCE(1, "invalid counter number (%d)\n", idx);
  1019. return;
  1020. }
  1021. /*
  1022. * Mask out the current event and set the counter to count the number
  1023. * of ETM bus signal assertion cycles. The external reporting should
  1024. * be disabled and so this should never increment.
  1025. */
  1026. spin_lock_irqsave(&pmu_lock, flags);
  1027. val = armv6_pmcr_read();
  1028. val &= ~mask;
  1029. val |= evt;
  1030. armv6_pmcr_write(val);
  1031. spin_unlock_irqrestore(&pmu_lock, flags);
  1032. }
  1033. static void
  1034. armv6mpcore_pmu_disable_event(struct hw_perf_event *hwc,
  1035. int idx)
  1036. {
  1037. unsigned long val, mask, flags, evt = 0;
  1038. if (ARMV6_CYCLE_COUNTER == idx) {
  1039. mask = ARMV6_PMCR_CCOUNT_IEN;
  1040. } else if (ARMV6_COUNTER0 == idx) {
  1041. mask = ARMV6_PMCR_COUNT0_IEN;
  1042. } else if (ARMV6_COUNTER1 == idx) {
  1043. mask = ARMV6_PMCR_COUNT1_IEN;
  1044. } else {
  1045. WARN_ONCE(1, "invalid counter number (%d)\n", idx);
  1046. return;
  1047. }
  1048. /*
  1049. * Unlike UP ARMv6, we don't have a way of stopping the counters. We
  1050. * simply disable the interrupt reporting.
  1051. */
  1052. spin_lock_irqsave(&pmu_lock, flags);
  1053. val = armv6_pmcr_read();
  1054. val &= ~mask;
  1055. val |= evt;
  1056. armv6_pmcr_write(val);
  1057. spin_unlock_irqrestore(&pmu_lock, flags);
  1058. }
  1059. static const struct arm_pmu armv6pmu = {
  1060. .id = ARM_PERF_PMU_ID_V6,
  1061. .handle_irq = armv6pmu_handle_irq,
  1062. .enable = armv6pmu_enable_event,
  1063. .disable = armv6pmu_disable_event,
  1064. .read_counter = armv6pmu_read_counter,
  1065. .write_counter = armv6pmu_write_counter,
  1066. .get_event_idx = armv6pmu_get_event_idx,
  1067. .start = armv6pmu_start,
  1068. .stop = armv6pmu_stop,
  1069. .cache_map = &armv6_perf_cache_map,
  1070. .event_map = &armv6_perf_map,
  1071. .raw_event_mask = 0xFF,
  1072. .num_events = 3,
  1073. .max_period = (1LLU << 32) - 1,
  1074. };
  1075. /*
  1076. * ARMv6mpcore is almost identical to single core ARMv6 with the exception
  1077. * that some of the events have different enumerations and that there is no
  1078. * *hack* to stop the programmable counters. To stop the counters we simply
  1079. * disable the interrupt reporting and update the event. When unthrottling we
  1080. * reset the period and enable the interrupt reporting.
  1081. */
  1082. static const struct arm_pmu armv6mpcore_pmu = {
  1083. .id = ARM_PERF_PMU_ID_V6MP,
  1084. .handle_irq = armv6pmu_handle_irq,
  1085. .enable = armv6pmu_enable_event,
  1086. .disable = armv6mpcore_pmu_disable_event,
  1087. .read_counter = armv6pmu_read_counter,
  1088. .write_counter = armv6pmu_write_counter,
  1089. .get_event_idx = armv6pmu_get_event_idx,
  1090. .start = armv6pmu_start,
  1091. .stop = armv6pmu_stop,
  1092. .cache_map = &armv6mpcore_perf_cache_map,
  1093. .event_map = &armv6mpcore_perf_map,
  1094. .raw_event_mask = 0xFF,
  1095. .num_events = 3,
  1096. .max_period = (1LLU << 32) - 1,
  1097. };
  1098. /*
  1099. * ARMv7 Cortex-A8 and Cortex-A9 Performance Events handling code.
  1100. *
  1101. * Copied from ARMv6 code, with the low level code inspired
  1102. * by the ARMv7 Oprofile code.
  1103. *
  1104. * Cortex-A8 has up to 4 configurable performance counters and
  1105. * a single cycle counter.
  1106. * Cortex-A9 has up to 31 configurable performance counters and
  1107. * a single cycle counter.
  1108. *
  1109. * All counters can be enabled/disabled and IRQ masked separately. The cycle
  1110. * counter and all 4 performance counters together can be reset separately.
  1111. */
  1112. /* Common ARMv7 event types */
  1113. enum armv7_perf_types {
  1114. ARMV7_PERFCTR_PMNC_SW_INCR = 0x00,
  1115. ARMV7_PERFCTR_IFETCH_MISS = 0x01,
  1116. ARMV7_PERFCTR_ITLB_MISS = 0x02,
  1117. ARMV7_PERFCTR_DCACHE_REFILL = 0x03,
  1118. ARMV7_PERFCTR_DCACHE_ACCESS = 0x04,
  1119. ARMV7_PERFCTR_DTLB_REFILL = 0x05,
  1120. ARMV7_PERFCTR_DREAD = 0x06,
  1121. ARMV7_PERFCTR_DWRITE = 0x07,
  1122. ARMV7_PERFCTR_EXC_TAKEN = 0x09,
  1123. ARMV7_PERFCTR_EXC_EXECUTED = 0x0A,
  1124. ARMV7_PERFCTR_CID_WRITE = 0x0B,
  1125. /* ARMV7_PERFCTR_PC_WRITE is equivalent to HW_BRANCH_INSTRUCTIONS.
  1126. * It counts:
  1127. * - all branch instructions,
  1128. * - instructions that explicitly write the PC,
  1129. * - exception generating instructions.
  1130. */
  1131. ARMV7_PERFCTR_PC_WRITE = 0x0C,
  1132. ARMV7_PERFCTR_PC_IMM_BRANCH = 0x0D,
  1133. ARMV7_PERFCTR_UNALIGNED_ACCESS = 0x0F,
  1134. ARMV7_PERFCTR_PC_BRANCH_MIS_PRED = 0x10,
  1135. ARMV7_PERFCTR_CLOCK_CYCLES = 0x11,
  1136. ARMV7_PERFCTR_PC_BRANCH_MIS_USED = 0x12,
  1137. ARMV7_PERFCTR_CPU_CYCLES = 0xFF
  1138. };
  1139. /* ARMv7 Cortex-A8 specific event types */
  1140. enum armv7_a8_perf_types {
  1141. ARMV7_PERFCTR_INSTR_EXECUTED = 0x08,
  1142. ARMV7_PERFCTR_PC_PROC_RETURN = 0x0E,
  1143. ARMV7_PERFCTR_WRITE_BUFFER_FULL = 0x40,
  1144. ARMV7_PERFCTR_L2_STORE_MERGED = 0x41,
  1145. ARMV7_PERFCTR_L2_STORE_BUFF = 0x42,
  1146. ARMV7_PERFCTR_L2_ACCESS = 0x43,
  1147. ARMV7_PERFCTR_L2_CACH_MISS = 0x44,
  1148. ARMV7_PERFCTR_AXI_READ_CYCLES = 0x45,
  1149. ARMV7_PERFCTR_AXI_WRITE_CYCLES = 0x46,
  1150. ARMV7_PERFCTR_MEMORY_REPLAY = 0x47,
  1151. ARMV7_PERFCTR_UNALIGNED_ACCESS_REPLAY = 0x48,
  1152. ARMV7_PERFCTR_L1_DATA_MISS = 0x49,
  1153. ARMV7_PERFCTR_L1_INST_MISS = 0x4A,
  1154. ARMV7_PERFCTR_L1_DATA_COLORING = 0x4B,
  1155. ARMV7_PERFCTR_L1_NEON_DATA = 0x4C,
  1156. ARMV7_PERFCTR_L1_NEON_CACH_DATA = 0x4D,
  1157. ARMV7_PERFCTR_L2_NEON = 0x4E,
  1158. ARMV7_PERFCTR_L2_NEON_HIT = 0x4F,
  1159. ARMV7_PERFCTR_L1_INST = 0x50,
  1160. ARMV7_PERFCTR_PC_RETURN_MIS_PRED = 0x51,
  1161. ARMV7_PERFCTR_PC_BRANCH_FAILED = 0x52,
  1162. ARMV7_PERFCTR_PC_BRANCH_TAKEN = 0x53,
  1163. ARMV7_PERFCTR_PC_BRANCH_EXECUTED = 0x54,
  1164. ARMV7_PERFCTR_OP_EXECUTED = 0x55,
  1165. ARMV7_PERFCTR_CYCLES_INST_STALL = 0x56,
  1166. ARMV7_PERFCTR_CYCLES_INST = 0x57,
  1167. ARMV7_PERFCTR_CYCLES_NEON_DATA_STALL = 0x58,
  1168. ARMV7_PERFCTR_CYCLES_NEON_INST_STALL = 0x59,
  1169. ARMV7_PERFCTR_NEON_CYCLES = 0x5A,
  1170. ARMV7_PERFCTR_PMU0_EVENTS = 0x70,
  1171. ARMV7_PERFCTR_PMU1_EVENTS = 0x71,
  1172. ARMV7_PERFCTR_PMU_EVENTS = 0x72,
  1173. };
  1174. /* ARMv7 Cortex-A9 specific event types */
  1175. enum armv7_a9_perf_types {
  1176. ARMV7_PERFCTR_JAVA_HW_BYTECODE_EXEC = 0x40,
  1177. ARMV7_PERFCTR_JAVA_SW_BYTECODE_EXEC = 0x41,
  1178. ARMV7_PERFCTR_JAZELLE_BRANCH_EXEC = 0x42,
  1179. ARMV7_PERFCTR_COHERENT_LINE_MISS = 0x50,
  1180. ARMV7_PERFCTR_COHERENT_LINE_HIT = 0x51,
  1181. ARMV7_PERFCTR_ICACHE_DEP_STALL_CYCLES = 0x60,
  1182. ARMV7_PERFCTR_DCACHE_DEP_STALL_CYCLES = 0x61,
  1183. ARMV7_PERFCTR_TLB_MISS_DEP_STALL_CYCLES = 0x62,
  1184. ARMV7_PERFCTR_STREX_EXECUTED_PASSED = 0x63,
  1185. ARMV7_PERFCTR_STREX_EXECUTED_FAILED = 0x64,
  1186. ARMV7_PERFCTR_DATA_EVICTION = 0x65,
  1187. ARMV7_PERFCTR_ISSUE_STAGE_NO_INST = 0x66,
  1188. ARMV7_PERFCTR_ISSUE_STAGE_EMPTY = 0x67,
  1189. ARMV7_PERFCTR_INST_OUT_OF_RENAME_STAGE = 0x68,
  1190. ARMV7_PERFCTR_PREDICTABLE_FUNCT_RETURNS = 0x6E,
  1191. ARMV7_PERFCTR_MAIN_UNIT_EXECUTED_INST = 0x70,
  1192. ARMV7_PERFCTR_SECOND_UNIT_EXECUTED_INST = 0x71,
  1193. ARMV7_PERFCTR_LD_ST_UNIT_EXECUTED_INST = 0x72,
  1194. ARMV7_PERFCTR_FP_EXECUTED_INST = 0x73,
  1195. ARMV7_PERFCTR_NEON_EXECUTED_INST = 0x74,
  1196. ARMV7_PERFCTR_PLD_FULL_DEP_STALL_CYCLES = 0x80,
  1197. ARMV7_PERFCTR_DATA_WR_DEP_STALL_CYCLES = 0x81,
  1198. ARMV7_PERFCTR_ITLB_MISS_DEP_STALL_CYCLES = 0x82,
  1199. ARMV7_PERFCTR_DTLB_MISS_DEP_STALL_CYCLES = 0x83,
  1200. ARMV7_PERFCTR_MICRO_ITLB_MISS_DEP_STALL_CYCLES = 0x84,
  1201. ARMV7_PERFCTR_MICRO_DTLB_MISS_DEP_STALL_CYCLES = 0x85,
  1202. ARMV7_PERFCTR_DMB_DEP_STALL_CYCLES = 0x86,
  1203. ARMV7_PERFCTR_INTGR_CLK_ENABLED_CYCLES = 0x8A,
  1204. ARMV7_PERFCTR_DATA_ENGINE_CLK_EN_CYCLES = 0x8B,
  1205. ARMV7_PERFCTR_ISB_INST = 0x90,
  1206. ARMV7_PERFCTR_DSB_INST = 0x91,
  1207. ARMV7_PERFCTR_DMB_INST = 0x92,
  1208. ARMV7_PERFCTR_EXT_INTERRUPTS = 0x93,
  1209. ARMV7_PERFCTR_PLE_CACHE_LINE_RQST_COMPLETED = 0xA0,
  1210. ARMV7_PERFCTR_PLE_CACHE_LINE_RQST_SKIPPED = 0xA1,
  1211. ARMV7_PERFCTR_PLE_FIFO_FLUSH = 0xA2,
  1212. ARMV7_PERFCTR_PLE_RQST_COMPLETED = 0xA3,
  1213. ARMV7_PERFCTR_PLE_FIFO_OVERFLOW = 0xA4,
  1214. ARMV7_PERFCTR_PLE_RQST_PROG = 0xA5
  1215. };
  1216. /*
  1217. * Cortex-A8 HW events mapping
  1218. *
  1219. * The hardware events that we support. We do support cache operations but
  1220. * we have harvard caches and no way to combine instruction and data
  1221. * accesses/misses in hardware.
  1222. */
  1223. static const unsigned armv7_a8_perf_map[PERF_COUNT_HW_MAX] = {
  1224. [PERF_COUNT_HW_CPU_CYCLES] = ARMV7_PERFCTR_CPU_CYCLES,
  1225. [PERF_COUNT_HW_INSTRUCTIONS] = ARMV7_PERFCTR_INSTR_EXECUTED,
  1226. [PERF_COUNT_HW_CACHE_REFERENCES] = HW_OP_UNSUPPORTED,
  1227. [PERF_COUNT_HW_CACHE_MISSES] = HW_OP_UNSUPPORTED,
  1228. [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = ARMV7_PERFCTR_PC_WRITE,
  1229. [PERF_COUNT_HW_BRANCH_MISSES] = ARMV7_PERFCTR_PC_BRANCH_MIS_PRED,
  1230. [PERF_COUNT_HW_BUS_CYCLES] = ARMV7_PERFCTR_CLOCK_CYCLES,
  1231. };
  1232. static const unsigned armv7_a8_perf_cache_map[PERF_COUNT_HW_CACHE_MAX]
  1233. [PERF_COUNT_HW_CACHE_OP_MAX]
  1234. [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
  1235. [C(L1D)] = {
  1236. /*
  1237. * The performance counters don't differentiate between read
  1238. * and write accesses/misses so this isn't strictly correct,
  1239. * but it's the best we can do. Writes and reads get
  1240. * combined.
  1241. */
  1242. [C(OP_READ)] = {
  1243. [C(RESULT_ACCESS)] = ARMV7_PERFCTR_DCACHE_ACCESS,
  1244. [C(RESULT_MISS)] = ARMV7_PERFCTR_DCACHE_REFILL,
  1245. },
  1246. [C(OP_WRITE)] = {
  1247. [C(RESULT_ACCESS)] = ARMV7_PERFCTR_DCACHE_ACCESS,
  1248. [C(RESULT_MISS)] = ARMV7_PERFCTR_DCACHE_REFILL,
  1249. },
  1250. [C(OP_PREFETCH)] = {
  1251. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  1252. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  1253. },
  1254. },
  1255. [C(L1I)] = {
  1256. [C(OP_READ)] = {
  1257. [C(RESULT_ACCESS)] = ARMV7_PERFCTR_L1_INST,
  1258. [C(RESULT_MISS)] = ARMV7_PERFCTR_L1_INST_MISS,
  1259. },
  1260. [C(OP_WRITE)] = {
  1261. [C(RESULT_ACCESS)] = ARMV7_PERFCTR_L1_INST,
  1262. [C(RESULT_MISS)] = ARMV7_PERFCTR_L1_INST_MISS,
  1263. },
  1264. [C(OP_PREFETCH)] = {
  1265. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  1266. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  1267. },
  1268. },
  1269. [C(LL)] = {
  1270. [C(OP_READ)] = {
  1271. [C(RESULT_ACCESS)] = ARMV7_PERFCTR_L2_ACCESS,
  1272. [C(RESULT_MISS)] = ARMV7_PERFCTR_L2_CACH_MISS,
  1273. },
  1274. [C(OP_WRITE)] = {
  1275. [C(RESULT_ACCESS)] = ARMV7_PERFCTR_L2_ACCESS,
  1276. [C(RESULT_MISS)] = ARMV7_PERFCTR_L2_CACH_MISS,
  1277. },
  1278. [C(OP_PREFETCH)] = {
  1279. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  1280. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  1281. },
  1282. },
  1283. [C(DTLB)] = {
  1284. /*
  1285. * Only ITLB misses and DTLB refills are supported.
  1286. * If users want the DTLB refills misses a raw counter
  1287. * must be used.
  1288. */
  1289. [C(OP_READ)] = {
  1290. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  1291. [C(RESULT_MISS)] = ARMV7_PERFCTR_DTLB_REFILL,
  1292. },
  1293. [C(OP_WRITE)] = {
  1294. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  1295. [C(RESULT_MISS)] = ARMV7_PERFCTR_DTLB_REFILL,
  1296. },
  1297. [C(OP_PREFETCH)] = {
  1298. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  1299. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  1300. },
  1301. },
  1302. [C(ITLB)] = {
  1303. [C(OP_READ)] = {
  1304. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  1305. [C(RESULT_MISS)] = ARMV7_PERFCTR_ITLB_MISS,
  1306. },
  1307. [C(OP_WRITE)] = {
  1308. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  1309. [C(RESULT_MISS)] = ARMV7_PERFCTR_ITLB_MISS,
  1310. },
  1311. [C(OP_PREFETCH)] = {
  1312. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  1313. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  1314. },
  1315. },
  1316. [C(BPU)] = {
  1317. [C(OP_READ)] = {
  1318. [C(RESULT_ACCESS)] = ARMV7_PERFCTR_PC_WRITE,
  1319. [C(RESULT_MISS)]
  1320. = ARMV7_PERFCTR_PC_BRANCH_MIS_PRED,
  1321. },
  1322. [C(OP_WRITE)] = {
  1323. [C(RESULT_ACCESS)] = ARMV7_PERFCTR_PC_WRITE,
  1324. [C(RESULT_MISS)]
  1325. = ARMV7_PERFCTR_PC_BRANCH_MIS_PRED,
  1326. },
  1327. [C(OP_PREFETCH)] = {
  1328. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  1329. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  1330. },
  1331. },
  1332. };
  1333. /*
  1334. * Cortex-A9 HW events mapping
  1335. */
  1336. static const unsigned armv7_a9_perf_map[PERF_COUNT_HW_MAX] = {
  1337. [PERF_COUNT_HW_CPU_CYCLES] = ARMV7_PERFCTR_CPU_CYCLES,
  1338. [PERF_COUNT_HW_INSTRUCTIONS] =
  1339. ARMV7_PERFCTR_INST_OUT_OF_RENAME_STAGE,
  1340. [PERF_COUNT_HW_CACHE_REFERENCES] = ARMV7_PERFCTR_COHERENT_LINE_HIT,
  1341. [PERF_COUNT_HW_CACHE_MISSES] = ARMV7_PERFCTR_COHERENT_LINE_MISS,
  1342. [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = ARMV7_PERFCTR_PC_WRITE,
  1343. [PERF_COUNT_HW_BRANCH_MISSES] = ARMV7_PERFCTR_PC_BRANCH_MIS_PRED,
  1344. [PERF_COUNT_HW_BUS_CYCLES] = ARMV7_PERFCTR_CLOCK_CYCLES,
  1345. };
  1346. static const unsigned armv7_a9_perf_cache_map[PERF_COUNT_HW_CACHE_MAX]
  1347. [PERF_COUNT_HW_CACHE_OP_MAX]
  1348. [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
  1349. [C(L1D)] = {
  1350. /*
  1351. * The performance counters don't differentiate between read
  1352. * and write accesses/misses so this isn't strictly correct,
  1353. * but it's the best we can do. Writes and reads get
  1354. * combined.
  1355. */
  1356. [C(OP_READ)] = {
  1357. [C(RESULT_ACCESS)] = ARMV7_PERFCTR_DCACHE_ACCESS,
  1358. [C(RESULT_MISS)] = ARMV7_PERFCTR_DCACHE_REFILL,
  1359. },
  1360. [C(OP_WRITE)] = {
  1361. [C(RESULT_ACCESS)] = ARMV7_PERFCTR_DCACHE_ACCESS,
  1362. [C(RESULT_MISS)] = ARMV7_PERFCTR_DCACHE_REFILL,
  1363. },
  1364. [C(OP_PREFETCH)] = {
  1365. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  1366. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  1367. },
  1368. },
  1369. [C(L1I)] = {
  1370. [C(OP_READ)] = {
  1371. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  1372. [C(RESULT_MISS)] = ARMV7_PERFCTR_IFETCH_MISS,
  1373. },
  1374. [C(OP_WRITE)] = {
  1375. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  1376. [C(RESULT_MISS)] = ARMV7_PERFCTR_IFETCH_MISS,
  1377. },
  1378. [C(OP_PREFETCH)] = {
  1379. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  1380. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  1381. },
  1382. },
  1383. [C(LL)] = {
  1384. [C(OP_READ)] = {
  1385. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  1386. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  1387. },
  1388. [C(OP_WRITE)] = {
  1389. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  1390. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  1391. },
  1392. [C(OP_PREFETCH)] = {
  1393. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  1394. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  1395. },
  1396. },
  1397. [C(DTLB)] = {
  1398. /*
  1399. * Only ITLB misses and DTLB refills are supported.
  1400. * If users want the DTLB refills misses a raw counter
  1401. * must be used.
  1402. */
  1403. [C(OP_READ)] = {
  1404. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  1405. [C(RESULT_MISS)] = ARMV7_PERFCTR_DTLB_REFILL,
  1406. },
  1407. [C(OP_WRITE)] = {
  1408. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  1409. [C(RESULT_MISS)] = ARMV7_PERFCTR_DTLB_REFILL,
  1410. },
  1411. [C(OP_PREFETCH)] = {
  1412. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  1413. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  1414. },
  1415. },
  1416. [C(ITLB)] = {
  1417. [C(OP_READ)] = {
  1418. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  1419. [C(RESULT_MISS)] = ARMV7_PERFCTR_ITLB_MISS,
  1420. },
  1421. [C(OP_WRITE)] = {
  1422. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  1423. [C(RESULT_MISS)] = ARMV7_PERFCTR_ITLB_MISS,
  1424. },
  1425. [C(OP_PREFETCH)] = {
  1426. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  1427. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  1428. },
  1429. },
  1430. [C(BPU)] = {
  1431. [C(OP_READ)] = {
  1432. [C(RESULT_ACCESS)] = ARMV7_PERFCTR_PC_WRITE,
  1433. [C(RESULT_MISS)]
  1434. = ARMV7_PERFCTR_PC_BRANCH_MIS_PRED,
  1435. },
  1436. [C(OP_WRITE)] = {
  1437. [C(RESULT_ACCESS)] = ARMV7_PERFCTR_PC_WRITE,
  1438. [C(RESULT_MISS)]
  1439. = ARMV7_PERFCTR_PC_BRANCH_MIS_PRED,
  1440. },
  1441. [C(OP_PREFETCH)] = {
  1442. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  1443. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  1444. },
  1445. },
  1446. };
  1447. /*
  1448. * Perf Events counters
  1449. */
  1450. enum armv7_counters {
  1451. ARMV7_CYCLE_COUNTER = 1, /* Cycle counter */
  1452. ARMV7_COUNTER0 = 2, /* First event counter */
  1453. };
  1454. /*
  1455. * The cycle counter is ARMV7_CYCLE_COUNTER.
  1456. * The first event counter is ARMV7_COUNTER0.
  1457. * The last event counter is (ARMV7_COUNTER0 + armpmu->num_events - 1).
  1458. */
  1459. #define ARMV7_COUNTER_LAST (ARMV7_COUNTER0 + armpmu->num_events - 1)
  1460. /*
  1461. * ARMv7 low level PMNC access
  1462. */
  1463. /*
  1464. * Per-CPU PMNC: config reg
  1465. */
  1466. #define ARMV7_PMNC_E (1 << 0) /* Enable all counters */
  1467. #define ARMV7_PMNC_P (1 << 1) /* Reset all counters */
  1468. #define ARMV7_PMNC_C (1 << 2) /* Cycle counter reset */
  1469. #define ARMV7_PMNC_D (1 << 3) /* CCNT counts every 64th cpu cycle */
  1470. #define ARMV7_PMNC_X (1 << 4) /* Export to ETM */
  1471. #define ARMV7_PMNC_DP (1 << 5) /* Disable CCNT if non-invasive debug*/
  1472. #define ARMV7_PMNC_N_SHIFT 11 /* Number of counters supported */
  1473. #define ARMV7_PMNC_N_MASK 0x1f
  1474. #define ARMV7_PMNC_MASK 0x3f /* Mask for writable bits */
  1475. /*
  1476. * Available counters
  1477. */
  1478. #define ARMV7_CNT0 0 /* First event counter */
  1479. #define ARMV7_CCNT 31 /* Cycle counter */
  1480. /* Perf Event to low level counters mapping */
  1481. #define ARMV7_EVENT_CNT_TO_CNTx (ARMV7_COUNTER0 - ARMV7_CNT0)
  1482. /*
  1483. * CNTENS: counters enable reg
  1484. */
  1485. #define ARMV7_CNTENS_P(idx) (1 << (idx - ARMV7_EVENT_CNT_TO_CNTx))
  1486. #define ARMV7_CNTENS_C (1 << ARMV7_CCNT)
  1487. /*
  1488. * CNTENC: counters disable reg
  1489. */
  1490. #define ARMV7_CNTENC_P(idx) (1 << (idx - ARMV7_EVENT_CNT_TO_CNTx))
  1491. #define ARMV7_CNTENC_C (1 << ARMV7_CCNT)
  1492. /*
  1493. * INTENS: counters overflow interrupt enable reg
  1494. */
  1495. #define ARMV7_INTENS_P(idx) (1 << (idx - ARMV7_EVENT_CNT_TO_CNTx))
  1496. #define ARMV7_INTENS_C (1 << ARMV7_CCNT)
  1497. /*
  1498. * INTENC: counters overflow interrupt disable reg
  1499. */
  1500. #define ARMV7_INTENC_P(idx) (1 << (idx - ARMV7_EVENT_CNT_TO_CNTx))
  1501. #define ARMV7_INTENC_C (1 << ARMV7_CCNT)
  1502. /*
  1503. * EVTSEL: Event selection reg
  1504. */
  1505. #define ARMV7_EVTSEL_MASK 0xff /* Mask for writable bits */
  1506. /*
  1507. * SELECT: Counter selection reg
  1508. */
  1509. #define ARMV7_SELECT_MASK 0x1f /* Mask for writable bits */
  1510. /*
  1511. * FLAG: counters overflow flag status reg
  1512. */
  1513. #define ARMV7_FLAG_P(idx) (1 << (idx - ARMV7_EVENT_CNT_TO_CNTx))
  1514. #define ARMV7_FLAG_C (1 << ARMV7_CCNT)
  1515. #define ARMV7_FLAG_MASK 0xffffffff /* Mask for writable bits */
  1516. #define ARMV7_OVERFLOWED_MASK ARMV7_FLAG_MASK
  1517. static inline unsigned long armv7_pmnc_read(void)
  1518. {
  1519. u32 val;
  1520. asm volatile("mrc p15, 0, %0, c9, c12, 0" : "=r"(val));
  1521. return val;
  1522. }
  1523. static inline void armv7_pmnc_write(unsigned long val)
  1524. {
  1525. val &= ARMV7_PMNC_MASK;
  1526. asm volatile("mcr p15, 0, %0, c9, c12, 0" : : "r"(val));
  1527. }
  1528. static inline int armv7_pmnc_has_overflowed(unsigned long pmnc)
  1529. {
  1530. return pmnc & ARMV7_OVERFLOWED_MASK;
  1531. }
  1532. static inline int armv7_pmnc_counter_has_overflowed(unsigned long pmnc,
  1533. enum armv7_counters counter)
  1534. {
  1535. int ret = 0;
  1536. if (counter == ARMV7_CYCLE_COUNTER)
  1537. ret = pmnc & ARMV7_FLAG_C;
  1538. else if ((counter >= ARMV7_COUNTER0) && (counter <= ARMV7_COUNTER_LAST))
  1539. ret = pmnc & ARMV7_FLAG_P(counter);
  1540. else
  1541. pr_err("CPU%u checking wrong counter %d overflow status\n",
  1542. smp_processor_id(), counter);
  1543. return ret;
  1544. }
  1545. static inline int armv7_pmnc_select_counter(unsigned int idx)
  1546. {
  1547. u32 val;
  1548. if ((idx < ARMV7_COUNTER0) || (idx > ARMV7_COUNTER_LAST)) {
  1549. pr_err("CPU%u selecting wrong PMNC counter"
  1550. " %d\n", smp_processor_id(), idx);
  1551. return -1;
  1552. }
  1553. val = (idx - ARMV7_EVENT_CNT_TO_CNTx) & ARMV7_SELECT_MASK;
  1554. asm volatile("mcr p15, 0, %0, c9, c12, 5" : : "r" (val));
  1555. return idx;
  1556. }
  1557. static inline u32 armv7pmu_read_counter(int idx)
  1558. {
  1559. unsigned long value = 0;
  1560. if (idx == ARMV7_CYCLE_COUNTER)
  1561. asm volatile("mrc p15, 0, %0, c9, c13, 0" : "=r" (value));
  1562. else if ((idx >= ARMV7_COUNTER0) && (idx <= ARMV7_COUNTER_LAST)) {
  1563. if (armv7_pmnc_select_counter(idx) == idx)
  1564. asm volatile("mrc p15, 0, %0, c9, c13, 2"
  1565. : "=r" (value));
  1566. } else
  1567. pr_err("CPU%u reading wrong counter %d\n",
  1568. smp_processor_id(), idx);
  1569. return value;
  1570. }
  1571. static inline void armv7pmu_write_counter(int idx, u32 value)
  1572. {
  1573. if (idx == ARMV7_CYCLE_COUNTER)
  1574. asm volatile("mcr p15, 0, %0, c9, c13, 0" : : "r" (value));
  1575. else if ((idx >= ARMV7_COUNTER0) && (idx <= ARMV7_COUNTER_LAST)) {
  1576. if (armv7_pmnc_select_counter(idx) == idx)
  1577. asm volatile("mcr p15, 0, %0, c9, c13, 2"
  1578. : : "r" (value));
  1579. } else
  1580. pr_err("CPU%u writing wrong counter %d\n",
  1581. smp_processor_id(), idx);
  1582. }
  1583. static inline void armv7_pmnc_write_evtsel(unsigned int idx, u32 val)
  1584. {
  1585. if (armv7_pmnc_select_counter(idx) == idx) {
  1586. val &= ARMV7_EVTSEL_MASK;
  1587. asm volatile("mcr p15, 0, %0, c9, c13, 1" : : "r" (val));
  1588. }
  1589. }
  1590. static inline u32 armv7_pmnc_enable_counter(unsigned int idx)
  1591. {
  1592. u32 val;
  1593. if ((idx != ARMV7_CYCLE_COUNTER) &&
  1594. ((idx < ARMV7_COUNTER0) || (idx > ARMV7_COUNTER_LAST))) {
  1595. pr_err("CPU%u enabling wrong PMNC counter"
  1596. " %d\n", smp_processor_id(), idx);
  1597. return -1;
  1598. }
  1599. if (idx == ARMV7_CYCLE_COUNTER)
  1600. val = ARMV7_CNTENS_C;
  1601. else
  1602. val = ARMV7_CNTENS_P(idx);
  1603. asm volatile("mcr p15, 0, %0, c9, c12, 1" : : "r" (val));
  1604. return idx;
  1605. }
  1606. static inline u32 armv7_pmnc_disable_counter(unsigned int idx)
  1607. {
  1608. u32 val;
  1609. if ((idx != ARMV7_CYCLE_COUNTER) &&
  1610. ((idx < ARMV7_COUNTER0) || (idx > ARMV7_COUNTER_LAST))) {
  1611. pr_err("CPU%u disabling wrong PMNC counter"
  1612. " %d\n", smp_processor_id(), idx);
  1613. return -1;
  1614. }
  1615. if (idx == ARMV7_CYCLE_COUNTER)
  1616. val = ARMV7_CNTENC_C;
  1617. else
  1618. val = ARMV7_CNTENC_P(idx);
  1619. asm volatile("mcr p15, 0, %0, c9, c12, 2" : : "r" (val));
  1620. return idx;
  1621. }
  1622. static inline u32 armv7_pmnc_enable_intens(unsigned int idx)
  1623. {
  1624. u32 val;
  1625. if ((idx != ARMV7_CYCLE_COUNTER) &&
  1626. ((idx < ARMV7_COUNTER0) || (idx > ARMV7_COUNTER_LAST))) {
  1627. pr_err("CPU%u enabling wrong PMNC counter"
  1628. " interrupt enable %d\n", smp_processor_id(), idx);
  1629. return -1;
  1630. }
  1631. if (idx == ARMV7_CYCLE_COUNTER)
  1632. val = ARMV7_INTENS_C;
  1633. else
  1634. val = ARMV7_INTENS_P(idx);
  1635. asm volatile("mcr p15, 0, %0, c9, c14, 1" : : "r" (val));
  1636. return idx;
  1637. }
  1638. static inline u32 armv7_pmnc_disable_intens(unsigned int idx)
  1639. {
  1640. u32 val;
  1641. if ((idx != ARMV7_CYCLE_COUNTER) &&
  1642. ((idx < ARMV7_COUNTER0) || (idx > ARMV7_COUNTER_LAST))) {
  1643. pr_err("CPU%u disabling wrong PMNC counter"
  1644. " interrupt enable %d\n", smp_processor_id(), idx);
  1645. return -1;
  1646. }
  1647. if (idx == ARMV7_CYCLE_COUNTER)
  1648. val = ARMV7_INTENC_C;
  1649. else
  1650. val = ARMV7_INTENC_P(idx);
  1651. asm volatile("mcr p15, 0, %0, c9, c14, 2" : : "r" (val));
  1652. return idx;
  1653. }
  1654. static inline u32 armv7_pmnc_getreset_flags(void)
  1655. {
  1656. u32 val;
  1657. /* Read */
  1658. asm volatile("mrc p15, 0, %0, c9, c12, 3" : "=r" (val));
  1659. /* Write to clear flags */
  1660. val &= ARMV7_FLAG_MASK;
  1661. asm volatile("mcr p15, 0, %0, c9, c12, 3" : : "r" (val));
  1662. return val;
  1663. }
  1664. #ifdef DEBUG
  1665. static void armv7_pmnc_dump_regs(void)
  1666. {
  1667. u32 val;
  1668. unsigned int cnt;
  1669. printk(KERN_INFO "PMNC registers dump:\n");
  1670. asm volatile("mrc p15, 0, %0, c9, c12, 0" : "=r" (val));
  1671. printk(KERN_INFO "PMNC =0x%08x\n", val);
  1672. asm volatile("mrc p15, 0, %0, c9, c12, 1" : "=r" (val));
  1673. printk(KERN_INFO "CNTENS=0x%08x\n", val);
  1674. asm volatile("mrc p15, 0, %0, c9, c14, 1" : "=r" (val));
  1675. printk(KERN_INFO "INTENS=0x%08x\n", val);
  1676. asm volatile("mrc p15, 0, %0, c9, c12, 3" : "=r" (val));
  1677. printk(KERN_INFO "FLAGS =0x%08x\n", val);
  1678. asm volatile("mrc p15, 0, %0, c9, c12, 5" : "=r" (val));
  1679. printk(KERN_INFO "SELECT=0x%08x\n", val);
  1680. asm volatile("mrc p15, 0, %0, c9, c13, 0" : "=r" (val));
  1681. printk(KERN_INFO "CCNT =0x%08x\n", val);
  1682. for (cnt = ARMV7_COUNTER0; cnt < ARMV7_COUNTER_LAST; cnt++) {
  1683. armv7_pmnc_select_counter(cnt);
  1684. asm volatile("mrc p15, 0, %0, c9, c13, 2" : "=r" (val));
  1685. printk(KERN_INFO "CNT[%d] count =0x%08x\n",
  1686. cnt-ARMV7_EVENT_CNT_TO_CNTx, val);
  1687. asm volatile("mrc p15, 0, %0, c9, c13, 1" : "=r" (val));
  1688. printk(KERN_INFO "CNT[%d] evtsel=0x%08x\n",
  1689. cnt-ARMV7_EVENT_CNT_TO_CNTx, val);
  1690. }
  1691. }
  1692. #endif
  1693. void armv7pmu_enable_event(struct hw_perf_event *hwc, int idx)
  1694. {
  1695. unsigned long flags;
  1696. /*
  1697. * Enable counter and interrupt, and set the counter to count
  1698. * the event that we're interested in.
  1699. */
  1700. spin_lock_irqsave(&pmu_lock, flags);
  1701. /*
  1702. * Disable counter
  1703. */
  1704. armv7_pmnc_disable_counter(idx);
  1705. /*
  1706. * Set event (if destined for PMNx counters)
  1707. * We don't need to set the event if it's a cycle count
  1708. */
  1709. if (idx != ARMV7_CYCLE_COUNTER)
  1710. armv7_pmnc_write_evtsel(idx, hwc->config_base);
  1711. /*
  1712. * Enable interrupt for this counter
  1713. */
  1714. armv7_pmnc_enable_intens(idx);
  1715. /*
  1716. * Enable counter
  1717. */
  1718. armv7_pmnc_enable_counter(idx);
  1719. spin_unlock_irqrestore(&pmu_lock, flags);
  1720. }
  1721. static void armv7pmu_disable_event(struct hw_perf_event *hwc, int idx)
  1722. {
  1723. unsigned long flags;
  1724. /*
  1725. * Disable counter and interrupt
  1726. */
  1727. spin_lock_irqsave(&pmu_lock, flags);
  1728. /*
  1729. * Disable counter
  1730. */
  1731. armv7_pmnc_disable_counter(idx);
  1732. /*
  1733. * Disable interrupt for this counter
  1734. */
  1735. armv7_pmnc_disable_intens(idx);
  1736. spin_unlock_irqrestore(&pmu_lock, flags);
  1737. }
  1738. static irqreturn_t armv7pmu_handle_irq(int irq_num, void *dev)
  1739. {
  1740. unsigned long pmnc;
  1741. struct perf_sample_data data;
  1742. struct cpu_hw_events *cpuc;
  1743. struct pt_regs *regs;
  1744. int idx;
  1745. /*
  1746. * Get and reset the IRQ flags
  1747. */
  1748. pmnc = armv7_pmnc_getreset_flags();
  1749. /*
  1750. * Did an overflow occur?
  1751. */
  1752. if (!armv7_pmnc_has_overflowed(pmnc))
  1753. return IRQ_NONE;
  1754. /*
  1755. * Handle the counter(s) overflow(s)
  1756. */
  1757. regs = get_irq_regs();
  1758. perf_sample_data_init(&data, 0);
  1759. cpuc = &__get_cpu_var(cpu_hw_events);
  1760. for (idx = 0; idx <= armpmu->num_events; ++idx) {
  1761. struct perf_event *event = cpuc->events[idx];
  1762. struct hw_perf_event *hwc;
  1763. if (!test_bit(idx, cpuc->active_mask))
  1764. continue;
  1765. /*
  1766. * We have a single interrupt for all counters. Check that
  1767. * each counter has overflowed before we process it.
  1768. */
  1769. if (!armv7_pmnc_counter_has_overflowed(pmnc, idx))
  1770. continue;
  1771. hwc = &event->hw;
  1772. armpmu_event_update(event, hwc, idx);
  1773. data.period = event->hw.last_period;
  1774. if (!armpmu_event_set_period(event, hwc, idx))
  1775. continue;
  1776. if (perf_event_overflow(event, 0, &data, regs))
  1777. armpmu->disable(hwc, idx);
  1778. }
  1779. /*
  1780. * Handle the pending perf events.
  1781. *
  1782. * Note: this call *must* be run with interrupts disabled. For
  1783. * platforms that can have the PMU interrupts raised as an NMI, this
  1784. * will not work.
  1785. */
  1786. irq_work_run();
  1787. return IRQ_HANDLED;
  1788. }
  1789. static void armv7pmu_start(void)
  1790. {
  1791. unsigned long flags;
  1792. spin_lock_irqsave(&pmu_lock, flags);
  1793. /* Enable all counters */
  1794. armv7_pmnc_write(armv7_pmnc_read() | ARMV7_PMNC_E);
  1795. spin_unlock_irqrestore(&pmu_lock, flags);
  1796. }
  1797. static void armv7pmu_stop(void)
  1798. {
  1799. unsigned long flags;
  1800. spin_lock_irqsave(&pmu_lock, flags);
  1801. /* Disable all counters */
  1802. armv7_pmnc_write(armv7_pmnc_read() & ~ARMV7_PMNC_E);
  1803. spin_unlock_irqrestore(&pmu_lock, flags);
  1804. }
  1805. static int armv7pmu_get_event_idx(struct cpu_hw_events *cpuc,
  1806. struct hw_perf_event *event)
  1807. {
  1808. int idx;
  1809. /* Always place a cycle counter into the cycle counter. */
  1810. if (event->config_base == ARMV7_PERFCTR_CPU_CYCLES) {
  1811. if (test_and_set_bit(ARMV7_CYCLE_COUNTER, cpuc->used_mask))
  1812. return -EAGAIN;
  1813. return ARMV7_CYCLE_COUNTER;
  1814. } else {
  1815. /*
  1816. * For anything other than a cycle counter, try and use
  1817. * the events counters
  1818. */
  1819. for (idx = ARMV7_COUNTER0; idx <= armpmu->num_events; ++idx) {
  1820. if (!test_and_set_bit(idx, cpuc->used_mask))
  1821. return idx;
  1822. }
  1823. /* The counters are all in use. */
  1824. return -EAGAIN;
  1825. }
  1826. }
  1827. static struct arm_pmu armv7pmu = {
  1828. .handle_irq = armv7pmu_handle_irq,
  1829. .enable = armv7pmu_enable_event,
  1830. .disable = armv7pmu_disable_event,
  1831. .read_counter = armv7pmu_read_counter,
  1832. .write_counter = armv7pmu_write_counter,
  1833. .get_event_idx = armv7pmu_get_event_idx,
  1834. .start = armv7pmu_start,
  1835. .stop = armv7pmu_stop,
  1836. .raw_event_mask = 0xFF,
  1837. .max_period = (1LLU << 32) - 1,
  1838. };
  1839. static u32 __init armv7_reset_read_pmnc(void)
  1840. {
  1841. u32 nb_cnt;
  1842. /* Initialize & Reset PMNC: C and P bits */
  1843. armv7_pmnc_write(ARMV7_PMNC_P | ARMV7_PMNC_C);
  1844. /* Read the nb of CNTx counters supported from PMNC */
  1845. nb_cnt = (armv7_pmnc_read() >> ARMV7_PMNC_N_SHIFT) & ARMV7_PMNC_N_MASK;
  1846. /* Add the CPU cycles counter and return */
  1847. return nb_cnt + 1;
  1848. }
  1849. /*
  1850. * ARMv5 [xscale] Performance counter handling code.
  1851. *
  1852. * Based on xscale OProfile code.
  1853. *
  1854. * There are two variants of the xscale PMU that we support:
  1855. * - xscale1pmu: 2 event counters and a cycle counter
  1856. * - xscale2pmu: 4 event counters and a cycle counter
  1857. * The two variants share event definitions, but have different
  1858. * PMU structures.
  1859. */
  1860. enum xscale_perf_types {
  1861. XSCALE_PERFCTR_ICACHE_MISS = 0x00,
  1862. XSCALE_PERFCTR_ICACHE_NO_DELIVER = 0x01,
  1863. XSCALE_PERFCTR_DATA_STALL = 0x02,
  1864. XSCALE_PERFCTR_ITLB_MISS = 0x03,
  1865. XSCALE_PERFCTR_DTLB_MISS = 0x04,
  1866. XSCALE_PERFCTR_BRANCH = 0x05,
  1867. XSCALE_PERFCTR_BRANCH_MISS = 0x06,
  1868. XSCALE_PERFCTR_INSTRUCTION = 0x07,
  1869. XSCALE_PERFCTR_DCACHE_FULL_STALL = 0x08,
  1870. XSCALE_PERFCTR_DCACHE_FULL_STALL_CONTIG = 0x09,
  1871. XSCALE_PERFCTR_DCACHE_ACCESS = 0x0A,
  1872. XSCALE_PERFCTR_DCACHE_MISS = 0x0B,
  1873. XSCALE_PERFCTR_DCACHE_WRITE_BACK = 0x0C,
  1874. XSCALE_PERFCTR_PC_CHANGED = 0x0D,
  1875. XSCALE_PERFCTR_BCU_REQUEST = 0x10,
  1876. XSCALE_PERFCTR_BCU_FULL = 0x11,
  1877. XSCALE_PERFCTR_BCU_DRAIN = 0x12,
  1878. XSCALE_PERFCTR_BCU_ECC_NO_ELOG = 0x14,
  1879. XSCALE_PERFCTR_BCU_1_BIT_ERR = 0x15,
  1880. XSCALE_PERFCTR_RMW = 0x16,
  1881. /* XSCALE_PERFCTR_CCNT is not hardware defined */
  1882. XSCALE_PERFCTR_CCNT = 0xFE,
  1883. XSCALE_PERFCTR_UNUSED = 0xFF,
  1884. };
  1885. enum xscale_counters {
  1886. XSCALE_CYCLE_COUNTER = 1,
  1887. XSCALE_COUNTER0,
  1888. XSCALE_COUNTER1,
  1889. XSCALE_COUNTER2,
  1890. XSCALE_COUNTER3,
  1891. };
  1892. static const unsigned xscale_perf_map[PERF_COUNT_HW_MAX] = {
  1893. [PERF_COUNT_HW_CPU_CYCLES] = XSCALE_PERFCTR_CCNT,
  1894. [PERF_COUNT_HW_INSTRUCTIONS] = XSCALE_PERFCTR_INSTRUCTION,
  1895. [PERF_COUNT_HW_CACHE_REFERENCES] = HW_OP_UNSUPPORTED,
  1896. [PERF_COUNT_HW_CACHE_MISSES] = HW_OP_UNSUPPORTED,
  1897. [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = XSCALE_PERFCTR_BRANCH,
  1898. [PERF_COUNT_HW_BRANCH_MISSES] = XSCALE_PERFCTR_BRANCH_MISS,
  1899. [PERF_COUNT_HW_BUS_CYCLES] = HW_OP_UNSUPPORTED,
  1900. };
  1901. static const unsigned xscale_perf_cache_map[PERF_COUNT_HW_CACHE_MAX]
  1902. [PERF_COUNT_HW_CACHE_OP_MAX]
  1903. [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
  1904. [C(L1D)] = {
  1905. [C(OP_READ)] = {
  1906. [C(RESULT_ACCESS)] = XSCALE_PERFCTR_DCACHE_ACCESS,
  1907. [C(RESULT_MISS)] = XSCALE_PERFCTR_DCACHE_MISS,
  1908. },
  1909. [C(OP_WRITE)] = {
  1910. [C(RESULT_ACCESS)] = XSCALE_PERFCTR_DCACHE_ACCESS,
  1911. [C(RESULT_MISS)] = XSCALE_PERFCTR_DCACHE_MISS,
  1912. },
  1913. [C(OP_PREFETCH)] = {
  1914. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  1915. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  1916. },
  1917. },
  1918. [C(L1I)] = {
  1919. [C(OP_READ)] = {
  1920. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  1921. [C(RESULT_MISS)] = XSCALE_PERFCTR_ICACHE_MISS,
  1922. },
  1923. [C(OP_WRITE)] = {
  1924. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  1925. [C(RESULT_MISS)] = XSCALE_PERFCTR_ICACHE_MISS,
  1926. },
  1927. [C(OP_PREFETCH)] = {
  1928. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  1929. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  1930. },
  1931. },
  1932. [C(LL)] = {
  1933. [C(OP_READ)] = {
  1934. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  1935. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  1936. },
  1937. [C(OP_WRITE)] = {
  1938. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  1939. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  1940. },
  1941. [C(OP_PREFETCH)] = {
  1942. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  1943. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  1944. },
  1945. },
  1946. [C(DTLB)] = {
  1947. [C(OP_READ)] = {
  1948. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  1949. [C(RESULT_MISS)] = XSCALE_PERFCTR_DTLB_MISS,
  1950. },
  1951. [C(OP_WRITE)] = {
  1952. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  1953. [C(RESULT_MISS)] = XSCALE_PERFCTR_DTLB_MISS,
  1954. },
  1955. [C(OP_PREFETCH)] = {
  1956. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  1957. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  1958. },
  1959. },
  1960. [C(ITLB)] = {
  1961. [C(OP_READ)] = {
  1962. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  1963. [C(RESULT_MISS)] = XSCALE_PERFCTR_ITLB_MISS,
  1964. },
  1965. [C(OP_WRITE)] = {
  1966. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  1967. [C(RESULT_MISS)] = XSCALE_PERFCTR_ITLB_MISS,
  1968. },
  1969. [C(OP_PREFETCH)] = {
  1970. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  1971. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  1972. },
  1973. },
  1974. [C(BPU)] = {
  1975. [C(OP_READ)] = {
  1976. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  1977. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  1978. },
  1979. [C(OP_WRITE)] = {
  1980. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  1981. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  1982. },
  1983. [C(OP_PREFETCH)] = {
  1984. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  1985. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  1986. },
  1987. },
  1988. };
  1989. #define XSCALE_PMU_ENABLE 0x001
  1990. #define XSCALE_PMN_RESET 0x002
  1991. #define XSCALE_CCNT_RESET 0x004
  1992. #define XSCALE_PMU_RESET (CCNT_RESET | PMN_RESET)
  1993. #define XSCALE_PMU_CNT64 0x008
  1994. #define XSCALE1_OVERFLOWED_MASK 0x700
  1995. #define XSCALE1_CCOUNT_OVERFLOW 0x400
  1996. #define XSCALE1_COUNT0_OVERFLOW 0x100
  1997. #define XSCALE1_COUNT1_OVERFLOW 0x200
  1998. #define XSCALE1_CCOUNT_INT_EN 0x040
  1999. #define XSCALE1_COUNT0_INT_EN 0x010
  2000. #define XSCALE1_COUNT1_INT_EN 0x020
  2001. #define XSCALE1_COUNT0_EVT_SHFT 12
  2002. #define XSCALE1_COUNT0_EVT_MASK (0xff << XSCALE1_COUNT0_EVT_SHFT)
  2003. #define XSCALE1_COUNT1_EVT_SHFT 20
  2004. #define XSCALE1_COUNT1_EVT_MASK (0xff << XSCALE1_COUNT1_EVT_SHFT)
  2005. static inline u32
  2006. xscale1pmu_read_pmnc(void)
  2007. {
  2008. u32 val;
  2009. asm volatile("mrc p14, 0, %0, c0, c0, 0" : "=r" (val));
  2010. return val;
  2011. }
  2012. static inline void
  2013. xscale1pmu_write_pmnc(u32 val)
  2014. {
  2015. /* upper 4bits and 7, 11 are write-as-0 */
  2016. val &= 0xffff77f;
  2017. asm volatile("mcr p14, 0, %0, c0, c0, 0" : : "r" (val));
  2018. }
  2019. static inline int
  2020. xscale1_pmnc_counter_has_overflowed(unsigned long pmnc,
  2021. enum xscale_counters counter)
  2022. {
  2023. int ret = 0;
  2024. switch (counter) {
  2025. case XSCALE_CYCLE_COUNTER:
  2026. ret = pmnc & XSCALE1_CCOUNT_OVERFLOW;
  2027. break;
  2028. case XSCALE_COUNTER0:
  2029. ret = pmnc & XSCALE1_COUNT0_OVERFLOW;
  2030. break;
  2031. case XSCALE_COUNTER1:
  2032. ret = pmnc & XSCALE1_COUNT1_OVERFLOW;
  2033. break;
  2034. default:
  2035. WARN_ONCE(1, "invalid counter number (%d)\n", counter);
  2036. }
  2037. return ret;
  2038. }
  2039. static irqreturn_t
  2040. xscale1pmu_handle_irq(int irq_num, void *dev)
  2041. {
  2042. unsigned long pmnc;
  2043. struct perf_sample_data data;
  2044. struct cpu_hw_events *cpuc;
  2045. struct pt_regs *regs;
  2046. int idx;
  2047. /*
  2048. * NOTE: there's an A stepping erratum that states if an overflow
  2049. * bit already exists and another occurs, the previous
  2050. * Overflow bit gets cleared. There's no workaround.
  2051. * Fixed in B stepping or later.
  2052. */
  2053. pmnc = xscale1pmu_read_pmnc();
  2054. /*
  2055. * Write the value back to clear the overflow flags. Overflow
  2056. * flags remain in pmnc for use below. We also disable the PMU
  2057. * while we process the interrupt.
  2058. */
  2059. xscale1pmu_write_pmnc(pmnc & ~XSCALE_PMU_ENABLE);
  2060. if (!(pmnc & XSCALE1_OVERFLOWED_MASK))
  2061. return IRQ_NONE;
  2062. regs = get_irq_regs();
  2063. perf_sample_data_init(&data, 0);
  2064. cpuc = &__get_cpu_var(cpu_hw_events);
  2065. for (idx = 0; idx <= armpmu->num_events; ++idx) {
  2066. struct perf_event *event = cpuc->events[idx];
  2067. struct hw_perf_event *hwc;
  2068. if (!test_bit(idx, cpuc->active_mask))
  2069. continue;
  2070. if (!xscale1_pmnc_counter_has_overflowed(pmnc, idx))
  2071. continue;
  2072. hwc = &event->hw;
  2073. armpmu_event_update(event, hwc, idx);
  2074. data.period = event->hw.last_period;
  2075. if (!armpmu_event_set_period(event, hwc, idx))
  2076. continue;
  2077. if (perf_event_overflow(event, 0, &data, regs))
  2078. armpmu->disable(hwc, idx);
  2079. }
  2080. irq_work_run();
  2081. /*
  2082. * Re-enable the PMU.
  2083. */
  2084. pmnc = xscale1pmu_read_pmnc() | XSCALE_PMU_ENABLE;
  2085. xscale1pmu_write_pmnc(pmnc);
  2086. return IRQ_HANDLED;
  2087. }
  2088. static void
  2089. xscale1pmu_enable_event(struct hw_perf_event *hwc, int idx)
  2090. {
  2091. unsigned long val, mask, evt, flags;
  2092. switch (idx) {
  2093. case XSCALE_CYCLE_COUNTER:
  2094. mask = 0;
  2095. evt = XSCALE1_CCOUNT_INT_EN;
  2096. break;
  2097. case XSCALE_COUNTER0:
  2098. mask = XSCALE1_COUNT0_EVT_MASK;
  2099. evt = (hwc->config_base << XSCALE1_COUNT0_EVT_SHFT) |
  2100. XSCALE1_COUNT0_INT_EN;
  2101. break;
  2102. case XSCALE_COUNTER1:
  2103. mask = XSCALE1_COUNT1_EVT_MASK;
  2104. evt = (hwc->config_base << XSCALE1_COUNT1_EVT_SHFT) |
  2105. XSCALE1_COUNT1_INT_EN;
  2106. break;
  2107. default:
  2108. WARN_ONCE(1, "invalid counter number (%d)\n", idx);
  2109. return;
  2110. }
  2111. spin_lock_irqsave(&pmu_lock, flags);
  2112. val = xscale1pmu_read_pmnc();
  2113. val &= ~mask;
  2114. val |= evt;
  2115. xscale1pmu_write_pmnc(val);
  2116. spin_unlock_irqrestore(&pmu_lock, flags);
  2117. }
  2118. static void
  2119. xscale1pmu_disable_event(struct hw_perf_event *hwc, int idx)
  2120. {
  2121. unsigned long val, mask, evt, flags;
  2122. switch (idx) {
  2123. case XSCALE_CYCLE_COUNTER:
  2124. mask = XSCALE1_CCOUNT_INT_EN;
  2125. evt = 0;
  2126. break;
  2127. case XSCALE_COUNTER0:
  2128. mask = XSCALE1_COUNT0_INT_EN | XSCALE1_COUNT0_EVT_MASK;
  2129. evt = XSCALE_PERFCTR_UNUSED << XSCALE1_COUNT0_EVT_SHFT;
  2130. break;
  2131. case XSCALE_COUNTER1:
  2132. mask = XSCALE1_COUNT1_INT_EN | XSCALE1_COUNT1_EVT_MASK;
  2133. evt = XSCALE_PERFCTR_UNUSED << XSCALE1_COUNT1_EVT_SHFT;
  2134. break;
  2135. default:
  2136. WARN_ONCE(1, "invalid counter number (%d)\n", idx);
  2137. return;
  2138. }
  2139. spin_lock_irqsave(&pmu_lock, flags);
  2140. val = xscale1pmu_read_pmnc();
  2141. val &= ~mask;
  2142. val |= evt;
  2143. xscale1pmu_write_pmnc(val);
  2144. spin_unlock_irqrestore(&pmu_lock, flags);
  2145. }
  2146. static int
  2147. xscale1pmu_get_event_idx(struct cpu_hw_events *cpuc,
  2148. struct hw_perf_event *event)
  2149. {
  2150. if (XSCALE_PERFCTR_CCNT == event->config_base) {
  2151. if (test_and_set_bit(XSCALE_CYCLE_COUNTER, cpuc->used_mask))
  2152. return -EAGAIN;
  2153. return XSCALE_CYCLE_COUNTER;
  2154. } else {
  2155. if (!test_and_set_bit(XSCALE_COUNTER1, cpuc->used_mask)) {
  2156. return XSCALE_COUNTER1;
  2157. }
  2158. if (!test_and_set_bit(XSCALE_COUNTER0, cpuc->used_mask)) {
  2159. return XSCALE_COUNTER0;
  2160. }
  2161. return -EAGAIN;
  2162. }
  2163. }
  2164. static void
  2165. xscale1pmu_start(void)
  2166. {
  2167. unsigned long flags, val;
  2168. spin_lock_irqsave(&pmu_lock, flags);
  2169. val = xscale1pmu_read_pmnc();
  2170. val |= XSCALE_PMU_ENABLE;
  2171. xscale1pmu_write_pmnc(val);
  2172. spin_unlock_irqrestore(&pmu_lock, flags);
  2173. }
  2174. static void
  2175. xscale1pmu_stop(void)
  2176. {
  2177. unsigned long flags, val;
  2178. spin_lock_irqsave(&pmu_lock, flags);
  2179. val = xscale1pmu_read_pmnc();
  2180. val &= ~XSCALE_PMU_ENABLE;
  2181. xscale1pmu_write_pmnc(val);
  2182. spin_unlock_irqrestore(&pmu_lock, flags);
  2183. }
  2184. static inline u32
  2185. xscale1pmu_read_counter(int counter)
  2186. {
  2187. u32 val = 0;
  2188. switch (counter) {
  2189. case XSCALE_CYCLE_COUNTER:
  2190. asm volatile("mrc p14, 0, %0, c1, c0, 0" : "=r" (val));
  2191. break;
  2192. case XSCALE_COUNTER0:
  2193. asm volatile("mrc p14, 0, %0, c2, c0, 0" : "=r" (val));
  2194. break;
  2195. case XSCALE_COUNTER1:
  2196. asm volatile("mrc p14, 0, %0, c3, c0, 0" : "=r" (val));
  2197. break;
  2198. }
  2199. return val;
  2200. }
  2201. static inline void
  2202. xscale1pmu_write_counter(int counter, u32 val)
  2203. {
  2204. switch (counter) {
  2205. case XSCALE_CYCLE_COUNTER:
  2206. asm volatile("mcr p14, 0, %0, c1, c0, 0" : : "r" (val));
  2207. break;
  2208. case XSCALE_COUNTER0:
  2209. asm volatile("mcr p14, 0, %0, c2, c0, 0" : : "r" (val));
  2210. break;
  2211. case XSCALE_COUNTER1:
  2212. asm volatile("mcr p14, 0, %0, c3, c0, 0" : : "r" (val));
  2213. break;
  2214. }
  2215. }
  2216. static const struct arm_pmu xscale1pmu = {
  2217. .id = ARM_PERF_PMU_ID_XSCALE1,
  2218. .handle_irq = xscale1pmu_handle_irq,
  2219. .enable = xscale1pmu_enable_event,
  2220. .disable = xscale1pmu_disable_event,
  2221. .read_counter = xscale1pmu_read_counter,
  2222. .write_counter = xscale1pmu_write_counter,
  2223. .get_event_idx = xscale1pmu_get_event_idx,
  2224. .start = xscale1pmu_start,
  2225. .stop = xscale1pmu_stop,
  2226. .cache_map = &xscale_perf_cache_map,
  2227. .event_map = &xscale_perf_map,
  2228. .raw_event_mask = 0xFF,
  2229. .num_events = 3,
  2230. .max_period = (1LLU << 32) - 1,
  2231. };
  2232. #define XSCALE2_OVERFLOWED_MASK 0x01f
  2233. #define XSCALE2_CCOUNT_OVERFLOW 0x001
  2234. #define XSCALE2_COUNT0_OVERFLOW 0x002
  2235. #define XSCALE2_COUNT1_OVERFLOW 0x004
  2236. #define XSCALE2_COUNT2_OVERFLOW 0x008
  2237. #define XSCALE2_COUNT3_OVERFLOW 0x010
  2238. #define XSCALE2_CCOUNT_INT_EN 0x001
  2239. #define XSCALE2_COUNT0_INT_EN 0x002
  2240. #define XSCALE2_COUNT1_INT_EN 0x004
  2241. #define XSCALE2_COUNT2_INT_EN 0x008
  2242. #define XSCALE2_COUNT3_INT_EN 0x010
  2243. #define XSCALE2_COUNT0_EVT_SHFT 0
  2244. #define XSCALE2_COUNT0_EVT_MASK (0xff << XSCALE2_COUNT0_EVT_SHFT)
  2245. #define XSCALE2_COUNT1_EVT_SHFT 8
  2246. #define XSCALE2_COUNT1_EVT_MASK (0xff << XSCALE2_COUNT1_EVT_SHFT)
  2247. #define XSCALE2_COUNT2_EVT_SHFT 16
  2248. #define XSCALE2_COUNT2_EVT_MASK (0xff << XSCALE2_COUNT2_EVT_SHFT)
  2249. #define XSCALE2_COUNT3_EVT_SHFT 24
  2250. #define XSCALE2_COUNT3_EVT_MASK (0xff << XSCALE2_COUNT3_EVT_SHFT)
  2251. static inline u32
  2252. xscale2pmu_read_pmnc(void)
  2253. {
  2254. u32 val;
  2255. asm volatile("mrc p14, 0, %0, c0, c1, 0" : "=r" (val));
  2256. /* bits 1-2 and 4-23 are read-unpredictable */
  2257. return val & 0xff000009;
  2258. }
  2259. static inline void
  2260. xscale2pmu_write_pmnc(u32 val)
  2261. {
  2262. /* bits 4-23 are write-as-0, 24-31 are write ignored */
  2263. val &= 0xf;
  2264. asm volatile("mcr p14, 0, %0, c0, c1, 0" : : "r" (val));
  2265. }
  2266. static inline u32
  2267. xscale2pmu_read_overflow_flags(void)
  2268. {
  2269. u32 val;
  2270. asm volatile("mrc p14, 0, %0, c5, c1, 0" : "=r" (val));
  2271. return val;
  2272. }
  2273. static inline void
  2274. xscale2pmu_write_overflow_flags(u32 val)
  2275. {
  2276. asm volatile("mcr p14, 0, %0, c5, c1, 0" : : "r" (val));
  2277. }
  2278. static inline u32
  2279. xscale2pmu_read_event_select(void)
  2280. {
  2281. u32 val;
  2282. asm volatile("mrc p14, 0, %0, c8, c1, 0" : "=r" (val));
  2283. return val;
  2284. }
  2285. static inline void
  2286. xscale2pmu_write_event_select(u32 val)
  2287. {
  2288. asm volatile("mcr p14, 0, %0, c8, c1, 0" : : "r"(val));
  2289. }
  2290. static inline u32
  2291. xscale2pmu_read_int_enable(void)
  2292. {
  2293. u32 val;
  2294. asm volatile("mrc p14, 0, %0, c4, c1, 0" : "=r" (val));
  2295. return val;
  2296. }
  2297. static void
  2298. xscale2pmu_write_int_enable(u32 val)
  2299. {
  2300. asm volatile("mcr p14, 0, %0, c4, c1, 0" : : "r" (val));
  2301. }
  2302. static inline int
  2303. xscale2_pmnc_counter_has_overflowed(unsigned long of_flags,
  2304. enum xscale_counters counter)
  2305. {
  2306. int ret = 0;
  2307. switch (counter) {
  2308. case XSCALE_CYCLE_COUNTER:
  2309. ret = of_flags & XSCALE2_CCOUNT_OVERFLOW;
  2310. break;
  2311. case XSCALE_COUNTER0:
  2312. ret = of_flags & XSCALE2_COUNT0_OVERFLOW;
  2313. break;
  2314. case XSCALE_COUNTER1:
  2315. ret = of_flags & XSCALE2_COUNT1_OVERFLOW;
  2316. break;
  2317. case XSCALE_COUNTER2:
  2318. ret = of_flags & XSCALE2_COUNT2_OVERFLOW;
  2319. break;
  2320. case XSCALE_COUNTER3:
  2321. ret = of_flags & XSCALE2_COUNT3_OVERFLOW;
  2322. break;
  2323. default:
  2324. WARN_ONCE(1, "invalid counter number (%d)\n", counter);
  2325. }
  2326. return ret;
  2327. }
  2328. static irqreturn_t
  2329. xscale2pmu_handle_irq(int irq_num, void *dev)
  2330. {
  2331. unsigned long pmnc, of_flags;
  2332. struct perf_sample_data data;
  2333. struct cpu_hw_events *cpuc;
  2334. struct pt_regs *regs;
  2335. int idx;
  2336. /* Disable the PMU. */
  2337. pmnc = xscale2pmu_read_pmnc();
  2338. xscale2pmu_write_pmnc(pmnc & ~XSCALE_PMU_ENABLE);
  2339. /* Check the overflow flag register. */
  2340. of_flags = xscale2pmu_read_overflow_flags();
  2341. if (!(of_flags & XSCALE2_OVERFLOWED_MASK))
  2342. return IRQ_NONE;
  2343. /* Clear the overflow bits. */
  2344. xscale2pmu_write_overflow_flags(of_flags);
  2345. regs = get_irq_regs();
  2346. perf_sample_data_init(&data, 0);
  2347. cpuc = &__get_cpu_var(cpu_hw_events);
  2348. for (idx = 0; idx <= armpmu->num_events; ++idx) {
  2349. struct perf_event *event = cpuc->events[idx];
  2350. struct hw_perf_event *hwc;
  2351. if (!test_bit(idx, cpuc->active_mask))
  2352. continue;
  2353. if (!xscale2_pmnc_counter_has_overflowed(pmnc, idx))
  2354. continue;
  2355. hwc = &event->hw;
  2356. armpmu_event_update(event, hwc, idx);
  2357. data.period = event->hw.last_period;
  2358. if (!armpmu_event_set_period(event, hwc, idx))
  2359. continue;
  2360. if (perf_event_overflow(event, 0, &data, regs))
  2361. armpmu->disable(hwc, idx);
  2362. }
  2363. irq_work_run();
  2364. /*
  2365. * Re-enable the PMU.
  2366. */
  2367. pmnc = xscale2pmu_read_pmnc() | XSCALE_PMU_ENABLE;
  2368. xscale2pmu_write_pmnc(pmnc);
  2369. return IRQ_HANDLED;
  2370. }
  2371. static void
  2372. xscale2pmu_enable_event(struct hw_perf_event *hwc, int idx)
  2373. {
  2374. unsigned long flags, ien, evtsel;
  2375. ien = xscale2pmu_read_int_enable();
  2376. evtsel = xscale2pmu_read_event_select();
  2377. switch (idx) {
  2378. case XSCALE_CYCLE_COUNTER:
  2379. ien |= XSCALE2_CCOUNT_INT_EN;
  2380. break;
  2381. case XSCALE_COUNTER0:
  2382. ien |= XSCALE2_COUNT0_INT_EN;
  2383. evtsel &= ~XSCALE2_COUNT0_EVT_MASK;
  2384. evtsel |= hwc->config_base << XSCALE2_COUNT0_EVT_SHFT;
  2385. break;
  2386. case XSCALE_COUNTER1:
  2387. ien |= XSCALE2_COUNT1_INT_EN;
  2388. evtsel &= ~XSCALE2_COUNT1_EVT_MASK;
  2389. evtsel |= hwc->config_base << XSCALE2_COUNT1_EVT_SHFT;
  2390. break;
  2391. case XSCALE_COUNTER2:
  2392. ien |= XSCALE2_COUNT2_INT_EN;
  2393. evtsel &= ~XSCALE2_COUNT2_EVT_MASK;
  2394. evtsel |= hwc->config_base << XSCALE2_COUNT2_EVT_SHFT;
  2395. break;
  2396. case XSCALE_COUNTER3:
  2397. ien |= XSCALE2_COUNT3_INT_EN;
  2398. evtsel &= ~XSCALE2_COUNT3_EVT_MASK;
  2399. evtsel |= hwc->config_base << XSCALE2_COUNT3_EVT_SHFT;
  2400. break;
  2401. default:
  2402. WARN_ONCE(1, "invalid counter number (%d)\n", idx);
  2403. return;
  2404. }
  2405. spin_lock_irqsave(&pmu_lock, flags);
  2406. xscale2pmu_write_event_select(evtsel);
  2407. xscale2pmu_write_int_enable(ien);
  2408. spin_unlock_irqrestore(&pmu_lock, flags);
  2409. }
  2410. static void
  2411. xscale2pmu_disable_event(struct hw_perf_event *hwc, int idx)
  2412. {
  2413. unsigned long flags, ien, evtsel;
  2414. ien = xscale2pmu_read_int_enable();
  2415. evtsel = xscale2pmu_read_event_select();
  2416. switch (idx) {
  2417. case XSCALE_CYCLE_COUNTER:
  2418. ien &= ~XSCALE2_CCOUNT_INT_EN;
  2419. break;
  2420. case XSCALE_COUNTER0:
  2421. ien &= ~XSCALE2_COUNT0_INT_EN;
  2422. evtsel &= ~XSCALE2_COUNT0_EVT_MASK;
  2423. evtsel |= XSCALE_PERFCTR_UNUSED << XSCALE2_COUNT0_EVT_SHFT;
  2424. break;
  2425. case XSCALE_COUNTER1:
  2426. ien &= ~XSCALE2_COUNT1_INT_EN;
  2427. evtsel &= ~XSCALE2_COUNT1_EVT_MASK;
  2428. evtsel |= XSCALE_PERFCTR_UNUSED << XSCALE2_COUNT1_EVT_SHFT;
  2429. break;
  2430. case XSCALE_COUNTER2:
  2431. ien &= ~XSCALE2_COUNT2_INT_EN;
  2432. evtsel &= ~XSCALE2_COUNT2_EVT_MASK;
  2433. evtsel |= XSCALE_PERFCTR_UNUSED << XSCALE2_COUNT2_EVT_SHFT;
  2434. break;
  2435. case XSCALE_COUNTER3:
  2436. ien &= ~XSCALE2_COUNT3_INT_EN;
  2437. evtsel &= ~XSCALE2_COUNT3_EVT_MASK;
  2438. evtsel |= XSCALE_PERFCTR_UNUSED << XSCALE2_COUNT3_EVT_SHFT;
  2439. break;
  2440. default:
  2441. WARN_ONCE(1, "invalid counter number (%d)\n", idx);
  2442. return;
  2443. }
  2444. spin_lock_irqsave(&pmu_lock, flags);
  2445. xscale2pmu_write_event_select(evtsel);
  2446. xscale2pmu_write_int_enable(ien);
  2447. spin_unlock_irqrestore(&pmu_lock, flags);
  2448. }
  2449. static int
  2450. xscale2pmu_get_event_idx(struct cpu_hw_events *cpuc,
  2451. struct hw_perf_event *event)
  2452. {
  2453. int idx = xscale1pmu_get_event_idx(cpuc, event);
  2454. if (idx >= 0)
  2455. goto out;
  2456. if (!test_and_set_bit(XSCALE_COUNTER3, cpuc->used_mask))
  2457. idx = XSCALE_COUNTER3;
  2458. else if (!test_and_set_bit(XSCALE_COUNTER2, cpuc->used_mask))
  2459. idx = XSCALE_COUNTER2;
  2460. out:
  2461. return idx;
  2462. }
  2463. static void
  2464. xscale2pmu_start(void)
  2465. {
  2466. unsigned long flags, val;
  2467. spin_lock_irqsave(&pmu_lock, flags);
  2468. val = xscale2pmu_read_pmnc() & ~XSCALE_PMU_CNT64;
  2469. val |= XSCALE_PMU_ENABLE;
  2470. xscale2pmu_write_pmnc(val);
  2471. spin_unlock_irqrestore(&pmu_lock, flags);
  2472. }
  2473. static void
  2474. xscale2pmu_stop(void)
  2475. {
  2476. unsigned long flags, val;
  2477. spin_lock_irqsave(&pmu_lock, flags);
  2478. val = xscale2pmu_read_pmnc();
  2479. val &= ~XSCALE_PMU_ENABLE;
  2480. xscale2pmu_write_pmnc(val);
  2481. spin_unlock_irqrestore(&pmu_lock, flags);
  2482. }
  2483. static inline u32
  2484. xscale2pmu_read_counter(int counter)
  2485. {
  2486. u32 val = 0;
  2487. switch (counter) {
  2488. case XSCALE_CYCLE_COUNTER:
  2489. asm volatile("mrc p14, 0, %0, c1, c1, 0" : "=r" (val));
  2490. break;
  2491. case XSCALE_COUNTER0:
  2492. asm volatile("mrc p14, 0, %0, c0, c2, 0" : "=r" (val));
  2493. break;
  2494. case XSCALE_COUNTER1:
  2495. asm volatile("mrc p14, 0, %0, c1, c2, 0" : "=r" (val));
  2496. break;
  2497. case XSCALE_COUNTER2:
  2498. asm volatile("mrc p14, 0, %0, c2, c2, 0" : "=r" (val));
  2499. break;
  2500. case XSCALE_COUNTER3:
  2501. asm volatile("mrc p14, 0, %0, c3, c2, 0" : "=r" (val));
  2502. break;
  2503. }
  2504. return val;
  2505. }
  2506. static inline void
  2507. xscale2pmu_write_counter(int counter, u32 val)
  2508. {
  2509. switch (counter) {
  2510. case XSCALE_CYCLE_COUNTER:
  2511. asm volatile("mcr p14, 0, %0, c1, c1, 0" : : "r" (val));
  2512. break;
  2513. case XSCALE_COUNTER0:
  2514. asm volatile("mcr p14, 0, %0, c0, c2, 0" : : "r" (val));
  2515. break;
  2516. case XSCALE_COUNTER1:
  2517. asm volatile("mcr p14, 0, %0, c1, c2, 0" : : "r" (val));
  2518. break;
  2519. case XSCALE_COUNTER2:
  2520. asm volatile("mcr p14, 0, %0, c2, c2, 0" : : "r" (val));
  2521. break;
  2522. case XSCALE_COUNTER3:
  2523. asm volatile("mcr p14, 0, %0, c3, c2, 0" : : "r" (val));
  2524. break;
  2525. }
  2526. }
  2527. static const struct arm_pmu xscale2pmu = {
  2528. .id = ARM_PERF_PMU_ID_XSCALE2,
  2529. .handle_irq = xscale2pmu_handle_irq,
  2530. .enable = xscale2pmu_enable_event,
  2531. .disable = xscale2pmu_disable_event,
  2532. .read_counter = xscale2pmu_read_counter,
  2533. .write_counter = xscale2pmu_write_counter,
  2534. .get_event_idx = xscale2pmu_get_event_idx,
  2535. .start = xscale2pmu_start,
  2536. .stop = xscale2pmu_stop,
  2537. .cache_map = &xscale_perf_cache_map,
  2538. .event_map = &xscale_perf_map,
  2539. .raw_event_mask = 0xFF,
  2540. .num_events = 5,
  2541. .max_period = (1LLU << 32) - 1,
  2542. };
  2543. static int __init
  2544. init_hw_perf_events(void)
  2545. {
  2546. unsigned long cpuid = read_cpuid_id();
  2547. unsigned long implementor = (cpuid & 0xFF000000) >> 24;
  2548. unsigned long part_number = (cpuid & 0xFFF0);
  2549. /* ARM Ltd CPUs. */
  2550. if (0x41 == implementor) {
  2551. switch (part_number) {
  2552. case 0xB360: /* ARM1136 */
  2553. case 0xB560: /* ARM1156 */
  2554. case 0xB760: /* ARM1176 */
  2555. armpmu = &armv6pmu;
  2556. break;
  2557. case 0xB020: /* ARM11mpcore */
  2558. armpmu = &armv6mpcore_pmu;
  2559. break;
  2560. case 0xC080: /* Cortex-A8 */
  2561. armv7pmu.id = ARM_PERF_PMU_ID_CA8;
  2562. armv7pmu.cache_map = &armv7_a8_perf_cache_map;
  2563. armv7pmu.event_map = &armv7_a8_perf_map;
  2564. armpmu = &armv7pmu;
  2565. /* Reset PMNC and read the nb of CNTx counters
  2566. supported */
  2567. armv7pmu.num_events = armv7_reset_read_pmnc();
  2568. break;
  2569. case 0xC090: /* Cortex-A9 */
  2570. armv7pmu.id = ARM_PERF_PMU_ID_CA9;
  2571. armv7pmu.cache_map = &armv7_a9_perf_cache_map;
  2572. armv7pmu.event_map = &armv7_a9_perf_map;
  2573. armpmu = &armv7pmu;
  2574. /* Reset PMNC and read the nb of CNTx counters
  2575. supported */
  2576. armv7pmu.num_events = armv7_reset_read_pmnc();
  2577. break;
  2578. }
  2579. /* Intel CPUs [xscale]. */
  2580. } else if (0x69 == implementor) {
  2581. part_number = (cpuid >> 13) & 0x7;
  2582. switch (part_number) {
  2583. case 1:
  2584. armpmu = &xscale1pmu;
  2585. break;
  2586. case 2:
  2587. armpmu = &xscale2pmu;
  2588. break;
  2589. }
  2590. }
  2591. if (armpmu) {
  2592. pr_info("enabled with %s PMU driver, %d counters available\n",
  2593. arm_pmu_names[armpmu->id], armpmu->num_events);
  2594. } else {
  2595. pr_info("no hardware support available\n");
  2596. }
  2597. perf_pmu_register(&pmu);
  2598. return 0;
  2599. }
  2600. arch_initcall(init_hw_perf_events);
  2601. /*
  2602. * Callchain handling code.
  2603. */
  2604. /*
  2605. * The registers we're interested in are at the end of the variable
  2606. * length saved register structure. The fp points at the end of this
  2607. * structure so the address of this struct is:
  2608. * (struct frame_tail *)(xxx->fp)-1
  2609. *
  2610. * This code has been adapted from the ARM OProfile support.
  2611. */
  2612. struct frame_tail {
  2613. struct frame_tail *fp;
  2614. unsigned long sp;
  2615. unsigned long lr;
  2616. } __attribute__((packed));
  2617. /*
  2618. * Get the return address for a single stackframe and return a pointer to the
  2619. * next frame tail.
  2620. */
  2621. static struct frame_tail *
  2622. user_backtrace(struct frame_tail *tail,
  2623. struct perf_callchain_entry *entry)
  2624. {
  2625. struct frame_tail buftail;
  2626. /* Also check accessibility of one struct frame_tail beyond */
  2627. if (!access_ok(VERIFY_READ, tail, sizeof(buftail)))
  2628. return NULL;
  2629. if (__copy_from_user_inatomic(&buftail, tail, sizeof(buftail)))
  2630. return NULL;
  2631. perf_callchain_store(entry, buftail.lr);
  2632. /*
  2633. * Frame pointers should strictly progress back up the stack
  2634. * (towards higher addresses).
  2635. */
  2636. if (tail >= buftail.fp)
  2637. return NULL;
  2638. return buftail.fp - 1;
  2639. }
  2640. void
  2641. perf_callchain_user(struct perf_callchain_entry *entry, struct pt_regs *regs)
  2642. {
  2643. struct frame_tail *tail;
  2644. tail = (struct frame_tail *)regs->ARM_fp - 1;
  2645. while (tail && !((unsigned long)tail & 0x3))
  2646. tail = user_backtrace(tail, entry);
  2647. }
  2648. /*
  2649. * Gets called by walk_stackframe() for every stackframe. This will be called
  2650. * whist unwinding the stackframe and is like a subroutine return so we use
  2651. * the PC.
  2652. */
  2653. static int
  2654. callchain_trace(struct stackframe *fr,
  2655. void *data)
  2656. {
  2657. struct perf_callchain_entry *entry = data;
  2658. perf_callchain_store(entry, fr->pc);
  2659. return 0;
  2660. }
  2661. void
  2662. perf_callchain_kernel(struct perf_callchain_entry *entry, struct pt_regs *regs)
  2663. {
  2664. struct stackframe fr;
  2665. fr.fp = regs->ARM_fp;
  2666. fr.sp = regs->ARM_sp;
  2667. fr.lr = regs->ARM_lr;
  2668. fr.pc = regs->ARM_pc;
  2669. walk_stackframe(&fr, callchain_trace, entry);
  2670. }