amd_iommu_init.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718
  1. /*
  2. * Copyright (C) 2007-2010 Advanced Micro Devices, Inc.
  3. * Author: Joerg Roedel <joerg.roedel@amd.com>
  4. * Leo Duran <leo.duran@amd.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License version 2 as published
  8. * by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include <linux/pci.h>
  20. #include <linux/acpi.h>
  21. #include <linux/list.h>
  22. #include <linux/slab.h>
  23. #include <linux/syscore_ops.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/msi.h>
  26. #include <linux/amd-iommu.h>
  27. #include <linux/export.h>
  28. #include <asm/pci-direct.h>
  29. #include <asm/iommu.h>
  30. #include <asm/gart.h>
  31. #include <asm/x86_init.h>
  32. #include <asm/iommu_table.h>
  33. #include "amd_iommu_proto.h"
  34. #include "amd_iommu_types.h"
  35. /*
  36. * definitions for the ACPI scanning code
  37. */
  38. #define IVRS_HEADER_LENGTH 48
  39. #define ACPI_IVHD_TYPE 0x10
  40. #define ACPI_IVMD_TYPE_ALL 0x20
  41. #define ACPI_IVMD_TYPE 0x21
  42. #define ACPI_IVMD_TYPE_RANGE 0x22
  43. #define IVHD_DEV_ALL 0x01
  44. #define IVHD_DEV_SELECT 0x02
  45. #define IVHD_DEV_SELECT_RANGE_START 0x03
  46. #define IVHD_DEV_RANGE_END 0x04
  47. #define IVHD_DEV_ALIAS 0x42
  48. #define IVHD_DEV_ALIAS_RANGE 0x43
  49. #define IVHD_DEV_EXT_SELECT 0x46
  50. #define IVHD_DEV_EXT_SELECT_RANGE 0x47
  51. #define IVHD_FLAG_HT_TUN_EN_MASK 0x01
  52. #define IVHD_FLAG_PASSPW_EN_MASK 0x02
  53. #define IVHD_FLAG_RESPASSPW_EN_MASK 0x04
  54. #define IVHD_FLAG_ISOC_EN_MASK 0x08
  55. #define IVMD_FLAG_EXCL_RANGE 0x08
  56. #define IVMD_FLAG_UNITY_MAP 0x01
  57. #define ACPI_DEVFLAG_INITPASS 0x01
  58. #define ACPI_DEVFLAG_EXTINT 0x02
  59. #define ACPI_DEVFLAG_NMI 0x04
  60. #define ACPI_DEVFLAG_SYSMGT1 0x10
  61. #define ACPI_DEVFLAG_SYSMGT2 0x20
  62. #define ACPI_DEVFLAG_LINT0 0x40
  63. #define ACPI_DEVFLAG_LINT1 0x80
  64. #define ACPI_DEVFLAG_ATSDIS 0x10000000
  65. /*
  66. * ACPI table definitions
  67. *
  68. * These data structures are laid over the table to parse the important values
  69. * out of it.
  70. */
  71. /*
  72. * structure describing one IOMMU in the ACPI table. Typically followed by one
  73. * or more ivhd_entrys.
  74. */
  75. struct ivhd_header {
  76. u8 type;
  77. u8 flags;
  78. u16 length;
  79. u16 devid;
  80. u16 cap_ptr;
  81. u64 mmio_phys;
  82. u16 pci_seg;
  83. u16 info;
  84. u32 reserved;
  85. } __attribute__((packed));
  86. /*
  87. * A device entry describing which devices a specific IOMMU translates and
  88. * which requestor ids they use.
  89. */
  90. struct ivhd_entry {
  91. u8 type;
  92. u16 devid;
  93. u8 flags;
  94. u32 ext;
  95. } __attribute__((packed));
  96. /*
  97. * An AMD IOMMU memory definition structure. It defines things like exclusion
  98. * ranges for devices and regions that should be unity mapped.
  99. */
  100. struct ivmd_header {
  101. u8 type;
  102. u8 flags;
  103. u16 length;
  104. u16 devid;
  105. u16 aux;
  106. u64 resv;
  107. u64 range_start;
  108. u64 range_length;
  109. } __attribute__((packed));
  110. bool amd_iommu_dump;
  111. static int __initdata amd_iommu_detected;
  112. static bool __initdata amd_iommu_disabled;
  113. u16 amd_iommu_last_bdf; /* largest PCI device id we have
  114. to handle */
  115. LIST_HEAD(amd_iommu_unity_map); /* a list of required unity mappings
  116. we find in ACPI */
  117. bool amd_iommu_unmap_flush; /* if true, flush on every unmap */
  118. LIST_HEAD(amd_iommu_list); /* list of all AMD IOMMUs in the
  119. system */
  120. /* Array to assign indices to IOMMUs*/
  121. struct amd_iommu *amd_iommus[MAX_IOMMUS];
  122. int amd_iommus_present;
  123. /* IOMMUs have a non-present cache? */
  124. bool amd_iommu_np_cache __read_mostly;
  125. bool amd_iommu_iotlb_sup __read_mostly = true;
  126. u32 amd_iommu_max_pasids __read_mostly = ~0;
  127. bool amd_iommu_v2_present __read_mostly;
  128. bool amd_iommu_force_isolation __read_mostly;
  129. /*
  130. * The ACPI table parsing functions set this variable on an error
  131. */
  132. static int __initdata amd_iommu_init_err;
  133. /*
  134. * List of protection domains - used during resume
  135. */
  136. LIST_HEAD(amd_iommu_pd_list);
  137. spinlock_t amd_iommu_pd_lock;
  138. /*
  139. * Pointer to the device table which is shared by all AMD IOMMUs
  140. * it is indexed by the PCI device id or the HT unit id and contains
  141. * information about the domain the device belongs to as well as the
  142. * page table root pointer.
  143. */
  144. struct dev_table_entry *amd_iommu_dev_table;
  145. /*
  146. * The alias table is a driver specific data structure which contains the
  147. * mappings of the PCI device ids to the actual requestor ids on the IOMMU.
  148. * More than one device can share the same requestor id.
  149. */
  150. u16 *amd_iommu_alias_table;
  151. /*
  152. * The rlookup table is used to find the IOMMU which is responsible
  153. * for a specific device. It is also indexed by the PCI device id.
  154. */
  155. struct amd_iommu **amd_iommu_rlookup_table;
  156. /*
  157. * AMD IOMMU allows up to 2^16 differend protection domains. This is a bitmap
  158. * to know which ones are already in use.
  159. */
  160. unsigned long *amd_iommu_pd_alloc_bitmap;
  161. static u32 dev_table_size; /* size of the device table */
  162. static u32 alias_table_size; /* size of the alias table */
  163. static u32 rlookup_table_size; /* size if the rlookup table */
  164. /*
  165. * This function flushes all internal caches of
  166. * the IOMMU used by this driver.
  167. */
  168. extern void iommu_flush_all_caches(struct amd_iommu *iommu);
  169. static inline void update_last_devid(u16 devid)
  170. {
  171. if (devid > amd_iommu_last_bdf)
  172. amd_iommu_last_bdf = devid;
  173. }
  174. static inline unsigned long tbl_size(int entry_size)
  175. {
  176. unsigned shift = PAGE_SHIFT +
  177. get_order(((int)amd_iommu_last_bdf + 1) * entry_size);
  178. return 1UL << shift;
  179. }
  180. /* Access to l1 and l2 indexed register spaces */
  181. static u32 iommu_read_l1(struct amd_iommu *iommu, u16 l1, u8 address)
  182. {
  183. u32 val;
  184. pci_write_config_dword(iommu->dev, 0xf8, (address | l1 << 16));
  185. pci_read_config_dword(iommu->dev, 0xfc, &val);
  186. return val;
  187. }
  188. static void iommu_write_l1(struct amd_iommu *iommu, u16 l1, u8 address, u32 val)
  189. {
  190. pci_write_config_dword(iommu->dev, 0xf8, (address | l1 << 16 | 1 << 31));
  191. pci_write_config_dword(iommu->dev, 0xfc, val);
  192. pci_write_config_dword(iommu->dev, 0xf8, (address | l1 << 16));
  193. }
  194. static u32 iommu_read_l2(struct amd_iommu *iommu, u8 address)
  195. {
  196. u32 val;
  197. pci_write_config_dword(iommu->dev, 0xf0, address);
  198. pci_read_config_dword(iommu->dev, 0xf4, &val);
  199. return val;
  200. }
  201. static void iommu_write_l2(struct amd_iommu *iommu, u8 address, u32 val)
  202. {
  203. pci_write_config_dword(iommu->dev, 0xf0, (address | 1 << 8));
  204. pci_write_config_dword(iommu->dev, 0xf4, val);
  205. }
  206. /****************************************************************************
  207. *
  208. * AMD IOMMU MMIO register space handling functions
  209. *
  210. * These functions are used to program the IOMMU device registers in
  211. * MMIO space required for that driver.
  212. *
  213. ****************************************************************************/
  214. /*
  215. * This function set the exclusion range in the IOMMU. DMA accesses to the
  216. * exclusion range are passed through untranslated
  217. */
  218. static void iommu_set_exclusion_range(struct amd_iommu *iommu)
  219. {
  220. u64 start = iommu->exclusion_start & PAGE_MASK;
  221. u64 limit = (start + iommu->exclusion_length) & PAGE_MASK;
  222. u64 entry;
  223. if (!iommu->exclusion_start)
  224. return;
  225. entry = start | MMIO_EXCL_ENABLE_MASK;
  226. memcpy_toio(iommu->mmio_base + MMIO_EXCL_BASE_OFFSET,
  227. &entry, sizeof(entry));
  228. entry = limit;
  229. memcpy_toio(iommu->mmio_base + MMIO_EXCL_LIMIT_OFFSET,
  230. &entry, sizeof(entry));
  231. }
  232. /* Programs the physical address of the device table into the IOMMU hardware */
  233. static void __init iommu_set_device_table(struct amd_iommu *iommu)
  234. {
  235. u64 entry;
  236. BUG_ON(iommu->mmio_base == NULL);
  237. entry = virt_to_phys(amd_iommu_dev_table);
  238. entry |= (dev_table_size >> 12) - 1;
  239. memcpy_toio(iommu->mmio_base + MMIO_DEV_TABLE_OFFSET,
  240. &entry, sizeof(entry));
  241. }
  242. /* Generic functions to enable/disable certain features of the IOMMU. */
  243. static void iommu_feature_enable(struct amd_iommu *iommu, u8 bit)
  244. {
  245. u32 ctrl;
  246. ctrl = readl(iommu->mmio_base + MMIO_CONTROL_OFFSET);
  247. ctrl |= (1 << bit);
  248. writel(ctrl, iommu->mmio_base + MMIO_CONTROL_OFFSET);
  249. }
  250. static void iommu_feature_disable(struct amd_iommu *iommu, u8 bit)
  251. {
  252. u32 ctrl;
  253. ctrl = readl(iommu->mmio_base + MMIO_CONTROL_OFFSET);
  254. ctrl &= ~(1 << bit);
  255. writel(ctrl, iommu->mmio_base + MMIO_CONTROL_OFFSET);
  256. }
  257. static void iommu_set_inv_tlb_timeout(struct amd_iommu *iommu, int timeout)
  258. {
  259. u32 ctrl;
  260. ctrl = readl(iommu->mmio_base + MMIO_CONTROL_OFFSET);
  261. ctrl &= ~CTRL_INV_TO_MASK;
  262. ctrl |= (timeout << CONTROL_INV_TIMEOUT) & CTRL_INV_TO_MASK;
  263. writel(ctrl, iommu->mmio_base + MMIO_CONTROL_OFFSET);
  264. }
  265. /* Function to enable the hardware */
  266. static void iommu_enable(struct amd_iommu *iommu)
  267. {
  268. static const char * const feat_str[] = {
  269. "PreF", "PPR", "X2APIC", "NX", "GT", "[5]",
  270. "IA", "GA", "HE", "PC", NULL
  271. };
  272. int i;
  273. printk(KERN_INFO "AMD-Vi: Enabling IOMMU at %s cap 0x%hx",
  274. dev_name(&iommu->dev->dev), iommu->cap_ptr);
  275. if (iommu->cap & (1 << IOMMU_CAP_EFR)) {
  276. printk(KERN_CONT " extended features: ");
  277. for (i = 0; feat_str[i]; ++i)
  278. if (iommu_feature(iommu, (1ULL << i)))
  279. printk(KERN_CONT " %s", feat_str[i]);
  280. }
  281. printk(KERN_CONT "\n");
  282. iommu_feature_enable(iommu, CONTROL_IOMMU_EN);
  283. }
  284. static void iommu_disable(struct amd_iommu *iommu)
  285. {
  286. /* Disable command buffer */
  287. iommu_feature_disable(iommu, CONTROL_CMDBUF_EN);
  288. /* Disable event logging and event interrupts */
  289. iommu_feature_disable(iommu, CONTROL_EVT_INT_EN);
  290. iommu_feature_disable(iommu, CONTROL_EVT_LOG_EN);
  291. /* Disable IOMMU hardware itself */
  292. iommu_feature_disable(iommu, CONTROL_IOMMU_EN);
  293. }
  294. /*
  295. * mapping and unmapping functions for the IOMMU MMIO space. Each AMD IOMMU in
  296. * the system has one.
  297. */
  298. static u8 * __init iommu_map_mmio_space(u64 address)
  299. {
  300. if (!request_mem_region(address, MMIO_REGION_LENGTH, "amd_iommu")) {
  301. pr_err("AMD-Vi: Can not reserve memory region %llx for mmio\n",
  302. address);
  303. pr_err("AMD-Vi: This is a BIOS bug. Please contact your hardware vendor\n");
  304. return NULL;
  305. }
  306. return ioremap_nocache(address, MMIO_REGION_LENGTH);
  307. }
  308. static void __init iommu_unmap_mmio_space(struct amd_iommu *iommu)
  309. {
  310. if (iommu->mmio_base)
  311. iounmap(iommu->mmio_base);
  312. release_mem_region(iommu->mmio_phys, MMIO_REGION_LENGTH);
  313. }
  314. /****************************************************************************
  315. *
  316. * The functions below belong to the first pass of AMD IOMMU ACPI table
  317. * parsing. In this pass we try to find out the highest device id this
  318. * code has to handle. Upon this information the size of the shared data
  319. * structures is determined later.
  320. *
  321. ****************************************************************************/
  322. /*
  323. * This function calculates the length of a given IVHD entry
  324. */
  325. static inline int ivhd_entry_length(u8 *ivhd)
  326. {
  327. return 0x04 << (*ivhd >> 6);
  328. }
  329. /*
  330. * This function reads the last device id the IOMMU has to handle from the PCI
  331. * capability header for this IOMMU
  332. */
  333. static int __init find_last_devid_on_pci(int bus, int dev, int fn, int cap_ptr)
  334. {
  335. u32 cap;
  336. cap = read_pci_config(bus, dev, fn, cap_ptr+MMIO_RANGE_OFFSET);
  337. update_last_devid(calc_devid(MMIO_GET_BUS(cap), MMIO_GET_LD(cap)));
  338. return 0;
  339. }
  340. /*
  341. * After reading the highest device id from the IOMMU PCI capability header
  342. * this function looks if there is a higher device id defined in the ACPI table
  343. */
  344. static int __init find_last_devid_from_ivhd(struct ivhd_header *h)
  345. {
  346. u8 *p = (void *)h, *end = (void *)h;
  347. struct ivhd_entry *dev;
  348. p += sizeof(*h);
  349. end += h->length;
  350. find_last_devid_on_pci(PCI_BUS(h->devid),
  351. PCI_SLOT(h->devid),
  352. PCI_FUNC(h->devid),
  353. h->cap_ptr);
  354. while (p < end) {
  355. dev = (struct ivhd_entry *)p;
  356. switch (dev->type) {
  357. case IVHD_DEV_SELECT:
  358. case IVHD_DEV_RANGE_END:
  359. case IVHD_DEV_ALIAS:
  360. case IVHD_DEV_EXT_SELECT:
  361. /* all the above subfield types refer to device ids */
  362. update_last_devid(dev->devid);
  363. break;
  364. default:
  365. break;
  366. }
  367. p += ivhd_entry_length(p);
  368. }
  369. WARN_ON(p != end);
  370. return 0;
  371. }
  372. /*
  373. * Iterate over all IVHD entries in the ACPI table and find the highest device
  374. * id which we need to handle. This is the first of three functions which parse
  375. * the ACPI table. So we check the checksum here.
  376. */
  377. static int __init find_last_devid_acpi(struct acpi_table_header *table)
  378. {
  379. int i;
  380. u8 checksum = 0, *p = (u8 *)table, *end = (u8 *)table;
  381. struct ivhd_header *h;
  382. /*
  383. * Validate checksum here so we don't need to do it when
  384. * we actually parse the table
  385. */
  386. for (i = 0; i < table->length; ++i)
  387. checksum += p[i];
  388. if (checksum != 0) {
  389. /* ACPI table corrupt */
  390. amd_iommu_init_err = -ENODEV;
  391. return 0;
  392. }
  393. p += IVRS_HEADER_LENGTH;
  394. end += table->length;
  395. while (p < end) {
  396. h = (struct ivhd_header *)p;
  397. switch (h->type) {
  398. case ACPI_IVHD_TYPE:
  399. find_last_devid_from_ivhd(h);
  400. break;
  401. default:
  402. break;
  403. }
  404. p += h->length;
  405. }
  406. WARN_ON(p != end);
  407. return 0;
  408. }
  409. /****************************************************************************
  410. *
  411. * The following functions belong the the code path which parses the ACPI table
  412. * the second time. In this ACPI parsing iteration we allocate IOMMU specific
  413. * data structures, initialize the device/alias/rlookup table and also
  414. * basically initialize the hardware.
  415. *
  416. ****************************************************************************/
  417. /*
  418. * Allocates the command buffer. This buffer is per AMD IOMMU. We can
  419. * write commands to that buffer later and the IOMMU will execute them
  420. * asynchronously
  421. */
  422. static u8 * __init alloc_command_buffer(struct amd_iommu *iommu)
  423. {
  424. u8 *cmd_buf = (u8 *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
  425. get_order(CMD_BUFFER_SIZE));
  426. if (cmd_buf == NULL)
  427. return NULL;
  428. iommu->cmd_buf_size = CMD_BUFFER_SIZE | CMD_BUFFER_UNINITIALIZED;
  429. return cmd_buf;
  430. }
  431. /*
  432. * This function resets the command buffer if the IOMMU stopped fetching
  433. * commands from it.
  434. */
  435. void amd_iommu_reset_cmd_buffer(struct amd_iommu *iommu)
  436. {
  437. iommu_feature_disable(iommu, CONTROL_CMDBUF_EN);
  438. writel(0x00, iommu->mmio_base + MMIO_CMD_HEAD_OFFSET);
  439. writel(0x00, iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
  440. iommu_feature_enable(iommu, CONTROL_CMDBUF_EN);
  441. }
  442. /*
  443. * This function writes the command buffer address to the hardware and
  444. * enables it.
  445. */
  446. static void iommu_enable_command_buffer(struct amd_iommu *iommu)
  447. {
  448. u64 entry;
  449. BUG_ON(iommu->cmd_buf == NULL);
  450. entry = (u64)virt_to_phys(iommu->cmd_buf);
  451. entry |= MMIO_CMD_SIZE_512;
  452. memcpy_toio(iommu->mmio_base + MMIO_CMD_BUF_OFFSET,
  453. &entry, sizeof(entry));
  454. amd_iommu_reset_cmd_buffer(iommu);
  455. iommu->cmd_buf_size &= ~(CMD_BUFFER_UNINITIALIZED);
  456. }
  457. static void __init free_command_buffer(struct amd_iommu *iommu)
  458. {
  459. free_pages((unsigned long)iommu->cmd_buf,
  460. get_order(iommu->cmd_buf_size & ~(CMD_BUFFER_UNINITIALIZED)));
  461. }
  462. /* allocates the memory where the IOMMU will log its events to */
  463. static u8 * __init alloc_event_buffer(struct amd_iommu *iommu)
  464. {
  465. iommu->evt_buf = (u8 *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
  466. get_order(EVT_BUFFER_SIZE));
  467. if (iommu->evt_buf == NULL)
  468. return NULL;
  469. iommu->evt_buf_size = EVT_BUFFER_SIZE;
  470. return iommu->evt_buf;
  471. }
  472. static void iommu_enable_event_buffer(struct amd_iommu *iommu)
  473. {
  474. u64 entry;
  475. BUG_ON(iommu->evt_buf == NULL);
  476. entry = (u64)virt_to_phys(iommu->evt_buf) | EVT_LEN_MASK;
  477. memcpy_toio(iommu->mmio_base + MMIO_EVT_BUF_OFFSET,
  478. &entry, sizeof(entry));
  479. /* set head and tail to zero manually */
  480. writel(0x00, iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
  481. writel(0x00, iommu->mmio_base + MMIO_EVT_TAIL_OFFSET);
  482. iommu_feature_enable(iommu, CONTROL_EVT_LOG_EN);
  483. }
  484. static void __init free_event_buffer(struct amd_iommu *iommu)
  485. {
  486. free_pages((unsigned long)iommu->evt_buf, get_order(EVT_BUFFER_SIZE));
  487. }
  488. /* allocates the memory where the IOMMU will log its events to */
  489. static u8 * __init alloc_ppr_log(struct amd_iommu *iommu)
  490. {
  491. iommu->ppr_log = (u8 *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
  492. get_order(PPR_LOG_SIZE));
  493. if (iommu->ppr_log == NULL)
  494. return NULL;
  495. return iommu->ppr_log;
  496. }
  497. static void iommu_enable_ppr_log(struct amd_iommu *iommu)
  498. {
  499. u64 entry;
  500. if (iommu->ppr_log == NULL)
  501. return;
  502. entry = (u64)virt_to_phys(iommu->ppr_log) | PPR_LOG_SIZE_512;
  503. memcpy_toio(iommu->mmio_base + MMIO_PPR_LOG_OFFSET,
  504. &entry, sizeof(entry));
  505. /* set head and tail to zero manually */
  506. writel(0x00, iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
  507. writel(0x00, iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
  508. iommu_feature_enable(iommu, CONTROL_PPFLOG_EN);
  509. iommu_feature_enable(iommu, CONTROL_PPR_EN);
  510. }
  511. static void __init free_ppr_log(struct amd_iommu *iommu)
  512. {
  513. if (iommu->ppr_log == NULL)
  514. return;
  515. free_pages((unsigned long)iommu->ppr_log, get_order(PPR_LOG_SIZE));
  516. }
  517. static void iommu_enable_gt(struct amd_iommu *iommu)
  518. {
  519. if (!iommu_feature(iommu, FEATURE_GT))
  520. return;
  521. iommu_feature_enable(iommu, CONTROL_GT_EN);
  522. }
  523. /* sets a specific bit in the device table entry. */
  524. static void set_dev_entry_bit(u16 devid, u8 bit)
  525. {
  526. int i = (bit >> 6) & 0x03;
  527. int _bit = bit & 0x3f;
  528. amd_iommu_dev_table[devid].data[i] |= (1UL << _bit);
  529. }
  530. static int get_dev_entry_bit(u16 devid, u8 bit)
  531. {
  532. int i = (bit >> 6) & 0x03;
  533. int _bit = bit & 0x3f;
  534. return (amd_iommu_dev_table[devid].data[i] & (1UL << _bit)) >> _bit;
  535. }
  536. void amd_iommu_apply_erratum_63(u16 devid)
  537. {
  538. int sysmgt;
  539. sysmgt = get_dev_entry_bit(devid, DEV_ENTRY_SYSMGT1) |
  540. (get_dev_entry_bit(devid, DEV_ENTRY_SYSMGT2) << 1);
  541. if (sysmgt == 0x01)
  542. set_dev_entry_bit(devid, DEV_ENTRY_IW);
  543. }
  544. /* Writes the specific IOMMU for a device into the rlookup table */
  545. static void __init set_iommu_for_device(struct amd_iommu *iommu, u16 devid)
  546. {
  547. amd_iommu_rlookup_table[devid] = iommu;
  548. }
  549. /*
  550. * This function takes the device specific flags read from the ACPI
  551. * table and sets up the device table entry with that information
  552. */
  553. static void __init set_dev_entry_from_acpi(struct amd_iommu *iommu,
  554. u16 devid, u32 flags, u32 ext_flags)
  555. {
  556. if (flags & ACPI_DEVFLAG_INITPASS)
  557. set_dev_entry_bit(devid, DEV_ENTRY_INIT_PASS);
  558. if (flags & ACPI_DEVFLAG_EXTINT)
  559. set_dev_entry_bit(devid, DEV_ENTRY_EINT_PASS);
  560. if (flags & ACPI_DEVFLAG_NMI)
  561. set_dev_entry_bit(devid, DEV_ENTRY_NMI_PASS);
  562. if (flags & ACPI_DEVFLAG_SYSMGT1)
  563. set_dev_entry_bit(devid, DEV_ENTRY_SYSMGT1);
  564. if (flags & ACPI_DEVFLAG_SYSMGT2)
  565. set_dev_entry_bit(devid, DEV_ENTRY_SYSMGT2);
  566. if (flags & ACPI_DEVFLAG_LINT0)
  567. set_dev_entry_bit(devid, DEV_ENTRY_LINT0_PASS);
  568. if (flags & ACPI_DEVFLAG_LINT1)
  569. set_dev_entry_bit(devid, DEV_ENTRY_LINT1_PASS);
  570. amd_iommu_apply_erratum_63(devid);
  571. set_iommu_for_device(iommu, devid);
  572. }
  573. /*
  574. * Reads the device exclusion range from ACPI and initialize IOMMU with
  575. * it
  576. */
  577. static void __init set_device_exclusion_range(u16 devid, struct ivmd_header *m)
  578. {
  579. struct amd_iommu *iommu = amd_iommu_rlookup_table[devid];
  580. if (!(m->flags & IVMD_FLAG_EXCL_RANGE))
  581. return;
  582. if (iommu) {
  583. /*
  584. * We only can configure exclusion ranges per IOMMU, not
  585. * per device. But we can enable the exclusion range per
  586. * device. This is done here
  587. */
  588. set_dev_entry_bit(m->devid, DEV_ENTRY_EX);
  589. iommu->exclusion_start = m->range_start;
  590. iommu->exclusion_length = m->range_length;
  591. }
  592. }
  593. /*
  594. * This function reads some important data from the IOMMU PCI space and
  595. * initializes the driver data structure with it. It reads the hardware
  596. * capabilities and the first/last device entries
  597. */
  598. static void __init init_iommu_from_pci(struct amd_iommu *iommu)
  599. {
  600. int cap_ptr = iommu->cap_ptr;
  601. u32 range, misc, low, high;
  602. int i, j;
  603. pci_read_config_dword(iommu->dev, cap_ptr + MMIO_CAP_HDR_OFFSET,
  604. &iommu->cap);
  605. pci_read_config_dword(iommu->dev, cap_ptr + MMIO_RANGE_OFFSET,
  606. &range);
  607. pci_read_config_dword(iommu->dev, cap_ptr + MMIO_MISC_OFFSET,
  608. &misc);
  609. iommu->first_device = calc_devid(MMIO_GET_BUS(range),
  610. MMIO_GET_FD(range));
  611. iommu->last_device = calc_devid(MMIO_GET_BUS(range),
  612. MMIO_GET_LD(range));
  613. iommu->evt_msi_num = MMIO_MSI_NUM(misc);
  614. if (!(iommu->cap & (1 << IOMMU_CAP_IOTLB)))
  615. amd_iommu_iotlb_sup = false;
  616. /* read extended feature bits */
  617. low = readl(iommu->mmio_base + MMIO_EXT_FEATURES);
  618. high = readl(iommu->mmio_base + MMIO_EXT_FEATURES + 4);
  619. iommu->features = ((u64)high << 32) | low;
  620. if (iommu_feature(iommu, FEATURE_GT)) {
  621. int glxval;
  622. u32 pasids;
  623. u64 shift;
  624. shift = iommu->features & FEATURE_PASID_MASK;
  625. shift >>= FEATURE_PASID_SHIFT;
  626. pasids = (1 << shift);
  627. amd_iommu_max_pasids = min(amd_iommu_max_pasids, pasids);
  628. glxval = iommu->features & FEATURE_GLXVAL_MASK;
  629. glxval >>= FEATURE_GLXVAL_SHIFT;
  630. if (amd_iommu_max_glx_val == -1)
  631. amd_iommu_max_glx_val = glxval;
  632. else
  633. amd_iommu_max_glx_val = min(amd_iommu_max_glx_val, glxval);
  634. }
  635. if (iommu_feature(iommu, FEATURE_GT) &&
  636. iommu_feature(iommu, FEATURE_PPR)) {
  637. iommu->is_iommu_v2 = true;
  638. amd_iommu_v2_present = true;
  639. }
  640. if (!is_rd890_iommu(iommu->dev))
  641. return;
  642. /*
  643. * Some rd890 systems may not be fully reconfigured by the BIOS, so
  644. * it's necessary for us to store this information so it can be
  645. * reprogrammed on resume
  646. */
  647. pci_read_config_dword(iommu->dev, iommu->cap_ptr + 4,
  648. &iommu->stored_addr_lo);
  649. pci_read_config_dword(iommu->dev, iommu->cap_ptr + 8,
  650. &iommu->stored_addr_hi);
  651. /* Low bit locks writes to configuration space */
  652. iommu->stored_addr_lo &= ~1;
  653. for (i = 0; i < 6; i++)
  654. for (j = 0; j < 0x12; j++)
  655. iommu->stored_l1[i][j] = iommu_read_l1(iommu, i, j);
  656. for (i = 0; i < 0x83; i++)
  657. iommu->stored_l2[i] = iommu_read_l2(iommu, i);
  658. }
  659. /*
  660. * Takes a pointer to an AMD IOMMU entry in the ACPI table and
  661. * initializes the hardware and our data structures with it.
  662. */
  663. static void __init init_iommu_from_acpi(struct amd_iommu *iommu,
  664. struct ivhd_header *h)
  665. {
  666. u8 *p = (u8 *)h;
  667. u8 *end = p, flags = 0;
  668. u16 devid = 0, devid_start = 0, devid_to = 0;
  669. u32 dev_i, ext_flags = 0;
  670. bool alias = false;
  671. struct ivhd_entry *e;
  672. /*
  673. * First save the recommended feature enable bits from ACPI
  674. */
  675. iommu->acpi_flags = h->flags;
  676. /*
  677. * Done. Now parse the device entries
  678. */
  679. p += sizeof(struct ivhd_header);
  680. end += h->length;
  681. while (p < end) {
  682. e = (struct ivhd_entry *)p;
  683. switch (e->type) {
  684. case IVHD_DEV_ALL:
  685. DUMP_printk(" DEV_ALL\t\t\t first devid: %02x:%02x.%x"
  686. " last device %02x:%02x.%x flags: %02x\n",
  687. PCI_BUS(iommu->first_device),
  688. PCI_SLOT(iommu->first_device),
  689. PCI_FUNC(iommu->first_device),
  690. PCI_BUS(iommu->last_device),
  691. PCI_SLOT(iommu->last_device),
  692. PCI_FUNC(iommu->last_device),
  693. e->flags);
  694. for (dev_i = iommu->first_device;
  695. dev_i <= iommu->last_device; ++dev_i)
  696. set_dev_entry_from_acpi(iommu, dev_i,
  697. e->flags, 0);
  698. break;
  699. case IVHD_DEV_SELECT:
  700. DUMP_printk(" DEV_SELECT\t\t\t devid: %02x:%02x.%x "
  701. "flags: %02x\n",
  702. PCI_BUS(e->devid),
  703. PCI_SLOT(e->devid),
  704. PCI_FUNC(e->devid),
  705. e->flags);
  706. devid = e->devid;
  707. set_dev_entry_from_acpi(iommu, devid, e->flags, 0);
  708. break;
  709. case IVHD_DEV_SELECT_RANGE_START:
  710. DUMP_printk(" DEV_SELECT_RANGE_START\t "
  711. "devid: %02x:%02x.%x flags: %02x\n",
  712. PCI_BUS(e->devid),
  713. PCI_SLOT(e->devid),
  714. PCI_FUNC(e->devid),
  715. e->flags);
  716. devid_start = e->devid;
  717. flags = e->flags;
  718. ext_flags = 0;
  719. alias = false;
  720. break;
  721. case IVHD_DEV_ALIAS:
  722. DUMP_printk(" DEV_ALIAS\t\t\t devid: %02x:%02x.%x "
  723. "flags: %02x devid_to: %02x:%02x.%x\n",
  724. PCI_BUS(e->devid),
  725. PCI_SLOT(e->devid),
  726. PCI_FUNC(e->devid),
  727. e->flags,
  728. PCI_BUS(e->ext >> 8),
  729. PCI_SLOT(e->ext >> 8),
  730. PCI_FUNC(e->ext >> 8));
  731. devid = e->devid;
  732. devid_to = e->ext >> 8;
  733. set_dev_entry_from_acpi(iommu, devid , e->flags, 0);
  734. set_dev_entry_from_acpi(iommu, devid_to, e->flags, 0);
  735. amd_iommu_alias_table[devid] = devid_to;
  736. break;
  737. case IVHD_DEV_ALIAS_RANGE:
  738. DUMP_printk(" DEV_ALIAS_RANGE\t\t "
  739. "devid: %02x:%02x.%x flags: %02x "
  740. "devid_to: %02x:%02x.%x\n",
  741. PCI_BUS(e->devid),
  742. PCI_SLOT(e->devid),
  743. PCI_FUNC(e->devid),
  744. e->flags,
  745. PCI_BUS(e->ext >> 8),
  746. PCI_SLOT(e->ext >> 8),
  747. PCI_FUNC(e->ext >> 8));
  748. devid_start = e->devid;
  749. flags = e->flags;
  750. devid_to = e->ext >> 8;
  751. ext_flags = 0;
  752. alias = true;
  753. break;
  754. case IVHD_DEV_EXT_SELECT:
  755. DUMP_printk(" DEV_EXT_SELECT\t\t devid: %02x:%02x.%x "
  756. "flags: %02x ext: %08x\n",
  757. PCI_BUS(e->devid),
  758. PCI_SLOT(e->devid),
  759. PCI_FUNC(e->devid),
  760. e->flags, e->ext);
  761. devid = e->devid;
  762. set_dev_entry_from_acpi(iommu, devid, e->flags,
  763. e->ext);
  764. break;
  765. case IVHD_DEV_EXT_SELECT_RANGE:
  766. DUMP_printk(" DEV_EXT_SELECT_RANGE\t devid: "
  767. "%02x:%02x.%x flags: %02x ext: %08x\n",
  768. PCI_BUS(e->devid),
  769. PCI_SLOT(e->devid),
  770. PCI_FUNC(e->devid),
  771. e->flags, e->ext);
  772. devid_start = e->devid;
  773. flags = e->flags;
  774. ext_flags = e->ext;
  775. alias = false;
  776. break;
  777. case IVHD_DEV_RANGE_END:
  778. DUMP_printk(" DEV_RANGE_END\t\t devid: %02x:%02x.%x\n",
  779. PCI_BUS(e->devid),
  780. PCI_SLOT(e->devid),
  781. PCI_FUNC(e->devid));
  782. devid = e->devid;
  783. for (dev_i = devid_start; dev_i <= devid; ++dev_i) {
  784. if (alias) {
  785. amd_iommu_alias_table[dev_i] = devid_to;
  786. set_dev_entry_from_acpi(iommu,
  787. devid_to, flags, ext_flags);
  788. }
  789. set_dev_entry_from_acpi(iommu, dev_i,
  790. flags, ext_flags);
  791. }
  792. break;
  793. default:
  794. break;
  795. }
  796. p += ivhd_entry_length(p);
  797. }
  798. }
  799. /* Initializes the device->iommu mapping for the driver */
  800. static int __init init_iommu_devices(struct amd_iommu *iommu)
  801. {
  802. u32 i;
  803. for (i = iommu->first_device; i <= iommu->last_device; ++i)
  804. set_iommu_for_device(iommu, i);
  805. return 0;
  806. }
  807. static void __init free_iommu_one(struct amd_iommu *iommu)
  808. {
  809. free_command_buffer(iommu);
  810. free_event_buffer(iommu);
  811. free_ppr_log(iommu);
  812. iommu_unmap_mmio_space(iommu);
  813. }
  814. static void __init free_iommu_all(void)
  815. {
  816. struct amd_iommu *iommu, *next;
  817. for_each_iommu_safe(iommu, next) {
  818. list_del(&iommu->list);
  819. free_iommu_one(iommu);
  820. kfree(iommu);
  821. }
  822. }
  823. /*
  824. * This function clues the initialization function for one IOMMU
  825. * together and also allocates the command buffer and programs the
  826. * hardware. It does NOT enable the IOMMU. This is done afterwards.
  827. */
  828. static int __init init_iommu_one(struct amd_iommu *iommu, struct ivhd_header *h)
  829. {
  830. spin_lock_init(&iommu->lock);
  831. /* Add IOMMU to internal data structures */
  832. list_add_tail(&iommu->list, &amd_iommu_list);
  833. iommu->index = amd_iommus_present++;
  834. if (unlikely(iommu->index >= MAX_IOMMUS)) {
  835. WARN(1, "AMD-Vi: System has more IOMMUs than supported by this driver\n");
  836. return -ENOSYS;
  837. }
  838. /* Index is fine - add IOMMU to the array */
  839. amd_iommus[iommu->index] = iommu;
  840. /*
  841. * Copy data from ACPI table entry to the iommu struct
  842. */
  843. iommu->dev = pci_get_bus_and_slot(PCI_BUS(h->devid), h->devid & 0xff);
  844. if (!iommu->dev)
  845. return 1;
  846. iommu->cap_ptr = h->cap_ptr;
  847. iommu->pci_seg = h->pci_seg;
  848. iommu->mmio_phys = h->mmio_phys;
  849. iommu->mmio_base = iommu_map_mmio_space(h->mmio_phys);
  850. if (!iommu->mmio_base)
  851. return -ENOMEM;
  852. iommu->cmd_buf = alloc_command_buffer(iommu);
  853. if (!iommu->cmd_buf)
  854. return -ENOMEM;
  855. iommu->evt_buf = alloc_event_buffer(iommu);
  856. if (!iommu->evt_buf)
  857. return -ENOMEM;
  858. iommu->int_enabled = false;
  859. init_iommu_from_pci(iommu);
  860. init_iommu_from_acpi(iommu, h);
  861. init_iommu_devices(iommu);
  862. if (iommu_feature(iommu, FEATURE_PPR)) {
  863. iommu->ppr_log = alloc_ppr_log(iommu);
  864. if (!iommu->ppr_log)
  865. return -ENOMEM;
  866. }
  867. if (iommu->cap & (1UL << IOMMU_CAP_NPCACHE))
  868. amd_iommu_np_cache = true;
  869. return pci_enable_device(iommu->dev);
  870. }
  871. /*
  872. * Iterates over all IOMMU entries in the ACPI table, allocates the
  873. * IOMMU structure and initializes it with init_iommu_one()
  874. */
  875. static int __init init_iommu_all(struct acpi_table_header *table)
  876. {
  877. u8 *p = (u8 *)table, *end = (u8 *)table;
  878. struct ivhd_header *h;
  879. struct amd_iommu *iommu;
  880. int ret;
  881. end += table->length;
  882. p += IVRS_HEADER_LENGTH;
  883. while (p < end) {
  884. h = (struct ivhd_header *)p;
  885. switch (*p) {
  886. case ACPI_IVHD_TYPE:
  887. DUMP_printk("device: %02x:%02x.%01x cap: %04x "
  888. "seg: %d flags: %01x info %04x\n",
  889. PCI_BUS(h->devid), PCI_SLOT(h->devid),
  890. PCI_FUNC(h->devid), h->cap_ptr,
  891. h->pci_seg, h->flags, h->info);
  892. DUMP_printk(" mmio-addr: %016llx\n",
  893. h->mmio_phys);
  894. iommu = kzalloc(sizeof(struct amd_iommu), GFP_KERNEL);
  895. if (iommu == NULL) {
  896. amd_iommu_init_err = -ENOMEM;
  897. return 0;
  898. }
  899. ret = init_iommu_one(iommu, h);
  900. if (ret) {
  901. amd_iommu_init_err = ret;
  902. return 0;
  903. }
  904. break;
  905. default:
  906. break;
  907. }
  908. p += h->length;
  909. }
  910. WARN_ON(p != end);
  911. return 0;
  912. }
  913. /****************************************************************************
  914. *
  915. * The following functions initialize the MSI interrupts for all IOMMUs
  916. * in the system. Its a bit challenging because there could be multiple
  917. * IOMMUs per PCI BDF but we can call pci_enable_msi(x) only once per
  918. * pci_dev.
  919. *
  920. ****************************************************************************/
  921. static int iommu_setup_msi(struct amd_iommu *iommu)
  922. {
  923. int r;
  924. r = pci_enable_msi(iommu->dev);
  925. if (r)
  926. return r;
  927. r = request_threaded_irq(iommu->dev->irq,
  928. amd_iommu_int_handler,
  929. amd_iommu_int_thread,
  930. 0, "AMD-Vi",
  931. iommu->dev);
  932. if (r) {
  933. pci_disable_msi(iommu->dev);
  934. return r;
  935. }
  936. iommu->int_enabled = true;
  937. return 0;
  938. }
  939. static int iommu_init_msi(struct amd_iommu *iommu)
  940. {
  941. int ret;
  942. if (iommu->int_enabled)
  943. goto enable_faults;
  944. if (pci_find_capability(iommu->dev, PCI_CAP_ID_MSI))
  945. ret = iommu_setup_msi(iommu);
  946. else
  947. ret = -ENODEV;
  948. if (ret)
  949. return ret;
  950. enable_faults:
  951. iommu_feature_enable(iommu, CONTROL_EVT_INT_EN);
  952. if (iommu->ppr_log != NULL)
  953. iommu_feature_enable(iommu, CONTROL_PPFINT_EN);
  954. return 0;
  955. }
  956. /****************************************************************************
  957. *
  958. * The next functions belong to the third pass of parsing the ACPI
  959. * table. In this last pass the memory mapping requirements are
  960. * gathered (like exclusion and unity mapping reanges).
  961. *
  962. ****************************************************************************/
  963. static void __init free_unity_maps(void)
  964. {
  965. struct unity_map_entry *entry, *next;
  966. list_for_each_entry_safe(entry, next, &amd_iommu_unity_map, list) {
  967. list_del(&entry->list);
  968. kfree(entry);
  969. }
  970. }
  971. /* called when we find an exclusion range definition in ACPI */
  972. static int __init init_exclusion_range(struct ivmd_header *m)
  973. {
  974. int i;
  975. switch (m->type) {
  976. case ACPI_IVMD_TYPE:
  977. set_device_exclusion_range(m->devid, m);
  978. break;
  979. case ACPI_IVMD_TYPE_ALL:
  980. for (i = 0; i <= amd_iommu_last_bdf; ++i)
  981. set_device_exclusion_range(i, m);
  982. break;
  983. case ACPI_IVMD_TYPE_RANGE:
  984. for (i = m->devid; i <= m->aux; ++i)
  985. set_device_exclusion_range(i, m);
  986. break;
  987. default:
  988. break;
  989. }
  990. return 0;
  991. }
  992. /* called for unity map ACPI definition */
  993. static int __init init_unity_map_range(struct ivmd_header *m)
  994. {
  995. struct unity_map_entry *e = 0;
  996. char *s;
  997. e = kzalloc(sizeof(*e), GFP_KERNEL);
  998. if (e == NULL)
  999. return -ENOMEM;
  1000. switch (m->type) {
  1001. default:
  1002. kfree(e);
  1003. return 0;
  1004. case ACPI_IVMD_TYPE:
  1005. s = "IVMD_TYPEi\t\t\t";
  1006. e->devid_start = e->devid_end = m->devid;
  1007. break;
  1008. case ACPI_IVMD_TYPE_ALL:
  1009. s = "IVMD_TYPE_ALL\t\t";
  1010. e->devid_start = 0;
  1011. e->devid_end = amd_iommu_last_bdf;
  1012. break;
  1013. case ACPI_IVMD_TYPE_RANGE:
  1014. s = "IVMD_TYPE_RANGE\t\t";
  1015. e->devid_start = m->devid;
  1016. e->devid_end = m->aux;
  1017. break;
  1018. }
  1019. e->address_start = PAGE_ALIGN(m->range_start);
  1020. e->address_end = e->address_start + PAGE_ALIGN(m->range_length);
  1021. e->prot = m->flags >> 1;
  1022. DUMP_printk("%s devid_start: %02x:%02x.%x devid_end: %02x:%02x.%x"
  1023. " range_start: %016llx range_end: %016llx flags: %x\n", s,
  1024. PCI_BUS(e->devid_start), PCI_SLOT(e->devid_start),
  1025. PCI_FUNC(e->devid_start), PCI_BUS(e->devid_end),
  1026. PCI_SLOT(e->devid_end), PCI_FUNC(e->devid_end),
  1027. e->address_start, e->address_end, m->flags);
  1028. list_add_tail(&e->list, &amd_iommu_unity_map);
  1029. return 0;
  1030. }
  1031. /* iterates over all memory definitions we find in the ACPI table */
  1032. static int __init init_memory_definitions(struct acpi_table_header *table)
  1033. {
  1034. u8 *p = (u8 *)table, *end = (u8 *)table;
  1035. struct ivmd_header *m;
  1036. end += table->length;
  1037. p += IVRS_HEADER_LENGTH;
  1038. while (p < end) {
  1039. m = (struct ivmd_header *)p;
  1040. if (m->flags & IVMD_FLAG_EXCL_RANGE)
  1041. init_exclusion_range(m);
  1042. else if (m->flags & IVMD_FLAG_UNITY_MAP)
  1043. init_unity_map_range(m);
  1044. p += m->length;
  1045. }
  1046. return 0;
  1047. }
  1048. /*
  1049. * Init the device table to not allow DMA access for devices and
  1050. * suppress all page faults
  1051. */
  1052. static void init_device_table(void)
  1053. {
  1054. u32 devid;
  1055. for (devid = 0; devid <= amd_iommu_last_bdf; ++devid) {
  1056. set_dev_entry_bit(devid, DEV_ENTRY_VALID);
  1057. set_dev_entry_bit(devid, DEV_ENTRY_TRANSLATION);
  1058. }
  1059. }
  1060. static void iommu_init_flags(struct amd_iommu *iommu)
  1061. {
  1062. iommu->acpi_flags & IVHD_FLAG_HT_TUN_EN_MASK ?
  1063. iommu_feature_enable(iommu, CONTROL_HT_TUN_EN) :
  1064. iommu_feature_disable(iommu, CONTROL_HT_TUN_EN);
  1065. iommu->acpi_flags & IVHD_FLAG_PASSPW_EN_MASK ?
  1066. iommu_feature_enable(iommu, CONTROL_PASSPW_EN) :
  1067. iommu_feature_disable(iommu, CONTROL_PASSPW_EN);
  1068. iommu->acpi_flags & IVHD_FLAG_RESPASSPW_EN_MASK ?
  1069. iommu_feature_enable(iommu, CONTROL_RESPASSPW_EN) :
  1070. iommu_feature_disable(iommu, CONTROL_RESPASSPW_EN);
  1071. iommu->acpi_flags & IVHD_FLAG_ISOC_EN_MASK ?
  1072. iommu_feature_enable(iommu, CONTROL_ISOC_EN) :
  1073. iommu_feature_disable(iommu, CONTROL_ISOC_EN);
  1074. /*
  1075. * make IOMMU memory accesses cache coherent
  1076. */
  1077. iommu_feature_enable(iommu, CONTROL_COHERENT_EN);
  1078. /* Set IOTLB invalidation timeout to 1s */
  1079. iommu_set_inv_tlb_timeout(iommu, CTRL_INV_TO_1S);
  1080. }
  1081. static void iommu_apply_resume_quirks(struct amd_iommu *iommu)
  1082. {
  1083. int i, j;
  1084. u32 ioc_feature_control;
  1085. struct pci_dev *pdev = NULL;
  1086. /* RD890 BIOSes may not have completely reconfigured the iommu */
  1087. if (!is_rd890_iommu(iommu->dev))
  1088. return;
  1089. /*
  1090. * First, we need to ensure that the iommu is enabled. This is
  1091. * controlled by a register in the northbridge
  1092. */
  1093. pdev = pci_get_bus_and_slot(iommu->dev->bus->number, PCI_DEVFN(0, 0));
  1094. if (!pdev)
  1095. return;
  1096. /* Select Northbridge indirect register 0x75 and enable writing */
  1097. pci_write_config_dword(pdev, 0x60, 0x75 | (1 << 7));
  1098. pci_read_config_dword(pdev, 0x64, &ioc_feature_control);
  1099. /* Enable the iommu */
  1100. if (!(ioc_feature_control & 0x1))
  1101. pci_write_config_dword(pdev, 0x64, ioc_feature_control | 1);
  1102. pci_dev_put(pdev);
  1103. /* Restore the iommu BAR */
  1104. pci_write_config_dword(iommu->dev, iommu->cap_ptr + 4,
  1105. iommu->stored_addr_lo);
  1106. pci_write_config_dword(iommu->dev, iommu->cap_ptr + 8,
  1107. iommu->stored_addr_hi);
  1108. /* Restore the l1 indirect regs for each of the 6 l1s */
  1109. for (i = 0; i < 6; i++)
  1110. for (j = 0; j < 0x12; j++)
  1111. iommu_write_l1(iommu, i, j, iommu->stored_l1[i][j]);
  1112. /* Restore the l2 indirect regs */
  1113. for (i = 0; i < 0x83; i++)
  1114. iommu_write_l2(iommu, i, iommu->stored_l2[i]);
  1115. /* Lock PCI setup registers */
  1116. pci_write_config_dword(iommu->dev, iommu->cap_ptr + 4,
  1117. iommu->stored_addr_lo | 1);
  1118. }
  1119. /*
  1120. * This function finally enables all IOMMUs found in the system after
  1121. * they have been initialized
  1122. */
  1123. static void enable_iommus(void)
  1124. {
  1125. struct amd_iommu *iommu;
  1126. for_each_iommu(iommu) {
  1127. iommu_disable(iommu);
  1128. iommu_init_flags(iommu);
  1129. iommu_set_device_table(iommu);
  1130. iommu_enable_command_buffer(iommu);
  1131. iommu_enable_event_buffer(iommu);
  1132. iommu_enable_ppr_log(iommu);
  1133. iommu_enable_gt(iommu);
  1134. iommu_set_exclusion_range(iommu);
  1135. iommu_init_msi(iommu);
  1136. iommu_enable(iommu);
  1137. iommu_flush_all_caches(iommu);
  1138. }
  1139. }
  1140. static void disable_iommus(void)
  1141. {
  1142. struct amd_iommu *iommu;
  1143. for_each_iommu(iommu)
  1144. iommu_disable(iommu);
  1145. }
  1146. /*
  1147. * Suspend/Resume support
  1148. * disable suspend until real resume implemented
  1149. */
  1150. static void amd_iommu_resume(void)
  1151. {
  1152. struct amd_iommu *iommu;
  1153. for_each_iommu(iommu)
  1154. iommu_apply_resume_quirks(iommu);
  1155. /* re-load the hardware */
  1156. enable_iommus();
  1157. }
  1158. static int amd_iommu_suspend(void)
  1159. {
  1160. /* disable IOMMUs to go out of the way for BIOS */
  1161. disable_iommus();
  1162. return 0;
  1163. }
  1164. static struct syscore_ops amd_iommu_syscore_ops = {
  1165. .suspend = amd_iommu_suspend,
  1166. .resume = amd_iommu_resume,
  1167. };
  1168. static void __init free_on_init_error(void)
  1169. {
  1170. amd_iommu_uninit_devices();
  1171. free_pages((unsigned long)amd_iommu_pd_alloc_bitmap,
  1172. get_order(MAX_DOMAIN_ID/8));
  1173. free_pages((unsigned long)amd_iommu_rlookup_table,
  1174. get_order(rlookup_table_size));
  1175. free_pages((unsigned long)amd_iommu_alias_table,
  1176. get_order(alias_table_size));
  1177. free_pages((unsigned long)amd_iommu_dev_table,
  1178. get_order(dev_table_size));
  1179. free_iommu_all();
  1180. free_unity_maps();
  1181. #ifdef CONFIG_GART_IOMMU
  1182. /*
  1183. * We failed to initialize the AMD IOMMU - try fallback to GART
  1184. * if possible.
  1185. */
  1186. gart_iommu_init();
  1187. #endif
  1188. }
  1189. /*
  1190. * This is the hardware init function for AMD IOMMU in the system.
  1191. * This function is called either from amd_iommu_init or from the interrupt
  1192. * remapping setup code.
  1193. *
  1194. * This function basically parses the ACPI table for AMD IOMMU (IVRS)
  1195. * three times:
  1196. *
  1197. * 1 pass) Find the highest PCI device id the driver has to handle.
  1198. * Upon this information the size of the data structures is
  1199. * determined that needs to be allocated.
  1200. *
  1201. * 2 pass) Initialize the data structures just allocated with the
  1202. * information in the ACPI table about available AMD IOMMUs
  1203. * in the system. It also maps the PCI devices in the
  1204. * system to specific IOMMUs
  1205. *
  1206. * 3 pass) After the basic data structures are allocated and
  1207. * initialized we update them with information about memory
  1208. * remapping requirements parsed out of the ACPI table in
  1209. * this last pass.
  1210. *
  1211. * After everything is set up the IOMMUs are enabled and the necessary
  1212. * hotplug and suspend notifiers are registered.
  1213. */
  1214. int __init amd_iommu_init_hardware(void)
  1215. {
  1216. int i, ret = 0;
  1217. if (!amd_iommu_detected)
  1218. return -ENODEV;
  1219. if (amd_iommu_dev_table != NULL) {
  1220. /* Hardware already initialized */
  1221. return 0;
  1222. }
  1223. /*
  1224. * First parse ACPI tables to find the largest Bus/Dev/Func
  1225. * we need to handle. Upon this information the shared data
  1226. * structures for the IOMMUs in the system will be allocated
  1227. */
  1228. if (acpi_table_parse("IVRS", find_last_devid_acpi) != 0)
  1229. return -ENODEV;
  1230. ret = amd_iommu_init_err;
  1231. if (ret)
  1232. goto out;
  1233. dev_table_size = tbl_size(DEV_TABLE_ENTRY_SIZE);
  1234. alias_table_size = tbl_size(ALIAS_TABLE_ENTRY_SIZE);
  1235. rlookup_table_size = tbl_size(RLOOKUP_TABLE_ENTRY_SIZE);
  1236. /* Device table - directly used by all IOMMUs */
  1237. ret = -ENOMEM;
  1238. amd_iommu_dev_table = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
  1239. get_order(dev_table_size));
  1240. if (amd_iommu_dev_table == NULL)
  1241. goto out;
  1242. /*
  1243. * Alias table - map PCI Bus/Dev/Func to Bus/Dev/Func the
  1244. * IOMMU see for that device
  1245. */
  1246. amd_iommu_alias_table = (void *)__get_free_pages(GFP_KERNEL,
  1247. get_order(alias_table_size));
  1248. if (amd_iommu_alias_table == NULL)
  1249. goto free;
  1250. /* IOMMU rlookup table - find the IOMMU for a specific device */
  1251. amd_iommu_rlookup_table = (void *)__get_free_pages(
  1252. GFP_KERNEL | __GFP_ZERO,
  1253. get_order(rlookup_table_size));
  1254. if (amd_iommu_rlookup_table == NULL)
  1255. goto free;
  1256. amd_iommu_pd_alloc_bitmap = (void *)__get_free_pages(
  1257. GFP_KERNEL | __GFP_ZERO,
  1258. get_order(MAX_DOMAIN_ID/8));
  1259. if (amd_iommu_pd_alloc_bitmap == NULL)
  1260. goto free;
  1261. /* init the device table */
  1262. init_device_table();
  1263. /*
  1264. * let all alias entries point to itself
  1265. */
  1266. for (i = 0; i <= amd_iommu_last_bdf; ++i)
  1267. amd_iommu_alias_table[i] = i;
  1268. /*
  1269. * never allocate domain 0 because its used as the non-allocated and
  1270. * error value placeholder
  1271. */
  1272. amd_iommu_pd_alloc_bitmap[0] = 1;
  1273. spin_lock_init(&amd_iommu_pd_lock);
  1274. /*
  1275. * now the data structures are allocated and basically initialized
  1276. * start the real acpi table scan
  1277. */
  1278. ret = -ENODEV;
  1279. if (acpi_table_parse("IVRS", init_iommu_all) != 0)
  1280. goto free;
  1281. if (amd_iommu_init_err) {
  1282. ret = amd_iommu_init_err;
  1283. goto free;
  1284. }
  1285. if (acpi_table_parse("IVRS", init_memory_definitions) != 0)
  1286. goto free;
  1287. if (amd_iommu_init_err) {
  1288. ret = amd_iommu_init_err;
  1289. goto free;
  1290. }
  1291. ret = amd_iommu_init_devices();
  1292. if (ret)
  1293. goto free;
  1294. enable_iommus();
  1295. amd_iommu_init_notifier();
  1296. register_syscore_ops(&amd_iommu_syscore_ops);
  1297. out:
  1298. return ret;
  1299. free:
  1300. free_on_init_error();
  1301. return ret;
  1302. }
  1303. /*
  1304. * This is the core init function for AMD IOMMU hardware in the system.
  1305. * This function is called from the generic x86 DMA layer initialization
  1306. * code.
  1307. *
  1308. * The function calls amd_iommu_init_hardware() to setup and enable the
  1309. * IOMMU hardware if this has not happened yet. After that the driver
  1310. * registers for the DMA-API and for the IOMMU-API as necessary.
  1311. */
  1312. static int __init amd_iommu_init(void)
  1313. {
  1314. int ret = 0;
  1315. ret = amd_iommu_init_hardware();
  1316. if (ret)
  1317. goto out;
  1318. if (iommu_pass_through)
  1319. ret = amd_iommu_init_passthrough();
  1320. else
  1321. ret = amd_iommu_init_dma_ops();
  1322. if (ret)
  1323. goto free;
  1324. amd_iommu_init_api();
  1325. if (iommu_pass_through)
  1326. goto out;
  1327. if (amd_iommu_unmap_flush)
  1328. printk(KERN_INFO "AMD-Vi: IO/TLB flush on unmap enabled\n");
  1329. else
  1330. printk(KERN_INFO "AMD-Vi: Lazy IO/TLB flushing enabled\n");
  1331. x86_platform.iommu_shutdown = disable_iommus;
  1332. out:
  1333. return ret;
  1334. free:
  1335. disable_iommus();
  1336. free_on_init_error();
  1337. goto out;
  1338. }
  1339. /****************************************************************************
  1340. *
  1341. * Early detect code. This code runs at IOMMU detection time in the DMA
  1342. * layer. It just looks if there is an IVRS ACPI table to detect AMD
  1343. * IOMMUs
  1344. *
  1345. ****************************************************************************/
  1346. static int __init early_amd_iommu_detect(struct acpi_table_header *table)
  1347. {
  1348. return 0;
  1349. }
  1350. int __init amd_iommu_detect(void)
  1351. {
  1352. if (no_iommu || (iommu_detected && !gart_iommu_aperture))
  1353. return -ENODEV;
  1354. if (amd_iommu_disabled)
  1355. return -ENODEV;
  1356. if (acpi_table_parse("IVRS", early_amd_iommu_detect) == 0) {
  1357. iommu_detected = 1;
  1358. amd_iommu_detected = 1;
  1359. x86_init.iommu.iommu_init = amd_iommu_init;
  1360. /* Make sure ACS will be enabled */
  1361. pci_request_acs();
  1362. return 1;
  1363. }
  1364. return -ENODEV;
  1365. }
  1366. /****************************************************************************
  1367. *
  1368. * Parsing functions for the AMD IOMMU specific kernel command line
  1369. * options.
  1370. *
  1371. ****************************************************************************/
  1372. static int __init parse_amd_iommu_dump(char *str)
  1373. {
  1374. amd_iommu_dump = true;
  1375. return 1;
  1376. }
  1377. static int __init parse_amd_iommu_options(char *str)
  1378. {
  1379. for (; *str; ++str) {
  1380. if (strncmp(str, "fullflush", 9) == 0)
  1381. amd_iommu_unmap_flush = true;
  1382. if (strncmp(str, "off", 3) == 0)
  1383. amd_iommu_disabled = true;
  1384. if (strncmp(str, "force_isolation", 15) == 0)
  1385. amd_iommu_force_isolation = true;
  1386. }
  1387. return 1;
  1388. }
  1389. __setup("amd_iommu_dump", parse_amd_iommu_dump);
  1390. __setup("amd_iommu=", parse_amd_iommu_options);
  1391. IOMMU_INIT_FINISH(amd_iommu_detect,
  1392. gart_iommu_hole_init,
  1393. 0,
  1394. 0);
  1395. bool amd_iommu_v2_supported(void)
  1396. {
  1397. return amd_iommu_v2_present;
  1398. }
  1399. EXPORT_SYMBOL(amd_iommu_v2_supported);