vmd.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877
  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/srcu.h>
  14. #include <linux/rculist.h>
  15. #include <linux/rcupdate.h>
  16. #include <asm/irqdomain.h>
  17. #include <asm/device.h>
  18. #include <asm/msi.h>
  19. #include <asm/msidef.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 PCI_REG_VMLOCK 0x70
  28. #define MB2_SHADOW_EN(vmlock) (vmlock & 0x2)
  29. enum vmd_features {
  30. /*
  31. * Device may contain registers which hint the physical location of the
  32. * membars, in order to allow proper address translation during
  33. * resource assignment to enable guest virtualization
  34. */
  35. VMD_FEAT_HAS_MEMBAR_SHADOW = (1 << 0),
  36. /*
  37. * Device may provide root port configuration information which limits
  38. * bus numbering
  39. */
  40. VMD_FEAT_HAS_BUS_RESTRICTIONS = (1 << 1),
  41. };
  42. /*
  43. * Lock for manipulating VMD IRQ lists.
  44. */
  45. static DEFINE_RAW_SPINLOCK(list_lock);
  46. /**
  47. * struct vmd_irq - private data to map driver IRQ to the VMD shared vector
  48. * @node: list item for parent traversal.
  49. * @irq: back pointer to parent.
  50. * @enabled: true if driver enabled IRQ
  51. * @virq: the virtual IRQ value provided to the requesting driver.
  52. *
  53. * Every MSI/MSI-X IRQ requested for a device in a VMD domain will be mapped to
  54. * a VMD IRQ using this structure.
  55. */
  56. struct vmd_irq {
  57. struct list_head node;
  58. struct vmd_irq_list *irq;
  59. bool enabled;
  60. unsigned int virq;
  61. };
  62. /**
  63. * struct vmd_irq_list - list of driver requested IRQs mapping to a VMD vector
  64. * @irq_list: the list of irq's the VMD one demuxes to.
  65. * @srcu: SRCU struct for local synchronization.
  66. * @count: number of child IRQs assigned to this vector; used to track
  67. * sharing.
  68. */
  69. struct vmd_irq_list {
  70. struct list_head irq_list;
  71. struct srcu_struct srcu;
  72. unsigned int count;
  73. };
  74. struct vmd_dev {
  75. struct pci_dev *dev;
  76. spinlock_t cfg_lock;
  77. char __iomem *cfgbar;
  78. int msix_count;
  79. struct vmd_irq_list *irqs;
  80. struct pci_sysdata sysdata;
  81. struct resource resources[3];
  82. struct irq_domain *irq_domain;
  83. struct pci_bus *bus;
  84. #ifdef CONFIG_X86_DEV_DMA_OPS
  85. struct dma_map_ops dma_ops;
  86. struct dma_domain dma_domain;
  87. #endif
  88. };
  89. static inline struct vmd_dev *vmd_from_bus(struct pci_bus *bus)
  90. {
  91. return container_of(bus->sysdata, struct vmd_dev, sysdata);
  92. }
  93. static inline unsigned int index_from_irqs(struct vmd_dev *vmd,
  94. struct vmd_irq_list *irqs)
  95. {
  96. return irqs - vmd->irqs;
  97. }
  98. /*
  99. * Drivers managing a device in a VMD domain allocate their own IRQs as before,
  100. * but the MSI entry for the hardware it's driving will be programmed with a
  101. * destination ID for the VMD MSI-X table. The VMD muxes interrupts in its
  102. * domain into one of its own, and the VMD driver de-muxes these for the
  103. * handlers sharing that VMD IRQ. The vmd irq_domain provides the operations
  104. * and irq_chip to set this up.
  105. */
  106. static void vmd_compose_msi_msg(struct irq_data *data, struct msi_msg *msg)
  107. {
  108. struct vmd_irq *vmdirq = data->chip_data;
  109. struct vmd_irq_list *irq = vmdirq->irq;
  110. struct vmd_dev *vmd = irq_data_get_irq_handler_data(data);
  111. msg->address_hi = MSI_ADDR_BASE_HI;
  112. msg->address_lo = MSI_ADDR_BASE_LO |
  113. MSI_ADDR_DEST_ID(index_from_irqs(vmd, irq));
  114. msg->data = 0;
  115. }
  116. /*
  117. * We rely on MSI_FLAG_USE_DEF_CHIP_OPS to set the IRQ mask/unmask ops.
  118. */
  119. static void vmd_irq_enable(struct irq_data *data)
  120. {
  121. struct vmd_irq *vmdirq = data->chip_data;
  122. unsigned long flags;
  123. raw_spin_lock_irqsave(&list_lock, flags);
  124. WARN_ON(vmdirq->enabled);
  125. list_add_tail_rcu(&vmdirq->node, &vmdirq->irq->irq_list);
  126. vmdirq->enabled = true;
  127. raw_spin_unlock_irqrestore(&list_lock, flags);
  128. data->chip->irq_unmask(data);
  129. }
  130. static void vmd_irq_disable(struct irq_data *data)
  131. {
  132. struct vmd_irq *vmdirq = data->chip_data;
  133. unsigned long flags;
  134. data->chip->irq_mask(data);
  135. raw_spin_lock_irqsave(&list_lock, flags);
  136. if (vmdirq->enabled) {
  137. list_del_rcu(&vmdirq->node);
  138. vmdirq->enabled = false;
  139. }
  140. raw_spin_unlock_irqrestore(&list_lock, flags);
  141. }
  142. /*
  143. * XXX: Stubbed until we develop acceptable way to not create conflicts with
  144. * other devices sharing the same vector.
  145. */
  146. static int vmd_irq_set_affinity(struct irq_data *data,
  147. const struct cpumask *dest, bool force)
  148. {
  149. return -EINVAL;
  150. }
  151. static struct irq_chip vmd_msi_controller = {
  152. .name = "VMD-MSI",
  153. .irq_enable = vmd_irq_enable,
  154. .irq_disable = vmd_irq_disable,
  155. .irq_compose_msi_msg = vmd_compose_msi_msg,
  156. .irq_set_affinity = vmd_irq_set_affinity,
  157. };
  158. static irq_hw_number_t vmd_get_hwirq(struct msi_domain_info *info,
  159. msi_alloc_info_t *arg)
  160. {
  161. return 0;
  162. }
  163. /*
  164. * XXX: We can be even smarter selecting the best IRQ once we solve the
  165. * affinity problem.
  166. */
  167. static struct vmd_irq_list *vmd_next_irq(struct vmd_dev *vmd, struct msi_desc *desc)
  168. {
  169. int i, best = 1;
  170. unsigned long flags;
  171. if (vmd->msix_count == 1)
  172. return &vmd->irqs[0];
  173. /*
  174. * White list for fast-interrupt handlers. All others will share the
  175. * "slow" interrupt vector.
  176. */
  177. switch (msi_desc_to_pci_dev(desc)->class) {
  178. case PCI_CLASS_STORAGE_EXPRESS:
  179. break;
  180. default:
  181. return &vmd->irqs[0];
  182. }
  183. raw_spin_lock_irqsave(&list_lock, flags);
  184. for (i = 1; i < vmd->msix_count; i++)
  185. if (vmd->irqs[i].count < vmd->irqs[best].count)
  186. best = i;
  187. vmd->irqs[best].count++;
  188. raw_spin_unlock_irqrestore(&list_lock, flags);
  189. return &vmd->irqs[best];
  190. }
  191. static int vmd_msi_init(struct irq_domain *domain, struct msi_domain_info *info,
  192. unsigned int virq, irq_hw_number_t hwirq,
  193. msi_alloc_info_t *arg)
  194. {
  195. struct msi_desc *desc = arg->desc;
  196. struct vmd_dev *vmd = vmd_from_bus(msi_desc_to_pci_dev(desc)->bus);
  197. struct vmd_irq *vmdirq = kzalloc(sizeof(*vmdirq), GFP_KERNEL);
  198. unsigned int index, vector;
  199. if (!vmdirq)
  200. return -ENOMEM;
  201. INIT_LIST_HEAD(&vmdirq->node);
  202. vmdirq->irq = vmd_next_irq(vmd, desc);
  203. vmdirq->virq = virq;
  204. index = index_from_irqs(vmd, vmdirq->irq);
  205. vector = pci_irq_vector(vmd->dev, index);
  206. irq_domain_set_info(domain, virq, vector, info->chip, vmdirq,
  207. handle_untracked_irq, vmd, NULL);
  208. return 0;
  209. }
  210. static void vmd_msi_free(struct irq_domain *domain,
  211. struct msi_domain_info *info, unsigned int virq)
  212. {
  213. struct vmd_irq *vmdirq = irq_get_chip_data(virq);
  214. unsigned long flags;
  215. synchronize_srcu(&vmdirq->irq->srcu);
  216. /* XXX: Potential optimization to rebalance */
  217. raw_spin_lock_irqsave(&list_lock, flags);
  218. vmdirq->irq->count--;
  219. raw_spin_unlock_irqrestore(&list_lock, flags);
  220. kfree(vmdirq);
  221. }
  222. static int vmd_msi_prepare(struct irq_domain *domain, struct device *dev,
  223. int nvec, msi_alloc_info_t *arg)
  224. {
  225. struct pci_dev *pdev = to_pci_dev(dev);
  226. struct vmd_dev *vmd = vmd_from_bus(pdev->bus);
  227. if (nvec > vmd->msix_count)
  228. return vmd->msix_count;
  229. memset(arg, 0, sizeof(*arg));
  230. return 0;
  231. }
  232. static void vmd_set_desc(msi_alloc_info_t *arg, struct msi_desc *desc)
  233. {
  234. arg->desc = desc;
  235. }
  236. static struct msi_domain_ops vmd_msi_domain_ops = {
  237. .get_hwirq = vmd_get_hwirq,
  238. .msi_init = vmd_msi_init,
  239. .msi_free = vmd_msi_free,
  240. .msi_prepare = vmd_msi_prepare,
  241. .set_desc = vmd_set_desc,
  242. };
  243. static struct msi_domain_info vmd_msi_domain_info = {
  244. .flags = MSI_FLAG_USE_DEF_DOM_OPS | MSI_FLAG_USE_DEF_CHIP_OPS |
  245. MSI_FLAG_PCI_MSIX,
  246. .ops = &vmd_msi_domain_ops,
  247. .chip = &vmd_msi_controller,
  248. };
  249. #ifdef CONFIG_X86_DEV_DMA_OPS
  250. /*
  251. * VMD replaces the requester ID with its own. DMA mappings for devices in a
  252. * VMD domain need to be mapped for the VMD, not the device requiring
  253. * the mapping.
  254. */
  255. static struct device *to_vmd_dev(struct device *dev)
  256. {
  257. struct pci_dev *pdev = to_pci_dev(dev);
  258. struct vmd_dev *vmd = vmd_from_bus(pdev->bus);
  259. return &vmd->dev->dev;
  260. }
  261. static const struct dma_map_ops *vmd_dma_ops(struct device *dev)
  262. {
  263. return get_dma_ops(to_vmd_dev(dev));
  264. }
  265. static void *vmd_alloc(struct device *dev, size_t size, dma_addr_t *addr,
  266. gfp_t flag, unsigned long attrs)
  267. {
  268. return vmd_dma_ops(dev)->alloc(to_vmd_dev(dev), size, addr, flag,
  269. attrs);
  270. }
  271. static void vmd_free(struct device *dev, size_t size, void *vaddr,
  272. dma_addr_t addr, unsigned long attrs)
  273. {
  274. return vmd_dma_ops(dev)->free(to_vmd_dev(dev), size, vaddr, addr,
  275. attrs);
  276. }
  277. static int vmd_mmap(struct device *dev, struct vm_area_struct *vma,
  278. void *cpu_addr, dma_addr_t addr, size_t size,
  279. unsigned long attrs)
  280. {
  281. return vmd_dma_ops(dev)->mmap(to_vmd_dev(dev), vma, cpu_addr, addr,
  282. size, attrs);
  283. }
  284. static int vmd_get_sgtable(struct device *dev, struct sg_table *sgt,
  285. void *cpu_addr, dma_addr_t addr, size_t size,
  286. unsigned long attrs)
  287. {
  288. return vmd_dma_ops(dev)->get_sgtable(to_vmd_dev(dev), sgt, cpu_addr,
  289. addr, size, attrs);
  290. }
  291. static dma_addr_t vmd_map_page(struct device *dev, struct page *page,
  292. unsigned long offset, size_t size,
  293. enum dma_data_direction dir,
  294. unsigned long attrs)
  295. {
  296. return vmd_dma_ops(dev)->map_page(to_vmd_dev(dev), page, offset, size,
  297. dir, attrs);
  298. }
  299. static void vmd_unmap_page(struct device *dev, dma_addr_t addr, size_t size,
  300. enum dma_data_direction dir, unsigned long attrs)
  301. {
  302. vmd_dma_ops(dev)->unmap_page(to_vmd_dev(dev), addr, size, dir, attrs);
  303. }
  304. static int vmd_map_sg(struct device *dev, struct scatterlist *sg, int nents,
  305. enum dma_data_direction dir, unsigned long attrs)
  306. {
  307. return vmd_dma_ops(dev)->map_sg(to_vmd_dev(dev), sg, nents, dir, attrs);
  308. }
  309. static void vmd_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,
  310. enum dma_data_direction dir, unsigned long attrs)
  311. {
  312. vmd_dma_ops(dev)->unmap_sg(to_vmd_dev(dev), sg, nents, dir, attrs);
  313. }
  314. static void vmd_sync_single_for_cpu(struct device *dev, dma_addr_t addr,
  315. size_t size, enum dma_data_direction dir)
  316. {
  317. vmd_dma_ops(dev)->sync_single_for_cpu(to_vmd_dev(dev), addr, size, dir);
  318. }
  319. static void vmd_sync_single_for_device(struct device *dev, dma_addr_t addr,
  320. size_t size, enum dma_data_direction dir)
  321. {
  322. vmd_dma_ops(dev)->sync_single_for_device(to_vmd_dev(dev), addr, size,
  323. dir);
  324. }
  325. static void vmd_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
  326. int nents, enum dma_data_direction dir)
  327. {
  328. vmd_dma_ops(dev)->sync_sg_for_cpu(to_vmd_dev(dev), sg, nents, dir);
  329. }
  330. static void vmd_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
  331. int nents, enum dma_data_direction dir)
  332. {
  333. vmd_dma_ops(dev)->sync_sg_for_device(to_vmd_dev(dev), sg, nents, dir);
  334. }
  335. static int vmd_mapping_error(struct device *dev, dma_addr_t addr)
  336. {
  337. return vmd_dma_ops(dev)->mapping_error(to_vmd_dev(dev), addr);
  338. }
  339. static int vmd_dma_supported(struct device *dev, u64 mask)
  340. {
  341. return vmd_dma_ops(dev)->dma_supported(to_vmd_dev(dev), mask);
  342. }
  343. static u64 vmd_get_required_mask(struct device *dev)
  344. {
  345. return vmd_dma_ops(dev)->get_required_mask(to_vmd_dev(dev));
  346. }
  347. static void vmd_teardown_dma_ops(struct vmd_dev *vmd)
  348. {
  349. struct dma_domain *domain = &vmd->dma_domain;
  350. if (get_dma_ops(&vmd->dev->dev))
  351. del_dma_domain(domain);
  352. }
  353. #define ASSIGN_VMD_DMA_OPS(source, dest, fn) \
  354. do { \
  355. if (source->fn) \
  356. dest->fn = vmd_##fn; \
  357. } while (0)
  358. static void vmd_setup_dma_ops(struct vmd_dev *vmd)
  359. {
  360. const struct dma_map_ops *source = get_dma_ops(&vmd->dev->dev);
  361. struct dma_map_ops *dest = &vmd->dma_ops;
  362. struct dma_domain *domain = &vmd->dma_domain;
  363. domain->domain_nr = vmd->sysdata.domain;
  364. domain->dma_ops = dest;
  365. if (!source)
  366. return;
  367. ASSIGN_VMD_DMA_OPS(source, dest, alloc);
  368. ASSIGN_VMD_DMA_OPS(source, dest, free);
  369. ASSIGN_VMD_DMA_OPS(source, dest, mmap);
  370. ASSIGN_VMD_DMA_OPS(source, dest, get_sgtable);
  371. ASSIGN_VMD_DMA_OPS(source, dest, map_page);
  372. ASSIGN_VMD_DMA_OPS(source, dest, unmap_page);
  373. ASSIGN_VMD_DMA_OPS(source, dest, map_sg);
  374. ASSIGN_VMD_DMA_OPS(source, dest, unmap_sg);
  375. ASSIGN_VMD_DMA_OPS(source, dest, sync_single_for_cpu);
  376. ASSIGN_VMD_DMA_OPS(source, dest, sync_single_for_device);
  377. ASSIGN_VMD_DMA_OPS(source, dest, sync_sg_for_cpu);
  378. ASSIGN_VMD_DMA_OPS(source, dest, sync_sg_for_device);
  379. ASSIGN_VMD_DMA_OPS(source, dest, mapping_error);
  380. ASSIGN_VMD_DMA_OPS(source, dest, dma_supported);
  381. ASSIGN_VMD_DMA_OPS(source, dest, get_required_mask);
  382. add_dma_domain(domain);
  383. }
  384. #undef ASSIGN_VMD_DMA_OPS
  385. #else
  386. static void vmd_teardown_dma_ops(struct vmd_dev *vmd) {}
  387. static void vmd_setup_dma_ops(struct vmd_dev *vmd) {}
  388. #endif
  389. static char __iomem *vmd_cfg_addr(struct vmd_dev *vmd, struct pci_bus *bus,
  390. unsigned int devfn, int reg, int len)
  391. {
  392. char __iomem *addr = vmd->cfgbar +
  393. (bus->number << 20) + (devfn << 12) + reg;
  394. if ((addr - vmd->cfgbar) + len >=
  395. resource_size(&vmd->dev->resource[VMD_CFGBAR]))
  396. return NULL;
  397. return addr;
  398. }
  399. /*
  400. * CPU may deadlock if config space is not serialized on some versions of this
  401. * hardware, so all config space access is done under a spinlock.
  402. */
  403. static int vmd_pci_read(struct pci_bus *bus, unsigned int devfn, int reg,
  404. int len, u32 *value)
  405. {
  406. struct vmd_dev *vmd = vmd_from_bus(bus);
  407. char __iomem *addr = vmd_cfg_addr(vmd, bus, devfn, reg, len);
  408. unsigned long flags;
  409. int ret = 0;
  410. if (!addr)
  411. return -EFAULT;
  412. spin_lock_irqsave(&vmd->cfg_lock, flags);
  413. switch (len) {
  414. case 1:
  415. *value = readb(addr);
  416. break;
  417. case 2:
  418. *value = readw(addr);
  419. break;
  420. case 4:
  421. *value = readl(addr);
  422. break;
  423. default:
  424. ret = -EINVAL;
  425. break;
  426. }
  427. spin_unlock_irqrestore(&vmd->cfg_lock, flags);
  428. return ret;
  429. }
  430. /*
  431. * VMD h/w converts non-posted config writes to posted memory writes. The
  432. * read-back in this function forces the completion so it returns only after
  433. * the config space was written, as expected.
  434. */
  435. static int vmd_pci_write(struct pci_bus *bus, unsigned int devfn, int reg,
  436. int len, u32 value)
  437. {
  438. struct vmd_dev *vmd = vmd_from_bus(bus);
  439. char __iomem *addr = vmd_cfg_addr(vmd, bus, devfn, reg, len);
  440. unsigned long flags;
  441. int ret = 0;
  442. if (!addr)
  443. return -EFAULT;
  444. spin_lock_irqsave(&vmd->cfg_lock, flags);
  445. switch (len) {
  446. case 1:
  447. writeb(value, addr);
  448. readb(addr);
  449. break;
  450. case 2:
  451. writew(value, addr);
  452. readw(addr);
  453. break;
  454. case 4:
  455. writel(value, addr);
  456. readl(addr);
  457. break;
  458. default:
  459. ret = -EINVAL;
  460. break;
  461. }
  462. spin_unlock_irqrestore(&vmd->cfg_lock, flags);
  463. return ret;
  464. }
  465. static struct pci_ops vmd_ops = {
  466. .read = vmd_pci_read,
  467. .write = vmd_pci_write,
  468. };
  469. static void vmd_attach_resources(struct vmd_dev *vmd)
  470. {
  471. vmd->dev->resource[VMD_MEMBAR1].child = &vmd->resources[1];
  472. vmd->dev->resource[VMD_MEMBAR2].child = &vmd->resources[2];
  473. }
  474. static void vmd_detach_resources(struct vmd_dev *vmd)
  475. {
  476. vmd->dev->resource[VMD_MEMBAR1].child = NULL;
  477. vmd->dev->resource[VMD_MEMBAR2].child = NULL;
  478. }
  479. /*
  480. * VMD domains start at 0x10000 to not clash with ACPI _SEG domains.
  481. * Per ACPI r6.0, sec 6.5.6, _SEG returns an integer, of which the lower
  482. * 16 bits are the PCI Segment Group (domain) number. Other bits are
  483. * currently reserved.
  484. */
  485. static int vmd_find_free_domain(void)
  486. {
  487. int domain = 0xffff;
  488. struct pci_bus *bus = NULL;
  489. while ((bus = pci_find_next_bus(bus)) != NULL)
  490. domain = max_t(int, domain, pci_domain_nr(bus));
  491. return domain + 1;
  492. }
  493. static int vmd_enable_domain(struct vmd_dev *vmd, unsigned long features)
  494. {
  495. struct pci_sysdata *sd = &vmd->sysdata;
  496. struct fwnode_handle *fn;
  497. struct resource *res;
  498. u32 upper_bits;
  499. unsigned long flags;
  500. LIST_HEAD(resources);
  501. resource_size_t offset[2] = {0};
  502. resource_size_t membar2_offset = 0x2000, busn_start = 0;
  503. /*
  504. * Shadow registers may exist in certain VMD device ids which allow
  505. * guests to correctly assign host physical addresses to the root ports
  506. * and child devices. These registers will either return the host value
  507. * or 0, depending on an enable bit in the VMD device.
  508. */
  509. if (features & VMD_FEAT_HAS_MEMBAR_SHADOW) {
  510. u32 vmlock;
  511. int ret;
  512. membar2_offset = 0x2018;
  513. ret = pci_read_config_dword(vmd->dev, PCI_REG_VMLOCK, &vmlock);
  514. if (ret || vmlock == ~0)
  515. return -ENODEV;
  516. if (MB2_SHADOW_EN(vmlock)) {
  517. void __iomem *membar2;
  518. membar2 = pci_iomap(vmd->dev, VMD_MEMBAR2, 0);
  519. if (!membar2)
  520. return -ENOMEM;
  521. offset[0] = vmd->dev->resource[VMD_MEMBAR1].start -
  522. readq(membar2 + 0x2008);
  523. offset[1] = vmd->dev->resource[VMD_MEMBAR2].start -
  524. readq(membar2 + 0x2010);
  525. pci_iounmap(vmd->dev, membar2);
  526. }
  527. }
  528. /*
  529. * Certain VMD devices may have a root port configuration option which
  530. * limits the bus range to between 0-127 or 128-255
  531. */
  532. if (features & VMD_FEAT_HAS_BUS_RESTRICTIONS) {
  533. u32 vmcap, vmconfig;
  534. pci_read_config_dword(vmd->dev, PCI_REG_VMCAP, &vmcap);
  535. pci_read_config_dword(vmd->dev, PCI_REG_VMCONFIG, &vmconfig);
  536. if (BUS_RESTRICT_CAP(vmcap) &&
  537. (BUS_RESTRICT_CFG(vmconfig) == 0x1))
  538. busn_start = 128;
  539. }
  540. res = &vmd->dev->resource[VMD_CFGBAR];
  541. vmd->resources[0] = (struct resource) {
  542. .name = "VMD CFGBAR",
  543. .start = busn_start,
  544. .end = busn_start + (resource_size(res) >> 20) - 1,
  545. .flags = IORESOURCE_BUS | IORESOURCE_PCI_FIXED,
  546. };
  547. /*
  548. * If the window is below 4GB, clear IORESOURCE_MEM_64 so we can
  549. * put 32-bit resources in the window.
  550. *
  551. * There's no hardware reason why a 64-bit window *couldn't*
  552. * contain a 32-bit resource, but pbus_size_mem() computes the
  553. * bridge window size assuming a 64-bit window will contain no
  554. * 32-bit resources. __pci_assign_resource() enforces that
  555. * artificial restriction to make sure everything will fit.
  556. *
  557. * The only way we could use a 64-bit non-prefechable MEMBAR is
  558. * if its address is <4GB so that we can convert it to a 32-bit
  559. * resource. To be visible to the host OS, all VMD endpoints must
  560. * be initially configured by platform BIOS, which includes setting
  561. * up these resources. We can assume the device is configured
  562. * according to the platform needs.
  563. */
  564. res = &vmd->dev->resource[VMD_MEMBAR1];
  565. upper_bits = upper_32_bits(res->end);
  566. flags = res->flags & ~IORESOURCE_SIZEALIGN;
  567. if (!upper_bits)
  568. flags &= ~IORESOURCE_MEM_64;
  569. vmd->resources[1] = (struct resource) {
  570. .name = "VMD MEMBAR1",
  571. .start = res->start,
  572. .end = res->end,
  573. .flags = flags,
  574. .parent = res,
  575. };
  576. res = &vmd->dev->resource[VMD_MEMBAR2];
  577. upper_bits = upper_32_bits(res->end);
  578. flags = res->flags & ~IORESOURCE_SIZEALIGN;
  579. if (!upper_bits)
  580. flags &= ~IORESOURCE_MEM_64;
  581. vmd->resources[2] = (struct resource) {
  582. .name = "VMD MEMBAR2",
  583. .start = res->start + membar2_offset,
  584. .end = res->end,
  585. .flags = flags,
  586. .parent = res,
  587. };
  588. sd->vmd_domain = true;
  589. sd->domain = vmd_find_free_domain();
  590. if (sd->domain < 0)
  591. return sd->domain;
  592. sd->node = pcibus_to_node(vmd->dev->bus);
  593. fn = irq_domain_alloc_named_id_fwnode("VMD-MSI", vmd->sysdata.domain);
  594. if (!fn)
  595. return -ENODEV;
  596. vmd->irq_domain = pci_msi_create_irq_domain(fn, &vmd_msi_domain_info,
  597. x86_vector_domain);
  598. irq_domain_free_fwnode(fn);
  599. if (!vmd->irq_domain)
  600. return -ENODEV;
  601. pci_add_resource(&resources, &vmd->resources[0]);
  602. pci_add_resource_offset(&resources, &vmd->resources[1], offset[0]);
  603. pci_add_resource_offset(&resources, &vmd->resources[2], offset[1]);
  604. vmd->bus = pci_create_root_bus(&vmd->dev->dev, busn_start, &vmd_ops,
  605. sd, &resources);
  606. if (!vmd->bus) {
  607. pci_free_resource_list(&resources);
  608. irq_domain_remove(vmd->irq_domain);
  609. return -ENODEV;
  610. }
  611. vmd_attach_resources(vmd);
  612. vmd_setup_dma_ops(vmd);
  613. dev_set_msi_domain(&vmd->bus->dev, vmd->irq_domain);
  614. pci_rescan_bus(vmd->bus);
  615. WARN(sysfs_create_link(&vmd->dev->dev.kobj, &vmd->bus->dev.kobj,
  616. "domain"), "Can't create symlink to domain\n");
  617. return 0;
  618. }
  619. static irqreturn_t vmd_irq(int irq, void *data)
  620. {
  621. struct vmd_irq_list *irqs = data;
  622. struct vmd_irq *vmdirq;
  623. int idx;
  624. idx = srcu_read_lock(&irqs->srcu);
  625. list_for_each_entry_rcu(vmdirq, &irqs->irq_list, node)
  626. generic_handle_irq(vmdirq->virq);
  627. srcu_read_unlock(&irqs->srcu, idx);
  628. return IRQ_HANDLED;
  629. }
  630. static int vmd_probe(struct pci_dev *dev, const struct pci_device_id *id)
  631. {
  632. struct vmd_dev *vmd;
  633. int i, err;
  634. if (resource_size(&dev->resource[VMD_CFGBAR]) < (1 << 20))
  635. return -ENOMEM;
  636. vmd = devm_kzalloc(&dev->dev, sizeof(*vmd), GFP_KERNEL);
  637. if (!vmd)
  638. return -ENOMEM;
  639. vmd->dev = dev;
  640. err = pcim_enable_device(dev);
  641. if (err < 0)
  642. return err;
  643. vmd->cfgbar = pcim_iomap(dev, VMD_CFGBAR, 0);
  644. if (!vmd->cfgbar)
  645. return -ENOMEM;
  646. pci_set_master(dev);
  647. if (dma_set_mask_and_coherent(&dev->dev, DMA_BIT_MASK(64)) &&
  648. dma_set_mask_and_coherent(&dev->dev, DMA_BIT_MASK(32)))
  649. return -ENODEV;
  650. vmd->msix_count = pci_msix_vec_count(dev);
  651. if (vmd->msix_count < 0)
  652. return -ENODEV;
  653. vmd->msix_count = pci_alloc_irq_vectors(dev, 1, vmd->msix_count,
  654. PCI_IRQ_MSIX);
  655. if (vmd->msix_count < 0)
  656. return vmd->msix_count;
  657. vmd->irqs = devm_kcalloc(&dev->dev, vmd->msix_count, sizeof(*vmd->irqs),
  658. GFP_KERNEL);
  659. if (!vmd->irqs)
  660. return -ENOMEM;
  661. for (i = 0; i < vmd->msix_count; i++) {
  662. err = init_srcu_struct(&vmd->irqs[i].srcu);
  663. if (err)
  664. return err;
  665. INIT_LIST_HEAD(&vmd->irqs[i].irq_list);
  666. err = devm_request_irq(&dev->dev, pci_irq_vector(dev, i),
  667. vmd_irq, IRQF_NO_THREAD,
  668. "vmd", &vmd->irqs[i]);
  669. if (err)
  670. return err;
  671. }
  672. spin_lock_init(&vmd->cfg_lock);
  673. pci_set_drvdata(dev, vmd);
  674. err = vmd_enable_domain(vmd, (unsigned long) id->driver_data);
  675. if (err)
  676. return err;
  677. dev_info(&vmd->dev->dev, "Bound to PCI domain %04x\n",
  678. vmd->sysdata.domain);
  679. return 0;
  680. }
  681. static void vmd_cleanup_srcu(struct vmd_dev *vmd)
  682. {
  683. int i;
  684. for (i = 0; i < vmd->msix_count; i++)
  685. cleanup_srcu_struct(&vmd->irqs[i].srcu);
  686. }
  687. static void vmd_remove(struct pci_dev *dev)
  688. {
  689. struct vmd_dev *vmd = pci_get_drvdata(dev);
  690. sysfs_remove_link(&vmd->dev->dev.kobj, "domain");
  691. pci_stop_root_bus(vmd->bus);
  692. pci_remove_root_bus(vmd->bus);
  693. vmd_cleanup_srcu(vmd);
  694. vmd_teardown_dma_ops(vmd);
  695. vmd_detach_resources(vmd);
  696. irq_domain_remove(vmd->irq_domain);
  697. }
  698. #ifdef CONFIG_PM_SLEEP
  699. static int vmd_suspend(struct device *dev)
  700. {
  701. struct pci_dev *pdev = to_pci_dev(dev);
  702. struct vmd_dev *vmd = pci_get_drvdata(pdev);
  703. int i;
  704. for (i = 0; i < vmd->msix_count; i++)
  705. devm_free_irq(dev, pci_irq_vector(pdev, i), &vmd->irqs[i]);
  706. pci_save_state(pdev);
  707. return 0;
  708. }
  709. static int vmd_resume(struct device *dev)
  710. {
  711. struct pci_dev *pdev = to_pci_dev(dev);
  712. struct vmd_dev *vmd = pci_get_drvdata(pdev);
  713. int err, i;
  714. for (i = 0; i < vmd->msix_count; i++) {
  715. err = devm_request_irq(dev, pci_irq_vector(pdev, i),
  716. vmd_irq, IRQF_NO_THREAD,
  717. "vmd", &vmd->irqs[i]);
  718. if (err)
  719. return err;
  720. }
  721. pci_restore_state(pdev);
  722. return 0;
  723. }
  724. #endif
  725. static SIMPLE_DEV_PM_OPS(vmd_dev_pm_ops, vmd_suspend, vmd_resume);
  726. static const struct pci_device_id vmd_ids[] = {
  727. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_VMD_201D),},
  728. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_VMD_28C0),
  729. .driver_data = VMD_FEAT_HAS_MEMBAR_SHADOW |
  730. VMD_FEAT_HAS_BUS_RESTRICTIONS,},
  731. {0,}
  732. };
  733. MODULE_DEVICE_TABLE(pci, vmd_ids);
  734. static struct pci_driver vmd_drv = {
  735. .name = "vmd",
  736. .id_table = vmd_ids,
  737. .probe = vmd_probe,
  738. .remove = vmd_remove,
  739. .driver = {
  740. .pm = &vmd_dev_pm_ops,
  741. },
  742. };
  743. module_pci_driver(vmd_drv);
  744. MODULE_AUTHOR("Intel Corporation");
  745. MODULE_LICENSE("GPL v2");
  746. MODULE_VERSION("0.6");