amd_iommu_init.c 43 KB

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