amd_iommu_init.c 67 KB

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