intr_remapping.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935
  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_node(iommu->node, GFP_ATOMIC | __GFP_ZERO,
  463. INTR_REMAP_PAGE_ORDER);
  464. if (!pages) {
  465. printk(KERN_ERR "failed to allocate pages of order %d\n",
  466. INTR_REMAP_PAGE_ORDER);
  467. kfree(iommu->ir_table);
  468. return -ENOMEM;
  469. }
  470. ir_table->base = page_address(pages);
  471. iommu_set_intr_remapping(iommu, mode);
  472. return 0;
  473. }
  474. /*
  475. * Disable Interrupt Remapping.
  476. */
  477. static void iommu_disable_intr_remapping(struct intel_iommu *iommu)
  478. {
  479. unsigned long flags;
  480. u32 sts;
  481. if (!ecap_ir_support(iommu->ecap))
  482. return;
  483. /*
  484. * global invalidation of interrupt entry cache before disabling
  485. * interrupt-remapping.
  486. */
  487. qi_global_iec(iommu);
  488. spin_lock_irqsave(&iommu->register_lock, flags);
  489. sts = dmar_readq(iommu->reg + DMAR_GSTS_REG);
  490. if (!(sts & DMA_GSTS_IRES))
  491. goto end;
  492. iommu->gcmd &= ~DMA_GCMD_IRE;
  493. writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
  494. IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
  495. readl, !(sts & DMA_GSTS_IRES), sts);
  496. end:
  497. spin_unlock_irqrestore(&iommu->register_lock, flags);
  498. }
  499. int __init intr_remapping_supported(void)
  500. {
  501. struct dmar_drhd_unit *drhd;
  502. if (disable_intremap)
  503. return 0;
  504. if (!dmar_ir_support())
  505. return 0;
  506. for_each_drhd_unit(drhd) {
  507. struct intel_iommu *iommu = drhd->iommu;
  508. if (!ecap_ir_support(iommu->ecap))
  509. return 0;
  510. }
  511. return 1;
  512. }
  513. int __init enable_intr_remapping(int eim)
  514. {
  515. struct dmar_drhd_unit *drhd;
  516. int setup = 0;
  517. if (parse_ioapics_under_ir() != 1) {
  518. printk(KERN_INFO "Not enable interrupt remapping\n");
  519. return -1;
  520. }
  521. for_each_drhd_unit(drhd) {
  522. struct intel_iommu *iommu = drhd->iommu;
  523. /*
  524. * If the queued invalidation is already initialized,
  525. * shouldn't disable it.
  526. */
  527. if (iommu->qi)
  528. continue;
  529. /*
  530. * Clear previous faults.
  531. */
  532. dmar_fault(-1, iommu);
  533. /*
  534. * Disable intr remapping and queued invalidation, if already
  535. * enabled prior to OS handover.
  536. */
  537. iommu_disable_intr_remapping(iommu);
  538. dmar_disable_qi(iommu);
  539. }
  540. /*
  541. * check for the Interrupt-remapping support
  542. */
  543. for_each_drhd_unit(drhd) {
  544. struct intel_iommu *iommu = drhd->iommu;
  545. if (!ecap_ir_support(iommu->ecap))
  546. continue;
  547. if (eim && !ecap_eim_support(iommu->ecap)) {
  548. printk(KERN_INFO "DRHD %Lx: EIM not supported by DRHD, "
  549. " ecap %Lx\n", drhd->reg_base_addr, iommu->ecap);
  550. return -1;
  551. }
  552. }
  553. /*
  554. * Enable queued invalidation for all the DRHD's.
  555. */
  556. for_each_drhd_unit(drhd) {
  557. int ret;
  558. struct intel_iommu *iommu = drhd->iommu;
  559. ret = dmar_enable_qi(iommu);
  560. if (ret) {
  561. printk(KERN_ERR "DRHD %Lx: failed to enable queued, "
  562. " invalidation, ecap %Lx, ret %d\n",
  563. drhd->reg_base_addr, iommu->ecap, ret);
  564. return -1;
  565. }
  566. }
  567. /*
  568. * Setup Interrupt-remapping for all the DRHD's now.
  569. */
  570. for_each_drhd_unit(drhd) {
  571. struct intel_iommu *iommu = drhd->iommu;
  572. if (!ecap_ir_support(iommu->ecap))
  573. continue;
  574. if (setup_intr_remapping(iommu, eim))
  575. goto error;
  576. setup = 1;
  577. }
  578. if (!setup)
  579. goto error;
  580. intr_remapping_enabled = 1;
  581. return 0;
  582. error:
  583. /*
  584. * handle error condition gracefully here!
  585. */
  586. return -1;
  587. }
  588. static void ir_parse_one_hpet_scope(struct acpi_dmar_device_scope *scope,
  589. struct intel_iommu *iommu)
  590. {
  591. struct acpi_dmar_pci_path *path;
  592. u8 bus;
  593. int count;
  594. bus = scope->bus;
  595. path = (struct acpi_dmar_pci_path *)(scope + 1);
  596. count = (scope->length - sizeof(struct acpi_dmar_device_scope))
  597. / sizeof(struct acpi_dmar_pci_path);
  598. while (--count > 0) {
  599. /*
  600. * Access PCI directly due to the PCI
  601. * subsystem isn't initialized yet.
  602. */
  603. bus = read_pci_config_byte(bus, path->dev, path->fn,
  604. PCI_SECONDARY_BUS);
  605. path++;
  606. }
  607. ir_hpet[ir_hpet_num].bus = bus;
  608. ir_hpet[ir_hpet_num].devfn = PCI_DEVFN(path->dev, path->fn);
  609. ir_hpet[ir_hpet_num].iommu = iommu;
  610. ir_hpet[ir_hpet_num].id = scope->enumeration_id;
  611. ir_hpet_num++;
  612. }
  613. static void ir_parse_one_ioapic_scope(struct acpi_dmar_device_scope *scope,
  614. struct intel_iommu *iommu)
  615. {
  616. struct acpi_dmar_pci_path *path;
  617. u8 bus;
  618. int count;
  619. bus = scope->bus;
  620. path = (struct acpi_dmar_pci_path *)(scope + 1);
  621. count = (scope->length - sizeof(struct acpi_dmar_device_scope))
  622. / sizeof(struct acpi_dmar_pci_path);
  623. while (--count > 0) {
  624. /*
  625. * Access PCI directly due to the PCI
  626. * subsystem isn't initialized yet.
  627. */
  628. bus = read_pci_config_byte(bus, path->dev, path->fn,
  629. PCI_SECONDARY_BUS);
  630. path++;
  631. }
  632. ir_ioapic[ir_ioapic_num].bus = bus;
  633. ir_ioapic[ir_ioapic_num].devfn = PCI_DEVFN(path->dev, path->fn);
  634. ir_ioapic[ir_ioapic_num].iommu = iommu;
  635. ir_ioapic[ir_ioapic_num].id = scope->enumeration_id;
  636. ir_ioapic_num++;
  637. }
  638. static int ir_parse_ioapic_hpet_scope(struct acpi_dmar_header *header,
  639. struct intel_iommu *iommu)
  640. {
  641. struct acpi_dmar_hardware_unit *drhd;
  642. struct acpi_dmar_device_scope *scope;
  643. void *start, *end;
  644. drhd = (struct acpi_dmar_hardware_unit *)header;
  645. start = (void *)(drhd + 1);
  646. end = ((void *)drhd) + header->length;
  647. while (start < end) {
  648. scope = start;
  649. if (scope->entry_type == ACPI_DMAR_SCOPE_TYPE_IOAPIC) {
  650. if (ir_ioapic_num == MAX_IO_APICS) {
  651. printk(KERN_WARNING "Exceeded Max IO APICS\n");
  652. return -1;
  653. }
  654. printk(KERN_INFO "IOAPIC id %d under DRHD base"
  655. " 0x%Lx\n", scope->enumeration_id,
  656. drhd->address);
  657. ir_parse_one_ioapic_scope(scope, iommu);
  658. } else if (scope->entry_type == ACPI_DMAR_SCOPE_TYPE_HPET) {
  659. if (ir_hpet_num == MAX_HPET_TBS) {
  660. printk(KERN_WARNING "Exceeded Max HPET blocks\n");
  661. return -1;
  662. }
  663. printk(KERN_INFO "HPET id %d under DRHD base"
  664. " 0x%Lx\n", scope->enumeration_id,
  665. drhd->address);
  666. ir_parse_one_hpet_scope(scope, iommu);
  667. }
  668. start += scope->length;
  669. }
  670. return 0;
  671. }
  672. /*
  673. * Finds the assocaition between IOAPIC's and its Interrupt-remapping
  674. * hardware unit.
  675. */
  676. int __init parse_ioapics_under_ir(void)
  677. {
  678. struct dmar_drhd_unit *drhd;
  679. int ir_supported = 0;
  680. for_each_drhd_unit(drhd) {
  681. struct intel_iommu *iommu = drhd->iommu;
  682. if (ecap_ir_support(iommu->ecap)) {
  683. if (ir_parse_ioapic_hpet_scope(drhd->hdr, iommu))
  684. return -1;
  685. ir_supported = 1;
  686. }
  687. }
  688. if (ir_supported && ir_ioapic_num != nr_ioapics) {
  689. printk(KERN_WARNING
  690. "Not all IO-APIC's listed under remapping hardware\n");
  691. return -1;
  692. }
  693. return ir_supported;
  694. }
  695. void disable_intr_remapping(void)
  696. {
  697. struct dmar_drhd_unit *drhd;
  698. struct intel_iommu *iommu = NULL;
  699. /*
  700. * Disable Interrupt-remapping for all the DRHD's now.
  701. */
  702. for_each_iommu(iommu, drhd) {
  703. if (!ecap_ir_support(iommu->ecap))
  704. continue;
  705. iommu_disable_intr_remapping(iommu);
  706. }
  707. }
  708. int reenable_intr_remapping(int eim)
  709. {
  710. struct dmar_drhd_unit *drhd;
  711. int setup = 0;
  712. struct intel_iommu *iommu = NULL;
  713. for_each_iommu(iommu, drhd)
  714. if (iommu->qi)
  715. dmar_reenable_qi(iommu);
  716. /*
  717. * Setup Interrupt-remapping for all the DRHD's now.
  718. */
  719. for_each_iommu(iommu, drhd) {
  720. if (!ecap_ir_support(iommu->ecap))
  721. continue;
  722. /* Set up interrupt remapping for iommu.*/
  723. iommu_set_intr_remapping(iommu, eim);
  724. setup = 1;
  725. }
  726. if (!setup)
  727. goto error;
  728. return 0;
  729. error:
  730. /*
  731. * handle error condition gracefully here!
  732. */
  733. return -1;
  734. }