intel_irq_remapping.c 25 KB

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