amd_iommu_init.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346
  1. /*
  2. * Copyright (C) 2007-2008 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/gfp.h>
  22. #include <linux/list.h>
  23. #include <linux/sysdev.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/msi.h>
  26. #include <asm/pci-direct.h>
  27. #include <asm/amd_iommu_types.h>
  28. #include <asm/amd_iommu.h>
  29. #include <asm/iommu.h>
  30. #include <asm/gart.h>
  31. /*
  32. * definitions for the ACPI scanning code
  33. */
  34. #define IVRS_HEADER_LENGTH 48
  35. #define ACPI_IVHD_TYPE 0x10
  36. #define ACPI_IVMD_TYPE_ALL 0x20
  37. #define ACPI_IVMD_TYPE 0x21
  38. #define ACPI_IVMD_TYPE_RANGE 0x22
  39. #define IVHD_DEV_ALL 0x01
  40. #define IVHD_DEV_SELECT 0x02
  41. #define IVHD_DEV_SELECT_RANGE_START 0x03
  42. #define IVHD_DEV_RANGE_END 0x04
  43. #define IVHD_DEV_ALIAS 0x42
  44. #define IVHD_DEV_ALIAS_RANGE 0x43
  45. #define IVHD_DEV_EXT_SELECT 0x46
  46. #define IVHD_DEV_EXT_SELECT_RANGE 0x47
  47. #define IVHD_FLAG_HT_TUN_EN_MASK 0x01
  48. #define IVHD_FLAG_PASSPW_EN_MASK 0x02
  49. #define IVHD_FLAG_RESPASSPW_EN_MASK 0x04
  50. #define IVHD_FLAG_ISOC_EN_MASK 0x08
  51. #define IVMD_FLAG_EXCL_RANGE 0x08
  52. #define IVMD_FLAG_UNITY_MAP 0x01
  53. #define ACPI_DEVFLAG_INITPASS 0x01
  54. #define ACPI_DEVFLAG_EXTINT 0x02
  55. #define ACPI_DEVFLAG_NMI 0x04
  56. #define ACPI_DEVFLAG_SYSMGT1 0x10
  57. #define ACPI_DEVFLAG_SYSMGT2 0x20
  58. #define ACPI_DEVFLAG_LINT0 0x40
  59. #define ACPI_DEVFLAG_LINT1 0x80
  60. #define ACPI_DEVFLAG_ATSDIS 0x10000000
  61. /*
  62. * ACPI table definitions
  63. *
  64. * These data structures are laid over the table to parse the important values
  65. * out of it.
  66. */
  67. /*
  68. * structure describing one IOMMU in the ACPI table. Typically followed by one
  69. * or more ivhd_entrys.
  70. */
  71. struct ivhd_header {
  72. u8 type;
  73. u8 flags;
  74. u16 length;
  75. u16 devid;
  76. u16 cap_ptr;
  77. u64 mmio_phys;
  78. u16 pci_seg;
  79. u16 info;
  80. u32 reserved;
  81. } __attribute__((packed));
  82. /*
  83. * A device entry describing which devices a specific IOMMU translates and
  84. * which requestor ids they use.
  85. */
  86. struct ivhd_entry {
  87. u8 type;
  88. u16 devid;
  89. u8 flags;
  90. u32 ext;
  91. } __attribute__((packed));
  92. /*
  93. * An AMD IOMMU memory definition structure. It defines things like exclusion
  94. * ranges for devices and regions that should be unity mapped.
  95. */
  96. struct ivmd_header {
  97. u8 type;
  98. u8 flags;
  99. u16 length;
  100. u16 devid;
  101. u16 aux;
  102. u64 resv;
  103. u64 range_start;
  104. u64 range_length;
  105. } __attribute__((packed));
  106. bool amd_iommu_dump;
  107. static int __initdata amd_iommu_detected;
  108. u16 amd_iommu_last_bdf; /* largest PCI device id we have
  109. to handle */
  110. LIST_HEAD(amd_iommu_unity_map); /* a list of required unity mappings
  111. we find in ACPI */
  112. #ifdef CONFIG_IOMMU_STRESS
  113. bool amd_iommu_isolate = false;
  114. #else
  115. bool amd_iommu_isolate = true; /* if true, device isolation is
  116. enabled */
  117. #endif
  118. bool amd_iommu_unmap_flush; /* if true, flush on every unmap */
  119. LIST_HEAD(amd_iommu_list); /* list of all AMD IOMMUs in the
  120. system */
  121. /*
  122. * Pointer to the device table which is shared by all AMD IOMMUs
  123. * it is indexed by the PCI device id or the HT unit id and contains
  124. * information about the domain the device belongs to as well as the
  125. * page table root pointer.
  126. */
  127. struct dev_table_entry *amd_iommu_dev_table;
  128. /*
  129. * The alias table is a driver specific data structure which contains the
  130. * mappings of the PCI device ids to the actual requestor ids on the IOMMU.
  131. * More than one device can share the same requestor id.
  132. */
  133. u16 *amd_iommu_alias_table;
  134. /*
  135. * The rlookup table is used to find the IOMMU which is responsible
  136. * for a specific device. It is also indexed by the PCI device id.
  137. */
  138. struct amd_iommu **amd_iommu_rlookup_table;
  139. /*
  140. * The pd table (protection domain table) is used to find the protection domain
  141. * data structure a device belongs to. Indexed with the PCI device id too.
  142. */
  143. struct protection_domain **amd_iommu_pd_table;
  144. /*
  145. * AMD IOMMU allows up to 2^16 differend protection domains. This is a bitmap
  146. * to know which ones are already in use.
  147. */
  148. unsigned long *amd_iommu_pd_alloc_bitmap;
  149. static u32 dev_table_size; /* size of the device table */
  150. static u32 alias_table_size; /* size of the alias table */
  151. static u32 rlookup_table_size; /* size if the rlookup table */
  152. static inline void update_last_devid(u16 devid)
  153. {
  154. if (devid > amd_iommu_last_bdf)
  155. amd_iommu_last_bdf = devid;
  156. }
  157. static inline unsigned long tbl_size(int entry_size)
  158. {
  159. unsigned shift = PAGE_SHIFT +
  160. get_order(((int)amd_iommu_last_bdf + 1) * entry_size);
  161. return 1UL << shift;
  162. }
  163. /****************************************************************************
  164. *
  165. * AMD IOMMU MMIO register space handling functions
  166. *
  167. * These functions are used to program the IOMMU device registers in
  168. * MMIO space required for that driver.
  169. *
  170. ****************************************************************************/
  171. /*
  172. * This function set the exclusion range in the IOMMU. DMA accesses to the
  173. * exclusion range are passed through untranslated
  174. */
  175. static void iommu_set_exclusion_range(struct amd_iommu *iommu)
  176. {
  177. u64 start = iommu->exclusion_start & PAGE_MASK;
  178. u64 limit = (start + iommu->exclusion_length) & PAGE_MASK;
  179. u64 entry;
  180. if (!iommu->exclusion_start)
  181. return;
  182. entry = start | MMIO_EXCL_ENABLE_MASK;
  183. memcpy_toio(iommu->mmio_base + MMIO_EXCL_BASE_OFFSET,
  184. &entry, sizeof(entry));
  185. entry = limit;
  186. memcpy_toio(iommu->mmio_base + MMIO_EXCL_LIMIT_OFFSET,
  187. &entry, sizeof(entry));
  188. }
  189. /* Programs the physical address of the device table into the IOMMU hardware */
  190. static void __init iommu_set_device_table(struct amd_iommu *iommu)
  191. {
  192. u64 entry;
  193. BUG_ON(iommu->mmio_base == NULL);
  194. entry = virt_to_phys(amd_iommu_dev_table);
  195. entry |= (dev_table_size >> 12) - 1;
  196. memcpy_toio(iommu->mmio_base + MMIO_DEV_TABLE_OFFSET,
  197. &entry, sizeof(entry));
  198. }
  199. /* Generic functions to enable/disable certain features of the IOMMU. */
  200. static void iommu_feature_enable(struct amd_iommu *iommu, u8 bit)
  201. {
  202. u32 ctrl;
  203. ctrl = readl(iommu->mmio_base + MMIO_CONTROL_OFFSET);
  204. ctrl |= (1 << bit);
  205. writel(ctrl, iommu->mmio_base + MMIO_CONTROL_OFFSET);
  206. }
  207. static void __init iommu_feature_disable(struct amd_iommu *iommu, u8 bit)
  208. {
  209. u32 ctrl;
  210. ctrl = readl(iommu->mmio_base + MMIO_CONTROL_OFFSET);
  211. ctrl &= ~(1 << bit);
  212. writel(ctrl, iommu->mmio_base + MMIO_CONTROL_OFFSET);
  213. }
  214. /* Function to enable the hardware */
  215. static void iommu_enable(struct amd_iommu *iommu)
  216. {
  217. printk(KERN_INFO "AMD IOMMU: Enabling IOMMU at %s cap 0x%hx\n",
  218. dev_name(&iommu->dev->dev), iommu->cap_ptr);
  219. iommu_feature_enable(iommu, CONTROL_IOMMU_EN);
  220. }
  221. static void iommu_disable(struct amd_iommu *iommu)
  222. {
  223. /* Disable command buffer */
  224. iommu_feature_disable(iommu, CONTROL_CMDBUF_EN);
  225. /* Disable event logging and event interrupts */
  226. iommu_feature_disable(iommu, CONTROL_EVT_INT_EN);
  227. iommu_feature_disable(iommu, CONTROL_EVT_LOG_EN);
  228. /* Disable IOMMU hardware itself */
  229. iommu_feature_disable(iommu, CONTROL_IOMMU_EN);
  230. }
  231. /*
  232. * mapping and unmapping functions for the IOMMU MMIO space. Each AMD IOMMU in
  233. * the system has one.
  234. */
  235. static u8 * __init iommu_map_mmio_space(u64 address)
  236. {
  237. u8 *ret;
  238. if (!request_mem_region(address, MMIO_REGION_LENGTH, "amd_iommu"))
  239. return NULL;
  240. ret = ioremap_nocache(address, MMIO_REGION_LENGTH);
  241. if (ret != NULL)
  242. return ret;
  243. release_mem_region(address, MMIO_REGION_LENGTH);
  244. return NULL;
  245. }
  246. static void __init iommu_unmap_mmio_space(struct amd_iommu *iommu)
  247. {
  248. if (iommu->mmio_base)
  249. iounmap(iommu->mmio_base);
  250. release_mem_region(iommu->mmio_phys, MMIO_REGION_LENGTH);
  251. }
  252. /****************************************************************************
  253. *
  254. * The functions below belong to the first pass of AMD IOMMU ACPI table
  255. * parsing. In this pass we try to find out the highest device id this
  256. * code has to handle. Upon this information the size of the shared data
  257. * structures is determined later.
  258. *
  259. ****************************************************************************/
  260. /*
  261. * This function calculates the length of a given IVHD entry
  262. */
  263. static inline int ivhd_entry_length(u8 *ivhd)
  264. {
  265. return 0x04 << (*ivhd >> 6);
  266. }
  267. /*
  268. * This function reads the last device id the IOMMU has to handle from the PCI
  269. * capability header for this IOMMU
  270. */
  271. static int __init find_last_devid_on_pci(int bus, int dev, int fn, int cap_ptr)
  272. {
  273. u32 cap;
  274. cap = read_pci_config(bus, dev, fn, cap_ptr+MMIO_RANGE_OFFSET);
  275. update_last_devid(calc_devid(MMIO_GET_BUS(cap), MMIO_GET_LD(cap)));
  276. return 0;
  277. }
  278. /*
  279. * After reading the highest device id from the IOMMU PCI capability header
  280. * this function looks if there is a higher device id defined in the ACPI table
  281. */
  282. static int __init find_last_devid_from_ivhd(struct ivhd_header *h)
  283. {
  284. u8 *p = (void *)h, *end = (void *)h;
  285. struct ivhd_entry *dev;
  286. p += sizeof(*h);
  287. end += h->length;
  288. find_last_devid_on_pci(PCI_BUS(h->devid),
  289. PCI_SLOT(h->devid),
  290. PCI_FUNC(h->devid),
  291. h->cap_ptr);
  292. while (p < end) {
  293. dev = (struct ivhd_entry *)p;
  294. switch (dev->type) {
  295. case IVHD_DEV_SELECT:
  296. case IVHD_DEV_RANGE_END:
  297. case IVHD_DEV_ALIAS:
  298. case IVHD_DEV_EXT_SELECT:
  299. /* all the above subfield types refer to device ids */
  300. update_last_devid(dev->devid);
  301. break;
  302. default:
  303. break;
  304. }
  305. p += ivhd_entry_length(p);
  306. }
  307. WARN_ON(p != end);
  308. return 0;
  309. }
  310. /*
  311. * Iterate over all IVHD entries in the ACPI table and find the highest device
  312. * id which we need to handle. This is the first of three functions which parse
  313. * the ACPI table. So we check the checksum here.
  314. */
  315. static int __init find_last_devid_acpi(struct acpi_table_header *table)
  316. {
  317. int i;
  318. u8 checksum = 0, *p = (u8 *)table, *end = (u8 *)table;
  319. struct ivhd_header *h;
  320. /*
  321. * Validate checksum here so we don't need to do it when
  322. * we actually parse the table
  323. */
  324. for (i = 0; i < table->length; ++i)
  325. checksum += p[i];
  326. if (checksum != 0)
  327. /* ACPI table corrupt */
  328. return -ENODEV;
  329. p += IVRS_HEADER_LENGTH;
  330. end += table->length;
  331. while (p < end) {
  332. h = (struct ivhd_header *)p;
  333. switch (h->type) {
  334. case ACPI_IVHD_TYPE:
  335. find_last_devid_from_ivhd(h);
  336. break;
  337. default:
  338. break;
  339. }
  340. p += h->length;
  341. }
  342. WARN_ON(p != end);
  343. return 0;
  344. }
  345. /****************************************************************************
  346. *
  347. * The following functions belong the the code path which parses the ACPI table
  348. * the second time. In this ACPI parsing iteration we allocate IOMMU specific
  349. * data structures, initialize the device/alias/rlookup table and also
  350. * basically initialize the hardware.
  351. *
  352. ****************************************************************************/
  353. /*
  354. * Allocates the command buffer. This buffer is per AMD IOMMU. We can
  355. * write commands to that buffer later and the IOMMU will execute them
  356. * asynchronously
  357. */
  358. static u8 * __init alloc_command_buffer(struct amd_iommu *iommu)
  359. {
  360. u8 *cmd_buf = (u8 *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
  361. get_order(CMD_BUFFER_SIZE));
  362. if (cmd_buf == NULL)
  363. return NULL;
  364. iommu->cmd_buf_size = CMD_BUFFER_SIZE;
  365. return cmd_buf;
  366. }
  367. /*
  368. * This function writes the command buffer address to the hardware and
  369. * enables it.
  370. */
  371. static void iommu_enable_command_buffer(struct amd_iommu *iommu)
  372. {
  373. u64 entry;
  374. BUG_ON(iommu->cmd_buf == NULL);
  375. entry = (u64)virt_to_phys(iommu->cmd_buf);
  376. entry |= MMIO_CMD_SIZE_512;
  377. memcpy_toio(iommu->mmio_base + MMIO_CMD_BUF_OFFSET,
  378. &entry, sizeof(entry));
  379. /* set head and tail to zero manually */
  380. writel(0x00, iommu->mmio_base + MMIO_CMD_HEAD_OFFSET);
  381. writel(0x00, iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
  382. iommu_feature_enable(iommu, CONTROL_CMDBUF_EN);
  383. }
  384. static void __init free_command_buffer(struct amd_iommu *iommu)
  385. {
  386. free_pages((unsigned long)iommu->cmd_buf,
  387. get_order(iommu->cmd_buf_size));
  388. }
  389. /* allocates the memory where the IOMMU will log its events to */
  390. static u8 * __init alloc_event_buffer(struct amd_iommu *iommu)
  391. {
  392. iommu->evt_buf = (u8 *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
  393. get_order(EVT_BUFFER_SIZE));
  394. if (iommu->evt_buf == NULL)
  395. return NULL;
  396. return iommu->evt_buf;
  397. }
  398. static void iommu_enable_event_buffer(struct amd_iommu *iommu)
  399. {
  400. u64 entry;
  401. BUG_ON(iommu->evt_buf == NULL);
  402. entry = (u64)virt_to_phys(iommu->evt_buf) | EVT_LEN_MASK;
  403. memcpy_toio(iommu->mmio_base + MMIO_EVT_BUF_OFFSET,
  404. &entry, sizeof(entry));
  405. iommu_feature_enable(iommu, CONTROL_EVT_LOG_EN);
  406. }
  407. static void __init free_event_buffer(struct amd_iommu *iommu)
  408. {
  409. free_pages((unsigned long)iommu->evt_buf, get_order(EVT_BUFFER_SIZE));
  410. }
  411. /* sets a specific bit in the device table entry. */
  412. static void set_dev_entry_bit(u16 devid, u8 bit)
  413. {
  414. int i = (bit >> 5) & 0x07;
  415. int _bit = bit & 0x1f;
  416. amd_iommu_dev_table[devid].data[i] |= (1 << _bit);
  417. }
  418. /* Writes the specific IOMMU for a device into the rlookup table */
  419. static void __init set_iommu_for_device(struct amd_iommu *iommu, u16 devid)
  420. {
  421. amd_iommu_rlookup_table[devid] = iommu;
  422. }
  423. /*
  424. * This function takes the device specific flags read from the ACPI
  425. * table and sets up the device table entry with that information
  426. */
  427. static void __init set_dev_entry_from_acpi(struct amd_iommu *iommu,
  428. u16 devid, u32 flags, u32 ext_flags)
  429. {
  430. if (flags & ACPI_DEVFLAG_INITPASS)
  431. set_dev_entry_bit(devid, DEV_ENTRY_INIT_PASS);
  432. if (flags & ACPI_DEVFLAG_EXTINT)
  433. set_dev_entry_bit(devid, DEV_ENTRY_EINT_PASS);
  434. if (flags & ACPI_DEVFLAG_NMI)
  435. set_dev_entry_bit(devid, DEV_ENTRY_NMI_PASS);
  436. if (flags & ACPI_DEVFLAG_SYSMGT1)
  437. set_dev_entry_bit(devid, DEV_ENTRY_SYSMGT1);
  438. if (flags & ACPI_DEVFLAG_SYSMGT2)
  439. set_dev_entry_bit(devid, DEV_ENTRY_SYSMGT2);
  440. if (flags & ACPI_DEVFLAG_LINT0)
  441. set_dev_entry_bit(devid, DEV_ENTRY_LINT0_PASS);
  442. if (flags & ACPI_DEVFLAG_LINT1)
  443. set_dev_entry_bit(devid, DEV_ENTRY_LINT1_PASS);
  444. set_iommu_for_device(iommu, devid);
  445. }
  446. /*
  447. * Reads the device exclusion range from ACPI and initialize IOMMU with
  448. * it
  449. */
  450. static void __init set_device_exclusion_range(u16 devid, struct ivmd_header *m)
  451. {
  452. struct amd_iommu *iommu = amd_iommu_rlookup_table[devid];
  453. if (!(m->flags & IVMD_FLAG_EXCL_RANGE))
  454. return;
  455. if (iommu) {
  456. /*
  457. * We only can configure exclusion ranges per IOMMU, not
  458. * per device. But we can enable the exclusion range per
  459. * device. This is done here
  460. */
  461. set_dev_entry_bit(m->devid, DEV_ENTRY_EX);
  462. iommu->exclusion_start = m->range_start;
  463. iommu->exclusion_length = m->range_length;
  464. }
  465. }
  466. /*
  467. * This function reads some important data from the IOMMU PCI space and
  468. * initializes the driver data structure with it. It reads the hardware
  469. * capabilities and the first/last device entries
  470. */
  471. static void __init init_iommu_from_pci(struct amd_iommu *iommu)
  472. {
  473. int cap_ptr = iommu->cap_ptr;
  474. u32 range, misc;
  475. pci_read_config_dword(iommu->dev, cap_ptr + MMIO_CAP_HDR_OFFSET,
  476. &iommu->cap);
  477. pci_read_config_dword(iommu->dev, cap_ptr + MMIO_RANGE_OFFSET,
  478. &range);
  479. pci_read_config_dword(iommu->dev, cap_ptr + MMIO_MISC_OFFSET,
  480. &misc);
  481. iommu->first_device = calc_devid(MMIO_GET_BUS(range),
  482. MMIO_GET_FD(range));
  483. iommu->last_device = calc_devid(MMIO_GET_BUS(range),
  484. MMIO_GET_LD(range));
  485. iommu->evt_msi_num = MMIO_MSI_NUM(misc);
  486. }
  487. /*
  488. * Takes a pointer to an AMD IOMMU entry in the ACPI table and
  489. * initializes the hardware and our data structures with it.
  490. */
  491. static void __init init_iommu_from_acpi(struct amd_iommu *iommu,
  492. struct ivhd_header *h)
  493. {
  494. u8 *p = (u8 *)h;
  495. u8 *end = p, flags = 0;
  496. u16 dev_i, devid = 0, devid_start = 0, devid_to = 0;
  497. u32 ext_flags = 0;
  498. bool alias = false;
  499. struct ivhd_entry *e;
  500. /*
  501. * First set the recommended feature enable bits from ACPI
  502. * into the IOMMU control registers
  503. */
  504. h->flags & IVHD_FLAG_HT_TUN_EN_MASK ?
  505. iommu_feature_enable(iommu, CONTROL_HT_TUN_EN) :
  506. iommu_feature_disable(iommu, CONTROL_HT_TUN_EN);
  507. h->flags & IVHD_FLAG_PASSPW_EN_MASK ?
  508. iommu_feature_enable(iommu, CONTROL_PASSPW_EN) :
  509. iommu_feature_disable(iommu, CONTROL_PASSPW_EN);
  510. h->flags & IVHD_FLAG_RESPASSPW_EN_MASK ?
  511. iommu_feature_enable(iommu, CONTROL_RESPASSPW_EN) :
  512. iommu_feature_disable(iommu, CONTROL_RESPASSPW_EN);
  513. h->flags & IVHD_FLAG_ISOC_EN_MASK ?
  514. iommu_feature_enable(iommu, CONTROL_ISOC_EN) :
  515. iommu_feature_disable(iommu, CONTROL_ISOC_EN);
  516. /*
  517. * make IOMMU memory accesses cache coherent
  518. */
  519. iommu_feature_enable(iommu, CONTROL_COHERENT_EN);
  520. /*
  521. * Done. Now parse the device entries
  522. */
  523. p += sizeof(struct ivhd_header);
  524. end += h->length;
  525. while (p < end) {
  526. e = (struct ivhd_entry *)p;
  527. switch (e->type) {
  528. case IVHD_DEV_ALL:
  529. DUMP_printk(" DEV_ALL\t\t\t first devid: %02x:%02x.%x"
  530. " last device %02x:%02x.%x flags: %02x\n",
  531. PCI_BUS(iommu->first_device),
  532. PCI_SLOT(iommu->first_device),
  533. PCI_FUNC(iommu->first_device),
  534. PCI_BUS(iommu->last_device),
  535. PCI_SLOT(iommu->last_device),
  536. PCI_FUNC(iommu->last_device),
  537. e->flags);
  538. for (dev_i = iommu->first_device;
  539. dev_i <= iommu->last_device; ++dev_i)
  540. set_dev_entry_from_acpi(iommu, dev_i,
  541. e->flags, 0);
  542. break;
  543. case IVHD_DEV_SELECT:
  544. DUMP_printk(" DEV_SELECT\t\t\t devid: %02x:%02x.%x "
  545. "flags: %02x\n",
  546. PCI_BUS(e->devid),
  547. PCI_SLOT(e->devid),
  548. PCI_FUNC(e->devid),
  549. e->flags);
  550. devid = e->devid;
  551. set_dev_entry_from_acpi(iommu, devid, e->flags, 0);
  552. break;
  553. case IVHD_DEV_SELECT_RANGE_START:
  554. DUMP_printk(" DEV_SELECT_RANGE_START\t "
  555. "devid: %02x:%02x.%x flags: %02x\n",
  556. PCI_BUS(e->devid),
  557. PCI_SLOT(e->devid),
  558. PCI_FUNC(e->devid),
  559. e->flags);
  560. devid_start = e->devid;
  561. flags = e->flags;
  562. ext_flags = 0;
  563. alias = false;
  564. break;
  565. case IVHD_DEV_ALIAS:
  566. DUMP_printk(" DEV_ALIAS\t\t\t devid: %02x:%02x.%x "
  567. "flags: %02x devid_to: %02x:%02x.%x\n",
  568. PCI_BUS(e->devid),
  569. PCI_SLOT(e->devid),
  570. PCI_FUNC(e->devid),
  571. e->flags,
  572. PCI_BUS(e->ext >> 8),
  573. PCI_SLOT(e->ext >> 8),
  574. PCI_FUNC(e->ext >> 8));
  575. devid = e->devid;
  576. devid_to = e->ext >> 8;
  577. set_dev_entry_from_acpi(iommu, devid_to, e->flags, 0);
  578. amd_iommu_alias_table[devid] = devid_to;
  579. break;
  580. case IVHD_DEV_ALIAS_RANGE:
  581. DUMP_printk(" DEV_ALIAS_RANGE\t\t "
  582. "devid: %02x:%02x.%x flags: %02x "
  583. "devid_to: %02x:%02x.%x\n",
  584. PCI_BUS(e->devid),
  585. PCI_SLOT(e->devid),
  586. PCI_FUNC(e->devid),
  587. e->flags,
  588. PCI_BUS(e->ext >> 8),
  589. PCI_SLOT(e->ext >> 8),
  590. PCI_FUNC(e->ext >> 8));
  591. devid_start = e->devid;
  592. flags = e->flags;
  593. devid_to = e->ext >> 8;
  594. ext_flags = 0;
  595. alias = true;
  596. break;
  597. case IVHD_DEV_EXT_SELECT:
  598. DUMP_printk(" DEV_EXT_SELECT\t\t devid: %02x:%02x.%x "
  599. "flags: %02x ext: %08x\n",
  600. PCI_BUS(e->devid),
  601. PCI_SLOT(e->devid),
  602. PCI_FUNC(e->devid),
  603. e->flags, e->ext);
  604. devid = e->devid;
  605. set_dev_entry_from_acpi(iommu, devid, e->flags,
  606. e->ext);
  607. break;
  608. case IVHD_DEV_EXT_SELECT_RANGE:
  609. DUMP_printk(" DEV_EXT_SELECT_RANGE\t devid: "
  610. "%02x:%02x.%x flags: %02x ext: %08x\n",
  611. PCI_BUS(e->devid),
  612. PCI_SLOT(e->devid),
  613. PCI_FUNC(e->devid),
  614. e->flags, e->ext);
  615. devid_start = e->devid;
  616. flags = e->flags;
  617. ext_flags = e->ext;
  618. alias = false;
  619. break;
  620. case IVHD_DEV_RANGE_END:
  621. DUMP_printk(" DEV_RANGE_END\t\t devid: %02x:%02x.%x\n",
  622. PCI_BUS(e->devid),
  623. PCI_SLOT(e->devid),
  624. PCI_FUNC(e->devid));
  625. devid = e->devid;
  626. for (dev_i = devid_start; dev_i <= devid; ++dev_i) {
  627. if (alias)
  628. amd_iommu_alias_table[dev_i] = devid_to;
  629. set_dev_entry_from_acpi(iommu,
  630. amd_iommu_alias_table[dev_i],
  631. flags, ext_flags);
  632. }
  633. break;
  634. default:
  635. break;
  636. }
  637. p += ivhd_entry_length(p);
  638. }
  639. }
  640. /* Initializes the device->iommu mapping for the driver */
  641. static int __init init_iommu_devices(struct amd_iommu *iommu)
  642. {
  643. u16 i;
  644. for (i = iommu->first_device; i <= iommu->last_device; ++i)
  645. set_iommu_for_device(iommu, i);
  646. return 0;
  647. }
  648. static void __init free_iommu_one(struct amd_iommu *iommu)
  649. {
  650. free_command_buffer(iommu);
  651. free_event_buffer(iommu);
  652. iommu_unmap_mmio_space(iommu);
  653. }
  654. static void __init free_iommu_all(void)
  655. {
  656. struct amd_iommu *iommu, *next;
  657. for_each_iommu_safe(iommu, next) {
  658. list_del(&iommu->list);
  659. free_iommu_one(iommu);
  660. kfree(iommu);
  661. }
  662. }
  663. /*
  664. * This function clues the initialization function for one IOMMU
  665. * together and also allocates the command buffer and programs the
  666. * hardware. It does NOT enable the IOMMU. This is done afterwards.
  667. */
  668. static int __init init_iommu_one(struct amd_iommu *iommu, struct ivhd_header *h)
  669. {
  670. spin_lock_init(&iommu->lock);
  671. list_add_tail(&iommu->list, &amd_iommu_list);
  672. /*
  673. * Copy data from ACPI table entry to the iommu struct
  674. */
  675. iommu->dev = pci_get_bus_and_slot(PCI_BUS(h->devid), h->devid & 0xff);
  676. if (!iommu->dev)
  677. return 1;
  678. iommu->cap_ptr = h->cap_ptr;
  679. iommu->pci_seg = h->pci_seg;
  680. iommu->mmio_phys = h->mmio_phys;
  681. iommu->mmio_base = iommu_map_mmio_space(h->mmio_phys);
  682. if (!iommu->mmio_base)
  683. return -ENOMEM;
  684. iommu->cmd_buf = alloc_command_buffer(iommu);
  685. if (!iommu->cmd_buf)
  686. return -ENOMEM;
  687. iommu->evt_buf = alloc_event_buffer(iommu);
  688. if (!iommu->evt_buf)
  689. return -ENOMEM;
  690. iommu->int_enabled = false;
  691. init_iommu_from_pci(iommu);
  692. init_iommu_from_acpi(iommu, h);
  693. init_iommu_devices(iommu);
  694. return pci_enable_device(iommu->dev);
  695. }
  696. /*
  697. * Iterates over all IOMMU entries in the ACPI table, allocates the
  698. * IOMMU structure and initializes it with init_iommu_one()
  699. */
  700. static int __init init_iommu_all(struct acpi_table_header *table)
  701. {
  702. u8 *p = (u8 *)table, *end = (u8 *)table;
  703. struct ivhd_header *h;
  704. struct amd_iommu *iommu;
  705. int ret;
  706. end += table->length;
  707. p += IVRS_HEADER_LENGTH;
  708. while (p < end) {
  709. h = (struct ivhd_header *)p;
  710. switch (*p) {
  711. case ACPI_IVHD_TYPE:
  712. DUMP_printk("IOMMU: device: %02x:%02x.%01x cap: %04x "
  713. "seg: %d flags: %01x info %04x\n",
  714. PCI_BUS(h->devid), PCI_SLOT(h->devid),
  715. PCI_FUNC(h->devid), h->cap_ptr,
  716. h->pci_seg, h->flags, h->info);
  717. DUMP_printk(" mmio-addr: %016llx\n",
  718. h->mmio_phys);
  719. iommu = kzalloc(sizeof(struct amd_iommu), GFP_KERNEL);
  720. if (iommu == NULL)
  721. return -ENOMEM;
  722. ret = init_iommu_one(iommu, h);
  723. if (ret)
  724. return ret;
  725. break;
  726. default:
  727. break;
  728. }
  729. p += h->length;
  730. }
  731. WARN_ON(p != end);
  732. return 0;
  733. }
  734. /****************************************************************************
  735. *
  736. * The following functions initialize the MSI interrupts for all IOMMUs
  737. * in the system. Its a bit challenging because there could be multiple
  738. * IOMMUs per PCI BDF but we can call pci_enable_msi(x) only once per
  739. * pci_dev.
  740. *
  741. ****************************************************************************/
  742. static int __init iommu_setup_msi(struct amd_iommu *iommu)
  743. {
  744. int r;
  745. if (pci_enable_msi(iommu->dev))
  746. return 1;
  747. r = request_irq(iommu->dev->irq, amd_iommu_int_handler,
  748. IRQF_SAMPLE_RANDOM,
  749. "AMD IOMMU",
  750. NULL);
  751. if (r) {
  752. pci_disable_msi(iommu->dev);
  753. return 1;
  754. }
  755. iommu->int_enabled = true;
  756. iommu_feature_enable(iommu, CONTROL_EVT_INT_EN);
  757. return 0;
  758. }
  759. static int iommu_init_msi(struct amd_iommu *iommu)
  760. {
  761. if (iommu->int_enabled)
  762. return 0;
  763. if (pci_find_capability(iommu->dev, PCI_CAP_ID_MSI))
  764. return iommu_setup_msi(iommu);
  765. return 1;
  766. }
  767. /****************************************************************************
  768. *
  769. * The next functions belong to the third pass of parsing the ACPI
  770. * table. In this last pass the memory mapping requirements are
  771. * gathered (like exclusion and unity mapping reanges).
  772. *
  773. ****************************************************************************/
  774. static void __init free_unity_maps(void)
  775. {
  776. struct unity_map_entry *entry, *next;
  777. list_for_each_entry_safe(entry, next, &amd_iommu_unity_map, list) {
  778. list_del(&entry->list);
  779. kfree(entry);
  780. }
  781. }
  782. /* called when we find an exclusion range definition in ACPI */
  783. static int __init init_exclusion_range(struct ivmd_header *m)
  784. {
  785. int i;
  786. switch (m->type) {
  787. case ACPI_IVMD_TYPE:
  788. set_device_exclusion_range(m->devid, m);
  789. break;
  790. case ACPI_IVMD_TYPE_ALL:
  791. for (i = 0; i <= amd_iommu_last_bdf; ++i)
  792. set_device_exclusion_range(i, m);
  793. break;
  794. case ACPI_IVMD_TYPE_RANGE:
  795. for (i = m->devid; i <= m->aux; ++i)
  796. set_device_exclusion_range(i, m);
  797. break;
  798. default:
  799. break;
  800. }
  801. return 0;
  802. }
  803. /* called for unity map ACPI definition */
  804. static int __init init_unity_map_range(struct ivmd_header *m)
  805. {
  806. struct unity_map_entry *e = 0;
  807. char *s;
  808. e = kzalloc(sizeof(*e), GFP_KERNEL);
  809. if (e == NULL)
  810. return -ENOMEM;
  811. switch (m->type) {
  812. default:
  813. kfree(e);
  814. return 0;
  815. case ACPI_IVMD_TYPE:
  816. s = "IVMD_TYPEi\t\t\t";
  817. e->devid_start = e->devid_end = m->devid;
  818. break;
  819. case ACPI_IVMD_TYPE_ALL:
  820. s = "IVMD_TYPE_ALL\t\t";
  821. e->devid_start = 0;
  822. e->devid_end = amd_iommu_last_bdf;
  823. break;
  824. case ACPI_IVMD_TYPE_RANGE:
  825. s = "IVMD_TYPE_RANGE\t\t";
  826. e->devid_start = m->devid;
  827. e->devid_end = m->aux;
  828. break;
  829. }
  830. e->address_start = PAGE_ALIGN(m->range_start);
  831. e->address_end = e->address_start + PAGE_ALIGN(m->range_length);
  832. e->prot = m->flags >> 1;
  833. DUMP_printk("%s devid_start: %02x:%02x.%x devid_end: %02x:%02x.%x"
  834. " range_start: %016llx range_end: %016llx flags: %x\n", s,
  835. PCI_BUS(e->devid_start), PCI_SLOT(e->devid_start),
  836. PCI_FUNC(e->devid_start), PCI_BUS(e->devid_end),
  837. PCI_SLOT(e->devid_end), PCI_FUNC(e->devid_end),
  838. e->address_start, e->address_end, m->flags);
  839. list_add_tail(&e->list, &amd_iommu_unity_map);
  840. return 0;
  841. }
  842. /* iterates over all memory definitions we find in the ACPI table */
  843. static int __init init_memory_definitions(struct acpi_table_header *table)
  844. {
  845. u8 *p = (u8 *)table, *end = (u8 *)table;
  846. struct ivmd_header *m;
  847. end += table->length;
  848. p += IVRS_HEADER_LENGTH;
  849. while (p < end) {
  850. m = (struct ivmd_header *)p;
  851. if (m->flags & IVMD_FLAG_EXCL_RANGE)
  852. init_exclusion_range(m);
  853. else if (m->flags & IVMD_FLAG_UNITY_MAP)
  854. init_unity_map_range(m);
  855. p += m->length;
  856. }
  857. return 0;
  858. }
  859. /*
  860. * Init the device table to not allow DMA access for devices and
  861. * suppress all page faults
  862. */
  863. static void init_device_table(void)
  864. {
  865. u16 devid;
  866. for (devid = 0; devid <= amd_iommu_last_bdf; ++devid) {
  867. set_dev_entry_bit(devid, DEV_ENTRY_VALID);
  868. set_dev_entry_bit(devid, DEV_ENTRY_TRANSLATION);
  869. }
  870. }
  871. /*
  872. * This function finally enables all IOMMUs found in the system after
  873. * they have been initialized
  874. */
  875. static void enable_iommus(void)
  876. {
  877. struct amd_iommu *iommu;
  878. for_each_iommu(iommu) {
  879. iommu_disable(iommu);
  880. iommu_set_device_table(iommu);
  881. iommu_enable_command_buffer(iommu);
  882. iommu_enable_event_buffer(iommu);
  883. iommu_set_exclusion_range(iommu);
  884. iommu_init_msi(iommu);
  885. iommu_enable(iommu);
  886. }
  887. }
  888. static void disable_iommus(void)
  889. {
  890. struct amd_iommu *iommu;
  891. for_each_iommu(iommu)
  892. iommu_disable(iommu);
  893. }
  894. /*
  895. * Suspend/Resume support
  896. * disable suspend until real resume implemented
  897. */
  898. static int amd_iommu_resume(struct sys_device *dev)
  899. {
  900. /*
  901. * Disable IOMMUs before reprogramming the hardware registers.
  902. * IOMMU is still enabled from the resume kernel.
  903. */
  904. disable_iommus();
  905. /* re-load the hardware */
  906. enable_iommus();
  907. /*
  908. * we have to flush after the IOMMUs are enabled because a
  909. * disabled IOMMU will never execute the commands we send
  910. */
  911. amd_iommu_flush_all_domains();
  912. amd_iommu_flush_all_devices();
  913. return 0;
  914. }
  915. static int amd_iommu_suspend(struct sys_device *dev, pm_message_t state)
  916. {
  917. /* disable IOMMUs to go out of the way for BIOS */
  918. disable_iommus();
  919. return 0;
  920. }
  921. static struct sysdev_class amd_iommu_sysdev_class = {
  922. .name = "amd_iommu",
  923. .suspend = amd_iommu_suspend,
  924. .resume = amd_iommu_resume,
  925. };
  926. static struct sys_device device_amd_iommu = {
  927. .id = 0,
  928. .cls = &amd_iommu_sysdev_class,
  929. };
  930. /*
  931. * This is the core init function for AMD IOMMU hardware in the system.
  932. * This function is called from the generic x86 DMA layer initialization
  933. * code.
  934. *
  935. * This function basically parses the ACPI table for AMD IOMMU (IVRS)
  936. * three times:
  937. *
  938. * 1 pass) Find the highest PCI device id the driver has to handle.
  939. * Upon this information the size of the data structures is
  940. * determined that needs to be allocated.
  941. *
  942. * 2 pass) Initialize the data structures just allocated with the
  943. * information in the ACPI table about available AMD IOMMUs
  944. * in the system. It also maps the PCI devices in the
  945. * system to specific IOMMUs
  946. *
  947. * 3 pass) After the basic data structures are allocated and
  948. * initialized we update them with information about memory
  949. * remapping requirements parsed out of the ACPI table in
  950. * this last pass.
  951. *
  952. * After that the hardware is initialized and ready to go. In the last
  953. * step we do some Linux specific things like registering the driver in
  954. * the dma_ops interface and initializing the suspend/resume support
  955. * functions. Finally it prints some information about AMD IOMMUs and
  956. * the driver state and enables the hardware.
  957. */
  958. int __init amd_iommu_init(void)
  959. {
  960. int i, ret = 0;
  961. if (no_iommu) {
  962. printk(KERN_INFO "AMD IOMMU disabled by kernel command line\n");
  963. return 0;
  964. }
  965. if (!amd_iommu_detected)
  966. return -ENODEV;
  967. /*
  968. * First parse ACPI tables to find the largest Bus/Dev/Func
  969. * we need to handle. Upon this information the shared data
  970. * structures for the IOMMUs in the system will be allocated
  971. */
  972. if (acpi_table_parse("IVRS", find_last_devid_acpi) != 0)
  973. return -ENODEV;
  974. dev_table_size = tbl_size(DEV_TABLE_ENTRY_SIZE);
  975. alias_table_size = tbl_size(ALIAS_TABLE_ENTRY_SIZE);
  976. rlookup_table_size = tbl_size(RLOOKUP_TABLE_ENTRY_SIZE);
  977. ret = -ENOMEM;
  978. /* Device table - directly used by all IOMMUs */
  979. amd_iommu_dev_table = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
  980. get_order(dev_table_size));
  981. if (amd_iommu_dev_table == NULL)
  982. goto out;
  983. /*
  984. * Alias table - map PCI Bus/Dev/Func to Bus/Dev/Func the
  985. * IOMMU see for that device
  986. */
  987. amd_iommu_alias_table = (void *)__get_free_pages(GFP_KERNEL,
  988. get_order(alias_table_size));
  989. if (amd_iommu_alias_table == NULL)
  990. goto free;
  991. /* IOMMU rlookup table - find the IOMMU for a specific device */
  992. amd_iommu_rlookup_table = (void *)__get_free_pages(
  993. GFP_KERNEL | __GFP_ZERO,
  994. get_order(rlookup_table_size));
  995. if (amd_iommu_rlookup_table == NULL)
  996. goto free;
  997. /*
  998. * Protection Domain table - maps devices to protection domains
  999. * This table has the same size as the rlookup_table
  1000. */
  1001. amd_iommu_pd_table = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
  1002. get_order(rlookup_table_size));
  1003. if (amd_iommu_pd_table == NULL)
  1004. goto free;
  1005. amd_iommu_pd_alloc_bitmap = (void *)__get_free_pages(
  1006. GFP_KERNEL | __GFP_ZERO,
  1007. get_order(MAX_DOMAIN_ID/8));
  1008. if (amd_iommu_pd_alloc_bitmap == NULL)
  1009. goto free;
  1010. /* init the device table */
  1011. init_device_table();
  1012. /*
  1013. * let all alias entries point to itself
  1014. */
  1015. for (i = 0; i <= amd_iommu_last_bdf; ++i)
  1016. amd_iommu_alias_table[i] = i;
  1017. /*
  1018. * never allocate domain 0 because its used as the non-allocated and
  1019. * error value placeholder
  1020. */
  1021. amd_iommu_pd_alloc_bitmap[0] = 1;
  1022. /*
  1023. * now the data structures are allocated and basically initialized
  1024. * start the real acpi table scan
  1025. */
  1026. ret = -ENODEV;
  1027. if (acpi_table_parse("IVRS", init_iommu_all) != 0)
  1028. goto free;
  1029. if (acpi_table_parse("IVRS", init_memory_definitions) != 0)
  1030. goto free;
  1031. ret = sysdev_class_register(&amd_iommu_sysdev_class);
  1032. if (ret)
  1033. goto free;
  1034. ret = sysdev_register(&device_amd_iommu);
  1035. if (ret)
  1036. goto free;
  1037. ret = amd_iommu_init_dma_ops();
  1038. if (ret)
  1039. goto free;
  1040. enable_iommus();
  1041. printk(KERN_INFO "AMD IOMMU: device isolation ");
  1042. if (amd_iommu_isolate)
  1043. printk("enabled\n");
  1044. else
  1045. printk("disabled\n");
  1046. if (amd_iommu_unmap_flush)
  1047. printk(KERN_INFO "AMD IOMMU: IO/TLB flush on unmap enabled\n");
  1048. else
  1049. printk(KERN_INFO "AMD IOMMU: Lazy IO/TLB flushing enabled\n");
  1050. out:
  1051. return ret;
  1052. free:
  1053. free_pages((unsigned long)amd_iommu_pd_alloc_bitmap,
  1054. get_order(MAX_DOMAIN_ID/8));
  1055. free_pages((unsigned long)amd_iommu_pd_table,
  1056. get_order(rlookup_table_size));
  1057. free_pages((unsigned long)amd_iommu_rlookup_table,
  1058. get_order(rlookup_table_size));
  1059. free_pages((unsigned long)amd_iommu_alias_table,
  1060. get_order(alias_table_size));
  1061. free_pages((unsigned long)amd_iommu_dev_table,
  1062. get_order(dev_table_size));
  1063. free_iommu_all();
  1064. free_unity_maps();
  1065. goto out;
  1066. }
  1067. void amd_iommu_shutdown(void)
  1068. {
  1069. disable_iommus();
  1070. }
  1071. /****************************************************************************
  1072. *
  1073. * Early detect code. This code runs at IOMMU detection time in the DMA
  1074. * layer. It just looks if there is an IVRS ACPI table to detect AMD
  1075. * IOMMUs
  1076. *
  1077. ****************************************************************************/
  1078. static int __init early_amd_iommu_detect(struct acpi_table_header *table)
  1079. {
  1080. return 0;
  1081. }
  1082. void __init amd_iommu_detect(void)
  1083. {
  1084. if (swiotlb || no_iommu || (iommu_detected && !gart_iommu_aperture))
  1085. return;
  1086. if (acpi_table_parse("IVRS", early_amd_iommu_detect) == 0) {
  1087. iommu_detected = 1;
  1088. amd_iommu_detected = 1;
  1089. #ifdef CONFIG_GART_IOMMU
  1090. gart_iommu_aperture_disabled = 1;
  1091. gart_iommu_aperture = 0;
  1092. #endif
  1093. }
  1094. }
  1095. /****************************************************************************
  1096. *
  1097. * Parsing functions for the AMD IOMMU specific kernel command line
  1098. * options.
  1099. *
  1100. ****************************************************************************/
  1101. static int __init parse_amd_iommu_dump(char *str)
  1102. {
  1103. amd_iommu_dump = true;
  1104. return 1;
  1105. }
  1106. static int __init parse_amd_iommu_options(char *str)
  1107. {
  1108. for (; *str; ++str) {
  1109. if (strncmp(str, "isolate", 7) == 0)
  1110. amd_iommu_isolate = true;
  1111. if (strncmp(str, "share", 5) == 0)
  1112. amd_iommu_isolate = false;
  1113. if (strncmp(str, "fullflush", 9) == 0)
  1114. amd_iommu_unmap_flush = true;
  1115. }
  1116. return 1;
  1117. }
  1118. __setup("amd_iommu_dump", parse_amd_iommu_dump);
  1119. __setup("amd_iommu=", parse_amd_iommu_options);