pci.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. /*
  2. Only teted for x86.
  3. Usage:
  4. /pci.sh
  5. The standard is non-free, obviously: https://pcisig.com/specifications
  6. but Google gives several illegal PDF hits :-)
  7. And of course: http://wiki.osdev.org/PCI
  8. Like every other hardware, we could interact with PCI on x86
  9. using only IO instructions and memory operations.
  10. But PCI is a complex communication protocol that the Linux kernel
  11. implements beautifully for us, so let's use the kernel API.
  12. This example relies on the QEMU "edu" educational device.
  13. Grep QEMU source for the device description, and keep it open at all times!
  14. - edu device source and spec in QEMU tree:
  15. - https://github.com/qemu/qemu/blob/v2.7.0/hw/misc/edu.c
  16. - https://github.com/qemu/qemu/blob/v2.7.0/docs/specs/edu.txt
  17. - http://www.zarb.org/~trem/kernel/pci/pci-driver.c inb outb runnable example (no device)
  18. - LDD3 PCI chapter
  19. - another QEMU device + module, but using a custom QEMU device:
  20. - https://github.com/levex/kernel-qemu-pci/blob/31fc9355161b87cea8946b49857447ddd34c7aa6/module/levpci.c
  21. - https://github.com/levex/kernel-qemu-pci/blob/31fc9355161b87cea8946b49857447ddd34c7aa6/qemu/hw/char/lev-pci.c
  22. - https://is.muni.cz/el/1433/podzim2016/PB173/um/65218991/ course given by the creator of the edu device.
  23. In Czech, and only describes API
  24. - http://nairobi-embedded.org/linux_pci_device_driver.html
  25. DMA:
  26. - https://stackoverflow.com/questions/32592734/are-there-any-dma-driver-example-pcie-and-fpga/44716747#44716747
  27. - https://stackoverflow.com/questions/17913679/how-to-instantiate-and-use-a-dma-driver-linux-module
  28. lspci -k shows something like:
  29. 00:04.0 Class 00ff: 1234:11e8 lkmc_pci
  30. Meaning of the first numbers:
  31. <8:bus>:<5:device>.<3:function>
  32. Often abbreviated to BDF.
  33. - bus: groups PCI slots
  34. - device: maps to one slot
  35. - function: https://stackoverflow.com/questions/19223394/what-is-the-function-number-in-pci/44735372#44735372
  36. Sometimes a fourth number is also added, e.g.:
  37. 0000:00:04.0
  38. TODO is that the domain?
  39. Class: pure magic: https://www-s.acm.illinois.edu/sigops/2007/roll_your_own/7.c.1.html
  40. TODO: does it have any side effects? Set in the edu device at:
  41. k->class_id = PCI_CLASS_OTHERS
  42. ## Play with registers from the CLI
  43. Use setpci, devmem and /sys.
  44. */
  45. #include <linux/cdev.h> /* cdev_ */
  46. #include <linux/fs.h>
  47. #include <linux/init.h>
  48. #include <linux/interrupt.h>
  49. #include <linux/kernel.h>
  50. #include <linux/module.h>
  51. #include <linux/pci.h>
  52. #include <linux/uaccess.h> /* put_user */
  53. /* https://stackoverflow.com/questions/30190050/what-is-base-address-register-bar-in-pcie/44716618#44716618
  54. *
  55. * Each PCI device has 6 BAR IOs (base address register) as per the PCI spec.
  56. *
  57. * Each BAR corresponds to an address range that can be used to communicate with the PCI.
  58. *
  59. * Eech BAR is of one of the two types:
  60. *
  61. * - IORESOURCE_IO: must be accessed with inX and outX
  62. * - IORESOURCE_MEM: must be accessed with ioreadX and iowriteX
  63. * This is the saner method apparently, and what the edu device uses.
  64. *
  65. * The length of each region is defined BY THE HARDWARE, and communicated to software
  66. * via the configuration registers.
  67. *
  68. * The Linux kernel automatically parses the 64 bytes of standardized configuration registers for us.
  69. *
  70. * QEMU devices register those regions with:
  71. *
  72. * memory_region_init_io(&edu->mmio, OBJECT(edu), &edu_mmio_ops, edu,
  73. * "edu-mmio", 1 << 20);
  74. * pci_register_bar(pdev, 0, PCI_BASE_ADDRESS_SPACE_MEMORY, &edu->mmio);
  75. **/
  76. #define BAR 0
  77. #define CDEV_NAME "lkmc_pci"
  78. #define EDU_DEVICE_ID 0x11e8
  79. #define QEMU_VENDOR_ID 0x1234
  80. /* Registers. */
  81. #define IO_IRQ_STATUS 0x24
  82. #define IO_IRQ_ACK 0x64
  83. #define IO_DMA_SRC 0x80
  84. #define IO_DMA_DST 0x88
  85. #define IO_DMA_CNT 0x90
  86. #define IO_DMA_CMD 0x98
  87. /* Constants. */
  88. /* TODO what is this magic value for? Can't it be always deduced from the direction? */
  89. #define DMA_BASE 0x40000
  90. /* Must give this for the DMA command to to anything. */
  91. #define DMA_CMD 0x1
  92. /* If given, device -> RAM. Otherwise: RAM -> dev. */
  93. #define DMA_FROM_DEV 0x2
  94. /* If given, raise an IRQ, and write 100 to the IRQ status register. */
  95. #define DMA_IRQ 0x4
  96. static struct pci_device_id pci_ids[] = {
  97. { PCI_DEVICE(QEMU_VENDOR_ID, EDU_DEVICE_ID), },
  98. { 0, }
  99. };
  100. MODULE_DEVICE_TABLE(pci, pci_ids);
  101. static int major;
  102. static struct pci_dev *pdev;
  103. static void __iomem *mmio;
  104. static irqreturn_t irq_handler(int irq, void *dev)
  105. {
  106. int devi;
  107. irqreturn_t ret;
  108. u32 irq_status;
  109. devi = *(int *)dev;
  110. if (devi == major) {
  111. irq_status = ioread32(mmio + IO_IRQ_STATUS);
  112. pr_info("irq_handler irq = %d dev = %d irq_status = %llx\n",
  113. irq, devi, (unsigned long long)irq_status);
  114. /* Must do this ACK, or else the interrupts just keeps firing. */
  115. iowrite32(irq_status, mmio + IO_IRQ_ACK);
  116. ret = IRQ_HANDLED;
  117. } else {
  118. ret = IRQ_NONE;
  119. }
  120. return ret;
  121. }
  122. static ssize_t read(struct file *filp, char __user *buf, size_t len, loff_t *off)
  123. {
  124. ssize_t ret;
  125. u32 kbuf;
  126. if (*off % 4 || len == 0) {
  127. ret = 0;
  128. } else {
  129. kbuf = ioread32(mmio + *off);
  130. if (copy_to_user(buf, (void *)&kbuf, 4)) {
  131. ret = -EFAULT;
  132. } else {
  133. ret = 4;
  134. (*off)++;
  135. }
  136. }
  137. return ret;
  138. }
  139. static ssize_t write(struct file *filp, const char __user *buf, size_t len, loff_t *off)
  140. {
  141. ssize_t ret;
  142. u32 kbuf;
  143. ret = len;
  144. if (!(*off % 4)) {
  145. if (copy_from_user((void *)&kbuf, buf, 4) || len != 4) {
  146. ret = -EFAULT;
  147. } else {
  148. iowrite32(kbuf, mmio + *off);
  149. }
  150. }
  151. return ret;
  152. }
  153. static loff_t llseek(struct file *filp, loff_t off, int whence)
  154. {
  155. filp->f_pos = off;
  156. return off;
  157. }
  158. /* These fops are a bit daft since read and write interfaces don't map well to IO registers.
  159. *
  160. * One ioctl per register would likely be the saner option. But we are lazy.
  161. *
  162. * We use the fact that every IO is aligned to 4 bytes. Misaligned reads means EOF. */
  163. static struct file_operations fops = {
  164. .owner = THIS_MODULE,
  165. .llseek = llseek,
  166. .read = read,
  167. .write = write,
  168. };
  169. /* https://stackoverflow.com/questions/5059501/probe-method-device-drivers/44739823#44739823
  170. *
  171. * Called just after insmod if the hardware device is connected,
  172. * not called otherwise.
  173. *
  174. * 0: all good
  175. * 1: failed
  176. */
  177. static int pci_probe(struct pci_dev *dev, const struct pci_device_id *id)
  178. {
  179. /* https://stackoverflow.com/questions/31382803/how-does-dev-family-functions-are-useful-while-debugging-kernel/44734857#44734857 */
  180. dev_info(&(dev->dev), "pci_probe\n");
  181. major = register_chrdev(0, CDEV_NAME, &fops);
  182. pdev = dev;
  183. if (pci_enable_device(dev) < 0) {
  184. dev_err(&(dev->dev), "pci_enable_device\n");
  185. goto error;
  186. }
  187. if (pci_request_region(dev, BAR, "myregion0")) {
  188. dev_err(&(dev->dev), "pci_request_region\n");
  189. goto error;
  190. }
  191. mmio = pci_iomap(dev, BAR, pci_resource_len(dev, BAR));
  192. /* IRQ setup.
  193. *
  194. * pci_read_config_byte(dev, PCI_INTERRUPT_LINE, &val);
  195. * has a different value and does not work if we insert the PCI device
  196. * after boot with device_add:
  197. * https://stackoverflow.com/questions/44740254/how-to-handle-interrupts-from-a-pci-device-that-already-have-a-non-shareable-han?noredirect=1#comment76558680_44740254
  198. */
  199. if (request_irq(dev->irq, irq_handler, IRQF_SHARED, "pci_irq_handler0", &major) < 0) {
  200. dev_err(&(dev->dev), "request_irq\n");
  201. goto error;
  202. }
  203. /* Optional sanity checks. The PCI is ready now, all of this could also be called from fops. */
  204. {
  205. unsigned i;
  206. u8 val;
  207. /* Check that we are using MEM instead of IO.
  208. *
  209. * In QEMU, the type is defiened by either:
  210. *
  211. * - PCI_BASE_ADDRESS_SPACE_IO
  212. * - PCI_BASE_ADDRESS_SPACE_MEMORY
  213. */
  214. if ((pci_resource_flags(dev, BAR) & IORESOURCE_MEM) != IORESOURCE_MEM) {
  215. dev_err(&(dev->dev), "pci_resource_flags\n");
  216. goto error;
  217. }
  218. /* 1Mb, as defined by the "1 << 20" in QEMU's memory_region_init_io. Same as pci_resource_len. */
  219. resource_size_t start = pci_resource_start(dev, BAR);
  220. resource_size_t end = pci_resource_end(dev, BAR);
  221. pr_info("length %llx\n", (unsigned long long)(end + 1 - start));
  222. /* The PCI standardized 64 bytes of the configuration space, see LDD3. */
  223. for (i = 0; i < 64u; ++i) {
  224. pci_read_config_byte(dev, i, &val);
  225. pr_info("config %x %x\n", i, val);
  226. }
  227. pr_info("dev->irq %x\n", dev->irq);
  228. /* Initial value of the IO memory. */
  229. for (i = 0; i < 0x28; i += 4) {
  230. pr_info("io %x %x\n", i, ioread32((void*)(mmio + i)));
  231. }
  232. /* DMA test.
  233. *
  234. * TODO:
  235. *
  236. * - deal with interrupts properly.
  237. * - printf / gdb in QEMU source says dma_buf is not being set correctly
  238. *
  239. * Resources:
  240. *
  241. * - http://elixir.free-electrons.com/linux/v4.12/source/Documentation/DMA-API-HOWTO.txt
  242. * - http://www.makelinux.net/ldd3/chp-15-sect-4
  243. * - https://stackoverflow.com/questions/32592734/are-there-any-dma-linux-kernel-driver-example-with-pcie-for-fpga/44716747#44716747
  244. * - https://stackoverflow.com/questions/17913679/how-to-instantiate-and-use-a-dma-driver-linux-module
  245. * - https://stackoverflow.com/questions/5539375/linux-kernel-device-driver-to-dma-from-a-device-into-user-space-memory
  246. * - RPI userland /dev/mem https://github.com/Wallacoloo/Raspberry-Pi-DMA-Example
  247. * - https://stackoverflow.com/questions/34188369/easiest-way-to-use-dma-in-linux
  248. */
  249. {
  250. dma_addr_t dma_handle_from, dma_handle_to;
  251. void *vaddr_from, *vaddr_to;
  252. enum { SIZE = 4 };
  253. /* RAM -> device. */
  254. vaddr_from = dma_alloc_coherent(&(dev->dev), 4, &dma_handle_from, GFP_ATOMIC);
  255. dev_info(&(dev->dev), "vaddr_from = %p\n", vaddr_from);
  256. dev_info(&(dev->dev), "dma_handle_from = %llx\n", (unsigned long long)dma_handle_from);
  257. *((volatile u32*)vaddr_from) = 0x12345678;
  258. iowrite32((u32)dma_handle_from, mmio + IO_DMA_SRC);
  259. iowrite32(DMA_BASE, mmio + IO_DMA_DST);
  260. iowrite32(SIZE, mmio + IO_DMA_CNT);
  261. iowrite32(DMA_CMD | DMA_IRQ, mmio + IO_DMA_CMD);
  262. /* device -> RAM. */
  263. vaddr_to = dma_alloc_coherent(&(dev->dev), 4, &dma_handle_to, GFP_ATOMIC);
  264. dev_info(&(dev->dev), "vaddr_to = %p\n", vaddr_to);
  265. dev_info(&(dev->dev), "dma_handle_to = %llx\n", (unsigned long long)dma_handle_to);
  266. /*
  267. iowrite32(DMA_BASE, mmio + IO_DMA_SRC);
  268. iowrite32((u32)dma_handle_to, mmio + IO_DMA_DST);
  269. iowrite32(SIZE, mmio + IO_DMA_CNT);
  270. iowrite32(DMA_CMD | DMA_FROM_DEV | DMA_IRQ, mmio + IO_DMA_CMD);
  271. dev_info(&(dev->dev), "*vaddr_to = %llx\n", (unsigned long long)(*((u32*)vaddr_to)));
  272. */
  273. /*dma_free_coherent(&(dev->dev), SIZE, vaddr_from, dma_handle_from);*/
  274. /*dma_free_coherent(&(dev->dev), SIZE, vaddr_to, dma_handle_to);*/
  275. }
  276. }
  277. return 0;
  278. error:
  279. return 1;
  280. }
  281. static void pci_remove(struct pci_dev *dev)
  282. {
  283. pr_info("pci_remove\n");
  284. free_irq(pdev->irq, &major);
  285. pci_release_region(dev, BAR);
  286. unregister_chrdev(major, CDEV_NAME);
  287. }
  288. static struct pci_driver pci_driver = {
  289. .name = "lkmc_pci",
  290. .id_table = pci_ids,
  291. .probe = pci_probe,
  292. .remove = pci_remove,
  293. };
  294. static int myinit(void)
  295. {
  296. if (pci_register_driver(&pci_driver) < 0) {
  297. return 1;
  298. }
  299. return 0;
  300. }
  301. static void myexit(void)
  302. {
  303. pci_unregister_driver(&pci_driver);
  304. }
  305. module_init(myinit);
  306. module_exit(myexit);
  307. MODULE_LICENSE("GPL");