amd_iommu_init.c 39 KB

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