intr_remapping.c 15 KB

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