amd_iommu_init.c 69 KB

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