intr_remapping.c 18 KB

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