vmd.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Volume Management Device driver
  4. * Copyright (c) 2015, Intel Corporation.
  5. */
  6. #include <linux/device.h>
  7. #include <linux/interrupt.h>
  8. #include <linux/irq.h>
  9. #include <linux/kernel.h>
  10. #include <linux/module.h>
  11. #include <linux/msi.h>
  12. #include <linux/pci.h>
  13. #include <linux/pci-acpi.h>
  14. #include <linux/pci-ecam.h>
  15. #include <linux/srcu.h>
  16. #include <linux/rculist.h>
  17. #include <linux/rcupdate.h>
  18. #include <asm/irqdomain.h>
  19. #define VMD_CFGBAR 0
  20. #define VMD_MEMBAR1 2
  21. #define VMD_MEMBAR2 4
  22. #define PCI_REG_VMCAP 0x40
  23. #define BUS_RESTRICT_CAP(vmcap) (vmcap & 0x1)
  24. #define PCI_REG_VMCONFIG 0x44
  25. #define BUS_RESTRICT_CFG(vmcfg) ((vmcfg >> 8) & 0x3)
  26. #define VMCONFIG_MSI_REMAP 0x2
  27. #define PCI_REG_VMLOCK 0x70
  28. #define MB2_SHADOW_EN(vmlock) (vmlock & 0x2)
  29. #define MB2_SHADOW_OFFSET 0x2000
  30. #define MB2_SHADOW_SIZE 16
  31. enum vmd_features {
  32. /*
  33. * Device may contain registers which hint the physical location of the
  34. * membars, in order to allow proper address translation during
  35. * resource assignment to enable guest virtualization
  36. */
  37. VMD_FEAT_HAS_MEMBAR_SHADOW = (1 << 0),
  38. /*
  39. * Device may provide root port configuration information which limits
  40. * bus numbering
  41. */
  42. VMD_FEAT_HAS_BUS_RESTRICTIONS = (1 << 1),
  43. /*
  44. * Device contains physical location shadow registers in
  45. * vendor-specific capability space
  46. */
  47. VMD_FEAT_HAS_MEMBAR_SHADOW_VSCAP = (1 << 2),
  48. /*
  49. * Device may use MSI-X vector 0 for software triggering and will not
  50. * be used for MSI remapping
  51. */
  52. VMD_FEAT_OFFSET_FIRST_VECTOR = (1 << 3),
  53. /*
  54. * Device can bypass remapping MSI-X transactions into its MSI-X table,
  55. * avoiding the requirement of a VMD MSI domain for child device
  56. * interrupt handling.
  57. */
  58. VMD_FEAT_CAN_BYPASS_MSI_REMAP = (1 << 4),
  59. /*
  60. * Enable ASPM on the PCIE root ports and set the default LTR of the
  61. * storage devices on platforms where these values are not configured by
  62. * BIOS. This is needed for laptops, which require these settings for
  63. * proper power management of the SoC.
  64. */
  65. VMD_FEAT_BIOS_PM_QUIRK = (1 << 5),
  66. };
  67. #define VMD_BIOS_PM_QUIRK_LTR 0x1003 /* 3145728 ns */
  68. #define VMD_FEATS_CLIENT (VMD_FEAT_HAS_MEMBAR_SHADOW_VSCAP | \
  69. VMD_FEAT_HAS_BUS_RESTRICTIONS | \
  70. VMD_FEAT_OFFSET_FIRST_VECTOR | \
  71. VMD_FEAT_BIOS_PM_QUIRK)
  72. static DEFINE_IDA(vmd_instance_ida);
  73. /*
  74. * Lock for manipulating VMD IRQ lists.
  75. */
  76. static DEFINE_RAW_SPINLOCK(list_lock);
  77. /**
  78. * struct vmd_irq - private data to map driver IRQ to the VMD shared vector
  79. * @node: list item for parent traversal.
  80. * @irq: back pointer to parent.
  81. * @enabled: true if driver enabled IRQ
  82. * @virq: the virtual IRQ value provided to the requesting driver.
  83. *
  84. * Every MSI/MSI-X IRQ requested for a device in a VMD domain will be mapped to
  85. * a VMD IRQ using this structure.
  86. */
  87. struct vmd_irq {
  88. struct list_head node;
  89. struct vmd_irq_list *irq;
  90. bool enabled;
  91. unsigned int virq;
  92. };
  93. /**
  94. * struct vmd_irq_list - list of driver requested IRQs mapping to a VMD vector
  95. * @irq_list: the list of irq's the VMD one demuxes to.
  96. * @srcu: SRCU struct for local synchronization.
  97. * @count: number of child IRQs assigned to this vector; used to track
  98. * sharing.
  99. * @virq: The underlying VMD Linux interrupt number
  100. */
  101. struct vmd_irq_list {
  102. struct list_head irq_list;
  103. struct srcu_struct srcu;
  104. unsigned int count;
  105. unsigned int virq;
  106. };
  107. struct vmd_dev {
  108. struct pci_dev *dev;
  109. spinlock_t cfg_lock;
  110. void __iomem *cfgbar;
  111. int msix_count;
  112. struct vmd_irq_list *irqs;
  113. struct pci_sysdata sysdata;
  114. struct resource resources[3];
  115. struct irq_domain *irq_domain;
  116. struct pci_bus *bus;
  117. u8 busn_start;
  118. u8 first_vec;
  119. char *name;
  120. int instance;
  121. };
  122. static inline struct vmd_dev *vmd_from_bus(struct pci_bus *bus)
  123. {
  124. return container_of(bus->sysdata, struct vmd_dev, sysdata);
  125. }
  126. static inline unsigned int index_from_irqs(struct vmd_dev *vmd,
  127. struct vmd_irq_list *irqs)
  128. {
  129. return irqs - vmd->irqs;
  130. }
  131. /*
  132. * Drivers managing a device in a VMD domain allocate their own IRQs as before,
  133. * but the MSI entry for the hardware it's driving will be programmed with a
  134. * destination ID for the VMD MSI-X table. The VMD muxes interrupts in its
  135. * domain into one of its own, and the VMD driver de-muxes these for the
  136. * handlers sharing that VMD IRQ. The vmd irq_domain provides the operations
  137. * and irq_chip to set this up.
  138. */
  139. static void vmd_compose_msi_msg(struct irq_data *data, struct msi_msg *msg)
  140. {
  141. struct vmd_irq *vmdirq = data->chip_data;
  142. struct vmd_irq_list *irq = vmdirq->irq;
  143. struct vmd_dev *vmd = irq_data_get_irq_handler_data(data);
  144. memset(msg, 0, sizeof(*msg));
  145. msg->address_hi = X86_MSI_BASE_ADDRESS_HIGH;
  146. msg->arch_addr_lo.base_address = X86_MSI_BASE_ADDRESS_LOW;
  147. msg->arch_addr_lo.destid_0_7 = index_from_irqs(vmd, irq);
  148. }
  149. /*
  150. * We rely on MSI_FLAG_USE_DEF_CHIP_OPS to set the IRQ mask/unmask ops.
  151. */
  152. static void vmd_irq_enable(struct irq_data *data)
  153. {
  154. struct vmd_irq *vmdirq = data->chip_data;
  155. unsigned long flags;
  156. raw_spin_lock_irqsave(&list_lock, flags);
  157. WARN_ON(vmdirq->enabled);
  158. list_add_tail_rcu(&vmdirq->node, &vmdirq->irq->irq_list);
  159. vmdirq->enabled = true;
  160. raw_spin_unlock_irqrestore(&list_lock, flags);
  161. data->chip->irq_unmask(data);
  162. }
  163. static void vmd_irq_disable(struct irq_data *data)
  164. {
  165. struct vmd_irq *vmdirq = data->chip_data;
  166. unsigned long flags;
  167. data->chip->irq_mask(data);
  168. raw_spin_lock_irqsave(&list_lock, flags);
  169. if (vmdirq->enabled) {
  170. list_del_rcu(&vmdirq->node);
  171. vmdirq->enabled = false;
  172. }
  173. raw_spin_unlock_irqrestore(&list_lock, flags);
  174. }
  175. static struct irq_chip vmd_msi_controller = {
  176. .name = "VMD-MSI",
  177. .irq_enable = vmd_irq_enable,
  178. .irq_disable = vmd_irq_disable,
  179. .irq_compose_msi_msg = vmd_compose_msi_msg,
  180. };
  181. static irq_hw_number_t vmd_get_hwirq(struct msi_domain_info *info,
  182. msi_alloc_info_t *arg)
  183. {
  184. return 0;
  185. }
  186. /*
  187. * XXX: We can be even smarter selecting the best IRQ once we solve the
  188. * affinity problem.
  189. */
  190. static struct vmd_irq_list *vmd_next_irq(struct vmd_dev *vmd, struct msi_desc *desc)
  191. {
  192. unsigned long flags;
  193. int i, best;
  194. if (vmd->msix_count == 1 + vmd->first_vec)
  195. return &vmd->irqs[vmd->first_vec];
  196. /*
  197. * White list for fast-interrupt handlers. All others will share the
  198. * "slow" interrupt vector.
  199. */
  200. switch (msi_desc_to_pci_dev(desc)->class) {
  201. case PCI_CLASS_STORAGE_EXPRESS:
  202. break;
  203. default:
  204. return &vmd->irqs[vmd->first_vec];
  205. }
  206. raw_spin_lock_irqsave(&list_lock, flags);
  207. best = vmd->first_vec + 1;
  208. for (i = best; i < vmd->msix_count; i++)
  209. if (vmd->irqs[i].count < vmd->irqs[best].count)
  210. best = i;
  211. vmd->irqs[best].count++;
  212. raw_spin_unlock_irqrestore(&list_lock, flags);
  213. return &vmd->irqs[best];
  214. }
  215. static int vmd_msi_init(struct irq_domain *domain, struct msi_domain_info *info,
  216. unsigned int virq, irq_hw_number_t hwirq,
  217. msi_alloc_info_t *arg)
  218. {
  219. struct msi_desc *desc = arg->desc;
  220. struct vmd_dev *vmd = vmd_from_bus(msi_desc_to_pci_dev(desc)->bus);
  221. struct vmd_irq *vmdirq = kzalloc(sizeof(*vmdirq), GFP_KERNEL);
  222. if (!vmdirq)
  223. return -ENOMEM;
  224. INIT_LIST_HEAD(&vmdirq->node);
  225. vmdirq->irq = vmd_next_irq(vmd, desc);
  226. vmdirq->virq = virq;
  227. irq_domain_set_info(domain, virq, vmdirq->irq->virq, info->chip, vmdirq,
  228. handle_untracked_irq, vmd, NULL);
  229. return 0;
  230. }
  231. static void vmd_msi_free(struct irq_domain *domain,
  232. struct msi_domain_info *info, unsigned int virq)
  233. {
  234. struct vmd_irq *vmdirq = irq_get_chip_data(virq);
  235. unsigned long flags;
  236. synchronize_srcu(&vmdirq->irq->srcu);
  237. /* XXX: Potential optimization to rebalance */
  238. raw_spin_lock_irqsave(&list_lock, flags);
  239. vmdirq->irq->count--;
  240. raw_spin_unlock_irqrestore(&list_lock, flags);
  241. kfree(vmdirq);
  242. }
  243. static int vmd_msi_prepare(struct irq_domain *domain, struct device *dev,
  244. int nvec, msi_alloc_info_t *arg)
  245. {
  246. struct pci_dev *pdev = to_pci_dev(dev);
  247. struct vmd_dev *vmd = vmd_from_bus(pdev->bus);
  248. if (nvec > vmd->msix_count)
  249. return vmd->msix_count;
  250. memset(arg, 0, sizeof(*arg));
  251. return 0;
  252. }
  253. static void vmd_set_desc(msi_alloc_info_t *arg, struct msi_desc *desc)
  254. {
  255. arg->desc = desc;
  256. }
  257. static struct msi_domain_ops vmd_msi_domain_ops = {
  258. .get_hwirq = vmd_get_hwirq,
  259. .msi_init = vmd_msi_init,
  260. .msi_free = vmd_msi_free,
  261. .msi_prepare = vmd_msi_prepare,
  262. .set_desc = vmd_set_desc,
  263. };
  264. static struct msi_domain_info vmd_msi_domain_info = {
  265. .flags = MSI_FLAG_USE_DEF_DOM_OPS | MSI_FLAG_USE_DEF_CHIP_OPS |
  266. MSI_FLAG_NO_AFFINITY | MSI_FLAG_PCI_MSIX,
  267. .ops = &vmd_msi_domain_ops,
  268. .chip = &vmd_msi_controller,
  269. };
  270. static void vmd_set_msi_remapping(struct vmd_dev *vmd, bool enable)
  271. {
  272. u16 reg;
  273. pci_read_config_word(vmd->dev, PCI_REG_VMCONFIG, &reg);
  274. reg = enable ? (reg & ~VMCONFIG_MSI_REMAP) :
  275. (reg | VMCONFIG_MSI_REMAP);
  276. pci_write_config_word(vmd->dev, PCI_REG_VMCONFIG, reg);
  277. }
  278. static int vmd_create_irq_domain(struct vmd_dev *vmd)
  279. {
  280. struct fwnode_handle *fn;
  281. fn = irq_domain_alloc_named_id_fwnode("VMD-MSI", vmd->sysdata.domain);
  282. if (!fn)
  283. return -ENODEV;
  284. vmd->irq_domain = pci_msi_create_irq_domain(fn, &vmd_msi_domain_info, NULL);
  285. if (!vmd->irq_domain) {
  286. irq_domain_free_fwnode(fn);
  287. return -ENODEV;
  288. }
  289. return 0;
  290. }
  291. static void vmd_remove_irq_domain(struct vmd_dev *vmd)
  292. {
  293. /*
  294. * Some production BIOS won't enable remapping between soft reboots.
  295. * Ensure remapping is restored before unloading the driver.
  296. */
  297. if (!vmd->msix_count)
  298. vmd_set_msi_remapping(vmd, true);
  299. if (vmd->irq_domain) {
  300. struct fwnode_handle *fn = vmd->irq_domain->fwnode;
  301. irq_domain_remove(vmd->irq_domain);
  302. irq_domain_free_fwnode(fn);
  303. }
  304. }
  305. static void __iomem *vmd_cfg_addr(struct vmd_dev *vmd, struct pci_bus *bus,
  306. unsigned int devfn, int reg, int len)
  307. {
  308. unsigned int busnr_ecam = bus->number - vmd->busn_start;
  309. u32 offset = PCIE_ECAM_OFFSET(busnr_ecam, devfn, reg);
  310. if (offset + len >= resource_size(&vmd->dev->resource[VMD_CFGBAR]))
  311. return NULL;
  312. return vmd->cfgbar + offset;
  313. }
  314. /*
  315. * CPU may deadlock if config space is not serialized on some versions of this
  316. * hardware, so all config space access is done under a spinlock.
  317. */
  318. static int vmd_pci_read(struct pci_bus *bus, unsigned int devfn, int reg,
  319. int len, u32 *value)
  320. {
  321. struct vmd_dev *vmd = vmd_from_bus(bus);
  322. void __iomem *addr = vmd_cfg_addr(vmd, bus, devfn, reg, len);
  323. unsigned long flags;
  324. int ret = 0;
  325. if (!addr)
  326. return -EFAULT;
  327. spin_lock_irqsave(&vmd->cfg_lock, flags);
  328. switch (len) {
  329. case 1:
  330. *value = readb(addr);
  331. break;
  332. case 2:
  333. *value = readw(addr);
  334. break;
  335. case 4:
  336. *value = readl(addr);
  337. break;
  338. default:
  339. ret = -EINVAL;
  340. break;
  341. }
  342. spin_unlock_irqrestore(&vmd->cfg_lock, flags);
  343. return ret;
  344. }
  345. /*
  346. * VMD h/w converts non-posted config writes to posted memory writes. The
  347. * read-back in this function forces the completion so it returns only after
  348. * the config space was written, as expected.
  349. */
  350. static int vmd_pci_write(struct pci_bus *bus, unsigned int devfn, int reg,
  351. int len, u32 value)
  352. {
  353. struct vmd_dev *vmd = vmd_from_bus(bus);
  354. void __iomem *addr = vmd_cfg_addr(vmd, bus, devfn, reg, len);
  355. unsigned long flags;
  356. int ret = 0;
  357. if (!addr)
  358. return -EFAULT;
  359. spin_lock_irqsave(&vmd->cfg_lock, flags);
  360. switch (len) {
  361. case 1:
  362. writeb(value, addr);
  363. readb(addr);
  364. break;
  365. case 2:
  366. writew(value, addr);
  367. readw(addr);
  368. break;
  369. case 4:
  370. writel(value, addr);
  371. readl(addr);
  372. break;
  373. default:
  374. ret = -EINVAL;
  375. break;
  376. }
  377. spin_unlock_irqrestore(&vmd->cfg_lock, flags);
  378. return ret;
  379. }
  380. static struct pci_ops vmd_ops = {
  381. .read = vmd_pci_read,
  382. .write = vmd_pci_write,
  383. };
  384. #ifdef CONFIG_ACPI
  385. static struct acpi_device *vmd_acpi_find_companion(struct pci_dev *pci_dev)
  386. {
  387. struct pci_host_bridge *bridge;
  388. u32 busnr, addr;
  389. if (pci_dev->bus->ops != &vmd_ops)
  390. return NULL;
  391. bridge = pci_find_host_bridge(pci_dev->bus);
  392. busnr = pci_dev->bus->number - bridge->bus->number;
  393. /*
  394. * The address computation below is only applicable to relative bus
  395. * numbers below 32.
  396. */
  397. if (busnr > 31)
  398. return NULL;
  399. addr = (busnr << 24) | ((u32)pci_dev->devfn << 16) | 0x8000FFFFU;
  400. dev_dbg(&pci_dev->dev, "Looking for ACPI companion (address 0x%x)\n",
  401. addr);
  402. return acpi_find_child_device(ACPI_COMPANION(bridge->dev.parent), addr,
  403. false);
  404. }
  405. static bool hook_installed;
  406. static void vmd_acpi_begin(void)
  407. {
  408. if (pci_acpi_set_companion_lookup_hook(vmd_acpi_find_companion))
  409. return;
  410. hook_installed = true;
  411. }
  412. static void vmd_acpi_end(void)
  413. {
  414. if (!hook_installed)
  415. return;
  416. pci_acpi_clear_companion_lookup_hook();
  417. hook_installed = false;
  418. }
  419. #else
  420. static inline void vmd_acpi_begin(void) { }
  421. static inline void vmd_acpi_end(void) { }
  422. #endif /* CONFIG_ACPI */
  423. static void vmd_domain_reset(struct vmd_dev *vmd)
  424. {
  425. u16 bus, max_buses = resource_size(&vmd->resources[0]);
  426. u8 dev, functions, fn, hdr_type;
  427. char __iomem *base;
  428. for (bus = 0; bus < max_buses; bus++) {
  429. for (dev = 0; dev < 32; dev++) {
  430. base = vmd->cfgbar + PCIE_ECAM_OFFSET(bus,
  431. PCI_DEVFN(dev, 0), 0);
  432. hdr_type = readb(base + PCI_HEADER_TYPE);
  433. functions = (hdr_type & PCI_HEADER_TYPE_MFD) ? 8 : 1;
  434. for (fn = 0; fn < functions; fn++) {
  435. base = vmd->cfgbar + PCIE_ECAM_OFFSET(bus,
  436. PCI_DEVFN(dev, fn), 0);
  437. hdr_type = readb(base + PCI_HEADER_TYPE) &
  438. PCI_HEADER_TYPE_MASK;
  439. if (hdr_type != PCI_HEADER_TYPE_BRIDGE ||
  440. (readw(base + PCI_CLASS_DEVICE) !=
  441. PCI_CLASS_BRIDGE_PCI))
  442. continue;
  443. /*
  444. * Temporarily disable the I/O range before updating
  445. * PCI_IO_BASE.
  446. */
  447. writel(0x0000ffff, base + PCI_IO_BASE_UPPER16);
  448. /* Update lower 16 bits of I/O base/limit */
  449. writew(0x00f0, base + PCI_IO_BASE);
  450. /* Update upper 16 bits of I/O base/limit */
  451. writel(0, base + PCI_IO_BASE_UPPER16);
  452. /* MMIO Base/Limit */
  453. writel(0x0000fff0, base + PCI_MEMORY_BASE);
  454. /* Prefetchable MMIO Base/Limit */
  455. writel(0, base + PCI_PREF_LIMIT_UPPER32);
  456. writel(0x0000fff0, base + PCI_PREF_MEMORY_BASE);
  457. writel(0xffffffff, base + PCI_PREF_BASE_UPPER32);
  458. }
  459. }
  460. }
  461. }
  462. static void vmd_attach_resources(struct vmd_dev *vmd)
  463. {
  464. vmd->dev->resource[VMD_MEMBAR1].child = &vmd->resources[1];
  465. vmd->dev->resource[VMD_MEMBAR2].child = &vmd->resources[2];
  466. }
  467. static void vmd_detach_resources(struct vmd_dev *vmd)
  468. {
  469. vmd->dev->resource[VMD_MEMBAR1].child = NULL;
  470. vmd->dev->resource[VMD_MEMBAR2].child = NULL;
  471. }
  472. /*
  473. * VMD domains start at 0x10000 to not clash with ACPI _SEG domains.
  474. * Per ACPI r6.0, sec 6.5.6, _SEG returns an integer, of which the lower
  475. * 16 bits are the PCI Segment Group (domain) number. Other bits are
  476. * currently reserved.
  477. */
  478. static int vmd_find_free_domain(void)
  479. {
  480. int domain = 0xffff;
  481. struct pci_bus *bus = NULL;
  482. while ((bus = pci_find_next_bus(bus)) != NULL)
  483. domain = max_t(int, domain, pci_domain_nr(bus));
  484. return domain + 1;
  485. }
  486. static int vmd_get_phys_offsets(struct vmd_dev *vmd, bool native_hint,
  487. resource_size_t *offset1,
  488. resource_size_t *offset2)
  489. {
  490. struct pci_dev *dev = vmd->dev;
  491. u64 phys1, phys2;
  492. if (native_hint) {
  493. u32 vmlock;
  494. int ret;
  495. ret = pci_read_config_dword(dev, PCI_REG_VMLOCK, &vmlock);
  496. if (ret || PCI_POSSIBLE_ERROR(vmlock))
  497. return -ENODEV;
  498. if (MB2_SHADOW_EN(vmlock)) {
  499. void __iomem *membar2;
  500. membar2 = pci_iomap(dev, VMD_MEMBAR2, 0);
  501. if (!membar2)
  502. return -ENOMEM;
  503. phys1 = readq(membar2 + MB2_SHADOW_OFFSET);
  504. phys2 = readq(membar2 + MB2_SHADOW_OFFSET + 8);
  505. pci_iounmap(dev, membar2);
  506. } else
  507. return 0;
  508. } else {
  509. /* Hypervisor-Emulated Vendor-Specific Capability */
  510. int pos = pci_find_capability(dev, PCI_CAP_ID_VNDR);
  511. u32 reg, regu;
  512. pci_read_config_dword(dev, pos + 4, &reg);
  513. /* "SHDW" */
  514. if (pos && reg == 0x53484457) {
  515. pci_read_config_dword(dev, pos + 8, &reg);
  516. pci_read_config_dword(dev, pos + 12, &regu);
  517. phys1 = (u64) regu << 32 | reg;
  518. pci_read_config_dword(dev, pos + 16, &reg);
  519. pci_read_config_dword(dev, pos + 20, &regu);
  520. phys2 = (u64) regu << 32 | reg;
  521. } else
  522. return 0;
  523. }
  524. *offset1 = dev->resource[VMD_MEMBAR1].start -
  525. (phys1 & PCI_BASE_ADDRESS_MEM_MASK);
  526. *offset2 = dev->resource[VMD_MEMBAR2].start -
  527. (phys2 & PCI_BASE_ADDRESS_MEM_MASK);
  528. return 0;
  529. }
  530. static int vmd_get_bus_number_start(struct vmd_dev *vmd)
  531. {
  532. struct pci_dev *dev = vmd->dev;
  533. u16 reg;
  534. pci_read_config_word(dev, PCI_REG_VMCAP, &reg);
  535. if (BUS_RESTRICT_CAP(reg)) {
  536. pci_read_config_word(dev, PCI_REG_VMCONFIG, &reg);
  537. switch (BUS_RESTRICT_CFG(reg)) {
  538. case 0:
  539. vmd->busn_start = 0;
  540. break;
  541. case 1:
  542. vmd->busn_start = 128;
  543. break;
  544. case 2:
  545. vmd->busn_start = 224;
  546. break;
  547. default:
  548. pci_err(dev, "Unknown Bus Offset Setting (%d)\n",
  549. BUS_RESTRICT_CFG(reg));
  550. return -ENODEV;
  551. }
  552. }
  553. return 0;
  554. }
  555. static irqreturn_t vmd_irq(int irq, void *data)
  556. {
  557. struct vmd_irq_list *irqs = data;
  558. struct vmd_irq *vmdirq;
  559. int idx;
  560. idx = srcu_read_lock(&irqs->srcu);
  561. list_for_each_entry_rcu(vmdirq, &irqs->irq_list, node)
  562. generic_handle_irq(vmdirq->virq);
  563. srcu_read_unlock(&irqs->srcu, idx);
  564. return IRQ_HANDLED;
  565. }
  566. static int vmd_alloc_irqs(struct vmd_dev *vmd)
  567. {
  568. struct pci_dev *dev = vmd->dev;
  569. int i, err;
  570. vmd->msix_count = pci_msix_vec_count(dev);
  571. if (vmd->msix_count < 0)
  572. return -ENODEV;
  573. vmd->msix_count = pci_alloc_irq_vectors(dev, vmd->first_vec + 1,
  574. vmd->msix_count, PCI_IRQ_MSIX);
  575. if (vmd->msix_count < 0)
  576. return vmd->msix_count;
  577. vmd->irqs = devm_kcalloc(&dev->dev, vmd->msix_count, sizeof(*vmd->irqs),
  578. GFP_KERNEL);
  579. if (!vmd->irqs)
  580. return -ENOMEM;
  581. for (i = 0; i < vmd->msix_count; i++) {
  582. err = init_srcu_struct(&vmd->irqs[i].srcu);
  583. if (err)
  584. return err;
  585. INIT_LIST_HEAD(&vmd->irqs[i].irq_list);
  586. vmd->irqs[i].virq = pci_irq_vector(dev, i);
  587. err = devm_request_irq(&dev->dev, vmd->irqs[i].virq,
  588. vmd_irq, IRQF_NO_THREAD,
  589. vmd->name, &vmd->irqs[i]);
  590. if (err)
  591. return err;
  592. }
  593. return 0;
  594. }
  595. /*
  596. * Since VMD is an aperture to regular PCIe root ports, only allow it to
  597. * control features that the OS is allowed to control on the physical PCI bus.
  598. */
  599. static void vmd_copy_host_bridge_flags(struct pci_host_bridge *root_bridge,
  600. struct pci_host_bridge *vmd_bridge)
  601. {
  602. vmd_bridge->native_pcie_hotplug = root_bridge->native_pcie_hotplug;
  603. vmd_bridge->native_shpc_hotplug = root_bridge->native_shpc_hotplug;
  604. vmd_bridge->native_aer = root_bridge->native_aer;
  605. vmd_bridge->native_pme = root_bridge->native_pme;
  606. vmd_bridge->native_ltr = root_bridge->native_ltr;
  607. vmd_bridge->native_dpc = root_bridge->native_dpc;
  608. }
  609. /*
  610. * Enable ASPM and LTR settings on devices that aren't configured by BIOS.
  611. */
  612. static int vmd_pm_enable_quirk(struct pci_dev *pdev, void *userdata)
  613. {
  614. unsigned long features = *(unsigned long *)userdata;
  615. u16 ltr = VMD_BIOS_PM_QUIRK_LTR;
  616. u32 ltr_reg;
  617. int pos;
  618. if (!(features & VMD_FEAT_BIOS_PM_QUIRK))
  619. return 0;
  620. pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_LTR);
  621. if (!pos)
  622. goto out_state_change;
  623. /*
  624. * Skip if the max snoop LTR is non-zero, indicating BIOS has set it
  625. * so the LTR quirk is not needed.
  626. */
  627. pci_read_config_dword(pdev, pos + PCI_LTR_MAX_SNOOP_LAT, &ltr_reg);
  628. if (!!(ltr_reg & (PCI_LTR_VALUE_MASK | PCI_LTR_SCALE_MASK)))
  629. goto out_state_change;
  630. /*
  631. * Set the default values to the maximum required by the platform to
  632. * allow the deepest power management savings. Write as a DWORD where
  633. * the lower word is the max snoop latency and the upper word is the
  634. * max non-snoop latency.
  635. */
  636. ltr_reg = (ltr << 16) | ltr;
  637. pci_write_config_dword(pdev, pos + PCI_LTR_MAX_SNOOP_LAT, ltr_reg);
  638. pci_info(pdev, "VMD: Default LTR value set by driver\n");
  639. out_state_change:
  640. /*
  641. * Ensure devices are in D0 before enabling PCI-PM L1 PM Substates, per
  642. * PCIe r6.0, sec 5.5.4.
  643. */
  644. pci_set_power_state_locked(pdev, PCI_D0);
  645. pci_enable_link_state_locked(pdev, PCIE_LINK_STATE_ALL);
  646. return 0;
  647. }
  648. static int vmd_enable_domain(struct vmd_dev *vmd, unsigned long features)
  649. {
  650. struct pci_sysdata *sd = &vmd->sysdata;
  651. struct resource *res;
  652. u32 upper_bits;
  653. unsigned long flags;
  654. LIST_HEAD(resources);
  655. resource_size_t offset[2] = {0};
  656. resource_size_t membar2_offset = 0x2000;
  657. struct pci_bus *child;
  658. struct pci_dev *dev;
  659. int ret;
  660. /*
  661. * Shadow registers may exist in certain VMD device ids which allow
  662. * guests to correctly assign host physical addresses to the root ports
  663. * and child devices. These registers will either return the host value
  664. * or 0, depending on an enable bit in the VMD device.
  665. */
  666. if (features & VMD_FEAT_HAS_MEMBAR_SHADOW) {
  667. membar2_offset = MB2_SHADOW_OFFSET + MB2_SHADOW_SIZE;
  668. ret = vmd_get_phys_offsets(vmd, true, &offset[0], &offset[1]);
  669. if (ret)
  670. return ret;
  671. } else if (features & VMD_FEAT_HAS_MEMBAR_SHADOW_VSCAP) {
  672. ret = vmd_get_phys_offsets(vmd, false, &offset[0], &offset[1]);
  673. if (ret)
  674. return ret;
  675. }
  676. /*
  677. * Certain VMD devices may have a root port configuration option which
  678. * limits the bus range to between 0-127, 128-255, or 224-255
  679. */
  680. if (features & VMD_FEAT_HAS_BUS_RESTRICTIONS) {
  681. ret = vmd_get_bus_number_start(vmd);
  682. if (ret)
  683. return ret;
  684. }
  685. res = &vmd->dev->resource[VMD_CFGBAR];
  686. vmd->resources[0] = (struct resource) {
  687. .name = "VMD CFGBAR",
  688. .start = vmd->busn_start,
  689. .end = vmd->busn_start + (resource_size(res) >> 20) - 1,
  690. .flags = IORESOURCE_BUS | IORESOURCE_PCI_FIXED,
  691. };
  692. /*
  693. * If the window is below 4GB, clear IORESOURCE_MEM_64 so we can
  694. * put 32-bit resources in the window.
  695. *
  696. * There's no hardware reason why a 64-bit window *couldn't*
  697. * contain a 32-bit resource, but pbus_size_mem() computes the
  698. * bridge window size assuming a 64-bit window will contain no
  699. * 32-bit resources. __pci_assign_resource() enforces that
  700. * artificial restriction to make sure everything will fit.
  701. *
  702. * The only way we could use a 64-bit non-prefetchable MEMBAR is
  703. * if its address is <4GB so that we can convert it to a 32-bit
  704. * resource. To be visible to the host OS, all VMD endpoints must
  705. * be initially configured by platform BIOS, which includes setting
  706. * up these resources. We can assume the device is configured
  707. * according to the platform needs.
  708. */
  709. res = &vmd->dev->resource[VMD_MEMBAR1];
  710. upper_bits = upper_32_bits(res->end);
  711. flags = res->flags & ~IORESOURCE_SIZEALIGN;
  712. if (!upper_bits)
  713. flags &= ~IORESOURCE_MEM_64;
  714. vmd->resources[1] = (struct resource) {
  715. .name = "VMD MEMBAR1",
  716. .start = res->start,
  717. .end = res->end,
  718. .flags = flags,
  719. .parent = res,
  720. };
  721. res = &vmd->dev->resource[VMD_MEMBAR2];
  722. upper_bits = upper_32_bits(res->end);
  723. flags = res->flags & ~IORESOURCE_SIZEALIGN;
  724. if (!upper_bits)
  725. flags &= ~IORESOURCE_MEM_64;
  726. vmd->resources[2] = (struct resource) {
  727. .name = "VMD MEMBAR2",
  728. .start = res->start + membar2_offset,
  729. .end = res->end,
  730. .flags = flags,
  731. .parent = res,
  732. };
  733. sd->vmd_dev = vmd->dev;
  734. sd->domain = vmd_find_free_domain();
  735. if (sd->domain < 0)
  736. return sd->domain;
  737. sd->node = pcibus_to_node(vmd->dev->bus);
  738. /*
  739. * Currently MSI remapping must be enabled in guest passthrough mode
  740. * due to some missing interrupt remapping plumbing. This is probably
  741. * acceptable because the guest is usually CPU-limited and MSI
  742. * remapping doesn't become a performance bottleneck.
  743. */
  744. if (!(features & VMD_FEAT_CAN_BYPASS_MSI_REMAP) ||
  745. offset[0] || offset[1]) {
  746. ret = vmd_alloc_irqs(vmd);
  747. if (ret)
  748. return ret;
  749. vmd_set_msi_remapping(vmd, true);
  750. ret = vmd_create_irq_domain(vmd);
  751. if (ret)
  752. return ret;
  753. /*
  754. * Override the IRQ domain bus token so the domain can be
  755. * distinguished from a regular PCI/MSI domain.
  756. */
  757. irq_domain_update_bus_token(vmd->irq_domain, DOMAIN_BUS_VMD_MSI);
  758. } else {
  759. vmd_set_msi_remapping(vmd, false);
  760. }
  761. pci_add_resource(&resources, &vmd->resources[0]);
  762. pci_add_resource_offset(&resources, &vmd->resources[1], offset[0]);
  763. pci_add_resource_offset(&resources, &vmd->resources[2], offset[1]);
  764. vmd->bus = pci_create_root_bus(&vmd->dev->dev, vmd->busn_start,
  765. &vmd_ops, sd, &resources);
  766. if (!vmd->bus) {
  767. pci_free_resource_list(&resources);
  768. vmd_remove_irq_domain(vmd);
  769. return -ENODEV;
  770. }
  771. vmd_copy_host_bridge_flags(pci_find_host_bridge(vmd->dev->bus),
  772. to_pci_host_bridge(vmd->bus->bridge));
  773. vmd_attach_resources(vmd);
  774. if (vmd->irq_domain)
  775. dev_set_msi_domain(&vmd->bus->dev, vmd->irq_domain);
  776. else
  777. dev_set_msi_domain(&vmd->bus->dev,
  778. dev_get_msi_domain(&vmd->dev->dev));
  779. WARN(sysfs_create_link(&vmd->dev->dev.kobj, &vmd->bus->dev.kobj,
  780. "domain"), "Can't create symlink to domain\n");
  781. vmd_acpi_begin();
  782. pci_scan_child_bus(vmd->bus);
  783. vmd_domain_reset(vmd);
  784. /* When Intel VMD is enabled, the OS does not discover the Root Ports
  785. * owned by Intel VMD within the MMCFG space. pci_reset_bus() applies
  786. * a reset to the parent of the PCI device supplied as argument. This
  787. * is why we pass a child device, so the reset can be triggered at
  788. * the Intel bridge level and propagated to all the children in the
  789. * hierarchy.
  790. */
  791. list_for_each_entry(child, &vmd->bus->children, node) {
  792. if (!list_empty(&child->devices)) {
  793. dev = list_first_entry(&child->devices,
  794. struct pci_dev, bus_list);
  795. ret = pci_reset_bus(dev);
  796. if (ret)
  797. pci_warn(dev, "can't reset device: %d\n", ret);
  798. break;
  799. }
  800. }
  801. pci_assign_unassigned_bus_resources(vmd->bus);
  802. pci_walk_bus(vmd->bus, vmd_pm_enable_quirk, &features);
  803. /*
  804. * VMD root buses are virtual and don't return true on pci_is_pcie()
  805. * and will fail pcie_bus_configure_settings() early. It can instead be
  806. * run on each of the real root ports.
  807. */
  808. list_for_each_entry(child, &vmd->bus->children, node)
  809. pcie_bus_configure_settings(child);
  810. pci_bus_add_devices(vmd->bus);
  811. vmd_acpi_end();
  812. return 0;
  813. }
  814. static int vmd_probe(struct pci_dev *dev, const struct pci_device_id *id)
  815. {
  816. unsigned long features = (unsigned long) id->driver_data;
  817. struct vmd_dev *vmd;
  818. int err;
  819. if (resource_size(&dev->resource[VMD_CFGBAR]) < (1 << 20))
  820. return -ENOMEM;
  821. vmd = devm_kzalloc(&dev->dev, sizeof(*vmd), GFP_KERNEL);
  822. if (!vmd)
  823. return -ENOMEM;
  824. vmd->dev = dev;
  825. vmd->instance = ida_alloc(&vmd_instance_ida, GFP_KERNEL);
  826. if (vmd->instance < 0)
  827. return vmd->instance;
  828. vmd->name = devm_kasprintf(&dev->dev, GFP_KERNEL, "vmd%d",
  829. vmd->instance);
  830. if (!vmd->name) {
  831. err = -ENOMEM;
  832. goto out_release_instance;
  833. }
  834. err = pcim_enable_device(dev);
  835. if (err < 0)
  836. goto out_release_instance;
  837. vmd->cfgbar = pcim_iomap(dev, VMD_CFGBAR, 0);
  838. if (!vmd->cfgbar) {
  839. err = -ENOMEM;
  840. goto out_release_instance;
  841. }
  842. pci_set_master(dev);
  843. if (dma_set_mask_and_coherent(&dev->dev, DMA_BIT_MASK(64)) &&
  844. dma_set_mask_and_coherent(&dev->dev, DMA_BIT_MASK(32))) {
  845. err = -ENODEV;
  846. goto out_release_instance;
  847. }
  848. if (features & VMD_FEAT_OFFSET_FIRST_VECTOR)
  849. vmd->first_vec = 1;
  850. spin_lock_init(&vmd->cfg_lock);
  851. pci_set_drvdata(dev, vmd);
  852. err = vmd_enable_domain(vmd, features);
  853. if (err)
  854. goto out_release_instance;
  855. dev_info(&vmd->dev->dev, "Bound to PCI domain %04x\n",
  856. vmd->sysdata.domain);
  857. return 0;
  858. out_release_instance:
  859. ida_free(&vmd_instance_ida, vmd->instance);
  860. return err;
  861. }
  862. static void vmd_cleanup_srcu(struct vmd_dev *vmd)
  863. {
  864. int i;
  865. for (i = 0; i < vmd->msix_count; i++)
  866. cleanup_srcu_struct(&vmd->irqs[i].srcu);
  867. }
  868. static void vmd_remove(struct pci_dev *dev)
  869. {
  870. struct vmd_dev *vmd = pci_get_drvdata(dev);
  871. pci_stop_root_bus(vmd->bus);
  872. sysfs_remove_link(&vmd->dev->dev.kobj, "domain");
  873. pci_remove_root_bus(vmd->bus);
  874. vmd_cleanup_srcu(vmd);
  875. vmd_detach_resources(vmd);
  876. vmd_remove_irq_domain(vmd);
  877. ida_free(&vmd_instance_ida, vmd->instance);
  878. }
  879. static void vmd_shutdown(struct pci_dev *dev)
  880. {
  881. struct vmd_dev *vmd = pci_get_drvdata(dev);
  882. vmd_remove_irq_domain(vmd);
  883. }
  884. #ifdef CONFIG_PM_SLEEP
  885. static int vmd_suspend(struct device *dev)
  886. {
  887. struct pci_dev *pdev = to_pci_dev(dev);
  888. struct vmd_dev *vmd = pci_get_drvdata(pdev);
  889. int i;
  890. for (i = 0; i < vmd->msix_count; i++)
  891. devm_free_irq(dev, vmd->irqs[i].virq, &vmd->irqs[i]);
  892. return 0;
  893. }
  894. static int vmd_resume(struct device *dev)
  895. {
  896. struct pci_dev *pdev = to_pci_dev(dev);
  897. struct vmd_dev *vmd = pci_get_drvdata(pdev);
  898. int err, i;
  899. vmd_set_msi_remapping(vmd, !!vmd->irq_domain);
  900. for (i = 0; i < vmd->msix_count; i++) {
  901. err = devm_request_irq(dev, vmd->irqs[i].virq,
  902. vmd_irq, IRQF_NO_THREAD,
  903. vmd->name, &vmd->irqs[i]);
  904. if (err)
  905. return err;
  906. }
  907. return 0;
  908. }
  909. #endif
  910. static SIMPLE_DEV_PM_OPS(vmd_dev_pm_ops, vmd_suspend, vmd_resume);
  911. static const struct pci_device_id vmd_ids[] = {
  912. {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_VMD_201D),
  913. .driver_data = VMD_FEAT_HAS_MEMBAR_SHADOW_VSCAP,},
  914. {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_VMD_28C0),
  915. .driver_data = VMD_FEAT_HAS_MEMBAR_SHADOW |
  916. VMD_FEAT_HAS_BUS_RESTRICTIONS |
  917. VMD_FEAT_CAN_BYPASS_MSI_REMAP,},
  918. {PCI_VDEVICE(INTEL, 0x467f),
  919. .driver_data = VMD_FEATS_CLIENT,},
  920. {PCI_VDEVICE(INTEL, 0x4c3d),
  921. .driver_data = VMD_FEATS_CLIENT,},
  922. {PCI_VDEVICE(INTEL, 0xa77f),
  923. .driver_data = VMD_FEATS_CLIENT,},
  924. {PCI_VDEVICE(INTEL, 0x7d0b),
  925. .driver_data = VMD_FEATS_CLIENT,},
  926. {PCI_VDEVICE(INTEL, 0xad0b),
  927. .driver_data = VMD_FEATS_CLIENT,},
  928. {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_VMD_9A0B),
  929. .driver_data = VMD_FEATS_CLIENT,},
  930. {PCI_VDEVICE(INTEL, 0xb60b),
  931. .driver_data = VMD_FEATS_CLIENT,},
  932. {PCI_VDEVICE(INTEL, 0xb06f),
  933. .driver_data = VMD_FEATS_CLIENT,},
  934. {0,}
  935. };
  936. MODULE_DEVICE_TABLE(pci, vmd_ids);
  937. static struct pci_driver vmd_drv = {
  938. .name = "vmd",
  939. .id_table = vmd_ids,
  940. .probe = vmd_probe,
  941. .remove = vmd_remove,
  942. .shutdown = vmd_shutdown,
  943. .driver = {
  944. .pm = &vmd_dev_pm_ops,
  945. },
  946. };
  947. module_pci_driver(vmd_drv);
  948. MODULE_AUTHOR("Intel Corporation");
  949. MODULE_DESCRIPTION("Volume Management Device driver");
  950. MODULE_LICENSE("GPL v2");
  951. MODULE_VERSION("0.6");