perf_event.c 79 KB

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