amd_iommu_init.c 31 KB

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