1 // 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/fs.h>
13 #include <linux/io-64-nonatomic-lo-hi.h>
14 #include <linux/device.h>
15 #include <linux/idr.h>
16 #include <linux/iommu.h>
17 #include <uapi/linux/idxd.h>
18 #include <linux/dmaengine.h>
19 #include "../dmaengine.h"
20 #include "registers.h"
21 #include "idxd.h"
22 #include "perfmon.h"
23
24 MODULE_VERSION(IDXD_DRIVER_VERSION);
25 MODULE_LICENSE("GPL v2");
26 MODULE_AUTHOR("Intel Corporation");
27 MODULE_IMPORT_NS(IDXD);
28
29 static bool sva = true;
30 module_param(sva, bool, 0644);
31 MODULE_PARM_DESC(sva, "Toggle SVA support on/off");
32
33 bool tc_override;
34 module_param(tc_override, bool, 0644);
35 MODULE_PARM_DESC(tc_override, "Override traffic class defaults");
36
37 #define DRV_NAME "idxd"
38
39 bool support_enqcmd;
40 DEFINE_IDA(idxd_ida);
41
42 static struct idxd_driver_data idxd_driver_data[] = {
43 [IDXD_TYPE_DSA] = {
44 .name_prefix = "dsa",
45 .type = IDXD_TYPE_DSA,
46 .compl_size = sizeof(struct dsa_completion_record),
47 .align = 32,
48 .dev_type = &dsa_device_type,
49 .evl_cr_off = offsetof(struct dsa_evl_entry, cr),
50 .user_submission_safe = false, /* See INTEL-SA-01084 security advisory */
51 .cr_status_off = offsetof(struct dsa_completion_record, status),
52 .cr_result_off = offsetof(struct dsa_completion_record, result),
53 },
54 [IDXD_TYPE_IAX] = {
55 .name_prefix = "iax",
56 .type = IDXD_TYPE_IAX,
57 .compl_size = sizeof(struct iax_completion_record),
58 .align = 64,
59 .dev_type = &iax_device_type,
60 .evl_cr_off = offsetof(struct iax_evl_entry, cr),
61 .user_submission_safe = false, /* See INTEL-SA-01084 security advisory */
62 .cr_status_off = offsetof(struct iax_completion_record, status),
63 .cr_result_off = offsetof(struct iax_completion_record, error_code),
64 },
65 };
66
67 static struct pci_device_id idxd_pci_tbl[] = {
68 /* DSA ver 1.0 platforms */
69 { PCI_DEVICE_DATA(INTEL, DSA_SPR0, &idxd_driver_data[IDXD_TYPE_DSA]) },
70
71 /* IAX ver 1.0 platforms */
72 { PCI_DEVICE_DATA(INTEL, IAX_SPR0, &idxd_driver_data[IDXD_TYPE_IAX]) },
73 { 0, }
74 };
75 MODULE_DEVICE_TABLE(pci, idxd_pci_tbl);
76
idxd_setup_interrupts(struct idxd_device * idxd)77 static int idxd_setup_interrupts(struct idxd_device *idxd)
78 {
79 struct pci_dev *pdev = idxd->pdev;
80 struct device *dev = &pdev->dev;
81 struct idxd_irq_entry *ie;
82 int i, msixcnt;
83 int rc = 0;
84
85 msixcnt = pci_msix_vec_count(pdev);
86 if (msixcnt < 0) {
87 dev_err(dev, "Not MSI-X interrupt capable.\n");
88 return -ENOSPC;
89 }
90 idxd->irq_cnt = msixcnt;
91
92 rc = pci_alloc_irq_vectors(pdev, msixcnt, msixcnt, PCI_IRQ_MSIX);
93 if (rc != msixcnt) {
94 dev_err(dev, "Failed enabling %d MSIX entries: %d\n", msixcnt, rc);
95 return -ENOSPC;
96 }
97 dev_dbg(dev, "Enabled %d msix vectors\n", msixcnt);
98
99
100 ie = idxd_get_ie(idxd, 0);
101 ie->vector = pci_irq_vector(pdev, 0);
102 rc = request_threaded_irq(ie->vector, NULL, idxd_misc_thread, 0, "idxd-misc", ie);
103 if (rc < 0) {
104 dev_err(dev, "Failed to allocate misc interrupt.\n");
105 goto err_misc_irq;
106 }
107 dev_dbg(dev, "Requested idxd-misc handler on msix vector %d\n", ie->vector);
108
109 for (i = 0; i < idxd->max_wqs; i++) {
110 int msix_idx = i + 1;
111
112 ie = idxd_get_ie(idxd, msix_idx);
113 ie->id = msix_idx;
114 ie->int_handle = INVALID_INT_HANDLE;
115 ie->pasid = IOMMU_PASID_INVALID;
116
117 spin_lock_init(&ie->list_lock);
118 init_llist_head(&ie->pending_llist);
119 INIT_LIST_HEAD(&ie->work_list);
120 }
121
122 idxd_unmask_error_interrupts(idxd);
123 return 0;
124
125 err_misc_irq:
126 idxd_mask_error_interrupts(idxd);
127 pci_free_irq_vectors(pdev);
128 dev_err(dev, "No usable interrupts\n");
129 return rc;
130 }
131
idxd_cleanup_interrupts(struct idxd_device * idxd)132 static void idxd_cleanup_interrupts(struct idxd_device *idxd)
133 {
134 struct pci_dev *pdev = idxd->pdev;
135 struct idxd_irq_entry *ie;
136 int msixcnt;
137
138 msixcnt = pci_msix_vec_count(pdev);
139 if (msixcnt <= 0)
140 return;
141
142 ie = idxd_get_ie(idxd, 0);
143 idxd_mask_error_interrupts(idxd);
144 free_irq(ie->vector, ie);
145 pci_free_irq_vectors(pdev);
146 }
147
idxd_clean_wqs(struct idxd_device * idxd)148 static void idxd_clean_wqs(struct idxd_device *idxd)
149 {
150 struct idxd_wq *wq;
151 struct device *conf_dev;
152 int i;
153
154 for (i = 0; i < idxd->max_wqs; i++) {
155 wq = idxd->wqs[i];
156 if (idxd->hw.wq_cap.op_config)
157 bitmap_free(wq->opcap_bmap);
158 kfree(wq->wqcfg);
159 conf_dev = wq_confdev(wq);
160 put_device(conf_dev);
161 kfree(wq);
162 }
163 bitmap_free(idxd->wq_enable_map);
164 kfree(idxd->wqs);
165 }
166
idxd_setup_wqs(struct idxd_device * idxd)167 static int idxd_setup_wqs(struct idxd_device *idxd)
168 {
169 struct device *dev = &idxd->pdev->dev;
170 struct idxd_wq *wq;
171 struct device *conf_dev;
172 int i, rc;
173
174 idxd->wqs = kcalloc_node(idxd->max_wqs, sizeof(struct idxd_wq *),
175 GFP_KERNEL, dev_to_node(dev));
176 if (!idxd->wqs)
177 return -ENOMEM;
178
179 idxd->wq_enable_map = bitmap_zalloc_node(idxd->max_wqs, GFP_KERNEL, dev_to_node(dev));
180 if (!idxd->wq_enable_map) {
181 rc = -ENOMEM;
182 goto err_bitmap;
183 }
184
185 for (i = 0; i < idxd->max_wqs; i++) {
186 wq = kzalloc_node(sizeof(*wq), GFP_KERNEL, dev_to_node(dev));
187 if (!wq) {
188 rc = -ENOMEM;
189 goto err;
190 }
191
192 idxd_dev_set_type(&wq->idxd_dev, IDXD_DEV_WQ);
193 conf_dev = wq_confdev(wq);
194 wq->id = i;
195 wq->idxd = idxd;
196 device_initialize(wq_confdev(wq));
197 conf_dev->parent = idxd_confdev(idxd);
198 conf_dev->bus = &dsa_bus_type;
199 conf_dev->type = &idxd_wq_device_type;
200 rc = dev_set_name(conf_dev, "wq%d.%d", idxd->id, wq->id);
201 if (rc < 0)
202 goto err;
203
204 mutex_init(&wq->wq_lock);
205 init_waitqueue_head(&wq->err_queue);
206 init_completion(&wq->wq_dead);
207 init_completion(&wq->wq_resurrect);
208 wq->max_xfer_bytes = WQ_DEFAULT_MAX_XFER;
209 idxd_wq_set_max_batch_size(idxd->data->type, wq, WQ_DEFAULT_MAX_BATCH);
210 wq->enqcmds_retries = IDXD_ENQCMDS_RETRIES;
211 wq->wqcfg = kzalloc_node(idxd->wqcfg_size, GFP_KERNEL, dev_to_node(dev));
212 if (!wq->wqcfg) {
213 rc = -ENOMEM;
214 goto err;
215 }
216
217 if (idxd->hw.wq_cap.op_config) {
218 wq->opcap_bmap = bitmap_zalloc(IDXD_MAX_OPCAP_BITS, GFP_KERNEL);
219 if (!wq->opcap_bmap) {
220 rc = -ENOMEM;
221 goto err_opcap_bmap;
222 }
223 bitmap_copy(wq->opcap_bmap, idxd->opcap_bmap, IDXD_MAX_OPCAP_BITS);
224 }
225 mutex_init(&wq->uc_lock);
226 xa_init(&wq->upasid_xa);
227 idxd->wqs[i] = wq;
228 }
229
230 return 0;
231
232 err_opcap_bmap:
233 kfree(wq->wqcfg);
234
235 err:
236 put_device(conf_dev);
237 kfree(wq);
238
239 while (--i >= 0) {
240 wq = idxd->wqs[i];
241 if (idxd->hw.wq_cap.op_config)
242 bitmap_free(wq->opcap_bmap);
243 kfree(wq->wqcfg);
244 conf_dev = wq_confdev(wq);
245 put_device(conf_dev);
246 kfree(wq);
247
248 }
249 bitmap_free(idxd->wq_enable_map);
250
251 err_bitmap:
252 kfree(idxd->wqs);
253
254 return rc;
255 }
256
idxd_clean_engines(struct idxd_device * idxd)257 static void idxd_clean_engines(struct idxd_device *idxd)
258 {
259 struct idxd_engine *engine;
260 struct device *conf_dev;
261 int i;
262
263 for (i = 0; i < idxd->max_engines; i++) {
264 engine = idxd->engines[i];
265 conf_dev = engine_confdev(engine);
266 put_device(conf_dev);
267 kfree(engine);
268 }
269 kfree(idxd->engines);
270 }
271
idxd_setup_engines(struct idxd_device * idxd)272 static int idxd_setup_engines(struct idxd_device *idxd)
273 {
274 struct idxd_engine *engine;
275 struct device *dev = &idxd->pdev->dev;
276 struct device *conf_dev;
277 int i, rc;
278
279 idxd->engines = kcalloc_node(idxd->max_engines, sizeof(struct idxd_engine *),
280 GFP_KERNEL, dev_to_node(dev));
281 if (!idxd->engines)
282 return -ENOMEM;
283
284 for (i = 0; i < idxd->max_engines; i++) {
285 engine = kzalloc_node(sizeof(*engine), GFP_KERNEL, dev_to_node(dev));
286 if (!engine) {
287 rc = -ENOMEM;
288 goto err;
289 }
290
291 idxd_dev_set_type(&engine->idxd_dev, IDXD_DEV_ENGINE);
292 conf_dev = engine_confdev(engine);
293 engine->id = i;
294 engine->idxd = idxd;
295 device_initialize(conf_dev);
296 conf_dev->parent = idxd_confdev(idxd);
297 conf_dev->bus = &dsa_bus_type;
298 conf_dev->type = &idxd_engine_device_type;
299 rc = dev_set_name(conf_dev, "engine%d.%d", idxd->id, engine->id);
300 if (rc < 0) {
301 put_device(conf_dev);
302 kfree(engine);
303 goto err;
304 }
305
306 idxd->engines[i] = engine;
307 }
308
309 return 0;
310
311 err:
312 while (--i >= 0) {
313 engine = idxd->engines[i];
314 conf_dev = engine_confdev(engine);
315 put_device(conf_dev);
316 kfree(engine);
317 }
318 kfree(idxd->engines);
319
320 return rc;
321 }
322
idxd_clean_groups(struct idxd_device * idxd)323 static void idxd_clean_groups(struct idxd_device *idxd)
324 {
325 struct idxd_group *group;
326 int i;
327
328 for (i = 0; i < idxd->max_groups; i++) {
329 group = idxd->groups[i];
330 put_device(group_confdev(group));
331 kfree(group);
332 }
333 kfree(idxd->groups);
334 }
335
idxd_setup_groups(struct idxd_device * idxd)336 static int idxd_setup_groups(struct idxd_device *idxd)
337 {
338 struct device *dev = &idxd->pdev->dev;
339 struct device *conf_dev;
340 struct idxd_group *group;
341 int i, rc;
342
343 idxd->groups = kcalloc_node(idxd->max_groups, sizeof(struct idxd_group *),
344 GFP_KERNEL, dev_to_node(dev));
345 if (!idxd->groups)
346 return -ENOMEM;
347
348 for (i = 0; i < idxd->max_groups; i++) {
349 group = kzalloc_node(sizeof(*group), GFP_KERNEL, dev_to_node(dev));
350 if (!group) {
351 rc = -ENOMEM;
352 goto err;
353 }
354
355 idxd_dev_set_type(&group->idxd_dev, IDXD_DEV_GROUP);
356 conf_dev = group_confdev(group);
357 group->id = i;
358 group->idxd = idxd;
359 device_initialize(conf_dev);
360 conf_dev->parent = idxd_confdev(idxd);
361 conf_dev->bus = &dsa_bus_type;
362 conf_dev->type = &idxd_group_device_type;
363 rc = dev_set_name(conf_dev, "group%d.%d", idxd->id, group->id);
364 if (rc < 0) {
365 put_device(conf_dev);
366 kfree(group);
367 goto err;
368 }
369
370 idxd->groups[i] = group;
371 if (idxd->hw.version <= DEVICE_VERSION_2 && !tc_override) {
372 group->tc_a = 1;
373 group->tc_b = 1;
374 } else {
375 group->tc_a = -1;
376 group->tc_b = -1;
377 }
378 /*
379 * The default value is the same as the value of
380 * total read buffers in GRPCAP.
381 */
382 group->rdbufs_allowed = idxd->max_rdbufs;
383 }
384
385 return 0;
386
387 err:
388 while (--i >= 0) {
389 group = idxd->groups[i];
390 put_device(group_confdev(group));
391 kfree(group);
392 }
393 kfree(idxd->groups);
394
395 return rc;
396 }
397
idxd_cleanup_internals(struct idxd_device * idxd)398 static void idxd_cleanup_internals(struct idxd_device *idxd)
399 {
400 idxd_clean_groups(idxd);
401 idxd_clean_engines(idxd);
402 idxd_clean_wqs(idxd);
403 destroy_workqueue(idxd->wq);
404 }
405
idxd_init_evl(struct idxd_device * idxd)406 static int idxd_init_evl(struct idxd_device *idxd)
407 {
408 struct device *dev = &idxd->pdev->dev;
409 unsigned int evl_cache_size;
410 struct idxd_evl *evl;
411 const char *idxd_name;
412
413 if (idxd->hw.gen_cap.evl_support == 0)
414 return 0;
415
416 evl = kzalloc_node(sizeof(*evl), GFP_KERNEL, dev_to_node(dev));
417 if (!evl)
418 return -ENOMEM;
419
420 mutex_init(&evl->lock);
421 evl->size = IDXD_EVL_SIZE_MIN;
422
423 idxd_name = dev_name(idxd_confdev(idxd));
424 evl_cache_size = sizeof(struct idxd_evl_fault) + evl_ent_size(idxd);
425 /*
426 * Since completion record in evl_cache will be copied to user
427 * when handling completion record page fault, need to create
428 * the cache suitable for user copy.
429 */
430 idxd->evl_cache = kmem_cache_create_usercopy(idxd_name, evl_cache_size,
431 0, 0, 0, evl_cache_size,
432 NULL);
433 if (!idxd->evl_cache) {
434 kfree(evl);
435 return -ENOMEM;
436 }
437
438 idxd->evl = evl;
439 return 0;
440 }
441
idxd_setup_internals(struct idxd_device * idxd)442 static int idxd_setup_internals(struct idxd_device *idxd)
443 {
444 struct device *dev = &idxd->pdev->dev;
445 int rc;
446
447 init_waitqueue_head(&idxd->cmd_waitq);
448
449 rc = idxd_setup_wqs(idxd);
450 if (rc < 0)
451 goto err_wqs;
452
453 rc = idxd_setup_engines(idxd);
454 if (rc < 0)
455 goto err_engine;
456
457 rc = idxd_setup_groups(idxd);
458 if (rc < 0)
459 goto err_group;
460
461 idxd->wq = create_workqueue(dev_name(dev));
462 if (!idxd->wq) {
463 rc = -ENOMEM;
464 goto err_wkq_create;
465 }
466
467 rc = idxd_init_evl(idxd);
468 if (rc < 0)
469 goto err_evl;
470
471 return 0;
472
473 err_evl:
474 destroy_workqueue(idxd->wq);
475 err_wkq_create:
476 idxd_clean_groups(idxd);
477 err_group:
478 idxd_clean_engines(idxd);
479 err_engine:
480 idxd_clean_wqs(idxd);
481 err_wqs:
482 return rc;
483 }
484
idxd_read_table_offsets(struct idxd_device * idxd)485 static void idxd_read_table_offsets(struct idxd_device *idxd)
486 {
487 union offsets_reg offsets;
488 struct device *dev = &idxd->pdev->dev;
489
490 offsets.bits[0] = ioread64(idxd->reg_base + IDXD_TABLE_OFFSET);
491 offsets.bits[1] = ioread64(idxd->reg_base + IDXD_TABLE_OFFSET + sizeof(u64));
492 idxd->grpcfg_offset = offsets.grpcfg * IDXD_TABLE_MULT;
493 dev_dbg(dev, "IDXD Group Config Offset: %#x\n", idxd->grpcfg_offset);
494 idxd->wqcfg_offset = offsets.wqcfg * IDXD_TABLE_MULT;
495 dev_dbg(dev, "IDXD Work Queue Config Offset: %#x\n", idxd->wqcfg_offset);
496 idxd->msix_perm_offset = offsets.msix_perm * IDXD_TABLE_MULT;
497 dev_dbg(dev, "IDXD MSIX Permission Offset: %#x\n", idxd->msix_perm_offset);
498 idxd->perfmon_offset = offsets.perfmon * IDXD_TABLE_MULT;
499 dev_dbg(dev, "IDXD Perfmon Offset: %#x\n", idxd->perfmon_offset);
500 }
501
multi_u64_to_bmap(unsigned long * bmap,u64 * val,int count)502 void multi_u64_to_bmap(unsigned long *bmap, u64 *val, int count)
503 {
504 int i, j, nr;
505
506 for (i = 0, nr = 0; i < count; i++) {
507 for (j = 0; j < BITS_PER_LONG_LONG; j++) {
508 if (val[i] & BIT(j))
509 set_bit(nr, bmap);
510 nr++;
511 }
512 }
513 }
514
idxd_read_caps(struct idxd_device * idxd)515 static void idxd_read_caps(struct idxd_device *idxd)
516 {
517 struct device *dev = &idxd->pdev->dev;
518 int i;
519
520 /* reading generic capabilities */
521 idxd->hw.gen_cap.bits = ioread64(idxd->reg_base + IDXD_GENCAP_OFFSET);
522 dev_dbg(dev, "gen_cap: %#llx\n", idxd->hw.gen_cap.bits);
523
524 if (idxd->hw.gen_cap.cmd_cap) {
525 idxd->hw.cmd_cap = ioread32(idxd->reg_base + IDXD_CMDCAP_OFFSET);
526 dev_dbg(dev, "cmd_cap: %#x\n", idxd->hw.cmd_cap);
527 }
528
529 /* reading command capabilities */
530 if (idxd->hw.cmd_cap & BIT(IDXD_CMD_REQUEST_INT_HANDLE))
531 idxd->request_int_handles = true;
532
533 idxd->max_xfer_bytes = 1ULL << idxd->hw.gen_cap.max_xfer_shift;
534 dev_dbg(dev, "max xfer size: %llu bytes\n", idxd->max_xfer_bytes);
535 idxd_set_max_batch_size(idxd->data->type, idxd, 1U << idxd->hw.gen_cap.max_batch_shift);
536 dev_dbg(dev, "max batch size: %u\n", idxd->max_batch_size);
537 if (idxd->hw.gen_cap.config_en)
538 set_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags);
539
540 /* reading group capabilities */
541 idxd->hw.group_cap.bits =
542 ioread64(idxd->reg_base + IDXD_GRPCAP_OFFSET);
543 dev_dbg(dev, "group_cap: %#llx\n", idxd->hw.group_cap.bits);
544 idxd->max_groups = idxd->hw.group_cap.num_groups;
545 dev_dbg(dev, "max groups: %u\n", idxd->max_groups);
546 idxd->max_rdbufs = idxd->hw.group_cap.total_rdbufs;
547 dev_dbg(dev, "max read buffers: %u\n", idxd->max_rdbufs);
548 idxd->nr_rdbufs = idxd->max_rdbufs;
549
550 /* read engine capabilities */
551 idxd->hw.engine_cap.bits =
552 ioread64(idxd->reg_base + IDXD_ENGCAP_OFFSET);
553 dev_dbg(dev, "engine_cap: %#llx\n", idxd->hw.engine_cap.bits);
554 idxd->max_engines = idxd->hw.engine_cap.num_engines;
555 dev_dbg(dev, "max engines: %u\n", idxd->max_engines);
556
557 /* read workqueue capabilities */
558 idxd->hw.wq_cap.bits = ioread64(idxd->reg_base + IDXD_WQCAP_OFFSET);
559 dev_dbg(dev, "wq_cap: %#llx\n", idxd->hw.wq_cap.bits);
560 idxd->max_wq_size = idxd->hw.wq_cap.total_wq_size;
561 dev_dbg(dev, "total workqueue size: %u\n", idxd->max_wq_size);
562 idxd->max_wqs = idxd->hw.wq_cap.num_wqs;
563 dev_dbg(dev, "max workqueues: %u\n", idxd->max_wqs);
564 idxd->wqcfg_size = 1 << (idxd->hw.wq_cap.wqcfg_size + IDXD_WQCFG_MIN);
565 dev_dbg(dev, "wqcfg size: %u\n", idxd->wqcfg_size);
566
567 /* reading operation capabilities */
568 for (i = 0; i < 4; i++) {
569 idxd->hw.opcap.bits[i] = ioread64(idxd->reg_base +
570 IDXD_OPCAP_OFFSET + i * sizeof(u64));
571 dev_dbg(dev, "opcap[%d]: %#llx\n", i, idxd->hw.opcap.bits[i]);
572 }
573 multi_u64_to_bmap(idxd->opcap_bmap, &idxd->hw.opcap.bits[0], 4);
574
575 /* read iaa cap */
576 if (idxd->data->type == IDXD_TYPE_IAX && idxd->hw.version >= DEVICE_VERSION_2)
577 idxd->hw.iaa_cap.bits = ioread64(idxd->reg_base + IDXD_IAACAP_OFFSET);
578 }
579
idxd_free(struct idxd_device * idxd)580 static void idxd_free(struct idxd_device *idxd)
581 {
582 if (!idxd)
583 return;
584
585 put_device(idxd_confdev(idxd));
586 bitmap_free(idxd->opcap_bmap);
587 ida_free(&idxd_ida, idxd->id);
588 kfree(idxd);
589 }
590
idxd_alloc(struct pci_dev * pdev,struct idxd_driver_data * data)591 static struct idxd_device *idxd_alloc(struct pci_dev *pdev, struct idxd_driver_data *data)
592 {
593 struct device *dev = &pdev->dev;
594 struct device *conf_dev;
595 struct idxd_device *idxd;
596 int rc;
597
598 idxd = kzalloc_node(sizeof(*idxd), GFP_KERNEL, dev_to_node(dev));
599 if (!idxd)
600 return NULL;
601
602 conf_dev = idxd_confdev(idxd);
603 idxd->pdev = pdev;
604 idxd->data = data;
605 idxd_dev_set_type(&idxd->idxd_dev, idxd->data->type);
606 idxd->id = ida_alloc(&idxd_ida, GFP_KERNEL);
607 if (idxd->id < 0)
608 goto err_ida;
609
610 idxd->opcap_bmap = bitmap_zalloc_node(IDXD_MAX_OPCAP_BITS, GFP_KERNEL, dev_to_node(dev));
611 if (!idxd->opcap_bmap)
612 goto err_opcap;
613
614 device_initialize(conf_dev);
615 conf_dev->parent = dev;
616 conf_dev->bus = &dsa_bus_type;
617 conf_dev->type = idxd->data->dev_type;
618 rc = dev_set_name(conf_dev, "%s%d", idxd->data->name_prefix, idxd->id);
619 if (rc < 0)
620 goto err_name;
621
622 spin_lock_init(&idxd->dev_lock);
623 spin_lock_init(&idxd->cmd_lock);
624
625 return idxd;
626
627 err_name:
628 put_device(conf_dev);
629 bitmap_free(idxd->opcap_bmap);
630 err_opcap:
631 ida_free(&idxd_ida, idxd->id);
632 err_ida:
633 kfree(idxd);
634
635 return NULL;
636 }
637
idxd_enable_system_pasid(struct idxd_device * idxd)638 static int idxd_enable_system_pasid(struct idxd_device *idxd)
639 {
640 struct pci_dev *pdev = idxd->pdev;
641 struct device *dev = &pdev->dev;
642 struct iommu_domain *domain;
643 ioasid_t pasid;
644 int ret;
645
646 /*
647 * Attach a global PASID to the DMA domain so that we can use ENQCMDS
648 * to submit work on buffers mapped by DMA API.
649 */
650 domain = iommu_get_domain_for_dev(dev);
651 if (!domain)
652 return -EPERM;
653
654 pasid = iommu_alloc_global_pasid(dev);
655 if (pasid == IOMMU_PASID_INVALID)
656 return -ENOSPC;
657
658 /*
659 * DMA domain is owned by the driver, it should support all valid
660 * types such as DMA-FQ, identity, etc.
661 */
662 ret = iommu_attach_device_pasid(domain, dev, pasid);
663 if (ret) {
664 dev_err(dev, "failed to attach device pasid %d, domain type %d",
665 pasid, domain->type);
666 iommu_free_global_pasid(pasid);
667 return ret;
668 }
669
670 /* Since we set user privilege for kernel DMA, enable completion IRQ */
671 idxd_set_user_intr(idxd, 1);
672 idxd->pasid = pasid;
673
674 return ret;
675 }
676
idxd_disable_system_pasid(struct idxd_device * idxd)677 static void idxd_disable_system_pasid(struct idxd_device *idxd)
678 {
679 struct pci_dev *pdev = idxd->pdev;
680 struct device *dev = &pdev->dev;
681 struct iommu_domain *domain;
682
683 domain = iommu_get_domain_for_dev(dev);
684 if (!domain)
685 return;
686
687 iommu_detach_device_pasid(domain, dev, idxd->pasid);
688 iommu_free_global_pasid(idxd->pasid);
689
690 idxd_set_user_intr(idxd, 0);
691 idxd->sva = NULL;
692 idxd->pasid = IOMMU_PASID_INVALID;
693 }
694
idxd_enable_sva(struct pci_dev * pdev)695 static int idxd_enable_sva(struct pci_dev *pdev)
696 {
697 int ret;
698
699 ret = iommu_dev_enable_feature(&pdev->dev, IOMMU_DEV_FEAT_IOPF);
700 if (ret)
701 return ret;
702
703 ret = iommu_dev_enable_feature(&pdev->dev, IOMMU_DEV_FEAT_SVA);
704 if (ret)
705 iommu_dev_disable_feature(&pdev->dev, IOMMU_DEV_FEAT_IOPF);
706
707 return ret;
708 }
709
idxd_disable_sva(struct pci_dev * pdev)710 static void idxd_disable_sva(struct pci_dev *pdev)
711 {
712 iommu_dev_disable_feature(&pdev->dev, IOMMU_DEV_FEAT_SVA);
713 iommu_dev_disable_feature(&pdev->dev, IOMMU_DEV_FEAT_IOPF);
714 }
715
idxd_probe(struct idxd_device * idxd)716 static int idxd_probe(struct idxd_device *idxd)
717 {
718 struct pci_dev *pdev = idxd->pdev;
719 struct device *dev = &pdev->dev;
720 int rc;
721
722 dev_dbg(dev, "%s entered and resetting device\n", __func__);
723 rc = idxd_device_init_reset(idxd);
724 if (rc < 0)
725 return rc;
726
727 dev_dbg(dev, "IDXD reset complete\n");
728
729 if (IS_ENABLED(CONFIG_INTEL_IDXD_SVM) && sva) {
730 if (idxd_enable_sva(pdev)) {
731 dev_warn(dev, "Unable to turn on user SVA feature.\n");
732 } else {
733 set_bit(IDXD_FLAG_USER_PASID_ENABLED, &idxd->flags);
734
735 rc = idxd_enable_system_pasid(idxd);
736 if (rc)
737 dev_warn(dev, "No in-kernel DMA with PASID. %d\n", rc);
738 else
739 set_bit(IDXD_FLAG_PASID_ENABLED, &idxd->flags);
740 }
741 } else if (!sva) {
742 dev_warn(dev, "User forced SVA off via module param.\n");
743 }
744
745 idxd_read_caps(idxd);
746 idxd_read_table_offsets(idxd);
747
748 rc = idxd_setup_internals(idxd);
749 if (rc)
750 goto err;
751
752 /* If the configs are readonly, then load them from device */
753 if (!test_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags)) {
754 dev_dbg(dev, "Loading RO device config\n");
755 rc = idxd_device_load_config(idxd);
756 if (rc < 0)
757 goto err_config;
758 }
759
760 rc = idxd_setup_interrupts(idxd);
761 if (rc)
762 goto err_config;
763
764 idxd->major = idxd_cdev_get_major(idxd);
765
766 rc = perfmon_pmu_init(idxd);
767 if (rc < 0)
768 dev_warn(dev, "Failed to initialize perfmon. No PMU support: %d\n", rc);
769
770 dev_dbg(dev, "IDXD device %d probed successfully\n", idxd->id);
771 return 0;
772
773 err_config:
774 idxd_cleanup_internals(idxd);
775 err:
776 if (device_pasid_enabled(idxd))
777 idxd_disable_system_pasid(idxd);
778 if (device_user_pasid_enabled(idxd))
779 idxd_disable_sva(pdev);
780 return rc;
781 }
782
idxd_cleanup(struct idxd_device * idxd)783 static void idxd_cleanup(struct idxd_device *idxd)
784 {
785 perfmon_pmu_remove(idxd);
786 idxd_cleanup_interrupts(idxd);
787 idxd_cleanup_internals(idxd);
788 if (device_pasid_enabled(idxd))
789 idxd_disable_system_pasid(idxd);
790 if (device_user_pasid_enabled(idxd))
791 idxd_disable_sva(idxd->pdev);
792 }
793
idxd_pci_probe(struct pci_dev * pdev,const struct pci_device_id * id)794 static int idxd_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
795 {
796 struct device *dev = &pdev->dev;
797 struct idxd_device *idxd;
798 struct idxd_driver_data *data = (struct idxd_driver_data *)id->driver_data;
799 int rc;
800
801 rc = pci_enable_device(pdev);
802 if (rc)
803 return rc;
804
805 dev_dbg(dev, "Alloc IDXD context\n");
806 idxd = idxd_alloc(pdev, data);
807 if (!idxd) {
808 rc = -ENOMEM;
809 goto err_idxd_alloc;
810 }
811
812 dev_dbg(dev, "Mapping BARs\n");
813 idxd->reg_base = pci_iomap(pdev, IDXD_MMIO_BAR, 0);
814 if (!idxd->reg_base) {
815 rc = -ENOMEM;
816 goto err_iomap;
817 }
818
819 dev_dbg(dev, "Set DMA masks\n");
820 rc = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
821 if (rc)
822 goto err;
823
824 dev_dbg(dev, "Set PCI master\n");
825 pci_set_master(pdev);
826 pci_set_drvdata(pdev, idxd);
827
828 idxd->hw.version = ioread32(idxd->reg_base + IDXD_VER_OFFSET);
829 rc = idxd_probe(idxd);
830 if (rc) {
831 dev_err(dev, "Intel(R) IDXD DMA Engine init failed\n");
832 goto err;
833 }
834
835 rc = idxd_register_devices(idxd);
836 if (rc) {
837 dev_err(dev, "IDXD sysfs setup failed\n");
838 goto err_dev_register;
839 }
840
841 rc = idxd_device_init_debugfs(idxd);
842 if (rc)
843 dev_warn(dev, "IDXD debugfs failed to setup\n");
844
845 dev_info(&pdev->dev, "Intel(R) Accelerator Device (v%x)\n",
846 idxd->hw.version);
847
848 idxd->user_submission_safe = data->user_submission_safe;
849
850 return 0;
851
852 err_dev_register:
853 idxd_cleanup(idxd);
854 err:
855 pci_iounmap(pdev, idxd->reg_base);
856 err_iomap:
857 idxd_free(idxd);
858 err_idxd_alloc:
859 pci_disable_device(pdev);
860 return rc;
861 }
862
idxd_wqs_quiesce(struct idxd_device * idxd)863 void idxd_wqs_quiesce(struct idxd_device *idxd)
864 {
865 struct idxd_wq *wq;
866 int i;
867
868 for (i = 0; i < idxd->max_wqs; i++) {
869 wq = idxd->wqs[i];
870 if (wq->state == IDXD_WQ_ENABLED && wq->type == IDXD_WQT_KERNEL)
871 idxd_wq_quiesce(wq);
872 }
873 }
874
idxd_shutdown(struct pci_dev * pdev)875 static void idxd_shutdown(struct pci_dev *pdev)
876 {
877 struct idxd_device *idxd = pci_get_drvdata(pdev);
878 struct idxd_irq_entry *irq_entry;
879 int rc;
880
881 rc = idxd_device_disable(idxd);
882 if (rc)
883 dev_err(&pdev->dev, "Disabling device failed\n");
884
885 irq_entry = &idxd->ie;
886 synchronize_irq(irq_entry->vector);
887 idxd_mask_error_interrupts(idxd);
888 flush_workqueue(idxd->wq);
889 }
890
idxd_remove(struct pci_dev * pdev)891 static void idxd_remove(struct pci_dev *pdev)
892 {
893 struct idxd_device *idxd = pci_get_drvdata(pdev);
894
895 idxd_unregister_devices(idxd);
896 /*
897 * When ->release() is called for the idxd->conf_dev, it frees all the memory related
898 * to the idxd context. The driver still needs those bits in order to do the rest of
899 * the cleanup. However, we do need to unbound the idxd sub-driver. So take a ref
900 * on the device here to hold off the freeing while allowing the idxd sub-driver
901 * to unbind.
902 */
903 get_device(idxd_confdev(idxd));
904 device_unregister(idxd_confdev(idxd));
905 idxd_shutdown(pdev);
906 idxd_device_remove_debugfs(idxd);
907 idxd_cleanup(idxd);
908 pci_iounmap(pdev, idxd->reg_base);
909 put_device(idxd_confdev(idxd));
910 idxd_free(idxd);
911 pci_disable_device(pdev);
912 }
913
914 static struct pci_driver idxd_pci_driver = {
915 .name = DRV_NAME,
916 .id_table = idxd_pci_tbl,
917 .probe = idxd_pci_probe,
918 .remove = idxd_remove,
919 .shutdown = idxd_shutdown,
920 };
921
idxd_init_module(void)922 static int __init idxd_init_module(void)
923 {
924 int err;
925
926 /*
927 * If the CPU does not support MOVDIR64B or ENQCMDS, there's no point in
928 * enumerating the device. We can not utilize it.
929 */
930 if (!cpu_feature_enabled(X86_FEATURE_MOVDIR64B)) {
931 pr_warn("idxd driver failed to load without MOVDIR64B.\n");
932 return -ENODEV;
933 }
934
935 if (!cpu_feature_enabled(X86_FEATURE_ENQCMD))
936 pr_warn("Platform does not have ENQCMD(S) support.\n");
937 else
938 support_enqcmd = true;
939
940 perfmon_init();
941
942 err = idxd_driver_register(&idxd_drv);
943 if (err < 0)
944 goto err_idxd_driver_register;
945
946 err = idxd_driver_register(&idxd_dmaengine_drv);
947 if (err < 0)
948 goto err_idxd_dmaengine_driver_register;
949
950 err = idxd_driver_register(&idxd_user_drv);
951 if (err < 0)
952 goto err_idxd_user_driver_register;
953
954 err = idxd_cdev_register();
955 if (err)
956 goto err_cdev_register;
957
958 err = idxd_init_debugfs();
959 if (err)
960 goto err_debugfs;
961
962 err = pci_register_driver(&idxd_pci_driver);
963 if (err)
964 goto err_pci_register;
965
966 return 0;
967
968 err_pci_register:
969 idxd_remove_debugfs();
970 err_debugfs:
971 idxd_cdev_remove();
972 err_cdev_register:
973 idxd_driver_unregister(&idxd_user_drv);
974 err_idxd_user_driver_register:
975 idxd_driver_unregister(&idxd_dmaengine_drv);
976 err_idxd_dmaengine_driver_register:
977 idxd_driver_unregister(&idxd_drv);
978 err_idxd_driver_register:
979 return err;
980 }
981 module_init(idxd_init_module);
982
idxd_exit_module(void)983 static void __exit idxd_exit_module(void)
984 {
985 idxd_driver_unregister(&idxd_user_drv);
986 idxd_driver_unregister(&idxd_dmaengine_drv);
987 idxd_driver_unregister(&idxd_drv);
988 pci_unregister_driver(&idxd_pci_driver);
989 idxd_cdev_remove();
990 perfmon_exit();
991 idxd_remove_debugfs();
992 }
993 module_exit(idxd_exit_module);
994