amd_iommu_init.c 35 KB

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