hcd-ehci.h 11 KB

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