2
0

hcd-xhci.c 107 KB

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