amd_iommu_init.c 45 KB

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