intr_remapping.c 19 KB

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