intel_irq_remapping.c 31 KB

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