hcd-ehci.c 74 KB

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