intel_irq_remapping.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444
  1. #define pr_fmt(fmt) "DMAR-IR: " fmt
  2. #include <linux/interrupt.h>
  3. #include <linux/dmar.h>
  4. #include <linux/spinlock.h>
  5. #include <linux/slab.h>
  6. #include <linux/jiffies.h>
  7. #include <linux/hpet.h>
  8. #include <linux/pci.h>
  9. #include <linux/irq.h>
  10. #include <linux/intel-iommu.h>
  11. #include <linux/acpi.h>
  12. #include <linux/irqdomain.h>
  13. #include <linux/crash_dump.h>
  14. #include <asm/io_apic.h>
  15. #include <asm/smp.h>
  16. #include <asm/cpu.h>
  17. #include <asm/irq_remapping.h>
  18. #include <asm/pci-direct.h>
  19. #include <asm/msidef.h>
  20. #include "irq_remapping.h"
  21. enum irq_mode {
  22. IRQ_REMAPPING,
  23. IRQ_POSTING,
  24. };
  25. struct ioapic_scope {
  26. struct intel_iommu *iommu;
  27. unsigned int id;
  28. unsigned int bus; /* PCI bus number */
  29. unsigned int devfn; /* PCI devfn number */
  30. };
  31. struct hpet_scope {
  32. struct intel_iommu *iommu;
  33. u8 id;
  34. unsigned int bus;
  35. unsigned int devfn;
  36. };
  37. struct irq_2_iommu {
  38. struct intel_iommu *iommu;
  39. u16 irte_index;
  40. u16 sub_handle;
  41. u8 irte_mask;
  42. enum irq_mode mode;
  43. };
  44. struct intel_ir_data {
  45. struct irq_2_iommu irq_2_iommu;
  46. struct irte irte_entry;
  47. union {
  48. struct msi_msg msi_entry;
  49. };
  50. };
  51. #define IR_X2APIC_MODE(mode) (mode ? (1 << 11) : 0)
  52. #define IRTE_DEST(dest) ((eim_mode) ? dest : dest << 8)
  53. static int __read_mostly eim_mode;
  54. static struct ioapic_scope ir_ioapic[MAX_IO_APICS];
  55. static struct hpet_scope ir_hpet[MAX_HPET_TBS];
  56. /*
  57. * Lock ordering:
  58. * ->dmar_global_lock
  59. * ->irq_2_ir_lock
  60. * ->qi->q_lock
  61. * ->iommu->register_lock
  62. * Note:
  63. * intel_irq_remap_ops.{supported,prepare,enable,disable,reenable} are called
  64. * in single-threaded environment with interrupt disabled, so no need to tabke
  65. * the dmar_global_lock.
  66. */
  67. static DEFINE_RAW_SPINLOCK(irq_2_ir_lock);
  68. static struct irq_domain_ops intel_ir_domain_ops;
  69. static void iommu_disable_irq_remapping(struct intel_iommu *iommu);
  70. static int __init parse_ioapics_under_ir(void);
  71. static bool ir_pre_enabled(struct intel_iommu *iommu)
  72. {
  73. return (iommu->flags & VTD_FLAG_IRQ_REMAP_PRE_ENABLED);
  74. }
  75. static void clear_ir_pre_enabled(struct intel_iommu *iommu)
  76. {
  77. iommu->flags &= ~VTD_FLAG_IRQ_REMAP_PRE_ENABLED;
  78. }
  79. static void init_ir_status(struct intel_iommu *iommu)
  80. {
  81. u32 gsts;
  82. gsts = readl(iommu->reg + DMAR_GSTS_REG);
  83. if (gsts & DMA_GSTS_IRES)
  84. iommu->flags |= VTD_FLAG_IRQ_REMAP_PRE_ENABLED;
  85. }
  86. static int alloc_irte(struct intel_iommu *iommu, int irq,
  87. struct irq_2_iommu *irq_iommu, u16 count)
  88. {
  89. struct ir_table *table = iommu->ir_table;
  90. unsigned int mask = 0;
  91. unsigned long flags;
  92. int index;
  93. if (!count || !irq_iommu)
  94. return -1;
  95. if (count > 1) {
  96. count = __roundup_pow_of_two(count);
  97. mask = ilog2(count);
  98. }
  99. if (mask > ecap_max_handle_mask(iommu->ecap)) {
  100. pr_err("Requested mask %x exceeds the max invalidation handle"
  101. " mask value %Lx\n", mask,
  102. ecap_max_handle_mask(iommu->ecap));
  103. return -1;
  104. }
  105. raw_spin_lock_irqsave(&irq_2_ir_lock, flags);
  106. index = bitmap_find_free_region(table->bitmap,
  107. INTR_REMAP_TABLE_ENTRIES, mask);
  108. if (index < 0) {
  109. pr_warn("IR%d: can't allocate an IRTE\n", iommu->seq_id);
  110. } else {
  111. irq_iommu->iommu = iommu;
  112. irq_iommu->irte_index = index;
  113. irq_iommu->sub_handle = 0;
  114. irq_iommu->irte_mask = mask;
  115. irq_iommu->mode = IRQ_REMAPPING;
  116. }
  117. raw_spin_unlock_irqrestore(&irq_2_ir_lock, flags);
  118. return index;
  119. }
  120. static int qi_flush_iec(struct intel_iommu *iommu, int index, int mask)
  121. {
  122. struct qi_desc desc;
  123. desc.low = QI_IEC_IIDEX(index) | QI_IEC_TYPE | QI_IEC_IM(mask)
  124. | QI_IEC_SELECTIVE;
  125. desc.high = 0;
  126. return qi_submit_sync(&desc, iommu);
  127. }
  128. static int modify_irte(struct irq_2_iommu *irq_iommu,
  129. struct irte *irte_modified)
  130. {
  131. struct intel_iommu *iommu;
  132. unsigned long flags;
  133. struct irte *irte;
  134. int rc, index;
  135. if (!irq_iommu)
  136. return -1;
  137. raw_spin_lock_irqsave(&irq_2_ir_lock, flags);
  138. iommu = irq_iommu->iommu;
  139. index = irq_iommu->irte_index + irq_iommu->sub_handle;
  140. irte = &iommu->ir_table->base[index];
  141. set_64bit(&irte->low, irte_modified->low);
  142. set_64bit(&irte->high, irte_modified->high);
  143. __iommu_flush_cache(iommu, irte, sizeof(*irte));
  144. rc = qi_flush_iec(iommu, index, 0);
  145. /* Update iommu mode according to the IRTE mode */
  146. irq_iommu->mode = irte->pst ? IRQ_POSTING : IRQ_REMAPPING;
  147. raw_spin_unlock_irqrestore(&irq_2_ir_lock, flags);
  148. return rc;
  149. }
  150. static struct intel_iommu *map_hpet_to_ir(u8 hpet_id)
  151. {
  152. int i;
  153. for (i = 0; i < MAX_HPET_TBS; i++)
  154. if (ir_hpet[i].id == hpet_id && ir_hpet[i].iommu)
  155. return ir_hpet[i].iommu;
  156. return NULL;
  157. }
  158. static struct intel_iommu *map_ioapic_to_ir(int apic)
  159. {
  160. int i;
  161. for (i = 0; i < MAX_IO_APICS; i++)
  162. if (ir_ioapic[i].id == apic && ir_ioapic[i].iommu)
  163. return ir_ioapic[i].iommu;
  164. return NULL;
  165. }
  166. static struct intel_iommu *map_dev_to_ir(struct pci_dev *dev)
  167. {
  168. struct dmar_drhd_unit *drhd;
  169. drhd = dmar_find_matched_drhd_unit(dev);
  170. if (!drhd)
  171. return NULL;
  172. return drhd->iommu;
  173. }
  174. static int clear_entries(struct irq_2_iommu *irq_iommu)
  175. {
  176. struct irte *start, *entry, *end;
  177. struct intel_iommu *iommu;
  178. int index;
  179. if (irq_iommu->sub_handle)
  180. return 0;
  181. iommu = irq_iommu->iommu;
  182. index = irq_iommu->irte_index;
  183. start = iommu->ir_table->base + index;
  184. end = start + (1 << irq_iommu->irte_mask);
  185. for (entry = start; entry < end; entry++) {
  186. set_64bit(&entry->low, 0);
  187. set_64bit(&entry->high, 0);
  188. }
  189. bitmap_release_region(iommu->ir_table->bitmap, index,
  190. irq_iommu->irte_mask);
  191. return qi_flush_iec(iommu, index, irq_iommu->irte_mask);
  192. }
  193. /*
  194. * source validation type
  195. */
  196. #define SVT_NO_VERIFY 0x0 /* no verification is required */
  197. #define SVT_VERIFY_SID_SQ 0x1 /* verify using SID and SQ fields */
  198. #define SVT_VERIFY_BUS 0x2 /* verify bus of request-id */
  199. /*
  200. * source-id qualifier
  201. */
  202. #define SQ_ALL_16 0x0 /* verify all 16 bits of request-id */
  203. #define SQ_13_IGNORE_1 0x1 /* verify most significant 13 bits, ignore
  204. * the third least significant bit
  205. */
  206. #define SQ_13_IGNORE_2 0x2 /* verify most significant 13 bits, ignore
  207. * the second and third least significant bits
  208. */
  209. #define SQ_13_IGNORE_3 0x3 /* verify most significant 13 bits, ignore
  210. * the least three significant bits
  211. */
  212. /*
  213. * set SVT, SQ and SID fields of irte to verify
  214. * source ids of interrupt requests
  215. */
  216. static void set_irte_sid(struct irte *irte, unsigned int svt,
  217. unsigned int sq, unsigned int sid)
  218. {
  219. if (disable_sourceid_checking)
  220. svt = SVT_NO_VERIFY;
  221. irte->svt = svt;
  222. irte->sq = sq;
  223. irte->sid = sid;
  224. }
  225. static int set_ioapic_sid(struct irte *irte, int apic)
  226. {
  227. int i;
  228. u16 sid = 0;
  229. if (!irte)
  230. return -1;
  231. down_read(&dmar_global_lock);
  232. for (i = 0; i < MAX_IO_APICS; i++) {
  233. if (ir_ioapic[i].iommu && ir_ioapic[i].id == apic) {
  234. sid = (ir_ioapic[i].bus << 8) | ir_ioapic[i].devfn;
  235. break;
  236. }
  237. }
  238. up_read(&dmar_global_lock);
  239. if (sid == 0) {
  240. pr_warn("Failed to set source-id of IOAPIC (%d)\n", apic);
  241. return -1;
  242. }
  243. set_irte_sid(irte, SVT_VERIFY_SID_SQ, SQ_ALL_16, sid);
  244. return 0;
  245. }
  246. static int set_hpet_sid(struct irte *irte, u8 id)
  247. {
  248. int i;
  249. u16 sid = 0;
  250. if (!irte)
  251. return -1;
  252. down_read(&dmar_global_lock);
  253. for (i = 0; i < MAX_HPET_TBS; i++) {
  254. if (ir_hpet[i].iommu && ir_hpet[i].id == id) {
  255. sid = (ir_hpet[i].bus << 8) | ir_hpet[i].devfn;
  256. break;
  257. }
  258. }
  259. up_read(&dmar_global_lock);
  260. if (sid == 0) {
  261. pr_warn("Failed to set source-id of HPET block (%d)\n", id);
  262. return -1;
  263. }
  264. /*
  265. * Should really use SQ_ALL_16. Some platforms are broken.
  266. * While we figure out the right quirks for these broken platforms, use
  267. * SQ_13_IGNORE_3 for now.
  268. */
  269. set_irte_sid(irte, SVT_VERIFY_SID_SQ, SQ_13_IGNORE_3, sid);
  270. return 0;
  271. }
  272. struct set_msi_sid_data {
  273. struct pci_dev *pdev;
  274. u16 alias;
  275. };
  276. static int set_msi_sid_cb(struct pci_dev *pdev, u16 alias, void *opaque)
  277. {
  278. struct set_msi_sid_data *data = opaque;
  279. data->pdev = pdev;
  280. data->alias = alias;
  281. return 0;
  282. }
  283. static int set_msi_sid(struct irte *irte, struct pci_dev *dev)
  284. {
  285. struct set_msi_sid_data data;
  286. if (!irte || !dev)
  287. return -1;
  288. pci_for_each_dma_alias(dev, set_msi_sid_cb, &data);
  289. /*
  290. * DMA alias provides us with a PCI device and alias. The only case
  291. * where the it will return an alias on a different bus than the
  292. * device is the case of a PCIe-to-PCI bridge, where the alias is for
  293. * the subordinate bus. In this case we can only verify the bus.
  294. *
  295. * If the alias device is on a different bus than our source device
  296. * then we have a topology based alias, use it.
  297. *
  298. * Otherwise, the alias is for a device DMA quirk and we cannot
  299. * assume that MSI uses the same requester ID. Therefore use the
  300. * original device.
  301. */
  302. if (PCI_BUS_NUM(data.alias) != data.pdev->bus->number)
  303. set_irte_sid(irte, SVT_VERIFY_BUS, SQ_ALL_16,
  304. PCI_DEVID(PCI_BUS_NUM(data.alias),
  305. dev->bus->number));
  306. else if (data.pdev->bus->number != dev->bus->number)
  307. set_irte_sid(irte, SVT_VERIFY_SID_SQ, SQ_ALL_16, data.alias);
  308. else
  309. set_irte_sid(irte, SVT_VERIFY_SID_SQ, SQ_ALL_16,
  310. PCI_DEVID(dev->bus->number, dev->devfn));
  311. return 0;
  312. }
  313. static int iommu_load_old_irte(struct intel_iommu *iommu)
  314. {
  315. struct irte *old_ir_table;
  316. phys_addr_t irt_phys;
  317. unsigned int i;
  318. size_t size;
  319. u64 irta;
  320. if (!is_kdump_kernel()) {
  321. pr_warn("IRQ remapping was enabled on %s but we are not in kdump mode\n",
  322. iommu->name);
  323. clear_ir_pre_enabled(iommu);
  324. iommu_disable_irq_remapping(iommu);
  325. return -EINVAL;
  326. }
  327. /* Check whether the old ir-table has the same size as ours */
  328. irta = dmar_readq(iommu->reg + DMAR_IRTA_REG);
  329. if ((irta & INTR_REMAP_TABLE_REG_SIZE_MASK)
  330. != INTR_REMAP_TABLE_REG_SIZE)
  331. return -EINVAL;
  332. irt_phys = irta & VTD_PAGE_MASK;
  333. size = INTR_REMAP_TABLE_ENTRIES*sizeof(struct irte);
  334. /* Map the old IR table */
  335. old_ir_table = ioremap_cache(irt_phys, size);
  336. if (!old_ir_table)
  337. return -ENOMEM;
  338. /* Copy data over */
  339. memcpy(iommu->ir_table->base, old_ir_table, size);
  340. __iommu_flush_cache(iommu, iommu->ir_table->base, size);
  341. /*
  342. * Now check the table for used entries and mark those as
  343. * allocated in the bitmap
  344. */
  345. for (i = 0; i < INTR_REMAP_TABLE_ENTRIES; i++) {
  346. if (iommu->ir_table->base[i].present)
  347. bitmap_set(iommu->ir_table->bitmap, i, 1);
  348. }
  349. return 0;
  350. }
  351. static void iommu_set_irq_remapping(struct intel_iommu *iommu, int mode)
  352. {
  353. unsigned long flags;
  354. u64 addr;
  355. u32 sts;
  356. addr = virt_to_phys((void *)iommu->ir_table->base);
  357. raw_spin_lock_irqsave(&iommu->register_lock, flags);
  358. dmar_writeq(iommu->reg + DMAR_IRTA_REG,
  359. (addr) | IR_X2APIC_MODE(mode) | INTR_REMAP_TABLE_REG_SIZE);
  360. /* Set interrupt-remapping table pointer */
  361. writel(iommu->gcmd | DMA_GCMD_SIRTP, iommu->reg + DMAR_GCMD_REG);
  362. IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
  363. readl, (sts & DMA_GSTS_IRTPS), sts);
  364. raw_spin_unlock_irqrestore(&iommu->register_lock, flags);
  365. /*
  366. * Global invalidation of interrupt entry cache to make sure the
  367. * hardware uses the new irq remapping table.
  368. */
  369. qi_global_iec(iommu);
  370. }
  371. static void iommu_enable_irq_remapping(struct intel_iommu *iommu)
  372. {
  373. unsigned long flags;
  374. u32 sts;
  375. raw_spin_lock_irqsave(&iommu->register_lock, flags);
  376. /* Enable interrupt-remapping */
  377. iommu->gcmd |= DMA_GCMD_IRE;
  378. iommu->gcmd &= ~DMA_GCMD_CFI; /* Block compatibility-format MSIs */
  379. writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
  380. IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
  381. readl, (sts & DMA_GSTS_IRES), sts);
  382. /*
  383. * With CFI clear in the Global Command register, we should be
  384. * protected from dangerous (i.e. compatibility) interrupts
  385. * regardless of x2apic status. Check just to be sure.
  386. */
  387. if (sts & DMA_GSTS_CFIS)
  388. WARN(1, KERN_WARNING
  389. "Compatibility-format IRQs enabled despite intr remapping;\n"
  390. "you are vulnerable to IRQ injection.\n");
  391. raw_spin_unlock_irqrestore(&iommu->register_lock, flags);
  392. }
  393. static int intel_setup_irq_remapping(struct intel_iommu *iommu)
  394. {
  395. struct ir_table *ir_table;
  396. struct page *pages;
  397. unsigned long *bitmap;
  398. if (iommu->ir_table)
  399. return 0;
  400. ir_table = kzalloc(sizeof(struct ir_table), GFP_KERNEL);
  401. if (!ir_table)
  402. return -ENOMEM;
  403. pages = alloc_pages_node(iommu->node, GFP_KERNEL | __GFP_ZERO,
  404. INTR_REMAP_PAGE_ORDER);
  405. if (!pages) {
  406. pr_err("IR%d: failed to allocate pages of order %d\n",
  407. iommu->seq_id, INTR_REMAP_PAGE_ORDER);
  408. goto out_free_table;
  409. }
  410. bitmap = kcalloc(BITS_TO_LONGS(INTR_REMAP_TABLE_ENTRIES),
  411. sizeof(long), GFP_ATOMIC);
  412. if (bitmap == NULL) {
  413. pr_err("IR%d: failed to allocate bitmap\n", iommu->seq_id);
  414. goto out_free_pages;
  415. }
  416. iommu->ir_domain = irq_domain_add_hierarchy(arch_get_ir_parent_domain(),
  417. 0, INTR_REMAP_TABLE_ENTRIES,
  418. NULL, &intel_ir_domain_ops,
  419. iommu);
  420. if (!iommu->ir_domain) {
  421. pr_err("IR%d: failed to allocate irqdomain\n", iommu->seq_id);
  422. goto out_free_bitmap;
  423. }
  424. iommu->ir_msi_domain = arch_create_msi_irq_domain(iommu->ir_domain);
  425. ir_table->base = page_address(pages);
  426. ir_table->bitmap = bitmap;
  427. iommu->ir_table = ir_table;
  428. /*
  429. * If the queued invalidation is already initialized,
  430. * shouldn't disable it.
  431. */
  432. if (!iommu->qi) {
  433. /*
  434. * Clear previous faults.
  435. */
  436. dmar_fault(-1, iommu);
  437. dmar_disable_qi(iommu);
  438. if (dmar_enable_qi(iommu)) {
  439. pr_err("Failed to enable queued invalidation\n");
  440. goto out_free_bitmap;
  441. }
  442. }
  443. init_ir_status(iommu);
  444. if (ir_pre_enabled(iommu)) {
  445. if (iommu_load_old_irte(iommu))
  446. pr_err("Failed to copy IR table for %s from previous kernel\n",
  447. iommu->name);
  448. else
  449. pr_info("Copied IR table for %s from previous kernel\n",
  450. iommu->name);
  451. }
  452. iommu_set_irq_remapping(iommu, eim_mode);
  453. return 0;
  454. out_free_bitmap:
  455. kfree(bitmap);
  456. out_free_pages:
  457. __free_pages(pages, INTR_REMAP_PAGE_ORDER);
  458. out_free_table:
  459. kfree(ir_table);
  460. iommu->ir_table = NULL;
  461. return -ENOMEM;
  462. }
  463. static void intel_teardown_irq_remapping(struct intel_iommu *iommu)
  464. {
  465. if (iommu && iommu->ir_table) {
  466. if (iommu->ir_msi_domain) {
  467. irq_domain_remove(iommu->ir_msi_domain);
  468. iommu->ir_msi_domain = NULL;
  469. }
  470. if (iommu->ir_domain) {
  471. irq_domain_remove(iommu->ir_domain);
  472. iommu->ir_domain = NULL;
  473. }
  474. free_pages((unsigned long)iommu->ir_table->base,
  475. INTR_REMAP_PAGE_ORDER);
  476. kfree(iommu->ir_table->bitmap);
  477. kfree(iommu->ir_table);
  478. iommu->ir_table = NULL;
  479. }
  480. }
  481. /*
  482. * Disable Interrupt Remapping.
  483. */
  484. static void iommu_disable_irq_remapping(struct intel_iommu *iommu)
  485. {
  486. unsigned long flags;
  487. u32 sts;
  488. if (!ecap_ir_support(iommu->ecap))
  489. return;
  490. /*
  491. * global invalidation of interrupt entry cache before disabling
  492. * interrupt-remapping.
  493. */
  494. qi_global_iec(iommu);
  495. raw_spin_lock_irqsave(&iommu->register_lock, flags);
  496. sts = dmar_readq(iommu->reg + DMAR_GSTS_REG);
  497. if (!(sts & DMA_GSTS_IRES))
  498. goto end;
  499. iommu->gcmd &= ~DMA_GCMD_IRE;
  500. writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
  501. IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
  502. readl, !(sts & DMA_GSTS_IRES), sts);
  503. end:
  504. raw_spin_unlock_irqrestore(&iommu->register_lock, flags);
  505. }
  506. static int __init dmar_x2apic_optout(void)
  507. {
  508. struct acpi_table_dmar *dmar;
  509. dmar = (struct acpi_table_dmar *)dmar_tbl;
  510. if (!dmar || no_x2apic_optout)
  511. return 0;
  512. return dmar->flags & DMAR_X2APIC_OPT_OUT;
  513. }
  514. static void __init intel_cleanup_irq_remapping(void)
  515. {
  516. struct dmar_drhd_unit *drhd;
  517. struct intel_iommu *iommu;
  518. for_each_iommu(iommu, drhd) {
  519. if (ecap_ir_support(iommu->ecap)) {
  520. iommu_disable_irq_remapping(iommu);
  521. intel_teardown_irq_remapping(iommu);
  522. }
  523. }
  524. if (x2apic_supported())
  525. pr_warn("Failed to enable irq remapping. You are vulnerable to irq-injection attacks.\n");
  526. }
  527. static int __init intel_prepare_irq_remapping(void)
  528. {
  529. struct dmar_drhd_unit *drhd;
  530. struct intel_iommu *iommu;
  531. int eim = 0;
  532. if (irq_remap_broken) {
  533. pr_warn("This system BIOS has enabled interrupt remapping\n"
  534. "on a chipset that contains an erratum making that\n"
  535. "feature unstable. To maintain system stability\n"
  536. "interrupt remapping is being disabled. Please\n"
  537. "contact your BIOS vendor for an update\n");
  538. add_taint(TAINT_FIRMWARE_WORKAROUND, LOCKDEP_STILL_OK);
  539. return -ENODEV;
  540. }
  541. if (dmar_table_init() < 0)
  542. return -ENODEV;
  543. if (!dmar_ir_support())
  544. return -ENODEV;
  545. if (parse_ioapics_under_ir() != 1) {
  546. pr_info("Not enabling interrupt remapping\n");
  547. goto error;
  548. }
  549. /* First make sure all IOMMUs support IRQ remapping */
  550. for_each_iommu(iommu, drhd)
  551. if (!ecap_ir_support(iommu->ecap))
  552. goto error;
  553. /* Detect remapping mode: lapic or x2apic */
  554. if (x2apic_supported()) {
  555. eim = !dmar_x2apic_optout();
  556. if (!eim) {
  557. pr_info("x2apic is disabled because BIOS sets x2apic opt out bit.");
  558. pr_info("Use 'intremap=no_x2apic_optout' to override the BIOS setting.\n");
  559. }
  560. }
  561. for_each_iommu(iommu, drhd) {
  562. if (eim && !ecap_eim_support(iommu->ecap)) {
  563. pr_info("%s does not support EIM\n", iommu->name);
  564. eim = 0;
  565. }
  566. }
  567. eim_mode = eim;
  568. if (eim)
  569. pr_info("Queued invalidation will be enabled to support x2apic and Intr-remapping.\n");
  570. /* Do the initializations early */
  571. for_each_iommu(iommu, drhd) {
  572. if (intel_setup_irq_remapping(iommu)) {
  573. pr_err("Failed to setup irq remapping for %s\n",
  574. iommu->name);
  575. goto error;
  576. }
  577. }
  578. return 0;
  579. error:
  580. intel_cleanup_irq_remapping();
  581. return -ENODEV;
  582. }
  583. /*
  584. * Set Posted-Interrupts capability.
  585. */
  586. static inline void set_irq_posting_cap(void)
  587. {
  588. struct dmar_drhd_unit *drhd;
  589. struct intel_iommu *iommu;
  590. if (!disable_irq_post) {
  591. intel_irq_remap_ops.capability |= 1 << IRQ_POSTING_CAP;
  592. for_each_iommu(iommu, drhd)
  593. if (!cap_pi_support(iommu->cap)) {
  594. intel_irq_remap_ops.capability &=
  595. ~(1 << IRQ_POSTING_CAP);
  596. break;
  597. }
  598. }
  599. }
  600. static int __init intel_enable_irq_remapping(void)
  601. {
  602. struct dmar_drhd_unit *drhd;
  603. struct intel_iommu *iommu;
  604. bool setup = false;
  605. /*
  606. * Setup Interrupt-remapping for all the DRHD's now.
  607. */
  608. for_each_iommu(iommu, drhd) {
  609. if (!ir_pre_enabled(iommu))
  610. iommu_enable_irq_remapping(iommu);
  611. setup = true;
  612. }
  613. if (!setup)
  614. goto error;
  615. irq_remapping_enabled = 1;
  616. set_irq_posting_cap();
  617. pr_info("Enabled IRQ remapping in %s mode\n", eim_mode ? "x2apic" : "xapic");
  618. return eim_mode ? IRQ_REMAP_X2APIC_MODE : IRQ_REMAP_XAPIC_MODE;
  619. error:
  620. intel_cleanup_irq_remapping();
  621. return -1;
  622. }
  623. static int ir_parse_one_hpet_scope(struct acpi_dmar_device_scope *scope,
  624. struct intel_iommu *iommu,
  625. struct acpi_dmar_hardware_unit *drhd)
  626. {
  627. struct acpi_dmar_pci_path *path;
  628. u8 bus;
  629. int count, free = -1;
  630. bus = scope->bus;
  631. path = (struct acpi_dmar_pci_path *)(scope + 1);
  632. count = (scope->length - sizeof(struct acpi_dmar_device_scope))
  633. / sizeof(struct acpi_dmar_pci_path);
  634. while (--count > 0) {
  635. /*
  636. * Access PCI directly due to the PCI
  637. * subsystem isn't initialized yet.
  638. */
  639. bus = read_pci_config_byte(bus, path->device, path->function,
  640. PCI_SECONDARY_BUS);
  641. path++;
  642. }
  643. for (count = 0; count < MAX_HPET_TBS; count++) {
  644. if (ir_hpet[count].iommu == iommu &&
  645. ir_hpet[count].id == scope->enumeration_id)
  646. return 0;
  647. else if (ir_hpet[count].iommu == NULL && free == -1)
  648. free = count;
  649. }
  650. if (free == -1) {
  651. pr_warn("Exceeded Max HPET blocks\n");
  652. return -ENOSPC;
  653. }
  654. ir_hpet[free].iommu = iommu;
  655. ir_hpet[free].id = scope->enumeration_id;
  656. ir_hpet[free].bus = bus;
  657. ir_hpet[free].devfn = PCI_DEVFN(path->device, path->function);
  658. pr_info("HPET id %d under DRHD base 0x%Lx\n",
  659. scope->enumeration_id, drhd->address);
  660. return 0;
  661. }
  662. static int ir_parse_one_ioapic_scope(struct acpi_dmar_device_scope *scope,
  663. struct intel_iommu *iommu,
  664. struct acpi_dmar_hardware_unit *drhd)
  665. {
  666. struct acpi_dmar_pci_path *path;
  667. u8 bus;
  668. int count, free = -1;
  669. bus = scope->bus;
  670. path = (struct acpi_dmar_pci_path *)(scope + 1);
  671. count = (scope->length - sizeof(struct acpi_dmar_device_scope))
  672. / sizeof(struct acpi_dmar_pci_path);
  673. while (--count > 0) {
  674. /*
  675. * Access PCI directly due to the PCI
  676. * subsystem isn't initialized yet.
  677. */
  678. bus = read_pci_config_byte(bus, path->device, path->function,
  679. PCI_SECONDARY_BUS);
  680. path++;
  681. }
  682. for (count = 0; count < MAX_IO_APICS; count++) {
  683. if (ir_ioapic[count].iommu == iommu &&
  684. ir_ioapic[count].id == scope->enumeration_id)
  685. return 0;
  686. else if (ir_ioapic[count].iommu == NULL && free == -1)
  687. free = count;
  688. }
  689. if (free == -1) {
  690. pr_warn("Exceeded Max IO APICS\n");
  691. return -ENOSPC;
  692. }
  693. ir_ioapic[free].bus = bus;
  694. ir_ioapic[free].devfn = PCI_DEVFN(path->device, path->function);
  695. ir_ioapic[free].iommu = iommu;
  696. ir_ioapic[free].id = scope->enumeration_id;
  697. pr_info("IOAPIC id %d under DRHD base 0x%Lx IOMMU %d\n",
  698. scope->enumeration_id, drhd->address, iommu->seq_id);
  699. return 0;
  700. }
  701. static int ir_parse_ioapic_hpet_scope(struct acpi_dmar_header *header,
  702. struct intel_iommu *iommu)
  703. {
  704. int ret = 0;
  705. struct acpi_dmar_hardware_unit *drhd;
  706. struct acpi_dmar_device_scope *scope;
  707. void *start, *end;
  708. drhd = (struct acpi_dmar_hardware_unit *)header;
  709. start = (void *)(drhd + 1);
  710. end = ((void *)drhd) + header->length;
  711. while (start < end && ret == 0) {
  712. scope = start;
  713. if (scope->entry_type == ACPI_DMAR_SCOPE_TYPE_IOAPIC)
  714. ret = ir_parse_one_ioapic_scope(scope, iommu, drhd);
  715. else if (scope->entry_type == ACPI_DMAR_SCOPE_TYPE_HPET)
  716. ret = ir_parse_one_hpet_scope(scope, iommu, drhd);
  717. start += scope->length;
  718. }
  719. return ret;
  720. }
  721. static void ir_remove_ioapic_hpet_scope(struct intel_iommu *iommu)
  722. {
  723. int i;
  724. for (i = 0; i < MAX_HPET_TBS; i++)
  725. if (ir_hpet[i].iommu == iommu)
  726. ir_hpet[i].iommu = NULL;
  727. for (i = 0; i < MAX_IO_APICS; i++)
  728. if (ir_ioapic[i].iommu == iommu)
  729. ir_ioapic[i].iommu = NULL;
  730. }
  731. /*
  732. * Finds the assocaition between IOAPIC's and its Interrupt-remapping
  733. * hardware unit.
  734. */
  735. static int __init parse_ioapics_under_ir(void)
  736. {
  737. struct dmar_drhd_unit *drhd;
  738. struct intel_iommu *iommu;
  739. bool ir_supported = false;
  740. int ioapic_idx;
  741. for_each_iommu(iommu, drhd)
  742. if (ecap_ir_support(iommu->ecap)) {
  743. if (ir_parse_ioapic_hpet_scope(drhd->hdr, iommu))
  744. return -1;
  745. ir_supported = true;
  746. }
  747. if (!ir_supported)
  748. return 0;
  749. for (ioapic_idx = 0; ioapic_idx < nr_ioapics; ioapic_idx++) {
  750. int ioapic_id = mpc_ioapic_id(ioapic_idx);
  751. if (!map_ioapic_to_ir(ioapic_id)) {
  752. pr_err(FW_BUG "ioapic %d has no mapping iommu, "
  753. "interrupt remapping will be disabled\n",
  754. ioapic_id);
  755. return -1;
  756. }
  757. }
  758. return 1;
  759. }
  760. static int __init ir_dev_scope_init(void)
  761. {
  762. int ret;
  763. if (!irq_remapping_enabled)
  764. return 0;
  765. down_write(&dmar_global_lock);
  766. ret = dmar_dev_scope_init();
  767. up_write(&dmar_global_lock);
  768. return ret;
  769. }
  770. rootfs_initcall(ir_dev_scope_init);
  771. static void disable_irq_remapping(void)
  772. {
  773. struct dmar_drhd_unit *drhd;
  774. struct intel_iommu *iommu = NULL;
  775. /*
  776. * Disable Interrupt-remapping for all the DRHD's now.
  777. */
  778. for_each_iommu(iommu, drhd) {
  779. if (!ecap_ir_support(iommu->ecap))
  780. continue;
  781. iommu_disable_irq_remapping(iommu);
  782. }
  783. /*
  784. * Clear Posted-Interrupts capability.
  785. */
  786. if (!disable_irq_post)
  787. intel_irq_remap_ops.capability &= ~(1 << IRQ_POSTING_CAP);
  788. }
  789. static int reenable_irq_remapping(int eim)
  790. {
  791. struct dmar_drhd_unit *drhd;
  792. bool setup = false;
  793. struct intel_iommu *iommu = NULL;
  794. for_each_iommu(iommu, drhd)
  795. if (iommu->qi)
  796. dmar_reenable_qi(iommu);
  797. /*
  798. * Setup Interrupt-remapping for all the DRHD's now.
  799. */
  800. for_each_iommu(iommu, drhd) {
  801. if (!ecap_ir_support(iommu->ecap))
  802. continue;
  803. /* Set up interrupt remapping for iommu.*/
  804. iommu_set_irq_remapping(iommu, eim);
  805. iommu_enable_irq_remapping(iommu);
  806. setup = true;
  807. }
  808. if (!setup)
  809. goto error;
  810. set_irq_posting_cap();
  811. return 0;
  812. error:
  813. /*
  814. * handle error condition gracefully here!
  815. */
  816. return -1;
  817. }
  818. static void prepare_irte(struct irte *irte, int vector, unsigned int dest)
  819. {
  820. memset(irte, 0, sizeof(*irte));
  821. irte->present = 1;
  822. irte->dst_mode = apic->irq_dest_mode;
  823. /*
  824. * Trigger mode in the IRTE will always be edge, and for IO-APIC, the
  825. * actual level or edge trigger will be setup in the IO-APIC
  826. * RTE. This will help simplify level triggered irq migration.
  827. * For more details, see the comments (in io_apic.c) explainig IO-APIC
  828. * irq migration in the presence of interrupt-remapping.
  829. */
  830. irte->trigger_mode = 0;
  831. irte->dlvry_mode = apic->irq_delivery_mode;
  832. irte->vector = vector;
  833. irte->dest_id = IRTE_DEST(dest);
  834. irte->redir_hint = 1;
  835. }
  836. static struct irq_domain *intel_get_ir_irq_domain(struct irq_alloc_info *info)
  837. {
  838. struct intel_iommu *iommu = NULL;
  839. if (!info)
  840. return NULL;
  841. switch (info->type) {
  842. case X86_IRQ_ALLOC_TYPE_IOAPIC:
  843. iommu = map_ioapic_to_ir(info->ioapic_id);
  844. break;
  845. case X86_IRQ_ALLOC_TYPE_HPET:
  846. iommu = map_hpet_to_ir(info->hpet_id);
  847. break;
  848. case X86_IRQ_ALLOC_TYPE_MSI:
  849. case X86_IRQ_ALLOC_TYPE_MSIX:
  850. iommu = map_dev_to_ir(info->msi_dev);
  851. break;
  852. default:
  853. BUG_ON(1);
  854. break;
  855. }
  856. return iommu ? iommu->ir_domain : NULL;
  857. }
  858. static struct irq_domain *intel_get_irq_domain(struct irq_alloc_info *info)
  859. {
  860. struct intel_iommu *iommu;
  861. if (!info)
  862. return NULL;
  863. switch (info->type) {
  864. case X86_IRQ_ALLOC_TYPE_MSI:
  865. case X86_IRQ_ALLOC_TYPE_MSIX:
  866. iommu = map_dev_to_ir(info->msi_dev);
  867. if (iommu)
  868. return iommu->ir_msi_domain;
  869. break;
  870. default:
  871. break;
  872. }
  873. return NULL;
  874. }
  875. struct irq_remap_ops intel_irq_remap_ops = {
  876. .prepare = intel_prepare_irq_remapping,
  877. .enable = intel_enable_irq_remapping,
  878. .disable = disable_irq_remapping,
  879. .reenable = reenable_irq_remapping,
  880. .enable_faulting = enable_drhd_fault_handling,
  881. .get_ir_irq_domain = intel_get_ir_irq_domain,
  882. .get_irq_domain = intel_get_irq_domain,
  883. };
  884. /*
  885. * Migrate the IO-APIC irq in the presence of intr-remapping.
  886. *
  887. * For both level and edge triggered, irq migration is a simple atomic
  888. * update(of vector and cpu destination) of IRTE and flush the hardware cache.
  889. *
  890. * For level triggered, we eliminate the io-apic RTE modification (with the
  891. * updated vector information), by using a virtual vector (io-apic pin number).
  892. * Real vector that is used for interrupting cpu will be coming from
  893. * the interrupt-remapping table entry.
  894. *
  895. * As the migration is a simple atomic update of IRTE, the same mechanism
  896. * is used to migrate MSI irq's in the presence of interrupt-remapping.
  897. */
  898. static int
  899. intel_ir_set_affinity(struct irq_data *data, const struct cpumask *mask,
  900. bool force)
  901. {
  902. struct intel_ir_data *ir_data = data->chip_data;
  903. struct irte *irte = &ir_data->irte_entry;
  904. struct irq_cfg *cfg = irqd_cfg(data);
  905. struct irq_data *parent = data->parent_data;
  906. int ret;
  907. ret = parent->chip->irq_set_affinity(parent, mask, force);
  908. if (ret < 0 || ret == IRQ_SET_MASK_OK_DONE)
  909. return ret;
  910. /*
  911. * Atomically updates the IRTE with the new destination, vector
  912. * and flushes the interrupt entry cache.
  913. */
  914. irte->vector = cfg->vector;
  915. irte->dest_id = IRTE_DEST(cfg->dest_apicid);
  916. /* Update the hardware only if the interrupt is in remapped mode. */
  917. if (ir_data->irq_2_iommu.mode == IRQ_REMAPPING)
  918. modify_irte(&ir_data->irq_2_iommu, irte);
  919. /*
  920. * After this point, all the interrupts will start arriving
  921. * at the new destination. So, time to cleanup the previous
  922. * vector allocation.
  923. */
  924. send_cleanup_vector(cfg);
  925. return IRQ_SET_MASK_OK_DONE;
  926. }
  927. static void intel_ir_compose_msi_msg(struct irq_data *irq_data,
  928. struct msi_msg *msg)
  929. {
  930. struct intel_ir_data *ir_data = irq_data->chip_data;
  931. *msg = ir_data->msi_entry;
  932. }
  933. static int intel_ir_set_vcpu_affinity(struct irq_data *data, void *info)
  934. {
  935. struct intel_ir_data *ir_data = data->chip_data;
  936. struct vcpu_data *vcpu_pi_info = info;
  937. /* stop posting interrupts, back to remapping mode */
  938. if (!vcpu_pi_info) {
  939. modify_irte(&ir_data->irq_2_iommu, &ir_data->irte_entry);
  940. } else {
  941. struct irte irte_pi;
  942. /*
  943. * We are not caching the posted interrupt entry. We
  944. * copy the data from the remapped entry and modify
  945. * the fields which are relevant for posted mode. The
  946. * cached remapped entry is used for switching back to
  947. * remapped mode.
  948. */
  949. memset(&irte_pi, 0, sizeof(irte_pi));
  950. dmar_copy_shared_irte(&irte_pi, &ir_data->irte_entry);
  951. /* Update the posted mode fields */
  952. irte_pi.p_pst = 1;
  953. irte_pi.p_urgent = 0;
  954. irte_pi.p_vector = vcpu_pi_info->vector;
  955. irte_pi.pda_l = (vcpu_pi_info->pi_desc_addr >>
  956. (32 - PDA_LOW_BIT)) & ~(-1UL << PDA_LOW_BIT);
  957. irte_pi.pda_h = (vcpu_pi_info->pi_desc_addr >> 32) &
  958. ~(-1UL << PDA_HIGH_BIT);
  959. modify_irte(&ir_data->irq_2_iommu, &irte_pi);
  960. }
  961. return 0;
  962. }
  963. static struct irq_chip intel_ir_chip = {
  964. .irq_ack = ir_ack_apic_edge,
  965. .irq_set_affinity = intel_ir_set_affinity,
  966. .irq_compose_msi_msg = intel_ir_compose_msi_msg,
  967. .irq_set_vcpu_affinity = intel_ir_set_vcpu_affinity,
  968. };
  969. static void intel_irq_remapping_prepare_irte(struct intel_ir_data *data,
  970. struct irq_cfg *irq_cfg,
  971. struct irq_alloc_info *info,
  972. int index, int sub_handle)
  973. {
  974. struct IR_IO_APIC_route_entry *entry;
  975. struct irte *irte = &data->irte_entry;
  976. struct msi_msg *msg = &data->msi_entry;
  977. prepare_irte(irte, irq_cfg->vector, irq_cfg->dest_apicid);
  978. switch (info->type) {
  979. case X86_IRQ_ALLOC_TYPE_IOAPIC:
  980. /* Set source-id of interrupt request */
  981. set_ioapic_sid(irte, info->ioapic_id);
  982. apic_printk(APIC_VERBOSE, KERN_DEBUG "IOAPIC[%d]: Set IRTE entry (P:%d FPD:%d Dst_Mode:%d Redir_hint:%d Trig_Mode:%d Dlvry_Mode:%X Avail:%X Vector:%02X Dest:%08X SID:%04X SQ:%X SVT:%X)\n",
  983. info->ioapic_id, irte->present, irte->fpd,
  984. irte->dst_mode, irte->redir_hint,
  985. irte->trigger_mode, irte->dlvry_mode,
  986. irte->avail, irte->vector, irte->dest_id,
  987. irte->sid, irte->sq, irte->svt);
  988. entry = (struct IR_IO_APIC_route_entry *)info->ioapic_entry;
  989. info->ioapic_entry = NULL;
  990. memset(entry, 0, sizeof(*entry));
  991. entry->index2 = (index >> 15) & 0x1;
  992. entry->zero = 0;
  993. entry->format = 1;
  994. entry->index = (index & 0x7fff);
  995. /*
  996. * IO-APIC RTE will be configured with virtual vector.
  997. * irq handler will do the explicit EOI to the io-apic.
  998. */
  999. entry->vector = info->ioapic_pin;
  1000. entry->mask = 0; /* enable IRQ */
  1001. entry->trigger = info->ioapic_trigger;
  1002. entry->polarity = info->ioapic_polarity;
  1003. if (info->ioapic_trigger)
  1004. entry->mask = 1; /* Mask level triggered irqs. */
  1005. break;
  1006. case X86_IRQ_ALLOC_TYPE_HPET:
  1007. case X86_IRQ_ALLOC_TYPE_MSI:
  1008. case X86_IRQ_ALLOC_TYPE_MSIX:
  1009. if (info->type == X86_IRQ_ALLOC_TYPE_HPET)
  1010. set_hpet_sid(irte, info->hpet_id);
  1011. else
  1012. set_msi_sid(irte, info->msi_dev);
  1013. msg->address_hi = MSI_ADDR_BASE_HI;
  1014. msg->data = sub_handle;
  1015. msg->address_lo = MSI_ADDR_BASE_LO | MSI_ADDR_IR_EXT_INT |
  1016. MSI_ADDR_IR_SHV |
  1017. MSI_ADDR_IR_INDEX1(index) |
  1018. MSI_ADDR_IR_INDEX2(index);
  1019. break;
  1020. default:
  1021. BUG_ON(1);
  1022. break;
  1023. }
  1024. }
  1025. static void intel_free_irq_resources(struct irq_domain *domain,
  1026. unsigned int virq, unsigned int nr_irqs)
  1027. {
  1028. struct irq_data *irq_data;
  1029. struct intel_ir_data *data;
  1030. struct irq_2_iommu *irq_iommu;
  1031. unsigned long flags;
  1032. int i;
  1033. for (i = 0; i < nr_irqs; i++) {
  1034. irq_data = irq_domain_get_irq_data(domain, virq + i);
  1035. if (irq_data && irq_data->chip_data) {
  1036. data = irq_data->chip_data;
  1037. irq_iommu = &data->irq_2_iommu;
  1038. raw_spin_lock_irqsave(&irq_2_ir_lock, flags);
  1039. clear_entries(irq_iommu);
  1040. raw_spin_unlock_irqrestore(&irq_2_ir_lock, flags);
  1041. irq_domain_reset_irq_data(irq_data);
  1042. kfree(data);
  1043. }
  1044. }
  1045. }
  1046. static int intel_irq_remapping_alloc(struct irq_domain *domain,
  1047. unsigned int virq, unsigned int nr_irqs,
  1048. void *arg)
  1049. {
  1050. struct intel_iommu *iommu = domain->host_data;
  1051. struct irq_alloc_info *info = arg;
  1052. struct intel_ir_data *data, *ird;
  1053. struct irq_data *irq_data;
  1054. struct irq_cfg *irq_cfg;
  1055. int i, ret, index;
  1056. if (!info || !iommu)
  1057. return -EINVAL;
  1058. if (nr_irqs > 1 && info->type != X86_IRQ_ALLOC_TYPE_MSI &&
  1059. info->type != X86_IRQ_ALLOC_TYPE_MSIX)
  1060. return -EINVAL;
  1061. /*
  1062. * With IRQ remapping enabled, don't need contiguous CPU vectors
  1063. * to support multiple MSI interrupts.
  1064. */
  1065. if (info->type == X86_IRQ_ALLOC_TYPE_MSI)
  1066. info->flags &= ~X86_IRQ_ALLOC_CONTIGUOUS_VECTORS;
  1067. ret = irq_domain_alloc_irqs_parent(domain, virq, nr_irqs, arg);
  1068. if (ret < 0)
  1069. return ret;
  1070. ret = -ENOMEM;
  1071. data = kzalloc(sizeof(*data), GFP_KERNEL);
  1072. if (!data)
  1073. goto out_free_parent;
  1074. down_read(&dmar_global_lock);
  1075. index = alloc_irte(iommu, virq, &data->irq_2_iommu, nr_irqs);
  1076. up_read(&dmar_global_lock);
  1077. if (index < 0) {
  1078. pr_warn("Failed to allocate IRTE\n");
  1079. kfree(data);
  1080. goto out_free_parent;
  1081. }
  1082. for (i = 0; i < nr_irqs; i++) {
  1083. irq_data = irq_domain_get_irq_data(domain, virq + i);
  1084. irq_cfg = irqd_cfg(irq_data);
  1085. if (!irq_data || !irq_cfg) {
  1086. ret = -EINVAL;
  1087. goto out_free_data;
  1088. }
  1089. if (i > 0) {
  1090. ird = kzalloc(sizeof(*ird), GFP_KERNEL);
  1091. if (!ird)
  1092. goto out_free_data;
  1093. /* Initialize the common data */
  1094. ird->irq_2_iommu = data->irq_2_iommu;
  1095. ird->irq_2_iommu.sub_handle = i;
  1096. } else {
  1097. ird = data;
  1098. }
  1099. irq_data->hwirq = (index << 16) + i;
  1100. irq_data->chip_data = ird;
  1101. irq_data->chip = &intel_ir_chip;
  1102. intel_irq_remapping_prepare_irte(ird, irq_cfg, info, index, i);
  1103. irq_set_status_flags(virq + i, IRQ_MOVE_PCNTXT);
  1104. }
  1105. return 0;
  1106. out_free_data:
  1107. intel_free_irq_resources(domain, virq, i);
  1108. out_free_parent:
  1109. irq_domain_free_irqs_common(domain, virq, nr_irqs);
  1110. return ret;
  1111. }
  1112. static void intel_irq_remapping_free(struct irq_domain *domain,
  1113. unsigned int virq, unsigned int nr_irqs)
  1114. {
  1115. intel_free_irq_resources(domain, virq, nr_irqs);
  1116. irq_domain_free_irqs_common(domain, virq, nr_irqs);
  1117. }
  1118. static void intel_irq_remapping_activate(struct irq_domain *domain,
  1119. struct irq_data *irq_data)
  1120. {
  1121. struct intel_ir_data *data = irq_data->chip_data;
  1122. modify_irte(&data->irq_2_iommu, &data->irte_entry);
  1123. }
  1124. static void intel_irq_remapping_deactivate(struct irq_domain *domain,
  1125. struct irq_data *irq_data)
  1126. {
  1127. struct intel_ir_data *data = irq_data->chip_data;
  1128. struct irte entry;
  1129. memset(&entry, 0, sizeof(entry));
  1130. modify_irte(&data->irq_2_iommu, &entry);
  1131. }
  1132. static struct irq_domain_ops intel_ir_domain_ops = {
  1133. .alloc = intel_irq_remapping_alloc,
  1134. .free = intel_irq_remapping_free,
  1135. .activate = intel_irq_remapping_activate,
  1136. .deactivate = intel_irq_remapping_deactivate,
  1137. };
  1138. /*
  1139. * Support of Interrupt Remapping Unit Hotplug
  1140. */
  1141. static int dmar_ir_add(struct dmar_drhd_unit *dmaru, struct intel_iommu *iommu)
  1142. {
  1143. int ret;
  1144. int eim = x2apic_enabled();
  1145. if (eim && !ecap_eim_support(iommu->ecap)) {
  1146. pr_info("DRHD %Lx: EIM not supported by DRHD, ecap %Lx\n",
  1147. iommu->reg_phys, iommu->ecap);
  1148. return -ENODEV;
  1149. }
  1150. if (ir_parse_ioapic_hpet_scope(dmaru->hdr, iommu)) {
  1151. pr_warn("DRHD %Lx: failed to parse managed IOAPIC/HPET\n",
  1152. iommu->reg_phys);
  1153. return -ENODEV;
  1154. }
  1155. /* TODO: check all IOAPICs are covered by IOMMU */
  1156. /* Setup Interrupt-remapping now. */
  1157. ret = intel_setup_irq_remapping(iommu);
  1158. if (ret) {
  1159. pr_err("Failed to setup irq remapping for %s\n",
  1160. iommu->name);
  1161. intel_teardown_irq_remapping(iommu);
  1162. ir_remove_ioapic_hpet_scope(iommu);
  1163. } else {
  1164. iommu_enable_irq_remapping(iommu);
  1165. }
  1166. return ret;
  1167. }
  1168. int dmar_ir_hotplug(struct dmar_drhd_unit *dmaru, bool insert)
  1169. {
  1170. int ret = 0;
  1171. struct intel_iommu *iommu = dmaru->iommu;
  1172. if (!irq_remapping_enabled)
  1173. return 0;
  1174. if (iommu == NULL)
  1175. return -EINVAL;
  1176. if (!ecap_ir_support(iommu->ecap))
  1177. return 0;
  1178. if (irq_remapping_cap(IRQ_POSTING_CAP) &&
  1179. !cap_pi_support(iommu->cap))
  1180. return -EBUSY;
  1181. if (insert) {
  1182. if (!iommu->ir_table)
  1183. ret = dmar_ir_add(dmaru, iommu);
  1184. } else {
  1185. if (iommu->ir_table) {
  1186. if (!bitmap_empty(iommu->ir_table->bitmap,
  1187. INTR_REMAP_TABLE_ENTRIES)) {
  1188. ret = -EBUSY;
  1189. } else {
  1190. iommu_disable_irq_remapping(iommu);
  1191. intel_teardown_irq_remapping(iommu);
  1192. ir_remove_ioapic_hpet_scope(iommu);
  1193. }
  1194. }
  1195. }
  1196. return ret;
  1197. }