intel_irq_remapping.c 30 KB

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