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