blob: e8f64324bb3a71d6584598033c627cdcbe38300f [file] [log] [blame]
Dave Jiangbfe1d562020-01-21 16:43:59 -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/slab.h>
7#include <linux/pci.h>
8#include <linux/interrupt.h>
9#include <linux/delay.h>
10#include <linux/dma-mapping.h>
11#include <linux/workqueue.h>
12#include <linux/aer.h>
13#include <linux/fs.h>
14#include <linux/io-64-nonatomic-lo-hi.h>
15#include <linux/device.h>
16#include <linux/idr.h>
Dave Jiang8e50d392020-10-27 10:34:35 -070017#include <linux/intel-svm.h>
18#include <linux/iommu.h>
Dave Jiangbfe1d562020-01-21 16:43:59 -070019#include <uapi/linux/idxd.h>
Dave Jiang8f47d1a2020-01-21 16:44:23 -070020#include <linux/dmaengine.h>
21#include "../dmaengine.h"
Dave Jiangbfe1d562020-01-21 16:43:59 -070022#include "registers.h"
23#include "idxd.h"
24
25MODULE_VERSION(IDXD_DRIVER_VERSION);
26MODULE_LICENSE("GPL v2");
27MODULE_AUTHOR("Intel Corporation");
28
Dave Jiang03d939c2021-01-22 11:46:00 -070029static bool sva = true;
30module_param(sva, bool, 0644);
31MODULE_PARM_DESC(sva, "Toggle SVA support on/off");
32
Dave Jiangbfe1d562020-01-21 16:43:59 -070033#define DRV_NAME "idxd"
34
Dave Jiang8e50d392020-10-27 10:34:35 -070035bool support_enqcmd;
Dave Jiang4b73e4e2021-04-15 16:38:03 -070036DEFINE_IDA(idxd_ida);
Dave Jiangbfe1d562020-01-21 16:43:59 -070037
Dave Jiang435b5122021-04-15 16:38:09 -070038static struct idxd_driver_data idxd_driver_data[] = {
39 [IDXD_TYPE_DSA] = {
40 .name_prefix = "dsa",
41 .type = IDXD_TYPE_DSA,
42 .compl_size = sizeof(struct dsa_completion_record),
43 .align = 32,
44 .dev_type = &dsa_device_type,
45 },
46 [IDXD_TYPE_IAX] = {
47 .name_prefix = "iax",
48 .type = IDXD_TYPE_IAX,
49 .compl_size = sizeof(struct iax_completion_record),
50 .align = 64,
51 .dev_type = &iax_device_type,
52 },
53};
54
Dave Jiangbfe1d562020-01-21 16:43:59 -070055static struct pci_device_id idxd_pci_tbl[] = {
56 /* DSA ver 1.0 platforms */
Dave Jiang435b5122021-04-15 16:38:09 -070057 { PCI_DEVICE_DATA(INTEL, DSA_SPR0, &idxd_driver_data[IDXD_TYPE_DSA]) },
Dave Jiangf25b46382020-11-17 13:39:14 -070058
59 /* IAX ver 1.0 platforms */
Dave Jiang435b5122021-04-15 16:38:09 -070060 { PCI_DEVICE_DATA(INTEL, IAX_SPR0, &idxd_driver_data[IDXD_TYPE_IAX]) },
Dave Jiangbfe1d562020-01-21 16:43:59 -070061 { 0, }
62};
63MODULE_DEVICE_TABLE(pci, idxd_pci_tbl);
64
Dave Jiangbfe1d562020-01-21 16:43:59 -070065static int idxd_setup_interrupts(struct idxd_device *idxd)
66{
67 struct pci_dev *pdev = idxd->pdev;
68 struct device *dev = &pdev->dev;
Dave Jiangbfe1d562020-01-21 16:43:59 -070069 struct idxd_irq_entry *irq_entry;
70 int i, msixcnt;
71 int rc = 0;
72
73 msixcnt = pci_msix_vec_count(pdev);
74 if (msixcnt < 0) {
75 dev_err(dev, "Not MSI-X interrupt capable.\n");
Dave Jiang5fc8e852021-04-15 16:37:15 -070076 return -ENOSPC;
Dave Jiangbfe1d562020-01-21 16:43:59 -070077 }
78
Dave Jiang5fc8e852021-04-15 16:37:15 -070079 rc = pci_alloc_irq_vectors(pdev, msixcnt, msixcnt, PCI_IRQ_MSIX);
80 if (rc != msixcnt) {
81 dev_err(dev, "Failed enabling %d MSIX entries: %d\n", msixcnt, rc);
82 return -ENOSPC;
Dave Jiangbfe1d562020-01-21 16:43:59 -070083 }
84 dev_dbg(dev, "Enabled %d msix vectors\n", msixcnt);
85
86 /*
87 * We implement 1 completion list per MSI-X entry except for
88 * entry 0, which is for errors and others.
89 */
Dave Jiang47c16ac2021-04-15 16:37:33 -070090 idxd->irq_entries = kcalloc_node(msixcnt, sizeof(struct idxd_irq_entry),
91 GFP_KERNEL, dev_to_node(dev));
Dave Jiangbfe1d562020-01-21 16:43:59 -070092 if (!idxd->irq_entries) {
93 rc = -ENOMEM;
Dave Jiang5fc8e852021-04-15 16:37:15 -070094 goto err_irq_entries;
Dave Jiangbfe1d562020-01-21 16:43:59 -070095 }
96
97 for (i = 0; i < msixcnt; i++) {
98 idxd->irq_entries[i].id = i;
99 idxd->irq_entries[i].idxd = idxd;
Dave Jiang5fc8e852021-04-15 16:37:15 -0700100 idxd->irq_entries[i].vector = pci_irq_vector(pdev, i);
Dave Jiange4f4d8c2020-10-27 10:34:40 -0700101 spin_lock_init(&idxd->irq_entries[i].list_lock);
Dave Jiangbfe1d562020-01-21 16:43:59 -0700102 }
103
Dave Jiangbfe1d562020-01-21 16:43:59 -0700104 irq_entry = &idxd->irq_entries[0];
Dave Jiang5fc8e852021-04-15 16:37:15 -0700105 rc = request_threaded_irq(irq_entry->vector, idxd_irq_handler, idxd_misc_thread,
106 0, "idxd-misc", irq_entry);
Dave Jiangbfe1d562020-01-21 16:43:59 -0700107 if (rc < 0) {
108 dev_err(dev, "Failed to allocate misc interrupt.\n");
Dave Jiang5fc8e852021-04-15 16:37:15 -0700109 goto err_misc_irq;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700110 }
111
Dave Jiang5fc8e852021-04-15 16:37:15 -0700112 dev_dbg(dev, "Allocated idxd-misc handler on msix vector %d\n", irq_entry->vector);
Dave Jiangbfe1d562020-01-21 16:43:59 -0700113
114 /* first MSI-X entry is not for wq interrupts */
115 idxd->num_wq_irqs = msixcnt - 1;
116
117 for (i = 1; i < msixcnt; i++) {
Dave Jiangbfe1d562020-01-21 16:43:59 -0700118 irq_entry = &idxd->irq_entries[i];
119
120 init_llist_head(&idxd->irq_entries[i].pending_llist);
121 INIT_LIST_HEAD(&idxd->irq_entries[i].work_list);
Dave Jiang5fc8e852021-04-15 16:37:15 -0700122 rc = request_threaded_irq(irq_entry->vector, idxd_irq_handler,
123 idxd_wq_thread, 0, "idxd-portal", irq_entry);
Dave Jiangbfe1d562020-01-21 16:43:59 -0700124 if (rc < 0) {
Dave Jiang5fc8e852021-04-15 16:37:15 -0700125 dev_err(dev, "Failed to allocate irq %d.\n", irq_entry->vector);
126 goto err_wq_irqs;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700127 }
Dave Jiang5fc8e852021-04-15 16:37:15 -0700128 dev_dbg(dev, "Allocated idxd-msix %d for vector %d\n", i, irq_entry->vector);
Dave Jiangbfe1d562020-01-21 16:43:59 -0700129 }
130
131 idxd_unmask_error_interrupts(idxd);
Dave Jiang6df0e6c2021-04-12 09:23:27 -0700132 idxd_msix_perm_setup(idxd);
Dave Jiangbfe1d562020-01-21 16:43:59 -0700133 return 0;
134
Dave Jiang5fc8e852021-04-15 16:37:15 -0700135 err_wq_irqs:
136 while (--i >= 0) {
137 irq_entry = &idxd->irq_entries[i];
138 free_irq(irq_entry->vector, irq_entry);
139 }
140 err_misc_irq:
Dave Jiangbfe1d562020-01-21 16:43:59 -0700141 /* Disable error interrupt generation */
142 idxd_mask_error_interrupts(idxd);
Dave Jiang5fc8e852021-04-15 16:37:15 -0700143 err_irq_entries:
144 pci_free_irq_vectors(pdev);
Dave Jiangbfe1d562020-01-21 16:43:59 -0700145 dev_err(dev, "No usable interrupts\n");
146 return rc;
147}
148
Dave Jiang7c5dd232021-04-15 16:37:39 -0700149static int idxd_setup_wqs(struct idxd_device *idxd)
150{
151 struct device *dev = &idxd->pdev->dev;
152 struct idxd_wq *wq;
153 int i, rc;
154
155 idxd->wqs = kcalloc_node(idxd->max_wqs, sizeof(struct idxd_wq *),
156 GFP_KERNEL, dev_to_node(dev));
157 if (!idxd->wqs)
158 return -ENOMEM;
159
160 for (i = 0; i < idxd->max_wqs; i++) {
161 wq = kzalloc_node(sizeof(*wq), GFP_KERNEL, dev_to_node(dev));
162 if (!wq) {
163 rc = -ENOMEM;
164 goto err;
165 }
166
167 wq->id = i;
168 wq->idxd = idxd;
169 device_initialize(&wq->conf_dev);
170 wq->conf_dev.parent = &idxd->conf_dev;
Dave Jiang4b73e4e2021-04-15 16:38:03 -0700171 wq->conf_dev.bus = &dsa_bus_type;
Dave Jiang7c5dd232021-04-15 16:37:39 -0700172 wq->conf_dev.type = &idxd_wq_device_type;
173 rc = dev_set_name(&wq->conf_dev, "wq%d.%d", idxd->id, wq->id);
174 if (rc < 0) {
175 put_device(&wq->conf_dev);
176 goto err;
177 }
178
179 mutex_init(&wq->wq_lock);
Dave Jiang04922b72021-04-15 16:37:57 -0700180 init_waitqueue_head(&wq->err_queue);
Dave Jiang7c5dd232021-04-15 16:37:39 -0700181 wq->max_xfer_bytes = idxd->max_xfer_bytes;
182 wq->max_batch_size = idxd->max_batch_size;
183 wq->wqcfg = kzalloc_node(idxd->wqcfg_size, GFP_KERNEL, dev_to_node(dev));
184 if (!wq->wqcfg) {
185 put_device(&wq->conf_dev);
186 rc = -ENOMEM;
187 goto err;
188 }
189 idxd->wqs[i] = wq;
190 }
191
192 return 0;
193
194 err:
195 while (--i >= 0)
196 put_device(&idxd->wqs[i]->conf_dev);
197 return rc;
198}
199
Dave Jiang75b91132021-04-15 16:37:44 -0700200static int idxd_setup_engines(struct idxd_device *idxd)
201{
202 struct idxd_engine *engine;
203 struct device *dev = &idxd->pdev->dev;
204 int i, rc;
205
206 idxd->engines = kcalloc_node(idxd->max_engines, sizeof(struct idxd_engine *),
207 GFP_KERNEL, dev_to_node(dev));
208 if (!idxd->engines)
209 return -ENOMEM;
210
211 for (i = 0; i < idxd->max_engines; i++) {
212 engine = kzalloc_node(sizeof(*engine), GFP_KERNEL, dev_to_node(dev));
213 if (!engine) {
214 rc = -ENOMEM;
215 goto err;
216 }
217
218 engine->id = i;
219 engine->idxd = idxd;
220 device_initialize(&engine->conf_dev);
221 engine->conf_dev.parent = &idxd->conf_dev;
222 engine->conf_dev.type = &idxd_engine_device_type;
223 rc = dev_set_name(&engine->conf_dev, "engine%d.%d", idxd->id, engine->id);
224 if (rc < 0) {
225 put_device(&engine->conf_dev);
226 goto err;
227 }
228
229 idxd->engines[i] = engine;
230 }
231
232 return 0;
233
234 err:
235 while (--i >= 0)
236 put_device(&idxd->engines[i]->conf_dev);
237 return rc;
238}
239
Dave Jiangdefe49f2021-04-15 16:37:51 -0700240static int idxd_setup_groups(struct idxd_device *idxd)
241{
242 struct device *dev = &idxd->pdev->dev;
243 struct idxd_group *group;
244 int i, rc;
245
246 idxd->groups = kcalloc_node(idxd->max_groups, sizeof(struct idxd_group *),
247 GFP_KERNEL, dev_to_node(dev));
248 if (!idxd->groups)
249 return -ENOMEM;
250
251 for (i = 0; i < idxd->max_groups; i++) {
252 group = kzalloc_node(sizeof(*group), GFP_KERNEL, dev_to_node(dev));
253 if (!group) {
254 rc = -ENOMEM;
255 goto err;
256 }
257
258 group->id = i;
259 group->idxd = idxd;
260 device_initialize(&group->conf_dev);
261 group->conf_dev.parent = &idxd->conf_dev;
Dave Jiang4b73e4e2021-04-15 16:38:03 -0700262 group->conf_dev.bus = &dsa_bus_type;
Dave Jiangdefe49f2021-04-15 16:37:51 -0700263 group->conf_dev.type = &idxd_group_device_type;
264 rc = dev_set_name(&group->conf_dev, "group%d.%d", idxd->id, group->id);
265 if (rc < 0) {
266 put_device(&group->conf_dev);
267 goto err;
268 }
269
270 idxd->groups[i] = group;
271 group->tc_a = -1;
272 group->tc_b = -1;
273 }
274
275 return 0;
276
277 err:
278 while (--i >= 0)
279 put_device(&idxd->groups[i]->conf_dev);
280 return rc;
281}
282
Dave Jiangbfe1d562020-01-21 16:43:59 -0700283static int idxd_setup_internals(struct idxd_device *idxd)
284{
285 struct device *dev = &idxd->pdev->dev;
Dave Jiangdefe49f2021-04-15 16:37:51 -0700286 int rc, i;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700287
Dave Jiang0d5c10b2020-06-26 11:11:18 -0700288 init_waitqueue_head(&idxd->cmd_waitq);
Dave Jiang7c5dd232021-04-15 16:37:39 -0700289
290 rc = idxd_setup_wqs(idxd);
291 if (rc < 0)
292 return rc;
293
Dave Jiang75b91132021-04-15 16:37:44 -0700294 rc = idxd_setup_engines(idxd);
295 if (rc < 0)
296 goto err_engine;
297
Dave Jiangdefe49f2021-04-15 16:37:51 -0700298 rc = idxd_setup_groups(idxd);
299 if (rc < 0)
300 goto err_group;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700301
Dave Jiang0d5c10b2020-06-26 11:11:18 -0700302 idxd->wq = create_workqueue(dev_name(dev));
Dave Jiang7c5dd232021-04-15 16:37:39 -0700303 if (!idxd->wq) {
304 rc = -ENOMEM;
Dave Jiangdefe49f2021-04-15 16:37:51 -0700305 goto err_wkq_create;
Dave Jiang7c5dd232021-04-15 16:37:39 -0700306 }
Dave Jiang0d5c10b2020-06-26 11:11:18 -0700307
Dave Jiangbfe1d562020-01-21 16:43:59 -0700308 return 0;
Dave Jiang7c5dd232021-04-15 16:37:39 -0700309
Dave Jiangdefe49f2021-04-15 16:37:51 -0700310 err_wkq_create:
311 for (i = 0; i < idxd->max_groups; i++)
312 put_device(&idxd->groups[i]->conf_dev);
313 err_group:
Dave Jiang75b91132021-04-15 16:37:44 -0700314 for (i = 0; i < idxd->max_engines; i++)
315 put_device(&idxd->engines[i]->conf_dev);
316 err_engine:
Dave Jiang7c5dd232021-04-15 16:37:39 -0700317 for (i = 0; i < idxd->max_wqs; i++)
318 put_device(&idxd->wqs[i]->conf_dev);
319 return rc;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700320}
321
322static void idxd_read_table_offsets(struct idxd_device *idxd)
323{
324 union offsets_reg offsets;
325 struct device *dev = &idxd->pdev->dev;
326
327 offsets.bits[0] = ioread64(idxd->reg_base + IDXD_TABLE_OFFSET);
Dave Jiang2f8417a2020-10-30 08:51:56 -0700328 offsets.bits[1] = ioread64(idxd->reg_base + IDXD_TABLE_OFFSET + sizeof(u64));
329 idxd->grpcfg_offset = offsets.grpcfg * IDXD_TABLE_MULT;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700330 dev_dbg(dev, "IDXD Group Config Offset: %#x\n", idxd->grpcfg_offset);
Dave Jiang2f8417a2020-10-30 08:51:56 -0700331 idxd->wqcfg_offset = offsets.wqcfg * IDXD_TABLE_MULT;
332 dev_dbg(dev, "IDXD Work Queue Config Offset: %#x\n", idxd->wqcfg_offset);
333 idxd->msix_perm_offset = offsets.msix_perm * IDXD_TABLE_MULT;
334 dev_dbg(dev, "IDXD MSIX Permission Offset: %#x\n", idxd->msix_perm_offset);
335 idxd->perfmon_offset = offsets.perfmon * IDXD_TABLE_MULT;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700336 dev_dbg(dev, "IDXD Perfmon Offset: %#x\n", idxd->perfmon_offset);
337}
338
339static void idxd_read_caps(struct idxd_device *idxd)
340{
341 struct device *dev = &idxd->pdev->dev;
342 int i;
343
344 /* reading generic capabilities */
345 idxd->hw.gen_cap.bits = ioread64(idxd->reg_base + IDXD_GENCAP_OFFSET);
346 dev_dbg(dev, "gen_cap: %#llx\n", idxd->hw.gen_cap.bits);
347 idxd->max_xfer_bytes = 1ULL << idxd->hw.gen_cap.max_xfer_shift;
348 dev_dbg(dev, "max xfer size: %llu bytes\n", idxd->max_xfer_bytes);
349 idxd->max_batch_size = 1U << idxd->hw.gen_cap.max_batch_shift;
350 dev_dbg(dev, "max batch size: %u\n", idxd->max_batch_size);
351 if (idxd->hw.gen_cap.config_en)
352 set_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags);
353
354 /* reading group capabilities */
355 idxd->hw.group_cap.bits =
356 ioread64(idxd->reg_base + IDXD_GRPCAP_OFFSET);
357 dev_dbg(dev, "group_cap: %#llx\n", idxd->hw.group_cap.bits);
358 idxd->max_groups = idxd->hw.group_cap.num_groups;
359 dev_dbg(dev, "max groups: %u\n", idxd->max_groups);
360 idxd->max_tokens = idxd->hw.group_cap.total_tokens;
361 dev_dbg(dev, "max tokens: %u\n", idxd->max_tokens);
Dave Jiangc52ca472020-01-21 16:44:05 -0700362 idxd->nr_tokens = idxd->max_tokens;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700363
364 /* read engine capabilities */
365 idxd->hw.engine_cap.bits =
366 ioread64(idxd->reg_base + IDXD_ENGCAP_OFFSET);
367 dev_dbg(dev, "engine_cap: %#llx\n", idxd->hw.engine_cap.bits);
368 idxd->max_engines = idxd->hw.engine_cap.num_engines;
369 dev_dbg(dev, "max engines: %u\n", idxd->max_engines);
370
371 /* read workqueue capabilities */
372 idxd->hw.wq_cap.bits = ioread64(idxd->reg_base + IDXD_WQCAP_OFFSET);
373 dev_dbg(dev, "wq_cap: %#llx\n", idxd->hw.wq_cap.bits);
374 idxd->max_wq_size = idxd->hw.wq_cap.total_wq_size;
375 dev_dbg(dev, "total workqueue size: %u\n", idxd->max_wq_size);
376 idxd->max_wqs = idxd->hw.wq_cap.num_wqs;
377 dev_dbg(dev, "max workqueues: %u\n", idxd->max_wqs);
Dave Jiangd98793b2020-10-27 14:34:09 -0700378 idxd->wqcfg_size = 1 << (idxd->hw.wq_cap.wqcfg_size + IDXD_WQCFG_MIN);
379 dev_dbg(dev, "wqcfg size: %u\n", idxd->wqcfg_size);
Dave Jiangbfe1d562020-01-21 16:43:59 -0700380
381 /* reading operation capabilities */
382 for (i = 0; i < 4; i++) {
383 idxd->hw.opcap.bits[i] = ioread64(idxd->reg_base +
384 IDXD_OPCAP_OFFSET + i * sizeof(u64));
385 dev_dbg(dev, "opcap[%d]: %#llx\n", i, idxd->hw.opcap.bits[i]);
386 }
387}
388
Dave Jiang435b5122021-04-15 16:38:09 -0700389static struct idxd_device *idxd_alloc(struct pci_dev *pdev, struct idxd_driver_data *data)
Dave Jiangbfe1d562020-01-21 16:43:59 -0700390{
391 struct device *dev = &pdev->dev;
392 struct idxd_device *idxd;
Dave Jiang47c16ac2021-04-15 16:37:33 -0700393 int rc;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700394
Dave Jiang47c16ac2021-04-15 16:37:33 -0700395 idxd = kzalloc_node(sizeof(*idxd), GFP_KERNEL, dev_to_node(dev));
Dave Jiangbfe1d562020-01-21 16:43:59 -0700396 if (!idxd)
397 return NULL;
398
399 idxd->pdev = pdev;
Dave Jiang435b5122021-04-15 16:38:09 -0700400 idxd->data = data;
Dave Jiang4b73e4e2021-04-15 16:38:03 -0700401 idxd->id = ida_alloc(&idxd_ida, GFP_KERNEL);
Dave Jiang47c16ac2021-04-15 16:37:33 -0700402 if (idxd->id < 0)
403 return NULL;
404
405 device_initialize(&idxd->conf_dev);
406 idxd->conf_dev.parent = dev;
Dave Jiang4b73e4e2021-04-15 16:38:03 -0700407 idxd->conf_dev.bus = &dsa_bus_type;
Dave Jiang435b5122021-04-15 16:38:09 -0700408 idxd->conf_dev.type = idxd->data->dev_type;
409 rc = dev_set_name(&idxd->conf_dev, "%s%d", idxd->data->name_prefix, idxd->id);
Dave Jiang47c16ac2021-04-15 16:37:33 -0700410 if (rc < 0) {
411 put_device(&idxd->conf_dev);
412 return NULL;
413 }
414
Dave Jiangbfe1d562020-01-21 16:43:59 -0700415 spin_lock_init(&idxd->dev_lock);
416
417 return idxd;
418}
419
Dave Jiang8e50d392020-10-27 10:34:35 -0700420static int idxd_enable_system_pasid(struct idxd_device *idxd)
421{
422 int flags;
423 unsigned int pasid;
424 struct iommu_sva *sva;
425
426 flags = SVM_FLAG_SUPERVISOR_MODE;
427
428 sva = iommu_sva_bind_device(&idxd->pdev->dev, NULL, &flags);
429 if (IS_ERR(sva)) {
430 dev_warn(&idxd->pdev->dev,
431 "iommu sva bind failed: %ld\n", PTR_ERR(sva));
432 return PTR_ERR(sva);
433 }
434
435 pasid = iommu_sva_get_pasid(sva);
436 if (pasid == IOMMU_PASID_INVALID) {
437 iommu_sva_unbind_device(sva);
438 return -ENODEV;
439 }
440
441 idxd->sva = sva;
442 idxd->pasid = pasid;
443 dev_dbg(&idxd->pdev->dev, "system pasid: %u\n", pasid);
444 return 0;
445}
446
447static void idxd_disable_system_pasid(struct idxd_device *idxd)
448{
449
450 iommu_sva_unbind_device(idxd->sva);
451 idxd->sva = NULL;
452}
453
Dave Jiangbfe1d562020-01-21 16:43:59 -0700454static int idxd_probe(struct idxd_device *idxd)
455{
456 struct pci_dev *pdev = idxd->pdev;
457 struct device *dev = &pdev->dev;
458 int rc;
459
460 dev_dbg(dev, "%s entered and resetting device\n", __func__);
Dave Jiang89e3bec2021-02-01 08:26:14 -0700461 rc = idxd_device_init_reset(idxd);
462 if (rc < 0)
463 return rc;
464
Dave Jiangbfe1d562020-01-21 16:43:59 -0700465 dev_dbg(dev, "IDXD reset complete\n");
466
Dave Jiang03d939c2021-01-22 11:46:00 -0700467 if (IS_ENABLED(CONFIG_INTEL_IDXD_SVM) && sva) {
Dave Jiang8e50d392020-10-27 10:34:35 -0700468 rc = idxd_enable_system_pasid(idxd);
469 if (rc < 0)
470 dev_warn(dev, "Failed to enable PASID. No SVA support: %d\n", rc);
471 else
472 set_bit(IDXD_FLAG_PASID_ENABLED, &idxd->flags);
Dave Jiang03d939c2021-01-22 11:46:00 -0700473 } else if (!sva) {
474 dev_warn(dev, "User forced SVA off via module param.\n");
Dave Jiang8e50d392020-10-27 10:34:35 -0700475 }
476
Dave Jiangbfe1d562020-01-21 16:43:59 -0700477 idxd_read_caps(idxd);
478 idxd_read_table_offsets(idxd);
479
480 rc = idxd_setup_internals(idxd);
481 if (rc)
Dave Jiang7c5dd232021-04-15 16:37:39 -0700482 goto err;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700483
484 rc = idxd_setup_interrupts(idxd);
485 if (rc)
Dave Jiang7c5dd232021-04-15 16:37:39 -0700486 goto err;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700487
488 dev_dbg(dev, "IDXD interrupt setup complete.\n");
489
Dave Jiang42d279f2020-01-21 16:44:29 -0700490 idxd->major = idxd_cdev_get_major(idxd);
491
Dave Jiangbfe1d562020-01-21 16:43:59 -0700492 dev_dbg(dev, "IDXD device %d probed successfully\n", idxd->id);
493 return 0;
494
Dave Jiang7c5dd232021-04-15 16:37:39 -0700495 err:
Dave Jiang8e50d392020-10-27 10:34:35 -0700496 if (device_pasid_enabled(idxd))
497 idxd_disable_system_pasid(idxd);
Dave Jiangbfe1d562020-01-21 16:43:59 -0700498 return rc;
499}
500
501static int idxd_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
502{
Dave Jiangbfe1d562020-01-21 16:43:59 -0700503 struct device *dev = &pdev->dev;
504 struct idxd_device *idxd;
Dave Jiang435b5122021-04-15 16:38:09 -0700505 struct idxd_driver_data *data = (struct idxd_driver_data *)id->driver_data;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700506 int rc;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700507
Dave Jianga39c7cd2021-04-15 16:37:21 -0700508 rc = pci_enable_device(pdev);
Dave Jiangbfe1d562020-01-21 16:43:59 -0700509 if (rc)
510 return rc;
511
Dave Jiang8e50d392020-10-27 10:34:35 -0700512 dev_dbg(dev, "Alloc IDXD context\n");
Dave Jiang435b5122021-04-15 16:38:09 -0700513 idxd = idxd_alloc(pdev, data);
Dave Jianga39c7cd2021-04-15 16:37:21 -0700514 if (!idxd) {
515 rc = -ENOMEM;
516 goto err_idxd_alloc;
517 }
Dave Jiangbfe1d562020-01-21 16:43:59 -0700518
Dave Jiang8e50d392020-10-27 10:34:35 -0700519 dev_dbg(dev, "Mapping BARs\n");
Dave Jianga39c7cd2021-04-15 16:37:21 -0700520 idxd->reg_base = pci_iomap(pdev, IDXD_MMIO_BAR, 0);
521 if (!idxd->reg_base) {
522 rc = -ENOMEM;
523 goto err_iomap;
524 }
Dave Jiangbfe1d562020-01-21 16:43:59 -0700525
526 dev_dbg(dev, "Set DMA masks\n");
527 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
528 if (rc)
529 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
530 if (rc)
Dave Jianga39c7cd2021-04-15 16:37:21 -0700531 goto err;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700532
533 rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
534 if (rc)
535 rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
536 if (rc)
Dave Jianga39c7cd2021-04-15 16:37:21 -0700537 goto err;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700538
Dave Jiangbfe1d562020-01-21 16:43:59 -0700539 dev_dbg(dev, "Set PCI master\n");
540 pci_set_master(pdev);
541 pci_set_drvdata(pdev, idxd);
542
543 idxd->hw.version = ioread32(idxd->reg_base + IDXD_VER_OFFSET);
544 rc = idxd_probe(idxd);
545 if (rc) {
546 dev_err(dev, "Intel(R) IDXD DMA Engine init failed\n");
Dave Jianga39c7cd2021-04-15 16:37:21 -0700547 goto err;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700548 }
549
Dave Jiang47c16ac2021-04-15 16:37:33 -0700550 rc = idxd_register_devices(idxd);
Dave Jiangc52ca472020-01-21 16:44:05 -0700551 if (rc) {
552 dev_err(dev, "IDXD sysfs setup failed\n");
Dave Jianga39c7cd2021-04-15 16:37:21 -0700553 goto err;
Dave Jiangc52ca472020-01-21 16:44:05 -0700554 }
555
556 idxd->state = IDXD_DEV_CONF_READY;
557
Dave Jiangbfe1d562020-01-21 16:43:59 -0700558 dev_info(&pdev->dev, "Intel(R) Accelerator Device (v%x)\n",
559 idxd->hw.version);
560
561 return 0;
Dave Jianga39c7cd2021-04-15 16:37:21 -0700562
563 err:
564 pci_iounmap(pdev, idxd->reg_base);
565 err_iomap:
Dave Jiang47c16ac2021-04-15 16:37:33 -0700566 put_device(&idxd->conf_dev);
Dave Jianga39c7cd2021-04-15 16:37:21 -0700567 err_idxd_alloc:
568 pci_disable_device(pdev);
569 return rc;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700570}
571
Dave Jiang8f47d1a2020-01-21 16:44:23 -0700572static void idxd_flush_pending_llist(struct idxd_irq_entry *ie)
573{
574 struct idxd_desc *desc, *itr;
575 struct llist_node *head;
576
577 head = llist_del_all(&ie->pending_llist);
578 if (!head)
579 return;
580
581 llist_for_each_entry_safe(desc, itr, head, llnode) {
582 idxd_dma_complete_txd(desc, IDXD_COMPLETE_ABORT);
583 idxd_free_desc(desc->wq, desc);
584 }
585}
586
587static void idxd_flush_work_list(struct idxd_irq_entry *ie)
588{
589 struct idxd_desc *desc, *iter;
590
591 list_for_each_entry_safe(desc, iter, &ie->work_list, list) {
592 list_del(&desc->list);
593 idxd_dma_complete_txd(desc, IDXD_COMPLETE_ABORT);
594 idxd_free_desc(desc->wq, desc);
595 }
596}
597
Dave Jiangbfe1d562020-01-21 16:43:59 -0700598static void idxd_shutdown(struct pci_dev *pdev)
599{
600 struct idxd_device *idxd = pci_get_drvdata(pdev);
601 int rc, i;
602 struct idxd_irq_entry *irq_entry;
603 int msixcnt = pci_msix_vec_count(pdev);
Dave Jiangbfe1d562020-01-21 16:43:59 -0700604
Dave Jiangbfe1d562020-01-21 16:43:59 -0700605 rc = idxd_device_disable(idxd);
Dave Jiangbfe1d562020-01-21 16:43:59 -0700606 if (rc)
607 dev_err(&pdev->dev, "Disabling device failed\n");
608
609 dev_dbg(&pdev->dev, "%s called\n", __func__);
610 idxd_mask_msix_vectors(idxd);
611 idxd_mask_error_interrupts(idxd);
612
613 for (i = 0; i < msixcnt; i++) {
614 irq_entry = &idxd->irq_entries[i];
Dave Jiang5fc8e852021-04-15 16:37:15 -0700615 synchronize_irq(irq_entry->vector);
616 free_irq(irq_entry->vector, irq_entry);
Dave Jiangbfe1d562020-01-21 16:43:59 -0700617 if (i == 0)
618 continue;
Dave Jiang8f47d1a2020-01-21 16:44:23 -0700619 idxd_flush_pending_llist(irq_entry);
620 idxd_flush_work_list(irq_entry);
Dave Jiangbfe1d562020-01-21 16:43:59 -0700621 }
Dave Jiang0d5c10b2020-06-26 11:11:18 -0700622
Dave Jiang6df0e6c2021-04-12 09:23:27 -0700623 idxd_msix_perm_clear(idxd);
Dave Jiang5fc8e852021-04-15 16:37:15 -0700624 pci_free_irq_vectors(pdev);
Dave Jianga39c7cd2021-04-15 16:37:21 -0700625 pci_iounmap(pdev, idxd->reg_base);
626 pci_disable_device(pdev);
Dave Jiang0d5c10b2020-06-26 11:11:18 -0700627 destroy_workqueue(idxd->wq);
Dave Jiangbfe1d562020-01-21 16:43:59 -0700628}
629
630static void idxd_remove(struct pci_dev *pdev)
631{
632 struct idxd_device *idxd = pci_get_drvdata(pdev);
633
634 dev_dbg(&pdev->dev, "%s called\n", __func__);
635 idxd_shutdown(pdev);
Dave Jiang8e50d392020-10-27 10:34:35 -0700636 if (device_pasid_enabled(idxd))
637 idxd_disable_system_pasid(idxd);
Dave Jiang47c16ac2021-04-15 16:37:33 -0700638 idxd_unregister_devices(idxd);
Dave Jiangbfe1d562020-01-21 16:43:59 -0700639}
640
641static struct pci_driver idxd_pci_driver = {
642 .name = DRV_NAME,
643 .id_table = idxd_pci_tbl,
644 .probe = idxd_pci_probe,
645 .remove = idxd_remove,
646 .shutdown = idxd_shutdown,
647};
648
649static int __init idxd_init_module(void)
650{
Dave Jiang4b73e4e2021-04-15 16:38:03 -0700651 int err;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700652
653 /*
Dave Jiang8e50d392020-10-27 10:34:35 -0700654 * If the CPU does not support MOVDIR64B or ENQCMDS, there's no point in
Dave Jiangbfe1d562020-01-21 16:43:59 -0700655 * enumerating the device. We can not utilize it.
656 */
657 if (!boot_cpu_has(X86_FEATURE_MOVDIR64B)) {
658 pr_warn("idxd driver failed to load without MOVDIR64B.\n");
659 return -ENODEV;
660 }
661
Dave Jiang8e50d392020-10-27 10:34:35 -0700662 if (!boot_cpu_has(X86_FEATURE_ENQCMD))
663 pr_warn("Platform does not have ENQCMD(S) support.\n");
664 else
665 support_enqcmd = true;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700666
Dave Jiangc52ca472020-01-21 16:44:05 -0700667 err = idxd_register_bus_type();
668 if (err < 0)
Dave Jiangbfe1d562020-01-21 16:43:59 -0700669 return err;
670
Dave Jiangc52ca472020-01-21 16:44:05 -0700671 err = idxd_register_driver();
672 if (err < 0)
673 goto err_idxd_driver_register;
674
Dave Jiang42d279f2020-01-21 16:44:29 -0700675 err = idxd_cdev_register();
676 if (err)
677 goto err_cdev_register;
678
Dave Jiangc52ca472020-01-21 16:44:05 -0700679 err = pci_register_driver(&idxd_pci_driver);
680 if (err)
681 goto err_pci_register;
682
Dave Jiangbfe1d562020-01-21 16:43:59 -0700683 return 0;
Dave Jiangc52ca472020-01-21 16:44:05 -0700684
685err_pci_register:
Dave Jiang42d279f2020-01-21 16:44:29 -0700686 idxd_cdev_remove();
687err_cdev_register:
Dave Jiangc52ca472020-01-21 16:44:05 -0700688 idxd_unregister_driver();
689err_idxd_driver_register:
690 idxd_unregister_bus_type();
691 return err;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700692}
693module_init(idxd_init_module);
694
695static void __exit idxd_exit_module(void)
696{
697 pci_unregister_driver(&idxd_pci_driver);
Dave Jiang42d279f2020-01-21 16:44:29 -0700698 idxd_cdev_remove();
Dave Jiangc52ca472020-01-21 16:44:05 -0700699 idxd_unregister_bus_type();
Dave Jiangbfe1d562020-01-21 16:43:59 -0700700}
701module_exit(idxd_exit_module);