amd_iommu_init.c 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112
  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. * Family15h Model 10h-1fh erratum 746 (IOMMU Logging May Stall Translations)
  806. * Workaround:
  807. * BIOS should disable L2B micellaneous clock gating by setting
  808. * L2_L2B_CK_GATE_CONTROL[CKGateL2BMiscDisable](D0F2xF4_x90[2]) = 1b
  809. */
  810. static void amd_iommu_erratum_746_workaround(struct amd_iommu *iommu)
  811. {
  812. u32 value;
  813. if ((boot_cpu_data.x86 != 0x15) ||
  814. (boot_cpu_data.x86_model < 0x10) ||
  815. (boot_cpu_data.x86_model > 0x1f))
  816. return;
  817. pci_write_config_dword(iommu->dev, 0xf0, 0x90);
  818. pci_read_config_dword(iommu->dev, 0xf4, &value);
  819. if (value & BIT(2))
  820. return;
  821. /* Select NB indirect register 0x90 and enable writing */
  822. pci_write_config_dword(iommu->dev, 0xf0, 0x90 | (1 << 8));
  823. pci_write_config_dword(iommu->dev, 0xf4, value | 0x4);
  824. pr_info("AMD-Vi: Applying erratum 746 workaround for IOMMU at %s\n",
  825. dev_name(&iommu->dev->dev));
  826. /* Clear the enable writing bit */
  827. pci_write_config_dword(iommu->dev, 0xf0, 0x90);
  828. }
  829. /*
  830. * This function clues the initialization function for one IOMMU
  831. * together and also allocates the command buffer and programs the
  832. * hardware. It does NOT enable the IOMMU. This is done afterwards.
  833. */
  834. static int __init init_iommu_one(struct amd_iommu *iommu, struct ivhd_header *h)
  835. {
  836. int ret;
  837. spin_lock_init(&iommu->lock);
  838. /* Add IOMMU to internal data structures */
  839. list_add_tail(&iommu->list, &amd_iommu_list);
  840. iommu->index = amd_iommus_present++;
  841. if (unlikely(iommu->index >= MAX_IOMMUS)) {
  842. WARN(1, "AMD-Vi: System has more IOMMUs than supported by this driver\n");
  843. return -ENOSYS;
  844. }
  845. /* Index is fine - add IOMMU to the array */
  846. amd_iommus[iommu->index] = iommu;
  847. /*
  848. * Copy data from ACPI table entry to the iommu struct
  849. */
  850. iommu->devid = h->devid;
  851. iommu->cap_ptr = h->cap_ptr;
  852. iommu->pci_seg = h->pci_seg;
  853. iommu->mmio_phys = h->mmio_phys;
  854. iommu->mmio_base = iommu_map_mmio_space(h->mmio_phys);
  855. if (!iommu->mmio_base)
  856. return -ENOMEM;
  857. iommu->cmd_buf = alloc_command_buffer(iommu);
  858. if (!iommu->cmd_buf)
  859. return -ENOMEM;
  860. iommu->evt_buf = alloc_event_buffer(iommu);
  861. if (!iommu->evt_buf)
  862. return -ENOMEM;
  863. iommu->int_enabled = false;
  864. ret = init_iommu_from_acpi(iommu, h);
  865. if (ret)
  866. return ret;
  867. /*
  868. * Make sure IOMMU is not considered to translate itself. The IVRS
  869. * table tells us so, but this is a lie!
  870. */
  871. amd_iommu_rlookup_table[iommu->devid] = NULL;
  872. init_iommu_devices(iommu);
  873. return 0;
  874. }
  875. /*
  876. * Iterates over all IOMMU entries in the ACPI table, allocates the
  877. * IOMMU structure and initializes it with init_iommu_one()
  878. */
  879. static int __init init_iommu_all(struct acpi_table_header *table)
  880. {
  881. u8 *p = (u8 *)table, *end = (u8 *)table;
  882. struct ivhd_header *h;
  883. struct amd_iommu *iommu;
  884. int ret;
  885. end += table->length;
  886. p += IVRS_HEADER_LENGTH;
  887. while (p < end) {
  888. h = (struct ivhd_header *)p;
  889. switch (*p) {
  890. case ACPI_IVHD_TYPE:
  891. DUMP_printk("device: %02x:%02x.%01x cap: %04x "
  892. "seg: %d flags: %01x info %04x\n",
  893. PCI_BUS(h->devid), PCI_SLOT(h->devid),
  894. PCI_FUNC(h->devid), h->cap_ptr,
  895. h->pci_seg, h->flags, h->info);
  896. DUMP_printk(" mmio-addr: %016llx\n",
  897. h->mmio_phys);
  898. iommu = kzalloc(sizeof(struct amd_iommu), GFP_KERNEL);
  899. if (iommu == NULL)
  900. return -ENOMEM;
  901. ret = init_iommu_one(iommu, h);
  902. if (ret)
  903. return ret;
  904. break;
  905. default:
  906. break;
  907. }
  908. p += h->length;
  909. }
  910. WARN_ON(p != end);
  911. return 0;
  912. }
  913. static int iommu_init_pci(struct amd_iommu *iommu)
  914. {
  915. int cap_ptr = iommu->cap_ptr;
  916. u32 range, misc, low, high;
  917. iommu->dev = pci_get_bus_and_slot(PCI_BUS(iommu->devid),
  918. iommu->devid & 0xff);
  919. if (!iommu->dev)
  920. return -ENODEV;
  921. pci_read_config_dword(iommu->dev, cap_ptr + MMIO_CAP_HDR_OFFSET,
  922. &iommu->cap);
  923. pci_read_config_dword(iommu->dev, cap_ptr + MMIO_RANGE_OFFSET,
  924. &range);
  925. pci_read_config_dword(iommu->dev, cap_ptr + MMIO_MISC_OFFSET,
  926. &misc);
  927. iommu->first_device = calc_devid(MMIO_GET_BUS(range),
  928. MMIO_GET_FD(range));
  929. iommu->last_device = calc_devid(MMIO_GET_BUS(range),
  930. MMIO_GET_LD(range));
  931. if (!(iommu->cap & (1 << IOMMU_CAP_IOTLB)))
  932. amd_iommu_iotlb_sup = false;
  933. /* read extended feature bits */
  934. low = readl(iommu->mmio_base + MMIO_EXT_FEATURES);
  935. high = readl(iommu->mmio_base + MMIO_EXT_FEATURES + 4);
  936. iommu->features = ((u64)high << 32) | low;
  937. if (iommu_feature(iommu, FEATURE_GT)) {
  938. int glxval;
  939. u32 pasids;
  940. u64 shift;
  941. shift = iommu->features & FEATURE_PASID_MASK;
  942. shift >>= FEATURE_PASID_SHIFT;
  943. pasids = (1 << shift);
  944. amd_iommu_max_pasids = min(amd_iommu_max_pasids, pasids);
  945. glxval = iommu->features & FEATURE_GLXVAL_MASK;
  946. glxval >>= FEATURE_GLXVAL_SHIFT;
  947. if (amd_iommu_max_glx_val == -1)
  948. amd_iommu_max_glx_val = glxval;
  949. else
  950. amd_iommu_max_glx_val = min(amd_iommu_max_glx_val, glxval);
  951. }
  952. if (iommu_feature(iommu, FEATURE_GT) &&
  953. iommu_feature(iommu, FEATURE_PPR)) {
  954. iommu->is_iommu_v2 = true;
  955. amd_iommu_v2_present = true;
  956. }
  957. if (iommu_feature(iommu, FEATURE_PPR)) {
  958. iommu->ppr_log = alloc_ppr_log(iommu);
  959. if (!iommu->ppr_log)
  960. return -ENOMEM;
  961. }
  962. if (iommu->cap & (1UL << IOMMU_CAP_NPCACHE))
  963. amd_iommu_np_cache = true;
  964. if (is_rd890_iommu(iommu->dev)) {
  965. int i, j;
  966. iommu->root_pdev = pci_get_bus_and_slot(iommu->dev->bus->number,
  967. PCI_DEVFN(0, 0));
  968. /*
  969. * Some rd890 systems may not be fully reconfigured by the
  970. * BIOS, so it's necessary for us to store this information so
  971. * it can be reprogrammed on resume
  972. */
  973. pci_read_config_dword(iommu->dev, iommu->cap_ptr + 4,
  974. &iommu->stored_addr_lo);
  975. pci_read_config_dword(iommu->dev, iommu->cap_ptr + 8,
  976. &iommu->stored_addr_hi);
  977. /* Low bit locks writes to configuration space */
  978. iommu->stored_addr_lo &= ~1;
  979. for (i = 0; i < 6; i++)
  980. for (j = 0; j < 0x12; j++)
  981. iommu->stored_l1[i][j] = iommu_read_l1(iommu, i, j);
  982. for (i = 0; i < 0x83; i++)
  983. iommu->stored_l2[i] = iommu_read_l2(iommu, i);
  984. }
  985. amd_iommu_erratum_746_workaround(iommu);
  986. return pci_enable_device(iommu->dev);
  987. }
  988. static void print_iommu_info(void)
  989. {
  990. static const char * const feat_str[] = {
  991. "PreF", "PPR", "X2APIC", "NX", "GT", "[5]",
  992. "IA", "GA", "HE", "PC"
  993. };
  994. struct amd_iommu *iommu;
  995. for_each_iommu(iommu) {
  996. int i;
  997. pr_info("AMD-Vi: Found IOMMU at %s cap 0x%hx\n",
  998. dev_name(&iommu->dev->dev), iommu->cap_ptr);
  999. if (iommu->cap & (1 << IOMMU_CAP_EFR)) {
  1000. pr_info("AMD-Vi: Extended features: ");
  1001. for (i = 0; i < ARRAY_SIZE(feat_str); ++i) {
  1002. if (iommu_feature(iommu, (1ULL << i)))
  1003. pr_cont(" %s", feat_str[i]);
  1004. }
  1005. pr_cont("\n");
  1006. }
  1007. }
  1008. if (irq_remapping_enabled)
  1009. pr_info("AMD-Vi: Interrupt remapping enabled\n");
  1010. }
  1011. static int __init amd_iommu_init_pci(void)
  1012. {
  1013. struct amd_iommu *iommu;
  1014. int ret = 0;
  1015. for_each_iommu(iommu) {
  1016. ret = iommu_init_pci(iommu);
  1017. if (ret)
  1018. break;
  1019. }
  1020. ret = amd_iommu_init_devices();
  1021. print_iommu_info();
  1022. return ret;
  1023. }
  1024. /****************************************************************************
  1025. *
  1026. * The following functions initialize the MSI interrupts for all IOMMUs
  1027. * in the system. It's a bit challenging because there could be multiple
  1028. * IOMMUs per PCI BDF but we can call pci_enable_msi(x) only once per
  1029. * pci_dev.
  1030. *
  1031. ****************************************************************************/
  1032. static int iommu_setup_msi(struct amd_iommu *iommu)
  1033. {
  1034. int r;
  1035. r = pci_enable_msi(iommu->dev);
  1036. if (r)
  1037. return r;
  1038. r = request_threaded_irq(iommu->dev->irq,
  1039. amd_iommu_int_handler,
  1040. amd_iommu_int_thread,
  1041. 0, "AMD-Vi",
  1042. iommu->dev);
  1043. if (r) {
  1044. pci_disable_msi(iommu->dev);
  1045. return r;
  1046. }
  1047. iommu->int_enabled = true;
  1048. return 0;
  1049. }
  1050. static int iommu_init_msi(struct amd_iommu *iommu)
  1051. {
  1052. int ret;
  1053. if (iommu->int_enabled)
  1054. goto enable_faults;
  1055. if (pci_find_capability(iommu->dev, PCI_CAP_ID_MSI))
  1056. ret = iommu_setup_msi(iommu);
  1057. else
  1058. ret = -ENODEV;
  1059. if (ret)
  1060. return ret;
  1061. enable_faults:
  1062. iommu_feature_enable(iommu, CONTROL_EVT_INT_EN);
  1063. if (iommu->ppr_log != NULL)
  1064. iommu_feature_enable(iommu, CONTROL_PPFINT_EN);
  1065. return 0;
  1066. }
  1067. /****************************************************************************
  1068. *
  1069. * The next functions belong to the third pass of parsing the ACPI
  1070. * table. In this last pass the memory mapping requirements are
  1071. * gathered (like exclusion and unity mapping ranges).
  1072. *
  1073. ****************************************************************************/
  1074. static void __init free_unity_maps(void)
  1075. {
  1076. struct unity_map_entry *entry, *next;
  1077. list_for_each_entry_safe(entry, next, &amd_iommu_unity_map, list) {
  1078. list_del(&entry->list);
  1079. kfree(entry);
  1080. }
  1081. }
  1082. /* called when we find an exclusion range definition in ACPI */
  1083. static int __init init_exclusion_range(struct ivmd_header *m)
  1084. {
  1085. int i;
  1086. switch (m->type) {
  1087. case ACPI_IVMD_TYPE:
  1088. set_device_exclusion_range(m->devid, m);
  1089. break;
  1090. case ACPI_IVMD_TYPE_ALL:
  1091. for (i = 0; i <= amd_iommu_last_bdf; ++i)
  1092. set_device_exclusion_range(i, m);
  1093. break;
  1094. case ACPI_IVMD_TYPE_RANGE:
  1095. for (i = m->devid; i <= m->aux; ++i)
  1096. set_device_exclusion_range(i, m);
  1097. break;
  1098. default:
  1099. break;
  1100. }
  1101. return 0;
  1102. }
  1103. /* called for unity map ACPI definition */
  1104. static int __init init_unity_map_range(struct ivmd_header *m)
  1105. {
  1106. struct unity_map_entry *e = NULL;
  1107. char *s;
  1108. e = kzalloc(sizeof(*e), GFP_KERNEL);
  1109. if (e == NULL)
  1110. return -ENOMEM;
  1111. switch (m->type) {
  1112. default:
  1113. kfree(e);
  1114. return 0;
  1115. case ACPI_IVMD_TYPE:
  1116. s = "IVMD_TYPEi\t\t\t";
  1117. e->devid_start = e->devid_end = m->devid;
  1118. break;
  1119. case ACPI_IVMD_TYPE_ALL:
  1120. s = "IVMD_TYPE_ALL\t\t";
  1121. e->devid_start = 0;
  1122. e->devid_end = amd_iommu_last_bdf;
  1123. break;
  1124. case ACPI_IVMD_TYPE_RANGE:
  1125. s = "IVMD_TYPE_RANGE\t\t";
  1126. e->devid_start = m->devid;
  1127. e->devid_end = m->aux;
  1128. break;
  1129. }
  1130. e->address_start = PAGE_ALIGN(m->range_start);
  1131. e->address_end = e->address_start + PAGE_ALIGN(m->range_length);
  1132. e->prot = m->flags >> 1;
  1133. DUMP_printk("%s devid_start: %02x:%02x.%x devid_end: %02x:%02x.%x"
  1134. " range_start: %016llx range_end: %016llx flags: %x\n", s,
  1135. PCI_BUS(e->devid_start), PCI_SLOT(e->devid_start),
  1136. PCI_FUNC(e->devid_start), PCI_BUS(e->devid_end),
  1137. PCI_SLOT(e->devid_end), PCI_FUNC(e->devid_end),
  1138. e->address_start, e->address_end, m->flags);
  1139. list_add_tail(&e->list, &amd_iommu_unity_map);
  1140. return 0;
  1141. }
  1142. /* iterates over all memory definitions we find in the ACPI table */
  1143. static int __init init_memory_definitions(struct acpi_table_header *table)
  1144. {
  1145. u8 *p = (u8 *)table, *end = (u8 *)table;
  1146. struct ivmd_header *m;
  1147. end += table->length;
  1148. p += IVRS_HEADER_LENGTH;
  1149. while (p < end) {
  1150. m = (struct ivmd_header *)p;
  1151. if (m->flags & IVMD_FLAG_EXCL_RANGE)
  1152. init_exclusion_range(m);
  1153. else if (m->flags & IVMD_FLAG_UNITY_MAP)
  1154. init_unity_map_range(m);
  1155. p += m->length;
  1156. }
  1157. return 0;
  1158. }
  1159. /*
  1160. * Init the device table to not allow DMA access for devices and
  1161. * suppress all page faults
  1162. */
  1163. static void init_device_table_dma(void)
  1164. {
  1165. u32 devid;
  1166. for (devid = 0; devid <= amd_iommu_last_bdf; ++devid) {
  1167. set_dev_entry_bit(devid, DEV_ENTRY_VALID);
  1168. set_dev_entry_bit(devid, DEV_ENTRY_TRANSLATION);
  1169. }
  1170. }
  1171. static void __init uninit_device_table_dma(void)
  1172. {
  1173. u32 devid;
  1174. for (devid = 0; devid <= amd_iommu_last_bdf; ++devid) {
  1175. amd_iommu_dev_table[devid].data[0] = 0ULL;
  1176. amd_iommu_dev_table[devid].data[1] = 0ULL;
  1177. }
  1178. }
  1179. static void init_device_table(void)
  1180. {
  1181. u32 devid;
  1182. if (!amd_iommu_irq_remap)
  1183. return;
  1184. for (devid = 0; devid <= amd_iommu_last_bdf; ++devid)
  1185. set_dev_entry_bit(devid, DEV_ENTRY_IRQ_TBL_EN);
  1186. }
  1187. static void iommu_init_flags(struct amd_iommu *iommu)
  1188. {
  1189. iommu->acpi_flags & IVHD_FLAG_HT_TUN_EN_MASK ?
  1190. iommu_feature_enable(iommu, CONTROL_HT_TUN_EN) :
  1191. iommu_feature_disable(iommu, CONTROL_HT_TUN_EN);
  1192. iommu->acpi_flags & IVHD_FLAG_PASSPW_EN_MASK ?
  1193. iommu_feature_enable(iommu, CONTROL_PASSPW_EN) :
  1194. iommu_feature_disable(iommu, CONTROL_PASSPW_EN);
  1195. iommu->acpi_flags & IVHD_FLAG_RESPASSPW_EN_MASK ?
  1196. iommu_feature_enable(iommu, CONTROL_RESPASSPW_EN) :
  1197. iommu_feature_disable(iommu, CONTROL_RESPASSPW_EN);
  1198. iommu->acpi_flags & IVHD_FLAG_ISOC_EN_MASK ?
  1199. iommu_feature_enable(iommu, CONTROL_ISOC_EN) :
  1200. iommu_feature_disable(iommu, CONTROL_ISOC_EN);
  1201. /*
  1202. * make IOMMU memory accesses cache coherent
  1203. */
  1204. iommu_feature_enable(iommu, CONTROL_COHERENT_EN);
  1205. /* Set IOTLB invalidation timeout to 1s */
  1206. iommu_set_inv_tlb_timeout(iommu, CTRL_INV_TO_1S);
  1207. }
  1208. static void iommu_apply_resume_quirks(struct amd_iommu *iommu)
  1209. {
  1210. int i, j;
  1211. u32 ioc_feature_control;
  1212. struct pci_dev *pdev = iommu->root_pdev;
  1213. /* RD890 BIOSes may not have completely reconfigured the iommu */
  1214. if (!is_rd890_iommu(iommu->dev) || !pdev)
  1215. return;
  1216. /*
  1217. * First, we need to ensure that the iommu is enabled. This is
  1218. * controlled by a register in the northbridge
  1219. */
  1220. /* Select Northbridge indirect register 0x75 and enable writing */
  1221. pci_write_config_dword(pdev, 0x60, 0x75 | (1 << 7));
  1222. pci_read_config_dword(pdev, 0x64, &ioc_feature_control);
  1223. /* Enable the iommu */
  1224. if (!(ioc_feature_control & 0x1))
  1225. pci_write_config_dword(pdev, 0x64, ioc_feature_control | 1);
  1226. /* Restore the iommu BAR */
  1227. pci_write_config_dword(iommu->dev, iommu->cap_ptr + 4,
  1228. iommu->stored_addr_lo);
  1229. pci_write_config_dword(iommu->dev, iommu->cap_ptr + 8,
  1230. iommu->stored_addr_hi);
  1231. /* Restore the l1 indirect regs for each of the 6 l1s */
  1232. for (i = 0; i < 6; i++)
  1233. for (j = 0; j < 0x12; j++)
  1234. iommu_write_l1(iommu, i, j, iommu->stored_l1[i][j]);
  1235. /* Restore the l2 indirect regs */
  1236. for (i = 0; i < 0x83; i++)
  1237. iommu_write_l2(iommu, i, iommu->stored_l2[i]);
  1238. /* Lock PCI setup registers */
  1239. pci_write_config_dword(iommu->dev, iommu->cap_ptr + 4,
  1240. iommu->stored_addr_lo | 1);
  1241. }
  1242. /*
  1243. * This function finally enables all IOMMUs found in the system after
  1244. * they have been initialized
  1245. */
  1246. static void early_enable_iommus(void)
  1247. {
  1248. struct amd_iommu *iommu;
  1249. for_each_iommu(iommu) {
  1250. iommu_disable(iommu);
  1251. iommu_init_flags(iommu);
  1252. iommu_set_device_table(iommu);
  1253. iommu_enable_command_buffer(iommu);
  1254. iommu_enable_event_buffer(iommu);
  1255. iommu_set_exclusion_range(iommu);
  1256. iommu_enable(iommu);
  1257. iommu_flush_all_caches(iommu);
  1258. }
  1259. }
  1260. static void enable_iommus_v2(void)
  1261. {
  1262. struct amd_iommu *iommu;
  1263. for_each_iommu(iommu) {
  1264. iommu_enable_ppr_log(iommu);
  1265. iommu_enable_gt(iommu);
  1266. }
  1267. }
  1268. static void enable_iommus(void)
  1269. {
  1270. early_enable_iommus();
  1271. enable_iommus_v2();
  1272. }
  1273. static void disable_iommus(void)
  1274. {
  1275. struct amd_iommu *iommu;
  1276. for_each_iommu(iommu)
  1277. iommu_disable(iommu);
  1278. }
  1279. /*
  1280. * Suspend/Resume support
  1281. * disable suspend until real resume implemented
  1282. */
  1283. static void amd_iommu_resume(void)
  1284. {
  1285. struct amd_iommu *iommu;
  1286. for_each_iommu(iommu)
  1287. iommu_apply_resume_quirks(iommu);
  1288. /* re-load the hardware */
  1289. enable_iommus();
  1290. amd_iommu_enable_interrupts();
  1291. }
  1292. static int amd_iommu_suspend(void)
  1293. {
  1294. /* disable IOMMUs to go out of the way for BIOS */
  1295. disable_iommus();
  1296. return 0;
  1297. }
  1298. static struct syscore_ops amd_iommu_syscore_ops = {
  1299. .suspend = amd_iommu_suspend,
  1300. .resume = amd_iommu_resume,
  1301. };
  1302. static void __init free_on_init_error(void)
  1303. {
  1304. free_pages((unsigned long)irq_lookup_table,
  1305. get_order(rlookup_table_size));
  1306. if (amd_iommu_irq_cache) {
  1307. kmem_cache_destroy(amd_iommu_irq_cache);
  1308. amd_iommu_irq_cache = NULL;
  1309. }
  1310. free_pages((unsigned long)amd_iommu_rlookup_table,
  1311. get_order(rlookup_table_size));
  1312. free_pages((unsigned long)amd_iommu_alias_table,
  1313. get_order(alias_table_size));
  1314. free_pages((unsigned long)amd_iommu_dev_table,
  1315. get_order(dev_table_size));
  1316. free_iommu_all();
  1317. #ifdef CONFIG_GART_IOMMU
  1318. /*
  1319. * We failed to initialize the AMD IOMMU - try fallback to GART
  1320. * if possible.
  1321. */
  1322. gart_iommu_init();
  1323. #endif
  1324. }
  1325. /* SB IOAPIC is always on this device in AMD systems */
  1326. #define IOAPIC_SB_DEVID ((0x00 << 8) | PCI_DEVFN(0x14, 0))
  1327. static bool __init check_ioapic_information(void)
  1328. {
  1329. bool ret, has_sb_ioapic;
  1330. int idx;
  1331. has_sb_ioapic = false;
  1332. ret = false;
  1333. for (idx = 0; idx < nr_ioapics; idx++) {
  1334. int devid, id = mpc_ioapic_id(idx);
  1335. devid = get_ioapic_devid(id);
  1336. if (devid < 0) {
  1337. pr_err(FW_BUG "AMD-Vi: IOAPIC[%d] not in IVRS table\n", id);
  1338. ret = false;
  1339. } else if (devid == IOAPIC_SB_DEVID) {
  1340. has_sb_ioapic = true;
  1341. ret = true;
  1342. }
  1343. }
  1344. if (!has_sb_ioapic) {
  1345. /*
  1346. * We expect the SB IOAPIC to be listed in the IVRS
  1347. * table. The system timer is connected to the SB IOAPIC
  1348. * and if we don't have it in the list the system will
  1349. * panic at boot time. This situation usually happens
  1350. * when the BIOS is buggy and provides us the wrong
  1351. * device id for the IOAPIC in the system.
  1352. */
  1353. pr_err(FW_BUG "AMD-Vi: No southbridge IOAPIC found in IVRS table\n");
  1354. }
  1355. if (!ret)
  1356. pr_err("AMD-Vi: Disabling interrupt remapping due to BIOS Bug(s)\n");
  1357. return ret;
  1358. }
  1359. static void __init free_dma_resources(void)
  1360. {
  1361. amd_iommu_uninit_devices();
  1362. free_pages((unsigned long)amd_iommu_pd_alloc_bitmap,
  1363. get_order(MAX_DOMAIN_ID/8));
  1364. free_unity_maps();
  1365. }
  1366. /*
  1367. * This is the hardware init function for AMD IOMMU in the system.
  1368. * This function is called either from amd_iommu_init or from the interrupt
  1369. * remapping setup code.
  1370. *
  1371. * This function basically parses the ACPI table for AMD IOMMU (IVRS)
  1372. * three times:
  1373. *
  1374. * 1 pass) Find the highest PCI device id the driver has to handle.
  1375. * Upon this information the size of the data structures is
  1376. * determined that needs to be allocated.
  1377. *
  1378. * 2 pass) Initialize the data structures just allocated with the
  1379. * information in the ACPI table about available AMD IOMMUs
  1380. * in the system. It also maps the PCI devices in the
  1381. * system to specific IOMMUs
  1382. *
  1383. * 3 pass) After the basic data structures are allocated and
  1384. * initialized we update them with information about memory
  1385. * remapping requirements parsed out of the ACPI table in
  1386. * this last pass.
  1387. *
  1388. * After everything is set up the IOMMUs are enabled and the necessary
  1389. * hotplug and suspend notifiers are registered.
  1390. */
  1391. static int __init early_amd_iommu_init(void)
  1392. {
  1393. struct acpi_table_header *ivrs_base;
  1394. acpi_size ivrs_size;
  1395. acpi_status status;
  1396. int i, ret = 0;
  1397. if (!amd_iommu_detected)
  1398. return -ENODEV;
  1399. status = acpi_get_table_with_size("IVRS", 0, &ivrs_base, &ivrs_size);
  1400. if (status == AE_NOT_FOUND)
  1401. return -ENODEV;
  1402. else if (ACPI_FAILURE(status)) {
  1403. const char *err = acpi_format_exception(status);
  1404. pr_err("AMD-Vi: IVRS table error: %s\n", err);
  1405. return -EINVAL;
  1406. }
  1407. /*
  1408. * First parse ACPI tables to find the largest Bus/Dev/Func
  1409. * we need to handle. Upon this information the shared data
  1410. * structures for the IOMMUs in the system will be allocated
  1411. */
  1412. ret = find_last_devid_acpi(ivrs_base);
  1413. if (ret)
  1414. goto out;
  1415. dev_table_size = tbl_size(DEV_TABLE_ENTRY_SIZE);
  1416. alias_table_size = tbl_size(ALIAS_TABLE_ENTRY_SIZE);
  1417. rlookup_table_size = tbl_size(RLOOKUP_TABLE_ENTRY_SIZE);
  1418. /* Device table - directly used by all IOMMUs */
  1419. ret = -ENOMEM;
  1420. amd_iommu_dev_table = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
  1421. get_order(dev_table_size));
  1422. if (amd_iommu_dev_table == NULL)
  1423. goto out;
  1424. /*
  1425. * Alias table - map PCI Bus/Dev/Func to Bus/Dev/Func the
  1426. * IOMMU see for that device
  1427. */
  1428. amd_iommu_alias_table = (void *)__get_free_pages(GFP_KERNEL,
  1429. get_order(alias_table_size));
  1430. if (amd_iommu_alias_table == NULL)
  1431. goto out;
  1432. /* IOMMU rlookup table - find the IOMMU for a specific device */
  1433. amd_iommu_rlookup_table = (void *)__get_free_pages(
  1434. GFP_KERNEL | __GFP_ZERO,
  1435. get_order(rlookup_table_size));
  1436. if (amd_iommu_rlookup_table == NULL)
  1437. goto out;
  1438. amd_iommu_pd_alloc_bitmap = (void *)__get_free_pages(
  1439. GFP_KERNEL | __GFP_ZERO,
  1440. get_order(MAX_DOMAIN_ID/8));
  1441. if (amd_iommu_pd_alloc_bitmap == NULL)
  1442. goto out;
  1443. /*
  1444. * let all alias entries point to itself
  1445. */
  1446. for (i = 0; i <= amd_iommu_last_bdf; ++i)
  1447. amd_iommu_alias_table[i] = i;
  1448. /*
  1449. * never allocate domain 0 because its used as the non-allocated and
  1450. * error value placeholder
  1451. */
  1452. amd_iommu_pd_alloc_bitmap[0] = 1;
  1453. spin_lock_init(&amd_iommu_pd_lock);
  1454. /*
  1455. * now the data structures are allocated and basically initialized
  1456. * start the real acpi table scan
  1457. */
  1458. ret = init_iommu_all(ivrs_base);
  1459. if (ret)
  1460. goto out;
  1461. if (amd_iommu_irq_remap)
  1462. amd_iommu_irq_remap = check_ioapic_information();
  1463. if (amd_iommu_irq_remap) {
  1464. /*
  1465. * Interrupt remapping enabled, create kmem_cache for the
  1466. * remapping tables.
  1467. */
  1468. amd_iommu_irq_cache = kmem_cache_create("irq_remap_cache",
  1469. MAX_IRQS_PER_TABLE * sizeof(u32),
  1470. IRQ_TABLE_ALIGNMENT,
  1471. 0, NULL);
  1472. if (!amd_iommu_irq_cache)
  1473. goto out;
  1474. irq_lookup_table = (void *)__get_free_pages(
  1475. GFP_KERNEL | __GFP_ZERO,
  1476. get_order(rlookup_table_size));
  1477. if (!irq_lookup_table)
  1478. goto out;
  1479. }
  1480. ret = init_memory_definitions(ivrs_base);
  1481. if (ret)
  1482. goto out;
  1483. /* init the device table */
  1484. init_device_table();
  1485. out:
  1486. /* Don't leak any ACPI memory */
  1487. early_acpi_os_unmap_memory((char __iomem *)ivrs_base, ivrs_size);
  1488. ivrs_base = NULL;
  1489. return ret;
  1490. }
  1491. static int amd_iommu_enable_interrupts(void)
  1492. {
  1493. struct amd_iommu *iommu;
  1494. int ret = 0;
  1495. for_each_iommu(iommu) {
  1496. ret = iommu_init_msi(iommu);
  1497. if (ret)
  1498. goto out;
  1499. }
  1500. out:
  1501. return ret;
  1502. }
  1503. static bool detect_ivrs(void)
  1504. {
  1505. struct acpi_table_header *ivrs_base;
  1506. acpi_size ivrs_size;
  1507. acpi_status status;
  1508. status = acpi_get_table_with_size("IVRS", 0, &ivrs_base, &ivrs_size);
  1509. if (status == AE_NOT_FOUND)
  1510. return false;
  1511. else if (ACPI_FAILURE(status)) {
  1512. const char *err = acpi_format_exception(status);
  1513. pr_err("AMD-Vi: IVRS table error: %s\n", err);
  1514. return false;
  1515. }
  1516. early_acpi_os_unmap_memory((char __iomem *)ivrs_base, ivrs_size);
  1517. /* Make sure ACS will be enabled during PCI probe */
  1518. pci_request_acs();
  1519. if (!disable_irq_remap)
  1520. amd_iommu_irq_remap = true;
  1521. return true;
  1522. }
  1523. static int amd_iommu_init_dma(void)
  1524. {
  1525. struct amd_iommu *iommu;
  1526. int ret;
  1527. if (iommu_pass_through)
  1528. ret = amd_iommu_init_passthrough();
  1529. else
  1530. ret = amd_iommu_init_dma_ops();
  1531. if (ret)
  1532. return ret;
  1533. init_device_table_dma();
  1534. for_each_iommu(iommu)
  1535. iommu_flush_all_caches(iommu);
  1536. amd_iommu_init_api();
  1537. amd_iommu_init_notifier();
  1538. return 0;
  1539. }
  1540. /****************************************************************************
  1541. *
  1542. * AMD IOMMU Initialization State Machine
  1543. *
  1544. ****************************************************************************/
  1545. static int __init state_next(void)
  1546. {
  1547. int ret = 0;
  1548. switch (init_state) {
  1549. case IOMMU_START_STATE:
  1550. if (!detect_ivrs()) {
  1551. init_state = IOMMU_NOT_FOUND;
  1552. ret = -ENODEV;
  1553. } else {
  1554. init_state = IOMMU_IVRS_DETECTED;
  1555. }
  1556. break;
  1557. case IOMMU_IVRS_DETECTED:
  1558. ret = early_amd_iommu_init();
  1559. init_state = ret ? IOMMU_INIT_ERROR : IOMMU_ACPI_FINISHED;
  1560. break;
  1561. case IOMMU_ACPI_FINISHED:
  1562. early_enable_iommus();
  1563. register_syscore_ops(&amd_iommu_syscore_ops);
  1564. x86_platform.iommu_shutdown = disable_iommus;
  1565. init_state = IOMMU_ENABLED;
  1566. break;
  1567. case IOMMU_ENABLED:
  1568. ret = amd_iommu_init_pci();
  1569. init_state = ret ? IOMMU_INIT_ERROR : IOMMU_PCI_INIT;
  1570. enable_iommus_v2();
  1571. break;
  1572. case IOMMU_PCI_INIT:
  1573. ret = amd_iommu_enable_interrupts();
  1574. init_state = ret ? IOMMU_INIT_ERROR : IOMMU_INTERRUPTS_EN;
  1575. break;
  1576. case IOMMU_INTERRUPTS_EN:
  1577. ret = amd_iommu_init_dma();
  1578. init_state = ret ? IOMMU_INIT_ERROR : IOMMU_DMA_OPS;
  1579. break;
  1580. case IOMMU_DMA_OPS:
  1581. init_state = IOMMU_INITIALIZED;
  1582. break;
  1583. case IOMMU_INITIALIZED:
  1584. /* Nothing to do */
  1585. break;
  1586. case IOMMU_NOT_FOUND:
  1587. case IOMMU_INIT_ERROR:
  1588. /* Error states => do nothing */
  1589. ret = -EINVAL;
  1590. break;
  1591. default:
  1592. /* Unknown state */
  1593. BUG();
  1594. }
  1595. return ret;
  1596. }
  1597. static int __init iommu_go_to_state(enum iommu_init_state state)
  1598. {
  1599. int ret = 0;
  1600. while (init_state != state) {
  1601. ret = state_next();
  1602. if (init_state == IOMMU_NOT_FOUND ||
  1603. init_state == IOMMU_INIT_ERROR)
  1604. break;
  1605. }
  1606. return ret;
  1607. }
  1608. #ifdef CONFIG_IRQ_REMAP
  1609. int __init amd_iommu_prepare(void)
  1610. {
  1611. return iommu_go_to_state(IOMMU_ACPI_FINISHED);
  1612. }
  1613. int __init amd_iommu_supported(void)
  1614. {
  1615. return amd_iommu_irq_remap ? 1 : 0;
  1616. }
  1617. int __init amd_iommu_enable(void)
  1618. {
  1619. int ret;
  1620. ret = iommu_go_to_state(IOMMU_ENABLED);
  1621. if (ret)
  1622. return ret;
  1623. irq_remapping_enabled = 1;
  1624. return 0;
  1625. }
  1626. void amd_iommu_disable(void)
  1627. {
  1628. amd_iommu_suspend();
  1629. }
  1630. int amd_iommu_reenable(int mode)
  1631. {
  1632. amd_iommu_resume();
  1633. return 0;
  1634. }
  1635. int __init amd_iommu_enable_faulting(void)
  1636. {
  1637. /* We enable MSI later when PCI is initialized */
  1638. return 0;
  1639. }
  1640. #endif
  1641. /*
  1642. * This is the core init function for AMD IOMMU hardware in the system.
  1643. * This function is called from the generic x86 DMA layer initialization
  1644. * code.
  1645. */
  1646. static int __init amd_iommu_init(void)
  1647. {
  1648. int ret;
  1649. ret = iommu_go_to_state(IOMMU_INITIALIZED);
  1650. if (ret) {
  1651. free_dma_resources();
  1652. if (!irq_remapping_enabled) {
  1653. disable_iommus();
  1654. free_on_init_error();
  1655. } else {
  1656. struct amd_iommu *iommu;
  1657. uninit_device_table_dma();
  1658. for_each_iommu(iommu)
  1659. iommu_flush_all_caches(iommu);
  1660. }
  1661. }
  1662. return ret;
  1663. }
  1664. /****************************************************************************
  1665. *
  1666. * Early detect code. This code runs at IOMMU detection time in the DMA
  1667. * layer. It just looks if there is an IVRS ACPI table to detect AMD
  1668. * IOMMUs
  1669. *
  1670. ****************************************************************************/
  1671. int __init amd_iommu_detect(void)
  1672. {
  1673. int ret;
  1674. if (no_iommu || (iommu_detected && !gart_iommu_aperture))
  1675. return -ENODEV;
  1676. if (amd_iommu_disabled)
  1677. return -ENODEV;
  1678. ret = iommu_go_to_state(IOMMU_IVRS_DETECTED);
  1679. if (ret)
  1680. return ret;
  1681. amd_iommu_detected = true;
  1682. iommu_detected = 1;
  1683. x86_init.iommu.iommu_init = amd_iommu_init;
  1684. return 0;
  1685. }
  1686. /****************************************************************************
  1687. *
  1688. * Parsing functions for the AMD IOMMU specific kernel command line
  1689. * options.
  1690. *
  1691. ****************************************************************************/
  1692. static int __init parse_amd_iommu_dump(char *str)
  1693. {
  1694. amd_iommu_dump = true;
  1695. return 1;
  1696. }
  1697. static int __init parse_amd_iommu_options(char *str)
  1698. {
  1699. for (; *str; ++str) {
  1700. if (strncmp(str, "fullflush", 9) == 0)
  1701. amd_iommu_unmap_flush = true;
  1702. if (strncmp(str, "off", 3) == 0)
  1703. amd_iommu_disabled = true;
  1704. if (strncmp(str, "force_isolation", 15) == 0)
  1705. amd_iommu_force_isolation = true;
  1706. }
  1707. return 1;
  1708. }
  1709. __setup("amd_iommu_dump", parse_amd_iommu_dump);
  1710. __setup("amd_iommu=", parse_amd_iommu_options);
  1711. IOMMU_INIT_FINISH(amd_iommu_detect,
  1712. gart_iommu_hole_init,
  1713. NULL,
  1714. NULL);
  1715. bool amd_iommu_v2_supported(void)
  1716. {
  1717. return amd_iommu_v2_present;
  1718. }
  1719. EXPORT_SYMBOL(amd_iommu_v2_supported);