intel_irq_remapping.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481
  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. /*
  569. * VT-d has a different layout for IO-APIC entries when
  570. * interrupt remapping is enabled. So it needs a special routine
  571. * to print IO-APIC entries for debugging purposes too.
  572. */
  573. x86_io_apic_ops.print_entries = intel_ir_io_apic_print_entries;
  574. pr_info("Enabled IRQ remapping in %s mode\n", eim ? "x2apic" : "xapic");
  575. return eim ? IRQ_REMAP_X2APIC_MODE : IRQ_REMAP_XAPIC_MODE;
  576. error:
  577. intel_cleanup_irq_remapping();
  578. return -1;
  579. }
  580. static int ir_parse_one_hpet_scope(struct acpi_dmar_device_scope *scope,
  581. struct intel_iommu *iommu,
  582. struct acpi_dmar_hardware_unit *drhd)
  583. {
  584. struct acpi_dmar_pci_path *path;
  585. u8 bus;
  586. int count, free = -1;
  587. bus = scope->bus;
  588. path = (struct acpi_dmar_pci_path *)(scope + 1);
  589. count = (scope->length - sizeof(struct acpi_dmar_device_scope))
  590. / sizeof(struct acpi_dmar_pci_path);
  591. while (--count > 0) {
  592. /*
  593. * Access PCI directly due to the PCI
  594. * subsystem isn't initialized yet.
  595. */
  596. bus = read_pci_config_byte(bus, path->device, path->function,
  597. PCI_SECONDARY_BUS);
  598. path++;
  599. }
  600. for (count = 0; count < MAX_HPET_TBS; count++) {
  601. if (ir_hpet[count].iommu == iommu &&
  602. ir_hpet[count].id == scope->enumeration_id)
  603. return 0;
  604. else if (ir_hpet[count].iommu == NULL && free == -1)
  605. free = count;
  606. }
  607. if (free == -1) {
  608. pr_warn("Exceeded Max HPET blocks\n");
  609. return -ENOSPC;
  610. }
  611. ir_hpet[free].iommu = iommu;
  612. ir_hpet[free].id = scope->enumeration_id;
  613. ir_hpet[free].bus = bus;
  614. ir_hpet[free].devfn = PCI_DEVFN(path->device, path->function);
  615. pr_info("HPET id %d under DRHD base 0x%Lx\n",
  616. scope->enumeration_id, drhd->address);
  617. return 0;
  618. }
  619. static int ir_parse_one_ioapic_scope(struct acpi_dmar_device_scope *scope,
  620. struct intel_iommu *iommu,
  621. struct acpi_dmar_hardware_unit *drhd)
  622. {
  623. struct acpi_dmar_pci_path *path;
  624. u8 bus;
  625. int count, free = -1;
  626. bus = scope->bus;
  627. path = (struct acpi_dmar_pci_path *)(scope + 1);
  628. count = (scope->length - sizeof(struct acpi_dmar_device_scope))
  629. / sizeof(struct acpi_dmar_pci_path);
  630. while (--count > 0) {
  631. /*
  632. * Access PCI directly due to the PCI
  633. * subsystem isn't initialized yet.
  634. */
  635. bus = read_pci_config_byte(bus, path->device, path->function,
  636. PCI_SECONDARY_BUS);
  637. path++;
  638. }
  639. for (count = 0; count < MAX_IO_APICS; count++) {
  640. if (ir_ioapic[count].iommu == iommu &&
  641. ir_ioapic[count].id == scope->enumeration_id)
  642. return 0;
  643. else if (ir_ioapic[count].iommu == NULL && free == -1)
  644. free = count;
  645. }
  646. if (free == -1) {
  647. pr_warn("Exceeded Max IO APICS\n");
  648. return -ENOSPC;
  649. }
  650. ir_ioapic[free].bus = bus;
  651. ir_ioapic[free].devfn = PCI_DEVFN(path->device, path->function);
  652. ir_ioapic[free].iommu = iommu;
  653. ir_ioapic[free].id = scope->enumeration_id;
  654. pr_info("IOAPIC id %d under DRHD base 0x%Lx IOMMU %d\n",
  655. scope->enumeration_id, drhd->address, iommu->seq_id);
  656. return 0;
  657. }
  658. static int ir_parse_ioapic_hpet_scope(struct acpi_dmar_header *header,
  659. struct intel_iommu *iommu)
  660. {
  661. int ret = 0;
  662. struct acpi_dmar_hardware_unit *drhd;
  663. struct acpi_dmar_device_scope *scope;
  664. void *start, *end;
  665. drhd = (struct acpi_dmar_hardware_unit *)header;
  666. start = (void *)(drhd + 1);
  667. end = ((void *)drhd) + header->length;
  668. while (start < end && ret == 0) {
  669. scope = start;
  670. if (scope->entry_type == ACPI_DMAR_SCOPE_TYPE_IOAPIC)
  671. ret = ir_parse_one_ioapic_scope(scope, iommu, drhd);
  672. else if (scope->entry_type == ACPI_DMAR_SCOPE_TYPE_HPET)
  673. ret = ir_parse_one_hpet_scope(scope, iommu, drhd);
  674. start += scope->length;
  675. }
  676. return ret;
  677. }
  678. static void ir_remove_ioapic_hpet_scope(struct intel_iommu *iommu)
  679. {
  680. int i;
  681. for (i = 0; i < MAX_HPET_TBS; i++)
  682. if (ir_hpet[i].iommu == iommu)
  683. ir_hpet[i].iommu = NULL;
  684. for (i = 0; i < MAX_IO_APICS; i++)
  685. if (ir_ioapic[i].iommu == iommu)
  686. ir_ioapic[i].iommu = NULL;
  687. }
  688. /*
  689. * Finds the assocaition between IOAPIC's and its Interrupt-remapping
  690. * hardware unit.
  691. */
  692. static int __init parse_ioapics_under_ir(void)
  693. {
  694. struct dmar_drhd_unit *drhd;
  695. struct intel_iommu *iommu;
  696. bool ir_supported = false;
  697. int ioapic_idx;
  698. for_each_iommu(iommu, drhd)
  699. if (ecap_ir_support(iommu->ecap)) {
  700. if (ir_parse_ioapic_hpet_scope(drhd->hdr, iommu))
  701. return -1;
  702. ir_supported = true;
  703. }
  704. if (!ir_supported)
  705. return 0;
  706. for (ioapic_idx = 0; ioapic_idx < nr_ioapics; ioapic_idx++) {
  707. int ioapic_id = mpc_ioapic_id(ioapic_idx);
  708. if (!map_ioapic_to_ir(ioapic_id)) {
  709. pr_err(FW_BUG "ioapic %d has no mapping iommu, "
  710. "interrupt remapping will be disabled\n",
  711. ioapic_id);
  712. return -1;
  713. }
  714. }
  715. return 1;
  716. }
  717. static int __init ir_dev_scope_init(void)
  718. {
  719. int ret;
  720. if (!irq_remapping_enabled)
  721. return 0;
  722. down_write(&dmar_global_lock);
  723. ret = dmar_dev_scope_init();
  724. up_write(&dmar_global_lock);
  725. return ret;
  726. }
  727. rootfs_initcall(ir_dev_scope_init);
  728. static void disable_irq_remapping(void)
  729. {
  730. struct dmar_drhd_unit *drhd;
  731. struct intel_iommu *iommu = NULL;
  732. /*
  733. * Disable Interrupt-remapping for all the DRHD's now.
  734. */
  735. for_each_iommu(iommu, drhd) {
  736. if (!ecap_ir_support(iommu->ecap))
  737. continue;
  738. iommu_disable_irq_remapping(iommu);
  739. }
  740. }
  741. static int reenable_irq_remapping(int eim)
  742. {
  743. struct dmar_drhd_unit *drhd;
  744. bool setup = false;
  745. struct intel_iommu *iommu = NULL;
  746. for_each_iommu(iommu, drhd)
  747. if (iommu->qi)
  748. dmar_reenable_qi(iommu);
  749. /*
  750. * Setup Interrupt-remapping for all the DRHD's now.
  751. */
  752. for_each_iommu(iommu, drhd) {
  753. if (!ecap_ir_support(iommu->ecap))
  754. continue;
  755. /* Set up interrupt remapping for iommu.*/
  756. iommu_set_irq_remapping(iommu, eim);
  757. setup = true;
  758. }
  759. if (!setup)
  760. goto error;
  761. return 0;
  762. error:
  763. /*
  764. * handle error condition gracefully here!
  765. */
  766. return -1;
  767. }
  768. static void prepare_irte(struct irte *irte, int vector,
  769. unsigned int dest)
  770. {
  771. memset(irte, 0, sizeof(*irte));
  772. irte->present = 1;
  773. irte->dst_mode = apic->irq_dest_mode;
  774. /*
  775. * Trigger mode in the IRTE will always be edge, and for IO-APIC, the
  776. * actual level or edge trigger will be setup in the IO-APIC
  777. * RTE. This will help simplify level triggered irq migration.
  778. * For more details, see the comments (in io_apic.c) explainig IO-APIC
  779. * irq migration in the presence of interrupt-remapping.
  780. */
  781. irte->trigger_mode = 0;
  782. irte->dlvry_mode = apic->irq_delivery_mode;
  783. irte->vector = vector;
  784. irte->dest_id = IRTE_DEST(dest);
  785. irte->redir_hint = 1;
  786. }
  787. static int intel_setup_ioapic_entry(int irq,
  788. struct IO_APIC_route_entry *route_entry,
  789. unsigned int destination, int vector,
  790. struct io_apic_irq_attr *attr)
  791. {
  792. int ioapic_id = mpc_ioapic_id(attr->ioapic);
  793. struct intel_iommu *iommu;
  794. struct IR_IO_APIC_route_entry *entry;
  795. struct irte irte;
  796. int index;
  797. down_read(&dmar_global_lock);
  798. iommu = map_ioapic_to_ir(ioapic_id);
  799. if (!iommu) {
  800. pr_warn("No mapping iommu for ioapic %d\n", ioapic_id);
  801. index = -ENODEV;
  802. } else {
  803. index = alloc_irte(iommu, irq, irq_2_iommu(irq), 1);
  804. if (index < 0) {
  805. pr_warn("Failed to allocate IRTE for ioapic %d\n",
  806. ioapic_id);
  807. index = -ENOMEM;
  808. }
  809. }
  810. up_read(&dmar_global_lock);
  811. if (index < 0)
  812. return index;
  813. prepare_irte(&irte, vector, destination);
  814. /* Set source-id of interrupt request */
  815. set_ioapic_sid(&irte, ioapic_id);
  816. modify_irte(irq_2_iommu(irq), &irte);
  817. apic_printk(APIC_VERBOSE, KERN_DEBUG "IOAPIC[%d]: "
  818. "Set IRTE entry (P:%d FPD:%d Dst_Mode:%d "
  819. "Redir_hint:%d Trig_Mode:%d Dlvry_Mode:%X "
  820. "Avail:%X Vector:%02X Dest:%08X "
  821. "SID:%04X SQ:%X SVT:%X)\n",
  822. attr->ioapic, irte.present, irte.fpd, irte.dst_mode,
  823. irte.redir_hint, irte.trigger_mode, irte.dlvry_mode,
  824. irte.avail, irte.vector, irte.dest_id,
  825. irte.sid, irte.sq, irte.svt);
  826. entry = (struct IR_IO_APIC_route_entry *)route_entry;
  827. memset(entry, 0, sizeof(*entry));
  828. entry->index2 = (index >> 15) & 0x1;
  829. entry->zero = 0;
  830. entry->format = 1;
  831. entry->index = (index & 0x7fff);
  832. /*
  833. * IO-APIC RTE will be configured with virtual vector.
  834. * irq handler will do the explicit EOI to the io-apic.
  835. */
  836. entry->vector = attr->ioapic_pin;
  837. entry->mask = 0; /* enable IRQ */
  838. entry->trigger = attr->trigger;
  839. entry->polarity = attr->polarity;
  840. /* Mask level triggered irqs.
  841. * Use IRQ_DELAYED_DISABLE for edge triggered irqs.
  842. */
  843. if (attr->trigger)
  844. entry->mask = 1;
  845. return 0;
  846. }
  847. /*
  848. * Migrate the IO-APIC irq in the presence of intr-remapping.
  849. *
  850. * For both level and edge triggered, irq migration is a simple atomic
  851. * update(of vector and cpu destination) of IRTE and flush the hardware cache.
  852. *
  853. * For level triggered, we eliminate the io-apic RTE modification (with the
  854. * updated vector information), by using a virtual vector (io-apic pin number).
  855. * Real vector that is used for interrupting cpu will be coming from
  856. * the interrupt-remapping table entry.
  857. *
  858. * As the migration is a simple atomic update of IRTE, the same mechanism
  859. * is used to migrate MSI irq's in the presence of interrupt-remapping.
  860. */
  861. static int
  862. intel_ioapic_set_affinity(struct irq_data *data, const struct cpumask *mask,
  863. bool force)
  864. {
  865. struct irq_cfg *cfg = irqd_cfg(data);
  866. unsigned int dest, irq = data->irq;
  867. struct irte irte;
  868. int err;
  869. if (get_irte(irq, &irte))
  870. return -EBUSY;
  871. err = assign_irq_vector(irq, cfg, mask);
  872. if (err)
  873. return err;
  874. err = apic->cpu_mask_to_apicid_and(cfg->domain, mask, &dest);
  875. if (err) {
  876. if (assign_irq_vector(irq, cfg, data->affinity))
  877. pr_err("Failed to recover vector for irq %d\n", irq);
  878. return err;
  879. }
  880. irte.vector = cfg->vector;
  881. irte.dest_id = IRTE_DEST(dest);
  882. /*
  883. * Atomically updates the IRTE with the new destination, vector
  884. * and flushes the interrupt entry cache.
  885. */
  886. modify_irte(irq_2_iommu(irq), &irte);
  887. /*
  888. * After this point, all the interrupts will start arriving
  889. * at the new destination. So, time to cleanup the previous
  890. * vector allocation.
  891. */
  892. if (cfg->move_in_progress)
  893. send_cleanup_vector(cfg);
  894. cpumask_copy(data->affinity, mask);
  895. return 0;
  896. }
  897. static struct irq_domain *intel_get_ir_irq_domain(struct irq_alloc_info *info)
  898. {
  899. struct intel_iommu *iommu = NULL;
  900. if (!info)
  901. return NULL;
  902. switch (info->type) {
  903. case X86_IRQ_ALLOC_TYPE_IOAPIC:
  904. iommu = map_ioapic_to_ir(info->ioapic_id);
  905. break;
  906. case X86_IRQ_ALLOC_TYPE_HPET:
  907. iommu = map_hpet_to_ir(info->hpet_id);
  908. break;
  909. case X86_IRQ_ALLOC_TYPE_MSI:
  910. case X86_IRQ_ALLOC_TYPE_MSIX:
  911. iommu = map_dev_to_ir(info->msi_dev);
  912. break;
  913. default:
  914. BUG_ON(1);
  915. break;
  916. }
  917. return iommu ? iommu->ir_domain : NULL;
  918. }
  919. static struct irq_domain *intel_get_irq_domain(struct irq_alloc_info *info)
  920. {
  921. struct intel_iommu *iommu;
  922. if (!info)
  923. return NULL;
  924. switch (info->type) {
  925. case X86_IRQ_ALLOC_TYPE_MSI:
  926. case X86_IRQ_ALLOC_TYPE_MSIX:
  927. iommu = map_dev_to_ir(info->msi_dev);
  928. if (iommu)
  929. return iommu->ir_msi_domain;
  930. break;
  931. default:
  932. break;
  933. }
  934. return NULL;
  935. }
  936. struct irq_remap_ops intel_irq_remap_ops = {
  937. .prepare = intel_prepare_irq_remapping,
  938. .enable = intel_enable_irq_remapping,
  939. .disable = disable_irq_remapping,
  940. .reenable = reenable_irq_remapping,
  941. .enable_faulting = enable_drhd_fault_handling,
  942. .setup_ioapic_entry = intel_setup_ioapic_entry,
  943. .set_affinity = intel_ioapic_set_affinity,
  944. .free_irq = free_irte,
  945. .get_ir_irq_domain = intel_get_ir_irq_domain,
  946. .get_irq_domain = intel_get_irq_domain,
  947. };
  948. /*
  949. * Migrate the IO-APIC irq in the presence of intr-remapping.
  950. *
  951. * For both level and edge triggered, irq migration is a simple atomic
  952. * update(of vector and cpu destination) of IRTE and flush the hardware cache.
  953. *
  954. * For level triggered, we eliminate the io-apic RTE modification (with the
  955. * updated vector information), by using a virtual vector (io-apic pin number).
  956. * Real vector that is used for interrupting cpu will be coming from
  957. * the interrupt-remapping table entry.
  958. *
  959. * As the migration is a simple atomic update of IRTE, the same mechanism
  960. * is used to migrate MSI irq's in the presence of interrupt-remapping.
  961. */
  962. static int
  963. intel_ir_set_affinity(struct irq_data *data, const struct cpumask *mask,
  964. bool force)
  965. {
  966. struct intel_ir_data *ir_data = data->chip_data;
  967. struct irte *irte = &ir_data->irte_entry;
  968. struct irq_cfg *cfg = irqd_cfg(data);
  969. struct irq_data *parent = data->parent_data;
  970. int ret;
  971. ret = parent->chip->irq_set_affinity(parent, mask, force);
  972. if (ret < 0 || ret == IRQ_SET_MASK_OK_DONE)
  973. return ret;
  974. /*
  975. * Atomically updates the IRTE with the new destination, vector
  976. * and flushes the interrupt entry cache.
  977. */
  978. irte->vector = cfg->vector;
  979. irte->dest_id = IRTE_DEST(cfg->dest_apicid);
  980. modify_irte(&ir_data->irq_2_iommu, irte);
  981. /*
  982. * After this point, all the interrupts will start arriving
  983. * at the new destination. So, time to cleanup the previous
  984. * vector allocation.
  985. */
  986. if (cfg->move_in_progress)
  987. send_cleanup_vector(cfg);
  988. return IRQ_SET_MASK_OK_DONE;
  989. }
  990. static void intel_ir_compose_msi_msg(struct irq_data *irq_data,
  991. struct msi_msg *msg)
  992. {
  993. struct intel_ir_data *ir_data = irq_data->chip_data;
  994. *msg = ir_data->msi_entry;
  995. }
  996. static struct irq_chip intel_ir_chip = {
  997. .irq_ack = ir_ack_apic_edge,
  998. .irq_set_affinity = intel_ir_set_affinity,
  999. .irq_compose_msi_msg = intel_ir_compose_msi_msg,
  1000. };
  1001. static void intel_irq_remapping_prepare_irte(struct intel_ir_data *data,
  1002. struct irq_cfg *irq_cfg,
  1003. struct irq_alloc_info *info,
  1004. int index, int sub_handle)
  1005. {
  1006. struct IR_IO_APIC_route_entry *entry;
  1007. struct irte *irte = &data->irte_entry;
  1008. struct msi_msg *msg = &data->msi_entry;
  1009. prepare_irte(irte, irq_cfg->vector, irq_cfg->dest_apicid);
  1010. switch (info->type) {
  1011. case X86_IRQ_ALLOC_TYPE_IOAPIC:
  1012. /* Set source-id of interrupt request */
  1013. set_ioapic_sid(irte, info->ioapic_id);
  1014. 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",
  1015. info->ioapic_id, irte->present, irte->fpd,
  1016. irte->dst_mode, irte->redir_hint,
  1017. irte->trigger_mode, irte->dlvry_mode,
  1018. irte->avail, irte->vector, irte->dest_id,
  1019. irte->sid, irte->sq, irte->svt);
  1020. entry = (struct IR_IO_APIC_route_entry *)info->ioapic_entry;
  1021. info->ioapic_entry = NULL;
  1022. memset(entry, 0, sizeof(*entry));
  1023. entry->index2 = (index >> 15) & 0x1;
  1024. entry->zero = 0;
  1025. entry->format = 1;
  1026. entry->index = (index & 0x7fff);
  1027. /*
  1028. * IO-APIC RTE will be configured with virtual vector.
  1029. * irq handler will do the explicit EOI to the io-apic.
  1030. */
  1031. entry->vector = info->ioapic_pin;
  1032. entry->mask = 0; /* enable IRQ */
  1033. entry->trigger = info->ioapic_trigger;
  1034. entry->polarity = info->ioapic_polarity;
  1035. if (info->ioapic_trigger)
  1036. entry->mask = 1; /* Mask level triggered irqs. */
  1037. break;
  1038. case X86_IRQ_ALLOC_TYPE_HPET:
  1039. case X86_IRQ_ALLOC_TYPE_MSI:
  1040. case X86_IRQ_ALLOC_TYPE_MSIX:
  1041. if (info->type == X86_IRQ_ALLOC_TYPE_HPET)
  1042. set_hpet_sid(irte, info->hpet_id);
  1043. else
  1044. set_msi_sid(irte, info->msi_dev);
  1045. msg->address_hi = MSI_ADDR_BASE_HI;
  1046. msg->data = sub_handle;
  1047. msg->address_lo = MSI_ADDR_BASE_LO | MSI_ADDR_IR_EXT_INT |
  1048. MSI_ADDR_IR_SHV |
  1049. MSI_ADDR_IR_INDEX1(index) |
  1050. MSI_ADDR_IR_INDEX2(index);
  1051. break;
  1052. default:
  1053. BUG_ON(1);
  1054. break;
  1055. }
  1056. }
  1057. static void intel_free_irq_resources(struct irq_domain *domain,
  1058. unsigned int virq, unsigned int nr_irqs)
  1059. {
  1060. struct irq_data *irq_data;
  1061. struct intel_ir_data *data;
  1062. struct irq_2_iommu *irq_iommu;
  1063. unsigned long flags;
  1064. int i;
  1065. for (i = 0; i < nr_irqs; i++) {
  1066. irq_data = irq_domain_get_irq_data(domain, virq + i);
  1067. if (irq_data && irq_data->chip_data) {
  1068. data = irq_data->chip_data;
  1069. irq_iommu = &data->irq_2_iommu;
  1070. raw_spin_lock_irqsave(&irq_2_ir_lock, flags);
  1071. clear_entries(irq_iommu);
  1072. raw_spin_unlock_irqrestore(&irq_2_ir_lock, flags);
  1073. irq_domain_reset_irq_data(irq_data);
  1074. kfree(data);
  1075. }
  1076. }
  1077. }
  1078. static int intel_irq_remapping_alloc(struct irq_domain *domain,
  1079. unsigned int virq, unsigned int nr_irqs,
  1080. void *arg)
  1081. {
  1082. struct intel_iommu *iommu = domain->host_data;
  1083. struct irq_alloc_info *info = arg;
  1084. struct intel_ir_data *data;
  1085. struct irq_data *irq_data;
  1086. struct irq_cfg *irq_cfg;
  1087. int i, ret, index;
  1088. if (!info || !iommu)
  1089. return -EINVAL;
  1090. if (nr_irqs > 1 && info->type != X86_IRQ_ALLOC_TYPE_MSI &&
  1091. info->type != X86_IRQ_ALLOC_TYPE_MSIX)
  1092. return -EINVAL;
  1093. /*
  1094. * With IRQ remapping enabled, don't need contiguous CPU vectors
  1095. * to support multiple MSI interrupts.
  1096. */
  1097. if (info->type == X86_IRQ_ALLOC_TYPE_MSI)
  1098. info->flags &= ~X86_IRQ_ALLOC_CONTIGUOUS_VECTORS;
  1099. ret = irq_domain_alloc_irqs_parent(domain, virq, nr_irqs, arg);
  1100. if (ret < 0)
  1101. return ret;
  1102. ret = -ENOMEM;
  1103. data = kzalloc(sizeof(*data), GFP_KERNEL);
  1104. if (!data)
  1105. goto out_free_parent;
  1106. down_read(&dmar_global_lock);
  1107. index = alloc_irte(iommu, virq, &data->irq_2_iommu, nr_irqs);
  1108. up_read(&dmar_global_lock);
  1109. if (index < 0) {
  1110. pr_warn("Failed to allocate IRTE\n");
  1111. kfree(data);
  1112. goto out_free_parent;
  1113. }
  1114. for (i = 0; i < nr_irqs; i++) {
  1115. irq_data = irq_domain_get_irq_data(domain, virq + i);
  1116. irq_cfg = irqd_cfg(irq_data);
  1117. if (!irq_data || !irq_cfg) {
  1118. ret = -EINVAL;
  1119. goto out_free_data;
  1120. }
  1121. if (i > 0) {
  1122. data = kzalloc(sizeof(*data), GFP_KERNEL);
  1123. if (!data)
  1124. goto out_free_data;
  1125. }
  1126. irq_data->hwirq = (index << 16) + i;
  1127. irq_data->chip_data = data;
  1128. irq_data->chip = &intel_ir_chip;
  1129. intel_irq_remapping_prepare_irte(data, irq_cfg, info, index, i);
  1130. irq_set_status_flags(virq + i, IRQ_MOVE_PCNTXT);
  1131. }
  1132. return 0;
  1133. out_free_data:
  1134. intel_free_irq_resources(domain, virq, i);
  1135. out_free_parent:
  1136. irq_domain_free_irqs_common(domain, virq, nr_irqs);
  1137. return ret;
  1138. }
  1139. static void intel_irq_remapping_free(struct irq_domain *domain,
  1140. unsigned int virq, unsigned int nr_irqs)
  1141. {
  1142. intel_free_irq_resources(domain, virq, nr_irqs);
  1143. irq_domain_free_irqs_common(domain, virq, nr_irqs);
  1144. }
  1145. static void intel_irq_remapping_activate(struct irq_domain *domain,
  1146. struct irq_data *irq_data)
  1147. {
  1148. struct intel_ir_data *data = irq_data->chip_data;
  1149. modify_irte(&data->irq_2_iommu, &data->irte_entry);
  1150. }
  1151. static void intel_irq_remapping_deactivate(struct irq_domain *domain,
  1152. struct irq_data *irq_data)
  1153. {
  1154. struct intel_ir_data *data = irq_data->chip_data;
  1155. struct irte entry;
  1156. memset(&entry, 0, sizeof(entry));
  1157. modify_irte(&data->irq_2_iommu, &entry);
  1158. }
  1159. static struct irq_domain_ops intel_ir_domain_ops = {
  1160. .alloc = intel_irq_remapping_alloc,
  1161. .free = intel_irq_remapping_free,
  1162. .activate = intel_irq_remapping_activate,
  1163. .deactivate = intel_irq_remapping_deactivate,
  1164. };
  1165. /*
  1166. * Support of Interrupt Remapping Unit Hotplug
  1167. */
  1168. static int dmar_ir_add(struct dmar_drhd_unit *dmaru, struct intel_iommu *iommu)
  1169. {
  1170. int ret;
  1171. int eim = x2apic_enabled();
  1172. if (eim && !ecap_eim_support(iommu->ecap)) {
  1173. pr_info("DRHD %Lx: EIM not supported by DRHD, ecap %Lx\n",
  1174. iommu->reg_phys, iommu->ecap);
  1175. return -ENODEV;
  1176. }
  1177. if (ir_parse_ioapic_hpet_scope(dmaru->hdr, iommu)) {
  1178. pr_warn("DRHD %Lx: failed to parse managed IOAPIC/HPET\n",
  1179. iommu->reg_phys);
  1180. return -ENODEV;
  1181. }
  1182. /* TODO: check all IOAPICs are covered by IOMMU */
  1183. /* Setup Interrupt-remapping now. */
  1184. ret = intel_setup_irq_remapping(iommu);
  1185. if (ret) {
  1186. pr_err("DRHD %Lx: failed to allocate resource\n",
  1187. iommu->reg_phys);
  1188. ir_remove_ioapic_hpet_scope(iommu);
  1189. return ret;
  1190. }
  1191. if (!iommu->qi) {
  1192. /* Clear previous faults. */
  1193. dmar_fault(-1, iommu);
  1194. iommu_disable_irq_remapping(iommu);
  1195. dmar_disable_qi(iommu);
  1196. }
  1197. /* Enable queued invalidation */
  1198. ret = dmar_enable_qi(iommu);
  1199. if (!ret) {
  1200. iommu_set_irq_remapping(iommu, eim);
  1201. } else {
  1202. pr_err("DRHD %Lx: failed to enable queued invalidation, ecap %Lx, ret %d\n",
  1203. iommu->reg_phys, iommu->ecap, ret);
  1204. intel_teardown_irq_remapping(iommu);
  1205. ir_remove_ioapic_hpet_scope(iommu);
  1206. }
  1207. return ret;
  1208. }
  1209. int dmar_ir_hotplug(struct dmar_drhd_unit *dmaru, bool insert)
  1210. {
  1211. int ret = 0;
  1212. struct intel_iommu *iommu = dmaru->iommu;
  1213. if (!irq_remapping_enabled)
  1214. return 0;
  1215. if (iommu == NULL)
  1216. return -EINVAL;
  1217. if (!ecap_ir_support(iommu->ecap))
  1218. return 0;
  1219. if (insert) {
  1220. if (!iommu->ir_table)
  1221. ret = dmar_ir_add(dmaru, iommu);
  1222. } else {
  1223. if (iommu->ir_table) {
  1224. if (!bitmap_empty(iommu->ir_table->bitmap,
  1225. INTR_REMAP_TABLE_ENTRIES)) {
  1226. ret = -EBUSY;
  1227. } else {
  1228. iommu_disable_irq_remapping(iommu);
  1229. intel_teardown_irq_remapping(iommu);
  1230. ir_remove_ioapic_hpet_scope(iommu);
  1231. }
  1232. }
  1233. }
  1234. return ret;
  1235. }