xgene_pmu.c 37 KB

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