bar.h 4.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #ifndef _PCILIB_BAR_H
  2. #define _PCILIB_BAR_H
  3. typedef struct {
  4. pcilib_bar_t bar;
  5. size_t size;
  6. uintptr_t phys_addr;
  7. void *virt_addr;
  8. } pcilib_bar_info_t;
  9. #ifdef __cplusplus
  10. extern "C" {
  11. #endif
  12. /**
  13. * Detects in which PCI BAR the specified buffer is residing. The complete specified buffer
  14. * of \a size bytes should fit into the BAR. Otherwise, an error will be returned.
  15. * @param[in] ctx - pcilib context
  16. * @param[in] addr - physical address of the begining of the buffer
  17. * @param[in] size - the size of the buffer
  18. * @return - return the BAR (numbered from 0) or PCILIB_BAR_INVALID if buffer does not belong to any BAR.
  19. */
  20. pcilib_bar_t pcilib_detect_bar(pcilib_t *ctx, uintptr_t addr, size_t size);
  21. /**
  22. * Maps the specified bar to the address space of the process. This function may be called multiple times for
  23. * the same bar. It will only map the BAR once. Normally, the required BARs will be automatically mapped when
  24. * BAR addresses are resolved with pcilib_resolve_bar_address() and similar.
  25. * @param[in,out] ctx - pcilib context
  26. * @param[in] bar - the PCI BAR number (numbered from 0)
  27. * return - the address where the BAR is mapped. You can't use this address directly,
  28. * instead pcilib_resolve_register_address(ctx, bar, 0) have to be used to find
  29. * the start of the BAR in virtual address space.
  30. */
  31. void *pcilib_map_bar(pcilib_t *ctx, pcilib_bar_t bar);
  32. /**
  33. * Unmaps the specified bar from the address space of the process. Actually, it will only unmap the BAR if it is
  34. * not used by DMA or Event egines. So, it is fine to include the calls to pcilib_map_bar() / pcilib_unmap_bar()
  35. * when a specific BAR is needed. On other hand, there is a little point in doing so. The BAR may be left mapped
  36. * and will be automatically umapped when pcilib_close() is called.
  37. * @param[in,out] ctx - pcilib context
  38. * @param[in] bar - BAR number (numbered from 0)
  39. * @param[in,out] data - The address returned by pcilib_map_bar() call
  40. */
  41. void pcilib_unmap_bar(pcilib_t *ctx, pcilib_bar_t bar, void *data);
  42. /**
  43. * Detects the BAR and mapping offset of the specified PCI buffer. The complete specified buffer
  44. * of \a size bytes should fit into the BAR. Otherwise, an error will be returned.
  45. * @param[in] ctx - pcilib context
  46. * @param[in,out] bar - the function will check if the buffer belongs to the specified BAR unless bar is PCILIB_BAR_DETECT.
  47. * It will be set to the actually detected BAR in the last case.
  48. * @param[in,out] addr - specifies the address to detect. The address may be specified as absolute physical address or offset in the BAR.
  49. * On success, the addr will contain offset which should be added to the address returned by pcilib_map_bar()
  50. * to get position of BAR mapping in virtual address space.
  51. * @param[in] size - the size of the buffer
  52. * @return - error code or 0 in case of success
  53. */
  54. int pcilib_detect_address(pcilib_t *ctx, pcilib_bar_t *bar, uintptr_t *addr, size_t size);
  55. /**
  56. * Resolves the virtual address and the size of PCI BAR space used for data exchange.
  57. * This is left-over from older version of pcilib and currently unused. We may reconsider
  58. * how it is organized and implemented. The data_space parameter may go into the model definition.
  59. * @param[in] ctx - pcilib context
  60. * @param[in] addr - may hint there the data space is located, use 0 to autodetect
  61. * @param[out] size - the size of data space is returned
  62. * @return - virtual address of data space (ready to use) or NULL if detection has failed
  63. */
  64. char *pcilib_resolve_data_space(pcilib_t *ctx, uintptr_t addr, size_t *size);
  65. /**
  66. * Return information about the specified BAR or NULL if BAR is not present in hardware
  67. * @param[in,out] ctx - pcilib context
  68. * @param[in] bar - the PCI BAR number (numbered from 0)
  69. * @return - pointer to structure describing the specified BAR or NULL in case of error
  70. */
  71. const pcilib_bar_info_t *pcilib_get_bar_info(pcilib_t *ctx, pcilib_bar_t bar);
  72. /**
  73. * Return a list of BAR memory regions available in the hardware.
  74. * The list is terminated by a dummy BAR description with 0 size.
  75. *
  76. * @param[in,out] ctx - pcilib context
  77. * @return - pointer to structure describing the BARs or NULL in case of error
  78. */
  79. const pcilib_bar_info_t *pcilib_get_bar_list(pcilib_t *ctx);
  80. #ifdef __cplusplus
  81. }
  82. #endif
  83. #endif /* _PCILIB_BAR_H */