intr_remapping.c 18 KB

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