amd_iommu_init.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371
  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. iommu->evt_buf_size = EVT_BUFFER_SIZE;
  397. return iommu->evt_buf;
  398. }
  399. static void iommu_enable_event_buffer(struct amd_iommu *iommu)
  400. {
  401. u64 entry;
  402. BUG_ON(iommu->evt_buf == NULL);
  403. entry = (u64)virt_to_phys(iommu->evt_buf) | EVT_LEN_MASK;
  404. memcpy_toio(iommu->mmio_base + MMIO_EVT_BUF_OFFSET,
  405. &entry, sizeof(entry));
  406. /* set head and tail to zero manually */
  407. writel(0x00, iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
  408. writel(0x00, iommu->mmio_base + MMIO_EVT_TAIL_OFFSET);
  409. iommu_feature_enable(iommu, CONTROL_EVT_LOG_EN);
  410. }
  411. static void __init free_event_buffer(struct amd_iommu *iommu)
  412. {
  413. free_pages((unsigned long)iommu->evt_buf, get_order(EVT_BUFFER_SIZE));
  414. }
  415. /* sets a specific bit in the device table entry. */
  416. static void set_dev_entry_bit(u16 devid, u8 bit)
  417. {
  418. int i = (bit >> 5) & 0x07;
  419. int _bit = bit & 0x1f;
  420. amd_iommu_dev_table[devid].data[i] |= (1 << _bit);
  421. }
  422. static int get_dev_entry_bit(u16 devid, u8 bit)
  423. {
  424. int i = (bit >> 5) & 0x07;
  425. int _bit = bit & 0x1f;
  426. return (amd_iommu_dev_table[devid].data[i] & (1 << _bit)) >> _bit;
  427. }
  428. void amd_iommu_apply_erratum_63(u16 devid)
  429. {
  430. int sysmgt;
  431. sysmgt = get_dev_entry_bit(devid, DEV_ENTRY_SYSMGT1) |
  432. (get_dev_entry_bit(devid, DEV_ENTRY_SYSMGT2) << 1);
  433. if (sysmgt == 0x01)
  434. set_dev_entry_bit(devid, DEV_ENTRY_IW);
  435. }
  436. /* Writes the specific IOMMU for a device into the rlookup table */
  437. static void __init set_iommu_for_device(struct amd_iommu *iommu, u16 devid)
  438. {
  439. amd_iommu_rlookup_table[devid] = iommu;
  440. }
  441. /*
  442. * This function takes the device specific flags read from the ACPI
  443. * table and sets up the device table entry with that information
  444. */
  445. static void __init set_dev_entry_from_acpi(struct amd_iommu *iommu,
  446. u16 devid, u32 flags, u32 ext_flags)
  447. {
  448. if (flags & ACPI_DEVFLAG_INITPASS)
  449. set_dev_entry_bit(devid, DEV_ENTRY_INIT_PASS);
  450. if (flags & ACPI_DEVFLAG_EXTINT)
  451. set_dev_entry_bit(devid, DEV_ENTRY_EINT_PASS);
  452. if (flags & ACPI_DEVFLAG_NMI)
  453. set_dev_entry_bit(devid, DEV_ENTRY_NMI_PASS);
  454. if (flags & ACPI_DEVFLAG_SYSMGT1)
  455. set_dev_entry_bit(devid, DEV_ENTRY_SYSMGT1);
  456. if (flags & ACPI_DEVFLAG_SYSMGT2)
  457. set_dev_entry_bit(devid, DEV_ENTRY_SYSMGT2);
  458. if (flags & ACPI_DEVFLAG_LINT0)
  459. set_dev_entry_bit(devid, DEV_ENTRY_LINT0_PASS);
  460. if (flags & ACPI_DEVFLAG_LINT1)
  461. set_dev_entry_bit(devid, DEV_ENTRY_LINT1_PASS);
  462. amd_iommu_apply_erratum_63(devid);
  463. set_iommu_for_device(iommu, devid);
  464. }
  465. /*
  466. * Reads the device exclusion range from ACPI and initialize IOMMU with
  467. * it
  468. */
  469. static void __init set_device_exclusion_range(u16 devid, struct ivmd_header *m)
  470. {
  471. struct amd_iommu *iommu = amd_iommu_rlookup_table[devid];
  472. if (!(m->flags & IVMD_FLAG_EXCL_RANGE))
  473. return;
  474. if (iommu) {
  475. /*
  476. * We only can configure exclusion ranges per IOMMU, not
  477. * per device. But we can enable the exclusion range per
  478. * device. This is done here
  479. */
  480. set_dev_entry_bit(m->devid, DEV_ENTRY_EX);
  481. iommu->exclusion_start = m->range_start;
  482. iommu->exclusion_length = m->range_length;
  483. }
  484. }
  485. /*
  486. * This function reads some important data from the IOMMU PCI space and
  487. * initializes the driver data structure with it. It reads the hardware
  488. * capabilities and the first/last device entries
  489. */
  490. static void __init init_iommu_from_pci(struct amd_iommu *iommu)
  491. {
  492. int cap_ptr = iommu->cap_ptr;
  493. u32 range, misc;
  494. pci_read_config_dword(iommu->dev, cap_ptr + MMIO_CAP_HDR_OFFSET,
  495. &iommu->cap);
  496. pci_read_config_dword(iommu->dev, cap_ptr + MMIO_RANGE_OFFSET,
  497. &range);
  498. pci_read_config_dword(iommu->dev, cap_ptr + MMIO_MISC_OFFSET,
  499. &misc);
  500. iommu->first_device = calc_devid(MMIO_GET_BUS(range),
  501. MMIO_GET_FD(range));
  502. iommu->last_device = calc_devid(MMIO_GET_BUS(range),
  503. MMIO_GET_LD(range));
  504. iommu->evt_msi_num = MMIO_MSI_NUM(misc);
  505. }
  506. /*
  507. * Takes a pointer to an AMD IOMMU entry in the ACPI table and
  508. * initializes the hardware and our data structures with it.
  509. */
  510. static void __init init_iommu_from_acpi(struct amd_iommu *iommu,
  511. struct ivhd_header *h)
  512. {
  513. u8 *p = (u8 *)h;
  514. u8 *end = p, flags = 0;
  515. u16 dev_i, devid = 0, devid_start = 0, devid_to = 0;
  516. u32 ext_flags = 0;
  517. bool alias = false;
  518. struct ivhd_entry *e;
  519. /*
  520. * First set the recommended feature enable bits from ACPI
  521. * into the IOMMU control registers
  522. */
  523. h->flags & IVHD_FLAG_HT_TUN_EN_MASK ?
  524. iommu_feature_enable(iommu, CONTROL_HT_TUN_EN) :
  525. iommu_feature_disable(iommu, CONTROL_HT_TUN_EN);
  526. h->flags & IVHD_FLAG_PASSPW_EN_MASK ?
  527. iommu_feature_enable(iommu, CONTROL_PASSPW_EN) :
  528. iommu_feature_disable(iommu, CONTROL_PASSPW_EN);
  529. h->flags & IVHD_FLAG_RESPASSPW_EN_MASK ?
  530. iommu_feature_enable(iommu, CONTROL_RESPASSPW_EN) :
  531. iommu_feature_disable(iommu, CONTROL_RESPASSPW_EN);
  532. h->flags & IVHD_FLAG_ISOC_EN_MASK ?
  533. iommu_feature_enable(iommu, CONTROL_ISOC_EN) :
  534. iommu_feature_disable(iommu, CONTROL_ISOC_EN);
  535. /*
  536. * make IOMMU memory accesses cache coherent
  537. */
  538. iommu_feature_enable(iommu, CONTROL_COHERENT_EN);
  539. /*
  540. * Done. Now parse the device entries
  541. */
  542. p += sizeof(struct ivhd_header);
  543. end += h->length;
  544. while (p < end) {
  545. e = (struct ivhd_entry *)p;
  546. switch (e->type) {
  547. case IVHD_DEV_ALL:
  548. DUMP_printk(" DEV_ALL\t\t\t first devid: %02x:%02x.%x"
  549. " last device %02x:%02x.%x flags: %02x\n",
  550. PCI_BUS(iommu->first_device),
  551. PCI_SLOT(iommu->first_device),
  552. PCI_FUNC(iommu->first_device),
  553. PCI_BUS(iommu->last_device),
  554. PCI_SLOT(iommu->last_device),
  555. PCI_FUNC(iommu->last_device),
  556. e->flags);
  557. for (dev_i = iommu->first_device;
  558. dev_i <= iommu->last_device; ++dev_i)
  559. set_dev_entry_from_acpi(iommu, dev_i,
  560. e->flags, 0);
  561. break;
  562. case IVHD_DEV_SELECT:
  563. DUMP_printk(" DEV_SELECT\t\t\t devid: %02x:%02x.%x "
  564. "flags: %02x\n",
  565. PCI_BUS(e->devid),
  566. PCI_SLOT(e->devid),
  567. PCI_FUNC(e->devid),
  568. e->flags);
  569. devid = e->devid;
  570. set_dev_entry_from_acpi(iommu, devid, e->flags, 0);
  571. break;
  572. case IVHD_DEV_SELECT_RANGE_START:
  573. DUMP_printk(" DEV_SELECT_RANGE_START\t "
  574. "devid: %02x:%02x.%x flags: %02x\n",
  575. PCI_BUS(e->devid),
  576. PCI_SLOT(e->devid),
  577. PCI_FUNC(e->devid),
  578. e->flags);
  579. devid_start = e->devid;
  580. flags = e->flags;
  581. ext_flags = 0;
  582. alias = false;
  583. break;
  584. case IVHD_DEV_ALIAS:
  585. DUMP_printk(" DEV_ALIAS\t\t\t devid: %02x:%02x.%x "
  586. "flags: %02x devid_to: %02x:%02x.%x\n",
  587. PCI_BUS(e->devid),
  588. PCI_SLOT(e->devid),
  589. PCI_FUNC(e->devid),
  590. e->flags,
  591. PCI_BUS(e->ext >> 8),
  592. PCI_SLOT(e->ext >> 8),
  593. PCI_FUNC(e->ext >> 8));
  594. devid = e->devid;
  595. devid_to = e->ext >> 8;
  596. set_dev_entry_from_acpi(iommu, devid , e->flags, 0);
  597. set_dev_entry_from_acpi(iommu, devid_to, e->flags, 0);
  598. amd_iommu_alias_table[devid] = devid_to;
  599. break;
  600. case IVHD_DEV_ALIAS_RANGE:
  601. DUMP_printk(" DEV_ALIAS_RANGE\t\t "
  602. "devid: %02x:%02x.%x flags: %02x "
  603. "devid_to: %02x:%02x.%x\n",
  604. PCI_BUS(e->devid),
  605. PCI_SLOT(e->devid),
  606. PCI_FUNC(e->devid),
  607. e->flags,
  608. PCI_BUS(e->ext >> 8),
  609. PCI_SLOT(e->ext >> 8),
  610. PCI_FUNC(e->ext >> 8));
  611. devid_start = e->devid;
  612. flags = e->flags;
  613. devid_to = e->ext >> 8;
  614. ext_flags = 0;
  615. alias = true;
  616. break;
  617. case IVHD_DEV_EXT_SELECT:
  618. DUMP_printk(" DEV_EXT_SELECT\t\t devid: %02x:%02x.%x "
  619. "flags: %02x ext: %08x\n",
  620. PCI_BUS(e->devid),
  621. PCI_SLOT(e->devid),
  622. PCI_FUNC(e->devid),
  623. e->flags, e->ext);
  624. devid = e->devid;
  625. set_dev_entry_from_acpi(iommu, devid, e->flags,
  626. e->ext);
  627. break;
  628. case IVHD_DEV_EXT_SELECT_RANGE:
  629. DUMP_printk(" DEV_EXT_SELECT_RANGE\t devid: "
  630. "%02x:%02x.%x flags: %02x ext: %08x\n",
  631. PCI_BUS(e->devid),
  632. PCI_SLOT(e->devid),
  633. PCI_FUNC(e->devid),
  634. e->flags, e->ext);
  635. devid_start = e->devid;
  636. flags = e->flags;
  637. ext_flags = e->ext;
  638. alias = false;
  639. break;
  640. case IVHD_DEV_RANGE_END:
  641. DUMP_printk(" DEV_RANGE_END\t\t devid: %02x:%02x.%x\n",
  642. PCI_BUS(e->devid),
  643. PCI_SLOT(e->devid),
  644. PCI_FUNC(e->devid));
  645. devid = e->devid;
  646. for (dev_i = devid_start; dev_i <= devid; ++dev_i) {
  647. if (alias) {
  648. amd_iommu_alias_table[dev_i] = devid_to;
  649. set_dev_entry_from_acpi(iommu,
  650. devid_to, flags, ext_flags);
  651. }
  652. set_dev_entry_from_acpi(iommu, dev_i,
  653. flags, ext_flags);
  654. }
  655. break;
  656. default:
  657. break;
  658. }
  659. p += ivhd_entry_length(p);
  660. }
  661. }
  662. /* Initializes the device->iommu mapping for the driver */
  663. static int __init init_iommu_devices(struct amd_iommu *iommu)
  664. {
  665. u16 i;
  666. for (i = iommu->first_device; i <= iommu->last_device; ++i)
  667. set_iommu_for_device(iommu, i);
  668. return 0;
  669. }
  670. static void __init free_iommu_one(struct amd_iommu *iommu)
  671. {
  672. free_command_buffer(iommu);
  673. free_event_buffer(iommu);
  674. iommu_unmap_mmio_space(iommu);
  675. }
  676. static void __init free_iommu_all(void)
  677. {
  678. struct amd_iommu *iommu, *next;
  679. for_each_iommu_safe(iommu, next) {
  680. list_del(&iommu->list);
  681. free_iommu_one(iommu);
  682. kfree(iommu);
  683. }
  684. }
  685. /*
  686. * This function clues the initialization function for one IOMMU
  687. * together and also allocates the command buffer and programs the
  688. * hardware. It does NOT enable the IOMMU. This is done afterwards.
  689. */
  690. static int __init init_iommu_one(struct amd_iommu *iommu, struct ivhd_header *h)
  691. {
  692. spin_lock_init(&iommu->lock);
  693. list_add_tail(&iommu->list, &amd_iommu_list);
  694. /*
  695. * Copy data from ACPI table entry to the iommu struct
  696. */
  697. iommu->dev = pci_get_bus_and_slot(PCI_BUS(h->devid), h->devid & 0xff);
  698. if (!iommu->dev)
  699. return 1;
  700. iommu->cap_ptr = h->cap_ptr;
  701. iommu->pci_seg = h->pci_seg;
  702. iommu->mmio_phys = h->mmio_phys;
  703. iommu->mmio_base = iommu_map_mmio_space(h->mmio_phys);
  704. if (!iommu->mmio_base)
  705. return -ENOMEM;
  706. iommu->cmd_buf = alloc_command_buffer(iommu);
  707. if (!iommu->cmd_buf)
  708. return -ENOMEM;
  709. iommu->evt_buf = alloc_event_buffer(iommu);
  710. if (!iommu->evt_buf)
  711. return -ENOMEM;
  712. iommu->int_enabled = false;
  713. init_iommu_from_pci(iommu);
  714. init_iommu_from_acpi(iommu, h);
  715. init_iommu_devices(iommu);
  716. return pci_enable_device(iommu->dev);
  717. }
  718. /*
  719. * Iterates over all IOMMU entries in the ACPI table, allocates the
  720. * IOMMU structure and initializes it with init_iommu_one()
  721. */
  722. static int __init init_iommu_all(struct acpi_table_header *table)
  723. {
  724. u8 *p = (u8 *)table, *end = (u8 *)table;
  725. struct ivhd_header *h;
  726. struct amd_iommu *iommu;
  727. int ret;
  728. end += table->length;
  729. p += IVRS_HEADER_LENGTH;
  730. while (p < end) {
  731. h = (struct ivhd_header *)p;
  732. switch (*p) {
  733. case ACPI_IVHD_TYPE:
  734. DUMP_printk("IOMMU: device: %02x:%02x.%01x cap: %04x "
  735. "seg: %d flags: %01x info %04x\n",
  736. PCI_BUS(h->devid), PCI_SLOT(h->devid),
  737. PCI_FUNC(h->devid), h->cap_ptr,
  738. h->pci_seg, h->flags, h->info);
  739. DUMP_printk(" mmio-addr: %016llx\n",
  740. h->mmio_phys);
  741. iommu = kzalloc(sizeof(struct amd_iommu), GFP_KERNEL);
  742. if (iommu == NULL)
  743. return -ENOMEM;
  744. ret = init_iommu_one(iommu, h);
  745. if (ret)
  746. return ret;
  747. break;
  748. default:
  749. break;
  750. }
  751. p += h->length;
  752. }
  753. WARN_ON(p != end);
  754. return 0;
  755. }
  756. /****************************************************************************
  757. *
  758. * The following functions initialize the MSI interrupts for all IOMMUs
  759. * in the system. Its a bit challenging because there could be multiple
  760. * IOMMUs per PCI BDF but we can call pci_enable_msi(x) only once per
  761. * pci_dev.
  762. *
  763. ****************************************************************************/
  764. static int __init iommu_setup_msi(struct amd_iommu *iommu)
  765. {
  766. int r;
  767. if (pci_enable_msi(iommu->dev))
  768. return 1;
  769. r = request_irq(iommu->dev->irq, amd_iommu_int_handler,
  770. IRQF_SAMPLE_RANDOM,
  771. "AMD IOMMU",
  772. NULL);
  773. if (r) {
  774. pci_disable_msi(iommu->dev);
  775. return 1;
  776. }
  777. iommu->int_enabled = true;
  778. iommu_feature_enable(iommu, CONTROL_EVT_INT_EN);
  779. return 0;
  780. }
  781. static int iommu_init_msi(struct amd_iommu *iommu)
  782. {
  783. if (iommu->int_enabled)
  784. return 0;
  785. if (pci_find_capability(iommu->dev, PCI_CAP_ID_MSI))
  786. return iommu_setup_msi(iommu);
  787. return 1;
  788. }
  789. /****************************************************************************
  790. *
  791. * The next functions belong to the third pass of parsing the ACPI
  792. * table. In this last pass the memory mapping requirements are
  793. * gathered (like exclusion and unity mapping reanges).
  794. *
  795. ****************************************************************************/
  796. static void __init free_unity_maps(void)
  797. {
  798. struct unity_map_entry *entry, *next;
  799. list_for_each_entry_safe(entry, next, &amd_iommu_unity_map, list) {
  800. list_del(&entry->list);
  801. kfree(entry);
  802. }
  803. }
  804. /* called when we find an exclusion range definition in ACPI */
  805. static int __init init_exclusion_range(struct ivmd_header *m)
  806. {
  807. int i;
  808. switch (m->type) {
  809. case ACPI_IVMD_TYPE:
  810. set_device_exclusion_range(m->devid, m);
  811. break;
  812. case ACPI_IVMD_TYPE_ALL:
  813. for (i = 0; i <= amd_iommu_last_bdf; ++i)
  814. set_device_exclusion_range(i, m);
  815. break;
  816. case ACPI_IVMD_TYPE_RANGE:
  817. for (i = m->devid; i <= m->aux; ++i)
  818. set_device_exclusion_range(i, m);
  819. break;
  820. default:
  821. break;
  822. }
  823. return 0;
  824. }
  825. /* called for unity map ACPI definition */
  826. static int __init init_unity_map_range(struct ivmd_header *m)
  827. {
  828. struct unity_map_entry *e = 0;
  829. char *s;
  830. e = kzalloc(sizeof(*e), GFP_KERNEL);
  831. if (e == NULL)
  832. return -ENOMEM;
  833. switch (m->type) {
  834. default:
  835. kfree(e);
  836. return 0;
  837. case ACPI_IVMD_TYPE:
  838. s = "IVMD_TYPEi\t\t\t";
  839. e->devid_start = e->devid_end = m->devid;
  840. break;
  841. case ACPI_IVMD_TYPE_ALL:
  842. s = "IVMD_TYPE_ALL\t\t";
  843. e->devid_start = 0;
  844. e->devid_end = amd_iommu_last_bdf;
  845. break;
  846. case ACPI_IVMD_TYPE_RANGE:
  847. s = "IVMD_TYPE_RANGE\t\t";
  848. e->devid_start = m->devid;
  849. e->devid_end = m->aux;
  850. break;
  851. }
  852. e->address_start = PAGE_ALIGN(m->range_start);
  853. e->address_end = e->address_start + PAGE_ALIGN(m->range_length);
  854. e->prot = m->flags >> 1;
  855. DUMP_printk("%s devid_start: %02x:%02x.%x devid_end: %02x:%02x.%x"
  856. " range_start: %016llx range_end: %016llx flags: %x\n", s,
  857. PCI_BUS(e->devid_start), PCI_SLOT(e->devid_start),
  858. PCI_FUNC(e->devid_start), PCI_BUS(e->devid_end),
  859. PCI_SLOT(e->devid_end), PCI_FUNC(e->devid_end),
  860. e->address_start, e->address_end, m->flags);
  861. list_add_tail(&e->list, &amd_iommu_unity_map);
  862. return 0;
  863. }
  864. /* iterates over all memory definitions we find in the ACPI table */
  865. static int __init init_memory_definitions(struct acpi_table_header *table)
  866. {
  867. u8 *p = (u8 *)table, *end = (u8 *)table;
  868. struct ivmd_header *m;
  869. end += table->length;
  870. p += IVRS_HEADER_LENGTH;
  871. while (p < end) {
  872. m = (struct ivmd_header *)p;
  873. if (m->flags & IVMD_FLAG_EXCL_RANGE)
  874. init_exclusion_range(m);
  875. else if (m->flags & IVMD_FLAG_UNITY_MAP)
  876. init_unity_map_range(m);
  877. p += m->length;
  878. }
  879. return 0;
  880. }
  881. /*
  882. * Init the device table to not allow DMA access for devices and
  883. * suppress all page faults
  884. */
  885. static void init_device_table(void)
  886. {
  887. u16 devid;
  888. for (devid = 0; devid <= amd_iommu_last_bdf; ++devid) {
  889. set_dev_entry_bit(devid, DEV_ENTRY_VALID);
  890. set_dev_entry_bit(devid, DEV_ENTRY_TRANSLATION);
  891. }
  892. }
  893. /*
  894. * This function finally enables all IOMMUs found in the system after
  895. * they have been initialized
  896. */
  897. static void enable_iommus(void)
  898. {
  899. struct amd_iommu *iommu;
  900. for_each_iommu(iommu) {
  901. iommu_disable(iommu);
  902. iommu_set_device_table(iommu);
  903. iommu_enable_command_buffer(iommu);
  904. iommu_enable_event_buffer(iommu);
  905. iommu_set_exclusion_range(iommu);
  906. iommu_init_msi(iommu);
  907. iommu_enable(iommu);
  908. }
  909. }
  910. static void disable_iommus(void)
  911. {
  912. struct amd_iommu *iommu;
  913. for_each_iommu(iommu)
  914. iommu_disable(iommu);
  915. }
  916. /*
  917. * Suspend/Resume support
  918. * disable suspend until real resume implemented
  919. */
  920. static int amd_iommu_resume(struct sys_device *dev)
  921. {
  922. /* re-load the hardware */
  923. enable_iommus();
  924. /*
  925. * we have to flush after the IOMMUs are enabled because a
  926. * disabled IOMMU will never execute the commands we send
  927. */
  928. amd_iommu_flush_all_devices();
  929. amd_iommu_flush_all_domains();
  930. return 0;
  931. }
  932. static int amd_iommu_suspend(struct sys_device *dev, pm_message_t state)
  933. {
  934. /* disable IOMMUs to go out of the way for BIOS */
  935. disable_iommus();
  936. return 0;
  937. }
  938. static struct sysdev_class amd_iommu_sysdev_class = {
  939. .name = "amd_iommu",
  940. .suspend = amd_iommu_suspend,
  941. .resume = amd_iommu_resume,
  942. };
  943. static struct sys_device device_amd_iommu = {
  944. .id = 0,
  945. .cls = &amd_iommu_sysdev_class,
  946. };
  947. /*
  948. * This is the core init function for AMD IOMMU hardware in the system.
  949. * This function is called from the generic x86 DMA layer initialization
  950. * code.
  951. *
  952. * This function basically parses the ACPI table for AMD IOMMU (IVRS)
  953. * three times:
  954. *
  955. * 1 pass) Find the highest PCI device id the driver has to handle.
  956. * Upon this information the size of the data structures is
  957. * determined that needs to be allocated.
  958. *
  959. * 2 pass) Initialize the data structures just allocated with the
  960. * information in the ACPI table about available AMD IOMMUs
  961. * in the system. It also maps the PCI devices in the
  962. * system to specific IOMMUs
  963. *
  964. * 3 pass) After the basic data structures are allocated and
  965. * initialized we update them with information about memory
  966. * remapping requirements parsed out of the ACPI table in
  967. * this last pass.
  968. *
  969. * After that the hardware is initialized and ready to go. In the last
  970. * step we do some Linux specific things like registering the driver in
  971. * the dma_ops interface and initializing the suspend/resume support
  972. * functions. Finally it prints some information about AMD IOMMUs and
  973. * the driver state and enables the hardware.
  974. */
  975. int __init amd_iommu_init(void)
  976. {
  977. int i, ret = 0;
  978. if (no_iommu) {
  979. printk(KERN_INFO "AMD IOMMU disabled by kernel command line\n");
  980. return 0;
  981. }
  982. if (!amd_iommu_detected)
  983. return -ENODEV;
  984. /*
  985. * First parse ACPI tables to find the largest Bus/Dev/Func
  986. * we need to handle. Upon this information the shared data
  987. * structures for the IOMMUs in the system will be allocated
  988. */
  989. if (acpi_table_parse("IVRS", find_last_devid_acpi) != 0)
  990. return -ENODEV;
  991. dev_table_size = tbl_size(DEV_TABLE_ENTRY_SIZE);
  992. alias_table_size = tbl_size(ALIAS_TABLE_ENTRY_SIZE);
  993. rlookup_table_size = tbl_size(RLOOKUP_TABLE_ENTRY_SIZE);
  994. ret = -ENOMEM;
  995. /* Device table - directly used by all IOMMUs */
  996. amd_iommu_dev_table = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
  997. get_order(dev_table_size));
  998. if (amd_iommu_dev_table == NULL)
  999. goto out;
  1000. /*
  1001. * Alias table - map PCI Bus/Dev/Func to Bus/Dev/Func the
  1002. * IOMMU see for that device
  1003. */
  1004. amd_iommu_alias_table = (void *)__get_free_pages(GFP_KERNEL,
  1005. get_order(alias_table_size));
  1006. if (amd_iommu_alias_table == NULL)
  1007. goto free;
  1008. /* IOMMU rlookup table - find the IOMMU for a specific device */
  1009. amd_iommu_rlookup_table = (void *)__get_free_pages(
  1010. GFP_KERNEL | __GFP_ZERO,
  1011. get_order(rlookup_table_size));
  1012. if (amd_iommu_rlookup_table == NULL)
  1013. goto free;
  1014. /*
  1015. * Protection Domain table - maps devices to protection domains
  1016. * This table has the same size as the rlookup_table
  1017. */
  1018. amd_iommu_pd_table = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
  1019. get_order(rlookup_table_size));
  1020. if (amd_iommu_pd_table == NULL)
  1021. goto free;
  1022. amd_iommu_pd_alloc_bitmap = (void *)__get_free_pages(
  1023. GFP_KERNEL | __GFP_ZERO,
  1024. get_order(MAX_DOMAIN_ID/8));
  1025. if (amd_iommu_pd_alloc_bitmap == NULL)
  1026. goto free;
  1027. /* init the device table */
  1028. init_device_table();
  1029. /*
  1030. * let all alias entries point to itself
  1031. */
  1032. for (i = 0; i <= amd_iommu_last_bdf; ++i)
  1033. amd_iommu_alias_table[i] = i;
  1034. /*
  1035. * never allocate domain 0 because its used as the non-allocated and
  1036. * error value placeholder
  1037. */
  1038. amd_iommu_pd_alloc_bitmap[0] = 1;
  1039. /*
  1040. * now the data structures are allocated and basically initialized
  1041. * start the real acpi table scan
  1042. */
  1043. ret = -ENODEV;
  1044. if (acpi_table_parse("IVRS", init_iommu_all) != 0)
  1045. goto free;
  1046. if (acpi_table_parse("IVRS", init_memory_definitions) != 0)
  1047. goto free;
  1048. ret = sysdev_class_register(&amd_iommu_sysdev_class);
  1049. if (ret)
  1050. goto free;
  1051. ret = sysdev_register(&device_amd_iommu);
  1052. if (ret)
  1053. goto free;
  1054. ret = amd_iommu_init_dma_ops();
  1055. if (ret)
  1056. goto free;
  1057. enable_iommus();
  1058. printk(KERN_INFO "AMD IOMMU: device isolation ");
  1059. if (amd_iommu_isolate)
  1060. printk("enabled\n");
  1061. else
  1062. printk("disabled\n");
  1063. if (amd_iommu_unmap_flush)
  1064. printk(KERN_INFO "AMD IOMMU: IO/TLB flush on unmap enabled\n");
  1065. else
  1066. printk(KERN_INFO "AMD IOMMU: Lazy IO/TLB flushing enabled\n");
  1067. out:
  1068. return ret;
  1069. free:
  1070. free_pages((unsigned long)amd_iommu_pd_alloc_bitmap,
  1071. get_order(MAX_DOMAIN_ID/8));
  1072. free_pages((unsigned long)amd_iommu_pd_table,
  1073. get_order(rlookup_table_size));
  1074. free_pages((unsigned long)amd_iommu_rlookup_table,
  1075. get_order(rlookup_table_size));
  1076. free_pages((unsigned long)amd_iommu_alias_table,
  1077. get_order(alias_table_size));
  1078. free_pages((unsigned long)amd_iommu_dev_table,
  1079. get_order(dev_table_size));
  1080. free_iommu_all();
  1081. free_unity_maps();
  1082. goto out;
  1083. }
  1084. void amd_iommu_shutdown(void)
  1085. {
  1086. disable_iommus();
  1087. }
  1088. /****************************************************************************
  1089. *
  1090. * Early detect code. This code runs at IOMMU detection time in the DMA
  1091. * layer. It just looks if there is an IVRS ACPI table to detect AMD
  1092. * IOMMUs
  1093. *
  1094. ****************************************************************************/
  1095. static int __init early_amd_iommu_detect(struct acpi_table_header *table)
  1096. {
  1097. return 0;
  1098. }
  1099. void __init amd_iommu_detect(void)
  1100. {
  1101. if (swiotlb || no_iommu || (iommu_detected && !gart_iommu_aperture))
  1102. return;
  1103. if (acpi_table_parse("IVRS", early_amd_iommu_detect) == 0) {
  1104. iommu_detected = 1;
  1105. amd_iommu_detected = 1;
  1106. #ifdef CONFIG_GART_IOMMU
  1107. gart_iommu_aperture_disabled = 1;
  1108. gart_iommu_aperture = 0;
  1109. #endif
  1110. }
  1111. }
  1112. /****************************************************************************
  1113. *
  1114. * Parsing functions for the AMD IOMMU specific kernel command line
  1115. * options.
  1116. *
  1117. ****************************************************************************/
  1118. static int __init parse_amd_iommu_dump(char *str)
  1119. {
  1120. amd_iommu_dump = true;
  1121. return 1;
  1122. }
  1123. static int __init parse_amd_iommu_options(char *str)
  1124. {
  1125. for (; *str; ++str) {
  1126. if (strncmp(str, "isolate", 7) == 0)
  1127. amd_iommu_isolate = true;
  1128. if (strncmp(str, "share", 5) == 0)
  1129. amd_iommu_isolate = false;
  1130. if (strncmp(str, "fullflush", 9) == 0)
  1131. amd_iommu_unmap_flush = true;
  1132. }
  1133. return 1;
  1134. }
  1135. __setup("amd_iommu_dump", parse_amd_iommu_dump);
  1136. __setup("amd_iommu=", parse_amd_iommu_options);