perf_event.c 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808
  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. 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. /* 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 = this_cpu_ptr(&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 = this_cpu_ptr(&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 = this_cpu_ptr(&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 = this_cpu_ptr(&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 = this_cpu_ptr(&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 = this_cpu_ptr(&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 = this_cpu_ptr(&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. static 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. static 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 = this_cpu_ptr(&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 = this_cpu_ptr(&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 = this_cpu_ptr(&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 = this_cpu_ptr(&cpu_hw_events);
  1337. int n;
  1338. if (!sparc_pmu)
  1339. return -EINVAL;
  1340. cpuc = this_cpu_ptr(&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 = this_cpu_ptr(&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. !strcmp(sparc_pmu_type, "niagara5")) {
  1451. sparc_pmu = &niagara4_pmu;
  1452. return true;
  1453. }
  1454. return false;
  1455. }
  1456. static int __init init_hw_perf_events(void)
  1457. {
  1458. int err;
  1459. pr_info("Performance events: ");
  1460. err = pcr_arch_init();
  1461. if (err || !supported_pmu()) {
  1462. pr_cont("No support for PMU type '%s'\n", sparc_pmu_type);
  1463. return 0;
  1464. }
  1465. pr_cont("Supported PMU type is '%s'\n", sparc_pmu_type);
  1466. perf_pmu_register(&pmu, "cpu", PERF_TYPE_RAW);
  1467. register_die_notifier(&perf_event_nmi_notifier);
  1468. return 0;
  1469. }
  1470. pure_initcall(init_hw_perf_events);
  1471. void perf_callchain_kernel(struct perf_callchain_entry *entry,
  1472. struct pt_regs *regs)
  1473. {
  1474. unsigned long ksp, fp;
  1475. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  1476. int graph = 0;
  1477. #endif
  1478. stack_trace_flush();
  1479. perf_callchain_store(entry, regs->tpc);
  1480. ksp = regs->u_regs[UREG_I6];
  1481. fp = ksp + STACK_BIAS;
  1482. do {
  1483. struct sparc_stackf *sf;
  1484. struct pt_regs *regs;
  1485. unsigned long pc;
  1486. if (!kstack_valid(current_thread_info(), fp))
  1487. break;
  1488. sf = (struct sparc_stackf *) fp;
  1489. regs = (struct pt_regs *) (sf + 1);
  1490. if (kstack_is_trap_frame(current_thread_info(), regs)) {
  1491. if (user_mode(regs))
  1492. break;
  1493. pc = regs->tpc;
  1494. fp = regs->u_regs[UREG_I6] + STACK_BIAS;
  1495. } else {
  1496. pc = sf->callers_pc;
  1497. fp = (unsigned long)sf->fp + STACK_BIAS;
  1498. }
  1499. perf_callchain_store(entry, pc);
  1500. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  1501. if ((pc + 8UL) == (unsigned long) &return_to_handler) {
  1502. int index = current->curr_ret_stack;
  1503. if (current->ret_stack && index >= graph) {
  1504. pc = current->ret_stack[index - graph].ret;
  1505. perf_callchain_store(entry, pc);
  1506. graph++;
  1507. }
  1508. }
  1509. #endif
  1510. } while (entry->nr < PERF_MAX_STACK_DEPTH);
  1511. }
  1512. static void perf_callchain_user_64(struct perf_callchain_entry *entry,
  1513. struct pt_regs *regs)
  1514. {
  1515. unsigned long ufp;
  1516. ufp = regs->u_regs[UREG_I6] + STACK_BIAS;
  1517. do {
  1518. struct sparc_stackf __user *usf;
  1519. struct sparc_stackf sf;
  1520. unsigned long pc;
  1521. usf = (struct sparc_stackf __user *)ufp;
  1522. if (__copy_from_user_inatomic(&sf, usf, sizeof(sf)))
  1523. break;
  1524. pc = sf.callers_pc;
  1525. ufp = (unsigned long)sf.fp + STACK_BIAS;
  1526. perf_callchain_store(entry, pc);
  1527. } while (entry->nr < PERF_MAX_STACK_DEPTH);
  1528. }
  1529. static void perf_callchain_user_32(struct perf_callchain_entry *entry,
  1530. struct pt_regs *regs)
  1531. {
  1532. unsigned long ufp;
  1533. ufp = regs->u_regs[UREG_I6] & 0xffffffffUL;
  1534. do {
  1535. unsigned long pc;
  1536. if (thread32_stack_is_64bit(ufp)) {
  1537. struct sparc_stackf __user *usf;
  1538. struct sparc_stackf sf;
  1539. ufp += STACK_BIAS;
  1540. usf = (struct sparc_stackf __user *)ufp;
  1541. if (__copy_from_user_inatomic(&sf, usf, sizeof(sf)))
  1542. break;
  1543. pc = sf.callers_pc & 0xffffffff;
  1544. ufp = ((unsigned long) sf.fp) & 0xffffffff;
  1545. } else {
  1546. struct sparc_stackf32 __user *usf;
  1547. struct sparc_stackf32 sf;
  1548. usf = (struct sparc_stackf32 __user *)ufp;
  1549. if (__copy_from_user_inatomic(&sf, usf, sizeof(sf)))
  1550. break;
  1551. pc = sf.callers_pc;
  1552. ufp = (unsigned long)sf.fp;
  1553. }
  1554. perf_callchain_store(entry, pc);
  1555. } while (entry->nr < PERF_MAX_STACK_DEPTH);
  1556. }
  1557. void
  1558. perf_callchain_user(struct perf_callchain_entry *entry, struct pt_regs *regs)
  1559. {
  1560. perf_callchain_store(entry, regs->tpc);
  1561. if (!current->mm)
  1562. return;
  1563. flushw_user();
  1564. if (test_thread_flag(TIF_32BIT))
  1565. perf_callchain_user_32(entry, regs);
  1566. else
  1567. perf_callchain_user_64(entry, regs);
  1568. }