amd_iommu_init.c 58 KB

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