pci-epf-test.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  1. /**
  2. * 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/io.h>
  22. #include <linux/module.h>
  23. #include <linux/slab.h>
  24. #include <linux/pci_ids.h>
  25. #include <linux/random.h>
  26. #include <linux/pci-epc.h>
  27. #include <linux/pci-epf.h>
  28. #include <linux/pci_regs.h>
  29. #define COMMAND_RAISE_LEGACY_IRQ BIT(0)
  30. #define COMMAND_RAISE_MSI_IRQ BIT(1)
  31. #define MSI_NUMBER_SHIFT 2
  32. #define MSI_NUMBER_MASK (0x3f << MSI_NUMBER_SHIFT)
  33. #define COMMAND_READ BIT(8)
  34. #define COMMAND_WRITE BIT(9)
  35. #define COMMAND_COPY BIT(10)
  36. #define STATUS_READ_SUCCESS BIT(0)
  37. #define STATUS_READ_FAIL BIT(1)
  38. #define STATUS_WRITE_SUCCESS BIT(2)
  39. #define STATUS_WRITE_FAIL BIT(3)
  40. #define STATUS_COPY_SUCCESS BIT(4)
  41. #define STATUS_COPY_FAIL BIT(5)
  42. #define STATUS_IRQ_RAISED BIT(6)
  43. #define STATUS_SRC_ADDR_INVALID BIT(7)
  44. #define STATUS_DST_ADDR_INVALID BIT(8)
  45. #define TIMER_RESOLUTION 1
  46. static struct workqueue_struct *kpcitest_workqueue;
  47. struct pci_epf_test {
  48. void *reg[6];
  49. struct pci_epf *epf;
  50. enum pci_barno test_reg_bar;
  51. bool linkup_notifier;
  52. struct delayed_work cmd_handler;
  53. };
  54. struct pci_epf_test_reg {
  55. u32 magic;
  56. u32 command;
  57. u32 status;
  58. u64 src_addr;
  59. u64 dst_addr;
  60. u32 size;
  61. u32 checksum;
  62. } __packed;
  63. static struct pci_epf_header test_header = {
  64. .vendorid = PCI_ANY_ID,
  65. .deviceid = PCI_ANY_ID,
  66. .baseclass_code = PCI_CLASS_OTHERS,
  67. .interrupt_pin = PCI_INTERRUPT_INTA,
  68. };
  69. struct pci_epf_test_data {
  70. enum pci_barno test_reg_bar;
  71. bool linkup_notifier;
  72. };
  73. static int bar_size[] = { 512, 512, 1024, 16384, 131072, 1048576 };
  74. static int pci_epf_test_copy(struct pci_epf_test *epf_test)
  75. {
  76. int ret;
  77. void __iomem *src_addr;
  78. void __iomem *dst_addr;
  79. phys_addr_t src_phys_addr;
  80. phys_addr_t dst_phys_addr;
  81. struct pci_epf *epf = epf_test->epf;
  82. struct device *dev = &epf->dev;
  83. struct pci_epc *epc = epf->epc;
  84. enum pci_barno test_reg_bar = epf_test->test_reg_bar;
  85. struct pci_epf_test_reg *reg = epf_test->reg[test_reg_bar];
  86. src_addr = pci_epc_mem_alloc_addr(epc, &src_phys_addr, reg->size);
  87. if (!src_addr) {
  88. dev_err(dev, "failed to allocate source address\n");
  89. reg->status = STATUS_SRC_ADDR_INVALID;
  90. ret = -ENOMEM;
  91. goto err;
  92. }
  93. ret = pci_epc_map_addr(epc, src_phys_addr, reg->src_addr, reg->size);
  94. if (ret) {
  95. dev_err(dev, "failed to map source address\n");
  96. reg->status = STATUS_SRC_ADDR_INVALID;
  97. goto err_src_addr;
  98. }
  99. dst_addr = pci_epc_mem_alloc_addr(epc, &dst_phys_addr, reg->size);
  100. if (!dst_addr) {
  101. dev_err(dev, "failed to allocate destination address\n");
  102. reg->status = STATUS_DST_ADDR_INVALID;
  103. ret = -ENOMEM;
  104. goto err_src_map_addr;
  105. }
  106. ret = pci_epc_map_addr(epc, dst_phys_addr, reg->dst_addr, reg->size);
  107. if (ret) {
  108. dev_err(dev, "failed to map destination address\n");
  109. reg->status = STATUS_DST_ADDR_INVALID;
  110. goto err_dst_addr;
  111. }
  112. memcpy(dst_addr, src_addr, reg->size);
  113. pci_epc_unmap_addr(epc, dst_phys_addr);
  114. err_dst_addr:
  115. pci_epc_mem_free_addr(epc, dst_phys_addr, dst_addr, reg->size);
  116. err_src_map_addr:
  117. pci_epc_unmap_addr(epc, src_phys_addr);
  118. err_src_addr:
  119. pci_epc_mem_free_addr(epc, src_phys_addr, src_addr, reg->size);
  120. err:
  121. return ret;
  122. }
  123. static int pci_epf_test_read(struct pci_epf_test *epf_test)
  124. {
  125. int ret;
  126. void __iomem *src_addr;
  127. void *buf;
  128. u32 crc32;
  129. phys_addr_t phys_addr;
  130. struct pci_epf *epf = epf_test->epf;
  131. struct device *dev = &epf->dev;
  132. struct pci_epc *epc = epf->epc;
  133. enum pci_barno test_reg_bar = epf_test->test_reg_bar;
  134. struct pci_epf_test_reg *reg = epf_test->reg[test_reg_bar];
  135. src_addr = pci_epc_mem_alloc_addr(epc, &phys_addr, reg->size);
  136. if (!src_addr) {
  137. dev_err(dev, "failed to allocate address\n");
  138. reg->status = STATUS_SRC_ADDR_INVALID;
  139. ret = -ENOMEM;
  140. goto err;
  141. }
  142. ret = pci_epc_map_addr(epc, phys_addr, reg->src_addr, reg->size);
  143. if (ret) {
  144. dev_err(dev, "failed to map address\n");
  145. reg->status = STATUS_SRC_ADDR_INVALID;
  146. goto err_addr;
  147. }
  148. buf = kzalloc(reg->size, GFP_KERNEL);
  149. if (!buf) {
  150. ret = -ENOMEM;
  151. goto err_map_addr;
  152. }
  153. memcpy(buf, src_addr, reg->size);
  154. crc32 = crc32_le(~0, buf, reg->size);
  155. if (crc32 != reg->checksum)
  156. ret = -EIO;
  157. kfree(buf);
  158. err_map_addr:
  159. pci_epc_unmap_addr(epc, phys_addr);
  160. err_addr:
  161. pci_epc_mem_free_addr(epc, phys_addr, src_addr, reg->size);
  162. err:
  163. return ret;
  164. }
  165. static int pci_epf_test_write(struct pci_epf_test *epf_test)
  166. {
  167. int ret;
  168. void __iomem *dst_addr;
  169. void *buf;
  170. phys_addr_t phys_addr;
  171. struct pci_epf *epf = epf_test->epf;
  172. struct device *dev = &epf->dev;
  173. struct pci_epc *epc = epf->epc;
  174. enum pci_barno test_reg_bar = epf_test->test_reg_bar;
  175. struct pci_epf_test_reg *reg = epf_test->reg[test_reg_bar];
  176. dst_addr = pci_epc_mem_alloc_addr(epc, &phys_addr, reg->size);
  177. if (!dst_addr) {
  178. dev_err(dev, "failed to allocate address\n");
  179. reg->status = STATUS_DST_ADDR_INVALID;
  180. ret = -ENOMEM;
  181. goto err;
  182. }
  183. ret = pci_epc_map_addr(epc, phys_addr, reg->dst_addr, reg->size);
  184. if (ret) {
  185. dev_err(dev, "failed to map address\n");
  186. reg->status = STATUS_DST_ADDR_INVALID;
  187. goto err_addr;
  188. }
  189. buf = kzalloc(reg->size, GFP_KERNEL);
  190. if (!buf) {
  191. ret = -ENOMEM;
  192. goto err_map_addr;
  193. }
  194. get_random_bytes(buf, reg->size);
  195. reg->checksum = crc32_le(~0, buf, reg->size);
  196. memcpy(dst_addr, buf, reg->size);
  197. /*
  198. * wait 1ms inorder for the write to complete. Without this delay L3
  199. * error in observed in the host system.
  200. */
  201. mdelay(1);
  202. kfree(buf);
  203. err_map_addr:
  204. pci_epc_unmap_addr(epc, phys_addr);
  205. err_addr:
  206. pci_epc_mem_free_addr(epc, phys_addr, dst_addr, reg->size);
  207. err:
  208. return ret;
  209. }
  210. static void pci_epf_test_raise_irq(struct pci_epf_test *epf_test)
  211. {
  212. u8 irq;
  213. u8 msi_count;
  214. struct pci_epf *epf = epf_test->epf;
  215. struct pci_epc *epc = epf->epc;
  216. enum pci_barno test_reg_bar = epf_test->test_reg_bar;
  217. struct pci_epf_test_reg *reg = epf_test->reg[test_reg_bar];
  218. reg->status |= STATUS_IRQ_RAISED;
  219. msi_count = pci_epc_get_msi(epc);
  220. irq = (reg->command & MSI_NUMBER_MASK) >> MSI_NUMBER_SHIFT;
  221. if (irq > msi_count || msi_count <= 0)
  222. pci_epc_raise_irq(epc, PCI_EPC_IRQ_LEGACY, 0);
  223. else
  224. pci_epc_raise_irq(epc, PCI_EPC_IRQ_MSI, irq);
  225. }
  226. static void pci_epf_test_cmd_handler(struct work_struct *work)
  227. {
  228. int ret;
  229. u8 irq;
  230. u8 msi_count;
  231. u32 command;
  232. struct pci_epf_test *epf_test = container_of(work, struct pci_epf_test,
  233. cmd_handler.work);
  234. struct pci_epf *epf = epf_test->epf;
  235. struct pci_epc *epc = epf->epc;
  236. enum pci_barno test_reg_bar = epf_test->test_reg_bar;
  237. struct pci_epf_test_reg *reg = epf_test->reg[test_reg_bar];
  238. command = reg->command;
  239. if (!command)
  240. goto reset_handler;
  241. reg->command = 0;
  242. reg->status = 0;
  243. if (command & COMMAND_RAISE_LEGACY_IRQ) {
  244. reg->status = STATUS_IRQ_RAISED;
  245. pci_epc_raise_irq(epc, PCI_EPC_IRQ_LEGACY, 0);
  246. goto reset_handler;
  247. }
  248. if (command & COMMAND_WRITE) {
  249. ret = pci_epf_test_write(epf_test);
  250. if (ret)
  251. reg->status |= STATUS_WRITE_FAIL;
  252. else
  253. reg->status |= STATUS_WRITE_SUCCESS;
  254. pci_epf_test_raise_irq(epf_test);
  255. goto reset_handler;
  256. }
  257. if (command & COMMAND_READ) {
  258. ret = pci_epf_test_read(epf_test);
  259. if (!ret)
  260. reg->status |= STATUS_READ_SUCCESS;
  261. else
  262. reg->status |= STATUS_READ_FAIL;
  263. pci_epf_test_raise_irq(epf_test);
  264. goto reset_handler;
  265. }
  266. if (command & COMMAND_COPY) {
  267. ret = pci_epf_test_copy(epf_test);
  268. if (!ret)
  269. reg->status |= STATUS_COPY_SUCCESS;
  270. else
  271. reg->status |= STATUS_COPY_FAIL;
  272. pci_epf_test_raise_irq(epf_test);
  273. goto reset_handler;
  274. }
  275. if (command & COMMAND_RAISE_MSI_IRQ) {
  276. msi_count = pci_epc_get_msi(epc);
  277. irq = (command & MSI_NUMBER_MASK) >> MSI_NUMBER_SHIFT;
  278. if (irq > msi_count || msi_count <= 0)
  279. goto reset_handler;
  280. reg->status = STATUS_IRQ_RAISED;
  281. pci_epc_raise_irq(epc, PCI_EPC_IRQ_MSI, irq);
  282. goto reset_handler;
  283. }
  284. reset_handler:
  285. queue_delayed_work(kpcitest_workqueue, &epf_test->cmd_handler,
  286. msecs_to_jiffies(1));
  287. }
  288. static void pci_epf_test_linkup(struct pci_epf *epf)
  289. {
  290. struct pci_epf_test *epf_test = epf_get_drvdata(epf);
  291. queue_delayed_work(kpcitest_workqueue, &epf_test->cmd_handler,
  292. msecs_to_jiffies(1));
  293. }
  294. static void pci_epf_test_unbind(struct pci_epf *epf)
  295. {
  296. struct pci_epf_test *epf_test = epf_get_drvdata(epf);
  297. struct pci_epc *epc = epf->epc;
  298. int bar;
  299. cancel_delayed_work(&epf_test->cmd_handler);
  300. pci_epc_stop(epc);
  301. for (bar = BAR_0; bar <= BAR_5; bar++) {
  302. if (epf_test->reg[bar]) {
  303. pci_epf_free_space(epf, epf_test->reg[bar], bar);
  304. pci_epc_clear_bar(epc, bar);
  305. }
  306. }
  307. }
  308. static int pci_epf_test_set_bar(struct pci_epf *epf)
  309. {
  310. int flags;
  311. int bar;
  312. int ret;
  313. struct pci_epf_bar *epf_bar;
  314. struct pci_epc *epc = epf->epc;
  315. struct device *dev = &epf->dev;
  316. struct pci_epf_test *epf_test = epf_get_drvdata(epf);
  317. enum pci_barno test_reg_bar = epf_test->test_reg_bar;
  318. flags = PCI_BASE_ADDRESS_SPACE_MEMORY | PCI_BASE_ADDRESS_MEM_TYPE_32;
  319. if (sizeof(dma_addr_t) == 0x8)
  320. flags |= PCI_BASE_ADDRESS_MEM_TYPE_64;
  321. for (bar = BAR_0; bar <= BAR_5; bar++) {
  322. epf_bar = &epf->bar[bar];
  323. ret = pci_epc_set_bar(epc, bar, epf_bar->phys_addr,
  324. epf_bar->size, flags);
  325. if (ret) {
  326. pci_epf_free_space(epf, epf_test->reg[bar], bar);
  327. dev_err(dev, "failed to set BAR%d\n", bar);
  328. if (bar == test_reg_bar)
  329. return ret;
  330. }
  331. }
  332. return 0;
  333. }
  334. static int pci_epf_test_alloc_space(struct pci_epf *epf)
  335. {
  336. struct pci_epf_test *epf_test = epf_get_drvdata(epf);
  337. struct device *dev = &epf->dev;
  338. void *base;
  339. int bar;
  340. enum pci_barno test_reg_bar = epf_test->test_reg_bar;
  341. base = pci_epf_alloc_space(epf, sizeof(struct pci_epf_test_reg),
  342. test_reg_bar);
  343. if (!base) {
  344. dev_err(dev, "failed to allocated register space\n");
  345. return -ENOMEM;
  346. }
  347. epf_test->reg[test_reg_bar] = base;
  348. for (bar = BAR_0; bar <= BAR_5; bar++) {
  349. if (bar == test_reg_bar)
  350. continue;
  351. base = pci_epf_alloc_space(epf, bar_size[bar], bar);
  352. if (!base)
  353. dev_err(dev, "failed to allocate space for BAR%d\n",
  354. bar);
  355. epf_test->reg[bar] = base;
  356. }
  357. return 0;
  358. }
  359. static int pci_epf_test_bind(struct pci_epf *epf)
  360. {
  361. int ret;
  362. struct pci_epf_test *epf_test = epf_get_drvdata(epf);
  363. struct pci_epf_header *header = epf->header;
  364. struct pci_epc *epc = epf->epc;
  365. struct device *dev = &epf->dev;
  366. ret = pci_epc_write_header(epc, header);
  367. if (ret) {
  368. dev_err(dev, "configuration header write failed\n");
  369. return ret;
  370. }
  371. ret = pci_epf_test_alloc_space(epf);
  372. if (ret)
  373. return ret;
  374. ret = pci_epf_test_set_bar(epf);
  375. if (ret)
  376. return ret;
  377. ret = pci_epc_set_msi(epc, epf->msi_interrupts);
  378. if (ret)
  379. return ret;
  380. ret = pci_epc_start(epc);
  381. if (ret) {
  382. dev_err(dev, "failed to start endpoint controller\n");
  383. return ret;
  384. }
  385. if (!epf_test->linkup_notifier)
  386. queue_work(kpcitest_workqueue, &epf_test->cmd_handler.work);
  387. return 0;
  388. }
  389. static const struct pci_epf_test_data k2g_data = {
  390. .test_reg_bar = BAR_1,
  391. .linkup_notifier = false
  392. };
  393. static const struct pci_epf_device_id pci_epf_test_ids[] = {
  394. {
  395. .name = "pci_epf_test",
  396. },
  397. {
  398. .name = "pci_epf_test_k2g",
  399. .driver_data = (kernel_ulong_t)&k2g_data,
  400. },
  401. {},
  402. };
  403. static int pci_epf_test_probe(struct pci_epf *epf)
  404. {
  405. struct pci_epf_test *epf_test;
  406. struct device *dev = &epf->dev;
  407. const struct pci_epf_device_id *match;
  408. struct pci_epf_test_data *data;
  409. enum pci_barno test_reg_bar = BAR_0;
  410. bool linkup_notifier = true;
  411. match = pci_epf_match_device(pci_epf_test_ids, epf);
  412. data = (struct pci_epf_test_data *)match->driver_data;
  413. if (data) {
  414. test_reg_bar = data->test_reg_bar;
  415. linkup_notifier = data->linkup_notifier;
  416. }
  417. epf_test = devm_kzalloc(dev, sizeof(*epf_test), GFP_KERNEL);
  418. if (!epf)
  419. return -ENOMEM;
  420. epf->header = &test_header;
  421. epf_test->epf = epf;
  422. epf_test->test_reg_bar = test_reg_bar;
  423. epf_test->linkup_notifier = linkup_notifier;
  424. INIT_DELAYED_WORK(&epf_test->cmd_handler, pci_epf_test_cmd_handler);
  425. epf_set_drvdata(epf, epf_test);
  426. return 0;
  427. }
  428. static int pci_epf_test_remove(struct pci_epf *epf)
  429. {
  430. struct pci_epf_test *epf_test = epf_get_drvdata(epf);
  431. kfree(epf_test);
  432. return 0;
  433. }
  434. static struct pci_epf_ops ops = {
  435. .unbind = pci_epf_test_unbind,
  436. .bind = pci_epf_test_bind,
  437. .linkup = pci_epf_test_linkup,
  438. };
  439. static struct pci_epf_driver test_driver = {
  440. .driver.name = "pci_epf_test",
  441. .probe = pci_epf_test_probe,
  442. .remove = pci_epf_test_remove,
  443. .id_table = pci_epf_test_ids,
  444. .ops = &ops,
  445. .owner = THIS_MODULE,
  446. };
  447. static int __init pci_epf_test_init(void)
  448. {
  449. int ret;
  450. kpcitest_workqueue = alloc_workqueue("kpcitest",
  451. WQ_MEM_RECLAIM | WQ_HIGHPRI, 0);
  452. ret = pci_epf_register_driver(&test_driver);
  453. if (ret) {
  454. pr_err("failed to register pci epf test driver --> %d\n", ret);
  455. return ret;
  456. }
  457. return 0;
  458. }
  459. module_init(pci_epf_test_init);
  460. static void __exit pci_epf_test_exit(void)
  461. {
  462. pci_epf_unregister_driver(&test_driver);
  463. }
  464. module_exit(pci_epf_test_exit);
  465. MODULE_DESCRIPTION("PCI EPF TEST DRIVER");
  466. MODULE_AUTHOR("Kishon Vijay Abraham I <kishon@ti.com>");
  467. MODULE_LICENSE("GPL v2");