hisi_ptt.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Driver for HiSilicon PCIe tune and trace device
  4. *
  5. * Copyright (c) 2022 HiSilicon Technologies Co., Ltd.
  6. * Author: Yicong Yang <[email protected]>
  7. */
  8. #include <linux/bitfield.h>
  9. #include <linux/bitops.h>
  10. #include <linux/cpuhotplug.h>
  11. #include <linux/delay.h>
  12. #include <linux/dma-mapping.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/io.h>
  15. #include <linux/iommu.h>
  16. #include <linux/iopoll.h>
  17. #include <linux/module.h>
  18. #include <linux/sysfs.h>
  19. #include <linux/vmalloc.h>
  20. #include "hisi_ptt.h"
  21. /* Dynamic CPU hotplug state used by PTT */
  22. static enum cpuhp_state hisi_ptt_pmu_online;
  23. static bool hisi_ptt_wait_tuning_finish(struct hisi_ptt *hisi_ptt)
  24. {
  25. u32 val;
  26. return !readl_poll_timeout(hisi_ptt->iobase + HISI_PTT_TUNING_INT_STAT,
  27. val, !(val & HISI_PTT_TUNING_INT_STAT_MASK),
  28. HISI_PTT_WAIT_POLL_INTERVAL_US,
  29. HISI_PTT_WAIT_TUNE_TIMEOUT_US);
  30. }
  31. static ssize_t hisi_ptt_tune_attr_show(struct device *dev,
  32. struct device_attribute *attr,
  33. char *buf)
  34. {
  35. struct hisi_ptt *hisi_ptt = to_hisi_ptt(dev_get_drvdata(dev));
  36. struct dev_ext_attribute *ext_attr;
  37. struct hisi_ptt_tune_desc *desc;
  38. u32 reg;
  39. u16 val;
  40. ext_attr = container_of(attr, struct dev_ext_attribute, attr);
  41. desc = ext_attr->var;
  42. mutex_lock(&hisi_ptt->tune_lock);
  43. reg = readl(hisi_ptt->iobase + HISI_PTT_TUNING_CTRL);
  44. reg &= ~(HISI_PTT_TUNING_CTRL_CODE | HISI_PTT_TUNING_CTRL_SUB);
  45. reg |= FIELD_PREP(HISI_PTT_TUNING_CTRL_CODE | HISI_PTT_TUNING_CTRL_SUB,
  46. desc->event_code);
  47. writel(reg, hisi_ptt->iobase + HISI_PTT_TUNING_CTRL);
  48. /* Write all 1 to indicates it's the read process */
  49. writel(~0U, hisi_ptt->iobase + HISI_PTT_TUNING_DATA);
  50. if (!hisi_ptt_wait_tuning_finish(hisi_ptt)) {
  51. mutex_unlock(&hisi_ptt->tune_lock);
  52. return -ETIMEDOUT;
  53. }
  54. reg = readl(hisi_ptt->iobase + HISI_PTT_TUNING_DATA);
  55. reg &= HISI_PTT_TUNING_DATA_VAL_MASK;
  56. val = FIELD_GET(HISI_PTT_TUNING_DATA_VAL_MASK, reg);
  57. mutex_unlock(&hisi_ptt->tune_lock);
  58. return sysfs_emit(buf, "%u\n", val);
  59. }
  60. static ssize_t hisi_ptt_tune_attr_store(struct device *dev,
  61. struct device_attribute *attr,
  62. const char *buf, size_t count)
  63. {
  64. struct hisi_ptt *hisi_ptt = to_hisi_ptt(dev_get_drvdata(dev));
  65. struct dev_ext_attribute *ext_attr;
  66. struct hisi_ptt_tune_desc *desc;
  67. u32 reg;
  68. u16 val;
  69. ext_attr = container_of(attr, struct dev_ext_attribute, attr);
  70. desc = ext_attr->var;
  71. if (kstrtou16(buf, 10, &val))
  72. return -EINVAL;
  73. mutex_lock(&hisi_ptt->tune_lock);
  74. reg = readl(hisi_ptt->iobase + HISI_PTT_TUNING_CTRL);
  75. reg &= ~(HISI_PTT_TUNING_CTRL_CODE | HISI_PTT_TUNING_CTRL_SUB);
  76. reg |= FIELD_PREP(HISI_PTT_TUNING_CTRL_CODE | HISI_PTT_TUNING_CTRL_SUB,
  77. desc->event_code);
  78. writel(reg, hisi_ptt->iobase + HISI_PTT_TUNING_CTRL);
  79. writel(FIELD_PREP(HISI_PTT_TUNING_DATA_VAL_MASK, val),
  80. hisi_ptt->iobase + HISI_PTT_TUNING_DATA);
  81. if (!hisi_ptt_wait_tuning_finish(hisi_ptt)) {
  82. mutex_unlock(&hisi_ptt->tune_lock);
  83. return -ETIMEDOUT;
  84. }
  85. mutex_unlock(&hisi_ptt->tune_lock);
  86. return count;
  87. }
  88. #define HISI_PTT_TUNE_ATTR(_name, _val, _show, _store) \
  89. static struct hisi_ptt_tune_desc _name##_desc = { \
  90. .name = #_name, \
  91. .event_code = (_val), \
  92. }; \
  93. static struct dev_ext_attribute hisi_ptt_##_name##_attr = { \
  94. .attr = __ATTR(_name, 0600, _show, _store), \
  95. .var = &_name##_desc, \
  96. }
  97. #define HISI_PTT_TUNE_ATTR_COMMON(_name, _val) \
  98. HISI_PTT_TUNE_ATTR(_name, _val, \
  99. hisi_ptt_tune_attr_show, \
  100. hisi_ptt_tune_attr_store)
  101. /*
  102. * The value of the tuning event are composed of two parts: main event code
  103. * in BIT[0,15] and subevent code in BIT[16,23]. For example, qox_tx_cpl is
  104. * a subevent of 'Tx path QoS control' which for tuning the weight of Tx
  105. * completion TLPs. See hisi_ptt.rst documentation for more information.
  106. */
  107. #define HISI_PTT_TUNE_QOS_TX_CPL (0x4 | (3 << 16))
  108. #define HISI_PTT_TUNE_QOS_TX_NP (0x4 | (4 << 16))
  109. #define HISI_PTT_TUNE_QOS_TX_P (0x4 | (5 << 16))
  110. #define HISI_PTT_TUNE_RX_ALLOC_BUF_LEVEL (0x5 | (6 << 16))
  111. #define HISI_PTT_TUNE_TX_ALLOC_BUF_LEVEL (0x5 | (7 << 16))
  112. HISI_PTT_TUNE_ATTR_COMMON(qos_tx_cpl, HISI_PTT_TUNE_QOS_TX_CPL);
  113. HISI_PTT_TUNE_ATTR_COMMON(qos_tx_np, HISI_PTT_TUNE_QOS_TX_NP);
  114. HISI_PTT_TUNE_ATTR_COMMON(qos_tx_p, HISI_PTT_TUNE_QOS_TX_P);
  115. HISI_PTT_TUNE_ATTR_COMMON(rx_alloc_buf_level, HISI_PTT_TUNE_RX_ALLOC_BUF_LEVEL);
  116. HISI_PTT_TUNE_ATTR_COMMON(tx_alloc_buf_level, HISI_PTT_TUNE_TX_ALLOC_BUF_LEVEL);
  117. static struct attribute *hisi_ptt_tune_attrs[] = {
  118. &hisi_ptt_qos_tx_cpl_attr.attr.attr,
  119. &hisi_ptt_qos_tx_np_attr.attr.attr,
  120. &hisi_ptt_qos_tx_p_attr.attr.attr,
  121. &hisi_ptt_rx_alloc_buf_level_attr.attr.attr,
  122. &hisi_ptt_tx_alloc_buf_level_attr.attr.attr,
  123. NULL,
  124. };
  125. static struct attribute_group hisi_ptt_tune_group = {
  126. .name = "tune",
  127. .attrs = hisi_ptt_tune_attrs,
  128. };
  129. static u16 hisi_ptt_get_filter_val(u16 devid, bool is_port)
  130. {
  131. if (is_port)
  132. return BIT(HISI_PCIE_CORE_PORT_ID(devid & 0xff));
  133. return devid;
  134. }
  135. static bool hisi_ptt_wait_trace_hw_idle(struct hisi_ptt *hisi_ptt)
  136. {
  137. u32 val;
  138. return !readl_poll_timeout_atomic(hisi_ptt->iobase + HISI_PTT_TRACE_STS,
  139. val, val & HISI_PTT_TRACE_IDLE,
  140. HISI_PTT_WAIT_POLL_INTERVAL_US,
  141. HISI_PTT_WAIT_TRACE_TIMEOUT_US);
  142. }
  143. static void hisi_ptt_wait_dma_reset_done(struct hisi_ptt *hisi_ptt)
  144. {
  145. u32 val;
  146. readl_poll_timeout_atomic(hisi_ptt->iobase + HISI_PTT_TRACE_WR_STS,
  147. val, !val, HISI_PTT_RESET_POLL_INTERVAL_US,
  148. HISI_PTT_RESET_TIMEOUT_US);
  149. }
  150. static void hisi_ptt_trace_end(struct hisi_ptt *hisi_ptt)
  151. {
  152. writel(0, hisi_ptt->iobase + HISI_PTT_TRACE_CTRL);
  153. hisi_ptt->trace_ctrl.started = false;
  154. }
  155. static int hisi_ptt_trace_start(struct hisi_ptt *hisi_ptt)
  156. {
  157. struct hisi_ptt_trace_ctrl *ctrl = &hisi_ptt->trace_ctrl;
  158. u32 val;
  159. int i;
  160. /* Check device idle before start trace */
  161. if (!hisi_ptt_wait_trace_hw_idle(hisi_ptt)) {
  162. pci_err(hisi_ptt->pdev, "Failed to start trace, the device is still busy\n");
  163. return -EBUSY;
  164. }
  165. ctrl->started = true;
  166. /* Reset the DMA before start tracing */
  167. val = readl(hisi_ptt->iobase + HISI_PTT_TRACE_CTRL);
  168. val |= HISI_PTT_TRACE_CTRL_RST;
  169. writel(val, hisi_ptt->iobase + HISI_PTT_TRACE_CTRL);
  170. hisi_ptt_wait_dma_reset_done(hisi_ptt);
  171. val = readl(hisi_ptt->iobase + HISI_PTT_TRACE_CTRL);
  172. val &= ~HISI_PTT_TRACE_CTRL_RST;
  173. writel(val, hisi_ptt->iobase + HISI_PTT_TRACE_CTRL);
  174. /* Reset the index of current buffer */
  175. hisi_ptt->trace_ctrl.buf_index = 0;
  176. /* Zero the trace buffers */
  177. for (i = 0; i < HISI_PTT_TRACE_BUF_CNT; i++)
  178. memset(ctrl->trace_buf[i].addr, 0, HISI_PTT_TRACE_BUF_SIZE);
  179. /* Clear the interrupt status */
  180. writel(HISI_PTT_TRACE_INT_STAT_MASK, hisi_ptt->iobase + HISI_PTT_TRACE_INT_STAT);
  181. writel(0, hisi_ptt->iobase + HISI_PTT_TRACE_INT_MASK);
  182. /* Set the trace control register */
  183. val = FIELD_PREP(HISI_PTT_TRACE_CTRL_TYPE_SEL, ctrl->type);
  184. val |= FIELD_PREP(HISI_PTT_TRACE_CTRL_RXTX_SEL, ctrl->direction);
  185. val |= FIELD_PREP(HISI_PTT_TRACE_CTRL_DATA_FORMAT, ctrl->format);
  186. val |= FIELD_PREP(HISI_PTT_TRACE_CTRL_TARGET_SEL, hisi_ptt->trace_ctrl.filter);
  187. if (!hisi_ptt->trace_ctrl.is_port)
  188. val |= HISI_PTT_TRACE_CTRL_FILTER_MODE;
  189. /* Start the Trace */
  190. val |= HISI_PTT_TRACE_CTRL_EN;
  191. writel(val, hisi_ptt->iobase + HISI_PTT_TRACE_CTRL);
  192. return 0;
  193. }
  194. static int hisi_ptt_update_aux(struct hisi_ptt *hisi_ptt, int index, bool stop)
  195. {
  196. struct hisi_ptt_trace_ctrl *ctrl = &hisi_ptt->trace_ctrl;
  197. struct perf_output_handle *handle = &ctrl->handle;
  198. struct perf_event *event = handle->event;
  199. struct hisi_ptt_pmu_buf *buf;
  200. size_t size;
  201. void *addr;
  202. buf = perf_get_aux(handle);
  203. if (!buf || !handle->size)
  204. return -EINVAL;
  205. addr = ctrl->trace_buf[ctrl->buf_index].addr;
  206. /*
  207. * If we're going to stop, read the size of already traced data from
  208. * HISI_PTT_TRACE_WR_STS. Otherwise we're coming from the interrupt,
  209. * the data size is always HISI_PTT_TRACE_BUF_SIZE.
  210. */
  211. if (stop) {
  212. u32 reg;
  213. reg = readl(hisi_ptt->iobase + HISI_PTT_TRACE_WR_STS);
  214. size = FIELD_GET(HISI_PTT_TRACE_WR_STS_WRITE, reg);
  215. } else {
  216. size = HISI_PTT_TRACE_BUF_SIZE;
  217. }
  218. memcpy(buf->base + buf->pos, addr, size);
  219. buf->pos += size;
  220. /*
  221. * Just commit the traced data if we're going to stop. Otherwise if the
  222. * resident AUX buffer cannot contain the data of next trace buffer,
  223. * apply a new one.
  224. */
  225. if (stop) {
  226. perf_aux_output_end(handle, buf->pos);
  227. } else if (buf->length - buf->pos < HISI_PTT_TRACE_BUF_SIZE) {
  228. perf_aux_output_end(handle, buf->pos);
  229. buf = perf_aux_output_begin(handle, event);
  230. if (!buf)
  231. return -EINVAL;
  232. buf->pos = handle->head % buf->length;
  233. if (buf->length - buf->pos < HISI_PTT_TRACE_BUF_SIZE) {
  234. perf_aux_output_end(handle, 0);
  235. return -EINVAL;
  236. }
  237. }
  238. return 0;
  239. }
  240. static irqreturn_t hisi_ptt_isr(int irq, void *context)
  241. {
  242. struct hisi_ptt *hisi_ptt = context;
  243. u32 status, buf_idx;
  244. status = readl(hisi_ptt->iobase + HISI_PTT_TRACE_INT_STAT);
  245. if (!(status & HISI_PTT_TRACE_INT_STAT_MASK))
  246. return IRQ_NONE;
  247. buf_idx = ffs(status) - 1;
  248. /* Clear the interrupt status of buffer @buf_idx */
  249. writel(status, hisi_ptt->iobase + HISI_PTT_TRACE_INT_STAT);
  250. /*
  251. * Update the AUX buffer and cache the current buffer index,
  252. * as we need to know this and save the data when the trace
  253. * is ended out of the interrupt handler. End the trace
  254. * if the updating fails.
  255. */
  256. if (hisi_ptt_update_aux(hisi_ptt, buf_idx, false))
  257. hisi_ptt_trace_end(hisi_ptt);
  258. else
  259. hisi_ptt->trace_ctrl.buf_index = (buf_idx + 1) % HISI_PTT_TRACE_BUF_CNT;
  260. return IRQ_HANDLED;
  261. }
  262. static void hisi_ptt_irq_free_vectors(void *pdev)
  263. {
  264. pci_free_irq_vectors(pdev);
  265. }
  266. static int hisi_ptt_register_irq(struct hisi_ptt *hisi_ptt)
  267. {
  268. struct pci_dev *pdev = hisi_ptt->pdev;
  269. int ret;
  270. ret = pci_alloc_irq_vectors(pdev, 1, 1, PCI_IRQ_MSI);
  271. if (ret < 0) {
  272. pci_err(pdev, "failed to allocate irq vector, ret = %d\n", ret);
  273. return ret;
  274. }
  275. ret = devm_add_action_or_reset(&pdev->dev, hisi_ptt_irq_free_vectors, pdev);
  276. if (ret < 0)
  277. return ret;
  278. hisi_ptt->trace_irq = pci_irq_vector(pdev, HISI_PTT_TRACE_DMA_IRQ);
  279. ret = devm_request_threaded_irq(&pdev->dev, hisi_ptt->trace_irq,
  280. NULL, hisi_ptt_isr, 0,
  281. DRV_NAME, hisi_ptt);
  282. if (ret) {
  283. pci_err(pdev, "failed to request irq %d, ret = %d\n",
  284. hisi_ptt->trace_irq, ret);
  285. return ret;
  286. }
  287. return 0;
  288. }
  289. static int hisi_ptt_init_filters(struct pci_dev *pdev, void *data)
  290. {
  291. struct pci_dev *root_port = pcie_find_root_port(pdev);
  292. struct hisi_ptt_filter_desc *filter;
  293. struct hisi_ptt *hisi_ptt = data;
  294. u32 port_devid;
  295. if (!root_port)
  296. return 0;
  297. port_devid = PCI_DEVID(root_port->bus->number, root_port->devfn);
  298. if (port_devid < hisi_ptt->lower_bdf ||
  299. port_devid > hisi_ptt->upper_bdf)
  300. return 0;
  301. /*
  302. * We won't fail the probe if filter allocation failed here. The filters
  303. * should be partial initialized and users would know which filter fails
  304. * through the log. Other functions of PTT device are still available.
  305. */
  306. filter = kzalloc(sizeof(*filter), GFP_KERNEL);
  307. if (!filter) {
  308. pci_err(hisi_ptt->pdev, "failed to add filter %s\n", pci_name(pdev));
  309. return -ENOMEM;
  310. }
  311. filter->devid = PCI_DEVID(pdev->bus->number, pdev->devfn);
  312. if (pci_pcie_type(pdev) == PCI_EXP_TYPE_ROOT_PORT) {
  313. filter->is_port = true;
  314. list_add_tail(&filter->list, &hisi_ptt->port_filters);
  315. /* Update the available port mask */
  316. hisi_ptt->port_mask |= hisi_ptt_get_filter_val(filter->devid, true);
  317. } else {
  318. list_add_tail(&filter->list, &hisi_ptt->req_filters);
  319. }
  320. return 0;
  321. }
  322. static void hisi_ptt_release_filters(void *data)
  323. {
  324. struct hisi_ptt_filter_desc *filter, *tmp;
  325. struct hisi_ptt *hisi_ptt = data;
  326. list_for_each_entry_safe(filter, tmp, &hisi_ptt->req_filters, list) {
  327. list_del(&filter->list);
  328. kfree(filter);
  329. }
  330. list_for_each_entry_safe(filter, tmp, &hisi_ptt->port_filters, list) {
  331. list_del(&filter->list);
  332. kfree(filter);
  333. }
  334. }
  335. static int hisi_ptt_config_trace_buf(struct hisi_ptt *hisi_ptt)
  336. {
  337. struct hisi_ptt_trace_ctrl *ctrl = &hisi_ptt->trace_ctrl;
  338. struct device *dev = &hisi_ptt->pdev->dev;
  339. int i;
  340. ctrl->trace_buf = devm_kcalloc(dev, HISI_PTT_TRACE_BUF_CNT,
  341. sizeof(*ctrl->trace_buf), GFP_KERNEL);
  342. if (!ctrl->trace_buf)
  343. return -ENOMEM;
  344. for (i = 0; i < HISI_PTT_TRACE_BUF_CNT; ++i) {
  345. ctrl->trace_buf[i].addr = dmam_alloc_coherent(dev, HISI_PTT_TRACE_BUF_SIZE,
  346. &ctrl->trace_buf[i].dma,
  347. GFP_KERNEL);
  348. if (!ctrl->trace_buf[i].addr)
  349. return -ENOMEM;
  350. }
  351. /* Configure the trace DMA buffer */
  352. for (i = 0; i < HISI_PTT_TRACE_BUF_CNT; i++) {
  353. writel(lower_32_bits(ctrl->trace_buf[i].dma),
  354. hisi_ptt->iobase + HISI_PTT_TRACE_ADDR_BASE_LO_0 +
  355. i * HISI_PTT_TRACE_ADDR_STRIDE);
  356. writel(upper_32_bits(ctrl->trace_buf[i].dma),
  357. hisi_ptt->iobase + HISI_PTT_TRACE_ADDR_BASE_HI_0 +
  358. i * HISI_PTT_TRACE_ADDR_STRIDE);
  359. }
  360. writel(HISI_PTT_TRACE_BUF_SIZE, hisi_ptt->iobase + HISI_PTT_TRACE_ADDR_SIZE);
  361. return 0;
  362. }
  363. static int hisi_ptt_init_ctrls(struct hisi_ptt *hisi_ptt)
  364. {
  365. struct pci_dev *pdev = hisi_ptt->pdev;
  366. struct pci_bus *bus;
  367. int ret;
  368. u32 reg;
  369. INIT_LIST_HEAD(&hisi_ptt->port_filters);
  370. INIT_LIST_HEAD(&hisi_ptt->req_filters);
  371. ret = hisi_ptt_config_trace_buf(hisi_ptt);
  372. if (ret)
  373. return ret;
  374. /*
  375. * The device range register provides the information about the root
  376. * ports which the RCiEP can control and trace. The RCiEP and the root
  377. * ports which it supports are on the same PCIe core, with same domain
  378. * number but maybe different bus number. The device range register
  379. * will tell us which root ports we can support, Bit[31:16] indicates
  380. * the upper BDF numbers of the root port, while Bit[15:0] indicates
  381. * the lower.
  382. */
  383. reg = readl(hisi_ptt->iobase + HISI_PTT_DEVICE_RANGE);
  384. hisi_ptt->upper_bdf = FIELD_GET(HISI_PTT_DEVICE_RANGE_UPPER, reg);
  385. hisi_ptt->lower_bdf = FIELD_GET(HISI_PTT_DEVICE_RANGE_LOWER, reg);
  386. bus = pci_find_bus(pci_domain_nr(pdev->bus), PCI_BUS_NUM(hisi_ptt->upper_bdf));
  387. if (bus)
  388. pci_walk_bus(bus, hisi_ptt_init_filters, hisi_ptt);
  389. ret = devm_add_action_or_reset(&pdev->dev, hisi_ptt_release_filters, hisi_ptt);
  390. if (ret)
  391. return ret;
  392. hisi_ptt->trace_ctrl.on_cpu = -1;
  393. return 0;
  394. }
  395. static ssize_t cpumask_show(struct device *dev, struct device_attribute *attr,
  396. char *buf)
  397. {
  398. struct hisi_ptt *hisi_ptt = to_hisi_ptt(dev_get_drvdata(dev));
  399. const cpumask_t *cpumask = cpumask_of_node(dev_to_node(&hisi_ptt->pdev->dev));
  400. return cpumap_print_to_pagebuf(true, buf, cpumask);
  401. }
  402. static DEVICE_ATTR_RO(cpumask);
  403. static struct attribute *hisi_ptt_cpumask_attrs[] = {
  404. &dev_attr_cpumask.attr,
  405. NULL
  406. };
  407. static const struct attribute_group hisi_ptt_cpumask_attr_group = {
  408. .attrs = hisi_ptt_cpumask_attrs,
  409. };
  410. /*
  411. * Bit 19 indicates the filter type, 1 for Root Port filter and 0 for Requester
  412. * filter. Bit[15:0] indicates the filter value, for Root Port filter it's
  413. * a bit mask of desired ports and for Requester filter it's the Requester ID
  414. * of the desired PCIe function. Bit[18:16] is reserved for extension.
  415. *
  416. * See hisi_ptt.rst documentation for detailed information.
  417. */
  418. PMU_FORMAT_ATTR(filter, "config:0-19");
  419. PMU_FORMAT_ATTR(direction, "config:20-23");
  420. PMU_FORMAT_ATTR(type, "config:24-31");
  421. PMU_FORMAT_ATTR(format, "config:32-35");
  422. static struct attribute *hisi_ptt_pmu_format_attrs[] = {
  423. &format_attr_filter.attr,
  424. &format_attr_direction.attr,
  425. &format_attr_type.attr,
  426. &format_attr_format.attr,
  427. NULL
  428. };
  429. static struct attribute_group hisi_ptt_pmu_format_group = {
  430. .name = "format",
  431. .attrs = hisi_ptt_pmu_format_attrs,
  432. };
  433. static const struct attribute_group *hisi_ptt_pmu_groups[] = {
  434. &hisi_ptt_cpumask_attr_group,
  435. &hisi_ptt_pmu_format_group,
  436. &hisi_ptt_tune_group,
  437. NULL
  438. };
  439. static int hisi_ptt_trace_valid_direction(u32 val)
  440. {
  441. /*
  442. * The direction values have different effects according to the data
  443. * format (specified in the parentheses). TLP set A/B means different
  444. * set of TLP types. See hisi_ptt.rst documentation for more details.
  445. */
  446. static const u32 hisi_ptt_trace_available_direction[] = {
  447. 0, /* inbound(4DW) or reserved(8DW) */
  448. 1, /* outbound(4DW) */
  449. 2, /* {in, out}bound(4DW) or inbound(8DW), TLP set A */
  450. 3, /* {in, out}bound(4DW) or inbound(8DW), TLP set B */
  451. };
  452. int i;
  453. for (i = 0; i < ARRAY_SIZE(hisi_ptt_trace_available_direction); i++) {
  454. if (val == hisi_ptt_trace_available_direction[i])
  455. return 0;
  456. }
  457. return -EINVAL;
  458. }
  459. static int hisi_ptt_trace_valid_type(u32 val)
  460. {
  461. /* Different types can be set simultaneously */
  462. static const u32 hisi_ptt_trace_available_type[] = {
  463. 1, /* posted_request */
  464. 2, /* non-posted_request */
  465. 4, /* completion */
  466. };
  467. int i;
  468. if (!val)
  469. return -EINVAL;
  470. /*
  471. * Walk the available list and clear the valid bits of
  472. * the config. If there is any resident bit after the
  473. * walk then the config is invalid.
  474. */
  475. for (i = 0; i < ARRAY_SIZE(hisi_ptt_trace_available_type); i++)
  476. val &= ~hisi_ptt_trace_available_type[i];
  477. if (val)
  478. return -EINVAL;
  479. return 0;
  480. }
  481. static int hisi_ptt_trace_valid_format(u32 val)
  482. {
  483. static const u32 hisi_ptt_trace_availble_format[] = {
  484. 0, /* 4DW */
  485. 1, /* 8DW */
  486. };
  487. int i;
  488. for (i = 0; i < ARRAY_SIZE(hisi_ptt_trace_availble_format); i++) {
  489. if (val == hisi_ptt_trace_availble_format[i])
  490. return 0;
  491. }
  492. return -EINVAL;
  493. }
  494. static int hisi_ptt_trace_valid_filter(struct hisi_ptt *hisi_ptt, u64 config)
  495. {
  496. unsigned long val, port_mask = hisi_ptt->port_mask;
  497. struct hisi_ptt_filter_desc *filter;
  498. hisi_ptt->trace_ctrl.is_port = FIELD_GET(HISI_PTT_PMU_FILTER_IS_PORT, config);
  499. val = FIELD_GET(HISI_PTT_PMU_FILTER_VAL_MASK, config);
  500. /*
  501. * Port filters are defined as bit mask. For port filters, check
  502. * the bits in the @val are within the range of hisi_ptt->port_mask
  503. * and whether it's empty or not, otherwise user has specified
  504. * some unsupported root ports.
  505. *
  506. * For Requester ID filters, walk the available filter list to see
  507. * whether we have one matched.
  508. */
  509. if (!hisi_ptt->trace_ctrl.is_port) {
  510. list_for_each_entry(filter, &hisi_ptt->req_filters, list) {
  511. if (val == hisi_ptt_get_filter_val(filter->devid, filter->is_port))
  512. return 0;
  513. }
  514. } else if (bitmap_subset(&val, &port_mask, BITS_PER_LONG)) {
  515. return 0;
  516. }
  517. return -EINVAL;
  518. }
  519. static void hisi_ptt_pmu_init_configs(struct hisi_ptt *hisi_ptt, struct perf_event *event)
  520. {
  521. struct hisi_ptt_trace_ctrl *ctrl = &hisi_ptt->trace_ctrl;
  522. u32 val;
  523. val = FIELD_GET(HISI_PTT_PMU_FILTER_VAL_MASK, event->attr.config);
  524. hisi_ptt->trace_ctrl.filter = val;
  525. val = FIELD_GET(HISI_PTT_PMU_DIRECTION_MASK, event->attr.config);
  526. ctrl->direction = val;
  527. val = FIELD_GET(HISI_PTT_PMU_TYPE_MASK, event->attr.config);
  528. ctrl->type = val;
  529. val = FIELD_GET(HISI_PTT_PMU_FORMAT_MASK, event->attr.config);
  530. ctrl->format = val;
  531. }
  532. static int hisi_ptt_pmu_event_init(struct perf_event *event)
  533. {
  534. struct hisi_ptt *hisi_ptt = to_hisi_ptt(event->pmu);
  535. int ret;
  536. u32 val;
  537. if (event->cpu < 0) {
  538. dev_dbg(event->pmu->dev, "Per-task mode not supported\n");
  539. return -EOPNOTSUPP;
  540. }
  541. if (event->attr.type != hisi_ptt->hisi_ptt_pmu.type)
  542. return -ENOENT;
  543. ret = hisi_ptt_trace_valid_filter(hisi_ptt, event->attr.config);
  544. if (ret < 0)
  545. return ret;
  546. val = FIELD_GET(HISI_PTT_PMU_DIRECTION_MASK, event->attr.config);
  547. ret = hisi_ptt_trace_valid_direction(val);
  548. if (ret < 0)
  549. return ret;
  550. val = FIELD_GET(HISI_PTT_PMU_TYPE_MASK, event->attr.config);
  551. ret = hisi_ptt_trace_valid_type(val);
  552. if (ret < 0)
  553. return ret;
  554. val = FIELD_GET(HISI_PTT_PMU_FORMAT_MASK, event->attr.config);
  555. return hisi_ptt_trace_valid_format(val);
  556. }
  557. static void *hisi_ptt_pmu_setup_aux(struct perf_event *event, void **pages,
  558. int nr_pages, bool overwrite)
  559. {
  560. struct hisi_ptt_pmu_buf *buf;
  561. struct page **pagelist;
  562. int i;
  563. if (overwrite) {
  564. dev_warn(event->pmu->dev, "Overwrite mode is not supported\n");
  565. return NULL;
  566. }
  567. /* If the pages size less than buffers, we cannot start trace */
  568. if (nr_pages < HISI_PTT_TRACE_TOTAL_BUF_SIZE / PAGE_SIZE)
  569. return NULL;
  570. buf = kzalloc(sizeof(*buf), GFP_KERNEL);
  571. if (!buf)
  572. return NULL;
  573. pagelist = kcalloc(nr_pages, sizeof(*pagelist), GFP_KERNEL);
  574. if (!pagelist)
  575. goto err;
  576. for (i = 0; i < nr_pages; i++)
  577. pagelist[i] = virt_to_page(pages[i]);
  578. buf->base = vmap(pagelist, nr_pages, VM_MAP, PAGE_KERNEL);
  579. if (!buf->base) {
  580. kfree(pagelist);
  581. goto err;
  582. }
  583. buf->nr_pages = nr_pages;
  584. buf->length = nr_pages * PAGE_SIZE;
  585. buf->pos = 0;
  586. kfree(pagelist);
  587. return buf;
  588. err:
  589. kfree(buf);
  590. return NULL;
  591. }
  592. static void hisi_ptt_pmu_free_aux(void *aux)
  593. {
  594. struct hisi_ptt_pmu_buf *buf = aux;
  595. vunmap(buf->base);
  596. kfree(buf);
  597. }
  598. static void hisi_ptt_pmu_start(struct perf_event *event, int flags)
  599. {
  600. struct hisi_ptt *hisi_ptt = to_hisi_ptt(event->pmu);
  601. struct perf_output_handle *handle = &hisi_ptt->trace_ctrl.handle;
  602. struct hw_perf_event *hwc = &event->hw;
  603. struct device *dev = event->pmu->dev;
  604. struct hisi_ptt_pmu_buf *buf;
  605. int cpu = event->cpu;
  606. int ret;
  607. hwc->state = 0;
  608. /* Serialize the perf process if user specified several CPUs */
  609. spin_lock(&hisi_ptt->pmu_lock);
  610. if (hisi_ptt->trace_ctrl.started) {
  611. dev_dbg(dev, "trace has already started\n");
  612. goto stop;
  613. }
  614. /*
  615. * Handle the interrupt on the same cpu which starts the trace to avoid
  616. * context mismatch. Otherwise we'll trigger the WARN from the perf
  617. * core in event_function_local(). If CPU passed is offline we'll fail
  618. * here, just log it since we can do nothing here.
  619. */
  620. ret = irq_set_affinity(hisi_ptt->trace_irq, cpumask_of(cpu));
  621. if (ret)
  622. dev_warn(dev, "failed to set the affinity of trace interrupt\n");
  623. hisi_ptt->trace_ctrl.on_cpu = cpu;
  624. buf = perf_aux_output_begin(handle, event);
  625. if (!buf) {
  626. dev_dbg(dev, "aux output begin failed\n");
  627. goto stop;
  628. }
  629. buf->pos = handle->head % buf->length;
  630. hisi_ptt_pmu_init_configs(hisi_ptt, event);
  631. ret = hisi_ptt_trace_start(hisi_ptt);
  632. if (ret) {
  633. dev_dbg(dev, "trace start failed, ret = %d\n", ret);
  634. perf_aux_output_end(handle, 0);
  635. goto stop;
  636. }
  637. spin_unlock(&hisi_ptt->pmu_lock);
  638. return;
  639. stop:
  640. event->hw.state |= PERF_HES_STOPPED;
  641. spin_unlock(&hisi_ptt->pmu_lock);
  642. }
  643. static void hisi_ptt_pmu_stop(struct perf_event *event, int flags)
  644. {
  645. struct hisi_ptt *hisi_ptt = to_hisi_ptt(event->pmu);
  646. struct hw_perf_event *hwc = &event->hw;
  647. if (hwc->state & PERF_HES_STOPPED)
  648. return;
  649. spin_lock(&hisi_ptt->pmu_lock);
  650. if (hisi_ptt->trace_ctrl.started) {
  651. hisi_ptt_trace_end(hisi_ptt);
  652. if (!hisi_ptt_wait_trace_hw_idle(hisi_ptt))
  653. dev_warn(event->pmu->dev, "Device is still busy\n");
  654. hisi_ptt_update_aux(hisi_ptt, hisi_ptt->trace_ctrl.buf_index, true);
  655. }
  656. spin_unlock(&hisi_ptt->pmu_lock);
  657. hwc->state |= PERF_HES_STOPPED;
  658. perf_event_update_userpage(event);
  659. hwc->state |= PERF_HES_UPTODATE;
  660. }
  661. static int hisi_ptt_pmu_add(struct perf_event *event, int flags)
  662. {
  663. struct hisi_ptt *hisi_ptt = to_hisi_ptt(event->pmu);
  664. struct hw_perf_event *hwc = &event->hw;
  665. int cpu = event->cpu;
  666. /* Only allow the cpus on the device's node to add the event */
  667. if (!cpumask_test_cpu(cpu, cpumask_of_node(dev_to_node(&hisi_ptt->pdev->dev))))
  668. return 0;
  669. hwc->state = PERF_HES_STOPPED | PERF_HES_UPTODATE;
  670. if (flags & PERF_EF_START) {
  671. hisi_ptt_pmu_start(event, PERF_EF_RELOAD);
  672. if (hwc->state & PERF_HES_STOPPED)
  673. return -EINVAL;
  674. }
  675. return 0;
  676. }
  677. static void hisi_ptt_pmu_del(struct perf_event *event, int flags)
  678. {
  679. hisi_ptt_pmu_stop(event, PERF_EF_UPDATE);
  680. }
  681. static void hisi_ptt_pmu_read(struct perf_event *event)
  682. {
  683. }
  684. static void hisi_ptt_remove_cpuhp_instance(void *hotplug_node)
  685. {
  686. cpuhp_state_remove_instance_nocalls(hisi_ptt_pmu_online, hotplug_node);
  687. }
  688. static void hisi_ptt_unregister_pmu(void *pmu)
  689. {
  690. perf_pmu_unregister(pmu);
  691. }
  692. static int hisi_ptt_register_pmu(struct hisi_ptt *hisi_ptt)
  693. {
  694. u16 core_id, sicl_id;
  695. char *pmu_name;
  696. u32 reg;
  697. int ret;
  698. ret = cpuhp_state_add_instance_nocalls(hisi_ptt_pmu_online,
  699. &hisi_ptt->hotplug_node);
  700. if (ret)
  701. return ret;
  702. ret = devm_add_action_or_reset(&hisi_ptt->pdev->dev,
  703. hisi_ptt_remove_cpuhp_instance,
  704. &hisi_ptt->hotplug_node);
  705. if (ret)
  706. return ret;
  707. mutex_init(&hisi_ptt->tune_lock);
  708. spin_lock_init(&hisi_ptt->pmu_lock);
  709. hisi_ptt->hisi_ptt_pmu = (struct pmu) {
  710. .module = THIS_MODULE,
  711. .capabilities = PERF_PMU_CAP_EXCLUSIVE | PERF_PMU_CAP_ITRACE,
  712. .task_ctx_nr = perf_sw_context,
  713. .attr_groups = hisi_ptt_pmu_groups,
  714. .event_init = hisi_ptt_pmu_event_init,
  715. .setup_aux = hisi_ptt_pmu_setup_aux,
  716. .free_aux = hisi_ptt_pmu_free_aux,
  717. .start = hisi_ptt_pmu_start,
  718. .stop = hisi_ptt_pmu_stop,
  719. .add = hisi_ptt_pmu_add,
  720. .del = hisi_ptt_pmu_del,
  721. .read = hisi_ptt_pmu_read,
  722. };
  723. reg = readl(hisi_ptt->iobase + HISI_PTT_LOCATION);
  724. core_id = FIELD_GET(HISI_PTT_CORE_ID, reg);
  725. sicl_id = FIELD_GET(HISI_PTT_SICL_ID, reg);
  726. pmu_name = devm_kasprintf(&hisi_ptt->pdev->dev, GFP_KERNEL, "hisi_ptt%u_%u",
  727. sicl_id, core_id);
  728. if (!pmu_name)
  729. return -ENOMEM;
  730. ret = perf_pmu_register(&hisi_ptt->hisi_ptt_pmu, pmu_name, -1);
  731. if (ret)
  732. return ret;
  733. return devm_add_action_or_reset(&hisi_ptt->pdev->dev,
  734. hisi_ptt_unregister_pmu,
  735. &hisi_ptt->hisi_ptt_pmu);
  736. }
  737. /*
  738. * The DMA of PTT trace can only use direct mappings due to some
  739. * hardware restriction. Check whether there is no IOMMU or the
  740. * policy of the IOMMU domain is passthrough, otherwise the trace
  741. * cannot work.
  742. *
  743. * The PTT device is supposed to behind an ARM SMMUv3, which
  744. * should have passthrough the device by a quirk.
  745. */
  746. static int hisi_ptt_check_iommu_mapping(struct pci_dev *pdev)
  747. {
  748. struct iommu_domain *iommu_domain;
  749. iommu_domain = iommu_get_domain_for_dev(&pdev->dev);
  750. if (!iommu_domain || iommu_domain->type == IOMMU_DOMAIN_IDENTITY)
  751. return 0;
  752. return -EOPNOTSUPP;
  753. }
  754. static int hisi_ptt_probe(struct pci_dev *pdev,
  755. const struct pci_device_id *id)
  756. {
  757. struct hisi_ptt *hisi_ptt;
  758. int ret;
  759. ret = hisi_ptt_check_iommu_mapping(pdev);
  760. if (ret) {
  761. pci_err(pdev, "requires direct DMA mappings\n");
  762. return ret;
  763. }
  764. hisi_ptt = devm_kzalloc(&pdev->dev, sizeof(*hisi_ptt), GFP_KERNEL);
  765. if (!hisi_ptt)
  766. return -ENOMEM;
  767. hisi_ptt->pdev = pdev;
  768. pci_set_drvdata(pdev, hisi_ptt);
  769. ret = pcim_enable_device(pdev);
  770. if (ret) {
  771. pci_err(pdev, "failed to enable device, ret = %d\n", ret);
  772. return ret;
  773. }
  774. ret = pcim_iomap_regions(pdev, BIT(2), DRV_NAME);
  775. if (ret) {
  776. pci_err(pdev, "failed to remap io memory, ret = %d\n", ret);
  777. return ret;
  778. }
  779. hisi_ptt->iobase = pcim_iomap_table(pdev)[2];
  780. ret = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(64));
  781. if (ret) {
  782. pci_err(pdev, "failed to set 64 bit dma mask, ret = %d\n", ret);
  783. return ret;
  784. }
  785. pci_set_master(pdev);
  786. ret = hisi_ptt_register_irq(hisi_ptt);
  787. if (ret)
  788. return ret;
  789. ret = hisi_ptt_init_ctrls(hisi_ptt);
  790. if (ret) {
  791. pci_err(pdev, "failed to init controls, ret = %d\n", ret);
  792. return ret;
  793. }
  794. ret = hisi_ptt_register_pmu(hisi_ptt);
  795. if (ret) {
  796. pci_err(pdev, "failed to register PMU device, ret = %d", ret);
  797. return ret;
  798. }
  799. return 0;
  800. }
  801. static const struct pci_device_id hisi_ptt_id_tbl[] = {
  802. { PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, 0xa12e) },
  803. { }
  804. };
  805. MODULE_DEVICE_TABLE(pci, hisi_ptt_id_tbl);
  806. static struct pci_driver hisi_ptt_driver = {
  807. .name = DRV_NAME,
  808. .id_table = hisi_ptt_id_tbl,
  809. .probe = hisi_ptt_probe,
  810. };
  811. static int hisi_ptt_cpu_teardown(unsigned int cpu, struct hlist_node *node)
  812. {
  813. struct hisi_ptt *hisi_ptt;
  814. struct device *dev;
  815. int target, src;
  816. hisi_ptt = hlist_entry_safe(node, struct hisi_ptt, hotplug_node);
  817. src = hisi_ptt->trace_ctrl.on_cpu;
  818. dev = hisi_ptt->hisi_ptt_pmu.dev;
  819. if (!hisi_ptt->trace_ctrl.started || src != cpu)
  820. return 0;
  821. target = cpumask_any_but(cpumask_of_node(dev_to_node(&hisi_ptt->pdev->dev)), cpu);
  822. if (target >= nr_cpu_ids) {
  823. dev_err(dev, "no available cpu for perf context migration\n");
  824. return 0;
  825. }
  826. perf_pmu_migrate_context(&hisi_ptt->hisi_ptt_pmu, src, target);
  827. /*
  828. * Also make sure the interrupt bind to the migrated CPU as well. Warn
  829. * the user on failure here.
  830. */
  831. if (irq_set_affinity(hisi_ptt->trace_irq, cpumask_of(target)))
  832. dev_warn(dev, "failed to set the affinity of trace interrupt\n");
  833. hisi_ptt->trace_ctrl.on_cpu = target;
  834. return 0;
  835. }
  836. static int __init hisi_ptt_init(void)
  837. {
  838. int ret;
  839. ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, DRV_NAME, NULL,
  840. hisi_ptt_cpu_teardown);
  841. if (ret < 0)
  842. return ret;
  843. hisi_ptt_pmu_online = ret;
  844. ret = pci_register_driver(&hisi_ptt_driver);
  845. if (ret)
  846. cpuhp_remove_multi_state(hisi_ptt_pmu_online);
  847. return ret;
  848. }
  849. module_init(hisi_ptt_init);
  850. static void __exit hisi_ptt_exit(void)
  851. {
  852. pci_unregister_driver(&hisi_ptt_driver);
  853. cpuhp_remove_multi_state(hisi_ptt_pmu_online);
  854. }
  855. module_exit(hisi_ptt_exit);
  856. MODULE_LICENSE("GPL");
  857. MODULE_AUTHOR("Yicong Yang <[email protected]>");
  858. MODULE_DESCRIPTION("Driver for HiSilicon PCIe tune and trace device");