intel_irq_remapping.c 26 KB

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