pci.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. #include <linux/cdev.h> /* cdev_ */
  2. #include <linux/fs.h>
  3. #include <linux/init.h>
  4. #include <linux/interrupt.h>
  5. #include <linux/kernel.h>
  6. #include <linux/module.h>
  7. #include <linux/pci.h>
  8. #include <linux/uaccess.h> /* put_user */
  9. #define BAR 0
  10. #define CDEV_NAME "lkmc_pci"
  11. #define EDU_DEVICE_ID 0x11e8
  12. #define QEMU_VENDOR_ID 0x1234
  13. /* Registers. */
  14. #define IO_IRQ_STATUS 0x24
  15. #define IO_IRQ_ACK 0x64
  16. #define IO_DMA_SRC 0x80
  17. #define IO_DMA_DST 0x88
  18. #define IO_DMA_CNT 0x90
  19. #define IO_DMA_CMD 0x98
  20. /* Constants. */
  21. /* TODO what is this magic value for? Can't it be always deduced from the direction? */
  22. #define DMA_BASE 0x40000
  23. /* Must give this for the DMA command to to anything. */
  24. #define DMA_CMD 0x1
  25. /* If given, device -> RAM. Otherwise: RAM -> dev. */
  26. #define DMA_FROM_DEV 0x2
  27. /* If given, raise an IRQ, and write 100 to the IRQ status register. */
  28. #define DMA_IRQ 0x4
  29. static struct pci_device_id pci_ids[] = {
  30. { PCI_DEVICE(QEMU_VENDOR_ID, EDU_DEVICE_ID), },
  31. { 0, }
  32. };
  33. MODULE_DEVICE_TABLE(pci, pci_ids);
  34. static int major;
  35. static struct pci_dev *pdev;
  36. static void __iomem *mmio;
  37. static irqreturn_t irq_handler(int irq, void *dev)
  38. {
  39. int devi;
  40. irqreturn_t ret;
  41. u32 irq_status;
  42. devi = *(int *)dev;
  43. if (devi == major) {
  44. irq_status = ioread32(mmio + IO_IRQ_STATUS);
  45. pr_info("irq_handler irq = %d dev = %d irq_status = %llx\n",
  46. irq, devi, (unsigned long long)irq_status);
  47. /* Must do this ACK, or else the interrupts just keeps firing. */
  48. iowrite32(irq_status, mmio + IO_IRQ_ACK);
  49. ret = IRQ_HANDLED;
  50. } else {
  51. ret = IRQ_NONE;
  52. }
  53. return ret;
  54. }
  55. static ssize_t read(struct file *filp, char __user *buf, size_t len, loff_t *off)
  56. {
  57. ssize_t ret;
  58. u32 kbuf;
  59. if (*off % 4 || len == 0) {
  60. ret = 0;
  61. } else {
  62. kbuf = ioread32(mmio + *off);
  63. if (copy_to_user(buf, (void *)&kbuf, 4)) {
  64. ret = -EFAULT;
  65. } else {
  66. ret = 4;
  67. (*off)++;
  68. }
  69. }
  70. return ret;
  71. }
  72. static ssize_t write(struct file *filp, const char __user *buf, size_t len, loff_t *off)
  73. {
  74. ssize_t ret;
  75. u32 kbuf;
  76. ret = len;
  77. if (!(*off % 4)) {
  78. if (copy_from_user((void *)&kbuf, buf, 4) || len != 4) {
  79. ret = -EFAULT;
  80. } else {
  81. iowrite32(kbuf, mmio + *off);
  82. }
  83. }
  84. return ret;
  85. }
  86. static loff_t llseek(struct file *filp, loff_t off, int whence)
  87. {
  88. filp->f_pos = off;
  89. return off;
  90. }
  91. /* These fops are a bit daft since read and write interfaces don't map well to IO registers.
  92. *
  93. * One ioctl per register would likely be the saner option. But we are lazy.
  94. *
  95. * We use the fact that every IO is aligned to 4 bytes. Misaligned reads means EOF. */
  96. static struct file_operations fops = {
  97. .owner = THIS_MODULE,
  98. .llseek = llseek,
  99. .read = read,
  100. .write = write,
  101. };
  102. /* https://stackoverflow.com/questions/5059501/probe-method-device-drivers/44739823#44739823
  103. *
  104. * Called just after insmod if the hardware device is connected,
  105. * not called otherwise.
  106. *
  107. * 0: all good
  108. * 1: failed
  109. */
  110. static int pci_probe(struct pci_dev *dev, const struct pci_device_id *id)
  111. {
  112. /* https://stackoverflow.com/questions/31382803/how-does-dev-family-functions-are-useful-while-debugging-kernel/44734857#44734857 */
  113. dev_info(&(dev->dev), "pci_probe\n");
  114. major = register_chrdev(0, CDEV_NAME, &fops);
  115. pdev = dev;
  116. if (pci_enable_device(dev) < 0) {
  117. dev_err(&(dev->dev), "pci_enable_device\n");
  118. goto error;
  119. }
  120. if (pci_request_region(dev, BAR, "myregion0")) {
  121. dev_err(&(dev->dev), "pci_request_region\n");
  122. goto error;
  123. }
  124. mmio = pci_iomap(dev, BAR, pci_resource_len(dev, BAR));
  125. /* IRQ setup.
  126. *
  127. * pci_read_config_byte(dev, PCI_INTERRUPT_LINE, &val);
  128. * has a different value and does not work if we insert the PCI device
  129. * after boot with device_add:
  130. * https://stackoverflow.com/questions/44740254/how-to-handle-interrupts-from-a-pci-device-that-already-have-a-non-shareable-han?noredirect=1#comment76558680_44740254
  131. */
  132. if (request_irq(dev->irq, irq_handler, IRQF_SHARED, "pci_irq_handler0", &major) < 0) {
  133. dev_err(&(dev->dev), "request_irq\n");
  134. goto error;
  135. }
  136. /* Optional sanity checks. The PCI is ready now, all of this could also be called from fops. */
  137. {
  138. unsigned i;
  139. u8 val;
  140. /* Check that we are using MEM instead of IO.
  141. *
  142. * In QEMU, the type is defiened by either:
  143. *
  144. * - PCI_BASE_ADDRESS_SPACE_IO
  145. * - PCI_BASE_ADDRESS_SPACE_MEMORY
  146. */
  147. if ((pci_resource_flags(dev, BAR) & IORESOURCE_MEM) != IORESOURCE_MEM) {
  148. dev_err(&(dev->dev), "pci_resource_flags\n");
  149. goto error;
  150. }
  151. /* 1Mb, as defined by the "1 << 20" in QEMU's memory_region_init_io. Same as pci_resource_len. */
  152. resource_size_t start = pci_resource_start(dev, BAR);
  153. resource_size_t end = pci_resource_end(dev, BAR);
  154. pr_info("length %llx\n", (unsigned long long)(end + 1 - start));
  155. /* The PCI standardized 64 bytes of the configuration space, see LDD3. */
  156. for (i = 0; i < 64u; ++i) {
  157. pci_read_config_byte(dev, i, &val);
  158. pr_info("config %x %x\n", i, val);
  159. }
  160. pr_info("dev->irq %x\n", dev->irq);
  161. /* Initial value of the IO memory. */
  162. for (i = 0; i < 0x28; i += 4) {
  163. pr_info("io %x %x\n", i, ioread32((void*)(mmio + i)));
  164. }
  165. /* DMA test.
  166. *
  167. * TODO:
  168. *
  169. * - deal with interrupts properly.
  170. * - printf / gdb in QEMU source says dma_buf is not being set correctly
  171. *
  172. * Resources:
  173. *
  174. * - http://elixir.free-electrons.com/linux/v4.12/source/Documentation/DMA-API-HOWTO.txt
  175. * - http://www.makelinux.net/ldd3/chp-15-sect-4
  176. * - https://stackoverflow.com/questions/32592734/are-there-any-dma-linux-kernel-driver-example-with-pcie-for-fpga/44716747#44716747
  177. * - https://stackoverflow.com/questions/17913679/how-to-instantiate-and-use-a-dma-driver-linux-module
  178. * - https://stackoverflow.com/questions/5539375/linux-kernel-device-driver-to-dma-from-a-device-into-user-space-memory
  179. * - RPI userland /dev/mem https://github.com/Wallacoloo/Raspberry-Pi-DMA-Example
  180. * - https://stackoverflow.com/questions/34188369/easiest-way-to-use-dma-in-linux
  181. */
  182. {
  183. dma_addr_t dma_handle_from, dma_handle_to;
  184. void *vaddr_from, *vaddr_to;
  185. enum { SIZE = 4 };
  186. /* RAM -> device. */
  187. vaddr_from = dma_alloc_coherent(&(dev->dev), 4, &dma_handle_from, GFP_ATOMIC);
  188. dev_info(&(dev->dev), "vaddr_from = %p\n", vaddr_from);
  189. dev_info(&(dev->dev), "dma_handle_from = %llx\n", (unsigned long long)dma_handle_from);
  190. *((volatile u32*)vaddr_from) = 0x12345678;
  191. iowrite32((u32)dma_handle_from, mmio + IO_DMA_SRC);
  192. iowrite32(DMA_BASE, mmio + IO_DMA_DST);
  193. iowrite32(SIZE, mmio + IO_DMA_CNT);
  194. iowrite32(DMA_CMD | DMA_IRQ, mmio + IO_DMA_CMD);
  195. /* device -> RAM. */
  196. vaddr_to = dma_alloc_coherent(&(dev->dev), 4, &dma_handle_to, GFP_ATOMIC);
  197. dev_info(&(dev->dev), "vaddr_to = %p\n", vaddr_to);
  198. dev_info(&(dev->dev), "dma_handle_to = %llx\n", (unsigned long long)dma_handle_to);
  199. /*
  200. iowrite32(DMA_BASE, mmio + IO_DMA_SRC);
  201. iowrite32((u32)dma_handle_to, mmio + IO_DMA_DST);
  202. iowrite32(SIZE, mmio + IO_DMA_CNT);
  203. iowrite32(DMA_CMD | DMA_FROM_DEV | DMA_IRQ, mmio + IO_DMA_CMD);
  204. dev_info(&(dev->dev), "*vaddr_to = %llx\n", (unsigned long long)(*((u32*)vaddr_to)));
  205. */
  206. /*dma_free_coherent(&(dev->dev), SIZE, vaddr_from, dma_handle_from);*/
  207. /*dma_free_coherent(&(dev->dev), SIZE, vaddr_to, dma_handle_to);*/
  208. }
  209. }
  210. return 0;
  211. error:
  212. return 1;
  213. }
  214. static void pci_remove(struct pci_dev *dev)
  215. {
  216. pr_info("pci_remove\n");
  217. free_irq(pdev->irq, &major);
  218. pci_release_region(dev, BAR);
  219. unregister_chrdev(major, CDEV_NAME);
  220. }
  221. static struct pci_driver pci_driver = {
  222. .name = "lkmc_pci",
  223. .id_table = pci_ids,
  224. .probe = pci_probe,
  225. .remove = pci_remove,
  226. };
  227. static int myinit(void)
  228. {
  229. if (pci_register_driver(&pci_driver) < 0) {
  230. return 1;
  231. }
  232. return 0;
  233. }
  234. static void myexit(void)
  235. {
  236. pci_unregister_driver(&pci_driver);
  237. }
  238. module_init(myinit);
  239. module_exit(myexit);
  240. MODULE_LICENSE("GPL");