Kishon Vijay Abraham I | 2c156ac | 2017-03-27 15:15:14 +0530 | [diff] [blame] | 1 | /** |
| 2 | * Host side test driver to test endpoint functionality |
| 3 | * |
| 4 | * Copyright (C) 2017 Texas Instruments |
| 5 | * Author: Kishon Vijay Abraham I <kishon@ti.com> |
| 6 | * |
| 7 | * This program is free software: you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License version 2 of |
| 9 | * the License as published by the Free Software Foundation. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | */ |
| 19 | |
| 20 | #include <linux/crc32.h> |
| 21 | #include <linux/delay.h> |
| 22 | #include <linux/fs.h> |
| 23 | #include <linux/io.h> |
| 24 | #include <linux/interrupt.h> |
| 25 | #include <linux/irq.h> |
| 26 | #include <linux/miscdevice.h> |
| 27 | #include <linux/module.h> |
| 28 | #include <linux/mutex.h> |
| 29 | #include <linux/random.h> |
| 30 | #include <linux/slab.h> |
| 31 | #include <linux/pci.h> |
| 32 | #include <linux/pci_ids.h> |
| 33 | |
| 34 | #include <linux/pci_regs.h> |
| 35 | |
| 36 | #include <uapi/linux/pcitest.h> |
| 37 | |
Gustavo Pimentel | e8817de | 2018-07-19 10:32:17 +0200 | [diff] [blame^] | 38 | #define DRV_MODULE_NAME "pci-endpoint-test" |
Kishon Vijay Abraham I | 2c156ac | 2017-03-27 15:15:14 +0530 | [diff] [blame] | 39 | |
Gustavo Pimentel | e8817de | 2018-07-19 10:32:17 +0200 | [diff] [blame^] | 40 | #define IRQ_TYPE_LEGACY 0 |
| 41 | #define IRQ_TYPE_MSI 1 |
Kishon Vijay Abraham I | 2c156ac | 2017-03-27 15:15:14 +0530 | [diff] [blame] | 42 | |
Gustavo Pimentel | e8817de | 2018-07-19 10:32:17 +0200 | [diff] [blame^] | 43 | #define PCI_ENDPOINT_TEST_MAGIC 0x0 |
Kishon Vijay Abraham I | 2c156ac | 2017-03-27 15:15:14 +0530 | [diff] [blame] | 44 | |
Gustavo Pimentel | e8817de | 2018-07-19 10:32:17 +0200 | [diff] [blame^] | 45 | #define PCI_ENDPOINT_TEST_COMMAND 0x4 |
| 46 | #define COMMAND_RAISE_LEGACY_IRQ BIT(0) |
| 47 | #define COMMAND_RAISE_MSI_IRQ BIT(1) |
| 48 | /* BIT(2) is reserved for raising MSI-X IRQ command */ |
| 49 | #define COMMAND_READ BIT(3) |
| 50 | #define COMMAND_WRITE BIT(4) |
| 51 | #define COMMAND_COPY BIT(5) |
Kishon Vijay Abraham I | 2c156ac | 2017-03-27 15:15:14 +0530 | [diff] [blame] | 52 | |
Gustavo Pimentel | e8817de | 2018-07-19 10:32:17 +0200 | [diff] [blame^] | 53 | #define PCI_ENDPOINT_TEST_STATUS 0x8 |
| 54 | #define STATUS_READ_SUCCESS BIT(0) |
| 55 | #define STATUS_READ_FAIL BIT(1) |
| 56 | #define STATUS_WRITE_SUCCESS BIT(2) |
| 57 | #define STATUS_WRITE_FAIL BIT(3) |
| 58 | #define STATUS_COPY_SUCCESS BIT(4) |
| 59 | #define STATUS_COPY_FAIL BIT(5) |
| 60 | #define STATUS_IRQ_RAISED BIT(6) |
| 61 | #define STATUS_SRC_ADDR_INVALID BIT(7) |
| 62 | #define STATUS_DST_ADDR_INVALID BIT(8) |
| 63 | |
| 64 | #define PCI_ENDPOINT_TEST_LOWER_SRC_ADDR 0x0c |
Kishon Vijay Abraham I | 2c156ac | 2017-03-27 15:15:14 +0530 | [diff] [blame] | 65 | #define PCI_ENDPOINT_TEST_UPPER_SRC_ADDR 0x10 |
| 66 | |
| 67 | #define PCI_ENDPOINT_TEST_LOWER_DST_ADDR 0x14 |
| 68 | #define PCI_ENDPOINT_TEST_UPPER_DST_ADDR 0x18 |
| 69 | |
Gustavo Pimentel | e8817de | 2018-07-19 10:32:17 +0200 | [diff] [blame^] | 70 | #define PCI_ENDPOINT_TEST_SIZE 0x1c |
| 71 | #define PCI_ENDPOINT_TEST_CHECKSUM 0x20 |
| 72 | |
| 73 | #define PCI_ENDPOINT_TEST_IRQ_TYPE 0x24 |
| 74 | #define PCI_ENDPOINT_TEST_IRQ_NUMBER 0x28 |
Kishon Vijay Abraham I | 2c156ac | 2017-03-27 15:15:14 +0530 | [diff] [blame] | 75 | |
| 76 | static DEFINE_IDA(pci_endpoint_test_ida); |
| 77 | |
| 78 | #define to_endpoint_test(priv) container_of((priv), struct pci_endpoint_test, \ |
| 79 | miscdev) |
Kishon Vijay Abraham I | 0c8a5f9 | 2017-08-18 20:28:09 +0530 | [diff] [blame] | 80 | |
| 81 | static bool no_msi; |
| 82 | module_param(no_msi, bool, 0444); |
| 83 | MODULE_PARM_DESC(no_msi, "Disable MSI interrupt in pci_endpoint_test"); |
| 84 | |
Kishon Vijay Abraham I | 2c156ac | 2017-03-27 15:15:14 +0530 | [diff] [blame] | 85 | enum pci_barno { |
| 86 | BAR_0, |
| 87 | BAR_1, |
| 88 | BAR_2, |
| 89 | BAR_3, |
| 90 | BAR_4, |
| 91 | BAR_5, |
| 92 | }; |
| 93 | |
| 94 | struct pci_endpoint_test { |
| 95 | struct pci_dev *pdev; |
| 96 | void __iomem *base; |
| 97 | void __iomem *bar[6]; |
| 98 | struct completion irq_raised; |
| 99 | int last_irq; |
Kishon Vijay Abraham I | b7636e8 | 2017-10-11 14:14:38 +0530 | [diff] [blame] | 100 | int num_irqs; |
Kishon Vijay Abraham I | 2c156ac | 2017-03-27 15:15:14 +0530 | [diff] [blame] | 101 | /* mutex to protect the ioctls */ |
| 102 | struct mutex mutex; |
| 103 | struct miscdevice miscdev; |
Kishon Vijay Abraham I | 834b905 | 2017-08-18 20:28:05 +0530 | [diff] [blame] | 104 | enum pci_barno test_reg_bar; |
Kishon Vijay Abraham I | 13107c6 | 2017-08-18 20:28:06 +0530 | [diff] [blame] | 105 | size_t alignment; |
Kishon Vijay Abraham I | 2c156ac | 2017-03-27 15:15:14 +0530 | [diff] [blame] | 106 | }; |
| 107 | |
Kishon Vijay Abraham I | 834b905 | 2017-08-18 20:28:05 +0530 | [diff] [blame] | 108 | struct pci_endpoint_test_data { |
| 109 | enum pci_barno test_reg_bar; |
Kishon Vijay Abraham I | 13107c6 | 2017-08-18 20:28:06 +0530 | [diff] [blame] | 110 | size_t alignment; |
Kishon Vijay Abraham I | 0b91516 | 2017-08-18 20:28:07 +0530 | [diff] [blame] | 111 | bool no_msi; |
Kishon Vijay Abraham I | 834b905 | 2017-08-18 20:28:05 +0530 | [diff] [blame] | 112 | }; |
| 113 | |
Kishon Vijay Abraham I | 2c156ac | 2017-03-27 15:15:14 +0530 | [diff] [blame] | 114 | static inline u32 pci_endpoint_test_readl(struct pci_endpoint_test *test, |
| 115 | u32 offset) |
| 116 | { |
| 117 | return readl(test->base + offset); |
| 118 | } |
| 119 | |
| 120 | static inline void pci_endpoint_test_writel(struct pci_endpoint_test *test, |
| 121 | u32 offset, u32 value) |
| 122 | { |
| 123 | writel(value, test->base + offset); |
| 124 | } |
| 125 | |
| 126 | static inline u32 pci_endpoint_test_bar_readl(struct pci_endpoint_test *test, |
| 127 | int bar, int offset) |
| 128 | { |
| 129 | return readl(test->bar[bar] + offset); |
| 130 | } |
| 131 | |
| 132 | static inline void pci_endpoint_test_bar_writel(struct pci_endpoint_test *test, |
| 133 | int bar, u32 offset, u32 value) |
| 134 | { |
| 135 | writel(value, test->bar[bar] + offset); |
| 136 | } |
| 137 | |
| 138 | static irqreturn_t pci_endpoint_test_irqhandler(int irq, void *dev_id) |
| 139 | { |
| 140 | struct pci_endpoint_test *test = dev_id; |
| 141 | u32 reg; |
| 142 | |
| 143 | reg = pci_endpoint_test_readl(test, PCI_ENDPOINT_TEST_STATUS); |
| 144 | if (reg & STATUS_IRQ_RAISED) { |
| 145 | test->last_irq = irq; |
| 146 | complete(&test->irq_raised); |
| 147 | reg &= ~STATUS_IRQ_RAISED; |
| 148 | } |
| 149 | pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_STATUS, |
| 150 | reg); |
| 151 | |
| 152 | return IRQ_HANDLED; |
| 153 | } |
| 154 | |
| 155 | static bool pci_endpoint_test_bar(struct pci_endpoint_test *test, |
| 156 | enum pci_barno barno) |
| 157 | { |
| 158 | int j; |
| 159 | u32 val; |
| 160 | int size; |
Kishon Vijay Abraham I | cda370e | 2017-08-18 20:28:08 +0530 | [diff] [blame] | 161 | struct pci_dev *pdev = test->pdev; |
Kishon Vijay Abraham I | 2c156ac | 2017-03-27 15:15:14 +0530 | [diff] [blame] | 162 | |
| 163 | if (!test->bar[barno]) |
| 164 | return false; |
| 165 | |
Kishon Vijay Abraham I | cda370e | 2017-08-18 20:28:08 +0530 | [diff] [blame] | 166 | size = pci_resource_len(pdev, barno); |
Kishon Vijay Abraham I | 2c156ac | 2017-03-27 15:15:14 +0530 | [diff] [blame] | 167 | |
Kishon Vijay Abraham I | 834b905 | 2017-08-18 20:28:05 +0530 | [diff] [blame] | 168 | if (barno == test->test_reg_bar) |
| 169 | size = 0x4; |
| 170 | |
Kishon Vijay Abraham I | 2c156ac | 2017-03-27 15:15:14 +0530 | [diff] [blame] | 171 | for (j = 0; j < size; j += 4) |
| 172 | pci_endpoint_test_bar_writel(test, barno, j, 0xA0A0A0A0); |
| 173 | |
| 174 | for (j = 0; j < size; j += 4) { |
| 175 | val = pci_endpoint_test_bar_readl(test, barno, j); |
| 176 | if (val != 0xA0A0A0A0) |
| 177 | return false; |
| 178 | } |
| 179 | |
| 180 | return true; |
| 181 | } |
| 182 | |
| 183 | static bool pci_endpoint_test_legacy_irq(struct pci_endpoint_test *test) |
| 184 | { |
| 185 | u32 val; |
| 186 | |
Gustavo Pimentel | e8817de | 2018-07-19 10:32:17 +0200 | [diff] [blame^] | 187 | pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_IRQ_TYPE, |
| 188 | IRQ_TYPE_LEGACY); |
| 189 | pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_IRQ_NUMBER, 0); |
Kishon Vijay Abraham I | 2c156ac | 2017-03-27 15:15:14 +0530 | [diff] [blame] | 190 | pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_COMMAND, |
| 191 | COMMAND_RAISE_LEGACY_IRQ); |
| 192 | val = wait_for_completion_timeout(&test->irq_raised, |
| 193 | msecs_to_jiffies(1000)); |
| 194 | if (!val) |
| 195 | return false; |
| 196 | |
| 197 | return true; |
| 198 | } |
| 199 | |
| 200 | static bool pci_endpoint_test_msi_irq(struct pci_endpoint_test *test, |
| 201 | u8 msi_num) |
| 202 | { |
| 203 | u32 val; |
| 204 | struct pci_dev *pdev = test->pdev; |
| 205 | |
Gustavo Pimentel | e8817de | 2018-07-19 10:32:17 +0200 | [diff] [blame^] | 206 | pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_IRQ_TYPE, |
| 207 | IRQ_TYPE_MSI); |
| 208 | pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_IRQ_NUMBER, msi_num); |
Kishon Vijay Abraham I | 2c156ac | 2017-03-27 15:15:14 +0530 | [diff] [blame] | 209 | pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_COMMAND, |
Kishon Vijay Abraham I | 2c156ac | 2017-03-27 15:15:14 +0530 | [diff] [blame] | 210 | COMMAND_RAISE_MSI_IRQ); |
| 211 | val = wait_for_completion_timeout(&test->irq_raised, |
| 212 | msecs_to_jiffies(1000)); |
| 213 | if (!val) |
| 214 | return false; |
| 215 | |
Gustavo Pimentel | ecc57ef | 2018-05-14 18:27:48 +0100 | [diff] [blame] | 216 | if (pci_irq_vector(pdev, msi_num - 1) == test->last_irq) |
Kishon Vijay Abraham I | 2c156ac | 2017-03-27 15:15:14 +0530 | [diff] [blame] | 217 | return true; |
| 218 | |
| 219 | return false; |
| 220 | } |
| 221 | |
| 222 | static bool pci_endpoint_test_copy(struct pci_endpoint_test *test, size_t size) |
| 223 | { |
| 224 | bool ret = false; |
| 225 | void *src_addr; |
| 226 | void *dst_addr; |
| 227 | dma_addr_t src_phys_addr; |
| 228 | dma_addr_t dst_phys_addr; |
| 229 | struct pci_dev *pdev = test->pdev; |
| 230 | struct device *dev = &pdev->dev; |
Kishon Vijay Abraham I | 13107c6 | 2017-08-18 20:28:06 +0530 | [diff] [blame] | 231 | void *orig_src_addr; |
| 232 | dma_addr_t orig_src_phys_addr; |
| 233 | void *orig_dst_addr; |
| 234 | dma_addr_t orig_dst_phys_addr; |
| 235 | size_t offset; |
| 236 | size_t alignment = test->alignment; |
Kishon Vijay Abraham I | 2c156ac | 2017-03-27 15:15:14 +0530 | [diff] [blame] | 237 | u32 src_crc32; |
| 238 | u32 dst_crc32; |
| 239 | |
Dan Carpenter | 343dc69 | 2017-09-30 11:15:52 +0300 | [diff] [blame] | 240 | if (size > SIZE_MAX - alignment) |
| 241 | goto err; |
| 242 | |
Kishon Vijay Abraham I | 13107c6 | 2017-08-18 20:28:06 +0530 | [diff] [blame] | 243 | orig_src_addr = dma_alloc_coherent(dev, size + alignment, |
| 244 | &orig_src_phys_addr, GFP_KERNEL); |
| 245 | if (!orig_src_addr) { |
Gustavo Pimentel | 0e52ea6 | 2018-05-14 17:56:23 +0100 | [diff] [blame] | 246 | dev_err(dev, "Failed to allocate source buffer\n"); |
Kishon Vijay Abraham I | 2c156ac | 2017-03-27 15:15:14 +0530 | [diff] [blame] | 247 | ret = false; |
| 248 | goto err; |
| 249 | } |
| 250 | |
Kishon Vijay Abraham I | 13107c6 | 2017-08-18 20:28:06 +0530 | [diff] [blame] | 251 | if (alignment && !IS_ALIGNED(orig_src_phys_addr, alignment)) { |
| 252 | src_phys_addr = PTR_ALIGN(orig_src_phys_addr, alignment); |
| 253 | offset = src_phys_addr - orig_src_phys_addr; |
| 254 | src_addr = orig_src_addr + offset; |
| 255 | } else { |
| 256 | src_phys_addr = orig_src_phys_addr; |
| 257 | src_addr = orig_src_addr; |
| 258 | } |
| 259 | |
Kishon Vijay Abraham I | 2c156ac | 2017-03-27 15:15:14 +0530 | [diff] [blame] | 260 | pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_LOWER_SRC_ADDR, |
| 261 | lower_32_bits(src_phys_addr)); |
| 262 | |
| 263 | pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_UPPER_SRC_ADDR, |
| 264 | upper_32_bits(src_phys_addr)); |
| 265 | |
| 266 | get_random_bytes(src_addr, size); |
| 267 | src_crc32 = crc32_le(~0, src_addr, size); |
| 268 | |
Kishon Vijay Abraham I | 13107c6 | 2017-08-18 20:28:06 +0530 | [diff] [blame] | 269 | orig_dst_addr = dma_alloc_coherent(dev, size + alignment, |
| 270 | &orig_dst_phys_addr, GFP_KERNEL); |
| 271 | if (!orig_dst_addr) { |
Gustavo Pimentel | 0e52ea6 | 2018-05-14 17:56:23 +0100 | [diff] [blame] | 272 | dev_err(dev, "Failed to allocate destination address\n"); |
Kishon Vijay Abraham I | 2c156ac | 2017-03-27 15:15:14 +0530 | [diff] [blame] | 273 | ret = false; |
Kishon Vijay Abraham I | 13107c6 | 2017-08-18 20:28:06 +0530 | [diff] [blame] | 274 | goto err_orig_src_addr; |
| 275 | } |
| 276 | |
| 277 | if (alignment && !IS_ALIGNED(orig_dst_phys_addr, alignment)) { |
| 278 | dst_phys_addr = PTR_ALIGN(orig_dst_phys_addr, alignment); |
| 279 | offset = dst_phys_addr - orig_dst_phys_addr; |
| 280 | dst_addr = orig_dst_addr + offset; |
| 281 | } else { |
| 282 | dst_phys_addr = orig_dst_phys_addr; |
| 283 | dst_addr = orig_dst_addr; |
Kishon Vijay Abraham I | 2c156ac | 2017-03-27 15:15:14 +0530 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_LOWER_DST_ADDR, |
| 287 | lower_32_bits(dst_phys_addr)); |
| 288 | pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_UPPER_DST_ADDR, |
| 289 | upper_32_bits(dst_phys_addr)); |
| 290 | |
| 291 | pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_SIZE, |
| 292 | size); |
| 293 | |
Gustavo Pimentel | e8817de | 2018-07-19 10:32:17 +0200 | [diff] [blame^] | 294 | pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_IRQ_TYPE, |
| 295 | no_msi ? IRQ_TYPE_LEGACY : IRQ_TYPE_MSI); |
| 296 | pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_IRQ_NUMBER, 1); |
Kishon Vijay Abraham I | 2c156ac | 2017-03-27 15:15:14 +0530 | [diff] [blame] | 297 | pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_COMMAND, |
Gustavo Pimentel | e8817de | 2018-07-19 10:32:17 +0200 | [diff] [blame^] | 298 | COMMAND_COPY); |
Kishon Vijay Abraham I | 2c156ac | 2017-03-27 15:15:14 +0530 | [diff] [blame] | 299 | |
| 300 | wait_for_completion(&test->irq_raised); |
| 301 | |
| 302 | dst_crc32 = crc32_le(~0, dst_addr, size); |
| 303 | if (dst_crc32 == src_crc32) |
| 304 | ret = true; |
| 305 | |
Kishon Vijay Abraham I | 13107c6 | 2017-08-18 20:28:06 +0530 | [diff] [blame] | 306 | dma_free_coherent(dev, size + alignment, orig_dst_addr, |
| 307 | orig_dst_phys_addr); |
Kishon Vijay Abraham I | 2c156ac | 2017-03-27 15:15:14 +0530 | [diff] [blame] | 308 | |
Kishon Vijay Abraham I | 13107c6 | 2017-08-18 20:28:06 +0530 | [diff] [blame] | 309 | err_orig_src_addr: |
| 310 | dma_free_coherent(dev, size + alignment, orig_src_addr, |
| 311 | orig_src_phys_addr); |
Kishon Vijay Abraham I | 2c156ac | 2017-03-27 15:15:14 +0530 | [diff] [blame] | 312 | |
| 313 | err: |
| 314 | return ret; |
| 315 | } |
| 316 | |
| 317 | static bool pci_endpoint_test_write(struct pci_endpoint_test *test, size_t size) |
| 318 | { |
| 319 | bool ret = false; |
| 320 | u32 reg; |
| 321 | void *addr; |
| 322 | dma_addr_t phys_addr; |
| 323 | struct pci_dev *pdev = test->pdev; |
| 324 | struct device *dev = &pdev->dev; |
Kishon Vijay Abraham I | 13107c6 | 2017-08-18 20:28:06 +0530 | [diff] [blame] | 325 | void *orig_addr; |
| 326 | dma_addr_t orig_phys_addr; |
| 327 | size_t offset; |
| 328 | size_t alignment = test->alignment; |
Kishon Vijay Abraham I | 2c156ac | 2017-03-27 15:15:14 +0530 | [diff] [blame] | 329 | u32 crc32; |
| 330 | |
Dan Carpenter | 343dc69 | 2017-09-30 11:15:52 +0300 | [diff] [blame] | 331 | if (size > SIZE_MAX - alignment) |
| 332 | goto err; |
| 333 | |
Kishon Vijay Abraham I | 13107c6 | 2017-08-18 20:28:06 +0530 | [diff] [blame] | 334 | orig_addr = dma_alloc_coherent(dev, size + alignment, &orig_phys_addr, |
| 335 | GFP_KERNEL); |
| 336 | if (!orig_addr) { |
Gustavo Pimentel | 0e52ea6 | 2018-05-14 17:56:23 +0100 | [diff] [blame] | 337 | dev_err(dev, "Failed to allocate address\n"); |
Kishon Vijay Abraham I | 2c156ac | 2017-03-27 15:15:14 +0530 | [diff] [blame] | 338 | ret = false; |
| 339 | goto err; |
| 340 | } |
| 341 | |
Kishon Vijay Abraham I | 13107c6 | 2017-08-18 20:28:06 +0530 | [diff] [blame] | 342 | if (alignment && !IS_ALIGNED(orig_phys_addr, alignment)) { |
| 343 | phys_addr = PTR_ALIGN(orig_phys_addr, alignment); |
| 344 | offset = phys_addr - orig_phys_addr; |
| 345 | addr = orig_addr + offset; |
| 346 | } else { |
| 347 | phys_addr = orig_phys_addr; |
| 348 | addr = orig_addr; |
| 349 | } |
| 350 | |
Kishon Vijay Abraham I | 2c156ac | 2017-03-27 15:15:14 +0530 | [diff] [blame] | 351 | get_random_bytes(addr, size); |
| 352 | |
| 353 | crc32 = crc32_le(~0, addr, size); |
| 354 | pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_CHECKSUM, |
| 355 | crc32); |
| 356 | |
| 357 | pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_LOWER_SRC_ADDR, |
| 358 | lower_32_bits(phys_addr)); |
| 359 | pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_UPPER_SRC_ADDR, |
| 360 | upper_32_bits(phys_addr)); |
| 361 | |
| 362 | pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_SIZE, size); |
| 363 | |
Gustavo Pimentel | e8817de | 2018-07-19 10:32:17 +0200 | [diff] [blame^] | 364 | pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_IRQ_TYPE, |
| 365 | no_msi ? IRQ_TYPE_LEGACY : IRQ_TYPE_MSI); |
| 366 | pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_IRQ_NUMBER, 1); |
Kishon Vijay Abraham I | 2c156ac | 2017-03-27 15:15:14 +0530 | [diff] [blame] | 367 | pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_COMMAND, |
Gustavo Pimentel | e8817de | 2018-07-19 10:32:17 +0200 | [diff] [blame^] | 368 | COMMAND_READ); |
Kishon Vijay Abraham I | 2c156ac | 2017-03-27 15:15:14 +0530 | [diff] [blame] | 369 | |
| 370 | wait_for_completion(&test->irq_raised); |
| 371 | |
| 372 | reg = pci_endpoint_test_readl(test, PCI_ENDPOINT_TEST_STATUS); |
| 373 | if (reg & STATUS_READ_SUCCESS) |
| 374 | ret = true; |
| 375 | |
Kishon Vijay Abraham I | 13107c6 | 2017-08-18 20:28:06 +0530 | [diff] [blame] | 376 | dma_free_coherent(dev, size + alignment, orig_addr, orig_phys_addr); |
Kishon Vijay Abraham I | 2c156ac | 2017-03-27 15:15:14 +0530 | [diff] [blame] | 377 | |
| 378 | err: |
| 379 | return ret; |
| 380 | } |
| 381 | |
| 382 | static bool pci_endpoint_test_read(struct pci_endpoint_test *test, size_t size) |
| 383 | { |
| 384 | bool ret = false; |
| 385 | void *addr; |
| 386 | dma_addr_t phys_addr; |
| 387 | struct pci_dev *pdev = test->pdev; |
| 388 | struct device *dev = &pdev->dev; |
Kishon Vijay Abraham I | 13107c6 | 2017-08-18 20:28:06 +0530 | [diff] [blame] | 389 | void *orig_addr; |
| 390 | dma_addr_t orig_phys_addr; |
| 391 | size_t offset; |
| 392 | size_t alignment = test->alignment; |
Kishon Vijay Abraham I | 2c156ac | 2017-03-27 15:15:14 +0530 | [diff] [blame] | 393 | u32 crc32; |
| 394 | |
Dan Carpenter | 343dc69 | 2017-09-30 11:15:52 +0300 | [diff] [blame] | 395 | if (size > SIZE_MAX - alignment) |
| 396 | goto err; |
| 397 | |
Kishon Vijay Abraham I | 13107c6 | 2017-08-18 20:28:06 +0530 | [diff] [blame] | 398 | orig_addr = dma_alloc_coherent(dev, size + alignment, &orig_phys_addr, |
| 399 | GFP_KERNEL); |
| 400 | if (!orig_addr) { |
Gustavo Pimentel | 0e52ea6 | 2018-05-14 17:56:23 +0100 | [diff] [blame] | 401 | dev_err(dev, "Failed to allocate destination address\n"); |
Kishon Vijay Abraham I | 2c156ac | 2017-03-27 15:15:14 +0530 | [diff] [blame] | 402 | ret = false; |
| 403 | goto err; |
| 404 | } |
| 405 | |
Kishon Vijay Abraham I | 13107c6 | 2017-08-18 20:28:06 +0530 | [diff] [blame] | 406 | if (alignment && !IS_ALIGNED(orig_phys_addr, alignment)) { |
| 407 | phys_addr = PTR_ALIGN(orig_phys_addr, alignment); |
| 408 | offset = phys_addr - orig_phys_addr; |
| 409 | addr = orig_addr + offset; |
| 410 | } else { |
| 411 | phys_addr = orig_phys_addr; |
| 412 | addr = orig_addr; |
| 413 | } |
| 414 | |
Kishon Vijay Abraham I | 2c156ac | 2017-03-27 15:15:14 +0530 | [diff] [blame] | 415 | pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_LOWER_DST_ADDR, |
| 416 | lower_32_bits(phys_addr)); |
| 417 | pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_UPPER_DST_ADDR, |
| 418 | upper_32_bits(phys_addr)); |
| 419 | |
| 420 | pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_SIZE, size); |
| 421 | |
Gustavo Pimentel | e8817de | 2018-07-19 10:32:17 +0200 | [diff] [blame^] | 422 | pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_IRQ_TYPE, |
| 423 | no_msi ? IRQ_TYPE_LEGACY : IRQ_TYPE_MSI); |
| 424 | pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_IRQ_NUMBER, 1); |
Kishon Vijay Abraham I | 2c156ac | 2017-03-27 15:15:14 +0530 | [diff] [blame] | 425 | pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_COMMAND, |
Gustavo Pimentel | e8817de | 2018-07-19 10:32:17 +0200 | [diff] [blame^] | 426 | COMMAND_WRITE); |
Kishon Vijay Abraham I | 2c156ac | 2017-03-27 15:15:14 +0530 | [diff] [blame] | 427 | |
| 428 | wait_for_completion(&test->irq_raised); |
| 429 | |
| 430 | crc32 = crc32_le(~0, addr, size); |
| 431 | if (crc32 == pci_endpoint_test_readl(test, PCI_ENDPOINT_TEST_CHECKSUM)) |
| 432 | ret = true; |
| 433 | |
Kishon Vijay Abraham I | 13107c6 | 2017-08-18 20:28:06 +0530 | [diff] [blame] | 434 | dma_free_coherent(dev, size + alignment, orig_addr, orig_phys_addr); |
Kishon Vijay Abraham I | 2c156ac | 2017-03-27 15:15:14 +0530 | [diff] [blame] | 435 | err: |
| 436 | return ret; |
| 437 | } |
| 438 | |
| 439 | static long pci_endpoint_test_ioctl(struct file *file, unsigned int cmd, |
| 440 | unsigned long arg) |
| 441 | { |
| 442 | int ret = -EINVAL; |
| 443 | enum pci_barno bar; |
| 444 | struct pci_endpoint_test *test = to_endpoint_test(file->private_data); |
| 445 | |
| 446 | mutex_lock(&test->mutex); |
| 447 | switch (cmd) { |
| 448 | case PCITEST_BAR: |
| 449 | bar = arg; |
| 450 | if (bar < 0 || bar > 5) |
| 451 | goto ret; |
| 452 | ret = pci_endpoint_test_bar(test, bar); |
| 453 | break; |
| 454 | case PCITEST_LEGACY_IRQ: |
| 455 | ret = pci_endpoint_test_legacy_irq(test); |
| 456 | break; |
| 457 | case PCITEST_MSI: |
| 458 | ret = pci_endpoint_test_msi_irq(test, arg); |
| 459 | break; |
| 460 | case PCITEST_WRITE: |
| 461 | ret = pci_endpoint_test_write(test, arg); |
| 462 | break; |
| 463 | case PCITEST_READ: |
| 464 | ret = pci_endpoint_test_read(test, arg); |
| 465 | break; |
| 466 | case PCITEST_COPY: |
| 467 | ret = pci_endpoint_test_copy(test, arg); |
| 468 | break; |
| 469 | } |
| 470 | |
| 471 | ret: |
| 472 | mutex_unlock(&test->mutex); |
| 473 | return ret; |
| 474 | } |
| 475 | |
| 476 | static const struct file_operations pci_endpoint_test_fops = { |
| 477 | .owner = THIS_MODULE, |
| 478 | .unlocked_ioctl = pci_endpoint_test_ioctl, |
| 479 | }; |
| 480 | |
| 481 | static int pci_endpoint_test_probe(struct pci_dev *pdev, |
| 482 | const struct pci_device_id *ent) |
| 483 | { |
| 484 | int i; |
| 485 | int err; |
Kishon Vijay Abraham I | 0b91516 | 2017-08-18 20:28:07 +0530 | [diff] [blame] | 486 | int irq = 0; |
Kishon Vijay Abraham I | 2c156ac | 2017-03-27 15:15:14 +0530 | [diff] [blame] | 487 | int id; |
| 488 | char name[20]; |
| 489 | enum pci_barno bar; |
| 490 | void __iomem *base; |
| 491 | struct device *dev = &pdev->dev; |
| 492 | struct pci_endpoint_test *test; |
Kishon Vijay Abraham I | 834b905 | 2017-08-18 20:28:05 +0530 | [diff] [blame] | 493 | struct pci_endpoint_test_data *data; |
| 494 | enum pci_barno test_reg_bar = BAR_0; |
Kishon Vijay Abraham I | 2c156ac | 2017-03-27 15:15:14 +0530 | [diff] [blame] | 495 | struct miscdevice *misc_device; |
| 496 | |
| 497 | if (pci_is_bridge(pdev)) |
| 498 | return -ENODEV; |
| 499 | |
| 500 | test = devm_kzalloc(dev, sizeof(*test), GFP_KERNEL); |
| 501 | if (!test) |
| 502 | return -ENOMEM; |
| 503 | |
Kishon Vijay Abraham I | 834b905 | 2017-08-18 20:28:05 +0530 | [diff] [blame] | 504 | test->test_reg_bar = 0; |
Kishon Vijay Abraham I | 13107c6 | 2017-08-18 20:28:06 +0530 | [diff] [blame] | 505 | test->alignment = 0; |
Kishon Vijay Abraham I | 2c156ac | 2017-03-27 15:15:14 +0530 | [diff] [blame] | 506 | test->pdev = pdev; |
Kishon Vijay Abraham I | 834b905 | 2017-08-18 20:28:05 +0530 | [diff] [blame] | 507 | |
| 508 | data = (struct pci_endpoint_test_data *)ent->driver_data; |
Kishon Vijay Abraham I | 13107c6 | 2017-08-18 20:28:06 +0530 | [diff] [blame] | 509 | if (data) { |
Kishon Vijay Abraham I | 834b905 | 2017-08-18 20:28:05 +0530 | [diff] [blame] | 510 | test_reg_bar = data->test_reg_bar; |
Kishon Vijay Abraham I | 13107c6 | 2017-08-18 20:28:06 +0530 | [diff] [blame] | 511 | test->alignment = data->alignment; |
Kishon Vijay Abraham I | 0b91516 | 2017-08-18 20:28:07 +0530 | [diff] [blame] | 512 | no_msi = data->no_msi; |
Kishon Vijay Abraham I | 13107c6 | 2017-08-18 20:28:06 +0530 | [diff] [blame] | 513 | } |
Kishon Vijay Abraham I | 834b905 | 2017-08-18 20:28:05 +0530 | [diff] [blame] | 514 | |
Kishon Vijay Abraham I | 2c156ac | 2017-03-27 15:15:14 +0530 | [diff] [blame] | 515 | init_completion(&test->irq_raised); |
| 516 | mutex_init(&test->mutex); |
| 517 | |
| 518 | err = pci_enable_device(pdev); |
| 519 | if (err) { |
| 520 | dev_err(dev, "Cannot enable PCI device\n"); |
| 521 | return err; |
| 522 | } |
| 523 | |
| 524 | err = pci_request_regions(pdev, DRV_MODULE_NAME); |
| 525 | if (err) { |
| 526 | dev_err(dev, "Cannot obtain PCI resources\n"); |
| 527 | goto err_disable_pdev; |
| 528 | } |
| 529 | |
| 530 | pci_set_master(pdev); |
| 531 | |
Kishon Vijay Abraham I | 0b91516 | 2017-08-18 20:28:07 +0530 | [diff] [blame] | 532 | if (!no_msi) { |
| 533 | irq = pci_alloc_irq_vectors(pdev, 1, 32, PCI_IRQ_MSI); |
| 534 | if (irq < 0) |
Gustavo Pimentel | 0e52ea6 | 2018-05-14 17:56:23 +0100 | [diff] [blame] | 535 | dev_err(dev, "Failed to get MSI interrupts\n"); |
Kishon Vijay Abraham I | b7636e8 | 2017-10-11 14:14:38 +0530 | [diff] [blame] | 536 | test->num_irqs = irq; |
Kishon Vijay Abraham I | 0b91516 | 2017-08-18 20:28:07 +0530 | [diff] [blame] | 537 | } |
Kishon Vijay Abraham I | 2c156ac | 2017-03-27 15:15:14 +0530 | [diff] [blame] | 538 | |
| 539 | err = devm_request_irq(dev, pdev->irq, pci_endpoint_test_irqhandler, |
| 540 | IRQF_SHARED, DRV_MODULE_NAME, test); |
| 541 | if (err) { |
Gustavo Pimentel | 0e52ea6 | 2018-05-14 17:56:23 +0100 | [diff] [blame] | 542 | dev_err(dev, "Failed to request IRQ %d\n", pdev->irq); |
Kishon Vijay Abraham I | 2c156ac | 2017-03-27 15:15:14 +0530 | [diff] [blame] | 543 | goto err_disable_msi; |
| 544 | } |
| 545 | |
| 546 | for (i = 1; i < irq; i++) { |
Gustavo Pimentel | ecc57ef | 2018-05-14 18:27:48 +0100 | [diff] [blame] | 547 | err = devm_request_irq(dev, pci_irq_vector(pdev, i), |
Kishon Vijay Abraham I | 2c156ac | 2017-03-27 15:15:14 +0530 | [diff] [blame] | 548 | pci_endpoint_test_irqhandler, |
| 549 | IRQF_SHARED, DRV_MODULE_NAME, test); |
| 550 | if (err) |
| 551 | dev_err(dev, "failed to request IRQ %d for MSI %d\n", |
Gustavo Pimentel | ecc57ef | 2018-05-14 18:27:48 +0100 | [diff] [blame] | 552 | pci_irq_vector(pdev, i), i + 1); |
Kishon Vijay Abraham I | 2c156ac | 2017-03-27 15:15:14 +0530 | [diff] [blame] | 553 | } |
| 554 | |
| 555 | for (bar = BAR_0; bar <= BAR_5; bar++) { |
Niklas Cassel | 16b17ca | 2018-03-28 13:50:17 +0200 | [diff] [blame] | 556 | if (pci_resource_flags(pdev, bar) & IORESOURCE_MEM) { |
| 557 | base = pci_ioremap_bar(pdev, bar); |
| 558 | if (!base) { |
Gustavo Pimentel | 0e52ea6 | 2018-05-14 17:56:23 +0100 | [diff] [blame] | 559 | dev_err(dev, "Failed to read BAR%d\n", bar); |
Niklas Cassel | 16b17ca | 2018-03-28 13:50:17 +0200 | [diff] [blame] | 560 | WARN_ON(bar == test_reg_bar); |
| 561 | } |
| 562 | test->bar[bar] = base; |
Kishon Vijay Abraham I | 2c156ac | 2017-03-27 15:15:14 +0530 | [diff] [blame] | 563 | } |
Kishon Vijay Abraham I | 2c156ac | 2017-03-27 15:15:14 +0530 | [diff] [blame] | 564 | } |
| 565 | |
Kishon Vijay Abraham I | 834b905 | 2017-08-18 20:28:05 +0530 | [diff] [blame] | 566 | test->base = test->bar[test_reg_bar]; |
Kishon Vijay Abraham I | 2c156ac | 2017-03-27 15:15:14 +0530 | [diff] [blame] | 567 | if (!test->base) { |
Kishon Vijay Abraham I | 80068c9 | 2017-10-11 14:14:36 +0530 | [diff] [blame] | 568 | err = -ENOMEM; |
Kishon Vijay Abraham I | 834b905 | 2017-08-18 20:28:05 +0530 | [diff] [blame] | 569 | dev_err(dev, "Cannot perform PCI test without BAR%d\n", |
| 570 | test_reg_bar); |
Kishon Vijay Abraham I | 2c156ac | 2017-03-27 15:15:14 +0530 | [diff] [blame] | 571 | goto err_iounmap; |
| 572 | } |
| 573 | |
| 574 | pci_set_drvdata(pdev, test); |
| 575 | |
| 576 | id = ida_simple_get(&pci_endpoint_test_ida, 0, 0, GFP_KERNEL); |
| 577 | if (id < 0) { |
Kishon Vijay Abraham I | 80068c9 | 2017-10-11 14:14:36 +0530 | [diff] [blame] | 578 | err = id; |
Gustavo Pimentel | 0e52ea6 | 2018-05-14 17:56:23 +0100 | [diff] [blame] | 579 | dev_err(dev, "Unable to get id\n"); |
Kishon Vijay Abraham I | 2c156ac | 2017-03-27 15:15:14 +0530 | [diff] [blame] | 580 | goto err_iounmap; |
| 581 | } |
| 582 | |
| 583 | snprintf(name, sizeof(name), DRV_MODULE_NAME ".%d", id); |
| 584 | misc_device = &test->miscdev; |
| 585 | misc_device->minor = MISC_DYNAMIC_MINOR; |
Kishon Vijay Abraham I | 139838f | 2017-10-11 14:14:37 +0530 | [diff] [blame] | 586 | misc_device->name = kstrdup(name, GFP_KERNEL); |
| 587 | if (!misc_device->name) { |
| 588 | err = -ENOMEM; |
| 589 | goto err_ida_remove; |
| 590 | } |
Kishon Vijay Abraham I | 2c156ac | 2017-03-27 15:15:14 +0530 | [diff] [blame] | 591 | misc_device->fops = &pci_endpoint_test_fops, |
| 592 | |
| 593 | err = misc_register(misc_device); |
| 594 | if (err) { |
Gustavo Pimentel | 0e52ea6 | 2018-05-14 17:56:23 +0100 | [diff] [blame] | 595 | dev_err(dev, "Failed to register device\n"); |
Kishon Vijay Abraham I | 139838f | 2017-10-11 14:14:37 +0530 | [diff] [blame] | 596 | goto err_kfree_name; |
Kishon Vijay Abraham I | 2c156ac | 2017-03-27 15:15:14 +0530 | [diff] [blame] | 597 | } |
| 598 | |
| 599 | return 0; |
| 600 | |
Kishon Vijay Abraham I | 139838f | 2017-10-11 14:14:37 +0530 | [diff] [blame] | 601 | err_kfree_name: |
| 602 | kfree(misc_device->name); |
| 603 | |
Kishon Vijay Abraham I | 2c156ac | 2017-03-27 15:15:14 +0530 | [diff] [blame] | 604 | err_ida_remove: |
| 605 | ida_simple_remove(&pci_endpoint_test_ida, id); |
| 606 | |
| 607 | err_iounmap: |
| 608 | for (bar = BAR_0; bar <= BAR_5; bar++) { |
| 609 | if (test->bar[bar]) |
| 610 | pci_iounmap(pdev, test->bar[bar]); |
| 611 | } |
| 612 | |
Kishon Vijay Abraham I | b7636e8 | 2017-10-11 14:14:38 +0530 | [diff] [blame] | 613 | for (i = 0; i < irq; i++) |
Gustavo Pimentel | ecc57ef | 2018-05-14 18:27:48 +0100 | [diff] [blame] | 614 | devm_free_irq(&pdev->dev, pci_irq_vector(pdev, i), test); |
Kishon Vijay Abraham I | b7636e8 | 2017-10-11 14:14:38 +0530 | [diff] [blame] | 615 | |
Kishon Vijay Abraham I | 2c156ac | 2017-03-27 15:15:14 +0530 | [diff] [blame] | 616 | err_disable_msi: |
| 617 | pci_disable_msi(pdev); |
| 618 | pci_release_regions(pdev); |
| 619 | |
| 620 | err_disable_pdev: |
| 621 | pci_disable_device(pdev); |
| 622 | |
| 623 | return err; |
| 624 | } |
| 625 | |
| 626 | static void pci_endpoint_test_remove(struct pci_dev *pdev) |
| 627 | { |
| 628 | int id; |
Kishon Vijay Abraham I | b7636e8 | 2017-10-11 14:14:38 +0530 | [diff] [blame] | 629 | int i; |
Kishon Vijay Abraham I | 2c156ac | 2017-03-27 15:15:14 +0530 | [diff] [blame] | 630 | enum pci_barno bar; |
| 631 | struct pci_endpoint_test *test = pci_get_drvdata(pdev); |
| 632 | struct miscdevice *misc_device = &test->miscdev; |
| 633 | |
| 634 | if (sscanf(misc_device->name, DRV_MODULE_NAME ".%d", &id) != 1) |
| 635 | return; |
Dan Carpenter | a2db266 | 2017-09-30 11:16:51 +0300 | [diff] [blame] | 636 | if (id < 0) |
| 637 | return; |
Kishon Vijay Abraham I | 2c156ac | 2017-03-27 15:15:14 +0530 | [diff] [blame] | 638 | |
| 639 | misc_deregister(&test->miscdev); |
Kishon Vijay Abraham I | 139838f | 2017-10-11 14:14:37 +0530 | [diff] [blame] | 640 | kfree(misc_device->name); |
Kishon Vijay Abraham I | 2c156ac | 2017-03-27 15:15:14 +0530 | [diff] [blame] | 641 | ida_simple_remove(&pci_endpoint_test_ida, id); |
| 642 | for (bar = BAR_0; bar <= BAR_5; bar++) { |
| 643 | if (test->bar[bar]) |
| 644 | pci_iounmap(pdev, test->bar[bar]); |
| 645 | } |
Kishon Vijay Abraham I | b7636e8 | 2017-10-11 14:14:38 +0530 | [diff] [blame] | 646 | for (i = 0; i < test->num_irqs; i++) |
Gustavo Pimentel | ecc57ef | 2018-05-14 18:27:48 +0100 | [diff] [blame] | 647 | devm_free_irq(&pdev->dev, pci_irq_vector(pdev, i), test); |
Kishon Vijay Abraham I | 2c156ac | 2017-03-27 15:15:14 +0530 | [diff] [blame] | 648 | pci_disable_msi(pdev); |
| 649 | pci_release_regions(pdev); |
| 650 | pci_disable_device(pdev); |
| 651 | } |
| 652 | |
| 653 | static const struct pci_device_id pci_endpoint_test_tbl[] = { |
| 654 | { PCI_DEVICE(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_DRA74x) }, |
| 655 | { PCI_DEVICE(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_DRA72x) }, |
Gustavo Pimentel | 14b06dd | 2018-05-15 15:41:44 +0100 | [diff] [blame] | 656 | { PCI_DEVICE(PCI_VENDOR_ID_SYNOPSYS, 0xedda) }, |
Kishon Vijay Abraham I | 2c156ac | 2017-03-27 15:15:14 +0530 | [diff] [blame] | 657 | { } |
| 658 | }; |
| 659 | MODULE_DEVICE_TABLE(pci, pci_endpoint_test_tbl); |
| 660 | |
| 661 | static struct pci_driver pci_endpoint_test_driver = { |
| 662 | .name = DRV_MODULE_NAME, |
| 663 | .id_table = pci_endpoint_test_tbl, |
| 664 | .probe = pci_endpoint_test_probe, |
| 665 | .remove = pci_endpoint_test_remove, |
| 666 | }; |
| 667 | module_pci_driver(pci_endpoint_test_driver); |
| 668 | |
| 669 | MODULE_DESCRIPTION("PCI ENDPOINT TEST HOST DRIVER"); |
| 670 | MODULE_AUTHOR("Kishon Vijay Abraham I <kishon@ti.com>"); |
| 671 | MODULE_LICENSE("GPL v2"); |