amd_iommu_init.c 52 KB

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