base.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731
  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. #include "../pcilib/version.h"
  161. /* Configuration for the driver (what should be compiled in, module name, etc...) */
  162. #include "config.h"
  163. /* Compatibility functions/definitions (provides functions which are not available on older kernels) */
  164. #include "compat.h"
  165. /* External interface for the driver */
  166. #include "pciDriver.h"
  167. /* Internal definitions for all parts (prototypes, data, macros) */
  168. #include "common.h"
  169. /* Internal definitions for the base part */
  170. #include "base.h"
  171. /* Internal definitions of the IRQ handling part */
  172. #include "int.h"
  173. /* Internal definitions for kernel memory */
  174. #include "kmem.h"
  175. /* Internal definitions for user space memory */
  176. #include "umem.h"
  177. #include "ioctl.h"
  178. #include "build.h"
  179. /*************************************************************************/
  180. /* Module device table associated with this driver */
  181. MODULE_DEVICE_TABLE(pci, pcidriver_ids);
  182. /* Module init and exit points */
  183. module_init(pcidriver_init);
  184. module_exit(pcidriver_exit);
  185. /* Module info */
  186. MODULE_AUTHOR("Guillermo Marcus");
  187. MODULE_DESCRIPTION("Simple PCI Driver");
  188. MODULE_LICENSE("GPL v2");
  189. /* Module class */
  190. static struct class_compat *pcidriver_class;
  191. #ifdef PCIDRIVER_DUMMY_DEVICE
  192. pcidriver_privdata_t *pcidriver_privdata = NULL;
  193. #endif /* PCIDRIVER_DUMMY_DEVICE */
  194. /**
  195. *
  196. * Called when loading the driver
  197. *
  198. */
  199. static int __init pcidriver_init(void)
  200. {
  201. int err = 0;
  202. /* Initialize the device count */
  203. atomic_set(&pcidriver_deviceCount, 0);
  204. /* Allocate character device region dynamically */
  205. if ((err = alloc_chrdev_region(&pcidriver_devt, MINORNR, MAXDEVICES, NODENAME)) != 0) {
  206. mod_info("Couldn't allocate chrdev region. Module not loaded.\n");
  207. goto init_alloc_fail;
  208. }
  209. mod_info("Major %d allocated to nodename '%s'\n", MAJOR(pcidriver_devt), NODENAME);
  210. /* Register driver class */
  211. pcidriver_class = class_create(THIS_MODULE, NODENAME);
  212. if (IS_ERR(pcidriver_class)) {
  213. mod_info("No sysfs support. Module not loaded.\n");
  214. goto init_class_fail;
  215. }
  216. /* Register PCI driver. This function returns the number of devices on some
  217. * systems, therefore check for errors as < 0. */
  218. #ifdef PCIDRIVER_DUMMY_DEVICE
  219. if ((err = pcidriver_probe(NULL, NULL)) < 0) {
  220. #else /* PCIDRIVER_DUMMY_DEVICE */
  221. if ((err = pci_register_driver(&pcidriver_driver)) < 0) {
  222. #endif /* PCIDRIVER_DUMMY_DEVICE */
  223. mod_info("Couldn't register PCI driver. Module not loaded.\n");
  224. goto init_pcireg_fail;
  225. }
  226. mod_info("pcidriver %u.%u.%u loaded\n", PCILIB_VERSION_GET_MAJOR(PCILIB_VERSION), PCILIB_VERSION_GET_MINOR(PCILIB_VERSION), PCILIB_VERSION_GET_MICRO(PCILIB_VERSION));
  227. mod_info("%s\n", PCIDRIVER_BUILD);
  228. mod_info("%s\n", PCIDRIVER_REVISION);
  229. if (strlen(PCIDRIVER_CHANGES)) {
  230. mod_info("Extra changes - %s\n", PCIDRIVER_CHANGES);
  231. }
  232. return 0;
  233. init_pcireg_fail:
  234. class_destroy(pcidriver_class);
  235. init_class_fail:
  236. unregister_chrdev_region(pcidriver_devt, MAXDEVICES);
  237. init_alloc_fail:
  238. return err;
  239. }
  240. /**
  241. *
  242. * Called when unloading the driver
  243. *
  244. */
  245. static void pcidriver_exit(void)
  246. {
  247. #ifdef PCIDRIVER_DUMMY_DEVICE
  248. pcidriver_remove(NULL);
  249. #else
  250. pci_unregister_driver(&pcidriver_driver);
  251. #endif /* PCIDRIVER_DUMMY_DEVICE */
  252. unregister_chrdev_region(pcidriver_devt, MAXDEVICES);
  253. if (pcidriver_class != NULL)
  254. class_destroy(pcidriver_class);
  255. mod_info("Module unloaded\n");
  256. }
  257. /*************************************************************************/
  258. /* Driver functions */
  259. /**
  260. *
  261. * This struct defines the PCI entry points.
  262. * Will be registered at module init.
  263. *
  264. */
  265. #ifndef PCIDRIVER_DUMMY_DEVICE
  266. static struct pci_driver pcidriver_driver = {
  267. .name = MODNAME,
  268. .id_table = pcidriver_ids,
  269. .probe = pcidriver_probe,
  270. .remove = pcidriver_remove,
  271. };
  272. #endif /* ! PCIDRIVER_DUMMY_DEVICE */
  273. /**
  274. *
  275. * This function is called when installing the driver for a device
  276. * @param pdev Pointer to the PCI device
  277. *
  278. */
  279. static int __devinit pcidriver_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  280. {
  281. int err = 0;
  282. int devno;
  283. pcidriver_privdata_t *privdata;
  284. int devid;
  285. /* At the moment there is no difference between these boards here, other than
  286. * printing a different message in the log.
  287. *
  288. * However, there is some difference in the interrupt handling functions.
  289. */
  290. #ifdef PCIDRIVER_DUMMY_DEVICE
  291. mod_info("Emulated device\n");
  292. #else /* PCIDRIVER_DUMMY_DEVICE */
  293. if (id->vendor == PCIE_XILINX_VENDOR_ID) {
  294. if (id->device == PCIE_ML605_DEVICE_ID) {
  295. mod_info("Found ML605 board at %s\n", dev_name(&pdev->dev));
  296. } else if (id->device == PCIE_IPECAMERA_DEVICE_ID) {
  297. mod_info("Found IPE Camera at %s\n", dev_name(&pdev->dev));
  298. } else if (id->device == PCIE_KAPTURE_DEVICE_ID) {
  299. mod_info("Found KAPTURE board at %s\n", dev_name(&pdev->dev));
  300. } else {
  301. mod_info("Found unknown Xilinx device (%x) at %s\n", id->device, dev_name(&pdev->dev));
  302. }
  303. } else {
  304. /* It is something else */
  305. mod_info("Found unknown board (%x:%x) at %s\n", id->vendor, id->device, dev_name(&pdev->dev));
  306. }
  307. /* Enable the device */
  308. if ((err = pci_enable_device(pdev)) != 0) {
  309. mod_info("Couldn't enable device\n");
  310. goto probe_pcien_fail;
  311. }
  312. /* Bus master & dma */
  313. pci_set_master(pdev);
  314. err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  315. if (err < 0) {
  316. printk(KERN_ERR "pci_set_dma_mask failed\n");
  317. goto probe_dma_fail;
  318. }
  319. /* Set Memory-Write-Invalidate support */
  320. if ((err = pci_set_mwi(pdev)) != 0)
  321. mod_info("MWI not supported. Continue without enabling MWI.\n");
  322. #endif /* PCIDRIVER_DUMMY_DEVICE */
  323. /* Get / Increment the device id */
  324. devid = atomic_inc_return(&pcidriver_deviceCount) - 1;
  325. if (devid >= MAXDEVICES) {
  326. mod_info("Maximum number of devices reached! Increase MAXDEVICES.\n");
  327. err = -ENOMSG;
  328. goto probe_maxdevices_fail;
  329. }
  330. /* Allocate and initialize the private data for this device */
  331. if ((privdata = kcalloc(1, sizeof(*privdata), GFP_KERNEL)) == NULL) {
  332. err = -ENOMEM;
  333. goto probe_nomem;
  334. }
  335. INIT_LIST_HEAD(&(privdata->kmem_list));
  336. spin_lock_init(&(privdata->kmemlist_lock));
  337. atomic_set(&privdata->kmem_count, 0);
  338. INIT_LIST_HEAD(&(privdata->umem_list));
  339. spin_lock_init(&(privdata->umemlist_lock));
  340. atomic_set(&privdata->umem_count, 0);
  341. #ifdef PCIDRIVER_DUMMY_DEVICE
  342. pcidriver_privdata = privdata;
  343. #else /* PCIDRIVER_DUMMY_DEVICE */
  344. pci_set_drvdata(pdev, privdata);
  345. privdata->pdev = pdev;
  346. #endif /* PCIDRIVER_DUMMY_DEVICE */
  347. /* Device add to sysfs */
  348. devno = MKDEV(MAJOR(pcidriver_devt), MINOR(pcidriver_devt) + devid);
  349. privdata->devno = devno;
  350. /* FIXME: some error checking missing here */
  351. #ifdef PCIDRIVER_DUMMY_DEVICE
  352. privdata->class_dev = class_device_create(pcidriver_class, NULL, devno, NULL, NODENAMEFMT, MINOR(pcidriver_devt) + devid, privdata);
  353. #else /* PCIDRIVER_DUMMY_DEVICE */
  354. privdata->class_dev = class_device_create(pcidriver_class, NULL, devno, &(pdev->dev), NODENAMEFMT, MINOR(pcidriver_devt) + devid, privdata);
  355. #endif /* PCIDRIVER_DUMMY_DEVICE */
  356. class_set_devdata( privdata->class_dev, privdata );
  357. mod_info("Device /dev/%s%d added\n",NODENAME,MINOR(pcidriver_devt) + devid);
  358. #ifndef PCIDRIVER_DUMMY_DEVICE
  359. /* Setup mmaped BARs into kernel space */
  360. if ((err = pcidriver_probe_irq(privdata)) != 0)
  361. goto probe_irq_probe_fail;
  362. #endif /* ! PCIDRIVER_DUMMY_DEVICE */
  363. /* Populate sysfs attributes for the class device */
  364. /* TODO: correct errorhandling. ewww. must remove the files in reversed order :-( */
  365. #define sysfs_attr(name) do { \
  366. if (class_device_create_file(sysfs_attr_def_pointer, &sysfs_attr_def_name(name)) != 0) \
  367. goto probe_device_create_fail; \
  368. } while (0)
  369. #ifdef ENABLE_IRQ
  370. sysfs_attr(irq_count);
  371. sysfs_attr(irq_queues);
  372. #endif
  373. sysfs_attr(mmap_mode);
  374. sysfs_attr(mmap_area);
  375. sysfs_attr(kmem_count);
  376. sysfs_attr(kmem_alloc);
  377. sysfs_attr(kmem_free);
  378. sysfs_attr(kbuffers);
  379. sysfs_attr(umappings);
  380. sysfs_attr(umem_unmap);
  381. #undef sysfs_attr
  382. /* Register character device */
  383. cdev_init( &(privdata->cdev), &pcidriver_fops );
  384. privdata->cdev.owner = THIS_MODULE;
  385. privdata->cdev.ops = &pcidriver_fops;
  386. err = cdev_add( &privdata->cdev, devno, 1 );
  387. if (err) {
  388. mod_info( "Couldn't add character device.\n" );
  389. goto probe_cdevadd_fail;
  390. }
  391. return 0;
  392. probe_device_create_fail:
  393. probe_cdevadd_fail:
  394. #ifndef PCIDRIVER_DUMMY_DEVICE
  395. probe_irq_probe_fail:
  396. pcidriver_irq_unmap_bars(privdata);
  397. #endif /* ! PCIDRIVER_DUMMY_DEVICE */
  398. kfree(privdata);
  399. probe_nomem:
  400. atomic_dec(&pcidriver_deviceCount);
  401. probe_maxdevices_fail:
  402. #ifndef PCIDRIVER_DUMMY_DEVICE
  403. probe_dma_fail:
  404. pci_disable_device(pdev);
  405. probe_pcien_fail:
  406. #endif /* ! PCIDRIVER_DUMMY_DEVICE */
  407. return err;
  408. }
  409. /**
  410. *
  411. * This function is called when disconnecting a device
  412. *
  413. */
  414. static void __devexit pcidriver_remove(struct pci_dev *pdev)
  415. {
  416. pcidriver_privdata_t *privdata;
  417. #ifdef PCIDRIVER_DUMMY_DEVICE
  418. privdata = pcidriver_privdata;
  419. pcidriver_privdata = NULL;
  420. #else /* PCIDRIVER_DUMMY_DEVICE */
  421. /* Get private data from the device */
  422. privdata = pci_get_drvdata(pdev);
  423. #endif /* PCIDRIVER_DUMMY_DEVICE */
  424. /* Removing sysfs attributes from class device */
  425. #define sysfs_attr(name) do { \
  426. class_device_remove_file(sysfs_attr_def_pointer, &sysfs_attr_def_name(name)); \
  427. } while (0)
  428. #ifdef ENABLE_IRQ
  429. sysfs_attr(irq_count);
  430. sysfs_attr(irq_queues);
  431. #endif
  432. sysfs_attr(mmap_mode);
  433. sysfs_attr(mmap_area);
  434. sysfs_attr(kmem_count);
  435. sysfs_attr(kmem_alloc);
  436. sysfs_attr(kmem_free);
  437. sysfs_attr(kbuffers);
  438. sysfs_attr(umappings);
  439. sysfs_attr(umem_unmap);
  440. #undef sysfs_attr
  441. /* Free all allocated kmem buffers before leaving */
  442. pcidriver_kmem_free_all( privdata );
  443. #ifndef PCIDRIVER_DUMMY_DEVICE
  444. # ifdef ENABLE_IRQ
  445. pcidriver_remove_irq(privdata);
  446. # endif
  447. #endif /* ! PCIDRIVER_DUMMY_DEVICE */
  448. /* Removing Character device */
  449. cdev_del(&(privdata->cdev));
  450. /* Removing the device from sysfs */
  451. class_device_destroy(pcidriver_class, privdata->devno);
  452. /* Releasing privdata */
  453. kfree(privdata);
  454. #ifdef PCIDRIVER_DUMMY_DEVICE
  455. mod_info("Device at " NODENAMEFMT " removed\n", 0);
  456. #else /* PCIDRIVER_DUMMY_DEVICE */
  457. /* Disabling PCI device */
  458. pci_disable_device(pdev);
  459. mod_info("Device at %s removed\n", dev_name(&pdev->dev));
  460. #endif /* PCIDRIVER_DUMMY_DEVICE */
  461. }
  462. /*************************************************************************/
  463. /* File operations */
  464. /*************************************************************************/
  465. /**
  466. * This struct defines the file operation entry points.
  467. *
  468. * @see pcidriver_ioctl
  469. * @see pcidriver_mmap
  470. * @see pcidriver_open
  471. * @see pcidriver_release
  472. *
  473. */
  474. static struct file_operations pcidriver_fops = {
  475. .owner = THIS_MODULE,
  476. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,11)
  477. .ioctl = pcidriver_ioctl,
  478. #else
  479. .unlocked_ioctl = pcidriver_ioctl,
  480. #endif
  481. .mmap = pcidriver_mmap,
  482. .open = pcidriver_open,
  483. .release = pcidriver_release,
  484. };
  485. void pcidriver_module_get(pcidriver_privdata_t *privdata) {
  486. atomic_inc(&(privdata->refs));
  487. // mod_info("Ref: %i\n", atomic_read(&(privdata->refs)));
  488. }
  489. void pcidriver_module_put(pcidriver_privdata_t *privdata) {
  490. if (atomic_add_negative(-1, &(privdata->refs))) {
  491. atomic_inc(&(privdata->refs));
  492. mod_info("Reference counting error...");
  493. } else {
  494. // mod_info("Unref: %i\n", atomic_read(&(privdata->refs)));
  495. }
  496. }
  497. /**
  498. *
  499. * Called when an application open()s a /dev/fpga*, attaches the private data
  500. * with the file pointer.
  501. *
  502. */
  503. int pcidriver_open(struct inode *inode, struct file *filp)
  504. {
  505. pcidriver_privdata_t *privdata;
  506. /* Set the private data area for the file */
  507. privdata = container_of( inode->i_cdev, pcidriver_privdata_t, cdev);
  508. filp->private_data = privdata;
  509. pcidriver_module_get(privdata);
  510. return 0;
  511. }
  512. /**
  513. *
  514. * Called when the application close()s the file descriptor. Does nothing at
  515. * the moment.
  516. *
  517. */
  518. int pcidriver_release(struct inode *inode, struct file *filp)
  519. {
  520. pcidriver_privdata_t *privdata;
  521. /* Get the private data area */
  522. privdata = filp->private_data;
  523. pcidriver_module_put(privdata);
  524. return 0;
  525. }
  526. /**
  527. *
  528. * This function is the entry point for mmap() and calls either pcidriver_mmap_pci
  529. * or pcidriver_mmap_kmem
  530. *
  531. * @see pcidriver_mmap_pci
  532. * @see pcidriver_mmap_kmem
  533. *
  534. */
  535. int pcidriver_mmap(struct file *filp, struct vm_area_struct *vma)
  536. {
  537. pcidriver_privdata_t *privdata;
  538. int ret = 0, bar;
  539. mod_info_dbg("Entering mmap\n");
  540. /* Get the private data area */
  541. privdata = filp->private_data;
  542. /* Check the current mmap mode */
  543. switch (privdata->mmap_mode) {
  544. case PCIDRIVER_MMAP_PCI:
  545. /* Mmap a PCI region */
  546. switch (privdata->mmap_area) {
  547. case PCIDRIVER_BAR0: bar = 0; break;
  548. case PCIDRIVER_BAR1: bar = 1; break;
  549. case PCIDRIVER_BAR2: bar = 2; break;
  550. case PCIDRIVER_BAR3: bar = 3; break;
  551. case PCIDRIVER_BAR4: bar = 4; break;
  552. case PCIDRIVER_BAR5: bar = 5; break;
  553. default:
  554. mod_info("Attempted to mmap a PCI area with the wrong mmap_area value: %d\n",privdata->mmap_area);
  555. return -EINVAL; /* invalid parameter */
  556. break;
  557. }
  558. ret = pcidriver_mmap_pci(privdata, vma, bar);
  559. break;
  560. case PCIDRIVER_MMAP_KMEM:
  561. /* mmap a Kernel buffer */
  562. ret = pcidriver_mmap_kmem(privdata, vma);
  563. break;
  564. default:
  565. mod_info( "Invalid mmap_mode value (%d)\n",privdata->mmap_mode );
  566. return -EINVAL; /* Invalid parameter (mode) */
  567. }
  568. return ret;
  569. }
  570. /*************************************************************************/
  571. /* Internal driver functions */
  572. int pcidriver_mmap_pci(pcidriver_privdata_t *privdata, struct vm_area_struct *vmap, int bar)
  573. {
  574. #ifdef PCIDRIVER_DUMMY_DEVICE
  575. return -ENXIO;
  576. #else /* PCIDRIVER_DUMMY_DEVICE */
  577. int ret = 0;
  578. unsigned long bar_addr;
  579. unsigned long bar_length, vma_size;
  580. unsigned long bar_flags;
  581. mod_info_dbg("Entering mmap_pci\n");
  582. /* Get info of the BAR to be mapped */
  583. bar_addr = pci_resource_start(privdata->pdev, bar);
  584. bar_length = pci_resource_len(privdata->pdev, bar);
  585. bar_flags = pci_resource_flags(privdata->pdev, bar);
  586. /* Check sizes */
  587. vma_size = (vmap->vm_end - vmap->vm_start);
  588. if ((vma_size != bar_length) &&
  589. ((bar_length < PAGE_SIZE) && (vma_size != PAGE_SIZE))) {
  590. mod_info( "mmap size is not correct! bar: %lu - vma: %lu\n", bar_length, vma_size );
  591. return -EINVAL;
  592. }
  593. if (bar_flags & IORESOURCE_IO) {
  594. /* Unlikely case, we will mmap a IO region */
  595. /* IO regions are never cacheable */
  596. #ifdef pgprot_noncached
  597. vmap->vm_page_prot = pgprot_noncached(vmap->vm_page_prot);
  598. #endif
  599. /* Map the BAR */
  600. ret = io_remap_pfn_range_compat(
  601. vmap,
  602. vmap->vm_start,
  603. bar_addr,
  604. bar_length,
  605. vmap->vm_page_prot);
  606. } else {
  607. /* Normal case, mmap a memory region */
  608. /* Ensure this VMA is non-cached, if it is not flaged as prefetchable.
  609. * If it is prefetchable, caching is allowed and will give better performance.
  610. * This should be set properly by the BIOS, but we want to be sure. */
  611. /* adapted from drivers/char/mem.c, mmap function. */
  612. #ifdef pgprot_noncached
  613. /* Setting noncached disables MTRR registers, and we want to use them.
  614. * So we take this code out. This can lead to caching problems if and only if
  615. * the System BIOS set something wrong. Check LDDv3, page 425.
  616. */
  617. // if (!(bar_flags & IORESOURCE_PREFETCH))
  618. // vmap->vm_page_prot = pgprot_noncached(vmap->vm_page_prot);
  619. #endif
  620. /* Map the BAR */
  621. ret = remap_pfn_range_compat(
  622. vmap,
  623. vmap->vm_start,
  624. bar_addr,
  625. bar_length,
  626. vmap->vm_page_prot);
  627. }
  628. if (ret) {
  629. mod_info("remap_pfn_range failed\n");
  630. return -EAGAIN;
  631. }
  632. return 0; /* success */
  633. #endif /* PCIDRIVER_DUMMY_DEVICE */
  634. }