pci_endpoint_test.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  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. #include <linux/crc32.h>
  20. #include <linux/delay.h>
  21. #include <linux/fs.h>
  22. #include <linux/io.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/irq.h>
  25. #include <linux/miscdevice.h>
  26. #include <linux/module.h>
  27. #include <linux/mutex.h>
  28. #include <linux/random.h>
  29. #include <linux/slab.h>
  30. #include <linux/pci.h>
  31. #include <linux/pci_ids.h>
  32. #include <linux/pci_regs.h>
  33. #include <uapi/linux/pcitest.h>
  34. #define DRV_MODULE_NAME "pci-endpoint-test"
  35. #define PCI_ENDPOINT_TEST_MAGIC 0x0
  36. #define PCI_ENDPOINT_TEST_COMMAND 0x4
  37. #define COMMAND_RAISE_LEGACY_IRQ BIT(0)
  38. #define COMMAND_RAISE_MSI_IRQ BIT(1)
  39. #define MSI_NUMBER_SHIFT 2
  40. /* 6 bits for MSI number */
  41. #define COMMAND_READ BIT(8)
  42. #define COMMAND_WRITE BIT(9)
  43. #define COMMAND_COPY BIT(10)
  44. #define PCI_ENDPOINT_TEST_STATUS 0x8
  45. #define STATUS_READ_SUCCESS BIT(0)
  46. #define STATUS_READ_FAIL BIT(1)
  47. #define STATUS_WRITE_SUCCESS BIT(2)
  48. #define STATUS_WRITE_FAIL BIT(3)
  49. #define STATUS_COPY_SUCCESS BIT(4)
  50. #define STATUS_COPY_FAIL BIT(5)
  51. #define STATUS_IRQ_RAISED BIT(6)
  52. #define STATUS_SRC_ADDR_INVALID BIT(7)
  53. #define STATUS_DST_ADDR_INVALID BIT(8)
  54. #define PCI_ENDPOINT_TEST_LOWER_SRC_ADDR 0xc
  55. #define PCI_ENDPOINT_TEST_UPPER_SRC_ADDR 0x10
  56. #define PCI_ENDPOINT_TEST_LOWER_DST_ADDR 0x14
  57. #define PCI_ENDPOINT_TEST_UPPER_DST_ADDR 0x18
  58. #define PCI_ENDPOINT_TEST_SIZE 0x1c
  59. #define PCI_ENDPOINT_TEST_CHECKSUM 0x20
  60. static DEFINE_IDA(pci_endpoint_test_ida);
  61. #define to_endpoint_test(priv) container_of((priv), struct pci_endpoint_test, \
  62. miscdev)
  63. enum pci_barno {
  64. BAR_0,
  65. BAR_1,
  66. BAR_2,
  67. BAR_3,
  68. BAR_4,
  69. BAR_5,
  70. };
  71. struct pci_endpoint_test {
  72. struct pci_dev *pdev;
  73. void __iomem *base;
  74. void __iomem *bar[6];
  75. struct completion irq_raised;
  76. int last_irq;
  77. /* mutex to protect the ioctls */
  78. struct mutex mutex;
  79. struct miscdevice miscdev;
  80. enum pci_barno test_reg_bar;
  81. size_t alignment;
  82. };
  83. struct pci_endpoint_test_data {
  84. enum pci_barno test_reg_bar;
  85. size_t alignment;
  86. bool no_msi;
  87. };
  88. static inline u32 pci_endpoint_test_readl(struct pci_endpoint_test *test,
  89. u32 offset)
  90. {
  91. return readl(test->base + offset);
  92. }
  93. static inline void pci_endpoint_test_writel(struct pci_endpoint_test *test,
  94. u32 offset, u32 value)
  95. {
  96. writel(value, test->base + offset);
  97. }
  98. static inline u32 pci_endpoint_test_bar_readl(struct pci_endpoint_test *test,
  99. int bar, int offset)
  100. {
  101. return readl(test->bar[bar] + offset);
  102. }
  103. static inline void pci_endpoint_test_bar_writel(struct pci_endpoint_test *test,
  104. int bar, u32 offset, u32 value)
  105. {
  106. writel(value, test->bar[bar] + offset);
  107. }
  108. static irqreturn_t pci_endpoint_test_irqhandler(int irq, void *dev_id)
  109. {
  110. struct pci_endpoint_test *test = dev_id;
  111. u32 reg;
  112. reg = pci_endpoint_test_readl(test, PCI_ENDPOINT_TEST_STATUS);
  113. if (reg & STATUS_IRQ_RAISED) {
  114. test->last_irq = irq;
  115. complete(&test->irq_raised);
  116. reg &= ~STATUS_IRQ_RAISED;
  117. }
  118. pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_STATUS,
  119. reg);
  120. return IRQ_HANDLED;
  121. }
  122. static bool pci_endpoint_test_bar(struct pci_endpoint_test *test,
  123. enum pci_barno barno)
  124. {
  125. int j;
  126. u32 val;
  127. int size;
  128. struct pci_dev *pdev = test->pdev;
  129. if (!test->bar[barno])
  130. return false;
  131. size = pci_resource_len(pdev, barno);
  132. if (barno == test->test_reg_bar)
  133. size = 0x4;
  134. for (j = 0; j < size; j += 4)
  135. pci_endpoint_test_bar_writel(test, barno, j, 0xA0A0A0A0);
  136. for (j = 0; j < size; j += 4) {
  137. val = pci_endpoint_test_bar_readl(test, barno, j);
  138. if (val != 0xA0A0A0A0)
  139. return false;
  140. }
  141. return true;
  142. }
  143. static bool pci_endpoint_test_legacy_irq(struct pci_endpoint_test *test)
  144. {
  145. u32 val;
  146. pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_COMMAND,
  147. COMMAND_RAISE_LEGACY_IRQ);
  148. val = wait_for_completion_timeout(&test->irq_raised,
  149. msecs_to_jiffies(1000));
  150. if (!val)
  151. return false;
  152. return true;
  153. }
  154. static bool pci_endpoint_test_msi_irq(struct pci_endpoint_test *test,
  155. u8 msi_num)
  156. {
  157. u32 val;
  158. struct pci_dev *pdev = test->pdev;
  159. pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_COMMAND,
  160. msi_num << MSI_NUMBER_SHIFT |
  161. COMMAND_RAISE_MSI_IRQ);
  162. val = wait_for_completion_timeout(&test->irq_raised,
  163. msecs_to_jiffies(1000));
  164. if (!val)
  165. return false;
  166. if (test->last_irq - pdev->irq == msi_num - 1)
  167. return true;
  168. return false;
  169. }
  170. static bool pci_endpoint_test_copy(struct pci_endpoint_test *test, size_t size)
  171. {
  172. bool ret = false;
  173. void *src_addr;
  174. void *dst_addr;
  175. dma_addr_t src_phys_addr;
  176. dma_addr_t dst_phys_addr;
  177. struct pci_dev *pdev = test->pdev;
  178. struct device *dev = &pdev->dev;
  179. void *orig_src_addr;
  180. dma_addr_t orig_src_phys_addr;
  181. void *orig_dst_addr;
  182. dma_addr_t orig_dst_phys_addr;
  183. size_t offset;
  184. size_t alignment = test->alignment;
  185. u32 src_crc32;
  186. u32 dst_crc32;
  187. orig_src_addr = dma_alloc_coherent(dev, size + alignment,
  188. &orig_src_phys_addr, GFP_KERNEL);
  189. if (!orig_src_addr) {
  190. dev_err(dev, "failed to allocate source buffer\n");
  191. ret = false;
  192. goto err;
  193. }
  194. if (alignment && !IS_ALIGNED(orig_src_phys_addr, alignment)) {
  195. src_phys_addr = PTR_ALIGN(orig_src_phys_addr, alignment);
  196. offset = src_phys_addr - orig_src_phys_addr;
  197. src_addr = orig_src_addr + offset;
  198. } else {
  199. src_phys_addr = orig_src_phys_addr;
  200. src_addr = orig_src_addr;
  201. }
  202. pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_LOWER_SRC_ADDR,
  203. lower_32_bits(src_phys_addr));
  204. pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_UPPER_SRC_ADDR,
  205. upper_32_bits(src_phys_addr));
  206. get_random_bytes(src_addr, size);
  207. src_crc32 = crc32_le(~0, src_addr, size);
  208. orig_dst_addr = dma_alloc_coherent(dev, size + alignment,
  209. &orig_dst_phys_addr, GFP_KERNEL);
  210. if (!orig_dst_addr) {
  211. dev_err(dev, "failed to allocate destination address\n");
  212. ret = false;
  213. goto err_orig_src_addr;
  214. }
  215. if (alignment && !IS_ALIGNED(orig_dst_phys_addr, alignment)) {
  216. dst_phys_addr = PTR_ALIGN(orig_dst_phys_addr, alignment);
  217. offset = dst_phys_addr - orig_dst_phys_addr;
  218. dst_addr = orig_dst_addr + offset;
  219. } else {
  220. dst_phys_addr = orig_dst_phys_addr;
  221. dst_addr = orig_dst_addr;
  222. }
  223. pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_LOWER_DST_ADDR,
  224. lower_32_bits(dst_phys_addr));
  225. pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_UPPER_DST_ADDR,
  226. upper_32_bits(dst_phys_addr));
  227. pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_SIZE,
  228. size);
  229. pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_COMMAND,
  230. 1 << MSI_NUMBER_SHIFT | COMMAND_COPY);
  231. wait_for_completion(&test->irq_raised);
  232. dst_crc32 = crc32_le(~0, dst_addr, size);
  233. if (dst_crc32 == src_crc32)
  234. ret = true;
  235. dma_free_coherent(dev, size + alignment, orig_dst_addr,
  236. orig_dst_phys_addr);
  237. err_orig_src_addr:
  238. dma_free_coherent(dev, size + alignment, orig_src_addr,
  239. orig_src_phys_addr);
  240. err:
  241. return ret;
  242. }
  243. static bool pci_endpoint_test_write(struct pci_endpoint_test *test, size_t size)
  244. {
  245. bool ret = false;
  246. u32 reg;
  247. void *addr;
  248. dma_addr_t phys_addr;
  249. struct pci_dev *pdev = test->pdev;
  250. struct device *dev = &pdev->dev;
  251. void *orig_addr;
  252. dma_addr_t orig_phys_addr;
  253. size_t offset;
  254. size_t alignment = test->alignment;
  255. u32 crc32;
  256. orig_addr = dma_alloc_coherent(dev, size + alignment, &orig_phys_addr,
  257. GFP_KERNEL);
  258. if (!orig_addr) {
  259. dev_err(dev, "failed to allocate address\n");
  260. ret = false;
  261. goto err;
  262. }
  263. if (alignment && !IS_ALIGNED(orig_phys_addr, alignment)) {
  264. phys_addr = PTR_ALIGN(orig_phys_addr, alignment);
  265. offset = phys_addr - orig_phys_addr;
  266. addr = orig_addr + offset;
  267. } else {
  268. phys_addr = orig_phys_addr;
  269. addr = orig_addr;
  270. }
  271. get_random_bytes(addr, size);
  272. crc32 = crc32_le(~0, addr, size);
  273. pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_CHECKSUM,
  274. crc32);
  275. pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_LOWER_SRC_ADDR,
  276. lower_32_bits(phys_addr));
  277. pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_UPPER_SRC_ADDR,
  278. upper_32_bits(phys_addr));
  279. pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_SIZE, size);
  280. pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_COMMAND,
  281. 1 << MSI_NUMBER_SHIFT | COMMAND_READ);
  282. wait_for_completion(&test->irq_raised);
  283. reg = pci_endpoint_test_readl(test, PCI_ENDPOINT_TEST_STATUS);
  284. if (reg & STATUS_READ_SUCCESS)
  285. ret = true;
  286. dma_free_coherent(dev, size + alignment, orig_addr, orig_phys_addr);
  287. err:
  288. return ret;
  289. }
  290. static bool pci_endpoint_test_read(struct pci_endpoint_test *test, size_t size)
  291. {
  292. bool ret = false;
  293. void *addr;
  294. dma_addr_t phys_addr;
  295. struct pci_dev *pdev = test->pdev;
  296. struct device *dev = &pdev->dev;
  297. void *orig_addr;
  298. dma_addr_t orig_phys_addr;
  299. size_t offset;
  300. size_t alignment = test->alignment;
  301. u32 crc32;
  302. orig_addr = dma_alloc_coherent(dev, size + alignment, &orig_phys_addr,
  303. GFP_KERNEL);
  304. if (!orig_addr) {
  305. dev_err(dev, "failed to allocate destination address\n");
  306. ret = false;
  307. goto err;
  308. }
  309. if (alignment && !IS_ALIGNED(orig_phys_addr, alignment)) {
  310. phys_addr = PTR_ALIGN(orig_phys_addr, alignment);
  311. offset = phys_addr - orig_phys_addr;
  312. addr = orig_addr + offset;
  313. } else {
  314. phys_addr = orig_phys_addr;
  315. addr = orig_addr;
  316. }
  317. pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_LOWER_DST_ADDR,
  318. lower_32_bits(phys_addr));
  319. pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_UPPER_DST_ADDR,
  320. upper_32_bits(phys_addr));
  321. pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_SIZE, size);
  322. pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_COMMAND,
  323. 1 << MSI_NUMBER_SHIFT | COMMAND_WRITE);
  324. wait_for_completion(&test->irq_raised);
  325. crc32 = crc32_le(~0, addr, size);
  326. if (crc32 == pci_endpoint_test_readl(test, PCI_ENDPOINT_TEST_CHECKSUM))
  327. ret = true;
  328. dma_free_coherent(dev, size + alignment, orig_addr, orig_phys_addr);
  329. err:
  330. return ret;
  331. }
  332. static long pci_endpoint_test_ioctl(struct file *file, unsigned int cmd,
  333. unsigned long arg)
  334. {
  335. int ret = -EINVAL;
  336. enum pci_barno bar;
  337. struct pci_endpoint_test *test = to_endpoint_test(file->private_data);
  338. mutex_lock(&test->mutex);
  339. switch (cmd) {
  340. case PCITEST_BAR:
  341. bar = arg;
  342. if (bar < 0 || bar > 5)
  343. goto ret;
  344. ret = pci_endpoint_test_bar(test, bar);
  345. break;
  346. case PCITEST_LEGACY_IRQ:
  347. ret = pci_endpoint_test_legacy_irq(test);
  348. break;
  349. case PCITEST_MSI:
  350. ret = pci_endpoint_test_msi_irq(test, arg);
  351. break;
  352. case PCITEST_WRITE:
  353. ret = pci_endpoint_test_write(test, arg);
  354. break;
  355. case PCITEST_READ:
  356. ret = pci_endpoint_test_read(test, arg);
  357. break;
  358. case PCITEST_COPY:
  359. ret = pci_endpoint_test_copy(test, arg);
  360. break;
  361. }
  362. ret:
  363. mutex_unlock(&test->mutex);
  364. return ret;
  365. }
  366. static const struct file_operations pci_endpoint_test_fops = {
  367. .owner = THIS_MODULE,
  368. .unlocked_ioctl = pci_endpoint_test_ioctl,
  369. };
  370. static int pci_endpoint_test_probe(struct pci_dev *pdev,
  371. const struct pci_device_id *ent)
  372. {
  373. int i;
  374. int err;
  375. int irq = 0;
  376. int id;
  377. bool no_msi = false;
  378. char name[20];
  379. enum pci_barno bar;
  380. void __iomem *base;
  381. struct device *dev = &pdev->dev;
  382. struct pci_endpoint_test *test;
  383. struct pci_endpoint_test_data *data;
  384. enum pci_barno test_reg_bar = BAR_0;
  385. struct miscdevice *misc_device;
  386. if (pci_is_bridge(pdev))
  387. return -ENODEV;
  388. test = devm_kzalloc(dev, sizeof(*test), GFP_KERNEL);
  389. if (!test)
  390. return -ENOMEM;
  391. test->test_reg_bar = 0;
  392. test->alignment = 0;
  393. test->pdev = pdev;
  394. data = (struct pci_endpoint_test_data *)ent->driver_data;
  395. if (data) {
  396. test_reg_bar = data->test_reg_bar;
  397. test->alignment = data->alignment;
  398. no_msi = data->no_msi;
  399. }
  400. init_completion(&test->irq_raised);
  401. mutex_init(&test->mutex);
  402. err = pci_enable_device(pdev);
  403. if (err) {
  404. dev_err(dev, "Cannot enable PCI device\n");
  405. return err;
  406. }
  407. err = pci_request_regions(pdev, DRV_MODULE_NAME);
  408. if (err) {
  409. dev_err(dev, "Cannot obtain PCI resources\n");
  410. goto err_disable_pdev;
  411. }
  412. pci_set_master(pdev);
  413. if (!no_msi) {
  414. irq = pci_alloc_irq_vectors(pdev, 1, 32, PCI_IRQ_MSI);
  415. if (irq < 0)
  416. dev_err(dev, "failed to get MSI interrupts\n");
  417. }
  418. err = devm_request_irq(dev, pdev->irq, pci_endpoint_test_irqhandler,
  419. IRQF_SHARED, DRV_MODULE_NAME, test);
  420. if (err) {
  421. dev_err(dev, "failed to request IRQ %d\n", pdev->irq);
  422. goto err_disable_msi;
  423. }
  424. for (i = 1; i < irq; i++) {
  425. err = devm_request_irq(dev, pdev->irq + i,
  426. pci_endpoint_test_irqhandler,
  427. IRQF_SHARED, DRV_MODULE_NAME, test);
  428. if (err)
  429. dev_err(dev, "failed to request IRQ %d for MSI %d\n",
  430. pdev->irq + i, i + 1);
  431. }
  432. for (bar = BAR_0; bar <= BAR_5; bar++) {
  433. base = pci_ioremap_bar(pdev, bar);
  434. if (!base) {
  435. dev_err(dev, "failed to read BAR%d\n", bar);
  436. WARN_ON(bar == test_reg_bar);
  437. }
  438. test->bar[bar] = base;
  439. }
  440. test->base = test->bar[test_reg_bar];
  441. if (!test->base) {
  442. dev_err(dev, "Cannot perform PCI test without BAR%d\n",
  443. test_reg_bar);
  444. goto err_iounmap;
  445. }
  446. pci_set_drvdata(pdev, test);
  447. id = ida_simple_get(&pci_endpoint_test_ida, 0, 0, GFP_KERNEL);
  448. if (id < 0) {
  449. dev_err(dev, "unable to get id\n");
  450. goto err_iounmap;
  451. }
  452. snprintf(name, sizeof(name), DRV_MODULE_NAME ".%d", id);
  453. misc_device = &test->miscdev;
  454. misc_device->minor = MISC_DYNAMIC_MINOR;
  455. misc_device->name = name;
  456. misc_device->fops = &pci_endpoint_test_fops,
  457. err = misc_register(misc_device);
  458. if (err) {
  459. dev_err(dev, "failed to register device\n");
  460. goto err_ida_remove;
  461. }
  462. return 0;
  463. err_ida_remove:
  464. ida_simple_remove(&pci_endpoint_test_ida, id);
  465. err_iounmap:
  466. for (bar = BAR_0; bar <= BAR_5; bar++) {
  467. if (test->bar[bar])
  468. pci_iounmap(pdev, test->bar[bar]);
  469. }
  470. err_disable_msi:
  471. pci_disable_msi(pdev);
  472. pci_release_regions(pdev);
  473. err_disable_pdev:
  474. pci_disable_device(pdev);
  475. return err;
  476. }
  477. static void pci_endpoint_test_remove(struct pci_dev *pdev)
  478. {
  479. int id;
  480. enum pci_barno bar;
  481. struct pci_endpoint_test *test = pci_get_drvdata(pdev);
  482. struct miscdevice *misc_device = &test->miscdev;
  483. if (sscanf(misc_device->name, DRV_MODULE_NAME ".%d", &id) != 1)
  484. return;
  485. misc_deregister(&test->miscdev);
  486. ida_simple_remove(&pci_endpoint_test_ida, id);
  487. for (bar = BAR_0; bar <= BAR_5; bar++) {
  488. if (test->bar[bar])
  489. pci_iounmap(pdev, test->bar[bar]);
  490. }
  491. pci_disable_msi(pdev);
  492. pci_release_regions(pdev);
  493. pci_disable_device(pdev);
  494. }
  495. static const struct pci_device_id pci_endpoint_test_tbl[] = {
  496. { PCI_DEVICE(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_DRA74x) },
  497. { PCI_DEVICE(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_DRA72x) },
  498. { }
  499. };
  500. MODULE_DEVICE_TABLE(pci, pci_endpoint_test_tbl);
  501. static struct pci_driver pci_endpoint_test_driver = {
  502. .name = DRV_MODULE_NAME,
  503. .id_table = pci_endpoint_test_tbl,
  504. .probe = pci_endpoint_test_probe,
  505. .remove = pci_endpoint_test_remove,
  506. };
  507. module_pci_driver(pci_endpoint_test_driver);
  508. MODULE_DESCRIPTION("PCI ENDPOINT TEST HOST DRIVER");
  509. MODULE_AUTHOR("Kishon Vijay Abraham I <kishon@ti.com>");
  510. MODULE_LICENSE("GPL v2");