Merge tag 'io_uring-5.15-2021-09-11' of git://git.kernel.dk/linux-block
[linux-2.6-microblaze.git] / drivers / dma / idxd / device.c
index 8d8e249..83a5ff2 100644 (file)
@@ -141,8 +141,8 @@ int idxd_wq_alloc_resources(struct idxd_wq *wq)
        if (wq->type != IDXD_WQT_KERNEL)
                return 0;
 
-       wq->num_descs = wq->size;
-       num_descs = wq->size;
+       num_descs = wq_dedicated(wq) ? wq->size : wq->threshold;
+       wq->num_descs = num_descs;
 
        rc = alloc_hw_descs(wq, num_descs);
        if (rc < 0)
@@ -320,6 +320,7 @@ void idxd_wq_unmap_portal(struct idxd_wq *wq)
 
        devm_iounmap(dev, wq->portal);
        wq->portal = NULL;
+       wq->portal_offset = 0;
 }
 
 void idxd_wqs_unmap_portal(struct idxd_device *idxd)
@@ -340,19 +341,18 @@ int idxd_wq_set_pasid(struct idxd_wq *wq, int pasid)
        int rc;
        union wqcfg wqcfg;
        unsigned int offset;
-       unsigned long flags;
 
        rc = idxd_wq_disable(wq, false);
        if (rc < 0)
                return rc;
 
        offset = WQCFG_OFFSET(idxd, wq->id, WQCFG_PASID_IDX);
-       spin_lock_irqsave(&idxd->dev_lock, flags);
+       spin_lock(&idxd->dev_lock);
        wqcfg.bits[WQCFG_PASID_IDX] = ioread32(idxd->reg_base + offset);
        wqcfg.pasid_en = 1;
        wqcfg.pasid = pasid;
        iowrite32(wqcfg.bits[WQCFG_PASID_IDX], idxd->reg_base + offset);
-       spin_unlock_irqrestore(&idxd->dev_lock, flags);
+       spin_unlock(&idxd->dev_lock);
 
        rc = idxd_wq_enable(wq);
        if (rc < 0)
@@ -367,19 +367,18 @@ int idxd_wq_disable_pasid(struct idxd_wq *wq)
        int rc;
        union wqcfg wqcfg;
        unsigned int offset;
-       unsigned long flags;
 
        rc = idxd_wq_disable(wq, false);
        if (rc < 0)
                return rc;
 
        offset = WQCFG_OFFSET(idxd, wq->id, WQCFG_PASID_IDX);
-       spin_lock_irqsave(&idxd->dev_lock, flags);
+       spin_lock(&idxd->dev_lock);
        wqcfg.bits[WQCFG_PASID_IDX] = ioread32(idxd->reg_base + offset);
        wqcfg.pasid_en = 0;
        wqcfg.pasid = 0;
        iowrite32(wqcfg.bits[WQCFG_PASID_IDX], idxd->reg_base + offset);
-       spin_unlock_irqrestore(&idxd->dev_lock, flags);
+       spin_unlock(&idxd->dev_lock);
 
        rc = idxd_wq_enable(wq);
        if (rc < 0)
@@ -401,6 +400,7 @@ static void idxd_wq_disable_cleanup(struct idxd_wq *wq)
        wq->priority = 0;
        wq->ats_dis = 0;
        clear_bit(WQ_FLAG_DEDICATED, &wq->flags);
+       clear_bit(WQ_FLAG_BLOCK_ON_FAULT, &wq->flags);
        memset(wq->name, 0, WQ_NAME_SIZE);
 }
 
