intel_irq_remapping.c 27 KB

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