hcd-xhci.c 92 KB

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