amd_iommu_init.c 58 KB

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