···2266226622672267 ivrs_ioapic [HW,X86-64]22682268 Provide an override to the IOAPIC-ID<->DEVICE-ID22692269- mapping provided in the IVRS ACPI table. For22702270- example, to map IOAPIC-ID decimal 10 to22712271- PCI device 00:14.0 write the parameter as:22692269+ mapping provided in the IVRS ACPI table.22702270+ By default, PCI segment is 0, and can be omitted.22712271+ For example:22722272+ * To map IOAPIC-ID decimal 10 to PCI device 00:14.022732273+ write the parameter as:22722274 ivrs_ioapic[10]=00:14.022752275+ * To map IOAPIC-ID decimal 10 to PCI segment 0x1 and22762276+ PCI device 00:14.0 write the parameter as:22772277+ ivrs_ioapic[10]=0001:00:14.02273227822742279 ivrs_hpet [HW,X86-64]22752280 Provide an override to the HPET-ID<->DEVICE-ID22762276- mapping provided in the IVRS ACPI table. For22772277- example, to map HPET-ID decimal 0 to22782278- PCI device 00:14.0 write the parameter as:22812281+ mapping provided in the IVRS ACPI table.22822282+ By default, PCI segment is 0, and can be omitted.22832283+ For example:22842284+ * To map HPET-ID decimal 0 to PCI device 00:14.022852285+ write the parameter as:22792286 ivrs_hpet[0]=00:14.022872287+ * To map HPET-ID decimal 10 to PCI segment 0x1 and22882288+ PCI device 00:14.0 write the parameter as:22892289+ ivrs_ioapic[10]=0001:00:14.02280229022812291 ivrs_acpihid [HW,X86-64]22822292 Provide an override to the ACPI-HID:UID<->DEVICE-ID22832283- mapping provided in the IVRS ACPI table. For22842284- example, to map UART-HID:UID AMD0020:0 to22852285- PCI device 00:14.5 write the parameter as:22932293+ mapping provided in the IVRS ACPI table.22942294+22952295+ For example, to map UART-HID:UID AMD0020:0 to22962296+ PCI segment 0x1 and PCI device ID 00:14.5,22972297+ write the parameter as:22982298+ ivrs_acpihid[0001:00:14.5]=AMD0020:022992299+23002300+ By default, PCI segment is 0, and can be omitted.23012301+ For example, PCI device 00:14.5 write the parameter as:22862302 ivrs_acpihid[00:14.5]=AMD0020:02287230322882304 js= [HW,JOY] Analog joystick
···788788}789789790790#ifdef CONFIG_IOMMU_API791791+static void iort_rmr_free(struct device *dev,792792+ struct iommu_resv_region *region)793793+{794794+ struct iommu_iort_rmr_data *rmr_data;795795+796796+ rmr_data = container_of(region, struct iommu_iort_rmr_data, rr);797797+ kfree(rmr_data->sids);798798+ kfree(rmr_data);799799+}800800+801801+static struct iommu_iort_rmr_data *iort_rmr_alloc(802802+ struct acpi_iort_rmr_desc *rmr_desc,803803+ int prot, enum iommu_resv_type type,804804+ u32 *sids, u32 num_sids)805805+{806806+ struct iommu_iort_rmr_data *rmr_data;807807+ struct iommu_resv_region *region;808808+ u32 *sids_copy;809809+ u64 addr = rmr_desc->base_address, size = rmr_desc->length;810810+811811+ rmr_data = kmalloc(sizeof(*rmr_data), GFP_KERNEL);812812+ if (!rmr_data)813813+ return NULL;814814+815815+ /* Create a copy of SIDs array to associate with this rmr_data */816816+ sids_copy = kmemdup(sids, num_sids * sizeof(*sids), GFP_KERNEL);817817+ if (!sids_copy) {818818+ kfree(rmr_data);819819+ return NULL;820820+ }821821+ rmr_data->sids = sids_copy;822822+ rmr_data->num_sids = num_sids;823823+824824+ if (!IS_ALIGNED(addr, SZ_64K) || !IS_ALIGNED(size, SZ_64K)) {825825+ /* PAGE align base addr and size */826826+ addr &= PAGE_MASK;827827+ size = PAGE_ALIGN(size + offset_in_page(rmr_desc->base_address));828828+829829+ pr_err(FW_BUG "RMR descriptor[0x%llx - 0x%llx] not aligned to 64K, continue with [0x%llx - 0x%llx]\n",830830+ rmr_desc->base_address,831831+ rmr_desc->base_address + rmr_desc->length - 1,832832+ addr, addr + size - 1);833833+ }834834+835835+ region = &rmr_data->rr;836836+ INIT_LIST_HEAD(®ion->list);837837+ region->start = addr;838838+ region->length = size;839839+ region->prot = prot;840840+ region->type = type;841841+ region->free = iort_rmr_free;842842+843843+ return rmr_data;844844+}845845+846846+static void iort_rmr_desc_check_overlap(struct acpi_iort_rmr_desc *desc,847847+ u32 count)848848+{849849+ int i, j;850850+851851+ for (i = 0; i < count; i++) {852852+ u64 end, start = desc[i].base_address, length = desc[i].length;853853+854854+ if (!length) {855855+ pr_err(FW_BUG "RMR descriptor[0x%llx] with zero length, continue anyway\n",856856+ start);857857+ continue;858858+ }859859+860860+ end = start + length - 1;861861+862862+ /* Check for address overlap */863863+ for (j = i + 1; j < count; j++) {864864+ u64 e_start = desc[j].base_address;865865+ u64 e_end = e_start + desc[j].length - 1;866866+867867+ if (start <= e_end && end >= e_start)868868+ pr_err(FW_BUG "RMR descriptor[0x%llx - 0x%llx] overlaps, continue anyway\n",869869+ start, end);870870+ }871871+ }872872+}873873+874874+/*875875+ * Please note, we will keep the already allocated RMR reserve876876+ * regions in case of a memory allocation failure.877877+ */878878+static void iort_get_rmrs(struct acpi_iort_node *node,879879+ struct acpi_iort_node *smmu,880880+ u32 *sids, u32 num_sids,881881+ struct list_head *head)882882+{883883+ struct acpi_iort_rmr *rmr = (struct acpi_iort_rmr *)node->node_data;884884+ struct acpi_iort_rmr_desc *rmr_desc;885885+ int i;886886+887887+ rmr_desc = ACPI_ADD_PTR(struct acpi_iort_rmr_desc, node,888888+ rmr->rmr_offset);889889+890890+ iort_rmr_desc_check_overlap(rmr_desc, rmr->rmr_count);891891+892892+ for (i = 0; i < rmr->rmr_count; i++, rmr_desc++) {893893+ struct iommu_iort_rmr_data *rmr_data;894894+ enum iommu_resv_type type;895895+ int prot = IOMMU_READ | IOMMU_WRITE;896896+897897+ if (rmr->flags & ACPI_IORT_RMR_REMAP_PERMITTED)898898+ type = IOMMU_RESV_DIRECT_RELAXABLE;899899+ else900900+ type = IOMMU_RESV_DIRECT;901901+902902+ if (rmr->flags & ACPI_IORT_RMR_ACCESS_PRIVILEGE)903903+ prot |= IOMMU_PRIV;904904+905905+ /* Attributes 0x00 - 0x03 represents device memory */906906+ if (ACPI_IORT_RMR_ACCESS_ATTRIBUTES(rmr->flags) <=907907+ ACPI_IORT_RMR_ATTR_DEVICE_GRE)908908+ prot |= IOMMU_MMIO;909909+ else if (ACPI_IORT_RMR_ACCESS_ATTRIBUTES(rmr->flags) ==910910+ ACPI_IORT_RMR_ATTR_NORMAL_IWB_OWB)911911+ prot |= IOMMU_CACHE;912912+913913+ rmr_data = iort_rmr_alloc(rmr_desc, prot, type,914914+ sids, num_sids);915915+ if (!rmr_data)916916+ return;917917+918918+ list_add_tail(&rmr_data->rr.list, head);919919+ }920920+}921921+922922+static u32 *iort_rmr_alloc_sids(u32 *sids, u32 count, u32 id_start,923923+ u32 new_count)924924+{925925+ u32 *new_sids;926926+ u32 total_count = count + new_count;927927+ int i;928928+929929+ new_sids = krealloc_array(sids, count + new_count,930930+ sizeof(*new_sids), GFP_KERNEL);931931+ if (!new_sids)932932+ return NULL;933933+934934+ for (i = count; i < total_count; i++)935935+ new_sids[i] = id_start++;936936+937937+ return new_sids;938938+}939939+940940+static bool iort_rmr_has_dev(struct device *dev, u32 id_start,941941+ u32 id_count)942942+{943943+ int i;944944+ struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);945945+946946+ /*947947+ * Make sure the kernel has preserved the boot firmware PCIe948948+ * configuration. This is required to ensure that the RMR PCIe949949+ * StreamIDs are still valid (Refer: ARM DEN 0049E.d Section 3.1.1.5).950950+ */951951+ if (dev_is_pci(dev)) {952952+ struct pci_dev *pdev = to_pci_dev(dev);953953+ struct pci_host_bridge *host = pci_find_host_bridge(pdev->bus);954954+955955+ if (!host->preserve_config)956956+ return false;957957+ }958958+959959+ for (i = 0; i < fwspec->num_ids; i++) {960960+ if (fwspec->ids[i] >= id_start &&961961+ fwspec->ids[i] <= id_start + id_count)962962+ return true;963963+ }964964+965965+ return false;966966+}967967+968968+static void iort_node_get_rmr_info(struct acpi_iort_node *node,969969+ struct acpi_iort_node *iommu,970970+ struct device *dev, struct list_head *head)971971+{972972+ struct acpi_iort_node *smmu = NULL;973973+ struct acpi_iort_rmr *rmr;974974+ struct acpi_iort_id_mapping *map;975975+ u32 *sids = NULL;976976+ u32 num_sids = 0;977977+ int i;978978+979979+ if (!node->mapping_offset || !node->mapping_count) {980980+ pr_err(FW_BUG "Invalid ID mapping, skipping RMR node %p\n",981981+ node);982982+ return;983983+ }984984+985985+ rmr = (struct acpi_iort_rmr *)node->node_data;986986+ if (!rmr->rmr_offset || !rmr->rmr_count)987987+ return;988988+989989+ map = ACPI_ADD_PTR(struct acpi_iort_id_mapping, node,990990+ node->mapping_offset);991991+992992+ /*993993+ * Go through the ID mappings and see if we have a match for SMMU994994+ * and dev(if !NULL). If found, get the sids for the Node.995995+ * Please note, id_count is equal to the number of IDs in the996996+ * range minus one.997997+ */998998+ for (i = 0; i < node->mapping_count; i++, map++) {999999+ struct acpi_iort_node *parent;10001000+10011001+ if (!map->id_count)10021002+ continue;10031003+10041004+ parent = ACPI_ADD_PTR(struct acpi_iort_node, iort_table,10051005+ map->output_reference);10061006+ if (parent != iommu)10071007+ continue;10081008+10091009+ /* If dev is valid, check RMR node corresponds to the dev SID */10101010+ if (dev && !iort_rmr_has_dev(dev, map->output_base,10111011+ map->id_count))10121012+ continue;10131013+10141014+ /* Retrieve SIDs associated with the Node. */10151015+ sids = iort_rmr_alloc_sids(sids, num_sids, map->output_base,10161016+ map->id_count + 1);10171017+ if (!sids)10181018+ return;10191019+10201020+ num_sids += map->id_count + 1;10211021+ }10221022+10231023+ if (!sids)10241024+ return;10251025+10261026+ iort_get_rmrs(node, smmu, sids, num_sids, head);10271027+ kfree(sids);10281028+}10291029+10301030+static void iort_find_rmrs(struct acpi_iort_node *iommu, struct device *dev,10311031+ struct list_head *head)10321032+{10331033+ struct acpi_table_iort *iort;10341034+ struct acpi_iort_node *iort_node, *iort_end;10351035+ int i;10361036+10371037+ /* Only supports ARM DEN 0049E.d onwards */10381038+ if (iort_table->revision < 5)10391039+ return;10401040+10411041+ iort = (struct acpi_table_iort *)iort_table;10421042+10431043+ iort_node = ACPI_ADD_PTR(struct acpi_iort_node, iort,10441044+ iort->node_offset);10451045+ iort_end = ACPI_ADD_PTR(struct acpi_iort_node, iort,10461046+ iort_table->length);10471047+10481048+ for (i = 0; i < iort->node_count; i++) {10491049+ if (WARN_TAINT(iort_node >= iort_end, TAINT_FIRMWARE_WORKAROUND,10501050+ "IORT node pointer overflows, bad table!\n"))10511051+ return;10521052+10531053+ if (iort_node->type == ACPI_IORT_NODE_RMR)10541054+ iort_node_get_rmr_info(iort_node, iommu, dev, head);10551055+10561056+ iort_node = ACPI_ADD_PTR(struct acpi_iort_node, iort_node,10571057+ iort_node->length);10581058+ }10591059+}10601060+10611061+/*10621062+ * Populate the RMR list associated with a given IOMMU and dev(if provided).10631063+ * If dev is NULL, the function populates all the RMRs associated with the10641064+ * given IOMMU.10651065+ */10661066+static void iort_iommu_rmr_get_resv_regions(struct fwnode_handle *iommu_fwnode,10671067+ struct device *dev,10681068+ struct list_head *head)10691069+{10701070+ struct acpi_iort_node *iommu;10711071+10721072+ iommu = iort_get_iort_node(iommu_fwnode);10731073+ if (!iommu)10741074+ return;10751075+10761076+ iort_find_rmrs(iommu, dev, head);10771077+}10781078+7911079static struct acpi_iort_node *iort_get_msi_resv_iommu(struct device *dev)7921080{7931081 struct acpi_iort_node *iommu;···1094806 return NULL;1095807}109680810971097-/**10981098- * iort_iommu_msi_get_resv_regions - Reserved region driver helper10991099- * @dev: Device from iommu_get_resv_regions()11001100- * @head: Reserved region list from iommu_get_resv_regions()11011101- *11021102- * Returns: Number of msi reserved regions on success (0 if platform11031103- * doesn't require the reservation or no associated msi regions),11041104- * appropriate error value otherwise. The ITS interrupt translation11051105- * spaces (ITS_base + SZ_64K, SZ_64K) associated with the device11061106- * are the msi reserved regions.809809+/*810810+ * Retrieve platform specific HW MSI reserve regions.811811+ * The ITS interrupt translation spaces (ITS_base + SZ_64K, SZ_64K)812812+ * associated with the device are the HW MSI reserved regions.1107813 */11081108-int iort_iommu_msi_get_resv_regions(struct device *dev, struct list_head *head)814814+static void iort_iommu_msi_get_resv_regions(struct device *dev,815815+ struct list_head *head)1109816{1110817 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);1111818 struct acpi_iort_its_group *its;1112819 struct acpi_iort_node *iommu_node, *its_node = NULL;11131113- int i, resv = 0;820820+ int i;11148211115822 iommu_node = iort_get_msi_resv_iommu(dev);1116823 if (!iommu_node)11171117- return 0;824824+ return;11188251119826 /*1120827 * Current logic to reserve ITS regions relies on HW topologies···1129846 }11308471131848 if (!its_node)11321132- return 0;849849+ return;11338501134851 /* Move to ITS specific data */1135852 its = (struct acpi_iort_its_group *)its_node->node_data;···11438601144861 region = iommu_alloc_resv_region(base + SZ_64K, SZ_64K,1145862 prot, IOMMU_RESV_MSI);11461146- if (region) {863863+ if (region)1147864 list_add_tail(®ion->list, head);11481148- resv++;11491149- }1150865 }1151866 }11521152-11531153- return (resv == its->its_count) ? resv : -ENODEV;1154867}868868+869869+/**870870+ * iort_iommu_get_resv_regions - Generic helper to retrieve reserved regions.871871+ * @dev: Device from iommu_get_resv_regions()872872+ * @head: Reserved region list from iommu_get_resv_regions()873873+ */874874+void iort_iommu_get_resv_regions(struct device *dev, struct list_head *head)875875+{876876+ struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);877877+878878+ iort_iommu_msi_get_resv_regions(dev, head);879879+ iort_iommu_rmr_get_resv_regions(fwspec->iommu_fwnode, dev, head);880880+}881881+882882+/**883883+ * iort_get_rmr_sids - Retrieve IORT RMR node reserved regions with884884+ * associated StreamIDs information.885885+ * @iommu_fwnode: fwnode associated with IOMMU886886+ * @head: Resereved region list887887+ */888888+void iort_get_rmr_sids(struct fwnode_handle *iommu_fwnode,889889+ struct list_head *head)890890+{891891+ iort_iommu_rmr_get_resv_regions(iommu_fwnode, NULL, head);892892+}893893+EXPORT_SYMBOL_GPL(iort_get_rmr_sids);894894+895895+/**896896+ * iort_put_rmr_sids - Free memory allocated for RMR reserved regions.897897+ * @iommu_fwnode: fwnode associated with IOMMU898898+ * @head: Resereved region list899899+ */900900+void iort_put_rmr_sids(struct fwnode_handle *iommu_fwnode,901901+ struct list_head *head)902902+{903903+ struct iommu_resv_region *entry, *next;904904+905905+ list_for_each_entry_safe(entry, next, head, list)906906+ entry->free(NULL, entry);907907+}908908+EXPORT_SYMBOL_GPL(iort_put_rmr_sids);11559091156910static inline bool iort_iommu_driver_enabled(u8 type)1157911{···13541034}1355103513561036#else13571357-int iort_iommu_msi_get_resv_regions(struct device *dev, struct list_head *head)13581358-{ return 0; }10371037+void iort_iommu_get_resv_regions(struct device *dev, struct list_head *head)10381038+{ }13591039int iort_iommu_configure_id(struct device *dev, const u32 *input_id)13601040{ return -ENODEV; }13611041#endif
+7-10
drivers/char/agp/intel-gtt.c
···2020#include <linux/kernel.h>2121#include <linux/pagemap.h>2222#include <linux/agp_backend.h>2323-#include <linux/intel-iommu.h>2323+#include <linux/iommu.h>2424#include <linux/delay.h>2525#include <asm/smp.h>2626#include "agp.h"···573573 */574574static inline int needs_ilk_vtd_wa(void)575575{576576-#ifdef CONFIG_INTEL_IOMMU577576 const unsigned short gpu_devid = intel_private.pcidev->device;578577579579- /* Query intel_iommu to see if we need the workaround. Presumably that580580- * was loaded first.578578+ /*579579+ * Query iommu subsystem to see if we need the workaround. Presumably580580+ * that was loaded first.581581 */582582- if ((gpu_devid == PCI_DEVICE_ID_INTEL_IRONLAKE_D_IG ||583583- gpu_devid == PCI_DEVICE_ID_INTEL_IRONLAKE_M_IG) &&584584- intel_iommu_gfx_mapped)585585- return 1;586586-#endif587587- return 0;582582+ return ((gpu_devid == PCI_DEVICE_ID_INTEL_IRONLAKE_D_IG ||583583+ gpu_devid == PCI_DEVICE_ID_INTEL_IRONLAKE_M_IG) &&584584+ device_iommu_mapped(&intel_private.pcidev->dev));588585}589586590587static bool intel_gtt_can_wc(void)
···144144 select IRQ_MSI_IOMMU145145 select NEED_SG_DMA_LENGTH146146147147+config IOMMU_DMA_PCI_SAC148148+ bool "Enable 64-bit legacy PCI optimisation by default"149149+ depends on IOMMU_DMA150150+ help151151+ Enable by default an IOMMU optimisation for 64-bit legacy PCI devices,152152+ wherein the DMA API layer will always first try to allocate a 32-bit153153+ DMA address suitable for a single address cycle, before falling back154154+ to allocating from the device's full usable address range. If your155155+ system has 64-bit legacy PCI devices in 32-bit slots where using dual156156+ address cycles reduces DMA throughput significantly, this may be157157+ beneficial to overall performance.158158+159159+ If you have a modern PCI Express based system, this feature mostly just160160+ represents extra overhead in the allocation path for no practical161161+ benefit, and it should usually be preferable to say "n" here.162162+163163+ However, beware that this feature has also historically papered over164164+ bugs where the IOMMU address width and/or device DMA mask is not set165165+ correctly. If device DMA problems and IOMMU faults start occurring166166+ after disabling this option, it is almost certainly indicative of a167167+ latent driver or firmware/BIOS bug, which would previously have only168168+ manifested with several gigabytes worth of concurrent DMA mappings.169169+170170+ If this option is not set, the feature can still be re-enabled at171171+ boot time with the "iommu.forcedac=0" command-line argument.172172+147173# Shared Virtual Addressing148174config IOMMU_SVA149175 bool···388362 help389363 When running on a Qualcomm platform that has the custom variant390364 of the ARM SMMU, this needs to be built into the SMMU driver.365365+366366+config ARM_SMMU_QCOM_DEBUG367367+ bool "ARM SMMU QCOM implementation defined debug support"368368+ depends on ARM_SMMU_QCOM369369+ help370370+ Support for implementation specific debug features in ARM SMMU371371+ hardware found in QTI platforms.372372+373373+ Say Y here to enable debug for issues such as TLB sync timeouts374374+ which requires implementation defined register dumps.391375392376config ARM_SMMU_V3393377 tristate "ARM Ltd. System MMU Version 3 (SMMUv3) Support"
···6767#define MMIO_INTCAPXT_EVT_OFFSET 0x01706868#define MMIO_INTCAPXT_PPR_OFFSET 0x01786969#define MMIO_INTCAPXT_GALOG_OFFSET 0x01807070+#define MMIO_EXT_FEATURES2 0x01A07071#define MMIO_CMD_HEAD_OFFSET 0x20007172#define MMIO_CMD_TAIL_OFFSET 0x20087273#define MMIO_EVT_HEAD_OFFSET 0x2010···102101103102#define FEATURE_GLXVAL_SHIFT 14104103#define FEATURE_GLXVAL_MASK (0x03ULL << FEATURE_GLXVAL_SHIFT)104104+105105+/* Extended Feature 2 Bits */106106+#define FEATURE_SNPAVICSUP_SHIFT 5107107+#define FEATURE_SNPAVICSUP_MASK (0x07ULL << FEATURE_SNPAVICSUP_SHIFT)108108+#define FEATURE_SNPAVICSUP_GAM(x) \109109+ ((x & FEATURE_SNPAVICSUP_MASK) >> FEATURE_SNPAVICSUP_SHIFT == 0x1)105110106111/* Note:107112 * The current driver only support 16-bit PASID.···150143#define EVENT_FLAG_I 0x008151144152145/* feature control bits */153153-#define CONTROL_IOMMU_EN 0x00ULL154154-#define CONTROL_HT_TUN_EN 0x01ULL155155-#define CONTROL_EVT_LOG_EN 0x02ULL156156-#define CONTROL_EVT_INT_EN 0x03ULL157157-#define CONTROL_COMWAIT_EN 0x04ULL158158-#define CONTROL_INV_TIMEOUT 0x05ULL159159-#define CONTROL_PASSPW_EN 0x08ULL160160-#define CONTROL_RESPASSPW_EN 0x09ULL161161-#define CONTROL_COHERENT_EN 0x0aULL162162-#define CONTROL_ISOC_EN 0x0bULL163163-#define CONTROL_CMDBUF_EN 0x0cULL164164-#define CONTROL_PPRLOG_EN 0x0dULL165165-#define CONTROL_PPRINT_EN 0x0eULL166166-#define CONTROL_PPR_EN 0x0fULL167167-#define CONTROL_GT_EN 0x10ULL168168-#define CONTROL_GA_EN 0x11ULL169169-#define CONTROL_GAM_EN 0x19ULL170170-#define CONTROL_GALOG_EN 0x1CULL171171-#define CONTROL_GAINT_EN 0x1DULL172172-#define CONTROL_XT_EN 0x32ULL173173-#define CONTROL_INTCAPXT_EN 0x33ULL146146+#define CONTROL_IOMMU_EN 0147147+#define CONTROL_HT_TUN_EN 1148148+#define CONTROL_EVT_LOG_EN 2149149+#define CONTROL_EVT_INT_EN 3150150+#define CONTROL_COMWAIT_EN 4151151+#define CONTROL_INV_TIMEOUT 5152152+#define CONTROL_PASSPW_EN 8153153+#define CONTROL_RESPASSPW_EN 9154154+#define CONTROL_COHERENT_EN 10155155+#define CONTROL_ISOC_EN 11156156+#define CONTROL_CMDBUF_EN 12157157+#define CONTROL_PPRLOG_EN 13158158+#define CONTROL_PPRINT_EN 14159159+#define CONTROL_PPR_EN 15160160+#define CONTROL_GT_EN 16161161+#define CONTROL_GA_EN 17162162+#define CONTROL_GAM_EN 25163163+#define CONTROL_GALOG_EN 28164164+#define CONTROL_GAINT_EN 29165165+#define CONTROL_XT_EN 50166166+#define CONTROL_INTCAPXT_EN 51167167+#define CONTROL_SNPAVIC_EN 61174168175169#define CTRL_INV_TO_MASK (7 << CONTROL_INV_TIMEOUT)176170#define CTRL_INV_TO_NONE 0···453445 u32 *table;454446};455447456456-extern struct irq_remap_table **irq_lookup_table;457457-458448/* Interrupt remapping feature used? */459449extern bool amd_iommu_irq_remap;460450···462456/* kmem_cache to get tables with 128 byte alignement */463457extern struct kmem_cache *amd_iommu_irq_cache;464458459459+#define PCI_SBDF_TO_SEGID(sbdf) (((sbdf) >> 16) & 0xffff)460460+#define PCI_SBDF_TO_DEVID(sbdf) ((sbdf) & 0xffff)461461+#define PCI_SEG_DEVID_TO_SBDF(seg, devid) ((((u32)(seg) & 0xffff) << 16) | \462462+ ((devid) & 0xffff))463463+464464+/* Make iterating over all pci segment easier */465465+#define for_each_pci_segment(pci_seg) \466466+ list_for_each_entry((pci_seg), &amd_iommu_pci_seg_list, list)467467+#define for_each_pci_segment_safe(pci_seg, next) \468468+ list_for_each_entry_safe((pci_seg), (next), &amd_iommu_pci_seg_list, list)465469/*466470 * Make iterating over all IOMMUs easier467471 */···494478struct amd_iommu_fault {495479 u64 address; /* IO virtual address of the fault*/496480 u32 pasid; /* Address space identifier */497497- u16 device_id; /* Originating PCI device id */481481+ u32 sbdf; /* Originating PCI device id */498482 u16 tag; /* PPR tag */499483 u16 flags; /* Fault flags */500484501485};502486503487488488+struct amd_iommu;504489struct iommu_domain;505490struct irq_domain;506491struct amd_irte_ops;···548531};549532550533/*534534+ * This structure contains information about one PCI segment in the system.535535+ */536536+struct amd_iommu_pci_seg {537537+ /* List with all PCI segments in the system */538538+ struct list_head list;539539+540540+ /* List of all available dev_data structures */541541+ struct llist_head dev_data_list;542542+543543+ /* PCI segment number */544544+ u16 id;545545+546546+ /* Largest PCI device id we expect translation requests for */547547+ u16 last_bdf;548548+549549+ /* Size of the device table */550550+ u32 dev_table_size;551551+552552+ /* Size of the alias table */553553+ u32 alias_table_size;554554+555555+ /* Size of the rlookup table */556556+ u32 rlookup_table_size;557557+558558+ /*559559+ * device table virtual address560560+ *561561+ * Pointer to the per PCI segment device table.562562+ * It is indexed by the PCI device id or the HT unit id and contains563563+ * information about the domain the device belongs to as well as the564564+ * page table root pointer.565565+ */566566+ struct dev_table_entry *dev_table;567567+568568+ /*569569+ * The rlookup iommu table is used to find the IOMMU which is570570+ * responsible for a specific device. It is indexed by the PCI571571+ * device id.572572+ */573573+ struct amd_iommu **rlookup_table;574574+575575+ /*576576+ * This table is used to find the irq remapping table for a given577577+ * device id quickly.578578+ */579579+ struct irq_remap_table **irq_lookup_table;580580+581581+ /*582582+ * Pointer to a device table which the content of old device table583583+ * will be copied to. It's only be used in kdump kernel.584584+ */585585+ struct dev_table_entry *old_dev_tbl_cpy;586586+587587+ /*588588+ * The alias table is a driver specific data structure which contains the589589+ * mappings of the PCI device ids to the actual requestor ids on the IOMMU.590590+ * More than one device can share the same requestor id.591591+ */592592+ u16 *alias_table;593593+594594+ /*595595+ * A list of required unity mappings we find in ACPI. It is not locked596596+ * because as runtime it is only read. It is created at ACPI table597597+ * parsing time.598598+ */599599+ struct list_head unity_map;600600+};601601+602602+/*551603 * Structure where we save information about one hardware AMD IOMMU in the552604 * system.553605 */···653567 /* Extended features */654568 u64 features;655569570570+ /* Extended features 2 */571571+ u64 features2;572572+656573 /* IOMMUv2 */657574 bool is_iommu_v2;658575···670581 u16 cap_ptr;671582672583 /* pci domain of this IOMMU */673673- u16 pci_seg;584584+ struct amd_iommu_pci_seg *pci_seg;674585675586 /* start of exclusion range of that IOMMU */676587 u64 exclusion_start;···755666 struct list_head list;756667 u8 uid[ACPIHID_UID_LEN];757668 u8 hid[ACPIHID_HID_LEN];758758- u16 devid;759759- u16 root_devid;669669+ u32 devid;670670+ u32 root_devid;760671 bool cmd_line;761672 struct iommu_group *group;762673};···764675struct devid_map {765676 struct list_head list;766677 u8 id;767767- u16 devid;678678+ u32 devid;768679 bool cmd_line;769680};770681···778689 struct list_head list; /* For domain->dev_list */779690 struct llist_node dev_data_list; /* For global dev_data_list */780691 struct protection_domain *domain; /* Domain the device is bound to */781781- struct pci_dev *pdev;692692+ struct device *dev;782693 u16 devid; /* PCI Device ID */783694 bool iommu_v2; /* Device can make use of IOMMUv2 */784695 struct {···797708extern struct list_head ioapic_map;798709extern struct list_head hpet_map;799710extern struct list_head acpihid_map;711711+712712+/*713713+ * List with all PCI segments in the system. This list is not locked because714714+ * it is only written at driver initialization time715715+ */716716+extern struct list_head amd_iommu_pci_seg_list;800717801718/*802719 * List with all IOMMUs in the system. This list is not locked because it is···844749};845750846751/*847847- * List of all unity mappings. It is not locked because as runtime it is only848848- * read. It is created at ACPI table parsing time.849849- */850850-extern struct list_head amd_iommu_unity_map;851851-852852-/*853752 * Data structures for device handling854753 */855754856856-/*857857- * Device table used by hardware. Read and write accesses by software are858858- * locked with the amd_iommu_pd_table lock.859859- */860860-extern struct dev_table_entry *amd_iommu_dev_table;861861-862862-/*863863- * Alias table to find requestor ids to device ids. Not locked because only864864- * read on runtime.865865- */866866-extern u16 *amd_iommu_alias_table;867867-868868-/*869869- * Reverse lookup table to find the IOMMU which translates a specific device.870870- */871871-extern struct amd_iommu **amd_iommu_rlookup_table;872872-873755/* size of the dma_ops aperture as power of 2 */874756extern unsigned amd_iommu_aperture_order;875875-876876-/* largest PCI device id we expect translation requests for */877877-extern u16 amd_iommu_last_bdf;878757879758/* allocation bitmap for domain ids */880759extern unsigned long *amd_iommu_pd_alloc_bitmap;···982913983914struct amd_ir_data {984915 u32 cached_ga_tag;916916+ struct amd_iommu *iommu;985917 struct irq_2_irte irq_2_irte;986918 struct msi_msg msi_entry;987919 void *entry; /* Pointer to union irte or struct irte_ga */···10009301001931struct amd_irte_ops {1002932 void (*prepare)(void *, u32, bool, u8, u32, int);10031003- void (*activate)(void *, u16, u16);10041004- void (*deactivate)(void *, u16, u16);10051005- void (*set_affinity)(void *, u16, u16, u8, u32);933933+ void (*activate)(struct amd_iommu *iommu, void *, u16, u16);934934+ void (*deactivate)(struct amd_iommu *iommu, void *, u16, u16);935935+ void (*set_affinity)(struct amd_iommu *iommu, void *, u16, u16, u8, u32);1006936 void *(*get)(struct irq_remap_table *, int);1007937 void (*set_allocated)(struct irq_remap_table *, int);1008938 bool (*is_allocated)(struct irq_remap_table *, int);
+608-336
drivers/iommu/amd/init.c
···8484#define ACPI_DEVFLAG_ATSDIS 0x1000000085858686#define LOOP_TIMEOUT 20000008787+8888+#define IVRS_GET_SBDF_ID(seg, bus, dev, fd) (((seg & 0xffff) << 16) | ((bus & 0xff) << 8) \8989+ | ((dev & 0x1f) << 3) | (fn & 0x7))9090+8791/*8892 * ACPI table definitions8993 *···114110115111 /* Following only valid on IVHD type 11h and 40h */116112 u64 efr_reg; /* Exact copy of MMIO_EXT_FEATURES */117117- u64 res;113113+ u64 efr_reg2;118114} __attribute__((packed));119115120116/*···145141 u16 length;146142 u16 devid;147143 u16 aux;148148- u64 resv;144144+ u16 pci_seg;145145+ u8 resv[6];149146 u64 range_start;150147 u64 range_length;151148} __attribute__((packed));···164159static bool amd_iommu_force_enable __initdata;165160static int amd_iommu_target_ivhd_type;166161167167-u16 amd_iommu_last_bdf; /* largest PCI device id we have168168- to handle */169169-LIST_HEAD(amd_iommu_unity_map); /* a list of required unity mappings170170- we find in ACPI */162162+/* Global EFR and EFR2 registers */163163+u64 amd_iommu_efr;164164+u64 amd_iommu_efr2;171165166166+/* SNP is enabled on the system? */167167+bool amd_iommu_snp_en;168168+EXPORT_SYMBOL(amd_iommu_snp_en);169169+170170+LIST_HEAD(amd_iommu_pci_seg_list); /* list of all PCI segments */172171LIST_HEAD(amd_iommu_list); /* list of all AMD IOMMUs in the173172 system */174173···195186bool amd_iommu_force_isolation __read_mostly;196187197188/*198198- * Pointer to the device table which is shared by all AMD IOMMUs199199- * it is indexed by the PCI device id or the HT unit id and contains200200- * information about the domain the device belongs to as well as the201201- * page table root pointer.202202- */203203-struct dev_table_entry *amd_iommu_dev_table;204204-/*205205- * Pointer to a device table which the content of old device table206206- * will be copied to. It's only be used in kdump kernel.207207- */208208-static struct dev_table_entry *old_dev_tbl_cpy;209209-210210-/*211211- * The alias table is a driver specific data structure which contains the212212- * mappings of the PCI device ids to the actual requestor ids on the IOMMU.213213- * More than one device can share the same requestor id.214214- */215215-u16 *amd_iommu_alias_table;216216-217217-/*218218- * The rlookup table is used to find the IOMMU which is responsible219219- * for a specific device. It is also indexed by the PCI device id.220220- */221221-struct amd_iommu **amd_iommu_rlookup_table;222222-223223-/*224224- * This table is used to find the irq remapping table for a given device id225225- * quickly.226226- */227227-struct irq_remap_table **irq_lookup_table;228228-229229-/*230189 * AMD IOMMU allows up to 2^16 different protection domains. This is a bitmap231190 * to know which ones are already in use.232191 */233192unsigned long *amd_iommu_pd_alloc_bitmap;234234-235235-static u32 dev_table_size; /* size of the device table */236236-static u32 alias_table_size; /* size of the alias table */237237-static u32 rlookup_table_size; /* size if the rlookup table */238193239194enum iommu_init_state {240195 IOMMU_START_STATE,···229256230257static int amd_iommu_enable_interrupts(void);231258static int __init iommu_go_to_state(enum iommu_init_state state);232232-static void init_device_table_dma(void);259259+static void init_device_table_dma(struct amd_iommu_pci_seg *pci_seg);233260234261static bool amd_iommu_pre_enabled = true;235262···254281 iommu->flags |= AMD_IOMMU_FLAG_TRANS_PRE_ENABLED;255282}256283257257-static inline void update_last_devid(u16 devid)258258-{259259- if (devid > amd_iommu_last_bdf)260260- amd_iommu_last_bdf = devid;261261-}262262-263263-static inline unsigned long tbl_size(int entry_size)284284+static inline unsigned long tbl_size(int entry_size, int last_bdf)264285{265286 unsigned shift = PAGE_SHIFT +266266- get_order(((int)amd_iommu_last_bdf + 1) * entry_size);287287+ get_order((last_bdf + 1) * entry_size);267288268289 return 1UL << shift;269290}···267300 return amd_iommus_present;268301}269302270270-#ifdef CONFIG_IRQ_REMAP271271-static bool check_feature_on_all_iommus(u64 mask)303303+/*304304+ * Iterate through all the IOMMUs to get common EFR305305+ * masks among all IOMMUs and warn if found inconsistency.306306+ */307307+static void get_global_efr(void)272308{273273- bool ret = false;274309 struct amd_iommu *iommu;275310276311 for_each_iommu(iommu) {277277- ret = iommu_feature(iommu, mask);278278- if (!ret)279279- return false;312312+ u64 tmp = iommu->features;313313+ u64 tmp2 = iommu->features2;314314+315315+ if (list_is_first(&iommu->list, &amd_iommu_list)) {316316+ amd_iommu_efr = tmp;317317+ amd_iommu_efr2 = tmp2;318318+ continue;319319+ }320320+321321+ if (amd_iommu_efr == tmp &&322322+ amd_iommu_efr2 == tmp2)323323+ continue;324324+325325+ pr_err(FW_BUG326326+ "Found inconsistent EFR/EFR2 %#llx,%#llx (global %#llx,%#llx) on iommu%d (%04x:%02x:%02x.%01x).\n",327327+ tmp, tmp2, amd_iommu_efr, amd_iommu_efr2,328328+ iommu->index, iommu->pci_seg->id,329329+ PCI_BUS_NUM(iommu->devid), PCI_SLOT(iommu->devid),330330+ PCI_FUNC(iommu->devid));331331+332332+ amd_iommu_efr &= tmp;333333+ amd_iommu_efr2 &= tmp2;280334 }281335282282- return true;336336+ pr_info("Using global IVHD EFR:%#llx, EFR2:%#llx\n", amd_iommu_efr, amd_iommu_efr2);283337}284284-#endif338338+339339+static bool check_feature_on_all_iommus(u64 mask)340340+{341341+ return !!(amd_iommu_efr & mask);342342+}285343286344/*287345 * For IVHD type 0x11/0x40, EFR is also available via IVHD.···316324static void __init early_iommu_features_init(struct amd_iommu *iommu,317325 struct ivhd_header *h)318326{319319- if (amd_iommu_ivinfo & IOMMU_IVINFO_EFRSUP)327327+ if (amd_iommu_ivinfo & IOMMU_IVINFO_EFRSUP) {320328 iommu->features = h->efr_reg;329329+ iommu->features2 = h->efr_reg2;330330+ }321331 if (amd_iommu_ivinfo & IOMMU_IVINFO_DMA_REMAP)322332 amdr_ivrs_remap_support = true;323333}···393399 u64 start = iommu_virt_to_phys((void *)iommu->cmd_sem);394400 u64 entry = start & PM_ADDR_MASK;395401396396- if (!iommu_feature(iommu, FEATURE_SNP))402402+ if (!check_feature_on_all_iommus(FEATURE_SNP))397403 return;398404399405 /* Note:···415421static void iommu_set_device_table(struct amd_iommu *iommu)416422{417423 u64 entry;424424+ u32 dev_table_size = iommu->pci_seg->dev_table_size;425425+ void *dev_table = (void *)get_dev_table(iommu);418426419427 BUG_ON(iommu->mmio_base == NULL);420428421421- entry = iommu_virt_to_phys(amd_iommu_dev_table);429429+ entry = iommu_virt_to_phys(dev_table);422430 entry |= (dev_table_size >> 12) - 1;423431 memcpy_toio(iommu->mmio_base + MMIO_DEV_TABLE_OFFSET,424432 &entry, sizeof(entry));···553557{554558 u8 *p = (void *)h, *end = (void *)h;555559 struct ivhd_entry *dev;560560+ int last_devid = -EINVAL;556561557562 u32 ivhd_size = get_ivhd_header_size(h);558563···570573 switch (dev->type) {571574 case IVHD_DEV_ALL:572575 /* Use maximum BDF value for DEV_ALL */573573- update_last_devid(0xffff);574574- break;576576+ return 0xffff;575577 case IVHD_DEV_SELECT:576578 case IVHD_DEV_RANGE_END:577579 case IVHD_DEV_ALIAS:578580 case IVHD_DEV_EXT_SELECT:579581 /* all the above subfield types refer to device ids */580580- update_last_devid(dev->devid);582582+ if (dev->devid > last_devid)583583+ last_devid = dev->devid;581584 break;582585 default:583586 break;···587590588591 WARN_ON(p != end);589592590590- return 0;593593+ return last_devid;591594}592595593596static int __init check_ivrs_checksum(struct acpi_table_header *table)···611614 * id which we need to handle. This is the first of three functions which parse612615 * the ACPI table. So we check the checksum here.613616 */614614-static int __init find_last_devid_acpi(struct acpi_table_header *table)617617+static int __init find_last_devid_acpi(struct acpi_table_header *table, u16 pci_seg)615618{616619 u8 *p = (u8 *)table, *end = (u8 *)table;617620 struct ivhd_header *h;621621+ int last_devid, last_bdf = 0;618622619623 p += IVRS_HEADER_LENGTH;620624621625 end += table->length;622626 while (p < end) {623627 h = (struct ivhd_header *)p;624624- if (h->type == amd_iommu_target_ivhd_type) {625625- int ret = find_last_devid_from_ivhd(h);628628+ if (h->pci_seg == pci_seg &&629629+ h->type == amd_iommu_target_ivhd_type) {630630+ last_devid = find_last_devid_from_ivhd(h);626631627627- if (ret)628628- return ret;632632+ if (last_devid < 0)633633+ return -EINVAL;634634+ if (last_devid > last_bdf)635635+ last_bdf = last_devid;629636 }630637 p += h->length;631638 }632639 WARN_ON(p != end);633640634634- return 0;641641+ return last_bdf;635642}636643637644/****************************************************************************638645 *639646 * The following functions belong to the code path which parses the ACPI table640647 * the second time. In this ACPI parsing iteration we allocate IOMMU specific641641- * data structures, initialize the device/alias/rlookup table and also642642- * basically initialize the hardware.648648+ * data structures, initialize the per PCI segment device/alias/rlookup table649649+ * and also basically initialize the hardware.643650 *644651 ****************************************************************************/652652+653653+/* Allocate per PCI segment device table */654654+static inline int __init alloc_dev_table(struct amd_iommu_pci_seg *pci_seg)655655+{656656+ pci_seg->dev_table = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO | GFP_DMA32,657657+ get_order(pci_seg->dev_table_size));658658+ if (!pci_seg->dev_table)659659+ return -ENOMEM;660660+661661+ return 0;662662+}663663+664664+static inline void free_dev_table(struct amd_iommu_pci_seg *pci_seg)665665+{666666+ free_pages((unsigned long)pci_seg->dev_table,667667+ get_order(pci_seg->dev_table_size));668668+ pci_seg->dev_table = NULL;669669+}670670+671671+/* Allocate per PCI segment IOMMU rlookup table. */672672+static inline int __init alloc_rlookup_table(struct amd_iommu_pci_seg *pci_seg)673673+{674674+ pci_seg->rlookup_table = (void *)__get_free_pages(675675+ GFP_KERNEL | __GFP_ZERO,676676+ get_order(pci_seg->rlookup_table_size));677677+ if (pci_seg->rlookup_table == NULL)678678+ return -ENOMEM;679679+680680+ return 0;681681+}682682+683683+static inline void free_rlookup_table(struct amd_iommu_pci_seg *pci_seg)684684+{685685+ free_pages((unsigned long)pci_seg->rlookup_table,686686+ get_order(pci_seg->rlookup_table_size));687687+ pci_seg->rlookup_table = NULL;688688+}689689+690690+static inline int __init alloc_irq_lookup_table(struct amd_iommu_pci_seg *pci_seg)691691+{692692+ pci_seg->irq_lookup_table = (void *)__get_free_pages(693693+ GFP_KERNEL | __GFP_ZERO,694694+ get_order(pci_seg->rlookup_table_size));695695+ kmemleak_alloc(pci_seg->irq_lookup_table,696696+ pci_seg->rlookup_table_size, 1, GFP_KERNEL);697697+ if (pci_seg->irq_lookup_table == NULL)698698+ return -ENOMEM;699699+700700+ return 0;701701+}702702+703703+static inline void free_irq_lookup_table(struct amd_iommu_pci_seg *pci_seg)704704+{705705+ kmemleak_free(pci_seg->irq_lookup_table);706706+ free_pages((unsigned long)pci_seg->irq_lookup_table,707707+ get_order(pci_seg->rlookup_table_size));708708+ pci_seg->irq_lookup_table = NULL;709709+}710710+711711+static int __init alloc_alias_table(struct amd_iommu_pci_seg *pci_seg)712712+{713713+ int i;714714+715715+ pci_seg->alias_table = (void *)__get_free_pages(GFP_KERNEL,716716+ get_order(pci_seg->alias_table_size));717717+ if (!pci_seg->alias_table)718718+ return -ENOMEM;719719+720720+ /*721721+ * let all alias entries point to itself722722+ */723723+ for (i = 0; i <= pci_seg->last_bdf; ++i)724724+ pci_seg->alias_table[i] = i;725725+726726+ return 0;727727+}728728+729729+static void __init free_alias_table(struct amd_iommu_pci_seg *pci_seg)730730+{731731+ free_pages((unsigned long)pci_seg->alias_table,732732+ get_order(pci_seg->alias_table_size));733733+ pci_seg->alias_table = NULL;734734+}645735646736/*647737 * Allocates the command buffer. This buffer is per AMD IOMMU. We can···808724 void *buf = (void *)__get_free_pages(gfp, order);809725810726 if (buf &&811811- iommu_feature(iommu, FEATURE_SNP) &&727727+ check_feature_on_all_iommus(FEATURE_SNP) &&812728 set_memory_4k((unsigned long)buf, (1 << order))) {813729 free_pages((unsigned long)buf, order);814730 buf = NULL;···899815#endif900816}901817818818+#ifdef CONFIG_IRQ_REMAP902819static int iommu_ga_log_enable(struct amd_iommu *iommu)903820{904904-#ifdef CONFIG_IRQ_REMAP905821 u32 status, i;906822 u64 entry;907823908824 if (!iommu->ga_log)909825 return -EINVAL;910910-911911- /* Check if already running */912912- status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);913913- if (WARN_ON(status & (MMIO_STATUS_GALOG_RUN_MASK)))914914- return 0;915826916827 entry = iommu_virt_to_phys(iommu->ga_log) | GA_LOG_SIZE_512;917828 memcpy_toio(iommu->mmio_base + MMIO_GA_LOG_BASE_OFFSET,···931852932853 if (WARN_ON(i >= LOOP_TIMEOUT))933854 return -EINVAL;934934-#endif /* CONFIG_IRQ_REMAP */855855+935856 return 0;936857}937858938859static int iommu_init_ga_log(struct amd_iommu *iommu)939860{940940-#ifdef CONFIG_IRQ_REMAP941861 if (!AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir))942862 return 0;943863···954876err_out:955877 free_ga_log(iommu);956878 return -EINVAL;957957-#else958958- return 0;959959-#endif /* CONFIG_IRQ_REMAP */960879}880880+#endif /* CONFIG_IRQ_REMAP */961881962882static int __init alloc_cwwb_sem(struct amd_iommu *iommu)963883{···992916}993917994918/* sets a specific bit in the device table entry. */995995-static void set_dev_entry_bit(u16 devid, u8 bit)919919+static void __set_dev_entry_bit(struct dev_table_entry *dev_table,920920+ u16 devid, u8 bit)996921{997922 int i = (bit >> 6) & 0x03;998923 int _bit = bit & 0x3f;99992410001000- amd_iommu_dev_table[devid].data[i] |= (1UL << _bit);925925+ dev_table[devid].data[i] |= (1UL << _bit);1001926}100292710031003-static int get_dev_entry_bit(u16 devid, u8 bit)928928+static void set_dev_entry_bit(struct amd_iommu *iommu, u16 devid, u8 bit)929929+{930930+ struct dev_table_entry *dev_table = get_dev_table(iommu);931931+932932+ return __set_dev_entry_bit(dev_table, devid, bit);933933+}934934+935935+static int __get_dev_entry_bit(struct dev_table_entry *dev_table,936936+ u16 devid, u8 bit)1004937{1005938 int i = (bit >> 6) & 0x03;1006939 int _bit = bit & 0x3f;100794010081008- return (amd_iommu_dev_table[devid].data[i] & (1UL << _bit)) >> _bit;941941+ return (dev_table[devid].data[i] & (1UL << _bit)) >> _bit;1009942}101094310111011-10121012-static bool copy_device_table(void)944944+static int get_dev_entry_bit(struct amd_iommu *iommu, u16 devid, u8 bit)1013945{10141014- u64 int_ctl, int_tab_len, entry = 0, last_entry = 0;946946+ struct dev_table_entry *dev_table = get_dev_table(iommu);947947+948948+ return __get_dev_entry_bit(dev_table, devid, bit);949949+}950950+951951+static bool __copy_device_table(struct amd_iommu *iommu)952952+{953953+ u64 int_ctl, int_tab_len, entry = 0;954954+ struct amd_iommu_pci_seg *pci_seg = iommu->pci_seg;1015955 struct dev_table_entry *old_devtb = NULL;1016956 u32 lo, hi, devid, old_devtb_size;1017957 phys_addr_t old_devtb_phys;10181018- struct amd_iommu *iommu;1019958 u16 dom_id, dte_v, irq_v;1020959 gfp_t gfp_flag;1021960 u64 tmp;102296110231023- if (!amd_iommu_pre_enabled)962962+ /* Each IOMMU use separate device table with the same size */963963+ lo = readl(iommu->mmio_base + MMIO_DEV_TABLE_OFFSET);964964+ hi = readl(iommu->mmio_base + MMIO_DEV_TABLE_OFFSET + 4);965965+ entry = (((u64) hi) << 32) + lo;966966+967967+ old_devtb_size = ((entry & ~PAGE_MASK) + 1) << 12;968968+ if (old_devtb_size != pci_seg->dev_table_size) {969969+ pr_err("The device table size of IOMMU:%d is not expected!\n",970970+ iommu->index);1024971 return false;10251025-10261026- pr_warn("Translation is already enabled - trying to copy translation structures\n");10271027- for_each_iommu(iommu) {10281028- /* All IOMMUs should use the same device table with the same size */10291029- lo = readl(iommu->mmio_base + MMIO_DEV_TABLE_OFFSET);10301030- hi = readl(iommu->mmio_base + MMIO_DEV_TABLE_OFFSET + 4);10311031- entry = (((u64) hi) << 32) + lo;10321032- if (last_entry && last_entry != entry) {10331033- pr_err("IOMMU:%d should use the same dev table as others!\n",10341034- iommu->index);10351035- return false;10361036- }10371037- last_entry = entry;10381038-10391039- old_devtb_size = ((entry & ~PAGE_MASK) + 1) << 12;10401040- if (old_devtb_size != dev_table_size) {10411041- pr_err("The device table size of IOMMU:%d is not expected!\n",10421042- iommu->index);10431043- return false;10441044- }1045972 }10469731047974 /*···1060981 }1061982 old_devtb = (cc_platform_has(CC_ATTR_HOST_MEM_ENCRYPT) && is_kdump_kernel())1062983 ? (__force void *)ioremap_encrypted(old_devtb_phys,10631063- dev_table_size)10641064- : memremap(old_devtb_phys, dev_table_size, MEMREMAP_WB);984984+ pci_seg->dev_table_size)985985+ : memremap(old_devtb_phys, pci_seg->dev_table_size, MEMREMAP_WB);10659861066987 if (!old_devtb)1067988 return false;10689891069990 gfp_flag = GFP_KERNEL | __GFP_ZERO | GFP_DMA32;10701070- old_dev_tbl_cpy = (void *)__get_free_pages(gfp_flag,10711071- get_order(dev_table_size));10721072- if (old_dev_tbl_cpy == NULL) {991991+ pci_seg->old_dev_tbl_cpy = (void *)__get_free_pages(gfp_flag,992992+ get_order(pci_seg->dev_table_size));993993+ if (pci_seg->old_dev_tbl_cpy == NULL) {1073994 pr_err("Failed to allocate memory for copying old device table!\n");1074995 memunmap(old_devtb);1075996 return false;1076997 }107799810781078- for (devid = 0; devid <= amd_iommu_last_bdf; ++devid) {10791079- old_dev_tbl_cpy[devid] = old_devtb[devid];999999+ for (devid = 0; devid <= pci_seg->last_bdf; ++devid) {10001000+ pci_seg->old_dev_tbl_cpy[devid] = old_devtb[devid];10801001 dom_id = old_devtb[devid].data[1] & DEV_DOMID_MASK;10811002 dte_v = old_devtb[devid].data[0] & DTE_FLAG_V;1082100310831004 if (dte_v && dom_id) {10841084- old_dev_tbl_cpy[devid].data[0] = old_devtb[devid].data[0];10851085- old_dev_tbl_cpy[devid].data[1] = old_devtb[devid].data[1];10051005+ pci_seg->old_dev_tbl_cpy[devid].data[0] = old_devtb[devid].data[0];10061006+ pci_seg->old_dev_tbl_cpy[devid].data[1] = old_devtb[devid].data[1];10861007 __set_bit(dom_id, amd_iommu_pd_alloc_bitmap);10871008 /* If gcr3 table existed, mask it out */10881009 if (old_devtb[devid].data[0] & DTE_FLAG_GV) {10891010 tmp = DTE_GCR3_VAL_B(~0ULL) << DTE_GCR3_SHIFT_B;10901011 tmp |= DTE_GCR3_VAL_C(~0ULL) << DTE_GCR3_SHIFT_C;10911091- old_dev_tbl_cpy[devid].data[1] &= ~tmp;10121012+ pci_seg->old_dev_tbl_cpy[devid].data[1] &= ~tmp;10921013 tmp = DTE_GCR3_VAL_A(~0ULL) << DTE_GCR3_SHIFT_A;10931014 tmp |= DTE_FLAG_GV;10941094- old_dev_tbl_cpy[devid].data[0] &= ~tmp;10151015+ pci_seg->old_dev_tbl_cpy[devid].data[0] &= ~tmp;10951016 }10961017 }10971018···11061027 return false;11071028 }1108102911091109- old_dev_tbl_cpy[devid].data[2] = old_devtb[devid].data[2];10301030+ pci_seg->old_dev_tbl_cpy[devid].data[2] = old_devtb[devid].data[2];11101031 }11111032 }11121033 memunmap(old_devtb);···11141035 return true;11151036}1116103711171117-void amd_iommu_apply_erratum_63(u16 devid)10381038+static bool copy_device_table(void)10391039+{10401040+ struct amd_iommu *iommu;10411041+ struct amd_iommu_pci_seg *pci_seg;10421042+10431043+ if (!amd_iommu_pre_enabled)10441044+ return false;10451045+10461046+ pr_warn("Translation is already enabled - trying to copy translation structures\n");10471047+10481048+ /*10491049+ * All IOMMUs within PCI segment shares common device table.10501050+ * Hence copy device table only once per PCI segment.10511051+ */10521052+ for_each_pci_segment(pci_seg) {10531053+ for_each_iommu(iommu) {10541054+ if (pci_seg->id != iommu->pci_seg->id)10551055+ continue;10561056+ if (!__copy_device_table(iommu))10571057+ return false;10581058+ break;10591059+ }10601060+ }10611061+10621062+ return true;10631063+}10641064+10651065+void amd_iommu_apply_erratum_63(struct amd_iommu *iommu, u16 devid)11181066{11191067 int sysmgt;1120106811211121- sysmgt = get_dev_entry_bit(devid, DEV_ENTRY_SYSMGT1) |11221122- (get_dev_entry_bit(devid, DEV_ENTRY_SYSMGT2) << 1);10691069+ sysmgt = get_dev_entry_bit(iommu, devid, DEV_ENTRY_SYSMGT1) |10701070+ (get_dev_entry_bit(iommu, devid, DEV_ENTRY_SYSMGT2) << 1);1123107111241072 if (sysmgt == 0x01)11251125- set_dev_entry_bit(devid, DEV_ENTRY_IW);11261126-}11271127-11281128-/* Writes the specific IOMMU for a device into the rlookup table */11291129-static void __init set_iommu_for_device(struct amd_iommu *iommu, u16 devid)11301130-{11311131- amd_iommu_rlookup_table[devid] = iommu;10731073+ set_dev_entry_bit(iommu, devid, DEV_ENTRY_IW);11321074}1133107511341076/*···11601060 u16 devid, u32 flags, u32 ext_flags)11611061{11621062 if (flags & ACPI_DEVFLAG_INITPASS)11631163- set_dev_entry_bit(devid, DEV_ENTRY_INIT_PASS);10631063+ set_dev_entry_bit(iommu, devid, DEV_ENTRY_INIT_PASS);11641064 if (flags & ACPI_DEVFLAG_EXTINT)11651165- set_dev_entry_bit(devid, DEV_ENTRY_EINT_PASS);10651065+ set_dev_entry_bit(iommu, devid, DEV_ENTRY_EINT_PASS);11661066 if (flags & ACPI_DEVFLAG_NMI)11671167- set_dev_entry_bit(devid, DEV_ENTRY_NMI_PASS);10671067+ set_dev_entry_bit(iommu, devid, DEV_ENTRY_NMI_PASS);11681068 if (flags & ACPI_DEVFLAG_SYSMGT1)11691169- set_dev_entry_bit(devid, DEV_ENTRY_SYSMGT1);10691069+ set_dev_entry_bit(iommu, devid, DEV_ENTRY_SYSMGT1);11701070 if (flags & ACPI_DEVFLAG_SYSMGT2)11711171- set_dev_entry_bit(devid, DEV_ENTRY_SYSMGT2);10711071+ set_dev_entry_bit(iommu, devid, DEV_ENTRY_SYSMGT2);11721072 if (flags & ACPI_DEVFLAG_LINT0)11731173- set_dev_entry_bit(devid, DEV_ENTRY_LINT0_PASS);10731073+ set_dev_entry_bit(iommu, devid, DEV_ENTRY_LINT0_PASS);11741074 if (flags & ACPI_DEVFLAG_LINT1)11751175- set_dev_entry_bit(devid, DEV_ENTRY_LINT1_PASS);10751075+ set_dev_entry_bit(iommu, devid, DEV_ENTRY_LINT1_PASS);1176107611771177- amd_iommu_apply_erratum_63(devid);10771077+ amd_iommu_apply_erratum_63(iommu, devid);1178107811791179- set_iommu_for_device(iommu, devid);10791079+ amd_iommu_set_rlookup_table(iommu, devid);11801080}1181108111821182-int __init add_special_device(u8 type, u8 id, u16 *devid, bool cmd_line)10821082+int __init add_special_device(u8 type, u8 id, u32 *devid, bool cmd_line)11831083{11841084 struct devid_map *entry;11851085 struct list_head *list;···12161116 return 0;12171117}1218111812191219-static int __init add_acpi_hid_device(u8 *hid, u8 *uid, u16 *devid,11191119+static int __init add_acpi_hid_device(u8 *hid, u8 *uid, u32 *devid,12201120 bool cmd_line)12211121{12221122 struct acpihid_map_entry *entry;···12951195{12961196 u8 *p = (u8 *)h;12971197 u8 *end = p, flags = 0;12981298- u16 devid = 0, devid_start = 0, devid_to = 0;11981198+ u16 devid = 0, devid_start = 0, devid_to = 0, seg_id;12991199 u32 dev_i, ext_flags = 0;13001200 bool alias = false;13011201 struct ivhd_entry *e;12021202+ struct amd_iommu_pci_seg *pci_seg = iommu->pci_seg;13021203 u32 ivhd_size;13031204 int ret;13041205···1331123013321231 while (p < end) {13331232 e = (struct ivhd_entry *)p;12331233+ seg_id = pci_seg->id;12341234+13341235 switch (e->type) {13351236 case IVHD_DEV_ALL:1336123713371238 DUMP_printk(" DEV_ALL\t\t\tflags: %02x\n", e->flags);1338123913391339- for (dev_i = 0; dev_i <= amd_iommu_last_bdf; ++dev_i)12401240+ for (dev_i = 0; dev_i <= pci_seg->last_bdf; ++dev_i)13401241 set_dev_entry_from_acpi(iommu, dev_i, e->flags, 0);13411242 break;13421243 case IVHD_DEV_SELECT:1343124413441344- DUMP_printk(" DEV_SELECT\t\t\t devid: %02x:%02x.%x "12451245+ DUMP_printk(" DEV_SELECT\t\t\t devid: %04x:%02x:%02x.%x "13451246 "flags: %02x\n",13461346- PCI_BUS_NUM(e->devid),12471247+ seg_id, PCI_BUS_NUM(e->devid),13471248 PCI_SLOT(e->devid),13481249 PCI_FUNC(e->devid),13491250 e->flags);···13561253 case IVHD_DEV_SELECT_RANGE_START:1357125413581255 DUMP_printk(" DEV_SELECT_RANGE_START\t "13591359- "devid: %02x:%02x.%x flags: %02x\n",13601360- PCI_BUS_NUM(e->devid),12561256+ "devid: %04x:%02x:%02x.%x flags: %02x\n",12571257+ seg_id, PCI_BUS_NUM(e->devid),13611258 PCI_SLOT(e->devid),13621259 PCI_FUNC(e->devid),13631260 e->flags);···13691266 break;13701267 case IVHD_DEV_ALIAS:1371126813721372- DUMP_printk(" DEV_ALIAS\t\t\t devid: %02x:%02x.%x "12691269+ DUMP_printk(" DEV_ALIAS\t\t\t devid: %04x:%02x:%02x.%x "13731270 "flags: %02x devid_to: %02x:%02x.%x\n",13741374- PCI_BUS_NUM(e->devid),12711271+ seg_id, PCI_BUS_NUM(e->devid),13751272 PCI_SLOT(e->devid),13761273 PCI_FUNC(e->devid),13771274 e->flags,···13831280 devid_to = e->ext >> 8;13841281 set_dev_entry_from_acpi(iommu, devid , e->flags, 0);13851282 set_dev_entry_from_acpi(iommu, devid_to, e->flags, 0);13861386- amd_iommu_alias_table[devid] = devid_to;12831283+ pci_seg->alias_table[devid] = devid_to;13871284 break;13881285 case IVHD_DEV_ALIAS_RANGE:1389128613901287 DUMP_printk(" DEV_ALIAS_RANGE\t\t "13911391- "devid: %02x:%02x.%x flags: %02x "13921392- "devid_to: %02x:%02x.%x\n",13931393- PCI_BUS_NUM(e->devid),12881288+ "devid: %04x:%02x:%02x.%x flags: %02x "12891289+ "devid_to: %04x:%02x:%02x.%x\n",12901290+ seg_id, PCI_BUS_NUM(e->devid),13941291 PCI_SLOT(e->devid),13951292 PCI_FUNC(e->devid),13961293 e->flags,13971397- PCI_BUS_NUM(e->ext >> 8),12941294+ seg_id, PCI_BUS_NUM(e->ext >> 8),13981295 PCI_SLOT(e->ext >> 8),13991296 PCI_FUNC(e->ext >> 8));14001297···14061303 break;14071304 case IVHD_DEV_EXT_SELECT:1408130514091409- DUMP_printk(" DEV_EXT_SELECT\t\t devid: %02x:%02x.%x "13061306+ DUMP_printk(" DEV_EXT_SELECT\t\t devid: %04x:%02x:%02x.%x "14101307 "flags: %02x ext: %08x\n",14111411- PCI_BUS_NUM(e->devid),13081308+ seg_id, PCI_BUS_NUM(e->devid),14121309 PCI_SLOT(e->devid),14131310 PCI_FUNC(e->devid),14141311 e->flags, e->ext);···14201317 case IVHD_DEV_EXT_SELECT_RANGE:1421131814221319 DUMP_printk(" DEV_EXT_SELECT_RANGE\t devid: "14231423- "%02x:%02x.%x flags: %02x ext: %08x\n",14241424- PCI_BUS_NUM(e->devid),13201320+ "%04x:%02x:%02x.%x flags: %02x ext: %08x\n",13211321+ seg_id, PCI_BUS_NUM(e->devid),14251322 PCI_SLOT(e->devid),14261323 PCI_FUNC(e->devid),14271324 e->flags, e->ext);···14331330 break;14341331 case IVHD_DEV_RANGE_END:1435133214361436- DUMP_printk(" DEV_RANGE_END\t\t devid: %02x:%02x.%x\n",14371437- PCI_BUS_NUM(e->devid),13331333+ DUMP_printk(" DEV_RANGE_END\t\t devid: %04x:%02x:%02x.%x\n",13341334+ seg_id, PCI_BUS_NUM(e->devid),14381335 PCI_SLOT(e->devid),14391336 PCI_FUNC(e->devid));1440133714411338 devid = e->devid;14421339 for (dev_i = devid_start; dev_i <= devid; ++dev_i) {14431340 if (alias) {14441444- amd_iommu_alias_table[dev_i] = devid_to;13411341+ pci_seg->alias_table[dev_i] = devid_to;14451342 set_dev_entry_from_acpi(iommu,14461343 devid_to, flags, ext_flags);14471344 }···14521349 case IVHD_DEV_SPECIAL: {14531350 u8 handle, type;14541351 const char *var;14551455- u16 devid;13521352+ u32 devid;14561353 int ret;1457135414581355 handle = e->ext & 0xff;14591459- devid = (e->ext >> 8) & 0xffff;13561356+ devid = PCI_SEG_DEVID_TO_SBDF(seg_id, (e->ext >> 8));14601357 type = (e->ext >> 24) & 0xff;1461135814621359 if (type == IVHD_SPECIAL_IOAPIC)···14661363 else14671364 var = "UNKNOWN";1468136514691469- DUMP_printk(" DEV_SPECIAL(%s[%d])\t\tdevid: %02x:%02x.%x\n",13661366+ DUMP_printk(" DEV_SPECIAL(%s[%d])\t\tdevid: %04x:%02x:%02x.%x\n",14701367 var, (int)handle,14711471- PCI_BUS_NUM(devid),13681368+ seg_id, PCI_BUS_NUM(devid),14721369 PCI_SLOT(devid),14731370 PCI_FUNC(devid));14741371···14861383 break;14871384 }14881385 case IVHD_DEV_ACPI_HID: {14891489- u16 devid;13861386+ u32 devid;14901387 u8 hid[ACPIHID_HID_LEN];14911388 u8 uid[ACPIHID_UID_LEN];14921389 int ret;···15291426 break;15301427 }1531142815321532- devid = e->devid;15331533- DUMP_printk(" DEV_ACPI_HID(%s[%s])\t\tdevid: %02x:%02x.%x\n",15341534- hid, uid,14291429+ devid = PCI_SEG_DEVID_TO_SBDF(seg_id, e->devid);14301430+ DUMP_printk(" DEV_ACPI_HID(%s[%s])\t\tdevid: %04x:%02x:%02x.%x\n",14311431+ hid, uid, seg_id,15351432 PCI_BUS_NUM(devid),15361433 PCI_SLOT(devid),15371434 PCI_FUNC(devid));···15591456 }1560145715611458 return 0;14591459+}14601460+14611461+/* Allocate PCI segment data structure */14621462+static struct amd_iommu_pci_seg *__init alloc_pci_segment(u16 id,14631463+ struct acpi_table_header *ivrs_base)14641464+{14651465+ struct amd_iommu_pci_seg *pci_seg;14661466+ int last_bdf;14671467+14681468+ /*14691469+ * First parse ACPI tables to find the largest Bus/Dev/Func we need to14701470+ * handle in this PCI segment. Upon this information the shared data14711471+ * structures for the PCI segments in the system will be allocated.14721472+ */14731473+ last_bdf = find_last_devid_acpi(ivrs_base, id);14741474+ if (last_bdf < 0)14751475+ return NULL;14761476+14771477+ pci_seg = kzalloc(sizeof(struct amd_iommu_pci_seg), GFP_KERNEL);14781478+ if (pci_seg == NULL)14791479+ return NULL;14801480+14811481+ pci_seg->last_bdf = last_bdf;14821482+ DUMP_printk("PCI segment : 0x%0x, last bdf : 0x%04x\n", id, last_bdf);14831483+ pci_seg->dev_table_size = tbl_size(DEV_TABLE_ENTRY_SIZE, last_bdf);14841484+ pci_seg->alias_table_size = tbl_size(ALIAS_TABLE_ENTRY_SIZE, last_bdf);14851485+ pci_seg->rlookup_table_size = tbl_size(RLOOKUP_TABLE_ENTRY_SIZE, last_bdf);14861486+14871487+ pci_seg->id = id;14881488+ init_llist_head(&pci_seg->dev_data_list);14891489+ INIT_LIST_HEAD(&pci_seg->unity_map);14901490+ list_add_tail(&pci_seg->list, &amd_iommu_pci_seg_list);14911491+14921492+ if (alloc_dev_table(pci_seg))14931493+ return NULL;14941494+ if (alloc_alias_table(pci_seg))14951495+ return NULL;14961496+ if (alloc_rlookup_table(pci_seg))14971497+ return NULL;14981498+14991499+ return pci_seg;15001500+}15011501+15021502+static struct amd_iommu_pci_seg *__init get_pci_segment(u16 id,15031503+ struct acpi_table_header *ivrs_base)15041504+{15051505+ struct amd_iommu_pci_seg *pci_seg;15061506+15071507+ for_each_pci_segment(pci_seg) {15081508+ if (pci_seg->id == id)15091509+ return pci_seg;15101510+ }15111511+15121512+ return alloc_pci_segment(id, ivrs_base);15131513+}15141514+15151515+static void __init free_pci_segments(void)15161516+{15171517+ struct amd_iommu_pci_seg *pci_seg, *next;15181518+15191519+ for_each_pci_segment_safe(pci_seg, next) {15201520+ list_del(&pci_seg->list);15211521+ free_irq_lookup_table(pci_seg);15221522+ free_rlookup_table(pci_seg);15231523+ free_alias_table(pci_seg);15241524+ free_dev_table(pci_seg);15251525+ kfree(pci_seg);15261526+ }15621527}1563152815641529static void __init free_iommu_one(struct amd_iommu *iommu)···17131542 * together and also allocates the command buffer and programs the17141543 * hardware. It does NOT enable the IOMMU. This is done afterwards.17151544 */17161716-static int __init init_iommu_one(struct amd_iommu *iommu, struct ivhd_header *h)15451545+static int __init init_iommu_one(struct amd_iommu *iommu, struct ivhd_header *h,15461546+ struct acpi_table_header *ivrs_base)17171547{17181718- int ret;15481548+ struct amd_iommu_pci_seg *pci_seg;15491549+15501550+ pci_seg = get_pci_segment(h->pci_seg, ivrs_base);15511551+ if (pci_seg == NULL)15521552+ return -ENOMEM;15531553+ iommu->pci_seg = pci_seg;1719155417201555 raw_spin_lock_init(&iommu->lock);17211556 iommu->cmd_sem_val = 0;···17431566 */17441567 iommu->devid = h->devid;17451568 iommu->cap_ptr = h->cap_ptr;17461746- iommu->pci_seg = h->pci_seg;17471569 iommu->mmio_phys = h->mmio_phys;1748157017491571 switch (h->type) {···17971621 if (!iommu->mmio_base)17981622 return -ENOMEM;1799162316241624+ return init_iommu_from_acpi(iommu, h);16251625+}16261626+16271627+static int __init init_iommu_one_late(struct amd_iommu *iommu)16281628+{16291629+ int ret;16301630+18001631 if (alloc_cwwb_sem(iommu))18011632 return -ENOMEM;18021633···18251642 if (amd_iommu_pre_enabled)18261643 amd_iommu_pre_enabled = translation_pre_enabled(iommu);1827164418281828- ret = init_iommu_from_acpi(iommu, h);18291829- if (ret)18301830- return ret;18311831-18321645 if (amd_iommu_irq_remap) {18331646 ret = amd_iommu_create_irq_domain(iommu);18341647 if (ret)···18351656 * Make sure IOMMU is not considered to translate itself. The IVRS18361657 * table tells us so, but this is a lie!18371658 */18381838- amd_iommu_rlookup_table[iommu->devid] = NULL;16591659+ iommu->pci_seg->rlookup_table[iommu->devid] = NULL;1839166018401661 return 0;18411662}···18801701 end += table->length;18811702 p += IVRS_HEADER_LENGTH;1882170317041704+ /* Phase 1: Process all IVHD blocks */18831705 while (p < end) {18841706 h = (struct ivhd_header *)p;18851707 if (*p == amd_iommu_target_ivhd_type) {1886170818871887- DUMP_printk("device: %02x:%02x.%01x cap: %04x "18881888- "seg: %d flags: %01x info %04x\n",18891889- PCI_BUS_NUM(h->devid), PCI_SLOT(h->devid),18901890- PCI_FUNC(h->devid), h->cap_ptr,18911891- h->pci_seg, h->flags, h->info);17091709+ DUMP_printk("device: %04x:%02x:%02x.%01x cap: %04x "17101710+ "flags: %01x info %04x\n",17111711+ h->pci_seg, PCI_BUS_NUM(h->devid),17121712+ PCI_SLOT(h->devid), PCI_FUNC(h->devid),17131713+ h->cap_ptr, h->flags, h->info);18921714 DUMP_printk(" mmio-addr: %016llx\n",18931715 h->mmio_phys);18941716···18971717 if (iommu == NULL)18981718 return -ENOMEM;1899171919001900- ret = init_iommu_one(iommu, h);17201720+ ret = init_iommu_one(iommu, h, table);19011721 if (ret)19021722 return ret;19031723 }···1905172519061726 }19071727 WARN_ON(p != end);17281728+17291729+ /* Phase 2 : Early feature support check */17301730+ get_global_efr();17311731+17321732+ /* Phase 3 : Enabling IOMMU features */17331733+ for_each_iommu(iommu) {17341734+ ret = init_iommu_one_late(iommu);17351735+ if (ret)17361736+ return ret;17371737+ }1908173819091739 return 0;19101740}···19521762 char *buf)19531763{19541764 struct amd_iommu *iommu = dev_to_amd_iommu(dev);19551955- return sprintf(buf, "%llx\n", iommu->features);17651765+ return sprintf(buf, "%llx:%llx\n", iommu->features2, iommu->features);19561766}19571767static DEVICE_ATTR(features, S_IRUGO, amd_iommu_show_features, NULL);19581768···19791789 */19801790static void __init late_iommu_features_init(struct amd_iommu *iommu)19811791{19821982- u64 features;17921792+ u64 features, features2;1983179319841794 if (!(iommu->cap & (1 << IOMMU_CAP_EFR)))19851795 return;1986179619871797 /* read extended feature bits */19881798 features = readq(iommu->mmio_base + MMIO_EXT_FEATURES);17991799+ features2 = readq(iommu->mmio_base + MMIO_EXT_FEATURES2);1989180019901801 if (!iommu->features) {19911802 iommu->features = features;18031803+ iommu->features2 = features2;19921804 return;19931805 }19941806···19981806 * Sanity check and warn if EFR values from19991807 * IVHD and MMIO conflict.20001808 */20012001- if (features != iommu->features)20022002- pr_warn(FW_WARN "EFR mismatch. Use IVHD EFR (%#llx : %#llx).\n",20032003- features, iommu->features);18091809+ if (features != iommu->features ||18101810+ features2 != iommu->features2) {18111811+ pr_warn(FW_WARN18121812+ "EFR mismatch. Use IVHD EFR (%#llx : %#llx), EFR2 (%#llx : %#llx).\n",18131813+ features, iommu->features,18141814+ features2, iommu->features2);18151815+ }20041816}2005181720061818static int __init iommu_init_pci(struct amd_iommu *iommu)···20121816 int cap_ptr = iommu->cap_ptr;20131817 int ret;2014181820152015- iommu->dev = pci_get_domain_bus_and_slot(0, PCI_BUS_NUM(iommu->devid),18191819+ iommu->dev = pci_get_domain_bus_and_slot(iommu->pci_seg->id,18201820+ PCI_BUS_NUM(iommu->devid),20161821 iommu->devid & 0xff);20171822 if (!iommu->dev)20181823 return -ENODEV;···20601863 if (iommu_feature(iommu, FEATURE_PPR) && alloc_ppr_log(iommu))20611864 return -ENOMEM;2062186520632063- ret = iommu_init_ga_log(iommu);20642064- if (ret)20652065- return ret;20662066-20671866 if (iommu->cap & (1UL << IOMMU_CAP_NPCACHE)) {20681867 pr_info("Using strict mode due to virtualization\n");20691868 iommu_set_dma_strict();···20721879 int i, j;2073188020741881 iommu->root_pdev =20752075- pci_get_domain_bus_and_slot(0, iommu->dev->bus->number,18821882+ pci_get_domain_bus_and_slot(iommu->pci_seg->id,18831883+ iommu->dev->bus->number,20761884 PCI_DEVFN(0, 0));2077188520781886 /*···21001906 amd_iommu_erratum_746_workaround(iommu);21011907 amd_iommu_ats_write_check_workaround(iommu);2102190821032103- iommu_device_sysfs_add(&iommu->iommu, &iommu->dev->dev,19091909+ ret = iommu_device_sysfs_add(&iommu->iommu, &iommu->dev->dev,21041910 amd_iommu_groups, "ivhd%d", iommu->index);19111911+ if (ret)19121912+ return ret;19131913+21051914 iommu_device_register(&iommu->iommu, &amd_iommu_ops, NULL);2106191521071916 return pci_enable_device(iommu->dev);···21251928 pci_info(pdev, "Found IOMMU cap 0x%x\n", iommu->cap_ptr);2126192921271930 if (iommu->cap & (1 << IOMMU_CAP_EFR)) {21282128- pr_info("Extended features (%#llx):", iommu->features);19311931+ pr_info("Extended features (%#llx, %#llx):", iommu->features, iommu->features2);2129193221301933 for (i = 0; i < ARRAY_SIZE(feat_str); ++i) {21311934 if (iommu_feature(iommu, (1ULL << i)))···21351938 if (iommu->features & FEATURE_GAM_VAPIC)21361939 pr_cont(" GA_vAPIC");2137194019411941+ if (iommu->features & FEATURE_SNP)19421942+ pr_cont(" SNP");19431943+21381944 pr_cont("\n");21391945 }21401946 }21411947 if (irq_remapping_enabled) {21421948 pr_info("Interrupt remapping enabled\n");21432143- if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir))21442144- pr_info("Virtual APIC enabled\n");21451949 if (amd_iommu_xt_mode == IRQ_REMAP_X2APIC_MODE)21461950 pr_info("X2APIC enabled\n");21471951 }···21511953static int __init amd_iommu_init_pci(void)21521954{21531955 struct amd_iommu *iommu;19561956+ struct amd_iommu_pci_seg *pci_seg;21541957 int ret;2155195821561959 for_each_iommu(iommu) {···21821983 goto out;21831984 }2184198521852185- init_device_table_dma();19861986+ for_each_pci_segment(pci_seg)19871987+ init_device_table_dma(pci_seg);2186198821871989 for_each_iommu(iommu)21881990 iommu_flush_all_caches(iommu);···2432223224332233 if (iommu->ppr_log != NULL)24342234 iommu_feature_enable(iommu, CONTROL_PPRINT_EN);24352435-24362436- iommu_ga_log_enable(iommu);24372437-24382235 return 0;24392236}24402237···24462249static void __init free_unity_maps(void)24472250{24482251 struct unity_map_entry *entry, *next;22522252+ struct amd_iommu_pci_seg *p, *pci_seg;2449225324502450- list_for_each_entry_safe(entry, next, &amd_iommu_unity_map, list) {24512451- list_del(&entry->list);24522452- kfree(entry);22542254+ for_each_pci_segment_safe(pci_seg, p) {22552255+ list_for_each_entry_safe(entry, next, &pci_seg->unity_map, list) {22562256+ list_del(&entry->list);22572257+ kfree(entry);22582258+ }24532259 }24542260}2455226124562262/* called for unity map ACPI definition */24572457-static int __init init_unity_map_range(struct ivmd_header *m)22632263+static int __init init_unity_map_range(struct ivmd_header *m,22642264+ struct acpi_table_header *ivrs_base)24582265{24592266 struct unity_map_entry *e = NULL;22672267+ struct amd_iommu_pci_seg *pci_seg;24602268 char *s;22692269+22702270+ pci_seg = get_pci_segment(m->pci_seg, ivrs_base);22712271+ if (pci_seg == NULL)22722272+ return -ENOMEM;2461227324622274 e = kzalloc(sizeof(*e), GFP_KERNEL);24632275 if (e == NULL)···24832277 case ACPI_IVMD_TYPE_ALL:24842278 s = "IVMD_TYPE_ALL\t\t";24852279 e->devid_start = 0;24862486- e->devid_end = amd_iommu_last_bdf;22802280+ e->devid_end = pci_seg->last_bdf;24872281 break;24882282 case ACPI_IVMD_TYPE_RANGE:24892283 s = "IVMD_TYPE_RANGE\t\t";···25052299 if (m->flags & IVMD_FLAG_EXCL_RANGE)25062300 e->prot = (IVMD_FLAG_IW | IVMD_FLAG_IR) >> 1;2507230125082508- DUMP_printk("%s devid_start: %02x:%02x.%x devid_end: %02x:%02x.%x"25092509- " range_start: %016llx range_end: %016llx flags: %x\n", s,23022302+ DUMP_printk("%s devid_start: %04x:%02x:%02x.%x devid_end: "23032303+ "%04x:%02x:%02x.%x range_start: %016llx range_end: %016llx"23042304+ " flags: %x\n", s, m->pci_seg,25102305 PCI_BUS_NUM(e->devid_start), PCI_SLOT(e->devid_start),25112511- PCI_FUNC(e->devid_start), PCI_BUS_NUM(e->devid_end),23062306+ PCI_FUNC(e->devid_start), m->pci_seg,23072307+ PCI_BUS_NUM(e->devid_end),25122308 PCI_SLOT(e->devid_end), PCI_FUNC(e->devid_end),25132309 e->address_start, e->address_end, m->flags);2514231025152515- list_add_tail(&e->list, &amd_iommu_unity_map);23112311+ list_add_tail(&e->list, &pci_seg->unity_map);2516231225172313 return 0;25182314}···25312323 while (p < end) {25322324 m = (struct ivmd_header *)p;25332325 if (m->flags & (IVMD_FLAG_UNITY_MAP | IVMD_FLAG_EXCL_RANGE))25342534- init_unity_map_range(m);23262326+ init_unity_map_range(m, table);2535232725362328 p += m->length;25372329 }···25422334/*25432335 * Init the device table to not allow DMA access for devices25442336 */25452545-static void init_device_table_dma(void)23372337+static void init_device_table_dma(struct amd_iommu_pci_seg *pci_seg)25462338{25472339 u32 devid;23402340+ struct dev_table_entry *dev_table = pci_seg->dev_table;2548234125492549- for (devid = 0; devid <= amd_iommu_last_bdf; ++devid) {25502550- set_dev_entry_bit(devid, DEV_ENTRY_VALID);25512551- set_dev_entry_bit(devid, DEV_ENTRY_TRANSLATION);23422342+ if (dev_table == NULL)23432343+ return;23442344+23452345+ for (devid = 0; devid <= pci_seg->last_bdf; ++devid) {23462346+ __set_dev_entry_bit(dev_table, devid, DEV_ENTRY_VALID);23472347+ if (!amd_iommu_snp_en)23482348+ __set_dev_entry_bit(dev_table, devid, DEV_ENTRY_TRANSLATION);25522349 }25532350}2554235125552555-static void __init uninit_device_table_dma(void)23522352+static void __init uninit_device_table_dma(struct amd_iommu_pci_seg *pci_seg)25562353{25572354 u32 devid;23552355+ struct dev_table_entry *dev_table = pci_seg->dev_table;2558235625592559- for (devid = 0; devid <= amd_iommu_last_bdf; ++devid) {25602560- amd_iommu_dev_table[devid].data[0] = 0ULL;25612561- amd_iommu_dev_table[devid].data[1] = 0ULL;23572357+ if (dev_table == NULL)23582358+ return;23592359+23602360+ for (devid = 0; devid <= pci_seg->last_bdf; ++devid) {23612361+ dev_table[devid].data[0] = 0ULL;23622362+ dev_table[devid].data[1] = 0ULL;25622363 }25632364}2564236525652366static void init_device_table(void)25662367{23682368+ struct amd_iommu_pci_seg *pci_seg;25672369 u32 devid;2568237025692371 if (!amd_iommu_irq_remap)25702372 return;2571237325722572- for (devid = 0; devid <= amd_iommu_last_bdf; ++devid)25732573- set_dev_entry_bit(devid, DEV_ENTRY_IRQ_TBL_EN);23742374+ for_each_pci_segment(pci_seg) {23752375+ for (devid = 0; devid <= pci_seg->last_bdf; ++devid)23762376+ __set_dev_entry_bit(pci_seg->dev_table,23772377+ devid, DEV_ENTRY_IRQ_TBL_EN);23782378+ }25742379}2575238025762381static void iommu_init_flags(struct amd_iommu *iommu)···26612440#ifdef CONFIG_IRQ_REMAP26622441 switch (amd_iommu_guest_ir) {26632442 case AMD_IOMMU_GUEST_IR_VAPIC:26642664- iommu_feature_enable(iommu, CONTROL_GAM_EN);26652665- fallthrough;26662443 case AMD_IOMMU_GUEST_IR_LEGACY_GA:26672444 iommu_feature_enable(iommu, CONTROL_GA_EN);26682445 iommu->irte_ops = &irte_128_ops;···26972478static void early_enable_iommus(void)26982479{26992480 struct amd_iommu *iommu;27002700-24812481+ struct amd_iommu_pci_seg *pci_seg;2701248227022483 if (!copy_device_table()) {27032484 /*···27072488 */27082489 if (amd_iommu_pre_enabled)27092490 pr_err("Failed to copy DEV table from previous kernel.\n");27102710- if (old_dev_tbl_cpy != NULL)27112711- free_pages((unsigned long)old_dev_tbl_cpy,27122712- get_order(dev_table_size));24912491+24922492+ for_each_pci_segment(pci_seg) {24932493+ if (pci_seg->old_dev_tbl_cpy != NULL) {24942494+ free_pages((unsigned long)pci_seg->old_dev_tbl_cpy,24952495+ get_order(pci_seg->dev_table_size));24962496+ pci_seg->old_dev_tbl_cpy = NULL;24972497+ }24982498+ }2713249927142500 for_each_iommu(iommu) {27152501 clear_translation_pre_enabled(iommu);···27222498 }27232499 } else {27242500 pr_info("Copied DEV table from previous kernel.\n");27252725- free_pages((unsigned long)amd_iommu_dev_table,27262726- get_order(dev_table_size));27272727- amd_iommu_dev_table = old_dev_tbl_cpy;25012501+25022502+ for_each_pci_segment(pci_seg) {25032503+ free_pages((unsigned long)pci_seg->dev_table,25042504+ get_order(pci_seg->dev_table_size));25052505+ pci_seg->dev_table = pci_seg->old_dev_tbl_cpy;25062506+ }25072507+27282508 for_each_iommu(iommu) {27292509 iommu_disable_command_buffer(iommu);27302510 iommu_disable_event_buffer(iommu);···27402512 iommu_flush_all_caches(iommu);27412513 }27422514 }27432743-27442744-#ifdef CONFIG_IRQ_REMAP27452745- /*27462746- * Note: We have already checked GASup from IVRS table.27472747- * Now, we need to make sure that GAMSup is set.27482748- */27492749- if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir) &&27502750- !check_feature_on_all_iommus(FEATURE_GAM_VAPIC))27512751- amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY_GA;27522752-27532753- if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir))27542754- amd_iommu_irq_ops.capability |= (1 << IRQ_POSTING_CAP);27552755-#endif27562515}2757251627582517static void enable_iommus_v2(void)···27522537 }27532538}2754253925402540+static void enable_iommus_vapic(void)25412541+{25422542+#ifdef CONFIG_IRQ_REMAP25432543+ u32 status, i;25442544+ struct amd_iommu *iommu;25452545+25462546+ for_each_iommu(iommu) {25472547+ /*25482548+ * Disable GALog if already running. It could have been enabled25492549+ * in the previous boot before kdump.25502550+ */25512551+ status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);25522552+ if (!(status & MMIO_STATUS_GALOG_RUN_MASK))25532553+ continue;25542554+25552555+ iommu_feature_disable(iommu, CONTROL_GALOG_EN);25562556+ iommu_feature_disable(iommu, CONTROL_GAINT_EN);25572557+25582558+ /*25592559+ * Need to set and poll check the GALOGRun bit to zero before25602560+ * we can set/ modify GA Log registers safely.25612561+ */25622562+ for (i = 0; i < LOOP_TIMEOUT; ++i) {25632563+ status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);25642564+ if (!(status & MMIO_STATUS_GALOG_RUN_MASK))25652565+ break;25662566+ udelay(10);25672567+ }25682568+25692569+ if (WARN_ON(i >= LOOP_TIMEOUT))25702570+ return;25712571+ }25722572+25732573+ if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir) &&25742574+ !check_feature_on_all_iommus(FEATURE_GAM_VAPIC)) {25752575+ amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY_GA;25762576+ return;25772577+ }25782578+25792579+ if (amd_iommu_snp_en &&25802580+ !FEATURE_SNPAVICSUP_GAM(amd_iommu_efr2)) {25812581+ pr_warn("Force to disable Virtual APIC due to SNP\n");25822582+ amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY_GA;25832583+ return;25842584+ }25852585+25862586+ /* Enabling GAM and SNPAVIC support */25872587+ for_each_iommu(iommu) {25882588+ if (iommu_init_ga_log(iommu) ||25892589+ iommu_ga_log_enable(iommu))25902590+ return;25912591+25922592+ iommu_feature_enable(iommu, CONTROL_GAM_EN);25932593+ if (amd_iommu_snp_en)25942594+ iommu_feature_enable(iommu, CONTROL_SNPAVIC_EN);25952595+ }25962596+25972597+ amd_iommu_irq_ops.capability |= (1 << IRQ_POSTING_CAP);25982598+ pr_info("Virtual APIC enabled\n");25992599+#endif26002600+}26012601+27552602static void enable_iommus(void)27562603{27572604 early_enable_iommus();27582758-26052605+ enable_iommus_vapic();27592606 enable_iommus_v2();27602607}27612608···2867259028682591static void __init free_iommu_resources(void)28692592{28702870- kmemleak_free(irq_lookup_table);28712871- free_pages((unsigned long)irq_lookup_table,28722872- get_order(rlookup_table_size));28732873- irq_lookup_table = NULL;28742874-28752593 kmem_cache_destroy(amd_iommu_irq_cache);28762594 amd_iommu_irq_cache = NULL;2877259528782878- free_pages((unsigned long)amd_iommu_rlookup_table,28792879- get_order(rlookup_table_size));28802880- amd_iommu_rlookup_table = NULL;28812881-28822882- free_pages((unsigned long)amd_iommu_alias_table,28832883- get_order(alias_table_size));28842884- amd_iommu_alias_table = NULL;28852885-28862886- free_pages((unsigned long)amd_iommu_dev_table,28872887- get_order(dev_table_size));28882888- amd_iommu_dev_table = NULL;28892889-28902596 free_iommu_all();25972597+ free_pci_segments();28912598}2892259928932600/* SB IOAPIC is always on this device in AMD systems */···29702709static int __init early_amd_iommu_init(void)29712710{29722711 struct acpi_table_header *ivrs_base;29732973- int i, remap_cache_sz, ret;27122712+ int remap_cache_sz, ret;29742713 acpi_status status;2975271429762715 if (!amd_iommu_detected)···29982737 amd_iommu_target_ivhd_type = get_highest_supported_ivhd_type(ivrs_base);29992738 DUMP_printk("Using IVHD type %#x\n", amd_iommu_target_ivhd_type);3000273930013001- /*30023002- * First parse ACPI tables to find the largest Bus/Dev/Func30033003- * we need to handle. Upon this information the shared data30043004- * structures for the IOMMUs in the system will be allocated30053005- */30063006- ret = find_last_devid_acpi(ivrs_base);30073007- if (ret)30083008- goto out;30093009-30103010- dev_table_size = tbl_size(DEV_TABLE_ENTRY_SIZE);30113011- alias_table_size = tbl_size(ALIAS_TABLE_ENTRY_SIZE);30123012- rlookup_table_size = tbl_size(RLOOKUP_TABLE_ENTRY_SIZE);30133013-30142740 /* Device table - directly used by all IOMMUs */30152741 ret = -ENOMEM;30163016- amd_iommu_dev_table = (void *)__get_free_pages(30173017- GFP_KERNEL | __GFP_ZERO | GFP_DMA32,30183018- get_order(dev_table_size));30193019- if (amd_iommu_dev_table == NULL)30203020- goto out;30213021-30223022- /*30233023- * Alias table - map PCI Bus/Dev/Func to Bus/Dev/Func the30243024- * IOMMU see for that device30253025- */30263026- amd_iommu_alias_table = (void *)__get_free_pages(GFP_KERNEL,30273027- get_order(alias_table_size));30283028- if (amd_iommu_alias_table == NULL)30293029- goto out;30303030-30313031- /* IOMMU rlookup table - find the IOMMU for a specific device */30323032- amd_iommu_rlookup_table = (void *)__get_free_pages(30333033- GFP_KERNEL | __GFP_ZERO,30343034- get_order(rlookup_table_size));30353035- if (amd_iommu_rlookup_table == NULL)30363036- goto out;3037274230382743 amd_iommu_pd_alloc_bitmap = (void *)__get_free_pages(30392744 GFP_KERNEL | __GFP_ZERO,30402745 get_order(MAX_DOMAIN_ID/8));30412746 if (amd_iommu_pd_alloc_bitmap == NULL)30422747 goto out;30433043-30443044- /*30453045- * let all alias entries point to itself30463046- */30473047- for (i = 0; i <= amd_iommu_last_bdf; ++i)30483048- amd_iommu_alias_table[i] = i;3049274830502749 /*30512750 * never allocate domain 0 because its used as the non-allocated and···30292808 amd_iommu_irq_remap = check_ioapic_information();3030280930312810 if (amd_iommu_irq_remap) {28112811+ struct amd_iommu_pci_seg *pci_seg;30322812 /*30332813 * Interrupt remapping enabled, create kmem_cache for the30342814 * remapping tables.···30462824 if (!amd_iommu_irq_cache)30472825 goto out;3048282630493049- irq_lookup_table = (void *)__get_free_pages(30503050- GFP_KERNEL | __GFP_ZERO,30513051- get_order(rlookup_table_size));30523052- kmemleak_alloc(irq_lookup_table, rlookup_table_size,30533053- 1, GFP_KERNEL);30543054- if (!irq_lookup_table)30553055- goto out;28272827+ for_each_pci_segment(pci_seg) {28282828+ if (alloc_irq_lookup_table(pci_seg))28292829+ goto out;28302830+ }30562831 }3057283230582833 ret = init_memory_definitions(ivrs_base);···31562937 register_syscore_ops(&amd_iommu_syscore_ops);31572938 ret = amd_iommu_init_pci();31582939 init_state = ret ? IOMMU_INIT_ERROR : IOMMU_PCI_INIT;29402940+ enable_iommus_vapic();31592941 enable_iommus_v2();31602942 break;31612943 case IOMMU_PCI_INIT:···31872967 free_iommu_resources();31882968 } else {31892969 struct amd_iommu *iommu;29702970+ struct amd_iommu_pci_seg *pci_seg;3190297131913191- uninit_device_table_dma();29722972+ for_each_pci_segment(pci_seg)29732973+ uninit_device_table_dma(pci_seg);29742974+31922975 for_each_iommu(iommu)31932976 iommu_flush_all_caches(iommu);31942977 }···3384316133853162static int __init parse_ivrs_ioapic(char *str)33863163{33873387- unsigned int bus, dev, fn;31643164+ u32 seg = 0, bus, dev, fn;33883165 int ret, id, i;33893389- u16 devid;31663166+ u32 devid;3390316733913168 ret = sscanf(str, "[%d]=%x:%x.%x", &id, &bus, &dev, &fn);33923392-33933169 if (ret != 4) {33943394- pr_err("Invalid command line: ivrs_ioapic%s\n", str);33953395- return 1;31703170+ ret = sscanf(str, "[%d]=%x:%x:%x.%x", &id, &seg, &bus, &dev, &fn);31713171+ if (ret != 5) {31723172+ pr_err("Invalid command line: ivrs_ioapic%s\n", str);31733173+ return 1;31743174+ }33963175 }3397317633983177 if (early_ioapic_map_size == EARLY_MAP_SIZE) {···34033178 return 1;34043179 }3405318034063406- devid = ((bus & 0xff) << 8) | ((dev & 0x1f) << 3) | (fn & 0x7);31813181+ devid = IVRS_GET_SBDF_ID(seg, bus, dev, fn);3407318234083183 cmdline_maps = true;34093184 i = early_ioapic_map_size++;···3416319134173192static int __init parse_ivrs_hpet(char *str)34183193{34193419- unsigned int bus, dev, fn;31943194+ u32 seg = 0, bus, dev, fn;34203195 int ret, id, i;34213421- u16 devid;31963196+ u32 devid;3422319734233198 ret = sscanf(str, "[%d]=%x:%x.%x", &id, &bus, &dev, &fn);34243424-34253199 if (ret != 4) {34263426- pr_err("Invalid command line: ivrs_hpet%s\n", str);34273427- return 1;32003200+ ret = sscanf(str, "[%d]=%x:%x:%x.%x", &id, &seg, &bus, &dev, &fn);32013201+ if (ret != 5) {32023202+ pr_err("Invalid command line: ivrs_hpet%s\n", str);32033203+ return 1;32043204+ }34283205 }3429320634303207 if (early_hpet_map_size == EARLY_MAP_SIZE) {···34353208 return 1;34363209 }3437321034383438- devid = ((bus & 0xff) << 8) | ((dev & 0x1f) << 3) | (fn & 0x7);32113211+ devid = IVRS_GET_SBDF_ID(seg, bus, dev, fn);3439321234403213 cmdline_maps = true;34413214 i = early_hpet_map_size++;···3448322134493222static int __init parse_ivrs_acpihid(char *str)34503223{34513451- u32 bus, dev, fn;32243224+ u32 seg = 0, bus, dev, fn;34523225 char *hid, *uid, *p;34533226 char acpiid[ACPIHID_UID_LEN + ACPIHID_HID_LEN] = {0};34543227 int ret, i;3455322834563229 ret = sscanf(str, "[%x:%x.%x]=%s", &bus, &dev, &fn, acpiid);34573230 if (ret != 4) {34583458- pr_err("Invalid command line: ivrs_acpihid(%s)\n", str);34593459- return 1;32313231+ ret = sscanf(str, "[%x:%x:%x.%x]=%s", &seg, &bus, &dev, &fn, acpiid);32323232+ if (ret != 5) {32333233+ pr_err("Invalid command line: ivrs_acpihid(%s)\n", str);32343234+ return 1;32353235+ }34603236 }3461323734623238 p = acpiid;···34743244 i = early_acpihid_map_size++;34753245 memcpy(early_acpihid_map[i].hid, hid, strlen(hid));34763246 memcpy(early_acpihid_map[i].uid, uid, strlen(uid));34773477- early_acpihid_map[i].devid =34783478- ((bus & 0xff) << 8) | ((dev & 0x1f) << 3) | (fn & 0x7);32473247+ early_acpihid_map[i].devid = IVRS_GET_SBDF_ID(seg, bus, dev, fn);34793248 early_acpihid_map[i].cmd_line = true;3480324934813250 return 1;···3489326034903261bool amd_iommu_v2_supported(void)34913262{34923492- return amd_iommu_v2_present;32633263+ /*32643264+ * Since DTE[Mode]=0 is prohibited on SNP-enabled system32653265+ * (i.e. EFR[SNPSup]=1), IOMMUv2 page table cannot be used without32663266+ * setting up IOMMUv1 page table.32673267+ */32683268+ return amd_iommu_v2_present && !amd_iommu_snp_en;34933269}34943270EXPORT_SYMBOL(amd_iommu_v2_supported);34953271···3597336335983364 return iommu_pc_get_set_reg(iommu, bank, cntr, fxn, value, true);35993365}33663366+33673367+#ifdef CONFIG_AMD_MEM_ENCRYPT33683368+int amd_iommu_snp_enable(void)33693369+{33703370+ /*33713371+ * The SNP support requires that IOMMU must be enabled, and is33723372+ * not configured in the passthrough mode.33733373+ */33743374+ if (no_iommu || iommu_default_passthrough()) {33753375+ pr_err("SNP: IOMMU is disabled or configured in passthrough mode, SNP cannot be supported");33763376+ return -EINVAL;33773377+ }33783378+33793379+ /*33803380+ * Prevent enabling SNP after IOMMU_ENABLED state because this process33813381+ * affect how IOMMU driver sets up data structures and configures33823382+ * IOMMU hardware.33833383+ */33843384+ if (init_state > IOMMU_ENABLED) {33853385+ pr_err("SNP: Too late to enable SNP for IOMMU.\n");33863386+ return -EINVAL;33873387+ }33883388+33893389+ amd_iommu_snp_en = check_feature_on_all_iommus(FEATURE_SNP);33903390+ if (!amd_iommu_snp_en)33913391+ return -EINVAL;33923392+33933393+ pr_info("SNP enabled\n");33943394+33953395+ /* Enforce IOMMU v1 pagetable when SNP is enabled. */33963396+ if (amd_iommu_pgtable != AMD_IOMMU_V1) {33973397+ pr_warn("Force to using AMD IOMMU v1 page table due to SNP\n");33983398+ amd_iommu_pgtable = AMD_IOMMU_V1;33993399+ }34003400+34013401+ return 0;34023402+}34033403+#endif
+2-4
drivers/iommu/amd/io_pgtable.c
···258258 __npte = PM_LEVEL_PDE(level, iommu_virt_to_phys(page));259259260260 /* pte could have been changed somewhere. */261261- if (cmpxchg64(pte, __pte, __npte) != __pte)261261+ if (!try_cmpxchg64(pte, &__pte, __npte))262262 free_page((unsigned long)page);263263 else if (IOMMU_PTE_PRESENT(__pte))264264 *updated = true;···341341 u64 *pt;342342 int mode;343343344344- while (cmpxchg64(pte, pteval, 0) != pteval) {344344+ while (!try_cmpxchg64(pte, &pteval, 0))345345 pr_warn("AMD-Vi: IOMMU pte changed since we read it\n");346346- pteval = *pte;347347- }348346349347 if (!IOMMU_PTE_PRESENT(pteval))350348 return;
···64646565 /* Domain for flush queue callback; NULL if flush queue not in use */6666 struct iommu_domain *fq_domain;6767+ struct mutex mutex;6768};68696970static DEFINE_STATIC_KEY_FALSE(iommu_deferred_attach_enabled);7070-bool iommu_dma_forcedac __read_mostly;7171+bool iommu_dma_forcedac __read_mostly = !IS_ENABLED(CONFIG_IOMMU_DMA_PCI_SAC);71727273static int __init iommu_dma_forcedac_setup(char *str)7374{···311310 if (!domain->iova_cookie)312311 return -ENOMEM;313312313313+ mutex_init(&domain->iova_cookie->mutex);314314 return 0;315315}316316···387385{388386389387 if (!is_of_node(dev_iommu_fwspec_get(dev)->iommu_fwnode))390390- iort_iommu_msi_get_resv_regions(dev, list);388388+ iort_iommu_get_resv_regions(dev, list);391389392390}393391EXPORT_SYMBOL(iommu_dma_get_resv_regions);···562560 }563561564562 /* start_pfn is always nonzero for an already-initialised domain */563563+ mutex_lock(&cookie->mutex);565564 if (iovad->start_pfn) {566565 if (1UL << order != iovad->granule ||567566 base_pfn != iovad->start_pfn) {568567 pr_warn("Incompatible range for DMA domain\n");569569- return -EFAULT;568568+ ret = -EFAULT;569569+ goto done_unlock;570570 }571571572572- return 0;572572+ ret = 0;573573+ goto done_unlock;573574 }574575575576 init_iova_domain(iovad, 1UL << order, base_pfn);576577 ret = iova_domain_init_rcaches(iovad);577578 if (ret)578578- return ret;579579+ goto done_unlock;579580580581 /* If the FQ fails we can simply fall back to strict mode */581582 if (domain->type == IOMMU_DOMAIN_DMA_FQ && iommu_dma_init_fq(domain))582583 domain->type = IOMMU_DOMAIN_DMA;583584584584- return iova_reserve_iommu_regions(dev, domain);585585+ ret = iova_reserve_iommu_regions(dev, domain);586586+587587+done_unlock:588588+ mutex_unlock(&cookie->mutex);589589+ return ret;585590}586591587592/**
+138-48
drivers/iommu/exynos-iommu.c
···135135#define CFG_SYSSEL (1 << 22) /* System MMU 3.2 only */136136#define CFG_FLPDCACHE (1 << 20) /* System MMU 3.2+ only */137137138138+#define CTRL_VM_ENABLE BIT(0)139139+#define CTRL_VM_FAULT_MODE_STALL BIT(3)140140+#define CAPA0_CAPA1_EXIST BIT(11)141141+#define CAPA1_VCR_ENABLED BIT(14)142142+138143/* common registers */139144#define REG_MMU_CTRL 0x000140145#define REG_MMU_CFG 0x004···153148#define MAKE_MMU_VER(maj, min) ((((maj) & 0xF) << 7) | ((min) & 0x7F))154149155150/* v1.x - v3.x registers */156156-#define REG_MMU_FLUSH 0x00C157157-#define REG_MMU_FLUSH_ENTRY 0x010158158-#define REG_PT_BASE_ADDR 0x014159159-#define REG_INT_STATUS 0x018160160-#define REG_INT_CLEAR 0x01C161161-162151#define REG_PAGE_FAULT_ADDR 0x024163152#define REG_AW_FAULT_ADDR 0x028164153#define REG_AR_FAULT_ADDR 0x02C165154#define REG_DEFAULT_SLAVE_ADDR 0x030166155167156/* v5.x registers */168168-#define REG_V5_PT_BASE_PFN 0x00C169169-#define REG_V5_MMU_FLUSH_ALL 0x010170170-#define REG_V5_MMU_FLUSH_ENTRY 0x014171171-#define REG_V5_MMU_FLUSH_RANGE 0x018172172-#define REG_V5_MMU_FLUSH_START 0x020173173-#define REG_V5_MMU_FLUSH_END 0x024174174-#define REG_V5_INT_STATUS 0x060175175-#define REG_V5_INT_CLEAR 0x064176157#define REG_V5_FAULT_AR_VA 0x070177158#define REG_V5_FAULT_AW_VA 0x080159159+160160+/* v7.x registers */161161+#define REG_V7_CAPA0 0x870162162+#define REG_V7_CAPA1 0x874163163+#define REG_V7_CTRL_VM 0x8000178164179165#define has_sysmmu(dev) (dev_iommu_priv_get(dev) != NULL)180166···247251};248252249253/*254254+ * SysMMU version specific data. Contains offsets for the registers which can255255+ * be found in different SysMMU variants, but have different offset values.256256+ */257257+struct sysmmu_variant {258258+ u32 pt_base; /* page table base address (physical) */259259+ u32 flush_all; /* invalidate all TLB entries */260260+ u32 flush_entry; /* invalidate specific TLB entry */261261+ u32 flush_range; /* invalidate TLB entries in specified range */262262+ u32 flush_start; /* start address of range invalidation */263263+ u32 flush_end; /* end address of range invalidation */264264+ u32 int_status; /* interrupt status information */265265+ u32 int_clear; /* clear the interrupt */266266+};267267+268268+/*250269 * This structure hold all data of a single SYSMMU controller, this includes251270 * hw resources like registers and clocks, pointers and list nodes to connect252271 * it to all other structures, internal state and parameters read from device···285274 unsigned int version; /* our version */286275287276 struct iommu_device iommu; /* IOMMU core handle */277277+ const struct sysmmu_variant *variant; /* version specific data */278278+279279+ /* v7 fields */280280+ bool has_vcr; /* virtual machine control register */281281+};282282+283283+#define SYSMMU_REG(data, reg) ((data)->sfrbase + (data)->variant->reg)284284+285285+/* SysMMU v1..v3 */286286+static const struct sysmmu_variant sysmmu_v1_variant = {287287+ .flush_all = 0x0c,288288+ .flush_entry = 0x10,289289+ .pt_base = 0x14,290290+ .int_status = 0x18,291291+ .int_clear = 0x1c,292292+};293293+294294+/* SysMMU v5 and v7 (non-VM capable) */295295+static const struct sysmmu_variant sysmmu_v5_variant = {296296+ .pt_base = 0x0c,297297+ .flush_all = 0x10,298298+ .flush_entry = 0x14,299299+ .flush_range = 0x18,300300+ .flush_start = 0x20,301301+ .flush_end = 0x24,302302+ .int_status = 0x60,303303+ .int_clear = 0x64,304304+};305305+306306+/* SysMMU v7: VM capable register set */307307+static const struct sysmmu_variant sysmmu_v7_vm_variant = {308308+ .pt_base = 0x800c,309309+ .flush_all = 0x8010,310310+ .flush_entry = 0x8014,311311+ .flush_range = 0x8018,312312+ .flush_start = 0x8020,313313+ .flush_end = 0x8024,314314+ .int_status = 0x60,315315+ .int_clear = 0x64,288316};289317290318static struct exynos_iommu_domain *to_exynos_domain(struct iommu_domain *dom)···354304355305static void __sysmmu_tlb_invalidate(struct sysmmu_drvdata *data)356306{357357- if (MMU_MAJ_VER(data->version) < 5)358358- writel(0x1, data->sfrbase + REG_MMU_FLUSH);359359- else360360- writel(0x1, data->sfrbase + REG_V5_MMU_FLUSH_ALL);307307+ writel(0x1, SYSMMU_REG(data, flush_all));361308}362309363310static void __sysmmu_tlb_invalidate_entry(struct sysmmu_drvdata *data,···362315{363316 unsigned int i;364317365365- if (MMU_MAJ_VER(data->version) < 5) {318318+ if (MMU_MAJ_VER(data->version) < 5 || num_inv == 1) {366319 for (i = 0; i < num_inv; i++) {367320 writel((iova & SPAGE_MASK) | 1,368368- data->sfrbase + REG_MMU_FLUSH_ENTRY);321321+ SYSMMU_REG(data, flush_entry));369322 iova += SPAGE_SIZE;370323 }371324 } else {372372- if (num_inv == 1) {373373- writel((iova & SPAGE_MASK) | 1,374374- data->sfrbase + REG_V5_MMU_FLUSH_ENTRY);375375- } else {376376- writel((iova & SPAGE_MASK),377377- data->sfrbase + REG_V5_MMU_FLUSH_START);378378- writel((iova & SPAGE_MASK) + (num_inv - 1) * SPAGE_SIZE,379379- data->sfrbase + REG_V5_MMU_FLUSH_END);380380- writel(1, data->sfrbase + REG_V5_MMU_FLUSH_RANGE);381381- }325325+ writel(iova & SPAGE_MASK, SYSMMU_REG(data, flush_start));326326+ writel((iova & SPAGE_MASK) + (num_inv - 1) * SPAGE_SIZE,327327+ SYSMMU_REG(data, flush_end));328328+ writel(0x1, SYSMMU_REG(data, flush_range));382329 }383330}384331385332static void __sysmmu_set_ptbase(struct sysmmu_drvdata *data, phys_addr_t pgd)386333{387387- if (MMU_MAJ_VER(data->version) < 5)388388- writel(pgd, data->sfrbase + REG_PT_BASE_ADDR);389389- else390390- writel(pgd >> PAGE_SHIFT,391391- data->sfrbase + REG_V5_PT_BASE_PFN);334334+ u32 pt_base;392335336336+ if (MMU_MAJ_VER(data->version) < 5)337337+ pt_base = pgd;338338+ else339339+ pt_base = pgd >> SPAGE_ORDER;340340+341341+ writel(pt_base, SYSMMU_REG(data, pt_base));393342 __sysmmu_tlb_invalidate(data);394343}395344···405362 clk_disable_unprepare(data->clk_master);406363}407364365365+static bool __sysmmu_has_capa1(struct sysmmu_drvdata *data)366366+{367367+ u32 capa0 = readl(data->sfrbase + REG_V7_CAPA0);368368+369369+ return capa0 & CAPA0_CAPA1_EXIST;370370+}371371+372372+static void __sysmmu_get_vcr(struct sysmmu_drvdata *data)373373+{374374+ u32 capa1 = readl(data->sfrbase + REG_V7_CAPA1);375375+376376+ data->has_vcr = capa1 & CAPA1_VCR_ENABLED;377377+}378378+408379static void __sysmmu_get_version(struct sysmmu_drvdata *data)409380{410381 u32 ver;···435378436379 dev_dbg(data->sysmmu, "hardware version: %d.%d\n",437380 MMU_MAJ_VER(data->version), MMU_MIN_VER(data->version));381381+382382+ if (MMU_MAJ_VER(data->version) < 5) {383383+ data->variant = &sysmmu_v1_variant;384384+ } else if (MMU_MAJ_VER(data->version) < 7) {385385+ data->variant = &sysmmu_v5_variant;386386+ } else {387387+ if (__sysmmu_has_capa1(data))388388+ __sysmmu_get_vcr(data);389389+ if (data->has_vcr)390390+ data->variant = &sysmmu_v7_vm_variant;391391+ else392392+ data->variant = &sysmmu_v5_variant;393393+ }438394439395 __sysmmu_disable_clocks(data);440396}···476406 const struct sysmmu_fault_info *finfo;477407 unsigned int i, n, itype;478408 sysmmu_iova_t fault_addr;479479- unsigned short reg_status, reg_clear;480409 int ret = -ENOSYS;481410482411 WARN_ON(!data->active);483412484413 if (MMU_MAJ_VER(data->version) < 5) {485485- reg_status = REG_INT_STATUS;486486- reg_clear = REG_INT_CLEAR;487414 finfo = sysmmu_faults;488415 n = ARRAY_SIZE(sysmmu_faults);489416 } else {490490- reg_status = REG_V5_INT_STATUS;491491- reg_clear = REG_V5_INT_CLEAR;492417 finfo = sysmmu_v5_faults;493418 n = ARRAY_SIZE(sysmmu_v5_faults);494419 }···492427493428 clk_enable(data->clk_master);494429495495- itype = __ffs(readl(data->sfrbase + reg_status));430430+ itype = __ffs(readl(SYSMMU_REG(data, int_status)));496431 for (i = 0; i < n; i++, finfo++)497432 if (finfo->bit == itype)498433 break;···509444 /* fault is not recovered by fault handler */510445 BUG_ON(ret != 0);511446512512- writel(1 << itype, data->sfrbase + reg_clear);447447+ writel(1 << itype, SYSMMU_REG(data, int_clear));513448514449 sysmmu_unblock(data);515450···551486 writel(cfg, data->sfrbase + REG_MMU_CFG);552487}553488489489+static void __sysmmu_enable_vid(struct sysmmu_drvdata *data)490490+{491491+ u32 ctrl;492492+493493+ if (MMU_MAJ_VER(data->version) < 7 || !data->has_vcr)494494+ return;495495+496496+ ctrl = readl(data->sfrbase + REG_V7_CTRL_VM);497497+ ctrl |= CTRL_VM_ENABLE | CTRL_VM_FAULT_MODE_STALL;498498+ writel(ctrl, data->sfrbase + REG_V7_CTRL_VM);499499+}500500+554501static void __sysmmu_enable(struct sysmmu_drvdata *data)555502{556503 unsigned long flags;···573496 writel(CTRL_BLOCK, data->sfrbase + REG_MMU_CTRL);574497 __sysmmu_init_config(data);575498 __sysmmu_set_ptbase(data, data->pgtable);499499+ __sysmmu_enable_vid(data);576500 writel(CTRL_ENABLE, data->sfrbase + REG_MMU_CTRL);577501 data->active = true;578502 spin_unlock_irqrestore(&data->lock, flags);···629551 * 64KB page can be one of 16 consecutive sets.630552 */631553 if (MMU_MAJ_VER(data->version) == 2)632632- num_inv = min_t(unsigned int, size / PAGE_SIZE, 64);554554+ num_inv = min_t(unsigned int, size / SPAGE_SIZE, 64);633555634556 if (sysmmu_block(data)) {635557 __sysmmu_tlb_invalidate_entry(data, iova, num_inv);···701623 data->sysmmu = dev;702624 spin_lock_init(&data->lock);703625626626+ __sysmmu_get_version(data);627627+704628 ret = iommu_device_sysfs_add(&data->iommu, &pdev->dev, NULL,705629 dev_name(data->sysmmu));706630 if (ret)···710630711631 ret = iommu_device_register(&data->iommu, &exynos_iommu_ops, dev);712632 if (ret)713713- return ret;633633+ goto err_iommu_register;714634715635 platform_set_drvdata(pdev, data);716636717717- __sysmmu_get_version(data);718637 if (PG_ENT_SHIFT < 0) {719638 if (MMU_MAJ_VER(data->version) < 5) {720639 PG_ENT_SHIFT = SYSMMU_PG_ENT_SHIFT;···723644 PG_ENT_SHIFT = SYSMMU_V5_PG_ENT_SHIFT;724645 LV1_PROT = SYSMMU_V5_LV1_PROT;725646 LV2_PROT = SYSMMU_V5_LV2_PROT;647647+ }648648+ }649649+650650+ if (MMU_MAJ_VER(data->version) >= 5) {651651+ ret = dma_set_mask(dev, DMA_BIT_MASK(36));652652+ if (ret) {653653+ dev_err(dev, "Unable to set DMA mask: %d\n", ret);654654+ goto err_dma_set_mask;726655 }727656 }728657···744657 pm_runtime_enable(dev);745658746659 return 0;660660+661661+err_dma_set_mask:662662+ iommu_device_unregister(&data->iommu);663663+err_iommu_register:664664+ iommu_device_sysfs_remove(&data->iommu);665665+ return ret;747666}748667749668static int __maybe_unused exynos_sysmmu_suspend(struct device *dev)···13431250{13441251 struct exynos_iommu_owner *owner = dev_iommu_priv_get(dev);13451252 struct sysmmu_drvdata *data;13461346-13471347- if (!has_sysmmu(dev))13481348- return;1349125313501254 if (owner->domain) {13511255 struct iommu_group *group = iommu_group_get(dev);
···10101111#include <linux/debugfs.h>1212#include <linux/dmar.h>1313-#include <linux/intel-iommu.h>1413#include <linux/pci.h>15141615#include <asm/irq_remapping.h>17161717+#include "iommu.h"1818#include "pasid.h"1919#include "perf.h"2020···263263264264static void root_tbl_walk(struct seq_file *m, struct intel_iommu *iommu)265265{266266- unsigned long flags;267266 u16 bus;268267269269- spin_lock_irqsave(&iommu->lock, flags);268268+ spin_lock(&iommu->lock);270269 seq_printf(m, "IOMMU %s: Root Table Address: 0x%llx\n", iommu->name,271270 (u64)virt_to_phys(iommu->root_entry));272271 seq_puts(m, "B.D.F\tRoot_entry\t\t\t\tContext_entry\t\t\t\tPASID\tPASID_table_entry\n");···277278 */278279 for (bus = 0; bus < 256; bus++)279280 ctx_tbl_walk(m, iommu, bus);280280-281281- spin_unlock_irqrestore(&iommu->lock, flags);281281+ spin_unlock(&iommu->lock);282282}283283284284static int dmar_translation_struct_show(struct seq_file *m, void *unused)···340342 }341343}342344343343-static int show_device_domain_translation(struct device *dev, void *data)345345+static int __show_device_domain_translation(struct device *dev, void *data)344346{345345- struct device_domain_info *info = dev_iommu_priv_get(dev);346346- struct dmar_domain *domain = info->domain;347347+ struct dmar_domain *domain;347348 struct seq_file *m = data;348349 u64 path[6] = { 0 };349350351351+ domain = to_dmar_domain(iommu_get_domain_for_dev(dev));350352 if (!domain)351353 return 0;352354···357359 pgtable_walk_level(m, domain->pgd, domain->agaw + 2, 0, path);358360 seq_putc(m, '\n');359361362362+ /* Don't iterate */363363+ return 1;364364+}365365+366366+static int show_device_domain_translation(struct device *dev, void *data)367367+{368368+ struct iommu_group *group;369369+370370+ group = iommu_group_get(dev);371371+ if (group) {372372+ /*373373+ * The group->mutex is held across the callback, which will374374+ * block calls to iommu_attach/detach_group/device. Hence,375375+ * the domain of the device will not change during traversal.376376+ *377377+ * All devices in an iommu group share a single domain, hence378378+ * we only dump the domain of the first device. Even though,379379+ * this code still possibly races with the iommu_unmap()380380+ * interface. This could be solved by RCU-freeing the page381381+ * table pages in the iommu_unmap() path.382382+ */383383+ iommu_group_for_each_dev(group, data,384384+ __show_device_domain_translation);385385+ iommu_group_put(group);386386+ }387387+360388 return 0;361389}362390363391static int domain_translation_struct_show(struct seq_file *m, void *unused)364392{365365- unsigned long flags;366366- int ret;367367-368368- spin_lock_irqsave(&device_domain_lock, flags);369369- ret = bus_for_each_dev(&pci_bus_type, NULL, m,370370- show_device_domain_translation);371371- spin_unlock_irqrestore(&device_domain_lock, flags);372372-373373- return ret;393393+ return bus_for_each_dev(&pci_bus_type, NULL, m,394394+ show_device_domain_translation);374395}375396DEFINE_SHOW_ATTRIBUTE(domain_translation_struct);376397
···1212#include <linux/bitops.h>1313#include <linux/cpufeature.h>1414#include <linux/dmar.h>1515-#include <linux/intel-iommu.h>1615#include <linux/iommu.h>1716#include <linux/memory.h>1817#include <linux/pci.h>1918#include <linux/pci-ats.h>2019#include <linux/spinlock.h>21202121+#include "iommu.h"2222#include "pasid.h"23232424/*···450450 struct pasid_entry *pte;451451 u16 did, pgtt;452452453453+ spin_lock(&iommu->lock);453454 pte = intel_pasid_get_entry(dev, pasid);454454- if (WARN_ON(!pte))455455+ if (WARN_ON(!pte) || !pasid_pte_is_present(pte)) {456456+ spin_unlock(&iommu->lock);455457 return;456456-457457- if (!pasid_pte_is_present(pte))458458- return;458458+ }459459460460 did = pasid_get_domain_id(pte);461461 pgtt = pasid_pte_get_pgtt(pte);462462-463462 intel_pasid_clear_entry(dev, pasid, fault_ignore);463463+ spin_unlock(&iommu->lock);464464465465 if (!ecap_coherent(iommu->ecap))466466 clflush_cache_range(pte, sizeof(*pte));···496496 }497497}498498499499-static inline int pasid_enable_wpe(struct pasid_entry *pte)500500-{501501-#ifdef CONFIG_X86502502- unsigned long cr0 = read_cr0();503503-504504- /* CR0.WP is normally set but just to be sure */505505- if (unlikely(!(cr0 & X86_CR0_WP))) {506506- pr_err_ratelimited("No CPU write protect!\n");507507- return -EINVAL;508508- }509509-#endif510510- pasid_set_wpe(pte);511511-512512- return 0;513513-};514514-515499/*516500 * Set up the scalable mode pasid table entry for first only517501 * translation type.···512528 return -EINVAL;513529 }514530515515- pte = intel_pasid_get_entry(dev, pasid);516516- if (WARN_ON(!pte))517517- return -EINVAL;531531+ if (flags & PASID_FLAG_SUPERVISOR_MODE) {532532+#ifdef CONFIG_X86533533+ unsigned long cr0 = read_cr0();518534519519- /* Caller must ensure PASID entry is not in use. */520520- if (pasid_pte_is_present(pte))535535+ /* CR0.WP is normally set but just to be sure */536536+ if (unlikely(!(cr0 & X86_CR0_WP))) {537537+ pr_err("No CPU write protect!\n");538538+ return -EINVAL;539539+ }540540+#endif541541+ if (!ecap_srs(iommu->ecap)) {542542+ pr_err("No supervisor request support on %s\n",543543+ iommu->name);544544+ return -EINVAL;545545+ }546546+ }547547+548548+ if ((flags & PASID_FLAG_FL5LP) && !cap_5lp_support(iommu->cap)) {549549+ pr_err("No 5-level paging support for first-level on %s\n",550550+ iommu->name);551551+ return -EINVAL;552552+ }553553+554554+ spin_lock(&iommu->lock);555555+ pte = intel_pasid_get_entry(dev, pasid);556556+ if (!pte) {557557+ spin_unlock(&iommu->lock);558558+ return -ENODEV;559559+ }560560+561561+ if (pasid_pte_is_present(pte)) {562562+ spin_unlock(&iommu->lock);521563 return -EBUSY;564564+ }522565523566 pasid_clear_entry(pte);524567525568 /* Setup the first level page table pointer: */526569 pasid_set_flptr(pte, (u64)__pa(pgd));527570 if (flags & PASID_FLAG_SUPERVISOR_MODE) {528528- if (!ecap_srs(iommu->ecap)) {529529- pr_err("No supervisor request support on %s\n",530530- iommu->name);531531- return -EINVAL;532532- }533571 pasid_set_sre(pte);534534- if (pasid_enable_wpe(pte))535535- return -EINVAL;536536-572572+ pasid_set_wpe(pte);537573 }538574539539- if (flags & PASID_FLAG_FL5LP) {540540- if (cap_5lp_support(iommu->cap)) {541541- pasid_set_flpm(pte, 1);542542- } else {543543- pr_err("No 5-level paging support for first-level\n");544544- pasid_clear_entry(pte);545545- return -EINVAL;546546- }547547- }575575+ if (flags & PASID_FLAG_FL5LP)576576+ pasid_set_flpm(pte, 1);548577549578 if (flags & PASID_FLAG_PAGE_SNOOP)550579 pasid_set_pgsnp(pte);···569572 /* Setup Present and PASID Granular Transfer Type: */570573 pasid_set_translation_type(pte, PASID_ENTRY_PGTT_FL_ONLY);571574 pasid_set_present(pte);575575+ spin_unlock(&iommu->lock);576576+572577 pasid_flush_caches(iommu, pte, pasid, did);573578574579 return 0;···626627 }627628628629 pgd_val = virt_to_phys(pgd);629629- did = domain->iommu_did[iommu->seq_id];630630+ did = domain_id_iommu(domain, iommu);630631632632+ spin_lock(&iommu->lock);631633 pte = intel_pasid_get_entry(dev, pasid);632634 if (!pte) {633633- dev_err(dev, "Failed to get pasid entry of PASID %d\n", pasid);635635+ spin_unlock(&iommu->lock);634636 return -ENODEV;635637 }636638637637- /* Caller must ensure PASID entry is not in use. */638638- if (pasid_pte_is_present(pte))639639+ if (pasid_pte_is_present(pte)) {640640+ spin_unlock(&iommu->lock);639641 return -EBUSY;642642+ }640643641644 pasid_clear_entry(pte);642645 pasid_set_domain_id(pte, did);···655654 if (pasid != PASID_RID2PASID)656655 pasid_set_sre(pte);657656 pasid_set_present(pte);657657+ spin_unlock(&iommu->lock);658658+658659 pasid_flush_caches(iommu, pte, pasid, did);659660660661 return 0;···672669 u16 did = FLPT_DEFAULT_DID;673670 struct pasid_entry *pte;674671672672+ spin_lock(&iommu->lock);675673 pte = intel_pasid_get_entry(dev, pasid);676674 if (!pte) {677677- dev_err(dev, "Failed to get pasid entry of PASID %d\n", pasid);675675+ spin_unlock(&iommu->lock);678676 return -ENODEV;679677 }680678681681- /* Caller must ensure PASID entry is not in use. */682682- if (pasid_pte_is_present(pte))679679+ if (pasid_pte_is_present(pte)) {680680+ spin_unlock(&iommu->lock);683681 return -EBUSY;682682+ }684683685684 pasid_clear_entry(pte);686685 pasid_set_domain_id(pte, did);···697692 */698693 pasid_set_sre(pte);699694 pasid_set_present(pte);695695+ spin_unlock(&iommu->lock);696696+700697 pasid_flush_caches(iommu, pte, pasid, did);701698702699 return 0;
+1
drivers/iommu/intel/pasid.h
···3939 * only and pass-through transfer modes.4040 */4141#define FLPT_DEFAULT_DID 14242+#define NUM_RESERVED_DID 242434344/*4445 * The SUPERVISOR_MODE flag indicates a first level translation which
···182182 (cfg->quirks & IO_PGTABLE_QUIRK_ARM_MTK_EXT);183183}184184185185-static arm_v7s_iopte paddr_to_iopte(phys_addr_t paddr, int lvl,186186- struct io_pgtable_cfg *cfg)185185+static arm_v7s_iopte to_mtk_iopte(phys_addr_t paddr, arm_v7s_iopte pte)187186{188188- arm_v7s_iopte pte = paddr & ARM_V7S_LVL_MASK(lvl);189189-190190- if (!arm_v7s_is_mtk_enabled(cfg))191191- return pte;192192-193187 if (paddr & BIT_ULL(32))194188 pte |= ARM_V7S_ATTR_MTK_PA_BIT32;195189 if (paddr & BIT_ULL(33))196190 pte |= ARM_V7S_ATTR_MTK_PA_BIT33;197191 if (paddr & BIT_ULL(34))198192 pte |= ARM_V7S_ATTR_MTK_PA_BIT34;193193+ return pte;194194+}195195+196196+static arm_v7s_iopte paddr_to_iopte(phys_addr_t paddr, int lvl,197197+ struct io_pgtable_cfg *cfg)198198+{199199+ arm_v7s_iopte pte = paddr & ARM_V7S_LVL_MASK(lvl);200200+201201+ if (arm_v7s_is_mtk_enabled(cfg))202202+ return to_mtk_iopte(paddr, pte);203203+199204 return pte;200205}201206···245240 dma_addr_t dma;246241 size_t size = ARM_V7S_TABLE_SIZE(lvl, cfg);247242 void *table = NULL;243243+ gfp_t gfp_l1;244244+245245+ /*246246+ * ARM_MTK_TTBR_EXT extend the translation table base support larger247247+ * memory address.248248+ */249249+ gfp_l1 = cfg->quirks & IO_PGTABLE_QUIRK_ARM_MTK_TTBR_EXT ?250250+ GFP_KERNEL : ARM_V7S_TABLE_GFP_DMA;248251249252 if (lvl == 1)250250- table = (void *)__get_free_pages(251251- __GFP_ZERO | ARM_V7S_TABLE_GFP_DMA, get_order(size));253253+ table = (void *)__get_free_pages(gfp_l1 | __GFP_ZERO, get_order(size));252254 else if (lvl == 2)253255 table = kmem_cache_zalloc(data->l2_tables, gfp);254256···263251 return NULL;264252265253 phys = virt_to_phys(table);266266- if (phys != (arm_v7s_iopte)phys) {254254+ if (cfg->quirks & IO_PGTABLE_QUIRK_ARM_MTK_TTBR_EXT ?255255+ phys >= (1ULL << cfg->oas) : phys != (arm_v7s_iopte)phys) {267256 /* Doesn't fit in PTE */268257 dev_err(dev, "Page table does not fit in PTE: %pa", &phys);269258 goto out_free;···470457 arm_v7s_iopte curr,471458 struct io_pgtable_cfg *cfg)472459{460460+ phys_addr_t phys = virt_to_phys(table);473461 arm_v7s_iopte old, new;474462475475- new = virt_to_phys(table) | ARM_V7S_PTE_TYPE_TABLE;463463+ new = phys | ARM_V7S_PTE_TYPE_TABLE;464464+465465+ if (cfg->quirks & IO_PGTABLE_QUIRK_ARM_MTK_TTBR_EXT)466466+ new = to_mtk_iopte(phys, new);467467+476468 if (cfg->quirks & IO_PGTABLE_QUIRK_ARM_NS)477469 new |= ARM_V7S_ATTR_NS_TABLE;478470···797779 void *cookie)798780{799781 struct arm_v7s_io_pgtable *data;782782+ slab_flags_t slab_flag;783783+ phys_addr_t paddr;800784801785 if (cfg->ias > (arm_v7s_is_mtk_enabled(cfg) ? 34 : ARM_V7S_ADDR_BITS))802786 return NULL;···808788809789 if (cfg->quirks & ~(IO_PGTABLE_QUIRK_ARM_NS |810790 IO_PGTABLE_QUIRK_NO_PERMS |811811- IO_PGTABLE_QUIRK_ARM_MTK_EXT))791791+ IO_PGTABLE_QUIRK_ARM_MTK_EXT |792792+ IO_PGTABLE_QUIRK_ARM_MTK_TTBR_EXT))812793 return NULL;813794814795 /* If ARM_MTK_4GB is enabled, the NO_PERMS is also expected. */···817796 !(cfg->quirks & IO_PGTABLE_QUIRK_NO_PERMS))818797 return NULL;819798799799+ if ((cfg->quirks & IO_PGTABLE_QUIRK_ARM_MTK_TTBR_EXT) &&800800+ !arm_v7s_is_mtk_enabled(cfg))801801+ return NULL;802802+820803 data = kmalloc(sizeof(*data), GFP_KERNEL);821804 if (!data)822805 return NULL;823806824807 spin_lock_init(&data->split_lock);808808+809809+ /*810810+ * ARM_MTK_TTBR_EXT extend the translation table base support larger811811+ * memory address.812812+ */813813+ slab_flag = cfg->quirks & IO_PGTABLE_QUIRK_ARM_MTK_TTBR_EXT ?814814+ 0 : ARM_V7S_TABLE_SLAB_FLAGS;815815+825816 data->l2_tables = kmem_cache_create("io-pgtable_armv7s_l2",826817 ARM_V7S_TABLE_SIZE(2, cfg),827818 ARM_V7S_TABLE_SIZE(2, cfg),828828- ARM_V7S_TABLE_SLAB_FLAGS, NULL);819819+ slab_flag, NULL);829820 if (!data->l2_tables)830821 goto out_free_data;831822···883850 wmb();884851885852 /* TTBR */886886- cfg->arm_v7s_cfg.ttbr = virt_to_phys(data->pgd) | ARM_V7S_TTBR_S |887887- (cfg->coherent_walk ? (ARM_V7S_TTBR_NOS |888888- ARM_V7S_TTBR_IRGN_ATTR(ARM_V7S_RGN_WBWA) |889889- ARM_V7S_TTBR_ORGN_ATTR(ARM_V7S_RGN_WBWA)) :890890- (ARM_V7S_TTBR_IRGN_ATTR(ARM_V7S_RGN_NC) |891891- ARM_V7S_TTBR_ORGN_ATTR(ARM_V7S_RGN_NC)));853853+ paddr = virt_to_phys(data->pgd);854854+ if (arm_v7s_is_mtk_enabled(cfg))855855+ cfg->arm_v7s_cfg.ttbr = paddr | upper_32_bits(paddr);856856+ else857857+ cfg->arm_v7s_cfg.ttbr = paddr | ARM_V7S_TTBR_S |858858+ (cfg->coherent_walk ? (ARM_V7S_TTBR_NOS |859859+ ARM_V7S_TTBR_IRGN_ATTR(ARM_V7S_RGN_WBWA) |860860+ ARM_V7S_TTBR_ORGN_ATTR(ARM_V7S_RGN_WBWA)) :861861+ (ARM_V7S_TTBR_IRGN_ATTR(ARM_V7S_RGN_NC) |862862+ ARM_V7S_TTBR_ORGN_ATTR(ARM_V7S_RGN_NC)));892863 return &data->iop;893864894865out_free_data:
+19-36
drivers/iommu/iommu.c
···259259 return 0;260260261261out_release:262262- ops->release_device(dev);262262+ if (ops->release_device)263263+ ops->release_device(dev);263264264265out_module_put:265266 module_put(ops->owner);···273272274273int iommu_probe_device(struct device *dev)275274{276276- const struct iommu_ops *ops = dev->bus->iommu_ops;275275+ const struct iommu_ops *ops;277276 struct iommu_group *group;278277 int ret;279278···314313 mutex_unlock(&group->mutex);315314 iommu_group_put(group);316315316316+ ops = dev_iommu_ops(dev);317317 if (ops->probe_finalize)318318 ops->probe_finalize(dev);319319···338336 iommu_device_unlink(dev->iommu->iommu_dev, dev);339337340338 ops = dev_iommu_ops(dev);341341- ops->release_device(dev);339339+ if (ops->release_device)340340+ ops->release_device(dev);342341343342 iommu_group_remove_device(dev);344343 module_put(ops->owner);···603600 if (group->iommu_data_release)604601 group->iommu_data_release(group->iommu_data);605602606606- ida_simple_remove(&iommu_group_ida, group->id);603603+ ida_free(&iommu_group_ida, group->id);607604608605 if (group->default_domain)609606 iommu_domain_free(group->default_domain);···644641 INIT_LIST_HEAD(&group->devices);645642 INIT_LIST_HEAD(&group->entry);646643647647- ret = ida_simple_get(&iommu_group_ida, 0, 0, GFP_KERNEL);644644+ ret = ida_alloc(&iommu_group_ida, GFP_KERNEL);648645 if (ret < 0) {649646 kfree(group);650647 return ERR_PTR(ret);···654651 ret = kobject_init_and_add(&group->kobj, &iommu_group_ktype,655652 NULL, "%d", group->id);656653 if (ret) {657657- ida_simple_remove(&iommu_group_ida, group->id);654654+ ida_free(&iommu_group_ida, group->id);658655 kobject_put(&group->kobj);659656 return ERR_PTR(ret);660657 }···25792576 ops->get_resv_regions(dev, list);25802577}2581257825822582-void iommu_put_resv_regions(struct device *dev, struct list_head *list)25832583-{25842584- const struct iommu_ops *ops = dev_iommu_ops(dev);25852585-25862586- if (ops->put_resv_regions)25872587- ops->put_resv_regions(dev, list);25882588-}25892589-25902579/**25912591- * generic_iommu_put_resv_regions - Reserved region driver helper25802580+ * iommu_put_resv_regions - release resered regions25922581 * @dev: device for which to free reserved regions25932582 * @list: reserved region list for device25942583 *25952595- * IOMMU drivers can use this to implement their .put_resv_regions() callback25962596- * for simple reservations. Memory allocated for each reserved region will be25972597- * freed. If an IOMMU driver allocates additional resources per region, it is25982598- * going to have to implement a custom callback.25842584+ * This releases a reserved region list acquired by iommu_get_resv_regions().25992585 */26002600-void generic_iommu_put_resv_regions(struct device *dev, struct list_head *list)25862586+void iommu_put_resv_regions(struct device *dev, struct list_head *list)26012587{26022588 struct iommu_resv_region *entry, *next;2603258926042604- list_for_each_entry_safe(entry, next, list, list)26052605- kfree(entry);25902590+ list_for_each_entry_safe(entry, next, list, list) {25912591+ if (entry->free)25922592+ entry->free(dev, entry);25932593+ else25942594+ kfree(entry);25952595+ }26062596}26072607-EXPORT_SYMBOL(generic_iommu_put_resv_regions);25972597+EXPORT_SYMBOL(iommu_put_resv_regions);2608259826092599struct iommu_resv_region *iommu_alloc_resv_region(phys_addr_t start,26102600 size_t length, int prot,···27462750 return -EBUSY;27472751}27482752EXPORT_SYMBOL_GPL(iommu_dev_disable_feature);27492749-27502750-bool iommu_dev_feature_enabled(struct device *dev, enum iommu_dev_features feat)27512751-{27522752- if (dev->iommu && dev->iommu->iommu_dev) {27532753- const struct iommu_ops *ops = dev->iommu->iommu_dev->ops;27542754-27552755- if (ops->dev_feat_enabled)27562756- return ops->dev_feat_enabled(dev, feat);27572757- }27582758-27592759- return false;27602760-}27612761-EXPORT_SYMBOL_GPL(iommu_dev_feature_enabled);2762275327632754/**27642755 * iommu_sva_bind_device() - Bind a process address space to a device
+6-1
drivers/iommu/iova.c
···614614 * dynamic size tuning described in the paper.615615 */616616617617-#define IOVA_MAG_SIZE 128617617+/*618618+ * As kmalloc's buffer size is fixed to power of 2, 127 is chosen to619619+ * assure size of 'iova_magazine' to be 1024 bytes, so that no memory620620+ * will be wasted.621621+ */622622+#define IOVA_MAG_SIZE 127618623#define MAX_GLOBAL_MAGS 32 /* magazines per bin */619624620625struct iova_magazine {
+1-6
drivers/iommu/msm_iommu.c
···394394 return &iommu->iommu;395395}396396397397-static void msm_iommu_release_device(struct device *dev)398398-{399399-}400400-401397static int msm_iommu_attach_dev(struct iommu_domain *domain, struct device *dev)402398{403399 int ret = 0;···599603600604 for (sid = 0; sid < master->num_mids; sid++)601605 if (master->mids[sid] == spec->args[0]) {602602- dev_warn(dev, "Stream ID 0x%hx repeated; ignoring\n",606606+ dev_warn(dev, "Stream ID 0x%x repeated; ignoring\n",603607 sid);604608 return 0;605609 }···673677static struct iommu_ops msm_iommu_ops = {674678 .domain_alloc = msm_iommu_domain_alloc,675679 .probe_device = msm_iommu_probe_device,676676- .release_device = msm_iommu_release_device,677680 .device_group = generic_device_group,678681 .pgsize_bitmap = MSM_IOMMU_PGSIZES,679682 .of_xlate = qcom_iommu_of_xlate,
+37-34
drivers/iommu/mtk_iommu.c
···3434#include <dt-bindings/memory/mtk-memory-port.h>35353636#define REG_MMU_PT_BASE_ADDR 0x0003737-#define MMU_PT_ADDR_MASK GENMASK(31, 7)38373938#define REG_MMU_INVALIDATE 0x0204039#define F_ALL_INVLD 0x2···137138/* PM and clock always on. e.g. infra iommu */138139#define PM_CLK_AO BIT(15)139140#define IFA_IOMMU_PCIE_SUPPORT BIT(16)141141+#define PGTABLE_PA_35_EN BIT(17)140142141143#define MTK_IOMMU_HAS_FLAG_MASK(pdata, _x, mask) \142144 ((((pdata)->flags) & (mask)) == (_x))···596596 .iommu_dev = data->dev,597597 };598598599599+ if (MTK_IOMMU_HAS_FLAG(data->plat_data, PGTABLE_PA_35_EN))600600+ dom->cfg.quirks |= IO_PGTABLE_QUIRK_ARM_MTK_TTBR_EXT;601601+599602 if (MTK_IOMMU_HAS_FLAG(data->plat_data, HAS_4GB_MODE))600603 dom->cfg.oas = data->enable_4GB ? 33 : 32;601604 else···687684 goto err_unlock;688685 }689686 bank->m4u_dom = dom;690690- writel(dom->cfg.arm_v7s_cfg.ttbr & MMU_PT_ADDR_MASK,691691- bank->base + REG_MMU_PT_BASE_ADDR);687687+ writel(dom->cfg.arm_v7s_cfg.ttbr, bank->base + REG_MMU_PT_BASE_ADDR);692688693689 pm_runtime_put(m4udev);694690 }···821819 struct device *larbdev;822820 unsigned int larbid;823821824824- if (!fwspec || fwspec->ops != &mtk_iommu_ops)825825- return;826826-827822 data = dev_iommu_priv_get(dev);828823 if (MTK_IOMMU_IS_TYPE(data->plat_data, MTK_IOMMU_TYPE_MM)) {829824 larbid = MTK_M4U_TO_LARB(fwspec->ids[0]);830825 larbdev = data->larb_imu[larbid].dev;831826 device_link_remove(dev, larbdev);832827 }833833-834834- iommu_fwspec_free(dev);835828}836829837830static int mtk_iommu_get_group_id(struct device *dev, const struct mtk_iommu_plat_data *plat_data)···930933 .device_group = mtk_iommu_device_group,931934 .of_xlate = mtk_iommu_of_xlate,932935 .get_resv_regions = mtk_iommu_get_resv_regions,933933- .put_resv_regions = generic_iommu_put_resv_regions,934936 .pgsize_bitmap = SZ_4K | SZ_64K | SZ_1M | SZ_16M,935937 .owner = THIS_MODULE,936938 .default_domain_ops = &(const struct iommu_domain_ops) {···11361140 data->protect_base = ALIGN(virt_to_phys(protect), MTK_PROTECT_PA_ALIGN);1137114111381142 if (MTK_IOMMU_HAS_FLAG(data->plat_data, HAS_4GB_MODE)) {11391139- switch (data->plat_data->m4u_plat) {11401140- case M4U_MT2712:11411141- p = "mediatek,mt2712-infracfg";11421142- break;11431143- case M4U_MT8173:11441144- p = "mediatek,mt8173-infracfg";11451145- break;11461146- default:11471147- p = NULL;11431143+ infracfg = syscon_regmap_lookup_by_phandle(dev->of_node, "mediatek,infracfg");11441144+ if (IS_ERR(infracfg)) {11451145+ /*11461146+ * Legacy devicetrees will not specify a phandle to11471147+ * mediatek,infracfg: in that case, we use the older11481148+ * way to retrieve a syscon to infra.11491149+ *11501150+ * This is for retrocompatibility purposes only, hence11511151+ * no more compatibles shall be added to this.11521152+ */11531153+ switch (data->plat_data->m4u_plat) {11541154+ case M4U_MT2712:11551155+ p = "mediatek,mt2712-infracfg";11561156+ break;11571157+ case M4U_MT8173:11581158+ p = "mediatek,mt8173-infracfg";11591159+ break;11601160+ default:11611161+ p = NULL;11621162+ }11631163+11641164+ infracfg = syscon_regmap_lookup_by_compatible(p);11651165+ if (IS_ERR(infracfg))11661166+ return PTR_ERR(infracfg);11481167 }11491149-11501150- infracfg = syscon_regmap_lookup_by_compatible(p);11511151-11521152- if (IS_ERR(infracfg))11531153- return PTR_ERR(infracfg);1154116811551169 ret = regmap_read(infracfg, REG_INFRA_MISC, &val);11561170 if (ret)···12101204 if (MTK_IOMMU_IS_TYPE(data->plat_data, MTK_IOMMU_TYPE_MM)) {12111205 ret = mtk_iommu_mm_dts_parse(dev, &match, data);12121206 if (ret) {12131213- dev_err(dev, "mm dts parse fail(%d).", ret);12071207+ dev_err_probe(dev, ret, "mm dts parse fail\n");12141208 goto out_runtime_disable;12151209 }12161216- } else if (MTK_IOMMU_IS_TYPE(data->plat_data, MTK_IOMMU_TYPE_INFRA) &&12171217- data->plat_data->pericfg_comp_str) {12181218- infracfg = syscon_regmap_lookup_by_compatible(data->plat_data->pericfg_comp_str);12191219- if (IS_ERR(infracfg)) {12201220- ret = PTR_ERR(infracfg);12101210+ } else if (MTK_IOMMU_IS_TYPE(data->plat_data, MTK_IOMMU_TYPE_INFRA)) {12111211+ p = data->plat_data->pericfg_comp_str;12121212+ data->pericfg = syscon_regmap_lookup_by_compatible(p);12131213+ if (IS_ERR(data->pericfg)) {12141214+ ret = PTR_ERR(data->pericfg);12211215 goto out_runtime_disable;12221216 }12231223-12241224- data->pericfg = infracfg;12251217 }1226121812271219 platform_set_drvdata(pdev, data);···13701366 writel_relaxed(reg->int_control[i], base + REG_MMU_INT_CONTROL0);13711367 writel_relaxed(reg->int_main_control[i], base + REG_MMU_INT_MAIN_CONTROL);13721368 writel_relaxed(reg->ivrp_paddr[i], base + REG_MMU_IVRP_PADDR);13731373- writel(m4u_dom->cfg.arm_v7s_cfg.ttbr & MMU_PT_ADDR_MASK,13741374- base + REG_MMU_PT_BASE_ADDR);13691369+ writel(m4u_dom->cfg.arm_v7s_cfg.ttbr, base + REG_MMU_PT_BASE_ADDR);13751370 } while (++i < data->plat_data->banks_num);1376137113771372 /*···14041401static const struct mtk_iommu_plat_data mt6779_data = {14051402 .m4u_plat = M4U_MT6779,14061403 .flags = HAS_SUB_COMM_2BITS | OUT_ORDER_WR_EN | WR_THROT_EN |14071407- MTK_IOMMU_TYPE_MM,14041404+ MTK_IOMMU_TYPE_MM | PGTABLE_PA_35_EN,14081405 .inv_sel_reg = REG_MMU_INV_SEL_GEN2,14091406 .banks_num = 1,14101407 .banks_enable = {true},
-5
drivers/iommu/mtk_iommu_v1.c
···532532 struct device *larbdev;533533 unsigned int larbid;534534535535- if (!fwspec || fwspec->ops != &mtk_iommu_v1_ops)536536- return;537537-538535 data = dev_iommu_priv_get(dev);539536 larbid = mt2701_m4u_to_larb(fwspec->ids[0]);540537 larbdev = data->larb_imu[larbid].dev;541538 device_link_remove(dev, larbdev);542542-543543- iommu_fwspec_free(dev);544539}545540546541static int mtk_iommu_v1_hw_init(const struct mtk_iommu_v1_data *data)
···2121#include <linux/dmar.h>2222#include <linux/ioasid.h>2323#include <linux/bitfield.h>2424+#include <linux/xarray.h>24252526#include <asm/cacheflush.h>2627#include <asm/iommu.h>···481480#define VTD_FLAG_SVM_CAPABLE (1 << 2)482481483482extern int intel_iommu_sm;484484-extern spinlock_t device_domain_lock;485483486484#define sm_supported(iommu) (intel_iommu_sm && ecap_smts((iommu)->ecap))487485#define pasid_supported(iommu) (sm_supported(iommu) && \···525525 */526526#define DOMAIN_FLAG_USE_FIRST_LEVEL BIT(1)527527528528-struct dmar_domain {529529- int nid; /* node id */530530-531531- unsigned int iommu_refcnt[DMAR_UNITS_SUPPORTED];532532- /* Refcount of devices per iommu */533533-534534-535535- u16 iommu_did[DMAR_UNITS_SUPPORTED];536536- /* Domain ids per IOMMU. Use u16 since528528+struct iommu_domain_info {529529+ struct intel_iommu *iommu;530530+ unsigned int refcnt; /* Refcount of devices per iommu */531531+ u16 did; /* Domain ids per IOMMU. Use u16 since537532 * domain ids are 16 bit wide according538533 * to VT-d spec, section 9.3 */534534+};535535+536536+struct dmar_domain {537537+ int nid; /* node id */538538+ struct xarray iommu_array; /* Attached IOMMU array */539539540540 u8 has_iotlb_device: 1;541541 u8 iommu_coherency: 1; /* indicate coherency of iommu access */542542 u8 force_snooping : 1; /* Create IOPTEs with snoop control */543543 u8 set_pte_snp:1;544544545545+ spinlock_t lock; /* Protect device tracking lists */545546 struct list_head devices; /* all devices' list */546546- struct iova_domain iovad; /* iova's that belong to this domain */547547548548 struct dma_pte *pgd; /* virtual address */549549 int gaw; /* max guest address width */···611611/* PCI domain-device relationship */612612struct device_domain_info {613613 struct list_head link; /* link to domain siblings */614614- struct list_head global; /* link to global list */615614 u32 segment; /* PCI segment number */616615 u8 bus; /* PCI bus number */617616 u8 devfn; /* PCI devfn number */···639640static inline struct dmar_domain *to_dmar_domain(struct iommu_domain *dom)640641{641642 return container_of(dom, struct dmar_domain, domain);643643+}644644+645645+/* Retrieve the domain ID which has allocated to the domain */646646+static inline u16647647+domain_id_iommu(struct dmar_domain *domain, struct intel_iommu *iommu)648648+{649649+ struct iommu_domain_info *info =650650+ xa_load(&domain->iommu_array, iommu->seq_id);651651+652652+ return info->did;642653}643654644655/*···736727737728void *alloc_pgtable_page(int node);738729void free_pgtable_page(void *vaddr);739739-struct intel_iommu *domain_get_iommu(struct dmar_domain *domain);740730void iommu_flush_write_buffer(struct intel_iommu *iommu);741731int intel_iommu_enable_pasid(struct intel_iommu *iommu, struct device *dev);742732struct intel_iommu *device_to_iommu(struct device *dev, u8 *bus, u8 *devfn);···795787extern int iommu_calculate_max_sagaw(struct intel_iommu *iommu);796788extern int dmar_disabled;797789extern int intel_iommu_enabled;798798-extern int intel_iommu_gfx_mapped;799790#else800791static inline int iommu_calculate_agaw(struct intel_iommu *iommu)801792{
+10-5
include/linux/io-pgtable.h
···7474 * to support up to 35 bits PA where the bit32, bit33 and bit34 are7575 * encoded in the bit9, bit4 and bit5 of the PTE respectively.7676 *7777+ * IO_PGTABLE_QUIRK_ARM_MTK_TTBR_EXT: (ARM v7s format) MediaTek IOMMUs7878+ * extend the translation table base support up to 35 bits PA, the7979+ * encoding format is same with IO_PGTABLE_QUIRK_ARM_MTK_EXT.8080+ *7781 * IO_PGTABLE_QUIRK_ARM_TTBR1: (ARM LPAE format) Configure the table7882 * for use in the upper half of a split address space.7983 *8084 * IO_PGTABLE_QUIRK_ARM_OUTER_WBWA: Override the outer-cacheability8185 * attributes set in the TCR for a non-coherent page-table walker.8286 */8383- #define IO_PGTABLE_QUIRK_ARM_NS BIT(0)8484- #define IO_PGTABLE_QUIRK_NO_PERMS BIT(1)8585- #define IO_PGTABLE_QUIRK_ARM_MTK_EXT BIT(3)8686- #define IO_PGTABLE_QUIRK_ARM_TTBR1 BIT(5)8787- #define IO_PGTABLE_QUIRK_ARM_OUTER_WBWA BIT(6)8787+ #define IO_PGTABLE_QUIRK_ARM_NS BIT(0)8888+ #define IO_PGTABLE_QUIRK_NO_PERMS BIT(1)8989+ #define IO_PGTABLE_QUIRK_ARM_MTK_EXT BIT(3)9090+ #define IO_PGTABLE_QUIRK_ARM_MTK_TTBR_EXT BIT(4)9191+ #define IO_PGTABLE_QUIRK_ARM_TTBR1 BIT(5)9292+ #define IO_PGTABLE_QUIRK_ARM_OUTER_WBWA BIT(6)8893 unsigned long quirks;8994 unsigned long pgsize_bitmap;9095 unsigned int ias;
+11-16
include/linux/iommu.h
···135135 * @length: Length of the region in bytes136136 * @prot: IOMMU Protection flags (READ/WRITE/...)137137 * @type: Type of the reserved region138138+ * @free: Callback to free associated memory allocations138139 */139140struct iommu_resv_region {140141 struct list_head list;···143142 size_t length;144143 int prot;145144 enum iommu_resv_type type;145145+ void (*free)(struct device *dev, struct iommu_resv_region *region);146146+};147147+148148+struct iommu_iort_rmr_data {149149+ struct iommu_resv_region rr;150150+151151+ /* Stream IDs associated with IORT RMR entry */152152+ const u32 *sids;153153+ u32 num_sids;146154};147155148156/**···164154 * supported, this feature must be enabled before and165155 * disabled after %IOMMU_DEV_FEAT_SVA.166156 *167167- * Device drivers query whether a feature is supported using168168- * iommu_dev_has_feature(), and enable it using iommu_dev_enable_feature().157157+ * Device drivers enable a feature using iommu_dev_enable_feature().169158 */170159enum iommu_dev_features {171160 IOMMU_DEV_FEAT_SVA,···209200 * group and attached to the groups domain210201 * @device_group: find iommu group for a particular device211202 * @get_resv_regions: Request list of reserved regions for a device212212- * @put_resv_regions: Free list of reserved regions for a device213203 * @of_xlate: add OF master IDs to iommu grouping214204 * @is_attach_deferred: Check if domain attach should be deferred from iommu215205 * driver init to device driver init (default no)216206 * @dev_has/enable/disable_feat: per device entries to check/enable/disable217207 * iommu specific features.218218- * @dev_feat_enabled: check enabled feature219208 * @sva_bind: Bind process address space to device220209 * @sva_unbind: Unbind process address space from device221210 * @sva_get_pasid: Get PASID associated to a SVA handle···239232240233 /* Request/Free a list of reserved regions for a device */241234 void (*get_resv_regions)(struct device *dev, struct list_head *list);242242- void (*put_resv_regions)(struct device *dev, struct list_head *list);243235244236 int (*of_xlate)(struct device *dev, struct of_phandle_args *args);245237 bool (*is_attach_deferred)(struct device *dev);246238247239 /* Per device IOMMU features */248248- bool (*dev_has_feat)(struct device *dev, enum iommu_dev_features f);249249- bool (*dev_feat_enabled)(struct device *dev, enum iommu_dev_features f);250240 int (*dev_enable_feat)(struct device *dev, enum iommu_dev_features f);251241 int (*dev_disable_feat)(struct device *dev, enum iommu_dev_features f);252242···452448453449extern void iommu_get_resv_regions(struct device *dev, struct list_head *list);454450extern void iommu_put_resv_regions(struct device *dev, struct list_head *list);455455-extern void generic_iommu_put_resv_regions(struct device *dev,456456- struct list_head *list);457451extern void iommu_set_default_passthrough(bool cmd_line);458452extern void iommu_set_default_translated(bool cmd_line);459453extern bool iommu_default_passthrough(void);···664662665663int iommu_dev_enable_feature(struct device *dev, enum iommu_dev_features f);666664int iommu_dev_disable_feature(struct device *dev, enum iommu_dev_features f);667667-bool iommu_dev_feature_enabled(struct device *dev, enum iommu_dev_features f);668665669666struct iommu_sva *iommu_sva_bind_device(struct device *dev,670667 struct mm_struct *mm,···988987const struct iommu_ops *iommu_ops_from_fwnode(struct fwnode_handle *fwnode)989988{990989 return NULL;991991-}992992-993993-static inline bool994994-iommu_dev_feature_enabled(struct device *dev, enum iommu_dev_features feat)995995-{996996- return false;997990}998991999992static inline int
-2
include/linux/tboot.h
···126126extern void tboot_shutdown(u32 shutdown_type);127127extern struct acpi_table_header *tboot_get_dmar_table(128128 struct acpi_table_header *dmar_tbl);129129-extern int tboot_force_iommu(void);130129131130#else132131···135136#define tboot_sleep(sleep_state, pm1a_control, pm1b_control) \136137 do { } while (0)137138#define tboot_get_dmar_table(dmar_tbl) (dmar_tbl)138138-#define tboot_force_iommu() 0139139140140#endif /* !CONFIG_INTEL_TXT */141141