intr_remapping.c 19 KB

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