hcd-xhci.c 109 KB

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