irq_remapping.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520
  1. // SPDX-License-Identifier: GPL-2.0
  2. #define pr_fmt(fmt) "DMAR-IR: " fmt
  3. #include <linux/interrupt.h>
  4. #include <linux/dmar.h>
  5. #include <linux/spinlock.h>
  6. #include <linux/slab.h>
  7. #include <linux/jiffies.h>
  8. #include <linux/hpet.h>
  9. #include <linux/pci.h>
  10. #include <linux/irq.h>
  11. #include <linux/acpi.h>
  12. #include <linux/irqdomain.h>
  13. #include <linux/crash_dump.h>
  14. #include <asm/io_apic.h>
  15. #include <asm/apic.h>
  16. #include <asm/smp.h>
  17. #include <asm/cpu.h>
  18. #include <asm/irq_remapping.h>
  19. #include <asm/pci-direct.h>
  20. #include "iommu.h"
  21. #include "../irq_remapping.h"
  22. #include "cap_audit.h"
  23. enum irq_mode {
  24. IRQ_REMAPPING,
  25. IRQ_POSTING,
  26. };
  27. struct ioapic_scope {
  28. struct intel_iommu *iommu;
  29. unsigned int id;
  30. unsigned int bus; /* PCI bus number */
  31. unsigned int devfn; /* PCI devfn number */
  32. };
  33. struct hpet_scope {
  34. struct intel_iommu *iommu;
  35. u8 id;
  36. unsigned int bus;
  37. unsigned int devfn;
  38. };
  39. struct irq_2_iommu {
  40. struct intel_iommu *iommu;
  41. u16 irte_index;
  42. u16 sub_handle;
  43. u8 irte_mask;
  44. enum irq_mode mode;
  45. };
  46. struct intel_ir_data {
  47. struct irq_2_iommu irq_2_iommu;
  48. struct irte irte_entry;
  49. union {
  50. struct msi_msg msi_entry;
  51. };
  52. };
  53. #define IR_X2APIC_MODE(mode) (mode ? (1 << 11) : 0)
  54. #define IRTE_DEST(dest) ((eim_mode) ? dest : dest << 8)
  55. static int __read_mostly eim_mode;
  56. static struct ioapic_scope ir_ioapic[MAX_IO_APICS];
  57. static struct hpet_scope ir_hpet[MAX_HPET_TBS];
  58. /*
  59. * Lock ordering:
  60. * ->dmar_global_lock
  61. * ->irq_2_ir_lock
  62. * ->qi->q_lock
  63. * ->iommu->register_lock
  64. * Note:
  65. * intel_irq_remap_ops.{supported,prepare,enable,disable,reenable} are called
  66. * in single-threaded environment with interrupt disabled, so no need to tabke
  67. * the dmar_global_lock.
  68. */
  69. DEFINE_RAW_SPINLOCK(irq_2_ir_lock);
  70. static const struct irq_domain_ops intel_ir_domain_ops;
  71. static void iommu_disable_irq_remapping(struct intel_iommu *iommu);
  72. static int __init parse_ioapics_under_ir(void);
  73. static bool ir_pre_enabled(struct intel_iommu *iommu)
  74. {
  75. return (iommu->flags & VTD_FLAG_IRQ_REMAP_PRE_ENABLED);
  76. }
  77. static void clear_ir_pre_enabled(struct intel_iommu *iommu)
  78. {
  79. iommu->flags &= ~VTD_FLAG_IRQ_REMAP_PRE_ENABLED;
  80. }
  81. static void init_ir_status(struct intel_iommu *iommu)
  82. {
  83. u32 gsts;
  84. gsts = readl(iommu->reg + DMAR_GSTS_REG);
  85. if (gsts & DMA_GSTS_IRES)
  86. iommu->flags |= VTD_FLAG_IRQ_REMAP_PRE_ENABLED;
  87. }
  88. static int alloc_irte(struct intel_iommu *iommu,
  89. struct irq_2_iommu *irq_iommu, u16 count)
  90. {
  91. struct ir_table *table = iommu->ir_table;
  92. unsigned int mask = 0;
  93. unsigned long flags;
  94. int index;
  95. if (!count || !irq_iommu)
  96. return -1;
  97. if (count > 1) {
  98. count = __roundup_pow_of_two(count);
  99. mask = ilog2(count);
  100. }
  101. if (mask > ecap_max_handle_mask(iommu->ecap)) {
  102. pr_err("Requested mask %x exceeds the max invalidation handle"
  103. " mask value %Lx\n", mask,
  104. ecap_max_handle_mask(iommu->ecap));
  105. return -1;
  106. }
  107. raw_spin_lock_irqsave(&irq_2_ir_lock, flags);
  108. index = bitmap_find_free_region(table->bitmap,
  109. INTR_REMAP_TABLE_ENTRIES, mask);
  110. if (index < 0) {
  111. pr_warn("IR%d: can't allocate an IRTE\n", iommu->seq_id);
  112. } else {
  113. irq_iommu->iommu = iommu;
  114. irq_iommu->irte_index = index;
  115. irq_iommu->sub_handle = 0;
  116. irq_iommu->irte_mask = mask;
  117. irq_iommu->mode = IRQ_REMAPPING;
  118. }
  119. raw_spin_unlock_irqrestore(&irq_2_ir_lock, flags);
  120. return index;
  121. }
  122. static int qi_flush_iec(struct intel_iommu *iommu, int index, int mask)
  123. {
  124. struct qi_desc desc;
  125. desc.qw0 = QI_IEC_IIDEX(index) | QI_IEC_TYPE | QI_IEC_IM(mask)
  126. | QI_IEC_SELECTIVE;
  127. desc.qw1 = 0;
  128. desc.qw2 = 0;
  129. desc.qw3 = 0;
  130. return qi_submit_sync(iommu, &desc, 1, 0);
  131. }
  132. static int modify_irte(struct irq_2_iommu *irq_iommu,
  133. struct irte *irte_modified)
  134. {
  135. struct intel_iommu *iommu;
  136. unsigned long flags;
  137. struct irte *irte;
  138. int rc, index;
  139. if (!irq_iommu)
  140. return -1;
  141. raw_spin_lock_irqsave(&irq_2_ir_lock, flags);
  142. iommu = irq_iommu->iommu;
  143. index = irq_iommu->irte_index + irq_iommu->sub_handle;
  144. irte = &iommu->ir_table->base[index];
  145. #if defined(CONFIG_HAVE_CMPXCHG_DOUBLE)
  146. if ((irte->pst == 1) || (irte_modified->pst == 1)) {
  147. bool ret;
  148. ret = cmpxchg_double(&irte->low, &irte->high,
  149. irte->low, irte->high,
  150. irte_modified->low, irte_modified->high);
  151. /*
  152. * We use cmpxchg16 to atomically update the 128-bit IRTE,
  153. * and it cannot be updated by the hardware or other processors
  154. * behind us, so the return value of cmpxchg16 should be the
  155. * same as the old value.
  156. */
  157. WARN_ON(!ret);
  158. } else
  159. #endif
  160. {
  161. set_64bit(&irte->low, irte_modified->low);
  162. set_64bit(&irte->high, irte_modified->high);
  163. }
  164. __iommu_flush_cache(iommu, irte, sizeof(*irte));
  165. rc = qi_flush_iec(iommu, index, 0);
  166. /* Update iommu mode according to the IRTE mode */
  167. irq_iommu->mode = irte->pst ? IRQ_POSTING : IRQ_REMAPPING;
  168. raw_spin_unlock_irqrestore(&irq_2_ir_lock, flags);
  169. return rc;
  170. }
  171. static struct intel_iommu *map_hpet_to_iommu(u8 hpet_id)
  172. {
  173. int i;
  174. for (i = 0; i < MAX_HPET_TBS; i++) {
  175. if (ir_hpet[i].id == hpet_id && ir_hpet[i].iommu)
  176. return ir_hpet[i].iommu;
  177. }
  178. return NULL;
  179. }
  180. static struct intel_iommu *map_ioapic_to_iommu(int apic)
  181. {
  182. int i;
  183. for (i = 0; i < MAX_IO_APICS; i++) {
  184. if (ir_ioapic[i].id == apic && ir_ioapic[i].iommu)
  185. return ir_ioapic[i].iommu;
  186. }
  187. return NULL;
  188. }
  189. static struct irq_domain *map_dev_to_ir(struct pci_dev *dev)
  190. {
  191. struct dmar_drhd_unit *drhd = dmar_find_matched_drhd_unit(dev);
  192. return drhd ? drhd->iommu->ir_msi_domain : NULL;
  193. }
  194. static int clear_entries(struct irq_2_iommu *irq_iommu)
  195. {
  196. struct irte *start, *entry, *end;
  197. struct intel_iommu *iommu;
  198. int index;
  199. if (irq_iommu->sub_handle)
  200. return 0;
  201. iommu = irq_iommu->iommu;
  202. index = irq_iommu->irte_index;
  203. start = iommu->ir_table->base + index;
  204. end = start + (1 << irq_iommu->irte_mask);
  205. for (entry = start; entry < end; entry++) {
  206. set_64bit(&entry->low, 0);
  207. set_64bit(&entry->high, 0);
  208. }
  209. bitmap_release_region(iommu->ir_table->bitmap, index,
  210. irq_iommu->irte_mask);
  211. return qi_flush_iec(iommu, index, irq_iommu->irte_mask);
  212. }
  213. /*
  214. * source validation type
  215. */
  216. #define SVT_NO_VERIFY 0x0 /* no verification is required */
  217. #define SVT_VERIFY_SID_SQ 0x1 /* verify using SID and SQ fields */
  218. #define SVT_VERIFY_BUS 0x2 /* verify bus of request-id */
  219. /*
  220. * source-id qualifier
  221. */
  222. #define SQ_ALL_16 0x0 /* verify all 16 bits of request-id */
  223. #define SQ_13_IGNORE_1 0x1 /* verify most significant 13 bits, ignore
  224. * the third least significant bit
  225. */
  226. #define SQ_13_IGNORE_2 0x2 /* verify most significant 13 bits, ignore
  227. * the second and third least significant bits
  228. */
  229. #define SQ_13_IGNORE_3 0x3 /* verify most significant 13 bits, ignore
  230. * the least three significant bits
  231. */
  232. /*
  233. * set SVT, SQ and SID fields of irte to verify
  234. * source ids of interrupt requests
  235. */
  236. static void set_irte_sid(struct irte *irte, unsigned int svt,
  237. unsigned int sq, unsigned int sid)
  238. {
  239. if (disable_sourceid_checking)
  240. svt = SVT_NO_VERIFY;
  241. irte->svt = svt;
  242. irte->sq = sq;
  243. irte->sid = sid;
  244. }
  245. /*
  246. * Set an IRTE to match only the bus number. Interrupt requests that reference
  247. * this IRTE must have a requester-id whose bus number is between or equal
  248. * to the start_bus and end_bus arguments.
  249. */
  250. static void set_irte_verify_bus(struct irte *irte, unsigned int start_bus,
  251. unsigned int end_bus)
  252. {
  253. set_irte_sid(irte, SVT_VERIFY_BUS, SQ_ALL_16,
  254. (start_bus << 8) | end_bus);
  255. }
  256. static int set_ioapic_sid(struct irte *irte, int apic)
  257. {
  258. int i;
  259. u16 sid = 0;
  260. if (!irte)
  261. return -1;
  262. down_read(&dmar_global_lock);
  263. for (i = 0; i < MAX_IO_APICS; i++) {
  264. if (ir_ioapic[i].iommu && ir_ioapic[i].id == apic) {
  265. sid = (ir_ioapic[i].bus << 8) | ir_ioapic[i].devfn;
  266. break;
  267. }
  268. }
  269. up_read(&dmar_global_lock);
  270. if (sid == 0) {
  271. pr_warn("Failed to set source-id of IOAPIC (%d)\n", apic);
  272. return -1;
  273. }
  274. set_irte_sid(irte, SVT_VERIFY_SID_SQ, SQ_ALL_16, sid);
  275. return 0;
  276. }
  277. static int set_hpet_sid(struct irte *irte, u8 id)
  278. {
  279. int i;
  280. u16 sid = 0;
  281. if (!irte)
  282. return -1;
  283. down_read(&dmar_global_lock);
  284. for (i = 0; i < MAX_HPET_TBS; i++) {
  285. if (ir_hpet[i].iommu && ir_hpet[i].id == id) {
  286. sid = (ir_hpet[i].bus << 8) | ir_hpet[i].devfn;
  287. break;
  288. }
  289. }
  290. up_read(&dmar_global_lock);
  291. if (sid == 0) {
  292. pr_warn("Failed to set source-id of HPET block (%d)\n", id);
  293. return -1;
  294. }
  295. /*
  296. * Should really use SQ_ALL_16. Some platforms are broken.
  297. * While we figure out the right quirks for these broken platforms, use
  298. * SQ_13_IGNORE_3 for now.
  299. */
  300. set_irte_sid(irte, SVT_VERIFY_SID_SQ, SQ_13_IGNORE_3, sid);
  301. return 0;
  302. }
  303. struct set_msi_sid_data {
  304. struct pci_dev *pdev;
  305. u16 alias;
  306. int count;
  307. int busmatch_count;
  308. };
  309. static int set_msi_sid_cb(struct pci_dev *pdev, u16 alias, void *opaque)
  310. {
  311. struct set_msi_sid_data *data = opaque;
  312. if (data->count == 0 || PCI_BUS_NUM(alias) == PCI_BUS_NUM(data->alias))
  313. data->busmatch_count++;
  314. data->pdev = pdev;
  315. data->alias = alias;
  316. data->count++;
  317. return 0;
  318. }
  319. static int set_msi_sid(struct irte *irte, struct pci_dev *dev)
  320. {
  321. struct set_msi_sid_data data;
  322. if (!irte || !dev)
  323. return -1;
  324. data.count = 0;
  325. data.busmatch_count = 0;
  326. pci_for_each_dma_alias(dev, set_msi_sid_cb, &data);
  327. /*
  328. * DMA alias provides us with a PCI device and alias. The only case
  329. * where the it will return an alias on a different bus than the
  330. * device is the case of a PCIe-to-PCI bridge, where the alias is for
  331. * the subordinate bus. In this case we can only verify the bus.
  332. *
  333. * If there are multiple aliases, all with the same bus number,
  334. * then all we can do is verify the bus. This is typical in NTB
  335. * hardware which use proxy IDs where the device will generate traffic
  336. * from multiple devfn numbers on the same bus.
  337. *
  338. * If the alias device is on a different bus than our source device
  339. * then we have a topology based alias, use it.
  340. *
  341. * Otherwise, the alias is for a device DMA quirk and we cannot
  342. * assume that MSI uses the same requester ID. Therefore use the
  343. * original device.
  344. */
  345. if (PCI_BUS_NUM(data.alias) != data.pdev->bus->number)
  346. set_irte_verify_bus(irte, PCI_BUS_NUM(data.alias),
  347. dev->bus->number);
  348. else if (data.count >= 2 && data.busmatch_count == data.count)
  349. set_irte_verify_bus(irte, dev->bus->number, dev->bus->number);
  350. else if (data.pdev->bus->number != dev->bus->number)
  351. set_irte_sid(irte, SVT_VERIFY_SID_SQ, SQ_ALL_16, data.alias);
  352. else
  353. set_irte_sid(irte, SVT_VERIFY_SID_SQ, SQ_ALL_16,
  354. pci_dev_id(dev));
  355. return 0;
  356. }
  357. static int iommu_load_old_irte(struct intel_iommu *iommu)
  358. {
  359. struct irte *old_ir_table;
  360. phys_addr_t irt_phys;
  361. unsigned int i;
  362. size_t size;
  363. u64 irta;
  364. /* Check whether the old ir-table has the same size as ours */
  365. irta = dmar_readq(iommu->reg + DMAR_IRTA_REG);
  366. if ((irta & INTR_REMAP_TABLE_REG_SIZE_MASK)
  367. != INTR_REMAP_TABLE_REG_SIZE)
  368. return -EINVAL;
  369. irt_phys = irta & VTD_PAGE_MASK;
  370. size = INTR_REMAP_TABLE_ENTRIES*sizeof(struct irte);
  371. /* Map the old IR table */
  372. old_ir_table = memremap(irt_phys, size, MEMREMAP_WB);
  373. if (!old_ir_table)
  374. return -ENOMEM;
  375. /* Copy data over */
  376. memcpy(iommu->ir_table->base, old_ir_table, size);
  377. __iommu_flush_cache(iommu, iommu->ir_table->base, size);
  378. /*
  379. * Now check the table for used entries and mark those as
  380. * allocated in the bitmap
  381. */
  382. for (i = 0; i < INTR_REMAP_TABLE_ENTRIES; i++) {
  383. if (iommu->ir_table->base[i].present)
  384. bitmap_set(iommu->ir_table->bitmap, i, 1);
  385. }
  386. memunmap(old_ir_table);
  387. return 0;
  388. }
  389. static void iommu_set_irq_remapping(struct intel_iommu *iommu, int mode)
  390. {
  391. unsigned long flags;
  392. u64 addr;
  393. u32 sts;
  394. addr = virt_to_phys((void *)iommu->ir_table->base);
  395. raw_spin_lock_irqsave(&iommu->register_lock, flags);
  396. dmar_writeq(iommu->reg + DMAR_IRTA_REG,
  397. (addr) | IR_X2APIC_MODE(mode) | INTR_REMAP_TABLE_REG_SIZE);
  398. /* Set interrupt-remapping table pointer */
  399. writel(iommu->gcmd | DMA_GCMD_SIRTP, iommu->reg + DMAR_GCMD_REG);
  400. IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
  401. readl, (sts & DMA_GSTS_IRTPS), sts);
  402. raw_spin_unlock_irqrestore(&iommu->register_lock, flags);
  403. /*
  404. * Global invalidation of interrupt entry cache to make sure the
  405. * hardware uses the new irq remapping table.
  406. */
  407. if (!cap_esirtps(iommu->cap))
  408. qi_global_iec(iommu);
  409. }
  410. static void iommu_enable_irq_remapping(struct intel_iommu *iommu)
  411. {
  412. unsigned long flags;
  413. u32 sts;
  414. raw_spin_lock_irqsave(&iommu->register_lock, flags);
  415. /* Enable interrupt-remapping */
  416. iommu->gcmd |= DMA_GCMD_IRE;
  417. writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
  418. IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
  419. readl, (sts & DMA_GSTS_IRES), sts);
  420. /* Block compatibility-format MSIs */
  421. if (sts & DMA_GSTS_CFIS) {
  422. iommu->gcmd &= ~DMA_GCMD_CFI;
  423. writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
  424. IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
  425. readl, !(sts & DMA_GSTS_CFIS), sts);
  426. }
  427. /*
  428. * With CFI clear in the Global Command register, we should be
  429. * protected from dangerous (i.e. compatibility) interrupts
  430. * regardless of x2apic status. Check just to be sure.
  431. */
  432. if (sts & DMA_GSTS_CFIS)
  433. WARN(1, KERN_WARNING
  434. "Compatibility-format IRQs enabled despite intr remapping;\n"
  435. "you are vulnerable to IRQ injection.\n");
  436. raw_spin_unlock_irqrestore(&iommu->register_lock, flags);
  437. }
  438. static int intel_setup_irq_remapping(struct intel_iommu *iommu)
  439. {
  440. struct ir_table *ir_table;
  441. struct fwnode_handle *fn;
  442. unsigned long *bitmap;
  443. struct page *pages;
  444. if (iommu->ir_table)
  445. return 0;
  446. ir_table = kzalloc(sizeof(struct ir_table), GFP_KERNEL);
  447. if (!ir_table)
  448. return -ENOMEM;
  449. pages = alloc_pages_node(iommu->node, GFP_KERNEL | __GFP_ZERO,
  450. INTR_REMAP_PAGE_ORDER);
  451. if (!pages) {
  452. pr_err("IR%d: failed to allocate pages of order %d\n",
  453. iommu->seq_id, INTR_REMAP_PAGE_ORDER);
  454. goto out_free_table;
  455. }
  456. bitmap = bitmap_zalloc(INTR_REMAP_TABLE_ENTRIES, GFP_ATOMIC);
  457. if (bitmap == NULL) {
  458. pr_err("IR%d: failed to allocate bitmap\n", iommu->seq_id);
  459. goto out_free_pages;
  460. }
  461. fn = irq_domain_alloc_named_id_fwnode("INTEL-IR", iommu->seq_id);
  462. if (!fn)
  463. goto out_free_bitmap;
  464. iommu->ir_domain =
  465. irq_domain_create_hierarchy(arch_get_ir_parent_domain(),
  466. 0, INTR_REMAP_TABLE_ENTRIES,
  467. fn, &intel_ir_domain_ops,
  468. iommu);
  469. if (!iommu->ir_domain) {
  470. pr_err("IR%d: failed to allocate irqdomain\n", iommu->seq_id);
  471. goto out_free_fwnode;
  472. }
  473. iommu->ir_msi_domain =
  474. arch_create_remap_msi_irq_domain(iommu->ir_domain,
  475. "INTEL-IR-MSI",
  476. iommu->seq_id);
  477. ir_table->base = page_address(pages);
  478. ir_table->bitmap = bitmap;
  479. iommu->ir_table = ir_table;
  480. /*
  481. * If the queued invalidation is already initialized,
  482. * shouldn't disable it.
  483. */
  484. if (!iommu->qi) {
  485. /*
  486. * Clear previous faults.
  487. */
  488. dmar_fault(-1, iommu);
  489. dmar_disable_qi(iommu);
  490. if (dmar_enable_qi(iommu)) {
  491. pr_err("Failed to enable queued invalidation\n");
  492. goto out_free_ir_domain;
  493. }
  494. }
  495. init_ir_status(iommu);
  496. if (ir_pre_enabled(iommu)) {
  497. if (!is_kdump_kernel()) {
  498. pr_warn("IRQ remapping was enabled on %s but we are not in kdump mode\n",
  499. iommu->name);
  500. clear_ir_pre_enabled(iommu);
  501. iommu_disable_irq_remapping(iommu);
  502. } else if (iommu_load_old_irte(iommu))
  503. pr_err("Failed to copy IR table for %s from previous kernel\n",
  504. iommu->name);
  505. else
  506. pr_info("Copied IR table for %s from previous kernel\n",
  507. iommu->name);
  508. }
  509. iommu_set_irq_remapping(iommu, eim_mode);
  510. return 0;
  511. out_free_ir_domain:
  512. if (iommu->ir_msi_domain)
  513. irq_domain_remove(iommu->ir_msi_domain);
  514. iommu->ir_msi_domain = NULL;
  515. irq_domain_remove(iommu->ir_domain);
  516. iommu->ir_domain = NULL;
  517. out_free_fwnode:
  518. irq_domain_free_fwnode(fn);
  519. out_free_bitmap:
  520. bitmap_free(bitmap);
  521. out_free_pages:
  522. __free_pages(pages, INTR_REMAP_PAGE_ORDER);
  523. out_free_table:
  524. kfree(ir_table);
  525. iommu->ir_table = NULL;
  526. return -ENOMEM;
  527. }
  528. static void intel_teardown_irq_remapping(struct intel_iommu *iommu)
  529. {
  530. struct fwnode_handle *fn;
  531. if (iommu && iommu->ir_table) {
  532. if (iommu->ir_msi_domain) {
  533. fn = iommu->ir_msi_domain->fwnode;
  534. irq_domain_remove(iommu->ir_msi_domain);
  535. irq_domain_free_fwnode(fn);
  536. iommu->ir_msi_domain = NULL;
  537. }
  538. if (iommu->ir_domain) {
  539. fn = iommu->ir_domain->fwnode;
  540. irq_domain_remove(iommu->ir_domain);
  541. irq_domain_free_fwnode(fn);
  542. iommu->ir_domain = NULL;
  543. }
  544. free_pages((unsigned long)iommu->ir_table->base,
  545. INTR_REMAP_PAGE_ORDER);
  546. bitmap_free(iommu->ir_table->bitmap);
  547. kfree(iommu->ir_table);
  548. iommu->ir_table = NULL;
  549. }
  550. }
  551. /*
  552. * Disable Interrupt Remapping.
  553. */
  554. static void iommu_disable_irq_remapping(struct intel_iommu *iommu)
  555. {
  556. unsigned long flags;
  557. u32 sts;
  558. if (!ecap_ir_support(iommu->ecap))
  559. return;
  560. /*
  561. * global invalidation of interrupt entry cache before disabling
  562. * interrupt-remapping.
  563. */
  564. if (!cap_esirtps(iommu->cap))
  565. qi_global_iec(iommu);
  566. raw_spin_lock_irqsave(&iommu->register_lock, flags);
  567. sts = readl(iommu->reg + DMAR_GSTS_REG);
  568. if (!(sts & DMA_GSTS_IRES))
  569. goto end;
  570. iommu->gcmd &= ~DMA_GCMD_IRE;
  571. writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
  572. IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
  573. readl, !(sts & DMA_GSTS_IRES), sts);
  574. end:
  575. raw_spin_unlock_irqrestore(&iommu->register_lock, flags);
  576. }
  577. static int __init dmar_x2apic_optout(void)
  578. {
  579. struct acpi_table_dmar *dmar;
  580. dmar = (struct acpi_table_dmar *)dmar_tbl;
  581. if (!dmar || no_x2apic_optout)
  582. return 0;
  583. return dmar->flags & DMAR_X2APIC_OPT_OUT;
  584. }
  585. static void __init intel_cleanup_irq_remapping(void)
  586. {
  587. struct dmar_drhd_unit *drhd;
  588. struct intel_iommu *iommu;
  589. for_each_iommu(iommu, drhd) {
  590. if (ecap_ir_support(iommu->ecap)) {
  591. iommu_disable_irq_remapping(iommu);
  592. intel_teardown_irq_remapping(iommu);
  593. }
  594. }
  595. if (x2apic_supported())
  596. pr_warn("Failed to enable irq remapping. You are vulnerable to irq-injection attacks.\n");
  597. }
  598. static int __init intel_prepare_irq_remapping(void)
  599. {
  600. struct dmar_drhd_unit *drhd;
  601. struct intel_iommu *iommu;
  602. int eim = 0;
  603. if (irq_remap_broken) {
  604. pr_warn("This system BIOS has enabled interrupt remapping\n"
  605. "on a chipset that contains an erratum making that\n"
  606. "feature unstable. To maintain system stability\n"
  607. "interrupt remapping is being disabled. Please\n"
  608. "contact your BIOS vendor for an update\n");
  609. add_taint(TAINT_FIRMWARE_WORKAROUND, LOCKDEP_STILL_OK);
  610. return -ENODEV;
  611. }
  612. if (dmar_table_init() < 0)
  613. return -ENODEV;
  614. if (intel_cap_audit(CAP_AUDIT_STATIC_IRQR, NULL))
  615. return -ENODEV;
  616. if (!dmar_ir_support())
  617. return -ENODEV;
  618. if (parse_ioapics_under_ir()) {
  619. pr_info("Not enabling interrupt remapping\n");
  620. goto error;
  621. }
  622. /* First make sure all IOMMUs support IRQ remapping */
  623. for_each_iommu(iommu, drhd)
  624. if (!ecap_ir_support(iommu->ecap))
  625. goto error;
  626. /* Detect remapping mode: lapic or x2apic */
  627. if (x2apic_supported()) {
  628. eim = !dmar_x2apic_optout();
  629. if (!eim) {
  630. pr_info("x2apic is disabled because BIOS sets x2apic opt out bit.");
  631. pr_info("Use 'intremap=no_x2apic_optout' to override the BIOS setting.\n");
  632. }
  633. }
  634. for_each_iommu(iommu, drhd) {
  635. if (eim && !ecap_eim_support(iommu->ecap)) {
  636. pr_info("%s does not support EIM\n", iommu->name);
  637. eim = 0;
  638. }
  639. }
  640. eim_mode = eim;
  641. if (eim)
  642. pr_info("Queued invalidation will be enabled to support x2apic and Intr-remapping.\n");
  643. /* Do the initializations early */
  644. for_each_iommu(iommu, drhd) {
  645. if (intel_setup_irq_remapping(iommu)) {
  646. pr_err("Failed to setup irq remapping for %s\n",
  647. iommu->name);
  648. goto error;
  649. }
  650. }
  651. return 0;
  652. error:
  653. intel_cleanup_irq_remapping();
  654. return -ENODEV;
  655. }
  656. /*
  657. * Set Posted-Interrupts capability.
  658. */
  659. static inline void set_irq_posting_cap(void)
  660. {
  661. struct dmar_drhd_unit *drhd;
  662. struct intel_iommu *iommu;
  663. if (!disable_irq_post) {
  664. /*
  665. * If IRTE is in posted format, the 'pda' field goes across the
  666. * 64-bit boundary, we need use cmpxchg16b to atomically update
  667. * it. We only expose posted-interrupt when X86_FEATURE_CX16
  668. * is supported. Actually, hardware platforms supporting PI
  669. * should have X86_FEATURE_CX16 support, this has been confirmed
  670. * with Intel hardware guys.
  671. */
  672. if (boot_cpu_has(X86_FEATURE_CX16))
  673. intel_irq_remap_ops.capability |= 1 << IRQ_POSTING_CAP;
  674. for_each_iommu(iommu, drhd)
  675. if (!cap_pi_support(iommu->cap)) {
  676. intel_irq_remap_ops.capability &=
  677. ~(1 << IRQ_POSTING_CAP);
  678. break;
  679. }
  680. }
  681. }
  682. static int __init intel_enable_irq_remapping(void)
  683. {
  684. struct dmar_drhd_unit *drhd;
  685. struct intel_iommu *iommu;
  686. bool setup = false;
  687. /*
  688. * Setup Interrupt-remapping for all the DRHD's now.
  689. */
  690. for_each_iommu(iommu, drhd) {
  691. if (!ir_pre_enabled(iommu))
  692. iommu_enable_irq_remapping(iommu);
  693. setup = true;
  694. }
  695. if (!setup)
  696. goto error;
  697. irq_remapping_enabled = 1;
  698. set_irq_posting_cap();
  699. pr_info("Enabled IRQ remapping in %s mode\n", eim_mode ? "x2apic" : "xapic");
  700. return eim_mode ? IRQ_REMAP_X2APIC_MODE : IRQ_REMAP_XAPIC_MODE;
  701. error:
  702. intel_cleanup_irq_remapping();
  703. return -1;
  704. }
  705. static int ir_parse_one_hpet_scope(struct acpi_dmar_device_scope *scope,
  706. struct intel_iommu *iommu,
  707. struct acpi_dmar_hardware_unit *drhd)
  708. {
  709. struct acpi_dmar_pci_path *path;
  710. u8 bus;
  711. int count, free = -1;
  712. bus = scope->bus;
  713. path = (struct acpi_dmar_pci_path *)(scope + 1);
  714. count = (scope->length - sizeof(struct acpi_dmar_device_scope))
  715. / sizeof(struct acpi_dmar_pci_path);
  716. while (--count > 0) {
  717. /*
  718. * Access PCI directly due to the PCI
  719. * subsystem isn't initialized yet.
  720. */
  721. bus = read_pci_config_byte(bus, path->device, path->function,
  722. PCI_SECONDARY_BUS);
  723. path++;
  724. }
  725. for (count = 0; count < MAX_HPET_TBS; count++) {
  726. if (ir_hpet[count].iommu == iommu &&
  727. ir_hpet[count].id == scope->enumeration_id)
  728. return 0;
  729. else if (ir_hpet[count].iommu == NULL && free == -1)
  730. free = count;
  731. }
  732. if (free == -1) {
  733. pr_warn("Exceeded Max HPET blocks\n");
  734. return -ENOSPC;
  735. }
  736. ir_hpet[free].iommu = iommu;
  737. ir_hpet[free].id = scope->enumeration_id;
  738. ir_hpet[free].bus = bus;
  739. ir_hpet[free].devfn = PCI_DEVFN(path->device, path->function);
  740. pr_info("HPET id %d under DRHD base 0x%Lx\n",
  741. scope->enumeration_id, drhd->address);
  742. return 0;
  743. }
  744. static int ir_parse_one_ioapic_scope(struct acpi_dmar_device_scope *scope,
  745. struct intel_iommu *iommu,
  746. struct acpi_dmar_hardware_unit *drhd)
  747. {
  748. struct acpi_dmar_pci_path *path;
  749. u8 bus;
  750. int count, free = -1;
  751. bus = scope->bus;
  752. path = (struct acpi_dmar_pci_path *)(scope + 1);
  753. count = (scope->length - sizeof(struct acpi_dmar_device_scope))
  754. / sizeof(struct acpi_dmar_pci_path);
  755. while (--count > 0) {
  756. /*
  757. * Access PCI directly due to the PCI
  758. * subsystem isn't initialized yet.
  759. */
  760. bus = read_pci_config_byte(bus, path->device, path->function,
  761. PCI_SECONDARY_BUS);
  762. path++;
  763. }
  764. for (count = 0; count < MAX_IO_APICS; count++) {
  765. if (ir_ioapic[count].iommu == iommu &&
  766. ir_ioapic[count].id == scope->enumeration_id)
  767. return 0;
  768. else if (ir_ioapic[count].iommu == NULL && free == -1)
  769. free = count;
  770. }
  771. if (free == -1) {
  772. pr_warn("Exceeded Max IO APICS\n");
  773. return -ENOSPC;
  774. }
  775. ir_ioapic[free].bus = bus;
  776. ir_ioapic[free].devfn = PCI_DEVFN(path->device, path->function);
  777. ir_ioapic[free].iommu = iommu;
  778. ir_ioapic[free].id = scope->enumeration_id;
  779. pr_info("IOAPIC id %d under DRHD base 0x%Lx IOMMU %d\n",
  780. scope->enumeration_id, drhd->address, iommu->seq_id);
  781. return 0;
  782. }
  783. static int ir_parse_ioapic_hpet_scope(struct acpi_dmar_header *header,
  784. struct intel_iommu *iommu)
  785. {
  786. int ret = 0;
  787. struct acpi_dmar_hardware_unit *drhd;
  788. struct acpi_dmar_device_scope *scope;
  789. void *start, *end;
  790. drhd = (struct acpi_dmar_hardware_unit *)header;
  791. start = (void *)(drhd + 1);
  792. end = ((void *)drhd) + header->length;
  793. while (start < end && ret == 0) {
  794. scope = start;
  795. if (scope->entry_type == ACPI_DMAR_SCOPE_TYPE_IOAPIC)
  796. ret = ir_parse_one_ioapic_scope(scope, iommu, drhd);
  797. else if (scope->entry_type == ACPI_DMAR_SCOPE_TYPE_HPET)
  798. ret = ir_parse_one_hpet_scope(scope, iommu, drhd);
  799. start += scope->length;
  800. }
  801. return ret;
  802. }
  803. static void ir_remove_ioapic_hpet_scope(struct intel_iommu *iommu)
  804. {
  805. int i;
  806. for (i = 0; i < MAX_HPET_TBS; i++)
  807. if (ir_hpet[i].iommu == iommu)
  808. ir_hpet[i].iommu = NULL;
  809. for (i = 0; i < MAX_IO_APICS; i++)
  810. if (ir_ioapic[i].iommu == iommu)
  811. ir_ioapic[i].iommu = NULL;
  812. }
  813. /*
  814. * Finds the assocaition between IOAPIC's and its Interrupt-remapping
  815. * hardware unit.
  816. */
  817. static int __init parse_ioapics_under_ir(void)
  818. {
  819. struct dmar_drhd_unit *drhd;
  820. struct intel_iommu *iommu;
  821. bool ir_supported = false;
  822. int ioapic_idx;
  823. for_each_iommu(iommu, drhd) {
  824. int ret;
  825. if (!ecap_ir_support(iommu->ecap))
  826. continue;
  827. ret = ir_parse_ioapic_hpet_scope(drhd->hdr, iommu);
  828. if (ret)
  829. return ret;
  830. ir_supported = true;
  831. }
  832. if (!ir_supported)
  833. return -ENODEV;
  834. for (ioapic_idx = 0; ioapic_idx < nr_ioapics; ioapic_idx++) {
  835. int ioapic_id = mpc_ioapic_id(ioapic_idx);
  836. if (!map_ioapic_to_iommu(ioapic_id)) {
  837. pr_err(FW_BUG "ioapic %d has no mapping iommu, "
  838. "interrupt remapping will be disabled\n",
  839. ioapic_id);
  840. return -1;
  841. }
  842. }
  843. return 0;
  844. }
  845. static int __init ir_dev_scope_init(void)
  846. {
  847. int ret;
  848. if (!irq_remapping_enabled)
  849. return 0;
  850. down_write(&dmar_global_lock);
  851. ret = dmar_dev_scope_init();
  852. up_write(&dmar_global_lock);
  853. return ret;
  854. }
  855. rootfs_initcall(ir_dev_scope_init);
  856. static void disable_irq_remapping(void)
  857. {
  858. struct dmar_drhd_unit *drhd;
  859. struct intel_iommu *iommu = NULL;
  860. /*
  861. * Disable Interrupt-remapping for all the DRHD's now.
  862. */
  863. for_each_iommu(iommu, drhd) {
  864. if (!ecap_ir_support(iommu->ecap))
  865. continue;
  866. iommu_disable_irq_remapping(iommu);
  867. }
  868. /*
  869. * Clear Posted-Interrupts capability.
  870. */
  871. if (!disable_irq_post)
  872. intel_irq_remap_ops.capability &= ~(1 << IRQ_POSTING_CAP);
  873. }
  874. static int reenable_irq_remapping(int eim)
  875. {
  876. struct dmar_drhd_unit *drhd;
  877. bool setup = false;
  878. struct intel_iommu *iommu = NULL;
  879. for_each_iommu(iommu, drhd)
  880. if (iommu->qi)
  881. dmar_reenable_qi(iommu);
  882. /*
  883. * Setup Interrupt-remapping for all the DRHD's now.
  884. */
  885. for_each_iommu(iommu, drhd) {
  886. if (!ecap_ir_support(iommu->ecap))
  887. continue;
  888. /* Set up interrupt remapping for iommu.*/
  889. iommu_set_irq_remapping(iommu, eim);
  890. iommu_enable_irq_remapping(iommu);
  891. setup = true;
  892. }
  893. if (!setup)
  894. goto error;
  895. set_irq_posting_cap();
  896. return 0;
  897. error:
  898. /*
  899. * handle error condition gracefully here!
  900. */
  901. return -1;
  902. }
  903. /*
  904. * Store the MSI remapping domain pointer in the device if enabled.
  905. *
  906. * This is called from dmar_pci_bus_add_dev() so it works even when DMA
  907. * remapping is disabled. Only update the pointer if the device is not
  908. * already handled by a non default PCI/MSI interrupt domain. This protects
  909. * e.g. VMD devices.
  910. */
  911. void intel_irq_remap_add_device(struct dmar_pci_notify_info *info)
  912. {
  913. if (!irq_remapping_enabled || pci_dev_has_special_msi_domain(info->dev))
  914. return;
  915. dev_set_msi_domain(&info->dev->dev, map_dev_to_ir(info->dev));
  916. }
  917. static void prepare_irte(struct irte *irte, int vector, unsigned int dest)
  918. {
  919. memset(irte, 0, sizeof(*irte));
  920. irte->present = 1;
  921. irte->dst_mode = apic->dest_mode_logical;
  922. /*
  923. * Trigger mode in the IRTE will always be edge, and for IO-APIC, the
  924. * actual level or edge trigger will be setup in the IO-APIC
  925. * RTE. This will help simplify level triggered irq migration.
  926. * For more details, see the comments (in io_apic.c) explainig IO-APIC
  927. * irq migration in the presence of interrupt-remapping.
  928. */
  929. irte->trigger_mode = 0;
  930. irte->dlvry_mode = apic->delivery_mode;
  931. irte->vector = vector;
  932. irte->dest_id = IRTE_DEST(dest);
  933. irte->redir_hint = 1;
  934. }
  935. struct irq_remap_ops intel_irq_remap_ops = {
  936. .prepare = intel_prepare_irq_remapping,
  937. .enable = intel_enable_irq_remapping,
  938. .disable = disable_irq_remapping,
  939. .reenable = reenable_irq_remapping,
  940. .enable_faulting = enable_drhd_fault_handling,
  941. };
  942. static void intel_ir_reconfigure_irte(struct irq_data *irqd, bool force)
  943. {
  944. struct intel_ir_data *ir_data = irqd->chip_data;
  945. struct irte *irte = &ir_data->irte_entry;
  946. struct irq_cfg *cfg = irqd_cfg(irqd);
  947. /*
  948. * Atomically updates the IRTE with the new destination, vector
  949. * and flushes the interrupt entry cache.
  950. */
  951. irte->vector = cfg->vector;
  952. irte->dest_id = IRTE_DEST(cfg->dest_apicid);
  953. /* Update the hardware only if the interrupt is in remapped mode. */
  954. if (force || ir_data->irq_2_iommu.mode == IRQ_REMAPPING)
  955. modify_irte(&ir_data->irq_2_iommu, irte);
  956. }
  957. /*
  958. * Migrate the IO-APIC irq in the presence of intr-remapping.
  959. *
  960. * For both level and edge triggered, irq migration is a simple atomic
  961. * update(of vector and cpu destination) of IRTE and flush the hardware cache.
  962. *
  963. * For level triggered, we eliminate the io-apic RTE modification (with the
  964. * updated vector information), by using a virtual vector (io-apic pin number).
  965. * Real vector that is used for interrupting cpu will be coming from
  966. * the interrupt-remapping table entry.
  967. *
  968. * As the migration is a simple atomic update of IRTE, the same mechanism
  969. * is used to migrate MSI irq's in the presence of interrupt-remapping.
  970. */
  971. static int
  972. intel_ir_set_affinity(struct irq_data *data, const struct cpumask *mask,
  973. bool force)
  974. {
  975. struct irq_data *parent = data->parent_data;
  976. struct irq_cfg *cfg = irqd_cfg(data);
  977. int ret;
  978. ret = parent->chip->irq_set_affinity(parent, mask, force);
  979. if (ret < 0 || ret == IRQ_SET_MASK_OK_DONE)
  980. return ret;
  981. intel_ir_reconfigure_irte(data, false);
  982. /*
  983. * After this point, all the interrupts will start arriving
  984. * at the new destination. So, time to cleanup the previous
  985. * vector allocation.
  986. */
  987. send_cleanup_vector(cfg);
  988. return IRQ_SET_MASK_OK_DONE;
  989. }
  990. static void intel_ir_compose_msi_msg(struct irq_data *irq_data,
  991. struct msi_msg *msg)
  992. {
  993. struct intel_ir_data *ir_data = irq_data->chip_data;
  994. *msg = ir_data->msi_entry;
  995. }
  996. static int intel_ir_set_vcpu_affinity(struct irq_data *data, void *info)
  997. {
  998. struct intel_ir_data *ir_data = data->chip_data;
  999. struct vcpu_data *vcpu_pi_info = info;
  1000. /* stop posting interrupts, back to remapping mode */
  1001. if (!vcpu_pi_info) {
  1002. modify_irte(&ir_data->irq_2_iommu, &ir_data->irte_entry);
  1003. } else {
  1004. struct irte irte_pi;
  1005. /*
  1006. * We are not caching the posted interrupt entry. We
  1007. * copy the data from the remapped entry and modify
  1008. * the fields which are relevant for posted mode. The
  1009. * cached remapped entry is used for switching back to
  1010. * remapped mode.
  1011. */
  1012. memset(&irte_pi, 0, sizeof(irte_pi));
  1013. dmar_copy_shared_irte(&irte_pi, &ir_data->irte_entry);
  1014. /* Update the posted mode fields */
  1015. irte_pi.p_pst = 1;
  1016. irte_pi.p_urgent = 0;
  1017. irte_pi.p_vector = vcpu_pi_info->vector;
  1018. irte_pi.pda_l = (vcpu_pi_info->pi_desc_addr >>
  1019. (32 - PDA_LOW_BIT)) & ~(-1UL << PDA_LOW_BIT);
  1020. irte_pi.pda_h = (vcpu_pi_info->pi_desc_addr >> 32) &
  1021. ~(-1UL << PDA_HIGH_BIT);
  1022. modify_irte(&ir_data->irq_2_iommu, &irte_pi);
  1023. }
  1024. return 0;
  1025. }
  1026. static struct irq_chip intel_ir_chip = {
  1027. .name = "INTEL-IR",
  1028. .irq_ack = apic_ack_irq,
  1029. .irq_set_affinity = intel_ir_set_affinity,
  1030. .irq_compose_msi_msg = intel_ir_compose_msi_msg,
  1031. .irq_set_vcpu_affinity = intel_ir_set_vcpu_affinity,
  1032. };
  1033. static void fill_msi_msg(struct msi_msg *msg, u32 index, u32 subhandle)
  1034. {
  1035. memset(msg, 0, sizeof(*msg));
  1036. msg->arch_addr_lo.dmar_base_address = X86_MSI_BASE_ADDRESS_LOW;
  1037. msg->arch_addr_lo.dmar_subhandle_valid = true;
  1038. msg->arch_addr_lo.dmar_format = true;
  1039. msg->arch_addr_lo.dmar_index_0_14 = index & 0x7FFF;
  1040. msg->arch_addr_lo.dmar_index_15 = !!(index & 0x8000);
  1041. msg->address_hi = X86_MSI_BASE_ADDRESS_HIGH;
  1042. msg->arch_data.dmar_subhandle = subhandle;
  1043. }
  1044. static void intel_irq_remapping_prepare_irte(struct intel_ir_data *data,
  1045. struct irq_cfg *irq_cfg,
  1046. struct irq_alloc_info *info,
  1047. int index, int sub_handle)
  1048. {
  1049. struct irte *irte = &data->irte_entry;
  1050. prepare_irte(irte, irq_cfg->vector, irq_cfg->dest_apicid);
  1051. switch (info->type) {
  1052. case X86_IRQ_ALLOC_TYPE_IOAPIC:
  1053. /* Set source-id of interrupt request */
  1054. set_ioapic_sid(irte, info->devid);
  1055. apic_printk(APIC_VERBOSE, KERN_DEBUG "IOAPIC[%d]: Set IRTE entry (P:%d FPD:%d Dst_Mode:%d Redir_hint:%d Trig_Mode:%d Dlvry_Mode:%X Avail:%X Vector:%02X Dest:%08X SID:%04X SQ:%X SVT:%X)\n",
  1056. info->devid, irte->present, irte->fpd,
  1057. irte->dst_mode, irte->redir_hint,
  1058. irte->trigger_mode, irte->dlvry_mode,
  1059. irte->avail, irte->vector, irte->dest_id,
  1060. irte->sid, irte->sq, irte->svt);
  1061. sub_handle = info->ioapic.pin;
  1062. break;
  1063. case X86_IRQ_ALLOC_TYPE_HPET:
  1064. set_hpet_sid(irte, info->devid);
  1065. break;
  1066. case X86_IRQ_ALLOC_TYPE_PCI_MSI:
  1067. case X86_IRQ_ALLOC_TYPE_PCI_MSIX:
  1068. set_msi_sid(irte,
  1069. pci_real_dma_dev(msi_desc_to_pci_dev(info->desc)));
  1070. break;
  1071. default:
  1072. BUG_ON(1);
  1073. break;
  1074. }
  1075. fill_msi_msg(&data->msi_entry, index, sub_handle);
  1076. }
  1077. static void intel_free_irq_resources(struct irq_domain *domain,
  1078. unsigned int virq, unsigned int nr_irqs)
  1079. {
  1080. struct irq_data *irq_data;
  1081. struct intel_ir_data *data;
  1082. struct irq_2_iommu *irq_iommu;
  1083. unsigned long flags;
  1084. int i;
  1085. for (i = 0; i < nr_irqs; i++) {
  1086. irq_data = irq_domain_get_irq_data(domain, virq + i);
  1087. if (irq_data && irq_data->chip_data) {
  1088. data = irq_data->chip_data;
  1089. irq_iommu = &data->irq_2_iommu;
  1090. raw_spin_lock_irqsave(&irq_2_ir_lock, flags);
  1091. clear_entries(irq_iommu);
  1092. raw_spin_unlock_irqrestore(&irq_2_ir_lock, flags);
  1093. irq_domain_reset_irq_data(irq_data);
  1094. kfree(data);
  1095. }
  1096. }
  1097. }
  1098. static int intel_irq_remapping_alloc(struct irq_domain *domain,
  1099. unsigned int virq, unsigned int nr_irqs,
  1100. void *arg)
  1101. {
  1102. struct intel_iommu *iommu = domain->host_data;
  1103. struct irq_alloc_info *info = arg;
  1104. struct intel_ir_data *data, *ird;
  1105. struct irq_data *irq_data;
  1106. struct irq_cfg *irq_cfg;
  1107. int i, ret, index;
  1108. if (!info || !iommu)
  1109. return -EINVAL;
  1110. if (nr_irqs > 1 && info->type != X86_IRQ_ALLOC_TYPE_PCI_MSI &&
  1111. info->type != X86_IRQ_ALLOC_TYPE_PCI_MSIX)
  1112. return -EINVAL;
  1113. /*
  1114. * With IRQ remapping enabled, don't need contiguous CPU vectors
  1115. * to support multiple MSI interrupts.
  1116. */
  1117. if (info->type == X86_IRQ_ALLOC_TYPE_PCI_MSI)
  1118. info->flags &= ~X86_IRQ_ALLOC_CONTIGUOUS_VECTORS;
  1119. ret = irq_domain_alloc_irqs_parent(domain, virq, nr_irqs, arg);
  1120. if (ret < 0)
  1121. return ret;
  1122. ret = -ENOMEM;
  1123. data = kzalloc(sizeof(*data), GFP_KERNEL);
  1124. if (!data)
  1125. goto out_free_parent;
  1126. down_read(&dmar_global_lock);
  1127. index = alloc_irte(iommu, &data->irq_2_iommu, nr_irqs);
  1128. up_read(&dmar_global_lock);
  1129. if (index < 0) {
  1130. pr_warn("Failed to allocate IRTE\n");
  1131. kfree(data);
  1132. goto out_free_parent;
  1133. }
  1134. for (i = 0; i < nr_irqs; i++) {
  1135. irq_data = irq_domain_get_irq_data(domain, virq + i);
  1136. irq_cfg = irqd_cfg(irq_data);
  1137. if (!irq_data || !irq_cfg) {
  1138. if (!i)
  1139. kfree(data);
  1140. ret = -EINVAL;
  1141. goto out_free_data;
  1142. }
  1143. if (i > 0) {
  1144. ird = kzalloc(sizeof(*ird), GFP_KERNEL);
  1145. if (!ird)
  1146. goto out_free_data;
  1147. /* Initialize the common data */
  1148. ird->irq_2_iommu = data->irq_2_iommu;
  1149. ird->irq_2_iommu.sub_handle = i;
  1150. } else {
  1151. ird = data;
  1152. }
  1153. irq_data->hwirq = (index << 16) + i;
  1154. irq_data->chip_data = ird;
  1155. irq_data->chip = &intel_ir_chip;
  1156. intel_irq_remapping_prepare_irte(ird, irq_cfg, info, index, i);
  1157. irq_set_status_flags(virq + i, IRQ_MOVE_PCNTXT);
  1158. }
  1159. return 0;
  1160. out_free_data:
  1161. intel_free_irq_resources(domain, virq, i);
  1162. out_free_parent:
  1163. irq_domain_free_irqs_common(domain, virq, nr_irqs);
  1164. return ret;
  1165. }
  1166. static void intel_irq_remapping_free(struct irq_domain *domain,
  1167. unsigned int virq, unsigned int nr_irqs)
  1168. {
  1169. intel_free_irq_resources(domain, virq, nr_irqs);
  1170. irq_domain_free_irqs_common(domain, virq, nr_irqs);
  1171. }
  1172. static int intel_irq_remapping_activate(struct irq_domain *domain,
  1173. struct irq_data *irq_data, bool reserve)
  1174. {
  1175. intel_ir_reconfigure_irte(irq_data, true);
  1176. return 0;
  1177. }
  1178. static void intel_irq_remapping_deactivate(struct irq_domain *domain,
  1179. struct irq_data *irq_data)
  1180. {
  1181. struct intel_ir_data *data = irq_data->chip_data;
  1182. struct irte entry;
  1183. memset(&entry, 0, sizeof(entry));
  1184. modify_irte(&data->irq_2_iommu, &entry);
  1185. }
  1186. static int intel_irq_remapping_select(struct irq_domain *d,
  1187. struct irq_fwspec *fwspec,
  1188. enum irq_domain_bus_token bus_token)
  1189. {
  1190. struct intel_iommu *iommu = NULL;
  1191. if (x86_fwspec_is_ioapic(fwspec))
  1192. iommu = map_ioapic_to_iommu(fwspec->param[0]);
  1193. else if (x86_fwspec_is_hpet(fwspec))
  1194. iommu = map_hpet_to_iommu(fwspec->param[0]);
  1195. return iommu && d == iommu->ir_domain;
  1196. }
  1197. static const struct irq_domain_ops intel_ir_domain_ops = {
  1198. .select = intel_irq_remapping_select,
  1199. .alloc = intel_irq_remapping_alloc,
  1200. .free = intel_irq_remapping_free,
  1201. .activate = intel_irq_remapping_activate,
  1202. .deactivate = intel_irq_remapping_deactivate,
  1203. };
  1204. /*
  1205. * Support of Interrupt Remapping Unit Hotplug
  1206. */
  1207. static int dmar_ir_add(struct dmar_drhd_unit *dmaru, struct intel_iommu *iommu)
  1208. {
  1209. int ret;
  1210. int eim = x2apic_enabled();
  1211. ret = intel_cap_audit(CAP_AUDIT_HOTPLUG_IRQR, iommu);
  1212. if (ret)
  1213. return ret;
  1214. if (eim && !ecap_eim_support(iommu->ecap)) {
  1215. pr_info("DRHD %Lx: EIM not supported by DRHD, ecap %Lx\n",
  1216. iommu->reg_phys, iommu->ecap);
  1217. return -ENODEV;
  1218. }
  1219. if (ir_parse_ioapic_hpet_scope(dmaru->hdr, iommu)) {
  1220. pr_warn("DRHD %Lx: failed to parse managed IOAPIC/HPET\n",
  1221. iommu->reg_phys);
  1222. return -ENODEV;
  1223. }
  1224. /* TODO: check all IOAPICs are covered by IOMMU */
  1225. /* Setup Interrupt-remapping now. */
  1226. ret = intel_setup_irq_remapping(iommu);
  1227. if (ret) {
  1228. pr_err("Failed to setup irq remapping for %s\n",
  1229. iommu->name);
  1230. intel_teardown_irq_remapping(iommu);
  1231. ir_remove_ioapic_hpet_scope(iommu);
  1232. } else {
  1233. iommu_enable_irq_remapping(iommu);
  1234. }
  1235. return ret;
  1236. }
  1237. int dmar_ir_hotplug(struct dmar_drhd_unit *dmaru, bool insert)
  1238. {
  1239. int ret = 0;
  1240. struct intel_iommu *iommu = dmaru->iommu;
  1241. if (!irq_remapping_enabled)
  1242. return 0;
  1243. if (iommu == NULL)
  1244. return -EINVAL;
  1245. if (!ecap_ir_support(iommu->ecap))
  1246. return 0;
  1247. if (irq_remapping_cap(IRQ_POSTING_CAP) &&
  1248. !cap_pi_support(iommu->cap))
  1249. return -EBUSY;
  1250. if (insert) {
  1251. if (!iommu->ir_table)
  1252. ret = dmar_ir_add(dmaru, iommu);
  1253. } else {
  1254. if (iommu->ir_table) {
  1255. if (!bitmap_empty(iommu->ir_table->bitmap,
  1256. INTR_REMAP_TABLE_ENTRIES)) {
  1257. ret = -EBUSY;
  1258. } else {
  1259. iommu_disable_irq_remapping(iommu);
  1260. intel_teardown_irq_remapping(iommu);
  1261. ir_remove_ioapic_hpet_scope(iommu);
  1262. }
  1263. }
  1264. }
  1265. return ret;
  1266. }