amd_iommu_init.c 67 KB

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