1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (C) 2007-2010 Advanced Micro Devices, Inc.
4 * Author: Joerg Roedel <jroedel@suse.de>
5 * Leo Duran <leo.duran@amd.com>
6 */
7
8 #define pr_fmt(fmt) "AMD-Vi: " fmt
9 #define dev_fmt(fmt) pr_fmt(fmt)
10
11 #include <linux/ratelimit.h>
12 #include <linux/pci.h>
13 #include <linux/acpi.h>
14 #include <linux/pci-ats.h>
15 #include <linux/bitmap.h>
16 #include <linux/slab.h>
17 #include <linux/string_choices.h>
18 #include <linux/debugfs.h>
19 #include <linux/scatterlist.h>
20 #include <linux/dma-map-ops.h>
21 #include <linux/dma-direct.h>
22 #include <linux/idr.h>
23 #include <linux/iommu-helper.h>
24 #include <linux/delay.h>
25 #include <linux/amd-iommu.h>
26 #include <linux/notifier.h>
27 #include <linux/export.h>
28 #include <linux/irq.h>
29 #include <linux/irqchip/irq-msi-lib.h>
30 #include <linux/msi.h>
31 #include <linux/irqdomain.h>
32 #include <linux/percpu.h>
33 #include <linux/cc_platform.h>
34 #include <asm/irq_remapping.h>
35 #include <asm/io_apic.h>
36 #include <asm/apic.h>
37 #include <asm/hw_irq.h>
38 #include <asm/proto.h>
39 #include <asm/iommu.h>
40 #include <asm/gart.h>
41 #include <asm/dma.h>
42 #include <uapi/linux/iommufd.h>
43 #include <linux/generic_pt/iommu.h>
44
45 #include "amd_iommu.h"
46 #include "iommufd.h"
47 #include "../irq_remapping.h"
48 #include "../iommu-pages.h"
49
50 #define CMD_SET_TYPE(cmd, t) ((cmd)->data[1] |= ((t) << 28))
51
52 /* Reserved IOVA ranges */
53 #define MSI_RANGE_START (0xfee00000)
54 #define MSI_RANGE_END (0xfeefffff)
55 #define HT_RANGE_START (0xfd00000000ULL)
56 #define HT_RANGE_END (0xffffffffffULL)
57
58 LIST_HEAD(ioapic_map);
59 LIST_HEAD(hpet_map);
60 LIST_HEAD(acpihid_map);
61
62 const struct iommu_ops amd_iommu_ops;
63
64 int amd_iommu_max_glx_val = -1;
65
66 /*
67 * AMD IOMMU allows up to 2^16 different protection domains. This is a bitmap
68 * to know which ones are already in use.
69 */
70 DEFINE_IDA(pdom_ids);
71
72 static int amd_iommu_attach_device(struct iommu_domain *dom, struct device *dev,
73 struct iommu_domain *old);
74
75 static void set_dte_entry(struct amd_iommu *iommu,
76 struct iommu_dev_data *dev_data,
77 phys_addr_t top_paddr, unsigned int top_level);
78
79 static int device_flush_dte(struct iommu_dev_data *dev_data);
80
81 static void amd_iommu_change_top(struct pt_iommu *iommu_table,
82 phys_addr_t top_paddr, unsigned int top_level);
83
84 static void iommu_flush_dte_sync(struct amd_iommu *iommu, u16 devid);
85
86 static struct iommu_dev_data *find_dev_data(struct amd_iommu *iommu, u16 devid);
87 static bool amd_iommu_enforce_cache_coherency(struct iommu_domain *domain);
88 static int amd_iommu_set_dirty_tracking(struct iommu_domain *domain,
89 bool enable);
90
91 static void clone_aliases(struct amd_iommu *iommu, struct device *dev);
92
93 static int iommu_completion_wait(struct amd_iommu *iommu);
94
95 static int __amd_iommu_complete_ppr(struct device *dev, u32 pasid,
96 int status, int tag, bool gn);
97
98 /****************************************************************************
99 *
100 * Helper functions
101 *
102 ****************************************************************************/
103
amd_iommu_atomic128_set(__int128 * ptr,__int128 val)104 static __always_inline void amd_iommu_atomic128_set(__int128 *ptr, __int128 val)
105 {
106 /*
107 * Note:
108 * We use arch_cmpxchg128_local() because:
109 * - Need cmpxchg16b instruction mainly for 128-bit store to DTE
110 * (not necessary for cmpxchg since this function is already
111 * protected by a spin_lock for this DTE).
112 * - Neither need LOCK_PREFIX nor try loop because of the spin_lock.
113 */
114 arch_cmpxchg128_local(ptr, *ptr, val);
115 }
116
write_dte_upper128(struct dev_table_entry * ptr,struct dev_table_entry * new)117 static void write_dte_upper128(struct dev_table_entry *ptr, struct dev_table_entry *new)
118 {
119 struct dev_table_entry old;
120
121 old.data128[1] = ptr->data128[1];
122 /*
123 * Preserve DTE_DATA2_INTR_MASK. This needs to be
124 * done here since it requires to be inside
125 * spin_lock(&dev_data->dte_lock) context.
126 */
127 new->data[2] &= ~DTE_DATA2_INTR_MASK;
128 new->data[2] |= old.data[2] & DTE_DATA2_INTR_MASK;
129
130 amd_iommu_atomic128_set(&ptr->data128[1], new->data128[1]);
131 }
132
write_dte_lower128(struct dev_table_entry * ptr,struct dev_table_entry * new)133 static void write_dte_lower128(struct dev_table_entry *ptr, struct dev_table_entry *new)
134 {
135 amd_iommu_atomic128_set(&ptr->data128[0], new->data128[0]);
136 }
137
138 /*
139 * Note:
140 * IOMMU reads the entire Device Table entry in a single 256-bit transaction
141 * but the driver is programming DTE using 2 128-bit cmpxchg. So, the driver
142 * need to ensure the following:
143 * - DTE[V|GV] bit is being written last when setting.
144 * - DTE[V|GV] bit is being written first when clearing.
145 *
146 * This function is used only by code, which updates DMA translation part of the DTE.
147 * So, only consider control bits related to DMA when updating the entry.
148 */
update_dte256(struct amd_iommu * iommu,struct iommu_dev_data * dev_data,struct dev_table_entry * new)149 static void update_dte256(struct amd_iommu *iommu, struct iommu_dev_data *dev_data,
150 struct dev_table_entry *new)
151 {
152 unsigned long flags;
153 struct dev_table_entry *dev_table = get_dev_table(iommu);
154 struct dev_table_entry *ptr = &dev_table[dev_data->devid];
155
156 spin_lock_irqsave(&dev_data->dte_lock, flags);
157
158 if (!(ptr->data[0] & DTE_FLAG_V)) {
159 /* Existing DTE is not valid. */
160 write_dte_upper128(ptr, new);
161 write_dte_lower128(ptr, new);
162 iommu_flush_dte_sync(iommu, dev_data->devid);
163 } else if (!(new->data[0] & DTE_FLAG_V)) {
164 /* Existing DTE is valid. New DTE is not valid. */
165 write_dte_lower128(ptr, new);
166 write_dte_upper128(ptr, new);
167 iommu_flush_dte_sync(iommu, dev_data->devid);
168 } else if (!FIELD_GET(DTE_FLAG_GV, ptr->data[0])) {
169 /*
170 * Both DTEs are valid.
171 * Existing DTE has no guest page table.
172 */
173 write_dte_upper128(ptr, new);
174 write_dte_lower128(ptr, new);
175 iommu_flush_dte_sync(iommu, dev_data->devid);
176 } else if (!FIELD_GET(DTE_FLAG_GV, new->data[0])) {
177 /*
178 * Both DTEs are valid.
179 * Existing DTE has guest page table,
180 * new DTE has no guest page table,
181 */
182 write_dte_lower128(ptr, new);
183 write_dte_upper128(ptr, new);
184 iommu_flush_dte_sync(iommu, dev_data->devid);
185 } else if (FIELD_GET(DTE_GPT_LEVEL_MASK, ptr->data[2]) !=
186 FIELD_GET(DTE_GPT_LEVEL_MASK, new->data[2])) {
187 /*
188 * Both DTEs are valid and have guest page table,
189 * but have different number of levels. So, we need
190 * to upadte both upper and lower 128-bit value, which
191 * require disabling and flushing.
192 */
193 struct dev_table_entry clear = {};
194
195 /* First disable DTE */
196 write_dte_lower128(ptr, &clear);
197 iommu_flush_dte_sync(iommu, dev_data->devid);
198
199 /* Then update DTE */
200 write_dte_upper128(ptr, new);
201 write_dte_lower128(ptr, new);
202 iommu_flush_dte_sync(iommu, dev_data->devid);
203 } else {
204 /*
205 * Both DTEs are valid and have guest page table,
206 * and same number of levels. We just need to only
207 * update the lower 128-bit. So no need to disable DTE.
208 */
209 write_dte_lower128(ptr, new);
210 }
211
212 spin_unlock_irqrestore(&dev_data->dte_lock, flags);
213 }
214
amd_iommu_update_dte(struct amd_iommu * iommu,struct iommu_dev_data * dev_data,struct dev_table_entry * new)215 void amd_iommu_update_dte(struct amd_iommu *iommu,
216 struct iommu_dev_data *dev_data,
217 struct dev_table_entry *new)
218 {
219 update_dte256(iommu, dev_data, new);
220 clone_aliases(iommu, dev_data->dev);
221 device_flush_dte(dev_data);
222 iommu_completion_wait(iommu);
223 }
224
get_dte256(struct amd_iommu * iommu,struct iommu_dev_data * dev_data,struct dev_table_entry * dte)225 static void get_dte256(struct amd_iommu *iommu, struct iommu_dev_data *dev_data,
226 struct dev_table_entry *dte)
227 {
228 unsigned long flags;
229 struct dev_table_entry *ptr;
230 struct dev_table_entry *dev_table = get_dev_table(iommu);
231
232 ptr = &dev_table[dev_data->devid];
233
234 spin_lock_irqsave(&dev_data->dte_lock, flags);
235 dte->data128[0] = ptr->data128[0];
236 dte->data128[1] = ptr->data128[1];
237 spin_unlock_irqrestore(&dev_data->dte_lock, flags);
238 }
239
pdom_is_v2_pgtbl_mode(struct protection_domain * pdom)240 static inline bool pdom_is_v2_pgtbl_mode(struct protection_domain *pdom)
241 {
242 return (pdom && (pdom->pd_mode == PD_MODE_V2));
243 }
244
pdom_is_in_pt_mode(struct protection_domain * pdom)245 static inline bool pdom_is_in_pt_mode(struct protection_domain *pdom)
246 {
247 return (pdom->domain.type == IOMMU_DOMAIN_IDENTITY);
248 }
249
250 /*
251 * We cannot support PASID w/ existing v1 page table in the same domain
252 * since it will be nested. However, existing domain w/ v2 page table
253 * or passthrough mode can be used for PASID.
254 */
pdom_is_sva_capable(struct protection_domain * pdom)255 static inline bool pdom_is_sva_capable(struct protection_domain *pdom)
256 {
257 return pdom_is_v2_pgtbl_mode(pdom) || pdom_is_in_pt_mode(pdom);
258 }
259
get_acpihid_device_id(struct device * dev,struct acpihid_map_entry ** entry)260 static inline int get_acpihid_device_id(struct device *dev,
261 struct acpihid_map_entry **entry)
262 {
263 struct acpi_device *adev = ACPI_COMPANION(dev);
264 struct acpihid_map_entry *p, *p1 = NULL;
265 int hid_count = 0;
266 bool fw_bug;
267
268 if (!adev)
269 return -ENODEV;
270
271 list_for_each_entry(p, &acpihid_map, list) {
272 if (acpi_dev_hid_uid_match(adev, p->hid,
273 p->uid[0] ? p->uid : NULL)) {
274 p1 = p;
275 fw_bug = false;
276 hid_count = 1;
277 break;
278 }
279
280 /*
281 * Count HID matches w/o UID, raise FW_BUG but allow exactly one match
282 */
283 if (acpi_dev_hid_match(adev, p->hid)) {
284 p1 = p;
285 hid_count++;
286 fw_bug = true;
287 }
288 }
289
290 if (!p1)
291 return -EINVAL;
292 if (fw_bug)
293 dev_err_once(dev, FW_BUG "No ACPI device matched UID, but %d device%s matched HID.\n",
294 hid_count, str_plural(hid_count));
295 if (hid_count > 1)
296 return -EINVAL;
297 if (entry)
298 *entry = p1;
299
300 return p1->devid;
301 }
302
get_device_sbdf_id(struct device * dev)303 static inline int get_device_sbdf_id(struct device *dev)
304 {
305 int sbdf;
306
307 if (dev_is_pci(dev))
308 sbdf = get_pci_sbdf_id(to_pci_dev(dev));
309 else
310 sbdf = get_acpihid_device_id(dev, NULL);
311
312 return sbdf;
313 }
314
get_dev_table(struct amd_iommu * iommu)315 struct dev_table_entry *get_dev_table(struct amd_iommu *iommu)
316 {
317 struct dev_table_entry *dev_table;
318 struct amd_iommu_pci_seg *pci_seg = iommu->pci_seg;
319
320 BUG_ON(pci_seg == NULL);
321 dev_table = pci_seg->dev_table;
322 BUG_ON(dev_table == NULL);
323
324 return dev_table;
325 }
326
get_device_segment(struct device * dev)327 static inline u16 get_device_segment(struct device *dev)
328 {
329 u16 seg;
330
331 if (dev_is_pci(dev)) {
332 struct pci_dev *pdev = to_pci_dev(dev);
333
334 seg = pci_domain_nr(pdev->bus);
335 } else {
336 u32 devid = get_acpihid_device_id(dev, NULL);
337
338 seg = PCI_SBDF_TO_SEGID(devid);
339 }
340
341 return seg;
342 }
343
344 /* Writes the specific IOMMU for a device into the PCI segment rlookup table */
amd_iommu_set_rlookup_table(struct amd_iommu * iommu,u16 devid)345 void amd_iommu_set_rlookup_table(struct amd_iommu *iommu, u16 devid)
346 {
347 struct amd_iommu_pci_seg *pci_seg = iommu->pci_seg;
348
349 pci_seg->rlookup_table[devid] = iommu;
350 }
351
__rlookup_amd_iommu(u16 seg,u16 devid)352 static struct amd_iommu *__rlookup_amd_iommu(u16 seg, u16 devid)
353 {
354 struct amd_iommu_pci_seg *pci_seg;
355
356 for_each_pci_segment(pci_seg) {
357 if (pci_seg->id != seg)
358 continue;
359 /* IVRS may not describe every device on the bus */
360 if (devid > pci_seg->last_bdf)
361 return NULL;
362 return pci_seg->rlookup_table[devid];
363 }
364 return NULL;
365 }
366
rlookup_amd_iommu(struct device * dev)367 static struct amd_iommu *rlookup_amd_iommu(struct device *dev)
368 {
369 u16 seg = get_device_segment(dev);
370 int devid = get_device_sbdf_id(dev);
371
372 if (devid < 0)
373 return NULL;
374 return __rlookup_amd_iommu(seg, PCI_SBDF_TO_DEVID(devid));
375 }
376
alloc_dev_data(struct amd_iommu * iommu,u16 devid)377 static struct iommu_dev_data *alloc_dev_data(struct amd_iommu *iommu, u16 devid)
378 {
379 struct iommu_dev_data *dev_data;
380 struct amd_iommu_pci_seg *pci_seg = iommu->pci_seg;
381
382 dev_data = kzalloc_obj(*dev_data);
383 if (!dev_data)
384 return NULL;
385
386 mutex_init(&dev_data->mutex);
387 spin_lock_init(&dev_data->dte_lock);
388 dev_data->devid = devid;
389 ratelimit_default_init(&dev_data->rs);
390
391 llist_add(&dev_data->dev_data_list, &pci_seg->dev_data_list);
392 return dev_data;
393 }
394
search_dev_data(struct amd_iommu * iommu,u16 devid)395 struct iommu_dev_data *search_dev_data(struct amd_iommu *iommu, u16 devid)
396 {
397 struct iommu_dev_data *dev_data;
398 struct llist_node *node;
399 struct amd_iommu_pci_seg *pci_seg = iommu->pci_seg;
400
401 if (llist_empty(&pci_seg->dev_data_list))
402 return NULL;
403
404 node = pci_seg->dev_data_list.first;
405 llist_for_each_entry(dev_data, node, dev_data_list) {
406 if (dev_data->devid == devid)
407 return dev_data;
408 }
409
410 return NULL;
411 }
412
clone_alias(struct pci_dev * pdev_origin,u16 alias,void * data)413 static int clone_alias(struct pci_dev *pdev_origin, u16 alias, void *data)
414 {
415 struct dev_table_entry new;
416 struct amd_iommu *iommu;
417 struct iommu_dev_data *dev_data, *alias_data;
418 struct pci_dev *pdev = data;
419 u16 devid = pci_dev_id(pdev);
420 int ret = 0;
421
422 if (devid == alias)
423 return 0;
424
425 iommu = rlookup_amd_iommu(&pdev->dev);
426 if (!iommu)
427 return 0;
428
429 /* Copy the data from pdev */
430 dev_data = dev_iommu_priv_get(&pdev->dev);
431 if (!dev_data) {
432 pr_err("%s : Failed to get dev_data for 0x%x\n", __func__, devid);
433 ret = -EINVAL;
434 goto out;
435 }
436 get_dte256(iommu, dev_data, &new);
437
438 /* Setup alias */
439 alias_data = find_dev_data(iommu, alias);
440 if (!alias_data) {
441 pr_err("%s : Failed to get alias dev_data for 0x%x\n", __func__, alias);
442 ret = -EINVAL;
443 goto out;
444 }
445 update_dte256(iommu, alias_data, &new);
446
447 amd_iommu_set_rlookup_table(iommu, alias);
448 out:
449 return ret;
450 }
451
clone_aliases(struct amd_iommu * iommu,struct device * dev)452 static void clone_aliases(struct amd_iommu *iommu, struct device *dev)
453 {
454 struct pci_dev *pdev;
455
456 if (!dev_is_pci(dev))
457 return;
458 pdev = to_pci_dev(dev);
459
460 /*
461 * The IVRS alias stored in the alias table may not be
462 * part of the PCI DMA aliases if its bus differs
463 * from the original device.
464 */
465 clone_alias(pdev, iommu->pci_seg->alias_table[pci_dev_id(pdev)], pdev);
466
467 pci_for_each_dma_alias(pdev, clone_alias, pdev);
468 }
469
setup_aliases(struct amd_iommu * iommu,struct device * dev)470 static void setup_aliases(struct amd_iommu *iommu, struct device *dev)
471 {
472 struct pci_dev *pdev = to_pci_dev(dev);
473 struct amd_iommu_pci_seg *pci_seg = iommu->pci_seg;
474 u16 ivrs_alias;
475
476 /* For ACPI HID devices, there are no aliases */
477 if (!dev_is_pci(dev))
478 return;
479
480 /*
481 * Add the IVRS alias to the pci aliases if it is on the same
482 * bus. The IVRS table may know about a quirk that we don't.
483 */
484 ivrs_alias = pci_seg->alias_table[pci_dev_id(pdev)];
485 if (ivrs_alias != pci_dev_id(pdev) &&
486 PCI_BUS_NUM(ivrs_alias) == pdev->bus->number)
487 pci_add_dma_alias(pdev, ivrs_alias & 0xff, 1);
488
489 clone_aliases(iommu, dev);
490 }
491
find_dev_data(struct amd_iommu * iommu,u16 devid)492 static struct iommu_dev_data *find_dev_data(struct amd_iommu *iommu, u16 devid)
493 {
494 struct iommu_dev_data *dev_data;
495
496 dev_data = search_dev_data(iommu, devid);
497
498 if (dev_data == NULL) {
499 dev_data = alloc_dev_data(iommu, devid);
500 if (!dev_data)
501 return NULL;
502
503 if (translation_pre_enabled(iommu))
504 dev_data->defer_attach = true;
505 }
506
507 return dev_data;
508 }
509
510 /*
511 * Find or create an IOMMU group for an acpihid device.
512 */
acpihid_device_group(struct device * dev)513 static struct iommu_group *acpihid_device_group(struct device *dev)
514 {
515 struct acpihid_map_entry *p, *entry = NULL;
516 int devid;
517
518 devid = get_acpihid_device_id(dev, &entry);
519 if (devid < 0)
520 return ERR_PTR(devid);
521
522 list_for_each_entry(p, &acpihid_map, list) {
523 if ((devid == p->devid) && p->group)
524 entry->group = p->group;
525 }
526
527 if (!entry->group)
528 entry->group = generic_device_group(dev);
529 else
530 iommu_group_ref_get(entry->group);
531
532 return entry->group;
533 }
534
pdev_pasid_supported(struct iommu_dev_data * dev_data)535 static inline bool pdev_pasid_supported(struct iommu_dev_data *dev_data)
536 {
537 return (dev_data->flags & AMD_IOMMU_DEVICE_FLAG_PASID_SUP);
538 }
539
pdev_get_caps(struct pci_dev * pdev)540 static u32 pdev_get_caps(struct pci_dev *pdev)
541 {
542 int features;
543 u32 flags = 0;
544
545 if (pci_ats_supported(pdev))
546 flags |= AMD_IOMMU_DEVICE_FLAG_ATS_SUP;
547
548 if (pci_pri_supported(pdev))
549 flags |= AMD_IOMMU_DEVICE_FLAG_PRI_SUP;
550
551 features = pci_pasid_features(pdev);
552 if (features >= 0) {
553 flags |= AMD_IOMMU_DEVICE_FLAG_PASID_SUP;
554
555 if (features & PCI_PASID_CAP_EXEC)
556 flags |= AMD_IOMMU_DEVICE_FLAG_EXEC_SUP;
557
558 if (features & PCI_PASID_CAP_PRIV)
559 flags |= AMD_IOMMU_DEVICE_FLAG_PRIV_SUP;
560 }
561
562 return flags;
563 }
564
pdev_enable_cap_ats(struct pci_dev * pdev)565 static inline int pdev_enable_cap_ats(struct pci_dev *pdev)
566 {
567 struct iommu_dev_data *dev_data = dev_iommu_priv_get(&pdev->dev);
568 int ret = -EINVAL;
569
570 if (dev_data->ats_enabled)
571 return 0;
572
573 if (amd_iommu_iotlb_sup &&
574 (dev_data->flags & AMD_IOMMU_DEVICE_FLAG_ATS_SUP)) {
575 ret = pci_enable_ats(pdev, PAGE_SHIFT);
576 if (!ret) {
577 dev_data->ats_enabled = 1;
578 dev_data->ats_qdep = pci_ats_queue_depth(pdev);
579 }
580 }
581
582 return ret;
583 }
584
pdev_disable_cap_ats(struct pci_dev * pdev)585 static inline void pdev_disable_cap_ats(struct pci_dev *pdev)
586 {
587 struct iommu_dev_data *dev_data = dev_iommu_priv_get(&pdev->dev);
588
589 if (dev_data->ats_enabled) {
590 pci_disable_ats(pdev);
591 dev_data->ats_enabled = 0;
592 }
593 }
594
pdev_enable_cap_pri(struct pci_dev * pdev)595 static inline int pdev_enable_cap_pri(struct pci_dev *pdev)
596 {
597 struct iommu_dev_data *dev_data = dev_iommu_priv_get(&pdev->dev);
598 int ret = -EINVAL;
599
600 if (dev_data->pri_enabled)
601 return 0;
602
603 if (!dev_data->ats_enabled)
604 return 0;
605
606 if (dev_data->flags & AMD_IOMMU_DEVICE_FLAG_PRI_SUP) {
607 /*
608 * First reset the PRI state of the device.
609 * FIXME: Hardcode number of outstanding requests for now
610 */
611 if (!pci_reset_pri(pdev) && !pci_enable_pri(pdev, 32)) {
612 dev_data->pri_enabled = 1;
613 dev_data->pri_tlp = pci_prg_resp_pasid_required(pdev);
614
615 ret = 0;
616 }
617 }
618
619 return ret;
620 }
621
pdev_disable_cap_pri(struct pci_dev * pdev)622 static inline void pdev_disable_cap_pri(struct pci_dev *pdev)
623 {
624 struct iommu_dev_data *dev_data = dev_iommu_priv_get(&pdev->dev);
625
626 if (dev_data->pri_enabled) {
627 pci_disable_pri(pdev);
628 dev_data->pri_enabled = 0;
629 }
630 }
631
pdev_enable_cap_pasid(struct pci_dev * pdev)632 static inline int pdev_enable_cap_pasid(struct pci_dev *pdev)
633 {
634 struct iommu_dev_data *dev_data = dev_iommu_priv_get(&pdev->dev);
635 int ret = -EINVAL;
636
637 if (dev_data->pasid_enabled)
638 return 0;
639
640 if (dev_data->flags & AMD_IOMMU_DEVICE_FLAG_PASID_SUP) {
641 /* Only allow access to user-accessible pages */
642 ret = pci_enable_pasid(pdev, 0);
643 if (!ret)
644 dev_data->pasid_enabled = 1;
645 }
646
647 return ret;
648 }
649
pdev_disable_cap_pasid(struct pci_dev * pdev)650 static inline void pdev_disable_cap_pasid(struct pci_dev *pdev)
651 {
652 struct iommu_dev_data *dev_data = dev_iommu_priv_get(&pdev->dev);
653
654 if (dev_data->pasid_enabled) {
655 pci_disable_pasid(pdev);
656 dev_data->pasid_enabled = 0;
657 }
658 }
659
pdev_enable_caps(struct pci_dev * pdev)660 static void pdev_enable_caps(struct pci_dev *pdev)
661 {
662 pdev_enable_cap_pasid(pdev);
663 pdev_enable_cap_ats(pdev);
664 pdev_enable_cap_pri(pdev);
665 }
666
pdev_disable_caps(struct pci_dev * pdev)667 static void pdev_disable_caps(struct pci_dev *pdev)
668 {
669 pdev_disable_cap_ats(pdev);
670 pdev_disable_cap_pasid(pdev);
671 pdev_disable_cap_pri(pdev);
672 }
673
674 /*
675 * This function checks if the driver got a valid device from the caller to
676 * avoid dereferencing invalid pointers.
677 */
check_device(struct device * dev)678 static bool check_device(struct device *dev)
679 {
680 struct amd_iommu_pci_seg *pci_seg;
681 struct amd_iommu *iommu;
682 int devid, sbdf;
683
684 if (!dev)
685 return false;
686
687 sbdf = get_device_sbdf_id(dev);
688 if (sbdf < 0)
689 return false;
690 devid = PCI_SBDF_TO_DEVID(sbdf);
691
692 iommu = rlookup_amd_iommu(dev);
693 if (!iommu)
694 return false;
695
696 /* Out of our scope? */
697 pci_seg = iommu->pci_seg;
698 if (devid > pci_seg->last_bdf)
699 return false;
700
701 return true;
702 }
703
iommu_init_device(struct amd_iommu * iommu,struct device * dev)704 static int iommu_init_device(struct amd_iommu *iommu, struct device *dev)
705 {
706 struct iommu_dev_data *dev_data;
707 int devid, sbdf;
708
709 if (dev_iommu_priv_get(dev))
710 return 0;
711
712 sbdf = get_device_sbdf_id(dev);
713 if (sbdf < 0)
714 return sbdf;
715
716 devid = PCI_SBDF_TO_DEVID(sbdf);
717 dev_data = find_dev_data(iommu, devid);
718 if (!dev_data)
719 return -ENOMEM;
720
721 dev_data->dev = dev;
722
723 /*
724 * The dev_iommu_priv_set() needes to be called before setup_aliases.
725 * Otherwise, subsequent call to dev_iommu_priv_get() will fail.
726 */
727 dev_iommu_priv_set(dev, dev_data);
728 setup_aliases(iommu, dev);
729
730 /*
731 * By default we use passthrough mode for IOMMUv2 capable device.
732 * But if amd_iommu=force_isolation is set (e.g. to debug DMA to
733 * invalid address), we ignore the capability for the device so
734 * it'll be forced to go into translation mode.
735 */
736 if ((iommu_default_passthrough() || !amd_iommu_force_isolation) &&
737 dev_is_pci(dev) && amd_iommu_gt_ppr_supported()) {
738 dev_data->flags = pdev_get_caps(to_pci_dev(dev));
739 }
740
741 return 0;
742 }
743
iommu_ignore_device(struct amd_iommu * iommu,struct device * dev)744 static void iommu_ignore_device(struct amd_iommu *iommu, struct device *dev)
745 {
746 struct amd_iommu_pci_seg *pci_seg = iommu->pci_seg;
747 struct dev_table_entry *dev_table = get_dev_table(iommu);
748 int devid, sbdf;
749
750 sbdf = get_device_sbdf_id(dev);
751 if (sbdf < 0)
752 return;
753
754 devid = PCI_SBDF_TO_DEVID(sbdf);
755 pci_seg->rlookup_table[devid] = NULL;
756 memset(&dev_table[devid], 0, sizeof(struct dev_table_entry));
757
758 setup_aliases(iommu, dev);
759 }
760
761
762 /****************************************************************************
763 *
764 * Interrupt handling functions
765 *
766 ****************************************************************************/
767
dump_dte_entry(struct amd_iommu * iommu,u16 devid)768 static void dump_dte_entry(struct amd_iommu *iommu, u16 devid)
769 {
770 int i;
771 struct dev_table_entry dte;
772 struct iommu_dev_data *dev_data = find_dev_data(iommu, devid);
773
774 get_dte256(iommu, dev_data, &dte);
775
776 for (i = 0; i < 4; ++i)
777 pr_err("DTE[%d]: %016llx\n", i, dte.data[i]);
778 }
779
dump_command(unsigned long phys_addr)780 static void dump_command(unsigned long phys_addr)
781 {
782 struct iommu_cmd *cmd = iommu_phys_to_virt(phys_addr);
783 int i;
784
785 for (i = 0; i < 4; ++i)
786 pr_err("CMD[%d]: %08x\n", i, cmd->data[i]);
787 }
788
amd_iommu_report_rmp_hw_error(struct amd_iommu * iommu,volatile u32 * event)789 static void amd_iommu_report_rmp_hw_error(struct amd_iommu *iommu, volatile u32 *event)
790 {
791 struct iommu_dev_data *dev_data = NULL;
792 int devid, vmg_tag, flags;
793 struct pci_dev *pdev;
794 u64 spa;
795
796 devid = (event[0] >> EVENT_DEVID_SHIFT) & EVENT_DEVID_MASK;
797 vmg_tag = (event[1]) & 0xFFFF;
798 flags = (event[1] >> EVENT_FLAGS_SHIFT) & EVENT_FLAGS_MASK;
799 spa = ((u64)event[3] << 32) | (event[2] & 0xFFFFFFF8);
800
801 pdev = pci_get_domain_bus_and_slot(iommu->pci_seg->id, PCI_BUS_NUM(devid),
802 devid & 0xff);
803 if (pdev)
804 dev_data = dev_iommu_priv_get(&pdev->dev);
805
806 if (dev_data) {
807 if (__ratelimit(&dev_data->rs)) {
808 pci_err(pdev, "Event logged [RMP_HW_ERROR vmg_tag=0x%04x, spa=0x%llx, flags=0x%04x]\n",
809 vmg_tag, spa, flags);
810 }
811 } else {
812 pr_err_ratelimited("Event logged [RMP_HW_ERROR device=%04x:%02x:%02x.%x, vmg_tag=0x%04x, spa=0x%llx, flags=0x%04x]\n",
813 iommu->pci_seg->id, PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
814 vmg_tag, spa, flags);
815 }
816
817 if (pdev)
818 pci_dev_put(pdev);
819 }
820
amd_iommu_report_rmp_fault(struct amd_iommu * iommu,volatile u32 * event)821 static void amd_iommu_report_rmp_fault(struct amd_iommu *iommu, volatile u32 *event)
822 {
823 struct iommu_dev_data *dev_data = NULL;
824 int devid, flags_rmp, vmg_tag, flags;
825 struct pci_dev *pdev;
826 u64 gpa;
827
828 devid = (event[0] >> EVENT_DEVID_SHIFT) & EVENT_DEVID_MASK;
829 flags_rmp = (event[0] >> EVENT_FLAGS_SHIFT) & 0xFF;
830 vmg_tag = (event[1]) & 0xFFFF;
831 flags = (event[1] >> EVENT_FLAGS_SHIFT) & EVENT_FLAGS_MASK;
832 gpa = ((u64)event[3] << 32) | event[2];
833
834 pdev = pci_get_domain_bus_and_slot(iommu->pci_seg->id, PCI_BUS_NUM(devid),
835 devid & 0xff);
836 if (pdev)
837 dev_data = dev_iommu_priv_get(&pdev->dev);
838
839 if (dev_data) {
840 if (__ratelimit(&dev_data->rs)) {
841 pci_err(pdev, "Event logged [RMP_PAGE_FAULT vmg_tag=0x%04x, gpa=0x%llx, flags_rmp=0x%04x, flags=0x%04x]\n",
842 vmg_tag, gpa, flags_rmp, flags);
843 }
844 } else {
845 pr_err_ratelimited("Event logged [RMP_PAGE_FAULT device=%04x:%02x:%02x.%x, vmg_tag=0x%04x, gpa=0x%llx, flags_rmp=0x%04x, flags=0x%04x]\n",
846 iommu->pci_seg->id, PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
847 vmg_tag, gpa, flags_rmp, flags);
848 }
849
850 if (pdev)
851 pci_dev_put(pdev);
852 }
853
854 #define IS_IOMMU_MEM_TRANSACTION(flags) \
855 (((flags) & EVENT_FLAG_I) == 0)
856
857 #define IS_WRITE_REQUEST(flags) \
858 ((flags) & EVENT_FLAG_RW)
859
amd_iommu_report_page_fault(struct amd_iommu * iommu,u16 devid,u16 domain_id,u64 address,int flags)860 static void amd_iommu_report_page_fault(struct amd_iommu *iommu,
861 u16 devid, u16 domain_id,
862 u64 address, int flags)
863 {
864 struct iommu_dev_data *dev_data = NULL;
865 struct pci_dev *pdev;
866
867 pdev = pci_get_domain_bus_and_slot(iommu->pci_seg->id, PCI_BUS_NUM(devid),
868 devid & 0xff);
869 if (pdev)
870 dev_data = dev_iommu_priv_get(&pdev->dev);
871
872 if (dev_data) {
873 /*
874 * If this is a DMA fault (for which the I(nterrupt)
875 * bit will be unset), allow report_iommu_fault() to
876 * prevent logging it.
877 */
878 if (IS_IOMMU_MEM_TRANSACTION(flags)) {
879 /* Device not attached to domain properly */
880 if (dev_data->domain == NULL) {
881 pr_err_ratelimited("Event logged [Device not attached to domain properly]\n");
882 pr_err_ratelimited(" device=%04x:%02x:%02x.%x domain=0x%04x\n",
883 iommu->pci_seg->id, PCI_BUS_NUM(devid), PCI_SLOT(devid),
884 PCI_FUNC(devid), domain_id);
885 goto out;
886 }
887
888 if (!report_iommu_fault(&dev_data->domain->domain,
889 &pdev->dev, address,
890 IS_WRITE_REQUEST(flags) ?
891 IOMMU_FAULT_WRITE :
892 IOMMU_FAULT_READ))
893 goto out;
894 }
895
896 if (__ratelimit(&dev_data->rs)) {
897 pci_err(pdev, "Event logged [IO_PAGE_FAULT domain=0x%04x address=0x%llx flags=0x%04x]\n",
898 domain_id, address, flags);
899 }
900 } else {
901 pr_err_ratelimited("Event logged [IO_PAGE_FAULT device=%04x:%02x:%02x.%x domain=0x%04x address=0x%llx flags=0x%04x]\n",
902 iommu->pci_seg->id, PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
903 domain_id, address, flags);
904 }
905
906 out:
907 if (pdev)
908 pci_dev_put(pdev);
909 }
910
amd_iommu_report_ppr_err(struct amd_iommu * iommu,volatile u32 * event,u16 devid,u64 address,int flags)911 static void amd_iommu_report_ppr_err(struct amd_iommu *iommu, volatile u32 *event,
912 u16 devid, u64 address, int flags)
913 {
914 struct pci_dev *pdev;
915 struct device *dev = iommu->iommu.dev;
916 u32 pasid = PPR_PASID(*((u64 *)event));
917 int tag = event[1] & 0x03FF;
918 bool gn;
919
920 dev_err_ratelimited(dev, "Event logged [INVALID_PPR_REQUEST device=%04x:%02x:%02x.%x "
921 "pasid=0x%05x address=0x%llx flags=0x%04x tag=0x%03x]\n",
922 iommu->pci_seg->id, PCI_BUS_NUM(devid), PCI_SLOT(devid),
923 PCI_FUNC(devid), pasid, address, flags, tag);
924
925 /* Skip COMPLETE_PPR_REQUEST response if RX=1 */
926 if (flags & EVENT_FLAG_PPR_RX)
927 return;
928
929 pdev = pci_get_domain_bus_and_slot(iommu->pci_seg->id, PCI_BUS_NUM(devid),
930 devid & 0xff);
931 if (!pdev)
932 return;
933
934 if (!dev_iommu_priv_get(&pdev->dev)) {
935 pci_dev_put(pdev);
936 return;
937 }
938
939 gn = (flags & EVENT_FLAG_PPR_GN);
940
941 __amd_iommu_complete_ppr(&pdev->dev, pasid, IOMMU_PAGE_RESP_FAILURE, tag, gn);
942 pci_dev_put(pdev);
943 }
944
iommu_print_event(struct amd_iommu * iommu,void * __evt)945 static void iommu_print_event(struct amd_iommu *iommu, void *__evt)
946 {
947 struct device *dev = iommu->iommu.dev;
948 int type, devid, flags;
949 volatile u32 *event = __evt;
950 int count = 0;
951 u64 address, ctrl;
952 u32 pasid;
953
954 retry:
955 type = (event[1] >> EVENT_TYPE_SHIFT) & EVENT_TYPE_MASK;
956 devid = (event[0] >> EVENT_DEVID_SHIFT) & EVENT_DEVID_MASK;
957 pasid = (event[0] & EVENT_DOMID_MASK_HI) |
958 (event[1] & EVENT_DOMID_MASK_LO);
959 flags = (event[1] >> EVENT_FLAGS_SHIFT) & EVENT_FLAGS_MASK;
960 address = (u64)(((u64)event[3]) << 32) | event[2];
961 ctrl = readq(iommu->mmio_base + MMIO_CONTROL_OFFSET);
962
963 if (type == 0) {
964 /* Did we hit the erratum? */
965 if (++count == LOOP_TIMEOUT) {
966 pr_err("No event written to event log\n");
967 return;
968 }
969 udelay(1);
970 goto retry;
971 }
972
973 if (type == EVENT_TYPE_IO_FAULT) {
974 amd_iommu_report_page_fault(iommu, devid, pasid, address, flags);
975 return;
976 }
977
978 switch (type) {
979 case EVENT_TYPE_ILL_DEV:
980 dev_err(dev, "Event logged [ILLEGAL_DEV_TABLE_ENTRY device=%04x:%02x:%02x.%x pasid=0x%05x address=0x%llx flags=0x%04x]\n",
981 iommu->pci_seg->id, PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
982 pasid, address, flags);
983 dev_err(dev, "Control Reg : 0x%llx\n", ctrl);
984 dump_dte_entry(iommu, devid);
985 break;
986 case EVENT_TYPE_DEV_TAB_ERR:
987 dev_err(dev, "Event logged [DEV_TAB_HARDWARE_ERROR device=%04x:%02x:%02x.%x "
988 "address=0x%llx flags=0x%04x]\n",
989 iommu->pci_seg->id, PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
990 address, flags);
991 break;
992 case EVENT_TYPE_PAGE_TAB_ERR:
993 dev_err(dev, "Event logged [PAGE_TAB_HARDWARE_ERROR device=%04x:%02x:%02x.%x pasid=0x%04x address=0x%llx flags=0x%04x]\n",
994 iommu->pci_seg->id, PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
995 pasid, address, flags);
996 break;
997 case EVENT_TYPE_ILL_CMD:
998 dev_err(dev, "Event logged [ILLEGAL_COMMAND_ERROR address=0x%llx]\n", address);
999 dump_command(address);
1000 break;
1001 case EVENT_TYPE_CMD_HARD_ERR:
1002 dev_err(dev, "Event logged [COMMAND_HARDWARE_ERROR address=0x%llx flags=0x%04x]\n",
1003 address, flags);
1004 break;
1005 case EVENT_TYPE_IOTLB_INV_TO:
1006 dev_err(dev, "Event logged [IOTLB_INV_TIMEOUT device=%04x:%02x:%02x.%x address=0x%llx]\n",
1007 iommu->pci_seg->id, PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
1008 address);
1009 break;
1010 case EVENT_TYPE_INV_DEV_REQ:
1011 dev_err(dev, "Event logged [INVALID_DEVICE_REQUEST device=%04x:%02x:%02x.%x pasid=0x%05x address=0x%llx flags=0x%04x]\n",
1012 iommu->pci_seg->id, PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
1013 pasid, address, flags);
1014 break;
1015 case EVENT_TYPE_RMP_FAULT:
1016 amd_iommu_report_rmp_fault(iommu, event);
1017 break;
1018 case EVENT_TYPE_RMP_HW_ERR:
1019 amd_iommu_report_rmp_hw_error(iommu, event);
1020 break;
1021 case EVENT_TYPE_INV_PPR_REQ:
1022 amd_iommu_report_ppr_err(iommu, event, devid, address, flags);
1023 break;
1024 default:
1025 dev_err(dev, "Event logged [UNKNOWN event[0]=0x%08x event[1]=0x%08x event[2]=0x%08x event[3]=0x%08x\n",
1026 event[0], event[1], event[2], event[3]);
1027 }
1028
1029 /*
1030 * To detect the hardware errata 732 we need to clear the
1031 * entry back to zero. This issue does not exist on SNP
1032 * enabled system. Also this buffer is not writeable on
1033 * SNP enabled system.
1034 */
1035 if (!amd_iommu_snp_en)
1036 memset(__evt, 0, 4 * sizeof(u32));
1037 }
1038
iommu_poll_events(struct amd_iommu * iommu)1039 static void iommu_poll_events(struct amd_iommu *iommu)
1040 {
1041 u32 head, tail;
1042
1043 head = readl(iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
1044 tail = readl(iommu->mmio_base + MMIO_EVT_TAIL_OFFSET);
1045
1046 while (head != tail) {
1047 iommu_print_event(iommu, iommu->evt_buf + head);
1048
1049 /* Update head pointer of hardware ring-buffer */
1050 head = (head + EVTLOG_ENTRY_SIZE) % amd_iommu_evtlog_size;
1051 writel(head, iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
1052 }
1053
1054 }
1055
1056 #ifdef CONFIG_IRQ_REMAP
1057 static int (*iommu_ga_log_notifier)(u32);
1058
amd_iommu_register_ga_log_notifier(int (* notifier)(u32))1059 int amd_iommu_register_ga_log_notifier(int (*notifier)(u32))
1060 {
1061 iommu_ga_log_notifier = notifier;
1062
1063 /*
1064 * Ensure all in-flight IRQ handlers run to completion before returning
1065 * to the caller, e.g. to ensure module code isn't unloaded while it's
1066 * being executed in the IRQ handler.
1067 */
1068 if (!notifier)
1069 synchronize_rcu();
1070
1071 return 0;
1072 }
1073 EXPORT_SYMBOL(amd_iommu_register_ga_log_notifier);
1074
iommu_poll_ga_log(struct amd_iommu * iommu)1075 static void iommu_poll_ga_log(struct amd_iommu *iommu)
1076 {
1077 u32 head, tail;
1078
1079 head = readl(iommu->mmio_base + MMIO_GA_HEAD_OFFSET);
1080 tail = readl(iommu->mmio_base + MMIO_GA_TAIL_OFFSET);
1081
1082 while (head != tail) {
1083 volatile u64 *raw;
1084 u64 log_entry;
1085
1086 raw = (u64 *)(iommu->ga_log + head);
1087
1088 /* Avoid memcpy function-call overhead */
1089 log_entry = *raw;
1090
1091 /* Update head pointer of hardware ring-buffer */
1092 head = (head + GA_ENTRY_SIZE) % GA_LOG_SIZE;
1093 writel(head, iommu->mmio_base + MMIO_GA_HEAD_OFFSET);
1094
1095 /* Handle GA entry */
1096 switch (GA_REQ_TYPE(log_entry)) {
1097 case GA_GUEST_NR:
1098 if (!iommu_ga_log_notifier)
1099 break;
1100
1101 pr_debug("%s: devid=%#x, ga_tag=%#x\n",
1102 __func__, GA_DEVID(log_entry),
1103 GA_TAG(log_entry));
1104
1105 if (iommu_ga_log_notifier(GA_TAG(log_entry)) != 0)
1106 pr_err("GA log notifier failed.\n");
1107 break;
1108 default:
1109 break;
1110 }
1111 }
1112 }
1113
1114 static void
amd_iommu_set_pci_msi_domain(struct device * dev,struct amd_iommu * iommu)1115 amd_iommu_set_pci_msi_domain(struct device *dev, struct amd_iommu *iommu)
1116 {
1117 if (!irq_remapping_enabled || !dev_is_pci(dev) ||
1118 !pci_dev_has_default_msi_parent_domain(to_pci_dev(dev)))
1119 return;
1120
1121 dev_set_msi_domain(dev, iommu->ir_domain);
1122 }
1123
1124 #else /* CONFIG_IRQ_REMAP */
1125 static inline void
amd_iommu_set_pci_msi_domain(struct device * dev,struct amd_iommu * iommu)1126 amd_iommu_set_pci_msi_domain(struct device *dev, struct amd_iommu *iommu) { }
1127 #endif /* !CONFIG_IRQ_REMAP */
1128
amd_iommu_handle_irq(void * data,const char * evt_type,u32 int_mask,u32 overflow_mask,void (* int_handler)(struct amd_iommu *),void (* overflow_handler)(struct amd_iommu *))1129 static void amd_iommu_handle_irq(void *data, const char *evt_type,
1130 u32 int_mask, u32 overflow_mask,
1131 void (*int_handler)(struct amd_iommu *),
1132 void (*overflow_handler)(struct amd_iommu *))
1133 {
1134 struct amd_iommu *iommu = (struct amd_iommu *) data;
1135 u32 status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
1136 u32 mask = int_mask | overflow_mask;
1137
1138 while (status & mask) {
1139 /* Enable interrupt sources again */
1140 writel(mask, iommu->mmio_base + MMIO_STATUS_OFFSET);
1141
1142 if (int_handler) {
1143 pr_devel("Processing IOMMU (ivhd%d) %s Log\n",
1144 iommu->index, evt_type);
1145 int_handler(iommu);
1146 }
1147
1148 if ((status & overflow_mask) && overflow_handler)
1149 overflow_handler(iommu);
1150
1151 /*
1152 * Hardware bug: ERBT1312
1153 * When re-enabling interrupt (by writing 1
1154 * to clear the bit), the hardware might also try to set
1155 * the interrupt bit in the event status register.
1156 * In this scenario, the bit will be set, and disable
1157 * subsequent interrupts.
1158 *
1159 * Workaround: The IOMMU driver should read back the
1160 * status register and check if the interrupt bits are cleared.
1161 * If not, driver will need to go through the interrupt handler
1162 * again and re-clear the bits
1163 */
1164 status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
1165 }
1166 }
1167
amd_iommu_int_thread_evtlog(int irq,void * data)1168 irqreturn_t amd_iommu_int_thread_evtlog(int irq, void *data)
1169 {
1170 amd_iommu_handle_irq(data, "Evt", MMIO_STATUS_EVT_INT_MASK,
1171 MMIO_STATUS_EVT_OVERFLOW_MASK,
1172 iommu_poll_events, amd_iommu_restart_event_logging);
1173
1174 return IRQ_HANDLED;
1175 }
1176
amd_iommu_int_thread_pprlog(int irq,void * data)1177 irqreturn_t amd_iommu_int_thread_pprlog(int irq, void *data)
1178 {
1179 amd_iommu_handle_irq(data, "PPR", MMIO_STATUS_PPR_INT_MASK,
1180 MMIO_STATUS_PPR_OVERFLOW_MASK,
1181 amd_iommu_poll_ppr_log, amd_iommu_restart_ppr_log);
1182
1183 return IRQ_HANDLED;
1184 }
1185
amd_iommu_int_thread_galog(int irq,void * data)1186 irqreturn_t amd_iommu_int_thread_galog(int irq, void *data)
1187 {
1188 #ifdef CONFIG_IRQ_REMAP
1189 amd_iommu_handle_irq(data, "GA", MMIO_STATUS_GALOG_INT_MASK,
1190 MMIO_STATUS_GALOG_OVERFLOW_MASK,
1191 iommu_poll_ga_log, amd_iommu_restart_ga_log);
1192 #endif
1193
1194 return IRQ_HANDLED;
1195 }
1196
amd_iommu_int_thread(int irq,void * data)1197 irqreturn_t amd_iommu_int_thread(int irq, void *data)
1198 {
1199 amd_iommu_int_thread_evtlog(irq, data);
1200 amd_iommu_int_thread_pprlog(irq, data);
1201 amd_iommu_int_thread_galog(irq, data);
1202
1203 return IRQ_HANDLED;
1204 }
1205
1206 /****************************************************************************
1207 *
1208 * IOMMU command queuing functions
1209 *
1210 ****************************************************************************/
1211
dump_command_buffer(struct amd_iommu * iommu)1212 static void dump_command_buffer(struct amd_iommu *iommu)
1213 {
1214 struct iommu_cmd *cmd;
1215 u32 head, tail;
1216 int i;
1217
1218 head = readl(iommu->mmio_base + MMIO_CMD_HEAD_OFFSET);
1219 tail = readl(iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
1220
1221 pr_err("CMD Buffer head=%llu tail=%llu\n", MMIO_CMD_BUFFER_HEAD(head),
1222 MMIO_CMD_BUFFER_TAIL(tail));
1223
1224 for (i = 0; i < CMD_BUFFER_ENTRIES; i++) {
1225 cmd = (struct iommu_cmd *)(iommu->cmd_buf + i * sizeof(*cmd));
1226 pr_err("%3d: %08x %08x %08x %08x\n", i, cmd->data[0], cmd->data[1], cmd->data[2],
1227 cmd->data[3]);
1228 }
1229 }
1230
wait_on_sem(struct amd_iommu * iommu,u64 data)1231 static int wait_on_sem(struct amd_iommu *iommu, u64 data)
1232 {
1233 int i = 0;
1234
1235 /*
1236 * cmd_sem holds a monotonically non-decreasing completion sequence
1237 * number.
1238 */
1239 while ((__s64)(READ_ONCE(*iommu->cmd_sem) - data) < 0 &&
1240 i < LOOP_TIMEOUT) {
1241 udelay(1);
1242 i += 1;
1243 }
1244
1245 if (i == LOOP_TIMEOUT) {
1246
1247 pr_alert("IOMMU %04x:%02x:%02x.%01x: Completion-Wait loop timed out\n",
1248 iommu->pci_seg->id, PCI_BUS_NUM(iommu->devid),
1249 PCI_SLOT(iommu->devid), PCI_FUNC(iommu->devid));
1250
1251 if (amd_iommu_dump)
1252 DO_ONCE_LITE(dump_command_buffer, iommu);
1253
1254 return -EIO;
1255 }
1256
1257 return 0;
1258 }
1259
copy_cmd_to_buffer(struct amd_iommu * iommu,struct iommu_cmd * cmd)1260 static void copy_cmd_to_buffer(struct amd_iommu *iommu,
1261 struct iommu_cmd *cmd)
1262 {
1263 u8 *target;
1264 u32 tail;
1265
1266 /* Copy command to buffer */
1267 tail = iommu->cmd_buf_tail;
1268 target = iommu->cmd_buf + tail;
1269 memcpy(target, cmd, sizeof(*cmd));
1270
1271 tail = (tail + sizeof(*cmd)) % CMD_BUFFER_SIZE;
1272 iommu->cmd_buf_tail = tail;
1273
1274 /* Tell the IOMMU about it */
1275 writel(tail, iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
1276 }
1277
build_completion_wait(struct iommu_cmd * cmd,struct amd_iommu * iommu,u64 data)1278 static void build_completion_wait(struct iommu_cmd *cmd,
1279 struct amd_iommu *iommu,
1280 u64 data)
1281 {
1282 u64 paddr = iommu->cmd_sem_paddr;
1283
1284 memset(cmd, 0, sizeof(*cmd));
1285 cmd->data[0] = lower_32_bits(paddr) | CMD_COMPL_WAIT_STORE_MASK;
1286 cmd->data[1] = upper_32_bits(paddr);
1287 cmd->data[2] = lower_32_bits(data);
1288 cmd->data[3] = upper_32_bits(data);
1289 CMD_SET_TYPE(cmd, CMD_COMPL_WAIT);
1290 }
1291
build_inv_dte(struct iommu_cmd * cmd,u16 devid)1292 static void build_inv_dte(struct iommu_cmd *cmd, u16 devid)
1293 {
1294 memset(cmd, 0, sizeof(*cmd));
1295 cmd->data[0] = devid;
1296 CMD_SET_TYPE(cmd, CMD_INV_DEV_ENTRY);
1297 }
1298
1299 /*
1300 * Builds an invalidation address which is suitable for one page or multiple
1301 * pages. Sets the size bit (S) as needed if more than one page is flushed.
1302 */
build_inv_address(u64 address,u64 last)1303 static inline u64 build_inv_address(u64 address, u64 last)
1304 {
1305 unsigned int sz_lg2;
1306
1307 address &= GENMASK_U64(63, 12);
1308 sz_lg2 = fls64(address ^ last);
1309 if (sz_lg2 <= 12)
1310 return address;
1311
1312 /*
1313 * Encode sz_lg2 according to Table 14: Example Page Size Encodings
1314 *
1315 * See "Note *":
1316 * Address bits 51:32 can be used to encode page sizes greater
1317 * that 4 Gbytes.
1318 * Which we take to mean that the highest page size has bit
1319 * [51]=0, [50:12]=1
1320 * and that coding happens when sz_lg2 is 52. Fall back to full
1321 * invalidation if the size is too big.
1322 *
1323 */
1324 if (unlikely(sz_lg2 > 52))
1325 return CMD_INV_IOMMU_ALL_PAGES_ADDRESS |
1326 CMD_INV_IOMMU_PAGES_SIZE_MASK;
1327
1328 /*
1329 * The sz_lg2 calculation with fls() ensures that:
1330 * address & BIT(sz_lg2 - 1) == 0
1331 * Therefore only the 1's need to be added. 8KB requires no 1's
1332 */
1333 if (sz_lg2 > 13)
1334 address |= GENMASK_U64(sz_lg2 - 2, 12);
1335 return address | CMD_INV_IOMMU_PAGES_SIZE_MASK;
1336 }
1337
build_inv_iommu_pages(struct iommu_cmd * cmd,u64 address,u64 last,u16 domid,ioasid_t pasid,u32 flags)1338 static void build_inv_iommu_pages(struct iommu_cmd *cmd, u64 address,
1339 u64 last, u16 domid, ioasid_t pasid,
1340 u32 flags)
1341 {
1342 u64 inv_address = build_inv_address(address, last);
1343
1344 memset(cmd, 0, sizeof(*cmd));
1345
1346 cmd->data[1] |= domid;
1347 cmd->data[2] = lower_32_bits(inv_address);
1348 cmd->data[3] = upper_32_bits(inv_address);
1349 cmd->data[2] |= flags;
1350 if (flags & CMD_INV_IOMMU_PAGES_GN_MASK)
1351 cmd->data[0] |= pasid;
1352 CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES);
1353 }
1354
build_inv_iotlb_pages(struct iommu_cmd * cmd,u16 devid,int qdep,u64 address,u64 last,ioasid_t pasid,bool gn)1355 static void build_inv_iotlb_pages(struct iommu_cmd *cmd, u16 devid, int qdep,
1356 u64 address, u64 last,
1357 ioasid_t pasid, bool gn)
1358 {
1359 u64 inv_address = build_inv_address(address, last);
1360
1361 memset(cmd, 0, sizeof(*cmd));
1362
1363 cmd->data[0] = devid;
1364 cmd->data[0] |= (qdep & 0xff) << 24;
1365 cmd->data[1] = devid;
1366 cmd->data[2] = lower_32_bits(inv_address);
1367 cmd->data[3] = upper_32_bits(inv_address);
1368 if (gn) {
1369 cmd->data[0] |= ((pasid >> 8) & 0xff) << 16;
1370 cmd->data[1] |= (pasid & 0xff) << 16;
1371 cmd->data[2] |= CMD_INV_IOMMU_PAGES_GN_MASK;
1372 }
1373
1374 CMD_SET_TYPE(cmd, CMD_INV_IOTLB_PAGES);
1375 }
1376
build_complete_ppr(struct iommu_cmd * cmd,u16 devid,u32 pasid,int status,int tag,bool gn)1377 static void build_complete_ppr(struct iommu_cmd *cmd, u16 devid, u32 pasid,
1378 int status, int tag, bool gn)
1379 {
1380 memset(cmd, 0, sizeof(*cmd));
1381
1382 cmd->data[0] = devid;
1383 if (gn) {
1384 cmd->data[1] = pasid;
1385 cmd->data[2] = CMD_INV_IOMMU_PAGES_GN_MASK;
1386 }
1387 cmd->data[3] = tag & 0x1ff;
1388 cmd->data[3] |= (status & PPR_STATUS_MASK) << PPR_STATUS_SHIFT;
1389
1390 CMD_SET_TYPE(cmd, CMD_COMPLETE_PPR);
1391 }
1392
build_inv_all(struct iommu_cmd * cmd)1393 static void build_inv_all(struct iommu_cmd *cmd)
1394 {
1395 memset(cmd, 0, sizeof(*cmd));
1396 CMD_SET_TYPE(cmd, CMD_INV_ALL);
1397 }
1398
build_inv_irt(struct iommu_cmd * cmd,u16 devid)1399 static void build_inv_irt(struct iommu_cmd *cmd, u16 devid)
1400 {
1401 memset(cmd, 0, sizeof(*cmd));
1402 cmd->data[0] = devid;
1403 CMD_SET_TYPE(cmd, CMD_INV_IRT);
1404 }
1405
1406 /*
1407 * Writes the command to the IOMMUs command buffer and informs the
1408 * hardware about the new command.
1409 */
__iommu_queue_command_sync(struct amd_iommu * iommu,struct iommu_cmd * cmd,bool sync)1410 static int __iommu_queue_command_sync(struct amd_iommu *iommu,
1411 struct iommu_cmd *cmd,
1412 bool sync)
1413 {
1414 unsigned int count = 0;
1415 u32 left, next_tail;
1416
1417 next_tail = (iommu->cmd_buf_tail + sizeof(*cmd)) % CMD_BUFFER_SIZE;
1418 again:
1419 left = (iommu->cmd_buf_head - next_tail) % CMD_BUFFER_SIZE;
1420
1421 if (left <= 0x20) {
1422 /* Skip udelay() the first time around */
1423 if (count++) {
1424 if (count == LOOP_TIMEOUT) {
1425 pr_err("Command buffer timeout\n");
1426 return -EIO;
1427 }
1428
1429 udelay(1);
1430 }
1431
1432 /* Update head and recheck remaining space */
1433 iommu->cmd_buf_head = readl(iommu->mmio_base +
1434 MMIO_CMD_HEAD_OFFSET);
1435
1436 goto again;
1437 }
1438
1439 copy_cmd_to_buffer(iommu, cmd);
1440
1441 /* Do we need to make sure all commands are processed? */
1442 iommu->need_sync = sync;
1443
1444 return 0;
1445 }
1446
iommu_queue_command_sync(struct amd_iommu * iommu,struct iommu_cmd * cmd,bool sync)1447 static int iommu_queue_command_sync(struct amd_iommu *iommu,
1448 struct iommu_cmd *cmd,
1449 bool sync)
1450 {
1451 unsigned long flags;
1452 int ret;
1453
1454 raw_spin_lock_irqsave(&iommu->lock, flags);
1455 ret = __iommu_queue_command_sync(iommu, cmd, sync);
1456 raw_spin_unlock_irqrestore(&iommu->lock, flags);
1457
1458 return ret;
1459 }
1460
iommu_queue_command(struct amd_iommu * iommu,struct iommu_cmd * cmd)1461 static int iommu_queue_command(struct amd_iommu *iommu, struct iommu_cmd *cmd)
1462 {
1463 return iommu_queue_command_sync(iommu, cmd, true);
1464 }
1465
get_cmdsem_val(struct amd_iommu * iommu)1466 static u64 get_cmdsem_val(struct amd_iommu *iommu)
1467 {
1468 lockdep_assert_held(&iommu->lock);
1469 return ++iommu->cmd_sem_val;
1470 }
1471
1472 /*
1473 * This function queues a completion wait command into the command
1474 * buffer of an IOMMU
1475 */
iommu_completion_wait(struct amd_iommu * iommu)1476 static int iommu_completion_wait(struct amd_iommu *iommu)
1477 {
1478 struct iommu_cmd cmd;
1479 unsigned long flags;
1480 int ret;
1481 u64 data;
1482
1483 raw_spin_lock_irqsave(&iommu->lock, flags);
1484
1485 if (!iommu->need_sync) {
1486 /*
1487 * No command has been queued since the last completion-wait.
1488 * A concurrent CPU may have already queued that CWAIT and
1489 * cleared need_sync; need_sync == false only means a covering
1490 * CWAIT is queued, not that all prior commands have completed.
1491 * Wait for the last allocated sequence number so that any
1492 * command queued before this call (possibly on another CPU)
1493 * is guaranteed to have completed before returning.
1494 */
1495 data = iommu->cmd_sem_val;
1496 raw_spin_unlock_irqrestore(&iommu->lock, flags);
1497 return wait_on_sem(iommu, data);
1498 }
1499
1500 data = get_cmdsem_val(iommu);
1501 build_completion_wait(&cmd, iommu, data);
1502
1503 ret = __iommu_queue_command_sync(iommu, &cmd, false);
1504 raw_spin_unlock_irqrestore(&iommu->lock, flags);
1505
1506 if (ret)
1507 return ret;
1508
1509 return wait_on_sem(iommu, data);
1510 }
1511
domain_flush_complete(struct protection_domain * domain)1512 static void domain_flush_complete(struct protection_domain *domain)
1513 {
1514 struct pdom_iommu_info *pdom_iommu_info;
1515 unsigned long i;
1516
1517 lockdep_assert_held(&domain->lock);
1518
1519 /*
1520 * Devices of this domain are behind this IOMMU
1521 * We need to wait for completion of all commands.
1522 */
1523 xa_for_each(&domain->iommu_array, i, pdom_iommu_info)
1524 iommu_completion_wait(pdom_iommu_info->iommu);
1525 }
1526
iommu_flush_dte(struct amd_iommu * iommu,u16 devid)1527 static int iommu_flush_dte(struct amd_iommu *iommu, u16 devid)
1528 {
1529 struct iommu_cmd cmd;
1530
1531 build_inv_dte(&cmd, devid);
1532
1533 return iommu_queue_command(iommu, &cmd);
1534 }
1535
iommu_flush_dte_sync(struct amd_iommu * iommu,u16 devid)1536 static void iommu_flush_dte_sync(struct amd_iommu *iommu, u16 devid)
1537 {
1538 int ret;
1539
1540 ret = iommu_flush_dte(iommu, devid);
1541 if (!ret)
1542 iommu_completion_wait(iommu);
1543 }
1544
amd_iommu_flush_dte_all(struct amd_iommu * iommu)1545 static void amd_iommu_flush_dte_all(struct amd_iommu *iommu)
1546 {
1547 u32 devid;
1548 u16 last_bdf = iommu->pci_seg->last_bdf;
1549
1550 for (devid = 0; devid <= last_bdf; ++devid)
1551 iommu_flush_dte(iommu, devid);
1552
1553 iommu_completion_wait(iommu);
1554 }
1555
1556 /*
1557 * This function uses heavy locking and may disable irqs for some time. But
1558 * this is no issue because it is only called during resume.
1559 */
amd_iommu_flush_tlb_all(struct amd_iommu * iommu)1560 static void amd_iommu_flush_tlb_all(struct amd_iommu *iommu)
1561 {
1562 u32 dom_id;
1563 u16 last_bdf = iommu->pci_seg->last_bdf;
1564
1565 for (dom_id = 0; dom_id <= last_bdf; ++dom_id) {
1566 struct iommu_cmd cmd;
1567 build_inv_iommu_pages(&cmd, 0, U64_MAX,
1568 dom_id, IOMMU_NO_PASID,
1569 CMD_INV_IOMMU_PAGES_PDE_MASK);
1570 iommu_queue_command(iommu, &cmd);
1571 }
1572
1573 iommu_completion_wait(iommu);
1574 }
1575
amd_iommu_flush_tlb_domid(struct amd_iommu * iommu,u32 dom_id)1576 static void amd_iommu_flush_tlb_domid(struct amd_iommu *iommu, u32 dom_id)
1577 {
1578 struct iommu_cmd cmd;
1579
1580 build_inv_iommu_pages(&cmd, 0, U64_MAX,
1581 dom_id, IOMMU_NO_PASID,
1582 CMD_INV_IOMMU_PAGES_PDE_MASK);
1583 iommu_queue_command(iommu, &cmd);
1584
1585 iommu_completion_wait(iommu);
1586 }
1587
iommu_flush_pages_v1_hdom_ids(struct protection_domain * pdom,u64 address,u64 last,u32 flags)1588 static int iommu_flush_pages_v1_hdom_ids(struct protection_domain *pdom,
1589 u64 address, u64 last, u32 flags)
1590 {
1591 int ret = 0;
1592 struct amd_iommu_viommu *aviommu;
1593
1594 list_for_each_entry(aviommu, &pdom->viommu_list, pdom_list) {
1595 unsigned long i;
1596 struct guest_domain_mapping_info *gdom_info;
1597 struct amd_iommu *iommu = container_of(aviommu->core.iommu_dev,
1598 struct amd_iommu, iommu);
1599
1600 xa_lock(&aviommu->gdomid_array);
1601 xa_for_each(&aviommu->gdomid_array, i, gdom_info) {
1602 struct iommu_cmd cmd;
1603
1604 pr_debug("%s: iommu=%#x, hdom_id=%#x\n", __func__,
1605 iommu->devid, gdom_info->hdom_id);
1606 build_inv_iommu_pages(&cmd, address, last, gdom_info->hdom_id,
1607 IOMMU_NO_PASID, flags);
1608 ret |= iommu_queue_command(iommu, &cmd);
1609 }
1610 xa_unlock(&aviommu->gdomid_array);
1611 }
1612 return ret;
1613 }
1614
amd_iommu_flush_all(struct amd_iommu * iommu)1615 static void amd_iommu_flush_all(struct amd_iommu *iommu)
1616 {
1617 struct iommu_cmd cmd;
1618
1619 build_inv_all(&cmd);
1620
1621 iommu_queue_command(iommu, &cmd);
1622 iommu_completion_wait(iommu);
1623 }
1624
iommu_flush_irt(struct amd_iommu * iommu,u16 devid)1625 static void iommu_flush_irt(struct amd_iommu *iommu, u16 devid)
1626 {
1627 struct iommu_cmd cmd;
1628
1629 build_inv_irt(&cmd, devid);
1630
1631 iommu_queue_command(iommu, &cmd);
1632 }
1633
amd_iommu_flush_irt_all(struct amd_iommu * iommu)1634 static void amd_iommu_flush_irt_all(struct amd_iommu *iommu)
1635 {
1636 u32 devid;
1637 u16 last_bdf = iommu->pci_seg->last_bdf;
1638
1639 if (iommu->irtcachedis_enabled)
1640 return;
1641
1642 for (devid = 0; devid <= last_bdf; devid++)
1643 iommu_flush_irt(iommu, devid);
1644
1645 iommu_completion_wait(iommu);
1646 }
1647
amd_iommu_flush_all_caches(struct amd_iommu * iommu)1648 void amd_iommu_flush_all_caches(struct amd_iommu *iommu)
1649 {
1650 if (check_feature(FEATURE_IA)) {
1651 amd_iommu_flush_all(iommu);
1652 } else {
1653 amd_iommu_flush_dte_all(iommu);
1654 amd_iommu_flush_irt_all(iommu);
1655 amd_iommu_flush_tlb_all(iommu);
1656 }
1657 }
1658
1659 /*
1660 * Command send function for flushing on-device TLB
1661 */
device_flush_iotlb(struct iommu_dev_data * dev_data,u64 address,u64 last,ioasid_t pasid,bool gn)1662 static int device_flush_iotlb(struct iommu_dev_data *dev_data, u64 address,
1663 u64 last, ioasid_t pasid, bool gn)
1664 {
1665 struct amd_iommu *iommu = get_amd_iommu_from_dev_data(dev_data);
1666 struct iommu_cmd cmd;
1667 int qdep = dev_data->ats_qdep;
1668
1669 build_inv_iotlb_pages(&cmd, dev_data->devid, qdep, address,
1670 last, pasid, gn);
1671
1672 return iommu_queue_command(iommu, &cmd);
1673 }
1674
device_flush_dte_alias(struct pci_dev * pdev,u16 alias,void * data)1675 static int device_flush_dte_alias(struct pci_dev *pdev, u16 alias, void *data)
1676 {
1677 struct amd_iommu *iommu = data;
1678
1679 return iommu_flush_dte(iommu, alias);
1680 }
1681
1682 /*
1683 * Command send function for invalidating a device table entry
1684 */
device_flush_dte(struct iommu_dev_data * dev_data)1685 static int device_flush_dte(struct iommu_dev_data *dev_data)
1686 {
1687 struct amd_iommu *iommu = get_amd_iommu_from_dev_data(dev_data);
1688 struct pci_dev *pdev = NULL;
1689 struct amd_iommu_pci_seg *pci_seg;
1690 u16 alias;
1691 int ret;
1692
1693 if (dev_is_pci(dev_data->dev))
1694 pdev = to_pci_dev(dev_data->dev);
1695
1696 if (pdev)
1697 ret = pci_for_each_dma_alias(pdev,
1698 device_flush_dte_alias, iommu);
1699 else
1700 ret = iommu_flush_dte(iommu, dev_data->devid);
1701 if (ret)
1702 return ret;
1703
1704 pci_seg = iommu->pci_seg;
1705 alias = pci_seg->alias_table[dev_data->devid];
1706 if (alias != dev_data->devid) {
1707 ret = iommu_flush_dte(iommu, alias);
1708 if (ret)
1709 return ret;
1710 }
1711
1712 if (dev_data->ats_enabled) {
1713 /* Invalidate the entire contents of an IOTLB */
1714 ret = device_flush_iotlb(dev_data, 0, U64_MAX,
1715 IOMMU_NO_PASID, false);
1716 }
1717
1718 return ret;
1719 }
1720
domain_flush_pages_v2(struct protection_domain * pdom,u64 address,u64 last,u32 flags)1721 static int domain_flush_pages_v2(struct protection_domain *pdom,
1722 u64 address, u64 last, u32 flags)
1723 {
1724 struct iommu_dev_data *dev_data;
1725 struct iommu_cmd cmd;
1726 int ret = 0;
1727
1728 lockdep_assert_held(&pdom->lock);
1729 list_for_each_entry(dev_data, &pdom->dev_list, list) {
1730 struct amd_iommu *iommu = get_amd_iommu_from_dev(dev_data->dev);
1731 u16 domid = dev_data->gcr3_info.domid;
1732
1733 build_inv_iommu_pages(&cmd, address, last, domid,
1734 IOMMU_NO_PASID,
1735 flags | CMD_INV_IOMMU_PAGES_GN_MASK);
1736
1737 ret |= iommu_queue_command(iommu, &cmd);
1738 }
1739
1740 return ret;
1741 }
1742
domain_flush_pages_v1(struct protection_domain * pdom,u64 address,u64 last,u32 flags)1743 static int domain_flush_pages_v1(struct protection_domain *pdom,
1744 u64 address, u64 last, u32 flags)
1745 {
1746 struct pdom_iommu_info *pdom_iommu_info;
1747 struct iommu_cmd cmd;
1748 int ret = 0;
1749 unsigned long i;
1750
1751 lockdep_assert_held(&pdom->lock);
1752
1753 build_inv_iommu_pages(&cmd, address, last,
1754 pdom->id, IOMMU_NO_PASID, flags);
1755
1756 xa_for_each(&pdom->iommu_array, i, pdom_iommu_info) {
1757 /*
1758 * Devices of this domain are behind this IOMMU
1759 * We need a TLB flush
1760 */
1761 ret |= iommu_queue_command(pdom_iommu_info->iommu, &cmd);
1762 }
1763
1764 /*
1765 * A domain w/ v1 table can be a nest parent, which can have
1766 * multiple nested domains. Each nested domain has 1:1 mapping
1767 * between gDomID and hDomID. Therefore, flush every hDomID
1768 * associated to this nest parent domain.
1769 *
1770 * See drivers/iommu/amd/nested.c: amd_iommu_alloc_domain_nested()
1771 */
1772 if (!list_empty(&pdom->viommu_list))
1773 ret |= iommu_flush_pages_v1_hdom_ids(pdom, address, last, flags);
1774
1775 return ret;
1776 }
1777
1778 /*
1779 * TLB invalidation function which is called from the mapping functions.
1780 * It flushes range of PTEs of the domain.
1781 */
__domain_flush_pages(struct protection_domain * domain,u64 address,u64 last,u32 flags)1782 static void __domain_flush_pages(struct protection_domain *domain,
1783 u64 address, u64 last, u32 flags)
1784 {
1785 struct iommu_dev_data *dev_data;
1786 int ret = 0;
1787 ioasid_t pasid = IOMMU_NO_PASID;
1788 bool gn = false;
1789
1790 lockdep_assert_held(&domain->lock);
1791
1792 if (pdom_is_v2_pgtbl_mode(domain)) {
1793 gn = true;
1794 ret = domain_flush_pages_v2(domain, address, last, flags);
1795 } else {
1796 ret = domain_flush_pages_v1(domain, address, last, flags);
1797 }
1798
1799 list_for_each_entry(dev_data, &domain->dev_list, list) {
1800
1801 if (!dev_data->ats_enabled)
1802 continue;
1803
1804 ret |= device_flush_iotlb(dev_data, address, last, pasid, gn);
1805 }
1806
1807 WARN_ON(ret);
1808 }
1809
amd_iommu_domain_flush_pages(struct protection_domain * domain,u64 address,u64 last,u32 flags)1810 void amd_iommu_domain_flush_pages(struct protection_domain *domain,
1811 u64 address, u64 last, u32 flags)
1812 {
1813 lockdep_assert_held(&domain->lock);
1814
1815 if (likely(!amd_iommu_np_cache) ||
1816 unlikely(address == 0 && last == U64_MAX)) {
1817 __domain_flush_pages(domain, address, last, flags);
1818
1819 /* Wait until IOMMU TLB and all device IOTLB flushes are complete */
1820 domain_flush_complete(domain);
1821
1822 return;
1823 }
1824
1825 /*
1826 * When NpCache is on, we infer that we run in a VM and use a vIOMMU.
1827 * In such setups it is best to avoid flushes of ranges which are not
1828 * naturally aligned, since it would lead to flushes of unmodified
1829 * PTEs. Such flushes would require the hypervisor to do more work than
1830 * necessary. Therefore, perform repeated flushes of aligned ranges
1831 * until you cover the range. Each iteration flushes the smaller
1832 * between the natural alignment of the address that we flush and the
1833 * greatest naturally aligned region that fits in the range.
1834 */
1835 while (address <= last) {
1836 unsigned int sz_lg2 = ilog2(last - address + 1);
1837 u64 flush_last;
1838
1839 if (likely(address))
1840 sz_lg2 = min_t(unsigned int, sz_lg2, __ffs64(address));
1841
1842 flush_last = address + (1ULL << sz_lg2) - 1;
1843 __domain_flush_pages(domain, address, flush_last, flags);
1844 if (check_add_overflow(flush_last, 1, &address))
1845 break;
1846 }
1847
1848 /* Wait until IOMMU TLB and all device IOTLB flushes are complete */
1849 domain_flush_complete(domain);
1850 }
1851
1852 /* Flush the whole IO/TLB for a given protection domain - including PDE */
amd_iommu_domain_flush_all(struct protection_domain * domain)1853 static void amd_iommu_domain_flush_all(struct protection_domain *domain)
1854 {
1855 amd_iommu_domain_flush_pages(domain, 0, U64_MAX,
1856 CMD_INV_IOMMU_PAGES_PDE_MASK);
1857 }
1858
amd_iommu_dev_flush_pasid_pages(struct iommu_dev_data * dev_data,ioasid_t pasid,u64 address,u64 last)1859 void amd_iommu_dev_flush_pasid_pages(struct iommu_dev_data *dev_data,
1860 ioasid_t pasid, u64 address, u64 last)
1861 {
1862 struct iommu_cmd cmd;
1863 struct amd_iommu *iommu = get_amd_iommu_from_dev(dev_data->dev);
1864
1865 build_inv_iommu_pages(&cmd, address, last,
1866 dev_data->gcr3_info.domid, pasid,
1867 CMD_INV_IOMMU_PAGES_GN_MASK |
1868 CMD_INV_IOMMU_PAGES_PDE_MASK);
1869 iommu_queue_command(iommu, &cmd);
1870
1871 if (dev_data->ats_enabled)
1872 device_flush_iotlb(dev_data, address, last, pasid, true);
1873
1874 iommu_completion_wait(iommu);
1875 }
1876
dev_flush_pasid_all(struct iommu_dev_data * dev_data,ioasid_t pasid)1877 static void dev_flush_pasid_all(struct iommu_dev_data *dev_data,
1878 ioasid_t pasid)
1879 {
1880 amd_iommu_dev_flush_pasid_pages(dev_data, pasid, 0, U64_MAX);
1881 }
1882
__amd_iommu_complete_ppr(struct device * dev,u32 pasid,int status,int tag,bool gn)1883 static int __amd_iommu_complete_ppr(struct device *dev, u32 pasid,
1884 int status, int tag, bool gn)
1885 {
1886 struct iommu_dev_data *dev_data;
1887 struct amd_iommu *iommu;
1888 struct iommu_cmd cmd;
1889
1890 dev_data = dev_iommu_priv_get(dev);
1891 iommu = get_amd_iommu_from_dev(dev);
1892
1893 build_complete_ppr(&cmd, dev_data->devid, pasid, status, tag, gn);
1894
1895 return iommu_queue_command(iommu, &cmd);
1896 }
1897
amd_iommu_complete_ppr(struct device * dev,u32 pasid,int status,int tag)1898 int amd_iommu_complete_ppr(struct device *dev, u32 pasid, int status, int tag)
1899 {
1900 struct iommu_dev_data *dev_data = dev_iommu_priv_get(dev);
1901 bool gn;
1902
1903 gn = pdom_is_v2_pgtbl_mode(dev_data->domain);
1904
1905 return __amd_iommu_complete_ppr(dev, pasid, status, tag, gn);
1906 }
1907
1908 /****************************************************************************
1909 *
1910 * The next functions belong to the domain allocation. A domain is
1911 * allocated for every IOMMU as the default domain. If device isolation
1912 * is enabled, every device get its own domain. The most important thing
1913 * about domains is the page table mapping the DMA address space they
1914 * contain.
1915 *
1916 ****************************************************************************/
amd_iommu_pdom_id_alloc(void)1917 int amd_iommu_pdom_id_alloc(void)
1918 {
1919 return ida_alloc_range(&pdom_ids, 1, MAX_DOMAIN_ID - 1, GFP_ATOMIC);
1920 }
1921
amd_iommu_pdom_id_reserve(u16 id,gfp_t gfp)1922 int amd_iommu_pdom_id_reserve(u16 id, gfp_t gfp)
1923 {
1924 return ida_alloc_range(&pdom_ids, id, id, gfp);
1925 }
1926
amd_iommu_pdom_id_free(int id)1927 void amd_iommu_pdom_id_free(int id)
1928 {
1929 ida_free(&pdom_ids, id);
1930 }
1931
amd_iommu_pdom_id_destroy(void)1932 void amd_iommu_pdom_id_destroy(void)
1933 {
1934 ida_destroy(&pdom_ids);
1935 }
1936
free_gcr3_tbl_level1(u64 * tbl)1937 static void free_gcr3_tbl_level1(u64 *tbl)
1938 {
1939 u64 *ptr;
1940 int i;
1941
1942 for (i = 0; i < 512; ++i) {
1943 if (!(tbl[i] & GCR3_VALID))
1944 continue;
1945
1946 ptr = iommu_phys_to_virt(tbl[i] & PAGE_MASK);
1947
1948 iommu_free_pages(ptr);
1949 }
1950 }
1951
free_gcr3_tbl_level2(u64 * tbl)1952 static void free_gcr3_tbl_level2(u64 *tbl)
1953 {
1954 u64 *ptr;
1955 int i;
1956
1957 for (i = 0; i < 512; ++i) {
1958 if (!(tbl[i] & GCR3_VALID))
1959 continue;
1960
1961 ptr = iommu_phys_to_virt(tbl[i] & PAGE_MASK);
1962
1963 free_gcr3_tbl_level1(ptr);
1964 }
1965 }
1966
free_gcr3_table(struct gcr3_tbl_info * gcr3_info)1967 static void free_gcr3_table(struct gcr3_tbl_info *gcr3_info)
1968 {
1969 if (gcr3_info->glx == 2)
1970 free_gcr3_tbl_level2(gcr3_info->gcr3_tbl);
1971 else if (gcr3_info->glx == 1)
1972 free_gcr3_tbl_level1(gcr3_info->gcr3_tbl);
1973 else
1974 WARN_ON_ONCE(gcr3_info->glx != 0);
1975
1976 gcr3_info->glx = 0;
1977
1978 /* Free per device domain ID */
1979 amd_iommu_pdom_id_free(gcr3_info->domid);
1980
1981 iommu_free_pages(gcr3_info->gcr3_tbl);
1982 gcr3_info->gcr3_tbl = NULL;
1983 }
1984
1985 /*
1986 * Number of GCR3 table levels required. Level must be 4-Kbyte
1987 * page and can contain up to 512 entries.
1988 */
get_gcr3_levels(int pasids)1989 static int get_gcr3_levels(int pasids)
1990 {
1991 int levels;
1992
1993 if (pasids == -1)
1994 return amd_iommu_max_glx_val;
1995
1996 levels = get_count_order(pasids);
1997
1998 return levels ? (DIV_ROUND_UP(levels, 9) - 1) : levels;
1999 }
2000
setup_gcr3_table(struct gcr3_tbl_info * gcr3_info,struct amd_iommu * iommu,int pasids)2001 static int setup_gcr3_table(struct gcr3_tbl_info *gcr3_info,
2002 struct amd_iommu *iommu, int pasids)
2003 {
2004 int levels = get_gcr3_levels(pasids);
2005 int nid = iommu ? dev_to_node(&iommu->dev->dev) : NUMA_NO_NODE;
2006 int domid;
2007
2008 if (levels > amd_iommu_max_glx_val)
2009 return -EINVAL;
2010
2011 if (gcr3_info->gcr3_tbl)
2012 return -EBUSY;
2013
2014 /* Allocate per device domain ID */
2015 domid = amd_iommu_pdom_id_alloc();
2016 if (domid <= 0)
2017 return -ENOSPC;
2018 gcr3_info->domid = domid;
2019
2020 gcr3_info->gcr3_tbl = iommu_alloc_pages_node_sz(nid, GFP_ATOMIC, SZ_4K);
2021 if (gcr3_info->gcr3_tbl == NULL) {
2022 amd_iommu_pdom_id_free(domid);
2023 return -ENOMEM;
2024 }
2025
2026 gcr3_info->glx = levels;
2027
2028 return 0;
2029 }
2030
__get_gcr3_pte(struct gcr3_tbl_info * gcr3_info,ioasid_t pasid,bool alloc)2031 static u64 *__get_gcr3_pte(struct gcr3_tbl_info *gcr3_info,
2032 ioasid_t pasid, bool alloc)
2033 {
2034 int index;
2035 u64 *pte;
2036 u64 *root = gcr3_info->gcr3_tbl;
2037 int level = gcr3_info->glx;
2038
2039 while (true) {
2040
2041 index = (pasid >> (9 * level)) & 0x1ff;
2042 pte = &root[index];
2043
2044 if (level == 0)
2045 break;
2046
2047 if (!(*pte & GCR3_VALID)) {
2048 if (!alloc)
2049 return NULL;
2050
2051 root = (void *)get_zeroed_page(GFP_ATOMIC);
2052 if (root == NULL)
2053 return NULL;
2054
2055 *pte = iommu_virt_to_phys(root) | GCR3_VALID;
2056 }
2057
2058 root = iommu_phys_to_virt(*pte & PAGE_MASK);
2059
2060 level -= 1;
2061 }
2062
2063 return pte;
2064 }
2065
update_gcr3(struct iommu_dev_data * dev_data,ioasid_t pasid,unsigned long gcr3,bool set)2066 static int update_gcr3(struct iommu_dev_data *dev_data,
2067 ioasid_t pasid, unsigned long gcr3, bool set)
2068 {
2069 struct gcr3_tbl_info *gcr3_info = &dev_data->gcr3_info;
2070 u64 *pte;
2071
2072 pte = __get_gcr3_pte(gcr3_info, pasid, true);
2073 if (pte == NULL)
2074 return -ENOMEM;
2075
2076 if (set)
2077 *pte = (gcr3 & PAGE_MASK) | GCR3_VALID;
2078 else
2079 *pte = 0;
2080
2081 dev_flush_pasid_all(dev_data, pasid);
2082 return 0;
2083 }
2084
amd_iommu_set_gcr3(struct iommu_dev_data * dev_data,ioasid_t pasid,unsigned long gcr3)2085 int amd_iommu_set_gcr3(struct iommu_dev_data *dev_data, ioasid_t pasid,
2086 unsigned long gcr3)
2087 {
2088 struct gcr3_tbl_info *gcr3_info = &dev_data->gcr3_info;
2089 int ret;
2090
2091 iommu_group_mutex_assert(dev_data->dev);
2092
2093 ret = update_gcr3(dev_data, pasid, gcr3, true);
2094 if (ret)
2095 return ret;
2096
2097 gcr3_info->pasid_cnt++;
2098 return ret;
2099 }
2100
amd_iommu_clear_gcr3(struct iommu_dev_data * dev_data,ioasid_t pasid)2101 int amd_iommu_clear_gcr3(struct iommu_dev_data *dev_data, ioasid_t pasid)
2102 {
2103 struct gcr3_tbl_info *gcr3_info = &dev_data->gcr3_info;
2104 int ret;
2105
2106 iommu_group_mutex_assert(dev_data->dev);
2107
2108 ret = update_gcr3(dev_data, pasid, 0, false);
2109 if (ret)
2110 return ret;
2111
2112 gcr3_info->pasid_cnt--;
2113 return ret;
2114 }
2115
2116 /*
2117 * Note:
2118 * The old value for GCR3 table and GPT have been cleared from caller.
2119 */
set_dte_gcr3_table(struct iommu_dev_data * dev_data,struct dev_table_entry * new)2120 static void set_dte_gcr3_table(struct iommu_dev_data *dev_data,
2121 struct dev_table_entry *new)
2122 {
2123 struct gcr3_tbl_info *gcr3_info = &dev_data->gcr3_info;
2124 u64 gcr3 = iommu_virt_to_phys(gcr3_info->gcr3_tbl);
2125
2126 new->data[0] |= DTE_FLAG_TV |
2127 (dev_data->ppr ? DTE_FLAG_PPR : 0) |
2128 (pdom_is_v2_pgtbl_mode(dev_data->domain) ? DTE_FLAG_GIOV : 0) |
2129 DTE_FLAG_GV |
2130 FIELD_PREP(DTE_GLX, gcr3_info->glx) |
2131 FIELD_PREP(DTE_GCR3_14_12, gcr3 >> 12) |
2132 DTE_FLAG_IR | DTE_FLAG_IW;
2133
2134 new->data[1] |= FIELD_PREP(DTE_DOMID_MASK, dev_data->gcr3_info.domid) |
2135 FIELD_PREP(DTE_GCR3_30_15, gcr3 >> 15) |
2136 (dev_data->ats_enabled ? DTE_FLAG_IOTLB : 0) |
2137 FIELD_PREP(DTE_GCR3_51_31, gcr3 >> 31);
2138
2139 /* Guest page table can only support 4 and 5 levels */
2140 if (amd_iommu_gpt_level == PAGE_MODE_5_LEVEL)
2141 new->data[2] |= FIELD_PREP(DTE_GPT_LEVEL_MASK, GUEST_PGTABLE_5_LEVEL);
2142 else
2143 new->data[2] |= FIELD_PREP(DTE_GPT_LEVEL_MASK, GUEST_PGTABLE_4_LEVEL);
2144 }
2145
amd_iommu_set_dte_v1(struct iommu_dev_data * dev_data,struct protection_domain * domain,u16 domid,struct pt_iommu_amdv1_hw_info * pt_info,struct dev_table_entry * new)2146 void amd_iommu_set_dte_v1(struct iommu_dev_data *dev_data,
2147 struct protection_domain *domain, u16 domid,
2148 struct pt_iommu_amdv1_hw_info *pt_info,
2149 struct dev_table_entry *new)
2150 {
2151 u64 host_pt_root = __sme_set(pt_info->host_pt_root);
2152
2153 /* Note Dirty tracking is used for v1 table only for now */
2154 new->data[0] |= DTE_FLAG_TV |
2155 FIELD_PREP(DTE_MODE_MASK, pt_info->mode) |
2156 (domain->dirty_tracking ? DTE_FLAG_HAD : 0) |
2157 FIELD_PREP(DTE_HOST_TRP, host_pt_root >> 12) |
2158 DTE_FLAG_IR | DTE_FLAG_IW;
2159
2160 new->data[1] |= FIELD_PREP(DTE_DOMID_MASK, domid) |
2161 (dev_data->ats_enabled ? DTE_FLAG_IOTLB : 0);
2162 }
2163
set_dte_v1(struct iommu_dev_data * dev_data,struct protection_domain * domain,u16 domid,phys_addr_t top_paddr,unsigned int top_level,struct dev_table_entry * new)2164 static void set_dte_v1(struct iommu_dev_data *dev_data,
2165 struct protection_domain *domain, u16 domid,
2166 phys_addr_t top_paddr, unsigned int top_level,
2167 struct dev_table_entry *new)
2168 {
2169 struct pt_iommu_amdv1_hw_info pt_info;
2170
2171 /*
2172 * When updating the IO pagetable, the new top and level
2173 * are provided as parameters. For other operations i.e.
2174 * device attach, retrieve the current pagetable info
2175 * via the IOMMU PT API.
2176 */
2177 if (top_paddr) {
2178 pt_info.host_pt_root = top_paddr;
2179 pt_info.mode = top_level + 1;
2180 } else {
2181 WARN_ON(top_paddr || top_level);
2182 pt_iommu_amdv1_hw_info(&domain->amdv1, &pt_info);
2183 }
2184
2185 amd_iommu_set_dte_v1(dev_data, domain, domid, &pt_info, new);
2186 }
2187
set_dte_passthrough(struct iommu_dev_data * dev_data,struct protection_domain * domain,struct dev_table_entry * new)2188 static void set_dte_passthrough(struct iommu_dev_data *dev_data,
2189 struct protection_domain *domain,
2190 struct dev_table_entry *new)
2191 {
2192 new->data[0] |= DTE_FLAG_TV | DTE_FLAG_IR | DTE_FLAG_IW;
2193
2194 new->data[1] |= FIELD_PREP(DTE_DOMID_MASK, domain->id) |
2195 (dev_data->ats_enabled ? DTE_FLAG_IOTLB : 0);
2196
2197 }
2198
set_dte_entry(struct amd_iommu * iommu,struct iommu_dev_data * dev_data,phys_addr_t top_paddr,unsigned int top_level)2199 static void set_dte_entry(struct amd_iommu *iommu,
2200 struct iommu_dev_data *dev_data,
2201 phys_addr_t top_paddr, unsigned int top_level)
2202 {
2203 u32 old_domid;
2204 struct dev_table_entry new = {};
2205 struct protection_domain *domain = dev_data->domain;
2206 struct gcr3_tbl_info *gcr3_info = &dev_data->gcr3_info;
2207 struct dev_table_entry *dte = &get_dev_table(iommu)[dev_data->devid];
2208
2209 amd_iommu_make_clear_dte(dev_data, &new);
2210
2211 old_domid = READ_ONCE(dte->data[1]) & DTE_DOMID_MASK;
2212 if (gcr3_info->gcr3_tbl)
2213 set_dte_gcr3_table(dev_data, &new);
2214 else if (domain->domain.type == IOMMU_DOMAIN_IDENTITY)
2215 set_dte_passthrough(dev_data, domain, &new);
2216 else if ((domain->domain.type & __IOMMU_DOMAIN_PAGING) &&
2217 domain->pd_mode == PD_MODE_V1)
2218 set_dte_v1(dev_data, domain, domain->id, top_paddr, top_level, &new);
2219 else
2220 WARN_ON(true);
2221
2222 amd_iommu_update_dte(iommu, dev_data, &new);
2223
2224 /*
2225 * A kdump kernel might be replacing a domain ID that was copied from
2226 * the previous kernel--if so, it needs to flush the translation cache
2227 * entries for the old domain ID that is being overwritten
2228 */
2229 if (old_domid) {
2230 amd_iommu_flush_tlb_domid(iommu, old_domid);
2231 }
2232 }
2233
2234 /*
2235 * Clear DMA-remap related flags to block all DMA (blockeded domain)
2236 */
clear_dte_entry(struct amd_iommu * iommu,struct iommu_dev_data * dev_data)2237 static void clear_dte_entry(struct amd_iommu *iommu, struct iommu_dev_data *dev_data)
2238 {
2239 struct dev_table_entry new = {};
2240
2241 amd_iommu_make_clear_dte(dev_data, &new);
2242 amd_iommu_update_dte(iommu, dev_data, &new);
2243 }
2244
2245 /* Update and flush DTE for the given device */
dev_update_dte(struct iommu_dev_data * dev_data,bool set)2246 static void dev_update_dte(struct iommu_dev_data *dev_data, bool set)
2247 {
2248 struct amd_iommu *iommu = get_amd_iommu_from_dev(dev_data->dev);
2249
2250 if (set)
2251 set_dte_entry(iommu, dev_data, 0, 0);
2252 else
2253 clear_dte_entry(iommu, dev_data);
2254 }
2255
2256 /*
2257 * If domain is SVA capable then initialize GCR3 table. Also if domain is
2258 * in v2 page table mode then update GCR3[0].
2259 */
init_gcr3_table(struct iommu_dev_data * dev_data,struct protection_domain * pdom)2260 static int init_gcr3_table(struct iommu_dev_data *dev_data,
2261 struct protection_domain *pdom)
2262 {
2263 struct amd_iommu *iommu = get_amd_iommu_from_dev_data(dev_data);
2264 int max_pasids = dev_data->max_pasids;
2265 struct pt_iommu_x86_64_hw_info pt_info;
2266 int ret = 0;
2267
2268 /*
2269 * If domain is in pt mode then setup GCR3 table only if device
2270 * is PASID capable
2271 */
2272 if (pdom_is_in_pt_mode(pdom) && !pdev_pasid_supported(dev_data))
2273 return ret;
2274
2275 /*
2276 * By default, setup GCR3 table to support MAX PASIDs
2277 * supported by the device/IOMMU.
2278 */
2279 ret = setup_gcr3_table(&dev_data->gcr3_info, iommu,
2280 max_pasids > 0 ? max_pasids : 1);
2281 if (ret)
2282 return ret;
2283
2284 /* Setup GCR3[0] only if domain is setup with v2 page table mode */
2285 if (!pdom_is_v2_pgtbl_mode(pdom))
2286 return ret;
2287
2288 pt_iommu_x86_64_hw_info(&pdom->amdv2, &pt_info);
2289 ret = update_gcr3(dev_data, 0, __sme_set(pt_info.gcr3_pt), true);
2290 if (ret)
2291 free_gcr3_table(&dev_data->gcr3_info);
2292
2293 return ret;
2294 }
2295
destroy_gcr3_table(struct iommu_dev_data * dev_data,struct protection_domain * pdom)2296 static void destroy_gcr3_table(struct iommu_dev_data *dev_data,
2297 struct protection_domain *pdom)
2298 {
2299 struct gcr3_tbl_info *gcr3_info = &dev_data->gcr3_info;
2300
2301 if (pdom_is_v2_pgtbl_mode(pdom))
2302 update_gcr3(dev_data, 0, 0, false);
2303
2304 if (gcr3_info->gcr3_tbl == NULL)
2305 return;
2306
2307 free_gcr3_table(gcr3_info);
2308 }
2309
pdom_attach_iommu(struct amd_iommu * iommu,struct protection_domain * pdom)2310 static int pdom_attach_iommu(struct amd_iommu *iommu,
2311 struct protection_domain *pdom)
2312 {
2313 struct pdom_iommu_info *pdom_iommu_info, *curr;
2314 unsigned long flags;
2315 int ret = 0;
2316
2317 spin_lock_irqsave(&pdom->lock, flags);
2318
2319 pdom_iommu_info = xa_load(&pdom->iommu_array, iommu->index);
2320 if (pdom_iommu_info) {
2321 pdom_iommu_info->refcnt++;
2322 goto out_unlock;
2323 }
2324
2325 pdom_iommu_info = kzalloc_obj(*pdom_iommu_info, GFP_ATOMIC);
2326 if (!pdom_iommu_info) {
2327 ret = -ENOMEM;
2328 goto out_unlock;
2329 }
2330
2331 pdom_iommu_info->iommu = iommu;
2332 pdom_iommu_info->refcnt = 1;
2333
2334 curr = xa_cmpxchg(&pdom->iommu_array, iommu->index,
2335 NULL, pdom_iommu_info, GFP_ATOMIC);
2336 if (curr) {
2337 kfree(pdom_iommu_info);
2338 ret = -ENOSPC;
2339 goto out_unlock;
2340 }
2341
2342 out_unlock:
2343 spin_unlock_irqrestore(&pdom->lock, flags);
2344 return ret;
2345 }
2346
pdom_detach_iommu(struct amd_iommu * iommu,struct protection_domain * pdom)2347 static void pdom_detach_iommu(struct amd_iommu *iommu,
2348 struct protection_domain *pdom)
2349 {
2350 struct pdom_iommu_info *pdom_iommu_info;
2351 unsigned long flags;
2352
2353 spin_lock_irqsave(&pdom->lock, flags);
2354
2355 pdom_iommu_info = xa_load(&pdom->iommu_array, iommu->index);
2356 if (!pdom_iommu_info) {
2357 spin_unlock_irqrestore(&pdom->lock, flags);
2358 return;
2359 }
2360
2361 pdom_iommu_info->refcnt--;
2362 if (pdom_iommu_info->refcnt == 0) {
2363 xa_erase(&pdom->iommu_array, iommu->index);
2364 kfree(pdom_iommu_info);
2365 }
2366
2367 spin_unlock_irqrestore(&pdom->lock, flags);
2368 }
2369
2370 /*
2371 * If a device is not yet associated with a domain, this function makes the
2372 * device visible in the domain
2373 */
attach_device(struct device * dev,struct protection_domain * domain)2374 static int attach_device(struct device *dev,
2375 struct protection_domain *domain)
2376 {
2377 struct iommu_dev_data *dev_data = dev_iommu_priv_get(dev);
2378 struct amd_iommu *iommu = get_amd_iommu_from_dev_data(dev_data);
2379 struct pci_dev *pdev;
2380 unsigned long flags;
2381 int ret = 0;
2382
2383 mutex_lock(&dev_data->mutex);
2384
2385 if (dev_data->domain != NULL) {
2386 ret = -EBUSY;
2387 goto out;
2388 }
2389
2390 /* Do reference counting */
2391 ret = pdom_attach_iommu(iommu, domain);
2392 if (ret)
2393 goto out;
2394
2395 /* Setup GCR3 table */
2396 if (pdom_is_sva_capable(domain)) {
2397 ret = init_gcr3_table(dev_data, domain);
2398 if (ret) {
2399 pdom_detach_iommu(iommu, domain);
2400 goto out;
2401 }
2402 }
2403
2404 pdev = dev_is_pci(dev_data->dev) ? to_pci_dev(dev_data->dev) : NULL;
2405 if (pdev && pdom_is_sva_capable(domain)) {
2406 pdev_enable_caps(pdev);
2407
2408 /*
2409 * Device can continue to function even if IOPF
2410 * enablement failed. Hence in error path just
2411 * disable device PRI support.
2412 */
2413 if (amd_iommu_iopf_add_device(iommu, dev_data))
2414 pdev_disable_cap_pri(pdev);
2415 } else if (pdev) {
2416 pdev_enable_cap_ats(pdev);
2417 }
2418
2419 /* Update data structures */
2420 dev_data->domain = domain;
2421 spin_lock_irqsave(&domain->lock, flags);
2422 list_add(&dev_data->list, &domain->dev_list);
2423 spin_unlock_irqrestore(&domain->lock, flags);
2424
2425 /* Update device table */
2426 dev_update_dte(dev_data, true);
2427
2428 out:
2429 mutex_unlock(&dev_data->mutex);
2430
2431 return ret;
2432 }
2433
2434 /*
2435 * Removes a device from a protection domain (with devtable_lock held)
2436 */
detach_device(struct device * dev)2437 static void detach_device(struct device *dev)
2438 {
2439 struct iommu_dev_data *dev_data = dev_iommu_priv_get(dev);
2440 struct amd_iommu *iommu = get_amd_iommu_from_dev_data(dev_data);
2441 struct protection_domain *domain = dev_data->domain;
2442 unsigned long flags;
2443
2444 mutex_lock(&dev_data->mutex);
2445
2446 /*
2447 * First check if the device is still attached. It might already
2448 * be detached from its domain because the generic
2449 * iommu_detach_group code detached it and we try again here in
2450 * our alias handling.
2451 */
2452 if (WARN_ON(!dev_data->domain))
2453 goto out;
2454
2455 /* Remove IOPF handler */
2456 if (dev_data->ppr) {
2457 iopf_queue_flush_dev(dev);
2458 amd_iommu_iopf_remove_device(iommu, dev_data);
2459 }
2460
2461 if (dev_is_pci(dev))
2462 pdev_disable_caps(to_pci_dev(dev));
2463
2464 /* Clear DTE and flush the entry */
2465 dev_update_dte(dev_data, false);
2466
2467 /* Flush IOTLB and wait for the flushes to finish */
2468 spin_lock_irqsave(&domain->lock, flags);
2469 amd_iommu_domain_flush_all(domain);
2470 list_del(&dev_data->list);
2471 spin_unlock_irqrestore(&domain->lock, flags);
2472
2473 /* Clear GCR3 table */
2474 if (pdom_is_sva_capable(domain))
2475 destroy_gcr3_table(dev_data, domain);
2476
2477 /* Update data structures */
2478 dev_data->domain = NULL;
2479
2480 /* decrease reference counters - needs to happen after the flushes */
2481 pdom_detach_iommu(iommu, domain);
2482
2483 out:
2484 mutex_unlock(&dev_data->mutex);
2485 }
2486
amd_iommu_probe_device(struct device * dev)2487 static struct iommu_device *amd_iommu_probe_device(struct device *dev)
2488 {
2489 struct iommu_device *iommu_dev;
2490 struct amd_iommu *iommu;
2491 struct iommu_dev_data *dev_data;
2492 int ret;
2493
2494 if (!check_device(dev))
2495 return ERR_PTR(-ENODEV);
2496
2497 iommu = rlookup_amd_iommu(dev);
2498 if (!iommu)
2499 return ERR_PTR(-ENODEV);
2500
2501 /* Not registered yet? */
2502 if (!iommu->iommu.ops)
2503 return ERR_PTR(-ENODEV);
2504
2505 if (dev_iommu_priv_get(dev))
2506 return &iommu->iommu;
2507
2508 ret = iommu_init_device(iommu, dev);
2509 if (ret) {
2510 dev_err(dev, "Failed to initialize - trying to proceed anyway\n");
2511 iommu_dev = ERR_PTR(ret);
2512 iommu_ignore_device(iommu, dev);
2513 goto out_err;
2514 }
2515
2516 amd_iommu_set_pci_msi_domain(dev, iommu);
2517 iommu_dev = &iommu->iommu;
2518
2519 /*
2520 * If IOMMU and device supports PASID then it will contain max
2521 * supported PASIDs, else it will be zero.
2522 */
2523 dev_data = dev_iommu_priv_get(dev);
2524 if (amd_iommu_pasid_supported() && dev_is_pci(dev) &&
2525 pdev_pasid_supported(dev_data)) {
2526 dev_data->max_pasids = min_t(u32, iommu->iommu.max_pasids,
2527 pci_max_pasids(to_pci_dev(dev)));
2528 }
2529
2530 if (amd_iommu_pgtable == PD_MODE_NONE) {
2531 pr_warn_once("%s: DMA translation not supported by iommu.\n",
2532 __func__);
2533 iommu_dev = ERR_PTR(-ENODEV);
2534 goto out_err;
2535 }
2536
2537 iommu_completion_wait(iommu);
2538
2539 if (FEATURE_NUM_INT_REMAP_SUP_2K(amd_iommu_efr2))
2540 dev_data->max_irqs = MAX_IRQS_PER_TABLE_2K;
2541 else
2542 dev_data->max_irqs = MAX_IRQS_PER_TABLE_512;
2543
2544 if (dev_is_pci(dev))
2545 pci_prepare_ats(to_pci_dev(dev), PAGE_SHIFT);
2546
2547 out_err:
2548 return iommu_dev;
2549 }
2550
amd_iommu_release_device(struct device * dev)2551 static void amd_iommu_release_device(struct device *dev)
2552 {
2553 struct iommu_dev_data *dev_data = dev_iommu_priv_get(dev);
2554
2555 WARN_ON(dev_data->domain);
2556
2557 /*
2558 * We keep dev_data around for unplugged devices and reuse it when the
2559 * device is re-plugged - not doing so would introduce a ton of races.
2560 */
2561 }
2562
amd_iommu_device_group(struct device * dev)2563 static struct iommu_group *amd_iommu_device_group(struct device *dev)
2564 {
2565 if (dev_is_pci(dev))
2566 return pci_device_group(dev);
2567
2568 return acpihid_device_group(dev);
2569 }
2570
2571 /*****************************************************************************
2572 *
2573 * The following functions belong to the exported interface of AMD IOMMU
2574 *
2575 * This interface allows access to lower level functions of the IOMMU
2576 * like protection domain handling and assignement of devices to domains
2577 * which is not possible with the dma_ops interface.
2578 *
2579 *****************************************************************************/
2580
protection_domain_init(struct protection_domain * domain)2581 static void protection_domain_init(struct protection_domain *domain)
2582 {
2583 spin_lock_init(&domain->lock);
2584 INIT_LIST_HEAD(&domain->dev_list);
2585 INIT_LIST_HEAD(&domain->dev_data_list);
2586 INIT_LIST_HEAD(&domain->viommu_list);
2587 xa_init(&domain->iommu_array);
2588 }
2589
protection_domain_alloc(void)2590 struct protection_domain *protection_domain_alloc(void)
2591 {
2592 struct protection_domain *domain;
2593 int domid;
2594
2595 domain = kzalloc_obj(*domain);
2596 if (!domain)
2597 return NULL;
2598
2599 domid = amd_iommu_pdom_id_alloc();
2600 if (domid <= 0) {
2601 kfree(domain);
2602 return NULL;
2603 }
2604 domain->id = domid;
2605
2606 protection_domain_init(domain);
2607
2608 return domain;
2609 }
2610
amd_iommu_hd_support(struct amd_iommu * iommu)2611 static bool amd_iommu_hd_support(struct amd_iommu *iommu)
2612 {
2613 if (amd_iommu_hatdis)
2614 return false;
2615
2616 return iommu && (iommu->features & FEATURE_HDSUP);
2617 }
2618
amd_iommu_get_top_lock(struct pt_iommu * iommupt)2619 static spinlock_t *amd_iommu_get_top_lock(struct pt_iommu *iommupt)
2620 {
2621 struct protection_domain *pdom =
2622 container_of(iommupt, struct protection_domain, iommu);
2623
2624 return &pdom->lock;
2625 }
2626
2627 /*
2628 * Update all HW references to the domain with a new pgtable configuration.
2629 */
amd_iommu_change_top(struct pt_iommu * iommu_table,phys_addr_t top_paddr,unsigned int top_level)2630 static void amd_iommu_change_top(struct pt_iommu *iommu_table,
2631 phys_addr_t top_paddr, unsigned int top_level)
2632 {
2633 struct protection_domain *pdom =
2634 container_of(iommu_table, struct protection_domain, iommu);
2635 struct iommu_dev_data *dev_data;
2636
2637 lockdep_assert_held(&pdom->lock);
2638
2639 /* Update the DTE for all devices attached to this domain */
2640 list_for_each_entry(dev_data, &pdom->dev_list, list) {
2641 struct amd_iommu *iommu = rlookup_amd_iommu(dev_data->dev);
2642
2643 /* Update the HW references with the new level and top ptr */
2644 set_dte_entry(iommu, dev_data, top_paddr, top_level);
2645 clone_aliases(iommu, dev_data->dev);
2646 }
2647
2648 list_for_each_entry(dev_data, &pdom->dev_list, list)
2649 device_flush_dte(dev_data);
2650
2651 domain_flush_complete(pdom);
2652 }
2653
2654 /*
2655 * amd_iommu_iotlb_sync_map() is used to generate flushes for non-present to
2656 * present (ie mapping) operations. It is a NOP if the IOMMU doesn't have non
2657 * present caching (like hypervisor shadowing).
2658 */
amd_iommu_iotlb_sync_map(struct iommu_domain * dom,unsigned long iova,size_t size)2659 static int amd_iommu_iotlb_sync_map(struct iommu_domain *dom,
2660 unsigned long iova, size_t size)
2661 {
2662 struct protection_domain *domain = to_pdomain(dom);
2663 unsigned long flags;
2664
2665 if (likely(!amd_iommu_np_cache))
2666 return 0;
2667
2668 spin_lock_irqsave(&domain->lock, flags);
2669 amd_iommu_domain_flush_pages(domain, iova, iova + size - 1,
2670 CMD_INV_IOMMU_PAGES_PDE_MASK);
2671 spin_unlock_irqrestore(&domain->lock, flags);
2672 return 0;
2673 }
2674
amd_iommu_flush_iotlb_all(struct iommu_domain * domain)2675 static void amd_iommu_flush_iotlb_all(struct iommu_domain *domain)
2676 {
2677 struct protection_domain *dom = to_pdomain(domain);
2678 unsigned long flags;
2679
2680 spin_lock_irqsave(&dom->lock, flags);
2681 amd_iommu_domain_flush_all(dom);
2682 spin_unlock_irqrestore(&dom->lock, flags);
2683 }
2684
amd_iommu_iotlb_sync(struct iommu_domain * domain,struct iommu_iotlb_gather * gather)2685 static void amd_iommu_iotlb_sync(struct iommu_domain *domain,
2686 struct iommu_iotlb_gather *gather)
2687 {
2688 struct protection_domain *dom = to_pdomain(domain);
2689 unsigned long flags;
2690
2691 spin_lock_irqsave(&dom->lock, flags);
2692 amd_iommu_domain_flush_pages(dom, gather->start, gather->end,
2693 iommu_pages_list_empty(&gather->freelist) ?
2694 0 : CMD_INV_IOMMU_PAGES_PDE_MASK);
2695 spin_unlock_irqrestore(&dom->lock, flags);
2696 iommu_put_pages_list(&gather->freelist);
2697 }
2698
2699 static const struct pt_iommu_driver_ops amd_hw_driver_ops_v1 = {
2700 .get_top_lock = amd_iommu_get_top_lock,
2701 .change_top = amd_iommu_change_top,
2702 };
2703
2704 static const struct iommu_domain_ops amdv1_ops = {
2705 IOMMU_PT_DOMAIN_OPS(amdv1),
2706 .iotlb_sync_map = amd_iommu_iotlb_sync_map,
2707 .flush_iotlb_all = amd_iommu_flush_iotlb_all,
2708 .iotlb_sync = amd_iommu_iotlb_sync,
2709 .attach_dev = amd_iommu_attach_device,
2710 .free = amd_iommu_domain_free,
2711 .enforce_cache_coherency = amd_iommu_enforce_cache_coherency,
2712 };
2713
2714 static const struct iommu_dirty_ops amdv1_dirty_ops = {
2715 IOMMU_PT_DIRTY_OPS(amdv1),
2716 .set_dirty_tracking = amd_iommu_set_dirty_tracking,
2717 };
2718
amd_iommu_domain_alloc_paging_v1(struct device * dev,u32 flags)2719 static struct iommu_domain *amd_iommu_domain_alloc_paging_v1(struct device *dev,
2720 u32 flags)
2721 {
2722 struct pt_iommu_amdv1_cfg cfg = {};
2723 struct protection_domain *domain;
2724 int ret;
2725
2726 if (amd_iommu_hatdis)
2727 return ERR_PTR(-EOPNOTSUPP);
2728
2729 domain = protection_domain_alloc();
2730 if (!domain)
2731 return ERR_PTR(-ENOMEM);
2732
2733 domain->pd_mode = PD_MODE_V1;
2734 domain->iommu.driver_ops = &amd_hw_driver_ops_v1;
2735 domain->iommu.nid = dev_to_node(dev);
2736 if (flags & IOMMU_HWPT_ALLOC_DIRTY_TRACKING)
2737 domain->domain.dirty_ops = &amdv1_dirty_ops;
2738
2739 /*
2740 * Someday FORCE_COHERENCE should be set by
2741 * amd_iommu_enforce_cache_coherency() like VT-d does.
2742 */
2743 cfg.common.features = BIT(PT_FEAT_DYNAMIC_TOP) |
2744 BIT(PT_FEAT_AMDV1_ENCRYPT_TABLES) |
2745 BIT(PT_FEAT_AMDV1_FORCE_COHERENCE);
2746
2747 /*
2748 * AMD's IOMMU can flush as many pages as necessary in a single flush.
2749 * Unless we run in a virtual machine, which can be inferred according
2750 * to whether "non-present cache" is on, it is probably best to prefer
2751 * (potentially) too extensive TLB flushing (i.e., more misses) over
2752 * multiple TLB flushes (i.e., more flushes). For virtual machines the
2753 * hypervisor needs to synchronize the host IOMMU PTEs with those of
2754 * the guest, and the trade-off is different: unnecessary TLB flushes
2755 * should be avoided.
2756 */
2757 if (amd_iommu_np_cache)
2758 cfg.common.features |= BIT(PT_FEAT_FLUSH_RANGE_NO_GAPS);
2759 else
2760 cfg.common.features |= BIT(PT_FEAT_FLUSH_RANGE);
2761
2762 cfg.common.hw_max_vasz_lg2 = amd_iommu_hpt_vasize;
2763 cfg.common.hw_max_oasz_lg2 = 52;
2764 cfg.starting_level = 2;
2765 domain->domain.ops = &amdv1_ops;
2766
2767 ret = pt_iommu_amdv1_init(&domain->amdv1, &cfg, GFP_KERNEL);
2768 if (ret) {
2769 amd_iommu_domain_free(&domain->domain);
2770 return ERR_PTR(ret);
2771 }
2772
2773 /*
2774 * Narrow the supported page sizes to those selected by the kernel
2775 * command line.
2776 */
2777 domain->domain.pgsize_bitmap &= amd_iommu_pgsize_bitmap;
2778 return &domain->domain;
2779 }
2780
2781 static const struct iommu_domain_ops amdv2_ops = {
2782 IOMMU_PT_DOMAIN_OPS(x86_64),
2783 .iotlb_sync_map = amd_iommu_iotlb_sync_map,
2784 .flush_iotlb_all = amd_iommu_flush_iotlb_all,
2785 .iotlb_sync = amd_iommu_iotlb_sync,
2786 .attach_dev = amd_iommu_attach_device,
2787 .free = amd_iommu_domain_free,
2788 /*
2789 * Note the AMDv2 page table format does not support a Force Coherency
2790 * bit, so enforce_cache_coherency should not be set. However VFIO is
2791 * not prepared to handle a case where some domains will support
2792 * enforcement and others do not. VFIO and iommufd will have to be fixed
2793 * before it can fully use the V2 page table. See the comment in
2794 * iommufd_hwpt_paging_alloc(). For now leave things as they have
2795 * historically been and lie about enforce_cache_coherencey.
2796 */
2797 .enforce_cache_coherency = amd_iommu_enforce_cache_coherency,
2798 };
2799
amd_iommu_domain_alloc_paging_v2(struct device * dev,u32 flags)2800 static struct iommu_domain *amd_iommu_domain_alloc_paging_v2(struct device *dev,
2801 u32 flags)
2802 {
2803 struct pt_iommu_x86_64_cfg cfg = {};
2804 struct protection_domain *domain;
2805 int ret;
2806
2807 if (!amd_iommu_v2_pgtbl_supported())
2808 return ERR_PTR(-EOPNOTSUPP);
2809
2810 domain = protection_domain_alloc();
2811 if (!domain)
2812 return ERR_PTR(-ENOMEM);
2813
2814 domain->pd_mode = PD_MODE_V2;
2815 domain->iommu.nid = dev_to_node(dev);
2816
2817 cfg.common.features = BIT(PT_FEAT_X86_64_AMD_ENCRYPT_TABLES);
2818 if (amd_iommu_np_cache)
2819 cfg.common.features |= BIT(PT_FEAT_FLUSH_RANGE_NO_GAPS);
2820 else
2821 cfg.common.features |= BIT(PT_FEAT_FLUSH_RANGE);
2822
2823 /*
2824 * The v2 table behaves differently if it is attached to PASID 0 vs a
2825 * non-zero PASID. On PASID 0 it has no sign extension and the full
2826 * 57/48 bits decode the lower addresses. Otherwise it behaves like a
2827 * normal sign extended x86 page table. Since we want the domain to work
2828 * in both modes the top bit is removed and PT_FEAT_SIGN_EXTEND is not
2829 * set which creates a table that is compatible in both modes.
2830 */
2831 if (amd_iommu_gpt_level == PAGE_MODE_5_LEVEL) {
2832 cfg.common.hw_max_vasz_lg2 = 56;
2833 cfg.top_level = 4;
2834 } else {
2835 cfg.common.hw_max_vasz_lg2 = 47;
2836 cfg.top_level = 3;
2837 }
2838 cfg.common.hw_max_oasz_lg2 = 52;
2839 domain->domain.ops = &amdv2_ops;
2840
2841 ret = pt_iommu_x86_64_init(&domain->amdv2, &cfg, GFP_KERNEL);
2842 if (ret) {
2843 amd_iommu_domain_free(&domain->domain);
2844 return ERR_PTR(ret);
2845 }
2846 return &domain->domain;
2847 }
2848
is_nest_parent_supported(u32 flags)2849 static inline bool is_nest_parent_supported(u32 flags)
2850 {
2851 /* Only allow nest parent when these features are supported */
2852 return check_feature(FEATURE_GT) &&
2853 check_feature(FEATURE_GIOSUP) &&
2854 check_feature2(FEATURE_GCR3TRPMODE);
2855 }
2856
2857 static struct iommu_domain *
amd_iommu_domain_alloc_paging_flags(struct device * dev,u32 flags,const struct iommu_user_data * user_data)2858 amd_iommu_domain_alloc_paging_flags(struct device *dev, u32 flags,
2859 const struct iommu_user_data *user_data)
2860
2861 {
2862 struct amd_iommu *iommu = get_amd_iommu_from_dev(dev);
2863 const u32 supported_flags = IOMMU_HWPT_ALLOC_DIRTY_TRACKING |
2864 IOMMU_HWPT_ALLOC_PASID |
2865 IOMMU_HWPT_ALLOC_NEST_PARENT;
2866
2867 if ((flags & ~supported_flags) || user_data)
2868 return ERR_PTR(-EOPNOTSUPP);
2869
2870 switch (flags & supported_flags) {
2871 case IOMMU_HWPT_ALLOC_DIRTY_TRACKING:
2872 case IOMMU_HWPT_ALLOC_NEST_PARENT:
2873 case IOMMU_HWPT_ALLOC_DIRTY_TRACKING | IOMMU_HWPT_ALLOC_NEST_PARENT:
2874 /*
2875 * Allocate domain with v1 page table for dirty tracking
2876 * and/or Nest parent.
2877 */
2878 if ((flags & IOMMU_HWPT_ALLOC_DIRTY_TRACKING) &&
2879 !amd_iommu_hd_support(iommu))
2880 break;
2881
2882 if ((flags & IOMMU_HWPT_ALLOC_NEST_PARENT) &&
2883 !is_nest_parent_supported(flags))
2884 break;
2885
2886 return amd_iommu_domain_alloc_paging_v1(dev, flags);
2887 case IOMMU_HWPT_ALLOC_PASID:
2888 /* Allocate domain with v2 page table if IOMMU supports PASID. */
2889 if (!amd_iommu_pasid_supported())
2890 break;
2891 return amd_iommu_domain_alloc_paging_v2(dev, flags);
2892 case 0: {
2893 struct iommu_domain *ret;
2894
2895 /* If nothing specific is required use the kernel commandline default */
2896 if (amd_iommu_pgtable == PD_MODE_V1) {
2897 ret = amd_iommu_domain_alloc_paging_v1(dev, flags);
2898 if (ret != ERR_PTR(-EOPNOTSUPP))
2899 return ret;
2900 return amd_iommu_domain_alloc_paging_v2(dev, flags);
2901 }
2902 ret = amd_iommu_domain_alloc_paging_v2(dev, flags);
2903 if (ret != ERR_PTR(-EOPNOTSUPP))
2904 return ret;
2905 return amd_iommu_domain_alloc_paging_v1(dev, flags);
2906 }
2907 default:
2908 break;
2909 }
2910 return ERR_PTR(-EOPNOTSUPP);
2911 }
2912
amd_iommu_domain_free(struct iommu_domain * dom)2913 void amd_iommu_domain_free(struct iommu_domain *dom)
2914 {
2915 struct protection_domain *domain = to_pdomain(dom);
2916
2917 WARN_ON(!list_empty(&domain->dev_list));
2918 pt_iommu_deinit(&domain->iommu);
2919 amd_iommu_pdom_id_free(domain->id);
2920 kfree(domain);
2921 }
2922
blocked_domain_attach_device(struct iommu_domain * domain,struct device * dev,struct iommu_domain * old)2923 static int blocked_domain_attach_device(struct iommu_domain *domain,
2924 struct device *dev,
2925 struct iommu_domain *old)
2926 {
2927 struct iommu_dev_data *dev_data = dev_iommu_priv_get(dev);
2928
2929 if (dev_data->domain)
2930 detach_device(dev);
2931
2932 /* Clear DTE and flush the entry */
2933 mutex_lock(&dev_data->mutex);
2934 dev_update_dte(dev_data, false);
2935 mutex_unlock(&dev_data->mutex);
2936
2937 return 0;
2938 }
2939
blocked_domain_set_dev_pasid(struct iommu_domain * domain,struct device * dev,ioasid_t pasid,struct iommu_domain * old)2940 static int blocked_domain_set_dev_pasid(struct iommu_domain *domain,
2941 struct device *dev, ioasid_t pasid,
2942 struct iommu_domain *old)
2943 {
2944 amd_iommu_remove_dev_pasid(dev, pasid, old);
2945 return 0;
2946 }
2947
2948 static struct iommu_domain blocked_domain = {
2949 .type = IOMMU_DOMAIN_BLOCKED,
2950 .ops = &(const struct iommu_domain_ops) {
2951 .attach_dev = blocked_domain_attach_device,
2952 .set_dev_pasid = blocked_domain_set_dev_pasid,
2953 }
2954 };
2955
2956 static struct protection_domain identity_domain;
2957
amd_iommu_identity_attach(struct iommu_domain * dom,struct device * dev,struct iommu_domain * old)2958 static int amd_iommu_identity_attach(struct iommu_domain *dom, struct device *dev,
2959 struct iommu_domain *old)
2960 {
2961 /*
2962 * Don't allow attaching a device to the identity domain if SNP is
2963 * enabled and SNP Mode0 support is not present.
2964 */
2965 if (amd_iommu_snp_en && !amd_iommu_snp_mode0_sup)
2966 return -EINVAL;
2967
2968 return amd_iommu_attach_device(dom, dev, old);
2969 }
2970
2971 static const struct iommu_domain_ops identity_domain_ops = {
2972 .attach_dev = amd_iommu_identity_attach,
2973 };
2974
amd_iommu_init_identity_domain(void)2975 void amd_iommu_init_identity_domain(void)
2976 {
2977 struct iommu_domain *domain = &identity_domain.domain;
2978
2979 domain->type = IOMMU_DOMAIN_IDENTITY;
2980 domain->ops = &identity_domain_ops;
2981 domain->owner = &amd_iommu_ops;
2982
2983 identity_domain.id = amd_iommu_pdom_id_alloc();
2984
2985 protection_domain_init(&identity_domain);
2986 }
2987
amd_iommu_attach_device(struct iommu_domain * dom,struct device * dev,struct iommu_domain * old)2988 static int amd_iommu_attach_device(struct iommu_domain *dom, struct device *dev,
2989 struct iommu_domain *old)
2990 {
2991 struct iommu_dev_data *dev_data = dev_iommu_priv_get(dev);
2992 struct protection_domain *domain = to_pdomain(dom);
2993 struct amd_iommu *iommu = get_amd_iommu_from_dev(dev);
2994 int ret;
2995
2996 /*
2997 * Skip attach device to domain if new domain is same as
2998 * devices current domain
2999 */
3000 if (dev_data->domain == domain)
3001 return 0;
3002
3003 dev_data->defer_attach = false;
3004
3005 /*
3006 * Restrict to devices with compatible IOMMU hardware support
3007 * when enforcement of dirty tracking is enabled.
3008 */
3009 if (dom->dirty_ops && !amd_iommu_hd_support(iommu))
3010 return -EINVAL;
3011
3012 if (dev_data->domain)
3013 detach_device(dev);
3014
3015 ret = attach_device(dev, domain);
3016
3017 #ifdef CONFIG_IRQ_REMAP
3018 if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir)) {
3019 if (dom->type == IOMMU_DOMAIN_UNMANAGED)
3020 dev_data->use_vapic = 1;
3021 else
3022 dev_data->use_vapic = 0;
3023 }
3024 #endif
3025
3026 return ret;
3027 }
3028
amd_iommu_capable(struct device * dev,enum iommu_cap cap)3029 static bool amd_iommu_capable(struct device *dev, enum iommu_cap cap)
3030 {
3031 switch (cap) {
3032 case IOMMU_CAP_CACHE_COHERENCY:
3033 return true;
3034 case IOMMU_CAP_NOEXEC:
3035 return false;
3036 case IOMMU_CAP_PRE_BOOT_PROTECTION:
3037 return amdr_ivrs_remap_support;
3038 case IOMMU_CAP_ENFORCE_CACHE_COHERENCY:
3039 return true;
3040 case IOMMU_CAP_DIRTY_TRACKING: {
3041 struct amd_iommu *iommu = get_amd_iommu_from_dev(dev);
3042
3043 return amd_iommu_hd_support(iommu);
3044 }
3045 case IOMMU_CAP_PCI_ATS_SUPPORTED: {
3046 struct iommu_dev_data *dev_data = dev_iommu_priv_get(dev);
3047
3048 return amd_iommu_iotlb_sup &&
3049 (dev_data->flags & AMD_IOMMU_DEVICE_FLAG_ATS_SUP);
3050 }
3051 default:
3052 break;
3053 }
3054
3055 return false;
3056 }
3057
amd_iommu_set_dirty_tracking(struct iommu_domain * domain,bool enable)3058 static int amd_iommu_set_dirty_tracking(struct iommu_domain *domain,
3059 bool enable)
3060 {
3061 struct protection_domain *pdomain = to_pdomain(domain);
3062 struct dev_table_entry *dte;
3063 struct iommu_dev_data *dev_data;
3064 bool domain_flush = false;
3065 struct amd_iommu *iommu;
3066 unsigned long flags;
3067 u64 new;
3068
3069 spin_lock_irqsave(&pdomain->lock, flags);
3070 if (!(pdomain->dirty_tracking ^ enable)) {
3071 spin_unlock_irqrestore(&pdomain->lock, flags);
3072 return 0;
3073 }
3074
3075 list_for_each_entry(dev_data, &pdomain->dev_list, list) {
3076 spin_lock(&dev_data->dte_lock);
3077 iommu = get_amd_iommu_from_dev_data(dev_data);
3078 dte = &get_dev_table(iommu)[dev_data->devid];
3079 new = dte->data[0];
3080 new = (enable ? new | DTE_FLAG_HAD : new & ~DTE_FLAG_HAD);
3081 dte->data[0] = new;
3082 spin_unlock(&dev_data->dte_lock);
3083
3084 /* Flush device DTE */
3085 device_flush_dte(dev_data);
3086 domain_flush = true;
3087 }
3088
3089 /* Flush IOTLB to mark IOPTE dirty on the next translation(s) */
3090 if (domain_flush)
3091 amd_iommu_domain_flush_all(pdomain);
3092
3093 pdomain->dirty_tracking = enable;
3094 spin_unlock_irqrestore(&pdomain->lock, flags);
3095
3096 return 0;
3097 }
3098
amd_iommu_get_resv_regions(struct device * dev,struct list_head * head)3099 static void amd_iommu_get_resv_regions(struct device *dev,
3100 struct list_head *head)
3101 {
3102 struct iommu_resv_region *region;
3103 struct unity_map_entry *entry;
3104 struct amd_iommu *iommu;
3105 struct amd_iommu_pci_seg *pci_seg;
3106 int devid, sbdf;
3107
3108 sbdf = get_device_sbdf_id(dev);
3109 if (sbdf < 0)
3110 return;
3111
3112 devid = PCI_SBDF_TO_DEVID(sbdf);
3113 iommu = get_amd_iommu_from_dev(dev);
3114 pci_seg = iommu->pci_seg;
3115
3116 list_for_each_entry(entry, &pci_seg->unity_map, list) {
3117 int type, prot = 0;
3118 size_t length;
3119
3120 if (devid < entry->devid_start || devid > entry->devid_end)
3121 continue;
3122
3123 type = IOMMU_RESV_DIRECT;
3124 length = entry->address_end - entry->address_start;
3125 if (entry->prot & IOMMU_PROT_IR)
3126 prot |= IOMMU_READ;
3127 if (entry->prot & IOMMU_PROT_IW)
3128 prot |= IOMMU_WRITE;
3129
3130 region = iommu_alloc_resv_region(entry->address_start,
3131 length, prot, type,
3132 GFP_KERNEL);
3133 if (!region) {
3134 dev_err(dev, "Out of memory allocating dm-regions\n");
3135 return;
3136 }
3137 list_add_tail(®ion->list, head);
3138 }
3139
3140 region = iommu_alloc_resv_region(MSI_RANGE_START,
3141 MSI_RANGE_END - MSI_RANGE_START + 1,
3142 0, IOMMU_RESV_MSI, GFP_KERNEL);
3143 if (!region)
3144 return;
3145 list_add_tail(®ion->list, head);
3146
3147 if (amd_iommu_ht_range_ignore())
3148 return;
3149
3150 region = iommu_alloc_resv_region(HT_RANGE_START,
3151 HT_RANGE_END - HT_RANGE_START + 1,
3152 0, IOMMU_RESV_RESERVED, GFP_KERNEL);
3153 if (!region)
3154 return;
3155 list_add_tail(®ion->list, head);
3156 }
3157
amd_iommu_is_attach_deferred(struct device * dev)3158 static bool amd_iommu_is_attach_deferred(struct device *dev)
3159 {
3160 struct iommu_dev_data *dev_data = dev_iommu_priv_get(dev);
3161
3162 return dev_data->defer_attach;
3163 }
3164
amd_iommu_def_domain_type(struct device * dev)3165 static int amd_iommu_def_domain_type(struct device *dev)
3166 {
3167 struct iommu_dev_data *dev_data;
3168
3169 dev_data = dev_iommu_priv_get(dev);
3170 if (!dev_data)
3171 return 0;
3172
3173 /* Always use DMA domain for untrusted device */
3174 if (dev_is_pci(dev) && to_pci_dev(dev)->untrusted)
3175 return IOMMU_DOMAIN_DMA;
3176
3177 /*
3178 * Do not identity map IOMMUv2 capable devices when:
3179 * - memory encryption is active, because some of those devices
3180 * (AMD GPUs) don't have the encryption bit in their DMA-mask
3181 * and require remapping.
3182 * - SNP is enabled, because it prohibits DTE[Mode]=0.
3183 */
3184 if (pdev_pasid_supported(dev_data) &&
3185 !cc_platform_has(CC_ATTR_MEM_ENCRYPT) &&
3186 !amd_iommu_snp_en) {
3187 return IOMMU_DOMAIN_IDENTITY;
3188 }
3189
3190 return 0;
3191 }
3192
amd_iommu_enforce_cache_coherency(struct iommu_domain * domain)3193 static bool amd_iommu_enforce_cache_coherency(struct iommu_domain *domain)
3194 {
3195 /* IOMMU_PTE_FC is always set */
3196 return true;
3197 }
3198
3199 const struct iommu_ops amd_iommu_ops = {
3200 .capable = amd_iommu_capable,
3201 .hw_info = amd_iommufd_hw_info,
3202 .blocked_domain = &blocked_domain,
3203 .release_domain = &blocked_domain,
3204 .identity_domain = &identity_domain.domain,
3205 .domain_alloc_paging_flags = amd_iommu_domain_alloc_paging_flags,
3206 .domain_alloc_sva = amd_iommu_domain_alloc_sva,
3207 .probe_device = amd_iommu_probe_device,
3208 .release_device = amd_iommu_release_device,
3209 .device_group = amd_iommu_device_group,
3210 .get_resv_regions = amd_iommu_get_resv_regions,
3211 .is_attach_deferred = amd_iommu_is_attach_deferred,
3212 .def_domain_type = amd_iommu_def_domain_type,
3213 .page_response = amd_iommu_page_response,
3214 .get_viommu_size = amd_iommufd_get_viommu_size,
3215 .viommu_init = amd_iommufd_viommu_init,
3216 };
3217
3218 #ifdef CONFIG_IRQ_REMAP
3219
3220 /*****************************************************************************
3221 *
3222 * Interrupt Remapping Implementation
3223 *
3224 *****************************************************************************/
3225
3226 static struct irq_chip amd_ir_chip;
3227 static DEFINE_SPINLOCK(iommu_table_lock);
3228
iommu_flush_dev_irt(struct pci_dev * unused,u16 devid,void * data)3229 static int iommu_flush_dev_irt(struct pci_dev *unused, u16 devid, void *data)
3230 {
3231 int ret;
3232 struct iommu_cmd cmd;
3233 struct amd_iommu *iommu = data;
3234
3235 build_inv_irt(&cmd, devid);
3236 ret = __iommu_queue_command_sync(iommu, &cmd, true);
3237 return ret;
3238 }
3239
iommu_flush_irt_and_complete(struct amd_iommu * iommu,u16 devid)3240 static void iommu_flush_irt_and_complete(struct amd_iommu *iommu, u16 devid)
3241 {
3242 int ret;
3243 u64 data;
3244 unsigned long flags;
3245 struct iommu_cmd cmd;
3246 struct pci_dev *pdev = NULL;
3247 struct iommu_dev_data *dev_data = search_dev_data(iommu, devid);
3248
3249 if (iommu->irtcachedis_enabled)
3250 return;
3251
3252 if (dev_data && dev_data->dev && dev_is_pci(dev_data->dev))
3253 pdev = to_pci_dev(dev_data->dev);
3254
3255 raw_spin_lock_irqsave(&iommu->lock, flags);
3256 data = get_cmdsem_val(iommu);
3257 build_completion_wait(&cmd, iommu, data);
3258
3259 if (pdev)
3260 ret = pci_for_each_dma_alias(pdev, iommu_flush_dev_irt, iommu);
3261 else
3262 ret = iommu_flush_dev_irt(NULL, devid, iommu);
3263 if (ret)
3264 goto out_err;
3265
3266 ret = __iommu_queue_command_sync(iommu, &cmd, false);
3267 if (ret)
3268 goto out_err;
3269 raw_spin_unlock_irqrestore(&iommu->lock, flags);
3270
3271 wait_on_sem(iommu, data);
3272 return;
3273
3274 out_err:
3275 raw_spin_unlock_irqrestore(&iommu->lock, flags);
3276 }
3277
iommu_get_int_tablen(struct iommu_dev_data * dev_data)3278 static inline u8 iommu_get_int_tablen(struct iommu_dev_data *dev_data)
3279 {
3280 if (dev_data && dev_data->max_irqs == MAX_IRQS_PER_TABLE_2K)
3281 return DTE_INTTABLEN_2K;
3282 return DTE_INTTABLEN_512;
3283 }
3284
set_dte_irq_entry(struct amd_iommu * iommu,u16 devid,struct irq_remap_table * table)3285 static void set_dte_irq_entry(struct amd_iommu *iommu, u16 devid,
3286 struct irq_remap_table *table)
3287 {
3288 u64 new;
3289 struct dev_table_entry *dte = &get_dev_table(iommu)[devid];
3290 struct iommu_dev_data *dev_data = search_dev_data(iommu, devid);
3291
3292 if (dev_data)
3293 spin_lock(&dev_data->dte_lock);
3294
3295 new = READ_ONCE(dte->data[2]);
3296 new &= ~DTE_IRQ_PHYS_ADDR_MASK;
3297 new |= iommu_virt_to_phys(table->table);
3298 new |= DTE_IRQ_REMAP_INTCTL;
3299 new |= iommu_get_int_tablen(dev_data);
3300 new |= DTE_IRQ_REMAP_ENABLE;
3301 WRITE_ONCE(dte->data[2], new);
3302
3303 if (dev_data)
3304 spin_unlock(&dev_data->dte_lock);
3305 }
3306
get_irq_table(struct amd_iommu * iommu,u16 devid)3307 static struct irq_remap_table *get_irq_table(struct amd_iommu *iommu, u16 devid)
3308 {
3309 struct irq_remap_table *table;
3310 struct amd_iommu_pci_seg *pci_seg = iommu->pci_seg;
3311
3312 if (WARN_ONCE(!pci_seg->rlookup_table[devid],
3313 "%s: no iommu for devid %x:%x\n",
3314 __func__, pci_seg->id, devid))
3315 return NULL;
3316
3317 table = pci_seg->irq_lookup_table[devid];
3318 if (WARN_ONCE(!table, "%s: no table for devid %x:%x\n",
3319 __func__, pci_seg->id, devid))
3320 return NULL;
3321
3322 return table;
3323 }
3324
__alloc_irq_table(int nid,size_t size)3325 static struct irq_remap_table *__alloc_irq_table(int nid, size_t size)
3326 {
3327 struct irq_remap_table *table;
3328
3329 table = kzalloc_obj(*table);
3330 if (!table)
3331 return NULL;
3332
3333 table->table = iommu_alloc_pages_node_sz(
3334 nid, GFP_KERNEL, max(DTE_INTTAB_ALIGNMENT, size));
3335 if (!table->table) {
3336 kfree(table);
3337 return NULL;
3338 }
3339 raw_spin_lock_init(&table->lock);
3340
3341 return table;
3342 }
3343
set_remap_table_entry(struct amd_iommu * iommu,u16 devid,struct irq_remap_table * table)3344 static void set_remap_table_entry(struct amd_iommu *iommu, u16 devid,
3345 struct irq_remap_table *table)
3346 {
3347 struct amd_iommu_pci_seg *pci_seg = iommu->pci_seg;
3348
3349 pci_seg->irq_lookup_table[devid] = table;
3350 set_dte_irq_entry(iommu, devid, table);
3351 iommu_flush_dte(iommu, devid);
3352 }
3353
set_remap_table_entry_alias(struct pci_dev * pdev,u16 alias,void * data)3354 static int set_remap_table_entry_alias(struct pci_dev *pdev, u16 alias,
3355 void *data)
3356 {
3357 struct irq_remap_table *table = data;
3358 struct amd_iommu_pci_seg *pci_seg;
3359 struct amd_iommu *iommu = rlookup_amd_iommu(&pdev->dev);
3360
3361 if (!iommu)
3362 return -EINVAL;
3363
3364 pci_seg = iommu->pci_seg;
3365 pci_seg->irq_lookup_table[alias] = table;
3366 set_dte_irq_entry(iommu, alias, table);
3367 iommu_flush_dte(pci_seg->rlookup_table[alias], alias);
3368
3369 return 0;
3370 }
3371
get_irq_table_size(unsigned int max_irqs)3372 static inline size_t get_irq_table_size(unsigned int max_irqs)
3373 {
3374 if (!AMD_IOMMU_GUEST_IR_GA(amd_iommu_guest_ir))
3375 return max_irqs * sizeof(u32);
3376
3377 return max_irqs * (sizeof(u64) * 2);
3378 }
3379
alloc_irq_table(struct amd_iommu * iommu,u16 devid,struct pci_dev * pdev,unsigned int max_irqs)3380 static struct irq_remap_table *alloc_irq_table(struct amd_iommu *iommu,
3381 u16 devid, struct pci_dev *pdev,
3382 unsigned int max_irqs)
3383 {
3384 struct irq_remap_table *table = NULL;
3385 struct irq_remap_table *new_table = NULL;
3386 struct amd_iommu_pci_seg *pci_seg;
3387 unsigned long flags;
3388 int nid = iommu->dev ? dev_to_node(&iommu->dev->dev) : NUMA_NO_NODE;
3389 u16 alias;
3390
3391 spin_lock_irqsave(&iommu_table_lock, flags);
3392
3393 pci_seg = iommu->pci_seg;
3394 table = pci_seg->irq_lookup_table[devid];
3395 if (table)
3396 goto out_unlock;
3397
3398 alias = pci_seg->alias_table[devid];
3399 table = pci_seg->irq_lookup_table[alias];
3400 if (table) {
3401 set_remap_table_entry(iommu, devid, table);
3402 goto out_wait;
3403 }
3404 spin_unlock_irqrestore(&iommu_table_lock, flags);
3405
3406 /* Nothing there yet, allocate new irq remapping table */
3407 new_table = __alloc_irq_table(nid, get_irq_table_size(max_irqs));
3408 if (!new_table)
3409 return NULL;
3410
3411 spin_lock_irqsave(&iommu_table_lock, flags);
3412
3413 table = pci_seg->irq_lookup_table[devid];
3414 if (table)
3415 goto out_unlock;
3416
3417 table = pci_seg->irq_lookup_table[alias];
3418 if (table) {
3419 set_remap_table_entry(iommu, devid, table);
3420 goto out_wait;
3421 }
3422
3423 table = new_table;
3424 new_table = NULL;
3425
3426 if (pdev)
3427 pci_for_each_dma_alias(pdev, set_remap_table_entry_alias,
3428 table);
3429 else
3430 set_remap_table_entry(iommu, devid, table);
3431
3432 if (devid != alias)
3433 set_remap_table_entry(iommu, alias, table);
3434
3435 out_wait:
3436 iommu_completion_wait(iommu);
3437
3438 out_unlock:
3439 spin_unlock_irqrestore(&iommu_table_lock, flags);
3440
3441 if (new_table) {
3442 iommu_free_pages(new_table->table);
3443 kfree(new_table);
3444 }
3445 return table;
3446 }
3447
alloc_irq_index(struct amd_iommu * iommu,u16 devid,int count,bool align,struct pci_dev * pdev,unsigned long max_irqs)3448 static int alloc_irq_index(struct amd_iommu *iommu, u16 devid, int count,
3449 bool align, struct pci_dev *pdev,
3450 unsigned long max_irqs)
3451 {
3452 struct irq_remap_table *table;
3453 int index, c, alignment = 1;
3454 unsigned long flags;
3455
3456 table = alloc_irq_table(iommu, devid, pdev, max_irqs);
3457 if (!table)
3458 return -ENODEV;
3459
3460 if (align)
3461 alignment = roundup_pow_of_two(count);
3462
3463 raw_spin_lock_irqsave(&table->lock, flags);
3464
3465 /* Scan table for free entries */
3466 for (index = ALIGN(table->min_index, alignment), c = 0;
3467 index < max_irqs;) {
3468 if (!iommu->irte_ops->is_allocated(table, index)) {
3469 c += 1;
3470 } else {
3471 c = 0;
3472 index = ALIGN(index + 1, alignment);
3473 continue;
3474 }
3475
3476 if (c == count) {
3477 for (; c != 0; --c)
3478 iommu->irte_ops->set_allocated(table, index - c + 1);
3479
3480 index -= count - 1;
3481 goto out;
3482 }
3483
3484 index++;
3485 }
3486
3487 index = -ENOSPC;
3488
3489 out:
3490 raw_spin_unlock_irqrestore(&table->lock, flags);
3491
3492 return index;
3493 }
3494
__modify_irte_ga(struct amd_iommu * iommu,u16 devid,int index,struct irte_ga * irte)3495 static int __modify_irte_ga(struct amd_iommu *iommu, u16 devid, int index,
3496 struct irte_ga *irte)
3497 {
3498 struct irq_remap_table *table;
3499 struct irte_ga *entry;
3500 unsigned long flags;
3501 u128 old;
3502
3503 table = get_irq_table(iommu, devid);
3504 if (!table)
3505 return -ENOMEM;
3506
3507 raw_spin_lock_irqsave(&table->lock, flags);
3508
3509 entry = (struct irte_ga *)table->table;
3510 entry = &entry[index];
3511
3512 /*
3513 * We use cmpxchg16 to atomically update the 128-bit IRTE,
3514 * and it cannot be updated by the hardware or other processors
3515 * behind us, so the return value of cmpxchg16 should be the
3516 * same as the old value.
3517 */
3518 old = entry->irte;
3519 WARN_ON(!try_cmpxchg128(&entry->irte, &old, irte->irte));
3520
3521 raw_spin_unlock_irqrestore(&table->lock, flags);
3522
3523 return 0;
3524 }
3525
modify_irte_ga(struct amd_iommu * iommu,u16 devid,int index,struct irte_ga * irte)3526 static int modify_irte_ga(struct amd_iommu *iommu, u16 devid, int index,
3527 struct irte_ga *irte)
3528 {
3529 int ret;
3530
3531 ret = __modify_irte_ga(iommu, devid, index, irte);
3532 if (ret)
3533 return ret;
3534
3535 iommu_flush_irt_and_complete(iommu, devid);
3536
3537 return 0;
3538 }
3539
modify_irte(struct amd_iommu * iommu,u16 devid,int index,union irte * irte)3540 static int modify_irte(struct amd_iommu *iommu,
3541 u16 devid, int index, union irte *irte)
3542 {
3543 struct irq_remap_table *table;
3544 unsigned long flags;
3545
3546 table = get_irq_table(iommu, devid);
3547 if (!table)
3548 return -ENOMEM;
3549
3550 raw_spin_lock_irqsave(&table->lock, flags);
3551 table->table[index] = irte->val;
3552 raw_spin_unlock_irqrestore(&table->lock, flags);
3553
3554 iommu_flush_irt_and_complete(iommu, devid);
3555
3556 return 0;
3557 }
3558
free_irte(struct amd_iommu * iommu,u16 devid,int index)3559 static void free_irte(struct amd_iommu *iommu, u16 devid, int index)
3560 {
3561 struct irq_remap_table *table;
3562 unsigned long flags;
3563
3564 table = get_irq_table(iommu, devid);
3565 if (!table)
3566 return;
3567
3568 raw_spin_lock_irqsave(&table->lock, flags);
3569 iommu->irte_ops->clear_allocated(table, index);
3570 raw_spin_unlock_irqrestore(&table->lock, flags);
3571
3572 iommu_flush_irt_and_complete(iommu, devid);
3573 }
3574
irte_prepare(void * entry,u32 delivery_mode,bool dest_mode,u8 vector,u32 dest_apicid,int devid)3575 static void irte_prepare(void *entry,
3576 u32 delivery_mode, bool dest_mode,
3577 u8 vector, u32 dest_apicid, int devid)
3578 {
3579 union irte *irte = (union irte *) entry;
3580
3581 irte->val = 0;
3582 irte->fields.vector = vector;
3583 irte->fields.int_type = delivery_mode;
3584 irte->fields.destination = dest_apicid;
3585 irte->fields.dm = dest_mode;
3586 irte->fields.valid = 1;
3587 }
3588
irte_ga_prepare(void * entry,u32 delivery_mode,bool dest_mode,u8 vector,u32 dest_apicid,int devid)3589 static void irte_ga_prepare(void *entry,
3590 u32 delivery_mode, bool dest_mode,
3591 u8 vector, u32 dest_apicid, int devid)
3592 {
3593 struct irte_ga *irte = (struct irte_ga *) entry;
3594
3595 irte->lo.val = 0;
3596 irte->hi.val = 0;
3597 irte->lo.fields_remap.int_type = delivery_mode;
3598 irte->lo.fields_remap.dm = dest_mode;
3599 irte->hi.fields.vector = vector;
3600 irte->lo.fields_remap.destination = APICID_TO_IRTE_DEST_LO(dest_apicid);
3601 irte->hi.fields.destination = APICID_TO_IRTE_DEST_HI(dest_apicid);
3602 irte->lo.fields_remap.valid = 1;
3603 }
3604
irte_activate(struct amd_iommu * iommu,void * entry,u16 devid,u16 index)3605 static void irte_activate(struct amd_iommu *iommu, void *entry, u16 devid, u16 index)
3606 {
3607 union irte *irte = (union irte *) entry;
3608
3609 irte->fields.valid = 1;
3610 modify_irte(iommu, devid, index, irte);
3611 }
3612
irte_ga_activate(struct amd_iommu * iommu,void * entry,u16 devid,u16 index)3613 static void irte_ga_activate(struct amd_iommu *iommu, void *entry, u16 devid, u16 index)
3614 {
3615 struct irte_ga *irte = (struct irte_ga *) entry;
3616
3617 irte->lo.fields_remap.valid = 1;
3618 modify_irte_ga(iommu, devid, index, irte);
3619 }
3620
irte_deactivate(struct amd_iommu * iommu,void * entry,u16 devid,u16 index)3621 static void irte_deactivate(struct amd_iommu *iommu, void *entry, u16 devid, u16 index)
3622 {
3623 union irte *irte = (union irte *) entry;
3624
3625 irte->fields.valid = 0;
3626 modify_irte(iommu, devid, index, irte);
3627 }
3628
irte_ga_deactivate(struct amd_iommu * iommu,void * entry,u16 devid,u16 index)3629 static void irte_ga_deactivate(struct amd_iommu *iommu, void *entry, u16 devid, u16 index)
3630 {
3631 struct irte_ga *irte = (struct irte_ga *) entry;
3632
3633 irte->lo.fields_remap.valid = 0;
3634 modify_irte_ga(iommu, devid, index, irte);
3635 }
3636
irte_set_affinity(struct amd_iommu * iommu,void * entry,u16 devid,u16 index,u8 vector,u32 dest_apicid)3637 static void irte_set_affinity(struct amd_iommu *iommu, void *entry, u16 devid, u16 index,
3638 u8 vector, u32 dest_apicid)
3639 {
3640 union irte *irte = (union irte *) entry;
3641
3642 irte->fields.vector = vector;
3643 irte->fields.destination = dest_apicid;
3644 modify_irte(iommu, devid, index, irte);
3645 }
3646
irte_ga_set_affinity(struct amd_iommu * iommu,void * entry,u16 devid,u16 index,u8 vector,u32 dest_apicid)3647 static void irte_ga_set_affinity(struct amd_iommu *iommu, void *entry, u16 devid, u16 index,
3648 u8 vector, u32 dest_apicid)
3649 {
3650 struct irte_ga *irte = (struct irte_ga *) entry;
3651
3652 if (!irte->lo.fields_remap.guest_mode) {
3653 irte->hi.fields.vector = vector;
3654 irte->lo.fields_remap.destination =
3655 APICID_TO_IRTE_DEST_LO(dest_apicid);
3656 irte->hi.fields.destination =
3657 APICID_TO_IRTE_DEST_HI(dest_apicid);
3658 modify_irte_ga(iommu, devid, index, irte);
3659 }
3660 }
3661
3662 #define IRTE_ALLOCATED (~1U)
irte_set_allocated(struct irq_remap_table * table,int index)3663 static void irte_set_allocated(struct irq_remap_table *table, int index)
3664 {
3665 table->table[index] = IRTE_ALLOCATED;
3666 }
3667
irte_ga_set_allocated(struct irq_remap_table * table,int index)3668 static void irte_ga_set_allocated(struct irq_remap_table *table, int index)
3669 {
3670 struct irte_ga *ptr = (struct irte_ga *)table->table;
3671 struct irte_ga *irte = &ptr[index];
3672
3673 memset(&irte->lo.val, 0, sizeof(u64));
3674 memset(&irte->hi.val, 0, sizeof(u64));
3675 irte->hi.fields.vector = 0xff;
3676 }
3677
irte_is_allocated(struct irq_remap_table * table,int index)3678 static bool irte_is_allocated(struct irq_remap_table *table, int index)
3679 {
3680 union irte *ptr = (union irte *)table->table;
3681 union irte *irte = &ptr[index];
3682
3683 return irte->val != 0;
3684 }
3685
irte_ga_is_allocated(struct irq_remap_table * table,int index)3686 static bool irte_ga_is_allocated(struct irq_remap_table *table, int index)
3687 {
3688 struct irte_ga *ptr = (struct irte_ga *)table->table;
3689 struct irte_ga *irte = &ptr[index];
3690
3691 return irte->hi.fields.vector != 0;
3692 }
3693
irte_clear_allocated(struct irq_remap_table * table,int index)3694 static void irte_clear_allocated(struct irq_remap_table *table, int index)
3695 {
3696 table->table[index] = 0;
3697 }
3698
irte_ga_clear_allocated(struct irq_remap_table * table,int index)3699 static void irte_ga_clear_allocated(struct irq_remap_table *table, int index)
3700 {
3701 struct irte_ga *ptr = (struct irte_ga *)table->table;
3702 struct irte_ga *irte = &ptr[index];
3703
3704 memset(&irte->lo.val, 0, sizeof(u64));
3705 memset(&irte->hi.val, 0, sizeof(u64));
3706 }
3707
get_devid(struct irq_alloc_info * info)3708 static int get_devid(struct irq_alloc_info *info)
3709 {
3710 switch (info->type) {
3711 case X86_IRQ_ALLOC_TYPE_IOAPIC:
3712 return get_ioapic_devid(info->devid);
3713 case X86_IRQ_ALLOC_TYPE_HPET:
3714 return get_hpet_devid(info->devid);
3715 case X86_IRQ_ALLOC_TYPE_PCI_MSI:
3716 case X86_IRQ_ALLOC_TYPE_PCI_MSIX:
3717 return get_device_sbdf_id(msi_desc_to_dev(info->desc));
3718 default:
3719 WARN_ON_ONCE(1);
3720 return -1;
3721 }
3722 }
3723
3724 struct irq_remap_ops amd_iommu_irq_ops = {
3725 .prepare = amd_iommu_prepare,
3726 .enable = amd_iommu_enable,
3727 .disable = amd_iommu_disable,
3728 .reenable = amd_iommu_reenable,
3729 .enable_faulting = amd_iommu_enable_faulting,
3730 };
3731
fill_msi_msg(struct msi_msg * msg,u32 index)3732 static void fill_msi_msg(struct msi_msg *msg, u32 index)
3733 {
3734 msg->data = index;
3735 msg->address_lo = 0;
3736 msg->arch_addr_lo.base_address = X86_MSI_BASE_ADDRESS_LOW;
3737 /*
3738 * The struct msi_msg.dest_mode_logical is used to set the DM bit
3739 * in MSI Message Address Register. For device w/ 2K int-remap support,
3740 * this bit must be set to 1 regardless of the actual destination
3741 * mode, which is signified by the IRTE[DM].
3742 */
3743 if (FEATURE_NUM_INT_REMAP_SUP_2K(amd_iommu_efr2))
3744 msg->arch_addr_lo.dest_mode_logical = true;
3745 msg->address_hi = X86_MSI_BASE_ADDRESS_HIGH;
3746 }
3747
irq_remapping_prepare_irte(struct amd_ir_data * data,struct irq_cfg * irq_cfg,struct irq_alloc_info * info,int devid,int index,int sub_handle)3748 static void irq_remapping_prepare_irte(struct amd_ir_data *data,
3749 struct irq_cfg *irq_cfg,
3750 struct irq_alloc_info *info,
3751 int devid, int index, int sub_handle)
3752 {
3753 struct irq_2_irte *irte_info = &data->irq_2_irte;
3754 struct amd_iommu *iommu = data->iommu;
3755
3756 if (!iommu)
3757 return;
3758
3759 data->irq_2_irte.devid = devid;
3760 data->irq_2_irte.index = index + sub_handle;
3761 iommu->irte_ops->prepare(data->entry, APIC_DELIVERY_MODE_FIXED,
3762 apic->dest_mode_logical, irq_cfg->vector,
3763 irq_cfg->dest_apicid, devid);
3764
3765 switch (info->type) {
3766 case X86_IRQ_ALLOC_TYPE_IOAPIC:
3767 case X86_IRQ_ALLOC_TYPE_HPET:
3768 case X86_IRQ_ALLOC_TYPE_PCI_MSI:
3769 case X86_IRQ_ALLOC_TYPE_PCI_MSIX:
3770 fill_msi_msg(&data->msi_entry, irte_info->index);
3771 break;
3772
3773 default:
3774 BUG_ON(1);
3775 break;
3776 }
3777 }
3778
3779 struct amd_irte_ops irte_32_ops = {
3780 .prepare = irte_prepare,
3781 .activate = irte_activate,
3782 .deactivate = irte_deactivate,
3783 .set_affinity = irte_set_affinity,
3784 .set_allocated = irte_set_allocated,
3785 .is_allocated = irte_is_allocated,
3786 .clear_allocated = irte_clear_allocated,
3787 };
3788
3789 struct amd_irte_ops irte_128_ops = {
3790 .prepare = irte_ga_prepare,
3791 .activate = irte_ga_activate,
3792 .deactivate = irte_ga_deactivate,
3793 .set_affinity = irte_ga_set_affinity,
3794 .set_allocated = irte_ga_set_allocated,
3795 .is_allocated = irte_ga_is_allocated,
3796 .clear_allocated = irte_ga_clear_allocated,
3797 };
3798
irq_remapping_alloc(struct irq_domain * domain,unsigned int virq,unsigned int nr_irqs,void * arg)3799 static int irq_remapping_alloc(struct irq_domain *domain, unsigned int virq,
3800 unsigned int nr_irqs, void *arg)
3801 {
3802 struct irq_alloc_info *info = arg;
3803 struct irq_data *irq_data;
3804 struct amd_ir_data *data = NULL;
3805 struct amd_iommu *iommu;
3806 struct irq_cfg *cfg;
3807 struct iommu_dev_data *dev_data;
3808 unsigned long max_irqs;
3809 int i, ret, devid, seg, sbdf;
3810 int index;
3811
3812 if (!info)
3813 return -EINVAL;
3814 if (nr_irqs > 1 && info->type != X86_IRQ_ALLOC_TYPE_PCI_MSI)
3815 return -EINVAL;
3816
3817 sbdf = get_devid(info);
3818 if (sbdf < 0)
3819 return -EINVAL;
3820
3821 seg = PCI_SBDF_TO_SEGID(sbdf);
3822 devid = PCI_SBDF_TO_DEVID(sbdf);
3823 iommu = __rlookup_amd_iommu(seg, devid);
3824 if (!iommu)
3825 return -EINVAL;
3826
3827 dev_data = search_dev_data(iommu, devid);
3828 max_irqs = dev_data ? dev_data->max_irqs : MAX_IRQS_PER_TABLE_512;
3829
3830 ret = irq_domain_alloc_irqs_parent(domain, virq, nr_irqs, arg);
3831 if (ret < 0)
3832 return ret;
3833
3834 if (info->type == X86_IRQ_ALLOC_TYPE_IOAPIC) {
3835 struct irq_remap_table *table;
3836
3837 table = alloc_irq_table(iommu, devid, NULL, max_irqs);
3838 if (table) {
3839 if (!table->min_index) {
3840 /*
3841 * Keep the first 32 indexes free for IOAPIC
3842 * interrupts.
3843 */
3844 table->min_index = 32;
3845 for (i = 0; i < 32; ++i)
3846 iommu->irte_ops->set_allocated(table, i);
3847 }
3848 WARN_ON(table->min_index != 32);
3849 index = info->ioapic.pin;
3850 } else {
3851 index = -ENOMEM;
3852 }
3853 } else if (info->type == X86_IRQ_ALLOC_TYPE_PCI_MSI ||
3854 info->type == X86_IRQ_ALLOC_TYPE_PCI_MSIX) {
3855 bool align = (info->type == X86_IRQ_ALLOC_TYPE_PCI_MSI);
3856
3857 index = alloc_irq_index(iommu, devid, nr_irqs, align,
3858 msi_desc_to_pci_dev(info->desc),
3859 max_irqs);
3860 } else {
3861 index = alloc_irq_index(iommu, devid, nr_irqs, false, NULL,
3862 max_irqs);
3863 }
3864
3865 if (index < 0) {
3866 pr_warn("Failed to allocate IRTE\n");
3867 ret = index;
3868 goto out_free_parent;
3869 }
3870
3871 for (i = 0; i < nr_irqs; i++) {
3872 irq_data = irq_domain_get_irq_data(domain, virq + i);
3873 cfg = irq_data ? irqd_cfg(irq_data) : NULL;
3874 if (!cfg) {
3875 ret = -EINVAL;
3876 goto out_free_data;
3877 }
3878
3879 ret = -ENOMEM;
3880 data = kzalloc_obj(*data);
3881 if (!data)
3882 goto out_free_data;
3883
3884 if (!AMD_IOMMU_GUEST_IR_GA(amd_iommu_guest_ir))
3885 data->entry = kzalloc_obj(union irte);
3886 else
3887 data->entry = kzalloc_obj(struct irte_ga);
3888 if (!data->entry) {
3889 kfree(data);
3890 goto out_free_data;
3891 }
3892
3893 data->iommu = iommu;
3894 irq_data->hwirq = (devid << 16) + i;
3895 irq_data->chip_data = data;
3896 irq_data->chip = &amd_ir_chip;
3897 irq_remapping_prepare_irte(data, cfg, info, devid, index, i);
3898 }
3899
3900 return 0;
3901
3902 out_free_data:
3903 for (i--; i >= 0; i--) {
3904 irq_data = irq_domain_get_irq_data(domain, virq + i);
3905 if (irq_data)
3906 kfree(irq_data->chip_data);
3907 }
3908 for (i = 0; i < nr_irqs; i++)
3909 free_irte(iommu, devid, index + i);
3910 out_free_parent:
3911 irq_domain_free_irqs_common(domain, virq, nr_irqs);
3912 return ret;
3913 }
3914
irq_remapping_free(struct irq_domain * domain,unsigned int virq,unsigned int nr_irqs)3915 static void irq_remapping_free(struct irq_domain *domain, unsigned int virq,
3916 unsigned int nr_irqs)
3917 {
3918 struct irq_2_irte *irte_info;
3919 struct irq_data *irq_data;
3920 struct amd_ir_data *data;
3921 int i;
3922
3923 for (i = 0; i < nr_irqs; i++) {
3924 irq_data = irq_domain_get_irq_data(domain, virq + i);
3925 if (irq_data && irq_data->chip_data) {
3926 data = irq_data->chip_data;
3927 irte_info = &data->irq_2_irte;
3928 free_irte(data->iommu, irte_info->devid, irte_info->index);
3929 kfree(data->entry);
3930 kfree(data);
3931 }
3932 }
3933 irq_domain_free_irqs_common(domain, virq, nr_irqs);
3934 }
3935
3936 static void amd_ir_update_irte(struct irq_data *irqd, struct amd_iommu *iommu,
3937 struct amd_ir_data *ir_data,
3938 struct irq_2_irte *irte_info,
3939 struct irq_cfg *cfg);
3940
irq_remapping_activate(struct irq_domain * domain,struct irq_data * irq_data,bool reserve)3941 static int irq_remapping_activate(struct irq_domain *domain,
3942 struct irq_data *irq_data, bool reserve)
3943 {
3944 struct amd_ir_data *data = irq_data->chip_data;
3945 struct irq_2_irte *irte_info = &data->irq_2_irte;
3946 struct amd_iommu *iommu = data->iommu;
3947 struct irq_cfg *cfg = irqd_cfg(irq_data);
3948
3949 if (!iommu)
3950 return 0;
3951
3952 iommu->irte_ops->activate(iommu, data->entry, irte_info->devid,
3953 irte_info->index);
3954 amd_ir_update_irte(irq_data, iommu, data, irte_info, cfg);
3955 return 0;
3956 }
3957
irq_remapping_deactivate(struct irq_domain * domain,struct irq_data * irq_data)3958 static void irq_remapping_deactivate(struct irq_domain *domain,
3959 struct irq_data *irq_data)
3960 {
3961 struct amd_ir_data *data = irq_data->chip_data;
3962 struct irq_2_irte *irte_info = &data->irq_2_irte;
3963 struct amd_iommu *iommu = data->iommu;
3964
3965 if (iommu)
3966 iommu->irte_ops->deactivate(iommu, data->entry, irte_info->devid,
3967 irte_info->index);
3968 }
3969
irq_remapping_select(struct irq_domain * d,struct irq_fwspec * fwspec,enum irq_domain_bus_token bus_token)3970 static int irq_remapping_select(struct irq_domain *d, struct irq_fwspec *fwspec,
3971 enum irq_domain_bus_token bus_token)
3972 {
3973 struct amd_iommu *iommu;
3974 int devid = -1;
3975
3976 if (x86_fwspec_is_ioapic(fwspec))
3977 devid = get_ioapic_devid(fwspec->param[0]);
3978 else if (x86_fwspec_is_hpet(fwspec))
3979 devid = get_hpet_devid(fwspec->param[0]);
3980
3981 if (devid < 0)
3982 return 0;
3983 iommu = __rlookup_amd_iommu((devid >> 16), (devid & 0xffff));
3984
3985 return iommu && iommu->ir_domain == d;
3986 }
3987
3988 static const struct irq_domain_ops amd_ir_domain_ops = {
3989 .select = irq_remapping_select,
3990 .alloc = irq_remapping_alloc,
3991 .free = irq_remapping_free,
3992 .activate = irq_remapping_activate,
3993 .deactivate = irq_remapping_deactivate,
3994 };
3995
__amd_iommu_update_ga(struct irte_ga * entry,int cpu,bool ga_log_intr)3996 static void __amd_iommu_update_ga(struct irte_ga *entry, int cpu,
3997 bool ga_log_intr)
3998 {
3999 if (cpu >= 0) {
4000 entry->lo.fields_vapic.destination =
4001 APICID_TO_IRTE_DEST_LO(cpu);
4002 entry->hi.fields.destination =
4003 APICID_TO_IRTE_DEST_HI(cpu);
4004 entry->lo.fields_vapic.is_run = true;
4005 entry->lo.fields_vapic.ga_log_intr = false;
4006 } else {
4007 entry->lo.fields_vapic.is_run = false;
4008 entry->lo.fields_vapic.ga_log_intr = ga_log_intr;
4009 }
4010 }
4011
4012 /*
4013 * Update the pCPU information for an IRTE that is configured to post IRQs to
4014 * a vCPU, without issuing an IOMMU invalidation for the IRTE.
4015 *
4016 * If the vCPU is associated with a pCPU (@cpu >= 0), configure the Destination
4017 * with the pCPU's APIC ID, set IsRun, and clear GALogIntr. If the vCPU isn't
4018 * associated with a pCPU (@cpu < 0), clear IsRun and set/clear GALogIntr based
4019 * on input from the caller (e.g. KVM only requests GALogIntr when the vCPU is
4020 * blocking and requires a notification wake event). I.e. treat vCPUs that are
4021 * associated with a pCPU as running. This API is intended to be used when a
4022 * vCPU is scheduled in/out (or stops running for any reason), to do a fast
4023 * update of IsRun, GALogIntr, and (conditionally) Destination.
4024 *
4025 * Per the IOMMU spec, the Destination, IsRun, and GATag fields are not cached
4026 * and thus don't require an invalidation to ensure the IOMMU consumes fresh
4027 * information.
4028 */
amd_iommu_update_ga(void * data,int cpu,bool ga_log_intr)4029 int amd_iommu_update_ga(void *data, int cpu, bool ga_log_intr)
4030 {
4031 struct amd_ir_data *ir_data = (struct amd_ir_data *)data;
4032 struct irte_ga *entry = (struct irte_ga *) ir_data->entry;
4033
4034 if (WARN_ON_ONCE(!AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir)))
4035 return -EINVAL;
4036
4037 if (!entry || !entry->lo.fields_vapic.guest_mode)
4038 return 0;
4039
4040 if (!ir_data->iommu)
4041 return -ENODEV;
4042
4043 __amd_iommu_update_ga(entry, cpu, ga_log_intr);
4044
4045 return __modify_irte_ga(ir_data->iommu, ir_data->irq_2_irte.devid,
4046 ir_data->irq_2_irte.index, entry);
4047 }
4048 EXPORT_SYMBOL(amd_iommu_update_ga);
4049
amd_iommu_activate_guest_mode(void * data,int cpu,bool ga_log_intr)4050 int amd_iommu_activate_guest_mode(void *data, int cpu, bool ga_log_intr)
4051 {
4052 struct amd_ir_data *ir_data = (struct amd_ir_data *)data;
4053 struct irte_ga *entry = (struct irte_ga *) ir_data->entry;
4054 u64 valid;
4055
4056 if (WARN_ON_ONCE(!AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir)))
4057 return -EINVAL;
4058
4059 if (!entry)
4060 return 0;
4061
4062 valid = entry->lo.fields_vapic.valid;
4063
4064 entry->lo.val = 0;
4065 entry->hi.val = 0;
4066
4067 entry->lo.fields_vapic.valid = valid;
4068 entry->lo.fields_vapic.guest_mode = 1;
4069 entry->hi.fields.ga_root_ptr = ir_data->ga_root_ptr;
4070 entry->hi.fields.vector = ir_data->ga_vector;
4071 entry->lo.fields_vapic.ga_tag = ir_data->ga_tag;
4072
4073 __amd_iommu_update_ga(entry, cpu, ga_log_intr);
4074
4075 return modify_irte_ga(ir_data->iommu, ir_data->irq_2_irte.devid,
4076 ir_data->irq_2_irte.index, entry);
4077 }
4078 EXPORT_SYMBOL(amd_iommu_activate_guest_mode);
4079
amd_iommu_deactivate_guest_mode(void * data)4080 int amd_iommu_deactivate_guest_mode(void *data)
4081 {
4082 struct amd_ir_data *ir_data = (struct amd_ir_data *)data;
4083 struct irte_ga *entry = (struct irte_ga *) ir_data->entry;
4084 struct irq_cfg *cfg = ir_data->cfg;
4085 u64 valid;
4086
4087 if (WARN_ON_ONCE(!AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir)))
4088 return -EINVAL;
4089
4090 if (!entry || !entry->lo.fields_vapic.guest_mode)
4091 return 0;
4092
4093 valid = entry->lo.fields_remap.valid;
4094
4095 entry->lo.val = 0;
4096 entry->hi.val = 0;
4097
4098 entry->lo.fields_remap.valid = valid;
4099 entry->lo.fields_remap.dm = apic->dest_mode_logical;
4100 entry->lo.fields_remap.int_type = APIC_DELIVERY_MODE_FIXED;
4101 entry->hi.fields.vector = cfg->vector;
4102 entry->lo.fields_remap.destination =
4103 APICID_TO_IRTE_DEST_LO(cfg->dest_apicid);
4104 entry->hi.fields.destination =
4105 APICID_TO_IRTE_DEST_HI(cfg->dest_apicid);
4106
4107 return modify_irte_ga(ir_data->iommu, ir_data->irq_2_irte.devid,
4108 ir_data->irq_2_irte.index, entry);
4109 }
4110 EXPORT_SYMBOL(amd_iommu_deactivate_guest_mode);
4111
amd_ir_set_vcpu_affinity(struct irq_data * data,void * info)4112 static int amd_ir_set_vcpu_affinity(struct irq_data *data, void *info)
4113 {
4114 int ret;
4115 struct amd_iommu_pi_data *pi_data = info;
4116 struct amd_ir_data *ir_data = data->chip_data;
4117 struct irq_2_irte *irte_info = &ir_data->irq_2_irte;
4118 struct iommu_dev_data *dev_data;
4119
4120 if (WARN_ON_ONCE(!AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir)))
4121 return -EINVAL;
4122
4123 if (ir_data->iommu == NULL)
4124 return -EINVAL;
4125
4126 dev_data = search_dev_data(ir_data->iommu, irte_info->devid);
4127
4128 /* Note:
4129 * This device has never been set up for guest mode.
4130 * we should not modify the IRTE
4131 */
4132 if (!dev_data || !dev_data->use_vapic)
4133 return -EINVAL;
4134
4135 ir_data->cfg = irqd_cfg(data);
4136
4137 if (pi_data) {
4138 pi_data->ir_data = ir_data;
4139
4140 ir_data->ga_root_ptr = (pi_data->vapic_addr >> 12);
4141 ir_data->ga_vector = pi_data->vector;
4142 ir_data->ga_tag = pi_data->ga_tag;
4143 if (pi_data->is_guest_mode)
4144 ret = amd_iommu_activate_guest_mode(ir_data, pi_data->cpu,
4145 pi_data->ga_log_intr);
4146 else
4147 ret = amd_iommu_deactivate_guest_mode(ir_data);
4148 } else {
4149 ret = amd_iommu_deactivate_guest_mode(ir_data);
4150 }
4151
4152 return ret;
4153 }
4154
4155
amd_ir_update_irte(struct irq_data * irqd,struct amd_iommu * iommu,struct amd_ir_data * ir_data,struct irq_2_irte * irte_info,struct irq_cfg * cfg)4156 static void amd_ir_update_irte(struct irq_data *irqd, struct amd_iommu *iommu,
4157 struct amd_ir_data *ir_data,
4158 struct irq_2_irte *irte_info,
4159 struct irq_cfg *cfg)
4160 {
4161
4162 /*
4163 * Atomically updates the IRTE with the new destination, vector
4164 * and flushes the interrupt entry cache.
4165 */
4166 iommu->irte_ops->set_affinity(iommu, ir_data->entry, irte_info->devid,
4167 irte_info->index, cfg->vector,
4168 cfg->dest_apicid);
4169 }
4170
amd_ir_set_affinity(struct irq_data * data,const struct cpumask * mask,bool force)4171 static int amd_ir_set_affinity(struct irq_data *data,
4172 const struct cpumask *mask, bool force)
4173 {
4174 struct amd_ir_data *ir_data = data->chip_data;
4175 struct irq_2_irte *irte_info = &ir_data->irq_2_irte;
4176 struct irq_cfg *cfg = irqd_cfg(data);
4177 struct irq_data *parent = data->parent_data;
4178 struct amd_iommu *iommu = ir_data->iommu;
4179 int ret;
4180
4181 if (!iommu)
4182 return -ENODEV;
4183
4184 ret = parent->chip->irq_set_affinity(parent, mask, force);
4185 if (ret < 0 || ret == IRQ_SET_MASK_OK_DONE)
4186 return ret;
4187
4188 amd_ir_update_irte(data, iommu, ir_data, irte_info, cfg);
4189 /*
4190 * After this point, all the interrupts will start arriving
4191 * at the new destination. So, time to cleanup the previous
4192 * vector allocation.
4193 */
4194 vector_schedule_cleanup(cfg);
4195
4196 return IRQ_SET_MASK_OK_DONE;
4197 }
4198
ir_compose_msi_msg(struct irq_data * irq_data,struct msi_msg * msg)4199 static void ir_compose_msi_msg(struct irq_data *irq_data, struct msi_msg *msg)
4200 {
4201 struct amd_ir_data *ir_data = irq_data->chip_data;
4202
4203 *msg = ir_data->msi_entry;
4204 }
4205
4206 static struct irq_chip amd_ir_chip = {
4207 .name = "AMD-IR",
4208 .irq_ack = apic_ack_irq,
4209 .irq_set_affinity = amd_ir_set_affinity,
4210 .irq_set_vcpu_affinity = amd_ir_set_vcpu_affinity,
4211 .irq_compose_msi_msg = ir_compose_msi_msg,
4212 };
4213
4214 static const struct msi_parent_ops amdvi_msi_parent_ops = {
4215 .supported_flags = X86_VECTOR_MSI_FLAGS_SUPPORTED | MSI_FLAG_MULTI_PCI_MSI,
4216 .bus_select_token = DOMAIN_BUS_AMDVI,
4217 .bus_select_mask = MATCH_PCI_MSI,
4218 .prefix = "IR-",
4219 .init_dev_msi_info = msi_parent_init_dev_msi_info,
4220 };
4221
amd_iommu_create_irq_domain(struct amd_iommu * iommu)4222 int amd_iommu_create_irq_domain(struct amd_iommu *iommu)
4223 {
4224 struct irq_domain_info info = {
4225 .fwnode = irq_domain_alloc_named_id_fwnode("AMD-IR", iommu->index),
4226 .ops = &amd_ir_domain_ops,
4227 .domain_flags = IRQ_DOMAIN_FLAG_ISOLATED_MSI,
4228 .host_data = iommu,
4229 .parent = arch_get_ir_parent_domain(),
4230 };
4231
4232 if (!info.fwnode)
4233 return -ENOMEM;
4234
4235 iommu->ir_domain = msi_create_parent_irq_domain(&info, &amdvi_msi_parent_ops);
4236 if (!iommu->ir_domain) {
4237 irq_domain_free_fwnode(info.fwnode);
4238 return -ENOMEM;
4239 }
4240 return 0;
4241 }
4242 #endif
4243
4244 MODULE_IMPORT_NS("GENERIC_PT_IOMMU");
4245