intel_irq_remapping.c 35 KB

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