amd_iommu_init.c 57 KB

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