irq_remapping.c 40 KB

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