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_DESCRIPTION("Intel Data Streaming Accelerator and In-Memory Analytics Accelerator common driver"); 26 MODULE_LICENSE("GPL v2"); 27 MODULE_AUTHOR("Intel Corporation"); 28 MODULE_IMPORT_NS("IDXD"); 29 30 static bool sva = true; 31 module_param(sva, bool, 0644); 32 MODULE_PARM_DESC(sva, "Toggle SVA support on/off"); 33 34 bool tc_override; 35 module_param(tc_override, bool, 0644); 36 MODULE_PARM_DESC(tc_override, "Override traffic class defaults"); 37 38 #define DRV_NAME "idxd" 39 40 bool support_enqcmd; 41 DEFINE_IDA(idxd_ida); 42 43 static struct idxd_driver_data idxd_driver_data[] = { 44 [IDXD_TYPE_DSA] = { 45 .name_prefix = "dsa", 46 .type = IDXD_TYPE_DSA, 47 .compl_size = sizeof(struct dsa_completion_record), 48 .align = 32, 49 .dev_type = &dsa_device_type, 50 .evl_cr_off = offsetof(struct dsa_evl_entry, cr), 51 .user_submission_safe = false, /* See INTEL-SA-01084 security advisory */ 52 .cr_status_off = offsetof(struct dsa_completion_record, status), 53 .cr_result_off = offsetof(struct dsa_completion_record, result), 54 }, 55 [IDXD_TYPE_IAX] = { 56 .name_prefix = "iax", 57 .type = IDXD_TYPE_IAX, 58 .compl_size = sizeof(struct iax_completion_record), 59 .align = 64, 60 .dev_type = &iax_device_type, 61 .evl_cr_off = offsetof(struct iax_evl_entry, cr), 62 .user_submission_safe = false, /* See INTEL-SA-01084 security advisory */ 63 .cr_status_off = offsetof(struct iax_completion_record, status), 64 .cr_result_off = offsetof(struct iax_completion_record, error_code), 65 .load_device_defaults = idxd_load_iaa_device_defaults, 66 }, 67 }; 68 69 static struct pci_device_id idxd_pci_tbl[] = { 70 /* DSA ver 1.0 platforms */ 71 { PCI_DEVICE_DATA(INTEL, DSA_SPR0, &idxd_driver_data[IDXD_TYPE_DSA]) }, 72 /* DSA on GNR-D platforms */ 73 { PCI_DEVICE_DATA(INTEL, DSA_GNRD, &idxd_driver_data[IDXD_TYPE_DSA]) }, 74 /* DSA on DMR platforms */ 75 { PCI_DEVICE_DATA(INTEL, DSA_DMR, &idxd_driver_data[IDXD_TYPE_DSA]) }, 76 77 /* IAX ver 1.0 platforms */ 78 { PCI_DEVICE_DATA(INTEL, IAX_SPR0, &idxd_driver_data[IDXD_TYPE_IAX]) }, 79 /* IAA on DMR platforms */ 80 { PCI_DEVICE_DATA(INTEL, IAA_DMR, &idxd_driver_data[IDXD_TYPE_IAX]) }, 81 /* IAA PTL platforms */ 82 { PCI_DEVICE_DATA(INTEL, IAA_PTL, &idxd_driver_data[IDXD_TYPE_IAX]) }, 83 { 0, } 84 }; 85 MODULE_DEVICE_TABLE(pci, idxd_pci_tbl); 86 87 static int idxd_setup_interrupts(struct idxd_device *idxd) 88 { 89 struct pci_dev *pdev = idxd->pdev; 90 struct device *dev = &pdev->dev; 91 struct idxd_irq_entry *ie; 92 int i, msixcnt; 93 int rc = 0; 94 95 msixcnt = pci_msix_vec_count(pdev); 96 if (msixcnt < 0) { 97 dev_err(dev, "Not MSI-X interrupt capable.\n"); 98 return -ENOSPC; 99 } 100 idxd->irq_cnt = msixcnt; 101 102 rc = pci_alloc_irq_vectors(pdev, msixcnt, msixcnt, PCI_IRQ_MSIX); 103 if (rc != msixcnt) { 104 dev_err(dev, "Failed enabling %d MSIX entries: %d\n", msixcnt, rc); 105 return -ENOSPC; 106 } 107 dev_dbg(dev, "Enabled %d msix vectors\n", msixcnt); 108 109 110 ie = idxd_get_ie(idxd, 0); 111 ie->vector = pci_irq_vector(pdev, 0); 112 rc = request_threaded_irq(ie->vector, NULL, idxd_misc_thread, 0, "idxd-misc", ie); 113 if (rc < 0) { 114 dev_err(dev, "Failed to allocate misc interrupt.\n"); 115 goto err_misc_irq; 116 } 117 dev_dbg(dev, "Requested idxd-misc handler on msix vector %d\n", ie->vector); 118 119 for (i = 0; i < idxd->max_wqs; i++) { 120 int msix_idx = i + 1; 121 122 ie = idxd_get_ie(idxd, msix_idx); 123 ie->id = msix_idx; 124 ie->int_handle = INVALID_INT_HANDLE; 125 ie->pasid = IOMMU_PASID_INVALID; 126 127 spin_lock_init(&ie->list_lock); 128 init_llist_head(&ie->pending_llist); 129 INIT_LIST_HEAD(&ie->work_list); 130 } 131 132 idxd_unmask_error_interrupts(idxd); 133 return 0; 134 135 err_misc_irq: 136 idxd_mask_error_interrupts(idxd); 137 pci_free_irq_vectors(pdev); 138 dev_err(dev, "No usable interrupts\n"); 139 return rc; 140 } 141 142 static void idxd_cleanup_interrupts(struct idxd_device *idxd) 143 { 144 struct pci_dev *pdev = idxd->pdev; 145 struct idxd_irq_entry *ie; 146 int msixcnt; 147 148 msixcnt = pci_msix_vec_count(pdev); 149 if (msixcnt <= 0) 150 return; 151 152 ie = idxd_get_ie(idxd, 0); 153 idxd_mask_error_interrupts(idxd); 154 free_irq(ie->vector, ie); 155 pci_free_irq_vectors(pdev); 156 } 157 158 static int idxd_setup_wqs(struct idxd_device *idxd) 159 { 160 struct device *dev = &idxd->pdev->dev; 161 struct idxd_wq *wq; 162 struct device *conf_dev; 163 int i, rc; 164 165 idxd->wqs = kcalloc_node(idxd->max_wqs, sizeof(struct idxd_wq *), 166 GFP_KERNEL, dev_to_node(dev)); 167 if (!idxd->wqs) 168 return -ENOMEM; 169 170 idxd->wq_enable_map = bitmap_zalloc_node(idxd->max_wqs, GFP_KERNEL, dev_to_node(dev)); 171 if (!idxd->wq_enable_map) { 172 kfree(idxd->wqs); 173 return -ENOMEM; 174 } 175 176 for (i = 0; i < idxd->max_wqs; i++) { 177 wq = kzalloc_node(sizeof(*wq), GFP_KERNEL, dev_to_node(dev)); 178 if (!wq) { 179 rc = -ENOMEM; 180 goto err; 181 } 182 183 idxd_dev_set_type(&wq->idxd_dev, IDXD_DEV_WQ); 184 conf_dev = wq_confdev(wq); 185 wq->id = i; 186 wq->idxd = idxd; 187 device_initialize(wq_confdev(wq)); 188 conf_dev->parent = idxd_confdev(idxd); 189 conf_dev->bus = &dsa_bus_type; 190 conf_dev->type = &idxd_wq_device_type; 191 rc = dev_set_name(conf_dev, "wq%d.%d", idxd->id, wq->id); 192 if (rc < 0) { 193 put_device(conf_dev); 194 goto err; 195 } 196 197 mutex_init(&wq->wq_lock); 198 init_waitqueue_head(&wq->err_queue); 199 init_completion(&wq->wq_dead); 200 init_completion(&wq->wq_resurrect); 201 wq->max_xfer_bytes = WQ_DEFAULT_MAX_XFER; 202 idxd_wq_set_max_batch_size(idxd->data->type, wq, WQ_DEFAULT_MAX_BATCH); 203 wq->enqcmds_retries = IDXD_ENQCMDS_RETRIES; 204 wq->wqcfg = kzalloc_node(idxd->wqcfg_size, GFP_KERNEL, dev_to_node(dev)); 205 if (!wq->wqcfg) { 206 put_device(conf_dev); 207 rc = -ENOMEM; 208 goto err; 209 } 210 211 if (idxd->hw.wq_cap.op_config) { 212 wq->opcap_bmap = bitmap_zalloc(IDXD_MAX_OPCAP_BITS, GFP_KERNEL); 213 if (!wq->opcap_bmap) { 214 put_device(conf_dev); 215 rc = -ENOMEM; 216 goto err; 217 } 218 bitmap_copy(wq->opcap_bmap, idxd->opcap_bmap, IDXD_MAX_OPCAP_BITS); 219 } 220 mutex_init(&wq->uc_lock); 221 xa_init(&wq->upasid_xa); 222 idxd->wqs[i] = wq; 223 } 224 225 return 0; 226 227 err: 228 while (--i >= 0) { 229 wq = idxd->wqs[i]; 230 conf_dev = wq_confdev(wq); 231 put_device(conf_dev); 232 } 233 return rc; 234 } 235 236 static int idxd_setup_engines(struct idxd_device *idxd) 237 { 238 struct idxd_engine *engine; 239 struct device *dev = &idxd->pdev->dev; 240 struct device *conf_dev; 241 int i, rc; 242 243 idxd->engines = kcalloc_node(idxd->max_engines, sizeof(struct idxd_engine *), 244 GFP_KERNEL, dev_to_node(dev)); 245 if (!idxd->engines) 246 return -ENOMEM; 247 248 for (i = 0; i < idxd->max_engines; i++) { 249 engine = kzalloc_node(sizeof(*engine), GFP_KERNEL, dev_to_node(dev)); 250 if (!engine) { 251 rc = -ENOMEM; 252 goto err; 253 } 254 255 idxd_dev_set_type(&engine->idxd_dev, IDXD_DEV_ENGINE); 256 conf_dev = engine_confdev(engine); 257 engine->id = i; 258 engine->idxd = idxd; 259 device_initialize(conf_dev); 260 conf_dev->parent = idxd_confdev(idxd); 261 conf_dev->bus = &dsa_bus_type; 262 conf_dev->type = &idxd_engine_device_type; 263 rc = dev_set_name(conf_dev, "engine%d.%d", idxd->id, engine->id); 264 if (rc < 0) { 265 put_device(conf_dev); 266 goto err; 267 } 268 269 idxd->engines[i] = engine; 270 } 271 272 return 0; 273 274 err: 275 while (--i >= 0) { 276 engine = idxd->engines[i]; 277 conf_dev = engine_confdev(engine); 278 put_device(conf_dev); 279 } 280 return rc; 281 } 282 283 static int idxd_setup_groups(struct idxd_device *idxd) 284 { 285 struct device *dev = &idxd->pdev->dev; 286 struct device *conf_dev; 287 struct idxd_group *group; 288 int i, rc; 289 290 idxd->groups = kcalloc_node(idxd->max_groups, sizeof(struct idxd_group *), 291 GFP_KERNEL, dev_to_node(dev)); 292 if (!idxd->groups) 293 return -ENOMEM; 294 295 for (i = 0; i < idxd->max_groups; i++) { 296 group = kzalloc_node(sizeof(*group), GFP_KERNEL, dev_to_node(dev)); 297 if (!group) { 298 rc = -ENOMEM; 299 goto err; 300 } 301 302 idxd_dev_set_type(&group->idxd_dev, IDXD_DEV_GROUP); 303 conf_dev = group_confdev(group); 304 group->id = i; 305 group->idxd = idxd; 306 device_initialize(conf_dev); 307 conf_dev->parent = idxd_confdev(idxd); 308 conf_dev->bus = &dsa_bus_type; 309 conf_dev->type = &idxd_group_device_type; 310 rc = dev_set_name(conf_dev, "group%d.%d", idxd->id, group->id); 311 if (rc < 0) { 312 put_device(conf_dev); 313 goto err; 314 } 315 316 idxd->groups[i] = group; 317 if (idxd->hw.version <= DEVICE_VERSION_2 && !tc_override) { 318 group->tc_a = 1; 319 group->tc_b = 1; 320 } else { 321 group->tc_a = -1; 322 group->tc_b = -1; 323 } 324 /* 325 * The default value is the same as the value of 326 * total read buffers in GRPCAP. 327 */ 328 group->rdbufs_allowed = idxd->max_rdbufs; 329 } 330 331 return 0; 332 333 err: 334 while (--i >= 0) { 335 group = idxd->groups[i]; 336 put_device(group_confdev(group)); 337 } 338 return rc; 339 } 340 341 static void idxd_cleanup_internals(struct idxd_device *idxd) 342 { 343 int i; 344 345 for (i = 0; i < idxd->max_groups; i++) 346 put_device(group_confdev(idxd->groups[i])); 347 for (i = 0; i < idxd->max_engines; i++) 348 put_device(engine_confdev(idxd->engines[i])); 349 for (i = 0; i < idxd->max_wqs; i++) 350 put_device(wq_confdev(idxd->wqs[i])); 351 destroy_workqueue(idxd->wq); 352 } 353 354 static int idxd_init_evl(struct idxd_device *idxd) 355 { 356 struct device *dev = &idxd->pdev->dev; 357 unsigned int evl_cache_size; 358 struct idxd_evl *evl; 359 const char *idxd_name; 360 361 if (idxd->hw.gen_cap.evl_support == 0) 362 return 0; 363 364 evl = kzalloc_node(sizeof(*evl), GFP_KERNEL, dev_to_node(dev)); 365 if (!evl) 366 return -ENOMEM; 367 368 mutex_init(&evl->lock); 369 evl->size = IDXD_EVL_SIZE_MIN; 370 371 idxd_name = dev_name(idxd_confdev(idxd)); 372 evl_cache_size = sizeof(struct idxd_evl_fault) + evl_ent_size(idxd); 373 /* 374 * Since completion record in evl_cache will be copied to user 375 * when handling completion record page fault, need to create 376 * the cache suitable for user copy. 377 */ 378 idxd->evl_cache = kmem_cache_create_usercopy(idxd_name, evl_cache_size, 379 0, 0, 0, evl_cache_size, 380 NULL); 381 if (!idxd->evl_cache) { 382 kfree(evl); 383 return -ENOMEM; 384 } 385 386 idxd->evl = evl; 387 return 0; 388 } 389 390 static int idxd_setup_internals(struct idxd_device *idxd) 391 { 392 struct device *dev = &idxd->pdev->dev; 393 int rc, i; 394 395 init_waitqueue_head(&idxd->cmd_waitq); 396 397 rc = idxd_setup_wqs(idxd); 398 if (rc < 0) 399 goto err_wqs; 400 401 rc = idxd_setup_engines(idxd); 402 if (rc < 0) 403 goto err_engine; 404 405 rc = idxd_setup_groups(idxd); 406 if (rc < 0) 407 goto err_group; 408 409 idxd->wq = create_workqueue(dev_name(dev)); 410 if (!idxd->wq) { 411 rc = -ENOMEM; 412 goto err_wkq_create; 413 } 414 415 rc = idxd_init_evl(idxd); 416 if (rc < 0) 417 goto err_evl; 418 419 return 0; 420 421 err_evl: 422 destroy_workqueue(idxd->wq); 423 err_wkq_create: 424 for (i = 0; i < idxd->max_groups; i++) 425 put_device(group_confdev(idxd->groups[i])); 426 err_group: 427 for (i = 0; i < idxd->max_engines; i++) 428 put_device(engine_confdev(idxd->engines[i])); 429 err_engine: 430 for (i = 0; i < idxd->max_wqs; i++) 431 put_device(wq_confdev(idxd->wqs[i])); 432 err_wqs: 433 return rc; 434 } 435 436 static void idxd_read_table_offsets(struct idxd_device *idxd) 437 { 438 union offsets_reg offsets; 439 struct device *dev = &idxd->pdev->dev; 440 441 offsets.bits[0] = ioread64(idxd->reg_base + IDXD_TABLE_OFFSET); 442 offsets.bits[1] = ioread64(idxd->reg_base + IDXD_TABLE_OFFSET + sizeof(u64)); 443 idxd->grpcfg_offset = offsets.grpcfg * IDXD_TABLE_MULT; 444 dev_dbg(dev, "IDXD Group Config Offset: %#x\n", idxd->grpcfg_offset); 445 idxd->wqcfg_offset = offsets.wqcfg * IDXD_TABLE_MULT; 446 dev_dbg(dev, "IDXD Work Queue Config Offset: %#x\n", idxd->wqcfg_offset); 447 idxd->msix_perm_offset = offsets.msix_perm * IDXD_TABLE_MULT; 448 dev_dbg(dev, "IDXD MSIX Permission Offset: %#x\n", idxd->msix_perm_offset); 449 idxd->perfmon_offset = offsets.perfmon * IDXD_TABLE_MULT; 450 dev_dbg(dev, "IDXD Perfmon Offset: %#x\n", idxd->perfmon_offset); 451 } 452 453 void multi_u64_to_bmap(unsigned long *bmap, u64 *val, int count) 454 { 455 int i, j, nr; 456 457 for (i = 0, nr = 0; i < count; i++) { 458 for (j = 0; j < BITS_PER_LONG_LONG; j++) { 459 if (val[i] & BIT(j)) 460 set_bit(nr, bmap); 461 nr++; 462 } 463 } 464 } 465 466 static void idxd_read_caps(struct idxd_device *idxd) 467 { 468 struct device *dev = &idxd->pdev->dev; 469 int i; 470 471 /* reading generic capabilities */ 472 idxd->hw.gen_cap.bits = ioread64(idxd->reg_base + IDXD_GENCAP_OFFSET); 473 dev_dbg(dev, "gen_cap: %#llx\n", idxd->hw.gen_cap.bits); 474 475 if (idxd->hw.gen_cap.cmd_cap) { 476 idxd->hw.cmd_cap = ioread32(idxd->reg_base + IDXD_CMDCAP_OFFSET); 477 dev_dbg(dev, "cmd_cap: %#x\n", idxd->hw.cmd_cap); 478 } 479 480 /* reading command capabilities */ 481 if (idxd->hw.cmd_cap & BIT(IDXD_CMD_REQUEST_INT_HANDLE)) 482 idxd->request_int_handles = true; 483 484 idxd->max_xfer_bytes = 1ULL << idxd->hw.gen_cap.max_xfer_shift; 485 dev_dbg(dev, "max xfer size: %llu bytes\n", idxd->max_xfer_bytes); 486 idxd_set_max_batch_size(idxd->data->type, idxd, 1U << idxd->hw.gen_cap.max_batch_shift); 487 dev_dbg(dev, "max batch size: %u\n", idxd->max_batch_size); 488 if (idxd->hw.gen_cap.config_en) 489 set_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags); 490 491 /* reading group capabilities */ 492 idxd->hw.group_cap.bits = 493 ioread64(idxd->reg_base + IDXD_GRPCAP_OFFSET); 494 dev_dbg(dev, "group_cap: %#llx\n", idxd->hw.group_cap.bits); 495 idxd->max_groups = idxd->hw.group_cap.num_groups; 496 dev_dbg(dev, "max groups: %u\n", idxd->max_groups); 497 idxd->max_rdbufs = idxd->hw.group_cap.total_rdbufs; 498 dev_dbg(dev, "max read buffers: %u\n", idxd->max_rdbufs); 499 idxd->nr_rdbufs = idxd->max_rdbufs; 500 501 /* read engine capabilities */ 502 idxd->hw.engine_cap.bits = 503 ioread64(idxd->reg_base + IDXD_ENGCAP_OFFSET); 504 dev_dbg(dev, "engine_cap: %#llx\n", idxd->hw.engine_cap.bits); 505 idxd->max_engines = idxd->hw.engine_cap.num_engines; 506 dev_dbg(dev, "max engines: %u\n", idxd->max_engines); 507 508 /* read workqueue capabilities */ 509 idxd->hw.wq_cap.bits = ioread64(idxd->reg_base + IDXD_WQCAP_OFFSET); 510 dev_dbg(dev, "wq_cap: %#llx\n", idxd->hw.wq_cap.bits); 511 idxd->max_wq_size = idxd->hw.wq_cap.total_wq_size; 512 dev_dbg(dev, "total workqueue size: %u\n", idxd->max_wq_size); 513 idxd->max_wqs = idxd->hw.wq_cap.num_wqs; 514 dev_dbg(dev, "max workqueues: %u\n", idxd->max_wqs); 515 idxd->wqcfg_size = 1 << (idxd->hw.wq_cap.wqcfg_size + IDXD_WQCFG_MIN); 516 dev_dbg(dev, "wqcfg size: %u\n", idxd->wqcfg_size); 517 518 /* reading operation capabilities */ 519 for (i = 0; i < 4; i++) { 520 idxd->hw.opcap.bits[i] = ioread64(idxd->reg_base + 521 IDXD_OPCAP_OFFSET + i * sizeof(u64)); 522 dev_dbg(dev, "opcap[%d]: %#llx\n", i, idxd->hw.opcap.bits[i]); 523 } 524 multi_u64_to_bmap(idxd->opcap_bmap, &idxd->hw.opcap.bits[0], 4); 525 526 /* read iaa cap */ 527 if (idxd->data->type == IDXD_TYPE_IAX && idxd->hw.version >= DEVICE_VERSION_2) 528 idxd->hw.iaa_cap.bits = ioread64(idxd->reg_base + IDXD_IAACAP_OFFSET); 529 } 530 531 static struct idxd_device *idxd_alloc(struct pci_dev *pdev, struct idxd_driver_data *data) 532 { 533 struct device *dev = &pdev->dev; 534 struct device *conf_dev; 535 struct idxd_device *idxd; 536 int rc; 537 538 idxd = kzalloc_node(sizeof(*idxd), GFP_KERNEL, dev_to_node(dev)); 539 if (!idxd) 540 return NULL; 541 542 conf_dev = idxd_confdev(idxd); 543 idxd->pdev = pdev; 544 idxd->data = data; 545 idxd_dev_set_type(&idxd->idxd_dev, idxd->data->type); 546 idxd->id = ida_alloc(&idxd_ida, GFP_KERNEL); 547 if (idxd->id < 0) 548 return NULL; 549 550 idxd->opcap_bmap = bitmap_zalloc_node(IDXD_MAX_OPCAP_BITS, GFP_KERNEL, dev_to_node(dev)); 551 if (!idxd->opcap_bmap) { 552 ida_free(&idxd_ida, idxd->id); 553 return NULL; 554 } 555 556 device_initialize(conf_dev); 557 conf_dev->parent = dev; 558 conf_dev->bus = &dsa_bus_type; 559 conf_dev->type = idxd->data->dev_type; 560 rc = dev_set_name(conf_dev, "%s%d", idxd->data->name_prefix, idxd->id); 561 if (rc < 0) { 562 put_device(conf_dev); 563 return NULL; 564 } 565 566 spin_lock_init(&idxd->dev_lock); 567 spin_lock_init(&idxd->cmd_lock); 568 569 return idxd; 570 } 571 572 static int idxd_enable_system_pasid(struct idxd_device *idxd) 573 { 574 struct pci_dev *pdev = idxd->pdev; 575 struct device *dev = &pdev->dev; 576 struct iommu_domain *domain; 577 ioasid_t pasid; 578 int ret; 579 580 /* 581 * Attach a global PASID to the DMA domain so that we can use ENQCMDS 582 * to submit work on buffers mapped by DMA API. 583 */ 584 domain = iommu_get_domain_for_dev(dev); 585 if (!domain) 586 return -EPERM; 587 588 pasid = iommu_alloc_global_pasid(dev); 589 if (pasid == IOMMU_PASID_INVALID) 590 return -ENOSPC; 591 592 /* 593 * DMA domain is owned by the driver, it should support all valid 594 * types such as DMA-FQ, identity, etc. 595 */ 596 ret = iommu_attach_device_pasid(domain, dev, pasid, NULL); 597 if (ret) { 598 dev_err(dev, "failed to attach device pasid %d, domain type %d", 599 pasid, domain->type); 600 iommu_free_global_pasid(pasid); 601 return ret; 602 } 603 604 /* Since we set user privilege for kernel DMA, enable completion IRQ */ 605 idxd_set_user_intr(idxd, 1); 606 idxd->pasid = pasid; 607 608 return ret; 609 } 610 611 static void idxd_disable_system_pasid(struct idxd_device *idxd) 612 { 613 struct pci_dev *pdev = idxd->pdev; 614 struct device *dev = &pdev->dev; 615 struct iommu_domain *domain; 616 617 domain = iommu_get_domain_for_dev(dev); 618 if (!domain) 619 return; 620 621 iommu_detach_device_pasid(domain, dev, idxd->pasid); 622 iommu_free_global_pasid(idxd->pasid); 623 624 idxd_set_user_intr(idxd, 0); 625 idxd->sva = NULL; 626 idxd->pasid = IOMMU_PASID_INVALID; 627 } 628 629 static int idxd_enable_sva(struct pci_dev *pdev) 630 { 631 int ret; 632 633 ret = iommu_dev_enable_feature(&pdev->dev, IOMMU_DEV_FEAT_IOPF); 634 if (ret) 635 return ret; 636 637 ret = iommu_dev_enable_feature(&pdev->dev, IOMMU_DEV_FEAT_SVA); 638 if (ret) 639 iommu_dev_disable_feature(&pdev->dev, IOMMU_DEV_FEAT_IOPF); 640 641 return ret; 642 } 643 644 static void idxd_disable_sva(struct pci_dev *pdev) 645 { 646 iommu_dev_disable_feature(&pdev->dev, IOMMU_DEV_FEAT_SVA); 647 iommu_dev_disable_feature(&pdev->dev, IOMMU_DEV_FEAT_IOPF); 648 } 649 650 static int idxd_probe(struct idxd_device *idxd) 651 { 652 struct pci_dev *pdev = idxd->pdev; 653 struct device *dev = &pdev->dev; 654 int rc; 655 656 dev_dbg(dev, "%s entered and resetting device\n", __func__); 657 rc = idxd_device_init_reset(idxd); 658 if (rc < 0) 659 return rc; 660 661 dev_dbg(dev, "IDXD reset complete\n"); 662 663 if (IS_ENABLED(CONFIG_INTEL_IDXD_SVM) && sva) { 664 if (idxd_enable_sva(pdev)) { 665 dev_warn(dev, "Unable to turn on user SVA feature.\n"); 666 } else { 667 set_bit(IDXD_FLAG_USER_PASID_ENABLED, &idxd->flags); 668 669 rc = idxd_enable_system_pasid(idxd); 670 if (rc) 671 dev_warn(dev, "No in-kernel DMA with PASID. %d\n", rc); 672 else 673 set_bit(IDXD_FLAG_PASID_ENABLED, &idxd->flags); 674 } 675 } else if (!sva) { 676 dev_warn(dev, "User forced SVA off via module param.\n"); 677 } 678 679 idxd_read_caps(idxd); 680 idxd_read_table_offsets(idxd); 681 682 rc = idxd_setup_internals(idxd); 683 if (rc) 684 goto err; 685 686 /* If the configs are readonly, then load them from device */ 687 if (!test_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags)) { 688 dev_dbg(dev, "Loading RO device config\n"); 689 rc = idxd_device_load_config(idxd); 690 if (rc < 0) 691 goto err_config; 692 } 693 694 rc = idxd_setup_interrupts(idxd); 695 if (rc) 696 goto err_config; 697 698 idxd->major = idxd_cdev_get_major(idxd); 699 700 rc = perfmon_pmu_init(idxd); 701 if (rc < 0) 702 dev_warn(dev, "Failed to initialize perfmon. No PMU support: %d\n", rc); 703 704 dev_dbg(dev, "IDXD device %d probed successfully\n", idxd->id); 705 return 0; 706 707 err_config: 708 idxd_cleanup_internals(idxd); 709 err: 710 if (device_pasid_enabled(idxd)) 711 idxd_disable_system_pasid(idxd); 712 if (device_user_pasid_enabled(idxd)) 713 idxd_disable_sva(pdev); 714 return rc; 715 } 716 717 static void idxd_cleanup(struct idxd_device *idxd) 718 { 719 perfmon_pmu_remove(idxd); 720 idxd_cleanup_interrupts(idxd); 721 idxd_cleanup_internals(idxd); 722 if (device_pasid_enabled(idxd)) 723 idxd_disable_system_pasid(idxd); 724 if (device_user_pasid_enabled(idxd)) 725 idxd_disable_sva(idxd->pdev); 726 } 727 728 /* 729 * Attach IDXD device to IDXD driver. 730 */ 731 static int idxd_bind(struct device_driver *drv, const char *buf) 732 { 733 const struct bus_type *bus = drv->bus; 734 struct device *dev; 735 int err = -ENODEV; 736 737 dev = bus_find_device_by_name(bus, NULL, buf); 738 if (dev) 739 err = device_driver_attach(drv, dev); 740 741 put_device(dev); 742 743 return err; 744 } 745 746 /* 747 * Detach IDXD device from driver. 748 */ 749 static void idxd_unbind(struct device_driver *drv, const char *buf) 750 { 751 const struct bus_type *bus = drv->bus; 752 struct device *dev; 753 754 dev = bus_find_device_by_name(bus, NULL, buf); 755 if (dev && dev->driver == drv) 756 device_release_driver(dev); 757 758 put_device(dev); 759 } 760 761 #define idxd_free_saved_configs(saved_configs, count) \ 762 do { \ 763 int i; \ 764 \ 765 for (i = 0; i < (count); i++) \ 766 kfree(saved_configs[i]); \ 767 } while (0) 768 769 static void idxd_free_saved(struct idxd_group **saved_groups, 770 struct idxd_engine **saved_engines, 771 struct idxd_wq **saved_wqs, 772 struct idxd_device *idxd) 773 { 774 if (saved_groups) 775 idxd_free_saved_configs(saved_groups, idxd->max_groups); 776 if (saved_engines) 777 idxd_free_saved_configs(saved_engines, idxd->max_engines); 778 if (saved_wqs) 779 idxd_free_saved_configs(saved_wqs, idxd->max_wqs); 780 } 781 782 /* 783 * Save IDXD device configurations including engines, groups, wqs etc. 784 * The saved configurations can be restored when needed. 785 */ 786 static int idxd_device_config_save(struct idxd_device *idxd, 787 struct idxd_saved_states *idxd_saved) 788 { 789 struct device *dev = &idxd->pdev->dev; 790 int i; 791 792 memcpy(&idxd_saved->saved_idxd, idxd, sizeof(*idxd)); 793 794 if (idxd->evl) { 795 memcpy(&idxd_saved->saved_evl, idxd->evl, 796 sizeof(struct idxd_evl)); 797 } 798 799 struct idxd_group **saved_groups __free(kfree) = 800 kcalloc_node(idxd->max_groups, 801 sizeof(struct idxd_group *), 802 GFP_KERNEL, dev_to_node(dev)); 803 if (!saved_groups) 804 return -ENOMEM; 805 806 for (i = 0; i < idxd->max_groups; i++) { 807 struct idxd_group *saved_group __free(kfree) = 808 kzalloc_node(sizeof(*saved_group), GFP_KERNEL, 809 dev_to_node(dev)); 810 811 if (!saved_group) { 812 /* Free saved groups */ 813 idxd_free_saved(saved_groups, NULL, NULL, idxd); 814 815 return -ENOMEM; 816 } 817 818 memcpy(saved_group, idxd->groups[i], sizeof(*saved_group)); 819 saved_groups[i] = no_free_ptr(saved_group); 820 } 821 822 struct idxd_engine **saved_engines = 823 kcalloc_node(idxd->max_engines, 824 sizeof(struct idxd_engine *), 825 GFP_KERNEL, dev_to_node(dev)); 826 if (!saved_engines) { 827 /* Free saved groups */ 828 idxd_free_saved(saved_groups, NULL, NULL, idxd); 829 830 return -ENOMEM; 831 } 832 for (i = 0; i < idxd->max_engines; i++) { 833 struct idxd_engine *saved_engine __free(kfree) = 834 kzalloc_node(sizeof(*saved_engine), GFP_KERNEL, 835 dev_to_node(dev)); 836 if (!saved_engine) { 837 /* Free saved groups and engines */ 838 idxd_free_saved(saved_groups, saved_engines, NULL, 839 idxd); 840 841 return -ENOMEM; 842 } 843 844 memcpy(saved_engine, idxd->engines[i], sizeof(*saved_engine)); 845 saved_engines[i] = no_free_ptr(saved_engine); 846 } 847 848 unsigned long *saved_wq_enable_map __free(bitmap) = 849 bitmap_zalloc_node(idxd->max_wqs, GFP_KERNEL, 850 dev_to_node(dev)); 851 if (!saved_wq_enable_map) { 852 /* Free saved groups and engines */ 853 idxd_free_saved(saved_groups, saved_engines, NULL, idxd); 854 855 return -ENOMEM; 856 } 857 858 bitmap_copy(saved_wq_enable_map, idxd->wq_enable_map, idxd->max_wqs); 859 860 struct idxd_wq **saved_wqs __free(kfree) = 861 kcalloc_node(idxd->max_wqs, sizeof(struct idxd_wq *), 862 GFP_KERNEL, dev_to_node(dev)); 863 if (!saved_wqs) { 864 /* Free saved groups and engines */ 865 idxd_free_saved(saved_groups, saved_engines, NULL, idxd); 866 867 return -ENOMEM; 868 } 869 870 for (i = 0; i < idxd->max_wqs; i++) { 871 struct idxd_wq *saved_wq __free(kfree) = 872 kzalloc_node(sizeof(*saved_wq), GFP_KERNEL, 873 dev_to_node(dev)); 874 struct idxd_wq *wq; 875 876 if (!saved_wq) { 877 /* Free saved groups, engines, and wqs */ 878 idxd_free_saved(saved_groups, saved_engines, saved_wqs, 879 idxd); 880 881 return -ENOMEM; 882 } 883 884 if (!test_bit(i, saved_wq_enable_map)) 885 continue; 886 887 wq = idxd->wqs[i]; 888 mutex_lock(&wq->wq_lock); 889 memcpy(saved_wq, wq, sizeof(*saved_wq)); 890 saved_wqs[i] = no_free_ptr(saved_wq); 891 mutex_unlock(&wq->wq_lock); 892 } 893 894 /* Save configurations */ 895 idxd_saved->saved_groups = no_free_ptr(saved_groups); 896 idxd_saved->saved_engines = no_free_ptr(saved_engines); 897 idxd_saved->saved_wq_enable_map = no_free_ptr(saved_wq_enable_map); 898 idxd_saved->saved_wqs = no_free_ptr(saved_wqs); 899 900 return 0; 901 } 902 903 /* 904 * Restore IDXD device configurations including engines, groups, wqs etc 905 * that were saved before. 906 */ 907 static void idxd_device_config_restore(struct idxd_device *idxd, 908 struct idxd_saved_states *idxd_saved) 909 { 910 struct idxd_evl *saved_evl = &idxd_saved->saved_evl; 911 int i; 912 913 idxd->rdbuf_limit = idxd_saved->saved_idxd.rdbuf_limit; 914 915 idxd->evl->size = saved_evl->size; 916 917 for (i = 0; i < idxd->max_groups; i++) { 918 struct idxd_group *saved_group, *group; 919 920 saved_group = idxd_saved->saved_groups[i]; 921 group = idxd->groups[i]; 922 923 group->rdbufs_allowed = saved_group->rdbufs_allowed; 924 group->rdbufs_reserved = saved_group->rdbufs_reserved; 925 group->tc_a = saved_group->tc_a; 926 group->tc_b = saved_group->tc_b; 927 group->use_rdbuf_limit = saved_group->use_rdbuf_limit; 928 929 kfree(saved_group); 930 } 931 kfree(idxd_saved->saved_groups); 932 933 for (i = 0; i < idxd->max_engines; i++) { 934 struct idxd_engine *saved_engine, *engine; 935 936 saved_engine = idxd_saved->saved_engines[i]; 937 engine = idxd->engines[i]; 938 939 engine->group = saved_engine->group; 940 941 kfree(saved_engine); 942 } 943 kfree(idxd_saved->saved_engines); 944 945 bitmap_copy(idxd->wq_enable_map, idxd_saved->saved_wq_enable_map, 946 idxd->max_wqs); 947 bitmap_free(idxd_saved->saved_wq_enable_map); 948 949 for (i = 0; i < idxd->max_wqs; i++) { 950 struct idxd_wq *saved_wq, *wq; 951 size_t len; 952 953 if (!test_bit(i, idxd->wq_enable_map)) 954 continue; 955 956 saved_wq = idxd_saved->saved_wqs[i]; 957 wq = idxd->wqs[i]; 958 959 mutex_lock(&wq->wq_lock); 960 961 wq->group = saved_wq->group; 962 wq->flags = saved_wq->flags; 963 wq->threshold = saved_wq->threshold; 964 wq->size = saved_wq->size; 965 wq->priority = saved_wq->priority; 966 wq->type = saved_wq->type; 967 len = strlen(saved_wq->name) + 1; 968 strscpy(wq->name, saved_wq->name, len); 969 wq->max_xfer_bytes = saved_wq->max_xfer_bytes; 970 wq->max_batch_size = saved_wq->max_batch_size; 971 wq->enqcmds_retries = saved_wq->enqcmds_retries; 972 wq->descs = saved_wq->descs; 973 wq->idxd_chan = saved_wq->idxd_chan; 974 len = strlen(saved_wq->driver_name) + 1; 975 strscpy(wq->driver_name, saved_wq->driver_name, len); 976 977 mutex_unlock(&wq->wq_lock); 978 979 kfree(saved_wq); 980 } 981 982 kfree(idxd_saved->saved_wqs); 983 } 984 985 static void idxd_reset_prepare(struct pci_dev *pdev) 986 { 987 struct idxd_device *idxd = pci_get_drvdata(pdev); 988 struct device *dev = &idxd->pdev->dev; 989 const char *idxd_name; 990 int rc; 991 992 dev = &idxd->pdev->dev; 993 idxd_name = dev_name(idxd_confdev(idxd)); 994 995 struct idxd_saved_states *idxd_saved __free(kfree) = 996 kzalloc_node(sizeof(*idxd_saved), GFP_KERNEL, 997 dev_to_node(&pdev->dev)); 998 if (!idxd_saved) { 999 dev_err(dev, "HALT: no memory\n"); 1000 1001 return; 1002 } 1003 1004 /* Save IDXD configurations. */ 1005 rc = idxd_device_config_save(idxd, idxd_saved); 1006 if (rc < 0) { 1007 dev_err(dev, "HALT: cannot save %s configs\n", idxd_name); 1008 1009 return; 1010 } 1011 1012 idxd->idxd_saved = no_free_ptr(idxd_saved); 1013 1014 /* Save PCI device state. */ 1015 pci_save_state(idxd->pdev); 1016 } 1017 1018 static void idxd_reset_done(struct pci_dev *pdev) 1019 { 1020 struct idxd_device *idxd = pci_get_drvdata(pdev); 1021 const char *idxd_name; 1022 struct device *dev; 1023 int rc, i; 1024 1025 if (!idxd->idxd_saved) 1026 return; 1027 1028 dev = &idxd->pdev->dev; 1029 idxd_name = dev_name(idxd_confdev(idxd)); 1030 1031 /* Restore PCI device state. */ 1032 pci_restore_state(idxd->pdev); 1033 1034 /* Unbind idxd device from driver. */ 1035 idxd_unbind(&idxd_drv.drv, idxd_name); 1036 1037 /* 1038 * Probe PCI device without allocating or changing 1039 * idxd software data which keeps the same as before FLR. 1040 */ 1041 idxd_pci_probe_alloc(idxd, NULL, NULL); 1042 1043 /* Restore IDXD configurations. */ 1044 idxd_device_config_restore(idxd, idxd->idxd_saved); 1045 1046 /* Re-configure IDXD device if allowed. */ 1047 if (test_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags)) { 1048 rc = idxd_device_config(idxd); 1049 if (rc < 0) { 1050 dev_err(dev, "HALT: %s config fails\n", idxd_name); 1051 goto out; 1052 } 1053 } 1054 1055 /* Bind IDXD device to driver. */ 1056 rc = idxd_bind(&idxd_drv.drv, idxd_name); 1057 if (rc < 0) { 1058 dev_err(dev, "HALT: binding %s to driver fails\n", idxd_name); 1059 goto out; 1060 } 1061 1062 /* Bind enabled wq in the IDXD device to driver. */ 1063 for (i = 0; i < idxd->max_wqs; i++) { 1064 if (test_bit(i, idxd->wq_enable_map)) { 1065 struct idxd_wq *wq = idxd->wqs[i]; 1066 char wq_name[32]; 1067 1068 wq->state = IDXD_WQ_DISABLED; 1069 sprintf(wq_name, "wq%d.%d", idxd->id, wq->id); 1070 /* 1071 * Bind to user driver depending on wq type. 1072 * 1073 * Currently only support user type WQ. Will support 1074 * kernel type WQ in the future. 1075 */ 1076 if (wq->type == IDXD_WQT_USER) 1077 rc = idxd_bind(&idxd_user_drv.drv, wq_name); 1078 else 1079 rc = -EINVAL; 1080 if (rc < 0) { 1081 clear_bit(i, idxd->wq_enable_map); 1082 dev_err(dev, 1083 "HALT: unable to re-enable wq %s\n", 1084 dev_name(wq_confdev(wq))); 1085 } 1086 } 1087 } 1088 out: 1089 kfree(idxd->idxd_saved); 1090 } 1091 1092 static const struct pci_error_handlers idxd_error_handler = { 1093 .reset_prepare = idxd_reset_prepare, 1094 .reset_done = idxd_reset_done, 1095 }; 1096 1097 /* 1098 * Probe idxd PCI device. 1099 * If idxd is not given, need to allocate idxd and set up its data. 1100 * 1101 * If idxd is given, idxd was allocated and setup already. Just need to 1102 * configure device without re-allocating and re-configuring idxd data. 1103 * This is useful for recovering from FLR. 1104 */ 1105 int idxd_pci_probe_alloc(struct idxd_device *idxd, struct pci_dev *pdev, 1106 const struct pci_device_id *id) 1107 { 1108 bool alloc_idxd = idxd ? false : true; 1109 struct idxd_driver_data *data; 1110 struct device *dev; 1111 int rc; 1112 1113 pdev = idxd ? idxd->pdev : pdev; 1114 dev = &pdev->dev; 1115 data = id ? (struct idxd_driver_data *)id->driver_data : NULL; 1116 rc = pci_enable_device(pdev); 1117 if (rc) 1118 return rc; 1119 1120 if (alloc_idxd) { 1121 dev_dbg(dev, "Alloc IDXD context\n"); 1122 idxd = idxd_alloc(pdev, data); 1123 if (!idxd) { 1124 rc = -ENOMEM; 1125 goto err_idxd_alloc; 1126 } 1127 1128 dev_dbg(dev, "Mapping BARs\n"); 1129 idxd->reg_base = pci_iomap(pdev, IDXD_MMIO_BAR, 0); 1130 if (!idxd->reg_base) { 1131 rc = -ENOMEM; 1132 goto err_iomap; 1133 } 1134 1135 dev_dbg(dev, "Set DMA masks\n"); 1136 rc = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)); 1137 if (rc) 1138 goto err; 1139 } 1140 1141 dev_dbg(dev, "Set PCI master\n"); 1142 pci_set_master(pdev); 1143 pci_set_drvdata(pdev, idxd); 1144 1145 if (alloc_idxd) { 1146 idxd->hw.version = ioread32(idxd->reg_base + IDXD_VER_OFFSET); 1147 rc = idxd_probe(idxd); 1148 if (rc) { 1149 dev_err(dev, "Intel(R) IDXD DMA Engine init failed\n"); 1150 goto err; 1151 } 1152 1153 if (data->load_device_defaults) { 1154 rc = data->load_device_defaults(idxd); 1155 if (rc) 1156 dev_warn(dev, "IDXD loading device defaults failed\n"); 1157 } 1158 1159 rc = idxd_register_devices(idxd); 1160 if (rc) { 1161 dev_err(dev, "IDXD sysfs setup failed\n"); 1162 goto err_dev_register; 1163 } 1164 1165 rc = idxd_device_init_debugfs(idxd); 1166 if (rc) 1167 dev_warn(dev, "IDXD debugfs failed to setup\n"); 1168 } 1169 1170 if (!alloc_idxd) { 1171 /* Release interrupts in the IDXD device. */ 1172 idxd_cleanup_interrupts(idxd); 1173 1174 /* Re-enable interrupts in the IDXD device. */ 1175 rc = idxd_setup_interrupts(idxd); 1176 if (rc) 1177 dev_warn(dev, "IDXD interrupts failed to setup\n"); 1178 } 1179 1180 dev_info(&pdev->dev, "Intel(R) Accelerator Device (v%x)\n", 1181 idxd->hw.version); 1182 1183 if (data) 1184 idxd->user_submission_safe = data->user_submission_safe; 1185 1186 return 0; 1187 1188 err_dev_register: 1189 idxd_cleanup(idxd); 1190 err: 1191 pci_iounmap(pdev, idxd->reg_base); 1192 err_iomap: 1193 put_device(idxd_confdev(idxd)); 1194 err_idxd_alloc: 1195 pci_disable_device(pdev); 1196 return rc; 1197 } 1198 1199 static int idxd_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id) 1200 { 1201 return idxd_pci_probe_alloc(NULL, pdev, id); 1202 } 1203 1204 void idxd_wqs_quiesce(struct idxd_device *idxd) 1205 { 1206 struct idxd_wq *wq; 1207 int i; 1208 1209 for (i = 0; i < idxd->max_wqs; i++) { 1210 wq = idxd->wqs[i]; 1211 if (wq->state == IDXD_WQ_ENABLED && wq->type == IDXD_WQT_KERNEL) 1212 idxd_wq_quiesce(wq); 1213 } 1214 } 1215 1216 static void idxd_shutdown(struct pci_dev *pdev) 1217 { 1218 struct idxd_device *idxd = pci_get_drvdata(pdev); 1219 struct idxd_irq_entry *irq_entry; 1220 int rc; 1221 1222 rc = idxd_device_disable(idxd); 1223 if (rc) 1224 dev_err(&pdev->dev, "Disabling device failed\n"); 1225 1226 irq_entry = &idxd->ie; 1227 synchronize_irq(irq_entry->vector); 1228 idxd_mask_error_interrupts(idxd); 1229 flush_workqueue(idxd->wq); 1230 } 1231 1232 static void idxd_remove(struct pci_dev *pdev) 1233 { 1234 struct idxd_device *idxd = pci_get_drvdata(pdev); 1235 struct idxd_irq_entry *irq_entry; 1236 1237 idxd_unregister_devices(idxd); 1238 /* 1239 * When ->release() is called for the idxd->conf_dev, it frees all the memory related 1240 * to the idxd context. The driver still needs those bits in order to do the rest of 1241 * the cleanup. However, we do need to unbound the idxd sub-driver. So take a ref 1242 * on the device here to hold off the freeing while allowing the idxd sub-driver 1243 * to unbind. 1244 */ 1245 get_device(idxd_confdev(idxd)); 1246 device_unregister(idxd_confdev(idxd)); 1247 idxd_shutdown(pdev); 1248 if (device_pasid_enabled(idxd)) 1249 idxd_disable_system_pasid(idxd); 1250 idxd_device_remove_debugfs(idxd); 1251 1252 irq_entry = idxd_get_ie(idxd, 0); 1253 free_irq(irq_entry->vector, irq_entry); 1254 pci_free_irq_vectors(pdev); 1255 pci_iounmap(pdev, idxd->reg_base); 1256 if (device_user_pasid_enabled(idxd)) 1257 idxd_disable_sva(pdev); 1258 pci_disable_device(pdev); 1259 destroy_workqueue(idxd->wq); 1260 perfmon_pmu_remove(idxd); 1261 put_device(idxd_confdev(idxd)); 1262 } 1263 1264 static struct pci_driver idxd_pci_driver = { 1265 .name = DRV_NAME, 1266 .id_table = idxd_pci_tbl, 1267 .probe = idxd_pci_probe, 1268 .remove = idxd_remove, 1269 .shutdown = idxd_shutdown, 1270 .err_handler = &idxd_error_handler, 1271 }; 1272 1273 static int __init idxd_init_module(void) 1274 { 1275 int err; 1276 1277 /* 1278 * If the CPU does not support MOVDIR64B or ENQCMDS, there's no point in 1279 * enumerating the device. We can not utilize it. 1280 */ 1281 if (!cpu_feature_enabled(X86_FEATURE_MOVDIR64B)) { 1282 pr_warn("idxd driver failed to load without MOVDIR64B.\n"); 1283 return -ENODEV; 1284 } 1285 1286 if (!cpu_feature_enabled(X86_FEATURE_ENQCMD)) 1287 pr_warn("Platform does not have ENQCMD(S) support.\n"); 1288 else 1289 support_enqcmd = true; 1290 1291 err = idxd_driver_register(&idxd_drv); 1292 if (err < 0) 1293 goto err_idxd_driver_register; 1294 1295 err = idxd_driver_register(&idxd_dmaengine_drv); 1296 if (err < 0) 1297 goto err_idxd_dmaengine_driver_register; 1298 1299 err = idxd_driver_register(&idxd_user_drv); 1300 if (err < 0) 1301 goto err_idxd_user_driver_register; 1302 1303 err = idxd_cdev_register(); 1304 if (err) 1305 goto err_cdev_register; 1306 1307 err = idxd_init_debugfs(); 1308 if (err) 1309 goto err_debugfs; 1310 1311 err = pci_register_driver(&idxd_pci_driver); 1312 if (err) 1313 goto err_pci_register; 1314 1315 return 0; 1316 1317 err_pci_register: 1318 idxd_remove_debugfs(); 1319 err_debugfs: 1320 idxd_cdev_remove(); 1321 err_cdev_register: 1322 idxd_driver_unregister(&idxd_user_drv); 1323 err_idxd_user_driver_register: 1324 idxd_driver_unregister(&idxd_dmaengine_drv); 1325 err_idxd_dmaengine_driver_register: 1326 idxd_driver_unregister(&idxd_drv); 1327 err_idxd_driver_register: 1328 return err; 1329 } 1330 module_init(idxd_init_module); 1331 1332 static void __exit idxd_exit_module(void) 1333 { 1334 idxd_driver_unregister(&idxd_user_drv); 1335 idxd_driver_unregister(&idxd_dmaengine_drv); 1336 idxd_driver_unregister(&idxd_drv); 1337 pci_unregister_driver(&idxd_pci_driver); 1338 idxd_cdev_remove(); 1339 idxd_remove_debugfs(); 1340 } 1341 module_exit(idxd_exit_module); 1342