intel_irq_remapping.c 36 KB

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