amd_iommu_init.c 35 KB

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