blob: 9b797fcdfd7bb36c74afaaef914feba7a1065629 [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"
Tom Zanussi0bde4442021-04-24 10:04:16 -050024#include "perfmon.h"
Dave Jiangbfe1d562020-01-21 16:43:59 -070025
26MODULE_VERSION(IDXD_DRIVER_VERSION);
27MODULE_LICENSE("GPL v2");
28MODULE_AUTHOR("Intel Corporation");
Dave Jiangd9e54812021-07-15 11:44:41 -070029MODULE_IMPORT_NS(IDXD);
Dave Jiangbfe1d562020-01-21 16:43:59 -070030
Dave Jiang03d939c2021-01-22 11:46:00 -070031static bool sva = true;
32module_param(sva, bool, 0644);
33MODULE_PARM_DESC(sva, "Toggle SVA support on/off");
34
Dave Jiangbfe1d562020-01-21 16:43:59 -070035#define DRV_NAME "idxd"
36
Dave Jiang8e50d392020-10-27 10:34:35 -070037bool support_enqcmd;
Dave Jiang4b73e4e2021-04-15 16:38:03 -070038DEFINE_IDA(idxd_ida);
Dave Jiangbfe1d562020-01-21 16:43:59 -070039
Dave Jiang435b5122021-04-15 16:38:09 -070040static struct idxd_driver_data idxd_driver_data[] = {
41 [IDXD_TYPE_DSA] = {
42 .name_prefix = "dsa",
43 .type = IDXD_TYPE_DSA,
44 .compl_size = sizeof(struct dsa_completion_record),
45 .align = 32,
46 .dev_type = &dsa_device_type,
47 },
48 [IDXD_TYPE_IAX] = {
49 .name_prefix = "iax",
50 .type = IDXD_TYPE_IAX,
51 .compl_size = sizeof(struct iax_completion_record),
52 .align = 64,
53 .dev_type = &iax_device_type,
54 },
55};
56
Dave Jiangbfe1d562020-01-21 16:43:59 -070057static struct pci_device_id idxd_pci_tbl[] = {
58 /* DSA ver 1.0 platforms */
Dave Jiang435b5122021-04-15 16:38:09 -070059 { PCI_DEVICE_DATA(INTEL, DSA_SPR0, &idxd_driver_data[IDXD_TYPE_DSA]) },
Dave Jiangf25b46382020-11-17 13:39:14 -070060
61 /* IAX ver 1.0 platforms */
Dave Jiang435b5122021-04-15 16:38:09 -070062 { PCI_DEVICE_DATA(INTEL, IAX_SPR0, &idxd_driver_data[IDXD_TYPE_IAX]) },
Dave Jiangbfe1d562020-01-21 16:43:59 -070063 { 0, }
64};
65MODULE_DEVICE_TABLE(pci, idxd_pci_tbl);
66
Dave Jiangbfe1d562020-01-21 16:43:59 -070067static int idxd_setup_interrupts(struct idxd_device *idxd)
68{
69 struct pci_dev *pdev = idxd->pdev;
70 struct device *dev = &pdev->dev;
Dave Jiangbfe1d562020-01-21 16:43:59 -070071 struct idxd_irq_entry *irq_entry;
72 int i, msixcnt;
73 int rc = 0;
74
75 msixcnt = pci_msix_vec_count(pdev);
76 if (msixcnt < 0) {
77 dev_err(dev, "Not MSI-X interrupt capable.\n");
Dave Jiang5fc8e852021-04-15 16:37:15 -070078 return -ENOSPC;
Dave Jiangbfe1d562020-01-21 16:43:59 -070079 }
80
Dave Jiang5fc8e852021-04-15 16:37:15 -070081 rc = pci_alloc_irq_vectors(pdev, msixcnt, msixcnt, PCI_IRQ_MSIX);
82 if (rc != msixcnt) {
83 dev_err(dev, "Failed enabling %d MSIX entries: %d\n", msixcnt, rc);
84 return -ENOSPC;
Dave Jiangbfe1d562020-01-21 16:43:59 -070085 }
86 dev_dbg(dev, "Enabled %d msix vectors\n", msixcnt);
87
88 /*
89 * We implement 1 completion list per MSI-X entry except for
90 * entry 0, which is for errors and others.
91 */
Dave Jiang47c16ac2021-04-15 16:37:33 -070092 idxd->irq_entries = kcalloc_node(msixcnt, sizeof(struct idxd_irq_entry),
93 GFP_KERNEL, dev_to_node(dev));
Dave Jiangbfe1d562020-01-21 16:43:59 -070094 if (!idxd->irq_entries) {
95 rc = -ENOMEM;
Dave Jiang5fc8e852021-04-15 16:37:15 -070096 goto err_irq_entries;
Dave Jiangbfe1d562020-01-21 16:43:59 -070097 }
98
99 for (i = 0; i < msixcnt; i++) {
100 idxd->irq_entries[i].id = i;
101 idxd->irq_entries[i].idxd = idxd;
Dave Jiang5fc8e852021-04-15 16:37:15 -0700102 idxd->irq_entries[i].vector = pci_irq_vector(pdev, i);
Dave Jiange4f4d8c2020-10-27 10:34:40 -0700103 spin_lock_init(&idxd->irq_entries[i].list_lock);
Dave Jiangbfe1d562020-01-21 16:43:59 -0700104 }
105
Dave Jiangd5c10e02021-06-24 13:43:32 -0700106 idxd_msix_perm_setup(idxd);
107
Dave Jiangbfe1d562020-01-21 16:43:59 -0700108 irq_entry = &idxd->irq_entries[0];
Dave Jianga1610462021-04-20 12:00:34 -0700109 rc = request_threaded_irq(irq_entry->vector, NULL, idxd_misc_thread,
Dave Jiang5fc8e852021-04-15 16:37:15 -0700110 0, "idxd-misc", irq_entry);
Dave Jiangbfe1d562020-01-21 16:43:59 -0700111 if (rc < 0) {
112 dev_err(dev, "Failed to allocate misc interrupt.\n");
Dave Jiang5fc8e852021-04-15 16:37:15 -0700113 goto err_misc_irq;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700114 }
115
Dave Jiang5fc8e852021-04-15 16:37:15 -0700116 dev_dbg(dev, "Allocated idxd-misc handler on msix vector %d\n", irq_entry->vector);
Dave Jiangbfe1d562020-01-21 16:43:59 -0700117
118 /* first MSI-X entry is not for wq interrupts */
119 idxd->num_wq_irqs = msixcnt - 1;
120
121 for (i = 1; i < msixcnt; i++) {
Dave Jiangbfe1d562020-01-21 16:43:59 -0700122 irq_entry = &idxd->irq_entries[i];
123
124 init_llist_head(&idxd->irq_entries[i].pending_llist);
125 INIT_LIST_HEAD(&idxd->irq_entries[i].work_list);
Dave Jianga1610462021-04-20 12:00:34 -0700126 rc = request_threaded_irq(irq_entry->vector, NULL,
Dave Jiang5fc8e852021-04-15 16:37:15 -0700127 idxd_wq_thread, 0, "idxd-portal", irq_entry);
Dave Jiangbfe1d562020-01-21 16:43:59 -0700128 if (rc < 0) {
Dave Jiang5fc8e852021-04-15 16:37:15 -0700129 dev_err(dev, "Failed to allocate irq %d.\n", irq_entry->vector);
130 goto err_wq_irqs;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700131 }
Dave Jiangeb15e712021-04-20 11:46:34 -0700132
Dave Jiang5fc8e852021-04-15 16:37:15 -0700133 dev_dbg(dev, "Allocated idxd-msix %d for vector %d\n", i, irq_entry->vector);
Dave Jiangeb15e712021-04-20 11:46:34 -0700134 if (idxd->hw.cmd_cap & BIT(IDXD_CMD_REQUEST_INT_HANDLE)) {
135 /*
136 * The MSIX vector enumeration starts at 1 with vector 0 being the
137 * misc interrupt that handles non I/O completion events. The
138 * interrupt handles are for IMS enumeration on guest. The misc
139 * interrupt vector does not require a handle and therefore we start
140 * the int_handles at index 0. Since 'i' starts at 1, the first
141 * int_handles index will be 0.
142 */
143 rc = idxd_device_request_int_handle(idxd, i, &idxd->int_handles[i - 1],
144 IDXD_IRQ_MSIX);
145 if (rc < 0) {
146 free_irq(irq_entry->vector, irq_entry);
147 goto err_wq_irqs;
148 }
149 dev_dbg(dev, "int handle requested: %u\n", idxd->int_handles[i - 1]);
150 }
Dave Jiangbfe1d562020-01-21 16:43:59 -0700151 }
152
153 idxd_unmask_error_interrupts(idxd);
Dave Jiangbfe1d562020-01-21 16:43:59 -0700154 return 0;
155
Dave Jiang5fc8e852021-04-15 16:37:15 -0700156 err_wq_irqs:
157 while (--i >= 0) {
158 irq_entry = &idxd->irq_entries[i];
159 free_irq(irq_entry->vector, irq_entry);
Dave Jiangeb15e712021-04-20 11:46:34 -0700160 if (i != 0)
161 idxd_device_release_int_handle(idxd,
162 idxd->int_handles[i], IDXD_IRQ_MSIX);
Dave Jiang5fc8e852021-04-15 16:37:15 -0700163 }
164 err_misc_irq:
Dave Jiangbfe1d562020-01-21 16:43:59 -0700165 /* Disable error interrupt generation */
166 idxd_mask_error_interrupts(idxd);
Dave Jiangd5c10e02021-06-24 13:43:32 -0700167 idxd_msix_perm_clear(idxd);
Dave Jiang5fc8e852021-04-15 16:37:15 -0700168 err_irq_entries:
169 pci_free_irq_vectors(pdev);
Dave Jiangbfe1d562020-01-21 16:43:59 -0700170 dev_err(dev, "No usable interrupts\n");
171 return rc;
172}
173
Dave Jiangddf742d2021-05-25 12:23:37 -0700174static void idxd_cleanup_interrupts(struct idxd_device *idxd)
175{
176 struct pci_dev *pdev = idxd->pdev;
177 struct idxd_irq_entry *irq_entry;
178 int i, msixcnt;
179
180 msixcnt = pci_msix_vec_count(pdev);
181 if (msixcnt <= 0)
182 return;
183
184 irq_entry = &idxd->irq_entries[0];
185 free_irq(irq_entry->vector, irq_entry);
186
187 for (i = 1; i < msixcnt; i++) {
188
189 irq_entry = &idxd->irq_entries[i];
190 if (idxd->hw.cmd_cap & BIT(IDXD_CMD_RELEASE_INT_HANDLE))
191 idxd_device_release_int_handle(idxd, idxd->int_handles[i],
192 IDXD_IRQ_MSIX);
193 free_irq(irq_entry->vector, irq_entry);
194 }
195
196 idxd_mask_error_interrupts(idxd);
197 pci_free_irq_vectors(pdev);
198}
199
Dave Jiang7c5dd232021-04-15 16:37:39 -0700200static int idxd_setup_wqs(struct idxd_device *idxd)
201{
202 struct device *dev = &idxd->pdev->dev;
203 struct idxd_wq *wq;
Dave Jiang700af3a2021-07-15 11:43:20 -0700204 struct device *conf_dev;
Dave Jiang7c5dd232021-04-15 16:37:39 -0700205 int i, rc;
206
207 idxd->wqs = kcalloc_node(idxd->max_wqs, sizeof(struct idxd_wq *),
208 GFP_KERNEL, dev_to_node(dev));
209 if (!idxd->wqs)
210 return -ENOMEM;
211
212 for (i = 0; i < idxd->max_wqs; i++) {
213 wq = kzalloc_node(sizeof(*wq), GFP_KERNEL, dev_to_node(dev));
214 if (!wq) {
215 rc = -ENOMEM;
216 goto err;
217 }
218
Dave Jiang700af3a2021-07-15 11:43:20 -0700219 idxd_dev_set_type(&wq->idxd_dev, IDXD_DEV_WQ);
220 conf_dev = wq_confdev(wq);
Dave Jiang7c5dd232021-04-15 16:37:39 -0700221 wq->id = i;
222 wq->idxd = idxd;
Dave Jiang700af3a2021-07-15 11:43:20 -0700223 device_initialize(wq_confdev(wq));
224 conf_dev->parent = idxd_confdev(idxd);
225 conf_dev->bus = &dsa_bus_type;
226 conf_dev->type = &idxd_wq_device_type;
227 rc = dev_set_name(conf_dev, "wq%d.%d", idxd->id, wq->id);
Dave Jiang7c5dd232021-04-15 16:37:39 -0700228 if (rc < 0) {
Dave Jiang700af3a2021-07-15 11:43:20 -0700229 put_device(conf_dev);
Dave Jiang7c5dd232021-04-15 16:37:39 -0700230 goto err;
231 }
232
233 mutex_init(&wq->wq_lock);
Dave Jiang04922b72021-04-15 16:37:57 -0700234 init_waitqueue_head(&wq->err_queue);
Dave Jiang93a40a62021-04-20 11:46:22 -0700235 init_completion(&wq->wq_dead);
Dave Jiang7c5dd232021-04-15 16:37:39 -0700236 wq->max_xfer_bytes = idxd->max_xfer_bytes;
237 wq->max_batch_size = idxd->max_batch_size;
238 wq->wqcfg = kzalloc_node(idxd->wqcfg_size, GFP_KERNEL, dev_to_node(dev));
239 if (!wq->wqcfg) {
Dave Jiang700af3a2021-07-15 11:43:20 -0700240 put_device(conf_dev);
Dave Jiang7c5dd232021-04-15 16:37:39 -0700241 rc = -ENOMEM;
242 goto err;
243 }
244 idxd->wqs[i] = wq;
245 }
246
247 return 0;
248
249 err:
Dave Jiang700af3a2021-07-15 11:43:20 -0700250 while (--i >= 0) {
251 wq = idxd->wqs[i];
252 conf_dev = wq_confdev(wq);
253 put_device(conf_dev);
254 }
Dave Jiang7c5dd232021-04-15 16:37:39 -0700255 return rc;
256}
257
Dave Jiang75b91132021-04-15 16:37:44 -0700258static int idxd_setup_engines(struct idxd_device *idxd)
259{
260 struct idxd_engine *engine;
261 struct device *dev = &idxd->pdev->dev;
Dave Jiang700af3a2021-07-15 11:43:20 -0700262 struct device *conf_dev;
Dave Jiang75b91132021-04-15 16:37:44 -0700263 int i, rc;
264
265 idxd->engines = kcalloc_node(idxd->max_engines, sizeof(struct idxd_engine *),
266 GFP_KERNEL, dev_to_node(dev));
267 if (!idxd->engines)
268 return -ENOMEM;
269
270 for (i = 0; i < idxd->max_engines; i++) {
271 engine = kzalloc_node(sizeof(*engine), GFP_KERNEL, dev_to_node(dev));
272 if (!engine) {
273 rc = -ENOMEM;
274 goto err;
275 }
276
Dave Jiang700af3a2021-07-15 11:43:20 -0700277 idxd_dev_set_type(&engine->idxd_dev, IDXD_DEV_ENGINE);
278 conf_dev = engine_confdev(engine);
Dave Jiang75b91132021-04-15 16:37:44 -0700279 engine->id = i;
280 engine->idxd = idxd;
Dave Jiang700af3a2021-07-15 11:43:20 -0700281 device_initialize(conf_dev);
282 conf_dev->parent = idxd_confdev(idxd);
283 conf_dev->bus = &dsa_bus_type;
284 conf_dev->type = &idxd_engine_device_type;
285 rc = dev_set_name(conf_dev, "engine%d.%d", idxd->id, engine->id);
Dave Jiang75b91132021-04-15 16:37:44 -0700286 if (rc < 0) {
Dave Jiang700af3a2021-07-15 11:43:20 -0700287 put_device(conf_dev);
Dave Jiang75b91132021-04-15 16:37:44 -0700288 goto err;
289 }
290
291 idxd->engines[i] = engine;
292 }
293
294 return 0;
295
296 err:
Dave Jiang700af3a2021-07-15 11:43:20 -0700297 while (--i >= 0) {
298 engine = idxd->engines[i];
299 conf_dev = engine_confdev(engine);
300 put_device(conf_dev);
301 }
Dave Jiang75b91132021-04-15 16:37:44 -0700302 return rc;
303}
304
Dave Jiangdefe49f2021-04-15 16:37:51 -0700305static int idxd_setup_groups(struct idxd_device *idxd)
306{
307 struct device *dev = &idxd->pdev->dev;
Dave Jiang700af3a2021-07-15 11:43:20 -0700308 struct device *conf_dev;
Dave Jiangdefe49f2021-04-15 16:37:51 -0700309 struct idxd_group *group;
310 int i, rc;
311
312 idxd->groups = kcalloc_node(idxd->max_groups, sizeof(struct idxd_group *),
313 GFP_KERNEL, dev_to_node(dev));
314 if (!idxd->groups)
315 return -ENOMEM;
316
317 for (i = 0; i < idxd->max_groups; i++) {
318 group = kzalloc_node(sizeof(*group), GFP_KERNEL, dev_to_node(dev));
319 if (!group) {
320 rc = -ENOMEM;
321 goto err;
322 }
323
Dave Jiang700af3a2021-07-15 11:43:20 -0700324 idxd_dev_set_type(&group->idxd_dev, IDXD_DEV_GROUP);
325 conf_dev = group_confdev(group);
Dave Jiangdefe49f2021-04-15 16:37:51 -0700326 group->id = i;
327 group->idxd = idxd;
Dave Jiang700af3a2021-07-15 11:43:20 -0700328 device_initialize(conf_dev);
329 conf_dev->parent = idxd_confdev(idxd);
330 conf_dev->bus = &dsa_bus_type;
331 conf_dev->type = &idxd_group_device_type;
332 rc = dev_set_name(conf_dev, "group%d.%d", idxd->id, group->id);
Dave Jiangdefe49f2021-04-15 16:37:51 -0700333 if (rc < 0) {
Dave Jiang700af3a2021-07-15 11:43:20 -0700334 put_device(conf_dev);
Dave Jiangdefe49f2021-04-15 16:37:51 -0700335 goto err;
336 }
337
338 idxd->groups[i] = group;
339 group->tc_a = -1;
340 group->tc_b = -1;
341 }
342
343 return 0;
344
345 err:
Dave Jiang700af3a2021-07-15 11:43:20 -0700346 while (--i >= 0) {
347 group = idxd->groups[i];
348 put_device(group_confdev(group));
349 }
Dave Jiangdefe49f2021-04-15 16:37:51 -0700350 return rc;
351}
352
Dave Jiangddf742d2021-05-25 12:23:37 -0700353static void idxd_cleanup_internals(struct idxd_device *idxd)
354{
355 int i;
356
357 for (i = 0; i < idxd->max_groups; i++)
Dave Jiang700af3a2021-07-15 11:43:20 -0700358 put_device(group_confdev(idxd->groups[i]));
Dave Jiangddf742d2021-05-25 12:23:37 -0700359 for (i = 0; i < idxd->max_engines; i++)
Dave Jiang700af3a2021-07-15 11:43:20 -0700360 put_device(engine_confdev(idxd->engines[i]));
Dave Jiangddf742d2021-05-25 12:23:37 -0700361 for (i = 0; i < idxd->max_wqs; i++)
Dave Jiang700af3a2021-07-15 11:43:20 -0700362 put_device(wq_confdev(idxd->wqs[i]));
Dave Jiangddf742d2021-05-25 12:23:37 -0700363 destroy_workqueue(idxd->wq);
364}
365
Dave Jiangbfe1d562020-01-21 16:43:59 -0700366static int idxd_setup_internals(struct idxd_device *idxd)
367{
368 struct device *dev = &idxd->pdev->dev;
Dave Jiangdefe49f2021-04-15 16:37:51 -0700369 int rc, i;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700370
Dave Jiang0d5c10b2020-06-26 11:11:18 -0700371 init_waitqueue_head(&idxd->cmd_waitq);
Dave Jiang7c5dd232021-04-15 16:37:39 -0700372
Dave Jiangeb15e712021-04-20 11:46:34 -0700373 if (idxd->hw.cmd_cap & BIT(IDXD_CMD_REQUEST_INT_HANDLE)) {
Dave Jiang33f9f3c2021-05-09 17:38:25 -0700374 idxd->int_handles = kcalloc_node(idxd->max_wqs, sizeof(int), GFP_KERNEL,
375 dev_to_node(dev));
Dave Jiangeb15e712021-04-20 11:46:34 -0700376 if (!idxd->int_handles)
377 return -ENOMEM;
378 }
379
Dave Jiang7c5dd232021-04-15 16:37:39 -0700380 rc = idxd_setup_wqs(idxd);
381 if (rc < 0)
Dave Jiangeb15e712021-04-20 11:46:34 -0700382 goto err_wqs;
Dave Jiang7c5dd232021-04-15 16:37:39 -0700383
Dave Jiang75b91132021-04-15 16:37:44 -0700384 rc = idxd_setup_engines(idxd);
385 if (rc < 0)
386 goto err_engine;
387
Dave Jiangdefe49f2021-04-15 16:37:51 -0700388 rc = idxd_setup_groups(idxd);
389 if (rc < 0)
390 goto err_group;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700391
Dave Jiang0d5c10b2020-06-26 11:11:18 -0700392 idxd->wq = create_workqueue(dev_name(dev));
Dave Jiang7c5dd232021-04-15 16:37:39 -0700393 if (!idxd->wq) {
394 rc = -ENOMEM;
Dave Jiangdefe49f2021-04-15 16:37:51 -0700395 goto err_wkq_create;
Dave Jiang7c5dd232021-04-15 16:37:39 -0700396 }
Dave Jiang0d5c10b2020-06-26 11:11:18 -0700397
Dave Jiangbfe1d562020-01-21 16:43:59 -0700398 return 0;
Dave Jiang7c5dd232021-04-15 16:37:39 -0700399
Dave Jiangdefe49f2021-04-15 16:37:51 -0700400 err_wkq_create:
401 for (i = 0; i < idxd->max_groups; i++)
Dave Jiang700af3a2021-07-15 11:43:20 -0700402 put_device(group_confdev(idxd->groups[i]));
Dave Jiangdefe49f2021-04-15 16:37:51 -0700403 err_group:
Dave Jiang75b91132021-04-15 16:37:44 -0700404 for (i = 0; i < idxd->max_engines; i++)
Dave Jiang700af3a2021-07-15 11:43:20 -0700405 put_device(engine_confdev(idxd->engines[i]));
Dave Jiang75b91132021-04-15 16:37:44 -0700406 err_engine:
Dave Jiang7c5dd232021-04-15 16:37:39 -0700407 for (i = 0; i < idxd->max_wqs; i++)
Dave Jiang700af3a2021-07-15 11:43:20 -0700408 put_device(wq_confdev(idxd->wqs[i]));
Dave Jiangeb15e712021-04-20 11:46:34 -0700409 err_wqs:
410 kfree(idxd->int_handles);
Dave Jiang7c5dd232021-04-15 16:37:39 -0700411 return rc;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700412}
413
414static void idxd_read_table_offsets(struct idxd_device *idxd)
415{
416 union offsets_reg offsets;
417 struct device *dev = &idxd->pdev->dev;
418
419 offsets.bits[0] = ioread64(idxd->reg_base + IDXD_TABLE_OFFSET);
Dave Jiang2f8417a2020-10-30 08:51:56 -0700420 offsets.bits[1] = ioread64(idxd->reg_base + IDXD_TABLE_OFFSET + sizeof(u64));
421 idxd->grpcfg_offset = offsets.grpcfg * IDXD_TABLE_MULT;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700422 dev_dbg(dev, "IDXD Group Config Offset: %#x\n", idxd->grpcfg_offset);
Dave Jiang2f8417a2020-10-30 08:51:56 -0700423 idxd->wqcfg_offset = offsets.wqcfg * IDXD_TABLE_MULT;
424 dev_dbg(dev, "IDXD Work Queue Config Offset: %#x\n", idxd->wqcfg_offset);
425 idxd->msix_perm_offset = offsets.msix_perm * IDXD_TABLE_MULT;
426 dev_dbg(dev, "IDXD MSIX Permission Offset: %#x\n", idxd->msix_perm_offset);
427 idxd->perfmon_offset = offsets.perfmon * IDXD_TABLE_MULT;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700428 dev_dbg(dev, "IDXD Perfmon Offset: %#x\n", idxd->perfmon_offset);
429}
430
431static void idxd_read_caps(struct idxd_device *idxd)
432{
433 struct device *dev = &idxd->pdev->dev;
434 int i;
435
436 /* reading generic capabilities */
437 idxd->hw.gen_cap.bits = ioread64(idxd->reg_base + IDXD_GENCAP_OFFSET);
438 dev_dbg(dev, "gen_cap: %#llx\n", idxd->hw.gen_cap.bits);
Dave Jiangeb15e712021-04-20 11:46:34 -0700439
440 if (idxd->hw.gen_cap.cmd_cap) {
441 idxd->hw.cmd_cap = ioread32(idxd->reg_base + IDXD_CMDCAP_OFFSET);
442 dev_dbg(dev, "cmd_cap: %#x\n", idxd->hw.cmd_cap);
443 }
444
Dave Jiangbfe1d562020-01-21 16:43:59 -0700445 idxd->max_xfer_bytes = 1ULL << idxd->hw.gen_cap.max_xfer_shift;
446 dev_dbg(dev, "max xfer size: %llu bytes\n", idxd->max_xfer_bytes);
447 idxd->max_batch_size = 1U << idxd->hw.gen_cap.max_batch_shift;
448 dev_dbg(dev, "max batch size: %u\n", idxd->max_batch_size);
449 if (idxd->hw.gen_cap.config_en)
450 set_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags);
451
452 /* reading group capabilities */
453 idxd->hw.group_cap.bits =
454 ioread64(idxd->reg_base + IDXD_GRPCAP_OFFSET);
455 dev_dbg(dev, "group_cap: %#llx\n", idxd->hw.group_cap.bits);
456 idxd->max_groups = idxd->hw.group_cap.num_groups;
457 dev_dbg(dev, "max groups: %u\n", idxd->max_groups);
458 idxd->max_tokens = idxd->hw.group_cap.total_tokens;
459 dev_dbg(dev, "max tokens: %u\n", idxd->max_tokens);
Dave Jiangc52ca472020-01-21 16:44:05 -0700460 idxd->nr_tokens = idxd->max_tokens;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700461
462 /* read engine capabilities */
463 idxd->hw.engine_cap.bits =
464 ioread64(idxd->reg_base + IDXD_ENGCAP_OFFSET);
465 dev_dbg(dev, "engine_cap: %#llx\n", idxd->hw.engine_cap.bits);
466 idxd->max_engines = idxd->hw.engine_cap.num_engines;
467 dev_dbg(dev, "max engines: %u\n", idxd->max_engines);
468
469 /* read workqueue capabilities */
470 idxd->hw.wq_cap.bits = ioread64(idxd->reg_base + IDXD_WQCAP_OFFSET);
471 dev_dbg(dev, "wq_cap: %#llx\n", idxd->hw.wq_cap.bits);
472 idxd->max_wq_size = idxd->hw.wq_cap.total_wq_size;
473 dev_dbg(dev, "total workqueue size: %u\n", idxd->max_wq_size);
474 idxd->max_wqs = idxd->hw.wq_cap.num_wqs;
475 dev_dbg(dev, "max workqueues: %u\n", idxd->max_wqs);
Dave Jiangd98793b2020-10-27 14:34:09 -0700476 idxd->wqcfg_size = 1 << (idxd->hw.wq_cap.wqcfg_size + IDXD_WQCFG_MIN);
477 dev_dbg(dev, "wqcfg size: %u\n", idxd->wqcfg_size);
Dave Jiangbfe1d562020-01-21 16:43:59 -0700478
479 /* reading operation capabilities */
480 for (i = 0; i < 4; i++) {
481 idxd->hw.opcap.bits[i] = ioread64(idxd->reg_base +
482 IDXD_OPCAP_OFFSET + i * sizeof(u64));
483 dev_dbg(dev, "opcap[%d]: %#llx\n", i, idxd->hw.opcap.bits[i]);
484 }
485}
486
Dave Jiang435b5122021-04-15 16:38:09 -0700487static struct idxd_device *idxd_alloc(struct pci_dev *pdev, struct idxd_driver_data *data)
Dave Jiangbfe1d562020-01-21 16:43:59 -0700488{
489 struct device *dev = &pdev->dev;
Dave Jiang700af3a2021-07-15 11:43:20 -0700490 struct device *conf_dev;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700491 struct idxd_device *idxd;
Dave Jiang47c16ac2021-04-15 16:37:33 -0700492 int rc;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700493
Dave Jiang47c16ac2021-04-15 16:37:33 -0700494 idxd = kzalloc_node(sizeof(*idxd), GFP_KERNEL, dev_to_node(dev));
Dave Jiangbfe1d562020-01-21 16:43:59 -0700495 if (!idxd)
496 return NULL;
497
Dave Jiang700af3a2021-07-15 11:43:20 -0700498 conf_dev = idxd_confdev(idxd);
Dave Jiangbfe1d562020-01-21 16:43:59 -0700499 idxd->pdev = pdev;
Dave Jiang435b5122021-04-15 16:38:09 -0700500 idxd->data = data;
Dave Jiang700af3a2021-07-15 11:43:20 -0700501 idxd_dev_set_type(&idxd->idxd_dev, idxd->data->type);
Dave Jiang4b73e4e2021-04-15 16:38:03 -0700502 idxd->id = ida_alloc(&idxd_ida, GFP_KERNEL);
Dave Jiang47c16ac2021-04-15 16:37:33 -0700503 if (idxd->id < 0)
504 return NULL;
505
Dave Jiang700af3a2021-07-15 11:43:20 -0700506 device_initialize(conf_dev);
507 conf_dev->parent = dev;
508 conf_dev->bus = &dsa_bus_type;
509 conf_dev->type = idxd->data->dev_type;
510 rc = dev_set_name(conf_dev, "%s%d", idxd->data->name_prefix, idxd->id);
Dave Jiang47c16ac2021-04-15 16:37:33 -0700511 if (rc < 0) {
Dave Jiang700af3a2021-07-15 11:43:20 -0700512 put_device(conf_dev);
Dave Jiang47c16ac2021-04-15 16:37:33 -0700513 return NULL;
514 }
515
Dave Jiangbfe1d562020-01-21 16:43:59 -0700516 spin_lock_init(&idxd->dev_lock);
Dave Jiang53b2ee7f2021-04-20 12:00:56 -0700517 spin_lock_init(&idxd->cmd_lock);
Dave Jiangbfe1d562020-01-21 16:43:59 -0700518
519 return idxd;
520}
521
Dave Jiang8e50d392020-10-27 10:34:35 -0700522static int idxd_enable_system_pasid(struct idxd_device *idxd)
523{
524 int flags;
525 unsigned int pasid;
526 struct iommu_sva *sva;
527
528 flags = SVM_FLAG_SUPERVISOR_MODE;
529
530 sva = iommu_sva_bind_device(&idxd->pdev->dev, NULL, &flags);
531 if (IS_ERR(sva)) {
532 dev_warn(&idxd->pdev->dev,
533 "iommu sva bind failed: %ld\n", PTR_ERR(sva));
534 return PTR_ERR(sva);
535 }
536
537 pasid = iommu_sva_get_pasid(sva);
538 if (pasid == IOMMU_PASID_INVALID) {
539 iommu_sva_unbind_device(sva);
540 return -ENODEV;
541 }
542
543 idxd->sva = sva;
544 idxd->pasid = pasid;
545 dev_dbg(&idxd->pdev->dev, "system pasid: %u\n", pasid);
546 return 0;
547}
548
549static void idxd_disable_system_pasid(struct idxd_device *idxd)
550{
551
552 iommu_sva_unbind_device(idxd->sva);
553 idxd->sva = NULL;
554}
555
Dave Jiangbfe1d562020-01-21 16:43:59 -0700556static int idxd_probe(struct idxd_device *idxd)
557{
558 struct pci_dev *pdev = idxd->pdev;
559 struct device *dev = &pdev->dev;
560 int rc;
561
562 dev_dbg(dev, "%s entered and resetting device\n", __func__);
Dave Jiang89e3bec2021-02-01 08:26:14 -0700563 rc = idxd_device_init_reset(idxd);
564 if (rc < 0)
565 return rc;
566
Dave Jiangbfe1d562020-01-21 16:43:59 -0700567 dev_dbg(dev, "IDXD reset complete\n");
568
Dave Jiang03d939c2021-01-22 11:46:00 -0700569 if (IS_ENABLED(CONFIG_INTEL_IDXD_SVM) && sva) {
Dave Jiangcf5f86a2021-04-20 11:46:46 -0700570 rc = iommu_dev_enable_feature(dev, IOMMU_DEV_FEAT_SVA);
571 if (rc == 0) {
572 rc = idxd_enable_system_pasid(idxd);
573 if (rc < 0) {
574 iommu_dev_disable_feature(dev, IOMMU_DEV_FEAT_SVA);
575 dev_warn(dev, "Failed to enable PASID. No SVA support: %d\n", rc);
576 } else {
577 set_bit(IDXD_FLAG_PASID_ENABLED, &idxd->flags);
578 }
579 } else {
580 dev_warn(dev, "Unable to turn on SVA feature.\n");
581 }
Dave Jiang03d939c2021-01-22 11:46:00 -0700582 } else if (!sva) {
583 dev_warn(dev, "User forced SVA off via module param.\n");
Dave Jiang8e50d392020-10-27 10:34:35 -0700584 }
585
Dave Jiangbfe1d562020-01-21 16:43:59 -0700586 idxd_read_caps(idxd);
587 idxd_read_table_offsets(idxd);
588
589 rc = idxd_setup_internals(idxd);
590 if (rc)
Dave Jiang7c5dd232021-04-15 16:37:39 -0700591 goto err;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700592
Dave Jiang8c66bbdc2021-04-20 11:46:28 -0700593 /* If the configs are readonly, then load them from device */
594 if (!test_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags)) {
595 dev_dbg(dev, "Loading RO device config\n");
596 rc = idxd_device_load_config(idxd);
597 if (rc < 0)
Dave Jiangddf742d2021-05-25 12:23:37 -0700598 goto err_config;
Dave Jiang8c66bbdc2021-04-20 11:46:28 -0700599 }
600
Dave Jiangbfe1d562020-01-21 16:43:59 -0700601 rc = idxd_setup_interrupts(idxd);
602 if (rc)
Dave Jiangddf742d2021-05-25 12:23:37 -0700603 goto err_config;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700604
605 dev_dbg(dev, "IDXD interrupt setup complete.\n");
606
Dave Jiang42d279f2020-01-21 16:44:29 -0700607 idxd->major = idxd_cdev_get_major(idxd);
608
Tom Zanussi0bde4442021-04-24 10:04:16 -0500609 rc = perfmon_pmu_init(idxd);
610 if (rc < 0)
611 dev_warn(dev, "Failed to initialize perfmon. No PMU support: %d\n", rc);
612
Dave Jiangbfe1d562020-01-21 16:43:59 -0700613 dev_dbg(dev, "IDXD device %d probed successfully\n", idxd->id);
614 return 0;
615
Dave Jiangddf742d2021-05-25 12:23:37 -0700616 err_config:
617 idxd_cleanup_internals(idxd);
Dave Jiang7c5dd232021-04-15 16:37:39 -0700618 err:
Dave Jiang8e50d392020-10-27 10:34:35 -0700619 if (device_pasid_enabled(idxd))
620 idxd_disable_system_pasid(idxd);
Dave Jiangcf5f86a2021-04-20 11:46:46 -0700621 iommu_dev_disable_feature(dev, IOMMU_DEV_FEAT_SVA);
Dave Jiangbfe1d562020-01-21 16:43:59 -0700622 return rc;
623}
624
Dave Jiangddf742d2021-05-25 12:23:37 -0700625static void idxd_cleanup(struct idxd_device *idxd)
626{
627 struct device *dev = &idxd->pdev->dev;
628
629 perfmon_pmu_remove(idxd);
630 idxd_cleanup_interrupts(idxd);
631 idxd_cleanup_internals(idxd);
632 if (device_pasid_enabled(idxd))
633 idxd_disable_system_pasid(idxd);
634 iommu_dev_disable_feature(dev, IOMMU_DEV_FEAT_SVA);
635}
636
Dave Jiangbfe1d562020-01-21 16:43:59 -0700637static int idxd_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
638{
Dave Jiangbfe1d562020-01-21 16:43:59 -0700639 struct device *dev = &pdev->dev;
640 struct idxd_device *idxd;
Dave Jiang435b5122021-04-15 16:38:09 -0700641 struct idxd_driver_data *data = (struct idxd_driver_data *)id->driver_data;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700642 int rc;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700643
Dave Jianga39c7cd2021-04-15 16:37:21 -0700644 rc = pci_enable_device(pdev);
Dave Jiangbfe1d562020-01-21 16:43:59 -0700645 if (rc)
646 return rc;
647
Dave Jiang8e50d392020-10-27 10:34:35 -0700648 dev_dbg(dev, "Alloc IDXD context\n");
Dave Jiang435b5122021-04-15 16:38:09 -0700649 idxd = idxd_alloc(pdev, data);
Dave Jianga39c7cd2021-04-15 16:37:21 -0700650 if (!idxd) {
651 rc = -ENOMEM;
652 goto err_idxd_alloc;
653 }
Dave Jiangbfe1d562020-01-21 16:43:59 -0700654
Dave Jiang8e50d392020-10-27 10:34:35 -0700655 dev_dbg(dev, "Mapping BARs\n");
Dave Jianga39c7cd2021-04-15 16:37:21 -0700656 idxd->reg_base = pci_iomap(pdev, IDXD_MMIO_BAR, 0);
657 if (!idxd->reg_base) {
658 rc = -ENOMEM;
659 goto err_iomap;
660 }
Dave Jiangbfe1d562020-01-21 16:43:59 -0700661
662 dev_dbg(dev, "Set DMA masks\n");
Christophe JAILLET53b50452021-07-08 07:08:26 +0200663 rc = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
Dave Jiangbfe1d562020-01-21 16:43:59 -0700664 if (rc)
Christophe JAILLET53b50452021-07-08 07:08:26 +0200665 rc = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
Dave Jiangbfe1d562020-01-21 16:43:59 -0700666 if (rc)
Dave Jianga39c7cd2021-04-15 16:37:21 -0700667 goto err;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700668
Dave Jiangbfe1d562020-01-21 16:43:59 -0700669 dev_dbg(dev, "Set PCI master\n");
670 pci_set_master(pdev);
671 pci_set_drvdata(pdev, idxd);
672
673 idxd->hw.version = ioread32(idxd->reg_base + IDXD_VER_OFFSET);
674 rc = idxd_probe(idxd);
675 if (rc) {
676 dev_err(dev, "Intel(R) IDXD DMA Engine init failed\n");
Dave Jianga39c7cd2021-04-15 16:37:21 -0700677 goto err;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700678 }
679
Dave Jiang47c16ac2021-04-15 16:37:33 -0700680 rc = idxd_register_devices(idxd);
Dave Jiangc52ca472020-01-21 16:44:05 -0700681 if (rc) {
682 dev_err(dev, "IDXD sysfs setup failed\n");
Dave Jiangddf742d2021-05-25 12:23:37 -0700683 goto err_dev_register;
Dave Jiangc52ca472020-01-21 16:44:05 -0700684 }
685
Dave Jiangbfe1d562020-01-21 16:43:59 -0700686 dev_info(&pdev->dev, "Intel(R) Accelerator Device (v%x)\n",
687 idxd->hw.version);
688
689 return 0;
Dave Jianga39c7cd2021-04-15 16:37:21 -0700690
Dave Jiangddf742d2021-05-25 12:23:37 -0700691 err_dev_register:
692 idxd_cleanup(idxd);
Dave Jianga39c7cd2021-04-15 16:37:21 -0700693 err:
694 pci_iounmap(pdev, idxd->reg_base);
695 err_iomap:
Dave Jiang700af3a2021-07-15 11:43:20 -0700696 put_device(idxd_confdev(idxd));
Dave Jianga39c7cd2021-04-15 16:37:21 -0700697 err_idxd_alloc:
698 pci_disable_device(pdev);
699 return rc;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700700}
701
Dave Jiang8f47d1a2020-01-21 16:44:23 -0700702static void idxd_flush_pending_llist(struct idxd_irq_entry *ie)
703{
704 struct idxd_desc *desc, *itr;
705 struct llist_node *head;
706
707 head = llist_del_all(&ie->pending_llist);
708 if (!head)
709 return;
710
711 llist_for_each_entry_safe(desc, itr, head, llnode) {
712 idxd_dma_complete_txd(desc, IDXD_COMPLETE_ABORT);
713 idxd_free_desc(desc->wq, desc);
714 }
715}
716
717static void idxd_flush_work_list(struct idxd_irq_entry *ie)
718{
719 struct idxd_desc *desc, *iter;
720
721 list_for_each_entry_safe(desc, iter, &ie->work_list, list) {
722 list_del(&desc->list);
723 idxd_dma_complete_txd(desc, IDXD_COMPLETE_ABORT);
724 idxd_free_desc(desc->wq, desc);
725 }
726}
727
Dave Jiang5b0c68c2021-04-20 11:46:51 -0700728void idxd_wqs_quiesce(struct idxd_device *idxd)
729{
730 struct idxd_wq *wq;
731 int i;
732
733 for (i = 0; i < idxd->max_wqs; i++) {
734 wq = idxd->wqs[i];
735 if (wq->state == IDXD_WQ_ENABLED && wq->type == IDXD_WQT_KERNEL)
736 idxd_wq_quiesce(wq);
737 }
738}
739
Dave Jiangeb15e712021-04-20 11:46:34 -0700740static void idxd_release_int_handles(struct idxd_device *idxd)
741{
742 struct device *dev = &idxd->pdev->dev;
743 int i, rc;
744
745 for (i = 0; i < idxd->num_wq_irqs; i++) {
746 if (idxd->hw.cmd_cap & BIT(IDXD_CMD_RELEASE_INT_HANDLE)) {
747 rc = idxd_device_release_int_handle(idxd, idxd->int_handles[i],
748 IDXD_IRQ_MSIX);
749 if (rc < 0)
750 dev_warn(dev, "irq handle %d release failed\n",
751 idxd->int_handles[i]);
752 else
753 dev_dbg(dev, "int handle requested: %u\n", idxd->int_handles[i]);
754 }
755 }
756}
757
Dave Jiangbfe1d562020-01-21 16:43:59 -0700758static void idxd_shutdown(struct pci_dev *pdev)
759{
760 struct idxd_device *idxd = pci_get_drvdata(pdev);
761 int rc, i;
762 struct idxd_irq_entry *irq_entry;
763 int msixcnt = pci_msix_vec_count(pdev);
Dave Jiangbfe1d562020-01-21 16:43:59 -0700764
Dave Jiangbfe1d562020-01-21 16:43:59 -0700765 rc = idxd_device_disable(idxd);
Dave Jiangbfe1d562020-01-21 16:43:59 -0700766 if (rc)
767 dev_err(&pdev->dev, "Disabling device failed\n");
768
769 dev_dbg(&pdev->dev, "%s called\n", __func__);
770 idxd_mask_msix_vectors(idxd);
771 idxd_mask_error_interrupts(idxd);
772
773 for (i = 0; i < msixcnt; i++) {
774 irq_entry = &idxd->irq_entries[i];
Dave Jiang5fc8e852021-04-15 16:37:15 -0700775 synchronize_irq(irq_entry->vector);
Dave Jiangbfe1d562020-01-21 16:43:59 -0700776 if (i == 0)
777 continue;
Dave Jiang8f47d1a2020-01-21 16:44:23 -0700778 idxd_flush_pending_llist(irq_entry);
779 idxd_flush_work_list(irq_entry);
Dave Jiangbfe1d562020-01-21 16:43:59 -0700780 }
Dave Jiang49c49592021-07-14 14:57:19 -0700781 flush_workqueue(idxd->wq);
Dave Jiangbfe1d562020-01-21 16:43:59 -0700782}
783
784static void idxd_remove(struct pci_dev *pdev)
785{
786 struct idxd_device *idxd = pci_get_drvdata(pdev);
Dave Jiang49c49592021-07-14 14:57:19 -0700787 struct idxd_irq_entry *irq_entry;
788 int msixcnt = pci_msix_vec_count(pdev);
789 int i;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700790
791 dev_dbg(&pdev->dev, "%s called\n", __func__);
792 idxd_shutdown(pdev);
Dave Jiang8e50d392020-10-27 10:34:35 -0700793 if (device_pasid_enabled(idxd))
794 idxd_disable_system_pasid(idxd);
Dave Jiang47c16ac2021-04-15 16:37:33 -0700795 idxd_unregister_devices(idxd);
Dave Jiang49c49592021-07-14 14:57:19 -0700796
797 for (i = 0; i < msixcnt; i++) {
798 irq_entry = &idxd->irq_entries[i];
799 free_irq(irq_entry->vector, irq_entry);
800 }
801 idxd_msix_perm_clear(idxd);
802 idxd_release_int_handles(idxd);
803 pci_free_irq_vectors(pdev);
804 pci_iounmap(pdev, idxd->reg_base);
Dave Jiangcf5f86a2021-04-20 11:46:46 -0700805 iommu_dev_disable_feature(&pdev->dev, IOMMU_DEV_FEAT_SVA);
Dave Jiang49c49592021-07-14 14:57:19 -0700806 pci_disable_device(pdev);
807 destroy_workqueue(idxd->wq);
808 perfmon_pmu_remove(idxd);
Dave Jiang700af3a2021-07-15 11:43:20 -0700809 device_unregister(idxd_confdev(idxd));
Dave Jiangbfe1d562020-01-21 16:43:59 -0700810}
811
812static struct pci_driver idxd_pci_driver = {
813 .name = DRV_NAME,
814 .id_table = idxd_pci_tbl,
815 .probe = idxd_pci_probe,
816 .remove = idxd_remove,
817 .shutdown = idxd_shutdown,
818};
819
820static int __init idxd_init_module(void)
821{
Dave Jiang4b73e4e2021-04-15 16:38:03 -0700822 int err;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700823
824 /*
Dave Jiang8e50d392020-10-27 10:34:35 -0700825 * If the CPU does not support MOVDIR64B or ENQCMDS, there's no point in
Dave Jiangbfe1d562020-01-21 16:43:59 -0700826 * enumerating the device. We can not utilize it.
827 */
Borislav Petkov74b2fc82021-06-02 12:07:52 +0200828 if (!cpu_feature_enabled(X86_FEATURE_MOVDIR64B)) {
Dave Jiangbfe1d562020-01-21 16:43:59 -0700829 pr_warn("idxd driver failed to load without MOVDIR64B.\n");
830 return -ENODEV;
831 }
832
Borislav Petkov74b2fc82021-06-02 12:07:52 +0200833 if (!cpu_feature_enabled(X86_FEATURE_ENQCMD))
Dave Jiang8e50d392020-10-27 10:34:35 -0700834 pr_warn("Platform does not have ENQCMD(S) support.\n");
835 else
836 support_enqcmd = true;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700837
Tom Zanussi0bde4442021-04-24 10:04:16 -0500838 perfmon_init();
839
Dave Jiang034b3292021-07-15 11:44:24 -0700840 err = idxd_driver_register(&idxd_drv);
841 if (err < 0)
842 goto err_idxd_driver_register;
843
Dave Jiang0cda4f62021-07-15 11:44:30 -0700844 err = idxd_driver_register(&idxd_dmaengine_drv);
845 if (err < 0)
846 goto err_idxd_dmaengine_driver_register;
847
Dave Jiang448c3de2021-07-15 11:44:35 -0700848 err = idxd_driver_register(&idxd_user_drv);
849 if (err < 0)
850 goto err_idxd_user_driver_register;
851
Dave Jiangc05257b2021-07-15 11:44:13 -0700852 err = idxd_driver_register(&dsa_drv);
Dave Jiangc52ca472020-01-21 16:44:05 -0700853 if (err < 0)
Dave Jiangc05257b2021-07-15 11:44:13 -0700854 goto err_dsa_driver_register;
Dave Jiangc52ca472020-01-21 16:44:05 -0700855
Dave Jiang42d279f2020-01-21 16:44:29 -0700856 err = idxd_cdev_register();
857 if (err)
858 goto err_cdev_register;
859
Dave Jiangc52ca472020-01-21 16:44:05 -0700860 err = pci_register_driver(&idxd_pci_driver);
861 if (err)
862 goto err_pci_register;
863
Dave Jiangbfe1d562020-01-21 16:43:59 -0700864 return 0;
Dave Jiangc52ca472020-01-21 16:44:05 -0700865
866err_pci_register:
Dave Jiang42d279f2020-01-21 16:44:29 -0700867 idxd_cdev_remove();
868err_cdev_register:
Dave Jiangc05257b2021-07-15 11:44:13 -0700869 idxd_driver_unregister(&dsa_drv);
870err_dsa_driver_register:
Dave Jiang448c3de2021-07-15 11:44:35 -0700871 idxd_driver_unregister(&idxd_user_drv);
872err_idxd_user_driver_register:
Dave Jiang0cda4f62021-07-15 11:44:30 -0700873 idxd_driver_unregister(&idxd_dmaengine_drv);
874err_idxd_dmaengine_driver_register:
Dave Jiang034b3292021-07-15 11:44:24 -0700875 idxd_driver_unregister(&idxd_drv);
876err_idxd_driver_register:
Dave Jiangc52ca472020-01-21 16:44:05 -0700877 return err;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700878}
879module_init(idxd_init_module);
880
881static void __exit idxd_exit_module(void)
882{
Dave Jiang448c3de2021-07-15 11:44:35 -0700883 idxd_driver_unregister(&idxd_user_drv);
Dave Jiang0cda4f62021-07-15 11:44:30 -0700884 idxd_driver_unregister(&idxd_dmaengine_drv);
Dave Jiang034b3292021-07-15 11:44:24 -0700885 idxd_driver_unregister(&idxd_drv);
Dave Jiangc05257b2021-07-15 11:44:13 -0700886 idxd_driver_unregister(&dsa_drv);
Dave Jiangbfe1d562020-01-21 16:43:59 -0700887 pci_unregister_driver(&idxd_pci_driver);
Dave Jiang42d279f2020-01-21 16:44:29 -0700888 idxd_cdev_remove();
Tom Zanussi0bde4442021-04-24 10:04:16 -0500889 perfmon_exit();
Dave Jiangbfe1d562020-01-21 16:43:59 -0700890}
891module_exit(idxd_exit_module);