intel_irq_remapping.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095
  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 <acpi/acpi.h>
  14. #include <asm/irq_remapping.h>
  15. #include <asm/pci-direct.h>
  16. #include <asm/msidef.h>
  17. #include "irq_remapping.h"
  18. struct ioapic_scope {
  19. struct intel_iommu *iommu;
  20. unsigned int id;
  21. unsigned int bus; /* PCI bus number */
  22. unsigned int devfn; /* PCI devfn number */
  23. };
  24. struct hpet_scope {
  25. struct intel_iommu *iommu;
  26. u8 id;
  27. unsigned int bus;
  28. unsigned int devfn;
  29. };
  30. #define IR_X2APIC_MODE(mode) (mode ? (1 << 11) : 0)
  31. #define IRTE_DEST(dest) ((x2apic_mode) ? dest : dest << 8)
  32. static struct ioapic_scope ir_ioapic[MAX_IO_APICS];
  33. static struct hpet_scope ir_hpet[MAX_HPET_TBS];
  34. static int ir_ioapic_num, ir_hpet_num;
  35. static DEFINE_RAW_SPINLOCK(irq_2_ir_lock);
  36. static struct irq_2_iommu *irq_2_iommu(unsigned int irq)
  37. {
  38. struct irq_cfg *cfg = irq_get_chip_data(irq);
  39. return cfg ? &cfg->irq_2_iommu : NULL;
  40. }
  41. int get_irte(int irq, struct irte *entry)
  42. {
  43. struct irq_2_iommu *irq_iommu = irq_2_iommu(irq);
  44. unsigned long flags;
  45. int index;
  46. if (!entry || !irq_iommu)
  47. return -1;
  48. raw_spin_lock_irqsave(&irq_2_ir_lock, flags);
  49. index = irq_iommu->irte_index + irq_iommu->sub_handle;
  50. *entry = *(irq_iommu->iommu->ir_table->base + index);
  51. raw_spin_unlock_irqrestore(&irq_2_ir_lock, flags);
  52. return 0;
  53. }
  54. static int alloc_irte(struct intel_iommu *iommu, int irq, u16 count)
  55. {
  56. struct ir_table *table = iommu->ir_table;
  57. struct irq_2_iommu *irq_iommu = irq_2_iommu(irq);
  58. struct irq_cfg *cfg = irq_get_chip_data(irq);
  59. u16 index, start_index;
  60. unsigned int mask = 0;
  61. unsigned long flags;
  62. int i;
  63. if (!count || !irq_iommu)
  64. return -1;
  65. /*
  66. * start the IRTE search from index 0.
  67. */
  68. index = start_index = 0;
  69. if (count > 1) {
  70. count = __roundup_pow_of_two(count);
  71. mask = ilog2(count);
  72. }
  73. if (mask > ecap_max_handle_mask(iommu->ecap)) {
  74. printk(KERN_ERR
  75. "Requested mask %x exceeds the max invalidation handle"
  76. " mask value %Lx\n", mask,
  77. ecap_max_handle_mask(iommu->ecap));
  78. return -1;
  79. }
  80. raw_spin_lock_irqsave(&irq_2_ir_lock, flags);
  81. do {
  82. for (i = index; i < index + count; i++)
  83. if (table->base[i].present)
  84. break;
  85. /* empty index found */
  86. if (i == index + count)
  87. break;
  88. index = (index + count) % INTR_REMAP_TABLE_ENTRIES;
  89. if (index == start_index) {
  90. raw_spin_unlock_irqrestore(&irq_2_ir_lock, flags);
  91. printk(KERN_ERR "can't allocate an IRTE\n");
  92. return -1;
  93. }
  94. } while (1);
  95. for (i = index; i < index + count; i++)
  96. table->base[i].present = 1;
  97. cfg->remapped = 1;
  98. irq_iommu->iommu = iommu;
  99. irq_iommu->irte_index = index;
  100. irq_iommu->sub_handle = 0;
  101. irq_iommu->irte_mask = mask;
  102. raw_spin_unlock_irqrestore(&irq_2_ir_lock, flags);
  103. return index;
  104. }
  105. static int qi_flush_iec(struct intel_iommu *iommu, int index, int mask)
  106. {
  107. struct qi_desc desc;
  108. desc.low = QI_IEC_IIDEX(index) | QI_IEC_TYPE | QI_IEC_IM(mask)
  109. | QI_IEC_SELECTIVE;
  110. desc.high = 0;
  111. return qi_submit_sync(&desc, iommu);
  112. }
  113. static int map_irq_to_irte_handle(int irq, u16 *sub_handle)
  114. {
  115. struct irq_2_iommu *irq_iommu = irq_2_iommu(irq);
  116. unsigned long flags;
  117. int index;
  118. if (!irq_iommu)
  119. return -1;
  120. raw_spin_lock_irqsave(&irq_2_ir_lock, flags);
  121. *sub_handle = irq_iommu->sub_handle;
  122. index = irq_iommu->irte_index;
  123. raw_spin_unlock_irqrestore(&irq_2_ir_lock, flags);
  124. return index;
  125. }
  126. static int set_irte_irq(int irq, struct intel_iommu *iommu, u16 index, u16 subhandle)
  127. {
  128. struct irq_2_iommu *irq_iommu = irq_2_iommu(irq);
  129. struct irq_cfg *cfg = irq_get_chip_data(irq);
  130. unsigned long flags;
  131. if (!irq_iommu)
  132. return -1;
  133. raw_spin_lock_irqsave(&irq_2_ir_lock, flags);
  134. cfg->remapped = 1;
  135. irq_iommu->iommu = iommu;
  136. irq_iommu->irte_index = index;
  137. irq_iommu->sub_handle = subhandle;
  138. irq_iommu->irte_mask = 0;
  139. raw_spin_unlock_irqrestore(&irq_2_ir_lock, flags);
  140. return 0;
  141. }
  142. static int modify_irte(int irq, struct irte *irte_modified)
  143. {
  144. struct irq_2_iommu *irq_iommu = irq_2_iommu(irq);
  145. struct intel_iommu *iommu;
  146. unsigned long flags;
  147. struct irte *irte;
  148. int rc, index;
  149. if (!irq_iommu)
  150. return -1;
  151. raw_spin_lock_irqsave(&irq_2_ir_lock, flags);
  152. iommu = irq_iommu->iommu;
  153. index = irq_iommu->irte_index + irq_iommu->sub_handle;
  154. irte = &iommu->ir_table->base[index];
  155. set_64bit(&irte->low, irte_modified->low);
  156. set_64bit(&irte->high, irte_modified->high);
  157. __iommu_flush_cache(iommu, irte, sizeof(*irte));
  158. rc = qi_flush_iec(iommu, index, 0);
  159. raw_spin_unlock_irqrestore(&irq_2_ir_lock, flags);
  160. return rc;
  161. }
  162. static struct intel_iommu *map_hpet_to_ir(u8 hpet_id)
  163. {
  164. int i;
  165. for (i = 0; i < MAX_HPET_TBS; i++)
  166. if (ir_hpet[i].id == hpet_id)
  167. return ir_hpet[i].iommu;
  168. return NULL;
  169. }
  170. static struct intel_iommu *map_ioapic_to_ir(int apic)
  171. {
  172. int i;
  173. for (i = 0; i < MAX_IO_APICS; i++)
  174. if (ir_ioapic[i].id == apic)
  175. return ir_ioapic[i].iommu;
  176. return NULL;
  177. }
  178. static struct intel_iommu *map_dev_to_ir(struct pci_dev *dev)
  179. {
  180. struct dmar_drhd_unit *drhd;
  181. drhd = dmar_find_matched_drhd_unit(dev);
  182. if (!drhd)
  183. return NULL;
  184. return drhd->iommu;
  185. }
  186. static int clear_entries(struct irq_2_iommu *irq_iommu)
  187. {
  188. struct irte *start, *entry, *end;
  189. struct intel_iommu *iommu;
  190. int index;
  191. if (irq_iommu->sub_handle)
  192. return 0;
  193. iommu = irq_iommu->iommu;
  194. index = irq_iommu->irte_index + irq_iommu->sub_handle;
  195. start = iommu->ir_table->base + index;
  196. end = start + (1 << irq_iommu->irte_mask);
  197. for (entry = start; entry < end; entry++) {
  198. set_64bit(&entry->low, 0);
  199. set_64bit(&entry->high, 0);
  200. }
  201. return qi_flush_iec(iommu, index, irq_iommu->irte_mask);
  202. }
  203. static int free_irte(int irq)
  204. {
  205. struct irq_2_iommu *irq_iommu = irq_2_iommu(irq);
  206. unsigned long flags;
  207. int rc;
  208. if (!irq_iommu)
  209. return -1;
  210. raw_spin_lock_irqsave(&irq_2_ir_lock, flags);
  211. rc = clear_entries(irq_iommu);
  212. irq_iommu->iommu = NULL;
  213. irq_iommu->irte_index = 0;
  214. irq_iommu->sub_handle = 0;
  215. irq_iommu->irte_mask = 0;
  216. raw_spin_unlock_irqrestore(&irq_2_ir_lock, flags);
  217. return rc;
  218. }
  219. /*
  220. * source validation type
  221. */
  222. #define SVT_NO_VERIFY 0x0 /* no verification is required */
  223. #define SVT_VERIFY_SID_SQ 0x1 /* verify using SID and SQ fields */
  224. #define SVT_VERIFY_BUS 0x2 /* verify bus of request-id */
  225. /*
  226. * source-id qualifier
  227. */
  228. #define SQ_ALL_16 0x0 /* verify all 16 bits of request-id */
  229. #define SQ_13_IGNORE_1 0x1 /* verify most significant 13 bits, ignore
  230. * the third least significant bit
  231. */
  232. #define SQ_13_IGNORE_2 0x2 /* verify most significant 13 bits, ignore
  233. * the second and third least significant bits
  234. */
  235. #define SQ_13_IGNORE_3 0x3 /* verify most significant 13 bits, ignore
  236. * the least three significant bits
  237. */
  238. /*
  239. * set SVT, SQ and SID fields of irte to verify
  240. * source ids of interrupt requests
  241. */
  242. static void set_irte_sid(struct irte *irte, unsigned int svt,
  243. unsigned int sq, unsigned int sid)
  244. {
  245. if (disable_sourceid_checking)
  246. svt = SVT_NO_VERIFY;
  247. irte->svt = svt;
  248. irte->sq = sq;
  249. irte->sid = sid;
  250. }
  251. static int set_ioapic_sid(struct irte *irte, int apic)
  252. {
  253. int i;
  254. u16 sid = 0;
  255. if (!irte)
  256. return -1;
  257. for (i = 0; i < MAX_IO_APICS; i++) {
  258. if (ir_ioapic[i].id == apic) {
  259. sid = (ir_ioapic[i].bus << 8) | ir_ioapic[i].devfn;
  260. break;
  261. }
  262. }
  263. if (sid == 0) {
  264. pr_warning("Failed to set source-id of IOAPIC (%d)\n", apic);
  265. return -1;
  266. }
  267. set_irte_sid(irte, 1, 0, sid);
  268. return 0;
  269. }
  270. static int set_hpet_sid(struct irte *irte, u8 id)
  271. {
  272. int i;
  273. u16 sid = 0;
  274. if (!irte)
  275. return -1;
  276. for (i = 0; i < MAX_HPET_TBS; i++) {
  277. if (ir_hpet[i].id == id) {
  278. sid = (ir_hpet[i].bus << 8) | ir_hpet[i].devfn;
  279. break;
  280. }
  281. }
  282. if (sid == 0) {
  283. pr_warning("Failed to set source-id of HPET block (%d)\n", id);
  284. return -1;
  285. }
  286. /*
  287. * Should really use SQ_ALL_16. Some platforms are broken.
  288. * While we figure out the right quirks for these broken platforms, use
  289. * SQ_13_IGNORE_3 for now.
  290. */
  291. set_irte_sid(irte, SVT_VERIFY_SID_SQ, SQ_13_IGNORE_3, sid);
  292. return 0;
  293. }
  294. static int set_msi_sid(struct irte *irte, struct pci_dev *dev)
  295. {
  296. struct pci_dev *bridge;
  297. if (!irte || !dev)
  298. return -1;
  299. /* PCIe device or Root Complex integrated PCI device */
  300. if (pci_is_pcie(dev) || !dev->bus->parent) {
  301. set_irte_sid(irte, SVT_VERIFY_SID_SQ, SQ_ALL_16,
  302. (dev->bus->number << 8) | dev->devfn);
  303. return 0;
  304. }
  305. bridge = pci_find_upstream_pcie_bridge(dev);
  306. if (bridge) {
  307. if (pci_is_pcie(bridge))/* this is a PCIe-to-PCI/PCIX bridge */
  308. set_irte_sid(irte, SVT_VERIFY_BUS, SQ_ALL_16,
  309. (bridge->bus->number << 8) | dev->bus->number);
  310. else /* this is a legacy PCI bridge */
  311. set_irte_sid(irte, SVT_VERIFY_SID_SQ, SQ_ALL_16,
  312. (bridge->bus->number << 8) | bridge->devfn);
  313. }
  314. return 0;
  315. }
  316. static void iommu_set_irq_remapping(struct intel_iommu *iommu, int mode)
  317. {
  318. u64 addr;
  319. u32 sts;
  320. unsigned long flags;
  321. addr = virt_to_phys((void *)iommu->ir_table->base);
  322. raw_spin_lock_irqsave(&iommu->register_lock, flags);
  323. dmar_writeq(iommu->reg + DMAR_IRTA_REG,
  324. (addr) | IR_X2APIC_MODE(mode) | INTR_REMAP_TABLE_REG_SIZE);
  325. /* Set interrupt-remapping table pointer */
  326. iommu->gcmd |= DMA_GCMD_SIRTP;
  327. writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
  328. IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
  329. readl, (sts & DMA_GSTS_IRTPS), sts);
  330. raw_spin_unlock_irqrestore(&iommu->register_lock, flags);
  331. /*
  332. * global invalidation of interrupt entry cache before enabling
  333. * interrupt-remapping.
  334. */
  335. qi_global_iec(iommu);
  336. raw_spin_lock_irqsave(&iommu->register_lock, flags);
  337. /* Enable interrupt-remapping */
  338. iommu->gcmd |= DMA_GCMD_IRE;
  339. writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
  340. IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
  341. readl, (sts & DMA_GSTS_IRES), sts);
  342. raw_spin_unlock_irqrestore(&iommu->register_lock, flags);
  343. }
  344. static int intel_setup_irq_remapping(struct intel_iommu *iommu, int mode)
  345. {
  346. struct ir_table *ir_table;
  347. struct page *pages;
  348. ir_table = iommu->ir_table = kzalloc(sizeof(struct ir_table),
  349. GFP_ATOMIC);
  350. if (!iommu->ir_table)
  351. return -ENOMEM;
  352. pages = alloc_pages_node(iommu->node, GFP_ATOMIC | __GFP_ZERO,
  353. INTR_REMAP_PAGE_ORDER);
  354. if (!pages) {
  355. printk(KERN_ERR "failed to allocate pages of order %d\n",
  356. INTR_REMAP_PAGE_ORDER);
  357. kfree(iommu->ir_table);
  358. return -ENOMEM;
  359. }
  360. ir_table->base = page_address(pages);
  361. iommu_set_irq_remapping(iommu, mode);
  362. return 0;
  363. }
  364. /*
  365. * Disable Interrupt Remapping.
  366. */
  367. static void iommu_disable_irq_remapping(struct intel_iommu *iommu)
  368. {
  369. unsigned long flags;
  370. u32 sts;
  371. if (!ecap_ir_support(iommu->ecap))
  372. return;
  373. /*
  374. * global invalidation of interrupt entry cache before disabling
  375. * interrupt-remapping.
  376. */
  377. qi_global_iec(iommu);
  378. raw_spin_lock_irqsave(&iommu->register_lock, flags);
  379. sts = dmar_readq(iommu->reg + DMAR_GSTS_REG);
  380. if (!(sts & DMA_GSTS_IRES))
  381. goto end;
  382. iommu->gcmd &= ~DMA_GCMD_IRE;
  383. writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
  384. IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
  385. readl, !(sts & DMA_GSTS_IRES), sts);
  386. end:
  387. raw_spin_unlock_irqrestore(&iommu->register_lock, flags);
  388. }
  389. static int __init dmar_x2apic_optout(void)
  390. {
  391. struct acpi_table_dmar *dmar;
  392. dmar = (struct acpi_table_dmar *)dmar_tbl;
  393. if (!dmar || no_x2apic_optout)
  394. return 0;
  395. return dmar->flags & DMAR_X2APIC_OPT_OUT;
  396. }
  397. static int __init intel_irq_remapping_supported(void)
  398. {
  399. struct dmar_drhd_unit *drhd;
  400. if (disable_irq_remap)
  401. return 0;
  402. if (!dmar_ir_support())
  403. return 0;
  404. for_each_drhd_unit(drhd) {
  405. struct intel_iommu *iommu = drhd->iommu;
  406. if (!ecap_ir_support(iommu->ecap))
  407. return 0;
  408. }
  409. return 1;
  410. }
  411. static int __init intel_enable_irq_remapping(void)
  412. {
  413. struct dmar_drhd_unit *drhd;
  414. int setup = 0;
  415. int eim = 0;
  416. if (parse_ioapics_under_ir() != 1) {
  417. printk(KERN_INFO "Not enable interrupt remapping\n");
  418. return -1;
  419. }
  420. if (x2apic_supported()) {
  421. eim = !dmar_x2apic_optout();
  422. WARN(!eim, KERN_WARNING
  423. "Your BIOS is broken and requested that x2apic be disabled\n"
  424. "This will leave your machine vulnerable to irq-injection attacks\n"
  425. "Use 'intremap=no_x2apic_optout' to override BIOS request\n");
  426. }
  427. for_each_drhd_unit(drhd) {
  428. struct intel_iommu *iommu = drhd->iommu;
  429. /*
  430. * If the queued invalidation is already initialized,
  431. * shouldn't disable it.
  432. */
  433. if (iommu->qi)
  434. continue;
  435. /*
  436. * Clear previous faults.
  437. */
  438. dmar_fault(-1, iommu);
  439. /*
  440. * Disable intr remapping and queued invalidation, if already
  441. * enabled prior to OS handover.
  442. */
  443. iommu_disable_irq_remapping(iommu);
  444. dmar_disable_qi(iommu);
  445. }
  446. /*
  447. * check for the Interrupt-remapping support
  448. */
  449. for_each_drhd_unit(drhd) {
  450. struct intel_iommu *iommu = drhd->iommu;
  451. if (!ecap_ir_support(iommu->ecap))
  452. continue;
  453. if (eim && !ecap_eim_support(iommu->ecap)) {
  454. printk(KERN_INFO "DRHD %Lx: EIM not supported by DRHD, "
  455. " ecap %Lx\n", drhd->reg_base_addr, iommu->ecap);
  456. return -1;
  457. }
  458. }
  459. /*
  460. * Enable queued invalidation for all the DRHD's.
  461. */
  462. for_each_drhd_unit(drhd) {
  463. int ret;
  464. struct intel_iommu *iommu = drhd->iommu;
  465. ret = dmar_enable_qi(iommu);
  466. if (ret) {
  467. printk(KERN_ERR "DRHD %Lx: failed to enable queued, "
  468. " invalidation, ecap %Lx, ret %d\n",
  469. drhd->reg_base_addr, iommu->ecap, ret);
  470. return -1;
  471. }
  472. }
  473. /*
  474. * Setup Interrupt-remapping for all the DRHD's now.
  475. */
  476. for_each_drhd_unit(drhd) {
  477. struct intel_iommu *iommu = drhd->iommu;
  478. if (!ecap_ir_support(iommu->ecap))
  479. continue;
  480. if (intel_setup_irq_remapping(iommu, eim))
  481. goto error;
  482. setup = 1;
  483. }
  484. if (!setup)
  485. goto error;
  486. irq_remapping_enabled = 1;
  487. /*
  488. * VT-d has a different layout for IO-APIC entries when
  489. * interrupt remapping is enabled. So it needs a special routine
  490. * to print IO-APIC entries for debugging purposes too.
  491. */
  492. x86_io_apic_ops.print_entries = intel_ir_io_apic_print_entries;
  493. pr_info("Enabled IRQ remapping in %s mode\n", eim ? "x2apic" : "xapic");
  494. return eim ? IRQ_REMAP_X2APIC_MODE : IRQ_REMAP_XAPIC_MODE;
  495. error:
  496. /*
  497. * handle error condition gracefully here!
  498. */
  499. return -1;
  500. }
  501. static void ir_parse_one_hpet_scope(struct acpi_dmar_device_scope *scope,
  502. struct intel_iommu *iommu)
  503. {
  504. struct acpi_dmar_pci_path *path;
  505. u8 bus;
  506. int count;
  507. bus = scope->bus;
  508. path = (struct acpi_dmar_pci_path *)(scope + 1);
  509. count = (scope->length - sizeof(struct acpi_dmar_device_scope))
  510. / sizeof(struct acpi_dmar_pci_path);
  511. while (--count > 0) {
  512. /*
  513. * Access PCI directly due to the PCI
  514. * subsystem isn't initialized yet.
  515. */
  516. bus = read_pci_config_byte(bus, path->dev, path->fn,
  517. PCI_SECONDARY_BUS);
  518. path++;
  519. }
  520. ir_hpet[ir_hpet_num].bus = bus;
  521. ir_hpet[ir_hpet_num].devfn = PCI_DEVFN(path->dev, path->fn);
  522. ir_hpet[ir_hpet_num].iommu = iommu;
  523. ir_hpet[ir_hpet_num].id = scope->enumeration_id;
  524. ir_hpet_num++;
  525. }
  526. static void ir_parse_one_ioapic_scope(struct acpi_dmar_device_scope *scope,
  527. struct intel_iommu *iommu)
  528. {
  529. struct acpi_dmar_pci_path *path;
  530. u8 bus;
  531. int count;
  532. bus = scope->bus;
  533. path = (struct acpi_dmar_pci_path *)(scope + 1);
  534. count = (scope->length - sizeof(struct acpi_dmar_device_scope))
  535. / sizeof(struct acpi_dmar_pci_path);
  536. while (--count > 0) {
  537. /*
  538. * Access PCI directly due to the PCI
  539. * subsystem isn't initialized yet.
  540. */
  541. bus = read_pci_config_byte(bus, path->dev, path->fn,
  542. PCI_SECONDARY_BUS);
  543. path++;
  544. }
  545. ir_ioapic[ir_ioapic_num].bus = bus;
  546. ir_ioapic[ir_ioapic_num].devfn = PCI_DEVFN(path->dev, path->fn);
  547. ir_ioapic[ir_ioapic_num].iommu = iommu;
  548. ir_ioapic[ir_ioapic_num].id = scope->enumeration_id;
  549. ir_ioapic_num++;
  550. }
  551. static int ir_parse_ioapic_hpet_scope(struct acpi_dmar_header *header,
  552. struct intel_iommu *iommu)
  553. {
  554. struct acpi_dmar_hardware_unit *drhd;
  555. struct acpi_dmar_device_scope *scope;
  556. void *start, *end;
  557. drhd = (struct acpi_dmar_hardware_unit *)header;
  558. start = (void *)(drhd + 1);
  559. end = ((void *)drhd) + header->length;
  560. while (start < end) {
  561. scope = start;
  562. if (scope->entry_type == ACPI_DMAR_SCOPE_TYPE_IOAPIC) {
  563. if (ir_ioapic_num == MAX_IO_APICS) {
  564. printk(KERN_WARNING "Exceeded Max IO APICS\n");
  565. return -1;
  566. }
  567. printk(KERN_INFO "IOAPIC id %d under DRHD base "
  568. " 0x%Lx IOMMU %d\n", scope->enumeration_id,
  569. drhd->address, iommu->seq_id);
  570. ir_parse_one_ioapic_scope(scope, iommu);
  571. } else if (scope->entry_type == ACPI_DMAR_SCOPE_TYPE_HPET) {
  572. if (ir_hpet_num == MAX_HPET_TBS) {
  573. printk(KERN_WARNING "Exceeded Max HPET blocks\n");
  574. return -1;
  575. }
  576. printk(KERN_INFO "HPET id %d under DRHD base"
  577. " 0x%Lx\n", scope->enumeration_id,
  578. drhd->address);
  579. ir_parse_one_hpet_scope(scope, iommu);
  580. }
  581. start += scope->length;
  582. }
  583. return 0;
  584. }
  585. /*
  586. * Finds the assocaition between IOAPIC's and its Interrupt-remapping
  587. * hardware unit.
  588. */
  589. int __init parse_ioapics_under_ir(void)
  590. {
  591. struct dmar_drhd_unit *drhd;
  592. int ir_supported = 0;
  593. int ioapic_idx;
  594. for_each_drhd_unit(drhd) {
  595. struct intel_iommu *iommu = drhd->iommu;
  596. if (ecap_ir_support(iommu->ecap)) {
  597. if (ir_parse_ioapic_hpet_scope(drhd->hdr, iommu))
  598. return -1;
  599. ir_supported = 1;
  600. }
  601. }
  602. if (!ir_supported)
  603. return 0;
  604. for (ioapic_idx = 0; ioapic_idx < nr_ioapics; ioapic_idx++) {
  605. int ioapic_id = mpc_ioapic_id(ioapic_idx);
  606. if (!map_ioapic_to_ir(ioapic_id)) {
  607. pr_err(FW_BUG "ioapic %d has no mapping iommu, "
  608. "interrupt remapping will be disabled\n",
  609. ioapic_id);
  610. return -1;
  611. }
  612. }
  613. return 1;
  614. }
  615. int __init ir_dev_scope_init(void)
  616. {
  617. if (!irq_remapping_enabled)
  618. return 0;
  619. return dmar_dev_scope_init();
  620. }
  621. rootfs_initcall(ir_dev_scope_init);
  622. static void disable_irq_remapping(void)
  623. {
  624. struct dmar_drhd_unit *drhd;
  625. struct intel_iommu *iommu = NULL;
  626. /*
  627. * Disable Interrupt-remapping for all the DRHD's now.
  628. */
  629. for_each_iommu(iommu, drhd) {
  630. if (!ecap_ir_support(iommu->ecap))
  631. continue;
  632. iommu_disable_irq_remapping(iommu);
  633. }
  634. }
  635. static int reenable_irq_remapping(int eim)
  636. {
  637. struct dmar_drhd_unit *drhd;
  638. int setup = 0;
  639. struct intel_iommu *iommu = NULL;
  640. for_each_iommu(iommu, drhd)
  641. if (iommu->qi)
  642. dmar_reenable_qi(iommu);
  643. /*
  644. * Setup Interrupt-remapping for all the DRHD's now.
  645. */
  646. for_each_iommu(iommu, drhd) {
  647. if (!ecap_ir_support(iommu->ecap))
  648. continue;
  649. /* Set up interrupt remapping for iommu.*/
  650. iommu_set_irq_remapping(iommu, eim);
  651. setup = 1;
  652. }
  653. if (!setup)
  654. goto error;
  655. return 0;
  656. error:
  657. /*
  658. * handle error condition gracefully here!
  659. */
  660. return -1;
  661. }
  662. static void prepare_irte(struct irte *irte, int vector,
  663. unsigned int dest)
  664. {
  665. memset(irte, 0, sizeof(*irte));
  666. irte->present = 1;
  667. irte->dst_mode = apic->irq_dest_mode;
  668. /*
  669. * Trigger mode in the IRTE will always be edge, and for IO-APIC, the
  670. * actual level or edge trigger will be setup in the IO-APIC
  671. * RTE. This will help simplify level triggered irq migration.
  672. * For more details, see the comments (in io_apic.c) explainig IO-APIC
  673. * irq migration in the presence of interrupt-remapping.
  674. */
  675. irte->trigger_mode = 0;
  676. irte->dlvry_mode = apic->irq_delivery_mode;
  677. irte->vector = vector;
  678. irte->dest_id = IRTE_DEST(dest);
  679. irte->redir_hint = 1;
  680. }
  681. static int intel_setup_ioapic_entry(int irq,
  682. struct IO_APIC_route_entry *route_entry,
  683. unsigned int destination, int vector,
  684. struct io_apic_irq_attr *attr)
  685. {
  686. int ioapic_id = mpc_ioapic_id(attr->ioapic);
  687. struct intel_iommu *iommu = map_ioapic_to_ir(ioapic_id);
  688. struct IR_IO_APIC_route_entry *entry;
  689. struct irte irte;
  690. int index;
  691. if (!iommu) {
  692. pr_warn("No mapping iommu for ioapic %d\n", ioapic_id);
  693. return -ENODEV;
  694. }
  695. entry = (struct IR_IO_APIC_route_entry *)route_entry;
  696. index = alloc_irte(iommu, irq, 1);
  697. if (index < 0) {
  698. pr_warn("Failed to allocate IRTE for ioapic %d\n", ioapic_id);
  699. return -ENOMEM;
  700. }
  701. prepare_irte(&irte, vector, destination);
  702. /* Set source-id of interrupt request */
  703. set_ioapic_sid(&irte, ioapic_id);
  704. modify_irte(irq, &irte);
  705. apic_printk(APIC_VERBOSE, KERN_DEBUG "IOAPIC[%d]: "
  706. "Set IRTE entry (P:%d FPD:%d Dst_Mode:%d "
  707. "Redir_hint:%d Trig_Mode:%d Dlvry_Mode:%X "
  708. "Avail:%X Vector:%02X Dest:%08X "
  709. "SID:%04X SQ:%X SVT:%X)\n",
  710. attr->ioapic, irte.present, irte.fpd, irte.dst_mode,
  711. irte.redir_hint, irte.trigger_mode, irte.dlvry_mode,
  712. irte.avail, irte.vector, irte.dest_id,
  713. irte.sid, irte.sq, irte.svt);
  714. memset(entry, 0, sizeof(*entry));
  715. entry->index2 = (index >> 15) & 0x1;
  716. entry->zero = 0;
  717. entry->format = 1;
  718. entry->index = (index & 0x7fff);
  719. /*
  720. * IO-APIC RTE will be configured with virtual vector.
  721. * irq handler will do the explicit EOI to the io-apic.
  722. */
  723. entry->vector = attr->ioapic_pin;
  724. entry->mask = 0; /* enable IRQ */
  725. entry->trigger = attr->trigger;
  726. entry->polarity = attr->polarity;
  727. /* Mask level triggered irqs.
  728. * Use IRQ_DELAYED_DISABLE for edge triggered irqs.
  729. */
  730. if (attr->trigger)
  731. entry->mask = 1;
  732. return 0;
  733. }
  734. /*
  735. * Migrate the IO-APIC irq in the presence of intr-remapping.
  736. *
  737. * For both level and edge triggered, irq migration is a simple atomic
  738. * update(of vector and cpu destination) of IRTE and flush the hardware cache.
  739. *
  740. * For level triggered, we eliminate the io-apic RTE modification (with the
  741. * updated vector information), by using a virtual vector (io-apic pin number).
  742. * Real vector that is used for interrupting cpu will be coming from
  743. * the interrupt-remapping table entry.
  744. *
  745. * As the migration is a simple atomic update of IRTE, the same mechanism
  746. * is used to migrate MSI irq's in the presence of interrupt-remapping.
  747. */
  748. static int
  749. intel_ioapic_set_affinity(struct irq_data *data, const struct cpumask *mask,
  750. bool force)
  751. {
  752. struct irq_cfg *cfg = data->chip_data;
  753. unsigned int dest, irq = data->irq;
  754. struct irte irte;
  755. int err;
  756. if (!config_enabled(CONFIG_SMP))
  757. return -EINVAL;
  758. if (!cpumask_intersects(mask, cpu_online_mask))
  759. return -EINVAL;
  760. if (get_irte(irq, &irte))
  761. return -EBUSY;
  762. err = assign_irq_vector(irq, cfg, mask);
  763. if (err)
  764. return err;
  765. err = apic->cpu_mask_to_apicid_and(cfg->domain, mask, &dest);
  766. if (err) {
  767. if (assign_irq_vector(irq, cfg, data->affinity))
  768. pr_err("Failed to recover vector for irq %d\n", irq);
  769. return err;
  770. }
  771. irte.vector = cfg->vector;
  772. irte.dest_id = IRTE_DEST(dest);
  773. /*
  774. * Atomically updates the IRTE with the new destination, vector
  775. * and flushes the interrupt entry cache.
  776. */
  777. modify_irte(irq, &irte);
  778. /*
  779. * After this point, all the interrupts will start arriving
  780. * at the new destination. So, time to cleanup the previous
  781. * vector allocation.
  782. */
  783. if (cfg->move_in_progress)
  784. send_cleanup_vector(cfg);
  785. cpumask_copy(data->affinity, mask);
  786. return 0;
  787. }
  788. static void intel_compose_msi_msg(struct pci_dev *pdev,
  789. unsigned int irq, unsigned int dest,
  790. struct msi_msg *msg, u8 hpet_id)
  791. {
  792. struct irq_cfg *cfg;
  793. struct irte irte;
  794. u16 sub_handle = 0;
  795. int ir_index;
  796. cfg = irq_get_chip_data(irq);
  797. ir_index = map_irq_to_irte_handle(irq, &sub_handle);
  798. BUG_ON(ir_index == -1);
  799. prepare_irte(&irte, cfg->vector, dest);
  800. /* Set source-id of interrupt request */
  801. if (pdev)
  802. set_msi_sid(&irte, pdev);
  803. else
  804. set_hpet_sid(&irte, hpet_id);
  805. modify_irte(irq, &irte);
  806. msg->address_hi = MSI_ADDR_BASE_HI;
  807. msg->data = sub_handle;
  808. msg->address_lo = MSI_ADDR_BASE_LO | MSI_ADDR_IR_EXT_INT |
  809. MSI_ADDR_IR_SHV |
  810. MSI_ADDR_IR_INDEX1(ir_index) |
  811. MSI_ADDR_IR_INDEX2(ir_index);
  812. }
  813. /*
  814. * Map the PCI dev to the corresponding remapping hardware unit
  815. * and allocate 'nvec' consecutive interrupt-remapping table entries
  816. * in it.
  817. */
  818. static int intel_msi_alloc_irq(struct pci_dev *dev, int irq, int nvec)
  819. {
  820. struct intel_iommu *iommu;
  821. int index;
  822. iommu = map_dev_to_ir(dev);
  823. if (!iommu) {
  824. printk(KERN_ERR
  825. "Unable to map PCI %s to iommu\n", pci_name(dev));
  826. return -ENOENT;
  827. }
  828. index = alloc_irte(iommu, irq, nvec);
  829. if (index < 0) {
  830. printk(KERN_ERR
  831. "Unable to allocate %d IRTE for PCI %s\n", nvec,
  832. pci_name(dev));
  833. return -ENOSPC;
  834. }
  835. return index;
  836. }
  837. static int intel_msi_setup_irq(struct pci_dev *pdev, unsigned int irq,
  838. int index, int sub_handle)
  839. {
  840. struct intel_iommu *iommu;
  841. iommu = map_dev_to_ir(pdev);
  842. if (!iommu)
  843. return -ENOENT;
  844. /*
  845. * setup the mapping between the irq and the IRTE
  846. * base index, the sub_handle pointing to the
  847. * appropriate interrupt remap table entry.
  848. */
  849. set_irte_irq(irq, iommu, index, sub_handle);
  850. return 0;
  851. }
  852. static int intel_setup_hpet_msi(unsigned int irq, unsigned int id)
  853. {
  854. struct intel_iommu *iommu = map_hpet_to_ir(id);
  855. int index;
  856. if (!iommu)
  857. return -1;
  858. index = alloc_irte(iommu, irq, 1);
  859. if (index < 0)
  860. return -1;
  861. return 0;
  862. }
  863. struct irq_remap_ops intel_irq_remap_ops = {
  864. .supported = intel_irq_remapping_supported,
  865. .prepare = dmar_table_init,
  866. .enable = intel_enable_irq_remapping,
  867. .disable = disable_irq_remapping,
  868. .reenable = reenable_irq_remapping,
  869. .enable_faulting = enable_drhd_fault_handling,
  870. .setup_ioapic_entry = intel_setup_ioapic_entry,
  871. .set_affinity = intel_ioapic_set_affinity,
  872. .free_irq = free_irte,
  873. .compose_msi_msg = intel_compose_msi_msg,
  874. .msi_alloc_irq = intel_msi_alloc_irq,
  875. .msi_setup_irq = intel_msi_setup_irq,
  876. .setup_hpet_msi = intel_setup_hpet_msi,
  877. };