intr_remapping.c 18 KB

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