hcd-xhci.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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.1 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 "hw/usb/xhci.h"
  26. #include "system/dma.h"
  27. OBJECT_DECLARE_SIMPLE_TYPE(XHCIState, XHCI)
  28. /* Very pessimistic, let's hope it's enough for all cases */
  29. #define EV_QUEUE (((3 * 24) + 16) * XHCI_MAXSLOTS)
  30. typedef struct XHCIStreamContext XHCIStreamContext;
  31. typedef struct XHCIEPContext XHCIEPContext;
  32. enum xhci_flags {
  33. XHCI_FLAG_ENABLE_STREAMS = 1,
  34. };
  35. typedef enum TRBType {
  36. TRB_RESERVED = 0,
  37. TR_NORMAL,
  38. TR_SETUP,
  39. TR_DATA,
  40. TR_STATUS,
  41. TR_ISOCH,
  42. TR_LINK,
  43. TR_EVDATA,
  44. TR_NOOP,
  45. CR_ENABLE_SLOT,
  46. CR_DISABLE_SLOT,
  47. CR_ADDRESS_DEVICE,
  48. CR_CONFIGURE_ENDPOINT,
  49. CR_EVALUATE_CONTEXT,
  50. CR_RESET_ENDPOINT,
  51. CR_STOP_ENDPOINT,
  52. CR_SET_TR_DEQUEUE,
  53. CR_RESET_DEVICE,
  54. CR_FORCE_EVENT,
  55. CR_NEGOTIATE_BW,
  56. CR_SET_LATENCY_TOLERANCE,
  57. CR_GET_PORT_BANDWIDTH,
  58. CR_FORCE_HEADER,
  59. CR_NOOP,
  60. ER_TRANSFER = 32,
  61. ER_COMMAND_COMPLETE,
  62. ER_PORT_STATUS_CHANGE,
  63. ER_BANDWIDTH_REQUEST,
  64. ER_DOORBELL,
  65. ER_HOST_CONTROLLER,
  66. ER_DEVICE_NOTIFICATION,
  67. ER_MFINDEX_WRAP,
  68. /* vendor specific bits */
  69. CR_VENDOR_NEC_FIRMWARE_REVISION = 49,
  70. CR_VENDOR_NEC_CHALLENGE_RESPONSE = 50,
  71. } TRBType;
  72. typedef enum TRBCCode {
  73. CC_INVALID = 0,
  74. CC_SUCCESS,
  75. CC_DATA_BUFFER_ERROR,
  76. CC_BABBLE_DETECTED,
  77. CC_USB_TRANSACTION_ERROR,
  78. CC_TRB_ERROR,
  79. CC_STALL_ERROR,
  80. CC_RESOURCE_ERROR,
  81. CC_BANDWIDTH_ERROR,
  82. CC_NO_SLOTS_ERROR,
  83. CC_INVALID_STREAM_TYPE_ERROR,
  84. CC_SLOT_NOT_ENABLED_ERROR,
  85. CC_EP_NOT_ENABLED_ERROR,
  86. CC_SHORT_PACKET,
  87. CC_RING_UNDERRUN,
  88. CC_RING_OVERRUN,
  89. CC_VF_ER_FULL,
  90. CC_PARAMETER_ERROR,
  91. CC_BANDWIDTH_OVERRUN,
  92. CC_CONTEXT_STATE_ERROR,
  93. CC_NO_PING_RESPONSE_ERROR,
  94. CC_EVENT_RING_FULL_ERROR,
  95. CC_INCOMPATIBLE_DEVICE_ERROR,
  96. CC_MISSED_SERVICE_ERROR,
  97. CC_COMMAND_RING_STOPPED,
  98. CC_COMMAND_ABORTED,
  99. CC_STOPPED,
  100. CC_STOPPED_LENGTH_INVALID,
  101. CC_MAX_EXIT_LATENCY_TOO_LARGE_ERROR = 29,
  102. CC_ISOCH_BUFFER_OVERRUN = 31,
  103. CC_EVENT_LOST_ERROR,
  104. CC_UNDEFINED_ERROR,
  105. CC_INVALID_STREAM_ID_ERROR,
  106. CC_SECONDARY_BANDWIDTH_ERROR,
  107. CC_SPLIT_TRANSACTION_ERROR
  108. } TRBCCode;
  109. typedef struct XHCIRing {
  110. dma_addr_t dequeue;
  111. bool ccs;
  112. } XHCIRing;
  113. typedef struct XHCIPort {
  114. XHCIState *xhci;
  115. uint32_t portsc;
  116. uint32_t portnr;
  117. USBPort *uport;
  118. uint32_t speedmask;
  119. char name[20];
  120. MemoryRegion mem;
  121. } XHCIPort;
  122. typedef struct XHCISlot {
  123. bool enabled;
  124. bool addressed;
  125. uint16_t intr;
  126. dma_addr_t ctx;
  127. USBPort *uport;
  128. XHCIEPContext *eps[31];
  129. } XHCISlot;
  130. typedef struct XHCIEvent {
  131. TRBType type;
  132. TRBCCode ccode;
  133. uint64_t ptr;
  134. uint32_t length;
  135. uint32_t flags;
  136. uint8_t slotid;
  137. uint8_t epid;
  138. } XHCIEvent;
  139. typedef struct XHCIInterrupter {
  140. uint32_t iman;
  141. uint32_t imod;
  142. uint32_t erstsz;
  143. uint32_t erstba_low;
  144. uint32_t erstba_high;
  145. uint32_t erdp_low;
  146. uint32_t erdp_high;
  147. bool msix_used, er_pcs;
  148. dma_addr_t er_start;
  149. uint32_t er_size;
  150. unsigned int er_ep_idx;
  151. /* kept for live migration compat only */
  152. bool er_full_unused;
  153. XHCIEvent ev_buffer[EV_QUEUE];
  154. unsigned int ev_buffer_put;
  155. unsigned int ev_buffer_get;
  156. } XHCIInterrupter;
  157. typedef struct XHCIState {
  158. DeviceState parent;
  159. USBBus bus;
  160. MemoryRegion mem;
  161. MemoryRegion *dma_mr;
  162. AddressSpace *as;
  163. MemoryRegion mem_cap;
  164. MemoryRegion mem_oper;
  165. MemoryRegion mem_runtime;
  166. MemoryRegion mem_doorbell;
  167. /* properties */
  168. uint32_t numports_2;
  169. uint32_t numports_3;
  170. uint32_t numintrs;
  171. uint32_t numslots;
  172. uint32_t flags;
  173. uint32_t max_pstreams_mask;
  174. void (*intr_update)(XHCIState *s, int n, bool enable);
  175. bool (*intr_raise)(XHCIState *s, int n, bool level);
  176. /*
  177. * Callback for special-casing interrupter mapping support. NULL for most
  178. * implementations, for defaulting to enabled mapping unless numintrs == 1.
  179. */
  180. bool (*intr_mapping_supported)(XHCIState *s);
  181. DeviceState *hostOpaque;
  182. /* Operational Registers */
  183. uint32_t usbcmd;
  184. uint32_t usbsts;
  185. uint32_t dnctrl;
  186. uint32_t crcr_low;
  187. uint32_t crcr_high;
  188. uint32_t dcbaap_low;
  189. uint32_t dcbaap_high;
  190. uint32_t config;
  191. USBPort uports[MAX_CONST(XHCI_MAXPORTS_2, XHCI_MAXPORTS_3)];
  192. XHCIPort ports[XHCI_MAXPORTS];
  193. XHCISlot slots[XHCI_MAXSLOTS];
  194. uint32_t numports;
  195. /* Runtime Registers */
  196. int64_t mfindex_start;
  197. QEMUTimer *mfwrap_timer;
  198. XHCIInterrupter intr[XHCI_MAXINTRS];
  199. XHCIRing cmd_ring;
  200. bool nec_quirks;
  201. } XHCIState;
  202. extern const VMStateDescription vmstate_xhci;
  203. bool xhci_get_flag(XHCIState *xhci, enum xhci_flags bit);
  204. void xhci_set_flag(XHCIState *xhci, enum xhci_flags bit);
  205. #endif