intr_remapping.c 13 KB

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