amd_iommu_init.c 63 KB

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