intr_remapping.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718
  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. static struct ioapic_scope ir_ioapic[MAX_IO_APICS];
  14. static int ir_ioapic_num;
  15. int intr_remapping_enabled;
  16. struct irq_2_iommu {
  17. struct intel_iommu *iommu;
  18. u16 irte_index;
  19. u16 sub_handle;
  20. u8 irte_mask;
  21. };
  22. #ifdef CONFIG_GENERIC_HARDIRQS
  23. static struct irq_2_iommu *get_one_free_irq_2_iommu(int node)
  24. {
  25. struct irq_2_iommu *iommu;
  26. iommu = kzalloc_node(sizeof(*iommu), GFP_ATOMIC, node);
  27. printk(KERN_DEBUG "alloc irq_2_iommu on node %d\n", node);
  28. return iommu;
  29. }
  30. static struct irq_2_iommu *irq_2_iommu(unsigned int irq)
  31. {
  32. struct irq_desc *desc;
  33. desc = irq_to_desc(irq);
  34. if (WARN_ON_ONCE(!desc))
  35. return NULL;
  36. return desc->irq_2_iommu;
  37. }
  38. static struct irq_2_iommu *irq_2_iommu_alloc_node(unsigned int irq, int node)
  39. {
  40. struct irq_desc *desc;
  41. struct irq_2_iommu *irq_iommu;
  42. /*
  43. * alloc irq desc if not allocated already.
  44. */
  45. desc = irq_to_desc_alloc_node(irq, node);
  46. if (!desc) {
  47. printk(KERN_INFO "can not get irq_desc for %d\n", irq);
  48. return NULL;
  49. }
  50. irq_iommu = desc->irq_2_iommu;
  51. if (!irq_iommu)
  52. desc->irq_2_iommu = get_one_free_irq_2_iommu(node);
  53. return desc->irq_2_iommu;
  54. }
  55. static struct irq_2_iommu *irq_2_iommu_alloc(unsigned int irq)
  56. {
  57. return irq_2_iommu_alloc_node(irq, cpu_to_node(boot_cpu_id));
  58. }
  59. #else /* !CONFIG_SPARSE_IRQ */
  60. static struct irq_2_iommu irq_2_iommuX[NR_IRQS];
  61. static struct irq_2_iommu *irq_2_iommu(unsigned int irq)
  62. {
  63. if (irq < nr_irqs)
  64. return &irq_2_iommuX[irq];
  65. return NULL;
  66. }
  67. static struct irq_2_iommu *irq_2_iommu_alloc(unsigned int irq)
  68. {
  69. return irq_2_iommu(irq);
  70. }
  71. #endif
  72. static DEFINE_SPINLOCK(irq_2_ir_lock);
  73. static struct irq_2_iommu *valid_irq_2_iommu(unsigned int irq)
  74. {
  75. struct irq_2_iommu *irq_iommu;
  76. irq_iommu = irq_2_iommu(irq);
  77. if (!irq_iommu)
  78. return NULL;
  79. if (!irq_iommu->iommu)
  80. return NULL;
  81. return irq_iommu;
  82. }
  83. int irq_remapped(int irq)
  84. {
  85. return valid_irq_2_iommu(irq) != NULL;
  86. }
  87. int get_irte(int irq, struct irte *entry)
  88. {
  89. int index;
  90. struct irq_2_iommu *irq_iommu;
  91. unsigned long flags;
  92. if (!entry)
  93. return -1;
  94. spin_lock_irqsave(&irq_2_ir_lock, flags);
  95. irq_iommu = valid_irq_2_iommu(irq);
  96. if (!irq_iommu) {
  97. spin_unlock_irqrestore(&irq_2_ir_lock, flags);
  98. return -1;
  99. }
  100. index = irq_iommu->irte_index + irq_iommu->sub_handle;
  101. *entry = *(irq_iommu->iommu->ir_table->base + index);
  102. spin_unlock_irqrestore(&irq_2_ir_lock, flags);
  103. return 0;
  104. }
  105. int alloc_irte(struct intel_iommu *iommu, int irq, u16 count)
  106. {
  107. struct ir_table *table = iommu->ir_table;
  108. struct irq_2_iommu *irq_iommu;
  109. u16 index, start_index;
  110. unsigned int mask = 0;
  111. unsigned long flags;
  112. int i;
  113. if (!count)
  114. return -1;
  115. #ifndef CONFIG_SPARSE_IRQ
  116. /* protect irq_2_iommu_alloc later */
  117. if (irq >= nr_irqs)
  118. return -1;
  119. #endif
  120. /*
  121. * start the IRTE search from index 0.
  122. */
  123. index = start_index = 0;
  124. if (count > 1) {
  125. count = __roundup_pow_of_two(count);
  126. mask = ilog2(count);
  127. }
  128. if (mask > ecap_max_handle_mask(iommu->ecap)) {
  129. printk(KERN_ERR
  130. "Requested mask %x exceeds the max invalidation handle"
  131. " mask value %Lx\n", mask,
  132. ecap_max_handle_mask(iommu->ecap));
  133. return -1;
  134. }
  135. spin_lock_irqsave(&irq_2_ir_lock, flags);
  136. do {
  137. for (i = index; i < index + count; i++)
  138. if (table->base[i].present)
  139. break;
  140. /* empty index found */
  141. if (i == index + count)
  142. break;
  143. index = (index + count) % INTR_REMAP_TABLE_ENTRIES;
  144. if (index == start_index) {
  145. spin_unlock_irqrestore(&irq_2_ir_lock, flags);
  146. printk(KERN_ERR "can't allocate an IRTE\n");
  147. return -1;
  148. }
  149. } while (1);
  150. for (i = index; i < index + count; i++)
  151. table->base[i].present = 1;
  152. irq_iommu = irq_2_iommu_alloc(irq);
  153. if (!irq_iommu) {
  154. spin_unlock_irqrestore(&irq_2_ir_lock, flags);
  155. printk(KERN_ERR "can't allocate irq_2_iommu\n");
  156. return -1;
  157. }
  158. irq_iommu->iommu = iommu;
  159. irq_iommu->irte_index = index;
  160. irq_iommu->sub_handle = 0;
  161. irq_iommu->irte_mask = mask;
  162. spin_unlock_irqrestore(&irq_2_ir_lock, flags);
  163. return index;
  164. }
  165. static int qi_flush_iec(struct intel_iommu *iommu, int index, int mask)
  166. {
  167. struct qi_desc desc;
  168. desc.low = QI_IEC_IIDEX(index) | QI_IEC_TYPE | QI_IEC_IM(mask)
  169. | QI_IEC_SELECTIVE;
  170. desc.high = 0;
  171. return qi_submit_sync(&desc, iommu);
  172. }
  173. int map_irq_to_irte_handle(int irq, u16 *sub_handle)
  174. {
  175. int index;
  176. struct irq_2_iommu *irq_iommu;
  177. unsigned long flags;
  178. spin_lock_irqsave(&irq_2_ir_lock, flags);
  179. irq_iommu = valid_irq_2_iommu(irq);
  180. if (!irq_iommu) {
  181. spin_unlock_irqrestore(&irq_2_ir_lock, flags);
  182. return -1;
  183. }
  184. *sub_handle = irq_iommu->sub_handle;
  185. index = irq_iommu->irte_index;
  186. spin_unlock_irqrestore(&irq_2_ir_lock, flags);
  187. return index;
  188. }
  189. int set_irte_irq(int irq, struct intel_iommu *iommu, u16 index, u16 subhandle)
  190. {
  191. struct irq_2_iommu *irq_iommu;
  192. unsigned long flags;
  193. spin_lock_irqsave(&irq_2_ir_lock, flags);
  194. irq_iommu = irq_2_iommu_alloc(irq);
  195. if (!irq_iommu) {
  196. spin_unlock_irqrestore(&irq_2_ir_lock, flags);
  197. printk(KERN_ERR "can't allocate irq_2_iommu\n");
  198. return -1;
  199. }
  200. irq_iommu->iommu = iommu;
  201. irq_iommu->irte_index = index;
  202. irq_iommu->sub_handle = subhandle;
  203. irq_iommu->irte_mask = 0;
  204. spin_unlock_irqrestore(&irq_2_ir_lock, flags);
  205. return 0;
  206. }
  207. int clear_irte_irq(int irq, struct intel_iommu *iommu, u16 index)
  208. {
  209. struct irq_2_iommu *irq_iommu;
  210. unsigned long flags;
  211. spin_lock_irqsave(&irq_2_ir_lock, flags);
  212. irq_iommu = valid_irq_2_iommu(irq);
  213. if (!irq_iommu) {
  214. spin_unlock_irqrestore(&irq_2_ir_lock, flags);
  215. return -1;
  216. }
  217. irq_iommu->iommu = NULL;
  218. irq_iommu->irte_index = 0;
  219. irq_iommu->sub_handle = 0;
  220. irq_2_iommu(irq)->irte_mask = 0;
  221. spin_unlock_irqrestore(&irq_2_ir_lock, flags);
  222. return 0;
  223. }
  224. int modify_irte(int irq, struct irte *irte_modified)
  225. {
  226. int rc;
  227. int index;
  228. struct irte *irte;
  229. struct intel_iommu *iommu;
  230. struct irq_2_iommu *irq_iommu;
  231. unsigned long flags;
  232. spin_lock_irqsave(&irq_2_ir_lock, flags);
  233. irq_iommu = valid_irq_2_iommu(irq);
  234. if (!irq_iommu) {
  235. spin_unlock_irqrestore(&irq_2_ir_lock, flags);
  236. return -1;
  237. }
  238. iommu = irq_iommu->iommu;
  239. index = irq_iommu->irte_index + irq_iommu->sub_handle;
  240. irte = &iommu->ir_table->base[index];
  241. set_64bit((unsigned long *)irte, irte_modified->low);
  242. __iommu_flush_cache(iommu, irte, sizeof(*irte));
  243. rc = qi_flush_iec(iommu, index, 0);
  244. spin_unlock_irqrestore(&irq_2_ir_lock, flags);
  245. return rc;
  246. }
  247. int flush_irte(int irq)
  248. {
  249. int rc;
  250. int index;
  251. struct intel_iommu *iommu;
  252. struct irq_2_iommu *irq_iommu;
  253. unsigned long flags;
  254. spin_lock_irqsave(&irq_2_ir_lock, flags);
  255. irq_iommu = valid_irq_2_iommu(irq);
  256. if (!irq_iommu) {
  257. spin_unlock_irqrestore(&irq_2_ir_lock, flags);
  258. return -1;
  259. }
  260. iommu = irq_iommu->iommu;
  261. index = irq_iommu->irte_index + irq_iommu->sub_handle;
  262. rc = qi_flush_iec(iommu, index, irq_iommu->irte_mask);
  263. spin_unlock_irqrestore(&irq_2_ir_lock, flags);
  264. return rc;
  265. }
  266. struct intel_iommu *map_ioapic_to_ir(int apic)
  267. {
  268. int i;
  269. for (i = 0; i < MAX_IO_APICS; i++)
  270. if (ir_ioapic[i].id == apic)
  271. return ir_ioapic[i].iommu;
  272. return NULL;
  273. }
  274. struct intel_iommu *map_dev_to_ir(struct pci_dev *dev)
  275. {
  276. struct dmar_drhd_unit *drhd;
  277. drhd = dmar_find_matched_drhd_unit(dev);
  278. if (!drhd)
  279. return NULL;
  280. return drhd->iommu;
  281. }
  282. int free_irte(int irq)
  283. {
  284. int rc = 0;
  285. int index, i;
  286. struct irte *irte;
  287. struct intel_iommu *iommu;
  288. struct irq_2_iommu *irq_iommu;
  289. unsigned long flags;
  290. spin_lock_irqsave(&irq_2_ir_lock, flags);
  291. irq_iommu = valid_irq_2_iommu(irq);
  292. if (!irq_iommu) {
  293. spin_unlock_irqrestore(&irq_2_ir_lock, flags);
  294. return -1;
  295. }
  296. iommu = irq_iommu->iommu;
  297. index = irq_iommu->irte_index + irq_iommu->sub_handle;
  298. irte = &iommu->ir_table->base[index];
  299. if (!irq_iommu->sub_handle) {
  300. for (i = 0; i < (1 << irq_iommu->irte_mask); i++)
  301. set_64bit((unsigned long *)(irte + i), 0);
  302. rc = qi_flush_iec(iommu, index, irq_iommu->irte_mask);
  303. }
  304. irq_iommu->iommu = NULL;
  305. irq_iommu->irte_index = 0;
  306. irq_iommu->sub_handle = 0;
  307. irq_iommu->irte_mask = 0;
  308. spin_unlock_irqrestore(&irq_2_ir_lock, flags);
  309. return rc;
  310. }
  311. static void iommu_set_intr_remapping(struct intel_iommu *iommu, int mode)
  312. {
  313. u64 addr;
  314. u32 cmd, sts;
  315. unsigned long flags;
  316. addr = virt_to_phys((void *)iommu->ir_table->base);
  317. spin_lock_irqsave(&iommu->register_lock, flags);
  318. dmar_writeq(iommu->reg + DMAR_IRTA_REG,
  319. (addr) | IR_X2APIC_MODE(mode) | INTR_REMAP_TABLE_REG_SIZE);
  320. /* Set interrupt-remapping table pointer */
  321. cmd = iommu->gcmd | DMA_GCMD_SIRTP;
  322. iommu->gcmd |= DMA_GCMD_SIRTP;
  323. writel(cmd, iommu->reg + DMAR_GCMD_REG);
  324. IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
  325. readl, (sts & DMA_GSTS_IRTPS), sts);
  326. spin_unlock_irqrestore(&iommu->register_lock, flags);
  327. if (mode == 0) {
  328. spin_lock_irqsave(&iommu->register_lock, flags);
  329. /* enable comaptiblity format interrupt pass through */
  330. cmd = iommu->gcmd | DMA_GCMD_CFI;
  331. iommu->gcmd |= DMA_GCMD_CFI;
  332. writel(cmd, iommu->reg + DMAR_GCMD_REG);
  333. IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
  334. readl, (sts & DMA_GSTS_CFIS), sts);
  335. spin_unlock_irqrestore(&iommu->register_lock, flags);
  336. }
  337. /*
  338. * global invalidation of interrupt entry cache before enabling
  339. * interrupt-remapping.
  340. */
  341. qi_global_iec(iommu);
  342. spin_lock_irqsave(&iommu->register_lock, flags);
  343. /* Enable interrupt-remapping */
  344. cmd = iommu->gcmd | DMA_GCMD_IRE;
  345. iommu->gcmd |= DMA_GCMD_IRE;
  346. writel(cmd, iommu->reg + DMAR_GCMD_REG);
  347. IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
  348. readl, (sts & DMA_GSTS_IRES), sts);
  349. spin_unlock_irqrestore(&iommu->register_lock, flags);
  350. }
  351. static int setup_intr_remapping(struct intel_iommu *iommu, int mode)
  352. {
  353. struct ir_table *ir_table;
  354. struct page *pages;
  355. ir_table = iommu->ir_table = kzalloc(sizeof(struct ir_table),
  356. GFP_ATOMIC);
  357. if (!iommu->ir_table)
  358. return -ENOMEM;
  359. pages = alloc_pages(GFP_ATOMIC | __GFP_ZERO, INTR_REMAP_PAGE_ORDER);
  360. if (!pages) {
  361. printk(KERN_ERR "failed to allocate pages of order %d\n",
  362. INTR_REMAP_PAGE_ORDER);
  363. kfree(iommu->ir_table);
  364. return -ENOMEM;
  365. }
  366. ir_table->base = page_address(pages);
  367. iommu_set_intr_remapping(iommu, mode);
  368. return 0;
  369. }
  370. /*
  371. * Disable Interrupt Remapping.
  372. */
  373. static void iommu_disable_intr_remapping(struct intel_iommu *iommu)
  374. {
  375. unsigned long flags;
  376. u32 sts;
  377. if (!ecap_ir_support(iommu->ecap))
  378. return;
  379. /*
  380. * global invalidation of interrupt entry cache before disabling
  381. * interrupt-remapping.
  382. */
  383. qi_global_iec(iommu);
  384. spin_lock_irqsave(&iommu->register_lock, flags);
  385. sts = dmar_readq(iommu->reg + DMAR_GSTS_REG);
  386. if (!(sts & DMA_GSTS_IRES))
  387. goto end;
  388. iommu->gcmd &= ~DMA_GCMD_IRE;
  389. writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
  390. IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
  391. readl, !(sts & DMA_GSTS_IRES), sts);
  392. end:
  393. spin_unlock_irqrestore(&iommu->register_lock, flags);
  394. }
  395. int __init enable_intr_remapping(int eim)
  396. {
  397. struct dmar_drhd_unit *drhd;
  398. int setup = 0;
  399. for_each_drhd_unit(drhd) {
  400. struct intel_iommu *iommu = drhd->iommu;
  401. /*
  402. * If the queued invalidation is already initialized,
  403. * shouldn't disable it.
  404. */
  405. if (iommu->qi)
  406. continue;
  407. /*
  408. * Clear previous faults.
  409. */
  410. dmar_fault(-1, iommu);
  411. /*
  412. * Disable intr remapping and queued invalidation, if already
  413. * enabled prior to OS handover.
  414. */
  415. iommu_disable_intr_remapping(iommu);
  416. dmar_disable_qi(iommu);
  417. }
  418. /*
  419. * check for the Interrupt-remapping support
  420. */
  421. for_each_drhd_unit(drhd) {
  422. struct intel_iommu *iommu = drhd->iommu;
  423. if (!ecap_ir_support(iommu->ecap))
  424. continue;
  425. if (eim && !ecap_eim_support(iommu->ecap)) {
  426. printk(KERN_INFO "DRHD %Lx: EIM not supported by DRHD, "
  427. " ecap %Lx\n", drhd->reg_base_addr, iommu->ecap);
  428. return -1;
  429. }
  430. }
  431. /*
  432. * Enable queued invalidation for all the DRHD's.
  433. */
  434. for_each_drhd_unit(drhd) {
  435. int ret;
  436. struct intel_iommu *iommu = drhd->iommu;
  437. ret = dmar_enable_qi(iommu);
  438. if (ret) {
  439. printk(KERN_ERR "DRHD %Lx: failed to enable queued, "
  440. " invalidation, ecap %Lx, ret %d\n",
  441. drhd->reg_base_addr, iommu->ecap, ret);
  442. return -1;
  443. }
  444. }
  445. /*
  446. * Setup Interrupt-remapping for all the DRHD's now.
  447. */
  448. for_each_drhd_unit(drhd) {
  449. struct intel_iommu *iommu = drhd->iommu;
  450. if (!ecap_ir_support(iommu->ecap))
  451. continue;
  452. if (setup_intr_remapping(iommu, eim))
  453. goto error;
  454. setup = 1;
  455. }
  456. if (!setup)
  457. goto error;
  458. intr_remapping_enabled = 1;
  459. return 0;
  460. error:
  461. /*
  462. * handle error condition gracefully here!
  463. */
  464. return -1;
  465. }
  466. static int ir_parse_ioapic_scope(struct acpi_dmar_header *header,
  467. struct intel_iommu *iommu)
  468. {
  469. struct acpi_dmar_hardware_unit *drhd;
  470. struct acpi_dmar_device_scope *scope;
  471. void *start, *end;
  472. drhd = (struct acpi_dmar_hardware_unit *)header;
  473. start = (void *)(drhd + 1);
  474. end = ((void *)drhd) + header->length;
  475. while (start < end) {
  476. scope = start;
  477. if (scope->entry_type == ACPI_DMAR_SCOPE_TYPE_IOAPIC) {
  478. if (ir_ioapic_num == MAX_IO_APICS) {
  479. printk(KERN_WARNING "Exceeded Max IO APICS\n");
  480. return -1;
  481. }
  482. printk(KERN_INFO "IOAPIC id %d under DRHD base"
  483. " 0x%Lx\n", scope->enumeration_id,
  484. drhd->address);
  485. ir_ioapic[ir_ioapic_num].iommu = iommu;
  486. ir_ioapic[ir_ioapic_num].id = scope->enumeration_id;
  487. ir_ioapic_num++;
  488. }
  489. start += scope->length;
  490. }
  491. return 0;
  492. }
  493. /*
  494. * Finds the assocaition between IOAPIC's and its Interrupt-remapping
  495. * hardware unit.
  496. */
  497. int __init parse_ioapics_under_ir(void)
  498. {
  499. struct dmar_drhd_unit *drhd;
  500. int ir_supported = 0;
  501. for_each_drhd_unit(drhd) {
  502. struct intel_iommu *iommu = drhd->iommu;
  503. if (ecap_ir_support(iommu->ecap)) {
  504. if (ir_parse_ioapic_scope(drhd->hdr, iommu))
  505. return -1;
  506. ir_supported = 1;
  507. }
  508. }
  509. if (ir_supported && ir_ioapic_num != nr_ioapics) {
  510. printk(KERN_WARNING
  511. "Not all IO-APIC's listed under remapping hardware\n");
  512. return -1;
  513. }
  514. return ir_supported;
  515. }
  516. void disable_intr_remapping(void)
  517. {
  518. struct dmar_drhd_unit *drhd;
  519. struct intel_iommu *iommu = NULL;
  520. /*
  521. * Disable Interrupt-remapping for all the DRHD's now.
  522. */
  523. for_each_iommu(iommu, drhd) {
  524. if (!ecap_ir_support(iommu->ecap))
  525. continue;
  526. iommu_disable_intr_remapping(iommu);
  527. }
  528. }
  529. int reenable_intr_remapping(int eim)
  530. {
  531. struct dmar_drhd_unit *drhd;
  532. int setup = 0;
  533. struct intel_iommu *iommu = NULL;
  534. for_each_iommu(iommu, drhd)
  535. if (iommu->qi)
  536. dmar_reenable_qi(iommu);
  537. /*
  538. * Setup Interrupt-remapping for all the DRHD's now.
  539. */
  540. for_each_iommu(iommu, drhd) {
  541. if (!ecap_ir_support(iommu->ecap))
  542. continue;
  543. /* Set up interrupt remapping for iommu.*/
  544. iommu_set_intr_remapping(iommu, eim);
  545. setup = 1;
  546. }
  547. if (!setup)
  548. goto error;
  549. return 0;
  550. error:
  551. /*
  552. * handle error condition gracefully here!
  553. */
  554. return -1;
  555. }