amd_iommu_init.c 56 KB

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