intel_intr_remapping.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824
  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/intr_remapping.h>
  15. #include <asm/pci-direct.h>
  16. #include "intr_remapping.h"
  17. struct ioapic_scope {
  18. struct intel_iommu *iommu;
  19. unsigned int id;
  20. unsigned int bus; /* PCI bus number */
  21. unsigned int devfn; /* PCI devfn number */
  22. };
  23. struct hpet_scope {
  24. struct intel_iommu *iommu;
  25. u8 id;
  26. unsigned int bus;
  27. unsigned int devfn;
  28. };
  29. #define IR_X2APIC_MODE(mode) (mode ? (1 << 11) : 0)
  30. static struct ioapic_scope ir_ioapic[MAX_IO_APICS];
  31. static struct hpet_scope ir_hpet[MAX_HPET_TBS];
  32. static int ir_ioapic_num, ir_hpet_num;
  33. static DEFINE_RAW_SPINLOCK(irq_2_ir_lock);
  34. static struct irq_2_iommu *irq_2_iommu(unsigned int irq)
  35. {
  36. struct irq_cfg *cfg = irq_get_chip_data(irq);
  37. return cfg ? &cfg->irq_2_iommu : NULL;
  38. }
  39. int get_irte(int irq, struct irte *entry)
  40. {
  41. struct irq_2_iommu *irq_iommu = irq_2_iommu(irq);
  42. unsigned long flags;
  43. int index;
  44. if (!entry || !irq_iommu)
  45. return -1;
  46. raw_spin_lock_irqsave(&irq_2_ir_lock, flags);
  47. index = irq_iommu->irte_index + irq_iommu->sub_handle;
  48. *entry = *(irq_iommu->iommu->ir_table->base + index);
  49. raw_spin_unlock_irqrestore(&irq_2_ir_lock, flags);
  50. return 0;
  51. }
  52. int alloc_irte(struct intel_iommu *iommu, int irq, u16 count)
  53. {
  54. struct ir_table *table = iommu->ir_table;
  55. struct irq_2_iommu *irq_iommu = irq_2_iommu(irq);
  56. u16 index, start_index;
  57. unsigned int mask = 0;
  58. unsigned long flags;
  59. int i;
  60. if (!count || !irq_iommu)
  61. return -1;
  62. /*
  63. * start the IRTE search from index 0.
  64. */
  65. index = start_index = 0;
  66. if (count > 1) {
  67. count = __roundup_pow_of_two(count);
  68. mask = ilog2(count);
  69. }
  70. if (mask > ecap_max_handle_mask(iommu->ecap)) {
  71. printk(KERN_ERR
  72. "Requested mask %x exceeds the max invalidation handle"
  73. " mask value %Lx\n", mask,
  74. ecap_max_handle_mask(iommu->ecap));
  75. return -1;
  76. }
  77. raw_spin_lock_irqsave(&irq_2_ir_lock, flags);
  78. do {
  79. for (i = index; i < index + count; i++)
  80. if (table->base[i].present)
  81. break;
  82. /* empty index found */
  83. if (i == index + count)
  84. break;
  85. index = (index + count) % INTR_REMAP_TABLE_ENTRIES;
  86. if (index == start_index) {
  87. raw_spin_unlock_irqrestore(&irq_2_ir_lock, flags);
  88. printk(KERN_ERR "can't allocate an IRTE\n");
  89. return -1;
  90. }
  91. } while (1);
  92. for (i = index; i < index + count; i++)
  93. table->base[i].present = 1;
  94. irq_iommu->iommu = iommu;
  95. irq_iommu->irte_index = index;
  96. irq_iommu->sub_handle = 0;
  97. irq_iommu->irte_mask = mask;
  98. raw_spin_unlock_irqrestore(&irq_2_ir_lock, flags);
  99. return index;
  100. }
  101. static int qi_flush_iec(struct intel_iommu *iommu, int index, int mask)
  102. {
  103. struct qi_desc desc;
  104. desc.low = QI_IEC_IIDEX(index) | QI_IEC_TYPE | QI_IEC_IM(mask)
  105. | QI_IEC_SELECTIVE;
  106. desc.high = 0;
  107. return qi_submit_sync(&desc, iommu);
  108. }
  109. int map_irq_to_irte_handle(int irq, u16 *sub_handle)
  110. {
  111. struct irq_2_iommu *irq_iommu = irq_2_iommu(irq);
  112. unsigned long flags;
  113. int index;
  114. if (!irq_iommu)
  115. return -1;
  116. raw_spin_lock_irqsave(&irq_2_ir_lock, flags);
  117. *sub_handle = irq_iommu->sub_handle;
  118. index = irq_iommu->irte_index;
  119. raw_spin_unlock_irqrestore(&irq_2_ir_lock, flags);
  120. return index;
  121. }
  122. int set_irte_irq(int irq, struct intel_iommu *iommu, u16 index, u16 subhandle)
  123. {
  124. struct irq_2_iommu *irq_iommu = irq_2_iommu(irq);
  125. unsigned long flags;
  126. if (!irq_iommu)
  127. return -1;
  128. raw_spin_lock_irqsave(&irq_2_ir_lock, flags);
  129. irq_iommu->iommu = iommu;
  130. irq_iommu->irte_index = index;
  131. irq_iommu->sub_handle = subhandle;
  132. irq_iommu->irte_mask = 0;
  133. raw_spin_unlock_irqrestore(&irq_2_ir_lock, flags);
  134. return 0;
  135. }
  136. int modify_irte(int irq, struct irte *irte_modified)
  137. {
  138. struct irq_2_iommu *irq_iommu = irq_2_iommu(irq);
  139. struct intel_iommu *iommu;
  140. unsigned long flags;
  141. struct irte *irte;
  142. int rc, index;
  143. if (!irq_iommu)
  144. return -1;
  145. raw_spin_lock_irqsave(&irq_2_ir_lock, flags);
  146. iommu = irq_iommu->iommu;
  147. index = irq_iommu->irte_index + irq_iommu->sub_handle;
  148. irte = &iommu->ir_table->base[index];
  149. set_64bit(&irte->low, irte_modified->low);
  150. set_64bit(&irte->high, irte_modified->high);
  151. __iommu_flush_cache(iommu, irte, sizeof(*irte));
  152. rc = qi_flush_iec(iommu, index, 0);
  153. raw_spin_unlock_irqrestore(&irq_2_ir_lock, flags);
  154. return rc;
  155. }
  156. struct intel_iommu *map_hpet_to_ir(u8 hpet_id)
  157. {
  158. int i;
  159. for (i = 0; i < MAX_HPET_TBS; i++)
  160. if (ir_hpet[i].id == hpet_id)
  161. return ir_hpet[i].iommu;
  162. return NULL;
  163. }
  164. struct intel_iommu *map_ioapic_to_ir(int apic)
  165. {
  166. int i;
  167. for (i = 0; i < MAX_IO_APICS; i++)
  168. if (ir_ioapic[i].id == apic)
  169. return ir_ioapic[i].iommu;
  170. return NULL;
  171. }
  172. struct intel_iommu *map_dev_to_ir(struct pci_dev *dev)
  173. {
  174. struct dmar_drhd_unit *drhd;
  175. drhd = dmar_find_matched_drhd_unit(dev);
  176. if (!drhd)
  177. return NULL;
  178. return drhd->iommu;
  179. }
  180. static int clear_entries(struct irq_2_iommu *irq_iommu)
  181. {
  182. struct irte *start, *entry, *end;
  183. struct intel_iommu *iommu;
  184. int index;
  185. if (irq_iommu->sub_handle)
  186. return 0;
  187. iommu = irq_iommu->iommu;
  188. index = irq_iommu->irte_index + irq_iommu->sub_handle;
  189. start = iommu->ir_table->base + index;
  190. end = start + (1 << irq_iommu->irte_mask);
  191. for (entry = start; entry < end; entry++) {
  192. set_64bit(&entry->low, 0);
  193. set_64bit(&entry->high, 0);
  194. }
  195. return qi_flush_iec(iommu, index, irq_iommu->irte_mask);
  196. }
  197. int free_irte(int irq)
  198. {
  199. struct irq_2_iommu *irq_iommu = irq_2_iommu(irq);
  200. unsigned long flags;
  201. int rc;
  202. if (!irq_iommu)
  203. return -1;
  204. raw_spin_lock_irqsave(&irq_2_ir_lock, flags);
  205. rc = clear_entries(irq_iommu);
  206. irq_iommu->iommu = NULL;
  207. irq_iommu->irte_index = 0;
  208. irq_iommu->sub_handle = 0;
  209. irq_iommu->irte_mask = 0;
  210. raw_spin_unlock_irqrestore(&irq_2_ir_lock, flags);
  211. return rc;
  212. }
  213. /*
  214. * source validation type
  215. */
  216. #define SVT_NO_VERIFY 0x0 /* no verification is required */
  217. #define SVT_VERIFY_SID_SQ 0x1 /* verify using SID and SQ fields */
  218. #define SVT_VERIFY_BUS 0x2 /* verify bus of request-id */
  219. /*
  220. * source-id qualifier
  221. */
  222. #define SQ_ALL_16 0x0 /* verify all 16 bits of request-id */
  223. #define SQ_13_IGNORE_1 0x1 /* verify most significant 13 bits, ignore
  224. * the third least significant bit
  225. */
  226. #define SQ_13_IGNORE_2 0x2 /* verify most significant 13 bits, ignore
  227. * the second and third least significant bits
  228. */
  229. #define SQ_13_IGNORE_3 0x3 /* verify most significant 13 bits, ignore
  230. * the least three significant bits
  231. */
  232. /*
  233. * set SVT, SQ and SID fields of irte to verify
  234. * source ids of interrupt requests
  235. */
  236. static void set_irte_sid(struct irte *irte, unsigned int svt,
  237. unsigned int sq, unsigned int sid)
  238. {
  239. if (disable_sourceid_checking)
  240. svt = SVT_NO_VERIFY;
  241. irte->svt = svt;
  242. irte->sq = sq;
  243. irte->sid = sid;
  244. }
  245. int set_ioapic_sid(struct irte *irte, int apic)
  246. {
  247. int i;
  248. u16 sid = 0;
  249. if (!irte)
  250. return -1;
  251. for (i = 0; i < MAX_IO_APICS; i++) {
  252. if (ir_ioapic[i].id == apic) {
  253. sid = (ir_ioapic[i].bus << 8) | ir_ioapic[i].devfn;
  254. break;
  255. }
  256. }
  257. if (sid == 0) {
  258. pr_warning("Failed to set source-id of IOAPIC (%d)\n", apic);
  259. return -1;
  260. }
  261. set_irte_sid(irte, 1, 0, sid);
  262. return 0;
  263. }
  264. int set_hpet_sid(struct irte *irte, u8 id)
  265. {
  266. int i;
  267. u16 sid = 0;
  268. if (!irte)
  269. return -1;
  270. for (i = 0; i < MAX_HPET_TBS; i++) {
  271. if (ir_hpet[i].id == id) {
  272. sid = (ir_hpet[i].bus << 8) | ir_hpet[i].devfn;
  273. break;
  274. }
  275. }
  276. if (sid == 0) {
  277. pr_warning("Failed to set source-id of HPET block (%d)\n", id);
  278. return -1;
  279. }
  280. /*
  281. * Should really use SQ_ALL_16. Some platforms are broken.
  282. * While we figure out the right quirks for these broken platforms, use
  283. * SQ_13_IGNORE_3 for now.
  284. */
  285. set_irte_sid(irte, SVT_VERIFY_SID_SQ, SQ_13_IGNORE_3, sid);
  286. return 0;
  287. }
  288. int set_msi_sid(struct irte *irte, struct pci_dev *dev)
  289. {
  290. struct pci_dev *bridge;
  291. if (!irte || !dev)
  292. return -1;
  293. /* PCIe device or Root Complex integrated PCI device */
  294. if (pci_is_pcie(dev) || !dev->bus->parent) {
  295. set_irte_sid(irte, SVT_VERIFY_SID_SQ, SQ_ALL_16,
  296. (dev->bus->number << 8) | dev->devfn);
  297. return 0;
  298. }
  299. bridge = pci_find_upstream_pcie_bridge(dev);
  300. if (bridge) {
  301. if (pci_is_pcie(bridge))/* this is a PCIe-to-PCI/PCIX bridge */
  302. set_irte_sid(irte, SVT_VERIFY_BUS, SQ_ALL_16,
  303. (bridge->bus->number << 8) | dev->bus->number);
  304. else /* this is a legacy PCI bridge */
  305. set_irte_sid(irte, SVT_VERIFY_SID_SQ, SQ_ALL_16,
  306. (bridge->bus->number << 8) | bridge->devfn);
  307. }
  308. return 0;
  309. }
  310. static void iommu_set_intr_remapping(struct intel_iommu *iommu, int mode)
  311. {
  312. u64 addr;
  313. u32 sts;
  314. unsigned long flags;
  315. addr = virt_to_phys((void *)iommu->ir_table->base);
  316. raw_spin_lock_irqsave(&iommu->register_lock, flags);
  317. dmar_writeq(iommu->reg + DMAR_IRTA_REG,
  318. (addr) | IR_X2APIC_MODE(mode) | INTR_REMAP_TABLE_REG_SIZE);
  319. /* Set interrupt-remapping table pointer */
  320. iommu->gcmd |= DMA_GCMD_SIRTP;
  321. writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
  322. IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
  323. readl, (sts & DMA_GSTS_IRTPS), sts);
  324. raw_spin_unlock_irqrestore(&iommu->register_lock, flags);
  325. /*
  326. * global invalidation of interrupt entry cache before enabling
  327. * interrupt-remapping.
  328. */
  329. qi_global_iec(iommu);
  330. raw_spin_lock_irqsave(&iommu->register_lock, flags);
  331. /* Enable interrupt-remapping */
  332. iommu->gcmd |= DMA_GCMD_IRE;
  333. writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
  334. IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
  335. readl, (sts & DMA_GSTS_IRES), sts);
  336. raw_spin_unlock_irqrestore(&iommu->register_lock, flags);
  337. }
  338. static int intel_setup_intr_remapping(struct intel_iommu *iommu, int mode)
  339. {
  340. struct ir_table *ir_table;
  341. struct page *pages;
  342. ir_table = iommu->ir_table = kzalloc(sizeof(struct ir_table),
  343. GFP_ATOMIC);
  344. if (!iommu->ir_table)
  345. return -ENOMEM;
  346. pages = alloc_pages_node(iommu->node, GFP_ATOMIC | __GFP_ZERO,
  347. INTR_REMAP_PAGE_ORDER);
  348. if (!pages) {
  349. printk(KERN_ERR "failed to allocate pages of order %d\n",
  350. INTR_REMAP_PAGE_ORDER);
  351. kfree(iommu->ir_table);
  352. return -ENOMEM;
  353. }
  354. ir_table->base = page_address(pages);
  355. iommu_set_intr_remapping(iommu, mode);
  356. return 0;
  357. }
  358. /*
  359. * Disable Interrupt Remapping.
  360. */
  361. static void iommu_disable_intr_remapping(struct intel_iommu *iommu)
  362. {
  363. unsigned long flags;
  364. u32 sts;
  365. if (!ecap_ir_support(iommu->ecap))
  366. return;
  367. /*
  368. * global invalidation of interrupt entry cache before disabling
  369. * interrupt-remapping.
  370. */
  371. qi_global_iec(iommu);
  372. raw_spin_lock_irqsave(&iommu->register_lock, flags);
  373. sts = dmar_readq(iommu->reg + DMAR_GSTS_REG);
  374. if (!(sts & DMA_GSTS_IRES))
  375. goto end;
  376. iommu->gcmd &= ~DMA_GCMD_IRE;
  377. writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
  378. IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
  379. readl, !(sts & DMA_GSTS_IRES), sts);
  380. end:
  381. raw_spin_unlock_irqrestore(&iommu->register_lock, flags);
  382. }
  383. static int __init dmar_x2apic_optout(void)
  384. {
  385. struct acpi_table_dmar *dmar;
  386. dmar = (struct acpi_table_dmar *)dmar_tbl;
  387. if (!dmar || no_x2apic_optout)
  388. return 0;
  389. return dmar->flags & DMAR_X2APIC_OPT_OUT;
  390. }
  391. static int __init intel_intr_remapping_supported(void)
  392. {
  393. struct dmar_drhd_unit *drhd;
  394. if (disable_intremap)
  395. return 0;
  396. if (!dmar_ir_support())
  397. return 0;
  398. for_each_drhd_unit(drhd) {
  399. struct intel_iommu *iommu = drhd->iommu;
  400. if (!ecap_ir_support(iommu->ecap))
  401. return 0;
  402. }
  403. return 1;
  404. }
  405. static int __init intel_enable_intr_remapping(void)
  406. {
  407. struct dmar_drhd_unit *drhd;
  408. int setup = 0;
  409. int eim = 0;
  410. if (parse_ioapics_under_ir() != 1) {
  411. printk(KERN_INFO "Not enable interrupt remapping\n");
  412. return -1;
  413. }
  414. if (x2apic_supported()) {
  415. eim = !dmar_x2apic_optout();
  416. WARN(!eim, KERN_WARNING
  417. "Your BIOS is broken and requested that x2apic be disabled\n"
  418. "This will leave your machine vulnerable to irq-injection attacks\n"
  419. "Use 'intremap=no_x2apic_optout' to override BIOS request\n");
  420. }
  421. for_each_drhd_unit(drhd) {
  422. struct intel_iommu *iommu = drhd->iommu;
  423. /*
  424. * If the queued invalidation is already initialized,
  425. * shouldn't disable it.
  426. */
  427. if (iommu->qi)
  428. continue;
  429. /*
  430. * Clear previous faults.
  431. */
  432. dmar_fault(-1, iommu);
  433. /*
  434. * Disable intr remapping and queued invalidation, if already
  435. * enabled prior to OS handover.
  436. */
  437. iommu_disable_intr_remapping(iommu);
  438. dmar_disable_qi(iommu);
  439. }
  440. /*
  441. * check for the Interrupt-remapping support
  442. */
  443. for_each_drhd_unit(drhd) {
  444. struct intel_iommu *iommu = drhd->iommu;
  445. if (!ecap_ir_support(iommu->ecap))
  446. continue;
  447. if (eim && !ecap_eim_support(iommu->ecap)) {
  448. printk(KERN_INFO "DRHD %Lx: EIM not supported by DRHD, "
  449. " ecap %Lx\n", drhd->reg_base_addr, iommu->ecap);
  450. return -1;
  451. }
  452. }
  453. /*
  454. * Enable queued invalidation for all the DRHD's.
  455. */
  456. for_each_drhd_unit(drhd) {
  457. int ret;
  458. struct intel_iommu *iommu = drhd->iommu;
  459. ret = dmar_enable_qi(iommu);
  460. if (ret) {
  461. printk(KERN_ERR "DRHD %Lx: failed to enable queued, "
  462. " invalidation, ecap %Lx, ret %d\n",
  463. drhd->reg_base_addr, iommu->ecap, ret);
  464. return -1;
  465. }
  466. }
  467. /*
  468. * Setup Interrupt-remapping for all the DRHD's now.
  469. */
  470. for_each_drhd_unit(drhd) {
  471. struct intel_iommu *iommu = drhd->iommu;
  472. if (!ecap_ir_support(iommu->ecap))
  473. continue;
  474. if (intel_setup_intr_remapping(iommu, eim))
  475. goto error;
  476. setup = 1;
  477. }
  478. if (!setup)
  479. goto error;
  480. intr_remapping_enabled = 1;
  481. pr_info("Enabled IRQ remapping in %s mode\n", eim ? "x2apic" : "xapic");
  482. return eim ? IRQ_REMAP_X2APIC_MODE : IRQ_REMAP_XAPIC_MODE;
  483. error:
  484. /*
  485. * handle error condition gracefully here!
  486. */
  487. return -1;
  488. }
  489. static void ir_parse_one_hpet_scope(struct acpi_dmar_device_scope *scope,
  490. struct intel_iommu *iommu)
  491. {
  492. struct acpi_dmar_pci_path *path;
  493. u8 bus;
  494. int count;
  495. bus = scope->bus;
  496. path = (struct acpi_dmar_pci_path *)(scope + 1);
  497. count = (scope->length - sizeof(struct acpi_dmar_device_scope))
  498. / sizeof(struct acpi_dmar_pci_path);
  499. while (--count > 0) {
  500. /*
  501. * Access PCI directly due to the PCI
  502. * subsystem isn't initialized yet.
  503. */
  504. bus = read_pci_config_byte(bus, path->dev, path->fn,
  505. PCI_SECONDARY_BUS);
  506. path++;
  507. }
  508. ir_hpet[ir_hpet_num].bus = bus;
  509. ir_hpet[ir_hpet_num].devfn = PCI_DEVFN(path->dev, path->fn);
  510. ir_hpet[ir_hpet_num].iommu = iommu;
  511. ir_hpet[ir_hpet_num].id = scope->enumeration_id;
  512. ir_hpet_num++;
  513. }
  514. static void ir_parse_one_ioapic_scope(struct acpi_dmar_device_scope *scope,
  515. struct intel_iommu *iommu)
  516. {
  517. struct acpi_dmar_pci_path *path;
  518. u8 bus;
  519. int count;
  520. bus = scope->bus;
  521. path = (struct acpi_dmar_pci_path *)(scope + 1);
  522. count = (scope->length - sizeof(struct acpi_dmar_device_scope))
  523. / sizeof(struct acpi_dmar_pci_path);
  524. while (--count > 0) {
  525. /*
  526. * Access PCI directly due to the PCI
  527. * subsystem isn't initialized yet.
  528. */
  529. bus = read_pci_config_byte(bus, path->dev, path->fn,
  530. PCI_SECONDARY_BUS);
  531. path++;
  532. }
  533. ir_ioapic[ir_ioapic_num].bus = bus;
  534. ir_ioapic[ir_ioapic_num].devfn = PCI_DEVFN(path->dev, path->fn);
  535. ir_ioapic[ir_ioapic_num].iommu = iommu;
  536. ir_ioapic[ir_ioapic_num].id = scope->enumeration_id;
  537. ir_ioapic_num++;
  538. }
  539. static int ir_parse_ioapic_hpet_scope(struct acpi_dmar_header *header,
  540. struct intel_iommu *iommu)
  541. {
  542. struct acpi_dmar_hardware_unit *drhd;
  543. struct acpi_dmar_device_scope *scope;
  544. void *start, *end;
  545. drhd = (struct acpi_dmar_hardware_unit *)header;
  546. start = (void *)(drhd + 1);
  547. end = ((void *)drhd) + header->length;
  548. while (start < end) {
  549. scope = start;
  550. if (scope->entry_type == ACPI_DMAR_SCOPE_TYPE_IOAPIC) {
  551. if (ir_ioapic_num == MAX_IO_APICS) {
  552. printk(KERN_WARNING "Exceeded Max IO APICS\n");
  553. return -1;
  554. }
  555. printk(KERN_INFO "IOAPIC id %d under DRHD base "
  556. " 0x%Lx IOMMU %d\n", scope->enumeration_id,
  557. drhd->address, iommu->seq_id);
  558. ir_parse_one_ioapic_scope(scope, iommu);
  559. } else if (scope->entry_type == ACPI_DMAR_SCOPE_TYPE_HPET) {
  560. if (ir_hpet_num == MAX_HPET_TBS) {
  561. printk(KERN_WARNING "Exceeded Max HPET blocks\n");
  562. return -1;
  563. }
  564. printk(KERN_INFO "HPET id %d under DRHD base"
  565. " 0x%Lx\n", scope->enumeration_id,
  566. drhd->address);
  567. ir_parse_one_hpet_scope(scope, iommu);
  568. }
  569. start += scope->length;
  570. }
  571. return 0;
  572. }
  573. /*
  574. * Finds the assocaition between IOAPIC's and its Interrupt-remapping
  575. * hardware unit.
  576. */
  577. int __init parse_ioapics_under_ir(void)
  578. {
  579. struct dmar_drhd_unit *drhd;
  580. int ir_supported = 0;
  581. for_each_drhd_unit(drhd) {
  582. struct intel_iommu *iommu = drhd->iommu;
  583. if (ecap_ir_support(iommu->ecap)) {
  584. if (ir_parse_ioapic_hpet_scope(drhd->hdr, iommu))
  585. return -1;
  586. ir_supported = 1;
  587. }
  588. }
  589. if (ir_supported && ir_ioapic_num != nr_ioapics) {
  590. printk(KERN_WARNING
  591. "Not all IO-APIC's listed under remapping hardware\n");
  592. return -1;
  593. }
  594. return ir_supported;
  595. }
  596. int __init ir_dev_scope_init(void)
  597. {
  598. if (!intr_remapping_enabled)
  599. return 0;
  600. return dmar_dev_scope_init();
  601. }
  602. rootfs_initcall(ir_dev_scope_init);
  603. static void disable_intr_remapping(void)
  604. {
  605. struct dmar_drhd_unit *drhd;
  606. struct intel_iommu *iommu = NULL;
  607. /*
  608. * Disable Interrupt-remapping for all the DRHD's now.
  609. */
  610. for_each_iommu(iommu, drhd) {
  611. if (!ecap_ir_support(iommu->ecap))
  612. continue;
  613. iommu_disable_intr_remapping(iommu);
  614. }
  615. }
  616. static int reenable_intr_remapping(int eim)
  617. {
  618. struct dmar_drhd_unit *drhd;
  619. int setup = 0;
  620. struct intel_iommu *iommu = NULL;
  621. for_each_iommu(iommu, drhd)
  622. if (iommu->qi)
  623. dmar_reenable_qi(iommu);
  624. /*
  625. * Setup Interrupt-remapping for all the DRHD's now.
  626. */
  627. for_each_iommu(iommu, drhd) {
  628. if (!ecap_ir_support(iommu->ecap))
  629. continue;
  630. /* Set up interrupt remapping for iommu.*/
  631. iommu_set_intr_remapping(iommu, eim);
  632. setup = 1;
  633. }
  634. if (!setup)
  635. goto error;
  636. return 0;
  637. error:
  638. /*
  639. * handle error condition gracefully here!
  640. */
  641. return -1;
  642. }
  643. struct irq_remap_ops intel_irq_remap_ops = {
  644. .supported = intel_intr_remapping_supported,
  645. .hardware_init = dmar_table_init,
  646. .hardware_enable = intel_enable_intr_remapping,
  647. .hardware_disable = disable_intr_remapping,
  648. .hardware_reenable = reenable_intr_remapping,
  649. .enable_faulting = enable_drhd_fault_handling,
  650. };