amd_iommu_init.c 39 KB

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