blob: 2a926bef87f2ac8b19a246244a478987e3750bc9 [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"
Tom Zanussi0bde4442021-04-24 10:04:16 -050024#include "perfmon.h"
Dave Jiangbfe1d562020-01-21 16:43:59 -070025
26MODULE_VERSION(IDXD_DRIVER_VERSION);
27MODULE_LICENSE("GPL v2");
28MODULE_AUTHOR("Intel Corporation");
29
Dave Jiang03d939c2021-01-22 11:46:00 -070030static bool sva = true;
31module_param(sva, bool, 0644);
32MODULE_PARM_DESC(sva, "Toggle SVA support on/off");
33
Dave Jiangbfe1d562020-01-21 16:43:59 -070034#define DRV_NAME "idxd"
35
Dave Jiang8e50d392020-10-27 10:34:35 -070036bool support_enqcmd;
Dave Jiang4b73e4e2021-04-15 16:38:03 -070037DEFINE_IDA(idxd_ida);
Dave Jiangbfe1d562020-01-21 16:43:59 -070038
Dave Jiang435b5122021-04-15 16:38:09 -070039static 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 Jiangbfe1d562020-01-21 16:43:59 -070056static struct pci_device_id idxd_pci_tbl[] = {
57 /* DSA ver 1.0 platforms */
Dave Jiang435b5122021-04-15 16:38:09 -070058 { PCI_DEVICE_DATA(INTEL, DSA_SPR0, &idxd_driver_data[IDXD_TYPE_DSA]) },
Dave Jiangf25b46382020-11-17 13:39:14 -070059
60 /* IAX ver 1.0 platforms */
Dave Jiang435b5122021-04-15 16:38:09 -070061 { PCI_DEVICE_DATA(INTEL, IAX_SPR0, &idxd_driver_data[IDXD_TYPE_IAX]) },
Dave Jiangbfe1d562020-01-21 16:43:59 -070062 { 0, }
63};
64MODULE_DEVICE_TABLE(pci, idxd_pci_tbl);
65
Dave Jiangbfe1d562020-01-21 16:43:59 -070066static int idxd_setup_interrupts(struct idxd_device *idxd)
67{
68 struct pci_dev *pdev = idxd->pdev;
69 struct device *dev = &pdev->dev;
Dave Jiangbfe1d562020-01-21 16:43:59 -070070 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 Jiang5fc8e852021-04-15 16:37:15 -070077 return -ENOSPC;
Dave Jiangbfe1d562020-01-21 16:43:59 -070078 }
79
Dave Jiang5fc8e852021-04-15 16:37:15 -070080 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 Jiangbfe1d562020-01-21 16:43:59 -070084 }
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 Jiang47c16ac2021-04-15 16:37:33 -070091 idxd->irq_entries = kcalloc_node(msixcnt, sizeof(struct idxd_irq_entry),
92 GFP_KERNEL, dev_to_node(dev));
Dave Jiangbfe1d562020-01-21 16:43:59 -070093 if (!idxd->irq_entries) {
94 rc = -ENOMEM;
Dave Jiang5fc8e852021-04-15 16:37:15 -070095 goto err_irq_entries;
Dave Jiangbfe1d562020-01-21 16:43:59 -070096 }
97
98 for (i = 0; i < msixcnt; i++) {
99 idxd->irq_entries[i].id = i;
100 idxd->irq_entries[i].idxd = idxd;
Dave Jiang5fc8e852021-04-15 16:37:15 -0700101 idxd->irq_entries[i].vector = pci_irq_vector(pdev, i);
Dave Jiange4f4d8c2020-10-27 10:34:40 -0700102 spin_lock_init(&idxd->irq_entries[i].list_lock);
Dave Jiangbfe1d562020-01-21 16:43:59 -0700103 }
104
Dave Jiangbfe1d562020-01-21 16:43:59 -0700105 irq_entry = &idxd->irq_entries[0];
Dave Jianga1610462021-04-20 12:00:34 -0700106 rc = request_threaded_irq(irq_entry->vector, NULL, idxd_misc_thread,
Dave Jiang5fc8e852021-04-15 16:37:15 -0700107 0, "idxd-misc", irq_entry);
Dave Jiangbfe1d562020-01-21 16:43:59 -0700108 if (rc < 0) {
109 dev_err(dev, "Failed to allocate misc interrupt.\n");
Dave Jiang5fc8e852021-04-15 16:37:15 -0700110 goto err_misc_irq;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700111 }
112
Dave Jiang5fc8e852021-04-15 16:37:15 -0700113 dev_dbg(dev, "Allocated idxd-misc handler on msix vector %d\n", irq_entry->vector);
Dave Jiangbfe1d562020-01-21 16:43:59 -0700114
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 Jiangbfe1d562020-01-21 16:43:59 -0700119 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 Jianga1610462021-04-20 12:00:34 -0700123 rc = request_threaded_irq(irq_entry->vector, NULL,
Dave Jiang5fc8e852021-04-15 16:37:15 -0700124 idxd_wq_thread, 0, "idxd-portal", irq_entry);
Dave Jiangbfe1d562020-01-21 16:43:59 -0700125 if (rc < 0) {
Dave Jiang5fc8e852021-04-15 16:37:15 -0700126 dev_err(dev, "Failed to allocate irq %d.\n", irq_entry->vector);
127 goto err_wq_irqs;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700128 }
Dave Jiangeb15e712021-04-20 11:46:34 -0700129
Dave Jiang5fc8e852021-04-15 16:37:15 -0700130 dev_dbg(dev, "Allocated idxd-msix %d for vector %d\n", i, irq_entry->vector);
Dave Jiangeb15e712021-04-20 11:46:34 -0700131 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 Jiangbfe1d562020-01-21 16:43:59 -0700148 }
149
150 idxd_unmask_error_interrupts(idxd);
Dave Jiang6df0e6c2021-04-12 09:23:27 -0700151 idxd_msix_perm_setup(idxd);
Dave Jiangbfe1d562020-01-21 16:43:59 -0700152 return 0;
153
Dave Jiang5fc8e852021-04-15 16:37:15 -0700154 err_wq_irqs:
155 while (--i >= 0) {
156 irq_entry = &idxd->irq_entries[i];
157 free_irq(irq_entry->vector, irq_entry);
Dave Jiangeb15e712021-04-20 11:46:34 -0700158 if (i != 0)
159 idxd_device_release_int_handle(idxd,
160 idxd->int_handles[i], IDXD_IRQ_MSIX);
Dave Jiang5fc8e852021-04-15 16:37:15 -0700161 }
162 err_misc_irq:
Dave Jiangbfe1d562020-01-21 16:43:59 -0700163 /* Disable error interrupt generation */
164 idxd_mask_error_interrupts(idxd);
Dave Jiang5fc8e852021-04-15 16:37:15 -0700165 err_irq_entries:
166 pci_free_irq_vectors(pdev);
Dave Jiangbfe1d562020-01-21 16:43:59 -0700167 dev_err(dev, "No usable interrupts\n");
168 return rc;
169}
170
Dave Jiang7c5dd232021-04-15 16:37:39 -0700171static 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 Jiang4b73e4e2021-04-15 16:38:03 -0700193 wq->conf_dev.bus = &dsa_bus_type;
Dave Jiang7c5dd232021-04-15 16:37:39 -0700194 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 Jiang04922b72021-04-15 16:37:57 -0700202 init_waitqueue_head(&wq->err_queue);
Dave Jiang93a40a62021-04-20 11:46:22 -0700203 init_completion(&wq->wq_dead);
Dave Jiang7c5dd232021-04-15 16:37:39 -0700204 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 Jiang75b91132021-04-15 16:37:44 -0700223static 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 Jiangdefe49f2021-04-15 16:37:51 -0700263static 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 Jiang4b73e4e2021-04-15 16:38:03 -0700285 group->conf_dev.bus = &dsa_bus_type;
Dave Jiangdefe49f2021-04-15 16:37:51 -0700286 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 Jiangbfe1d562020-01-21 16:43:59 -0700306static int idxd_setup_internals(struct idxd_device *idxd)
307{
308 struct device *dev = &idxd->pdev->dev;
Dave Jiangdefe49f2021-04-15 16:37:51 -0700309 int rc, i;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700310
Dave Jiang0d5c10b2020-06-26 11:11:18 -0700311 init_waitqueue_head(&idxd->cmd_waitq);
Dave Jiang7c5dd232021-04-15 16:37:39 -0700312
Dave Jiangeb15e712021-04-20 11:46:34 -0700313 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 Jiang7c5dd232021-04-15 16:37:39 -0700319 rc = idxd_setup_wqs(idxd);
320 if (rc < 0)
Dave Jiangeb15e712021-04-20 11:46:34 -0700321 goto err_wqs;
Dave Jiang7c5dd232021-04-15 16:37:39 -0700322
Dave Jiang75b91132021-04-15 16:37:44 -0700323 rc = idxd_setup_engines(idxd);
324 if (rc < 0)
325 goto err_engine;
326
Dave Jiangdefe49f2021-04-15 16:37:51 -0700327 rc = idxd_setup_groups(idxd);
328 if (rc < 0)
329 goto err_group;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700330
Dave Jiang0d5c10b2020-06-26 11:11:18 -0700331 idxd->wq = create_workqueue(dev_name(dev));
Dave Jiang7c5dd232021-04-15 16:37:39 -0700332 if (!idxd->wq) {
333 rc = -ENOMEM;
Dave Jiangdefe49f2021-04-15 16:37:51 -0700334 goto err_wkq_create;
Dave Jiang7c5dd232021-04-15 16:37:39 -0700335 }
Dave Jiang0d5c10b2020-06-26 11:11:18 -0700336
Dave Jiangbfe1d562020-01-21 16:43:59 -0700337 return 0;
Dave Jiang7c5dd232021-04-15 16:37:39 -0700338
Dave Jiangdefe49f2021-04-15 16:37:51 -0700339 err_wkq_create:
340 for (i = 0; i < idxd->max_groups; i++)
341 put_device(&idxd->groups[i]->conf_dev);
342 err_group:
Dave Jiang75b91132021-04-15 16:37:44 -0700343 for (i = 0; i < idxd->max_engines; i++)
344 put_device(&idxd->engines[i]->conf_dev);
345 err_engine:
Dave Jiang7c5dd232021-04-15 16:37:39 -0700346 for (i = 0; i < idxd->max_wqs; i++)
347 put_device(&idxd->wqs[i]->conf_dev);
Dave Jiangeb15e712021-04-20 11:46:34 -0700348 err_wqs:
349 kfree(idxd->int_handles);
Dave Jiang7c5dd232021-04-15 16:37:39 -0700350 return rc;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700351}
352
353static 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 Jiang2f8417a2020-10-30 08:51:56 -0700359 offsets.bits[1] = ioread64(idxd->reg_base + IDXD_TABLE_OFFSET + sizeof(u64));
360 idxd->grpcfg_offset = offsets.grpcfg * IDXD_TABLE_MULT;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700361 dev_dbg(dev, "IDXD Group Config Offset: %#x\n", idxd->grpcfg_offset);
Dave Jiang2f8417a2020-10-30 08:51:56 -0700362 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 Jiangbfe1d562020-01-21 16:43:59 -0700367 dev_dbg(dev, "IDXD Perfmon Offset: %#x\n", idxd->perfmon_offset);
368}
369
370static 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 Jiangeb15e712021-04-20 11:46:34 -0700378
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 Jiangbfe1d562020-01-21 16:43:59 -0700384 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 Jiangc52ca472020-01-21 16:44:05 -0700399 idxd->nr_tokens = idxd->max_tokens;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700400
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 Jiangd98793b2020-10-27 14:34:09 -0700415 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 Jiangbfe1d562020-01-21 16:43:59 -0700417
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 Jiang435b5122021-04-15 16:38:09 -0700426static struct idxd_device *idxd_alloc(struct pci_dev *pdev, struct idxd_driver_data *data)
Dave Jiangbfe1d562020-01-21 16:43:59 -0700427{
428 struct device *dev = &pdev->dev;
429 struct idxd_device *idxd;
Dave Jiang47c16ac2021-04-15 16:37:33 -0700430 int rc;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700431
Dave Jiang47c16ac2021-04-15 16:37:33 -0700432 idxd = kzalloc_node(sizeof(*idxd), GFP_KERNEL, dev_to_node(dev));
Dave Jiangbfe1d562020-01-21 16:43:59 -0700433 if (!idxd)
434 return NULL;
435
436 idxd->pdev = pdev;
Dave Jiang435b5122021-04-15 16:38:09 -0700437 idxd->data = data;
Dave Jiang4b73e4e2021-04-15 16:38:03 -0700438 idxd->id = ida_alloc(&idxd_ida, GFP_KERNEL);
Dave Jiang47c16ac2021-04-15 16:37:33 -0700439 if (idxd->id < 0)
440 return NULL;
441
442 device_initialize(&idxd->conf_dev);
443 idxd->conf_dev.parent = dev;
Dave Jiang4b73e4e2021-04-15 16:38:03 -0700444 idxd->conf_dev.bus = &dsa_bus_type;
Dave Jiang435b5122021-04-15 16:38:09 -0700445 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 Jiang47c16ac2021-04-15 16:37:33 -0700447 if (rc < 0) {
448 put_device(&idxd->conf_dev);
449 return NULL;
450 }
451
Dave Jiangbfe1d562020-01-21 16:43:59 -0700452 spin_lock_init(&idxd->dev_lock);
Dave Jiang53b2ee7f2021-04-20 12:00:56 -0700453 spin_lock_init(&idxd->cmd_lock);
Dave Jiangbfe1d562020-01-21 16:43:59 -0700454
455 return idxd;
456}
457
Dave Jiang8e50d392020-10-27 10:34:35 -0700458static 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
485static 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 Jiangbfe1d562020-01-21 16:43:59 -0700492static 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 Jiang89e3bec2021-02-01 08:26:14 -0700499 rc = idxd_device_init_reset(idxd);
500 if (rc < 0)
501 return rc;
502
Dave Jiangbfe1d562020-01-21 16:43:59 -0700503 dev_dbg(dev, "IDXD reset complete\n");
504
Dave Jiang03d939c2021-01-22 11:46:00 -0700505 if (IS_ENABLED(CONFIG_INTEL_IDXD_SVM) && sva) {
Dave Jiangcf5f86a2021-04-20 11:46:46 -0700506 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 Jiang03d939c2021-01-22 11:46:00 -0700518 } else if (!sva) {
519 dev_warn(dev, "User forced SVA off via module param.\n");
Dave Jiang8e50d392020-10-27 10:34:35 -0700520 }
521
Dave Jiangbfe1d562020-01-21 16:43:59 -0700522 idxd_read_caps(idxd);
523 idxd_read_table_offsets(idxd);
524
525 rc = idxd_setup_internals(idxd);
526 if (rc)
Dave Jiang7c5dd232021-04-15 16:37:39 -0700527 goto err;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700528
Dave Jiang8c66bbdc2021-04-20 11:46:28 -0700529 /* 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 Jiangbfe1d562020-01-21 16:43:59 -0700537 rc = idxd_setup_interrupts(idxd);
538 if (rc)
Dave Jiang7c5dd232021-04-15 16:37:39 -0700539 goto err;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700540
541 dev_dbg(dev, "IDXD interrupt setup complete.\n");
542
Dave Jiang42d279f2020-01-21 16:44:29 -0700543 idxd->major = idxd_cdev_get_major(idxd);
544
Tom Zanussi0bde4442021-04-24 10:04:16 -0500545 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 Jiangbfe1d562020-01-21 16:43:59 -0700549 dev_dbg(dev, "IDXD device %d probed successfully\n", idxd->id);
550 return 0;
551
Dave Jiang7c5dd232021-04-15 16:37:39 -0700552 err:
Dave Jiang8e50d392020-10-27 10:34:35 -0700553 if (device_pasid_enabled(idxd))
554 idxd_disable_system_pasid(idxd);
Dave Jiangcf5f86a2021-04-20 11:46:46 -0700555 iommu_dev_disable_feature(dev, IOMMU_DEV_FEAT_SVA);
Dave Jiangbfe1d562020-01-21 16:43:59 -0700556 return rc;
557}
558
559static int idxd_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
560{
Dave Jiangbfe1d562020-01-21 16:43:59 -0700561 struct device *dev = &pdev->dev;
562 struct idxd_device *idxd;
Dave Jiang435b5122021-04-15 16:38:09 -0700563 struct idxd_driver_data *data = (struct idxd_driver_data *)id->driver_data;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700564 int rc;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700565
Dave Jianga39c7cd2021-04-15 16:37:21 -0700566 rc = pci_enable_device(pdev);
Dave Jiangbfe1d562020-01-21 16:43:59 -0700567 if (rc)
568 return rc;
569
Dave Jiang8e50d392020-10-27 10:34:35 -0700570 dev_dbg(dev, "Alloc IDXD context\n");
Dave Jiang435b5122021-04-15 16:38:09 -0700571 idxd = idxd_alloc(pdev, data);
Dave Jianga39c7cd2021-04-15 16:37:21 -0700572 if (!idxd) {
573 rc = -ENOMEM;
574 goto err_idxd_alloc;
575 }
Dave Jiangbfe1d562020-01-21 16:43:59 -0700576
Dave Jiang8e50d392020-10-27 10:34:35 -0700577 dev_dbg(dev, "Mapping BARs\n");
Dave Jianga39c7cd2021-04-15 16:37:21 -0700578 idxd->reg_base = pci_iomap(pdev, IDXD_MMIO_BAR, 0);
579 if (!idxd->reg_base) {
580 rc = -ENOMEM;
581 goto err_iomap;
582 }
Dave Jiangbfe1d562020-01-21 16:43:59 -0700583
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 Jianga39c7cd2021-04-15 16:37:21 -0700589 goto err;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700590
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 Jianga39c7cd2021-04-15 16:37:21 -0700595 goto err;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700596
Dave Jiangbfe1d562020-01-21 16:43:59 -0700597 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 Jianga39c7cd2021-04-15 16:37:21 -0700605 goto err;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700606 }
607
Dave Jiang47c16ac2021-04-15 16:37:33 -0700608 rc = idxd_register_devices(idxd);
Dave Jiangc52ca472020-01-21 16:44:05 -0700609 if (rc) {
610 dev_err(dev, "IDXD sysfs setup failed\n");
Dave Jianga39c7cd2021-04-15 16:37:21 -0700611 goto err;
Dave Jiangc52ca472020-01-21 16:44:05 -0700612 }
613
614 idxd->state = IDXD_DEV_CONF_READY;
615
Dave Jiangbfe1d562020-01-21 16:43:59 -0700616 dev_info(&pdev->dev, "Intel(R) Accelerator Device (v%x)\n",
617 idxd->hw.version);
618
619 return 0;
Dave Jianga39c7cd2021-04-15 16:37:21 -0700620
621 err:
622 pci_iounmap(pdev, idxd->reg_base);
623 err_iomap:
Dave Jiang47c16ac2021-04-15 16:37:33 -0700624 put_device(&idxd->conf_dev);
Dave Jianga39c7cd2021-04-15 16:37:21 -0700625 err_idxd_alloc:
626 pci_disable_device(pdev);
627 return rc;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700628}
629
Dave Jiang8f47d1a2020-01-21 16:44:23 -0700630static 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
645static 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 Jiang5b0c68c2021-04-20 11:46:51 -0700656void 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 Jiangeb15e712021-04-20 11:46:34 -0700668static 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 Jiangbfe1d562020-01-21 16:43:59 -0700686static 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 Jiangbfe1d562020-01-21 16:43:59 -0700692
Dave Jiangbfe1d562020-01-21 16:43:59 -0700693 rc = idxd_device_disable(idxd);
Dave Jiangbfe1d562020-01-21 16:43:59 -0700694 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 Jiang5fc8e852021-04-15 16:37:15 -0700703 synchronize_irq(irq_entry->vector);
704 free_irq(irq_entry->vector, irq_entry);
Dave Jiangbfe1d562020-01-21 16:43:59 -0700705 if (i == 0)
706 continue;
Dave Jiang8f47d1a2020-01-21 16:44:23 -0700707 idxd_flush_pending_llist(irq_entry);
708 idxd_flush_work_list(irq_entry);
Dave Jiangbfe1d562020-01-21 16:43:59 -0700709 }
Dave Jiang0d5c10b2020-06-26 11:11:18 -0700710
Dave Jiang6df0e6c2021-04-12 09:23:27 -0700711 idxd_msix_perm_clear(idxd);
Dave Jiangeb15e712021-04-20 11:46:34 -0700712 idxd_release_int_handles(idxd);
Dave Jiang5fc8e852021-04-15 16:37:15 -0700713 pci_free_irq_vectors(pdev);
Dave Jianga39c7cd2021-04-15 16:37:21 -0700714 pci_iounmap(pdev, idxd->reg_base);
715 pci_disable_device(pdev);
Dave Jiang0d5c10b2020-06-26 11:11:18 -0700716 destroy_workqueue(idxd->wq);
Dave Jiangbfe1d562020-01-21 16:43:59 -0700717}
718
719static 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 Jiang8e50d392020-10-27 10:34:35 -0700725 if (device_pasid_enabled(idxd))
726 idxd_disable_system_pasid(idxd);
Dave Jiang47c16ac2021-04-15 16:37:33 -0700727 idxd_unregister_devices(idxd);
Tom Zanussi0bde4442021-04-24 10:04:16 -0500728 perfmon_pmu_remove(idxd);
Dave Jiangcf5f86a2021-04-20 11:46:46 -0700729 iommu_dev_disable_feature(&pdev->dev, IOMMU_DEV_FEAT_SVA);
Dave Jiangbfe1d562020-01-21 16:43:59 -0700730}
731
732static 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
740static int __init idxd_init_module(void)
741{
Dave Jiang4b73e4e2021-04-15 16:38:03 -0700742 int err;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700743
744 /*
Dave Jiang8e50d392020-10-27 10:34:35 -0700745 * If the CPU does not support MOVDIR64B or ENQCMDS, there's no point in
Dave Jiangbfe1d562020-01-21 16:43:59 -0700746 * 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 Jiang8e50d392020-10-27 10:34:35 -0700753 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 Jiangbfe1d562020-01-21 16:43:59 -0700757
Tom Zanussi0bde4442021-04-24 10:04:16 -0500758 perfmon_init();
759
Dave Jiangc52ca472020-01-21 16:44:05 -0700760 err = idxd_register_bus_type();
761 if (err < 0)
Dave Jiangbfe1d562020-01-21 16:43:59 -0700762 return err;
763
Dave Jiangc52ca472020-01-21 16:44:05 -0700764 err = idxd_register_driver();
765 if (err < 0)
766 goto err_idxd_driver_register;
767
Dave Jiang42d279f2020-01-21 16:44:29 -0700768 err = idxd_cdev_register();
769 if (err)
770 goto err_cdev_register;
771
Dave Jiangc52ca472020-01-21 16:44:05 -0700772 err = pci_register_driver(&idxd_pci_driver);
773 if (err)
774 goto err_pci_register;
775
Dave Jiangbfe1d562020-01-21 16:43:59 -0700776 return 0;
Dave Jiangc52ca472020-01-21 16:44:05 -0700777
778err_pci_register:
Dave Jiang42d279f2020-01-21 16:44:29 -0700779 idxd_cdev_remove();
780err_cdev_register:
Dave Jiangc52ca472020-01-21 16:44:05 -0700781 idxd_unregister_driver();
782err_idxd_driver_register:
783 idxd_unregister_bus_type();
784 return err;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700785}
786module_init(idxd_init_module);
787
788static void __exit idxd_exit_module(void)
789{
790 pci_unregister_driver(&idxd_pci_driver);
Dave Jiang42d279f2020-01-21 16:44:29 -0700791 idxd_cdev_remove();
Dave Jiangc52ca472020-01-21 16:44:05 -0700792 idxd_unregister_bus_type();
Tom Zanussi0bde4442021-04-24 10:04:16 -0500793 perfmon_exit();
Dave Jiangbfe1d562020-01-21 16:43:59 -0700794}
795module_exit(idxd_exit_module);