intel_irq_remapping.c 36 KB

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