perf_event.c 37 KB

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