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>
7 #include <linux/io-64-nonatomic-lo-hi.h>
8 #include <linux/dmaengine.h>
10 #include <linux/msi.h>
11 #include <uapi/linux/idxd.h>
12 #include "../dmaengine.h"
14 #include "registers.h"
16 static void idxd_cmd_exec(struct idxd_device *idxd, int cmd_code, u32 operand,
19 /* Interrupt control bits */
20 void idxd_mask_msix_vector(struct idxd_device *idxd, int vec_id)
22 struct irq_data *data = irq_get_irq_data(idxd->msix_entries[vec_id].vector);
24 pci_msi_mask_irq(data);
27 void idxd_mask_msix_vectors(struct idxd_device *idxd)
29 struct pci_dev *pdev = idxd->pdev;
30 int msixcnt = pci_msix_vec_count(pdev);
33 for (i = 0; i < msixcnt; i++)
34 idxd_mask_msix_vector(idxd, i);
37 void idxd_unmask_msix_vector(struct idxd_device *idxd, int vec_id)
39 struct irq_data *data = irq_get_irq_data(idxd->msix_entries[vec_id].vector);
41 pci_msi_unmask_irq(data);
44 void idxd_unmask_error_interrupts(struct idxd_device *idxd)
46 union genctrl_reg genctrl;
48 genctrl.bits = ioread32(idxd->reg_base + IDXD_GENCTRL_OFFSET);
49 genctrl.softerr_int_en = 1;
50 iowrite32(genctrl.bits, idxd->reg_base + IDXD_GENCTRL_OFFSET);
53 void idxd_mask_error_interrupts(struct idxd_device *idxd)
55 union genctrl_reg genctrl;
57 genctrl.bits = ioread32(idxd->reg_base + IDXD_GENCTRL_OFFSET);
58 genctrl.softerr_int_en = 0;
59 iowrite32(genctrl.bits, idxd->reg_base + IDXD_GENCTRL_OFFSET);
62 static void free_hw_descs(struct idxd_wq *wq)
66 for (i = 0; i < wq->num_descs; i++)
67 kfree(wq->hw_descs[i]);
72 static int alloc_hw_descs(struct idxd_wq *wq, int num)
74 struct device *dev = &wq->idxd->pdev->dev;
76 int node = dev_to_node(dev);
78 wq->hw_descs = kcalloc_node(num, sizeof(struct dsa_hw_desc *),
83 for (i = 0; i < num; i++) {
84 wq->hw_descs[i] = kzalloc_node(sizeof(*wq->hw_descs[i]),
86 if (!wq->hw_descs[i]) {
95 static void free_descs(struct idxd_wq *wq)
99 for (i = 0; i < wq->num_descs; i++)
105 static int alloc_descs(struct idxd_wq *wq, int num)
107 struct device *dev = &wq->idxd->pdev->dev;
109 int node = dev_to_node(dev);
111 wq->descs = kcalloc_node(num, sizeof(struct idxd_desc *),
116 for (i = 0; i < num; i++) {
117 wq->descs[i] = kzalloc_node(sizeof(*wq->descs[i]),
128 /* WQ control bits */
129 int idxd_wq_alloc_resources(struct idxd_wq *wq)
131 struct idxd_device *idxd = wq->idxd;
132 struct device *dev = &idxd->pdev->dev;
133 int rc, num_descs, i;
137 if (wq->type != IDXD_WQT_KERNEL)
140 wq->num_descs = wq->size;
141 num_descs = wq->size;
143 rc = alloc_hw_descs(wq, num_descs);
147 if (idxd->type == IDXD_TYPE_DSA)
149 else if (idxd->type == IDXD_TYPE_IAX)
154 wq->compls_size = num_descs * idxd->compl_size + align;
155 wq->compls_raw = dma_alloc_coherent(dev, wq->compls_size,
156 &wq->compls_addr_raw, GFP_KERNEL);
157 if (!wq->compls_raw) {
159 goto fail_alloc_compls;
162 /* Adjust alignment */
163 wq->compls_addr = (wq->compls_addr_raw + (align - 1)) & ~(align - 1);
164 tmp = (u64)wq->compls_raw;
165 tmp = (tmp + (align - 1)) & ~(align - 1);
166 wq->compls = (struct dsa_completion_record *)tmp;
168 rc = alloc_descs(wq, num_descs);
170 goto fail_alloc_descs;
172 rc = sbitmap_queue_init_node(&wq->sbq, num_descs, -1, false, GFP_KERNEL,
175 goto fail_sbitmap_init;
177 for (i = 0; i < num_descs; i++) {
178 struct idxd_desc *desc = wq->descs[i];
180 desc->hw = wq->hw_descs[i];
181 if (idxd->type == IDXD_TYPE_DSA)
182 desc->completion = &wq->compls[i];
183 else if (idxd->type == IDXD_TYPE_IAX)
184 desc->iax_completion = &wq->iax_compls[i];
185 desc->compl_dma = wq->compls_addr + idxd->compl_size * i;
189 dma_async_tx_descriptor_init(&desc->txd, &wq->dma_chan);
190 desc->txd.tx_submit = idxd_dma_tx_submit;
198 dma_free_coherent(dev, wq->compls_size, wq->compls_raw,
199 wq->compls_addr_raw);
205 void idxd_wq_free_resources(struct idxd_wq *wq)
207 struct device *dev = &wq->idxd->pdev->dev;
209 if (wq->type != IDXD_WQT_KERNEL)
214 dma_free_coherent(dev, wq->compls_size, wq->compls_raw,
215 wq->compls_addr_raw);
216 sbitmap_queue_free(&wq->sbq);
219 int idxd_wq_enable(struct idxd_wq *wq)
221 struct idxd_device *idxd = wq->idxd;
222 struct device *dev = &idxd->pdev->dev;
225 if (wq->state == IDXD_WQ_ENABLED) {
226 dev_dbg(dev, "WQ %d already enabled\n", wq->id);
230 idxd_cmd_exec(idxd, IDXD_CMD_ENABLE_WQ, wq->id, &status);
232 if (status != IDXD_CMDSTS_SUCCESS &&
233 status != IDXD_CMDSTS_ERR_WQ_ENABLED) {
234 dev_dbg(dev, "WQ enable failed: %#x\n", status);
238 wq->state = IDXD_WQ_ENABLED;
239 dev_dbg(dev, "WQ %d enabled\n", wq->id);
243 int idxd_wq_disable(struct idxd_wq *wq)
245 struct idxd_device *idxd = wq->idxd;
246 struct device *dev = &idxd->pdev->dev;
249 dev_dbg(dev, "Disabling WQ %d\n", wq->id);
251 if (wq->state != IDXD_WQ_ENABLED) {
252 dev_dbg(dev, "WQ %d in wrong state: %d\n", wq->id, wq->state);
256 operand = BIT(wq->id % 16) | ((wq->id / 16) << 16);
257 idxd_cmd_exec(idxd, IDXD_CMD_DISABLE_WQ, operand, &status);
259 if (status != IDXD_CMDSTS_SUCCESS) {
260 dev_dbg(dev, "WQ disable failed: %#x\n", status);
264 wq->state = IDXD_WQ_DISABLED;
265 dev_dbg(dev, "WQ %d disabled\n", wq->id);
269 void idxd_wq_drain(struct idxd_wq *wq)
271 struct idxd_device *idxd = wq->idxd;
272 struct device *dev = &idxd->pdev->dev;
275 if (wq->state != IDXD_WQ_ENABLED) {
276 dev_dbg(dev, "WQ %d in wrong state: %d\n", wq->id, wq->state);
280 dev_dbg(dev, "Draining WQ %d\n", wq->id);
281 operand = BIT(wq->id % 16) | ((wq->id / 16) << 16);
282 idxd_cmd_exec(idxd, IDXD_CMD_DRAIN_WQ, operand, NULL);
285 int idxd_wq_map_portal(struct idxd_wq *wq)
287 struct idxd_device *idxd = wq->idxd;
288 struct pci_dev *pdev = idxd->pdev;
289 struct device *dev = &pdev->dev;
290 resource_size_t start;
292 start = pci_resource_start(pdev, IDXD_WQ_BAR);
293 start += idxd_get_wq_portal_full_offset(wq->id, IDXD_PORTAL_LIMITED);
295 wq->portal = devm_ioremap(dev, start, IDXD_PORTAL_SIZE);
302 void idxd_wq_unmap_portal(struct idxd_wq *wq)
304 struct device *dev = &wq->idxd->pdev->dev;
306 devm_iounmap(dev, wq->portal);
309 int idxd_wq_set_pasid(struct idxd_wq *wq, int pasid)
311 struct idxd_device *idxd = wq->idxd;
317 rc = idxd_wq_disable(wq);
321 offset = WQCFG_OFFSET(idxd, wq->id, WQCFG_PASID_IDX);
322 spin_lock_irqsave(&idxd->dev_lock, flags);
323 wqcfg.bits[WQCFG_PASID_IDX] = ioread32(idxd->reg_base + offset);
326 iowrite32(wqcfg.bits[WQCFG_PASID_IDX], idxd->reg_base + offset);
327 spin_unlock_irqrestore(&idxd->dev_lock, flags);
329 rc = idxd_wq_enable(wq);
336 int idxd_wq_disable_pasid(struct idxd_wq *wq)
338 struct idxd_device *idxd = wq->idxd;
344 rc = idxd_wq_disable(wq);
348 offset = WQCFG_OFFSET(idxd, wq->id, WQCFG_PASID_IDX);
349 spin_lock_irqsave(&idxd->dev_lock, flags);
350 wqcfg.bits[WQCFG_PASID_IDX] = ioread32(idxd->reg_base + offset);
353 iowrite32(wqcfg.bits[WQCFG_PASID_IDX], idxd->reg_base + offset);
354 spin_unlock_irqrestore(&idxd->dev_lock, flags);
356 rc = idxd_wq_enable(wq);
363 void idxd_wq_disable_cleanup(struct idxd_wq *wq)
365 struct idxd_device *idxd = wq->idxd;
366 struct device *dev = &idxd->pdev->dev;
369 lockdep_assert_held(&idxd->dev_lock);
370 memset(wq->wqcfg, 0, idxd->wqcfg_size);
371 wq->type = IDXD_WQT_NONE;
377 clear_bit(WQ_FLAG_DEDICATED, &wq->flags);
378 memset(wq->name, 0, WQ_NAME_SIZE);
380 for (i = 0; i < WQCFG_STRIDES(idxd); i++) {
381 wq_offset = WQCFG_OFFSET(idxd, wq->id, i);
382 iowrite32(0, idxd->reg_base + wq_offset);
383 dev_dbg(dev, "WQ[%d][%d][%#x]: %#x\n",
384 wq->id, i, wq_offset,
385 ioread32(idxd->reg_base + wq_offset));
389 /* Device control bits */
390 static inline bool idxd_is_enabled(struct idxd_device *idxd)
392 union gensts_reg gensts;
394 gensts.bits = ioread32(idxd->reg_base + IDXD_GENSTATS_OFFSET);
396 if (gensts.state == IDXD_DEVICE_STATE_ENABLED)
401 static inline bool idxd_device_is_halted(struct idxd_device *idxd)
403 union gensts_reg gensts;
405 gensts.bits = ioread32(idxd->reg_base + IDXD_GENSTATS_OFFSET);
407 return (gensts.state == IDXD_DEVICE_STATE_HALT);
411 * This is function is only used for reset during probe and will
412 * poll for completion. Once the device is setup with interrupts,
413 * all commands will be done via interrupt completion.
415 int idxd_device_init_reset(struct idxd_device *idxd)
417 struct device *dev = &idxd->pdev->dev;
418 union idxd_command_reg cmd;
421 if (idxd_device_is_halted(idxd)) {
422 dev_warn(&idxd->pdev->dev, "Device is HALTED!\n");
426 memset(&cmd, 0, sizeof(cmd));
427 cmd.cmd = IDXD_CMD_RESET_DEVICE;
428 dev_dbg(dev, "%s: sending reset for init.\n", __func__);
429 spin_lock_irqsave(&idxd->dev_lock, flags);
430 iowrite32(cmd.bits, idxd->reg_base + IDXD_CMD_OFFSET);
432 while (ioread32(idxd->reg_base + IDXD_CMDSTS_OFFSET) &
435 spin_unlock_irqrestore(&idxd->dev_lock, flags);
439 static void idxd_cmd_exec(struct idxd_device *idxd, int cmd_code, u32 operand,
442 union idxd_command_reg cmd;
443 DECLARE_COMPLETION_ONSTACK(done);
446 if (idxd_device_is_halted(idxd)) {
447 dev_warn(&idxd->pdev->dev, "Device is HALTED!\n");
449 *status = IDXD_CMDSTS_HW_ERR;
453 memset(&cmd, 0, sizeof(cmd));
455 cmd.operand = operand;
458 spin_lock_irqsave(&idxd->dev_lock, flags);
459 wait_event_lock_irq(idxd->cmd_waitq,
460 !test_bit(IDXD_FLAG_CMD_RUNNING, &idxd->flags),
463 dev_dbg(&idxd->pdev->dev, "%s: sending cmd: %#x op: %#x\n",
464 __func__, cmd_code, operand);
466 idxd->cmd_status = 0;
467 __set_bit(IDXD_FLAG_CMD_RUNNING, &idxd->flags);
468 idxd->cmd_done = &done;
469 iowrite32(cmd.bits, idxd->reg_base + IDXD_CMD_OFFSET);
472 * After command submitted, release lock and go to sleep until
473 * the command completes via interrupt.
475 spin_unlock_irqrestore(&idxd->dev_lock, flags);
476 wait_for_completion(&done);
477 spin_lock_irqsave(&idxd->dev_lock, flags);
479 *status = ioread32(idxd->reg_base + IDXD_CMDSTS_OFFSET);
480 idxd->cmd_status = *status & GENMASK(7, 0);
483 __clear_bit(IDXD_FLAG_CMD_RUNNING, &idxd->flags);
484 /* Wake up other pending commands */
485 wake_up(&idxd->cmd_waitq);
486 spin_unlock_irqrestore(&idxd->dev_lock, flags);
489 int idxd_device_enable(struct idxd_device *idxd)
491 struct device *dev = &idxd->pdev->dev;
494 if (idxd_is_enabled(idxd)) {
495 dev_dbg(dev, "Device already enabled\n");
499 idxd_cmd_exec(idxd, IDXD_CMD_ENABLE_DEVICE, 0, &status);
501 /* If the command is successful or if the device was enabled */
502 if (status != IDXD_CMDSTS_SUCCESS &&
503 status != IDXD_CMDSTS_ERR_DEV_ENABLED) {
504 dev_dbg(dev, "%s: err_code: %#x\n", __func__, status);
508 idxd->state = IDXD_DEV_ENABLED;
512 void idxd_device_wqs_clear_state(struct idxd_device *idxd)
516 lockdep_assert_held(&idxd->dev_lock);
518 for (i = 0; i < idxd->max_wqs; i++) {
519 struct idxd_wq *wq = &idxd->wqs[i];
521 if (wq->state == IDXD_WQ_ENABLED) {
522 idxd_wq_disable_cleanup(wq);
523 wq->state = IDXD_WQ_DISABLED;
528 int idxd_device_disable(struct idxd_device *idxd)
530 struct device *dev = &idxd->pdev->dev;
534 if (!idxd_is_enabled(idxd)) {
535 dev_dbg(dev, "Device is not enabled\n");
539 idxd_cmd_exec(idxd, IDXD_CMD_DISABLE_DEVICE, 0, &status);
541 /* If the command is successful or if the device was disabled */
542 if (status != IDXD_CMDSTS_SUCCESS &&
543 !(status & IDXD_CMDSTS_ERR_DIS_DEV_EN)) {
544 dev_dbg(dev, "%s: err_code: %#x\n", __func__, status);
548 spin_lock_irqsave(&idxd->dev_lock, flags);
549 idxd_device_wqs_clear_state(idxd);
550 idxd->state = IDXD_DEV_CONF_READY;
551 spin_unlock_irqrestore(&idxd->dev_lock, flags);
555 void idxd_device_reset(struct idxd_device *idxd)
559 idxd_cmd_exec(idxd, IDXD_CMD_RESET_DEVICE, 0, NULL);
560 spin_lock_irqsave(&idxd->dev_lock, flags);
561 idxd_device_wqs_clear_state(idxd);
562 idxd->state = IDXD_DEV_CONF_READY;
563 spin_unlock_irqrestore(&idxd->dev_lock, flags);
566 void idxd_device_drain_pasid(struct idxd_device *idxd, int pasid)
568 struct device *dev = &idxd->pdev->dev;
572 dev_dbg(dev, "cmd: %u operand: %#x\n", IDXD_CMD_DRAIN_PASID, operand);
573 idxd_cmd_exec(idxd, IDXD_CMD_DRAIN_PASID, operand, NULL);
574 dev_dbg(dev, "pasid %d drained\n", pasid);
577 /* Device configuration bits */
578 static void idxd_group_config_write(struct idxd_group *group)
580 struct idxd_device *idxd = group->idxd;
581 struct device *dev = &idxd->pdev->dev;
585 dev_dbg(dev, "Writing group %d cfg registers\n", group->id);
588 for (i = 0; i < GRPWQCFG_STRIDES; i++) {
589 grpcfg_offset = GRPWQCFG_OFFSET(idxd, group->id, i);
590 iowrite64(group->grpcfg.wqs[i], idxd->reg_base + grpcfg_offset);
591 dev_dbg(dev, "GRPCFG wq[%d:%d: %#x]: %#llx\n",
592 group->id, i, grpcfg_offset,
593 ioread64(idxd->reg_base + grpcfg_offset));
596 /* setup GRPENGCFG */
597 grpcfg_offset = GRPENGCFG_OFFSET(idxd, group->id);
598 iowrite64(group->grpcfg.engines, idxd->reg_base + grpcfg_offset);
599 dev_dbg(dev, "GRPCFG engs[%d: %#x]: %#llx\n", group->id,
600 grpcfg_offset, ioread64(idxd->reg_base + grpcfg_offset));
603 grpcfg_offset = GRPFLGCFG_OFFSET(idxd, group->id);
604 iowrite32(group->grpcfg.flags.bits, idxd->reg_base + grpcfg_offset);
605 dev_dbg(dev, "GRPFLAGS flags[%d: %#x]: %#x\n",
606 group->id, grpcfg_offset,
607 ioread32(idxd->reg_base + grpcfg_offset));
610 static int idxd_groups_config_write(struct idxd_device *idxd)
613 union gencfg_reg reg;
615 struct device *dev = &idxd->pdev->dev;
617 /* Setup bandwidth token limit */
618 if (idxd->token_limit) {
619 reg.bits = ioread32(idxd->reg_base + IDXD_GENCFG_OFFSET);
620 reg.token_limit = idxd->token_limit;
621 iowrite32(reg.bits, idxd->reg_base + IDXD_GENCFG_OFFSET);
624 dev_dbg(dev, "GENCFG(%#x): %#x\n", IDXD_GENCFG_OFFSET,
625 ioread32(idxd->reg_base + IDXD_GENCFG_OFFSET));
627 for (i = 0; i < idxd->max_groups; i++) {
628 struct idxd_group *group = &idxd->groups[i];
630 idxd_group_config_write(group);
636 static int idxd_wq_config_write(struct idxd_wq *wq)
638 struct idxd_device *idxd = wq->idxd;
639 struct device *dev = &idxd->pdev->dev;
646 memset(wq->wqcfg, 0, idxd->wqcfg_size);
649 wq->wqcfg->wq_size = wq->size;
652 dev_warn(dev, "Incorrect work queue size: 0\n");
657 wq->wqcfg->wq_thresh = wq->threshold;
660 wq->wqcfg->priv = !!(wq->type == IDXD_WQT_KERNEL);
661 if (wq_dedicated(wq))
664 if (device_pasid_enabled(idxd)) {
665 wq->wqcfg->pasid_en = 1;
666 if (wq->type == IDXD_WQT_KERNEL && wq_dedicated(wq))
667 wq->wqcfg->pasid = idxd->pasid;
670 wq->wqcfg->priority = wq->priority;
672 if (idxd->hw.gen_cap.block_on_fault &&
673 test_bit(WQ_FLAG_BLOCK_ON_FAULT, &wq->flags))
676 if (idxd->hw.wq_cap.wq_ats_support)
677 wq->wqcfg->wq_ats_disable = wq->ats_dis;
680 wq->wqcfg->max_xfer_shift = ilog2(wq->max_xfer_bytes);
681 wq->wqcfg->max_batch_shift = ilog2(wq->max_batch_size);
683 dev_dbg(dev, "WQ %d CFGs\n", wq->id);
684 for (i = 0; i < WQCFG_STRIDES(idxd); i++) {
685 wq_offset = WQCFG_OFFSET(idxd, wq->id, i);
686 iowrite32(wq->wqcfg->bits[i], idxd->reg_base + wq_offset);
687 dev_dbg(dev, "WQ[%d][%d][%#x]: %#x\n",
688 wq->id, i, wq_offset,
689 ioread32(idxd->reg_base + wq_offset));
695 static int idxd_wqs_config_write(struct idxd_device *idxd)
699 for (i = 0; i < idxd->max_wqs; i++) {
700 struct idxd_wq *wq = &idxd->wqs[i];
702 rc = idxd_wq_config_write(wq);
710 static void idxd_group_flags_setup(struct idxd_device *idxd)
714 /* TC-A 0 and TC-B 1 should be defaults */
715 for (i = 0; i < idxd->max_groups; i++) {
716 struct idxd_group *group = &idxd->groups[i];
718 if (group->tc_a == -1)
719 group->tc_a = group->grpcfg.flags.tc_a = 0;
721 group->grpcfg.flags.tc_a = group->tc_a;
722 if (group->tc_b == -1)
723 group->tc_b = group->grpcfg.flags.tc_b = 1;
725 group->grpcfg.flags.tc_b = group->tc_b;
726 group->grpcfg.flags.use_token_limit = group->use_token_limit;
727 group->grpcfg.flags.tokens_reserved = group->tokens_reserved;
728 if (group->tokens_allowed)
729 group->grpcfg.flags.tokens_allowed =
730 group->tokens_allowed;
732 group->grpcfg.flags.tokens_allowed = idxd->max_tokens;
736 static int idxd_engines_setup(struct idxd_device *idxd)
739 struct idxd_engine *eng;
740 struct idxd_group *group;
742 for (i = 0; i < idxd->max_groups; i++) {
743 group = &idxd->groups[i];
744 group->grpcfg.engines = 0;
747 for (i = 0; i < idxd->max_engines; i++) {
748 eng = &idxd->engines[i];
754 group->grpcfg.engines |= BIT(eng->id);
764 static int idxd_wqs_setup(struct idxd_device *idxd)
767 struct idxd_group *group;
768 int i, j, configured = 0;
769 struct device *dev = &idxd->pdev->dev;
771 for (i = 0; i < idxd->max_groups; i++) {
772 group = &idxd->groups[i];
773 for (j = 0; j < 4; j++)
774 group->grpcfg.wqs[j] = 0;
777 for (i = 0; i < idxd->max_wqs; i++) {
786 if (wq_shared(wq) && !device_swq_supported(idxd)) {
787 dev_warn(dev, "No shared wq support but configured.\n");
791 group->grpcfg.wqs[wq->id / 64] |= BIT(wq->id % 64);
801 int idxd_device_config(struct idxd_device *idxd)
805 lockdep_assert_held(&idxd->dev_lock);
806 rc = idxd_wqs_setup(idxd);
810 rc = idxd_engines_setup(idxd);
814 idxd_group_flags_setup(idxd);
816 rc = idxd_wqs_config_write(idxd);
820 rc = idxd_groups_config_write(idxd);