amd_iommu_init.c 56 KB

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