blob: 417048e3c42aa88cdb27288625695720407018a1 [file] [log] [blame]
Dave Jiangd1dfe5b2020-01-21 16:44:17 -07001// 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 <uapi/linux/idxd.h>
8#include "idxd.h"
9#include "registers.h"
10
Dave Jiang07051072020-06-15 13:54:26 -070011static struct idxd_desc *__get_desc(struct idxd_wq *wq, int idx, int cpu)
Dave Jiangd1dfe5b2020-01-21 16:44:17 -070012{
13 struct idxd_desc *desc;
Dave Jiangd1dfe5b2020-01-21 16:44:17 -070014
15 desc = wq->descs[idx];
16 memset(desc->hw, 0, sizeof(struct dsa_hw_desc));
17 memset(desc->completion, 0, sizeof(struct dsa_completion_record));
Dave Jiang07051072020-06-15 13:54:26 -070018 desc->cpu = cpu;
Dave Jiangd1dfe5b2020-01-21 16:44:17 -070019 return desc;
20}
21
Dave Jiang07051072020-06-15 13:54:26 -070022struct idxd_desc *idxd_alloc_desc(struct idxd_wq *wq, enum idxd_op_type optype)
23{
24 int cpu, idx;
25 struct idxd_device *idxd = wq->idxd;
26 DEFINE_SBQ_WAIT(wait);
27 struct sbq_wait_state *ws;
28 struct sbitmap_queue *sbq;
29
30 if (idxd->state != IDXD_DEV_ENABLED)
31 return ERR_PTR(-EIO);
32
33 sbq = &wq->sbq;
34 idx = sbitmap_queue_get(sbq, &cpu);
35 if (idx < 0) {
36 if (optype == IDXD_OP_NONBLOCK)
37 return ERR_PTR(-EAGAIN);
38 } else {
39 return __get_desc(wq, idx, cpu);
40 }
41
42 ws = &sbq->ws[0];
43 for (;;) {
44 sbitmap_prepare_to_wait(sbq, ws, &wait, TASK_INTERRUPTIBLE);
45 if (signal_pending_state(TASK_INTERRUPTIBLE, current))
46 break;
47 idx = sbitmap_queue_get(sbq, &cpu);
48 if (idx > 0)
49 break;
50 schedule();
51 }
52
53 sbitmap_finish_wait(sbq, ws, &wait);
54 if (idx < 0)
55 return ERR_PTR(-EAGAIN);
56
57 return __get_desc(wq, idx, cpu);
58}
59
Dave Jiangd1dfe5b2020-01-21 16:44:17 -070060void idxd_free_desc(struct idxd_wq *wq, struct idxd_desc *desc)
61{
Dave Jiang07051072020-06-15 13:54:26 -070062 int cpu = desc->cpu;
Dave Jiangd1dfe5b2020-01-21 16:44:17 -070063
Dave Jiang07051072020-06-15 13:54:26 -070064 desc->cpu = -1;
65 sbitmap_queue_clear(&wq->sbq, desc->id, cpu);
Dave Jiangd1dfe5b2020-01-21 16:44:17 -070066}
67
68int idxd_submit_desc(struct idxd_wq *wq, struct idxd_desc *desc)
69{
70 struct idxd_device *idxd = wq->idxd;
71 int vec = desc->hw->int_handle;
Dave Jiang42d279f2020-01-21 16:44:29 -070072 void __iomem *portal;
Dave Jiangd1dfe5b2020-01-21 16:44:17 -070073
74 if (idxd->state != IDXD_DEV_ENABLED)
75 return -EIO;
76
Dave Jiang8326be9f2020-11-11 15:23:46 -070077 portal = wq->dportal;
Dave Jiangd1dfe5b2020-01-21 16:44:17 -070078 /*
79 * The wmb() flushes writes to coherent DMA data before possibly
80 * triggering a DMA read. The wmb() is necessary even on UP because
81 * the recipient is a device.
82 */
83 wmb();
Dave Jiang42d279f2020-01-21 16:44:29 -070084 iosubmit_cmds512(portal, desc->hw, 1);
Dave Jiangd1dfe5b2020-01-21 16:44:17 -070085
86 /*
87 * Pending the descriptor to the lockless list for the irq_entry
88 * that we designated the descriptor to.
89 */
Dave Jiang8f47d1a2020-01-21 16:44:23 -070090 if (desc->hw->flags & IDXD_OP_FLAG_RCI)
91 llist_add(&desc->llnode,
92 &idxd->irq_entries[vec].pending_llist);
Dave Jiangd1dfe5b2020-01-21 16:44:17 -070093
94 return 0;
95}