amd_iommu_init.c 20 KB

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