intel_irq_remapping.c 33 KB

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