pci-hyperv.c 65 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364
  1. /*
  2. * Copyright (c) Microsoft Corporation.
  3. *
  4. * Author:
  5. * Jake Oshins <jakeo@microsoft.com>
  6. *
  7. * This driver acts as a paravirtual front-end for PCI Express root buses.
  8. * When a PCI Express function (either an entire device or an SR-IOV
  9. * Virtual Function) is being passed through to the VM, this driver exposes
  10. * a new bus to the guest VM. This is modeled as a root PCI bus because
  11. * no bridges are being exposed to the VM. In fact, with a "Generation 2"
  12. * VM within Hyper-V, there may seem to be no PCI bus at all in the VM
  13. * until a device as been exposed using this driver.
  14. *
  15. * Each root PCI bus has its own PCI domain, which is called "Segment" in
  16. * the PCI Firmware Specifications. Thus while each device passed through
  17. * to the VM using this front-end will appear at "device 0", the domain will
  18. * be unique. Typically, each bus will have one PCI function on it, though
  19. * this driver does support more than one.
  20. *
  21. * In order to map the interrupts from the device through to the guest VM,
  22. * this driver also implements an IRQ Domain, which handles interrupts (either
  23. * MSI or MSI-X) associated with the functions on the bus. As interrupts are
  24. * set up, torn down, or reaffined, this driver communicates with the
  25. * underlying hypervisor to adjust the mappings in the I/O MMU so that each
  26. * interrupt will be delivered to the correct virtual processor at the right
  27. * vector. This driver does not support level-triggered (line-based)
  28. * interrupts, and will report that the Interrupt Line register in the
  29. * function's configuration space is zero.
  30. *
  31. * The rest of this driver mostly maps PCI concepts onto underlying Hyper-V
  32. * facilities. For instance, the configuration space of a function exposed
  33. * by Hyper-V is mapped into a single page of memory space, and the
  34. * read and write handlers for config space must be aware of this mechanism.
  35. * Similarly, device setup and teardown involves messages sent to and from
  36. * the PCI back-end driver in Hyper-V.
  37. *
  38. * This program is free software; you can redistribute it and/or modify it
  39. * under the terms of the GNU General Public License version 2 as published
  40. * by the Free Software Foundation.
  41. *
  42. * This program is distributed in the hope that it will be useful, but
  43. * WITHOUT ANY WARRANTY; without even the implied warranty of
  44. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  45. * NON INFRINGEMENT. See the GNU General Public License for more
  46. * details.
  47. *
  48. */
  49. #include <linux/kernel.h>
  50. #include <linux/module.h>
  51. #include <linux/pci.h>
  52. #include <linux/semaphore.h>
  53. #include <linux/irqdomain.h>
  54. #include <asm/irqdomain.h>
  55. #include <asm/apic.h>
  56. #include <linux/msi.h>
  57. #include <linux/hyperv.h>
  58. #include <asm/mshyperv.h>
  59. /*
  60. * Protocol versions. The low word is the minor version, the high word the
  61. * major version.
  62. */
  63. #define PCI_MAKE_VERSION(major, minor) ((u32)(((major) << 16) | (major)))
  64. #define PCI_MAJOR_VERSION(version) ((u32)(version) >> 16)
  65. #define PCI_MINOR_VERSION(version) ((u32)(version) & 0xff)
  66. enum {
  67. PCI_PROTOCOL_VERSION_1_1 = PCI_MAKE_VERSION(1, 1),
  68. PCI_PROTOCOL_VERSION_CURRENT = PCI_PROTOCOL_VERSION_1_1
  69. };
  70. #define PCI_CONFIG_MMIO_LENGTH 0x2000
  71. #define CFG_PAGE_OFFSET 0x1000
  72. #define CFG_PAGE_SIZE (PCI_CONFIG_MMIO_LENGTH - CFG_PAGE_OFFSET)
  73. #define MAX_SUPPORTED_MSI_MESSAGES 0x400
  74. /*
  75. * Message Types
  76. */
  77. enum pci_message_type {
  78. /*
  79. * Version 1.1
  80. */
  81. PCI_MESSAGE_BASE = 0x42490000,
  82. PCI_BUS_RELATIONS = PCI_MESSAGE_BASE + 0,
  83. PCI_QUERY_BUS_RELATIONS = PCI_MESSAGE_BASE + 1,
  84. PCI_POWER_STATE_CHANGE = PCI_MESSAGE_BASE + 4,
  85. PCI_QUERY_RESOURCE_REQUIREMENTS = PCI_MESSAGE_BASE + 5,
  86. PCI_QUERY_RESOURCE_RESOURCES = PCI_MESSAGE_BASE + 6,
  87. PCI_BUS_D0ENTRY = PCI_MESSAGE_BASE + 7,
  88. PCI_BUS_D0EXIT = PCI_MESSAGE_BASE + 8,
  89. PCI_READ_BLOCK = PCI_MESSAGE_BASE + 9,
  90. PCI_WRITE_BLOCK = PCI_MESSAGE_BASE + 0xA,
  91. PCI_EJECT = PCI_MESSAGE_BASE + 0xB,
  92. PCI_QUERY_STOP = PCI_MESSAGE_BASE + 0xC,
  93. PCI_REENABLE = PCI_MESSAGE_BASE + 0xD,
  94. PCI_QUERY_STOP_FAILED = PCI_MESSAGE_BASE + 0xE,
  95. PCI_EJECTION_COMPLETE = PCI_MESSAGE_BASE + 0xF,
  96. PCI_RESOURCES_ASSIGNED = PCI_MESSAGE_BASE + 0x10,
  97. PCI_RESOURCES_RELEASED = PCI_MESSAGE_BASE + 0x11,
  98. PCI_INVALIDATE_BLOCK = PCI_MESSAGE_BASE + 0x12,
  99. PCI_QUERY_PROTOCOL_VERSION = PCI_MESSAGE_BASE + 0x13,
  100. PCI_CREATE_INTERRUPT_MESSAGE = PCI_MESSAGE_BASE + 0x14,
  101. PCI_DELETE_INTERRUPT_MESSAGE = PCI_MESSAGE_BASE + 0x15,
  102. PCI_MESSAGE_MAXIMUM
  103. };
  104. /*
  105. * Structures defining the virtual PCI Express protocol.
  106. */
  107. union pci_version {
  108. struct {
  109. u16 minor_version;
  110. u16 major_version;
  111. } parts;
  112. u32 version;
  113. } __packed;
  114. /*
  115. * Function numbers are 8-bits wide on Express, as interpreted through ARI,
  116. * which is all this driver does. This representation is the one used in
  117. * Windows, which is what is expected when sending this back and forth with
  118. * the Hyper-V parent partition.
  119. */
  120. union win_slot_encoding {
  121. struct {
  122. u32 func:8;
  123. u32 reserved:24;
  124. } bits;
  125. u32 slot;
  126. } __packed;
  127. /*
  128. * Pretty much as defined in the PCI Specifications.
  129. */
  130. struct pci_function_description {
  131. u16 v_id; /* vendor ID */
  132. u16 d_id; /* device ID */
  133. u8 rev;
  134. u8 prog_intf;
  135. u8 subclass;
  136. u8 base_class;
  137. u32 subsystem_id;
  138. union win_slot_encoding win_slot;
  139. u32 ser; /* serial number */
  140. } __packed;
  141. /**
  142. * struct hv_msi_desc
  143. * @vector: IDT entry
  144. * @delivery_mode: As defined in Intel's Programmer's
  145. * Reference Manual, Volume 3, Chapter 8.
  146. * @vector_count: Number of contiguous entries in the
  147. * Interrupt Descriptor Table that are
  148. * occupied by this Message-Signaled
  149. * Interrupt. For "MSI", as first defined
  150. * in PCI 2.2, this can be between 1 and
  151. * 32. For "MSI-X," as first defined in PCI
  152. * 3.0, this must be 1, as each MSI-X table
  153. * entry would have its own descriptor.
  154. * @reserved: Empty space
  155. * @cpu_mask: All the target virtual processors.
  156. */
  157. struct hv_msi_desc {
  158. u8 vector;
  159. u8 delivery_mode;
  160. u16 vector_count;
  161. u32 reserved;
  162. u64 cpu_mask;
  163. } __packed;
  164. /**
  165. * struct tran_int_desc
  166. * @reserved: unused, padding
  167. * @vector_count: same as in hv_msi_desc
  168. * @data: This is the "data payload" value that is
  169. * written by the device when it generates
  170. * a message-signaled interrupt, either MSI
  171. * or MSI-X.
  172. * @address: This is the address to which the data
  173. * payload is written on interrupt
  174. * generation.
  175. */
  176. struct tran_int_desc {
  177. u16 reserved;
  178. u16 vector_count;
  179. u32 data;
  180. u64 address;
  181. } __packed;
  182. /*
  183. * A generic message format for virtual PCI.
  184. * Specific message formats are defined later in the file.
  185. */
  186. struct pci_message {
  187. u32 message_type;
  188. } __packed;
  189. struct pci_child_message {
  190. u32 message_type;
  191. union win_slot_encoding wslot;
  192. } __packed;
  193. struct pci_incoming_message {
  194. struct vmpacket_descriptor hdr;
  195. struct pci_message message_type;
  196. } __packed;
  197. struct pci_response {
  198. struct vmpacket_descriptor hdr;
  199. s32 status; /* negative values are failures */
  200. } __packed;
  201. struct pci_packet {
  202. void (*completion_func)(void *context, struct pci_response *resp,
  203. int resp_packet_size);
  204. void *compl_ctxt;
  205. struct pci_message message;
  206. };
  207. /*
  208. * Specific message types supporting the PCI protocol.
  209. */
  210. /*
  211. * Version negotiation message. Sent from the guest to the host.
  212. * The guest is free to try different versions until the host
  213. * accepts the version.
  214. *
  215. * pci_version: The protocol version requested.
  216. * is_last_attempt: If TRUE, this is the last version guest will request.
  217. * reservedz: Reserved field, set to zero.
  218. */
  219. struct pci_version_request {
  220. struct pci_message message_type;
  221. enum pci_message_type protocol_version;
  222. } __packed;
  223. /*
  224. * Bus D0 Entry. This is sent from the guest to the host when the virtual
  225. * bus (PCI Express port) is ready for action.
  226. */
  227. struct pci_bus_d0_entry {
  228. struct pci_message message_type;
  229. u32 reserved;
  230. u64 mmio_base;
  231. } __packed;
  232. struct pci_bus_relations {
  233. struct pci_incoming_message incoming;
  234. u32 device_count;
  235. struct pci_function_description func[1];
  236. } __packed;
  237. struct pci_q_res_req_response {
  238. struct vmpacket_descriptor hdr;
  239. s32 status; /* negative values are failures */
  240. u32 probed_bar[6];
  241. } __packed;
  242. struct pci_set_power {
  243. struct pci_message message_type;
  244. union win_slot_encoding wslot;
  245. u32 power_state; /* In Windows terms */
  246. u32 reserved;
  247. } __packed;
  248. struct pci_set_power_response {
  249. struct vmpacket_descriptor hdr;
  250. s32 status; /* negative values are failures */
  251. union win_slot_encoding wslot;
  252. u32 resultant_state; /* In Windows terms */
  253. u32 reserved;
  254. } __packed;
  255. struct pci_resources_assigned {
  256. struct pci_message message_type;
  257. union win_slot_encoding wslot;
  258. u8 memory_range[0x14][6]; /* not used here */
  259. u32 msi_descriptors;
  260. u32 reserved[4];
  261. } __packed;
  262. struct pci_create_interrupt {
  263. struct pci_message message_type;
  264. union win_slot_encoding wslot;
  265. struct hv_msi_desc int_desc;
  266. } __packed;
  267. struct pci_create_int_response {
  268. struct pci_response response;
  269. u32 reserved;
  270. struct tran_int_desc int_desc;
  271. } __packed;
  272. struct pci_delete_interrupt {
  273. struct pci_message message_type;
  274. union win_slot_encoding wslot;
  275. struct tran_int_desc int_desc;
  276. } __packed;
  277. struct pci_dev_incoming {
  278. struct pci_incoming_message incoming;
  279. union win_slot_encoding wslot;
  280. } __packed;
  281. struct pci_eject_response {
  282. u32 message_type;
  283. union win_slot_encoding wslot;
  284. u32 status;
  285. } __packed;
  286. static int pci_ring_size = (4 * PAGE_SIZE);
  287. /*
  288. * Definitions or interrupt steering hypercall.
  289. */
  290. #define HV_PARTITION_ID_SELF ((u64)-1)
  291. #define HVCALL_RETARGET_INTERRUPT 0x7e
  292. struct retarget_msi_interrupt {
  293. u64 partition_id; /* use "self" */
  294. u64 device_id;
  295. u32 source; /* 1 for MSI(-X) */
  296. u32 reserved1;
  297. u32 address;
  298. u32 data;
  299. u64 reserved2;
  300. u32 vector;
  301. u32 flags;
  302. u64 vp_mask;
  303. } __packed;
  304. /*
  305. * Driver specific state.
  306. */
  307. enum hv_pcibus_state {
  308. hv_pcibus_init = 0,
  309. hv_pcibus_probed,
  310. hv_pcibus_installed,
  311. hv_pcibus_maximum
  312. };
  313. struct hv_pcibus_device {
  314. struct pci_sysdata sysdata;
  315. enum hv_pcibus_state state;
  316. atomic_t remove_lock;
  317. struct hv_device *hdev;
  318. resource_size_t low_mmio_space;
  319. resource_size_t high_mmio_space;
  320. struct resource *mem_config;
  321. struct resource *low_mmio_res;
  322. struct resource *high_mmio_res;
  323. struct completion *survey_event;
  324. struct completion remove_event;
  325. struct pci_bus *pci_bus;
  326. spinlock_t config_lock; /* Avoid two threads writing index page */
  327. spinlock_t device_list_lock; /* Protect lists below */
  328. void __iomem *cfg_addr;
  329. struct semaphore enum_sem;
  330. struct list_head resources_for_children;
  331. struct list_head children;
  332. struct list_head dr_list;
  333. struct work_struct wrk;
  334. struct msi_domain_info msi_info;
  335. struct msi_controller msi_chip;
  336. struct irq_domain *irq_domain;
  337. };
  338. /*
  339. * Tracks "Device Relations" messages from the host, which must be both
  340. * processed in order and deferred so that they don't run in the context
  341. * of the incoming packet callback.
  342. */
  343. struct hv_dr_work {
  344. struct work_struct wrk;
  345. struct hv_pcibus_device *bus;
  346. };
  347. struct hv_dr_state {
  348. struct list_head list_entry;
  349. u32 device_count;
  350. struct pci_function_description func[1];
  351. };
  352. enum hv_pcichild_state {
  353. hv_pcichild_init = 0,
  354. hv_pcichild_requirements,
  355. hv_pcichild_resourced,
  356. hv_pcichild_ejecting,
  357. hv_pcichild_maximum
  358. };
  359. enum hv_pcidev_ref_reason {
  360. hv_pcidev_ref_invalid = 0,
  361. hv_pcidev_ref_initial,
  362. hv_pcidev_ref_by_slot,
  363. hv_pcidev_ref_packet,
  364. hv_pcidev_ref_pnp,
  365. hv_pcidev_ref_childlist,
  366. hv_pcidev_irqdata,
  367. hv_pcidev_ref_max
  368. };
  369. struct hv_pci_dev {
  370. /* List protected by pci_rescan_remove_lock */
  371. struct list_head list_entry;
  372. atomic_t refs;
  373. enum hv_pcichild_state state;
  374. struct pci_function_description desc;
  375. bool reported_missing;
  376. struct hv_pcibus_device *hbus;
  377. struct work_struct wrk;
  378. /*
  379. * What would be observed if one wrote 0xFFFFFFFF to a BAR and then
  380. * read it back, for each of the BAR offsets within config space.
  381. */
  382. u32 probed_bar[6];
  383. };
  384. struct hv_pci_compl {
  385. struct completion host_event;
  386. s32 completion_status;
  387. };
  388. /**
  389. * hv_pci_generic_compl() - Invoked for a completion packet
  390. * @context: Set up by the sender of the packet.
  391. * @resp: The response packet
  392. * @resp_packet_size: Size in bytes of the packet
  393. *
  394. * This function is used to trigger an event and report status
  395. * for any message for which the completion packet contains a
  396. * status and nothing else.
  397. */
  398. static
  399. void
  400. hv_pci_generic_compl(void *context, struct pci_response *resp,
  401. int resp_packet_size)
  402. {
  403. struct hv_pci_compl *comp_pkt = context;
  404. if (resp_packet_size >= offsetofend(struct pci_response, status))
  405. comp_pkt->completion_status = resp->status;
  406. complete(&comp_pkt->host_event);
  407. }
  408. static struct hv_pci_dev *get_pcichild_wslot(struct hv_pcibus_device *hbus,
  409. u32 wslot);
  410. static void get_pcichild(struct hv_pci_dev *hv_pcidev,
  411. enum hv_pcidev_ref_reason reason);
  412. static void put_pcichild(struct hv_pci_dev *hv_pcidev,
  413. enum hv_pcidev_ref_reason reason);
  414. static void get_hvpcibus(struct hv_pcibus_device *hv_pcibus);
  415. static void put_hvpcibus(struct hv_pcibus_device *hv_pcibus);
  416. /**
  417. * devfn_to_wslot() - Convert from Linux PCI slot to Windows
  418. * @devfn: The Linux representation of PCI slot
  419. *
  420. * Windows uses a slightly different representation of PCI slot.
  421. *
  422. * Return: The Windows representation
  423. */
  424. static u32 devfn_to_wslot(int devfn)
  425. {
  426. union win_slot_encoding wslot;
  427. wslot.slot = 0;
  428. wslot.bits.func = PCI_SLOT(devfn) | (PCI_FUNC(devfn) << 5);
  429. return wslot.slot;
  430. }
  431. /**
  432. * wslot_to_devfn() - Convert from Windows PCI slot to Linux
  433. * @wslot: The Windows representation of PCI slot
  434. *
  435. * Windows uses a slightly different representation of PCI slot.
  436. *
  437. * Return: The Linux representation
  438. */
  439. static int wslot_to_devfn(u32 wslot)
  440. {
  441. union win_slot_encoding slot_no;
  442. slot_no.slot = wslot;
  443. return PCI_DEVFN(0, slot_no.bits.func);
  444. }
  445. /*
  446. * PCI Configuration Space for these root PCI buses is implemented as a pair
  447. * of pages in memory-mapped I/O space. Writing to the first page chooses
  448. * the PCI function being written or read. Once the first page has been
  449. * written to, the following page maps in the entire configuration space of
  450. * the function.
  451. */
  452. /**
  453. * _hv_pcifront_read_config() - Internal PCI config read
  454. * @hpdev: The PCI driver's representation of the device
  455. * @where: Offset within config space
  456. * @size: Size of the transfer
  457. * @val: Pointer to the buffer receiving the data
  458. */
  459. static void _hv_pcifront_read_config(struct hv_pci_dev *hpdev, int where,
  460. int size, u32 *val)
  461. {
  462. unsigned long flags;
  463. void __iomem *addr = hpdev->hbus->cfg_addr + CFG_PAGE_OFFSET + where;
  464. /*
  465. * If the attempt is to read the IDs or the ROM BAR, simulate that.
  466. */
  467. if (where + size <= PCI_COMMAND) {
  468. memcpy(val, ((u8 *)&hpdev->desc.v_id) + where, size);
  469. } else if (where >= PCI_CLASS_REVISION && where + size <=
  470. PCI_CACHE_LINE_SIZE) {
  471. memcpy(val, ((u8 *)&hpdev->desc.rev) + where -
  472. PCI_CLASS_REVISION, size);
  473. } else if (where >= PCI_SUBSYSTEM_VENDOR_ID && where + size <=
  474. PCI_ROM_ADDRESS) {
  475. memcpy(val, (u8 *)&hpdev->desc.subsystem_id + where -
  476. PCI_SUBSYSTEM_VENDOR_ID, size);
  477. } else if (where >= PCI_ROM_ADDRESS && where + size <=
  478. PCI_CAPABILITY_LIST) {
  479. /* ROM BARs are unimplemented */
  480. *val = 0;
  481. } else if (where >= PCI_INTERRUPT_LINE && where + size <=
  482. PCI_INTERRUPT_PIN) {
  483. /*
  484. * Interrupt Line and Interrupt PIN are hard-wired to zero
  485. * because this front-end only supports message-signaled
  486. * interrupts.
  487. */
  488. *val = 0;
  489. } else if (where + size <= CFG_PAGE_SIZE) {
  490. spin_lock_irqsave(&hpdev->hbus->config_lock, flags);
  491. /* Choose the function to be read. (See comment above) */
  492. writel(hpdev->desc.win_slot.slot, hpdev->hbus->cfg_addr);
  493. /* Make sure the function was chosen before we start reading. */
  494. mb();
  495. /* Read from that function's config space. */
  496. switch (size) {
  497. case 1:
  498. *val = readb(addr);
  499. break;
  500. case 2:
  501. *val = readw(addr);
  502. break;
  503. default:
  504. *val = readl(addr);
  505. break;
  506. }
  507. /*
  508. * Make sure the write was done before we release the spinlock
  509. * allowing consecutive reads/writes.
  510. */
  511. mb();
  512. spin_unlock_irqrestore(&hpdev->hbus->config_lock, flags);
  513. } else {
  514. dev_err(&hpdev->hbus->hdev->device,
  515. "Attempt to read beyond a function's config space.\n");
  516. }
  517. }
  518. /**
  519. * _hv_pcifront_write_config() - Internal PCI config write
  520. * @hpdev: The PCI driver's representation of the device
  521. * @where: Offset within config space
  522. * @size: Size of the transfer
  523. * @val: The data being transferred
  524. */
  525. static void _hv_pcifront_write_config(struct hv_pci_dev *hpdev, int where,
  526. int size, u32 val)
  527. {
  528. unsigned long flags;
  529. void __iomem *addr = hpdev->hbus->cfg_addr + CFG_PAGE_OFFSET + where;
  530. if (where >= PCI_SUBSYSTEM_VENDOR_ID &&
  531. where + size <= PCI_CAPABILITY_LIST) {
  532. /* SSIDs and ROM BARs are read-only */
  533. } else if (where >= PCI_COMMAND && where + size <= CFG_PAGE_SIZE) {
  534. spin_lock_irqsave(&hpdev->hbus->config_lock, flags);
  535. /* Choose the function to be written. (See comment above) */
  536. writel(hpdev->desc.win_slot.slot, hpdev->hbus->cfg_addr);
  537. /* Make sure the function was chosen before we start writing. */
  538. wmb();
  539. /* Write to that function's config space. */
  540. switch (size) {
  541. case 1:
  542. writeb(val, addr);
  543. break;
  544. case 2:
  545. writew(val, addr);
  546. break;
  547. default:
  548. writel(val, addr);
  549. break;
  550. }
  551. /*
  552. * Make sure the write was done before we release the spinlock
  553. * allowing consecutive reads/writes.
  554. */
  555. mb();
  556. spin_unlock_irqrestore(&hpdev->hbus->config_lock, flags);
  557. } else {
  558. dev_err(&hpdev->hbus->hdev->device,
  559. "Attempt to write beyond a function's config space.\n");
  560. }
  561. }
  562. /**
  563. * hv_pcifront_read_config() - Read configuration space
  564. * @bus: PCI Bus structure
  565. * @devfn: Device/function
  566. * @where: Offset from base
  567. * @size: Byte/word/dword
  568. * @val: Value to be read
  569. *
  570. * Return: PCIBIOS_SUCCESSFUL on success
  571. * PCIBIOS_DEVICE_NOT_FOUND on failure
  572. */
  573. static int hv_pcifront_read_config(struct pci_bus *bus, unsigned int devfn,
  574. int where, int size, u32 *val)
  575. {
  576. struct hv_pcibus_device *hbus =
  577. container_of(bus->sysdata, struct hv_pcibus_device, sysdata);
  578. struct hv_pci_dev *hpdev;
  579. hpdev = get_pcichild_wslot(hbus, devfn_to_wslot(devfn));
  580. if (!hpdev)
  581. return PCIBIOS_DEVICE_NOT_FOUND;
  582. _hv_pcifront_read_config(hpdev, where, size, val);
  583. put_pcichild(hpdev, hv_pcidev_ref_by_slot);
  584. return PCIBIOS_SUCCESSFUL;
  585. }
  586. /**
  587. * hv_pcifront_write_config() - Write configuration space
  588. * @bus: PCI Bus structure
  589. * @devfn: Device/function
  590. * @where: Offset from base
  591. * @size: Byte/word/dword
  592. * @val: Value to be written to device
  593. *
  594. * Return: PCIBIOS_SUCCESSFUL on success
  595. * PCIBIOS_DEVICE_NOT_FOUND on failure
  596. */
  597. static int hv_pcifront_write_config(struct pci_bus *bus, unsigned int devfn,
  598. int where, int size, u32 val)
  599. {
  600. struct hv_pcibus_device *hbus =
  601. container_of(bus->sysdata, struct hv_pcibus_device, sysdata);
  602. struct hv_pci_dev *hpdev;
  603. hpdev = get_pcichild_wslot(hbus, devfn_to_wslot(devfn));
  604. if (!hpdev)
  605. return PCIBIOS_DEVICE_NOT_FOUND;
  606. _hv_pcifront_write_config(hpdev, where, size, val);
  607. put_pcichild(hpdev, hv_pcidev_ref_by_slot);
  608. return PCIBIOS_SUCCESSFUL;
  609. }
  610. /* PCIe operations */
  611. static struct pci_ops hv_pcifront_ops = {
  612. .read = hv_pcifront_read_config,
  613. .write = hv_pcifront_write_config,
  614. };
  615. /* Interrupt management hooks */
  616. static void hv_int_desc_free(struct hv_pci_dev *hpdev,
  617. struct tran_int_desc *int_desc)
  618. {
  619. struct pci_delete_interrupt *int_pkt;
  620. struct {
  621. struct pci_packet pkt;
  622. u8 buffer[sizeof(struct pci_delete_interrupt) -
  623. sizeof(struct pci_message)];
  624. } ctxt;
  625. memset(&ctxt, 0, sizeof(ctxt));
  626. int_pkt = (struct pci_delete_interrupt *)&ctxt.pkt.message;
  627. int_pkt->message_type.message_type =
  628. PCI_DELETE_INTERRUPT_MESSAGE;
  629. int_pkt->wslot.slot = hpdev->desc.win_slot.slot;
  630. int_pkt->int_desc = *int_desc;
  631. vmbus_sendpacket(hpdev->hbus->hdev->channel, int_pkt, sizeof(*int_pkt),
  632. (unsigned long)&ctxt.pkt, VM_PKT_DATA_INBAND, 0);
  633. kfree(int_desc);
  634. }
  635. /**
  636. * hv_msi_free() - Free the MSI.
  637. * @domain: The interrupt domain pointer
  638. * @info: Extra MSI-related context
  639. * @irq: Identifies the IRQ.
  640. *
  641. * The Hyper-V parent partition and hypervisor are tracking the
  642. * messages that are in use, keeping the interrupt redirection
  643. * table up to date. This callback sends a message that frees
  644. * the IRT entry and related tracking nonsense.
  645. */
  646. static void hv_msi_free(struct irq_domain *domain, struct msi_domain_info *info,
  647. unsigned int irq)
  648. {
  649. struct hv_pcibus_device *hbus;
  650. struct hv_pci_dev *hpdev;
  651. struct pci_dev *pdev;
  652. struct tran_int_desc *int_desc;
  653. struct irq_data *irq_data = irq_domain_get_irq_data(domain, irq);
  654. struct msi_desc *msi = irq_data_get_msi_desc(irq_data);
  655. pdev = msi_desc_to_pci_dev(msi);
  656. hbus = info->data;
  657. int_desc = irq_data_get_irq_chip_data(irq_data);
  658. if (!int_desc)
  659. return;
  660. irq_data->chip_data = NULL;
  661. hpdev = get_pcichild_wslot(hbus, devfn_to_wslot(pdev->devfn));
  662. if (!hpdev) {
  663. kfree(int_desc);
  664. return;
  665. }
  666. hv_int_desc_free(hpdev, int_desc);
  667. put_pcichild(hpdev, hv_pcidev_ref_by_slot);
  668. }
  669. static int hv_set_affinity(struct irq_data *data, const struct cpumask *dest,
  670. bool force)
  671. {
  672. struct irq_data *parent = data->parent_data;
  673. return parent->chip->irq_set_affinity(parent, dest, force);
  674. }
  675. void hv_irq_mask(struct irq_data *data)
  676. {
  677. pci_msi_mask_irq(data);
  678. }
  679. /**
  680. * hv_irq_unmask() - "Unmask" the IRQ by setting its current
  681. * affinity.
  682. * @data: Describes the IRQ
  683. *
  684. * Build new a destination for the MSI and make a hypercall to
  685. * update the Interrupt Redirection Table. "Device Logical ID"
  686. * is built out of this PCI bus's instance GUID and the function
  687. * number of the device.
  688. */
  689. void hv_irq_unmask(struct irq_data *data)
  690. {
  691. struct msi_desc *msi_desc = irq_data_get_msi_desc(data);
  692. struct irq_cfg *cfg = irqd_cfg(data);
  693. struct retarget_msi_interrupt params;
  694. struct hv_pcibus_device *hbus;
  695. struct cpumask *dest;
  696. struct pci_bus *pbus;
  697. struct pci_dev *pdev;
  698. int cpu;
  699. dest = irq_data_get_affinity_mask(data);
  700. pdev = msi_desc_to_pci_dev(msi_desc);
  701. pbus = pdev->bus;
  702. hbus = container_of(pbus->sysdata, struct hv_pcibus_device, sysdata);
  703. memset(&params, 0, sizeof(params));
  704. params.partition_id = HV_PARTITION_ID_SELF;
  705. params.source = 1; /* MSI(-X) */
  706. params.address = msi_desc->msg.address_lo;
  707. params.data = msi_desc->msg.data;
  708. params.device_id = (hbus->hdev->dev_instance.b[5] << 24) |
  709. (hbus->hdev->dev_instance.b[4] << 16) |
  710. (hbus->hdev->dev_instance.b[7] << 8) |
  711. (hbus->hdev->dev_instance.b[6] & 0xf8) |
  712. PCI_FUNC(pdev->devfn);
  713. params.vector = cfg->vector;
  714. for_each_cpu_and(cpu, dest, cpu_online_mask)
  715. params.vp_mask |= (1ULL << vmbus_cpu_number_to_vp_number(cpu));
  716. hv_do_hypercall(HVCALL_RETARGET_INTERRUPT, &params, NULL);
  717. pci_msi_unmask_irq(data);
  718. }
  719. struct compose_comp_ctxt {
  720. struct hv_pci_compl comp_pkt;
  721. struct tran_int_desc int_desc;
  722. };
  723. static void hv_pci_compose_compl(void *context, struct pci_response *resp,
  724. int resp_packet_size)
  725. {
  726. struct compose_comp_ctxt *comp_pkt = context;
  727. struct pci_create_int_response *int_resp =
  728. (struct pci_create_int_response *)resp;
  729. comp_pkt->comp_pkt.completion_status = resp->status;
  730. comp_pkt->int_desc = int_resp->int_desc;
  731. complete(&comp_pkt->comp_pkt.host_event);
  732. }
  733. /**
  734. * hv_compose_msi_msg() - Supplies a valid MSI address/data
  735. * @data: Everything about this MSI
  736. * @msg: Buffer that is filled in by this function
  737. *
  738. * This function unpacks the IRQ looking for target CPU set, IDT
  739. * vector and mode and sends a message to the parent partition
  740. * asking for a mapping for that tuple in this partition. The
  741. * response supplies a data value and address to which that data
  742. * should be written to trigger that interrupt.
  743. */
  744. static void hv_compose_msi_msg(struct irq_data *data, struct msi_msg *msg)
  745. {
  746. struct irq_cfg *cfg = irqd_cfg(data);
  747. struct hv_pcibus_device *hbus;
  748. struct hv_pci_dev *hpdev;
  749. struct pci_bus *pbus;
  750. struct pci_dev *pdev;
  751. struct pci_create_interrupt *int_pkt;
  752. struct compose_comp_ctxt comp;
  753. struct tran_int_desc *int_desc;
  754. struct cpumask *affinity;
  755. struct {
  756. struct pci_packet pkt;
  757. u8 buffer[sizeof(struct pci_create_interrupt) -
  758. sizeof(struct pci_message)];
  759. } ctxt;
  760. int cpu;
  761. int ret;
  762. pdev = msi_desc_to_pci_dev(irq_data_get_msi_desc(data));
  763. pbus = pdev->bus;
  764. hbus = container_of(pbus->sysdata, struct hv_pcibus_device, sysdata);
  765. hpdev = get_pcichild_wslot(hbus, devfn_to_wslot(pdev->devfn));
  766. if (!hpdev)
  767. goto return_null_message;
  768. /* Free any previous message that might have already been composed. */
  769. if (data->chip_data) {
  770. int_desc = data->chip_data;
  771. data->chip_data = NULL;
  772. hv_int_desc_free(hpdev, int_desc);
  773. }
  774. int_desc = kzalloc(sizeof(*int_desc), GFP_KERNEL);
  775. if (!int_desc)
  776. goto drop_reference;
  777. memset(&ctxt, 0, sizeof(ctxt));
  778. init_completion(&comp.comp_pkt.host_event);
  779. ctxt.pkt.completion_func = hv_pci_compose_compl;
  780. ctxt.pkt.compl_ctxt = &comp;
  781. int_pkt = (struct pci_create_interrupt *)&ctxt.pkt.message;
  782. int_pkt->message_type.message_type = PCI_CREATE_INTERRUPT_MESSAGE;
  783. int_pkt->wslot.slot = hpdev->desc.win_slot.slot;
  784. int_pkt->int_desc.vector = cfg->vector;
  785. int_pkt->int_desc.vector_count = 1;
  786. int_pkt->int_desc.delivery_mode =
  787. (apic->irq_delivery_mode == dest_LowestPrio) ? 1 : 0;
  788. /*
  789. * This bit doesn't have to work on machines with more than 64
  790. * processors because Hyper-V only supports 64 in a guest.
  791. */
  792. affinity = irq_data_get_affinity_mask(data);
  793. for_each_cpu_and(cpu, affinity, cpu_online_mask) {
  794. int_pkt->int_desc.cpu_mask |=
  795. (1ULL << vmbus_cpu_number_to_vp_number(cpu));
  796. }
  797. ret = vmbus_sendpacket(hpdev->hbus->hdev->channel, int_pkt,
  798. sizeof(*int_pkt), (unsigned long)&ctxt.pkt,
  799. VM_PKT_DATA_INBAND,
  800. VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
  801. if (!ret)
  802. wait_for_completion(&comp.comp_pkt.host_event);
  803. if (comp.comp_pkt.completion_status < 0) {
  804. dev_err(&hbus->hdev->device,
  805. "Request for interrupt failed: 0x%x",
  806. comp.comp_pkt.completion_status);
  807. goto free_int_desc;
  808. }
  809. /*
  810. * Record the assignment so that this can be unwound later. Using
  811. * irq_set_chip_data() here would be appropriate, but the lock it takes
  812. * is already held.
  813. */
  814. *int_desc = comp.int_desc;
  815. data->chip_data = int_desc;
  816. /* Pass up the result. */
  817. msg->address_hi = comp.int_desc.address >> 32;
  818. msg->address_lo = comp.int_desc.address & 0xffffffff;
  819. msg->data = comp.int_desc.data;
  820. put_pcichild(hpdev, hv_pcidev_ref_by_slot);
  821. return;
  822. free_int_desc:
  823. kfree(int_desc);
  824. drop_reference:
  825. put_pcichild(hpdev, hv_pcidev_ref_by_slot);
  826. return_null_message:
  827. msg->address_hi = 0;
  828. msg->address_lo = 0;
  829. msg->data = 0;
  830. }
  831. /* HW Interrupt Chip Descriptor */
  832. static struct irq_chip hv_msi_irq_chip = {
  833. .name = "Hyper-V PCIe MSI",
  834. .irq_compose_msi_msg = hv_compose_msi_msg,
  835. .irq_set_affinity = hv_set_affinity,
  836. .irq_ack = irq_chip_ack_parent,
  837. .irq_mask = hv_irq_mask,
  838. .irq_unmask = hv_irq_unmask,
  839. };
  840. static irq_hw_number_t hv_msi_domain_ops_get_hwirq(struct msi_domain_info *info,
  841. msi_alloc_info_t *arg)
  842. {
  843. return arg->msi_hwirq;
  844. }
  845. static struct msi_domain_ops hv_msi_ops = {
  846. .get_hwirq = hv_msi_domain_ops_get_hwirq,
  847. .msi_prepare = pci_msi_prepare,
  848. .set_desc = pci_msi_set_desc,
  849. .msi_free = hv_msi_free,
  850. };
  851. /**
  852. * hv_pcie_init_irq_domain() - Initialize IRQ domain
  853. * @hbus: The root PCI bus
  854. *
  855. * This function creates an IRQ domain which will be used for
  856. * interrupts from devices that have been passed through. These
  857. * devices only support MSI and MSI-X, not line-based interrupts
  858. * or simulations of line-based interrupts through PCIe's
  859. * fabric-layer messages. Because interrupts are remapped, we
  860. * can support multi-message MSI here.
  861. *
  862. * Return: '0' on success and error value on failure
  863. */
  864. static int hv_pcie_init_irq_domain(struct hv_pcibus_device *hbus)
  865. {
  866. hbus->msi_info.chip = &hv_msi_irq_chip;
  867. hbus->msi_info.ops = &hv_msi_ops;
  868. hbus->msi_info.flags = (MSI_FLAG_USE_DEF_DOM_OPS |
  869. MSI_FLAG_USE_DEF_CHIP_OPS | MSI_FLAG_MULTI_PCI_MSI |
  870. MSI_FLAG_PCI_MSIX);
  871. hbus->msi_info.handler = handle_edge_irq;
  872. hbus->msi_info.handler_name = "edge";
  873. hbus->msi_info.data = hbus;
  874. hbus->irq_domain = pci_msi_create_irq_domain(hbus->sysdata.fwnode,
  875. &hbus->msi_info,
  876. x86_vector_domain);
  877. if (!hbus->irq_domain) {
  878. dev_err(&hbus->hdev->device,
  879. "Failed to build an MSI IRQ domain\n");
  880. return -ENODEV;
  881. }
  882. return 0;
  883. }
  884. /**
  885. * get_bar_size() - Get the address space consumed by a BAR
  886. * @bar_val: Value that a BAR returned after -1 was written
  887. * to it.
  888. *
  889. * This function returns the size of the BAR, rounded up to 1
  890. * page. It has to be rounded up because the hypervisor's page
  891. * table entry that maps the BAR into the VM can't specify an
  892. * offset within a page. The invariant is that the hypervisor
  893. * must place any BARs of smaller than page length at the
  894. * beginning of a page.
  895. *
  896. * Return: Size in bytes of the consumed MMIO space.
  897. */
  898. static u64 get_bar_size(u64 bar_val)
  899. {
  900. return round_up((1 + ~(bar_val & PCI_BASE_ADDRESS_MEM_MASK)),
  901. PAGE_SIZE);
  902. }
  903. /**
  904. * survey_child_resources() - Total all MMIO requirements
  905. * @hbus: Root PCI bus, as understood by this driver
  906. */
  907. static void survey_child_resources(struct hv_pcibus_device *hbus)
  908. {
  909. struct list_head *iter;
  910. struct hv_pci_dev *hpdev;
  911. resource_size_t bar_size = 0;
  912. unsigned long flags;
  913. struct completion *event;
  914. u64 bar_val;
  915. int i;
  916. /* If nobody is waiting on the answer, don't compute it. */
  917. event = xchg(&hbus->survey_event, NULL);
  918. if (!event)
  919. return;
  920. /* If the answer has already been computed, go with it. */
  921. if (hbus->low_mmio_space || hbus->high_mmio_space) {
  922. complete(event);
  923. return;
  924. }
  925. spin_lock_irqsave(&hbus->device_list_lock, flags);
  926. /*
  927. * Due to an interesting quirk of the PCI spec, all memory regions
  928. * for a child device are a power of 2 in size and aligned in memory,
  929. * so it's sufficient to just add them up without tracking alignment.
  930. */
  931. list_for_each(iter, &hbus->children) {
  932. hpdev = container_of(iter, struct hv_pci_dev, list_entry);
  933. for (i = 0; i < 6; i++) {
  934. if (hpdev->probed_bar[i] & PCI_BASE_ADDRESS_SPACE_IO)
  935. dev_err(&hbus->hdev->device,
  936. "There's an I/O BAR in this list!\n");
  937. if (hpdev->probed_bar[i] != 0) {
  938. /*
  939. * A probed BAR has all the upper bits set that
  940. * can be changed.
  941. */
  942. bar_val = hpdev->probed_bar[i];
  943. if (bar_val & PCI_BASE_ADDRESS_MEM_TYPE_64)
  944. bar_val |=
  945. ((u64)hpdev->probed_bar[++i] << 32);
  946. else
  947. bar_val |= 0xffffffff00000000ULL;
  948. bar_size = get_bar_size(bar_val);
  949. if (bar_val & PCI_BASE_ADDRESS_MEM_TYPE_64)
  950. hbus->high_mmio_space += bar_size;
  951. else
  952. hbus->low_mmio_space += bar_size;
  953. }
  954. }
  955. }
  956. spin_unlock_irqrestore(&hbus->device_list_lock, flags);
  957. complete(event);
  958. }
  959. /**
  960. * prepopulate_bars() - Fill in BARs with defaults
  961. * @hbus: Root PCI bus, as understood by this driver
  962. *
  963. * The core PCI driver code seems much, much happier if the BARs
  964. * for a device have values upon first scan. So fill them in.
  965. * The algorithm below works down from large sizes to small,
  966. * attempting to pack the assignments optimally. The assumption,
  967. * enforced in other parts of the code, is that the beginning of
  968. * the memory-mapped I/O space will be aligned on the largest
  969. * BAR size.
  970. */
  971. static void prepopulate_bars(struct hv_pcibus_device *hbus)
  972. {
  973. resource_size_t high_size = 0;
  974. resource_size_t low_size = 0;
  975. resource_size_t high_base = 0;
  976. resource_size_t low_base = 0;
  977. resource_size_t bar_size;
  978. struct hv_pci_dev *hpdev;
  979. struct list_head *iter;
  980. unsigned long flags;
  981. u64 bar_val;
  982. u32 command;
  983. bool high;
  984. int i;
  985. if (hbus->low_mmio_space) {
  986. low_size = 1ULL << (63 - __builtin_clzll(hbus->low_mmio_space));
  987. low_base = hbus->low_mmio_res->start;
  988. }
  989. if (hbus->high_mmio_space) {
  990. high_size = 1ULL <<
  991. (63 - __builtin_clzll(hbus->high_mmio_space));
  992. high_base = hbus->high_mmio_res->start;
  993. }
  994. spin_lock_irqsave(&hbus->device_list_lock, flags);
  995. /* Pick addresses for the BARs. */
  996. do {
  997. list_for_each(iter, &hbus->children) {
  998. hpdev = container_of(iter, struct hv_pci_dev,
  999. list_entry);
  1000. for (i = 0; i < 6; i++) {
  1001. bar_val = hpdev->probed_bar[i];
  1002. if (bar_val == 0)
  1003. continue;
  1004. high = bar_val & PCI_BASE_ADDRESS_MEM_TYPE_64;
  1005. if (high) {
  1006. bar_val |=
  1007. ((u64)hpdev->probed_bar[i + 1]
  1008. << 32);
  1009. } else {
  1010. bar_val |= 0xffffffffULL << 32;
  1011. }
  1012. bar_size = get_bar_size(bar_val);
  1013. if (high) {
  1014. if (high_size != bar_size) {
  1015. i++;
  1016. continue;
  1017. }
  1018. _hv_pcifront_write_config(hpdev,
  1019. PCI_BASE_ADDRESS_0 + (4 * i),
  1020. 4,
  1021. (u32)(high_base & 0xffffff00));
  1022. i++;
  1023. _hv_pcifront_write_config(hpdev,
  1024. PCI_BASE_ADDRESS_0 + (4 * i),
  1025. 4, (u32)(high_base >> 32));
  1026. high_base += bar_size;
  1027. } else {
  1028. if (low_size != bar_size)
  1029. continue;
  1030. _hv_pcifront_write_config(hpdev,
  1031. PCI_BASE_ADDRESS_0 + (4 * i),
  1032. 4,
  1033. (u32)(low_base & 0xffffff00));
  1034. low_base += bar_size;
  1035. }
  1036. }
  1037. if (high_size <= 1 && low_size <= 1) {
  1038. /* Set the memory enable bit. */
  1039. _hv_pcifront_read_config(hpdev, PCI_COMMAND, 2,
  1040. &command);
  1041. command |= PCI_COMMAND_MEMORY;
  1042. _hv_pcifront_write_config(hpdev, PCI_COMMAND, 2,
  1043. command);
  1044. break;
  1045. }
  1046. }
  1047. high_size >>= 1;
  1048. low_size >>= 1;
  1049. } while (high_size || low_size);
  1050. spin_unlock_irqrestore(&hbus->device_list_lock, flags);
  1051. }
  1052. /**
  1053. * create_root_hv_pci_bus() - Expose a new root PCI bus
  1054. * @hbus: Root PCI bus, as understood by this driver
  1055. *
  1056. * Return: 0 on success, -errno on failure
  1057. */
  1058. static int create_root_hv_pci_bus(struct hv_pcibus_device *hbus)
  1059. {
  1060. /* Register the device */
  1061. hbus->pci_bus = pci_create_root_bus(&hbus->hdev->device,
  1062. 0, /* bus number is always zero */
  1063. &hv_pcifront_ops,
  1064. &hbus->sysdata,
  1065. &hbus->resources_for_children);
  1066. if (!hbus->pci_bus)
  1067. return -ENODEV;
  1068. hbus->pci_bus->msi = &hbus->msi_chip;
  1069. hbus->pci_bus->msi->dev = &hbus->hdev->device;
  1070. pci_scan_child_bus(hbus->pci_bus);
  1071. pci_bus_assign_resources(hbus->pci_bus);
  1072. pci_bus_add_devices(hbus->pci_bus);
  1073. hbus->state = hv_pcibus_installed;
  1074. return 0;
  1075. }
  1076. struct q_res_req_compl {
  1077. struct completion host_event;
  1078. struct hv_pci_dev *hpdev;
  1079. };
  1080. /**
  1081. * q_resource_requirements() - Query Resource Requirements
  1082. * @context: The completion context.
  1083. * @resp: The response that came from the host.
  1084. * @resp_packet_size: The size in bytes of resp.
  1085. *
  1086. * This function is invoked on completion of a Query Resource
  1087. * Requirements packet.
  1088. */
  1089. static void q_resource_requirements(void *context, struct pci_response *resp,
  1090. int resp_packet_size)
  1091. {
  1092. struct q_res_req_compl *completion = context;
  1093. struct pci_q_res_req_response *q_res_req =
  1094. (struct pci_q_res_req_response *)resp;
  1095. int i;
  1096. if (resp->status < 0) {
  1097. dev_err(&completion->hpdev->hbus->hdev->device,
  1098. "query resource requirements failed: %x\n",
  1099. resp->status);
  1100. } else {
  1101. for (i = 0; i < 6; i++) {
  1102. completion->hpdev->probed_bar[i] =
  1103. q_res_req->probed_bar[i];
  1104. }
  1105. }
  1106. complete(&completion->host_event);
  1107. }
  1108. static void get_pcichild(struct hv_pci_dev *hpdev,
  1109. enum hv_pcidev_ref_reason reason)
  1110. {
  1111. atomic_inc(&hpdev->refs);
  1112. }
  1113. static void put_pcichild(struct hv_pci_dev *hpdev,
  1114. enum hv_pcidev_ref_reason reason)
  1115. {
  1116. if (atomic_dec_and_test(&hpdev->refs))
  1117. kfree(hpdev);
  1118. }
  1119. /**
  1120. * new_pcichild_device() - Create a new child device
  1121. * @hbus: The internal struct tracking this root PCI bus.
  1122. * @desc: The information supplied so far from the host
  1123. * about the device.
  1124. *
  1125. * This function creates the tracking structure for a new child
  1126. * device and kicks off the process of figuring out what it is.
  1127. *
  1128. * Return: Pointer to the new tracking struct
  1129. */
  1130. static struct hv_pci_dev *new_pcichild_device(struct hv_pcibus_device *hbus,
  1131. struct pci_function_description *desc)
  1132. {
  1133. struct hv_pci_dev *hpdev;
  1134. struct pci_child_message *res_req;
  1135. struct q_res_req_compl comp_pkt;
  1136. union {
  1137. struct pci_packet init_packet;
  1138. u8 buffer[0x100];
  1139. } pkt;
  1140. unsigned long flags;
  1141. int ret;
  1142. hpdev = kzalloc(sizeof(*hpdev), GFP_ATOMIC);
  1143. if (!hpdev)
  1144. return NULL;
  1145. hpdev->hbus = hbus;
  1146. memset(&pkt, 0, sizeof(pkt));
  1147. init_completion(&comp_pkt.host_event);
  1148. comp_pkt.hpdev = hpdev;
  1149. pkt.init_packet.compl_ctxt = &comp_pkt;
  1150. pkt.init_packet.completion_func = q_resource_requirements;
  1151. res_req = (struct pci_child_message *)&pkt.init_packet.message;
  1152. res_req->message_type = PCI_QUERY_RESOURCE_REQUIREMENTS;
  1153. res_req->wslot.slot = desc->win_slot.slot;
  1154. ret = vmbus_sendpacket(hbus->hdev->channel, res_req,
  1155. sizeof(struct pci_child_message),
  1156. (unsigned long)&pkt.init_packet,
  1157. VM_PKT_DATA_INBAND,
  1158. VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
  1159. if (ret)
  1160. goto error;
  1161. wait_for_completion(&comp_pkt.host_event);
  1162. hpdev->desc = *desc;
  1163. get_pcichild(hpdev, hv_pcidev_ref_initial);
  1164. get_pcichild(hpdev, hv_pcidev_ref_childlist);
  1165. spin_lock_irqsave(&hbus->device_list_lock, flags);
  1166. list_add_tail(&hpdev->list_entry, &hbus->children);
  1167. spin_unlock_irqrestore(&hbus->device_list_lock, flags);
  1168. return hpdev;
  1169. error:
  1170. kfree(hpdev);
  1171. return NULL;
  1172. }
  1173. /**
  1174. * get_pcichild_wslot() - Find device from slot
  1175. * @hbus: Root PCI bus, as understood by this driver
  1176. * @wslot: Location on the bus
  1177. *
  1178. * This function looks up a PCI device and returns the internal
  1179. * representation of it. It acquires a reference on it, so that
  1180. * the device won't be deleted while somebody is using it. The
  1181. * caller is responsible for calling put_pcichild() to release
  1182. * this reference.
  1183. *
  1184. * Return: Internal representation of a PCI device
  1185. */
  1186. static struct hv_pci_dev *get_pcichild_wslot(struct hv_pcibus_device *hbus,
  1187. u32 wslot)
  1188. {
  1189. unsigned long flags;
  1190. struct hv_pci_dev *iter, *hpdev = NULL;
  1191. spin_lock_irqsave(&hbus->device_list_lock, flags);
  1192. list_for_each_entry(iter, &hbus->children, list_entry) {
  1193. if (iter->desc.win_slot.slot == wslot) {
  1194. hpdev = iter;
  1195. get_pcichild(hpdev, hv_pcidev_ref_by_slot);
  1196. break;
  1197. }
  1198. }
  1199. spin_unlock_irqrestore(&hbus->device_list_lock, flags);
  1200. return hpdev;
  1201. }
  1202. /**
  1203. * pci_devices_present_work() - Handle new list of child devices
  1204. * @work: Work struct embedded in struct hv_dr_work
  1205. *
  1206. * "Bus Relations" is the Windows term for "children of this
  1207. * bus." The terminology is preserved here for people trying to
  1208. * debug the interaction between Hyper-V and Linux. This
  1209. * function is called when the parent partition reports a list
  1210. * of functions that should be observed under this PCI Express
  1211. * port (bus).
  1212. *
  1213. * This function updates the list, and must tolerate being
  1214. * called multiple times with the same information. The typical
  1215. * number of child devices is one, with very atypical cases
  1216. * involving three or four, so the algorithms used here can be
  1217. * simple and inefficient.
  1218. *
  1219. * It must also treat the omission of a previously observed device as
  1220. * notification that the device no longer exists.
  1221. *
  1222. * Note that this function is a work item, and it may not be
  1223. * invoked in the order that it was queued. Back to back
  1224. * updates of the list of present devices may involve queuing
  1225. * multiple work items, and this one may run before ones that
  1226. * were sent later. As such, this function only does something
  1227. * if is the last one in the queue.
  1228. */
  1229. static void pci_devices_present_work(struct work_struct *work)
  1230. {
  1231. u32 child_no;
  1232. bool found;
  1233. struct list_head *iter;
  1234. struct pci_function_description *new_desc;
  1235. struct hv_pci_dev *hpdev;
  1236. struct hv_pcibus_device *hbus;
  1237. struct list_head removed;
  1238. struct hv_dr_work *dr_wrk;
  1239. struct hv_dr_state *dr = NULL;
  1240. unsigned long flags;
  1241. dr_wrk = container_of(work, struct hv_dr_work, wrk);
  1242. hbus = dr_wrk->bus;
  1243. kfree(dr_wrk);
  1244. INIT_LIST_HEAD(&removed);
  1245. if (down_interruptible(&hbus->enum_sem)) {
  1246. put_hvpcibus(hbus);
  1247. return;
  1248. }
  1249. /* Pull this off the queue and process it if it was the last one. */
  1250. spin_lock_irqsave(&hbus->device_list_lock, flags);
  1251. while (!list_empty(&hbus->dr_list)) {
  1252. dr = list_first_entry(&hbus->dr_list, struct hv_dr_state,
  1253. list_entry);
  1254. list_del(&dr->list_entry);
  1255. /* Throw this away if the list still has stuff in it. */
  1256. if (!list_empty(&hbus->dr_list)) {
  1257. kfree(dr);
  1258. continue;
  1259. }
  1260. }
  1261. spin_unlock_irqrestore(&hbus->device_list_lock, flags);
  1262. if (!dr) {
  1263. up(&hbus->enum_sem);
  1264. put_hvpcibus(hbus);
  1265. return;
  1266. }
  1267. /* First, mark all existing children as reported missing. */
  1268. spin_lock_irqsave(&hbus->device_list_lock, flags);
  1269. list_for_each(iter, &hbus->children) {
  1270. hpdev = container_of(iter, struct hv_pci_dev,
  1271. list_entry);
  1272. hpdev->reported_missing = true;
  1273. }
  1274. spin_unlock_irqrestore(&hbus->device_list_lock, flags);
  1275. /* Next, add back any reported devices. */
  1276. for (child_no = 0; child_no < dr->device_count; child_no++) {
  1277. found = false;
  1278. new_desc = &dr->func[child_no];
  1279. spin_lock_irqsave(&hbus->device_list_lock, flags);
  1280. list_for_each(iter, &hbus->children) {
  1281. hpdev = container_of(iter, struct hv_pci_dev,
  1282. list_entry);
  1283. if ((hpdev->desc.win_slot.slot ==
  1284. new_desc->win_slot.slot) &&
  1285. (hpdev->desc.v_id == new_desc->v_id) &&
  1286. (hpdev->desc.d_id == new_desc->d_id) &&
  1287. (hpdev->desc.ser == new_desc->ser)) {
  1288. hpdev->reported_missing = false;
  1289. found = true;
  1290. }
  1291. }
  1292. spin_unlock_irqrestore(&hbus->device_list_lock, flags);
  1293. if (!found) {
  1294. hpdev = new_pcichild_device(hbus, new_desc);
  1295. if (!hpdev)
  1296. dev_err(&hbus->hdev->device,
  1297. "couldn't record a child device.\n");
  1298. }
  1299. }
  1300. /* Move missing children to a list on the stack. */
  1301. spin_lock_irqsave(&hbus->device_list_lock, flags);
  1302. do {
  1303. found = false;
  1304. list_for_each(iter, &hbus->children) {
  1305. hpdev = container_of(iter, struct hv_pci_dev,
  1306. list_entry);
  1307. if (hpdev->reported_missing) {
  1308. found = true;
  1309. put_pcichild(hpdev, hv_pcidev_ref_childlist);
  1310. list_del(&hpdev->list_entry);
  1311. list_add_tail(&hpdev->list_entry, &removed);
  1312. break;
  1313. }
  1314. }
  1315. } while (found);
  1316. spin_unlock_irqrestore(&hbus->device_list_lock, flags);
  1317. /* Delete everything that should no longer exist. */
  1318. while (!list_empty(&removed)) {
  1319. hpdev = list_first_entry(&removed, struct hv_pci_dev,
  1320. list_entry);
  1321. list_del(&hpdev->list_entry);
  1322. put_pcichild(hpdev, hv_pcidev_ref_initial);
  1323. }
  1324. /* Tell the core to rescan bus because there may have been changes. */
  1325. if (hbus->state == hv_pcibus_installed) {
  1326. pci_lock_rescan_remove();
  1327. pci_scan_child_bus(hbus->pci_bus);
  1328. pci_unlock_rescan_remove();
  1329. } else {
  1330. survey_child_resources(hbus);
  1331. }
  1332. up(&hbus->enum_sem);
  1333. put_hvpcibus(hbus);
  1334. kfree(dr);
  1335. }
  1336. /**
  1337. * hv_pci_devices_present() - Handles list of new children
  1338. * @hbus: Root PCI bus, as understood by this driver
  1339. * @relations: Packet from host listing children
  1340. *
  1341. * This function is invoked whenever a new list of devices for
  1342. * this bus appears.
  1343. */
  1344. static void hv_pci_devices_present(struct hv_pcibus_device *hbus,
  1345. struct pci_bus_relations *relations)
  1346. {
  1347. struct hv_dr_state *dr;
  1348. struct hv_dr_work *dr_wrk;
  1349. unsigned long flags;
  1350. dr_wrk = kzalloc(sizeof(*dr_wrk), GFP_NOWAIT);
  1351. if (!dr_wrk)
  1352. return;
  1353. dr = kzalloc(offsetof(struct hv_dr_state, func) +
  1354. (sizeof(struct pci_function_description) *
  1355. (relations->device_count)), GFP_NOWAIT);
  1356. if (!dr) {
  1357. kfree(dr_wrk);
  1358. return;
  1359. }
  1360. INIT_WORK(&dr_wrk->wrk, pci_devices_present_work);
  1361. dr_wrk->bus = hbus;
  1362. dr->device_count = relations->device_count;
  1363. if (dr->device_count != 0) {
  1364. memcpy(dr->func, relations->func,
  1365. sizeof(struct pci_function_description) *
  1366. dr->device_count);
  1367. }
  1368. spin_lock_irqsave(&hbus->device_list_lock, flags);
  1369. list_add_tail(&dr->list_entry, &hbus->dr_list);
  1370. spin_unlock_irqrestore(&hbus->device_list_lock, flags);
  1371. get_hvpcibus(hbus);
  1372. schedule_work(&dr_wrk->wrk);
  1373. }
  1374. /**
  1375. * hv_eject_device_work() - Asynchronously handles ejection
  1376. * @work: Work struct embedded in internal device struct
  1377. *
  1378. * This function handles ejecting a device. Windows will
  1379. * attempt to gracefully eject a device, waiting 60 seconds to
  1380. * hear back from the guest OS that this completed successfully.
  1381. * If this timer expires, the device will be forcibly removed.
  1382. */
  1383. static void hv_eject_device_work(struct work_struct *work)
  1384. {
  1385. struct pci_eject_response *ejct_pkt;
  1386. struct hv_pci_dev *hpdev;
  1387. struct pci_dev *pdev;
  1388. unsigned long flags;
  1389. int wslot;
  1390. struct {
  1391. struct pci_packet pkt;
  1392. u8 buffer[sizeof(struct pci_eject_response) -
  1393. sizeof(struct pci_message)];
  1394. } ctxt;
  1395. hpdev = container_of(work, struct hv_pci_dev, wrk);
  1396. if (hpdev->state != hv_pcichild_ejecting) {
  1397. put_pcichild(hpdev, hv_pcidev_ref_pnp);
  1398. return;
  1399. }
  1400. /*
  1401. * Ejection can come before or after the PCI bus has been set up, so
  1402. * attempt to find it and tear down the bus state, if it exists. This
  1403. * must be done without constructs like pci_domain_nr(hbus->pci_bus)
  1404. * because hbus->pci_bus may not exist yet.
  1405. */
  1406. wslot = wslot_to_devfn(hpdev->desc.win_slot.slot);
  1407. pdev = pci_get_domain_bus_and_slot(hpdev->hbus->sysdata.domain, 0,
  1408. wslot);
  1409. if (pdev) {
  1410. pci_stop_and_remove_bus_device(pdev);
  1411. pci_dev_put(pdev);
  1412. }
  1413. memset(&ctxt, 0, sizeof(ctxt));
  1414. ejct_pkt = (struct pci_eject_response *)&ctxt.pkt.message;
  1415. ejct_pkt->message_type = PCI_EJECTION_COMPLETE;
  1416. ejct_pkt->wslot.slot = hpdev->desc.win_slot.slot;
  1417. vmbus_sendpacket(hpdev->hbus->hdev->channel, ejct_pkt,
  1418. sizeof(*ejct_pkt), (unsigned long)&ctxt.pkt,
  1419. VM_PKT_DATA_INBAND, 0);
  1420. spin_lock_irqsave(&hpdev->hbus->device_list_lock, flags);
  1421. list_del(&hpdev->list_entry);
  1422. spin_unlock_irqrestore(&hpdev->hbus->device_list_lock, flags);
  1423. put_pcichild(hpdev, hv_pcidev_ref_childlist);
  1424. put_pcichild(hpdev, hv_pcidev_ref_pnp);
  1425. put_hvpcibus(hpdev->hbus);
  1426. }
  1427. /**
  1428. * hv_pci_eject_device() - Handles device ejection
  1429. * @hpdev: Internal device tracking struct
  1430. *
  1431. * This function is invoked when an ejection packet arrives. It
  1432. * just schedules work so that we don't re-enter the packet
  1433. * delivery code handling the ejection.
  1434. */
  1435. static void hv_pci_eject_device(struct hv_pci_dev *hpdev)
  1436. {
  1437. hpdev->state = hv_pcichild_ejecting;
  1438. get_pcichild(hpdev, hv_pcidev_ref_pnp);
  1439. INIT_WORK(&hpdev->wrk, hv_eject_device_work);
  1440. get_hvpcibus(hpdev->hbus);
  1441. schedule_work(&hpdev->wrk);
  1442. }
  1443. /**
  1444. * hv_pci_onchannelcallback() - Handles incoming packets
  1445. * @context: Internal bus tracking struct
  1446. *
  1447. * This function is invoked whenever the host sends a packet to
  1448. * this channel (which is private to this root PCI bus).
  1449. */
  1450. static void hv_pci_onchannelcallback(void *context)
  1451. {
  1452. const int packet_size = 0x100;
  1453. int ret;
  1454. struct hv_pcibus_device *hbus = context;
  1455. u32 bytes_recvd;
  1456. u64 req_id;
  1457. struct vmpacket_descriptor *desc;
  1458. unsigned char *buffer;
  1459. int bufferlen = packet_size;
  1460. struct pci_packet *comp_packet;
  1461. struct pci_response *response;
  1462. struct pci_incoming_message *new_message;
  1463. struct pci_bus_relations *bus_rel;
  1464. struct pci_dev_incoming *dev_message;
  1465. struct hv_pci_dev *hpdev;
  1466. buffer = kmalloc(bufferlen, GFP_ATOMIC);
  1467. if (!buffer)
  1468. return;
  1469. while (1) {
  1470. ret = vmbus_recvpacket_raw(hbus->hdev->channel, buffer,
  1471. bufferlen, &bytes_recvd, &req_id);
  1472. if (ret == -ENOBUFS) {
  1473. kfree(buffer);
  1474. /* Handle large packet */
  1475. bufferlen = bytes_recvd;
  1476. buffer = kmalloc(bytes_recvd, GFP_ATOMIC);
  1477. if (!buffer)
  1478. return;
  1479. continue;
  1480. }
  1481. /* Zero length indicates there are no more packets. */
  1482. if (ret || !bytes_recvd)
  1483. break;
  1484. /*
  1485. * All incoming packets must be at least as large as a
  1486. * response.
  1487. */
  1488. if (bytes_recvd <= sizeof(struct pci_response))
  1489. continue;
  1490. desc = (struct vmpacket_descriptor *)buffer;
  1491. switch (desc->type) {
  1492. case VM_PKT_COMP:
  1493. /*
  1494. * The host is trusted, and thus it's safe to interpret
  1495. * this transaction ID as a pointer.
  1496. */
  1497. comp_packet = (struct pci_packet *)req_id;
  1498. response = (struct pci_response *)buffer;
  1499. comp_packet->completion_func(comp_packet->compl_ctxt,
  1500. response,
  1501. bytes_recvd);
  1502. break;
  1503. case VM_PKT_DATA_INBAND:
  1504. new_message = (struct pci_incoming_message *)buffer;
  1505. switch (new_message->message_type.message_type) {
  1506. case PCI_BUS_RELATIONS:
  1507. bus_rel = (struct pci_bus_relations *)buffer;
  1508. if (bytes_recvd <
  1509. offsetof(struct pci_bus_relations, func) +
  1510. (sizeof(struct pci_function_description) *
  1511. (bus_rel->device_count))) {
  1512. dev_err(&hbus->hdev->device,
  1513. "bus relations too small\n");
  1514. break;
  1515. }
  1516. hv_pci_devices_present(hbus, bus_rel);
  1517. break;
  1518. case PCI_EJECT:
  1519. dev_message = (struct pci_dev_incoming *)buffer;
  1520. hpdev = get_pcichild_wslot(hbus,
  1521. dev_message->wslot.slot);
  1522. if (hpdev) {
  1523. hv_pci_eject_device(hpdev);
  1524. put_pcichild(hpdev,
  1525. hv_pcidev_ref_by_slot);
  1526. }
  1527. break;
  1528. default:
  1529. dev_warn(&hbus->hdev->device,
  1530. "Unimplemented protocol message %x\n",
  1531. new_message->message_type.message_type);
  1532. break;
  1533. }
  1534. break;
  1535. default:
  1536. dev_err(&hbus->hdev->device,
  1537. "unhandled packet type %d, tid %llx len %d\n",
  1538. desc->type, req_id, bytes_recvd);
  1539. break;
  1540. }
  1541. }
  1542. kfree(buffer);
  1543. }
  1544. /**
  1545. * hv_pci_protocol_negotiation() - Set up protocol
  1546. * @hdev: VMBus's tracking struct for this root PCI bus
  1547. *
  1548. * This driver is intended to support running on Windows 10
  1549. * (server) and later versions. It will not run on earlier
  1550. * versions, as they assume that many of the operations which
  1551. * Linux needs accomplished with a spinlock held were done via
  1552. * asynchronous messaging via VMBus. Windows 10 increases the
  1553. * surface area of PCI emulation so that these actions can take
  1554. * place by suspending a virtual processor for their duration.
  1555. *
  1556. * This function negotiates the channel protocol version,
  1557. * failing if the host doesn't support the necessary protocol
  1558. * level.
  1559. */
  1560. static int hv_pci_protocol_negotiation(struct hv_device *hdev)
  1561. {
  1562. struct pci_version_request *version_req;
  1563. struct hv_pci_compl comp_pkt;
  1564. struct pci_packet *pkt;
  1565. int ret;
  1566. /*
  1567. * Initiate the handshake with the host and negotiate
  1568. * a version that the host can support. We start with the
  1569. * highest version number and go down if the host cannot
  1570. * support it.
  1571. */
  1572. pkt = kzalloc(sizeof(*pkt) + sizeof(*version_req), GFP_KERNEL);
  1573. if (!pkt)
  1574. return -ENOMEM;
  1575. init_completion(&comp_pkt.host_event);
  1576. pkt->completion_func = hv_pci_generic_compl;
  1577. pkt->compl_ctxt = &comp_pkt;
  1578. version_req = (struct pci_version_request *)&pkt->message;
  1579. version_req->message_type.message_type = PCI_QUERY_PROTOCOL_VERSION;
  1580. version_req->protocol_version = PCI_PROTOCOL_VERSION_CURRENT;
  1581. ret = vmbus_sendpacket(hdev->channel, version_req,
  1582. sizeof(struct pci_version_request),
  1583. (unsigned long)pkt, VM_PKT_DATA_INBAND,
  1584. VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
  1585. if (ret)
  1586. goto exit;
  1587. wait_for_completion(&comp_pkt.host_event);
  1588. if (comp_pkt.completion_status < 0) {
  1589. dev_err(&hdev->device,
  1590. "PCI Pass-through VSP failed version request %x\n",
  1591. comp_pkt.completion_status);
  1592. ret = -EPROTO;
  1593. goto exit;
  1594. }
  1595. ret = 0;
  1596. exit:
  1597. kfree(pkt);
  1598. return ret;
  1599. }
  1600. /**
  1601. * hv_pci_free_bridge_windows() - Release memory regions for the
  1602. * bus
  1603. * @hbus: Root PCI bus, as understood by this driver
  1604. */
  1605. static void hv_pci_free_bridge_windows(struct hv_pcibus_device *hbus)
  1606. {
  1607. /*
  1608. * Set the resources back to the way they looked when they
  1609. * were allocated by setting IORESOURCE_BUSY again.
  1610. */
  1611. if (hbus->low_mmio_space && hbus->low_mmio_res) {
  1612. hbus->low_mmio_res->flags |= IORESOURCE_BUSY;
  1613. vmbus_free_mmio(hbus->low_mmio_res->start,
  1614. resource_size(hbus->low_mmio_res));
  1615. }
  1616. if (hbus->high_mmio_space && hbus->high_mmio_res) {
  1617. hbus->high_mmio_res->flags |= IORESOURCE_BUSY;
  1618. vmbus_free_mmio(hbus->high_mmio_res->start,
  1619. resource_size(hbus->high_mmio_res));
  1620. }
  1621. }
  1622. /**
  1623. * hv_pci_allocate_bridge_windows() - Allocate memory regions
  1624. * for the bus
  1625. * @hbus: Root PCI bus, as understood by this driver
  1626. *
  1627. * This function calls vmbus_allocate_mmio(), which is itself a
  1628. * bit of a compromise. Ideally, we might change the pnp layer
  1629. * in the kernel such that it comprehends either PCI devices
  1630. * which are "grandchildren of ACPI," with some intermediate bus
  1631. * node (in this case, VMBus) or change it such that it
  1632. * understands VMBus. The pnp layer, however, has been declared
  1633. * deprecated, and not subject to change.
  1634. *
  1635. * The workaround, implemented here, is to ask VMBus to allocate
  1636. * MMIO space for this bus. VMBus itself knows which ranges are
  1637. * appropriate by looking at its own ACPI objects. Then, after
  1638. * these ranges are claimed, they're modified to look like they
  1639. * would have looked if the ACPI and pnp code had allocated
  1640. * bridge windows. These descriptors have to exist in this form
  1641. * in order to satisfy the code which will get invoked when the
  1642. * endpoint PCI function driver calls request_mem_region() or
  1643. * request_mem_region_exclusive().
  1644. *
  1645. * Return: 0 on success, -errno on failure
  1646. */
  1647. static int hv_pci_allocate_bridge_windows(struct hv_pcibus_device *hbus)
  1648. {
  1649. resource_size_t align;
  1650. int ret;
  1651. if (hbus->low_mmio_space) {
  1652. align = 1ULL << (63 - __builtin_clzll(hbus->low_mmio_space));
  1653. ret = vmbus_allocate_mmio(&hbus->low_mmio_res, hbus->hdev, 0,
  1654. (u64)(u32)0xffffffff,
  1655. hbus->low_mmio_space,
  1656. align, false);
  1657. if (ret) {
  1658. dev_err(&hbus->hdev->device,
  1659. "Need %#llx of low MMIO space. Consider reconfiguring the VM.\n",
  1660. hbus->low_mmio_space);
  1661. return ret;
  1662. }
  1663. /* Modify this resource to become a bridge window. */
  1664. hbus->low_mmio_res->flags |= IORESOURCE_WINDOW;
  1665. hbus->low_mmio_res->flags &= ~IORESOURCE_BUSY;
  1666. pci_add_resource(&hbus->resources_for_children,
  1667. hbus->low_mmio_res);
  1668. }
  1669. if (hbus->high_mmio_space) {
  1670. align = 1ULL << (63 - __builtin_clzll(hbus->high_mmio_space));
  1671. ret = vmbus_allocate_mmio(&hbus->high_mmio_res, hbus->hdev,
  1672. 0x100000000, -1,
  1673. hbus->high_mmio_space, align,
  1674. false);
  1675. if (ret) {
  1676. dev_err(&hbus->hdev->device,
  1677. "Need %#llx of high MMIO space. Consider reconfiguring the VM.\n",
  1678. hbus->high_mmio_space);
  1679. goto release_low_mmio;
  1680. }
  1681. /* Modify this resource to become a bridge window. */
  1682. hbus->high_mmio_res->flags |= IORESOURCE_WINDOW;
  1683. hbus->high_mmio_res->flags &= ~IORESOURCE_BUSY;
  1684. pci_add_resource(&hbus->resources_for_children,
  1685. hbus->high_mmio_res);
  1686. }
  1687. return 0;
  1688. release_low_mmio:
  1689. if (hbus->low_mmio_res) {
  1690. vmbus_free_mmio(hbus->low_mmio_res->start,
  1691. resource_size(hbus->low_mmio_res));
  1692. }
  1693. return ret;
  1694. }
  1695. /**
  1696. * hv_allocate_config_window() - Find MMIO space for PCI Config
  1697. * @hbus: Root PCI bus, as understood by this driver
  1698. *
  1699. * This function claims memory-mapped I/O space for accessing
  1700. * configuration space for the functions on this bus.
  1701. *
  1702. * Return: 0 on success, -errno on failure
  1703. */
  1704. static int hv_allocate_config_window(struct hv_pcibus_device *hbus)
  1705. {
  1706. int ret;
  1707. /*
  1708. * Set up a region of MMIO space to use for accessing configuration
  1709. * space.
  1710. */
  1711. ret = vmbus_allocate_mmio(&hbus->mem_config, hbus->hdev, 0, -1,
  1712. PCI_CONFIG_MMIO_LENGTH, 0x1000, false);
  1713. if (ret)
  1714. return ret;
  1715. /*
  1716. * vmbus_allocate_mmio() gets used for allocating both device endpoint
  1717. * resource claims (those which cannot be overlapped) and the ranges
  1718. * which are valid for the children of this bus, which are intended
  1719. * to be overlapped by those children. Set the flag on this claim
  1720. * meaning that this region can't be overlapped.
  1721. */
  1722. hbus->mem_config->flags |= IORESOURCE_BUSY;
  1723. return 0;
  1724. }
  1725. static void hv_free_config_window(struct hv_pcibus_device *hbus)
  1726. {
  1727. vmbus_free_mmio(hbus->mem_config->start, PCI_CONFIG_MMIO_LENGTH);
  1728. }
  1729. /**
  1730. * hv_pci_enter_d0() - Bring the "bus" into the D0 power state
  1731. * @hdev: VMBus's tracking struct for this root PCI bus
  1732. *
  1733. * Return: 0 on success, -errno on failure
  1734. */
  1735. static int hv_pci_enter_d0(struct hv_device *hdev)
  1736. {
  1737. struct hv_pcibus_device *hbus = hv_get_drvdata(hdev);
  1738. struct pci_bus_d0_entry *d0_entry;
  1739. struct hv_pci_compl comp_pkt;
  1740. struct pci_packet *pkt;
  1741. int ret;
  1742. /*
  1743. * Tell the host that the bus is ready to use, and moved into the
  1744. * powered-on state. This includes telling the host which region
  1745. * of memory-mapped I/O space has been chosen for configuration space
  1746. * access.
  1747. */
  1748. pkt = kzalloc(sizeof(*pkt) + sizeof(*d0_entry), GFP_KERNEL);
  1749. if (!pkt)
  1750. return -ENOMEM;
  1751. init_completion(&comp_pkt.host_event);
  1752. pkt->completion_func = hv_pci_generic_compl;
  1753. pkt->compl_ctxt = &comp_pkt;
  1754. d0_entry = (struct pci_bus_d0_entry *)&pkt->message;
  1755. d0_entry->message_type.message_type = PCI_BUS_D0ENTRY;
  1756. d0_entry->mmio_base = hbus->mem_config->start;
  1757. ret = vmbus_sendpacket(hdev->channel, d0_entry, sizeof(*d0_entry),
  1758. (unsigned long)pkt, VM_PKT_DATA_INBAND,
  1759. VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
  1760. if (ret)
  1761. goto exit;
  1762. wait_for_completion(&comp_pkt.host_event);
  1763. if (comp_pkt.completion_status < 0) {
  1764. dev_err(&hdev->device,
  1765. "PCI Pass-through VSP failed D0 Entry with status %x\n",
  1766. comp_pkt.completion_status);
  1767. ret = -EPROTO;
  1768. goto exit;
  1769. }
  1770. ret = 0;
  1771. exit:
  1772. kfree(pkt);
  1773. return ret;
  1774. }
  1775. /**
  1776. * hv_pci_query_relations() - Ask host to send list of child
  1777. * devices
  1778. * @hdev: VMBus's tracking struct for this root PCI bus
  1779. *
  1780. * Return: 0 on success, -errno on failure
  1781. */
  1782. static int hv_pci_query_relations(struct hv_device *hdev)
  1783. {
  1784. struct hv_pcibus_device *hbus = hv_get_drvdata(hdev);
  1785. struct pci_message message;
  1786. struct completion comp;
  1787. int ret;
  1788. /* Ask the host to send along the list of child devices */
  1789. init_completion(&comp);
  1790. if (cmpxchg(&hbus->survey_event, NULL, &comp))
  1791. return -ENOTEMPTY;
  1792. memset(&message, 0, sizeof(message));
  1793. message.message_type = PCI_QUERY_BUS_RELATIONS;
  1794. ret = vmbus_sendpacket(hdev->channel, &message, sizeof(message),
  1795. 0, VM_PKT_DATA_INBAND, 0);
  1796. if (ret)
  1797. return ret;
  1798. wait_for_completion(&comp);
  1799. return 0;
  1800. }
  1801. /**
  1802. * hv_send_resources_allocated() - Report local resource choices
  1803. * @hdev: VMBus's tracking struct for this root PCI bus
  1804. *
  1805. * The host OS is expecting to be sent a request as a message
  1806. * which contains all the resources that the device will use.
  1807. * The response contains those same resources, "translated"
  1808. * which is to say, the values which should be used by the
  1809. * hardware, when it delivers an interrupt. (MMIO resources are
  1810. * used in local terms.) This is nice for Windows, and lines up
  1811. * with the FDO/PDO split, which doesn't exist in Linux. Linux
  1812. * is deeply expecting to scan an emulated PCI configuration
  1813. * space. So this message is sent here only to drive the state
  1814. * machine on the host forward.
  1815. *
  1816. * Return: 0 on success, -errno on failure
  1817. */
  1818. static int hv_send_resources_allocated(struct hv_device *hdev)
  1819. {
  1820. struct hv_pcibus_device *hbus = hv_get_drvdata(hdev);
  1821. struct pci_resources_assigned *res_assigned;
  1822. struct hv_pci_compl comp_pkt;
  1823. struct hv_pci_dev *hpdev;
  1824. struct pci_packet *pkt;
  1825. u32 wslot;
  1826. int ret;
  1827. pkt = kmalloc(sizeof(*pkt) + sizeof(*res_assigned), GFP_KERNEL);
  1828. if (!pkt)
  1829. return -ENOMEM;
  1830. ret = 0;
  1831. for (wslot = 0; wslot < 256; wslot++) {
  1832. hpdev = get_pcichild_wslot(hbus, wslot);
  1833. if (!hpdev)
  1834. continue;
  1835. memset(pkt, 0, sizeof(*pkt) + sizeof(*res_assigned));
  1836. init_completion(&comp_pkt.host_event);
  1837. pkt->completion_func = hv_pci_generic_compl;
  1838. pkt->compl_ctxt = &comp_pkt;
  1839. pkt->message.message_type = PCI_RESOURCES_ASSIGNED;
  1840. res_assigned = (struct pci_resources_assigned *)&pkt->message;
  1841. res_assigned->wslot.slot = hpdev->desc.win_slot.slot;
  1842. put_pcichild(hpdev, hv_pcidev_ref_by_slot);
  1843. ret = vmbus_sendpacket(
  1844. hdev->channel, &pkt->message,
  1845. sizeof(*res_assigned),
  1846. (unsigned long)pkt,
  1847. VM_PKT_DATA_INBAND,
  1848. VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
  1849. if (ret)
  1850. break;
  1851. wait_for_completion(&comp_pkt.host_event);
  1852. if (comp_pkt.completion_status < 0) {
  1853. ret = -EPROTO;
  1854. dev_err(&hdev->device,
  1855. "resource allocated returned 0x%x",
  1856. comp_pkt.completion_status);
  1857. break;
  1858. }
  1859. }
  1860. kfree(pkt);
  1861. return ret;
  1862. }
  1863. /**
  1864. * hv_send_resources_released() - Report local resources
  1865. * released
  1866. * @hdev: VMBus's tracking struct for this root PCI bus
  1867. *
  1868. * Return: 0 on success, -errno on failure
  1869. */
  1870. static int hv_send_resources_released(struct hv_device *hdev)
  1871. {
  1872. struct hv_pcibus_device *hbus = hv_get_drvdata(hdev);
  1873. struct pci_child_message pkt;
  1874. struct hv_pci_dev *hpdev;
  1875. u32 wslot;
  1876. int ret;
  1877. for (wslot = 0; wslot < 256; wslot++) {
  1878. hpdev = get_pcichild_wslot(hbus, wslot);
  1879. if (!hpdev)
  1880. continue;
  1881. memset(&pkt, 0, sizeof(pkt));
  1882. pkt.message_type = PCI_RESOURCES_RELEASED;
  1883. pkt.wslot.slot = hpdev->desc.win_slot.slot;
  1884. put_pcichild(hpdev, hv_pcidev_ref_by_slot);
  1885. ret = vmbus_sendpacket(hdev->channel, &pkt, sizeof(pkt), 0,
  1886. VM_PKT_DATA_INBAND, 0);
  1887. if (ret)
  1888. return ret;
  1889. }
  1890. return 0;
  1891. }
  1892. static void get_hvpcibus(struct hv_pcibus_device *hbus)
  1893. {
  1894. atomic_inc(&hbus->remove_lock);
  1895. }
  1896. static void put_hvpcibus(struct hv_pcibus_device *hbus)
  1897. {
  1898. if (atomic_dec_and_test(&hbus->remove_lock))
  1899. complete(&hbus->remove_event);
  1900. }
  1901. /**
  1902. * hv_pci_probe() - New VMBus channel probe, for a root PCI bus
  1903. * @hdev: VMBus's tracking struct for this root PCI bus
  1904. * @dev_id: Identifies the device itself
  1905. *
  1906. * Return: 0 on success, -errno on failure
  1907. */
  1908. static int hv_pci_probe(struct hv_device *hdev,
  1909. const struct hv_vmbus_device_id *dev_id)
  1910. {
  1911. struct hv_pcibus_device *hbus;
  1912. int ret;
  1913. hbus = kzalloc(sizeof(*hbus), GFP_KERNEL);
  1914. if (!hbus)
  1915. return -ENOMEM;
  1916. /*
  1917. * The PCI bus "domain" is what is called "segment" in ACPI and
  1918. * other specs. Pull it from the instance ID, to get something
  1919. * unique. Bytes 8 and 9 are what is used in Windows guests, so
  1920. * do the same thing for consistency. Note that, since this code
  1921. * only runs in a Hyper-V VM, Hyper-V can (and does) guarantee
  1922. * that (1) the only domain in use for something that looks like
  1923. * a physical PCI bus (which is actually emulated by the
  1924. * hypervisor) is domain 0 and (2) there will be no overlap
  1925. * between domains derived from these instance IDs in the same
  1926. * VM.
  1927. */
  1928. hbus->sysdata.domain = hdev->dev_instance.b[9] |
  1929. hdev->dev_instance.b[8] << 8;
  1930. hbus->hdev = hdev;
  1931. atomic_inc(&hbus->remove_lock);
  1932. INIT_LIST_HEAD(&hbus->children);
  1933. INIT_LIST_HEAD(&hbus->dr_list);
  1934. INIT_LIST_HEAD(&hbus->resources_for_children);
  1935. spin_lock_init(&hbus->config_lock);
  1936. spin_lock_init(&hbus->device_list_lock);
  1937. sema_init(&hbus->enum_sem, 1);
  1938. init_completion(&hbus->remove_event);
  1939. ret = vmbus_open(hdev->channel, pci_ring_size, pci_ring_size, NULL, 0,
  1940. hv_pci_onchannelcallback, hbus);
  1941. if (ret)
  1942. goto free_bus;
  1943. hv_set_drvdata(hdev, hbus);
  1944. ret = hv_pci_protocol_negotiation(hdev);
  1945. if (ret)
  1946. goto close;
  1947. ret = hv_allocate_config_window(hbus);
  1948. if (ret)
  1949. goto close;
  1950. hbus->cfg_addr = ioremap(hbus->mem_config->start,
  1951. PCI_CONFIG_MMIO_LENGTH);
  1952. if (!hbus->cfg_addr) {
  1953. dev_err(&hdev->device,
  1954. "Unable to map a virtual address for config space\n");
  1955. ret = -ENOMEM;
  1956. goto free_config;
  1957. }
  1958. hbus->sysdata.fwnode = irq_domain_alloc_fwnode(hbus);
  1959. if (!hbus->sysdata.fwnode) {
  1960. ret = -ENOMEM;
  1961. goto unmap;
  1962. }
  1963. ret = hv_pcie_init_irq_domain(hbus);
  1964. if (ret)
  1965. goto free_fwnode;
  1966. ret = hv_pci_query_relations(hdev);
  1967. if (ret)
  1968. goto free_irq_domain;
  1969. ret = hv_pci_enter_d0(hdev);
  1970. if (ret)
  1971. goto free_irq_domain;
  1972. ret = hv_pci_allocate_bridge_windows(hbus);
  1973. if (ret)
  1974. goto free_irq_domain;
  1975. ret = hv_send_resources_allocated(hdev);
  1976. if (ret)
  1977. goto free_windows;
  1978. prepopulate_bars(hbus);
  1979. hbus->state = hv_pcibus_probed;
  1980. ret = create_root_hv_pci_bus(hbus);
  1981. if (ret)
  1982. goto free_windows;
  1983. return 0;
  1984. free_windows:
  1985. hv_pci_free_bridge_windows(hbus);
  1986. free_irq_domain:
  1987. irq_domain_remove(hbus->irq_domain);
  1988. free_fwnode:
  1989. irq_domain_free_fwnode(hbus->sysdata.fwnode);
  1990. unmap:
  1991. iounmap(hbus->cfg_addr);
  1992. free_config:
  1993. hv_free_config_window(hbus);
  1994. close:
  1995. vmbus_close(hdev->channel);
  1996. free_bus:
  1997. kfree(hbus);
  1998. return ret;
  1999. }
  2000. /**
  2001. * hv_pci_remove() - Remove routine for this VMBus channel
  2002. * @hdev: VMBus's tracking struct for this root PCI bus
  2003. *
  2004. * Return: 0 on success, -errno on failure
  2005. */
  2006. static int hv_pci_remove(struct hv_device *hdev)
  2007. {
  2008. int ret;
  2009. struct hv_pcibus_device *hbus;
  2010. union {
  2011. struct pci_packet teardown_packet;
  2012. u8 buffer[0x100];
  2013. } pkt;
  2014. struct pci_bus_relations relations;
  2015. struct hv_pci_compl comp_pkt;
  2016. hbus = hv_get_drvdata(hdev);
  2017. memset(&pkt.teardown_packet, 0, sizeof(pkt.teardown_packet));
  2018. init_completion(&comp_pkt.host_event);
  2019. pkt.teardown_packet.completion_func = hv_pci_generic_compl;
  2020. pkt.teardown_packet.compl_ctxt = &comp_pkt;
  2021. pkt.teardown_packet.message.message_type = PCI_BUS_D0EXIT;
  2022. ret = vmbus_sendpacket(hdev->channel, &pkt.teardown_packet.message,
  2023. sizeof(struct pci_message),
  2024. (unsigned long)&pkt.teardown_packet,
  2025. VM_PKT_DATA_INBAND,
  2026. VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
  2027. if (!ret)
  2028. wait_for_completion_timeout(&comp_pkt.host_event, 10 * HZ);
  2029. if (hbus->state == hv_pcibus_installed) {
  2030. /* Remove the bus from PCI's point of view. */
  2031. pci_lock_rescan_remove();
  2032. pci_stop_root_bus(hbus->pci_bus);
  2033. pci_remove_root_bus(hbus->pci_bus);
  2034. pci_unlock_rescan_remove();
  2035. }
  2036. ret = hv_send_resources_released(hdev);
  2037. if (ret)
  2038. dev_err(&hdev->device,
  2039. "Couldn't send resources released packet(s)\n");
  2040. vmbus_close(hdev->channel);
  2041. /* Delete any children which might still exist. */
  2042. memset(&relations, 0, sizeof(relations));
  2043. hv_pci_devices_present(hbus, &relations);
  2044. iounmap(hbus->cfg_addr);
  2045. hv_free_config_window(hbus);
  2046. pci_free_resource_list(&hbus->resources_for_children);
  2047. hv_pci_free_bridge_windows(hbus);
  2048. irq_domain_remove(hbus->irq_domain);
  2049. irq_domain_free_fwnode(hbus->sysdata.fwnode);
  2050. put_hvpcibus(hbus);
  2051. wait_for_completion(&hbus->remove_event);
  2052. kfree(hbus);
  2053. return 0;
  2054. }
  2055. static const struct hv_vmbus_device_id hv_pci_id_table[] = {
  2056. /* PCI Pass-through Class ID */
  2057. /* 44C4F61D-4444-4400-9D52-802E27EDE19F */
  2058. { HV_PCIE_GUID, },
  2059. { },
  2060. };
  2061. MODULE_DEVICE_TABLE(vmbus, hv_pci_id_table);
  2062. static struct hv_driver hv_pci_drv = {
  2063. .name = "hv_pci",
  2064. .id_table = hv_pci_id_table,
  2065. .probe = hv_pci_probe,
  2066. .remove = hv_pci_remove,
  2067. };
  2068. static void __exit exit_hv_pci_drv(void)
  2069. {
  2070. vmbus_driver_unregister(&hv_pci_drv);
  2071. }
  2072. static int __init init_hv_pci_drv(void)
  2073. {
  2074. return vmbus_driver_register(&hv_pci_drv);
  2075. }
  2076. module_init(init_hv_pci_drv);
  2077. module_exit(exit_hv_pci_drv);
  2078. MODULE_DESCRIPTION("Hyper-V PCI");
  2079. MODULE_LICENSE("GPL v2");