vmd.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870
  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 (pci_is_bridge(msi_desc_to_pci_dev(desc)) || vmd->msix_count == 1)
  172. return &vmd->irqs[0];
  173. raw_spin_lock_irqsave(&list_lock, flags);
  174. for (i = 1; i < vmd->msix_count; i++)
  175. if (vmd->irqs[i].count < vmd->irqs[best].count)
  176. best = i;
  177. vmd->irqs[best].count++;
  178. raw_spin_unlock_irqrestore(&list_lock, flags);
  179. return &vmd->irqs[best];
  180. }
  181. static int vmd_msi_init(struct irq_domain *domain, struct msi_domain_info *info,
  182. unsigned int virq, irq_hw_number_t hwirq,
  183. msi_alloc_info_t *arg)
  184. {
  185. struct msi_desc *desc = arg->desc;
  186. struct vmd_dev *vmd = vmd_from_bus(msi_desc_to_pci_dev(desc)->bus);
  187. struct vmd_irq *vmdirq = kzalloc(sizeof(*vmdirq), GFP_KERNEL);
  188. unsigned int index, vector;
  189. if (!vmdirq)
  190. return -ENOMEM;
  191. INIT_LIST_HEAD(&vmdirq->node);
  192. vmdirq->irq = vmd_next_irq(vmd, desc);
  193. vmdirq->virq = virq;
  194. index = index_from_irqs(vmd, vmdirq->irq);
  195. vector = pci_irq_vector(vmd->dev, index);
  196. irq_domain_set_info(domain, virq, vector, info->chip, vmdirq,
  197. handle_untracked_irq, vmd, NULL);
  198. return 0;
  199. }
  200. static void vmd_msi_free(struct irq_domain *domain,
  201. struct msi_domain_info *info, unsigned int virq)
  202. {
  203. struct vmd_irq *vmdirq = irq_get_chip_data(virq);
  204. unsigned long flags;
  205. synchronize_srcu(&vmdirq->irq->srcu);
  206. /* XXX: Potential optimization to rebalance */
  207. raw_spin_lock_irqsave(&list_lock, flags);
  208. vmdirq->irq->count--;
  209. raw_spin_unlock_irqrestore(&list_lock, flags);
  210. kfree(vmdirq);
  211. }
  212. static int vmd_msi_prepare(struct irq_domain *domain, struct device *dev,
  213. int nvec, msi_alloc_info_t *arg)
  214. {
  215. struct pci_dev *pdev = to_pci_dev(dev);
  216. struct vmd_dev *vmd = vmd_from_bus(pdev->bus);
  217. if (nvec > vmd->msix_count)
  218. return vmd->msix_count;
  219. memset(arg, 0, sizeof(*arg));
  220. return 0;
  221. }
  222. static void vmd_set_desc(msi_alloc_info_t *arg, struct msi_desc *desc)
  223. {
  224. arg->desc = desc;
  225. }
  226. static struct msi_domain_ops vmd_msi_domain_ops = {
  227. .get_hwirq = vmd_get_hwirq,
  228. .msi_init = vmd_msi_init,
  229. .msi_free = vmd_msi_free,
  230. .msi_prepare = vmd_msi_prepare,
  231. .set_desc = vmd_set_desc,
  232. };
  233. static struct msi_domain_info vmd_msi_domain_info = {
  234. .flags = MSI_FLAG_USE_DEF_DOM_OPS | MSI_FLAG_USE_DEF_CHIP_OPS |
  235. MSI_FLAG_PCI_MSIX,
  236. .ops = &vmd_msi_domain_ops,
  237. .chip = &vmd_msi_controller,
  238. };
  239. #ifdef CONFIG_X86_DEV_DMA_OPS
  240. /*
  241. * VMD replaces the requester ID with its own. DMA mappings for devices in a
  242. * VMD domain need to be mapped for the VMD, not the device requiring
  243. * the mapping.
  244. */
  245. static struct device *to_vmd_dev(struct device *dev)
  246. {
  247. struct pci_dev *pdev = to_pci_dev(dev);
  248. struct vmd_dev *vmd = vmd_from_bus(pdev->bus);
  249. return &vmd->dev->dev;
  250. }
  251. static const struct dma_map_ops *vmd_dma_ops(struct device *dev)
  252. {
  253. return get_dma_ops(to_vmd_dev(dev));
  254. }
  255. static void *vmd_alloc(struct device *dev, size_t size, dma_addr_t *addr,
  256. gfp_t flag, unsigned long attrs)
  257. {
  258. return vmd_dma_ops(dev)->alloc(to_vmd_dev(dev), size, addr, flag,
  259. attrs);
  260. }
  261. static void vmd_free(struct device *dev, size_t size, void *vaddr,
  262. dma_addr_t addr, unsigned long attrs)
  263. {
  264. return vmd_dma_ops(dev)->free(to_vmd_dev(dev), size, vaddr, addr,
  265. attrs);
  266. }
  267. static int vmd_mmap(struct device *dev, struct vm_area_struct *vma,
  268. void *cpu_addr, dma_addr_t addr, size_t size,
  269. unsigned long attrs)
  270. {
  271. return vmd_dma_ops(dev)->mmap(to_vmd_dev(dev), vma, cpu_addr, addr,
  272. size, attrs);
  273. }
  274. static int vmd_get_sgtable(struct device *dev, struct sg_table *sgt,
  275. void *cpu_addr, dma_addr_t addr, size_t size,
  276. unsigned long attrs)
  277. {
  278. return vmd_dma_ops(dev)->get_sgtable(to_vmd_dev(dev), sgt, cpu_addr,
  279. addr, size, attrs);
  280. }
  281. static dma_addr_t vmd_map_page(struct device *dev, struct page *page,
  282. unsigned long offset, size_t size,
  283. enum dma_data_direction dir,
  284. unsigned long attrs)
  285. {
  286. return vmd_dma_ops(dev)->map_page(to_vmd_dev(dev), page, offset, size,
  287. dir, attrs);
  288. }
  289. static void vmd_unmap_page(struct device *dev, dma_addr_t addr, size_t size,
  290. enum dma_data_direction dir, unsigned long attrs)
  291. {
  292. vmd_dma_ops(dev)->unmap_page(to_vmd_dev(dev), addr, size, dir, attrs);
  293. }
  294. static int vmd_map_sg(struct device *dev, struct scatterlist *sg, int nents,
  295. enum dma_data_direction dir, unsigned long attrs)
  296. {
  297. return vmd_dma_ops(dev)->map_sg(to_vmd_dev(dev), sg, nents, dir, attrs);
  298. }
  299. static void vmd_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,
  300. enum dma_data_direction dir, unsigned long attrs)
  301. {
  302. vmd_dma_ops(dev)->unmap_sg(to_vmd_dev(dev), sg, nents, dir, attrs);
  303. }
  304. static void vmd_sync_single_for_cpu(struct device *dev, dma_addr_t addr,
  305. size_t size, enum dma_data_direction dir)
  306. {
  307. vmd_dma_ops(dev)->sync_single_for_cpu(to_vmd_dev(dev), addr, size, dir);
  308. }
  309. static void vmd_sync_single_for_device(struct device *dev, dma_addr_t addr,
  310. size_t size, enum dma_data_direction dir)
  311. {
  312. vmd_dma_ops(dev)->sync_single_for_device(to_vmd_dev(dev), addr, size,
  313. dir);
  314. }
  315. static void vmd_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
  316. int nents, enum dma_data_direction dir)
  317. {
  318. vmd_dma_ops(dev)->sync_sg_for_cpu(to_vmd_dev(dev), sg, nents, dir);
  319. }
  320. static void vmd_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
  321. int nents, enum dma_data_direction dir)
  322. {
  323. vmd_dma_ops(dev)->sync_sg_for_device(to_vmd_dev(dev), sg, nents, dir);
  324. }
  325. static int vmd_mapping_error(struct device *dev, dma_addr_t addr)
  326. {
  327. return vmd_dma_ops(dev)->mapping_error(to_vmd_dev(dev), addr);
  328. }
  329. static int vmd_dma_supported(struct device *dev, u64 mask)
  330. {
  331. return vmd_dma_ops(dev)->dma_supported(to_vmd_dev(dev), mask);
  332. }
  333. #ifdef ARCH_HAS_DMA_GET_REQUIRED_MASK
  334. static u64 vmd_get_required_mask(struct device *dev)
  335. {
  336. return vmd_dma_ops(dev)->get_required_mask(to_vmd_dev(dev));
  337. }
  338. #endif
  339. static void vmd_teardown_dma_ops(struct vmd_dev *vmd)
  340. {
  341. struct dma_domain *domain = &vmd->dma_domain;
  342. if (get_dma_ops(&vmd->dev->dev))
  343. del_dma_domain(domain);
  344. }
  345. #define ASSIGN_VMD_DMA_OPS(source, dest, fn) \
  346. do { \
  347. if (source->fn) \
  348. dest->fn = vmd_##fn; \
  349. } while (0)
  350. static void vmd_setup_dma_ops(struct vmd_dev *vmd)
  351. {
  352. const struct dma_map_ops *source = get_dma_ops(&vmd->dev->dev);
  353. struct dma_map_ops *dest = &vmd->dma_ops;
  354. struct dma_domain *domain = &vmd->dma_domain;
  355. domain->domain_nr = vmd->sysdata.domain;
  356. domain->dma_ops = dest;
  357. if (!source)
  358. return;
  359. ASSIGN_VMD_DMA_OPS(source, dest, alloc);
  360. ASSIGN_VMD_DMA_OPS(source, dest, free);
  361. ASSIGN_VMD_DMA_OPS(source, dest, mmap);
  362. ASSIGN_VMD_DMA_OPS(source, dest, get_sgtable);
  363. ASSIGN_VMD_DMA_OPS(source, dest, map_page);
  364. ASSIGN_VMD_DMA_OPS(source, dest, unmap_page);
  365. ASSIGN_VMD_DMA_OPS(source, dest, map_sg);
  366. ASSIGN_VMD_DMA_OPS(source, dest, unmap_sg);
  367. ASSIGN_VMD_DMA_OPS(source, dest, sync_single_for_cpu);
  368. ASSIGN_VMD_DMA_OPS(source, dest, sync_single_for_device);
  369. ASSIGN_VMD_DMA_OPS(source, dest, sync_sg_for_cpu);
  370. ASSIGN_VMD_DMA_OPS(source, dest, sync_sg_for_device);
  371. ASSIGN_VMD_DMA_OPS(source, dest, mapping_error);
  372. ASSIGN_VMD_DMA_OPS(source, dest, dma_supported);
  373. #ifdef ARCH_HAS_DMA_GET_REQUIRED_MASK
  374. ASSIGN_VMD_DMA_OPS(source, dest, get_required_mask);
  375. #endif
  376. add_dma_domain(domain);
  377. }
  378. #undef ASSIGN_VMD_DMA_OPS
  379. #else
  380. static void vmd_teardown_dma_ops(struct vmd_dev *vmd) {}
  381. static void vmd_setup_dma_ops(struct vmd_dev *vmd) {}
  382. #endif
  383. static char __iomem *vmd_cfg_addr(struct vmd_dev *vmd, struct pci_bus *bus,
  384. unsigned int devfn, int reg, int len)
  385. {
  386. char __iomem *addr = vmd->cfgbar +
  387. (bus->number << 20) + (devfn << 12) + reg;
  388. if ((addr - vmd->cfgbar) + len >=
  389. resource_size(&vmd->dev->resource[VMD_CFGBAR]))
  390. return NULL;
  391. return addr;
  392. }
  393. /*
  394. * CPU may deadlock if config space is not serialized on some versions of this
  395. * hardware, so all config space access is done under a spinlock.
  396. */
  397. static int vmd_pci_read(struct pci_bus *bus, unsigned int devfn, int reg,
  398. int len, u32 *value)
  399. {
  400. struct vmd_dev *vmd = vmd_from_bus(bus);
  401. char __iomem *addr = vmd_cfg_addr(vmd, bus, devfn, reg, len);
  402. unsigned long flags;
  403. int ret = 0;
  404. if (!addr)
  405. return -EFAULT;
  406. spin_lock_irqsave(&vmd->cfg_lock, flags);
  407. switch (len) {
  408. case 1:
  409. *value = readb(addr);
  410. break;
  411. case 2:
  412. *value = readw(addr);
  413. break;
  414. case 4:
  415. *value = readl(addr);
  416. break;
  417. default:
  418. ret = -EINVAL;
  419. break;
  420. }
  421. spin_unlock_irqrestore(&vmd->cfg_lock, flags);
  422. return ret;
  423. }
  424. /*
  425. * VMD h/w converts non-posted config writes to posted memory writes. The
  426. * read-back in this function forces the completion so it returns only after
  427. * the config space was written, as expected.
  428. */
  429. static int vmd_pci_write(struct pci_bus *bus, unsigned int devfn, int reg,
  430. int len, u32 value)
  431. {
  432. struct vmd_dev *vmd = vmd_from_bus(bus);
  433. char __iomem *addr = vmd_cfg_addr(vmd, bus, devfn, reg, len);
  434. unsigned long flags;
  435. int ret = 0;
  436. if (!addr)
  437. return -EFAULT;
  438. spin_lock_irqsave(&vmd->cfg_lock, flags);
  439. switch (len) {
  440. case 1:
  441. writeb(value, addr);
  442. readb(addr);
  443. break;
  444. case 2:
  445. writew(value, addr);
  446. readw(addr);
  447. break;
  448. case 4:
  449. writel(value, addr);
  450. readl(addr);
  451. break;
  452. default:
  453. ret = -EINVAL;
  454. break;
  455. }
  456. spin_unlock_irqrestore(&vmd->cfg_lock, flags);
  457. return ret;
  458. }
  459. static struct pci_ops vmd_ops = {
  460. .read = vmd_pci_read,
  461. .write = vmd_pci_write,
  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_enable_domain(struct vmd_dev *vmd, unsigned long features)
  488. {
  489. struct pci_sysdata *sd = &vmd->sysdata;
  490. struct fwnode_handle *fn;
  491. struct resource *res;
  492. u32 upper_bits;
  493. unsigned long flags;
  494. LIST_HEAD(resources);
  495. resource_size_t offset[2] = {0};
  496. resource_size_t membar2_offset = 0x2000, busn_start = 0;
  497. /*
  498. * Shadow registers may exist in certain VMD device ids which allow
  499. * guests to correctly assign host physical addresses to the root ports
  500. * and child devices. These registers will either return the host value
  501. * or 0, depending on an enable bit in the VMD device.
  502. */
  503. if (features & VMD_FEAT_HAS_MEMBAR_SHADOW) {
  504. u32 vmlock;
  505. int ret;
  506. membar2_offset = 0x2018;
  507. ret = pci_read_config_dword(vmd->dev, PCI_REG_VMLOCK, &vmlock);
  508. if (ret || vmlock == ~0)
  509. return -ENODEV;
  510. if (MB2_SHADOW_EN(vmlock)) {
  511. void __iomem *membar2;
  512. membar2 = pci_iomap(vmd->dev, VMD_MEMBAR2, 0);
  513. if (!membar2)
  514. return -ENOMEM;
  515. offset[0] = vmd->dev->resource[VMD_MEMBAR1].start -
  516. readq(membar2 + 0x2008);
  517. offset[1] = vmd->dev->resource[VMD_MEMBAR2].start -
  518. readq(membar2 + 0x2010);
  519. pci_iounmap(vmd->dev, membar2);
  520. }
  521. }
  522. /*
  523. * Certain VMD devices may have a root port configuration option which
  524. * limits the bus range to between 0-127 or 128-255
  525. */
  526. if (features & VMD_FEAT_HAS_BUS_RESTRICTIONS) {
  527. u32 vmcap, vmconfig;
  528. pci_read_config_dword(vmd->dev, PCI_REG_VMCAP, &vmcap);
  529. pci_read_config_dword(vmd->dev, PCI_REG_VMCONFIG, &vmconfig);
  530. if (BUS_RESTRICT_CAP(vmcap) &&
  531. (BUS_RESTRICT_CFG(vmconfig) == 0x1))
  532. busn_start = 128;
  533. }
  534. res = &vmd->dev->resource[VMD_CFGBAR];
  535. vmd->resources[0] = (struct resource) {
  536. .name = "VMD CFGBAR",
  537. .start = busn_start,
  538. .end = busn_start + (resource_size(res) >> 20) - 1,
  539. .flags = IORESOURCE_BUS | IORESOURCE_PCI_FIXED,
  540. };
  541. /*
  542. * If the window is below 4GB, clear IORESOURCE_MEM_64 so we can
  543. * put 32-bit resources in the window.
  544. *
  545. * There's no hardware reason why a 64-bit window *couldn't*
  546. * contain a 32-bit resource, but pbus_size_mem() computes the
  547. * bridge window size assuming a 64-bit window will contain no
  548. * 32-bit resources. __pci_assign_resource() enforces that
  549. * artificial restriction to make sure everything will fit.
  550. *
  551. * The only way we could use a 64-bit non-prefechable MEMBAR is
  552. * if its address is <4GB so that we can convert it to a 32-bit
  553. * resource. To be visible to the host OS, all VMD endpoints must
  554. * be initially configured by platform BIOS, which includes setting
  555. * up these resources. We can assume the device is configured
  556. * according to the platform needs.
  557. */
  558. res = &vmd->dev->resource[VMD_MEMBAR1];
  559. upper_bits = upper_32_bits(res->end);
  560. flags = res->flags & ~IORESOURCE_SIZEALIGN;
  561. if (!upper_bits)
  562. flags &= ~IORESOURCE_MEM_64;
  563. vmd->resources[1] = (struct resource) {
  564. .name = "VMD MEMBAR1",
  565. .start = res->start,
  566. .end = res->end,
  567. .flags = flags,
  568. .parent = res,
  569. };
  570. res = &vmd->dev->resource[VMD_MEMBAR2];
  571. upper_bits = upper_32_bits(res->end);
  572. flags = res->flags & ~IORESOURCE_SIZEALIGN;
  573. if (!upper_bits)
  574. flags &= ~IORESOURCE_MEM_64;
  575. vmd->resources[2] = (struct resource) {
  576. .name = "VMD MEMBAR2",
  577. .start = res->start + membar2_offset,
  578. .end = res->end,
  579. .flags = flags,
  580. .parent = res,
  581. };
  582. sd->vmd_domain = true;
  583. sd->domain = vmd_find_free_domain();
  584. if (sd->domain < 0)
  585. return sd->domain;
  586. sd->node = pcibus_to_node(vmd->dev->bus);
  587. fn = irq_domain_alloc_named_id_fwnode("VMD-MSI", vmd->sysdata.domain);
  588. if (!fn)
  589. return -ENODEV;
  590. vmd->irq_domain = pci_msi_create_irq_domain(fn, &vmd_msi_domain_info,
  591. x86_vector_domain);
  592. irq_domain_free_fwnode(fn);
  593. if (!vmd->irq_domain)
  594. return -ENODEV;
  595. pci_add_resource(&resources, &vmd->resources[0]);
  596. pci_add_resource_offset(&resources, &vmd->resources[1], offset[0]);
  597. pci_add_resource_offset(&resources, &vmd->resources[2], offset[1]);
  598. vmd->bus = pci_create_root_bus(&vmd->dev->dev, busn_start, &vmd_ops,
  599. sd, &resources);
  600. if (!vmd->bus) {
  601. pci_free_resource_list(&resources);
  602. irq_domain_remove(vmd->irq_domain);
  603. return -ENODEV;
  604. }
  605. vmd_attach_resources(vmd);
  606. vmd_setup_dma_ops(vmd);
  607. dev_set_msi_domain(&vmd->bus->dev, vmd->irq_domain);
  608. pci_rescan_bus(vmd->bus);
  609. WARN(sysfs_create_link(&vmd->dev->dev.kobj, &vmd->bus->dev.kobj,
  610. "domain"), "Can't create symlink to domain\n");
  611. return 0;
  612. }
  613. static irqreturn_t vmd_irq(int irq, void *data)
  614. {
  615. struct vmd_irq_list *irqs = data;
  616. struct vmd_irq *vmdirq;
  617. int idx;
  618. idx = srcu_read_lock(&irqs->srcu);
  619. list_for_each_entry_rcu(vmdirq, &irqs->irq_list, node)
  620. generic_handle_irq(vmdirq->virq);
  621. srcu_read_unlock(&irqs->srcu, idx);
  622. return IRQ_HANDLED;
  623. }
  624. static int vmd_probe(struct pci_dev *dev, const struct pci_device_id *id)
  625. {
  626. struct vmd_dev *vmd;
  627. int i, err;
  628. if (resource_size(&dev->resource[VMD_CFGBAR]) < (1 << 20))
  629. return -ENOMEM;
  630. vmd = devm_kzalloc(&dev->dev, sizeof(*vmd), GFP_KERNEL);
  631. if (!vmd)
  632. return -ENOMEM;
  633. vmd->dev = dev;
  634. err = pcim_enable_device(dev);
  635. if (err < 0)
  636. return err;
  637. vmd->cfgbar = pcim_iomap(dev, VMD_CFGBAR, 0);
  638. if (!vmd->cfgbar)
  639. return -ENOMEM;
  640. pci_set_master(dev);
  641. if (dma_set_mask_and_coherent(&dev->dev, DMA_BIT_MASK(64)) &&
  642. dma_set_mask_and_coherent(&dev->dev, DMA_BIT_MASK(32)))
  643. return -ENODEV;
  644. vmd->msix_count = pci_msix_vec_count(dev);
  645. if (vmd->msix_count < 0)
  646. return -ENODEV;
  647. vmd->msix_count = pci_alloc_irq_vectors(dev, 1, vmd->msix_count,
  648. PCI_IRQ_MSIX);
  649. if (vmd->msix_count < 0)
  650. return vmd->msix_count;
  651. vmd->irqs = devm_kcalloc(&dev->dev, vmd->msix_count, sizeof(*vmd->irqs),
  652. GFP_KERNEL);
  653. if (!vmd->irqs)
  654. return -ENOMEM;
  655. for (i = 0; i < vmd->msix_count; i++) {
  656. err = init_srcu_struct(&vmd->irqs[i].srcu);
  657. if (err)
  658. return err;
  659. INIT_LIST_HEAD(&vmd->irqs[i].irq_list);
  660. err = devm_request_irq(&dev->dev, pci_irq_vector(dev, i),
  661. vmd_irq, IRQF_NO_THREAD,
  662. "vmd", &vmd->irqs[i]);
  663. if (err)
  664. return err;
  665. }
  666. spin_lock_init(&vmd->cfg_lock);
  667. pci_set_drvdata(dev, vmd);
  668. err = vmd_enable_domain(vmd, (unsigned long) id->driver_data);
  669. if (err)
  670. return err;
  671. dev_info(&vmd->dev->dev, "Bound to PCI domain %04x\n",
  672. vmd->sysdata.domain);
  673. return 0;
  674. }
  675. static void vmd_cleanup_srcu(struct vmd_dev *vmd)
  676. {
  677. int i;
  678. for (i = 0; i < vmd->msix_count; i++)
  679. cleanup_srcu_struct(&vmd->irqs[i].srcu);
  680. }
  681. static void vmd_remove(struct pci_dev *dev)
  682. {
  683. struct vmd_dev *vmd = pci_get_drvdata(dev);
  684. vmd_detach_resources(vmd);
  685. sysfs_remove_link(&vmd->dev->dev.kobj, "domain");
  686. pci_stop_root_bus(vmd->bus);
  687. pci_remove_root_bus(vmd->bus);
  688. vmd_cleanup_srcu(vmd);
  689. vmd_teardown_dma_ops(vmd);
  690. irq_domain_remove(vmd->irq_domain);
  691. }
  692. #ifdef CONFIG_PM_SLEEP
  693. static int vmd_suspend(struct device *dev)
  694. {
  695. struct pci_dev *pdev = to_pci_dev(dev);
  696. struct vmd_dev *vmd = pci_get_drvdata(pdev);
  697. int i;
  698. for (i = 0; i < vmd->msix_count; i++)
  699. devm_free_irq(dev, pci_irq_vector(pdev, i), &vmd->irqs[i]);
  700. pci_save_state(pdev);
  701. return 0;
  702. }
  703. static int vmd_resume(struct device *dev)
  704. {
  705. struct pci_dev *pdev = to_pci_dev(dev);
  706. struct vmd_dev *vmd = pci_get_drvdata(pdev);
  707. int err, i;
  708. for (i = 0; i < vmd->msix_count; i++) {
  709. err = devm_request_irq(dev, pci_irq_vector(pdev, i),
  710. vmd_irq, IRQF_NO_THREAD,
  711. "vmd", &vmd->irqs[i]);
  712. if (err)
  713. return err;
  714. }
  715. pci_restore_state(pdev);
  716. return 0;
  717. }
  718. #endif
  719. static SIMPLE_DEV_PM_OPS(vmd_dev_pm_ops, vmd_suspend, vmd_resume);
  720. static const struct pci_device_id vmd_ids[] = {
  721. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_VMD_201D),},
  722. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_VMD_28C0),
  723. .driver_data = VMD_FEAT_HAS_MEMBAR_SHADOW |
  724. VMD_FEAT_HAS_BUS_RESTRICTIONS,},
  725. {0,}
  726. };
  727. MODULE_DEVICE_TABLE(pci, vmd_ids);
  728. static struct pci_driver vmd_drv = {
  729. .name = "vmd",
  730. .id_table = vmd_ids,
  731. .probe = vmd_probe,
  732. .remove = vmd_remove,
  733. .driver = {
  734. .pm = &vmd_dev_pm_ops,
  735. },
  736. };
  737. module_pci_driver(vmd_drv);
  738. MODULE_AUTHOR("Intel Corporation");
  739. MODULE_LICENSE("GPL v2");
  740. MODULE_VERSION("0.6");