1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * intel-pasid.c - PASID idr, table and entry manipulation
4 *
5 * Copyright (C) 2018 Intel Corporation
6 *
7 * Author: Lu Baolu <baolu.lu@linux.intel.com>
8 */
9
10 #define pr_fmt(fmt) "DMAR: " fmt
11
12 #include <linux/bitops.h>
13 #include <linux/cpufeature.h>
14 #include <linux/dmar.h>
15 #include <linux/iommu.h>
16 #include <linux/memory.h>
17 #include <linux/pci.h>
18 #include <linux/pci-ats.h>
19 #include <linux/spinlock.h>
20
21 #include "iommu.h"
22 #include "pasid.h"
23 #include "../iommu-pages.h"
24
25 /*
26 * Intel IOMMU system wide PASID name space:
27 */
28 u32 intel_pasid_max_id = PASID_MAX;
29
30 /*
31 * Per device pasid table management:
32 */
33
34 /*
35 * Allocate a pasid table for @dev. It should be called in a
36 * single-thread context.
37 */
intel_pasid_alloc_table(struct device * dev)38 int intel_pasid_alloc_table(struct device *dev)
39 {
40 struct device_domain_info *info;
41 struct pasid_table *pasid_table;
42 struct pasid_dir_entry *dir;
43 u32 max_pasid = 0;
44 int order, size;
45
46 might_sleep();
47 info = dev_iommu_priv_get(dev);
48 if (WARN_ON(!info || !dev_is_pci(dev)))
49 return -ENODEV;
50 if (WARN_ON(info->pasid_table))
51 return -EEXIST;
52
53 pasid_table = kzalloc(sizeof(*pasid_table), GFP_KERNEL);
54 if (!pasid_table)
55 return -ENOMEM;
56
57 if (info->pasid_supported)
58 max_pasid = min_t(u32, pci_max_pasids(to_pci_dev(dev)),
59 intel_pasid_max_id);
60
61 size = max_pasid >> (PASID_PDE_SHIFT - 3);
62 order = size ? get_order(size) : 0;
63 dir = iommu_alloc_pages_node_sz(info->iommu->node, GFP_KERNEL,
64 1 << (order + PAGE_SHIFT));
65 if (!dir) {
66 kfree(pasid_table);
67 return -ENOMEM;
68 }
69
70 pasid_table->table = dir;
71 pasid_table->max_pasid = 1 << (order + PAGE_SHIFT + 3);
72 info->pasid_table = pasid_table;
73
74 if (!ecap_coherent(info->iommu->ecap))
75 clflush_cache_range(pasid_table->table, (1 << order) * PAGE_SIZE);
76
77 return 0;
78 }
79
intel_pasid_free_table(struct device * dev)80 void intel_pasid_free_table(struct device *dev)
81 {
82 struct device_domain_info *info;
83 struct pasid_table *pasid_table;
84 struct pasid_dir_entry *dir;
85 struct pasid_entry *table;
86 int i, max_pde;
87
88 info = dev_iommu_priv_get(dev);
89 if (!info || !dev_is_pci(dev) || !info->pasid_table)
90 return;
91
92 pasid_table = info->pasid_table;
93 info->pasid_table = NULL;
94
95 /* Free scalable mode PASID directory tables: */
96 dir = pasid_table->table;
97 max_pde = pasid_table->max_pasid >> PASID_PDE_SHIFT;
98 for (i = 0; i < max_pde; i++) {
99 table = get_pasid_table_from_pde(&dir[i]);
100 iommu_free_pages(table);
101 }
102
103 iommu_free_pages(pasid_table->table);
104 kfree(pasid_table);
105 }
106
intel_pasid_get_table(struct device * dev)107 struct pasid_table *intel_pasid_get_table(struct device *dev)
108 {
109 struct device_domain_info *info;
110
111 info = dev_iommu_priv_get(dev);
112 if (!info)
113 return NULL;
114
115 return info->pasid_table;
116 }
117
intel_pasid_get_dev_max_id(struct device * dev)118 static int intel_pasid_get_dev_max_id(struct device *dev)
119 {
120 struct device_domain_info *info;
121
122 info = dev_iommu_priv_get(dev);
123 if (!info || !info->pasid_table)
124 return 0;
125
126 return info->pasid_table->max_pasid;
127 }
128
intel_pasid_get_entry(struct device * dev,u32 pasid)129 static struct pasid_entry *intel_pasid_get_entry(struct device *dev, u32 pasid)
130 {
131 struct device_domain_info *info;
132 struct pasid_table *pasid_table;
133 struct pasid_dir_entry *dir;
134 struct pasid_entry *entries;
135 int dir_index, index;
136
137 pasid_table = intel_pasid_get_table(dev);
138 if (WARN_ON(!pasid_table || pasid >= intel_pasid_get_dev_max_id(dev)))
139 return NULL;
140
141 dir = pasid_table->table;
142 info = dev_iommu_priv_get(dev);
143 dir_index = pasid >> PASID_PDE_SHIFT;
144 index = pasid & PASID_PTE_MASK;
145
146 retry:
147 entries = get_pasid_table_from_pde(&dir[dir_index]);
148 if (!entries) {
149 u64 tmp;
150
151 entries = iommu_alloc_pages_node_sz(info->iommu->node,
152 GFP_ATOMIC, SZ_4K);
153 if (!entries)
154 return NULL;
155
156 /*
157 * The pasid directory table entry won't be freed after
158 * allocation. No worry about the race with free and
159 * clear. However, this entry might be populated by others
160 * while we are preparing it. Use theirs with a retry.
161 */
162 tmp = 0ULL;
163 if (!try_cmpxchg64(&dir[dir_index].val, &tmp,
164 (u64)virt_to_phys(entries) | PASID_PTE_PRESENT)) {
165 iommu_free_pages(entries);
166 goto retry;
167 }
168 if (!ecap_coherent(info->iommu->ecap)) {
169 clflush_cache_range(entries, VTD_PAGE_SIZE);
170 clflush_cache_range(&dir[dir_index].val, sizeof(*dir));
171 }
172 }
173
174 return &entries[index];
175 }
176
177 /*
178 * Interfaces for PASID table entry manipulation:
179 */
180 static void
intel_pasid_clear_entry(struct device * dev,u32 pasid,bool fault_ignore)181 intel_pasid_clear_entry(struct device *dev, u32 pasid, bool fault_ignore)
182 {
183 struct pasid_entry *pe;
184
185 pe = intel_pasid_get_entry(dev, pasid);
186 if (WARN_ON(!pe))
187 return;
188
189 if (fault_ignore && pasid_pte_is_present(pe))
190 pasid_clear_entry_with_fpd(pe);
191 else
192 pasid_clear_entry(pe);
193 }
194
195 static void
pasid_cache_invalidation_with_pasid(struct intel_iommu * iommu,u16 did,u32 pasid)196 pasid_cache_invalidation_with_pasid(struct intel_iommu *iommu,
197 u16 did, u32 pasid)
198 {
199 struct qi_desc desc;
200
201 desc.qw0 = QI_PC_DID(did) | QI_PC_GRAN(QI_PC_PASID_SEL) |
202 QI_PC_PASID(pasid) | QI_PC_TYPE;
203 desc.qw1 = 0;
204 desc.qw2 = 0;
205 desc.qw3 = 0;
206
207 qi_submit_sync(iommu, &desc, 1, 0);
208 }
209
210 static void
devtlb_invalidation_with_pasid(struct intel_iommu * iommu,struct device * dev,u32 pasid)211 devtlb_invalidation_with_pasid(struct intel_iommu *iommu,
212 struct device *dev, u32 pasid)
213 {
214 struct device_domain_info *info;
215 u16 sid, qdep, pfsid;
216
217 info = dev_iommu_priv_get(dev);
218 if (!info || !info->ats_enabled)
219 return;
220
221 if (pci_dev_is_disconnected(to_pci_dev(dev)))
222 return;
223
224 sid = PCI_DEVID(info->bus, info->devfn);
225 qdep = info->ats_qdep;
226 pfsid = info->pfsid;
227
228 /*
229 * When PASID 0 is used, it indicates RID2PASID(DMA request w/o PASID),
230 * devTLB flush w/o PASID should be used. For non-zero PASID under
231 * SVA usage, device could do DMA with multiple PASIDs. It is more
232 * efficient to flush devTLB specific to the PASID.
233 */
234 if (pasid == IOMMU_NO_PASID)
235 qi_flush_dev_iotlb(iommu, sid, pfsid, qdep, 0, 64 - VTD_PAGE_SHIFT);
236 else
237 qi_flush_dev_iotlb_pasid(iommu, sid, pfsid, pasid, qdep, 0, 64 - VTD_PAGE_SHIFT);
238 }
239
intel_pasid_tear_down_entry(struct intel_iommu * iommu,struct device * dev,u32 pasid,bool fault_ignore)240 void intel_pasid_tear_down_entry(struct intel_iommu *iommu, struct device *dev,
241 u32 pasid, bool fault_ignore)
242 {
243 struct pasid_entry *pte;
244 u16 did, pgtt;
245
246 spin_lock(&iommu->lock);
247 pte = intel_pasid_get_entry(dev, pasid);
248 if (WARN_ON(!pte)) {
249 spin_unlock(&iommu->lock);
250 return;
251 }
252
253 if (!pasid_pte_is_present(pte)) {
254 if (!pasid_pte_is_fault_disabled(pte)) {
255 WARN_ON(READ_ONCE(pte->val[0]) != 0);
256 spin_unlock(&iommu->lock);
257 return;
258 }
259
260 /*
261 * When a PASID is used for SVA by a device, it's possible
262 * that the pasid entry is non-present with the Fault
263 * Processing Disabled bit set. Clear the pasid entry and
264 * drain the PRQ for the PASID before return.
265 */
266 pasid_clear_entry(pte);
267 spin_unlock(&iommu->lock);
268 intel_iommu_drain_pasid_prq(dev, pasid);
269
270 return;
271 }
272
273 did = pasid_get_domain_id(pte);
274 pgtt = pasid_pte_get_pgtt(pte);
275 intel_pasid_clear_entry(dev, pasid, fault_ignore);
276 spin_unlock(&iommu->lock);
277
278 if (!ecap_coherent(iommu->ecap))
279 clflush_cache_range(pte, sizeof(*pte));
280
281 pasid_cache_invalidation_with_pasid(iommu, did, pasid);
282
283 if (pgtt == PASID_ENTRY_PGTT_PT || pgtt == PASID_ENTRY_PGTT_FL_ONLY)
284 qi_flush_piotlb(iommu, did, pasid, 0, -1, 0);
285 else
286 iommu->flush.flush_iotlb(iommu, did, 0, 0, DMA_TLB_DSI_FLUSH);
287
288 devtlb_invalidation_with_pasid(iommu, dev, pasid);
289 if (!fault_ignore)
290 intel_iommu_drain_pasid_prq(dev, pasid);
291 }
292
293 /*
294 * This function flushes cache for a newly setup pasid table entry.
295 * Caller of it should not modify the in-use pasid table entries.
296 */
pasid_flush_caches(struct intel_iommu * iommu,struct pasid_entry * pte,u32 pasid,u16 did)297 static void pasid_flush_caches(struct intel_iommu *iommu,
298 struct pasid_entry *pte,
299 u32 pasid, u16 did)
300 {
301 if (!ecap_coherent(iommu->ecap))
302 clflush_cache_range(pte, sizeof(*pte));
303
304 if (cap_caching_mode(iommu->cap)) {
305 pasid_cache_invalidation_with_pasid(iommu, did, pasid);
306 qi_flush_piotlb(iommu, did, pasid, 0, -1, 0);
307 } else {
308 iommu_flush_write_buffer(iommu);
309 }
310 }
311
312 /*
313 * This function is supposed to be used after caller updates the fields
314 * except for the SSADE and P bit of a pasid table entry. It does the
315 * below:
316 * - Flush cacheline if needed
317 * - Flush the caches per Table 28 ”Guidance to Software for Invalidations“
318 * of VT-d spec 5.0.
319 */
intel_pasid_flush_present(struct intel_iommu * iommu,struct device * dev,u32 pasid,u16 did,struct pasid_entry * pte)320 static void intel_pasid_flush_present(struct intel_iommu *iommu,
321 struct device *dev,
322 u32 pasid, u16 did,
323 struct pasid_entry *pte)
324 {
325 if (!ecap_coherent(iommu->ecap))
326 clflush_cache_range(pte, sizeof(*pte));
327
328 /*
329 * VT-d spec 5.0 table28 states guides for cache invalidation:
330 *
331 * - PASID-selective-within-Domain PASID-cache invalidation
332 * - PASID-selective PASID-based IOTLB invalidation
333 * - If (pasid is RID_PASID)
334 * - Global Device-TLB invalidation to affected functions
335 * Else
336 * - PASID-based Device-TLB invalidation (with S=1 and
337 * Addr[63:12]=0x7FFFFFFF_FFFFF) to affected functions
338 */
339 pasid_cache_invalidation_with_pasid(iommu, did, pasid);
340 qi_flush_piotlb(iommu, did, pasid, 0, -1, 0);
341
342 devtlb_invalidation_with_pasid(iommu, dev, pasid);
343 }
344
345 /*
346 * Set up the scalable mode pasid table entry for first only
347 * translation type.
348 */
pasid_pte_config_first_level(struct intel_iommu * iommu,struct pasid_entry * pte,pgd_t * pgd,u16 did,int flags)349 static void pasid_pte_config_first_level(struct intel_iommu *iommu,
350 struct pasid_entry *pte,
351 pgd_t *pgd, u16 did, int flags)
352 {
353 lockdep_assert_held(&iommu->lock);
354
355 pasid_clear_entry(pte);
356
357 /* Setup the first level page table pointer: */
358 pasid_set_flptr(pte, (u64)__pa(pgd));
359
360 if (flags & PASID_FLAG_FL5LP)
361 pasid_set_flpm(pte, 1);
362
363 if (flags & PASID_FLAG_PAGE_SNOOP)
364 pasid_set_pgsnp(pte);
365
366 pasid_set_domain_id(pte, did);
367 pasid_set_address_width(pte, iommu->agaw);
368 pasid_set_page_snoop(pte, !!ecap_smpwc(iommu->ecap));
369
370 /* Setup Present and PASID Granular Transfer Type: */
371 pasid_set_translation_type(pte, PASID_ENTRY_PGTT_FL_ONLY);
372 pasid_set_present(pte);
373 }
374
intel_pasid_setup_first_level(struct intel_iommu * iommu,struct device * dev,pgd_t * pgd,u32 pasid,u16 did,int flags)375 int intel_pasid_setup_first_level(struct intel_iommu *iommu,
376 struct device *dev, pgd_t *pgd,
377 u32 pasid, u16 did, int flags)
378 {
379 struct pasid_entry *pte;
380
381 if (!ecap_flts(iommu->ecap)) {
382 pr_err("No first level translation support on %s\n",
383 iommu->name);
384 return -EINVAL;
385 }
386
387 if ((flags & PASID_FLAG_FL5LP) && !cap_fl5lp_support(iommu->cap)) {
388 pr_err("No 5-level paging support for first-level on %s\n",
389 iommu->name);
390 return -EINVAL;
391 }
392
393 spin_lock(&iommu->lock);
394 pte = intel_pasid_get_entry(dev, pasid);
395 if (!pte) {
396 spin_unlock(&iommu->lock);
397 return -ENODEV;
398 }
399
400 if (pasid_pte_is_present(pte)) {
401 spin_unlock(&iommu->lock);
402 return -EBUSY;
403 }
404
405 pasid_pte_config_first_level(iommu, pte, pgd, did, flags);
406
407 spin_unlock(&iommu->lock);
408
409 pasid_flush_caches(iommu, pte, pasid, did);
410
411 return 0;
412 }
413
intel_pasid_replace_first_level(struct intel_iommu * iommu,struct device * dev,pgd_t * pgd,u32 pasid,u16 did,u16 old_did,int flags)414 int intel_pasid_replace_first_level(struct intel_iommu *iommu,
415 struct device *dev, pgd_t *pgd,
416 u32 pasid, u16 did, u16 old_did,
417 int flags)
418 {
419 struct pasid_entry *pte, new_pte;
420
421 if (!ecap_flts(iommu->ecap)) {
422 pr_err("No first level translation support on %s\n",
423 iommu->name);
424 return -EINVAL;
425 }
426
427 if ((flags & PASID_FLAG_FL5LP) && !cap_fl5lp_support(iommu->cap)) {
428 pr_err("No 5-level paging support for first-level on %s\n",
429 iommu->name);
430 return -EINVAL;
431 }
432
433 pasid_pte_config_first_level(iommu, &new_pte, pgd, did, flags);
434
435 spin_lock(&iommu->lock);
436 pte = intel_pasid_get_entry(dev, pasid);
437 if (!pte) {
438 spin_unlock(&iommu->lock);
439 return -ENODEV;
440 }
441
442 if (!pasid_pte_is_present(pte)) {
443 spin_unlock(&iommu->lock);
444 return -EINVAL;
445 }
446
447 WARN_ON(old_did != pasid_get_domain_id(pte));
448
449 *pte = new_pte;
450 spin_unlock(&iommu->lock);
451
452 intel_pasid_flush_present(iommu, dev, pasid, old_did, pte);
453 intel_iommu_drain_pasid_prq(dev, pasid);
454
455 return 0;
456 }
457
458 /*
459 * Set up the scalable mode pasid entry for second only translation type.
460 */
pasid_pte_config_second_level(struct intel_iommu * iommu,struct pasid_entry * pte,u64 pgd_val,int agaw,u16 did,bool dirty_tracking)461 static void pasid_pte_config_second_level(struct intel_iommu *iommu,
462 struct pasid_entry *pte,
463 u64 pgd_val, int agaw, u16 did,
464 bool dirty_tracking)
465 {
466 lockdep_assert_held(&iommu->lock);
467
468 pasid_clear_entry(pte);
469 pasid_set_domain_id(pte, did);
470 pasid_set_slptr(pte, pgd_val);
471 pasid_set_address_width(pte, agaw);
472 pasid_set_translation_type(pte, PASID_ENTRY_PGTT_SL_ONLY);
473 pasid_set_fault_enable(pte);
474 pasid_set_page_snoop(pte, !!ecap_smpwc(iommu->ecap));
475 if (dirty_tracking)
476 pasid_set_ssade(pte);
477
478 pasid_set_present(pte);
479 }
480
intel_pasid_setup_second_level(struct intel_iommu * iommu,struct dmar_domain * domain,struct device * dev,u32 pasid)481 int intel_pasid_setup_second_level(struct intel_iommu *iommu,
482 struct dmar_domain *domain,
483 struct device *dev, u32 pasid)
484 {
485 struct pasid_entry *pte;
486 struct dma_pte *pgd;
487 u64 pgd_val;
488 u16 did;
489
490 /*
491 * If hardware advertises no support for second level
492 * translation, return directly.
493 */
494 if (!ecap_slts(iommu->ecap)) {
495 pr_err("No second level translation support on %s\n",
496 iommu->name);
497 return -EINVAL;
498 }
499
500 pgd = domain->pgd;
501 pgd_val = virt_to_phys(pgd);
502 did = domain_id_iommu(domain, iommu);
503
504 spin_lock(&iommu->lock);
505 pte = intel_pasid_get_entry(dev, pasid);
506 if (!pte) {
507 spin_unlock(&iommu->lock);
508 return -ENODEV;
509 }
510
511 if (pasid_pte_is_present(pte)) {
512 spin_unlock(&iommu->lock);
513 return -EBUSY;
514 }
515
516 pasid_pte_config_second_level(iommu, pte, pgd_val, domain->agaw,
517 did, domain->dirty_tracking);
518 spin_unlock(&iommu->lock);
519
520 pasid_flush_caches(iommu, pte, pasid, did);
521
522 return 0;
523 }
524
intel_pasid_replace_second_level(struct intel_iommu * iommu,struct dmar_domain * domain,struct device * dev,u16 old_did,u32 pasid)525 int intel_pasid_replace_second_level(struct intel_iommu *iommu,
526 struct dmar_domain *domain,
527 struct device *dev, u16 old_did,
528 u32 pasid)
529 {
530 struct pasid_entry *pte, new_pte;
531 struct dma_pte *pgd;
532 u64 pgd_val;
533 u16 did;
534
535 /*
536 * If hardware advertises no support for second level
537 * translation, return directly.
538 */
539 if (!ecap_slts(iommu->ecap)) {
540 pr_err("No second level translation support on %s\n",
541 iommu->name);
542 return -EINVAL;
543 }
544
545 pgd = domain->pgd;
546 pgd_val = virt_to_phys(pgd);
547 did = domain_id_iommu(domain, iommu);
548
549 pasid_pte_config_second_level(iommu, &new_pte, pgd_val,
550 domain->agaw, did,
551 domain->dirty_tracking);
552
553 spin_lock(&iommu->lock);
554 pte = intel_pasid_get_entry(dev, pasid);
555 if (!pte) {
556 spin_unlock(&iommu->lock);
557 return -ENODEV;
558 }
559
560 if (!pasid_pte_is_present(pte)) {
561 spin_unlock(&iommu->lock);
562 return -EINVAL;
563 }
564
565 WARN_ON(old_did != pasid_get_domain_id(pte));
566
567 *pte = new_pte;
568 spin_unlock(&iommu->lock);
569
570 intel_pasid_flush_present(iommu, dev, pasid, old_did, pte);
571 intel_iommu_drain_pasid_prq(dev, pasid);
572
573 return 0;
574 }
575
576 /*
577 * Set up dirty tracking on a second only or nested translation type.
578 */
intel_pasid_setup_dirty_tracking(struct intel_iommu * iommu,struct device * dev,u32 pasid,bool enabled)579 int intel_pasid_setup_dirty_tracking(struct intel_iommu *iommu,
580 struct device *dev, u32 pasid,
581 bool enabled)
582 {
583 struct pasid_entry *pte;
584 u16 did, pgtt;
585
586 spin_lock(&iommu->lock);
587
588 pte = intel_pasid_get_entry(dev, pasid);
589 if (!pte) {
590 spin_unlock(&iommu->lock);
591 dev_err_ratelimited(
592 dev, "Failed to get pasid entry of PASID %d\n", pasid);
593 return -ENODEV;
594 }
595
596 did = pasid_get_domain_id(pte);
597 pgtt = pasid_pte_get_pgtt(pte);
598 if (pgtt != PASID_ENTRY_PGTT_SL_ONLY &&
599 pgtt != PASID_ENTRY_PGTT_NESTED) {
600 spin_unlock(&iommu->lock);
601 dev_err_ratelimited(
602 dev,
603 "Dirty tracking not supported on translation type %d\n",
604 pgtt);
605 return -EOPNOTSUPP;
606 }
607
608 if (pasid_get_ssade(pte) == enabled) {
609 spin_unlock(&iommu->lock);
610 return 0;
611 }
612
613 if (enabled)
614 pasid_set_ssade(pte);
615 else
616 pasid_clear_ssade(pte);
617 spin_unlock(&iommu->lock);
618
619 if (!ecap_coherent(iommu->ecap))
620 clflush_cache_range(pte, sizeof(*pte));
621
622 /*
623 * From VT-d spec table 25 "Guidance to Software for Invalidations":
624 *
625 * - PASID-selective-within-Domain PASID-cache invalidation
626 * If (PGTT=SS or Nested)
627 * - Domain-selective IOTLB invalidation
628 * Else
629 * - PASID-selective PASID-based IOTLB invalidation
630 * - If (pasid is RID_PASID)
631 * - Global Device-TLB invalidation to affected functions
632 * Else
633 * - PASID-based Device-TLB invalidation (with S=1 and
634 * Addr[63:12]=0x7FFFFFFF_FFFFF) to affected functions
635 */
636 pasid_cache_invalidation_with_pasid(iommu, did, pasid);
637
638 iommu->flush.flush_iotlb(iommu, did, 0, 0, DMA_TLB_DSI_FLUSH);
639
640 devtlb_invalidation_with_pasid(iommu, dev, pasid);
641
642 return 0;
643 }
644
645 /*
646 * Set up the scalable mode pasid entry for passthrough translation type.
647 */
pasid_pte_config_pass_through(struct intel_iommu * iommu,struct pasid_entry * pte,u16 did)648 static void pasid_pte_config_pass_through(struct intel_iommu *iommu,
649 struct pasid_entry *pte, u16 did)
650 {
651 lockdep_assert_held(&iommu->lock);
652
653 pasid_clear_entry(pte);
654 pasid_set_domain_id(pte, did);
655 pasid_set_address_width(pte, iommu->agaw);
656 pasid_set_translation_type(pte, PASID_ENTRY_PGTT_PT);
657 pasid_set_fault_enable(pte);
658 pasid_set_page_snoop(pte, !!ecap_smpwc(iommu->ecap));
659 pasid_set_present(pte);
660 }
661
intel_pasid_setup_pass_through(struct intel_iommu * iommu,struct device * dev,u32 pasid)662 int intel_pasid_setup_pass_through(struct intel_iommu *iommu,
663 struct device *dev, u32 pasid)
664 {
665 u16 did = FLPT_DEFAULT_DID;
666 struct pasid_entry *pte;
667
668 spin_lock(&iommu->lock);
669 pte = intel_pasid_get_entry(dev, pasid);
670 if (!pte) {
671 spin_unlock(&iommu->lock);
672 return -ENODEV;
673 }
674
675 if (pasid_pte_is_present(pte)) {
676 spin_unlock(&iommu->lock);
677 return -EBUSY;
678 }
679
680 pasid_pte_config_pass_through(iommu, pte, did);
681 spin_unlock(&iommu->lock);
682
683 pasid_flush_caches(iommu, pte, pasid, did);
684
685 return 0;
686 }
687
intel_pasid_replace_pass_through(struct intel_iommu * iommu,struct device * dev,u16 old_did,u32 pasid)688 int intel_pasid_replace_pass_through(struct intel_iommu *iommu,
689 struct device *dev, u16 old_did,
690 u32 pasid)
691 {
692 struct pasid_entry *pte, new_pte;
693 u16 did = FLPT_DEFAULT_DID;
694
695 pasid_pte_config_pass_through(iommu, &new_pte, did);
696
697 spin_lock(&iommu->lock);
698 pte = intel_pasid_get_entry(dev, pasid);
699 if (!pte) {
700 spin_unlock(&iommu->lock);
701 return -ENODEV;
702 }
703
704 if (!pasid_pte_is_present(pte)) {
705 spin_unlock(&iommu->lock);
706 return -EINVAL;
707 }
708
709 WARN_ON(old_did != pasid_get_domain_id(pte));
710
711 *pte = new_pte;
712 spin_unlock(&iommu->lock);
713
714 intel_pasid_flush_present(iommu, dev, pasid, old_did, pte);
715 intel_iommu_drain_pasid_prq(dev, pasid);
716
717 return 0;
718 }
719
720 /*
721 * Set the page snoop control for a pasid entry which has been set up.
722 */
intel_pasid_setup_page_snoop_control(struct intel_iommu * iommu,struct device * dev,u32 pasid)723 void intel_pasid_setup_page_snoop_control(struct intel_iommu *iommu,
724 struct device *dev, u32 pasid)
725 {
726 struct pasid_entry *pte;
727 u16 did;
728
729 spin_lock(&iommu->lock);
730 pte = intel_pasid_get_entry(dev, pasid);
731 if (WARN_ON(!pte || !pasid_pte_is_present(pte))) {
732 spin_unlock(&iommu->lock);
733 return;
734 }
735
736 pasid_set_pgsnp(pte);
737 did = pasid_get_domain_id(pte);
738 spin_unlock(&iommu->lock);
739
740 intel_pasid_flush_present(iommu, dev, pasid, did, pte);
741 }
742
pasid_pte_config_nestd(struct intel_iommu * iommu,struct pasid_entry * pte,struct iommu_hwpt_vtd_s1 * s1_cfg,struct dmar_domain * s2_domain,u16 did)743 static void pasid_pte_config_nestd(struct intel_iommu *iommu,
744 struct pasid_entry *pte,
745 struct iommu_hwpt_vtd_s1 *s1_cfg,
746 struct dmar_domain *s2_domain,
747 u16 did)
748 {
749 struct dma_pte *pgd = s2_domain->pgd;
750
751 lockdep_assert_held(&iommu->lock);
752
753 pasid_clear_entry(pte);
754
755 if (s1_cfg->addr_width == ADDR_WIDTH_5LEVEL)
756 pasid_set_flpm(pte, 1);
757
758 pasid_set_flptr(pte, s1_cfg->pgtbl_addr);
759
760 if (s1_cfg->flags & IOMMU_VTD_S1_SRE) {
761 pasid_set_sre(pte);
762 if (s1_cfg->flags & IOMMU_VTD_S1_WPE)
763 pasid_set_wpe(pte);
764 }
765
766 if (s1_cfg->flags & IOMMU_VTD_S1_EAFE)
767 pasid_set_eafe(pte);
768
769 if (s2_domain->force_snooping)
770 pasid_set_pgsnp(pte);
771
772 pasid_set_slptr(pte, virt_to_phys(pgd));
773 pasid_set_fault_enable(pte);
774 pasid_set_domain_id(pte, did);
775 pasid_set_address_width(pte, s2_domain->agaw);
776 pasid_set_page_snoop(pte, !!ecap_smpwc(iommu->ecap));
777 if (s2_domain->dirty_tracking)
778 pasid_set_ssade(pte);
779 pasid_set_translation_type(pte, PASID_ENTRY_PGTT_NESTED);
780 pasid_set_present(pte);
781 }
782
783 /**
784 * intel_pasid_setup_nested() - Set up PASID entry for nested translation.
785 * @iommu: IOMMU which the device belong to
786 * @dev: Device to be set up for translation
787 * @pasid: PASID to be programmed in the device PASID table
788 * @domain: User stage-1 domain nested on a stage-2 domain
789 *
790 * This is used for nested translation. The input domain should be
791 * nested type and nested on a parent with 'is_nested_parent' flag
792 * set.
793 */
intel_pasid_setup_nested(struct intel_iommu * iommu,struct device * dev,u32 pasid,struct dmar_domain * domain)794 int intel_pasid_setup_nested(struct intel_iommu *iommu, struct device *dev,
795 u32 pasid, struct dmar_domain *domain)
796 {
797 struct iommu_hwpt_vtd_s1 *s1_cfg = &domain->s1_cfg;
798 struct dmar_domain *s2_domain = domain->s2_domain;
799 u16 did = domain_id_iommu(domain, iommu);
800 struct pasid_entry *pte;
801
802 /* Address width should match the address width supported by hardware */
803 switch (s1_cfg->addr_width) {
804 case ADDR_WIDTH_4LEVEL:
805 break;
806 case ADDR_WIDTH_5LEVEL:
807 if (!cap_fl5lp_support(iommu->cap)) {
808 dev_err_ratelimited(dev,
809 "5-level paging not supported\n");
810 return -EINVAL;
811 }
812 break;
813 default:
814 dev_err_ratelimited(dev, "Invalid stage-1 address width %d\n",
815 s1_cfg->addr_width);
816 return -EINVAL;
817 }
818
819 if ((s1_cfg->flags & IOMMU_VTD_S1_SRE) && !ecap_srs(iommu->ecap)) {
820 pr_err_ratelimited("No supervisor request support on %s\n",
821 iommu->name);
822 return -EINVAL;
823 }
824
825 if ((s1_cfg->flags & IOMMU_VTD_S1_EAFE) && !ecap_eafs(iommu->ecap)) {
826 pr_err_ratelimited("No extended access flag support on %s\n",
827 iommu->name);
828 return -EINVAL;
829 }
830
831 spin_lock(&iommu->lock);
832 pte = intel_pasid_get_entry(dev, pasid);
833 if (!pte) {
834 spin_unlock(&iommu->lock);
835 return -ENODEV;
836 }
837 if (pasid_pte_is_present(pte)) {
838 spin_unlock(&iommu->lock);
839 return -EBUSY;
840 }
841
842 pasid_pte_config_nestd(iommu, pte, s1_cfg, s2_domain, did);
843 spin_unlock(&iommu->lock);
844
845 pasid_flush_caches(iommu, pte, pasid, did);
846
847 return 0;
848 }
849
intel_pasid_replace_nested(struct intel_iommu * iommu,struct device * dev,u32 pasid,u16 old_did,struct dmar_domain * domain)850 int intel_pasid_replace_nested(struct intel_iommu *iommu,
851 struct device *dev, u32 pasid,
852 u16 old_did, struct dmar_domain *domain)
853 {
854 struct iommu_hwpt_vtd_s1 *s1_cfg = &domain->s1_cfg;
855 struct dmar_domain *s2_domain = domain->s2_domain;
856 u16 did = domain_id_iommu(domain, iommu);
857 struct pasid_entry *pte, new_pte;
858
859 /* Address width should match the address width supported by hardware */
860 switch (s1_cfg->addr_width) {
861 case ADDR_WIDTH_4LEVEL:
862 break;
863 case ADDR_WIDTH_5LEVEL:
864 if (!cap_fl5lp_support(iommu->cap)) {
865 dev_err_ratelimited(dev,
866 "5-level paging not supported\n");
867 return -EINVAL;
868 }
869 break;
870 default:
871 dev_err_ratelimited(dev, "Invalid stage-1 address width %d\n",
872 s1_cfg->addr_width);
873 return -EINVAL;
874 }
875
876 if ((s1_cfg->flags & IOMMU_VTD_S1_SRE) && !ecap_srs(iommu->ecap)) {
877 pr_err_ratelimited("No supervisor request support on %s\n",
878 iommu->name);
879 return -EINVAL;
880 }
881
882 if ((s1_cfg->flags & IOMMU_VTD_S1_EAFE) && !ecap_eafs(iommu->ecap)) {
883 pr_err_ratelimited("No extended access flag support on %s\n",
884 iommu->name);
885 return -EINVAL;
886 }
887
888 pasid_pte_config_nestd(iommu, &new_pte, s1_cfg, s2_domain, did);
889
890 spin_lock(&iommu->lock);
891 pte = intel_pasid_get_entry(dev, pasid);
892 if (!pte) {
893 spin_unlock(&iommu->lock);
894 return -ENODEV;
895 }
896
897 if (!pasid_pte_is_present(pte)) {
898 spin_unlock(&iommu->lock);
899 return -EINVAL;
900 }
901
902 WARN_ON(old_did != pasid_get_domain_id(pte));
903
904 *pte = new_pte;
905 spin_unlock(&iommu->lock);
906
907 intel_pasid_flush_present(iommu, dev, pasid, old_did, pte);
908 intel_iommu_drain_pasid_prq(dev, pasid);
909
910 return 0;
911 }
912
913 /*
914 * Interfaces to setup or teardown a pasid table to the scalable-mode
915 * context table entry:
916 */
917
device_pasid_table_teardown(struct device * dev,u8 bus,u8 devfn)918 static void device_pasid_table_teardown(struct device *dev, u8 bus, u8 devfn)
919 {
920 struct device_domain_info *info = dev_iommu_priv_get(dev);
921 struct intel_iommu *iommu = info->iommu;
922 struct context_entry *context;
923 u16 did;
924
925 spin_lock(&iommu->lock);
926 context = iommu_context_addr(iommu, bus, devfn, false);
927 if (!context) {
928 spin_unlock(&iommu->lock);
929 return;
930 }
931
932 did = context_domain_id(context);
933 context_clear_entry(context);
934 __iommu_flush_cache(iommu, context, sizeof(*context));
935 spin_unlock(&iommu->lock);
936 intel_context_flush_no_pasid(info, context, did);
937 }
938
pci_pasid_table_teardown(struct pci_dev * pdev,u16 alias,void * data)939 static int pci_pasid_table_teardown(struct pci_dev *pdev, u16 alias, void *data)
940 {
941 struct device *dev = data;
942
943 if (dev == &pdev->dev)
944 device_pasid_table_teardown(dev, PCI_BUS_NUM(alias), alias & 0xff);
945
946 return 0;
947 }
948
intel_pasid_teardown_sm_context(struct device * dev)949 void intel_pasid_teardown_sm_context(struct device *dev)
950 {
951 struct device_domain_info *info = dev_iommu_priv_get(dev);
952
953 if (!dev_is_pci(dev)) {
954 device_pasid_table_teardown(dev, info->bus, info->devfn);
955 return;
956 }
957
958 pci_for_each_dma_alias(to_pci_dev(dev), pci_pasid_table_teardown, dev);
959 }
960
961 /*
962 * Get the PASID directory size for scalable mode context entry.
963 * Value of X in the PDTS field of a scalable mode context entry
964 * indicates PASID directory with 2^(X + 7) entries.
965 */
context_get_sm_pds(struct pasid_table * table)966 static unsigned long context_get_sm_pds(struct pasid_table *table)
967 {
968 unsigned long pds, max_pde;
969
970 max_pde = table->max_pasid >> PASID_PDE_SHIFT;
971 pds = find_first_bit(&max_pde, MAX_NR_PASID_BITS);
972 if (pds < 7)
973 return 0;
974
975 return pds - 7;
976 }
977
context_entry_set_pasid_table(struct context_entry * context,struct device * dev)978 static int context_entry_set_pasid_table(struct context_entry *context,
979 struct device *dev)
980 {
981 struct device_domain_info *info = dev_iommu_priv_get(dev);
982 struct pasid_table *table = info->pasid_table;
983 struct intel_iommu *iommu = info->iommu;
984 unsigned long pds;
985
986 context_clear_entry(context);
987
988 pds = context_get_sm_pds(table);
989 context->lo = (u64)virt_to_phys(table->table) | context_pdts(pds);
990 context_set_sm_rid2pasid(context, IOMMU_NO_PASID);
991
992 if (info->ats_supported)
993 context_set_sm_dte(context);
994 if (info->pasid_supported)
995 context_set_pasid(context);
996 if (info->pri_supported)
997 context_set_sm_pre(context);
998
999 context_set_fault_enable(context);
1000 context_set_present(context);
1001 __iommu_flush_cache(iommu, context, sizeof(*context));
1002
1003 return 0;
1004 }
1005
device_pasid_table_setup(struct device * dev,u8 bus,u8 devfn)1006 static int device_pasid_table_setup(struct device *dev, u8 bus, u8 devfn)
1007 {
1008 struct device_domain_info *info = dev_iommu_priv_get(dev);
1009 struct intel_iommu *iommu = info->iommu;
1010 struct context_entry *context;
1011
1012 spin_lock(&iommu->lock);
1013 context = iommu_context_addr(iommu, bus, devfn, true);
1014 if (!context) {
1015 spin_unlock(&iommu->lock);
1016 return -ENOMEM;
1017 }
1018
1019 if (context_present(context) && !context_copied(iommu, bus, devfn)) {
1020 spin_unlock(&iommu->lock);
1021 return 0;
1022 }
1023
1024 if (context_copied(iommu, bus, devfn)) {
1025 context_clear_entry(context);
1026 __iommu_flush_cache(iommu, context, sizeof(*context));
1027
1028 /*
1029 * For kdump cases, old valid entries may be cached due to
1030 * the in-flight DMA and copied pgtable, but there is no
1031 * unmapping behaviour for them, thus we need explicit cache
1032 * flushes for all affected domain IDs and PASIDs used in
1033 * the copied PASID table. Given that we have no idea about
1034 * which domain IDs and PASIDs were used in the copied tables,
1035 * upgrade them to global PASID and IOTLB cache invalidation.
1036 */
1037 iommu->flush.flush_context(iommu, 0,
1038 PCI_DEVID(bus, devfn),
1039 DMA_CCMD_MASK_NOBIT,
1040 DMA_CCMD_DEVICE_INVL);
1041 qi_flush_pasid_cache(iommu, 0, QI_PC_GLOBAL, 0);
1042 iommu->flush.flush_iotlb(iommu, 0, 0, 0, DMA_TLB_GLOBAL_FLUSH);
1043 devtlb_invalidation_with_pasid(iommu, dev, IOMMU_NO_PASID);
1044
1045 /*
1046 * At this point, the device is supposed to finish reset at
1047 * its driver probe stage, so no in-flight DMA will exist,
1048 * and we don't need to worry anymore hereafter.
1049 */
1050 clear_context_copied(iommu, bus, devfn);
1051 }
1052
1053 context_entry_set_pasid_table(context, dev);
1054 spin_unlock(&iommu->lock);
1055
1056 /*
1057 * It's a non-present to present mapping. If hardware doesn't cache
1058 * non-present entry we don't need to flush the caches. If it does
1059 * cache non-present entries, then it does so in the special
1060 * domain #0, which we have to flush:
1061 */
1062 if (cap_caching_mode(iommu->cap)) {
1063 iommu->flush.flush_context(iommu, 0,
1064 PCI_DEVID(bus, devfn),
1065 DMA_CCMD_MASK_NOBIT,
1066 DMA_CCMD_DEVICE_INVL);
1067 iommu->flush.flush_iotlb(iommu, 0, 0, 0, DMA_TLB_DSI_FLUSH);
1068 }
1069
1070 return 0;
1071 }
1072
pci_pasid_table_setup(struct pci_dev * pdev,u16 alias,void * data)1073 static int pci_pasid_table_setup(struct pci_dev *pdev, u16 alias, void *data)
1074 {
1075 struct device *dev = data;
1076
1077 if (dev != &pdev->dev)
1078 return 0;
1079
1080 return device_pasid_table_setup(dev, PCI_BUS_NUM(alias), alias & 0xff);
1081 }
1082
1083 /*
1084 * Set the device's PASID table to its context table entry.
1085 *
1086 * The PASID table is set to the context entries of both device itself
1087 * and its alias requester ID for DMA.
1088 */
intel_pasid_setup_sm_context(struct device * dev)1089 int intel_pasid_setup_sm_context(struct device *dev)
1090 {
1091 struct device_domain_info *info = dev_iommu_priv_get(dev);
1092
1093 if (!dev_is_pci(dev))
1094 return device_pasid_table_setup(dev, info->bus, info->devfn);
1095
1096 return pci_for_each_dma_alias(to_pci_dev(dev), pci_pasid_table_setup, dev);
1097 }
1098
1099 /*
1100 * Global Device-TLB invalidation following changes in a context entry which
1101 * was present.
1102 */
__context_flush_dev_iotlb(struct device_domain_info * info)1103 static void __context_flush_dev_iotlb(struct device_domain_info *info)
1104 {
1105 if (!info->ats_enabled)
1106 return;
1107
1108 qi_flush_dev_iotlb(info->iommu, PCI_DEVID(info->bus, info->devfn),
1109 info->pfsid, info->ats_qdep, 0, MAX_AGAW_PFN_WIDTH);
1110
1111 /*
1112 * There is no guarantee that the device DMA is stopped when it reaches
1113 * here. Therefore, always attempt the extra device TLB invalidation
1114 * quirk. The impact on performance is acceptable since this is not a
1115 * performance-critical path.
1116 */
1117 quirk_extra_dev_tlb_flush(info, 0, MAX_AGAW_PFN_WIDTH, IOMMU_NO_PASID,
1118 info->ats_qdep);
1119 }
1120
1121 /*
1122 * Cache invalidations after change in a context table entry that was present
1123 * according to the Spec 6.5.3.3 (Guidance to Software for Invalidations).
1124 * This helper can only be used when IOMMU is working in the legacy mode or
1125 * IOMMU is in scalable mode but all PASID table entries of the device are
1126 * non-present.
1127 */
intel_context_flush_no_pasid(struct device_domain_info * info,struct context_entry * context,u16 did)1128 void intel_context_flush_no_pasid(struct device_domain_info *info,
1129 struct context_entry *context, u16 did)
1130 {
1131 struct intel_iommu *iommu = info->iommu;
1132
1133 /*
1134 * Device-selective context-cache invalidation. The Domain-ID field
1135 * of the Context-cache Invalidate Descriptor is ignored by hardware
1136 * when operating in scalable mode. Therefore the @did value doesn't
1137 * matter in scalable mode.
1138 */
1139 iommu->flush.flush_context(iommu, did, PCI_DEVID(info->bus, info->devfn),
1140 DMA_CCMD_MASK_NOBIT, DMA_CCMD_DEVICE_INVL);
1141
1142 /*
1143 * For legacy mode:
1144 * - Domain-selective IOTLB invalidation
1145 * - Global Device-TLB invalidation to all affected functions
1146 */
1147 if (!sm_supported(iommu)) {
1148 iommu->flush.flush_iotlb(iommu, did, 0, 0, DMA_TLB_DSI_FLUSH);
1149 __context_flush_dev_iotlb(info);
1150
1151 return;
1152 }
1153
1154 __context_flush_dev_iotlb(info);
1155 }
1156