dev.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. #include <linux/string.h>
  2. #include <linux/slab.h>
  3. #include <linux/types.h>
  4. #include <linux/init.h>
  5. #include <linux/module.h>
  6. #include <linux/pci.h>
  7. #include <linux/kernel.h>
  8. #include <linux/errno.h>
  9. #include <linux/fs.h>
  10. #include <linux/cdev.h>
  11. #include <linux/sysfs.h>
  12. #include <asm/atomic.h>
  13. #include <linux/pagemap.h>
  14. #include <linux/spinlock.h>
  15. #include <linux/list.h>
  16. #include <linux/scatterlist.h>
  17. #include <linux/vmalloc.h>
  18. #include <linux/stat.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/wait.h>
  21. #include "base.h"
  22. /**
  23. *
  24. * Called when an application open()s a /dev/fpga*, attaches the private data
  25. * with the file pointer.
  26. *
  27. */
  28. static int pcidriver_open(struct inode *inode, struct file *filp)
  29. {
  30. pcidriver_privdata_t *privdata;
  31. /* Set the private data area for the file */
  32. privdata = container_of( inode->i_cdev, pcidriver_privdata_t, cdev);
  33. filp->private_data = privdata;
  34. pcidriver_module_get(privdata);
  35. return 0;
  36. }
  37. /**
  38. *
  39. * Called when the application close()s the file descriptor. Does nothing at
  40. * the moment.
  41. *
  42. */
  43. static int pcidriver_release(struct inode *inode, struct file *filp)
  44. {
  45. pcidriver_privdata_t *privdata;
  46. /* Get the private data area */
  47. privdata = filp->private_data;
  48. pcidriver_module_put(privdata);
  49. return 0;
  50. }
  51. /*************************************************************************/
  52. /* Internal driver functions */
  53. static int pcidriver_mmap_bar(pcidriver_privdata_t *privdata, struct vm_area_struct *vmap, int bar)
  54. {
  55. #ifdef PCIDRIVER_DUMMY_DEVICE
  56. return -ENXIO;
  57. #else /* PCIDRIVER_DUMMY_DEVICE */
  58. int ret = 0;
  59. unsigned long bar_addr;
  60. unsigned long bar_length, vma_size;
  61. unsigned long bar_flags;
  62. mod_info_dbg("Entering mmap_pci\n");
  63. /* Get info of the BAR to be mapped */
  64. bar_addr = pci_resource_start(privdata->pdev, bar);
  65. bar_length = pci_resource_len(privdata->pdev, bar);
  66. bar_flags = pci_resource_flags(privdata->pdev, bar);
  67. /* Check sizes */
  68. vma_size = (vmap->vm_end - vmap->vm_start);
  69. if ((vma_size != bar_length) &&
  70. ((bar_length < PAGE_SIZE) && (vma_size != PAGE_SIZE))) {
  71. mod_info( "mmap size is not correct! bar: %lu - vma: %lu\n", bar_length, vma_size );
  72. return -EINVAL;
  73. }
  74. if (bar_flags & IORESOURCE_IO) {
  75. /* Unlikely case, we will mmap a IO region */
  76. /* IO regions are never cacheable */
  77. vmap->vm_page_prot = pgprot_noncached(vmap->vm_page_prot);
  78. /* Map the BAR */
  79. ret = io_remap_pfn_range(vmap, vmap->vm_start, (bar_addr >> PAGE_SHIFT), bar_length, vmap->vm_page_prot);
  80. } else {
  81. /* Normal case, mmap a memory region */
  82. /* Ensure this VMA is non-cached, if it is not flaged as prefetchable.
  83. * If it is prefetchable, caching is allowed and will give better performance.
  84. * This should be set properly by the BIOS, but we want to be sure. */
  85. /* adapted from drivers/char/mem.c, mmap function. */
  86. /* Setting noncached disables MTRR registers, and we want to use them.
  87. * So we take this code out. This can lead to caching problems if and only if
  88. * the System BIOS set something wrong. Check LDDv3, page 425.
  89. */
  90. // if (!(bar_flags & IORESOURCE_PREFETCH))
  91. // vmap->vm_page_prot = pgprot_noncached(vmap->vm_page_prot);
  92. /* Map the BAR */
  93. ret = remap_pfn_range(vmap, vmap->vm_start, (bar_addr >> PAGE_SHIFT), bar_length, vmap->vm_page_prot);
  94. }
  95. if (ret) {
  96. mod_info("remap_pfn_range failed\n");
  97. return -EAGAIN;
  98. }
  99. return 0; /* success */
  100. #endif /* PCIDRIVER_DUMMY_DEVICE */
  101. }
  102. static int pcidriver_mmap_area(pcidriver_privdata_t *privdata, struct vm_area_struct *vmap)
  103. {
  104. int ret = 0;
  105. unsigned long vma_size;
  106. mod_info_dbg("Entering mmap_addr\n");
  107. /* Check sizes */
  108. vma_size = (vmap->vm_end - vmap->vm_start);
  109. ret = remap_pfn_range(vmap, vmap->vm_start, vmap->vm_pgoff, vma_size, vmap->vm_page_prot);
  110. if (ret) {
  111. mod_info("remap_pfn_range failed\n");
  112. return -EAGAIN;
  113. }
  114. return 0; /* success */
  115. }
  116. /**
  117. *
  118. * This function is the entry point for mmap() and calls either pcidriver_mmap_bar
  119. * or pcidriver_mmap_kmem
  120. *
  121. * @see pcidriver_mmap_bar
  122. * @see pcidriver_mmap_kmem
  123. *
  124. */
  125. static int pcidriver_mmap(struct file *filp, struct vm_area_struct *vma)
  126. {
  127. pcidriver_privdata_t *privdata;
  128. int ret = 0, bar;
  129. mod_info_dbg("Entering mmap\n");
  130. /* Get the private data area */
  131. privdata = filp->private_data;
  132. /* Check the current mmap mode */
  133. switch (privdata->mmap_mode) {
  134. case PCIDRIVER_MMAP_PCI:
  135. bar = privdata->mmap_area;
  136. if ((bar < 0)||(bar > 5)) {
  137. mod_info("Attempted to mmap a PCI area with the wrong mmap_area value: %d\n",privdata->mmap_area);
  138. return -EINVAL;
  139. }
  140. ret = pcidriver_mmap_bar(privdata, vma, bar);
  141. break;
  142. case PCIDRIVER_MMAP_AREA:
  143. ret = pcidriver_mmap_area(privdata, vma);
  144. break;
  145. case PCIDRIVER_MMAP_KMEM:
  146. ret = pcidriver_mmap_kmem(privdata, vma);
  147. break;
  148. default:
  149. mod_info( "Invalid mmap_mode value (%d)\n",privdata->mmap_mode );
  150. return -EINVAL; /* Invalid parameter (mode) */
  151. }
  152. return ret;
  153. }
  154. static struct file_operations pcidriver_fops = {
  155. .owner = THIS_MODULE,
  156. .unlocked_ioctl = pcidriver_ioctl,
  157. .mmap = pcidriver_mmap,
  158. .open = pcidriver_open,
  159. .release = pcidriver_release,
  160. };
  161. const struct file_operations *pcidriver_get_fops(void)
  162. {
  163. return &pcidriver_fops;
  164. }
  165. void pcidriver_module_get(pcidriver_privdata_t *privdata) {
  166. atomic_inc(&(privdata->refs));
  167. // mod_info("Ref: %i\n", atomic_read(&(privdata->refs)));
  168. }
  169. void pcidriver_module_put(pcidriver_privdata_t *privdata) {
  170. if (atomic_add_negative(-1, &(privdata->refs))) {
  171. atomic_inc(&(privdata->refs));
  172. mod_info("Reference counting error...");
  173. } else {
  174. // mod_info("Unref: %i\n", atomic_read(&(privdata->refs)));
  175. }
  176. }