vmd.c 20 KB

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