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/intel-iommu.h>
16 #include <linux/iommu.h>
17 #include <linux/memory.h>
18 #include <linux/pci.h>
19 #include <linux/pci-ats.h>
20 #include <linux/spinlock.h>
21
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 static inline void
device_attach_pasid_table(struct device_domain_info * info,struct pasid_table * pasid_table)90 device_attach_pasid_table(struct device_domain_info *info,
91 struct pasid_table *pasid_table)
92 {
93 info->pasid_table = pasid_table;
94 list_add(&info->table, &pasid_table->dev);
95 }
96
97 static inline void
device_detach_pasid_table(struct device_domain_info * info,struct pasid_table * pasid_table)98 device_detach_pasid_table(struct device_domain_info *info,
99 struct pasid_table *pasid_table)
100 {
101 info->pasid_table = NULL;
102 list_del(&info->table);
103 }
104
105 struct pasid_table_opaque {
106 struct pasid_table **pasid_table;
107 int segment;
108 int bus;
109 int devfn;
110 };
111
search_pasid_table(struct device_domain_info * info,void * opaque)112 static int search_pasid_table(struct device_domain_info *info, void *opaque)
113 {
114 struct pasid_table_opaque *data = opaque;
115
116 if (info->iommu->segment == data->segment &&
117 info->bus == data->bus &&
118 info->devfn == data->devfn &&
119 info->pasid_table) {
120 *data->pasid_table = info->pasid_table;
121 return 1;
122 }
123
124 return 0;
125 }
126
get_alias_pasid_table(struct pci_dev * pdev,u16 alias,void * opaque)127 static int get_alias_pasid_table(struct pci_dev *pdev, u16 alias, void *opaque)
128 {
129 struct pasid_table_opaque *data = opaque;
130
131 data->segment = pci_domain_nr(pdev->bus);
132 data->bus = PCI_BUS_NUM(alias);
133 data->devfn = alias & 0xff;
134
135 return for_each_device_domain(&search_pasid_table, data);
136 }
137
138 /*
139 * Allocate a pasid table for @dev. It should be called in a
140 * single-thread context.
141 */
intel_pasid_alloc_table(struct device * dev)142 int intel_pasid_alloc_table(struct device *dev)
143 {
144 struct device_domain_info *info;
145 struct pasid_table *pasid_table;
146 struct pasid_table_opaque data;
147 struct page *pages;
148 u32 max_pasid = 0;
149 int ret, order;
150 int size;
151
152 might_sleep();
153 info = get_domain_info(dev);
154 if (WARN_ON(!info || !dev_is_pci(dev) || info->pasid_table))
155 return -EINVAL;
156
157 /* DMA alias device already has a pasid table, use it: */
158 data.pasid_table = &pasid_table;
159 ret = pci_for_each_dma_alias(to_pci_dev(dev),
160 &get_alias_pasid_table, &data);
161 if (ret)
162 goto attach_out;
163
164 pasid_table = kzalloc(sizeof(*pasid_table), GFP_KERNEL);
165 if (!pasid_table)
166 return -ENOMEM;
167 INIT_LIST_HEAD(&pasid_table->dev);
168
169 if (info->pasid_supported)
170 max_pasid = min_t(u32, pci_max_pasids(to_pci_dev(dev)),
171 intel_pasid_max_id);
172
173 size = max_pasid >> (PASID_PDE_SHIFT - 3);
174 order = size ? get_order(size) : 0;
175 pages = alloc_pages_node(info->iommu->node,
176 GFP_KERNEL | __GFP_ZERO, order);
177 if (!pages) {
178 kfree(pasid_table);
179 return -ENOMEM;
180 }
181
182 pasid_table->table = page_address(pages);
183 pasid_table->order = order;
184 pasid_table->max_pasid = 1 << (order + PAGE_SHIFT + 3);
185
186 attach_out:
187 device_attach_pasid_table(info, pasid_table);
188
189 if (!ecap_coherent(info->iommu->ecap))
190 clflush_cache_range(pasid_table->table, (1 << order) * PAGE_SIZE);
191
192 return 0;
193 }
194
intel_pasid_free_table(struct device * dev)195 void intel_pasid_free_table(struct device *dev)
196 {
197 struct device_domain_info *info;
198 struct pasid_table *pasid_table;
199 struct pasid_dir_entry *dir;
200 struct pasid_entry *table;
201 int i, max_pde;
202
203 info = get_domain_info(dev);
204 if (!info || !dev_is_pci(dev) || !info->pasid_table)
205 return;
206
207 pasid_table = info->pasid_table;
208 device_detach_pasid_table(info, pasid_table);
209
210 if (!list_empty(&pasid_table->dev))
211 return;
212
213 /* Free scalable mode PASID directory tables: */
214 dir = pasid_table->table;
215 max_pde = pasid_table->max_pasid >> PASID_PDE_SHIFT;
216 for (i = 0; i < max_pde; i++) {
217 table = get_pasid_table_from_pde(&dir[i]);
218 free_pgtable_page(table);
219 }
220
221 free_pages((unsigned long)pasid_table->table, pasid_table->order);
222 kfree(pasid_table);
223 }
224
intel_pasid_get_table(struct device * dev)225 struct pasid_table *intel_pasid_get_table(struct device *dev)
226 {
227 struct device_domain_info *info;
228
229 info = get_domain_info(dev);
230 if (!info)
231 return NULL;
232
233 return info->pasid_table;
234 }
235
intel_pasid_get_dev_max_id(struct device * dev)236 static int intel_pasid_get_dev_max_id(struct device *dev)
237 {
238 struct device_domain_info *info;
239
240 info = get_domain_info(dev);
241 if (!info || !info->pasid_table)
242 return 0;
243
244 return info->pasid_table->max_pasid;
245 }
246
intel_pasid_get_entry(struct device * dev,u32 pasid)247 static struct pasid_entry *intel_pasid_get_entry(struct device *dev, u32 pasid)
248 {
249 struct device_domain_info *info;
250 struct pasid_table *pasid_table;
251 struct pasid_dir_entry *dir;
252 struct pasid_entry *entries;
253 int dir_index, index;
254
255 pasid_table = intel_pasid_get_table(dev);
256 if (WARN_ON(!pasid_table || pasid >= intel_pasid_get_dev_max_id(dev)))
257 return NULL;
258
259 dir = pasid_table->table;
260 info = get_domain_info(dev);
261 dir_index = pasid >> PASID_PDE_SHIFT;
262 index = pasid & PASID_PTE_MASK;
263
264 retry:
265 entries = get_pasid_table_from_pde(&dir[dir_index]);
266 if (!entries) {
267 entries = alloc_pgtable_page(info->iommu->node);
268 if (!entries)
269 return NULL;
270
271 /*
272 * The pasid directory table entry won't be freed after
273 * allocation. No worry about the race with free and
274 * clear. However, this entry might be populated by others
275 * while we are preparing it. Use theirs with a retry.
276 */
277 if (cmpxchg64(&dir[dir_index].val, 0ULL,
278 (u64)virt_to_phys(entries) | PASID_PTE_PRESENT)) {
279 free_pgtable_page(entries);
280 goto retry;
281 }
282 if (!ecap_coherent(info->iommu->ecap)) {
283 clflush_cache_range(entries, VTD_PAGE_SIZE);
284 clflush_cache_range(&dir[dir_index].val, sizeof(*dir));
285 }
286 }
287
288 return &entries[index];
289 }
290
291 /*
292 * Interfaces for PASID table entry manipulation:
293 */
pasid_clear_entry(struct pasid_entry * pe)294 static inline void pasid_clear_entry(struct pasid_entry *pe)
295 {
296 WRITE_ONCE(pe->val[0], 0);
297 WRITE_ONCE(pe->val[1], 0);
298 WRITE_ONCE(pe->val[2], 0);
299 WRITE_ONCE(pe->val[3], 0);
300 WRITE_ONCE(pe->val[4], 0);
301 WRITE_ONCE(pe->val[5], 0);
302 WRITE_ONCE(pe->val[6], 0);
303 WRITE_ONCE(pe->val[7], 0);
304 }
305
pasid_clear_entry_with_fpd(struct pasid_entry * pe)306 static inline void pasid_clear_entry_with_fpd(struct pasid_entry *pe)
307 {
308 WRITE_ONCE(pe->val[0], PASID_PTE_FPD);
309 WRITE_ONCE(pe->val[1], 0);
310 WRITE_ONCE(pe->val[2], 0);
311 WRITE_ONCE(pe->val[3], 0);
312 WRITE_ONCE(pe->val[4], 0);
313 WRITE_ONCE(pe->val[5], 0);
314 WRITE_ONCE(pe->val[6], 0);
315 WRITE_ONCE(pe->val[7], 0);
316 }
317
318 static void
intel_pasid_clear_entry(struct device * dev,u32 pasid,bool fault_ignore)319 intel_pasid_clear_entry(struct device *dev, u32 pasid, bool fault_ignore)
320 {
321 struct pasid_entry *pe;
322
323 pe = intel_pasid_get_entry(dev, pasid);
324 if (WARN_ON(!pe))
325 return;
326
327 if (fault_ignore && pasid_pte_is_present(pe))
328 pasid_clear_entry_with_fpd(pe);
329 else
330 pasid_clear_entry(pe);
331 }
332
pasid_set_bits(u64 * ptr,u64 mask,u64 bits)333 static inline void pasid_set_bits(u64 *ptr, u64 mask, u64 bits)
334 {
335 u64 old;
336
337 old = READ_ONCE(*ptr);
338 WRITE_ONCE(*ptr, (old & ~mask) | bits);
339 }
340
341 /*
342 * Setup the DID(Domain Identifier) field (Bit 64~79) of scalable mode
343 * PASID entry.
344 */
345 static inline void
pasid_set_domain_id(struct pasid_entry * pe,u64 value)346 pasid_set_domain_id(struct pasid_entry *pe, u64 value)
347 {
348 pasid_set_bits(&pe->val[1], GENMASK_ULL(15, 0), value);
349 }
350
351 /*
352 * Get domain ID value of a scalable mode PASID entry.
353 */
354 static inline u16
pasid_get_domain_id(struct pasid_entry * pe)355 pasid_get_domain_id(struct pasid_entry *pe)
356 {
357 return (u16)(READ_ONCE(pe->val[1]) & GENMASK_ULL(15, 0));
358 }
359
360 /*
361 * Setup the SLPTPTR(Second Level Page Table Pointer) field (Bit 12~63)
362 * of a scalable mode PASID entry.
363 */
364 static inline void
pasid_set_slptr(struct pasid_entry * pe,u64 value)365 pasid_set_slptr(struct pasid_entry *pe, u64 value)
366 {
367 pasid_set_bits(&pe->val[0], VTD_PAGE_MASK, value);
368 }
369
370 /*
371 * Setup the AW(Address Width) field (Bit 2~4) of a scalable mode PASID
372 * entry.
373 */
374 static inline void
pasid_set_address_width(struct pasid_entry * pe,u64 value)375 pasid_set_address_width(struct pasid_entry *pe, u64 value)
376 {
377 pasid_set_bits(&pe->val[0], GENMASK_ULL(4, 2), value << 2);
378 }
379
380 /*
381 * Setup the PGTT(PASID Granular Translation Type) field (Bit 6~8)
382 * of a scalable mode PASID entry.
383 */
384 static inline void
pasid_set_translation_type(struct pasid_entry * pe,u64 value)385 pasid_set_translation_type(struct pasid_entry *pe, u64 value)
386 {
387 pasid_set_bits(&pe->val[0], GENMASK_ULL(8, 6), value << 6);
388 }
389
390 /*
391 * Enable fault processing by clearing the FPD(Fault Processing
392 * Disable) field (Bit 1) of a scalable mode PASID entry.
393 */
pasid_set_fault_enable(struct pasid_entry * pe)394 static inline void pasid_set_fault_enable(struct pasid_entry *pe)
395 {
396 pasid_set_bits(&pe->val[0], 1 << 1, 0);
397 }
398
399 /*
400 * Setup the SRE(Supervisor Request Enable) field (Bit 128) of a
401 * scalable mode PASID entry.
402 */
pasid_set_sre(struct pasid_entry * pe)403 static inline void pasid_set_sre(struct pasid_entry *pe)
404 {
405 pasid_set_bits(&pe->val[2], 1 << 0, 1);
406 }
407
408 /*
409 * Setup the WPE(Write Protect Enable) field (Bit 132) of a
410 * scalable mode PASID entry.
411 */
pasid_set_wpe(struct pasid_entry * pe)412 static inline void pasid_set_wpe(struct pasid_entry *pe)
413 {
414 pasid_set_bits(&pe->val[2], 1 << 4, 1 << 4);
415 }
416
417 /*
418 * Setup the P(Present) field (Bit 0) of a scalable mode PASID
419 * entry.
420 */
pasid_set_present(struct pasid_entry * pe)421 static inline void pasid_set_present(struct pasid_entry *pe)
422 {
423 pasid_set_bits(&pe->val[0], 1 << 0, 1);
424 }
425
426 /*
427 * Setup Page Walk Snoop bit (Bit 87) of a scalable mode PASID
428 * entry.
429 */
pasid_set_page_snoop(struct pasid_entry * pe,bool value)430 static inline void pasid_set_page_snoop(struct pasid_entry *pe, bool value)
431 {
432 pasid_set_bits(&pe->val[1], 1 << 23, value << 23);
433 }
434
435 /*
436 * Setup No Execute Enable bit (Bit 133) of a scalable mode PASID
437 * entry. It is required when XD bit of the first level page table
438 * entry is about to be set.
439 */
pasid_set_nxe(struct pasid_entry * pe)440 static inline void pasid_set_nxe(struct pasid_entry *pe)
441 {
442 pasid_set_bits(&pe->val[2], 1 << 5, 1 << 5);
443 }
444
445 /*
446 * Setup the Page Snoop (PGSNP) field (Bit 88) of a scalable mode
447 * PASID entry.
448 */
449 static inline void
pasid_set_pgsnp(struct pasid_entry * pe)450 pasid_set_pgsnp(struct pasid_entry *pe)
451 {
452 pasid_set_bits(&pe->val[1], 1ULL << 24, 1ULL << 24);
453 }
454
455 /*
456 * Setup the First Level Page table Pointer field (Bit 140~191)
457 * of a scalable mode PASID entry.
458 */
459 static inline void
pasid_set_flptr(struct pasid_entry * pe,u64 value)460 pasid_set_flptr(struct pasid_entry *pe, u64 value)
461 {
462 pasid_set_bits(&pe->val[2], VTD_PAGE_MASK, value);
463 }
464
465 /*
466 * Setup the First Level Paging Mode field (Bit 130~131) of a
467 * scalable mode PASID entry.
468 */
469 static inline void
pasid_set_flpm(struct pasid_entry * pe,u64 value)470 pasid_set_flpm(struct pasid_entry *pe, u64 value)
471 {
472 pasid_set_bits(&pe->val[2], GENMASK_ULL(3, 2), value << 2);
473 }
474
475 /*
476 * Setup the Extended Access Flag Enable (EAFE) field (Bit 135)
477 * of a scalable mode PASID entry.
478 */
479 static inline void
pasid_set_eafe(struct pasid_entry * pe)480 pasid_set_eafe(struct pasid_entry *pe)
481 {
482 pasid_set_bits(&pe->val[2], 1 << 7, 1 << 7);
483 }
484
485 static void
pasid_cache_invalidation_with_pasid(struct intel_iommu * iommu,u16 did,u32 pasid)486 pasid_cache_invalidation_with_pasid(struct intel_iommu *iommu,
487 u16 did, u32 pasid)
488 {
489 struct qi_desc desc;
490
491 desc.qw0 = QI_PC_DID(did) | QI_PC_GRAN(QI_PC_PASID_SEL) |
492 QI_PC_PASID(pasid) | QI_PC_TYPE;
493 desc.qw1 = 0;
494 desc.qw2 = 0;
495 desc.qw3 = 0;
496
497 qi_submit_sync(iommu, &desc, 1, 0);
498 }
499
500 static void
devtlb_invalidation_with_pasid(struct intel_iommu * iommu,struct device * dev,u32 pasid)501 devtlb_invalidation_with_pasid(struct intel_iommu *iommu,
502 struct device *dev, u32 pasid)
503 {
504 struct device_domain_info *info;
505 u16 sid, qdep, pfsid;
506
507 info = get_domain_info(dev);
508 if (!info || !info->ats_enabled)
509 return;
510
511 sid = info->bus << 8 | info->devfn;
512 qdep = info->ats_qdep;
513 pfsid = info->pfsid;
514
515 /*
516 * When PASID 0 is used, it indicates RID2PASID(DMA request w/o PASID),
517 * devTLB flush w/o PASID should be used. For non-zero PASID under
518 * SVA usage, device could do DMA with multiple PASIDs. It is more
519 * efficient to flush devTLB specific to the PASID.
520 */
521 if (pasid == PASID_RID2PASID)
522 qi_flush_dev_iotlb(iommu, sid, pfsid, qdep, 0, 64 - VTD_PAGE_SHIFT);
523 else
524 qi_flush_dev_iotlb_pasid(iommu, sid, pfsid, pasid, qdep, 0, 64 - VTD_PAGE_SHIFT);
525 }
526
intel_pasid_tear_down_entry(struct intel_iommu * iommu,struct device * dev,u32 pasid,bool fault_ignore)527 void intel_pasid_tear_down_entry(struct intel_iommu *iommu, struct device *dev,
528 u32 pasid, bool fault_ignore)
529 {
530 struct pasid_entry *pte;
531 u16 did, pgtt;
532
533 pte = intel_pasid_get_entry(dev, pasid);
534 if (WARN_ON(!pte))
535 return;
536
537 if (!pasid_pte_is_present(pte))
538 return;
539
540 did = pasid_get_domain_id(pte);
541 pgtt = pasid_pte_get_pgtt(pte);
542
543 intel_pasid_clear_entry(dev, pasid, fault_ignore);
544
545 if (!ecap_coherent(iommu->ecap))
546 clflush_cache_range(pte, sizeof(*pte));
547
548 pasid_cache_invalidation_with_pasid(iommu, did, pasid);
549
550 if (pgtt == PASID_ENTRY_PGTT_PT || pgtt == PASID_ENTRY_PGTT_FL_ONLY)
551 qi_flush_piotlb(iommu, did, pasid, 0, -1, 0);
552 else
553 iommu->flush.flush_iotlb(iommu, did, 0, 0, DMA_TLB_DSI_FLUSH);
554
555 /* Device IOTLB doesn't need to be flushed in caching mode. */
556 if (!cap_caching_mode(iommu->cap))
557 devtlb_invalidation_with_pasid(iommu, dev, pasid);
558 }
559
560 /*
561 * This function flushes cache for a newly setup pasid table entry.
562 * Caller of it should not modify the in-use pasid table entries.
563 */
pasid_flush_caches(struct intel_iommu * iommu,struct pasid_entry * pte,u32 pasid,u16 did)564 static void pasid_flush_caches(struct intel_iommu *iommu,
565 struct pasid_entry *pte,
566 u32 pasid, u16 did)
567 {
568 if (!ecap_coherent(iommu->ecap))
569 clflush_cache_range(pte, sizeof(*pte));
570
571 if (cap_caching_mode(iommu->cap)) {
572 pasid_cache_invalidation_with_pasid(iommu, did, pasid);
573 qi_flush_piotlb(iommu, did, pasid, 0, -1, 0);
574 } else {
575 iommu_flush_write_buffer(iommu);
576 }
577 }
578
pasid_enable_wpe(struct pasid_entry * pte)579 static inline int pasid_enable_wpe(struct pasid_entry *pte)
580 {
581 #ifdef CONFIG_X86
582 unsigned long cr0 = read_cr0();
583
584 /* CR0.WP is normally set but just to be sure */
585 if (unlikely(!(cr0 & X86_CR0_WP))) {
586 pr_err_ratelimited("No CPU write protect!\n");
587 return -EINVAL;
588 }
589 #endif
590 pasid_set_wpe(pte);
591
592 return 0;
593 };
594
595 /*
596 * Set up the scalable mode pasid table entry for first only
597 * translation type.
598 */
intel_pasid_setup_first_level(struct intel_iommu * iommu,struct device * dev,pgd_t * pgd,u32 pasid,u16 did,int flags)599 int intel_pasid_setup_first_level(struct intel_iommu *iommu,
600 struct device *dev, pgd_t *pgd,
601 u32 pasid, u16 did, int flags)
602 {
603 struct pasid_entry *pte;
604
605 if (!ecap_flts(iommu->ecap)) {
606 pr_err("No first level translation support on %s\n",
607 iommu->name);
608 return -EINVAL;
609 }
610
611 pte = intel_pasid_get_entry(dev, pasid);
612 if (WARN_ON(!pte))
613 return -EINVAL;
614
615 /* Caller must ensure PASID entry is not in use. */
616 if (pasid_pte_is_present(pte))
617 return -EBUSY;
618
619 pasid_clear_entry(pte);
620
621 /* Setup the first level page table pointer: */
622 pasid_set_flptr(pte, (u64)__pa(pgd));
623 if (flags & PASID_FLAG_SUPERVISOR_MODE) {
624 if (!ecap_srs(iommu->ecap)) {
625 pr_err("No supervisor request support on %s\n",
626 iommu->name);
627 return -EINVAL;
628 }
629 pasid_set_sre(pte);
630 if (pasid_enable_wpe(pte))
631 return -EINVAL;
632
633 }
634
635 if (flags & PASID_FLAG_FL5LP) {
636 if (cap_5lp_support(iommu->cap)) {
637 pasid_set_flpm(pte, 1);
638 } else {
639 pr_err("No 5-level paging support for first-level\n");
640 pasid_clear_entry(pte);
641 return -EINVAL;
642 }
643 }
644
645 if (flags & PASID_FLAG_PAGE_SNOOP)
646 pasid_set_pgsnp(pte);
647
648 pasid_set_domain_id(pte, did);
649 pasid_set_address_width(pte, iommu->agaw);
650 pasid_set_page_snoop(pte, !!ecap_smpwc(iommu->ecap));
651 pasid_set_nxe(pte);
652
653 /* Setup Present and PASID Granular Transfer Type: */
654 pasid_set_translation_type(pte, PASID_ENTRY_PGTT_FL_ONLY);
655 pasid_set_present(pte);
656 pasid_flush_caches(iommu, pte, pasid, did);
657
658 return 0;
659 }
660
661 /*
662 * Skip top levels of page tables for iommu which has less agaw
663 * than default. Unnecessary for PT mode.
664 */
iommu_skip_agaw(struct dmar_domain * domain,struct intel_iommu * iommu,struct dma_pte ** pgd)665 static inline int iommu_skip_agaw(struct dmar_domain *domain,
666 struct intel_iommu *iommu,
667 struct dma_pte **pgd)
668 {
669 int agaw;
670
671 for (agaw = domain->agaw; agaw > iommu->agaw; agaw--) {
672 *pgd = phys_to_virt(dma_pte_addr(*pgd));
673 if (!dma_pte_present(*pgd))
674 return -EINVAL;
675 }
676
677 return agaw;
678 }
679
680 /*
681 * Set up the scalable mode pasid entry for second only translation type.
682 */
intel_pasid_setup_second_level(struct intel_iommu * iommu,struct dmar_domain * domain,struct device * dev,u32 pasid)683 int intel_pasid_setup_second_level(struct intel_iommu *iommu,
684 struct dmar_domain *domain,
685 struct device *dev, u32 pasid)
686 {
687 struct pasid_entry *pte;
688 struct dma_pte *pgd;
689 u64 pgd_val;
690 int agaw;
691 u16 did;
692
693 /*
694 * If hardware advertises no support for second level
695 * translation, return directly.
696 */
697 if (!ecap_slts(iommu->ecap)) {
698 pr_err("No second level translation support on %s\n",
699 iommu->name);
700 return -EINVAL;
701 }
702
703 pgd = domain->pgd;
704 agaw = iommu_skip_agaw(domain, iommu, &pgd);
705 if (agaw < 0) {
706 dev_err(dev, "Invalid domain page table\n");
707 return -EINVAL;
708 }
709
710 pgd_val = virt_to_phys(pgd);
711 did = domain->iommu_did[iommu->seq_id];
712
713 pte = intel_pasid_get_entry(dev, pasid);
714 if (!pte) {
715 dev_err(dev, "Failed to get pasid entry of PASID %d\n", pasid);
716 return -ENODEV;
717 }
718
719 /* Caller must ensure PASID entry is not in use. */
720 if (pasid_pte_is_present(pte))
721 return -EBUSY;
722
723 pasid_clear_entry(pte);
724 pasid_set_domain_id(pte, did);
725 pasid_set_slptr(pte, pgd_val);
726 pasid_set_address_width(pte, agaw);
727 pasid_set_translation_type(pte, PASID_ENTRY_PGTT_SL_ONLY);
728 pasid_set_fault_enable(pte);
729 pasid_set_page_snoop(pte, !!ecap_smpwc(iommu->ecap));
730
731 if (domain->domain.type == IOMMU_DOMAIN_UNMANAGED)
732 pasid_set_pgsnp(pte);
733
734 /*
735 * Since it is a second level only translation setup, we should
736 * set SRE bit as well (addresses are expected to be GPAs).
737 */
738 if (pasid != PASID_RID2PASID && ecap_srs(iommu->ecap))
739 pasid_set_sre(pte);
740 pasid_set_present(pte);
741 pasid_flush_caches(iommu, pte, pasid, did);
742
743 return 0;
744 }
745
746 /*
747 * Set up the scalable mode pasid entry for passthrough translation type.
748 */
intel_pasid_setup_pass_through(struct intel_iommu * iommu,struct dmar_domain * domain,struct device * dev,u32 pasid)749 int intel_pasid_setup_pass_through(struct intel_iommu *iommu,
750 struct dmar_domain *domain,
751 struct device *dev, u32 pasid)
752 {
753 u16 did = FLPT_DEFAULT_DID;
754 struct pasid_entry *pte;
755
756 pte = intel_pasid_get_entry(dev, pasid);
757 if (!pte) {
758 dev_err(dev, "Failed to get pasid entry of PASID %d\n", pasid);
759 return -ENODEV;
760 }
761
762 /* Caller must ensure PASID entry is not in use. */
763 if (pasid_pte_is_present(pte))
764 return -EBUSY;
765
766 pasid_clear_entry(pte);
767 pasid_set_domain_id(pte, did);
768 pasid_set_address_width(pte, iommu->agaw);
769 pasid_set_translation_type(pte, PASID_ENTRY_PGTT_PT);
770 pasid_set_fault_enable(pte);
771 pasid_set_page_snoop(pte, !!ecap_smpwc(iommu->ecap));
772
773 /*
774 * We should set SRE bit as well since the addresses are expected
775 * to be GPAs.
776 */
777 if (ecap_srs(iommu->ecap))
778 pasid_set_sre(pte);
779 pasid_set_present(pte);
780 pasid_flush_caches(iommu, pte, pasid, did);
781
782 return 0;
783 }
784
785 static int
intel_pasid_setup_bind_data(struct intel_iommu * iommu,struct pasid_entry * pte,struct iommu_gpasid_bind_data_vtd * pasid_data)786 intel_pasid_setup_bind_data(struct intel_iommu *iommu, struct pasid_entry *pte,
787 struct iommu_gpasid_bind_data_vtd *pasid_data)
788 {
789 /*
790 * Not all guest PASID table entry fields are passed down during bind,
791 * here we only set up the ones that are dependent on guest settings.
792 * Execution related bits such as NXE, SMEP are not supported.
793 * Other fields, such as snoop related, are set based on host needs
794 * regardless of guest settings.
795 */
796 if (pasid_data->flags & IOMMU_SVA_VTD_GPASID_SRE) {
797 if (!ecap_srs(iommu->ecap)) {
798 pr_err_ratelimited("No supervisor request support on %s\n",
799 iommu->name);
800 return -EINVAL;
801 }
802 pasid_set_sre(pte);
803 /* Enable write protect WP if guest requested */
804 if (pasid_data->flags & IOMMU_SVA_VTD_GPASID_WPE)
805 pasid_set_wpe(pte);
806 }
807
808 if (pasid_data->flags & IOMMU_SVA_VTD_GPASID_EAFE) {
809 if (!ecap_eafs(iommu->ecap)) {
810 pr_err_ratelimited("No extended access flag support on %s\n",
811 iommu->name);
812 return -EINVAL;
813 }
814 pasid_set_eafe(pte);
815 }
816
817 /*
818 * Memory type is only applicable to devices inside processor coherent
819 * domain. Will add MTS support once coherent devices are available.
820 */
821 if (pasid_data->flags & IOMMU_SVA_VTD_GPASID_MTS_MASK) {
822 pr_warn_ratelimited("No memory type support %s\n",
823 iommu->name);
824 return -EINVAL;
825 }
826
827 return 0;
828 }
829
830 /**
831 * intel_pasid_setup_nested() - Set up PASID entry for nested translation.
832 * This could be used for guest shared virtual address. In this case, the
833 * first level page tables are used for GVA-GPA translation in the guest,
834 * second level page tables are used for GPA-HPA translation.
835 *
836 * @iommu: IOMMU which the device belong to
837 * @dev: Device to be set up for translation
838 * @gpgd: FLPTPTR: First Level Page translation pointer in GPA
839 * @pasid: PASID to be programmed in the device PASID table
840 * @pasid_data: Additional PASID info from the guest bind request
841 * @domain: Domain info for setting up second level page tables
842 * @addr_width: Address width of the first level (guest)
843 */
intel_pasid_setup_nested(struct intel_iommu * iommu,struct device * dev,pgd_t * gpgd,u32 pasid,struct iommu_gpasid_bind_data_vtd * pasid_data,struct dmar_domain * domain,int addr_width)844 int intel_pasid_setup_nested(struct intel_iommu *iommu, struct device *dev,
845 pgd_t *gpgd, u32 pasid,
846 struct iommu_gpasid_bind_data_vtd *pasid_data,
847 struct dmar_domain *domain, int addr_width)
848 {
849 struct pasid_entry *pte;
850 struct dma_pte *pgd;
851 int ret = 0;
852 u64 pgd_val;
853 int agaw;
854 u16 did;
855
856 if (!ecap_nest(iommu->ecap)) {
857 pr_err_ratelimited("IOMMU: %s: No nested translation support\n",
858 iommu->name);
859 return -EINVAL;
860 }
861
862 if (!(domain->flags & DOMAIN_FLAG_NESTING_MODE)) {
863 pr_err_ratelimited("Domain is not in nesting mode, %x\n",
864 domain->flags);
865 return -EINVAL;
866 }
867
868 pte = intel_pasid_get_entry(dev, pasid);
869 if (WARN_ON(!pte))
870 return -EINVAL;
871
872 /*
873 * Caller must ensure PASID entry is not in use, i.e. not bind the
874 * same PASID to the same device twice.
875 */
876 if (pasid_pte_is_present(pte))
877 return -EBUSY;
878
879 pasid_clear_entry(pte);
880
881 /* Sanity checking performed by caller to make sure address
882 * width matching in two dimensions:
883 * 1. CPU vs. IOMMU
884 * 2. Guest vs. Host.
885 */
886 switch (addr_width) {
887 #ifdef CONFIG_X86
888 case ADDR_WIDTH_5LEVEL:
889 if (!cpu_feature_enabled(X86_FEATURE_LA57) ||
890 !cap_5lp_support(iommu->cap)) {
891 dev_err_ratelimited(dev,
892 "5-level paging not supported\n");
893 return -EINVAL;
894 }
895
896 pasid_set_flpm(pte, 1);
897 break;
898 #endif
899 case ADDR_WIDTH_4LEVEL:
900 pasid_set_flpm(pte, 0);
901 break;
902 default:
903 dev_err_ratelimited(dev, "Invalid guest address width %d\n",
904 addr_width);
905 return -EINVAL;
906 }
907
908 /* First level PGD is in GPA, must be supported by the second level */
909 if ((uintptr_t)gpgd > domain->max_addr) {
910 dev_err_ratelimited(dev,
911 "Guest PGD %lx not supported, max %llx\n",
912 (uintptr_t)gpgd, domain->max_addr);
913 return -EINVAL;
914 }
915 pasid_set_flptr(pte, (uintptr_t)gpgd);
916
917 ret = intel_pasid_setup_bind_data(iommu, pte, pasid_data);
918 if (ret)
919 return ret;
920
921 /* Setup the second level based on the given domain */
922 pgd = domain->pgd;
923
924 agaw = iommu_skip_agaw(domain, iommu, &pgd);
925 if (agaw < 0) {
926 dev_err_ratelimited(dev, "Invalid domain page table\n");
927 return -EINVAL;
928 }
929 pgd_val = virt_to_phys(pgd);
930 pasid_set_slptr(pte, pgd_val);
931 pasid_set_fault_enable(pte);
932
933 did = domain->iommu_did[iommu->seq_id];
934 pasid_set_domain_id(pte, did);
935
936 pasid_set_address_width(pte, agaw);
937 pasid_set_page_snoop(pte, !!ecap_smpwc(iommu->ecap));
938
939 pasid_set_translation_type(pte, PASID_ENTRY_PGTT_NESTED);
940 pasid_set_present(pte);
941 pasid_flush_caches(iommu, pte, pasid, did);
942
943 return ret;
944 }
945