hcd-xhci.c 107 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669
  1. /*
  2. * USB xHCI controller emulation
  3. *
  4. * Copyright (c) 2011 Securiforest
  5. * Date: 2011-05-11 ; Author: Hector Martin <hector@marcansoft.com>
  6. * Based on usb-ohci.c, emulates Renesas NEC USB 3.0
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #include "qemu/osdep.h"
  22. #include "qemu/timer.h"
  23. #include "qemu/log.h"
  24. #include "qemu/module.h"
  25. #include "qemu/queue.h"
  26. #include "migration/vmstate.h"
  27. #include "hw/qdev-properties.h"
  28. #include "trace.h"
  29. #include "qapi/error.h"
  30. #include "hcd-xhci.h"
  31. //#define DEBUG_XHCI
  32. //#define DEBUG_DATA
  33. #ifdef DEBUG_XHCI
  34. #define DPRINTF(...) fprintf(stderr, __VA_ARGS__)
  35. #else
  36. #define DPRINTF(...) do {} while (0)
  37. #endif
  38. #define FIXME(_msg) do { fprintf(stderr, "FIXME %s:%d %s\n", \
  39. __func__, __LINE__, _msg); abort(); } while (0)
  40. #define TRB_LINK_LIMIT 32
  41. #define COMMAND_LIMIT 256
  42. #define TRANSFER_LIMIT 256
  43. #define LEN_CAP 0x40
  44. #define LEN_OPER (0x400 + 0x10 * XHCI_MAXPORTS)
  45. #define LEN_RUNTIME ((XHCI_MAXINTRS + 1) * 0x20)
  46. #define LEN_DOORBELL ((XHCI_MAXSLOTS + 1) * 0x20)
  47. #define OFF_OPER LEN_CAP
  48. #define OFF_RUNTIME 0x1000
  49. #define OFF_DOORBELL 0x2000
  50. #if (OFF_OPER + LEN_OPER) > OFF_RUNTIME
  51. #error Increase OFF_RUNTIME
  52. #endif
  53. #if (OFF_RUNTIME + LEN_RUNTIME) > OFF_DOORBELL
  54. #error Increase OFF_DOORBELL
  55. #endif
  56. #if (OFF_DOORBELL + LEN_DOORBELL) > XHCI_LEN_REGS
  57. # error Increase XHCI_LEN_REGS
  58. #endif
  59. /* bit definitions */
  60. #define USBCMD_RS (1<<0)
  61. #define USBCMD_HCRST (1<<1)
  62. #define USBCMD_INTE (1<<2)
  63. #define USBCMD_HSEE (1<<3)
  64. #define USBCMD_LHCRST (1<<7)
  65. #define USBCMD_CSS (1<<8)
  66. #define USBCMD_CRS (1<<9)
  67. #define USBCMD_EWE (1<<10)
  68. #define USBCMD_EU3S (1<<11)
  69. #define USBSTS_HCH (1<<0)
  70. #define USBSTS_HSE (1<<2)
  71. #define USBSTS_EINT (1<<3)
  72. #define USBSTS_PCD (1<<4)
  73. #define USBSTS_SSS (1<<8)
  74. #define USBSTS_RSS (1<<9)
  75. #define USBSTS_SRE (1<<10)
  76. #define USBSTS_CNR (1<<11)
  77. #define USBSTS_HCE (1<<12)
  78. #define PORTSC_CCS (1<<0)
  79. #define PORTSC_PED (1<<1)
  80. #define PORTSC_OCA (1<<3)
  81. #define PORTSC_PR (1<<4)
  82. #define PORTSC_PLS_SHIFT 5
  83. #define PORTSC_PLS_MASK 0xf
  84. #define PORTSC_PP (1<<9)
  85. #define PORTSC_SPEED_SHIFT 10
  86. #define PORTSC_SPEED_MASK 0xf
  87. #define PORTSC_SPEED_FULL (1<<10)
  88. #define PORTSC_SPEED_LOW (2<<10)
  89. #define PORTSC_SPEED_HIGH (3<<10)
  90. #define PORTSC_SPEED_SUPER (4<<10)
  91. #define PORTSC_PIC_SHIFT 14
  92. #define PORTSC_PIC_MASK 0x3
  93. #define PORTSC_LWS (1<<16)
  94. #define PORTSC_CSC (1<<17)
  95. #define PORTSC_PEC (1<<18)
  96. #define PORTSC_WRC (1<<19)
  97. #define PORTSC_OCC (1<<20)
  98. #define PORTSC_PRC (1<<21)
  99. #define PORTSC_PLC (1<<22)
  100. #define PORTSC_CEC (1<<23)
  101. #define PORTSC_CAS (1<<24)
  102. #define PORTSC_WCE (1<<25)
  103. #define PORTSC_WDE (1<<26)
  104. #define PORTSC_WOE (1<<27)
  105. #define PORTSC_DR (1<<30)
  106. #define PORTSC_WPR (1<<31)
  107. #define CRCR_RCS (1<<0)
  108. #define CRCR_CS (1<<1)
  109. #define CRCR_CA (1<<2)
  110. #define CRCR_CRR (1<<3)
  111. #define IMAN_IP (1<<0)
  112. #define IMAN_IE (1<<1)
  113. #define ERDP_EHB (1<<3)
  114. #define TRB_SIZE 16
  115. typedef struct XHCITRB {
  116. uint64_t parameter;
  117. uint32_t status;
  118. uint32_t control;
  119. dma_addr_t addr;
  120. bool ccs;
  121. } XHCITRB;
  122. enum {
  123. PLS_U0 = 0,
  124. PLS_U1 = 1,
  125. PLS_U2 = 2,
  126. PLS_U3 = 3,
  127. PLS_DISABLED = 4,
  128. PLS_RX_DETECT = 5,
  129. PLS_INACTIVE = 6,
  130. PLS_POLLING = 7,
  131. PLS_RECOVERY = 8,
  132. PLS_HOT_RESET = 9,
  133. PLS_COMPILANCE_MODE = 10,
  134. PLS_TEST_MODE = 11,
  135. PLS_RESUME = 15,
  136. };
  137. #define CR_LINK TR_LINK
  138. #define TRB_C (1<<0)
  139. #define TRB_TYPE_SHIFT 10
  140. #define TRB_TYPE_MASK 0x3f
  141. #define TRB_TYPE(t) (((t).control >> TRB_TYPE_SHIFT) & TRB_TYPE_MASK)
  142. #define TRB_EV_ED (1<<2)
  143. #define TRB_TR_ENT (1<<1)
  144. #define TRB_TR_ISP (1<<2)
  145. #define TRB_TR_NS (1<<3)
  146. #define TRB_TR_CH (1<<4)
  147. #define TRB_TR_IOC (1<<5)
  148. #define TRB_TR_IDT (1<<6)
  149. #define TRB_TR_TBC_SHIFT 7
  150. #define TRB_TR_TBC_MASK 0x3
  151. #define TRB_TR_BEI (1<<9)
  152. #define TRB_TR_TLBPC_SHIFT 16
  153. #define TRB_TR_TLBPC_MASK 0xf
  154. #define TRB_TR_FRAMEID_SHIFT 20
  155. #define TRB_TR_FRAMEID_MASK 0x7ff
  156. #define TRB_TR_SIA (1<<31)
  157. #define TRB_TR_DIR (1<<16)
  158. #define TRB_CR_SLOTID_SHIFT 24
  159. #define TRB_CR_SLOTID_MASK 0xff
  160. #define TRB_CR_EPID_SHIFT 16
  161. #define TRB_CR_EPID_MASK 0x1f
  162. #define TRB_CR_BSR (1<<9)
  163. #define TRB_CR_DC (1<<9)
  164. #define TRB_LK_TC (1<<1)
  165. #define TRB_INTR_SHIFT 22
  166. #define TRB_INTR_MASK 0x3ff
  167. #define TRB_INTR(t) (((t).status >> TRB_INTR_SHIFT) & TRB_INTR_MASK)
  168. #define EP_TYPE_MASK 0x7
  169. #define EP_TYPE_SHIFT 3
  170. #define EP_STATE_MASK 0x7
  171. #define EP_DISABLED (0<<0)
  172. #define EP_RUNNING (1<<0)
  173. #define EP_HALTED (2<<0)
  174. #define EP_STOPPED (3<<0)
  175. #define EP_ERROR (4<<0)
  176. #define SLOT_STATE_MASK 0x1f
  177. #define SLOT_STATE_SHIFT 27
  178. #define SLOT_STATE(s) (((s)>>SLOT_STATE_SHIFT)&SLOT_STATE_MASK)
  179. #define SLOT_ENABLED 0
  180. #define SLOT_DEFAULT 1
  181. #define SLOT_ADDRESSED 2
  182. #define SLOT_CONFIGURED 3
  183. #define SLOT_CONTEXT_ENTRIES_MASK 0x1f
  184. #define SLOT_CONTEXT_ENTRIES_SHIFT 27
  185. #define get_field(data, field) \
  186. (((data) >> field##_SHIFT) & field##_MASK)
  187. #define set_field(data, newval, field) do { \
  188. uint32_t val = *data; \
  189. val &= ~(field##_MASK << field##_SHIFT); \
  190. val |= ((newval) & field##_MASK) << field##_SHIFT; \
  191. *data = val; \
  192. } while (0)
  193. typedef enum EPType {
  194. ET_INVALID = 0,
  195. ET_ISO_OUT,
  196. ET_BULK_OUT,
  197. ET_INTR_OUT,
  198. ET_CONTROL,
  199. ET_ISO_IN,
  200. ET_BULK_IN,
  201. ET_INTR_IN,
  202. } EPType;
  203. typedef struct XHCITransfer {
  204. XHCIEPContext *epctx;
  205. USBPacket packet;
  206. QEMUSGList sgl;
  207. bool running_async;
  208. bool running_retry;
  209. bool complete;
  210. bool int_req;
  211. unsigned int iso_pkts;
  212. unsigned int streamid;
  213. bool in_xfer;
  214. bool iso_xfer;
  215. bool timed_xfer;
  216. unsigned int trb_count;
  217. XHCITRB *trbs;
  218. TRBCCode status;
  219. unsigned int pkts;
  220. unsigned int pktsize;
  221. unsigned int cur_pkt;
  222. uint64_t mfindex_kick;
  223. QTAILQ_ENTRY(XHCITransfer) next;
  224. } XHCITransfer;
  225. struct XHCIStreamContext {
  226. dma_addr_t pctx;
  227. unsigned int sct;
  228. XHCIRing ring;
  229. };
  230. struct XHCIEPContext {
  231. XHCIState *xhci;
  232. unsigned int slotid;
  233. unsigned int epid;
  234. XHCIRing ring;
  235. uint32_t xfer_count;
  236. QTAILQ_HEAD(, XHCITransfer) transfers;
  237. XHCITransfer *retry;
  238. EPType type;
  239. dma_addr_t pctx;
  240. unsigned int max_psize;
  241. uint32_t state;
  242. uint32_t kick_active;
  243. /* streams */
  244. unsigned int max_pstreams;
  245. bool lsa;
  246. unsigned int nr_pstreams;
  247. XHCIStreamContext *pstreams;
  248. /* iso xfer scheduling */
  249. unsigned int interval;
  250. int64_t mfindex_last;
  251. QEMUTimer *kick_timer;
  252. };
  253. typedef struct XHCIEvRingSeg {
  254. uint32_t addr_low;
  255. uint32_t addr_high;
  256. uint32_t size;
  257. uint32_t rsvd;
  258. } XHCIEvRingSeg;
  259. static void xhci_kick_ep(XHCIState *xhci, unsigned int slotid,
  260. unsigned int epid, unsigned int streamid);
  261. static void xhci_kick_epctx(XHCIEPContext *epctx, unsigned int streamid);
  262. static TRBCCode xhci_disable_ep(XHCIState *xhci, unsigned int slotid,
  263. unsigned int epid);
  264. static void xhci_xfer_report(XHCITransfer *xfer);
  265. static void xhci_event(XHCIState *xhci, XHCIEvent *event, int v);
  266. static void xhci_write_event(XHCIState *xhci, XHCIEvent *event, int v);
  267. static USBEndpoint *xhci_epid_to_usbep(XHCIEPContext *epctx);
  268. static const char *TRBType_names[] = {
  269. [TRB_RESERVED] = "TRB_RESERVED",
  270. [TR_NORMAL] = "TR_NORMAL",
  271. [TR_SETUP] = "TR_SETUP",
  272. [TR_DATA] = "TR_DATA",
  273. [TR_STATUS] = "TR_STATUS",
  274. [TR_ISOCH] = "TR_ISOCH",
  275. [TR_LINK] = "TR_LINK",
  276. [TR_EVDATA] = "TR_EVDATA",
  277. [TR_NOOP] = "TR_NOOP",
  278. [CR_ENABLE_SLOT] = "CR_ENABLE_SLOT",
  279. [CR_DISABLE_SLOT] = "CR_DISABLE_SLOT",
  280. [CR_ADDRESS_DEVICE] = "CR_ADDRESS_DEVICE",
  281. [CR_CONFIGURE_ENDPOINT] = "CR_CONFIGURE_ENDPOINT",
  282. [CR_EVALUATE_CONTEXT] = "CR_EVALUATE_CONTEXT",
  283. [CR_RESET_ENDPOINT] = "CR_RESET_ENDPOINT",
  284. [CR_STOP_ENDPOINT] = "CR_STOP_ENDPOINT",
  285. [CR_SET_TR_DEQUEUE] = "CR_SET_TR_DEQUEUE",
  286. [CR_RESET_DEVICE] = "CR_RESET_DEVICE",
  287. [CR_FORCE_EVENT] = "CR_FORCE_EVENT",
  288. [CR_NEGOTIATE_BW] = "CR_NEGOTIATE_BW",
  289. [CR_SET_LATENCY_TOLERANCE] = "CR_SET_LATENCY_TOLERANCE",
  290. [CR_GET_PORT_BANDWIDTH] = "CR_GET_PORT_BANDWIDTH",
  291. [CR_FORCE_HEADER] = "CR_FORCE_HEADER",
  292. [CR_NOOP] = "CR_NOOP",
  293. [ER_TRANSFER] = "ER_TRANSFER",
  294. [ER_COMMAND_COMPLETE] = "ER_COMMAND_COMPLETE",
  295. [ER_PORT_STATUS_CHANGE] = "ER_PORT_STATUS_CHANGE",
  296. [ER_BANDWIDTH_REQUEST] = "ER_BANDWIDTH_REQUEST",
  297. [ER_DOORBELL] = "ER_DOORBELL",
  298. [ER_HOST_CONTROLLER] = "ER_HOST_CONTROLLER",
  299. [ER_DEVICE_NOTIFICATION] = "ER_DEVICE_NOTIFICATION",
  300. [ER_MFINDEX_WRAP] = "ER_MFINDEX_WRAP",
  301. [CR_VENDOR_NEC_FIRMWARE_REVISION] = "CR_VENDOR_NEC_FIRMWARE_REVISION",
  302. [CR_VENDOR_NEC_CHALLENGE_RESPONSE] = "CR_VENDOR_NEC_CHALLENGE_RESPONSE",
  303. };
  304. static const char *TRBCCode_names[] = {
  305. [CC_INVALID] = "CC_INVALID",
  306. [CC_SUCCESS] = "CC_SUCCESS",
  307. [CC_DATA_BUFFER_ERROR] = "CC_DATA_BUFFER_ERROR",
  308. [CC_BABBLE_DETECTED] = "CC_BABBLE_DETECTED",
  309. [CC_USB_TRANSACTION_ERROR] = "CC_USB_TRANSACTION_ERROR",
  310. [CC_TRB_ERROR] = "CC_TRB_ERROR",
  311. [CC_STALL_ERROR] = "CC_STALL_ERROR",
  312. [CC_RESOURCE_ERROR] = "CC_RESOURCE_ERROR",
  313. [CC_BANDWIDTH_ERROR] = "CC_BANDWIDTH_ERROR",
  314. [CC_NO_SLOTS_ERROR] = "CC_NO_SLOTS_ERROR",
  315. [CC_INVALID_STREAM_TYPE_ERROR] = "CC_INVALID_STREAM_TYPE_ERROR",
  316. [CC_SLOT_NOT_ENABLED_ERROR] = "CC_SLOT_NOT_ENABLED_ERROR",
  317. [CC_EP_NOT_ENABLED_ERROR] = "CC_EP_NOT_ENABLED_ERROR",
  318. [CC_SHORT_PACKET] = "CC_SHORT_PACKET",
  319. [CC_RING_UNDERRUN] = "CC_RING_UNDERRUN",
  320. [CC_RING_OVERRUN] = "CC_RING_OVERRUN",
  321. [CC_VF_ER_FULL] = "CC_VF_ER_FULL",
  322. [CC_PARAMETER_ERROR] = "CC_PARAMETER_ERROR",
  323. [CC_BANDWIDTH_OVERRUN] = "CC_BANDWIDTH_OVERRUN",
  324. [CC_CONTEXT_STATE_ERROR] = "CC_CONTEXT_STATE_ERROR",
  325. [CC_NO_PING_RESPONSE_ERROR] = "CC_NO_PING_RESPONSE_ERROR",
  326. [CC_EVENT_RING_FULL_ERROR] = "CC_EVENT_RING_FULL_ERROR",
  327. [CC_INCOMPATIBLE_DEVICE_ERROR] = "CC_INCOMPATIBLE_DEVICE_ERROR",
  328. [CC_MISSED_SERVICE_ERROR] = "CC_MISSED_SERVICE_ERROR",
  329. [CC_COMMAND_RING_STOPPED] = "CC_COMMAND_RING_STOPPED",
  330. [CC_COMMAND_ABORTED] = "CC_COMMAND_ABORTED",
  331. [CC_STOPPED] = "CC_STOPPED",
  332. [CC_STOPPED_LENGTH_INVALID] = "CC_STOPPED_LENGTH_INVALID",
  333. [CC_MAX_EXIT_LATENCY_TOO_LARGE_ERROR]
  334. = "CC_MAX_EXIT_LATENCY_TOO_LARGE_ERROR",
  335. [CC_ISOCH_BUFFER_OVERRUN] = "CC_ISOCH_BUFFER_OVERRUN",
  336. [CC_EVENT_LOST_ERROR] = "CC_EVENT_LOST_ERROR",
  337. [CC_UNDEFINED_ERROR] = "CC_UNDEFINED_ERROR",
  338. [CC_INVALID_STREAM_ID_ERROR] = "CC_INVALID_STREAM_ID_ERROR",
  339. [CC_SECONDARY_BANDWIDTH_ERROR] = "CC_SECONDARY_BANDWIDTH_ERROR",
  340. [CC_SPLIT_TRANSACTION_ERROR] = "CC_SPLIT_TRANSACTION_ERROR",
  341. };
  342. static const char *ep_state_names[] = {
  343. [EP_DISABLED] = "disabled",
  344. [EP_RUNNING] = "running",
  345. [EP_HALTED] = "halted",
  346. [EP_STOPPED] = "stopped",
  347. [EP_ERROR] = "error",
  348. };
  349. static const char *lookup_name(uint32_t index, const char **list, uint32_t llen)
  350. {
  351. if (index >= llen || list[index] == NULL) {
  352. return "???";
  353. }
  354. return list[index];
  355. }
  356. static const char *trb_name(XHCITRB *trb)
  357. {
  358. return lookup_name(TRB_TYPE(*trb), TRBType_names,
  359. ARRAY_SIZE(TRBType_names));
  360. }
  361. static const char *event_name(XHCIEvent *event)
  362. {
  363. return lookup_name(event->ccode, TRBCCode_names,
  364. ARRAY_SIZE(TRBCCode_names));
  365. }
  366. static const char *ep_state_name(uint32_t state)
  367. {
  368. return lookup_name(state, ep_state_names,
  369. ARRAY_SIZE(ep_state_names));
  370. }
  371. bool xhci_get_flag(XHCIState *xhci, enum xhci_flags bit)
  372. {
  373. return xhci->flags & (1 << bit);
  374. }
  375. void xhci_set_flag(XHCIState *xhci, enum xhci_flags bit)
  376. {
  377. xhci->flags |= (1 << bit);
  378. }
  379. static uint64_t xhci_mfindex_get(XHCIState *xhci)
  380. {
  381. int64_t now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
  382. return (now - xhci->mfindex_start) / 125000;
  383. }
  384. static void xhci_mfwrap_update(XHCIState *xhci)
  385. {
  386. const uint32_t bits = USBCMD_RS | USBCMD_EWE;
  387. uint32_t mfindex, left;
  388. int64_t now;
  389. if ((xhci->usbcmd & bits) == bits) {
  390. now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
  391. mfindex = ((now - xhci->mfindex_start) / 125000) & 0x3fff;
  392. left = 0x4000 - mfindex;
  393. timer_mod(xhci->mfwrap_timer, now + left * 125000);
  394. } else {
  395. timer_del(xhci->mfwrap_timer);
  396. }
  397. }
  398. static void xhci_mfwrap_timer(void *opaque)
  399. {
  400. XHCIState *xhci = opaque;
  401. XHCIEvent wrap = { ER_MFINDEX_WRAP, CC_SUCCESS };
  402. xhci_event(xhci, &wrap, 0);
  403. xhci_mfwrap_update(xhci);
  404. }
  405. static void xhci_die(XHCIState *xhci)
  406. {
  407. xhci->usbsts |= USBSTS_HCE;
  408. DPRINTF("xhci: asserted controller error\n");
  409. }
  410. static inline dma_addr_t xhci_addr64(uint32_t low, uint32_t high)
  411. {
  412. if (sizeof(dma_addr_t) == 4) {
  413. return low;
  414. } else {
  415. return low | (((dma_addr_t)high << 16) << 16);
  416. }
  417. }
  418. static inline dma_addr_t xhci_mask64(uint64_t addr)
  419. {
  420. if (sizeof(dma_addr_t) == 4) {
  421. return addr & 0xffffffff;
  422. } else {
  423. return addr;
  424. }
  425. }
  426. static inline void xhci_dma_read_u32s(XHCIState *xhci, dma_addr_t addr,
  427. uint32_t *buf, size_t len)
  428. {
  429. int i;
  430. assert((len % sizeof(uint32_t)) == 0);
  431. if (dma_memory_read(xhci->as, addr, buf, len,
  432. MEMTXATTRS_UNSPECIFIED) != MEMTX_OK) {
  433. qemu_log_mask(LOG_GUEST_ERROR, "%s: DMA memory access failed!\n",
  434. __func__);
  435. memset(buf, 0xff, len);
  436. xhci_die(xhci);
  437. return;
  438. }
  439. for (i = 0; i < (len / sizeof(uint32_t)); i++) {
  440. buf[i] = le32_to_cpu(buf[i]);
  441. }
  442. }
  443. static inline void xhci_dma_write_u32s(XHCIState *xhci, dma_addr_t addr,
  444. const uint32_t *buf, size_t len)
  445. {
  446. int i;
  447. uint32_t tmp[5];
  448. uint32_t n = len / sizeof(uint32_t);
  449. assert((len % sizeof(uint32_t)) == 0);
  450. assert(n <= ARRAY_SIZE(tmp));
  451. for (i = 0; i < n; i++) {
  452. tmp[i] = cpu_to_le32(buf[i]);
  453. }
  454. if (dma_memory_write(xhci->as, addr, tmp, len,
  455. MEMTXATTRS_UNSPECIFIED) != MEMTX_OK) {
  456. qemu_log_mask(LOG_GUEST_ERROR, "%s: DMA memory access failed!\n",
  457. __func__);
  458. xhci_die(xhci);
  459. return;
  460. }
  461. }
  462. static XHCIPort *xhci_lookup_port(XHCIState *xhci, struct USBPort *uport)
  463. {
  464. int index;
  465. if (!uport->dev) {
  466. return NULL;
  467. }
  468. switch (uport->dev->speed) {
  469. case USB_SPEED_LOW:
  470. case USB_SPEED_FULL:
  471. case USB_SPEED_HIGH:
  472. if (xhci_get_flag(xhci, XHCI_FLAG_SS_FIRST)) {
  473. index = uport->index + xhci->numports_3;
  474. } else {
  475. index = uport->index;
  476. }
  477. break;
  478. case USB_SPEED_SUPER:
  479. if (xhci_get_flag(xhci, XHCI_FLAG_SS_FIRST)) {
  480. index = uport->index;
  481. } else {
  482. index = uport->index + xhci->numports_2;
  483. }
  484. break;
  485. default:
  486. return NULL;
  487. }
  488. return &xhci->ports[index];
  489. }
  490. static void xhci_intr_update(XHCIState *xhci, int v)
  491. {
  492. int level = 0;
  493. if (v == 0) {
  494. if (xhci->intr[0].iman & IMAN_IP &&
  495. xhci->intr[0].iman & IMAN_IE &&
  496. xhci->usbcmd & USBCMD_INTE) {
  497. level = 1;
  498. }
  499. if (xhci->intr_raise) {
  500. if (xhci->intr_raise(xhci, 0, level)) {
  501. xhci->intr[0].iman &= ~IMAN_IP;
  502. }
  503. }
  504. }
  505. if (xhci->intr_update) {
  506. xhci->intr_update(xhci, v,
  507. xhci->intr[v].iman & IMAN_IE);
  508. }
  509. }
  510. static void xhci_intr_raise(XHCIState *xhci, int v)
  511. {
  512. bool pending = (xhci->intr[v].erdp_low & ERDP_EHB);
  513. xhci->intr[v].erdp_low |= ERDP_EHB;
  514. xhci->intr[v].iman |= IMAN_IP;
  515. xhci->usbsts |= USBSTS_EINT;
  516. if (pending) {
  517. return;
  518. }
  519. if (!(xhci->intr[v].iman & IMAN_IE)) {
  520. return;
  521. }
  522. if (!(xhci->usbcmd & USBCMD_INTE)) {
  523. return;
  524. }
  525. if (xhci->intr_raise) {
  526. if (xhci->intr_raise(xhci, v, true)) {
  527. xhci->intr[v].iman &= ~IMAN_IP;
  528. }
  529. }
  530. }
  531. static inline int xhci_running(XHCIState *xhci)
  532. {
  533. return !(xhci->usbsts & USBSTS_HCH);
  534. }
  535. static void xhci_write_event(XHCIState *xhci, XHCIEvent *event, int v)
  536. {
  537. XHCIInterrupter *intr = &xhci->intr[v];
  538. XHCITRB ev_trb;
  539. dma_addr_t addr;
  540. ev_trb.parameter = cpu_to_le64(event->ptr);
  541. ev_trb.status = cpu_to_le32(event->length | (event->ccode << 24));
  542. ev_trb.control = (event->slotid << 24) | (event->epid << 16) |
  543. event->flags | (event->type << TRB_TYPE_SHIFT);
  544. if (intr->er_pcs) {
  545. ev_trb.control |= TRB_C;
  546. }
  547. ev_trb.control = cpu_to_le32(ev_trb.control);
  548. trace_usb_xhci_queue_event(v, intr->er_ep_idx, trb_name(&ev_trb),
  549. event_name(event), ev_trb.parameter,
  550. ev_trb.status, ev_trb.control);
  551. addr = intr->er_start + TRB_SIZE*intr->er_ep_idx;
  552. if (dma_memory_write(xhci->as, addr, &ev_trb, TRB_SIZE,
  553. MEMTXATTRS_UNSPECIFIED) != MEMTX_OK) {
  554. qemu_log_mask(LOG_GUEST_ERROR, "%s: DMA memory access failed!\n",
  555. __func__);
  556. xhci_die(xhci);
  557. }
  558. intr->er_ep_idx++;
  559. if (intr->er_ep_idx >= intr->er_size) {
  560. intr->er_ep_idx = 0;
  561. intr->er_pcs = !intr->er_pcs;
  562. }
  563. }
  564. static void xhci_event(XHCIState *xhci, XHCIEvent *event, int v)
  565. {
  566. XHCIInterrupter *intr;
  567. dma_addr_t erdp;
  568. unsigned int dp_idx;
  569. if (v >= xhci->numintrs) {
  570. DPRINTF("intr nr out of range (%d >= %d)\n", v, xhci->numintrs);
  571. return;
  572. }
  573. intr = &xhci->intr[v];
  574. erdp = xhci_addr64(intr->erdp_low, intr->erdp_high);
  575. if (erdp < intr->er_start ||
  576. erdp >= (intr->er_start + TRB_SIZE*intr->er_size)) {
  577. DPRINTF("xhci: ERDP out of bounds: "DMA_ADDR_FMT"\n", erdp);
  578. DPRINTF("xhci: ER[%d] at "DMA_ADDR_FMT" len %d\n",
  579. v, intr->er_start, intr->er_size);
  580. xhci_die(xhci);
  581. return;
  582. }
  583. dp_idx = (erdp - intr->er_start) / TRB_SIZE;
  584. assert(dp_idx < intr->er_size);
  585. if ((intr->er_ep_idx + 2) % intr->er_size == dp_idx) {
  586. DPRINTF("xhci: ER %d full, send ring full error\n", v);
  587. XHCIEvent full = {ER_HOST_CONTROLLER, CC_EVENT_RING_FULL_ERROR};
  588. xhci_write_event(xhci, &full, v);
  589. } else if ((intr->er_ep_idx + 1) % intr->er_size == dp_idx) {
  590. DPRINTF("xhci: ER %d full, drop event\n", v);
  591. } else {
  592. xhci_write_event(xhci, event, v);
  593. }
  594. xhci_intr_raise(xhci, v);
  595. }
  596. static void xhci_ring_init(XHCIState *xhci, XHCIRing *ring,
  597. dma_addr_t base)
  598. {
  599. ring->dequeue = base;
  600. ring->ccs = 1;
  601. }
  602. static TRBType xhci_ring_fetch(XHCIState *xhci, XHCIRing *ring, XHCITRB *trb,
  603. dma_addr_t *addr)
  604. {
  605. uint32_t link_cnt = 0;
  606. while (1) {
  607. TRBType type;
  608. if (dma_memory_read(xhci->as, ring->dequeue, trb, TRB_SIZE,
  609. MEMTXATTRS_UNSPECIFIED) != MEMTX_OK) {
  610. qemu_log_mask(LOG_GUEST_ERROR, "%s: DMA memory access failed!\n",
  611. __func__);
  612. return 0;
  613. }
  614. trb->addr = ring->dequeue;
  615. trb->ccs = ring->ccs;
  616. le64_to_cpus(&trb->parameter);
  617. le32_to_cpus(&trb->status);
  618. le32_to_cpus(&trb->control);
  619. trace_usb_xhci_fetch_trb(ring->dequeue, trb_name(trb),
  620. trb->parameter, trb->status, trb->control);
  621. if ((trb->control & TRB_C) != ring->ccs) {
  622. return 0;
  623. }
  624. type = TRB_TYPE(*trb);
  625. if (type != TR_LINK) {
  626. if (addr) {
  627. *addr = ring->dequeue;
  628. }
  629. ring->dequeue += TRB_SIZE;
  630. return type;
  631. } else {
  632. if (++link_cnt > TRB_LINK_LIMIT) {
  633. trace_usb_xhci_enforced_limit("trb-link");
  634. return 0;
  635. }
  636. ring->dequeue = xhci_mask64(trb->parameter);
  637. if (trb->control & TRB_LK_TC) {
  638. ring->ccs = !ring->ccs;
  639. }
  640. }
  641. }
  642. }
  643. static int xhci_ring_chain_length(XHCIState *xhci, const XHCIRing *ring)
  644. {
  645. XHCITRB trb;
  646. int length = 0;
  647. dma_addr_t dequeue = ring->dequeue;
  648. bool ccs = ring->ccs;
  649. /* hack to bundle together the two/three TDs that make a setup transfer */
  650. bool control_td_set = 0;
  651. uint32_t link_cnt = 0;
  652. do {
  653. TRBType type;
  654. if (dma_memory_read(xhci->as, dequeue, &trb, TRB_SIZE,
  655. MEMTXATTRS_UNSPECIFIED) != MEMTX_OK) {
  656. qemu_log_mask(LOG_GUEST_ERROR, "%s: DMA memory access failed!\n",
  657. __func__);
  658. return -1;
  659. }
  660. le64_to_cpus(&trb.parameter);
  661. le32_to_cpus(&trb.status);
  662. le32_to_cpus(&trb.control);
  663. if ((trb.control & TRB_C) != ccs) {
  664. return -length;
  665. }
  666. type = TRB_TYPE(trb);
  667. if (type == TR_LINK) {
  668. if (++link_cnt > TRB_LINK_LIMIT) {
  669. return -length;
  670. }
  671. dequeue = xhci_mask64(trb.parameter);
  672. if (trb.control & TRB_LK_TC) {
  673. ccs = !ccs;
  674. }
  675. continue;
  676. }
  677. length += 1;
  678. dequeue += TRB_SIZE;
  679. if (type == TR_SETUP) {
  680. control_td_set = 1;
  681. } else if (type == TR_STATUS) {
  682. control_td_set = 0;
  683. }
  684. if (!control_td_set && !(trb.control & TRB_TR_CH)) {
  685. return length;
  686. }
  687. /*
  688. * According to the xHCI spec, Transfer Ring segments should have
  689. * a maximum size of 64 kB (see chapter "6 Data Structures")
  690. */
  691. } while (length < TRB_LINK_LIMIT * 65536 / TRB_SIZE);
  692. qemu_log_mask(LOG_GUEST_ERROR, "%s: exceeded maximum transfer ring size!\n",
  693. __func__);
  694. return -1;
  695. }
  696. static void xhci_er_reset(XHCIState *xhci, int v)
  697. {
  698. XHCIInterrupter *intr = &xhci->intr[v];
  699. XHCIEvRingSeg seg;
  700. dma_addr_t erstba = xhci_addr64(intr->erstba_low, intr->erstba_high);
  701. if (intr->erstsz == 0 || erstba == 0) {
  702. /* disabled */
  703. intr->er_start = 0;
  704. intr->er_size = 0;
  705. return;
  706. }
  707. /* cache the (sole) event ring segment location */
  708. if (intr->erstsz != 1) {
  709. DPRINTF("xhci: invalid value for ERSTSZ: %d\n", intr->erstsz);
  710. xhci_die(xhci);
  711. return;
  712. }
  713. if (dma_memory_read(xhci->as, erstba, &seg, sizeof(seg),
  714. MEMTXATTRS_UNSPECIFIED) != MEMTX_OK) {
  715. qemu_log_mask(LOG_GUEST_ERROR, "%s: DMA memory access failed!\n",
  716. __func__);
  717. xhci_die(xhci);
  718. return;
  719. }
  720. le32_to_cpus(&seg.addr_low);
  721. le32_to_cpus(&seg.addr_high);
  722. le32_to_cpus(&seg.size);
  723. if (seg.size < 16 || seg.size > 4096) {
  724. DPRINTF("xhci: invalid value for segment size: %d\n", seg.size);
  725. xhci_die(xhci);
  726. return;
  727. }
  728. intr->er_start = xhci_addr64(seg.addr_low, seg.addr_high);
  729. intr->er_size = seg.size;
  730. intr->er_ep_idx = 0;
  731. intr->er_pcs = 1;
  732. DPRINTF("xhci: event ring[%d]:" DMA_ADDR_FMT " [%d]\n",
  733. v, intr->er_start, intr->er_size);
  734. }
  735. static void xhci_run(XHCIState *xhci)
  736. {
  737. trace_usb_xhci_run();
  738. xhci->usbsts &= ~USBSTS_HCH;
  739. xhci->mfindex_start = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
  740. }
  741. static void xhci_stop(XHCIState *xhci)
  742. {
  743. trace_usb_xhci_stop();
  744. xhci->usbsts |= USBSTS_HCH;
  745. xhci->crcr_low &= ~CRCR_CRR;
  746. }
  747. static XHCIStreamContext *xhci_alloc_stream_contexts(unsigned count,
  748. dma_addr_t base)
  749. {
  750. XHCIStreamContext *stctx;
  751. unsigned int i;
  752. stctx = g_new0(XHCIStreamContext, count);
  753. for (i = 0; i < count; i++) {
  754. stctx[i].pctx = base + i * 16;
  755. stctx[i].sct = -1;
  756. }
  757. return stctx;
  758. }
  759. static void xhci_reset_streams(XHCIEPContext *epctx)
  760. {
  761. unsigned int i;
  762. for (i = 0; i < epctx->nr_pstreams; i++) {
  763. epctx->pstreams[i].sct = -1;
  764. }
  765. }
  766. static void xhci_alloc_streams(XHCIEPContext *epctx, dma_addr_t base)
  767. {
  768. assert(epctx->pstreams == NULL);
  769. epctx->nr_pstreams = 2 << epctx->max_pstreams;
  770. epctx->pstreams = xhci_alloc_stream_contexts(epctx->nr_pstreams, base);
  771. }
  772. static void xhci_free_streams(XHCIEPContext *epctx)
  773. {
  774. assert(epctx->pstreams != NULL);
  775. g_free(epctx->pstreams);
  776. epctx->pstreams = NULL;
  777. epctx->nr_pstreams = 0;
  778. }
  779. static int xhci_epmask_to_eps_with_streams(XHCIState *xhci,
  780. unsigned int slotid,
  781. uint32_t epmask,
  782. XHCIEPContext **epctxs,
  783. USBEndpoint **eps)
  784. {
  785. XHCISlot *slot;
  786. XHCIEPContext *epctx;
  787. USBEndpoint *ep;
  788. int i, j;
  789. assert(slotid >= 1 && slotid <= xhci->numslots);
  790. slot = &xhci->slots[slotid - 1];
  791. for (i = 2, j = 0; i <= 31; i++) {
  792. if (!(epmask & (1u << i))) {
  793. continue;
  794. }
  795. epctx = slot->eps[i - 1];
  796. ep = xhci_epid_to_usbep(epctx);
  797. if (!epctx || !epctx->nr_pstreams || !ep) {
  798. continue;
  799. }
  800. if (epctxs) {
  801. epctxs[j] = epctx;
  802. }
  803. eps[j++] = ep;
  804. }
  805. return j;
  806. }
  807. static void xhci_free_device_streams(XHCIState *xhci, unsigned int slotid,
  808. uint32_t epmask)
  809. {
  810. USBEndpoint *eps[30];
  811. int nr_eps;
  812. nr_eps = xhci_epmask_to_eps_with_streams(xhci, slotid, epmask, NULL, eps);
  813. if (nr_eps) {
  814. usb_device_free_streams(eps[0]->dev, eps, nr_eps);
  815. }
  816. }
  817. static TRBCCode xhci_alloc_device_streams(XHCIState *xhci, unsigned int slotid,
  818. uint32_t epmask)
  819. {
  820. XHCIEPContext *epctxs[30];
  821. USBEndpoint *eps[30];
  822. int i, r, nr_eps, req_nr_streams, dev_max_streams;
  823. nr_eps = xhci_epmask_to_eps_with_streams(xhci, slotid, epmask, epctxs,
  824. eps);
  825. if (nr_eps == 0) {
  826. return CC_SUCCESS;
  827. }
  828. req_nr_streams = epctxs[0]->nr_pstreams;
  829. dev_max_streams = eps[0]->max_streams;
  830. for (i = 1; i < nr_eps; i++) {
  831. /*
  832. * HdG: I don't expect these to ever trigger, but if they do we need
  833. * to come up with another solution, ie group identical endpoints
  834. * together and make an usb_device_alloc_streams call per group.
  835. */
  836. if (epctxs[i]->nr_pstreams != req_nr_streams) {
  837. FIXME("guest streams config not identical for all eps");
  838. return CC_RESOURCE_ERROR;
  839. }
  840. if (eps[i]->max_streams != dev_max_streams) {
  841. FIXME("device streams config not identical for all eps");
  842. return CC_RESOURCE_ERROR;
  843. }
  844. }
  845. /*
  846. * max-streams in both the device descriptor and in the controller is a
  847. * power of 2. But stream id 0 is reserved, so if a device can do up to 4
  848. * streams the guest will ask for 5 rounded up to the next power of 2 which
  849. * becomes 8. For emulated devices usb_device_alloc_streams is a nop.
  850. *
  851. * For redirected devices however this is an issue, as there we must ask
  852. * the real xhci controller to alloc streams, and the host driver for the
  853. * real xhci controller will likely disallow allocating more streams then
  854. * the device can handle.
  855. *
  856. * So we limit the requested nr_streams to the maximum number the device
  857. * can handle.
  858. */
  859. if (req_nr_streams > dev_max_streams) {
  860. req_nr_streams = dev_max_streams;
  861. }
  862. r = usb_device_alloc_streams(eps[0]->dev, eps, nr_eps, req_nr_streams);
  863. if (r != 0) {
  864. DPRINTF("xhci: alloc streams failed\n");
  865. return CC_RESOURCE_ERROR;
  866. }
  867. return CC_SUCCESS;
  868. }
  869. static XHCIStreamContext *xhci_find_stream(XHCIEPContext *epctx,
  870. unsigned int streamid,
  871. uint32_t *cc_error)
  872. {
  873. XHCIStreamContext *sctx;
  874. dma_addr_t base;
  875. uint32_t ctx[2], sct;
  876. assert(streamid != 0);
  877. if (epctx->lsa) {
  878. if (streamid >= epctx->nr_pstreams) {
  879. *cc_error = CC_INVALID_STREAM_ID_ERROR;
  880. return NULL;
  881. }
  882. sctx = epctx->pstreams + streamid;
  883. } else {
  884. fprintf(stderr, "xhci: FIXME: secondary streams not implemented yet");
  885. *cc_error = CC_INVALID_STREAM_TYPE_ERROR;
  886. return NULL;
  887. }
  888. if (sctx->sct == -1) {
  889. xhci_dma_read_u32s(epctx->xhci, sctx->pctx, ctx, sizeof(ctx));
  890. sct = (ctx[0] >> 1) & 0x07;
  891. if (epctx->lsa && sct != 1) {
  892. *cc_error = CC_INVALID_STREAM_TYPE_ERROR;
  893. return NULL;
  894. }
  895. sctx->sct = sct;
  896. base = xhci_addr64(ctx[0] & ~0xf, ctx[1]);
  897. xhci_ring_init(epctx->xhci, &sctx->ring, base);
  898. }
  899. return sctx;
  900. }
  901. static void xhci_set_ep_state(XHCIState *xhci, XHCIEPContext *epctx,
  902. XHCIStreamContext *sctx, uint32_t state)
  903. {
  904. XHCIRing *ring = NULL;
  905. uint32_t ctx[5];
  906. uint32_t ctx2[2];
  907. xhci_dma_read_u32s(xhci, epctx->pctx, ctx, sizeof(ctx));
  908. ctx[0] &= ~EP_STATE_MASK;
  909. ctx[0] |= state;
  910. /* update ring dequeue ptr */
  911. if (epctx->nr_pstreams) {
  912. if (sctx != NULL) {
  913. ring = &sctx->ring;
  914. xhci_dma_read_u32s(xhci, sctx->pctx, ctx2, sizeof(ctx2));
  915. ctx2[0] &= 0xe;
  916. ctx2[0] |= sctx->ring.dequeue | sctx->ring.ccs;
  917. ctx2[1] = (sctx->ring.dequeue >> 16) >> 16;
  918. xhci_dma_write_u32s(xhci, sctx->pctx, ctx2, sizeof(ctx2));
  919. }
  920. } else {
  921. ring = &epctx->ring;
  922. }
  923. if (ring) {
  924. ctx[2] = ring->dequeue | ring->ccs;
  925. ctx[3] = (ring->dequeue >> 16) >> 16;
  926. DPRINTF("xhci: set epctx: " DMA_ADDR_FMT " state=%d dequeue=%08x%08x\n",
  927. epctx->pctx, state, ctx[3], ctx[2]);
  928. }
  929. xhci_dma_write_u32s(xhci, epctx->pctx, ctx, sizeof(ctx));
  930. if (epctx->state != state) {
  931. trace_usb_xhci_ep_state(epctx->slotid, epctx->epid,
  932. ep_state_name(epctx->state),
  933. ep_state_name(state));
  934. }
  935. epctx->state = state;
  936. }
  937. static void xhci_ep_kick_timer(void *opaque)
  938. {
  939. XHCIEPContext *epctx = opaque;
  940. xhci_kick_epctx(epctx, 0);
  941. }
  942. static XHCIEPContext *xhci_alloc_epctx(XHCIState *xhci,
  943. unsigned int slotid,
  944. unsigned int epid)
  945. {
  946. XHCIEPContext *epctx;
  947. epctx = g_new0(XHCIEPContext, 1);
  948. epctx->xhci = xhci;
  949. epctx->slotid = slotid;
  950. epctx->epid = epid;
  951. QTAILQ_INIT(&epctx->transfers);
  952. epctx->kick_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, xhci_ep_kick_timer, epctx);
  953. return epctx;
  954. }
  955. static void xhci_init_epctx(XHCIEPContext *epctx,
  956. dma_addr_t pctx, uint32_t *ctx)
  957. {
  958. dma_addr_t dequeue;
  959. dequeue = xhci_addr64(ctx[2] & ~0xf, ctx[3]);
  960. epctx->type = (ctx[1] >> EP_TYPE_SHIFT) & EP_TYPE_MASK;
  961. epctx->pctx = pctx;
  962. epctx->max_psize = ctx[1]>>16;
  963. epctx->max_psize *= 1+((ctx[1]>>8)&0xff);
  964. epctx->max_pstreams = (ctx[0] >> 10) & epctx->xhci->max_pstreams_mask;
  965. epctx->lsa = (ctx[0] >> 15) & 1;
  966. if (epctx->max_pstreams) {
  967. xhci_alloc_streams(epctx, dequeue);
  968. } else {
  969. xhci_ring_init(epctx->xhci, &epctx->ring, dequeue);
  970. epctx->ring.ccs = ctx[2] & 1;
  971. }
  972. epctx->interval = 1 << ((ctx[0] >> 16) & 0xff);
  973. }
  974. static TRBCCode xhci_enable_ep(XHCIState *xhci, unsigned int slotid,
  975. unsigned int epid, dma_addr_t pctx,
  976. uint32_t *ctx)
  977. {
  978. XHCISlot *slot;
  979. XHCIEPContext *epctx;
  980. trace_usb_xhci_ep_enable(slotid, epid);
  981. assert(slotid >= 1 && slotid <= xhci->numslots);
  982. assert(epid >= 1 && epid <= 31);
  983. slot = &xhci->slots[slotid-1];
  984. if (slot->eps[epid-1]) {
  985. xhci_disable_ep(xhci, slotid, epid);
  986. }
  987. epctx = xhci_alloc_epctx(xhci, slotid, epid);
  988. slot->eps[epid-1] = epctx;
  989. xhci_init_epctx(epctx, pctx, ctx);
  990. DPRINTF("xhci: endpoint %d.%d type is %d, max transaction (burst) "
  991. "size is %d\n", epid/2, epid%2, epctx->type, epctx->max_psize);
  992. epctx->mfindex_last = 0;
  993. epctx->state = EP_RUNNING;
  994. ctx[0] &= ~EP_STATE_MASK;
  995. ctx[0] |= EP_RUNNING;
  996. return CC_SUCCESS;
  997. }
  998. static XHCITransfer *xhci_ep_alloc_xfer(XHCIEPContext *epctx,
  999. uint32_t length)
  1000. {
  1001. uint32_t limit = epctx->nr_pstreams + 16;
  1002. XHCITransfer *xfer;
  1003. if (epctx->xfer_count >= limit) {
  1004. return NULL;
  1005. }
  1006. xfer = g_new0(XHCITransfer, 1);
  1007. xfer->epctx = epctx;
  1008. xfer->trbs = g_new(XHCITRB, length);
  1009. xfer->trb_count = length;
  1010. usb_packet_init(&xfer->packet);
  1011. QTAILQ_INSERT_TAIL(&epctx->transfers, xfer, next);
  1012. epctx->xfer_count++;
  1013. return xfer;
  1014. }
  1015. static void xhci_ep_free_xfer(XHCITransfer *xfer)
  1016. {
  1017. QTAILQ_REMOVE(&xfer->epctx->transfers, xfer, next);
  1018. xfer->epctx->xfer_count--;
  1019. usb_packet_cleanup(&xfer->packet);
  1020. g_free(xfer->trbs);
  1021. g_free(xfer);
  1022. }
  1023. static int xhci_ep_nuke_one_xfer(XHCITransfer *t, TRBCCode report)
  1024. {
  1025. int killed = 0;
  1026. if (report && (t->running_async || t->running_retry)) {
  1027. t->status = report;
  1028. xhci_xfer_report(t);
  1029. }
  1030. if (t->running_async) {
  1031. usb_cancel_packet(&t->packet);
  1032. t->running_async = 0;
  1033. killed = 1;
  1034. }
  1035. if (t->running_retry) {
  1036. if (t->epctx) {
  1037. t->epctx->retry = NULL;
  1038. timer_del(t->epctx->kick_timer);
  1039. }
  1040. t->running_retry = 0;
  1041. killed = 1;
  1042. }
  1043. g_free(t->trbs);
  1044. t->trbs = NULL;
  1045. t->trb_count = 0;
  1046. return killed;
  1047. }
  1048. static int xhci_ep_nuke_xfers(XHCIState *xhci, unsigned int slotid,
  1049. unsigned int epid, TRBCCode report)
  1050. {
  1051. XHCISlot *slot;
  1052. XHCIEPContext *epctx;
  1053. XHCITransfer *xfer;
  1054. int killed = 0;
  1055. USBEndpoint *ep = NULL;
  1056. assert(slotid >= 1 && slotid <= xhci->numslots);
  1057. assert(epid >= 1 && epid <= 31);
  1058. DPRINTF("xhci_ep_nuke_xfers(%d, %d)\n", slotid, epid);
  1059. slot = &xhci->slots[slotid-1];
  1060. if (!slot->eps[epid-1]) {
  1061. return 0;
  1062. }
  1063. epctx = slot->eps[epid-1];
  1064. for (;;) {
  1065. xfer = QTAILQ_FIRST(&epctx->transfers);
  1066. if (xfer == NULL) {
  1067. break;
  1068. }
  1069. killed += xhci_ep_nuke_one_xfer(xfer, report);
  1070. if (killed) {
  1071. report = 0; /* Only report once */
  1072. }
  1073. xhci_ep_free_xfer(xfer);
  1074. }
  1075. ep = xhci_epid_to_usbep(epctx);
  1076. if (ep) {
  1077. usb_device_ep_stopped(ep->dev, ep);
  1078. }
  1079. return killed;
  1080. }
  1081. static TRBCCode xhci_disable_ep(XHCIState *xhci, unsigned int slotid,
  1082. unsigned int epid)
  1083. {
  1084. XHCISlot *slot;
  1085. XHCIEPContext *epctx;
  1086. trace_usb_xhci_ep_disable(slotid, epid);
  1087. assert(slotid >= 1 && slotid <= xhci->numslots);
  1088. assert(epid >= 1 && epid <= 31);
  1089. slot = &xhci->slots[slotid-1];
  1090. if (!slot->eps[epid-1]) {
  1091. DPRINTF("xhci: slot %d ep %d already disabled\n", slotid, epid);
  1092. return CC_SUCCESS;
  1093. }
  1094. xhci_ep_nuke_xfers(xhci, slotid, epid, 0);
  1095. epctx = slot->eps[epid-1];
  1096. if (epctx->nr_pstreams) {
  1097. xhci_free_streams(epctx);
  1098. }
  1099. /* only touch guest RAM if we're not resetting the HC */
  1100. if (xhci->dcbaap_low || xhci->dcbaap_high) {
  1101. xhci_set_ep_state(xhci, epctx, NULL, EP_DISABLED);
  1102. }
  1103. timer_free(epctx->kick_timer);
  1104. g_free(epctx);
  1105. slot->eps[epid-1] = NULL;
  1106. return CC_SUCCESS;
  1107. }
  1108. static TRBCCode xhci_stop_ep(XHCIState *xhci, unsigned int slotid,
  1109. unsigned int epid)
  1110. {
  1111. XHCISlot *slot;
  1112. XHCIEPContext *epctx;
  1113. trace_usb_xhci_ep_stop(slotid, epid);
  1114. assert(slotid >= 1 && slotid <= xhci->numslots);
  1115. if (epid < 1 || epid > 31) {
  1116. DPRINTF("xhci: bad ep %d\n", epid);
  1117. return CC_TRB_ERROR;
  1118. }
  1119. slot = &xhci->slots[slotid-1];
  1120. if (!slot->eps[epid-1]) {
  1121. DPRINTF("xhci: slot %d ep %d not enabled\n", slotid, epid);
  1122. return CC_EP_NOT_ENABLED_ERROR;
  1123. }
  1124. if (xhci_ep_nuke_xfers(xhci, slotid, epid, CC_STOPPED) > 0) {
  1125. DPRINTF("xhci: FIXME: endpoint stopped w/ xfers running, "
  1126. "data might be lost\n");
  1127. }
  1128. epctx = slot->eps[epid-1];
  1129. xhci_set_ep_state(xhci, epctx, NULL, EP_STOPPED);
  1130. if (epctx->nr_pstreams) {
  1131. xhci_reset_streams(epctx);
  1132. }
  1133. return CC_SUCCESS;
  1134. }
  1135. static TRBCCode xhci_reset_ep(XHCIState *xhci, unsigned int slotid,
  1136. unsigned int epid)
  1137. {
  1138. XHCISlot *slot;
  1139. XHCIEPContext *epctx;
  1140. trace_usb_xhci_ep_reset(slotid, epid);
  1141. assert(slotid >= 1 && slotid <= xhci->numslots);
  1142. if (epid < 1 || epid > 31) {
  1143. DPRINTF("xhci: bad ep %d\n", epid);
  1144. return CC_TRB_ERROR;
  1145. }
  1146. slot = &xhci->slots[slotid-1];
  1147. if (!slot->eps[epid-1]) {
  1148. DPRINTF("xhci: slot %d ep %d not enabled\n", slotid, epid);
  1149. return CC_EP_NOT_ENABLED_ERROR;
  1150. }
  1151. epctx = slot->eps[epid-1];
  1152. if (epctx->state != EP_HALTED) {
  1153. DPRINTF("xhci: reset EP while EP %d not halted (%d)\n",
  1154. epid, epctx->state);
  1155. return CC_CONTEXT_STATE_ERROR;
  1156. }
  1157. if (xhci_ep_nuke_xfers(xhci, slotid, epid, 0) > 0) {
  1158. DPRINTF("xhci: FIXME: endpoint reset w/ xfers running, "
  1159. "data might be lost\n");
  1160. }
  1161. if (!xhci->slots[slotid-1].uport ||
  1162. !xhci->slots[slotid-1].uport->dev ||
  1163. !xhci->slots[slotid-1].uport->dev->attached) {
  1164. return CC_USB_TRANSACTION_ERROR;
  1165. }
  1166. xhci_set_ep_state(xhci, epctx, NULL, EP_STOPPED);
  1167. if (epctx->nr_pstreams) {
  1168. xhci_reset_streams(epctx);
  1169. }
  1170. return CC_SUCCESS;
  1171. }
  1172. static TRBCCode xhci_set_ep_dequeue(XHCIState *xhci, unsigned int slotid,
  1173. unsigned int epid, unsigned int streamid,
  1174. uint64_t pdequeue)
  1175. {
  1176. XHCISlot *slot;
  1177. XHCIEPContext *epctx;
  1178. XHCIStreamContext *sctx;
  1179. dma_addr_t dequeue;
  1180. assert(slotid >= 1 && slotid <= xhci->numslots);
  1181. if (epid < 1 || epid > 31) {
  1182. DPRINTF("xhci: bad ep %d\n", epid);
  1183. return CC_TRB_ERROR;
  1184. }
  1185. trace_usb_xhci_ep_set_dequeue(slotid, epid, streamid, pdequeue);
  1186. dequeue = xhci_mask64(pdequeue);
  1187. slot = &xhci->slots[slotid-1];
  1188. if (!slot->eps[epid-1]) {
  1189. DPRINTF("xhci: slot %d ep %d not enabled\n", slotid, epid);
  1190. return CC_EP_NOT_ENABLED_ERROR;
  1191. }
  1192. epctx = slot->eps[epid-1];
  1193. if (epctx->state != EP_STOPPED) {
  1194. DPRINTF("xhci: set EP dequeue pointer while EP %d not stopped\n", epid);
  1195. return CC_CONTEXT_STATE_ERROR;
  1196. }
  1197. if (epctx->nr_pstreams) {
  1198. uint32_t err;
  1199. sctx = xhci_find_stream(epctx, streamid, &err);
  1200. if (sctx == NULL) {
  1201. return err;
  1202. }
  1203. xhci_ring_init(xhci, &sctx->ring, dequeue & ~0xf);
  1204. sctx->ring.ccs = dequeue & 1;
  1205. } else {
  1206. sctx = NULL;
  1207. xhci_ring_init(xhci, &epctx->ring, dequeue & ~0xF);
  1208. epctx->ring.ccs = dequeue & 1;
  1209. }
  1210. xhci_set_ep_state(xhci, epctx, sctx, EP_STOPPED);
  1211. return CC_SUCCESS;
  1212. }
  1213. static int xhci_xfer_create_sgl(XHCITransfer *xfer, int in_xfer)
  1214. {
  1215. XHCIState *xhci = xfer->epctx->xhci;
  1216. int i;
  1217. xfer->int_req = false;
  1218. qemu_sglist_init(&xfer->sgl, DEVICE(xhci), xfer->trb_count, xhci->as);
  1219. for (i = 0; i < xfer->trb_count; i++) {
  1220. XHCITRB *trb = &xfer->trbs[i];
  1221. dma_addr_t addr;
  1222. unsigned int chunk = 0;
  1223. if (trb->control & TRB_TR_IOC) {
  1224. xfer->int_req = true;
  1225. }
  1226. switch (TRB_TYPE(*trb)) {
  1227. case TR_DATA:
  1228. if ((!(trb->control & TRB_TR_DIR)) != (!in_xfer)) {
  1229. DPRINTF("xhci: data direction mismatch for TR_DATA\n");
  1230. goto err;
  1231. }
  1232. /* fallthrough */
  1233. case TR_NORMAL:
  1234. case TR_ISOCH:
  1235. addr = xhci_mask64(trb->parameter);
  1236. chunk = trb->status & 0x1ffff;
  1237. if (trb->control & TRB_TR_IDT) {
  1238. if (chunk > 8 || in_xfer) {
  1239. DPRINTF("xhci: invalid immediate data TRB\n");
  1240. goto err;
  1241. }
  1242. qemu_sglist_add(&xfer->sgl, trb->addr, chunk);
  1243. } else {
  1244. qemu_sglist_add(&xfer->sgl, addr, chunk);
  1245. }
  1246. break;
  1247. }
  1248. }
  1249. return 0;
  1250. err:
  1251. qemu_sglist_destroy(&xfer->sgl);
  1252. xhci_die(xhci);
  1253. return -1;
  1254. }
  1255. static void xhci_xfer_unmap(XHCITransfer *xfer)
  1256. {
  1257. usb_packet_unmap(&xfer->packet, &xfer->sgl);
  1258. qemu_sglist_destroy(&xfer->sgl);
  1259. }
  1260. static void xhci_xfer_report(XHCITransfer *xfer)
  1261. {
  1262. uint32_t edtla = 0;
  1263. unsigned int left;
  1264. bool reported = 0;
  1265. bool shortpkt = 0;
  1266. XHCIEvent event = {ER_TRANSFER, CC_SUCCESS};
  1267. XHCIState *xhci = xfer->epctx->xhci;
  1268. int i;
  1269. left = xfer->packet.actual_length;
  1270. for (i = 0; i < xfer->trb_count; i++) {
  1271. XHCITRB *trb = &xfer->trbs[i];
  1272. unsigned int chunk = 0;
  1273. switch (TRB_TYPE(*trb)) {
  1274. case TR_SETUP:
  1275. chunk = trb->status & 0x1ffff;
  1276. if (chunk > 8) {
  1277. chunk = 8;
  1278. }
  1279. break;
  1280. case TR_DATA:
  1281. case TR_NORMAL:
  1282. case TR_ISOCH:
  1283. chunk = trb->status & 0x1ffff;
  1284. if (chunk > left) {
  1285. chunk = left;
  1286. if (xfer->status == CC_SUCCESS) {
  1287. shortpkt = 1;
  1288. }
  1289. }
  1290. left -= chunk;
  1291. edtla += chunk;
  1292. break;
  1293. case TR_STATUS:
  1294. reported = 0;
  1295. shortpkt = 0;
  1296. break;
  1297. }
  1298. if (!reported && ((trb->control & TRB_TR_IOC) ||
  1299. (shortpkt && (trb->control & TRB_TR_ISP)) ||
  1300. (xfer->status != CC_SUCCESS && left == 0))) {
  1301. event.slotid = xfer->epctx->slotid;
  1302. event.epid = xfer->epctx->epid;
  1303. event.length = (trb->status & 0x1ffff) - chunk;
  1304. event.flags = 0;
  1305. event.ptr = trb->addr;
  1306. if (xfer->status == CC_SUCCESS) {
  1307. event.ccode = shortpkt ? CC_SHORT_PACKET : CC_SUCCESS;
  1308. } else {
  1309. event.ccode = xfer->status;
  1310. }
  1311. if (TRB_TYPE(*trb) == TR_EVDATA) {
  1312. event.ptr = trb->parameter;
  1313. event.flags |= TRB_EV_ED;
  1314. event.length = edtla & 0xffffff;
  1315. DPRINTF("xhci_xfer_data: EDTLA=%d\n", event.length);
  1316. edtla = 0;
  1317. }
  1318. xhci_event(xhci, &event, TRB_INTR(*trb));
  1319. reported = 1;
  1320. if (xfer->status != CC_SUCCESS) {
  1321. return;
  1322. }
  1323. }
  1324. switch (TRB_TYPE(*trb)) {
  1325. case TR_SETUP:
  1326. reported = 0;
  1327. shortpkt = 0;
  1328. break;
  1329. }
  1330. }
  1331. }
  1332. static void xhci_stall_ep(XHCITransfer *xfer)
  1333. {
  1334. XHCIEPContext *epctx = xfer->epctx;
  1335. XHCIState *xhci = epctx->xhci;
  1336. uint32_t err;
  1337. XHCIStreamContext *sctx;
  1338. if (epctx->type == ET_ISO_IN || epctx->type == ET_ISO_OUT) {
  1339. /* never halt isoch endpoints, 4.10.2 */
  1340. return;
  1341. }
  1342. if (epctx->nr_pstreams) {
  1343. sctx = xhci_find_stream(epctx, xfer->streamid, &err);
  1344. if (sctx == NULL) {
  1345. return;
  1346. }
  1347. sctx->ring.dequeue = xfer->trbs[0].addr;
  1348. sctx->ring.ccs = xfer->trbs[0].ccs;
  1349. xhci_set_ep_state(xhci, epctx, sctx, EP_HALTED);
  1350. } else {
  1351. epctx->ring.dequeue = xfer->trbs[0].addr;
  1352. epctx->ring.ccs = xfer->trbs[0].ccs;
  1353. xhci_set_ep_state(xhci, epctx, NULL, EP_HALTED);
  1354. }
  1355. }
  1356. static int xhci_setup_packet(XHCITransfer *xfer)
  1357. {
  1358. USBEndpoint *ep;
  1359. int dir;
  1360. dir = xfer->in_xfer ? USB_TOKEN_IN : USB_TOKEN_OUT;
  1361. if (xfer->packet.ep) {
  1362. ep = xfer->packet.ep;
  1363. } else {
  1364. ep = xhci_epid_to_usbep(xfer->epctx);
  1365. if (!ep) {
  1366. DPRINTF("xhci: slot %d has no device\n",
  1367. xfer->epctx->slotid);
  1368. return -1;
  1369. }
  1370. }
  1371. xhci_xfer_create_sgl(xfer, dir == USB_TOKEN_IN); /* Also sets int_req */
  1372. usb_packet_setup(&xfer->packet, dir, ep, xfer->streamid,
  1373. xfer->trbs[0].addr, false, xfer->int_req);
  1374. if (usb_packet_map(&xfer->packet, &xfer->sgl)) {
  1375. qemu_sglist_destroy(&xfer->sgl);
  1376. return -1;
  1377. }
  1378. DPRINTF("xhci: setup packet pid 0x%x addr %d ep %d\n",
  1379. xfer->packet.pid, ep->dev->addr, ep->nr);
  1380. return 0;
  1381. }
  1382. static int xhci_try_complete_packet(XHCITransfer *xfer)
  1383. {
  1384. if (xfer->packet.status == USB_RET_ASYNC) {
  1385. trace_usb_xhci_xfer_async(xfer);
  1386. xfer->running_async = 1;
  1387. xfer->running_retry = 0;
  1388. xfer->complete = 0;
  1389. return 0;
  1390. } else if (xfer->packet.status == USB_RET_NAK) {
  1391. trace_usb_xhci_xfer_nak(xfer);
  1392. xfer->running_async = 0;
  1393. xfer->running_retry = 1;
  1394. xfer->complete = 0;
  1395. return 0;
  1396. } else {
  1397. xfer->running_async = 0;
  1398. xfer->running_retry = 0;
  1399. xfer->complete = 1;
  1400. xhci_xfer_unmap(xfer);
  1401. }
  1402. if (xfer->packet.status == USB_RET_SUCCESS) {
  1403. trace_usb_xhci_xfer_success(xfer, xfer->packet.actual_length);
  1404. xfer->status = CC_SUCCESS;
  1405. xhci_xfer_report(xfer);
  1406. return 0;
  1407. }
  1408. /* error */
  1409. trace_usb_xhci_xfer_error(xfer, xfer->packet.status);
  1410. switch (xfer->packet.status) {
  1411. case USB_RET_NODEV:
  1412. case USB_RET_IOERROR:
  1413. xfer->status = CC_USB_TRANSACTION_ERROR;
  1414. xhci_xfer_report(xfer);
  1415. xhci_stall_ep(xfer);
  1416. break;
  1417. case USB_RET_STALL:
  1418. xfer->status = CC_STALL_ERROR;
  1419. xhci_xfer_report(xfer);
  1420. xhci_stall_ep(xfer);
  1421. break;
  1422. case USB_RET_BABBLE:
  1423. xfer->status = CC_BABBLE_DETECTED;
  1424. xhci_xfer_report(xfer);
  1425. xhci_stall_ep(xfer);
  1426. break;
  1427. default:
  1428. DPRINTF("%s: FIXME: status = %d\n", __func__,
  1429. xfer->packet.status);
  1430. FIXME("unhandled USB_RET_*");
  1431. }
  1432. return 0;
  1433. }
  1434. static int xhci_fire_ctl_transfer(XHCIState *xhci, XHCITransfer *xfer)
  1435. {
  1436. XHCITRB *trb_setup, *trb_status;
  1437. uint8_t bmRequestType;
  1438. trb_setup = &xfer->trbs[0];
  1439. trb_status = &xfer->trbs[xfer->trb_count-1];
  1440. trace_usb_xhci_xfer_start(xfer, xfer->epctx->slotid,
  1441. xfer->epctx->epid, xfer->streamid);
  1442. /* at most one Event Data TRB allowed after STATUS */
  1443. if (TRB_TYPE(*trb_status) == TR_EVDATA && xfer->trb_count > 2) {
  1444. trb_status--;
  1445. }
  1446. /* do some sanity checks */
  1447. if (TRB_TYPE(*trb_setup) != TR_SETUP) {
  1448. DPRINTF("xhci: ep0 first TD not SETUP: %d\n",
  1449. TRB_TYPE(*trb_setup));
  1450. return -1;
  1451. }
  1452. if (TRB_TYPE(*trb_status) != TR_STATUS) {
  1453. DPRINTF("xhci: ep0 last TD not STATUS: %d\n",
  1454. TRB_TYPE(*trb_status));
  1455. return -1;
  1456. }
  1457. if (!(trb_setup->control & TRB_TR_IDT)) {
  1458. DPRINTF("xhci: Setup TRB doesn't have IDT set\n");
  1459. return -1;
  1460. }
  1461. if ((trb_setup->status & 0x1ffff) != 8) {
  1462. DPRINTF("xhci: Setup TRB has bad length (%d)\n",
  1463. (trb_setup->status & 0x1ffff));
  1464. return -1;
  1465. }
  1466. bmRequestType = trb_setup->parameter;
  1467. xfer->in_xfer = bmRequestType & USB_DIR_IN;
  1468. xfer->iso_xfer = false;
  1469. xfer->timed_xfer = false;
  1470. if (xhci_setup_packet(xfer) < 0) {
  1471. return -1;
  1472. }
  1473. xfer->packet.parameter = trb_setup->parameter;
  1474. usb_handle_packet(xfer->packet.ep->dev, &xfer->packet);
  1475. xhci_try_complete_packet(xfer);
  1476. return 0;
  1477. }
  1478. static void xhci_calc_intr_kick(XHCIState *xhci, XHCITransfer *xfer,
  1479. XHCIEPContext *epctx, uint64_t mfindex)
  1480. {
  1481. uint64_t asap = ((mfindex + epctx->interval - 1) &
  1482. ~(epctx->interval-1));
  1483. uint64_t kick = epctx->mfindex_last + epctx->interval;
  1484. assert(epctx->interval != 0);
  1485. xfer->mfindex_kick = MAX(asap, kick);
  1486. }
  1487. static void xhci_calc_iso_kick(XHCIState *xhci, XHCITransfer *xfer,
  1488. XHCIEPContext *epctx, uint64_t mfindex)
  1489. {
  1490. if (xfer->trbs[0].control & TRB_TR_SIA) {
  1491. uint64_t asap = ((mfindex + epctx->interval - 1) &
  1492. ~(epctx->interval-1));
  1493. if (asap >= epctx->mfindex_last &&
  1494. asap <= epctx->mfindex_last + epctx->interval * 4) {
  1495. xfer->mfindex_kick = epctx->mfindex_last + epctx->interval;
  1496. } else {
  1497. xfer->mfindex_kick = asap;
  1498. }
  1499. } else {
  1500. xfer->mfindex_kick = ((xfer->trbs[0].control >> TRB_TR_FRAMEID_SHIFT)
  1501. & TRB_TR_FRAMEID_MASK) << 3;
  1502. xfer->mfindex_kick |= mfindex & ~0x3fff;
  1503. if (xfer->mfindex_kick + 0x100 < mfindex) {
  1504. xfer->mfindex_kick += 0x4000;
  1505. }
  1506. }
  1507. }
  1508. static void xhci_check_intr_iso_kick(XHCIState *xhci, XHCITransfer *xfer,
  1509. XHCIEPContext *epctx, uint64_t mfindex)
  1510. {
  1511. if (xfer->mfindex_kick > mfindex) {
  1512. timer_mod(epctx->kick_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
  1513. (xfer->mfindex_kick - mfindex) * 125000);
  1514. xfer->running_retry = 1;
  1515. } else {
  1516. epctx->mfindex_last = xfer->mfindex_kick;
  1517. timer_del(epctx->kick_timer);
  1518. xfer->running_retry = 0;
  1519. }
  1520. }
  1521. static int xhci_submit(XHCIState *xhci, XHCITransfer *xfer, XHCIEPContext *epctx)
  1522. {
  1523. uint64_t mfindex;
  1524. DPRINTF("xhci_submit(slotid=%d,epid=%d)\n", epctx->slotid, epctx->epid);
  1525. xfer->in_xfer = epctx->type>>2;
  1526. switch(epctx->type) {
  1527. case ET_INTR_OUT:
  1528. case ET_INTR_IN:
  1529. xfer->pkts = 0;
  1530. xfer->iso_xfer = false;
  1531. xfer->timed_xfer = true;
  1532. mfindex = xhci_mfindex_get(xhci);
  1533. xhci_calc_intr_kick(xhci, xfer, epctx, mfindex);
  1534. xhci_check_intr_iso_kick(xhci, xfer, epctx, mfindex);
  1535. if (xfer->running_retry) {
  1536. return -1;
  1537. }
  1538. break;
  1539. case ET_BULK_OUT:
  1540. case ET_BULK_IN:
  1541. xfer->pkts = 0;
  1542. xfer->iso_xfer = false;
  1543. xfer->timed_xfer = false;
  1544. break;
  1545. case ET_ISO_OUT:
  1546. case ET_ISO_IN:
  1547. xfer->pkts = 1;
  1548. xfer->iso_xfer = true;
  1549. xfer->timed_xfer = true;
  1550. mfindex = xhci_mfindex_get(xhci);
  1551. xhci_calc_iso_kick(xhci, xfer, epctx, mfindex);
  1552. xhci_check_intr_iso_kick(xhci, xfer, epctx, mfindex);
  1553. if (xfer->running_retry) {
  1554. return -1;
  1555. }
  1556. break;
  1557. default:
  1558. trace_usb_xhci_unimplemented("endpoint type", epctx->type);
  1559. return -1;
  1560. }
  1561. if (xhci_setup_packet(xfer) < 0) {
  1562. return -1;
  1563. }
  1564. usb_handle_packet(xfer->packet.ep->dev, &xfer->packet);
  1565. xhci_try_complete_packet(xfer);
  1566. return 0;
  1567. }
  1568. static int xhci_fire_transfer(XHCIState *xhci, XHCITransfer *xfer, XHCIEPContext *epctx)
  1569. {
  1570. trace_usb_xhci_xfer_start(xfer, xfer->epctx->slotid,
  1571. xfer->epctx->epid, xfer->streamid);
  1572. return xhci_submit(xhci, xfer, epctx);
  1573. }
  1574. static void xhci_kick_ep(XHCIState *xhci, unsigned int slotid,
  1575. unsigned int epid, unsigned int streamid)
  1576. {
  1577. XHCIEPContext *epctx;
  1578. assert(slotid >= 1 && slotid <= xhci->numslots);
  1579. assert(epid >= 1 && epid <= 31);
  1580. if (!xhci->slots[slotid-1].enabled) {
  1581. DPRINTF("xhci: xhci_kick_ep for disabled slot %d\n", slotid);
  1582. return;
  1583. }
  1584. epctx = xhci->slots[slotid-1].eps[epid-1];
  1585. if (!epctx) {
  1586. DPRINTF("xhci: xhci_kick_ep for disabled endpoint %d,%d\n",
  1587. epid, slotid);
  1588. return;
  1589. }
  1590. if (epctx->kick_active) {
  1591. return;
  1592. }
  1593. xhci_kick_epctx(epctx, streamid);
  1594. }
  1595. static bool xhci_slot_ok(XHCIState *xhci, int slotid)
  1596. {
  1597. return (xhci->slots[slotid - 1].uport &&
  1598. xhci->slots[slotid - 1].uport->dev &&
  1599. xhci->slots[slotid - 1].uport->dev->attached);
  1600. }
  1601. static void xhci_kick_epctx(XHCIEPContext *epctx, unsigned int streamid)
  1602. {
  1603. XHCIState *xhci = epctx->xhci;
  1604. XHCIStreamContext *stctx = NULL;
  1605. XHCITransfer *xfer;
  1606. XHCIRing *ring;
  1607. USBEndpoint *ep = NULL;
  1608. uint64_t mfindex;
  1609. unsigned int count = 0;
  1610. int length;
  1611. int i;
  1612. trace_usb_xhci_ep_kick(epctx->slotid, epctx->epid, streamid);
  1613. assert(!epctx->kick_active);
  1614. /* If the device has been detached, but the guest has not noticed this
  1615. yet the 2 above checks will succeed, but we must NOT continue */
  1616. if (!xhci_slot_ok(xhci, epctx->slotid)) {
  1617. return;
  1618. }
  1619. if (epctx->retry) {
  1620. XHCITransfer *xfer = epctx->retry;
  1621. trace_usb_xhci_xfer_retry(xfer);
  1622. assert(xfer->running_retry);
  1623. if (xfer->timed_xfer) {
  1624. /* time to kick the transfer? */
  1625. mfindex = xhci_mfindex_get(xhci);
  1626. xhci_check_intr_iso_kick(xhci, xfer, epctx, mfindex);
  1627. if (xfer->running_retry) {
  1628. return;
  1629. }
  1630. xfer->timed_xfer = 0;
  1631. xfer->running_retry = 1;
  1632. }
  1633. if (xfer->iso_xfer) {
  1634. /* retry iso transfer */
  1635. if (xhci_setup_packet(xfer) < 0) {
  1636. return;
  1637. }
  1638. usb_handle_packet(xfer->packet.ep->dev, &xfer->packet);
  1639. assert(xfer->packet.status != USB_RET_NAK);
  1640. xhci_try_complete_packet(xfer);
  1641. } else {
  1642. /* retry nak'ed transfer */
  1643. if (xhci_setup_packet(xfer) < 0) {
  1644. return;
  1645. }
  1646. usb_handle_packet(xfer->packet.ep->dev, &xfer->packet);
  1647. if (xfer->packet.status == USB_RET_NAK) {
  1648. xhci_xfer_unmap(xfer);
  1649. return;
  1650. }
  1651. xhci_try_complete_packet(xfer);
  1652. }
  1653. assert(!xfer->running_retry);
  1654. if (xfer->complete) {
  1655. /* update ring dequeue ptr */
  1656. xhci_set_ep_state(xhci, epctx, stctx, epctx->state);
  1657. xhci_ep_free_xfer(epctx->retry);
  1658. }
  1659. epctx->retry = NULL;
  1660. }
  1661. if (epctx->state == EP_HALTED) {
  1662. DPRINTF("xhci: ep halted, not running schedule\n");
  1663. return;
  1664. }
  1665. if (epctx->nr_pstreams) {
  1666. uint32_t err;
  1667. stctx = xhci_find_stream(epctx, streamid, &err);
  1668. if (stctx == NULL) {
  1669. return;
  1670. }
  1671. ring = &stctx->ring;
  1672. xhci_set_ep_state(xhci, epctx, stctx, EP_RUNNING);
  1673. } else {
  1674. ring = &epctx->ring;
  1675. streamid = 0;
  1676. xhci_set_ep_state(xhci, epctx, NULL, EP_RUNNING);
  1677. }
  1678. if (!ring->dequeue) {
  1679. return;
  1680. }
  1681. epctx->kick_active++;
  1682. while (1) {
  1683. length = xhci_ring_chain_length(xhci, ring);
  1684. if (length <= 0) {
  1685. if (epctx->type == ET_ISO_OUT || epctx->type == ET_ISO_IN) {
  1686. /* 4.10.3.1 */
  1687. XHCIEvent ev = { ER_TRANSFER };
  1688. ev.ccode = epctx->type == ET_ISO_IN ?
  1689. CC_RING_OVERRUN : CC_RING_UNDERRUN;
  1690. ev.slotid = epctx->slotid;
  1691. ev.epid = epctx->epid;
  1692. ev.ptr = epctx->ring.dequeue;
  1693. xhci_event(xhci, &ev, xhci->slots[epctx->slotid-1].intr);
  1694. }
  1695. break;
  1696. }
  1697. xfer = xhci_ep_alloc_xfer(epctx, length);
  1698. if (xfer == NULL) {
  1699. break;
  1700. }
  1701. for (i = 0; i < length; i++) {
  1702. TRBType type;
  1703. type = xhci_ring_fetch(xhci, ring, &xfer->trbs[i], NULL);
  1704. if (!type) {
  1705. xhci_die(xhci);
  1706. xhci_ep_free_xfer(xfer);
  1707. epctx->kick_active--;
  1708. return;
  1709. }
  1710. }
  1711. xfer->streamid = streamid;
  1712. if (epctx->epid == 1) {
  1713. xhci_fire_ctl_transfer(xhci, xfer);
  1714. } else {
  1715. xhci_fire_transfer(xhci, xfer, epctx);
  1716. }
  1717. if (!xhci_slot_ok(xhci, epctx->slotid)) {
  1718. /* surprise removal -> stop processing */
  1719. break;
  1720. }
  1721. if (xfer->complete) {
  1722. /* update ring dequeue ptr */
  1723. xhci_set_ep_state(xhci, epctx, stctx, epctx->state);
  1724. xhci_ep_free_xfer(xfer);
  1725. xfer = NULL;
  1726. }
  1727. if (epctx->state == EP_HALTED) {
  1728. break;
  1729. }
  1730. if (xfer != NULL && xfer->running_retry) {
  1731. DPRINTF("xhci: xfer nacked, stopping schedule\n");
  1732. epctx->retry = xfer;
  1733. xhci_xfer_unmap(xfer);
  1734. break;
  1735. }
  1736. if (count++ > TRANSFER_LIMIT) {
  1737. trace_usb_xhci_enforced_limit("transfers");
  1738. break;
  1739. }
  1740. }
  1741. epctx->kick_active--;
  1742. ep = xhci_epid_to_usbep(epctx);
  1743. if (ep) {
  1744. usb_device_flush_ep_queue(ep->dev, ep);
  1745. }
  1746. }
  1747. static TRBCCode xhci_enable_slot(XHCIState *xhci, unsigned int slotid)
  1748. {
  1749. trace_usb_xhci_slot_enable(slotid);
  1750. assert(slotid >= 1 && slotid <= xhci->numslots);
  1751. xhci->slots[slotid-1].enabled = 1;
  1752. xhci->slots[slotid-1].uport = NULL;
  1753. memset(xhci->slots[slotid-1].eps, 0, sizeof(XHCIEPContext*)*31);
  1754. return CC_SUCCESS;
  1755. }
  1756. static TRBCCode xhci_disable_slot(XHCIState *xhci, unsigned int slotid)
  1757. {
  1758. int i;
  1759. trace_usb_xhci_slot_disable(slotid);
  1760. assert(slotid >= 1 && slotid <= xhci->numslots);
  1761. for (i = 1; i <= 31; i++) {
  1762. if (xhci->slots[slotid-1].eps[i-1]) {
  1763. xhci_disable_ep(xhci, slotid, i);
  1764. }
  1765. }
  1766. xhci->slots[slotid-1].enabled = 0;
  1767. xhci->slots[slotid-1].addressed = 0;
  1768. xhci->slots[slotid-1].uport = NULL;
  1769. xhci->slots[slotid-1].intr = 0;
  1770. return CC_SUCCESS;
  1771. }
  1772. static USBPort *xhci_lookup_uport(XHCIState *xhci, uint32_t *slot_ctx)
  1773. {
  1774. USBPort *uport;
  1775. char path[32];
  1776. int i, pos, port;
  1777. port = (slot_ctx[1]>>16) & 0xFF;
  1778. if (port < 1 || port > xhci->numports) {
  1779. return NULL;
  1780. }
  1781. port = xhci->ports[port-1].uport->index+1;
  1782. pos = snprintf(path, sizeof(path), "%d", port);
  1783. for (i = 0; i < 5; i++) {
  1784. port = (slot_ctx[0] >> 4*i) & 0x0f;
  1785. if (!port) {
  1786. break;
  1787. }
  1788. pos += snprintf(path + pos, sizeof(path) - pos, ".%d", port);
  1789. }
  1790. QTAILQ_FOREACH(uport, &xhci->bus.used, next) {
  1791. if (strcmp(uport->path, path) == 0) {
  1792. return uport;
  1793. }
  1794. }
  1795. return NULL;
  1796. }
  1797. static TRBCCode xhci_address_slot(XHCIState *xhci, unsigned int slotid,
  1798. uint64_t pictx, bool bsr)
  1799. {
  1800. XHCISlot *slot;
  1801. USBPort *uport;
  1802. USBDevice *dev;
  1803. dma_addr_t ictx, octx, dcbaap;
  1804. uint64_t poctx;
  1805. uint32_t ictl_ctx[2];
  1806. uint32_t slot_ctx[4];
  1807. uint32_t ep0_ctx[5];
  1808. int i;
  1809. TRBCCode res;
  1810. assert(slotid >= 1 && slotid <= xhci->numslots);
  1811. dcbaap = xhci_addr64(xhci->dcbaap_low, xhci->dcbaap_high);
  1812. ldq_le_dma(xhci->as, dcbaap + 8 * slotid, &poctx, MEMTXATTRS_UNSPECIFIED);
  1813. ictx = xhci_mask64(pictx);
  1814. octx = xhci_mask64(poctx);
  1815. DPRINTF("xhci: input context at "DMA_ADDR_FMT"\n", ictx);
  1816. DPRINTF("xhci: output context at "DMA_ADDR_FMT"\n", octx);
  1817. xhci_dma_read_u32s(xhci, ictx, ictl_ctx, sizeof(ictl_ctx));
  1818. if (ictl_ctx[0] != 0x0 || ictl_ctx[1] != 0x3) {
  1819. DPRINTF("xhci: invalid input context control %08x %08x\n",
  1820. ictl_ctx[0], ictl_ctx[1]);
  1821. return CC_TRB_ERROR;
  1822. }
  1823. xhci_dma_read_u32s(xhci, ictx+32, slot_ctx, sizeof(slot_ctx));
  1824. xhci_dma_read_u32s(xhci, ictx+64, ep0_ctx, sizeof(ep0_ctx));
  1825. DPRINTF("xhci: input slot context: %08x %08x %08x %08x\n",
  1826. slot_ctx[0], slot_ctx[1], slot_ctx[2], slot_ctx[3]);
  1827. DPRINTF("xhci: input ep0 context: %08x %08x %08x %08x %08x\n",
  1828. ep0_ctx[0], ep0_ctx[1], ep0_ctx[2], ep0_ctx[3], ep0_ctx[4]);
  1829. uport = xhci_lookup_uport(xhci, slot_ctx);
  1830. if (uport == NULL) {
  1831. DPRINTF("xhci: port not found\n");
  1832. return CC_TRB_ERROR;
  1833. }
  1834. trace_usb_xhci_slot_address(slotid, uport->path);
  1835. dev = uport->dev;
  1836. if (!dev || !dev->attached) {
  1837. DPRINTF("xhci: port %s not connected\n", uport->path);
  1838. return CC_USB_TRANSACTION_ERROR;
  1839. }
  1840. for (i = 0; i < xhci->numslots; i++) {
  1841. if (i == slotid-1) {
  1842. continue;
  1843. }
  1844. if (xhci->slots[i].uport == uport) {
  1845. DPRINTF("xhci: port %s already assigned to slot %d\n",
  1846. uport->path, i+1);
  1847. return CC_TRB_ERROR;
  1848. }
  1849. }
  1850. slot = &xhci->slots[slotid-1];
  1851. slot->uport = uport;
  1852. slot->ctx = octx;
  1853. slot->intr = get_field(slot_ctx[2], TRB_INTR);
  1854. /* Make sure device is in USB_STATE_DEFAULT state */
  1855. usb_device_reset(dev);
  1856. if (bsr) {
  1857. slot_ctx[3] = SLOT_DEFAULT << SLOT_STATE_SHIFT;
  1858. } else {
  1859. USBPacket p;
  1860. uint8_t buf[1];
  1861. slot_ctx[3] = (SLOT_ADDRESSED << SLOT_STATE_SHIFT) | slotid;
  1862. memset(&p, 0, sizeof(p));
  1863. usb_packet_addbuf(&p, buf, sizeof(buf));
  1864. usb_packet_setup(&p, USB_TOKEN_OUT,
  1865. usb_ep_get(dev, USB_TOKEN_OUT, 0), 0,
  1866. 0, false, false);
  1867. usb_device_handle_control(dev, &p,
  1868. DeviceOutRequest | USB_REQ_SET_ADDRESS,
  1869. slotid, 0, 0, NULL);
  1870. assert(p.status != USB_RET_ASYNC);
  1871. usb_packet_cleanup(&p);
  1872. }
  1873. res = xhci_enable_ep(xhci, slotid, 1, octx+32, ep0_ctx);
  1874. DPRINTF("xhci: output slot context: %08x %08x %08x %08x\n",
  1875. slot_ctx[0], slot_ctx[1], slot_ctx[2], slot_ctx[3]);
  1876. DPRINTF("xhci: output ep0 context: %08x %08x %08x %08x %08x\n",
  1877. ep0_ctx[0], ep0_ctx[1], ep0_ctx[2], ep0_ctx[3], ep0_ctx[4]);
  1878. xhci_dma_write_u32s(xhci, octx, slot_ctx, sizeof(slot_ctx));
  1879. xhci_dma_write_u32s(xhci, octx+32, ep0_ctx, sizeof(ep0_ctx));
  1880. xhci->slots[slotid-1].addressed = 1;
  1881. return res;
  1882. }
  1883. static TRBCCode xhci_configure_slot(XHCIState *xhci, unsigned int slotid,
  1884. uint64_t pictx, bool dc)
  1885. {
  1886. dma_addr_t ictx, octx;
  1887. uint32_t ictl_ctx[2];
  1888. uint32_t slot_ctx[4];
  1889. uint32_t islot_ctx[4];
  1890. uint32_t ep_ctx[5];
  1891. int i;
  1892. TRBCCode res;
  1893. trace_usb_xhci_slot_configure(slotid);
  1894. assert(slotid >= 1 && slotid <= xhci->numslots);
  1895. ictx = xhci_mask64(pictx);
  1896. octx = xhci->slots[slotid-1].ctx;
  1897. DPRINTF("xhci: input context at "DMA_ADDR_FMT"\n", ictx);
  1898. DPRINTF("xhci: output context at "DMA_ADDR_FMT"\n", octx);
  1899. if (dc) {
  1900. for (i = 2; i <= 31; i++) {
  1901. if (xhci->slots[slotid-1].eps[i-1]) {
  1902. xhci_disable_ep(xhci, slotid, i);
  1903. }
  1904. }
  1905. xhci_dma_read_u32s(xhci, octx, slot_ctx, sizeof(slot_ctx));
  1906. slot_ctx[3] &= ~(SLOT_STATE_MASK << SLOT_STATE_SHIFT);
  1907. slot_ctx[3] |= SLOT_ADDRESSED << SLOT_STATE_SHIFT;
  1908. DPRINTF("xhci: output slot context: %08x %08x %08x %08x\n",
  1909. slot_ctx[0], slot_ctx[1], slot_ctx[2], slot_ctx[3]);
  1910. xhci_dma_write_u32s(xhci, octx, slot_ctx, sizeof(slot_ctx));
  1911. return CC_SUCCESS;
  1912. }
  1913. xhci_dma_read_u32s(xhci, ictx, ictl_ctx, sizeof(ictl_ctx));
  1914. if ((ictl_ctx[0] & 0x3) != 0x0 || (ictl_ctx[1] & 0x3) != 0x1) {
  1915. DPRINTF("xhci: invalid input context control %08x %08x\n",
  1916. ictl_ctx[0], ictl_ctx[1]);
  1917. return CC_TRB_ERROR;
  1918. }
  1919. xhci_dma_read_u32s(xhci, ictx+32, islot_ctx, sizeof(islot_ctx));
  1920. xhci_dma_read_u32s(xhci, octx, slot_ctx, sizeof(slot_ctx));
  1921. if (SLOT_STATE(slot_ctx[3]) < SLOT_ADDRESSED) {
  1922. DPRINTF("xhci: invalid slot state %08x\n", slot_ctx[3]);
  1923. return CC_CONTEXT_STATE_ERROR;
  1924. }
  1925. xhci_free_device_streams(xhci, slotid, ictl_ctx[0] | ictl_ctx[1]);
  1926. for (i = 2; i <= 31; i++) {
  1927. if (ictl_ctx[0] & (1<<i)) {
  1928. xhci_disable_ep(xhci, slotid, i);
  1929. }
  1930. if (ictl_ctx[1] & (1<<i)) {
  1931. xhci_dma_read_u32s(xhci, ictx+32+(32*i), ep_ctx, sizeof(ep_ctx));
  1932. DPRINTF("xhci: input ep%d.%d context: %08x %08x %08x %08x %08x\n",
  1933. i/2, i%2, ep_ctx[0], ep_ctx[1], ep_ctx[2],
  1934. ep_ctx[3], ep_ctx[4]);
  1935. xhci_disable_ep(xhci, slotid, i);
  1936. res = xhci_enable_ep(xhci, slotid, i, octx+(32*i), ep_ctx);
  1937. if (res != CC_SUCCESS) {
  1938. return res;
  1939. }
  1940. DPRINTF("xhci: output ep%d.%d context: %08x %08x %08x %08x %08x\n",
  1941. i/2, i%2, ep_ctx[0], ep_ctx[1], ep_ctx[2],
  1942. ep_ctx[3], ep_ctx[4]);
  1943. xhci_dma_write_u32s(xhci, octx+(32*i), ep_ctx, sizeof(ep_ctx));
  1944. }
  1945. }
  1946. res = xhci_alloc_device_streams(xhci, slotid, ictl_ctx[1]);
  1947. if (res != CC_SUCCESS) {
  1948. for (i = 2; i <= 31; i++) {
  1949. if (ictl_ctx[1] & (1u << i)) {
  1950. xhci_disable_ep(xhci, slotid, i);
  1951. }
  1952. }
  1953. return res;
  1954. }
  1955. slot_ctx[3] &= ~(SLOT_STATE_MASK << SLOT_STATE_SHIFT);
  1956. slot_ctx[3] |= SLOT_CONFIGURED << SLOT_STATE_SHIFT;
  1957. slot_ctx[0] &= ~(SLOT_CONTEXT_ENTRIES_MASK << SLOT_CONTEXT_ENTRIES_SHIFT);
  1958. slot_ctx[0] |= islot_ctx[0] & (SLOT_CONTEXT_ENTRIES_MASK <<
  1959. SLOT_CONTEXT_ENTRIES_SHIFT);
  1960. DPRINTF("xhci: output slot context: %08x %08x %08x %08x\n",
  1961. slot_ctx[0], slot_ctx[1], slot_ctx[2], slot_ctx[3]);
  1962. xhci_dma_write_u32s(xhci, octx, slot_ctx, sizeof(slot_ctx));
  1963. return CC_SUCCESS;
  1964. }
  1965. static TRBCCode xhci_evaluate_slot(XHCIState *xhci, unsigned int slotid,
  1966. uint64_t pictx)
  1967. {
  1968. dma_addr_t ictx, octx;
  1969. uint32_t ictl_ctx[2];
  1970. uint32_t iep0_ctx[5];
  1971. uint32_t ep0_ctx[5];
  1972. uint32_t islot_ctx[4];
  1973. uint32_t slot_ctx[4];
  1974. trace_usb_xhci_slot_evaluate(slotid);
  1975. assert(slotid >= 1 && slotid <= xhci->numslots);
  1976. ictx = xhci_mask64(pictx);
  1977. octx = xhci->slots[slotid-1].ctx;
  1978. DPRINTF("xhci: input context at "DMA_ADDR_FMT"\n", ictx);
  1979. DPRINTF("xhci: output context at "DMA_ADDR_FMT"\n", octx);
  1980. xhci_dma_read_u32s(xhci, ictx, ictl_ctx, sizeof(ictl_ctx));
  1981. if (ictl_ctx[0] != 0x0 || ictl_ctx[1] & ~0x3) {
  1982. DPRINTF("xhci: invalid input context control %08x %08x\n",
  1983. ictl_ctx[0], ictl_ctx[1]);
  1984. return CC_TRB_ERROR;
  1985. }
  1986. if (ictl_ctx[1] & 0x1) {
  1987. xhci_dma_read_u32s(xhci, ictx+32, islot_ctx, sizeof(islot_ctx));
  1988. DPRINTF("xhci: input slot context: %08x %08x %08x %08x\n",
  1989. islot_ctx[0], islot_ctx[1], islot_ctx[2], islot_ctx[3]);
  1990. xhci_dma_read_u32s(xhci, octx, slot_ctx, sizeof(slot_ctx));
  1991. slot_ctx[1] &= ~0xFFFF; /* max exit latency */
  1992. slot_ctx[1] |= islot_ctx[1] & 0xFFFF;
  1993. /* update interrupter target field */
  1994. xhci->slots[slotid-1].intr = get_field(islot_ctx[2], TRB_INTR);
  1995. set_field(&slot_ctx[2], xhci->slots[slotid-1].intr, TRB_INTR);
  1996. DPRINTF("xhci: output slot context: %08x %08x %08x %08x\n",
  1997. slot_ctx[0], slot_ctx[1], slot_ctx[2], slot_ctx[3]);
  1998. xhci_dma_write_u32s(xhci, octx, slot_ctx, sizeof(slot_ctx));
  1999. }
  2000. if (ictl_ctx[1] & 0x2) {
  2001. xhci_dma_read_u32s(xhci, ictx+64, iep0_ctx, sizeof(iep0_ctx));
  2002. DPRINTF("xhci: input ep0 context: %08x %08x %08x %08x %08x\n",
  2003. iep0_ctx[0], iep0_ctx[1], iep0_ctx[2],
  2004. iep0_ctx[3], iep0_ctx[4]);
  2005. xhci_dma_read_u32s(xhci, octx+32, ep0_ctx, sizeof(ep0_ctx));
  2006. ep0_ctx[1] &= ~0xFFFF0000; /* max packet size*/
  2007. ep0_ctx[1] |= iep0_ctx[1] & 0xFFFF0000;
  2008. DPRINTF("xhci: output ep0 context: %08x %08x %08x %08x %08x\n",
  2009. ep0_ctx[0], ep0_ctx[1], ep0_ctx[2], ep0_ctx[3], ep0_ctx[4]);
  2010. xhci_dma_write_u32s(xhci, octx+32, ep0_ctx, sizeof(ep0_ctx));
  2011. }
  2012. return CC_SUCCESS;
  2013. }
  2014. static TRBCCode xhci_reset_slot(XHCIState *xhci, unsigned int slotid)
  2015. {
  2016. uint32_t slot_ctx[4];
  2017. dma_addr_t octx;
  2018. int i;
  2019. trace_usb_xhci_slot_reset(slotid);
  2020. assert(slotid >= 1 && slotid <= xhci->numslots);
  2021. octx = xhci->slots[slotid-1].ctx;
  2022. DPRINTF("xhci: output context at "DMA_ADDR_FMT"\n", octx);
  2023. for (i = 2; i <= 31; i++) {
  2024. if (xhci->slots[slotid-1].eps[i-1]) {
  2025. xhci_disable_ep(xhci, slotid, i);
  2026. }
  2027. }
  2028. xhci_dma_read_u32s(xhci, octx, slot_ctx, sizeof(slot_ctx));
  2029. slot_ctx[3] &= ~(SLOT_STATE_MASK << SLOT_STATE_SHIFT);
  2030. slot_ctx[3] |= SLOT_DEFAULT << SLOT_STATE_SHIFT;
  2031. DPRINTF("xhci: output slot context: %08x %08x %08x %08x\n",
  2032. slot_ctx[0], slot_ctx[1], slot_ctx[2], slot_ctx[3]);
  2033. xhci_dma_write_u32s(xhci, octx, slot_ctx, sizeof(slot_ctx));
  2034. return CC_SUCCESS;
  2035. }
  2036. static unsigned int xhci_get_slot(XHCIState *xhci, XHCIEvent *event, XHCITRB *trb)
  2037. {
  2038. unsigned int slotid;
  2039. slotid = (trb->control >> TRB_CR_SLOTID_SHIFT) & TRB_CR_SLOTID_MASK;
  2040. if (slotid < 1 || slotid > xhci->numslots) {
  2041. DPRINTF("xhci: bad slot id %d\n", slotid);
  2042. event->ccode = CC_TRB_ERROR;
  2043. return 0;
  2044. } else if (!xhci->slots[slotid-1].enabled) {
  2045. DPRINTF("xhci: slot id %d not enabled\n", slotid);
  2046. event->ccode = CC_SLOT_NOT_ENABLED_ERROR;
  2047. return 0;
  2048. }
  2049. return slotid;
  2050. }
  2051. /* cleanup slot state on usb device detach */
  2052. static void xhci_detach_slot(XHCIState *xhci, USBPort *uport)
  2053. {
  2054. int slot, ep;
  2055. for (slot = 0; slot < xhci->numslots; slot++) {
  2056. if (xhci->slots[slot].uport == uport) {
  2057. break;
  2058. }
  2059. }
  2060. if (slot == xhci->numslots) {
  2061. return;
  2062. }
  2063. for (ep = 0; ep < 31; ep++) {
  2064. if (xhci->slots[slot].eps[ep]) {
  2065. xhci_ep_nuke_xfers(xhci, slot + 1, ep + 1, 0);
  2066. }
  2067. }
  2068. xhci->slots[slot].uport = NULL;
  2069. }
  2070. static TRBCCode xhci_get_port_bandwidth(XHCIState *xhci, uint64_t pctx)
  2071. {
  2072. dma_addr_t ctx;
  2073. uint8_t bw_ctx[xhci->numports+1];
  2074. DPRINTF("xhci_get_port_bandwidth()\n");
  2075. ctx = xhci_mask64(pctx);
  2076. DPRINTF("xhci: bandwidth context at "DMA_ADDR_FMT"\n", ctx);
  2077. /* TODO: actually implement real values here */
  2078. bw_ctx[0] = 0;
  2079. memset(&bw_ctx[1], 80, xhci->numports); /* 80% */
  2080. if (dma_memory_write(xhci->as, ctx, bw_ctx, sizeof(bw_ctx),
  2081. MEMTXATTRS_UNSPECIFIED) != MEMTX_OK) {
  2082. qemu_log_mask(LOG_GUEST_ERROR, "%s: DMA memory write failed!\n",
  2083. __func__);
  2084. return CC_TRB_ERROR;
  2085. }
  2086. return CC_SUCCESS;
  2087. }
  2088. static uint32_t rotl(uint32_t v, unsigned count)
  2089. {
  2090. count &= 31;
  2091. return (v << count) | (v >> (32 - count));
  2092. }
  2093. static uint32_t xhci_nec_challenge(uint32_t hi, uint32_t lo)
  2094. {
  2095. uint32_t val;
  2096. val = rotl(lo - 0x49434878, 32 - ((hi>>8) & 0x1F));
  2097. val += rotl(lo + 0x49434878, hi & 0x1F);
  2098. val -= rotl(hi ^ 0x49434878, (lo >> 16) & 0x1F);
  2099. return ~val;
  2100. }
  2101. static void xhci_process_commands(XHCIState *xhci)
  2102. {
  2103. XHCITRB trb;
  2104. TRBType type;
  2105. XHCIEvent event = {ER_COMMAND_COMPLETE, CC_SUCCESS};
  2106. dma_addr_t addr;
  2107. unsigned int i, slotid = 0, count = 0;
  2108. DPRINTF("xhci_process_commands()\n");
  2109. if (!xhci_running(xhci)) {
  2110. DPRINTF("xhci_process_commands() called while xHC stopped or paused\n");
  2111. return;
  2112. }
  2113. xhci->crcr_low |= CRCR_CRR;
  2114. while ((type = xhci_ring_fetch(xhci, &xhci->cmd_ring, &trb, &addr))) {
  2115. event.ptr = addr;
  2116. switch (type) {
  2117. case CR_ENABLE_SLOT:
  2118. for (i = 0; i < xhci->numslots; i++) {
  2119. if (!xhci->slots[i].enabled) {
  2120. break;
  2121. }
  2122. }
  2123. if (i >= xhci->numslots) {
  2124. DPRINTF("xhci: no device slots available\n");
  2125. event.ccode = CC_NO_SLOTS_ERROR;
  2126. } else {
  2127. slotid = i+1;
  2128. event.ccode = xhci_enable_slot(xhci, slotid);
  2129. }
  2130. break;
  2131. case CR_DISABLE_SLOT:
  2132. slotid = xhci_get_slot(xhci, &event, &trb);
  2133. if (slotid) {
  2134. event.ccode = xhci_disable_slot(xhci, slotid);
  2135. }
  2136. break;
  2137. case CR_ADDRESS_DEVICE:
  2138. slotid = xhci_get_slot(xhci, &event, &trb);
  2139. if (slotid) {
  2140. event.ccode = xhci_address_slot(xhci, slotid, trb.parameter,
  2141. trb.control & TRB_CR_BSR);
  2142. }
  2143. break;
  2144. case CR_CONFIGURE_ENDPOINT:
  2145. slotid = xhci_get_slot(xhci, &event, &trb);
  2146. if (slotid) {
  2147. event.ccode = xhci_configure_slot(xhci, slotid, trb.parameter,
  2148. trb.control & TRB_CR_DC);
  2149. }
  2150. break;
  2151. case CR_EVALUATE_CONTEXT:
  2152. slotid = xhci_get_slot(xhci, &event, &trb);
  2153. if (slotid) {
  2154. event.ccode = xhci_evaluate_slot(xhci, slotid, trb.parameter);
  2155. }
  2156. break;
  2157. case CR_STOP_ENDPOINT:
  2158. slotid = xhci_get_slot(xhci, &event, &trb);
  2159. if (slotid) {
  2160. unsigned int epid = (trb.control >> TRB_CR_EPID_SHIFT)
  2161. & TRB_CR_EPID_MASK;
  2162. event.ccode = xhci_stop_ep(xhci, slotid, epid);
  2163. }
  2164. break;
  2165. case CR_RESET_ENDPOINT:
  2166. slotid = xhci_get_slot(xhci, &event, &trb);
  2167. if (slotid) {
  2168. unsigned int epid = (trb.control >> TRB_CR_EPID_SHIFT)
  2169. & TRB_CR_EPID_MASK;
  2170. event.ccode = xhci_reset_ep(xhci, slotid, epid);
  2171. }
  2172. break;
  2173. case CR_SET_TR_DEQUEUE:
  2174. slotid = xhci_get_slot(xhci, &event, &trb);
  2175. if (slotid) {
  2176. unsigned int epid = (trb.control >> TRB_CR_EPID_SHIFT)
  2177. & TRB_CR_EPID_MASK;
  2178. unsigned int streamid = (trb.status >> 16) & 0xffff;
  2179. event.ccode = xhci_set_ep_dequeue(xhci, slotid,
  2180. epid, streamid,
  2181. trb.parameter);
  2182. }
  2183. break;
  2184. case CR_RESET_DEVICE:
  2185. slotid = xhci_get_slot(xhci, &event, &trb);
  2186. if (slotid) {
  2187. event.ccode = xhci_reset_slot(xhci, slotid);
  2188. }
  2189. break;
  2190. case CR_GET_PORT_BANDWIDTH:
  2191. event.ccode = xhci_get_port_bandwidth(xhci, trb.parameter);
  2192. break;
  2193. case CR_NOOP:
  2194. event.ccode = CC_SUCCESS;
  2195. break;
  2196. case CR_VENDOR_NEC_FIRMWARE_REVISION:
  2197. if (xhci->nec_quirks) {
  2198. event.type = 48; /* NEC reply */
  2199. event.length = 0x3034;
  2200. } else {
  2201. event.ccode = CC_TRB_ERROR;
  2202. }
  2203. break;
  2204. case CR_VENDOR_NEC_CHALLENGE_RESPONSE:
  2205. if (xhci->nec_quirks) {
  2206. uint32_t chi = trb.parameter >> 32;
  2207. uint32_t clo = trb.parameter;
  2208. uint32_t val = xhci_nec_challenge(chi, clo);
  2209. event.length = val & 0xFFFF;
  2210. event.epid = val >> 16;
  2211. slotid = val >> 24;
  2212. event.type = 48; /* NEC reply */
  2213. } else {
  2214. event.ccode = CC_TRB_ERROR;
  2215. }
  2216. break;
  2217. default:
  2218. trace_usb_xhci_unimplemented("command", type);
  2219. event.ccode = CC_TRB_ERROR;
  2220. break;
  2221. }
  2222. event.slotid = slotid;
  2223. xhci_event(xhci, &event, 0);
  2224. if (count++ > COMMAND_LIMIT) {
  2225. trace_usb_xhci_enforced_limit("commands");
  2226. return;
  2227. }
  2228. }
  2229. }
  2230. static bool xhci_port_have_device(XHCIPort *port)
  2231. {
  2232. if (!port->uport->dev || !port->uport->dev->attached) {
  2233. return false; /* no device present */
  2234. }
  2235. if (!((1 << port->uport->dev->speed) & port->speedmask)) {
  2236. return false; /* speed mismatch */
  2237. }
  2238. return true;
  2239. }
  2240. static void xhci_port_notify(XHCIPort *port, uint32_t bits)
  2241. {
  2242. XHCIEvent ev = { ER_PORT_STATUS_CHANGE, CC_SUCCESS,
  2243. port->portnr << 24 };
  2244. if ((port->portsc & bits) == bits) {
  2245. return;
  2246. }
  2247. trace_usb_xhci_port_notify(port->portnr, bits);
  2248. port->portsc |= bits;
  2249. if (!xhci_running(port->xhci)) {
  2250. return;
  2251. }
  2252. xhci_event(port->xhci, &ev, 0);
  2253. }
  2254. static void xhci_port_update(XHCIPort *port, int is_detach)
  2255. {
  2256. uint32_t pls = PLS_RX_DETECT;
  2257. assert(port);
  2258. port->portsc = PORTSC_PP;
  2259. if (!is_detach && xhci_port_have_device(port)) {
  2260. port->portsc |= PORTSC_CCS;
  2261. switch (port->uport->dev->speed) {
  2262. case USB_SPEED_LOW:
  2263. port->portsc |= PORTSC_SPEED_LOW;
  2264. pls = PLS_POLLING;
  2265. break;
  2266. case USB_SPEED_FULL:
  2267. port->portsc |= PORTSC_SPEED_FULL;
  2268. pls = PLS_POLLING;
  2269. break;
  2270. case USB_SPEED_HIGH:
  2271. port->portsc |= PORTSC_SPEED_HIGH;
  2272. pls = PLS_POLLING;
  2273. break;
  2274. case USB_SPEED_SUPER:
  2275. port->portsc |= PORTSC_SPEED_SUPER;
  2276. port->portsc |= PORTSC_PED;
  2277. pls = PLS_U0;
  2278. break;
  2279. }
  2280. }
  2281. set_field(&port->portsc, pls, PORTSC_PLS);
  2282. trace_usb_xhci_port_link(port->portnr, pls);
  2283. xhci_port_notify(port, PORTSC_CSC);
  2284. }
  2285. static void xhci_port_reset(XHCIPort *port, bool warm_reset)
  2286. {
  2287. trace_usb_xhci_port_reset(port->portnr, warm_reset);
  2288. if (!xhci_port_have_device(port)) {
  2289. return;
  2290. }
  2291. usb_device_reset(port->uport->dev);
  2292. switch (port->uport->dev->speed) {
  2293. case USB_SPEED_SUPER:
  2294. if (warm_reset) {
  2295. port->portsc |= PORTSC_WRC;
  2296. }
  2297. /* fall through */
  2298. case USB_SPEED_LOW:
  2299. case USB_SPEED_FULL:
  2300. case USB_SPEED_HIGH:
  2301. set_field(&port->portsc, PLS_U0, PORTSC_PLS);
  2302. trace_usb_xhci_port_link(port->portnr, PLS_U0);
  2303. port->portsc |= PORTSC_PED;
  2304. break;
  2305. }
  2306. port->portsc &= ~PORTSC_PR;
  2307. xhci_port_notify(port, PORTSC_PRC);
  2308. }
  2309. static void xhci_reset(DeviceState *dev)
  2310. {
  2311. XHCIState *xhci = XHCI(dev);
  2312. int i;
  2313. trace_usb_xhci_reset();
  2314. if (!(xhci->usbsts & USBSTS_HCH)) {
  2315. DPRINTF("xhci: reset while running!\n");
  2316. }
  2317. xhci->usbcmd = 0;
  2318. xhci->usbsts = USBSTS_HCH;
  2319. xhci->dnctrl = 0;
  2320. xhci->crcr_low = 0;
  2321. xhci->crcr_high = 0;
  2322. xhci->dcbaap_low = 0;
  2323. xhci->dcbaap_high = 0;
  2324. xhci->config = 0;
  2325. for (i = 0; i < xhci->numslots; i++) {
  2326. xhci_disable_slot(xhci, i+1);
  2327. }
  2328. for (i = 0; i < xhci->numports; i++) {
  2329. xhci_port_update(xhci->ports + i, 0);
  2330. }
  2331. for (i = 0; i < xhci->numintrs; i++) {
  2332. xhci->intr[i].iman = 0;
  2333. xhci->intr[i].imod = 0;
  2334. xhci->intr[i].erstsz = 0;
  2335. xhci->intr[i].erstba_low = 0;
  2336. xhci->intr[i].erstba_high = 0;
  2337. xhci->intr[i].erdp_low = 0;
  2338. xhci->intr[i].erdp_high = 0;
  2339. xhci->intr[i].er_ep_idx = 0;
  2340. xhci->intr[i].er_pcs = 1;
  2341. xhci->intr[i].ev_buffer_put = 0;
  2342. xhci->intr[i].ev_buffer_get = 0;
  2343. }
  2344. xhci->mfindex_start = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
  2345. xhci_mfwrap_update(xhci);
  2346. }
  2347. static uint64_t xhci_cap_read(void *ptr, hwaddr reg, unsigned size)
  2348. {
  2349. XHCIState *xhci = ptr;
  2350. uint32_t ret;
  2351. switch (reg) {
  2352. case 0x00: /* HCIVERSION, CAPLENGTH */
  2353. ret = 0x01000000 | LEN_CAP;
  2354. break;
  2355. case 0x04: /* HCSPARAMS 1 */
  2356. ret = ((xhci->numports_2+xhci->numports_3)<<24)
  2357. | (xhci->numintrs<<8) | xhci->numslots;
  2358. break;
  2359. case 0x08: /* HCSPARAMS 2 */
  2360. ret = 0x0000000f;
  2361. break;
  2362. case 0x0c: /* HCSPARAMS 3 */
  2363. ret = 0x00000000;
  2364. break;
  2365. case 0x10: /* HCCPARAMS */
  2366. if (sizeof(dma_addr_t) == 4) {
  2367. ret = 0x00080000 | (xhci->max_pstreams_mask << 12);
  2368. } else {
  2369. ret = 0x00080001 | (xhci->max_pstreams_mask << 12);
  2370. }
  2371. break;
  2372. case 0x14: /* DBOFF */
  2373. ret = OFF_DOORBELL;
  2374. break;
  2375. case 0x18: /* RTSOFF */
  2376. ret = OFF_RUNTIME;
  2377. break;
  2378. /* extended capabilities */
  2379. case 0x20: /* Supported Protocol:00 */
  2380. ret = 0x02000402; /* USB 2.0 */
  2381. break;
  2382. case 0x24: /* Supported Protocol:04 */
  2383. ret = 0x20425355; /* "USB " */
  2384. break;
  2385. case 0x28: /* Supported Protocol:08 */
  2386. if (xhci_get_flag(xhci, XHCI_FLAG_SS_FIRST)) {
  2387. ret = (xhci->numports_2<<8) | (xhci->numports_3+1);
  2388. } else {
  2389. ret = (xhci->numports_2<<8) | 1;
  2390. }
  2391. break;
  2392. case 0x2c: /* Supported Protocol:0c */
  2393. ret = 0x00000000; /* reserved */
  2394. break;
  2395. case 0x30: /* Supported Protocol:00 */
  2396. ret = 0x03000002; /* USB 3.0 */
  2397. break;
  2398. case 0x34: /* Supported Protocol:04 */
  2399. ret = 0x20425355; /* "USB " */
  2400. break;
  2401. case 0x38: /* Supported Protocol:08 */
  2402. if (xhci_get_flag(xhci, XHCI_FLAG_SS_FIRST)) {
  2403. ret = (xhci->numports_3<<8) | 1;
  2404. } else {
  2405. ret = (xhci->numports_3<<8) | (xhci->numports_2+1);
  2406. }
  2407. break;
  2408. case 0x3c: /* Supported Protocol:0c */
  2409. ret = 0x00000000; /* reserved */
  2410. break;
  2411. default:
  2412. trace_usb_xhci_unimplemented("cap read", reg);
  2413. ret = 0;
  2414. }
  2415. trace_usb_xhci_cap_read(reg, ret);
  2416. return ret;
  2417. }
  2418. static uint64_t xhci_port_read(void *ptr, hwaddr reg, unsigned size)
  2419. {
  2420. XHCIPort *port = ptr;
  2421. uint32_t ret;
  2422. switch (reg) {
  2423. case 0x00: /* PORTSC */
  2424. ret = port->portsc;
  2425. break;
  2426. case 0x04: /* PORTPMSC */
  2427. case 0x08: /* PORTLI */
  2428. ret = 0;
  2429. break;
  2430. case 0x0c: /* reserved */
  2431. default:
  2432. trace_usb_xhci_unimplemented("port read", reg);
  2433. ret = 0;
  2434. }
  2435. trace_usb_xhci_port_read(port->portnr, reg, ret);
  2436. return ret;
  2437. }
  2438. static void xhci_port_write(void *ptr, hwaddr reg,
  2439. uint64_t val, unsigned size)
  2440. {
  2441. XHCIPort *port = ptr;
  2442. uint32_t portsc, notify;
  2443. trace_usb_xhci_port_write(port->portnr, reg, val);
  2444. switch (reg) {
  2445. case 0x00: /* PORTSC */
  2446. /* write-1-to-start bits */
  2447. if (val & PORTSC_WPR) {
  2448. xhci_port_reset(port, true);
  2449. break;
  2450. }
  2451. if (val & PORTSC_PR) {
  2452. xhci_port_reset(port, false);
  2453. break;
  2454. }
  2455. portsc = port->portsc;
  2456. notify = 0;
  2457. /* write-1-to-clear bits*/
  2458. portsc &= ~(val & (PORTSC_CSC|PORTSC_PEC|PORTSC_WRC|PORTSC_OCC|
  2459. PORTSC_PRC|PORTSC_PLC|PORTSC_CEC));
  2460. if (val & PORTSC_LWS) {
  2461. /* overwrite PLS only when LWS=1 */
  2462. uint32_t old_pls = get_field(port->portsc, PORTSC_PLS);
  2463. uint32_t new_pls = get_field(val, PORTSC_PLS);
  2464. switch (new_pls) {
  2465. case PLS_U0:
  2466. if (old_pls != PLS_U0) {
  2467. set_field(&portsc, new_pls, PORTSC_PLS);
  2468. trace_usb_xhci_port_link(port->portnr, new_pls);
  2469. notify = PORTSC_PLC;
  2470. }
  2471. break;
  2472. case PLS_U3:
  2473. if (old_pls < PLS_U3) {
  2474. set_field(&portsc, new_pls, PORTSC_PLS);
  2475. trace_usb_xhci_port_link(port->portnr, new_pls);
  2476. }
  2477. break;
  2478. case PLS_RESUME:
  2479. /* windows does this for some reason, don't spam stderr */
  2480. break;
  2481. default:
  2482. DPRINTF("%s: ignore pls write (old %d, new %d)\n",
  2483. __func__, old_pls, new_pls);
  2484. break;
  2485. }
  2486. }
  2487. /* read/write bits */
  2488. portsc &= ~(PORTSC_PP|PORTSC_WCE|PORTSC_WDE|PORTSC_WOE);
  2489. portsc |= (val & (PORTSC_PP|PORTSC_WCE|PORTSC_WDE|PORTSC_WOE));
  2490. port->portsc = portsc;
  2491. if (notify) {
  2492. xhci_port_notify(port, notify);
  2493. }
  2494. break;
  2495. case 0x04: /* PORTPMSC */
  2496. case 0x08: /* PORTLI */
  2497. default:
  2498. trace_usb_xhci_unimplemented("port write", reg);
  2499. }
  2500. }
  2501. static uint64_t xhci_oper_read(void *ptr, hwaddr reg, unsigned size)
  2502. {
  2503. XHCIState *xhci = ptr;
  2504. uint32_t ret;
  2505. switch (reg) {
  2506. case 0x00: /* USBCMD */
  2507. ret = xhci->usbcmd;
  2508. break;
  2509. case 0x04: /* USBSTS */
  2510. ret = xhci->usbsts;
  2511. break;
  2512. case 0x08: /* PAGESIZE */
  2513. ret = 1; /* 4KiB */
  2514. break;
  2515. case 0x14: /* DNCTRL */
  2516. ret = xhci->dnctrl;
  2517. break;
  2518. case 0x18: /* CRCR low */
  2519. ret = xhci->crcr_low & ~0xe;
  2520. break;
  2521. case 0x1c: /* CRCR high */
  2522. ret = xhci->crcr_high;
  2523. break;
  2524. case 0x30: /* DCBAAP low */
  2525. ret = xhci->dcbaap_low;
  2526. break;
  2527. case 0x34: /* DCBAAP high */
  2528. ret = xhci->dcbaap_high;
  2529. break;
  2530. case 0x38: /* CONFIG */
  2531. ret = xhci->config;
  2532. break;
  2533. default:
  2534. trace_usb_xhci_unimplemented("oper read", reg);
  2535. ret = 0;
  2536. }
  2537. trace_usb_xhci_oper_read(reg, ret);
  2538. return ret;
  2539. }
  2540. static void xhci_oper_write(void *ptr, hwaddr reg,
  2541. uint64_t val, unsigned size)
  2542. {
  2543. XHCIState *xhci = XHCI(ptr);
  2544. trace_usb_xhci_oper_write(reg, val);
  2545. switch (reg) {
  2546. case 0x00: /* USBCMD */
  2547. if ((val & USBCMD_RS) && !(xhci->usbcmd & USBCMD_RS)) {
  2548. xhci_run(xhci);
  2549. } else if (!(val & USBCMD_RS) && (xhci->usbcmd & USBCMD_RS)) {
  2550. xhci_stop(xhci);
  2551. }
  2552. if (val & USBCMD_CSS) {
  2553. /* save state */
  2554. xhci->usbsts &= ~USBSTS_SRE;
  2555. }
  2556. if (val & USBCMD_CRS) {
  2557. /* restore state */
  2558. xhci->usbsts |= USBSTS_SRE;
  2559. }
  2560. xhci->usbcmd = val & 0xc0f;
  2561. xhci_mfwrap_update(xhci);
  2562. if (val & USBCMD_HCRST) {
  2563. xhci_reset(DEVICE(xhci));
  2564. }
  2565. xhci_intr_update(xhci, 0);
  2566. break;
  2567. case 0x04: /* USBSTS */
  2568. /* these bits are write-1-to-clear */
  2569. xhci->usbsts &= ~(val & (USBSTS_HSE|USBSTS_EINT|USBSTS_PCD|USBSTS_SRE));
  2570. xhci_intr_update(xhci, 0);
  2571. break;
  2572. case 0x14: /* DNCTRL */
  2573. xhci->dnctrl = val & 0xffff;
  2574. break;
  2575. case 0x18: /* CRCR low */
  2576. xhci->crcr_low = (val & 0xffffffcf) | (xhci->crcr_low & CRCR_CRR);
  2577. break;
  2578. case 0x1c: /* CRCR high */
  2579. xhci->crcr_high = val;
  2580. if (xhci->crcr_low & (CRCR_CA|CRCR_CS) && (xhci->crcr_low & CRCR_CRR)) {
  2581. XHCIEvent event = {ER_COMMAND_COMPLETE, CC_COMMAND_RING_STOPPED};
  2582. xhci->crcr_low &= ~CRCR_CRR;
  2583. xhci_event(xhci, &event, 0);
  2584. DPRINTF("xhci: command ring stopped (CRCR=%08x)\n", xhci->crcr_low);
  2585. } else {
  2586. dma_addr_t base = xhci_addr64(xhci->crcr_low & ~0x3f, val);
  2587. xhci_ring_init(xhci, &xhci->cmd_ring, base);
  2588. }
  2589. xhci->crcr_low &= ~(CRCR_CA | CRCR_CS);
  2590. break;
  2591. case 0x30: /* DCBAAP low */
  2592. xhci->dcbaap_low = val & 0xffffffc0;
  2593. break;
  2594. case 0x34: /* DCBAAP high */
  2595. xhci->dcbaap_high = val;
  2596. break;
  2597. case 0x38: /* CONFIG */
  2598. xhci->config = val & 0xff;
  2599. break;
  2600. default:
  2601. trace_usb_xhci_unimplemented("oper write", reg);
  2602. }
  2603. }
  2604. static uint64_t xhci_runtime_read(void *ptr, hwaddr reg,
  2605. unsigned size)
  2606. {
  2607. XHCIState *xhci = ptr;
  2608. uint32_t ret = 0;
  2609. if (reg < 0x20) {
  2610. switch (reg) {
  2611. case 0x00: /* MFINDEX */
  2612. ret = xhci_mfindex_get(xhci) & 0x3fff;
  2613. break;
  2614. default:
  2615. trace_usb_xhci_unimplemented("runtime read", reg);
  2616. break;
  2617. }
  2618. } else {
  2619. int v = (reg - 0x20) / 0x20;
  2620. XHCIInterrupter *intr = &xhci->intr[v];
  2621. switch (reg & 0x1f) {
  2622. case 0x00: /* IMAN */
  2623. ret = intr->iman;
  2624. break;
  2625. case 0x04: /* IMOD */
  2626. ret = intr->imod;
  2627. break;
  2628. case 0x08: /* ERSTSZ */
  2629. ret = intr->erstsz;
  2630. break;
  2631. case 0x10: /* ERSTBA low */
  2632. ret = intr->erstba_low;
  2633. break;
  2634. case 0x14: /* ERSTBA high */
  2635. ret = intr->erstba_high;
  2636. break;
  2637. case 0x18: /* ERDP low */
  2638. ret = intr->erdp_low;
  2639. break;
  2640. case 0x1c: /* ERDP high */
  2641. ret = intr->erdp_high;
  2642. break;
  2643. }
  2644. }
  2645. trace_usb_xhci_runtime_read(reg, ret);
  2646. return ret;
  2647. }
  2648. static void xhci_runtime_write(void *ptr, hwaddr reg,
  2649. uint64_t val, unsigned size)
  2650. {
  2651. XHCIState *xhci = ptr;
  2652. XHCIInterrupter *intr;
  2653. int v;
  2654. trace_usb_xhci_runtime_write(reg, val);
  2655. if (reg < 0x20) {
  2656. trace_usb_xhci_unimplemented("runtime write", reg);
  2657. return;
  2658. }
  2659. v = (reg - 0x20) / 0x20;
  2660. intr = &xhci->intr[v];
  2661. switch (reg & 0x1f) {
  2662. case 0x00: /* IMAN */
  2663. if (val & IMAN_IP) {
  2664. intr->iman &= ~IMAN_IP;
  2665. }
  2666. intr->iman &= ~IMAN_IE;
  2667. intr->iman |= val & IMAN_IE;
  2668. xhci_intr_update(xhci, v);
  2669. break;
  2670. case 0x04: /* IMOD */
  2671. intr->imod = val;
  2672. break;
  2673. case 0x08: /* ERSTSZ */
  2674. intr->erstsz = val & 0xffff;
  2675. break;
  2676. case 0x10: /* ERSTBA low */
  2677. if (xhci->nec_quirks) {
  2678. /* NEC driver bug: it doesn't align this to 64 bytes */
  2679. intr->erstba_low = val & 0xfffffff0;
  2680. } else {
  2681. intr->erstba_low = val & 0xffffffc0;
  2682. }
  2683. break;
  2684. case 0x14: /* ERSTBA high */
  2685. intr->erstba_high = val;
  2686. xhci_er_reset(xhci, v);
  2687. break;
  2688. case 0x18: /* ERDP low */
  2689. if (val & ERDP_EHB) {
  2690. intr->erdp_low &= ~ERDP_EHB;
  2691. }
  2692. intr->erdp_low = (val & ~ERDP_EHB) | (intr->erdp_low & ERDP_EHB);
  2693. if (val & ERDP_EHB) {
  2694. dma_addr_t erdp = xhci_addr64(intr->erdp_low, intr->erdp_high);
  2695. unsigned int dp_idx = (erdp - intr->er_start) / TRB_SIZE;
  2696. if (erdp >= intr->er_start &&
  2697. erdp < (intr->er_start + TRB_SIZE * intr->er_size) &&
  2698. dp_idx != intr->er_ep_idx) {
  2699. xhci_intr_raise(xhci, v);
  2700. }
  2701. }
  2702. break;
  2703. case 0x1c: /* ERDP high */
  2704. intr->erdp_high = val;
  2705. break;
  2706. default:
  2707. trace_usb_xhci_unimplemented("oper write", reg);
  2708. }
  2709. }
  2710. static uint64_t xhci_doorbell_read(void *ptr, hwaddr reg,
  2711. unsigned size)
  2712. {
  2713. /* doorbells always read as 0 */
  2714. trace_usb_xhci_doorbell_read(reg, 0);
  2715. return 0;
  2716. }
  2717. static void xhci_doorbell_write(void *ptr, hwaddr reg,
  2718. uint64_t val, unsigned size)
  2719. {
  2720. XHCIState *xhci = ptr;
  2721. unsigned int epid, streamid;
  2722. trace_usb_xhci_doorbell_write(reg, val);
  2723. if (!xhci_running(xhci)) {
  2724. DPRINTF("xhci: wrote doorbell while xHC stopped or paused\n");
  2725. return;
  2726. }
  2727. reg >>= 2;
  2728. if (reg == 0) {
  2729. if (val == 0) {
  2730. xhci_process_commands(xhci);
  2731. } else {
  2732. DPRINTF("xhci: bad doorbell 0 write: 0x%x\n",
  2733. (uint32_t)val);
  2734. }
  2735. } else {
  2736. epid = val & 0xff;
  2737. streamid = (val >> 16) & 0xffff;
  2738. if (reg > xhci->numslots) {
  2739. DPRINTF("xhci: bad doorbell %d\n", (int)reg);
  2740. } else if (epid == 0 || epid > 31) {
  2741. DPRINTF("xhci: bad doorbell %d write: 0x%x\n",
  2742. (int)reg, (uint32_t)val);
  2743. } else {
  2744. xhci_kick_ep(xhci, reg, epid, streamid);
  2745. }
  2746. }
  2747. }
  2748. static void xhci_cap_write(void *opaque, hwaddr addr, uint64_t val,
  2749. unsigned width)
  2750. {
  2751. /* nothing */
  2752. }
  2753. static const MemoryRegionOps xhci_cap_ops = {
  2754. .read = xhci_cap_read,
  2755. .write = xhci_cap_write,
  2756. .valid.min_access_size = 1,
  2757. .valid.max_access_size = 4,
  2758. .impl.min_access_size = 4,
  2759. .impl.max_access_size = 4,
  2760. .endianness = DEVICE_LITTLE_ENDIAN,
  2761. };
  2762. static const MemoryRegionOps xhci_oper_ops = {
  2763. .read = xhci_oper_read,
  2764. .write = xhci_oper_write,
  2765. .valid.min_access_size = 4,
  2766. .valid.max_access_size = sizeof(dma_addr_t),
  2767. .endianness = DEVICE_LITTLE_ENDIAN,
  2768. };
  2769. static const MemoryRegionOps xhci_port_ops = {
  2770. .read = xhci_port_read,
  2771. .write = xhci_port_write,
  2772. .valid.min_access_size = 4,
  2773. .valid.max_access_size = 4,
  2774. .endianness = DEVICE_LITTLE_ENDIAN,
  2775. };
  2776. static const MemoryRegionOps xhci_runtime_ops = {
  2777. .read = xhci_runtime_read,
  2778. .write = xhci_runtime_write,
  2779. .valid.min_access_size = 4,
  2780. .valid.max_access_size = sizeof(dma_addr_t),
  2781. .endianness = DEVICE_LITTLE_ENDIAN,
  2782. };
  2783. static const MemoryRegionOps xhci_doorbell_ops = {
  2784. .read = xhci_doorbell_read,
  2785. .write = xhci_doorbell_write,
  2786. .valid.min_access_size = 4,
  2787. .valid.max_access_size = 4,
  2788. .endianness = DEVICE_LITTLE_ENDIAN,
  2789. };
  2790. static void xhci_attach(USBPort *usbport)
  2791. {
  2792. XHCIState *xhci = usbport->opaque;
  2793. XHCIPort *port = xhci_lookup_port(xhci, usbport);
  2794. xhci_port_update(port, 0);
  2795. }
  2796. static void xhci_detach(USBPort *usbport)
  2797. {
  2798. XHCIState *xhci = usbport->opaque;
  2799. XHCIPort *port = xhci_lookup_port(xhci, usbport);
  2800. xhci_detach_slot(xhci, usbport);
  2801. xhci_port_update(port, 1);
  2802. }
  2803. static void xhci_wakeup(USBPort *usbport)
  2804. {
  2805. XHCIState *xhci = usbport->opaque;
  2806. XHCIPort *port = xhci_lookup_port(xhci, usbport);
  2807. assert(port);
  2808. if (get_field(port->portsc, PORTSC_PLS) != PLS_U3) {
  2809. return;
  2810. }
  2811. set_field(&port->portsc, PLS_RESUME, PORTSC_PLS);
  2812. xhci_port_notify(port, PORTSC_PLC);
  2813. }
  2814. static void xhci_complete(USBPort *port, USBPacket *packet)
  2815. {
  2816. XHCITransfer *xfer = container_of(packet, XHCITransfer, packet);
  2817. if (packet->status == USB_RET_REMOVE_FROM_QUEUE) {
  2818. xhci_ep_nuke_one_xfer(xfer, 0);
  2819. return;
  2820. }
  2821. xhci_try_complete_packet(xfer);
  2822. xhci_kick_epctx(xfer->epctx, xfer->streamid);
  2823. if (xfer->complete) {
  2824. xhci_ep_free_xfer(xfer);
  2825. }
  2826. }
  2827. static void xhci_child_detach(USBPort *uport, USBDevice *child)
  2828. {
  2829. USBBus *bus = usb_bus_from_device(child);
  2830. XHCIState *xhci = container_of(bus, XHCIState, bus);
  2831. xhci_detach_slot(xhci, child->port);
  2832. }
  2833. static USBPortOps xhci_uport_ops = {
  2834. .attach = xhci_attach,
  2835. .detach = xhci_detach,
  2836. .wakeup = xhci_wakeup,
  2837. .complete = xhci_complete,
  2838. .child_detach = xhci_child_detach,
  2839. };
  2840. static int xhci_find_epid(USBEndpoint *ep)
  2841. {
  2842. if (ep->nr == 0) {
  2843. return 1;
  2844. }
  2845. if (ep->pid == USB_TOKEN_IN) {
  2846. return ep->nr * 2 + 1;
  2847. } else {
  2848. return ep->nr * 2;
  2849. }
  2850. }
  2851. static USBEndpoint *xhci_epid_to_usbep(XHCIEPContext *epctx)
  2852. {
  2853. USBPort *uport;
  2854. uint32_t token;
  2855. if (!epctx) {
  2856. return NULL;
  2857. }
  2858. uport = epctx->xhci->slots[epctx->slotid - 1].uport;
  2859. if (!uport || !uport->dev) {
  2860. return NULL;
  2861. }
  2862. token = (epctx->epid & 1) ? USB_TOKEN_IN : USB_TOKEN_OUT;
  2863. return usb_ep_get(uport->dev, token, epctx->epid >> 1);
  2864. }
  2865. static void xhci_wakeup_endpoint(USBBus *bus, USBEndpoint *ep,
  2866. unsigned int stream)
  2867. {
  2868. XHCIState *xhci = container_of(bus, XHCIState, bus);
  2869. int slotid;
  2870. DPRINTF("%s\n", __func__);
  2871. slotid = ep->dev->addr;
  2872. if (slotid == 0 || slotid > xhci->numslots ||
  2873. !xhci->slots[slotid - 1].enabled) {
  2874. DPRINTF("%s: oops, no slot for dev %d\n", __func__, ep->dev->addr);
  2875. return;
  2876. }
  2877. xhci_kick_ep(xhci, slotid, xhci_find_epid(ep), stream);
  2878. }
  2879. static USBBusOps xhci_bus_ops = {
  2880. .wakeup_endpoint = xhci_wakeup_endpoint,
  2881. };
  2882. static void usb_xhci_init(XHCIState *xhci)
  2883. {
  2884. XHCIPort *port;
  2885. unsigned int i, usbports, speedmask;
  2886. xhci->usbsts = USBSTS_HCH;
  2887. if (xhci->numports_2 > XHCI_MAXPORTS_2) {
  2888. xhci->numports_2 = XHCI_MAXPORTS_2;
  2889. }
  2890. if (xhci->numports_3 > XHCI_MAXPORTS_3) {
  2891. xhci->numports_3 = XHCI_MAXPORTS_3;
  2892. }
  2893. usbports = MAX(xhci->numports_2, xhci->numports_3);
  2894. xhci->numports = xhci->numports_2 + xhci->numports_3;
  2895. usb_bus_new(&xhci->bus, sizeof(xhci->bus), &xhci_bus_ops, xhci->hostOpaque);
  2896. for (i = 0; i < usbports; i++) {
  2897. speedmask = 0;
  2898. if (i < xhci->numports_2) {
  2899. if (xhci_get_flag(xhci, XHCI_FLAG_SS_FIRST)) {
  2900. port = &xhci->ports[i + xhci->numports_3];
  2901. port->portnr = i + 1 + xhci->numports_3;
  2902. } else {
  2903. port = &xhci->ports[i];
  2904. port->portnr = i + 1;
  2905. }
  2906. port->uport = &xhci->uports[i];
  2907. port->speedmask =
  2908. USB_SPEED_MASK_LOW |
  2909. USB_SPEED_MASK_FULL |
  2910. USB_SPEED_MASK_HIGH;
  2911. assert(i < XHCI_MAXPORTS);
  2912. snprintf(port->name, sizeof(port->name), "usb2 port #%d", i+1);
  2913. speedmask |= port->speedmask;
  2914. }
  2915. if (i < xhci->numports_3) {
  2916. if (xhci_get_flag(xhci, XHCI_FLAG_SS_FIRST)) {
  2917. port = &xhci->ports[i];
  2918. port->portnr = i + 1;
  2919. } else {
  2920. port = &xhci->ports[i + xhci->numports_2];
  2921. port->portnr = i + 1 + xhci->numports_2;
  2922. }
  2923. port->uport = &xhci->uports[i];
  2924. port->speedmask = USB_SPEED_MASK_SUPER;
  2925. assert(i < XHCI_MAXPORTS);
  2926. snprintf(port->name, sizeof(port->name), "usb3 port #%d", i+1);
  2927. speedmask |= port->speedmask;
  2928. }
  2929. usb_register_port(&xhci->bus, &xhci->uports[i], xhci, i,
  2930. &xhci_uport_ops, speedmask);
  2931. }
  2932. }
  2933. static void usb_xhci_realize(DeviceState *dev, Error **errp)
  2934. {
  2935. int i;
  2936. XHCIState *xhci = XHCI(dev);
  2937. if (xhci->numintrs > XHCI_MAXINTRS) {
  2938. xhci->numintrs = XHCI_MAXINTRS;
  2939. }
  2940. while (xhci->numintrs & (xhci->numintrs - 1)) { /* ! power of 2 */
  2941. xhci->numintrs++;
  2942. }
  2943. if (xhci->numintrs < 1) {
  2944. xhci->numintrs = 1;
  2945. }
  2946. if (xhci->numslots > XHCI_MAXSLOTS) {
  2947. xhci->numslots = XHCI_MAXSLOTS;
  2948. }
  2949. if (xhci->numslots < 1) {
  2950. xhci->numslots = 1;
  2951. }
  2952. if (xhci_get_flag(xhci, XHCI_FLAG_ENABLE_STREAMS)) {
  2953. xhci->max_pstreams_mask = 7; /* == 256 primary streams */
  2954. } else {
  2955. xhci->max_pstreams_mask = 0;
  2956. }
  2957. usb_xhci_init(xhci);
  2958. xhci->mfwrap_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, xhci_mfwrap_timer, xhci);
  2959. memory_region_init(&xhci->mem, OBJECT(dev), "xhci", XHCI_LEN_REGS);
  2960. memory_region_init_io(&xhci->mem_cap, OBJECT(dev), &xhci_cap_ops, xhci,
  2961. "capabilities", LEN_CAP);
  2962. memory_region_init_io(&xhci->mem_oper, OBJECT(dev), &xhci_oper_ops, xhci,
  2963. "operational", 0x400);
  2964. memory_region_init_io(&xhci->mem_runtime, OBJECT(dev), &xhci_runtime_ops,
  2965. xhci, "runtime", LEN_RUNTIME);
  2966. memory_region_init_io(&xhci->mem_doorbell, OBJECT(dev), &xhci_doorbell_ops,
  2967. xhci, "doorbell", LEN_DOORBELL);
  2968. memory_region_add_subregion(&xhci->mem, 0, &xhci->mem_cap);
  2969. memory_region_add_subregion(&xhci->mem, OFF_OPER, &xhci->mem_oper);
  2970. memory_region_add_subregion(&xhci->mem, OFF_RUNTIME, &xhci->mem_runtime);
  2971. memory_region_add_subregion(&xhci->mem, OFF_DOORBELL, &xhci->mem_doorbell);
  2972. for (i = 0; i < xhci->numports; i++) {
  2973. XHCIPort *port = &xhci->ports[i];
  2974. uint32_t offset = OFF_OPER + 0x400 + 0x10 * i;
  2975. port->xhci = xhci;
  2976. memory_region_init_io(&port->mem, OBJECT(dev), &xhci_port_ops, port,
  2977. port->name, 0x10);
  2978. memory_region_add_subregion(&xhci->mem, offset, &port->mem);
  2979. }
  2980. }
  2981. static void usb_xhci_unrealize(DeviceState *dev)
  2982. {
  2983. int i;
  2984. XHCIState *xhci = XHCI(dev);
  2985. trace_usb_xhci_exit();
  2986. for (i = 0; i < xhci->numslots; i++) {
  2987. xhci_disable_slot(xhci, i + 1);
  2988. }
  2989. if (xhci->mfwrap_timer) {
  2990. timer_free(xhci->mfwrap_timer);
  2991. xhci->mfwrap_timer = NULL;
  2992. }
  2993. memory_region_del_subregion(&xhci->mem, &xhci->mem_cap);
  2994. memory_region_del_subregion(&xhci->mem, &xhci->mem_oper);
  2995. memory_region_del_subregion(&xhci->mem, &xhci->mem_runtime);
  2996. memory_region_del_subregion(&xhci->mem, &xhci->mem_doorbell);
  2997. for (i = 0; i < xhci->numports; i++) {
  2998. XHCIPort *port = &xhci->ports[i];
  2999. memory_region_del_subregion(&xhci->mem, &port->mem);
  3000. }
  3001. usb_bus_release(&xhci->bus);
  3002. }
  3003. static int usb_xhci_post_load(void *opaque, int version_id)
  3004. {
  3005. XHCIState *xhci = opaque;
  3006. XHCISlot *slot;
  3007. XHCIEPContext *epctx;
  3008. dma_addr_t dcbaap, pctx;
  3009. uint32_t slot_ctx[4];
  3010. uint32_t ep_ctx[5];
  3011. int slotid, epid, state;
  3012. uint64_t addr;
  3013. dcbaap = xhci_addr64(xhci->dcbaap_low, xhci->dcbaap_high);
  3014. for (slotid = 1; slotid <= xhci->numslots; slotid++) {
  3015. slot = &xhci->slots[slotid-1];
  3016. if (!slot->addressed) {
  3017. continue;
  3018. }
  3019. ldq_le_dma(xhci->as, dcbaap + 8 * slotid, &addr, MEMTXATTRS_UNSPECIFIED);
  3020. slot->ctx = xhci_mask64(addr);
  3021. xhci_dma_read_u32s(xhci, slot->ctx, slot_ctx, sizeof(slot_ctx));
  3022. slot->uport = xhci_lookup_uport(xhci, slot_ctx);
  3023. if (!slot->uport) {
  3024. /* should not happen, but may trigger on guest bugs */
  3025. slot->enabled = 0;
  3026. slot->addressed = 0;
  3027. continue;
  3028. }
  3029. assert(slot->uport && slot->uport->dev);
  3030. for (epid = 1; epid <= 31; epid++) {
  3031. pctx = slot->ctx + 32 * epid;
  3032. xhci_dma_read_u32s(xhci, pctx, ep_ctx, sizeof(ep_ctx));
  3033. state = ep_ctx[0] & EP_STATE_MASK;
  3034. if (state == EP_DISABLED) {
  3035. continue;
  3036. }
  3037. epctx = xhci_alloc_epctx(xhci, slotid, epid);
  3038. slot->eps[epid-1] = epctx;
  3039. xhci_init_epctx(epctx, pctx, ep_ctx);
  3040. epctx->state = state;
  3041. if (state == EP_RUNNING) {
  3042. /* kick endpoint after vmload is finished */
  3043. timer_mod(epctx->kick_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL));
  3044. }
  3045. }
  3046. }
  3047. return 0;
  3048. }
  3049. static const VMStateDescription vmstate_xhci_ring = {
  3050. .name = "xhci-ring",
  3051. .version_id = 1,
  3052. .fields = (VMStateField[]) {
  3053. VMSTATE_UINT64(dequeue, XHCIRing),
  3054. VMSTATE_BOOL(ccs, XHCIRing),
  3055. VMSTATE_END_OF_LIST()
  3056. }
  3057. };
  3058. static const VMStateDescription vmstate_xhci_port = {
  3059. .name = "xhci-port",
  3060. .version_id = 1,
  3061. .fields = (VMStateField[]) {
  3062. VMSTATE_UINT32(portsc, XHCIPort),
  3063. VMSTATE_END_OF_LIST()
  3064. }
  3065. };
  3066. static const VMStateDescription vmstate_xhci_slot = {
  3067. .name = "xhci-slot",
  3068. .version_id = 1,
  3069. .fields = (VMStateField[]) {
  3070. VMSTATE_BOOL(enabled, XHCISlot),
  3071. VMSTATE_BOOL(addressed, XHCISlot),
  3072. VMSTATE_END_OF_LIST()
  3073. }
  3074. };
  3075. static const VMStateDescription vmstate_xhci_event = {
  3076. .name = "xhci-event",
  3077. .version_id = 1,
  3078. .fields = (VMStateField[]) {
  3079. VMSTATE_UINT32(type, XHCIEvent),
  3080. VMSTATE_UINT32(ccode, XHCIEvent),
  3081. VMSTATE_UINT64(ptr, XHCIEvent),
  3082. VMSTATE_UINT32(length, XHCIEvent),
  3083. VMSTATE_UINT32(flags, XHCIEvent),
  3084. VMSTATE_UINT8(slotid, XHCIEvent),
  3085. VMSTATE_UINT8(epid, XHCIEvent),
  3086. VMSTATE_END_OF_LIST()
  3087. }
  3088. };
  3089. static bool xhci_er_full(void *opaque, int version_id)
  3090. {
  3091. return false;
  3092. }
  3093. static const VMStateDescription vmstate_xhci_intr = {
  3094. .name = "xhci-intr",
  3095. .version_id = 1,
  3096. .fields = (VMStateField[]) {
  3097. /* registers */
  3098. VMSTATE_UINT32(iman, XHCIInterrupter),
  3099. VMSTATE_UINT32(imod, XHCIInterrupter),
  3100. VMSTATE_UINT32(erstsz, XHCIInterrupter),
  3101. VMSTATE_UINT32(erstba_low, XHCIInterrupter),
  3102. VMSTATE_UINT32(erstba_high, XHCIInterrupter),
  3103. VMSTATE_UINT32(erdp_low, XHCIInterrupter),
  3104. VMSTATE_UINT32(erdp_high, XHCIInterrupter),
  3105. /* state */
  3106. VMSTATE_BOOL(msix_used, XHCIInterrupter),
  3107. VMSTATE_BOOL(er_pcs, XHCIInterrupter),
  3108. VMSTATE_UINT64(er_start, XHCIInterrupter),
  3109. VMSTATE_UINT32(er_size, XHCIInterrupter),
  3110. VMSTATE_UINT32(er_ep_idx, XHCIInterrupter),
  3111. /* event queue (used if ring is full) */
  3112. VMSTATE_BOOL(er_full_unused, XHCIInterrupter),
  3113. VMSTATE_UINT32_TEST(ev_buffer_put, XHCIInterrupter, xhci_er_full),
  3114. VMSTATE_UINT32_TEST(ev_buffer_get, XHCIInterrupter, xhci_er_full),
  3115. VMSTATE_STRUCT_ARRAY_TEST(ev_buffer, XHCIInterrupter, EV_QUEUE,
  3116. xhci_er_full, 1,
  3117. vmstate_xhci_event, XHCIEvent),
  3118. VMSTATE_END_OF_LIST()
  3119. }
  3120. };
  3121. const VMStateDescription vmstate_xhci = {
  3122. .name = "xhci-core",
  3123. .version_id = 1,
  3124. .post_load = usb_xhci_post_load,
  3125. .fields = (VMStateField[]) {
  3126. VMSTATE_STRUCT_VARRAY_UINT32(ports, XHCIState, numports, 1,
  3127. vmstate_xhci_port, XHCIPort),
  3128. VMSTATE_STRUCT_VARRAY_UINT32(slots, XHCIState, numslots, 1,
  3129. vmstate_xhci_slot, XHCISlot),
  3130. VMSTATE_STRUCT_VARRAY_UINT32(intr, XHCIState, numintrs, 1,
  3131. vmstate_xhci_intr, XHCIInterrupter),
  3132. /* Operational Registers */
  3133. VMSTATE_UINT32(usbcmd, XHCIState),
  3134. VMSTATE_UINT32(usbsts, XHCIState),
  3135. VMSTATE_UINT32(dnctrl, XHCIState),
  3136. VMSTATE_UINT32(crcr_low, XHCIState),
  3137. VMSTATE_UINT32(crcr_high, XHCIState),
  3138. VMSTATE_UINT32(dcbaap_low, XHCIState),
  3139. VMSTATE_UINT32(dcbaap_high, XHCIState),
  3140. VMSTATE_UINT32(config, XHCIState),
  3141. /* Runtime Registers & state */
  3142. VMSTATE_INT64(mfindex_start, XHCIState),
  3143. VMSTATE_TIMER_PTR(mfwrap_timer, XHCIState),
  3144. VMSTATE_STRUCT(cmd_ring, XHCIState, 1, vmstate_xhci_ring, XHCIRing),
  3145. VMSTATE_END_OF_LIST()
  3146. }
  3147. };
  3148. static Property xhci_properties[] = {
  3149. DEFINE_PROP_BIT("streams", XHCIState, flags,
  3150. XHCI_FLAG_ENABLE_STREAMS, true),
  3151. DEFINE_PROP_UINT32("p2", XHCIState, numports_2, 4),
  3152. DEFINE_PROP_UINT32("p3", XHCIState, numports_3, 4),
  3153. DEFINE_PROP_LINK("host", XHCIState, hostOpaque, TYPE_DEVICE,
  3154. DeviceState *),
  3155. DEFINE_PROP_END_OF_LIST(),
  3156. };
  3157. static void xhci_class_init(ObjectClass *klass, void *data)
  3158. {
  3159. DeviceClass *dc = DEVICE_CLASS(klass);
  3160. dc->realize = usb_xhci_realize;
  3161. dc->unrealize = usb_xhci_unrealize;
  3162. dc->reset = xhci_reset;
  3163. device_class_set_props(dc, xhci_properties);
  3164. dc->user_creatable = false;
  3165. }
  3166. static const TypeInfo xhci_info = {
  3167. .name = TYPE_XHCI,
  3168. .parent = TYPE_DEVICE,
  3169. .instance_size = sizeof(XHCIState),
  3170. .class_init = xhci_class_init,
  3171. };
  3172. static void xhci_register_types(void)
  3173. {
  3174. type_register_static(&xhci_info);
  3175. }
  3176. type_init(xhci_register_types)