blob: e6bfd55e421ba3526acce47002a2d85b058ab442 [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 Jiangeb15e712021-04-20 11:46:34 -0700128
Dave Jiang5fc8e852021-04-15 16:37:15 -0700129 dev_dbg(dev, "Allocated idxd-msix %d for vector %d\n", i, irq_entry->vector);
Dave Jiangeb15e712021-04-20 11:46:34 -0700130 if (idxd->hw.cmd_cap & BIT(IDXD_CMD_REQUEST_INT_HANDLE)) {
131 /*
132 * The MSIX vector enumeration starts at 1 with vector 0 being the
133 * misc interrupt that handles non I/O completion events. The
134 * interrupt handles are for IMS enumeration on guest. The misc
135 * interrupt vector does not require a handle and therefore we start
136 * the int_handles at index 0. Since 'i' starts at 1, the first
137 * int_handles index will be 0.
138 */
139 rc = idxd_device_request_int_handle(idxd, i, &idxd->int_handles[i - 1],
140 IDXD_IRQ_MSIX);
141 if (rc < 0) {
142 free_irq(irq_entry->vector, irq_entry);
143 goto err_wq_irqs;
144 }
145 dev_dbg(dev, "int handle requested: %u\n", idxd->int_handles[i - 1]);
146 }
Dave Jiangbfe1d562020-01-21 16:43:59 -0700147 }
148
149 idxd_unmask_error_interrupts(idxd);
Dave Jiang6df0e6c2021-04-12 09:23:27 -0700150 idxd_msix_perm_setup(idxd);
Dave Jiangbfe1d562020-01-21 16:43:59 -0700151 return 0;
152
Dave Jiang5fc8e852021-04-15 16:37:15 -0700153 err_wq_irqs:
154 while (--i >= 0) {
155 irq_entry = &idxd->irq_entries[i];
156 free_irq(irq_entry->vector, irq_entry);
Dave Jiangeb15e712021-04-20 11:46:34 -0700157 if (i != 0)
158 idxd_device_release_int_handle(idxd,
159 idxd->int_handles[i], IDXD_IRQ_MSIX);
Dave Jiang5fc8e852021-04-15 16:37:15 -0700160 }
161 err_misc_irq:
Dave Jiangbfe1d562020-01-21 16:43:59 -0700162 /* Disable error interrupt generation */
163 idxd_mask_error_interrupts(idxd);
Dave Jiang5fc8e852021-04-15 16:37:15 -0700164 err_irq_entries:
165 pci_free_irq_vectors(pdev);
Dave Jiangbfe1d562020-01-21 16:43:59 -0700166 dev_err(dev, "No usable interrupts\n");
167 return rc;
168}
169
Dave Jiang7c5dd232021-04-15 16:37:39 -0700170static int idxd_setup_wqs(struct idxd_device *idxd)
171{
172 struct device *dev = &idxd->pdev->dev;
173 struct idxd_wq *wq;
174 int i, rc;
175
176 idxd->wqs = kcalloc_node(idxd->max_wqs, sizeof(struct idxd_wq *),
177 GFP_KERNEL, dev_to_node(dev));
178 if (!idxd->wqs)
179 return -ENOMEM;
180
181 for (i = 0; i < idxd->max_wqs; i++) {
182 wq = kzalloc_node(sizeof(*wq), GFP_KERNEL, dev_to_node(dev));
183 if (!wq) {
184 rc = -ENOMEM;
185 goto err;
186 }
187
188 wq->id = i;
189 wq->idxd = idxd;
190 device_initialize(&wq->conf_dev);
191 wq->conf_dev.parent = &idxd->conf_dev;
Dave Jiang4b73e4e2021-04-15 16:38:03 -0700192 wq->conf_dev.bus = &dsa_bus_type;
Dave Jiang7c5dd232021-04-15 16:37:39 -0700193 wq->conf_dev.type = &idxd_wq_device_type;
194 rc = dev_set_name(&wq->conf_dev, "wq%d.%d", idxd->id, wq->id);
195 if (rc < 0) {
196 put_device(&wq->conf_dev);
197 goto err;
198 }
199
200 mutex_init(&wq->wq_lock);
Dave Jiang04922b72021-04-15 16:37:57 -0700201 init_waitqueue_head(&wq->err_queue);
Dave Jiang93a40a62021-04-20 11:46:22 -0700202 init_completion(&wq->wq_dead);
Dave Jiang7c5dd232021-04-15 16:37:39 -0700203 wq->max_xfer_bytes = idxd->max_xfer_bytes;
204 wq->max_batch_size = idxd->max_batch_size;
205 wq->wqcfg = kzalloc_node(idxd->wqcfg_size, GFP_KERNEL, dev_to_node(dev));
206 if (!wq->wqcfg) {
207 put_device(&wq->conf_dev);
208 rc = -ENOMEM;
209 goto err;
210 }
211 idxd->wqs[i] = wq;
212 }
213
214 return 0;
215
216 err:
217 while (--i >= 0)
218 put_device(&idxd->wqs[i]->conf_dev);
219 return rc;
220}
221
Dave Jiang75b91132021-04-15 16:37:44 -0700222static int idxd_setup_engines(struct idxd_device *idxd)
223{
224 struct idxd_engine *engine;
225 struct device *dev = &idxd->pdev->dev;
226 int i, rc;
227
228 idxd->engines = kcalloc_node(idxd->max_engines, sizeof(struct idxd_engine *),
229 GFP_KERNEL, dev_to_node(dev));
230 if (!idxd->engines)
231 return -ENOMEM;
232
233 for (i = 0; i < idxd->max_engines; i++) {
234 engine = kzalloc_node(sizeof(*engine), GFP_KERNEL, dev_to_node(dev));
235 if (!engine) {
236 rc = -ENOMEM;
237 goto err;
238 }
239
240 engine->id = i;
241 engine->idxd = idxd;
242 device_initialize(&engine->conf_dev);
243 engine->conf_dev.parent = &idxd->conf_dev;
244 engine->conf_dev.type = &idxd_engine_device_type;
245 rc = dev_set_name(&engine->conf_dev, "engine%d.%d", idxd->id, engine->id);
246 if (rc < 0) {
247 put_device(&engine->conf_dev);
248 goto err;
249 }
250
251 idxd->engines[i] = engine;
252 }
253
254 return 0;
255
256 err:
257 while (--i >= 0)
258 put_device(&idxd->engines[i]->conf_dev);
259 return rc;
260}
261
Dave Jiangdefe49f2021-04-15 16:37:51 -0700262static int idxd_setup_groups(struct idxd_device *idxd)
263{
264 struct device *dev = &idxd->pdev->dev;
265 struct idxd_group *group;
266 int i, rc;
267
268 idxd->groups = kcalloc_node(idxd->max_groups, sizeof(struct idxd_group *),
269 GFP_KERNEL, dev_to_node(dev));
270 if (!idxd->groups)
271 return -ENOMEM;
272
273 for (i = 0; i < idxd->max_groups; i++) {
274 group = kzalloc_node(sizeof(*group), GFP_KERNEL, dev_to_node(dev));
275 if (!group) {
276 rc = -ENOMEM;
277 goto err;
278 }
279
280 group->id = i;
281 group->idxd = idxd;
282 device_initialize(&group->conf_dev);
283 group->conf_dev.parent = &idxd->conf_dev;
Dave Jiang4b73e4e2021-04-15 16:38:03 -0700284 group->conf_dev.bus = &dsa_bus_type;
Dave Jiangdefe49f2021-04-15 16:37:51 -0700285 group->conf_dev.type = &idxd_group_device_type;
286 rc = dev_set_name(&group->conf_dev, "group%d.%d", idxd->id, group->id);
287 if (rc < 0) {
288 put_device(&group->conf_dev);
289 goto err;
290 }
291
292 idxd->groups[i] = group;
293 group->tc_a = -1;
294 group->tc_b = -1;
295 }
296
297 return 0;
298
299 err:
300 while (--i >= 0)
301 put_device(&idxd->groups[i]->conf_dev);
302 return rc;
303}
304
Dave Jiangbfe1d562020-01-21 16:43:59 -0700305static int idxd_setup_internals(struct idxd_device *idxd)
306{
307 struct device *dev = &idxd->pdev->dev;
Dave Jiangdefe49f2021-04-15 16:37:51 -0700308 int rc, i;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700309
Dave Jiang0d5c10b2020-06-26 11:11:18 -0700310 init_waitqueue_head(&idxd->cmd_waitq);
Dave Jiang7c5dd232021-04-15 16:37:39 -0700311
Dave Jiangeb15e712021-04-20 11:46:34 -0700312 if (idxd->hw.cmd_cap & BIT(IDXD_CMD_REQUEST_INT_HANDLE)) {
313 idxd->int_handles = devm_kcalloc(dev, idxd->max_wqs, sizeof(int), GFP_KERNEL);
314 if (!idxd->int_handles)
315 return -ENOMEM;
316 }
317
Dave Jiang7c5dd232021-04-15 16:37:39 -0700318 rc = idxd_setup_wqs(idxd);
319 if (rc < 0)
Dave Jiangeb15e712021-04-20 11:46:34 -0700320 goto err_wqs;
Dave Jiang7c5dd232021-04-15 16:37:39 -0700321
Dave Jiang75b91132021-04-15 16:37:44 -0700322 rc = idxd_setup_engines(idxd);
323 if (rc < 0)
324 goto err_engine;
325
Dave Jiangdefe49f2021-04-15 16:37:51 -0700326 rc = idxd_setup_groups(idxd);
327 if (rc < 0)
328 goto err_group;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700329
Dave Jiang0d5c10b2020-06-26 11:11:18 -0700330 idxd->wq = create_workqueue(dev_name(dev));
Dave Jiang7c5dd232021-04-15 16:37:39 -0700331 if (!idxd->wq) {
332 rc = -ENOMEM;
Dave Jiangdefe49f2021-04-15 16:37:51 -0700333 goto err_wkq_create;
Dave Jiang7c5dd232021-04-15 16:37:39 -0700334 }
Dave Jiang0d5c10b2020-06-26 11:11:18 -0700335
Dave Jiangbfe1d562020-01-21 16:43:59 -0700336 return 0;
Dave Jiang7c5dd232021-04-15 16:37:39 -0700337
Dave Jiangdefe49f2021-04-15 16:37:51 -0700338 err_wkq_create:
339 for (i = 0; i < idxd->max_groups; i++)
340 put_device(&idxd->groups[i]->conf_dev);
341 err_group:
Dave Jiang75b91132021-04-15 16:37:44 -0700342 for (i = 0; i < idxd->max_engines; i++)
343 put_device(&idxd->engines[i]->conf_dev);
344 err_engine:
Dave Jiang7c5dd232021-04-15 16:37:39 -0700345 for (i = 0; i < idxd->max_wqs; i++)
346 put_device(&idxd->wqs[i]->conf_dev);
Dave Jiangeb15e712021-04-20 11:46:34 -0700347 err_wqs:
348 kfree(idxd->int_handles);
Dave Jiang7c5dd232021-04-15 16:37:39 -0700349 return rc;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700350}
351
352static void idxd_read_table_offsets(struct idxd_device *idxd)
353{
354 union offsets_reg offsets;
355 struct device *dev = &idxd->pdev->dev;
356
357 offsets.bits[0] = ioread64(idxd->reg_base + IDXD_TABLE_OFFSET);
Dave Jiang2f8417a2020-10-30 08:51:56 -0700358 offsets.bits[1] = ioread64(idxd->reg_base + IDXD_TABLE_OFFSET + sizeof(u64));
359 idxd->grpcfg_offset = offsets.grpcfg * IDXD_TABLE_MULT;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700360 dev_dbg(dev, "IDXD Group Config Offset: %#x\n", idxd->grpcfg_offset);
Dave Jiang2f8417a2020-10-30 08:51:56 -0700361 idxd->wqcfg_offset = offsets.wqcfg * IDXD_TABLE_MULT;
362 dev_dbg(dev, "IDXD Work Queue Config Offset: %#x\n", idxd->wqcfg_offset);
363 idxd->msix_perm_offset = offsets.msix_perm * IDXD_TABLE_MULT;
364 dev_dbg(dev, "IDXD MSIX Permission Offset: %#x\n", idxd->msix_perm_offset);
365 idxd->perfmon_offset = offsets.perfmon * IDXD_TABLE_MULT;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700366 dev_dbg(dev, "IDXD Perfmon Offset: %#x\n", idxd->perfmon_offset);
367}
368
369static void idxd_read_caps(struct idxd_device *idxd)
370{
371 struct device *dev = &idxd->pdev->dev;
372 int i;
373
374 /* reading generic capabilities */
375 idxd->hw.gen_cap.bits = ioread64(idxd->reg_base + IDXD_GENCAP_OFFSET);
376 dev_dbg(dev, "gen_cap: %#llx\n", idxd->hw.gen_cap.bits);
Dave Jiangeb15e712021-04-20 11:46:34 -0700377
378 if (idxd->hw.gen_cap.cmd_cap) {
379 idxd->hw.cmd_cap = ioread32(idxd->reg_base + IDXD_CMDCAP_OFFSET);
380 dev_dbg(dev, "cmd_cap: %#x\n", idxd->hw.cmd_cap);
381 }
382
Dave Jiangbfe1d562020-01-21 16:43:59 -0700383 idxd->max_xfer_bytes = 1ULL << idxd->hw.gen_cap.max_xfer_shift;
384 dev_dbg(dev, "max xfer size: %llu bytes\n", idxd->max_xfer_bytes);
385 idxd->max_batch_size = 1U << idxd->hw.gen_cap.max_batch_shift;
386 dev_dbg(dev, "max batch size: %u\n", idxd->max_batch_size);
387 if (idxd->hw.gen_cap.config_en)
388 set_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags);
389
390 /* reading group capabilities */
391 idxd->hw.group_cap.bits =
392 ioread64(idxd->reg_base + IDXD_GRPCAP_OFFSET);
393 dev_dbg(dev, "group_cap: %#llx\n", idxd->hw.group_cap.bits);
394 idxd->max_groups = idxd->hw.group_cap.num_groups;
395 dev_dbg(dev, "max groups: %u\n", idxd->max_groups);
396 idxd->max_tokens = idxd->hw.group_cap.total_tokens;
397 dev_dbg(dev, "max tokens: %u\n", idxd->max_tokens);
Dave Jiangc52ca472020-01-21 16:44:05 -0700398 idxd->nr_tokens = idxd->max_tokens;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700399
400 /* read engine capabilities */
401 idxd->hw.engine_cap.bits =
402 ioread64(idxd->reg_base + IDXD_ENGCAP_OFFSET);
403 dev_dbg(dev, "engine_cap: %#llx\n", idxd->hw.engine_cap.bits);
404 idxd->max_engines = idxd->hw.engine_cap.num_engines;
405 dev_dbg(dev, "max engines: %u\n", idxd->max_engines);
406
407 /* read workqueue capabilities */
408 idxd->hw.wq_cap.bits = ioread64(idxd->reg_base + IDXD_WQCAP_OFFSET);
409 dev_dbg(dev, "wq_cap: %#llx\n", idxd->hw.wq_cap.bits);
410 idxd->max_wq_size = idxd->hw.wq_cap.total_wq_size;
411 dev_dbg(dev, "total workqueue size: %u\n", idxd->max_wq_size);
412 idxd->max_wqs = idxd->hw.wq_cap.num_wqs;
413 dev_dbg(dev, "max workqueues: %u\n", idxd->max_wqs);
Dave Jiangd98793b2020-10-27 14:34:09 -0700414 idxd->wqcfg_size = 1 << (idxd->hw.wq_cap.wqcfg_size + IDXD_WQCFG_MIN);
415 dev_dbg(dev, "wqcfg size: %u\n", idxd->wqcfg_size);
Dave Jiangbfe1d562020-01-21 16:43:59 -0700416
417 /* reading operation capabilities */
418 for (i = 0; i < 4; i++) {
419 idxd->hw.opcap.bits[i] = ioread64(idxd->reg_base +
420 IDXD_OPCAP_OFFSET + i * sizeof(u64));
421 dev_dbg(dev, "opcap[%d]: %#llx\n", i, idxd->hw.opcap.bits[i]);
422 }
423}
424
Dave Jiang435b5122021-04-15 16:38:09 -0700425static struct idxd_device *idxd_alloc(struct pci_dev *pdev, struct idxd_driver_data *data)
Dave Jiangbfe1d562020-01-21 16:43:59 -0700426{
427 struct device *dev = &pdev->dev;
428 struct idxd_device *idxd;
Dave Jiang47c16ac2021-04-15 16:37:33 -0700429 int rc;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700430
Dave Jiang47c16ac2021-04-15 16:37:33 -0700431 idxd = kzalloc_node(sizeof(*idxd), GFP_KERNEL, dev_to_node(dev));
Dave Jiangbfe1d562020-01-21 16:43:59 -0700432 if (!idxd)
433 return NULL;
434
435 idxd->pdev = pdev;
Dave Jiang435b5122021-04-15 16:38:09 -0700436 idxd->data = data;
Dave Jiang4b73e4e2021-04-15 16:38:03 -0700437 idxd->id = ida_alloc(&idxd_ida, GFP_KERNEL);
Dave Jiang47c16ac2021-04-15 16:37:33 -0700438 if (idxd->id < 0)
439 return NULL;
440
441 device_initialize(&idxd->conf_dev);
442 idxd->conf_dev.parent = dev;
Dave Jiang4b73e4e2021-04-15 16:38:03 -0700443 idxd->conf_dev.bus = &dsa_bus_type;
Dave Jiang435b5122021-04-15 16:38:09 -0700444 idxd->conf_dev.type = idxd->data->dev_type;
445 rc = dev_set_name(&idxd->conf_dev, "%s%d", idxd->data->name_prefix, idxd->id);
Dave Jiang47c16ac2021-04-15 16:37:33 -0700446 if (rc < 0) {
447 put_device(&idxd->conf_dev);
448 return NULL;
449 }
450
Dave Jiangbfe1d562020-01-21 16:43:59 -0700451 spin_lock_init(&idxd->dev_lock);
452
453 return idxd;
454}
455
Dave Jiang8e50d392020-10-27 10:34:35 -0700456static int idxd_enable_system_pasid(struct idxd_device *idxd)
457{
458 int flags;
459 unsigned int pasid;
460 struct iommu_sva *sva;
461
462 flags = SVM_FLAG_SUPERVISOR_MODE;
463
464 sva = iommu_sva_bind_device(&idxd->pdev->dev, NULL, &flags);
465 if (IS_ERR(sva)) {
466 dev_warn(&idxd->pdev->dev,
467 "iommu sva bind failed: %ld\n", PTR_ERR(sva));
468 return PTR_ERR(sva);
469 }
470
471 pasid = iommu_sva_get_pasid(sva);
472 if (pasid == IOMMU_PASID_INVALID) {
473 iommu_sva_unbind_device(sva);
474 return -ENODEV;
475 }
476
477 idxd->sva = sva;
478 idxd->pasid = pasid;
479 dev_dbg(&idxd->pdev->dev, "system pasid: %u\n", pasid);
480 return 0;
481}
482
483static void idxd_disable_system_pasid(struct idxd_device *idxd)
484{
485
486 iommu_sva_unbind_device(idxd->sva);
487 idxd->sva = NULL;
488}
489
Dave Jiangbfe1d562020-01-21 16:43:59 -0700490static int idxd_probe(struct idxd_device *idxd)
491{
492 struct pci_dev *pdev = idxd->pdev;
493 struct device *dev = &pdev->dev;
494 int rc;
495
496 dev_dbg(dev, "%s entered and resetting device\n", __func__);
Dave Jiang89e3bec2021-02-01 08:26:14 -0700497 rc = idxd_device_init_reset(idxd);
498 if (rc < 0)
499 return rc;
500
Dave Jiangbfe1d562020-01-21 16:43:59 -0700501 dev_dbg(dev, "IDXD reset complete\n");
502
Dave Jiang03d939c2021-01-22 11:46:00 -0700503 if (IS_ENABLED(CONFIG_INTEL_IDXD_SVM) && sva) {
Dave Jiangcf5f86a2021-04-20 11:46:46 -0700504 rc = iommu_dev_enable_feature(dev, IOMMU_DEV_FEAT_SVA);
505 if (rc == 0) {
506 rc = idxd_enable_system_pasid(idxd);
507 if (rc < 0) {
508 iommu_dev_disable_feature(dev, IOMMU_DEV_FEAT_SVA);
509 dev_warn(dev, "Failed to enable PASID. No SVA support: %d\n", rc);
510 } else {
511 set_bit(IDXD_FLAG_PASID_ENABLED, &idxd->flags);
512 }
513 } else {
514 dev_warn(dev, "Unable to turn on SVA feature.\n");
515 }
Dave Jiang03d939c2021-01-22 11:46:00 -0700516 } else if (!sva) {
517 dev_warn(dev, "User forced SVA off via module param.\n");
Dave Jiang8e50d392020-10-27 10:34:35 -0700518 }
519
Dave Jiangbfe1d562020-01-21 16:43:59 -0700520 idxd_read_caps(idxd);
521 idxd_read_table_offsets(idxd);
522
523 rc = idxd_setup_internals(idxd);
524 if (rc)
Dave Jiang7c5dd232021-04-15 16:37:39 -0700525 goto err;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700526
Dave Jiang8c66bbdc2021-04-20 11:46:28 -0700527 /* If the configs are readonly, then load them from device */
528 if (!test_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags)) {
529 dev_dbg(dev, "Loading RO device config\n");
530 rc = idxd_device_load_config(idxd);
531 if (rc < 0)
532 goto err;
533 }
534
Dave Jiangbfe1d562020-01-21 16:43:59 -0700535 rc = idxd_setup_interrupts(idxd);
536 if (rc)
Dave Jiang7c5dd232021-04-15 16:37:39 -0700537 goto err;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700538
539 dev_dbg(dev, "IDXD interrupt setup complete.\n");
540
Dave Jiang42d279f2020-01-21 16:44:29 -0700541 idxd->major = idxd_cdev_get_major(idxd);
542
Dave Jiangbfe1d562020-01-21 16:43:59 -0700543 dev_dbg(dev, "IDXD device %d probed successfully\n", idxd->id);
544 return 0;
545
Dave Jiang7c5dd232021-04-15 16:37:39 -0700546 err:
Dave Jiang8e50d392020-10-27 10:34:35 -0700547 if (device_pasid_enabled(idxd))
548 idxd_disable_system_pasid(idxd);
Dave Jiangcf5f86a2021-04-20 11:46:46 -0700549 iommu_dev_disable_feature(dev, IOMMU_DEV_FEAT_SVA);
Dave Jiangbfe1d562020-01-21 16:43:59 -0700550 return rc;
551}
552
553static int idxd_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
554{
Dave Jiangbfe1d562020-01-21 16:43:59 -0700555 struct device *dev = &pdev->dev;
556 struct idxd_device *idxd;
Dave Jiang435b5122021-04-15 16:38:09 -0700557 struct idxd_driver_data *data = (struct idxd_driver_data *)id->driver_data;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700558 int rc;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700559
Dave Jianga39c7cd2021-04-15 16:37:21 -0700560 rc = pci_enable_device(pdev);
Dave Jiangbfe1d562020-01-21 16:43:59 -0700561 if (rc)
562 return rc;
563
Dave Jiang8e50d392020-10-27 10:34:35 -0700564 dev_dbg(dev, "Alloc IDXD context\n");
Dave Jiang435b5122021-04-15 16:38:09 -0700565 idxd = idxd_alloc(pdev, data);
Dave Jianga39c7cd2021-04-15 16:37:21 -0700566 if (!idxd) {
567 rc = -ENOMEM;
568 goto err_idxd_alloc;
569 }
Dave Jiangbfe1d562020-01-21 16:43:59 -0700570
Dave Jiang8e50d392020-10-27 10:34:35 -0700571 dev_dbg(dev, "Mapping BARs\n");
Dave Jianga39c7cd2021-04-15 16:37:21 -0700572 idxd->reg_base = pci_iomap(pdev, IDXD_MMIO_BAR, 0);
573 if (!idxd->reg_base) {
574 rc = -ENOMEM;
575 goto err_iomap;
576 }
Dave Jiangbfe1d562020-01-21 16:43:59 -0700577
578 dev_dbg(dev, "Set DMA masks\n");
579 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
580 if (rc)
581 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
582 if (rc)
Dave Jianga39c7cd2021-04-15 16:37:21 -0700583 goto err;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700584
585 rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
586 if (rc)
587 rc = pci_set_consistent_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
Dave Jiangbfe1d562020-01-21 16:43:59 -0700591 dev_dbg(dev, "Set PCI master\n");
592 pci_set_master(pdev);
593 pci_set_drvdata(pdev, idxd);
594
595 idxd->hw.version = ioread32(idxd->reg_base + IDXD_VER_OFFSET);
596 rc = idxd_probe(idxd);
597 if (rc) {
598 dev_err(dev, "Intel(R) IDXD DMA Engine init failed\n");
Dave Jianga39c7cd2021-04-15 16:37:21 -0700599 goto err;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700600 }
601
Dave Jiang47c16ac2021-04-15 16:37:33 -0700602 rc = idxd_register_devices(idxd);
Dave Jiangc52ca472020-01-21 16:44:05 -0700603 if (rc) {
604 dev_err(dev, "IDXD sysfs setup failed\n");
Dave Jianga39c7cd2021-04-15 16:37:21 -0700605 goto err;
Dave Jiangc52ca472020-01-21 16:44:05 -0700606 }
607
608 idxd->state = IDXD_DEV_CONF_READY;
609
Dave Jiangbfe1d562020-01-21 16:43:59 -0700610 dev_info(&pdev->dev, "Intel(R) Accelerator Device (v%x)\n",
611 idxd->hw.version);
612
613 return 0;
Dave Jianga39c7cd2021-04-15 16:37:21 -0700614
615 err:
616 pci_iounmap(pdev, idxd->reg_base);
617 err_iomap:
Dave Jiang47c16ac2021-04-15 16:37:33 -0700618 put_device(&idxd->conf_dev);
Dave Jianga39c7cd2021-04-15 16:37:21 -0700619 err_idxd_alloc:
620 pci_disable_device(pdev);
621 return rc;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700622}
623
Dave Jiang8f47d1a2020-01-21 16:44:23 -0700624static void idxd_flush_pending_llist(struct idxd_irq_entry *ie)
625{
626 struct idxd_desc *desc, *itr;
627 struct llist_node *head;
628
629 head = llist_del_all(&ie->pending_llist);
630 if (!head)
631 return;
632
633 llist_for_each_entry_safe(desc, itr, head, llnode) {
634 idxd_dma_complete_txd(desc, IDXD_COMPLETE_ABORT);
635 idxd_free_desc(desc->wq, desc);
636 }
637}
638
639static void idxd_flush_work_list(struct idxd_irq_entry *ie)
640{
641 struct idxd_desc *desc, *iter;
642
643 list_for_each_entry_safe(desc, iter, &ie->work_list, list) {
644 list_del(&desc->list);
645 idxd_dma_complete_txd(desc, IDXD_COMPLETE_ABORT);
646 idxd_free_desc(desc->wq, desc);
647 }
648}
649
Dave Jiang5b0c68c2021-04-20 11:46:51 -0700650void idxd_wqs_quiesce(struct idxd_device *idxd)
651{
652 struct idxd_wq *wq;
653 int i;
654
655 for (i = 0; i < idxd->max_wqs; i++) {
656 wq = idxd->wqs[i];
657 if (wq->state == IDXD_WQ_ENABLED && wq->type == IDXD_WQT_KERNEL)
658 idxd_wq_quiesce(wq);
659 }
660}
661
Dave Jiangeb15e712021-04-20 11:46:34 -0700662static void idxd_release_int_handles(struct idxd_device *idxd)
663{
664 struct device *dev = &idxd->pdev->dev;
665 int i, rc;
666
667 for (i = 0; i < idxd->num_wq_irqs; i++) {
668 if (idxd->hw.cmd_cap & BIT(IDXD_CMD_RELEASE_INT_HANDLE)) {
669 rc = idxd_device_release_int_handle(idxd, idxd->int_handles[i],
670 IDXD_IRQ_MSIX);
671 if (rc < 0)
672 dev_warn(dev, "irq handle %d release failed\n",
673 idxd->int_handles[i]);
674 else
675 dev_dbg(dev, "int handle requested: %u\n", idxd->int_handles[i]);
676 }
677 }
678}
679
Dave Jiangbfe1d562020-01-21 16:43:59 -0700680static void idxd_shutdown(struct pci_dev *pdev)
681{
682 struct idxd_device *idxd = pci_get_drvdata(pdev);
683 int rc, i;
684 struct idxd_irq_entry *irq_entry;
685 int msixcnt = pci_msix_vec_count(pdev);
Dave Jiangbfe1d562020-01-21 16:43:59 -0700686
Dave Jiangbfe1d562020-01-21 16:43:59 -0700687 rc = idxd_device_disable(idxd);
Dave Jiangbfe1d562020-01-21 16:43:59 -0700688 if (rc)
689 dev_err(&pdev->dev, "Disabling device failed\n");
690
691 dev_dbg(&pdev->dev, "%s called\n", __func__);
692 idxd_mask_msix_vectors(idxd);
693 idxd_mask_error_interrupts(idxd);
694
695 for (i = 0; i < msixcnt; i++) {
696 irq_entry = &idxd->irq_entries[i];
Dave Jiang5fc8e852021-04-15 16:37:15 -0700697 synchronize_irq(irq_entry->vector);
698 free_irq(irq_entry->vector, irq_entry);
Dave Jiangbfe1d562020-01-21 16:43:59 -0700699 if (i == 0)
700 continue;
Dave Jiang8f47d1a2020-01-21 16:44:23 -0700701 idxd_flush_pending_llist(irq_entry);
702 idxd_flush_work_list(irq_entry);
Dave Jiangbfe1d562020-01-21 16:43:59 -0700703 }
Dave Jiang0d5c10b2020-06-26 11:11:18 -0700704
Dave Jiang6df0e6c2021-04-12 09:23:27 -0700705 idxd_msix_perm_clear(idxd);
Dave Jiangeb15e712021-04-20 11:46:34 -0700706 idxd_release_int_handles(idxd);
Dave Jiang5fc8e852021-04-15 16:37:15 -0700707 pci_free_irq_vectors(pdev);
Dave Jianga39c7cd2021-04-15 16:37:21 -0700708 pci_iounmap(pdev, idxd->reg_base);
709 pci_disable_device(pdev);
Dave Jiang0d5c10b2020-06-26 11:11:18 -0700710 destroy_workqueue(idxd->wq);
Dave Jiangbfe1d562020-01-21 16:43:59 -0700711}
712
713static void idxd_remove(struct pci_dev *pdev)
714{
715 struct idxd_device *idxd = pci_get_drvdata(pdev);
716
717 dev_dbg(&pdev->dev, "%s called\n", __func__);
718 idxd_shutdown(pdev);
Dave Jiang8e50d392020-10-27 10:34:35 -0700719 if (device_pasid_enabled(idxd))
720 idxd_disable_system_pasid(idxd);
Dave Jiang47c16ac2021-04-15 16:37:33 -0700721 idxd_unregister_devices(idxd);
Dave Jiangcf5f86a2021-04-20 11:46:46 -0700722 iommu_dev_disable_feature(&pdev->dev, IOMMU_DEV_FEAT_SVA);
Dave Jiangbfe1d562020-01-21 16:43:59 -0700723}
724
725static struct pci_driver idxd_pci_driver = {
726 .name = DRV_NAME,
727 .id_table = idxd_pci_tbl,
728 .probe = idxd_pci_probe,
729 .remove = idxd_remove,
730 .shutdown = idxd_shutdown,
731};
732
733static int __init idxd_init_module(void)
734{
Dave Jiang4b73e4e2021-04-15 16:38:03 -0700735 int err;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700736
737 /*
Dave Jiang8e50d392020-10-27 10:34:35 -0700738 * If the CPU does not support MOVDIR64B or ENQCMDS, there's no point in
Dave Jiangbfe1d562020-01-21 16:43:59 -0700739 * enumerating the device. We can not utilize it.
740 */
741 if (!boot_cpu_has(X86_FEATURE_MOVDIR64B)) {
742 pr_warn("idxd driver failed to load without MOVDIR64B.\n");
743 return -ENODEV;
744 }
745
Dave Jiang8e50d392020-10-27 10:34:35 -0700746 if (!boot_cpu_has(X86_FEATURE_ENQCMD))
747 pr_warn("Platform does not have ENQCMD(S) support.\n");
748 else
749 support_enqcmd = true;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700750
Dave Jiangc52ca472020-01-21 16:44:05 -0700751 err = idxd_register_bus_type();
752 if (err < 0)
Dave Jiangbfe1d562020-01-21 16:43:59 -0700753 return err;
754
Dave Jiangc52ca472020-01-21 16:44:05 -0700755 err = idxd_register_driver();
756 if (err < 0)
757 goto err_idxd_driver_register;
758
Dave Jiang42d279f2020-01-21 16:44:29 -0700759 err = idxd_cdev_register();
760 if (err)
761 goto err_cdev_register;
762
Dave Jiangc52ca472020-01-21 16:44:05 -0700763 err = pci_register_driver(&idxd_pci_driver);
764 if (err)
765 goto err_pci_register;
766
Dave Jiangbfe1d562020-01-21 16:43:59 -0700767 return 0;
Dave Jiangc52ca472020-01-21 16:44:05 -0700768
769err_pci_register:
Dave Jiang42d279f2020-01-21 16:44:29 -0700770 idxd_cdev_remove();
771err_cdev_register:
Dave Jiangc52ca472020-01-21 16:44:05 -0700772 idxd_unregister_driver();
773err_idxd_driver_register:
774 idxd_unregister_bus_type();
775 return err;
Dave Jiangbfe1d562020-01-21 16:43:59 -0700776}
777module_init(idxd_init_module);
778
779static void __exit idxd_exit_module(void)
780{
781 pci_unregister_driver(&idxd_pci_driver);
Dave Jiang42d279f2020-01-21 16:44:29 -0700782 idxd_cdev_remove();
Dave Jiangc52ca472020-01-21 16:44:05 -0700783 idxd_unregister_bus_type();
Dave Jiangbfe1d562020-01-21 16:43:59 -0700784}
785module_exit(idxd_exit_module);