hcd-ehci.c 73 KB

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