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" |
Tom Zanussi | 0bde444 | 2021-04-24 10:04:16 -0500 | [diff] [blame^] | 24 | #include "perfmon.h" |
Dave Jiang | bfe1d56 | 2020-01-21 16:43:59 -0700 | [diff] [blame] | 25 | |
| 26 | MODULE_VERSION(IDXD_DRIVER_VERSION); |
| 27 | MODULE_LICENSE("GPL v2"); |
| 28 | MODULE_AUTHOR("Intel Corporation"); |
| 29 | |
Dave Jiang | 03d939c | 2021-01-22 11:46:00 -0700 | [diff] [blame] | 30 | static bool sva = true; |
| 31 | module_param(sva, bool, 0644); |
| 32 | MODULE_PARM_DESC(sva, "Toggle SVA support on/off"); |
| 33 | |
Dave Jiang | bfe1d56 | 2020-01-21 16:43:59 -0700 | [diff] [blame] | 34 | #define DRV_NAME "idxd" |
| 35 | |
Dave Jiang | 8e50d39 | 2020-10-27 10:34:35 -0700 | [diff] [blame] | 36 | bool support_enqcmd; |
Dave Jiang | 4b73e4e | 2021-04-15 16:38:03 -0700 | [diff] [blame] | 37 | DEFINE_IDA(idxd_ida); |
Dave Jiang | bfe1d56 | 2020-01-21 16:43:59 -0700 | [diff] [blame] | 38 | |
Dave Jiang | 435b512 | 2021-04-15 16:38:09 -0700 | [diff] [blame] | 39 | static struct idxd_driver_data idxd_driver_data[] = { |
| 40 | [IDXD_TYPE_DSA] = { |
| 41 | .name_prefix = "dsa", |
| 42 | .type = IDXD_TYPE_DSA, |
| 43 | .compl_size = sizeof(struct dsa_completion_record), |
| 44 | .align = 32, |
| 45 | .dev_type = &dsa_device_type, |
| 46 | }, |
| 47 | [IDXD_TYPE_IAX] = { |
| 48 | .name_prefix = "iax", |
| 49 | .type = IDXD_TYPE_IAX, |
| 50 | .compl_size = sizeof(struct iax_completion_record), |
| 51 | .align = 64, |
| 52 | .dev_type = &iax_device_type, |
| 53 | }, |
| 54 | }; |
| 55 | |
Dave Jiang | bfe1d56 | 2020-01-21 16:43:59 -0700 | [diff] [blame] | 56 | static struct pci_device_id idxd_pci_tbl[] = { |
| 57 | /* DSA ver 1.0 platforms */ |
Dave Jiang | 435b512 | 2021-04-15 16:38:09 -0700 | [diff] [blame] | 58 | { PCI_DEVICE_DATA(INTEL, DSA_SPR0, &idxd_driver_data[IDXD_TYPE_DSA]) }, |
Dave Jiang | f25b4638 | 2020-11-17 13:39:14 -0700 | [diff] [blame] | 59 | |
| 60 | /* IAX ver 1.0 platforms */ |
Dave Jiang | 435b512 | 2021-04-15 16:38:09 -0700 | [diff] [blame] | 61 | { PCI_DEVICE_DATA(INTEL, IAX_SPR0, &idxd_driver_data[IDXD_TYPE_IAX]) }, |
Dave Jiang | bfe1d56 | 2020-01-21 16:43:59 -0700 | [diff] [blame] | 62 | { 0, } |
| 63 | }; |
| 64 | MODULE_DEVICE_TABLE(pci, idxd_pci_tbl); |
| 65 | |
Dave Jiang | bfe1d56 | 2020-01-21 16:43:59 -0700 | [diff] [blame] | 66 | static int idxd_setup_interrupts(struct idxd_device *idxd) |
| 67 | { |
| 68 | struct pci_dev *pdev = idxd->pdev; |
| 69 | struct device *dev = &pdev->dev; |
Dave Jiang | bfe1d56 | 2020-01-21 16:43:59 -0700 | [diff] [blame] | 70 | struct idxd_irq_entry *irq_entry; |
| 71 | int i, msixcnt; |
| 72 | int rc = 0; |
| 73 | |
| 74 | msixcnt = pci_msix_vec_count(pdev); |
| 75 | if (msixcnt < 0) { |
| 76 | dev_err(dev, "Not MSI-X interrupt capable.\n"); |
Dave Jiang | 5fc8e85 | 2021-04-15 16:37:15 -0700 | [diff] [blame] | 77 | return -ENOSPC; |
Dave Jiang | bfe1d56 | 2020-01-21 16:43:59 -0700 | [diff] [blame] | 78 | } |
| 79 | |
Dave Jiang | 5fc8e85 | 2021-04-15 16:37:15 -0700 | [diff] [blame] | 80 | rc = pci_alloc_irq_vectors(pdev, msixcnt, msixcnt, PCI_IRQ_MSIX); |
| 81 | if (rc != msixcnt) { |
| 82 | dev_err(dev, "Failed enabling %d MSIX entries: %d\n", msixcnt, rc); |
| 83 | return -ENOSPC; |
Dave Jiang | bfe1d56 | 2020-01-21 16:43:59 -0700 | [diff] [blame] | 84 | } |
| 85 | dev_dbg(dev, "Enabled %d msix vectors\n", msixcnt); |
| 86 | |
| 87 | /* |
| 88 | * We implement 1 completion list per MSI-X entry except for |
| 89 | * entry 0, which is for errors and others. |
| 90 | */ |
Dave Jiang | 47c16ac | 2021-04-15 16:37:33 -0700 | [diff] [blame] | 91 | idxd->irq_entries = kcalloc_node(msixcnt, sizeof(struct idxd_irq_entry), |
| 92 | GFP_KERNEL, dev_to_node(dev)); |
Dave Jiang | bfe1d56 | 2020-01-21 16:43:59 -0700 | [diff] [blame] | 93 | if (!idxd->irq_entries) { |
| 94 | rc = -ENOMEM; |
Dave Jiang | 5fc8e85 | 2021-04-15 16:37:15 -0700 | [diff] [blame] | 95 | goto err_irq_entries; |
Dave Jiang | bfe1d56 | 2020-01-21 16:43:59 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | for (i = 0; i < msixcnt; i++) { |
| 99 | idxd->irq_entries[i].id = i; |
| 100 | idxd->irq_entries[i].idxd = idxd; |
Dave Jiang | 5fc8e85 | 2021-04-15 16:37:15 -0700 | [diff] [blame] | 101 | idxd->irq_entries[i].vector = pci_irq_vector(pdev, i); |
Dave Jiang | e4f4d8c | 2020-10-27 10:34:40 -0700 | [diff] [blame] | 102 | spin_lock_init(&idxd->irq_entries[i].list_lock); |
Dave Jiang | bfe1d56 | 2020-01-21 16:43:59 -0700 | [diff] [blame] | 103 | } |
| 104 | |
Dave Jiang | bfe1d56 | 2020-01-21 16:43:59 -0700 | [diff] [blame] | 105 | irq_entry = &idxd->irq_entries[0]; |
Dave Jiang | a161046 | 2021-04-20 12:00:34 -0700 | [diff] [blame] | 106 | rc = request_threaded_irq(irq_entry->vector, NULL, idxd_misc_thread, |
Dave Jiang | 5fc8e85 | 2021-04-15 16:37:15 -0700 | [diff] [blame] | 107 | 0, "idxd-misc", irq_entry); |
Dave Jiang | bfe1d56 | 2020-01-21 16:43:59 -0700 | [diff] [blame] | 108 | if (rc < 0) { |
| 109 | dev_err(dev, "Failed to allocate misc interrupt.\n"); |
Dave Jiang | 5fc8e85 | 2021-04-15 16:37:15 -0700 | [diff] [blame] | 110 | goto err_misc_irq; |
Dave Jiang | bfe1d56 | 2020-01-21 16:43:59 -0700 | [diff] [blame] | 111 | } |
| 112 | |
Dave Jiang | 5fc8e85 | 2021-04-15 16:37:15 -0700 | [diff] [blame] | 113 | dev_dbg(dev, "Allocated idxd-misc handler on msix vector %d\n", irq_entry->vector); |
Dave Jiang | bfe1d56 | 2020-01-21 16:43:59 -0700 | [diff] [blame] | 114 | |
| 115 | /* first MSI-X entry is not for wq interrupts */ |
| 116 | idxd->num_wq_irqs = msixcnt - 1; |
| 117 | |
| 118 | for (i = 1; i < msixcnt; i++) { |
Dave Jiang | bfe1d56 | 2020-01-21 16:43:59 -0700 | [diff] [blame] | 119 | irq_entry = &idxd->irq_entries[i]; |
| 120 | |
| 121 | init_llist_head(&idxd->irq_entries[i].pending_llist); |
| 122 | INIT_LIST_HEAD(&idxd->irq_entries[i].work_list); |
Dave Jiang | a161046 | 2021-04-20 12:00:34 -0700 | [diff] [blame] | 123 | rc = request_threaded_irq(irq_entry->vector, NULL, |
Dave Jiang | 5fc8e85 | 2021-04-15 16:37:15 -0700 | [diff] [blame] | 124 | idxd_wq_thread, 0, "idxd-portal", irq_entry); |
Dave Jiang | bfe1d56 | 2020-01-21 16:43:59 -0700 | [diff] [blame] | 125 | if (rc < 0) { |
Dave Jiang | 5fc8e85 | 2021-04-15 16:37:15 -0700 | [diff] [blame] | 126 | dev_err(dev, "Failed to allocate irq %d.\n", irq_entry->vector); |
| 127 | goto err_wq_irqs; |
Dave Jiang | bfe1d56 | 2020-01-21 16:43:59 -0700 | [diff] [blame] | 128 | } |
Dave Jiang | eb15e71 | 2021-04-20 11:46:34 -0700 | [diff] [blame] | 129 | |
Dave Jiang | 5fc8e85 | 2021-04-15 16:37:15 -0700 | [diff] [blame] | 130 | dev_dbg(dev, "Allocated idxd-msix %d for vector %d\n", i, irq_entry->vector); |
Dave Jiang | eb15e71 | 2021-04-20 11:46:34 -0700 | [diff] [blame] | 131 | if (idxd->hw.cmd_cap & BIT(IDXD_CMD_REQUEST_INT_HANDLE)) { |
| 132 | /* |
| 133 | * The MSIX vector enumeration starts at 1 with vector 0 being the |
| 134 | * misc interrupt that handles non I/O completion events. The |
| 135 | * interrupt handles are for IMS enumeration on guest. The misc |
| 136 | * interrupt vector does not require a handle and therefore we start |
| 137 | * the int_handles at index 0. Since 'i' starts at 1, the first |
| 138 | * int_handles index will be 0. |
| 139 | */ |
| 140 | rc = idxd_device_request_int_handle(idxd, i, &idxd->int_handles[i - 1], |
| 141 | IDXD_IRQ_MSIX); |
| 142 | if (rc < 0) { |
| 143 | free_irq(irq_entry->vector, irq_entry); |
| 144 | goto err_wq_irqs; |
| 145 | } |
| 146 | dev_dbg(dev, "int handle requested: %u\n", idxd->int_handles[i - 1]); |
| 147 | } |
Dave Jiang | bfe1d56 | 2020-01-21 16:43:59 -0700 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | idxd_unmask_error_interrupts(idxd); |
Dave Jiang | 6df0e6c | 2021-04-12 09:23:27 -0700 | [diff] [blame] | 151 | idxd_msix_perm_setup(idxd); |
Dave Jiang | bfe1d56 | 2020-01-21 16:43:59 -0700 | [diff] [blame] | 152 | return 0; |
| 153 | |
Dave Jiang | 5fc8e85 | 2021-04-15 16:37:15 -0700 | [diff] [blame] | 154 | err_wq_irqs: |
| 155 | while (--i >= 0) { |
| 156 | irq_entry = &idxd->irq_entries[i]; |
| 157 | free_irq(irq_entry->vector, irq_entry); |
Dave Jiang | eb15e71 | 2021-04-20 11:46:34 -0700 | [diff] [blame] | 158 | if (i != 0) |
| 159 | idxd_device_release_int_handle(idxd, |
| 160 | idxd->int_handles[i], IDXD_IRQ_MSIX); |
Dave Jiang | 5fc8e85 | 2021-04-15 16:37:15 -0700 | [diff] [blame] | 161 | } |
| 162 | err_misc_irq: |
Dave Jiang | bfe1d56 | 2020-01-21 16:43:59 -0700 | [diff] [blame] | 163 | /* Disable error interrupt generation */ |
| 164 | idxd_mask_error_interrupts(idxd); |
Dave Jiang | 5fc8e85 | 2021-04-15 16:37:15 -0700 | [diff] [blame] | 165 | err_irq_entries: |
| 166 | pci_free_irq_vectors(pdev); |
Dave Jiang | bfe1d56 | 2020-01-21 16:43:59 -0700 | [diff] [blame] | 167 | dev_err(dev, "No usable interrupts\n"); |
| 168 | return rc; |
| 169 | } |
| 170 | |
Dave Jiang | 7c5dd23 | 2021-04-15 16:37:39 -0700 | [diff] [blame] | 171 | static int idxd_setup_wqs(struct idxd_device *idxd) |
| 172 | { |
| 173 | struct device *dev = &idxd->pdev->dev; |
| 174 | struct idxd_wq *wq; |
| 175 | int i, rc; |
| 176 | |
| 177 | idxd->wqs = kcalloc_node(idxd->max_wqs, sizeof(struct idxd_wq *), |
| 178 | GFP_KERNEL, dev_to_node(dev)); |
| 179 | if (!idxd->wqs) |
| 180 | return -ENOMEM; |
| 181 | |
| 182 | for (i = 0; i < idxd->max_wqs; i++) { |
| 183 | wq = kzalloc_node(sizeof(*wq), GFP_KERNEL, dev_to_node(dev)); |
| 184 | if (!wq) { |
| 185 | rc = -ENOMEM; |
| 186 | goto err; |
| 187 | } |
| 188 | |
| 189 | wq->id = i; |
| 190 | wq->idxd = idxd; |
| 191 | device_initialize(&wq->conf_dev); |
| 192 | wq->conf_dev.parent = &idxd->conf_dev; |
Dave Jiang | 4b73e4e | 2021-04-15 16:38:03 -0700 | [diff] [blame] | 193 | wq->conf_dev.bus = &dsa_bus_type; |
Dave Jiang | 7c5dd23 | 2021-04-15 16:37:39 -0700 | [diff] [blame] | 194 | wq->conf_dev.type = &idxd_wq_device_type; |
| 195 | rc = dev_set_name(&wq->conf_dev, "wq%d.%d", idxd->id, wq->id); |
| 196 | if (rc < 0) { |
| 197 | put_device(&wq->conf_dev); |
| 198 | goto err; |
| 199 | } |
| 200 | |
| 201 | mutex_init(&wq->wq_lock); |
Dave Jiang | 04922b7 | 2021-04-15 16:37:57 -0700 | [diff] [blame] | 202 | init_waitqueue_head(&wq->err_queue); |
Dave Jiang | 93a40a6 | 2021-04-20 11:46:22 -0700 | [diff] [blame] | 203 | init_completion(&wq->wq_dead); |
Dave Jiang | 7c5dd23 | 2021-04-15 16:37:39 -0700 | [diff] [blame] | 204 | wq->max_xfer_bytes = idxd->max_xfer_bytes; |
| 205 | wq->max_batch_size = idxd->max_batch_size; |
| 206 | wq->wqcfg = kzalloc_node(idxd->wqcfg_size, GFP_KERNEL, dev_to_node(dev)); |
| 207 | if (!wq->wqcfg) { |
| 208 | put_device(&wq->conf_dev); |
| 209 | rc = -ENOMEM; |
| 210 | goto err; |
| 211 | } |
| 212 | idxd->wqs[i] = wq; |
| 213 | } |
| 214 | |
| 215 | return 0; |
| 216 | |
| 217 | err: |
| 218 | while (--i >= 0) |
| 219 | put_device(&idxd->wqs[i]->conf_dev); |
| 220 | return rc; |
| 221 | } |
| 222 | |
Dave Jiang | 75b9113 | 2021-04-15 16:37:44 -0700 | [diff] [blame] | 223 | static int idxd_setup_engines(struct idxd_device *idxd) |
| 224 | { |
| 225 | struct idxd_engine *engine; |
| 226 | struct device *dev = &idxd->pdev->dev; |
| 227 | int i, rc; |
| 228 | |
| 229 | idxd->engines = kcalloc_node(idxd->max_engines, sizeof(struct idxd_engine *), |
| 230 | GFP_KERNEL, dev_to_node(dev)); |
| 231 | if (!idxd->engines) |
| 232 | return -ENOMEM; |
| 233 | |
| 234 | for (i = 0; i < idxd->max_engines; i++) { |
| 235 | engine = kzalloc_node(sizeof(*engine), GFP_KERNEL, dev_to_node(dev)); |
| 236 | if (!engine) { |
| 237 | rc = -ENOMEM; |
| 238 | goto err; |
| 239 | } |
| 240 | |
| 241 | engine->id = i; |
| 242 | engine->idxd = idxd; |
| 243 | device_initialize(&engine->conf_dev); |
| 244 | engine->conf_dev.parent = &idxd->conf_dev; |
| 245 | engine->conf_dev.type = &idxd_engine_device_type; |
| 246 | rc = dev_set_name(&engine->conf_dev, "engine%d.%d", idxd->id, engine->id); |
| 247 | if (rc < 0) { |
| 248 | put_device(&engine->conf_dev); |
| 249 | goto err; |
| 250 | } |
| 251 | |
| 252 | idxd->engines[i] = engine; |
| 253 | } |
| 254 | |
| 255 | return 0; |
| 256 | |
| 257 | err: |
| 258 | while (--i >= 0) |
| 259 | put_device(&idxd->engines[i]->conf_dev); |
| 260 | return rc; |
| 261 | } |
| 262 | |
Dave Jiang | defe49f | 2021-04-15 16:37:51 -0700 | [diff] [blame] | 263 | static int idxd_setup_groups(struct idxd_device *idxd) |
| 264 | { |
| 265 | struct device *dev = &idxd->pdev->dev; |
| 266 | struct idxd_group *group; |
| 267 | int i, rc; |
| 268 | |
| 269 | idxd->groups = kcalloc_node(idxd->max_groups, sizeof(struct idxd_group *), |
| 270 | GFP_KERNEL, dev_to_node(dev)); |
| 271 | if (!idxd->groups) |
| 272 | return -ENOMEM; |
| 273 | |
| 274 | for (i = 0; i < idxd->max_groups; i++) { |
| 275 | group = kzalloc_node(sizeof(*group), GFP_KERNEL, dev_to_node(dev)); |
| 276 | if (!group) { |
| 277 | rc = -ENOMEM; |
| 278 | goto err; |
| 279 | } |
| 280 | |
| 281 | group->id = i; |
| 282 | group->idxd = idxd; |
| 283 | device_initialize(&group->conf_dev); |
| 284 | group->conf_dev.parent = &idxd->conf_dev; |
Dave Jiang | 4b73e4e | 2021-04-15 16:38:03 -0700 | [diff] [blame] | 285 | group->conf_dev.bus = &dsa_bus_type; |
Dave Jiang | defe49f | 2021-04-15 16:37:51 -0700 | [diff] [blame] | 286 | group->conf_dev.type = &idxd_group_device_type; |
| 287 | rc = dev_set_name(&group->conf_dev, "group%d.%d", idxd->id, group->id); |
| 288 | if (rc < 0) { |
| 289 | put_device(&group->conf_dev); |
| 290 | goto err; |
| 291 | } |
| 292 | |
| 293 | idxd->groups[i] = group; |
| 294 | group->tc_a = -1; |
| 295 | group->tc_b = -1; |
| 296 | } |
| 297 | |
| 298 | return 0; |
| 299 | |
| 300 | err: |
| 301 | while (--i >= 0) |
| 302 | put_device(&idxd->groups[i]->conf_dev); |
| 303 | return rc; |
| 304 | } |
| 305 | |
Dave Jiang | bfe1d56 | 2020-01-21 16:43:59 -0700 | [diff] [blame] | 306 | static int idxd_setup_internals(struct idxd_device *idxd) |
| 307 | { |
| 308 | struct device *dev = &idxd->pdev->dev; |
Dave Jiang | defe49f | 2021-04-15 16:37:51 -0700 | [diff] [blame] | 309 | int rc, i; |
Dave Jiang | bfe1d56 | 2020-01-21 16:43:59 -0700 | [diff] [blame] | 310 | |
Dave Jiang | 0d5c10b | 2020-06-26 11:11:18 -0700 | [diff] [blame] | 311 | init_waitqueue_head(&idxd->cmd_waitq); |
Dave Jiang | 7c5dd23 | 2021-04-15 16:37:39 -0700 | [diff] [blame] | 312 | |
Dave Jiang | eb15e71 | 2021-04-20 11:46:34 -0700 | [diff] [blame] | 313 | if (idxd->hw.cmd_cap & BIT(IDXD_CMD_REQUEST_INT_HANDLE)) { |
| 314 | idxd->int_handles = devm_kcalloc(dev, idxd->max_wqs, sizeof(int), GFP_KERNEL); |
| 315 | if (!idxd->int_handles) |
| 316 | return -ENOMEM; |
| 317 | } |
| 318 | |
Dave Jiang | 7c5dd23 | 2021-04-15 16:37:39 -0700 | [diff] [blame] | 319 | rc = idxd_setup_wqs(idxd); |
| 320 | if (rc < 0) |
Dave Jiang | eb15e71 | 2021-04-20 11:46:34 -0700 | [diff] [blame] | 321 | goto err_wqs; |
Dave Jiang | 7c5dd23 | 2021-04-15 16:37:39 -0700 | [diff] [blame] | 322 | |
Dave Jiang | 75b9113 | 2021-04-15 16:37:44 -0700 | [diff] [blame] | 323 | rc = idxd_setup_engines(idxd); |
| 324 | if (rc < 0) |
| 325 | goto err_engine; |
| 326 | |
Dave Jiang | defe49f | 2021-04-15 16:37:51 -0700 | [diff] [blame] | 327 | rc = idxd_setup_groups(idxd); |
| 328 | if (rc < 0) |
| 329 | goto err_group; |
Dave Jiang | bfe1d56 | 2020-01-21 16:43:59 -0700 | [diff] [blame] | 330 | |
Dave Jiang | 0d5c10b | 2020-06-26 11:11:18 -0700 | [diff] [blame] | 331 | idxd->wq = create_workqueue(dev_name(dev)); |
Dave Jiang | 7c5dd23 | 2021-04-15 16:37:39 -0700 | [diff] [blame] | 332 | if (!idxd->wq) { |
| 333 | rc = -ENOMEM; |
Dave Jiang | defe49f | 2021-04-15 16:37:51 -0700 | [diff] [blame] | 334 | goto err_wkq_create; |
Dave Jiang | 7c5dd23 | 2021-04-15 16:37:39 -0700 | [diff] [blame] | 335 | } |
Dave Jiang | 0d5c10b | 2020-06-26 11:11:18 -0700 | [diff] [blame] | 336 | |
Dave Jiang | bfe1d56 | 2020-01-21 16:43:59 -0700 | [diff] [blame] | 337 | return 0; |
Dave Jiang | 7c5dd23 | 2021-04-15 16:37:39 -0700 | [diff] [blame] | 338 | |
Dave Jiang | defe49f | 2021-04-15 16:37:51 -0700 | [diff] [blame] | 339 | err_wkq_create: |
| 340 | for (i = 0; i < idxd->max_groups; i++) |
| 341 | put_device(&idxd->groups[i]->conf_dev); |
| 342 | err_group: |
Dave Jiang | 75b9113 | 2021-04-15 16:37:44 -0700 | [diff] [blame] | 343 | for (i = 0; i < idxd->max_engines; i++) |
| 344 | put_device(&idxd->engines[i]->conf_dev); |
| 345 | err_engine: |
Dave Jiang | 7c5dd23 | 2021-04-15 16:37:39 -0700 | [diff] [blame] | 346 | for (i = 0; i < idxd->max_wqs; i++) |
| 347 | put_device(&idxd->wqs[i]->conf_dev); |
Dave Jiang | eb15e71 | 2021-04-20 11:46:34 -0700 | [diff] [blame] | 348 | err_wqs: |
| 349 | kfree(idxd->int_handles); |
Dave Jiang | 7c5dd23 | 2021-04-15 16:37:39 -0700 | [diff] [blame] | 350 | return rc; |
Dave Jiang | bfe1d56 | 2020-01-21 16:43:59 -0700 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | static void idxd_read_table_offsets(struct idxd_device *idxd) |
| 354 | { |
| 355 | union offsets_reg offsets; |
| 356 | struct device *dev = &idxd->pdev->dev; |
| 357 | |
| 358 | offsets.bits[0] = ioread64(idxd->reg_base + IDXD_TABLE_OFFSET); |
Dave Jiang | 2f8417a | 2020-10-30 08:51:56 -0700 | [diff] [blame] | 359 | offsets.bits[1] = ioread64(idxd->reg_base + IDXD_TABLE_OFFSET + sizeof(u64)); |
| 360 | idxd->grpcfg_offset = offsets.grpcfg * IDXD_TABLE_MULT; |
Dave Jiang | bfe1d56 | 2020-01-21 16:43:59 -0700 | [diff] [blame] | 361 | dev_dbg(dev, "IDXD Group Config Offset: %#x\n", idxd->grpcfg_offset); |
Dave Jiang | 2f8417a | 2020-10-30 08:51:56 -0700 | [diff] [blame] | 362 | idxd->wqcfg_offset = offsets.wqcfg * IDXD_TABLE_MULT; |
| 363 | dev_dbg(dev, "IDXD Work Queue Config Offset: %#x\n", idxd->wqcfg_offset); |
| 364 | idxd->msix_perm_offset = offsets.msix_perm * IDXD_TABLE_MULT; |
| 365 | dev_dbg(dev, "IDXD MSIX Permission Offset: %#x\n", idxd->msix_perm_offset); |
| 366 | idxd->perfmon_offset = offsets.perfmon * IDXD_TABLE_MULT; |
Dave Jiang | bfe1d56 | 2020-01-21 16:43:59 -0700 | [diff] [blame] | 367 | dev_dbg(dev, "IDXD Perfmon Offset: %#x\n", idxd->perfmon_offset); |
| 368 | } |
| 369 | |
| 370 | static void idxd_read_caps(struct idxd_device *idxd) |
| 371 | { |
| 372 | struct device *dev = &idxd->pdev->dev; |
| 373 | int i; |
| 374 | |
| 375 | /* reading generic capabilities */ |
| 376 | idxd->hw.gen_cap.bits = ioread64(idxd->reg_base + IDXD_GENCAP_OFFSET); |
| 377 | dev_dbg(dev, "gen_cap: %#llx\n", idxd->hw.gen_cap.bits); |
Dave Jiang | eb15e71 | 2021-04-20 11:46:34 -0700 | [diff] [blame] | 378 | |
| 379 | if (idxd->hw.gen_cap.cmd_cap) { |
| 380 | idxd->hw.cmd_cap = ioread32(idxd->reg_base + IDXD_CMDCAP_OFFSET); |
| 381 | dev_dbg(dev, "cmd_cap: %#x\n", idxd->hw.cmd_cap); |
| 382 | } |
| 383 | |
Dave Jiang | bfe1d56 | 2020-01-21 16:43:59 -0700 | [diff] [blame] | 384 | idxd->max_xfer_bytes = 1ULL << idxd->hw.gen_cap.max_xfer_shift; |
| 385 | dev_dbg(dev, "max xfer size: %llu bytes\n", idxd->max_xfer_bytes); |
| 386 | idxd->max_batch_size = 1U << idxd->hw.gen_cap.max_batch_shift; |
| 387 | dev_dbg(dev, "max batch size: %u\n", idxd->max_batch_size); |
| 388 | if (idxd->hw.gen_cap.config_en) |
| 389 | set_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags); |
| 390 | |
| 391 | /* reading group capabilities */ |
| 392 | idxd->hw.group_cap.bits = |
| 393 | ioread64(idxd->reg_base + IDXD_GRPCAP_OFFSET); |
| 394 | dev_dbg(dev, "group_cap: %#llx\n", idxd->hw.group_cap.bits); |
| 395 | idxd->max_groups = idxd->hw.group_cap.num_groups; |
| 396 | dev_dbg(dev, "max groups: %u\n", idxd->max_groups); |
| 397 | idxd->max_tokens = idxd->hw.group_cap.total_tokens; |
| 398 | dev_dbg(dev, "max tokens: %u\n", idxd->max_tokens); |
Dave Jiang | c52ca47 | 2020-01-21 16:44:05 -0700 | [diff] [blame] | 399 | idxd->nr_tokens = idxd->max_tokens; |
Dave Jiang | bfe1d56 | 2020-01-21 16:43:59 -0700 | [diff] [blame] | 400 | |
| 401 | /* read engine capabilities */ |
| 402 | idxd->hw.engine_cap.bits = |
| 403 | ioread64(idxd->reg_base + IDXD_ENGCAP_OFFSET); |
| 404 | dev_dbg(dev, "engine_cap: %#llx\n", idxd->hw.engine_cap.bits); |
| 405 | idxd->max_engines = idxd->hw.engine_cap.num_engines; |
| 406 | dev_dbg(dev, "max engines: %u\n", idxd->max_engines); |
| 407 | |
| 408 | /* read workqueue capabilities */ |
| 409 | idxd->hw.wq_cap.bits = ioread64(idxd->reg_base + IDXD_WQCAP_OFFSET); |
| 410 | dev_dbg(dev, "wq_cap: %#llx\n", idxd->hw.wq_cap.bits); |
| 411 | idxd->max_wq_size = idxd->hw.wq_cap.total_wq_size; |
| 412 | dev_dbg(dev, "total workqueue size: %u\n", idxd->max_wq_size); |
| 413 | idxd->max_wqs = idxd->hw.wq_cap.num_wqs; |
| 414 | dev_dbg(dev, "max workqueues: %u\n", idxd->max_wqs); |
Dave Jiang | d98793b | 2020-10-27 14:34:09 -0700 | [diff] [blame] | 415 | idxd->wqcfg_size = 1 << (idxd->hw.wq_cap.wqcfg_size + IDXD_WQCFG_MIN); |
| 416 | dev_dbg(dev, "wqcfg size: %u\n", idxd->wqcfg_size); |
Dave Jiang | bfe1d56 | 2020-01-21 16:43:59 -0700 | [diff] [blame] | 417 | |
| 418 | /* reading operation capabilities */ |
| 419 | for (i = 0; i < 4; i++) { |
| 420 | idxd->hw.opcap.bits[i] = ioread64(idxd->reg_base + |
| 421 | IDXD_OPCAP_OFFSET + i * sizeof(u64)); |
| 422 | dev_dbg(dev, "opcap[%d]: %#llx\n", i, idxd->hw.opcap.bits[i]); |
| 423 | } |
| 424 | } |
| 425 | |
Dave Jiang | 435b512 | 2021-04-15 16:38:09 -0700 | [diff] [blame] | 426 | static struct idxd_device *idxd_alloc(struct pci_dev *pdev, struct idxd_driver_data *data) |
Dave Jiang | bfe1d56 | 2020-01-21 16:43:59 -0700 | [diff] [blame] | 427 | { |
| 428 | struct device *dev = &pdev->dev; |
| 429 | struct idxd_device *idxd; |
Dave Jiang | 47c16ac | 2021-04-15 16:37:33 -0700 | [diff] [blame] | 430 | int rc; |
Dave Jiang | bfe1d56 | 2020-01-21 16:43:59 -0700 | [diff] [blame] | 431 | |
Dave Jiang | 47c16ac | 2021-04-15 16:37:33 -0700 | [diff] [blame] | 432 | idxd = kzalloc_node(sizeof(*idxd), GFP_KERNEL, dev_to_node(dev)); |
Dave Jiang | bfe1d56 | 2020-01-21 16:43:59 -0700 | [diff] [blame] | 433 | if (!idxd) |
| 434 | return NULL; |
| 435 | |
| 436 | idxd->pdev = pdev; |
Dave Jiang | 435b512 | 2021-04-15 16:38:09 -0700 | [diff] [blame] | 437 | idxd->data = data; |
Dave Jiang | 4b73e4e | 2021-04-15 16:38:03 -0700 | [diff] [blame] | 438 | idxd->id = ida_alloc(&idxd_ida, GFP_KERNEL); |
Dave Jiang | 47c16ac | 2021-04-15 16:37:33 -0700 | [diff] [blame] | 439 | if (idxd->id < 0) |
| 440 | return NULL; |
| 441 | |
| 442 | device_initialize(&idxd->conf_dev); |
| 443 | idxd->conf_dev.parent = dev; |
Dave Jiang | 4b73e4e | 2021-04-15 16:38:03 -0700 | [diff] [blame] | 444 | idxd->conf_dev.bus = &dsa_bus_type; |
Dave Jiang | 435b512 | 2021-04-15 16:38:09 -0700 | [diff] [blame] | 445 | idxd->conf_dev.type = idxd->data->dev_type; |
| 446 | rc = dev_set_name(&idxd->conf_dev, "%s%d", idxd->data->name_prefix, idxd->id); |
Dave Jiang | 47c16ac | 2021-04-15 16:37:33 -0700 | [diff] [blame] | 447 | if (rc < 0) { |
| 448 | put_device(&idxd->conf_dev); |
| 449 | return NULL; |
| 450 | } |
| 451 | |
Dave Jiang | bfe1d56 | 2020-01-21 16:43:59 -0700 | [diff] [blame] | 452 | spin_lock_init(&idxd->dev_lock); |
Dave Jiang | 53b2ee7f | 2021-04-20 12:00:56 -0700 | [diff] [blame] | 453 | spin_lock_init(&idxd->cmd_lock); |
Dave Jiang | bfe1d56 | 2020-01-21 16:43:59 -0700 | [diff] [blame] | 454 | |
| 455 | return idxd; |
| 456 | } |
| 457 | |
Dave Jiang | 8e50d39 | 2020-10-27 10:34:35 -0700 | [diff] [blame] | 458 | static int idxd_enable_system_pasid(struct idxd_device *idxd) |
| 459 | { |
| 460 | int flags; |
| 461 | unsigned int pasid; |
| 462 | struct iommu_sva *sva; |
| 463 | |
| 464 | flags = SVM_FLAG_SUPERVISOR_MODE; |
| 465 | |
| 466 | sva = iommu_sva_bind_device(&idxd->pdev->dev, NULL, &flags); |
| 467 | if (IS_ERR(sva)) { |
| 468 | dev_warn(&idxd->pdev->dev, |
| 469 | "iommu sva bind failed: %ld\n", PTR_ERR(sva)); |
| 470 | return PTR_ERR(sva); |
| 471 | } |
| 472 | |
| 473 | pasid = iommu_sva_get_pasid(sva); |
| 474 | if (pasid == IOMMU_PASID_INVALID) { |
| 475 | iommu_sva_unbind_device(sva); |
| 476 | return -ENODEV; |
| 477 | } |
| 478 | |
| 479 | idxd->sva = sva; |
| 480 | idxd->pasid = pasid; |
| 481 | dev_dbg(&idxd->pdev->dev, "system pasid: %u\n", pasid); |
| 482 | return 0; |
| 483 | } |
| 484 | |
| 485 | static void idxd_disable_system_pasid(struct idxd_device *idxd) |
| 486 | { |
| 487 | |
| 488 | iommu_sva_unbind_device(idxd->sva); |
| 489 | idxd->sva = NULL; |
| 490 | } |
| 491 | |
Dave Jiang | bfe1d56 | 2020-01-21 16:43:59 -0700 | [diff] [blame] | 492 | static int idxd_probe(struct idxd_device *idxd) |
| 493 | { |
| 494 | struct pci_dev *pdev = idxd->pdev; |
| 495 | struct device *dev = &pdev->dev; |
| 496 | int rc; |
| 497 | |
| 498 | dev_dbg(dev, "%s entered and resetting device\n", __func__); |
Dave Jiang | 89e3bec | 2021-02-01 08:26:14 -0700 | [diff] [blame] | 499 | rc = idxd_device_init_reset(idxd); |
| 500 | if (rc < 0) |
| 501 | return rc; |
| 502 | |
Dave Jiang | bfe1d56 | 2020-01-21 16:43:59 -0700 | [diff] [blame] | 503 | dev_dbg(dev, "IDXD reset complete\n"); |
| 504 | |
Dave Jiang | 03d939c | 2021-01-22 11:46:00 -0700 | [diff] [blame] | 505 | if (IS_ENABLED(CONFIG_INTEL_IDXD_SVM) && sva) { |
Dave Jiang | cf5f86a | 2021-04-20 11:46:46 -0700 | [diff] [blame] | 506 | rc = iommu_dev_enable_feature(dev, IOMMU_DEV_FEAT_SVA); |
| 507 | if (rc == 0) { |
| 508 | rc = idxd_enable_system_pasid(idxd); |
| 509 | if (rc < 0) { |
| 510 | iommu_dev_disable_feature(dev, IOMMU_DEV_FEAT_SVA); |
| 511 | dev_warn(dev, "Failed to enable PASID. No SVA support: %d\n", rc); |
| 512 | } else { |
| 513 | set_bit(IDXD_FLAG_PASID_ENABLED, &idxd->flags); |
| 514 | } |
| 515 | } else { |
| 516 | dev_warn(dev, "Unable to turn on SVA feature.\n"); |
| 517 | } |
Dave Jiang | 03d939c | 2021-01-22 11:46:00 -0700 | [diff] [blame] | 518 | } else if (!sva) { |
| 519 | dev_warn(dev, "User forced SVA off via module param.\n"); |
Dave Jiang | 8e50d39 | 2020-10-27 10:34:35 -0700 | [diff] [blame] | 520 | } |
| 521 | |
Dave Jiang | bfe1d56 | 2020-01-21 16:43:59 -0700 | [diff] [blame] | 522 | idxd_read_caps(idxd); |
| 523 | idxd_read_table_offsets(idxd); |
| 524 | |
| 525 | rc = idxd_setup_internals(idxd); |
| 526 | if (rc) |
Dave Jiang | 7c5dd23 | 2021-04-15 16:37:39 -0700 | [diff] [blame] | 527 | goto err; |
Dave Jiang | bfe1d56 | 2020-01-21 16:43:59 -0700 | [diff] [blame] | 528 | |
Dave Jiang | 8c66bbdc | 2021-04-20 11:46:28 -0700 | [diff] [blame] | 529 | /* If the configs are readonly, then load them from device */ |
| 530 | if (!test_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags)) { |
| 531 | dev_dbg(dev, "Loading RO device config\n"); |
| 532 | rc = idxd_device_load_config(idxd); |
| 533 | if (rc < 0) |
| 534 | goto err; |
| 535 | } |
| 536 | |
Dave Jiang | bfe1d56 | 2020-01-21 16:43:59 -0700 | [diff] [blame] | 537 | rc = idxd_setup_interrupts(idxd); |
| 538 | if (rc) |
Dave Jiang | 7c5dd23 | 2021-04-15 16:37:39 -0700 | [diff] [blame] | 539 | goto err; |
Dave Jiang | bfe1d56 | 2020-01-21 16:43:59 -0700 | [diff] [blame] | 540 | |
| 541 | dev_dbg(dev, "IDXD interrupt setup complete.\n"); |
| 542 | |
Dave Jiang | 42d279f | 2020-01-21 16:44:29 -0700 | [diff] [blame] | 543 | idxd->major = idxd_cdev_get_major(idxd); |
| 544 | |
Tom Zanussi | 0bde444 | 2021-04-24 10:04:16 -0500 | [diff] [blame^] | 545 | rc = perfmon_pmu_init(idxd); |
| 546 | if (rc < 0) |
| 547 | dev_warn(dev, "Failed to initialize perfmon. No PMU support: %d\n", rc); |
| 548 | |
Dave Jiang | bfe1d56 | 2020-01-21 16:43:59 -0700 | [diff] [blame] | 549 | dev_dbg(dev, "IDXD device %d probed successfully\n", idxd->id); |
| 550 | return 0; |
| 551 | |
Dave Jiang | 7c5dd23 | 2021-04-15 16:37:39 -0700 | [diff] [blame] | 552 | err: |
Dave Jiang | 8e50d39 | 2020-10-27 10:34:35 -0700 | [diff] [blame] | 553 | if (device_pasid_enabled(idxd)) |
| 554 | idxd_disable_system_pasid(idxd); |
Dave Jiang | cf5f86a | 2021-04-20 11:46:46 -0700 | [diff] [blame] | 555 | iommu_dev_disable_feature(dev, IOMMU_DEV_FEAT_SVA); |
Dave Jiang | bfe1d56 | 2020-01-21 16:43:59 -0700 | [diff] [blame] | 556 | return rc; |
| 557 | } |
| 558 | |
| 559 | static int idxd_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id) |
| 560 | { |
Dave Jiang | bfe1d56 | 2020-01-21 16:43:59 -0700 | [diff] [blame] | 561 | struct device *dev = &pdev->dev; |
| 562 | struct idxd_device *idxd; |
Dave Jiang | 435b512 | 2021-04-15 16:38:09 -0700 | [diff] [blame] | 563 | struct idxd_driver_data *data = (struct idxd_driver_data *)id->driver_data; |
Dave Jiang | bfe1d56 | 2020-01-21 16:43:59 -0700 | [diff] [blame] | 564 | int rc; |
Dave Jiang | bfe1d56 | 2020-01-21 16:43:59 -0700 | [diff] [blame] | 565 | |
Dave Jiang | a39c7cd | 2021-04-15 16:37:21 -0700 | [diff] [blame] | 566 | rc = pci_enable_device(pdev); |
Dave Jiang | bfe1d56 | 2020-01-21 16:43:59 -0700 | [diff] [blame] | 567 | if (rc) |
| 568 | return rc; |
| 569 | |
Dave Jiang | 8e50d39 | 2020-10-27 10:34:35 -0700 | [diff] [blame] | 570 | dev_dbg(dev, "Alloc IDXD context\n"); |
Dave Jiang | 435b512 | 2021-04-15 16:38:09 -0700 | [diff] [blame] | 571 | idxd = idxd_alloc(pdev, data); |
Dave Jiang | a39c7cd | 2021-04-15 16:37:21 -0700 | [diff] [blame] | 572 | if (!idxd) { |
| 573 | rc = -ENOMEM; |
| 574 | goto err_idxd_alloc; |
| 575 | } |
Dave Jiang | bfe1d56 | 2020-01-21 16:43:59 -0700 | [diff] [blame] | 576 | |
Dave Jiang | 8e50d39 | 2020-10-27 10:34:35 -0700 | [diff] [blame] | 577 | dev_dbg(dev, "Mapping BARs\n"); |
Dave Jiang | a39c7cd | 2021-04-15 16:37:21 -0700 | [diff] [blame] | 578 | idxd->reg_base = pci_iomap(pdev, IDXD_MMIO_BAR, 0); |
| 579 | if (!idxd->reg_base) { |
| 580 | rc = -ENOMEM; |
| 581 | goto err_iomap; |
| 582 | } |
Dave Jiang | bfe1d56 | 2020-01-21 16:43:59 -0700 | [diff] [blame] | 583 | |
| 584 | dev_dbg(dev, "Set DMA masks\n"); |
| 585 | rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64)); |
| 586 | if (rc) |
| 587 | rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32)); |
| 588 | if (rc) |
Dave Jiang | a39c7cd | 2021-04-15 16:37:21 -0700 | [diff] [blame] | 589 | goto err; |
Dave Jiang | bfe1d56 | 2020-01-21 16:43:59 -0700 | [diff] [blame] | 590 | |
| 591 | rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64)); |
| 592 | if (rc) |
| 593 | rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)); |
| 594 | if (rc) |
Dave Jiang | a39c7cd | 2021-04-15 16:37:21 -0700 | [diff] [blame] | 595 | goto err; |
Dave Jiang | bfe1d56 | 2020-01-21 16:43:59 -0700 | [diff] [blame] | 596 | |
Dave Jiang | bfe1d56 | 2020-01-21 16:43:59 -0700 | [diff] [blame] | 597 | dev_dbg(dev, "Set PCI master\n"); |
| 598 | pci_set_master(pdev); |
| 599 | pci_set_drvdata(pdev, idxd); |
| 600 | |
| 601 | idxd->hw.version = ioread32(idxd->reg_base + IDXD_VER_OFFSET); |
| 602 | rc = idxd_probe(idxd); |
| 603 | if (rc) { |
| 604 | dev_err(dev, "Intel(R) IDXD DMA Engine init failed\n"); |
Dave Jiang | a39c7cd | 2021-04-15 16:37:21 -0700 | [diff] [blame] | 605 | goto err; |
Dave Jiang | bfe1d56 | 2020-01-21 16:43:59 -0700 | [diff] [blame] | 606 | } |
| 607 | |
Dave Jiang | 47c16ac | 2021-04-15 16:37:33 -0700 | [diff] [blame] | 608 | rc = idxd_register_devices(idxd); |
Dave Jiang | c52ca47 | 2020-01-21 16:44:05 -0700 | [diff] [blame] | 609 | if (rc) { |
| 610 | dev_err(dev, "IDXD sysfs setup failed\n"); |
Dave Jiang | a39c7cd | 2021-04-15 16:37:21 -0700 | [diff] [blame] | 611 | goto err; |
Dave Jiang | c52ca47 | 2020-01-21 16:44:05 -0700 | [diff] [blame] | 612 | } |
| 613 | |
| 614 | idxd->state = IDXD_DEV_CONF_READY; |
| 615 | |
Dave Jiang | bfe1d56 | 2020-01-21 16:43:59 -0700 | [diff] [blame] | 616 | dev_info(&pdev->dev, "Intel(R) Accelerator Device (v%x)\n", |
| 617 | idxd->hw.version); |
| 618 | |
| 619 | return 0; |
Dave Jiang | a39c7cd | 2021-04-15 16:37:21 -0700 | [diff] [blame] | 620 | |
| 621 | err: |
| 622 | pci_iounmap(pdev, idxd->reg_base); |
| 623 | err_iomap: |
Dave Jiang | 47c16ac | 2021-04-15 16:37:33 -0700 | [diff] [blame] | 624 | put_device(&idxd->conf_dev); |
Dave Jiang | a39c7cd | 2021-04-15 16:37:21 -0700 | [diff] [blame] | 625 | err_idxd_alloc: |
| 626 | pci_disable_device(pdev); |
| 627 | return rc; |
Dave Jiang | bfe1d56 | 2020-01-21 16:43:59 -0700 | [diff] [blame] | 628 | } |
| 629 | |
Dave Jiang | 8f47d1a | 2020-01-21 16:44:23 -0700 | [diff] [blame] | 630 | static void idxd_flush_pending_llist(struct idxd_irq_entry *ie) |
| 631 | { |
| 632 | struct idxd_desc *desc, *itr; |
| 633 | struct llist_node *head; |
| 634 | |
| 635 | head = llist_del_all(&ie->pending_llist); |
| 636 | if (!head) |
| 637 | return; |
| 638 | |
| 639 | llist_for_each_entry_safe(desc, itr, head, llnode) { |
| 640 | idxd_dma_complete_txd(desc, IDXD_COMPLETE_ABORT); |
| 641 | idxd_free_desc(desc->wq, desc); |
| 642 | } |
| 643 | } |
| 644 | |
| 645 | static void idxd_flush_work_list(struct idxd_irq_entry *ie) |
| 646 | { |
| 647 | struct idxd_desc *desc, *iter; |
| 648 | |
| 649 | list_for_each_entry_safe(desc, iter, &ie->work_list, list) { |
| 650 | list_del(&desc->list); |
| 651 | idxd_dma_complete_txd(desc, IDXD_COMPLETE_ABORT); |
| 652 | idxd_free_desc(desc->wq, desc); |
| 653 | } |
| 654 | } |
| 655 | |
Dave Jiang | 5b0c68c | 2021-04-20 11:46:51 -0700 | [diff] [blame] | 656 | void idxd_wqs_quiesce(struct idxd_device *idxd) |
| 657 | { |
| 658 | struct idxd_wq *wq; |
| 659 | int i; |
| 660 | |
| 661 | for (i = 0; i < idxd->max_wqs; i++) { |
| 662 | wq = idxd->wqs[i]; |
| 663 | if (wq->state == IDXD_WQ_ENABLED && wq->type == IDXD_WQT_KERNEL) |
| 664 | idxd_wq_quiesce(wq); |
| 665 | } |
| 666 | } |
| 667 | |
Dave Jiang | eb15e71 | 2021-04-20 11:46:34 -0700 | [diff] [blame] | 668 | static void idxd_release_int_handles(struct idxd_device *idxd) |
| 669 | { |
| 670 | struct device *dev = &idxd->pdev->dev; |
| 671 | int i, rc; |
| 672 | |
| 673 | for (i = 0; i < idxd->num_wq_irqs; i++) { |
| 674 | if (idxd->hw.cmd_cap & BIT(IDXD_CMD_RELEASE_INT_HANDLE)) { |
| 675 | rc = idxd_device_release_int_handle(idxd, idxd->int_handles[i], |
| 676 | IDXD_IRQ_MSIX); |
| 677 | if (rc < 0) |
| 678 | dev_warn(dev, "irq handle %d release failed\n", |
| 679 | idxd->int_handles[i]); |
| 680 | else |
| 681 | dev_dbg(dev, "int handle requested: %u\n", idxd->int_handles[i]); |
| 682 | } |
| 683 | } |
| 684 | } |
| 685 | |
Dave Jiang | bfe1d56 | 2020-01-21 16:43:59 -0700 | [diff] [blame] | 686 | static void idxd_shutdown(struct pci_dev *pdev) |
| 687 | { |
| 688 | struct idxd_device *idxd = pci_get_drvdata(pdev); |
| 689 | int rc, i; |
| 690 | struct idxd_irq_entry *irq_entry; |
| 691 | int msixcnt = pci_msix_vec_count(pdev); |
Dave Jiang | bfe1d56 | 2020-01-21 16:43:59 -0700 | [diff] [blame] | 692 | |
Dave Jiang | bfe1d56 | 2020-01-21 16:43:59 -0700 | [diff] [blame] | 693 | rc = idxd_device_disable(idxd); |
Dave Jiang | bfe1d56 | 2020-01-21 16:43:59 -0700 | [diff] [blame] | 694 | if (rc) |
| 695 | dev_err(&pdev->dev, "Disabling device failed\n"); |
| 696 | |
| 697 | dev_dbg(&pdev->dev, "%s called\n", __func__); |
| 698 | idxd_mask_msix_vectors(idxd); |
| 699 | idxd_mask_error_interrupts(idxd); |
| 700 | |
| 701 | for (i = 0; i < msixcnt; i++) { |
| 702 | irq_entry = &idxd->irq_entries[i]; |
Dave Jiang | 5fc8e85 | 2021-04-15 16:37:15 -0700 | [diff] [blame] | 703 | synchronize_irq(irq_entry->vector); |
| 704 | free_irq(irq_entry->vector, irq_entry); |
Dave Jiang | bfe1d56 | 2020-01-21 16:43:59 -0700 | [diff] [blame] | 705 | if (i == 0) |
| 706 | continue; |
Dave Jiang | 8f47d1a | 2020-01-21 16:44:23 -0700 | [diff] [blame] | 707 | idxd_flush_pending_llist(irq_entry); |
| 708 | idxd_flush_work_list(irq_entry); |
Dave Jiang | bfe1d56 | 2020-01-21 16:43:59 -0700 | [diff] [blame] | 709 | } |
Dave Jiang | 0d5c10b | 2020-06-26 11:11:18 -0700 | [diff] [blame] | 710 | |
Dave Jiang | 6df0e6c | 2021-04-12 09:23:27 -0700 | [diff] [blame] | 711 | idxd_msix_perm_clear(idxd); |
Dave Jiang | eb15e71 | 2021-04-20 11:46:34 -0700 | [diff] [blame] | 712 | idxd_release_int_handles(idxd); |
Dave Jiang | 5fc8e85 | 2021-04-15 16:37:15 -0700 | [diff] [blame] | 713 | pci_free_irq_vectors(pdev); |
Dave Jiang | a39c7cd | 2021-04-15 16:37:21 -0700 | [diff] [blame] | 714 | pci_iounmap(pdev, idxd->reg_base); |
| 715 | pci_disable_device(pdev); |
Dave Jiang | 0d5c10b | 2020-06-26 11:11:18 -0700 | [diff] [blame] | 716 | destroy_workqueue(idxd->wq); |
Dave Jiang | bfe1d56 | 2020-01-21 16:43:59 -0700 | [diff] [blame] | 717 | } |
| 718 | |
| 719 | static void idxd_remove(struct pci_dev *pdev) |
| 720 | { |
| 721 | struct idxd_device *idxd = pci_get_drvdata(pdev); |
| 722 | |
| 723 | dev_dbg(&pdev->dev, "%s called\n", __func__); |
| 724 | idxd_shutdown(pdev); |
Dave Jiang | 8e50d39 | 2020-10-27 10:34:35 -0700 | [diff] [blame] | 725 | if (device_pasid_enabled(idxd)) |
| 726 | idxd_disable_system_pasid(idxd); |
Dave Jiang | 47c16ac | 2021-04-15 16:37:33 -0700 | [diff] [blame] | 727 | idxd_unregister_devices(idxd); |
Tom Zanussi | 0bde444 | 2021-04-24 10:04:16 -0500 | [diff] [blame^] | 728 | perfmon_pmu_remove(idxd); |
Dave Jiang | cf5f86a | 2021-04-20 11:46:46 -0700 | [diff] [blame] | 729 | iommu_dev_disable_feature(&pdev->dev, IOMMU_DEV_FEAT_SVA); |
Dave Jiang | bfe1d56 | 2020-01-21 16:43:59 -0700 | [diff] [blame] | 730 | } |
| 731 | |
| 732 | static struct pci_driver idxd_pci_driver = { |
| 733 | .name = DRV_NAME, |
| 734 | .id_table = idxd_pci_tbl, |
| 735 | .probe = idxd_pci_probe, |
| 736 | .remove = idxd_remove, |
| 737 | .shutdown = idxd_shutdown, |
| 738 | }; |
| 739 | |
| 740 | static int __init idxd_init_module(void) |
| 741 | { |
Dave Jiang | 4b73e4e | 2021-04-15 16:38:03 -0700 | [diff] [blame] | 742 | int err; |
Dave Jiang | bfe1d56 | 2020-01-21 16:43:59 -0700 | [diff] [blame] | 743 | |
| 744 | /* |
Dave Jiang | 8e50d39 | 2020-10-27 10:34:35 -0700 | [diff] [blame] | 745 | * 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] | 746 | * enumerating the device. We can not utilize it. |
| 747 | */ |
| 748 | if (!boot_cpu_has(X86_FEATURE_MOVDIR64B)) { |
| 749 | pr_warn("idxd driver failed to load without MOVDIR64B.\n"); |
| 750 | return -ENODEV; |
| 751 | } |
| 752 | |
Dave Jiang | 8e50d39 | 2020-10-27 10:34:35 -0700 | [diff] [blame] | 753 | if (!boot_cpu_has(X86_FEATURE_ENQCMD)) |
| 754 | pr_warn("Platform does not have ENQCMD(S) support.\n"); |
| 755 | else |
| 756 | support_enqcmd = true; |
Dave Jiang | bfe1d56 | 2020-01-21 16:43:59 -0700 | [diff] [blame] | 757 | |
Tom Zanussi | 0bde444 | 2021-04-24 10:04:16 -0500 | [diff] [blame^] | 758 | perfmon_init(); |
| 759 | |
Dave Jiang | c52ca47 | 2020-01-21 16:44:05 -0700 | [diff] [blame] | 760 | err = idxd_register_bus_type(); |
| 761 | if (err < 0) |
Dave Jiang | bfe1d56 | 2020-01-21 16:43:59 -0700 | [diff] [blame] | 762 | return err; |
| 763 | |
Dave Jiang | c52ca47 | 2020-01-21 16:44:05 -0700 | [diff] [blame] | 764 | err = idxd_register_driver(); |
| 765 | if (err < 0) |
| 766 | goto err_idxd_driver_register; |
| 767 | |
Dave Jiang | 42d279f | 2020-01-21 16:44:29 -0700 | [diff] [blame] | 768 | err = idxd_cdev_register(); |
| 769 | if (err) |
| 770 | goto err_cdev_register; |
| 771 | |
Dave Jiang | c52ca47 | 2020-01-21 16:44:05 -0700 | [diff] [blame] | 772 | err = pci_register_driver(&idxd_pci_driver); |
| 773 | if (err) |
| 774 | goto err_pci_register; |
| 775 | |
Dave Jiang | bfe1d56 | 2020-01-21 16:43:59 -0700 | [diff] [blame] | 776 | return 0; |
Dave Jiang | c52ca47 | 2020-01-21 16:44:05 -0700 | [diff] [blame] | 777 | |
| 778 | err_pci_register: |
Dave Jiang | 42d279f | 2020-01-21 16:44:29 -0700 | [diff] [blame] | 779 | idxd_cdev_remove(); |
| 780 | err_cdev_register: |
Dave Jiang | c52ca47 | 2020-01-21 16:44:05 -0700 | [diff] [blame] | 781 | idxd_unregister_driver(); |
| 782 | err_idxd_driver_register: |
| 783 | idxd_unregister_bus_type(); |
| 784 | return err; |
Dave Jiang | bfe1d56 | 2020-01-21 16:43:59 -0700 | [diff] [blame] | 785 | } |
| 786 | module_init(idxd_init_module); |
| 787 | |
| 788 | static void __exit idxd_exit_module(void) |
| 789 | { |
| 790 | pci_unregister_driver(&idxd_pci_driver); |
Dave Jiang | 42d279f | 2020-01-21 16:44:29 -0700 | [diff] [blame] | 791 | idxd_cdev_remove(); |
Dave Jiang | c52ca47 | 2020-01-21 16:44:05 -0700 | [diff] [blame] | 792 | idxd_unregister_bus_type(); |
Tom Zanussi | 0bde444 | 2021-04-24 10:04:16 -0500 | [diff] [blame^] | 793 | perfmon_exit(); |
Dave Jiang | bfe1d56 | 2020-01-21 16:43:59 -0700 | [diff] [blame] | 794 | } |
| 795 | module_exit(idxd_exit_module); |