hcd-xhci.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. * USB xHCI controller emulation
  3. *
  4. * Copyright (c) 2011 Securiforest
  5. * Date: 2011-05-11 ; Author: Hector Martin <hector@marcansoft.com>
  6. * Based on usb-ohci.c, emulates Renesas NEC USB 3.0
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2 of the License, or (at your option) any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #ifndef HW_USB_HCD_XHCI_H
  22. #define HW_USB_HCD_XHCI_H
  23. #include "qom/object.h"
  24. #include "hw/usb.h"
  25. #include "sysemu/dma.h"
  26. #define TYPE_XHCI "base-xhci"
  27. #define TYPE_NEC_XHCI "nec-usb-xhci"
  28. #define TYPE_QEMU_XHCI "qemu-xhci"
  29. OBJECT_DECLARE_SIMPLE_TYPE(XHCIState, XHCI)
  30. #define MAXPORTS_2 15
  31. #define MAXPORTS_3 15
  32. #define MAXPORTS (MAXPORTS_2 + MAXPORTS_3)
  33. #define MAXSLOTS 64
  34. #define MAXINTRS 16
  35. /* Very pessimistic, let's hope it's enough for all cases */
  36. #define EV_QUEUE (((3 * 24) + 16) * MAXSLOTS)
  37. typedef struct XHCIStreamContext XHCIStreamContext;
  38. typedef struct XHCIEPContext XHCIEPContext;
  39. enum xhci_flags {
  40. XHCI_FLAG_SS_FIRST = 1,
  41. XHCI_FLAG_FORCE_PCIE_ENDCAP,
  42. XHCI_FLAG_ENABLE_STREAMS,
  43. };
  44. typedef enum TRBType {
  45. TRB_RESERVED = 0,
  46. TR_NORMAL,
  47. TR_SETUP,
  48. TR_DATA,
  49. TR_STATUS,
  50. TR_ISOCH,
  51. TR_LINK,
  52. TR_EVDATA,
  53. TR_NOOP,
  54. CR_ENABLE_SLOT,
  55. CR_DISABLE_SLOT,
  56. CR_ADDRESS_DEVICE,
  57. CR_CONFIGURE_ENDPOINT,
  58. CR_EVALUATE_CONTEXT,
  59. CR_RESET_ENDPOINT,
  60. CR_STOP_ENDPOINT,
  61. CR_SET_TR_DEQUEUE,
  62. CR_RESET_DEVICE,
  63. CR_FORCE_EVENT,
  64. CR_NEGOTIATE_BW,
  65. CR_SET_LATENCY_TOLERANCE,
  66. CR_GET_PORT_BANDWIDTH,
  67. CR_FORCE_HEADER,
  68. CR_NOOP,
  69. ER_TRANSFER = 32,
  70. ER_COMMAND_COMPLETE,
  71. ER_PORT_STATUS_CHANGE,
  72. ER_BANDWIDTH_REQUEST,
  73. ER_DOORBELL,
  74. ER_HOST_CONTROLLER,
  75. ER_DEVICE_NOTIFICATION,
  76. ER_MFINDEX_WRAP,
  77. /* vendor specific bits */
  78. CR_VENDOR_NEC_FIRMWARE_REVISION = 49,
  79. CR_VENDOR_NEC_CHALLENGE_RESPONSE = 50,
  80. } TRBType;
  81. typedef enum TRBCCode {
  82. CC_INVALID = 0,
  83. CC_SUCCESS,
  84. CC_DATA_BUFFER_ERROR,
  85. CC_BABBLE_DETECTED,
  86. CC_USB_TRANSACTION_ERROR,
  87. CC_TRB_ERROR,
  88. CC_STALL_ERROR,
  89. CC_RESOURCE_ERROR,
  90. CC_BANDWIDTH_ERROR,
  91. CC_NO_SLOTS_ERROR,
  92. CC_INVALID_STREAM_TYPE_ERROR,
  93. CC_SLOT_NOT_ENABLED_ERROR,
  94. CC_EP_NOT_ENABLED_ERROR,
  95. CC_SHORT_PACKET,
  96. CC_RING_UNDERRUN,
  97. CC_RING_OVERRUN,
  98. CC_VF_ER_FULL,
  99. CC_PARAMETER_ERROR,
  100. CC_BANDWIDTH_OVERRUN,
  101. CC_CONTEXT_STATE_ERROR,
  102. CC_NO_PING_RESPONSE_ERROR,
  103. CC_EVENT_RING_FULL_ERROR,
  104. CC_INCOMPATIBLE_DEVICE_ERROR,
  105. CC_MISSED_SERVICE_ERROR,
  106. CC_COMMAND_RING_STOPPED,
  107. CC_COMMAND_ABORTED,
  108. CC_STOPPED,
  109. CC_STOPPED_LENGTH_INVALID,
  110. CC_MAX_EXIT_LATENCY_TOO_LARGE_ERROR = 29,
  111. CC_ISOCH_BUFFER_OVERRUN = 31,
  112. CC_EVENT_LOST_ERROR,
  113. CC_UNDEFINED_ERROR,
  114. CC_INVALID_STREAM_ID_ERROR,
  115. CC_SECONDARY_BANDWIDTH_ERROR,
  116. CC_SPLIT_TRANSACTION_ERROR
  117. } TRBCCode;
  118. typedef struct XHCIRing {
  119. dma_addr_t dequeue;
  120. bool ccs;
  121. } XHCIRing;
  122. typedef struct XHCIPort {
  123. XHCIState *xhci;
  124. uint32_t portsc;
  125. uint32_t portnr;
  126. USBPort *uport;
  127. uint32_t speedmask;
  128. char name[16];
  129. MemoryRegion mem;
  130. } XHCIPort;
  131. typedef struct XHCISlot {
  132. bool enabled;
  133. bool addressed;
  134. uint16_t intr;
  135. dma_addr_t ctx;
  136. USBPort *uport;
  137. XHCIEPContext *eps[31];
  138. } XHCISlot;
  139. typedef struct XHCIEvent {
  140. TRBType type;
  141. TRBCCode ccode;
  142. uint64_t ptr;
  143. uint32_t length;
  144. uint32_t flags;
  145. uint8_t slotid;
  146. uint8_t epid;
  147. } XHCIEvent;
  148. typedef struct XHCIInterrupter {
  149. uint32_t iman;
  150. uint32_t imod;
  151. uint32_t erstsz;
  152. uint32_t erstba_low;
  153. uint32_t erstba_high;
  154. uint32_t erdp_low;
  155. uint32_t erdp_high;
  156. bool msix_used, er_pcs;
  157. dma_addr_t er_start;
  158. uint32_t er_size;
  159. unsigned int er_ep_idx;
  160. /* kept for live migration compat only */
  161. bool er_full_unused;
  162. XHCIEvent ev_buffer[EV_QUEUE];
  163. unsigned int ev_buffer_put;
  164. unsigned int ev_buffer_get;
  165. } XHCIInterrupter;
  166. typedef struct XHCIState {
  167. DeviceState parent;
  168. USBBus bus;
  169. MemoryRegion mem;
  170. MemoryRegion *dma_mr;
  171. AddressSpace *as;
  172. MemoryRegion mem_cap;
  173. MemoryRegion mem_oper;
  174. MemoryRegion mem_runtime;
  175. MemoryRegion mem_doorbell;
  176. /* properties */
  177. uint32_t numports_2;
  178. uint32_t numports_3;
  179. uint32_t numintrs;
  180. uint32_t numslots;
  181. uint32_t flags;
  182. uint32_t max_pstreams_mask;
  183. void (*intr_update)(XHCIState *s, int n, bool enable);
  184. void (*intr_raise)(XHCIState *s, int n, bool level);
  185. DeviceState *hostOpaque;
  186. /* Operational Registers */
  187. uint32_t usbcmd;
  188. uint32_t usbsts;
  189. uint32_t dnctrl;
  190. uint32_t crcr_low;
  191. uint32_t crcr_high;
  192. uint32_t dcbaap_low;
  193. uint32_t dcbaap_high;
  194. uint32_t config;
  195. USBPort uports[MAX_CONST(MAXPORTS_2, MAXPORTS_3)];
  196. XHCIPort ports[MAXPORTS];
  197. XHCISlot slots[MAXSLOTS];
  198. uint32_t numports;
  199. /* Runtime Registers */
  200. int64_t mfindex_start;
  201. QEMUTimer *mfwrap_timer;
  202. XHCIInterrupter intr[MAXINTRS];
  203. XHCIRing cmd_ring;
  204. bool nec_quirks;
  205. } XHCIState;
  206. extern const VMStateDescription vmstate_xhci;
  207. bool xhci_get_flag(XHCIState *xhci, enum xhci_flags bit);
  208. void xhci_set_flag(XHCIState *xhci, enum xhci_flags bit);
  209. #endif