perf_event.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801
  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 <pzijlstr@redhat.com>
  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 <asm/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 group_flag;
  98. };
  99. 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. /* There seems to be an internal latch on the overflow event
  693. * on SPARC-T4 that prevents it from triggering unless you
  694. * update the PIC exactly as we do here. The requirement
  695. * seems to be that you have to turn off event counting in the
  696. * PCR around the PIC update.
  697. *
  698. * For example, after the following sequence:
  699. *
  700. * 1) set PIC to -1
  701. * 2) enable event counting and overflow reporting in PCR
  702. * 3) overflow triggers, softint 15 handler invoked
  703. * 4) clear OV bit in PCR
  704. * 5) write PIC to -1
  705. *
  706. * a subsequent overflow event will not trigger. This
  707. * sequence works on SPARC-T3 and previous chips.
  708. */
  709. pcr = pcr_ops->read_pcr(idx);
  710. pcr_ops->write_pcr(idx, PCR_N4_PICNPT);
  711. pcr_ops->write_pic(idx, val & 0xffffffff);
  712. pcr_ops->write_pcr(idx, pcr);
  713. }
  714. static const struct sparc_pmu niagara4_pmu = {
  715. .event_map = niagara4_event_map,
  716. .cache_map = &niagara4_cache_map,
  717. .max_events = ARRAY_SIZE(niagara4_perfmon_event_map),
  718. .read_pmc = sparc_vt_read_pmc,
  719. .write_pmc = sparc_vt_write_pmc,
  720. .upper_shift = 5,
  721. .lower_shift = 5,
  722. .event_mask = 0x7ff,
  723. .user_bit = PCR_N4_UTRACE,
  724. .priv_bit = PCR_N4_STRACE,
  725. /* We explicitly don't support hypervisor tracing. The T4
  726. * generates the overflow event for precise events via a trap
  727. * which will not be generated (ie. it's completely lost) if
  728. * we happen to be in the hypervisor when the event triggers.
  729. * Essentially, the overflow event reporting is completely
  730. * unusable when you have hypervisor mode tracing enabled.
  731. */
  732. .hv_bit = 0,
  733. .irq_bit = PCR_N4_TOE,
  734. .upper_nop = 0,
  735. .lower_nop = 0,
  736. .flags = 0,
  737. .max_hw_events = 4,
  738. .num_pcrs = 4,
  739. .num_pic_regs = 4,
  740. };
  741. static const struct sparc_pmu *sparc_pmu __read_mostly;
  742. static u64 event_encoding(u64 event_id, int idx)
  743. {
  744. if (idx == PIC_UPPER_INDEX)
  745. event_id <<= sparc_pmu->upper_shift;
  746. else
  747. event_id <<= sparc_pmu->lower_shift;
  748. return event_id;
  749. }
  750. static u64 mask_for_index(int idx)
  751. {
  752. return event_encoding(sparc_pmu->event_mask, idx);
  753. }
  754. static u64 nop_for_index(int idx)
  755. {
  756. return event_encoding(idx == PIC_UPPER_INDEX ?
  757. sparc_pmu->upper_nop :
  758. sparc_pmu->lower_nop, idx);
  759. }
  760. static inline void sparc_pmu_enable_event(struct cpu_hw_events *cpuc, struct hw_perf_event *hwc, int idx)
  761. {
  762. u64 enc, val, mask = mask_for_index(idx);
  763. int pcr_index = 0;
  764. if (sparc_pmu->num_pcrs > 1)
  765. pcr_index = idx;
  766. enc = perf_event_get_enc(cpuc->events[idx]);
  767. val = cpuc->pcr[pcr_index];
  768. val &= ~mask;
  769. val |= event_encoding(enc, idx);
  770. cpuc->pcr[pcr_index] = val;
  771. pcr_ops->write_pcr(pcr_index, cpuc->pcr[pcr_index]);
  772. }
  773. static inline void sparc_pmu_disable_event(struct cpu_hw_events *cpuc, struct hw_perf_event *hwc, int idx)
  774. {
  775. u64 mask = mask_for_index(idx);
  776. u64 nop = nop_for_index(idx);
  777. int pcr_index = 0;
  778. u64 val;
  779. if (sparc_pmu->num_pcrs > 1)
  780. pcr_index = idx;
  781. val = cpuc->pcr[pcr_index];
  782. val &= ~mask;
  783. val |= nop;
  784. cpuc->pcr[pcr_index] = val;
  785. pcr_ops->write_pcr(pcr_index, cpuc->pcr[pcr_index]);
  786. }
  787. static u64 sparc_perf_event_update(struct perf_event *event,
  788. struct hw_perf_event *hwc, int idx)
  789. {
  790. int shift = 64 - 32;
  791. u64 prev_raw_count, new_raw_count;
  792. s64 delta;
  793. again:
  794. prev_raw_count = local64_read(&hwc->prev_count);
  795. new_raw_count = sparc_pmu->read_pmc(idx);
  796. if (local64_cmpxchg(&hwc->prev_count, prev_raw_count,
  797. new_raw_count) != prev_raw_count)
  798. goto again;
  799. delta = (new_raw_count << shift) - (prev_raw_count << shift);
  800. delta >>= shift;
  801. local64_add(delta, &event->count);
  802. local64_sub(delta, &hwc->period_left);
  803. return new_raw_count;
  804. }
  805. static int sparc_perf_event_set_period(struct perf_event *event,
  806. struct hw_perf_event *hwc, int idx)
  807. {
  808. s64 left = local64_read(&hwc->period_left);
  809. s64 period = hwc->sample_period;
  810. int ret = 0;
  811. if (unlikely(left <= -period)) {
  812. left = period;
  813. local64_set(&hwc->period_left, left);
  814. hwc->last_period = period;
  815. ret = 1;
  816. }
  817. if (unlikely(left <= 0)) {
  818. left += period;
  819. local64_set(&hwc->period_left, left);
  820. hwc->last_period = period;
  821. ret = 1;
  822. }
  823. if (left > MAX_PERIOD)
  824. left = MAX_PERIOD;
  825. local64_set(&hwc->prev_count, (u64)-left);
  826. sparc_pmu->write_pmc(idx, (u64)(-left) & 0xffffffff);
  827. perf_event_update_userpage(event);
  828. return ret;
  829. }
  830. static void read_in_all_counters(struct cpu_hw_events *cpuc)
  831. {
  832. int i;
  833. for (i = 0; i < cpuc->n_events; i++) {
  834. struct perf_event *cp = cpuc->event[i];
  835. if (cpuc->current_idx[i] != PIC_NO_INDEX &&
  836. cpuc->current_idx[i] != cp->hw.idx) {
  837. sparc_perf_event_update(cp, &cp->hw,
  838. cpuc->current_idx[i]);
  839. cpuc->current_idx[i] = PIC_NO_INDEX;
  840. }
  841. }
  842. }
  843. /* On this PMU all PICs are programmed using a single PCR. Calculate
  844. * the combined control register value.
  845. *
  846. * For such chips we require that all of the events have the same
  847. * configuration, so just fetch the settings from the first entry.
  848. */
  849. static void calculate_single_pcr(struct cpu_hw_events *cpuc)
  850. {
  851. int i;
  852. if (!cpuc->n_added)
  853. goto out;
  854. /* Assign to counters all unassigned events. */
  855. for (i = 0; i < cpuc->n_events; i++) {
  856. struct perf_event *cp = cpuc->event[i];
  857. struct hw_perf_event *hwc = &cp->hw;
  858. int idx = hwc->idx;
  859. u64 enc;
  860. if (cpuc->current_idx[i] != PIC_NO_INDEX)
  861. continue;
  862. sparc_perf_event_set_period(cp, hwc, idx);
  863. cpuc->current_idx[i] = idx;
  864. enc = perf_event_get_enc(cpuc->events[i]);
  865. cpuc->pcr[0] &= ~mask_for_index(idx);
  866. if (hwc->state & PERF_HES_STOPPED)
  867. cpuc->pcr[0] |= nop_for_index(idx);
  868. else
  869. cpuc->pcr[0] |= event_encoding(enc, idx);
  870. }
  871. out:
  872. cpuc->pcr[0] |= cpuc->event[0]->hw.config_base;
  873. }
  874. /* On this PMU each PIC has it's own PCR control register. */
  875. static void calculate_multiple_pcrs(struct cpu_hw_events *cpuc)
  876. {
  877. int i;
  878. if (!cpuc->n_added)
  879. goto out;
  880. for (i = 0; i < cpuc->n_events; i++) {
  881. struct perf_event *cp = cpuc->event[i];
  882. struct hw_perf_event *hwc = &cp->hw;
  883. int idx = hwc->idx;
  884. u64 enc;
  885. if (cpuc->current_idx[i] != PIC_NO_INDEX)
  886. continue;
  887. sparc_perf_event_set_period(cp, hwc, idx);
  888. cpuc->current_idx[i] = idx;
  889. enc = perf_event_get_enc(cpuc->events[i]);
  890. cpuc->pcr[idx] &= ~mask_for_index(idx);
  891. if (hwc->state & PERF_HES_STOPPED)
  892. cpuc->pcr[idx] |= nop_for_index(idx);
  893. else
  894. cpuc->pcr[idx] |= event_encoding(enc, idx);
  895. }
  896. out:
  897. for (i = 0; i < cpuc->n_events; i++) {
  898. struct perf_event *cp = cpuc->event[i];
  899. int idx = cp->hw.idx;
  900. cpuc->pcr[idx] |= cp->hw.config_base;
  901. }
  902. }
  903. /* If performance event entries have been added, move existing events
  904. * around (if necessary) and then assign new entries to counters.
  905. */
  906. static void update_pcrs_for_enable(struct cpu_hw_events *cpuc)
  907. {
  908. if (cpuc->n_added)
  909. read_in_all_counters(cpuc);
  910. if (sparc_pmu->num_pcrs == 1) {
  911. calculate_single_pcr(cpuc);
  912. } else {
  913. calculate_multiple_pcrs(cpuc);
  914. }
  915. }
  916. static void sparc_pmu_enable(struct pmu *pmu)
  917. {
  918. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  919. int i;
  920. if (cpuc->enabled)
  921. return;
  922. cpuc->enabled = 1;
  923. barrier();
  924. if (cpuc->n_events)
  925. update_pcrs_for_enable(cpuc);
  926. for (i = 0; i < sparc_pmu->num_pcrs; i++)
  927. pcr_ops->write_pcr(i, cpuc->pcr[i]);
  928. }
  929. static void sparc_pmu_disable(struct pmu *pmu)
  930. {
  931. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  932. int i;
  933. if (!cpuc->enabled)
  934. return;
  935. cpuc->enabled = 0;
  936. cpuc->n_added = 0;
  937. for (i = 0; i < sparc_pmu->num_pcrs; i++) {
  938. u64 val = cpuc->pcr[i];
  939. val &= ~(sparc_pmu->user_bit | sparc_pmu->priv_bit |
  940. sparc_pmu->hv_bit | sparc_pmu->irq_bit);
  941. cpuc->pcr[i] = val;
  942. pcr_ops->write_pcr(i, cpuc->pcr[i]);
  943. }
  944. }
  945. static int active_event_index(struct cpu_hw_events *cpuc,
  946. struct perf_event *event)
  947. {
  948. int i;
  949. for (i = 0; i < cpuc->n_events; i++) {
  950. if (cpuc->event[i] == event)
  951. break;
  952. }
  953. BUG_ON(i == cpuc->n_events);
  954. return cpuc->current_idx[i];
  955. }
  956. static void sparc_pmu_start(struct perf_event *event, int flags)
  957. {
  958. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  959. int idx = active_event_index(cpuc, event);
  960. if (flags & PERF_EF_RELOAD) {
  961. WARN_ON_ONCE(!(event->hw.state & PERF_HES_UPTODATE));
  962. sparc_perf_event_set_period(event, &event->hw, idx);
  963. }
  964. event->hw.state = 0;
  965. sparc_pmu_enable_event(cpuc, &event->hw, idx);
  966. }
  967. static void sparc_pmu_stop(struct perf_event *event, int flags)
  968. {
  969. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  970. int idx = active_event_index(cpuc, event);
  971. if (!(event->hw.state & PERF_HES_STOPPED)) {
  972. sparc_pmu_disable_event(cpuc, &event->hw, idx);
  973. event->hw.state |= PERF_HES_STOPPED;
  974. }
  975. if (!(event->hw.state & PERF_HES_UPTODATE) && (flags & PERF_EF_UPDATE)) {
  976. sparc_perf_event_update(event, &event->hw, idx);
  977. event->hw.state |= PERF_HES_UPTODATE;
  978. }
  979. }
  980. static void sparc_pmu_del(struct perf_event *event, int _flags)
  981. {
  982. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  983. unsigned long flags;
  984. int i;
  985. local_irq_save(flags);
  986. perf_pmu_disable(event->pmu);
  987. for (i = 0; i < cpuc->n_events; i++) {
  988. if (event == cpuc->event[i]) {
  989. /* Absorb the final count and turn off the
  990. * event.
  991. */
  992. sparc_pmu_stop(event, PERF_EF_UPDATE);
  993. /* Shift remaining entries down into
  994. * the existing slot.
  995. */
  996. while (++i < cpuc->n_events) {
  997. cpuc->event[i - 1] = cpuc->event[i];
  998. cpuc->events[i - 1] = cpuc->events[i];
  999. cpuc->current_idx[i - 1] =
  1000. cpuc->current_idx[i];
  1001. }
  1002. perf_event_update_userpage(event);
  1003. cpuc->n_events--;
  1004. break;
  1005. }
  1006. }
  1007. perf_pmu_enable(event->pmu);
  1008. local_irq_restore(flags);
  1009. }
  1010. static void sparc_pmu_read(struct perf_event *event)
  1011. {
  1012. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  1013. int idx = active_event_index(cpuc, event);
  1014. struct hw_perf_event *hwc = &event->hw;
  1015. sparc_perf_event_update(event, hwc, idx);
  1016. }
  1017. static atomic_t active_events = ATOMIC_INIT(0);
  1018. static DEFINE_MUTEX(pmc_grab_mutex);
  1019. static void perf_stop_nmi_watchdog(void *unused)
  1020. {
  1021. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  1022. int i;
  1023. stop_nmi_watchdog(NULL);
  1024. for (i = 0; i < sparc_pmu->num_pcrs; i++)
  1025. cpuc->pcr[i] = pcr_ops->read_pcr(i);
  1026. }
  1027. void perf_event_grab_pmc(void)
  1028. {
  1029. if (atomic_inc_not_zero(&active_events))
  1030. return;
  1031. mutex_lock(&pmc_grab_mutex);
  1032. if (atomic_read(&active_events) == 0) {
  1033. if (atomic_read(&nmi_active) > 0) {
  1034. on_each_cpu(perf_stop_nmi_watchdog, NULL, 1);
  1035. BUG_ON(atomic_read(&nmi_active) != 0);
  1036. }
  1037. atomic_inc(&active_events);
  1038. }
  1039. mutex_unlock(&pmc_grab_mutex);
  1040. }
  1041. void perf_event_release_pmc(void)
  1042. {
  1043. if (atomic_dec_and_mutex_lock(&active_events, &pmc_grab_mutex)) {
  1044. if (atomic_read(&nmi_active) == 0)
  1045. on_each_cpu(start_nmi_watchdog, NULL, 1);
  1046. mutex_unlock(&pmc_grab_mutex);
  1047. }
  1048. }
  1049. static const struct perf_event_map *sparc_map_cache_event(u64 config)
  1050. {
  1051. unsigned int cache_type, cache_op, cache_result;
  1052. const struct perf_event_map *pmap;
  1053. if (!sparc_pmu->cache_map)
  1054. return ERR_PTR(-ENOENT);
  1055. cache_type = (config >> 0) & 0xff;
  1056. if (cache_type >= PERF_COUNT_HW_CACHE_MAX)
  1057. return ERR_PTR(-EINVAL);
  1058. cache_op = (config >> 8) & 0xff;
  1059. if (cache_op >= PERF_COUNT_HW_CACHE_OP_MAX)
  1060. return ERR_PTR(-EINVAL);
  1061. cache_result = (config >> 16) & 0xff;
  1062. if (cache_result >= PERF_COUNT_HW_CACHE_RESULT_MAX)
  1063. return ERR_PTR(-EINVAL);
  1064. pmap = &((*sparc_pmu->cache_map)[cache_type][cache_op][cache_result]);
  1065. if (pmap->encoding == CACHE_OP_UNSUPPORTED)
  1066. return ERR_PTR(-ENOENT);
  1067. if (pmap->encoding == CACHE_OP_NONSENSE)
  1068. return ERR_PTR(-EINVAL);
  1069. return pmap;
  1070. }
  1071. static void hw_perf_event_destroy(struct perf_event *event)
  1072. {
  1073. perf_event_release_pmc();
  1074. }
  1075. /* Make sure all events can be scheduled into the hardware at
  1076. * the same time. This is simplified by the fact that we only
  1077. * need to support 2 simultaneous HW events.
  1078. *
  1079. * As a side effect, the evts[]->hw.idx values will be assigned
  1080. * on success. These are pending indexes. When the events are
  1081. * actually programmed into the chip, these values will propagate
  1082. * to the per-cpu cpuc->current_idx[] slots, see the code in
  1083. * maybe_change_configuration() for details.
  1084. */
  1085. static int sparc_check_constraints(struct perf_event **evts,
  1086. unsigned long *events, int n_ev)
  1087. {
  1088. u8 msk0 = 0, msk1 = 0;
  1089. int idx0 = 0;
  1090. /* This case is possible when we are invoked from
  1091. * hw_perf_group_sched_in().
  1092. */
  1093. if (!n_ev)
  1094. return 0;
  1095. if (n_ev > sparc_pmu->max_hw_events)
  1096. return -1;
  1097. if (!(sparc_pmu->flags & SPARC_PMU_HAS_CONFLICTS)) {
  1098. int i;
  1099. for (i = 0; i < n_ev; i++)
  1100. evts[i]->hw.idx = i;
  1101. return 0;
  1102. }
  1103. msk0 = perf_event_get_msk(events[0]);
  1104. if (n_ev == 1) {
  1105. if (msk0 & PIC_LOWER)
  1106. idx0 = 1;
  1107. goto success;
  1108. }
  1109. BUG_ON(n_ev != 2);
  1110. msk1 = perf_event_get_msk(events[1]);
  1111. /* If both events can go on any counter, OK. */
  1112. if (msk0 == (PIC_UPPER | PIC_LOWER) &&
  1113. msk1 == (PIC_UPPER | PIC_LOWER))
  1114. goto success;
  1115. /* If one event is limited to a specific counter,
  1116. * and the other can go on both, OK.
  1117. */
  1118. if ((msk0 == PIC_UPPER || msk0 == PIC_LOWER) &&
  1119. msk1 == (PIC_UPPER | PIC_LOWER)) {
  1120. if (msk0 & PIC_LOWER)
  1121. idx0 = 1;
  1122. goto success;
  1123. }
  1124. if ((msk1 == PIC_UPPER || msk1 == PIC_LOWER) &&
  1125. msk0 == (PIC_UPPER | PIC_LOWER)) {
  1126. if (msk1 & PIC_UPPER)
  1127. idx0 = 1;
  1128. goto success;
  1129. }
  1130. /* If the events are fixed to different counters, OK. */
  1131. if ((msk0 == PIC_UPPER && msk1 == PIC_LOWER) ||
  1132. (msk0 == PIC_LOWER && msk1 == PIC_UPPER)) {
  1133. if (msk0 & PIC_LOWER)
  1134. idx0 = 1;
  1135. goto success;
  1136. }
  1137. /* Otherwise, there is a conflict. */
  1138. return -1;
  1139. success:
  1140. evts[0]->hw.idx = idx0;
  1141. if (n_ev == 2)
  1142. evts[1]->hw.idx = idx0 ^ 1;
  1143. return 0;
  1144. }
  1145. static int check_excludes(struct perf_event **evts, int n_prev, int n_new)
  1146. {
  1147. int eu = 0, ek = 0, eh = 0;
  1148. struct perf_event *event;
  1149. int i, n, first;
  1150. if (!(sparc_pmu->flags & SPARC_PMU_ALL_EXCLUDES_SAME))
  1151. return 0;
  1152. n = n_prev + n_new;
  1153. if (n <= 1)
  1154. return 0;
  1155. first = 1;
  1156. for (i = 0; i < n; i++) {
  1157. event = evts[i];
  1158. if (first) {
  1159. eu = event->attr.exclude_user;
  1160. ek = event->attr.exclude_kernel;
  1161. eh = event->attr.exclude_hv;
  1162. first = 0;
  1163. } else if (event->attr.exclude_user != eu ||
  1164. event->attr.exclude_kernel != ek ||
  1165. event->attr.exclude_hv != eh) {
  1166. return -EAGAIN;
  1167. }
  1168. }
  1169. return 0;
  1170. }
  1171. static int collect_events(struct perf_event *group, int max_count,
  1172. struct perf_event *evts[], unsigned long *events,
  1173. int *current_idx)
  1174. {
  1175. struct perf_event *event;
  1176. int n = 0;
  1177. if (!is_software_event(group)) {
  1178. if (n >= max_count)
  1179. return -1;
  1180. evts[n] = group;
  1181. events[n] = group->hw.event_base;
  1182. current_idx[n++] = PIC_NO_INDEX;
  1183. }
  1184. list_for_each_entry(event, &group->sibling_list, group_entry) {
  1185. if (!is_software_event(event) &&
  1186. event->state != PERF_EVENT_STATE_OFF) {
  1187. if (n >= max_count)
  1188. return -1;
  1189. evts[n] = event;
  1190. events[n] = event->hw.event_base;
  1191. current_idx[n++] = PIC_NO_INDEX;
  1192. }
  1193. }
  1194. return n;
  1195. }
  1196. static int sparc_pmu_add(struct perf_event *event, int ef_flags)
  1197. {
  1198. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  1199. int n0, ret = -EAGAIN;
  1200. unsigned long flags;
  1201. local_irq_save(flags);
  1202. perf_pmu_disable(event->pmu);
  1203. n0 = cpuc->n_events;
  1204. if (n0 >= sparc_pmu->max_hw_events)
  1205. goto out;
  1206. cpuc->event[n0] = event;
  1207. cpuc->events[n0] = event->hw.event_base;
  1208. cpuc->current_idx[n0] = PIC_NO_INDEX;
  1209. event->hw.state = PERF_HES_UPTODATE;
  1210. if (!(ef_flags & PERF_EF_START))
  1211. event->hw.state |= PERF_HES_STOPPED;
  1212. /*
  1213. * If group events scheduling transaction was started,
  1214. * skip the schedulability test here, it will be performed
  1215. * at commit time(->commit_txn) as a whole
  1216. */
  1217. if (cpuc->group_flag & PERF_EVENT_TXN)
  1218. goto nocheck;
  1219. if (check_excludes(cpuc->event, n0, 1))
  1220. goto out;
  1221. if (sparc_check_constraints(cpuc->event, cpuc->events, n0 + 1))
  1222. goto out;
  1223. nocheck:
  1224. cpuc->n_events++;
  1225. cpuc->n_added++;
  1226. ret = 0;
  1227. out:
  1228. perf_pmu_enable(event->pmu);
  1229. local_irq_restore(flags);
  1230. return ret;
  1231. }
  1232. static int sparc_pmu_event_init(struct perf_event *event)
  1233. {
  1234. struct perf_event_attr *attr = &event->attr;
  1235. struct perf_event *evts[MAX_HWEVENTS];
  1236. struct hw_perf_event *hwc = &event->hw;
  1237. unsigned long events[MAX_HWEVENTS];
  1238. int current_idx_dmy[MAX_HWEVENTS];
  1239. const struct perf_event_map *pmap;
  1240. int n;
  1241. if (atomic_read(&nmi_active) < 0)
  1242. return -ENODEV;
  1243. /* does not support taken branch sampling */
  1244. if (has_branch_stack(event))
  1245. return -EOPNOTSUPP;
  1246. switch (attr->type) {
  1247. case PERF_TYPE_HARDWARE:
  1248. if (attr->config >= sparc_pmu->max_events)
  1249. return -EINVAL;
  1250. pmap = sparc_pmu->event_map(attr->config);
  1251. break;
  1252. case PERF_TYPE_HW_CACHE:
  1253. pmap = sparc_map_cache_event(attr->config);
  1254. if (IS_ERR(pmap))
  1255. return PTR_ERR(pmap);
  1256. break;
  1257. case PERF_TYPE_RAW:
  1258. pmap = NULL;
  1259. break;
  1260. default:
  1261. return -ENOENT;
  1262. }
  1263. if (pmap) {
  1264. hwc->event_base = perf_event_encode(pmap);
  1265. } else {
  1266. /*
  1267. * User gives us "(encoding << 16) | pic_mask" for
  1268. * PERF_TYPE_RAW events.
  1269. */
  1270. hwc->event_base = attr->config;
  1271. }
  1272. /* We save the enable bits in the config_base. */
  1273. hwc->config_base = sparc_pmu->irq_bit;
  1274. if (!attr->exclude_user)
  1275. hwc->config_base |= sparc_pmu->user_bit;
  1276. if (!attr->exclude_kernel)
  1277. hwc->config_base |= sparc_pmu->priv_bit;
  1278. if (!attr->exclude_hv)
  1279. hwc->config_base |= sparc_pmu->hv_bit;
  1280. n = 0;
  1281. if (event->group_leader != event) {
  1282. n = collect_events(event->group_leader,
  1283. sparc_pmu->max_hw_events - 1,
  1284. evts, events, current_idx_dmy);
  1285. if (n < 0)
  1286. return -EINVAL;
  1287. }
  1288. events[n] = hwc->event_base;
  1289. evts[n] = event;
  1290. if (check_excludes(evts, n, 1))
  1291. return -EINVAL;
  1292. if (sparc_check_constraints(evts, events, n + 1))
  1293. return -EINVAL;
  1294. hwc->idx = PIC_NO_INDEX;
  1295. /* Try to do all error checking before this point, as unwinding
  1296. * state after grabbing the PMC is difficult.
  1297. */
  1298. perf_event_grab_pmc();
  1299. event->destroy = hw_perf_event_destroy;
  1300. if (!hwc->sample_period) {
  1301. hwc->sample_period = MAX_PERIOD;
  1302. hwc->last_period = hwc->sample_period;
  1303. local64_set(&hwc->period_left, hwc->sample_period);
  1304. }
  1305. return 0;
  1306. }
  1307. /*
  1308. * Start group events scheduling transaction
  1309. * Set the flag to make pmu::enable() not perform the
  1310. * schedulability test, it will be performed at commit time
  1311. */
  1312. static void sparc_pmu_start_txn(struct pmu *pmu)
  1313. {
  1314. struct cpu_hw_events *cpuhw = &__get_cpu_var(cpu_hw_events);
  1315. perf_pmu_disable(pmu);
  1316. cpuhw->group_flag |= PERF_EVENT_TXN;
  1317. }
  1318. /*
  1319. * Stop group events scheduling transaction
  1320. * Clear the flag and pmu::enable() will perform the
  1321. * schedulability test.
  1322. */
  1323. static void sparc_pmu_cancel_txn(struct pmu *pmu)
  1324. {
  1325. struct cpu_hw_events *cpuhw = &__get_cpu_var(cpu_hw_events);
  1326. cpuhw->group_flag &= ~PERF_EVENT_TXN;
  1327. perf_pmu_enable(pmu);
  1328. }
  1329. /*
  1330. * Commit group events scheduling transaction
  1331. * Perform the group schedulability test as a whole
  1332. * Return 0 if success
  1333. */
  1334. static int sparc_pmu_commit_txn(struct pmu *pmu)
  1335. {
  1336. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  1337. int n;
  1338. if (!sparc_pmu)
  1339. return -EINVAL;
  1340. cpuc = &__get_cpu_var(cpu_hw_events);
  1341. n = cpuc->n_events;
  1342. if (check_excludes(cpuc->event, 0, n))
  1343. return -EINVAL;
  1344. if (sparc_check_constraints(cpuc->event, cpuc->events, n))
  1345. return -EAGAIN;
  1346. cpuc->group_flag &= ~PERF_EVENT_TXN;
  1347. perf_pmu_enable(pmu);
  1348. return 0;
  1349. }
  1350. static struct pmu pmu = {
  1351. .pmu_enable = sparc_pmu_enable,
  1352. .pmu_disable = sparc_pmu_disable,
  1353. .event_init = sparc_pmu_event_init,
  1354. .add = sparc_pmu_add,
  1355. .del = sparc_pmu_del,
  1356. .start = sparc_pmu_start,
  1357. .stop = sparc_pmu_stop,
  1358. .read = sparc_pmu_read,
  1359. .start_txn = sparc_pmu_start_txn,
  1360. .cancel_txn = sparc_pmu_cancel_txn,
  1361. .commit_txn = sparc_pmu_commit_txn,
  1362. };
  1363. void perf_event_print_debug(void)
  1364. {
  1365. unsigned long flags;
  1366. int cpu, i;
  1367. if (!sparc_pmu)
  1368. return;
  1369. local_irq_save(flags);
  1370. cpu = smp_processor_id();
  1371. pr_info("\n");
  1372. for (i = 0; i < sparc_pmu->num_pcrs; i++)
  1373. pr_info("CPU#%d: PCR%d[%016llx]\n",
  1374. cpu, i, pcr_ops->read_pcr(i));
  1375. for (i = 0; i < sparc_pmu->num_pic_regs; i++)
  1376. pr_info("CPU#%d: PIC%d[%016llx]\n",
  1377. cpu, i, pcr_ops->read_pic(i));
  1378. local_irq_restore(flags);
  1379. }
  1380. static int __kprobes perf_event_nmi_handler(struct notifier_block *self,
  1381. unsigned long cmd, void *__args)
  1382. {
  1383. struct die_args *args = __args;
  1384. struct perf_sample_data data;
  1385. struct cpu_hw_events *cpuc;
  1386. struct pt_regs *regs;
  1387. int i;
  1388. if (!atomic_read(&active_events))
  1389. return NOTIFY_DONE;
  1390. switch (cmd) {
  1391. case DIE_NMI:
  1392. break;
  1393. default:
  1394. return NOTIFY_DONE;
  1395. }
  1396. regs = args->regs;
  1397. cpuc = &__get_cpu_var(cpu_hw_events);
  1398. /* If the PMU has the TOE IRQ enable bits, we need to do a
  1399. * dummy write to the %pcr to clear the overflow bits and thus
  1400. * the interrupt.
  1401. *
  1402. * Do this before we peek at the counters to determine
  1403. * overflow so we don't lose any events.
  1404. */
  1405. if (sparc_pmu->irq_bit &&
  1406. sparc_pmu->num_pcrs == 1)
  1407. pcr_ops->write_pcr(0, cpuc->pcr[0]);
  1408. for (i = 0; i < cpuc->n_events; i++) {
  1409. struct perf_event *event = cpuc->event[i];
  1410. int idx = cpuc->current_idx[i];
  1411. struct hw_perf_event *hwc;
  1412. u64 val;
  1413. if (sparc_pmu->irq_bit &&
  1414. sparc_pmu->num_pcrs > 1)
  1415. pcr_ops->write_pcr(idx, cpuc->pcr[idx]);
  1416. hwc = &event->hw;
  1417. val = sparc_perf_event_update(event, hwc, idx);
  1418. if (val & (1ULL << 31))
  1419. continue;
  1420. perf_sample_data_init(&data, 0, hwc->last_period);
  1421. if (!sparc_perf_event_set_period(event, hwc, idx))
  1422. continue;
  1423. if (perf_event_overflow(event, &data, regs))
  1424. sparc_pmu_stop(event, 0);
  1425. }
  1426. return NOTIFY_STOP;
  1427. }
  1428. static __read_mostly struct notifier_block perf_event_nmi_notifier = {
  1429. .notifier_call = perf_event_nmi_handler,
  1430. };
  1431. static bool __init supported_pmu(void)
  1432. {
  1433. if (!strcmp(sparc_pmu_type, "ultra3") ||
  1434. !strcmp(sparc_pmu_type, "ultra3+") ||
  1435. !strcmp(sparc_pmu_type, "ultra3i") ||
  1436. !strcmp(sparc_pmu_type, "ultra4+")) {
  1437. sparc_pmu = &ultra3_pmu;
  1438. return true;
  1439. }
  1440. if (!strcmp(sparc_pmu_type, "niagara")) {
  1441. sparc_pmu = &niagara1_pmu;
  1442. return true;
  1443. }
  1444. if (!strcmp(sparc_pmu_type, "niagara2") ||
  1445. !strcmp(sparc_pmu_type, "niagara3")) {
  1446. sparc_pmu = &niagara2_pmu;
  1447. return true;
  1448. }
  1449. if (!strcmp(sparc_pmu_type, "niagara4")) {
  1450. sparc_pmu = &niagara4_pmu;
  1451. return true;
  1452. }
  1453. return false;
  1454. }
  1455. int __init init_hw_perf_events(void)
  1456. {
  1457. pr_info("Performance events: ");
  1458. if (!supported_pmu()) {
  1459. pr_cont("No support for PMU type '%s'\n", sparc_pmu_type);
  1460. return 0;
  1461. }
  1462. pr_cont("Supported PMU type is '%s'\n", sparc_pmu_type);
  1463. perf_pmu_register(&pmu, "cpu", PERF_TYPE_RAW);
  1464. register_die_notifier(&perf_event_nmi_notifier);
  1465. return 0;
  1466. }
  1467. early_initcall(init_hw_perf_events);
  1468. void perf_callchain_kernel(struct perf_callchain_entry *entry,
  1469. struct pt_regs *regs)
  1470. {
  1471. unsigned long ksp, fp;
  1472. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  1473. int graph = 0;
  1474. #endif
  1475. stack_trace_flush();
  1476. perf_callchain_store(entry, regs->tpc);
  1477. ksp = regs->u_regs[UREG_I6];
  1478. fp = ksp + STACK_BIAS;
  1479. do {
  1480. struct sparc_stackf *sf;
  1481. struct pt_regs *regs;
  1482. unsigned long pc;
  1483. if (!kstack_valid(current_thread_info(), fp))
  1484. break;
  1485. sf = (struct sparc_stackf *) fp;
  1486. regs = (struct pt_regs *) (sf + 1);
  1487. if (kstack_is_trap_frame(current_thread_info(), regs)) {
  1488. if (user_mode(regs))
  1489. break;
  1490. pc = regs->tpc;
  1491. fp = regs->u_regs[UREG_I6] + STACK_BIAS;
  1492. } else {
  1493. pc = sf->callers_pc;
  1494. fp = (unsigned long)sf->fp + STACK_BIAS;
  1495. }
  1496. perf_callchain_store(entry, pc);
  1497. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  1498. if ((pc + 8UL) == (unsigned long) &return_to_handler) {
  1499. int index = current->curr_ret_stack;
  1500. if (current->ret_stack && index >= graph) {
  1501. pc = current->ret_stack[index - graph].ret;
  1502. perf_callchain_store(entry, pc);
  1503. graph++;
  1504. }
  1505. }
  1506. #endif
  1507. } while (entry->nr < PERF_MAX_STACK_DEPTH);
  1508. }
  1509. static void perf_callchain_user_64(struct perf_callchain_entry *entry,
  1510. struct pt_regs *regs)
  1511. {
  1512. unsigned long ufp;
  1513. ufp = regs->u_regs[UREG_I6] + STACK_BIAS;
  1514. do {
  1515. struct sparc_stackf *usf, sf;
  1516. unsigned long pc;
  1517. usf = (struct sparc_stackf *) ufp;
  1518. if (__copy_from_user_inatomic(&sf, usf, sizeof(sf)))
  1519. break;
  1520. pc = sf.callers_pc;
  1521. ufp = (unsigned long)sf.fp + STACK_BIAS;
  1522. perf_callchain_store(entry, pc);
  1523. } while (entry->nr < PERF_MAX_STACK_DEPTH);
  1524. }
  1525. static void perf_callchain_user_32(struct perf_callchain_entry *entry,
  1526. struct pt_regs *regs)
  1527. {
  1528. unsigned long ufp;
  1529. ufp = regs->u_regs[UREG_I6] & 0xffffffffUL;
  1530. do {
  1531. unsigned long pc;
  1532. if (thread32_stack_is_64bit(ufp)) {
  1533. struct sparc_stackf *usf, sf;
  1534. ufp += STACK_BIAS;
  1535. usf = (struct sparc_stackf *) ufp;
  1536. if (__copy_from_user_inatomic(&sf, usf, sizeof(sf)))
  1537. break;
  1538. pc = sf.callers_pc & 0xffffffff;
  1539. ufp = ((unsigned long) sf.fp) & 0xffffffff;
  1540. } else {
  1541. struct sparc_stackf32 *usf, sf;
  1542. usf = (struct sparc_stackf32 *) ufp;
  1543. if (__copy_from_user_inatomic(&sf, usf, sizeof(sf)))
  1544. break;
  1545. pc = sf.callers_pc;
  1546. ufp = (unsigned long)sf.fp;
  1547. }
  1548. perf_callchain_store(entry, pc);
  1549. } while (entry->nr < PERF_MAX_STACK_DEPTH);
  1550. }
  1551. void
  1552. perf_callchain_user(struct perf_callchain_entry *entry, struct pt_regs *regs)
  1553. {
  1554. perf_callchain_store(entry, regs->tpc);
  1555. if (!current->mm)
  1556. return;
  1557. flushw_user();
  1558. if (test_thread_flag(TIF_32BIT))
  1559. perf_callchain_user_32(entry, regs);
  1560. else
  1561. perf_callchain_user_64(entry, regs);
  1562. }