amd_iommu_init.c 36 KB

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