hcd-ehci.c 73 KB

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