intel_irq_remapping.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309
  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) ((eim_mode) ? dest : dest << 8)
  32. static int __read_mostly eim_mode;
  33. static struct ioapic_scope ir_ioapic[MAX_IO_APICS];
  34. static struct hpet_scope ir_hpet[MAX_HPET_TBS];
  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_cfg(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_cfg(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_cfg(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 && ir_hpet[i].iommu)
  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 && ir_ioapic[i].iommu)
  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].iommu && 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].iommu && 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)
  380. {
  381. struct ir_table *ir_table;
  382. struct page *pages;
  383. unsigned long *bitmap;
  384. if (iommu->ir_table)
  385. return 0;
  386. ir_table = kzalloc(sizeof(struct ir_table), GFP_KERNEL);
  387. if (!ir_table)
  388. return -ENOMEM;
  389. pages = alloc_pages_node(iommu->node, GFP_KERNEL | __GFP_ZERO,
  390. INTR_REMAP_PAGE_ORDER);
  391. if (!pages) {
  392. pr_err("IR%d: failed to allocate pages of order %d\n",
  393. iommu->seq_id, INTR_REMAP_PAGE_ORDER);
  394. goto out_free_table;
  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. goto out_free_pages;
  401. }
  402. ir_table->base = page_address(pages);
  403. ir_table->bitmap = bitmap;
  404. iommu->ir_table = ir_table;
  405. return 0;
  406. out_free_pages:
  407. __free_pages(pages, INTR_REMAP_PAGE_ORDER);
  408. out_free_table:
  409. kfree(ir_table);
  410. return -ENOMEM;
  411. }
  412. static void intel_teardown_irq_remapping(struct intel_iommu *iommu)
  413. {
  414. if (iommu && iommu->ir_table) {
  415. free_pages((unsigned long)iommu->ir_table->base,
  416. INTR_REMAP_PAGE_ORDER);
  417. kfree(iommu->ir_table->bitmap);
  418. kfree(iommu->ir_table);
  419. iommu->ir_table = NULL;
  420. }
  421. }
  422. /*
  423. * Disable Interrupt Remapping.
  424. */
  425. static void iommu_disable_irq_remapping(struct intel_iommu *iommu)
  426. {
  427. unsigned long flags;
  428. u32 sts;
  429. if (!ecap_ir_support(iommu->ecap))
  430. return;
  431. /*
  432. * global invalidation of interrupt entry cache before disabling
  433. * interrupt-remapping.
  434. */
  435. qi_global_iec(iommu);
  436. raw_spin_lock_irqsave(&iommu->register_lock, flags);
  437. sts = dmar_readq(iommu->reg + DMAR_GSTS_REG);
  438. if (!(sts & DMA_GSTS_IRES))
  439. goto end;
  440. iommu->gcmd &= ~DMA_GCMD_IRE;
  441. writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
  442. IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
  443. readl, !(sts & DMA_GSTS_IRES), sts);
  444. end:
  445. raw_spin_unlock_irqrestore(&iommu->register_lock, flags);
  446. }
  447. static int __init dmar_x2apic_optout(void)
  448. {
  449. struct acpi_table_dmar *dmar;
  450. dmar = (struct acpi_table_dmar *)dmar_tbl;
  451. if (!dmar || no_x2apic_optout)
  452. return 0;
  453. return dmar->flags & DMAR_X2APIC_OPT_OUT;
  454. }
  455. static void __init intel_cleanup_irq_remapping(void)
  456. {
  457. struct dmar_drhd_unit *drhd;
  458. struct intel_iommu *iommu;
  459. for_each_iommu(iommu, drhd) {
  460. if (ecap_ir_support(iommu->ecap)) {
  461. iommu_disable_irq_remapping(iommu);
  462. intel_teardown_irq_remapping(iommu);
  463. }
  464. }
  465. if (x2apic_supported())
  466. pr_warn("Failed to enable irq remapping. You are vulnerable to irq-injection attacks.\n");
  467. }
  468. static int __init intel_prepare_irq_remapping(void)
  469. {
  470. struct dmar_drhd_unit *drhd;
  471. struct intel_iommu *iommu;
  472. /* First check whether IRQ remapping should be enabled */
  473. if (disable_irq_remap)
  474. return -ENODEV;
  475. if (irq_remap_broken) {
  476. printk(KERN_WARNING
  477. "This system BIOS has enabled interrupt remapping\n"
  478. "on a chipset that contains an erratum making that\n"
  479. "feature unstable. To maintain system stability\n"
  480. "interrupt remapping is being disabled. Please\n"
  481. "contact your BIOS vendor for an update\n");
  482. add_taint(TAINT_FIRMWARE_WORKAROUND, LOCKDEP_STILL_OK);
  483. disable_irq_remap = 1;
  484. return -ENODEV;
  485. }
  486. if (dmar_table_init() < 0)
  487. return -ENODEV;
  488. if (!dmar_ir_support())
  489. return -ENODEV;
  490. if (parse_ioapics_under_ir() != 1) {
  491. printk(KERN_INFO "Not enabling interrupt remapping\n");
  492. goto error;
  493. }
  494. /* First make sure all IOMMUs support IRQ remapping */
  495. for_each_iommu(iommu, drhd)
  496. if (!ecap_ir_support(iommu->ecap))
  497. goto error;
  498. /* Do the allocations early */
  499. for_each_iommu(iommu, drhd)
  500. if (intel_setup_irq_remapping(iommu))
  501. goto error;
  502. return 0;
  503. error:
  504. intel_cleanup_irq_remapping();
  505. return -ENODEV;
  506. }
  507. static int __init intel_enable_irq_remapping(void)
  508. {
  509. struct dmar_drhd_unit *drhd;
  510. struct intel_iommu *iommu;
  511. int setup = 0;
  512. int eim = 0;
  513. if (x2apic_supported()) {
  514. eim = !dmar_x2apic_optout();
  515. if (!eim)
  516. printk(KERN_WARNING
  517. "Your BIOS is broken and requested that x2apic be disabled.\n"
  518. "This will slightly decrease performance.\n"
  519. "Use 'intremap=no_x2apic_optout' to override BIOS request.\n");
  520. }
  521. for_each_iommu(iommu, drhd) {
  522. /*
  523. * If the queued invalidation is already initialized,
  524. * shouldn't disable it.
  525. */
  526. if (iommu->qi)
  527. continue;
  528. /*
  529. * Clear previous faults.
  530. */
  531. dmar_fault(-1, iommu);
  532. /*
  533. * Disable intr remapping and queued invalidation, if already
  534. * enabled prior to OS handover.
  535. */
  536. iommu_disable_irq_remapping(iommu);
  537. dmar_disable_qi(iommu);
  538. }
  539. /*
  540. * check for the Interrupt-remapping support
  541. */
  542. for_each_iommu(iommu, drhd)
  543. if (eim && !ecap_eim_support(iommu->ecap)) {
  544. printk(KERN_INFO "DRHD %Lx: EIM not supported by DRHD, "
  545. " ecap %Lx\n", drhd->reg_base_addr, iommu->ecap);
  546. eim = 0;
  547. }
  548. eim_mode = eim;
  549. if (eim)
  550. pr_info("Queued invalidation will be enabled to support x2apic and Intr-remapping.\n");
  551. /*
  552. * Enable queued invalidation for all the DRHD's.
  553. */
  554. for_each_iommu(iommu, drhd) {
  555. int ret = dmar_enable_qi(iommu);
  556. if (ret) {
  557. printk(KERN_ERR "DRHD %Lx: failed to enable queued, "
  558. " invalidation, ecap %Lx, ret %d\n",
  559. drhd->reg_base_addr, iommu->ecap, ret);
  560. goto error;
  561. }
  562. }
  563. /*
  564. * Setup Interrupt-remapping for all the DRHD's now.
  565. */
  566. for_each_iommu(iommu, drhd) {
  567. iommu_set_irq_remapping(iommu, eim);
  568. setup = 1;
  569. }
  570. if (!setup)
  571. goto error;
  572. irq_remapping_enabled = 1;
  573. /*
  574. * VT-d has a different layout for IO-APIC entries when
  575. * interrupt remapping is enabled. So it needs a special routine
  576. * to print IO-APIC entries for debugging purposes too.
  577. */
  578. x86_io_apic_ops.print_entries = intel_ir_io_apic_print_entries;
  579. pr_info("Enabled IRQ remapping in %s mode\n", eim ? "x2apic" : "xapic");
  580. return eim ? IRQ_REMAP_X2APIC_MODE : IRQ_REMAP_XAPIC_MODE;
  581. error:
  582. intel_cleanup_irq_remapping();
  583. return -1;
  584. }
  585. static int ir_parse_one_hpet_scope(struct acpi_dmar_device_scope *scope,
  586. struct intel_iommu *iommu,
  587. struct acpi_dmar_hardware_unit *drhd)
  588. {
  589. struct acpi_dmar_pci_path *path;
  590. u8 bus;
  591. int count, free = -1;
  592. bus = scope->bus;
  593. path = (struct acpi_dmar_pci_path *)(scope + 1);
  594. count = (scope->length - sizeof(struct acpi_dmar_device_scope))
  595. / sizeof(struct acpi_dmar_pci_path);
  596. while (--count > 0) {
  597. /*
  598. * Access PCI directly due to the PCI
  599. * subsystem isn't initialized yet.
  600. */
  601. bus = read_pci_config_byte(bus, path->device, path->function,
  602. PCI_SECONDARY_BUS);
  603. path++;
  604. }
  605. for (count = 0; count < MAX_HPET_TBS; count++) {
  606. if (ir_hpet[count].iommu == iommu &&
  607. ir_hpet[count].id == scope->enumeration_id)
  608. return 0;
  609. else if (ir_hpet[count].iommu == NULL && free == -1)
  610. free = count;
  611. }
  612. if (free == -1) {
  613. pr_warn("Exceeded Max HPET blocks\n");
  614. return -ENOSPC;
  615. }
  616. ir_hpet[free].iommu = iommu;
  617. ir_hpet[free].id = scope->enumeration_id;
  618. ir_hpet[free].bus = bus;
  619. ir_hpet[free].devfn = PCI_DEVFN(path->device, path->function);
  620. pr_info("HPET id %d under DRHD base 0x%Lx\n",
  621. scope->enumeration_id, drhd->address);
  622. return 0;
  623. }
  624. static int ir_parse_one_ioapic_scope(struct acpi_dmar_device_scope *scope,
  625. struct intel_iommu *iommu,
  626. struct acpi_dmar_hardware_unit *drhd)
  627. {
  628. struct acpi_dmar_pci_path *path;
  629. u8 bus;
  630. int count, free = -1;
  631. bus = scope->bus;
  632. path = (struct acpi_dmar_pci_path *)(scope + 1);
  633. count = (scope->length - sizeof(struct acpi_dmar_device_scope))
  634. / sizeof(struct acpi_dmar_pci_path);
  635. while (--count > 0) {
  636. /*
  637. * Access PCI directly due to the PCI
  638. * subsystem isn't initialized yet.
  639. */
  640. bus = read_pci_config_byte(bus, path->device, path->function,
  641. PCI_SECONDARY_BUS);
  642. path++;
  643. }
  644. for (count = 0; count < MAX_IO_APICS; count++) {
  645. if (ir_ioapic[count].iommu == iommu &&
  646. ir_ioapic[count].id == scope->enumeration_id)
  647. return 0;
  648. else if (ir_ioapic[count].iommu == NULL && free == -1)
  649. free = count;
  650. }
  651. if (free == -1) {
  652. pr_warn("Exceeded Max IO APICS\n");
  653. return -ENOSPC;
  654. }
  655. ir_ioapic[free].bus = bus;
  656. ir_ioapic[free].devfn = PCI_DEVFN(path->device, path->function);
  657. ir_ioapic[free].iommu = iommu;
  658. ir_ioapic[free].id = scope->enumeration_id;
  659. pr_info("IOAPIC id %d under DRHD base 0x%Lx IOMMU %d\n",
  660. scope->enumeration_id, drhd->address, iommu->seq_id);
  661. return 0;
  662. }
  663. static int ir_parse_ioapic_hpet_scope(struct acpi_dmar_header *header,
  664. struct intel_iommu *iommu)
  665. {
  666. int ret = 0;
  667. struct acpi_dmar_hardware_unit *drhd;
  668. struct acpi_dmar_device_scope *scope;
  669. void *start, *end;
  670. drhd = (struct acpi_dmar_hardware_unit *)header;
  671. start = (void *)(drhd + 1);
  672. end = ((void *)drhd) + header->length;
  673. while (start < end && ret == 0) {
  674. scope = start;
  675. if (scope->entry_type == ACPI_DMAR_SCOPE_TYPE_IOAPIC)
  676. ret = ir_parse_one_ioapic_scope(scope, iommu, drhd);
  677. else if (scope->entry_type == ACPI_DMAR_SCOPE_TYPE_HPET)
  678. ret = ir_parse_one_hpet_scope(scope, iommu, drhd);
  679. start += scope->length;
  680. }
  681. return ret;
  682. }
  683. static void ir_remove_ioapic_hpet_scope(struct intel_iommu *iommu)
  684. {
  685. int i;
  686. for (i = 0; i < MAX_HPET_TBS; i++)
  687. if (ir_hpet[i].iommu == iommu)
  688. ir_hpet[i].iommu = NULL;
  689. for (i = 0; i < MAX_IO_APICS; i++)
  690. if (ir_ioapic[i].iommu == iommu)
  691. ir_ioapic[i].iommu = NULL;
  692. }
  693. /*
  694. * Finds the assocaition between IOAPIC's and its Interrupt-remapping
  695. * hardware unit.
  696. */
  697. static int __init parse_ioapics_under_ir(void)
  698. {
  699. struct dmar_drhd_unit *drhd;
  700. struct intel_iommu *iommu;
  701. int ir_supported = 0;
  702. int ioapic_idx;
  703. for_each_iommu(iommu, drhd)
  704. if (ecap_ir_support(iommu->ecap)) {
  705. if (ir_parse_ioapic_hpet_scope(drhd->hdr, iommu))
  706. return -1;
  707. ir_supported = 1;
  708. }
  709. if (!ir_supported)
  710. return 0;
  711. for (ioapic_idx = 0; ioapic_idx < nr_ioapics; ioapic_idx++) {
  712. int ioapic_id = mpc_ioapic_id(ioapic_idx);
  713. if (!map_ioapic_to_ir(ioapic_id)) {
  714. pr_err(FW_BUG "ioapic %d has no mapping iommu, "
  715. "interrupt remapping will be disabled\n",
  716. ioapic_id);
  717. return -1;
  718. }
  719. }
  720. return 1;
  721. }
  722. static int __init ir_dev_scope_init(void)
  723. {
  724. int ret;
  725. if (!irq_remapping_enabled)
  726. return 0;
  727. down_write(&dmar_global_lock);
  728. ret = dmar_dev_scope_init();
  729. up_write(&dmar_global_lock);
  730. return ret;
  731. }
  732. rootfs_initcall(ir_dev_scope_init);
  733. static void disable_irq_remapping(void)
  734. {
  735. struct dmar_drhd_unit *drhd;
  736. struct intel_iommu *iommu = NULL;
  737. /*
  738. * Disable Interrupt-remapping for all the DRHD's now.
  739. */
  740. for_each_iommu(iommu, drhd) {
  741. if (!ecap_ir_support(iommu->ecap))
  742. continue;
  743. iommu_disable_irq_remapping(iommu);
  744. }
  745. }
  746. static int reenable_irq_remapping(int eim)
  747. {
  748. struct dmar_drhd_unit *drhd;
  749. int setup = 0;
  750. struct intel_iommu *iommu = NULL;
  751. for_each_iommu(iommu, drhd)
  752. if (iommu->qi)
  753. dmar_reenable_qi(iommu);
  754. /*
  755. * Setup Interrupt-remapping for all the DRHD's now.
  756. */
  757. for_each_iommu(iommu, drhd) {
  758. if (!ecap_ir_support(iommu->ecap))
  759. continue;
  760. /* Set up interrupt remapping for iommu.*/
  761. iommu_set_irq_remapping(iommu, eim);
  762. setup = 1;
  763. }
  764. if (!setup)
  765. goto error;
  766. return 0;
  767. error:
  768. /*
  769. * handle error condition gracefully here!
  770. */
  771. return -1;
  772. }
  773. static void prepare_irte(struct irte *irte, int vector,
  774. unsigned int dest)
  775. {
  776. memset(irte, 0, sizeof(*irte));
  777. irte->present = 1;
  778. irte->dst_mode = apic->irq_dest_mode;
  779. /*
  780. * Trigger mode in the IRTE will always be edge, and for IO-APIC, the
  781. * actual level or edge trigger will be setup in the IO-APIC
  782. * RTE. This will help simplify level triggered irq migration.
  783. * For more details, see the comments (in io_apic.c) explainig IO-APIC
  784. * irq migration in the presence of interrupt-remapping.
  785. */
  786. irte->trigger_mode = 0;
  787. irte->dlvry_mode = apic->irq_delivery_mode;
  788. irte->vector = vector;
  789. irte->dest_id = IRTE_DEST(dest);
  790. irte->redir_hint = 1;
  791. }
  792. static int intel_setup_ioapic_entry(int irq,
  793. struct IO_APIC_route_entry *route_entry,
  794. unsigned int destination, int vector,
  795. struct io_apic_irq_attr *attr)
  796. {
  797. int ioapic_id = mpc_ioapic_id(attr->ioapic);
  798. struct intel_iommu *iommu;
  799. struct IR_IO_APIC_route_entry *entry;
  800. struct irte irte;
  801. int index;
  802. down_read(&dmar_global_lock);
  803. iommu = map_ioapic_to_ir(ioapic_id);
  804. if (!iommu) {
  805. pr_warn("No mapping iommu for ioapic %d\n", ioapic_id);
  806. index = -ENODEV;
  807. } else {
  808. index = alloc_irte(iommu, irq, 1);
  809. if (index < 0) {
  810. pr_warn("Failed to allocate IRTE for ioapic %d\n",
  811. ioapic_id);
  812. index = -ENOMEM;
  813. }
  814. }
  815. up_read(&dmar_global_lock);
  816. if (index < 0)
  817. return index;
  818. prepare_irte(&irte, vector, destination);
  819. /* Set source-id of interrupt request */
  820. set_ioapic_sid(&irte, ioapic_id);
  821. modify_irte(irq, &irte);
  822. apic_printk(APIC_VERBOSE, KERN_DEBUG "IOAPIC[%d]: "
  823. "Set IRTE entry (P:%d FPD:%d Dst_Mode:%d "
  824. "Redir_hint:%d Trig_Mode:%d Dlvry_Mode:%X "
  825. "Avail:%X Vector:%02X Dest:%08X "
  826. "SID:%04X SQ:%X SVT:%X)\n",
  827. attr->ioapic, irte.present, irte.fpd, irte.dst_mode,
  828. irte.redir_hint, irte.trigger_mode, irte.dlvry_mode,
  829. irte.avail, irte.vector, irte.dest_id,
  830. irte.sid, irte.sq, irte.svt);
  831. entry = (struct IR_IO_APIC_route_entry *)route_entry;
  832. memset(entry, 0, sizeof(*entry));
  833. entry->index2 = (index >> 15) & 0x1;
  834. entry->zero = 0;
  835. entry->format = 1;
  836. entry->index = (index & 0x7fff);
  837. /*
  838. * IO-APIC RTE will be configured with virtual vector.
  839. * irq handler will do the explicit EOI to the io-apic.
  840. */
  841. entry->vector = attr->ioapic_pin;
  842. entry->mask = 0; /* enable IRQ */
  843. entry->trigger = attr->trigger;
  844. entry->polarity = attr->polarity;
  845. /* Mask level triggered irqs.
  846. * Use IRQ_DELAYED_DISABLE for edge triggered irqs.
  847. */
  848. if (attr->trigger)
  849. entry->mask = 1;
  850. return 0;
  851. }
  852. /*
  853. * Migrate the IO-APIC irq in the presence of intr-remapping.
  854. *
  855. * For both level and edge triggered, irq migration is a simple atomic
  856. * update(of vector and cpu destination) of IRTE and flush the hardware cache.
  857. *
  858. * For level triggered, we eliminate the io-apic RTE modification (with the
  859. * updated vector information), by using a virtual vector (io-apic pin number).
  860. * Real vector that is used for interrupting cpu will be coming from
  861. * the interrupt-remapping table entry.
  862. *
  863. * As the migration is a simple atomic update of IRTE, the same mechanism
  864. * is used to migrate MSI irq's in the presence of interrupt-remapping.
  865. */
  866. static int
  867. intel_ioapic_set_affinity(struct irq_data *data, const struct cpumask *mask,
  868. bool force)
  869. {
  870. struct irq_cfg *cfg = irqd_cfg(data);
  871. unsigned int dest, irq = data->irq;
  872. struct irte irte;
  873. int err;
  874. if (!config_enabled(CONFIG_SMP))
  875. return -EINVAL;
  876. if (!cpumask_intersects(mask, cpu_online_mask))
  877. return -EINVAL;
  878. if (get_irte(irq, &irte))
  879. return -EBUSY;
  880. err = assign_irq_vector(irq, cfg, mask);
  881. if (err)
  882. return err;
  883. err = apic->cpu_mask_to_apicid_and(cfg->domain, mask, &dest);
  884. if (err) {
  885. if (assign_irq_vector(irq, cfg, data->affinity))
  886. pr_err("Failed to recover vector for irq %d\n", irq);
  887. return err;
  888. }
  889. irte.vector = cfg->vector;
  890. irte.dest_id = IRTE_DEST(dest);
  891. /*
  892. * Atomically updates the IRTE with the new destination, vector
  893. * and flushes the interrupt entry cache.
  894. */
  895. modify_irte(irq, &irte);
  896. /*
  897. * After this point, all the interrupts will start arriving
  898. * at the new destination. So, time to cleanup the previous
  899. * vector allocation.
  900. */
  901. if (cfg->move_in_progress)
  902. send_cleanup_vector(cfg);
  903. cpumask_copy(data->affinity, mask);
  904. return 0;
  905. }
  906. static void intel_compose_msi_msg(struct pci_dev *pdev,
  907. unsigned int irq, unsigned int dest,
  908. struct msi_msg *msg, u8 hpet_id)
  909. {
  910. struct irq_cfg *cfg;
  911. struct irte irte;
  912. u16 sub_handle = 0;
  913. int ir_index;
  914. cfg = irq_cfg(irq);
  915. ir_index = map_irq_to_irte_handle(irq, &sub_handle);
  916. BUG_ON(ir_index == -1);
  917. prepare_irte(&irte, cfg->vector, dest);
  918. /* Set source-id of interrupt request */
  919. if (pdev)
  920. set_msi_sid(&irte, pdev);
  921. else
  922. set_hpet_sid(&irte, hpet_id);
  923. modify_irte(irq, &irte);
  924. msg->address_hi = MSI_ADDR_BASE_HI;
  925. msg->data = sub_handle;
  926. msg->address_lo = MSI_ADDR_BASE_LO | MSI_ADDR_IR_EXT_INT |
  927. MSI_ADDR_IR_SHV |
  928. MSI_ADDR_IR_INDEX1(ir_index) |
  929. MSI_ADDR_IR_INDEX2(ir_index);
  930. }
  931. /*
  932. * Map the PCI dev to the corresponding remapping hardware unit
  933. * and allocate 'nvec' consecutive interrupt-remapping table entries
  934. * in it.
  935. */
  936. static int intel_msi_alloc_irq(struct pci_dev *dev, int irq, int nvec)
  937. {
  938. struct intel_iommu *iommu;
  939. int index;
  940. down_read(&dmar_global_lock);
  941. iommu = map_dev_to_ir(dev);
  942. if (!iommu) {
  943. printk(KERN_ERR
  944. "Unable to map PCI %s to iommu\n", pci_name(dev));
  945. index = -ENOENT;
  946. } else {
  947. index = alloc_irte(iommu, irq, nvec);
  948. if (index < 0) {
  949. printk(KERN_ERR
  950. "Unable to allocate %d IRTE for PCI %s\n",
  951. nvec, pci_name(dev));
  952. index = -ENOSPC;
  953. }
  954. }
  955. up_read(&dmar_global_lock);
  956. return index;
  957. }
  958. static int intel_msi_setup_irq(struct pci_dev *pdev, unsigned int irq,
  959. int index, int sub_handle)
  960. {
  961. struct intel_iommu *iommu;
  962. int ret = -ENOENT;
  963. down_read(&dmar_global_lock);
  964. iommu = map_dev_to_ir(pdev);
  965. if (iommu) {
  966. /*
  967. * setup the mapping between the irq and the IRTE
  968. * base index, the sub_handle pointing to the
  969. * appropriate interrupt remap table entry.
  970. */
  971. set_irte_irq(irq, iommu, index, sub_handle);
  972. ret = 0;
  973. }
  974. up_read(&dmar_global_lock);
  975. return ret;
  976. }
  977. static int intel_alloc_hpet_msi(unsigned int irq, unsigned int id)
  978. {
  979. int ret = -1;
  980. struct intel_iommu *iommu;
  981. int index;
  982. down_read(&dmar_global_lock);
  983. iommu = map_hpet_to_ir(id);
  984. if (iommu) {
  985. index = alloc_irte(iommu, irq, 1);
  986. if (index >= 0)
  987. ret = 0;
  988. }
  989. up_read(&dmar_global_lock);
  990. return ret;
  991. }
  992. struct irq_remap_ops intel_irq_remap_ops = {
  993. .prepare = intel_prepare_irq_remapping,
  994. .enable = intel_enable_irq_remapping,
  995. .disable = disable_irq_remapping,
  996. .reenable = reenable_irq_remapping,
  997. .enable_faulting = enable_drhd_fault_handling,
  998. .setup_ioapic_entry = intel_setup_ioapic_entry,
  999. .set_affinity = intel_ioapic_set_affinity,
  1000. .free_irq = free_irte,
  1001. .compose_msi_msg = intel_compose_msi_msg,
  1002. .msi_alloc_irq = intel_msi_alloc_irq,
  1003. .msi_setup_irq = intel_msi_setup_irq,
  1004. .alloc_hpet_msi = intel_alloc_hpet_msi,
  1005. };
  1006. /*
  1007. * Support of Interrupt Remapping Unit Hotplug
  1008. */
  1009. static int dmar_ir_add(struct dmar_drhd_unit *dmaru, struct intel_iommu *iommu)
  1010. {
  1011. int ret;
  1012. int eim = x2apic_enabled();
  1013. if (eim && !ecap_eim_support(iommu->ecap)) {
  1014. pr_info("DRHD %Lx: EIM not supported by DRHD, ecap %Lx\n",
  1015. iommu->reg_phys, iommu->ecap);
  1016. return -ENODEV;
  1017. }
  1018. if (ir_parse_ioapic_hpet_scope(dmaru->hdr, iommu)) {
  1019. pr_warn("DRHD %Lx: failed to parse managed IOAPIC/HPET\n",
  1020. iommu->reg_phys);
  1021. return -ENODEV;
  1022. }
  1023. /* TODO: check all IOAPICs are covered by IOMMU */
  1024. /* Setup Interrupt-remapping now. */
  1025. ret = intel_setup_irq_remapping(iommu);
  1026. if (ret) {
  1027. pr_err("DRHD %Lx: failed to allocate resource\n",
  1028. iommu->reg_phys);
  1029. ir_remove_ioapic_hpet_scope(iommu);
  1030. return ret;
  1031. }
  1032. if (!iommu->qi) {
  1033. /* Clear previous faults. */
  1034. dmar_fault(-1, iommu);
  1035. iommu_disable_irq_remapping(iommu);
  1036. dmar_disable_qi(iommu);
  1037. }
  1038. /* Enable queued invalidation */
  1039. ret = dmar_enable_qi(iommu);
  1040. if (!ret) {
  1041. iommu_set_irq_remapping(iommu, eim);
  1042. } else {
  1043. pr_err("DRHD %Lx: failed to enable queued invalidation, ecap %Lx, ret %d\n",
  1044. iommu->reg_phys, iommu->ecap, ret);
  1045. intel_teardown_irq_remapping(iommu);
  1046. ir_remove_ioapic_hpet_scope(iommu);
  1047. }
  1048. return ret;
  1049. }
  1050. int dmar_ir_hotplug(struct dmar_drhd_unit *dmaru, bool insert)
  1051. {
  1052. int ret = 0;
  1053. struct intel_iommu *iommu = dmaru->iommu;
  1054. if (!irq_remapping_enabled)
  1055. return 0;
  1056. if (iommu == NULL)
  1057. return -EINVAL;
  1058. if (!ecap_ir_support(iommu->ecap))
  1059. return 0;
  1060. if (insert) {
  1061. if (!iommu->ir_table)
  1062. ret = dmar_ir_add(dmaru, iommu);
  1063. } else {
  1064. if (iommu->ir_table) {
  1065. if (!bitmap_empty(iommu->ir_table->bitmap,
  1066. INTR_REMAP_TABLE_ENTRIES)) {
  1067. ret = -EBUSY;
  1068. } else {
  1069. iommu_disable_irq_remapping(iommu);
  1070. intel_teardown_irq_remapping(iommu);
  1071. ir_remove_ioapic_hpet_scope(iommu);
  1072. }
  1073. }
  1074. }
  1075. return ret;
  1076. }