amd_iommu_init.c 39 KB

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