amd_iommu_init.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224
  1. /*
  2. * Copyright (C) 2007-2008 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/gfp.h>
  22. #include <linux/list.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_types.h>
  28. #include <asm/amd_iommu.h>
  29. #include <asm/iommu.h>
  30. #include <asm/gart.h>
  31. /*
  32. * definitions for the ACPI scanning code
  33. */
  34. #define IVRS_HEADER_LENGTH 48
  35. #define ACPI_IVHD_TYPE 0x10
  36. #define ACPI_IVMD_TYPE_ALL 0x20
  37. #define ACPI_IVMD_TYPE 0x21
  38. #define ACPI_IVMD_TYPE_RANGE 0x22
  39. #define IVHD_DEV_ALL 0x01
  40. #define IVHD_DEV_SELECT 0x02
  41. #define IVHD_DEV_SELECT_RANGE_START 0x03
  42. #define IVHD_DEV_RANGE_END 0x04
  43. #define IVHD_DEV_ALIAS 0x42
  44. #define IVHD_DEV_ALIAS_RANGE 0x43
  45. #define IVHD_DEV_EXT_SELECT 0x46
  46. #define IVHD_DEV_EXT_SELECT_RANGE 0x47
  47. #define IVHD_FLAG_HT_TUN_EN_MASK 0x01
  48. #define IVHD_FLAG_PASSPW_EN_MASK 0x02
  49. #define IVHD_FLAG_RESPASSPW_EN_MASK 0x04
  50. #define IVHD_FLAG_ISOC_EN_MASK 0x08
  51. #define IVMD_FLAG_EXCL_RANGE 0x08
  52. #define IVMD_FLAG_UNITY_MAP 0x01
  53. #define ACPI_DEVFLAG_INITPASS 0x01
  54. #define ACPI_DEVFLAG_EXTINT 0x02
  55. #define ACPI_DEVFLAG_NMI 0x04
  56. #define ACPI_DEVFLAG_SYSMGT1 0x10
  57. #define ACPI_DEVFLAG_SYSMGT2 0x20
  58. #define ACPI_DEVFLAG_LINT0 0x40
  59. #define ACPI_DEVFLAG_LINT1 0x80
  60. #define ACPI_DEVFLAG_ATSDIS 0x10000000
  61. /*
  62. * ACPI table definitions
  63. *
  64. * These data structures are laid over the table to parse the important values
  65. * out of it.
  66. */
  67. /*
  68. * structure describing one IOMMU in the ACPI table. Typically followed by one
  69. * or more ivhd_entrys.
  70. */
  71. struct ivhd_header {
  72. u8 type;
  73. u8 flags;
  74. u16 length;
  75. u16 devid;
  76. u16 cap_ptr;
  77. u64 mmio_phys;
  78. u16 pci_seg;
  79. u16 info;
  80. u32 reserved;
  81. } __attribute__((packed));
  82. /*
  83. * A device entry describing which devices a specific IOMMU translates and
  84. * which requestor ids they use.
  85. */
  86. struct ivhd_entry {
  87. u8 type;
  88. u16 devid;
  89. u8 flags;
  90. u32 ext;
  91. } __attribute__((packed));
  92. /*
  93. * An AMD IOMMU memory definition structure. It defines things like exclusion
  94. * ranges for devices and regions that should be unity mapped.
  95. */
  96. struct ivmd_header {
  97. u8 type;
  98. u8 flags;
  99. u16 length;
  100. u16 devid;
  101. u16 aux;
  102. u64 resv;
  103. u64 range_start;
  104. u64 range_length;
  105. } __attribute__((packed));
  106. static int __initdata amd_iommu_detected;
  107. u16 amd_iommu_last_bdf; /* largest PCI device id we have
  108. to handle */
  109. LIST_HEAD(amd_iommu_unity_map); /* a list of required unity mappings
  110. we find in ACPI */
  111. bool amd_iommu_isolate = true; /* if true, device isolation is
  112. enabled */
  113. bool amd_iommu_unmap_flush; /* if true, flush on every unmap */
  114. LIST_HEAD(amd_iommu_list); /* list of all AMD IOMMUs in the
  115. system */
  116. /*
  117. * Pointer to the device table which is shared by all AMD IOMMUs
  118. * it is indexed by the PCI device id or the HT unit id and contains
  119. * information about the domain the device belongs to as well as the
  120. * page table root pointer.
  121. */
  122. struct dev_table_entry *amd_iommu_dev_table;
  123. /*
  124. * The alias table is a driver specific data structure which contains the
  125. * mappings of the PCI device ids to the actual requestor ids on the IOMMU.
  126. * More than one device can share the same requestor id.
  127. */
  128. u16 *amd_iommu_alias_table;
  129. /*
  130. * The rlookup table is used to find the IOMMU which is responsible
  131. * for a specific device. It is also indexed by the PCI device id.
  132. */
  133. struct amd_iommu **amd_iommu_rlookup_table;
  134. /*
  135. * The pd table (protection domain table) is used to find the protection domain
  136. * data structure a device belongs to. Indexed with the PCI device id too.
  137. */
  138. struct protection_domain **amd_iommu_pd_table;
  139. /*
  140. * AMD IOMMU allows up to 2^16 differend protection domains. This is a bitmap
  141. * to know which ones are already in use.
  142. */
  143. unsigned long *amd_iommu_pd_alloc_bitmap;
  144. static u32 dev_table_size; /* size of the device table */
  145. static u32 alias_table_size; /* size of the alias table */
  146. static u32 rlookup_table_size; /* size if the rlookup table */
  147. static inline void update_last_devid(u16 devid)
  148. {
  149. if (devid > amd_iommu_last_bdf)
  150. amd_iommu_last_bdf = devid;
  151. }
  152. static inline unsigned long tbl_size(int entry_size)
  153. {
  154. unsigned shift = PAGE_SHIFT +
  155. get_order(amd_iommu_last_bdf * entry_size);
  156. return 1UL << shift;
  157. }
  158. /****************************************************************************
  159. *
  160. * AMD IOMMU MMIO register space handling functions
  161. *
  162. * These functions are used to program the IOMMU device registers in
  163. * MMIO space required for that driver.
  164. *
  165. ****************************************************************************/
  166. /*
  167. * This function set the exclusion range in the IOMMU. DMA accesses to the
  168. * exclusion range are passed through untranslated
  169. */
  170. static void __init iommu_set_exclusion_range(struct amd_iommu *iommu)
  171. {
  172. u64 start = iommu->exclusion_start & PAGE_MASK;
  173. u64 limit = (start + iommu->exclusion_length) & PAGE_MASK;
  174. u64 entry;
  175. if (!iommu->exclusion_start)
  176. return;
  177. entry = start | MMIO_EXCL_ENABLE_MASK;
  178. memcpy_toio(iommu->mmio_base + MMIO_EXCL_BASE_OFFSET,
  179. &entry, sizeof(entry));
  180. entry = limit;
  181. memcpy_toio(iommu->mmio_base + MMIO_EXCL_LIMIT_OFFSET,
  182. &entry, sizeof(entry));
  183. }
  184. /* Programs the physical address of the device table into the IOMMU hardware */
  185. static void __init iommu_set_device_table(struct amd_iommu *iommu)
  186. {
  187. u64 entry;
  188. BUG_ON(iommu->mmio_base == NULL);
  189. entry = virt_to_phys(amd_iommu_dev_table);
  190. entry |= (dev_table_size >> 12) - 1;
  191. memcpy_toio(iommu->mmio_base + MMIO_DEV_TABLE_OFFSET,
  192. &entry, sizeof(entry));
  193. }
  194. /* Generic functions to enable/disable certain features of the IOMMU. */
  195. static void __init iommu_feature_enable(struct amd_iommu *iommu, u8 bit)
  196. {
  197. u32 ctrl;
  198. ctrl = readl(iommu->mmio_base + MMIO_CONTROL_OFFSET);
  199. ctrl |= (1 << bit);
  200. writel(ctrl, iommu->mmio_base + MMIO_CONTROL_OFFSET);
  201. }
  202. static void __init iommu_feature_disable(struct amd_iommu *iommu, u8 bit)
  203. {
  204. u32 ctrl;
  205. ctrl = readl(iommu->mmio_base + MMIO_CONTROL_OFFSET);
  206. ctrl &= ~(1 << bit);
  207. writel(ctrl, iommu->mmio_base + MMIO_CONTROL_OFFSET);
  208. }
  209. /* Function to enable the hardware */
  210. static void __init iommu_enable(struct amd_iommu *iommu)
  211. {
  212. printk(KERN_INFO "AMD IOMMU: Enabling IOMMU at %s cap 0x%hx\n",
  213. dev_name(&iommu->dev->dev), iommu->cap_ptr);
  214. iommu_feature_enable(iommu, CONTROL_IOMMU_EN);
  215. }
  216. /* Function to enable IOMMU event logging and event interrupts */
  217. static void __init iommu_enable_event_logging(struct amd_iommu *iommu)
  218. {
  219. iommu_feature_enable(iommu, CONTROL_EVT_LOG_EN);
  220. iommu_feature_enable(iommu, CONTROL_EVT_INT_EN);
  221. }
  222. /*
  223. * mapping and unmapping functions for the IOMMU MMIO space. Each AMD IOMMU in
  224. * the system has one.
  225. */
  226. static u8 * __init iommu_map_mmio_space(u64 address)
  227. {
  228. u8 *ret;
  229. if (!request_mem_region(address, MMIO_REGION_LENGTH, "amd_iommu"))
  230. return NULL;
  231. ret = ioremap_nocache(address, MMIO_REGION_LENGTH);
  232. if (ret != NULL)
  233. return ret;
  234. release_mem_region(address, MMIO_REGION_LENGTH);
  235. return NULL;
  236. }
  237. static void __init iommu_unmap_mmio_space(struct amd_iommu *iommu)
  238. {
  239. if (iommu->mmio_base)
  240. iounmap(iommu->mmio_base);
  241. release_mem_region(iommu->mmio_phys, MMIO_REGION_LENGTH);
  242. }
  243. /****************************************************************************
  244. *
  245. * The functions below belong to the first pass of AMD IOMMU ACPI table
  246. * parsing. In this pass we try to find out the highest device id this
  247. * code has to handle. Upon this information the size of the shared data
  248. * structures is determined later.
  249. *
  250. ****************************************************************************/
  251. /*
  252. * This function calculates the length of a given IVHD entry
  253. */
  254. static inline int ivhd_entry_length(u8 *ivhd)
  255. {
  256. return 0x04 << (*ivhd >> 6);
  257. }
  258. /*
  259. * This function reads the last device id the IOMMU has to handle from the PCI
  260. * capability header for this IOMMU
  261. */
  262. static int __init find_last_devid_on_pci(int bus, int dev, int fn, int cap_ptr)
  263. {
  264. u32 cap;
  265. cap = read_pci_config(bus, dev, fn, cap_ptr+MMIO_RANGE_OFFSET);
  266. update_last_devid(calc_devid(MMIO_GET_BUS(cap), MMIO_GET_LD(cap)));
  267. return 0;
  268. }
  269. /*
  270. * After reading the highest device id from the IOMMU PCI capability header
  271. * this function looks if there is a higher device id defined in the ACPI table
  272. */
  273. static int __init find_last_devid_from_ivhd(struct ivhd_header *h)
  274. {
  275. u8 *p = (void *)h, *end = (void *)h;
  276. struct ivhd_entry *dev;
  277. p += sizeof(*h);
  278. end += h->length;
  279. find_last_devid_on_pci(PCI_BUS(h->devid),
  280. PCI_SLOT(h->devid),
  281. PCI_FUNC(h->devid),
  282. h->cap_ptr);
  283. while (p < end) {
  284. dev = (struct ivhd_entry *)p;
  285. switch (dev->type) {
  286. case IVHD_DEV_SELECT:
  287. case IVHD_DEV_RANGE_END:
  288. case IVHD_DEV_ALIAS:
  289. case IVHD_DEV_EXT_SELECT:
  290. /* all the above subfield types refer to device ids */
  291. update_last_devid(dev->devid);
  292. break;
  293. default:
  294. break;
  295. }
  296. p += ivhd_entry_length(p);
  297. }
  298. WARN_ON(p != end);
  299. return 0;
  300. }
  301. /*
  302. * Iterate over all IVHD entries in the ACPI table and find the highest device
  303. * id which we need to handle. This is the first of three functions which parse
  304. * the ACPI table. So we check the checksum here.
  305. */
  306. static int __init find_last_devid_acpi(struct acpi_table_header *table)
  307. {
  308. int i;
  309. u8 checksum = 0, *p = (u8 *)table, *end = (u8 *)table;
  310. struct ivhd_header *h;
  311. /*
  312. * Validate checksum here so we don't need to do it when
  313. * we actually parse the table
  314. */
  315. for (i = 0; i < table->length; ++i)
  316. checksum += p[i];
  317. if (checksum != 0)
  318. /* ACPI table corrupt */
  319. return -ENODEV;
  320. p += IVRS_HEADER_LENGTH;
  321. end += table->length;
  322. while (p < end) {
  323. h = (struct ivhd_header *)p;
  324. switch (h->type) {
  325. case ACPI_IVHD_TYPE:
  326. find_last_devid_from_ivhd(h);
  327. break;
  328. default:
  329. break;
  330. }
  331. p += h->length;
  332. }
  333. WARN_ON(p != end);
  334. return 0;
  335. }
  336. /****************************************************************************
  337. *
  338. * The following functions belong the the code path which parses the ACPI table
  339. * the second time. In this ACPI parsing iteration we allocate IOMMU specific
  340. * data structures, initialize the device/alias/rlookup table and also
  341. * basically initialize the hardware.
  342. *
  343. ****************************************************************************/
  344. /*
  345. * Allocates the command buffer. This buffer is per AMD IOMMU. We can
  346. * write commands to that buffer later and the IOMMU will execute them
  347. * asynchronously
  348. */
  349. static u8 * __init alloc_command_buffer(struct amd_iommu *iommu)
  350. {
  351. u8 *cmd_buf = (u8 *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
  352. get_order(CMD_BUFFER_SIZE));
  353. u64 entry;
  354. if (cmd_buf == NULL)
  355. return NULL;
  356. iommu->cmd_buf_size = CMD_BUFFER_SIZE;
  357. entry = (u64)virt_to_phys(cmd_buf);
  358. entry |= MMIO_CMD_SIZE_512;
  359. memcpy_toio(iommu->mmio_base + MMIO_CMD_BUF_OFFSET,
  360. &entry, sizeof(entry));
  361. /* set head and tail to zero manually */
  362. writel(0x00, iommu->mmio_base + MMIO_CMD_HEAD_OFFSET);
  363. writel(0x00, iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
  364. iommu_feature_enable(iommu, CONTROL_CMDBUF_EN);
  365. return cmd_buf;
  366. }
  367. static void __init free_command_buffer(struct amd_iommu *iommu)
  368. {
  369. free_pages((unsigned long)iommu->cmd_buf,
  370. get_order(iommu->cmd_buf_size));
  371. }
  372. /* allocates the memory where the IOMMU will log its events to */
  373. static u8 * __init alloc_event_buffer(struct amd_iommu *iommu)
  374. {
  375. u64 entry;
  376. iommu->evt_buf = (u8 *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
  377. get_order(EVT_BUFFER_SIZE));
  378. if (iommu->evt_buf == NULL)
  379. return NULL;
  380. entry = (u64)virt_to_phys(iommu->evt_buf) | EVT_LEN_MASK;
  381. memcpy_toio(iommu->mmio_base + MMIO_EVT_BUF_OFFSET,
  382. &entry, sizeof(entry));
  383. iommu->evt_buf_size = EVT_BUFFER_SIZE;
  384. return iommu->evt_buf;
  385. }
  386. static void __init free_event_buffer(struct amd_iommu *iommu)
  387. {
  388. free_pages((unsigned long)iommu->evt_buf, get_order(EVT_BUFFER_SIZE));
  389. }
  390. /* sets a specific bit in the device table entry. */
  391. static void set_dev_entry_bit(u16 devid, u8 bit)
  392. {
  393. int i = (bit >> 5) & 0x07;
  394. int _bit = bit & 0x1f;
  395. amd_iommu_dev_table[devid].data[i] |= (1 << _bit);
  396. }
  397. /* Writes the specific IOMMU for a device into the rlookup table */
  398. static void __init set_iommu_for_device(struct amd_iommu *iommu, u16 devid)
  399. {
  400. amd_iommu_rlookup_table[devid] = iommu;
  401. }
  402. /*
  403. * This function takes the device specific flags read from the ACPI
  404. * table and sets up the device table entry with that information
  405. */
  406. static void __init set_dev_entry_from_acpi(struct amd_iommu *iommu,
  407. u16 devid, u32 flags, u32 ext_flags)
  408. {
  409. if (flags & ACPI_DEVFLAG_INITPASS)
  410. set_dev_entry_bit(devid, DEV_ENTRY_INIT_PASS);
  411. if (flags & ACPI_DEVFLAG_EXTINT)
  412. set_dev_entry_bit(devid, DEV_ENTRY_EINT_PASS);
  413. if (flags & ACPI_DEVFLAG_NMI)
  414. set_dev_entry_bit(devid, DEV_ENTRY_NMI_PASS);
  415. if (flags & ACPI_DEVFLAG_SYSMGT1)
  416. set_dev_entry_bit(devid, DEV_ENTRY_SYSMGT1);
  417. if (flags & ACPI_DEVFLAG_SYSMGT2)
  418. set_dev_entry_bit(devid, DEV_ENTRY_SYSMGT2);
  419. if (flags & ACPI_DEVFLAG_LINT0)
  420. set_dev_entry_bit(devid, DEV_ENTRY_LINT0_PASS);
  421. if (flags & ACPI_DEVFLAG_LINT1)
  422. set_dev_entry_bit(devid, DEV_ENTRY_LINT1_PASS);
  423. set_iommu_for_device(iommu, devid);
  424. }
  425. /*
  426. * Reads the device exclusion range from ACPI and initialize IOMMU with
  427. * it
  428. */
  429. static void __init set_device_exclusion_range(u16 devid, struct ivmd_header *m)
  430. {
  431. struct amd_iommu *iommu = amd_iommu_rlookup_table[devid];
  432. if (!(m->flags & IVMD_FLAG_EXCL_RANGE))
  433. return;
  434. if (iommu) {
  435. /*
  436. * We only can configure exclusion ranges per IOMMU, not
  437. * per device. But we can enable the exclusion range per
  438. * device. This is done here
  439. */
  440. set_dev_entry_bit(m->devid, DEV_ENTRY_EX);
  441. iommu->exclusion_start = m->range_start;
  442. iommu->exclusion_length = m->range_length;
  443. }
  444. }
  445. /*
  446. * This function reads some important data from the IOMMU PCI space and
  447. * initializes the driver data structure with it. It reads the hardware
  448. * capabilities and the first/last device entries
  449. */
  450. static void __init init_iommu_from_pci(struct amd_iommu *iommu)
  451. {
  452. int cap_ptr = iommu->cap_ptr;
  453. u32 range, misc;
  454. pci_read_config_dword(iommu->dev, cap_ptr + MMIO_CAP_HDR_OFFSET,
  455. &iommu->cap);
  456. pci_read_config_dword(iommu->dev, cap_ptr + MMIO_RANGE_OFFSET,
  457. &range);
  458. pci_read_config_dword(iommu->dev, cap_ptr + MMIO_MISC_OFFSET,
  459. &misc);
  460. iommu->first_device = calc_devid(MMIO_GET_BUS(range),
  461. MMIO_GET_FD(range));
  462. iommu->last_device = calc_devid(MMIO_GET_BUS(range),
  463. MMIO_GET_LD(range));
  464. iommu->evt_msi_num = MMIO_MSI_NUM(misc);
  465. }
  466. /*
  467. * Takes a pointer to an AMD IOMMU entry in the ACPI table and
  468. * initializes the hardware and our data structures with it.
  469. */
  470. static void __init init_iommu_from_acpi(struct amd_iommu *iommu,
  471. struct ivhd_header *h)
  472. {
  473. u8 *p = (u8 *)h;
  474. u8 *end = p, flags = 0;
  475. u16 dev_i, devid = 0, devid_start = 0, devid_to = 0;
  476. u32 ext_flags = 0;
  477. bool alias = false;
  478. struct ivhd_entry *e;
  479. /*
  480. * First set the recommended feature enable bits from ACPI
  481. * into the IOMMU control registers
  482. */
  483. h->flags & IVHD_FLAG_HT_TUN_EN_MASK ?
  484. iommu_feature_enable(iommu, CONTROL_HT_TUN_EN) :
  485. iommu_feature_disable(iommu, CONTROL_HT_TUN_EN);
  486. h->flags & IVHD_FLAG_PASSPW_EN_MASK ?
  487. iommu_feature_enable(iommu, CONTROL_PASSPW_EN) :
  488. iommu_feature_disable(iommu, CONTROL_PASSPW_EN);
  489. h->flags & IVHD_FLAG_RESPASSPW_EN_MASK ?
  490. iommu_feature_enable(iommu, CONTROL_RESPASSPW_EN) :
  491. iommu_feature_disable(iommu, CONTROL_RESPASSPW_EN);
  492. h->flags & IVHD_FLAG_ISOC_EN_MASK ?
  493. iommu_feature_enable(iommu, CONTROL_ISOC_EN) :
  494. iommu_feature_disable(iommu, CONTROL_ISOC_EN);
  495. /*
  496. * make IOMMU memory accesses cache coherent
  497. */
  498. iommu_feature_enable(iommu, CONTROL_COHERENT_EN);
  499. /*
  500. * Done. Now parse the device entries
  501. */
  502. p += sizeof(struct ivhd_header);
  503. end += h->length;
  504. while (p < end) {
  505. e = (struct ivhd_entry *)p;
  506. switch (e->type) {
  507. case IVHD_DEV_ALL:
  508. for (dev_i = iommu->first_device;
  509. dev_i <= iommu->last_device; ++dev_i)
  510. set_dev_entry_from_acpi(iommu, dev_i,
  511. e->flags, 0);
  512. break;
  513. case IVHD_DEV_SELECT:
  514. devid = e->devid;
  515. set_dev_entry_from_acpi(iommu, devid, e->flags, 0);
  516. break;
  517. case IVHD_DEV_SELECT_RANGE_START:
  518. devid_start = e->devid;
  519. flags = e->flags;
  520. ext_flags = 0;
  521. alias = false;
  522. break;
  523. case IVHD_DEV_ALIAS:
  524. devid = e->devid;
  525. devid_to = e->ext >> 8;
  526. set_dev_entry_from_acpi(iommu, devid, e->flags, 0);
  527. amd_iommu_alias_table[devid] = devid_to;
  528. break;
  529. case IVHD_DEV_ALIAS_RANGE:
  530. devid_start = e->devid;
  531. flags = e->flags;
  532. devid_to = e->ext >> 8;
  533. ext_flags = 0;
  534. alias = true;
  535. break;
  536. case IVHD_DEV_EXT_SELECT:
  537. devid = e->devid;
  538. set_dev_entry_from_acpi(iommu, devid, e->flags,
  539. e->ext);
  540. break;
  541. case IVHD_DEV_EXT_SELECT_RANGE:
  542. devid_start = e->devid;
  543. flags = e->flags;
  544. ext_flags = e->ext;
  545. alias = false;
  546. break;
  547. case IVHD_DEV_RANGE_END:
  548. devid = e->devid;
  549. for (dev_i = devid_start; dev_i <= devid; ++dev_i) {
  550. if (alias)
  551. amd_iommu_alias_table[dev_i] = devid_to;
  552. set_dev_entry_from_acpi(iommu,
  553. amd_iommu_alias_table[dev_i],
  554. flags, ext_flags);
  555. }
  556. break;
  557. default:
  558. break;
  559. }
  560. p += ivhd_entry_length(p);
  561. }
  562. }
  563. /* Initializes the device->iommu mapping for the driver */
  564. static int __init init_iommu_devices(struct amd_iommu *iommu)
  565. {
  566. u16 i;
  567. for (i = iommu->first_device; i <= iommu->last_device; ++i)
  568. set_iommu_for_device(iommu, i);
  569. return 0;
  570. }
  571. static void __init free_iommu_one(struct amd_iommu *iommu)
  572. {
  573. free_command_buffer(iommu);
  574. free_event_buffer(iommu);
  575. iommu_unmap_mmio_space(iommu);
  576. }
  577. static void __init free_iommu_all(void)
  578. {
  579. struct amd_iommu *iommu, *next;
  580. list_for_each_entry_safe(iommu, next, &amd_iommu_list, list) {
  581. list_del(&iommu->list);
  582. free_iommu_one(iommu);
  583. kfree(iommu);
  584. }
  585. }
  586. /*
  587. * This function clues the initialization function for one IOMMU
  588. * together and also allocates the command buffer and programs the
  589. * hardware. It does NOT enable the IOMMU. This is done afterwards.
  590. */
  591. static int __init init_iommu_one(struct amd_iommu *iommu, struct ivhd_header *h)
  592. {
  593. spin_lock_init(&iommu->lock);
  594. list_add_tail(&iommu->list, &amd_iommu_list);
  595. /*
  596. * Copy data from ACPI table entry to the iommu struct
  597. */
  598. iommu->dev = pci_get_bus_and_slot(PCI_BUS(h->devid), h->devid & 0xff);
  599. if (!iommu->dev)
  600. return 1;
  601. iommu->cap_ptr = h->cap_ptr;
  602. iommu->pci_seg = h->pci_seg;
  603. iommu->mmio_phys = h->mmio_phys;
  604. iommu->mmio_base = iommu_map_mmio_space(h->mmio_phys);
  605. if (!iommu->mmio_base)
  606. return -ENOMEM;
  607. iommu_set_device_table(iommu);
  608. iommu->cmd_buf = alloc_command_buffer(iommu);
  609. if (!iommu->cmd_buf)
  610. return -ENOMEM;
  611. iommu->evt_buf = alloc_event_buffer(iommu);
  612. if (!iommu->evt_buf)
  613. return -ENOMEM;
  614. iommu->int_enabled = false;
  615. init_iommu_from_pci(iommu);
  616. init_iommu_from_acpi(iommu, h);
  617. init_iommu_devices(iommu);
  618. return pci_enable_device(iommu->dev);
  619. }
  620. /*
  621. * Iterates over all IOMMU entries in the ACPI table, allocates the
  622. * IOMMU structure and initializes it with init_iommu_one()
  623. */
  624. static int __init init_iommu_all(struct acpi_table_header *table)
  625. {
  626. u8 *p = (u8 *)table, *end = (u8 *)table;
  627. struct ivhd_header *h;
  628. struct amd_iommu *iommu;
  629. int ret;
  630. end += table->length;
  631. p += IVRS_HEADER_LENGTH;
  632. while (p < end) {
  633. h = (struct ivhd_header *)p;
  634. switch (*p) {
  635. case ACPI_IVHD_TYPE:
  636. iommu = kzalloc(sizeof(struct amd_iommu), GFP_KERNEL);
  637. if (iommu == NULL)
  638. return -ENOMEM;
  639. ret = init_iommu_one(iommu, h);
  640. if (ret)
  641. return ret;
  642. break;
  643. default:
  644. break;
  645. }
  646. p += h->length;
  647. }
  648. WARN_ON(p != end);
  649. return 0;
  650. }
  651. /****************************************************************************
  652. *
  653. * The following functions initialize the MSI interrupts for all IOMMUs
  654. * in the system. Its a bit challenging because there could be multiple
  655. * IOMMUs per PCI BDF but we can call pci_enable_msi(x) only once per
  656. * pci_dev.
  657. *
  658. ****************************************************************************/
  659. static int __init iommu_setup_msix(struct amd_iommu *iommu)
  660. {
  661. struct amd_iommu *curr;
  662. struct msix_entry entries[32]; /* only 32 supported by AMD IOMMU */
  663. int nvec = 0, i;
  664. list_for_each_entry(curr, &amd_iommu_list, list) {
  665. if (curr->dev == iommu->dev) {
  666. entries[nvec].entry = curr->evt_msi_num;
  667. entries[nvec].vector = 0;
  668. curr->int_enabled = true;
  669. nvec++;
  670. }
  671. }
  672. if (pci_enable_msix(iommu->dev, entries, nvec)) {
  673. pci_disable_msix(iommu->dev);
  674. return 1;
  675. }
  676. for (i = 0; i < nvec; ++i) {
  677. int r = request_irq(entries->vector, amd_iommu_int_handler,
  678. IRQF_SAMPLE_RANDOM,
  679. "AMD IOMMU",
  680. NULL);
  681. if (r)
  682. goto out_free;
  683. }
  684. return 0;
  685. out_free:
  686. for (i -= 1; i >= 0; --i)
  687. free_irq(entries->vector, NULL);
  688. pci_disable_msix(iommu->dev);
  689. return 1;
  690. }
  691. static int __init iommu_setup_msi(struct amd_iommu *iommu)
  692. {
  693. int r;
  694. struct amd_iommu *curr;
  695. list_for_each_entry(curr, &amd_iommu_list, list) {
  696. if (curr->dev == iommu->dev)
  697. curr->int_enabled = true;
  698. }
  699. if (pci_enable_msi(iommu->dev))
  700. return 1;
  701. r = request_irq(iommu->dev->irq, amd_iommu_int_handler,
  702. IRQF_SAMPLE_RANDOM,
  703. "AMD IOMMU",
  704. NULL);
  705. if (r) {
  706. pci_disable_msi(iommu->dev);
  707. return 1;
  708. }
  709. return 0;
  710. }
  711. static int __init iommu_init_msi(struct amd_iommu *iommu)
  712. {
  713. if (iommu->int_enabled)
  714. return 0;
  715. if (pci_find_capability(iommu->dev, PCI_CAP_ID_MSIX))
  716. return iommu_setup_msix(iommu);
  717. else if (pci_find_capability(iommu->dev, PCI_CAP_ID_MSI))
  718. return iommu_setup_msi(iommu);
  719. return 1;
  720. }
  721. /****************************************************************************
  722. *
  723. * The next functions belong to the third pass of parsing the ACPI
  724. * table. In this last pass the memory mapping requirements are
  725. * gathered (like exclusion and unity mapping reanges).
  726. *
  727. ****************************************************************************/
  728. static void __init free_unity_maps(void)
  729. {
  730. struct unity_map_entry *entry, *next;
  731. list_for_each_entry_safe(entry, next, &amd_iommu_unity_map, list) {
  732. list_del(&entry->list);
  733. kfree(entry);
  734. }
  735. }
  736. /* called when we find an exclusion range definition in ACPI */
  737. static int __init init_exclusion_range(struct ivmd_header *m)
  738. {
  739. int i;
  740. switch (m->type) {
  741. case ACPI_IVMD_TYPE:
  742. set_device_exclusion_range(m->devid, m);
  743. break;
  744. case ACPI_IVMD_TYPE_ALL:
  745. for (i = 0; i <= amd_iommu_last_bdf; ++i)
  746. set_device_exclusion_range(i, m);
  747. break;
  748. case ACPI_IVMD_TYPE_RANGE:
  749. for (i = m->devid; i <= m->aux; ++i)
  750. set_device_exclusion_range(i, m);
  751. break;
  752. default:
  753. break;
  754. }
  755. return 0;
  756. }
  757. /* called for unity map ACPI definition */
  758. static int __init init_unity_map_range(struct ivmd_header *m)
  759. {
  760. struct unity_map_entry *e = 0;
  761. e = kzalloc(sizeof(*e), GFP_KERNEL);
  762. if (e == NULL)
  763. return -ENOMEM;
  764. switch (m->type) {
  765. default:
  766. case ACPI_IVMD_TYPE:
  767. e->devid_start = e->devid_end = m->devid;
  768. break;
  769. case ACPI_IVMD_TYPE_ALL:
  770. e->devid_start = 0;
  771. e->devid_end = amd_iommu_last_bdf;
  772. break;
  773. case ACPI_IVMD_TYPE_RANGE:
  774. e->devid_start = m->devid;
  775. e->devid_end = m->aux;
  776. break;
  777. }
  778. e->address_start = PAGE_ALIGN(m->range_start);
  779. e->address_end = e->address_start + PAGE_ALIGN(m->range_length);
  780. e->prot = m->flags >> 1;
  781. list_add_tail(&e->list, &amd_iommu_unity_map);
  782. return 0;
  783. }
  784. /* iterates over all memory definitions we find in the ACPI table */
  785. static int __init init_memory_definitions(struct acpi_table_header *table)
  786. {
  787. u8 *p = (u8 *)table, *end = (u8 *)table;
  788. struct ivmd_header *m;
  789. end += table->length;
  790. p += IVRS_HEADER_LENGTH;
  791. while (p < end) {
  792. m = (struct ivmd_header *)p;
  793. if (m->flags & IVMD_FLAG_EXCL_RANGE)
  794. init_exclusion_range(m);
  795. else if (m->flags & IVMD_FLAG_UNITY_MAP)
  796. init_unity_map_range(m);
  797. p += m->length;
  798. }
  799. return 0;
  800. }
  801. /*
  802. * Init the device table to not allow DMA access for devices and
  803. * suppress all page faults
  804. */
  805. static void init_device_table(void)
  806. {
  807. u16 devid;
  808. for (devid = 0; devid <= amd_iommu_last_bdf; ++devid) {
  809. set_dev_entry_bit(devid, DEV_ENTRY_VALID);
  810. set_dev_entry_bit(devid, DEV_ENTRY_TRANSLATION);
  811. }
  812. }
  813. /*
  814. * This function finally enables all IOMMUs found in the system after
  815. * they have been initialized
  816. */
  817. static void __init enable_iommus(void)
  818. {
  819. struct amd_iommu *iommu;
  820. list_for_each_entry(iommu, &amd_iommu_list, list) {
  821. iommu_set_exclusion_range(iommu);
  822. iommu_init_msi(iommu);
  823. iommu_enable_event_logging(iommu);
  824. iommu_enable(iommu);
  825. }
  826. }
  827. /*
  828. * Suspend/Resume support
  829. * disable suspend until real resume implemented
  830. */
  831. static int amd_iommu_resume(struct sys_device *dev)
  832. {
  833. return 0;
  834. }
  835. static int amd_iommu_suspend(struct sys_device *dev, pm_message_t state)
  836. {
  837. return -EINVAL;
  838. }
  839. static struct sysdev_class amd_iommu_sysdev_class = {
  840. .name = "amd_iommu",
  841. .suspend = amd_iommu_suspend,
  842. .resume = amd_iommu_resume,
  843. };
  844. static struct sys_device device_amd_iommu = {
  845. .id = 0,
  846. .cls = &amd_iommu_sysdev_class,
  847. };
  848. /*
  849. * This is the core init function for AMD IOMMU hardware in the system.
  850. * This function is called from the generic x86 DMA layer initialization
  851. * code.
  852. *
  853. * This function basically parses the ACPI table for AMD IOMMU (IVRS)
  854. * three times:
  855. *
  856. * 1 pass) Find the highest PCI device id the driver has to handle.
  857. * Upon this information the size of the data structures is
  858. * determined that needs to be allocated.
  859. *
  860. * 2 pass) Initialize the data structures just allocated with the
  861. * information in the ACPI table about available AMD IOMMUs
  862. * in the system. It also maps the PCI devices in the
  863. * system to specific IOMMUs
  864. *
  865. * 3 pass) After the basic data structures are allocated and
  866. * initialized we update them with information about memory
  867. * remapping requirements parsed out of the ACPI table in
  868. * this last pass.
  869. *
  870. * After that the hardware is initialized and ready to go. In the last
  871. * step we do some Linux specific things like registering the driver in
  872. * the dma_ops interface and initializing the suspend/resume support
  873. * functions. Finally it prints some information about AMD IOMMUs and
  874. * the driver state and enables the hardware.
  875. */
  876. int __init amd_iommu_init(void)
  877. {
  878. int i, ret = 0;
  879. if (no_iommu) {
  880. printk(KERN_INFO "AMD IOMMU disabled by kernel command line\n");
  881. return 0;
  882. }
  883. if (!amd_iommu_detected)
  884. return -ENODEV;
  885. /*
  886. * First parse ACPI tables to find the largest Bus/Dev/Func
  887. * we need to handle. Upon this information the shared data
  888. * structures for the IOMMUs in the system will be allocated
  889. */
  890. if (acpi_table_parse("IVRS", find_last_devid_acpi) != 0)
  891. return -ENODEV;
  892. dev_table_size = tbl_size(DEV_TABLE_ENTRY_SIZE);
  893. alias_table_size = tbl_size(ALIAS_TABLE_ENTRY_SIZE);
  894. rlookup_table_size = tbl_size(RLOOKUP_TABLE_ENTRY_SIZE);
  895. ret = -ENOMEM;
  896. /* Device table - directly used by all IOMMUs */
  897. amd_iommu_dev_table = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
  898. get_order(dev_table_size));
  899. if (amd_iommu_dev_table == NULL)
  900. goto out;
  901. /*
  902. * Alias table - map PCI Bus/Dev/Func to Bus/Dev/Func the
  903. * IOMMU see for that device
  904. */
  905. amd_iommu_alias_table = (void *)__get_free_pages(GFP_KERNEL,
  906. get_order(alias_table_size));
  907. if (amd_iommu_alias_table == NULL)
  908. goto free;
  909. /* IOMMU rlookup table - find the IOMMU for a specific device */
  910. amd_iommu_rlookup_table = (void *)__get_free_pages(
  911. GFP_KERNEL | __GFP_ZERO,
  912. get_order(rlookup_table_size));
  913. if (amd_iommu_rlookup_table == NULL)
  914. goto free;
  915. /*
  916. * Protection Domain table - maps devices to protection domains
  917. * This table has the same size as the rlookup_table
  918. */
  919. amd_iommu_pd_table = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
  920. get_order(rlookup_table_size));
  921. if (amd_iommu_pd_table == NULL)
  922. goto free;
  923. amd_iommu_pd_alloc_bitmap = (void *)__get_free_pages(
  924. GFP_KERNEL | __GFP_ZERO,
  925. get_order(MAX_DOMAIN_ID/8));
  926. if (amd_iommu_pd_alloc_bitmap == NULL)
  927. goto free;
  928. /* init the device table */
  929. init_device_table();
  930. /*
  931. * let all alias entries point to itself
  932. */
  933. for (i = 0; i <= amd_iommu_last_bdf; ++i)
  934. amd_iommu_alias_table[i] = i;
  935. /*
  936. * never allocate domain 0 because its used as the non-allocated and
  937. * error value placeholder
  938. */
  939. amd_iommu_pd_alloc_bitmap[0] = 1;
  940. /*
  941. * now the data structures are allocated and basically initialized
  942. * start the real acpi table scan
  943. */
  944. ret = -ENODEV;
  945. if (acpi_table_parse("IVRS", init_iommu_all) != 0)
  946. goto free;
  947. if (acpi_table_parse("IVRS", init_memory_definitions) != 0)
  948. goto free;
  949. ret = sysdev_class_register(&amd_iommu_sysdev_class);
  950. if (ret)
  951. goto free;
  952. ret = sysdev_register(&device_amd_iommu);
  953. if (ret)
  954. goto free;
  955. ret = amd_iommu_init_dma_ops();
  956. if (ret)
  957. goto free;
  958. enable_iommus();
  959. printk(KERN_INFO "AMD IOMMU: device isolation ");
  960. if (amd_iommu_isolate)
  961. printk("enabled\n");
  962. else
  963. printk("disabled\n");
  964. if (amd_iommu_unmap_flush)
  965. printk(KERN_INFO "AMD IOMMU: IO/TLB flush on unmap enabled\n");
  966. else
  967. printk(KERN_INFO "AMD IOMMU: Lazy IO/TLB flushing enabled\n");
  968. out:
  969. return ret;
  970. free:
  971. free_pages((unsigned long)amd_iommu_pd_alloc_bitmap,
  972. get_order(MAX_DOMAIN_ID/8));
  973. free_pages((unsigned long)amd_iommu_pd_table,
  974. get_order(rlookup_table_size));
  975. free_pages((unsigned long)amd_iommu_rlookup_table,
  976. get_order(rlookup_table_size));
  977. free_pages((unsigned long)amd_iommu_alias_table,
  978. get_order(alias_table_size));
  979. free_pages((unsigned long)amd_iommu_dev_table,
  980. get_order(dev_table_size));
  981. free_iommu_all();
  982. free_unity_maps();
  983. goto out;
  984. }
  985. /****************************************************************************
  986. *
  987. * Early detect code. This code runs at IOMMU detection time in the DMA
  988. * layer. It just looks if there is an IVRS ACPI table to detect AMD
  989. * IOMMUs
  990. *
  991. ****************************************************************************/
  992. static int __init early_amd_iommu_detect(struct acpi_table_header *table)
  993. {
  994. return 0;
  995. }
  996. void __init amd_iommu_detect(void)
  997. {
  998. if (swiotlb || no_iommu || (iommu_detected && !gart_iommu_aperture))
  999. return;
  1000. if (acpi_table_parse("IVRS", early_amd_iommu_detect) == 0) {
  1001. iommu_detected = 1;
  1002. amd_iommu_detected = 1;
  1003. #ifdef CONFIG_GART_IOMMU
  1004. gart_iommu_aperture_disabled = 1;
  1005. gart_iommu_aperture = 0;
  1006. #endif
  1007. }
  1008. }
  1009. /****************************************************************************
  1010. *
  1011. * Parsing functions for the AMD IOMMU specific kernel command line
  1012. * options.
  1013. *
  1014. ****************************************************************************/
  1015. static int __init parse_amd_iommu_options(char *str)
  1016. {
  1017. for (; *str; ++str) {
  1018. if (strncmp(str, "isolate", 7) == 0)
  1019. amd_iommu_isolate = true;
  1020. if (strncmp(str, "share", 5) == 0)
  1021. amd_iommu_isolate = false;
  1022. if (strncmp(str, "fullflush", 9) == 0)
  1023. amd_iommu_unmap_flush = true;
  1024. }
  1025. return 1;
  1026. }
  1027. __setup("amd_iommu=", parse_amd_iommu_options);