irq.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright(c) 2019 Intel Corporation. All rights rsvd. */
  3. #include <linux/init.h>
  4. #include <linux/kernel.h>
  5. #include <linux/module.h>
  6. #include <linux/pci.h>
  7. #include <linux/io-64-nonatomic-lo-hi.h>
  8. #include <linux/dmaengine.h>
  9. #include <linux/delay.h>
  10. #include <uapi/linux/idxd.h>
  11. #include "../dmaengine.h"
  12. #include "idxd.h"
  13. #include "registers.h"
  14. enum irq_work_type {
  15. IRQ_WORK_NORMAL = 0,
  16. IRQ_WORK_PROCESS_FAULT,
  17. };
  18. struct idxd_resubmit {
  19. struct work_struct work;
  20. struct idxd_desc *desc;
  21. };
  22. struct idxd_int_handle_revoke {
  23. struct work_struct work;
  24. struct idxd_device *idxd;
  25. };
  26. static void idxd_device_reinit(struct work_struct *work)
  27. {
  28. struct idxd_device *idxd = container_of(work, struct idxd_device, work);
  29. struct device *dev = &idxd->pdev->dev;
  30. int rc, i;
  31. idxd_device_reset(idxd);
  32. rc = idxd_device_config(idxd);
  33. if (rc < 0)
  34. goto out;
  35. rc = idxd_device_enable(idxd);
  36. if (rc < 0)
  37. goto out;
  38. for (i = 0; i < idxd->max_wqs; i++) {
  39. if (test_bit(i, idxd->wq_enable_map)) {
  40. struct idxd_wq *wq = idxd->wqs[i];
  41. rc = idxd_wq_enable(wq);
  42. if (rc < 0) {
  43. clear_bit(i, idxd->wq_enable_map);
  44. dev_warn(dev, "Unable to re-enable wq %s\n",
  45. dev_name(wq_confdev(wq)));
  46. }
  47. }
  48. }
  49. return;
  50. out:
  51. idxd_device_clear_state(idxd);
  52. }
  53. /*
  54. * The function sends a drain descriptor for the interrupt handle. The drain ensures
  55. * all descriptors with this interrupt handle is flushed and the interrupt
  56. * will allow the cleanup of the outstanding descriptors.
  57. */
  58. static void idxd_int_handle_revoke_drain(struct idxd_irq_entry *ie)
  59. {
  60. struct idxd_wq *wq = ie_to_wq(ie);
  61. struct idxd_device *idxd = wq->idxd;
  62. struct device *dev = &idxd->pdev->dev;
  63. struct dsa_hw_desc desc = {};
  64. void __iomem *portal;
  65. int rc;
  66. /* Issue a simple drain operation with interrupt but no completion record */
  67. desc.flags = IDXD_OP_FLAG_RCI;
  68. desc.opcode = DSA_OPCODE_DRAIN;
  69. desc.priv = 1;
  70. if (ie->pasid != INVALID_IOASID)
  71. desc.pasid = ie->pasid;
  72. desc.int_handle = ie->int_handle;
  73. portal = idxd_wq_portal_addr(wq);
  74. /*
  75. * The wmb() makes sure that the descriptor is all there before we
  76. * issue.
  77. */
  78. wmb();
  79. if (wq_dedicated(wq)) {
  80. iosubmit_cmds512(portal, &desc, 1);
  81. } else {
  82. rc = idxd_enqcmds(wq, portal, &desc);
  83. /* This should not fail unless hardware failed. */
  84. if (rc < 0)
  85. dev_warn(dev, "Failed to submit drain desc on wq %d\n", wq->id);
  86. }
  87. }
  88. static void idxd_abort_invalid_int_handle_descs(struct idxd_irq_entry *ie)
  89. {
  90. LIST_HEAD(flist);
  91. struct idxd_desc *d, *t;
  92. struct llist_node *head;
  93. spin_lock(&ie->list_lock);
  94. head = llist_del_all(&ie->pending_llist);
  95. if (head) {
  96. llist_for_each_entry_safe(d, t, head, llnode)
  97. list_add_tail(&d->list, &ie->work_list);
  98. }
  99. list_for_each_entry_safe(d, t, &ie->work_list, list) {
  100. if (d->completion->status == DSA_COMP_INT_HANDLE_INVAL)
  101. list_move_tail(&d->list, &flist);
  102. }
  103. spin_unlock(&ie->list_lock);
  104. list_for_each_entry_safe(d, t, &flist, list) {
  105. list_del(&d->list);
  106. idxd_dma_complete_txd(d, IDXD_COMPLETE_ABORT, true);
  107. }
  108. }
  109. static void idxd_int_handle_revoke(struct work_struct *work)
  110. {
  111. struct idxd_int_handle_revoke *revoke =
  112. container_of(work, struct idxd_int_handle_revoke, work);
  113. struct idxd_device *idxd = revoke->idxd;
  114. struct pci_dev *pdev = idxd->pdev;
  115. struct device *dev = &pdev->dev;
  116. int i, new_handle, rc;
  117. if (!idxd->request_int_handles) {
  118. kfree(revoke);
  119. dev_warn(dev, "Unexpected int handle refresh interrupt.\n");
  120. return;
  121. }
  122. /*
  123. * The loop attempts to acquire new interrupt handle for all interrupt
  124. * vectors that supports a handle. If a new interrupt handle is acquired and the
  125. * wq is kernel type, the driver will kill the percpu_ref to pause all
  126. * ongoing descriptor submissions. The interrupt handle is then changed.
  127. * After change, the percpu_ref is revived and all the pending submissions
  128. * are woken to try again. A drain is sent to for the interrupt handle
  129. * at the end to make sure all invalid int handle descriptors are processed.
  130. */
  131. for (i = 1; i < idxd->irq_cnt; i++) {
  132. struct idxd_irq_entry *ie = idxd_get_ie(idxd, i);
  133. struct idxd_wq *wq = ie_to_wq(ie);
  134. if (ie->int_handle == INVALID_INT_HANDLE)
  135. continue;
  136. rc = idxd_device_request_int_handle(idxd, i, &new_handle, IDXD_IRQ_MSIX);
  137. if (rc < 0) {
  138. dev_warn(dev, "get int handle %d failed: %d\n", i, rc);
  139. /*
  140. * Failed to acquire new interrupt handle. Kill the WQ
  141. * and release all the pending submitters. The submitters will
  142. * get error return code and handle appropriately.
  143. */
  144. ie->int_handle = INVALID_INT_HANDLE;
  145. idxd_wq_quiesce(wq);
  146. idxd_abort_invalid_int_handle_descs(ie);
  147. continue;
  148. }
  149. /* No change in interrupt handle, nothing needs to be done */
  150. if (ie->int_handle == new_handle)
  151. continue;
  152. if (wq->state != IDXD_WQ_ENABLED || wq->type != IDXD_WQT_KERNEL) {
  153. /*
  154. * All the MSIX interrupts are allocated at once during probe.
  155. * Therefore we need to update all interrupts even if the WQ
  156. * isn't supporting interrupt operations.
  157. */
  158. ie->int_handle = new_handle;
  159. continue;
  160. }
  161. mutex_lock(&wq->wq_lock);
  162. reinit_completion(&wq->wq_resurrect);
  163. /* Kill percpu_ref to pause additional descriptor submissions */
  164. percpu_ref_kill(&wq->wq_active);
  165. /* Wait for all submitters quiesce before we change interrupt handle */
  166. wait_for_completion(&wq->wq_dead);
  167. ie->int_handle = new_handle;
  168. /* Revive percpu ref and wake up all the waiting submitters */
  169. percpu_ref_reinit(&wq->wq_active);
  170. complete_all(&wq->wq_resurrect);
  171. mutex_unlock(&wq->wq_lock);
  172. /*
  173. * The delay here is to wait for all possible MOVDIR64B that
  174. * are issued before percpu_ref_kill() has happened to have
  175. * reached the PCIe domain before the drain is issued. The driver
  176. * needs to ensure that the drain descriptor issued does not pass
  177. * all the other issued descriptors that contain the invalid
  178. * interrupt handle in order to ensure that the drain descriptor
  179. * interrupt will allow the cleanup of all the descriptors with
  180. * invalid interrupt handle.
  181. */
  182. if (wq_dedicated(wq))
  183. udelay(100);
  184. idxd_int_handle_revoke_drain(ie);
  185. }
  186. kfree(revoke);
  187. }
  188. static int process_misc_interrupts(struct idxd_device *idxd, u32 cause)
  189. {
  190. struct device *dev = &idxd->pdev->dev;
  191. union gensts_reg gensts;
  192. u32 val = 0;
  193. int i;
  194. bool err = false;
  195. if (cause & IDXD_INTC_HALT_STATE)
  196. goto halt;
  197. if (cause & IDXD_INTC_ERR) {
  198. spin_lock(&idxd->dev_lock);
  199. for (i = 0; i < 4; i++)
  200. idxd->sw_err.bits[i] = ioread64(idxd->reg_base +
  201. IDXD_SWERR_OFFSET + i * sizeof(u64));
  202. iowrite64(idxd->sw_err.bits[0] & IDXD_SWERR_ACK,
  203. idxd->reg_base + IDXD_SWERR_OFFSET);
  204. if (idxd->sw_err.valid && idxd->sw_err.wq_idx_valid) {
  205. int id = idxd->sw_err.wq_idx;
  206. struct idxd_wq *wq = idxd->wqs[id];
  207. if (wq->type == IDXD_WQT_USER)
  208. wake_up_interruptible(&wq->err_queue);
  209. } else {
  210. int i;
  211. for (i = 0; i < idxd->max_wqs; i++) {
  212. struct idxd_wq *wq = idxd->wqs[i];
  213. if (wq->type == IDXD_WQT_USER)
  214. wake_up_interruptible(&wq->err_queue);
  215. }
  216. }
  217. spin_unlock(&idxd->dev_lock);
  218. val |= IDXD_INTC_ERR;
  219. for (i = 0; i < 4; i++)
  220. dev_warn(dev, "err[%d]: %#16.16llx\n",
  221. i, idxd->sw_err.bits[i]);
  222. err = true;
  223. }
  224. if (cause & IDXD_INTC_INT_HANDLE_REVOKED) {
  225. struct idxd_int_handle_revoke *revoke;
  226. val |= IDXD_INTC_INT_HANDLE_REVOKED;
  227. revoke = kzalloc(sizeof(*revoke), GFP_ATOMIC);
  228. if (revoke) {
  229. revoke->idxd = idxd;
  230. INIT_WORK(&revoke->work, idxd_int_handle_revoke);
  231. queue_work(idxd->wq, &revoke->work);
  232. } else {
  233. dev_err(dev, "Failed to allocate work for int handle revoke\n");
  234. idxd_wqs_quiesce(idxd);
  235. }
  236. }
  237. if (cause & IDXD_INTC_CMD) {
  238. val |= IDXD_INTC_CMD;
  239. complete(idxd->cmd_done);
  240. }
  241. if (cause & IDXD_INTC_OCCUPY) {
  242. /* Driver does not utilize occupancy interrupt */
  243. val |= IDXD_INTC_OCCUPY;
  244. }
  245. if (cause & IDXD_INTC_PERFMON_OVFL) {
  246. val |= IDXD_INTC_PERFMON_OVFL;
  247. perfmon_counter_overflow(idxd);
  248. }
  249. val ^= cause;
  250. if (val)
  251. dev_warn_once(dev, "Unexpected interrupt cause bits set: %#x\n",
  252. val);
  253. if (!err)
  254. return 0;
  255. halt:
  256. gensts.bits = ioread32(idxd->reg_base + IDXD_GENSTATS_OFFSET);
  257. if (gensts.state == IDXD_DEVICE_STATE_HALT) {
  258. idxd->state = IDXD_DEV_HALTED;
  259. if (gensts.reset_type == IDXD_DEVICE_RESET_SOFTWARE) {
  260. /*
  261. * If we need a software reset, we will throw the work
  262. * on a system workqueue in order to allow interrupts
  263. * for the device command completions.
  264. */
  265. INIT_WORK(&idxd->work, idxd_device_reinit);
  266. queue_work(idxd->wq, &idxd->work);
  267. } else {
  268. idxd->state = IDXD_DEV_HALTED;
  269. idxd_wqs_quiesce(idxd);
  270. idxd_wqs_unmap_portal(idxd);
  271. idxd_device_clear_state(idxd);
  272. dev_err(&idxd->pdev->dev,
  273. "idxd halted, need %s.\n",
  274. gensts.reset_type == IDXD_DEVICE_RESET_FLR ?
  275. "FLR" : "system reset");
  276. return -ENXIO;
  277. }
  278. }
  279. return 0;
  280. }
  281. irqreturn_t idxd_misc_thread(int vec, void *data)
  282. {
  283. struct idxd_irq_entry *irq_entry = data;
  284. struct idxd_device *idxd = ie_to_idxd(irq_entry);
  285. int rc;
  286. u32 cause;
  287. cause = ioread32(idxd->reg_base + IDXD_INTCAUSE_OFFSET);
  288. if (cause)
  289. iowrite32(cause, idxd->reg_base + IDXD_INTCAUSE_OFFSET);
  290. while (cause) {
  291. rc = process_misc_interrupts(idxd, cause);
  292. if (rc < 0)
  293. break;
  294. cause = ioread32(idxd->reg_base + IDXD_INTCAUSE_OFFSET);
  295. if (cause)
  296. iowrite32(cause, idxd->reg_base + IDXD_INTCAUSE_OFFSET);
  297. }
  298. return IRQ_HANDLED;
  299. }
  300. static void idxd_int_handle_resubmit_work(struct work_struct *work)
  301. {
  302. struct idxd_resubmit *irw = container_of(work, struct idxd_resubmit, work);
  303. struct idxd_desc *desc = irw->desc;
  304. struct idxd_wq *wq = desc->wq;
  305. int rc;
  306. desc->completion->status = 0;
  307. rc = idxd_submit_desc(wq, desc);
  308. if (rc < 0) {
  309. dev_dbg(&wq->idxd->pdev->dev, "Failed to resubmit desc %d to wq %d.\n",
  310. desc->id, wq->id);
  311. /*
  312. * If the error is not -EAGAIN, it means the submission failed due to wq
  313. * has been killed instead of ENQCMDS failure. Here the driver needs to
  314. * notify the submitter of the failure by reporting abort status.
  315. *
  316. * -EAGAIN comes from ENQCMDS failure. idxd_submit_desc() will handle the
  317. * abort.
  318. */
  319. if (rc != -EAGAIN) {
  320. desc->completion->status = IDXD_COMP_DESC_ABORT;
  321. idxd_dma_complete_txd(desc, IDXD_COMPLETE_ABORT, false);
  322. }
  323. idxd_free_desc(wq, desc);
  324. }
  325. kfree(irw);
  326. }
  327. bool idxd_queue_int_handle_resubmit(struct idxd_desc *desc)
  328. {
  329. struct idxd_wq *wq = desc->wq;
  330. struct idxd_device *idxd = wq->idxd;
  331. struct idxd_resubmit *irw;
  332. irw = kzalloc(sizeof(*irw), GFP_KERNEL);
  333. if (!irw)
  334. return false;
  335. irw->desc = desc;
  336. INIT_WORK(&irw->work, idxd_int_handle_resubmit_work);
  337. queue_work(idxd->wq, &irw->work);
  338. return true;
  339. }
  340. static void irq_process_pending_llist(struct idxd_irq_entry *irq_entry)
  341. {
  342. struct idxd_desc *desc, *t;
  343. struct llist_node *head;
  344. head = llist_del_all(&irq_entry->pending_llist);
  345. if (!head)
  346. return;
  347. llist_for_each_entry_safe(desc, t, head, llnode) {
  348. u8 status = desc->completion->status & DSA_COMP_STATUS_MASK;
  349. if (status) {
  350. /*
  351. * Check against the original status as ABORT is software defined
  352. * and 0xff, which DSA_COMP_STATUS_MASK can mask out.
  353. */
  354. if (unlikely(desc->completion->status == IDXD_COMP_DESC_ABORT)) {
  355. idxd_dma_complete_txd(desc, IDXD_COMPLETE_ABORT, true);
  356. continue;
  357. }
  358. idxd_dma_complete_txd(desc, IDXD_COMPLETE_NORMAL, true);
  359. } else {
  360. spin_lock(&irq_entry->list_lock);
  361. list_add_tail(&desc->list,
  362. &irq_entry->work_list);
  363. spin_unlock(&irq_entry->list_lock);
  364. }
  365. }
  366. }
  367. static void irq_process_work_list(struct idxd_irq_entry *irq_entry)
  368. {
  369. LIST_HEAD(flist);
  370. struct idxd_desc *desc, *n;
  371. /*
  372. * This lock protects list corruption from access of list outside of the irq handler
  373. * thread.
  374. */
  375. spin_lock(&irq_entry->list_lock);
  376. if (list_empty(&irq_entry->work_list)) {
  377. spin_unlock(&irq_entry->list_lock);
  378. return;
  379. }
  380. list_for_each_entry_safe(desc, n, &irq_entry->work_list, list) {
  381. if (desc->completion->status) {
  382. list_move_tail(&desc->list, &flist);
  383. }
  384. }
  385. spin_unlock(&irq_entry->list_lock);
  386. list_for_each_entry(desc, &flist, list) {
  387. /*
  388. * Check against the original status as ABORT is software defined
  389. * and 0xff, which DSA_COMP_STATUS_MASK can mask out.
  390. */
  391. if (unlikely(desc->completion->status == IDXD_COMP_DESC_ABORT)) {
  392. idxd_dma_complete_txd(desc, IDXD_COMPLETE_ABORT, true);
  393. continue;
  394. }
  395. idxd_dma_complete_txd(desc, IDXD_COMPLETE_NORMAL, true);
  396. }
  397. }
  398. irqreturn_t idxd_wq_thread(int irq, void *data)
  399. {
  400. struct idxd_irq_entry *irq_entry = data;
  401. /*
  402. * There are two lists we are processing. The pending_llist is where
  403. * submmiter adds all the submitted descriptor after sending it to
  404. * the workqueue. It's a lockless singly linked list. The work_list
  405. * is the common linux double linked list. We are in a scenario of
  406. * multiple producers and a single consumer. The producers are all
  407. * the kernel submitters of descriptors, and the consumer is the
  408. * kernel irq handler thread for the msix vector when using threaded
  409. * irq. To work with the restrictions of llist to remain lockless,
  410. * we are doing the following steps:
  411. * 1. Iterate through the work_list and process any completed
  412. * descriptor. Delete the completed entries during iteration.
  413. * 2. llist_del_all() from the pending list.
  414. * 3. Iterate through the llist that was deleted from the pending list
  415. * and process the completed entries.
  416. * 4. If the entry is still waiting on hardware, list_add_tail() to
  417. * the work_list.
  418. */
  419. irq_process_work_list(irq_entry);
  420. irq_process_pending_llist(irq_entry);
  421. return IRQ_HANDLED;
  422. }