amd_iommu_init.c 35 KB

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