amd_iommu_init.c 57 KB

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