hcd-xhci.c 109 KB

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