perf_event.c 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858
  1. /* Performance event support for sparc64.
  2. *
  3. * Copyright (C) 2009, 2010 David S. Miller <davem@davemloft.net>
  4. *
  5. * This code is based almost entirely upon the x86 perf event
  6. * code, which is:
  7. *
  8. * Copyright (C) 2008 Thomas Gleixner <tglx@linutronix.de>
  9. * Copyright (C) 2008-2009 Red Hat, Inc., Ingo Molnar
  10. * Copyright (C) 2009 Jaswinder Singh Rajput
  11. * Copyright (C) 2009 Advanced Micro Devices, Inc., Robert Richter
  12. * Copyright (C) 2008-2009 Red Hat, Inc., Peter Zijlstra
  13. */
  14. #include <linux/perf_event.h>
  15. #include <linux/kprobes.h>
  16. #include <linux/ftrace.h>
  17. #include <linux/kernel.h>
  18. #include <linux/kdebug.h>
  19. #include <linux/mutex.h>
  20. #include <asm/stacktrace.h>
  21. #include <asm/cpudata.h>
  22. #include <linux/uaccess.h>
  23. #include <linux/atomic.h>
  24. #include <asm/nmi.h>
  25. #include <asm/pcr.h>
  26. #include <asm/cacheflush.h>
  27. #include "kernel.h"
  28. #include "kstack.h"
  29. /* Two classes of sparc64 chips currently exist. All of which have
  30. * 32-bit counters which can generate overflow interrupts on the
  31. * transition from 0xffffffff to 0.
  32. *
  33. * All chips upto and including SPARC-T3 have two performance
  34. * counters. The two 32-bit counters are accessed in one go using a
  35. * single 64-bit register.
  36. *
  37. * On these older chips both counters are controlled using a single
  38. * control register. The only way to stop all sampling is to clear
  39. * all of the context (user, supervisor, hypervisor) sampling enable
  40. * bits. But these bits apply to both counters, thus the two counters
  41. * can't be enabled/disabled individually.
  42. *
  43. * Furthermore, the control register on these older chips have two
  44. * event fields, one for each of the two counters. It's thus nearly
  45. * impossible to have one counter going while keeping the other one
  46. * stopped. Therefore it is possible to get overflow interrupts for
  47. * counters not currently "in use" and that condition must be checked
  48. * in the overflow interrupt handler.
  49. *
  50. * So we use a hack, in that we program inactive counters with the
  51. * "sw_count0" and "sw_count1" events. These count how many times
  52. * the instruction "sethi %hi(0xfc000), %g0" is executed. It's an
  53. * unusual way to encode a NOP and therefore will not trigger in
  54. * normal code.
  55. *
  56. * Starting with SPARC-T4 we have one control register per counter.
  57. * And the counters are stored in individual registers. The registers
  58. * for the counters are 64-bit but only a 32-bit counter is
  59. * implemented. The event selections on SPARC-T4 lack any
  60. * restrictions, therefore we can elide all of the complicated
  61. * conflict resolution code we have for SPARC-T3 and earlier chips.
  62. */
  63. #define MAX_HWEVENTS 4
  64. #define MAX_PCRS 4
  65. #define MAX_PERIOD ((1UL << 32) - 1)
  66. #define PIC_UPPER_INDEX 0
  67. #define PIC_LOWER_INDEX 1
  68. #define PIC_NO_INDEX -1
  69. struct cpu_hw_events {
  70. /* Number of events currently scheduled onto this cpu.
  71. * This tells how many entries in the arrays below
  72. * are valid.
  73. */
  74. int n_events;
  75. /* Number of new events added since the last hw_perf_disable().
  76. * This works because the perf event layer always adds new
  77. * events inside of a perf_{disable,enable}() sequence.
  78. */
  79. int n_added;
  80. /* Array of events current scheduled on this cpu. */
  81. struct perf_event *event[MAX_HWEVENTS];
  82. /* Array of encoded longs, specifying the %pcr register
  83. * encoding and the mask of PIC counters this even can
  84. * be scheduled on. See perf_event_encode() et al.
  85. */
  86. unsigned long events[MAX_HWEVENTS];
  87. /* The current counter index assigned to an event. When the
  88. * event hasn't been programmed into the cpu yet, this will
  89. * hold PIC_NO_INDEX. The event->hw.idx value tells us where
  90. * we ought to schedule the event.
  91. */
  92. int current_idx[MAX_HWEVENTS];
  93. /* Software copy of %pcr register(s) on this cpu. */
  94. u64 pcr[MAX_HWEVENTS];
  95. /* Enabled/disable state. */
  96. int enabled;
  97. unsigned int txn_flags;
  98. };
  99. static DEFINE_PER_CPU(struct cpu_hw_events, cpu_hw_events) = { .enabled = 1, };
  100. /* An event map describes the characteristics of a performance
  101. * counter event. In particular it gives the encoding as well as
  102. * a mask telling which counters the event can be measured on.
  103. *
  104. * The mask is unused on SPARC-T4 and later.
  105. */
  106. struct perf_event_map {
  107. u16 encoding;
  108. u8 pic_mask;
  109. #define PIC_NONE 0x00
  110. #define PIC_UPPER 0x01
  111. #define PIC_LOWER 0x02
  112. };
  113. /* Encode a perf_event_map entry into a long. */
  114. static unsigned long perf_event_encode(const struct perf_event_map *pmap)
  115. {
  116. return ((unsigned long) pmap->encoding << 16) | pmap->pic_mask;
  117. }
  118. static u8 perf_event_get_msk(unsigned long val)
  119. {
  120. return val & 0xff;
  121. }
  122. static u64 perf_event_get_enc(unsigned long val)
  123. {
  124. return val >> 16;
  125. }
  126. #define C(x) PERF_COUNT_HW_CACHE_##x
  127. #define CACHE_OP_UNSUPPORTED 0xfffe
  128. #define CACHE_OP_NONSENSE 0xffff
  129. typedef struct perf_event_map cache_map_t
  130. [PERF_COUNT_HW_CACHE_MAX]
  131. [PERF_COUNT_HW_CACHE_OP_MAX]
  132. [PERF_COUNT_HW_CACHE_RESULT_MAX];
  133. struct sparc_pmu {
  134. const struct perf_event_map *(*event_map)(int);
  135. const cache_map_t *cache_map;
  136. int max_events;
  137. u32 (*read_pmc)(int);
  138. void (*write_pmc)(int, u64);
  139. int upper_shift;
  140. int lower_shift;
  141. int event_mask;
  142. int user_bit;
  143. int priv_bit;
  144. int hv_bit;
  145. int irq_bit;
  146. int upper_nop;
  147. int lower_nop;
  148. unsigned int flags;
  149. #define SPARC_PMU_ALL_EXCLUDES_SAME 0x00000001
  150. #define SPARC_PMU_HAS_CONFLICTS 0x00000002
  151. int max_hw_events;
  152. int num_pcrs;
  153. int num_pic_regs;
  154. };
  155. static u32 sparc_default_read_pmc(int idx)
  156. {
  157. u64 val;
  158. val = pcr_ops->read_pic(0);
  159. if (idx == PIC_UPPER_INDEX)
  160. val >>= 32;
  161. return val & 0xffffffff;
  162. }
  163. static void sparc_default_write_pmc(int idx, u64 val)
  164. {
  165. u64 shift, mask, pic;
  166. shift = 0;
  167. if (idx == PIC_UPPER_INDEX)
  168. shift = 32;
  169. mask = ((u64) 0xffffffff) << shift;
  170. val <<= shift;
  171. pic = pcr_ops->read_pic(0);
  172. pic &= ~mask;
  173. pic |= val;
  174. pcr_ops->write_pic(0, pic);
  175. }
  176. static const struct perf_event_map ultra3_perfmon_event_map[] = {
  177. [PERF_COUNT_HW_CPU_CYCLES] = { 0x0000, PIC_UPPER | PIC_LOWER },
  178. [PERF_COUNT_HW_INSTRUCTIONS] = { 0x0001, PIC_UPPER | PIC_LOWER },
  179. [PERF_COUNT_HW_CACHE_REFERENCES] = { 0x0009, PIC_LOWER },
  180. [PERF_COUNT_HW_CACHE_MISSES] = { 0x0009, PIC_UPPER },
  181. };
  182. static const struct perf_event_map *ultra3_event_map(int event_id)
  183. {
  184. return &ultra3_perfmon_event_map[event_id];
  185. }
  186. static const cache_map_t ultra3_cache_map = {
  187. [C(L1D)] = {
  188. [C(OP_READ)] = {
  189. [C(RESULT_ACCESS)] = { 0x09, PIC_LOWER, },
  190. [C(RESULT_MISS)] = { 0x09, PIC_UPPER, },
  191. },
  192. [C(OP_WRITE)] = {
  193. [C(RESULT_ACCESS)] = { 0x0a, PIC_LOWER },
  194. [C(RESULT_MISS)] = { 0x0a, PIC_UPPER },
  195. },
  196. [C(OP_PREFETCH)] = {
  197. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  198. [C(RESULT_MISS)] = { CACHE_OP_UNSUPPORTED },
  199. },
  200. },
  201. [C(L1I)] = {
  202. [C(OP_READ)] = {
  203. [C(RESULT_ACCESS)] = { 0x09, PIC_LOWER, },
  204. [C(RESULT_MISS)] = { 0x09, PIC_UPPER, },
  205. },
  206. [ C(OP_WRITE) ] = {
  207. [ C(RESULT_ACCESS) ] = { CACHE_OP_NONSENSE },
  208. [ C(RESULT_MISS) ] = { CACHE_OP_NONSENSE },
  209. },
  210. [ C(OP_PREFETCH) ] = {
  211. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  212. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  213. },
  214. },
  215. [C(LL)] = {
  216. [C(OP_READ)] = {
  217. [C(RESULT_ACCESS)] = { 0x0c, PIC_LOWER, },
  218. [C(RESULT_MISS)] = { 0x0c, PIC_UPPER, },
  219. },
  220. [C(OP_WRITE)] = {
  221. [C(RESULT_ACCESS)] = { 0x0c, PIC_LOWER },
  222. [C(RESULT_MISS)] = { 0x0c, PIC_UPPER },
  223. },
  224. [C(OP_PREFETCH)] = {
  225. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  226. [C(RESULT_MISS)] = { CACHE_OP_UNSUPPORTED },
  227. },
  228. },
  229. [C(DTLB)] = {
  230. [C(OP_READ)] = {
  231. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  232. [C(RESULT_MISS)] = { 0x12, PIC_UPPER, },
  233. },
  234. [ C(OP_WRITE) ] = {
  235. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  236. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  237. },
  238. [ C(OP_PREFETCH) ] = {
  239. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  240. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  241. },
  242. },
  243. [C(ITLB)] = {
  244. [C(OP_READ)] = {
  245. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  246. [C(RESULT_MISS)] = { 0x11, PIC_UPPER, },
  247. },
  248. [ C(OP_WRITE) ] = {
  249. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  250. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  251. },
  252. [ C(OP_PREFETCH) ] = {
  253. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  254. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  255. },
  256. },
  257. [C(BPU)] = {
  258. [C(OP_READ)] = {
  259. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  260. [C(RESULT_MISS)] = { CACHE_OP_UNSUPPORTED },
  261. },
  262. [ C(OP_WRITE) ] = {
  263. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  264. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  265. },
  266. [ C(OP_PREFETCH) ] = {
  267. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  268. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  269. },
  270. },
  271. [C(NODE)] = {
  272. [C(OP_READ)] = {
  273. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  274. [C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  275. },
  276. [ C(OP_WRITE) ] = {
  277. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  278. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  279. },
  280. [ C(OP_PREFETCH) ] = {
  281. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  282. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  283. },
  284. },
  285. };
  286. static const struct sparc_pmu ultra3_pmu = {
  287. .event_map = ultra3_event_map,
  288. .cache_map = &ultra3_cache_map,
  289. .max_events = ARRAY_SIZE(ultra3_perfmon_event_map),
  290. .read_pmc = sparc_default_read_pmc,
  291. .write_pmc = sparc_default_write_pmc,
  292. .upper_shift = 11,
  293. .lower_shift = 4,
  294. .event_mask = 0x3f,
  295. .user_bit = PCR_UTRACE,
  296. .priv_bit = PCR_STRACE,
  297. .upper_nop = 0x1c,
  298. .lower_nop = 0x14,
  299. .flags = (SPARC_PMU_ALL_EXCLUDES_SAME |
  300. SPARC_PMU_HAS_CONFLICTS),
  301. .max_hw_events = 2,
  302. .num_pcrs = 1,
  303. .num_pic_regs = 1,
  304. };
  305. /* Niagara1 is very limited. The upper PIC is hard-locked to count
  306. * only instructions, so it is free running which creates all kinds of
  307. * problems. Some hardware designs make one wonder if the creator
  308. * even looked at how this stuff gets used by software.
  309. */
  310. static const struct perf_event_map niagara1_perfmon_event_map[] = {
  311. [PERF_COUNT_HW_CPU_CYCLES] = { 0x00, PIC_UPPER },
  312. [PERF_COUNT_HW_INSTRUCTIONS] = { 0x00, PIC_UPPER },
  313. [PERF_COUNT_HW_CACHE_REFERENCES] = { 0, PIC_NONE },
  314. [PERF_COUNT_HW_CACHE_MISSES] = { 0x03, PIC_LOWER },
  315. };
  316. static const struct perf_event_map *niagara1_event_map(int event_id)
  317. {
  318. return &niagara1_perfmon_event_map[event_id];
  319. }
  320. static const cache_map_t niagara1_cache_map = {
  321. [C(L1D)] = {
  322. [C(OP_READ)] = {
  323. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  324. [C(RESULT_MISS)] = { 0x03, PIC_LOWER, },
  325. },
  326. [C(OP_WRITE)] = {
  327. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  328. [C(RESULT_MISS)] = { 0x03, PIC_LOWER, },
  329. },
  330. [C(OP_PREFETCH)] = {
  331. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  332. [C(RESULT_MISS)] = { CACHE_OP_UNSUPPORTED },
  333. },
  334. },
  335. [C(L1I)] = {
  336. [C(OP_READ)] = {
  337. [C(RESULT_ACCESS)] = { 0x00, PIC_UPPER },
  338. [C(RESULT_MISS)] = { 0x02, PIC_LOWER, },
  339. },
  340. [ C(OP_WRITE) ] = {
  341. [ C(RESULT_ACCESS) ] = { CACHE_OP_NONSENSE },
  342. [ C(RESULT_MISS) ] = { CACHE_OP_NONSENSE },
  343. },
  344. [ C(OP_PREFETCH) ] = {
  345. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  346. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  347. },
  348. },
  349. [C(LL)] = {
  350. [C(OP_READ)] = {
  351. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  352. [C(RESULT_MISS)] = { 0x07, PIC_LOWER, },
  353. },
  354. [C(OP_WRITE)] = {
  355. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  356. [C(RESULT_MISS)] = { 0x07, PIC_LOWER, },
  357. },
  358. [C(OP_PREFETCH)] = {
  359. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  360. [C(RESULT_MISS)] = { CACHE_OP_UNSUPPORTED },
  361. },
  362. },
  363. [C(DTLB)] = {
  364. [C(OP_READ)] = {
  365. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  366. [C(RESULT_MISS)] = { 0x05, PIC_LOWER, },
  367. },
  368. [ C(OP_WRITE) ] = {
  369. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  370. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  371. },
  372. [ C(OP_PREFETCH) ] = {
  373. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  374. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  375. },
  376. },
  377. [C(ITLB)] = {
  378. [C(OP_READ)] = {
  379. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  380. [C(RESULT_MISS)] = { 0x04, PIC_LOWER, },
  381. },
  382. [ C(OP_WRITE) ] = {
  383. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  384. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  385. },
  386. [ C(OP_PREFETCH) ] = {
  387. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  388. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  389. },
  390. },
  391. [C(BPU)] = {
  392. [C(OP_READ)] = {
  393. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  394. [C(RESULT_MISS)] = { CACHE_OP_UNSUPPORTED },
  395. },
  396. [ C(OP_WRITE) ] = {
  397. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  398. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  399. },
  400. [ C(OP_PREFETCH) ] = {
  401. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  402. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  403. },
  404. },
  405. [C(NODE)] = {
  406. [C(OP_READ)] = {
  407. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  408. [C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  409. },
  410. [ C(OP_WRITE) ] = {
  411. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  412. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  413. },
  414. [ C(OP_PREFETCH) ] = {
  415. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  416. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  417. },
  418. },
  419. };
  420. static const struct sparc_pmu niagara1_pmu = {
  421. .event_map = niagara1_event_map,
  422. .cache_map = &niagara1_cache_map,
  423. .max_events = ARRAY_SIZE(niagara1_perfmon_event_map),
  424. .read_pmc = sparc_default_read_pmc,
  425. .write_pmc = sparc_default_write_pmc,
  426. .upper_shift = 0,
  427. .lower_shift = 4,
  428. .event_mask = 0x7,
  429. .user_bit = PCR_UTRACE,
  430. .priv_bit = PCR_STRACE,
  431. .upper_nop = 0x0,
  432. .lower_nop = 0x0,
  433. .flags = (SPARC_PMU_ALL_EXCLUDES_SAME |
  434. SPARC_PMU_HAS_CONFLICTS),
  435. .max_hw_events = 2,
  436. .num_pcrs = 1,
  437. .num_pic_regs = 1,
  438. };
  439. static const struct perf_event_map niagara2_perfmon_event_map[] = {
  440. [PERF_COUNT_HW_CPU_CYCLES] = { 0x02ff, PIC_UPPER | PIC_LOWER },
  441. [PERF_COUNT_HW_INSTRUCTIONS] = { 0x02ff, PIC_UPPER | PIC_LOWER },
  442. [PERF_COUNT_HW_CACHE_REFERENCES] = { 0x0208, PIC_UPPER | PIC_LOWER },
  443. [PERF_COUNT_HW_CACHE_MISSES] = { 0x0302, PIC_UPPER | PIC_LOWER },
  444. [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = { 0x0201, PIC_UPPER | PIC_LOWER },
  445. [PERF_COUNT_HW_BRANCH_MISSES] = { 0x0202, PIC_UPPER | PIC_LOWER },
  446. };
  447. static const struct perf_event_map *niagara2_event_map(int event_id)
  448. {
  449. return &niagara2_perfmon_event_map[event_id];
  450. }
  451. static const cache_map_t niagara2_cache_map = {
  452. [C(L1D)] = {
  453. [C(OP_READ)] = {
  454. [C(RESULT_ACCESS)] = { 0x0208, PIC_UPPER | PIC_LOWER, },
  455. [C(RESULT_MISS)] = { 0x0302, PIC_UPPER | PIC_LOWER, },
  456. },
  457. [C(OP_WRITE)] = {
  458. [C(RESULT_ACCESS)] = { 0x0210, PIC_UPPER | PIC_LOWER, },
  459. [C(RESULT_MISS)] = { 0x0302, PIC_UPPER | PIC_LOWER, },
  460. },
  461. [C(OP_PREFETCH)] = {
  462. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  463. [C(RESULT_MISS)] = { CACHE_OP_UNSUPPORTED },
  464. },
  465. },
  466. [C(L1I)] = {
  467. [C(OP_READ)] = {
  468. [C(RESULT_ACCESS)] = { 0x02ff, PIC_UPPER | PIC_LOWER, },
  469. [C(RESULT_MISS)] = { 0x0301, PIC_UPPER | PIC_LOWER, },
  470. },
  471. [ C(OP_WRITE) ] = {
  472. [ C(RESULT_ACCESS) ] = { CACHE_OP_NONSENSE },
  473. [ C(RESULT_MISS) ] = { CACHE_OP_NONSENSE },
  474. },
  475. [ C(OP_PREFETCH) ] = {
  476. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  477. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  478. },
  479. },
  480. [C(LL)] = {
  481. [C(OP_READ)] = {
  482. [C(RESULT_ACCESS)] = { 0x0208, PIC_UPPER | PIC_LOWER, },
  483. [C(RESULT_MISS)] = { 0x0330, PIC_UPPER | PIC_LOWER, },
  484. },
  485. [C(OP_WRITE)] = {
  486. [C(RESULT_ACCESS)] = { 0x0210, PIC_UPPER | PIC_LOWER, },
  487. [C(RESULT_MISS)] = { 0x0320, PIC_UPPER | PIC_LOWER, },
  488. },
  489. [C(OP_PREFETCH)] = {
  490. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  491. [C(RESULT_MISS)] = { CACHE_OP_UNSUPPORTED },
  492. },
  493. },
  494. [C(DTLB)] = {
  495. [C(OP_READ)] = {
  496. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  497. [C(RESULT_MISS)] = { 0x0b08, PIC_UPPER | PIC_LOWER, },
  498. },
  499. [ C(OP_WRITE) ] = {
  500. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  501. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  502. },
  503. [ C(OP_PREFETCH) ] = {
  504. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  505. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  506. },
  507. },
  508. [C(ITLB)] = {
  509. [C(OP_READ)] = {
  510. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  511. [C(RESULT_MISS)] = { 0xb04, PIC_UPPER | PIC_LOWER, },
  512. },
  513. [ C(OP_WRITE) ] = {
  514. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  515. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  516. },
  517. [ C(OP_PREFETCH) ] = {
  518. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  519. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  520. },
  521. },
  522. [C(BPU)] = {
  523. [C(OP_READ)] = {
  524. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  525. [C(RESULT_MISS)] = { CACHE_OP_UNSUPPORTED },
  526. },
  527. [ C(OP_WRITE) ] = {
  528. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  529. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  530. },
  531. [ C(OP_PREFETCH) ] = {
  532. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  533. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  534. },
  535. },
  536. [C(NODE)] = {
  537. [C(OP_READ)] = {
  538. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  539. [C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  540. },
  541. [ C(OP_WRITE) ] = {
  542. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  543. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  544. },
  545. [ C(OP_PREFETCH) ] = {
  546. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  547. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  548. },
  549. },
  550. };
  551. static const struct sparc_pmu niagara2_pmu = {
  552. .event_map = niagara2_event_map,
  553. .cache_map = &niagara2_cache_map,
  554. .max_events = ARRAY_SIZE(niagara2_perfmon_event_map),
  555. .read_pmc = sparc_default_read_pmc,
  556. .write_pmc = sparc_default_write_pmc,
  557. .upper_shift = 19,
  558. .lower_shift = 6,
  559. .event_mask = 0xfff,
  560. .user_bit = PCR_UTRACE,
  561. .priv_bit = PCR_STRACE,
  562. .hv_bit = PCR_N2_HTRACE,
  563. .irq_bit = 0x30,
  564. .upper_nop = 0x220,
  565. .lower_nop = 0x220,
  566. .flags = (SPARC_PMU_ALL_EXCLUDES_SAME |
  567. SPARC_PMU_HAS_CONFLICTS),
  568. .max_hw_events = 2,
  569. .num_pcrs = 1,
  570. .num_pic_regs = 1,
  571. };
  572. static const struct perf_event_map niagara4_perfmon_event_map[] = {
  573. [PERF_COUNT_HW_CPU_CYCLES] = { (26 << 6) },
  574. [PERF_COUNT_HW_INSTRUCTIONS] = { (3 << 6) | 0x3f },
  575. [PERF_COUNT_HW_CACHE_REFERENCES] = { (3 << 6) | 0x04 },
  576. [PERF_COUNT_HW_CACHE_MISSES] = { (16 << 6) | 0x07 },
  577. [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = { (4 << 6) | 0x01 },
  578. [PERF_COUNT_HW_BRANCH_MISSES] = { (25 << 6) | 0x0f },
  579. };
  580. static const struct perf_event_map *niagara4_event_map(int event_id)
  581. {
  582. return &niagara4_perfmon_event_map[event_id];
  583. }
  584. static const cache_map_t niagara4_cache_map = {
  585. [C(L1D)] = {
  586. [C(OP_READ)] = {
  587. [C(RESULT_ACCESS)] = { (3 << 6) | 0x04 },
  588. [C(RESULT_MISS)] = { (16 << 6) | 0x07 },
  589. },
  590. [C(OP_WRITE)] = {
  591. [C(RESULT_ACCESS)] = { (3 << 6) | 0x08 },
  592. [C(RESULT_MISS)] = { (16 << 6) | 0x07 },
  593. },
  594. [C(OP_PREFETCH)] = {
  595. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  596. [C(RESULT_MISS)] = { CACHE_OP_UNSUPPORTED },
  597. },
  598. },
  599. [C(L1I)] = {
  600. [C(OP_READ)] = {
  601. [C(RESULT_ACCESS)] = { (3 << 6) | 0x3f },
  602. [C(RESULT_MISS)] = { (11 << 6) | 0x03 },
  603. },
  604. [ C(OP_WRITE) ] = {
  605. [ C(RESULT_ACCESS) ] = { CACHE_OP_NONSENSE },
  606. [ C(RESULT_MISS) ] = { CACHE_OP_NONSENSE },
  607. },
  608. [ C(OP_PREFETCH) ] = {
  609. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  610. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  611. },
  612. },
  613. [C(LL)] = {
  614. [C(OP_READ)] = {
  615. [C(RESULT_ACCESS)] = { (3 << 6) | 0x04 },
  616. [C(RESULT_MISS)] = { CACHE_OP_UNSUPPORTED },
  617. },
  618. [C(OP_WRITE)] = {
  619. [C(RESULT_ACCESS)] = { (3 << 6) | 0x08 },
  620. [C(RESULT_MISS)] = { CACHE_OP_UNSUPPORTED },
  621. },
  622. [C(OP_PREFETCH)] = {
  623. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  624. [C(RESULT_MISS)] = { CACHE_OP_UNSUPPORTED },
  625. },
  626. },
  627. [C(DTLB)] = {
  628. [C(OP_READ)] = {
  629. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  630. [C(RESULT_MISS)] = { (17 << 6) | 0x3f },
  631. },
  632. [ C(OP_WRITE) ] = {
  633. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  634. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  635. },
  636. [ C(OP_PREFETCH) ] = {
  637. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  638. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  639. },
  640. },
  641. [C(ITLB)] = {
  642. [C(OP_READ)] = {
  643. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  644. [C(RESULT_MISS)] = { (6 << 6) | 0x3f },
  645. },
  646. [ C(OP_WRITE) ] = {
  647. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  648. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  649. },
  650. [ C(OP_PREFETCH) ] = {
  651. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  652. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  653. },
  654. },
  655. [C(BPU)] = {
  656. [C(OP_READ)] = {
  657. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  658. [C(RESULT_MISS)] = { CACHE_OP_UNSUPPORTED },
  659. },
  660. [ C(OP_WRITE) ] = {
  661. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  662. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  663. },
  664. [ C(OP_PREFETCH) ] = {
  665. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  666. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  667. },
  668. },
  669. [C(NODE)] = {
  670. [C(OP_READ)] = {
  671. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  672. [C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  673. },
  674. [ C(OP_WRITE) ] = {
  675. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  676. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  677. },
  678. [ C(OP_PREFETCH) ] = {
  679. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  680. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  681. },
  682. },
  683. };
  684. static u32 sparc_vt_read_pmc(int idx)
  685. {
  686. u64 val = pcr_ops->read_pic(idx);
  687. return val & 0xffffffff;
  688. }
  689. static void sparc_vt_write_pmc(int idx, u64 val)
  690. {
  691. u64 pcr;
  692. pcr = pcr_ops->read_pcr(idx);
  693. /* ensure ov and ntc are reset */
  694. pcr &= ~(PCR_N4_OV | PCR_N4_NTC);
  695. pcr_ops->write_pic(idx, val & 0xffffffff);
  696. pcr_ops->write_pcr(idx, pcr);
  697. }
  698. static const struct sparc_pmu niagara4_pmu = {
  699. .event_map = niagara4_event_map,
  700. .cache_map = &niagara4_cache_map,
  701. .max_events = ARRAY_SIZE(niagara4_perfmon_event_map),
  702. .read_pmc = sparc_vt_read_pmc,
  703. .write_pmc = sparc_vt_write_pmc,
  704. .upper_shift = 5,
  705. .lower_shift = 5,
  706. .event_mask = 0x7ff,
  707. .user_bit = PCR_N4_UTRACE,
  708. .priv_bit = PCR_N4_STRACE,
  709. /* We explicitly don't support hypervisor tracing. The T4
  710. * generates the overflow event for precise events via a trap
  711. * which will not be generated (ie. it's completely lost) if
  712. * we happen to be in the hypervisor when the event triggers.
  713. * Essentially, the overflow event reporting is completely
  714. * unusable when you have hypervisor mode tracing enabled.
  715. */
  716. .hv_bit = 0,
  717. .irq_bit = PCR_N4_TOE,
  718. .upper_nop = 0,
  719. .lower_nop = 0,
  720. .flags = 0,
  721. .max_hw_events = 4,
  722. .num_pcrs = 4,
  723. .num_pic_regs = 4,
  724. };
  725. static const struct sparc_pmu sparc_m7_pmu = {
  726. .event_map = niagara4_event_map,
  727. .cache_map = &niagara4_cache_map,
  728. .max_events = ARRAY_SIZE(niagara4_perfmon_event_map),
  729. .read_pmc = sparc_vt_read_pmc,
  730. .write_pmc = sparc_vt_write_pmc,
  731. .upper_shift = 5,
  732. .lower_shift = 5,
  733. .event_mask = 0x7ff,
  734. .user_bit = PCR_N4_UTRACE,
  735. .priv_bit = PCR_N4_STRACE,
  736. /* We explicitly don't support hypervisor tracing. */
  737. .hv_bit = 0,
  738. .irq_bit = PCR_N4_TOE,
  739. .upper_nop = 0,
  740. .lower_nop = 0,
  741. .flags = 0,
  742. .max_hw_events = 4,
  743. .num_pcrs = 4,
  744. .num_pic_regs = 4,
  745. };
  746. static const struct sparc_pmu *sparc_pmu __read_mostly;
  747. static u64 event_encoding(u64 event_id, int idx)
  748. {
  749. if (idx == PIC_UPPER_INDEX)
  750. event_id <<= sparc_pmu->upper_shift;
  751. else
  752. event_id <<= sparc_pmu->lower_shift;
  753. return event_id;
  754. }
  755. static u64 mask_for_index(int idx)
  756. {
  757. return event_encoding(sparc_pmu->event_mask, idx);
  758. }
  759. static u64 nop_for_index(int idx)
  760. {
  761. return event_encoding(idx == PIC_UPPER_INDEX ?
  762. sparc_pmu->upper_nop :
  763. sparc_pmu->lower_nop, idx);
  764. }
  765. static inline void sparc_pmu_enable_event(struct cpu_hw_events *cpuc, struct hw_perf_event *hwc, int idx)
  766. {
  767. u64 enc, val, mask = mask_for_index(idx);
  768. int pcr_index = 0;
  769. if (sparc_pmu->num_pcrs > 1)
  770. pcr_index = idx;
  771. enc = perf_event_get_enc(cpuc->events[idx]);
  772. val = cpuc->pcr[pcr_index];
  773. val &= ~mask;
  774. val |= event_encoding(enc, idx);
  775. cpuc->pcr[pcr_index] = val;
  776. pcr_ops->write_pcr(pcr_index, cpuc->pcr[pcr_index]);
  777. }
  778. static inline void sparc_pmu_disable_event(struct cpu_hw_events *cpuc, struct hw_perf_event *hwc, int idx)
  779. {
  780. u64 mask = mask_for_index(idx);
  781. u64 nop = nop_for_index(idx);
  782. int pcr_index = 0;
  783. u64 val;
  784. if (sparc_pmu->num_pcrs > 1)
  785. pcr_index = idx;
  786. val = cpuc->pcr[pcr_index];
  787. val &= ~mask;
  788. val |= nop;
  789. cpuc->pcr[pcr_index] = val;
  790. pcr_ops->write_pcr(pcr_index, cpuc->pcr[pcr_index]);
  791. }
  792. static u64 sparc_perf_event_update(struct perf_event *event,
  793. struct hw_perf_event *hwc, int idx)
  794. {
  795. int shift = 64 - 32;
  796. u64 prev_raw_count, new_raw_count;
  797. s64 delta;
  798. again:
  799. prev_raw_count = local64_read(&hwc->prev_count);
  800. new_raw_count = sparc_pmu->read_pmc(idx);
  801. if (local64_cmpxchg(&hwc->prev_count, prev_raw_count,
  802. new_raw_count) != prev_raw_count)
  803. goto again;
  804. delta = (new_raw_count << shift) - (prev_raw_count << shift);
  805. delta >>= shift;
  806. local64_add(delta, &event->count);
  807. local64_sub(delta, &hwc->period_left);
  808. return new_raw_count;
  809. }
  810. static int sparc_perf_event_set_period(struct perf_event *event,
  811. struct hw_perf_event *hwc, int idx)
  812. {
  813. s64 left = local64_read(&hwc->period_left);
  814. s64 period = hwc->sample_period;
  815. int ret = 0;
  816. if (unlikely(left <= -period)) {
  817. left = period;
  818. local64_set(&hwc->period_left, left);
  819. hwc->last_period = period;
  820. ret = 1;
  821. }
  822. if (unlikely(left <= 0)) {
  823. left += period;
  824. local64_set(&hwc->period_left, left);
  825. hwc->last_period = period;
  826. ret = 1;
  827. }
  828. if (left > MAX_PERIOD)
  829. left = MAX_PERIOD;
  830. local64_set(&hwc->prev_count, (u64)-left);
  831. sparc_pmu->write_pmc(idx, (u64)(-left) & 0xffffffff);
  832. perf_event_update_userpage(event);
  833. return ret;
  834. }
  835. static void read_in_all_counters(struct cpu_hw_events *cpuc)
  836. {
  837. int i;
  838. for (i = 0; i < cpuc->n_events; i++) {
  839. struct perf_event *cp = cpuc->event[i];
  840. if (cpuc->current_idx[i] != PIC_NO_INDEX &&
  841. cpuc->current_idx[i] != cp->hw.idx) {
  842. sparc_perf_event_update(cp, &cp->hw,
  843. cpuc->current_idx[i]);
  844. cpuc->current_idx[i] = PIC_NO_INDEX;
  845. }
  846. }
  847. }
  848. /* On this PMU all PICs are programmed using a single PCR. Calculate
  849. * the combined control register value.
  850. *
  851. * For such chips we require that all of the events have the same
  852. * configuration, so just fetch the settings from the first entry.
  853. */
  854. static void calculate_single_pcr(struct cpu_hw_events *cpuc)
  855. {
  856. int i;
  857. if (!cpuc->n_added)
  858. goto out;
  859. /* Assign to counters all unassigned events. */
  860. for (i = 0; i < cpuc->n_events; i++) {
  861. struct perf_event *cp = cpuc->event[i];
  862. struct hw_perf_event *hwc = &cp->hw;
  863. int idx = hwc->idx;
  864. u64 enc;
  865. if (cpuc->current_idx[i] != PIC_NO_INDEX)
  866. continue;
  867. sparc_perf_event_set_period(cp, hwc, idx);
  868. cpuc->current_idx[i] = idx;
  869. enc = perf_event_get_enc(cpuc->events[i]);
  870. cpuc->pcr[0] &= ~mask_for_index(idx);
  871. if (hwc->state & PERF_HES_STOPPED)
  872. cpuc->pcr[0] |= nop_for_index(idx);
  873. else
  874. cpuc->pcr[0] |= event_encoding(enc, idx);
  875. }
  876. out:
  877. cpuc->pcr[0] |= cpuc->event[0]->hw.config_base;
  878. }
  879. static void sparc_pmu_start(struct perf_event *event, int flags);
  880. /* On this PMU each PIC has it's own PCR control register. */
  881. static void calculate_multiple_pcrs(struct cpu_hw_events *cpuc)
  882. {
  883. int i;
  884. if (!cpuc->n_added)
  885. goto out;
  886. for (i = 0; i < cpuc->n_events; i++) {
  887. struct perf_event *cp = cpuc->event[i];
  888. struct hw_perf_event *hwc = &cp->hw;
  889. int idx = hwc->idx;
  890. if (cpuc->current_idx[i] != PIC_NO_INDEX)
  891. continue;
  892. cpuc->current_idx[i] = idx;
  893. sparc_pmu_start(cp, PERF_EF_RELOAD);
  894. }
  895. out:
  896. for (i = 0; i < cpuc->n_events; i++) {
  897. struct perf_event *cp = cpuc->event[i];
  898. int idx = cp->hw.idx;
  899. cpuc->pcr[idx] |= cp->hw.config_base;
  900. }
  901. }
  902. /* If performance event entries have been added, move existing events
  903. * around (if necessary) and then assign new entries to counters.
  904. */
  905. static void update_pcrs_for_enable(struct cpu_hw_events *cpuc)
  906. {
  907. if (cpuc->n_added)
  908. read_in_all_counters(cpuc);
  909. if (sparc_pmu->num_pcrs == 1) {
  910. calculate_single_pcr(cpuc);
  911. } else {
  912. calculate_multiple_pcrs(cpuc);
  913. }
  914. }
  915. static void sparc_pmu_enable(struct pmu *pmu)
  916. {
  917. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  918. int i;
  919. if (cpuc->enabled)
  920. return;
  921. cpuc->enabled = 1;
  922. barrier();
  923. if (cpuc->n_events)
  924. update_pcrs_for_enable(cpuc);
  925. for (i = 0; i < sparc_pmu->num_pcrs; i++)
  926. pcr_ops->write_pcr(i, cpuc->pcr[i]);
  927. }
  928. static void sparc_pmu_disable(struct pmu *pmu)
  929. {
  930. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  931. int i;
  932. if (!cpuc->enabled)
  933. return;
  934. cpuc->enabled = 0;
  935. cpuc->n_added = 0;
  936. for (i = 0; i < sparc_pmu->num_pcrs; i++) {
  937. u64 val = cpuc->pcr[i];
  938. val &= ~(sparc_pmu->user_bit | sparc_pmu->priv_bit |
  939. sparc_pmu->hv_bit | sparc_pmu->irq_bit);
  940. cpuc->pcr[i] = val;
  941. pcr_ops->write_pcr(i, cpuc->pcr[i]);
  942. }
  943. }
  944. static int active_event_index(struct cpu_hw_events *cpuc,
  945. struct perf_event *event)
  946. {
  947. int i;
  948. for (i = 0; i < cpuc->n_events; i++) {
  949. if (cpuc->event[i] == event)
  950. break;
  951. }
  952. BUG_ON(i == cpuc->n_events);
  953. return cpuc->current_idx[i];
  954. }
  955. static void sparc_pmu_start(struct perf_event *event, int flags)
  956. {
  957. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  958. int idx = active_event_index(cpuc, event);
  959. if (flags & PERF_EF_RELOAD) {
  960. WARN_ON_ONCE(!(event->hw.state & PERF_HES_UPTODATE));
  961. sparc_perf_event_set_period(event, &event->hw, idx);
  962. }
  963. event->hw.state = 0;
  964. sparc_pmu_enable_event(cpuc, &event->hw, idx);
  965. }
  966. static void sparc_pmu_stop(struct perf_event *event, int flags)
  967. {
  968. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  969. int idx = active_event_index(cpuc, event);
  970. if (!(event->hw.state & PERF_HES_STOPPED)) {
  971. sparc_pmu_disable_event(cpuc, &event->hw, idx);
  972. event->hw.state |= PERF_HES_STOPPED;
  973. }
  974. if (!(event->hw.state & PERF_HES_UPTODATE) && (flags & PERF_EF_UPDATE)) {
  975. sparc_perf_event_update(event, &event->hw, idx);
  976. event->hw.state |= PERF_HES_UPTODATE;
  977. }
  978. }
  979. static void sparc_pmu_del(struct perf_event *event, int _flags)
  980. {
  981. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  982. unsigned long flags;
  983. int i;
  984. local_irq_save(flags);
  985. for (i = 0; i < cpuc->n_events; i++) {
  986. if (event == cpuc->event[i]) {
  987. /* Absorb the final count and turn off the
  988. * event.
  989. */
  990. sparc_pmu_stop(event, PERF_EF_UPDATE);
  991. /* Shift remaining entries down into
  992. * the existing slot.
  993. */
  994. while (++i < cpuc->n_events) {
  995. cpuc->event[i - 1] = cpuc->event[i];
  996. cpuc->events[i - 1] = cpuc->events[i];
  997. cpuc->current_idx[i - 1] =
  998. cpuc->current_idx[i];
  999. }
  1000. perf_event_update_userpage(event);
  1001. cpuc->n_events--;
  1002. break;
  1003. }
  1004. }
  1005. local_irq_restore(flags);
  1006. }
  1007. static void sparc_pmu_read(struct perf_event *event)
  1008. {
  1009. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  1010. int idx = active_event_index(cpuc, event);
  1011. struct hw_perf_event *hwc = &event->hw;
  1012. sparc_perf_event_update(event, hwc, idx);
  1013. }
  1014. static atomic_t active_events = ATOMIC_INIT(0);
  1015. static DEFINE_MUTEX(pmc_grab_mutex);
  1016. static void perf_stop_nmi_watchdog(void *unused)
  1017. {
  1018. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  1019. int i;
  1020. stop_nmi_watchdog(NULL);
  1021. for (i = 0; i < sparc_pmu->num_pcrs; i++)
  1022. cpuc->pcr[i] = pcr_ops->read_pcr(i);
  1023. }
  1024. static void perf_event_grab_pmc(void)
  1025. {
  1026. if (atomic_inc_not_zero(&active_events))
  1027. return;
  1028. mutex_lock(&pmc_grab_mutex);
  1029. if (atomic_read(&active_events) == 0) {
  1030. if (atomic_read(&nmi_active) > 0) {
  1031. on_each_cpu(perf_stop_nmi_watchdog, NULL, 1);
  1032. BUG_ON(atomic_read(&nmi_active) != 0);
  1033. }
  1034. atomic_inc(&active_events);
  1035. }
  1036. mutex_unlock(&pmc_grab_mutex);
  1037. }
  1038. static void perf_event_release_pmc(void)
  1039. {
  1040. if (atomic_dec_and_mutex_lock(&active_events, &pmc_grab_mutex)) {
  1041. if (atomic_read(&nmi_active) == 0)
  1042. on_each_cpu(start_nmi_watchdog, NULL, 1);
  1043. mutex_unlock(&pmc_grab_mutex);
  1044. }
  1045. }
  1046. static const struct perf_event_map *sparc_map_cache_event(u64 config)
  1047. {
  1048. unsigned int cache_type, cache_op, cache_result;
  1049. const struct perf_event_map *pmap;
  1050. if (!sparc_pmu->cache_map)
  1051. return ERR_PTR(-ENOENT);
  1052. cache_type = (config >> 0) & 0xff;
  1053. if (cache_type >= PERF_COUNT_HW_CACHE_MAX)
  1054. return ERR_PTR(-EINVAL);
  1055. cache_op = (config >> 8) & 0xff;
  1056. if (cache_op >= PERF_COUNT_HW_CACHE_OP_MAX)
  1057. return ERR_PTR(-EINVAL);
  1058. cache_result = (config >> 16) & 0xff;
  1059. if (cache_result >= PERF_COUNT_HW_CACHE_RESULT_MAX)
  1060. return ERR_PTR(-EINVAL);
  1061. pmap = &((*sparc_pmu->cache_map)[cache_type][cache_op][cache_result]);
  1062. if (pmap->encoding == CACHE_OP_UNSUPPORTED)
  1063. return ERR_PTR(-ENOENT);
  1064. if (pmap->encoding == CACHE_OP_NONSENSE)
  1065. return ERR_PTR(-EINVAL);
  1066. return pmap;
  1067. }
  1068. static void hw_perf_event_destroy(struct perf_event *event)
  1069. {
  1070. perf_event_release_pmc();
  1071. }
  1072. /* Make sure all events can be scheduled into the hardware at
  1073. * the same time. This is simplified by the fact that we only
  1074. * need to support 2 simultaneous HW events.
  1075. *
  1076. * As a side effect, the evts[]->hw.idx values will be assigned
  1077. * on success. These are pending indexes. When the events are
  1078. * actually programmed into the chip, these values will propagate
  1079. * to the per-cpu cpuc->current_idx[] slots, see the code in
  1080. * maybe_change_configuration() for details.
  1081. */
  1082. static int sparc_check_constraints(struct perf_event **evts,
  1083. unsigned long *events, int n_ev)
  1084. {
  1085. u8 msk0 = 0, msk1 = 0;
  1086. int idx0 = 0;
  1087. /* This case is possible when we are invoked from
  1088. * hw_perf_group_sched_in().
  1089. */
  1090. if (!n_ev)
  1091. return 0;
  1092. if (n_ev > sparc_pmu->max_hw_events)
  1093. return -1;
  1094. if (!(sparc_pmu->flags & SPARC_PMU_HAS_CONFLICTS)) {
  1095. int i;
  1096. for (i = 0; i < n_ev; i++)
  1097. evts[i]->hw.idx = i;
  1098. return 0;
  1099. }
  1100. msk0 = perf_event_get_msk(events[0]);
  1101. if (n_ev == 1) {
  1102. if (msk0 & PIC_LOWER)
  1103. idx0 = 1;
  1104. goto success;
  1105. }
  1106. BUG_ON(n_ev != 2);
  1107. msk1 = perf_event_get_msk(events[1]);
  1108. /* If both events can go on any counter, OK. */
  1109. if (msk0 == (PIC_UPPER | PIC_LOWER) &&
  1110. msk1 == (PIC_UPPER | PIC_LOWER))
  1111. goto success;
  1112. /* If one event is limited to a specific counter,
  1113. * and the other can go on both, OK.
  1114. */
  1115. if ((msk0 == PIC_UPPER || msk0 == PIC_LOWER) &&
  1116. msk1 == (PIC_UPPER | PIC_LOWER)) {
  1117. if (msk0 & PIC_LOWER)
  1118. idx0 = 1;
  1119. goto success;
  1120. }
  1121. if ((msk1 == PIC_UPPER || msk1 == PIC_LOWER) &&
  1122. msk0 == (PIC_UPPER | PIC_LOWER)) {
  1123. if (msk1 & PIC_UPPER)
  1124. idx0 = 1;
  1125. goto success;
  1126. }
  1127. /* If the events are fixed to different counters, OK. */
  1128. if ((msk0 == PIC_UPPER && msk1 == PIC_LOWER) ||
  1129. (msk0 == PIC_LOWER && msk1 == PIC_UPPER)) {
  1130. if (msk0 & PIC_LOWER)
  1131. idx0 = 1;
  1132. goto success;
  1133. }
  1134. /* Otherwise, there is a conflict. */
  1135. return -1;
  1136. success:
  1137. evts[0]->hw.idx = idx0;
  1138. if (n_ev == 2)
  1139. evts[1]->hw.idx = idx0 ^ 1;
  1140. return 0;
  1141. }
  1142. static int check_excludes(struct perf_event **evts, int n_prev, int n_new)
  1143. {
  1144. int eu = 0, ek = 0, eh = 0;
  1145. struct perf_event *event;
  1146. int i, n, first;
  1147. if (!(sparc_pmu->flags & SPARC_PMU_ALL_EXCLUDES_SAME))
  1148. return 0;
  1149. n = n_prev + n_new;
  1150. if (n <= 1)
  1151. return 0;
  1152. first = 1;
  1153. for (i = 0; i < n; i++) {
  1154. event = evts[i];
  1155. if (first) {
  1156. eu = event->attr.exclude_user;
  1157. ek = event->attr.exclude_kernel;
  1158. eh = event->attr.exclude_hv;
  1159. first = 0;
  1160. } else if (event->attr.exclude_user != eu ||
  1161. event->attr.exclude_kernel != ek ||
  1162. event->attr.exclude_hv != eh) {
  1163. return -EAGAIN;
  1164. }
  1165. }
  1166. return 0;
  1167. }
  1168. static int collect_events(struct perf_event *group, int max_count,
  1169. struct perf_event *evts[], unsigned long *events,
  1170. int *current_idx)
  1171. {
  1172. struct perf_event *event;
  1173. int n = 0;
  1174. if (!is_software_event(group)) {
  1175. if (n >= max_count)
  1176. return -1;
  1177. evts[n] = group;
  1178. events[n] = group->hw.event_base;
  1179. current_idx[n++] = PIC_NO_INDEX;
  1180. }
  1181. list_for_each_entry(event, &group->sibling_list, group_entry) {
  1182. if (!is_software_event(event) &&
  1183. event->state != PERF_EVENT_STATE_OFF) {
  1184. if (n >= max_count)
  1185. return -1;
  1186. evts[n] = event;
  1187. events[n] = event->hw.event_base;
  1188. current_idx[n++] = PIC_NO_INDEX;
  1189. }
  1190. }
  1191. return n;
  1192. }
  1193. static int sparc_pmu_add(struct perf_event *event, int ef_flags)
  1194. {
  1195. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  1196. int n0, ret = -EAGAIN;
  1197. unsigned long flags;
  1198. local_irq_save(flags);
  1199. n0 = cpuc->n_events;
  1200. if (n0 >= sparc_pmu->max_hw_events)
  1201. goto out;
  1202. cpuc->event[n0] = event;
  1203. cpuc->events[n0] = event->hw.event_base;
  1204. cpuc->current_idx[n0] = PIC_NO_INDEX;
  1205. event->hw.state = PERF_HES_UPTODATE;
  1206. if (!(ef_flags & PERF_EF_START))
  1207. event->hw.state |= PERF_HES_STOPPED;
  1208. /*
  1209. * If group events scheduling transaction was started,
  1210. * skip the schedulability test here, it will be performed
  1211. * at commit time(->commit_txn) as a whole
  1212. */
  1213. if (cpuc->txn_flags & PERF_PMU_TXN_ADD)
  1214. goto nocheck;
  1215. if (check_excludes(cpuc->event, n0, 1))
  1216. goto out;
  1217. if (sparc_check_constraints(cpuc->event, cpuc->events, n0 + 1))
  1218. goto out;
  1219. nocheck:
  1220. cpuc->n_events++;
  1221. cpuc->n_added++;
  1222. ret = 0;
  1223. out:
  1224. local_irq_restore(flags);
  1225. return ret;
  1226. }
  1227. static int sparc_pmu_event_init(struct perf_event *event)
  1228. {
  1229. struct perf_event_attr *attr = &event->attr;
  1230. struct perf_event *evts[MAX_HWEVENTS];
  1231. struct hw_perf_event *hwc = &event->hw;
  1232. unsigned long events[MAX_HWEVENTS];
  1233. int current_idx_dmy[MAX_HWEVENTS];
  1234. const struct perf_event_map *pmap;
  1235. int n;
  1236. if (atomic_read(&nmi_active) < 0)
  1237. return -ENODEV;
  1238. /* does not support taken branch sampling */
  1239. if (has_branch_stack(event))
  1240. return -EOPNOTSUPP;
  1241. switch (attr->type) {
  1242. case PERF_TYPE_HARDWARE:
  1243. if (attr->config >= sparc_pmu->max_events)
  1244. return -EINVAL;
  1245. pmap = sparc_pmu->event_map(attr->config);
  1246. break;
  1247. case PERF_TYPE_HW_CACHE:
  1248. pmap = sparc_map_cache_event(attr->config);
  1249. if (IS_ERR(pmap))
  1250. return PTR_ERR(pmap);
  1251. break;
  1252. case PERF_TYPE_RAW:
  1253. pmap = NULL;
  1254. break;
  1255. default:
  1256. return -ENOENT;
  1257. }
  1258. if (pmap) {
  1259. hwc->event_base = perf_event_encode(pmap);
  1260. } else {
  1261. /*
  1262. * User gives us "(encoding << 16) | pic_mask" for
  1263. * PERF_TYPE_RAW events.
  1264. */
  1265. hwc->event_base = attr->config;
  1266. }
  1267. /* We save the enable bits in the config_base. */
  1268. hwc->config_base = sparc_pmu->irq_bit;
  1269. if (!attr->exclude_user)
  1270. hwc->config_base |= sparc_pmu->user_bit;
  1271. if (!attr->exclude_kernel)
  1272. hwc->config_base |= sparc_pmu->priv_bit;
  1273. if (!attr->exclude_hv)
  1274. hwc->config_base |= sparc_pmu->hv_bit;
  1275. n = 0;
  1276. if (event->group_leader != event) {
  1277. n = collect_events(event->group_leader,
  1278. sparc_pmu->max_hw_events - 1,
  1279. evts, events, current_idx_dmy);
  1280. if (n < 0)
  1281. return -EINVAL;
  1282. }
  1283. events[n] = hwc->event_base;
  1284. evts[n] = event;
  1285. if (check_excludes(evts, n, 1))
  1286. return -EINVAL;
  1287. if (sparc_check_constraints(evts, events, n + 1))
  1288. return -EINVAL;
  1289. hwc->idx = PIC_NO_INDEX;
  1290. /* Try to do all error checking before this point, as unwinding
  1291. * state after grabbing the PMC is difficult.
  1292. */
  1293. perf_event_grab_pmc();
  1294. event->destroy = hw_perf_event_destroy;
  1295. if (!hwc->sample_period) {
  1296. hwc->sample_period = MAX_PERIOD;
  1297. hwc->last_period = hwc->sample_period;
  1298. local64_set(&hwc->period_left, hwc->sample_period);
  1299. }
  1300. return 0;
  1301. }
  1302. /*
  1303. * Start group events scheduling transaction
  1304. * Set the flag to make pmu::enable() not perform the
  1305. * schedulability test, it will be performed at commit time
  1306. */
  1307. static void sparc_pmu_start_txn(struct pmu *pmu, unsigned int txn_flags)
  1308. {
  1309. struct cpu_hw_events *cpuhw = this_cpu_ptr(&cpu_hw_events);
  1310. WARN_ON_ONCE(cpuhw->txn_flags); /* txn already in flight */
  1311. cpuhw->txn_flags = txn_flags;
  1312. if (txn_flags & ~PERF_PMU_TXN_ADD)
  1313. return;
  1314. perf_pmu_disable(pmu);
  1315. }
  1316. /*
  1317. * Stop group events scheduling transaction
  1318. * Clear the flag and pmu::enable() will perform the
  1319. * schedulability test.
  1320. */
  1321. static void sparc_pmu_cancel_txn(struct pmu *pmu)
  1322. {
  1323. struct cpu_hw_events *cpuhw = this_cpu_ptr(&cpu_hw_events);
  1324. unsigned int txn_flags;
  1325. WARN_ON_ONCE(!cpuhw->txn_flags); /* no txn in flight */
  1326. txn_flags = cpuhw->txn_flags;
  1327. cpuhw->txn_flags = 0;
  1328. if (txn_flags & ~PERF_PMU_TXN_ADD)
  1329. return;
  1330. perf_pmu_enable(pmu);
  1331. }
  1332. /*
  1333. * Commit group events scheduling transaction
  1334. * Perform the group schedulability test as a whole
  1335. * Return 0 if success
  1336. */
  1337. static int sparc_pmu_commit_txn(struct pmu *pmu)
  1338. {
  1339. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  1340. int n;
  1341. if (!sparc_pmu)
  1342. return -EINVAL;
  1343. WARN_ON_ONCE(!cpuc->txn_flags); /* no txn in flight */
  1344. if (cpuc->txn_flags & ~PERF_PMU_TXN_ADD) {
  1345. cpuc->txn_flags = 0;
  1346. return 0;
  1347. }
  1348. n = cpuc->n_events;
  1349. if (check_excludes(cpuc->event, 0, n))
  1350. return -EINVAL;
  1351. if (sparc_check_constraints(cpuc->event, cpuc->events, n))
  1352. return -EAGAIN;
  1353. cpuc->txn_flags = 0;
  1354. perf_pmu_enable(pmu);
  1355. return 0;
  1356. }
  1357. static struct pmu pmu = {
  1358. .pmu_enable = sparc_pmu_enable,
  1359. .pmu_disable = sparc_pmu_disable,
  1360. .event_init = sparc_pmu_event_init,
  1361. .add = sparc_pmu_add,
  1362. .del = sparc_pmu_del,
  1363. .start = sparc_pmu_start,
  1364. .stop = sparc_pmu_stop,
  1365. .read = sparc_pmu_read,
  1366. .start_txn = sparc_pmu_start_txn,
  1367. .cancel_txn = sparc_pmu_cancel_txn,
  1368. .commit_txn = sparc_pmu_commit_txn,
  1369. };
  1370. void perf_event_print_debug(void)
  1371. {
  1372. unsigned long flags;
  1373. int cpu, i;
  1374. if (!sparc_pmu)
  1375. return;
  1376. local_irq_save(flags);
  1377. cpu = smp_processor_id();
  1378. pr_info("\n");
  1379. for (i = 0; i < sparc_pmu->num_pcrs; i++)
  1380. pr_info("CPU#%d: PCR%d[%016llx]\n",
  1381. cpu, i, pcr_ops->read_pcr(i));
  1382. for (i = 0; i < sparc_pmu->num_pic_regs; i++)
  1383. pr_info("CPU#%d: PIC%d[%016llx]\n",
  1384. cpu, i, pcr_ops->read_pic(i));
  1385. local_irq_restore(flags);
  1386. }
  1387. static int __kprobes perf_event_nmi_handler(struct notifier_block *self,
  1388. unsigned long cmd, void *__args)
  1389. {
  1390. struct die_args *args = __args;
  1391. struct perf_sample_data data;
  1392. struct cpu_hw_events *cpuc;
  1393. struct pt_regs *regs;
  1394. int i;
  1395. if (!atomic_read(&active_events))
  1396. return NOTIFY_DONE;
  1397. switch (cmd) {
  1398. case DIE_NMI:
  1399. break;
  1400. default:
  1401. return NOTIFY_DONE;
  1402. }
  1403. regs = args->regs;
  1404. cpuc = this_cpu_ptr(&cpu_hw_events);
  1405. /* If the PMU has the TOE IRQ enable bits, we need to do a
  1406. * dummy write to the %pcr to clear the overflow bits and thus
  1407. * the interrupt.
  1408. *
  1409. * Do this before we peek at the counters to determine
  1410. * overflow so we don't lose any events.
  1411. */
  1412. if (sparc_pmu->irq_bit &&
  1413. sparc_pmu->num_pcrs == 1)
  1414. pcr_ops->write_pcr(0, cpuc->pcr[0]);
  1415. for (i = 0; i < cpuc->n_events; i++) {
  1416. struct perf_event *event = cpuc->event[i];
  1417. int idx = cpuc->current_idx[i];
  1418. struct hw_perf_event *hwc;
  1419. u64 val;
  1420. if (sparc_pmu->irq_bit &&
  1421. sparc_pmu->num_pcrs > 1)
  1422. pcr_ops->write_pcr(idx, cpuc->pcr[idx]);
  1423. hwc = &event->hw;
  1424. val = sparc_perf_event_update(event, hwc, idx);
  1425. if (val & (1ULL << 31))
  1426. continue;
  1427. perf_sample_data_init(&data, 0, hwc->last_period);
  1428. if (!sparc_perf_event_set_period(event, hwc, idx))
  1429. continue;
  1430. if (perf_event_overflow(event, &data, regs))
  1431. sparc_pmu_stop(event, 0);
  1432. }
  1433. return NOTIFY_STOP;
  1434. }
  1435. static __read_mostly struct notifier_block perf_event_nmi_notifier = {
  1436. .notifier_call = perf_event_nmi_handler,
  1437. };
  1438. static bool __init supported_pmu(void)
  1439. {
  1440. if (!strcmp(sparc_pmu_type, "ultra3") ||
  1441. !strcmp(sparc_pmu_type, "ultra3+") ||
  1442. !strcmp(sparc_pmu_type, "ultra3i") ||
  1443. !strcmp(sparc_pmu_type, "ultra4+")) {
  1444. sparc_pmu = &ultra3_pmu;
  1445. return true;
  1446. }
  1447. if (!strcmp(sparc_pmu_type, "niagara")) {
  1448. sparc_pmu = &niagara1_pmu;
  1449. return true;
  1450. }
  1451. if (!strcmp(sparc_pmu_type, "niagara2") ||
  1452. !strcmp(sparc_pmu_type, "niagara3")) {
  1453. sparc_pmu = &niagara2_pmu;
  1454. return true;
  1455. }
  1456. if (!strcmp(sparc_pmu_type, "niagara4") ||
  1457. !strcmp(sparc_pmu_type, "niagara5")) {
  1458. sparc_pmu = &niagara4_pmu;
  1459. return true;
  1460. }
  1461. if (!strcmp(sparc_pmu_type, "sparc-m7")) {
  1462. sparc_pmu = &sparc_m7_pmu;
  1463. return true;
  1464. }
  1465. return false;
  1466. }
  1467. static int __init init_hw_perf_events(void)
  1468. {
  1469. int err;
  1470. pr_info("Performance events: ");
  1471. err = pcr_arch_init();
  1472. if (err || !supported_pmu()) {
  1473. pr_cont("No support for PMU type '%s'\n", sparc_pmu_type);
  1474. return 0;
  1475. }
  1476. pr_cont("Supported PMU type is '%s'\n", sparc_pmu_type);
  1477. perf_pmu_register(&pmu, "cpu", PERF_TYPE_RAW);
  1478. register_die_notifier(&perf_event_nmi_notifier);
  1479. return 0;
  1480. }
  1481. pure_initcall(init_hw_perf_events);
  1482. void perf_callchain_kernel(struct perf_callchain_entry_ctx *entry,
  1483. struct pt_regs *regs)
  1484. {
  1485. unsigned long ksp, fp;
  1486. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  1487. int graph = 0;
  1488. #endif
  1489. stack_trace_flush();
  1490. perf_callchain_store(entry, regs->tpc);
  1491. ksp = regs->u_regs[UREG_I6];
  1492. fp = ksp + STACK_BIAS;
  1493. do {
  1494. struct sparc_stackf *sf;
  1495. struct pt_regs *regs;
  1496. unsigned long pc;
  1497. if (!kstack_valid(current_thread_info(), fp))
  1498. break;
  1499. sf = (struct sparc_stackf *) fp;
  1500. regs = (struct pt_regs *) (sf + 1);
  1501. if (kstack_is_trap_frame(current_thread_info(), regs)) {
  1502. if (user_mode(regs))
  1503. break;
  1504. pc = regs->tpc;
  1505. fp = regs->u_regs[UREG_I6] + STACK_BIAS;
  1506. } else {
  1507. pc = sf->callers_pc;
  1508. fp = (unsigned long)sf->fp + STACK_BIAS;
  1509. }
  1510. perf_callchain_store(entry, pc);
  1511. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  1512. if ((pc + 8UL) == (unsigned long) &return_to_handler) {
  1513. int index = current->curr_ret_stack;
  1514. if (current->ret_stack && index >= graph) {
  1515. pc = current->ret_stack[index - graph].ret;
  1516. perf_callchain_store(entry, pc);
  1517. graph++;
  1518. }
  1519. }
  1520. #endif
  1521. } while (entry->nr < entry->max_stack);
  1522. }
  1523. static inline int
  1524. valid_user_frame(const void __user *fp, unsigned long size)
  1525. {
  1526. /* addresses should be at least 4-byte aligned */
  1527. if (((unsigned long) fp) & 3)
  1528. return 0;
  1529. return (__range_not_ok(fp, size, TASK_SIZE) == 0);
  1530. }
  1531. static void perf_callchain_user_64(struct perf_callchain_entry_ctx *entry,
  1532. struct pt_regs *regs)
  1533. {
  1534. unsigned long ufp;
  1535. ufp = regs->u_regs[UREG_FP] + STACK_BIAS;
  1536. do {
  1537. struct sparc_stackf __user *usf;
  1538. struct sparc_stackf sf;
  1539. unsigned long pc;
  1540. usf = (struct sparc_stackf __user *)ufp;
  1541. if (!valid_user_frame(usf, sizeof(sf)))
  1542. break;
  1543. if (__copy_from_user_inatomic(&sf, usf, sizeof(sf)))
  1544. break;
  1545. pc = sf.callers_pc;
  1546. ufp = (unsigned long)sf.fp + STACK_BIAS;
  1547. perf_callchain_store(entry, pc);
  1548. } while (entry->nr < entry->max_stack);
  1549. }
  1550. static void perf_callchain_user_32(struct perf_callchain_entry_ctx *entry,
  1551. struct pt_regs *regs)
  1552. {
  1553. unsigned long ufp;
  1554. ufp = regs->u_regs[UREG_FP] & 0xffffffffUL;
  1555. do {
  1556. unsigned long pc;
  1557. if (thread32_stack_is_64bit(ufp)) {
  1558. struct sparc_stackf __user *usf;
  1559. struct sparc_stackf sf;
  1560. ufp += STACK_BIAS;
  1561. usf = (struct sparc_stackf __user *)ufp;
  1562. if (__copy_from_user_inatomic(&sf, usf, sizeof(sf)))
  1563. break;
  1564. pc = sf.callers_pc & 0xffffffff;
  1565. ufp = ((unsigned long) sf.fp) & 0xffffffff;
  1566. } else {
  1567. struct sparc_stackf32 __user *usf;
  1568. struct sparc_stackf32 sf;
  1569. usf = (struct sparc_stackf32 __user *)ufp;
  1570. if (__copy_from_user_inatomic(&sf, usf, sizeof(sf)))
  1571. break;
  1572. pc = sf.callers_pc;
  1573. ufp = (unsigned long)sf.fp;
  1574. }
  1575. perf_callchain_store(entry, pc);
  1576. } while (entry->nr < entry->max_stack);
  1577. }
  1578. void
  1579. perf_callchain_user(struct perf_callchain_entry_ctx *entry, struct pt_regs *regs)
  1580. {
  1581. u64 saved_fault_address = current_thread_info()->fault_address;
  1582. u8 saved_fault_code = get_thread_fault_code();
  1583. mm_segment_t old_fs;
  1584. perf_callchain_store(entry, regs->tpc);
  1585. if (!current->mm)
  1586. return;
  1587. old_fs = get_fs();
  1588. set_fs(USER_DS);
  1589. flushw_user();
  1590. pagefault_disable();
  1591. if (test_thread_flag(TIF_32BIT))
  1592. perf_callchain_user_32(entry, regs);
  1593. else
  1594. perf_callchain_user_64(entry, regs);
  1595. pagefault_enable();
  1596. set_fs(old_fs);
  1597. set_thread_fault_code(saved_fault_code);
  1598. current_thread_info()->fault_address = saved_fault_address;
  1599. }