amd_iommu_init.c 29 KB

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