perf_event.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409
  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 <asm/atomic.h>
  24. #include <asm/nmi.h>
  25. #include <asm/pcr.h>
  26. #include "kstack.h"
  27. /* Sparc64 chips have two performance counters, 32-bits each, with
  28. * overflow interrupts generated on transition from 0xffffffff to 0.
  29. * The counters are accessed in one go using a 64-bit register.
  30. *
  31. * Both counters are controlled using a single control register. The
  32. * only way to stop all sampling is to clear all of the context (user,
  33. * supervisor, hypervisor) sampling enable bits. But these bits apply
  34. * to both counters, thus the two counters can't be enabled/disabled
  35. * individually.
  36. *
  37. * The control register has two event fields, one for each of the two
  38. * counters. It's thus nearly impossible to have one counter going
  39. * while keeping the other one stopped. Therefore it is possible to
  40. * get overflow interrupts for counters not currently "in use" and
  41. * that condition must be checked in the overflow interrupt handler.
  42. *
  43. * So we use a hack, in that we program inactive counters with the
  44. * "sw_count0" and "sw_count1" events. These count how many times
  45. * the instruction "sethi %hi(0xfc000), %g0" is executed. It's an
  46. * unusual way to encode a NOP and therefore will not trigger in
  47. * normal code.
  48. */
  49. #define MAX_HWEVENTS 2
  50. #define MAX_PERIOD ((1UL << 32) - 1)
  51. #define PIC_UPPER_INDEX 0
  52. #define PIC_LOWER_INDEX 1
  53. #define PIC_NO_INDEX -1
  54. struct cpu_hw_events {
  55. /* Number of events currently scheduled onto this cpu.
  56. * This tells how many entries in the arrays below
  57. * are valid.
  58. */
  59. int n_events;
  60. /* Number of new events added since the last hw_perf_disable().
  61. * This works because the perf event layer always adds new
  62. * events inside of a perf_{disable,enable}() sequence.
  63. */
  64. int n_added;
  65. /* Array of events current scheduled on this cpu. */
  66. struct perf_event *event[MAX_HWEVENTS];
  67. /* Array of encoded longs, specifying the %pcr register
  68. * encoding and the mask of PIC counters this even can
  69. * be scheduled on. See perf_event_encode() et al.
  70. */
  71. unsigned long events[MAX_HWEVENTS];
  72. /* The current counter index assigned to an event. When the
  73. * event hasn't been programmed into the cpu yet, this will
  74. * hold PIC_NO_INDEX. The event->hw.idx value tells us where
  75. * we ought to schedule the event.
  76. */
  77. int current_idx[MAX_HWEVENTS];
  78. /* Software copy of %pcr register on this cpu. */
  79. u64 pcr;
  80. /* Enabled/disable state. */
  81. int enabled;
  82. unsigned int group_flag;
  83. };
  84. DEFINE_PER_CPU(struct cpu_hw_events, cpu_hw_events) = { .enabled = 1, };
  85. /* An event map describes the characteristics of a performance
  86. * counter event. In particular it gives the encoding as well as
  87. * a mask telling which counters the event can be measured on.
  88. */
  89. struct perf_event_map {
  90. u16 encoding;
  91. u8 pic_mask;
  92. #define PIC_NONE 0x00
  93. #define PIC_UPPER 0x01
  94. #define PIC_LOWER 0x02
  95. };
  96. /* Encode a perf_event_map entry into a long. */
  97. static unsigned long perf_event_encode(const struct perf_event_map *pmap)
  98. {
  99. return ((unsigned long) pmap->encoding << 16) | pmap->pic_mask;
  100. }
  101. static u8 perf_event_get_msk(unsigned long val)
  102. {
  103. return val & 0xff;
  104. }
  105. static u64 perf_event_get_enc(unsigned long val)
  106. {
  107. return val >> 16;
  108. }
  109. #define C(x) PERF_COUNT_HW_CACHE_##x
  110. #define CACHE_OP_UNSUPPORTED 0xfffe
  111. #define CACHE_OP_NONSENSE 0xffff
  112. typedef struct perf_event_map cache_map_t
  113. [PERF_COUNT_HW_CACHE_MAX]
  114. [PERF_COUNT_HW_CACHE_OP_MAX]
  115. [PERF_COUNT_HW_CACHE_RESULT_MAX];
  116. struct sparc_pmu {
  117. const struct perf_event_map *(*event_map)(int);
  118. const cache_map_t *cache_map;
  119. int max_events;
  120. int upper_shift;
  121. int lower_shift;
  122. int event_mask;
  123. int hv_bit;
  124. int irq_bit;
  125. int upper_nop;
  126. int lower_nop;
  127. };
  128. static const struct perf_event_map ultra3_perfmon_event_map[] = {
  129. [PERF_COUNT_HW_CPU_CYCLES] = { 0x0000, PIC_UPPER | PIC_LOWER },
  130. [PERF_COUNT_HW_INSTRUCTIONS] = { 0x0001, PIC_UPPER | PIC_LOWER },
  131. [PERF_COUNT_HW_CACHE_REFERENCES] = { 0x0009, PIC_LOWER },
  132. [PERF_COUNT_HW_CACHE_MISSES] = { 0x0009, PIC_UPPER },
  133. };
  134. static const struct perf_event_map *ultra3_event_map(int event_id)
  135. {
  136. return &ultra3_perfmon_event_map[event_id];
  137. }
  138. static const cache_map_t ultra3_cache_map = {
  139. [C(L1D)] = {
  140. [C(OP_READ)] = {
  141. [C(RESULT_ACCESS)] = { 0x09, PIC_LOWER, },
  142. [C(RESULT_MISS)] = { 0x09, PIC_UPPER, },
  143. },
  144. [C(OP_WRITE)] = {
  145. [C(RESULT_ACCESS)] = { 0x0a, PIC_LOWER },
  146. [C(RESULT_MISS)] = { 0x0a, PIC_UPPER },
  147. },
  148. [C(OP_PREFETCH)] = {
  149. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  150. [C(RESULT_MISS)] = { CACHE_OP_UNSUPPORTED },
  151. },
  152. },
  153. [C(L1I)] = {
  154. [C(OP_READ)] = {
  155. [C(RESULT_ACCESS)] = { 0x09, PIC_LOWER, },
  156. [C(RESULT_MISS)] = { 0x09, PIC_UPPER, },
  157. },
  158. [ C(OP_WRITE) ] = {
  159. [ C(RESULT_ACCESS) ] = { CACHE_OP_NONSENSE },
  160. [ C(RESULT_MISS) ] = { CACHE_OP_NONSENSE },
  161. },
  162. [ C(OP_PREFETCH) ] = {
  163. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  164. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  165. },
  166. },
  167. [C(LL)] = {
  168. [C(OP_READ)] = {
  169. [C(RESULT_ACCESS)] = { 0x0c, PIC_LOWER, },
  170. [C(RESULT_MISS)] = { 0x0c, PIC_UPPER, },
  171. },
  172. [C(OP_WRITE)] = {
  173. [C(RESULT_ACCESS)] = { 0x0c, PIC_LOWER },
  174. [C(RESULT_MISS)] = { 0x0c, PIC_UPPER },
  175. },
  176. [C(OP_PREFETCH)] = {
  177. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  178. [C(RESULT_MISS)] = { CACHE_OP_UNSUPPORTED },
  179. },
  180. },
  181. [C(DTLB)] = {
  182. [C(OP_READ)] = {
  183. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  184. [C(RESULT_MISS)] = { 0x12, PIC_UPPER, },
  185. },
  186. [ C(OP_WRITE) ] = {
  187. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  188. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  189. },
  190. [ C(OP_PREFETCH) ] = {
  191. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  192. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  193. },
  194. },
  195. [C(ITLB)] = {
  196. [C(OP_READ)] = {
  197. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  198. [C(RESULT_MISS)] = { 0x11, PIC_UPPER, },
  199. },
  200. [ C(OP_WRITE) ] = {
  201. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  202. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  203. },
  204. [ C(OP_PREFETCH) ] = {
  205. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  206. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  207. },
  208. },
  209. [C(BPU)] = {
  210. [C(OP_READ)] = {
  211. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  212. [C(RESULT_MISS)] = { CACHE_OP_UNSUPPORTED },
  213. },
  214. [ C(OP_WRITE) ] = {
  215. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  216. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  217. },
  218. [ C(OP_PREFETCH) ] = {
  219. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  220. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  221. },
  222. },
  223. };
  224. static const struct sparc_pmu ultra3_pmu = {
  225. .event_map = ultra3_event_map,
  226. .cache_map = &ultra3_cache_map,
  227. .max_events = ARRAY_SIZE(ultra3_perfmon_event_map),
  228. .upper_shift = 11,
  229. .lower_shift = 4,
  230. .event_mask = 0x3f,
  231. .upper_nop = 0x1c,
  232. .lower_nop = 0x14,
  233. };
  234. /* Niagara1 is very limited. The upper PIC is hard-locked to count
  235. * only instructions, so it is free running which creates all kinds of
  236. * problems. Some hardware designs make one wonder if the creator
  237. * even looked at how this stuff gets used by software.
  238. */
  239. static const struct perf_event_map niagara1_perfmon_event_map[] = {
  240. [PERF_COUNT_HW_CPU_CYCLES] = { 0x00, PIC_UPPER },
  241. [PERF_COUNT_HW_INSTRUCTIONS] = { 0x00, PIC_UPPER },
  242. [PERF_COUNT_HW_CACHE_REFERENCES] = { 0, PIC_NONE },
  243. [PERF_COUNT_HW_CACHE_MISSES] = { 0x03, PIC_LOWER },
  244. };
  245. static const struct perf_event_map *niagara1_event_map(int event_id)
  246. {
  247. return &niagara1_perfmon_event_map[event_id];
  248. }
  249. static const cache_map_t niagara1_cache_map = {
  250. [C(L1D)] = {
  251. [C(OP_READ)] = {
  252. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  253. [C(RESULT_MISS)] = { 0x03, PIC_LOWER, },
  254. },
  255. [C(OP_WRITE)] = {
  256. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  257. [C(RESULT_MISS)] = { 0x03, PIC_LOWER, },
  258. },
  259. [C(OP_PREFETCH)] = {
  260. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  261. [C(RESULT_MISS)] = { CACHE_OP_UNSUPPORTED },
  262. },
  263. },
  264. [C(L1I)] = {
  265. [C(OP_READ)] = {
  266. [C(RESULT_ACCESS)] = { 0x00, PIC_UPPER },
  267. [C(RESULT_MISS)] = { 0x02, PIC_LOWER, },
  268. },
  269. [ C(OP_WRITE) ] = {
  270. [ C(RESULT_ACCESS) ] = { CACHE_OP_NONSENSE },
  271. [ C(RESULT_MISS) ] = { CACHE_OP_NONSENSE },
  272. },
  273. [ C(OP_PREFETCH) ] = {
  274. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  275. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  276. },
  277. },
  278. [C(LL)] = {
  279. [C(OP_READ)] = {
  280. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  281. [C(RESULT_MISS)] = { 0x07, PIC_LOWER, },
  282. },
  283. [C(OP_WRITE)] = {
  284. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  285. [C(RESULT_MISS)] = { 0x07, PIC_LOWER, },
  286. },
  287. [C(OP_PREFETCH)] = {
  288. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  289. [C(RESULT_MISS)] = { CACHE_OP_UNSUPPORTED },
  290. },
  291. },
  292. [C(DTLB)] = {
  293. [C(OP_READ)] = {
  294. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  295. [C(RESULT_MISS)] = { 0x05, PIC_LOWER, },
  296. },
  297. [ C(OP_WRITE) ] = {
  298. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  299. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  300. },
  301. [ C(OP_PREFETCH) ] = {
  302. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  303. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  304. },
  305. },
  306. [C(ITLB)] = {
  307. [C(OP_READ)] = {
  308. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  309. [C(RESULT_MISS)] = { 0x04, PIC_LOWER, },
  310. },
  311. [ C(OP_WRITE) ] = {
  312. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  313. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  314. },
  315. [ C(OP_PREFETCH) ] = {
  316. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  317. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  318. },
  319. },
  320. [C(BPU)] = {
  321. [C(OP_READ)] = {
  322. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  323. [C(RESULT_MISS)] = { CACHE_OP_UNSUPPORTED },
  324. },
  325. [ C(OP_WRITE) ] = {
  326. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  327. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  328. },
  329. [ C(OP_PREFETCH) ] = {
  330. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  331. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  332. },
  333. },
  334. };
  335. static const struct sparc_pmu niagara1_pmu = {
  336. .event_map = niagara1_event_map,
  337. .cache_map = &niagara1_cache_map,
  338. .max_events = ARRAY_SIZE(niagara1_perfmon_event_map),
  339. .upper_shift = 0,
  340. .lower_shift = 4,
  341. .event_mask = 0x7,
  342. .upper_nop = 0x0,
  343. .lower_nop = 0x0,
  344. };
  345. static const struct perf_event_map niagara2_perfmon_event_map[] = {
  346. [PERF_COUNT_HW_CPU_CYCLES] = { 0x02ff, PIC_UPPER | PIC_LOWER },
  347. [PERF_COUNT_HW_INSTRUCTIONS] = { 0x02ff, PIC_UPPER | PIC_LOWER },
  348. [PERF_COUNT_HW_CACHE_REFERENCES] = { 0x0208, PIC_UPPER | PIC_LOWER },
  349. [PERF_COUNT_HW_CACHE_MISSES] = { 0x0302, PIC_UPPER | PIC_LOWER },
  350. [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = { 0x0201, PIC_UPPER | PIC_LOWER },
  351. [PERF_COUNT_HW_BRANCH_MISSES] = { 0x0202, PIC_UPPER | PIC_LOWER },
  352. };
  353. static const struct perf_event_map *niagara2_event_map(int event_id)
  354. {
  355. return &niagara2_perfmon_event_map[event_id];
  356. }
  357. static const cache_map_t niagara2_cache_map = {
  358. [C(L1D)] = {
  359. [C(OP_READ)] = {
  360. [C(RESULT_ACCESS)] = { 0x0208, PIC_UPPER | PIC_LOWER, },
  361. [C(RESULT_MISS)] = { 0x0302, PIC_UPPER | PIC_LOWER, },
  362. },
  363. [C(OP_WRITE)] = {
  364. [C(RESULT_ACCESS)] = { 0x0210, PIC_UPPER | PIC_LOWER, },
  365. [C(RESULT_MISS)] = { 0x0302, PIC_UPPER | PIC_LOWER, },
  366. },
  367. [C(OP_PREFETCH)] = {
  368. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  369. [C(RESULT_MISS)] = { CACHE_OP_UNSUPPORTED },
  370. },
  371. },
  372. [C(L1I)] = {
  373. [C(OP_READ)] = {
  374. [C(RESULT_ACCESS)] = { 0x02ff, PIC_UPPER | PIC_LOWER, },
  375. [C(RESULT_MISS)] = { 0x0301, PIC_UPPER | PIC_LOWER, },
  376. },
  377. [ C(OP_WRITE) ] = {
  378. [ C(RESULT_ACCESS) ] = { CACHE_OP_NONSENSE },
  379. [ C(RESULT_MISS) ] = { CACHE_OP_NONSENSE },
  380. },
  381. [ C(OP_PREFETCH) ] = {
  382. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  383. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  384. },
  385. },
  386. [C(LL)] = {
  387. [C(OP_READ)] = {
  388. [C(RESULT_ACCESS)] = { 0x0208, PIC_UPPER | PIC_LOWER, },
  389. [C(RESULT_MISS)] = { 0x0330, PIC_UPPER | PIC_LOWER, },
  390. },
  391. [C(OP_WRITE)] = {
  392. [C(RESULT_ACCESS)] = { 0x0210, PIC_UPPER | PIC_LOWER, },
  393. [C(RESULT_MISS)] = { 0x0320, PIC_UPPER | PIC_LOWER, },
  394. },
  395. [C(OP_PREFETCH)] = {
  396. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  397. [C(RESULT_MISS)] = { CACHE_OP_UNSUPPORTED },
  398. },
  399. },
  400. [C(DTLB)] = {
  401. [C(OP_READ)] = {
  402. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  403. [C(RESULT_MISS)] = { 0x0b08, PIC_UPPER | PIC_LOWER, },
  404. },
  405. [ C(OP_WRITE) ] = {
  406. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  407. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  408. },
  409. [ C(OP_PREFETCH) ] = {
  410. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  411. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  412. },
  413. },
  414. [C(ITLB)] = {
  415. [C(OP_READ)] = {
  416. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  417. [C(RESULT_MISS)] = { 0xb04, PIC_UPPER | PIC_LOWER, },
  418. },
  419. [ C(OP_WRITE) ] = {
  420. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  421. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  422. },
  423. [ C(OP_PREFETCH) ] = {
  424. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  425. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  426. },
  427. },
  428. [C(BPU)] = {
  429. [C(OP_READ)] = {
  430. [C(RESULT_ACCESS)] = { CACHE_OP_UNSUPPORTED },
  431. [C(RESULT_MISS)] = { CACHE_OP_UNSUPPORTED },
  432. },
  433. [ C(OP_WRITE) ] = {
  434. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  435. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  436. },
  437. [ C(OP_PREFETCH) ] = {
  438. [ C(RESULT_ACCESS) ] = { CACHE_OP_UNSUPPORTED },
  439. [ C(RESULT_MISS) ] = { CACHE_OP_UNSUPPORTED },
  440. },
  441. },
  442. };
  443. static const struct sparc_pmu niagara2_pmu = {
  444. .event_map = niagara2_event_map,
  445. .cache_map = &niagara2_cache_map,
  446. .max_events = ARRAY_SIZE(niagara2_perfmon_event_map),
  447. .upper_shift = 19,
  448. .lower_shift = 6,
  449. .event_mask = 0xfff,
  450. .hv_bit = 0x8,
  451. .irq_bit = 0x30,
  452. .upper_nop = 0x220,
  453. .lower_nop = 0x220,
  454. };
  455. static const struct sparc_pmu *sparc_pmu __read_mostly;
  456. static u64 event_encoding(u64 event_id, int idx)
  457. {
  458. if (idx == PIC_UPPER_INDEX)
  459. event_id <<= sparc_pmu->upper_shift;
  460. else
  461. event_id <<= sparc_pmu->lower_shift;
  462. return event_id;
  463. }
  464. static u64 mask_for_index(int idx)
  465. {
  466. return event_encoding(sparc_pmu->event_mask, idx);
  467. }
  468. static u64 nop_for_index(int idx)
  469. {
  470. return event_encoding(idx == PIC_UPPER_INDEX ?
  471. sparc_pmu->upper_nop :
  472. sparc_pmu->lower_nop, idx);
  473. }
  474. static inline void sparc_pmu_enable_event(struct cpu_hw_events *cpuc, struct hw_perf_event *hwc, int idx)
  475. {
  476. u64 val, mask = mask_for_index(idx);
  477. val = cpuc->pcr;
  478. val &= ~mask;
  479. val |= hwc->config;
  480. cpuc->pcr = val;
  481. pcr_ops->write(cpuc->pcr);
  482. }
  483. static inline void sparc_pmu_disable_event(struct cpu_hw_events *cpuc, struct hw_perf_event *hwc, int idx)
  484. {
  485. u64 mask = mask_for_index(idx);
  486. u64 nop = nop_for_index(idx);
  487. u64 val;
  488. val = cpuc->pcr;
  489. val &= ~mask;
  490. val |= nop;
  491. cpuc->pcr = val;
  492. pcr_ops->write(cpuc->pcr);
  493. }
  494. static u32 read_pmc(int idx)
  495. {
  496. u64 val;
  497. read_pic(val);
  498. if (idx == PIC_UPPER_INDEX)
  499. val >>= 32;
  500. return val & 0xffffffff;
  501. }
  502. static void write_pmc(int idx, u64 val)
  503. {
  504. u64 shift, mask, pic;
  505. shift = 0;
  506. if (idx == PIC_UPPER_INDEX)
  507. shift = 32;
  508. mask = ((u64) 0xffffffff) << shift;
  509. val <<= shift;
  510. read_pic(pic);
  511. pic &= ~mask;
  512. pic |= val;
  513. write_pic(pic);
  514. }
  515. static u64 sparc_perf_event_update(struct perf_event *event,
  516. struct hw_perf_event *hwc, int idx)
  517. {
  518. int shift = 64 - 32;
  519. u64 prev_raw_count, new_raw_count;
  520. s64 delta;
  521. again:
  522. prev_raw_count = atomic64_read(&hwc->prev_count);
  523. new_raw_count = read_pmc(idx);
  524. if (atomic64_cmpxchg(&hwc->prev_count, prev_raw_count,
  525. new_raw_count) != prev_raw_count)
  526. goto again;
  527. delta = (new_raw_count << shift) - (prev_raw_count << shift);
  528. delta >>= shift;
  529. atomic64_add(delta, &event->count);
  530. atomic64_sub(delta, &hwc->period_left);
  531. return new_raw_count;
  532. }
  533. static int sparc_perf_event_set_period(struct perf_event *event,
  534. struct hw_perf_event *hwc, int idx)
  535. {
  536. s64 left = atomic64_read(&hwc->period_left);
  537. s64 period = hwc->sample_period;
  538. int ret = 0;
  539. if (unlikely(left <= -period)) {
  540. left = period;
  541. atomic64_set(&hwc->period_left, left);
  542. hwc->last_period = period;
  543. ret = 1;
  544. }
  545. if (unlikely(left <= 0)) {
  546. left += period;
  547. atomic64_set(&hwc->period_left, left);
  548. hwc->last_period = period;
  549. ret = 1;
  550. }
  551. if (left > MAX_PERIOD)
  552. left = MAX_PERIOD;
  553. atomic64_set(&hwc->prev_count, (u64)-left);
  554. write_pmc(idx, (u64)(-left) & 0xffffffff);
  555. perf_event_update_userpage(event);
  556. return ret;
  557. }
  558. /* If performance event entries have been added, move existing
  559. * events around (if necessary) and then assign new entries to
  560. * counters.
  561. */
  562. static u64 maybe_change_configuration(struct cpu_hw_events *cpuc, u64 pcr)
  563. {
  564. int i;
  565. if (!cpuc->n_added)
  566. goto out;
  567. /* Read in the counters which are moving. */
  568. for (i = 0; i < cpuc->n_events; i++) {
  569. struct perf_event *cp = cpuc->event[i];
  570. if (cpuc->current_idx[i] != PIC_NO_INDEX &&
  571. cpuc->current_idx[i] != cp->hw.idx) {
  572. sparc_perf_event_update(cp, &cp->hw,
  573. cpuc->current_idx[i]);
  574. cpuc->current_idx[i] = PIC_NO_INDEX;
  575. }
  576. }
  577. /* Assign to counters all unassigned events. */
  578. for (i = 0; i < cpuc->n_events; i++) {
  579. struct perf_event *cp = cpuc->event[i];
  580. struct hw_perf_event *hwc = &cp->hw;
  581. int idx = hwc->idx;
  582. u64 enc;
  583. if (cpuc->current_idx[i] != PIC_NO_INDEX)
  584. continue;
  585. sparc_perf_event_set_period(cp, hwc, idx);
  586. cpuc->current_idx[i] = idx;
  587. enc = perf_event_get_enc(cpuc->events[i]);
  588. pcr |= event_encoding(enc, idx);
  589. }
  590. out:
  591. return pcr;
  592. }
  593. void hw_perf_enable(void)
  594. {
  595. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  596. u64 pcr;
  597. if (cpuc->enabled)
  598. return;
  599. cpuc->enabled = 1;
  600. barrier();
  601. pcr = cpuc->pcr;
  602. if (!cpuc->n_events) {
  603. pcr = 0;
  604. } else {
  605. pcr = maybe_change_configuration(cpuc, pcr);
  606. /* We require that all of the events have the same
  607. * configuration, so just fetch the settings from the
  608. * first entry.
  609. */
  610. cpuc->pcr = pcr | cpuc->event[0]->hw.config_base;
  611. }
  612. pcr_ops->write(cpuc->pcr);
  613. }
  614. void hw_perf_disable(void)
  615. {
  616. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  617. u64 val;
  618. if (!cpuc->enabled)
  619. return;
  620. cpuc->enabled = 0;
  621. cpuc->n_added = 0;
  622. val = cpuc->pcr;
  623. val &= ~(PCR_UTRACE | PCR_STRACE |
  624. sparc_pmu->hv_bit | sparc_pmu->irq_bit);
  625. cpuc->pcr = val;
  626. pcr_ops->write(cpuc->pcr);
  627. }
  628. static void sparc_pmu_disable(struct perf_event *event)
  629. {
  630. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  631. struct hw_perf_event *hwc = &event->hw;
  632. unsigned long flags;
  633. int i;
  634. local_irq_save(flags);
  635. perf_disable();
  636. for (i = 0; i < cpuc->n_events; i++) {
  637. if (event == cpuc->event[i]) {
  638. int idx = cpuc->current_idx[i];
  639. /* Shift remaining entries down into
  640. * the existing slot.
  641. */
  642. while (++i < cpuc->n_events) {
  643. cpuc->event[i - 1] = cpuc->event[i];
  644. cpuc->events[i - 1] = cpuc->events[i];
  645. cpuc->current_idx[i - 1] =
  646. cpuc->current_idx[i];
  647. }
  648. /* Absorb the final count and turn off the
  649. * event.
  650. */
  651. sparc_pmu_disable_event(cpuc, hwc, idx);
  652. barrier();
  653. sparc_perf_event_update(event, hwc, idx);
  654. perf_event_update_userpage(event);
  655. cpuc->n_events--;
  656. break;
  657. }
  658. }
  659. perf_enable();
  660. local_irq_restore(flags);
  661. }
  662. static int active_event_index(struct cpu_hw_events *cpuc,
  663. struct perf_event *event)
  664. {
  665. int i;
  666. for (i = 0; i < cpuc->n_events; i++) {
  667. if (cpuc->event[i] == event)
  668. break;
  669. }
  670. BUG_ON(i == cpuc->n_events);
  671. return cpuc->current_idx[i];
  672. }
  673. static void sparc_pmu_read(struct perf_event *event)
  674. {
  675. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  676. int idx = active_event_index(cpuc, event);
  677. struct hw_perf_event *hwc = &event->hw;
  678. sparc_perf_event_update(event, hwc, idx);
  679. }
  680. static void sparc_pmu_unthrottle(struct perf_event *event)
  681. {
  682. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  683. int idx = active_event_index(cpuc, event);
  684. struct hw_perf_event *hwc = &event->hw;
  685. sparc_pmu_enable_event(cpuc, hwc, idx);
  686. }
  687. static atomic_t active_events = ATOMIC_INIT(0);
  688. static DEFINE_MUTEX(pmc_grab_mutex);
  689. static void perf_stop_nmi_watchdog(void *unused)
  690. {
  691. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  692. stop_nmi_watchdog(NULL);
  693. cpuc->pcr = pcr_ops->read();
  694. }
  695. void perf_event_grab_pmc(void)
  696. {
  697. if (atomic_inc_not_zero(&active_events))
  698. return;
  699. mutex_lock(&pmc_grab_mutex);
  700. if (atomic_read(&active_events) == 0) {
  701. if (atomic_read(&nmi_active) > 0) {
  702. on_each_cpu(perf_stop_nmi_watchdog, NULL, 1);
  703. BUG_ON(atomic_read(&nmi_active) != 0);
  704. }
  705. atomic_inc(&active_events);
  706. }
  707. mutex_unlock(&pmc_grab_mutex);
  708. }
  709. void perf_event_release_pmc(void)
  710. {
  711. if (atomic_dec_and_mutex_lock(&active_events, &pmc_grab_mutex)) {
  712. if (atomic_read(&nmi_active) == 0)
  713. on_each_cpu(start_nmi_watchdog, NULL, 1);
  714. mutex_unlock(&pmc_grab_mutex);
  715. }
  716. }
  717. static const struct perf_event_map *sparc_map_cache_event(u64 config)
  718. {
  719. unsigned int cache_type, cache_op, cache_result;
  720. const struct perf_event_map *pmap;
  721. if (!sparc_pmu->cache_map)
  722. return ERR_PTR(-ENOENT);
  723. cache_type = (config >> 0) & 0xff;
  724. if (cache_type >= PERF_COUNT_HW_CACHE_MAX)
  725. return ERR_PTR(-EINVAL);
  726. cache_op = (config >> 8) & 0xff;
  727. if (cache_op >= PERF_COUNT_HW_CACHE_OP_MAX)
  728. return ERR_PTR(-EINVAL);
  729. cache_result = (config >> 16) & 0xff;
  730. if (cache_result >= PERF_COUNT_HW_CACHE_RESULT_MAX)
  731. return ERR_PTR(-EINVAL);
  732. pmap = &((*sparc_pmu->cache_map)[cache_type][cache_op][cache_result]);
  733. if (pmap->encoding == CACHE_OP_UNSUPPORTED)
  734. return ERR_PTR(-ENOENT);
  735. if (pmap->encoding == CACHE_OP_NONSENSE)
  736. return ERR_PTR(-EINVAL);
  737. return pmap;
  738. }
  739. static void hw_perf_event_destroy(struct perf_event *event)
  740. {
  741. perf_event_release_pmc();
  742. }
  743. /* Make sure all events can be scheduled into the hardware at
  744. * the same time. This is simplified by the fact that we only
  745. * need to support 2 simultaneous HW events.
  746. *
  747. * As a side effect, the evts[]->hw.idx values will be assigned
  748. * on success. These are pending indexes. When the events are
  749. * actually programmed into the chip, these values will propagate
  750. * to the per-cpu cpuc->current_idx[] slots, see the code in
  751. * maybe_change_configuration() for details.
  752. */
  753. static int sparc_check_constraints(struct perf_event **evts,
  754. unsigned long *events, int n_ev)
  755. {
  756. u8 msk0 = 0, msk1 = 0;
  757. int idx0 = 0;
  758. /* This case is possible when we are invoked from
  759. * hw_perf_group_sched_in().
  760. */
  761. if (!n_ev)
  762. return 0;
  763. if (n_ev > perf_max_events)
  764. return -1;
  765. msk0 = perf_event_get_msk(events[0]);
  766. if (n_ev == 1) {
  767. if (msk0 & PIC_LOWER)
  768. idx0 = 1;
  769. goto success;
  770. }
  771. BUG_ON(n_ev != 2);
  772. msk1 = perf_event_get_msk(events[1]);
  773. /* If both events can go on any counter, OK. */
  774. if (msk0 == (PIC_UPPER | PIC_LOWER) &&
  775. msk1 == (PIC_UPPER | PIC_LOWER))
  776. goto success;
  777. /* If one event is limited to a specific counter,
  778. * and the other can go on both, OK.
  779. */
  780. if ((msk0 == PIC_UPPER || msk0 == PIC_LOWER) &&
  781. msk1 == (PIC_UPPER | PIC_LOWER)) {
  782. if (msk0 & PIC_LOWER)
  783. idx0 = 1;
  784. goto success;
  785. }
  786. if ((msk1 == PIC_UPPER || msk1 == PIC_LOWER) &&
  787. msk0 == (PIC_UPPER | PIC_LOWER)) {
  788. if (msk1 & PIC_UPPER)
  789. idx0 = 1;
  790. goto success;
  791. }
  792. /* If the events are fixed to different counters, OK. */
  793. if ((msk0 == PIC_UPPER && msk1 == PIC_LOWER) ||
  794. (msk0 == PIC_LOWER && msk1 == PIC_UPPER)) {
  795. if (msk0 & PIC_LOWER)
  796. idx0 = 1;
  797. goto success;
  798. }
  799. /* Otherwise, there is a conflict. */
  800. return -1;
  801. success:
  802. evts[0]->hw.idx = idx0;
  803. if (n_ev == 2)
  804. evts[1]->hw.idx = idx0 ^ 1;
  805. return 0;
  806. }
  807. static int check_excludes(struct perf_event **evts, int n_prev, int n_new)
  808. {
  809. int eu = 0, ek = 0, eh = 0;
  810. struct perf_event *event;
  811. int i, n, first;
  812. n = n_prev + n_new;
  813. if (n <= 1)
  814. return 0;
  815. first = 1;
  816. for (i = 0; i < n; i++) {
  817. event = evts[i];
  818. if (first) {
  819. eu = event->attr.exclude_user;
  820. ek = event->attr.exclude_kernel;
  821. eh = event->attr.exclude_hv;
  822. first = 0;
  823. } else if (event->attr.exclude_user != eu ||
  824. event->attr.exclude_kernel != ek ||
  825. event->attr.exclude_hv != eh) {
  826. return -EAGAIN;
  827. }
  828. }
  829. return 0;
  830. }
  831. static int collect_events(struct perf_event *group, int max_count,
  832. struct perf_event *evts[], unsigned long *events,
  833. int *current_idx)
  834. {
  835. struct perf_event *event;
  836. int n = 0;
  837. if (!is_software_event(group)) {
  838. if (n >= max_count)
  839. return -1;
  840. evts[n] = group;
  841. events[n] = group->hw.event_base;
  842. current_idx[n++] = PIC_NO_INDEX;
  843. }
  844. list_for_each_entry(event, &group->sibling_list, group_entry) {
  845. if (!is_software_event(event) &&
  846. event->state != PERF_EVENT_STATE_OFF) {
  847. if (n >= max_count)
  848. return -1;
  849. evts[n] = event;
  850. events[n] = event->hw.event_base;
  851. current_idx[n++] = PIC_NO_INDEX;
  852. }
  853. }
  854. return n;
  855. }
  856. static int sparc_pmu_enable(struct perf_event *event)
  857. {
  858. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  859. int n0, ret = -EAGAIN;
  860. unsigned long flags;
  861. local_irq_save(flags);
  862. perf_disable();
  863. n0 = cpuc->n_events;
  864. if (n0 >= perf_max_events)
  865. goto out;
  866. cpuc->event[n0] = event;
  867. cpuc->events[n0] = event->hw.event_base;
  868. cpuc->current_idx[n0] = PIC_NO_INDEX;
  869. /*
  870. * If group events scheduling transaction was started,
  871. * skip the schedulability test here, it will be peformed
  872. * at commit time(->commit_txn) as a whole
  873. */
  874. if (cpuc->group_flag & PERF_EVENT_TXN_STARTED)
  875. goto nocheck;
  876. if (check_excludes(cpuc->event, n0, 1))
  877. goto out;
  878. if (sparc_check_constraints(cpuc->event, cpuc->events, n0 + 1))
  879. goto out;
  880. nocheck:
  881. cpuc->n_events++;
  882. cpuc->n_added++;
  883. ret = 0;
  884. out:
  885. perf_enable();
  886. local_irq_restore(flags);
  887. return ret;
  888. }
  889. static int __hw_perf_event_init(struct perf_event *event)
  890. {
  891. struct perf_event_attr *attr = &event->attr;
  892. struct perf_event *evts[MAX_HWEVENTS];
  893. struct hw_perf_event *hwc = &event->hw;
  894. unsigned long events[MAX_HWEVENTS];
  895. int current_idx_dmy[MAX_HWEVENTS];
  896. const struct perf_event_map *pmap;
  897. int n;
  898. if (atomic_read(&nmi_active) < 0)
  899. return -ENODEV;
  900. if (attr->type == PERF_TYPE_HARDWARE) {
  901. if (attr->config >= sparc_pmu->max_events)
  902. return -EINVAL;
  903. pmap = sparc_pmu->event_map(attr->config);
  904. } else if (attr->type == PERF_TYPE_HW_CACHE) {
  905. pmap = sparc_map_cache_event(attr->config);
  906. if (IS_ERR(pmap))
  907. return PTR_ERR(pmap);
  908. } else
  909. return -EOPNOTSUPP;
  910. /* We save the enable bits in the config_base. */
  911. hwc->config_base = sparc_pmu->irq_bit;
  912. if (!attr->exclude_user)
  913. hwc->config_base |= PCR_UTRACE;
  914. if (!attr->exclude_kernel)
  915. hwc->config_base |= PCR_STRACE;
  916. if (!attr->exclude_hv)
  917. hwc->config_base |= sparc_pmu->hv_bit;
  918. hwc->event_base = perf_event_encode(pmap);
  919. n = 0;
  920. if (event->group_leader != event) {
  921. n = collect_events(event->group_leader,
  922. perf_max_events - 1,
  923. evts, events, current_idx_dmy);
  924. if (n < 0)
  925. return -EINVAL;
  926. }
  927. events[n] = hwc->event_base;
  928. evts[n] = event;
  929. if (check_excludes(evts, n, 1))
  930. return -EINVAL;
  931. if (sparc_check_constraints(evts, events, n + 1))
  932. return -EINVAL;
  933. hwc->idx = PIC_NO_INDEX;
  934. /* Try to do all error checking before this point, as unwinding
  935. * state after grabbing the PMC is difficult.
  936. */
  937. perf_event_grab_pmc();
  938. event->destroy = hw_perf_event_destroy;
  939. if (!hwc->sample_period) {
  940. hwc->sample_period = MAX_PERIOD;
  941. hwc->last_period = hwc->sample_period;
  942. atomic64_set(&hwc->period_left, hwc->sample_period);
  943. }
  944. return 0;
  945. }
  946. /*
  947. * Start group events scheduling transaction
  948. * Set the flag to make pmu::enable() not perform the
  949. * schedulability test, it will be performed at commit time
  950. */
  951. static void sparc_pmu_start_txn(const struct pmu *pmu)
  952. {
  953. struct cpu_hw_events *cpuhw = &__get_cpu_var(cpu_hw_events);
  954. cpuhw->group_flag |= PERF_EVENT_TXN_STARTED;
  955. }
  956. /*
  957. * Stop group events scheduling transaction
  958. * Clear the flag and pmu::enable() will perform the
  959. * schedulability test.
  960. */
  961. static void sparc_pmu_cancel_txn(const struct pmu *pmu)
  962. {
  963. struct cpu_hw_events *cpuhw = &__get_cpu_var(cpu_hw_events);
  964. cpuhw->group_flag &= ~PERF_EVENT_TXN_STARTED;
  965. }
  966. /*
  967. * Commit group events scheduling transaction
  968. * Perform the group schedulability test as a whole
  969. * Return 0 if success
  970. */
  971. static int sparc_pmu_commit_txn(const struct pmu *pmu)
  972. {
  973. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  974. int n;
  975. if (!sparc_pmu)
  976. return -EINVAL;
  977. cpuc = &__get_cpu_var(cpu_hw_events);
  978. n = cpuc->n_events;
  979. if (check_excludes(cpuc->event, 0, n))
  980. return -EINVAL;
  981. if (sparc_check_constraints(cpuc->event, cpuc->events, n))
  982. return -EAGAIN;
  983. return 0;
  984. }
  985. static const struct pmu pmu = {
  986. .enable = sparc_pmu_enable,
  987. .disable = sparc_pmu_disable,
  988. .read = sparc_pmu_read,
  989. .unthrottle = sparc_pmu_unthrottle,
  990. .start_txn = sparc_pmu_start_txn,
  991. .cancel_txn = sparc_pmu_cancel_txn,
  992. .commit_txn = sparc_pmu_commit_txn,
  993. };
  994. const struct pmu *hw_perf_event_init(struct perf_event *event)
  995. {
  996. int err = __hw_perf_event_init(event);
  997. if (err)
  998. return ERR_PTR(err);
  999. return &pmu;
  1000. }
  1001. void perf_event_print_debug(void)
  1002. {
  1003. unsigned long flags;
  1004. u64 pcr, pic;
  1005. int cpu;
  1006. if (!sparc_pmu)
  1007. return;
  1008. local_irq_save(flags);
  1009. cpu = smp_processor_id();
  1010. pcr = pcr_ops->read();
  1011. read_pic(pic);
  1012. pr_info("\n");
  1013. pr_info("CPU#%d: PCR[%016llx] PIC[%016llx]\n",
  1014. cpu, pcr, pic);
  1015. local_irq_restore(flags);
  1016. }
  1017. static int __kprobes perf_event_nmi_handler(struct notifier_block *self,
  1018. unsigned long cmd, void *__args)
  1019. {
  1020. struct die_args *args = __args;
  1021. struct perf_sample_data data;
  1022. struct cpu_hw_events *cpuc;
  1023. struct pt_regs *regs;
  1024. int i;
  1025. if (!atomic_read(&active_events))
  1026. return NOTIFY_DONE;
  1027. switch (cmd) {
  1028. case DIE_NMI:
  1029. break;
  1030. default:
  1031. return NOTIFY_DONE;
  1032. }
  1033. regs = args->regs;
  1034. perf_sample_data_init(&data, 0);
  1035. cpuc = &__get_cpu_var(cpu_hw_events);
  1036. /* If the PMU has the TOE IRQ enable bits, we need to do a
  1037. * dummy write to the %pcr to clear the overflow bits and thus
  1038. * the interrupt.
  1039. *
  1040. * Do this before we peek at the counters to determine
  1041. * overflow so we don't lose any events.
  1042. */
  1043. if (sparc_pmu->irq_bit)
  1044. pcr_ops->write(cpuc->pcr);
  1045. for (i = 0; i < cpuc->n_events; i++) {
  1046. struct perf_event *event = cpuc->event[i];
  1047. int idx = cpuc->current_idx[i];
  1048. struct hw_perf_event *hwc;
  1049. u64 val;
  1050. hwc = &event->hw;
  1051. val = sparc_perf_event_update(event, hwc, idx);
  1052. if (val & (1ULL << 31))
  1053. continue;
  1054. data.period = event->hw.last_period;
  1055. if (!sparc_perf_event_set_period(event, hwc, idx))
  1056. continue;
  1057. if (perf_event_overflow(event, 1, &data, regs))
  1058. sparc_pmu_disable_event(cpuc, hwc, idx);
  1059. }
  1060. return NOTIFY_STOP;
  1061. }
  1062. static __read_mostly struct notifier_block perf_event_nmi_notifier = {
  1063. .notifier_call = perf_event_nmi_handler,
  1064. };
  1065. static bool __init supported_pmu(void)
  1066. {
  1067. if (!strcmp(sparc_pmu_type, "ultra3") ||
  1068. !strcmp(sparc_pmu_type, "ultra3+") ||
  1069. !strcmp(sparc_pmu_type, "ultra3i") ||
  1070. !strcmp(sparc_pmu_type, "ultra4+")) {
  1071. sparc_pmu = &ultra3_pmu;
  1072. return true;
  1073. }
  1074. if (!strcmp(sparc_pmu_type, "niagara")) {
  1075. sparc_pmu = &niagara1_pmu;
  1076. return true;
  1077. }
  1078. if (!strcmp(sparc_pmu_type, "niagara2")) {
  1079. sparc_pmu = &niagara2_pmu;
  1080. return true;
  1081. }
  1082. return false;
  1083. }
  1084. void __init init_hw_perf_events(void)
  1085. {
  1086. pr_info("Performance events: ");
  1087. if (!supported_pmu()) {
  1088. pr_cont("No support for PMU type '%s'\n", sparc_pmu_type);
  1089. return;
  1090. }
  1091. pr_cont("Supported PMU type is '%s'\n", sparc_pmu_type);
  1092. /* All sparc64 PMUs currently have 2 events. */
  1093. perf_max_events = 2;
  1094. register_die_notifier(&perf_event_nmi_notifier);
  1095. }
  1096. static inline void callchain_store(struct perf_callchain_entry *entry, u64 ip)
  1097. {
  1098. if (entry->nr < PERF_MAX_STACK_DEPTH)
  1099. entry->ip[entry->nr++] = ip;
  1100. }
  1101. static void perf_callchain_kernel(struct pt_regs *regs,
  1102. struct perf_callchain_entry *entry)
  1103. {
  1104. unsigned long ksp, fp;
  1105. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  1106. int graph = 0;
  1107. #endif
  1108. callchain_store(entry, PERF_CONTEXT_KERNEL);
  1109. callchain_store(entry, regs->tpc);
  1110. ksp = regs->u_regs[UREG_I6];
  1111. fp = ksp + STACK_BIAS;
  1112. do {
  1113. struct sparc_stackf *sf;
  1114. struct pt_regs *regs;
  1115. unsigned long pc;
  1116. if (!kstack_valid(current_thread_info(), fp))
  1117. break;
  1118. sf = (struct sparc_stackf *) fp;
  1119. regs = (struct pt_regs *) (sf + 1);
  1120. if (kstack_is_trap_frame(current_thread_info(), regs)) {
  1121. if (user_mode(regs))
  1122. break;
  1123. pc = regs->tpc;
  1124. fp = regs->u_regs[UREG_I6] + STACK_BIAS;
  1125. } else {
  1126. pc = sf->callers_pc;
  1127. fp = (unsigned long)sf->fp + STACK_BIAS;
  1128. }
  1129. callchain_store(entry, pc);
  1130. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  1131. if ((pc + 8UL) == (unsigned long) &return_to_handler) {
  1132. int index = current->curr_ret_stack;
  1133. if (current->ret_stack && index >= graph) {
  1134. pc = current->ret_stack[index - graph].ret;
  1135. callchain_store(entry, pc);
  1136. graph++;
  1137. }
  1138. }
  1139. #endif
  1140. } while (entry->nr < PERF_MAX_STACK_DEPTH);
  1141. }
  1142. static void perf_callchain_user_64(struct pt_regs *regs,
  1143. struct perf_callchain_entry *entry)
  1144. {
  1145. unsigned long ufp;
  1146. callchain_store(entry, PERF_CONTEXT_USER);
  1147. callchain_store(entry, regs->tpc);
  1148. ufp = regs->u_regs[UREG_I6] + STACK_BIAS;
  1149. do {
  1150. struct sparc_stackf *usf, sf;
  1151. unsigned long pc;
  1152. usf = (struct sparc_stackf *) ufp;
  1153. if (__copy_from_user_inatomic(&sf, usf, sizeof(sf)))
  1154. break;
  1155. pc = sf.callers_pc;
  1156. ufp = (unsigned long)sf.fp + STACK_BIAS;
  1157. callchain_store(entry, pc);
  1158. } while (entry->nr < PERF_MAX_STACK_DEPTH);
  1159. }
  1160. static void perf_callchain_user_32(struct pt_regs *regs,
  1161. struct perf_callchain_entry *entry)
  1162. {
  1163. unsigned long ufp;
  1164. callchain_store(entry, PERF_CONTEXT_USER);
  1165. callchain_store(entry, regs->tpc);
  1166. ufp = regs->u_regs[UREG_I6] & 0xffffffffUL;
  1167. do {
  1168. struct sparc_stackf32 *usf, sf;
  1169. unsigned long pc;
  1170. usf = (struct sparc_stackf32 *) ufp;
  1171. if (__copy_from_user_inatomic(&sf, usf, sizeof(sf)))
  1172. break;
  1173. pc = sf.callers_pc;
  1174. ufp = (unsigned long)sf.fp;
  1175. callchain_store(entry, pc);
  1176. } while (entry->nr < PERF_MAX_STACK_DEPTH);
  1177. }
  1178. /* Like powerpc we can't get PMU interrupts within the PMU handler,
  1179. * so no need for separate NMI and IRQ chains as on x86.
  1180. */
  1181. static DEFINE_PER_CPU(struct perf_callchain_entry, callchain);
  1182. struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
  1183. {
  1184. struct perf_callchain_entry *entry = &__get_cpu_var(callchain);
  1185. entry->nr = 0;
  1186. if (!user_mode(regs)) {
  1187. stack_trace_flush();
  1188. perf_callchain_kernel(regs, entry);
  1189. if (current->mm)
  1190. regs = task_pt_regs(current);
  1191. else
  1192. regs = NULL;
  1193. }
  1194. if (regs) {
  1195. flushw_user();
  1196. if (test_thread_flag(TIF_32BIT))
  1197. perf_callchain_user_32(regs, entry);
  1198. else
  1199. perf_callchain_user_64(regs, entry);
  1200. }
  1201. return entry;
  1202. }