blob: 9687a24ff98266d310defb70a9072653ba87f304 [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;
36
Dave Jiangbfe1d562020-01-21 16:43:59 -070037static struct idr idxd_idrs[IDXD_TYPE_MAX];
Zheng Yongjune2fcd6e2020-12-24 21:22:54 +080038static DEFINE_MUTEX(idxd_idr_lock);
Dave Jiangbfe1d562020-01-21 16:43:59 -070039
40static struct pci_device_id idxd_pci_tbl[] = {
41 /* DSA ver 1.0 platforms */
42 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_DSA_SPR0) },
Dave Jiangf25b46382020-11-17 13:39:14 -070043
44 /* IAX ver 1.0 platforms */
45 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IAX_SPR0) },
Dave Jiangbfe1d562020-01-21 16:43:59 -070046 { 0, }
47};
48MODULE_DEVICE_TABLE(pci, idxd_pci_tbl);
49
50static char *idxd_name[] = {
51 "dsa",
Dave Jiangf25b46382020-11-17 13:39:14 -070052 "iax"
Dave Jiangbfe1d562020-01-21 16:43:59 -070053};
54
55const char *idxd_get_dev_name(struct idxd_device *idxd)
56{
57 return idxd_name[idxd->type];
58}
59
60static int idxd_setup_interrupts(struct idxd_device *idxd)
61{
62 struct pci_dev *pdev = idxd->pdev;
63 struct device *dev = &pdev->dev;
64 struct msix_entry *msix;
65 struct idxd_irq_entry *irq_entry;
66 int i, msixcnt;
67 int rc = 0;
Dave Jiang8e50d392020-10-27 10:34:35 -070068 union msix_perm mperm;
Dave Jiangbfe1d562020-01-21 16:43:59 -070069
70 msixcnt = pci_msix_vec_count(pdev);
71 if (msixcnt < 0) {
72 dev_err(dev, "Not MSI-X interrupt capable.\n");
73 goto err_no_irq;
74 }
75
76 idxd->msix_entries = devm_kzalloc(dev, sizeof(struct msix_entry) *
77 msixcnt, GFP_KERNEL);
78 if (!idxd->msix_entries) {
79 rc = -ENOMEM;
80 goto err_no_irq;
81 }
82
83 for (i = 0; i < msixcnt; i++)
84 idxd->msix_entries[i].entry = i;
85
86 rc = pci_enable_msix_exact(pdev, idxd->msix_entries, msixcnt);
87 if (rc) {
88 dev_err(dev, "Failed enabling %d MSIX entries.\n", msixcnt);
89 goto err_no_irq;
90 }
91 dev_dbg(dev, "Enabled %d msix vectors\n", msixcnt);
92
93 /*
94 * We implement 1 completion list per MSI-X entry except for
95 * entry 0, which is for errors and others.
96 */
97 idxd->irq_entries = devm_kcalloc(dev, msixcnt,
98 sizeof(struct idxd_irq_entry),
99 GFP_KERNEL);
100 if (!idxd->irq_entries) {
101 rc = -ENOMEM;
102 goto err_no_irq;
103 }
104
105 for (i = 0; i < msixcnt; i++) {
106 idxd->irq_entries[i].id = i;
107 idxd->irq_entries[i].idxd = idxd;
Dave Jiange4f4d8c2020-10-27 10:34:40 -0700108 spin_lock_init(&idxd->irq_entries[i].list_lock);
Dave Jiangbfe1d562020-01-21 16:43:59 -0700109 }
110
111 msix = &idxd->msix_entries[0];
112 irq_entry = &idxd->irq_entries[0];
113 rc = devm_request_threaded_irq(dev, msix->vector, idxd_irq_handler,
114 idxd_misc_thread, 0, "idxd-misc",
115 irq_entry);
116 if (rc < 0) {
117 dev_err(dev, "Failed to allocate misc interrupt.\n");
118 goto err_no_irq;
119 }
120
121 dev_dbg(dev, "Allocated idxd-misc handler on msix vector %d\n",
122 msix->vector);
123
124 /* first MSI-X entry is not for wq interrupts */
125 idxd->num_wq_irqs = msixcnt - 1;
126
127 for (i = 1; i < msixcnt; i++) {
128 msix = &idxd->msix_entries[i];
129 irq_entry = &idxd->irq_entries[i];
130
131 init_llist_head(&idxd->irq_entries[i].pending_llist);
132 INIT_LIST_HEAD(&idxd->irq_entries[i].work_list);
133 rc = devm_request_threaded_irq(dev, msix->vector,
134 idxd_irq_handler,
135 idxd_wq_thread, 0,
136 "idxd-portal", irq_entry);
137 if (rc < 0) {
138 dev_err(dev, "Failed to allocate irq %d.\n",
139 msix->vector);
140 goto err_no_irq;
141 }
142 dev_dbg(dev, "Allocated idxd-msix %d for vector %d\n",
143 i, msix->vector);
144 }
145
146 idxd_unmask_error_interrupts(idxd);
147
Dave Jiang8e50d392020-10-27 10:34:35 -0700148 /* Setup MSIX permission table */
149 mperm.bits = 0;
150 mperm.pasid = idxd->pasid;
151 mperm.pasid_en = device_pasid_enabled(idxd);
152 for (i = 1; i < msixcnt; i++)
153 iowrite32(mperm.bits, idxd->reg_base + idxd->msix_perm_offset + i * 8);
154
Dave Jiangbfe1d562020-01-21 16:43:59 -0700155 return 0;
156
157 err_no_irq:
158 /* Disable error interrupt generation */
159 idxd_mask_error_interrupts(idxd);
160 pci_disable_msix(pdev);
161 dev_err(dev, "No usable interrupts\n");
162 return rc;
163}
164
Dave Jiangbfe1d562020-01-21 16:43:59 -0700165static int idxd_setup_internals(struct idxd_device *idxd)
166{
167 struct device *dev = &idxd->pdev->dev;
168 int i;
169
Dave Jiang0d5c10b2020-06-26 11:11:18 -0700170 init_waitqueue_head(&idxd->cmd_waitq);
Dave Jiangbfe1d562020-01-21 16:43:59 -0700171 idxd->groups = devm_kcalloc(dev, idxd->max_groups,
172 sizeof(struct idxd_group), GFP_KERNEL);
173 if (!idxd->groups)
174 return -ENOMEM;
175
176 for (i = 0; i < idxd->max_groups; i++) {
177 idxd->groups[i].idxd = idxd;
178 idxd->groups[i].id = i;
179 idxd->groups[i].tc_a = -1;
180 idxd->groups[i].tc_b = -1;
181 }
182
183 idxd->wqs = devm_kcalloc(dev, idxd->max_wqs, sizeof(struct idxd_wq),
184 GFP_KERNEL);
185 if (!idxd->wqs)
186 return -ENOMEM;
187
188 idxd->engines = devm_kcalloc(dev, idxd->max_engines,
189 sizeof(struct idxd_engine), GFP_KERNEL);
190 if (!idxd->engines)
191 return -ENOMEM;
192
193 for (i = 0; i < idxd->max_wqs; i++) {
194 struct idxd_wq *wq = &idxd->wqs[i];
Dave Jiangbfe1d562020-01-21 16:43:59 -0700195
196 wq->id = i;
197 wq->idxd = idxd;
198 mutex_init(&wq->wq_lock);
Dave Jiang42d279f2020-01-21 16:44:29 -0700199 wq->idxd_cdev.minor = -1;
Dave Jiangd7aad552020-08-28 15:12:10 -0700200 wq->max_xfer_bytes = idxd->max_xfer_bytes;
Dave Jiange7184b12020-08-28 15:12:50 -0700201 wq->max_batch_size = idxd->max_batch_size;
Dave Jiangd98793b2020-10-27 14:34:09 -0700202 wq->wqcfg = devm_kzalloc(dev, idxd->wqcfg_size, GFP_KERNEL);
203 if (!wq->wqcfg)
204 return -ENOMEM;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700205 }
206
207 for (i = 0; i < idxd->max_engines; i++) {
208 idxd->engines[i].idxd = idxd;
209 idxd->engines[i].id = i;
210 }
211
Dave Jiang0d5c10b2020-06-26 11:11:18 -0700212 idxd->wq = create_workqueue(dev_name(dev));
213 if (!idxd->wq)
214 return -ENOMEM;
215
Dave Jiangbfe1d562020-01-21 16:43:59 -0700216 return 0;
217}
218
219static void idxd_read_table_offsets(struct idxd_device *idxd)
220{
221 union offsets_reg offsets;
222 struct device *dev = &idxd->pdev->dev;
223
224 offsets.bits[0] = ioread64(idxd->reg_base + IDXD_TABLE_OFFSET);
Dave Jiang2f8417a2020-10-30 08:51:56 -0700225 offsets.bits[1] = ioread64(idxd->reg_base + IDXD_TABLE_OFFSET + sizeof(u64));
226 idxd->grpcfg_offset = offsets.grpcfg * IDXD_TABLE_MULT;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700227 dev_dbg(dev, "IDXD Group Config Offset: %#x\n", idxd->grpcfg_offset);
Dave Jiang2f8417a2020-10-30 08:51:56 -0700228 idxd->wqcfg_offset = offsets.wqcfg * IDXD_TABLE_MULT;
229 dev_dbg(dev, "IDXD Work Queue Config Offset: %#x\n", idxd->wqcfg_offset);
230 idxd->msix_perm_offset = offsets.msix_perm * IDXD_TABLE_MULT;
231 dev_dbg(dev, "IDXD MSIX Permission Offset: %#x\n", idxd->msix_perm_offset);
232 idxd->perfmon_offset = offsets.perfmon * IDXD_TABLE_MULT;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700233 dev_dbg(dev, "IDXD Perfmon Offset: %#x\n", idxd->perfmon_offset);
234}
235
236static void idxd_read_caps(struct idxd_device *idxd)
237{
238 struct device *dev = &idxd->pdev->dev;
239 int i;
240
241 /* reading generic capabilities */
242 idxd->hw.gen_cap.bits = ioread64(idxd->reg_base + IDXD_GENCAP_OFFSET);
243 dev_dbg(dev, "gen_cap: %#llx\n", idxd->hw.gen_cap.bits);
244 idxd->max_xfer_bytes = 1ULL << idxd->hw.gen_cap.max_xfer_shift;
245 dev_dbg(dev, "max xfer size: %llu bytes\n", idxd->max_xfer_bytes);
246 idxd->max_batch_size = 1U << idxd->hw.gen_cap.max_batch_shift;
247 dev_dbg(dev, "max batch size: %u\n", idxd->max_batch_size);
248 if (idxd->hw.gen_cap.config_en)
249 set_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags);
250
251 /* reading group capabilities */
252 idxd->hw.group_cap.bits =
253 ioread64(idxd->reg_base + IDXD_GRPCAP_OFFSET);
254 dev_dbg(dev, "group_cap: %#llx\n", idxd->hw.group_cap.bits);
255 idxd->max_groups = idxd->hw.group_cap.num_groups;
256 dev_dbg(dev, "max groups: %u\n", idxd->max_groups);
257 idxd->max_tokens = idxd->hw.group_cap.total_tokens;
258 dev_dbg(dev, "max tokens: %u\n", idxd->max_tokens);
Dave Jiangc52ca472020-01-21 16:44:05 -0700259 idxd->nr_tokens = idxd->max_tokens;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700260
261 /* read engine capabilities */
262 idxd->hw.engine_cap.bits =
263 ioread64(idxd->reg_base + IDXD_ENGCAP_OFFSET);
264 dev_dbg(dev, "engine_cap: %#llx\n", idxd->hw.engine_cap.bits);
265 idxd->max_engines = idxd->hw.engine_cap.num_engines;
266 dev_dbg(dev, "max engines: %u\n", idxd->max_engines);
267
268 /* read workqueue capabilities */
269 idxd->hw.wq_cap.bits = ioread64(idxd->reg_base + IDXD_WQCAP_OFFSET);
270 dev_dbg(dev, "wq_cap: %#llx\n", idxd->hw.wq_cap.bits);
271 idxd->max_wq_size = idxd->hw.wq_cap.total_wq_size;
272 dev_dbg(dev, "total workqueue size: %u\n", idxd->max_wq_size);
273 idxd->max_wqs = idxd->hw.wq_cap.num_wqs;
274 dev_dbg(dev, "max workqueues: %u\n", idxd->max_wqs);
Dave Jiangd98793b2020-10-27 14:34:09 -0700275 idxd->wqcfg_size = 1 << (idxd->hw.wq_cap.wqcfg_size + IDXD_WQCFG_MIN);
276 dev_dbg(dev, "wqcfg size: %u\n", idxd->wqcfg_size);
Dave Jiangbfe1d562020-01-21 16:43:59 -0700277
278 /* reading operation capabilities */
279 for (i = 0; i < 4; i++) {
280 idxd->hw.opcap.bits[i] = ioread64(idxd->reg_base +
281 IDXD_OPCAP_OFFSET + i * sizeof(u64));
282 dev_dbg(dev, "opcap[%d]: %#llx\n", i, idxd->hw.opcap.bits[i]);
283 }
284}
285
Dave Jiang8e50d392020-10-27 10:34:35 -0700286static struct idxd_device *idxd_alloc(struct pci_dev *pdev)
Dave Jiangbfe1d562020-01-21 16:43:59 -0700287{
288 struct device *dev = &pdev->dev;
289 struct idxd_device *idxd;
290
291 idxd = devm_kzalloc(dev, sizeof(struct idxd_device), GFP_KERNEL);
292 if (!idxd)
293 return NULL;
294
295 idxd->pdev = pdev;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700296 spin_lock_init(&idxd->dev_lock);
297
298 return idxd;
299}
300
Dave Jiang8e50d392020-10-27 10:34:35 -0700301static int idxd_enable_system_pasid(struct idxd_device *idxd)
302{
303 int flags;
304 unsigned int pasid;
305 struct iommu_sva *sva;
306
307 flags = SVM_FLAG_SUPERVISOR_MODE;
308
309 sva = iommu_sva_bind_device(&idxd->pdev->dev, NULL, &flags);
310 if (IS_ERR(sva)) {
311 dev_warn(&idxd->pdev->dev,
312 "iommu sva bind failed: %ld\n", PTR_ERR(sva));
313 return PTR_ERR(sva);
314 }
315
316 pasid = iommu_sva_get_pasid(sva);
317 if (pasid == IOMMU_PASID_INVALID) {
318 iommu_sva_unbind_device(sva);
319 return -ENODEV;
320 }
321
322 idxd->sva = sva;
323 idxd->pasid = pasid;
324 dev_dbg(&idxd->pdev->dev, "system pasid: %u\n", pasid);
325 return 0;
326}
327
328static void idxd_disable_system_pasid(struct idxd_device *idxd)
329{
330
331 iommu_sva_unbind_device(idxd->sva);
332 idxd->sva = NULL;
333}
334
Dave Jiangbfe1d562020-01-21 16:43:59 -0700335static int idxd_probe(struct idxd_device *idxd)
336{
337 struct pci_dev *pdev = idxd->pdev;
338 struct device *dev = &pdev->dev;
339 int rc;
340
341 dev_dbg(dev, "%s entered and resetting device\n", __func__);
Dave Jiang0d5c10b2020-06-26 11:11:18 -0700342 idxd_device_init_reset(idxd);
Dave Jiangbfe1d562020-01-21 16:43:59 -0700343 dev_dbg(dev, "IDXD reset complete\n");
344
Dave Jiang03d939c2021-01-22 11:46:00 -0700345 if (IS_ENABLED(CONFIG_INTEL_IDXD_SVM) && sva) {
Dave Jiang8e50d392020-10-27 10:34:35 -0700346 rc = idxd_enable_system_pasid(idxd);
347 if (rc < 0)
348 dev_warn(dev, "Failed to enable PASID. No SVA support: %d\n", rc);
349 else
350 set_bit(IDXD_FLAG_PASID_ENABLED, &idxd->flags);
Dave Jiang03d939c2021-01-22 11:46:00 -0700351 } else if (!sva) {
352 dev_warn(dev, "User forced SVA off via module param.\n");
Dave Jiang8e50d392020-10-27 10:34:35 -0700353 }
354
Dave Jiangbfe1d562020-01-21 16:43:59 -0700355 idxd_read_caps(idxd);
356 idxd_read_table_offsets(idxd);
357
358 rc = idxd_setup_internals(idxd);
359 if (rc)
360 goto err_setup;
361
362 rc = idxd_setup_interrupts(idxd);
363 if (rc)
364 goto err_setup;
365
366 dev_dbg(dev, "IDXD interrupt setup complete.\n");
367
368 mutex_lock(&idxd_idr_lock);
369 idxd->id = idr_alloc(&idxd_idrs[idxd->type], idxd, 0, 0, GFP_KERNEL);
370 mutex_unlock(&idxd_idr_lock);
371 if (idxd->id < 0) {
372 rc = -ENOMEM;
373 goto err_idr_fail;
374 }
375
Dave Jiang42d279f2020-01-21 16:44:29 -0700376 idxd->major = idxd_cdev_get_major(idxd);
377
Dave Jiangbfe1d562020-01-21 16:43:59 -0700378 dev_dbg(dev, "IDXD device %d probed successfully\n", idxd->id);
379 return 0;
380
381 err_idr_fail:
382 idxd_mask_error_interrupts(idxd);
383 idxd_mask_msix_vectors(idxd);
384 err_setup:
Dave Jiang8e50d392020-10-27 10:34:35 -0700385 if (device_pasid_enabled(idxd))
386 idxd_disable_system_pasid(idxd);
Dave Jiangbfe1d562020-01-21 16:43:59 -0700387 return rc;
388}
389
Dave Jiangf25b46382020-11-17 13:39:14 -0700390static void idxd_type_init(struct idxd_device *idxd)
391{
392 if (idxd->type == IDXD_TYPE_DSA)
393 idxd->compl_size = sizeof(struct dsa_completion_record);
394 else if (idxd->type == IDXD_TYPE_IAX)
395 idxd->compl_size = sizeof(struct iax_completion_record);
396}
397
Dave Jiangbfe1d562020-01-21 16:43:59 -0700398static int idxd_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
399{
Dave Jiangbfe1d562020-01-21 16:43:59 -0700400 struct device *dev = &pdev->dev;
401 struct idxd_device *idxd;
402 int rc;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700403
404 rc = pcim_enable_device(pdev);
405 if (rc)
406 return rc;
407
Dave Jiang8e50d392020-10-27 10:34:35 -0700408 dev_dbg(dev, "Alloc IDXD context\n");
409 idxd = idxd_alloc(pdev);
410 if (!idxd)
411 return -ENOMEM;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700412
Dave Jiang8e50d392020-10-27 10:34:35 -0700413 dev_dbg(dev, "Mapping BARs\n");
414 idxd->reg_base = pcim_iomap(pdev, IDXD_MMIO_BAR, 0);
415 if (!idxd->reg_base)
Dave Jiangbfe1d562020-01-21 16:43:59 -0700416 return -ENOMEM;
417
418 dev_dbg(dev, "Set DMA masks\n");
419 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
420 if (rc)
421 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
422 if (rc)
423 return rc;
424
425 rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
426 if (rc)
427 rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
428 if (rc)
429 return rc;
430
Dave Jiangbfe1d562020-01-21 16:43:59 -0700431 idxd_set_type(idxd);
432
Dave Jiangf25b46382020-11-17 13:39:14 -0700433 idxd_type_init(idxd);
434
Dave Jiangbfe1d562020-01-21 16:43:59 -0700435 dev_dbg(dev, "Set PCI master\n");
436 pci_set_master(pdev);
437 pci_set_drvdata(pdev, idxd);
438
439 idxd->hw.version = ioread32(idxd->reg_base + IDXD_VER_OFFSET);
440 rc = idxd_probe(idxd);
441 if (rc) {
442 dev_err(dev, "Intel(R) IDXD DMA Engine init failed\n");
443 return -ENODEV;
444 }
445
Dave Jiangc52ca472020-01-21 16:44:05 -0700446 rc = idxd_setup_sysfs(idxd);
447 if (rc) {
448 dev_err(dev, "IDXD sysfs setup failed\n");
449 return -ENODEV;
450 }
451
452 idxd->state = IDXD_DEV_CONF_READY;
453
Dave Jiangbfe1d562020-01-21 16:43:59 -0700454 dev_info(&pdev->dev, "Intel(R) Accelerator Device (v%x)\n",
455 idxd->hw.version);
456
457 return 0;
458}
459
Dave Jiang8f47d1a2020-01-21 16:44:23 -0700460static void idxd_flush_pending_llist(struct idxd_irq_entry *ie)
461{
462 struct idxd_desc *desc, *itr;
463 struct llist_node *head;
464
465 head = llist_del_all(&ie->pending_llist);
466 if (!head)
467 return;
468
469 llist_for_each_entry_safe(desc, itr, head, llnode) {
470 idxd_dma_complete_txd(desc, IDXD_COMPLETE_ABORT);
471 idxd_free_desc(desc->wq, desc);
472 }
473}
474
475static void idxd_flush_work_list(struct idxd_irq_entry *ie)
476{
477 struct idxd_desc *desc, *iter;
478
479 list_for_each_entry_safe(desc, iter, &ie->work_list, list) {
480 list_del(&desc->list);
481 idxd_dma_complete_txd(desc, IDXD_COMPLETE_ABORT);
482 idxd_free_desc(desc->wq, desc);
483 }
484}
485
Dave Jiangbfe1d562020-01-21 16:43:59 -0700486static void idxd_shutdown(struct pci_dev *pdev)
487{
488 struct idxd_device *idxd = pci_get_drvdata(pdev);
489 int rc, i;
490 struct idxd_irq_entry *irq_entry;
491 int msixcnt = pci_msix_vec_count(pdev);
Dave Jiangbfe1d562020-01-21 16:43:59 -0700492
Dave Jiangbfe1d562020-01-21 16:43:59 -0700493 rc = idxd_device_disable(idxd);
Dave Jiangbfe1d562020-01-21 16:43:59 -0700494 if (rc)
495 dev_err(&pdev->dev, "Disabling device failed\n");
496
497 dev_dbg(&pdev->dev, "%s called\n", __func__);
498 idxd_mask_msix_vectors(idxd);
499 idxd_mask_error_interrupts(idxd);
500
501 for (i = 0; i < msixcnt; i++) {
502 irq_entry = &idxd->irq_entries[i];
503 synchronize_irq(idxd->msix_entries[i].vector);
504 if (i == 0)
505 continue;
Dave Jiang8f47d1a2020-01-21 16:44:23 -0700506 idxd_flush_pending_llist(irq_entry);
507 idxd_flush_work_list(irq_entry);
Dave Jiangbfe1d562020-01-21 16:43:59 -0700508 }
Dave Jiang0d5c10b2020-06-26 11:11:18 -0700509
510 destroy_workqueue(idxd->wq);
Dave Jiangbfe1d562020-01-21 16:43:59 -0700511}
512
513static void idxd_remove(struct pci_dev *pdev)
514{
515 struct idxd_device *idxd = pci_get_drvdata(pdev);
516
517 dev_dbg(&pdev->dev, "%s called\n", __func__);
Dave Jiangc52ca472020-01-21 16:44:05 -0700518 idxd_cleanup_sysfs(idxd);
Dave Jiangbfe1d562020-01-21 16:43:59 -0700519 idxd_shutdown(pdev);
Dave Jiang8e50d392020-10-27 10:34:35 -0700520 if (device_pasid_enabled(idxd))
521 idxd_disable_system_pasid(idxd);
Dave Jiangbfe1d562020-01-21 16:43:59 -0700522 mutex_lock(&idxd_idr_lock);
523 idr_remove(&idxd_idrs[idxd->type], idxd->id);
524 mutex_unlock(&idxd_idr_lock);
525}
526
527static struct pci_driver idxd_pci_driver = {
528 .name = DRV_NAME,
529 .id_table = idxd_pci_tbl,
530 .probe = idxd_pci_probe,
531 .remove = idxd_remove,
532 .shutdown = idxd_shutdown,
533};
534
535static int __init idxd_init_module(void)
536{
537 int err, i;
538
539 /*
Dave Jiang8e50d392020-10-27 10:34:35 -0700540 * If the CPU does not support MOVDIR64B or ENQCMDS, there's no point in
Dave Jiangbfe1d562020-01-21 16:43:59 -0700541 * enumerating the device. We can not utilize it.
542 */
543 if (!boot_cpu_has(X86_FEATURE_MOVDIR64B)) {
544 pr_warn("idxd driver failed to load without MOVDIR64B.\n");
545 return -ENODEV;
546 }
547
Dave Jiang8e50d392020-10-27 10:34:35 -0700548 if (!boot_cpu_has(X86_FEATURE_ENQCMD))
549 pr_warn("Platform does not have ENQCMD(S) support.\n");
550 else
551 support_enqcmd = true;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700552
Dave Jiangbfe1d562020-01-21 16:43:59 -0700553 for (i = 0; i < IDXD_TYPE_MAX; i++)
554 idr_init(&idxd_idrs[i]);
555
Dave Jiangc52ca472020-01-21 16:44:05 -0700556 err = idxd_register_bus_type();
557 if (err < 0)
Dave Jiangbfe1d562020-01-21 16:43:59 -0700558 return err;
559
Dave Jiangc52ca472020-01-21 16:44:05 -0700560 err = idxd_register_driver();
561 if (err < 0)
562 goto err_idxd_driver_register;
563
Dave Jiang42d279f2020-01-21 16:44:29 -0700564 err = idxd_cdev_register();
565 if (err)
566 goto err_cdev_register;
567
Dave Jiangc52ca472020-01-21 16:44:05 -0700568 err = pci_register_driver(&idxd_pci_driver);
569 if (err)
570 goto err_pci_register;
571
Dave Jiangbfe1d562020-01-21 16:43:59 -0700572 return 0;
Dave Jiangc52ca472020-01-21 16:44:05 -0700573
574err_pci_register:
Dave Jiang42d279f2020-01-21 16:44:29 -0700575 idxd_cdev_remove();
576err_cdev_register:
Dave Jiangc52ca472020-01-21 16:44:05 -0700577 idxd_unregister_driver();
578err_idxd_driver_register:
579 idxd_unregister_bus_type();
580 return err;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700581}
582module_init(idxd_init_module);
583
584static void __exit idxd_exit_module(void)
585{
586 pci_unregister_driver(&idxd_pci_driver);
Dave Jiang42d279f2020-01-21 16:44:29 -0700587 idxd_cdev_remove();
Dave Jiangc52ca472020-01-21 16:44:05 -0700588 idxd_unregister_bus_type();
Dave Jiangbfe1d562020-01-21 16:43:59 -0700589}
590module_exit(idxd_exit_module);