amd_iommu_init.c 39 KB

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