xen_pt.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. #ifndef XEN_PT_H
  2. #define XEN_PT_H
  3. #include "hw/xen/xen_common.h"
  4. #include "hw/pci/pci.h"
  5. #include "xen-host-pci-device.h"
  6. void xen_pt_log(const PCIDevice *d, const char *f, ...) GCC_FMT_ATTR(2, 3);
  7. #define XEN_PT_ERR(d, _f, _a...) xen_pt_log(d, "%s: Error: "_f, __func__, ##_a)
  8. #ifdef XEN_PT_LOGGING_ENABLED
  9. # define XEN_PT_LOG(d, _f, _a...) xen_pt_log(d, "%s: " _f, __func__, ##_a)
  10. # define XEN_PT_WARN(d, _f, _a...) \
  11. xen_pt_log(d, "%s: Warning: "_f, __func__, ##_a)
  12. #else
  13. # define XEN_PT_LOG(d, _f, _a...)
  14. # define XEN_PT_WARN(d, _f, _a...)
  15. #endif
  16. #ifdef XEN_PT_DEBUG_PCI_CONFIG_ACCESS
  17. # define XEN_PT_LOG_CONFIG(d, addr, val, len) \
  18. xen_pt_log(d, "%s: address=0x%04x val=0x%08x len=%d\n", \
  19. __func__, addr, val, len)
  20. #else
  21. # define XEN_PT_LOG_CONFIG(d, addr, val, len)
  22. #endif
  23. /* Helper */
  24. #define XEN_PFN(x) ((x) >> XC_PAGE_SHIFT)
  25. typedef const struct XenPTRegInfo XenPTRegInfo;
  26. typedef struct XenPTReg XenPTReg;
  27. typedef struct XenPCIPassthroughState XenPCIPassthroughState;
  28. #define TYPE_XEN_PT_DEVICE "xen-pci-passthrough"
  29. #define XEN_PT_DEVICE(obj) \
  30. OBJECT_CHECK(XenPCIPassthroughState, (obj), TYPE_XEN_PT_DEVICE)
  31. uint32_t igd_read_opregion(XenPCIPassthroughState *s);
  32. void igd_write_opregion(XenPCIPassthroughState *s, uint32_t val);
  33. /* function type for config reg */
  34. typedef int (*xen_pt_conf_reg_init)
  35. (XenPCIPassthroughState *, XenPTRegInfo *, uint32_t real_offset,
  36. uint32_t *data);
  37. typedef int (*xen_pt_conf_dword_write)
  38. (XenPCIPassthroughState *, XenPTReg *cfg_entry,
  39. uint32_t *val, uint32_t dev_value, uint32_t valid_mask);
  40. typedef int (*xen_pt_conf_word_write)
  41. (XenPCIPassthroughState *, XenPTReg *cfg_entry,
  42. uint16_t *val, uint16_t dev_value, uint16_t valid_mask);
  43. typedef int (*xen_pt_conf_byte_write)
  44. (XenPCIPassthroughState *, XenPTReg *cfg_entry,
  45. uint8_t *val, uint8_t dev_value, uint8_t valid_mask);
  46. typedef int (*xen_pt_conf_dword_read)
  47. (XenPCIPassthroughState *, XenPTReg *cfg_entry,
  48. uint32_t *val, uint32_t valid_mask);
  49. typedef int (*xen_pt_conf_word_read)
  50. (XenPCIPassthroughState *, XenPTReg *cfg_entry,
  51. uint16_t *val, uint16_t valid_mask);
  52. typedef int (*xen_pt_conf_byte_read)
  53. (XenPCIPassthroughState *, XenPTReg *cfg_entry,
  54. uint8_t *val, uint8_t valid_mask);
  55. #define XEN_PT_BAR_ALLF 0xFFFFFFFF
  56. #define XEN_PT_BAR_UNMAPPED (-1)
  57. #define XEN_PCI_CAP_MAX 48
  58. #define XEN_PCI_INTEL_OPREGION 0xfc
  59. typedef enum {
  60. XEN_PT_GRP_TYPE_HARDWIRED = 0, /* 0 Hardwired reg group */
  61. XEN_PT_GRP_TYPE_EMU, /* emul reg group */
  62. } XenPTRegisterGroupType;
  63. typedef enum {
  64. XEN_PT_BAR_FLAG_MEM = 0, /* Memory type BAR */
  65. XEN_PT_BAR_FLAG_IO, /* I/O type BAR */
  66. XEN_PT_BAR_FLAG_UPPER, /* upper 64bit BAR */
  67. XEN_PT_BAR_FLAG_UNUSED, /* unused BAR */
  68. } XenPTBarFlag;
  69. typedef struct XenPTRegion {
  70. /* BAR flag */
  71. XenPTBarFlag bar_flag;
  72. /* Translation of the emulated address */
  73. union {
  74. uint64_t maddr;
  75. uint64_t pio_base;
  76. uint64_t u;
  77. } access;
  78. } XenPTRegion;
  79. /* XenPTRegInfo declaration
  80. * - only for emulated register (either a part or whole bit).
  81. * - for passthrough register that need special behavior (like interacting with
  82. * other component), set emu_mask to all 0 and specify r/w func properly.
  83. * - do NOT use ALL F for init_val, otherwise the tbl will not be registered.
  84. */
  85. /* emulated register information */
  86. struct XenPTRegInfo {
  87. uint32_t offset;
  88. uint32_t size;
  89. uint32_t init_val;
  90. /* reg reserved field mask (ON:reserved, OFF:defined) */
  91. uint32_t res_mask;
  92. /* reg read only field mask (ON:RO/ROS, OFF:other) */
  93. uint32_t ro_mask;
  94. /* reg read/write-1-clear field mask (ON:RW1C/RW1CS, OFF:other) */
  95. uint32_t rw1c_mask;
  96. /* reg emulate field mask (ON:emu, OFF:passthrough) */
  97. uint32_t emu_mask;
  98. xen_pt_conf_reg_init init;
  99. /* read/write function pointer
  100. * for double_word/word/byte size */
  101. union {
  102. struct {
  103. xen_pt_conf_dword_write write;
  104. xen_pt_conf_dword_read read;
  105. } dw;
  106. struct {
  107. xen_pt_conf_word_write write;
  108. xen_pt_conf_word_read read;
  109. } w;
  110. struct {
  111. xen_pt_conf_byte_write write;
  112. xen_pt_conf_byte_read read;
  113. } b;
  114. } u;
  115. };
  116. /* emulated register management */
  117. struct XenPTReg {
  118. QLIST_ENTRY(XenPTReg) entries;
  119. XenPTRegInfo *reg;
  120. union {
  121. uint8_t *byte;
  122. uint16_t *half_word;
  123. uint32_t *word;
  124. } ptr; /* pointer to dev.config. */
  125. };
  126. typedef const struct XenPTRegGroupInfo XenPTRegGroupInfo;
  127. /* emul reg group size initialize method */
  128. typedef int (*xen_pt_reg_size_init_fn)
  129. (XenPCIPassthroughState *, XenPTRegGroupInfo *,
  130. uint32_t base_offset, uint8_t *size);
  131. /* emulated register group information */
  132. struct XenPTRegGroupInfo {
  133. uint8_t grp_id;
  134. XenPTRegisterGroupType grp_type;
  135. uint8_t grp_size;
  136. xen_pt_reg_size_init_fn size_init;
  137. XenPTRegInfo *emu_regs;
  138. };
  139. /* emul register group management table */
  140. typedef struct XenPTRegGroup {
  141. QLIST_ENTRY(XenPTRegGroup) entries;
  142. XenPTRegGroupInfo *reg_grp;
  143. uint32_t base_offset;
  144. uint8_t size;
  145. QLIST_HEAD(, XenPTReg) reg_tbl_list;
  146. } XenPTRegGroup;
  147. #define XEN_PT_UNASSIGNED_PIRQ (-1)
  148. typedef struct XenPTMSI {
  149. uint16_t flags;
  150. uint32_t addr_lo; /* guest message address */
  151. uint32_t addr_hi; /* guest message upper address */
  152. uint16_t data; /* guest message data */
  153. uint32_t ctrl_offset; /* saved control offset */
  154. uint32_t mask; /* guest mask bits */
  155. int pirq; /* guest pirq corresponding */
  156. bool initialized; /* when guest MSI is initialized */
  157. bool mapped; /* when pirq is mapped */
  158. } XenPTMSI;
  159. typedef struct XenPTMSIXEntry {
  160. int pirq;
  161. uint64_t addr;
  162. uint32_t data;
  163. uint32_t latch[4];
  164. bool updated; /* indicate whether MSI ADDR or DATA is updated */
  165. } XenPTMSIXEntry;
  166. typedef struct XenPTMSIX {
  167. uint32_t ctrl_offset;
  168. bool enabled;
  169. bool maskall;
  170. int total_entries;
  171. int bar_index;
  172. uint64_t table_base;
  173. uint32_t table_offset_adjust; /* page align mmap */
  174. uint64_t mmio_base_addr;
  175. MemoryRegion mmio;
  176. void *phys_iomem_base;
  177. XenPTMSIXEntry msix_entry[0];
  178. } XenPTMSIX;
  179. struct XenPCIPassthroughState {
  180. PCIDevice dev;
  181. PCIHostDeviceAddress hostaddr;
  182. bool is_virtfn;
  183. bool permissive;
  184. bool permissive_warned;
  185. XenHostPCIDevice real_device;
  186. XenPTRegion bases[PCI_NUM_REGIONS]; /* Access regions */
  187. QLIST_HEAD(, XenPTRegGroup) reg_grps;
  188. uint32_t machine_irq;
  189. XenPTMSI *msi;
  190. XenPTMSIX *msix;
  191. MemoryRegion bar[PCI_NUM_REGIONS - 1];
  192. MemoryRegion rom;
  193. MemoryListener memory_listener;
  194. MemoryListener io_listener;
  195. bool listener_set;
  196. };
  197. void xen_pt_config_init(XenPCIPassthroughState *s, Error **errp);
  198. void xen_pt_config_delete(XenPCIPassthroughState *s);
  199. XenPTRegGroup *xen_pt_find_reg_grp(XenPCIPassthroughState *s, uint32_t address);
  200. XenPTReg *xen_pt_find_reg(XenPTRegGroup *reg_grp, uint32_t address);
  201. int xen_pt_bar_offset_to_index(uint32_t offset);
  202. static inline pcibus_t xen_pt_get_emul_size(XenPTBarFlag flag, pcibus_t r_size)
  203. {
  204. /* align resource size (memory type only) */
  205. if (flag == XEN_PT_BAR_FLAG_MEM) {
  206. return (r_size + XC_PAGE_SIZE - 1) & XC_PAGE_MASK;
  207. } else {
  208. return r_size;
  209. }
  210. }
  211. /* INTx */
  212. /* The PCI Local Bus Specification, Rev. 3.0,
  213. * Section 6.2.4 Miscellaneous Registers, pp 223
  214. * outlines 5 valid values for the interrupt pin (intx).
  215. * 0: For devices (or device functions) that don't use an interrupt in
  216. * 1: INTA#
  217. * 2: INTB#
  218. * 3: INTC#
  219. * 4: INTD#
  220. *
  221. * Xen uses the following 4 values for intx
  222. * 0: INTA#
  223. * 1: INTB#
  224. * 2: INTC#
  225. * 3: INTD#
  226. *
  227. * Observing that these list of values are not the same, xen_pt_pci_read_intx()
  228. * uses the following mapping from hw to xen values.
  229. * This seems to reflect the current usage within Xen.
  230. *
  231. * PCI hardware | Xen | Notes
  232. * ----------------+-----+----------------------------------------------------
  233. * 0 | 0 | No interrupt
  234. * 1 | 0 | INTA#
  235. * 2 | 1 | INTB#
  236. * 3 | 2 | INTC#
  237. * 4 | 3 | INTD#
  238. * any other value | 0 | This should never happen, log error message
  239. */
  240. static inline uint8_t xen_pt_pci_read_intx(XenPCIPassthroughState *s)
  241. {
  242. uint8_t v = 0;
  243. xen_host_pci_get_byte(&s->real_device, PCI_INTERRUPT_PIN, &v);
  244. return v;
  245. }
  246. static inline uint8_t xen_pt_pci_intx(XenPCIPassthroughState *s)
  247. {
  248. uint8_t r_val = xen_pt_pci_read_intx(s);
  249. XEN_PT_LOG(&s->dev, "intx=%i\n", r_val);
  250. if (r_val < 1 || r_val > 4) {
  251. XEN_PT_LOG(&s->dev, "Interrupt pin read from hardware is out of range:"
  252. " value=%i, acceptable range is 1 - 4\n", r_val);
  253. r_val = 0;
  254. } else {
  255. /* Note that if s.real_device.config_fd is closed we make 0xff. */
  256. r_val -= 1;
  257. }
  258. return r_val;
  259. }
  260. /* MSI/MSI-X */
  261. int xen_pt_msi_setup(XenPCIPassthroughState *s);
  262. int xen_pt_msi_update(XenPCIPassthroughState *d);
  263. void xen_pt_msi_disable(XenPCIPassthroughState *s);
  264. int xen_pt_msix_init(XenPCIPassthroughState *s, uint32_t base);
  265. void xen_pt_msix_delete(XenPCIPassthroughState *s);
  266. void xen_pt_msix_unmap(XenPCIPassthroughState *s);
  267. int xen_pt_msix_update(XenPCIPassthroughState *s);
  268. int xen_pt_msix_update_remap(XenPCIPassthroughState *s, int bar_index);
  269. void xen_pt_msix_disable(XenPCIPassthroughState *s);
  270. static inline bool xen_pt_has_msix_mapping(XenPCIPassthroughState *s, int bar)
  271. {
  272. return s->msix && s->msix->bar_index == bar;
  273. }
  274. extern void *pci_assign_dev_load_option_rom(PCIDevice *dev,
  275. int *size,
  276. unsigned int domain,
  277. unsigned int bus, unsigned int slot,
  278. unsigned int function);
  279. extern bool has_igd_gfx_passthru;
  280. static inline bool is_igd_vga_passthrough(XenHostPCIDevice *dev)
  281. {
  282. return (has_igd_gfx_passthru
  283. && ((dev->class_code >> 0x8) == PCI_CLASS_DISPLAY_VGA));
  284. }
  285. int xen_pt_register_vga_regions(XenHostPCIDevice *dev);
  286. int xen_pt_unregister_vga_regions(XenHostPCIDevice *dev);
  287. void xen_pt_setup_vga(XenPCIPassthroughState *s, XenHostPCIDevice *dev,
  288. Error **errp);
  289. #endif /* XEN_PT_H */