amd_iommu_init.c 69 KB

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