base.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676
  1. /**
  2. *
  3. * @file base.c
  4. * @author Guillermo Marcus
  5. * @date 2009-04-05
  6. * @brief Contains the main code which connects all the different parts and does
  7. * basic driver tasks like initialization.
  8. *
  9. * This is a full rewrite of the pciDriver.
  10. * New default is to support kernel 2.6, using kernel 2.6 APIs.
  11. *
  12. */
  13. /*
  14. * Change History:
  15. *
  16. * $Log: not supported by cvs2svn $
  17. * Revision 1.13 2008-05-30 11:38:15 marcus
  18. * Added patches for kernel 2.6.24
  19. *
  20. * Revision 1.12 2008-01-24 14:21:36 marcus
  21. * Added a CLEAR_INTERRUPT_QUEUE ioctl.
  22. * Added a sysfs attribute to show the outstanding IRQ queues.
  23. *
  24. * Revision 1.11 2008-01-24 12:53:11 marcus
  25. * Corrected wait_event condition in waiti_ioctl. Improved the loop too.
  26. *
  27. * Revision 1.10 2008-01-14 10:39:39 marcus
  28. * Set some messages as debug instead of normal.
  29. *
  30. * Revision 1.9 2008-01-11 10:18:28 marcus
  31. * Modified interrupt mechanism. Added atomic functions and queues, to address race conditions. Removed unused interrupt code.
  32. *
  33. * Revision 1.8 2007-07-17 13:15:55 marcus
  34. * Removed Tasklets.
  35. * Using newest map for the ABB interrupts.
  36. *
  37. * Revision 1.7 2007-07-06 15:56:04 marcus
  38. * Change default status for OLD_REGISTERS to not defined.
  39. *
  40. * Revision 1.6 2007-07-05 15:29:59 marcus
  41. * Corrected issue with the bar mapping for interrupt handling.
  42. * Added support up to kernel 2.6.20
  43. *
  44. * Revision 1.5 2007-05-29 07:50:18 marcus
  45. * Split code into 2 files. May get merged in the future again....
  46. *
  47. * Revision 1.4 2007/03/01 17:47:34 marcus
  48. * Fixed bug when the kernel memory was less than one page, it was not locked properly, recalling an old mapping issue in this case.
  49. *
  50. * Revision 1.3 2007/03/01 17:01:22 marcus
  51. * comment fix (again).
  52. *
  53. * Revision 1.2 2007/03/01 17:00:25 marcus
  54. * Changed some comment in the log.
  55. *
  56. * Revision 1.1 2007/03/01 16:57:43 marcus
  57. * Divided driver file to ease the interrupt hooks for the user of the driver.
  58. * Modified Makefile accordingly.
  59. *
  60. * From pciDriver.c:
  61. * Revision 1.11 2006/12/11 16:15:43 marcus
  62. * Fixed kernel buffer mmapping, and driver crash when application crashes.
  63. * Buffer memory is now marked reserved during allocation, and mmaped with
  64. * remap_xx_range.
  65. *
  66. * Revision 1.10 2006/11/21 09:50:49 marcus
  67. * Added PROGRAPE4 vendor/device IDs.
  68. *
  69. * Revision 1.9 2006/11/17 18:47:36 marcus
  70. * Removed MERGE_SGENTRIES flag, now it is selected at runtime with 'type'.
  71. * Removed noncached in non-prefetchable areas, to allow the use of MTRRs.
  72. *
  73. * Revision 1.8 2006/11/17 16:41:21 marcus
  74. * Added slot number to the PCI info IOctl.
  75. *
  76. * Revision 1.7 2006/11/13 12:30:34 marcus
  77. * Added a IOctl call, to confiure the interrupt response. (testing pending).
  78. * Basic interrupts are now supported, using a Tasklet and Completions.
  79. *
  80. * Revision 1.6 2006/11/08 21:30:02 marcus
  81. * Added changes after compile tests in kernel 2.6.16
  82. *
  83. * Revision 1.5 2006/10/31 07:57:38 marcus
  84. * Improved the pfn calculation in nopage(), to deal with some possible border
  85. * conditions. It was really no issue, because they are normally page-aligned
  86. * anyway, but to be on the safe side.
  87. *
  88. * Revision 1.4 2006/10/30 19:37:40 marcus
  89. * Solved bug on kernel memory not mapping properly.
  90. *
  91. * Revision 1.3 2006/10/18 11:19:20 marcus
  92. * Added kernel 2.6.8 support based on comments from Joern Adamczewski (GSI).
  93. *
  94. * Revision 1.2 2006/10/18 11:04:15 marcus
  95. * Bus Master is only activated when we detect a specific board.
  96. *
  97. * Revision 1.1 2006/10/10 14:46:51 marcus
  98. * Initial commit of the new pciDriver for kernel 2.6
  99. *
  100. * Revision 1.9 2006/10/05 11:30:46 marcus
  101. * Prerelease. Added bus and devfn to pciInfo for compatibility.
  102. *
  103. * Revision 1.8 2006/09/25 16:51:07 marcus
  104. * Added PCI config IOctls, and implemented basic mmap functions.
  105. *
  106. * Revision 1.7 2006/09/20 11:12:41 marcus
  107. * Added Merge SG entries
  108. *
  109. * Revision 1.6 2006/09/19 17:22:18 marcus
  110. * backup commit.
  111. *
  112. * Revision 1.5 2006/09/18 17:13:11 marcus
  113. * backup commit.
  114. *
  115. * Revision 1.4 2006/09/15 15:44:41 marcus
  116. * backup commit.
  117. *
  118. * Revision 1.3 2006/08/15 11:40:02 marcus
  119. * backup commit.
  120. *
  121. * Revision 1.2 2006/08/12 18:28:42 marcus
  122. * Sync with the laptop
  123. *
  124. * Revision 1.1 2006/08/11 15:30:46 marcus
  125. * Sync with the laptop
  126. *
  127. */
  128. #include <linux/version.h>
  129. /* Check macros and kernel version first */
  130. #ifndef KERNEL_VERSION
  131. #error "No KERNEL_VERSION macro! Stopping."
  132. #endif
  133. #ifndef LINUX_VERSION_CODE
  134. #error "No LINUX_VERSION_CODE macro! Stopping."
  135. #endif
  136. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,8)
  137. #error "This driver has been tested only for Kernel 2.6.8 or above."
  138. #endif
  139. /* Required includes */
  140. #include <linux/string.h>
  141. #include <linux/slab.h>
  142. #include <linux/types.h>
  143. #include <linux/init.h>
  144. #include <linux/module.h>
  145. #include <linux/pci.h>
  146. #include <linux/kernel.h>
  147. #include <linux/errno.h>
  148. #include <linux/fs.h>
  149. #include <linux/cdev.h>
  150. #include <linux/sysfs.h>
  151. #include <asm/atomic.h>
  152. #include <linux/pagemap.h>
  153. #include <linux/spinlock.h>
  154. #include <linux/list.h>
  155. #include <asm/scatterlist.h>
  156. #include <linux/vmalloc.h>
  157. #include <linux/stat.h>
  158. #include <linux/interrupt.h>
  159. #include <linux/wait.h>
  160. /* Configuration for the driver (what should be compiled in, module name, etc...) */
  161. #include "config.h"
  162. /* Compatibility functions/definitions (provides functions which are not available on older kernels) */
  163. #include "compat.h"
  164. /* External interface for the driver */
  165. #include "pciDriver.h"
  166. /* Internal definitions for all parts (prototypes, data, macros) */
  167. #include "common.h"
  168. /* Internal definitions for the base part */
  169. #include "base.h"
  170. /* Internal definitions of the IRQ handling part */
  171. #include "int.h"
  172. /* Internal definitions for kernel memory */
  173. #include "kmem.h"
  174. /* Internal definitions for user space memory */
  175. #include "umem.h"
  176. #include "ioctl.h"
  177. /*************************************************************************/
  178. /* Module device table associated with this driver */
  179. MODULE_DEVICE_TABLE(pci, pcidriver_ids);
  180. /* Module init and exit points */
  181. module_init(pcidriver_init);
  182. module_exit(pcidriver_exit);
  183. /* Module info */
  184. MODULE_AUTHOR("Guillermo Marcus");
  185. MODULE_DESCRIPTION("Simple PCI Driver");
  186. MODULE_LICENSE("GPL v2");
  187. /* Module class */
  188. static struct class_compat *pcidriver_class;
  189. /**
  190. *
  191. * Called when loading the driver
  192. *
  193. */
  194. static int __init pcidriver_init(void)
  195. {
  196. int err;
  197. /* Initialize the device count */
  198. atomic_set(&pcidriver_deviceCount, 0);
  199. /* Allocate character device region dynamically */
  200. if ((err = alloc_chrdev_region(&pcidriver_devt, MINORNR, MAXDEVICES, NODENAME)) != 0) {
  201. mod_info("Couldn't allocate chrdev region. Module not loaded.\n");
  202. goto init_alloc_fail;
  203. }
  204. mod_info("Major %d allocated to nodename '%s'\n", MAJOR(pcidriver_devt), NODENAME);
  205. /* Register driver class */
  206. pcidriver_class = class_create(THIS_MODULE, NODENAME);
  207. if (IS_ERR(pcidriver_class)) {
  208. mod_info("No sysfs support. Module not loaded.\n");
  209. goto init_class_fail;
  210. }
  211. /* Register PCI driver. This function returns the number of devices on some
  212. * systems, therefore check for errors as < 0. */
  213. if ((err = pci_register_driver(&pcidriver_driver)) < 0) {
  214. mod_info("Couldn't register PCI driver. Module not loaded.\n");
  215. goto init_pcireg_fail;
  216. }
  217. mod_info("Module loaded\n");
  218. return 0;
  219. init_pcireg_fail:
  220. class_destroy(pcidriver_class);
  221. init_class_fail:
  222. unregister_chrdev_region(pcidriver_devt, MAXDEVICES);
  223. init_alloc_fail:
  224. return err;
  225. }
  226. /**
  227. *
  228. * Called when unloading the driver
  229. *
  230. */
  231. static void pcidriver_exit(void)
  232. {
  233. pci_unregister_driver(&pcidriver_driver);
  234. unregister_chrdev_region(pcidriver_devt, MAXDEVICES);
  235. if (pcidriver_class != NULL)
  236. class_destroy(pcidriver_class);
  237. mod_info("Module unloaded\n");
  238. }
  239. /*************************************************************************/
  240. /* Driver functions */
  241. /**
  242. *
  243. * This struct defines the PCI entry points.
  244. * Will be registered at module init.
  245. *
  246. */
  247. static struct pci_driver pcidriver_driver = {
  248. .name = MODNAME,
  249. .id_table = pcidriver_ids,
  250. .probe = pcidriver_probe,
  251. .remove = pcidriver_remove,
  252. };
  253. /**
  254. *
  255. * This function is called when installing the driver for a device
  256. * @param pdev Pointer to the PCI device
  257. *
  258. */
  259. static int __devinit pcidriver_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  260. {
  261. int err;
  262. int devno;
  263. pcidriver_privdata_t *privdata;
  264. int devid;
  265. /* At the moment there is no difference between these boards here, other than
  266. * printing a different message in the log.
  267. *
  268. * However, there is some difference in the interrupt handling functions.
  269. */
  270. if (id->vendor == PCIE_XILINX_VENDOR_ID) {
  271. if (id->device == PCIE_ML605_DEVICE_ID) {
  272. mod_info("Found ML605 board at %s\n", dev_name(&pdev->dev));
  273. } else if (id->device == PCIE_IPECAMERA_DEVICE_ID) {
  274. mod_info("Found IPE Camera at %s\n", dev_name(&pdev->dev));
  275. } else if (id->device == PCIE_KAPTURE_DEVICE_ID) {
  276. mod_info("Found KAPTURE board at %s\n", dev_name(&pdev->dev));
  277. } else {
  278. mod_info("Found unknown Xilinx device (%x) at %s\n", id->device, dev_name(&pdev->dev));
  279. }
  280. } else {
  281. /* It is something else */
  282. mod_info( "Found unknown board (%x:%x) at %s\n", id->vendor, id->device, dev_name(&pdev->dev));
  283. }
  284. /* Enable the device */
  285. if ((err = pci_enable_device(pdev)) != 0) {
  286. mod_info("Couldn't enable device\n");
  287. goto probe_pcien_fail;
  288. }
  289. /* Bus master & dma */
  290. if ((id->vendor == PCIE_XILINX_VENDOR_ID)&&(id->device == PCIE_IPECAMERA_DEVICE_ID)) {
  291. pci_set_master(pdev);
  292. err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  293. if (err < 0) {
  294. printk(KERN_ERR "pci_set_dma_mask failed\n");
  295. goto probe_dma_fail;
  296. }
  297. }
  298. /* Set Memory-Write-Invalidate support */
  299. if ((err = pci_set_mwi(pdev)) != 0)
  300. mod_info("MWI not supported. Continue without enabling MWI.\n");
  301. /* Get / Increment the device id */
  302. devid = atomic_inc_return(&pcidriver_deviceCount) - 1;
  303. if (devid >= MAXDEVICES) {
  304. mod_info("Maximum number of devices reached! Increase MAXDEVICES.\n");
  305. err = -ENOMSG;
  306. goto probe_maxdevices_fail;
  307. }
  308. /* Allocate and initialize the private data for this device */
  309. if ((privdata = kcalloc(1, sizeof(*privdata), GFP_KERNEL)) == NULL) {
  310. err = -ENOMEM;
  311. goto probe_nomem;
  312. }
  313. INIT_LIST_HEAD(&(privdata->kmem_list));
  314. spin_lock_init(&(privdata->kmemlist_lock));
  315. atomic_set(&privdata->kmem_count, 0);
  316. INIT_LIST_HEAD(&(privdata->umem_list));
  317. spin_lock_init(&(privdata->umemlist_lock));
  318. atomic_set(&privdata->umem_count, 0);
  319. pci_set_drvdata( pdev, privdata );
  320. privdata->pdev = pdev;
  321. /* Device add to sysfs */
  322. devno = MKDEV(MAJOR(pcidriver_devt), MINOR(pcidriver_devt) + devid);
  323. privdata->devno = devno;
  324. if (pcidriver_class != NULL) {
  325. /* FIXME: some error checking missing here */
  326. privdata->class_dev = class_device_create(pcidriver_class, NULL, devno, &(pdev->dev), NODENAMEFMT, MINOR(pcidriver_devt) + devid, privdata);
  327. class_set_devdata( privdata->class_dev, privdata );
  328. mod_info("Device /dev/%s%d added\n",NODENAME,MINOR(pcidriver_devt) + devid);
  329. }
  330. /* Setup mmaped BARs into kernel space */
  331. if ((err = pcidriver_probe_irq(privdata)) != 0)
  332. goto probe_irq_probe_fail;
  333. /* Populate sysfs attributes for the class device */
  334. /* TODO: correct errorhandling. ewww. must remove the files in reversed order :-( */
  335. #define sysfs_attr(name) do { \
  336. if (class_device_create_file(sysfs_attr_def_pointer, &sysfs_attr_def_name(name)) != 0) \
  337. goto probe_device_create_fail; \
  338. } while (0)
  339. #ifdef ENABLE_IRQ
  340. sysfs_attr(irq_count);
  341. sysfs_attr(irq_queues);
  342. #endif
  343. sysfs_attr(mmap_mode);
  344. sysfs_attr(mmap_area);
  345. sysfs_attr(kmem_count);
  346. sysfs_attr(kmem_alloc);
  347. sysfs_attr(kmem_free);
  348. sysfs_attr(kbuffers);
  349. sysfs_attr(umappings);
  350. sysfs_attr(umem_unmap);
  351. #undef sysfs_attr
  352. /* Register character device */
  353. cdev_init( &(privdata->cdev), &pcidriver_fops );
  354. privdata->cdev.owner = THIS_MODULE;
  355. privdata->cdev.ops = &pcidriver_fops;
  356. err = cdev_add( &privdata->cdev, devno, 1 );
  357. if (err) {
  358. mod_info( "Couldn't add character device.\n" );
  359. goto probe_cdevadd_fail;
  360. }
  361. return 0;
  362. probe_device_create_fail:
  363. probe_cdevadd_fail:
  364. probe_irq_probe_fail:
  365. pcidriver_irq_unmap_bars(privdata);
  366. kfree(privdata);
  367. probe_nomem:
  368. atomic_dec(&pcidriver_deviceCount);
  369. probe_maxdevices_fail:
  370. probe_dma_fail:
  371. pci_disable_device(pdev);
  372. probe_pcien_fail:
  373. return err;
  374. }
  375. /**
  376. *
  377. * This function is called when disconnecting a device
  378. *
  379. */
  380. static void __devexit pcidriver_remove(struct pci_dev *pdev)
  381. {
  382. pcidriver_privdata_t *privdata;
  383. /* Get private data from the device */
  384. privdata = pci_get_drvdata(pdev);
  385. /* Removing sysfs attributes from class device */
  386. #define sysfs_attr(name) do { \
  387. class_device_remove_file(sysfs_attr_def_pointer, &sysfs_attr_def_name(name)); \
  388. } while (0)
  389. #ifdef ENABLE_IRQ
  390. sysfs_attr(irq_count);
  391. sysfs_attr(irq_queues);
  392. #endif
  393. sysfs_attr(mmap_mode);
  394. sysfs_attr(mmap_area);
  395. sysfs_attr(kmem_count);
  396. sysfs_attr(kmem_alloc);
  397. sysfs_attr(kmem_free);
  398. sysfs_attr(kbuffers);
  399. sysfs_attr(umappings);
  400. sysfs_attr(umem_unmap);
  401. #undef sysfs_attr
  402. /* Free all allocated kmem buffers before leaving */
  403. pcidriver_kmem_free_all( privdata );
  404. #ifdef ENABLE_IRQ
  405. pcidriver_remove_irq(privdata);
  406. #endif
  407. /* Removing Character device */
  408. cdev_del(&(privdata->cdev));
  409. /* Removing the device from sysfs */
  410. class_device_destroy(pcidriver_class, privdata->devno);
  411. /* Releasing privdata */
  412. kfree(privdata);
  413. /* Disabling PCI device */
  414. pci_disable_device(pdev);
  415. mod_info("Device at %s removed\n", dev_name(&pdev->dev));
  416. }
  417. /*************************************************************************/
  418. /* File operations */
  419. /*************************************************************************/
  420. /**
  421. * This struct defines the file operation entry points.
  422. *
  423. * @see pcidriver_ioctl
  424. * @see pcidriver_mmap
  425. * @see pcidriver_open
  426. * @see pcidriver_release
  427. *
  428. */
  429. static struct file_operations pcidriver_fops = {
  430. .owner = THIS_MODULE,
  431. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,11)
  432. .ioctl = pcidriver_ioctl,
  433. #else
  434. .unlocked_ioctl = pcidriver_ioctl,
  435. #endif
  436. .mmap = pcidriver_mmap,
  437. .open = pcidriver_open,
  438. .release = pcidriver_release,
  439. };
  440. void pcidriver_module_get(pcidriver_privdata_t *privdata) {
  441. atomic_inc(&(privdata->refs));
  442. // mod_info("Ref: %i\n", atomic_read(&(privdata->refs)));
  443. }
  444. void pcidriver_module_put(pcidriver_privdata_t *privdata) {
  445. if (atomic_add_negative(-1, &(privdata->refs))) {
  446. atomic_inc(&(privdata->refs));
  447. mod_info("Reference counting error...");
  448. } else {
  449. // mod_info("Unref: %i\n", atomic_read(&(privdata->refs)));
  450. }
  451. }
  452. /**
  453. *
  454. * Called when an application open()s a /dev/fpga*, attaches the private data
  455. * with the file pointer.
  456. *
  457. */
  458. int pcidriver_open(struct inode *inode, struct file *filp)
  459. {
  460. pcidriver_privdata_t *privdata;
  461. /* Set the private data area for the file */
  462. privdata = container_of( inode->i_cdev, pcidriver_privdata_t, cdev);
  463. filp->private_data = privdata;
  464. pcidriver_module_get(privdata);
  465. return 0;
  466. }
  467. /**
  468. *
  469. * Called when the application close()s the file descriptor. Does nothing at
  470. * the moment.
  471. *
  472. */
  473. int pcidriver_release(struct inode *inode, struct file *filp)
  474. {
  475. pcidriver_privdata_t *privdata;
  476. /* Get the private data area */
  477. privdata = filp->private_data;
  478. pcidriver_module_put(privdata);
  479. return 0;
  480. }
  481. /**
  482. *
  483. * This function is the entry point for mmap() and calls either pcidriver_mmap_pci
  484. * or pcidriver_mmap_kmem
  485. *
  486. * @see pcidriver_mmap_pci
  487. * @see pcidriver_mmap_kmem
  488. *
  489. */
  490. int pcidriver_mmap(struct file *filp, struct vm_area_struct *vma)
  491. {
  492. pcidriver_privdata_t *privdata;
  493. int ret = 0, bar;
  494. mod_info_dbg("Entering mmap\n");
  495. /* Get the private data area */
  496. privdata = filp->private_data;
  497. /* Check the current mmap mode */
  498. switch (privdata->mmap_mode) {
  499. case PCIDRIVER_MMAP_PCI:
  500. /* Mmap a PCI region */
  501. switch (privdata->mmap_area) {
  502. case PCIDRIVER_BAR0: bar = 0; break;
  503. case PCIDRIVER_BAR1: bar = 1; break;
  504. case PCIDRIVER_BAR2: bar = 2; break;
  505. case PCIDRIVER_BAR3: bar = 3; break;
  506. case PCIDRIVER_BAR4: bar = 4; break;
  507. case PCIDRIVER_BAR5: bar = 5; break;
  508. default:
  509. mod_info("Attempted to mmap a PCI area with the wrong mmap_area value: %d\n",privdata->mmap_area);
  510. return -EINVAL; /* invalid parameter */
  511. break;
  512. }
  513. ret = pcidriver_mmap_pci(privdata, vma, bar);
  514. break;
  515. case PCIDRIVER_MMAP_KMEM:
  516. /* mmap a Kernel buffer */
  517. ret = pcidriver_mmap_kmem(privdata, vma);
  518. break;
  519. default:
  520. mod_info( "Invalid mmap_mode value (%d)\n",privdata->mmap_mode );
  521. return -EINVAL; /* Invalid parameter (mode) */
  522. }
  523. return ret;
  524. }
  525. /*************************************************************************/
  526. /* Internal driver functions */
  527. int pcidriver_mmap_pci(pcidriver_privdata_t *privdata, struct vm_area_struct *vmap, int bar)
  528. {
  529. int ret = 0;
  530. unsigned long bar_addr;
  531. unsigned long bar_length, vma_size;
  532. unsigned long bar_flags;
  533. mod_info_dbg("Entering mmap_pci\n");
  534. /* Get info of the BAR to be mapped */
  535. bar_addr = pci_resource_start(privdata->pdev, bar);
  536. bar_length = pci_resource_len(privdata->pdev, bar);
  537. bar_flags = pci_resource_flags(privdata->pdev, bar);
  538. /* Check sizes */
  539. vma_size = (vmap->vm_end - vmap->vm_start);
  540. if ((vma_size != bar_length) &&
  541. ((bar_length < PAGE_SIZE) && (vma_size != PAGE_SIZE))) {
  542. mod_info( "mmap size is not correct! bar: %lu - vma: %lu\n", bar_length, vma_size );
  543. return -EINVAL;
  544. }
  545. if (bar_flags & IORESOURCE_IO) {
  546. /* Unlikely case, we will mmap a IO region */
  547. /* IO regions are never cacheable */
  548. #ifdef pgprot_noncached
  549. vmap->vm_page_prot = pgprot_noncached(vmap->vm_page_prot);
  550. #endif
  551. /* Map the BAR */
  552. ret = io_remap_pfn_range_compat(
  553. vmap,
  554. vmap->vm_start,
  555. bar_addr,
  556. bar_length,
  557. vmap->vm_page_prot);
  558. } else {
  559. /* Normal case, mmap a memory region */
  560. /* Ensure this VMA is non-cached, if it is not flaged as prefetchable.
  561. * If it is prefetchable, caching is allowed and will give better performance.
  562. * This should be set properly by the BIOS, but we want to be sure. */
  563. /* adapted from drivers/char/mem.c, mmap function. */
  564. #ifdef pgprot_noncached
  565. /* Setting noncached disables MTRR registers, and we want to use them.
  566. * So we take this code out. This can lead to caching problems if and only if
  567. * the System BIOS set something wrong. Check LDDv3, page 425.
  568. */
  569. // if (!(bar_flags & IORESOURCE_PREFETCH))
  570. // vmap->vm_page_prot = pgprot_noncached(vmap->vm_page_prot);
  571. #endif
  572. /* Map the BAR */
  573. ret = remap_pfn_range_compat(
  574. vmap,
  575. vmap->vm_start,
  576. bar_addr,
  577. bar_length,
  578. vmap->vm_page_prot);
  579. }
  580. if (ret) {
  581. mod_info("remap_pfn_range failed\n");
  582. return -EAGAIN;
  583. }
  584. return 0; /* success */
  585. }