amd_iommu_init.c 40 KB

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