perf_event.c 36 KB

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