amd_iommu_init.c 43 KB

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