amd_iommu_init.c 49 KB

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