amd_iommu_init.c 67 KB

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