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
24 /*
25 * Intel IOMMU system wide PASID name space:
26 */
27 u32 intel_pasid_max_id = PASID_MAX;
28
vcmd_alloc_pasid(struct intel_iommu * iommu,u32 * pasid)29 int vcmd_alloc_pasid(struct intel_iommu *iommu, u32 *pasid)
30 {
31 unsigned long flags;
32 u8 status_code;
33 int ret = 0;
34 u64 res;
35
36 raw_spin_lock_irqsave(&iommu->register_lock, flags);
37 dmar_writeq(iommu->reg + DMAR_VCMD_REG, VCMD_CMD_ALLOC);
38 IOMMU_WAIT_OP(iommu, DMAR_VCRSP_REG, dmar_readq,
39 !(res & VCMD_VRSP_IP), res);
40 raw_spin_unlock_irqrestore(&iommu->register_lock, flags);
41
42 status_code = VCMD_VRSP_SC(res);
43 switch (status_code) {
44 case VCMD_VRSP_SC_SUCCESS:
45 *pasid = VCMD_VRSP_RESULT_PASID(res);
46 break;
47 case VCMD_VRSP_SC_NO_PASID_AVAIL:
48 pr_info("IOMMU: %s: No PASID available\n", iommu->name);
49 ret = -ENOSPC;
50 break;
51 default:
52 ret = -ENODEV;
53 pr_warn("IOMMU: %s: Unexpected error code %d\n",
54 iommu->name, status_code);
55 }
56
57 return ret;
58 }
59
vcmd_free_pasid(struct intel_iommu * iommu,u32 pasid)60 void vcmd_free_pasid(struct intel_iommu *iommu, u32 pasid)
61 {
62 unsigned long flags;
63 u8 status_code;
64 u64 res;
65
66 raw_spin_lock_irqsave(&iommu->register_lock, flags);
67 dmar_writeq(iommu->reg + DMAR_VCMD_REG,
68 VCMD_CMD_OPERAND(pasid) | VCMD_CMD_FREE);
69 IOMMU_WAIT_OP(iommu, DMAR_VCRSP_REG, dmar_readq,
70 !(res & VCMD_VRSP_IP), res);
71 raw_spin_unlock_irqrestore(&iommu->register_lock, flags);
72
73 status_code = VCMD_VRSP_SC(res);
74 switch (status_code) {
75 case VCMD_VRSP_SC_SUCCESS:
76 break;
77 case VCMD_VRSP_SC_INVALID_PASID:
78 pr_info("IOMMU: %s: Invalid PASID\n", iommu->name);
79 break;
80 default:
81 pr_warn("IOMMU: %s: Unexpected error code %d\n",
82 iommu->name, status_code);
83 }
84 }
85
86 /*
87 * Per device pasid table management:
88 */
89
90 /*
91 * Allocate a pasid table for @dev. It should be called in a
92 * single-thread context.
93 */
intel_pasid_alloc_table(struct device * dev)94 int intel_pasid_alloc_table(struct device *dev)
95 {
96 struct device_domain_info *info;
97 struct pasid_table *pasid_table;
98 struct page *pages;
99 u32 max_pasid = 0;
100 int order, size;
101
102 might_sleep();
103 info = dev_iommu_priv_get(dev);
104 if (WARN_ON(!info || !dev_is_pci(dev) || info->pasid_table))
105 return -EINVAL;
106
107 pasid_table = kzalloc(sizeof(*pasid_table), GFP_KERNEL);
108 if (!pasid_table)
109 return -ENOMEM;
110
111 if (info->pasid_supported)
112 max_pasid = min_t(u32, pci_max_pasids(to_pci_dev(dev)),
113 intel_pasid_max_id);
114
115 size = max_pasid >> (PASID_PDE_SHIFT - 3);
116 order = size ? get_order(size) : 0;
117 pages = alloc_pages_node(info->iommu->node,
118 GFP_KERNEL | __GFP_ZERO, order);
119 if (!pages) {
120 kfree(pasid_table);
121 return -ENOMEM;
122 }
123
124 pasid_table->table = page_address(pages);
125 pasid_table->order = order;
126 pasid_table->max_pasid = 1 << (order + PAGE_SHIFT + 3);
127 info->pasid_table = pasid_table;
128
129 if (!ecap_coherent(info->iommu->ecap))
130 clflush_cache_range(pasid_table->table, (1 << order) * PAGE_SIZE);
131
132 return 0;
133 }
134
intel_pasid_free_table(struct device * dev)135 void intel_pasid_free_table(struct device *dev)
136 {
137 struct device_domain_info *info;
138 struct pasid_table *pasid_table;
139 struct pasid_dir_entry *dir;
140 struct pasid_entry *table;
141 int i, max_pde;
142
143 info = dev_iommu_priv_get(dev);
144 if (!info || !dev_is_pci(dev) || !info->pasid_table)
145 return;
146
147 pasid_table = info->pasid_table;
148 info->pasid_table = NULL;
149
150 /* Free scalable mode PASID directory tables: */
151 dir = pasid_table->table;
152 max_pde = pasid_table->max_pasid >> PASID_PDE_SHIFT;
153 for (i = 0; i < max_pde; i++) {
154 table = get_pasid_table_from_pde(&dir[i]);
155 free_pgtable_page(table);
156 }
157
158 free_pages((unsigned long)pasid_table->table, pasid_table->order);
159 kfree(pasid_table);
160 }
161
intel_pasid_get_table(struct device * dev)162 struct pasid_table *intel_pasid_get_table(struct device *dev)
163 {
164 struct device_domain_info *info;
165
166 info = dev_iommu_priv_get(dev);
167 if (!info)
168 return NULL;
169
170 return info->pasid_table;
171 }
172
intel_pasid_get_dev_max_id(struct device * dev)173 static int intel_pasid_get_dev_max_id(struct device *dev)
174 {
175 struct device_domain_info *info;
176
177 info = dev_iommu_priv_get(dev);
178 if (!info || !info->pasid_table)
179 return 0;
180
181 return info->pasid_table->max_pasid;
182 }
183
intel_pasid_get_entry(struct device * dev,u32 pasid)184 static struct pasid_entry *intel_pasid_get_entry(struct device *dev, u32 pasid)
185 {
186 struct device_domain_info *info;
187 struct pasid_table *pasid_table;
188 struct pasid_dir_entry *dir;
189 struct pasid_entry *entries;
190 int dir_index, index;
191
192 pasid_table = intel_pasid_get_table(dev);
193 if (WARN_ON(!pasid_table || pasid >= intel_pasid_get_dev_max_id(dev)))
194 return NULL;
195
196 dir = pasid_table->table;
197 info = dev_iommu_priv_get(dev);
198 dir_index = pasid >> PASID_PDE_SHIFT;
199 index = pasid & PASID_PTE_MASK;
200
201 retry:
202 entries = get_pasid_table_from_pde(&dir[dir_index]);
203 if (!entries) {
204 entries = alloc_pgtable_page(info->iommu->node);
205 if (!entries)
206 return NULL;
207
208 /*
209 * The pasid directory table entry won't be freed after
210 * allocation. No worry about the race with free and
211 * clear. However, this entry might be populated by others
212 * while we are preparing it. Use theirs with a retry.
213 */
214 if (cmpxchg64(&dir[dir_index].val, 0ULL,
215 (u64)virt_to_phys(entries) | PASID_PTE_PRESENT)) {
216 free_pgtable_page(entries);
217 goto retry;
218 }
219 if (!ecap_coherent(info->iommu->ecap)) {
220 clflush_cache_range(entries, VTD_PAGE_SIZE);
221 clflush_cache_range(&dir[dir_index].val, sizeof(*dir));
222 }
223 }
224
225 return &entries[index];
226 }
227
228 /*
229 * Interfaces for PASID table entry manipulation:
230 */
pasid_clear_entry(struct pasid_entry * pe)231 static inline void pasid_clear_entry(struct pasid_entry *pe)
232 {
233 WRITE_ONCE(pe->val[0], 0);
234 WRITE_ONCE(pe->val[1], 0);
235 WRITE_ONCE(pe->val[2], 0);
236 WRITE_ONCE(pe->val[3], 0);
237 WRITE_ONCE(pe->val[4], 0);
238 WRITE_ONCE(pe->val[5], 0);
239 WRITE_ONCE(pe->val[6], 0);
240 WRITE_ONCE(pe->val[7], 0);
241 }
242
pasid_clear_entry_with_fpd(struct pasid_entry * pe)243 static inline void pasid_clear_entry_with_fpd(struct pasid_entry *pe)
244 {
245 WRITE_ONCE(pe->val[0], PASID_PTE_FPD);
246 WRITE_ONCE(pe->val[1], 0);
247 WRITE_ONCE(pe->val[2], 0);
248 WRITE_ONCE(pe->val[3], 0);
249 WRITE_ONCE(pe->val[4], 0);
250 WRITE_ONCE(pe->val[5], 0);
251 WRITE_ONCE(pe->val[6], 0);
252 WRITE_ONCE(pe->val[7], 0);
253 }
254
255 static void
intel_pasid_clear_entry(struct device * dev,u32 pasid,bool fault_ignore)256 intel_pasid_clear_entry(struct device *dev, u32 pasid, bool fault_ignore)
257 {
258 struct pasid_entry *pe;
259
260 pe = intel_pasid_get_entry(dev, pasid);
261 if (WARN_ON(!pe))
262 return;
263
264 if (fault_ignore && pasid_pte_is_present(pe))
265 pasid_clear_entry_with_fpd(pe);
266 else
267 pasid_clear_entry(pe);
268 }
269
pasid_set_bits(u64 * ptr,u64 mask,u64 bits)270 static inline void pasid_set_bits(u64 *ptr, u64 mask, u64 bits)
271 {
272 u64 old;
273
274 old = READ_ONCE(*ptr);
275 WRITE_ONCE(*ptr, (old & ~mask) | bits);
276 }
277
278 /*
279 * Setup the DID(Domain Identifier) field (Bit 64~79) of scalable mode
280 * PASID entry.
281 */
282 static inline void
pasid_set_domain_id(struct pasid_entry * pe,u64 value)283 pasid_set_domain_id(struct pasid_entry *pe, u64 value)
284 {
285 pasid_set_bits(&pe->val[1], GENMASK_ULL(15, 0), value);
286 }
287
288 /*
289 * Get domain ID value of a scalable mode PASID entry.
290 */
291 static inline u16
pasid_get_domain_id(struct pasid_entry * pe)292 pasid_get_domain_id(struct pasid_entry *pe)
293 {
294 return (u16)(READ_ONCE(pe->val[1]) & GENMASK_ULL(15, 0));
295 }
296
297 /*
298 * Setup the SLPTPTR(Second Level Page Table Pointer) field (Bit 12~63)
299 * of a scalable mode PASID entry.
300 */
301 static inline void
pasid_set_slptr(struct pasid_entry * pe,u64 value)302 pasid_set_slptr(struct pasid_entry *pe, u64 value)
303 {
304 pasid_set_bits(&pe->val[0], VTD_PAGE_MASK, value);
305 }
306
307 /*
308 * Setup the AW(Address Width) field (Bit 2~4) of a scalable mode PASID
309 * entry.
310 */
311 static inline void
pasid_set_address_width(struct pasid_entry * pe,u64 value)312 pasid_set_address_width(struct pasid_entry *pe, u64 value)
313 {
314 pasid_set_bits(&pe->val[0], GENMASK_ULL(4, 2), value << 2);
315 }
316
317 /*
318 * Setup the PGTT(PASID Granular Translation Type) field (Bit 6~8)
319 * of a scalable mode PASID entry.
320 */
321 static inline void
pasid_set_translation_type(struct pasid_entry * pe,u64 value)322 pasid_set_translation_type(struct pasid_entry *pe, u64 value)
323 {
324 pasid_set_bits(&pe->val[0], GENMASK_ULL(8, 6), value << 6);
325 }
326
327 /*
328 * Enable fault processing by clearing the FPD(Fault Processing
329 * Disable) field (Bit 1) of a scalable mode PASID entry.
330 */
pasid_set_fault_enable(struct pasid_entry * pe)331 static inline void pasid_set_fault_enable(struct pasid_entry *pe)
332 {
333 pasid_set_bits(&pe->val[0], 1 << 1, 0);
334 }
335
336 /*
337 * Setup the SRE(Supervisor Request Enable) field (Bit 128) of a
338 * scalable mode PASID entry.
339 */
pasid_set_sre(struct pasid_entry * pe)340 static inline void pasid_set_sre(struct pasid_entry *pe)
341 {
342 pasid_set_bits(&pe->val[2], 1 << 0, 1);
343 }
344
345 /*
346 * Setup the WPE(Write Protect Enable) field (Bit 132) of a
347 * scalable mode PASID entry.
348 */
pasid_set_wpe(struct pasid_entry * pe)349 static inline void pasid_set_wpe(struct pasid_entry *pe)
350 {
351 pasid_set_bits(&pe->val[2], 1 << 4, 1 << 4);
352 }
353
354 /*
355 * Setup the P(Present) field (Bit 0) of a scalable mode PASID
356 * entry.
357 */
pasid_set_present(struct pasid_entry * pe)358 static inline void pasid_set_present(struct pasid_entry *pe)
359 {
360 pasid_set_bits(&pe->val[0], 1 << 0, 1);
361 }
362
363 /*
364 * Setup Page Walk Snoop bit (Bit 87) of a scalable mode PASID
365 * entry.
366 */
pasid_set_page_snoop(struct pasid_entry * pe,bool value)367 static inline void pasid_set_page_snoop(struct pasid_entry *pe, bool value)
368 {
369 pasid_set_bits(&pe->val[1], 1 << 23, value << 23);
370 }
371
372 /*
373 * Setup No Execute Enable bit (Bit 133) of a scalable mode PASID
374 * entry. It is required when XD bit of the first level page table
375 * entry is about to be set.
376 */
pasid_set_nxe(struct pasid_entry * pe)377 static inline void pasid_set_nxe(struct pasid_entry *pe)
378 {
379 pasid_set_bits(&pe->val[2], 1 << 5, 1 << 5);
380 }
381
382 /*
383 * Setup the Page Snoop (PGSNP) field (Bit 88) of a scalable mode
384 * PASID entry.
385 */
386 static inline void
pasid_set_pgsnp(struct pasid_entry * pe)387 pasid_set_pgsnp(struct pasid_entry *pe)
388 {
389 pasid_set_bits(&pe->val[1], 1ULL << 24, 1ULL << 24);
390 }
391
392 /*
393 * Setup the First Level Page table Pointer field (Bit 140~191)
394 * of a scalable mode PASID entry.
395 */
396 static inline void
pasid_set_flptr(struct pasid_entry * pe,u64 value)397 pasid_set_flptr(struct pasid_entry *pe, u64 value)
398 {
399 pasid_set_bits(&pe->val[2], VTD_PAGE_MASK, value);
400 }
401
402 /*
403 * Setup the First Level Paging Mode field (Bit 130~131) of a
404 * scalable mode PASID entry.
405 */
406 static inline void
pasid_set_flpm(struct pasid_entry * pe,u64 value)407 pasid_set_flpm(struct pasid_entry *pe, u64 value)
408 {
409 pasid_set_bits(&pe->val[2], GENMASK_ULL(3, 2), value << 2);
410 }
411
412 static void
pasid_cache_invalidation_with_pasid(struct intel_iommu * iommu,u16 did,u32 pasid)413 pasid_cache_invalidation_with_pasid(struct intel_iommu *iommu,
414 u16 did, u32 pasid)
415 {
416 struct qi_desc desc;
417
418 desc.qw0 = QI_PC_DID(did) | QI_PC_GRAN(QI_PC_PASID_SEL) |
419 QI_PC_PASID(pasid) | QI_PC_TYPE;
420 desc.qw1 = 0;
421 desc.qw2 = 0;
422 desc.qw3 = 0;
423
424 qi_submit_sync(iommu, &desc, 1, 0);
425 }
426
427 static void
devtlb_invalidation_with_pasid(struct intel_iommu * iommu,struct device * dev,u32 pasid)428 devtlb_invalidation_with_pasid(struct intel_iommu *iommu,
429 struct device *dev, u32 pasid)
430 {
431 struct device_domain_info *info;
432 u16 sid, qdep, pfsid;
433
434 info = dev_iommu_priv_get(dev);
435 if (!info || !info->ats_enabled)
436 return;
437
438 sid = info->bus << 8 | info->devfn;
439 qdep = info->ats_qdep;
440 pfsid = info->pfsid;
441
442 /*
443 * When PASID 0 is used, it indicates RID2PASID(DMA request w/o PASID),
444 * devTLB flush w/o PASID should be used. For non-zero PASID under
445 * SVA usage, device could do DMA with multiple PASIDs. It is more
446 * efficient to flush devTLB specific to the PASID.
447 */
448 if (pasid == PASID_RID2PASID)
449 qi_flush_dev_iotlb(iommu, sid, pfsid, qdep, 0, 64 - VTD_PAGE_SHIFT);
450 else
451 qi_flush_dev_iotlb_pasid(iommu, sid, pfsid, pasid, qdep, 0, 64 - VTD_PAGE_SHIFT);
452 }
453
intel_pasid_tear_down_entry(struct intel_iommu * iommu,struct device * dev,u32 pasid,bool fault_ignore)454 void intel_pasid_tear_down_entry(struct intel_iommu *iommu, struct device *dev,
455 u32 pasid, bool fault_ignore)
456 {
457 struct pasid_entry *pte;
458 u16 did, pgtt;
459
460 spin_lock(&iommu->lock);
461 pte = intel_pasid_get_entry(dev, pasid);
462 if (WARN_ON(!pte) || !pasid_pte_is_present(pte)) {
463 spin_unlock(&iommu->lock);
464 return;
465 }
466
467 did = pasid_get_domain_id(pte);
468 pgtt = pasid_pte_get_pgtt(pte);
469 intel_pasid_clear_entry(dev, pasid, fault_ignore);
470 spin_unlock(&iommu->lock);
471
472 if (!ecap_coherent(iommu->ecap))
473 clflush_cache_range(pte, sizeof(*pte));
474
475 pasid_cache_invalidation_with_pasid(iommu, did, pasid);
476
477 if (pgtt == PASID_ENTRY_PGTT_PT || pgtt == PASID_ENTRY_PGTT_FL_ONLY)
478 qi_flush_piotlb(iommu, did, pasid, 0, -1, 0);
479 else
480 iommu->flush.flush_iotlb(iommu, did, 0, 0, DMA_TLB_DSI_FLUSH);
481
482 /* Device IOTLB doesn't need to be flushed in caching mode. */
483 if (!cap_caching_mode(iommu->cap))
484 devtlb_invalidation_with_pasid(iommu, dev, pasid);
485 }
486
487 /*
488 * This function flushes cache for a newly setup pasid table entry.
489 * Caller of it should not modify the in-use pasid table entries.
490 */
pasid_flush_caches(struct intel_iommu * iommu,struct pasid_entry * pte,u32 pasid,u16 did)491 static void pasid_flush_caches(struct intel_iommu *iommu,
492 struct pasid_entry *pte,
493 u32 pasid, u16 did)
494 {
495 if (!ecap_coherent(iommu->ecap))
496 clflush_cache_range(pte, sizeof(*pte));
497
498 if (cap_caching_mode(iommu->cap)) {
499 pasid_cache_invalidation_with_pasid(iommu, did, pasid);
500 qi_flush_piotlb(iommu, did, pasid, 0, -1, 0);
501 } else {
502 iommu_flush_write_buffer(iommu);
503 }
504 }
505
506 /*
507 * Set up the scalable mode pasid table entry for first only
508 * translation type.
509 */
intel_pasid_setup_first_level(struct intel_iommu * iommu,struct device * dev,pgd_t * pgd,u32 pasid,u16 did,int flags)510 int intel_pasid_setup_first_level(struct intel_iommu *iommu,
511 struct device *dev, pgd_t *pgd,
512 u32 pasid, u16 did, int flags)
513 {
514 struct pasid_entry *pte;
515
516 if (!ecap_flts(iommu->ecap)) {
517 pr_err("No first level translation support on %s\n",
518 iommu->name);
519 return -EINVAL;
520 }
521
522 if (flags & PASID_FLAG_SUPERVISOR_MODE) {
523 #ifdef CONFIG_X86
524 unsigned long cr0 = read_cr0();
525
526 /* CR0.WP is normally set but just to be sure */
527 if (unlikely(!(cr0 & X86_CR0_WP))) {
528 pr_err("No CPU write protect!\n");
529 return -EINVAL;
530 }
531 #endif
532 if (!ecap_srs(iommu->ecap)) {
533 pr_err("No supervisor request support on %s\n",
534 iommu->name);
535 return -EINVAL;
536 }
537 }
538
539 if ((flags & PASID_FLAG_FL5LP) && !cap_fl5lp_support(iommu->cap)) {
540 pr_err("No 5-level paging support for first-level on %s\n",
541 iommu->name);
542 return -EINVAL;
543 }
544
545 spin_lock(&iommu->lock);
546 pte = intel_pasid_get_entry(dev, pasid);
547 if (!pte) {
548 spin_unlock(&iommu->lock);
549 return -ENODEV;
550 }
551
552 if (pasid_pte_is_present(pte)) {
553 spin_unlock(&iommu->lock);
554 return -EBUSY;
555 }
556
557 pasid_clear_entry(pte);
558
559 /* Setup the first level page table pointer: */
560 pasid_set_flptr(pte, (u64)__pa(pgd));
561 if (flags & PASID_FLAG_SUPERVISOR_MODE) {
562 pasid_set_sre(pte);
563 pasid_set_wpe(pte);
564 }
565
566 if (flags & PASID_FLAG_FL5LP)
567 pasid_set_flpm(pte, 1);
568
569 if (flags & PASID_FLAG_PAGE_SNOOP)
570 pasid_set_pgsnp(pte);
571
572 pasid_set_domain_id(pte, did);
573 pasid_set_address_width(pte, iommu->agaw);
574 pasid_set_page_snoop(pte, !!ecap_smpwc(iommu->ecap));
575 pasid_set_nxe(pte);
576
577 /* Setup Present and PASID Granular Transfer Type: */
578 pasid_set_translation_type(pte, PASID_ENTRY_PGTT_FL_ONLY);
579 pasid_set_present(pte);
580 spin_unlock(&iommu->lock);
581
582 pasid_flush_caches(iommu, pte, pasid, did);
583
584 return 0;
585 }
586
587 /*
588 * Skip top levels of page tables for iommu which has less agaw
589 * than default. Unnecessary for PT mode.
590 */
iommu_skip_agaw(struct dmar_domain * domain,struct intel_iommu * iommu,struct dma_pte ** pgd)591 static inline int iommu_skip_agaw(struct dmar_domain *domain,
592 struct intel_iommu *iommu,
593 struct dma_pte **pgd)
594 {
595 int agaw;
596
597 for (agaw = domain->agaw; agaw > iommu->agaw; agaw--) {
598 *pgd = phys_to_virt(dma_pte_addr(*pgd));
599 if (!dma_pte_present(*pgd))
600 return -EINVAL;
601 }
602
603 return agaw;
604 }
605
606 /*
607 * Set up the scalable mode pasid entry for second only translation type.
608 */
intel_pasid_setup_second_level(struct intel_iommu * iommu,struct dmar_domain * domain,struct device * dev,u32 pasid)609 int intel_pasid_setup_second_level(struct intel_iommu *iommu,
610 struct dmar_domain *domain,
611 struct device *dev, u32 pasid)
612 {
613 struct pasid_entry *pte;
614 struct dma_pte *pgd;
615 u64 pgd_val;
616 int agaw;
617 u16 did;
618
619 /*
620 * If hardware advertises no support for second level
621 * translation, return directly.
622 */
623 if (!ecap_slts(iommu->ecap)) {
624 pr_err("No second level translation support on %s\n",
625 iommu->name);
626 return -EINVAL;
627 }
628
629 pgd = domain->pgd;
630 agaw = iommu_skip_agaw(domain, iommu, &pgd);
631 if (agaw < 0) {
632 dev_err(dev, "Invalid domain page table\n");
633 return -EINVAL;
634 }
635
636 pgd_val = virt_to_phys(pgd);
637 did = domain_id_iommu(domain, iommu);
638
639 spin_lock(&iommu->lock);
640 pte = intel_pasid_get_entry(dev, pasid);
641 if (!pte) {
642 spin_unlock(&iommu->lock);
643 return -ENODEV;
644 }
645
646 if (pasid_pte_is_present(pte)) {
647 spin_unlock(&iommu->lock);
648 return -EBUSY;
649 }
650
651 pasid_clear_entry(pte);
652 pasid_set_domain_id(pte, did);
653 pasid_set_slptr(pte, pgd_val);
654 pasid_set_address_width(pte, agaw);
655 pasid_set_translation_type(pte, PASID_ENTRY_PGTT_SL_ONLY);
656 pasid_set_fault_enable(pte);
657 pasid_set_page_snoop(pte, !!ecap_smpwc(iommu->ecap));
658
659 /*
660 * Since it is a second level only translation setup, we should
661 * set SRE bit as well (addresses are expected to be GPAs).
662 */
663 if (pasid != PASID_RID2PASID && ecap_srs(iommu->ecap))
664 pasid_set_sre(pte);
665 pasid_set_present(pte);
666 spin_unlock(&iommu->lock);
667
668 pasid_flush_caches(iommu, pte, pasid, did);
669
670 return 0;
671 }
672
673 /*
674 * Set up the scalable mode pasid entry for passthrough translation type.
675 */
intel_pasid_setup_pass_through(struct intel_iommu * iommu,struct dmar_domain * domain,struct device * dev,u32 pasid)676 int intel_pasid_setup_pass_through(struct intel_iommu *iommu,
677 struct dmar_domain *domain,
678 struct device *dev, u32 pasid)
679 {
680 u16 did = FLPT_DEFAULT_DID;
681 struct pasid_entry *pte;
682
683 spin_lock(&iommu->lock);
684 pte = intel_pasid_get_entry(dev, pasid);
685 if (!pte) {
686 spin_unlock(&iommu->lock);
687 return -ENODEV;
688 }
689
690 if (pasid_pte_is_present(pte)) {
691 spin_unlock(&iommu->lock);
692 return -EBUSY;
693 }
694
695 pasid_clear_entry(pte);
696 pasid_set_domain_id(pte, did);
697 pasid_set_address_width(pte, iommu->agaw);
698 pasid_set_translation_type(pte, PASID_ENTRY_PGTT_PT);
699 pasid_set_fault_enable(pte);
700 pasid_set_page_snoop(pte, !!ecap_smpwc(iommu->ecap));
701
702 /*
703 * We should set SRE bit as well since the addresses are expected
704 * to be GPAs.
705 */
706 if (ecap_srs(iommu->ecap))
707 pasid_set_sre(pte);
708 pasid_set_present(pte);
709 spin_unlock(&iommu->lock);
710
711 pasid_flush_caches(iommu, pte, pasid, did);
712
713 return 0;
714 }
715
716 /*
717 * Set the page snoop control for a pasid entry which has been set up.
718 */
intel_pasid_setup_page_snoop_control(struct intel_iommu * iommu,struct device * dev,u32 pasid)719 void intel_pasid_setup_page_snoop_control(struct intel_iommu *iommu,
720 struct device *dev, u32 pasid)
721 {
722 struct pasid_entry *pte;
723 u16 did;
724
725 spin_lock(&iommu->lock);
726 pte = intel_pasid_get_entry(dev, pasid);
727 if (WARN_ON(!pte || !pasid_pte_is_present(pte))) {
728 spin_unlock(&iommu->lock);
729 return;
730 }
731
732 pasid_set_pgsnp(pte);
733 did = pasid_get_domain_id(pte);
734 spin_unlock(&iommu->lock);
735
736 if (!ecap_coherent(iommu->ecap))
737 clflush_cache_range(pte, sizeof(*pte));
738
739 /*
740 * VT-d spec 3.4 table23 states guides for cache invalidation:
741 *
742 * - PASID-selective-within-Domain PASID-cache invalidation
743 * - PASID-selective PASID-based IOTLB invalidation
744 * - If (pasid is RID_PASID)
745 * - Global Device-TLB invalidation to affected functions
746 * Else
747 * - PASID-based Device-TLB invalidation (with S=1 and
748 * Addr[63:12]=0x7FFFFFFF_FFFFF) to affected functions
749 */
750 pasid_cache_invalidation_with_pasid(iommu, did, pasid);
751 qi_flush_piotlb(iommu, did, pasid, 0, -1, 0);
752
753 /* Device IOTLB doesn't need to be flushed in caching mode. */
754 if (!cap_caching_mode(iommu->cap))
755 devtlb_invalidation_with_pasid(iommu, dev, pasid);
756 }
757