intel_irq_remapping.c 32 KB

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