diff options
author | Joerg Roedel <joerg.roedel@amd.com> | 2011-11-21 09:59:08 -0500 |
---|---|---|
committer | Joerg Roedel <joerg.roedel@amd.com> | 2011-12-12 09:19:03 -0500 |
commit | 22e266c79b5bd5441243863c89ea237e6e845295 (patch) | |
tree | 665f0dc9f3ebbd6c7bda7917eb1dbbfc78bb49d0 /drivers/iommu | |
parent | 52815b75682e25db45545911fd2b09ef5856e695 (diff) |
iommu/amd: Implement IOMMUv2 TLB flushing routines
The functions added with this patch allow to manage the
IOMMU and the device TLBs for all devices in an IOMMUv2
domain.
Signed-off-by: Joerg Roedel <joerg.roedel@amd.com>
Diffstat (limited to 'drivers/iommu')
-rw-r--r-- | drivers/iommu/amd_iommu.c | 136 | ||||
-rw-r--r-- | drivers/iommu/amd_iommu_proto.h | 3 | ||||
-rw-r--r-- | drivers/iommu/amd_iommu_types.h | 1 |
3 files changed, 140 insertions, 0 deletions
diff --git a/drivers/iommu/amd_iommu.c b/drivers/iommu/amd_iommu.c index 7dda0d4a8f8c..b0861739a27d 100644 --- a/drivers/iommu/amd_iommu.c +++ b/drivers/iommu/amd_iommu.c | |||
@@ -698,6 +698,44 @@ static void build_inv_iotlb_pages(struct iommu_cmd *cmd, u16 devid, int qdep, | |||
698 | cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK; | 698 | cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK; |
699 | } | 699 | } |
700 | 700 | ||
701 | static void build_inv_iommu_pasid(struct iommu_cmd *cmd, u16 domid, int pasid, | ||
702 | u64 address, bool size) | ||
703 | { | ||
704 | memset(cmd, 0, sizeof(*cmd)); | ||
705 | |||
706 | address &= ~(0xfffULL); | ||
707 | |||
708 | cmd->data[0] = pasid & PASID_MASK; | ||
709 | cmd->data[1] = domid; | ||
710 | cmd->data[2] = lower_32_bits(address); | ||
711 | cmd->data[3] = upper_32_bits(address); | ||
712 | cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK; | ||
713 | cmd->data[2] |= CMD_INV_IOMMU_PAGES_GN_MASK; | ||
714 | if (size) | ||
715 | cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK; | ||
716 | CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES); | ||
717 | } | ||
718 | |||
719 | static void build_inv_iotlb_pasid(struct iommu_cmd *cmd, u16 devid, int pasid, | ||
720 | int qdep, u64 address, bool size) | ||
721 | { | ||
722 | memset(cmd, 0, sizeof(*cmd)); | ||
723 | |||
724 | address &= ~(0xfffULL); | ||
725 | |||
726 | cmd->data[0] = devid; | ||
727 | cmd->data[0] |= (pasid & 0xff) << 16; | ||
728 | cmd->data[0] |= (qdep & 0xff) << 24; | ||
729 | cmd->data[1] = devid; | ||
730 | cmd->data[1] |= ((pasid >> 8) & 0xfff) << 16; | ||
731 | cmd->data[2] = lower_32_bits(address); | ||
732 | cmd->data[2] |= CMD_INV_IOMMU_PAGES_GN_MASK; | ||
733 | cmd->data[3] = upper_32_bits(address); | ||
734 | if (size) | ||
735 | cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK; | ||
736 | CMD_SET_TYPE(cmd, CMD_INV_IOTLB_PAGES); | ||
737 | } | ||
738 | |||
701 | static void build_inv_all(struct iommu_cmd *cmd) | 739 | static void build_inv_all(struct iommu_cmd *cmd) |
702 | { | 740 | { |
703 | memset(cmd, 0, sizeof(*cmd)); | 741 | memset(cmd, 0, sizeof(*cmd)); |
@@ -3146,3 +3184,101 @@ out: | |||
3146 | return ret; | 3184 | return ret; |
3147 | } | 3185 | } |
3148 | EXPORT_SYMBOL(amd_iommu_domain_enable_v2); | 3186 | EXPORT_SYMBOL(amd_iommu_domain_enable_v2); |
3187 | |||
3188 | static int __flush_pasid(struct protection_domain *domain, int pasid, | ||
3189 | u64 address, bool size) | ||
3190 | { | ||
3191 | struct iommu_dev_data *dev_data; | ||
3192 | struct iommu_cmd cmd; | ||
3193 | int i, ret; | ||
3194 | |||
3195 | if (!(domain->flags & PD_IOMMUV2_MASK)) | ||
3196 | return -EINVAL; | ||
3197 | |||
3198 | build_inv_iommu_pasid(&cmd, domain->id, pasid, address, size); | ||
3199 | |||
3200 | /* | ||
3201 | * IOMMU TLB needs to be flushed before Device TLB to | ||
3202 | * prevent device TLB refill from IOMMU TLB | ||
3203 | */ | ||
3204 | for (i = 0; i < amd_iommus_present; ++i) { | ||
3205 | if (domain->dev_iommu[i] == 0) | ||
3206 | continue; | ||
3207 | |||
3208 | ret = iommu_queue_command(amd_iommus[i], &cmd); | ||
3209 | if (ret != 0) | ||
3210 | goto out; | ||
3211 | } | ||
3212 | |||
3213 | /* Wait until IOMMU TLB flushes are complete */ | ||
3214 | domain_flush_complete(domain); | ||
3215 | |||
3216 | /* Now flush device TLBs */ | ||
3217 | list_for_each_entry(dev_data, &domain->dev_list, list) { | ||
3218 | struct amd_iommu *iommu; | ||
3219 | int qdep; | ||
3220 | |||
3221 | BUG_ON(!dev_data->ats.enabled); | ||
3222 | |||
3223 | qdep = dev_data->ats.qdep; | ||
3224 | iommu = amd_iommu_rlookup_table[dev_data->devid]; | ||
3225 | |||
3226 | build_inv_iotlb_pasid(&cmd, dev_data->devid, pasid, | ||
3227 | qdep, address, size); | ||
3228 | |||
3229 | ret = iommu_queue_command(iommu, &cmd); | ||
3230 | if (ret != 0) | ||
3231 | goto out; | ||
3232 | } | ||
3233 | |||
3234 | /* Wait until all device TLBs are flushed */ | ||
3235 | domain_flush_complete(domain); | ||
3236 | |||
3237 | ret = 0; | ||
3238 | |||
3239 | out: | ||
3240 | |||
3241 | return ret; | ||
3242 | } | ||
3243 | |||
3244 | static int __amd_iommu_flush_page(struct protection_domain *domain, int pasid, | ||
3245 | u64 address) | ||
3246 | { | ||
3247 | return __flush_pasid(domain, pasid, address, false); | ||
3248 | } | ||
3249 | |||
3250 | int amd_iommu_flush_page(struct iommu_domain *dom, int pasid, | ||
3251 | u64 address) | ||
3252 | { | ||
3253 | struct protection_domain *domain = dom->priv; | ||
3254 | unsigned long flags; | ||
3255 | int ret; | ||
3256 | |||
3257 | spin_lock_irqsave(&domain->lock, flags); | ||
3258 | ret = __amd_iommu_flush_page(domain, pasid, address); | ||
3259 | spin_unlock_irqrestore(&domain->lock, flags); | ||
3260 | |||
3261 | return ret; | ||
3262 | } | ||
3263 | EXPORT_SYMBOL(amd_iommu_flush_page); | ||
3264 | |||
3265 | static int __amd_iommu_flush_tlb(struct protection_domain *domain, int pasid) | ||
3266 | { | ||
3267 | return __flush_pasid(domain, pasid, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, | ||
3268 | true); | ||
3269 | } | ||
3270 | |||
3271 | int amd_iommu_flush_tlb(struct iommu_domain *dom, int pasid) | ||
3272 | { | ||
3273 | struct protection_domain *domain = dom->priv; | ||
3274 | unsigned long flags; | ||
3275 | int ret; | ||
3276 | |||
3277 | spin_lock_irqsave(&domain->lock, flags); | ||
3278 | ret = __amd_iommu_flush_tlb(domain, pasid); | ||
3279 | spin_unlock_irqrestore(&domain->lock, flags); | ||
3280 | |||
3281 | return ret; | ||
3282 | } | ||
3283 | EXPORT_SYMBOL(amd_iommu_flush_tlb); | ||
3284 | |||
diff --git a/drivers/iommu/amd_iommu_proto.h b/drivers/iommu/amd_iommu_proto.h index d207b1d951b2..a92dc6117e2f 100644 --- a/drivers/iommu/amd_iommu_proto.h +++ b/drivers/iommu/amd_iommu_proto.h | |||
@@ -40,6 +40,9 @@ extern int amd_iommu_register_ppr_notifier(struct notifier_block *nb); | |||
40 | extern int amd_iommu_unregister_ppr_notifier(struct notifier_block *nb); | 40 | extern int amd_iommu_unregister_ppr_notifier(struct notifier_block *nb); |
41 | extern void amd_iommu_domain_direct_map(struct iommu_domain *dom); | 41 | extern void amd_iommu_domain_direct_map(struct iommu_domain *dom); |
42 | extern int amd_iommu_domain_enable_v2(struct iommu_domain *dom, int pasids); | 42 | extern int amd_iommu_domain_enable_v2(struct iommu_domain *dom, int pasids); |
43 | extern int amd_iommu_flush_page(struct iommu_domain *dom, int pasid, | ||
44 | u64 address); | ||
45 | extern int amd_iommu_flush_tlb(struct iommu_domain *dom, int pasid); | ||
43 | 46 | ||
44 | #ifndef CONFIG_AMD_IOMMU_STATS | 47 | #ifndef CONFIG_AMD_IOMMU_STATS |
45 | 48 | ||
diff --git a/drivers/iommu/amd_iommu_types.h b/drivers/iommu/amd_iommu_types.h index b7583cb5ad66..ff1dfe9ad579 100644 --- a/drivers/iommu/amd_iommu_types.h +++ b/drivers/iommu/amd_iommu_types.h | |||
@@ -148,6 +148,7 @@ | |||
148 | #define CMD_COMPL_WAIT_INT_MASK 0x02 | 148 | #define CMD_COMPL_WAIT_INT_MASK 0x02 |
149 | #define CMD_INV_IOMMU_PAGES_SIZE_MASK 0x01 | 149 | #define CMD_INV_IOMMU_PAGES_SIZE_MASK 0x01 |
150 | #define CMD_INV_IOMMU_PAGES_PDE_MASK 0x02 | 150 | #define CMD_INV_IOMMU_PAGES_PDE_MASK 0x02 |
151 | #define CMD_INV_IOMMU_PAGES_GN_MASK 0x04 | ||
151 | 152 | ||
152 | #define CMD_INV_IOMMU_ALL_PAGES_ADDRESS 0x7fffffffffffffffULL | 153 | #define CMD_INV_IOMMU_ALL_PAGES_ADDRESS 0x7fffffffffffffffULL |
153 | 154 | ||