vmd.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
  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. raw_spin_lock(&list_lock);
  108. list_add_tail_rcu(&vmdirq->node, &vmdirq->irq->irq_list);
  109. raw_spin_unlock(&list_lock);
  110. data->chip->irq_unmask(data);
  111. }
  112. static void vmd_irq_disable(struct irq_data *data)
  113. {
  114. struct vmd_irq *vmdirq = data->chip_data;
  115. data->chip->irq_mask(data);
  116. raw_spin_lock(&list_lock);
  117. list_del_rcu(&vmdirq->node);
  118. raw_spin_unlock(&list_lock);
  119. }
  120. /*
  121. * XXX: Stubbed until we develop acceptable way to not create conflicts with
  122. * other devices sharing the same vector.
  123. */
  124. static int vmd_irq_set_affinity(struct irq_data *data,
  125. const struct cpumask *dest, bool force)
  126. {
  127. return -EINVAL;
  128. }
  129. static struct irq_chip vmd_msi_controller = {
  130. .name = "VMD-MSI",
  131. .irq_enable = vmd_irq_enable,
  132. .irq_disable = vmd_irq_disable,
  133. .irq_compose_msi_msg = vmd_compose_msi_msg,
  134. .irq_set_affinity = vmd_irq_set_affinity,
  135. };
  136. static irq_hw_number_t vmd_get_hwirq(struct msi_domain_info *info,
  137. msi_alloc_info_t *arg)
  138. {
  139. return 0;
  140. }
  141. /*
  142. * XXX: We can be even smarter selecting the best IRQ once we solve the
  143. * affinity problem.
  144. */
  145. static struct vmd_irq_list *vmd_next_irq(struct vmd_dev *vmd)
  146. {
  147. int i, best = 0;
  148. raw_spin_lock(&list_lock);
  149. for (i = 1; i < vmd->msix_count; i++)
  150. if (vmd->irqs[i].count < vmd->irqs[best].count)
  151. best = i;
  152. vmd->irqs[best].count++;
  153. raw_spin_unlock(&list_lock);
  154. return &vmd->irqs[best];
  155. }
  156. static int vmd_msi_init(struct irq_domain *domain, struct msi_domain_info *info,
  157. unsigned int virq, irq_hw_number_t hwirq,
  158. msi_alloc_info_t *arg)
  159. {
  160. struct vmd_dev *vmd = vmd_from_bus(msi_desc_to_pci_dev(arg->desc)->bus);
  161. struct vmd_irq *vmdirq = kzalloc(sizeof(*vmdirq), GFP_KERNEL);
  162. if (!vmdirq)
  163. return -ENOMEM;
  164. INIT_LIST_HEAD(&vmdirq->node);
  165. vmdirq->irq = vmd_next_irq(vmd);
  166. vmdirq->virq = virq;
  167. irq_domain_set_info(domain, virq, vmdirq->irq->vmd_vector, info->chip,
  168. vmdirq, handle_simple_irq, vmd, NULL);
  169. return 0;
  170. }
  171. static void vmd_msi_free(struct irq_domain *domain,
  172. struct msi_domain_info *info, unsigned int virq)
  173. {
  174. struct vmd_irq *vmdirq = irq_get_chip_data(virq);
  175. /* XXX: Potential optimization to rebalance */
  176. raw_spin_lock(&list_lock);
  177. vmdirq->irq->count--;
  178. raw_spin_unlock(&list_lock);
  179. kfree_rcu(vmdirq, rcu);
  180. }
  181. static int vmd_msi_prepare(struct irq_domain *domain, struct device *dev,
  182. int nvec, msi_alloc_info_t *arg)
  183. {
  184. struct pci_dev *pdev = to_pci_dev(dev);
  185. struct vmd_dev *vmd = vmd_from_bus(pdev->bus);
  186. if (nvec > vmd->msix_count)
  187. return vmd->msix_count;
  188. memset(arg, 0, sizeof(*arg));
  189. return 0;
  190. }
  191. static void vmd_set_desc(msi_alloc_info_t *arg, struct msi_desc *desc)
  192. {
  193. arg->desc = desc;
  194. }
  195. static struct msi_domain_ops vmd_msi_domain_ops = {
  196. .get_hwirq = vmd_get_hwirq,
  197. .msi_init = vmd_msi_init,
  198. .msi_free = vmd_msi_free,
  199. .msi_prepare = vmd_msi_prepare,
  200. .set_desc = vmd_set_desc,
  201. };
  202. static struct msi_domain_info vmd_msi_domain_info = {
  203. .flags = MSI_FLAG_USE_DEF_DOM_OPS | MSI_FLAG_USE_DEF_CHIP_OPS |
  204. MSI_FLAG_PCI_MSIX,
  205. .ops = &vmd_msi_domain_ops,
  206. .chip = &vmd_msi_controller,
  207. };
  208. #ifdef CONFIG_X86_DEV_DMA_OPS
  209. /*
  210. * VMD replaces the requester ID with its own. DMA mappings for devices in a
  211. * VMD domain need to be mapped for the VMD, not the device requiring
  212. * the mapping.
  213. */
  214. static struct device *to_vmd_dev(struct device *dev)
  215. {
  216. struct pci_dev *pdev = to_pci_dev(dev);
  217. struct vmd_dev *vmd = vmd_from_bus(pdev->bus);
  218. return &vmd->dev->dev;
  219. }
  220. static struct dma_map_ops *vmd_dma_ops(struct device *dev)
  221. {
  222. return to_vmd_dev(dev)->archdata.dma_ops;
  223. }
  224. static void *vmd_alloc(struct device *dev, size_t size, dma_addr_t *addr,
  225. gfp_t flag, struct dma_attrs *attrs)
  226. {
  227. return vmd_dma_ops(dev)->alloc(to_vmd_dev(dev), size, addr, flag,
  228. attrs);
  229. }
  230. static void vmd_free(struct device *dev, size_t size, void *vaddr,
  231. dma_addr_t addr, struct dma_attrs *attrs)
  232. {
  233. return vmd_dma_ops(dev)->free(to_vmd_dev(dev), size, vaddr, addr,
  234. attrs);
  235. }
  236. static int vmd_mmap(struct device *dev, struct vm_area_struct *vma,
  237. void *cpu_addr, dma_addr_t addr, size_t size,
  238. struct dma_attrs *attrs)
  239. {
  240. return vmd_dma_ops(dev)->mmap(to_vmd_dev(dev), vma, cpu_addr, addr,
  241. size, attrs);
  242. }
  243. static int vmd_get_sgtable(struct device *dev, struct sg_table *sgt,
  244. void *cpu_addr, dma_addr_t addr, size_t size,
  245. struct dma_attrs *attrs)
  246. {
  247. return vmd_dma_ops(dev)->get_sgtable(to_vmd_dev(dev), sgt, cpu_addr,
  248. addr, size, attrs);
  249. }
  250. static dma_addr_t vmd_map_page(struct device *dev, struct page *page,
  251. unsigned long offset, size_t size,
  252. enum dma_data_direction dir,
  253. struct dma_attrs *attrs)
  254. {
  255. return vmd_dma_ops(dev)->map_page(to_vmd_dev(dev), page, offset, size,
  256. dir, attrs);
  257. }
  258. static void vmd_unmap_page(struct device *dev, dma_addr_t addr, size_t size,
  259. enum dma_data_direction dir, struct dma_attrs *attrs)
  260. {
  261. vmd_dma_ops(dev)->unmap_page(to_vmd_dev(dev), addr, size, dir, attrs);
  262. }
  263. static int vmd_map_sg(struct device *dev, struct scatterlist *sg, int nents,
  264. enum dma_data_direction dir, struct dma_attrs *attrs)
  265. {
  266. return vmd_dma_ops(dev)->map_sg(to_vmd_dev(dev), sg, nents, dir, attrs);
  267. }
  268. static void vmd_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,
  269. enum dma_data_direction dir, struct dma_attrs *attrs)
  270. {
  271. vmd_dma_ops(dev)->unmap_sg(to_vmd_dev(dev), sg, nents, dir, attrs);
  272. }
  273. static void vmd_sync_single_for_cpu(struct device *dev, dma_addr_t addr,
  274. size_t size, enum dma_data_direction dir)
  275. {
  276. vmd_dma_ops(dev)->sync_single_for_cpu(to_vmd_dev(dev), addr, size, dir);
  277. }
  278. static void vmd_sync_single_for_device(struct device *dev, dma_addr_t addr,
  279. size_t size, enum dma_data_direction dir)
  280. {
  281. vmd_dma_ops(dev)->sync_single_for_device(to_vmd_dev(dev), addr, size,
  282. dir);
  283. }
  284. static void vmd_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
  285. int nents, enum dma_data_direction dir)
  286. {
  287. vmd_dma_ops(dev)->sync_sg_for_cpu(to_vmd_dev(dev), sg, nents, dir);
  288. }
  289. static void vmd_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
  290. int nents, enum dma_data_direction dir)
  291. {
  292. vmd_dma_ops(dev)->sync_sg_for_device(to_vmd_dev(dev), sg, nents, dir);
  293. }
  294. static int vmd_mapping_error(struct device *dev, dma_addr_t addr)
  295. {
  296. return vmd_dma_ops(dev)->mapping_error(to_vmd_dev(dev), addr);
  297. }
  298. static int vmd_dma_supported(struct device *dev, u64 mask)
  299. {
  300. return vmd_dma_ops(dev)->dma_supported(to_vmd_dev(dev), mask);
  301. }
  302. #ifdef ARCH_HAS_DMA_GET_REQUIRED_MASK
  303. static u64 vmd_get_required_mask(struct device *dev)
  304. {
  305. return vmd_dma_ops(dev)->get_required_mask(to_vmd_dev(dev));
  306. }
  307. #endif
  308. static void vmd_teardown_dma_ops(struct vmd_dev *vmd)
  309. {
  310. struct dma_domain *domain = &vmd->dma_domain;
  311. if (vmd->dev->dev.archdata.dma_ops)
  312. del_dma_domain(domain);
  313. }
  314. #define ASSIGN_VMD_DMA_OPS(source, dest, fn) \
  315. do { \
  316. if (source->fn) \
  317. dest->fn = vmd_##fn; \
  318. } while (0)
  319. static void vmd_setup_dma_ops(struct vmd_dev *vmd)
  320. {
  321. const struct dma_map_ops *source = vmd->dev->dev.archdata.dma_ops;
  322. struct dma_map_ops *dest = &vmd->dma_ops;
  323. struct dma_domain *domain = &vmd->dma_domain;
  324. domain->domain_nr = vmd->sysdata.domain;
  325. domain->dma_ops = dest;
  326. if (!source)
  327. return;
  328. ASSIGN_VMD_DMA_OPS(source, dest, alloc);
  329. ASSIGN_VMD_DMA_OPS(source, dest, free);
  330. ASSIGN_VMD_DMA_OPS(source, dest, mmap);
  331. ASSIGN_VMD_DMA_OPS(source, dest, get_sgtable);
  332. ASSIGN_VMD_DMA_OPS(source, dest, map_page);
  333. ASSIGN_VMD_DMA_OPS(source, dest, unmap_page);
  334. ASSIGN_VMD_DMA_OPS(source, dest, map_sg);
  335. ASSIGN_VMD_DMA_OPS(source, dest, unmap_sg);
  336. ASSIGN_VMD_DMA_OPS(source, dest, sync_single_for_cpu);
  337. ASSIGN_VMD_DMA_OPS(source, dest, sync_single_for_device);
  338. ASSIGN_VMD_DMA_OPS(source, dest, sync_sg_for_cpu);
  339. ASSIGN_VMD_DMA_OPS(source, dest, sync_sg_for_device);
  340. ASSIGN_VMD_DMA_OPS(source, dest, mapping_error);
  341. ASSIGN_VMD_DMA_OPS(source, dest, dma_supported);
  342. #ifdef ARCH_HAS_DMA_GET_REQUIRED_MASK
  343. ASSIGN_VMD_DMA_OPS(source, dest, get_required_mask);
  344. #endif
  345. add_dma_domain(domain);
  346. }
  347. #undef ASSIGN_VMD_DMA_OPS
  348. #else
  349. static void vmd_teardown_dma_ops(struct vmd_dev *vmd) {}
  350. static void vmd_setup_dma_ops(struct vmd_dev *vmd) {}
  351. #endif
  352. static char __iomem *vmd_cfg_addr(struct vmd_dev *vmd, struct pci_bus *bus,
  353. unsigned int devfn, int reg, int len)
  354. {
  355. char __iomem *addr = vmd->cfgbar +
  356. (bus->number << 20) + (devfn << 12) + reg;
  357. if ((addr - vmd->cfgbar) + len >=
  358. resource_size(&vmd->dev->resource[VMD_CFGBAR]))
  359. return NULL;
  360. return addr;
  361. }
  362. /*
  363. * CPU may deadlock if config space is not serialized on some versions of this
  364. * hardware, so all config space access is done under a spinlock.
  365. */
  366. static int vmd_pci_read(struct pci_bus *bus, unsigned int devfn, int reg,
  367. int len, u32 *value)
  368. {
  369. struct vmd_dev *vmd = vmd_from_bus(bus);
  370. char __iomem *addr = vmd_cfg_addr(vmd, bus, devfn, reg, len);
  371. unsigned long flags;
  372. int ret = 0;
  373. if (!addr)
  374. return -EFAULT;
  375. spin_lock_irqsave(&vmd->cfg_lock, flags);
  376. switch (len) {
  377. case 1:
  378. *value = readb(addr);
  379. break;
  380. case 2:
  381. *value = readw(addr);
  382. break;
  383. case 4:
  384. *value = readl(addr);
  385. break;
  386. default:
  387. ret = -EINVAL;
  388. break;
  389. }
  390. spin_unlock_irqrestore(&vmd->cfg_lock, flags);
  391. return ret;
  392. }
  393. /*
  394. * VMD h/w converts non-posted config writes to posted memory writes. The
  395. * read-back in this function forces the completion so it returns only after
  396. * the config space was written, as expected.
  397. */
  398. static int vmd_pci_write(struct pci_bus *bus, unsigned int devfn, int reg,
  399. int len, u32 value)
  400. {
  401. struct vmd_dev *vmd = vmd_from_bus(bus);
  402. char __iomem *addr = vmd_cfg_addr(vmd, bus, devfn, reg, len);
  403. unsigned long flags;
  404. int ret = 0;
  405. if (!addr)
  406. return -EFAULT;
  407. spin_lock_irqsave(&vmd->cfg_lock, flags);
  408. switch (len) {
  409. case 1:
  410. writeb(value, addr);
  411. readb(addr);
  412. break;
  413. case 2:
  414. writew(value, addr);
  415. readw(addr);
  416. break;
  417. case 4:
  418. writel(value, addr);
  419. readl(addr);
  420. break;
  421. default:
  422. ret = -EINVAL;
  423. break;
  424. }
  425. spin_unlock_irqrestore(&vmd->cfg_lock, flags);
  426. return ret;
  427. }
  428. static struct pci_ops vmd_ops = {
  429. .read = vmd_pci_read,
  430. .write = vmd_pci_write,
  431. };
  432. /*
  433. * VMD domains start at 0x1000 to not clash with ACPI _SEG domains.
  434. */
  435. static int vmd_find_free_domain(void)
  436. {
  437. int domain = 0xffff;
  438. struct pci_bus *bus = NULL;
  439. while ((bus = pci_find_next_bus(bus)) != NULL)
  440. domain = max_t(int, domain, pci_domain_nr(bus));
  441. return domain + 1;
  442. }
  443. static int vmd_enable_domain(struct vmd_dev *vmd)
  444. {
  445. struct pci_sysdata *sd = &vmd->sysdata;
  446. struct resource *res;
  447. u32 upper_bits;
  448. unsigned long flags;
  449. LIST_HEAD(resources);
  450. res = &vmd->dev->resource[VMD_CFGBAR];
  451. vmd->resources[0] = (struct resource) {
  452. .name = "VMD CFGBAR",
  453. .start = res->start,
  454. .end = (resource_size(res) >> 20) - 1,
  455. .flags = IORESOURCE_BUS | IORESOURCE_PCI_FIXED,
  456. };
  457. res = &vmd->dev->resource[VMD_MEMBAR1];
  458. upper_bits = upper_32_bits(res->end);
  459. flags = res->flags & ~IORESOURCE_SIZEALIGN;
  460. if (!upper_bits)
  461. flags &= ~IORESOURCE_MEM_64;
  462. vmd->resources[1] = (struct resource) {
  463. .name = "VMD MEMBAR1",
  464. .start = res->start,
  465. .end = res->end,
  466. .flags = flags,
  467. };
  468. res = &vmd->dev->resource[VMD_MEMBAR2];
  469. upper_bits = upper_32_bits(res->end);
  470. flags = res->flags & ~IORESOURCE_SIZEALIGN;
  471. if (!upper_bits)
  472. flags &= ~IORESOURCE_MEM_64;
  473. vmd->resources[2] = (struct resource) {
  474. .name = "VMD MEMBAR2",
  475. .start = res->start + 0x2000,
  476. .end = res->end,
  477. .flags = flags,
  478. };
  479. sd->domain = vmd_find_free_domain();
  480. if (sd->domain < 0)
  481. return sd->domain;
  482. sd->node = pcibus_to_node(vmd->dev->bus);
  483. vmd->irq_domain = pci_msi_create_irq_domain(NULL, &vmd_msi_domain_info,
  484. NULL);
  485. if (!vmd->irq_domain)
  486. return -ENODEV;
  487. pci_add_resource(&resources, &vmd->resources[0]);
  488. pci_add_resource(&resources, &vmd->resources[1]);
  489. pci_add_resource(&resources, &vmd->resources[2]);
  490. vmd->bus = pci_create_root_bus(&vmd->dev->dev, 0, &vmd_ops, sd,
  491. &resources);
  492. if (!vmd->bus) {
  493. pci_free_resource_list(&resources);
  494. irq_domain_remove(vmd->irq_domain);
  495. return -ENODEV;
  496. }
  497. vmd_setup_dma_ops(vmd);
  498. dev_set_msi_domain(&vmd->bus->dev, vmd->irq_domain);
  499. pci_rescan_bus(vmd->bus);
  500. WARN(sysfs_create_link(&vmd->dev->dev.kobj, &vmd->bus->dev.kobj,
  501. "domain"), "Can't create symlink to domain\n");
  502. return 0;
  503. }
  504. static irqreturn_t vmd_irq(int irq, void *data)
  505. {
  506. struct vmd_irq_list *irqs = data;
  507. struct vmd_irq *vmdirq;
  508. rcu_read_lock();
  509. list_for_each_entry_rcu(vmdirq, &irqs->irq_list, node)
  510. generic_handle_irq(vmdirq->virq);
  511. rcu_read_unlock();
  512. return IRQ_HANDLED;
  513. }
  514. static int vmd_probe(struct pci_dev *dev, const struct pci_device_id *id)
  515. {
  516. struct vmd_dev *vmd;
  517. int i, err;
  518. if (resource_size(&dev->resource[VMD_CFGBAR]) < (1 << 20))
  519. return -ENOMEM;
  520. vmd = devm_kzalloc(&dev->dev, sizeof(*vmd), GFP_KERNEL);
  521. if (!vmd)
  522. return -ENOMEM;
  523. vmd->dev = dev;
  524. err = pcim_enable_device(dev);
  525. if (err < 0)
  526. return err;
  527. vmd->cfgbar = pcim_iomap(dev, VMD_CFGBAR, 0);
  528. if (!vmd->cfgbar)
  529. return -ENOMEM;
  530. pci_set_master(dev);
  531. if (dma_set_mask_and_coherent(&dev->dev, DMA_BIT_MASK(64)) &&
  532. dma_set_mask_and_coherent(&dev->dev, DMA_BIT_MASK(32)))
  533. return -ENODEV;
  534. vmd->msix_count = pci_msix_vec_count(dev);
  535. if (vmd->msix_count < 0)
  536. return -ENODEV;
  537. vmd->irqs = devm_kcalloc(&dev->dev, vmd->msix_count, sizeof(*vmd->irqs),
  538. GFP_KERNEL);
  539. if (!vmd->irqs)
  540. return -ENOMEM;
  541. vmd->msix_entries = devm_kcalloc(&dev->dev, vmd->msix_count,
  542. sizeof(*vmd->msix_entries),
  543. GFP_KERNEL);
  544. if (!vmd->msix_entries)
  545. return -ENOMEM;
  546. for (i = 0; i < vmd->msix_count; i++)
  547. vmd->msix_entries[i].entry = i;
  548. vmd->msix_count = pci_enable_msix_range(vmd->dev, vmd->msix_entries, 1,
  549. vmd->msix_count);
  550. if (vmd->msix_count < 0)
  551. return vmd->msix_count;
  552. for (i = 0; i < vmd->msix_count; i++) {
  553. INIT_LIST_HEAD(&vmd->irqs[i].irq_list);
  554. vmd->irqs[i].vmd_vector = vmd->msix_entries[i].vector;
  555. vmd->irqs[i].index = i;
  556. err = devm_request_irq(&dev->dev, vmd->irqs[i].vmd_vector,
  557. vmd_irq, 0, "vmd", &vmd->irqs[i]);
  558. if (err)
  559. return err;
  560. }
  561. spin_lock_init(&vmd->cfg_lock);
  562. pci_set_drvdata(dev, vmd);
  563. err = vmd_enable_domain(vmd);
  564. if (err)
  565. return err;
  566. dev_info(&vmd->dev->dev, "Bound to PCI domain %04x\n",
  567. vmd->sysdata.domain);
  568. return 0;
  569. }
  570. static void vmd_remove(struct pci_dev *dev)
  571. {
  572. struct vmd_dev *vmd = pci_get_drvdata(dev);
  573. pci_set_drvdata(dev, NULL);
  574. sysfs_remove_link(&vmd->dev->dev.kobj, "domain");
  575. pci_stop_root_bus(vmd->bus);
  576. pci_remove_root_bus(vmd->bus);
  577. vmd_teardown_dma_ops(vmd);
  578. irq_domain_remove(vmd->irq_domain);
  579. }
  580. #ifdef CONFIG_PM
  581. static int vmd_suspend(struct device *dev)
  582. {
  583. struct pci_dev *pdev = to_pci_dev(dev);
  584. pci_save_state(pdev);
  585. return 0;
  586. }
  587. static int vmd_resume(struct device *dev)
  588. {
  589. struct pci_dev *pdev = to_pci_dev(dev);
  590. pci_restore_state(pdev);
  591. return 0;
  592. }
  593. #endif
  594. static SIMPLE_DEV_PM_OPS(vmd_dev_pm_ops, vmd_suspend, vmd_resume);
  595. static const struct pci_device_id vmd_ids[] = {
  596. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x201d),},
  597. {0,}
  598. };
  599. MODULE_DEVICE_TABLE(pci, vmd_ids);
  600. static struct pci_driver vmd_drv = {
  601. .name = "vmd",
  602. .id_table = vmd_ids,
  603. .probe = vmd_probe,
  604. .remove = vmd_remove,
  605. .driver = {
  606. .pm = &vmd_dev_pm_ops,
  607. },
  608. };
  609. module_pci_driver(vmd_drv);
  610. MODULE_AUTHOR("Intel Corporation");
  611. MODULE_LICENSE("GPL v2");
  612. MODULE_VERSION("0.6");