intel_irq_remapping.c 36 KB

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