pci_endpoint_test.c 20 KB

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