qemu_edu.c 7.7 KB

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