amd_iommu_init.c 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078
  1. /*
  2. * Copyright (C) 2007-2010 Advanced Micro Devices, Inc.
  3. * Author: Joerg Roedel <joerg.roedel@amd.com>
  4. * Leo Duran <leo.duran@amd.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License version 2 as published
  8. * by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include <linux/pci.h>
  20. #include <linux/acpi.h>
  21. #include <linux/list.h>
  22. #include <linux/slab.h>
  23. #include <linux/syscore_ops.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/msi.h>
  26. #include <linux/amd-iommu.h>
  27. #include <linux/export.h>
  28. #include <acpi/acpi.h>
  29. #include <asm/pci-direct.h>
  30. #include <asm/iommu.h>
  31. #include <asm/gart.h>
  32. #include <asm/x86_init.h>
  33. #include <asm/iommu_table.h>
  34. #include <asm/io_apic.h>
  35. #include <asm/irq_remapping.h>
  36. #include "amd_iommu_proto.h"
  37. #include "amd_iommu_types.h"
  38. #include "irq_remapping.h"
  39. /*
  40. * definitions for the ACPI scanning code
  41. */
  42. #define IVRS_HEADER_LENGTH 48
  43. #define ACPI_IVHD_TYPE 0x10
  44. #define ACPI_IVMD_TYPE_ALL 0x20
  45. #define ACPI_IVMD_TYPE 0x21
  46. #define ACPI_IVMD_TYPE_RANGE 0x22
  47. #define IVHD_DEV_ALL 0x01
  48. #define IVHD_DEV_SELECT 0x02
  49. #define IVHD_DEV_SELECT_RANGE_START 0x03
  50. #define IVHD_DEV_RANGE_END 0x04
  51. #define IVHD_DEV_ALIAS 0x42
  52. #define IVHD_DEV_ALIAS_RANGE 0x43
  53. #define IVHD_DEV_EXT_SELECT 0x46
  54. #define IVHD_DEV_EXT_SELECT_RANGE 0x47
  55. #define IVHD_DEV_SPECIAL 0x48
  56. #define IVHD_SPECIAL_IOAPIC 1
  57. #define IVHD_SPECIAL_HPET 2
  58. #define IVHD_FLAG_HT_TUN_EN_MASK 0x01
  59. #define IVHD_FLAG_PASSPW_EN_MASK 0x02
  60. #define IVHD_FLAG_RESPASSPW_EN_MASK 0x04
  61. #define IVHD_FLAG_ISOC_EN_MASK 0x08
  62. #define IVMD_FLAG_EXCL_RANGE 0x08
  63. #define IVMD_FLAG_UNITY_MAP 0x01
  64. #define ACPI_DEVFLAG_INITPASS 0x01
  65. #define ACPI_DEVFLAG_EXTINT 0x02
  66. #define ACPI_DEVFLAG_NMI 0x04
  67. #define ACPI_DEVFLAG_SYSMGT1 0x10
  68. #define ACPI_DEVFLAG_SYSMGT2 0x20
  69. #define ACPI_DEVFLAG_LINT0 0x40
  70. #define ACPI_DEVFLAG_LINT1 0x80
  71. #define ACPI_DEVFLAG_ATSDIS 0x10000000
  72. /*
  73. * ACPI table definitions
  74. *
  75. * These data structures are laid over the table to parse the important values
  76. * out of it.
  77. */
  78. /*
  79. * structure describing one IOMMU in the ACPI table. Typically followed by one
  80. * or more ivhd_entrys.
  81. */
  82. struct ivhd_header {
  83. u8 type;
  84. u8 flags;
  85. u16 length;
  86. u16 devid;
  87. u16 cap_ptr;
  88. u64 mmio_phys;
  89. u16 pci_seg;
  90. u16 info;
  91. u32 reserved;
  92. } __attribute__((packed));
  93. /*
  94. * A device entry describing which devices a specific IOMMU translates and
  95. * which requestor ids they use.
  96. */
  97. struct ivhd_entry {
  98. u8 type;
  99. u16 devid;
  100. u8 flags;
  101. u32 ext;
  102. } __attribute__((packed));
  103. /*
  104. * An AMD IOMMU memory definition structure. It defines things like exclusion
  105. * ranges for devices and regions that should be unity mapped.
  106. */
  107. struct ivmd_header {
  108. u8 type;
  109. u8 flags;
  110. u16 length;
  111. u16 devid;
  112. u16 aux;
  113. u64 resv;
  114. u64 range_start;
  115. u64 range_length;
  116. } __attribute__((packed));
  117. bool amd_iommu_dump;
  118. bool amd_iommu_irq_remap __read_mostly;
  119. static bool amd_iommu_detected;
  120. static bool __initdata amd_iommu_disabled;
  121. u16 amd_iommu_last_bdf; /* largest PCI device id we have
  122. to handle */
  123. LIST_HEAD(amd_iommu_unity_map); /* a list of required unity mappings
  124. we find in ACPI */
  125. u32 amd_iommu_unmap_flush; /* if true, flush on every unmap */
  126. LIST_HEAD(amd_iommu_list); /* list of all AMD IOMMUs in the
  127. system */
  128. /* Array to assign indices to IOMMUs*/
  129. struct amd_iommu *amd_iommus[MAX_IOMMUS];
  130. int amd_iommus_present;
  131. /* IOMMUs have a non-present cache? */
  132. bool amd_iommu_np_cache __read_mostly;
  133. bool amd_iommu_iotlb_sup __read_mostly = true;
  134. u32 amd_iommu_max_pasids __read_mostly = ~0;
  135. bool amd_iommu_v2_present __read_mostly;
  136. bool amd_iommu_force_isolation __read_mostly;
  137. /*
  138. * List of protection domains - used during resume
  139. */
  140. LIST_HEAD(amd_iommu_pd_list);
  141. spinlock_t amd_iommu_pd_lock;
  142. /*
  143. * Pointer to the device table which is shared by all AMD IOMMUs
  144. * it is indexed by the PCI device id or the HT unit id and contains
  145. * information about the domain the device belongs to as well as the
  146. * page table root pointer.
  147. */
  148. struct dev_table_entry *amd_iommu_dev_table;
  149. /*
  150. * The alias table is a driver specific data structure which contains the
  151. * mappings of the PCI device ids to the actual requestor ids on the IOMMU.
  152. * More than one device can share the same requestor id.
  153. */
  154. u16 *amd_iommu_alias_table;
  155. /*
  156. * The rlookup table is used to find the IOMMU which is responsible
  157. * for a specific device. It is also indexed by the PCI device id.
  158. */
  159. struct amd_iommu **amd_iommu_rlookup_table;
  160. /*
  161. * This table is used to find the irq remapping table for a given device id
  162. * quickly.
  163. */
  164. struct irq_remap_table **irq_lookup_table;
  165. /*
  166. * AMD IOMMU allows up to 2^16 different protection domains. This is a bitmap
  167. * to know which ones are already in use.
  168. */
  169. unsigned long *amd_iommu_pd_alloc_bitmap;
  170. static u32 dev_table_size; /* size of the device table */
  171. static u32 alias_table_size; /* size of the alias table */
  172. static u32 rlookup_table_size; /* size if the rlookup table */
  173. enum iommu_init_state {
  174. IOMMU_START_STATE,
  175. IOMMU_IVRS_DETECTED,
  176. IOMMU_ACPI_FINISHED,
  177. IOMMU_ENABLED,
  178. IOMMU_PCI_INIT,
  179. IOMMU_INTERRUPTS_EN,
  180. IOMMU_DMA_OPS,
  181. IOMMU_INITIALIZED,
  182. IOMMU_NOT_FOUND,
  183. IOMMU_INIT_ERROR,
  184. };
  185. static enum iommu_init_state init_state = IOMMU_START_STATE;
  186. static int amd_iommu_enable_interrupts(void);
  187. static int __init iommu_go_to_state(enum iommu_init_state state);
  188. static inline void update_last_devid(u16 devid)
  189. {
  190. if (devid > amd_iommu_last_bdf)
  191. amd_iommu_last_bdf = devid;
  192. }
  193. static inline unsigned long tbl_size(int entry_size)
  194. {
  195. unsigned shift = PAGE_SHIFT +
  196. get_order(((int)amd_iommu_last_bdf + 1) * entry_size);
  197. return 1UL << shift;
  198. }
  199. /* Access to l1 and l2 indexed register spaces */
  200. static u32 iommu_read_l1(struct amd_iommu *iommu, u16 l1, u8 address)
  201. {
  202. u32 val;
  203. pci_write_config_dword(iommu->dev, 0xf8, (address | l1 << 16));
  204. pci_read_config_dword(iommu->dev, 0xfc, &val);
  205. return val;
  206. }
  207. static void iommu_write_l1(struct amd_iommu *iommu, u16 l1, u8 address, u32 val)
  208. {
  209. pci_write_config_dword(iommu->dev, 0xf8, (address | l1 << 16 | 1 << 31));
  210. pci_write_config_dword(iommu->dev, 0xfc, val);
  211. pci_write_config_dword(iommu->dev, 0xf8, (address | l1 << 16));
  212. }
  213. static u32 iommu_read_l2(struct amd_iommu *iommu, u8 address)
  214. {
  215. u32 val;
  216. pci_write_config_dword(iommu->dev, 0xf0, address);
  217. pci_read_config_dword(iommu->dev, 0xf4, &val);
  218. return val;
  219. }
  220. static void iommu_write_l2(struct amd_iommu *iommu, u8 address, u32 val)
  221. {
  222. pci_write_config_dword(iommu->dev, 0xf0, (address | 1 << 8));
  223. pci_write_config_dword(iommu->dev, 0xf4, val);
  224. }
  225. /****************************************************************************
  226. *
  227. * AMD IOMMU MMIO register space handling functions
  228. *
  229. * These functions are used to program the IOMMU device registers in
  230. * MMIO space required for that driver.
  231. *
  232. ****************************************************************************/
  233. /*
  234. * This function set the exclusion range in the IOMMU. DMA accesses to the
  235. * exclusion range are passed through untranslated
  236. */
  237. static void iommu_set_exclusion_range(struct amd_iommu *iommu)
  238. {
  239. u64 start = iommu->exclusion_start & PAGE_MASK;
  240. u64 limit = (start + iommu->exclusion_length) & PAGE_MASK;
  241. u64 entry;
  242. if (!iommu->exclusion_start)
  243. return;
  244. entry = start | MMIO_EXCL_ENABLE_MASK;
  245. memcpy_toio(iommu->mmio_base + MMIO_EXCL_BASE_OFFSET,
  246. &entry, sizeof(entry));
  247. entry = limit;
  248. memcpy_toio(iommu->mmio_base + MMIO_EXCL_LIMIT_OFFSET,
  249. &entry, sizeof(entry));
  250. }
  251. /* Programs the physical address of the device table into the IOMMU hardware */
  252. static void iommu_set_device_table(struct amd_iommu *iommu)
  253. {
  254. u64 entry;
  255. BUG_ON(iommu->mmio_base == NULL);
  256. entry = virt_to_phys(amd_iommu_dev_table);
  257. entry |= (dev_table_size >> 12) - 1;
  258. memcpy_toio(iommu->mmio_base + MMIO_DEV_TABLE_OFFSET,
  259. &entry, sizeof(entry));
  260. }
  261. /* Generic functions to enable/disable certain features of the IOMMU. */
  262. static void iommu_feature_enable(struct amd_iommu *iommu, u8 bit)
  263. {
  264. u32 ctrl;
  265. ctrl = readl(iommu->mmio_base + MMIO_CONTROL_OFFSET);
  266. ctrl |= (1 << bit);
  267. writel(ctrl, iommu->mmio_base + MMIO_CONTROL_OFFSET);
  268. }
  269. static void iommu_feature_disable(struct amd_iommu *iommu, u8 bit)
  270. {
  271. u32 ctrl;
  272. ctrl = readl(iommu->mmio_base + MMIO_CONTROL_OFFSET);
  273. ctrl &= ~(1 << bit);
  274. writel(ctrl, iommu->mmio_base + MMIO_CONTROL_OFFSET);
  275. }
  276. static void iommu_set_inv_tlb_timeout(struct amd_iommu *iommu, int timeout)
  277. {
  278. u32 ctrl;
  279. ctrl = readl(iommu->mmio_base + MMIO_CONTROL_OFFSET);
  280. ctrl &= ~CTRL_INV_TO_MASK;
  281. ctrl |= (timeout << CONTROL_INV_TIMEOUT) & CTRL_INV_TO_MASK;
  282. writel(ctrl, iommu->mmio_base + MMIO_CONTROL_OFFSET);
  283. }
  284. /* Function to enable the hardware */
  285. static void iommu_enable(struct amd_iommu *iommu)
  286. {
  287. iommu_feature_enable(iommu, CONTROL_IOMMU_EN);
  288. }
  289. static void iommu_disable(struct amd_iommu *iommu)
  290. {
  291. /* Disable command buffer */
  292. iommu_feature_disable(iommu, CONTROL_CMDBUF_EN);
  293. /* Disable event logging and event interrupts */
  294. iommu_feature_disable(iommu, CONTROL_EVT_INT_EN);
  295. iommu_feature_disable(iommu, CONTROL_EVT_LOG_EN);
  296. /* Disable IOMMU hardware itself */
  297. iommu_feature_disable(iommu, CONTROL_IOMMU_EN);
  298. }
  299. /*
  300. * mapping and unmapping functions for the IOMMU MMIO space. Each AMD IOMMU in
  301. * the system has one.
  302. */
  303. static u8 __iomem * __init iommu_map_mmio_space(u64 address)
  304. {
  305. if (!request_mem_region(address, MMIO_REGION_LENGTH, "amd_iommu")) {
  306. pr_err("AMD-Vi: Can not reserve memory region %llx for mmio\n",
  307. address);
  308. pr_err("AMD-Vi: This is a BIOS bug. Please contact your hardware vendor\n");
  309. return NULL;
  310. }
  311. return (u8 __iomem *)ioremap_nocache(address, MMIO_REGION_LENGTH);
  312. }
  313. static void __init iommu_unmap_mmio_space(struct amd_iommu *iommu)
  314. {
  315. if (iommu->mmio_base)
  316. iounmap(iommu->mmio_base);
  317. release_mem_region(iommu->mmio_phys, MMIO_REGION_LENGTH);
  318. }
  319. /****************************************************************************
  320. *
  321. * The functions below belong to the first pass of AMD IOMMU ACPI table
  322. * parsing. In this pass we try to find out the highest device id this
  323. * code has to handle. Upon this information the size of the shared data
  324. * structures is determined later.
  325. *
  326. ****************************************************************************/
  327. /*
  328. * This function calculates the length of a given IVHD entry
  329. */
  330. static inline int ivhd_entry_length(u8 *ivhd)
  331. {
  332. return 0x04 << (*ivhd >> 6);
  333. }
  334. /*
  335. * This function reads the last device id the IOMMU has to handle from the PCI
  336. * capability header for this IOMMU
  337. */
  338. static int __init find_last_devid_on_pci(int bus, int dev, int fn, int cap_ptr)
  339. {
  340. u32 cap;
  341. cap = read_pci_config(bus, dev, fn, cap_ptr+MMIO_RANGE_OFFSET);
  342. update_last_devid(calc_devid(MMIO_GET_BUS(cap), MMIO_GET_LD(cap)));
  343. return 0;
  344. }
  345. /*
  346. * After reading the highest device id from the IOMMU PCI capability header
  347. * this function looks if there is a higher device id defined in the ACPI table
  348. */
  349. static int __init find_last_devid_from_ivhd(struct ivhd_header *h)
  350. {
  351. u8 *p = (void *)h, *end = (void *)h;
  352. struct ivhd_entry *dev;
  353. p += sizeof(*h);
  354. end += h->length;
  355. find_last_devid_on_pci(PCI_BUS(h->devid),
  356. PCI_SLOT(h->devid),
  357. PCI_FUNC(h->devid),
  358. h->cap_ptr);
  359. while (p < end) {
  360. dev = (struct ivhd_entry *)p;
  361. switch (dev->type) {
  362. case IVHD_DEV_SELECT:
  363. case IVHD_DEV_RANGE_END:
  364. case IVHD_DEV_ALIAS:
  365. case IVHD_DEV_EXT_SELECT:
  366. /* all the above subfield types refer to device ids */
  367. update_last_devid(dev->devid);
  368. break;
  369. default:
  370. break;
  371. }
  372. p += ivhd_entry_length(p);
  373. }
  374. WARN_ON(p != end);
  375. return 0;
  376. }
  377. /*
  378. * Iterate over all IVHD entries in the ACPI table and find the highest device
  379. * id which we need to handle. This is the first of three functions which parse
  380. * the ACPI table. So we check the checksum here.
  381. */
  382. static int __init find_last_devid_acpi(struct acpi_table_header *table)
  383. {
  384. int i;
  385. u8 checksum = 0, *p = (u8 *)table, *end = (u8 *)table;
  386. struct ivhd_header *h;
  387. /*
  388. * Validate checksum here so we don't need to do it when
  389. * we actually parse the table
  390. */
  391. for (i = 0; i < table->length; ++i)
  392. checksum += p[i];
  393. if (checksum != 0)
  394. /* ACPI table corrupt */
  395. return -ENODEV;
  396. p += IVRS_HEADER_LENGTH;
  397. end += table->length;
  398. while (p < end) {
  399. h = (struct ivhd_header *)p;
  400. switch (h->type) {
  401. case ACPI_IVHD_TYPE:
  402. find_last_devid_from_ivhd(h);
  403. break;
  404. default:
  405. break;
  406. }
  407. p += h->length;
  408. }
  409. WARN_ON(p != end);
  410. return 0;
  411. }
  412. /****************************************************************************
  413. *
  414. * The following functions belong to the code path which parses the ACPI table
  415. * the second time. In this ACPI parsing iteration we allocate IOMMU specific
  416. * data structures, initialize the device/alias/rlookup table and also
  417. * basically initialize the hardware.
  418. *
  419. ****************************************************************************/
  420. /*
  421. * Allocates the command buffer. This buffer is per AMD IOMMU. We can
  422. * write commands to that buffer later and the IOMMU will execute them
  423. * asynchronously
  424. */
  425. static u8 * __init alloc_command_buffer(struct amd_iommu *iommu)
  426. {
  427. u8 *cmd_buf = (u8 *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
  428. get_order(CMD_BUFFER_SIZE));
  429. if (cmd_buf == NULL)
  430. return NULL;
  431. iommu->cmd_buf_size = CMD_BUFFER_SIZE | CMD_BUFFER_UNINITIALIZED;
  432. return cmd_buf;
  433. }
  434. /*
  435. * This function resets the command buffer if the IOMMU stopped fetching
  436. * commands from it.
  437. */
  438. void amd_iommu_reset_cmd_buffer(struct amd_iommu *iommu)
  439. {
  440. iommu_feature_disable(iommu, CONTROL_CMDBUF_EN);
  441. writel(0x00, iommu->mmio_base + MMIO_CMD_HEAD_OFFSET);
  442. writel(0x00, iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
  443. iommu_feature_enable(iommu, CONTROL_CMDBUF_EN);
  444. }
  445. /*
  446. * This function writes the command buffer address to the hardware and
  447. * enables it.
  448. */
  449. static void iommu_enable_command_buffer(struct amd_iommu *iommu)
  450. {
  451. u64 entry;
  452. BUG_ON(iommu->cmd_buf == NULL);
  453. entry = (u64)virt_to_phys(iommu->cmd_buf);
  454. entry |= MMIO_CMD_SIZE_512;
  455. memcpy_toio(iommu->mmio_base + MMIO_CMD_BUF_OFFSET,
  456. &entry, sizeof(entry));
  457. amd_iommu_reset_cmd_buffer(iommu);
  458. iommu->cmd_buf_size &= ~(CMD_BUFFER_UNINITIALIZED);
  459. }
  460. static void __init free_command_buffer(struct amd_iommu *iommu)
  461. {
  462. free_pages((unsigned long)iommu->cmd_buf,
  463. get_order(iommu->cmd_buf_size & ~(CMD_BUFFER_UNINITIALIZED)));
  464. }
  465. /* allocates the memory where the IOMMU will log its events to */
  466. static u8 * __init alloc_event_buffer(struct amd_iommu *iommu)
  467. {
  468. iommu->evt_buf = (u8 *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
  469. get_order(EVT_BUFFER_SIZE));
  470. if (iommu->evt_buf == NULL)
  471. return NULL;
  472. iommu->evt_buf_size = EVT_BUFFER_SIZE;
  473. return iommu->evt_buf;
  474. }
  475. static void iommu_enable_event_buffer(struct amd_iommu *iommu)
  476. {
  477. u64 entry;
  478. BUG_ON(iommu->evt_buf == NULL);
  479. entry = (u64)virt_to_phys(iommu->evt_buf) | EVT_LEN_MASK;
  480. memcpy_toio(iommu->mmio_base + MMIO_EVT_BUF_OFFSET,
  481. &entry, sizeof(entry));
  482. /* set head and tail to zero manually */
  483. writel(0x00, iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
  484. writel(0x00, iommu->mmio_base + MMIO_EVT_TAIL_OFFSET);
  485. iommu_feature_enable(iommu, CONTROL_EVT_LOG_EN);
  486. }
  487. static void __init free_event_buffer(struct amd_iommu *iommu)
  488. {
  489. free_pages((unsigned long)iommu->evt_buf, get_order(EVT_BUFFER_SIZE));
  490. }
  491. /* allocates the memory where the IOMMU will log its events to */
  492. static u8 * __init alloc_ppr_log(struct amd_iommu *iommu)
  493. {
  494. iommu->ppr_log = (u8 *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
  495. get_order(PPR_LOG_SIZE));
  496. if (iommu->ppr_log == NULL)
  497. return NULL;
  498. return iommu->ppr_log;
  499. }
  500. static void iommu_enable_ppr_log(struct amd_iommu *iommu)
  501. {
  502. u64 entry;
  503. if (iommu->ppr_log == NULL)
  504. return;
  505. entry = (u64)virt_to_phys(iommu->ppr_log) | PPR_LOG_SIZE_512;
  506. memcpy_toio(iommu->mmio_base + MMIO_PPR_LOG_OFFSET,
  507. &entry, sizeof(entry));
  508. /* set head and tail to zero manually */
  509. writel(0x00, iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
  510. writel(0x00, iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
  511. iommu_feature_enable(iommu, CONTROL_PPFLOG_EN);
  512. iommu_feature_enable(iommu, CONTROL_PPR_EN);
  513. }
  514. static void __init free_ppr_log(struct amd_iommu *iommu)
  515. {
  516. if (iommu->ppr_log == NULL)
  517. return;
  518. free_pages((unsigned long)iommu->ppr_log, get_order(PPR_LOG_SIZE));
  519. }
  520. static void iommu_enable_gt(struct amd_iommu *iommu)
  521. {
  522. if (!iommu_feature(iommu, FEATURE_GT))
  523. return;
  524. iommu_feature_enable(iommu, CONTROL_GT_EN);
  525. }
  526. /* sets a specific bit in the device table entry. */
  527. static void set_dev_entry_bit(u16 devid, u8 bit)
  528. {
  529. int i = (bit >> 6) & 0x03;
  530. int _bit = bit & 0x3f;
  531. amd_iommu_dev_table[devid].data[i] |= (1UL << _bit);
  532. }
  533. static int get_dev_entry_bit(u16 devid, u8 bit)
  534. {
  535. int i = (bit >> 6) & 0x03;
  536. int _bit = bit & 0x3f;
  537. return (amd_iommu_dev_table[devid].data[i] & (1UL << _bit)) >> _bit;
  538. }
  539. void amd_iommu_apply_erratum_63(u16 devid)
  540. {
  541. int sysmgt;
  542. sysmgt = get_dev_entry_bit(devid, DEV_ENTRY_SYSMGT1) |
  543. (get_dev_entry_bit(devid, DEV_ENTRY_SYSMGT2) << 1);
  544. if (sysmgt == 0x01)
  545. set_dev_entry_bit(devid, DEV_ENTRY_IW);
  546. }
  547. /* Writes the specific IOMMU for a device into the rlookup table */
  548. static void __init set_iommu_for_device(struct amd_iommu *iommu, u16 devid)
  549. {
  550. amd_iommu_rlookup_table[devid] = iommu;
  551. }
  552. /*
  553. * This function takes the device specific flags read from the ACPI
  554. * table and sets up the device table entry with that information
  555. */
  556. static void __init set_dev_entry_from_acpi(struct amd_iommu *iommu,
  557. u16 devid, u32 flags, u32 ext_flags)
  558. {
  559. if (flags & ACPI_DEVFLAG_INITPASS)
  560. set_dev_entry_bit(devid, DEV_ENTRY_INIT_PASS);
  561. if (flags & ACPI_DEVFLAG_EXTINT)
  562. set_dev_entry_bit(devid, DEV_ENTRY_EINT_PASS);
  563. if (flags & ACPI_DEVFLAG_NMI)
  564. set_dev_entry_bit(devid, DEV_ENTRY_NMI_PASS);
  565. if (flags & ACPI_DEVFLAG_SYSMGT1)
  566. set_dev_entry_bit(devid, DEV_ENTRY_SYSMGT1);
  567. if (flags & ACPI_DEVFLAG_SYSMGT2)
  568. set_dev_entry_bit(devid, DEV_ENTRY_SYSMGT2);
  569. if (flags & ACPI_DEVFLAG_LINT0)
  570. set_dev_entry_bit(devid, DEV_ENTRY_LINT0_PASS);
  571. if (flags & ACPI_DEVFLAG_LINT1)
  572. set_dev_entry_bit(devid, DEV_ENTRY_LINT1_PASS);
  573. amd_iommu_apply_erratum_63(devid);
  574. set_iommu_for_device(iommu, devid);
  575. }
  576. static int add_special_device(u8 type, u8 id, u16 devid)
  577. {
  578. struct devid_map *entry;
  579. struct list_head *list;
  580. if (type != IVHD_SPECIAL_IOAPIC && type != IVHD_SPECIAL_HPET)
  581. return -EINVAL;
  582. entry = kzalloc(sizeof(*entry), GFP_KERNEL);
  583. if (!entry)
  584. return -ENOMEM;
  585. entry->id = id;
  586. entry->devid = devid;
  587. if (type == IVHD_SPECIAL_IOAPIC)
  588. list = &ioapic_map;
  589. else
  590. list = &hpet_map;
  591. list_add_tail(&entry->list, list);
  592. return 0;
  593. }
  594. /*
  595. * Reads the device exclusion range from ACPI and initializes the IOMMU with
  596. * it
  597. */
  598. static void __init set_device_exclusion_range(u16 devid, struct ivmd_header *m)
  599. {
  600. struct amd_iommu *iommu = amd_iommu_rlookup_table[devid];
  601. if (!(m->flags & IVMD_FLAG_EXCL_RANGE))
  602. return;
  603. if (iommu) {
  604. /*
  605. * We only can configure exclusion ranges per IOMMU, not
  606. * per device. But we can enable the exclusion range per
  607. * device. This is done here
  608. */
  609. set_dev_entry_bit(m->devid, DEV_ENTRY_EX);
  610. iommu->exclusion_start = m->range_start;
  611. iommu->exclusion_length = m->range_length;
  612. }
  613. }
  614. /*
  615. * Takes a pointer to an AMD IOMMU entry in the ACPI table and
  616. * initializes the hardware and our data structures with it.
  617. */
  618. static int __init init_iommu_from_acpi(struct amd_iommu *iommu,
  619. struct ivhd_header *h)
  620. {
  621. u8 *p = (u8 *)h;
  622. u8 *end = p, flags = 0;
  623. u16 devid = 0, devid_start = 0, devid_to = 0;
  624. u32 dev_i, ext_flags = 0;
  625. bool alias = false;
  626. struct ivhd_entry *e;
  627. /*
  628. * First save the recommended feature enable bits from ACPI
  629. */
  630. iommu->acpi_flags = h->flags;
  631. /*
  632. * Done. Now parse the device entries
  633. */
  634. p += sizeof(struct ivhd_header);
  635. end += h->length;
  636. while (p < end) {
  637. e = (struct ivhd_entry *)p;
  638. switch (e->type) {
  639. case IVHD_DEV_ALL:
  640. DUMP_printk(" DEV_ALL\t\t\t first devid: %02x:%02x.%x"
  641. " last device %02x:%02x.%x flags: %02x\n",
  642. PCI_BUS(iommu->first_device),
  643. PCI_SLOT(iommu->first_device),
  644. PCI_FUNC(iommu->first_device),
  645. PCI_BUS(iommu->last_device),
  646. PCI_SLOT(iommu->last_device),
  647. PCI_FUNC(iommu->last_device),
  648. e->flags);
  649. for (dev_i = iommu->first_device;
  650. dev_i <= iommu->last_device; ++dev_i)
  651. set_dev_entry_from_acpi(iommu, dev_i,
  652. e->flags, 0);
  653. break;
  654. case IVHD_DEV_SELECT:
  655. DUMP_printk(" DEV_SELECT\t\t\t devid: %02x:%02x.%x "
  656. "flags: %02x\n",
  657. PCI_BUS(e->devid),
  658. PCI_SLOT(e->devid),
  659. PCI_FUNC(e->devid),
  660. e->flags);
  661. devid = e->devid;
  662. set_dev_entry_from_acpi(iommu, devid, e->flags, 0);
  663. break;
  664. case IVHD_DEV_SELECT_RANGE_START:
  665. DUMP_printk(" DEV_SELECT_RANGE_START\t "
  666. "devid: %02x:%02x.%x flags: %02x\n",
  667. PCI_BUS(e->devid),
  668. PCI_SLOT(e->devid),
  669. PCI_FUNC(e->devid),
  670. e->flags);
  671. devid_start = e->devid;
  672. flags = e->flags;
  673. ext_flags = 0;
  674. alias = false;
  675. break;
  676. case IVHD_DEV_ALIAS:
  677. DUMP_printk(" DEV_ALIAS\t\t\t devid: %02x:%02x.%x "
  678. "flags: %02x devid_to: %02x:%02x.%x\n",
  679. PCI_BUS(e->devid),
  680. PCI_SLOT(e->devid),
  681. PCI_FUNC(e->devid),
  682. e->flags,
  683. PCI_BUS(e->ext >> 8),
  684. PCI_SLOT(e->ext >> 8),
  685. PCI_FUNC(e->ext >> 8));
  686. devid = e->devid;
  687. devid_to = e->ext >> 8;
  688. set_dev_entry_from_acpi(iommu, devid , e->flags, 0);
  689. set_dev_entry_from_acpi(iommu, devid_to, e->flags, 0);
  690. amd_iommu_alias_table[devid] = devid_to;
  691. break;
  692. case IVHD_DEV_ALIAS_RANGE:
  693. DUMP_printk(" DEV_ALIAS_RANGE\t\t "
  694. "devid: %02x:%02x.%x flags: %02x "
  695. "devid_to: %02x:%02x.%x\n",
  696. PCI_BUS(e->devid),
  697. PCI_SLOT(e->devid),
  698. PCI_FUNC(e->devid),
  699. e->flags,
  700. PCI_BUS(e->ext >> 8),
  701. PCI_SLOT(e->ext >> 8),
  702. PCI_FUNC(e->ext >> 8));
  703. devid_start = e->devid;
  704. flags = e->flags;
  705. devid_to = e->ext >> 8;
  706. ext_flags = 0;
  707. alias = true;
  708. break;
  709. case IVHD_DEV_EXT_SELECT:
  710. DUMP_printk(" DEV_EXT_SELECT\t\t devid: %02x:%02x.%x "
  711. "flags: %02x ext: %08x\n",
  712. PCI_BUS(e->devid),
  713. PCI_SLOT(e->devid),
  714. PCI_FUNC(e->devid),
  715. e->flags, e->ext);
  716. devid = e->devid;
  717. set_dev_entry_from_acpi(iommu, devid, e->flags,
  718. e->ext);
  719. break;
  720. case IVHD_DEV_EXT_SELECT_RANGE:
  721. DUMP_printk(" DEV_EXT_SELECT_RANGE\t devid: "
  722. "%02x:%02x.%x flags: %02x ext: %08x\n",
  723. PCI_BUS(e->devid),
  724. PCI_SLOT(e->devid),
  725. PCI_FUNC(e->devid),
  726. e->flags, e->ext);
  727. devid_start = e->devid;
  728. flags = e->flags;
  729. ext_flags = e->ext;
  730. alias = false;
  731. break;
  732. case IVHD_DEV_RANGE_END:
  733. DUMP_printk(" DEV_RANGE_END\t\t devid: %02x:%02x.%x\n",
  734. PCI_BUS(e->devid),
  735. PCI_SLOT(e->devid),
  736. PCI_FUNC(e->devid));
  737. devid = e->devid;
  738. for (dev_i = devid_start; dev_i <= devid; ++dev_i) {
  739. if (alias) {
  740. amd_iommu_alias_table[dev_i] = devid_to;
  741. set_dev_entry_from_acpi(iommu,
  742. devid_to, flags, ext_flags);
  743. }
  744. set_dev_entry_from_acpi(iommu, dev_i,
  745. flags, ext_flags);
  746. }
  747. break;
  748. case IVHD_DEV_SPECIAL: {
  749. u8 handle, type;
  750. const char *var;
  751. u16 devid;
  752. int ret;
  753. handle = e->ext & 0xff;
  754. devid = (e->ext >> 8) & 0xffff;
  755. type = (e->ext >> 24) & 0xff;
  756. if (type == IVHD_SPECIAL_IOAPIC)
  757. var = "IOAPIC";
  758. else if (type == IVHD_SPECIAL_HPET)
  759. var = "HPET";
  760. else
  761. var = "UNKNOWN";
  762. DUMP_printk(" DEV_SPECIAL(%s[%d])\t\tdevid: %02x:%02x.%x\n",
  763. var, (int)handle,
  764. PCI_BUS(devid),
  765. PCI_SLOT(devid),
  766. PCI_FUNC(devid));
  767. set_dev_entry_from_acpi(iommu, devid, e->flags, 0);
  768. ret = add_special_device(type, handle, devid);
  769. if (ret)
  770. return ret;
  771. break;
  772. }
  773. default:
  774. break;
  775. }
  776. p += ivhd_entry_length(p);
  777. }
  778. return 0;
  779. }
  780. /* Initializes the device->iommu mapping for the driver */
  781. static int __init init_iommu_devices(struct amd_iommu *iommu)
  782. {
  783. u32 i;
  784. for (i = iommu->first_device; i <= iommu->last_device; ++i)
  785. set_iommu_for_device(iommu, i);
  786. return 0;
  787. }
  788. static void __init free_iommu_one(struct amd_iommu *iommu)
  789. {
  790. free_command_buffer(iommu);
  791. free_event_buffer(iommu);
  792. free_ppr_log(iommu);
  793. iommu_unmap_mmio_space(iommu);
  794. }
  795. static void __init free_iommu_all(void)
  796. {
  797. struct amd_iommu *iommu, *next;
  798. for_each_iommu_safe(iommu, next) {
  799. list_del(&iommu->list);
  800. free_iommu_one(iommu);
  801. kfree(iommu);
  802. }
  803. }
  804. /*
  805. * This function clues the initialization function for one IOMMU
  806. * together and also allocates the command buffer and programs the
  807. * hardware. It does NOT enable the IOMMU. This is done afterwards.
  808. */
  809. static int __init init_iommu_one(struct amd_iommu *iommu, struct ivhd_header *h)
  810. {
  811. int ret;
  812. spin_lock_init(&iommu->lock);
  813. /* Add IOMMU to internal data structures */
  814. list_add_tail(&iommu->list, &amd_iommu_list);
  815. iommu->index = amd_iommus_present++;
  816. if (unlikely(iommu->index >= MAX_IOMMUS)) {
  817. WARN(1, "AMD-Vi: System has more IOMMUs than supported by this driver\n");
  818. return -ENOSYS;
  819. }
  820. /* Index is fine - add IOMMU to the array */
  821. amd_iommus[iommu->index] = iommu;
  822. /*
  823. * Copy data from ACPI table entry to the iommu struct
  824. */
  825. iommu->devid = h->devid;
  826. iommu->cap_ptr = h->cap_ptr;
  827. iommu->pci_seg = h->pci_seg;
  828. iommu->mmio_phys = h->mmio_phys;
  829. iommu->mmio_base = iommu_map_mmio_space(h->mmio_phys);
  830. if (!iommu->mmio_base)
  831. return -ENOMEM;
  832. iommu->cmd_buf = alloc_command_buffer(iommu);
  833. if (!iommu->cmd_buf)
  834. return -ENOMEM;
  835. iommu->evt_buf = alloc_event_buffer(iommu);
  836. if (!iommu->evt_buf)
  837. return -ENOMEM;
  838. iommu->int_enabled = false;
  839. ret = init_iommu_from_acpi(iommu, h);
  840. if (ret)
  841. return ret;
  842. /*
  843. * Make sure IOMMU is not considered to translate itself. The IVRS
  844. * table tells us so, but this is a lie!
  845. */
  846. amd_iommu_rlookup_table[iommu->devid] = NULL;
  847. init_iommu_devices(iommu);
  848. return 0;
  849. }
  850. /*
  851. * Iterates over all IOMMU entries in the ACPI table, allocates the
  852. * IOMMU structure and initializes it with init_iommu_one()
  853. */
  854. static int __init init_iommu_all(struct acpi_table_header *table)
  855. {
  856. u8 *p = (u8 *)table, *end = (u8 *)table;
  857. struct ivhd_header *h;
  858. struct amd_iommu *iommu;
  859. int ret;
  860. end += table->length;
  861. p += IVRS_HEADER_LENGTH;
  862. while (p < end) {
  863. h = (struct ivhd_header *)p;
  864. switch (*p) {
  865. case ACPI_IVHD_TYPE:
  866. DUMP_printk("device: %02x:%02x.%01x cap: %04x "
  867. "seg: %d flags: %01x info %04x\n",
  868. PCI_BUS(h->devid), PCI_SLOT(h->devid),
  869. PCI_FUNC(h->devid), h->cap_ptr,
  870. h->pci_seg, h->flags, h->info);
  871. DUMP_printk(" mmio-addr: %016llx\n",
  872. h->mmio_phys);
  873. iommu = kzalloc(sizeof(struct amd_iommu), GFP_KERNEL);
  874. if (iommu == NULL)
  875. return -ENOMEM;
  876. ret = init_iommu_one(iommu, h);
  877. if (ret)
  878. return ret;
  879. break;
  880. default:
  881. break;
  882. }
  883. p += h->length;
  884. }
  885. WARN_ON(p != end);
  886. return 0;
  887. }
  888. static int iommu_init_pci(struct amd_iommu *iommu)
  889. {
  890. int cap_ptr = iommu->cap_ptr;
  891. u32 range, misc, low, high;
  892. iommu->dev = pci_get_bus_and_slot(PCI_BUS(iommu->devid),
  893. iommu->devid & 0xff);
  894. if (!iommu->dev)
  895. return -ENODEV;
  896. pci_read_config_dword(iommu->dev, cap_ptr + MMIO_CAP_HDR_OFFSET,
  897. &iommu->cap);
  898. pci_read_config_dword(iommu->dev, cap_ptr + MMIO_RANGE_OFFSET,
  899. &range);
  900. pci_read_config_dword(iommu->dev, cap_ptr + MMIO_MISC_OFFSET,
  901. &misc);
  902. iommu->first_device = calc_devid(MMIO_GET_BUS(range),
  903. MMIO_GET_FD(range));
  904. iommu->last_device = calc_devid(MMIO_GET_BUS(range),
  905. MMIO_GET_LD(range));
  906. if (!(iommu->cap & (1 << IOMMU_CAP_IOTLB)))
  907. amd_iommu_iotlb_sup = false;
  908. /* read extended feature bits */
  909. low = readl(iommu->mmio_base + MMIO_EXT_FEATURES);
  910. high = readl(iommu->mmio_base + MMIO_EXT_FEATURES + 4);
  911. iommu->features = ((u64)high << 32) | low;
  912. if (iommu_feature(iommu, FEATURE_GT)) {
  913. int glxval;
  914. u32 pasids;
  915. u64 shift;
  916. shift = iommu->features & FEATURE_PASID_MASK;
  917. shift >>= FEATURE_PASID_SHIFT;
  918. pasids = (1 << shift);
  919. amd_iommu_max_pasids = min(amd_iommu_max_pasids, pasids);
  920. glxval = iommu->features & FEATURE_GLXVAL_MASK;
  921. glxval >>= FEATURE_GLXVAL_SHIFT;
  922. if (amd_iommu_max_glx_val == -1)
  923. amd_iommu_max_glx_val = glxval;
  924. else
  925. amd_iommu_max_glx_val = min(amd_iommu_max_glx_val, glxval);
  926. }
  927. if (iommu_feature(iommu, FEATURE_GT) &&
  928. iommu_feature(iommu, FEATURE_PPR)) {
  929. iommu->is_iommu_v2 = true;
  930. amd_iommu_v2_present = true;
  931. }
  932. if (iommu_feature(iommu, FEATURE_PPR)) {
  933. iommu->ppr_log = alloc_ppr_log(iommu);
  934. if (!iommu->ppr_log)
  935. return -ENOMEM;
  936. }
  937. if (iommu->cap & (1UL << IOMMU_CAP_NPCACHE))
  938. amd_iommu_np_cache = true;
  939. if (is_rd890_iommu(iommu->dev)) {
  940. int i, j;
  941. iommu->root_pdev = pci_get_bus_and_slot(iommu->dev->bus->number,
  942. PCI_DEVFN(0, 0));
  943. /*
  944. * Some rd890 systems may not be fully reconfigured by the
  945. * BIOS, so it's necessary for us to store this information so
  946. * it can be reprogrammed on resume
  947. */
  948. pci_read_config_dword(iommu->dev, iommu->cap_ptr + 4,
  949. &iommu->stored_addr_lo);
  950. pci_read_config_dword(iommu->dev, iommu->cap_ptr + 8,
  951. &iommu->stored_addr_hi);
  952. /* Low bit locks writes to configuration space */
  953. iommu->stored_addr_lo &= ~1;
  954. for (i = 0; i < 6; i++)
  955. for (j = 0; j < 0x12; j++)
  956. iommu->stored_l1[i][j] = iommu_read_l1(iommu, i, j);
  957. for (i = 0; i < 0x83; i++)
  958. iommu->stored_l2[i] = iommu_read_l2(iommu, i);
  959. }
  960. return pci_enable_device(iommu->dev);
  961. }
  962. static void print_iommu_info(void)
  963. {
  964. static const char * const feat_str[] = {
  965. "PreF", "PPR", "X2APIC", "NX", "GT", "[5]",
  966. "IA", "GA", "HE", "PC"
  967. };
  968. struct amd_iommu *iommu;
  969. for_each_iommu(iommu) {
  970. int i;
  971. pr_info("AMD-Vi: Found IOMMU at %s cap 0x%hx\n",
  972. dev_name(&iommu->dev->dev), iommu->cap_ptr);
  973. if (iommu->cap & (1 << IOMMU_CAP_EFR)) {
  974. pr_info("AMD-Vi: Extended features: ");
  975. for (i = 0; i < ARRAY_SIZE(feat_str); ++i) {
  976. if (iommu_feature(iommu, (1ULL << i)))
  977. pr_cont(" %s", feat_str[i]);
  978. }
  979. pr_cont("\n");
  980. }
  981. }
  982. if (irq_remapping_enabled)
  983. pr_info("AMD-Vi: Interrupt remapping enabled\n");
  984. }
  985. static int __init amd_iommu_init_pci(void)
  986. {
  987. struct amd_iommu *iommu;
  988. int ret = 0;
  989. for_each_iommu(iommu) {
  990. ret = iommu_init_pci(iommu);
  991. if (ret)
  992. break;
  993. }
  994. ret = amd_iommu_init_devices();
  995. print_iommu_info();
  996. return ret;
  997. }
  998. /****************************************************************************
  999. *
  1000. * The following functions initialize the MSI interrupts for all IOMMUs
  1001. * in the system. It's a bit challenging because there could be multiple
  1002. * IOMMUs per PCI BDF but we can call pci_enable_msi(x) only once per
  1003. * pci_dev.
  1004. *
  1005. ****************************************************************************/
  1006. static int iommu_setup_msi(struct amd_iommu *iommu)
  1007. {
  1008. int r;
  1009. r = pci_enable_msi(iommu->dev);
  1010. if (r)
  1011. return r;
  1012. r = request_threaded_irq(iommu->dev->irq,
  1013. amd_iommu_int_handler,
  1014. amd_iommu_int_thread,
  1015. 0, "AMD-Vi",
  1016. iommu->dev);
  1017. if (r) {
  1018. pci_disable_msi(iommu->dev);
  1019. return r;
  1020. }
  1021. iommu->int_enabled = true;
  1022. return 0;
  1023. }
  1024. static int iommu_init_msi(struct amd_iommu *iommu)
  1025. {
  1026. int ret;
  1027. if (iommu->int_enabled)
  1028. goto enable_faults;
  1029. if (pci_find_capability(iommu->dev, PCI_CAP_ID_MSI))
  1030. ret = iommu_setup_msi(iommu);
  1031. else
  1032. ret = -ENODEV;
  1033. if (ret)
  1034. return ret;
  1035. enable_faults:
  1036. iommu_feature_enable(iommu, CONTROL_EVT_INT_EN);
  1037. if (iommu->ppr_log != NULL)
  1038. iommu_feature_enable(iommu, CONTROL_PPFINT_EN);
  1039. return 0;
  1040. }
  1041. /****************************************************************************
  1042. *
  1043. * The next functions belong to the third pass of parsing the ACPI
  1044. * table. In this last pass the memory mapping requirements are
  1045. * gathered (like exclusion and unity mapping ranges).
  1046. *
  1047. ****************************************************************************/
  1048. static void __init free_unity_maps(void)
  1049. {
  1050. struct unity_map_entry *entry, *next;
  1051. list_for_each_entry_safe(entry, next, &amd_iommu_unity_map, list) {
  1052. list_del(&entry->list);
  1053. kfree(entry);
  1054. }
  1055. }
  1056. /* called when we find an exclusion range definition in ACPI */
  1057. static int __init init_exclusion_range(struct ivmd_header *m)
  1058. {
  1059. int i;
  1060. switch (m->type) {
  1061. case ACPI_IVMD_TYPE:
  1062. set_device_exclusion_range(m->devid, m);
  1063. break;
  1064. case ACPI_IVMD_TYPE_ALL:
  1065. for (i = 0; i <= amd_iommu_last_bdf; ++i)
  1066. set_device_exclusion_range(i, m);
  1067. break;
  1068. case ACPI_IVMD_TYPE_RANGE:
  1069. for (i = m->devid; i <= m->aux; ++i)
  1070. set_device_exclusion_range(i, m);
  1071. break;
  1072. default:
  1073. break;
  1074. }
  1075. return 0;
  1076. }
  1077. /* called for unity map ACPI definition */
  1078. static int __init init_unity_map_range(struct ivmd_header *m)
  1079. {
  1080. struct unity_map_entry *e = NULL;
  1081. char *s;
  1082. e = kzalloc(sizeof(*e), GFP_KERNEL);
  1083. if (e == NULL)
  1084. return -ENOMEM;
  1085. switch (m->type) {
  1086. default:
  1087. kfree(e);
  1088. return 0;
  1089. case ACPI_IVMD_TYPE:
  1090. s = "IVMD_TYPEi\t\t\t";
  1091. e->devid_start = e->devid_end = m->devid;
  1092. break;
  1093. case ACPI_IVMD_TYPE_ALL:
  1094. s = "IVMD_TYPE_ALL\t\t";
  1095. e->devid_start = 0;
  1096. e->devid_end = amd_iommu_last_bdf;
  1097. break;
  1098. case ACPI_IVMD_TYPE_RANGE:
  1099. s = "IVMD_TYPE_RANGE\t\t";
  1100. e->devid_start = m->devid;
  1101. e->devid_end = m->aux;
  1102. break;
  1103. }
  1104. e->address_start = PAGE_ALIGN(m->range_start);
  1105. e->address_end = e->address_start + PAGE_ALIGN(m->range_length);
  1106. e->prot = m->flags >> 1;
  1107. DUMP_printk("%s devid_start: %02x:%02x.%x devid_end: %02x:%02x.%x"
  1108. " range_start: %016llx range_end: %016llx flags: %x\n", s,
  1109. PCI_BUS(e->devid_start), PCI_SLOT(e->devid_start),
  1110. PCI_FUNC(e->devid_start), PCI_BUS(e->devid_end),
  1111. PCI_SLOT(e->devid_end), PCI_FUNC(e->devid_end),
  1112. e->address_start, e->address_end, m->flags);
  1113. list_add_tail(&e->list, &amd_iommu_unity_map);
  1114. return 0;
  1115. }
  1116. /* iterates over all memory definitions we find in the ACPI table */
  1117. static int __init init_memory_definitions(struct acpi_table_header *table)
  1118. {
  1119. u8 *p = (u8 *)table, *end = (u8 *)table;
  1120. struct ivmd_header *m;
  1121. end += table->length;
  1122. p += IVRS_HEADER_LENGTH;
  1123. while (p < end) {
  1124. m = (struct ivmd_header *)p;
  1125. if (m->flags & IVMD_FLAG_EXCL_RANGE)
  1126. init_exclusion_range(m);
  1127. else if (m->flags & IVMD_FLAG_UNITY_MAP)
  1128. init_unity_map_range(m);
  1129. p += m->length;
  1130. }
  1131. return 0;
  1132. }
  1133. /*
  1134. * Init the device table to not allow DMA access for devices and
  1135. * suppress all page faults
  1136. */
  1137. static void init_device_table_dma(void)
  1138. {
  1139. u32 devid;
  1140. for (devid = 0; devid <= amd_iommu_last_bdf; ++devid) {
  1141. set_dev_entry_bit(devid, DEV_ENTRY_VALID);
  1142. set_dev_entry_bit(devid, DEV_ENTRY_TRANSLATION);
  1143. }
  1144. }
  1145. static void __init uninit_device_table_dma(void)
  1146. {
  1147. u32 devid;
  1148. for (devid = 0; devid <= amd_iommu_last_bdf; ++devid) {
  1149. amd_iommu_dev_table[devid].data[0] = 0ULL;
  1150. amd_iommu_dev_table[devid].data[1] = 0ULL;
  1151. }
  1152. }
  1153. static void init_device_table(void)
  1154. {
  1155. u32 devid;
  1156. if (!amd_iommu_irq_remap)
  1157. return;
  1158. for (devid = 0; devid <= amd_iommu_last_bdf; ++devid)
  1159. set_dev_entry_bit(devid, DEV_ENTRY_IRQ_TBL_EN);
  1160. }
  1161. static void iommu_init_flags(struct amd_iommu *iommu)
  1162. {
  1163. iommu->acpi_flags & IVHD_FLAG_HT_TUN_EN_MASK ?
  1164. iommu_feature_enable(iommu, CONTROL_HT_TUN_EN) :
  1165. iommu_feature_disable(iommu, CONTROL_HT_TUN_EN);
  1166. iommu->acpi_flags & IVHD_FLAG_PASSPW_EN_MASK ?
  1167. iommu_feature_enable(iommu, CONTROL_PASSPW_EN) :
  1168. iommu_feature_disable(iommu, CONTROL_PASSPW_EN);
  1169. iommu->acpi_flags & IVHD_FLAG_RESPASSPW_EN_MASK ?
  1170. iommu_feature_enable(iommu, CONTROL_RESPASSPW_EN) :
  1171. iommu_feature_disable(iommu, CONTROL_RESPASSPW_EN);
  1172. iommu->acpi_flags & IVHD_FLAG_ISOC_EN_MASK ?
  1173. iommu_feature_enable(iommu, CONTROL_ISOC_EN) :
  1174. iommu_feature_disable(iommu, CONTROL_ISOC_EN);
  1175. /*
  1176. * make IOMMU memory accesses cache coherent
  1177. */
  1178. iommu_feature_enable(iommu, CONTROL_COHERENT_EN);
  1179. /* Set IOTLB invalidation timeout to 1s */
  1180. iommu_set_inv_tlb_timeout(iommu, CTRL_INV_TO_1S);
  1181. }
  1182. static void iommu_apply_resume_quirks(struct amd_iommu *iommu)
  1183. {
  1184. int i, j;
  1185. u32 ioc_feature_control;
  1186. struct pci_dev *pdev = iommu->root_pdev;
  1187. /* RD890 BIOSes may not have completely reconfigured the iommu */
  1188. if (!is_rd890_iommu(iommu->dev) || !pdev)
  1189. return;
  1190. /*
  1191. * First, we need to ensure that the iommu is enabled. This is
  1192. * controlled by a register in the northbridge
  1193. */
  1194. /* Select Northbridge indirect register 0x75 and enable writing */
  1195. pci_write_config_dword(pdev, 0x60, 0x75 | (1 << 7));
  1196. pci_read_config_dword(pdev, 0x64, &ioc_feature_control);
  1197. /* Enable the iommu */
  1198. if (!(ioc_feature_control & 0x1))
  1199. pci_write_config_dword(pdev, 0x64, ioc_feature_control | 1);
  1200. /* Restore the iommu BAR */
  1201. pci_write_config_dword(iommu->dev, iommu->cap_ptr + 4,
  1202. iommu->stored_addr_lo);
  1203. pci_write_config_dword(iommu->dev, iommu->cap_ptr + 8,
  1204. iommu->stored_addr_hi);
  1205. /* Restore the l1 indirect regs for each of the 6 l1s */
  1206. for (i = 0; i < 6; i++)
  1207. for (j = 0; j < 0x12; j++)
  1208. iommu_write_l1(iommu, i, j, iommu->stored_l1[i][j]);
  1209. /* Restore the l2 indirect regs */
  1210. for (i = 0; i < 0x83; i++)
  1211. iommu_write_l2(iommu, i, iommu->stored_l2[i]);
  1212. /* Lock PCI setup registers */
  1213. pci_write_config_dword(iommu->dev, iommu->cap_ptr + 4,
  1214. iommu->stored_addr_lo | 1);
  1215. }
  1216. /*
  1217. * This function finally enables all IOMMUs found in the system after
  1218. * they have been initialized
  1219. */
  1220. static void early_enable_iommus(void)
  1221. {
  1222. struct amd_iommu *iommu;
  1223. for_each_iommu(iommu) {
  1224. iommu_disable(iommu);
  1225. iommu_init_flags(iommu);
  1226. iommu_set_device_table(iommu);
  1227. iommu_enable_command_buffer(iommu);
  1228. iommu_enable_event_buffer(iommu);
  1229. iommu_set_exclusion_range(iommu);
  1230. iommu_enable(iommu);
  1231. iommu_flush_all_caches(iommu);
  1232. }
  1233. }
  1234. static void enable_iommus_v2(void)
  1235. {
  1236. struct amd_iommu *iommu;
  1237. for_each_iommu(iommu) {
  1238. iommu_enable_ppr_log(iommu);
  1239. iommu_enable_gt(iommu);
  1240. }
  1241. }
  1242. static void enable_iommus(void)
  1243. {
  1244. early_enable_iommus();
  1245. enable_iommus_v2();
  1246. }
  1247. static void disable_iommus(void)
  1248. {
  1249. struct amd_iommu *iommu;
  1250. for_each_iommu(iommu)
  1251. iommu_disable(iommu);
  1252. }
  1253. /*
  1254. * Suspend/Resume support
  1255. * disable suspend until real resume implemented
  1256. */
  1257. static void amd_iommu_resume(void)
  1258. {
  1259. struct amd_iommu *iommu;
  1260. for_each_iommu(iommu)
  1261. iommu_apply_resume_quirks(iommu);
  1262. /* re-load the hardware */
  1263. enable_iommus();
  1264. amd_iommu_enable_interrupts();
  1265. }
  1266. static int amd_iommu_suspend(void)
  1267. {
  1268. /* disable IOMMUs to go out of the way for BIOS */
  1269. disable_iommus();
  1270. return 0;
  1271. }
  1272. static struct syscore_ops amd_iommu_syscore_ops = {
  1273. .suspend = amd_iommu_suspend,
  1274. .resume = amd_iommu_resume,
  1275. };
  1276. static void __init free_on_init_error(void)
  1277. {
  1278. free_pages((unsigned long)irq_lookup_table,
  1279. get_order(rlookup_table_size));
  1280. if (amd_iommu_irq_cache) {
  1281. kmem_cache_destroy(amd_iommu_irq_cache);
  1282. amd_iommu_irq_cache = NULL;
  1283. }
  1284. free_pages((unsigned long)amd_iommu_rlookup_table,
  1285. get_order(rlookup_table_size));
  1286. free_pages((unsigned long)amd_iommu_alias_table,
  1287. get_order(alias_table_size));
  1288. free_pages((unsigned long)amd_iommu_dev_table,
  1289. get_order(dev_table_size));
  1290. free_iommu_all();
  1291. #ifdef CONFIG_GART_IOMMU
  1292. /*
  1293. * We failed to initialize the AMD IOMMU - try fallback to GART
  1294. * if possible.
  1295. */
  1296. gart_iommu_init();
  1297. #endif
  1298. }
  1299. /* SB IOAPIC is always on this device in AMD systems */
  1300. #define IOAPIC_SB_DEVID ((0x00 << 8) | PCI_DEVFN(0x14, 0))
  1301. static bool __init check_ioapic_information(void)
  1302. {
  1303. bool ret, has_sb_ioapic;
  1304. int idx;
  1305. has_sb_ioapic = false;
  1306. ret = false;
  1307. for (idx = 0; idx < nr_ioapics; idx++) {
  1308. int devid, id = mpc_ioapic_id(idx);
  1309. devid = get_ioapic_devid(id);
  1310. if (devid < 0) {
  1311. pr_err(FW_BUG "AMD-Vi: IOAPIC[%d] not in IVRS table\n", id);
  1312. ret = false;
  1313. } else if (devid == IOAPIC_SB_DEVID) {
  1314. has_sb_ioapic = true;
  1315. ret = true;
  1316. }
  1317. }
  1318. if (!has_sb_ioapic) {
  1319. /*
  1320. * We expect the SB IOAPIC to be listed in the IVRS
  1321. * table. The system timer is connected to the SB IOAPIC
  1322. * and if we don't have it in the list the system will
  1323. * panic at boot time. This situation usually happens
  1324. * when the BIOS is buggy and provides us the wrong
  1325. * device id for the IOAPIC in the system.
  1326. */
  1327. pr_err(FW_BUG "AMD-Vi: No southbridge IOAPIC found in IVRS table\n");
  1328. }
  1329. if (!ret)
  1330. pr_err("AMD-Vi: Disabling interrupt remapping due to BIOS Bug(s)\n");
  1331. return ret;
  1332. }
  1333. static void __init free_dma_resources(void)
  1334. {
  1335. amd_iommu_uninit_devices();
  1336. free_pages((unsigned long)amd_iommu_pd_alloc_bitmap,
  1337. get_order(MAX_DOMAIN_ID/8));
  1338. free_unity_maps();
  1339. }
  1340. /*
  1341. * This is the hardware init function for AMD IOMMU in the system.
  1342. * This function is called either from amd_iommu_init or from the interrupt
  1343. * remapping setup code.
  1344. *
  1345. * This function basically parses the ACPI table for AMD IOMMU (IVRS)
  1346. * three times:
  1347. *
  1348. * 1 pass) Find the highest PCI device id the driver has to handle.
  1349. * Upon this information the size of the data structures is
  1350. * determined that needs to be allocated.
  1351. *
  1352. * 2 pass) Initialize the data structures just allocated with the
  1353. * information in the ACPI table about available AMD IOMMUs
  1354. * in the system. It also maps the PCI devices in the
  1355. * system to specific IOMMUs
  1356. *
  1357. * 3 pass) After the basic data structures are allocated and
  1358. * initialized we update them with information about memory
  1359. * remapping requirements parsed out of the ACPI table in
  1360. * this last pass.
  1361. *
  1362. * After everything is set up the IOMMUs are enabled and the necessary
  1363. * hotplug and suspend notifiers are registered.
  1364. */
  1365. static int __init early_amd_iommu_init(void)
  1366. {
  1367. struct acpi_table_header *ivrs_base;
  1368. acpi_size ivrs_size;
  1369. acpi_status status;
  1370. int i, ret = 0;
  1371. if (!amd_iommu_detected)
  1372. return -ENODEV;
  1373. status = acpi_get_table_with_size("IVRS", 0, &ivrs_base, &ivrs_size);
  1374. if (status == AE_NOT_FOUND)
  1375. return -ENODEV;
  1376. else if (ACPI_FAILURE(status)) {
  1377. const char *err = acpi_format_exception(status);
  1378. pr_err("AMD-Vi: IVRS table error: %s\n", err);
  1379. return -EINVAL;
  1380. }
  1381. /*
  1382. * First parse ACPI tables to find the largest Bus/Dev/Func
  1383. * we need to handle. Upon this information the shared data
  1384. * structures for the IOMMUs in the system will be allocated
  1385. */
  1386. ret = find_last_devid_acpi(ivrs_base);
  1387. if (ret)
  1388. goto out;
  1389. dev_table_size = tbl_size(DEV_TABLE_ENTRY_SIZE);
  1390. alias_table_size = tbl_size(ALIAS_TABLE_ENTRY_SIZE);
  1391. rlookup_table_size = tbl_size(RLOOKUP_TABLE_ENTRY_SIZE);
  1392. /* Device table - directly used by all IOMMUs */
  1393. ret = -ENOMEM;
  1394. amd_iommu_dev_table = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
  1395. get_order(dev_table_size));
  1396. if (amd_iommu_dev_table == NULL)
  1397. goto out;
  1398. /*
  1399. * Alias table - map PCI Bus/Dev/Func to Bus/Dev/Func the
  1400. * IOMMU see for that device
  1401. */
  1402. amd_iommu_alias_table = (void *)__get_free_pages(GFP_KERNEL,
  1403. get_order(alias_table_size));
  1404. if (amd_iommu_alias_table == NULL)
  1405. goto out;
  1406. /* IOMMU rlookup table - find the IOMMU for a specific device */
  1407. amd_iommu_rlookup_table = (void *)__get_free_pages(
  1408. GFP_KERNEL | __GFP_ZERO,
  1409. get_order(rlookup_table_size));
  1410. if (amd_iommu_rlookup_table == NULL)
  1411. goto out;
  1412. amd_iommu_pd_alloc_bitmap = (void *)__get_free_pages(
  1413. GFP_KERNEL | __GFP_ZERO,
  1414. get_order(MAX_DOMAIN_ID/8));
  1415. if (amd_iommu_pd_alloc_bitmap == NULL)
  1416. goto out;
  1417. /*
  1418. * let all alias entries point to itself
  1419. */
  1420. for (i = 0; i <= amd_iommu_last_bdf; ++i)
  1421. amd_iommu_alias_table[i] = i;
  1422. /*
  1423. * never allocate domain 0 because its used as the non-allocated and
  1424. * error value placeholder
  1425. */
  1426. amd_iommu_pd_alloc_bitmap[0] = 1;
  1427. spin_lock_init(&amd_iommu_pd_lock);
  1428. /*
  1429. * now the data structures are allocated and basically initialized
  1430. * start the real acpi table scan
  1431. */
  1432. ret = init_iommu_all(ivrs_base);
  1433. if (ret)
  1434. goto out;
  1435. if (amd_iommu_irq_remap)
  1436. amd_iommu_irq_remap = check_ioapic_information();
  1437. if (amd_iommu_irq_remap) {
  1438. /*
  1439. * Interrupt remapping enabled, create kmem_cache for the
  1440. * remapping tables.
  1441. */
  1442. amd_iommu_irq_cache = kmem_cache_create("irq_remap_cache",
  1443. MAX_IRQS_PER_TABLE * sizeof(u32),
  1444. IRQ_TABLE_ALIGNMENT,
  1445. 0, NULL);
  1446. if (!amd_iommu_irq_cache)
  1447. goto out;
  1448. irq_lookup_table = (void *)__get_free_pages(
  1449. GFP_KERNEL | __GFP_ZERO,
  1450. get_order(rlookup_table_size));
  1451. if (!irq_lookup_table)
  1452. goto out;
  1453. }
  1454. ret = init_memory_definitions(ivrs_base);
  1455. if (ret)
  1456. goto out;
  1457. /* init the device table */
  1458. init_device_table();
  1459. out:
  1460. /* Don't leak any ACPI memory */
  1461. early_acpi_os_unmap_memory((char __iomem *)ivrs_base, ivrs_size);
  1462. ivrs_base = NULL;
  1463. return ret;
  1464. }
  1465. static int amd_iommu_enable_interrupts(void)
  1466. {
  1467. struct amd_iommu *iommu;
  1468. int ret = 0;
  1469. for_each_iommu(iommu) {
  1470. ret = iommu_init_msi(iommu);
  1471. if (ret)
  1472. goto out;
  1473. }
  1474. out:
  1475. return ret;
  1476. }
  1477. static bool detect_ivrs(void)
  1478. {
  1479. struct acpi_table_header *ivrs_base;
  1480. acpi_size ivrs_size;
  1481. acpi_status status;
  1482. status = acpi_get_table_with_size("IVRS", 0, &ivrs_base, &ivrs_size);
  1483. if (status == AE_NOT_FOUND)
  1484. return false;
  1485. else if (ACPI_FAILURE(status)) {
  1486. const char *err = acpi_format_exception(status);
  1487. pr_err("AMD-Vi: IVRS table error: %s\n", err);
  1488. return false;
  1489. }
  1490. early_acpi_os_unmap_memory((char __iomem *)ivrs_base, ivrs_size);
  1491. /* Make sure ACS will be enabled during PCI probe */
  1492. pci_request_acs();
  1493. if (!disable_irq_remap)
  1494. amd_iommu_irq_remap = true;
  1495. return true;
  1496. }
  1497. static int amd_iommu_init_dma(void)
  1498. {
  1499. struct amd_iommu *iommu;
  1500. int ret;
  1501. init_device_table_dma();
  1502. for_each_iommu(iommu)
  1503. iommu_flush_all_caches(iommu);
  1504. if (iommu_pass_through)
  1505. ret = amd_iommu_init_passthrough();
  1506. else
  1507. ret = amd_iommu_init_dma_ops();
  1508. if (ret)
  1509. return ret;
  1510. amd_iommu_init_api();
  1511. amd_iommu_init_notifier();
  1512. return 0;
  1513. }
  1514. /****************************************************************************
  1515. *
  1516. * AMD IOMMU Initialization State Machine
  1517. *
  1518. ****************************************************************************/
  1519. static int __init state_next(void)
  1520. {
  1521. int ret = 0;
  1522. switch (init_state) {
  1523. case IOMMU_START_STATE:
  1524. if (!detect_ivrs()) {
  1525. init_state = IOMMU_NOT_FOUND;
  1526. ret = -ENODEV;
  1527. } else {
  1528. init_state = IOMMU_IVRS_DETECTED;
  1529. }
  1530. break;
  1531. case IOMMU_IVRS_DETECTED:
  1532. ret = early_amd_iommu_init();
  1533. init_state = ret ? IOMMU_INIT_ERROR : IOMMU_ACPI_FINISHED;
  1534. break;
  1535. case IOMMU_ACPI_FINISHED:
  1536. early_enable_iommus();
  1537. register_syscore_ops(&amd_iommu_syscore_ops);
  1538. x86_platform.iommu_shutdown = disable_iommus;
  1539. init_state = IOMMU_ENABLED;
  1540. break;
  1541. case IOMMU_ENABLED:
  1542. ret = amd_iommu_init_pci();
  1543. init_state = ret ? IOMMU_INIT_ERROR : IOMMU_PCI_INIT;
  1544. enable_iommus_v2();
  1545. break;
  1546. case IOMMU_PCI_INIT:
  1547. ret = amd_iommu_enable_interrupts();
  1548. init_state = ret ? IOMMU_INIT_ERROR : IOMMU_INTERRUPTS_EN;
  1549. break;
  1550. case IOMMU_INTERRUPTS_EN:
  1551. ret = amd_iommu_init_dma();
  1552. init_state = ret ? IOMMU_INIT_ERROR : IOMMU_DMA_OPS;
  1553. break;
  1554. case IOMMU_DMA_OPS:
  1555. init_state = IOMMU_INITIALIZED;
  1556. break;
  1557. case IOMMU_INITIALIZED:
  1558. /* Nothing to do */
  1559. break;
  1560. case IOMMU_NOT_FOUND:
  1561. case IOMMU_INIT_ERROR:
  1562. /* Error states => do nothing */
  1563. ret = -EINVAL;
  1564. break;
  1565. default:
  1566. /* Unknown state */
  1567. BUG();
  1568. }
  1569. return ret;
  1570. }
  1571. static int __init iommu_go_to_state(enum iommu_init_state state)
  1572. {
  1573. int ret = 0;
  1574. while (init_state != state) {
  1575. ret = state_next();
  1576. if (init_state == IOMMU_NOT_FOUND ||
  1577. init_state == IOMMU_INIT_ERROR)
  1578. break;
  1579. }
  1580. return ret;
  1581. }
  1582. #ifdef CONFIG_IRQ_REMAP
  1583. int __init amd_iommu_prepare(void)
  1584. {
  1585. return iommu_go_to_state(IOMMU_ACPI_FINISHED);
  1586. }
  1587. int __init amd_iommu_supported(void)
  1588. {
  1589. return amd_iommu_irq_remap ? 1 : 0;
  1590. }
  1591. int __init amd_iommu_enable(void)
  1592. {
  1593. int ret;
  1594. ret = iommu_go_to_state(IOMMU_ENABLED);
  1595. if (ret)
  1596. return ret;
  1597. irq_remapping_enabled = 1;
  1598. return 0;
  1599. }
  1600. void amd_iommu_disable(void)
  1601. {
  1602. amd_iommu_suspend();
  1603. }
  1604. int amd_iommu_reenable(int mode)
  1605. {
  1606. amd_iommu_resume();
  1607. return 0;
  1608. }
  1609. int __init amd_iommu_enable_faulting(void)
  1610. {
  1611. /* We enable MSI later when PCI is initialized */
  1612. return 0;
  1613. }
  1614. #endif
  1615. /*
  1616. * This is the core init function for AMD IOMMU hardware in the system.
  1617. * This function is called from the generic x86 DMA layer initialization
  1618. * code.
  1619. */
  1620. static int __init amd_iommu_init(void)
  1621. {
  1622. int ret;
  1623. ret = iommu_go_to_state(IOMMU_INITIALIZED);
  1624. if (ret) {
  1625. free_dma_resources();
  1626. if (!irq_remapping_enabled) {
  1627. disable_iommus();
  1628. free_on_init_error();
  1629. } else {
  1630. struct amd_iommu *iommu;
  1631. uninit_device_table_dma();
  1632. for_each_iommu(iommu)
  1633. iommu_flush_all_caches(iommu);
  1634. }
  1635. }
  1636. return ret;
  1637. }
  1638. /****************************************************************************
  1639. *
  1640. * Early detect code. This code runs at IOMMU detection time in the DMA
  1641. * layer. It just looks if there is an IVRS ACPI table to detect AMD
  1642. * IOMMUs
  1643. *
  1644. ****************************************************************************/
  1645. int __init amd_iommu_detect(void)
  1646. {
  1647. int ret;
  1648. if (no_iommu || (iommu_detected && !gart_iommu_aperture))
  1649. return -ENODEV;
  1650. if (amd_iommu_disabled)
  1651. return -ENODEV;
  1652. ret = iommu_go_to_state(IOMMU_IVRS_DETECTED);
  1653. if (ret)
  1654. return ret;
  1655. amd_iommu_detected = true;
  1656. iommu_detected = 1;
  1657. x86_init.iommu.iommu_init = amd_iommu_init;
  1658. return 0;
  1659. }
  1660. /****************************************************************************
  1661. *
  1662. * Parsing functions for the AMD IOMMU specific kernel command line
  1663. * options.
  1664. *
  1665. ****************************************************************************/
  1666. static int __init parse_amd_iommu_dump(char *str)
  1667. {
  1668. amd_iommu_dump = true;
  1669. return 1;
  1670. }
  1671. static int __init parse_amd_iommu_options(char *str)
  1672. {
  1673. for (; *str; ++str) {
  1674. if (strncmp(str, "fullflush", 9) == 0)
  1675. amd_iommu_unmap_flush = true;
  1676. if (strncmp(str, "off", 3) == 0)
  1677. amd_iommu_disabled = true;
  1678. if (strncmp(str, "force_isolation", 15) == 0)
  1679. amd_iommu_force_isolation = true;
  1680. }
  1681. return 1;
  1682. }
  1683. __setup("amd_iommu_dump", parse_amd_iommu_dump);
  1684. __setup("amd_iommu=", parse_amd_iommu_options);
  1685. IOMMU_INIT_FINISH(amd_iommu_detect,
  1686. gart_iommu_hole_init,
  1687. NULL,
  1688. NULL);
  1689. bool amd_iommu_v2_supported(void)
  1690. {
  1691. return amd_iommu_v2_present;
  1692. }
  1693. EXPORT_SYMBOL(amd_iommu_v2_supported);