vmd.c 20 KB

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