amd_iommu_init.c 32 KB

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