amd_iommu_init.c 42 KB

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