intr_remapping.c 12 KB

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