intel_irq_remapping.c 24 KB

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