amd_iommu_init.c 41 KB

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