hcd-ehci.h 11 KB

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