amd_iommu_init.c 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324
  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. * After reading the highest device id from the IOMMU PCI capability header
  345. * this function looks if there is a higher device id defined in the ACPI table
  346. */
  347. static int __init find_last_devid_from_ivhd(struct ivhd_header *h)
  348. {
  349. u8 *p = (void *)h, *end = (void *)h;
  350. struct ivhd_entry *dev;
  351. p += sizeof(*h);
  352. end += h->length;
  353. while (p < end) {
  354. dev = (struct ivhd_entry *)p;
  355. switch (dev->type) {
  356. case IVHD_DEV_ALL:
  357. /* Use maximum BDF value for DEV_ALL */
  358. update_last_devid(0xffff);
  359. break;
  360. case IVHD_DEV_SELECT:
  361. case IVHD_DEV_RANGE_END:
  362. case IVHD_DEV_ALIAS:
  363. case IVHD_DEV_EXT_SELECT:
  364. /* all the above subfield types refer to device ids */
  365. update_last_devid(dev->devid);
  366. break;
  367. default:
  368. break;
  369. }
  370. p += ivhd_entry_length(p);
  371. }
  372. WARN_ON(p != end);
  373. return 0;
  374. }
  375. /*
  376. * Iterate over all IVHD entries in the ACPI table and find the highest device
  377. * id which we need to handle. This is the first of three functions which parse
  378. * the ACPI table. So we check the checksum here.
  379. */
  380. static int __init find_last_devid_acpi(struct acpi_table_header *table)
  381. {
  382. int i;
  383. u8 checksum = 0, *p = (u8 *)table, *end = (u8 *)table;
  384. struct ivhd_header *h;
  385. /*
  386. * Validate checksum here so we don't need to do it when
  387. * we actually parse the table
  388. */
  389. for (i = 0; i < table->length; ++i)
  390. checksum += p[i];
  391. if (checksum != 0)
  392. /* ACPI table corrupt */
  393. return -ENODEV;
  394. p += IVRS_HEADER_LENGTH;
  395. end += table->length;
  396. while (p < end) {
  397. h = (struct ivhd_header *)p;
  398. switch (h->type) {
  399. case ACPI_IVHD_TYPE:
  400. find_last_devid_from_ivhd(h);
  401. break;
  402. default:
  403. break;
  404. }
  405. p += h->length;
  406. }
  407. WARN_ON(p != end);
  408. return 0;
  409. }
  410. /****************************************************************************
  411. *
  412. * The following functions belong to the code path which parses the ACPI table
  413. * the second time. In this ACPI parsing iteration we allocate IOMMU specific
  414. * data structures, initialize the device/alias/rlookup table and also
  415. * basically initialize the hardware.
  416. *
  417. ****************************************************************************/
  418. /*
  419. * Allocates the command buffer. This buffer is per AMD IOMMU. We can
  420. * write commands to that buffer later and the IOMMU will execute them
  421. * asynchronously
  422. */
  423. static int __init alloc_command_buffer(struct amd_iommu *iommu)
  424. {
  425. iommu->cmd_buf = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
  426. get_order(CMD_BUFFER_SIZE));
  427. return iommu->cmd_buf ? 0 : -ENOMEM;
  428. }
  429. /*
  430. * This function resets the command buffer if the IOMMU stopped fetching
  431. * commands from it.
  432. */
  433. void amd_iommu_reset_cmd_buffer(struct amd_iommu *iommu)
  434. {
  435. iommu_feature_disable(iommu, CONTROL_CMDBUF_EN);
  436. writel(0x00, iommu->mmio_base + MMIO_CMD_HEAD_OFFSET);
  437. writel(0x00, iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
  438. iommu_feature_enable(iommu, CONTROL_CMDBUF_EN);
  439. }
  440. /*
  441. * This function writes the command buffer address to the hardware and
  442. * enables it.
  443. */
  444. static void iommu_enable_command_buffer(struct amd_iommu *iommu)
  445. {
  446. u64 entry;
  447. BUG_ON(iommu->cmd_buf == NULL);
  448. entry = (u64)virt_to_phys(iommu->cmd_buf);
  449. entry |= MMIO_CMD_SIZE_512;
  450. memcpy_toio(iommu->mmio_base + MMIO_CMD_BUF_OFFSET,
  451. &entry, sizeof(entry));
  452. amd_iommu_reset_cmd_buffer(iommu);
  453. }
  454. static void __init free_command_buffer(struct amd_iommu *iommu)
  455. {
  456. free_pages((unsigned long)iommu->cmd_buf, get_order(CMD_BUFFER_SIZE));
  457. }
  458. /* allocates the memory where the IOMMU will log its events to */
  459. static int __init alloc_event_buffer(struct amd_iommu *iommu)
  460. {
  461. iommu->evt_buf = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
  462. get_order(EVT_BUFFER_SIZE));
  463. return iommu->evt_buf ? 0 : -ENOMEM;
  464. }
  465. static void iommu_enable_event_buffer(struct amd_iommu *iommu)
  466. {
  467. u64 entry;
  468. BUG_ON(iommu->evt_buf == NULL);
  469. entry = (u64)virt_to_phys(iommu->evt_buf) | EVT_LEN_MASK;
  470. memcpy_toio(iommu->mmio_base + MMIO_EVT_BUF_OFFSET,
  471. &entry, sizeof(entry));
  472. /* set head and tail to zero manually */
  473. writel(0x00, iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
  474. writel(0x00, iommu->mmio_base + MMIO_EVT_TAIL_OFFSET);
  475. iommu_feature_enable(iommu, CONTROL_EVT_LOG_EN);
  476. }
  477. static void __init free_event_buffer(struct amd_iommu *iommu)
  478. {
  479. free_pages((unsigned long)iommu->evt_buf, get_order(EVT_BUFFER_SIZE));
  480. }
  481. /* allocates the memory where the IOMMU will log its events to */
  482. static int __init alloc_ppr_log(struct amd_iommu *iommu)
  483. {
  484. iommu->ppr_log = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
  485. get_order(PPR_LOG_SIZE));
  486. return iommu->ppr_log ? 0 : -ENOMEM;
  487. }
  488. static void iommu_enable_ppr_log(struct amd_iommu *iommu)
  489. {
  490. u64 entry;
  491. if (iommu->ppr_log == NULL)
  492. return;
  493. entry = (u64)virt_to_phys(iommu->ppr_log) | PPR_LOG_SIZE_512;
  494. memcpy_toio(iommu->mmio_base + MMIO_PPR_LOG_OFFSET,
  495. &entry, sizeof(entry));
  496. /* set head and tail to zero manually */
  497. writel(0x00, iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
  498. writel(0x00, iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
  499. iommu_feature_enable(iommu, CONTROL_PPFLOG_EN);
  500. iommu_feature_enable(iommu, CONTROL_PPR_EN);
  501. }
  502. static void __init free_ppr_log(struct amd_iommu *iommu)
  503. {
  504. if (iommu->ppr_log == NULL)
  505. return;
  506. free_pages((unsigned long)iommu->ppr_log, get_order(PPR_LOG_SIZE));
  507. }
  508. static void iommu_enable_gt(struct amd_iommu *iommu)
  509. {
  510. if (!iommu_feature(iommu, FEATURE_GT))
  511. return;
  512. iommu_feature_enable(iommu, CONTROL_GT_EN);
  513. }
  514. /* sets a specific bit in the device table entry. */
  515. static void set_dev_entry_bit(u16 devid, u8 bit)
  516. {
  517. int i = (bit >> 6) & 0x03;
  518. int _bit = bit & 0x3f;
  519. amd_iommu_dev_table[devid].data[i] |= (1UL << _bit);
  520. }
  521. static int get_dev_entry_bit(u16 devid, u8 bit)
  522. {
  523. int i = (bit >> 6) & 0x03;
  524. int _bit = bit & 0x3f;
  525. return (amd_iommu_dev_table[devid].data[i] & (1UL << _bit)) >> _bit;
  526. }
  527. void amd_iommu_apply_erratum_63(u16 devid)
  528. {
  529. int sysmgt;
  530. sysmgt = get_dev_entry_bit(devid, DEV_ENTRY_SYSMGT1) |
  531. (get_dev_entry_bit(devid, DEV_ENTRY_SYSMGT2) << 1);
  532. if (sysmgt == 0x01)
  533. set_dev_entry_bit(devid, DEV_ENTRY_IW);
  534. }
  535. /* Writes the specific IOMMU for a device into the rlookup table */
  536. static void __init set_iommu_for_device(struct amd_iommu *iommu, u16 devid)
  537. {
  538. amd_iommu_rlookup_table[devid] = iommu;
  539. }
  540. /*
  541. * This function takes the device specific flags read from the ACPI
  542. * table and sets up the device table entry with that information
  543. */
  544. static void __init set_dev_entry_from_acpi(struct amd_iommu *iommu,
  545. u16 devid, u32 flags, u32 ext_flags)
  546. {
  547. if (flags & ACPI_DEVFLAG_INITPASS)
  548. set_dev_entry_bit(devid, DEV_ENTRY_INIT_PASS);
  549. if (flags & ACPI_DEVFLAG_EXTINT)
  550. set_dev_entry_bit(devid, DEV_ENTRY_EINT_PASS);
  551. if (flags & ACPI_DEVFLAG_NMI)
  552. set_dev_entry_bit(devid, DEV_ENTRY_NMI_PASS);
  553. if (flags & ACPI_DEVFLAG_SYSMGT1)
  554. set_dev_entry_bit(devid, DEV_ENTRY_SYSMGT1);
  555. if (flags & ACPI_DEVFLAG_SYSMGT2)
  556. set_dev_entry_bit(devid, DEV_ENTRY_SYSMGT2);
  557. if (flags & ACPI_DEVFLAG_LINT0)
  558. set_dev_entry_bit(devid, DEV_ENTRY_LINT0_PASS);
  559. if (flags & ACPI_DEVFLAG_LINT1)
  560. set_dev_entry_bit(devid, DEV_ENTRY_LINT1_PASS);
  561. amd_iommu_apply_erratum_63(devid);
  562. set_iommu_for_device(iommu, devid);
  563. }
  564. static int __init add_special_device(u8 type, u8 id, u16 *devid, bool cmd_line)
  565. {
  566. struct devid_map *entry;
  567. struct list_head *list;
  568. if (type == IVHD_SPECIAL_IOAPIC)
  569. list = &ioapic_map;
  570. else if (type == IVHD_SPECIAL_HPET)
  571. list = &hpet_map;
  572. else
  573. return -EINVAL;
  574. list_for_each_entry(entry, list, list) {
  575. if (!(entry->id == id && entry->cmd_line))
  576. continue;
  577. pr_info("AMD-Vi: Command-line override present for %s id %d - ignoring\n",
  578. type == IVHD_SPECIAL_IOAPIC ? "IOAPIC" : "HPET", id);
  579. *devid = entry->devid;
  580. return 0;
  581. }
  582. entry = kzalloc(sizeof(*entry), GFP_KERNEL);
  583. if (!entry)
  584. return -ENOMEM;
  585. entry->id = id;
  586. entry->devid = *devid;
  587. entry->cmd_line = cmd_line;
  588. list_add_tail(&entry->list, list);
  589. return 0;
  590. }
  591. static int __init add_early_maps(void)
  592. {
  593. int i, ret;
  594. for (i = 0; i < early_ioapic_map_size; ++i) {
  595. ret = add_special_device(IVHD_SPECIAL_IOAPIC,
  596. early_ioapic_map[i].id,
  597. &early_ioapic_map[i].devid,
  598. early_ioapic_map[i].cmd_line);
  599. if (ret)
  600. return ret;
  601. }
  602. for (i = 0; i < early_hpet_map_size; ++i) {
  603. ret = add_special_device(IVHD_SPECIAL_HPET,
  604. early_hpet_map[i].id,
  605. &early_hpet_map[i].devid,
  606. early_hpet_map[i].cmd_line);
  607. if (ret)
  608. return ret;
  609. }
  610. return 0;
  611. }
  612. /*
  613. * Reads the device exclusion range from ACPI and initializes the IOMMU with
  614. * it
  615. */
  616. static void __init set_device_exclusion_range(u16 devid, struct ivmd_header *m)
  617. {
  618. struct amd_iommu *iommu = amd_iommu_rlookup_table[devid];
  619. if (!(m->flags & IVMD_FLAG_EXCL_RANGE))
  620. return;
  621. if (iommu) {
  622. /*
  623. * We only can configure exclusion ranges per IOMMU, not
  624. * per device. But we can enable the exclusion range per
  625. * device. This is done here
  626. */
  627. set_dev_entry_bit(devid, DEV_ENTRY_EX);
  628. iommu->exclusion_start = m->range_start;
  629. iommu->exclusion_length = m->range_length;
  630. }
  631. }
  632. /*
  633. * Takes a pointer to an AMD IOMMU entry in the ACPI table and
  634. * initializes the hardware and our data structures with it.
  635. */
  636. static int __init init_iommu_from_acpi(struct amd_iommu *iommu,
  637. struct ivhd_header *h)
  638. {
  639. u8 *p = (u8 *)h;
  640. u8 *end = p, flags = 0;
  641. u16 devid = 0, devid_start = 0, devid_to = 0;
  642. u32 dev_i, ext_flags = 0;
  643. bool alias = false;
  644. struct ivhd_entry *e;
  645. int ret;
  646. ret = add_early_maps();
  647. if (ret)
  648. return ret;
  649. /*
  650. * First save the recommended feature enable bits from ACPI
  651. */
  652. iommu->acpi_flags = h->flags;
  653. /*
  654. * Done. Now parse the device entries
  655. */
  656. p += sizeof(struct ivhd_header);
  657. end += h->length;
  658. while (p < end) {
  659. e = (struct ivhd_entry *)p;
  660. switch (e->type) {
  661. case IVHD_DEV_ALL:
  662. DUMP_printk(" DEV_ALL\t\t\tflags: %02x\n", e->flags);
  663. for (dev_i = 0; dev_i <= amd_iommu_last_bdf; ++dev_i)
  664. set_dev_entry_from_acpi(iommu, dev_i, e->flags, 0);
  665. break;
  666. case IVHD_DEV_SELECT:
  667. DUMP_printk(" DEV_SELECT\t\t\t devid: %02x:%02x.%x "
  668. "flags: %02x\n",
  669. PCI_BUS_NUM(e->devid),
  670. PCI_SLOT(e->devid),
  671. PCI_FUNC(e->devid),
  672. e->flags);
  673. devid = e->devid;
  674. set_dev_entry_from_acpi(iommu, devid, e->flags, 0);
  675. break;
  676. case IVHD_DEV_SELECT_RANGE_START:
  677. DUMP_printk(" DEV_SELECT_RANGE_START\t "
  678. "devid: %02x:%02x.%x flags: %02x\n",
  679. PCI_BUS_NUM(e->devid),
  680. PCI_SLOT(e->devid),
  681. PCI_FUNC(e->devid),
  682. e->flags);
  683. devid_start = e->devid;
  684. flags = e->flags;
  685. ext_flags = 0;
  686. alias = false;
  687. break;
  688. case IVHD_DEV_ALIAS:
  689. DUMP_printk(" DEV_ALIAS\t\t\t devid: %02x:%02x.%x "
  690. "flags: %02x devid_to: %02x:%02x.%x\n",
  691. PCI_BUS_NUM(e->devid),
  692. PCI_SLOT(e->devid),
  693. PCI_FUNC(e->devid),
  694. e->flags,
  695. PCI_BUS_NUM(e->ext >> 8),
  696. PCI_SLOT(e->ext >> 8),
  697. PCI_FUNC(e->ext >> 8));
  698. devid = e->devid;
  699. devid_to = e->ext >> 8;
  700. set_dev_entry_from_acpi(iommu, devid , e->flags, 0);
  701. set_dev_entry_from_acpi(iommu, devid_to, e->flags, 0);
  702. amd_iommu_alias_table[devid] = devid_to;
  703. break;
  704. case IVHD_DEV_ALIAS_RANGE:
  705. DUMP_printk(" DEV_ALIAS_RANGE\t\t "
  706. "devid: %02x:%02x.%x flags: %02x "
  707. "devid_to: %02x:%02x.%x\n",
  708. PCI_BUS_NUM(e->devid),
  709. PCI_SLOT(e->devid),
  710. PCI_FUNC(e->devid),
  711. e->flags,
  712. PCI_BUS_NUM(e->ext >> 8),
  713. PCI_SLOT(e->ext >> 8),
  714. PCI_FUNC(e->ext >> 8));
  715. devid_start = e->devid;
  716. flags = e->flags;
  717. devid_to = e->ext >> 8;
  718. ext_flags = 0;
  719. alias = true;
  720. break;
  721. case IVHD_DEV_EXT_SELECT:
  722. DUMP_printk(" DEV_EXT_SELECT\t\t devid: %02x:%02x.%x "
  723. "flags: %02x ext: %08x\n",
  724. PCI_BUS_NUM(e->devid),
  725. PCI_SLOT(e->devid),
  726. PCI_FUNC(e->devid),
  727. e->flags, e->ext);
  728. devid = e->devid;
  729. set_dev_entry_from_acpi(iommu, devid, e->flags,
  730. e->ext);
  731. break;
  732. case IVHD_DEV_EXT_SELECT_RANGE:
  733. DUMP_printk(" DEV_EXT_SELECT_RANGE\t devid: "
  734. "%02x:%02x.%x flags: %02x ext: %08x\n",
  735. PCI_BUS_NUM(e->devid),
  736. PCI_SLOT(e->devid),
  737. PCI_FUNC(e->devid),
  738. e->flags, e->ext);
  739. devid_start = e->devid;
  740. flags = e->flags;
  741. ext_flags = e->ext;
  742. alias = false;
  743. break;
  744. case IVHD_DEV_RANGE_END:
  745. DUMP_printk(" DEV_RANGE_END\t\t devid: %02x:%02x.%x\n",
  746. PCI_BUS_NUM(e->devid),
  747. PCI_SLOT(e->devid),
  748. PCI_FUNC(e->devid));
  749. devid = e->devid;
  750. for (dev_i = devid_start; dev_i <= devid; ++dev_i) {
  751. if (alias) {
  752. amd_iommu_alias_table[dev_i] = devid_to;
  753. set_dev_entry_from_acpi(iommu,
  754. devid_to, flags, ext_flags);
  755. }
  756. set_dev_entry_from_acpi(iommu, dev_i,
  757. flags, ext_flags);
  758. }
  759. break;
  760. case IVHD_DEV_SPECIAL: {
  761. u8 handle, type;
  762. const char *var;
  763. u16 devid;
  764. int ret;
  765. handle = e->ext & 0xff;
  766. devid = (e->ext >> 8) & 0xffff;
  767. type = (e->ext >> 24) & 0xff;
  768. if (type == IVHD_SPECIAL_IOAPIC)
  769. var = "IOAPIC";
  770. else if (type == IVHD_SPECIAL_HPET)
  771. var = "HPET";
  772. else
  773. var = "UNKNOWN";
  774. DUMP_printk(" DEV_SPECIAL(%s[%d])\t\tdevid: %02x:%02x.%x\n",
  775. var, (int)handle,
  776. PCI_BUS_NUM(devid),
  777. PCI_SLOT(devid),
  778. PCI_FUNC(devid));
  779. ret = add_special_device(type, handle, &devid, false);
  780. if (ret)
  781. return ret;
  782. /*
  783. * add_special_device might update the devid in case a
  784. * command-line override is present. So call
  785. * set_dev_entry_from_acpi after add_special_device.
  786. */
  787. set_dev_entry_from_acpi(iommu, devid, e->flags, 0);
  788. break;
  789. }
  790. default:
  791. break;
  792. }
  793. p += ivhd_entry_length(p);
  794. }
  795. return 0;
  796. }
  797. static void __init free_iommu_one(struct amd_iommu *iommu)
  798. {
  799. free_command_buffer(iommu);
  800. free_event_buffer(iommu);
  801. free_ppr_log(iommu);
  802. iommu_unmap_mmio_space(iommu);
  803. }
  804. static void __init free_iommu_all(void)
  805. {
  806. struct amd_iommu *iommu, *next;
  807. for_each_iommu_safe(iommu, next) {
  808. list_del(&iommu->list);
  809. free_iommu_one(iommu);
  810. kfree(iommu);
  811. }
  812. }
  813. /*
  814. * Family15h Model 10h-1fh erratum 746 (IOMMU Logging May Stall Translations)
  815. * Workaround:
  816. * BIOS should disable L2B micellaneous clock gating by setting
  817. * L2_L2B_CK_GATE_CONTROL[CKGateL2BMiscDisable](D0F2xF4_x90[2]) = 1b
  818. */
  819. static void amd_iommu_erratum_746_workaround(struct amd_iommu *iommu)
  820. {
  821. u32 value;
  822. if ((boot_cpu_data.x86 != 0x15) ||
  823. (boot_cpu_data.x86_model < 0x10) ||
  824. (boot_cpu_data.x86_model > 0x1f))
  825. return;
  826. pci_write_config_dword(iommu->dev, 0xf0, 0x90);
  827. pci_read_config_dword(iommu->dev, 0xf4, &value);
  828. if (value & BIT(2))
  829. return;
  830. /* Select NB indirect register 0x90 and enable writing */
  831. pci_write_config_dword(iommu->dev, 0xf0, 0x90 | (1 << 8));
  832. pci_write_config_dword(iommu->dev, 0xf4, value | 0x4);
  833. pr_info("AMD-Vi: Applying erratum 746 workaround for IOMMU at %s\n",
  834. dev_name(&iommu->dev->dev));
  835. /* Clear the enable writing bit */
  836. pci_write_config_dword(iommu->dev, 0xf0, 0x90);
  837. }
  838. /*
  839. * This function clues the initialization function for one IOMMU
  840. * together and also allocates the command buffer and programs the
  841. * hardware. It does NOT enable the IOMMU. This is done afterwards.
  842. */
  843. static int __init init_iommu_one(struct amd_iommu *iommu, struct ivhd_header *h)
  844. {
  845. int ret;
  846. spin_lock_init(&iommu->lock);
  847. /* Add IOMMU to internal data structures */
  848. list_add_tail(&iommu->list, &amd_iommu_list);
  849. iommu->index = amd_iommus_present++;
  850. if (unlikely(iommu->index >= MAX_IOMMUS)) {
  851. WARN(1, "AMD-Vi: System has more IOMMUs than supported by this driver\n");
  852. return -ENOSYS;
  853. }
  854. /* Index is fine - add IOMMU to the array */
  855. amd_iommus[iommu->index] = iommu;
  856. /*
  857. * Copy data from ACPI table entry to the iommu struct
  858. */
  859. iommu->devid = h->devid;
  860. iommu->cap_ptr = h->cap_ptr;
  861. iommu->pci_seg = h->pci_seg;
  862. iommu->mmio_phys = h->mmio_phys;
  863. /* Check if IVHD EFR contains proper max banks/counters */
  864. if ((h->efr != 0) &&
  865. ((h->efr & (0xF << 13)) != 0) &&
  866. ((h->efr & (0x3F << 17)) != 0)) {
  867. iommu->mmio_phys_end = MMIO_REG_END_OFFSET;
  868. } else {
  869. iommu->mmio_phys_end = MMIO_CNTR_CONF_OFFSET;
  870. }
  871. iommu->mmio_base = iommu_map_mmio_space(iommu->mmio_phys,
  872. iommu->mmio_phys_end);
  873. if (!iommu->mmio_base)
  874. return -ENOMEM;
  875. if (alloc_command_buffer(iommu))
  876. return -ENOMEM;
  877. if (alloc_event_buffer(iommu))
  878. return -ENOMEM;
  879. iommu->int_enabled = false;
  880. ret = init_iommu_from_acpi(iommu, h);
  881. if (ret)
  882. return ret;
  883. ret = amd_iommu_create_irq_domain(iommu);
  884. if (ret)
  885. return ret;
  886. /*
  887. * Make sure IOMMU is not considered to translate itself. The IVRS
  888. * table tells us so, but this is a lie!
  889. */
  890. amd_iommu_rlookup_table[iommu->devid] = NULL;
  891. return 0;
  892. }
  893. /*
  894. * Iterates over all IOMMU entries in the ACPI table, allocates the
  895. * IOMMU structure and initializes it with init_iommu_one()
  896. */
  897. static int __init init_iommu_all(struct acpi_table_header *table)
  898. {
  899. u8 *p = (u8 *)table, *end = (u8 *)table;
  900. struct ivhd_header *h;
  901. struct amd_iommu *iommu;
  902. int ret;
  903. end += table->length;
  904. p += IVRS_HEADER_LENGTH;
  905. while (p < end) {
  906. h = (struct ivhd_header *)p;
  907. switch (*p) {
  908. case ACPI_IVHD_TYPE:
  909. DUMP_printk("device: %02x:%02x.%01x cap: %04x "
  910. "seg: %d flags: %01x info %04x\n",
  911. PCI_BUS_NUM(h->devid), PCI_SLOT(h->devid),
  912. PCI_FUNC(h->devid), h->cap_ptr,
  913. h->pci_seg, h->flags, h->info);
  914. DUMP_printk(" mmio-addr: %016llx\n",
  915. h->mmio_phys);
  916. iommu = kzalloc(sizeof(struct amd_iommu), GFP_KERNEL);
  917. if (iommu == NULL)
  918. return -ENOMEM;
  919. ret = init_iommu_one(iommu, h);
  920. if (ret)
  921. return ret;
  922. break;
  923. default:
  924. break;
  925. }
  926. p += h->length;
  927. }
  928. WARN_ON(p != end);
  929. return 0;
  930. }
  931. static void init_iommu_perf_ctr(struct amd_iommu *iommu)
  932. {
  933. u64 val = 0xabcd, val2 = 0;
  934. if (!iommu_feature(iommu, FEATURE_PC))
  935. return;
  936. amd_iommu_pc_present = true;
  937. /* Check if the performance counters can be written to */
  938. if ((0 != amd_iommu_pc_get_set_reg_val(0, 0, 0, 0, &val, true)) ||
  939. (0 != amd_iommu_pc_get_set_reg_val(0, 0, 0, 0, &val2, false)) ||
  940. (val != val2)) {
  941. pr_err("AMD-Vi: Unable to write to IOMMU perf counter.\n");
  942. amd_iommu_pc_present = false;
  943. return;
  944. }
  945. pr_info("AMD-Vi: IOMMU performance counters supported\n");
  946. val = readl(iommu->mmio_base + MMIO_CNTR_CONF_OFFSET);
  947. iommu->max_banks = (u8) ((val >> 12) & 0x3f);
  948. iommu->max_counters = (u8) ((val >> 7) & 0xf);
  949. }
  950. static ssize_t amd_iommu_show_cap(struct device *dev,
  951. struct device_attribute *attr,
  952. char *buf)
  953. {
  954. struct amd_iommu *iommu = dev_get_drvdata(dev);
  955. return sprintf(buf, "%x\n", iommu->cap);
  956. }
  957. static DEVICE_ATTR(cap, S_IRUGO, amd_iommu_show_cap, NULL);
  958. static ssize_t amd_iommu_show_features(struct device *dev,
  959. struct device_attribute *attr,
  960. char *buf)
  961. {
  962. struct amd_iommu *iommu = dev_get_drvdata(dev);
  963. return sprintf(buf, "%llx\n", iommu->features);
  964. }
  965. static DEVICE_ATTR(features, S_IRUGO, amd_iommu_show_features, NULL);
  966. static struct attribute *amd_iommu_attrs[] = {
  967. &dev_attr_cap.attr,
  968. &dev_attr_features.attr,
  969. NULL,
  970. };
  971. static struct attribute_group amd_iommu_group = {
  972. .name = "amd-iommu",
  973. .attrs = amd_iommu_attrs,
  974. };
  975. static const struct attribute_group *amd_iommu_groups[] = {
  976. &amd_iommu_group,
  977. NULL,
  978. };
  979. static int iommu_init_pci(struct amd_iommu *iommu)
  980. {
  981. int cap_ptr = iommu->cap_ptr;
  982. u32 range, misc, low, high;
  983. iommu->dev = pci_get_bus_and_slot(PCI_BUS_NUM(iommu->devid),
  984. iommu->devid & 0xff);
  985. if (!iommu->dev)
  986. return -ENODEV;
  987. /* Prevent binding other PCI device drivers to IOMMU devices */
  988. iommu->dev->match_driver = false;
  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. if (!(iommu->cap & (1 << IOMMU_CAP_IOTLB)))
  996. amd_iommu_iotlb_sup = false;
  997. /* read extended feature bits */
  998. low = readl(iommu->mmio_base + MMIO_EXT_FEATURES);
  999. high = readl(iommu->mmio_base + MMIO_EXT_FEATURES + 4);
  1000. iommu->features = ((u64)high << 32) | low;
  1001. if (iommu_feature(iommu, FEATURE_GT)) {
  1002. int glxval;
  1003. u32 max_pasid;
  1004. u64 pasmax;
  1005. pasmax = iommu->features & FEATURE_PASID_MASK;
  1006. pasmax >>= FEATURE_PASID_SHIFT;
  1007. max_pasid = (1 << (pasmax + 1)) - 1;
  1008. amd_iommu_max_pasid = min(amd_iommu_max_pasid, max_pasid);
  1009. BUG_ON(amd_iommu_max_pasid & ~PASID_MASK);
  1010. glxval = iommu->features & FEATURE_GLXVAL_MASK;
  1011. glxval >>= FEATURE_GLXVAL_SHIFT;
  1012. if (amd_iommu_max_glx_val == -1)
  1013. amd_iommu_max_glx_val = glxval;
  1014. else
  1015. amd_iommu_max_glx_val = min(amd_iommu_max_glx_val, glxval);
  1016. }
  1017. if (iommu_feature(iommu, FEATURE_GT) &&
  1018. iommu_feature(iommu, FEATURE_PPR)) {
  1019. iommu->is_iommu_v2 = true;
  1020. amd_iommu_v2_present = true;
  1021. }
  1022. if (iommu_feature(iommu, FEATURE_PPR) && alloc_ppr_log(iommu))
  1023. return -ENOMEM;
  1024. if (iommu->cap & (1UL << IOMMU_CAP_NPCACHE))
  1025. amd_iommu_np_cache = true;
  1026. init_iommu_perf_ctr(iommu);
  1027. if (is_rd890_iommu(iommu->dev)) {
  1028. int i, j;
  1029. iommu->root_pdev = pci_get_bus_and_slot(iommu->dev->bus->number,
  1030. PCI_DEVFN(0, 0));
  1031. /*
  1032. * Some rd890 systems may not be fully reconfigured by the
  1033. * BIOS, so it's necessary for us to store this information so
  1034. * it can be reprogrammed on resume
  1035. */
  1036. pci_read_config_dword(iommu->dev, iommu->cap_ptr + 4,
  1037. &iommu->stored_addr_lo);
  1038. pci_read_config_dword(iommu->dev, iommu->cap_ptr + 8,
  1039. &iommu->stored_addr_hi);
  1040. /* Low bit locks writes to configuration space */
  1041. iommu->stored_addr_lo &= ~1;
  1042. for (i = 0; i < 6; i++)
  1043. for (j = 0; j < 0x12; j++)
  1044. iommu->stored_l1[i][j] = iommu_read_l1(iommu, i, j);
  1045. for (i = 0; i < 0x83; i++)
  1046. iommu->stored_l2[i] = iommu_read_l2(iommu, i);
  1047. }
  1048. amd_iommu_erratum_746_workaround(iommu);
  1049. iommu->iommu_dev = iommu_device_create(&iommu->dev->dev, iommu,
  1050. amd_iommu_groups, "ivhd%d",
  1051. iommu->index);
  1052. return pci_enable_device(iommu->dev);
  1053. }
  1054. static void print_iommu_info(void)
  1055. {
  1056. static const char * const feat_str[] = {
  1057. "PreF", "PPR", "X2APIC", "NX", "GT", "[5]",
  1058. "IA", "GA", "HE", "PC"
  1059. };
  1060. struct amd_iommu *iommu;
  1061. for_each_iommu(iommu) {
  1062. int i;
  1063. pr_info("AMD-Vi: Found IOMMU at %s cap 0x%hx\n",
  1064. dev_name(&iommu->dev->dev), iommu->cap_ptr);
  1065. if (iommu->cap & (1 << IOMMU_CAP_EFR)) {
  1066. pr_info("AMD-Vi: Extended features: ");
  1067. for (i = 0; i < ARRAY_SIZE(feat_str); ++i) {
  1068. if (iommu_feature(iommu, (1ULL << i)))
  1069. pr_cont(" %s", feat_str[i]);
  1070. }
  1071. pr_cont("\n");
  1072. }
  1073. }
  1074. if (irq_remapping_enabled)
  1075. pr_info("AMD-Vi: Interrupt remapping enabled\n");
  1076. }
  1077. static int __init amd_iommu_init_pci(void)
  1078. {
  1079. struct amd_iommu *iommu;
  1080. int ret = 0;
  1081. for_each_iommu(iommu) {
  1082. ret = iommu_init_pci(iommu);
  1083. if (ret)
  1084. break;
  1085. }
  1086. init_device_table_dma();
  1087. for_each_iommu(iommu)
  1088. iommu_flush_all_caches(iommu);
  1089. ret = amd_iommu_init_api();
  1090. if (!ret)
  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. kmem_cache_destroy(amd_iommu_irq_cache);
  1377. amd_iommu_irq_cache = NULL;
  1378. free_pages((unsigned long)amd_iommu_rlookup_table,
  1379. get_order(rlookup_table_size));
  1380. free_pages((unsigned long)amd_iommu_alias_table,
  1381. get_order(alias_table_size));
  1382. free_pages((unsigned long)amd_iommu_dev_table,
  1383. get_order(dev_table_size));
  1384. free_iommu_all();
  1385. #ifdef CONFIG_GART_IOMMU
  1386. /*
  1387. * We failed to initialize the AMD IOMMU - try fallback to GART
  1388. * if possible.
  1389. */
  1390. gart_iommu_init();
  1391. #endif
  1392. }
  1393. /* SB IOAPIC is always on this device in AMD systems */
  1394. #define IOAPIC_SB_DEVID ((0x00 << 8) | PCI_DEVFN(0x14, 0))
  1395. static bool __init check_ioapic_information(void)
  1396. {
  1397. const char *fw_bug = FW_BUG;
  1398. bool ret, has_sb_ioapic;
  1399. int idx;
  1400. has_sb_ioapic = false;
  1401. ret = false;
  1402. /*
  1403. * If we have map overrides on the kernel command line the
  1404. * messages in this function might not describe firmware bugs
  1405. * anymore - so be careful
  1406. */
  1407. if (cmdline_maps)
  1408. fw_bug = "";
  1409. for (idx = 0; idx < nr_ioapics; idx++) {
  1410. int devid, id = mpc_ioapic_id(idx);
  1411. devid = get_ioapic_devid(id);
  1412. if (devid < 0) {
  1413. pr_err("%sAMD-Vi: IOAPIC[%d] not in IVRS table\n",
  1414. fw_bug, id);
  1415. ret = false;
  1416. } else if (devid == IOAPIC_SB_DEVID) {
  1417. has_sb_ioapic = true;
  1418. ret = true;
  1419. }
  1420. }
  1421. if (!has_sb_ioapic) {
  1422. /*
  1423. * We expect the SB IOAPIC to be listed in the IVRS
  1424. * table. The system timer is connected to the SB IOAPIC
  1425. * and if we don't have it in the list the system will
  1426. * panic at boot time. This situation usually happens
  1427. * when the BIOS is buggy and provides us the wrong
  1428. * device id for the IOAPIC in the system.
  1429. */
  1430. pr_err("%sAMD-Vi: No southbridge IOAPIC found\n", fw_bug);
  1431. }
  1432. if (!ret)
  1433. pr_err("AMD-Vi: Disabling interrupt remapping\n");
  1434. return ret;
  1435. }
  1436. static void __init free_dma_resources(void)
  1437. {
  1438. free_pages((unsigned long)amd_iommu_pd_alloc_bitmap,
  1439. get_order(MAX_DOMAIN_ID/8));
  1440. free_unity_maps();
  1441. }
  1442. /*
  1443. * This is the hardware init function for AMD IOMMU in the system.
  1444. * This function is called either from amd_iommu_init or from the interrupt
  1445. * remapping setup code.
  1446. *
  1447. * This function basically parses the ACPI table for AMD IOMMU (IVRS)
  1448. * three times:
  1449. *
  1450. * 1 pass) Find the highest PCI device id the driver has to handle.
  1451. * Upon this information the size of the data structures is
  1452. * determined that needs to be allocated.
  1453. *
  1454. * 2 pass) Initialize the data structures just allocated with the
  1455. * information in the ACPI table about available AMD IOMMUs
  1456. * in the system. It also maps the PCI devices in the
  1457. * system to specific IOMMUs
  1458. *
  1459. * 3 pass) After the basic data structures are allocated and
  1460. * initialized we update them with information about memory
  1461. * remapping requirements parsed out of the ACPI table in
  1462. * this last pass.
  1463. *
  1464. * After everything is set up the IOMMUs are enabled and the necessary
  1465. * hotplug and suspend notifiers are registered.
  1466. */
  1467. static int __init early_amd_iommu_init(void)
  1468. {
  1469. struct acpi_table_header *ivrs_base;
  1470. acpi_size ivrs_size;
  1471. acpi_status status;
  1472. int i, ret = 0;
  1473. if (!amd_iommu_detected)
  1474. return -ENODEV;
  1475. status = acpi_get_table_with_size("IVRS", 0, &ivrs_base, &ivrs_size);
  1476. if (status == AE_NOT_FOUND)
  1477. return -ENODEV;
  1478. else if (ACPI_FAILURE(status)) {
  1479. const char *err = acpi_format_exception(status);
  1480. pr_err("AMD-Vi: IVRS table error: %s\n", err);
  1481. return -EINVAL;
  1482. }
  1483. /*
  1484. * First parse ACPI tables to find the largest Bus/Dev/Func
  1485. * we need to handle. Upon this information the shared data
  1486. * structures for the IOMMUs in the system will be allocated
  1487. */
  1488. ret = find_last_devid_acpi(ivrs_base);
  1489. if (ret)
  1490. goto out;
  1491. dev_table_size = tbl_size(DEV_TABLE_ENTRY_SIZE);
  1492. alias_table_size = tbl_size(ALIAS_TABLE_ENTRY_SIZE);
  1493. rlookup_table_size = tbl_size(RLOOKUP_TABLE_ENTRY_SIZE);
  1494. /* Device table - directly used by all IOMMUs */
  1495. ret = -ENOMEM;
  1496. amd_iommu_dev_table = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
  1497. get_order(dev_table_size));
  1498. if (amd_iommu_dev_table == NULL)
  1499. goto out;
  1500. /*
  1501. * Alias table - map PCI Bus/Dev/Func to Bus/Dev/Func the
  1502. * IOMMU see for that device
  1503. */
  1504. amd_iommu_alias_table = (void *)__get_free_pages(GFP_KERNEL,
  1505. get_order(alias_table_size));
  1506. if (amd_iommu_alias_table == NULL)
  1507. goto out;
  1508. /* IOMMU rlookup table - find the IOMMU for a specific device */
  1509. amd_iommu_rlookup_table = (void *)__get_free_pages(
  1510. GFP_KERNEL | __GFP_ZERO,
  1511. get_order(rlookup_table_size));
  1512. if (amd_iommu_rlookup_table == NULL)
  1513. goto out;
  1514. amd_iommu_pd_alloc_bitmap = (void *)__get_free_pages(
  1515. GFP_KERNEL | __GFP_ZERO,
  1516. get_order(MAX_DOMAIN_ID/8));
  1517. if (amd_iommu_pd_alloc_bitmap == NULL)
  1518. goto out;
  1519. /*
  1520. * let all alias entries point to itself
  1521. */
  1522. for (i = 0; i <= amd_iommu_last_bdf; ++i)
  1523. amd_iommu_alias_table[i] = i;
  1524. /*
  1525. * never allocate domain 0 because its used as the non-allocated and
  1526. * error value placeholder
  1527. */
  1528. amd_iommu_pd_alloc_bitmap[0] = 1;
  1529. spin_lock_init(&amd_iommu_pd_lock);
  1530. /*
  1531. * now the data structures are allocated and basically initialized
  1532. * start the real acpi table scan
  1533. */
  1534. ret = init_iommu_all(ivrs_base);
  1535. if (ret)
  1536. goto out;
  1537. if (amd_iommu_irq_remap)
  1538. amd_iommu_irq_remap = check_ioapic_information();
  1539. if (amd_iommu_irq_remap) {
  1540. /*
  1541. * Interrupt remapping enabled, create kmem_cache for the
  1542. * remapping tables.
  1543. */
  1544. ret = -ENOMEM;
  1545. amd_iommu_irq_cache = kmem_cache_create("irq_remap_cache",
  1546. MAX_IRQS_PER_TABLE * sizeof(u32),
  1547. IRQ_TABLE_ALIGNMENT,
  1548. 0, NULL);
  1549. if (!amd_iommu_irq_cache)
  1550. goto out;
  1551. irq_lookup_table = (void *)__get_free_pages(
  1552. GFP_KERNEL | __GFP_ZERO,
  1553. get_order(rlookup_table_size));
  1554. if (!irq_lookup_table)
  1555. goto out;
  1556. }
  1557. ret = init_memory_definitions(ivrs_base);
  1558. if (ret)
  1559. goto out;
  1560. /* init the device table */
  1561. init_device_table();
  1562. out:
  1563. /* Don't leak any ACPI memory */
  1564. early_acpi_os_unmap_memory((char __iomem *)ivrs_base, ivrs_size);
  1565. ivrs_base = NULL;
  1566. return ret;
  1567. }
  1568. static int amd_iommu_enable_interrupts(void)
  1569. {
  1570. struct amd_iommu *iommu;
  1571. int ret = 0;
  1572. for_each_iommu(iommu) {
  1573. ret = iommu_init_msi(iommu);
  1574. if (ret)
  1575. goto out;
  1576. }
  1577. out:
  1578. return ret;
  1579. }
  1580. static bool detect_ivrs(void)
  1581. {
  1582. struct acpi_table_header *ivrs_base;
  1583. acpi_size ivrs_size;
  1584. acpi_status status;
  1585. status = acpi_get_table_with_size("IVRS", 0, &ivrs_base, &ivrs_size);
  1586. if (status == AE_NOT_FOUND)
  1587. return false;
  1588. else if (ACPI_FAILURE(status)) {
  1589. const char *err = acpi_format_exception(status);
  1590. pr_err("AMD-Vi: IVRS table error: %s\n", err);
  1591. return false;
  1592. }
  1593. early_acpi_os_unmap_memory((char __iomem *)ivrs_base, ivrs_size);
  1594. /* Make sure ACS will be enabled during PCI probe */
  1595. pci_request_acs();
  1596. return true;
  1597. }
  1598. /****************************************************************************
  1599. *
  1600. * AMD IOMMU Initialization State Machine
  1601. *
  1602. ****************************************************************************/
  1603. static int __init state_next(void)
  1604. {
  1605. int ret = 0;
  1606. switch (init_state) {
  1607. case IOMMU_START_STATE:
  1608. if (!detect_ivrs()) {
  1609. init_state = IOMMU_NOT_FOUND;
  1610. ret = -ENODEV;
  1611. } else {
  1612. init_state = IOMMU_IVRS_DETECTED;
  1613. }
  1614. break;
  1615. case IOMMU_IVRS_DETECTED:
  1616. ret = early_amd_iommu_init();
  1617. init_state = ret ? IOMMU_INIT_ERROR : IOMMU_ACPI_FINISHED;
  1618. break;
  1619. case IOMMU_ACPI_FINISHED:
  1620. early_enable_iommus();
  1621. register_syscore_ops(&amd_iommu_syscore_ops);
  1622. x86_platform.iommu_shutdown = disable_iommus;
  1623. init_state = IOMMU_ENABLED;
  1624. break;
  1625. case IOMMU_ENABLED:
  1626. ret = amd_iommu_init_pci();
  1627. init_state = ret ? IOMMU_INIT_ERROR : IOMMU_PCI_INIT;
  1628. enable_iommus_v2();
  1629. break;
  1630. case IOMMU_PCI_INIT:
  1631. ret = amd_iommu_enable_interrupts();
  1632. init_state = ret ? IOMMU_INIT_ERROR : IOMMU_INTERRUPTS_EN;
  1633. break;
  1634. case IOMMU_INTERRUPTS_EN:
  1635. ret = amd_iommu_init_dma_ops();
  1636. init_state = ret ? IOMMU_INIT_ERROR : IOMMU_DMA_OPS;
  1637. break;
  1638. case IOMMU_DMA_OPS:
  1639. init_state = IOMMU_INITIALIZED;
  1640. break;
  1641. case IOMMU_INITIALIZED:
  1642. /* Nothing to do */
  1643. break;
  1644. case IOMMU_NOT_FOUND:
  1645. case IOMMU_INIT_ERROR:
  1646. /* Error states => do nothing */
  1647. ret = -EINVAL;
  1648. break;
  1649. default:
  1650. /* Unknown state */
  1651. BUG();
  1652. }
  1653. return ret;
  1654. }
  1655. static int __init iommu_go_to_state(enum iommu_init_state state)
  1656. {
  1657. int ret = 0;
  1658. while (init_state != state) {
  1659. ret = state_next();
  1660. if (init_state == IOMMU_NOT_FOUND ||
  1661. init_state == IOMMU_INIT_ERROR)
  1662. break;
  1663. }
  1664. return ret;
  1665. }
  1666. #ifdef CONFIG_IRQ_REMAP
  1667. int __init amd_iommu_prepare(void)
  1668. {
  1669. int ret;
  1670. amd_iommu_irq_remap = true;
  1671. ret = iommu_go_to_state(IOMMU_ACPI_FINISHED);
  1672. if (ret)
  1673. return ret;
  1674. return amd_iommu_irq_remap ? 0 : -ENODEV;
  1675. }
  1676. int __init amd_iommu_enable(void)
  1677. {
  1678. int ret;
  1679. ret = iommu_go_to_state(IOMMU_ENABLED);
  1680. if (ret)
  1681. return ret;
  1682. irq_remapping_enabled = 1;
  1683. return 0;
  1684. }
  1685. void amd_iommu_disable(void)
  1686. {
  1687. amd_iommu_suspend();
  1688. }
  1689. int amd_iommu_reenable(int mode)
  1690. {
  1691. amd_iommu_resume();
  1692. return 0;
  1693. }
  1694. int __init amd_iommu_enable_faulting(void)
  1695. {
  1696. /* We enable MSI later when PCI is initialized */
  1697. return 0;
  1698. }
  1699. #endif
  1700. /*
  1701. * This is the core init function for AMD IOMMU hardware in the system.
  1702. * This function is called from the generic x86 DMA layer initialization
  1703. * code.
  1704. */
  1705. static int __init amd_iommu_init(void)
  1706. {
  1707. int ret;
  1708. ret = iommu_go_to_state(IOMMU_INITIALIZED);
  1709. if (ret) {
  1710. free_dma_resources();
  1711. if (!irq_remapping_enabled) {
  1712. disable_iommus();
  1713. free_on_init_error();
  1714. } else {
  1715. struct amd_iommu *iommu;
  1716. uninit_device_table_dma();
  1717. for_each_iommu(iommu)
  1718. iommu_flush_all_caches(iommu);
  1719. }
  1720. }
  1721. return ret;
  1722. }
  1723. /****************************************************************************
  1724. *
  1725. * Early detect code. This code runs at IOMMU detection time in the DMA
  1726. * layer. It just looks if there is an IVRS ACPI table to detect AMD
  1727. * IOMMUs
  1728. *
  1729. ****************************************************************************/
  1730. int __init amd_iommu_detect(void)
  1731. {
  1732. int ret;
  1733. if (no_iommu || (iommu_detected && !gart_iommu_aperture))
  1734. return -ENODEV;
  1735. if (amd_iommu_disabled)
  1736. return -ENODEV;
  1737. ret = iommu_go_to_state(IOMMU_IVRS_DETECTED);
  1738. if (ret)
  1739. return ret;
  1740. amd_iommu_detected = true;
  1741. iommu_detected = 1;
  1742. x86_init.iommu.iommu_init = amd_iommu_init;
  1743. return 1;
  1744. }
  1745. /****************************************************************************
  1746. *
  1747. * Parsing functions for the AMD IOMMU specific kernel command line
  1748. * options.
  1749. *
  1750. ****************************************************************************/
  1751. static int __init parse_amd_iommu_dump(char *str)
  1752. {
  1753. amd_iommu_dump = true;
  1754. return 1;
  1755. }
  1756. static int __init parse_amd_iommu_options(char *str)
  1757. {
  1758. for (; *str; ++str) {
  1759. if (strncmp(str, "fullflush", 9) == 0)
  1760. amd_iommu_unmap_flush = true;
  1761. if (strncmp(str, "off", 3) == 0)
  1762. amd_iommu_disabled = true;
  1763. if (strncmp(str, "force_isolation", 15) == 0)
  1764. amd_iommu_force_isolation = true;
  1765. }
  1766. return 1;
  1767. }
  1768. static int __init parse_ivrs_ioapic(char *str)
  1769. {
  1770. unsigned int bus, dev, fn;
  1771. int ret, id, i;
  1772. u16 devid;
  1773. ret = sscanf(str, "[%d]=%x:%x.%x", &id, &bus, &dev, &fn);
  1774. if (ret != 4) {
  1775. pr_err("AMD-Vi: Invalid command line: ivrs_ioapic%s\n", str);
  1776. return 1;
  1777. }
  1778. if (early_ioapic_map_size == EARLY_MAP_SIZE) {
  1779. pr_err("AMD-Vi: Early IOAPIC map overflow - ignoring ivrs_ioapic%s\n",
  1780. str);
  1781. return 1;
  1782. }
  1783. devid = ((bus & 0xff) << 8) | ((dev & 0x1f) << 3) | (fn & 0x7);
  1784. cmdline_maps = true;
  1785. i = early_ioapic_map_size++;
  1786. early_ioapic_map[i].id = id;
  1787. early_ioapic_map[i].devid = devid;
  1788. early_ioapic_map[i].cmd_line = true;
  1789. return 1;
  1790. }
  1791. static int __init parse_ivrs_hpet(char *str)
  1792. {
  1793. unsigned int bus, dev, fn;
  1794. int ret, id, i;
  1795. u16 devid;
  1796. ret = sscanf(str, "[%d]=%x:%x.%x", &id, &bus, &dev, &fn);
  1797. if (ret != 4) {
  1798. pr_err("AMD-Vi: Invalid command line: ivrs_hpet%s\n", str);
  1799. return 1;
  1800. }
  1801. if (early_hpet_map_size == EARLY_MAP_SIZE) {
  1802. pr_err("AMD-Vi: Early HPET map overflow - ignoring ivrs_hpet%s\n",
  1803. str);
  1804. return 1;
  1805. }
  1806. devid = ((bus & 0xff) << 8) | ((dev & 0x1f) << 3) | (fn & 0x7);
  1807. cmdline_maps = true;
  1808. i = early_hpet_map_size++;
  1809. early_hpet_map[i].id = id;
  1810. early_hpet_map[i].devid = devid;
  1811. early_hpet_map[i].cmd_line = true;
  1812. return 1;
  1813. }
  1814. __setup("amd_iommu_dump", parse_amd_iommu_dump);
  1815. __setup("amd_iommu=", parse_amd_iommu_options);
  1816. __setup("ivrs_ioapic", parse_ivrs_ioapic);
  1817. __setup("ivrs_hpet", parse_ivrs_hpet);
  1818. IOMMU_INIT_FINISH(amd_iommu_detect,
  1819. gart_iommu_hole_init,
  1820. NULL,
  1821. NULL);
  1822. bool amd_iommu_v2_supported(void)
  1823. {
  1824. return amd_iommu_v2_present;
  1825. }
  1826. EXPORT_SYMBOL(amd_iommu_v2_supported);
  1827. /****************************************************************************
  1828. *
  1829. * IOMMU EFR Performance Counter support functionality. This code allows
  1830. * access to the IOMMU PC functionality.
  1831. *
  1832. ****************************************************************************/
  1833. u8 amd_iommu_pc_get_max_banks(u16 devid)
  1834. {
  1835. struct amd_iommu *iommu;
  1836. u8 ret = 0;
  1837. /* locate the iommu governing the devid */
  1838. iommu = amd_iommu_rlookup_table[devid];
  1839. if (iommu)
  1840. ret = iommu->max_banks;
  1841. return ret;
  1842. }
  1843. EXPORT_SYMBOL(amd_iommu_pc_get_max_banks);
  1844. bool amd_iommu_pc_supported(void)
  1845. {
  1846. return amd_iommu_pc_present;
  1847. }
  1848. EXPORT_SYMBOL(amd_iommu_pc_supported);
  1849. u8 amd_iommu_pc_get_max_counters(u16 devid)
  1850. {
  1851. struct amd_iommu *iommu;
  1852. u8 ret = 0;
  1853. /* locate the iommu governing the devid */
  1854. iommu = amd_iommu_rlookup_table[devid];
  1855. if (iommu)
  1856. ret = iommu->max_counters;
  1857. return ret;
  1858. }
  1859. EXPORT_SYMBOL(amd_iommu_pc_get_max_counters);
  1860. int amd_iommu_pc_get_set_reg_val(u16 devid, u8 bank, u8 cntr, u8 fxn,
  1861. u64 *value, bool is_write)
  1862. {
  1863. struct amd_iommu *iommu;
  1864. u32 offset;
  1865. u32 max_offset_lim;
  1866. /* Make sure the IOMMU PC resource is available */
  1867. if (!amd_iommu_pc_present)
  1868. return -ENODEV;
  1869. /* Locate the iommu associated with the device ID */
  1870. iommu = amd_iommu_rlookup_table[devid];
  1871. /* Check for valid iommu and pc register indexing */
  1872. if (WARN_ON((iommu == NULL) || (fxn > 0x28) || (fxn & 7)))
  1873. return -ENODEV;
  1874. offset = (u32)(((0x40|bank) << 12) | (cntr << 8) | fxn);
  1875. /* Limit the offset to the hw defined mmio region aperture */
  1876. max_offset_lim = (u32)(((0x40|iommu->max_banks) << 12) |
  1877. (iommu->max_counters << 8) | 0x28);
  1878. if ((offset < MMIO_CNTR_REG_OFFSET) ||
  1879. (offset > max_offset_lim))
  1880. return -EINVAL;
  1881. if (is_write) {
  1882. writel((u32)*value, iommu->mmio_base + offset);
  1883. writel((*value >> 32), iommu->mmio_base + offset + 4);
  1884. } else {
  1885. *value = readl(iommu->mmio_base + offset + 4);
  1886. *value <<= 32;
  1887. *value = readl(iommu->mmio_base + offset);
  1888. }
  1889. return 0;
  1890. }
  1891. EXPORT_SYMBOL(amd_iommu_pc_get_set_reg_val);