amd_iommu_init.c 40 KB

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