amd_iommu_init.c 35 KB

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