pci_endpoint_test.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Host side test driver to test endpoint functionality
  4. *
  5. * Copyright (C) 2017 Texas Instruments
  6. * Author: Kishon Vijay Abraham I <kishon@ti.com>
  7. */
  8. #include <linux/crc32.h>
  9. #include <linux/cleanup.h>
  10. #include <linux/delay.h>
  11. #include <linux/fs.h>
  12. #include <linux/io.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/irq.h>
  15. #include <linux/miscdevice.h>
  16. #include <linux/module.h>
  17. #include <linux/mutex.h>
  18. #include <linux/random.h>
  19. #include <linux/slab.h>
  20. #include <linux/uaccess.h>
  21. #include <linux/pci.h>
  22. #include <linux/pci_ids.h>
  23. #include <linux/pci_regs.h>
  24. #include <uapi/linux/pcitest.h>
  25. #define DRV_MODULE_NAME "pci-endpoint-test"
  26. #define IRQ_TYPE_UNDEFINED -1
  27. #define IRQ_TYPE_INTX 0
  28. #define IRQ_TYPE_MSI 1
  29. #define IRQ_TYPE_MSIX 2
  30. #define PCI_ENDPOINT_TEST_MAGIC 0x0
  31. #define PCI_ENDPOINT_TEST_COMMAND 0x4
  32. #define COMMAND_RAISE_INTX_IRQ BIT(0)
  33. #define COMMAND_RAISE_MSI_IRQ BIT(1)
  34. #define COMMAND_RAISE_MSIX_IRQ BIT(2)
  35. #define COMMAND_READ BIT(3)
  36. #define COMMAND_WRITE BIT(4)
  37. #define COMMAND_COPY BIT(5)
  38. #define PCI_ENDPOINT_TEST_STATUS 0x8
  39. #define STATUS_READ_SUCCESS BIT(0)
  40. #define STATUS_READ_FAIL BIT(1)
  41. #define STATUS_WRITE_SUCCESS BIT(2)
  42. #define STATUS_WRITE_FAIL BIT(3)
  43. #define STATUS_COPY_SUCCESS BIT(4)
  44. #define STATUS_COPY_FAIL BIT(5)
  45. #define STATUS_IRQ_RAISED BIT(6)
  46. #define STATUS_SRC_ADDR_INVALID BIT(7)
  47. #define STATUS_DST_ADDR_INVALID BIT(8)
  48. #define PCI_ENDPOINT_TEST_LOWER_SRC_ADDR 0x0c
  49. #define PCI_ENDPOINT_TEST_UPPER_SRC_ADDR 0x10
  50. #define PCI_ENDPOINT_TEST_LOWER_DST_ADDR 0x14
  51. #define PCI_ENDPOINT_TEST_UPPER_DST_ADDR 0x18
  52. #define PCI_ENDPOINT_TEST_SIZE 0x1c
  53. #define PCI_ENDPOINT_TEST_CHECKSUM 0x20
  54. #define PCI_ENDPOINT_TEST_IRQ_TYPE 0x24
  55. #define PCI_ENDPOINT_TEST_IRQ_NUMBER 0x28
  56. #define PCI_ENDPOINT_TEST_FLAGS 0x2c
  57. #define FLAG_USE_DMA BIT(0)
  58. #define PCI_DEVICE_ID_TI_AM654 0xb00c
  59. #define PCI_DEVICE_ID_TI_J7200 0xb00f
  60. #define PCI_DEVICE_ID_TI_AM64 0xb010
  61. #define PCI_DEVICE_ID_TI_J721S2 0xb013
  62. #define PCI_DEVICE_ID_LS1088A 0x80c0
  63. #define PCI_DEVICE_ID_IMX8 0x0808
  64. #define is_am654_pci_dev(pdev) \
  65. ((pdev)->device == PCI_DEVICE_ID_TI_AM654)
  66. #define PCI_DEVICE_ID_RENESAS_R8A774A1 0x0028
  67. #define PCI_DEVICE_ID_RENESAS_R8A774B1 0x002b
  68. #define PCI_DEVICE_ID_RENESAS_R8A774C0 0x002d
  69. #define PCI_DEVICE_ID_RENESAS_R8A774E1 0x0025
  70. #define PCI_DEVICE_ID_RENESAS_R8A779F0 0x0031
  71. #define PCI_DEVICE_ID_ROCKCHIP_RK3588 0x3588
  72. static DEFINE_IDA(pci_endpoint_test_ida);
  73. #define to_endpoint_test(priv) container_of((priv), struct pci_endpoint_test, \
  74. miscdev)
  75. static bool no_msi;
  76. module_param(no_msi, bool, 0444);
  77. MODULE_PARM_DESC(no_msi, "Disable MSI interrupt in pci_endpoint_test");
  78. static int irq_type = IRQ_TYPE_MSI;
  79. module_param(irq_type, int, 0444);
  80. MODULE_PARM_DESC(irq_type, "IRQ mode selection in pci_endpoint_test (0 - Legacy, 1 - MSI, 2 - MSI-X)");
  81. enum pci_barno {
  82. BAR_0,
  83. BAR_1,
  84. BAR_2,
  85. BAR_3,
  86. BAR_4,
  87. BAR_5,
  88. };
  89. struct pci_endpoint_test {
  90. struct pci_dev *pdev;
  91. void __iomem *base;
  92. void __iomem *bar[PCI_STD_NUM_BARS];
  93. struct completion irq_raised;
  94. int last_irq;
  95. int num_irqs;
  96. int irq_type;
  97. /* mutex to protect the ioctls */
  98. struct mutex mutex;
  99. struct miscdevice miscdev;
  100. enum pci_barno test_reg_bar;
  101. size_t alignment;
  102. const char *name;
  103. };
  104. struct pci_endpoint_test_data {
  105. enum pci_barno test_reg_bar;
  106. size_t alignment;
  107. int irq_type;
  108. };
  109. static inline u32 pci_endpoint_test_readl(struct pci_endpoint_test *test,
  110. u32 offset)
  111. {
  112. return readl(test->base + offset);
  113. }
  114. static inline void pci_endpoint_test_writel(struct pci_endpoint_test *test,
  115. u32 offset, u32 value)
  116. {
  117. writel(value, test->base + offset);
  118. }
  119. static irqreturn_t pci_endpoint_test_irqhandler(int irq, void *dev_id)
  120. {
  121. struct pci_endpoint_test *test = dev_id;
  122. u32 reg;
  123. reg = pci_endpoint_test_readl(test, PCI_ENDPOINT_TEST_STATUS);
  124. if (reg & STATUS_IRQ_RAISED) {
  125. test->last_irq = irq;
  126. complete(&test->irq_raised);
  127. }
  128. return IRQ_HANDLED;
  129. }
  130. static void pci_endpoint_test_free_irq_vectors(struct pci_endpoint_test *test)
  131. {
  132. struct pci_dev *pdev = test->pdev;
  133. pci_free_irq_vectors(pdev);
  134. test->irq_type = IRQ_TYPE_UNDEFINED;
  135. }
  136. static bool pci_endpoint_test_alloc_irq_vectors(struct pci_endpoint_test *test,
  137. int type)
  138. {
  139. int irq = -1;
  140. struct pci_dev *pdev = test->pdev;
  141. struct device *dev = &pdev->dev;
  142. bool res = true;
  143. switch (type) {
  144. case IRQ_TYPE_INTX:
  145. irq = pci_alloc_irq_vectors(pdev, 1, 1, PCI_IRQ_INTX);
  146. if (irq < 0)
  147. dev_err(dev, "Failed to get Legacy interrupt\n");
  148. break;
  149. case IRQ_TYPE_MSI:
  150. irq = pci_alloc_irq_vectors(pdev, 1, 32, PCI_IRQ_MSI);
  151. if (irq < 0)
  152. dev_err(dev, "Failed to get MSI interrupts\n");
  153. break;
  154. case IRQ_TYPE_MSIX:
  155. irq = pci_alloc_irq_vectors(pdev, 1, 2048, PCI_IRQ_MSIX);
  156. if (irq < 0)
  157. dev_err(dev, "Failed to get MSI-X interrupts\n");
  158. break;
  159. default:
  160. dev_err(dev, "Invalid IRQ type selected\n");
  161. }
  162. if (irq < 0) {
  163. irq = 0;
  164. res = false;
  165. }
  166. test->irq_type = type;
  167. test->num_irqs = irq;
  168. return res;
  169. }
  170. static void pci_endpoint_test_release_irq(struct pci_endpoint_test *test)
  171. {
  172. int i;
  173. struct pci_dev *pdev = test->pdev;
  174. struct device *dev = &pdev->dev;
  175. for (i = 0; i < test->num_irqs; i++)
  176. devm_free_irq(dev, pci_irq_vector(pdev, i), test);
  177. test->num_irqs = 0;
  178. }
  179. static bool pci_endpoint_test_request_irq(struct pci_endpoint_test *test)
  180. {
  181. int i;
  182. int err;
  183. struct pci_dev *pdev = test->pdev;
  184. struct device *dev = &pdev->dev;
  185. for (i = 0; i < test->num_irqs; i++) {
  186. err = devm_request_irq(dev, pci_irq_vector(pdev, i),
  187. pci_endpoint_test_irqhandler,
  188. IRQF_SHARED, test->name, test);
  189. if (err)
  190. goto fail;
  191. }
  192. return true;
  193. fail:
  194. switch (test->irq_type) {
  195. case IRQ_TYPE_INTX:
  196. dev_err(dev, "Failed to request IRQ %d for Legacy\n",
  197. pci_irq_vector(pdev, i));
  198. break;
  199. case IRQ_TYPE_MSI:
  200. dev_err(dev, "Failed to request IRQ %d for MSI %d\n",
  201. pci_irq_vector(pdev, i),
  202. i + 1);
  203. break;
  204. case IRQ_TYPE_MSIX:
  205. dev_err(dev, "Failed to request IRQ %d for MSI-X %d\n",
  206. pci_irq_vector(pdev, i),
  207. i + 1);
  208. break;
  209. }
  210. test->num_irqs = i;
  211. pci_endpoint_test_release_irq(test);
  212. return false;
  213. }
  214. static const u32 bar_test_pattern[] = {
  215. 0xA0A0A0A0,
  216. 0xA1A1A1A1,
  217. 0xA2A2A2A2,
  218. 0xA3A3A3A3,
  219. 0xA4A4A4A4,
  220. 0xA5A5A5A5,
  221. };
  222. static int pci_endpoint_test_bar_memcmp(struct pci_endpoint_test *test,
  223. enum pci_barno barno, int offset,
  224. void *write_buf, void *read_buf,
  225. int size)
  226. {
  227. memset(write_buf, bar_test_pattern[barno], size);
  228. memcpy_toio(test->bar[barno] + offset, write_buf, size);
  229. memcpy_fromio(read_buf, test->bar[barno] + offset, size);
  230. return memcmp(write_buf, read_buf, size);
  231. }
  232. static bool pci_endpoint_test_bar(struct pci_endpoint_test *test,
  233. enum pci_barno barno)
  234. {
  235. int j, bar_size, buf_size, iters, remain;
  236. void *write_buf __free(kfree) = NULL;
  237. void *read_buf __free(kfree) = NULL;
  238. struct pci_dev *pdev = test->pdev;
  239. bar_size = pci_resource_len(pdev, barno);
  240. if (!bar_size)
  241. return -ENODATA;
  242. if (!test->bar[barno])
  243. return false;
  244. if (barno == test->test_reg_bar)
  245. bar_size = 0x4;
  246. /*
  247. * Allocate a buffer of max size 1MB, and reuse that buffer while
  248. * iterating over the whole BAR size (which might be much larger).
  249. */
  250. buf_size = min(SZ_1M, bar_size);
  251. write_buf = kmalloc(buf_size, GFP_KERNEL);
  252. if (!write_buf)
  253. return false;
  254. read_buf = kmalloc(buf_size, GFP_KERNEL);
  255. if (!read_buf)
  256. return false;
  257. iters = bar_size / buf_size;
  258. for (j = 0; j < iters; j++)
  259. if (pci_endpoint_test_bar_memcmp(test, barno, buf_size * j,
  260. write_buf, read_buf, buf_size))
  261. return false;
  262. remain = bar_size % buf_size;
  263. if (remain)
  264. if (pci_endpoint_test_bar_memcmp(test, barno, buf_size * iters,
  265. write_buf, read_buf, remain))
  266. return false;
  267. return true;
  268. }
  269. static bool pci_endpoint_test_intx_irq(struct pci_endpoint_test *test)
  270. {
  271. u32 val;
  272. pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_IRQ_TYPE,
  273. IRQ_TYPE_INTX);
  274. pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_IRQ_NUMBER, 0);
  275. pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_COMMAND,
  276. COMMAND_RAISE_INTX_IRQ);
  277. val = wait_for_completion_timeout(&test->irq_raised,
  278. msecs_to_jiffies(1000));
  279. if (!val)
  280. return false;
  281. return true;
  282. }
  283. static bool pci_endpoint_test_msi_irq(struct pci_endpoint_test *test,
  284. u16 msi_num, bool msix)
  285. {
  286. u32 val;
  287. struct pci_dev *pdev = test->pdev;
  288. pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_IRQ_TYPE,
  289. msix ? IRQ_TYPE_MSIX : IRQ_TYPE_MSI);
  290. pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_IRQ_NUMBER, msi_num);
  291. pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_COMMAND,
  292. msix ? COMMAND_RAISE_MSIX_IRQ :
  293. COMMAND_RAISE_MSI_IRQ);
  294. val = wait_for_completion_timeout(&test->irq_raised,
  295. msecs_to_jiffies(1000));
  296. if (!val)
  297. return false;
  298. return pci_irq_vector(pdev, msi_num - 1) == test->last_irq;
  299. }
  300. static int pci_endpoint_test_validate_xfer_params(struct device *dev,
  301. struct pci_endpoint_test_xfer_param *param, size_t alignment)
  302. {
  303. if (!param->size) {
  304. dev_dbg(dev, "Data size is zero\n");
  305. return -EINVAL;
  306. }
  307. if (param->size > SIZE_MAX - alignment) {
  308. dev_dbg(dev, "Maximum transfer data size exceeded\n");
  309. return -EINVAL;
  310. }
  311. return 0;
  312. }
  313. static bool pci_endpoint_test_copy(struct pci_endpoint_test *test,
  314. unsigned long arg)
  315. {
  316. struct pci_endpoint_test_xfer_param param;
  317. bool ret = false;
  318. void *src_addr;
  319. void *dst_addr;
  320. u32 flags = 0;
  321. bool use_dma;
  322. size_t size;
  323. dma_addr_t src_phys_addr;
  324. dma_addr_t dst_phys_addr;
  325. struct pci_dev *pdev = test->pdev;
  326. struct device *dev = &pdev->dev;
  327. void *orig_src_addr;
  328. dma_addr_t orig_src_phys_addr;
  329. void *orig_dst_addr;
  330. dma_addr_t orig_dst_phys_addr;
  331. size_t offset;
  332. size_t alignment = test->alignment;
  333. int irq_type = test->irq_type;
  334. u32 src_crc32;
  335. u32 dst_crc32;
  336. int err;
  337. err = copy_from_user(&param, (void __user *)arg, sizeof(param));
  338. if (err) {
  339. dev_err(dev, "Failed to get transfer param\n");
  340. return false;
  341. }
  342. err = pci_endpoint_test_validate_xfer_params(dev, &param, alignment);
  343. if (err)
  344. return false;
  345. size = param.size;
  346. use_dma = !!(param.flags & PCITEST_FLAGS_USE_DMA);
  347. if (use_dma)
  348. flags |= FLAG_USE_DMA;
  349. if (irq_type < IRQ_TYPE_INTX || irq_type > IRQ_TYPE_MSIX) {
  350. dev_err(dev, "Invalid IRQ type option\n");
  351. goto err;
  352. }
  353. orig_src_addr = kzalloc(size + alignment, GFP_KERNEL);
  354. if (!orig_src_addr) {
  355. dev_err(dev, "Failed to allocate source buffer\n");
  356. ret = false;
  357. goto err;
  358. }
  359. get_random_bytes(orig_src_addr, size + alignment);
  360. orig_src_phys_addr = dma_map_single(dev, orig_src_addr,
  361. size + alignment, DMA_TO_DEVICE);
  362. if (dma_mapping_error(dev, orig_src_phys_addr)) {
  363. dev_err(dev, "failed to map source buffer address\n");
  364. ret = false;
  365. goto err_src_phys_addr;
  366. }
  367. if (alignment && !IS_ALIGNED(orig_src_phys_addr, alignment)) {
  368. src_phys_addr = PTR_ALIGN(orig_src_phys_addr, alignment);
  369. offset = src_phys_addr - orig_src_phys_addr;
  370. src_addr = orig_src_addr + offset;
  371. } else {
  372. src_phys_addr = orig_src_phys_addr;
  373. src_addr = orig_src_addr;
  374. }
  375. pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_LOWER_SRC_ADDR,
  376. lower_32_bits(src_phys_addr));
  377. pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_UPPER_SRC_ADDR,
  378. upper_32_bits(src_phys_addr));
  379. src_crc32 = crc32_le(~0, src_addr, size);
  380. orig_dst_addr = kzalloc(size + alignment, GFP_KERNEL);
  381. if (!orig_dst_addr) {
  382. dev_err(dev, "Failed to allocate destination address\n");
  383. ret = false;
  384. goto err_dst_addr;
  385. }
  386. orig_dst_phys_addr = dma_map_single(dev, orig_dst_addr,
  387. size + alignment, DMA_FROM_DEVICE);
  388. if (dma_mapping_error(dev, orig_dst_phys_addr)) {
  389. dev_err(dev, "failed to map destination buffer address\n");
  390. ret = false;
  391. goto err_dst_phys_addr;
  392. }
  393. if (alignment && !IS_ALIGNED(orig_dst_phys_addr, alignment)) {
  394. dst_phys_addr = PTR_ALIGN(orig_dst_phys_addr, alignment);
  395. offset = dst_phys_addr - orig_dst_phys_addr;
  396. dst_addr = orig_dst_addr + offset;
  397. } else {
  398. dst_phys_addr = orig_dst_phys_addr;
  399. dst_addr = orig_dst_addr;
  400. }
  401. pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_LOWER_DST_ADDR,
  402. lower_32_bits(dst_phys_addr));
  403. pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_UPPER_DST_ADDR,
  404. upper_32_bits(dst_phys_addr));
  405. pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_SIZE,
  406. size);
  407. pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_FLAGS, flags);
  408. pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_IRQ_TYPE, irq_type);
  409. pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_IRQ_NUMBER, 1);
  410. pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_COMMAND,
  411. COMMAND_COPY);
  412. wait_for_completion(&test->irq_raised);
  413. dma_unmap_single(dev, orig_dst_phys_addr, size + alignment,
  414. DMA_FROM_DEVICE);
  415. dst_crc32 = crc32_le(~0, dst_addr, size);
  416. if (dst_crc32 == src_crc32)
  417. ret = true;
  418. err_dst_phys_addr:
  419. kfree(orig_dst_addr);
  420. err_dst_addr:
  421. dma_unmap_single(dev, orig_src_phys_addr, size + alignment,
  422. DMA_TO_DEVICE);
  423. err_src_phys_addr:
  424. kfree(orig_src_addr);
  425. err:
  426. return ret;
  427. }
  428. static bool pci_endpoint_test_write(struct pci_endpoint_test *test,
  429. unsigned long arg)
  430. {
  431. struct pci_endpoint_test_xfer_param param;
  432. bool ret = false;
  433. u32 flags = 0;
  434. bool use_dma;
  435. u32 reg;
  436. void *addr;
  437. dma_addr_t phys_addr;
  438. struct pci_dev *pdev = test->pdev;
  439. struct device *dev = &pdev->dev;
  440. void *orig_addr;
  441. dma_addr_t orig_phys_addr;
  442. size_t offset;
  443. size_t alignment = test->alignment;
  444. int irq_type = test->irq_type;
  445. size_t size;
  446. u32 crc32;
  447. int err;
  448. err = copy_from_user(&param, (void __user *)arg, sizeof(param));
  449. if (err != 0) {
  450. dev_err(dev, "Failed to get transfer param\n");
  451. return false;
  452. }
  453. err = pci_endpoint_test_validate_xfer_params(dev, &param, alignment);
  454. if (err)
  455. return false;
  456. size = param.size;
  457. use_dma = !!(param.flags & PCITEST_FLAGS_USE_DMA);
  458. if (use_dma)
  459. flags |= FLAG_USE_DMA;
  460. if (irq_type < IRQ_TYPE_INTX || irq_type > IRQ_TYPE_MSIX) {
  461. dev_err(dev, "Invalid IRQ type option\n");
  462. goto err;
  463. }
  464. orig_addr = kzalloc(size + alignment, GFP_KERNEL);
  465. if (!orig_addr) {
  466. dev_err(dev, "Failed to allocate address\n");
  467. ret = false;
  468. goto err;
  469. }
  470. get_random_bytes(orig_addr, size + alignment);
  471. orig_phys_addr = dma_map_single(dev, orig_addr, size + alignment,
  472. DMA_TO_DEVICE);
  473. if (dma_mapping_error(dev, orig_phys_addr)) {
  474. dev_err(dev, "failed to map source buffer address\n");
  475. ret = false;
  476. goto err_phys_addr;
  477. }
  478. if (alignment && !IS_ALIGNED(orig_phys_addr, alignment)) {
  479. phys_addr = PTR_ALIGN(orig_phys_addr, alignment);
  480. offset = phys_addr - orig_phys_addr;
  481. addr = orig_addr + offset;
  482. } else {
  483. phys_addr = orig_phys_addr;
  484. addr = orig_addr;
  485. }
  486. crc32 = crc32_le(~0, addr, size);
  487. pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_CHECKSUM,
  488. crc32);
  489. pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_LOWER_SRC_ADDR,
  490. lower_32_bits(phys_addr));
  491. pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_UPPER_SRC_ADDR,
  492. upper_32_bits(phys_addr));
  493. pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_SIZE, size);
  494. pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_FLAGS, flags);
  495. pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_IRQ_TYPE, irq_type);
  496. pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_IRQ_NUMBER, 1);
  497. pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_COMMAND,
  498. COMMAND_READ);
  499. wait_for_completion(&test->irq_raised);
  500. reg = pci_endpoint_test_readl(test, PCI_ENDPOINT_TEST_STATUS);
  501. if (reg & STATUS_READ_SUCCESS)
  502. ret = true;
  503. dma_unmap_single(dev, orig_phys_addr, size + alignment,
  504. DMA_TO_DEVICE);
  505. err_phys_addr:
  506. kfree(orig_addr);
  507. err:
  508. return ret;
  509. }
  510. static bool pci_endpoint_test_read(struct pci_endpoint_test *test,
  511. unsigned long arg)
  512. {
  513. struct pci_endpoint_test_xfer_param param;
  514. bool ret = false;
  515. u32 flags = 0;
  516. bool use_dma;
  517. size_t size;
  518. void *addr;
  519. dma_addr_t phys_addr;
  520. struct pci_dev *pdev = test->pdev;
  521. struct device *dev = &pdev->dev;
  522. void *orig_addr;
  523. dma_addr_t orig_phys_addr;
  524. size_t offset;
  525. size_t alignment = test->alignment;
  526. int irq_type = test->irq_type;
  527. u32 crc32;
  528. int err;
  529. err = copy_from_user(&param, (void __user *)arg, sizeof(param));
  530. if (err) {
  531. dev_err(dev, "Failed to get transfer param\n");
  532. return false;
  533. }
  534. err = pci_endpoint_test_validate_xfer_params(dev, &param, alignment);
  535. if (err)
  536. return false;
  537. size = param.size;
  538. use_dma = !!(param.flags & PCITEST_FLAGS_USE_DMA);
  539. if (use_dma)
  540. flags |= FLAG_USE_DMA;
  541. if (irq_type < IRQ_TYPE_INTX || irq_type > IRQ_TYPE_MSIX) {
  542. dev_err(dev, "Invalid IRQ type option\n");
  543. goto err;
  544. }
  545. orig_addr = kzalloc(size + alignment, GFP_KERNEL);
  546. if (!orig_addr) {
  547. dev_err(dev, "Failed to allocate destination address\n");
  548. ret = false;
  549. goto err;
  550. }
  551. orig_phys_addr = dma_map_single(dev, orig_addr, size + alignment,
  552. DMA_FROM_DEVICE);
  553. if (dma_mapping_error(dev, orig_phys_addr)) {
  554. dev_err(dev, "failed to map source buffer address\n");
  555. ret = false;
  556. goto err_phys_addr;
  557. }
  558. if (alignment && !IS_ALIGNED(orig_phys_addr, alignment)) {
  559. phys_addr = PTR_ALIGN(orig_phys_addr, alignment);
  560. offset = phys_addr - orig_phys_addr;
  561. addr = orig_addr + offset;
  562. } else {
  563. phys_addr = orig_phys_addr;
  564. addr = orig_addr;
  565. }
  566. pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_LOWER_DST_ADDR,
  567. lower_32_bits(phys_addr));
  568. pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_UPPER_DST_ADDR,
  569. upper_32_bits(phys_addr));
  570. pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_SIZE, size);
  571. pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_FLAGS, flags);
  572. pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_IRQ_TYPE, irq_type);
  573. pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_IRQ_NUMBER, 1);
  574. pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_COMMAND,
  575. COMMAND_WRITE);
  576. wait_for_completion(&test->irq_raised);
  577. dma_unmap_single(dev, orig_phys_addr, size + alignment,
  578. DMA_FROM_DEVICE);
  579. crc32 = crc32_le(~0, addr, size);
  580. if (crc32 == pci_endpoint_test_readl(test, PCI_ENDPOINT_TEST_CHECKSUM))
  581. ret = true;
  582. err_phys_addr:
  583. kfree(orig_addr);
  584. err:
  585. return ret;
  586. }
  587. static bool pci_endpoint_test_clear_irq(struct pci_endpoint_test *test)
  588. {
  589. pci_endpoint_test_release_irq(test);
  590. pci_endpoint_test_free_irq_vectors(test);
  591. return true;
  592. }
  593. static bool pci_endpoint_test_set_irq(struct pci_endpoint_test *test,
  594. int req_irq_type)
  595. {
  596. struct pci_dev *pdev = test->pdev;
  597. struct device *dev = &pdev->dev;
  598. if (req_irq_type < IRQ_TYPE_INTX || req_irq_type > IRQ_TYPE_MSIX) {
  599. dev_err(dev, "Invalid IRQ type option\n");
  600. return false;
  601. }
  602. if (test->irq_type == req_irq_type)
  603. return true;
  604. pci_endpoint_test_release_irq(test);
  605. pci_endpoint_test_free_irq_vectors(test);
  606. if (!pci_endpoint_test_alloc_irq_vectors(test, req_irq_type))
  607. goto err;
  608. if (!pci_endpoint_test_request_irq(test))
  609. goto err;
  610. irq_type = test->irq_type;
  611. return true;
  612. err:
  613. pci_endpoint_test_free_irq_vectors(test);
  614. return false;
  615. }
  616. static long pci_endpoint_test_ioctl(struct file *file, unsigned int cmd,
  617. unsigned long arg)
  618. {
  619. int ret = -EINVAL;
  620. enum pci_barno bar;
  621. struct pci_endpoint_test *test = to_endpoint_test(file->private_data);
  622. struct pci_dev *pdev = test->pdev;
  623. mutex_lock(&test->mutex);
  624. reinit_completion(&test->irq_raised);
  625. test->last_irq = -ENODATA;
  626. switch (cmd) {
  627. case PCITEST_BAR:
  628. bar = arg;
  629. if (bar > BAR_5)
  630. goto ret;
  631. if (is_am654_pci_dev(pdev) && bar == BAR_0)
  632. goto ret;
  633. ret = pci_endpoint_test_bar(test, bar);
  634. break;
  635. case PCITEST_INTX_IRQ:
  636. ret = pci_endpoint_test_intx_irq(test);
  637. break;
  638. case PCITEST_MSI:
  639. case PCITEST_MSIX:
  640. ret = pci_endpoint_test_msi_irq(test, arg, cmd == PCITEST_MSIX);
  641. break;
  642. case PCITEST_WRITE:
  643. ret = pci_endpoint_test_write(test, arg);
  644. break;
  645. case PCITEST_READ:
  646. ret = pci_endpoint_test_read(test, arg);
  647. break;
  648. case PCITEST_COPY:
  649. ret = pci_endpoint_test_copy(test, arg);
  650. break;
  651. case PCITEST_SET_IRQTYPE:
  652. ret = pci_endpoint_test_set_irq(test, arg);
  653. break;
  654. case PCITEST_GET_IRQTYPE:
  655. ret = irq_type;
  656. break;
  657. case PCITEST_CLEAR_IRQ:
  658. ret = pci_endpoint_test_clear_irq(test);
  659. break;
  660. }
  661. ret:
  662. mutex_unlock(&test->mutex);
  663. return ret;
  664. }
  665. static const struct file_operations pci_endpoint_test_fops = {
  666. .owner = THIS_MODULE,
  667. .unlocked_ioctl = pci_endpoint_test_ioctl,
  668. };
  669. static int pci_endpoint_test_probe(struct pci_dev *pdev,
  670. const struct pci_device_id *ent)
  671. {
  672. int err;
  673. int id;
  674. char name[24];
  675. enum pci_barno bar;
  676. void __iomem *base;
  677. struct device *dev = &pdev->dev;
  678. struct pci_endpoint_test *test;
  679. struct pci_endpoint_test_data *data;
  680. enum pci_barno test_reg_bar = BAR_0;
  681. struct miscdevice *misc_device;
  682. if (pci_is_bridge(pdev))
  683. return -ENODEV;
  684. test = devm_kzalloc(dev, sizeof(*test), GFP_KERNEL);
  685. if (!test)
  686. return -ENOMEM;
  687. test->test_reg_bar = 0;
  688. test->alignment = 0;
  689. test->pdev = pdev;
  690. test->irq_type = IRQ_TYPE_UNDEFINED;
  691. if (no_msi)
  692. irq_type = IRQ_TYPE_INTX;
  693. data = (struct pci_endpoint_test_data *)ent->driver_data;
  694. if (data) {
  695. test_reg_bar = data->test_reg_bar;
  696. test->test_reg_bar = test_reg_bar;
  697. test->alignment = data->alignment;
  698. irq_type = data->irq_type;
  699. }
  700. init_completion(&test->irq_raised);
  701. mutex_init(&test->mutex);
  702. dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(48));
  703. err = pci_enable_device(pdev);
  704. if (err) {
  705. dev_err(dev, "Cannot enable PCI device\n");
  706. return err;
  707. }
  708. err = pci_request_regions(pdev, DRV_MODULE_NAME);
  709. if (err) {
  710. dev_err(dev, "Cannot obtain PCI resources\n");
  711. goto err_disable_pdev;
  712. }
  713. pci_set_master(pdev);
  714. if (!pci_endpoint_test_alloc_irq_vectors(test, irq_type)) {
  715. err = -EINVAL;
  716. goto err_disable_irq;
  717. }
  718. for (bar = 0; bar < PCI_STD_NUM_BARS; bar++) {
  719. if (pci_resource_flags(pdev, bar) & IORESOURCE_MEM) {
  720. base = pci_ioremap_bar(pdev, bar);
  721. if (!base) {
  722. dev_err(dev, "Failed to read BAR%d\n", bar);
  723. WARN_ON(bar == test_reg_bar);
  724. }
  725. test->bar[bar] = base;
  726. }
  727. }
  728. test->base = test->bar[test_reg_bar];
  729. if (!test->base) {
  730. err = -ENOMEM;
  731. dev_err(dev, "Cannot perform PCI test without BAR%d\n",
  732. test_reg_bar);
  733. goto err_iounmap;
  734. }
  735. pci_set_drvdata(pdev, test);
  736. id = ida_alloc(&pci_endpoint_test_ida, GFP_KERNEL);
  737. if (id < 0) {
  738. err = id;
  739. dev_err(dev, "Unable to get id\n");
  740. goto err_iounmap;
  741. }
  742. snprintf(name, sizeof(name), DRV_MODULE_NAME ".%d", id);
  743. test->name = kstrdup(name, GFP_KERNEL);
  744. if (!test->name) {
  745. err = -ENOMEM;
  746. goto err_ida_remove;
  747. }
  748. if (!pci_endpoint_test_request_irq(test)) {
  749. err = -EINVAL;
  750. goto err_kfree_test_name;
  751. }
  752. misc_device = &test->miscdev;
  753. misc_device->minor = MISC_DYNAMIC_MINOR;
  754. misc_device->name = kstrdup(name, GFP_KERNEL);
  755. if (!misc_device->name) {
  756. err = -ENOMEM;
  757. goto err_release_irq;
  758. }
  759. misc_device->parent = &pdev->dev;
  760. misc_device->fops = &pci_endpoint_test_fops;
  761. err = misc_register(misc_device);
  762. if (err) {
  763. dev_err(dev, "Failed to register device\n");
  764. goto err_kfree_name;
  765. }
  766. return 0;
  767. err_kfree_name:
  768. kfree(misc_device->name);
  769. err_release_irq:
  770. pci_endpoint_test_release_irq(test);
  771. err_kfree_test_name:
  772. kfree(test->name);
  773. err_ida_remove:
  774. ida_free(&pci_endpoint_test_ida, id);
  775. err_iounmap:
  776. for (bar = 0; bar < PCI_STD_NUM_BARS; bar++) {
  777. if (test->bar[bar])
  778. pci_iounmap(pdev, test->bar[bar]);
  779. }
  780. err_disable_irq:
  781. pci_endpoint_test_free_irq_vectors(test);
  782. pci_release_regions(pdev);
  783. err_disable_pdev:
  784. pci_disable_device(pdev);
  785. return err;
  786. }
  787. static void pci_endpoint_test_remove(struct pci_dev *pdev)
  788. {
  789. int id;
  790. enum pci_barno bar;
  791. struct pci_endpoint_test *test = pci_get_drvdata(pdev);
  792. struct miscdevice *misc_device = &test->miscdev;
  793. if (sscanf(misc_device->name, DRV_MODULE_NAME ".%d", &id) != 1)
  794. return;
  795. if (id < 0)
  796. return;
  797. pci_endpoint_test_release_irq(test);
  798. pci_endpoint_test_free_irq_vectors(test);
  799. misc_deregister(&test->miscdev);
  800. kfree(misc_device->name);
  801. kfree(test->name);
  802. ida_free(&pci_endpoint_test_ida, id);
  803. for (bar = 0; bar < PCI_STD_NUM_BARS; bar++) {
  804. if (test->bar[bar])
  805. pci_iounmap(pdev, test->bar[bar]);
  806. }
  807. pci_release_regions(pdev);
  808. pci_disable_device(pdev);
  809. }
  810. static const struct pci_endpoint_test_data default_data = {
  811. .test_reg_bar = BAR_0,
  812. .alignment = SZ_4K,
  813. .irq_type = IRQ_TYPE_MSI,
  814. };
  815. static const struct pci_endpoint_test_data am654_data = {
  816. .test_reg_bar = BAR_2,
  817. .alignment = SZ_64K,
  818. .irq_type = IRQ_TYPE_MSI,
  819. };
  820. static const struct pci_endpoint_test_data j721e_data = {
  821. .alignment = 256,
  822. .irq_type = IRQ_TYPE_MSI,
  823. };
  824. static const struct pci_endpoint_test_data rk3588_data = {
  825. .alignment = SZ_64K,
  826. .irq_type = IRQ_TYPE_MSI,
  827. };
  828. /*
  829. * If the controller's Vendor/Device ID are programmable, you may be able to
  830. * use one of the existing entries for testing instead of adding a new one.
  831. */
  832. static const struct pci_device_id pci_endpoint_test_tbl[] = {
  833. { PCI_DEVICE(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_DRA74x),
  834. .driver_data = (kernel_ulong_t)&default_data,
  835. },
  836. { PCI_DEVICE(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_DRA72x),
  837. .driver_data = (kernel_ulong_t)&default_data,
  838. },
  839. { PCI_DEVICE(PCI_VENDOR_ID_FREESCALE, 0x81c0),
  840. .driver_data = (kernel_ulong_t)&default_data,
  841. },
  842. { PCI_DEVICE(PCI_VENDOR_ID_FREESCALE, PCI_DEVICE_ID_IMX8),},
  843. { PCI_DEVICE(PCI_VENDOR_ID_FREESCALE, PCI_DEVICE_ID_LS1088A),
  844. .driver_data = (kernel_ulong_t)&default_data,
  845. },
  846. { PCI_DEVICE_DATA(SYNOPSYS, EDDA, NULL) },
  847. { PCI_DEVICE(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_AM654),
  848. .driver_data = (kernel_ulong_t)&am654_data
  849. },
  850. { PCI_DEVICE(PCI_VENDOR_ID_RENESAS, PCI_DEVICE_ID_RENESAS_R8A774A1),},
  851. { PCI_DEVICE(PCI_VENDOR_ID_RENESAS, PCI_DEVICE_ID_RENESAS_R8A774B1),},
  852. { PCI_DEVICE(PCI_VENDOR_ID_RENESAS, PCI_DEVICE_ID_RENESAS_R8A774C0),},
  853. { PCI_DEVICE(PCI_VENDOR_ID_RENESAS, PCI_DEVICE_ID_RENESAS_R8A774E1),},
  854. { PCI_DEVICE(PCI_VENDOR_ID_RENESAS, PCI_DEVICE_ID_RENESAS_R8A779F0),
  855. .driver_data = (kernel_ulong_t)&default_data,
  856. },
  857. { PCI_DEVICE(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_J721E),
  858. .driver_data = (kernel_ulong_t)&j721e_data,
  859. },
  860. { PCI_DEVICE(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_J7200),
  861. .driver_data = (kernel_ulong_t)&j721e_data,
  862. },
  863. { PCI_DEVICE(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_AM64),
  864. .driver_data = (kernel_ulong_t)&j721e_data,
  865. },
  866. { PCI_DEVICE(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_J721S2),
  867. .driver_data = (kernel_ulong_t)&j721e_data,
  868. },
  869. { PCI_DEVICE(PCI_VENDOR_ID_ROCKCHIP, PCI_DEVICE_ID_ROCKCHIP_RK3588),
  870. .driver_data = (kernel_ulong_t)&rk3588_data,
  871. },
  872. { }
  873. };
  874. MODULE_DEVICE_TABLE(pci, pci_endpoint_test_tbl);
  875. static struct pci_driver pci_endpoint_test_driver = {
  876. .name = DRV_MODULE_NAME,
  877. .id_table = pci_endpoint_test_tbl,
  878. .probe = pci_endpoint_test_probe,
  879. .remove = pci_endpoint_test_remove,
  880. .sriov_configure = pci_sriov_configure_simple,
  881. };
  882. module_pci_driver(pci_endpoint_test_driver);
  883. MODULE_DESCRIPTION("PCI ENDPOINT TEST HOST DRIVER");
  884. MODULE_AUTHOR("Kishon Vijay Abraham I <kishon@ti.com>");
  885. MODULE_LICENSE("GPL v2");