blob: 07cf7977a04500dc276332abd035ae51d8cb84dd [file] [log] [blame]
Dave Jiangbfe1d562020-01-21 16:43:59 -07001// SPDX-License-Identifier: GPL-2.0
2/* Copyright(c) 2019 Intel Corporation. All rights rsvd. */
3#include <linux/init.h>
4#include <linux/kernel.h>
5#include <linux/module.h>
6#include <linux/slab.h>
7#include <linux/pci.h>
8#include <linux/interrupt.h>
9#include <linux/delay.h>
10#include <linux/dma-mapping.h>
11#include <linux/workqueue.h>
12#include <linux/aer.h>
13#include <linux/fs.h>
14#include <linux/io-64-nonatomic-lo-hi.h>
15#include <linux/device.h>
16#include <linux/idr.h>
Dave Jiang8e50d392020-10-27 10:34:35 -070017#include <linux/intel-svm.h>
18#include <linux/iommu.h>
Dave Jiangbfe1d562020-01-21 16:43:59 -070019#include <uapi/linux/idxd.h>
Dave Jiang8f47d1a2020-01-21 16:44:23 -070020#include <linux/dmaengine.h>
21#include "../dmaengine.h"
Dave Jiangbfe1d562020-01-21 16:43:59 -070022#include "registers.h"
23#include "idxd.h"
24
25MODULE_VERSION(IDXD_DRIVER_VERSION);
26MODULE_LICENSE("GPL v2");
27MODULE_AUTHOR("Intel Corporation");
28
Dave Jiang03d939c2021-01-22 11:46:00 -070029static bool sva = true;
30module_param(sva, bool, 0644);
31MODULE_PARM_DESC(sva, "Toggle SVA support on/off");
32
Dave Jiangbfe1d562020-01-21 16:43:59 -070033#define DRV_NAME "idxd"
34
Dave Jiang8e50d392020-10-27 10:34:35 -070035bool support_enqcmd;
36
Dave Jiangf7f77392021-04-15 16:37:27 -070037static struct ida idxd_idas[IDXD_TYPE_MAX];
Dave Jiangbfe1d562020-01-21 16:43:59 -070038
39static struct pci_device_id idxd_pci_tbl[] = {
40 /* DSA ver 1.0 platforms */
41 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_DSA_SPR0) },
Dave Jiangf25b46382020-11-17 13:39:14 -070042
43 /* IAX ver 1.0 platforms */
44 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IAX_SPR0) },
Dave Jiangbfe1d562020-01-21 16:43:59 -070045 { 0, }
46};
47MODULE_DEVICE_TABLE(pci, idxd_pci_tbl);
48
49static char *idxd_name[] = {
50 "dsa",
Dave Jiangf25b46382020-11-17 13:39:14 -070051 "iax"
Dave Jiangbfe1d562020-01-21 16:43:59 -070052};
53
Dave Jiang47c16ac2021-04-15 16:37:33 -070054struct ida *idxd_ida(struct idxd_device *idxd)
55{
56 return &idxd_idas[idxd->type];
57}
58
Dave Jiangbfe1d562020-01-21 16:43:59 -070059const char *idxd_get_dev_name(struct idxd_device *idxd)
60{
61 return idxd_name[idxd->type];
62}
63
64static int idxd_setup_interrupts(struct idxd_device *idxd)
65{
66 struct pci_dev *pdev = idxd->pdev;
67 struct device *dev = &pdev->dev;
Dave Jiangbfe1d562020-01-21 16:43:59 -070068 struct idxd_irq_entry *irq_entry;
69 int i, msixcnt;
70 int rc = 0;
71
72 msixcnt = pci_msix_vec_count(pdev);
73 if (msixcnt < 0) {
74 dev_err(dev, "Not MSI-X interrupt capable.\n");
Dave Jiang5fc8e852021-04-15 16:37:15 -070075 return -ENOSPC;
Dave Jiangbfe1d562020-01-21 16:43:59 -070076 }
77
Dave Jiang5fc8e852021-04-15 16:37:15 -070078 rc = pci_alloc_irq_vectors(pdev, msixcnt, msixcnt, PCI_IRQ_MSIX);
79 if (rc != msixcnt) {
80 dev_err(dev, "Failed enabling %d MSIX entries: %d\n", msixcnt, rc);
81 return -ENOSPC;
Dave Jiangbfe1d562020-01-21 16:43:59 -070082 }
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 */
Dave Jiang47c16ac2021-04-15 16:37:33 -070089 idxd->irq_entries = kcalloc_node(msixcnt, sizeof(struct idxd_irq_entry),
90 GFP_KERNEL, dev_to_node(dev));
Dave Jiangbfe1d562020-01-21 16:43:59 -070091 if (!idxd->irq_entries) {
92 rc = -ENOMEM;
Dave Jiang5fc8e852021-04-15 16:37:15 -070093 goto err_irq_entries;
Dave Jiangbfe1d562020-01-21 16:43:59 -070094 }
95
96 for (i = 0; i < msixcnt; i++) {
97 idxd->irq_entries[i].id = i;
98 idxd->irq_entries[i].idxd = idxd;
Dave Jiang5fc8e852021-04-15 16:37:15 -070099 idxd->irq_entries[i].vector = pci_irq_vector(pdev, i);
Dave Jiange4f4d8c2020-10-27 10:34:40 -0700100 spin_lock_init(&idxd->irq_entries[i].list_lock);
Dave Jiangbfe1d562020-01-21 16:43:59 -0700101 }
102
Dave Jiangbfe1d562020-01-21 16:43:59 -0700103 irq_entry = &idxd->irq_entries[0];
Dave Jiang5fc8e852021-04-15 16:37:15 -0700104 rc = request_threaded_irq(irq_entry->vector, idxd_irq_handler, idxd_misc_thread,
105 0, "idxd-misc", irq_entry);
Dave Jiangbfe1d562020-01-21 16:43:59 -0700106 if (rc < 0) {
107 dev_err(dev, "Failed to allocate misc interrupt.\n");
Dave Jiang5fc8e852021-04-15 16:37:15 -0700108 goto err_misc_irq;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700109 }
110
Dave Jiang5fc8e852021-04-15 16:37:15 -0700111 dev_dbg(dev, "Allocated idxd-misc handler on msix vector %d\n", irq_entry->vector);
Dave Jiangbfe1d562020-01-21 16:43:59 -0700112
113 /* first MSI-X entry is not for wq interrupts */
114 idxd->num_wq_irqs = msixcnt - 1;
115
116 for (i = 1; i < msixcnt; i++) {
Dave Jiangbfe1d562020-01-21 16:43:59 -0700117 irq_entry = &idxd->irq_entries[i];
118
119 init_llist_head(&idxd->irq_entries[i].pending_llist);
120 INIT_LIST_HEAD(&idxd->irq_entries[i].work_list);
Dave Jiang5fc8e852021-04-15 16:37:15 -0700121 rc = request_threaded_irq(irq_entry->vector, idxd_irq_handler,
122 idxd_wq_thread, 0, "idxd-portal", irq_entry);
Dave Jiangbfe1d562020-01-21 16:43:59 -0700123 if (rc < 0) {
Dave Jiang5fc8e852021-04-15 16:37:15 -0700124 dev_err(dev, "Failed to allocate irq %d.\n", irq_entry->vector);
125 goto err_wq_irqs;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700126 }
Dave Jiang5fc8e852021-04-15 16:37:15 -0700127 dev_dbg(dev, "Allocated idxd-msix %d for vector %d\n", i, irq_entry->vector);
Dave Jiangbfe1d562020-01-21 16:43:59 -0700128 }
129
130 idxd_unmask_error_interrupts(idxd);
Dave Jiang6df0e6c2021-04-12 09:23:27 -0700131 idxd_msix_perm_setup(idxd);
Dave Jiangbfe1d562020-01-21 16:43:59 -0700132 return 0;
133
Dave Jiang5fc8e852021-04-15 16:37:15 -0700134 err_wq_irqs:
135 while (--i >= 0) {
136 irq_entry = &idxd->irq_entries[i];
137 free_irq(irq_entry->vector, irq_entry);
138 }
139 err_misc_irq:
Dave Jiangbfe1d562020-01-21 16:43:59 -0700140 /* Disable error interrupt generation */
141 idxd_mask_error_interrupts(idxd);
Dave Jiang5fc8e852021-04-15 16:37:15 -0700142 err_irq_entries:
143 pci_free_irq_vectors(pdev);
Dave Jiangbfe1d562020-01-21 16:43:59 -0700144 dev_err(dev, "No usable interrupts\n");
145 return rc;
146}
147
Dave Jiang7c5dd232021-04-15 16:37:39 -0700148static int idxd_setup_wqs(struct idxd_device *idxd)
149{
150 struct device *dev = &idxd->pdev->dev;
151 struct idxd_wq *wq;
152 int i, rc;
153
154 idxd->wqs = kcalloc_node(idxd->max_wqs, sizeof(struct idxd_wq *),
155 GFP_KERNEL, dev_to_node(dev));
156 if (!idxd->wqs)
157 return -ENOMEM;
158
159 for (i = 0; i < idxd->max_wqs; i++) {
160 wq = kzalloc_node(sizeof(*wq), GFP_KERNEL, dev_to_node(dev));
161 if (!wq) {
162 rc = -ENOMEM;
163 goto err;
164 }
165
166 wq->id = i;
167 wq->idxd = idxd;
168 device_initialize(&wq->conf_dev);
169 wq->conf_dev.parent = &idxd->conf_dev;
170 wq->conf_dev.bus = idxd_get_bus_type(idxd);
171 wq->conf_dev.type = &idxd_wq_device_type;
172 rc = dev_set_name(&wq->conf_dev, "wq%d.%d", idxd->id, wq->id);
173 if (rc < 0) {
174 put_device(&wq->conf_dev);
175 goto err;
176 }
177
178 mutex_init(&wq->wq_lock);
Dave Jiang04922b72021-04-15 16:37:57 -0700179 init_waitqueue_head(&wq->err_queue);
Dave Jiang7c5dd232021-04-15 16:37:39 -0700180 wq->max_xfer_bytes = idxd->max_xfer_bytes;
181 wq->max_batch_size = idxd->max_batch_size;
182 wq->wqcfg = kzalloc_node(idxd->wqcfg_size, GFP_KERNEL, dev_to_node(dev));
183 if (!wq->wqcfg) {
184 put_device(&wq->conf_dev);
185 rc = -ENOMEM;
186 goto err;
187 }
188 idxd->wqs[i] = wq;
189 }
190
191 return 0;
192
193 err:
194 while (--i >= 0)
195 put_device(&idxd->wqs[i]->conf_dev);
196 return rc;
197}
198
Dave Jiang75b91132021-04-15 16:37:44 -0700199static int idxd_setup_engines(struct idxd_device *idxd)
200{
201 struct idxd_engine *engine;
202 struct device *dev = &idxd->pdev->dev;
203 int i, rc;
204
205 idxd->engines = kcalloc_node(idxd->max_engines, sizeof(struct idxd_engine *),
206 GFP_KERNEL, dev_to_node(dev));
207 if (!idxd->engines)
208 return -ENOMEM;
209
210 for (i = 0; i < idxd->max_engines; i++) {
211 engine = kzalloc_node(sizeof(*engine), GFP_KERNEL, dev_to_node(dev));
212 if (!engine) {
213 rc = -ENOMEM;
214 goto err;
215 }
216
217 engine->id = i;
218 engine->idxd = idxd;
219 device_initialize(&engine->conf_dev);
220 engine->conf_dev.parent = &idxd->conf_dev;
221 engine->conf_dev.type = &idxd_engine_device_type;
222 rc = dev_set_name(&engine->conf_dev, "engine%d.%d", idxd->id, engine->id);
223 if (rc < 0) {
224 put_device(&engine->conf_dev);
225 goto err;
226 }
227
228 idxd->engines[i] = engine;
229 }
230
231 return 0;
232
233 err:
234 while (--i >= 0)
235 put_device(&idxd->engines[i]->conf_dev);
236 return rc;
237}
238
Dave Jiangdefe49f2021-04-15 16:37:51 -0700239static int idxd_setup_groups(struct idxd_device *idxd)
240{
241 struct device *dev = &idxd->pdev->dev;
242 struct idxd_group *group;
243 int i, rc;
244
245 idxd->groups = kcalloc_node(idxd->max_groups, sizeof(struct idxd_group *),
246 GFP_KERNEL, dev_to_node(dev));
247 if (!idxd->groups)
248 return -ENOMEM;
249
250 for (i = 0; i < idxd->max_groups; i++) {
251 group = kzalloc_node(sizeof(*group), GFP_KERNEL, dev_to_node(dev));
252 if (!group) {
253 rc = -ENOMEM;
254 goto err;
255 }
256
257 group->id = i;
258 group->idxd = idxd;
259 device_initialize(&group->conf_dev);
260 group->conf_dev.parent = &idxd->conf_dev;
261 group->conf_dev.bus = idxd_get_bus_type(idxd);
262 group->conf_dev.type = &idxd_group_device_type;
263 rc = dev_set_name(&group->conf_dev, "group%d.%d", idxd->id, group->id);
264 if (rc < 0) {
265 put_device(&group->conf_dev);
266 goto err;
267 }
268
269 idxd->groups[i] = group;
270 group->tc_a = -1;
271 group->tc_b = -1;
272 }
273
274 return 0;
275
276 err:
277 while (--i >= 0)
278 put_device(&idxd->groups[i]->conf_dev);
279 return rc;
280}
281
Dave Jiangbfe1d562020-01-21 16:43:59 -0700282static int idxd_setup_internals(struct idxd_device *idxd)
283{
284 struct device *dev = &idxd->pdev->dev;
Dave Jiangdefe49f2021-04-15 16:37:51 -0700285 int rc, i;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700286
Dave Jiang0d5c10b2020-06-26 11:11:18 -0700287 init_waitqueue_head(&idxd->cmd_waitq);
Dave Jiang7c5dd232021-04-15 16:37:39 -0700288
289 rc = idxd_setup_wqs(idxd);
290 if (rc < 0)
291 return rc;
292
Dave Jiang75b91132021-04-15 16:37:44 -0700293 rc = idxd_setup_engines(idxd);
294 if (rc < 0)
295 goto err_engine;
296
Dave Jiangdefe49f2021-04-15 16:37:51 -0700297 rc = idxd_setup_groups(idxd);
298 if (rc < 0)
299 goto err_group;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700300
Dave Jiang0d5c10b2020-06-26 11:11:18 -0700301 idxd->wq = create_workqueue(dev_name(dev));
Dave Jiang7c5dd232021-04-15 16:37:39 -0700302 if (!idxd->wq) {
303 rc = -ENOMEM;
Dave Jiangdefe49f2021-04-15 16:37:51 -0700304 goto err_wkq_create;
Dave Jiang7c5dd232021-04-15 16:37:39 -0700305 }
Dave Jiang0d5c10b2020-06-26 11:11:18 -0700306
Dave Jiangbfe1d562020-01-21 16:43:59 -0700307 return 0;
Dave Jiang7c5dd232021-04-15 16:37:39 -0700308
Dave Jiangdefe49f2021-04-15 16:37:51 -0700309 err_wkq_create:
310 for (i = 0; i < idxd->max_groups; i++)
311 put_device(&idxd->groups[i]->conf_dev);
312 err_group:
Dave Jiang75b91132021-04-15 16:37:44 -0700313 for (i = 0; i < idxd->max_engines; i++)
314 put_device(&idxd->engines[i]->conf_dev);
315 err_engine:
Dave Jiang7c5dd232021-04-15 16:37:39 -0700316 for (i = 0; i < idxd->max_wqs; i++)
317 put_device(&idxd->wqs[i]->conf_dev);
318 return rc;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700319}
320
321static void idxd_read_table_offsets(struct idxd_device *idxd)
322{
323 union offsets_reg offsets;
324 struct device *dev = &idxd->pdev->dev;
325
326 offsets.bits[0] = ioread64(idxd->reg_base + IDXD_TABLE_OFFSET);
Dave Jiang2f8417a2020-10-30 08:51:56 -0700327 offsets.bits[1] = ioread64(idxd->reg_base + IDXD_TABLE_OFFSET + sizeof(u64));
328 idxd->grpcfg_offset = offsets.grpcfg * IDXD_TABLE_MULT;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700329 dev_dbg(dev, "IDXD Group Config Offset: %#x\n", idxd->grpcfg_offset);
Dave Jiang2f8417a2020-10-30 08:51:56 -0700330 idxd->wqcfg_offset = offsets.wqcfg * IDXD_TABLE_MULT;
331 dev_dbg(dev, "IDXD Work Queue Config Offset: %#x\n", idxd->wqcfg_offset);
332 idxd->msix_perm_offset = offsets.msix_perm * IDXD_TABLE_MULT;
333 dev_dbg(dev, "IDXD MSIX Permission Offset: %#x\n", idxd->msix_perm_offset);
334 idxd->perfmon_offset = offsets.perfmon * IDXD_TABLE_MULT;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700335 dev_dbg(dev, "IDXD Perfmon Offset: %#x\n", idxd->perfmon_offset);
336}
337
338static void idxd_read_caps(struct idxd_device *idxd)
339{
340 struct device *dev = &idxd->pdev->dev;
341 int i;
342
343 /* reading generic capabilities */
344 idxd->hw.gen_cap.bits = ioread64(idxd->reg_base + IDXD_GENCAP_OFFSET);
345 dev_dbg(dev, "gen_cap: %#llx\n", idxd->hw.gen_cap.bits);
346 idxd->max_xfer_bytes = 1ULL << idxd->hw.gen_cap.max_xfer_shift;
347 dev_dbg(dev, "max xfer size: %llu bytes\n", idxd->max_xfer_bytes);
348 idxd->max_batch_size = 1U << idxd->hw.gen_cap.max_batch_shift;
349 dev_dbg(dev, "max batch size: %u\n", idxd->max_batch_size);
350 if (idxd->hw.gen_cap.config_en)
351 set_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags);
352
353 /* reading group capabilities */
354 idxd->hw.group_cap.bits =
355 ioread64(idxd->reg_base + IDXD_GRPCAP_OFFSET);
356 dev_dbg(dev, "group_cap: %#llx\n", idxd->hw.group_cap.bits);
357 idxd->max_groups = idxd->hw.group_cap.num_groups;
358 dev_dbg(dev, "max groups: %u\n", idxd->max_groups);
359 idxd->max_tokens = idxd->hw.group_cap.total_tokens;
360 dev_dbg(dev, "max tokens: %u\n", idxd->max_tokens);
Dave Jiangc52ca472020-01-21 16:44:05 -0700361 idxd->nr_tokens = idxd->max_tokens;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700362
363 /* read engine capabilities */
364 idxd->hw.engine_cap.bits =
365 ioread64(idxd->reg_base + IDXD_ENGCAP_OFFSET);
366 dev_dbg(dev, "engine_cap: %#llx\n", idxd->hw.engine_cap.bits);
367 idxd->max_engines = idxd->hw.engine_cap.num_engines;
368 dev_dbg(dev, "max engines: %u\n", idxd->max_engines);
369
370 /* read workqueue capabilities */
371 idxd->hw.wq_cap.bits = ioread64(idxd->reg_base + IDXD_WQCAP_OFFSET);
372 dev_dbg(dev, "wq_cap: %#llx\n", idxd->hw.wq_cap.bits);
373 idxd->max_wq_size = idxd->hw.wq_cap.total_wq_size;
374 dev_dbg(dev, "total workqueue size: %u\n", idxd->max_wq_size);
375 idxd->max_wqs = idxd->hw.wq_cap.num_wqs;
376 dev_dbg(dev, "max workqueues: %u\n", idxd->max_wqs);
Dave Jiangd98793b2020-10-27 14:34:09 -0700377 idxd->wqcfg_size = 1 << (idxd->hw.wq_cap.wqcfg_size + IDXD_WQCFG_MIN);
378 dev_dbg(dev, "wqcfg size: %u\n", idxd->wqcfg_size);
Dave Jiangbfe1d562020-01-21 16:43:59 -0700379
380 /* reading operation capabilities */
381 for (i = 0; i < 4; i++) {
382 idxd->hw.opcap.bits[i] = ioread64(idxd->reg_base +
383 IDXD_OPCAP_OFFSET + i * sizeof(u64));
384 dev_dbg(dev, "opcap[%d]: %#llx\n", i, idxd->hw.opcap.bits[i]);
385 }
386}
387
Dave Jiang47c16ac2021-04-15 16:37:33 -0700388static inline void idxd_set_type(struct idxd_device *idxd)
389{
390 struct pci_dev *pdev = idxd->pdev;
391
392 if (pdev->device == PCI_DEVICE_ID_INTEL_DSA_SPR0)
393 idxd->type = IDXD_TYPE_DSA;
394 else if (pdev->device == PCI_DEVICE_ID_INTEL_IAX_SPR0)
395 idxd->type = IDXD_TYPE_IAX;
396 else
397 idxd->type = IDXD_TYPE_UNKNOWN;
398}
399
Dave Jiang8e50d392020-10-27 10:34:35 -0700400static struct idxd_device *idxd_alloc(struct pci_dev *pdev)
Dave Jiangbfe1d562020-01-21 16:43:59 -0700401{
402 struct device *dev = &pdev->dev;
403 struct idxd_device *idxd;
Dave Jiang47c16ac2021-04-15 16:37:33 -0700404 int rc;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700405
Dave Jiang47c16ac2021-04-15 16:37:33 -0700406 idxd = kzalloc_node(sizeof(*idxd), GFP_KERNEL, dev_to_node(dev));
Dave Jiangbfe1d562020-01-21 16:43:59 -0700407 if (!idxd)
408 return NULL;
409
410 idxd->pdev = pdev;
Dave Jiang47c16ac2021-04-15 16:37:33 -0700411 idxd_set_type(idxd);
412 idxd->id = ida_alloc(idxd_ida(idxd), GFP_KERNEL);
413 if (idxd->id < 0)
414 return NULL;
415
416 device_initialize(&idxd->conf_dev);
417 idxd->conf_dev.parent = dev;
418 idxd->conf_dev.bus = idxd_get_bus_type(idxd);
419 idxd->conf_dev.type = idxd_get_device_type(idxd);
420 rc = dev_set_name(&idxd->conf_dev, "%s%d", idxd_get_dev_name(idxd), idxd->id);
421 if (rc < 0) {
422 put_device(&idxd->conf_dev);
423 return NULL;
424 }
425
Dave Jiangbfe1d562020-01-21 16:43:59 -0700426 spin_lock_init(&idxd->dev_lock);
427
428 return idxd;
429}
430
Dave Jiang8e50d392020-10-27 10:34:35 -0700431static int idxd_enable_system_pasid(struct idxd_device *idxd)
432{
433 int flags;
434 unsigned int pasid;
435 struct iommu_sva *sva;
436
437 flags = SVM_FLAG_SUPERVISOR_MODE;
438
439 sva = iommu_sva_bind_device(&idxd->pdev->dev, NULL, &flags);
440 if (IS_ERR(sva)) {
441 dev_warn(&idxd->pdev->dev,
442 "iommu sva bind failed: %ld\n", PTR_ERR(sva));
443 return PTR_ERR(sva);
444 }
445
446 pasid = iommu_sva_get_pasid(sva);
447 if (pasid == IOMMU_PASID_INVALID) {
448 iommu_sva_unbind_device(sva);
449 return -ENODEV;
450 }
451
452 idxd->sva = sva;
453 idxd->pasid = pasid;
454 dev_dbg(&idxd->pdev->dev, "system pasid: %u\n", pasid);
455 return 0;
456}
457
458static void idxd_disable_system_pasid(struct idxd_device *idxd)
459{
460
461 iommu_sva_unbind_device(idxd->sva);
462 idxd->sva = NULL;
463}
464
Dave Jiangbfe1d562020-01-21 16:43:59 -0700465static int idxd_probe(struct idxd_device *idxd)
466{
467 struct pci_dev *pdev = idxd->pdev;
468 struct device *dev = &pdev->dev;
469 int rc;
470
471 dev_dbg(dev, "%s entered and resetting device\n", __func__);
Dave Jiang89e3bec2021-02-01 08:26:14 -0700472 rc = idxd_device_init_reset(idxd);
473 if (rc < 0)
474 return rc;
475
Dave Jiangbfe1d562020-01-21 16:43:59 -0700476 dev_dbg(dev, "IDXD reset complete\n");
477
Dave Jiang03d939c2021-01-22 11:46:00 -0700478 if (IS_ENABLED(CONFIG_INTEL_IDXD_SVM) && sva) {
Dave Jiang8e50d392020-10-27 10:34:35 -0700479 rc = idxd_enable_system_pasid(idxd);
480 if (rc < 0)
481 dev_warn(dev, "Failed to enable PASID. No SVA support: %d\n", rc);
482 else
483 set_bit(IDXD_FLAG_PASID_ENABLED, &idxd->flags);
Dave Jiang03d939c2021-01-22 11:46:00 -0700484 } else if (!sva) {
485 dev_warn(dev, "User forced SVA off via module param.\n");
Dave Jiang8e50d392020-10-27 10:34:35 -0700486 }
487
Dave Jiangbfe1d562020-01-21 16:43:59 -0700488 idxd_read_caps(idxd);
489 idxd_read_table_offsets(idxd);
490
491 rc = idxd_setup_internals(idxd);
492 if (rc)
Dave Jiang7c5dd232021-04-15 16:37:39 -0700493 goto err;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700494
495 rc = idxd_setup_interrupts(idxd);
496 if (rc)
Dave Jiang7c5dd232021-04-15 16:37:39 -0700497 goto err;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700498
499 dev_dbg(dev, "IDXD interrupt setup complete.\n");
500
Dave Jiang42d279f2020-01-21 16:44:29 -0700501 idxd->major = idxd_cdev_get_major(idxd);
502
Dave Jiangbfe1d562020-01-21 16:43:59 -0700503 dev_dbg(dev, "IDXD device %d probed successfully\n", idxd->id);
504 return 0;
505
Dave Jiang7c5dd232021-04-15 16:37:39 -0700506 err:
Dave Jiang8e50d392020-10-27 10:34:35 -0700507 if (device_pasid_enabled(idxd))
508 idxd_disable_system_pasid(idxd);
Dave Jiangbfe1d562020-01-21 16:43:59 -0700509 return rc;
510}
511
Dave Jiangf25b46382020-11-17 13:39:14 -0700512static void idxd_type_init(struct idxd_device *idxd)
513{
514 if (idxd->type == IDXD_TYPE_DSA)
515 idxd->compl_size = sizeof(struct dsa_completion_record);
516 else if (idxd->type == IDXD_TYPE_IAX)
517 idxd->compl_size = sizeof(struct iax_completion_record);
518}
519
Dave Jiangbfe1d562020-01-21 16:43:59 -0700520static int idxd_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
521{
Dave Jiangbfe1d562020-01-21 16:43:59 -0700522 struct device *dev = &pdev->dev;
523 struct idxd_device *idxd;
524 int rc;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700525
Dave Jianga39c7cd2021-04-15 16:37:21 -0700526 rc = pci_enable_device(pdev);
Dave Jiangbfe1d562020-01-21 16:43:59 -0700527 if (rc)
528 return rc;
529
Dave Jiang8e50d392020-10-27 10:34:35 -0700530 dev_dbg(dev, "Alloc IDXD context\n");
531 idxd = idxd_alloc(pdev);
Dave Jianga39c7cd2021-04-15 16:37:21 -0700532 if (!idxd) {
533 rc = -ENOMEM;
534 goto err_idxd_alloc;
535 }
Dave Jiangbfe1d562020-01-21 16:43:59 -0700536
Dave Jiang8e50d392020-10-27 10:34:35 -0700537 dev_dbg(dev, "Mapping BARs\n");
Dave Jianga39c7cd2021-04-15 16:37:21 -0700538 idxd->reg_base = pci_iomap(pdev, IDXD_MMIO_BAR, 0);
539 if (!idxd->reg_base) {
540 rc = -ENOMEM;
541 goto err_iomap;
542 }
Dave Jiangbfe1d562020-01-21 16:43:59 -0700543
544 dev_dbg(dev, "Set DMA masks\n");
545 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
546 if (rc)
547 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
548 if (rc)
Dave Jianga39c7cd2021-04-15 16:37:21 -0700549 goto err;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700550
551 rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
552 if (rc)
553 rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
554 if (rc)
Dave Jianga39c7cd2021-04-15 16:37:21 -0700555 goto err;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700556
Dave Jiangbfe1d562020-01-21 16:43:59 -0700557
Dave Jiangf25b46382020-11-17 13:39:14 -0700558 idxd_type_init(idxd);
559
Dave Jiangbfe1d562020-01-21 16:43:59 -0700560 dev_dbg(dev, "Set PCI master\n");
561 pci_set_master(pdev);
562 pci_set_drvdata(pdev, idxd);
563
564 idxd->hw.version = ioread32(idxd->reg_base + IDXD_VER_OFFSET);
565 rc = idxd_probe(idxd);
566 if (rc) {
567 dev_err(dev, "Intel(R) IDXD DMA Engine init failed\n");
Dave Jianga39c7cd2021-04-15 16:37:21 -0700568 goto err;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700569 }
570
Dave Jiang47c16ac2021-04-15 16:37:33 -0700571 rc = idxd_register_devices(idxd);
Dave Jiangc52ca472020-01-21 16:44:05 -0700572 if (rc) {
573 dev_err(dev, "IDXD sysfs setup failed\n");
Dave Jianga39c7cd2021-04-15 16:37:21 -0700574 goto err;
Dave Jiangc52ca472020-01-21 16:44:05 -0700575 }
576
577 idxd->state = IDXD_DEV_CONF_READY;
578
Dave Jiangbfe1d562020-01-21 16:43:59 -0700579 dev_info(&pdev->dev, "Intel(R) Accelerator Device (v%x)\n",
580 idxd->hw.version);
581
582 return 0;
Dave Jianga39c7cd2021-04-15 16:37:21 -0700583
584 err:
585 pci_iounmap(pdev, idxd->reg_base);
586 err_iomap:
Dave Jiang47c16ac2021-04-15 16:37:33 -0700587 put_device(&idxd->conf_dev);
Dave Jianga39c7cd2021-04-15 16:37:21 -0700588 err_idxd_alloc:
589 pci_disable_device(pdev);
590 return rc;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700591}
592
Dave Jiang8f47d1a2020-01-21 16:44:23 -0700593static void idxd_flush_pending_llist(struct idxd_irq_entry *ie)
594{
595 struct idxd_desc *desc, *itr;
596 struct llist_node *head;
597
598 head = llist_del_all(&ie->pending_llist);
599 if (!head)
600 return;
601
602 llist_for_each_entry_safe(desc, itr, head, llnode) {
603 idxd_dma_complete_txd(desc, IDXD_COMPLETE_ABORT);
604 idxd_free_desc(desc->wq, desc);
605 }
606}
607
608static void idxd_flush_work_list(struct idxd_irq_entry *ie)
609{
610 struct idxd_desc *desc, *iter;
611
612 list_for_each_entry_safe(desc, iter, &ie->work_list, list) {
613 list_del(&desc->list);
614 idxd_dma_complete_txd(desc, IDXD_COMPLETE_ABORT);
615 idxd_free_desc(desc->wq, desc);
616 }
617}
618
Dave Jiangbfe1d562020-01-21 16:43:59 -0700619static void idxd_shutdown(struct pci_dev *pdev)
620{
621 struct idxd_device *idxd = pci_get_drvdata(pdev);
622 int rc, i;
623 struct idxd_irq_entry *irq_entry;
624 int msixcnt = pci_msix_vec_count(pdev);
Dave Jiangbfe1d562020-01-21 16:43:59 -0700625
Dave Jiangbfe1d562020-01-21 16:43:59 -0700626 rc = idxd_device_disable(idxd);
Dave Jiangbfe1d562020-01-21 16:43:59 -0700627 if (rc)
628 dev_err(&pdev->dev, "Disabling device failed\n");
629
630 dev_dbg(&pdev->dev, "%s called\n", __func__);
631 idxd_mask_msix_vectors(idxd);
632 idxd_mask_error_interrupts(idxd);
633
634 for (i = 0; i < msixcnt; i++) {
635 irq_entry = &idxd->irq_entries[i];
Dave Jiang5fc8e852021-04-15 16:37:15 -0700636 synchronize_irq(irq_entry->vector);
637 free_irq(irq_entry->vector, irq_entry);
Dave Jiangbfe1d562020-01-21 16:43:59 -0700638 if (i == 0)
639 continue;
Dave Jiang8f47d1a2020-01-21 16:44:23 -0700640 idxd_flush_pending_llist(irq_entry);
641 idxd_flush_work_list(irq_entry);
Dave Jiangbfe1d562020-01-21 16:43:59 -0700642 }
Dave Jiang0d5c10b2020-06-26 11:11:18 -0700643
Dave Jiang6df0e6c2021-04-12 09:23:27 -0700644 idxd_msix_perm_clear(idxd);
Dave Jiang5fc8e852021-04-15 16:37:15 -0700645 pci_free_irq_vectors(pdev);
Dave Jianga39c7cd2021-04-15 16:37:21 -0700646 pci_iounmap(pdev, idxd->reg_base);
647 pci_disable_device(pdev);
Dave Jiang0d5c10b2020-06-26 11:11:18 -0700648 destroy_workqueue(idxd->wq);
Dave Jiangbfe1d562020-01-21 16:43:59 -0700649}
650
651static void idxd_remove(struct pci_dev *pdev)
652{
653 struct idxd_device *idxd = pci_get_drvdata(pdev);
654
655 dev_dbg(&pdev->dev, "%s called\n", __func__);
656 idxd_shutdown(pdev);
Dave Jiang8e50d392020-10-27 10:34:35 -0700657 if (device_pasid_enabled(idxd))
658 idxd_disable_system_pasid(idxd);
Dave Jiang47c16ac2021-04-15 16:37:33 -0700659 idxd_unregister_devices(idxd);
Dave Jiangbfe1d562020-01-21 16:43:59 -0700660}
661
662static struct pci_driver idxd_pci_driver = {
663 .name = DRV_NAME,
664 .id_table = idxd_pci_tbl,
665 .probe = idxd_pci_probe,
666 .remove = idxd_remove,
667 .shutdown = idxd_shutdown,
668};
669
670static int __init idxd_init_module(void)
671{
672 int err, i;
673
674 /*
Dave Jiang8e50d392020-10-27 10:34:35 -0700675 * If the CPU does not support MOVDIR64B or ENQCMDS, there's no point in
Dave Jiangbfe1d562020-01-21 16:43:59 -0700676 * enumerating the device. We can not utilize it.
677 */
678 if (!boot_cpu_has(X86_FEATURE_MOVDIR64B)) {
679 pr_warn("idxd driver failed to load without MOVDIR64B.\n");
680 return -ENODEV;
681 }
682
Dave Jiang8e50d392020-10-27 10:34:35 -0700683 if (!boot_cpu_has(X86_FEATURE_ENQCMD))
684 pr_warn("Platform does not have ENQCMD(S) support.\n");
685 else
686 support_enqcmd = true;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700687
Dave Jiangbfe1d562020-01-21 16:43:59 -0700688 for (i = 0; i < IDXD_TYPE_MAX; i++)
Dave Jiangf7f77392021-04-15 16:37:27 -0700689 ida_init(&idxd_idas[i]);
Dave Jiangbfe1d562020-01-21 16:43:59 -0700690
Dave Jiangc52ca472020-01-21 16:44:05 -0700691 err = idxd_register_bus_type();
692 if (err < 0)
Dave Jiangbfe1d562020-01-21 16:43:59 -0700693 return err;
694
Dave Jiangc52ca472020-01-21 16:44:05 -0700695 err = idxd_register_driver();
696 if (err < 0)
697 goto err_idxd_driver_register;
698
Dave Jiang42d279f2020-01-21 16:44:29 -0700699 err = idxd_cdev_register();
700 if (err)
701 goto err_cdev_register;
702
Dave Jiangc52ca472020-01-21 16:44:05 -0700703 err = pci_register_driver(&idxd_pci_driver);
704 if (err)
705 goto err_pci_register;
706
Dave Jiangbfe1d562020-01-21 16:43:59 -0700707 return 0;
Dave Jiangc52ca472020-01-21 16:44:05 -0700708
709err_pci_register:
Dave Jiang42d279f2020-01-21 16:44:29 -0700710 idxd_cdev_remove();
711err_cdev_register:
Dave Jiangc52ca472020-01-21 16:44:05 -0700712 idxd_unregister_driver();
713err_idxd_driver_register:
714 idxd_unregister_bus_type();
715 return err;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700716}
717module_init(idxd_init_module);
718
719static void __exit idxd_exit_module(void)
720{
721 pci_unregister_driver(&idxd_pci_driver);
Dave Jiang42d279f2020-01-21 16:44:29 -0700722 idxd_cdev_remove();
Dave Jiangc52ca472020-01-21 16:44:05 -0700723 idxd_unregister_bus_type();
Dave Jiangbfe1d562020-01-21 16:43:59 -0700724}
725module_exit(idxd_exit_module);