Merge tag 'armsoc-devicetree' of git://git.kernel.org/pub/scm/linux/kernel/git/arm...
[sfrench/cifs-2.6.git] / drivers / misc / pci_endpoint_test.c
index 5fc0f6c6a9e566bde8789adf9113c1f8eeca4720..deb20302649652d157529b82570dc45d913160c6 100644 (file)
@@ -72,6 +72,11 @@ static DEFINE_IDA(pci_endpoint_test_ida);
 
 #define to_endpoint_test(priv) container_of((priv), struct pci_endpoint_test, \
                                            miscdev)
+
+static bool no_msi;
+module_param(no_msi, bool, 0444);
+MODULE_PARM_DESC(no_msi, "Disable MSI interrupt in pci_endpoint_test");
+
 enum pci_barno {
        BAR_0,
        BAR_1,
@@ -91,14 +96,15 @@ struct pci_endpoint_test {
        struct mutex    mutex;
        struct miscdevice miscdev;
        enum pci_barno test_reg_bar;
+       size_t alignment;
 };
 
 struct pci_endpoint_test_data {
        enum pci_barno test_reg_bar;
+       size_t alignment;
+       bool no_msi;
 };
 
-static int bar_size[] = { 512, 512, 1024, 16384, 131072, 1048576 };
-
 static inline u32 pci_endpoint_test_readl(struct pci_endpoint_test *test,
                                          u32 offset)
 {
@@ -146,11 +152,12 @@ static bool pci_endpoint_test_bar(struct pci_endpoint_test *test,
        int j;
        u32 val;
        int size;
+       struct pci_dev *pdev = test->pdev;
 
        if (!test->bar[barno])
                return false;
 
-       size = bar_size[barno];
+       size = pci_resource_len(pdev, barno);
 
        if (barno == test->test_reg_bar)
                size = 0x4;
@@ -210,16 +217,32 @@ static bool pci_endpoint_test_copy(struct pci_endpoint_test *test, size_t size)
        dma_addr_t dst_phys_addr;
        struct pci_dev *pdev = test->pdev;
        struct device *dev = &pdev->dev;
+       void *orig_src_addr;
+       dma_addr_t orig_src_phys_addr;
+       void *orig_dst_addr;
+       dma_addr_t orig_dst_phys_addr;
+       size_t offset;
+       size_t alignment = test->alignment;
        u32 src_crc32;
        u32 dst_crc32;
 
-       src_addr = dma_alloc_coherent(dev, size, &src_phys_addr, GFP_KERNEL);
-       if (!src_addr) {
+       orig_src_addr = dma_alloc_coherent(dev, size + alignment,
+                                          &orig_src_phys_addr, GFP_KERNEL);
+       if (!orig_src_addr) {
                dev_err(dev, "failed to allocate source buffer\n");
                ret = false;
                goto err;
        }
 
+       if (alignment && !IS_ALIGNED(orig_src_phys_addr, alignment)) {
+               src_phys_addr = PTR_ALIGN(orig_src_phys_addr, alignment);
+               offset = src_phys_addr - orig_src_phys_addr;
+               src_addr = orig_src_addr + offset;
+       } else {
+               src_phys_addr = orig_src_phys_addr;
+               src_addr = orig_src_addr;
+       }
+
        pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_LOWER_SRC_ADDR,
                                 lower_32_bits(src_phys_addr));
 
@@ -229,11 +252,21 @@ static bool pci_endpoint_test_copy(struct pci_endpoint_test *test, size_t size)
        get_random_bytes(src_addr, size);
        src_crc32 = crc32_le(~0, src_addr, size);
 
-       dst_addr = dma_alloc_coherent(dev, size, &dst_phys_addr, GFP_KERNEL);
-       if (!dst_addr) {
+       orig_dst_addr = dma_alloc_coherent(dev, size + alignment,
+                                          &orig_dst_phys_addr, GFP_KERNEL);
+       if (!orig_dst_addr) {
                dev_err(dev, "failed to allocate destination address\n");
                ret = false;
-               goto err_src_addr;
+               goto err_orig_src_addr;
+       }
+
+       if (alignment && !IS_ALIGNED(orig_dst_phys_addr, alignment)) {
+               dst_phys_addr = PTR_ALIGN(orig_dst_phys_addr, alignment);
+               offset = dst_phys_addr - orig_dst_phys_addr;
+               dst_addr = orig_dst_addr + offset;
+       } else {
+               dst_phys_addr = orig_dst_phys_addr;
+               dst_addr = orig_dst_addr;
        }
 
        pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_LOWER_DST_ADDR,
@@ -253,10 +286,12 @@ static bool pci_endpoint_test_copy(struct pci_endpoint_test *test, size_t size)
        if (dst_crc32 == src_crc32)
                ret = true;
 
-       dma_free_coherent(dev, size, dst_addr, dst_phys_addr);
+       dma_free_coherent(dev, size + alignment, orig_dst_addr,
+                         orig_dst_phys_addr);
 
-err_src_addr:
-       dma_free_coherent(dev, size, src_addr, src_phys_addr);
+err_orig_src_addr:
+       dma_free_coherent(dev, size + alignment, orig_src_addr,
+                         orig_src_phys_addr);
 
 err:
        return ret;
@@ -270,15 +305,29 @@ static bool pci_endpoint_test_write(struct pci_endpoint_test *test, size_t size)
        dma_addr_t phys_addr;
        struct pci_dev *pdev = test->pdev;
        struct device *dev = &pdev->dev;
+       void *orig_addr;
+       dma_addr_t orig_phys_addr;
+       size_t offset;
+       size_t alignment = test->alignment;
        u32 crc32;
 
-       addr = dma_alloc_coherent(dev, size, &phys_addr, GFP_KERNEL);
-       if (!addr) {
+       orig_addr = dma_alloc_coherent(dev, size + alignment, &orig_phys_addr,
+                                      GFP_KERNEL);
+       if (!orig_addr) {
                dev_err(dev, "failed to allocate address\n");
                ret = false;
                goto err;
        }
 
+       if (alignment && !IS_ALIGNED(orig_phys_addr, alignment)) {
+               phys_addr =  PTR_ALIGN(orig_phys_addr, alignment);
+               offset = phys_addr - orig_phys_addr;
+               addr = orig_addr + offset;
+       } else {
+               phys_addr = orig_phys_addr;
+               addr = orig_addr;
+       }
+
        get_random_bytes(addr, size);
 
        crc32 = crc32_le(~0, addr, size);
@@ -301,7 +350,7 @@ static bool pci_endpoint_test_write(struct pci_endpoint_test *test, size_t size)
        if (reg & STATUS_READ_SUCCESS)
                ret = true;
 
-       dma_free_coherent(dev, size, addr, phys_addr);
+       dma_free_coherent(dev, size + alignment, orig_addr, orig_phys_addr);
 
 err:
        return ret;
@@ -314,15 +363,29 @@ static bool pci_endpoint_test_read(struct pci_endpoint_test *test, size_t size)
        dma_addr_t phys_addr;
        struct pci_dev *pdev = test->pdev;
        struct device *dev = &pdev->dev;
+       void *orig_addr;
+       dma_addr_t orig_phys_addr;
+       size_t offset;
+       size_t alignment = test->alignment;
        u32 crc32;
 
-       addr = dma_alloc_coherent(dev, size, &phys_addr, GFP_KERNEL);
-       if (!addr) {
+       orig_addr = dma_alloc_coherent(dev, size + alignment, &orig_phys_addr,
+                                      GFP_KERNEL);
+       if (!orig_addr) {
                dev_err(dev, "failed to allocate destination address\n");
                ret = false;
                goto err;
        }
 
+       if (alignment && !IS_ALIGNED(orig_phys_addr, alignment)) {
+               phys_addr = PTR_ALIGN(orig_phys_addr, alignment);
+               offset = phys_addr - orig_phys_addr;
+               addr = orig_addr + offset;
+       } else {
+               phys_addr = orig_phys_addr;
+               addr = orig_addr;
+       }
+
        pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_LOWER_DST_ADDR,
                                 lower_32_bits(phys_addr));
        pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_UPPER_DST_ADDR,
@@ -339,7 +402,7 @@ static bool pci_endpoint_test_read(struct pci_endpoint_test *test, size_t size)
        if (crc32 == pci_endpoint_test_readl(test, PCI_ENDPOINT_TEST_CHECKSUM))
                ret = true;
 
-       dma_free_coherent(dev, size, addr, phys_addr);
+       dma_free_coherent(dev, size + alignment, orig_addr, orig_phys_addr);
 err:
        return ret;
 }
@@ -391,7 +454,7 @@ static int pci_endpoint_test_probe(struct pci_dev *pdev,
 {
        int i;
        int err;
-       int irq;
+       int irq = 0;
        int id;
        char name[20];
        enum pci_barno bar;
@@ -410,11 +473,15 @@ static int pci_endpoint_test_probe(struct pci_dev *pdev,
                return -ENOMEM;
 
        test->test_reg_bar = 0;
+       test->alignment = 0;
        test->pdev = pdev;
 
        data = (struct pci_endpoint_test_data *)ent->driver_data;
-       if (data)
+       if (data) {
                test_reg_bar = data->test_reg_bar;
+               test->alignment = data->alignment;
+               no_msi = data->no_msi;
+       }
 
        init_completion(&test->irq_raised);
        mutex_init(&test->mutex);
@@ -433,9 +500,11 @@ static int pci_endpoint_test_probe(struct pci_dev *pdev,
 
        pci_set_master(pdev);
 
-       irq = pci_alloc_irq_vectors(pdev, 1, 32, PCI_IRQ_MSI);
-       if (irq < 0)
-               dev_err(dev, "failed to get MSI interrupts\n");
+       if (!no_msi) {
+               irq = pci_alloc_irq_vectors(pdev, 1, 32, PCI_IRQ_MSI);
+               if (irq < 0)
+                       dev_err(dev, "failed to get MSI interrupts\n");
+       }
 
        err = devm_request_irq(dev, pdev->irq, pci_endpoint_test_irqhandler,
                               IRQF_SHARED, DRV_MODULE_NAME, test);