pci-hyperv.c 116 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) Microsoft Corporation.
  4. *
  5. * Author:
  6. * Jake Oshins <jakeo@microsoft.com>
  7. *
  8. * This driver acts as a paravirtual front-end for PCI Express root buses.
  9. * When a PCI Express function (either an entire device or an SR-IOV
  10. * Virtual Function) is being passed through to the VM, this driver exposes
  11. * a new bus to the guest VM. This is modeled as a root PCI bus because
  12. * no bridges are being exposed to the VM. In fact, with a "Generation 2"
  13. * VM within Hyper-V, there may seem to be no PCI bus at all in the VM
  14. * until a device as been exposed using this driver.
  15. *
  16. * Each root PCI bus has its own PCI domain, which is called "Segment" in
  17. * the PCI Firmware Specifications. Thus while each device passed through
  18. * to the VM using this front-end will appear at "device 0", the domain will
  19. * be unique. Typically, each bus will have one PCI function on it, though
  20. * this driver does support more than one.
  21. *
  22. * In order to map the interrupts from the device through to the guest VM,
  23. * this driver also implements an IRQ Domain, which handles interrupts (either
  24. * MSI or MSI-X) associated with the functions on the bus. As interrupts are
  25. * set up, torn down, or reaffined, this driver communicates with the
  26. * underlying hypervisor to adjust the mappings in the I/O MMU so that each
  27. * interrupt will be delivered to the correct virtual processor at the right
  28. * vector. This driver does not support level-triggered (line-based)
  29. * interrupts, and will report that the Interrupt Line register in the
  30. * function's configuration space is zero.
  31. *
  32. * The rest of this driver mostly maps PCI concepts onto underlying Hyper-V
  33. * facilities. For instance, the configuration space of a function exposed
  34. * by Hyper-V is mapped into a single page of memory space, and the
  35. * read and write handlers for config space must be aware of this mechanism.
  36. * Similarly, device setup and teardown involves messages sent to and from
  37. * the PCI back-end driver in Hyper-V.
  38. */
  39. #include <linux/kernel.h>
  40. #include <linux/module.h>
  41. #include <linux/pci.h>
  42. #include <linux/pci-ecam.h>
  43. #include <linux/delay.h>
  44. #include <linux/semaphore.h>
  45. #include <linux/irq.h>
  46. #include <linux/irqchip/irq-msi-lib.h>
  47. #include <linux/msi.h>
  48. #include <linux/hyperv.h>
  49. #include <linux/refcount.h>
  50. #include <linux/irqdomain.h>
  51. #include <linux/acpi.h>
  52. #include <linux/sizes.h>
  53. #include <linux/of_irq.h>
  54. #include <asm/mshyperv.h>
  55. /*
  56. * Protocol versions. The low word is the minor version, the high word the
  57. * major version.
  58. */
  59. #define PCI_MAKE_VERSION(major, minor) ((u32)(((major) << 16) | (minor)))
  60. #define PCI_MAJOR_VERSION(version) ((u32)(version) >> 16)
  61. #define PCI_MINOR_VERSION(version) ((u32)(version) & 0xff)
  62. enum pci_protocol_version_t {
  63. PCI_PROTOCOL_VERSION_1_1 = PCI_MAKE_VERSION(1, 1), /* Win10 */
  64. PCI_PROTOCOL_VERSION_1_2 = PCI_MAKE_VERSION(1, 2), /* RS1 */
  65. PCI_PROTOCOL_VERSION_1_3 = PCI_MAKE_VERSION(1, 3), /* Vibranium */
  66. PCI_PROTOCOL_VERSION_1_4 = PCI_MAKE_VERSION(1, 4), /* WS2022 */
  67. };
  68. #define CPU_AFFINITY_ALL -1ULL
  69. /*
  70. * Supported protocol versions in the order of probing - highest go
  71. * first.
  72. */
  73. static enum pci_protocol_version_t pci_protocol_versions[] = {
  74. PCI_PROTOCOL_VERSION_1_4,
  75. PCI_PROTOCOL_VERSION_1_3,
  76. PCI_PROTOCOL_VERSION_1_2,
  77. PCI_PROTOCOL_VERSION_1_1,
  78. };
  79. #define PCI_CONFIG_MMIO_LENGTH 0x2000
  80. #define CFG_PAGE_OFFSET 0x1000
  81. #define CFG_PAGE_SIZE (PCI_CONFIG_MMIO_LENGTH - CFG_PAGE_OFFSET)
  82. #define MAX_SUPPORTED_MSI_MESSAGES 0x400
  83. #define STATUS_REVISION_MISMATCH 0xC0000059
  84. /* space for 32bit serial number as string */
  85. #define SLOT_NAME_SIZE 11
  86. /*
  87. * Size of requestor for VMbus; the value is based on the observation
  88. * that having more than one request outstanding is 'rare', and so 64
  89. * should be generous in ensuring that we don't ever run out.
  90. */
  91. #define HV_PCI_RQSTOR_SIZE 64
  92. /*
  93. * Message Types
  94. */
  95. enum pci_message_type {
  96. /*
  97. * Version 1.1
  98. */
  99. PCI_MESSAGE_BASE = 0x42490000,
  100. PCI_BUS_RELATIONS = PCI_MESSAGE_BASE + 0,
  101. PCI_QUERY_BUS_RELATIONS = PCI_MESSAGE_BASE + 1,
  102. PCI_POWER_STATE_CHANGE = PCI_MESSAGE_BASE + 4,
  103. PCI_QUERY_RESOURCE_REQUIREMENTS = PCI_MESSAGE_BASE + 5,
  104. PCI_QUERY_RESOURCE_RESOURCES = PCI_MESSAGE_BASE + 6,
  105. PCI_BUS_D0ENTRY = PCI_MESSAGE_BASE + 7,
  106. PCI_BUS_D0EXIT = PCI_MESSAGE_BASE + 8,
  107. PCI_READ_BLOCK = PCI_MESSAGE_BASE + 9,
  108. PCI_WRITE_BLOCK = PCI_MESSAGE_BASE + 0xA,
  109. PCI_EJECT = PCI_MESSAGE_BASE + 0xB,
  110. PCI_QUERY_STOP = PCI_MESSAGE_BASE + 0xC,
  111. PCI_REENABLE = PCI_MESSAGE_BASE + 0xD,
  112. PCI_QUERY_STOP_FAILED = PCI_MESSAGE_BASE + 0xE,
  113. PCI_EJECTION_COMPLETE = PCI_MESSAGE_BASE + 0xF,
  114. PCI_RESOURCES_ASSIGNED = PCI_MESSAGE_BASE + 0x10,
  115. PCI_RESOURCES_RELEASED = PCI_MESSAGE_BASE + 0x11,
  116. PCI_INVALIDATE_BLOCK = PCI_MESSAGE_BASE + 0x12,
  117. PCI_QUERY_PROTOCOL_VERSION = PCI_MESSAGE_BASE + 0x13,
  118. PCI_CREATE_INTERRUPT_MESSAGE = PCI_MESSAGE_BASE + 0x14,
  119. PCI_DELETE_INTERRUPT_MESSAGE = PCI_MESSAGE_BASE + 0x15,
  120. PCI_RESOURCES_ASSIGNED2 = PCI_MESSAGE_BASE + 0x16,
  121. PCI_CREATE_INTERRUPT_MESSAGE2 = PCI_MESSAGE_BASE + 0x17,
  122. PCI_DELETE_INTERRUPT_MESSAGE2 = PCI_MESSAGE_BASE + 0x18, /* unused */
  123. PCI_BUS_RELATIONS2 = PCI_MESSAGE_BASE + 0x19,
  124. PCI_RESOURCES_ASSIGNED3 = PCI_MESSAGE_BASE + 0x1A,
  125. PCI_CREATE_INTERRUPT_MESSAGE3 = PCI_MESSAGE_BASE + 0x1B,
  126. PCI_MESSAGE_MAXIMUM
  127. };
  128. /*
  129. * Structures defining the virtual PCI Express protocol.
  130. */
  131. union pci_version {
  132. struct {
  133. u16 minor_version;
  134. u16 major_version;
  135. } parts;
  136. u32 version;
  137. } __packed;
  138. /*
  139. * Function numbers are 8-bits wide on Express, as interpreted through ARI,
  140. * which is all this driver does. This representation is the one used in
  141. * Windows, which is what is expected when sending this back and forth with
  142. * the Hyper-V parent partition.
  143. */
  144. union win_slot_encoding {
  145. struct {
  146. u32 dev:5;
  147. u32 func:3;
  148. u32 reserved:24;
  149. } bits;
  150. u32 slot;
  151. } __packed;
  152. /*
  153. * Pretty much as defined in the PCI Specifications.
  154. */
  155. struct pci_function_description {
  156. u16 v_id; /* vendor ID */
  157. u16 d_id; /* device ID */
  158. u8 rev;
  159. u8 prog_intf;
  160. u8 subclass;
  161. u8 base_class;
  162. u32 subsystem_id;
  163. union win_slot_encoding win_slot;
  164. u32 ser; /* serial number */
  165. } __packed;
  166. enum pci_device_description_flags {
  167. HV_PCI_DEVICE_FLAG_NONE = 0x0,
  168. HV_PCI_DEVICE_FLAG_NUMA_AFFINITY = 0x1,
  169. };
  170. struct pci_function_description2 {
  171. u16 v_id; /* vendor ID */
  172. u16 d_id; /* device ID */
  173. u8 rev;
  174. u8 prog_intf;
  175. u8 subclass;
  176. u8 base_class;
  177. u32 subsystem_id;
  178. union win_slot_encoding win_slot;
  179. u32 ser; /* serial number */
  180. u32 flags;
  181. u16 virtual_numa_node;
  182. u16 reserved;
  183. } __packed;
  184. /**
  185. * struct hv_msi_desc
  186. * @vector: IDT entry
  187. * @delivery_mode: As defined in Intel's Programmer's
  188. * Reference Manual, Volume 3, Chapter 8.
  189. * @vector_count: Number of contiguous entries in the
  190. * Interrupt Descriptor Table that are
  191. * occupied by this Message-Signaled
  192. * Interrupt. For "MSI", as first defined
  193. * in PCI 2.2, this can be between 1 and
  194. * 32. For "MSI-X," as first defined in PCI
  195. * 3.0, this must be 1, as each MSI-X table
  196. * entry would have its own descriptor.
  197. * @reserved: Empty space
  198. * @cpu_mask: All the target virtual processors.
  199. */
  200. struct hv_msi_desc {
  201. u8 vector;
  202. u8 delivery_mode;
  203. u16 vector_count;
  204. u32 reserved;
  205. u64 cpu_mask;
  206. } __packed;
  207. /**
  208. * struct hv_msi_desc2 - 1.2 version of hv_msi_desc
  209. * @vector: IDT entry
  210. * @delivery_mode: As defined in Intel's Programmer's
  211. * Reference Manual, Volume 3, Chapter 8.
  212. * @vector_count: Number of contiguous entries in the
  213. * Interrupt Descriptor Table that are
  214. * occupied by this Message-Signaled
  215. * Interrupt. For "MSI", as first defined
  216. * in PCI 2.2, this can be between 1 and
  217. * 32. For "MSI-X," as first defined in PCI
  218. * 3.0, this must be 1, as each MSI-X table
  219. * entry would have its own descriptor.
  220. * @processor_count: number of bits enabled in array.
  221. * @processor_array: All the target virtual processors.
  222. */
  223. struct hv_msi_desc2 {
  224. u8 vector;
  225. u8 delivery_mode;
  226. u16 vector_count;
  227. u16 processor_count;
  228. u16 processor_array[32];
  229. } __packed;
  230. /*
  231. * struct hv_msi_desc3 - 1.3 version of hv_msi_desc
  232. * Everything is the same as in 'hv_msi_desc2' except that the size of the
  233. * 'vector' field is larger to support bigger vector values. For ex: LPI
  234. * vectors on ARM.
  235. */
  236. struct hv_msi_desc3 {
  237. u32 vector;
  238. u8 delivery_mode;
  239. u8 reserved;
  240. u16 vector_count;
  241. u16 processor_count;
  242. u16 processor_array[32];
  243. } __packed;
  244. /**
  245. * struct tran_int_desc
  246. * @reserved: unused, padding
  247. * @vector_count: same as in hv_msi_desc
  248. * @data: This is the "data payload" value that is
  249. * written by the device when it generates
  250. * a message-signaled interrupt, either MSI
  251. * or MSI-X.
  252. * @address: This is the address to which the data
  253. * payload is written on interrupt
  254. * generation.
  255. */
  256. struct tran_int_desc {
  257. u16 reserved;
  258. u16 vector_count;
  259. u32 data;
  260. u64 address;
  261. } __packed;
  262. /*
  263. * A generic message format for virtual PCI.
  264. * Specific message formats are defined later in the file.
  265. */
  266. struct pci_message {
  267. u32 type;
  268. } __packed;
  269. struct pci_child_message {
  270. struct pci_message message_type;
  271. union win_slot_encoding wslot;
  272. } __packed;
  273. struct pci_incoming_message {
  274. struct vmpacket_descriptor hdr;
  275. struct pci_message message_type;
  276. } __packed;
  277. struct pci_response {
  278. struct vmpacket_descriptor hdr;
  279. s32 status; /* negative values are failures */
  280. } __packed;
  281. struct pci_packet {
  282. void (*completion_func)(void *context, struct pci_response *resp,
  283. int resp_packet_size);
  284. void *compl_ctxt;
  285. };
  286. /*
  287. * Specific message types supporting the PCI protocol.
  288. */
  289. /*
  290. * Version negotiation message. Sent from the guest to the host.
  291. * The guest is free to try different versions until the host
  292. * accepts the version.
  293. *
  294. * pci_version: The protocol version requested.
  295. * is_last_attempt: If TRUE, this is the last version guest will request.
  296. * reservedz: Reserved field, set to zero.
  297. */
  298. struct pci_version_request {
  299. struct pci_message message_type;
  300. u32 protocol_version;
  301. } __packed;
  302. /*
  303. * Bus D0 Entry. This is sent from the guest to the host when the virtual
  304. * bus (PCI Express port) is ready for action.
  305. */
  306. struct pci_bus_d0_entry {
  307. struct pci_message message_type;
  308. u32 reserved;
  309. u64 mmio_base;
  310. } __packed;
  311. struct pci_bus_relations {
  312. struct pci_incoming_message incoming;
  313. u32 device_count;
  314. struct pci_function_description func[];
  315. } __packed;
  316. struct pci_bus_relations2 {
  317. struct pci_incoming_message incoming;
  318. u32 device_count;
  319. struct pci_function_description2 func[];
  320. } __packed;
  321. struct pci_q_res_req_response {
  322. struct vmpacket_descriptor hdr;
  323. s32 status; /* negative values are failures */
  324. u32 probed_bar[PCI_STD_NUM_BARS];
  325. } __packed;
  326. struct pci_set_power {
  327. struct pci_message message_type;
  328. union win_slot_encoding wslot;
  329. u32 power_state; /* In Windows terms */
  330. u32 reserved;
  331. } __packed;
  332. struct pci_set_power_response {
  333. struct vmpacket_descriptor hdr;
  334. s32 status; /* negative values are failures */
  335. union win_slot_encoding wslot;
  336. u32 resultant_state; /* In Windows terms */
  337. u32 reserved;
  338. } __packed;
  339. struct pci_resources_assigned {
  340. struct pci_message message_type;
  341. union win_slot_encoding wslot;
  342. u8 memory_range[0x14][6]; /* not used here */
  343. u32 msi_descriptors;
  344. u32 reserved[4];
  345. } __packed;
  346. struct pci_resources_assigned2 {
  347. struct pci_message message_type;
  348. union win_slot_encoding wslot;
  349. u8 memory_range[0x14][6]; /* not used here */
  350. u32 msi_descriptor_count;
  351. u8 reserved[70];
  352. } __packed;
  353. struct pci_create_interrupt {
  354. struct pci_message message_type;
  355. union win_slot_encoding wslot;
  356. struct hv_msi_desc int_desc;
  357. } __packed;
  358. struct pci_create_int_response {
  359. struct pci_response response;
  360. u32 reserved;
  361. struct tran_int_desc int_desc;
  362. } __packed;
  363. struct pci_create_interrupt2 {
  364. struct pci_message message_type;
  365. union win_slot_encoding wslot;
  366. struct hv_msi_desc2 int_desc;
  367. } __packed;
  368. struct pci_create_interrupt3 {
  369. struct pci_message message_type;
  370. union win_slot_encoding wslot;
  371. struct hv_msi_desc3 int_desc;
  372. } __packed;
  373. struct pci_delete_interrupt {
  374. struct pci_message message_type;
  375. union win_slot_encoding wslot;
  376. struct tran_int_desc int_desc;
  377. } __packed;
  378. /*
  379. * Note: the VM must pass a valid block id, wslot and bytes_requested.
  380. */
  381. struct pci_read_block {
  382. struct pci_message message_type;
  383. u32 block_id;
  384. union win_slot_encoding wslot;
  385. u32 bytes_requested;
  386. } __packed;
  387. struct pci_read_block_response {
  388. struct vmpacket_descriptor hdr;
  389. u32 status;
  390. u8 bytes[HV_CONFIG_BLOCK_SIZE_MAX];
  391. } __packed;
  392. /*
  393. * Note: the VM must pass a valid block id, wslot and byte_count.
  394. */
  395. struct pci_write_block {
  396. struct pci_message message_type;
  397. u32 block_id;
  398. union win_slot_encoding wslot;
  399. u32 byte_count;
  400. u8 bytes[HV_CONFIG_BLOCK_SIZE_MAX];
  401. } __packed;
  402. struct pci_dev_inval_block {
  403. struct pci_incoming_message incoming;
  404. union win_slot_encoding wslot;
  405. u64 block_mask;
  406. } __packed;
  407. struct pci_dev_incoming {
  408. struct pci_incoming_message incoming;
  409. union win_slot_encoding wslot;
  410. } __packed;
  411. struct pci_eject_response {
  412. struct pci_message message_type;
  413. union win_slot_encoding wslot;
  414. u32 status;
  415. } __packed;
  416. static int pci_ring_size = VMBUS_RING_SIZE(SZ_16K);
  417. /*
  418. * Driver specific state.
  419. */
  420. enum hv_pcibus_state {
  421. hv_pcibus_init = 0,
  422. hv_pcibus_probed,
  423. hv_pcibus_installed,
  424. hv_pcibus_removing,
  425. hv_pcibus_maximum
  426. };
  427. struct hv_pcibus_device {
  428. #ifdef CONFIG_X86
  429. struct pci_sysdata sysdata;
  430. #elif defined(CONFIG_ARM64)
  431. struct pci_config_window sysdata;
  432. #endif
  433. struct pci_host_bridge *bridge;
  434. struct fwnode_handle *fwnode;
  435. /* Protocol version negotiated with the host */
  436. enum pci_protocol_version_t protocol_version;
  437. struct mutex state_lock;
  438. enum hv_pcibus_state state;
  439. struct hv_device *hdev;
  440. resource_size_t low_mmio_space;
  441. resource_size_t high_mmio_space;
  442. struct resource *mem_config;
  443. struct resource *low_mmio_res;
  444. struct resource *high_mmio_res;
  445. struct completion *survey_event;
  446. spinlock_t config_lock; /* Avoid two threads writing index page */
  447. spinlock_t device_list_lock; /* Protect lists below */
  448. void __iomem *cfg_addr;
  449. struct list_head children;
  450. struct list_head dr_list;
  451. struct irq_domain *irq_domain;
  452. struct workqueue_struct *wq;
  453. /* Highest slot of child device with resources allocated */
  454. int wslot_res_allocated;
  455. bool use_calls; /* Use hypercalls to access mmio cfg space */
  456. };
  457. /*
  458. * Tracks "Device Relations" messages from the host, which must be both
  459. * processed in order and deferred so that they don't run in the context
  460. * of the incoming packet callback.
  461. */
  462. struct hv_dr_work {
  463. struct work_struct wrk;
  464. struct hv_pcibus_device *bus;
  465. };
  466. struct hv_pcidev_description {
  467. u16 v_id; /* vendor ID */
  468. u16 d_id; /* device ID */
  469. u8 rev;
  470. u8 prog_intf;
  471. u8 subclass;
  472. u8 base_class;
  473. u32 subsystem_id;
  474. union win_slot_encoding win_slot;
  475. u32 ser; /* serial number */
  476. u32 flags;
  477. u16 virtual_numa_node;
  478. };
  479. struct hv_dr_state {
  480. struct list_head list_entry;
  481. u32 device_count;
  482. struct hv_pcidev_description func[] __counted_by(device_count);
  483. };
  484. struct hv_pci_dev {
  485. /* List protected by pci_rescan_remove_lock */
  486. struct list_head list_entry;
  487. refcount_t refs;
  488. struct pci_slot *pci_slot;
  489. struct hv_pcidev_description desc;
  490. bool reported_missing;
  491. struct hv_pcibus_device *hbus;
  492. struct work_struct wrk;
  493. void (*block_invalidate)(void *context, u64 block_mask);
  494. void *invalidate_context;
  495. /*
  496. * What would be observed if one wrote 0xFFFFFFFF to a BAR and then
  497. * read it back, for each of the BAR offsets within config space.
  498. */
  499. u32 probed_bar[PCI_STD_NUM_BARS];
  500. };
  501. struct hv_pci_compl {
  502. struct completion host_event;
  503. s32 completion_status;
  504. };
  505. static void hv_pci_onchannelcallback(void *context);
  506. #ifdef CONFIG_X86
  507. #define DELIVERY_MODE APIC_DELIVERY_MODE_FIXED
  508. #define HV_MSI_CHIP_FLAGS MSI_CHIP_FLAG_SET_ACK
  509. static int hv_pci_irqchip_init(void)
  510. {
  511. return 0;
  512. }
  513. static struct irq_domain *hv_pci_get_root_domain(void)
  514. {
  515. return x86_vector_domain;
  516. }
  517. static unsigned int hv_msi_get_int_vector(struct irq_data *data)
  518. {
  519. struct irq_cfg *cfg = irqd_cfg(data);
  520. return cfg->vector;
  521. }
  522. #define hv_msi_prepare pci_msi_prepare
  523. /**
  524. * hv_irq_retarget_interrupt() - "Unmask" the IRQ by setting its current
  525. * affinity.
  526. * @data: Describes the IRQ
  527. *
  528. * Build new a destination for the MSI and make a hypercall to
  529. * update the Interrupt Redirection Table. "Device Logical ID"
  530. * is built out of this PCI bus's instance GUID and the function
  531. * number of the device.
  532. */
  533. static void hv_irq_retarget_interrupt(struct irq_data *data)
  534. {
  535. struct msi_desc *msi_desc = irq_data_get_msi_desc(data);
  536. struct hv_retarget_device_interrupt *params;
  537. struct tran_int_desc *int_desc;
  538. struct hv_pcibus_device *hbus;
  539. const struct cpumask *dest;
  540. cpumask_var_t tmp;
  541. struct pci_bus *pbus;
  542. struct pci_dev *pdev;
  543. unsigned long flags;
  544. u32 var_size = 0;
  545. int cpu, nr_bank;
  546. u64 res;
  547. dest = irq_data_get_effective_affinity_mask(data);
  548. pdev = msi_desc_to_pci_dev(msi_desc);
  549. pbus = pdev->bus;
  550. hbus = container_of(pbus->sysdata, struct hv_pcibus_device, sysdata);
  551. int_desc = data->chip_data;
  552. if (!int_desc) {
  553. dev_warn(&hbus->hdev->device, "%s() can not unmask irq %u\n",
  554. __func__, data->irq);
  555. return;
  556. }
  557. local_irq_save(flags);
  558. params = *this_cpu_ptr(hyperv_pcpu_input_arg);
  559. memset(params, 0, sizeof(*params));
  560. params->partition_id = HV_PARTITION_ID_SELF;
  561. params->int_entry.source = HV_INTERRUPT_SOURCE_MSI;
  562. params->int_entry.msi_entry.address.as_uint32 = int_desc->address & 0xffffffff;
  563. params->int_entry.msi_entry.data.as_uint32 = int_desc->data;
  564. params->device_id = (hbus->hdev->dev_instance.b[5] << 24) |
  565. (hbus->hdev->dev_instance.b[4] << 16) |
  566. (hbus->hdev->dev_instance.b[7] << 8) |
  567. (hbus->hdev->dev_instance.b[6] & 0xf8) |
  568. PCI_FUNC(pdev->devfn);
  569. params->int_target.vector = hv_msi_get_int_vector(data);
  570. if (hbus->protocol_version >= PCI_PROTOCOL_VERSION_1_2) {
  571. /*
  572. * PCI_PROTOCOL_VERSION_1_2 supports the VP_SET version of the
  573. * HVCALL_RETARGET_INTERRUPT hypercall, which also coincides
  574. * with >64 VP support.
  575. * ms_hyperv.hints & HV_X64_EX_PROCESSOR_MASKS_RECOMMENDED
  576. * is not sufficient for this hypercall.
  577. */
  578. params->int_target.flags |=
  579. HV_DEVICE_INTERRUPT_TARGET_PROCESSOR_SET;
  580. if (!alloc_cpumask_var(&tmp, GFP_ATOMIC)) {
  581. res = 1;
  582. goto out;
  583. }
  584. cpumask_and(tmp, dest, cpu_online_mask);
  585. nr_bank = cpumask_to_vpset(&params->int_target.vp_set, tmp);
  586. free_cpumask_var(tmp);
  587. if (nr_bank <= 0) {
  588. res = 1;
  589. goto out;
  590. }
  591. /*
  592. * var-sized hypercall, var-size starts after vp_mask (thus
  593. * vp_set.format does not count, but vp_set.valid_bank_mask
  594. * does).
  595. */
  596. var_size = 1 + nr_bank;
  597. } else {
  598. for_each_cpu_and(cpu, dest, cpu_online_mask) {
  599. params->int_target.vp_mask |=
  600. (1ULL << hv_cpu_number_to_vp_number(cpu));
  601. }
  602. }
  603. res = hv_do_hypercall(HVCALL_RETARGET_INTERRUPT | (var_size << 17),
  604. params, NULL);
  605. out:
  606. local_irq_restore(flags);
  607. /*
  608. * During hibernation, when a CPU is offlined, the kernel tries
  609. * to move the interrupt to the remaining CPUs that haven't
  610. * been offlined yet. In this case, the below hv_do_hypercall()
  611. * always fails since the vmbus channel has been closed:
  612. * refer to cpu_disable_common() -> fixup_irqs() ->
  613. * irq_migrate_all_off_this_cpu() -> migrate_one_irq().
  614. *
  615. * Suppress the error message for hibernation because the failure
  616. * during hibernation does not matter (at this time all the devices
  617. * have been frozen). Note: the correct affinity info is still updated
  618. * into the irqdata data structure in migrate_one_irq() ->
  619. * irq_do_set_affinity(), so later when the VM resumes,
  620. * hv_pci_restore_msi_state() is able to correctly restore the
  621. * interrupt with the correct affinity.
  622. */
  623. if (!hv_result_success(res) && hbus->state != hv_pcibus_removing)
  624. dev_err(&hbus->hdev->device,
  625. "%s() failed: %#llx", __func__, res);
  626. }
  627. static void hv_arch_irq_unmask(struct irq_data *data)
  628. {
  629. if (hv_root_partition())
  630. /*
  631. * In case of the nested root partition, the nested hypervisor
  632. * is taking care of interrupt remapping and thus the
  633. * MAP_DEVICE_INTERRUPT hypercall is required instead of
  634. * RETARGET_INTERRUPT.
  635. */
  636. (void)hv_map_msi_interrupt(data, NULL);
  637. else
  638. hv_irq_retarget_interrupt(data);
  639. }
  640. #elif defined(CONFIG_ARM64)
  641. /*
  642. * SPI vectors to use for vPCI; arch SPIs range is [32, 1019], but leaving a bit
  643. * of room at the start to allow for SPIs to be specified through ACPI and
  644. * starting with a power of two to satisfy power of 2 multi-MSI requirement.
  645. */
  646. #define HV_PCI_MSI_SPI_START 64
  647. #define HV_PCI_MSI_SPI_NR (1020 - HV_PCI_MSI_SPI_START)
  648. #define DELIVERY_MODE 0
  649. #define HV_MSI_CHIP_FLAGS MSI_CHIP_FLAG_SET_EOI
  650. #define hv_msi_prepare NULL
  651. struct hv_pci_chip_data {
  652. DECLARE_BITMAP(spi_map, HV_PCI_MSI_SPI_NR);
  653. struct mutex map_lock;
  654. };
  655. /* Hyper-V vPCI MSI GIC IRQ domain */
  656. static struct irq_domain *hv_msi_gic_irq_domain;
  657. /* Hyper-V PCI MSI IRQ chip */
  658. static struct irq_chip hv_arm64_msi_irq_chip = {
  659. .name = "MSI",
  660. .irq_set_affinity = irq_chip_set_affinity_parent,
  661. .irq_eoi = irq_chip_eoi_parent,
  662. .irq_mask = irq_chip_mask_parent,
  663. .irq_unmask = irq_chip_unmask_parent
  664. };
  665. static unsigned int hv_msi_get_int_vector(struct irq_data *irqd)
  666. {
  667. return irqd->parent_data->hwirq;
  668. }
  669. /*
  670. * @nr_bm_irqs: Indicates the number of IRQs that were allocated from
  671. * the bitmap.
  672. * @nr_dom_irqs: Indicates the number of IRQs that were allocated from
  673. * the parent domain.
  674. */
  675. static void hv_pci_vec_irq_free(struct irq_domain *domain,
  676. unsigned int virq,
  677. unsigned int nr_bm_irqs,
  678. unsigned int nr_dom_irqs)
  679. {
  680. struct hv_pci_chip_data *chip_data = domain->host_data;
  681. struct irq_data *d = irq_domain_get_irq_data(domain, virq);
  682. int first = d->hwirq - HV_PCI_MSI_SPI_START;
  683. int i;
  684. mutex_lock(&chip_data->map_lock);
  685. bitmap_release_region(chip_data->spi_map,
  686. first,
  687. get_count_order(nr_bm_irqs));
  688. mutex_unlock(&chip_data->map_lock);
  689. for (i = 0; i < nr_dom_irqs; i++) {
  690. if (i)
  691. d = irq_domain_get_irq_data(domain, virq + i);
  692. irq_domain_reset_irq_data(d);
  693. }
  694. irq_domain_free_irqs_parent(domain, virq, nr_dom_irqs);
  695. }
  696. static void hv_pci_vec_irq_domain_free(struct irq_domain *domain,
  697. unsigned int virq,
  698. unsigned int nr_irqs)
  699. {
  700. hv_pci_vec_irq_free(domain, virq, nr_irqs, nr_irqs);
  701. }
  702. static int hv_pci_vec_alloc_device_irq(struct irq_domain *domain,
  703. unsigned int nr_irqs,
  704. irq_hw_number_t *hwirq)
  705. {
  706. struct hv_pci_chip_data *chip_data = domain->host_data;
  707. int index;
  708. /* Find and allocate region from the SPI bitmap */
  709. mutex_lock(&chip_data->map_lock);
  710. index = bitmap_find_free_region(chip_data->spi_map,
  711. HV_PCI_MSI_SPI_NR,
  712. get_count_order(nr_irqs));
  713. mutex_unlock(&chip_data->map_lock);
  714. if (index < 0)
  715. return -ENOSPC;
  716. *hwirq = index + HV_PCI_MSI_SPI_START;
  717. return 0;
  718. }
  719. static int hv_pci_vec_irq_gic_domain_alloc(struct irq_domain *domain,
  720. unsigned int virq,
  721. irq_hw_number_t hwirq)
  722. {
  723. struct irq_fwspec fwspec;
  724. struct irq_data *d;
  725. int ret;
  726. fwspec.fwnode = domain->parent->fwnode;
  727. if (is_of_node(fwspec.fwnode)) {
  728. /* SPI lines for OF translations start at offset 32 */
  729. fwspec.param_count = 3;
  730. fwspec.param[0] = 0;
  731. fwspec.param[1] = hwirq - 32;
  732. fwspec.param[2] = IRQ_TYPE_EDGE_RISING;
  733. } else {
  734. fwspec.param_count = 2;
  735. fwspec.param[0] = hwirq;
  736. fwspec.param[1] = IRQ_TYPE_EDGE_RISING;
  737. }
  738. ret = irq_domain_alloc_irqs_parent(domain, virq, 1, &fwspec);
  739. if (ret)
  740. return ret;
  741. /*
  742. * Since the interrupt specifier is not coming from ACPI or DT, the
  743. * trigger type will need to be set explicitly. Otherwise, it will be
  744. * set to whatever is in the GIC configuration.
  745. */
  746. d = irq_domain_get_irq_data(domain->parent, virq);
  747. return d->chip->irq_set_type(d, IRQ_TYPE_EDGE_RISING);
  748. }
  749. static int hv_pci_vec_irq_domain_alloc(struct irq_domain *domain,
  750. unsigned int virq, unsigned int nr_irqs,
  751. void *args)
  752. {
  753. irq_hw_number_t hwirq;
  754. unsigned int i;
  755. int ret;
  756. ret = hv_pci_vec_alloc_device_irq(domain, nr_irqs, &hwirq);
  757. if (ret)
  758. return ret;
  759. for (i = 0; i < nr_irqs; i++) {
  760. ret = hv_pci_vec_irq_gic_domain_alloc(domain, virq + i,
  761. hwirq + i);
  762. if (ret) {
  763. hv_pci_vec_irq_free(domain, virq, nr_irqs, i);
  764. return ret;
  765. }
  766. irq_domain_set_hwirq_and_chip(domain, virq + i,
  767. hwirq + i,
  768. &hv_arm64_msi_irq_chip,
  769. domain->host_data);
  770. pr_debug("pID:%d vID:%u\n", (int)(hwirq + i), virq + i);
  771. }
  772. return 0;
  773. }
  774. /*
  775. * Pick the first cpu as the irq affinity that can be temporarily used for
  776. * composing MSI from the hypervisor. GIC will eventually set the right
  777. * affinity for the irq and the 'unmask' will retarget the interrupt to that
  778. * cpu.
  779. */
  780. static int hv_pci_vec_irq_domain_activate(struct irq_domain *domain,
  781. struct irq_data *irqd, bool reserve)
  782. {
  783. int cpu = cpumask_first(cpu_present_mask);
  784. irq_data_update_effective_affinity(irqd, cpumask_of(cpu));
  785. return 0;
  786. }
  787. static const struct irq_domain_ops hv_pci_domain_ops = {
  788. .alloc = hv_pci_vec_irq_domain_alloc,
  789. .free = hv_pci_vec_irq_domain_free,
  790. .activate = hv_pci_vec_irq_domain_activate,
  791. };
  792. #ifdef CONFIG_OF
  793. static struct irq_domain *hv_pci_of_irq_domain_parent(void)
  794. {
  795. struct device_node *parent;
  796. struct irq_domain *domain;
  797. parent = of_irq_find_parent(hv_get_vmbus_root_device()->of_node);
  798. if (!parent)
  799. return NULL;
  800. domain = irq_find_host(parent);
  801. of_node_put(parent);
  802. return domain;
  803. }
  804. #endif
  805. #ifdef CONFIG_ACPI
  806. static struct irq_domain *hv_pci_acpi_irq_domain_parent(void)
  807. {
  808. acpi_gsi_domain_disp_fn gsi_domain_disp_fn;
  809. gsi_domain_disp_fn = acpi_get_gsi_dispatcher();
  810. if (!gsi_domain_disp_fn)
  811. return NULL;
  812. return irq_find_matching_fwnode(gsi_domain_disp_fn(0),
  813. DOMAIN_BUS_ANY);
  814. }
  815. #endif
  816. static int hv_pci_irqchip_init(void)
  817. {
  818. static struct hv_pci_chip_data *chip_data;
  819. struct fwnode_handle *fn = NULL;
  820. struct irq_domain *irq_domain_parent = NULL;
  821. int ret = -ENOMEM;
  822. chip_data = kzalloc_obj(*chip_data);
  823. if (!chip_data)
  824. return ret;
  825. mutex_init(&chip_data->map_lock);
  826. fn = irq_domain_alloc_named_fwnode("hv_vpci_arm64");
  827. if (!fn)
  828. goto free_chip;
  829. /*
  830. * IRQ domain once enabled, should not be removed since there is no
  831. * way to ensure that all the corresponding devices are also gone and
  832. * no interrupts will be generated.
  833. */
  834. #ifdef CONFIG_ACPI
  835. if (!acpi_disabled)
  836. irq_domain_parent = hv_pci_acpi_irq_domain_parent();
  837. #endif
  838. #ifdef CONFIG_OF
  839. if (!irq_domain_parent)
  840. irq_domain_parent = hv_pci_of_irq_domain_parent();
  841. #endif
  842. if (!irq_domain_parent) {
  843. WARN_ONCE(1, "Invalid firmware configuration for VMBus interrupts\n");
  844. ret = -EINVAL;
  845. goto free_chip;
  846. }
  847. hv_msi_gic_irq_domain = irq_domain_create_hierarchy(irq_domain_parent, 0,
  848. HV_PCI_MSI_SPI_NR,
  849. fn, &hv_pci_domain_ops,
  850. chip_data);
  851. if (!hv_msi_gic_irq_domain) {
  852. pr_err("Failed to create Hyper-V arm64 vPCI MSI IRQ domain\n");
  853. goto free_chip;
  854. }
  855. return 0;
  856. free_chip:
  857. kfree(chip_data);
  858. if (fn)
  859. irq_domain_free_fwnode(fn);
  860. return ret;
  861. }
  862. static struct irq_domain *hv_pci_get_root_domain(void)
  863. {
  864. return hv_msi_gic_irq_domain;
  865. }
  866. /*
  867. * SPIs are used for interrupts of PCI devices and SPIs is managed via GICD
  868. * registers which Hyper-V already supports, so no hypercall needed.
  869. */
  870. static void hv_arch_irq_unmask(struct irq_data *data) { }
  871. #endif /* CONFIG_ARM64 */
  872. /**
  873. * hv_pci_generic_compl() - Invoked for a completion packet
  874. * @context: Set up by the sender of the packet.
  875. * @resp: The response packet
  876. * @resp_packet_size: Size in bytes of the packet
  877. *
  878. * This function is used to trigger an event and report status
  879. * for any message for which the completion packet contains a
  880. * status and nothing else.
  881. */
  882. static void hv_pci_generic_compl(void *context, struct pci_response *resp,
  883. int resp_packet_size)
  884. {
  885. struct hv_pci_compl *comp_pkt = context;
  886. comp_pkt->completion_status = resp->status;
  887. complete(&comp_pkt->host_event);
  888. }
  889. static struct hv_pci_dev *get_pcichild_wslot(struct hv_pcibus_device *hbus,
  890. u32 wslot);
  891. static void get_pcichild(struct hv_pci_dev *hpdev)
  892. {
  893. refcount_inc(&hpdev->refs);
  894. }
  895. static void put_pcichild(struct hv_pci_dev *hpdev)
  896. {
  897. if (refcount_dec_and_test(&hpdev->refs))
  898. kfree(hpdev);
  899. }
  900. /*
  901. * There is no good way to get notified from vmbus_onoffer_rescind(),
  902. * so let's use polling here, since this is not a hot path.
  903. */
  904. static int wait_for_response(struct hv_device *hdev,
  905. struct completion *comp)
  906. {
  907. while (true) {
  908. if (hdev->channel->rescind) {
  909. dev_warn_once(&hdev->device, "The device is gone.\n");
  910. return -ENODEV;
  911. }
  912. if (wait_for_completion_timeout(comp, HZ / 10))
  913. break;
  914. }
  915. return 0;
  916. }
  917. /**
  918. * devfn_to_wslot() - Convert from Linux PCI slot to Windows
  919. * @devfn: The Linux representation of PCI slot
  920. *
  921. * Windows uses a slightly different representation of PCI slot.
  922. *
  923. * Return: The Windows representation
  924. */
  925. static u32 devfn_to_wslot(int devfn)
  926. {
  927. union win_slot_encoding wslot;
  928. wslot.slot = 0;
  929. wslot.bits.dev = PCI_SLOT(devfn);
  930. wslot.bits.func = PCI_FUNC(devfn);
  931. return wslot.slot;
  932. }
  933. /**
  934. * wslot_to_devfn() - Convert from Windows PCI slot to Linux
  935. * @wslot: The Windows representation of PCI slot
  936. *
  937. * Windows uses a slightly different representation of PCI slot.
  938. *
  939. * Return: The Linux representation
  940. */
  941. static int wslot_to_devfn(u32 wslot)
  942. {
  943. union win_slot_encoding slot_no;
  944. slot_no.slot = wslot;
  945. return PCI_DEVFN(slot_no.bits.dev, slot_no.bits.func);
  946. }
  947. static void hv_pci_read_mmio(struct device *dev, phys_addr_t gpa, int size, u32 *val)
  948. {
  949. struct hv_mmio_read_input *in;
  950. struct hv_mmio_read_output *out;
  951. u64 ret;
  952. /*
  953. * Must be called with interrupts disabled so it is safe
  954. * to use the per-cpu input argument page. Use it for
  955. * both input and output.
  956. */
  957. in = *this_cpu_ptr(hyperv_pcpu_input_arg);
  958. out = *this_cpu_ptr(hyperv_pcpu_input_arg) + sizeof(*in);
  959. in->gpa = gpa;
  960. in->size = size;
  961. ret = hv_do_hypercall(HVCALL_MMIO_READ, in, out);
  962. if (hv_result_success(ret)) {
  963. switch (size) {
  964. case 1:
  965. *val = *(u8 *)(out->data);
  966. break;
  967. case 2:
  968. *val = *(u16 *)(out->data);
  969. break;
  970. default:
  971. *val = *(u32 *)(out->data);
  972. break;
  973. }
  974. } else
  975. dev_err(dev, "MMIO read hypercall error %llx addr %llx size %d\n",
  976. ret, gpa, size);
  977. }
  978. static void hv_pci_write_mmio(struct device *dev, phys_addr_t gpa, int size, u32 val)
  979. {
  980. struct hv_mmio_write_input *in;
  981. u64 ret;
  982. /*
  983. * Must be called with interrupts disabled so it is safe
  984. * to use the per-cpu input argument memory.
  985. */
  986. in = *this_cpu_ptr(hyperv_pcpu_input_arg);
  987. in->gpa = gpa;
  988. in->size = size;
  989. switch (size) {
  990. case 1:
  991. *(u8 *)(in->data) = val;
  992. break;
  993. case 2:
  994. *(u16 *)(in->data) = val;
  995. break;
  996. default:
  997. *(u32 *)(in->data) = val;
  998. break;
  999. }
  1000. ret = hv_do_hypercall(HVCALL_MMIO_WRITE, in, NULL);
  1001. if (!hv_result_success(ret))
  1002. dev_err(dev, "MMIO write hypercall error %llx addr %llx size %d\n",
  1003. ret, gpa, size);
  1004. }
  1005. /*
  1006. * PCI Configuration Space for these root PCI buses is implemented as a pair
  1007. * of pages in memory-mapped I/O space. Writing to the first page chooses
  1008. * the PCI function being written or read. Once the first page has been
  1009. * written to, the following page maps in the entire configuration space of
  1010. * the function.
  1011. */
  1012. /**
  1013. * _hv_pcifront_read_config() - Internal PCI config read
  1014. * @hpdev: The PCI driver's representation of the device
  1015. * @where: Offset within config space
  1016. * @size: Size of the transfer
  1017. * @val: Pointer to the buffer receiving the data
  1018. */
  1019. static void _hv_pcifront_read_config(struct hv_pci_dev *hpdev, int where,
  1020. int size, u32 *val)
  1021. {
  1022. struct hv_pcibus_device *hbus = hpdev->hbus;
  1023. struct device *dev = &hbus->hdev->device;
  1024. int offset = where + CFG_PAGE_OFFSET;
  1025. unsigned long flags;
  1026. /*
  1027. * If the attempt is to read the IDs or the ROM BAR, simulate that.
  1028. */
  1029. if (where + size <= PCI_COMMAND) {
  1030. memcpy(val, ((u8 *)&hpdev->desc.v_id) + where, size);
  1031. } else if (where >= PCI_CLASS_REVISION && where + size <=
  1032. PCI_CACHE_LINE_SIZE) {
  1033. memcpy(val, ((u8 *)&hpdev->desc.rev) + where -
  1034. PCI_CLASS_REVISION, size);
  1035. } else if (where >= PCI_SUBSYSTEM_VENDOR_ID && where + size <=
  1036. PCI_ROM_ADDRESS) {
  1037. memcpy(val, (u8 *)&hpdev->desc.subsystem_id + where -
  1038. PCI_SUBSYSTEM_VENDOR_ID, size);
  1039. } else if (where >= PCI_ROM_ADDRESS && where + size <=
  1040. PCI_CAPABILITY_LIST) {
  1041. /* ROM BARs are unimplemented */
  1042. *val = 0;
  1043. } else if ((where >= PCI_INTERRUPT_LINE && where + size <= PCI_INTERRUPT_PIN) ||
  1044. (where >= PCI_INTERRUPT_PIN && where + size <= PCI_MIN_GNT)) {
  1045. /*
  1046. * Interrupt Line and Interrupt PIN are hard-wired to zero
  1047. * because this front-end only supports message-signaled
  1048. * interrupts.
  1049. */
  1050. *val = 0;
  1051. } else if (where + size <= CFG_PAGE_SIZE) {
  1052. spin_lock_irqsave(&hbus->config_lock, flags);
  1053. if (hbus->use_calls) {
  1054. phys_addr_t addr = hbus->mem_config->start + offset;
  1055. hv_pci_write_mmio(dev, hbus->mem_config->start, 4,
  1056. hpdev->desc.win_slot.slot);
  1057. hv_pci_read_mmio(dev, addr, size, val);
  1058. } else {
  1059. void __iomem *addr = hbus->cfg_addr + offset;
  1060. /* Choose the function to be read. (See comment above) */
  1061. writel(hpdev->desc.win_slot.slot, hbus->cfg_addr);
  1062. /* Make sure the function was chosen before reading. */
  1063. mb();
  1064. /* Read from that function's config space. */
  1065. switch (size) {
  1066. case 1:
  1067. *val = readb(addr);
  1068. break;
  1069. case 2:
  1070. *val = readw(addr);
  1071. break;
  1072. default:
  1073. *val = readl(addr);
  1074. break;
  1075. }
  1076. /*
  1077. * Make sure the read was done before we release the
  1078. * spinlock allowing consecutive reads/writes.
  1079. */
  1080. mb();
  1081. }
  1082. spin_unlock_irqrestore(&hbus->config_lock, flags);
  1083. } else {
  1084. dev_err(dev, "Attempt to read beyond a function's config space.\n");
  1085. }
  1086. }
  1087. static u16 hv_pcifront_get_vendor_id(struct hv_pci_dev *hpdev)
  1088. {
  1089. struct hv_pcibus_device *hbus = hpdev->hbus;
  1090. struct device *dev = &hbus->hdev->device;
  1091. u32 val;
  1092. u16 ret;
  1093. unsigned long flags;
  1094. spin_lock_irqsave(&hbus->config_lock, flags);
  1095. if (hbus->use_calls) {
  1096. phys_addr_t addr = hbus->mem_config->start +
  1097. CFG_PAGE_OFFSET + PCI_VENDOR_ID;
  1098. hv_pci_write_mmio(dev, hbus->mem_config->start, 4,
  1099. hpdev->desc.win_slot.slot);
  1100. hv_pci_read_mmio(dev, addr, 2, &val);
  1101. ret = val; /* Truncates to 16 bits */
  1102. } else {
  1103. void __iomem *addr = hbus->cfg_addr + CFG_PAGE_OFFSET +
  1104. PCI_VENDOR_ID;
  1105. /* Choose the function to be read. (See comment above) */
  1106. writel(hpdev->desc.win_slot.slot, hbus->cfg_addr);
  1107. /* Make sure the function was chosen before we start reading. */
  1108. mb();
  1109. /* Read from that function's config space. */
  1110. ret = readw(addr);
  1111. /*
  1112. * mb() is not required here, because the
  1113. * spin_unlock_irqrestore() is a barrier.
  1114. */
  1115. }
  1116. spin_unlock_irqrestore(&hbus->config_lock, flags);
  1117. return ret;
  1118. }
  1119. /**
  1120. * _hv_pcifront_write_config() - Internal PCI config write
  1121. * @hpdev: The PCI driver's representation of the device
  1122. * @where: Offset within config space
  1123. * @size: Size of the transfer
  1124. * @val: The data being transferred
  1125. */
  1126. static void _hv_pcifront_write_config(struct hv_pci_dev *hpdev, int where,
  1127. int size, u32 val)
  1128. {
  1129. struct hv_pcibus_device *hbus = hpdev->hbus;
  1130. struct device *dev = &hbus->hdev->device;
  1131. int offset = where + CFG_PAGE_OFFSET;
  1132. unsigned long flags;
  1133. if (where >= PCI_SUBSYSTEM_VENDOR_ID &&
  1134. where + size <= PCI_CAPABILITY_LIST) {
  1135. /* SSIDs and ROM BARs are read-only */
  1136. } else if (where >= PCI_COMMAND && where + size <= CFG_PAGE_SIZE) {
  1137. spin_lock_irqsave(&hbus->config_lock, flags);
  1138. if (hbus->use_calls) {
  1139. phys_addr_t addr = hbus->mem_config->start + offset;
  1140. hv_pci_write_mmio(dev, hbus->mem_config->start, 4,
  1141. hpdev->desc.win_slot.slot);
  1142. hv_pci_write_mmio(dev, addr, size, val);
  1143. } else {
  1144. void __iomem *addr = hbus->cfg_addr + offset;
  1145. /* Choose the function to write. (See comment above) */
  1146. writel(hpdev->desc.win_slot.slot, hbus->cfg_addr);
  1147. /* Make sure the function was chosen before writing. */
  1148. wmb();
  1149. /* Write to that function's config space. */
  1150. switch (size) {
  1151. case 1:
  1152. writeb(val, addr);
  1153. break;
  1154. case 2:
  1155. writew(val, addr);
  1156. break;
  1157. default:
  1158. writel(val, addr);
  1159. break;
  1160. }
  1161. /*
  1162. * Make sure the write was done before we release the
  1163. * spinlock allowing consecutive reads/writes.
  1164. */
  1165. mb();
  1166. }
  1167. spin_unlock_irqrestore(&hbus->config_lock, flags);
  1168. } else {
  1169. dev_err(dev, "Attempt to write beyond a function's config space.\n");
  1170. }
  1171. }
  1172. /**
  1173. * hv_pcifront_read_config() - Read configuration space
  1174. * @bus: PCI Bus structure
  1175. * @devfn: Device/function
  1176. * @where: Offset from base
  1177. * @size: Byte/word/dword
  1178. * @val: Value to be read
  1179. *
  1180. * Return: PCIBIOS_SUCCESSFUL on success
  1181. * PCIBIOS_DEVICE_NOT_FOUND on failure
  1182. */
  1183. static int hv_pcifront_read_config(struct pci_bus *bus, unsigned int devfn,
  1184. int where, int size, u32 *val)
  1185. {
  1186. struct hv_pcibus_device *hbus =
  1187. container_of(bus->sysdata, struct hv_pcibus_device, sysdata);
  1188. struct hv_pci_dev *hpdev;
  1189. hpdev = get_pcichild_wslot(hbus, devfn_to_wslot(devfn));
  1190. if (!hpdev)
  1191. return PCIBIOS_DEVICE_NOT_FOUND;
  1192. _hv_pcifront_read_config(hpdev, where, size, val);
  1193. put_pcichild(hpdev);
  1194. return PCIBIOS_SUCCESSFUL;
  1195. }
  1196. /**
  1197. * hv_pcifront_write_config() - Write configuration space
  1198. * @bus: PCI Bus structure
  1199. * @devfn: Device/function
  1200. * @where: Offset from base
  1201. * @size: Byte/word/dword
  1202. * @val: Value to be written to device
  1203. *
  1204. * Return: PCIBIOS_SUCCESSFUL on success
  1205. * PCIBIOS_DEVICE_NOT_FOUND on failure
  1206. */
  1207. static int hv_pcifront_write_config(struct pci_bus *bus, unsigned int devfn,
  1208. int where, int size, u32 val)
  1209. {
  1210. struct hv_pcibus_device *hbus =
  1211. container_of(bus->sysdata, struct hv_pcibus_device, sysdata);
  1212. struct hv_pci_dev *hpdev;
  1213. hpdev = get_pcichild_wslot(hbus, devfn_to_wslot(devfn));
  1214. if (!hpdev)
  1215. return PCIBIOS_DEVICE_NOT_FOUND;
  1216. _hv_pcifront_write_config(hpdev, where, size, val);
  1217. put_pcichild(hpdev);
  1218. return PCIBIOS_SUCCESSFUL;
  1219. }
  1220. /* PCIe operations */
  1221. static struct pci_ops hv_pcifront_ops = {
  1222. .read = hv_pcifront_read_config,
  1223. .write = hv_pcifront_write_config,
  1224. };
  1225. /*
  1226. * Paravirtual backchannel
  1227. *
  1228. * Hyper-V SR-IOV provides a backchannel mechanism in software for
  1229. * communication between a VF driver and a PF driver. These
  1230. * "configuration blocks" are similar in concept to PCI configuration space,
  1231. * but instead of doing reads and writes in 32-bit chunks through a very slow
  1232. * path, packets of up to 128 bytes can be sent or received asynchronously.
  1233. *
  1234. * Nearly every SR-IOV device contains just such a communications channel in
  1235. * hardware, so using this one in software is usually optional. Using the
  1236. * software channel, however, allows driver implementers to leverage software
  1237. * tools that fuzz the communications channel looking for vulnerabilities.
  1238. *
  1239. * The usage model for these packets puts the responsibility for reading or
  1240. * writing on the VF driver. The VF driver sends a read or a write packet,
  1241. * indicating which "block" is being referred to by number.
  1242. *
  1243. * If the PF driver wishes to initiate communication, it can "invalidate" one or
  1244. * more of the first 64 blocks. This invalidation is delivered via a callback
  1245. * supplied to the VF driver by this driver.
  1246. *
  1247. * No protocol is implied, except that supplied by the PF and VF drivers.
  1248. */
  1249. struct hv_read_config_compl {
  1250. struct hv_pci_compl comp_pkt;
  1251. void *buf;
  1252. unsigned int len;
  1253. unsigned int bytes_returned;
  1254. };
  1255. /**
  1256. * hv_pci_read_config_compl() - Invoked when a response packet
  1257. * for a read config block operation arrives.
  1258. * @context: Identifies the read config operation
  1259. * @resp: The response packet itself
  1260. * @resp_packet_size: Size in bytes of the response packet
  1261. */
  1262. static void hv_pci_read_config_compl(void *context, struct pci_response *resp,
  1263. int resp_packet_size)
  1264. {
  1265. struct hv_read_config_compl *comp = context;
  1266. struct pci_read_block_response *read_resp =
  1267. (struct pci_read_block_response *)resp;
  1268. unsigned int data_len, hdr_len;
  1269. hdr_len = offsetof(struct pci_read_block_response, bytes);
  1270. if (resp_packet_size < hdr_len) {
  1271. comp->comp_pkt.completion_status = -1;
  1272. goto out;
  1273. }
  1274. data_len = resp_packet_size - hdr_len;
  1275. if (data_len > 0 && read_resp->status == 0) {
  1276. comp->bytes_returned = min(comp->len, data_len);
  1277. memcpy(comp->buf, read_resp->bytes, comp->bytes_returned);
  1278. } else {
  1279. comp->bytes_returned = 0;
  1280. }
  1281. comp->comp_pkt.completion_status = read_resp->status;
  1282. out:
  1283. complete(&comp->comp_pkt.host_event);
  1284. }
  1285. /**
  1286. * hv_read_config_block() - Sends a read config block request to
  1287. * the back-end driver running in the Hyper-V parent partition.
  1288. * @pdev: The PCI driver's representation for this device.
  1289. * @buf: Buffer into which the config block will be copied.
  1290. * @len: Size in bytes of buf.
  1291. * @block_id: Identifies the config block which has been requested.
  1292. * @bytes_returned: Size which came back from the back-end driver.
  1293. *
  1294. * Return: 0 on success, -errno on failure
  1295. */
  1296. static int hv_read_config_block(struct pci_dev *pdev, void *buf,
  1297. unsigned int len, unsigned int block_id,
  1298. unsigned int *bytes_returned)
  1299. {
  1300. struct hv_pcibus_device *hbus =
  1301. container_of(pdev->bus->sysdata, struct hv_pcibus_device,
  1302. sysdata);
  1303. struct {
  1304. struct pci_packet pkt;
  1305. char buf[sizeof(struct pci_read_block)];
  1306. } pkt;
  1307. struct hv_read_config_compl comp_pkt;
  1308. struct pci_read_block *read_blk;
  1309. int ret;
  1310. if (len == 0 || len > HV_CONFIG_BLOCK_SIZE_MAX)
  1311. return -EINVAL;
  1312. init_completion(&comp_pkt.comp_pkt.host_event);
  1313. comp_pkt.buf = buf;
  1314. comp_pkt.len = len;
  1315. memset(&pkt, 0, sizeof(pkt));
  1316. pkt.pkt.completion_func = hv_pci_read_config_compl;
  1317. pkt.pkt.compl_ctxt = &comp_pkt;
  1318. read_blk = (struct pci_read_block *)pkt.buf;
  1319. read_blk->message_type.type = PCI_READ_BLOCK;
  1320. read_blk->wslot.slot = devfn_to_wslot(pdev->devfn);
  1321. read_blk->block_id = block_id;
  1322. read_blk->bytes_requested = len;
  1323. ret = vmbus_sendpacket(hbus->hdev->channel, read_blk,
  1324. sizeof(*read_blk), (unsigned long)&pkt.pkt,
  1325. VM_PKT_DATA_INBAND,
  1326. VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
  1327. if (ret)
  1328. return ret;
  1329. ret = wait_for_response(hbus->hdev, &comp_pkt.comp_pkt.host_event);
  1330. if (ret)
  1331. return ret;
  1332. if (comp_pkt.comp_pkt.completion_status != 0 ||
  1333. comp_pkt.bytes_returned == 0) {
  1334. dev_err(&hbus->hdev->device,
  1335. "Read Config Block failed: 0x%x, bytes_returned=%d\n",
  1336. comp_pkt.comp_pkt.completion_status,
  1337. comp_pkt.bytes_returned);
  1338. return -EIO;
  1339. }
  1340. *bytes_returned = comp_pkt.bytes_returned;
  1341. return 0;
  1342. }
  1343. /**
  1344. * hv_pci_write_config_compl() - Invoked when a response packet for a write
  1345. * config block operation arrives.
  1346. * @context: Identifies the write config operation
  1347. * @resp: The response packet itself
  1348. * @resp_packet_size: Size in bytes of the response packet
  1349. */
  1350. static void hv_pci_write_config_compl(void *context, struct pci_response *resp,
  1351. int resp_packet_size)
  1352. {
  1353. struct hv_pci_compl *comp_pkt = context;
  1354. comp_pkt->completion_status = resp->status;
  1355. complete(&comp_pkt->host_event);
  1356. }
  1357. /**
  1358. * hv_write_config_block() - Sends a write config block request to the
  1359. * back-end driver running in the Hyper-V parent partition.
  1360. * @pdev: The PCI driver's representation for this device.
  1361. * @buf: Buffer from which the config block will be copied.
  1362. * @len: Size in bytes of buf.
  1363. * @block_id: Identifies the config block which is being written.
  1364. *
  1365. * Return: 0 on success, -errno on failure
  1366. */
  1367. static int hv_write_config_block(struct pci_dev *pdev, void *buf,
  1368. unsigned int len, unsigned int block_id)
  1369. {
  1370. struct hv_pcibus_device *hbus =
  1371. container_of(pdev->bus->sysdata, struct hv_pcibus_device,
  1372. sysdata);
  1373. struct {
  1374. struct pci_packet pkt;
  1375. char buf[sizeof(struct pci_write_block)];
  1376. u32 reserved;
  1377. } pkt;
  1378. struct hv_pci_compl comp_pkt;
  1379. struct pci_write_block *write_blk;
  1380. u32 pkt_size;
  1381. int ret;
  1382. if (len == 0 || len > HV_CONFIG_BLOCK_SIZE_MAX)
  1383. return -EINVAL;
  1384. init_completion(&comp_pkt.host_event);
  1385. memset(&pkt, 0, sizeof(pkt));
  1386. pkt.pkt.completion_func = hv_pci_write_config_compl;
  1387. pkt.pkt.compl_ctxt = &comp_pkt;
  1388. write_blk = (struct pci_write_block *)pkt.buf;
  1389. write_blk->message_type.type = PCI_WRITE_BLOCK;
  1390. write_blk->wslot.slot = devfn_to_wslot(pdev->devfn);
  1391. write_blk->block_id = block_id;
  1392. write_blk->byte_count = len;
  1393. memcpy(write_blk->bytes, buf, len);
  1394. pkt_size = offsetof(struct pci_write_block, bytes) + len;
  1395. /*
  1396. * This quirk is required on some hosts shipped around 2018, because
  1397. * these hosts don't check the pkt_size correctly (new hosts have been
  1398. * fixed since early 2019). The quirk is also safe on very old hosts
  1399. * and new hosts, because, on them, what really matters is the length
  1400. * specified in write_blk->byte_count.
  1401. */
  1402. pkt_size += sizeof(pkt.reserved);
  1403. ret = vmbus_sendpacket(hbus->hdev->channel, write_blk, pkt_size,
  1404. (unsigned long)&pkt.pkt, VM_PKT_DATA_INBAND,
  1405. VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
  1406. if (ret)
  1407. return ret;
  1408. ret = wait_for_response(hbus->hdev, &comp_pkt.host_event);
  1409. if (ret)
  1410. return ret;
  1411. if (comp_pkt.completion_status != 0) {
  1412. dev_err(&hbus->hdev->device,
  1413. "Write Config Block failed: 0x%x\n",
  1414. comp_pkt.completion_status);
  1415. return -EIO;
  1416. }
  1417. return 0;
  1418. }
  1419. /**
  1420. * hv_register_block_invalidate() - Invoked when a config block invalidation
  1421. * arrives from the back-end driver.
  1422. * @pdev: The PCI driver's representation for this device.
  1423. * @context: Identifies the device.
  1424. * @block_invalidate: Identifies all of the blocks being invalidated.
  1425. *
  1426. * Return: 0 on success, -errno on failure
  1427. */
  1428. static int hv_register_block_invalidate(struct pci_dev *pdev, void *context,
  1429. void (*block_invalidate)(void *context,
  1430. u64 block_mask))
  1431. {
  1432. struct hv_pcibus_device *hbus =
  1433. container_of(pdev->bus->sysdata, struct hv_pcibus_device,
  1434. sysdata);
  1435. struct hv_pci_dev *hpdev;
  1436. hpdev = get_pcichild_wslot(hbus, devfn_to_wslot(pdev->devfn));
  1437. if (!hpdev)
  1438. return -ENODEV;
  1439. hpdev->block_invalidate = block_invalidate;
  1440. hpdev->invalidate_context = context;
  1441. put_pcichild(hpdev);
  1442. return 0;
  1443. }
  1444. /* Interrupt management hooks */
  1445. static void hv_int_desc_free(struct hv_pci_dev *hpdev,
  1446. struct tran_int_desc *int_desc)
  1447. {
  1448. struct pci_delete_interrupt *int_pkt;
  1449. struct {
  1450. struct pci_packet pkt;
  1451. u8 buffer[sizeof(struct pci_delete_interrupt)];
  1452. } ctxt;
  1453. if (!int_desc->vector_count) {
  1454. kfree(int_desc);
  1455. return;
  1456. }
  1457. memset(&ctxt, 0, sizeof(ctxt));
  1458. int_pkt = (struct pci_delete_interrupt *)ctxt.buffer;
  1459. int_pkt->message_type.type =
  1460. PCI_DELETE_INTERRUPT_MESSAGE;
  1461. int_pkt->wslot.slot = hpdev->desc.win_slot.slot;
  1462. int_pkt->int_desc = *int_desc;
  1463. vmbus_sendpacket(hpdev->hbus->hdev->channel, int_pkt, sizeof(*int_pkt),
  1464. 0, VM_PKT_DATA_INBAND, 0);
  1465. kfree(int_desc);
  1466. }
  1467. /**
  1468. * hv_msi_free() - Free the MSI.
  1469. * @domain: The interrupt domain pointer
  1470. * @irq: Identifies the IRQ.
  1471. *
  1472. * The Hyper-V parent partition and hypervisor are tracking the
  1473. * messages that are in use, keeping the interrupt redirection
  1474. * table up to date. This callback sends a message that frees
  1475. * the IRT entry and related tracking nonsense.
  1476. */
  1477. static void hv_msi_free(struct irq_domain *domain, unsigned int irq)
  1478. {
  1479. struct hv_pcibus_device *hbus;
  1480. struct hv_pci_dev *hpdev;
  1481. struct pci_dev *pdev;
  1482. struct tran_int_desc *int_desc;
  1483. struct irq_data *irq_data = irq_domain_get_irq_data(domain, irq);
  1484. struct msi_desc *msi = irq_data_get_msi_desc(irq_data);
  1485. pdev = msi_desc_to_pci_dev(msi);
  1486. hbus = domain->host_data;
  1487. int_desc = irq_data_get_irq_chip_data(irq_data);
  1488. if (!int_desc)
  1489. return;
  1490. irq_data->chip_data = NULL;
  1491. hpdev = get_pcichild_wslot(hbus, devfn_to_wslot(pdev->devfn));
  1492. if (!hpdev) {
  1493. kfree(int_desc);
  1494. return;
  1495. }
  1496. hv_int_desc_free(hpdev, int_desc);
  1497. put_pcichild(hpdev);
  1498. }
  1499. static void hv_irq_mask(struct irq_data *data)
  1500. {
  1501. if (data->parent_data->chip->irq_mask)
  1502. irq_chip_mask_parent(data);
  1503. }
  1504. static void hv_irq_unmask(struct irq_data *data)
  1505. {
  1506. hv_arch_irq_unmask(data);
  1507. if (data->parent_data->chip->irq_unmask)
  1508. irq_chip_unmask_parent(data);
  1509. }
  1510. struct compose_comp_ctxt {
  1511. struct hv_pci_compl comp_pkt;
  1512. struct tran_int_desc int_desc;
  1513. };
  1514. static void hv_pci_compose_compl(void *context, struct pci_response *resp,
  1515. int resp_packet_size)
  1516. {
  1517. struct compose_comp_ctxt *comp_pkt = context;
  1518. struct pci_create_int_response *int_resp =
  1519. (struct pci_create_int_response *)resp;
  1520. if (resp_packet_size < sizeof(*int_resp)) {
  1521. comp_pkt->comp_pkt.completion_status = -1;
  1522. goto out;
  1523. }
  1524. comp_pkt->comp_pkt.completion_status = resp->status;
  1525. comp_pkt->int_desc = int_resp->int_desc;
  1526. out:
  1527. complete(&comp_pkt->comp_pkt.host_event);
  1528. }
  1529. static u32 hv_compose_msi_req_v1(
  1530. struct pci_create_interrupt *int_pkt,
  1531. u32 slot, u8 vector, u16 vector_count)
  1532. {
  1533. int_pkt->message_type.type = PCI_CREATE_INTERRUPT_MESSAGE;
  1534. int_pkt->wslot.slot = slot;
  1535. int_pkt->int_desc.vector = vector;
  1536. int_pkt->int_desc.vector_count = vector_count;
  1537. int_pkt->int_desc.delivery_mode = DELIVERY_MODE;
  1538. /*
  1539. * Create MSI w/ dummy vCPU set, overwritten by subsequent retarget in
  1540. * hv_irq_unmask().
  1541. */
  1542. int_pkt->int_desc.cpu_mask = CPU_AFFINITY_ALL;
  1543. return sizeof(*int_pkt);
  1544. }
  1545. /*
  1546. * The vCPU selected by hv_compose_multi_msi_req_get_cpu() and
  1547. * hv_compose_msi_req_get_cpu() is a "dummy" vCPU because the final vCPU to be
  1548. * interrupted is specified later in hv_irq_unmask() and communicated to Hyper-V
  1549. * via the HVCALL_RETARGET_INTERRUPT hypercall. But the choice of dummy vCPU is
  1550. * not irrelevant because Hyper-V chooses the physical CPU to handle the
  1551. * interrupts based on the vCPU specified in message sent to the vPCI VSP in
  1552. * hv_compose_msi_msg(). Hyper-V's choice of pCPU is not visible to the guest,
  1553. * but assigning too many vPCI device interrupts to the same pCPU can cause a
  1554. * performance bottleneck. So we spread out the dummy vCPUs to influence Hyper-V
  1555. * to spread out the pCPUs that it selects.
  1556. *
  1557. * For the single-MSI and MSI-X cases, it's OK for hv_compose_msi_req_get_cpu()
  1558. * to always return the same dummy vCPU, because a second call to
  1559. * hv_compose_msi_msg() contains the "real" vCPU, causing Hyper-V to choose a
  1560. * new pCPU for the interrupt. But for the multi-MSI case, the second call to
  1561. * hv_compose_msi_msg() exits without sending a message to the vPCI VSP, so the
  1562. * original dummy vCPU is used. This dummy vCPU must be round-robin'ed so that
  1563. * the pCPUs are spread out. All interrupts for a multi-MSI device end up using
  1564. * the same pCPU, even though the vCPUs will be spread out by later calls
  1565. * to hv_irq_unmask(), but that is the best we can do now.
  1566. *
  1567. * With Hyper-V in Nov 2022, the HVCALL_RETARGET_INTERRUPT hypercall does *not*
  1568. * cause Hyper-V to reselect the pCPU based on the specified vCPU. Such an
  1569. * enhancement is planned for a future version. With that enhancement, the
  1570. * dummy vCPU selection won't matter, and interrupts for the same multi-MSI
  1571. * device will be spread across multiple pCPUs.
  1572. */
  1573. /*
  1574. * Create MSI w/ dummy vCPU set targeting just one vCPU, overwritten
  1575. * by subsequent retarget in hv_irq_unmask().
  1576. */
  1577. static int hv_compose_msi_req_get_cpu(const struct cpumask *affinity)
  1578. {
  1579. return cpumask_first_and(affinity, cpu_online_mask);
  1580. }
  1581. /*
  1582. * Make sure the dummy vCPU values for multi-MSI don't all point to vCPU0.
  1583. */
  1584. static int hv_compose_multi_msi_req_get_cpu(void)
  1585. {
  1586. static DEFINE_SPINLOCK(multi_msi_cpu_lock);
  1587. /* -1 means starting with CPU 0 */
  1588. static int cpu_next = -1;
  1589. unsigned long flags;
  1590. int cpu;
  1591. spin_lock_irqsave(&multi_msi_cpu_lock, flags);
  1592. cpu_next = cpumask_next_wrap(cpu_next, cpu_online_mask);
  1593. cpu = cpu_next;
  1594. spin_unlock_irqrestore(&multi_msi_cpu_lock, flags);
  1595. return cpu;
  1596. }
  1597. static u32 hv_compose_msi_req_v2(
  1598. struct pci_create_interrupt2 *int_pkt, int cpu,
  1599. u32 slot, u8 vector, u16 vector_count)
  1600. {
  1601. int_pkt->message_type.type = PCI_CREATE_INTERRUPT_MESSAGE2;
  1602. int_pkt->wslot.slot = slot;
  1603. int_pkt->int_desc.vector = vector;
  1604. int_pkt->int_desc.vector_count = vector_count;
  1605. int_pkt->int_desc.delivery_mode = DELIVERY_MODE;
  1606. int_pkt->int_desc.processor_array[0] =
  1607. hv_cpu_number_to_vp_number(cpu);
  1608. int_pkt->int_desc.processor_count = 1;
  1609. return sizeof(*int_pkt);
  1610. }
  1611. static u32 hv_compose_msi_req_v3(
  1612. struct pci_create_interrupt3 *int_pkt, int cpu,
  1613. u32 slot, u32 vector, u16 vector_count)
  1614. {
  1615. int_pkt->message_type.type = PCI_CREATE_INTERRUPT_MESSAGE3;
  1616. int_pkt->wslot.slot = slot;
  1617. int_pkt->int_desc.vector = vector;
  1618. int_pkt->int_desc.reserved = 0;
  1619. int_pkt->int_desc.vector_count = vector_count;
  1620. int_pkt->int_desc.delivery_mode = DELIVERY_MODE;
  1621. int_pkt->int_desc.processor_array[0] =
  1622. hv_cpu_number_to_vp_number(cpu);
  1623. int_pkt->int_desc.processor_count = 1;
  1624. return sizeof(*int_pkt);
  1625. }
  1626. /**
  1627. * hv_compose_msi_msg() - Supplies a valid MSI address/data
  1628. * @data: Everything about this MSI
  1629. * @msg: Buffer that is filled in by this function
  1630. *
  1631. * This function unpacks the IRQ looking for target CPU set, IDT
  1632. * vector and mode and sends a message to the parent partition
  1633. * asking for a mapping for that tuple in this partition. The
  1634. * response supplies a data value and address to which that data
  1635. * should be written to trigger that interrupt.
  1636. */
  1637. static void hv_compose_msi_msg(struct irq_data *data, struct msi_msg *msg)
  1638. {
  1639. struct hv_pcibus_device *hbus;
  1640. struct vmbus_channel *channel;
  1641. struct hv_pci_dev *hpdev;
  1642. struct pci_bus *pbus;
  1643. struct pci_dev *pdev;
  1644. const struct cpumask *dest;
  1645. struct compose_comp_ctxt comp;
  1646. struct tran_int_desc *int_desc;
  1647. struct msi_desc *msi_desc;
  1648. /*
  1649. * vector_count should be u16: see hv_msi_desc, hv_msi_desc2
  1650. * and hv_msi_desc3. vector must be u32: see hv_msi_desc3.
  1651. */
  1652. u16 vector_count;
  1653. u32 vector;
  1654. struct {
  1655. struct pci_packet pci_pkt;
  1656. union {
  1657. struct pci_create_interrupt v1;
  1658. struct pci_create_interrupt2 v2;
  1659. struct pci_create_interrupt3 v3;
  1660. } int_pkts;
  1661. } __packed ctxt;
  1662. bool multi_msi;
  1663. u64 trans_id;
  1664. u32 size;
  1665. int ret;
  1666. int cpu;
  1667. msi_desc = irq_data_get_msi_desc(data);
  1668. multi_msi = !msi_desc->pci.msi_attrib.is_msix &&
  1669. msi_desc->nvec_used > 1;
  1670. /* Reuse the previous allocation */
  1671. if (data->chip_data && multi_msi) {
  1672. int_desc = data->chip_data;
  1673. msg->address_hi = int_desc->address >> 32;
  1674. msg->address_lo = int_desc->address & 0xffffffff;
  1675. msg->data = int_desc->data;
  1676. return;
  1677. }
  1678. pdev = msi_desc_to_pci_dev(msi_desc);
  1679. dest = irq_data_get_effective_affinity_mask(data);
  1680. pbus = pdev->bus;
  1681. hbus = container_of(pbus->sysdata, struct hv_pcibus_device, sysdata);
  1682. channel = hbus->hdev->channel;
  1683. hpdev = get_pcichild_wslot(hbus, devfn_to_wslot(pdev->devfn));
  1684. if (!hpdev)
  1685. goto return_null_message;
  1686. /* Free any previous message that might have already been composed. */
  1687. if (data->chip_data && !multi_msi) {
  1688. int_desc = data->chip_data;
  1689. data->chip_data = NULL;
  1690. hv_int_desc_free(hpdev, int_desc);
  1691. }
  1692. int_desc = kzalloc_obj(*int_desc, GFP_ATOMIC);
  1693. if (!int_desc)
  1694. goto drop_reference;
  1695. if (multi_msi) {
  1696. /*
  1697. * If this is not the first MSI of Multi MSI, we already have
  1698. * a mapping. Can exit early.
  1699. */
  1700. if (msi_desc->irq != data->irq) {
  1701. data->chip_data = int_desc;
  1702. int_desc->address = msi_desc->msg.address_lo |
  1703. (u64)msi_desc->msg.address_hi << 32;
  1704. int_desc->data = msi_desc->msg.data +
  1705. (data->irq - msi_desc->irq);
  1706. msg->address_hi = msi_desc->msg.address_hi;
  1707. msg->address_lo = msi_desc->msg.address_lo;
  1708. msg->data = int_desc->data;
  1709. put_pcichild(hpdev);
  1710. return;
  1711. }
  1712. /*
  1713. * The vector we select here is a dummy value. The correct
  1714. * value gets sent to the hypervisor in unmask(). This needs
  1715. * to be aligned with the count, and also not zero. Multi-msi
  1716. * is powers of 2 up to 32, so 32 will always work here.
  1717. */
  1718. vector = 32;
  1719. vector_count = msi_desc->nvec_used;
  1720. cpu = hv_compose_multi_msi_req_get_cpu();
  1721. } else {
  1722. vector = hv_msi_get_int_vector(data);
  1723. vector_count = 1;
  1724. cpu = hv_compose_msi_req_get_cpu(dest);
  1725. }
  1726. /*
  1727. * hv_compose_msi_req_v1 and v2 are for x86 only, meaning 'vector'
  1728. * can't exceed u8. Cast 'vector' down to u8 for v1/v2 explicitly
  1729. * for better readability.
  1730. */
  1731. memset(&ctxt, 0, sizeof(ctxt));
  1732. init_completion(&comp.comp_pkt.host_event);
  1733. ctxt.pci_pkt.completion_func = hv_pci_compose_compl;
  1734. ctxt.pci_pkt.compl_ctxt = &comp;
  1735. switch (hbus->protocol_version) {
  1736. case PCI_PROTOCOL_VERSION_1_1:
  1737. size = hv_compose_msi_req_v1(&ctxt.int_pkts.v1,
  1738. hpdev->desc.win_slot.slot,
  1739. (u8)vector,
  1740. vector_count);
  1741. break;
  1742. case PCI_PROTOCOL_VERSION_1_2:
  1743. case PCI_PROTOCOL_VERSION_1_3:
  1744. size = hv_compose_msi_req_v2(&ctxt.int_pkts.v2,
  1745. cpu,
  1746. hpdev->desc.win_slot.slot,
  1747. (u8)vector,
  1748. vector_count);
  1749. break;
  1750. case PCI_PROTOCOL_VERSION_1_4:
  1751. size = hv_compose_msi_req_v3(&ctxt.int_pkts.v3,
  1752. cpu,
  1753. hpdev->desc.win_slot.slot,
  1754. vector,
  1755. vector_count);
  1756. break;
  1757. default:
  1758. /* As we only negotiate protocol versions known to this driver,
  1759. * this path should never hit. However, this is it not a hot
  1760. * path so we print a message to aid future updates.
  1761. */
  1762. dev_err(&hbus->hdev->device,
  1763. "Unexpected vPCI protocol, update driver.");
  1764. goto free_int_desc;
  1765. }
  1766. ret = vmbus_sendpacket_getid(hpdev->hbus->hdev->channel, &ctxt.int_pkts,
  1767. size, (unsigned long)&ctxt.pci_pkt,
  1768. &trans_id, VM_PKT_DATA_INBAND,
  1769. VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
  1770. if (ret) {
  1771. dev_err(&hbus->hdev->device,
  1772. "Sending request for interrupt failed: 0x%x",
  1773. comp.comp_pkt.completion_status);
  1774. goto free_int_desc;
  1775. }
  1776. /*
  1777. * Prevents hv_pci_onchannelcallback() from running concurrently
  1778. * in the tasklet.
  1779. */
  1780. tasklet_disable_in_atomic(&channel->callback_event);
  1781. /*
  1782. * Since this function is called with IRQ locks held, can't
  1783. * do normal wait for completion; instead poll.
  1784. */
  1785. while (!try_wait_for_completion(&comp.comp_pkt.host_event)) {
  1786. unsigned long flags;
  1787. /* 0xFFFF means an invalid PCI VENDOR ID. */
  1788. if (hv_pcifront_get_vendor_id(hpdev) == 0xFFFF) {
  1789. dev_err_once(&hbus->hdev->device,
  1790. "the device has gone\n");
  1791. goto enable_tasklet;
  1792. }
  1793. /*
  1794. * Make sure that the ring buffer data structure doesn't get
  1795. * freed while we dereference the ring buffer pointer. Test
  1796. * for the channel's onchannel_callback being NULL within a
  1797. * sched_lock critical section. See also the inline comments
  1798. * in vmbus_reset_channel_cb().
  1799. */
  1800. spin_lock_irqsave(&channel->sched_lock, flags);
  1801. if (unlikely(channel->onchannel_callback == NULL)) {
  1802. spin_unlock_irqrestore(&channel->sched_lock, flags);
  1803. goto enable_tasklet;
  1804. }
  1805. hv_pci_onchannelcallback(hbus);
  1806. spin_unlock_irqrestore(&channel->sched_lock, flags);
  1807. udelay(100);
  1808. }
  1809. tasklet_enable(&channel->callback_event);
  1810. if (comp.comp_pkt.completion_status < 0) {
  1811. dev_err(&hbus->hdev->device,
  1812. "Request for interrupt failed: 0x%x",
  1813. comp.comp_pkt.completion_status);
  1814. goto free_int_desc;
  1815. }
  1816. /*
  1817. * Record the assignment so that this can be unwound later. Using
  1818. * irq_set_chip_data() here would be appropriate, but the lock it takes
  1819. * is already held.
  1820. */
  1821. *int_desc = comp.int_desc;
  1822. data->chip_data = int_desc;
  1823. /* Pass up the result. */
  1824. msg->address_hi = comp.int_desc.address >> 32;
  1825. msg->address_lo = comp.int_desc.address & 0xffffffff;
  1826. msg->data = comp.int_desc.data;
  1827. put_pcichild(hpdev);
  1828. return;
  1829. enable_tasklet:
  1830. tasklet_enable(&channel->callback_event);
  1831. /*
  1832. * The completion packet on the stack becomes invalid after 'return';
  1833. * remove the ID from the VMbus requestor if the identifier is still
  1834. * mapped to/associated with the packet. (The identifier could have
  1835. * been 're-used', i.e., already removed and (re-)mapped.)
  1836. *
  1837. * Cf. hv_pci_onchannelcallback().
  1838. */
  1839. vmbus_request_addr_match(channel, trans_id, (unsigned long)&ctxt.pci_pkt);
  1840. free_int_desc:
  1841. kfree(int_desc);
  1842. drop_reference:
  1843. put_pcichild(hpdev);
  1844. return_null_message:
  1845. msg->address_hi = 0;
  1846. msg->address_lo = 0;
  1847. msg->data = 0;
  1848. }
  1849. static bool hv_pcie_init_dev_msi_info(struct device *dev, struct irq_domain *domain,
  1850. struct irq_domain *real_parent, struct msi_domain_info *info)
  1851. {
  1852. struct irq_chip *chip = info->chip;
  1853. if (!msi_lib_init_dev_msi_info(dev, domain, real_parent, info))
  1854. return false;
  1855. info->ops->msi_prepare = hv_msi_prepare;
  1856. chip->irq_set_affinity = irq_chip_set_affinity_parent;
  1857. if (IS_ENABLED(CONFIG_X86))
  1858. chip->flags |= IRQCHIP_MOVE_DEFERRED;
  1859. return true;
  1860. }
  1861. #define HV_PCIE_MSI_FLAGS_REQUIRED (MSI_FLAG_USE_DEF_DOM_OPS | \
  1862. MSI_FLAG_USE_DEF_CHIP_OPS | \
  1863. MSI_FLAG_PCI_MSI_MASK_PARENT)
  1864. #define HV_PCIE_MSI_FLAGS_SUPPORTED (MSI_FLAG_MULTI_PCI_MSI | \
  1865. MSI_FLAG_PCI_MSIX | \
  1866. MSI_FLAG_PCI_MSIX_ALLOC_DYN | \
  1867. MSI_GENERIC_FLAGS_MASK)
  1868. static const struct msi_parent_ops hv_pcie_msi_parent_ops = {
  1869. .required_flags = HV_PCIE_MSI_FLAGS_REQUIRED,
  1870. .supported_flags = HV_PCIE_MSI_FLAGS_SUPPORTED,
  1871. .bus_select_token = DOMAIN_BUS_PCI_MSI,
  1872. .chip_flags = HV_MSI_CHIP_FLAGS,
  1873. .prefix = "HV-",
  1874. .init_dev_msi_info = hv_pcie_init_dev_msi_info,
  1875. };
  1876. /* HW Interrupt Chip Descriptor */
  1877. static struct irq_chip hv_msi_irq_chip = {
  1878. .name = "Hyper-V PCIe MSI",
  1879. .irq_compose_msi_msg = hv_compose_msi_msg,
  1880. .irq_set_affinity = irq_chip_set_affinity_parent,
  1881. .irq_ack = irq_chip_ack_parent,
  1882. .irq_eoi = irq_chip_eoi_parent,
  1883. .irq_mask = hv_irq_mask,
  1884. .irq_unmask = hv_irq_unmask,
  1885. };
  1886. static int hv_pcie_domain_alloc(struct irq_domain *d, unsigned int virq, unsigned int nr_irqs,
  1887. void *arg)
  1888. {
  1889. /*
  1890. * TODO: Allocating and populating struct tran_int_desc in hv_compose_msi_msg()
  1891. * should be moved here.
  1892. */
  1893. int ret;
  1894. ret = irq_domain_alloc_irqs_parent(d, virq, nr_irqs, arg);
  1895. if (ret < 0)
  1896. return ret;
  1897. for (int i = 0; i < nr_irqs; i++) {
  1898. irq_domain_set_hwirq_and_chip(d, virq + i, 0, &hv_msi_irq_chip, NULL);
  1899. if (IS_ENABLED(CONFIG_X86))
  1900. __irq_set_handler(virq + i, handle_edge_irq, 0, "edge");
  1901. }
  1902. return 0;
  1903. }
  1904. static void hv_pcie_domain_free(struct irq_domain *d, unsigned int virq, unsigned int nr_irqs)
  1905. {
  1906. for (int i = 0; i < nr_irqs; i++)
  1907. hv_msi_free(d, virq + i);
  1908. irq_domain_free_irqs_top(d, virq, nr_irqs);
  1909. }
  1910. static const struct irq_domain_ops hv_pcie_domain_ops = {
  1911. .alloc = hv_pcie_domain_alloc,
  1912. .free = hv_pcie_domain_free,
  1913. };
  1914. /**
  1915. * hv_pcie_init_irq_domain() - Initialize IRQ domain
  1916. * @hbus: The root PCI bus
  1917. *
  1918. * This function creates an IRQ domain which will be used for
  1919. * interrupts from devices that have been passed through. These
  1920. * devices only support MSI and MSI-X, not line-based interrupts
  1921. * or simulations of line-based interrupts through PCIe's
  1922. * fabric-layer messages. Because interrupts are remapped, we
  1923. * can support multi-message MSI here.
  1924. *
  1925. * Return: '0' on success and error value on failure
  1926. */
  1927. static int hv_pcie_init_irq_domain(struct hv_pcibus_device *hbus)
  1928. {
  1929. struct irq_domain_info info = {
  1930. .fwnode = hbus->fwnode,
  1931. .ops = &hv_pcie_domain_ops,
  1932. .host_data = hbus,
  1933. .parent = hv_pci_get_root_domain(),
  1934. };
  1935. hbus->irq_domain = msi_create_parent_irq_domain(&info, &hv_pcie_msi_parent_ops);
  1936. if (!hbus->irq_domain) {
  1937. dev_err(&hbus->hdev->device,
  1938. "Failed to build an MSI IRQ domain\n");
  1939. return -ENODEV;
  1940. }
  1941. dev_set_msi_domain(&hbus->bridge->dev, hbus->irq_domain);
  1942. return 0;
  1943. }
  1944. /**
  1945. * get_bar_size() - Get the address space consumed by a BAR
  1946. * @bar_val: Value that a BAR returned after -1 was written
  1947. * to it.
  1948. *
  1949. * This function returns the size of the BAR, rounded up to 1
  1950. * page. It has to be rounded up because the hypervisor's page
  1951. * table entry that maps the BAR into the VM can't specify an
  1952. * offset within a page. The invariant is that the hypervisor
  1953. * must place any BARs of smaller than page length at the
  1954. * beginning of a page.
  1955. *
  1956. * Return: Size in bytes of the consumed MMIO space.
  1957. */
  1958. static u64 get_bar_size(u64 bar_val)
  1959. {
  1960. return round_up((1 + ~(bar_val & PCI_BASE_ADDRESS_MEM_MASK)),
  1961. PAGE_SIZE);
  1962. }
  1963. /**
  1964. * survey_child_resources() - Total all MMIO requirements
  1965. * @hbus: Root PCI bus, as understood by this driver
  1966. */
  1967. static void survey_child_resources(struct hv_pcibus_device *hbus)
  1968. {
  1969. struct hv_pci_dev *hpdev;
  1970. resource_size_t bar_size = 0;
  1971. unsigned long flags;
  1972. struct completion *event;
  1973. u64 bar_val;
  1974. int i;
  1975. /* If nobody is waiting on the answer, don't compute it. */
  1976. event = xchg(&hbus->survey_event, NULL);
  1977. if (!event)
  1978. return;
  1979. /* If the answer has already been computed, go with it. */
  1980. if (hbus->low_mmio_space || hbus->high_mmio_space) {
  1981. complete(event);
  1982. return;
  1983. }
  1984. spin_lock_irqsave(&hbus->device_list_lock, flags);
  1985. /*
  1986. * Due to an interesting quirk of the PCI spec, all memory regions
  1987. * for a child device are a power of 2 in size and aligned in memory,
  1988. * so it's sufficient to just add them up without tracking alignment.
  1989. */
  1990. list_for_each_entry(hpdev, &hbus->children, list_entry) {
  1991. for (i = 0; i < PCI_STD_NUM_BARS; i++) {
  1992. if (hpdev->probed_bar[i] & PCI_BASE_ADDRESS_SPACE_IO)
  1993. dev_err(&hbus->hdev->device,
  1994. "There's an I/O BAR in this list!\n");
  1995. if (hpdev->probed_bar[i] != 0) {
  1996. /*
  1997. * A probed BAR has all the upper bits set that
  1998. * can be changed.
  1999. */
  2000. bar_val = hpdev->probed_bar[i];
  2001. if (bar_val & PCI_BASE_ADDRESS_MEM_TYPE_64)
  2002. bar_val |=
  2003. ((u64)hpdev->probed_bar[++i] << 32);
  2004. else
  2005. bar_val |= 0xffffffff00000000ULL;
  2006. bar_size = get_bar_size(bar_val);
  2007. if (bar_val & PCI_BASE_ADDRESS_MEM_TYPE_64)
  2008. hbus->high_mmio_space += bar_size;
  2009. else
  2010. hbus->low_mmio_space += bar_size;
  2011. }
  2012. }
  2013. }
  2014. spin_unlock_irqrestore(&hbus->device_list_lock, flags);
  2015. complete(event);
  2016. }
  2017. /**
  2018. * prepopulate_bars() - Fill in BARs with defaults
  2019. * @hbus: Root PCI bus, as understood by this driver
  2020. *
  2021. * The core PCI driver code seems much, much happier if the BARs
  2022. * for a device have values upon first scan. So fill them in.
  2023. * The algorithm below works down from large sizes to small,
  2024. * attempting to pack the assignments optimally. The assumption,
  2025. * enforced in other parts of the code, is that the beginning of
  2026. * the memory-mapped I/O space will be aligned on the largest
  2027. * BAR size.
  2028. */
  2029. static void prepopulate_bars(struct hv_pcibus_device *hbus)
  2030. {
  2031. resource_size_t high_size = 0;
  2032. resource_size_t low_size = 0;
  2033. resource_size_t high_base = 0;
  2034. resource_size_t low_base = 0;
  2035. resource_size_t bar_size;
  2036. struct hv_pci_dev *hpdev;
  2037. unsigned long flags;
  2038. u64 bar_val;
  2039. u32 command;
  2040. bool high;
  2041. int i;
  2042. if (hbus->low_mmio_space) {
  2043. low_size = 1ULL << (63 - __builtin_clzll(hbus->low_mmio_space));
  2044. low_base = hbus->low_mmio_res->start;
  2045. }
  2046. if (hbus->high_mmio_space) {
  2047. high_size = 1ULL <<
  2048. (63 - __builtin_clzll(hbus->high_mmio_space));
  2049. high_base = hbus->high_mmio_res->start;
  2050. }
  2051. spin_lock_irqsave(&hbus->device_list_lock, flags);
  2052. /*
  2053. * Clear the memory enable bit, in case it's already set. This occurs
  2054. * in the suspend path of hibernation, where the device is suspended,
  2055. * resumed and suspended again: see hibernation_snapshot() and
  2056. * hibernation_platform_enter().
  2057. *
  2058. * If the memory enable bit is already set, Hyper-V silently ignores
  2059. * the below BAR updates, and the related PCI device driver can not
  2060. * work, because reading from the device register(s) always returns
  2061. * 0xFFFFFFFF (PCI_ERROR_RESPONSE).
  2062. */
  2063. list_for_each_entry(hpdev, &hbus->children, list_entry) {
  2064. _hv_pcifront_read_config(hpdev, PCI_COMMAND, 2, &command);
  2065. command &= ~PCI_COMMAND_MEMORY;
  2066. _hv_pcifront_write_config(hpdev, PCI_COMMAND, 2, command);
  2067. }
  2068. /* Pick addresses for the BARs. */
  2069. do {
  2070. list_for_each_entry(hpdev, &hbus->children, list_entry) {
  2071. for (i = 0; i < PCI_STD_NUM_BARS; i++) {
  2072. bar_val = hpdev->probed_bar[i];
  2073. if (bar_val == 0)
  2074. continue;
  2075. high = bar_val & PCI_BASE_ADDRESS_MEM_TYPE_64;
  2076. if (high) {
  2077. bar_val |=
  2078. ((u64)hpdev->probed_bar[i + 1]
  2079. << 32);
  2080. } else {
  2081. bar_val |= 0xffffffffULL << 32;
  2082. }
  2083. bar_size = get_bar_size(bar_val);
  2084. if (high) {
  2085. if (high_size != bar_size) {
  2086. i++;
  2087. continue;
  2088. }
  2089. _hv_pcifront_write_config(hpdev,
  2090. PCI_BASE_ADDRESS_0 + (4 * i),
  2091. 4,
  2092. (u32)(high_base & 0xffffff00));
  2093. i++;
  2094. _hv_pcifront_write_config(hpdev,
  2095. PCI_BASE_ADDRESS_0 + (4 * i),
  2096. 4, (u32)(high_base >> 32));
  2097. high_base += bar_size;
  2098. } else {
  2099. if (low_size != bar_size)
  2100. continue;
  2101. _hv_pcifront_write_config(hpdev,
  2102. PCI_BASE_ADDRESS_0 + (4 * i),
  2103. 4,
  2104. (u32)(low_base & 0xffffff00));
  2105. low_base += bar_size;
  2106. }
  2107. }
  2108. if (high_size <= 1 && low_size <= 1) {
  2109. /*
  2110. * No need to set the PCI_COMMAND_MEMORY bit as
  2111. * the core PCI driver doesn't require the bit
  2112. * to be pre-set. Actually here we intentionally
  2113. * keep the bit off so that the PCI BAR probing
  2114. * in the core PCI driver doesn't cause Hyper-V
  2115. * to unnecessarily unmap/map the virtual BARs
  2116. * from/to the physical BARs multiple times.
  2117. * This reduces the VM boot time significantly
  2118. * if the BAR sizes are huge.
  2119. */
  2120. break;
  2121. }
  2122. }
  2123. high_size >>= 1;
  2124. low_size >>= 1;
  2125. } while (high_size || low_size);
  2126. spin_unlock_irqrestore(&hbus->device_list_lock, flags);
  2127. }
  2128. /*
  2129. * Assign entries in sysfs pci slot directory.
  2130. *
  2131. * Note that this function does not need to lock the children list
  2132. * because it is called from pci_devices_present_work which
  2133. * is serialized with hv_eject_device_work because they are on the
  2134. * same ordered workqueue. Therefore hbus->children list will not change
  2135. * even when pci_create_slot sleeps.
  2136. */
  2137. static void hv_pci_assign_slots(struct hv_pcibus_device *hbus)
  2138. {
  2139. struct hv_pci_dev *hpdev;
  2140. char name[SLOT_NAME_SIZE];
  2141. int slot_nr;
  2142. list_for_each_entry(hpdev, &hbus->children, list_entry) {
  2143. if (hpdev->pci_slot)
  2144. continue;
  2145. slot_nr = PCI_SLOT(wslot_to_devfn(hpdev->desc.win_slot.slot));
  2146. snprintf(name, SLOT_NAME_SIZE, "%u", hpdev->desc.ser);
  2147. hpdev->pci_slot = pci_create_slot(hbus->bridge->bus, slot_nr,
  2148. name, NULL);
  2149. if (IS_ERR(hpdev->pci_slot)) {
  2150. pr_warn("pci_create slot %s failed\n", name);
  2151. hpdev->pci_slot = NULL;
  2152. }
  2153. }
  2154. }
  2155. /*
  2156. * Remove entries in sysfs pci slot directory.
  2157. */
  2158. static void hv_pci_remove_slots(struct hv_pcibus_device *hbus)
  2159. {
  2160. struct hv_pci_dev *hpdev;
  2161. list_for_each_entry(hpdev, &hbus->children, list_entry) {
  2162. if (!hpdev->pci_slot)
  2163. continue;
  2164. pci_destroy_slot(hpdev->pci_slot);
  2165. hpdev->pci_slot = NULL;
  2166. }
  2167. }
  2168. /*
  2169. * Set NUMA node for the devices on the bus
  2170. */
  2171. static void hv_pci_assign_numa_node(struct hv_pcibus_device *hbus)
  2172. {
  2173. struct pci_dev *dev;
  2174. struct pci_bus *bus = hbus->bridge->bus;
  2175. struct hv_pci_dev *hv_dev;
  2176. list_for_each_entry(dev, &bus->devices, bus_list) {
  2177. hv_dev = get_pcichild_wslot(hbus, devfn_to_wslot(dev->devfn));
  2178. if (!hv_dev)
  2179. continue;
  2180. /*
  2181. * If the Hyper-V host doesn't provide a NUMA node for the
  2182. * device, default to node 0. With NUMA_NO_NODE the kernel
  2183. * may spread work across NUMA nodes, which degrades
  2184. * performance on Hyper-V.
  2185. */
  2186. set_dev_node(&dev->dev, 0);
  2187. if (hv_dev->desc.flags & HV_PCI_DEVICE_FLAG_NUMA_AFFINITY &&
  2188. hv_dev->desc.virtual_numa_node < num_possible_nodes())
  2189. /*
  2190. * The kernel may boot with some NUMA nodes offline
  2191. * (e.g. in a KDUMP kernel) or with NUMA disabled via
  2192. * "numa=off". In those cases, adjust the host provided
  2193. * NUMA node to a valid NUMA node used by the kernel.
  2194. */
  2195. set_dev_node(&dev->dev,
  2196. numa_map_to_online_node(
  2197. hv_dev->desc.virtual_numa_node));
  2198. put_pcichild(hv_dev);
  2199. }
  2200. }
  2201. /**
  2202. * create_root_hv_pci_bus() - Expose a new root PCI bus
  2203. * @hbus: Root PCI bus, as understood by this driver
  2204. *
  2205. * Return: 0 on success, -errno on failure
  2206. */
  2207. static int create_root_hv_pci_bus(struct hv_pcibus_device *hbus)
  2208. {
  2209. int error;
  2210. struct pci_host_bridge *bridge = hbus->bridge;
  2211. bridge->dev.parent = &hbus->hdev->device;
  2212. bridge->sysdata = &hbus->sysdata;
  2213. bridge->ops = &hv_pcifront_ops;
  2214. error = pci_scan_root_bus_bridge(bridge);
  2215. if (error)
  2216. return error;
  2217. pci_lock_rescan_remove();
  2218. hv_pci_assign_numa_node(hbus);
  2219. pci_bus_assign_resources(bridge->bus);
  2220. hv_pci_assign_slots(hbus);
  2221. pci_bus_add_devices(bridge->bus);
  2222. pci_unlock_rescan_remove();
  2223. hbus->state = hv_pcibus_installed;
  2224. return 0;
  2225. }
  2226. struct q_res_req_compl {
  2227. struct completion host_event;
  2228. struct hv_pci_dev *hpdev;
  2229. };
  2230. /**
  2231. * q_resource_requirements() - Query Resource Requirements
  2232. * @context: The completion context.
  2233. * @resp: The response that came from the host.
  2234. * @resp_packet_size: The size in bytes of resp.
  2235. *
  2236. * This function is invoked on completion of a Query Resource
  2237. * Requirements packet.
  2238. */
  2239. static void q_resource_requirements(void *context, struct pci_response *resp,
  2240. int resp_packet_size)
  2241. {
  2242. struct q_res_req_compl *completion = context;
  2243. struct pci_q_res_req_response *q_res_req =
  2244. (struct pci_q_res_req_response *)resp;
  2245. s32 status;
  2246. int i;
  2247. status = (resp_packet_size < sizeof(*q_res_req)) ? -1 : resp->status;
  2248. if (status < 0) {
  2249. dev_err(&completion->hpdev->hbus->hdev->device,
  2250. "query resource requirements failed: %x\n",
  2251. status);
  2252. } else {
  2253. for (i = 0; i < PCI_STD_NUM_BARS; i++) {
  2254. completion->hpdev->probed_bar[i] =
  2255. q_res_req->probed_bar[i];
  2256. }
  2257. }
  2258. complete(&completion->host_event);
  2259. }
  2260. /**
  2261. * new_pcichild_device() - Create a new child device
  2262. * @hbus: The internal struct tracking this root PCI bus.
  2263. * @desc: The information supplied so far from the host
  2264. * about the device.
  2265. *
  2266. * This function creates the tracking structure for a new child
  2267. * device and kicks off the process of figuring out what it is.
  2268. *
  2269. * Return: Pointer to the new tracking struct
  2270. */
  2271. static struct hv_pci_dev *new_pcichild_device(struct hv_pcibus_device *hbus,
  2272. struct hv_pcidev_description *desc)
  2273. {
  2274. struct hv_pci_dev *hpdev;
  2275. struct pci_child_message *res_req;
  2276. struct q_res_req_compl comp_pkt;
  2277. struct {
  2278. struct pci_packet init_packet;
  2279. u8 buffer[sizeof(struct pci_child_message)];
  2280. } pkt;
  2281. unsigned long flags;
  2282. int ret;
  2283. hpdev = kzalloc_obj(*hpdev);
  2284. if (!hpdev)
  2285. return NULL;
  2286. hpdev->hbus = hbus;
  2287. memset(&pkt, 0, sizeof(pkt));
  2288. init_completion(&comp_pkt.host_event);
  2289. comp_pkt.hpdev = hpdev;
  2290. pkt.init_packet.compl_ctxt = &comp_pkt;
  2291. pkt.init_packet.completion_func = q_resource_requirements;
  2292. res_req = (struct pci_child_message *)pkt.buffer;
  2293. res_req->message_type.type = PCI_QUERY_RESOURCE_REQUIREMENTS;
  2294. res_req->wslot.slot = desc->win_slot.slot;
  2295. ret = vmbus_sendpacket(hbus->hdev->channel, res_req,
  2296. sizeof(struct pci_child_message),
  2297. (unsigned long)&pkt.init_packet,
  2298. VM_PKT_DATA_INBAND,
  2299. VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
  2300. if (ret)
  2301. goto error;
  2302. if (wait_for_response(hbus->hdev, &comp_pkt.host_event))
  2303. goto error;
  2304. hpdev->desc = *desc;
  2305. refcount_set(&hpdev->refs, 1);
  2306. get_pcichild(hpdev);
  2307. spin_lock_irqsave(&hbus->device_list_lock, flags);
  2308. list_add_tail(&hpdev->list_entry, &hbus->children);
  2309. spin_unlock_irqrestore(&hbus->device_list_lock, flags);
  2310. return hpdev;
  2311. error:
  2312. kfree(hpdev);
  2313. return NULL;
  2314. }
  2315. /**
  2316. * get_pcichild_wslot() - Find device from slot
  2317. * @hbus: Root PCI bus, as understood by this driver
  2318. * @wslot: Location on the bus
  2319. *
  2320. * This function looks up a PCI device and returns the internal
  2321. * representation of it. It acquires a reference on it, so that
  2322. * the device won't be deleted while somebody is using it. The
  2323. * caller is responsible for calling put_pcichild() to release
  2324. * this reference.
  2325. *
  2326. * Return: Internal representation of a PCI device
  2327. */
  2328. static struct hv_pci_dev *get_pcichild_wslot(struct hv_pcibus_device *hbus,
  2329. u32 wslot)
  2330. {
  2331. unsigned long flags;
  2332. struct hv_pci_dev *iter, *hpdev = NULL;
  2333. spin_lock_irqsave(&hbus->device_list_lock, flags);
  2334. list_for_each_entry(iter, &hbus->children, list_entry) {
  2335. if (iter->desc.win_slot.slot == wslot) {
  2336. hpdev = iter;
  2337. get_pcichild(hpdev);
  2338. break;
  2339. }
  2340. }
  2341. spin_unlock_irqrestore(&hbus->device_list_lock, flags);
  2342. return hpdev;
  2343. }
  2344. /**
  2345. * pci_devices_present_work() - Handle new list of child devices
  2346. * @work: Work struct embedded in struct hv_dr_work
  2347. *
  2348. * "Bus Relations" is the Windows term for "children of this
  2349. * bus." The terminology is preserved here for people trying to
  2350. * debug the interaction between Hyper-V and Linux. This
  2351. * function is called when the parent partition reports a list
  2352. * of functions that should be observed under this PCI Express
  2353. * port (bus).
  2354. *
  2355. * This function updates the list, and must tolerate being
  2356. * called multiple times with the same information. The typical
  2357. * number of child devices is one, with very atypical cases
  2358. * involving three or four, so the algorithms used here can be
  2359. * simple and inefficient.
  2360. *
  2361. * It must also treat the omission of a previously observed device as
  2362. * notification that the device no longer exists.
  2363. *
  2364. * Note that this function is serialized with hv_eject_device_work(),
  2365. * because both are pushed to the ordered workqueue hbus->wq.
  2366. */
  2367. static void pci_devices_present_work(struct work_struct *work)
  2368. {
  2369. u32 child_no;
  2370. bool found;
  2371. struct hv_pcidev_description *new_desc;
  2372. struct hv_pci_dev *hpdev;
  2373. struct hv_pcibus_device *hbus;
  2374. struct list_head removed;
  2375. struct hv_dr_work *dr_wrk;
  2376. struct hv_dr_state *dr = NULL;
  2377. unsigned long flags;
  2378. dr_wrk = container_of(work, struct hv_dr_work, wrk);
  2379. hbus = dr_wrk->bus;
  2380. kfree(dr_wrk);
  2381. INIT_LIST_HEAD(&removed);
  2382. /* Pull this off the queue and process it if it was the last one. */
  2383. spin_lock_irqsave(&hbus->device_list_lock, flags);
  2384. while (!list_empty(&hbus->dr_list)) {
  2385. dr = list_first_entry(&hbus->dr_list, struct hv_dr_state,
  2386. list_entry);
  2387. list_del(&dr->list_entry);
  2388. /* Throw this away if the list still has stuff in it. */
  2389. if (!list_empty(&hbus->dr_list)) {
  2390. kfree(dr);
  2391. continue;
  2392. }
  2393. }
  2394. spin_unlock_irqrestore(&hbus->device_list_lock, flags);
  2395. if (!dr)
  2396. return;
  2397. mutex_lock(&hbus->state_lock);
  2398. /* First, mark all existing children as reported missing. */
  2399. spin_lock_irqsave(&hbus->device_list_lock, flags);
  2400. list_for_each_entry(hpdev, &hbus->children, list_entry) {
  2401. hpdev->reported_missing = true;
  2402. }
  2403. spin_unlock_irqrestore(&hbus->device_list_lock, flags);
  2404. /* Next, add back any reported devices. */
  2405. for (child_no = 0; child_no < dr->device_count; child_no++) {
  2406. found = false;
  2407. new_desc = &dr->func[child_no];
  2408. spin_lock_irqsave(&hbus->device_list_lock, flags);
  2409. list_for_each_entry(hpdev, &hbus->children, list_entry) {
  2410. if ((hpdev->desc.win_slot.slot == new_desc->win_slot.slot) &&
  2411. (hpdev->desc.v_id == new_desc->v_id) &&
  2412. (hpdev->desc.d_id == new_desc->d_id) &&
  2413. (hpdev->desc.ser == new_desc->ser)) {
  2414. hpdev->reported_missing = false;
  2415. found = true;
  2416. }
  2417. }
  2418. spin_unlock_irqrestore(&hbus->device_list_lock, flags);
  2419. if (!found) {
  2420. hpdev = new_pcichild_device(hbus, new_desc);
  2421. if (!hpdev)
  2422. dev_err(&hbus->hdev->device,
  2423. "couldn't record a child device.\n");
  2424. }
  2425. }
  2426. /* Move missing children to a list on the stack. */
  2427. spin_lock_irqsave(&hbus->device_list_lock, flags);
  2428. do {
  2429. found = false;
  2430. list_for_each_entry(hpdev, &hbus->children, list_entry) {
  2431. if (hpdev->reported_missing) {
  2432. found = true;
  2433. put_pcichild(hpdev);
  2434. list_move_tail(&hpdev->list_entry, &removed);
  2435. break;
  2436. }
  2437. }
  2438. } while (found);
  2439. spin_unlock_irqrestore(&hbus->device_list_lock, flags);
  2440. /* Delete everything that should no longer exist. */
  2441. while (!list_empty(&removed)) {
  2442. hpdev = list_first_entry(&removed, struct hv_pci_dev,
  2443. list_entry);
  2444. list_del(&hpdev->list_entry);
  2445. if (hpdev->pci_slot)
  2446. pci_destroy_slot(hpdev->pci_slot);
  2447. put_pcichild(hpdev);
  2448. }
  2449. switch (hbus->state) {
  2450. case hv_pcibus_installed:
  2451. /*
  2452. * Tell the core to rescan bus
  2453. * because there may have been changes.
  2454. */
  2455. pci_lock_rescan_remove();
  2456. pci_scan_child_bus(hbus->bridge->bus);
  2457. hv_pci_assign_numa_node(hbus);
  2458. hv_pci_assign_slots(hbus);
  2459. pci_unlock_rescan_remove();
  2460. break;
  2461. case hv_pcibus_init:
  2462. case hv_pcibus_probed:
  2463. survey_child_resources(hbus);
  2464. break;
  2465. default:
  2466. break;
  2467. }
  2468. mutex_unlock(&hbus->state_lock);
  2469. kfree(dr);
  2470. }
  2471. /**
  2472. * hv_pci_start_relations_work() - Queue work to start device discovery
  2473. * @hbus: Root PCI bus, as understood by this driver
  2474. * @dr: The list of children returned from host
  2475. *
  2476. * Return: 0 on success, -errno on failure
  2477. */
  2478. static int hv_pci_start_relations_work(struct hv_pcibus_device *hbus,
  2479. struct hv_dr_state *dr)
  2480. {
  2481. struct hv_dr_work *dr_wrk;
  2482. unsigned long flags;
  2483. bool pending_dr;
  2484. if (hbus->state == hv_pcibus_removing) {
  2485. dev_info(&hbus->hdev->device,
  2486. "PCI VMBus BUS_RELATIONS: ignored\n");
  2487. return -ENOENT;
  2488. }
  2489. dr_wrk = kzalloc_obj(*dr_wrk, GFP_NOWAIT);
  2490. if (!dr_wrk)
  2491. return -ENOMEM;
  2492. INIT_WORK(&dr_wrk->wrk, pci_devices_present_work);
  2493. dr_wrk->bus = hbus;
  2494. spin_lock_irqsave(&hbus->device_list_lock, flags);
  2495. /*
  2496. * If pending_dr is true, we have already queued a work,
  2497. * which will see the new dr. Otherwise, we need to
  2498. * queue a new work.
  2499. */
  2500. pending_dr = !list_empty(&hbus->dr_list);
  2501. list_add_tail(&dr->list_entry, &hbus->dr_list);
  2502. spin_unlock_irqrestore(&hbus->device_list_lock, flags);
  2503. if (pending_dr)
  2504. kfree(dr_wrk);
  2505. else
  2506. queue_work(hbus->wq, &dr_wrk->wrk);
  2507. return 0;
  2508. }
  2509. /**
  2510. * hv_pci_devices_present() - Handle list of new children
  2511. * @hbus: Root PCI bus, as understood by this driver
  2512. * @relations: Packet from host listing children
  2513. *
  2514. * Process a new list of devices on the bus. The list of devices is
  2515. * discovered by VSP and sent to us via VSP message PCI_BUS_RELATIONS,
  2516. * whenever a new list of devices for this bus appears.
  2517. */
  2518. static void hv_pci_devices_present(struct hv_pcibus_device *hbus,
  2519. struct pci_bus_relations *relations)
  2520. {
  2521. struct hv_dr_state *dr;
  2522. int i;
  2523. dr = kzalloc_flex(*dr, func, relations->device_count, GFP_NOWAIT);
  2524. if (!dr)
  2525. return;
  2526. dr->device_count = relations->device_count;
  2527. for (i = 0; i < dr->device_count; i++) {
  2528. dr->func[i].v_id = relations->func[i].v_id;
  2529. dr->func[i].d_id = relations->func[i].d_id;
  2530. dr->func[i].rev = relations->func[i].rev;
  2531. dr->func[i].prog_intf = relations->func[i].prog_intf;
  2532. dr->func[i].subclass = relations->func[i].subclass;
  2533. dr->func[i].base_class = relations->func[i].base_class;
  2534. dr->func[i].subsystem_id = relations->func[i].subsystem_id;
  2535. dr->func[i].win_slot = relations->func[i].win_slot;
  2536. dr->func[i].ser = relations->func[i].ser;
  2537. }
  2538. if (hv_pci_start_relations_work(hbus, dr))
  2539. kfree(dr);
  2540. }
  2541. /**
  2542. * hv_pci_devices_present2() - Handle list of new children
  2543. * @hbus: Root PCI bus, as understood by this driver
  2544. * @relations: Packet from host listing children
  2545. *
  2546. * This function is the v2 version of hv_pci_devices_present()
  2547. */
  2548. static void hv_pci_devices_present2(struct hv_pcibus_device *hbus,
  2549. struct pci_bus_relations2 *relations)
  2550. {
  2551. struct hv_dr_state *dr;
  2552. int i;
  2553. dr = kzalloc_flex(*dr, func, relations->device_count, GFP_NOWAIT);
  2554. if (!dr)
  2555. return;
  2556. dr->device_count = relations->device_count;
  2557. for (i = 0; i < dr->device_count; i++) {
  2558. dr->func[i].v_id = relations->func[i].v_id;
  2559. dr->func[i].d_id = relations->func[i].d_id;
  2560. dr->func[i].rev = relations->func[i].rev;
  2561. dr->func[i].prog_intf = relations->func[i].prog_intf;
  2562. dr->func[i].subclass = relations->func[i].subclass;
  2563. dr->func[i].base_class = relations->func[i].base_class;
  2564. dr->func[i].subsystem_id = relations->func[i].subsystem_id;
  2565. dr->func[i].win_slot = relations->func[i].win_slot;
  2566. dr->func[i].ser = relations->func[i].ser;
  2567. dr->func[i].flags = relations->func[i].flags;
  2568. dr->func[i].virtual_numa_node =
  2569. relations->func[i].virtual_numa_node;
  2570. }
  2571. if (hv_pci_start_relations_work(hbus, dr))
  2572. kfree(dr);
  2573. }
  2574. /**
  2575. * hv_eject_device_work() - Asynchronously handles ejection
  2576. * @work: Work struct embedded in internal device struct
  2577. *
  2578. * This function handles ejecting a device. Windows will
  2579. * attempt to gracefully eject a device, waiting 60 seconds to
  2580. * hear back from the guest OS that this completed successfully.
  2581. * If this timer expires, the device will be forcibly removed.
  2582. */
  2583. static void hv_eject_device_work(struct work_struct *work)
  2584. {
  2585. struct pci_eject_response *ejct_pkt;
  2586. struct hv_pcibus_device *hbus;
  2587. struct hv_pci_dev *hpdev;
  2588. struct pci_dev *pdev;
  2589. unsigned long flags;
  2590. int wslot;
  2591. struct {
  2592. struct pci_packet pkt;
  2593. u8 buffer[sizeof(struct pci_eject_response)];
  2594. } ctxt;
  2595. hpdev = container_of(work, struct hv_pci_dev, wrk);
  2596. hbus = hpdev->hbus;
  2597. mutex_lock(&hbus->state_lock);
  2598. /*
  2599. * Ejection can come before or after the PCI bus has been set up, so
  2600. * attempt to find it and tear down the bus state, if it exists. This
  2601. * must be done without constructs like pci_domain_nr(hbus->bridge->bus)
  2602. * because hbus->bridge->bus may not exist yet.
  2603. */
  2604. wslot = wslot_to_devfn(hpdev->desc.win_slot.slot);
  2605. pdev = pci_get_domain_bus_and_slot(hbus->bridge->domain_nr, 0, wslot);
  2606. if (pdev) {
  2607. pci_lock_rescan_remove();
  2608. pci_stop_and_remove_bus_device(pdev);
  2609. pci_dev_put(pdev);
  2610. pci_unlock_rescan_remove();
  2611. }
  2612. spin_lock_irqsave(&hbus->device_list_lock, flags);
  2613. list_del(&hpdev->list_entry);
  2614. spin_unlock_irqrestore(&hbus->device_list_lock, flags);
  2615. if (hpdev->pci_slot)
  2616. pci_destroy_slot(hpdev->pci_slot);
  2617. memset(&ctxt, 0, sizeof(ctxt));
  2618. ejct_pkt = (struct pci_eject_response *)ctxt.buffer;
  2619. ejct_pkt->message_type.type = PCI_EJECTION_COMPLETE;
  2620. ejct_pkt->wslot.slot = hpdev->desc.win_slot.slot;
  2621. vmbus_sendpacket(hbus->hdev->channel, ejct_pkt,
  2622. sizeof(*ejct_pkt), 0,
  2623. VM_PKT_DATA_INBAND, 0);
  2624. /* For the get_pcichild() in hv_pci_eject_device() */
  2625. put_pcichild(hpdev);
  2626. /* For the two refs got in new_pcichild_device() */
  2627. put_pcichild(hpdev);
  2628. put_pcichild(hpdev);
  2629. /* hpdev has been freed. Do not use it any more. */
  2630. mutex_unlock(&hbus->state_lock);
  2631. }
  2632. /**
  2633. * hv_pci_eject_device() - Handles device ejection
  2634. * @hpdev: Internal device tracking struct
  2635. *
  2636. * This function is invoked when an ejection packet arrives. It
  2637. * just schedules work so that we don't re-enter the packet
  2638. * delivery code handling the ejection.
  2639. */
  2640. static void hv_pci_eject_device(struct hv_pci_dev *hpdev)
  2641. {
  2642. struct hv_pcibus_device *hbus = hpdev->hbus;
  2643. struct hv_device *hdev = hbus->hdev;
  2644. if (hbus->state == hv_pcibus_removing) {
  2645. dev_info(&hdev->device, "PCI VMBus EJECT: ignored\n");
  2646. return;
  2647. }
  2648. get_pcichild(hpdev);
  2649. INIT_WORK(&hpdev->wrk, hv_eject_device_work);
  2650. queue_work(hbus->wq, &hpdev->wrk);
  2651. }
  2652. /**
  2653. * hv_pci_onchannelcallback() - Handles incoming packets
  2654. * @context: Internal bus tracking struct
  2655. *
  2656. * This function is invoked whenever the host sends a packet to
  2657. * this channel (which is private to this root PCI bus).
  2658. */
  2659. static void hv_pci_onchannelcallback(void *context)
  2660. {
  2661. const int packet_size = 0x100;
  2662. int ret;
  2663. struct hv_pcibus_device *hbus = context;
  2664. struct vmbus_channel *chan = hbus->hdev->channel;
  2665. u32 bytes_recvd;
  2666. u64 req_id, req_addr;
  2667. struct vmpacket_descriptor *desc;
  2668. unsigned char *buffer;
  2669. int bufferlen = packet_size;
  2670. struct pci_packet *comp_packet;
  2671. struct pci_response *response;
  2672. struct pci_incoming_message *new_message;
  2673. struct pci_bus_relations *bus_rel;
  2674. struct pci_bus_relations2 *bus_rel2;
  2675. struct pci_dev_inval_block *inval;
  2676. struct pci_dev_incoming *dev_message;
  2677. struct hv_pci_dev *hpdev;
  2678. unsigned long flags;
  2679. buffer = kmalloc(bufferlen, GFP_ATOMIC);
  2680. if (!buffer)
  2681. return;
  2682. while (1) {
  2683. ret = vmbus_recvpacket_raw(chan, buffer, bufferlen,
  2684. &bytes_recvd, &req_id);
  2685. if (ret == -ENOBUFS) {
  2686. kfree(buffer);
  2687. /* Handle large packet */
  2688. bufferlen = bytes_recvd;
  2689. buffer = kmalloc(bytes_recvd, GFP_ATOMIC);
  2690. if (!buffer)
  2691. return;
  2692. continue;
  2693. }
  2694. /* Zero length indicates there are no more packets. */
  2695. if (ret || !bytes_recvd)
  2696. break;
  2697. /*
  2698. * All incoming packets must be at least as large as a
  2699. * response.
  2700. */
  2701. if (bytes_recvd <= sizeof(struct pci_response))
  2702. continue;
  2703. desc = (struct vmpacket_descriptor *)buffer;
  2704. switch (desc->type) {
  2705. case VM_PKT_COMP:
  2706. lock_requestor(chan, flags);
  2707. req_addr = __vmbus_request_addr_match(chan, req_id,
  2708. VMBUS_RQST_ADDR_ANY);
  2709. if (req_addr == VMBUS_RQST_ERROR) {
  2710. unlock_requestor(chan, flags);
  2711. dev_err(&hbus->hdev->device,
  2712. "Invalid transaction ID %llx\n",
  2713. req_id);
  2714. break;
  2715. }
  2716. comp_packet = (struct pci_packet *)req_addr;
  2717. response = (struct pci_response *)buffer;
  2718. /*
  2719. * Call ->completion_func() within the critical section to make
  2720. * sure that the packet pointer is still valid during the call:
  2721. * here 'valid' means that there's a task still waiting for the
  2722. * completion, and that the packet data is still on the waiting
  2723. * task's stack. Cf. hv_compose_msi_msg().
  2724. */
  2725. comp_packet->completion_func(comp_packet->compl_ctxt,
  2726. response,
  2727. bytes_recvd);
  2728. unlock_requestor(chan, flags);
  2729. break;
  2730. case VM_PKT_DATA_INBAND:
  2731. new_message = (struct pci_incoming_message *)buffer;
  2732. switch (new_message->message_type.type) {
  2733. case PCI_BUS_RELATIONS:
  2734. bus_rel = (struct pci_bus_relations *)buffer;
  2735. if (bytes_recvd < sizeof(*bus_rel) ||
  2736. bytes_recvd <
  2737. struct_size(bus_rel, func,
  2738. bus_rel->device_count)) {
  2739. dev_err(&hbus->hdev->device,
  2740. "bus relations too small\n");
  2741. break;
  2742. }
  2743. hv_pci_devices_present(hbus, bus_rel);
  2744. break;
  2745. case PCI_BUS_RELATIONS2:
  2746. bus_rel2 = (struct pci_bus_relations2 *)buffer;
  2747. if (bytes_recvd < sizeof(*bus_rel2) ||
  2748. bytes_recvd <
  2749. struct_size(bus_rel2, func,
  2750. bus_rel2->device_count)) {
  2751. dev_err(&hbus->hdev->device,
  2752. "bus relations v2 too small\n");
  2753. break;
  2754. }
  2755. hv_pci_devices_present2(hbus, bus_rel2);
  2756. break;
  2757. case PCI_EJECT:
  2758. dev_message = (struct pci_dev_incoming *)buffer;
  2759. if (bytes_recvd < sizeof(*dev_message)) {
  2760. dev_err(&hbus->hdev->device,
  2761. "eject message too small\n");
  2762. break;
  2763. }
  2764. hpdev = get_pcichild_wslot(hbus,
  2765. dev_message->wslot.slot);
  2766. if (hpdev) {
  2767. hv_pci_eject_device(hpdev);
  2768. put_pcichild(hpdev);
  2769. }
  2770. break;
  2771. case PCI_INVALIDATE_BLOCK:
  2772. inval = (struct pci_dev_inval_block *)buffer;
  2773. if (bytes_recvd < sizeof(*inval)) {
  2774. dev_err(&hbus->hdev->device,
  2775. "invalidate message too small\n");
  2776. break;
  2777. }
  2778. hpdev = get_pcichild_wslot(hbus,
  2779. inval->wslot.slot);
  2780. if (hpdev) {
  2781. if (hpdev->block_invalidate) {
  2782. hpdev->block_invalidate(
  2783. hpdev->invalidate_context,
  2784. inval->block_mask);
  2785. }
  2786. put_pcichild(hpdev);
  2787. }
  2788. break;
  2789. default:
  2790. dev_warn(&hbus->hdev->device,
  2791. "Unimplemented protocol message %x\n",
  2792. new_message->message_type.type);
  2793. break;
  2794. }
  2795. break;
  2796. default:
  2797. dev_err(&hbus->hdev->device,
  2798. "unhandled packet type %d, tid %llx len %d\n",
  2799. desc->type, req_id, bytes_recvd);
  2800. break;
  2801. }
  2802. }
  2803. kfree(buffer);
  2804. }
  2805. /**
  2806. * hv_pci_protocol_negotiation() - Set up protocol
  2807. * @hdev: VMBus's tracking struct for this root PCI bus.
  2808. * @version: Array of supported channel protocol versions in
  2809. * the order of probing - highest go first.
  2810. * @num_version: Number of elements in the version array.
  2811. *
  2812. * This driver is intended to support running on Windows 10
  2813. * (server) and later versions. It will not run on earlier
  2814. * versions, as they assume that many of the operations which
  2815. * Linux needs accomplished with a spinlock held were done via
  2816. * asynchronous messaging via VMBus. Windows 10 increases the
  2817. * surface area of PCI emulation so that these actions can take
  2818. * place by suspending a virtual processor for their duration.
  2819. *
  2820. * This function negotiates the channel protocol version,
  2821. * failing if the host doesn't support the necessary protocol
  2822. * level.
  2823. */
  2824. static int hv_pci_protocol_negotiation(struct hv_device *hdev,
  2825. enum pci_protocol_version_t version[],
  2826. int num_version)
  2827. {
  2828. struct hv_pcibus_device *hbus = hv_get_drvdata(hdev);
  2829. struct pci_version_request *version_req;
  2830. struct hv_pci_compl comp_pkt;
  2831. struct pci_packet *pkt;
  2832. int ret;
  2833. int i;
  2834. /*
  2835. * Initiate the handshake with the host and negotiate
  2836. * a version that the host can support. We start with the
  2837. * highest version number and go down if the host cannot
  2838. * support it.
  2839. */
  2840. pkt = kzalloc(sizeof(*pkt) + sizeof(*version_req), GFP_KERNEL);
  2841. if (!pkt)
  2842. return -ENOMEM;
  2843. init_completion(&comp_pkt.host_event);
  2844. pkt->completion_func = hv_pci_generic_compl;
  2845. pkt->compl_ctxt = &comp_pkt;
  2846. version_req = (struct pci_version_request *)(pkt + 1);
  2847. version_req->message_type.type = PCI_QUERY_PROTOCOL_VERSION;
  2848. for (i = 0; i < num_version; i++) {
  2849. version_req->protocol_version = version[i];
  2850. ret = vmbus_sendpacket(hdev->channel, version_req,
  2851. sizeof(struct pci_version_request),
  2852. (unsigned long)pkt, VM_PKT_DATA_INBAND,
  2853. VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
  2854. if (!ret)
  2855. ret = wait_for_response(hdev, &comp_pkt.host_event);
  2856. if (ret) {
  2857. dev_err(&hdev->device,
  2858. "PCI Pass-through VSP failed to request version: %d",
  2859. ret);
  2860. goto exit;
  2861. }
  2862. if (comp_pkt.completion_status >= 0) {
  2863. hbus->protocol_version = version[i];
  2864. dev_info(&hdev->device,
  2865. "PCI VMBus probing: Using version %#x\n",
  2866. hbus->protocol_version);
  2867. goto exit;
  2868. }
  2869. if (comp_pkt.completion_status != STATUS_REVISION_MISMATCH) {
  2870. dev_err(&hdev->device,
  2871. "PCI Pass-through VSP failed version request: %#x",
  2872. comp_pkt.completion_status);
  2873. ret = -EPROTO;
  2874. goto exit;
  2875. }
  2876. reinit_completion(&comp_pkt.host_event);
  2877. }
  2878. dev_err(&hdev->device,
  2879. "PCI pass-through VSP failed to find supported version");
  2880. ret = -EPROTO;
  2881. exit:
  2882. kfree(pkt);
  2883. return ret;
  2884. }
  2885. /**
  2886. * hv_pci_free_bridge_windows() - Release memory regions for the
  2887. * bus
  2888. * @hbus: Root PCI bus, as understood by this driver
  2889. */
  2890. static void hv_pci_free_bridge_windows(struct hv_pcibus_device *hbus)
  2891. {
  2892. /*
  2893. * Set the resources back to the way they looked when they
  2894. * were allocated by setting IORESOURCE_BUSY again.
  2895. */
  2896. if (hbus->low_mmio_space && hbus->low_mmio_res) {
  2897. hbus->low_mmio_res->flags |= IORESOURCE_BUSY;
  2898. vmbus_free_mmio(hbus->low_mmio_res->start,
  2899. resource_size(hbus->low_mmio_res));
  2900. }
  2901. if (hbus->high_mmio_space && hbus->high_mmio_res) {
  2902. hbus->high_mmio_res->flags |= IORESOURCE_BUSY;
  2903. vmbus_free_mmio(hbus->high_mmio_res->start,
  2904. resource_size(hbus->high_mmio_res));
  2905. }
  2906. }
  2907. /**
  2908. * hv_pci_allocate_bridge_windows() - Allocate memory regions
  2909. * for the bus
  2910. * @hbus: Root PCI bus, as understood by this driver
  2911. *
  2912. * This function calls vmbus_allocate_mmio(), which is itself a
  2913. * bit of a compromise. Ideally, we might change the pnp layer
  2914. * in the kernel such that it comprehends either PCI devices
  2915. * which are "grandchildren of ACPI," with some intermediate bus
  2916. * node (in this case, VMBus) or change it such that it
  2917. * understands VMBus. The pnp layer, however, has been declared
  2918. * deprecated, and not subject to change.
  2919. *
  2920. * The workaround, implemented here, is to ask VMBus to allocate
  2921. * MMIO space for this bus. VMBus itself knows which ranges are
  2922. * appropriate by looking at its own ACPI objects. Then, after
  2923. * these ranges are claimed, they're modified to look like they
  2924. * would have looked if the ACPI and pnp code had allocated
  2925. * bridge windows. These descriptors have to exist in this form
  2926. * in order to satisfy the code which will get invoked when the
  2927. * endpoint PCI function driver calls request_mem_region() or
  2928. * request_mem_region_exclusive().
  2929. *
  2930. * Return: 0 on success, -errno on failure
  2931. */
  2932. static int hv_pci_allocate_bridge_windows(struct hv_pcibus_device *hbus)
  2933. {
  2934. resource_size_t align;
  2935. int ret;
  2936. if (hbus->low_mmio_space) {
  2937. align = 1ULL << (63 - __builtin_clzll(hbus->low_mmio_space));
  2938. ret = vmbus_allocate_mmio(&hbus->low_mmio_res, hbus->hdev, 0,
  2939. (u64)(u32)0xffffffff,
  2940. hbus->low_mmio_space,
  2941. align, false);
  2942. if (ret) {
  2943. dev_err(&hbus->hdev->device,
  2944. "Need %#llx of low MMIO space. Consider reconfiguring the VM.\n",
  2945. hbus->low_mmio_space);
  2946. return ret;
  2947. }
  2948. /* Modify this resource to become a bridge window. */
  2949. hbus->low_mmio_res->flags |= IORESOURCE_WINDOW;
  2950. hbus->low_mmio_res->flags &= ~IORESOURCE_BUSY;
  2951. pci_add_resource(&hbus->bridge->windows, hbus->low_mmio_res);
  2952. }
  2953. if (hbus->high_mmio_space) {
  2954. align = 1ULL << (63 - __builtin_clzll(hbus->high_mmio_space));
  2955. ret = vmbus_allocate_mmio(&hbus->high_mmio_res, hbus->hdev,
  2956. 0x100000000, -1,
  2957. hbus->high_mmio_space, align,
  2958. false);
  2959. if (ret) {
  2960. dev_err(&hbus->hdev->device,
  2961. "Need %#llx of high MMIO space. Consider reconfiguring the VM.\n",
  2962. hbus->high_mmio_space);
  2963. goto release_low_mmio;
  2964. }
  2965. /* Modify this resource to become a bridge window. */
  2966. hbus->high_mmio_res->flags |= IORESOURCE_WINDOW;
  2967. hbus->high_mmio_res->flags &= ~IORESOURCE_BUSY;
  2968. pci_add_resource(&hbus->bridge->windows, hbus->high_mmio_res);
  2969. }
  2970. return 0;
  2971. release_low_mmio:
  2972. if (hbus->low_mmio_res) {
  2973. vmbus_free_mmio(hbus->low_mmio_res->start,
  2974. resource_size(hbus->low_mmio_res));
  2975. }
  2976. return ret;
  2977. }
  2978. /**
  2979. * hv_allocate_config_window() - Find MMIO space for PCI Config
  2980. * @hbus: Root PCI bus, as understood by this driver
  2981. *
  2982. * This function claims memory-mapped I/O space for accessing
  2983. * configuration space for the functions on this bus.
  2984. *
  2985. * Return: 0 on success, -errno on failure
  2986. */
  2987. static int hv_allocate_config_window(struct hv_pcibus_device *hbus)
  2988. {
  2989. int ret;
  2990. /*
  2991. * Set up a region of MMIO space to use for accessing configuration
  2992. * space.
  2993. */
  2994. ret = vmbus_allocate_mmio(&hbus->mem_config, hbus->hdev, 0, -1,
  2995. PCI_CONFIG_MMIO_LENGTH, 0x1000, false);
  2996. if (ret)
  2997. return ret;
  2998. /*
  2999. * vmbus_allocate_mmio() gets used for allocating both device endpoint
  3000. * resource claims (those which cannot be overlapped) and the ranges
  3001. * which are valid for the children of this bus, which are intended
  3002. * to be overlapped by those children. Set the flag on this claim
  3003. * meaning that this region can't be overlapped.
  3004. */
  3005. hbus->mem_config->flags |= IORESOURCE_BUSY;
  3006. return 0;
  3007. }
  3008. static void hv_free_config_window(struct hv_pcibus_device *hbus)
  3009. {
  3010. vmbus_free_mmio(hbus->mem_config->start, PCI_CONFIG_MMIO_LENGTH);
  3011. }
  3012. static int hv_pci_bus_exit(struct hv_device *hdev, bool keep_devs);
  3013. /**
  3014. * hv_pci_enter_d0() - Bring the "bus" into the D0 power state
  3015. * @hdev: VMBus's tracking struct for this root PCI bus
  3016. *
  3017. * Return: 0 on success, -errno on failure
  3018. */
  3019. static int hv_pci_enter_d0(struct hv_device *hdev)
  3020. {
  3021. struct hv_pcibus_device *hbus = hv_get_drvdata(hdev);
  3022. struct pci_bus_d0_entry *d0_entry;
  3023. struct hv_pci_compl comp_pkt;
  3024. struct pci_packet *pkt;
  3025. bool retry = true;
  3026. int ret;
  3027. enter_d0_retry:
  3028. /*
  3029. * Tell the host that the bus is ready to use, and moved into the
  3030. * powered-on state. This includes telling the host which region
  3031. * of memory-mapped I/O space has been chosen for configuration space
  3032. * access.
  3033. */
  3034. pkt = kzalloc(sizeof(*pkt) + sizeof(*d0_entry), GFP_KERNEL);
  3035. if (!pkt)
  3036. return -ENOMEM;
  3037. init_completion(&comp_pkt.host_event);
  3038. pkt->completion_func = hv_pci_generic_compl;
  3039. pkt->compl_ctxt = &comp_pkt;
  3040. d0_entry = (struct pci_bus_d0_entry *)(pkt + 1);
  3041. d0_entry->message_type.type = PCI_BUS_D0ENTRY;
  3042. d0_entry->mmio_base = hbus->mem_config->start;
  3043. ret = vmbus_sendpacket(hdev->channel, d0_entry, sizeof(*d0_entry),
  3044. (unsigned long)pkt, VM_PKT_DATA_INBAND,
  3045. VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
  3046. if (!ret)
  3047. ret = wait_for_response(hdev, &comp_pkt.host_event);
  3048. if (ret)
  3049. goto exit;
  3050. /*
  3051. * In certain case (Kdump) the pci device of interest was
  3052. * not cleanly shut down and resource is still held on host
  3053. * side, the host could return invalid device status.
  3054. * We need to explicitly request host to release the resource
  3055. * and try to enter D0 again.
  3056. */
  3057. if (comp_pkt.completion_status < 0 && retry) {
  3058. retry = false;
  3059. dev_err(&hdev->device, "Retrying D0 Entry\n");
  3060. /*
  3061. * Hv_pci_bus_exit() calls hv_send_resource_released()
  3062. * to free up resources of its child devices.
  3063. * In the kdump kernel we need to set the
  3064. * wslot_res_allocated to 255 so it scans all child
  3065. * devices to release resources allocated in the
  3066. * normal kernel before panic happened.
  3067. */
  3068. hbus->wslot_res_allocated = 255;
  3069. ret = hv_pci_bus_exit(hdev, true);
  3070. if (ret == 0) {
  3071. kfree(pkt);
  3072. goto enter_d0_retry;
  3073. }
  3074. dev_err(&hdev->device,
  3075. "Retrying D0 failed with ret %d\n", ret);
  3076. }
  3077. if (comp_pkt.completion_status < 0) {
  3078. dev_err(&hdev->device,
  3079. "PCI Pass-through VSP failed D0 Entry with status %x\n",
  3080. comp_pkt.completion_status);
  3081. ret = -EPROTO;
  3082. goto exit;
  3083. }
  3084. ret = 0;
  3085. exit:
  3086. kfree(pkt);
  3087. return ret;
  3088. }
  3089. /**
  3090. * hv_pci_query_relations() - Ask host to send list of child
  3091. * devices
  3092. * @hdev: VMBus's tracking struct for this root PCI bus
  3093. *
  3094. * Return: 0 on success, -errno on failure
  3095. */
  3096. static int hv_pci_query_relations(struct hv_device *hdev)
  3097. {
  3098. struct hv_pcibus_device *hbus = hv_get_drvdata(hdev);
  3099. struct pci_message message;
  3100. struct completion comp;
  3101. int ret;
  3102. /* Ask the host to send along the list of child devices */
  3103. init_completion(&comp);
  3104. if (cmpxchg(&hbus->survey_event, NULL, &comp))
  3105. return -ENOTEMPTY;
  3106. memset(&message, 0, sizeof(message));
  3107. message.type = PCI_QUERY_BUS_RELATIONS;
  3108. ret = vmbus_sendpacket(hdev->channel, &message, sizeof(message),
  3109. 0, VM_PKT_DATA_INBAND, 0);
  3110. if (!ret)
  3111. ret = wait_for_response(hdev, &comp);
  3112. /*
  3113. * In the case of fast device addition/removal, it's possible that
  3114. * vmbus_sendpacket() or wait_for_response() returns -ENODEV but we
  3115. * already got a PCI_BUS_RELATIONS* message from the host and the
  3116. * channel callback already scheduled a work to hbus->wq, which can be
  3117. * running pci_devices_present_work() -> survey_child_resources() ->
  3118. * complete(&hbus->survey_event), even after hv_pci_query_relations()
  3119. * exits and the stack variable 'comp' is no longer valid; as a result,
  3120. * a hang or a page fault may happen when the complete() calls
  3121. * raw_spin_lock_irqsave(). Flush hbus->wq before we exit from
  3122. * hv_pci_query_relations() to avoid the issues. Note: if 'ret' is
  3123. * -ENODEV, there can't be any more work item scheduled to hbus->wq
  3124. * after the flush_workqueue(): see vmbus_onoffer_rescind() ->
  3125. * vmbus_reset_channel_cb(), vmbus_rescind_cleanup() ->
  3126. * channel->rescind = true.
  3127. */
  3128. flush_workqueue(hbus->wq);
  3129. return ret;
  3130. }
  3131. /**
  3132. * hv_send_resources_allocated() - Report local resource choices
  3133. * @hdev: VMBus's tracking struct for this root PCI bus
  3134. *
  3135. * The host OS is expecting to be sent a request as a message
  3136. * which contains all the resources that the device will use.
  3137. * The response contains those same resources, "translated"
  3138. * which is to say, the values which should be used by the
  3139. * hardware, when it delivers an interrupt. (MMIO resources are
  3140. * used in local terms.) This is nice for Windows, and lines up
  3141. * with the FDO/PDO split, which doesn't exist in Linux. Linux
  3142. * is deeply expecting to scan an emulated PCI configuration
  3143. * space. So this message is sent here only to drive the state
  3144. * machine on the host forward.
  3145. *
  3146. * Return: 0 on success, -errno on failure
  3147. */
  3148. static int hv_send_resources_allocated(struct hv_device *hdev)
  3149. {
  3150. struct hv_pcibus_device *hbus = hv_get_drvdata(hdev);
  3151. struct pci_resources_assigned *res_assigned;
  3152. struct pci_resources_assigned2 *res_assigned2;
  3153. struct hv_pci_compl comp_pkt;
  3154. struct hv_pci_dev *hpdev;
  3155. struct pci_packet *pkt;
  3156. size_t size_res;
  3157. int wslot;
  3158. int ret;
  3159. size_res = (hbus->protocol_version < PCI_PROTOCOL_VERSION_1_2)
  3160. ? sizeof(*res_assigned) : sizeof(*res_assigned2);
  3161. pkt = kmalloc(sizeof(*pkt) + size_res, GFP_KERNEL);
  3162. if (!pkt)
  3163. return -ENOMEM;
  3164. ret = 0;
  3165. for (wslot = 0; wslot < 256; wslot++) {
  3166. hpdev = get_pcichild_wslot(hbus, wslot);
  3167. if (!hpdev)
  3168. continue;
  3169. memset(pkt, 0, sizeof(*pkt) + size_res);
  3170. init_completion(&comp_pkt.host_event);
  3171. pkt->completion_func = hv_pci_generic_compl;
  3172. pkt->compl_ctxt = &comp_pkt;
  3173. if (hbus->protocol_version < PCI_PROTOCOL_VERSION_1_2) {
  3174. res_assigned =
  3175. (struct pci_resources_assigned *)(pkt + 1);
  3176. res_assigned->message_type.type =
  3177. PCI_RESOURCES_ASSIGNED;
  3178. res_assigned->wslot.slot = hpdev->desc.win_slot.slot;
  3179. } else {
  3180. res_assigned2 =
  3181. (struct pci_resources_assigned2 *)(pkt + 1);
  3182. res_assigned2->message_type.type =
  3183. PCI_RESOURCES_ASSIGNED2;
  3184. res_assigned2->wslot.slot = hpdev->desc.win_slot.slot;
  3185. }
  3186. put_pcichild(hpdev);
  3187. ret = vmbus_sendpacket(hdev->channel, pkt + 1,
  3188. size_res, (unsigned long)pkt,
  3189. VM_PKT_DATA_INBAND,
  3190. VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
  3191. if (!ret)
  3192. ret = wait_for_response(hdev, &comp_pkt.host_event);
  3193. if (ret)
  3194. break;
  3195. if (comp_pkt.completion_status < 0) {
  3196. ret = -EPROTO;
  3197. dev_err(&hdev->device,
  3198. "resource allocated returned 0x%x",
  3199. comp_pkt.completion_status);
  3200. break;
  3201. }
  3202. hbus->wslot_res_allocated = wslot;
  3203. }
  3204. kfree(pkt);
  3205. return ret;
  3206. }
  3207. /**
  3208. * hv_send_resources_released() - Report local resources
  3209. * released
  3210. * @hdev: VMBus's tracking struct for this root PCI bus
  3211. *
  3212. * Return: 0 on success, -errno on failure
  3213. */
  3214. static int hv_send_resources_released(struct hv_device *hdev)
  3215. {
  3216. struct hv_pcibus_device *hbus = hv_get_drvdata(hdev);
  3217. struct pci_child_message pkt;
  3218. struct hv_pci_dev *hpdev;
  3219. int wslot;
  3220. int ret;
  3221. for (wslot = hbus->wslot_res_allocated; wslot >= 0; wslot--) {
  3222. hpdev = get_pcichild_wslot(hbus, wslot);
  3223. if (!hpdev)
  3224. continue;
  3225. memset(&pkt, 0, sizeof(pkt));
  3226. pkt.message_type.type = PCI_RESOURCES_RELEASED;
  3227. pkt.wslot.slot = hpdev->desc.win_slot.slot;
  3228. put_pcichild(hpdev);
  3229. ret = vmbus_sendpacket(hdev->channel, &pkt, sizeof(pkt), 0,
  3230. VM_PKT_DATA_INBAND, 0);
  3231. if (ret)
  3232. return ret;
  3233. hbus->wslot_res_allocated = wslot - 1;
  3234. }
  3235. hbus->wslot_res_allocated = -1;
  3236. return 0;
  3237. }
  3238. /**
  3239. * hv_pci_probe() - New VMBus channel probe, for a root PCI bus
  3240. * @hdev: VMBus's tracking struct for this root PCI bus
  3241. * @dev_id: Identifies the device itself
  3242. *
  3243. * Return: 0 on success, -errno on failure
  3244. */
  3245. static int hv_pci_probe(struct hv_device *hdev,
  3246. const struct hv_vmbus_device_id *dev_id)
  3247. {
  3248. struct pci_host_bridge *bridge;
  3249. struct hv_pcibus_device *hbus;
  3250. int ret, dom;
  3251. u16 dom_req;
  3252. char *name;
  3253. bridge = devm_pci_alloc_host_bridge(&hdev->device, 0);
  3254. if (!bridge)
  3255. return -ENOMEM;
  3256. hbus = kzalloc_obj(*hbus);
  3257. if (!hbus)
  3258. return -ENOMEM;
  3259. hbus->bridge = bridge;
  3260. mutex_init(&hbus->state_lock);
  3261. hbus->state = hv_pcibus_init;
  3262. hbus->wslot_res_allocated = -1;
  3263. /*
  3264. * The PCI bus "domain" is what is called "segment" in ACPI and other
  3265. * specs. Pull it from the instance ID, to get something usually
  3266. * unique. In rare cases of collision, we will find out another number
  3267. * not in use.
  3268. *
  3269. * Note that, since this code only runs in a Hyper-V VM, Hyper-V
  3270. * together with this guest driver can guarantee that (1) The only
  3271. * domain used by Gen1 VMs for something that looks like a physical
  3272. * PCI bus (which is actually emulated by the hypervisor) is domain 0.
  3273. * (2) There will be no overlap between domains (after fixing possible
  3274. * collisions) in the same VM.
  3275. *
  3276. * Because Gen1 VMs use domain 0, don't allow picking domain 0 here,
  3277. * even if bytes 4 and 5 of the instance GUID are both zero. For wider
  3278. * userspace compatibility, limit the domain ID to a 16-bit value.
  3279. */
  3280. dom_req = hdev->dev_instance.b[5] << 8 | hdev->dev_instance.b[4];
  3281. dom = pci_bus_find_emul_domain_nr(dom_req, 1, U16_MAX);
  3282. if (dom < 0) {
  3283. dev_err(&hdev->device,
  3284. "Unable to use dom# 0x%x or other numbers", dom_req);
  3285. ret = -EINVAL;
  3286. goto free_bus;
  3287. }
  3288. if (dom != dom_req)
  3289. dev_info(&hdev->device,
  3290. "PCI dom# 0x%x has collision, using 0x%x",
  3291. dom_req, dom);
  3292. hbus->bridge->domain_nr = dom;
  3293. #ifdef CONFIG_X86
  3294. hbus->sysdata.domain = dom;
  3295. hbus->use_calls = !!(ms_hyperv.hints & HV_X64_USE_MMIO_HYPERCALLS);
  3296. #elif defined(CONFIG_ARM64)
  3297. /*
  3298. * Set the PCI bus parent to be the corresponding VMbus
  3299. * device. Then the VMbus device will be assigned as the
  3300. * ACPI companion in pcibios_root_bridge_prepare() and
  3301. * pci_dma_configure() will propagate device coherence
  3302. * information to devices created on the bus.
  3303. */
  3304. hbus->sysdata.parent = hdev->device.parent;
  3305. hbus->use_calls = false;
  3306. #endif
  3307. hbus->hdev = hdev;
  3308. INIT_LIST_HEAD(&hbus->children);
  3309. INIT_LIST_HEAD(&hbus->dr_list);
  3310. spin_lock_init(&hbus->config_lock);
  3311. spin_lock_init(&hbus->device_list_lock);
  3312. hbus->wq = alloc_ordered_workqueue("hv_pci_%x", 0,
  3313. hbus->bridge->domain_nr);
  3314. if (!hbus->wq) {
  3315. ret = -ENOMEM;
  3316. goto free_bus;
  3317. }
  3318. hdev->channel->next_request_id_callback = vmbus_next_request_id;
  3319. hdev->channel->request_addr_callback = vmbus_request_addr;
  3320. hdev->channel->rqstor_size = HV_PCI_RQSTOR_SIZE;
  3321. ret = vmbus_open(hdev->channel, pci_ring_size, pci_ring_size, NULL, 0,
  3322. hv_pci_onchannelcallback, hbus);
  3323. if (ret)
  3324. goto destroy_wq;
  3325. hv_set_drvdata(hdev, hbus);
  3326. ret = hv_pci_protocol_negotiation(hdev, pci_protocol_versions,
  3327. ARRAY_SIZE(pci_protocol_versions));
  3328. if (ret)
  3329. goto close;
  3330. ret = hv_allocate_config_window(hbus);
  3331. if (ret)
  3332. goto close;
  3333. hbus->cfg_addr = ioremap(hbus->mem_config->start,
  3334. PCI_CONFIG_MMIO_LENGTH);
  3335. if (!hbus->cfg_addr) {
  3336. dev_err(&hdev->device,
  3337. "Unable to map a virtual address for config space\n");
  3338. ret = -ENOMEM;
  3339. goto free_config;
  3340. }
  3341. name = kasprintf(GFP_KERNEL, "%pUL", &hdev->dev_instance);
  3342. if (!name) {
  3343. ret = -ENOMEM;
  3344. goto unmap;
  3345. }
  3346. hbus->fwnode = irq_domain_alloc_named_fwnode(name);
  3347. kfree(name);
  3348. if (!hbus->fwnode) {
  3349. ret = -ENOMEM;
  3350. goto unmap;
  3351. }
  3352. ret = hv_pcie_init_irq_domain(hbus);
  3353. if (ret)
  3354. goto free_fwnode;
  3355. ret = hv_pci_query_relations(hdev);
  3356. if (ret)
  3357. goto free_irq_domain;
  3358. mutex_lock(&hbus->state_lock);
  3359. ret = hv_pci_enter_d0(hdev);
  3360. if (ret)
  3361. goto release_state_lock;
  3362. ret = hv_pci_allocate_bridge_windows(hbus);
  3363. if (ret)
  3364. goto exit_d0;
  3365. ret = hv_send_resources_allocated(hdev);
  3366. if (ret)
  3367. goto free_windows;
  3368. prepopulate_bars(hbus);
  3369. hbus->state = hv_pcibus_probed;
  3370. ret = create_root_hv_pci_bus(hbus);
  3371. if (ret)
  3372. goto free_windows;
  3373. mutex_unlock(&hbus->state_lock);
  3374. return 0;
  3375. free_windows:
  3376. hv_pci_free_bridge_windows(hbus);
  3377. exit_d0:
  3378. (void) hv_pci_bus_exit(hdev, true);
  3379. release_state_lock:
  3380. mutex_unlock(&hbus->state_lock);
  3381. free_irq_domain:
  3382. irq_domain_remove(hbus->irq_domain);
  3383. free_fwnode:
  3384. irq_domain_free_fwnode(hbus->fwnode);
  3385. unmap:
  3386. iounmap(hbus->cfg_addr);
  3387. free_config:
  3388. hv_free_config_window(hbus);
  3389. close:
  3390. vmbus_close(hdev->channel);
  3391. destroy_wq:
  3392. destroy_workqueue(hbus->wq);
  3393. free_bus:
  3394. kfree(hbus);
  3395. return ret;
  3396. }
  3397. static int hv_pci_bus_exit(struct hv_device *hdev, bool keep_devs)
  3398. {
  3399. struct hv_pcibus_device *hbus = hv_get_drvdata(hdev);
  3400. struct vmbus_channel *chan = hdev->channel;
  3401. struct {
  3402. struct pci_packet teardown_packet;
  3403. u8 buffer[sizeof(struct pci_message)];
  3404. } pkt;
  3405. struct pci_message *msg;
  3406. struct hv_pci_compl comp_pkt;
  3407. struct hv_pci_dev *hpdev, *tmp;
  3408. unsigned long flags;
  3409. u64 trans_id;
  3410. int ret;
  3411. /*
  3412. * After the host sends the RESCIND_CHANNEL message, it doesn't
  3413. * access the per-channel ringbuffer any longer.
  3414. */
  3415. if (chan->rescind)
  3416. return 0;
  3417. if (!keep_devs) {
  3418. struct list_head removed;
  3419. /* Move all present children to the list on stack */
  3420. INIT_LIST_HEAD(&removed);
  3421. spin_lock_irqsave(&hbus->device_list_lock, flags);
  3422. list_for_each_entry_safe(hpdev, tmp, &hbus->children, list_entry)
  3423. list_move_tail(&hpdev->list_entry, &removed);
  3424. spin_unlock_irqrestore(&hbus->device_list_lock, flags);
  3425. /* Remove all children in the list */
  3426. list_for_each_entry_safe(hpdev, tmp, &removed, list_entry) {
  3427. list_del(&hpdev->list_entry);
  3428. if (hpdev->pci_slot)
  3429. pci_destroy_slot(hpdev->pci_slot);
  3430. /* For the two refs got in new_pcichild_device() */
  3431. put_pcichild(hpdev);
  3432. put_pcichild(hpdev);
  3433. }
  3434. }
  3435. ret = hv_send_resources_released(hdev);
  3436. if (ret) {
  3437. dev_err(&hdev->device,
  3438. "Couldn't send resources released packet(s)\n");
  3439. return ret;
  3440. }
  3441. memset(&pkt.teardown_packet, 0, sizeof(pkt.teardown_packet));
  3442. init_completion(&comp_pkt.host_event);
  3443. pkt.teardown_packet.completion_func = hv_pci_generic_compl;
  3444. pkt.teardown_packet.compl_ctxt = &comp_pkt;
  3445. msg = (struct pci_message *)pkt.buffer;
  3446. msg->type = PCI_BUS_D0EXIT;
  3447. ret = vmbus_sendpacket_getid(chan, msg, sizeof(*msg),
  3448. (unsigned long)&pkt.teardown_packet,
  3449. &trans_id, VM_PKT_DATA_INBAND,
  3450. VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
  3451. if (ret)
  3452. return ret;
  3453. if (wait_for_completion_timeout(&comp_pkt.host_event, 10 * HZ) == 0) {
  3454. /*
  3455. * The completion packet on the stack becomes invalid after
  3456. * 'return'; remove the ID from the VMbus requestor if the
  3457. * identifier is still mapped to/associated with the packet.
  3458. *
  3459. * Cf. hv_pci_onchannelcallback().
  3460. */
  3461. vmbus_request_addr_match(chan, trans_id,
  3462. (unsigned long)&pkt.teardown_packet);
  3463. return -ETIMEDOUT;
  3464. }
  3465. return 0;
  3466. }
  3467. /**
  3468. * hv_pci_remove() - Remove routine for this VMBus channel
  3469. * @hdev: VMBus's tracking struct for this root PCI bus
  3470. */
  3471. static void hv_pci_remove(struct hv_device *hdev)
  3472. {
  3473. struct hv_pcibus_device *hbus;
  3474. hbus = hv_get_drvdata(hdev);
  3475. if (hbus->state == hv_pcibus_installed) {
  3476. tasklet_disable(&hdev->channel->callback_event);
  3477. hbus->state = hv_pcibus_removing;
  3478. tasklet_enable(&hdev->channel->callback_event);
  3479. destroy_workqueue(hbus->wq);
  3480. hbus->wq = NULL;
  3481. /*
  3482. * At this point, no work is running or can be scheduled
  3483. * on hbus-wq. We can't race with hv_pci_devices_present()
  3484. * or hv_pci_eject_device(), it's safe to proceed.
  3485. */
  3486. /* Remove the bus from PCI's point of view. */
  3487. pci_lock_rescan_remove();
  3488. pci_stop_root_bus(hbus->bridge->bus);
  3489. hv_pci_remove_slots(hbus);
  3490. pci_remove_root_bus(hbus->bridge->bus);
  3491. pci_unlock_rescan_remove();
  3492. }
  3493. hv_pci_bus_exit(hdev, false);
  3494. vmbus_close(hdev->channel);
  3495. iounmap(hbus->cfg_addr);
  3496. hv_free_config_window(hbus);
  3497. hv_pci_free_bridge_windows(hbus);
  3498. irq_domain_remove(hbus->irq_domain);
  3499. irq_domain_free_fwnode(hbus->fwnode);
  3500. kfree(hbus);
  3501. }
  3502. static int hv_pci_suspend(struct hv_device *hdev)
  3503. {
  3504. struct hv_pcibus_device *hbus = hv_get_drvdata(hdev);
  3505. enum hv_pcibus_state old_state;
  3506. int ret;
  3507. /*
  3508. * hv_pci_suspend() must make sure there are no pending work items
  3509. * before calling vmbus_close(), since it runs in a process context
  3510. * as a callback in dpm_suspend(). When it starts to run, the channel
  3511. * callback hv_pci_onchannelcallback(), which runs in a tasklet
  3512. * context, can be still running concurrently and scheduling new work
  3513. * items onto hbus->wq in hv_pci_devices_present() and
  3514. * hv_pci_eject_device(), and the work item handlers can access the
  3515. * vmbus channel, which can be being closed by hv_pci_suspend(), e.g.
  3516. * the work item handler pci_devices_present_work() ->
  3517. * new_pcichild_device() writes to the vmbus channel.
  3518. *
  3519. * To eliminate the race, hv_pci_suspend() disables the channel
  3520. * callback tasklet, sets hbus->state to hv_pcibus_removing, and
  3521. * re-enables the tasklet. This way, when hv_pci_suspend() proceeds,
  3522. * it knows that no new work item can be scheduled, and then it flushes
  3523. * hbus->wq and safely closes the vmbus channel.
  3524. */
  3525. tasklet_disable(&hdev->channel->callback_event);
  3526. /* Change the hbus state to prevent new work items. */
  3527. old_state = hbus->state;
  3528. if (hbus->state == hv_pcibus_installed)
  3529. hbus->state = hv_pcibus_removing;
  3530. tasklet_enable(&hdev->channel->callback_event);
  3531. if (old_state != hv_pcibus_installed)
  3532. return -EINVAL;
  3533. flush_workqueue(hbus->wq);
  3534. ret = hv_pci_bus_exit(hdev, true);
  3535. if (ret)
  3536. return ret;
  3537. vmbus_close(hdev->channel);
  3538. return 0;
  3539. }
  3540. static int hv_pci_restore_msi_msg(struct pci_dev *pdev, void *arg)
  3541. {
  3542. struct irq_data *irq_data;
  3543. struct msi_desc *entry;
  3544. if (!pdev->msi_enabled && !pdev->msix_enabled)
  3545. return 0;
  3546. guard(msi_descs_lock)(&pdev->dev);
  3547. msi_for_each_desc(entry, &pdev->dev, MSI_DESC_ASSOCIATED) {
  3548. irq_data = irq_get_irq_data(entry->irq);
  3549. if (WARN_ON_ONCE(!irq_data))
  3550. return -EINVAL;
  3551. hv_compose_msi_msg(irq_data, &entry->msg);
  3552. }
  3553. return 0;
  3554. }
  3555. /*
  3556. * Upon resume, pci_restore_msi_state() -> ... -> __pci_write_msi_msg()
  3557. * directly writes the MSI/MSI-X registers via MMIO, but since Hyper-V
  3558. * doesn't trap and emulate the MMIO accesses, here hv_compose_msi_msg()
  3559. * must be used to ask Hyper-V to re-create the IOMMU Interrupt Remapping
  3560. * Table entries.
  3561. */
  3562. static void hv_pci_restore_msi_state(struct hv_pcibus_device *hbus)
  3563. {
  3564. pci_walk_bus(hbus->bridge->bus, hv_pci_restore_msi_msg, NULL);
  3565. }
  3566. static int hv_pci_resume(struct hv_device *hdev)
  3567. {
  3568. struct hv_pcibus_device *hbus = hv_get_drvdata(hdev);
  3569. enum pci_protocol_version_t version[1];
  3570. int ret;
  3571. hbus->state = hv_pcibus_init;
  3572. hdev->channel->next_request_id_callback = vmbus_next_request_id;
  3573. hdev->channel->request_addr_callback = vmbus_request_addr;
  3574. hdev->channel->rqstor_size = HV_PCI_RQSTOR_SIZE;
  3575. ret = vmbus_open(hdev->channel, pci_ring_size, pci_ring_size, NULL, 0,
  3576. hv_pci_onchannelcallback, hbus);
  3577. if (ret)
  3578. return ret;
  3579. /* Only use the version that was in use before hibernation. */
  3580. version[0] = hbus->protocol_version;
  3581. ret = hv_pci_protocol_negotiation(hdev, version, 1);
  3582. if (ret)
  3583. goto out;
  3584. ret = hv_pci_query_relations(hdev);
  3585. if (ret)
  3586. goto out;
  3587. mutex_lock(&hbus->state_lock);
  3588. ret = hv_pci_enter_d0(hdev);
  3589. if (ret)
  3590. goto release_state_lock;
  3591. ret = hv_send_resources_allocated(hdev);
  3592. if (ret)
  3593. goto release_state_lock;
  3594. prepopulate_bars(hbus);
  3595. hv_pci_restore_msi_state(hbus);
  3596. hbus->state = hv_pcibus_installed;
  3597. mutex_unlock(&hbus->state_lock);
  3598. return 0;
  3599. release_state_lock:
  3600. mutex_unlock(&hbus->state_lock);
  3601. out:
  3602. vmbus_close(hdev->channel);
  3603. return ret;
  3604. }
  3605. static const struct hv_vmbus_device_id hv_pci_id_table[] = {
  3606. /* PCI Pass-through Class ID */
  3607. /* 44C4F61D-4444-4400-9D52-802E27EDE19F */
  3608. { HV_PCIE_GUID, },
  3609. { },
  3610. };
  3611. MODULE_DEVICE_TABLE(vmbus, hv_pci_id_table);
  3612. static struct hv_driver hv_pci_drv = {
  3613. .name = "hv_pci",
  3614. .id_table = hv_pci_id_table,
  3615. .probe = hv_pci_probe,
  3616. .remove = hv_pci_remove,
  3617. .suspend = hv_pci_suspend,
  3618. .resume = hv_pci_resume,
  3619. };
  3620. static void __exit exit_hv_pci_drv(void)
  3621. {
  3622. vmbus_driver_unregister(&hv_pci_drv);
  3623. hvpci_block_ops.read_block = NULL;
  3624. hvpci_block_ops.write_block = NULL;
  3625. hvpci_block_ops.reg_blk_invalidate = NULL;
  3626. }
  3627. static int __init init_hv_pci_drv(void)
  3628. {
  3629. int ret;
  3630. if (!hv_is_hyperv_initialized())
  3631. return -ENODEV;
  3632. if (hv_root_partition() && !hv_nested)
  3633. return -ENODEV;
  3634. ret = hv_pci_irqchip_init();
  3635. if (ret)
  3636. return ret;
  3637. /* Initialize PCI block r/w interface */
  3638. hvpci_block_ops.read_block = hv_read_config_block;
  3639. hvpci_block_ops.write_block = hv_write_config_block;
  3640. hvpci_block_ops.reg_blk_invalidate = hv_register_block_invalidate;
  3641. return vmbus_driver_register(&hv_pci_drv);
  3642. }
  3643. module_init(init_hv_pci_drv);
  3644. module_exit(exit_hv_pci_drv);
  3645. MODULE_DESCRIPTION("Hyper-V PCI");
  3646. MODULE_LICENSE("GPL v2");