intr_remapping.c 17 KB

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