intel_intr_remapping.c 18 KB

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