hcd-ehci.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. /*
  2. * QEMU USB EHCI Emulation
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public License
  15. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #ifndef HW_USB_HCD_EHCI_H
  18. #define HW_USB_HCD_EHCI_H
  19. #include "qemu/timer.h"
  20. #include "hw/usb.h"
  21. #include "sysemu/dma.h"
  22. #include "hw/pci/pci.h"
  23. #include "hw/sysbus.h"
  24. #include "qom/object.h"
  25. #ifndef EHCI_DEBUG
  26. #define EHCI_DEBUG 0
  27. #endif
  28. #if EHCI_DEBUG
  29. #define DPRINTF printf
  30. #else
  31. #define DPRINTF(...)
  32. #endif
  33. #define MMIO_SIZE 0x1000
  34. #define CAPA_SIZE 0x10
  35. #define NB_PORTS 6 /* Max. Number of downstream ports */
  36. typedef struct EHCIPacket EHCIPacket;
  37. typedef struct EHCIQueue EHCIQueue;
  38. typedef struct EHCIState EHCIState;
  39. /* EHCI spec version 1.0 Section 3.3
  40. */
  41. typedef struct EHCIitd {
  42. uint32_t next;
  43. uint32_t transact[8];
  44. #define ITD_XACT_ACTIVE (1 << 31)
  45. #define ITD_XACT_DBERROR (1 << 30)
  46. #define ITD_XACT_BABBLE (1 << 29)
  47. #define ITD_XACT_XACTERR (1 << 28)
  48. #define ITD_XACT_LENGTH_MASK 0x0fff0000
  49. #define ITD_XACT_LENGTH_SH 16
  50. #define ITD_XACT_IOC (1 << 15)
  51. #define ITD_XACT_PGSEL_MASK 0x00007000
  52. #define ITD_XACT_PGSEL_SH 12
  53. #define ITD_XACT_OFFSET_MASK 0x00000fff
  54. uint32_t bufptr[7];
  55. #define ITD_BUFPTR_MASK 0xfffff000
  56. #define ITD_BUFPTR_SH 12
  57. #define ITD_BUFPTR_EP_MASK 0x00000f00
  58. #define ITD_BUFPTR_EP_SH 8
  59. #define ITD_BUFPTR_DEVADDR_MASK 0x0000007f
  60. #define ITD_BUFPTR_DEVADDR_SH 0
  61. #define ITD_BUFPTR_DIRECTION (1 << 11)
  62. #define ITD_BUFPTR_MAXPKT_MASK 0x000007ff
  63. #define ITD_BUFPTR_MAXPKT_SH 0
  64. #define ITD_BUFPTR_MULT_MASK 0x00000003
  65. #define ITD_BUFPTR_MULT_SH 0
  66. } EHCIitd;
  67. /* EHCI spec version 1.0 Section 3.4
  68. */
  69. typedef struct EHCIsitd {
  70. uint32_t next; /* Standard next link pointer */
  71. uint32_t epchar;
  72. #define SITD_EPCHAR_IO (1 << 31)
  73. #define SITD_EPCHAR_PORTNUM_MASK 0x7f000000
  74. #define SITD_EPCHAR_PORTNUM_SH 24
  75. #define SITD_EPCHAR_HUBADD_MASK 0x007f0000
  76. #define SITD_EPCHAR_HUBADDR_SH 16
  77. #define SITD_EPCHAR_EPNUM_MASK 0x00000f00
  78. #define SITD_EPCHAR_EPNUM_SH 8
  79. #define SITD_EPCHAR_DEVADDR_MASK 0x0000007f
  80. uint32_t uframe;
  81. #define SITD_UFRAME_CMASK_MASK 0x0000ff00
  82. #define SITD_UFRAME_CMASK_SH 8
  83. #define SITD_UFRAME_SMASK_MASK 0x000000ff
  84. uint32_t results;
  85. #define SITD_RESULTS_IOC (1 << 31)
  86. #define SITD_RESULTS_PGSEL (1 << 30)
  87. #define SITD_RESULTS_TBYTES_MASK 0x03ff0000
  88. #define SITD_RESULTS_TYBYTES_SH 16
  89. #define SITD_RESULTS_CPROGMASK_MASK 0x0000ff00
  90. #define SITD_RESULTS_CPROGMASK_SH 8
  91. #define SITD_RESULTS_ACTIVE (1 << 7)
  92. #define SITD_RESULTS_ERR (1 << 6)
  93. #define SITD_RESULTS_DBERR (1 << 5)
  94. #define SITD_RESULTS_BABBLE (1 << 4)
  95. #define SITD_RESULTS_XACTERR (1 << 3)
  96. #define SITD_RESULTS_MISSEDUF (1 << 2)
  97. #define SITD_RESULTS_SPLITXSTATE (1 << 1)
  98. uint32_t bufptr[2];
  99. #define SITD_BUFPTR_MASK 0xfffff000
  100. #define SITD_BUFPTR_CURROFF_MASK 0x00000fff
  101. #define SITD_BUFPTR_TPOS_MASK 0x00000018
  102. #define SITD_BUFPTR_TPOS_SH 3
  103. #define SITD_BUFPTR_TCNT_MASK 0x00000007
  104. uint32_t backptr; /* Standard next link pointer */
  105. } EHCIsitd;
  106. /* EHCI spec version 1.0 Section 3.5
  107. */
  108. typedef struct EHCIqtd {
  109. uint32_t next; /* Standard next link pointer */
  110. uint32_t altnext; /* Standard next link pointer */
  111. uint32_t token;
  112. #define QTD_TOKEN_DTOGGLE (1 << 31)
  113. #define QTD_TOKEN_TBYTES_MASK 0x7fff0000
  114. #define QTD_TOKEN_TBYTES_SH 16
  115. #define QTD_TOKEN_IOC (1 << 15)
  116. #define QTD_TOKEN_CPAGE_MASK 0x00007000
  117. #define QTD_TOKEN_CPAGE_SH 12
  118. #define QTD_TOKEN_CERR_MASK 0x00000c00
  119. #define QTD_TOKEN_CERR_SH 10
  120. #define QTD_TOKEN_PID_MASK 0x00000300
  121. #define QTD_TOKEN_PID_SH 8
  122. #define QTD_TOKEN_ACTIVE (1 << 7)
  123. #define QTD_TOKEN_HALT (1 << 6)
  124. #define QTD_TOKEN_DBERR (1 << 5)
  125. #define QTD_TOKEN_BABBLE (1 << 4)
  126. #define QTD_TOKEN_XACTERR (1 << 3)
  127. #define QTD_TOKEN_MISSEDUF (1 << 2)
  128. #define QTD_TOKEN_SPLITXSTATE (1 << 1)
  129. #define QTD_TOKEN_PING (1 << 0)
  130. uint32_t bufptr[5]; /* Standard buffer pointer */
  131. #define QTD_BUFPTR_MASK 0xfffff000
  132. #define QTD_BUFPTR_SH 12
  133. } EHCIqtd;
  134. /* EHCI spec version 1.0 Section 3.6
  135. */
  136. typedef struct EHCIqh {
  137. uint32_t next; /* Standard next link pointer */
  138. /* endpoint characteristics */
  139. uint32_t epchar;
  140. #define QH_EPCHAR_RL_MASK 0xf0000000
  141. #define QH_EPCHAR_RL_SH 28
  142. #define QH_EPCHAR_C (1 << 27)
  143. #define QH_EPCHAR_MPLEN_MASK 0x07FF0000
  144. #define QH_EPCHAR_MPLEN_SH 16
  145. #define QH_EPCHAR_H (1 << 15)
  146. #define QH_EPCHAR_DTC (1 << 14)
  147. #define QH_EPCHAR_EPS_MASK 0x00003000
  148. #define QH_EPCHAR_EPS_SH 12
  149. #define EHCI_QH_EPS_FULL 0
  150. #define EHCI_QH_EPS_LOW 1
  151. #define EHCI_QH_EPS_HIGH 2
  152. #define EHCI_QH_EPS_RESERVED 3
  153. #define QH_EPCHAR_EP_MASK 0x00000f00
  154. #define QH_EPCHAR_EP_SH 8
  155. #define QH_EPCHAR_I (1 << 7)
  156. #define QH_EPCHAR_DEVADDR_MASK 0x0000007f
  157. #define QH_EPCHAR_DEVADDR_SH 0
  158. /* endpoint capabilities */
  159. uint32_t epcap;
  160. #define QH_EPCAP_MULT_MASK 0xc0000000
  161. #define QH_EPCAP_MULT_SH 30
  162. #define QH_EPCAP_PORTNUM_MASK 0x3f800000
  163. #define QH_EPCAP_PORTNUM_SH 23
  164. #define QH_EPCAP_HUBADDR_MASK 0x007f0000
  165. #define QH_EPCAP_HUBADDR_SH 16
  166. #define QH_EPCAP_CMASK_MASK 0x0000ff00
  167. #define QH_EPCAP_CMASK_SH 8
  168. #define QH_EPCAP_SMASK_MASK 0x000000ff
  169. #define QH_EPCAP_SMASK_SH 0
  170. uint32_t current_qtd; /* Standard next link pointer */
  171. uint32_t next_qtd; /* Standard next link pointer */
  172. uint32_t altnext_qtd;
  173. #define QH_ALTNEXT_NAKCNT_MASK 0x0000001e
  174. #define QH_ALTNEXT_NAKCNT_SH 1
  175. uint32_t token; /* Same as QTD token */
  176. uint32_t bufptr[5]; /* Standard buffer pointer */
  177. #define BUFPTR_CPROGMASK_MASK 0x000000ff
  178. #define BUFPTR_FRAMETAG_MASK 0x0000001f
  179. #define BUFPTR_SBYTES_MASK 0x00000fe0
  180. #define BUFPTR_SBYTES_SH 5
  181. } EHCIqh;
  182. /* EHCI spec version 1.0 Section 3.7
  183. */
  184. typedef struct EHCIfstn {
  185. uint32_t next; /* Standard next link pointer */
  186. uint32_t backptr; /* Standard next link pointer */
  187. } EHCIfstn;
  188. enum async_state {
  189. EHCI_ASYNC_NONE = 0,
  190. EHCI_ASYNC_INITIALIZED,
  191. EHCI_ASYNC_INFLIGHT,
  192. EHCI_ASYNC_FINISHED,
  193. };
  194. struct EHCIPacket {
  195. EHCIQueue *queue;
  196. QTAILQ_ENTRY(EHCIPacket) next;
  197. EHCIqtd qtd; /* copy of current QTD (being worked on) */
  198. uint32_t qtdaddr; /* address QTD read from */
  199. USBPacket packet;
  200. QEMUSGList sgl;
  201. int pid;
  202. enum async_state async;
  203. };
  204. struct EHCIQueue {
  205. EHCIState *ehci;
  206. QTAILQ_ENTRY(EHCIQueue) next;
  207. uint32_t seen;
  208. uint64_t ts;
  209. int async;
  210. int transact_ctr;
  211. /* cached data from guest - needs to be flushed
  212. * when guest removes an entry (doorbell, handshake sequence)
  213. */
  214. EHCIqh qh; /* copy of current QH (being worked on) */
  215. uint32_t qhaddr; /* address QH read from */
  216. uint32_t qtdaddr; /* address QTD read from */
  217. int last_pid; /* pid of last packet executed */
  218. USBDevice *dev;
  219. QTAILQ_HEAD(, EHCIPacket) packets;
  220. };
  221. typedef QTAILQ_HEAD(EHCIQueueHead, EHCIQueue) EHCIQueueHead;
  222. struct EHCIState {
  223. USBBus bus;
  224. DeviceState *device;
  225. qemu_irq irq;
  226. MemoryRegion mem;
  227. AddressSpace *as;
  228. MemoryRegion mem_caps;
  229. MemoryRegion mem_opreg;
  230. MemoryRegion mem_ports;
  231. int companion_count;
  232. bool companion_enable;
  233. uint16_t capsbase;
  234. uint16_t opregbase;
  235. uint16_t portscbase;
  236. uint16_t portnr;
  237. /* properties */
  238. uint32_t maxframes;
  239. /*
  240. * EHCI spec version 1.0 Section 2.3
  241. * Host Controller Operational Registers
  242. */
  243. uint8_t caps[CAPA_SIZE];
  244. union {
  245. uint32_t opreg[0x44/sizeof(uint32_t)];
  246. struct {
  247. uint32_t usbcmd;
  248. uint32_t usbsts;
  249. uint32_t usbintr;
  250. uint32_t frindex;
  251. uint32_t ctrldssegment;
  252. uint32_t periodiclistbase;
  253. uint32_t asynclistaddr;
  254. uint32_t notused[9];
  255. uint32_t configflag;
  256. };
  257. };
  258. uint32_t portsc[NB_PORTS];
  259. /*
  260. * Internal states, shadow registers, etc
  261. */
  262. QEMUTimer *frame_timer;
  263. QEMUBH *async_bh;
  264. bool working;
  265. uint32_t astate; /* Current state in asynchronous schedule */
  266. uint32_t pstate; /* Current state in periodic schedule */
  267. USBPort ports[NB_PORTS];
  268. USBPort *companion_ports[NB_PORTS];
  269. uint32_t usbsts_pending;
  270. uint32_t usbsts_frindex;
  271. EHCIQueueHead aqueues;
  272. EHCIQueueHead pqueues;
  273. /* which address to look at next */
  274. uint32_t a_fetch_addr;
  275. uint32_t p_fetch_addr;
  276. USBPacket ipacket;
  277. QEMUSGList isgl;
  278. uint64_t last_run_ns;
  279. uint32_t async_stepdown;
  280. uint32_t periodic_sched_active;
  281. bool int_req_by_async;
  282. VMChangeStateEntry *vmstate;
  283. };
  284. extern const VMStateDescription vmstate_ehci;
  285. void usb_ehci_init(EHCIState *s, DeviceState *dev);
  286. void usb_ehci_finalize(EHCIState *s);
  287. void usb_ehci_realize(EHCIState *s, DeviceState *dev, Error **errp);
  288. void usb_ehci_unrealize(EHCIState *s, DeviceState *dev);
  289. void ehci_reset(void *opaque);
  290. #define TYPE_PCI_EHCI "pci-ehci-usb"
  291. OBJECT_DECLARE_SIMPLE_TYPE(EHCIPCIState, PCI_EHCI)
  292. struct EHCIPCIState {
  293. /*< private >*/
  294. PCIDevice pcidev;
  295. /*< public >*/
  296. EHCIState ehci;
  297. };
  298. #define TYPE_SYS_BUS_EHCI "sysbus-ehci-usb"
  299. #define TYPE_PLATFORM_EHCI "platform-ehci-usb"
  300. #define TYPE_EXYNOS4210_EHCI "exynos4210-ehci-usb"
  301. #define TYPE_AW_H3_EHCI "aw-h3-ehci-usb"
  302. #define TYPE_TEGRA2_EHCI "tegra2-ehci-usb"
  303. #define TYPE_PPC4xx_EHCI "ppc4xx-ehci-usb"
  304. #define TYPE_FUSBH200_EHCI "fusbh200-ehci-usb"
  305. OBJECT_DECLARE_TYPE(EHCISysBusState, SysBusEHCIClass, SYS_BUS_EHCI)
  306. struct EHCISysBusState {
  307. /*< private >*/
  308. SysBusDevice parent_obj;
  309. /*< public >*/
  310. EHCIState ehci;
  311. };
  312. struct SysBusEHCIClass {
  313. /*< private >*/
  314. SysBusDeviceClass parent_class;
  315. /*< public >*/
  316. uint16_t capsbase;
  317. uint16_t opregbase;
  318. uint16_t portscbase;
  319. uint16_t portnr;
  320. };
  321. OBJECT_DECLARE_SIMPLE_TYPE(FUSBH200EHCIState, FUSBH200_EHCI)
  322. struct FUSBH200EHCIState {
  323. /*< private >*/
  324. EHCISysBusState parent_obj;
  325. /*< public >*/
  326. MemoryRegion mem_vendor;
  327. };
  328. #endif