intel_irq_remapping.c 36 KB

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