pci-hyperv.c 65 KB

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