2
0

hcd-xhci.c 105 KB

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