hcd-ehci.c 74 KB

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