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