intel_irq_remapping.c 36 KB

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