vmd.c 20 KB

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