amd_iommu_init.c 43 KB

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