vmd.c 20 KB

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