xgene_pmu.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398
  1. /*
  2. * APM X-Gene SoC PMU (Performance Monitor Unit)
  3. *
  4. * Copyright (c) 2016, Applied Micro Circuits Corporation
  5. * Author: Hoan Tran <hotran@apm.com>
  6. * Tai Nguyen <ttnguyen@apm.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation; either version 2 of the License, or (at your
  11. * option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #include <linux/acpi.h>
  22. #include <linux/clk.h>
  23. #include <linux/cpumask.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/io.h>
  26. #include <linux/mfd/syscon.h>
  27. #include <linux/of_address.h>
  28. #include <linux/of_fdt.h>
  29. #include <linux/of_irq.h>
  30. #include <linux/of_platform.h>
  31. #include <linux/perf_event.h>
  32. #include <linux/platform_device.h>
  33. #include <linux/regmap.h>
  34. #include <linux/slab.h>
  35. #define CSW_CSWCR 0x0000
  36. #define CSW_CSWCR_DUALMCB_MASK BIT(0)
  37. #define MCBADDRMR 0x0000
  38. #define MCBADDRMR_DUALMCU_MODE_MASK BIT(2)
  39. #define PCPPMU_INTSTATUS_REG 0x000
  40. #define PCPPMU_INTMASK_REG 0x004
  41. #define PCPPMU_INTMASK 0x0000000F
  42. #define PCPPMU_INTENMASK 0xFFFFFFFF
  43. #define PCPPMU_INTCLRMASK 0xFFFFFFF0
  44. #define PCPPMU_INT_MCU BIT(0)
  45. #define PCPPMU_INT_MCB BIT(1)
  46. #define PCPPMU_INT_L3C BIT(2)
  47. #define PCPPMU_INT_IOB BIT(3)
  48. #define PMU_MAX_COUNTERS 4
  49. #define PMU_CNT_MAX_PERIOD 0x100000000ULL
  50. #define PMU_OVERFLOW_MASK 0xF
  51. #define PMU_PMCR_E BIT(0)
  52. #define PMU_PMCR_P BIT(1)
  53. #define PMU_PMEVCNTR0 0x000
  54. #define PMU_PMEVCNTR1 0x004
  55. #define PMU_PMEVCNTR2 0x008
  56. #define PMU_PMEVCNTR3 0x00C
  57. #define PMU_PMEVTYPER0 0x400
  58. #define PMU_PMEVTYPER1 0x404
  59. #define PMU_PMEVTYPER2 0x408
  60. #define PMU_PMEVTYPER3 0x40C
  61. #define PMU_PMAMR0 0xA00
  62. #define PMU_PMAMR1 0xA04
  63. #define PMU_PMCNTENSET 0xC00
  64. #define PMU_PMCNTENCLR 0xC20
  65. #define PMU_PMINTENSET 0xC40
  66. #define PMU_PMINTENCLR 0xC60
  67. #define PMU_PMOVSR 0xC80
  68. #define PMU_PMCR 0xE04
  69. #define to_pmu_dev(p) container_of(p, struct xgene_pmu_dev, pmu)
  70. #define GET_CNTR(ev) (ev->hw.idx)
  71. #define GET_EVENTID(ev) (ev->hw.config & 0xFFULL)
  72. #define GET_AGENTID(ev) (ev->hw.config_base & 0xFFFFFFFFUL)
  73. #define GET_AGENT1ID(ev) ((ev->hw.config_base >> 32) & 0xFFFFFFFFUL)
  74. struct hw_pmu_info {
  75. u32 type;
  76. u32 enable_mask;
  77. void __iomem *csr;
  78. };
  79. struct xgene_pmu_dev {
  80. struct hw_pmu_info *inf;
  81. struct xgene_pmu *parent;
  82. struct pmu pmu;
  83. u8 max_counters;
  84. DECLARE_BITMAP(cntr_assign_mask, PMU_MAX_COUNTERS);
  85. u64 max_period;
  86. const struct attribute_group **attr_groups;
  87. struct perf_event *pmu_counter_event[PMU_MAX_COUNTERS];
  88. };
  89. struct xgene_pmu {
  90. struct device *dev;
  91. int version;
  92. void __iomem *pcppmu_csr;
  93. u32 mcb_active_mask;
  94. u32 mc_active_mask;
  95. cpumask_t cpu;
  96. raw_spinlock_t lock;
  97. struct list_head l3cpmus;
  98. struct list_head iobpmus;
  99. struct list_head mcbpmus;
  100. struct list_head mcpmus;
  101. };
  102. struct xgene_pmu_dev_ctx {
  103. char *name;
  104. struct list_head next;
  105. struct xgene_pmu_dev *pmu_dev;
  106. struct hw_pmu_info inf;
  107. };
  108. struct xgene_pmu_data {
  109. int id;
  110. u32 data;
  111. };
  112. enum xgene_pmu_version {
  113. PCP_PMU_V1 = 1,
  114. PCP_PMU_V2,
  115. };
  116. enum xgene_pmu_dev_type {
  117. PMU_TYPE_L3C = 0,
  118. PMU_TYPE_IOB,
  119. PMU_TYPE_MCB,
  120. PMU_TYPE_MC,
  121. };
  122. /*
  123. * sysfs format attributes
  124. */
  125. static ssize_t xgene_pmu_format_show(struct device *dev,
  126. struct device_attribute *attr, char *buf)
  127. {
  128. struct dev_ext_attribute *eattr;
  129. eattr = container_of(attr, struct dev_ext_attribute, attr);
  130. return sprintf(buf, "%s\n", (char *) eattr->var);
  131. }
  132. #define XGENE_PMU_FORMAT_ATTR(_name, _config) \
  133. (&((struct dev_ext_attribute[]) { \
  134. { .attr = __ATTR(_name, S_IRUGO, xgene_pmu_format_show, NULL), \
  135. .var = (void *) _config, } \
  136. })[0].attr.attr)
  137. static struct attribute *l3c_pmu_format_attrs[] = {
  138. XGENE_PMU_FORMAT_ATTR(l3c_eventid, "config:0-7"),
  139. XGENE_PMU_FORMAT_ATTR(l3c_agentid, "config1:0-9"),
  140. NULL,
  141. };
  142. static struct attribute *iob_pmu_format_attrs[] = {
  143. XGENE_PMU_FORMAT_ATTR(iob_eventid, "config:0-7"),
  144. XGENE_PMU_FORMAT_ATTR(iob_agentid, "config1:0-63"),
  145. NULL,
  146. };
  147. static struct attribute *mcb_pmu_format_attrs[] = {
  148. XGENE_PMU_FORMAT_ATTR(mcb_eventid, "config:0-5"),
  149. XGENE_PMU_FORMAT_ATTR(mcb_agentid, "config1:0-9"),
  150. NULL,
  151. };
  152. static struct attribute *mc_pmu_format_attrs[] = {
  153. XGENE_PMU_FORMAT_ATTR(mc_eventid, "config:0-28"),
  154. NULL,
  155. };
  156. static const struct attribute_group l3c_pmu_format_attr_group = {
  157. .name = "format",
  158. .attrs = l3c_pmu_format_attrs,
  159. };
  160. static const struct attribute_group iob_pmu_format_attr_group = {
  161. .name = "format",
  162. .attrs = iob_pmu_format_attrs,
  163. };
  164. static const struct attribute_group mcb_pmu_format_attr_group = {
  165. .name = "format",
  166. .attrs = mcb_pmu_format_attrs,
  167. };
  168. static const struct attribute_group mc_pmu_format_attr_group = {
  169. .name = "format",
  170. .attrs = mc_pmu_format_attrs,
  171. };
  172. /*
  173. * sysfs event attributes
  174. */
  175. static ssize_t xgene_pmu_event_show(struct device *dev,
  176. struct device_attribute *attr, char *buf)
  177. {
  178. struct dev_ext_attribute *eattr;
  179. eattr = container_of(attr, struct dev_ext_attribute, attr);
  180. return sprintf(buf, "config=0x%lx\n", (unsigned long) eattr->var);
  181. }
  182. #define XGENE_PMU_EVENT_ATTR(_name, _config) \
  183. (&((struct dev_ext_attribute[]) { \
  184. { .attr = __ATTR(_name, S_IRUGO, xgene_pmu_event_show, NULL), \
  185. .var = (void *) _config, } \
  186. })[0].attr.attr)
  187. static struct attribute *l3c_pmu_events_attrs[] = {
  188. XGENE_PMU_EVENT_ATTR(cycle-count, 0x00),
  189. XGENE_PMU_EVENT_ATTR(cycle-count-div-64, 0x01),
  190. XGENE_PMU_EVENT_ATTR(read-hit, 0x02),
  191. XGENE_PMU_EVENT_ATTR(read-miss, 0x03),
  192. XGENE_PMU_EVENT_ATTR(write-need-replacement, 0x06),
  193. XGENE_PMU_EVENT_ATTR(write-not-need-replacement, 0x07),
  194. XGENE_PMU_EVENT_ATTR(tq-full, 0x08),
  195. XGENE_PMU_EVENT_ATTR(ackq-full, 0x09),
  196. XGENE_PMU_EVENT_ATTR(wdb-full, 0x0a),
  197. XGENE_PMU_EVENT_ATTR(bank-fifo-full, 0x0b),
  198. XGENE_PMU_EVENT_ATTR(odb-full, 0x0c),
  199. XGENE_PMU_EVENT_ATTR(wbq-full, 0x0d),
  200. XGENE_PMU_EVENT_ATTR(bank-conflict-fifo-issue, 0x0e),
  201. XGENE_PMU_EVENT_ATTR(bank-fifo-issue, 0x0f),
  202. NULL,
  203. };
  204. static struct attribute *iob_pmu_events_attrs[] = {
  205. XGENE_PMU_EVENT_ATTR(cycle-count, 0x00),
  206. XGENE_PMU_EVENT_ATTR(cycle-count-div-64, 0x01),
  207. XGENE_PMU_EVENT_ATTR(axi0-read, 0x02),
  208. XGENE_PMU_EVENT_ATTR(axi0-read-partial, 0x03),
  209. XGENE_PMU_EVENT_ATTR(axi1-read, 0x04),
  210. XGENE_PMU_EVENT_ATTR(axi1-read-partial, 0x05),
  211. XGENE_PMU_EVENT_ATTR(csw-read-block, 0x06),
  212. XGENE_PMU_EVENT_ATTR(csw-read-partial, 0x07),
  213. XGENE_PMU_EVENT_ATTR(axi0-write, 0x10),
  214. XGENE_PMU_EVENT_ATTR(axi0-write-partial, 0x11),
  215. XGENE_PMU_EVENT_ATTR(axi1-write, 0x13),
  216. XGENE_PMU_EVENT_ATTR(axi1-write-partial, 0x14),
  217. XGENE_PMU_EVENT_ATTR(csw-inbound-dirty, 0x16),
  218. NULL,
  219. };
  220. static struct attribute *mcb_pmu_events_attrs[] = {
  221. XGENE_PMU_EVENT_ATTR(cycle-count, 0x00),
  222. XGENE_PMU_EVENT_ATTR(cycle-count-div-64, 0x01),
  223. XGENE_PMU_EVENT_ATTR(csw-read, 0x02),
  224. XGENE_PMU_EVENT_ATTR(csw-write-request, 0x03),
  225. XGENE_PMU_EVENT_ATTR(mcb-csw-stall, 0x04),
  226. XGENE_PMU_EVENT_ATTR(cancel-read-gack, 0x05),
  227. NULL,
  228. };
  229. static struct attribute *mc_pmu_events_attrs[] = {
  230. XGENE_PMU_EVENT_ATTR(cycle-count, 0x00),
  231. XGENE_PMU_EVENT_ATTR(cycle-count-div-64, 0x01),
  232. XGENE_PMU_EVENT_ATTR(act-cmd-sent, 0x02),
  233. XGENE_PMU_EVENT_ATTR(pre-cmd-sent, 0x03),
  234. XGENE_PMU_EVENT_ATTR(rd-cmd-sent, 0x04),
  235. XGENE_PMU_EVENT_ATTR(rda-cmd-sent, 0x05),
  236. XGENE_PMU_EVENT_ATTR(wr-cmd-sent, 0x06),
  237. XGENE_PMU_EVENT_ATTR(wra-cmd-sent, 0x07),
  238. XGENE_PMU_EVENT_ATTR(pde-cmd-sent, 0x08),
  239. XGENE_PMU_EVENT_ATTR(sre-cmd-sent, 0x09),
  240. XGENE_PMU_EVENT_ATTR(prea-cmd-sent, 0x0a),
  241. XGENE_PMU_EVENT_ATTR(ref-cmd-sent, 0x0b),
  242. XGENE_PMU_EVENT_ATTR(rd-rda-cmd-sent, 0x0c),
  243. XGENE_PMU_EVENT_ATTR(wr-wra-cmd-sent, 0x0d),
  244. XGENE_PMU_EVENT_ATTR(in-rd-collision, 0x0e),
  245. XGENE_PMU_EVENT_ATTR(in-wr-collision, 0x0f),
  246. XGENE_PMU_EVENT_ATTR(collision-queue-not-empty, 0x10),
  247. XGENE_PMU_EVENT_ATTR(collision-queue-full, 0x11),
  248. XGENE_PMU_EVENT_ATTR(mcu-request, 0x12),
  249. XGENE_PMU_EVENT_ATTR(mcu-rd-request, 0x13),
  250. XGENE_PMU_EVENT_ATTR(mcu-hp-rd-request, 0x14),
  251. XGENE_PMU_EVENT_ATTR(mcu-wr-request, 0x15),
  252. XGENE_PMU_EVENT_ATTR(mcu-rd-proceed-all, 0x16),
  253. XGENE_PMU_EVENT_ATTR(mcu-rd-proceed-cancel, 0x17),
  254. XGENE_PMU_EVENT_ATTR(mcu-rd-response, 0x18),
  255. XGENE_PMU_EVENT_ATTR(mcu-rd-proceed-speculative-all, 0x19),
  256. XGENE_PMU_EVENT_ATTR(mcu-rd-proceed-speculative-cancel, 0x1a),
  257. XGENE_PMU_EVENT_ATTR(mcu-wr-proceed-all, 0x1b),
  258. XGENE_PMU_EVENT_ATTR(mcu-wr-proceed-cancel, 0x1c),
  259. NULL,
  260. };
  261. static const struct attribute_group l3c_pmu_events_attr_group = {
  262. .name = "events",
  263. .attrs = l3c_pmu_events_attrs,
  264. };
  265. static const struct attribute_group iob_pmu_events_attr_group = {
  266. .name = "events",
  267. .attrs = iob_pmu_events_attrs,
  268. };
  269. static const struct attribute_group mcb_pmu_events_attr_group = {
  270. .name = "events",
  271. .attrs = mcb_pmu_events_attrs,
  272. };
  273. static const struct attribute_group mc_pmu_events_attr_group = {
  274. .name = "events",
  275. .attrs = mc_pmu_events_attrs,
  276. };
  277. /*
  278. * sysfs cpumask attributes
  279. */
  280. static ssize_t xgene_pmu_cpumask_show(struct device *dev,
  281. struct device_attribute *attr, char *buf)
  282. {
  283. struct xgene_pmu_dev *pmu_dev = to_pmu_dev(dev_get_drvdata(dev));
  284. return cpumap_print_to_pagebuf(true, buf, &pmu_dev->parent->cpu);
  285. }
  286. static DEVICE_ATTR(cpumask, S_IRUGO, xgene_pmu_cpumask_show, NULL);
  287. static struct attribute *xgene_pmu_cpumask_attrs[] = {
  288. &dev_attr_cpumask.attr,
  289. NULL,
  290. };
  291. static const struct attribute_group pmu_cpumask_attr_group = {
  292. .attrs = xgene_pmu_cpumask_attrs,
  293. };
  294. /*
  295. * Per PMU device attribute groups
  296. */
  297. static const struct attribute_group *l3c_pmu_attr_groups[] = {
  298. &l3c_pmu_format_attr_group,
  299. &pmu_cpumask_attr_group,
  300. &l3c_pmu_events_attr_group,
  301. NULL
  302. };
  303. static const struct attribute_group *iob_pmu_attr_groups[] = {
  304. &iob_pmu_format_attr_group,
  305. &pmu_cpumask_attr_group,
  306. &iob_pmu_events_attr_group,
  307. NULL
  308. };
  309. static const struct attribute_group *mcb_pmu_attr_groups[] = {
  310. &mcb_pmu_format_attr_group,
  311. &pmu_cpumask_attr_group,
  312. &mcb_pmu_events_attr_group,
  313. NULL
  314. };
  315. static const struct attribute_group *mc_pmu_attr_groups[] = {
  316. &mc_pmu_format_attr_group,
  317. &pmu_cpumask_attr_group,
  318. &mc_pmu_events_attr_group,
  319. NULL
  320. };
  321. static int get_next_avail_cntr(struct xgene_pmu_dev *pmu_dev)
  322. {
  323. int cntr;
  324. cntr = find_first_zero_bit(pmu_dev->cntr_assign_mask,
  325. pmu_dev->max_counters);
  326. if (cntr == pmu_dev->max_counters)
  327. return -ENOSPC;
  328. set_bit(cntr, pmu_dev->cntr_assign_mask);
  329. return cntr;
  330. }
  331. static void clear_avail_cntr(struct xgene_pmu_dev *pmu_dev, int cntr)
  332. {
  333. clear_bit(cntr, pmu_dev->cntr_assign_mask);
  334. }
  335. static inline void xgene_pmu_mask_int(struct xgene_pmu *xgene_pmu)
  336. {
  337. writel(PCPPMU_INTENMASK, xgene_pmu->pcppmu_csr + PCPPMU_INTMASK_REG);
  338. }
  339. static inline void xgene_pmu_unmask_int(struct xgene_pmu *xgene_pmu)
  340. {
  341. writel(PCPPMU_INTCLRMASK, xgene_pmu->pcppmu_csr + PCPPMU_INTMASK_REG);
  342. }
  343. static inline u32 xgene_pmu_read_counter(struct xgene_pmu_dev *pmu_dev, int idx)
  344. {
  345. return readl(pmu_dev->inf->csr + PMU_PMEVCNTR0 + (4 * idx));
  346. }
  347. static inline void
  348. xgene_pmu_write_counter(struct xgene_pmu_dev *pmu_dev, int idx, u32 val)
  349. {
  350. writel(val, pmu_dev->inf->csr + PMU_PMEVCNTR0 + (4 * idx));
  351. }
  352. static inline void
  353. xgene_pmu_write_evttype(struct xgene_pmu_dev *pmu_dev, int idx, u32 val)
  354. {
  355. writel(val, pmu_dev->inf->csr + PMU_PMEVTYPER0 + (4 * idx));
  356. }
  357. static inline void
  358. xgene_pmu_write_agentmsk(struct xgene_pmu_dev *pmu_dev, u32 val)
  359. {
  360. writel(val, pmu_dev->inf->csr + PMU_PMAMR0);
  361. }
  362. static inline void
  363. xgene_pmu_write_agent1msk(struct xgene_pmu_dev *pmu_dev, u32 val)
  364. {
  365. writel(val, pmu_dev->inf->csr + PMU_PMAMR1);
  366. }
  367. static inline void
  368. xgene_pmu_enable_counter(struct xgene_pmu_dev *pmu_dev, int idx)
  369. {
  370. u32 val;
  371. val = readl(pmu_dev->inf->csr + PMU_PMCNTENSET);
  372. val |= 1 << idx;
  373. writel(val, pmu_dev->inf->csr + PMU_PMCNTENSET);
  374. }
  375. static inline void
  376. xgene_pmu_disable_counter(struct xgene_pmu_dev *pmu_dev, int idx)
  377. {
  378. u32 val;
  379. val = readl(pmu_dev->inf->csr + PMU_PMCNTENCLR);
  380. val |= 1 << idx;
  381. writel(val, pmu_dev->inf->csr + PMU_PMCNTENCLR);
  382. }
  383. static inline void
  384. xgene_pmu_enable_counter_int(struct xgene_pmu_dev *pmu_dev, int idx)
  385. {
  386. u32 val;
  387. val = readl(pmu_dev->inf->csr + PMU_PMINTENSET);
  388. val |= 1 << idx;
  389. writel(val, pmu_dev->inf->csr + PMU_PMINTENSET);
  390. }
  391. static inline void
  392. xgene_pmu_disable_counter_int(struct xgene_pmu_dev *pmu_dev, int idx)
  393. {
  394. u32 val;
  395. val = readl(pmu_dev->inf->csr + PMU_PMINTENCLR);
  396. val |= 1 << idx;
  397. writel(val, pmu_dev->inf->csr + PMU_PMINTENCLR);
  398. }
  399. static inline void xgene_pmu_reset_counters(struct xgene_pmu_dev *pmu_dev)
  400. {
  401. u32 val;
  402. val = readl(pmu_dev->inf->csr + PMU_PMCR);
  403. val |= PMU_PMCR_P;
  404. writel(val, pmu_dev->inf->csr + PMU_PMCR);
  405. }
  406. static inline void xgene_pmu_start_counters(struct xgene_pmu_dev *pmu_dev)
  407. {
  408. u32 val;
  409. val = readl(pmu_dev->inf->csr + PMU_PMCR);
  410. val |= PMU_PMCR_E;
  411. writel(val, pmu_dev->inf->csr + PMU_PMCR);
  412. }
  413. static inline void xgene_pmu_stop_counters(struct xgene_pmu_dev *pmu_dev)
  414. {
  415. u32 val;
  416. val = readl(pmu_dev->inf->csr + PMU_PMCR);
  417. val &= ~PMU_PMCR_E;
  418. writel(val, pmu_dev->inf->csr + PMU_PMCR);
  419. }
  420. static void xgene_perf_pmu_enable(struct pmu *pmu)
  421. {
  422. struct xgene_pmu_dev *pmu_dev = to_pmu_dev(pmu);
  423. int enabled = bitmap_weight(pmu_dev->cntr_assign_mask,
  424. pmu_dev->max_counters);
  425. if (!enabled)
  426. return;
  427. xgene_pmu_start_counters(pmu_dev);
  428. }
  429. static void xgene_perf_pmu_disable(struct pmu *pmu)
  430. {
  431. struct xgene_pmu_dev *pmu_dev = to_pmu_dev(pmu);
  432. xgene_pmu_stop_counters(pmu_dev);
  433. }
  434. static int xgene_perf_event_init(struct perf_event *event)
  435. {
  436. struct xgene_pmu_dev *pmu_dev = to_pmu_dev(event->pmu);
  437. struct hw_perf_event *hw = &event->hw;
  438. struct perf_event *sibling;
  439. /* Test the event attr type check for PMU enumeration */
  440. if (event->attr.type != event->pmu->type)
  441. return -ENOENT;
  442. /*
  443. * SOC PMU counters are shared across all cores.
  444. * Therefore, it does not support per-process mode.
  445. * Also, it does not support event sampling mode.
  446. */
  447. if (is_sampling_event(event) || event->attach_state & PERF_ATTACH_TASK)
  448. return -EINVAL;
  449. /* SOC counters do not have usr/os/guest/host bits */
  450. if (event->attr.exclude_user || event->attr.exclude_kernel ||
  451. event->attr.exclude_host || event->attr.exclude_guest)
  452. return -EINVAL;
  453. if (event->cpu < 0)
  454. return -EINVAL;
  455. /*
  456. * Many perf core operations (eg. events rotation) operate on a
  457. * single CPU context. This is obvious for CPU PMUs, where one
  458. * expects the same sets of events being observed on all CPUs,
  459. * but can lead to issues for off-core PMUs, where each
  460. * event could be theoretically assigned to a different CPU. To
  461. * mitigate this, we enforce CPU assignment to one, selected
  462. * processor (the one described in the "cpumask" attribute).
  463. */
  464. event->cpu = cpumask_first(&pmu_dev->parent->cpu);
  465. hw->config = event->attr.config;
  466. /*
  467. * Each bit of the config1 field represents an agent from which the
  468. * request of the event come. The event is counted only if it's caused
  469. * by a request of an agent has the bit cleared.
  470. * By default, the event is counted for all agents.
  471. */
  472. hw->config_base = event->attr.config1;
  473. /*
  474. * We must NOT create groups containing mixed PMUs, although software
  475. * events are acceptable
  476. */
  477. if (event->group_leader->pmu != event->pmu &&
  478. !is_software_event(event->group_leader))
  479. return -EINVAL;
  480. list_for_each_entry(sibling, &event->group_leader->sibling_list,
  481. group_entry)
  482. if (sibling->pmu != event->pmu &&
  483. !is_software_event(sibling))
  484. return -EINVAL;
  485. return 0;
  486. }
  487. static void xgene_perf_enable_event(struct perf_event *event)
  488. {
  489. struct xgene_pmu_dev *pmu_dev = to_pmu_dev(event->pmu);
  490. xgene_pmu_write_evttype(pmu_dev, GET_CNTR(event), GET_EVENTID(event));
  491. xgene_pmu_write_agentmsk(pmu_dev, ~((u32)GET_AGENTID(event)));
  492. if (pmu_dev->inf->type == PMU_TYPE_IOB)
  493. xgene_pmu_write_agent1msk(pmu_dev, ~((u32)GET_AGENT1ID(event)));
  494. xgene_pmu_enable_counter(pmu_dev, GET_CNTR(event));
  495. xgene_pmu_enable_counter_int(pmu_dev, GET_CNTR(event));
  496. }
  497. static void xgene_perf_disable_event(struct perf_event *event)
  498. {
  499. struct xgene_pmu_dev *pmu_dev = to_pmu_dev(event->pmu);
  500. xgene_pmu_disable_counter(pmu_dev, GET_CNTR(event));
  501. xgene_pmu_disable_counter_int(pmu_dev, GET_CNTR(event));
  502. }
  503. static void xgene_perf_event_set_period(struct perf_event *event)
  504. {
  505. struct xgene_pmu_dev *pmu_dev = to_pmu_dev(event->pmu);
  506. struct hw_perf_event *hw = &event->hw;
  507. /*
  508. * The X-Gene PMU counters have a period of 2^32. To account for the
  509. * possiblity of extreme interrupt latency we program for a period of
  510. * half that. Hopefully we can handle the interrupt before another 2^31
  511. * events occur and the counter overtakes its previous value.
  512. */
  513. u64 val = 1ULL << 31;
  514. local64_set(&hw->prev_count, val);
  515. xgene_pmu_write_counter(pmu_dev, hw->idx, (u32) val);
  516. }
  517. static void xgene_perf_event_update(struct perf_event *event)
  518. {
  519. struct xgene_pmu_dev *pmu_dev = to_pmu_dev(event->pmu);
  520. struct hw_perf_event *hw = &event->hw;
  521. u64 delta, prev_raw_count, new_raw_count;
  522. again:
  523. prev_raw_count = local64_read(&hw->prev_count);
  524. new_raw_count = xgene_pmu_read_counter(pmu_dev, GET_CNTR(event));
  525. if (local64_cmpxchg(&hw->prev_count, prev_raw_count,
  526. new_raw_count) != prev_raw_count)
  527. goto again;
  528. delta = (new_raw_count - prev_raw_count) & pmu_dev->max_period;
  529. local64_add(delta, &event->count);
  530. }
  531. static void xgene_perf_read(struct perf_event *event)
  532. {
  533. xgene_perf_event_update(event);
  534. }
  535. static void xgene_perf_start(struct perf_event *event, int flags)
  536. {
  537. struct xgene_pmu_dev *pmu_dev = to_pmu_dev(event->pmu);
  538. struct hw_perf_event *hw = &event->hw;
  539. if (WARN_ON_ONCE(!(hw->state & PERF_HES_STOPPED)))
  540. return;
  541. WARN_ON_ONCE(!(hw->state & PERF_HES_UPTODATE));
  542. hw->state = 0;
  543. xgene_perf_event_set_period(event);
  544. if (flags & PERF_EF_RELOAD) {
  545. u64 prev_raw_count = local64_read(&hw->prev_count);
  546. xgene_pmu_write_counter(pmu_dev, GET_CNTR(event),
  547. (u32) prev_raw_count);
  548. }
  549. xgene_perf_enable_event(event);
  550. perf_event_update_userpage(event);
  551. }
  552. static void xgene_perf_stop(struct perf_event *event, int flags)
  553. {
  554. struct hw_perf_event *hw = &event->hw;
  555. u64 config;
  556. if (hw->state & PERF_HES_UPTODATE)
  557. return;
  558. xgene_perf_disable_event(event);
  559. WARN_ON_ONCE(hw->state & PERF_HES_STOPPED);
  560. hw->state |= PERF_HES_STOPPED;
  561. if (hw->state & PERF_HES_UPTODATE)
  562. return;
  563. config = hw->config;
  564. xgene_perf_read(event);
  565. hw->state |= PERF_HES_UPTODATE;
  566. }
  567. static int xgene_perf_add(struct perf_event *event, int flags)
  568. {
  569. struct xgene_pmu_dev *pmu_dev = to_pmu_dev(event->pmu);
  570. struct hw_perf_event *hw = &event->hw;
  571. hw->state = PERF_HES_UPTODATE | PERF_HES_STOPPED;
  572. /* Allocate an event counter */
  573. hw->idx = get_next_avail_cntr(pmu_dev);
  574. if (hw->idx < 0)
  575. return -EAGAIN;
  576. /* Update counter event pointer for Interrupt handler */
  577. pmu_dev->pmu_counter_event[hw->idx] = event;
  578. if (flags & PERF_EF_START)
  579. xgene_perf_start(event, PERF_EF_RELOAD);
  580. return 0;
  581. }
  582. static void xgene_perf_del(struct perf_event *event, int flags)
  583. {
  584. struct xgene_pmu_dev *pmu_dev = to_pmu_dev(event->pmu);
  585. struct hw_perf_event *hw = &event->hw;
  586. xgene_perf_stop(event, PERF_EF_UPDATE);
  587. /* clear the assigned counter */
  588. clear_avail_cntr(pmu_dev, GET_CNTR(event));
  589. perf_event_update_userpage(event);
  590. pmu_dev->pmu_counter_event[hw->idx] = NULL;
  591. }
  592. static int xgene_init_perf(struct xgene_pmu_dev *pmu_dev, char *name)
  593. {
  594. struct xgene_pmu *xgene_pmu;
  595. pmu_dev->max_period = PMU_CNT_MAX_PERIOD - 1;
  596. /* First version PMU supports only single event counter */
  597. xgene_pmu = pmu_dev->parent;
  598. if (xgene_pmu->version == PCP_PMU_V1)
  599. pmu_dev->max_counters = 1;
  600. else
  601. pmu_dev->max_counters = PMU_MAX_COUNTERS;
  602. /* Perf driver registration */
  603. pmu_dev->pmu = (struct pmu) {
  604. .attr_groups = pmu_dev->attr_groups,
  605. .task_ctx_nr = perf_invalid_context,
  606. .pmu_enable = xgene_perf_pmu_enable,
  607. .pmu_disable = xgene_perf_pmu_disable,
  608. .event_init = xgene_perf_event_init,
  609. .add = xgene_perf_add,
  610. .del = xgene_perf_del,
  611. .start = xgene_perf_start,
  612. .stop = xgene_perf_stop,
  613. .read = xgene_perf_read,
  614. };
  615. /* Hardware counter init */
  616. xgene_pmu_stop_counters(pmu_dev);
  617. xgene_pmu_reset_counters(pmu_dev);
  618. return perf_pmu_register(&pmu_dev->pmu, name, -1);
  619. }
  620. static int
  621. xgene_pmu_dev_add(struct xgene_pmu *xgene_pmu, struct xgene_pmu_dev_ctx *ctx)
  622. {
  623. struct device *dev = xgene_pmu->dev;
  624. struct xgene_pmu_dev *pmu;
  625. int rc;
  626. pmu = devm_kzalloc(dev, sizeof(*pmu), GFP_KERNEL);
  627. if (!pmu)
  628. return -ENOMEM;
  629. pmu->parent = xgene_pmu;
  630. pmu->inf = &ctx->inf;
  631. ctx->pmu_dev = pmu;
  632. switch (pmu->inf->type) {
  633. case PMU_TYPE_L3C:
  634. pmu->attr_groups = l3c_pmu_attr_groups;
  635. break;
  636. case PMU_TYPE_IOB:
  637. pmu->attr_groups = iob_pmu_attr_groups;
  638. break;
  639. case PMU_TYPE_MCB:
  640. if (!(xgene_pmu->mcb_active_mask & pmu->inf->enable_mask))
  641. goto dev_err;
  642. pmu->attr_groups = mcb_pmu_attr_groups;
  643. break;
  644. case PMU_TYPE_MC:
  645. if (!(xgene_pmu->mc_active_mask & pmu->inf->enable_mask))
  646. goto dev_err;
  647. pmu->attr_groups = mc_pmu_attr_groups;
  648. break;
  649. default:
  650. return -EINVAL;
  651. }
  652. rc = xgene_init_perf(pmu, ctx->name);
  653. if (rc) {
  654. dev_err(dev, "%s PMU: Failed to init perf driver\n", ctx->name);
  655. goto dev_err;
  656. }
  657. dev_info(dev, "%s PMU registered\n", ctx->name);
  658. return rc;
  659. dev_err:
  660. devm_kfree(dev, pmu);
  661. return -ENODEV;
  662. }
  663. static void _xgene_pmu_isr(int irq, struct xgene_pmu_dev *pmu_dev)
  664. {
  665. struct xgene_pmu *xgene_pmu = pmu_dev->parent;
  666. u32 pmovsr;
  667. int idx;
  668. pmovsr = readl(pmu_dev->inf->csr + PMU_PMOVSR) & PMU_OVERFLOW_MASK;
  669. if (!pmovsr)
  670. return;
  671. /* Clear interrupt flag */
  672. if (xgene_pmu->version == PCP_PMU_V1)
  673. writel(0x0, pmu_dev->inf->csr + PMU_PMOVSR);
  674. else
  675. writel(pmovsr, pmu_dev->inf->csr + PMU_PMOVSR);
  676. for (idx = 0; idx < PMU_MAX_COUNTERS; idx++) {
  677. struct perf_event *event = pmu_dev->pmu_counter_event[idx];
  678. int overflowed = pmovsr & BIT(idx);
  679. /* Ignore if we don't have an event. */
  680. if (!event || !overflowed)
  681. continue;
  682. xgene_perf_event_update(event);
  683. xgene_perf_event_set_period(event);
  684. }
  685. }
  686. static irqreturn_t xgene_pmu_isr(int irq, void *dev_id)
  687. {
  688. struct xgene_pmu_dev_ctx *ctx;
  689. struct xgene_pmu *xgene_pmu = dev_id;
  690. unsigned long flags;
  691. u32 val;
  692. raw_spin_lock_irqsave(&xgene_pmu->lock, flags);
  693. /* Get Interrupt PMU source */
  694. val = readl(xgene_pmu->pcppmu_csr + PCPPMU_INTSTATUS_REG);
  695. if (val & PCPPMU_INT_MCU) {
  696. list_for_each_entry(ctx, &xgene_pmu->mcpmus, next) {
  697. _xgene_pmu_isr(irq, ctx->pmu_dev);
  698. }
  699. }
  700. if (val & PCPPMU_INT_MCB) {
  701. list_for_each_entry(ctx, &xgene_pmu->mcbpmus, next) {
  702. _xgene_pmu_isr(irq, ctx->pmu_dev);
  703. }
  704. }
  705. if (val & PCPPMU_INT_L3C) {
  706. list_for_each_entry(ctx, &xgene_pmu->l3cpmus, next) {
  707. _xgene_pmu_isr(irq, ctx->pmu_dev);
  708. }
  709. }
  710. if (val & PCPPMU_INT_IOB) {
  711. list_for_each_entry(ctx, &xgene_pmu->iobpmus, next) {
  712. _xgene_pmu_isr(irq, ctx->pmu_dev);
  713. }
  714. }
  715. raw_spin_unlock_irqrestore(&xgene_pmu->lock, flags);
  716. return IRQ_HANDLED;
  717. }
  718. static int acpi_pmu_probe_active_mcb_mcu(struct xgene_pmu *xgene_pmu,
  719. struct platform_device *pdev)
  720. {
  721. void __iomem *csw_csr, *mcba_csr, *mcbb_csr;
  722. struct resource *res;
  723. unsigned int reg;
  724. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  725. csw_csr = devm_ioremap_resource(&pdev->dev, res);
  726. if (IS_ERR(csw_csr)) {
  727. dev_err(&pdev->dev, "ioremap failed for CSW CSR resource\n");
  728. return PTR_ERR(csw_csr);
  729. }
  730. res = platform_get_resource(pdev, IORESOURCE_MEM, 2);
  731. mcba_csr = devm_ioremap_resource(&pdev->dev, res);
  732. if (IS_ERR(mcba_csr)) {
  733. dev_err(&pdev->dev, "ioremap failed for MCBA CSR resource\n");
  734. return PTR_ERR(mcba_csr);
  735. }
  736. res = platform_get_resource(pdev, IORESOURCE_MEM, 3);
  737. mcbb_csr = devm_ioremap_resource(&pdev->dev, res);
  738. if (IS_ERR(mcbb_csr)) {
  739. dev_err(&pdev->dev, "ioremap failed for MCBB CSR resource\n");
  740. return PTR_ERR(mcbb_csr);
  741. }
  742. reg = readl(csw_csr + CSW_CSWCR);
  743. if (reg & CSW_CSWCR_DUALMCB_MASK) {
  744. /* Dual MCB active */
  745. xgene_pmu->mcb_active_mask = 0x3;
  746. /* Probe all active MC(s) */
  747. reg = readl(mcbb_csr + CSW_CSWCR);
  748. xgene_pmu->mc_active_mask =
  749. (reg & MCBADDRMR_DUALMCU_MODE_MASK) ? 0xF : 0x5;
  750. } else {
  751. /* Single MCB active */
  752. xgene_pmu->mcb_active_mask = 0x1;
  753. /* Probe all active MC(s) */
  754. reg = readl(mcba_csr + CSW_CSWCR);
  755. xgene_pmu->mc_active_mask =
  756. (reg & MCBADDRMR_DUALMCU_MODE_MASK) ? 0x3 : 0x1;
  757. }
  758. return 0;
  759. }
  760. static int fdt_pmu_probe_active_mcb_mcu(struct xgene_pmu *xgene_pmu,
  761. struct platform_device *pdev)
  762. {
  763. struct regmap *csw_map, *mcba_map, *mcbb_map;
  764. struct device_node *np = pdev->dev.of_node;
  765. unsigned int reg;
  766. csw_map = syscon_regmap_lookup_by_phandle(np, "regmap-csw");
  767. if (IS_ERR(csw_map)) {
  768. dev_err(&pdev->dev, "unable to get syscon regmap csw\n");
  769. return PTR_ERR(csw_map);
  770. }
  771. mcba_map = syscon_regmap_lookup_by_phandle(np, "regmap-mcba");
  772. if (IS_ERR(mcba_map)) {
  773. dev_err(&pdev->dev, "unable to get syscon regmap mcba\n");
  774. return PTR_ERR(mcba_map);
  775. }
  776. mcbb_map = syscon_regmap_lookup_by_phandle(np, "regmap-mcbb");
  777. if (IS_ERR(mcbb_map)) {
  778. dev_err(&pdev->dev, "unable to get syscon regmap mcbb\n");
  779. return PTR_ERR(mcbb_map);
  780. }
  781. if (regmap_read(csw_map, CSW_CSWCR, &reg))
  782. return -EINVAL;
  783. if (reg & CSW_CSWCR_DUALMCB_MASK) {
  784. /* Dual MCB active */
  785. xgene_pmu->mcb_active_mask = 0x3;
  786. /* Probe all active MC(s) */
  787. if (regmap_read(mcbb_map, MCBADDRMR, &reg))
  788. return 0;
  789. xgene_pmu->mc_active_mask =
  790. (reg & MCBADDRMR_DUALMCU_MODE_MASK) ? 0xF : 0x5;
  791. } else {
  792. /* Single MCB active */
  793. xgene_pmu->mcb_active_mask = 0x1;
  794. /* Probe all active MC(s) */
  795. if (regmap_read(mcba_map, MCBADDRMR, &reg))
  796. return 0;
  797. xgene_pmu->mc_active_mask =
  798. (reg & MCBADDRMR_DUALMCU_MODE_MASK) ? 0x3 : 0x1;
  799. }
  800. return 0;
  801. }
  802. static int xgene_pmu_probe_active_mcb_mcu(struct xgene_pmu *xgene_pmu,
  803. struct platform_device *pdev)
  804. {
  805. if (has_acpi_companion(&pdev->dev))
  806. return acpi_pmu_probe_active_mcb_mcu(xgene_pmu, pdev);
  807. return fdt_pmu_probe_active_mcb_mcu(xgene_pmu, pdev);
  808. }
  809. static char *xgene_pmu_dev_name(struct device *dev, u32 type, int id)
  810. {
  811. switch (type) {
  812. case PMU_TYPE_L3C:
  813. return devm_kasprintf(dev, GFP_KERNEL, "l3c%d", id);
  814. case PMU_TYPE_IOB:
  815. return devm_kasprintf(dev, GFP_KERNEL, "iob%d", id);
  816. case PMU_TYPE_MCB:
  817. return devm_kasprintf(dev, GFP_KERNEL, "mcb%d", id);
  818. case PMU_TYPE_MC:
  819. return devm_kasprintf(dev, GFP_KERNEL, "mc%d", id);
  820. default:
  821. return devm_kasprintf(dev, GFP_KERNEL, "unknown");
  822. }
  823. }
  824. #if defined(CONFIG_ACPI)
  825. static int acpi_pmu_dev_add_resource(struct acpi_resource *ares, void *data)
  826. {
  827. struct resource *res = data;
  828. if (ares->type == ACPI_RESOURCE_TYPE_FIXED_MEMORY32)
  829. acpi_dev_resource_memory(ares, res);
  830. /* Always tell the ACPI core to skip this resource */
  831. return 1;
  832. }
  833. static struct
  834. xgene_pmu_dev_ctx *acpi_get_pmu_hw_inf(struct xgene_pmu *xgene_pmu,
  835. struct acpi_device *adev, u32 type)
  836. {
  837. struct device *dev = xgene_pmu->dev;
  838. struct list_head resource_list;
  839. struct xgene_pmu_dev_ctx *ctx;
  840. const union acpi_object *obj;
  841. struct hw_pmu_info *inf;
  842. void __iomem *dev_csr;
  843. struct resource res;
  844. int enable_bit;
  845. int rc;
  846. ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
  847. if (!ctx)
  848. return NULL;
  849. INIT_LIST_HEAD(&resource_list);
  850. rc = acpi_dev_get_resources(adev, &resource_list,
  851. acpi_pmu_dev_add_resource, &res);
  852. acpi_dev_free_resource_list(&resource_list);
  853. if (rc < 0) {
  854. dev_err(dev, "PMU type %d: No resource address found\n", type);
  855. goto err;
  856. }
  857. dev_csr = devm_ioremap_resource(dev, &res);
  858. if (IS_ERR(dev_csr)) {
  859. dev_err(dev, "PMU type %d: Fail to map resource\n", type);
  860. goto err;
  861. }
  862. /* A PMU device node without enable-bit-index is always enabled */
  863. rc = acpi_dev_get_property(adev, "enable-bit-index",
  864. ACPI_TYPE_INTEGER, &obj);
  865. if (rc < 0)
  866. enable_bit = 0;
  867. else
  868. enable_bit = (int) obj->integer.value;
  869. ctx->name = xgene_pmu_dev_name(dev, type, enable_bit);
  870. if (!ctx->name) {
  871. dev_err(dev, "PMU type %d: Fail to get device name\n", type);
  872. goto err;
  873. }
  874. inf = &ctx->inf;
  875. inf->type = type;
  876. inf->csr = dev_csr;
  877. inf->enable_mask = 1 << enable_bit;
  878. return ctx;
  879. err:
  880. devm_kfree(dev, ctx);
  881. return NULL;
  882. }
  883. static acpi_status acpi_pmu_dev_add(acpi_handle handle, u32 level,
  884. void *data, void **return_value)
  885. {
  886. struct xgene_pmu *xgene_pmu = data;
  887. struct xgene_pmu_dev_ctx *ctx;
  888. struct acpi_device *adev;
  889. if (acpi_bus_get_device(handle, &adev))
  890. return AE_OK;
  891. if (acpi_bus_get_status(adev) || !adev->status.present)
  892. return AE_OK;
  893. if (!strcmp(acpi_device_hid(adev), "APMC0D5D"))
  894. ctx = acpi_get_pmu_hw_inf(xgene_pmu, adev, PMU_TYPE_L3C);
  895. else if (!strcmp(acpi_device_hid(adev), "APMC0D5E"))
  896. ctx = acpi_get_pmu_hw_inf(xgene_pmu, adev, PMU_TYPE_IOB);
  897. else if (!strcmp(acpi_device_hid(adev), "APMC0D5F"))
  898. ctx = acpi_get_pmu_hw_inf(xgene_pmu, adev, PMU_TYPE_MCB);
  899. else if (!strcmp(acpi_device_hid(adev), "APMC0D60"))
  900. ctx = acpi_get_pmu_hw_inf(xgene_pmu, adev, PMU_TYPE_MC);
  901. else
  902. ctx = NULL;
  903. if (!ctx)
  904. return AE_OK;
  905. if (xgene_pmu_dev_add(xgene_pmu, ctx)) {
  906. /* Can't add the PMU device, skip it */
  907. devm_kfree(xgene_pmu->dev, ctx);
  908. return AE_OK;
  909. }
  910. switch (ctx->inf.type) {
  911. case PMU_TYPE_L3C:
  912. list_add(&ctx->next, &xgene_pmu->l3cpmus);
  913. break;
  914. case PMU_TYPE_IOB:
  915. list_add(&ctx->next, &xgene_pmu->iobpmus);
  916. break;
  917. case PMU_TYPE_MCB:
  918. list_add(&ctx->next, &xgene_pmu->mcbpmus);
  919. break;
  920. case PMU_TYPE_MC:
  921. list_add(&ctx->next, &xgene_pmu->mcpmus);
  922. break;
  923. }
  924. return AE_OK;
  925. }
  926. static int acpi_pmu_probe_pmu_dev(struct xgene_pmu *xgene_pmu,
  927. struct platform_device *pdev)
  928. {
  929. struct device *dev = xgene_pmu->dev;
  930. acpi_handle handle;
  931. acpi_status status;
  932. handle = ACPI_HANDLE(dev);
  933. if (!handle)
  934. return -EINVAL;
  935. status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1,
  936. acpi_pmu_dev_add, NULL, xgene_pmu, NULL);
  937. if (ACPI_FAILURE(status)) {
  938. dev_err(dev, "failed to probe PMU devices\n");
  939. return -ENODEV;
  940. }
  941. return 0;
  942. }
  943. #else
  944. static int acpi_pmu_probe_pmu_dev(struct xgene_pmu *xgene_pmu,
  945. struct platform_device *pdev)
  946. {
  947. return 0;
  948. }
  949. #endif
  950. static struct
  951. xgene_pmu_dev_ctx *fdt_get_pmu_hw_inf(struct xgene_pmu *xgene_pmu,
  952. struct device_node *np, u32 type)
  953. {
  954. struct device *dev = xgene_pmu->dev;
  955. struct xgene_pmu_dev_ctx *ctx;
  956. struct hw_pmu_info *inf;
  957. void __iomem *dev_csr;
  958. struct resource res;
  959. int enable_bit;
  960. int rc;
  961. ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
  962. if (!ctx)
  963. return NULL;
  964. rc = of_address_to_resource(np, 0, &res);
  965. if (rc < 0) {
  966. dev_err(dev, "PMU type %d: No resource address found\n", type);
  967. goto err;
  968. }
  969. dev_csr = devm_ioremap_resource(dev, &res);
  970. if (IS_ERR(dev_csr)) {
  971. dev_err(dev, "PMU type %d: Fail to map resource\n", type);
  972. goto err;
  973. }
  974. /* A PMU device node without enable-bit-index is always enabled */
  975. if (of_property_read_u32(np, "enable-bit-index", &enable_bit))
  976. enable_bit = 0;
  977. ctx->name = xgene_pmu_dev_name(dev, type, enable_bit);
  978. if (!ctx->name) {
  979. dev_err(dev, "PMU type %d: Fail to get device name\n", type);
  980. goto err;
  981. }
  982. inf = &ctx->inf;
  983. inf->type = type;
  984. inf->csr = dev_csr;
  985. inf->enable_mask = 1 << enable_bit;
  986. return ctx;
  987. err:
  988. devm_kfree(dev, ctx);
  989. return NULL;
  990. }
  991. static int fdt_pmu_probe_pmu_dev(struct xgene_pmu *xgene_pmu,
  992. struct platform_device *pdev)
  993. {
  994. struct xgene_pmu_dev_ctx *ctx;
  995. struct device_node *np;
  996. for_each_child_of_node(pdev->dev.of_node, np) {
  997. if (!of_device_is_available(np))
  998. continue;
  999. if (of_device_is_compatible(np, "apm,xgene-pmu-l3c"))
  1000. ctx = fdt_get_pmu_hw_inf(xgene_pmu, np, PMU_TYPE_L3C);
  1001. else if (of_device_is_compatible(np, "apm,xgene-pmu-iob"))
  1002. ctx = fdt_get_pmu_hw_inf(xgene_pmu, np, PMU_TYPE_IOB);
  1003. else if (of_device_is_compatible(np, "apm,xgene-pmu-mcb"))
  1004. ctx = fdt_get_pmu_hw_inf(xgene_pmu, np, PMU_TYPE_MCB);
  1005. else if (of_device_is_compatible(np, "apm,xgene-pmu-mc"))
  1006. ctx = fdt_get_pmu_hw_inf(xgene_pmu, np, PMU_TYPE_MC);
  1007. else
  1008. ctx = NULL;
  1009. if (!ctx)
  1010. continue;
  1011. if (xgene_pmu_dev_add(xgene_pmu, ctx)) {
  1012. /* Can't add the PMU device, skip it */
  1013. devm_kfree(xgene_pmu->dev, ctx);
  1014. continue;
  1015. }
  1016. switch (ctx->inf.type) {
  1017. case PMU_TYPE_L3C:
  1018. list_add(&ctx->next, &xgene_pmu->l3cpmus);
  1019. break;
  1020. case PMU_TYPE_IOB:
  1021. list_add(&ctx->next, &xgene_pmu->iobpmus);
  1022. break;
  1023. case PMU_TYPE_MCB:
  1024. list_add(&ctx->next, &xgene_pmu->mcbpmus);
  1025. break;
  1026. case PMU_TYPE_MC:
  1027. list_add(&ctx->next, &xgene_pmu->mcpmus);
  1028. break;
  1029. }
  1030. }
  1031. return 0;
  1032. }
  1033. static int xgene_pmu_probe_pmu_dev(struct xgene_pmu *xgene_pmu,
  1034. struct platform_device *pdev)
  1035. {
  1036. if (has_acpi_companion(&pdev->dev))
  1037. return acpi_pmu_probe_pmu_dev(xgene_pmu, pdev);
  1038. return fdt_pmu_probe_pmu_dev(xgene_pmu, pdev);
  1039. }
  1040. static const struct xgene_pmu_data xgene_pmu_data = {
  1041. .id = PCP_PMU_V1,
  1042. };
  1043. static const struct xgene_pmu_data xgene_pmu_v2_data = {
  1044. .id = PCP_PMU_V2,
  1045. };
  1046. static const struct of_device_id xgene_pmu_of_match[] = {
  1047. { .compatible = "apm,xgene-pmu", .data = &xgene_pmu_data },
  1048. { .compatible = "apm,xgene-pmu-v2", .data = &xgene_pmu_v2_data },
  1049. {},
  1050. };
  1051. MODULE_DEVICE_TABLE(of, xgene_pmu_of_match);
  1052. #ifdef CONFIG_ACPI
  1053. static const struct acpi_device_id xgene_pmu_acpi_match[] = {
  1054. {"APMC0D5B", PCP_PMU_V1},
  1055. {"APMC0D5C", PCP_PMU_V2},
  1056. {},
  1057. };
  1058. MODULE_DEVICE_TABLE(acpi, xgene_pmu_acpi_match);
  1059. #endif
  1060. static int xgene_pmu_probe(struct platform_device *pdev)
  1061. {
  1062. const struct xgene_pmu_data *dev_data;
  1063. const struct of_device_id *of_id;
  1064. struct xgene_pmu *xgene_pmu;
  1065. struct resource *res;
  1066. int irq, rc;
  1067. int version;
  1068. xgene_pmu = devm_kzalloc(&pdev->dev, sizeof(*xgene_pmu), GFP_KERNEL);
  1069. if (!xgene_pmu)
  1070. return -ENOMEM;
  1071. xgene_pmu->dev = &pdev->dev;
  1072. platform_set_drvdata(pdev, xgene_pmu);
  1073. version = -EINVAL;
  1074. of_id = of_match_device(xgene_pmu_of_match, &pdev->dev);
  1075. if (of_id) {
  1076. dev_data = (const struct xgene_pmu_data *) of_id->data;
  1077. version = dev_data->id;
  1078. }
  1079. #ifdef CONFIG_ACPI
  1080. if (ACPI_COMPANION(&pdev->dev)) {
  1081. const struct acpi_device_id *acpi_id;
  1082. acpi_id = acpi_match_device(xgene_pmu_acpi_match, &pdev->dev);
  1083. if (acpi_id)
  1084. version = (int) acpi_id->driver_data;
  1085. }
  1086. #endif
  1087. if (version < 0)
  1088. return -ENODEV;
  1089. INIT_LIST_HEAD(&xgene_pmu->l3cpmus);
  1090. INIT_LIST_HEAD(&xgene_pmu->iobpmus);
  1091. INIT_LIST_HEAD(&xgene_pmu->mcbpmus);
  1092. INIT_LIST_HEAD(&xgene_pmu->mcpmus);
  1093. xgene_pmu->version = version;
  1094. dev_info(&pdev->dev, "X-Gene PMU version %d\n", xgene_pmu->version);
  1095. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1096. xgene_pmu->pcppmu_csr = devm_ioremap_resource(&pdev->dev, res);
  1097. if (IS_ERR(xgene_pmu->pcppmu_csr)) {
  1098. dev_err(&pdev->dev, "ioremap failed for PCP PMU resource\n");
  1099. rc = PTR_ERR(xgene_pmu->pcppmu_csr);
  1100. goto err;
  1101. }
  1102. irq = platform_get_irq(pdev, 0);
  1103. if (irq < 0) {
  1104. dev_err(&pdev->dev, "No IRQ resource\n");
  1105. rc = -EINVAL;
  1106. goto err;
  1107. }
  1108. rc = devm_request_irq(&pdev->dev, irq, xgene_pmu_isr,
  1109. IRQF_NOBALANCING | IRQF_NO_THREAD,
  1110. dev_name(&pdev->dev), xgene_pmu);
  1111. if (rc) {
  1112. dev_err(&pdev->dev, "Could not request IRQ %d\n", irq);
  1113. goto err;
  1114. }
  1115. raw_spin_lock_init(&xgene_pmu->lock);
  1116. /* Check for active MCBs and MCUs */
  1117. rc = xgene_pmu_probe_active_mcb_mcu(xgene_pmu, pdev);
  1118. if (rc) {
  1119. dev_warn(&pdev->dev, "Unknown MCB/MCU active status\n");
  1120. xgene_pmu->mcb_active_mask = 0x1;
  1121. xgene_pmu->mc_active_mask = 0x1;
  1122. }
  1123. /* Pick one core to use for cpumask attributes */
  1124. cpumask_set_cpu(smp_processor_id(), &xgene_pmu->cpu);
  1125. /* Make sure that the overflow interrupt is handled by this CPU */
  1126. rc = irq_set_affinity(irq, &xgene_pmu->cpu);
  1127. if (rc) {
  1128. dev_err(&pdev->dev, "Failed to set interrupt affinity!\n");
  1129. goto err;
  1130. }
  1131. /* Walk through the tree for all PMU perf devices */
  1132. rc = xgene_pmu_probe_pmu_dev(xgene_pmu, pdev);
  1133. if (rc) {
  1134. dev_err(&pdev->dev, "No PMU perf devices found!\n");
  1135. goto err;
  1136. }
  1137. /* Enable interrupt */
  1138. xgene_pmu_unmask_int(xgene_pmu);
  1139. return 0;
  1140. err:
  1141. if (xgene_pmu->pcppmu_csr)
  1142. devm_iounmap(&pdev->dev, xgene_pmu->pcppmu_csr);
  1143. devm_kfree(&pdev->dev, xgene_pmu);
  1144. return rc;
  1145. }
  1146. static void
  1147. xgene_pmu_dev_cleanup(struct xgene_pmu *xgene_pmu, struct list_head *pmus)
  1148. {
  1149. struct xgene_pmu_dev_ctx *ctx;
  1150. struct device *dev = xgene_pmu->dev;
  1151. struct xgene_pmu_dev *pmu_dev;
  1152. list_for_each_entry(ctx, pmus, next) {
  1153. pmu_dev = ctx->pmu_dev;
  1154. if (pmu_dev->inf->csr)
  1155. devm_iounmap(dev, pmu_dev->inf->csr);
  1156. devm_kfree(dev, ctx);
  1157. devm_kfree(dev, pmu_dev);
  1158. }
  1159. }
  1160. static int xgene_pmu_remove(struct platform_device *pdev)
  1161. {
  1162. struct xgene_pmu *xgene_pmu = dev_get_drvdata(&pdev->dev);
  1163. xgene_pmu_dev_cleanup(xgene_pmu, &xgene_pmu->l3cpmus);
  1164. xgene_pmu_dev_cleanup(xgene_pmu, &xgene_pmu->iobpmus);
  1165. xgene_pmu_dev_cleanup(xgene_pmu, &xgene_pmu->mcbpmus);
  1166. xgene_pmu_dev_cleanup(xgene_pmu, &xgene_pmu->mcpmus);
  1167. if (xgene_pmu->pcppmu_csr)
  1168. devm_iounmap(&pdev->dev, xgene_pmu->pcppmu_csr);
  1169. devm_kfree(&pdev->dev, xgene_pmu);
  1170. return 0;
  1171. }
  1172. static struct platform_driver xgene_pmu_driver = {
  1173. .probe = xgene_pmu_probe,
  1174. .remove = xgene_pmu_remove,
  1175. .driver = {
  1176. .name = "xgene-pmu",
  1177. .of_match_table = xgene_pmu_of_match,
  1178. .acpi_match_table = ACPI_PTR(xgene_pmu_acpi_match),
  1179. },
  1180. };
  1181. builtin_platform_driver(xgene_pmu_driver);