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