amd_iommu_init.c 31 KB

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