intr_remapping.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956
  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 "intr_remapping.h"
  14. #include <acpi/acpi.h>
  15. #include <asm/pci-direct.h>
  16. #include "pci.h"
  17. static struct ioapic_scope ir_ioapic[MAX_IO_APICS];
  18. static struct hpet_scope ir_hpet[MAX_HPET_TBS];
  19. static int ir_ioapic_num, ir_hpet_num;
  20. int intr_remapping_enabled;
  21. static int disable_intremap;
  22. static int disable_sourceid_checking;
  23. static __init int setup_nointremap(char *str)
  24. {
  25. disable_intremap = 1;
  26. return 0;
  27. }
  28. early_param("nointremap", setup_nointremap);
  29. static __init int setup_intremap(char *str)
  30. {
  31. if (!str)
  32. return -EINVAL;
  33. if (!strncmp(str, "on", 2))
  34. disable_intremap = 0;
  35. else if (!strncmp(str, "off", 3))
  36. disable_intremap = 1;
  37. else if (!strncmp(str, "nosid", 5))
  38. disable_sourceid_checking = 1;
  39. return 0;
  40. }
  41. early_param("intremap", setup_intremap);
  42. struct irq_2_iommu {
  43. struct intel_iommu *iommu;
  44. u16 irte_index;
  45. u16 sub_handle;
  46. u8 irte_mask;
  47. };
  48. #ifdef CONFIG_GENERIC_HARDIRQS
  49. static struct irq_2_iommu *get_one_free_irq_2_iommu(int node)
  50. {
  51. struct irq_2_iommu *iommu;
  52. iommu = kzalloc_node(sizeof(*iommu), GFP_ATOMIC, node);
  53. printk(KERN_DEBUG "alloc irq_2_iommu on node %d\n", node);
  54. return iommu;
  55. }
  56. static struct irq_2_iommu *irq_2_iommu(unsigned int irq)
  57. {
  58. struct irq_desc *desc;
  59. desc = irq_to_desc(irq);
  60. if (WARN_ON_ONCE(!desc))
  61. return NULL;
  62. return desc->irq_2_iommu;
  63. }
  64. static struct irq_2_iommu *irq_2_iommu_alloc(unsigned int irq)
  65. {
  66. struct irq_desc *desc;
  67. struct irq_2_iommu *irq_iommu;
  68. desc = irq_to_desc(irq);
  69. if (!desc) {
  70. printk(KERN_INFO "can not get irq_desc for %d\n", irq);
  71. return NULL;
  72. }
  73. irq_iommu = desc->irq_2_iommu;
  74. if (!irq_iommu)
  75. desc->irq_2_iommu = get_one_free_irq_2_iommu(irq_node(irq));
  76. return desc->irq_2_iommu;
  77. }
  78. #else /* !CONFIG_SPARSE_IRQ */
  79. static struct irq_2_iommu irq_2_iommuX[NR_IRQS];
  80. static struct irq_2_iommu *irq_2_iommu(unsigned int irq)
  81. {
  82. if (irq < nr_irqs)
  83. return &irq_2_iommuX[irq];
  84. return NULL;
  85. }
  86. static struct irq_2_iommu *irq_2_iommu_alloc(unsigned int irq)
  87. {
  88. return irq_2_iommu(irq);
  89. }
  90. #endif
  91. static DEFINE_SPINLOCK(irq_2_ir_lock);
  92. static struct irq_2_iommu *valid_irq_2_iommu(unsigned int irq)
  93. {
  94. struct irq_2_iommu *irq_iommu;
  95. irq_iommu = irq_2_iommu(irq);
  96. if (!irq_iommu)
  97. return NULL;
  98. if (!irq_iommu->iommu)
  99. return NULL;
  100. return irq_iommu;
  101. }
  102. int irq_remapped(int irq)
  103. {
  104. return valid_irq_2_iommu(irq) != NULL;
  105. }
  106. int get_irte(int irq, struct irte *entry)
  107. {
  108. int index;
  109. struct irq_2_iommu *irq_iommu;
  110. unsigned long flags;
  111. if (!entry)
  112. return -1;
  113. spin_lock_irqsave(&irq_2_ir_lock, flags);
  114. irq_iommu = valid_irq_2_iommu(irq);
  115. if (!irq_iommu) {
  116. spin_unlock_irqrestore(&irq_2_ir_lock, flags);
  117. return -1;
  118. }
  119. index = irq_iommu->irte_index + irq_iommu->sub_handle;
  120. *entry = *(irq_iommu->iommu->ir_table->base + index);
  121. spin_unlock_irqrestore(&irq_2_ir_lock, flags);
  122. return 0;
  123. }
  124. int alloc_irte(struct intel_iommu *iommu, int irq, u16 count)
  125. {
  126. struct ir_table *table = iommu->ir_table;
  127. struct irq_2_iommu *irq_iommu;
  128. u16 index, start_index;
  129. unsigned int mask = 0;
  130. unsigned long flags;
  131. int i;
  132. if (!count)
  133. return -1;
  134. #ifndef CONFIG_SPARSE_IRQ
  135. /* protect irq_2_iommu_alloc later */
  136. if (irq >= nr_irqs)
  137. return -1;
  138. #endif
  139. /*
  140. * start the IRTE search from index 0.
  141. */
  142. index = start_index = 0;
  143. if (count > 1) {
  144. count = __roundup_pow_of_two(count);
  145. mask = ilog2(count);
  146. }
  147. if (mask > ecap_max_handle_mask(iommu->ecap)) {
  148. printk(KERN_ERR
  149. "Requested mask %x exceeds the max invalidation handle"
  150. " mask value %Lx\n", mask,
  151. ecap_max_handle_mask(iommu->ecap));
  152. return -1;
  153. }
  154. spin_lock_irqsave(&irq_2_ir_lock, flags);
  155. do {
  156. for (i = index; i < index + count; i++)
  157. if (table->base[i].present)
  158. break;
  159. /* empty index found */
  160. if (i == index + count)
  161. break;
  162. index = (index + count) % INTR_REMAP_TABLE_ENTRIES;
  163. if (index == start_index) {
  164. spin_unlock_irqrestore(&irq_2_ir_lock, flags);
  165. printk(KERN_ERR "can't allocate an IRTE\n");
  166. return -1;
  167. }
  168. } while (1);
  169. for (i = index; i < index + count; i++)
  170. table->base[i].present = 1;
  171. irq_iommu = irq_2_iommu_alloc(irq);
  172. if (!irq_iommu) {
  173. spin_unlock_irqrestore(&irq_2_ir_lock, flags);
  174. printk(KERN_ERR "can't allocate irq_2_iommu\n");
  175. return -1;
  176. }
  177. irq_iommu->iommu = iommu;
  178. irq_iommu->irte_index = index;
  179. irq_iommu->sub_handle = 0;
  180. irq_iommu->irte_mask = mask;
  181. spin_unlock_irqrestore(&irq_2_ir_lock, flags);
  182. return index;
  183. }
  184. static int qi_flush_iec(struct intel_iommu *iommu, int index, int mask)
  185. {
  186. struct qi_desc desc;
  187. desc.low = QI_IEC_IIDEX(index) | QI_IEC_TYPE | QI_IEC_IM(mask)
  188. | QI_IEC_SELECTIVE;
  189. desc.high = 0;
  190. return qi_submit_sync(&desc, iommu);
  191. }
  192. int map_irq_to_irte_handle(int irq, u16 *sub_handle)
  193. {
  194. int index;
  195. struct irq_2_iommu *irq_iommu;
  196. unsigned long flags;
  197. spin_lock_irqsave(&irq_2_ir_lock, flags);
  198. irq_iommu = valid_irq_2_iommu(irq);
  199. if (!irq_iommu) {
  200. spin_unlock_irqrestore(&irq_2_ir_lock, flags);
  201. return -1;
  202. }
  203. *sub_handle = irq_iommu->sub_handle;
  204. index = irq_iommu->irte_index;
  205. spin_unlock_irqrestore(&irq_2_ir_lock, flags);
  206. return index;
  207. }
  208. int set_irte_irq(int irq, struct intel_iommu *iommu, u16 index, u16 subhandle)
  209. {
  210. struct irq_2_iommu *irq_iommu;
  211. unsigned long flags;
  212. spin_lock_irqsave(&irq_2_ir_lock, flags);
  213. irq_iommu = irq_2_iommu_alloc(irq);
  214. if (!irq_iommu) {
  215. spin_unlock_irqrestore(&irq_2_ir_lock, flags);
  216. printk(KERN_ERR "can't allocate irq_2_iommu\n");
  217. return -1;
  218. }
  219. irq_iommu->iommu = iommu;
  220. irq_iommu->irte_index = index;
  221. irq_iommu->sub_handle = subhandle;
  222. irq_iommu->irte_mask = 0;
  223. spin_unlock_irqrestore(&irq_2_ir_lock, flags);
  224. return 0;
  225. }
  226. int clear_irte_irq(int irq, struct intel_iommu *iommu, u16 index)
  227. {
  228. struct irq_2_iommu *irq_iommu;
  229. unsigned long flags;
  230. spin_lock_irqsave(&irq_2_ir_lock, flags);
  231. irq_iommu = valid_irq_2_iommu(irq);
  232. if (!irq_iommu) {
  233. spin_unlock_irqrestore(&irq_2_ir_lock, flags);
  234. return -1;
  235. }
  236. irq_iommu->iommu = NULL;
  237. irq_iommu->irte_index = 0;
  238. irq_iommu->sub_handle = 0;
  239. irq_2_iommu(irq)->irte_mask = 0;
  240. spin_unlock_irqrestore(&irq_2_ir_lock, flags);
  241. return 0;
  242. }
  243. int modify_irte(int irq, struct irte *irte_modified)
  244. {
  245. int rc;
  246. int index;
  247. struct irte *irte;
  248. struct intel_iommu *iommu;
  249. struct irq_2_iommu *irq_iommu;
  250. unsigned long flags;
  251. spin_lock_irqsave(&irq_2_ir_lock, flags);
  252. irq_iommu = valid_irq_2_iommu(irq);
  253. if (!irq_iommu) {
  254. spin_unlock_irqrestore(&irq_2_ir_lock, flags);
  255. return -1;
  256. }
  257. iommu = irq_iommu->iommu;
  258. index = irq_iommu->irte_index + irq_iommu->sub_handle;
  259. irte = &iommu->ir_table->base[index];
  260. set_64bit(&irte->low, irte_modified->low);
  261. set_64bit(&irte->high, irte_modified->high);
  262. __iommu_flush_cache(iommu, irte, sizeof(*irte));
  263. rc = qi_flush_iec(iommu, index, 0);
  264. spin_unlock_irqrestore(&irq_2_ir_lock, flags);
  265. return rc;
  266. }
  267. int flush_irte(int irq)
  268. {
  269. int rc;
  270. int index;
  271. struct intel_iommu *iommu;
  272. struct irq_2_iommu *irq_iommu;
  273. unsigned long flags;
  274. spin_lock_irqsave(&irq_2_ir_lock, flags);
  275. irq_iommu = valid_irq_2_iommu(irq);
  276. if (!irq_iommu) {
  277. spin_unlock_irqrestore(&irq_2_ir_lock, flags);
  278. return -1;
  279. }
  280. iommu = irq_iommu->iommu;
  281. index = irq_iommu->irte_index + irq_iommu->sub_handle;
  282. rc = qi_flush_iec(iommu, index, irq_iommu->irte_mask);
  283. spin_unlock_irqrestore(&irq_2_ir_lock, flags);
  284. return rc;
  285. }
  286. struct intel_iommu *map_hpet_to_ir(u8 hpet_id)
  287. {
  288. int i;
  289. for (i = 0; i < MAX_HPET_TBS; i++)
  290. if (ir_hpet[i].id == hpet_id)
  291. return ir_hpet[i].iommu;
  292. return NULL;
  293. }
  294. struct intel_iommu *map_ioapic_to_ir(int apic)
  295. {
  296. int i;
  297. for (i = 0; i < MAX_IO_APICS; i++)
  298. if (ir_ioapic[i].id == apic)
  299. return ir_ioapic[i].iommu;
  300. return NULL;
  301. }
  302. struct intel_iommu *map_dev_to_ir(struct pci_dev *dev)
  303. {
  304. struct dmar_drhd_unit *drhd;
  305. drhd = dmar_find_matched_drhd_unit(dev);
  306. if (!drhd)
  307. return NULL;
  308. return drhd->iommu;
  309. }
  310. static int clear_entries(struct irq_2_iommu *irq_iommu)
  311. {
  312. struct irte *start, *entry, *end;
  313. struct intel_iommu *iommu;
  314. int index;
  315. if (irq_iommu->sub_handle)
  316. return 0;
  317. iommu = irq_iommu->iommu;
  318. index = irq_iommu->irte_index + irq_iommu->sub_handle;
  319. start = iommu->ir_table->base + index;
  320. end = start + (1 << irq_iommu->irte_mask);
  321. for (entry = start; entry < end; entry++) {
  322. set_64bit(&entry->low, 0);
  323. set_64bit(&entry->high, 0);
  324. }
  325. return qi_flush_iec(iommu, index, irq_iommu->irte_mask);
  326. }
  327. int free_irte(int irq)
  328. {
  329. int rc = 0;
  330. struct irq_2_iommu *irq_iommu;
  331. unsigned long flags;
  332. spin_lock_irqsave(&irq_2_ir_lock, flags);
  333. irq_iommu = valid_irq_2_iommu(irq);
  334. if (!irq_iommu) {
  335. spin_unlock_irqrestore(&irq_2_ir_lock, flags);
  336. return -1;
  337. }
  338. rc = clear_entries(irq_iommu);
  339. irq_iommu->iommu = NULL;
  340. irq_iommu->irte_index = 0;
  341. irq_iommu->sub_handle = 0;
  342. irq_iommu->irte_mask = 0;
  343. spin_unlock_irqrestore(&irq_2_ir_lock, flags);
  344. return rc;
  345. }
  346. /*
  347. * source validation type
  348. */
  349. #define SVT_NO_VERIFY 0x0 /* no verification is required */
  350. #define SVT_VERIFY_SID_SQ 0x1 /* verify using SID and SQ fiels */
  351. #define SVT_VERIFY_BUS 0x2 /* verify bus of request-id */
  352. /*
  353. * source-id qualifier
  354. */
  355. #define SQ_ALL_16 0x0 /* verify all 16 bits of request-id */
  356. #define SQ_13_IGNORE_1 0x1 /* verify most significant 13 bits, ignore
  357. * the third least significant bit
  358. */
  359. #define SQ_13_IGNORE_2 0x2 /* verify most significant 13 bits, ignore
  360. * the second and third least significant bits
  361. */
  362. #define SQ_13_IGNORE_3 0x3 /* verify most significant 13 bits, ignore
  363. * the least three significant bits
  364. */
  365. /*
  366. * set SVT, SQ and SID fields of irte to verify
  367. * source ids of interrupt requests
  368. */
  369. static void set_irte_sid(struct irte *irte, unsigned int svt,
  370. unsigned int sq, unsigned int sid)
  371. {
  372. if (disable_sourceid_checking)
  373. svt = SVT_NO_VERIFY;
  374. irte->svt = svt;
  375. irte->sq = sq;
  376. irte->sid = sid;
  377. }
  378. int set_ioapic_sid(struct irte *irte, int apic)
  379. {
  380. int i;
  381. u16 sid = 0;
  382. if (!irte)
  383. return -1;
  384. for (i = 0; i < MAX_IO_APICS; i++) {
  385. if (ir_ioapic[i].id == apic) {
  386. sid = (ir_ioapic[i].bus << 8) | ir_ioapic[i].devfn;
  387. break;
  388. }
  389. }
  390. if (sid == 0) {
  391. pr_warning("Failed to set source-id of IOAPIC (%d)\n", apic);
  392. return -1;
  393. }
  394. set_irte_sid(irte, 1, 0, sid);
  395. return 0;
  396. }
  397. int set_hpet_sid(struct irte *irte, u8 id)
  398. {
  399. int i;
  400. u16 sid = 0;
  401. if (!irte)
  402. return -1;
  403. for (i = 0; i < MAX_HPET_TBS; i++) {
  404. if (ir_hpet[i].id == id) {
  405. sid = (ir_hpet[i].bus << 8) | ir_hpet[i].devfn;
  406. break;
  407. }
  408. }
  409. if (sid == 0) {
  410. pr_warning("Failed to set source-id of HPET block (%d)\n", id);
  411. return -1;
  412. }
  413. /*
  414. * Should really use SQ_ALL_16. Some platforms are broken.
  415. * While we figure out the right quirks for these broken platforms, use
  416. * SQ_13_IGNORE_3 for now.
  417. */
  418. set_irte_sid(irte, SVT_VERIFY_SID_SQ, SQ_13_IGNORE_3, sid);
  419. return 0;
  420. }
  421. int set_msi_sid(struct irte *irte, struct pci_dev *dev)
  422. {
  423. struct pci_dev *bridge;
  424. if (!irte || !dev)
  425. return -1;
  426. /* PCIe device or Root Complex integrated PCI device */
  427. if (pci_is_pcie(dev) || !dev->bus->parent) {
  428. set_irte_sid(irte, SVT_VERIFY_SID_SQ, SQ_ALL_16,
  429. (dev->bus->number << 8) | dev->devfn);
  430. return 0;
  431. }
  432. bridge = pci_find_upstream_pcie_bridge(dev);
  433. if (bridge) {
  434. if (pci_is_pcie(bridge))/* this is a PCIe-to-PCI/PCIX bridge */
  435. set_irte_sid(irte, SVT_VERIFY_BUS, SQ_ALL_16,
  436. (bridge->bus->number << 8) | dev->bus->number);
  437. else /* this is a legacy PCI bridge */
  438. set_irte_sid(irte, SVT_VERIFY_SID_SQ, SQ_ALL_16,
  439. (bridge->bus->number << 8) | bridge->devfn);
  440. }
  441. return 0;
  442. }
  443. static void iommu_set_intr_remapping(struct intel_iommu *iommu, int mode)
  444. {
  445. u64 addr;
  446. u32 sts;
  447. unsigned long flags;
  448. addr = virt_to_phys((void *)iommu->ir_table->base);
  449. spin_lock_irqsave(&iommu->register_lock, flags);
  450. dmar_writeq(iommu->reg + DMAR_IRTA_REG,
  451. (addr) | IR_X2APIC_MODE(mode) | INTR_REMAP_TABLE_REG_SIZE);
  452. /* Set interrupt-remapping table pointer */
  453. iommu->gcmd |= DMA_GCMD_SIRTP;
  454. writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
  455. IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
  456. readl, (sts & DMA_GSTS_IRTPS), sts);
  457. spin_unlock_irqrestore(&iommu->register_lock, flags);
  458. /*
  459. * global invalidation of interrupt entry cache before enabling
  460. * interrupt-remapping.
  461. */
  462. qi_global_iec(iommu);
  463. spin_lock_irqsave(&iommu->register_lock, flags);
  464. /* Enable interrupt-remapping */
  465. iommu->gcmd |= DMA_GCMD_IRE;
  466. writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
  467. IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
  468. readl, (sts & DMA_GSTS_IRES), sts);
  469. spin_unlock_irqrestore(&iommu->register_lock, flags);
  470. }
  471. static int setup_intr_remapping(struct intel_iommu *iommu, int mode)
  472. {
  473. struct ir_table *ir_table;
  474. struct page *pages;
  475. ir_table = iommu->ir_table = kzalloc(sizeof(struct ir_table),
  476. GFP_ATOMIC);
  477. if (!iommu->ir_table)
  478. return -ENOMEM;
  479. pages = alloc_pages_node(iommu->node, GFP_ATOMIC | __GFP_ZERO,
  480. INTR_REMAP_PAGE_ORDER);
  481. if (!pages) {
  482. printk(KERN_ERR "failed to allocate pages of order %d\n",
  483. INTR_REMAP_PAGE_ORDER);
  484. kfree(iommu->ir_table);
  485. return -ENOMEM;
  486. }
  487. ir_table->base = page_address(pages);
  488. iommu_set_intr_remapping(iommu, mode);
  489. return 0;
  490. }
  491. /*
  492. * Disable Interrupt Remapping.
  493. */
  494. static void iommu_disable_intr_remapping(struct intel_iommu *iommu)
  495. {
  496. unsigned long flags;
  497. u32 sts;
  498. if (!ecap_ir_support(iommu->ecap))
  499. return;
  500. /*
  501. * global invalidation of interrupt entry cache before disabling
  502. * interrupt-remapping.
  503. */
  504. qi_global_iec(iommu);
  505. spin_lock_irqsave(&iommu->register_lock, flags);
  506. sts = dmar_readq(iommu->reg + DMAR_GSTS_REG);
  507. if (!(sts & DMA_GSTS_IRES))
  508. goto end;
  509. iommu->gcmd &= ~DMA_GCMD_IRE;
  510. writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
  511. IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
  512. readl, !(sts & DMA_GSTS_IRES), sts);
  513. end:
  514. spin_unlock_irqrestore(&iommu->register_lock, flags);
  515. }
  516. int __init intr_remapping_supported(void)
  517. {
  518. struct dmar_drhd_unit *drhd;
  519. if (disable_intremap)
  520. return 0;
  521. if (!dmar_ir_support())
  522. return 0;
  523. for_each_drhd_unit(drhd) {
  524. struct intel_iommu *iommu = drhd->iommu;
  525. if (!ecap_ir_support(iommu->ecap))
  526. return 0;
  527. }
  528. return 1;
  529. }
  530. int __init enable_intr_remapping(int eim)
  531. {
  532. struct dmar_drhd_unit *drhd;
  533. int setup = 0;
  534. if (parse_ioapics_under_ir() != 1) {
  535. printk(KERN_INFO "Not enable interrupt remapping\n");
  536. return -1;
  537. }
  538. for_each_drhd_unit(drhd) {
  539. struct intel_iommu *iommu = drhd->iommu;
  540. /*
  541. * If the queued invalidation is already initialized,
  542. * shouldn't disable it.
  543. */
  544. if (iommu->qi)
  545. continue;
  546. /*
  547. * Clear previous faults.
  548. */
  549. dmar_fault(-1, iommu);
  550. /*
  551. * Disable intr remapping and queued invalidation, if already
  552. * enabled prior to OS handover.
  553. */
  554. iommu_disable_intr_remapping(iommu);
  555. dmar_disable_qi(iommu);
  556. }
  557. /*
  558. * check for the Interrupt-remapping support
  559. */
  560. for_each_drhd_unit(drhd) {
  561. struct intel_iommu *iommu = drhd->iommu;
  562. if (!ecap_ir_support(iommu->ecap))
  563. continue;
  564. if (eim && !ecap_eim_support(iommu->ecap)) {
  565. printk(KERN_INFO "DRHD %Lx: EIM not supported by DRHD, "
  566. " ecap %Lx\n", drhd->reg_base_addr, iommu->ecap);
  567. return -1;
  568. }
  569. }
  570. /*
  571. * Enable queued invalidation for all the DRHD's.
  572. */
  573. for_each_drhd_unit(drhd) {
  574. int ret;
  575. struct intel_iommu *iommu = drhd->iommu;
  576. ret = dmar_enable_qi(iommu);
  577. if (ret) {
  578. printk(KERN_ERR "DRHD %Lx: failed to enable queued, "
  579. " invalidation, ecap %Lx, ret %d\n",
  580. drhd->reg_base_addr, iommu->ecap, ret);
  581. return -1;
  582. }
  583. }
  584. /*
  585. * Setup Interrupt-remapping for all the DRHD's now.
  586. */
  587. for_each_drhd_unit(drhd) {
  588. struct intel_iommu *iommu = drhd->iommu;
  589. if (!ecap_ir_support(iommu->ecap))
  590. continue;
  591. if (setup_intr_remapping(iommu, eim))
  592. goto error;
  593. setup = 1;
  594. }
  595. if (!setup)
  596. goto error;
  597. intr_remapping_enabled = 1;
  598. return 0;
  599. error:
  600. /*
  601. * handle error condition gracefully here!
  602. */
  603. return -1;
  604. }
  605. static void ir_parse_one_hpet_scope(struct acpi_dmar_device_scope *scope,
  606. struct intel_iommu *iommu)
  607. {
  608. struct acpi_dmar_pci_path *path;
  609. u8 bus;
  610. int count;
  611. bus = scope->bus;
  612. path = (struct acpi_dmar_pci_path *)(scope + 1);
  613. count = (scope->length - sizeof(struct acpi_dmar_device_scope))
  614. / sizeof(struct acpi_dmar_pci_path);
  615. while (--count > 0) {
  616. /*
  617. * Access PCI directly due to the PCI
  618. * subsystem isn't initialized yet.
  619. */
  620. bus = read_pci_config_byte(bus, path->dev, path->fn,
  621. PCI_SECONDARY_BUS);
  622. path++;
  623. }
  624. ir_hpet[ir_hpet_num].bus = bus;
  625. ir_hpet[ir_hpet_num].devfn = PCI_DEVFN(path->dev, path->fn);
  626. ir_hpet[ir_hpet_num].iommu = iommu;
  627. ir_hpet[ir_hpet_num].id = scope->enumeration_id;
  628. ir_hpet_num++;
  629. }
  630. static void ir_parse_one_ioapic_scope(struct acpi_dmar_device_scope *scope,
  631. struct intel_iommu *iommu)
  632. {
  633. struct acpi_dmar_pci_path *path;
  634. u8 bus;
  635. int count;
  636. bus = scope->bus;
  637. path = (struct acpi_dmar_pci_path *)(scope + 1);
  638. count = (scope->length - sizeof(struct acpi_dmar_device_scope))
  639. / sizeof(struct acpi_dmar_pci_path);
  640. while (--count > 0) {
  641. /*
  642. * Access PCI directly due to the PCI
  643. * subsystem isn't initialized yet.
  644. */
  645. bus = read_pci_config_byte(bus, path->dev, path->fn,
  646. PCI_SECONDARY_BUS);
  647. path++;
  648. }
  649. ir_ioapic[ir_ioapic_num].bus = bus;
  650. ir_ioapic[ir_ioapic_num].devfn = PCI_DEVFN(path->dev, path->fn);
  651. ir_ioapic[ir_ioapic_num].iommu = iommu;
  652. ir_ioapic[ir_ioapic_num].id = scope->enumeration_id;
  653. ir_ioapic_num++;
  654. }
  655. static int ir_parse_ioapic_hpet_scope(struct acpi_dmar_header *header,
  656. struct intel_iommu *iommu)
  657. {
  658. struct acpi_dmar_hardware_unit *drhd;
  659. struct acpi_dmar_device_scope *scope;
  660. void *start, *end;
  661. drhd = (struct acpi_dmar_hardware_unit *)header;
  662. start = (void *)(drhd + 1);
  663. end = ((void *)drhd) + header->length;
  664. while (start < end) {
  665. scope = start;
  666. if (scope->entry_type == ACPI_DMAR_SCOPE_TYPE_IOAPIC) {
  667. if (ir_ioapic_num == MAX_IO_APICS) {
  668. printk(KERN_WARNING "Exceeded Max IO APICS\n");
  669. return -1;
  670. }
  671. printk(KERN_INFO "IOAPIC id %d under DRHD base "
  672. " 0x%Lx IOMMU %d\n", scope->enumeration_id,
  673. drhd->address, iommu->seq_id);
  674. ir_parse_one_ioapic_scope(scope, iommu);
  675. } else if (scope->entry_type == ACPI_DMAR_SCOPE_TYPE_HPET) {
  676. if (ir_hpet_num == MAX_HPET_TBS) {
  677. printk(KERN_WARNING "Exceeded Max HPET blocks\n");
  678. return -1;
  679. }
  680. printk(KERN_INFO "HPET id %d under DRHD base"
  681. " 0x%Lx\n", scope->enumeration_id,
  682. drhd->address);
  683. ir_parse_one_hpet_scope(scope, iommu);
  684. }
  685. start += scope->length;
  686. }
  687. return 0;
  688. }
  689. /*
  690. * Finds the assocaition between IOAPIC's and its Interrupt-remapping
  691. * hardware unit.
  692. */
  693. int __init parse_ioapics_under_ir(void)
  694. {
  695. struct dmar_drhd_unit *drhd;
  696. int ir_supported = 0;
  697. for_each_drhd_unit(drhd) {
  698. struct intel_iommu *iommu = drhd->iommu;
  699. if (ecap_ir_support(iommu->ecap)) {
  700. if (ir_parse_ioapic_hpet_scope(drhd->hdr, iommu))
  701. return -1;
  702. ir_supported = 1;
  703. }
  704. }
  705. if (ir_supported && ir_ioapic_num != nr_ioapics) {
  706. printk(KERN_WARNING
  707. "Not all IO-APIC's listed under remapping hardware\n");
  708. return -1;
  709. }
  710. return ir_supported;
  711. }
  712. void disable_intr_remapping(void)
  713. {
  714. struct dmar_drhd_unit *drhd;
  715. struct intel_iommu *iommu = NULL;
  716. /*
  717. * Disable Interrupt-remapping for all the DRHD's now.
  718. */
  719. for_each_iommu(iommu, drhd) {
  720. if (!ecap_ir_support(iommu->ecap))
  721. continue;
  722. iommu_disable_intr_remapping(iommu);
  723. }
  724. }
  725. int reenable_intr_remapping(int eim)
  726. {
  727. struct dmar_drhd_unit *drhd;
  728. int setup = 0;
  729. struct intel_iommu *iommu = NULL;
  730. for_each_iommu(iommu, drhd)
  731. if (iommu->qi)
  732. dmar_reenable_qi(iommu);
  733. /*
  734. * Setup Interrupt-remapping for all the DRHD's now.
  735. */
  736. for_each_iommu(iommu, drhd) {
  737. if (!ecap_ir_support(iommu->ecap))
  738. continue;
  739. /* Set up interrupt remapping for iommu.*/
  740. iommu_set_intr_remapping(iommu, eim);
  741. setup = 1;
  742. }
  743. if (!setup)
  744. goto error;
  745. return 0;
  746. error:
  747. /*
  748. * handle error condition gracefully here!
  749. */
  750. return -1;
  751. }