vmd.c 20 KB

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