@@ -460,7 +460,6 @@ int idxd_device_init_reset(struct idxd_device *idxd)
 {
        struct device *dev = &idxd->pdev->dev;
        union idxd_command_reg cmd;
-       unsigned long flags;
 
        if (idxd_device_is_halted(idxd)) {
                dev_warn(&idxd->pdev->dev, "Device is HALTED!\n");
@@ -470,13 +469,13 @@ int idxd_device_init_reset(struct idxd_device *idxd)
        memset(&cmd, 0, sizeof(cmd));
        cmd.cmd = IDXD_CMD_RESET_DEVICE;
        dev_dbg(dev, "%s: sending reset for init.\n", __func__);
-       spin_lock_irqsave(&idxd->cmd_lock, flags);
+       spin_lock(&idxd->cmd_lock);
        iowrite32(cmd.bits, idxd->reg_base + IDXD_CMD_OFFSET);
 
        while (ioread32(idxd->reg_base + IDXD_CMDSTS_OFFSET) &
               IDXD_CMDSTS_ACTIVE)
                cpu_relax();
-       spin_unlock_irqrestore(&idxd->cmd_lock, flags);
+       spin_unlock(&idxd->cmd_lock);
        return 0;
 }
 
@@ -485,7 +484,6 @@ static void idxd_cmd_exec(struct idxd_device *idxd, int cmd_code, u32 operand,
 {
        union idxd_command_reg cmd;
        DECLARE_COMPLETION_ONSTACK(done);
-       unsigned long flags;
        u32 stat;
 
        if (idxd_device_is_halted(idxd)) {
@@ -500,7 +498,7 @@ static void idxd_cmd_exec(struct idxd_device *idxd, int cmd_code, u32 operand,
        cmd.operand = operand;
        cmd.int_req = 1;
 
-       spin_lock_irqsave(&idxd->cmd_lock, flags);
+       spin_lock(&idxd->cmd_lock);
        wait_event_lock_irq(idxd->cmd_waitq,
                            !test_bit(IDXD_FLAG_CMD_RUNNING, &idxd->flags),
                            idxd->cmd_lock);
@@ -517,10 +515,10 @@ static void idxd_cmd_exec(struct idxd_device *idxd, int cmd_code, u32 operand,
         * After command submitted, release lock and go to sleep until
         * the command completes via interrupt.
         */
-       spin_unlock_irqrestore(&idxd->cmd_lock, flags);
+       spin_unlock(&idxd->cmd_lock);
        wait_for_completion(&done);
        stat = ioread32(idxd->reg_base + IDXD_CMDSTS_OFFSET);
-       spin_lock_irqsave(&idxd->cmd_lock, flags);
+       spin_lock(&idxd->cmd_lock);
        if (status)
                *status = stat;
        idxd->cmd_status = stat & GENMASK(7, 0);
@@ -528,7 +526,7 @@ static void idxd_cmd_exec(struct idxd_device *idxd, int cmd_code, u32 operand,
        __clear_bit(IDXD_FLAG_CMD_RUNNING, &idxd->flags);
        /* Wake up other pending commands */
        wake_up(&idxd->cmd_waitq);
-       spin_unlock_irqrestore(&idxd->cmd_lock, flags);
+       spin_unlock(&idxd->cmd_lock);
 }
 
 int idxd_device_enable(struct idxd_device *idxd)
@@ -558,7 +556,6 @@ int idxd_device_disable(struct idxd_device *idxd)
 {
        struct device *dev = &idxd->pdev->dev;
        u32 status;
-       unsigned long flags;
 
        if (!idxd_is_enabled(idxd)) {
                dev_dbg(dev, "Device is not enabled\n");
@@ -574,22 +571,20 @@ int idxd_device_disable(struct idxd_device *idxd)
                return -ENXIO;
        }
 
-       spin_lock_irqsave(&idxd->dev_lock, flags);
+       spin_lock(&idxd->dev_lock);
        idxd_device_clear_state(idxd);
        idxd->state = IDXD_DEV_DISABLED;
-       spin_unlock_irqrestore(&idxd->dev_lock, flags);
+       spin_unlock(&idxd->dev_lock);
        return 0;
 }
 
 void idxd_device_reset(struct idxd_device *idxd)
 {
-       unsigned long flags;
-
        idxd_cmd_exec(idxd, IDXD_CMD_RESET_DEVICE, 0, NULL);
-       spin_lock_irqsave(&idxd->dev_lock, flags);
+       spin_lock(&idxd->dev_lock);
        idxd_device_clear_state(idxd);
        idxd->state = IDXD_DEV_DISABLED;
-       spin_unlock_irqrestore(&idxd->dev_lock, flags);
+       spin_unlock(&idxd->dev_lock);
 }
 
 void idxd_device_drain_pasid(struct idxd_device *idxd, int pasid)
@@ -639,7 +634,6 @@ int idxd_device_release_int_handle(struct idxd_device *idxd, int handle,
        struct device *dev = &idxd->pdev->dev;
        u32 operand, status;
        union idxd_command_reg cmd;
-       unsigned long flags;
 
        if (!(idxd->hw.cmd_cap & BIT(IDXD_CMD_RELEASE_INT_HANDLE)))
                return -EOPNOTSUPP;
@@ -657,13 +651,13 @@ int idxd_device_release_int_handle(struct idxd_device *idxd, int handle,
 
        dev_dbg(dev, "cmd: %u operand: %#x\n", IDXD_CMD_RELEASE_INT_HANDLE, operand);
 
-       spin_lock_irqsave(&idxd->cmd_lock, flags);
+       spin_lock(&idxd->cmd_lock);
        iowrite32(cmd.bits, idxd->reg_base + IDXD_CMD_OFFSET);
 
        while (ioread32(idxd->reg_base + IDXD_CMDSTS_OFFSET) & IDXD_CMDSTS_ACTIVE)
                cpu_relax();
        status = ioread32(idxd->reg_base + IDXD_CMDSTS_OFFSET);
-       spin_unlock_irqrestore(&idxd->cmd_lock, flags);
+       spin_unlock(&idxd->cmd_lock);
 
        if ((status & IDXD_CMDSTS_ERR_MASK) != IDXD_CMDSTS_SUCCESS) {
                dev_dbg(dev, "release int handle failed: %#x\n", status);
@@ -816,6 +810,15 @@ static int idxd_groups_config_write(struct idxd_device *idxd)
        return 0;
 }
 
+static bool idxd_device_pasid_priv_enabled(struct idxd_device *idxd)
+{
+       struct pci_dev *pdev = idxd->pdev;
+
+       if (pdev->pasid_enabled && (pdev->pasid_features & PCI_PASID_CAP_PRIV))
+               return true;
+       return false;
+}
+
 static int idxd_wq_config_write(struct idxd_wq *wq)
 {
        struct idxd_device *idxd = wq->idxd;
@@ -839,6 +842,7 @@ static int idxd_wq_config_write(struct idxd_wq *wq)
        wq->wqcfg->wq_size = wq->size;
 
        if (wq->size == 0) {
+               idxd->cmd_status = IDXD_SCMD_WQ_NO_SIZE;
                dev_warn(dev, "Incorrect work queue size: 0\n");
                return -EINVAL;
        }
@@ -847,7 +851,6 @@ static int idxd_wq_config_write(struct idxd_wq *wq)
        wq->wqcfg->wq_thresh = wq->threshold;
 
        /* byte 8-11 */
-       wq->wqcfg->priv = !!(wq->type == IDXD_WQT_KERNEL);
        if (wq_dedicated(wq))
                wq->wqcfg->mode = 1;
 
@@ -857,6 +860,25 @@ static int idxd_wq_config_write(struct idxd_wq *wq)
                        wq->wqcfg->pasid = idxd->pasid;
        }
 
+       /*
+        * Here the priv bit is set depending on the WQ type. priv = 1 if the
+        * WQ type is kernel to indicate privileged access. This setting only
+        * matters for dedicated WQ. According to the DSA spec:
+        * If the WQ is in dedicated mode, WQ PASID Enable is 1, and the
+        * Privileged Mode Enable field of the PCI Express PASID capability
+        * is 0, this field must be 0.
+        *
+        * In the case of a dedicated kernel WQ that is not able to support
+        * the PASID cap, then the configuration will be rejected.
+        */
+       wq->wqcfg->priv = !!(wq->type == IDXD_WQT_KERNEL);
+       if (wq_dedicated(wq) && wq->wqcfg->pasid_en &&
+           !idxd_device_pasid_priv_enabled(idxd) &&
+           wq->type == IDXD_WQT_KERNEL) {
+               idxd->cmd_status = IDXD_SCMD_WQ_NO_PRIV;
+               return -EOPNOTSUPP;
+       }
+
        wq->wqcfg->priority = wq->priority;
 
        if (idxd->hw.gen_cap.block_on_fault &&
@@ -974,6 +996,7 @@ static int idxd_wqs_setup(struct idxd_device *idxd)
                        continue;
 
                if (wq_shared(wq) && !device_swq_supported(idxd)) {
+                       idxd->cmd_status = IDXD_SCMD_WQ_NO_SWQ_SUPPORT;
                        dev_warn(dev, "No shared wq support but configured.\n");
                        return -EINVAL;
                }
@@ -982,8 +1005,10 @@ static int idxd_wqs_setup(struct idxd_device *idxd)
                configured++;
        }
 
-       if (configured == 0)
+       if (configured == 0) {
+               idxd->cmd_status = IDXD_SCMD_WQ_NONE_CONFIGURED;
                return -EINVAL;
+       }
 
        return 0;
 }
@@ -1130,30 +1155,34 @@ int idxd_device_load_config(struct idxd_device *idxd)
        return 0;
 }
 
-static int __drv_enable_wq(struct idxd_wq *wq)
+int __drv_enable_wq(struct idxd_wq *wq)
 {
        struct idxd_device *idxd = wq->idxd;
        struct device *dev = &idxd->pdev->dev;
-       unsigned long flags;
        int rc = -ENXIO;
 
        lockdep_assert_held(&wq->wq_lock);
 
-       if (idxd->state != IDXD_DEV_ENABLED)
+       if (idxd->state != IDXD_DEV_ENABLED) {
+               idxd->cmd_status = IDXD_SCMD_DEV_NOT_ENABLED;
                goto err;
+       }
 
        if (wq->state != IDXD_WQ_DISABLED) {
                dev_dbg(dev, "wq %d already enabled.\n", wq->id);
+               idxd->cmd_status = IDXD_SCMD_WQ_ENABLED;
                rc = -EBUSY;
                goto err;
        }
 
        if (!wq->group) {
                dev_dbg(dev, "wq %d not attached to group.\n", wq->id);
+               idxd->cmd_status = IDXD_SCMD_WQ_NO_GRP;
                goto err;
        }
 
        if (strlen(wq->name) == 0) {
+               idxd->cmd_status = IDXD_SCMD_WQ_NO_NAME;
                dev_dbg(dev, "wq %d name not set.\n", wq->id);
                goto err;
        }
@@ -1161,6 +1190,7 @@ static int __drv_enable_wq(struct idxd_wq *wq)
        /* Shared WQ checks */
        if (wq_shared(wq)) {
                if (!device_swq_supported(idxd)) {
+                       idxd->cmd_status = IDXD_SCMD_WQ_NO_SVM;
                        dev_dbg(dev, "PASID not enabled and shared wq.\n");
                        goto err;
                }
@@ -1173,21 +1203,17 @@ static int __drv_enable_wq(struct idxd_wq *wq)
                 * threshold via sysfs.
                 */
                if (wq->threshold == 0) {
+                       idxd->cmd_status = IDXD_SCMD_WQ_NO_THRESH;
                        dev_dbg(dev, "Shared wq and threshold 0.\n");
                        goto err;
                }
        }
 
-       rc = idxd_wq_alloc_resources(wq);
-       if (rc < 0) {
-               dev_dbg(dev, "wq resource alloc failed\n");
-               goto err;
-       }
-
-       spin_lock_irqsave(&idxd->dev_lock, flags);
+       rc = 0;
+       spin_lock(&idxd->dev_lock);
        if (test_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags))
                rc = idxd_device_config(idxd);
-       spin_unlock_irqrestore(&idxd->dev_lock, flags);
+       spin_unlock(&idxd->dev_lock);
        if (rc < 0) {
                dev_dbg(dev, "Writing wq %d config failed: %d\n", wq->id, rc);
                goto err;
@@ -1201,41 +1227,14 @@ static int __drv_enable_wq(struct idxd_wq *wq)
 
        rc = idxd_wq_map_portal(wq);
        if (rc < 0) {
+               idxd->cmd_status = IDXD_SCMD_WQ_PORTAL_ERR;
                dev_dbg(dev, "wq %d portal mapping failed: %d\n", wq->id, rc);
                goto err_map_portal;
        }
 
        wq->client_count = 0;
-
-       if (wq->type == IDXD_WQT_KERNEL) {
-               rc = idxd_wq_init_percpu_ref(wq);
-               if (rc < 0) {
-                       dev_dbg(dev, "wq %d percpu_ref setup failed\n", wq->id);
-                       goto err_cpu_ref;
-               }
-       }
-
-       if (is_idxd_wq_dmaengine(wq)) {
-               rc = idxd_register_dma_channel(wq);
-               if (rc < 0) {
-                       dev_dbg(dev, "wq %d DMA channel register failed\n", wq->id);
-                       goto err_client;
-               }
-       } else if (is_idxd_wq_cdev(wq)) {
-               rc = idxd_wq_add_cdev(wq);
-               if (rc < 0) {
-                       dev_dbg(dev, "wq %d cdev creation failed\n", wq->id);
-                       goto err_client;
-               }
-       }
-
-       dev_info(dev, "wq %s enabled\n", dev_name(wq_confdev(wq)));
        return 0;
 
-err_client:
-       idxd_wq_quiesce(wq);
-err_cpu_ref:
-       idxd_wq_unmap_portal(wq);
 err_map_portal:
        rc = idxd_wq_disable(wq, false);
        if (rc < 0)
@@ -1254,21 +1253,13 @@ int drv_enable_wq(struct idxd_wq *wq)
        return rc;
 }
 
-static void __drv_disable_wq(struct idxd_wq *wq)
+void __drv_disable_wq(struct idxd_wq *wq)
 {
        struct idxd_device *idxd = wq->idxd;
        struct device *dev = &idxd->pdev->dev;
 
        lockdep_assert_held(&wq->wq_lock);
 
-       if (wq->type == IDXD_WQT_KERNEL)
-               idxd_wq_quiesce(wq);
-
-       if (is_idxd_wq_dmaengine(wq))
-               idxd_unregister_dma_channel(wq);
-       else if (is_idxd_wq_cdev(wq))
-               idxd_wq_del_cdev(wq);
-
        if (idxd_wq_refcount(wq))
                dev_warn(dev, "Clients has claim on wq %d: %d\n",
                         wq->id, idxd_wq_refcount(wq));
@@ -1278,10 +1269,7 @@ static void __drv_disable_wq(struct idxd_wq *wq)
        idxd_wq_drain(wq);
        idxd_wq_reset(wq);
 
-       idxd_wq_free_resources(wq);
        wq->client_count = 0;
-
-       dev_info(dev, "wq %s disabled\n", dev_name(wq_confdev(wq)));
 }
 
 void drv_disable_wq(struct idxd_wq *wq)
@@ -1290,3 +1278,79 @@ void drv_disable_wq(struct idxd_wq *wq)
        __drv_disable_wq(wq);
        mutex_unlock(&wq->wq_lock);
 }
+
+int idxd_device_drv_probe(struct idxd_dev *idxd_dev)
+{
+       struct idxd_device *idxd = idxd_dev_to_idxd(idxd_dev);
+       int rc = 0;
+
+       /*
+        * Device should be in disabled state for the idxd_drv to load. If it's in
+        * enabled state, then the device was altered outside of driver's control.
+        * If the state is in halted state, then we don't want to proceed.
+        */
+       if (idxd->state != IDXD_DEV_DISABLED) {
+               idxd->cmd_status = IDXD_SCMD_DEV_ENABLED;
+               return -ENXIO;
+       }
+
+       /* Device configuration */
+       spin_lock(&idxd->dev_lock);
+       if (test_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags))
+               rc = idxd_device_config(idxd);
+       spin_unlock(&idxd->dev_lock);
+       if (rc < 0)
+               return -ENXIO;
+
+       /* Start device */
+       rc = idxd_device_enable(idxd);
+       if (rc < 0)
+               return rc;
+
+       /* Setup DMA device without channels */
+       rc = idxd_register_dma_device(idxd);
+       if (rc < 0) {
+               idxd_device_disable(idxd);
+               idxd->cmd_status = IDXD_SCMD_DEV_DMA_ERR;
+               return rc;
+       }
+
+       idxd->cmd_status = 0;
+       return 0;
+}
+
+void idxd_device_drv_remove(struct idxd_dev *idxd_dev)
+{
+       struct device *dev = &idxd_dev->conf_dev;
+       struct idxd_device *idxd = idxd_dev_to_idxd(idxd_dev);
+       int i;
+
+       for (i = 0; i < idxd->max_wqs; i++) {
+               struct idxd_wq *wq = idxd->wqs[i];
+               struct device *wq_dev = wq_confdev(wq);
+
+               if (wq->state == IDXD_WQ_DISABLED)
+                       continue;
+               dev_warn(dev, "Active wq %d on disable %s.\n", i, dev_name(wq_dev));
+               device_release_driver(wq_dev);
+       }
+
+       idxd_unregister_dma_device(idxd);
+       idxd_device_disable(idxd);
+       if (test_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags))
+               idxd_device_reset(idxd);
+}
+
+static enum idxd_dev_type dev_types[] = {
+       IDXD_DEV_DSA,
+       IDXD_DEV_IAX,
+       IDXD_DEV_NONE,
+};
+
+struct idxd_device_driver idxd_drv = {
+       .type = dev_types,
+       .probe = idxd_device_drv_probe,
+       .remove = idxd_device_drv_remove,
+       .name = "idxd",
+};
+EXPORT_SYMBOL_GPL(idxd_drv);