amd_iommu_init.c 67 KB

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