pci_endpoint.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2022-08-25 GuEe-GUI first version
  9. */
  10. #ifndef __PCI_ENDPOINT_H__
  11. #define __PCI_ENDPOINT_H__
  12. #include <drivers/pci.h>
  13. enum rt_pci_ep_pin
  14. {
  15. RT_PCI_EP_PIN_UNKNOWN,
  16. RT_PCI_EP_PIN_INTA,
  17. RT_PCI_EP_PIN_INTB,
  18. RT_PCI_EP_PIN_INTC,
  19. RT_PCI_EP_PIN_INTD,
  20. };
  21. enum rt_pci_ep_irq
  22. {
  23. RT_PCI_EP_IRQ_UNKNOWN,
  24. RT_PCI_EP_IRQ_LEGACY,
  25. RT_PCI_EP_IRQ_MSI,
  26. RT_PCI_EP_IRQ_MSIX,
  27. };
  28. struct rt_pci_ep_header
  29. {
  30. rt_uint16_t vendor;
  31. rt_uint16_t device;
  32. rt_uint8_t revision;
  33. rt_uint8_t progif;
  34. rt_uint8_t subclass;
  35. rt_uint8_t class_code;
  36. rt_uint8_t cache_line_size;
  37. rt_uint16_t subsystem_vendor;
  38. rt_uint16_t subsystem_device;
  39. enum rt_pci_ep_pin intx;
  40. };
  41. struct rt_pci_ep_bar
  42. {
  43. /* To PCI Bus */
  44. struct rt_pci_bus_resource bus;
  45. /* To CPU */
  46. rt_ubase_t cpu_addr;
  47. };
  48. /*
  49. * Type of MSI-X table, For more format detail,
  50. * please read `components/drivers/include/drivers/pci_msi.h`
  51. */
  52. struct rt_pci_ep_msix_tbl
  53. {
  54. union
  55. {
  56. rt_uint64_t msg_addr;
  57. struct
  58. {
  59. rt_uint32_t msg_addr_upper;
  60. rt_uint32_t msg_addr_lower;
  61. };
  62. };
  63. rt_uint32_t msg_data;
  64. rt_uint32_t vector_ctrl;
  65. };
  66. struct rt_pci_ep_ops;
  67. struct rt_pci_ep
  68. {
  69. rt_list_t list;
  70. const char *name;
  71. struct rt_ref ref;
  72. const struct rt_device *rc_dev;
  73. const struct rt_pci_ep_ops *ops;
  74. rt_uint8_t max_functions;
  75. RT_BITMAP_DECLARE(functions_map, 8);
  76. rt_list_t epf_nodes;
  77. struct rt_mutex lock;
  78. void *priv;
  79. };
  80. struct rt_pci_epf
  81. {
  82. rt_list_t list;
  83. const char *name;
  84. struct rt_pci_ep_header *header;
  85. struct rt_pci_ep_bar bar[PCI_STD_NUM_BARS];
  86. rt_uint8_t msi_interrupts;
  87. rt_uint16_t msix_interrupts;
  88. rt_uint8_t func_no;
  89. struct rt_pci_ep *ep;
  90. };
  91. struct rt_pci_ep_ops
  92. {
  93. rt_err_t (*write_header)(struct rt_pci_ep *ep, rt_uint8_t func_no,
  94. struct rt_pci_ep_header *hdr);
  95. rt_err_t (*set_bar)(struct rt_pci_ep *ep, rt_uint8_t func_no,
  96. struct rt_pci_ep_bar *bar, int bar_idx);
  97. rt_err_t (*clear_bar)(struct rt_pci_ep *ep, rt_uint8_t func_no,
  98. struct rt_pci_ep_bar *bar, int bar_idx);
  99. rt_err_t (*map_addr)(struct rt_pci_ep *ep, rt_uint8_t func_no,
  100. rt_ubase_t addr, rt_uint64_t pci_addr, rt_size_t size);
  101. rt_err_t (*unmap_addr)(struct rt_pci_ep *ep, rt_uint8_t func_no, rt_ubase_t addr);
  102. rt_err_t (*set_msi)(struct rt_pci_ep *ep, rt_uint8_t func_no,
  103. unsigned irq_nr);
  104. rt_err_t (*get_msi)(struct rt_pci_ep *ep, rt_uint8_t func_no,
  105. unsigned *out_irq_nr);
  106. rt_err_t (*set_msix)(struct rt_pci_ep *ep, rt_uint8_t func_no,
  107. unsigned irq_nr, int bar_idx, rt_off_t offset);
  108. rt_err_t (*get_msix)(struct rt_pci_ep *ep, rt_uint8_t func_no,
  109. unsigned *out_irq_nr);
  110. rt_err_t (*raise_irq)(struct rt_pci_ep *ep, rt_uint8_t func_no,
  111. enum rt_pci_ep_irq type, unsigned irq);
  112. rt_err_t (*start)(struct rt_pci_ep *ep);
  113. rt_err_t (*stop)(struct rt_pci_ep *ep);
  114. };
  115. rt_err_t rt_pci_ep_write_header(struct rt_pci_ep *ep, rt_uint8_t func_no,
  116. struct rt_pci_ep_header *hdr);
  117. rt_err_t rt_pci_ep_set_bar(struct rt_pci_ep *ep, rt_uint8_t func_no,
  118. struct rt_pci_ep_bar *bar, int bar_idx);
  119. rt_err_t rt_pci_ep_clear_bar(struct rt_pci_ep *ep, rt_uint8_t func_no,
  120. struct rt_pci_ep_bar *bar, int bar_idx);
  121. rt_err_t rt_pci_ep_map_addr(struct rt_pci_ep *ep, rt_uint8_t func_no,
  122. rt_ubase_t addr, rt_uint64_t pci_addr, rt_size_t size);
  123. rt_err_t rt_pci_ep_unmap_addr(struct rt_pci_ep *ep, rt_uint8_t func_no,
  124. rt_ubase_t addr);
  125. rt_err_t rt_pci_ep_set_msi(struct rt_pci_ep *ep, rt_uint8_t func_no,
  126. unsigned irq_nr);
  127. rt_err_t rt_pci_ep_get_msi(struct rt_pci_ep *ep, rt_uint8_t func_no,
  128. unsigned *out_irq_nr);
  129. rt_err_t rt_pci_ep_set_msix(struct rt_pci_ep *ep, rt_uint8_t func_no,
  130. unsigned irq_nr, int bar_idx, rt_off_t offset);
  131. rt_err_t rt_pci_ep_get_msix(struct rt_pci_ep *ep, rt_uint8_t func_no,
  132. unsigned *out_irq_nr);
  133. rt_err_t rt_pci_ep_raise_irq(struct rt_pci_ep *ep, rt_uint8_t func_no,
  134. enum rt_pci_ep_irq type, unsigned irq);
  135. rt_err_t rt_pci_ep_start(struct rt_pci_ep *ep);
  136. rt_err_t rt_pci_ep_stop(struct rt_pci_ep *ep);
  137. rt_err_t rt_pci_ep_register(struct rt_pci_ep *ep);
  138. rt_err_t rt_pci_ep_unregister(struct rt_pci_ep *ep);
  139. rt_err_t rt_pci_ep_add_epf(struct rt_pci_ep *ep, struct rt_pci_epf *epf);
  140. rt_err_t rt_pci_ep_remove_epf(struct rt_pci_ep *ep, struct rt_pci_epf *epf);
  141. struct rt_pci_ep *rt_pci_ep_get(const char *name);
  142. void rt_pci_ep_put(struct rt_pci_ep *ep);
  143. #endif /* __PCI_ENDPOINT_H__ */