1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2019 Intel Corporation. All rights rsvd. */
3 #include <linux/init.h>
4 #include <linux/kernel.h>
5 #include <linux/module.h>
6 #include <linux/pci.h>
7 #include <linux/io-64-nonatomic-lo-hi.h>
8 #include <linux/dmaengine.h>
9 #include <linux/irq.h>
10 #include <uapi/linux/idxd.h>
11 #include "../dmaengine.h"
12 #include "idxd.h"
13 #include "registers.h"
14
15 static void idxd_cmd_exec(struct idxd_device *idxd, int cmd_code, u32 operand,
16 u32 *status);
17 static void idxd_device_wqs_clear_state(struct idxd_device *idxd);
18 static void idxd_wq_disable_cleanup(struct idxd_wq *wq);
19 static int idxd_wq_config_write(struct idxd_wq *wq);
20
21 /* Interrupt control bits */
idxd_unmask_error_interrupts(struct idxd_device * idxd)22 void idxd_unmask_error_interrupts(struct idxd_device *idxd)
23 {
24 union genctrl_reg genctrl;
25
26 genctrl.bits = ioread32(idxd->reg_base + IDXD_GENCTRL_OFFSET);
27 genctrl.softerr_int_en = 1;
28 genctrl.halt_int_en = 1;
29 iowrite32(genctrl.bits, idxd->reg_base + IDXD_GENCTRL_OFFSET);
30 }
31
idxd_mask_error_interrupts(struct idxd_device * idxd)32 void idxd_mask_error_interrupts(struct idxd_device *idxd)
33 {
34 union genctrl_reg genctrl;
35
36 genctrl.bits = ioread32(idxd->reg_base + IDXD_GENCTRL_OFFSET);
37 genctrl.softerr_int_en = 0;
38 genctrl.halt_int_en = 0;
39 iowrite32(genctrl.bits, idxd->reg_base + IDXD_GENCTRL_OFFSET);
40 }
41
free_hw_descs(struct idxd_wq * wq)42 static void free_hw_descs(struct idxd_wq *wq)
43 {
44 int i;
45
46 for (i = 0; i < wq->num_descs; i++)
47 kfree(wq->hw_descs[i]);
48
49 kfree(wq->hw_descs);
50 }
51
alloc_hw_descs(struct idxd_wq * wq,int num)52 static int alloc_hw_descs(struct idxd_wq *wq, int num)
53 {
54 struct device *dev = &wq->idxd->pdev->dev;
55 int i;
56 int node = dev_to_node(dev);
57
58 wq->hw_descs = kcalloc_node(num, sizeof(struct dsa_hw_desc *),
59 GFP_KERNEL, node);
60 if (!wq->hw_descs)
61 return -ENOMEM;
62
63 for (i = 0; i < num; i++) {
64 wq->hw_descs[i] = kzalloc_node(sizeof(*wq->hw_descs[i]),
65 GFP_KERNEL, node);
66 if (!wq->hw_descs[i]) {
67 free_hw_descs(wq);
68 return -ENOMEM;
69 }
70 }
71
72 return 0;
73 }
74
free_descs(struct idxd_wq * wq)75 static void free_descs(struct idxd_wq *wq)
76 {
77 int i;
78
79 for (i = 0; i < wq->num_descs; i++)
80 kfree(wq->descs[i]);
81
82 kfree(wq->descs);
83 }
84
alloc_descs(struct idxd_wq * wq,int num)85 static int alloc_descs(struct idxd_wq *wq, int num)
86 {
87 struct device *dev = &wq->idxd->pdev->dev;
88 int i;
89 int node = dev_to_node(dev);
90
91 wq->descs = kcalloc_node(num, sizeof(struct idxd_desc *),
92 GFP_KERNEL, node);
93 if (!wq->descs)
94 return -ENOMEM;
95
96 for (i = 0; i < num; i++) {
97 wq->descs[i] = kzalloc_node(sizeof(*wq->descs[i]),
98 GFP_KERNEL, node);
99 if (!wq->descs[i]) {
100 free_descs(wq);
101 return -ENOMEM;
102 }
103 }
104
105 return 0;
106 }
107
108 /* WQ control bits */
idxd_wq_alloc_resources(struct idxd_wq * wq)109 int idxd_wq_alloc_resources(struct idxd_wq *wq)
110 {
111 struct idxd_device *idxd = wq->idxd;
112 struct device *dev = &idxd->pdev->dev;
113 int rc, num_descs, i;
114
115 if (wq->type != IDXD_WQT_KERNEL)
116 return 0;
117
118 num_descs = wq_dedicated(wq) ? wq->size : wq->threshold;
119 wq->num_descs = num_descs;
120
121 rc = alloc_hw_descs(wq, num_descs);
122 if (rc < 0)
123 return rc;
124
125 wq->compls_size = num_descs * idxd->data->compl_size;
126 wq->compls = dma_alloc_coherent(dev, wq->compls_size, &wq->compls_addr, GFP_KERNEL);
127 if (!wq->compls) {
128 rc = -ENOMEM;
129 goto fail_alloc_compls;
130 }
131
132 rc = alloc_descs(wq, num_descs);
133 if (rc < 0)
134 goto fail_alloc_descs;
135
136 rc = sbitmap_queue_init_node(&wq->sbq, num_descs, -1, false, GFP_KERNEL,
137 dev_to_node(dev));
138 if (rc < 0)
139 goto fail_sbitmap_init;
140
141 for (i = 0; i < num_descs; i++) {
142 struct idxd_desc *desc = wq->descs[i];
143
144 desc->hw = wq->hw_descs[i];
145 if (idxd->data->type == IDXD_TYPE_DSA)
146 desc->completion = &wq->compls[i];
147 else if (idxd->data->type == IDXD_TYPE_IAX)
148 desc->iax_completion = &wq->iax_compls[i];
149 desc->compl_dma = wq->compls_addr + idxd->data->compl_size * i;
150 desc->id = i;
151 desc->wq = wq;
152 desc->cpu = -1;
153 }
154
155 return 0;
156
157 fail_sbitmap_init:
158 free_descs(wq);
159 fail_alloc_descs:
160 dma_free_coherent(dev, wq->compls_size, wq->compls, wq->compls_addr);
161 fail_alloc_compls:
162 free_hw_descs(wq);
163 return rc;
164 }
165 EXPORT_SYMBOL_NS_GPL(idxd_wq_alloc_resources, "IDXD");
166
idxd_wq_free_resources(struct idxd_wq * wq)167 void idxd_wq_free_resources(struct idxd_wq *wq)
168 {
169 struct device *dev = &wq->idxd->pdev->dev;
170
171 if (wq->type != IDXD_WQT_KERNEL)
172 return;
173
174 free_hw_descs(wq);
175 free_descs(wq);
176 dma_free_coherent(dev, wq->compls_size, wq->compls, wq->compls_addr);
177 sbitmap_queue_free(&wq->sbq);
178 wq->type = IDXD_WQT_NONE;
179 }
180 EXPORT_SYMBOL_NS_GPL(idxd_wq_free_resources, "IDXD");
181
idxd_wq_enable(struct idxd_wq * wq)182 int idxd_wq_enable(struct idxd_wq *wq)
183 {
184 struct idxd_device *idxd = wq->idxd;
185 struct device *dev = &idxd->pdev->dev;
186 u32 status;
187
188 if (wq->state == IDXD_WQ_ENABLED) {
189 dev_dbg(dev, "WQ %d already enabled\n", wq->id);
190 return 0;
191 }
192
193 idxd_cmd_exec(idxd, IDXD_CMD_ENABLE_WQ, wq->id, &status);
194
195 if (status != IDXD_CMDSTS_SUCCESS &&
196 status != IDXD_CMDSTS_ERR_WQ_ENABLED) {
197 dev_dbg(dev, "WQ enable failed: %#x\n", status);
198 return -ENXIO;
199 }
200
201 wq->state = IDXD_WQ_ENABLED;
202 set_bit(wq->id, idxd->wq_enable_map);
203 dev_dbg(dev, "WQ %d enabled\n", wq->id);
204 return 0;
205 }
206
idxd_wq_disable(struct idxd_wq * wq,bool reset_config)207 int idxd_wq_disable(struct idxd_wq *wq, bool reset_config)
208 {
209 struct idxd_device *idxd = wq->idxd;
210 struct device *dev = &idxd->pdev->dev;
211 u32 status, operand;
212
213 dev_dbg(dev, "Disabling WQ %d\n", wq->id);
214
215 if (wq->state != IDXD_WQ_ENABLED) {
216 dev_dbg(dev, "WQ %d in wrong state: %d\n", wq->id, wq->state);
217 return 0;
218 }
219
220 /*
221 * Disable WQ does not drain address translations, if WQ attributes are
222 * changed before translations are drained, pending translations can
223 * be issued using updated WQ attibutes, resulting in invalid
224 * translations being cached in the device translation cache.
225 *
226 * To make sure pending translations are drained before WQ
227 * attributes are changed, we use a WQ Drain followed by WQ Reset and
228 * then restore the WQ configuration.
229 */
230 idxd_wq_drain(wq);
231
232 operand = BIT(wq->id % 16) | ((wq->id / 16) << 16);
233 idxd_cmd_exec(idxd, IDXD_CMD_RESET_WQ, operand, &status);
234
235 if (status != IDXD_CMDSTS_SUCCESS) {
236 dev_dbg(dev, "WQ reset failed: %#x\n", status);
237 return -ENXIO;
238 }
239
240 idxd_wq_config_write(wq);
241
242 if (reset_config)
243 idxd_wq_disable_cleanup(wq);
244 clear_bit(wq->id, idxd->wq_enable_map);
245 wq->state = IDXD_WQ_DISABLED;
246 dev_dbg(dev, "WQ %d disabled\n", wq->id);
247 return 0;
248 }
249
idxd_wq_drain(struct idxd_wq * wq)250 void idxd_wq_drain(struct idxd_wq *wq)
251 {
252 struct idxd_device *idxd = wq->idxd;
253 struct device *dev = &idxd->pdev->dev;
254 u32 operand;
255
256 if (wq->state != IDXD_WQ_ENABLED) {
257 dev_dbg(dev, "WQ %d in wrong state: %d\n", wq->id, wq->state);
258 return;
259 }
260
261 dev_dbg(dev, "Draining WQ %d\n", wq->id);
262 operand = BIT(wq->id % 16) | ((wq->id / 16) << 16);
263 idxd_cmd_exec(idxd, IDXD_CMD_DRAIN_WQ, operand, NULL);
264 }
265
idxd_wq_reset(struct idxd_wq * wq)266 void idxd_wq_reset(struct idxd_wq *wq)
267 {
268 struct idxd_device *idxd = wq->idxd;
269 struct device *dev = &idxd->pdev->dev;
270 u32 operand;
271
272 if (wq->state != IDXD_WQ_ENABLED) {
273 dev_dbg(dev, "WQ %d in wrong state: %d\n", wq->id, wq->state);
274 return;
275 }
276
277 operand = BIT(wq->id % 16) | ((wq->id / 16) << 16);
278 idxd_cmd_exec(idxd, IDXD_CMD_RESET_WQ, operand, NULL);
279 idxd_wq_disable_cleanup(wq);
280 }
281
idxd_wq_map_portal(struct idxd_wq * wq)282 int idxd_wq_map_portal(struct idxd_wq *wq)
283 {
284 struct idxd_device *idxd = wq->idxd;
285 struct pci_dev *pdev = idxd->pdev;
286 struct device *dev = &pdev->dev;
287 resource_size_t start;
288
289 start = pci_resource_start(pdev, IDXD_WQ_BAR);
290 start += idxd_get_wq_portal_full_offset(wq->id, IDXD_PORTAL_LIMITED);
291
292 wq->portal = devm_ioremap(dev, start, IDXD_PORTAL_SIZE);
293 if (!wq->portal)
294 return -ENOMEM;
295
296 return 0;
297 }
298
idxd_wq_unmap_portal(struct idxd_wq * wq)299 void idxd_wq_unmap_portal(struct idxd_wq *wq)
300 {
301 struct device *dev = &wq->idxd->pdev->dev;
302
303 devm_iounmap(dev, wq->portal);
304 wq->portal = NULL;
305 wq->portal_offset = 0;
306 }
307
idxd_wqs_unmap_portal(struct idxd_device * idxd)308 void idxd_wqs_unmap_portal(struct idxd_device *idxd)
309 {
310 int i;
311
312 for (i = 0; i < idxd->max_wqs; i++) {
313 struct idxd_wq *wq = idxd->wqs[i];
314
315 if (wq->portal)
316 idxd_wq_unmap_portal(wq);
317 }
318 }
319
__idxd_wq_set_pasid_locked(struct idxd_wq * wq,int pasid)320 static void __idxd_wq_set_pasid_locked(struct idxd_wq *wq, int pasid)
321 {
322 struct idxd_device *idxd = wq->idxd;
323 union wqcfg wqcfg;
324 unsigned int offset;
325
326 offset = WQCFG_OFFSET(idxd, wq->id, WQCFG_PASID_IDX);
327 spin_lock(&idxd->dev_lock);
328 wqcfg.bits[WQCFG_PASID_IDX] = ioread32(idxd->reg_base + offset);
329 wqcfg.pasid_en = 1;
330 wqcfg.pasid = pasid;
331 wq->wqcfg->bits[WQCFG_PASID_IDX] = wqcfg.bits[WQCFG_PASID_IDX];
332 iowrite32(wqcfg.bits[WQCFG_PASID_IDX], idxd->reg_base + offset);
333 spin_unlock(&idxd->dev_lock);
334 }
335
idxd_wq_set_pasid(struct idxd_wq * wq,int pasid)336 int idxd_wq_set_pasid(struct idxd_wq *wq, int pasid)
337 {
338 int rc;
339
340 rc = idxd_wq_disable(wq, false);
341 if (rc < 0)
342 return rc;
343
344 __idxd_wq_set_pasid_locked(wq, pasid);
345
346 rc = idxd_wq_enable(wq);
347 if (rc < 0)
348 return rc;
349
350 return 0;
351 }
352
idxd_wq_disable_pasid(struct idxd_wq * wq)353 int idxd_wq_disable_pasid(struct idxd_wq *wq)
354 {
355 struct idxd_device *idxd = wq->idxd;
356 int rc;
357 union wqcfg wqcfg;
358 unsigned int offset;
359
360 rc = idxd_wq_disable(wq, false);
361 if (rc < 0)
362 return rc;
363
364 offset = WQCFG_OFFSET(idxd, wq->id, WQCFG_PASID_IDX);
365 spin_lock(&idxd->dev_lock);
366 wqcfg.bits[WQCFG_PASID_IDX] = ioread32(idxd->reg_base + offset);
367 wqcfg.pasid_en = 0;
368 wqcfg.pasid = 0;
369 iowrite32(wqcfg.bits[WQCFG_PASID_IDX], idxd->reg_base + offset);
370 spin_unlock(&idxd->dev_lock);
371
372 rc = idxd_wq_enable(wq);
373 if (rc < 0)
374 return rc;
375
376 return 0;
377 }
378
idxd_wq_disable_cleanup(struct idxd_wq * wq)379 static void idxd_wq_disable_cleanup(struct idxd_wq *wq)
380 {
381 struct idxd_device *idxd = wq->idxd;
382
383 lockdep_assert_held(&wq->wq_lock);
384 wq->state = IDXD_WQ_DISABLED;
385 memset(wq->wqcfg, 0, idxd->wqcfg_size);
386 wq->threshold = 0;
387 wq->priority = 0;
388 wq->enqcmds_retries = IDXD_ENQCMDS_RETRIES;
389 wq->flags = 0;
390 memset(wq->name, 0, WQ_NAME_SIZE);
391 wq->max_xfer_bytes = WQ_DEFAULT_MAX_XFER;
392 idxd_wq_set_max_batch_size(idxd->data->type, wq, WQ_DEFAULT_MAX_BATCH);
393 idxd_wq_set_init_max_sgl_size(idxd, wq);
394 if (wq->opcap_bmap)
395 bitmap_copy(wq->opcap_bmap, idxd->opcap_bmap, IDXD_MAX_OPCAP_BITS);
396 }
397
idxd_wq_device_reset_cleanup(struct idxd_wq * wq)398 static void idxd_wq_device_reset_cleanup(struct idxd_wq *wq)
399 {
400 lockdep_assert_held(&wq->wq_lock);
401
402 wq->size = 0;
403 wq->group = NULL;
404 }
405
idxd_wq_ref_release(struct percpu_ref * ref)406 static void idxd_wq_ref_release(struct percpu_ref *ref)
407 {
408 struct idxd_wq *wq = container_of(ref, struct idxd_wq, wq_active);
409
410 complete(&wq->wq_dead);
411 }
412
idxd_wq_init_percpu_ref(struct idxd_wq * wq)413 int idxd_wq_init_percpu_ref(struct idxd_wq *wq)
414 {
415 int rc;
416
417 memset(&wq->wq_active, 0, sizeof(wq->wq_active));
418 rc = percpu_ref_init(&wq->wq_active, idxd_wq_ref_release,
419 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL);
420 if (rc < 0)
421 return rc;
422 reinit_completion(&wq->wq_dead);
423 reinit_completion(&wq->wq_resurrect);
424 return 0;
425 }
426 EXPORT_SYMBOL_NS_GPL(idxd_wq_init_percpu_ref, "IDXD");
427
__idxd_wq_quiesce(struct idxd_wq * wq)428 void __idxd_wq_quiesce(struct idxd_wq *wq)
429 {
430 lockdep_assert_held(&wq->wq_lock);
431 reinit_completion(&wq->wq_resurrect);
432 percpu_ref_kill(&wq->wq_active);
433 complete_all(&wq->wq_resurrect);
434 wait_for_completion(&wq->wq_dead);
435 }
436 EXPORT_SYMBOL_NS_GPL(__idxd_wq_quiesce, "IDXD");
437
idxd_wq_quiesce(struct idxd_wq * wq)438 void idxd_wq_quiesce(struct idxd_wq *wq)
439 {
440 mutex_lock(&wq->wq_lock);
441 __idxd_wq_quiesce(wq);
442 mutex_unlock(&wq->wq_lock);
443 }
444 EXPORT_SYMBOL_NS_GPL(idxd_wq_quiesce, "IDXD");
445
446 /* Device control bits */
idxd_is_enabled(struct idxd_device * idxd)447 static inline bool idxd_is_enabled(struct idxd_device *idxd)
448 {
449 union gensts_reg gensts;
450
451 gensts.bits = ioread32(idxd->reg_base + IDXD_GENSTATS_OFFSET);
452
453 if (gensts.state == IDXD_DEVICE_STATE_ENABLED)
454 return true;
455 return false;
456 }
457
idxd_device_is_halted(struct idxd_device * idxd)458 static inline bool idxd_device_is_halted(struct idxd_device *idxd)
459 {
460 union gensts_reg gensts;
461
462 gensts.bits = ioread32(idxd->reg_base + IDXD_GENSTATS_OFFSET);
463
464 return (gensts.state == IDXD_DEVICE_STATE_HALT);
465 }
466
467 /*
468 * This is function is only used for reset during probe and will
469 * poll for completion. Once the device is setup with interrupts,
470 * all commands will be done via interrupt completion.
471 */
idxd_device_init_reset(struct idxd_device * idxd)472 int idxd_device_init_reset(struct idxd_device *idxd)
473 {
474 struct device *dev = &idxd->pdev->dev;
475 union idxd_command_reg cmd;
476
477 if (idxd_device_is_halted(idxd)) {
478 dev_warn(&idxd->pdev->dev, "Device is HALTED!\n");
479 return -ENXIO;
480 }
481
482 memset(&cmd, 0, sizeof(cmd));
483 cmd.cmd = IDXD_CMD_RESET_DEVICE;
484 dev_dbg(dev, "%s: sending reset for init.\n", __func__);
485 spin_lock(&idxd->cmd_lock);
486 iowrite32(cmd.bits, idxd->reg_base + IDXD_CMD_OFFSET);
487
488 while (ioread32(idxd->reg_base + IDXD_CMDSTS_OFFSET) &
489 IDXD_CMDSTS_ACTIVE)
490 cpu_relax();
491 spin_unlock(&idxd->cmd_lock);
492 return 0;
493 }
494
idxd_cmd_exec(struct idxd_device * idxd,int cmd_code,u32 operand,u32 * status)495 static void idxd_cmd_exec(struct idxd_device *idxd, int cmd_code, u32 operand,
496 u32 *status)
497 {
498 union idxd_command_reg cmd;
499 DECLARE_COMPLETION_ONSTACK(done);
500 u32 stat;
501 unsigned long flags;
502
503 if (idxd_device_is_halted(idxd)) {
504 dev_warn(&idxd->pdev->dev, "Device is HALTED!\n");
505 if (status)
506 *status = IDXD_CMDSTS_HW_ERR;
507 return;
508 }
509
510 memset(&cmd, 0, sizeof(cmd));
511 cmd.cmd = cmd_code;
512 cmd.operand = operand;
513 cmd.int_req = 1;
514
515 spin_lock_irqsave(&idxd->cmd_lock, flags);
516 wait_event_lock_irq(idxd->cmd_waitq,
517 !test_bit(IDXD_FLAG_CMD_RUNNING, &idxd->flags),
518 idxd->cmd_lock);
519
520 dev_dbg(&idxd->pdev->dev, "%s: sending cmd: %#x op: %#x\n",
521 __func__, cmd_code, operand);
522
523 idxd->cmd_status = 0;
524 __set_bit(IDXD_FLAG_CMD_RUNNING, &idxd->flags);
525 idxd->cmd_done = &done;
526 iowrite32(cmd.bits, idxd->reg_base + IDXD_CMD_OFFSET);
527
528 /*
529 * After command submitted, release lock and go to sleep until
530 * the command completes via interrupt.
531 */
532 spin_unlock_irqrestore(&idxd->cmd_lock, flags);
533 wait_for_completion(&done);
534 stat = ioread32(idxd->reg_base + IDXD_CMDSTS_OFFSET);
535 spin_lock(&idxd->cmd_lock);
536 if (status)
537 *status = stat;
538 idxd->cmd_status = stat & GENMASK(7, 0);
539
540 __clear_bit(IDXD_FLAG_CMD_RUNNING, &idxd->flags);
541 /* Wake up other pending commands */
542 wake_up(&idxd->cmd_waitq);
543 spin_unlock(&idxd->cmd_lock);
544 }
545
idxd_device_enable(struct idxd_device * idxd)546 int idxd_device_enable(struct idxd_device *idxd)
547 {
548 struct device *dev = &idxd->pdev->dev;
549 u32 status;
550
551 if (idxd_is_enabled(idxd)) {
552 dev_dbg(dev, "Device already enabled\n");
553 return -ENXIO;
554 }
555
556 idxd_cmd_exec(idxd, IDXD_CMD_ENABLE_DEVICE, 0, &status);
557
558 /* If the command is successful or if the device was enabled */
559 if (status != IDXD_CMDSTS_SUCCESS &&
560 status != IDXD_CMDSTS_ERR_DEV_ENABLED) {
561 dev_dbg(dev, "%s: err_code: %#x\n", __func__, status);
562 return -ENXIO;
563 }
564
565 idxd->state = IDXD_DEV_ENABLED;
566 return 0;
567 }
568
idxd_device_disable(struct idxd_device * idxd)569 int idxd_device_disable(struct idxd_device *idxd)
570 {
571 struct device *dev = &idxd->pdev->dev;
572 u32 status;
573
574 if (!idxd_is_enabled(idxd)) {
575 dev_dbg(dev, "Device is not enabled\n");
576 return 0;
577 }
578
579 idxd_cmd_exec(idxd, IDXD_CMD_DISABLE_DEVICE, 0, &status);
580
581 /* If the command is successful or if the device was disabled */
582 if (status != IDXD_CMDSTS_SUCCESS &&
583 !(status & IDXD_CMDSTS_ERR_DIS_DEV_EN)) {
584 dev_dbg(dev, "%s: err_code: %#x\n", __func__, status);
585 return -ENXIO;
586 }
587
588 idxd_device_clear_state(idxd);
589 return 0;
590 }
591
idxd_device_reset(struct idxd_device * idxd)592 void idxd_device_reset(struct idxd_device *idxd)
593 {
594 idxd_cmd_exec(idxd, IDXD_CMD_RESET_DEVICE, 0, NULL);
595 idxd_device_clear_state(idxd);
596 spin_lock(&idxd->dev_lock);
597 idxd_unmask_error_interrupts(idxd);
598 spin_unlock(&idxd->dev_lock);
599 }
600
idxd_device_drain_pasid(struct idxd_device * idxd,int pasid)601 void idxd_device_drain_pasid(struct idxd_device *idxd, int pasid)
602 {
603 struct device *dev = &idxd->pdev->dev;
604 u32 operand;
605
606 operand = pasid;
607 dev_dbg(dev, "cmd: %u operand: %#x\n", IDXD_CMD_DRAIN_PASID, operand);
608 idxd_cmd_exec(idxd, IDXD_CMD_DRAIN_PASID, operand, NULL);
609 dev_dbg(dev, "pasid %d drained\n", pasid);
610 }
611
idxd_device_request_int_handle(struct idxd_device * idxd,int idx,int * handle,enum idxd_interrupt_type irq_type)612 int idxd_device_request_int_handle(struct idxd_device *idxd, int idx, int *handle,
613 enum idxd_interrupt_type irq_type)
614 {
615 struct device *dev = &idxd->pdev->dev;
616 u32 operand, status;
617
618 if (!(idxd->hw.cmd_cap & BIT(IDXD_CMD_REQUEST_INT_HANDLE)))
619 return -EOPNOTSUPP;
620
621 dev_dbg(dev, "get int handle, idx %d\n", idx);
622
623 operand = idx & GENMASK(15, 0);
624 if (irq_type == IDXD_IRQ_IMS)
625 operand |= CMD_INT_HANDLE_IMS;
626
627 dev_dbg(dev, "cmd: %u operand: %#x\n", IDXD_CMD_REQUEST_INT_HANDLE, operand);
628
629 idxd_cmd_exec(idxd, IDXD_CMD_REQUEST_INT_HANDLE, operand, &status);
630
631 if ((status & IDXD_CMDSTS_ERR_MASK) != IDXD_CMDSTS_SUCCESS) {
632 dev_dbg(dev, "request int handle failed: %#x\n", status);
633 return -ENXIO;
634 }
635
636 *handle = (status >> IDXD_CMDSTS_RES_SHIFT) & GENMASK(15, 0);
637
638 dev_dbg(dev, "int handle acquired: %u\n", *handle);
639 return 0;
640 }
641
idxd_device_release_int_handle(struct idxd_device * idxd,int handle,enum idxd_interrupt_type irq_type)642 int idxd_device_release_int_handle(struct idxd_device *idxd, int handle,
643 enum idxd_interrupt_type irq_type)
644 {
645 struct device *dev = &idxd->pdev->dev;
646 u32 operand, status;
647 union idxd_command_reg cmd;
648
649 if (!(idxd->hw.cmd_cap & BIT(IDXD_CMD_RELEASE_INT_HANDLE)))
650 return -EOPNOTSUPP;
651
652 dev_dbg(dev, "release int handle, handle %d\n", handle);
653
654 memset(&cmd, 0, sizeof(cmd));
655 operand = handle & GENMASK(15, 0);
656
657 if (irq_type == IDXD_IRQ_IMS)
658 operand |= CMD_INT_HANDLE_IMS;
659
660 cmd.cmd = IDXD_CMD_RELEASE_INT_HANDLE;
661 cmd.operand = operand;
662
663 dev_dbg(dev, "cmd: %u operand: %#x\n", IDXD_CMD_RELEASE_INT_HANDLE, operand);
664
665 spin_lock(&idxd->cmd_lock);
666 iowrite32(cmd.bits, idxd->reg_base + IDXD_CMD_OFFSET);
667
668 while (ioread32(idxd->reg_base + IDXD_CMDSTS_OFFSET) & IDXD_CMDSTS_ACTIVE)
669 cpu_relax();
670 status = ioread32(idxd->reg_base + IDXD_CMDSTS_OFFSET);
671 spin_unlock(&idxd->cmd_lock);
672
673 if ((status & IDXD_CMDSTS_ERR_MASK) != IDXD_CMDSTS_SUCCESS) {
674 dev_dbg(dev, "release int handle failed: %#x\n", status);
675 return -ENXIO;
676 }
677
678 dev_dbg(dev, "int handle released.\n");
679 return 0;
680 }
681
682 /* Device configuration bits */
idxd_engines_clear_state(struct idxd_device * idxd)683 static void idxd_engines_clear_state(struct idxd_device *idxd)
684 {
685 struct idxd_engine *engine;
686 int i;
687
688 lockdep_assert_held(&idxd->dev_lock);
689 for (i = 0; i < idxd->max_engines; i++) {
690 engine = idxd->engines[i];
691 engine->group = NULL;
692 }
693 }
694
idxd_groups_clear_state(struct idxd_device * idxd)695 static void idxd_groups_clear_state(struct idxd_device *idxd)
696 {
697 struct idxd_group *group;
698 int i;
699
700 lockdep_assert_held(&idxd->dev_lock);
701 for (i = 0; i < idxd->max_groups; i++) {
702 group = idxd->groups[i];
703 memset(&group->grpcfg, 0, sizeof(group->grpcfg));
704 group->num_engines = 0;
705 group->num_wqs = 0;
706 group->use_rdbuf_limit = false;
707 /*
708 * The default value is the same as the value of
709 * total read buffers in GRPCAP.
710 */
711 group->rdbufs_allowed = idxd->max_rdbufs;
712 group->rdbufs_reserved = 0;
713 if (idxd->hw.version <= DEVICE_VERSION_2 && !tc_override) {
714 group->tc_a = 1;
715 group->tc_b = 1;
716 } else {
717 group->tc_a = -1;
718 group->tc_b = -1;
719 }
720 group->desc_progress_limit = 0;
721 group->batch_progress_limit = 0;
722 }
723 }
724
idxd_device_wqs_clear_state(struct idxd_device * idxd)725 static void idxd_device_wqs_clear_state(struct idxd_device *idxd)
726 {
727 int i;
728
729 for (i = 0; i < idxd->max_wqs; i++) {
730 struct idxd_wq *wq = idxd->wqs[i];
731
732 mutex_lock(&wq->wq_lock);
733 idxd_wq_disable_cleanup(wq);
734 idxd_wq_device_reset_cleanup(wq);
735 mutex_unlock(&wq->wq_lock);
736 }
737 }
738
idxd_device_clear_state(struct idxd_device * idxd)739 void idxd_device_clear_state(struct idxd_device *idxd)
740 {
741 /* IDXD is always disabled. Other states are cleared only when IDXD is configurable. */
742 if (test_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags)) {
743 /*
744 * Clearing wq state is protected by wq lock.
745 * So no need to be protected by device lock.
746 */
747 idxd_device_wqs_clear_state(idxd);
748
749 spin_lock(&idxd->dev_lock);
750 idxd_groups_clear_state(idxd);
751 idxd_engines_clear_state(idxd);
752 } else {
753 spin_lock(&idxd->dev_lock);
754 }
755
756 idxd->state = IDXD_DEV_DISABLED;
757 spin_unlock(&idxd->dev_lock);
758 }
759
idxd_device_evl_setup(struct idxd_device * idxd)760 static int idxd_device_evl_setup(struct idxd_device *idxd)
761 {
762 union gencfg_reg gencfg;
763 union evlcfg_reg evlcfg;
764 union genctrl_reg genctrl;
765 struct device *dev = &idxd->pdev->dev;
766 void *addr;
767 dma_addr_t dma_addr;
768 int size;
769 struct idxd_evl *evl = idxd->evl;
770 unsigned long *bmap;
771 int rc;
772
773 if (!evl)
774 return 0;
775
776 size = evl_size(idxd);
777
778 bmap = bitmap_zalloc(size, GFP_KERNEL);
779 if (!bmap) {
780 rc = -ENOMEM;
781 goto err_bmap;
782 }
783
784 /*
785 * Address needs to be page aligned. However, dma_alloc_coherent() provides
786 * at minimal page size aligned address. No manual alignment required.
787 */
788 addr = dma_alloc_coherent(dev, size, &dma_addr, GFP_KERNEL);
789 if (!addr) {
790 rc = -ENOMEM;
791 goto err_alloc;
792 }
793
794 mutex_lock(&evl->lock);
795 evl->log = addr;
796 evl->dma = dma_addr;
797 evl->log_size = size;
798 evl->bmap = bmap;
799
800 memset(&evlcfg, 0, sizeof(evlcfg));
801 evlcfg.bits[0] = dma_addr & GENMASK(63, 12);
802 evlcfg.size = evl->size;
803
804 iowrite64(evlcfg.bits[0], idxd->reg_base + IDXD_EVLCFG_OFFSET);
805 iowrite64(evlcfg.bits[1], idxd->reg_base + IDXD_EVLCFG_OFFSET + 8);
806
807 genctrl.bits = ioread32(idxd->reg_base + IDXD_GENCTRL_OFFSET);
808 genctrl.evl_int_en = 1;
809 iowrite32(genctrl.bits, idxd->reg_base + IDXD_GENCTRL_OFFSET);
810
811 gencfg.bits = ioread32(idxd->reg_base + IDXD_GENCFG_OFFSET);
812 gencfg.evl_en = 1;
813 iowrite32(gencfg.bits, idxd->reg_base + IDXD_GENCFG_OFFSET);
814
815 mutex_unlock(&evl->lock);
816 return 0;
817
818 err_alloc:
819 bitmap_free(bmap);
820 err_bmap:
821 return rc;
822 }
823
idxd_device_evl_free(struct idxd_device * idxd)824 static void idxd_device_evl_free(struct idxd_device *idxd)
825 {
826 void *evl_log;
827 unsigned int evl_log_size;
828 dma_addr_t evl_dma;
829 union gencfg_reg gencfg;
830 union genctrl_reg genctrl;
831 struct device *dev = &idxd->pdev->dev;
832 struct idxd_evl *evl = idxd->evl;
833
834 if (!evl)
835 return;
836
837 mutex_lock(&evl->lock);
838 gencfg.evl_en = 0;
839 iowrite32(gencfg.bits, idxd->reg_base + IDXD_GENCFG_OFFSET);
840
841 genctrl.bits = ioread32(idxd->reg_base + IDXD_GENCTRL_OFFSET);
842 genctrl.evl_int_en = 0;
843 iowrite32(genctrl.bits, idxd->reg_base + IDXD_GENCTRL_OFFSET);
844
845 iowrite64(0, idxd->reg_base + IDXD_EVLCFG_OFFSET);
846 iowrite64(0, idxd->reg_base + IDXD_EVLCFG_OFFSET + 8);
847
848 bitmap_free(evl->bmap);
849 evl_log = evl->log;
850 evl_log_size = evl->log_size;
851 evl_dma = evl->dma;
852 evl->log = NULL;
853 evl->size = IDXD_EVL_SIZE_MIN;
854 mutex_unlock(&evl->lock);
855
856 dma_free_coherent(dev, evl_log_size, evl_log, evl_dma);
857 }
858
idxd_group_config_write(struct idxd_group * group)859 static void idxd_group_config_write(struct idxd_group *group)
860 {
861 struct idxd_device *idxd = group->idxd;
862 struct device *dev = &idxd->pdev->dev;
863 int i;
864 u32 grpcfg_offset;
865
866 dev_dbg(dev, "Writing group %d cfg registers\n", group->id);
867
868 /* setup GRPWQCFG */
869 for (i = 0; i < GRPWQCFG_STRIDES; i++) {
870 grpcfg_offset = GRPWQCFG_OFFSET(idxd, group->id, i);
871 iowrite64(group->grpcfg.wqs[i], idxd->reg_base + grpcfg_offset);
872 dev_dbg(dev, "GRPCFG wq[%d:%d: %#x]: %#llx\n",
873 group->id, i, grpcfg_offset,
874 ioread64(idxd->reg_base + grpcfg_offset));
875 }
876
877 /* setup GRPENGCFG */
878 grpcfg_offset = GRPENGCFG_OFFSET(idxd, group->id);
879 iowrite64(group->grpcfg.engines, idxd->reg_base + grpcfg_offset);
880 dev_dbg(dev, "GRPCFG engs[%d: %#x]: %#llx\n", group->id,
881 grpcfg_offset, ioread64(idxd->reg_base + grpcfg_offset));
882
883 /* setup GRPFLAGS */
884 grpcfg_offset = GRPFLGCFG_OFFSET(idxd, group->id);
885 iowrite64(group->grpcfg.flags.bits, idxd->reg_base + grpcfg_offset);
886 dev_dbg(dev, "GRPFLAGS flags[%d: %#x]: %#llx\n",
887 group->id, grpcfg_offset,
888 ioread64(idxd->reg_base + grpcfg_offset));
889 }
890
idxd_groups_config_write(struct idxd_device * idxd)891 static int idxd_groups_config_write(struct idxd_device *idxd)
892
893 {
894 union gencfg_reg reg;
895 int i;
896 struct device *dev = &idxd->pdev->dev;
897
898 /* Setup bandwidth rdbuf limit */
899 if (idxd->hw.gen_cap.config_en && idxd->rdbuf_limit) {
900 reg.bits = ioread32(idxd->reg_base + IDXD_GENCFG_OFFSET);
901 reg.rdbuf_limit = idxd->rdbuf_limit;
902 iowrite32(reg.bits, idxd->reg_base + IDXD_GENCFG_OFFSET);
903 }
904
905 dev_dbg(dev, "GENCFG(%#x): %#x\n", IDXD_GENCFG_OFFSET,
906 ioread32(idxd->reg_base + IDXD_GENCFG_OFFSET));
907
908 for (i = 0; i < idxd->max_groups; i++) {
909 struct idxd_group *group = idxd->groups[i];
910
911 idxd_group_config_write(group);
912 }
913
914 return 0;
915 }
916
idxd_device_pasid_priv_enabled(struct idxd_device * idxd)917 static bool idxd_device_pasid_priv_enabled(struct idxd_device *idxd)
918 {
919 struct pci_dev *pdev = idxd->pdev;
920
921 if (pdev->pasid_enabled && (pdev->pasid_features & PCI_PASID_CAP_PRIV))
922 return true;
923 return false;
924 }
925
idxd_wq_config_write(struct idxd_wq * wq)926 static int idxd_wq_config_write(struct idxd_wq *wq)
927 {
928 struct idxd_device *idxd = wq->idxd;
929 struct device *dev = &idxd->pdev->dev;
930 u32 wq_offset;
931 int i, n;
932
933 if (!wq->group)
934 return 0;
935
936 /*
937 * Instead of memset the entire shadow copy of WQCFG, copy from the hardware after
938 * wq reset. This will copy back the sticky values that are present on some devices.
939 */
940 for (i = 0; i < WQCFG_STRIDES(idxd); i++) {
941 wq_offset = WQCFG_OFFSET(idxd, wq->id, i);
942 wq->wqcfg->bits[i] |= ioread32(idxd->reg_base + wq_offset);
943 }
944
945 if (wq->size == 0 && wq->type != IDXD_WQT_NONE)
946 wq->size = WQ_DEFAULT_QUEUE_DEPTH;
947
948 /* byte 0-3 */
949 wq->wqcfg->wq_size = wq->size;
950
951 /* bytes 4-7 */
952 wq->wqcfg->wq_thresh = wq->threshold;
953
954 /* byte 8-11 */
955 if (wq_dedicated(wq))
956 wq->wqcfg->mode = 1;
957
958 /*
959 * The WQ priv bit is set depending on the WQ type. priv = 1 if the
960 * WQ type is kernel to indicate privileged access. This setting only
961 * matters for dedicated WQ. According to the DSA spec:
962 * If the WQ is in dedicated mode, WQ PASID Enable is 1, and the
963 * Privileged Mode Enable field of the PCI Express PASID capability
964 * is 0, this field must be 0.
965 *
966 * In the case of a dedicated kernel WQ that is not able to support
967 * the PASID cap, then the configuration will be rejected.
968 */
969 if (wq_dedicated(wq) && wq->wqcfg->pasid_en &&
970 !idxd_device_pasid_priv_enabled(idxd) &&
971 wq->type == IDXD_WQT_KERNEL) {
972 idxd->cmd_status = IDXD_SCMD_WQ_NO_PRIV;
973 return -EOPNOTSUPP;
974 }
975
976 wq->wqcfg->priority = wq->priority;
977
978 if (idxd->hw.gen_cap.block_on_fault &&
979 test_bit(WQ_FLAG_BLOCK_ON_FAULT, &wq->flags) &&
980 !test_bit(WQ_FLAG_PRS_DISABLE, &wq->flags))
981 wq->wqcfg->bof = 1;
982
983 if (idxd->hw.wq_cap.wq_ats_support)
984 wq->wqcfg->wq_ats_disable = test_bit(WQ_FLAG_ATS_DISABLE, &wq->flags);
985
986 if (idxd->hw.wq_cap.wq_prs_support)
987 wq->wqcfg->wq_prs_disable = test_bit(WQ_FLAG_PRS_DISABLE, &wq->flags);
988
989 /* bytes 12-15 */
990 wq->wqcfg->max_xfer_shift = ilog2(wq->max_xfer_bytes);
991 idxd_wqcfg_set_max_batch_shift(idxd->data->type, wq->wqcfg, ilog2(wq->max_batch_size));
992 if (idxd_sgl_supported(idxd))
993 wq->wqcfg->max_sgl_shift = ilog2(wq->max_sgl_size);
994
995 /* bytes 32-63 */
996 if (idxd->hw.wq_cap.op_config && wq->opcap_bmap) {
997 memset(wq->wqcfg->op_config, 0, IDXD_MAX_OPCAP_BITS / 8);
998 for_each_set_bit(n, wq->opcap_bmap, IDXD_MAX_OPCAP_BITS) {
999 int pos = n % BITS_PER_LONG_LONG;
1000 int idx = n / BITS_PER_LONG_LONG;
1001
1002 wq->wqcfg->op_config[idx] |= BIT(pos);
1003 }
1004 }
1005
1006 dev_dbg(dev, "WQ %d CFGs\n", wq->id);
1007 for (i = 0; i < WQCFG_STRIDES(idxd); i++) {
1008 wq_offset = WQCFG_OFFSET(idxd, wq->id, i);
1009 iowrite32(wq->wqcfg->bits[i], idxd->reg_base + wq_offset);
1010 dev_dbg(dev, "WQ[%d][%d][%#x]: %#x\n",
1011 wq->id, i, wq_offset,
1012 ioread32(idxd->reg_base + wq_offset));
1013 }
1014
1015 return 0;
1016 }
1017
idxd_wqs_config_write(struct idxd_device * idxd)1018 static int idxd_wqs_config_write(struct idxd_device *idxd)
1019 {
1020 int i, rc;
1021
1022 for (i = 0; i < idxd->max_wqs; i++) {
1023 struct idxd_wq *wq = idxd->wqs[i];
1024
1025 rc = idxd_wq_config_write(wq);
1026 if (rc < 0)
1027 return rc;
1028 }
1029
1030 return 0;
1031 }
1032
idxd_group_flags_setup(struct idxd_device * idxd)1033 static void idxd_group_flags_setup(struct idxd_device *idxd)
1034 {
1035 int i;
1036
1037 /* TC-A 0 and TC-B 1 should be defaults */
1038 for (i = 0; i < idxd->max_groups; i++) {
1039 struct idxd_group *group = idxd->groups[i];
1040
1041 if (group->tc_a == -1)
1042 group->tc_a = group->grpcfg.flags.tc_a = 0;
1043 else
1044 group->grpcfg.flags.tc_a = group->tc_a;
1045 if (group->tc_b == -1)
1046 group->tc_b = group->grpcfg.flags.tc_b = 1;
1047 else
1048 group->grpcfg.flags.tc_b = group->tc_b;
1049 group->grpcfg.flags.use_rdbuf_limit = group->use_rdbuf_limit;
1050 group->grpcfg.flags.rdbufs_reserved = group->rdbufs_reserved;
1051 group->grpcfg.flags.rdbufs_allowed = group->rdbufs_allowed;
1052 group->grpcfg.flags.desc_progress_limit = group->desc_progress_limit;
1053 group->grpcfg.flags.batch_progress_limit = group->batch_progress_limit;
1054 }
1055 }
1056
idxd_engines_setup(struct idxd_device * idxd)1057 static int idxd_engines_setup(struct idxd_device *idxd)
1058 {
1059 int i, engines = 0;
1060 struct idxd_engine *eng;
1061 struct idxd_group *group;
1062
1063 for (i = 0; i < idxd->max_groups; i++) {
1064 group = idxd->groups[i];
1065 group->grpcfg.engines = 0;
1066 }
1067
1068 for (i = 0; i < idxd->max_engines; i++) {
1069 eng = idxd->engines[i];
1070 group = eng->group;
1071
1072 if (!group)
1073 continue;
1074
1075 group->grpcfg.engines |= BIT(eng->id);
1076 engines++;
1077 }
1078
1079 if (!engines)
1080 return -EINVAL;
1081
1082 return 0;
1083 }
1084
idxd_wqs_setup(struct idxd_device * idxd)1085 static int idxd_wqs_setup(struct idxd_device *idxd)
1086 {
1087 struct idxd_wq *wq;
1088 struct idxd_group *group;
1089 int i, j, configured = 0;
1090 struct device *dev = &idxd->pdev->dev;
1091
1092 for (i = 0; i < idxd->max_groups; i++) {
1093 group = idxd->groups[i];
1094 for (j = 0; j < 4; j++)
1095 group->grpcfg.wqs[j] = 0;
1096 }
1097
1098 for (i = 0; i < idxd->max_wqs; i++) {
1099 wq = idxd->wqs[i];
1100 group = wq->group;
1101
1102 if (!wq->group)
1103 continue;
1104
1105 if (wq_shared(wq) && !wq_shared_supported(wq)) {
1106 idxd->cmd_status = IDXD_SCMD_WQ_NO_SWQ_SUPPORT;
1107 dev_warn(dev, "No shared wq support but configured.\n");
1108 return -EINVAL;
1109 }
1110
1111 group->grpcfg.wqs[wq->id / 64] |= BIT(wq->id % 64);
1112 configured++;
1113 }
1114
1115 if (configured == 0) {
1116 idxd->cmd_status = IDXD_SCMD_WQ_NONE_CONFIGURED;
1117 return -EINVAL;
1118 }
1119
1120 return 0;
1121 }
1122
idxd_device_config(struct idxd_device * idxd)1123 int idxd_device_config(struct idxd_device *idxd)
1124 {
1125 int rc;
1126
1127 guard(spinlock)(&idxd->dev_lock);
1128
1129 if (!test_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags))
1130 return 0;
1131
1132 rc = idxd_wqs_setup(idxd);
1133 if (rc < 0)
1134 return rc;
1135
1136 rc = idxd_engines_setup(idxd);
1137 if (rc < 0)
1138 return rc;
1139
1140 idxd_group_flags_setup(idxd);
1141
1142 rc = idxd_wqs_config_write(idxd);
1143 if (rc < 0)
1144 return rc;
1145
1146 rc = idxd_groups_config_write(idxd);
1147 if (rc < 0)
1148 return rc;
1149
1150 return 0;
1151 }
1152
idxd_wq_load_config(struct idxd_wq * wq)1153 static int idxd_wq_load_config(struct idxd_wq *wq)
1154 {
1155 struct idxd_device *idxd = wq->idxd;
1156 struct device *dev = &idxd->pdev->dev;
1157 int wqcfg_offset;
1158 int i;
1159
1160 wqcfg_offset = WQCFG_OFFSET(idxd, wq->id, 0);
1161 memcpy_fromio(wq->wqcfg, idxd->reg_base + wqcfg_offset, idxd->wqcfg_size);
1162
1163 wq->size = wq->wqcfg->wq_size;
1164 wq->threshold = wq->wqcfg->wq_thresh;
1165
1166 /* The driver does not support shared WQ mode in read-only config yet */
1167 if (wq->wqcfg->mode == 0 || wq->wqcfg->pasid_en)
1168 return -EOPNOTSUPP;
1169
1170 set_bit(WQ_FLAG_DEDICATED, &wq->flags);
1171
1172 wq->priority = wq->wqcfg->priority;
1173
1174 wq->max_xfer_bytes = 1ULL << wq->wqcfg->max_xfer_shift;
1175 idxd_wq_set_max_batch_size(idxd->data->type, wq, 1U << wq->wqcfg->max_batch_shift);
1176 if (idxd_sgl_supported(idxd))
1177 wq->max_sgl_size = 1U << wq->wqcfg->max_sgl_shift;
1178
1179 for (i = 0; i < WQCFG_STRIDES(idxd); i++) {
1180 wqcfg_offset = WQCFG_OFFSET(idxd, wq->id, i);
1181 dev_dbg(dev, "WQ[%d][%d][%#x]: %#x\n", wq->id, i, wqcfg_offset, wq->wqcfg->bits[i]);
1182 }
1183
1184 return 0;
1185 }
1186
idxd_group_load_config(struct idxd_group * group)1187 static void idxd_group_load_config(struct idxd_group *group)
1188 {
1189 struct idxd_device *idxd = group->idxd;
1190 struct device *dev = &idxd->pdev->dev;
1191 int i, j, grpcfg_offset;
1192
1193 /*
1194 * Load WQS bit fields
1195 * Iterate through all 256 bits 64 bits at a time
1196 */
1197 for (i = 0; i < GRPWQCFG_STRIDES; i++) {
1198 struct idxd_wq *wq;
1199
1200 grpcfg_offset = GRPWQCFG_OFFSET(idxd, group->id, i);
1201 group->grpcfg.wqs[i] = ioread64(idxd->reg_base + grpcfg_offset);
1202 dev_dbg(dev, "GRPCFG wq[%d:%d: %#x]: %#llx\n",
1203 group->id, i, grpcfg_offset, group->grpcfg.wqs[i]);
1204
1205 if (i * 64 >= idxd->max_wqs)
1206 break;
1207
1208 /* Iterate through all 64 bits and check for wq set */
1209 for (j = 0; j < 64; j++) {
1210 int id = i * 64 + j;
1211
1212 /* No need to check beyond max wqs */
1213 if (id >= idxd->max_wqs)
1214 break;
1215
1216 /* Set group assignment for wq if wq bit is set */
1217 if (group->grpcfg.wqs[i] & BIT(j)) {
1218 wq = idxd->wqs[id];
1219 wq->group = group;
1220 }
1221 }
1222 }
1223
1224 grpcfg_offset = GRPENGCFG_OFFSET(idxd, group->id);
1225 group->grpcfg.engines = ioread64(idxd->reg_base + grpcfg_offset);
1226 dev_dbg(dev, "GRPCFG engs[%d: %#x]: %#llx\n", group->id,
1227 grpcfg_offset, group->grpcfg.engines);
1228
1229 /* Iterate through all 64 bits to check engines set */
1230 for (i = 0; i < 64; i++) {
1231 if (i >= idxd->max_engines)
1232 break;
1233
1234 if (group->grpcfg.engines & BIT(i)) {
1235 struct idxd_engine *engine = idxd->engines[i];
1236
1237 engine->group = group;
1238 }
1239 }
1240
1241 grpcfg_offset = GRPFLGCFG_OFFSET(idxd, group->id);
1242 group->grpcfg.flags.bits = ioread64(idxd->reg_base + grpcfg_offset);
1243 dev_dbg(dev, "GRPFLAGS flags[%d: %#x]: %#llx\n",
1244 group->id, grpcfg_offset, group->grpcfg.flags.bits);
1245 }
1246
idxd_device_load_config(struct idxd_device * idxd)1247 int idxd_device_load_config(struct idxd_device *idxd)
1248 {
1249 union gencfg_reg reg;
1250 int i, rc;
1251
1252 reg.bits = ioread32(idxd->reg_base + IDXD_GENCFG_OFFSET);
1253 idxd->rdbuf_limit = reg.rdbuf_limit;
1254
1255 for (i = 0; i < idxd->max_groups; i++) {
1256 struct idxd_group *group = idxd->groups[i];
1257
1258 idxd_group_load_config(group);
1259 }
1260
1261 for (i = 0; i < idxd->max_wqs; i++) {
1262 struct idxd_wq *wq = idxd->wqs[i];
1263
1264 rc = idxd_wq_load_config(wq);
1265 if (rc < 0)
1266 return rc;
1267 }
1268
1269 return 0;
1270 }
1271
idxd_flush_pending_descs(struct idxd_irq_entry * ie)1272 static void idxd_flush_pending_descs(struct idxd_irq_entry *ie)
1273 {
1274 struct idxd_desc *desc, *itr;
1275 struct llist_node *head;
1276 LIST_HEAD(flist);
1277 enum idxd_complete_type ctype;
1278
1279 spin_lock(&ie->list_lock);
1280 head = llist_del_all(&ie->pending_llist);
1281 if (head) {
1282 llist_for_each_entry_safe(desc, itr, head, llnode)
1283 list_add_tail(&desc->list, &ie->work_list);
1284 }
1285
1286 list_for_each_entry_safe(desc, itr, &ie->work_list, list)
1287 list_move_tail(&desc->list, &flist);
1288 spin_unlock(&ie->list_lock);
1289
1290 list_for_each_entry_safe(desc, itr, &flist, list) {
1291 struct dma_async_tx_descriptor *tx;
1292
1293 list_del(&desc->list);
1294 ctype = desc->completion->status ? IDXD_COMPLETE_NORMAL : IDXD_COMPLETE_ABORT;
1295 /*
1296 * wq is being disabled. Any remaining descriptors are
1297 * likely to be stuck and can be dropped. callback could
1298 * point to code that is no longer accessible, for example
1299 * if dmatest module has been unloaded.
1300 */
1301 tx = &desc->txd;
1302 tx->callback = NULL;
1303 tx->callback_result = NULL;
1304 idxd_dma_complete_txd(desc, ctype, true, NULL, NULL);
1305 }
1306 }
1307
idxd_device_set_perm_entry(struct idxd_device * idxd,struct idxd_irq_entry * ie)1308 static void idxd_device_set_perm_entry(struct idxd_device *idxd,
1309 struct idxd_irq_entry *ie)
1310 {
1311 union msix_perm mperm;
1312
1313 if (ie->pasid == IOMMU_PASID_INVALID)
1314 return;
1315
1316 mperm.bits = 0;
1317 mperm.pasid = ie->pasid;
1318 mperm.pasid_en = 1;
1319 iowrite32(mperm.bits, idxd->reg_base + idxd->msix_perm_offset + ie->id * 8);
1320 }
1321
idxd_device_clear_perm_entry(struct idxd_device * idxd,struct idxd_irq_entry * ie)1322 static void idxd_device_clear_perm_entry(struct idxd_device *idxd,
1323 struct idxd_irq_entry *ie)
1324 {
1325 iowrite32(0, idxd->reg_base + idxd->msix_perm_offset + ie->id * 8);
1326 }
1327
idxd_wq_free_irq(struct idxd_wq * wq)1328 void idxd_wq_free_irq(struct idxd_wq *wq)
1329 {
1330 struct idxd_device *idxd = wq->idxd;
1331 struct idxd_irq_entry *ie = &wq->ie;
1332
1333 if (wq->type != IDXD_WQT_KERNEL)
1334 return;
1335
1336 free_irq(ie->vector, ie);
1337 idxd_flush_pending_descs(ie);
1338
1339 /* The interrupt might have been already released by FLR */
1340 if (ie->int_handle == INVALID_INT_HANDLE)
1341 return;
1342
1343 if (idxd->request_int_handles)
1344 idxd_device_release_int_handle(idxd, ie->int_handle, IDXD_IRQ_MSIX);
1345 idxd_device_clear_perm_entry(idxd, ie);
1346 ie->vector = -1;
1347 ie->int_handle = INVALID_INT_HANDLE;
1348 ie->pasid = IOMMU_PASID_INVALID;
1349 }
1350
idxd_wq_flush_descs(struct idxd_wq * wq)1351 void idxd_wq_flush_descs(struct idxd_wq *wq)
1352 {
1353 struct idxd_irq_entry *ie = &wq->ie;
1354 struct idxd_device *idxd = wq->idxd;
1355
1356 guard(mutex)(&wq->wq_lock);
1357
1358 if (wq->state != IDXD_WQ_ENABLED || wq->type != IDXD_WQT_KERNEL)
1359 return;
1360
1361 idxd_flush_pending_descs(ie);
1362 if (idxd->request_int_handles)
1363 idxd_device_release_int_handle(idxd, ie->int_handle, IDXD_IRQ_MSIX);
1364 idxd_device_clear_perm_entry(idxd, ie);
1365 ie->int_handle = INVALID_INT_HANDLE;
1366 }
1367
idxd_wq_request_irq(struct idxd_wq * wq)1368 int idxd_wq_request_irq(struct idxd_wq *wq)
1369 {
1370 struct idxd_device *idxd = wq->idxd;
1371 struct pci_dev *pdev = idxd->pdev;
1372 struct device *dev = &pdev->dev;
1373 struct idxd_irq_entry *ie;
1374 int rc;
1375
1376 if (wq->type != IDXD_WQT_KERNEL)
1377 return 0;
1378
1379 ie = &wq->ie;
1380 ie->vector = pci_irq_vector(pdev, ie->id);
1381 ie->pasid = device_pasid_enabled(idxd) ? idxd->pasid : IOMMU_PASID_INVALID;
1382 idxd_device_set_perm_entry(idxd, ie);
1383
1384 rc = request_threaded_irq(ie->vector, NULL, idxd_wq_thread, 0, "idxd-portal", ie);
1385 if (rc < 0) {
1386 dev_err(dev, "Failed to request irq %d.\n", ie->vector);
1387 goto err_irq;
1388 }
1389
1390 if (idxd->request_int_handles) {
1391 rc = idxd_device_request_int_handle(idxd, ie->id, &ie->int_handle,
1392 IDXD_IRQ_MSIX);
1393 if (rc < 0)
1394 goto err_int_handle;
1395 } else {
1396 ie->int_handle = ie->id;
1397 }
1398
1399 return 0;
1400
1401 err_int_handle:
1402 ie->int_handle = INVALID_INT_HANDLE;
1403 free_irq(ie->vector, ie);
1404 err_irq:
1405 idxd_device_clear_perm_entry(idxd, ie);
1406 ie->pasid = IOMMU_PASID_INVALID;
1407 return rc;
1408 }
1409
idxd_drv_enable_wq(struct idxd_wq * wq)1410 int idxd_drv_enable_wq(struct idxd_wq *wq)
1411 {
1412 struct idxd_device *idxd = wq->idxd;
1413 struct device *dev = &idxd->pdev->dev;
1414 int rc = -ENXIO;
1415
1416 lockdep_assert_held(&wq->wq_lock);
1417
1418 if (idxd->state != IDXD_DEV_ENABLED) {
1419 idxd->cmd_status = IDXD_SCMD_DEV_NOT_ENABLED;
1420 goto err;
1421 }
1422
1423 if (wq->state != IDXD_WQ_DISABLED) {
1424 dev_dbg(dev, "wq %d already enabled.\n", wq->id);
1425 idxd->cmd_status = IDXD_SCMD_WQ_ENABLED;
1426 rc = -EBUSY;
1427 goto err;
1428 }
1429
1430 if (!wq->group) {
1431 dev_dbg(dev, "wq %d not attached to group.\n", wq->id);
1432 idxd->cmd_status = IDXD_SCMD_WQ_NO_GRP;
1433 goto err;
1434 }
1435
1436 if (strlen(wq->name) == 0) {
1437 idxd->cmd_status = IDXD_SCMD_WQ_NO_NAME;
1438 dev_dbg(dev, "wq %d name not set.\n", wq->id);
1439 goto err;
1440 }
1441
1442 /* Shared WQ checks */
1443 if (wq_shared(wq)) {
1444 if (!wq_shared_supported(wq)) {
1445 idxd->cmd_status = IDXD_SCMD_WQ_NO_SVM;
1446 dev_dbg(dev, "PASID not enabled and shared wq.\n");
1447 goto err;
1448 }
1449 /*
1450 * Shared wq with the threshold set to 0 means the user
1451 * did not set the threshold or transitioned from a
1452 * dedicated wq but did not set threshold. A value
1453 * of 0 would effectively disable the shared wq. The
1454 * driver does not allow a value of 0 to be set for
1455 * threshold via sysfs.
1456 */
1457 if (wq->threshold == 0) {
1458 idxd->cmd_status = IDXD_SCMD_WQ_NO_THRESH;
1459 dev_dbg(dev, "Shared wq and threshold 0.\n");
1460 goto err;
1461 }
1462 }
1463
1464 /*
1465 * In the event that the WQ is configurable for pasid, the driver
1466 * should setup the pasid, pasid_en bit. This is true for both kernel
1467 * and user shared workqueues. There is no need to setup priv bit in
1468 * that in-kernel DMA will also do user privileged requests.
1469 * A dedicated wq that is not 'kernel' type will configure pasid and
1470 * pasid_en later on so there is no need to setup.
1471 */
1472 if (test_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags)) {
1473 if (wq_pasid_enabled(wq)) {
1474 if (is_idxd_wq_kernel(wq) || wq_shared(wq)) {
1475 u32 pasid = wq_dedicated(wq) ? idxd->pasid : 0;
1476
1477 __idxd_wq_set_pasid_locked(wq, pasid);
1478 }
1479 }
1480 }
1481
1482 rc = idxd_device_config(idxd);
1483 if (rc < 0) {
1484 dev_dbg(dev, "Writing wq %d config failed: %d\n", wq->id, rc);
1485 goto err;
1486 }
1487
1488 rc = idxd_wq_enable(wq);
1489 if (rc < 0) {
1490 dev_dbg(dev, "wq %d enabling failed: %d\n", wq->id, rc);
1491 goto err;
1492 }
1493
1494 rc = idxd_wq_map_portal(wq);
1495 if (rc < 0) {
1496 idxd->cmd_status = IDXD_SCMD_WQ_PORTAL_ERR;
1497 dev_dbg(dev, "wq %d portal mapping failed: %d\n", wq->id, rc);
1498 goto err_map_portal;
1499 }
1500
1501 wq->client_count = 0;
1502
1503 rc = idxd_wq_request_irq(wq);
1504 if (rc < 0) {
1505 idxd->cmd_status = IDXD_SCMD_WQ_IRQ_ERR;
1506 dev_dbg(dev, "WQ %d irq setup failed: %d\n", wq->id, rc);
1507 goto err_irq;
1508 }
1509
1510 rc = idxd_wq_alloc_resources(wq);
1511 if (rc < 0) {
1512 idxd->cmd_status = IDXD_SCMD_WQ_RES_ALLOC_ERR;
1513 dev_dbg(dev, "WQ resource alloc failed\n");
1514 goto err_res_alloc;
1515 }
1516
1517 rc = idxd_wq_init_percpu_ref(wq);
1518 if (rc < 0) {
1519 idxd->cmd_status = IDXD_SCMD_PERCPU_ERR;
1520 dev_dbg(dev, "percpu_ref setup failed\n");
1521 goto err_ref;
1522 }
1523
1524 return 0;
1525
1526 err_ref:
1527 idxd_wq_free_resources(wq);
1528 err_res_alloc:
1529 idxd_wq_free_irq(wq);
1530 err_irq:
1531 idxd_wq_unmap_portal(wq);
1532 err_map_portal:
1533 if (idxd_wq_disable(wq, false))
1534 dev_dbg(dev, "wq %s disable failed\n", dev_name(wq_confdev(wq)));
1535 err:
1536 return rc;
1537 }
1538 EXPORT_SYMBOL_NS_GPL(idxd_drv_enable_wq, "IDXD");
1539
idxd_drv_disable_wq(struct idxd_wq * wq)1540 void idxd_drv_disable_wq(struct idxd_wq *wq)
1541 {
1542 struct idxd_device *idxd = wq->idxd;
1543 struct device *dev = &idxd->pdev->dev;
1544
1545 lockdep_assert_held(&wq->wq_lock);
1546
1547 if (idxd_wq_refcount(wq))
1548 dev_warn(dev, "Clients has claim on wq %d: %d\n",
1549 wq->id, idxd_wq_refcount(wq));
1550
1551 idxd_wq_unmap_portal(wq);
1552 idxd_wq_drain(wq);
1553 idxd_wq_free_irq(wq);
1554 idxd_wq_reset(wq);
1555 idxd_wq_free_resources(wq);
1556 percpu_ref_exit(&wq->wq_active);
1557 wq->client_count = 0;
1558 }
1559 EXPORT_SYMBOL_NS_GPL(idxd_drv_disable_wq, "IDXD");
1560
idxd_device_drv_probe(struct idxd_dev * idxd_dev)1561 int idxd_device_drv_probe(struct idxd_dev *idxd_dev)
1562 {
1563 struct idxd_device *idxd = idxd_dev_to_idxd(idxd_dev);
1564 int rc = 0;
1565
1566 /*
1567 * Device should be in disabled state for the idxd_drv to load. If it's in
1568 * enabled state, then the device was altered outside of driver's control.
1569 * If the state is in halted state, then we don't want to proceed.
1570 */
1571 if (idxd->state != IDXD_DEV_DISABLED) {
1572 idxd->cmd_status = IDXD_SCMD_DEV_ENABLED;
1573 return -ENXIO;
1574 }
1575
1576 /* Device configuration */
1577 rc = idxd_device_config(idxd);
1578 if (rc < 0)
1579 return -ENXIO;
1580
1581 /*
1582 * System PASID is preserved across device disable/enable cycle, but
1583 * genconfig register content gets cleared during device reset. We
1584 * need to re-enable user interrupts for kernel work queue completion
1585 * IRQ to function.
1586 */
1587 if (idxd->pasid != IOMMU_PASID_INVALID)
1588 idxd_set_user_intr(idxd, 1);
1589
1590 rc = idxd_device_evl_setup(idxd);
1591 if (rc < 0) {
1592 idxd->cmd_status = IDXD_SCMD_DEV_EVL_ERR;
1593 return rc;
1594 }
1595
1596 /* Start device */
1597 rc = idxd_device_enable(idxd);
1598 if (rc < 0) {
1599 idxd_device_evl_free(idxd);
1600 return rc;
1601 }
1602
1603 /* Setup DMA device without channels */
1604 rc = idxd_register_dma_device(idxd);
1605 if (rc < 0) {
1606 idxd_device_disable(idxd);
1607 idxd_device_evl_free(idxd);
1608 idxd->cmd_status = IDXD_SCMD_DEV_DMA_ERR;
1609 return rc;
1610 }
1611
1612 idxd->cmd_status = 0;
1613 return 0;
1614 }
1615
idxd_device_drv_remove(struct idxd_dev * idxd_dev)1616 void idxd_device_drv_remove(struct idxd_dev *idxd_dev)
1617 {
1618 struct device *dev = &idxd_dev->conf_dev;
1619 struct idxd_device *idxd = idxd_dev_to_idxd(idxd_dev);
1620 int i;
1621
1622 for (i = 0; i < idxd->max_wqs; i++) {
1623 struct idxd_wq *wq = idxd->wqs[i];
1624 struct device *wq_dev = wq_confdev(wq);
1625
1626 if (wq->state == IDXD_WQ_DISABLED)
1627 continue;
1628 dev_warn(dev, "Active wq %d on disable %s.\n", i, dev_name(wq_dev));
1629 device_release_driver(wq_dev);
1630 }
1631
1632 idxd_unregister_dma_device(idxd);
1633 idxd_device_disable(idxd);
1634 if (test_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags))
1635 idxd_device_reset(idxd);
1636 idxd_device_evl_free(idxd);
1637 }
1638
1639 static enum idxd_dev_type dev_types[] = {
1640 IDXD_DEV_DSA,
1641 IDXD_DEV_IAX,
1642 IDXD_DEV_NONE,
1643 };
1644
1645 struct idxd_device_driver idxd_drv = {
1646 .type = dev_types,
1647 .probe = idxd_device_drv_probe,
1648 .remove = idxd_device_drv_remove,
1649 .name = "idxd",
1650 };
1651 EXPORT_SYMBOL_GPL(idxd_drv);
1652