intel_irq_remapping.c 27 KB

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