Dave Jiang | bfe1d56 | 2020-01-21 16:43:59 -0700 | [diff] [blame] | 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/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 Jiang | 8e50d39 | 2020-10-27 10:34:35 -0700 | [diff] [blame] | 17 | #include <linux/intel-svm.h> |
| 18 | #include <linux/iommu.h> |
Dave Jiang | bfe1d56 | 2020-01-21 16:43:59 -0700 | [diff] [blame] | 19 | #include <uapi/linux/idxd.h> |
Dave Jiang | 8f47d1a | 2020-01-21 16:44:23 -0700 | [diff] [blame] | 20 | #include <linux/dmaengine.h> |
| 21 | #include "../dmaengine.h" |
Dave Jiang | bfe1d56 | 2020-01-21 16:43:59 -0700 | [diff] [blame] | 22 | #include "registers.h" |
| 23 | #include "idxd.h" |
| 24 | |
| 25 | MODULE_VERSION(IDXD_DRIVER_VERSION); |
| 26 | MODULE_LICENSE("GPL v2"); |
| 27 | MODULE_AUTHOR("Intel Corporation"); |
| 28 | |
| 29 | #define DRV_NAME "idxd" |
| 30 | |
Dave Jiang | 8e50d39 | 2020-10-27 10:34:35 -0700 | [diff] [blame] | 31 | bool support_enqcmd; |
| 32 | |
Dave Jiang | bfe1d56 | 2020-01-21 16:43:59 -0700 | [diff] [blame] | 33 | static struct idr idxd_idrs[IDXD_TYPE_MAX]; |
| 34 | static struct mutex idxd_idr_lock; |
| 35 | |
| 36 | static struct pci_device_id idxd_pci_tbl[] = { |
| 37 | /* DSA ver 1.0 platforms */ |
| 38 | { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_DSA_SPR0) }, |
| 39 | { 0, } |
| 40 | }; |
| 41 | MODULE_DEVICE_TABLE(pci, idxd_pci_tbl); |
| 42 | |
| 43 | static char *idxd_name[] = { |
| 44 | "dsa", |
| 45 | }; |
| 46 | |
| 47 | const char *idxd_get_dev_name(struct idxd_device *idxd) |
| 48 | { |
| 49 | return idxd_name[idxd->type]; |
| 50 | } |
| 51 | |
| 52 | static int idxd_setup_interrupts(struct idxd_device *idxd) |
| 53 | { |
| 54 | struct pci_dev *pdev = idxd->pdev; |
| 55 | struct device *dev = &pdev->dev; |
| 56 | struct msix_entry *msix; |
| 57 | struct idxd_irq_entry *irq_entry; |
| 58 | int i, msixcnt; |
| 59 | int rc = 0; |
Dave Jiang | 8e50d39 | 2020-10-27 10:34:35 -0700 | [diff] [blame] | 60 | union msix_perm mperm; |
Dave Jiang | bfe1d56 | 2020-01-21 16:43:59 -0700 | [diff] [blame] | 61 | |
| 62 | msixcnt = pci_msix_vec_count(pdev); |
| 63 | if (msixcnt < 0) { |
| 64 | dev_err(dev, "Not MSI-X interrupt capable.\n"); |
| 65 | goto err_no_irq; |
| 66 | } |
| 67 | |
| 68 | idxd->msix_entries = devm_kzalloc(dev, sizeof(struct msix_entry) * |
| 69 | msixcnt, GFP_KERNEL); |
| 70 | if (!idxd->msix_entries) { |
| 71 | rc = -ENOMEM; |
| 72 | goto err_no_irq; |
| 73 | } |
| 74 | |
| 75 | for (i = 0; i < msixcnt; i++) |
| 76 | idxd->msix_entries[i].entry = i; |
| 77 | |
| 78 | rc = pci_enable_msix_exact(pdev, idxd->msix_entries, msixcnt); |
| 79 | if (rc) { |
| 80 | dev_err(dev, "Failed enabling %d MSIX entries.\n", msixcnt); |
| 81 | goto err_no_irq; |
| 82 | } |
| 83 | dev_dbg(dev, "Enabled %d msix vectors\n", msixcnt); |
| 84 | |
| 85 | /* |
| 86 | * We implement 1 completion list per MSI-X entry except for |
| 87 | * entry 0, which is for errors and others. |
| 88 | */ |
| 89 | idxd->irq_entries = devm_kcalloc(dev, msixcnt, |
| 90 | sizeof(struct idxd_irq_entry), |
| 91 | GFP_KERNEL); |
| 92 | if (!idxd->irq_entries) { |
| 93 | rc = -ENOMEM; |
| 94 | goto err_no_irq; |
| 95 | } |
| 96 | |
| 97 | for (i = 0; i < msixcnt; i++) { |
| 98 | idxd->irq_entries[i].id = i; |
| 99 | idxd->irq_entries[i].idxd = idxd; |
Dave Jiang | e4f4d8c | 2020-10-27 10:34:40 -0700 | [diff] [blame^] | 100 | spin_lock_init(&idxd->irq_entries[i].list_lock); |
Dave Jiang | bfe1d56 | 2020-01-21 16:43:59 -0700 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | msix = &idxd->msix_entries[0]; |
| 104 | irq_entry = &idxd->irq_entries[0]; |
| 105 | rc = devm_request_threaded_irq(dev, msix->vector, idxd_irq_handler, |
| 106 | idxd_misc_thread, 0, "idxd-misc", |
| 107 | irq_entry); |
| 108 | if (rc < 0) { |
| 109 | dev_err(dev, "Failed to allocate misc interrupt.\n"); |
| 110 | goto err_no_irq; |
| 111 | } |
| 112 | |
| 113 | dev_dbg(dev, "Allocated idxd-misc handler on msix vector %d\n", |
| 114 | msix->vector); |
| 115 | |
| 116 | /* first MSI-X entry is not for wq interrupts */ |
| 117 | idxd->num_wq_irqs = msixcnt - 1; |
| 118 | |
| 119 | for (i = 1; i < msixcnt; i++) { |
| 120 | msix = &idxd->msix_entries[i]; |
| 121 | irq_entry = &idxd->irq_entries[i]; |
| 122 | |
| 123 | init_llist_head(&idxd->irq_entries[i].pending_llist); |
| 124 | INIT_LIST_HEAD(&idxd->irq_entries[i].work_list); |
| 125 | rc = devm_request_threaded_irq(dev, msix->vector, |
| 126 | idxd_irq_handler, |
| 127 | idxd_wq_thread, 0, |
| 128 | "idxd-portal", irq_entry); |
| 129 | if (rc < 0) { |
| 130 | dev_err(dev, "Failed to allocate irq %d.\n", |
| 131 | msix->vector); |
| 132 | goto err_no_irq; |
| 133 | } |
| 134 | dev_dbg(dev, "Allocated idxd-msix %d for vector %d\n", |
| 135 | i, msix->vector); |
| 136 | } |
| 137 | |
| 138 | idxd_unmask_error_interrupts(idxd); |
| 139 | |
Dave Jiang | 8e50d39 | 2020-10-27 10:34:35 -0700 | [diff] [blame] | 140 | /* Setup MSIX permission table */ |
| 141 | mperm.bits = 0; |
| 142 | mperm.pasid = idxd->pasid; |
| 143 | mperm.pasid_en = device_pasid_enabled(idxd); |
| 144 | for (i = 1; i < msixcnt; i++) |
| 145 | iowrite32(mperm.bits, idxd->reg_base + idxd->msix_perm_offset + i * 8); |
| 146 | |
Dave Jiang | bfe1d56 | 2020-01-21 16:43:59 -0700 | [diff] [blame] | 147 | return 0; |
| 148 | |
| 149 | err_no_irq: |
| 150 | /* Disable error interrupt generation */ |
| 151 | idxd_mask_error_interrupts(idxd); |
| 152 | pci_disable_msix(pdev); |
| 153 | dev_err(dev, "No usable interrupts\n"); |
| 154 | return rc; |
| 155 | } |
| 156 | |
Dave Jiang | bfe1d56 | 2020-01-21 16:43:59 -0700 | [diff] [blame] | 157 | static int idxd_setup_internals(struct idxd_device *idxd) |
| 158 | { |
| 159 | struct device *dev = &idxd->pdev->dev; |
| 160 | int i; |
| 161 | |
Dave Jiang | 0d5c10b | 2020-06-26 11:11:18 -0700 | [diff] [blame] | 162 | init_waitqueue_head(&idxd->cmd_waitq); |
Dave Jiang | bfe1d56 | 2020-01-21 16:43:59 -0700 | [diff] [blame] | 163 | idxd->groups = devm_kcalloc(dev, idxd->max_groups, |
| 164 | sizeof(struct idxd_group), GFP_KERNEL); |
| 165 | if (!idxd->groups) |
| 166 | return -ENOMEM; |
| 167 | |
| 168 | for (i = 0; i < idxd->max_groups; i++) { |
| 169 | idxd->groups[i].idxd = idxd; |
| 170 | idxd->groups[i].id = i; |
| 171 | idxd->groups[i].tc_a = -1; |
| 172 | idxd->groups[i].tc_b = -1; |
| 173 | } |
| 174 | |
| 175 | idxd->wqs = devm_kcalloc(dev, idxd->max_wqs, sizeof(struct idxd_wq), |
| 176 | GFP_KERNEL); |
| 177 | if (!idxd->wqs) |
| 178 | return -ENOMEM; |
| 179 | |
| 180 | idxd->engines = devm_kcalloc(dev, idxd->max_engines, |
| 181 | sizeof(struct idxd_engine), GFP_KERNEL); |
| 182 | if (!idxd->engines) |
| 183 | return -ENOMEM; |
| 184 | |
| 185 | for (i = 0; i < idxd->max_wqs; i++) { |
| 186 | struct idxd_wq *wq = &idxd->wqs[i]; |
Dave Jiang | bfe1d56 | 2020-01-21 16:43:59 -0700 | [diff] [blame] | 187 | |
| 188 | wq->id = i; |
| 189 | wq->idxd = idxd; |
| 190 | mutex_init(&wq->wq_lock); |
Dave Jiang | 42d279f | 2020-01-21 16:44:29 -0700 | [diff] [blame] | 191 | wq->idxd_cdev.minor = -1; |
Dave Jiang | d7aad55 | 2020-08-28 15:12:10 -0700 | [diff] [blame] | 192 | wq->max_xfer_bytes = idxd->max_xfer_bytes; |
Dave Jiang | e7184b1 | 2020-08-28 15:12:50 -0700 | [diff] [blame] | 193 | wq->max_batch_size = idxd->max_batch_size; |
Dave Jiang | d98793b | 2020-10-27 14:34:09 -0700 | [diff] [blame] | 194 | wq->wqcfg = devm_kzalloc(dev, idxd->wqcfg_size, GFP_KERNEL); |
| 195 | if (!wq->wqcfg) |
| 196 | return -ENOMEM; |
Dave Jiang | bfe1d56 | 2020-01-21 16:43:59 -0700 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | for (i = 0; i < idxd->max_engines; i++) { |
| 200 | idxd->engines[i].idxd = idxd; |
| 201 | idxd->engines[i].id = i; |
| 202 | } |
| 203 | |
Dave Jiang | 0d5c10b | 2020-06-26 11:11:18 -0700 | [diff] [blame] | 204 | idxd->wq = create_workqueue(dev_name(dev)); |
| 205 | if (!idxd->wq) |
| 206 | return -ENOMEM; |
| 207 | |
Dave Jiang | bfe1d56 | 2020-01-21 16:43:59 -0700 | [diff] [blame] | 208 | return 0; |
| 209 | } |
| 210 | |
| 211 | static void idxd_read_table_offsets(struct idxd_device *idxd) |
| 212 | { |
| 213 | union offsets_reg offsets; |
| 214 | struct device *dev = &idxd->pdev->dev; |
| 215 | |
| 216 | offsets.bits[0] = ioread64(idxd->reg_base + IDXD_TABLE_OFFSET); |
| 217 | offsets.bits[1] = ioread64(idxd->reg_base + IDXD_TABLE_OFFSET |
| 218 | + sizeof(u64)); |
| 219 | idxd->grpcfg_offset = offsets.grpcfg * 0x100; |
| 220 | dev_dbg(dev, "IDXD Group Config Offset: %#x\n", idxd->grpcfg_offset); |
| 221 | idxd->wqcfg_offset = offsets.wqcfg * 0x100; |
| 222 | dev_dbg(dev, "IDXD Work Queue Config Offset: %#x\n", |
| 223 | idxd->wqcfg_offset); |
| 224 | idxd->msix_perm_offset = offsets.msix_perm * 0x100; |
| 225 | dev_dbg(dev, "IDXD MSIX Permission Offset: %#x\n", |
| 226 | idxd->msix_perm_offset); |
| 227 | idxd->perfmon_offset = offsets.perfmon * 0x100; |
| 228 | dev_dbg(dev, "IDXD Perfmon Offset: %#x\n", idxd->perfmon_offset); |
| 229 | } |
| 230 | |
| 231 | static void idxd_read_caps(struct idxd_device *idxd) |
| 232 | { |
| 233 | struct device *dev = &idxd->pdev->dev; |
| 234 | int i; |
| 235 | |
| 236 | /* reading generic capabilities */ |
| 237 | idxd->hw.gen_cap.bits = ioread64(idxd->reg_base + IDXD_GENCAP_OFFSET); |
| 238 | dev_dbg(dev, "gen_cap: %#llx\n", idxd->hw.gen_cap.bits); |
| 239 | idxd->max_xfer_bytes = 1ULL << idxd->hw.gen_cap.max_xfer_shift; |
| 240 | dev_dbg(dev, "max xfer size: %llu bytes\n", idxd->max_xfer_bytes); |
| 241 | idxd->max_batch_size = 1U << idxd->hw.gen_cap.max_batch_shift; |
| 242 | dev_dbg(dev, "max batch size: %u\n", idxd->max_batch_size); |
| 243 | if (idxd->hw.gen_cap.config_en) |
| 244 | set_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags); |
| 245 | |
| 246 | /* reading group capabilities */ |
| 247 | idxd->hw.group_cap.bits = |
| 248 | ioread64(idxd->reg_base + IDXD_GRPCAP_OFFSET); |
| 249 | dev_dbg(dev, "group_cap: %#llx\n", idxd->hw.group_cap.bits); |
| 250 | idxd->max_groups = idxd->hw.group_cap.num_groups; |
| 251 | dev_dbg(dev, "max groups: %u\n", idxd->max_groups); |
| 252 | idxd->max_tokens = idxd->hw.group_cap.total_tokens; |
| 253 | dev_dbg(dev, "max tokens: %u\n", idxd->max_tokens); |
Dave Jiang | c52ca47 | 2020-01-21 16:44:05 -0700 | [diff] [blame] | 254 | idxd->nr_tokens = idxd->max_tokens; |
Dave Jiang | bfe1d56 | 2020-01-21 16:43:59 -0700 | [diff] [blame] | 255 | |
| 256 | /* read engine capabilities */ |
| 257 | idxd->hw.engine_cap.bits = |
| 258 | ioread64(idxd->reg_base + IDXD_ENGCAP_OFFSET); |
| 259 | dev_dbg(dev, "engine_cap: %#llx\n", idxd->hw.engine_cap.bits); |
| 260 | idxd->max_engines = idxd->hw.engine_cap.num_engines; |
| 261 | dev_dbg(dev, "max engines: %u\n", idxd->max_engines); |
| 262 | |
| 263 | /* read workqueue capabilities */ |
| 264 | idxd->hw.wq_cap.bits = ioread64(idxd->reg_base + IDXD_WQCAP_OFFSET); |
| 265 | dev_dbg(dev, "wq_cap: %#llx\n", idxd->hw.wq_cap.bits); |
| 266 | idxd->max_wq_size = idxd->hw.wq_cap.total_wq_size; |
| 267 | dev_dbg(dev, "total workqueue size: %u\n", idxd->max_wq_size); |
| 268 | idxd->max_wqs = idxd->hw.wq_cap.num_wqs; |
| 269 | dev_dbg(dev, "max workqueues: %u\n", idxd->max_wqs); |
Dave Jiang | d98793b | 2020-10-27 14:34:09 -0700 | [diff] [blame] | 270 | idxd->wqcfg_size = 1 << (idxd->hw.wq_cap.wqcfg_size + IDXD_WQCFG_MIN); |
| 271 | dev_dbg(dev, "wqcfg size: %u\n", idxd->wqcfg_size); |
Dave Jiang | bfe1d56 | 2020-01-21 16:43:59 -0700 | [diff] [blame] | 272 | |
| 273 | /* reading operation capabilities */ |
| 274 | for (i = 0; i < 4; i++) { |
| 275 | idxd->hw.opcap.bits[i] = ioread64(idxd->reg_base + |
| 276 | IDXD_OPCAP_OFFSET + i * sizeof(u64)); |
| 277 | dev_dbg(dev, "opcap[%d]: %#llx\n", i, idxd->hw.opcap.bits[i]); |
| 278 | } |
| 279 | } |
| 280 | |
Dave Jiang | 8e50d39 | 2020-10-27 10:34:35 -0700 | [diff] [blame] | 281 | static struct idxd_device *idxd_alloc(struct pci_dev *pdev) |
Dave Jiang | bfe1d56 | 2020-01-21 16:43:59 -0700 | [diff] [blame] | 282 | { |
| 283 | struct device *dev = &pdev->dev; |
| 284 | struct idxd_device *idxd; |
| 285 | |
| 286 | idxd = devm_kzalloc(dev, sizeof(struct idxd_device), GFP_KERNEL); |
| 287 | if (!idxd) |
| 288 | return NULL; |
| 289 | |
| 290 | idxd->pdev = pdev; |
Dave Jiang | bfe1d56 | 2020-01-21 16:43:59 -0700 | [diff] [blame] | 291 | spin_lock_init(&idxd->dev_lock); |
| 292 | |
| 293 | return idxd; |
| 294 | } |
| 295 | |
Dave Jiang | 8e50d39 | 2020-10-27 10:34:35 -0700 | [diff] [blame] | 296 | static int idxd_enable_system_pasid(struct idxd_device *idxd) |
| 297 | { |
| 298 | int flags; |
| 299 | unsigned int pasid; |
| 300 | struct iommu_sva *sva; |
| 301 | |
| 302 | flags = SVM_FLAG_SUPERVISOR_MODE; |
| 303 | |
| 304 | sva = iommu_sva_bind_device(&idxd->pdev->dev, NULL, &flags); |
| 305 | if (IS_ERR(sva)) { |
| 306 | dev_warn(&idxd->pdev->dev, |
| 307 | "iommu sva bind failed: %ld\n", PTR_ERR(sva)); |
| 308 | return PTR_ERR(sva); |
| 309 | } |
| 310 | |
| 311 | pasid = iommu_sva_get_pasid(sva); |
| 312 | if (pasid == IOMMU_PASID_INVALID) { |
| 313 | iommu_sva_unbind_device(sva); |
| 314 | return -ENODEV; |
| 315 | } |
| 316 | |
| 317 | idxd->sva = sva; |
| 318 | idxd->pasid = pasid; |
| 319 | dev_dbg(&idxd->pdev->dev, "system pasid: %u\n", pasid); |
| 320 | return 0; |
| 321 | } |
| 322 | |
| 323 | static void idxd_disable_system_pasid(struct idxd_device *idxd) |
| 324 | { |
| 325 | |
| 326 | iommu_sva_unbind_device(idxd->sva); |
| 327 | idxd->sva = NULL; |
| 328 | } |
| 329 | |
Dave Jiang | bfe1d56 | 2020-01-21 16:43:59 -0700 | [diff] [blame] | 330 | static int idxd_probe(struct idxd_device *idxd) |
| 331 | { |
| 332 | struct pci_dev *pdev = idxd->pdev; |
| 333 | struct device *dev = &pdev->dev; |
| 334 | int rc; |
| 335 | |
| 336 | dev_dbg(dev, "%s entered and resetting device\n", __func__); |
Dave Jiang | 0d5c10b | 2020-06-26 11:11:18 -0700 | [diff] [blame] | 337 | idxd_device_init_reset(idxd); |
Dave Jiang | bfe1d56 | 2020-01-21 16:43:59 -0700 | [diff] [blame] | 338 | dev_dbg(dev, "IDXD reset complete\n"); |
| 339 | |
Dave Jiang | 8e50d39 | 2020-10-27 10:34:35 -0700 | [diff] [blame] | 340 | if (IS_ENABLED(CONFIG_INTEL_IDXD_SVM)) { |
| 341 | rc = idxd_enable_system_pasid(idxd); |
| 342 | if (rc < 0) |
| 343 | dev_warn(dev, "Failed to enable PASID. No SVA support: %d\n", rc); |
| 344 | else |
| 345 | set_bit(IDXD_FLAG_PASID_ENABLED, &idxd->flags); |
| 346 | } |
| 347 | |
Dave Jiang | bfe1d56 | 2020-01-21 16:43:59 -0700 | [diff] [blame] | 348 | idxd_read_caps(idxd); |
| 349 | idxd_read_table_offsets(idxd); |
| 350 | |
| 351 | rc = idxd_setup_internals(idxd); |
| 352 | if (rc) |
| 353 | goto err_setup; |
| 354 | |
| 355 | rc = idxd_setup_interrupts(idxd); |
| 356 | if (rc) |
| 357 | goto err_setup; |
| 358 | |
| 359 | dev_dbg(dev, "IDXD interrupt setup complete.\n"); |
| 360 | |
| 361 | mutex_lock(&idxd_idr_lock); |
| 362 | idxd->id = idr_alloc(&idxd_idrs[idxd->type], idxd, 0, 0, GFP_KERNEL); |
| 363 | mutex_unlock(&idxd_idr_lock); |
| 364 | if (idxd->id < 0) { |
| 365 | rc = -ENOMEM; |
| 366 | goto err_idr_fail; |
| 367 | } |
| 368 | |
Dave Jiang | 42d279f | 2020-01-21 16:44:29 -0700 | [diff] [blame] | 369 | idxd->major = idxd_cdev_get_major(idxd); |
| 370 | |
Dave Jiang | bfe1d56 | 2020-01-21 16:43:59 -0700 | [diff] [blame] | 371 | dev_dbg(dev, "IDXD device %d probed successfully\n", idxd->id); |
| 372 | return 0; |
| 373 | |
| 374 | err_idr_fail: |
| 375 | idxd_mask_error_interrupts(idxd); |
| 376 | idxd_mask_msix_vectors(idxd); |
| 377 | err_setup: |
Dave Jiang | 8e50d39 | 2020-10-27 10:34:35 -0700 | [diff] [blame] | 378 | if (device_pasid_enabled(idxd)) |
| 379 | idxd_disable_system_pasid(idxd); |
Dave Jiang | bfe1d56 | 2020-01-21 16:43:59 -0700 | [diff] [blame] | 380 | return rc; |
| 381 | } |
| 382 | |
| 383 | static int idxd_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id) |
| 384 | { |
Dave Jiang | bfe1d56 | 2020-01-21 16:43:59 -0700 | [diff] [blame] | 385 | struct device *dev = &pdev->dev; |
| 386 | struct idxd_device *idxd; |
| 387 | int rc; |
Dave Jiang | bfe1d56 | 2020-01-21 16:43:59 -0700 | [diff] [blame] | 388 | |
| 389 | rc = pcim_enable_device(pdev); |
| 390 | if (rc) |
| 391 | return rc; |
| 392 | |
Dave Jiang | 8e50d39 | 2020-10-27 10:34:35 -0700 | [diff] [blame] | 393 | dev_dbg(dev, "Alloc IDXD context\n"); |
| 394 | idxd = idxd_alloc(pdev); |
| 395 | if (!idxd) |
| 396 | return -ENOMEM; |
Dave Jiang | bfe1d56 | 2020-01-21 16:43:59 -0700 | [diff] [blame] | 397 | |
Dave Jiang | 8e50d39 | 2020-10-27 10:34:35 -0700 | [diff] [blame] | 398 | dev_dbg(dev, "Mapping BARs\n"); |
| 399 | idxd->reg_base = pcim_iomap(pdev, IDXD_MMIO_BAR, 0); |
| 400 | if (!idxd->reg_base) |
Dave Jiang | bfe1d56 | 2020-01-21 16:43:59 -0700 | [diff] [blame] | 401 | return -ENOMEM; |
| 402 | |
| 403 | dev_dbg(dev, "Set DMA masks\n"); |
| 404 | rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64)); |
| 405 | if (rc) |
| 406 | rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32)); |
| 407 | if (rc) |
| 408 | return rc; |
| 409 | |
| 410 | rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64)); |
| 411 | if (rc) |
| 412 | rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)); |
| 413 | if (rc) |
| 414 | return rc; |
| 415 | |
Dave Jiang | bfe1d56 | 2020-01-21 16:43:59 -0700 | [diff] [blame] | 416 | idxd_set_type(idxd); |
| 417 | |
| 418 | dev_dbg(dev, "Set PCI master\n"); |
| 419 | pci_set_master(pdev); |
| 420 | pci_set_drvdata(pdev, idxd); |
| 421 | |
| 422 | idxd->hw.version = ioread32(idxd->reg_base + IDXD_VER_OFFSET); |
| 423 | rc = idxd_probe(idxd); |
| 424 | if (rc) { |
| 425 | dev_err(dev, "Intel(R) IDXD DMA Engine init failed\n"); |
| 426 | return -ENODEV; |
| 427 | } |
| 428 | |
Dave Jiang | c52ca47 | 2020-01-21 16:44:05 -0700 | [diff] [blame] | 429 | rc = idxd_setup_sysfs(idxd); |
| 430 | if (rc) { |
| 431 | dev_err(dev, "IDXD sysfs setup failed\n"); |
| 432 | return -ENODEV; |
| 433 | } |
| 434 | |
| 435 | idxd->state = IDXD_DEV_CONF_READY; |
| 436 | |
Dave Jiang | bfe1d56 | 2020-01-21 16:43:59 -0700 | [diff] [blame] | 437 | dev_info(&pdev->dev, "Intel(R) Accelerator Device (v%x)\n", |
| 438 | idxd->hw.version); |
| 439 | |
| 440 | return 0; |
| 441 | } |
| 442 | |
Dave Jiang | 8f47d1a | 2020-01-21 16:44:23 -0700 | [diff] [blame] | 443 | static void idxd_flush_pending_llist(struct idxd_irq_entry *ie) |
| 444 | { |
| 445 | struct idxd_desc *desc, *itr; |
| 446 | struct llist_node *head; |
| 447 | |
| 448 | head = llist_del_all(&ie->pending_llist); |
| 449 | if (!head) |
| 450 | return; |
| 451 | |
| 452 | llist_for_each_entry_safe(desc, itr, head, llnode) { |
| 453 | idxd_dma_complete_txd(desc, IDXD_COMPLETE_ABORT); |
| 454 | idxd_free_desc(desc->wq, desc); |
| 455 | } |
| 456 | } |
| 457 | |
| 458 | static void idxd_flush_work_list(struct idxd_irq_entry *ie) |
| 459 | { |
| 460 | struct idxd_desc *desc, *iter; |
| 461 | |
| 462 | list_for_each_entry_safe(desc, iter, &ie->work_list, list) { |
| 463 | list_del(&desc->list); |
| 464 | idxd_dma_complete_txd(desc, IDXD_COMPLETE_ABORT); |
| 465 | idxd_free_desc(desc->wq, desc); |
| 466 | } |
| 467 | } |
| 468 | |
Dave Jiang | bfe1d56 | 2020-01-21 16:43:59 -0700 | [diff] [blame] | 469 | static void idxd_shutdown(struct pci_dev *pdev) |
| 470 | { |
| 471 | struct idxd_device *idxd = pci_get_drvdata(pdev); |
| 472 | int rc, i; |
| 473 | struct idxd_irq_entry *irq_entry; |
| 474 | int msixcnt = pci_msix_vec_count(pdev); |
Dave Jiang | bfe1d56 | 2020-01-21 16:43:59 -0700 | [diff] [blame] | 475 | |
Dave Jiang | bfe1d56 | 2020-01-21 16:43:59 -0700 | [diff] [blame] | 476 | rc = idxd_device_disable(idxd); |
Dave Jiang | bfe1d56 | 2020-01-21 16:43:59 -0700 | [diff] [blame] | 477 | if (rc) |
| 478 | dev_err(&pdev->dev, "Disabling device failed\n"); |
| 479 | |
| 480 | dev_dbg(&pdev->dev, "%s called\n", __func__); |
| 481 | idxd_mask_msix_vectors(idxd); |
| 482 | idxd_mask_error_interrupts(idxd); |
| 483 | |
| 484 | for (i = 0; i < msixcnt; i++) { |
| 485 | irq_entry = &idxd->irq_entries[i]; |
| 486 | synchronize_irq(idxd->msix_entries[i].vector); |
| 487 | if (i == 0) |
| 488 | continue; |
Dave Jiang | 8f47d1a | 2020-01-21 16:44:23 -0700 | [diff] [blame] | 489 | idxd_flush_pending_llist(irq_entry); |
| 490 | idxd_flush_work_list(irq_entry); |
Dave Jiang | bfe1d56 | 2020-01-21 16:43:59 -0700 | [diff] [blame] | 491 | } |
Dave Jiang | 0d5c10b | 2020-06-26 11:11:18 -0700 | [diff] [blame] | 492 | |
| 493 | destroy_workqueue(idxd->wq); |
Dave Jiang | bfe1d56 | 2020-01-21 16:43:59 -0700 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | static void idxd_remove(struct pci_dev *pdev) |
| 497 | { |
| 498 | struct idxd_device *idxd = pci_get_drvdata(pdev); |
| 499 | |
| 500 | dev_dbg(&pdev->dev, "%s called\n", __func__); |
Dave Jiang | c52ca47 | 2020-01-21 16:44:05 -0700 | [diff] [blame] | 501 | idxd_cleanup_sysfs(idxd); |
Dave Jiang | bfe1d56 | 2020-01-21 16:43:59 -0700 | [diff] [blame] | 502 | idxd_shutdown(pdev); |
Dave Jiang | 8e50d39 | 2020-10-27 10:34:35 -0700 | [diff] [blame] | 503 | if (device_pasid_enabled(idxd)) |
| 504 | idxd_disable_system_pasid(idxd); |
Dave Jiang | bfe1d56 | 2020-01-21 16:43:59 -0700 | [diff] [blame] | 505 | mutex_lock(&idxd_idr_lock); |
| 506 | idr_remove(&idxd_idrs[idxd->type], idxd->id); |
| 507 | mutex_unlock(&idxd_idr_lock); |
| 508 | } |
| 509 | |
| 510 | static struct pci_driver idxd_pci_driver = { |
| 511 | .name = DRV_NAME, |
| 512 | .id_table = idxd_pci_tbl, |
| 513 | .probe = idxd_pci_probe, |
| 514 | .remove = idxd_remove, |
| 515 | .shutdown = idxd_shutdown, |
| 516 | }; |
| 517 | |
| 518 | static int __init idxd_init_module(void) |
| 519 | { |
| 520 | int err, i; |
| 521 | |
| 522 | /* |
Dave Jiang | 8e50d39 | 2020-10-27 10:34:35 -0700 | [diff] [blame] | 523 | * If the CPU does not support MOVDIR64B or ENQCMDS, there's no point in |
Dave Jiang | bfe1d56 | 2020-01-21 16:43:59 -0700 | [diff] [blame] | 524 | * enumerating the device. We can not utilize it. |
| 525 | */ |
| 526 | if (!boot_cpu_has(X86_FEATURE_MOVDIR64B)) { |
| 527 | pr_warn("idxd driver failed to load without MOVDIR64B.\n"); |
| 528 | return -ENODEV; |
| 529 | } |
| 530 | |
Dave Jiang | 8e50d39 | 2020-10-27 10:34:35 -0700 | [diff] [blame] | 531 | if (!boot_cpu_has(X86_FEATURE_ENQCMD)) |
| 532 | pr_warn("Platform does not have ENQCMD(S) support.\n"); |
| 533 | else |
| 534 | support_enqcmd = true; |
Dave Jiang | bfe1d56 | 2020-01-21 16:43:59 -0700 | [diff] [blame] | 535 | |
| 536 | mutex_init(&idxd_idr_lock); |
| 537 | for (i = 0; i < IDXD_TYPE_MAX; i++) |
| 538 | idr_init(&idxd_idrs[i]); |
| 539 | |
Dave Jiang | c52ca47 | 2020-01-21 16:44:05 -0700 | [diff] [blame] | 540 | err = idxd_register_bus_type(); |
| 541 | if (err < 0) |
Dave Jiang | bfe1d56 | 2020-01-21 16:43:59 -0700 | [diff] [blame] | 542 | return err; |
| 543 | |
Dave Jiang | c52ca47 | 2020-01-21 16:44:05 -0700 | [diff] [blame] | 544 | err = idxd_register_driver(); |
| 545 | if (err < 0) |
| 546 | goto err_idxd_driver_register; |
| 547 | |
Dave Jiang | 42d279f | 2020-01-21 16:44:29 -0700 | [diff] [blame] | 548 | err = idxd_cdev_register(); |
| 549 | if (err) |
| 550 | goto err_cdev_register; |
| 551 | |
Dave Jiang | c52ca47 | 2020-01-21 16:44:05 -0700 | [diff] [blame] | 552 | err = pci_register_driver(&idxd_pci_driver); |
| 553 | if (err) |
| 554 | goto err_pci_register; |
| 555 | |
Dave Jiang | bfe1d56 | 2020-01-21 16:43:59 -0700 | [diff] [blame] | 556 | return 0; |
Dave Jiang | c52ca47 | 2020-01-21 16:44:05 -0700 | [diff] [blame] | 557 | |
| 558 | err_pci_register: |
Dave Jiang | 42d279f | 2020-01-21 16:44:29 -0700 | [diff] [blame] | 559 | idxd_cdev_remove(); |
| 560 | err_cdev_register: |
Dave Jiang | c52ca47 | 2020-01-21 16:44:05 -0700 | [diff] [blame] | 561 | idxd_unregister_driver(); |
| 562 | err_idxd_driver_register: |
| 563 | idxd_unregister_bus_type(); |
| 564 | return err; |
Dave Jiang | bfe1d56 | 2020-01-21 16:43:59 -0700 | [diff] [blame] | 565 | } |
| 566 | module_init(idxd_init_module); |
| 567 | |
| 568 | static void __exit idxd_exit_module(void) |
| 569 | { |
| 570 | pci_unregister_driver(&idxd_pci_driver); |
Dave Jiang | 42d279f | 2020-01-21 16:44:29 -0700 | [diff] [blame] | 571 | idxd_cdev_remove(); |
Dave Jiang | c52ca47 | 2020-01-21 16:44:05 -0700 | [diff] [blame] | 572 | idxd_unregister_bus_type(); |
Dave Jiang | bfe1d56 | 2020-01-21 16:43:59 -0700 | [diff] [blame] | 573 | } |
| 574 | module_exit(idxd_exit_module); |