|
12 | 12 | #include <linux/dmar.h>
|
13 | 13 | #include <linux/iommu.h>
|
14 | 14 | #include <linux/memory.h>
|
| 15 | +#include <linux/pci.h> |
15 | 16 | #include <linux/spinlock.h>
|
16 | 17 |
|
17 | 18 | #include "iommu.h"
|
@@ -212,3 +213,197 @@ void cache_tag_unassign_domain(struct dmar_domain *domain,
|
212 | 213 | if (domain->domain.type == IOMMU_DOMAIN_NESTED)
|
213 | 214 | __cache_tag_unassign_parent_domain(domain->s2_domain, did, dev, pasid);
|
214 | 215 | }
|
| 216 | + |
| 217 | +static unsigned long calculate_psi_aligned_address(unsigned long start, |
| 218 | + unsigned long end, |
| 219 | + unsigned long *_pages, |
| 220 | + unsigned long *_mask) |
| 221 | +{ |
| 222 | + unsigned long pages = aligned_nrpages(start, end - start + 1); |
| 223 | + unsigned long aligned_pages = __roundup_pow_of_two(pages); |
| 224 | + unsigned long bitmask = aligned_pages - 1; |
| 225 | + unsigned long mask = ilog2(aligned_pages); |
| 226 | + unsigned long pfn = IOVA_PFN(start); |
| 227 | + |
| 228 | + /* |
| 229 | + * PSI masks the low order bits of the base address. If the |
| 230 | + * address isn't aligned to the mask, then compute a mask value |
| 231 | + * needed to ensure the target range is flushed. |
| 232 | + */ |
| 233 | + if (unlikely(bitmask & pfn)) { |
| 234 | + unsigned long end_pfn = pfn + pages - 1, shared_bits; |
| 235 | + |
| 236 | + /* |
| 237 | + * Since end_pfn <= pfn + bitmask, the only way bits |
| 238 | + * higher than bitmask can differ in pfn and end_pfn is |
| 239 | + * by carrying. This means after masking out bitmask, |
| 240 | + * high bits starting with the first set bit in |
| 241 | + * shared_bits are all equal in both pfn and end_pfn. |
| 242 | + */ |
| 243 | + shared_bits = ~(pfn ^ end_pfn) & ~bitmask; |
| 244 | + mask = shared_bits ? __ffs(shared_bits) : BITS_PER_LONG; |
| 245 | + } |
| 246 | + |
| 247 | + *_pages = aligned_pages; |
| 248 | + *_mask = mask; |
| 249 | + |
| 250 | + return ALIGN_DOWN(start, VTD_PAGE_SIZE << mask); |
| 251 | +} |
| 252 | + |
| 253 | +/* |
| 254 | + * Invalidates a range of IOVA from @start (inclusive) to @end (inclusive) |
| 255 | + * when the memory mappings in the target domain have been modified. |
| 256 | + */ |
| 257 | +void cache_tag_flush_range(struct dmar_domain *domain, unsigned long start, |
| 258 | + unsigned long end, int ih) |
| 259 | +{ |
| 260 | + unsigned long pages, mask, addr; |
| 261 | + struct cache_tag *tag; |
| 262 | + unsigned long flags; |
| 263 | + |
| 264 | + addr = calculate_psi_aligned_address(start, end, &pages, &mask); |
| 265 | + |
| 266 | + spin_lock_irqsave(&domain->cache_lock, flags); |
| 267 | + list_for_each_entry(tag, &domain->cache_tags, node) { |
| 268 | + struct intel_iommu *iommu = tag->iommu; |
| 269 | + struct device_domain_info *info; |
| 270 | + u16 sid; |
| 271 | + |
| 272 | + switch (tag->type) { |
| 273 | + case CACHE_TAG_IOTLB: |
| 274 | + case CACHE_TAG_NESTING_IOTLB: |
| 275 | + if (domain->use_first_level) { |
| 276 | + qi_flush_piotlb(iommu, tag->domain_id, |
| 277 | + tag->pasid, addr, pages, ih); |
| 278 | + } else { |
| 279 | + /* |
| 280 | + * Fallback to domain selective flush if no |
| 281 | + * PSI support or the size is too big. |
| 282 | + */ |
| 283 | + if (!cap_pgsel_inv(iommu->cap) || |
| 284 | + mask > cap_max_amask_val(iommu->cap)) |
| 285 | + iommu->flush.flush_iotlb(iommu, tag->domain_id, |
| 286 | + 0, 0, DMA_TLB_DSI_FLUSH); |
| 287 | + else |
| 288 | + iommu->flush.flush_iotlb(iommu, tag->domain_id, |
| 289 | + addr | ih, mask, |
| 290 | + DMA_TLB_PSI_FLUSH); |
| 291 | + } |
| 292 | + break; |
| 293 | + case CACHE_TAG_NESTING_DEVTLB: |
| 294 | + /* |
| 295 | + * Address translation cache in device side caches the |
| 296 | + * result of nested translation. There is no easy way |
| 297 | + * to identify the exact set of nested translations |
| 298 | + * affected by a change in S2. So just flush the entire |
| 299 | + * device cache. |
| 300 | + */ |
| 301 | + addr = 0; |
| 302 | + mask = MAX_AGAW_PFN_WIDTH; |
| 303 | + fallthrough; |
| 304 | + case CACHE_TAG_DEVTLB: |
| 305 | + info = dev_iommu_priv_get(tag->dev); |
| 306 | + sid = PCI_DEVID(info->bus, info->devfn); |
| 307 | + |
| 308 | + if (tag->pasid == IOMMU_NO_PASID) |
| 309 | + qi_flush_dev_iotlb(iommu, sid, info->pfsid, |
| 310 | + info->ats_qdep, addr, mask); |
| 311 | + else |
| 312 | + qi_flush_dev_iotlb_pasid(iommu, sid, info->pfsid, |
| 313 | + tag->pasid, info->ats_qdep, |
| 314 | + addr, mask); |
| 315 | + |
| 316 | + quirk_extra_dev_tlb_flush(info, addr, mask, tag->pasid, info->ats_qdep); |
| 317 | + break; |
| 318 | + } |
| 319 | + } |
| 320 | + spin_unlock_irqrestore(&domain->cache_lock, flags); |
| 321 | +} |
| 322 | + |
| 323 | +/* |
| 324 | + * Invalidates all ranges of IOVA when the memory mappings in the target |
| 325 | + * domain have been modified. |
| 326 | + */ |
| 327 | +void cache_tag_flush_all(struct dmar_domain *domain) |
| 328 | +{ |
| 329 | + struct cache_tag *tag; |
| 330 | + unsigned long flags; |
| 331 | + |
| 332 | + spin_lock_irqsave(&domain->cache_lock, flags); |
| 333 | + list_for_each_entry(tag, &domain->cache_tags, node) { |
| 334 | + struct intel_iommu *iommu = tag->iommu; |
| 335 | + struct device_domain_info *info; |
| 336 | + u16 sid; |
| 337 | + |
| 338 | + switch (tag->type) { |
| 339 | + case CACHE_TAG_IOTLB: |
| 340 | + case CACHE_TAG_NESTING_IOTLB: |
| 341 | + if (domain->use_first_level) |
| 342 | + qi_flush_piotlb(iommu, tag->domain_id, |
| 343 | + tag->pasid, 0, -1, 0); |
| 344 | + else |
| 345 | + iommu->flush.flush_iotlb(iommu, tag->domain_id, |
| 346 | + 0, 0, DMA_TLB_DSI_FLUSH); |
| 347 | + break; |
| 348 | + case CACHE_TAG_DEVTLB: |
| 349 | + case CACHE_TAG_NESTING_DEVTLB: |
| 350 | + info = dev_iommu_priv_get(tag->dev); |
| 351 | + sid = PCI_DEVID(info->bus, info->devfn); |
| 352 | + |
| 353 | + qi_flush_dev_iotlb(iommu, sid, info->pfsid, info->ats_qdep, |
| 354 | + 0, MAX_AGAW_PFN_WIDTH); |
| 355 | + quirk_extra_dev_tlb_flush(info, 0, MAX_AGAW_PFN_WIDTH, |
| 356 | + IOMMU_NO_PASID, info->ats_qdep); |
| 357 | + break; |
| 358 | + } |
| 359 | + } |
| 360 | + spin_unlock_irqrestore(&domain->cache_lock, flags); |
| 361 | +} |
| 362 | + |
| 363 | +/* |
| 364 | + * Invalidate a range of IOVA when new mappings are created in the target |
| 365 | + * domain. |
| 366 | + * |
| 367 | + * - VT-d spec, Section 6.1 Caching Mode: When the CM field is reported as |
| 368 | + * Set, any software updates to remapping structures other than first- |
| 369 | + * stage mapping requires explicit invalidation of the caches. |
| 370 | + * - VT-d spec, Section 6.8 Write Buffer Flushing: For hardware that requires |
| 371 | + * write buffer flushing, software must explicitly perform write-buffer |
| 372 | + * flushing, if cache invalidation is not required. |
| 373 | + */ |
| 374 | +void cache_tag_flush_range_np(struct dmar_domain *domain, unsigned long start, |
| 375 | + unsigned long end) |
| 376 | +{ |
| 377 | + unsigned long pages, mask, addr; |
| 378 | + struct cache_tag *tag; |
| 379 | + unsigned long flags; |
| 380 | + |
| 381 | + addr = calculate_psi_aligned_address(start, end, &pages, &mask); |
| 382 | + |
| 383 | + spin_lock_irqsave(&domain->cache_lock, flags); |
| 384 | + list_for_each_entry(tag, &domain->cache_tags, node) { |
| 385 | + struct intel_iommu *iommu = tag->iommu; |
| 386 | + |
| 387 | + if (!cap_caching_mode(iommu->cap) || domain->use_first_level) { |
| 388 | + iommu_flush_write_buffer(iommu); |
| 389 | + continue; |
| 390 | + } |
| 391 | + |
| 392 | + if (tag->type == CACHE_TAG_IOTLB || |
| 393 | + tag->type == CACHE_TAG_NESTING_IOTLB) { |
| 394 | + /* |
| 395 | + * Fallback to domain selective flush if no |
| 396 | + * PSI support or the size is too big. |
| 397 | + */ |
| 398 | + if (!cap_pgsel_inv(iommu->cap) || |
| 399 | + mask > cap_max_amask_val(iommu->cap)) |
| 400 | + iommu->flush.flush_iotlb(iommu, tag->domain_id, |
| 401 | + 0, 0, DMA_TLB_DSI_FLUSH); |
| 402 | + else |
| 403 | + iommu->flush.flush_iotlb(iommu, tag->domain_id, |
| 404 | + addr, mask, |
| 405 | + DMA_TLB_PSI_FLUSH); |
| 406 | + } |
| 407 | + } |
| 408 | + spin_unlock_irqrestore(&domain->cache_lock, flags); |
| 409 | +} |
0 commit comments