blob: a07e6d8eec00fabd300ee9364452439ac608c050 [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;
Dave Jiang4b73e4e2021-04-15 16:38:03 -070036DEFINE_IDA(idxd_ida);
Dave Jiangbfe1d562020-01-21 16:43:59 -070037
Dave Jiang435b5122021-04-15 16:38:09 -070038static struct idxd_driver_data idxd_driver_data[] = {
39 [IDXD_TYPE_DSA] = {
40 .name_prefix = "dsa",
41 .type = IDXD_TYPE_DSA,
42 .compl_size = sizeof(struct dsa_completion_record),
43 .align = 32,
44 .dev_type = &dsa_device_type,
45 },
46 [IDXD_TYPE_IAX] = {
47 .name_prefix = "iax",
48 .type = IDXD_TYPE_IAX,
49 .compl_size = sizeof(struct iax_completion_record),
50 .align = 64,
51 .dev_type = &iax_device_type,
52 },
53};
54
Dave Jiangbfe1d562020-01-21 16:43:59 -070055static struct pci_device_id idxd_pci_tbl[] = {
56 /* DSA ver 1.0 platforms */
Dave Jiang435b5122021-04-15 16:38:09 -070057 { PCI_DEVICE_DATA(INTEL, DSA_SPR0, &idxd_driver_data[IDXD_TYPE_DSA]) },
Dave Jiangf25b46382020-11-17 13:39:14 -070058
59 /* IAX ver 1.0 platforms */
Dave Jiang435b5122021-04-15 16:38:09 -070060 { PCI_DEVICE_DATA(INTEL, IAX_SPR0, &idxd_driver_data[IDXD_TYPE_IAX]) },
Dave Jiangbfe1d562020-01-21 16:43:59 -070061 { 0, }
62};
63MODULE_DEVICE_TABLE(pci, idxd_pci_tbl);
64
Dave Jiangbfe1d562020-01-21 16:43:59 -070065static int idxd_setup_interrupts(struct idxd_device *idxd)
66{
67 struct pci_dev *pdev = idxd->pdev;
68 struct device *dev = &pdev->dev;
Dave Jiangbfe1d562020-01-21 16:43:59 -070069 struct idxd_irq_entry *irq_entry;
70 int i, msixcnt;
71 int rc = 0;
72
73 msixcnt = pci_msix_vec_count(pdev);
74 if (msixcnt < 0) {
75 dev_err(dev, "Not MSI-X interrupt capable.\n");
Dave Jiang5fc8e852021-04-15 16:37:15 -070076 return -ENOSPC;
Dave Jiangbfe1d562020-01-21 16:43:59 -070077 }
78
Dave Jiang5fc8e852021-04-15 16:37:15 -070079 rc = pci_alloc_irq_vectors(pdev, msixcnt, msixcnt, PCI_IRQ_MSIX);
80 if (rc != msixcnt) {
81 dev_err(dev, "Failed enabling %d MSIX entries: %d\n", msixcnt, rc);
82 return -ENOSPC;
Dave Jiangbfe1d562020-01-21 16:43:59 -070083 }
84 dev_dbg(dev, "Enabled %d msix vectors\n", msixcnt);
85
86 /*
87 * We implement 1 completion list per MSI-X entry except for
88 * entry 0, which is for errors and others.
89 */
Dave Jiang47c16ac2021-04-15 16:37:33 -070090 idxd->irq_entries = kcalloc_node(msixcnt, sizeof(struct idxd_irq_entry),
91 GFP_KERNEL, dev_to_node(dev));
Dave Jiangbfe1d562020-01-21 16:43:59 -070092 if (!idxd->irq_entries) {
93 rc = -ENOMEM;
Dave Jiang5fc8e852021-04-15 16:37:15 -070094 goto err_irq_entries;
Dave Jiangbfe1d562020-01-21 16:43:59 -070095 }
96
97 for (i = 0; i < msixcnt; i++) {
98 idxd->irq_entries[i].id = i;
99 idxd->irq_entries[i].idxd = idxd;
Dave Jiang5fc8e852021-04-15 16:37:15 -0700100 idxd->irq_entries[i].vector = pci_irq_vector(pdev, i);
Dave Jiange4f4d8c2020-10-27 10:34:40 -0700101 spin_lock_init(&idxd->irq_entries[i].list_lock);
Dave Jiangbfe1d562020-01-21 16:43:59 -0700102 }
103
Dave Jiangbfe1d562020-01-21 16:43:59 -0700104 irq_entry = &idxd->irq_entries[0];
Dave Jiang5fc8e852021-04-15 16:37:15 -0700105 rc = request_threaded_irq(irq_entry->vector, idxd_irq_handler, idxd_misc_thread,
106 0, "idxd-misc", irq_entry);
Dave Jiangbfe1d562020-01-21 16:43:59 -0700107 if (rc < 0) {
108 dev_err(dev, "Failed to allocate misc interrupt.\n");
Dave Jiang5fc8e852021-04-15 16:37:15 -0700109 goto err_misc_irq;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700110 }
111
Dave Jiang5fc8e852021-04-15 16:37:15 -0700112 dev_dbg(dev, "Allocated idxd-misc handler on msix vector %d\n", irq_entry->vector);
Dave Jiangbfe1d562020-01-21 16:43:59 -0700113
114 /* first MSI-X entry is not for wq interrupts */
115 idxd->num_wq_irqs = msixcnt - 1;
116
117 for (i = 1; i < msixcnt; i++) {
Dave Jiangbfe1d562020-01-21 16:43:59 -0700118 irq_entry = &idxd->irq_entries[i];
119
120 init_llist_head(&idxd->irq_entries[i].pending_llist);
121 INIT_LIST_HEAD(&idxd->irq_entries[i].work_list);
Dave Jiang5fc8e852021-04-15 16:37:15 -0700122 rc = request_threaded_irq(irq_entry->vector, idxd_irq_handler,
123 idxd_wq_thread, 0, "idxd-portal", irq_entry);
Dave Jiangbfe1d562020-01-21 16:43:59 -0700124 if (rc < 0) {
Dave Jiang5fc8e852021-04-15 16:37:15 -0700125 dev_err(dev, "Failed to allocate irq %d.\n", irq_entry->vector);
126 goto err_wq_irqs;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700127 }
Dave Jiang5fc8e852021-04-15 16:37:15 -0700128 dev_dbg(dev, "Allocated idxd-msix %d for vector %d\n", i, irq_entry->vector);
Dave Jiangbfe1d562020-01-21 16:43:59 -0700129 }
130
131 idxd_unmask_error_interrupts(idxd);
Dave Jiang6df0e6c2021-04-12 09:23:27 -0700132 idxd_msix_perm_setup(idxd);
Dave Jiangbfe1d562020-01-21 16:43:59 -0700133 return 0;
134
Dave Jiang5fc8e852021-04-15 16:37:15 -0700135 err_wq_irqs:
136 while (--i >= 0) {
137 irq_entry = &idxd->irq_entries[i];
138 free_irq(irq_entry->vector, irq_entry);
139 }
140 err_misc_irq:
Dave Jiangbfe1d562020-01-21 16:43:59 -0700141 /* Disable error interrupt generation */
142 idxd_mask_error_interrupts(idxd);
Dave Jiang5fc8e852021-04-15 16:37:15 -0700143 err_irq_entries:
144 pci_free_irq_vectors(pdev);
Dave Jiangbfe1d562020-01-21 16:43:59 -0700145 dev_err(dev, "No usable interrupts\n");
146 return rc;
147}
148
Dave Jiang7c5dd232021-04-15 16:37:39 -0700149static int idxd_setup_wqs(struct idxd_device *idxd)
150{
151 struct device *dev = &idxd->pdev->dev;
152 struct idxd_wq *wq;
153 int i, rc;
154
155 idxd->wqs = kcalloc_node(idxd->max_wqs, sizeof(struct idxd_wq *),
156 GFP_KERNEL, dev_to_node(dev));
157 if (!idxd->wqs)
158 return -ENOMEM;
159
160 for (i = 0; i < idxd->max_wqs; i++) {
161 wq = kzalloc_node(sizeof(*wq), GFP_KERNEL, dev_to_node(dev));
162 if (!wq) {
163 rc = -ENOMEM;
164 goto err;
165 }
166
167 wq->id = i;
168 wq->idxd = idxd;
169 device_initialize(&wq->conf_dev);
170 wq->conf_dev.parent = &idxd->conf_dev;
Dave Jiang4b73e4e2021-04-15 16:38:03 -0700171 wq->conf_dev.bus = &dsa_bus_type;
Dave Jiang7c5dd232021-04-15 16:37:39 -0700172 wq->conf_dev.type = &idxd_wq_device_type;
173 rc = dev_set_name(&wq->conf_dev, "wq%d.%d", idxd->id, wq->id);
174 if (rc < 0) {
175 put_device(&wq->conf_dev);
176 goto err;
177 }
178
179 mutex_init(&wq->wq_lock);
Dave Jiang04922b72021-04-15 16:37:57 -0700180 init_waitqueue_head(&wq->err_queue);
Dave Jiang93a40a62021-04-20 11:46:22 -0700181 init_completion(&wq->wq_dead);
Dave Jiang7c5dd232021-04-15 16:37:39 -0700182 wq->max_xfer_bytes = idxd->max_xfer_bytes;
183 wq->max_batch_size = idxd->max_batch_size;
184 wq->wqcfg = kzalloc_node(idxd->wqcfg_size, GFP_KERNEL, dev_to_node(dev));
185 if (!wq->wqcfg) {
186 put_device(&wq->conf_dev);
187 rc = -ENOMEM;
188 goto err;
189 }
190 idxd->wqs[i] = wq;
191 }
192
193 return 0;
194
195 err:
196 while (--i >= 0)
197 put_device(&idxd->wqs[i]->conf_dev);
198 return rc;
199}
200
Dave Jiang75b91132021-04-15 16:37:44 -0700201static int idxd_setup_engines(struct idxd_device *idxd)
202{
203 struct idxd_engine *engine;
204 struct device *dev = &idxd->pdev->dev;
205 int i, rc;
206
207 idxd->engines = kcalloc_node(idxd->max_engines, sizeof(struct idxd_engine *),
208 GFP_KERNEL, dev_to_node(dev));
209 if (!idxd->engines)
210 return -ENOMEM;
211
212 for (i = 0; i < idxd->max_engines; i++) {
213 engine = kzalloc_node(sizeof(*engine), GFP_KERNEL, dev_to_node(dev));
214 if (!engine) {
215 rc = -ENOMEM;
216 goto err;
217 }
218
219 engine->id = i;
220 engine->idxd = idxd;
221 device_initialize(&engine->conf_dev);
222 engine->conf_dev.parent = &idxd->conf_dev;
223 engine->conf_dev.type = &idxd_engine_device_type;
224 rc = dev_set_name(&engine->conf_dev, "engine%d.%d", idxd->id, engine->id);
225 if (rc < 0) {
226 put_device(&engine->conf_dev);
227 goto err;
228 }
229
230 idxd->engines[i] = engine;
231 }
232
233 return 0;
234
235 err:
236 while (--i >= 0)
237 put_device(&idxd->engines[i]->conf_dev);
238 return rc;
239}
240
Dave Jiangdefe49f2021-04-15 16:37:51 -0700241static int idxd_setup_groups(struct idxd_device *idxd)
242{
243 struct device *dev = &idxd->pdev->dev;
244 struct idxd_group *group;
245 int i, rc;
246
247 idxd->groups = kcalloc_node(idxd->max_groups, sizeof(struct idxd_group *),
248 GFP_KERNEL, dev_to_node(dev));
249 if (!idxd->groups)
250 return -ENOMEM;
251
252 for (i = 0; i < idxd->max_groups; i++) {
253 group = kzalloc_node(sizeof(*group), GFP_KERNEL, dev_to_node(dev));
254 if (!group) {
255 rc = -ENOMEM;
256 goto err;
257 }
258
259 group->id = i;
260 group->idxd = idxd;
261 device_initialize(&group->conf_dev);
262 group->conf_dev.parent = &idxd->conf_dev;
Dave Jiang4b73e4e2021-04-15 16:38:03 -0700263 group->conf_dev.bus = &dsa_bus_type;
Dave Jiangdefe49f2021-04-15 16:37:51 -0700264 group->conf_dev.type = &idxd_group_device_type;
265 rc = dev_set_name(&group->conf_dev, "group%d.%d", idxd->id, group->id);
266 if (rc < 0) {
267 put_device(&group->conf_dev);
268 goto err;
269 }
270
271 idxd->groups[i] = group;
272 group->tc_a = -1;
273 group->tc_b = -1;
274 }
275
276 return 0;
277
278 err:
279 while (--i >= 0)
280 put_device(&idxd->groups[i]->conf_dev);
281 return rc;
282}
283
Dave Jiangbfe1d562020-01-21 16:43:59 -0700284static int idxd_setup_internals(struct idxd_device *idxd)
285{
286 struct device *dev = &idxd->pdev->dev;
Dave Jiangdefe49f2021-04-15 16:37:51 -0700287 int rc, i;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700288
Dave Jiang0d5c10b2020-06-26 11:11:18 -0700289 init_waitqueue_head(&idxd->cmd_waitq);
Dave Jiang7c5dd232021-04-15 16:37:39 -0700290
291 rc = idxd_setup_wqs(idxd);
292 if (rc < 0)
293 return rc;
294
Dave Jiang75b91132021-04-15 16:37:44 -0700295 rc = idxd_setup_engines(idxd);
296 if (rc < 0)
297 goto err_engine;
298
Dave Jiangdefe49f2021-04-15 16:37:51 -0700299 rc = idxd_setup_groups(idxd);
300 if (rc < 0)
301 goto err_group;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700302
Dave Jiang0d5c10b2020-06-26 11:11:18 -0700303 idxd->wq = create_workqueue(dev_name(dev));
Dave Jiang7c5dd232021-04-15 16:37:39 -0700304 if (!idxd->wq) {
305 rc = -ENOMEM;
Dave Jiangdefe49f2021-04-15 16:37:51 -0700306 goto err_wkq_create;
Dave Jiang7c5dd232021-04-15 16:37:39 -0700307 }
Dave Jiang0d5c10b2020-06-26 11:11:18 -0700308
Dave Jiangbfe1d562020-01-21 16:43:59 -0700309 return 0;
Dave Jiang7c5dd232021-04-15 16:37:39 -0700310
Dave Jiangdefe49f2021-04-15 16:37:51 -0700311 err_wkq_create:
312 for (i = 0; i < idxd->max_groups; i++)
313 put_device(&idxd->groups[i]->conf_dev);
314 err_group:
Dave Jiang75b91132021-04-15 16:37:44 -0700315 for (i = 0; i < idxd->max_engines; i++)
316 put_device(&idxd->engines[i]->conf_dev);
317 err_engine:
Dave Jiang7c5dd232021-04-15 16:37:39 -0700318 for (i = 0; i < idxd->max_wqs; i++)
319 put_device(&idxd->wqs[i]->conf_dev);
320 return rc;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700321}
322
323static void idxd_read_table_offsets(struct idxd_device *idxd)
324{
325 union offsets_reg offsets;
326 struct device *dev = &idxd->pdev->dev;
327
328 offsets.bits[0] = ioread64(idxd->reg_base + IDXD_TABLE_OFFSET);
Dave Jiang2f8417a2020-10-30 08:51:56 -0700329 offsets.bits[1] = ioread64(idxd->reg_base + IDXD_TABLE_OFFSET + sizeof(u64));
330 idxd->grpcfg_offset = offsets.grpcfg * IDXD_TABLE_MULT;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700331 dev_dbg(dev, "IDXD Group Config Offset: %#x\n", idxd->grpcfg_offset);
Dave Jiang2f8417a2020-10-30 08:51:56 -0700332 idxd->wqcfg_offset = offsets.wqcfg * IDXD_TABLE_MULT;
333 dev_dbg(dev, "IDXD Work Queue Config Offset: %#x\n", idxd->wqcfg_offset);
334 idxd->msix_perm_offset = offsets.msix_perm * IDXD_TABLE_MULT;
335 dev_dbg(dev, "IDXD MSIX Permission Offset: %#x\n", idxd->msix_perm_offset);
336 idxd->perfmon_offset = offsets.perfmon * IDXD_TABLE_MULT;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700337 dev_dbg(dev, "IDXD Perfmon Offset: %#x\n", idxd->perfmon_offset);
338}
339
340static void idxd_read_caps(struct idxd_device *idxd)
341{
342 struct device *dev = &idxd->pdev->dev;
343 int i;
344
345 /* reading generic capabilities */
346 idxd->hw.gen_cap.bits = ioread64(idxd->reg_base + IDXD_GENCAP_OFFSET);
347 dev_dbg(dev, "gen_cap: %#llx\n", idxd->hw.gen_cap.bits);
348 idxd->max_xfer_bytes = 1ULL << idxd->hw.gen_cap.max_xfer_shift;
349 dev_dbg(dev, "max xfer size: %llu bytes\n", idxd->max_xfer_bytes);
350 idxd->max_batch_size = 1U << idxd->hw.gen_cap.max_batch_shift;
351 dev_dbg(dev, "max batch size: %u\n", idxd->max_batch_size);
352 if (idxd->hw.gen_cap.config_en)
353 set_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags);
354
355 /* reading group capabilities */
356 idxd->hw.group_cap.bits =
357 ioread64(idxd->reg_base + IDXD_GRPCAP_OFFSET);
358 dev_dbg(dev, "group_cap: %#llx\n", idxd->hw.group_cap.bits);
359 idxd->max_groups = idxd->hw.group_cap.num_groups;
360 dev_dbg(dev, "max groups: %u\n", idxd->max_groups);
361 idxd->max_tokens = idxd->hw.group_cap.total_tokens;
362 dev_dbg(dev, "max tokens: %u\n", idxd->max_tokens);
Dave Jiangc52ca472020-01-21 16:44:05 -0700363 idxd->nr_tokens = idxd->max_tokens;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700364
365 /* read engine capabilities */
366 idxd->hw.engine_cap.bits =
367 ioread64(idxd->reg_base + IDXD_ENGCAP_OFFSET);
368 dev_dbg(dev, "engine_cap: %#llx\n", idxd->hw.engine_cap.bits);
369 idxd->max_engines = idxd->hw.engine_cap.num_engines;
370 dev_dbg(dev, "max engines: %u\n", idxd->max_engines);
371
372 /* read workqueue capabilities */
373 idxd->hw.wq_cap.bits = ioread64(idxd->reg_base + IDXD_WQCAP_OFFSET);
374 dev_dbg(dev, "wq_cap: %#llx\n", idxd->hw.wq_cap.bits);
375 idxd->max_wq_size = idxd->hw.wq_cap.total_wq_size;
376 dev_dbg(dev, "total workqueue size: %u\n", idxd->max_wq_size);
377 idxd->max_wqs = idxd->hw.wq_cap.num_wqs;
378 dev_dbg(dev, "max workqueues: %u\n", idxd->max_wqs);
Dave Jiangd98793b2020-10-27 14:34:09 -0700379 idxd->wqcfg_size = 1 << (idxd->hw.wq_cap.wqcfg_size + IDXD_WQCFG_MIN);
380 dev_dbg(dev, "wqcfg size: %u\n", idxd->wqcfg_size);
Dave Jiangbfe1d562020-01-21 16:43:59 -0700381
382 /* reading operation capabilities */
383 for (i = 0; i < 4; i++) {
384 idxd->hw.opcap.bits[i] = ioread64(idxd->reg_base +
385 IDXD_OPCAP_OFFSET + i * sizeof(u64));
386 dev_dbg(dev, "opcap[%d]: %#llx\n", i, idxd->hw.opcap.bits[i]);
387 }
388}
389
Dave Jiang435b5122021-04-15 16:38:09 -0700390static struct idxd_device *idxd_alloc(struct pci_dev *pdev, struct idxd_driver_data *data)
Dave Jiangbfe1d562020-01-21 16:43:59 -0700391{
392 struct device *dev = &pdev->dev;
393 struct idxd_device *idxd;
Dave Jiang47c16ac2021-04-15 16:37:33 -0700394 int rc;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700395
Dave Jiang47c16ac2021-04-15 16:37:33 -0700396 idxd = kzalloc_node(sizeof(*idxd), GFP_KERNEL, dev_to_node(dev));
Dave Jiangbfe1d562020-01-21 16:43:59 -0700397 if (!idxd)
398 return NULL;
399
400 idxd->pdev = pdev;
Dave Jiang435b5122021-04-15 16:38:09 -0700401 idxd->data = data;
Dave Jiang4b73e4e2021-04-15 16:38:03 -0700402 idxd->id = ida_alloc(&idxd_ida, GFP_KERNEL);
Dave Jiang47c16ac2021-04-15 16:37:33 -0700403 if (idxd->id < 0)
404 return NULL;
405
406 device_initialize(&idxd->conf_dev);
407 idxd->conf_dev.parent = dev;
Dave Jiang4b73e4e2021-04-15 16:38:03 -0700408 idxd->conf_dev.bus = &dsa_bus_type;
Dave Jiang435b5122021-04-15 16:38:09 -0700409 idxd->conf_dev.type = idxd->data->dev_type;
410 rc = dev_set_name(&idxd->conf_dev, "%s%d", idxd->data->name_prefix, idxd->id);
Dave Jiang47c16ac2021-04-15 16:37:33 -0700411 if (rc < 0) {
412 put_device(&idxd->conf_dev);
413 return NULL;
414 }
415
Dave Jiangbfe1d562020-01-21 16:43:59 -0700416 spin_lock_init(&idxd->dev_lock);
417
418 return idxd;
419}
420
Dave Jiang8e50d392020-10-27 10:34:35 -0700421static int idxd_enable_system_pasid(struct idxd_device *idxd)
422{
423 int flags;
424 unsigned int pasid;
425 struct iommu_sva *sva;
426
427 flags = SVM_FLAG_SUPERVISOR_MODE;
428
429 sva = iommu_sva_bind_device(&idxd->pdev->dev, NULL, &flags);
430 if (IS_ERR(sva)) {
431 dev_warn(&idxd->pdev->dev,
432 "iommu sva bind failed: %ld\n", PTR_ERR(sva));
433 return PTR_ERR(sva);
434 }
435
436 pasid = iommu_sva_get_pasid(sva);
437 if (pasid == IOMMU_PASID_INVALID) {
438 iommu_sva_unbind_device(sva);
439 return -ENODEV;
440 }
441
442 idxd->sva = sva;
443 idxd->pasid = pasid;
444 dev_dbg(&idxd->pdev->dev, "system pasid: %u\n", pasid);
445 return 0;
446}
447
448static void idxd_disable_system_pasid(struct idxd_device *idxd)
449{
450
451 iommu_sva_unbind_device(idxd->sva);
452 idxd->sva = NULL;
453}
454
Dave Jiangbfe1d562020-01-21 16:43:59 -0700455static int idxd_probe(struct idxd_device *idxd)
456{
457 struct pci_dev *pdev = idxd->pdev;
458 struct device *dev = &pdev->dev;
459 int rc;
460
461 dev_dbg(dev, "%s entered and resetting device\n", __func__);
Dave Jiang89e3bec2021-02-01 08:26:14 -0700462 rc = idxd_device_init_reset(idxd);
463 if (rc < 0)
464 return rc;
465
Dave Jiangbfe1d562020-01-21 16:43:59 -0700466 dev_dbg(dev, "IDXD reset complete\n");
467
Dave Jiang03d939c2021-01-22 11:46:00 -0700468 if (IS_ENABLED(CONFIG_INTEL_IDXD_SVM) && sva) {
Dave Jiang8e50d392020-10-27 10:34:35 -0700469 rc = idxd_enable_system_pasid(idxd);
470 if (rc < 0)
471 dev_warn(dev, "Failed to enable PASID. No SVA support: %d\n", rc);
472 else
473 set_bit(IDXD_FLAG_PASID_ENABLED, &idxd->flags);
Dave Jiang03d939c2021-01-22 11:46:00 -0700474 } else if (!sva) {
475 dev_warn(dev, "User forced SVA off via module param.\n");
Dave Jiang8e50d392020-10-27 10:34:35 -0700476 }
477
Dave Jiangbfe1d562020-01-21 16:43:59 -0700478 idxd_read_caps(idxd);
479 idxd_read_table_offsets(idxd);
480
481 rc = idxd_setup_internals(idxd);
482 if (rc)
Dave Jiang7c5dd232021-04-15 16:37:39 -0700483 goto err;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700484
Dave Jiang8c66bbdc2021-04-20 11:46:28 -0700485 /* If the configs are readonly, then load them from device */
486 if (!test_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags)) {
487 dev_dbg(dev, "Loading RO device config\n");
488 rc = idxd_device_load_config(idxd);
489 if (rc < 0)
490 goto err;
491 }
492
Dave Jiangbfe1d562020-01-21 16:43:59 -0700493 rc = idxd_setup_interrupts(idxd);
494 if (rc)
Dave Jiang7c5dd232021-04-15 16:37:39 -0700495 goto err;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700496
497 dev_dbg(dev, "IDXD interrupt setup complete.\n");
498
Dave Jiang42d279f2020-01-21 16:44:29 -0700499 idxd->major = idxd_cdev_get_major(idxd);
500
Dave Jiangbfe1d562020-01-21 16:43:59 -0700501 dev_dbg(dev, "IDXD device %d probed successfully\n", idxd->id);
502 return 0;
503
Dave Jiang7c5dd232021-04-15 16:37:39 -0700504 err:
Dave Jiang8e50d392020-10-27 10:34:35 -0700505 if (device_pasid_enabled(idxd))
506 idxd_disable_system_pasid(idxd);
Dave Jiangbfe1d562020-01-21 16:43:59 -0700507 return rc;
508}
509
510static int idxd_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
511{
Dave Jiangbfe1d562020-01-21 16:43:59 -0700512 struct device *dev = &pdev->dev;
513 struct idxd_device *idxd;
Dave Jiang435b5122021-04-15 16:38:09 -0700514 struct idxd_driver_data *data = (struct idxd_driver_data *)id->driver_data;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700515 int rc;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700516
Dave Jianga39c7cd2021-04-15 16:37:21 -0700517 rc = pci_enable_device(pdev);
Dave Jiangbfe1d562020-01-21 16:43:59 -0700518 if (rc)
519 return rc;
520
Dave Jiang8e50d392020-10-27 10:34:35 -0700521 dev_dbg(dev, "Alloc IDXD context\n");
Dave Jiang435b5122021-04-15 16:38:09 -0700522 idxd = idxd_alloc(pdev, data);
Dave Jianga39c7cd2021-04-15 16:37:21 -0700523 if (!idxd) {
524 rc = -ENOMEM;
525 goto err_idxd_alloc;
526 }
Dave Jiangbfe1d562020-01-21 16:43:59 -0700527
Dave Jiang8e50d392020-10-27 10:34:35 -0700528 dev_dbg(dev, "Mapping BARs\n");
Dave Jianga39c7cd2021-04-15 16:37:21 -0700529 idxd->reg_base = pci_iomap(pdev, IDXD_MMIO_BAR, 0);
530 if (!idxd->reg_base) {
531 rc = -ENOMEM;
532 goto err_iomap;
533 }
Dave Jiangbfe1d562020-01-21 16:43:59 -0700534
535 dev_dbg(dev, "Set DMA masks\n");
536 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
537 if (rc)
538 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
539 if (rc)
Dave Jianga39c7cd2021-04-15 16:37:21 -0700540 goto err;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700541
542 rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
543 if (rc)
544 rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
545 if (rc)
Dave Jianga39c7cd2021-04-15 16:37:21 -0700546 goto err;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700547
Dave Jiangbfe1d562020-01-21 16:43:59 -0700548 dev_dbg(dev, "Set PCI master\n");
549 pci_set_master(pdev);
550 pci_set_drvdata(pdev, idxd);
551
552 idxd->hw.version = ioread32(idxd->reg_base + IDXD_VER_OFFSET);
553 rc = idxd_probe(idxd);
554 if (rc) {
555 dev_err(dev, "Intel(R) IDXD DMA Engine init failed\n");
Dave Jianga39c7cd2021-04-15 16:37:21 -0700556 goto err;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700557 }
558
Dave Jiang47c16ac2021-04-15 16:37:33 -0700559 rc = idxd_register_devices(idxd);
Dave Jiangc52ca472020-01-21 16:44:05 -0700560 if (rc) {
561 dev_err(dev, "IDXD sysfs setup failed\n");
Dave Jianga39c7cd2021-04-15 16:37:21 -0700562 goto err;
Dave Jiangc52ca472020-01-21 16:44:05 -0700563 }
564
565 idxd->state = IDXD_DEV_CONF_READY;
566
Dave Jiangbfe1d562020-01-21 16:43:59 -0700567 dev_info(&pdev->dev, "Intel(R) Accelerator Device (v%x)\n",
568 idxd->hw.version);
569
570 return 0;
Dave Jianga39c7cd2021-04-15 16:37:21 -0700571
572 err:
573 pci_iounmap(pdev, idxd->reg_base);
574 err_iomap:
Dave Jiang47c16ac2021-04-15 16:37:33 -0700575 put_device(&idxd->conf_dev);
Dave Jianga39c7cd2021-04-15 16:37:21 -0700576 err_idxd_alloc:
577 pci_disable_device(pdev);
578 return rc;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700579}
580
Dave Jiang8f47d1a2020-01-21 16:44:23 -0700581static void idxd_flush_pending_llist(struct idxd_irq_entry *ie)
582{
583 struct idxd_desc *desc, *itr;
584 struct llist_node *head;
585
586 head = llist_del_all(&ie->pending_llist);
587 if (!head)
588 return;
589
590 llist_for_each_entry_safe(desc, itr, head, llnode) {
591 idxd_dma_complete_txd(desc, IDXD_COMPLETE_ABORT);
592 idxd_free_desc(desc->wq, desc);
593 }
594}
595
596static void idxd_flush_work_list(struct idxd_irq_entry *ie)
597{
598 struct idxd_desc *desc, *iter;
599
600 list_for_each_entry_safe(desc, iter, &ie->work_list, list) {
601 list_del(&desc->list);
602 idxd_dma_complete_txd(desc, IDXD_COMPLETE_ABORT);
603 idxd_free_desc(desc->wq, desc);
604 }
605}
606
Dave Jiangbfe1d562020-01-21 16:43:59 -0700607static void idxd_shutdown(struct pci_dev *pdev)
608{
609 struct idxd_device *idxd = pci_get_drvdata(pdev);
610 int rc, i;
611 struct idxd_irq_entry *irq_entry;
612 int msixcnt = pci_msix_vec_count(pdev);
Dave Jiangbfe1d562020-01-21 16:43:59 -0700613
Dave Jiangbfe1d562020-01-21 16:43:59 -0700614 rc = idxd_device_disable(idxd);
Dave Jiangbfe1d562020-01-21 16:43:59 -0700615 if (rc)
616 dev_err(&pdev->dev, "Disabling device failed\n");
617
618 dev_dbg(&pdev->dev, "%s called\n", __func__);
619 idxd_mask_msix_vectors(idxd);
620 idxd_mask_error_interrupts(idxd);
621
622 for (i = 0; i < msixcnt; i++) {
623 irq_entry = &idxd->irq_entries[i];
Dave Jiang5fc8e852021-04-15 16:37:15 -0700624 synchronize_irq(irq_entry->vector);
625 free_irq(irq_entry->vector, irq_entry);
Dave Jiangbfe1d562020-01-21 16:43:59 -0700626 if (i == 0)
627 continue;
Dave Jiang8f47d1a2020-01-21 16:44:23 -0700628 idxd_flush_pending_llist(irq_entry);
629 idxd_flush_work_list(irq_entry);
Dave Jiangbfe1d562020-01-21 16:43:59 -0700630 }
Dave Jiang0d5c10b2020-06-26 11:11:18 -0700631
Dave Jiang6df0e6c2021-04-12 09:23:27 -0700632 idxd_msix_perm_clear(idxd);
Dave Jiang5fc8e852021-04-15 16:37:15 -0700633 pci_free_irq_vectors(pdev);
Dave Jianga39c7cd2021-04-15 16:37:21 -0700634 pci_iounmap(pdev, idxd->reg_base);
635 pci_disable_device(pdev);
Dave Jiang0d5c10b2020-06-26 11:11:18 -0700636 destroy_workqueue(idxd->wq);
Dave Jiangbfe1d562020-01-21 16:43:59 -0700637}
638
639static void idxd_remove(struct pci_dev *pdev)
640{
641 struct idxd_device *idxd = pci_get_drvdata(pdev);
642
643 dev_dbg(&pdev->dev, "%s called\n", __func__);
644 idxd_shutdown(pdev);
Dave Jiang8e50d392020-10-27 10:34:35 -0700645 if (device_pasid_enabled(idxd))
646 idxd_disable_system_pasid(idxd);
Dave Jiang47c16ac2021-04-15 16:37:33 -0700647 idxd_unregister_devices(idxd);
Dave Jiangbfe1d562020-01-21 16:43:59 -0700648}
649
650static struct pci_driver idxd_pci_driver = {
651 .name = DRV_NAME,
652 .id_table = idxd_pci_tbl,
653 .probe = idxd_pci_probe,
654 .remove = idxd_remove,
655 .shutdown = idxd_shutdown,
656};
657
658static int __init idxd_init_module(void)
659{
Dave Jiang4b73e4e2021-04-15 16:38:03 -0700660 int err;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700661
662 /*
Dave Jiang8e50d392020-10-27 10:34:35 -0700663 * If the CPU does not support MOVDIR64B or ENQCMDS, there's no point in
Dave Jiangbfe1d562020-01-21 16:43:59 -0700664 * enumerating the device. We can not utilize it.
665 */
666 if (!boot_cpu_has(X86_FEATURE_MOVDIR64B)) {
667 pr_warn("idxd driver failed to load without MOVDIR64B.\n");
668 return -ENODEV;
669 }
670
Dave Jiang8e50d392020-10-27 10:34:35 -0700671 if (!boot_cpu_has(X86_FEATURE_ENQCMD))
672 pr_warn("Platform does not have ENQCMD(S) support.\n");
673 else
674 support_enqcmd = true;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700675
Dave Jiangc52ca472020-01-21 16:44:05 -0700676 err = idxd_register_bus_type();
677 if (err < 0)
Dave Jiangbfe1d562020-01-21 16:43:59 -0700678 return err;
679
Dave Jiangc52ca472020-01-21 16:44:05 -0700680 err = idxd_register_driver();
681 if (err < 0)
682 goto err_idxd_driver_register;
683
Dave Jiang42d279f2020-01-21 16:44:29 -0700684 err = idxd_cdev_register();
685 if (err)
686 goto err_cdev_register;
687
Dave Jiangc52ca472020-01-21 16:44:05 -0700688 err = pci_register_driver(&idxd_pci_driver);
689 if (err)
690 goto err_pci_register;
691
Dave Jiangbfe1d562020-01-21 16:43:59 -0700692 return 0;
Dave Jiangc52ca472020-01-21 16:44:05 -0700693
694err_pci_register:
Dave Jiang42d279f2020-01-21 16:44:29 -0700695 idxd_cdev_remove();
696err_cdev_register:
Dave Jiangc52ca472020-01-21 16:44:05 -0700697 idxd_unregister_driver();
698err_idxd_driver_register:
699 idxd_unregister_bus_type();
700 return err;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700701}
702module_init(idxd_init_module);
703
704static void __exit idxd_exit_module(void)
705{
706 pci_unregister_driver(&idxd_pci_driver);
Dave Jiang42d279f2020-01-21 16:44:29 -0700707 idxd_cdev_remove();
Dave Jiangc52ca472020-01-21 16:44:05 -0700708 idxd_unregister_bus_type();
Dave Jiangbfe1d562020-01-21 16:43:59 -0700709}
710module_exit(idxd_exit_module);