amd_iommu_init.c 58 KB

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