dwc_pcie_pmu.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Synopsys DesignWare PCIe PMU driver
  4. *
  5. * Copyright (C) 2021-2023 Alibaba Inc.
  6. */
  7. #include <linux/bitfield.h>
  8. #include <linux/bitops.h>
  9. #include <linux/cpuhotplug.h>
  10. #include <linux/cpumask.h>
  11. #include <linux/device.h>
  12. #include <linux/errno.h>
  13. #include <linux/kernel.h>
  14. #include <linux/list.h>
  15. #include <linux/perf_event.h>
  16. #include <linux/pci.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/smp.h>
  19. #include <linux/sysfs.h>
  20. #include <linux/types.h>
  21. #define DWC_PCIE_VSEC_RAS_DES_ID 0x02
  22. #define DWC_PCIE_EVENT_CNT_CTL 0x8
  23. /*
  24. * Event Counter Data Select includes two parts:
  25. * - 27-24: Group number(4-bit: 0..0x7)
  26. * - 23-16: Event number(8-bit: 0..0x13) within the Group
  27. *
  28. * Put them together as in TRM.
  29. */
  30. #define DWC_PCIE_CNT_EVENT_SEL GENMASK(27, 16)
  31. #define DWC_PCIE_CNT_LANE_SEL GENMASK(11, 8)
  32. #define DWC_PCIE_CNT_STATUS BIT(7)
  33. #define DWC_PCIE_CNT_ENABLE GENMASK(4, 2)
  34. #define DWC_PCIE_PER_EVENT_OFF 0x1
  35. #define DWC_PCIE_PER_EVENT_ON 0x3
  36. #define DWC_PCIE_EVENT_CLEAR GENMASK(1, 0)
  37. #define DWC_PCIE_EVENT_PER_CLEAR 0x1
  38. #define DWC_PCIE_EVENT_CNT_DATA 0xC
  39. #define DWC_PCIE_TIME_BASED_ANAL_CTL 0x10
  40. #define DWC_PCIE_TIME_BASED_REPORT_SEL GENMASK(31, 24)
  41. #define DWC_PCIE_TIME_BASED_DURATION_SEL GENMASK(15, 8)
  42. #define DWC_PCIE_DURATION_MANUAL_CTL 0x0
  43. #define DWC_PCIE_DURATION_1MS 0x1
  44. #define DWC_PCIE_DURATION_10MS 0x2
  45. #define DWC_PCIE_DURATION_100MS 0x3
  46. #define DWC_PCIE_DURATION_1S 0x4
  47. #define DWC_PCIE_DURATION_2S 0x5
  48. #define DWC_PCIE_DURATION_4S 0x6
  49. #define DWC_PCIE_DURATION_4US 0xFF
  50. #define DWC_PCIE_TIME_BASED_TIMER_START BIT(0)
  51. #define DWC_PCIE_TIME_BASED_CNT_ENABLE 0x1
  52. #define DWC_PCIE_TIME_BASED_ANAL_DATA_REG_LOW 0x14
  53. #define DWC_PCIE_TIME_BASED_ANAL_DATA_REG_HIGH 0x18
  54. /* Event attributes */
  55. #define DWC_PCIE_CONFIG_EVENTID GENMASK(15, 0)
  56. #define DWC_PCIE_CONFIG_TYPE GENMASK(19, 16)
  57. #define DWC_PCIE_CONFIG_LANE GENMASK(27, 20)
  58. #define DWC_PCIE_EVENT_ID(event) FIELD_GET(DWC_PCIE_CONFIG_EVENTID, (event)->attr.config)
  59. #define DWC_PCIE_EVENT_TYPE(event) FIELD_GET(DWC_PCIE_CONFIG_TYPE, (event)->attr.config)
  60. #define DWC_PCIE_EVENT_LANE(event) FIELD_GET(DWC_PCIE_CONFIG_LANE, (event)->attr.config)
  61. enum dwc_pcie_event_type {
  62. DWC_PCIE_TIME_BASE_EVENT,
  63. DWC_PCIE_LANE_EVENT,
  64. DWC_PCIE_EVENT_TYPE_MAX,
  65. };
  66. #define DWC_PCIE_LANE_EVENT_MAX_PERIOD GENMASK_ULL(31, 0)
  67. #define DWC_PCIE_MAX_PERIOD GENMASK_ULL(63, 0)
  68. struct dwc_pcie_pmu {
  69. struct pmu pmu;
  70. struct pci_dev *pdev; /* Root Port device */
  71. u16 ras_des_offset;
  72. u32 nr_lanes;
  73. struct list_head pmu_node;
  74. struct hlist_node cpuhp_node;
  75. struct perf_event *event[DWC_PCIE_EVENT_TYPE_MAX];
  76. int on_cpu;
  77. };
  78. #define to_dwc_pcie_pmu(p) (container_of(p, struct dwc_pcie_pmu, pmu))
  79. static int dwc_pcie_pmu_hp_state;
  80. static struct list_head dwc_pcie_dev_info_head =
  81. LIST_HEAD_INIT(dwc_pcie_dev_info_head);
  82. static bool notify;
  83. struct dwc_pcie_dev_info {
  84. struct platform_device *plat_dev;
  85. struct pci_dev *pdev;
  86. struct list_head dev_node;
  87. };
  88. struct dwc_pcie_vendor_id {
  89. int vendor_id;
  90. };
  91. static const struct dwc_pcie_vendor_id dwc_pcie_vendor_ids[] = {
  92. {.vendor_id = PCI_VENDOR_ID_ALIBABA },
  93. {.vendor_id = PCI_VENDOR_ID_QCOM },
  94. {} /* terminator */
  95. };
  96. static ssize_t cpumask_show(struct device *dev,
  97. struct device_attribute *attr,
  98. char *buf)
  99. {
  100. struct dwc_pcie_pmu *pcie_pmu = to_dwc_pcie_pmu(dev_get_drvdata(dev));
  101. return cpumap_print_to_pagebuf(true, buf, cpumask_of(pcie_pmu->on_cpu));
  102. }
  103. static DEVICE_ATTR_RO(cpumask);
  104. static struct attribute *dwc_pcie_pmu_cpumask_attrs[] = {
  105. &dev_attr_cpumask.attr,
  106. NULL
  107. };
  108. static struct attribute_group dwc_pcie_cpumask_attr_group = {
  109. .attrs = dwc_pcie_pmu_cpumask_attrs,
  110. };
  111. struct dwc_pcie_format_attr {
  112. struct device_attribute attr;
  113. u64 field;
  114. int config;
  115. };
  116. PMU_FORMAT_ATTR(eventid, "config:0-15");
  117. PMU_FORMAT_ATTR(type, "config:16-19");
  118. PMU_FORMAT_ATTR(lane, "config:20-27");
  119. static struct attribute *dwc_pcie_format_attrs[] = {
  120. &format_attr_type.attr,
  121. &format_attr_eventid.attr,
  122. &format_attr_lane.attr,
  123. NULL,
  124. };
  125. static struct attribute_group dwc_pcie_format_attrs_group = {
  126. .name = "format",
  127. .attrs = dwc_pcie_format_attrs,
  128. };
  129. struct dwc_pcie_event_attr {
  130. struct device_attribute attr;
  131. enum dwc_pcie_event_type type;
  132. u16 eventid;
  133. u8 lane;
  134. };
  135. static ssize_t dwc_pcie_event_show(struct device *dev,
  136. struct device_attribute *attr, char *buf)
  137. {
  138. struct dwc_pcie_event_attr *eattr;
  139. eattr = container_of(attr, typeof(*eattr), attr);
  140. if (eattr->type == DWC_PCIE_LANE_EVENT)
  141. return sysfs_emit(buf, "eventid=0x%x,type=0x%x,lane=?\n",
  142. eattr->eventid, eattr->type);
  143. else if (eattr->type == DWC_PCIE_TIME_BASE_EVENT)
  144. return sysfs_emit(buf, "eventid=0x%x,type=0x%x\n",
  145. eattr->eventid, eattr->type);
  146. return 0;
  147. }
  148. #define DWC_PCIE_EVENT_ATTR(_name, _type, _eventid, _lane) \
  149. (&((struct dwc_pcie_event_attr[]) {{ \
  150. .attr = __ATTR(_name, 0444, dwc_pcie_event_show, NULL), \
  151. .type = _type, \
  152. .eventid = _eventid, \
  153. .lane = _lane, \
  154. }})[0].attr.attr)
  155. #define DWC_PCIE_PMU_TIME_BASE_EVENT_ATTR(_name, _eventid) \
  156. DWC_PCIE_EVENT_ATTR(_name, DWC_PCIE_TIME_BASE_EVENT, _eventid, 0)
  157. #define DWC_PCIE_PMU_LANE_EVENT_ATTR(_name, _eventid) \
  158. DWC_PCIE_EVENT_ATTR(_name, DWC_PCIE_LANE_EVENT, _eventid, 0)
  159. static struct attribute *dwc_pcie_pmu_time_event_attrs[] = {
  160. /* Group #0 */
  161. DWC_PCIE_PMU_TIME_BASE_EVENT_ATTR(one_cycle, 0x00),
  162. DWC_PCIE_PMU_TIME_BASE_EVENT_ATTR(TX_L0S, 0x01),
  163. DWC_PCIE_PMU_TIME_BASE_EVENT_ATTR(RX_L0S, 0x02),
  164. DWC_PCIE_PMU_TIME_BASE_EVENT_ATTR(L0, 0x03),
  165. DWC_PCIE_PMU_TIME_BASE_EVENT_ATTR(L1, 0x04),
  166. DWC_PCIE_PMU_TIME_BASE_EVENT_ATTR(L1_1, 0x05),
  167. DWC_PCIE_PMU_TIME_BASE_EVENT_ATTR(L1_2, 0x06),
  168. DWC_PCIE_PMU_TIME_BASE_EVENT_ATTR(CFG_RCVRY, 0x07),
  169. DWC_PCIE_PMU_TIME_BASE_EVENT_ATTR(TX_RX_L0S, 0x08),
  170. DWC_PCIE_PMU_TIME_BASE_EVENT_ATTR(L1_AUX, 0x09),
  171. /* Group #1 */
  172. DWC_PCIE_PMU_TIME_BASE_EVENT_ATTR(Tx_PCIe_TLP_Data_Payload, 0x20),
  173. DWC_PCIE_PMU_TIME_BASE_EVENT_ATTR(Rx_PCIe_TLP_Data_Payload, 0x21),
  174. DWC_PCIE_PMU_TIME_BASE_EVENT_ATTR(Tx_CCIX_TLP_Data_Payload, 0x22),
  175. DWC_PCIE_PMU_TIME_BASE_EVENT_ATTR(Rx_CCIX_TLP_Data_Payload, 0x23),
  176. /*
  177. * Leave it to the user to specify the lane ID to avoid generating
  178. * a list of hundreds of events.
  179. */
  180. DWC_PCIE_PMU_LANE_EVENT_ATTR(tx_ack_dllp, 0x600),
  181. DWC_PCIE_PMU_LANE_EVENT_ATTR(tx_update_fc_dllp, 0x601),
  182. DWC_PCIE_PMU_LANE_EVENT_ATTR(rx_ack_dllp, 0x602),
  183. DWC_PCIE_PMU_LANE_EVENT_ATTR(rx_update_fc_dllp, 0x603),
  184. DWC_PCIE_PMU_LANE_EVENT_ATTR(rx_nulified_tlp, 0x604),
  185. DWC_PCIE_PMU_LANE_EVENT_ATTR(tx_nulified_tlp, 0x605),
  186. DWC_PCIE_PMU_LANE_EVENT_ATTR(rx_duplicate_tl, 0x606),
  187. DWC_PCIE_PMU_LANE_EVENT_ATTR(tx_memory_write, 0x700),
  188. DWC_PCIE_PMU_LANE_EVENT_ATTR(tx_memory_read, 0x701),
  189. DWC_PCIE_PMU_LANE_EVENT_ATTR(tx_configuration_write, 0x702),
  190. DWC_PCIE_PMU_LANE_EVENT_ATTR(tx_configuration_read, 0x703),
  191. DWC_PCIE_PMU_LANE_EVENT_ATTR(tx_io_write, 0x704),
  192. DWC_PCIE_PMU_LANE_EVENT_ATTR(tx_io_read, 0x705),
  193. DWC_PCIE_PMU_LANE_EVENT_ATTR(tx_completion_without_data, 0x706),
  194. DWC_PCIE_PMU_LANE_EVENT_ATTR(tx_completion_with_data, 0x707),
  195. DWC_PCIE_PMU_LANE_EVENT_ATTR(tx_message_tlp, 0x708),
  196. DWC_PCIE_PMU_LANE_EVENT_ATTR(tx_atomic, 0x709),
  197. DWC_PCIE_PMU_LANE_EVENT_ATTR(tx_tlp_with_prefix, 0x70A),
  198. DWC_PCIE_PMU_LANE_EVENT_ATTR(rx_memory_write, 0x70B),
  199. DWC_PCIE_PMU_LANE_EVENT_ATTR(rx_memory_read, 0x70C),
  200. DWC_PCIE_PMU_LANE_EVENT_ATTR(rx_io_write, 0x70F),
  201. DWC_PCIE_PMU_LANE_EVENT_ATTR(rx_io_read, 0x710),
  202. DWC_PCIE_PMU_LANE_EVENT_ATTR(rx_completion_without_data, 0x711),
  203. DWC_PCIE_PMU_LANE_EVENT_ATTR(rx_completion_with_data, 0x712),
  204. DWC_PCIE_PMU_LANE_EVENT_ATTR(rx_message_tlp, 0x713),
  205. DWC_PCIE_PMU_LANE_EVENT_ATTR(rx_atomic, 0x714),
  206. DWC_PCIE_PMU_LANE_EVENT_ATTR(rx_tlp_with_prefix, 0x715),
  207. DWC_PCIE_PMU_LANE_EVENT_ATTR(tx_ccix_tlp, 0x716),
  208. DWC_PCIE_PMU_LANE_EVENT_ATTR(rx_ccix_tlp, 0x717),
  209. NULL
  210. };
  211. static const struct attribute_group dwc_pcie_event_attrs_group = {
  212. .name = "events",
  213. .attrs = dwc_pcie_pmu_time_event_attrs,
  214. };
  215. static const struct attribute_group *dwc_pcie_attr_groups[] = {
  216. &dwc_pcie_event_attrs_group,
  217. &dwc_pcie_format_attrs_group,
  218. &dwc_pcie_cpumask_attr_group,
  219. NULL
  220. };
  221. static void dwc_pcie_pmu_lane_event_enable(struct dwc_pcie_pmu *pcie_pmu,
  222. bool enable)
  223. {
  224. struct pci_dev *pdev = pcie_pmu->pdev;
  225. u16 ras_des_offset = pcie_pmu->ras_des_offset;
  226. if (enable)
  227. pci_clear_and_set_config_dword(pdev,
  228. ras_des_offset + DWC_PCIE_EVENT_CNT_CTL,
  229. DWC_PCIE_CNT_ENABLE, DWC_PCIE_PER_EVENT_ON);
  230. else
  231. pci_clear_and_set_config_dword(pdev,
  232. ras_des_offset + DWC_PCIE_EVENT_CNT_CTL,
  233. DWC_PCIE_CNT_ENABLE, DWC_PCIE_PER_EVENT_OFF);
  234. }
  235. static void dwc_pcie_pmu_time_based_event_enable(struct dwc_pcie_pmu *pcie_pmu,
  236. bool enable)
  237. {
  238. struct pci_dev *pdev = pcie_pmu->pdev;
  239. u16 ras_des_offset = pcie_pmu->ras_des_offset;
  240. pci_clear_and_set_config_dword(pdev,
  241. ras_des_offset + DWC_PCIE_TIME_BASED_ANAL_CTL,
  242. DWC_PCIE_TIME_BASED_TIMER_START, enable);
  243. }
  244. static u64 dwc_pcie_pmu_read_lane_event_counter(struct perf_event *event)
  245. {
  246. struct dwc_pcie_pmu *pcie_pmu = to_dwc_pcie_pmu(event->pmu);
  247. struct pci_dev *pdev = pcie_pmu->pdev;
  248. u16 ras_des_offset = pcie_pmu->ras_des_offset;
  249. u32 val;
  250. pci_read_config_dword(pdev, ras_des_offset + DWC_PCIE_EVENT_CNT_DATA, &val);
  251. return val;
  252. }
  253. static u64 dwc_pcie_pmu_read_time_based_counter(struct perf_event *event)
  254. {
  255. struct dwc_pcie_pmu *pcie_pmu = to_dwc_pcie_pmu(event->pmu);
  256. struct pci_dev *pdev = pcie_pmu->pdev;
  257. int event_id = DWC_PCIE_EVENT_ID(event);
  258. u16 ras_des_offset = pcie_pmu->ras_des_offset;
  259. u32 lo, hi, ss;
  260. u64 val;
  261. /*
  262. * The 64-bit value of the data counter is spread across two
  263. * registers that are not synchronized. In order to read them
  264. * atomically, ensure that the high 32 bits match before and after
  265. * reading the low 32 bits.
  266. */
  267. pci_read_config_dword(pdev,
  268. ras_des_offset + DWC_PCIE_TIME_BASED_ANAL_DATA_REG_HIGH, &hi);
  269. do {
  270. /* snapshot the high 32 bits */
  271. ss = hi;
  272. pci_read_config_dword(
  273. pdev, ras_des_offset + DWC_PCIE_TIME_BASED_ANAL_DATA_REG_LOW,
  274. &lo);
  275. pci_read_config_dword(
  276. pdev, ras_des_offset + DWC_PCIE_TIME_BASED_ANAL_DATA_REG_HIGH,
  277. &hi);
  278. } while (hi != ss);
  279. val = ((u64)hi << 32) | lo;
  280. /*
  281. * The Group#1 event measures the amount of data processed in 16-byte
  282. * units. Simplify the end-user interface by multiplying the counter
  283. * at the point of read.
  284. */
  285. if (event_id >= 0x20 && event_id <= 0x23)
  286. val *= 16;
  287. return val;
  288. }
  289. static void dwc_pcie_pmu_event_update(struct perf_event *event)
  290. {
  291. struct hw_perf_event *hwc = &event->hw;
  292. enum dwc_pcie_event_type type = DWC_PCIE_EVENT_TYPE(event);
  293. u64 delta, prev, now = 0;
  294. do {
  295. prev = local64_read(&hwc->prev_count);
  296. if (type == DWC_PCIE_LANE_EVENT)
  297. now = dwc_pcie_pmu_read_lane_event_counter(event);
  298. else if (type == DWC_PCIE_TIME_BASE_EVENT)
  299. now = dwc_pcie_pmu_read_time_based_counter(event);
  300. } while (local64_cmpxchg(&hwc->prev_count, prev, now) != prev);
  301. delta = (now - prev) & DWC_PCIE_MAX_PERIOD;
  302. /* 32-bit counter for Lane Event Counting */
  303. if (type == DWC_PCIE_LANE_EVENT)
  304. delta &= DWC_PCIE_LANE_EVENT_MAX_PERIOD;
  305. local64_add(delta, &event->count);
  306. }
  307. static int dwc_pcie_pmu_event_init(struct perf_event *event)
  308. {
  309. struct dwc_pcie_pmu *pcie_pmu = to_dwc_pcie_pmu(event->pmu);
  310. enum dwc_pcie_event_type type = DWC_PCIE_EVENT_TYPE(event);
  311. struct perf_event *sibling;
  312. u32 lane;
  313. if (event->attr.type != event->pmu->type)
  314. return -ENOENT;
  315. /* We don't support sampling */
  316. if (is_sampling_event(event))
  317. return -EINVAL;
  318. /* We cannot support task bound events */
  319. if (event->cpu < 0 || event->attach_state & PERF_ATTACH_TASK)
  320. return -EINVAL;
  321. if (event->group_leader != event &&
  322. !is_software_event(event->group_leader))
  323. return -EINVAL;
  324. for_each_sibling_event(sibling, event->group_leader) {
  325. if (sibling->pmu != event->pmu && !is_software_event(sibling))
  326. return -EINVAL;
  327. }
  328. if (type < 0 || type >= DWC_PCIE_EVENT_TYPE_MAX)
  329. return -EINVAL;
  330. if (type == DWC_PCIE_LANE_EVENT) {
  331. lane = DWC_PCIE_EVENT_LANE(event);
  332. if (lane < 0 || lane >= pcie_pmu->nr_lanes)
  333. return -EINVAL;
  334. }
  335. event->cpu = pcie_pmu->on_cpu;
  336. return 0;
  337. }
  338. static void dwc_pcie_pmu_event_start(struct perf_event *event, int flags)
  339. {
  340. struct hw_perf_event *hwc = &event->hw;
  341. struct dwc_pcie_pmu *pcie_pmu = to_dwc_pcie_pmu(event->pmu);
  342. enum dwc_pcie_event_type type = DWC_PCIE_EVENT_TYPE(event);
  343. hwc->state = 0;
  344. local64_set(&hwc->prev_count, 0);
  345. if (type == DWC_PCIE_LANE_EVENT)
  346. dwc_pcie_pmu_lane_event_enable(pcie_pmu, true);
  347. else if (type == DWC_PCIE_TIME_BASE_EVENT)
  348. dwc_pcie_pmu_time_based_event_enable(pcie_pmu, true);
  349. }
  350. static void dwc_pcie_pmu_event_stop(struct perf_event *event, int flags)
  351. {
  352. struct dwc_pcie_pmu *pcie_pmu = to_dwc_pcie_pmu(event->pmu);
  353. enum dwc_pcie_event_type type = DWC_PCIE_EVENT_TYPE(event);
  354. struct hw_perf_event *hwc = &event->hw;
  355. if (event->hw.state & PERF_HES_STOPPED)
  356. return;
  357. if (type == DWC_PCIE_LANE_EVENT)
  358. dwc_pcie_pmu_lane_event_enable(pcie_pmu, false);
  359. else if (type == DWC_PCIE_TIME_BASE_EVENT)
  360. dwc_pcie_pmu_time_based_event_enable(pcie_pmu, false);
  361. dwc_pcie_pmu_event_update(event);
  362. hwc->state |= PERF_HES_STOPPED | PERF_HES_UPTODATE;
  363. }
  364. static int dwc_pcie_pmu_event_add(struct perf_event *event, int flags)
  365. {
  366. struct dwc_pcie_pmu *pcie_pmu = to_dwc_pcie_pmu(event->pmu);
  367. struct pci_dev *pdev = pcie_pmu->pdev;
  368. struct hw_perf_event *hwc = &event->hw;
  369. enum dwc_pcie_event_type type = DWC_PCIE_EVENT_TYPE(event);
  370. int event_id = DWC_PCIE_EVENT_ID(event);
  371. int lane = DWC_PCIE_EVENT_LANE(event);
  372. u16 ras_des_offset = pcie_pmu->ras_des_offset;
  373. u32 ctrl;
  374. /* one counter for each type and it is in use */
  375. if (pcie_pmu->event[type])
  376. return -ENOSPC;
  377. pcie_pmu->event[type] = event;
  378. hwc->state = PERF_HES_STOPPED | PERF_HES_UPTODATE;
  379. if (type == DWC_PCIE_LANE_EVENT) {
  380. /* EVENT_COUNTER_DATA_REG needs clear manually */
  381. ctrl = FIELD_PREP(DWC_PCIE_CNT_EVENT_SEL, event_id) |
  382. FIELD_PREP(DWC_PCIE_CNT_LANE_SEL, lane) |
  383. FIELD_PREP(DWC_PCIE_CNT_ENABLE, DWC_PCIE_PER_EVENT_OFF) |
  384. FIELD_PREP(DWC_PCIE_EVENT_CLEAR, DWC_PCIE_EVENT_PER_CLEAR);
  385. pci_write_config_dword(pdev, ras_des_offset + DWC_PCIE_EVENT_CNT_CTL,
  386. ctrl);
  387. } else if (type == DWC_PCIE_TIME_BASE_EVENT) {
  388. /*
  389. * TIME_BASED_ANAL_DATA_REG is a 64 bit register, we can safely
  390. * use it with any manually controlled duration. And it is
  391. * cleared when next measurement starts.
  392. */
  393. ctrl = FIELD_PREP(DWC_PCIE_TIME_BASED_REPORT_SEL, event_id) |
  394. FIELD_PREP(DWC_PCIE_TIME_BASED_DURATION_SEL,
  395. DWC_PCIE_DURATION_MANUAL_CTL) |
  396. DWC_PCIE_TIME_BASED_CNT_ENABLE;
  397. pci_write_config_dword(
  398. pdev, ras_des_offset + DWC_PCIE_TIME_BASED_ANAL_CTL, ctrl);
  399. }
  400. if (flags & PERF_EF_START)
  401. dwc_pcie_pmu_event_start(event, PERF_EF_RELOAD);
  402. perf_event_update_userpage(event);
  403. return 0;
  404. }
  405. static void dwc_pcie_pmu_event_del(struct perf_event *event, int flags)
  406. {
  407. struct dwc_pcie_pmu *pcie_pmu = to_dwc_pcie_pmu(event->pmu);
  408. enum dwc_pcie_event_type type = DWC_PCIE_EVENT_TYPE(event);
  409. dwc_pcie_pmu_event_stop(event, flags | PERF_EF_UPDATE);
  410. perf_event_update_userpage(event);
  411. pcie_pmu->event[type] = NULL;
  412. }
  413. static void dwc_pcie_pmu_remove_cpuhp_instance(void *hotplug_node)
  414. {
  415. cpuhp_state_remove_instance_nocalls(dwc_pcie_pmu_hp_state, hotplug_node);
  416. }
  417. /*
  418. * Find the binded DES capability device info of a PCI device.
  419. * @pdev: The PCI device.
  420. */
  421. static struct dwc_pcie_dev_info *dwc_pcie_find_dev_info(struct pci_dev *pdev)
  422. {
  423. struct dwc_pcie_dev_info *dev_info;
  424. list_for_each_entry(dev_info, &dwc_pcie_dev_info_head, dev_node)
  425. if (dev_info->pdev == pdev)
  426. return dev_info;
  427. return NULL;
  428. }
  429. static void dwc_pcie_unregister_pmu(void *data)
  430. {
  431. struct dwc_pcie_pmu *pcie_pmu = data;
  432. perf_pmu_unregister(&pcie_pmu->pmu);
  433. }
  434. static bool dwc_pcie_match_des_cap(struct pci_dev *pdev)
  435. {
  436. const struct dwc_pcie_vendor_id *vid;
  437. u16 vsec = 0;
  438. u32 val;
  439. if (!pci_is_pcie(pdev) || !(pci_pcie_type(pdev) == PCI_EXP_TYPE_ROOT_PORT))
  440. return false;
  441. for (vid = dwc_pcie_vendor_ids; vid->vendor_id; vid++) {
  442. vsec = pci_find_vsec_capability(pdev, vid->vendor_id,
  443. DWC_PCIE_VSEC_RAS_DES_ID);
  444. if (vsec)
  445. break;
  446. }
  447. if (!vsec)
  448. return false;
  449. pci_read_config_dword(pdev, vsec + PCI_VNDR_HEADER, &val);
  450. if (PCI_VNDR_HEADER_REV(val) != 0x04)
  451. return false;
  452. pci_dbg(pdev,
  453. "Detected PCIe Vendor-Specific Extended Capability RAS DES\n");
  454. return true;
  455. }
  456. static void dwc_pcie_unregister_dev(struct dwc_pcie_dev_info *dev_info)
  457. {
  458. platform_device_unregister(dev_info->plat_dev);
  459. list_del(&dev_info->dev_node);
  460. kfree(dev_info);
  461. }
  462. static int dwc_pcie_register_dev(struct pci_dev *pdev)
  463. {
  464. struct platform_device *plat_dev;
  465. struct dwc_pcie_dev_info *dev_info;
  466. u32 sbdf;
  467. sbdf = (pci_domain_nr(pdev->bus) << 16) | PCI_DEVID(pdev->bus->number, pdev->devfn);
  468. plat_dev = platform_device_register_data(NULL, "dwc_pcie_pmu", sbdf,
  469. pdev, sizeof(*pdev));
  470. if (IS_ERR(plat_dev))
  471. return PTR_ERR(plat_dev);
  472. dev_info = kzalloc(sizeof(*dev_info), GFP_KERNEL);
  473. if (!dev_info) {
  474. platform_device_unregister(plat_dev);
  475. return -ENOMEM;
  476. }
  477. /* Cache platform device to handle pci device hotplug */
  478. dev_info->plat_dev = plat_dev;
  479. dev_info->pdev = pdev;
  480. list_add(&dev_info->dev_node, &dwc_pcie_dev_info_head);
  481. return 0;
  482. }
  483. static int dwc_pcie_pmu_notifier(struct notifier_block *nb,
  484. unsigned long action, void *data)
  485. {
  486. struct device *dev = data;
  487. struct pci_dev *pdev = to_pci_dev(dev);
  488. struct dwc_pcie_dev_info *dev_info;
  489. switch (action) {
  490. case BUS_NOTIFY_ADD_DEVICE:
  491. if (!dwc_pcie_match_des_cap(pdev))
  492. return NOTIFY_DONE;
  493. if (dwc_pcie_register_dev(pdev))
  494. return NOTIFY_BAD;
  495. break;
  496. case BUS_NOTIFY_DEL_DEVICE:
  497. dev_info = dwc_pcie_find_dev_info(pdev);
  498. if (!dev_info)
  499. return NOTIFY_DONE;
  500. dwc_pcie_unregister_dev(dev_info);
  501. break;
  502. }
  503. return NOTIFY_OK;
  504. }
  505. static struct notifier_block dwc_pcie_pmu_nb = {
  506. .notifier_call = dwc_pcie_pmu_notifier,
  507. };
  508. static int dwc_pcie_pmu_probe(struct platform_device *plat_dev)
  509. {
  510. struct pci_dev *pdev = plat_dev->dev.platform_data;
  511. struct dwc_pcie_pmu *pcie_pmu;
  512. char *name;
  513. u32 sbdf, val;
  514. u16 vsec;
  515. int ret;
  516. vsec = pci_find_vsec_capability(pdev, pdev->vendor,
  517. DWC_PCIE_VSEC_RAS_DES_ID);
  518. pci_read_config_dword(pdev, vsec + PCI_VNDR_HEADER, &val);
  519. sbdf = plat_dev->id;
  520. name = devm_kasprintf(&plat_dev->dev, GFP_KERNEL, "dwc_rootport_%x", sbdf);
  521. if (!name)
  522. return -ENOMEM;
  523. pcie_pmu = devm_kzalloc(&plat_dev->dev, sizeof(*pcie_pmu), GFP_KERNEL);
  524. if (!pcie_pmu)
  525. return -ENOMEM;
  526. pcie_pmu->pdev = pdev;
  527. pcie_pmu->ras_des_offset = vsec;
  528. pcie_pmu->nr_lanes = pcie_get_width_cap(pdev);
  529. pcie_pmu->on_cpu = -1;
  530. pcie_pmu->pmu = (struct pmu){
  531. .name = name,
  532. .parent = &pdev->dev,
  533. .module = THIS_MODULE,
  534. .attr_groups = dwc_pcie_attr_groups,
  535. .capabilities = PERF_PMU_CAP_NO_EXCLUDE,
  536. .task_ctx_nr = perf_invalid_context,
  537. .event_init = dwc_pcie_pmu_event_init,
  538. .add = dwc_pcie_pmu_event_add,
  539. .del = dwc_pcie_pmu_event_del,
  540. .start = dwc_pcie_pmu_event_start,
  541. .stop = dwc_pcie_pmu_event_stop,
  542. .read = dwc_pcie_pmu_event_update,
  543. };
  544. /* Add this instance to the list used by the offline callback */
  545. ret = cpuhp_state_add_instance(dwc_pcie_pmu_hp_state,
  546. &pcie_pmu->cpuhp_node);
  547. if (ret) {
  548. pci_err(pdev, "Error %d registering hotplug @%x\n", ret, sbdf);
  549. return ret;
  550. }
  551. /* Unwind when platform driver removes */
  552. ret = devm_add_action_or_reset(&plat_dev->dev,
  553. dwc_pcie_pmu_remove_cpuhp_instance,
  554. &pcie_pmu->cpuhp_node);
  555. if (ret)
  556. return ret;
  557. ret = perf_pmu_register(&pcie_pmu->pmu, name, -1);
  558. if (ret) {
  559. pci_err(pdev, "Error %d registering PMU @%x\n", ret, sbdf);
  560. return ret;
  561. }
  562. ret = devm_add_action_or_reset(&plat_dev->dev, dwc_pcie_unregister_pmu,
  563. pcie_pmu);
  564. if (ret)
  565. return ret;
  566. return 0;
  567. }
  568. static int dwc_pcie_pmu_online_cpu(unsigned int cpu, struct hlist_node *cpuhp_node)
  569. {
  570. struct dwc_pcie_pmu *pcie_pmu;
  571. pcie_pmu = hlist_entry_safe(cpuhp_node, struct dwc_pcie_pmu, cpuhp_node);
  572. if (pcie_pmu->on_cpu == -1)
  573. pcie_pmu->on_cpu = cpumask_local_spread(
  574. 0, dev_to_node(&pcie_pmu->pdev->dev));
  575. return 0;
  576. }
  577. static int dwc_pcie_pmu_offline_cpu(unsigned int cpu, struct hlist_node *cpuhp_node)
  578. {
  579. struct dwc_pcie_pmu *pcie_pmu;
  580. struct pci_dev *pdev;
  581. unsigned int target;
  582. int node;
  583. pcie_pmu = hlist_entry_safe(cpuhp_node, struct dwc_pcie_pmu, cpuhp_node);
  584. /* Nothing to do if this CPU doesn't own the PMU */
  585. if (cpu != pcie_pmu->on_cpu)
  586. return 0;
  587. pcie_pmu->on_cpu = -1;
  588. pdev = pcie_pmu->pdev;
  589. node = dev_to_node(&pdev->dev);
  590. target = cpumask_any_and_but(cpumask_of_node(node), cpu_online_mask, cpu);
  591. if (target >= nr_cpu_ids)
  592. target = cpumask_any_but(cpu_online_mask, cpu);
  593. if (target >= nr_cpu_ids) {
  594. pci_err(pdev, "There is no CPU to set\n");
  595. return 0;
  596. }
  597. /* This PMU does NOT support interrupt, just migrate context. */
  598. perf_pmu_migrate_context(&pcie_pmu->pmu, cpu, target);
  599. pcie_pmu->on_cpu = target;
  600. return 0;
  601. }
  602. static struct platform_driver dwc_pcie_pmu_driver = {
  603. .probe = dwc_pcie_pmu_probe,
  604. .driver = {.name = "dwc_pcie_pmu",},
  605. };
  606. static void dwc_pcie_cleanup_devices(void)
  607. {
  608. struct dwc_pcie_dev_info *dev_info, *tmp;
  609. list_for_each_entry_safe(dev_info, tmp, &dwc_pcie_dev_info_head, dev_node) {
  610. dwc_pcie_unregister_dev(dev_info);
  611. }
  612. }
  613. static int __init dwc_pcie_pmu_init(void)
  614. {
  615. struct pci_dev *pdev = NULL;
  616. int ret;
  617. for_each_pci_dev(pdev) {
  618. if (!dwc_pcie_match_des_cap(pdev))
  619. continue;
  620. ret = dwc_pcie_register_dev(pdev);
  621. if (ret) {
  622. pci_dev_put(pdev);
  623. goto err_cleanup;
  624. }
  625. }
  626. ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN,
  627. "perf/dwc_pcie_pmu:online",
  628. dwc_pcie_pmu_online_cpu,
  629. dwc_pcie_pmu_offline_cpu);
  630. if (ret < 0)
  631. goto err_cleanup;
  632. dwc_pcie_pmu_hp_state = ret;
  633. ret = platform_driver_register(&dwc_pcie_pmu_driver);
  634. if (ret)
  635. goto err_remove_cpuhp;
  636. ret = bus_register_notifier(&pci_bus_type, &dwc_pcie_pmu_nb);
  637. if (ret)
  638. goto err_unregister_driver;
  639. notify = true;
  640. return 0;
  641. err_unregister_driver:
  642. platform_driver_unregister(&dwc_pcie_pmu_driver);
  643. err_remove_cpuhp:
  644. cpuhp_remove_multi_state(dwc_pcie_pmu_hp_state);
  645. err_cleanup:
  646. dwc_pcie_cleanup_devices();
  647. return ret;
  648. }
  649. static void __exit dwc_pcie_pmu_exit(void)
  650. {
  651. if (notify)
  652. bus_unregister_notifier(&pci_bus_type, &dwc_pcie_pmu_nb);
  653. dwc_pcie_cleanup_devices();
  654. platform_driver_unregister(&dwc_pcie_pmu_driver);
  655. cpuhp_remove_multi_state(dwc_pcie_pmu_hp_state);
  656. }
  657. module_init(dwc_pcie_pmu_init);
  658. module_exit(dwc_pcie_pmu_exit);
  659. MODULE_DESCRIPTION("PMU driver for DesignWare Cores PCI Express Controller");
  660. MODULE_AUTHOR("Shuai Xue <xueshuai@linux.alibaba.com>");
  661. MODULE_LICENSE("GPL v2");