blob: 7bf03f371ce191c6bc6cf05d226d4afec912e926 [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 Jiangade8a862021-07-20 13:42:10 -070035bool tc_override;
36module_param(tc_override, bool, 0644);
37MODULE_PARM_DESC(tc_override, "Override traffic class defaults");
38
Dave Jiangbfe1d562020-01-21 16:43:59 -070039#define DRV_NAME "idxd"
40
Dave Jiang8e50d392020-10-27 10:34:35 -070041bool support_enqcmd;
Dave Jiang4b73e4e2021-04-15 16:38:03 -070042DEFINE_IDA(idxd_ida);
Dave Jiangbfe1d562020-01-21 16:43:59 -070043
Dave Jiang435b5122021-04-15 16:38:09 -070044static struct idxd_driver_data idxd_driver_data[] = {
45 [IDXD_TYPE_DSA] = {
46 .name_prefix = "dsa",
47 .type = IDXD_TYPE_DSA,
48 .compl_size = sizeof(struct dsa_completion_record),
49 .align = 32,
50 .dev_type = &dsa_device_type,
51 },
52 [IDXD_TYPE_IAX] = {
53 .name_prefix = "iax",
54 .type = IDXD_TYPE_IAX,
55 .compl_size = sizeof(struct iax_completion_record),
56 .align = 64,
57 .dev_type = &iax_device_type,
58 },
59};
60
Dave Jiangbfe1d562020-01-21 16:43:59 -070061static struct pci_device_id idxd_pci_tbl[] = {
62 /* DSA ver 1.0 platforms */
Dave Jiang435b5122021-04-15 16:38:09 -070063 { PCI_DEVICE_DATA(INTEL, DSA_SPR0, &idxd_driver_data[IDXD_TYPE_DSA]) },
Dave Jiangf25b46382020-11-17 13:39:14 -070064
65 /* IAX ver 1.0 platforms */
Dave Jiang435b5122021-04-15 16:38:09 -070066 { PCI_DEVICE_DATA(INTEL, IAX_SPR0, &idxd_driver_data[IDXD_TYPE_IAX]) },
Dave Jiangbfe1d562020-01-21 16:43:59 -070067 { 0, }
68};
69MODULE_DEVICE_TABLE(pci, idxd_pci_tbl);
70
Dave Jiangbfe1d562020-01-21 16:43:59 -070071static int idxd_setup_interrupts(struct idxd_device *idxd)
72{
73 struct pci_dev *pdev = idxd->pdev;
74 struct device *dev = &pdev->dev;
Dave Jiangbfe1d562020-01-21 16:43:59 -070075 struct idxd_irq_entry *irq_entry;
76 int i, msixcnt;
77 int rc = 0;
78
79 msixcnt = pci_msix_vec_count(pdev);
80 if (msixcnt < 0) {
81 dev_err(dev, "Not MSI-X interrupt capable.\n");
Dave Jiang5fc8e852021-04-15 16:37:15 -070082 return -ENOSPC;
Dave Jiangbfe1d562020-01-21 16:43:59 -070083 }
84
Dave Jiang5fc8e852021-04-15 16:37:15 -070085 rc = pci_alloc_irq_vectors(pdev, msixcnt, msixcnt, PCI_IRQ_MSIX);
86 if (rc != msixcnt) {
87 dev_err(dev, "Failed enabling %d MSIX entries: %d\n", msixcnt, rc);
88 return -ENOSPC;
Dave Jiangbfe1d562020-01-21 16:43:59 -070089 }
90 dev_dbg(dev, "Enabled %d msix vectors\n", msixcnt);
91
92 /*
93 * We implement 1 completion list per MSI-X entry except for
94 * entry 0, which is for errors and others.
95 */
Dave Jiang47c16ac2021-04-15 16:37:33 -070096 idxd->irq_entries = kcalloc_node(msixcnt, sizeof(struct idxd_irq_entry),
97 GFP_KERNEL, dev_to_node(dev));
Dave Jiangbfe1d562020-01-21 16:43:59 -070098 if (!idxd->irq_entries) {
99 rc = -ENOMEM;
Dave Jiang5fc8e852021-04-15 16:37:15 -0700100 goto err_irq_entries;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700101 }
102
103 for (i = 0; i < msixcnt; i++) {
104 idxd->irq_entries[i].id = i;
105 idxd->irq_entries[i].idxd = idxd;
Dave Jiang5fc8e852021-04-15 16:37:15 -0700106 idxd->irq_entries[i].vector = pci_irq_vector(pdev, i);
Dave Jiange4f4d8c2020-10-27 10:34:40 -0700107 spin_lock_init(&idxd->irq_entries[i].list_lock);
Dave Jiangbfe1d562020-01-21 16:43:59 -0700108 }
109
Dave Jiangd5c10e02021-06-24 13:43:32 -0700110 idxd_msix_perm_setup(idxd);
111
Dave Jiangbfe1d562020-01-21 16:43:59 -0700112 irq_entry = &idxd->irq_entries[0];
Dave Jianga1610462021-04-20 12:00:34 -0700113 rc = request_threaded_irq(irq_entry->vector, NULL, idxd_misc_thread,
Dave Jiang5fc8e852021-04-15 16:37:15 -0700114 0, "idxd-misc", irq_entry);
Dave Jiangbfe1d562020-01-21 16:43:59 -0700115 if (rc < 0) {
116 dev_err(dev, "Failed to allocate misc interrupt.\n");
Dave Jiang5fc8e852021-04-15 16:37:15 -0700117 goto err_misc_irq;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700118 }
119
Dave Jiang5fc8e852021-04-15 16:37:15 -0700120 dev_dbg(dev, "Allocated idxd-misc handler on msix vector %d\n", irq_entry->vector);
Dave Jiangbfe1d562020-01-21 16:43:59 -0700121
122 /* first MSI-X entry is not for wq interrupts */
123 idxd->num_wq_irqs = msixcnt - 1;
124
125 for (i = 1; i < msixcnt; i++) {
Dave Jiangbfe1d562020-01-21 16:43:59 -0700126 irq_entry = &idxd->irq_entries[i];
127
128 init_llist_head(&idxd->irq_entries[i].pending_llist);
129 INIT_LIST_HEAD(&idxd->irq_entries[i].work_list);
Dave Jianga1610462021-04-20 12:00:34 -0700130 rc = request_threaded_irq(irq_entry->vector, NULL,
Dave Jiang5fc8e852021-04-15 16:37:15 -0700131 idxd_wq_thread, 0, "idxd-portal", irq_entry);
Dave Jiangbfe1d562020-01-21 16:43:59 -0700132 if (rc < 0) {
Dave Jiang5fc8e852021-04-15 16:37:15 -0700133 dev_err(dev, "Failed to allocate irq %d.\n", irq_entry->vector);
134 goto err_wq_irqs;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700135 }
Dave Jiangeb15e712021-04-20 11:46:34 -0700136
Dave Jiang5fc8e852021-04-15 16:37:15 -0700137 dev_dbg(dev, "Allocated idxd-msix %d for vector %d\n", i, irq_entry->vector);
Dave Jiangeb15e712021-04-20 11:46:34 -0700138 if (idxd->hw.cmd_cap & BIT(IDXD_CMD_REQUEST_INT_HANDLE)) {
139 /*
140 * The MSIX vector enumeration starts at 1 with vector 0 being the
141 * misc interrupt that handles non I/O completion events. The
142 * interrupt handles are for IMS enumeration on guest. The misc
143 * interrupt vector does not require a handle and therefore we start
144 * the int_handles at index 0. Since 'i' starts at 1, the first
145 * int_handles index will be 0.
146 */
147 rc = idxd_device_request_int_handle(idxd, i, &idxd->int_handles[i - 1],
148 IDXD_IRQ_MSIX);
149 if (rc < 0) {
150 free_irq(irq_entry->vector, irq_entry);
151 goto err_wq_irqs;
152 }
153 dev_dbg(dev, "int handle requested: %u\n", idxd->int_handles[i - 1]);
154 }
Dave Jiangbfe1d562020-01-21 16:43:59 -0700155 }
156
157 idxd_unmask_error_interrupts(idxd);
Dave Jiangbfe1d562020-01-21 16:43:59 -0700158 return 0;
159
Dave Jiang5fc8e852021-04-15 16:37:15 -0700160 err_wq_irqs:
161 while (--i >= 0) {
162 irq_entry = &idxd->irq_entries[i];
163 free_irq(irq_entry->vector, irq_entry);
Dave Jiangeb15e712021-04-20 11:46:34 -0700164 if (i != 0)
165 idxd_device_release_int_handle(idxd,
166 idxd->int_handles[i], IDXD_IRQ_MSIX);
Dave Jiang5fc8e852021-04-15 16:37:15 -0700167 }
168 err_misc_irq:
Dave Jiangbfe1d562020-01-21 16:43:59 -0700169 /* Disable error interrupt generation */
170 idxd_mask_error_interrupts(idxd);
Dave Jiangd5c10e02021-06-24 13:43:32 -0700171 idxd_msix_perm_clear(idxd);
Dave Jiang5fc8e852021-04-15 16:37:15 -0700172 err_irq_entries:
173 pci_free_irq_vectors(pdev);
Dave Jiangbfe1d562020-01-21 16:43:59 -0700174 dev_err(dev, "No usable interrupts\n");
175 return rc;
176}
177
Dave Jiangddf742d2021-05-25 12:23:37 -0700178static void idxd_cleanup_interrupts(struct idxd_device *idxd)
179{
180 struct pci_dev *pdev = idxd->pdev;
181 struct idxd_irq_entry *irq_entry;
182 int i, msixcnt;
183
184 msixcnt = pci_msix_vec_count(pdev);
185 if (msixcnt <= 0)
186 return;
187
188 irq_entry = &idxd->irq_entries[0];
189 free_irq(irq_entry->vector, irq_entry);
190
191 for (i = 1; i < msixcnt; i++) {
192
193 irq_entry = &idxd->irq_entries[i];
194 if (idxd->hw.cmd_cap & BIT(IDXD_CMD_RELEASE_INT_HANDLE))
195 idxd_device_release_int_handle(idxd, idxd->int_handles[i],
196 IDXD_IRQ_MSIX);
197 free_irq(irq_entry->vector, irq_entry);
198 }
199
200 idxd_mask_error_interrupts(idxd);
201 pci_free_irq_vectors(pdev);
202}
203
Dave Jiang7c5dd232021-04-15 16:37:39 -0700204static int idxd_setup_wqs(struct idxd_device *idxd)
205{
206 struct device *dev = &idxd->pdev->dev;
207 struct idxd_wq *wq;
Dave Jiang700af3a2021-07-15 11:43:20 -0700208 struct device *conf_dev;
Dave Jiang7c5dd232021-04-15 16:37:39 -0700209 int i, rc;
210
211 idxd->wqs = kcalloc_node(idxd->max_wqs, sizeof(struct idxd_wq *),
212 GFP_KERNEL, dev_to_node(dev));
213 if (!idxd->wqs)
214 return -ENOMEM;
215
216 for (i = 0; i < idxd->max_wqs; i++) {
217 wq = kzalloc_node(sizeof(*wq), GFP_KERNEL, dev_to_node(dev));
218 if (!wq) {
219 rc = -ENOMEM;
220 goto err;
221 }
222
Dave Jiang700af3a2021-07-15 11:43:20 -0700223 idxd_dev_set_type(&wq->idxd_dev, IDXD_DEV_WQ);
224 conf_dev = wq_confdev(wq);
Dave Jiang7c5dd232021-04-15 16:37:39 -0700225 wq->id = i;
226 wq->idxd = idxd;
Dave Jiang700af3a2021-07-15 11:43:20 -0700227 device_initialize(wq_confdev(wq));
228 conf_dev->parent = idxd_confdev(idxd);
229 conf_dev->bus = &dsa_bus_type;
230 conf_dev->type = &idxd_wq_device_type;
231 rc = dev_set_name(conf_dev, "wq%d.%d", idxd->id, wq->id);
Dave Jiang7c5dd232021-04-15 16:37:39 -0700232 if (rc < 0) {
Dave Jiang700af3a2021-07-15 11:43:20 -0700233 put_device(conf_dev);
Dave Jiang7c5dd232021-04-15 16:37:39 -0700234 goto err;
235 }
236
237 mutex_init(&wq->wq_lock);
Dave Jiang04922b72021-04-15 16:37:57 -0700238 init_waitqueue_head(&wq->err_queue);
Dave Jiang93a40a62021-04-20 11:46:22 -0700239 init_completion(&wq->wq_dead);
Dave Jiang7c5dd232021-04-15 16:37:39 -0700240 wq->max_xfer_bytes = idxd->max_xfer_bytes;
241 wq->max_batch_size = idxd->max_batch_size;
242 wq->wqcfg = kzalloc_node(idxd->wqcfg_size, GFP_KERNEL, dev_to_node(dev));
243 if (!wq->wqcfg) {
Dave Jiang700af3a2021-07-15 11:43:20 -0700244 put_device(conf_dev);
Dave Jiang7c5dd232021-04-15 16:37:39 -0700245 rc = -ENOMEM;
246 goto err;
247 }
248 idxd->wqs[i] = wq;
249 }
250
251 return 0;
252
253 err:
Dave Jiang700af3a2021-07-15 11:43:20 -0700254 while (--i >= 0) {
255 wq = idxd->wqs[i];
256 conf_dev = wq_confdev(wq);
257 put_device(conf_dev);
258 }
Dave Jiang7c5dd232021-04-15 16:37:39 -0700259 return rc;
260}
261
Dave Jiang75b91132021-04-15 16:37:44 -0700262static int idxd_setup_engines(struct idxd_device *idxd)
263{
264 struct idxd_engine *engine;
265 struct device *dev = &idxd->pdev->dev;
Dave Jiang700af3a2021-07-15 11:43:20 -0700266 struct device *conf_dev;
Dave Jiang75b91132021-04-15 16:37:44 -0700267 int i, rc;
268
269 idxd->engines = kcalloc_node(idxd->max_engines, sizeof(struct idxd_engine *),
270 GFP_KERNEL, dev_to_node(dev));
271 if (!idxd->engines)
272 return -ENOMEM;
273
274 for (i = 0; i < idxd->max_engines; i++) {
275 engine = kzalloc_node(sizeof(*engine), GFP_KERNEL, dev_to_node(dev));
276 if (!engine) {
277 rc = -ENOMEM;
278 goto err;
279 }
280
Dave Jiang700af3a2021-07-15 11:43:20 -0700281 idxd_dev_set_type(&engine->idxd_dev, IDXD_DEV_ENGINE);
282 conf_dev = engine_confdev(engine);
Dave Jiang75b91132021-04-15 16:37:44 -0700283 engine->id = i;
284 engine->idxd = idxd;
Dave Jiang700af3a2021-07-15 11:43:20 -0700285 device_initialize(conf_dev);
286 conf_dev->parent = idxd_confdev(idxd);
287 conf_dev->bus = &dsa_bus_type;
288 conf_dev->type = &idxd_engine_device_type;
289 rc = dev_set_name(conf_dev, "engine%d.%d", idxd->id, engine->id);
Dave Jiang75b91132021-04-15 16:37:44 -0700290 if (rc < 0) {
Dave Jiang700af3a2021-07-15 11:43:20 -0700291 put_device(conf_dev);
Dave Jiang75b91132021-04-15 16:37:44 -0700292 goto err;
293 }
294
295 idxd->engines[i] = engine;
296 }
297
298 return 0;
299
300 err:
Dave Jiang700af3a2021-07-15 11:43:20 -0700301 while (--i >= 0) {
302 engine = idxd->engines[i];
303 conf_dev = engine_confdev(engine);
304 put_device(conf_dev);
305 }
Dave Jiang75b91132021-04-15 16:37:44 -0700306 return rc;
307}
308
Dave Jiangdefe49f2021-04-15 16:37:51 -0700309static int idxd_setup_groups(struct idxd_device *idxd)
310{
311 struct device *dev = &idxd->pdev->dev;
Dave Jiang700af3a2021-07-15 11:43:20 -0700312 struct device *conf_dev;
Dave Jiangdefe49f2021-04-15 16:37:51 -0700313 struct idxd_group *group;
314 int i, rc;
315
316 idxd->groups = kcalloc_node(idxd->max_groups, sizeof(struct idxd_group *),
317 GFP_KERNEL, dev_to_node(dev));
318 if (!idxd->groups)
319 return -ENOMEM;
320
321 for (i = 0; i < idxd->max_groups; i++) {
322 group = kzalloc_node(sizeof(*group), GFP_KERNEL, dev_to_node(dev));
323 if (!group) {
324 rc = -ENOMEM;
325 goto err;
326 }
327
Dave Jiang700af3a2021-07-15 11:43:20 -0700328 idxd_dev_set_type(&group->idxd_dev, IDXD_DEV_GROUP);
329 conf_dev = group_confdev(group);
Dave Jiangdefe49f2021-04-15 16:37:51 -0700330 group->id = i;
331 group->idxd = idxd;
Dave Jiang700af3a2021-07-15 11:43:20 -0700332 device_initialize(conf_dev);
333 conf_dev->parent = idxd_confdev(idxd);
334 conf_dev->bus = &dsa_bus_type;
335 conf_dev->type = &idxd_group_device_type;
336 rc = dev_set_name(conf_dev, "group%d.%d", idxd->id, group->id);
Dave Jiangdefe49f2021-04-15 16:37:51 -0700337 if (rc < 0) {
Dave Jiang700af3a2021-07-15 11:43:20 -0700338 put_device(conf_dev);
Dave Jiangdefe49f2021-04-15 16:37:51 -0700339 goto err;
340 }
341
342 idxd->groups[i] = group;
Dave Jiangade8a862021-07-20 13:42:10 -0700343 if (idxd->hw.version < DEVICE_VERSION_2 && !tc_override) {
344 group->tc_a = 1;
345 group->tc_b = 1;
346 } else {
347 group->tc_a = -1;
348 group->tc_b = -1;
349 }
Dave Jiangdefe49f2021-04-15 16:37:51 -0700350 }
351
352 return 0;
353
354 err:
Dave Jiang700af3a2021-07-15 11:43:20 -0700355 while (--i >= 0) {
356 group = idxd->groups[i];
357 put_device(group_confdev(group));
358 }
Dave Jiangdefe49f2021-04-15 16:37:51 -0700359 return rc;
360}
361
Dave Jiangddf742d2021-05-25 12:23:37 -0700362static void idxd_cleanup_internals(struct idxd_device *idxd)
363{
364 int i;
365
366 for (i = 0; i < idxd->max_groups; i++)
Dave Jiang700af3a2021-07-15 11:43:20 -0700367 put_device(group_confdev(idxd->groups[i]));
Dave Jiangddf742d2021-05-25 12:23:37 -0700368 for (i = 0; i < idxd->max_engines; i++)
Dave Jiang700af3a2021-07-15 11:43:20 -0700369 put_device(engine_confdev(idxd->engines[i]));
Dave Jiangddf742d2021-05-25 12:23:37 -0700370 for (i = 0; i < idxd->max_wqs; i++)
Dave Jiang700af3a2021-07-15 11:43:20 -0700371 put_device(wq_confdev(idxd->wqs[i]));
Dave Jiangddf742d2021-05-25 12:23:37 -0700372 destroy_workqueue(idxd->wq);
373}
374
Dave Jiangbfe1d562020-01-21 16:43:59 -0700375static int idxd_setup_internals(struct idxd_device *idxd)
376{
377 struct device *dev = &idxd->pdev->dev;
Dave Jiangdefe49f2021-04-15 16:37:51 -0700378 int rc, i;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700379
Dave Jiang0d5c10b2020-06-26 11:11:18 -0700380 init_waitqueue_head(&idxd->cmd_waitq);
Dave Jiang7c5dd232021-04-15 16:37:39 -0700381
Dave Jiangeb15e712021-04-20 11:46:34 -0700382 if (idxd->hw.cmd_cap & BIT(IDXD_CMD_REQUEST_INT_HANDLE)) {
Dave Jiang33f9f3c2021-05-09 17:38:25 -0700383 idxd->int_handles = kcalloc_node(idxd->max_wqs, sizeof(int), GFP_KERNEL,
384 dev_to_node(dev));
Dave Jiangeb15e712021-04-20 11:46:34 -0700385 if (!idxd->int_handles)
386 return -ENOMEM;
387 }
388
Dave Jiang7c5dd232021-04-15 16:37:39 -0700389 rc = idxd_setup_wqs(idxd);
390 if (rc < 0)
Dave Jiangeb15e712021-04-20 11:46:34 -0700391 goto err_wqs;
Dave Jiang7c5dd232021-04-15 16:37:39 -0700392
Dave Jiang75b91132021-04-15 16:37:44 -0700393 rc = idxd_setup_engines(idxd);
394 if (rc < 0)
395 goto err_engine;
396
Dave Jiangdefe49f2021-04-15 16:37:51 -0700397 rc = idxd_setup_groups(idxd);
398 if (rc < 0)
399 goto err_group;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700400
Dave Jiang0d5c10b2020-06-26 11:11:18 -0700401 idxd->wq = create_workqueue(dev_name(dev));
Dave Jiang7c5dd232021-04-15 16:37:39 -0700402 if (!idxd->wq) {
403 rc = -ENOMEM;
Dave Jiangdefe49f2021-04-15 16:37:51 -0700404 goto err_wkq_create;
Dave Jiang7c5dd232021-04-15 16:37:39 -0700405 }
Dave Jiang0d5c10b2020-06-26 11:11:18 -0700406
Dave Jiangbfe1d562020-01-21 16:43:59 -0700407 return 0;
Dave Jiang7c5dd232021-04-15 16:37:39 -0700408
Dave Jiangdefe49f2021-04-15 16:37:51 -0700409 err_wkq_create:
410 for (i = 0; i < idxd->max_groups; i++)
Dave Jiang700af3a2021-07-15 11:43:20 -0700411 put_device(group_confdev(idxd->groups[i]));
Dave Jiangdefe49f2021-04-15 16:37:51 -0700412 err_group:
Dave Jiang75b91132021-04-15 16:37:44 -0700413 for (i = 0; i < idxd->max_engines; i++)
Dave Jiang700af3a2021-07-15 11:43:20 -0700414 put_device(engine_confdev(idxd->engines[i]));
Dave Jiang75b91132021-04-15 16:37:44 -0700415 err_engine:
Dave Jiang7c5dd232021-04-15 16:37:39 -0700416 for (i = 0; i < idxd->max_wqs; i++)
Dave Jiang700af3a2021-07-15 11:43:20 -0700417 put_device(wq_confdev(idxd->wqs[i]));
Dave Jiangeb15e712021-04-20 11:46:34 -0700418 err_wqs:
419 kfree(idxd->int_handles);
Dave Jiang7c5dd232021-04-15 16:37:39 -0700420 return rc;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700421}
422
423static void idxd_read_table_offsets(struct idxd_device *idxd)
424{
425 union offsets_reg offsets;
426 struct device *dev = &idxd->pdev->dev;
427
428 offsets.bits[0] = ioread64(idxd->reg_base + IDXD_TABLE_OFFSET);
Dave Jiang2f8417a2020-10-30 08:51:56 -0700429 offsets.bits[1] = ioread64(idxd->reg_base + IDXD_TABLE_OFFSET + sizeof(u64));
430 idxd->grpcfg_offset = offsets.grpcfg * IDXD_TABLE_MULT;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700431 dev_dbg(dev, "IDXD Group Config Offset: %#x\n", idxd->grpcfg_offset);
Dave Jiang2f8417a2020-10-30 08:51:56 -0700432 idxd->wqcfg_offset = offsets.wqcfg * IDXD_TABLE_MULT;
433 dev_dbg(dev, "IDXD Work Queue Config Offset: %#x\n", idxd->wqcfg_offset);
434 idxd->msix_perm_offset = offsets.msix_perm * IDXD_TABLE_MULT;
435 dev_dbg(dev, "IDXD MSIX Permission Offset: %#x\n", idxd->msix_perm_offset);
436 idxd->perfmon_offset = offsets.perfmon * IDXD_TABLE_MULT;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700437 dev_dbg(dev, "IDXD Perfmon Offset: %#x\n", idxd->perfmon_offset);
438}
439
440static void idxd_read_caps(struct idxd_device *idxd)
441{
442 struct device *dev = &idxd->pdev->dev;
443 int i;
444
445 /* reading generic capabilities */
446 idxd->hw.gen_cap.bits = ioread64(idxd->reg_base + IDXD_GENCAP_OFFSET);
447 dev_dbg(dev, "gen_cap: %#llx\n", idxd->hw.gen_cap.bits);
Dave Jiangeb15e712021-04-20 11:46:34 -0700448
449 if (idxd->hw.gen_cap.cmd_cap) {
450 idxd->hw.cmd_cap = ioread32(idxd->reg_base + IDXD_CMDCAP_OFFSET);
451 dev_dbg(dev, "cmd_cap: %#x\n", idxd->hw.cmd_cap);
452 }
453
Dave Jiangbfe1d562020-01-21 16:43:59 -0700454 idxd->max_xfer_bytes = 1ULL << idxd->hw.gen_cap.max_xfer_shift;
455 dev_dbg(dev, "max xfer size: %llu bytes\n", idxd->max_xfer_bytes);
456 idxd->max_batch_size = 1U << idxd->hw.gen_cap.max_batch_shift;
457 dev_dbg(dev, "max batch size: %u\n", idxd->max_batch_size);
458 if (idxd->hw.gen_cap.config_en)
459 set_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags);
460
461 /* reading group capabilities */
462 idxd->hw.group_cap.bits =
463 ioread64(idxd->reg_base + IDXD_GRPCAP_OFFSET);
464 dev_dbg(dev, "group_cap: %#llx\n", idxd->hw.group_cap.bits);
465 idxd->max_groups = idxd->hw.group_cap.num_groups;
466 dev_dbg(dev, "max groups: %u\n", idxd->max_groups);
467 idxd->max_tokens = idxd->hw.group_cap.total_tokens;
468 dev_dbg(dev, "max tokens: %u\n", idxd->max_tokens);
Dave Jiangc52ca472020-01-21 16:44:05 -0700469 idxd->nr_tokens = idxd->max_tokens;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700470
471 /* read engine capabilities */
472 idxd->hw.engine_cap.bits =
473 ioread64(idxd->reg_base + IDXD_ENGCAP_OFFSET);
474 dev_dbg(dev, "engine_cap: %#llx\n", idxd->hw.engine_cap.bits);
475 idxd->max_engines = idxd->hw.engine_cap.num_engines;
476 dev_dbg(dev, "max engines: %u\n", idxd->max_engines);
477
478 /* read workqueue capabilities */
479 idxd->hw.wq_cap.bits = ioread64(idxd->reg_base + IDXD_WQCAP_OFFSET);
480 dev_dbg(dev, "wq_cap: %#llx\n", idxd->hw.wq_cap.bits);
481 idxd->max_wq_size = idxd->hw.wq_cap.total_wq_size;
482 dev_dbg(dev, "total workqueue size: %u\n", idxd->max_wq_size);
483 idxd->max_wqs = idxd->hw.wq_cap.num_wqs;
484 dev_dbg(dev, "max workqueues: %u\n", idxd->max_wqs);
Dave Jiangd98793b2020-10-27 14:34:09 -0700485 idxd->wqcfg_size = 1 << (idxd->hw.wq_cap.wqcfg_size + IDXD_WQCFG_MIN);
486 dev_dbg(dev, "wqcfg size: %u\n", idxd->wqcfg_size);
Dave Jiangbfe1d562020-01-21 16:43:59 -0700487
488 /* reading operation capabilities */
489 for (i = 0; i < 4; i++) {
490 idxd->hw.opcap.bits[i] = ioread64(idxd->reg_base +
491 IDXD_OPCAP_OFFSET + i * sizeof(u64));
492 dev_dbg(dev, "opcap[%d]: %#llx\n", i, idxd->hw.opcap.bits[i]);
493 }
494}
495
Dave Jiang435b5122021-04-15 16:38:09 -0700496static struct idxd_device *idxd_alloc(struct pci_dev *pdev, struct idxd_driver_data *data)
Dave Jiangbfe1d562020-01-21 16:43:59 -0700497{
498 struct device *dev = &pdev->dev;
Dave Jiang700af3a2021-07-15 11:43:20 -0700499 struct device *conf_dev;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700500 struct idxd_device *idxd;
Dave Jiang47c16ac2021-04-15 16:37:33 -0700501 int rc;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700502
Dave Jiang47c16ac2021-04-15 16:37:33 -0700503 idxd = kzalloc_node(sizeof(*idxd), GFP_KERNEL, dev_to_node(dev));
Dave Jiangbfe1d562020-01-21 16:43:59 -0700504 if (!idxd)
505 return NULL;
506
Dave Jiang700af3a2021-07-15 11:43:20 -0700507 conf_dev = idxd_confdev(idxd);
Dave Jiangbfe1d562020-01-21 16:43:59 -0700508 idxd->pdev = pdev;
Dave Jiang435b5122021-04-15 16:38:09 -0700509 idxd->data = data;
Dave Jiang700af3a2021-07-15 11:43:20 -0700510 idxd_dev_set_type(&idxd->idxd_dev, idxd->data->type);
Dave Jiang4b73e4e2021-04-15 16:38:03 -0700511 idxd->id = ida_alloc(&idxd_ida, GFP_KERNEL);
Dave Jiang47c16ac2021-04-15 16:37:33 -0700512 if (idxd->id < 0)
513 return NULL;
514
Dave Jiang700af3a2021-07-15 11:43:20 -0700515 device_initialize(conf_dev);
516 conf_dev->parent = dev;
517 conf_dev->bus = &dsa_bus_type;
518 conf_dev->type = idxd->data->dev_type;
519 rc = dev_set_name(conf_dev, "%s%d", idxd->data->name_prefix, idxd->id);
Dave Jiang47c16ac2021-04-15 16:37:33 -0700520 if (rc < 0) {
Dave Jiang700af3a2021-07-15 11:43:20 -0700521 put_device(conf_dev);
Dave Jiang47c16ac2021-04-15 16:37:33 -0700522 return NULL;
523 }
524
Dave Jiangbfe1d562020-01-21 16:43:59 -0700525 spin_lock_init(&idxd->dev_lock);
Dave Jiang53b2ee7f2021-04-20 12:00:56 -0700526 spin_lock_init(&idxd->cmd_lock);
Dave Jiangbfe1d562020-01-21 16:43:59 -0700527
528 return idxd;
529}
530
Dave Jiang8e50d392020-10-27 10:34:35 -0700531static int idxd_enable_system_pasid(struct idxd_device *idxd)
532{
533 int flags;
534 unsigned int pasid;
535 struct iommu_sva *sva;
536
537 flags = SVM_FLAG_SUPERVISOR_MODE;
538
539 sva = iommu_sva_bind_device(&idxd->pdev->dev, NULL, &flags);
540 if (IS_ERR(sva)) {
541 dev_warn(&idxd->pdev->dev,
542 "iommu sva bind failed: %ld\n", PTR_ERR(sva));
543 return PTR_ERR(sva);
544 }
545
546 pasid = iommu_sva_get_pasid(sva);
547 if (pasid == IOMMU_PASID_INVALID) {
548 iommu_sva_unbind_device(sva);
549 return -ENODEV;
550 }
551
552 idxd->sva = sva;
553 idxd->pasid = pasid;
554 dev_dbg(&idxd->pdev->dev, "system pasid: %u\n", pasid);
555 return 0;
556}
557
558static void idxd_disable_system_pasid(struct idxd_device *idxd)
559{
560
561 iommu_sva_unbind_device(idxd->sva);
562 idxd->sva = NULL;
563}
564
Dave Jiangbfe1d562020-01-21 16:43:59 -0700565static int idxd_probe(struct idxd_device *idxd)
566{
567 struct pci_dev *pdev = idxd->pdev;
568 struct device *dev = &pdev->dev;
569 int rc;
570
571 dev_dbg(dev, "%s entered and resetting device\n", __func__);
Dave Jiang89e3bec2021-02-01 08:26:14 -0700572 rc = idxd_device_init_reset(idxd);
573 if (rc < 0)
574 return rc;
575
Dave Jiangbfe1d562020-01-21 16:43:59 -0700576 dev_dbg(dev, "IDXD reset complete\n");
577
Dave Jiang03d939c2021-01-22 11:46:00 -0700578 if (IS_ENABLED(CONFIG_INTEL_IDXD_SVM) && sva) {
Dave Jiangcf5f86a2021-04-20 11:46:46 -0700579 rc = iommu_dev_enable_feature(dev, IOMMU_DEV_FEAT_SVA);
580 if (rc == 0) {
581 rc = idxd_enable_system_pasid(idxd);
582 if (rc < 0) {
583 iommu_dev_disable_feature(dev, IOMMU_DEV_FEAT_SVA);
584 dev_warn(dev, "Failed to enable PASID. No SVA support: %d\n", rc);
585 } else {
586 set_bit(IDXD_FLAG_PASID_ENABLED, &idxd->flags);
587 }
588 } else {
589 dev_warn(dev, "Unable to turn on SVA feature.\n");
590 }
Dave Jiang03d939c2021-01-22 11:46:00 -0700591 } else if (!sva) {
592 dev_warn(dev, "User forced SVA off via module param.\n");
Dave Jiang8e50d392020-10-27 10:34:35 -0700593 }
594
Dave Jiangbfe1d562020-01-21 16:43:59 -0700595 idxd_read_caps(idxd);
596 idxd_read_table_offsets(idxd);
597
598 rc = idxd_setup_internals(idxd);
599 if (rc)
Dave Jiang7c5dd232021-04-15 16:37:39 -0700600 goto err;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700601
Dave Jiang8c66bbdc2021-04-20 11:46:28 -0700602 /* If the configs are readonly, then load them from device */
603 if (!test_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags)) {
604 dev_dbg(dev, "Loading RO device config\n");
605 rc = idxd_device_load_config(idxd);
606 if (rc < 0)
Dave Jiangddf742d2021-05-25 12:23:37 -0700607 goto err_config;
Dave Jiang8c66bbdc2021-04-20 11:46:28 -0700608 }
609
Dave Jiangbfe1d562020-01-21 16:43:59 -0700610 rc = idxd_setup_interrupts(idxd);
611 if (rc)
Dave Jiangddf742d2021-05-25 12:23:37 -0700612 goto err_config;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700613
614 dev_dbg(dev, "IDXD interrupt setup complete.\n");
615
Dave Jiang42d279f2020-01-21 16:44:29 -0700616 idxd->major = idxd_cdev_get_major(idxd);
617
Tom Zanussi0bde4442021-04-24 10:04:16 -0500618 rc = perfmon_pmu_init(idxd);
619 if (rc < 0)
620 dev_warn(dev, "Failed to initialize perfmon. No PMU support: %d\n", rc);
621
Dave Jiangbfe1d562020-01-21 16:43:59 -0700622 dev_dbg(dev, "IDXD device %d probed successfully\n", idxd->id);
623 return 0;
624
Dave Jiangddf742d2021-05-25 12:23:37 -0700625 err_config:
626 idxd_cleanup_internals(idxd);
Dave Jiang7c5dd232021-04-15 16:37:39 -0700627 err:
Dave Jiang8e50d392020-10-27 10:34:35 -0700628 if (device_pasid_enabled(idxd))
629 idxd_disable_system_pasid(idxd);
Dave Jiangcf5f86a2021-04-20 11:46:46 -0700630 iommu_dev_disable_feature(dev, IOMMU_DEV_FEAT_SVA);
Dave Jiangbfe1d562020-01-21 16:43:59 -0700631 return rc;
632}
633
Dave Jiangddf742d2021-05-25 12:23:37 -0700634static void idxd_cleanup(struct idxd_device *idxd)
635{
636 struct device *dev = &idxd->pdev->dev;
637
638 perfmon_pmu_remove(idxd);
639 idxd_cleanup_interrupts(idxd);
640 idxd_cleanup_internals(idxd);
641 if (device_pasid_enabled(idxd))
642 idxd_disable_system_pasid(idxd);
643 iommu_dev_disable_feature(dev, IOMMU_DEV_FEAT_SVA);
644}
645
Dave Jiangbfe1d562020-01-21 16:43:59 -0700646static int idxd_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
647{
Dave Jiangbfe1d562020-01-21 16:43:59 -0700648 struct device *dev = &pdev->dev;
649 struct idxd_device *idxd;
Dave Jiang435b5122021-04-15 16:38:09 -0700650 struct idxd_driver_data *data = (struct idxd_driver_data *)id->driver_data;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700651 int rc;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700652
Dave Jianga39c7cd2021-04-15 16:37:21 -0700653 rc = pci_enable_device(pdev);
Dave Jiangbfe1d562020-01-21 16:43:59 -0700654 if (rc)
655 return rc;
656
Dave Jiang8e50d392020-10-27 10:34:35 -0700657 dev_dbg(dev, "Alloc IDXD context\n");
Dave Jiang435b5122021-04-15 16:38:09 -0700658 idxd = idxd_alloc(pdev, data);
Dave Jianga39c7cd2021-04-15 16:37:21 -0700659 if (!idxd) {
660 rc = -ENOMEM;
661 goto err_idxd_alloc;
662 }
Dave Jiangbfe1d562020-01-21 16:43:59 -0700663
Dave Jiang8e50d392020-10-27 10:34:35 -0700664 dev_dbg(dev, "Mapping BARs\n");
Dave Jianga39c7cd2021-04-15 16:37:21 -0700665 idxd->reg_base = pci_iomap(pdev, IDXD_MMIO_BAR, 0);
666 if (!idxd->reg_base) {
667 rc = -ENOMEM;
668 goto err_iomap;
669 }
Dave Jiangbfe1d562020-01-21 16:43:59 -0700670
671 dev_dbg(dev, "Set DMA masks\n");
Christophe JAILLET53b50452021-07-08 07:08:26 +0200672 rc = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
Dave Jiangbfe1d562020-01-21 16:43:59 -0700673 if (rc)
Christophe JAILLET53b50452021-07-08 07:08:26 +0200674 rc = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
Dave Jiangbfe1d562020-01-21 16:43:59 -0700675 if (rc)
Dave Jianga39c7cd2021-04-15 16:37:21 -0700676 goto err;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700677
Dave Jiangbfe1d562020-01-21 16:43:59 -0700678 dev_dbg(dev, "Set PCI master\n");
679 pci_set_master(pdev);
680 pci_set_drvdata(pdev, idxd);
681
682 idxd->hw.version = ioread32(idxd->reg_base + IDXD_VER_OFFSET);
683 rc = idxd_probe(idxd);
684 if (rc) {
685 dev_err(dev, "Intel(R) IDXD DMA Engine init failed\n");
Dave Jianga39c7cd2021-04-15 16:37:21 -0700686 goto err;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700687 }
688
Dave Jiang47c16ac2021-04-15 16:37:33 -0700689 rc = idxd_register_devices(idxd);
Dave Jiangc52ca472020-01-21 16:44:05 -0700690 if (rc) {
691 dev_err(dev, "IDXD sysfs setup failed\n");
Dave Jiangddf742d2021-05-25 12:23:37 -0700692 goto err_dev_register;
Dave Jiangc52ca472020-01-21 16:44:05 -0700693 }
694
Dave Jiangbfe1d562020-01-21 16:43:59 -0700695 dev_info(&pdev->dev, "Intel(R) Accelerator Device (v%x)\n",
696 idxd->hw.version);
697
698 return 0;
Dave Jianga39c7cd2021-04-15 16:37:21 -0700699
Dave Jiangddf742d2021-05-25 12:23:37 -0700700 err_dev_register:
701 idxd_cleanup(idxd);
Dave Jianga39c7cd2021-04-15 16:37:21 -0700702 err:
703 pci_iounmap(pdev, idxd->reg_base);
704 err_iomap:
Dave Jiang700af3a2021-07-15 11:43:20 -0700705 put_device(idxd_confdev(idxd));
Dave Jianga39c7cd2021-04-15 16:37:21 -0700706 err_idxd_alloc:
707 pci_disable_device(pdev);
708 return rc;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700709}
710
Dave Jiang8f47d1a2020-01-21 16:44:23 -0700711static void idxd_flush_pending_llist(struct idxd_irq_entry *ie)
712{
713 struct idxd_desc *desc, *itr;
714 struct llist_node *head;
715
716 head = llist_del_all(&ie->pending_llist);
717 if (!head)
718 return;
719
720 llist_for_each_entry_safe(desc, itr, head, llnode) {
721 idxd_dma_complete_txd(desc, IDXD_COMPLETE_ABORT);
722 idxd_free_desc(desc->wq, desc);
723 }
724}
725
726static void idxd_flush_work_list(struct idxd_irq_entry *ie)
727{
728 struct idxd_desc *desc, *iter;
729
730 list_for_each_entry_safe(desc, iter, &ie->work_list, list) {
731 list_del(&desc->list);
732 idxd_dma_complete_txd(desc, IDXD_COMPLETE_ABORT);
733 idxd_free_desc(desc->wq, desc);
734 }
735}
736
Dave Jiang5b0c68c2021-04-20 11:46:51 -0700737void idxd_wqs_quiesce(struct idxd_device *idxd)
738{
739 struct idxd_wq *wq;
740 int i;
741
742 for (i = 0; i < idxd->max_wqs; i++) {
743 wq = idxd->wqs[i];
744 if (wq->state == IDXD_WQ_ENABLED && wq->type == IDXD_WQT_KERNEL)
745 idxd_wq_quiesce(wq);
746 }
747}
748
Dave Jiangeb15e712021-04-20 11:46:34 -0700749static void idxd_release_int_handles(struct idxd_device *idxd)
750{
751 struct device *dev = &idxd->pdev->dev;
752 int i, rc;
753
754 for (i = 0; i < idxd->num_wq_irqs; i++) {
755 if (idxd->hw.cmd_cap & BIT(IDXD_CMD_RELEASE_INT_HANDLE)) {
756 rc = idxd_device_release_int_handle(idxd, idxd->int_handles[i],
757 IDXD_IRQ_MSIX);
758 if (rc < 0)
759 dev_warn(dev, "irq handle %d release failed\n",
760 idxd->int_handles[i]);
761 else
762 dev_dbg(dev, "int handle requested: %u\n", idxd->int_handles[i]);
763 }
764 }
765}
766
Dave Jiangbfe1d562020-01-21 16:43:59 -0700767static void idxd_shutdown(struct pci_dev *pdev)
768{
769 struct idxd_device *idxd = pci_get_drvdata(pdev);
770 int rc, i;
771 struct idxd_irq_entry *irq_entry;
772 int msixcnt = pci_msix_vec_count(pdev);
Dave Jiangbfe1d562020-01-21 16:43:59 -0700773
Dave Jiangbfe1d562020-01-21 16:43:59 -0700774 rc = idxd_device_disable(idxd);
Dave Jiangbfe1d562020-01-21 16:43:59 -0700775 if (rc)
776 dev_err(&pdev->dev, "Disabling device failed\n");
777
778 dev_dbg(&pdev->dev, "%s called\n", __func__);
779 idxd_mask_msix_vectors(idxd);
780 idxd_mask_error_interrupts(idxd);
781
782 for (i = 0; i < msixcnt; i++) {
783 irq_entry = &idxd->irq_entries[i];
Dave Jiang5fc8e852021-04-15 16:37:15 -0700784 synchronize_irq(irq_entry->vector);
Dave Jiangbfe1d562020-01-21 16:43:59 -0700785 if (i == 0)
786 continue;
Dave Jiang8f47d1a2020-01-21 16:44:23 -0700787 idxd_flush_pending_llist(irq_entry);
788 idxd_flush_work_list(irq_entry);
Dave Jiangbfe1d562020-01-21 16:43:59 -0700789 }
Dave Jiang49c49592021-07-14 14:57:19 -0700790 flush_workqueue(idxd->wq);
Dave Jiangbfe1d562020-01-21 16:43:59 -0700791}
792
793static void idxd_remove(struct pci_dev *pdev)
794{
795 struct idxd_device *idxd = pci_get_drvdata(pdev);
Dave Jiang49c49592021-07-14 14:57:19 -0700796 struct idxd_irq_entry *irq_entry;
797 int msixcnt = pci_msix_vec_count(pdev);
798 int i;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700799
Dave Jiang98da01062021-09-21 13:15:58 -0700800 idxd_unregister_devices(idxd);
801 /*
802 * When ->release() is called for the idxd->conf_dev, it frees all the memory related
803 * to the idxd context. The driver still needs those bits in order to do the rest of
804 * the cleanup. However, we do need to unbound the idxd sub-driver. So take a ref
805 * on the device here to hold off the freeing while allowing the idxd sub-driver
806 * to unbind.
807 */
808 get_device(idxd_confdev(idxd));
809 device_unregister(idxd_confdev(idxd));
Dave Jiangbfe1d562020-01-21 16:43:59 -0700810 idxd_shutdown(pdev);
Dave Jiang8e50d392020-10-27 10:34:35 -0700811 if (device_pasid_enabled(idxd))
812 idxd_disable_system_pasid(idxd);
Dave Jiang49c49592021-07-14 14:57:19 -0700813
814 for (i = 0; i < msixcnt; i++) {
815 irq_entry = &idxd->irq_entries[i];
816 free_irq(irq_entry->vector, irq_entry);
817 }
818 idxd_msix_perm_clear(idxd);
819 idxd_release_int_handles(idxd);
820 pci_free_irq_vectors(pdev);
821 pci_iounmap(pdev, idxd->reg_base);
Dave Jiangcf5f86a2021-04-20 11:46:46 -0700822 iommu_dev_disable_feature(&pdev->dev, IOMMU_DEV_FEAT_SVA);
Dave Jiang49c49592021-07-14 14:57:19 -0700823 pci_disable_device(pdev);
824 destroy_workqueue(idxd->wq);
825 perfmon_pmu_remove(idxd);
Dave Jiang98da01062021-09-21 13:15:58 -0700826 put_device(idxd_confdev(idxd));
Dave Jiangbfe1d562020-01-21 16:43:59 -0700827}
828
829static struct pci_driver idxd_pci_driver = {
830 .name = DRV_NAME,
831 .id_table = idxd_pci_tbl,
832 .probe = idxd_pci_probe,
833 .remove = idxd_remove,
834 .shutdown = idxd_shutdown,
835};
836
837static int __init idxd_init_module(void)
838{
Dave Jiang4b73e4e2021-04-15 16:38:03 -0700839 int err;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700840
841 /*
Dave Jiang8e50d392020-10-27 10:34:35 -0700842 * If the CPU does not support MOVDIR64B or ENQCMDS, there's no point in
Dave Jiangbfe1d562020-01-21 16:43:59 -0700843 * enumerating the device. We can not utilize it.
844 */
Borislav Petkov74b2fc82021-06-02 12:07:52 +0200845 if (!cpu_feature_enabled(X86_FEATURE_MOVDIR64B)) {
Dave Jiangbfe1d562020-01-21 16:43:59 -0700846 pr_warn("idxd driver failed to load without MOVDIR64B.\n");
847 return -ENODEV;
848 }
849
Borislav Petkov74b2fc82021-06-02 12:07:52 +0200850 if (!cpu_feature_enabled(X86_FEATURE_ENQCMD))
Dave Jiang8e50d392020-10-27 10:34:35 -0700851 pr_warn("Platform does not have ENQCMD(S) support.\n");
852 else
853 support_enqcmd = true;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700854
Tom Zanussi0bde4442021-04-24 10:04:16 -0500855 perfmon_init();
856
Dave Jiang034b3292021-07-15 11:44:24 -0700857 err = idxd_driver_register(&idxd_drv);
858 if (err < 0)
859 goto err_idxd_driver_register;
860
Dave Jiang0cda4f62021-07-15 11:44:30 -0700861 err = idxd_driver_register(&idxd_dmaengine_drv);
862 if (err < 0)
863 goto err_idxd_dmaengine_driver_register;
864
Dave Jiang448c3de2021-07-15 11:44:35 -0700865 err = idxd_driver_register(&idxd_user_drv);
866 if (err < 0)
867 goto err_idxd_user_driver_register;
868
Dave Jiang42d279f2020-01-21 16:44:29 -0700869 err = idxd_cdev_register();
870 if (err)
871 goto err_cdev_register;
872
Dave Jiangc52ca472020-01-21 16:44:05 -0700873 err = pci_register_driver(&idxd_pci_driver);
874 if (err)
875 goto err_pci_register;
876
Dave Jiangbfe1d562020-01-21 16:43:59 -0700877 return 0;
Dave Jiangc52ca472020-01-21 16:44:05 -0700878
879err_pci_register:
Dave Jiang42d279f2020-01-21 16:44:29 -0700880 idxd_cdev_remove();
881err_cdev_register:
Dave Jiang448c3de2021-07-15 11:44:35 -0700882 idxd_driver_unregister(&idxd_user_drv);
883err_idxd_user_driver_register:
Dave Jiang0cda4f62021-07-15 11:44:30 -0700884 idxd_driver_unregister(&idxd_dmaengine_drv);
885err_idxd_dmaengine_driver_register:
Dave Jiang034b3292021-07-15 11:44:24 -0700886 idxd_driver_unregister(&idxd_drv);
887err_idxd_driver_register:
Dave Jiangc52ca472020-01-21 16:44:05 -0700888 return err;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700889}
890module_init(idxd_init_module);
891
892static void __exit idxd_exit_module(void)
893{
Dave Jiang448c3de2021-07-15 11:44:35 -0700894 idxd_driver_unregister(&idxd_user_drv);
Dave Jiang0cda4f62021-07-15 11:44:30 -0700895 idxd_driver_unregister(&idxd_dmaengine_drv);
Dave Jiang034b3292021-07-15 11:44:24 -0700896 idxd_driver_unregister(&idxd_drv);
Dave Jiangbfe1d562020-01-21 16:43:59 -0700897 pci_unregister_driver(&idxd_pci_driver);
Dave Jiang42d279f2020-01-21 16:44:29 -0700898 idxd_cdev_remove();
Tom Zanussi0bde4442021-04-24 10:04:16 -0500899 perfmon_exit();
Dave Jiangbfe1d562020-01-21 16:43:59 -0700900}
901module_exit(idxd_exit_module);