hcd-ehci.c 73 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553
  1. /*
  2. * QEMU USB EHCI Emulation
  3. *
  4. * Copyright(c) 2008 Emutex Ltd. (address@hidden)
  5. * Copyright(c) 2011-2012 Red Hat, Inc.
  6. *
  7. * Red Hat Authors:
  8. * Gerd Hoffmann <kraxel@redhat.com>
  9. * Hans de Goede <hdegoede@redhat.com>
  10. *
  11. * EHCI project was started by Mark Burkley, with contributions by
  12. * Niels de Vos. David S. Ahern continued working on it. Kevin Wolf,
  13. * Jan Kiszka and Vincent Palatin contributed bugfixes.
  14. *
  15. *
  16. * This library is free software; you can redistribute it and/or
  17. * modify it under the terms of the GNU Lesser General Public
  18. * License as published by the Free Software Foundation; either
  19. * version 2 of the License, or(at your option) any later version.
  20. *
  21. * This library is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  24. * Lesser General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU General Public License
  27. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  28. */
  29. #include "hw/usb/hcd-ehci.h"
  30. /* Capability Registers Base Address - section 2.2 */
  31. #define CAPLENGTH 0x0000 /* 1-byte, 0x0001 reserved */
  32. #define HCIVERSION 0x0002 /* 2-bytes, i/f version # */
  33. #define HCSPARAMS 0x0004 /* 4-bytes, structural params */
  34. #define HCCPARAMS 0x0008 /* 4-bytes, capability params */
  35. #define EECP HCCPARAMS + 1
  36. #define HCSPPORTROUTE1 0x000c
  37. #define HCSPPORTROUTE2 0x0010
  38. #define USBCMD 0x0000
  39. #define USBCMD_RUNSTOP (1 << 0) // run / Stop
  40. #define USBCMD_HCRESET (1 << 1) // HC Reset
  41. #define USBCMD_FLS (3 << 2) // Frame List Size
  42. #define USBCMD_FLS_SH 2 // Frame List Size Shift
  43. #define USBCMD_PSE (1 << 4) // Periodic Schedule Enable
  44. #define USBCMD_ASE (1 << 5) // Asynch Schedule Enable
  45. #define USBCMD_IAAD (1 << 6) // Int Asynch Advance Doorbell
  46. #define USBCMD_LHCR (1 << 7) // Light Host Controller Reset
  47. #define USBCMD_ASPMC (3 << 8) // Async Sched Park Mode Count
  48. #define USBCMD_ASPME (1 << 11) // Async Sched Park Mode Enable
  49. #define USBCMD_ITC (0x7f << 16) // Int Threshold Control
  50. #define USBCMD_ITC_SH 16 // Int Threshold Control Shift
  51. #define USBSTS 0x0004
  52. #define USBSTS_RO_MASK 0x0000003f
  53. #define USBSTS_INT (1 << 0) // USB Interrupt
  54. #define USBSTS_ERRINT (1 << 1) // Error Interrupt
  55. #define USBSTS_PCD (1 << 2) // Port Change Detect
  56. #define USBSTS_FLR (1 << 3) // Frame List Rollover
  57. #define USBSTS_HSE (1 << 4) // Host System Error
  58. #define USBSTS_IAA (1 << 5) // Interrupt on Async Advance
  59. #define USBSTS_HALT (1 << 12) // HC Halted
  60. #define USBSTS_REC (1 << 13) // Reclamation
  61. #define USBSTS_PSS (1 << 14) // Periodic Schedule Status
  62. #define USBSTS_ASS (1 << 15) // Asynchronous Schedule Status
  63. /*
  64. * Interrupt enable bits correspond to the interrupt active bits in USBSTS
  65. * so no need to redefine here.
  66. */
  67. #define USBINTR 0x0008
  68. #define USBINTR_MASK 0x0000003f
  69. #define FRINDEX 0x000c
  70. #define CTRLDSSEGMENT 0x0010
  71. #define PERIODICLISTBASE 0x0014
  72. #define ASYNCLISTADDR 0x0018
  73. #define ASYNCLISTADDR_MASK 0xffffffe0
  74. #define CONFIGFLAG 0x0040
  75. /*
  76. * Bits that are reserved or are read-only are masked out of values
  77. * written to us by software
  78. */
  79. #define PORTSC_RO_MASK 0x007001c0
  80. #define PORTSC_RWC_MASK 0x0000002a
  81. #define PORTSC_WKOC_E (1 << 22) // Wake on Over Current Enable
  82. #define PORTSC_WKDS_E (1 << 21) // Wake on Disconnect Enable
  83. #define PORTSC_WKCN_E (1 << 20) // Wake on Connect Enable
  84. #define PORTSC_PTC (15 << 16) // Port Test Control
  85. #define PORTSC_PTC_SH 16 // Port Test Control shift
  86. #define PORTSC_PIC (3 << 14) // Port Indicator Control
  87. #define PORTSC_PIC_SH 14 // Port Indicator Control Shift
  88. #define PORTSC_POWNER (1 << 13) // Port Owner
  89. #define PORTSC_PPOWER (1 << 12) // Port Power
  90. #define PORTSC_LINESTAT (3 << 10) // Port Line Status
  91. #define PORTSC_LINESTAT_SH 10 // Port Line Status Shift
  92. #define PORTSC_PRESET (1 << 8) // Port Reset
  93. #define PORTSC_SUSPEND (1 << 7) // Port Suspend
  94. #define PORTSC_FPRES (1 << 6) // Force Port Resume
  95. #define PORTSC_OCC (1 << 5) // Over Current Change
  96. #define PORTSC_OCA (1 << 4) // Over Current Active
  97. #define PORTSC_PEDC (1 << 3) // Port Enable/Disable Change
  98. #define PORTSC_PED (1 << 2) // Port Enable/Disable
  99. #define PORTSC_CSC (1 << 1) // Connect Status Change
  100. #define PORTSC_CONNECT (1 << 0) // Current Connect Status
  101. #define FRAME_TIMER_FREQ 1000
  102. #define FRAME_TIMER_NS (1000000000 / FRAME_TIMER_FREQ)
  103. #define UFRAME_TIMER_NS (FRAME_TIMER_NS / 8)
  104. #define NB_MAXINTRATE 8 // Max rate at which controller issues ints
  105. #define BUFF_SIZE 5*4096 // Max bytes to transfer per transaction
  106. #define MAX_QH 100 // Max allowable queue heads in a chain
  107. #define MIN_UFR_PER_TICK 24 /* Min frames to process when catching up */
  108. #define PERIODIC_ACTIVE 512 /* Micro-frames */
  109. /* Internal periodic / asynchronous schedule state machine states
  110. */
  111. typedef enum {
  112. EST_INACTIVE = 1000,
  113. EST_ACTIVE,
  114. EST_EXECUTING,
  115. EST_SLEEPING,
  116. /* The following states are internal to the state machine function
  117. */
  118. EST_WAITLISTHEAD,
  119. EST_FETCHENTRY,
  120. EST_FETCHQH,
  121. EST_FETCHITD,
  122. EST_FETCHSITD,
  123. EST_ADVANCEQUEUE,
  124. EST_FETCHQTD,
  125. EST_EXECUTE,
  126. EST_WRITEBACK,
  127. EST_HORIZONTALQH
  128. } EHCI_STATES;
  129. /* macros for accessing fields within next link pointer entry */
  130. #define NLPTR_GET(x) ((x) & 0xffffffe0)
  131. #define NLPTR_TYPE_GET(x) (((x) >> 1) & 3)
  132. #define NLPTR_TBIT(x) ((x) & 1) // 1=invalid, 0=valid
  133. /* link pointer types */
  134. #define NLPTR_TYPE_ITD 0 // isoc xfer descriptor
  135. #define NLPTR_TYPE_QH 1 // queue head
  136. #define NLPTR_TYPE_STITD 2 // split xaction, isoc xfer descriptor
  137. #define NLPTR_TYPE_FSTN 3 // frame span traversal node
  138. #define SET_LAST_RUN_CLOCK(s) \
  139. (s)->last_run_ns = qemu_get_clock_ns(vm_clock);
  140. /* nifty macros from Arnon's EHCI version */
  141. #define get_field(data, field) \
  142. (((data) & field##_MASK) >> field##_SH)
  143. #define set_field(data, newval, field) do { \
  144. uint32_t val = *data; \
  145. val &= ~ field##_MASK; \
  146. val |= ((newval) << field##_SH) & field##_MASK; \
  147. *data = val; \
  148. } while(0)
  149. static const char *ehci_state_names[] = {
  150. [EST_INACTIVE] = "INACTIVE",
  151. [EST_ACTIVE] = "ACTIVE",
  152. [EST_EXECUTING] = "EXECUTING",
  153. [EST_SLEEPING] = "SLEEPING",
  154. [EST_WAITLISTHEAD] = "WAITLISTHEAD",
  155. [EST_FETCHENTRY] = "FETCH ENTRY",
  156. [EST_FETCHQH] = "FETCH QH",
  157. [EST_FETCHITD] = "FETCH ITD",
  158. [EST_ADVANCEQUEUE] = "ADVANCEQUEUE",
  159. [EST_FETCHQTD] = "FETCH QTD",
  160. [EST_EXECUTE] = "EXECUTE",
  161. [EST_WRITEBACK] = "WRITEBACK",
  162. [EST_HORIZONTALQH] = "HORIZONTALQH",
  163. };
  164. static const char *ehci_mmio_names[] = {
  165. [USBCMD] = "USBCMD",
  166. [USBSTS] = "USBSTS",
  167. [USBINTR] = "USBINTR",
  168. [FRINDEX] = "FRINDEX",
  169. [PERIODICLISTBASE] = "P-LIST BASE",
  170. [ASYNCLISTADDR] = "A-LIST ADDR",
  171. [CONFIGFLAG] = "CONFIGFLAG",
  172. };
  173. static int ehci_state_executing(EHCIQueue *q);
  174. static int ehci_state_writeback(EHCIQueue *q);
  175. static int ehci_state_advqueue(EHCIQueue *q);
  176. static int ehci_fill_queue(EHCIPacket *p);
  177. static void ehci_free_packet(EHCIPacket *p);
  178. static const char *nr2str(const char **n, size_t len, uint32_t nr)
  179. {
  180. if (nr < len && n[nr] != NULL) {
  181. return n[nr];
  182. } else {
  183. return "unknown";
  184. }
  185. }
  186. static const char *state2str(uint32_t state)
  187. {
  188. return nr2str(ehci_state_names, ARRAY_SIZE(ehci_state_names), state);
  189. }
  190. static const char *addr2str(hwaddr addr)
  191. {
  192. return nr2str(ehci_mmio_names, ARRAY_SIZE(ehci_mmio_names), addr);
  193. }
  194. static void ehci_trace_usbsts(uint32_t mask, int state)
  195. {
  196. /* interrupts */
  197. if (mask & USBSTS_INT) {
  198. trace_usb_ehci_usbsts("INT", state);
  199. }
  200. if (mask & USBSTS_ERRINT) {
  201. trace_usb_ehci_usbsts("ERRINT", state);
  202. }
  203. if (mask & USBSTS_PCD) {
  204. trace_usb_ehci_usbsts("PCD", state);
  205. }
  206. if (mask & USBSTS_FLR) {
  207. trace_usb_ehci_usbsts("FLR", state);
  208. }
  209. if (mask & USBSTS_HSE) {
  210. trace_usb_ehci_usbsts("HSE", state);
  211. }
  212. if (mask & USBSTS_IAA) {
  213. trace_usb_ehci_usbsts("IAA", state);
  214. }
  215. /* status */
  216. if (mask & USBSTS_HALT) {
  217. trace_usb_ehci_usbsts("HALT", state);
  218. }
  219. if (mask & USBSTS_REC) {
  220. trace_usb_ehci_usbsts("REC", state);
  221. }
  222. if (mask & USBSTS_PSS) {
  223. trace_usb_ehci_usbsts("PSS", state);
  224. }
  225. if (mask & USBSTS_ASS) {
  226. trace_usb_ehci_usbsts("ASS", state);
  227. }
  228. }
  229. static inline void ehci_set_usbsts(EHCIState *s, int mask)
  230. {
  231. if ((s->usbsts & mask) == mask) {
  232. return;
  233. }
  234. ehci_trace_usbsts(mask, 1);
  235. s->usbsts |= mask;
  236. }
  237. static inline void ehci_clear_usbsts(EHCIState *s, int mask)
  238. {
  239. if ((s->usbsts & mask) == 0) {
  240. return;
  241. }
  242. ehci_trace_usbsts(mask, 0);
  243. s->usbsts &= ~mask;
  244. }
  245. /* update irq line */
  246. static inline void ehci_update_irq(EHCIState *s)
  247. {
  248. int level = 0;
  249. if ((s->usbsts & USBINTR_MASK) & s->usbintr) {
  250. level = 1;
  251. }
  252. trace_usb_ehci_irq(level, s->frindex, s->usbsts, s->usbintr);
  253. qemu_set_irq(s->irq, level);
  254. }
  255. /* flag interrupt condition */
  256. static inline void ehci_raise_irq(EHCIState *s, int intr)
  257. {
  258. if (intr & (USBSTS_PCD | USBSTS_FLR | USBSTS_HSE)) {
  259. s->usbsts |= intr;
  260. ehci_update_irq(s);
  261. } else {
  262. s->usbsts_pending |= intr;
  263. }
  264. }
  265. /*
  266. * Commit pending interrupts (added via ehci_raise_irq),
  267. * at the rate allowed by "Interrupt Threshold Control".
  268. */
  269. static inline void ehci_commit_irq(EHCIState *s)
  270. {
  271. uint32_t itc;
  272. if (!s->usbsts_pending) {
  273. return;
  274. }
  275. if (s->usbsts_frindex > s->frindex) {
  276. return;
  277. }
  278. itc = (s->usbcmd >> 16) & 0xff;
  279. s->usbsts |= s->usbsts_pending;
  280. s->usbsts_pending = 0;
  281. s->usbsts_frindex = s->frindex + itc;
  282. ehci_update_irq(s);
  283. }
  284. static void ehci_update_halt(EHCIState *s)
  285. {
  286. if (s->usbcmd & USBCMD_RUNSTOP) {
  287. ehci_clear_usbsts(s, USBSTS_HALT);
  288. } else {
  289. if (s->astate == EST_INACTIVE && s->pstate == EST_INACTIVE) {
  290. ehci_set_usbsts(s, USBSTS_HALT);
  291. }
  292. }
  293. }
  294. static void ehci_set_state(EHCIState *s, int async, int state)
  295. {
  296. if (async) {
  297. trace_usb_ehci_state("async", state2str(state));
  298. s->astate = state;
  299. if (s->astate == EST_INACTIVE) {
  300. ehci_clear_usbsts(s, USBSTS_ASS);
  301. ehci_update_halt(s);
  302. } else {
  303. ehci_set_usbsts(s, USBSTS_ASS);
  304. }
  305. } else {
  306. trace_usb_ehci_state("periodic", state2str(state));
  307. s->pstate = state;
  308. if (s->pstate == EST_INACTIVE) {
  309. ehci_clear_usbsts(s, USBSTS_PSS);
  310. ehci_update_halt(s);
  311. } else {
  312. ehci_set_usbsts(s, USBSTS_PSS);
  313. }
  314. }
  315. }
  316. static int ehci_get_state(EHCIState *s, int async)
  317. {
  318. return async ? s->astate : s->pstate;
  319. }
  320. static void ehci_set_fetch_addr(EHCIState *s, int async, uint32_t addr)
  321. {
  322. if (async) {
  323. s->a_fetch_addr = addr;
  324. } else {
  325. s->p_fetch_addr = addr;
  326. }
  327. }
  328. static int ehci_get_fetch_addr(EHCIState *s, int async)
  329. {
  330. return async ? s->a_fetch_addr : s->p_fetch_addr;
  331. }
  332. static void ehci_trace_qh(EHCIQueue *q, hwaddr addr, EHCIqh *qh)
  333. {
  334. /* need three here due to argument count limits */
  335. trace_usb_ehci_qh_ptrs(q, addr, qh->next,
  336. qh->current_qtd, qh->next_qtd, qh->altnext_qtd);
  337. trace_usb_ehci_qh_fields(addr,
  338. get_field(qh->epchar, QH_EPCHAR_RL),
  339. get_field(qh->epchar, QH_EPCHAR_MPLEN),
  340. get_field(qh->epchar, QH_EPCHAR_EPS),
  341. get_field(qh->epchar, QH_EPCHAR_EP),
  342. get_field(qh->epchar, QH_EPCHAR_DEVADDR));
  343. trace_usb_ehci_qh_bits(addr,
  344. (bool)(qh->epchar & QH_EPCHAR_C),
  345. (bool)(qh->epchar & QH_EPCHAR_H),
  346. (bool)(qh->epchar & QH_EPCHAR_DTC),
  347. (bool)(qh->epchar & QH_EPCHAR_I));
  348. }
  349. static void ehci_trace_qtd(EHCIQueue *q, hwaddr addr, EHCIqtd *qtd)
  350. {
  351. /* need three here due to argument count limits */
  352. trace_usb_ehci_qtd_ptrs(q, addr, qtd->next, qtd->altnext);
  353. trace_usb_ehci_qtd_fields(addr,
  354. get_field(qtd->token, QTD_TOKEN_TBYTES),
  355. get_field(qtd->token, QTD_TOKEN_CPAGE),
  356. get_field(qtd->token, QTD_TOKEN_CERR),
  357. get_field(qtd->token, QTD_TOKEN_PID));
  358. trace_usb_ehci_qtd_bits(addr,
  359. (bool)(qtd->token & QTD_TOKEN_IOC),
  360. (bool)(qtd->token & QTD_TOKEN_ACTIVE),
  361. (bool)(qtd->token & QTD_TOKEN_HALT),
  362. (bool)(qtd->token & QTD_TOKEN_BABBLE),
  363. (bool)(qtd->token & QTD_TOKEN_XACTERR));
  364. }
  365. static void ehci_trace_itd(EHCIState *s, hwaddr addr, EHCIitd *itd)
  366. {
  367. trace_usb_ehci_itd(addr, itd->next,
  368. get_field(itd->bufptr[1], ITD_BUFPTR_MAXPKT),
  369. get_field(itd->bufptr[2], ITD_BUFPTR_MULT),
  370. get_field(itd->bufptr[0], ITD_BUFPTR_EP),
  371. get_field(itd->bufptr[0], ITD_BUFPTR_DEVADDR));
  372. }
  373. static void ehci_trace_sitd(EHCIState *s, hwaddr addr,
  374. EHCIsitd *sitd)
  375. {
  376. trace_usb_ehci_sitd(addr, sitd->next,
  377. (bool)(sitd->results & SITD_RESULTS_ACTIVE));
  378. }
  379. static void ehci_trace_guest_bug(EHCIState *s, const char *message)
  380. {
  381. trace_usb_ehci_guest_bug(message);
  382. fprintf(stderr, "ehci warning: %s\n", message);
  383. }
  384. static inline bool ehci_enabled(EHCIState *s)
  385. {
  386. return s->usbcmd & USBCMD_RUNSTOP;
  387. }
  388. static inline bool ehci_async_enabled(EHCIState *s)
  389. {
  390. return ehci_enabled(s) && (s->usbcmd & USBCMD_ASE);
  391. }
  392. static inline bool ehci_periodic_enabled(EHCIState *s)
  393. {
  394. return ehci_enabled(s) && (s->usbcmd & USBCMD_PSE);
  395. }
  396. /* Get an array of dwords from main memory */
  397. static inline int get_dwords(EHCIState *ehci, uint32_t addr,
  398. uint32_t *buf, int num)
  399. {
  400. int i;
  401. if (!ehci->dma) {
  402. ehci_raise_irq(ehci, USBSTS_HSE);
  403. ehci->usbcmd &= ~USBCMD_RUNSTOP;
  404. trace_usb_ehci_dma_error();
  405. return -1;
  406. }
  407. for (i = 0; i < num; i++, buf++, addr += sizeof(*buf)) {
  408. dma_memory_read(ehci->dma, addr, buf, sizeof(*buf));
  409. *buf = le32_to_cpu(*buf);
  410. }
  411. return num;
  412. }
  413. /* Put an array of dwords in to main memory */
  414. static inline int put_dwords(EHCIState *ehci, uint32_t addr,
  415. uint32_t *buf, int num)
  416. {
  417. int i;
  418. if (!ehci->dma) {
  419. ehci_raise_irq(ehci, USBSTS_HSE);
  420. ehci->usbcmd &= ~USBCMD_RUNSTOP;
  421. trace_usb_ehci_dma_error();
  422. return -1;
  423. }
  424. for (i = 0; i < num; i++, buf++, addr += sizeof(*buf)) {
  425. uint32_t tmp = cpu_to_le32(*buf);
  426. dma_memory_write(ehci->dma, addr, &tmp, sizeof(tmp));
  427. }
  428. return num;
  429. }
  430. static int ehci_get_pid(EHCIqtd *qtd)
  431. {
  432. switch (get_field(qtd->token, QTD_TOKEN_PID)) {
  433. case 0:
  434. return USB_TOKEN_OUT;
  435. case 1:
  436. return USB_TOKEN_IN;
  437. case 2:
  438. return USB_TOKEN_SETUP;
  439. default:
  440. fprintf(stderr, "bad token\n");
  441. return 0;
  442. }
  443. }
  444. static bool ehci_verify_qh(EHCIQueue *q, EHCIqh *qh)
  445. {
  446. uint32_t devaddr = get_field(qh->epchar, QH_EPCHAR_DEVADDR);
  447. uint32_t endp = get_field(qh->epchar, QH_EPCHAR_EP);
  448. if ((devaddr != get_field(q->qh.epchar, QH_EPCHAR_DEVADDR)) ||
  449. (endp != get_field(q->qh.epchar, QH_EPCHAR_EP)) ||
  450. (qh->current_qtd != q->qh.current_qtd) ||
  451. (q->async && qh->next_qtd != q->qh.next_qtd) ||
  452. (memcmp(&qh->altnext_qtd, &q->qh.altnext_qtd,
  453. 7 * sizeof(uint32_t)) != 0) ||
  454. (q->dev != NULL && q->dev->addr != devaddr)) {
  455. return false;
  456. } else {
  457. return true;
  458. }
  459. }
  460. static bool ehci_verify_qtd(EHCIPacket *p, EHCIqtd *qtd)
  461. {
  462. if (p->qtdaddr != p->queue->qtdaddr ||
  463. (p->queue->async && !NLPTR_TBIT(p->qtd.next) &&
  464. (p->qtd.next != qtd->next)) ||
  465. (!NLPTR_TBIT(p->qtd.altnext) && (p->qtd.altnext != qtd->altnext)) ||
  466. p->qtd.token != qtd->token ||
  467. p->qtd.bufptr[0] != qtd->bufptr[0]) {
  468. return false;
  469. } else {
  470. return true;
  471. }
  472. }
  473. static bool ehci_verify_pid(EHCIQueue *q, EHCIqtd *qtd)
  474. {
  475. int ep = get_field(q->qh.epchar, QH_EPCHAR_EP);
  476. int pid = ehci_get_pid(qtd);
  477. /* Note the pid changing is normal for ep 0 (the control ep) */
  478. if (q->last_pid && ep != 0 && pid != q->last_pid) {
  479. return false;
  480. } else {
  481. return true;
  482. }
  483. }
  484. /* Finish executing and writeback a packet outside of the regular
  485. fetchqh -> fetchqtd -> execute -> writeback cycle */
  486. static void ehci_writeback_async_complete_packet(EHCIPacket *p)
  487. {
  488. EHCIQueue *q = p->queue;
  489. EHCIqtd qtd;
  490. EHCIqh qh;
  491. int state;
  492. /* Verify the qh + qtd, like we do when going through fetchqh & fetchqtd */
  493. get_dwords(q->ehci, NLPTR_GET(q->qhaddr),
  494. (uint32_t *) &qh, sizeof(EHCIqh) >> 2);
  495. get_dwords(q->ehci, NLPTR_GET(q->qtdaddr),
  496. (uint32_t *) &qtd, sizeof(EHCIqtd) >> 2);
  497. if (!ehci_verify_qh(q, &qh) || !ehci_verify_qtd(p, &qtd)) {
  498. p->async = EHCI_ASYNC_INITIALIZED;
  499. ehci_free_packet(p);
  500. return;
  501. }
  502. state = ehci_get_state(q->ehci, q->async);
  503. ehci_state_executing(q);
  504. ehci_state_writeback(q); /* Frees the packet! */
  505. if (!(q->qh.token & QTD_TOKEN_HALT)) {
  506. ehci_state_advqueue(q);
  507. }
  508. ehci_set_state(q->ehci, q->async, state);
  509. }
  510. /* packet management */
  511. static EHCIPacket *ehci_alloc_packet(EHCIQueue *q)
  512. {
  513. EHCIPacket *p;
  514. p = g_new0(EHCIPacket, 1);
  515. p->queue = q;
  516. usb_packet_init(&p->packet);
  517. QTAILQ_INSERT_TAIL(&q->packets, p, next);
  518. trace_usb_ehci_packet_action(p->queue, p, "alloc");
  519. return p;
  520. }
  521. static void ehci_free_packet(EHCIPacket *p)
  522. {
  523. if (p->async == EHCI_ASYNC_FINISHED) {
  524. ehci_writeback_async_complete_packet(p);
  525. return;
  526. }
  527. trace_usb_ehci_packet_action(p->queue, p, "free");
  528. if (p->async == EHCI_ASYNC_INITIALIZED) {
  529. usb_packet_unmap(&p->packet, &p->sgl);
  530. qemu_sglist_destroy(&p->sgl);
  531. }
  532. if (p->async == EHCI_ASYNC_INFLIGHT) {
  533. usb_cancel_packet(&p->packet);
  534. usb_packet_unmap(&p->packet, &p->sgl);
  535. qemu_sglist_destroy(&p->sgl);
  536. }
  537. QTAILQ_REMOVE(&p->queue->packets, p, next);
  538. usb_packet_cleanup(&p->packet);
  539. g_free(p);
  540. }
  541. /* queue management */
  542. static EHCIQueue *ehci_alloc_queue(EHCIState *ehci, uint32_t addr, int async)
  543. {
  544. EHCIQueueHead *head = async ? &ehci->aqueues : &ehci->pqueues;
  545. EHCIQueue *q;
  546. q = g_malloc0(sizeof(*q));
  547. q->ehci = ehci;
  548. q->qhaddr = addr;
  549. q->async = async;
  550. QTAILQ_INIT(&q->packets);
  551. QTAILQ_INSERT_HEAD(head, q, next);
  552. trace_usb_ehci_queue_action(q, "alloc");
  553. return q;
  554. }
  555. static void ehci_queue_stopped(EHCIQueue *q)
  556. {
  557. int endp = get_field(q->qh.epchar, QH_EPCHAR_EP);
  558. if (!q->last_pid || !q->dev) {
  559. return;
  560. }
  561. usb_device_ep_stopped(q->dev, usb_ep_get(q->dev, q->last_pid, endp));
  562. }
  563. static int ehci_cancel_queue(EHCIQueue *q)
  564. {
  565. EHCIPacket *p;
  566. int packets = 0;
  567. p = QTAILQ_FIRST(&q->packets);
  568. if (p == NULL) {
  569. goto leave;
  570. }
  571. trace_usb_ehci_queue_action(q, "cancel");
  572. do {
  573. ehci_free_packet(p);
  574. packets++;
  575. } while ((p = QTAILQ_FIRST(&q->packets)) != NULL);
  576. leave:
  577. ehci_queue_stopped(q);
  578. return packets;
  579. }
  580. static int ehci_reset_queue(EHCIQueue *q)
  581. {
  582. int packets;
  583. trace_usb_ehci_queue_action(q, "reset");
  584. packets = ehci_cancel_queue(q);
  585. q->dev = NULL;
  586. q->qtdaddr = 0;
  587. q->last_pid = 0;
  588. return packets;
  589. }
  590. static void ehci_free_queue(EHCIQueue *q, const char *warn)
  591. {
  592. EHCIQueueHead *head = q->async ? &q->ehci->aqueues : &q->ehci->pqueues;
  593. int cancelled;
  594. trace_usb_ehci_queue_action(q, "free");
  595. cancelled = ehci_cancel_queue(q);
  596. if (warn && cancelled > 0) {
  597. ehci_trace_guest_bug(q->ehci, warn);
  598. }
  599. QTAILQ_REMOVE(head, q, next);
  600. g_free(q);
  601. }
  602. static EHCIQueue *ehci_find_queue_by_qh(EHCIState *ehci, uint32_t addr,
  603. int async)
  604. {
  605. EHCIQueueHead *head = async ? &ehci->aqueues : &ehci->pqueues;
  606. EHCIQueue *q;
  607. QTAILQ_FOREACH(q, head, next) {
  608. if (addr == q->qhaddr) {
  609. return q;
  610. }
  611. }
  612. return NULL;
  613. }
  614. static void ehci_queues_rip_unused(EHCIState *ehci, int async)
  615. {
  616. EHCIQueueHead *head = async ? &ehci->aqueues : &ehci->pqueues;
  617. const char *warn = async ? "guest unlinked busy QH" : NULL;
  618. uint64_t maxage = FRAME_TIMER_NS * ehci->maxframes * 4;
  619. EHCIQueue *q, *tmp;
  620. QTAILQ_FOREACH_SAFE(q, head, next, tmp) {
  621. if (q->seen) {
  622. q->seen = 0;
  623. q->ts = ehci->last_run_ns;
  624. continue;
  625. }
  626. if (ehci->last_run_ns < q->ts + maxage) {
  627. continue;
  628. }
  629. ehci_free_queue(q, warn);
  630. }
  631. }
  632. static void ehci_queues_rip_unseen(EHCIState *ehci, int async)
  633. {
  634. EHCIQueueHead *head = async ? &ehci->aqueues : &ehci->pqueues;
  635. EHCIQueue *q, *tmp;
  636. QTAILQ_FOREACH_SAFE(q, head, next, tmp) {
  637. if (!q->seen) {
  638. ehci_free_queue(q, NULL);
  639. }
  640. }
  641. }
  642. static void ehci_queues_rip_device(EHCIState *ehci, USBDevice *dev, int async)
  643. {
  644. EHCIQueueHead *head = async ? &ehci->aqueues : &ehci->pqueues;
  645. EHCIQueue *q, *tmp;
  646. QTAILQ_FOREACH_SAFE(q, head, next, tmp) {
  647. if (q->dev != dev) {
  648. continue;
  649. }
  650. ehci_free_queue(q, NULL);
  651. }
  652. }
  653. static void ehci_queues_rip_all(EHCIState *ehci, int async)
  654. {
  655. EHCIQueueHead *head = async ? &ehci->aqueues : &ehci->pqueues;
  656. const char *warn = async ? "guest stopped busy async schedule" : NULL;
  657. EHCIQueue *q, *tmp;
  658. QTAILQ_FOREACH_SAFE(q, head, next, tmp) {
  659. ehci_free_queue(q, warn);
  660. }
  661. }
  662. /* Attach or detach a device on root hub */
  663. static void ehci_attach(USBPort *port)
  664. {
  665. EHCIState *s = port->opaque;
  666. uint32_t *portsc = &s->portsc[port->index];
  667. const char *owner = (*portsc & PORTSC_POWNER) ? "comp" : "ehci";
  668. trace_usb_ehci_port_attach(port->index, owner, port->dev->product_desc);
  669. if (*portsc & PORTSC_POWNER) {
  670. USBPort *companion = s->companion_ports[port->index];
  671. companion->dev = port->dev;
  672. companion->ops->attach(companion);
  673. return;
  674. }
  675. *portsc |= PORTSC_CONNECT;
  676. *portsc |= PORTSC_CSC;
  677. ehci_raise_irq(s, USBSTS_PCD);
  678. }
  679. static void ehci_detach(USBPort *port)
  680. {
  681. EHCIState *s = port->opaque;
  682. uint32_t *portsc = &s->portsc[port->index];
  683. const char *owner = (*portsc & PORTSC_POWNER) ? "comp" : "ehci";
  684. trace_usb_ehci_port_detach(port->index, owner);
  685. if (*portsc & PORTSC_POWNER) {
  686. USBPort *companion = s->companion_ports[port->index];
  687. companion->ops->detach(companion);
  688. companion->dev = NULL;
  689. /*
  690. * EHCI spec 4.2.2: "When a disconnect occurs... On the event,
  691. * the port ownership is returned immediately to the EHCI controller."
  692. */
  693. *portsc &= ~PORTSC_POWNER;
  694. return;
  695. }
  696. ehci_queues_rip_device(s, port->dev, 0);
  697. ehci_queues_rip_device(s, port->dev, 1);
  698. *portsc &= ~(PORTSC_CONNECT|PORTSC_PED);
  699. *portsc |= PORTSC_CSC;
  700. ehci_raise_irq(s, USBSTS_PCD);
  701. }
  702. static void ehci_child_detach(USBPort *port, USBDevice *child)
  703. {
  704. EHCIState *s = port->opaque;
  705. uint32_t portsc = s->portsc[port->index];
  706. if (portsc & PORTSC_POWNER) {
  707. USBPort *companion = s->companion_ports[port->index];
  708. companion->ops->child_detach(companion, child);
  709. return;
  710. }
  711. ehci_queues_rip_device(s, child, 0);
  712. ehci_queues_rip_device(s, child, 1);
  713. }
  714. static void ehci_wakeup(USBPort *port)
  715. {
  716. EHCIState *s = port->opaque;
  717. uint32_t portsc = s->portsc[port->index];
  718. if (portsc & PORTSC_POWNER) {
  719. USBPort *companion = s->companion_ports[port->index];
  720. if (companion->ops->wakeup) {
  721. companion->ops->wakeup(companion);
  722. }
  723. return;
  724. }
  725. qemu_bh_schedule(s->async_bh);
  726. }
  727. static int ehci_register_companion(USBBus *bus, USBPort *ports[],
  728. uint32_t portcount, uint32_t firstport)
  729. {
  730. EHCIState *s = container_of(bus, EHCIState, bus);
  731. uint32_t i;
  732. if (firstport + portcount > NB_PORTS) {
  733. qerror_report(QERR_INVALID_PARAMETER_VALUE, "firstport",
  734. "firstport on masterbus");
  735. error_printf_unless_qmp(
  736. "firstport value of %u makes companion take ports %u - %u, which "
  737. "is outside of the valid range of 0 - %u\n", firstport, firstport,
  738. firstport + portcount - 1, NB_PORTS - 1);
  739. return -1;
  740. }
  741. for (i = 0; i < portcount; i++) {
  742. if (s->companion_ports[firstport + i]) {
  743. qerror_report(QERR_INVALID_PARAMETER_VALUE, "masterbus",
  744. "an USB masterbus");
  745. error_printf_unless_qmp(
  746. "port %u on masterbus %s already has a companion assigned\n",
  747. firstport + i, bus->qbus.name);
  748. return -1;
  749. }
  750. }
  751. for (i = 0; i < portcount; i++) {
  752. s->companion_ports[firstport + i] = ports[i];
  753. s->ports[firstport + i].speedmask |=
  754. USB_SPEED_MASK_LOW | USB_SPEED_MASK_FULL;
  755. /* Ensure devs attached before the initial reset go to the companion */
  756. s->portsc[firstport + i] = PORTSC_POWNER;
  757. }
  758. s->companion_count++;
  759. s->caps[0x05] = (s->companion_count << 4) | portcount;
  760. return 0;
  761. }
  762. static void ehci_wakeup_endpoint(USBBus *bus, USBEndpoint *ep)
  763. {
  764. EHCIState *s = container_of(bus, EHCIState, bus);
  765. uint32_t portsc = s->portsc[ep->dev->port->index];
  766. if (portsc & PORTSC_POWNER) {
  767. return;
  768. }
  769. s->periodic_sched_active = PERIODIC_ACTIVE;
  770. qemu_bh_schedule(s->async_bh);
  771. }
  772. static USBDevice *ehci_find_device(EHCIState *ehci, uint8_t addr)
  773. {
  774. USBDevice *dev;
  775. USBPort *port;
  776. int i;
  777. for (i = 0; i < NB_PORTS; i++) {
  778. port = &ehci->ports[i];
  779. if (!(ehci->portsc[i] & PORTSC_PED)) {
  780. DPRINTF("Port %d not enabled\n", i);
  781. continue;
  782. }
  783. dev = usb_find_device(port, addr);
  784. if (dev != NULL) {
  785. return dev;
  786. }
  787. }
  788. return NULL;
  789. }
  790. /* 4.1 host controller initialization */
  791. static void ehci_reset(void *opaque)
  792. {
  793. EHCIState *s = opaque;
  794. int i;
  795. USBDevice *devs[NB_PORTS];
  796. trace_usb_ehci_reset();
  797. /*
  798. * Do the detach before touching portsc, so that it correctly gets send to
  799. * us or to our companion based on PORTSC_POWNER before the reset.
  800. */
  801. for(i = 0; i < NB_PORTS; i++) {
  802. devs[i] = s->ports[i].dev;
  803. if (devs[i] && devs[i]->attached) {
  804. usb_detach(&s->ports[i]);
  805. }
  806. }
  807. memset(&s->opreg, 0x00, sizeof(s->opreg));
  808. memset(&s->portsc, 0x00, sizeof(s->portsc));
  809. s->usbcmd = NB_MAXINTRATE << USBCMD_ITC_SH;
  810. s->usbsts = USBSTS_HALT;
  811. s->usbsts_pending = 0;
  812. s->usbsts_frindex = 0;
  813. s->astate = EST_INACTIVE;
  814. s->pstate = EST_INACTIVE;
  815. for(i = 0; i < NB_PORTS; i++) {
  816. if (s->companion_ports[i]) {
  817. s->portsc[i] = PORTSC_POWNER | PORTSC_PPOWER;
  818. } else {
  819. s->portsc[i] = PORTSC_PPOWER;
  820. }
  821. if (devs[i] && devs[i]->attached) {
  822. usb_attach(&s->ports[i]);
  823. usb_device_reset(devs[i]);
  824. }
  825. }
  826. ehci_queues_rip_all(s, 0);
  827. ehci_queues_rip_all(s, 1);
  828. qemu_del_timer(s->frame_timer);
  829. qemu_bh_cancel(s->async_bh);
  830. }
  831. static uint64_t ehci_caps_read(void *ptr, hwaddr addr,
  832. unsigned size)
  833. {
  834. EHCIState *s = ptr;
  835. return s->caps[addr];
  836. }
  837. static uint64_t ehci_opreg_read(void *ptr, hwaddr addr,
  838. unsigned size)
  839. {
  840. EHCIState *s = ptr;
  841. uint32_t val;
  842. switch (addr) {
  843. case FRINDEX:
  844. /* Round down to mult of 8, else it can go backwards on migration */
  845. val = s->frindex & ~7;
  846. break;
  847. default:
  848. val = s->opreg[addr >> 2];
  849. }
  850. trace_usb_ehci_opreg_read(addr + s->opregbase, addr2str(addr), val);
  851. return val;
  852. }
  853. static uint64_t ehci_port_read(void *ptr, hwaddr addr,
  854. unsigned size)
  855. {
  856. EHCIState *s = ptr;
  857. uint32_t val;
  858. val = s->portsc[addr >> 2];
  859. trace_usb_ehci_portsc_read(addr + PORTSC_BEGIN, addr >> 2, val);
  860. return val;
  861. }
  862. static void handle_port_owner_write(EHCIState *s, int port, uint32_t owner)
  863. {
  864. USBDevice *dev = s->ports[port].dev;
  865. uint32_t *portsc = &s->portsc[port];
  866. uint32_t orig;
  867. if (s->companion_ports[port] == NULL)
  868. return;
  869. owner = owner & PORTSC_POWNER;
  870. orig = *portsc & PORTSC_POWNER;
  871. if (!(owner ^ orig)) {
  872. return;
  873. }
  874. if (dev && dev->attached) {
  875. usb_detach(&s->ports[port]);
  876. }
  877. *portsc &= ~PORTSC_POWNER;
  878. *portsc |= owner;
  879. if (dev && dev->attached) {
  880. usb_attach(&s->ports[port]);
  881. }
  882. }
  883. static void ehci_port_write(void *ptr, hwaddr addr,
  884. uint64_t val, unsigned size)
  885. {
  886. EHCIState *s = ptr;
  887. int port = addr >> 2;
  888. uint32_t *portsc = &s->portsc[port];
  889. uint32_t old = *portsc;
  890. USBDevice *dev = s->ports[port].dev;
  891. trace_usb_ehci_portsc_write(addr + PORTSC_BEGIN, addr >> 2, val);
  892. /* Clear rwc bits */
  893. *portsc &= ~(val & PORTSC_RWC_MASK);
  894. /* The guest may clear, but not set the PED bit */
  895. *portsc &= val | ~PORTSC_PED;
  896. /* POWNER is masked out by RO_MASK as it is RO when we've no companion */
  897. handle_port_owner_write(s, port, val);
  898. /* And finally apply RO_MASK */
  899. val &= PORTSC_RO_MASK;
  900. if ((val & PORTSC_PRESET) && !(*portsc & PORTSC_PRESET)) {
  901. trace_usb_ehci_port_reset(port, 1);
  902. }
  903. if (!(val & PORTSC_PRESET) &&(*portsc & PORTSC_PRESET)) {
  904. trace_usb_ehci_port_reset(port, 0);
  905. if (dev && dev->attached) {
  906. usb_port_reset(&s->ports[port]);
  907. *portsc &= ~PORTSC_CSC;
  908. }
  909. /*
  910. * Table 2.16 Set the enable bit(and enable bit change) to indicate
  911. * to SW that this port has a high speed device attached
  912. */
  913. if (dev && dev->attached && (dev->speedmask & USB_SPEED_MASK_HIGH)) {
  914. val |= PORTSC_PED;
  915. }
  916. }
  917. *portsc &= ~PORTSC_RO_MASK;
  918. *portsc |= val;
  919. trace_usb_ehci_portsc_change(addr + PORTSC_BEGIN, addr >> 2, *portsc, old);
  920. }
  921. static void ehci_opreg_write(void *ptr, hwaddr addr,
  922. uint64_t val, unsigned size)
  923. {
  924. EHCIState *s = ptr;
  925. uint32_t *mmio = s->opreg + (addr >> 2);
  926. uint32_t old = *mmio;
  927. int i;
  928. trace_usb_ehci_opreg_write(addr + s->opregbase, addr2str(addr), val);
  929. switch (addr) {
  930. case USBCMD:
  931. if (val & USBCMD_HCRESET) {
  932. ehci_reset(s);
  933. val = s->usbcmd;
  934. break;
  935. }
  936. /* not supporting dynamic frame list size at the moment */
  937. if ((val & USBCMD_FLS) && !(s->usbcmd & USBCMD_FLS)) {
  938. fprintf(stderr, "attempt to set frame list size -- value %d\n",
  939. (int)val & USBCMD_FLS);
  940. val &= ~USBCMD_FLS;
  941. }
  942. if (val & USBCMD_IAAD) {
  943. /*
  944. * Process IAAD immediately, otherwise the Linux IAAD watchdog may
  945. * trigger and re-use a qh without us seeing the unlink.
  946. */
  947. s->async_stepdown = 0;
  948. qemu_bh_schedule(s->async_bh);
  949. trace_usb_ehci_doorbell_ring();
  950. }
  951. if (((USBCMD_RUNSTOP | USBCMD_PSE | USBCMD_ASE) & val) !=
  952. ((USBCMD_RUNSTOP | USBCMD_PSE | USBCMD_ASE) & s->usbcmd)) {
  953. if (s->pstate == EST_INACTIVE) {
  954. SET_LAST_RUN_CLOCK(s);
  955. }
  956. s->usbcmd = val; /* Set usbcmd for ehci_update_halt() */
  957. ehci_update_halt(s);
  958. s->async_stepdown = 0;
  959. qemu_bh_schedule(s->async_bh);
  960. }
  961. break;
  962. case USBSTS:
  963. val &= USBSTS_RO_MASK; // bits 6 through 31 are RO
  964. ehci_clear_usbsts(s, val); // bits 0 through 5 are R/WC
  965. val = s->usbsts;
  966. ehci_update_irq(s);
  967. break;
  968. case USBINTR:
  969. val &= USBINTR_MASK;
  970. if (ehci_enabled(s) && (USBSTS_FLR & val)) {
  971. qemu_bh_schedule(s->async_bh);
  972. }
  973. break;
  974. case FRINDEX:
  975. val &= 0x00003fff; /* frindex is 14bits */
  976. s->usbsts_frindex = val;
  977. break;
  978. case CONFIGFLAG:
  979. val &= 0x1;
  980. if (val) {
  981. for(i = 0; i < NB_PORTS; i++)
  982. handle_port_owner_write(s, i, 0);
  983. }
  984. break;
  985. case PERIODICLISTBASE:
  986. if (ehci_periodic_enabled(s)) {
  987. fprintf(stderr,
  988. "ehci: PERIODIC list base register set while periodic schedule\n"
  989. " is enabled and HC is enabled\n");
  990. }
  991. break;
  992. case ASYNCLISTADDR:
  993. if (ehci_async_enabled(s)) {
  994. fprintf(stderr,
  995. "ehci: ASYNC list address register set while async schedule\n"
  996. " is enabled and HC is enabled\n");
  997. }
  998. break;
  999. }
  1000. *mmio = val;
  1001. trace_usb_ehci_opreg_change(addr + s->opregbase, addr2str(addr),
  1002. *mmio, old);
  1003. }
  1004. /*
  1005. * Write the qh back to guest physical memory. This step isn't
  1006. * in the EHCI spec but we need to do it since we don't share
  1007. * physical memory with our guest VM.
  1008. *
  1009. * The first three dwords are read-only for the EHCI, so skip them
  1010. * when writing back the qh.
  1011. */
  1012. static void ehci_flush_qh(EHCIQueue *q)
  1013. {
  1014. uint32_t *qh = (uint32_t *) &q->qh;
  1015. uint32_t dwords = sizeof(EHCIqh) >> 2;
  1016. uint32_t addr = NLPTR_GET(q->qhaddr);
  1017. put_dwords(q->ehci, addr + 3 * sizeof(uint32_t), qh + 3, dwords - 3);
  1018. }
  1019. // 4.10.2
  1020. static int ehci_qh_do_overlay(EHCIQueue *q)
  1021. {
  1022. EHCIPacket *p = QTAILQ_FIRST(&q->packets);
  1023. int i;
  1024. int dtoggle;
  1025. int ping;
  1026. int eps;
  1027. int reload;
  1028. assert(p != NULL);
  1029. assert(p->qtdaddr == q->qtdaddr);
  1030. // remember values in fields to preserve in qh after overlay
  1031. dtoggle = q->qh.token & QTD_TOKEN_DTOGGLE;
  1032. ping = q->qh.token & QTD_TOKEN_PING;
  1033. q->qh.current_qtd = p->qtdaddr;
  1034. q->qh.next_qtd = p->qtd.next;
  1035. q->qh.altnext_qtd = p->qtd.altnext;
  1036. q->qh.token = p->qtd.token;
  1037. eps = get_field(q->qh.epchar, QH_EPCHAR_EPS);
  1038. if (eps == EHCI_QH_EPS_HIGH) {
  1039. q->qh.token &= ~QTD_TOKEN_PING;
  1040. q->qh.token |= ping;
  1041. }
  1042. reload = get_field(q->qh.epchar, QH_EPCHAR_RL);
  1043. set_field(&q->qh.altnext_qtd, reload, QH_ALTNEXT_NAKCNT);
  1044. for (i = 0; i < 5; i++) {
  1045. q->qh.bufptr[i] = p->qtd.bufptr[i];
  1046. }
  1047. if (!(q->qh.epchar & QH_EPCHAR_DTC)) {
  1048. // preserve QH DT bit
  1049. q->qh.token &= ~QTD_TOKEN_DTOGGLE;
  1050. q->qh.token |= dtoggle;
  1051. }
  1052. q->qh.bufptr[1] &= ~BUFPTR_CPROGMASK_MASK;
  1053. q->qh.bufptr[2] &= ~BUFPTR_FRAMETAG_MASK;
  1054. ehci_flush_qh(q);
  1055. return 0;
  1056. }
  1057. static int ehci_init_transfer(EHCIPacket *p)
  1058. {
  1059. uint32_t cpage, offset, bytes, plen;
  1060. dma_addr_t page;
  1061. cpage = get_field(p->qtd.token, QTD_TOKEN_CPAGE);
  1062. bytes = get_field(p->qtd.token, QTD_TOKEN_TBYTES);
  1063. offset = p->qtd.bufptr[0] & ~QTD_BUFPTR_MASK;
  1064. qemu_sglist_init(&p->sgl, 5, p->queue->ehci->dma);
  1065. while (bytes > 0) {
  1066. if (cpage > 4) {
  1067. fprintf(stderr, "cpage out of range (%d)\n", cpage);
  1068. return -1;
  1069. }
  1070. page = p->qtd.bufptr[cpage] & QTD_BUFPTR_MASK;
  1071. page += offset;
  1072. plen = bytes;
  1073. if (plen > 4096 - offset) {
  1074. plen = 4096 - offset;
  1075. offset = 0;
  1076. cpage++;
  1077. }
  1078. qemu_sglist_add(&p->sgl, page, plen);
  1079. bytes -= plen;
  1080. }
  1081. return 0;
  1082. }
  1083. static void ehci_finish_transfer(EHCIQueue *q, int len)
  1084. {
  1085. uint32_t cpage, offset;
  1086. if (len > 0) {
  1087. /* update cpage & offset */
  1088. cpage = get_field(q->qh.token, QTD_TOKEN_CPAGE);
  1089. offset = q->qh.bufptr[0] & ~QTD_BUFPTR_MASK;
  1090. offset += len;
  1091. cpage += offset >> QTD_BUFPTR_SH;
  1092. offset &= ~QTD_BUFPTR_MASK;
  1093. set_field(&q->qh.token, cpage, QTD_TOKEN_CPAGE);
  1094. q->qh.bufptr[0] &= QTD_BUFPTR_MASK;
  1095. q->qh.bufptr[0] |= offset;
  1096. }
  1097. }
  1098. static void ehci_async_complete_packet(USBPort *port, USBPacket *packet)
  1099. {
  1100. EHCIPacket *p;
  1101. EHCIState *s = port->opaque;
  1102. uint32_t portsc = s->portsc[port->index];
  1103. if (portsc & PORTSC_POWNER) {
  1104. USBPort *companion = s->companion_ports[port->index];
  1105. companion->ops->complete(companion, packet);
  1106. return;
  1107. }
  1108. p = container_of(packet, EHCIPacket, packet);
  1109. assert(p->async == EHCI_ASYNC_INFLIGHT);
  1110. if (packet->status == USB_RET_REMOVE_FROM_QUEUE) {
  1111. trace_usb_ehci_packet_action(p->queue, p, "remove");
  1112. ehci_free_packet(p);
  1113. return;
  1114. }
  1115. trace_usb_ehci_packet_action(p->queue, p, "wakeup");
  1116. p->async = EHCI_ASYNC_FINISHED;
  1117. if (!p->queue->async) {
  1118. s->periodic_sched_active = PERIODIC_ACTIVE;
  1119. }
  1120. qemu_bh_schedule(s->async_bh);
  1121. }
  1122. static void ehci_execute_complete(EHCIQueue *q)
  1123. {
  1124. EHCIPacket *p = QTAILQ_FIRST(&q->packets);
  1125. uint32_t tbytes;
  1126. assert(p != NULL);
  1127. assert(p->qtdaddr == q->qtdaddr);
  1128. assert(p->async == EHCI_ASYNC_INITIALIZED ||
  1129. p->async == EHCI_ASYNC_FINISHED);
  1130. DPRINTF("execute_complete: qhaddr 0x%x, next 0x%x, qtdaddr 0x%x, "
  1131. "status %d, actual_length %d\n",
  1132. q->qhaddr, q->qh.next, q->qtdaddr,
  1133. p->packet.status, p->packet.actual_length);
  1134. switch (p->packet.status) {
  1135. case USB_RET_SUCCESS:
  1136. break;
  1137. case USB_RET_IOERROR:
  1138. case USB_RET_NODEV:
  1139. q->qh.token |= (QTD_TOKEN_HALT | QTD_TOKEN_XACTERR);
  1140. set_field(&q->qh.token, 0, QTD_TOKEN_CERR);
  1141. ehci_raise_irq(q->ehci, USBSTS_ERRINT);
  1142. break;
  1143. case USB_RET_STALL:
  1144. q->qh.token |= QTD_TOKEN_HALT;
  1145. ehci_raise_irq(q->ehci, USBSTS_ERRINT);
  1146. break;
  1147. case USB_RET_NAK:
  1148. set_field(&q->qh.altnext_qtd, 0, QH_ALTNEXT_NAKCNT);
  1149. return; /* We're not done yet with this transaction */
  1150. case USB_RET_BABBLE:
  1151. q->qh.token |= (QTD_TOKEN_HALT | QTD_TOKEN_BABBLE);
  1152. ehci_raise_irq(q->ehci, USBSTS_ERRINT);
  1153. break;
  1154. default:
  1155. /* should not be triggerable */
  1156. fprintf(stderr, "USB invalid response %d\n", p->packet.status);
  1157. assert(0);
  1158. break;
  1159. }
  1160. /* TODO check 4.12 for splits */
  1161. tbytes = get_field(q->qh.token, QTD_TOKEN_TBYTES);
  1162. if (tbytes && p->pid == USB_TOKEN_IN) {
  1163. tbytes -= p->packet.actual_length;
  1164. if (tbytes) {
  1165. /* 4.15.1.2 must raise int on a short input packet */
  1166. ehci_raise_irq(q->ehci, USBSTS_INT);
  1167. if (q->async) {
  1168. q->ehci->int_req_by_async = true;
  1169. }
  1170. }
  1171. } else {
  1172. tbytes = 0;
  1173. }
  1174. DPRINTF("updating tbytes to %d\n", tbytes);
  1175. set_field(&q->qh.token, tbytes, QTD_TOKEN_TBYTES);
  1176. ehci_finish_transfer(q, p->packet.actual_length);
  1177. usb_packet_unmap(&p->packet, &p->sgl);
  1178. qemu_sglist_destroy(&p->sgl);
  1179. p->async = EHCI_ASYNC_NONE;
  1180. q->qh.token ^= QTD_TOKEN_DTOGGLE;
  1181. q->qh.token &= ~QTD_TOKEN_ACTIVE;
  1182. if (q->qh.token & QTD_TOKEN_IOC) {
  1183. ehci_raise_irq(q->ehci, USBSTS_INT);
  1184. if (q->async) {
  1185. q->ehci->int_req_by_async = true;
  1186. }
  1187. }
  1188. }
  1189. /* 4.10.3 returns "again" */
  1190. static int ehci_execute(EHCIPacket *p, const char *action)
  1191. {
  1192. USBEndpoint *ep;
  1193. int endp;
  1194. bool spd;
  1195. assert(p->async == EHCI_ASYNC_NONE ||
  1196. p->async == EHCI_ASYNC_INITIALIZED);
  1197. if (!(p->qtd.token & QTD_TOKEN_ACTIVE)) {
  1198. fprintf(stderr, "Attempting to execute inactive qtd\n");
  1199. return -1;
  1200. }
  1201. if (get_field(p->qtd.token, QTD_TOKEN_TBYTES) > BUFF_SIZE) {
  1202. ehci_trace_guest_bug(p->queue->ehci,
  1203. "guest requested more bytes than allowed");
  1204. return -1;
  1205. }
  1206. if (!ehci_verify_pid(p->queue, &p->qtd)) {
  1207. ehci_queue_stopped(p->queue); /* Mark the ep in the prev dir stopped */
  1208. }
  1209. p->pid = ehci_get_pid(&p->qtd);
  1210. p->queue->last_pid = p->pid;
  1211. endp = get_field(p->queue->qh.epchar, QH_EPCHAR_EP);
  1212. ep = usb_ep_get(p->queue->dev, p->pid, endp);
  1213. if (p->async == EHCI_ASYNC_NONE) {
  1214. if (ehci_init_transfer(p) != 0) {
  1215. return -1;
  1216. }
  1217. spd = (p->pid == USB_TOKEN_IN && NLPTR_TBIT(p->qtd.altnext) == 0);
  1218. usb_packet_setup(&p->packet, p->pid, ep, p->qtdaddr, spd,
  1219. (p->qtd.token & QTD_TOKEN_IOC) != 0);
  1220. usb_packet_map(&p->packet, &p->sgl);
  1221. p->async = EHCI_ASYNC_INITIALIZED;
  1222. }
  1223. trace_usb_ehci_packet_action(p->queue, p, action);
  1224. usb_handle_packet(p->queue->dev, &p->packet);
  1225. DPRINTF("submit: qh 0x%x next 0x%x qtd 0x%x pid 0x%x len %zd endp 0x%x "
  1226. "status %d actual_length %d\n", p->queue->qhaddr, p->qtd.next,
  1227. p->qtdaddr, p->pid, p->packet.iov.size, endp, p->packet.status,
  1228. p->packet.actual_length);
  1229. if (p->packet.actual_length > BUFF_SIZE) {
  1230. fprintf(stderr, "ret from usb_handle_packet > BUFF_SIZE\n");
  1231. return -1;
  1232. }
  1233. return 1;
  1234. }
  1235. /* 4.7.2
  1236. */
  1237. static int ehci_process_itd(EHCIState *ehci,
  1238. EHCIitd *itd,
  1239. uint32_t addr)
  1240. {
  1241. USBDevice *dev;
  1242. USBEndpoint *ep;
  1243. uint32_t i, len, pid, dir, devaddr, endp;
  1244. uint32_t pg, off, ptr1, ptr2, max, mult;
  1245. ehci->periodic_sched_active = PERIODIC_ACTIVE;
  1246. dir =(itd->bufptr[1] & ITD_BUFPTR_DIRECTION);
  1247. devaddr = get_field(itd->bufptr[0], ITD_BUFPTR_DEVADDR);
  1248. endp = get_field(itd->bufptr[0], ITD_BUFPTR_EP);
  1249. max = get_field(itd->bufptr[1], ITD_BUFPTR_MAXPKT);
  1250. mult = get_field(itd->bufptr[2], ITD_BUFPTR_MULT);
  1251. for(i = 0; i < 8; i++) {
  1252. if (itd->transact[i] & ITD_XACT_ACTIVE) {
  1253. pg = get_field(itd->transact[i], ITD_XACT_PGSEL);
  1254. off = itd->transact[i] & ITD_XACT_OFFSET_MASK;
  1255. ptr1 = (itd->bufptr[pg] & ITD_BUFPTR_MASK);
  1256. ptr2 = (itd->bufptr[pg+1] & ITD_BUFPTR_MASK);
  1257. len = get_field(itd->transact[i], ITD_XACT_LENGTH);
  1258. if (len > max * mult) {
  1259. len = max * mult;
  1260. }
  1261. if (len > BUFF_SIZE) {
  1262. return -1;
  1263. }
  1264. qemu_sglist_init(&ehci->isgl, 2, ehci->dma);
  1265. if (off + len > 4096) {
  1266. /* transfer crosses page border */
  1267. uint32_t len2 = off + len - 4096;
  1268. uint32_t len1 = len - len2;
  1269. qemu_sglist_add(&ehci->isgl, ptr1 + off, len1);
  1270. qemu_sglist_add(&ehci->isgl, ptr2, len2);
  1271. } else {
  1272. qemu_sglist_add(&ehci->isgl, ptr1 + off, len);
  1273. }
  1274. pid = dir ? USB_TOKEN_IN : USB_TOKEN_OUT;
  1275. dev = ehci_find_device(ehci, devaddr);
  1276. ep = usb_ep_get(dev, pid, endp);
  1277. if (ep && ep->type == USB_ENDPOINT_XFER_ISOC) {
  1278. usb_packet_setup(&ehci->ipacket, pid, ep, addr, false,
  1279. (itd->transact[i] & ITD_XACT_IOC) != 0);
  1280. usb_packet_map(&ehci->ipacket, &ehci->isgl);
  1281. usb_handle_packet(dev, &ehci->ipacket);
  1282. usb_packet_unmap(&ehci->ipacket, &ehci->isgl);
  1283. } else {
  1284. DPRINTF("ISOCH: attempt to addess non-iso endpoint\n");
  1285. ehci->ipacket.status = USB_RET_NAK;
  1286. ehci->ipacket.actual_length = 0;
  1287. }
  1288. qemu_sglist_destroy(&ehci->isgl);
  1289. switch (ehci->ipacket.status) {
  1290. case USB_RET_SUCCESS:
  1291. break;
  1292. default:
  1293. fprintf(stderr, "Unexpected iso usb result: %d\n",
  1294. ehci->ipacket.status);
  1295. /* Fall through */
  1296. case USB_RET_IOERROR:
  1297. case USB_RET_NODEV:
  1298. /* 3.3.2: XACTERR is only allowed on IN transactions */
  1299. if (dir) {
  1300. itd->transact[i] |= ITD_XACT_XACTERR;
  1301. ehci_raise_irq(ehci, USBSTS_ERRINT);
  1302. }
  1303. break;
  1304. case USB_RET_BABBLE:
  1305. itd->transact[i] |= ITD_XACT_BABBLE;
  1306. ehci_raise_irq(ehci, USBSTS_ERRINT);
  1307. break;
  1308. case USB_RET_NAK:
  1309. /* no data for us, so do a zero-length transfer */
  1310. ehci->ipacket.actual_length = 0;
  1311. break;
  1312. }
  1313. if (!dir) {
  1314. set_field(&itd->transact[i], len - ehci->ipacket.actual_length,
  1315. ITD_XACT_LENGTH); /* OUT */
  1316. } else {
  1317. set_field(&itd->transact[i], ehci->ipacket.actual_length,
  1318. ITD_XACT_LENGTH); /* IN */
  1319. }
  1320. if (itd->transact[i] & ITD_XACT_IOC) {
  1321. ehci_raise_irq(ehci, USBSTS_INT);
  1322. }
  1323. itd->transact[i] &= ~ITD_XACT_ACTIVE;
  1324. }
  1325. }
  1326. return 0;
  1327. }
  1328. /* This state is the entry point for asynchronous schedule
  1329. * processing. Entry here consitutes a EHCI start event state (4.8.5)
  1330. */
  1331. static int ehci_state_waitlisthead(EHCIState *ehci, int async)
  1332. {
  1333. EHCIqh qh;
  1334. int i = 0;
  1335. int again = 0;
  1336. uint32_t entry = ehci->asynclistaddr;
  1337. /* set reclamation flag at start event (4.8.6) */
  1338. if (async) {
  1339. ehci_set_usbsts(ehci, USBSTS_REC);
  1340. }
  1341. ehci_queues_rip_unused(ehci, async);
  1342. /* Find the head of the list (4.9.1.1) */
  1343. for(i = 0; i < MAX_QH; i++) {
  1344. if (get_dwords(ehci, NLPTR_GET(entry), (uint32_t *) &qh,
  1345. sizeof(EHCIqh) >> 2) < 0) {
  1346. return 0;
  1347. }
  1348. ehci_trace_qh(NULL, NLPTR_GET(entry), &qh);
  1349. if (qh.epchar & QH_EPCHAR_H) {
  1350. if (async) {
  1351. entry |= (NLPTR_TYPE_QH << 1);
  1352. }
  1353. ehci_set_fetch_addr(ehci, async, entry);
  1354. ehci_set_state(ehci, async, EST_FETCHENTRY);
  1355. again = 1;
  1356. goto out;
  1357. }
  1358. entry = qh.next;
  1359. if (entry == ehci->asynclistaddr) {
  1360. break;
  1361. }
  1362. }
  1363. /* no head found for list. */
  1364. ehci_set_state(ehci, async, EST_ACTIVE);
  1365. out:
  1366. return again;
  1367. }
  1368. /* This state is the entry point for periodic schedule processing as
  1369. * well as being a continuation state for async processing.
  1370. */
  1371. static int ehci_state_fetchentry(EHCIState *ehci, int async)
  1372. {
  1373. int again = 0;
  1374. uint32_t entry = ehci_get_fetch_addr(ehci, async);
  1375. if (NLPTR_TBIT(entry)) {
  1376. ehci_set_state(ehci, async, EST_ACTIVE);
  1377. goto out;
  1378. }
  1379. /* section 4.8, only QH in async schedule */
  1380. if (async && (NLPTR_TYPE_GET(entry) != NLPTR_TYPE_QH)) {
  1381. fprintf(stderr, "non queue head request in async schedule\n");
  1382. return -1;
  1383. }
  1384. switch (NLPTR_TYPE_GET(entry)) {
  1385. case NLPTR_TYPE_QH:
  1386. ehci_set_state(ehci, async, EST_FETCHQH);
  1387. again = 1;
  1388. break;
  1389. case NLPTR_TYPE_ITD:
  1390. ehci_set_state(ehci, async, EST_FETCHITD);
  1391. again = 1;
  1392. break;
  1393. case NLPTR_TYPE_STITD:
  1394. ehci_set_state(ehci, async, EST_FETCHSITD);
  1395. again = 1;
  1396. break;
  1397. default:
  1398. /* TODO: handle FSTN type */
  1399. fprintf(stderr, "FETCHENTRY: entry at %X is of type %d "
  1400. "which is not supported yet\n", entry, NLPTR_TYPE_GET(entry));
  1401. return -1;
  1402. }
  1403. out:
  1404. return again;
  1405. }
  1406. static EHCIQueue *ehci_state_fetchqh(EHCIState *ehci, int async)
  1407. {
  1408. uint32_t entry;
  1409. EHCIQueue *q;
  1410. EHCIqh qh;
  1411. entry = ehci_get_fetch_addr(ehci, async);
  1412. q = ehci_find_queue_by_qh(ehci, entry, async);
  1413. if (NULL == q) {
  1414. q = ehci_alloc_queue(ehci, entry, async);
  1415. }
  1416. q->seen++;
  1417. if (q->seen > 1) {
  1418. /* we are going in circles -- stop processing */
  1419. ehci_set_state(ehci, async, EST_ACTIVE);
  1420. q = NULL;
  1421. goto out;
  1422. }
  1423. if (get_dwords(ehci, NLPTR_GET(q->qhaddr),
  1424. (uint32_t *) &qh, sizeof(EHCIqh) >> 2) < 0) {
  1425. q = NULL;
  1426. goto out;
  1427. }
  1428. ehci_trace_qh(q, NLPTR_GET(q->qhaddr), &qh);
  1429. /*
  1430. * The overlay area of the qh should never be changed by the guest,
  1431. * except when idle, in which case the reset is a nop.
  1432. */
  1433. if (!ehci_verify_qh(q, &qh)) {
  1434. if (ehci_reset_queue(q) > 0) {
  1435. ehci_trace_guest_bug(ehci, "guest updated active QH");
  1436. }
  1437. }
  1438. q->qh = qh;
  1439. q->transact_ctr = get_field(q->qh.epcap, QH_EPCAP_MULT);
  1440. if (q->transact_ctr == 0) { /* Guest bug in some versions of windows */
  1441. q->transact_ctr = 4;
  1442. }
  1443. if (q->dev == NULL) {
  1444. q->dev = ehci_find_device(q->ehci,
  1445. get_field(q->qh.epchar, QH_EPCHAR_DEVADDR));
  1446. }
  1447. if (async && (q->qh.epchar & QH_EPCHAR_H)) {
  1448. /* EHCI spec version 1.0 Section 4.8.3 & 4.10.1 */
  1449. if (ehci->usbsts & USBSTS_REC) {
  1450. ehci_clear_usbsts(ehci, USBSTS_REC);
  1451. } else {
  1452. DPRINTF("FETCHQH: QH 0x%08x. H-bit set, reclamation status reset"
  1453. " - done processing\n", q->qhaddr);
  1454. ehci_set_state(ehci, async, EST_ACTIVE);
  1455. q = NULL;
  1456. goto out;
  1457. }
  1458. }
  1459. #if EHCI_DEBUG
  1460. if (q->qhaddr != q->qh.next) {
  1461. DPRINTF("FETCHQH: QH 0x%08x (h %x halt %x active %x) next 0x%08x\n",
  1462. q->qhaddr,
  1463. q->qh.epchar & QH_EPCHAR_H,
  1464. q->qh.token & QTD_TOKEN_HALT,
  1465. q->qh.token & QTD_TOKEN_ACTIVE,
  1466. q->qh.next);
  1467. }
  1468. #endif
  1469. if (q->qh.token & QTD_TOKEN_HALT) {
  1470. ehci_set_state(ehci, async, EST_HORIZONTALQH);
  1471. } else if ((q->qh.token & QTD_TOKEN_ACTIVE) &&
  1472. (NLPTR_TBIT(q->qh.current_qtd) == 0)) {
  1473. q->qtdaddr = q->qh.current_qtd;
  1474. ehci_set_state(ehci, async, EST_FETCHQTD);
  1475. } else {
  1476. /* EHCI spec version 1.0 Section 4.10.2 */
  1477. ehci_set_state(ehci, async, EST_ADVANCEQUEUE);
  1478. }
  1479. out:
  1480. return q;
  1481. }
  1482. static int ehci_state_fetchitd(EHCIState *ehci, int async)
  1483. {
  1484. uint32_t entry;
  1485. EHCIitd itd;
  1486. assert(!async);
  1487. entry = ehci_get_fetch_addr(ehci, async);
  1488. if (get_dwords(ehci, NLPTR_GET(entry), (uint32_t *) &itd,
  1489. sizeof(EHCIitd) >> 2) < 0) {
  1490. return -1;
  1491. }
  1492. ehci_trace_itd(ehci, entry, &itd);
  1493. if (ehci_process_itd(ehci, &itd, entry) != 0) {
  1494. return -1;
  1495. }
  1496. put_dwords(ehci, NLPTR_GET(entry), (uint32_t *) &itd,
  1497. sizeof(EHCIitd) >> 2);
  1498. ehci_set_fetch_addr(ehci, async, itd.next);
  1499. ehci_set_state(ehci, async, EST_FETCHENTRY);
  1500. return 1;
  1501. }
  1502. static int ehci_state_fetchsitd(EHCIState *ehci, int async)
  1503. {
  1504. uint32_t entry;
  1505. EHCIsitd sitd;
  1506. assert(!async);
  1507. entry = ehci_get_fetch_addr(ehci, async);
  1508. if (get_dwords(ehci, NLPTR_GET(entry), (uint32_t *)&sitd,
  1509. sizeof(EHCIsitd) >> 2) < 0) {
  1510. return 0;
  1511. }
  1512. ehci_trace_sitd(ehci, entry, &sitd);
  1513. if (!(sitd.results & SITD_RESULTS_ACTIVE)) {
  1514. /* siTD is not active, nothing to do */;
  1515. } else {
  1516. /* TODO: split transfers are not implemented */
  1517. fprintf(stderr, "WARNING: Skipping active siTD\n");
  1518. }
  1519. ehci_set_fetch_addr(ehci, async, sitd.next);
  1520. ehci_set_state(ehci, async, EST_FETCHENTRY);
  1521. return 1;
  1522. }
  1523. /* Section 4.10.2 - paragraph 3 */
  1524. static int ehci_state_advqueue(EHCIQueue *q)
  1525. {
  1526. #if 0
  1527. /* TO-DO: 4.10.2 - paragraph 2
  1528. * if I-bit is set to 1 and QH is not active
  1529. * go to horizontal QH
  1530. */
  1531. if (I-bit set) {
  1532. ehci_set_state(ehci, async, EST_HORIZONTALQH);
  1533. goto out;
  1534. }
  1535. #endif
  1536. /*
  1537. * want data and alt-next qTD is valid
  1538. */
  1539. if (((q->qh.token & QTD_TOKEN_TBYTES_MASK) != 0) &&
  1540. (NLPTR_TBIT(q->qh.altnext_qtd) == 0)) {
  1541. q->qtdaddr = q->qh.altnext_qtd;
  1542. ehci_set_state(q->ehci, q->async, EST_FETCHQTD);
  1543. /*
  1544. * next qTD is valid
  1545. */
  1546. } else if (NLPTR_TBIT(q->qh.next_qtd) == 0) {
  1547. q->qtdaddr = q->qh.next_qtd;
  1548. ehci_set_state(q->ehci, q->async, EST_FETCHQTD);
  1549. /*
  1550. * no valid qTD, try next QH
  1551. */
  1552. } else {
  1553. ehci_set_state(q->ehci, q->async, EST_HORIZONTALQH);
  1554. }
  1555. return 1;
  1556. }
  1557. /* Section 4.10.2 - paragraph 4 */
  1558. static int ehci_state_fetchqtd(EHCIQueue *q)
  1559. {
  1560. EHCIqtd qtd;
  1561. EHCIPacket *p;
  1562. int again = 1;
  1563. if (get_dwords(q->ehci, NLPTR_GET(q->qtdaddr), (uint32_t *) &qtd,
  1564. sizeof(EHCIqtd) >> 2) < 0) {
  1565. return 0;
  1566. }
  1567. ehci_trace_qtd(q, NLPTR_GET(q->qtdaddr), &qtd);
  1568. p = QTAILQ_FIRST(&q->packets);
  1569. if (p != NULL) {
  1570. if (!ehci_verify_qtd(p, &qtd)) {
  1571. ehci_cancel_queue(q);
  1572. if (qtd.token & QTD_TOKEN_ACTIVE) {
  1573. ehci_trace_guest_bug(q->ehci, "guest updated active qTD");
  1574. }
  1575. p = NULL;
  1576. } else {
  1577. p->qtd = qtd;
  1578. ehci_qh_do_overlay(q);
  1579. }
  1580. }
  1581. if (!(qtd.token & QTD_TOKEN_ACTIVE)) {
  1582. ehci_set_state(q->ehci, q->async, EST_HORIZONTALQH);
  1583. } else if (p != NULL) {
  1584. switch (p->async) {
  1585. case EHCI_ASYNC_NONE:
  1586. case EHCI_ASYNC_INITIALIZED:
  1587. /* Not yet executed (MULT), or previously nacked (int) packet */
  1588. ehci_set_state(q->ehci, q->async, EST_EXECUTE);
  1589. break;
  1590. case EHCI_ASYNC_INFLIGHT:
  1591. /* Check if the guest has added new tds to the queue */
  1592. again = ehci_fill_queue(QTAILQ_LAST(&q->packets, pkts_head));
  1593. /* Unfinished async handled packet, go horizontal */
  1594. ehci_set_state(q->ehci, q->async, EST_HORIZONTALQH);
  1595. break;
  1596. case EHCI_ASYNC_FINISHED:
  1597. /* Complete executing of the packet */
  1598. ehci_set_state(q->ehci, q->async, EST_EXECUTING);
  1599. break;
  1600. }
  1601. } else {
  1602. p = ehci_alloc_packet(q);
  1603. p->qtdaddr = q->qtdaddr;
  1604. p->qtd = qtd;
  1605. ehci_set_state(q->ehci, q->async, EST_EXECUTE);
  1606. }
  1607. return again;
  1608. }
  1609. static int ehci_state_horizqh(EHCIQueue *q)
  1610. {
  1611. int again = 0;
  1612. if (ehci_get_fetch_addr(q->ehci, q->async) != q->qh.next) {
  1613. ehci_set_fetch_addr(q->ehci, q->async, q->qh.next);
  1614. ehci_set_state(q->ehci, q->async, EST_FETCHENTRY);
  1615. again = 1;
  1616. } else {
  1617. ehci_set_state(q->ehci, q->async, EST_ACTIVE);
  1618. }
  1619. return again;
  1620. }
  1621. /* Returns "again" */
  1622. static int ehci_fill_queue(EHCIPacket *p)
  1623. {
  1624. USBEndpoint *ep = p->packet.ep;
  1625. EHCIQueue *q = p->queue;
  1626. EHCIqtd qtd = p->qtd;
  1627. uint32_t qtdaddr;
  1628. for (;;) {
  1629. if (NLPTR_TBIT(qtd.next) != 0) {
  1630. break;
  1631. }
  1632. qtdaddr = qtd.next;
  1633. /*
  1634. * Detect circular td lists, Windows creates these, counting on the
  1635. * active bit going low after execution to make the queue stop.
  1636. */
  1637. QTAILQ_FOREACH(p, &q->packets, next) {
  1638. if (p->qtdaddr == qtdaddr) {
  1639. goto leave;
  1640. }
  1641. }
  1642. if (get_dwords(q->ehci, NLPTR_GET(qtdaddr),
  1643. (uint32_t *) &qtd, sizeof(EHCIqtd) >> 2) < 0) {
  1644. return -1;
  1645. }
  1646. ehci_trace_qtd(q, NLPTR_GET(qtdaddr), &qtd);
  1647. if (!(qtd.token & QTD_TOKEN_ACTIVE)) {
  1648. break;
  1649. }
  1650. if (!ehci_verify_pid(q, &qtd)) {
  1651. ehci_trace_guest_bug(q->ehci, "guest queued token with wrong pid");
  1652. break;
  1653. }
  1654. p = ehci_alloc_packet(q);
  1655. p->qtdaddr = qtdaddr;
  1656. p->qtd = qtd;
  1657. if (ehci_execute(p, "queue") == -1) {
  1658. return -1;
  1659. }
  1660. assert(p->packet.status == USB_RET_ASYNC);
  1661. p->async = EHCI_ASYNC_INFLIGHT;
  1662. }
  1663. leave:
  1664. usb_device_flush_ep_queue(ep->dev, ep);
  1665. return 1;
  1666. }
  1667. static int ehci_state_execute(EHCIQueue *q)
  1668. {
  1669. EHCIPacket *p = QTAILQ_FIRST(&q->packets);
  1670. int again = 0;
  1671. assert(p != NULL);
  1672. assert(p->qtdaddr == q->qtdaddr);
  1673. if (ehci_qh_do_overlay(q) != 0) {
  1674. return -1;
  1675. }
  1676. // TODO verify enough time remains in the uframe as in 4.4.1.1
  1677. // TODO write back ptr to async list when done or out of time
  1678. /* 4.10.3, bottom of page 82, go horizontal on transaction counter == 0 */
  1679. if (!q->async && q->transact_ctr == 0) {
  1680. ehci_set_state(q->ehci, q->async, EST_HORIZONTALQH);
  1681. again = 1;
  1682. goto out;
  1683. }
  1684. if (q->async) {
  1685. ehci_set_usbsts(q->ehci, USBSTS_REC);
  1686. }
  1687. again = ehci_execute(p, "process");
  1688. if (again == -1) {
  1689. goto out;
  1690. }
  1691. if (p->packet.status == USB_RET_ASYNC) {
  1692. ehci_flush_qh(q);
  1693. trace_usb_ehci_packet_action(p->queue, p, "async");
  1694. p->async = EHCI_ASYNC_INFLIGHT;
  1695. ehci_set_state(q->ehci, q->async, EST_HORIZONTALQH);
  1696. if (q->async) {
  1697. again = ehci_fill_queue(p);
  1698. } else {
  1699. again = 1;
  1700. }
  1701. goto out;
  1702. }
  1703. ehci_set_state(q->ehci, q->async, EST_EXECUTING);
  1704. again = 1;
  1705. out:
  1706. return again;
  1707. }
  1708. static int ehci_state_executing(EHCIQueue *q)
  1709. {
  1710. EHCIPacket *p = QTAILQ_FIRST(&q->packets);
  1711. assert(p != NULL);
  1712. assert(p->qtdaddr == q->qtdaddr);
  1713. ehci_execute_complete(q);
  1714. /* 4.10.3 */
  1715. if (!q->async && q->transact_ctr > 0) {
  1716. q->transact_ctr--;
  1717. }
  1718. /* 4.10.5 */
  1719. if (p->packet.status == USB_RET_NAK) {
  1720. ehci_set_state(q->ehci, q->async, EST_HORIZONTALQH);
  1721. } else {
  1722. ehci_set_state(q->ehci, q->async, EST_WRITEBACK);
  1723. }
  1724. ehci_flush_qh(q);
  1725. return 1;
  1726. }
  1727. static int ehci_state_writeback(EHCIQueue *q)
  1728. {
  1729. EHCIPacket *p = QTAILQ_FIRST(&q->packets);
  1730. uint32_t *qtd, addr;
  1731. int again = 0;
  1732. /* Write back the QTD from the QH area */
  1733. assert(p != NULL);
  1734. assert(p->qtdaddr == q->qtdaddr);
  1735. ehci_trace_qtd(q, NLPTR_GET(p->qtdaddr), (EHCIqtd *) &q->qh.next_qtd);
  1736. qtd = (uint32_t *) &q->qh.next_qtd;
  1737. addr = NLPTR_GET(p->qtdaddr);
  1738. put_dwords(q->ehci, addr + 2 * sizeof(uint32_t), qtd + 2, 2);
  1739. ehci_free_packet(p);
  1740. /*
  1741. * EHCI specs say go horizontal here.
  1742. *
  1743. * We can also advance the queue here for performance reasons. We
  1744. * need to take care to only take that shortcut in case we've
  1745. * processed the qtd just written back without errors, i.e. halt
  1746. * bit is clear.
  1747. */
  1748. if (q->qh.token & QTD_TOKEN_HALT) {
  1749. ehci_set_state(q->ehci, q->async, EST_HORIZONTALQH);
  1750. again = 1;
  1751. } else {
  1752. ehci_set_state(q->ehci, q->async, EST_ADVANCEQUEUE);
  1753. again = 1;
  1754. }
  1755. return again;
  1756. }
  1757. /*
  1758. * This is the state machine that is common to both async and periodic
  1759. */
  1760. static void ehci_advance_state(EHCIState *ehci, int async)
  1761. {
  1762. EHCIQueue *q = NULL;
  1763. int again;
  1764. do {
  1765. switch(ehci_get_state(ehci, async)) {
  1766. case EST_WAITLISTHEAD:
  1767. again = ehci_state_waitlisthead(ehci, async);
  1768. break;
  1769. case EST_FETCHENTRY:
  1770. again = ehci_state_fetchentry(ehci, async);
  1771. break;
  1772. case EST_FETCHQH:
  1773. q = ehci_state_fetchqh(ehci, async);
  1774. if (q != NULL) {
  1775. assert(q->async == async);
  1776. again = 1;
  1777. } else {
  1778. again = 0;
  1779. }
  1780. break;
  1781. case EST_FETCHITD:
  1782. again = ehci_state_fetchitd(ehci, async);
  1783. break;
  1784. case EST_FETCHSITD:
  1785. again = ehci_state_fetchsitd(ehci, async);
  1786. break;
  1787. case EST_ADVANCEQUEUE:
  1788. assert(q != NULL);
  1789. again = ehci_state_advqueue(q);
  1790. break;
  1791. case EST_FETCHQTD:
  1792. assert(q != NULL);
  1793. again = ehci_state_fetchqtd(q);
  1794. break;
  1795. case EST_HORIZONTALQH:
  1796. assert(q != NULL);
  1797. again = ehci_state_horizqh(q);
  1798. break;
  1799. case EST_EXECUTE:
  1800. assert(q != NULL);
  1801. again = ehci_state_execute(q);
  1802. if (async) {
  1803. ehci->async_stepdown = 0;
  1804. }
  1805. break;
  1806. case EST_EXECUTING:
  1807. assert(q != NULL);
  1808. if (async) {
  1809. ehci->async_stepdown = 0;
  1810. }
  1811. again = ehci_state_executing(q);
  1812. break;
  1813. case EST_WRITEBACK:
  1814. assert(q != NULL);
  1815. again = ehci_state_writeback(q);
  1816. if (!async) {
  1817. ehci->periodic_sched_active = PERIODIC_ACTIVE;
  1818. }
  1819. break;
  1820. default:
  1821. fprintf(stderr, "Bad state!\n");
  1822. again = -1;
  1823. assert(0);
  1824. break;
  1825. }
  1826. if (again < 0) {
  1827. fprintf(stderr, "processing error - resetting ehci HC\n");
  1828. ehci_reset(ehci);
  1829. again = 0;
  1830. }
  1831. }
  1832. while (again);
  1833. }
  1834. static void ehci_advance_async_state(EHCIState *ehci)
  1835. {
  1836. const int async = 1;
  1837. switch(ehci_get_state(ehci, async)) {
  1838. case EST_INACTIVE:
  1839. if (!ehci_async_enabled(ehci)) {
  1840. break;
  1841. }
  1842. ehci_set_state(ehci, async, EST_ACTIVE);
  1843. // No break, fall through to ACTIVE
  1844. case EST_ACTIVE:
  1845. if (!ehci_async_enabled(ehci)) {
  1846. ehci_queues_rip_all(ehci, async);
  1847. ehci_set_state(ehci, async, EST_INACTIVE);
  1848. break;
  1849. }
  1850. /* make sure guest has acknowledged the doorbell interrupt */
  1851. /* TO-DO: is this really needed? */
  1852. if (ehci->usbsts & USBSTS_IAA) {
  1853. DPRINTF("IAA status bit still set.\n");
  1854. break;
  1855. }
  1856. /* check that address register has been set */
  1857. if (ehci->asynclistaddr == 0) {
  1858. break;
  1859. }
  1860. ehci_set_state(ehci, async, EST_WAITLISTHEAD);
  1861. ehci_advance_state(ehci, async);
  1862. /* If the doorbell is set, the guest wants to make a change to the
  1863. * schedule. The host controller needs to release cached data.
  1864. * (section 4.8.2)
  1865. */
  1866. if (ehci->usbcmd & USBCMD_IAAD) {
  1867. /* Remove all unseen qhs from the async qhs queue */
  1868. ehci_queues_rip_unseen(ehci, async);
  1869. trace_usb_ehci_doorbell_ack();
  1870. ehci->usbcmd &= ~USBCMD_IAAD;
  1871. ehci_raise_irq(ehci, USBSTS_IAA);
  1872. }
  1873. break;
  1874. default:
  1875. /* this should only be due to a developer mistake */
  1876. fprintf(stderr, "ehci: Bad asynchronous state %d. "
  1877. "Resetting to active\n", ehci->astate);
  1878. assert(0);
  1879. }
  1880. }
  1881. static void ehci_advance_periodic_state(EHCIState *ehci)
  1882. {
  1883. uint32_t entry;
  1884. uint32_t list;
  1885. const int async = 0;
  1886. // 4.6
  1887. switch(ehci_get_state(ehci, async)) {
  1888. case EST_INACTIVE:
  1889. if (!(ehci->frindex & 7) && ehci_periodic_enabled(ehci)) {
  1890. ehci_set_state(ehci, async, EST_ACTIVE);
  1891. // No break, fall through to ACTIVE
  1892. } else
  1893. break;
  1894. case EST_ACTIVE:
  1895. if (!(ehci->frindex & 7) && !ehci_periodic_enabled(ehci)) {
  1896. ehci_queues_rip_all(ehci, async);
  1897. ehci_set_state(ehci, async, EST_INACTIVE);
  1898. break;
  1899. }
  1900. list = ehci->periodiclistbase & 0xfffff000;
  1901. /* check that register has been set */
  1902. if (list == 0) {
  1903. break;
  1904. }
  1905. list |= ((ehci->frindex & 0x1ff8) >> 1);
  1906. if (get_dwords(ehci, list, &entry, 1) < 0) {
  1907. break;
  1908. }
  1909. DPRINTF("PERIODIC state adv fr=%d. [%08X] -> %08X\n",
  1910. ehci->frindex / 8, list, entry);
  1911. ehci_set_fetch_addr(ehci, async,entry);
  1912. ehci_set_state(ehci, async, EST_FETCHENTRY);
  1913. ehci_advance_state(ehci, async);
  1914. ehci_queues_rip_unused(ehci, async);
  1915. break;
  1916. default:
  1917. /* this should only be due to a developer mistake */
  1918. fprintf(stderr, "ehci: Bad periodic state %d. "
  1919. "Resetting to active\n", ehci->pstate);
  1920. assert(0);
  1921. }
  1922. }
  1923. static void ehci_update_frindex(EHCIState *ehci, int uframes)
  1924. {
  1925. int i;
  1926. if (!ehci_enabled(ehci) && ehci->pstate == EST_INACTIVE) {
  1927. return;
  1928. }
  1929. for (i = 0; i < uframes; i++) {
  1930. ehci->frindex++;
  1931. if (ehci->frindex == 0x00002000) {
  1932. ehci_raise_irq(ehci, USBSTS_FLR);
  1933. }
  1934. if (ehci->frindex == 0x00004000) {
  1935. ehci_raise_irq(ehci, USBSTS_FLR);
  1936. ehci->frindex = 0;
  1937. if (ehci->usbsts_frindex >= 0x00004000) {
  1938. ehci->usbsts_frindex -= 0x00004000;
  1939. } else {
  1940. ehci->usbsts_frindex = 0;
  1941. }
  1942. }
  1943. }
  1944. }
  1945. static void ehci_frame_timer(void *opaque)
  1946. {
  1947. EHCIState *ehci = opaque;
  1948. int need_timer = 0;
  1949. int64_t expire_time, t_now;
  1950. uint64_t ns_elapsed;
  1951. int uframes, skipped_uframes;
  1952. int i;
  1953. t_now = qemu_get_clock_ns(vm_clock);
  1954. ns_elapsed = t_now - ehci->last_run_ns;
  1955. uframes = ns_elapsed / UFRAME_TIMER_NS;
  1956. if (ehci_periodic_enabled(ehci) || ehci->pstate != EST_INACTIVE) {
  1957. need_timer++;
  1958. if (uframes > (ehci->maxframes * 8)) {
  1959. skipped_uframes = uframes - (ehci->maxframes * 8);
  1960. ehci_update_frindex(ehci, skipped_uframes);
  1961. ehci->last_run_ns += UFRAME_TIMER_NS * skipped_uframes;
  1962. uframes -= skipped_uframes;
  1963. DPRINTF("WARNING - EHCI skipped %d uframes\n", skipped_uframes);
  1964. }
  1965. for (i = 0; i < uframes; i++) {
  1966. /*
  1967. * If we're running behind schedule, we should not catch up
  1968. * too fast, as that will make some guests unhappy:
  1969. * 1) We must process a minimum of MIN_UFR_PER_TICK frames,
  1970. * otherwise we will never catch up
  1971. * 2) Process frames until the guest has requested an irq (IOC)
  1972. */
  1973. if (i >= MIN_UFR_PER_TICK) {
  1974. ehci_commit_irq(ehci);
  1975. if ((ehci->usbsts & USBINTR_MASK) & ehci->usbintr) {
  1976. break;
  1977. }
  1978. }
  1979. if (ehci->periodic_sched_active) {
  1980. ehci->periodic_sched_active--;
  1981. }
  1982. ehci_update_frindex(ehci, 1);
  1983. if ((ehci->frindex & 7) == 0) {
  1984. ehci_advance_periodic_state(ehci);
  1985. }
  1986. ehci->last_run_ns += UFRAME_TIMER_NS;
  1987. }
  1988. } else {
  1989. ehci->periodic_sched_active = 0;
  1990. ehci_update_frindex(ehci, uframes);
  1991. ehci->last_run_ns += UFRAME_TIMER_NS * uframes;
  1992. }
  1993. if (ehci->periodic_sched_active) {
  1994. ehci->async_stepdown = 0;
  1995. } else if (ehci->async_stepdown < ehci->maxframes / 2) {
  1996. ehci->async_stepdown++;
  1997. }
  1998. /* Async is not inside loop since it executes everything it can once
  1999. * called
  2000. */
  2001. if (ehci_async_enabled(ehci) || ehci->astate != EST_INACTIVE) {
  2002. need_timer++;
  2003. ehci_advance_async_state(ehci);
  2004. }
  2005. ehci_commit_irq(ehci);
  2006. if (ehci->usbsts_pending) {
  2007. need_timer++;
  2008. ehci->async_stepdown = 0;
  2009. }
  2010. if (ehci_enabled(ehci) && (ehci->usbintr & USBSTS_FLR)) {
  2011. need_timer++;
  2012. }
  2013. if (need_timer) {
  2014. /* If we've raised int, we speed up the timer, so that we quickly
  2015. * notice any new packets queued up in response */
  2016. if (ehci->int_req_by_async && (ehci->usbsts & USBSTS_INT)) {
  2017. expire_time = t_now + get_ticks_per_sec() / (FRAME_TIMER_FREQ * 4);
  2018. ehci->int_req_by_async = false;
  2019. } else {
  2020. expire_time = t_now + (get_ticks_per_sec()
  2021. * (ehci->async_stepdown+1) / FRAME_TIMER_FREQ);
  2022. }
  2023. qemu_mod_timer(ehci->frame_timer, expire_time);
  2024. }
  2025. }
  2026. static const MemoryRegionOps ehci_mmio_caps_ops = {
  2027. .read = ehci_caps_read,
  2028. .valid.min_access_size = 1,
  2029. .valid.max_access_size = 4,
  2030. .impl.min_access_size = 1,
  2031. .impl.max_access_size = 1,
  2032. .endianness = DEVICE_LITTLE_ENDIAN,
  2033. };
  2034. static const MemoryRegionOps ehci_mmio_opreg_ops = {
  2035. .read = ehci_opreg_read,
  2036. .write = ehci_opreg_write,
  2037. .valid.min_access_size = 4,
  2038. .valid.max_access_size = 4,
  2039. .endianness = DEVICE_LITTLE_ENDIAN,
  2040. };
  2041. static const MemoryRegionOps ehci_mmio_port_ops = {
  2042. .read = ehci_port_read,
  2043. .write = ehci_port_write,
  2044. .valid.min_access_size = 4,
  2045. .valid.max_access_size = 4,
  2046. .endianness = DEVICE_LITTLE_ENDIAN,
  2047. };
  2048. static USBPortOps ehci_port_ops = {
  2049. .attach = ehci_attach,
  2050. .detach = ehci_detach,
  2051. .child_detach = ehci_child_detach,
  2052. .wakeup = ehci_wakeup,
  2053. .complete = ehci_async_complete_packet,
  2054. };
  2055. static USBBusOps ehci_bus_ops = {
  2056. .register_companion = ehci_register_companion,
  2057. .wakeup_endpoint = ehci_wakeup_endpoint,
  2058. };
  2059. static void usb_ehci_pre_save(void *opaque)
  2060. {
  2061. EHCIState *ehci = opaque;
  2062. uint32_t new_frindex;
  2063. /* Round down frindex to a multiple of 8 for migration compatibility */
  2064. new_frindex = ehci->frindex & ~7;
  2065. ehci->last_run_ns -= (ehci->frindex - new_frindex) * UFRAME_TIMER_NS;
  2066. ehci->frindex = new_frindex;
  2067. }
  2068. static int usb_ehci_post_load(void *opaque, int version_id)
  2069. {
  2070. EHCIState *s = opaque;
  2071. int i;
  2072. for (i = 0; i < NB_PORTS; i++) {
  2073. USBPort *companion = s->companion_ports[i];
  2074. if (companion == NULL) {
  2075. continue;
  2076. }
  2077. if (s->portsc[i] & PORTSC_POWNER) {
  2078. companion->dev = s->ports[i].dev;
  2079. } else {
  2080. companion->dev = NULL;
  2081. }
  2082. }
  2083. return 0;
  2084. }
  2085. static void usb_ehci_vm_state_change(void *opaque, int running, RunState state)
  2086. {
  2087. EHCIState *ehci = opaque;
  2088. /*
  2089. * We don't migrate the EHCIQueue-s, instead we rebuild them for the
  2090. * schedule in guest memory. We must do the rebuilt ASAP, so that
  2091. * USB-devices which have async handled packages have a packet in the
  2092. * ep queue to match the completion with.
  2093. */
  2094. if (state == RUN_STATE_RUNNING) {
  2095. ehci_advance_async_state(ehci);
  2096. }
  2097. /*
  2098. * The schedule rebuilt from guest memory could cause the migration dest
  2099. * to miss a QH unlink, and fail to cancel packets, since the unlinked QH
  2100. * will never have existed on the destination. Therefor we must flush the
  2101. * async schedule on savevm to catch any not yet noticed unlinks.
  2102. */
  2103. if (state == RUN_STATE_SAVE_VM) {
  2104. ehci_advance_async_state(ehci);
  2105. ehci_queues_rip_unseen(ehci, 1);
  2106. }
  2107. }
  2108. const VMStateDescription vmstate_ehci = {
  2109. .name = "ehci-core",
  2110. .version_id = 2,
  2111. .minimum_version_id = 1,
  2112. .pre_save = usb_ehci_pre_save,
  2113. .post_load = usb_ehci_post_load,
  2114. .fields = (VMStateField[]) {
  2115. /* mmio registers */
  2116. VMSTATE_UINT32(usbcmd, EHCIState),
  2117. VMSTATE_UINT32(usbsts, EHCIState),
  2118. VMSTATE_UINT32_V(usbsts_pending, EHCIState, 2),
  2119. VMSTATE_UINT32_V(usbsts_frindex, EHCIState, 2),
  2120. VMSTATE_UINT32(usbintr, EHCIState),
  2121. VMSTATE_UINT32(frindex, EHCIState),
  2122. VMSTATE_UINT32(ctrldssegment, EHCIState),
  2123. VMSTATE_UINT32(periodiclistbase, EHCIState),
  2124. VMSTATE_UINT32(asynclistaddr, EHCIState),
  2125. VMSTATE_UINT32(configflag, EHCIState),
  2126. VMSTATE_UINT32(portsc[0], EHCIState),
  2127. VMSTATE_UINT32(portsc[1], EHCIState),
  2128. VMSTATE_UINT32(portsc[2], EHCIState),
  2129. VMSTATE_UINT32(portsc[3], EHCIState),
  2130. VMSTATE_UINT32(portsc[4], EHCIState),
  2131. VMSTATE_UINT32(portsc[5], EHCIState),
  2132. /* frame timer */
  2133. VMSTATE_TIMER(frame_timer, EHCIState),
  2134. VMSTATE_UINT64(last_run_ns, EHCIState),
  2135. VMSTATE_UINT32(async_stepdown, EHCIState),
  2136. /* schedule state */
  2137. VMSTATE_UINT32(astate, EHCIState),
  2138. VMSTATE_UINT32(pstate, EHCIState),
  2139. VMSTATE_UINT32(a_fetch_addr, EHCIState),
  2140. VMSTATE_UINT32(p_fetch_addr, EHCIState),
  2141. VMSTATE_END_OF_LIST()
  2142. }
  2143. };
  2144. void usb_ehci_initfn(EHCIState *s, DeviceState *dev)
  2145. {
  2146. int i;
  2147. /* 2.2 host controller interface version */
  2148. s->caps[0x00] = (uint8_t)(s->opregbase - s->capsbase);
  2149. s->caps[0x01] = 0x00;
  2150. s->caps[0x02] = 0x00;
  2151. s->caps[0x03] = 0x01; /* HC version */
  2152. s->caps[0x04] = NB_PORTS; /* Number of downstream ports */
  2153. s->caps[0x05] = 0x00; /* No companion ports at present */
  2154. s->caps[0x06] = 0x00;
  2155. s->caps[0x07] = 0x00;
  2156. s->caps[0x08] = 0x80; /* We can cache whole frame, no 64-bit */
  2157. s->caps[0x0a] = 0x00;
  2158. s->caps[0x0b] = 0x00;
  2159. usb_bus_new(&s->bus, &ehci_bus_ops, dev);
  2160. for(i = 0; i < NB_PORTS; i++) {
  2161. usb_register_port(&s->bus, &s->ports[i], s, i, &ehci_port_ops,
  2162. USB_SPEED_MASK_HIGH);
  2163. s->ports[i].dev = 0;
  2164. }
  2165. s->frame_timer = qemu_new_timer_ns(vm_clock, ehci_frame_timer, s);
  2166. s->async_bh = qemu_bh_new(ehci_frame_timer, s);
  2167. QTAILQ_INIT(&s->aqueues);
  2168. QTAILQ_INIT(&s->pqueues);
  2169. usb_packet_init(&s->ipacket);
  2170. qemu_register_reset(ehci_reset, s);
  2171. qemu_add_vm_change_state_handler(usb_ehci_vm_state_change, s);
  2172. memory_region_init(&s->mem, "ehci", MMIO_SIZE);
  2173. memory_region_init_io(&s->mem_caps, &ehci_mmio_caps_ops, s,
  2174. "capabilities", CAPA_SIZE);
  2175. memory_region_init_io(&s->mem_opreg, &ehci_mmio_opreg_ops, s,
  2176. "operational", PORTSC_BEGIN);
  2177. memory_region_init_io(&s->mem_ports, &ehci_mmio_port_ops, s,
  2178. "ports", PORTSC_END - PORTSC_BEGIN);
  2179. memory_region_add_subregion(&s->mem, s->capsbase, &s->mem_caps);
  2180. memory_region_add_subregion(&s->mem, s->opregbase, &s->mem_opreg);
  2181. memory_region_add_subregion(&s->mem, s->opregbase + PORTSC_BEGIN,
  2182. &s->mem_ports);
  2183. }
  2184. /*
  2185. * vim: expandtab ts=4
  2186. */