perf_event.c 36 KB

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