intel_irq_remapping.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115
  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 <asm/io_apic.h>
  10. #include <asm/smp.h>
  11. #include <asm/cpu.h>
  12. #include <linux/intel-iommu.h>
  13. #include <acpi/acpi.h>
  14. #include <asm/irq_remapping.h>
  15. #include <asm/pci-direct.h>
  16. #include <asm/msidef.h>
  17. #include "irq_remapping.h"
  18. struct ioapic_scope {
  19. struct intel_iommu *iommu;
  20. unsigned int id;
  21. unsigned int bus; /* PCI bus number */
  22. unsigned int devfn; /* PCI devfn number */
  23. };
  24. struct hpet_scope {
  25. struct intel_iommu *iommu;
  26. u8 id;
  27. unsigned int bus;
  28. unsigned int devfn;
  29. };
  30. #define IR_X2APIC_MODE(mode) (mode ? (1 << 11) : 0)
  31. #define IRTE_DEST(dest) ((x2apic_mode) ? dest : dest << 8)
  32. static struct ioapic_scope ir_ioapic[MAX_IO_APICS];
  33. static struct hpet_scope ir_hpet[MAX_HPET_TBS];
  34. static int ir_ioapic_num, ir_hpet_num;
  35. static DEFINE_RAW_SPINLOCK(irq_2_ir_lock);
  36. static int __init parse_ioapics_under_ir(void);
  37. static struct irq_2_iommu *irq_2_iommu(unsigned int irq)
  38. {
  39. struct irq_cfg *cfg = irq_get_chip_data(irq);
  40. return cfg ? &cfg->irq_2_iommu : NULL;
  41. }
  42. static int get_irte(int irq, struct irte *entry)
  43. {
  44. struct irq_2_iommu *irq_iommu = irq_2_iommu(irq);
  45. unsigned long flags;
  46. int index;
  47. if (!entry || !irq_iommu)
  48. return -1;
  49. raw_spin_lock_irqsave(&irq_2_ir_lock, flags);
  50. index = irq_iommu->irte_index + irq_iommu->sub_handle;
  51. *entry = *(irq_iommu->iommu->ir_table->base + index);
  52. raw_spin_unlock_irqrestore(&irq_2_ir_lock, flags);
  53. return 0;
  54. }
  55. static int alloc_irte(struct intel_iommu *iommu, int irq, u16 count)
  56. {
  57. struct ir_table *table = iommu->ir_table;
  58. struct irq_2_iommu *irq_iommu = irq_2_iommu(irq);
  59. struct irq_cfg *cfg = irq_get_chip_data(irq);
  60. u16 index, start_index;
  61. unsigned int mask = 0;
  62. unsigned long flags;
  63. if (!count || !irq_iommu)
  64. return -1;
  65. /*
  66. * start the IRTE search from index 0.
  67. */
  68. index = start_index = 0;
  69. if (count > 1) {
  70. count = __roundup_pow_of_two(count);
  71. mask = ilog2(count);
  72. }
  73. if (mask > ecap_max_handle_mask(iommu->ecap)) {
  74. printk(KERN_ERR
  75. "Requested mask %x exceeds the max invalidation handle"
  76. " mask value %Lx\n", mask,
  77. ecap_max_handle_mask(iommu->ecap));
  78. return -1;
  79. }
  80. raw_spin_lock_irqsave(&irq_2_ir_lock, flags);
  81. index = bitmap_find_free_region(table->bitmap,
  82. INTR_REMAP_TABLE_ENTRIES, mask);
  83. if (index < 0) {
  84. pr_warn("IR%d: can't allocate an IRTE\n", iommu->seq_id);
  85. } else {
  86. cfg->remapped = 1;
  87. irq_iommu->iommu = iommu;
  88. irq_iommu->irte_index = index;
  89. irq_iommu->sub_handle = 0;
  90. irq_iommu->irte_mask = mask;
  91. }
  92. raw_spin_unlock_irqrestore(&irq_2_ir_lock, flags);
  93. return index;
  94. }
  95. static int qi_flush_iec(struct intel_iommu *iommu, int index, int mask)
  96. {
  97. struct qi_desc desc;
  98. desc.low = QI_IEC_IIDEX(index) | QI_IEC_TYPE | QI_IEC_IM(mask)
  99. | QI_IEC_SELECTIVE;
  100. desc.high = 0;
  101. return qi_submit_sync(&desc, iommu);
  102. }
  103. static int map_irq_to_irte_handle(int irq, u16 *sub_handle)
  104. {
  105. struct irq_2_iommu *irq_iommu = irq_2_iommu(irq);
  106. unsigned long flags;
  107. int index;
  108. if (!irq_iommu)
  109. return -1;
  110. raw_spin_lock_irqsave(&irq_2_ir_lock, flags);
  111. *sub_handle = irq_iommu->sub_handle;
  112. index = irq_iommu->irte_index;
  113. raw_spin_unlock_irqrestore(&irq_2_ir_lock, flags);
  114. return index;
  115. }
  116. static int set_irte_irq(int irq, struct intel_iommu *iommu, u16 index, u16 subhandle)
  117. {
  118. struct irq_2_iommu *irq_iommu = irq_2_iommu(irq);
  119. struct irq_cfg *cfg = irq_get_chip_data(irq);
  120. unsigned long flags;
  121. if (!irq_iommu)
  122. return -1;
  123. raw_spin_lock_irqsave(&irq_2_ir_lock, flags);
  124. cfg->remapped = 1;
  125. irq_iommu->iommu = iommu;
  126. irq_iommu->irte_index = index;
  127. irq_iommu->sub_handle = subhandle;
  128. irq_iommu->irte_mask = 0;
  129. raw_spin_unlock_irqrestore(&irq_2_ir_lock, flags);
  130. return 0;
  131. }
  132. static int modify_irte(int irq, struct irte *irte_modified)
  133. {
  134. struct irq_2_iommu *irq_iommu = irq_2_iommu(irq);
  135. struct intel_iommu *iommu;
  136. unsigned long flags;
  137. struct irte *irte;
  138. int rc, index;
  139. if (!irq_iommu)
  140. return -1;
  141. raw_spin_lock_irqsave(&irq_2_ir_lock, flags);
  142. iommu = irq_iommu->iommu;
  143. index = irq_iommu->irte_index + irq_iommu->sub_handle;
  144. irte = &iommu->ir_table->base[index];
  145. set_64bit(&irte->low, irte_modified->low);
  146. set_64bit(&irte->high, irte_modified->high);
  147. __iommu_flush_cache(iommu, irte, sizeof(*irte));
  148. rc = qi_flush_iec(iommu, index, 0);
  149. raw_spin_unlock_irqrestore(&irq_2_ir_lock, flags);
  150. return rc;
  151. }
  152. static struct intel_iommu *map_hpet_to_ir(u8 hpet_id)
  153. {
  154. int i;
  155. for (i = 0; i < MAX_HPET_TBS; i++)
  156. if (ir_hpet[i].id == hpet_id)
  157. return ir_hpet[i].iommu;
  158. return NULL;
  159. }
  160. static struct intel_iommu *map_ioapic_to_ir(int apic)
  161. {
  162. int i;
  163. for (i = 0; i < MAX_IO_APICS; i++)
  164. if (ir_ioapic[i].id == apic)
  165. return ir_ioapic[i].iommu;
  166. return NULL;
  167. }
  168. static struct intel_iommu *map_dev_to_ir(struct pci_dev *dev)
  169. {
  170. struct dmar_drhd_unit *drhd;
  171. drhd = dmar_find_matched_drhd_unit(dev);
  172. if (!drhd)
  173. return NULL;
  174. return drhd->iommu;
  175. }
  176. static int clear_entries(struct irq_2_iommu *irq_iommu)
  177. {
  178. struct irte *start, *entry, *end;
  179. struct intel_iommu *iommu;
  180. int index;
  181. if (irq_iommu->sub_handle)
  182. return 0;
  183. iommu = irq_iommu->iommu;
  184. index = irq_iommu->irte_index + irq_iommu->sub_handle;
  185. start = iommu->ir_table->base + index;
  186. end = start + (1 << irq_iommu->irte_mask);
  187. for (entry = start; entry < end; entry++) {
  188. set_64bit(&entry->low, 0);
  189. set_64bit(&entry->high, 0);
  190. }
  191. bitmap_release_region(iommu->ir_table->bitmap, index,
  192. irq_iommu->irte_mask);
  193. return qi_flush_iec(iommu, index, irq_iommu->irte_mask);
  194. }
  195. static int free_irte(int irq)
  196. {
  197. struct irq_2_iommu *irq_iommu = irq_2_iommu(irq);
  198. unsigned long flags;
  199. int rc;
  200. if (!irq_iommu)
  201. return -1;
  202. raw_spin_lock_irqsave(&irq_2_ir_lock, flags);
  203. rc = clear_entries(irq_iommu);
  204. irq_iommu->iommu = NULL;
  205. irq_iommu->irte_index = 0;
  206. irq_iommu->sub_handle = 0;
  207. irq_iommu->irte_mask = 0;
  208. raw_spin_unlock_irqrestore(&irq_2_ir_lock, flags);
  209. return rc;
  210. }
  211. /*
  212. * source validation type
  213. */
  214. #define SVT_NO_VERIFY 0x0 /* no verification is required */
  215. #define SVT_VERIFY_SID_SQ 0x1 /* verify using SID and SQ fields */
  216. #define SVT_VERIFY_BUS 0x2 /* verify bus of request-id */
  217. /*
  218. * source-id qualifier
  219. */
  220. #define SQ_ALL_16 0x0 /* verify all 16 bits of request-id */
  221. #define SQ_13_IGNORE_1 0x1 /* verify most significant 13 bits, ignore
  222. * the third least significant bit
  223. */
  224. #define SQ_13_IGNORE_2 0x2 /* verify most significant 13 bits, ignore
  225. * the second and third least significant bits
  226. */
  227. #define SQ_13_IGNORE_3 0x3 /* verify most significant 13 bits, ignore
  228. * the least three significant bits
  229. */
  230. /*
  231. * set SVT, SQ and SID fields of irte to verify
  232. * source ids of interrupt requests
  233. */
  234. static void set_irte_sid(struct irte *irte, unsigned int svt,
  235. unsigned int sq, unsigned int sid)
  236. {
  237. if (disable_sourceid_checking)
  238. svt = SVT_NO_VERIFY;
  239. irte->svt = svt;
  240. irte->sq = sq;
  241. irte->sid = sid;
  242. }
  243. static int set_ioapic_sid(struct irte *irte, int apic)
  244. {
  245. int i;
  246. u16 sid = 0;
  247. if (!irte)
  248. return -1;
  249. for (i = 0; i < MAX_IO_APICS; i++) {
  250. if (ir_ioapic[i].id == apic) {
  251. sid = (ir_ioapic[i].bus << 8) | ir_ioapic[i].devfn;
  252. break;
  253. }
  254. }
  255. if (sid == 0) {
  256. pr_warning("Failed to set source-id of IOAPIC (%d)\n", apic);
  257. return -1;
  258. }
  259. set_irte_sid(irte, SVT_VERIFY_SID_SQ, SQ_ALL_16, sid);
  260. return 0;
  261. }
  262. static int set_hpet_sid(struct irte *irte, u8 id)
  263. {
  264. int i;
  265. u16 sid = 0;
  266. if (!irte)
  267. return -1;
  268. for (i = 0; i < MAX_HPET_TBS; i++) {
  269. if (ir_hpet[i].id == id) {
  270. sid = (ir_hpet[i].bus << 8) | ir_hpet[i].devfn;
  271. break;
  272. }
  273. }
  274. if (sid == 0) {
  275. pr_warning("Failed to set source-id of HPET block (%d)\n", id);
  276. return -1;
  277. }
  278. /*
  279. * Should really use SQ_ALL_16. Some platforms are broken.
  280. * While we figure out the right quirks for these broken platforms, use
  281. * SQ_13_IGNORE_3 for now.
  282. */
  283. set_irte_sid(irte, SVT_VERIFY_SID_SQ, SQ_13_IGNORE_3, sid);
  284. return 0;
  285. }
  286. static int set_msi_sid(struct irte *irte, struct pci_dev *dev)
  287. {
  288. struct pci_dev *bridge;
  289. if (!irte || !dev)
  290. return -1;
  291. /* PCIe device or Root Complex integrated PCI device */
  292. if (pci_is_pcie(dev) || !dev->bus->parent) {
  293. set_irte_sid(irte, SVT_VERIFY_SID_SQ, SQ_ALL_16,
  294. (dev->bus->number << 8) | dev->devfn);
  295. return 0;
  296. }
  297. bridge = pci_find_upstream_pcie_bridge(dev);
  298. if (bridge) {
  299. if (pci_is_pcie(bridge))/* this is a PCIe-to-PCI/PCIX bridge */
  300. set_irte_sid(irte, SVT_VERIFY_BUS, SQ_ALL_16,
  301. (bridge->bus->number << 8) | dev->bus->number);
  302. else /* this is a legacy PCI bridge */
  303. set_irte_sid(irte, SVT_VERIFY_SID_SQ, SQ_ALL_16,
  304. (bridge->bus->number << 8) | bridge->devfn);
  305. }
  306. return 0;
  307. }
  308. static void iommu_set_irq_remapping(struct intel_iommu *iommu, int mode)
  309. {
  310. u64 addr;
  311. u32 sts;
  312. unsigned long flags;
  313. addr = virt_to_phys((void *)iommu->ir_table->base);
  314. raw_spin_lock_irqsave(&iommu->register_lock, flags);
  315. dmar_writeq(iommu->reg + DMAR_IRTA_REG,
  316. (addr) | IR_X2APIC_MODE(mode) | INTR_REMAP_TABLE_REG_SIZE);
  317. /* Set interrupt-remapping table pointer */
  318. iommu->gcmd |= DMA_GCMD_SIRTP;
  319. writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
  320. IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
  321. readl, (sts & DMA_GSTS_IRTPS), sts);
  322. raw_spin_unlock_irqrestore(&iommu->register_lock, flags);
  323. /*
  324. * global invalidation of interrupt entry cache before enabling
  325. * interrupt-remapping.
  326. */
  327. qi_global_iec(iommu);
  328. raw_spin_lock_irqsave(&iommu->register_lock, flags);
  329. /* Enable interrupt-remapping */
  330. iommu->gcmd |= DMA_GCMD_IRE;
  331. iommu->gcmd &= ~DMA_GCMD_CFI; /* Block compatibility-format MSIs */
  332. writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
  333. IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
  334. readl, (sts & DMA_GSTS_IRES), sts);
  335. /*
  336. * With CFI clear in the Global Command register, we should be
  337. * protected from dangerous (i.e. compatibility) interrupts
  338. * regardless of x2apic status. Check just to be sure.
  339. */
  340. if (sts & DMA_GSTS_CFIS)
  341. WARN(1, KERN_WARNING
  342. "Compatibility-format IRQs enabled despite intr remapping;\n"
  343. "you are vulnerable to IRQ injection.\n");
  344. raw_spin_unlock_irqrestore(&iommu->register_lock, flags);
  345. }
  346. static int intel_setup_irq_remapping(struct intel_iommu *iommu, int mode)
  347. {
  348. struct ir_table *ir_table;
  349. struct page *pages;
  350. unsigned long *bitmap;
  351. ir_table = iommu->ir_table = kzalloc(sizeof(struct ir_table),
  352. GFP_ATOMIC);
  353. if (!iommu->ir_table)
  354. return -ENOMEM;
  355. pages = alloc_pages_node(iommu->node, GFP_ATOMIC | __GFP_ZERO,
  356. INTR_REMAP_PAGE_ORDER);
  357. if (!pages) {
  358. pr_err("IR%d: failed to allocate pages of order %d\n",
  359. iommu->seq_id, INTR_REMAP_PAGE_ORDER);
  360. kfree(iommu->ir_table);
  361. return -ENOMEM;
  362. }
  363. bitmap = kcalloc(BITS_TO_LONGS(INTR_REMAP_TABLE_ENTRIES),
  364. sizeof(long), GFP_ATOMIC);
  365. if (bitmap == NULL) {
  366. pr_err("IR%d: failed to allocate bitmap\n", iommu->seq_id);
  367. __free_pages(pages, INTR_REMAP_PAGE_ORDER);
  368. kfree(ir_table);
  369. return -ENOMEM;
  370. }
  371. ir_table->base = page_address(pages);
  372. ir_table->bitmap = bitmap;
  373. iommu_set_irq_remapping(iommu, mode);
  374. return 0;
  375. }
  376. /*
  377. * Disable Interrupt Remapping.
  378. */
  379. static void iommu_disable_irq_remapping(struct intel_iommu *iommu)
  380. {
  381. unsigned long flags;
  382. u32 sts;
  383. if (!ecap_ir_support(iommu->ecap))
  384. return;
  385. /*
  386. * global invalidation of interrupt entry cache before disabling
  387. * interrupt-remapping.
  388. */
  389. qi_global_iec(iommu);
  390. raw_spin_lock_irqsave(&iommu->register_lock, flags);
  391. sts = dmar_readq(iommu->reg + DMAR_GSTS_REG);
  392. if (!(sts & DMA_GSTS_IRES))
  393. goto end;
  394. iommu->gcmd &= ~DMA_GCMD_IRE;
  395. writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
  396. IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
  397. readl, !(sts & DMA_GSTS_IRES), sts);
  398. end:
  399. raw_spin_unlock_irqrestore(&iommu->register_lock, flags);
  400. }
  401. static int __init dmar_x2apic_optout(void)
  402. {
  403. struct acpi_table_dmar *dmar;
  404. dmar = (struct acpi_table_dmar *)dmar_tbl;
  405. if (!dmar || no_x2apic_optout)
  406. return 0;
  407. return dmar->flags & DMAR_X2APIC_OPT_OUT;
  408. }
  409. static int __init intel_irq_remapping_supported(void)
  410. {
  411. struct dmar_drhd_unit *drhd;
  412. struct intel_iommu *iommu;
  413. if (disable_irq_remap)
  414. return 0;
  415. if (irq_remap_broken) {
  416. printk(KERN_WARNING
  417. "This system BIOS has enabled interrupt remapping\n"
  418. "on a chipset that contains an erratum making that\n"
  419. "feature unstable. To maintain system stability\n"
  420. "interrupt remapping is being disabled. Please\n"
  421. "contact your BIOS vendor for an update\n");
  422. add_taint(TAINT_FIRMWARE_WORKAROUND, LOCKDEP_STILL_OK);
  423. disable_irq_remap = 1;
  424. return 0;
  425. }
  426. if (!dmar_ir_support())
  427. return 0;
  428. for_each_iommu(iommu, drhd)
  429. if (!ecap_ir_support(iommu->ecap))
  430. return 0;
  431. return 1;
  432. }
  433. static int __init intel_enable_irq_remapping(void)
  434. {
  435. struct dmar_drhd_unit *drhd;
  436. struct intel_iommu *iommu;
  437. bool x2apic_present;
  438. int setup = 0;
  439. int eim = 0;
  440. x2apic_present = x2apic_supported();
  441. if (parse_ioapics_under_ir() != 1) {
  442. printk(KERN_INFO "Not enable interrupt remapping\n");
  443. goto error;
  444. }
  445. if (x2apic_present) {
  446. pr_info("Queued invalidation will be enabled to support x2apic and Intr-remapping.\n");
  447. eim = !dmar_x2apic_optout();
  448. if (!eim)
  449. printk(KERN_WARNING
  450. "Your BIOS is broken and requested that x2apic be disabled.\n"
  451. "This will slightly decrease performance.\n"
  452. "Use 'intremap=no_x2apic_optout' to override BIOS request.\n");
  453. }
  454. for_each_iommu(iommu, drhd) {
  455. /*
  456. * If the queued invalidation is already initialized,
  457. * shouldn't disable it.
  458. */
  459. if (iommu->qi)
  460. continue;
  461. /*
  462. * Clear previous faults.
  463. */
  464. dmar_fault(-1, iommu);
  465. /*
  466. * Disable intr remapping and queued invalidation, if already
  467. * enabled prior to OS handover.
  468. */
  469. iommu_disable_irq_remapping(iommu);
  470. dmar_disable_qi(iommu);
  471. }
  472. /*
  473. * check for the Interrupt-remapping support
  474. */
  475. for_each_iommu(iommu, drhd) {
  476. if (!ecap_ir_support(iommu->ecap))
  477. continue;
  478. if (eim && !ecap_eim_support(iommu->ecap)) {
  479. printk(KERN_INFO "DRHD %Lx: EIM not supported by DRHD, "
  480. " ecap %Lx\n", drhd->reg_base_addr, iommu->ecap);
  481. goto error;
  482. }
  483. }
  484. /*
  485. * Enable queued invalidation for all the DRHD's.
  486. */
  487. for_each_iommu(iommu, drhd) {
  488. int ret = dmar_enable_qi(iommu);
  489. if (ret) {
  490. printk(KERN_ERR "DRHD %Lx: failed to enable queued, "
  491. " invalidation, ecap %Lx, ret %d\n",
  492. drhd->reg_base_addr, iommu->ecap, ret);
  493. goto error;
  494. }
  495. }
  496. /*
  497. * Setup Interrupt-remapping for all the DRHD's now.
  498. */
  499. for_each_iommu(iommu, drhd) {
  500. if (!ecap_ir_support(iommu->ecap))
  501. continue;
  502. if (intel_setup_irq_remapping(iommu, eim))
  503. goto error;
  504. setup = 1;
  505. }
  506. if (!setup)
  507. goto error;
  508. irq_remapping_enabled = 1;
  509. /*
  510. * VT-d has a different layout for IO-APIC entries when
  511. * interrupt remapping is enabled. So it needs a special routine
  512. * to print IO-APIC entries for debugging purposes too.
  513. */
  514. x86_io_apic_ops.print_entries = intel_ir_io_apic_print_entries;
  515. pr_info("Enabled IRQ remapping in %s mode\n", eim ? "x2apic" : "xapic");
  516. return eim ? IRQ_REMAP_X2APIC_MODE : IRQ_REMAP_XAPIC_MODE;
  517. error:
  518. /*
  519. * handle error condition gracefully here!
  520. */
  521. if (x2apic_present)
  522. pr_warn("Failed to enable irq remapping. You are vulnerable to irq-injection attacks.\n");
  523. return -1;
  524. }
  525. static void ir_parse_one_hpet_scope(struct acpi_dmar_device_scope *scope,
  526. struct intel_iommu *iommu)
  527. {
  528. struct acpi_dmar_pci_path *path;
  529. u8 bus;
  530. int count;
  531. bus = scope->bus;
  532. path = (struct acpi_dmar_pci_path *)(scope + 1);
  533. count = (scope->length - sizeof(struct acpi_dmar_device_scope))
  534. / sizeof(struct acpi_dmar_pci_path);
  535. while (--count > 0) {
  536. /*
  537. * Access PCI directly due to the PCI
  538. * subsystem isn't initialized yet.
  539. */
  540. bus = read_pci_config_byte(bus, path->device, path->function,
  541. PCI_SECONDARY_BUS);
  542. path++;
  543. }
  544. ir_hpet[ir_hpet_num].bus = bus;
  545. ir_hpet[ir_hpet_num].devfn = PCI_DEVFN(path->device, path->function);
  546. ir_hpet[ir_hpet_num].iommu = iommu;
  547. ir_hpet[ir_hpet_num].id = scope->enumeration_id;
  548. ir_hpet_num++;
  549. }
  550. static void ir_parse_one_ioapic_scope(struct acpi_dmar_device_scope *scope,
  551. struct intel_iommu *iommu)
  552. {
  553. struct acpi_dmar_pci_path *path;
  554. u8 bus;
  555. int count;
  556. bus = scope->bus;
  557. path = (struct acpi_dmar_pci_path *)(scope + 1);
  558. count = (scope->length - sizeof(struct acpi_dmar_device_scope))
  559. / sizeof(struct acpi_dmar_pci_path);
  560. while (--count > 0) {
  561. /*
  562. * Access PCI directly due to the PCI
  563. * subsystem isn't initialized yet.
  564. */
  565. bus = read_pci_config_byte(bus, path->device, path->function,
  566. PCI_SECONDARY_BUS);
  567. path++;
  568. }
  569. ir_ioapic[ir_ioapic_num].bus = bus;
  570. ir_ioapic[ir_ioapic_num].devfn = PCI_DEVFN(path->device, path->function);
  571. ir_ioapic[ir_ioapic_num].iommu = iommu;
  572. ir_ioapic[ir_ioapic_num].id = scope->enumeration_id;
  573. ir_ioapic_num++;
  574. }
  575. static int ir_parse_ioapic_hpet_scope(struct acpi_dmar_header *header,
  576. struct intel_iommu *iommu)
  577. {
  578. struct acpi_dmar_hardware_unit *drhd;
  579. struct acpi_dmar_device_scope *scope;
  580. void *start, *end;
  581. drhd = (struct acpi_dmar_hardware_unit *)header;
  582. start = (void *)(drhd + 1);
  583. end = ((void *)drhd) + header->length;
  584. while (start < end) {
  585. scope = start;
  586. if (scope->entry_type == ACPI_DMAR_SCOPE_TYPE_IOAPIC) {
  587. if (ir_ioapic_num == MAX_IO_APICS) {
  588. printk(KERN_WARNING "Exceeded Max IO APICS\n");
  589. return -1;
  590. }
  591. printk(KERN_INFO "IOAPIC id %d under DRHD base "
  592. " 0x%Lx IOMMU %d\n", scope->enumeration_id,
  593. drhd->address, iommu->seq_id);
  594. ir_parse_one_ioapic_scope(scope, iommu);
  595. } else if (scope->entry_type == ACPI_DMAR_SCOPE_TYPE_HPET) {
  596. if (ir_hpet_num == MAX_HPET_TBS) {
  597. printk(KERN_WARNING "Exceeded Max HPET blocks\n");
  598. return -1;
  599. }
  600. printk(KERN_INFO "HPET id %d under DRHD base"
  601. " 0x%Lx\n", scope->enumeration_id,
  602. drhd->address);
  603. ir_parse_one_hpet_scope(scope, iommu);
  604. }
  605. start += scope->length;
  606. }
  607. return 0;
  608. }
  609. /*
  610. * Finds the assocaition between IOAPIC's and its Interrupt-remapping
  611. * hardware unit.
  612. */
  613. static int __init parse_ioapics_under_ir(void)
  614. {
  615. struct dmar_drhd_unit *drhd;
  616. struct intel_iommu *iommu;
  617. int ir_supported = 0;
  618. int ioapic_idx;
  619. for_each_iommu(iommu, drhd)
  620. if (ecap_ir_support(iommu->ecap)) {
  621. if (ir_parse_ioapic_hpet_scope(drhd->hdr, iommu))
  622. return -1;
  623. ir_supported = 1;
  624. }
  625. if (!ir_supported)
  626. return 0;
  627. for (ioapic_idx = 0; ioapic_idx < nr_ioapics; ioapic_idx++) {
  628. int ioapic_id = mpc_ioapic_id(ioapic_idx);
  629. if (!map_ioapic_to_ir(ioapic_id)) {
  630. pr_err(FW_BUG "ioapic %d has no mapping iommu, "
  631. "interrupt remapping will be disabled\n",
  632. ioapic_id);
  633. return -1;
  634. }
  635. }
  636. return 1;
  637. }
  638. static int __init ir_dev_scope_init(void)
  639. {
  640. if (!irq_remapping_enabled)
  641. return 0;
  642. return dmar_dev_scope_init();
  643. }
  644. rootfs_initcall(ir_dev_scope_init);
  645. static void disable_irq_remapping(void)
  646. {
  647. struct dmar_drhd_unit *drhd;
  648. struct intel_iommu *iommu = NULL;
  649. /*
  650. * Disable Interrupt-remapping for all the DRHD's now.
  651. */
  652. for_each_iommu(iommu, drhd) {
  653. if (!ecap_ir_support(iommu->ecap))
  654. continue;
  655. iommu_disable_irq_remapping(iommu);
  656. }
  657. }
  658. static int reenable_irq_remapping(int eim)
  659. {
  660. struct dmar_drhd_unit *drhd;
  661. int setup = 0;
  662. struct intel_iommu *iommu = NULL;
  663. for_each_iommu(iommu, drhd)
  664. if (iommu->qi)
  665. dmar_reenable_qi(iommu);
  666. /*
  667. * Setup Interrupt-remapping for all the DRHD's now.
  668. */
  669. for_each_iommu(iommu, drhd) {
  670. if (!ecap_ir_support(iommu->ecap))
  671. continue;
  672. /* Set up interrupt remapping for iommu.*/
  673. iommu_set_irq_remapping(iommu, eim);
  674. setup = 1;
  675. }
  676. if (!setup)
  677. goto error;
  678. return 0;
  679. error:
  680. /*
  681. * handle error condition gracefully here!
  682. */
  683. return -1;
  684. }
  685. static void prepare_irte(struct irte *irte, int vector,
  686. unsigned int dest)
  687. {
  688. memset(irte, 0, sizeof(*irte));
  689. irte->present = 1;
  690. irte->dst_mode = apic->irq_dest_mode;
  691. /*
  692. * Trigger mode in the IRTE will always be edge, and for IO-APIC, the
  693. * actual level or edge trigger will be setup in the IO-APIC
  694. * RTE. This will help simplify level triggered irq migration.
  695. * For more details, see the comments (in io_apic.c) explainig IO-APIC
  696. * irq migration in the presence of interrupt-remapping.
  697. */
  698. irte->trigger_mode = 0;
  699. irte->dlvry_mode = apic->irq_delivery_mode;
  700. irte->vector = vector;
  701. irte->dest_id = IRTE_DEST(dest);
  702. irte->redir_hint = 1;
  703. }
  704. static int intel_setup_ioapic_entry(int irq,
  705. struct IO_APIC_route_entry *route_entry,
  706. unsigned int destination, int vector,
  707. struct io_apic_irq_attr *attr)
  708. {
  709. int ioapic_id = mpc_ioapic_id(attr->ioapic);
  710. struct intel_iommu *iommu = map_ioapic_to_ir(ioapic_id);
  711. struct IR_IO_APIC_route_entry *entry;
  712. struct irte irte;
  713. int index;
  714. if (!iommu) {
  715. pr_warn("No mapping iommu for ioapic %d\n", ioapic_id);
  716. return -ENODEV;
  717. }
  718. entry = (struct IR_IO_APIC_route_entry *)route_entry;
  719. index = alloc_irte(iommu, irq, 1);
  720. if (index < 0) {
  721. pr_warn("Failed to allocate IRTE for ioapic %d\n", ioapic_id);
  722. return -ENOMEM;
  723. }
  724. prepare_irte(&irte, vector, destination);
  725. /* Set source-id of interrupt request */
  726. set_ioapic_sid(&irte, ioapic_id);
  727. modify_irte(irq, &irte);
  728. apic_printk(APIC_VERBOSE, KERN_DEBUG "IOAPIC[%d]: "
  729. "Set IRTE entry (P:%d FPD:%d Dst_Mode:%d "
  730. "Redir_hint:%d Trig_Mode:%d Dlvry_Mode:%X "
  731. "Avail:%X Vector:%02X Dest:%08X "
  732. "SID:%04X SQ:%X SVT:%X)\n",
  733. attr->ioapic, irte.present, irte.fpd, irte.dst_mode,
  734. irte.redir_hint, irte.trigger_mode, irte.dlvry_mode,
  735. irte.avail, irte.vector, irte.dest_id,
  736. irte.sid, irte.sq, irte.svt);
  737. memset(entry, 0, sizeof(*entry));
  738. entry->index2 = (index >> 15) & 0x1;
  739. entry->zero = 0;
  740. entry->format = 1;
  741. entry->index = (index & 0x7fff);
  742. /*
  743. * IO-APIC RTE will be configured with virtual vector.
  744. * irq handler will do the explicit EOI to the io-apic.
  745. */
  746. entry->vector = attr->ioapic_pin;
  747. entry->mask = 0; /* enable IRQ */
  748. entry->trigger = attr->trigger;
  749. entry->polarity = attr->polarity;
  750. /* Mask level triggered irqs.
  751. * Use IRQ_DELAYED_DISABLE for edge triggered irqs.
  752. */
  753. if (attr->trigger)
  754. entry->mask = 1;
  755. return 0;
  756. }
  757. /*
  758. * Migrate the IO-APIC irq in the presence of intr-remapping.
  759. *
  760. * For both level and edge triggered, irq migration is a simple atomic
  761. * update(of vector and cpu destination) of IRTE and flush the hardware cache.
  762. *
  763. * For level triggered, we eliminate the io-apic RTE modification (with the
  764. * updated vector information), by using a virtual vector (io-apic pin number).
  765. * Real vector that is used for interrupting cpu will be coming from
  766. * the interrupt-remapping table entry.
  767. *
  768. * As the migration is a simple atomic update of IRTE, the same mechanism
  769. * is used to migrate MSI irq's in the presence of interrupt-remapping.
  770. */
  771. static int
  772. intel_ioapic_set_affinity(struct irq_data *data, const struct cpumask *mask,
  773. bool force)
  774. {
  775. struct irq_cfg *cfg = data->chip_data;
  776. unsigned int dest, irq = data->irq;
  777. struct irte irte;
  778. int err;
  779. if (!config_enabled(CONFIG_SMP))
  780. return -EINVAL;
  781. if (!cpumask_intersects(mask, cpu_online_mask))
  782. return -EINVAL;
  783. if (get_irte(irq, &irte))
  784. return -EBUSY;
  785. err = assign_irq_vector(irq, cfg, mask);
  786. if (err)
  787. return err;
  788. err = apic->cpu_mask_to_apicid_and(cfg->domain, mask, &dest);
  789. if (err) {
  790. if (assign_irq_vector(irq, cfg, data->affinity))
  791. pr_err("Failed to recover vector for irq %d\n", irq);
  792. return err;
  793. }
  794. irte.vector = cfg->vector;
  795. irte.dest_id = IRTE_DEST(dest);
  796. /*
  797. * Atomically updates the IRTE with the new destination, vector
  798. * and flushes the interrupt entry cache.
  799. */
  800. modify_irte(irq, &irte);
  801. /*
  802. * After this point, all the interrupts will start arriving
  803. * at the new destination. So, time to cleanup the previous
  804. * vector allocation.
  805. */
  806. if (cfg->move_in_progress)
  807. send_cleanup_vector(cfg);
  808. cpumask_copy(data->affinity, mask);
  809. return 0;
  810. }
  811. static void intel_compose_msi_msg(struct pci_dev *pdev,
  812. unsigned int irq, unsigned int dest,
  813. struct msi_msg *msg, u8 hpet_id)
  814. {
  815. struct irq_cfg *cfg;
  816. struct irte irte;
  817. u16 sub_handle = 0;
  818. int ir_index;
  819. cfg = irq_get_chip_data(irq);
  820. ir_index = map_irq_to_irte_handle(irq, &sub_handle);
  821. BUG_ON(ir_index == -1);
  822. prepare_irte(&irte, cfg->vector, dest);
  823. /* Set source-id of interrupt request */
  824. if (pdev)
  825. set_msi_sid(&irte, pdev);
  826. else
  827. set_hpet_sid(&irte, hpet_id);
  828. modify_irte(irq, &irte);
  829. msg->address_hi = MSI_ADDR_BASE_HI;
  830. msg->data = sub_handle;
  831. msg->address_lo = MSI_ADDR_BASE_LO | MSI_ADDR_IR_EXT_INT |
  832. MSI_ADDR_IR_SHV |
  833. MSI_ADDR_IR_INDEX1(ir_index) |
  834. MSI_ADDR_IR_INDEX2(ir_index);
  835. }
  836. /*
  837. * Map the PCI dev to the corresponding remapping hardware unit
  838. * and allocate 'nvec' consecutive interrupt-remapping table entries
  839. * in it.
  840. */
  841. static int intel_msi_alloc_irq(struct pci_dev *dev, int irq, int nvec)
  842. {
  843. struct intel_iommu *iommu;
  844. int index;
  845. iommu = map_dev_to_ir(dev);
  846. if (!iommu) {
  847. printk(KERN_ERR
  848. "Unable to map PCI %s to iommu\n", pci_name(dev));
  849. return -ENOENT;
  850. }
  851. index = alloc_irte(iommu, irq, nvec);
  852. if (index < 0) {
  853. printk(KERN_ERR
  854. "Unable to allocate %d IRTE for PCI %s\n", nvec,
  855. pci_name(dev));
  856. return -ENOSPC;
  857. }
  858. return index;
  859. }
  860. static int intel_msi_setup_irq(struct pci_dev *pdev, unsigned int irq,
  861. int index, int sub_handle)
  862. {
  863. struct intel_iommu *iommu;
  864. iommu = map_dev_to_ir(pdev);
  865. if (!iommu)
  866. return -ENOENT;
  867. /*
  868. * setup the mapping between the irq and the IRTE
  869. * base index, the sub_handle pointing to the
  870. * appropriate interrupt remap table entry.
  871. */
  872. set_irte_irq(irq, iommu, index, sub_handle);
  873. return 0;
  874. }
  875. static int intel_setup_hpet_msi(unsigned int irq, unsigned int id)
  876. {
  877. struct intel_iommu *iommu = map_hpet_to_ir(id);
  878. int index;
  879. if (!iommu)
  880. return -1;
  881. index = alloc_irte(iommu, irq, 1);
  882. if (index < 0)
  883. return -1;
  884. return 0;
  885. }
  886. struct irq_remap_ops intel_irq_remap_ops = {
  887. .supported = intel_irq_remapping_supported,
  888. .prepare = dmar_table_init,
  889. .enable = intel_enable_irq_remapping,
  890. .disable = disable_irq_remapping,
  891. .reenable = reenable_irq_remapping,
  892. .enable_faulting = enable_drhd_fault_handling,
  893. .setup_ioapic_entry = intel_setup_ioapic_entry,
  894. .set_affinity = intel_ioapic_set_affinity,
  895. .free_irq = free_irte,
  896. .compose_msi_msg = intel_compose_msi_msg,
  897. .msi_alloc_irq = intel_msi_alloc_irq,
  898. .msi_setup_irq = intel_msi_setup_irq,
  899. .setup_hpet_msi = intel_setup_hpet_msi,
  900. };