amd_iommu_init.c 31 KB

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