vmd.c 30 KB

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