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,
18 static void idxd_device_wqs_clear_state(struct idxd_device *idxd);
19 static void idxd_wq_disable_cleanup(struct idxd_wq *wq);
21 /* Interrupt control bits */
22 void idxd_mask_msix_vector(struct idxd_device *idxd, int vec_id)
24 struct irq_data *data = irq_get_irq_data(idxd->irq_entries[vec_id].vector);
26 pci_msi_mask_irq(data);
29 void idxd_mask_msix_vectors(struct idxd_device *idxd)
31 struct pci_dev *pdev = idxd->pdev;
32 int msixcnt = pci_msix_vec_count(pdev);
35 for (i = 0; i < msixcnt; i++)
36 idxd_mask_msix_vector(idxd, i);
39 void idxd_unmask_msix_vector(struct idxd_device *idxd, int vec_id)
41 struct irq_data *data = irq_get_irq_data(idxd->irq_entries[vec_id].vector);
43 pci_msi_unmask_irq(data);
46 void idxd_unmask_error_interrupts(struct idxd_device *idxd)
48 union genctrl_reg genctrl;
50 genctrl.bits = ioread32(idxd->reg_base + IDXD_GENCTRL_OFFSET);
51 genctrl.softerr_int_en = 1;
52 genctrl.halt_int_en = 1;
53 iowrite32(genctrl.bits, idxd->reg_base + IDXD_GENCTRL_OFFSET);
56 void idxd_mask_error_interrupts(struct idxd_device *idxd)
58 union genctrl_reg genctrl;
60 genctrl.bits = ioread32(idxd->reg_base + IDXD_GENCTRL_OFFSET);
61 genctrl.softerr_int_en = 0;
62 genctrl.halt_int_en = 0;
63 iowrite32(genctrl.bits, idxd->reg_base + IDXD_GENCTRL_OFFSET);
66 static void free_hw_descs(struct idxd_wq *wq)
70 for (i = 0; i < wq->num_descs; i++)
71 kfree(wq->hw_descs[i]);
76 static int alloc_hw_descs(struct idxd_wq *wq, int num)
78 struct device *dev = &wq->idxd->pdev->dev;
80 int node = dev_to_node(dev);
82 wq->hw_descs = kcalloc_node(num, sizeof(struct dsa_hw_desc *),
87 for (i = 0; i < num; i++) {
88 wq->hw_descs[i] = kzalloc_node(sizeof(*wq->hw_descs[i]),
90 if (!wq->hw_descs[i]) {
99 static void free_descs(struct idxd_wq *wq)
103 for (i = 0; i < wq->num_descs; i++)
109 static int alloc_descs(struct idxd_wq *wq, int num)
111 struct device *dev = &wq->idxd->pdev->dev;
113 int node = dev_to_node(dev);
115 wq->descs = kcalloc_node(num, sizeof(struct idxd_desc *),
120 for (i = 0; i < num; i++) {
121 wq->descs[i] = kzalloc_node(sizeof(*wq->descs[i]),
132 /* WQ control bits */
133 int idxd_wq_alloc_resources(struct idxd_wq *wq)
135 struct idxd_device *idxd = wq->idxd;
136 struct device *dev = &idxd->pdev->dev;
137 int rc, num_descs, i;
141 if (wq->type != IDXD_WQT_KERNEL)
144 num_descs = wq_dedicated(wq) ? wq->size : wq->threshold;
145 wq->num_descs = num_descs;
147 rc = alloc_hw_descs(wq, num_descs);
151 align = idxd->data->align;
152 wq->compls_size = num_descs * idxd->data->compl_size + align;
153 wq->compls_raw = dma_alloc_coherent(dev, wq->compls_size,
154 &wq->compls_addr_raw, GFP_KERNEL);
155 if (!wq->compls_raw) {
157 goto fail_alloc_compls;
160 /* Adjust alignment */
161 wq->compls_addr = (wq->compls_addr_raw + (align - 1)) & ~(align - 1);
162 tmp = (u64)wq->compls_raw;
163 tmp = (tmp + (align - 1)) & ~(align - 1);
164 wq->compls = (struct dsa_completion_record *)tmp;
166 rc = alloc_descs(wq, num_descs);
168 goto fail_alloc_descs;
170 rc = sbitmap_queue_init_node(&wq->sbq, num_descs, -1, false, GFP_KERNEL,
173 goto fail_sbitmap_init;
175 for (i = 0; i < num_descs; i++) {
176 struct idxd_desc *desc = wq->descs[i];
178 desc->hw = wq->hw_descs[i];
179 if (idxd->data->type == IDXD_TYPE_DSA)
180 desc->completion = &wq->compls[i];
181 else if (idxd->data->type == IDXD_TYPE_IAX)
182 desc->iax_completion = &wq->iax_compls[i];
183 desc->compl_dma = wq->compls_addr + idxd->data->compl_size * i;
194 dma_free_coherent(dev, wq->compls_size, wq->compls_raw,
195 wq->compls_addr_raw);
201 void idxd_wq_free_resources(struct idxd_wq *wq)
203 struct device *dev = &wq->idxd->pdev->dev;
205 if (wq->type != IDXD_WQT_KERNEL)
210 dma_free_coherent(dev, wq->compls_size, wq->compls_raw,
211 wq->compls_addr_raw);
212 sbitmap_queue_free(&wq->sbq);
215 int idxd_wq_enable(struct idxd_wq *wq)
217 struct idxd_device *idxd = wq->idxd;
218 struct device *dev = &idxd->pdev->dev;
221 if (wq->state == IDXD_WQ_ENABLED) {
222 dev_dbg(dev, "WQ %d already enabled\n", wq->id);
226 idxd_cmd_exec(idxd, IDXD_CMD_ENABLE_WQ, wq->id, &status);
228 if (status != IDXD_CMDSTS_SUCCESS &&
229 status != IDXD_CMDSTS_ERR_WQ_ENABLED) {
230 dev_dbg(dev, "WQ enable failed: %#x\n", status);
234 wq->state = IDXD_WQ_ENABLED;
235 dev_dbg(dev, "WQ %d enabled\n", wq->id);
239 int idxd_wq_disable(struct idxd_wq *wq, bool reset_config)
241 struct idxd_device *idxd = wq->idxd;
242 struct device *dev = &idxd->pdev->dev;
245 dev_dbg(dev, "Disabling WQ %d\n", wq->id);
247 if (wq->state != IDXD_WQ_ENABLED) {
248 dev_dbg(dev, "WQ %d in wrong state: %d\n", wq->id, wq->state);
252 operand = BIT(wq->id % 16) | ((wq->id / 16) << 16);
253 idxd_cmd_exec(idxd, IDXD_CMD_DISABLE_WQ, operand, &status);
255 if (status != IDXD_CMDSTS_SUCCESS) {
256 dev_dbg(dev, "WQ disable failed: %#x\n", status);
261 idxd_wq_disable_cleanup(wq);
262 wq->state = IDXD_WQ_DISABLED;
263 dev_dbg(dev, "WQ %d disabled\n", wq->id);
267 void idxd_wq_drain(struct idxd_wq *wq)
269 struct idxd_device *idxd = wq->idxd;
270 struct device *dev = &idxd->pdev->dev;
273 if (wq->state != IDXD_WQ_ENABLED) {
274 dev_dbg(dev, "WQ %d in wrong state: %d\n", wq->id, wq->state);
278 dev_dbg(dev, "Draining WQ %d\n", wq->id);
279 operand = BIT(wq->id % 16) | ((wq->id / 16) << 16);
280 idxd_cmd_exec(idxd, IDXD_CMD_DRAIN_WQ, operand, NULL);
283 void idxd_wq_reset(struct idxd_wq *wq)
285 struct idxd_device *idxd = wq->idxd;
286 struct device *dev = &idxd->pdev->dev;
289 if (wq->state != IDXD_WQ_ENABLED) {
290 dev_dbg(dev, "WQ %d in wrong state: %d\n", wq->id, wq->state);
294 operand = BIT(wq->id % 16) | ((wq->id / 16) << 16);
295 idxd_cmd_exec(idxd, IDXD_CMD_RESET_WQ, operand, NULL);
296 idxd_wq_disable_cleanup(wq);
297 wq->state = IDXD_WQ_DISABLED;
300 int idxd_wq_map_portal(struct idxd_wq *wq)
302 struct idxd_device *idxd = wq->idxd;
303 struct pci_dev *pdev = idxd->pdev;
304 struct device *dev = &pdev->dev;
305 resource_size_t start;
307 start = pci_resource_start(pdev, IDXD_WQ_BAR);
308 start += idxd_get_wq_portal_full_offset(wq->id, IDXD_PORTAL_LIMITED);
310 wq->portal = devm_ioremap(dev, start, IDXD_PORTAL_SIZE);
317 void idxd_wq_unmap_portal(struct idxd_wq *wq)
319 struct device *dev = &wq->idxd->pdev->dev;
321 devm_iounmap(dev, wq->portal);
323 wq->portal_offset = 0;
326 void idxd_wqs_unmap_portal(struct idxd_device *idxd)
330 for (i = 0; i < idxd->max_wqs; i++) {
331 struct idxd_wq *wq = idxd->wqs[i];
334 idxd_wq_unmap_portal(wq);
338 int idxd_wq_set_pasid(struct idxd_wq *wq, int pasid)
340 struct idxd_device *idxd = wq->idxd;
346 rc = idxd_wq_disable(wq, false);
350 offset = WQCFG_OFFSET(idxd, wq->id, WQCFG_PASID_IDX);
351 spin_lock_irqsave(&idxd->dev_lock, flags);
352 wqcfg.bits[WQCFG_PASID_IDX] = ioread32(idxd->reg_base + offset);
355 iowrite32(wqcfg.bits[WQCFG_PASID_IDX], idxd->reg_base + offset);
356 spin_unlock_irqrestore(&idxd->dev_lock, flags);
358 rc = idxd_wq_enable(wq);
365 int idxd_wq_disable_pasid(struct idxd_wq *wq)
367 struct idxd_device *idxd = wq->idxd;
373 rc = idxd_wq_disable(wq, false);
377 offset = WQCFG_OFFSET(idxd, wq->id, WQCFG_PASID_IDX);
378 spin_lock_irqsave(&idxd->dev_lock, flags);
379 wqcfg.bits[WQCFG_PASID_IDX] = ioread32(idxd->reg_base + offset);
382 iowrite32(wqcfg.bits[WQCFG_PASID_IDX], idxd->reg_base + offset);
383 spin_unlock_irqrestore(&idxd->dev_lock, flags);
385 rc = idxd_wq_enable(wq);
392 static void idxd_wq_disable_cleanup(struct idxd_wq *wq)
394 struct idxd_device *idxd = wq->idxd;
396 lockdep_assert_held(&wq->wq_lock);
397 memset(wq->wqcfg, 0, idxd->wqcfg_size);
398 wq->type = IDXD_WQT_NONE;
404 clear_bit(WQ_FLAG_DEDICATED, &wq->flags);
405 clear_bit(WQ_FLAG_BLOCK_ON_FAULT, &wq->flags);
406 memset(wq->name, 0, WQ_NAME_SIZE);
409 static void idxd_wq_ref_release(struct percpu_ref *ref)
411 struct idxd_wq *wq = container_of(ref, struct idxd_wq, wq_active);
413 complete(&wq->wq_dead);
416 int idxd_wq_init_percpu_ref(struct idxd_wq *wq)
420 memset(&wq->wq_active, 0, sizeof(wq->wq_active));
421 rc = percpu_ref_init(&wq->wq_active, idxd_wq_ref_release, 0, GFP_KERNEL);
424 reinit_completion(&wq->wq_dead);
428 void idxd_wq_quiesce(struct idxd_wq *wq)
430 percpu_ref_kill(&wq->wq_active);
431 wait_for_completion(&wq->wq_dead);
432 percpu_ref_exit(&wq->wq_active);
435 /* Device control bits */
436 static inline bool idxd_is_enabled(struct idxd_device *idxd)
438 union gensts_reg gensts;
440 gensts.bits = ioread32(idxd->reg_base + IDXD_GENSTATS_OFFSET);
442 if (gensts.state == IDXD_DEVICE_STATE_ENABLED)
447 static inline bool idxd_device_is_halted(struct idxd_device *idxd)
449 union gensts_reg gensts;
451 gensts.bits = ioread32(idxd->reg_base + IDXD_GENSTATS_OFFSET);
453 return (gensts.state == IDXD_DEVICE_STATE_HALT);
457 * This is function is only used for reset during probe and will
458 * poll for completion. Once the device is setup with interrupts,
459 * all commands will be done via interrupt completion.
461 int idxd_device_init_reset(struct idxd_device *idxd)
463 struct device *dev = &idxd->pdev->dev;
464 union idxd_command_reg cmd;
466 if (idxd_device_is_halted(idxd)) {
467 dev_warn(&idxd->pdev->dev, "Device is HALTED!\n");
471 memset(&cmd, 0, sizeof(cmd));
472 cmd.cmd = IDXD_CMD_RESET_DEVICE;
473 dev_dbg(dev, "%s: sending reset for init.\n", __func__);
474 spin_lock(&idxd->cmd_lock);
475 iowrite32(cmd.bits, idxd->reg_base + IDXD_CMD_OFFSET);
477 while (ioread32(idxd->reg_base + IDXD_CMDSTS_OFFSET) &
480 spin_unlock(&idxd->cmd_lock);
484 static void idxd_cmd_exec(struct idxd_device *idxd, int cmd_code, u32 operand,
487 union idxd_command_reg cmd;
488 DECLARE_COMPLETION_ONSTACK(done);
491 if (idxd_device_is_halted(idxd)) {
492 dev_warn(&idxd->pdev->dev, "Device is HALTED!\n");
494 *status = IDXD_CMDSTS_HW_ERR;
498 memset(&cmd, 0, sizeof(cmd));
500 cmd.operand = operand;
503 spin_lock(&idxd->cmd_lock);
504 wait_event_lock_irq(idxd->cmd_waitq,
505 !test_bit(IDXD_FLAG_CMD_RUNNING, &idxd->flags),
508 dev_dbg(&idxd->pdev->dev, "%s: sending cmd: %#x op: %#x\n",
509 __func__, cmd_code, operand);
511 idxd->cmd_status = 0;
512 __set_bit(IDXD_FLAG_CMD_RUNNING, &idxd->flags);
513 idxd->cmd_done = &done;
514 iowrite32(cmd.bits, idxd->reg_base + IDXD_CMD_OFFSET);
517 * After command submitted, release lock and go to sleep until
518 * the command completes via interrupt.
520 spin_unlock(&idxd->cmd_lock);
521 wait_for_completion(&done);
522 stat = ioread32(idxd->reg_base + IDXD_CMDSTS_OFFSET);
523 spin_lock(&idxd->cmd_lock);
526 idxd->cmd_status = stat & GENMASK(7, 0);
528 __clear_bit(IDXD_FLAG_CMD_RUNNING, &idxd->flags);
529 /* Wake up other pending commands */
530 wake_up(&idxd->cmd_waitq);
531 spin_unlock(&idxd->cmd_lock);
534 int idxd_device_enable(struct idxd_device *idxd)
536 struct device *dev = &idxd->pdev->dev;
539 if (idxd_is_enabled(idxd)) {
540 dev_dbg(dev, "Device already enabled\n");
544 idxd_cmd_exec(idxd, IDXD_CMD_ENABLE_DEVICE, 0, &status);
546 /* If the command is successful or if the device was enabled */
547 if (status != IDXD_CMDSTS_SUCCESS &&
548 status != IDXD_CMDSTS_ERR_DEV_ENABLED) {
549 dev_dbg(dev, "%s: err_code: %#x\n", __func__, status);
553 idxd->state = IDXD_DEV_ENABLED;
557 int idxd_device_disable(struct idxd_device *idxd)
559 struct device *dev = &idxd->pdev->dev;
563 if (!idxd_is_enabled(idxd)) {
564 dev_dbg(dev, "Device is not enabled\n");
568 idxd_cmd_exec(idxd, IDXD_CMD_DISABLE_DEVICE, 0, &status);
570 /* If the command is successful or if the device was disabled */
571 if (status != IDXD_CMDSTS_SUCCESS &&
572 !(status & IDXD_CMDSTS_ERR_DIS_DEV_EN)) {
573 dev_dbg(dev, "%s: err_code: %#x\n", __func__, status);
577 spin_lock_irqsave(&idxd->dev_lock, flags);
578 idxd_device_clear_state(idxd);
579 idxd->state = IDXD_DEV_DISABLED;
580 spin_unlock_irqrestore(&idxd->dev_lock, flags);
584 void idxd_device_reset(struct idxd_device *idxd)
588 idxd_cmd_exec(idxd, IDXD_CMD_RESET_DEVICE, 0, NULL);
589 spin_lock_irqsave(&idxd->dev_lock, flags);
590 idxd_device_clear_state(idxd);
591 idxd->state = IDXD_DEV_DISABLED;
592 spin_unlock_irqrestore(&idxd->dev_lock, flags);
595 void idxd_device_drain_pasid(struct idxd_device *idxd, int pasid)
597 struct device *dev = &idxd->pdev->dev;
601 dev_dbg(dev, "cmd: %u operand: %#x\n", IDXD_CMD_DRAIN_PASID, operand);
602 idxd_cmd_exec(idxd, IDXD_CMD_DRAIN_PASID, operand, NULL);
603 dev_dbg(dev, "pasid %d drained\n", pasid);
606 int idxd_device_request_int_handle(struct idxd_device *idxd, int idx, int *handle,
607 enum idxd_interrupt_type irq_type)
609 struct device *dev = &idxd->pdev->dev;
612 if (!(idxd->hw.cmd_cap & BIT(IDXD_CMD_REQUEST_INT_HANDLE)))
615 dev_dbg(dev, "get int handle, idx %d\n", idx);
617 operand = idx & GENMASK(15, 0);
618 if (irq_type == IDXD_IRQ_IMS)
619 operand |= CMD_INT_HANDLE_IMS;
621 dev_dbg(dev, "cmd: %u operand: %#x\n", IDXD_CMD_REQUEST_INT_HANDLE, operand);
623 idxd_cmd_exec(idxd, IDXD_CMD_REQUEST_INT_HANDLE, operand, &status);
625 if ((status & IDXD_CMDSTS_ERR_MASK) != IDXD_CMDSTS_SUCCESS) {
626 dev_dbg(dev, "request int handle failed: %#x\n", status);
630 *handle = (status >> IDXD_CMDSTS_RES_SHIFT) & GENMASK(15, 0);
632 dev_dbg(dev, "int handle acquired: %u\n", *handle);
636 int idxd_device_release_int_handle(struct idxd_device *idxd, int handle,
637 enum idxd_interrupt_type irq_type)
639 struct device *dev = &idxd->pdev->dev;
641 union idxd_command_reg cmd;
643 if (!(idxd->hw.cmd_cap & BIT(IDXD_CMD_RELEASE_INT_HANDLE)))
646 dev_dbg(dev, "release int handle, handle %d\n", handle);
648 memset(&cmd, 0, sizeof(cmd));
649 operand = handle & GENMASK(15, 0);
651 if (irq_type == IDXD_IRQ_IMS)
652 operand |= CMD_INT_HANDLE_IMS;
654 cmd.cmd = IDXD_CMD_RELEASE_INT_HANDLE;
655 cmd.operand = operand;
657 dev_dbg(dev, "cmd: %u operand: %#x\n", IDXD_CMD_RELEASE_INT_HANDLE, operand);
659 spin_lock(&idxd->cmd_lock);
660 iowrite32(cmd.bits, idxd->reg_base + IDXD_CMD_OFFSET);
662 while (ioread32(idxd->reg_base + IDXD_CMDSTS_OFFSET) & IDXD_CMDSTS_ACTIVE)
664 status = ioread32(idxd->reg_base + IDXD_CMDSTS_OFFSET);
665 spin_unlock(&idxd->cmd_lock);
667 if ((status & IDXD_CMDSTS_ERR_MASK) != IDXD_CMDSTS_SUCCESS) {
668 dev_dbg(dev, "release int handle failed: %#x\n", status);
672 dev_dbg(dev, "int handle released.\n");
676 /* Device configuration bits */
677 static void idxd_engines_clear_state(struct idxd_device *idxd)
679 struct idxd_engine *engine;
682 lockdep_assert_held(&idxd->dev_lock);
683 for (i = 0; i < idxd->max_engines; i++) {
684 engine = idxd->engines[i];
685 engine->group = NULL;
689 static void idxd_groups_clear_state(struct idxd_device *idxd)
691 struct idxd_group *group;
694 lockdep_assert_held(&idxd->dev_lock);
695 for (i = 0; i < idxd->max_groups; i++) {
696 group = idxd->groups[i];
697 memset(&group->grpcfg, 0, sizeof(group->grpcfg));
698 group->num_engines = 0;
700 group->use_token_limit = false;
701 group->tokens_allowed = 0;
702 group->tokens_reserved = 0;
708 static void idxd_device_wqs_clear_state(struct idxd_device *idxd)
712 lockdep_assert_held(&idxd->dev_lock);
713 for (i = 0; i < idxd->max_wqs; i++) {
714 struct idxd_wq *wq = idxd->wqs[i];
716 if (wq->state == IDXD_WQ_ENABLED) {
717 idxd_wq_disable_cleanup(wq);
718 wq->state = IDXD_WQ_DISABLED;
723 void idxd_device_clear_state(struct idxd_device *idxd)
725 idxd_groups_clear_state(idxd);
726 idxd_engines_clear_state(idxd);
727 idxd_device_wqs_clear_state(idxd);
730 void idxd_msix_perm_setup(struct idxd_device *idxd)
732 union msix_perm mperm;
735 msixcnt = pci_msix_vec_count(idxd->pdev);
740 mperm.pasid = idxd->pasid;
741 mperm.pasid_en = device_pasid_enabled(idxd);
742 for (i = 1; i < msixcnt; i++)
743 iowrite32(mperm.bits, idxd->reg_base + idxd->msix_perm_offset + i * 8);
746 void idxd_msix_perm_clear(struct idxd_device *idxd)
748 union msix_perm mperm;
751 msixcnt = pci_msix_vec_count(idxd->pdev);
756 for (i = 1; i < msixcnt; i++)
757 iowrite32(mperm.bits, idxd->reg_base + idxd->msix_perm_offset + i * 8);
760 static void idxd_group_config_write(struct idxd_group *group)
762 struct idxd_device *idxd = group->idxd;
763 struct device *dev = &idxd->pdev->dev;
767 dev_dbg(dev, "Writing group %d cfg registers\n", group->id);
770 for (i = 0; i < GRPWQCFG_STRIDES; i++) {
771 grpcfg_offset = GRPWQCFG_OFFSET(idxd, group->id, i);
772 iowrite64(group->grpcfg.wqs[i], idxd->reg_base + grpcfg_offset);
773 dev_dbg(dev, "GRPCFG wq[%d:%d: %#x]: %#llx\n",
774 group->id, i, grpcfg_offset,
775 ioread64(idxd->reg_base + grpcfg_offset));
778 /* setup GRPENGCFG */
779 grpcfg_offset = GRPENGCFG_OFFSET(idxd, group->id);
780 iowrite64(group->grpcfg.engines, idxd->reg_base + grpcfg_offset);
781 dev_dbg(dev, "GRPCFG engs[%d: %#x]: %#llx\n", group->id,
782 grpcfg_offset, ioread64(idxd->reg_base + grpcfg_offset));
785 grpcfg_offset = GRPFLGCFG_OFFSET(idxd, group->id);
786 iowrite32(group->grpcfg.flags.bits, idxd->reg_base + grpcfg_offset);
787 dev_dbg(dev, "GRPFLAGS flags[%d: %#x]: %#x\n",
788 group->id, grpcfg_offset,
789 ioread32(idxd->reg_base + grpcfg_offset));
792 static int idxd_groups_config_write(struct idxd_device *idxd)
795 union gencfg_reg reg;
797 struct device *dev = &idxd->pdev->dev;
799 /* Setup bandwidth token limit */
800 if (idxd->token_limit) {
801 reg.bits = ioread32(idxd->reg_base + IDXD_GENCFG_OFFSET);
802 reg.token_limit = idxd->token_limit;
803 iowrite32(reg.bits, idxd->reg_base + IDXD_GENCFG_OFFSET);
806 dev_dbg(dev, "GENCFG(%#x): %#x\n", IDXD_GENCFG_OFFSET,
807 ioread32(idxd->reg_base + IDXD_GENCFG_OFFSET));
809 for (i = 0; i < idxd->max_groups; i++) {
810 struct idxd_group *group = idxd->groups[i];
812 idxd_group_config_write(group);
818 static bool idxd_device_pasid_priv_enabled(struct idxd_device *idxd)
820 struct pci_dev *pdev = idxd->pdev;
822 if (pdev->pasid_enabled && (pdev->pasid_features & PCI_PASID_CAP_PRIV))
827 static int idxd_wq_config_write(struct idxd_wq *wq)
829 struct idxd_device *idxd = wq->idxd;
830 struct device *dev = &idxd->pdev->dev;
838 * Instead of memset the entire shadow copy of WQCFG, copy from the hardware after
839 * wq reset. This will copy back the sticky values that are present on some devices.
841 for (i = 0; i < WQCFG_STRIDES(idxd); i++) {
842 wq_offset = WQCFG_OFFSET(idxd, wq->id, i);
843 wq->wqcfg->bits[i] = ioread32(idxd->reg_base + wq_offset);
847 wq->wqcfg->wq_size = wq->size;
850 idxd->cmd_status = IDXD_SCMD_WQ_NO_SIZE;
851 dev_warn(dev, "Incorrect work queue size: 0\n");
856 wq->wqcfg->wq_thresh = wq->threshold;
859 if (wq_dedicated(wq))
862 if (device_pasid_enabled(idxd)) {
863 wq->wqcfg->pasid_en = 1;
864 if (wq->type == IDXD_WQT_KERNEL && wq_dedicated(wq))
865 wq->wqcfg->pasid = idxd->pasid;
869 * Here the priv bit is set depending on the WQ type. priv = 1 if the
870 * WQ type is kernel to indicate privileged access. This setting only
871 * matters for dedicated WQ. According to the DSA spec:
872 * If the WQ is in dedicated mode, WQ PASID Enable is 1, and the
873 * Privileged Mode Enable field of the PCI Express PASID capability
874 * is 0, this field must be 0.
876 * In the case of a dedicated kernel WQ that is not able to support
877 * the PASID cap, then the configuration will be rejected.
879 wq->wqcfg->priv = !!(wq->type == IDXD_WQT_KERNEL);
880 if (wq_dedicated(wq) && wq->wqcfg->pasid_en &&
881 !idxd_device_pasid_priv_enabled(idxd) &&
882 wq->type == IDXD_WQT_KERNEL) {
883 idxd->cmd_status = IDXD_SCMD_WQ_NO_PRIV;
887 wq->wqcfg->priority = wq->priority;
889 if (idxd->hw.gen_cap.block_on_fault &&
890 test_bit(WQ_FLAG_BLOCK_ON_FAULT, &wq->flags))
893 if (idxd->hw.wq_cap.wq_ats_support)
894 wq->wqcfg->wq_ats_disable = wq->ats_dis;
897 wq->wqcfg->max_xfer_shift = ilog2(wq->max_xfer_bytes);
898 wq->wqcfg->max_batch_shift = ilog2(wq->max_batch_size);
900 dev_dbg(dev, "WQ %d CFGs\n", wq->id);
901 for (i = 0; i < WQCFG_STRIDES(idxd); i++) {
902 wq_offset = WQCFG_OFFSET(idxd, wq->id, i);
903 iowrite32(wq->wqcfg->bits[i], idxd->reg_base + wq_offset);
904 dev_dbg(dev, "WQ[%d][%d][%#x]: %#x\n",
905 wq->id, i, wq_offset,
906 ioread32(idxd->reg_base + wq_offset));
912 static int idxd_wqs_config_write(struct idxd_device *idxd)
916 for (i = 0; i < idxd->max_wqs; i++) {
917 struct idxd_wq *wq = idxd->wqs[i];
919 rc = idxd_wq_config_write(wq);
927 static void idxd_group_flags_setup(struct idxd_device *idxd)
931 /* TC-A 0 and TC-B 1 should be defaults */
932 for (i = 0; i < idxd->max_groups; i++) {
933 struct idxd_group *group = idxd->groups[i];
935 if (group->tc_a == -1)
936 group->tc_a = group->grpcfg.flags.tc_a = 0;
938 group->grpcfg.flags.tc_a = group->tc_a;
939 if (group->tc_b == -1)
940 group->tc_b = group->grpcfg.flags.tc_b = 1;
942 group->grpcfg.flags.tc_b = group->tc_b;
943 group->grpcfg.flags.use_token_limit = group->use_token_limit;
944 group->grpcfg.flags.tokens_reserved = group->tokens_reserved;
945 if (group->tokens_allowed)
946 group->grpcfg.flags.tokens_allowed =
947 group->tokens_allowed;
949 group->grpcfg.flags.tokens_allowed = idxd->max_tokens;
953 static int idxd_engines_setup(struct idxd_device *idxd)
956 struct idxd_engine *eng;
957 struct idxd_group *group;
959 for (i = 0; i < idxd->max_groups; i++) {
960 group = idxd->groups[i];
961 group->grpcfg.engines = 0;
964 for (i = 0; i < idxd->max_engines; i++) {
965 eng = idxd->engines[i];
971 group->grpcfg.engines |= BIT(eng->id);
981 static int idxd_wqs_setup(struct idxd_device *idxd)
984 struct idxd_group *group;
985 int i, j, configured = 0;
986 struct device *dev = &idxd->pdev->dev;
988 for (i = 0; i < idxd->max_groups; i++) {
989 group = idxd->groups[i];
990 for (j = 0; j < 4; j++)
991 group->grpcfg.wqs[j] = 0;
994 for (i = 0; i < idxd->max_wqs; i++) {
1003 if (wq_shared(wq) && !device_swq_supported(idxd)) {
1004 idxd->cmd_status = IDXD_SCMD_WQ_NO_SWQ_SUPPORT;
1005 dev_warn(dev, "No shared wq support but configured.\n");
1009 group->grpcfg.wqs[wq->id / 64] |= BIT(wq->id % 64);
1013 if (configured == 0) {
1014 idxd->cmd_status = IDXD_SCMD_WQ_NONE_CONFIGURED;
1021 int idxd_device_config(struct idxd_device *idxd)
1025 lockdep_assert_held(&idxd->dev_lock);
1026 rc = idxd_wqs_setup(idxd);
1030 rc = idxd_engines_setup(idxd);
1034 idxd_group_flags_setup(idxd);
1036 rc = idxd_wqs_config_write(idxd);
1040 rc = idxd_groups_config_write(idxd);
1047 static int idxd_wq_load_config(struct idxd_wq *wq)
1049 struct idxd_device *idxd = wq->idxd;
1050 struct device *dev = &idxd->pdev->dev;
1054 wqcfg_offset = WQCFG_OFFSET(idxd, wq->id, 0);
1055 memcpy_fromio(wq->wqcfg, idxd->reg_base + wqcfg_offset, idxd->wqcfg_size);
1057 wq->size = wq->wqcfg->wq_size;
1058 wq->threshold = wq->wqcfg->wq_thresh;
1059 if (wq->wqcfg->priv)
1060 wq->type = IDXD_WQT_KERNEL;
1062 /* The driver does not support shared WQ mode in read-only config yet */
1063 if (wq->wqcfg->mode == 0 || wq->wqcfg->pasid_en)
1066 set_bit(WQ_FLAG_DEDICATED, &wq->flags);
1068 wq->priority = wq->wqcfg->priority;
1070 for (i = 0; i < WQCFG_STRIDES(idxd); i++) {
1071 wqcfg_offset = WQCFG_OFFSET(idxd, wq->id, i);
1072 dev_dbg(dev, "WQ[%d][%d][%#x]: %#x\n", wq->id, i, wqcfg_offset, wq->wqcfg->bits[i]);
1078 static void idxd_group_load_config(struct idxd_group *group)
1080 struct idxd_device *idxd = group->idxd;
1081 struct device *dev = &idxd->pdev->dev;
1082 int i, j, grpcfg_offset;
1085 * Load WQS bit fields
1086 * Iterate through all 256 bits 64 bits at a time
1088 for (i = 0; i < GRPWQCFG_STRIDES; i++) {
1091 grpcfg_offset = GRPWQCFG_OFFSET(idxd, group->id, i);
1092 group->grpcfg.wqs[i] = ioread64(idxd->reg_base + grpcfg_offset);
1093 dev_dbg(dev, "GRPCFG wq[%d:%d: %#x]: %#llx\n",
1094 group->id, i, grpcfg_offset, group->grpcfg.wqs[i]);
1096 if (i * 64 >= idxd->max_wqs)
1099 /* Iterate through all 64 bits and check for wq set */
1100 for (j = 0; j < 64; j++) {
1101 int id = i * 64 + j;
1103 /* No need to check beyond max wqs */
1104 if (id >= idxd->max_wqs)
1107 /* Set group assignment for wq if wq bit is set */
1108 if (group->grpcfg.wqs[i] & BIT(j)) {
1115 grpcfg_offset = GRPENGCFG_OFFSET(idxd, group->id);
1116 group->grpcfg.engines = ioread64(idxd->reg_base + grpcfg_offset);
1117 dev_dbg(dev, "GRPCFG engs[%d: %#x]: %#llx\n", group->id,
1118 grpcfg_offset, group->grpcfg.engines);
1120 /* Iterate through all 64 bits to check engines set */
1121 for (i = 0; i < 64; i++) {
1122 if (i >= idxd->max_engines)
1125 if (group->grpcfg.engines & BIT(i)) {
1126 struct idxd_engine *engine = idxd->engines[i];
1128 engine->group = group;
1132 grpcfg_offset = GRPFLGCFG_OFFSET(idxd, group->id);
1133 group->grpcfg.flags.bits = ioread32(idxd->reg_base + grpcfg_offset);
1134 dev_dbg(dev, "GRPFLAGS flags[%d: %#x]: %#x\n",
1135 group->id, grpcfg_offset, group->grpcfg.flags.bits);
1138 int idxd_device_load_config(struct idxd_device *idxd)
1140 union gencfg_reg reg;
1143 reg.bits = ioread32(idxd->reg_base + IDXD_GENCFG_OFFSET);
1144 idxd->token_limit = reg.token_limit;
1146 for (i = 0; i < idxd->max_groups; i++) {
1147 struct idxd_group *group = idxd->groups[i];
1149 idxd_group_load_config(group);
1152 for (i = 0; i < idxd->max_wqs; i++) {
1153 struct idxd_wq *wq = idxd->wqs[i];
1155 rc = idxd_wq_load_config(wq);
1163 int __drv_enable_wq(struct idxd_wq *wq)
1165 struct idxd_device *idxd = wq->idxd;
1166 struct device *dev = &idxd->pdev->dev;
1167 unsigned long flags;
1170 lockdep_assert_held(&wq->wq_lock);
1172 if (idxd->state != IDXD_DEV_ENABLED) {
1173 idxd->cmd_status = IDXD_SCMD_DEV_NOT_ENABLED;
1177 if (wq->state != IDXD_WQ_DISABLED) {
1178 dev_dbg(dev, "wq %d already enabled.\n", wq->id);
1179 idxd->cmd_status = IDXD_SCMD_WQ_ENABLED;
1185 dev_dbg(dev, "wq %d not attached to group.\n", wq->id);
1186 idxd->cmd_status = IDXD_SCMD_WQ_NO_GRP;
1190 if (strlen(wq->name) == 0) {
1191 idxd->cmd_status = IDXD_SCMD_WQ_NO_NAME;
1192 dev_dbg(dev, "wq %d name not set.\n", wq->id);
1196 /* Shared WQ checks */
1197 if (wq_shared(wq)) {
1198 if (!device_swq_supported(idxd)) {
1199 idxd->cmd_status = IDXD_SCMD_WQ_NO_SVM;
1200 dev_dbg(dev, "PASID not enabled and shared wq.\n");
1204 * Shared wq with the threshold set to 0 means the user
1205 * did not set the threshold or transitioned from a
1206 * dedicated wq but did not set threshold. A value
1207 * of 0 would effectively disable the shared wq. The
1208 * driver does not allow a value of 0 to be set for
1209 * threshold via sysfs.
1211 if (wq->threshold == 0) {
1212 idxd->cmd_status = IDXD_SCMD_WQ_NO_THRESH;
1213 dev_dbg(dev, "Shared wq and threshold 0.\n");
1219 spin_lock_irqsave(&idxd->dev_lock, flags);
1220 if (test_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags))
1221 rc = idxd_device_config(idxd);
1222 spin_unlock_irqrestore(&idxd->dev_lock, flags);
1224 dev_dbg(dev, "Writing wq %d config failed: %d\n", wq->id, rc);
1228 rc = idxd_wq_enable(wq);
1230 dev_dbg(dev, "wq %d enabling failed: %d\n", wq->id, rc);
1234 rc = idxd_wq_map_portal(wq);
1236 idxd->cmd_status = IDXD_SCMD_WQ_PORTAL_ERR;
1237 dev_dbg(dev, "wq %d portal mapping failed: %d\n", wq->id, rc);
1238 goto err_map_portal;
1241 wq->client_count = 0;
1245 rc = idxd_wq_disable(wq, false);
1247 dev_dbg(dev, "wq %s disable failed\n", dev_name(wq_confdev(wq)));
1252 int drv_enable_wq(struct idxd_wq *wq)
1256 mutex_lock(&wq->wq_lock);
1257 rc = __drv_enable_wq(wq);
1258 mutex_unlock(&wq->wq_lock);
1262 void __drv_disable_wq(struct idxd_wq *wq)
1264 struct idxd_device *idxd = wq->idxd;
1265 struct device *dev = &idxd->pdev->dev;
1267 lockdep_assert_held(&wq->wq_lock);
1269 if (idxd_wq_refcount(wq))
1270 dev_warn(dev, "Clients has claim on wq %d: %d\n",
1271 wq->id, idxd_wq_refcount(wq));
1273 idxd_wq_unmap_portal(wq);
1278 wq->client_count = 0;
1281 void drv_disable_wq(struct idxd_wq *wq)
1283 mutex_lock(&wq->wq_lock);
1284 __drv_disable_wq(wq);
1285 mutex_unlock(&wq->wq_lock);
1288 int idxd_device_drv_probe(struct idxd_dev *idxd_dev)
1290 struct idxd_device *idxd = idxd_dev_to_idxd(idxd_dev);
1291 unsigned long flags;
1295 * Device should be in disabled state for the idxd_drv to load. If it's in
1296 * enabled state, then the device was altered outside of driver's control.
1297 * If the state is in halted state, then we don't want to proceed.
1299 if (idxd->state != IDXD_DEV_DISABLED) {
1300 idxd->cmd_status = IDXD_SCMD_DEV_ENABLED;
1304 /* Device configuration */
1305 spin_lock_irqsave(&idxd->dev_lock, flags);
1306 if (test_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags))
1307 rc = idxd_device_config(idxd);
1308 spin_unlock_irqrestore(&idxd->dev_lock, flags);
1313 rc = idxd_device_enable(idxd);
1317 /* Setup DMA device without channels */
1318 rc = idxd_register_dma_device(idxd);
1320 idxd_device_disable(idxd);
1321 idxd->cmd_status = IDXD_SCMD_DEV_DMA_ERR;
1325 idxd->cmd_status = 0;
1329 void idxd_device_drv_remove(struct idxd_dev *idxd_dev)
1331 struct device *dev = &idxd_dev->conf_dev;
1332 struct idxd_device *idxd = idxd_dev_to_idxd(idxd_dev);
1335 for (i = 0; i < idxd->max_wqs; i++) {
1336 struct idxd_wq *wq = idxd->wqs[i];
1337 struct device *wq_dev = wq_confdev(wq);
1339 if (wq->state == IDXD_WQ_DISABLED)
1341 dev_warn(dev, "Active wq %d on disable %s.\n", i, dev_name(wq_dev));
1342 device_release_driver(wq_dev);
1345 idxd_unregister_dma_device(idxd);
1346 idxd_device_disable(idxd);
1347 if (test_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags))
1348 idxd_device_reset(idxd);
1351 static enum idxd_dev_type dev_types[] = {
1357 struct idxd_device_driver idxd_drv = {
1359 .probe = idxd_device_drv_probe,
1360 .remove = idxd_device_drv_remove,
1363 EXPORT_SYMBOL_GPL(idxd_drv);