amd_iommu_init.c 31 KB

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