amd_iommu_init.c 28 KB

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