amd_iommu_init.c 31 KB

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