intel_irq_remapping.c 26 KB

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