amd_iommu_init.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826
  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 <asm/pci-direct.h>
  24. #include <asm/amd_iommu_types.h>
  25. #include <asm/amd_iommu.h>
  26. #include <asm/gart.h>
  27. /*
  28. * definitions for the ACPI scanning code
  29. */
  30. #define UPDATE_LAST_BDF(x) do {\
  31. if ((x) > amd_iommu_last_bdf) \
  32. amd_iommu_last_bdf = (x); \
  33. } while (0);
  34. #define DEVID(bus, devfn) (((bus) << 8) | (devfn))
  35. #define PCI_BUS(x) (((x) >> 8) & 0xff)
  36. #define IVRS_HEADER_LENGTH 48
  37. #define TBL_SIZE(x) (1 << (PAGE_SHIFT + get_order(amd_iommu_last_bdf * (x))))
  38. #define ACPI_IVHD_TYPE 0x10
  39. #define ACPI_IVMD_TYPE_ALL 0x20
  40. #define ACPI_IVMD_TYPE 0x21
  41. #define ACPI_IVMD_TYPE_RANGE 0x22
  42. #define IVHD_DEV_ALL 0x01
  43. #define IVHD_DEV_SELECT 0x02
  44. #define IVHD_DEV_SELECT_RANGE_START 0x03
  45. #define IVHD_DEV_RANGE_END 0x04
  46. #define IVHD_DEV_ALIAS 0x42
  47. #define IVHD_DEV_ALIAS_RANGE 0x43
  48. #define IVHD_DEV_EXT_SELECT 0x46
  49. #define IVHD_DEV_EXT_SELECT_RANGE 0x47
  50. #define IVHD_FLAG_HT_TUN_EN 0x00
  51. #define IVHD_FLAG_PASSPW_EN 0x01
  52. #define IVHD_FLAG_RESPASSPW_EN 0x02
  53. #define IVHD_FLAG_ISOC_EN 0x03
  54. #define IVMD_FLAG_EXCL_RANGE 0x08
  55. #define IVMD_FLAG_UNITY_MAP 0x01
  56. #define ACPI_DEVFLAG_INITPASS 0x01
  57. #define ACPI_DEVFLAG_EXTINT 0x02
  58. #define ACPI_DEVFLAG_NMI 0x04
  59. #define ACPI_DEVFLAG_SYSMGT1 0x10
  60. #define ACPI_DEVFLAG_SYSMGT2 0x20
  61. #define ACPI_DEVFLAG_LINT0 0x40
  62. #define ACPI_DEVFLAG_LINT1 0x80
  63. #define ACPI_DEVFLAG_ATSDIS 0x10000000
  64. struct ivhd_header {
  65. u8 type;
  66. u8 flags;
  67. u16 length;
  68. u16 devid;
  69. u16 cap_ptr;
  70. u64 mmio_phys;
  71. u16 pci_seg;
  72. u16 info;
  73. u32 reserved;
  74. } __attribute__((packed));
  75. struct ivhd_entry {
  76. u8 type;
  77. u16 devid;
  78. u8 flags;
  79. u32 ext;
  80. } __attribute__((packed));
  81. struct ivmd_header {
  82. u8 type;
  83. u8 flags;
  84. u16 length;
  85. u16 devid;
  86. u16 aux;
  87. u64 resv;
  88. u64 range_start;
  89. u64 range_length;
  90. } __attribute__((packed));
  91. static int __initdata amd_iommu_disable;
  92. u16 amd_iommu_last_bdf;
  93. struct list_head amd_iommu_unity_map;
  94. unsigned amd_iommu_aperture_order = 26;
  95. int amd_iommu_isolate;
  96. struct list_head amd_iommu_list;
  97. struct dev_table_entry *amd_iommu_dev_table;
  98. u16 *amd_iommu_alias_table;
  99. struct amd_iommu **amd_iommu_rlookup_table;
  100. struct protection_domain **amd_iommu_pd_table;
  101. unsigned long *amd_iommu_pd_alloc_bitmap;
  102. static u32 dev_table_size;
  103. static u32 alias_table_size;
  104. static u32 rlookup_table_size;
  105. static void __init iommu_set_exclusion_range(struct amd_iommu *iommu)
  106. {
  107. u64 start = iommu->exclusion_start & PAGE_MASK;
  108. u64 limit = (start + iommu->exclusion_length) & PAGE_MASK;
  109. u64 entry;
  110. if (!iommu->exclusion_start)
  111. return;
  112. entry = start | MMIO_EXCL_ENABLE_MASK;
  113. memcpy_toio(iommu->mmio_base + MMIO_EXCL_BASE_OFFSET,
  114. &entry, sizeof(entry));
  115. entry = limit;
  116. memcpy_toio(iommu->mmio_base + MMIO_EXCL_LIMIT_OFFSET,
  117. &entry, sizeof(entry));
  118. }
  119. static void __init iommu_set_device_table(struct amd_iommu *iommu)
  120. {
  121. u32 entry;
  122. BUG_ON(iommu->mmio_base == NULL);
  123. entry = virt_to_phys(amd_iommu_dev_table);
  124. entry |= (dev_table_size >> 12) - 1;
  125. memcpy_toio(iommu->mmio_base + MMIO_DEV_TABLE_OFFSET,
  126. &entry, sizeof(entry));
  127. }
  128. static void __init iommu_feature_enable(struct amd_iommu *iommu, u8 bit)
  129. {
  130. u32 ctrl;
  131. ctrl = readl(iommu->mmio_base + MMIO_CONTROL_OFFSET);
  132. ctrl |= (1 << bit);
  133. writel(ctrl, iommu->mmio_base + MMIO_CONTROL_OFFSET);
  134. }
  135. static void __init iommu_feature_disable(struct amd_iommu *iommu, u8 bit)
  136. {
  137. u32 ctrl;
  138. ctrl = (u64)readl(iommu->mmio_base + MMIO_CONTROL_OFFSET);
  139. ctrl &= ~(1 << bit);
  140. writel(ctrl, iommu->mmio_base + MMIO_CONTROL_OFFSET);
  141. }
  142. void __init iommu_enable(struct amd_iommu *iommu)
  143. {
  144. u32 ctrl;
  145. printk(KERN_INFO "AMD IOMMU: Enabling IOMMU at ");
  146. print_devid(iommu->devid, 0);
  147. printk(" cap 0x%hx\n", iommu->cap_ptr);
  148. iommu_feature_enable(iommu, CONTROL_IOMMU_EN);
  149. ctrl = readl(iommu->mmio_base + MMIO_CONTROL_OFFSET);
  150. }
  151. static u8 * __init iommu_map_mmio_space(u64 address)
  152. {
  153. u8 *ret;
  154. if (!request_mem_region(address, MMIO_REGION_LENGTH, "amd_iommu"))
  155. return NULL;
  156. ret = ioremap_nocache(address, MMIO_REGION_LENGTH);
  157. if (ret != NULL)
  158. return ret;
  159. release_mem_region(address, MMIO_REGION_LENGTH);
  160. return NULL;
  161. }
  162. static void __init iommu_unmap_mmio_space(struct amd_iommu *iommu)
  163. {
  164. if (iommu->mmio_base)
  165. iounmap(iommu->mmio_base);
  166. release_mem_region(iommu->mmio_phys, MMIO_REGION_LENGTH);
  167. }
  168. static int __init find_last_devid_on_pci(int bus, int dev, int fn, int cap_ptr)
  169. {
  170. u32 cap;
  171. cap = read_pci_config(bus, dev, fn, cap_ptr+MMIO_RANGE_OFFSET);
  172. UPDATE_LAST_BDF(DEVID(MMIO_GET_BUS(cap), MMIO_GET_LD(cap)));
  173. return 0;
  174. }
  175. static int __init find_last_devid_from_ivhd(struct ivhd_header *h)
  176. {
  177. u8 *p = (void *)h, *end = (void *)h;
  178. struct ivhd_entry *dev;
  179. p += sizeof(*h);
  180. end += h->length;
  181. find_last_devid_on_pci(PCI_BUS(h->devid),
  182. PCI_SLOT(h->devid),
  183. PCI_FUNC(h->devid),
  184. h->cap_ptr);
  185. while (p < end) {
  186. dev = (struct ivhd_entry *)p;
  187. switch (dev->type) {
  188. case IVHD_DEV_SELECT:
  189. case IVHD_DEV_RANGE_END:
  190. case IVHD_DEV_ALIAS:
  191. case IVHD_DEV_EXT_SELECT:
  192. UPDATE_LAST_BDF(dev->devid);
  193. break;
  194. default:
  195. break;
  196. }
  197. p += 0x04 << (*p >> 6);
  198. }
  199. WARN_ON(p != end);
  200. return 0;
  201. }
  202. static int __init find_last_devid_acpi(struct acpi_table_header *table)
  203. {
  204. int i;
  205. u8 checksum = 0, *p = (u8 *)table, *end = (u8 *)table;
  206. struct ivhd_header *h;
  207. /*
  208. * Validate checksum here so we don't need to do it when
  209. * we actually parse the table
  210. */
  211. for (i = 0; i < table->length; ++i)
  212. checksum += p[i];
  213. if (checksum != 0)
  214. /* ACPI table corrupt */
  215. return -ENODEV;
  216. p += IVRS_HEADER_LENGTH;
  217. end += table->length;
  218. while (p < end) {
  219. h = (struct ivhd_header *)p;
  220. switch (h->type) {
  221. case ACPI_IVHD_TYPE:
  222. find_last_devid_from_ivhd(h);
  223. break;
  224. default:
  225. break;
  226. }
  227. p += h->length;
  228. }
  229. WARN_ON(p != end);
  230. return 0;
  231. }
  232. static u8 * __init alloc_command_buffer(struct amd_iommu *iommu)
  233. {
  234. u8 *cmd_buf = (u8 *)__get_free_pages(GFP_KERNEL,
  235. get_order(CMD_BUFFER_SIZE));
  236. u64 entry = 0;
  237. if (cmd_buf == NULL)
  238. return NULL;
  239. iommu->cmd_buf_size = CMD_BUFFER_SIZE;
  240. memset(cmd_buf, 0, CMD_BUFFER_SIZE);
  241. entry = (u64)virt_to_phys(cmd_buf);
  242. entry |= MMIO_CMD_SIZE_512;
  243. memcpy_toio(iommu->mmio_base + MMIO_CMD_BUF_OFFSET,
  244. &entry, sizeof(entry));
  245. iommu_feature_enable(iommu, CONTROL_CMDBUF_EN);
  246. return cmd_buf;
  247. }
  248. static void __init free_command_buffer(struct amd_iommu *iommu)
  249. {
  250. if (iommu->cmd_buf)
  251. free_pages((unsigned long)iommu->cmd_buf,
  252. get_order(CMD_BUFFER_SIZE));
  253. }
  254. static void set_dev_entry_bit(u16 devid, u8 bit)
  255. {
  256. int i = (bit >> 5) & 0x07;
  257. int _bit = bit & 0x1f;
  258. amd_iommu_dev_table[devid].data[i] |= (1 << _bit);
  259. }
  260. static void __init set_dev_entry_from_acpi(u16 devid, u32 flags, u32 ext_flags)
  261. {
  262. if (flags & ACPI_DEVFLAG_INITPASS)
  263. set_dev_entry_bit(devid, DEV_ENTRY_INIT_PASS);
  264. if (flags & ACPI_DEVFLAG_EXTINT)
  265. set_dev_entry_bit(devid, DEV_ENTRY_EINT_PASS);
  266. if (flags & ACPI_DEVFLAG_NMI)
  267. set_dev_entry_bit(devid, DEV_ENTRY_NMI_PASS);
  268. if (flags & ACPI_DEVFLAG_SYSMGT1)
  269. set_dev_entry_bit(devid, DEV_ENTRY_SYSMGT1);
  270. if (flags & ACPI_DEVFLAG_SYSMGT2)
  271. set_dev_entry_bit(devid, DEV_ENTRY_SYSMGT2);
  272. if (flags & ACPI_DEVFLAG_LINT0)
  273. set_dev_entry_bit(devid, DEV_ENTRY_LINT0_PASS);
  274. if (flags & ACPI_DEVFLAG_LINT1)
  275. set_dev_entry_bit(devid, DEV_ENTRY_LINT1_PASS);
  276. }
  277. static void __init set_iommu_for_device(struct amd_iommu *iommu, u16 devid)
  278. {
  279. amd_iommu_rlookup_table[devid] = iommu;
  280. }
  281. static void __init set_device_exclusion_range(u16 devid, struct ivmd_header *m)
  282. {
  283. struct amd_iommu *iommu = amd_iommu_rlookup_table[devid];
  284. if (!(m->flags & IVMD_FLAG_EXCL_RANGE))
  285. return;
  286. if (iommu) {
  287. set_dev_entry_bit(m->devid, DEV_ENTRY_EX);
  288. iommu->exclusion_start = m->range_start;
  289. iommu->exclusion_length = m->range_length;
  290. }
  291. }
  292. static void __init init_iommu_from_pci(struct amd_iommu *iommu)
  293. {
  294. int bus = PCI_BUS(iommu->devid);
  295. int dev = PCI_SLOT(iommu->devid);
  296. int fn = PCI_FUNC(iommu->devid);
  297. int cap_ptr = iommu->cap_ptr;
  298. u32 range;
  299. iommu->cap = read_pci_config(bus, dev, fn, cap_ptr+MMIO_CAP_HDR_OFFSET);
  300. range = read_pci_config(bus, dev, fn, cap_ptr+MMIO_RANGE_OFFSET);
  301. iommu->first_device = DEVID(MMIO_GET_BUS(range), MMIO_GET_FD(range));
  302. iommu->last_device = DEVID(MMIO_GET_BUS(range), MMIO_GET_LD(range));
  303. }
  304. static void __init init_iommu_from_acpi(struct amd_iommu *iommu,
  305. struct ivhd_header *h)
  306. {
  307. u8 *p = (u8 *)h;
  308. u8 *end = p, flags = 0;
  309. u16 dev_i, devid = 0, devid_start = 0, devid_to = 0;
  310. u32 ext_flags = 0;
  311. bool alias = 0;
  312. struct ivhd_entry *e;
  313. /*
  314. * First set the recommended feature enable bits from ACPI
  315. * into the IOMMU control registers
  316. */
  317. h->flags & IVHD_FLAG_HT_TUN_EN ?
  318. iommu_feature_enable(iommu, CONTROL_HT_TUN_EN) :
  319. iommu_feature_disable(iommu, CONTROL_HT_TUN_EN);
  320. h->flags & IVHD_FLAG_PASSPW_EN ?
  321. iommu_feature_enable(iommu, CONTROL_PASSPW_EN) :
  322. iommu_feature_disable(iommu, CONTROL_PASSPW_EN);
  323. h->flags & IVHD_FLAG_RESPASSPW_EN ?
  324. iommu_feature_enable(iommu, CONTROL_RESPASSPW_EN) :
  325. iommu_feature_disable(iommu, CONTROL_RESPASSPW_EN);
  326. h->flags & IVHD_FLAG_ISOC_EN ?
  327. iommu_feature_enable(iommu, CONTROL_ISOC_EN) :
  328. iommu_feature_disable(iommu, CONTROL_ISOC_EN);
  329. /*
  330. * make IOMMU memory accesses cache coherent
  331. */
  332. iommu_feature_enable(iommu, CONTROL_COHERENT_EN);
  333. /*
  334. * Done. Now parse the device entries
  335. */
  336. p += sizeof(struct ivhd_header);
  337. end += h->length;
  338. while (p < end) {
  339. e = (struct ivhd_entry *)p;
  340. switch (e->type) {
  341. case IVHD_DEV_ALL:
  342. for (dev_i = iommu->first_device;
  343. dev_i <= iommu->last_device; ++dev_i)
  344. set_dev_entry_from_acpi(dev_i, e->flags, 0);
  345. break;
  346. case IVHD_DEV_SELECT:
  347. devid = e->devid;
  348. set_dev_entry_from_acpi(devid, e->flags, 0);
  349. break;
  350. case IVHD_DEV_SELECT_RANGE_START:
  351. devid_start = e->devid;
  352. flags = e->flags;
  353. ext_flags = 0;
  354. alias = 0;
  355. break;
  356. case IVHD_DEV_ALIAS:
  357. devid = e->devid;
  358. devid_to = e->ext >> 8;
  359. set_dev_entry_from_acpi(devid, e->flags, 0);
  360. amd_iommu_alias_table[devid] = devid_to;
  361. break;
  362. case IVHD_DEV_ALIAS_RANGE:
  363. devid_start = e->devid;
  364. flags = e->flags;
  365. devid_to = e->ext >> 8;
  366. ext_flags = 0;
  367. alias = 1;
  368. break;
  369. case IVHD_DEV_EXT_SELECT:
  370. devid = e->devid;
  371. set_dev_entry_from_acpi(devid, e->flags, e->ext);
  372. break;
  373. case IVHD_DEV_EXT_SELECT_RANGE:
  374. devid_start = e->devid;
  375. flags = e->flags;
  376. ext_flags = e->ext;
  377. alias = 0;
  378. break;
  379. case IVHD_DEV_RANGE_END:
  380. devid = e->devid;
  381. for (dev_i = devid_start; dev_i <= devid; ++dev_i) {
  382. if (alias)
  383. amd_iommu_alias_table[dev_i] = devid_to;
  384. set_dev_entry_from_acpi(
  385. amd_iommu_alias_table[dev_i],
  386. flags, ext_flags);
  387. }
  388. break;
  389. default:
  390. break;
  391. }
  392. p += 0x04 << (e->type >> 6);
  393. }
  394. }
  395. static int __init init_iommu_devices(struct amd_iommu *iommu)
  396. {
  397. u16 i;
  398. for (i = iommu->first_device; i <= iommu->last_device; ++i)
  399. set_iommu_for_device(iommu, i);
  400. return 0;
  401. }
  402. static void __init free_iommu_one(struct amd_iommu *iommu)
  403. {
  404. free_command_buffer(iommu);
  405. iommu_unmap_mmio_space(iommu);
  406. }
  407. static void __init free_iommu_all(void)
  408. {
  409. struct amd_iommu *iommu, *next;
  410. list_for_each_entry_safe(iommu, next, &amd_iommu_list, list) {
  411. list_del(&iommu->list);
  412. free_iommu_one(iommu);
  413. kfree(iommu);
  414. }
  415. }
  416. static int __init init_iommu_one(struct amd_iommu *iommu, struct ivhd_header *h)
  417. {
  418. spin_lock_init(&iommu->lock);
  419. list_add_tail(&iommu->list, &amd_iommu_list);
  420. /*
  421. * Copy data from ACPI table entry to the iommu struct
  422. */
  423. iommu->devid = h->devid;
  424. iommu->cap_ptr = h->cap_ptr;
  425. iommu->mmio_phys = h->mmio_phys;
  426. iommu->mmio_base = iommu_map_mmio_space(h->mmio_phys);
  427. if (!iommu->mmio_base)
  428. return -ENOMEM;
  429. iommu_set_device_table(iommu);
  430. iommu->cmd_buf = alloc_command_buffer(iommu);
  431. if (!iommu->cmd_buf)
  432. return -ENOMEM;
  433. init_iommu_from_pci(iommu);
  434. init_iommu_from_acpi(iommu, h);
  435. init_iommu_devices(iommu);
  436. return 0;
  437. }
  438. static int __init init_iommu_all(struct acpi_table_header *table)
  439. {
  440. u8 *p = (u8 *)table, *end = (u8 *)table;
  441. struct ivhd_header *h;
  442. struct amd_iommu *iommu;
  443. int ret;
  444. INIT_LIST_HEAD(&amd_iommu_list);
  445. end += table->length;
  446. p += IVRS_HEADER_LENGTH;
  447. while (p < end) {
  448. h = (struct ivhd_header *)p;
  449. switch (*p) {
  450. case ACPI_IVHD_TYPE:
  451. iommu = kzalloc(sizeof(struct amd_iommu), GFP_KERNEL);
  452. if (iommu == NULL)
  453. return -ENOMEM;
  454. ret = init_iommu_one(iommu, h);
  455. if (ret)
  456. return ret;
  457. break;
  458. default:
  459. break;
  460. }
  461. p += h->length;
  462. }
  463. WARN_ON(p != end);
  464. return 0;
  465. }
  466. static void __init free_unity_maps(void)
  467. {
  468. struct unity_map_entry *entry, *next;
  469. list_for_each_entry_safe(entry, next, &amd_iommu_unity_map, list) {
  470. list_del(&entry->list);
  471. kfree(entry);
  472. }
  473. }
  474. static int __init init_exclusion_range(struct ivmd_header *m)
  475. {
  476. int i;
  477. switch (m->type) {
  478. case ACPI_IVMD_TYPE:
  479. set_device_exclusion_range(m->devid, m);
  480. break;
  481. case ACPI_IVMD_TYPE_ALL:
  482. for (i = 0; i < amd_iommu_last_bdf; ++i)
  483. set_device_exclusion_range(i, m);
  484. break;
  485. case ACPI_IVMD_TYPE_RANGE:
  486. for (i = m->devid; i <= m->aux; ++i)
  487. set_device_exclusion_range(i, m);
  488. break;
  489. default:
  490. break;
  491. }
  492. return 0;
  493. }
  494. static int __init init_unity_map_range(struct ivmd_header *m)
  495. {
  496. struct unity_map_entry *e = 0;
  497. e = kzalloc(sizeof(*e), GFP_KERNEL);
  498. if (e == NULL)
  499. return -ENOMEM;
  500. switch (m->type) {
  501. default:
  502. case ACPI_IVMD_TYPE:
  503. e->devid_start = e->devid_end = m->devid;
  504. break;
  505. case ACPI_IVMD_TYPE_ALL:
  506. e->devid_start = 0;
  507. e->devid_end = amd_iommu_last_bdf;
  508. break;
  509. case ACPI_IVMD_TYPE_RANGE:
  510. e->devid_start = m->devid;
  511. e->devid_end = m->aux;
  512. break;
  513. }
  514. e->address_start = PAGE_ALIGN(m->range_start);
  515. e->address_end = e->address_start + PAGE_ALIGN(m->range_length);
  516. e->prot = m->flags >> 1;
  517. list_add_tail(&e->list, &amd_iommu_unity_map);
  518. return 0;
  519. }
  520. static int __init init_memory_definitions(struct acpi_table_header *table)
  521. {
  522. u8 *p = (u8 *)table, *end = (u8 *)table;
  523. struct ivmd_header *m;
  524. INIT_LIST_HEAD(&amd_iommu_unity_map);
  525. end += table->length;
  526. p += IVRS_HEADER_LENGTH;
  527. while (p < end) {
  528. m = (struct ivmd_header *)p;
  529. if (m->flags & IVMD_FLAG_EXCL_RANGE)
  530. init_exclusion_range(m);
  531. else if (m->flags & IVMD_FLAG_UNITY_MAP)
  532. init_unity_map_range(m);
  533. p += m->length;
  534. }
  535. return 0;
  536. }
  537. int __init amd_iommu_init(void)
  538. {
  539. int i, ret = 0;
  540. if (amd_iommu_disable) {
  541. printk(KERN_INFO "AMD IOMMU disabled by kernel command line\n");
  542. return 0;
  543. }
  544. /*
  545. * First parse ACPI tables to find the largest Bus/Dev/Func
  546. * we need to handle. Upon this information the shared data
  547. * structures for the IOMMUs in the system will be allocated
  548. */
  549. if (acpi_table_parse("IVRS", find_last_devid_acpi) != 0)
  550. return -ENODEV;
  551. dev_table_size = TBL_SIZE(DEV_TABLE_ENTRY_SIZE);
  552. alias_table_size = TBL_SIZE(ALIAS_TABLE_ENTRY_SIZE);
  553. rlookup_table_size = TBL_SIZE(RLOOKUP_TABLE_ENTRY_SIZE);
  554. ret = -ENOMEM;
  555. /* Device table - directly used by all IOMMUs */
  556. amd_iommu_dev_table = (void *)__get_free_pages(GFP_KERNEL,
  557. get_order(dev_table_size));
  558. if (amd_iommu_dev_table == NULL)
  559. goto out;
  560. /*
  561. * Alias table - map PCI Bus/Dev/Func to Bus/Dev/Func the
  562. * IOMMU see for that device
  563. */
  564. amd_iommu_alias_table = (void *)__get_free_pages(GFP_KERNEL,
  565. get_order(alias_table_size));
  566. if (amd_iommu_alias_table == NULL)
  567. goto free;
  568. /* IOMMU rlookup table - find the IOMMU for a specific device */
  569. amd_iommu_rlookup_table = (void *)__get_free_pages(GFP_KERNEL,
  570. get_order(rlookup_table_size));
  571. if (amd_iommu_rlookup_table == NULL)
  572. goto free;
  573. /*
  574. * Protection Domain table - maps devices to protection domains
  575. * This table has the same size as the rlookup_table
  576. */
  577. amd_iommu_pd_table = (void *)__get_free_pages(GFP_KERNEL,
  578. get_order(rlookup_table_size));
  579. if (amd_iommu_pd_table == NULL)
  580. goto free;
  581. amd_iommu_pd_alloc_bitmap = (void *)__get_free_pages(GFP_KERNEL,
  582. get_order(MAX_DOMAIN_ID/8));
  583. if (amd_iommu_pd_alloc_bitmap == NULL)
  584. goto free;
  585. /*
  586. * memory is allocated now; initialize the device table with all zeroes
  587. * and let all alias entries point to itself
  588. */
  589. memset(amd_iommu_dev_table, 0, dev_table_size);
  590. for (i = 0; i < amd_iommu_last_bdf; ++i)
  591. amd_iommu_alias_table[i] = i;
  592. memset(amd_iommu_pd_table, 0, rlookup_table_size);
  593. memset(amd_iommu_pd_alloc_bitmap, 0, MAX_DOMAIN_ID / 8);
  594. /*
  595. * never allocate domain 0 because its used as the non-allocated and
  596. * error value placeholder
  597. */
  598. amd_iommu_pd_alloc_bitmap[0] = 1;
  599. /*
  600. * now the data structures are allocated and basically initialized
  601. * start the real acpi table scan
  602. */
  603. ret = -ENODEV;
  604. if (acpi_table_parse("IVRS", init_iommu_all) != 0)
  605. goto free;
  606. if (acpi_table_parse("IVRS", init_memory_definitions) != 0)
  607. goto free;
  608. printk(KERN_INFO "AMD IOMMU: aperture size is %d MB\n",
  609. (1 << (amd_iommu_aperture_order-20)));
  610. printk(KERN_INFO "AMD IOMMU: device isolation ");
  611. if (amd_iommu_isolate)
  612. printk("enabled\n");
  613. else
  614. printk("disabled\n");
  615. out:
  616. return ret;
  617. free:
  618. if (amd_iommu_pd_alloc_bitmap)
  619. free_pages((unsigned long)amd_iommu_pd_alloc_bitmap, 1);
  620. if (amd_iommu_pd_table)
  621. free_pages((unsigned long)amd_iommu_pd_table,
  622. get_order(rlookup_table_size));
  623. if (amd_iommu_rlookup_table)
  624. free_pages((unsigned long)amd_iommu_rlookup_table,
  625. get_order(rlookup_table_size));
  626. if (amd_iommu_alias_table)
  627. free_pages((unsigned long)amd_iommu_alias_table,
  628. get_order(alias_table_size));
  629. if (amd_iommu_dev_table)
  630. free_pages((unsigned long)amd_iommu_dev_table,
  631. get_order(dev_table_size));
  632. free_iommu_all();
  633. free_unity_maps();
  634. goto out;
  635. }
  636. static int __init early_amd_iommu_detect(struct acpi_table_header *table)
  637. {
  638. return 0;
  639. }
  640. void __init amd_iommu_detect(void)
  641. {
  642. if (swiotlb || no_iommu || iommu_detected)
  643. return;
  644. if (amd_iommu_disable)
  645. return;
  646. if (acpi_table_parse("IVRS", early_amd_iommu_detect) == 0) {
  647. iommu_detected = 1;
  648. gart_iommu_aperture_disabled = 1;
  649. gart_iommu_aperture = 0;
  650. }
  651. }
  652. static int __init parse_amd_iommu_options(char *str)
  653. {
  654. for (; *str; ++str) {
  655. if (strcmp(str, "off") == 0)
  656. amd_iommu_disable = 1;
  657. if (strcmp(str, "isolate") == 0)
  658. amd_iommu_isolate = 1;
  659. }
  660. return 1;
  661. }
  662. static int __init parse_amd_iommu_size_options(char *str)
  663. {
  664. for (; *str; ++str) {
  665. if (strcmp(str, "32M") == 0)
  666. amd_iommu_aperture_order = 25;
  667. if (strcmp(str, "64M") == 0)
  668. amd_iommu_aperture_order = 26;
  669. if (strcmp(str, "128M") == 0)
  670. amd_iommu_aperture_order = 27;
  671. if (strcmp(str, "256M") == 0)
  672. amd_iommu_aperture_order = 28;
  673. if (strcmp(str, "512M") == 0)
  674. amd_iommu_aperture_order = 29;
  675. if (strcmp(str, "1G") == 0)
  676. amd_iommu_aperture_order = 30;
  677. }
  678. return 1;
  679. }
  680. __setup("amd_iommu=", parse_amd_iommu_options);
  681. __setup("amd_iommu_size=", parse_amd_iommu_size_options);