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/debugfs.h>
18 #include <linux/scatterlist.h>
19 #include <linux/dma-map-ops.h>
20 #include <linux/dma-direct.h>
21 #include <linux/iommu-helper.h>
22 #include <linux/delay.h>
23 #include <linux/amd-iommu.h>
24 #include <linux/notifier.h>
25 #include <linux/export.h>
26 #include <linux/irq.h>
27 #include <linux/msi.h>
28 #include <linux/irqdomain.h>
29 #include <linux/percpu.h>
30 #include <linux/io-pgtable.h>
31 #include <linux/cc_platform.h>
32 #include <asm/irq_remapping.h>
33 #include <asm/io_apic.h>
34 #include <asm/apic.h>
35 #include <asm/hw_irq.h>
36 #include <asm/proto.h>
37 #include <asm/iommu.h>
38 #include <asm/gart.h>
39 #include <asm/dma.h>
40
41 #include "amd_iommu.h"
42 #include "../dma-iommu.h"
43 #include "../irq_remapping.h"
44
45 #define CMD_SET_TYPE(cmd, t) ((cmd)->data[1] |= ((t) << 28))
46
47 #define LOOP_TIMEOUT 100000
48
49 /* IO virtual address start page frame number */
50 #define IOVA_START_PFN (1)
51 #define IOVA_PFN(addr) ((addr) >> PAGE_SHIFT)
52
53 /* Reserved IOVA ranges */
54 #define MSI_RANGE_START (0xfee00000)
55 #define MSI_RANGE_END (0xfeefffff)
56 #define HT_RANGE_START (0xfd00000000ULL)
57 #define HT_RANGE_END (0xffffffffffULL)
58
59 #define DEFAULT_PGTABLE_LEVEL PAGE_MODE_3_LEVEL
60
61 static DEFINE_SPINLOCK(pd_bitmap_lock);
62
63 LIST_HEAD(ioapic_map);
64 LIST_HEAD(hpet_map);
65 LIST_HEAD(acpihid_map);
66
67 const struct iommu_ops amd_iommu_ops;
68
69 static ATOMIC_NOTIFIER_HEAD(ppr_notifier);
70 int amd_iommu_max_glx_val = -1;
71
72 /*
73 * general struct to manage commands send to an IOMMU
74 */
75 struct iommu_cmd {
76 u32 data[4];
77 };
78
79 struct kmem_cache *amd_iommu_irq_cache;
80
81 static void detach_device(struct device *dev);
82 static int domain_enable_v2(struct protection_domain *domain, int pasids);
83
84 /****************************************************************************
85 *
86 * Helper functions
87 *
88 ****************************************************************************/
89
get_acpihid_device_id(struct device * dev,struct acpihid_map_entry ** entry)90 static inline int get_acpihid_device_id(struct device *dev,
91 struct acpihid_map_entry **entry)
92 {
93 struct acpi_device *adev = ACPI_COMPANION(dev);
94 struct acpihid_map_entry *p;
95
96 if (!adev)
97 return -ENODEV;
98
99 list_for_each_entry(p, &acpihid_map, list) {
100 if (acpi_dev_hid_uid_match(adev, p->hid,
101 p->uid[0] ? p->uid : NULL)) {
102 if (entry)
103 *entry = p;
104 return p->devid;
105 }
106 }
107 return -EINVAL;
108 }
109
get_device_sbdf_id(struct device * dev)110 static inline int get_device_sbdf_id(struct device *dev)
111 {
112 int sbdf;
113
114 if (dev_is_pci(dev))
115 sbdf = get_pci_sbdf_id(to_pci_dev(dev));
116 else
117 sbdf = get_acpihid_device_id(dev, NULL);
118
119 return sbdf;
120 }
121
get_dev_table(struct amd_iommu * iommu)122 struct dev_table_entry *get_dev_table(struct amd_iommu *iommu)
123 {
124 struct dev_table_entry *dev_table;
125 struct amd_iommu_pci_seg *pci_seg = iommu->pci_seg;
126
127 BUG_ON(pci_seg == NULL);
128 dev_table = pci_seg->dev_table;
129 BUG_ON(dev_table == NULL);
130
131 return dev_table;
132 }
133
get_device_segment(struct device * dev)134 static inline u16 get_device_segment(struct device *dev)
135 {
136 u16 seg;
137
138 if (dev_is_pci(dev)) {
139 struct pci_dev *pdev = to_pci_dev(dev);
140
141 seg = pci_domain_nr(pdev->bus);
142 } else {
143 u32 devid = get_acpihid_device_id(dev, NULL);
144
145 seg = PCI_SBDF_TO_SEGID(devid);
146 }
147
148 return seg;
149 }
150
151 /* Writes the specific IOMMU for a device into the PCI segment rlookup table */
amd_iommu_set_rlookup_table(struct amd_iommu * iommu,u16 devid)152 void amd_iommu_set_rlookup_table(struct amd_iommu *iommu, u16 devid)
153 {
154 struct amd_iommu_pci_seg *pci_seg = iommu->pci_seg;
155
156 pci_seg->rlookup_table[devid] = iommu;
157 }
158
__rlookup_amd_iommu(u16 seg,u16 devid)159 static struct amd_iommu *__rlookup_amd_iommu(u16 seg, u16 devid)
160 {
161 struct amd_iommu_pci_seg *pci_seg;
162
163 for_each_pci_segment(pci_seg) {
164 if (pci_seg->id == seg)
165 return pci_seg->rlookup_table[devid];
166 }
167 return NULL;
168 }
169
rlookup_amd_iommu(struct device * dev)170 static struct amd_iommu *rlookup_amd_iommu(struct device *dev)
171 {
172 u16 seg = get_device_segment(dev);
173 int devid = get_device_sbdf_id(dev);
174
175 if (devid < 0)
176 return NULL;
177 return __rlookup_amd_iommu(seg, PCI_SBDF_TO_DEVID(devid));
178 }
179
to_pdomain(struct iommu_domain * dom)180 static struct protection_domain *to_pdomain(struct iommu_domain *dom)
181 {
182 return container_of(dom, struct protection_domain, domain);
183 }
184
alloc_dev_data(struct amd_iommu * iommu,u16 devid)185 static struct iommu_dev_data *alloc_dev_data(struct amd_iommu *iommu, u16 devid)
186 {
187 struct iommu_dev_data *dev_data;
188 struct amd_iommu_pci_seg *pci_seg = iommu->pci_seg;
189
190 dev_data = kzalloc(sizeof(*dev_data), GFP_KERNEL);
191 if (!dev_data)
192 return NULL;
193
194 spin_lock_init(&dev_data->lock);
195 dev_data->devid = devid;
196 ratelimit_default_init(&dev_data->rs);
197
198 llist_add(&dev_data->dev_data_list, &pci_seg->dev_data_list);
199 return dev_data;
200 }
201
search_dev_data(struct amd_iommu * iommu,u16 devid)202 static struct iommu_dev_data *search_dev_data(struct amd_iommu *iommu, u16 devid)
203 {
204 struct iommu_dev_data *dev_data;
205 struct llist_node *node;
206 struct amd_iommu_pci_seg *pci_seg = iommu->pci_seg;
207
208 if (llist_empty(&pci_seg->dev_data_list))
209 return NULL;
210
211 node = pci_seg->dev_data_list.first;
212 llist_for_each_entry(dev_data, node, dev_data_list) {
213 if (dev_data->devid == devid)
214 return dev_data;
215 }
216
217 return NULL;
218 }
219
clone_alias(struct pci_dev * pdev,u16 alias,void * data)220 static int clone_alias(struct pci_dev *pdev, u16 alias, void *data)
221 {
222 struct amd_iommu *iommu;
223 struct dev_table_entry *dev_table;
224 u16 devid = pci_dev_id(pdev);
225
226 if (devid == alias)
227 return 0;
228
229 iommu = rlookup_amd_iommu(&pdev->dev);
230 if (!iommu)
231 return 0;
232
233 amd_iommu_set_rlookup_table(iommu, alias);
234 dev_table = get_dev_table(iommu);
235 memcpy(dev_table[alias].data,
236 dev_table[devid].data,
237 sizeof(dev_table[alias].data));
238
239 return 0;
240 }
241
clone_aliases(struct amd_iommu * iommu,struct device * dev)242 static void clone_aliases(struct amd_iommu *iommu, struct device *dev)
243 {
244 struct pci_dev *pdev;
245
246 if (!dev_is_pci(dev))
247 return;
248 pdev = to_pci_dev(dev);
249
250 /*
251 * The IVRS alias stored in the alias table may not be
252 * part of the PCI DMA aliases if it's bus differs
253 * from the original device.
254 */
255 clone_alias(pdev, iommu->pci_seg->alias_table[pci_dev_id(pdev)], NULL);
256
257 pci_for_each_dma_alias(pdev, clone_alias, NULL);
258 }
259
setup_aliases(struct amd_iommu * iommu,struct device * dev)260 static void setup_aliases(struct amd_iommu *iommu, struct device *dev)
261 {
262 struct pci_dev *pdev = to_pci_dev(dev);
263 struct amd_iommu_pci_seg *pci_seg = iommu->pci_seg;
264 u16 ivrs_alias;
265
266 /* For ACPI HID devices, there are no aliases */
267 if (!dev_is_pci(dev))
268 return;
269
270 /*
271 * Add the IVRS alias to the pci aliases if it is on the same
272 * bus. The IVRS table may know about a quirk that we don't.
273 */
274 ivrs_alias = pci_seg->alias_table[pci_dev_id(pdev)];
275 if (ivrs_alias != pci_dev_id(pdev) &&
276 PCI_BUS_NUM(ivrs_alias) == pdev->bus->number)
277 pci_add_dma_alias(pdev, ivrs_alias & 0xff, 1);
278
279 clone_aliases(iommu, dev);
280 }
281
find_dev_data(struct amd_iommu * iommu,u16 devid)282 static struct iommu_dev_data *find_dev_data(struct amd_iommu *iommu, u16 devid)
283 {
284 struct iommu_dev_data *dev_data;
285
286 dev_data = search_dev_data(iommu, devid);
287
288 if (dev_data == NULL) {
289 dev_data = alloc_dev_data(iommu, devid);
290 if (!dev_data)
291 return NULL;
292
293 if (translation_pre_enabled(iommu))
294 dev_data->defer_attach = true;
295 }
296
297 return dev_data;
298 }
299
300 /*
301 * Find or create an IOMMU group for a acpihid device.
302 */
acpihid_device_group(struct device * dev)303 static struct iommu_group *acpihid_device_group(struct device *dev)
304 {
305 struct acpihid_map_entry *p, *entry = NULL;
306 int devid;
307
308 devid = get_acpihid_device_id(dev, &entry);
309 if (devid < 0)
310 return ERR_PTR(devid);
311
312 list_for_each_entry(p, &acpihid_map, list) {
313 if ((devid == p->devid) && p->group)
314 entry->group = p->group;
315 }
316
317 if (!entry->group)
318 entry->group = generic_device_group(dev);
319 else
320 iommu_group_ref_get(entry->group);
321
322 return entry->group;
323 }
324
pci_iommuv2_capable(struct pci_dev * pdev)325 static bool pci_iommuv2_capable(struct pci_dev *pdev)
326 {
327 static const int caps[] = {
328 PCI_EXT_CAP_ID_PRI,
329 PCI_EXT_CAP_ID_PASID,
330 };
331 int i, pos;
332
333 if (!pci_ats_supported(pdev))
334 return false;
335
336 for (i = 0; i < 2; ++i) {
337 pos = pci_find_ext_capability(pdev, caps[i]);
338 if (pos == 0)
339 return false;
340 }
341
342 return true;
343 }
344
345 /*
346 * This function checks if the driver got a valid device from the caller to
347 * avoid dereferencing invalid pointers.
348 */
check_device(struct device * dev)349 static bool check_device(struct device *dev)
350 {
351 struct amd_iommu_pci_seg *pci_seg;
352 struct amd_iommu *iommu;
353 int devid, sbdf;
354
355 if (!dev)
356 return false;
357
358 sbdf = get_device_sbdf_id(dev);
359 if (sbdf < 0)
360 return false;
361 devid = PCI_SBDF_TO_DEVID(sbdf);
362
363 iommu = rlookup_amd_iommu(dev);
364 if (!iommu)
365 return false;
366
367 /* Out of our scope? */
368 pci_seg = iommu->pci_seg;
369 if (devid > pci_seg->last_bdf)
370 return false;
371
372 return true;
373 }
374
iommu_init_device(struct amd_iommu * iommu,struct device * dev)375 static int iommu_init_device(struct amd_iommu *iommu, struct device *dev)
376 {
377 struct iommu_dev_data *dev_data;
378 int devid, sbdf;
379
380 if (dev_iommu_priv_get(dev))
381 return 0;
382
383 sbdf = get_device_sbdf_id(dev);
384 if (sbdf < 0)
385 return sbdf;
386
387 devid = PCI_SBDF_TO_DEVID(sbdf);
388 dev_data = find_dev_data(iommu, devid);
389 if (!dev_data)
390 return -ENOMEM;
391
392 dev_data->dev = dev;
393 setup_aliases(iommu, dev);
394
395 /*
396 * By default we use passthrough mode for IOMMUv2 capable device.
397 * But if amd_iommu=force_isolation is set (e.g. to debug DMA to
398 * invalid address), we ignore the capability for the device so
399 * it'll be forced to go into translation mode.
400 */
401 if ((iommu_default_passthrough() || !amd_iommu_force_isolation) &&
402 dev_is_pci(dev) && pci_iommuv2_capable(to_pci_dev(dev))) {
403 dev_data->iommu_v2 = iommu->is_iommu_v2;
404 }
405
406 dev_iommu_priv_set(dev, dev_data);
407
408 return 0;
409 }
410
iommu_ignore_device(struct amd_iommu * iommu,struct device * dev)411 static void iommu_ignore_device(struct amd_iommu *iommu, struct device *dev)
412 {
413 struct amd_iommu_pci_seg *pci_seg = iommu->pci_seg;
414 struct dev_table_entry *dev_table = get_dev_table(iommu);
415 int devid, sbdf;
416
417 sbdf = get_device_sbdf_id(dev);
418 if (sbdf < 0)
419 return;
420
421 devid = PCI_SBDF_TO_DEVID(sbdf);
422 pci_seg->rlookup_table[devid] = NULL;
423 memset(&dev_table[devid], 0, sizeof(struct dev_table_entry));
424
425 setup_aliases(iommu, dev);
426 }
427
amd_iommu_uninit_device(struct device * dev)428 static void amd_iommu_uninit_device(struct device *dev)
429 {
430 struct iommu_dev_data *dev_data;
431
432 dev_data = dev_iommu_priv_get(dev);
433 if (!dev_data)
434 return;
435
436 if (dev_data->domain)
437 detach_device(dev);
438
439 dev_iommu_priv_set(dev, NULL);
440
441 /*
442 * We keep dev_data around for unplugged devices and reuse it when the
443 * device is re-plugged - not doing so would introduce a ton of races.
444 */
445 }
446
447 /****************************************************************************
448 *
449 * Interrupt handling functions
450 *
451 ****************************************************************************/
452
dump_dte_entry(struct amd_iommu * iommu,u16 devid)453 static void dump_dte_entry(struct amd_iommu *iommu, u16 devid)
454 {
455 int i;
456 struct dev_table_entry *dev_table = get_dev_table(iommu);
457
458 for (i = 0; i < 4; ++i)
459 pr_err("DTE[%d]: %016llx\n", i, dev_table[devid].data[i]);
460 }
461
dump_command(unsigned long phys_addr)462 static void dump_command(unsigned long phys_addr)
463 {
464 struct iommu_cmd *cmd = iommu_phys_to_virt(phys_addr);
465 int i;
466
467 for (i = 0; i < 4; ++i)
468 pr_err("CMD[%d]: %08x\n", i, cmd->data[i]);
469 }
470
amd_iommu_report_rmp_hw_error(struct amd_iommu * iommu,volatile u32 * event)471 static void amd_iommu_report_rmp_hw_error(struct amd_iommu *iommu, volatile u32 *event)
472 {
473 struct iommu_dev_data *dev_data = NULL;
474 int devid, vmg_tag, flags;
475 struct pci_dev *pdev;
476 u64 spa;
477
478 devid = (event[0] >> EVENT_DEVID_SHIFT) & EVENT_DEVID_MASK;
479 vmg_tag = (event[1]) & 0xFFFF;
480 flags = (event[1] >> EVENT_FLAGS_SHIFT) & EVENT_FLAGS_MASK;
481 spa = ((u64)event[3] << 32) | (event[2] & 0xFFFFFFF8);
482
483 pdev = pci_get_domain_bus_and_slot(iommu->pci_seg->id, PCI_BUS_NUM(devid),
484 devid & 0xff);
485 if (pdev)
486 dev_data = dev_iommu_priv_get(&pdev->dev);
487
488 if (dev_data) {
489 if (__ratelimit(&dev_data->rs)) {
490 pci_err(pdev, "Event logged [RMP_HW_ERROR vmg_tag=0x%04x, spa=0x%llx, flags=0x%04x]\n",
491 vmg_tag, spa, flags);
492 }
493 } else {
494 pr_err_ratelimited("Event logged [RMP_HW_ERROR device=%04x:%02x:%02x.%x, vmg_tag=0x%04x, spa=0x%llx, flags=0x%04x]\n",
495 iommu->pci_seg->id, PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
496 vmg_tag, spa, flags);
497 }
498
499 if (pdev)
500 pci_dev_put(pdev);
501 }
502
amd_iommu_report_rmp_fault(struct amd_iommu * iommu,volatile u32 * event)503 static void amd_iommu_report_rmp_fault(struct amd_iommu *iommu, volatile u32 *event)
504 {
505 struct iommu_dev_data *dev_data = NULL;
506 int devid, flags_rmp, vmg_tag, flags;
507 struct pci_dev *pdev;
508 u64 gpa;
509
510 devid = (event[0] >> EVENT_DEVID_SHIFT) & EVENT_DEVID_MASK;
511 flags_rmp = (event[0] >> EVENT_FLAGS_SHIFT) & 0xFF;
512 vmg_tag = (event[1]) & 0xFFFF;
513 flags = (event[1] >> EVENT_FLAGS_SHIFT) & EVENT_FLAGS_MASK;
514 gpa = ((u64)event[3] << 32) | event[2];
515
516 pdev = pci_get_domain_bus_and_slot(iommu->pci_seg->id, PCI_BUS_NUM(devid),
517 devid & 0xff);
518 if (pdev)
519 dev_data = dev_iommu_priv_get(&pdev->dev);
520
521 if (dev_data) {
522 if (__ratelimit(&dev_data->rs)) {
523 pci_err(pdev, "Event logged [RMP_PAGE_FAULT vmg_tag=0x%04x, gpa=0x%llx, flags_rmp=0x%04x, flags=0x%04x]\n",
524 vmg_tag, gpa, flags_rmp, flags);
525 }
526 } else {
527 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",
528 iommu->pci_seg->id, PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
529 vmg_tag, gpa, flags_rmp, flags);
530 }
531
532 if (pdev)
533 pci_dev_put(pdev);
534 }
535
536 #define IS_IOMMU_MEM_TRANSACTION(flags) \
537 (((flags) & EVENT_FLAG_I) == 0)
538
539 #define IS_WRITE_REQUEST(flags) \
540 ((flags) & EVENT_FLAG_RW)
541
amd_iommu_report_page_fault(struct amd_iommu * iommu,u16 devid,u16 domain_id,u64 address,int flags)542 static void amd_iommu_report_page_fault(struct amd_iommu *iommu,
543 u16 devid, u16 domain_id,
544 u64 address, int flags)
545 {
546 struct iommu_dev_data *dev_data = NULL;
547 struct pci_dev *pdev;
548
549 pdev = pci_get_domain_bus_and_slot(iommu->pci_seg->id, PCI_BUS_NUM(devid),
550 devid & 0xff);
551 if (pdev)
552 dev_data = dev_iommu_priv_get(&pdev->dev);
553
554 if (dev_data) {
555 /*
556 * If this is a DMA fault (for which the I(nterrupt)
557 * bit will be unset), allow report_iommu_fault() to
558 * prevent logging it.
559 */
560 if (IS_IOMMU_MEM_TRANSACTION(flags)) {
561 /* Device not attached to domain properly */
562 if (dev_data->domain == NULL) {
563 pr_err_ratelimited("Event logged [Device not attached to domain properly]\n");
564 pr_err_ratelimited(" device=%04x:%02x:%02x.%x domain=0x%04x\n",
565 iommu->pci_seg->id, PCI_BUS_NUM(devid), PCI_SLOT(devid),
566 PCI_FUNC(devid), domain_id);
567 goto out;
568 }
569
570 if (!report_iommu_fault(&dev_data->domain->domain,
571 &pdev->dev, address,
572 IS_WRITE_REQUEST(flags) ?
573 IOMMU_FAULT_WRITE :
574 IOMMU_FAULT_READ))
575 goto out;
576 }
577
578 if (__ratelimit(&dev_data->rs)) {
579 pci_err(pdev, "Event logged [IO_PAGE_FAULT domain=0x%04x address=0x%llx flags=0x%04x]\n",
580 domain_id, address, flags);
581 }
582 } else {
583 pr_err_ratelimited("Event logged [IO_PAGE_FAULT device=%04x:%02x:%02x.%x domain=0x%04x address=0x%llx flags=0x%04x]\n",
584 iommu->pci_seg->id, PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
585 domain_id, address, flags);
586 }
587
588 out:
589 if (pdev)
590 pci_dev_put(pdev);
591 }
592
iommu_print_event(struct amd_iommu * iommu,void * __evt)593 static void iommu_print_event(struct amd_iommu *iommu, void *__evt)
594 {
595 struct device *dev = iommu->iommu.dev;
596 int type, devid, flags, tag;
597 volatile u32 *event = __evt;
598 int count = 0;
599 u64 address;
600 u32 pasid;
601
602 retry:
603 type = (event[1] >> EVENT_TYPE_SHIFT) & EVENT_TYPE_MASK;
604 devid = (event[0] >> EVENT_DEVID_SHIFT) & EVENT_DEVID_MASK;
605 pasid = (event[0] & EVENT_DOMID_MASK_HI) |
606 (event[1] & EVENT_DOMID_MASK_LO);
607 flags = (event[1] >> EVENT_FLAGS_SHIFT) & EVENT_FLAGS_MASK;
608 address = (u64)(((u64)event[3]) << 32) | event[2];
609
610 if (type == 0) {
611 /* Did we hit the erratum? */
612 if (++count == LOOP_TIMEOUT) {
613 pr_err("No event written to event log\n");
614 return;
615 }
616 udelay(1);
617 goto retry;
618 }
619
620 if (type == EVENT_TYPE_IO_FAULT) {
621 amd_iommu_report_page_fault(iommu, devid, pasid, address, flags);
622 return;
623 }
624
625 switch (type) {
626 case EVENT_TYPE_ILL_DEV:
627 dev_err(dev, "Event logged [ILLEGAL_DEV_TABLE_ENTRY device=%04x:%02x:%02x.%x pasid=0x%05x address=0x%llx flags=0x%04x]\n",
628 iommu->pci_seg->id, PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
629 pasid, address, flags);
630 dump_dte_entry(iommu, devid);
631 break;
632 case EVENT_TYPE_DEV_TAB_ERR:
633 dev_err(dev, "Event logged [DEV_TAB_HARDWARE_ERROR device=%04x:%02x:%02x.%x "
634 "address=0x%llx flags=0x%04x]\n",
635 iommu->pci_seg->id, PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
636 address, flags);
637 break;
638 case EVENT_TYPE_PAGE_TAB_ERR:
639 dev_err(dev, "Event logged [PAGE_TAB_HARDWARE_ERROR device=%04x:%02x:%02x.%x pasid=0x%04x address=0x%llx flags=0x%04x]\n",
640 iommu->pci_seg->id, PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
641 pasid, address, flags);
642 break;
643 case EVENT_TYPE_ILL_CMD:
644 dev_err(dev, "Event logged [ILLEGAL_COMMAND_ERROR address=0x%llx]\n", address);
645 dump_command(address);
646 break;
647 case EVENT_TYPE_CMD_HARD_ERR:
648 dev_err(dev, "Event logged [COMMAND_HARDWARE_ERROR address=0x%llx flags=0x%04x]\n",
649 address, flags);
650 break;
651 case EVENT_TYPE_IOTLB_INV_TO:
652 dev_err(dev, "Event logged [IOTLB_INV_TIMEOUT device=%04x:%02x:%02x.%x address=0x%llx]\n",
653 iommu->pci_seg->id, PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
654 address);
655 break;
656 case EVENT_TYPE_INV_DEV_REQ:
657 dev_err(dev, "Event logged [INVALID_DEVICE_REQUEST device=%04x:%02x:%02x.%x pasid=0x%05x address=0x%llx flags=0x%04x]\n",
658 iommu->pci_seg->id, PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
659 pasid, address, flags);
660 break;
661 case EVENT_TYPE_RMP_FAULT:
662 amd_iommu_report_rmp_fault(iommu, event);
663 break;
664 case EVENT_TYPE_RMP_HW_ERR:
665 amd_iommu_report_rmp_hw_error(iommu, event);
666 break;
667 case EVENT_TYPE_INV_PPR_REQ:
668 pasid = PPR_PASID(*((u64 *)__evt));
669 tag = event[1] & 0x03FF;
670 dev_err(dev, "Event logged [INVALID_PPR_REQUEST device=%04x:%02x:%02x.%x pasid=0x%05x address=0x%llx flags=0x%04x tag=0x%03x]\n",
671 iommu->pci_seg->id, PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
672 pasid, address, flags, tag);
673 break;
674 default:
675 dev_err(dev, "Event logged [UNKNOWN event[0]=0x%08x event[1]=0x%08x event[2]=0x%08x event[3]=0x%08x\n",
676 event[0], event[1], event[2], event[3]);
677 }
678
679 memset(__evt, 0, 4 * sizeof(u32));
680 }
681
iommu_poll_events(struct amd_iommu * iommu)682 static void iommu_poll_events(struct amd_iommu *iommu)
683 {
684 u32 head, tail;
685
686 head = readl(iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
687 tail = readl(iommu->mmio_base + MMIO_EVT_TAIL_OFFSET);
688
689 while (head != tail) {
690 iommu_print_event(iommu, iommu->evt_buf + head);
691 head = (head + EVENT_ENTRY_SIZE) % EVT_BUFFER_SIZE;
692 }
693
694 writel(head, iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
695 }
696
iommu_handle_ppr_entry(struct amd_iommu * iommu,u64 * raw)697 static void iommu_handle_ppr_entry(struct amd_iommu *iommu, u64 *raw)
698 {
699 struct amd_iommu_fault fault;
700
701 if (PPR_REQ_TYPE(raw[0]) != PPR_REQ_FAULT) {
702 pr_err_ratelimited("Unknown PPR request received\n");
703 return;
704 }
705
706 fault.address = raw[1];
707 fault.pasid = PPR_PASID(raw[0]);
708 fault.sbdf = PCI_SEG_DEVID_TO_SBDF(iommu->pci_seg->id, PPR_DEVID(raw[0]));
709 fault.tag = PPR_TAG(raw[0]);
710 fault.flags = PPR_FLAGS(raw[0]);
711
712 atomic_notifier_call_chain(&ppr_notifier, 0, &fault);
713 }
714
iommu_poll_ppr_log(struct amd_iommu * iommu)715 static void iommu_poll_ppr_log(struct amd_iommu *iommu)
716 {
717 u32 head, tail;
718
719 if (iommu->ppr_log == NULL)
720 return;
721
722 head = readl(iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
723 tail = readl(iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
724
725 while (head != tail) {
726 volatile u64 *raw;
727 u64 entry[2];
728 int i;
729
730 raw = (u64 *)(iommu->ppr_log + head);
731
732 /*
733 * Hardware bug: Interrupt may arrive before the entry is
734 * written to memory. If this happens we need to wait for the
735 * entry to arrive.
736 */
737 for (i = 0; i < LOOP_TIMEOUT; ++i) {
738 if (PPR_REQ_TYPE(raw[0]) != 0)
739 break;
740 udelay(1);
741 }
742
743 /* Avoid memcpy function-call overhead */
744 entry[0] = raw[0];
745 entry[1] = raw[1];
746
747 /*
748 * To detect the hardware bug we need to clear the entry
749 * back to zero.
750 */
751 raw[0] = raw[1] = 0UL;
752
753 /* Update head pointer of hardware ring-buffer */
754 head = (head + PPR_ENTRY_SIZE) % PPR_LOG_SIZE;
755 writel(head, iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
756
757 /* Handle PPR entry */
758 iommu_handle_ppr_entry(iommu, entry);
759
760 /* Refresh ring-buffer information */
761 head = readl(iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
762 tail = readl(iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
763 }
764 }
765
766 #ifdef CONFIG_IRQ_REMAP
767 static int (*iommu_ga_log_notifier)(u32);
768
amd_iommu_register_ga_log_notifier(int (* notifier)(u32))769 int amd_iommu_register_ga_log_notifier(int (*notifier)(u32))
770 {
771 iommu_ga_log_notifier = notifier;
772
773 return 0;
774 }
775 EXPORT_SYMBOL(amd_iommu_register_ga_log_notifier);
776
iommu_poll_ga_log(struct amd_iommu * iommu)777 static void iommu_poll_ga_log(struct amd_iommu *iommu)
778 {
779 u32 head, tail, cnt = 0;
780
781 if (iommu->ga_log == NULL)
782 return;
783
784 head = readl(iommu->mmio_base + MMIO_GA_HEAD_OFFSET);
785 tail = readl(iommu->mmio_base + MMIO_GA_TAIL_OFFSET);
786
787 while (head != tail) {
788 volatile u64 *raw;
789 u64 log_entry;
790
791 raw = (u64 *)(iommu->ga_log + head);
792 cnt++;
793
794 /* Avoid memcpy function-call overhead */
795 log_entry = *raw;
796
797 /* Update head pointer of hardware ring-buffer */
798 head = (head + GA_ENTRY_SIZE) % GA_LOG_SIZE;
799 writel(head, iommu->mmio_base + MMIO_GA_HEAD_OFFSET);
800
801 /* Handle GA entry */
802 switch (GA_REQ_TYPE(log_entry)) {
803 case GA_GUEST_NR:
804 if (!iommu_ga_log_notifier)
805 break;
806
807 pr_debug("%s: devid=%#x, ga_tag=%#x\n",
808 __func__, GA_DEVID(log_entry),
809 GA_TAG(log_entry));
810
811 if (iommu_ga_log_notifier(GA_TAG(log_entry)) != 0)
812 pr_err("GA log notifier failed.\n");
813 break;
814 default:
815 break;
816 }
817 }
818 }
819
820 static void
amd_iommu_set_pci_msi_domain(struct device * dev,struct amd_iommu * iommu)821 amd_iommu_set_pci_msi_domain(struct device *dev, struct amd_iommu *iommu)
822 {
823 if (!irq_remapping_enabled || !dev_is_pci(dev) ||
824 pci_dev_has_special_msi_domain(to_pci_dev(dev)))
825 return;
826
827 dev_set_msi_domain(dev, iommu->msi_domain);
828 }
829
830 #else /* CONFIG_IRQ_REMAP */
831 static inline void
amd_iommu_set_pci_msi_domain(struct device * dev,struct amd_iommu * iommu)832 amd_iommu_set_pci_msi_domain(struct device *dev, struct amd_iommu *iommu) { }
833 #endif /* !CONFIG_IRQ_REMAP */
834
835 #define AMD_IOMMU_INT_MASK \
836 (MMIO_STATUS_EVT_OVERFLOW_INT_MASK | \
837 MMIO_STATUS_EVT_INT_MASK | \
838 MMIO_STATUS_PPR_INT_MASK | \
839 MMIO_STATUS_GALOG_OVERFLOW_MASK | \
840 MMIO_STATUS_GALOG_INT_MASK)
841
amd_iommu_int_thread(int irq,void * data)842 irqreturn_t amd_iommu_int_thread(int irq, void *data)
843 {
844 struct amd_iommu *iommu = (struct amd_iommu *) data;
845 u32 status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
846
847 while (status & AMD_IOMMU_INT_MASK) {
848 /* Enable interrupt sources again */
849 writel(AMD_IOMMU_INT_MASK,
850 iommu->mmio_base + MMIO_STATUS_OFFSET);
851
852 if (status & MMIO_STATUS_EVT_INT_MASK) {
853 pr_devel("Processing IOMMU Event Log\n");
854 iommu_poll_events(iommu);
855 }
856
857 if (status & MMIO_STATUS_PPR_INT_MASK) {
858 pr_devel("Processing IOMMU PPR Log\n");
859 iommu_poll_ppr_log(iommu);
860 }
861
862 #ifdef CONFIG_IRQ_REMAP
863 if (status & (MMIO_STATUS_GALOG_INT_MASK |
864 MMIO_STATUS_GALOG_OVERFLOW_MASK)) {
865 pr_devel("Processing IOMMU GA Log\n");
866 iommu_poll_ga_log(iommu);
867 }
868
869 if (status & MMIO_STATUS_GALOG_OVERFLOW_MASK) {
870 pr_info_ratelimited("IOMMU GA Log overflow\n");
871 amd_iommu_restart_ga_log(iommu);
872 }
873 #endif
874
875 if (status & MMIO_STATUS_EVT_OVERFLOW_INT_MASK) {
876 pr_info_ratelimited("IOMMU event log overflow\n");
877 amd_iommu_restart_event_logging(iommu);
878 }
879
880 /*
881 * Hardware bug: ERBT1312
882 * When re-enabling interrupt (by writing 1
883 * to clear the bit), the hardware might also try to set
884 * the interrupt bit in the event status register.
885 * In this scenario, the bit will be set, and disable
886 * subsequent interrupts.
887 *
888 * Workaround: The IOMMU driver should read back the
889 * status register and check if the interrupt bits are cleared.
890 * If not, driver will need to go through the interrupt handler
891 * again and re-clear the bits
892 */
893 status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
894 }
895 return IRQ_HANDLED;
896 }
897
amd_iommu_int_handler(int irq,void * data)898 irqreturn_t amd_iommu_int_handler(int irq, void *data)
899 {
900 return IRQ_WAKE_THREAD;
901 }
902
903 /****************************************************************************
904 *
905 * IOMMU command queuing functions
906 *
907 ****************************************************************************/
908
wait_on_sem(struct amd_iommu * iommu,u64 data)909 static int wait_on_sem(struct amd_iommu *iommu, u64 data)
910 {
911 int i = 0;
912
913 while (*iommu->cmd_sem != data && i < LOOP_TIMEOUT) {
914 udelay(1);
915 i += 1;
916 }
917
918 if (i == LOOP_TIMEOUT) {
919 pr_alert("Completion-Wait loop timed out\n");
920 return -EIO;
921 }
922
923 return 0;
924 }
925
copy_cmd_to_buffer(struct amd_iommu * iommu,struct iommu_cmd * cmd)926 static void copy_cmd_to_buffer(struct amd_iommu *iommu,
927 struct iommu_cmd *cmd)
928 {
929 u8 *target;
930 u32 tail;
931
932 /* Copy command to buffer */
933 tail = iommu->cmd_buf_tail;
934 target = iommu->cmd_buf + tail;
935 memcpy(target, cmd, sizeof(*cmd));
936
937 tail = (tail + sizeof(*cmd)) % CMD_BUFFER_SIZE;
938 iommu->cmd_buf_tail = tail;
939
940 /* Tell the IOMMU about it */
941 writel(tail, iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
942 }
943
build_completion_wait(struct iommu_cmd * cmd,struct amd_iommu * iommu,u64 data)944 static void build_completion_wait(struct iommu_cmd *cmd,
945 struct amd_iommu *iommu,
946 u64 data)
947 {
948 u64 paddr = iommu_virt_to_phys((void *)iommu->cmd_sem);
949
950 memset(cmd, 0, sizeof(*cmd));
951 cmd->data[0] = lower_32_bits(paddr) | CMD_COMPL_WAIT_STORE_MASK;
952 cmd->data[1] = upper_32_bits(paddr);
953 cmd->data[2] = lower_32_bits(data);
954 cmd->data[3] = upper_32_bits(data);
955 CMD_SET_TYPE(cmd, CMD_COMPL_WAIT);
956 }
957
build_inv_dte(struct iommu_cmd * cmd,u16 devid)958 static void build_inv_dte(struct iommu_cmd *cmd, u16 devid)
959 {
960 memset(cmd, 0, sizeof(*cmd));
961 cmd->data[0] = devid;
962 CMD_SET_TYPE(cmd, CMD_INV_DEV_ENTRY);
963 }
964
965 /*
966 * Builds an invalidation address which is suitable for one page or multiple
967 * pages. Sets the size bit (S) as needed is more than one page is flushed.
968 */
build_inv_address(u64 address,size_t size)969 static inline u64 build_inv_address(u64 address, size_t size)
970 {
971 u64 pages, end, msb_diff;
972
973 pages = iommu_num_pages(address, size, PAGE_SIZE);
974
975 if (pages == 1)
976 return address & PAGE_MASK;
977
978 end = address + size - 1;
979
980 /*
981 * msb_diff would hold the index of the most significant bit that
982 * flipped between the start and end.
983 */
984 msb_diff = fls64(end ^ address) - 1;
985
986 /*
987 * Bits 63:52 are sign extended. If for some reason bit 51 is different
988 * between the start and the end, invalidate everything.
989 */
990 if (unlikely(msb_diff > 51)) {
991 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
992 } else {
993 /*
994 * The msb-bit must be clear on the address. Just set all the
995 * lower bits.
996 */
997 address |= (1ull << msb_diff) - 1;
998 }
999
1000 /* Clear bits 11:0 */
1001 address &= PAGE_MASK;
1002
1003 /* Set the size bit - we flush more than one 4kb page */
1004 return address | CMD_INV_IOMMU_PAGES_SIZE_MASK;
1005 }
1006
build_inv_iommu_pages(struct iommu_cmd * cmd,u64 address,size_t size,u16 domid,int pde)1007 static void build_inv_iommu_pages(struct iommu_cmd *cmd, u64 address,
1008 size_t size, u16 domid, int pde)
1009 {
1010 u64 inv_address = build_inv_address(address, size);
1011
1012 memset(cmd, 0, sizeof(*cmd));
1013 cmd->data[1] |= domid;
1014 cmd->data[2] = lower_32_bits(inv_address);
1015 cmd->data[3] = upper_32_bits(inv_address);
1016 CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES);
1017 if (pde) /* PDE bit - we want to flush everything, not only the PTEs */
1018 cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
1019 }
1020
build_inv_iotlb_pages(struct iommu_cmd * cmd,u16 devid,int qdep,u64 address,size_t size)1021 static void build_inv_iotlb_pages(struct iommu_cmd *cmd, u16 devid, int qdep,
1022 u64 address, size_t size)
1023 {
1024 u64 inv_address = build_inv_address(address, size);
1025
1026 memset(cmd, 0, sizeof(*cmd));
1027 cmd->data[0] = devid;
1028 cmd->data[0] |= (qdep & 0xff) << 24;
1029 cmd->data[1] = devid;
1030 cmd->data[2] = lower_32_bits(inv_address);
1031 cmd->data[3] = upper_32_bits(inv_address);
1032 CMD_SET_TYPE(cmd, CMD_INV_IOTLB_PAGES);
1033 }
1034
build_inv_iommu_pasid(struct iommu_cmd * cmd,u16 domid,u32 pasid,u64 address,bool size)1035 static void build_inv_iommu_pasid(struct iommu_cmd *cmd, u16 domid, u32 pasid,
1036 u64 address, bool size)
1037 {
1038 memset(cmd, 0, sizeof(*cmd));
1039
1040 address &= ~(0xfffULL);
1041
1042 cmd->data[0] = pasid;
1043 cmd->data[1] = domid;
1044 cmd->data[2] = lower_32_bits(address);
1045 cmd->data[3] = upper_32_bits(address);
1046 cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
1047 cmd->data[2] |= CMD_INV_IOMMU_PAGES_GN_MASK;
1048 if (size)
1049 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
1050 CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES);
1051 }
1052
build_inv_iotlb_pasid(struct iommu_cmd * cmd,u16 devid,u32 pasid,int qdep,u64 address,bool size)1053 static void build_inv_iotlb_pasid(struct iommu_cmd *cmd, u16 devid, u32 pasid,
1054 int qdep, u64 address, bool size)
1055 {
1056 memset(cmd, 0, sizeof(*cmd));
1057
1058 address &= ~(0xfffULL);
1059
1060 cmd->data[0] = devid;
1061 cmd->data[0] |= ((pasid >> 8) & 0xff) << 16;
1062 cmd->data[0] |= (qdep & 0xff) << 24;
1063 cmd->data[1] = devid;
1064 cmd->data[1] |= (pasid & 0xff) << 16;
1065 cmd->data[2] = lower_32_bits(address);
1066 cmd->data[2] |= CMD_INV_IOMMU_PAGES_GN_MASK;
1067 cmd->data[3] = upper_32_bits(address);
1068 if (size)
1069 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
1070 CMD_SET_TYPE(cmd, CMD_INV_IOTLB_PAGES);
1071 }
1072
build_complete_ppr(struct iommu_cmd * cmd,u16 devid,u32 pasid,int status,int tag,bool gn)1073 static void build_complete_ppr(struct iommu_cmd *cmd, u16 devid, u32 pasid,
1074 int status, int tag, bool gn)
1075 {
1076 memset(cmd, 0, sizeof(*cmd));
1077
1078 cmd->data[0] = devid;
1079 if (gn) {
1080 cmd->data[1] = pasid;
1081 cmd->data[2] = CMD_INV_IOMMU_PAGES_GN_MASK;
1082 }
1083 cmd->data[3] = tag & 0x1ff;
1084 cmd->data[3] |= (status & PPR_STATUS_MASK) << PPR_STATUS_SHIFT;
1085
1086 CMD_SET_TYPE(cmd, CMD_COMPLETE_PPR);
1087 }
1088
build_inv_all(struct iommu_cmd * cmd)1089 static void build_inv_all(struct iommu_cmd *cmd)
1090 {
1091 memset(cmd, 0, sizeof(*cmd));
1092 CMD_SET_TYPE(cmd, CMD_INV_ALL);
1093 }
1094
build_inv_irt(struct iommu_cmd * cmd,u16 devid)1095 static void build_inv_irt(struct iommu_cmd *cmd, u16 devid)
1096 {
1097 memset(cmd, 0, sizeof(*cmd));
1098 cmd->data[0] = devid;
1099 CMD_SET_TYPE(cmd, CMD_INV_IRT);
1100 }
1101
1102 /*
1103 * Writes the command to the IOMMUs command buffer and informs the
1104 * hardware about the new command.
1105 */
__iommu_queue_command_sync(struct amd_iommu * iommu,struct iommu_cmd * cmd,bool sync)1106 static int __iommu_queue_command_sync(struct amd_iommu *iommu,
1107 struct iommu_cmd *cmd,
1108 bool sync)
1109 {
1110 unsigned int count = 0;
1111 u32 left, next_tail;
1112
1113 next_tail = (iommu->cmd_buf_tail + sizeof(*cmd)) % CMD_BUFFER_SIZE;
1114 again:
1115 left = (iommu->cmd_buf_head - next_tail) % CMD_BUFFER_SIZE;
1116
1117 if (left <= 0x20) {
1118 /* Skip udelay() the first time around */
1119 if (count++) {
1120 if (count == LOOP_TIMEOUT) {
1121 pr_err("Command buffer timeout\n");
1122 return -EIO;
1123 }
1124
1125 udelay(1);
1126 }
1127
1128 /* Update head and recheck remaining space */
1129 iommu->cmd_buf_head = readl(iommu->mmio_base +
1130 MMIO_CMD_HEAD_OFFSET);
1131
1132 goto again;
1133 }
1134
1135 copy_cmd_to_buffer(iommu, cmd);
1136
1137 /* Do we need to make sure all commands are processed? */
1138 iommu->need_sync = sync;
1139
1140 return 0;
1141 }
1142
iommu_queue_command_sync(struct amd_iommu * iommu,struct iommu_cmd * cmd,bool sync)1143 static int iommu_queue_command_sync(struct amd_iommu *iommu,
1144 struct iommu_cmd *cmd,
1145 bool sync)
1146 {
1147 unsigned long flags;
1148 int ret;
1149
1150 raw_spin_lock_irqsave(&iommu->lock, flags);
1151 ret = __iommu_queue_command_sync(iommu, cmd, sync);
1152 raw_spin_unlock_irqrestore(&iommu->lock, flags);
1153
1154 return ret;
1155 }
1156
iommu_queue_command(struct amd_iommu * iommu,struct iommu_cmd * cmd)1157 static int iommu_queue_command(struct amd_iommu *iommu, struct iommu_cmd *cmd)
1158 {
1159 return iommu_queue_command_sync(iommu, cmd, true);
1160 }
1161
1162 /*
1163 * This function queues a completion wait command into the command
1164 * buffer of an IOMMU
1165 */
iommu_completion_wait(struct amd_iommu * iommu)1166 static int iommu_completion_wait(struct amd_iommu *iommu)
1167 {
1168 struct iommu_cmd cmd;
1169 unsigned long flags;
1170 int ret;
1171 u64 data;
1172
1173 if (!iommu->need_sync)
1174 return 0;
1175
1176 raw_spin_lock_irqsave(&iommu->lock, flags);
1177
1178 data = ++iommu->cmd_sem_val;
1179 build_completion_wait(&cmd, iommu, data);
1180
1181 ret = __iommu_queue_command_sync(iommu, &cmd, false);
1182 if (ret)
1183 goto out_unlock;
1184
1185 ret = wait_on_sem(iommu, data);
1186
1187 out_unlock:
1188 raw_spin_unlock_irqrestore(&iommu->lock, flags);
1189
1190 return ret;
1191 }
1192
iommu_flush_dte(struct amd_iommu * iommu,u16 devid)1193 static int iommu_flush_dte(struct amd_iommu *iommu, u16 devid)
1194 {
1195 struct iommu_cmd cmd;
1196
1197 build_inv_dte(&cmd, devid);
1198
1199 return iommu_queue_command(iommu, &cmd);
1200 }
1201
amd_iommu_flush_dte_all(struct amd_iommu * iommu)1202 static void amd_iommu_flush_dte_all(struct amd_iommu *iommu)
1203 {
1204 u32 devid;
1205 u16 last_bdf = iommu->pci_seg->last_bdf;
1206
1207 for (devid = 0; devid <= last_bdf; ++devid)
1208 iommu_flush_dte(iommu, devid);
1209
1210 iommu_completion_wait(iommu);
1211 }
1212
1213 /*
1214 * This function uses heavy locking and may disable irqs for some time. But
1215 * this is no issue because it is only called during resume.
1216 */
amd_iommu_flush_tlb_all(struct amd_iommu * iommu)1217 static void amd_iommu_flush_tlb_all(struct amd_iommu *iommu)
1218 {
1219 u32 dom_id;
1220 u16 last_bdf = iommu->pci_seg->last_bdf;
1221
1222 for (dom_id = 0; dom_id <= last_bdf; ++dom_id) {
1223 struct iommu_cmd cmd;
1224 build_inv_iommu_pages(&cmd, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
1225 dom_id, 1);
1226 iommu_queue_command(iommu, &cmd);
1227 }
1228
1229 iommu_completion_wait(iommu);
1230 }
1231
amd_iommu_flush_tlb_domid(struct amd_iommu * iommu,u32 dom_id)1232 static void amd_iommu_flush_tlb_domid(struct amd_iommu *iommu, u32 dom_id)
1233 {
1234 struct iommu_cmd cmd;
1235
1236 build_inv_iommu_pages(&cmd, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
1237 dom_id, 1);
1238 iommu_queue_command(iommu, &cmd);
1239
1240 iommu_completion_wait(iommu);
1241 }
1242
amd_iommu_flush_all(struct amd_iommu * iommu)1243 static void amd_iommu_flush_all(struct amd_iommu *iommu)
1244 {
1245 struct iommu_cmd cmd;
1246
1247 build_inv_all(&cmd);
1248
1249 iommu_queue_command(iommu, &cmd);
1250 iommu_completion_wait(iommu);
1251 }
1252
iommu_flush_irt(struct amd_iommu * iommu,u16 devid)1253 static void iommu_flush_irt(struct amd_iommu *iommu, u16 devid)
1254 {
1255 struct iommu_cmd cmd;
1256
1257 build_inv_irt(&cmd, devid);
1258
1259 iommu_queue_command(iommu, &cmd);
1260 }
1261
amd_iommu_flush_irt_all(struct amd_iommu * iommu)1262 static void amd_iommu_flush_irt_all(struct amd_iommu *iommu)
1263 {
1264 u32 devid;
1265 u16 last_bdf = iommu->pci_seg->last_bdf;
1266
1267 for (devid = 0; devid <= last_bdf; devid++)
1268 iommu_flush_irt(iommu, devid);
1269
1270 iommu_completion_wait(iommu);
1271 }
1272
iommu_flush_all_caches(struct amd_iommu * iommu)1273 void iommu_flush_all_caches(struct amd_iommu *iommu)
1274 {
1275 if (iommu_feature(iommu, FEATURE_IA)) {
1276 amd_iommu_flush_all(iommu);
1277 } else {
1278 amd_iommu_flush_dte_all(iommu);
1279 amd_iommu_flush_irt_all(iommu);
1280 amd_iommu_flush_tlb_all(iommu);
1281 }
1282 }
1283
1284 /*
1285 * Command send function for flushing on-device TLB
1286 */
device_flush_iotlb(struct iommu_dev_data * dev_data,u64 address,size_t size)1287 static int device_flush_iotlb(struct iommu_dev_data *dev_data,
1288 u64 address, size_t size)
1289 {
1290 struct amd_iommu *iommu;
1291 struct iommu_cmd cmd;
1292 int qdep;
1293
1294 qdep = dev_data->ats.qdep;
1295 iommu = rlookup_amd_iommu(dev_data->dev);
1296 if (!iommu)
1297 return -EINVAL;
1298
1299 build_inv_iotlb_pages(&cmd, dev_data->devid, qdep, address, size);
1300
1301 return iommu_queue_command(iommu, &cmd);
1302 }
1303
device_flush_dte_alias(struct pci_dev * pdev,u16 alias,void * data)1304 static int device_flush_dte_alias(struct pci_dev *pdev, u16 alias, void *data)
1305 {
1306 struct amd_iommu *iommu = data;
1307
1308 return iommu_flush_dte(iommu, alias);
1309 }
1310
1311 /*
1312 * Command send function for invalidating a device table entry
1313 */
device_flush_dte(struct iommu_dev_data * dev_data)1314 static int device_flush_dte(struct iommu_dev_data *dev_data)
1315 {
1316 struct amd_iommu *iommu;
1317 struct pci_dev *pdev = NULL;
1318 struct amd_iommu_pci_seg *pci_seg;
1319 u16 alias;
1320 int ret;
1321
1322 iommu = rlookup_amd_iommu(dev_data->dev);
1323 if (!iommu)
1324 return -EINVAL;
1325
1326 if (dev_is_pci(dev_data->dev))
1327 pdev = to_pci_dev(dev_data->dev);
1328
1329 if (pdev)
1330 ret = pci_for_each_dma_alias(pdev,
1331 device_flush_dte_alias, iommu);
1332 else
1333 ret = iommu_flush_dte(iommu, dev_data->devid);
1334 if (ret)
1335 return ret;
1336
1337 pci_seg = iommu->pci_seg;
1338 alias = pci_seg->alias_table[dev_data->devid];
1339 if (alias != dev_data->devid) {
1340 ret = iommu_flush_dte(iommu, alias);
1341 if (ret)
1342 return ret;
1343 }
1344
1345 if (dev_data->ats.enabled)
1346 ret = device_flush_iotlb(dev_data, 0, ~0UL);
1347
1348 return ret;
1349 }
1350
1351 /*
1352 * TLB invalidation function which is called from the mapping functions.
1353 * It invalidates a single PTE if the range to flush is within a single
1354 * page. Otherwise it flushes the whole TLB of the IOMMU.
1355 */
__domain_flush_pages(struct protection_domain * domain,u64 address,size_t size,int pde)1356 static void __domain_flush_pages(struct protection_domain *domain,
1357 u64 address, size_t size, int pde)
1358 {
1359 struct iommu_dev_data *dev_data;
1360 struct iommu_cmd cmd;
1361 int ret = 0, i;
1362
1363 build_inv_iommu_pages(&cmd, address, size, domain->id, pde);
1364
1365 for (i = 0; i < amd_iommu_get_num_iommus(); ++i) {
1366 if (!domain->dev_iommu[i])
1367 continue;
1368
1369 /*
1370 * Devices of this domain are behind this IOMMU
1371 * We need a TLB flush
1372 */
1373 ret |= iommu_queue_command(amd_iommus[i], &cmd);
1374 }
1375
1376 list_for_each_entry(dev_data, &domain->dev_list, list) {
1377
1378 if (!dev_data->ats.enabled)
1379 continue;
1380
1381 ret |= device_flush_iotlb(dev_data, address, size);
1382 }
1383
1384 WARN_ON(ret);
1385 }
1386
domain_flush_pages(struct protection_domain * domain,u64 address,size_t size,int pde)1387 static void domain_flush_pages(struct protection_domain *domain,
1388 u64 address, size_t size, int pde)
1389 {
1390 if (likely(!amd_iommu_np_cache)) {
1391 __domain_flush_pages(domain, address, size, pde);
1392 return;
1393 }
1394
1395 /*
1396 * When NpCache is on, we infer that we run in a VM and use a vIOMMU.
1397 * In such setups it is best to avoid flushes of ranges which are not
1398 * naturally aligned, since it would lead to flushes of unmodified
1399 * PTEs. Such flushes would require the hypervisor to do more work than
1400 * necessary. Therefore, perform repeated flushes of aligned ranges
1401 * until you cover the range. Each iteration flushes the smaller
1402 * between the natural alignment of the address that we flush and the
1403 * greatest naturally aligned region that fits in the range.
1404 */
1405 while (size != 0) {
1406 int addr_alignment = __ffs(address);
1407 int size_alignment = __fls(size);
1408 int min_alignment;
1409 size_t flush_size;
1410
1411 /*
1412 * size is always non-zero, but address might be zero, causing
1413 * addr_alignment to be negative. As the casting of the
1414 * argument in __ffs(address) to long might trim the high bits
1415 * of the address on x86-32, cast to long when doing the check.
1416 */
1417 if (likely((unsigned long)address != 0))
1418 min_alignment = min(addr_alignment, size_alignment);
1419 else
1420 min_alignment = size_alignment;
1421
1422 flush_size = 1ul << min_alignment;
1423
1424 __domain_flush_pages(domain, address, flush_size, pde);
1425 address += flush_size;
1426 size -= flush_size;
1427 }
1428 }
1429
1430 /* Flush the whole IO/TLB for a given protection domain - including PDE */
amd_iommu_domain_flush_tlb_pde(struct protection_domain * domain)1431 void amd_iommu_domain_flush_tlb_pde(struct protection_domain *domain)
1432 {
1433 domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 1);
1434 }
1435
amd_iommu_domain_flush_complete(struct protection_domain * domain)1436 void amd_iommu_domain_flush_complete(struct protection_domain *domain)
1437 {
1438 int i;
1439
1440 for (i = 0; i < amd_iommu_get_num_iommus(); ++i) {
1441 if (domain && !domain->dev_iommu[i])
1442 continue;
1443
1444 /*
1445 * Devices of this domain are behind this IOMMU
1446 * We need to wait for completion of all commands.
1447 */
1448 iommu_completion_wait(amd_iommus[i]);
1449 }
1450 }
1451
1452 /* Flush the not present cache if it exists */
domain_flush_np_cache(struct protection_domain * domain,dma_addr_t iova,size_t size)1453 static void domain_flush_np_cache(struct protection_domain *domain,
1454 dma_addr_t iova, size_t size)
1455 {
1456 if (unlikely(amd_iommu_np_cache)) {
1457 unsigned long flags;
1458
1459 spin_lock_irqsave(&domain->lock, flags);
1460 domain_flush_pages(domain, iova, size, 1);
1461 amd_iommu_domain_flush_complete(domain);
1462 spin_unlock_irqrestore(&domain->lock, flags);
1463 }
1464 }
1465
1466
1467 /*
1468 * This function flushes the DTEs for all devices in domain
1469 */
domain_flush_devices(struct protection_domain * domain)1470 static void domain_flush_devices(struct protection_domain *domain)
1471 {
1472 struct iommu_dev_data *dev_data;
1473
1474 list_for_each_entry(dev_data, &domain->dev_list, list)
1475 device_flush_dte(dev_data);
1476 }
1477
1478 /****************************************************************************
1479 *
1480 * The next functions belong to the domain allocation. A domain is
1481 * allocated for every IOMMU as the default domain. If device isolation
1482 * is enabled, every device get its own domain. The most important thing
1483 * about domains is the page table mapping the DMA address space they
1484 * contain.
1485 *
1486 ****************************************************************************/
1487
domain_id_alloc(void)1488 static u16 domain_id_alloc(void)
1489 {
1490 int id;
1491
1492 spin_lock(&pd_bitmap_lock);
1493 id = find_first_zero_bit(amd_iommu_pd_alloc_bitmap, MAX_DOMAIN_ID);
1494 BUG_ON(id == 0);
1495 if (id > 0 && id < MAX_DOMAIN_ID)
1496 __set_bit(id, amd_iommu_pd_alloc_bitmap);
1497 else
1498 id = 0;
1499 spin_unlock(&pd_bitmap_lock);
1500
1501 return id;
1502 }
1503
domain_id_free(int id)1504 static void domain_id_free(int id)
1505 {
1506 spin_lock(&pd_bitmap_lock);
1507 if (id > 0 && id < MAX_DOMAIN_ID)
1508 __clear_bit(id, amd_iommu_pd_alloc_bitmap);
1509 spin_unlock(&pd_bitmap_lock);
1510 }
1511
free_gcr3_tbl_level1(u64 * tbl)1512 static void free_gcr3_tbl_level1(u64 *tbl)
1513 {
1514 u64 *ptr;
1515 int i;
1516
1517 for (i = 0; i < 512; ++i) {
1518 if (!(tbl[i] & GCR3_VALID))
1519 continue;
1520
1521 ptr = iommu_phys_to_virt(tbl[i] & PAGE_MASK);
1522
1523 free_page((unsigned long)ptr);
1524 }
1525 }
1526
free_gcr3_tbl_level2(u64 * tbl)1527 static void free_gcr3_tbl_level2(u64 *tbl)
1528 {
1529 u64 *ptr;
1530 int i;
1531
1532 for (i = 0; i < 512; ++i) {
1533 if (!(tbl[i] & GCR3_VALID))
1534 continue;
1535
1536 ptr = iommu_phys_to_virt(tbl[i] & PAGE_MASK);
1537
1538 free_gcr3_tbl_level1(ptr);
1539 }
1540 }
1541
free_gcr3_table(struct protection_domain * domain)1542 static void free_gcr3_table(struct protection_domain *domain)
1543 {
1544 if (domain->glx == 2)
1545 free_gcr3_tbl_level2(domain->gcr3_tbl);
1546 else if (domain->glx == 1)
1547 free_gcr3_tbl_level1(domain->gcr3_tbl);
1548 else
1549 BUG_ON(domain->glx != 0);
1550
1551 free_page((unsigned long)domain->gcr3_tbl);
1552 }
1553
set_dte_entry(struct amd_iommu * iommu,u16 devid,struct protection_domain * domain,bool ats,bool ppr)1554 static void set_dte_entry(struct amd_iommu *iommu, u16 devid,
1555 struct protection_domain *domain, bool ats, bool ppr)
1556 {
1557 u64 pte_root = 0;
1558 u64 flags = 0;
1559 u32 old_domid;
1560 struct dev_table_entry *dev_table = get_dev_table(iommu);
1561
1562 if (domain->iop.mode != PAGE_MODE_NONE)
1563 pte_root = iommu_virt_to_phys(domain->iop.root);
1564
1565 pte_root |= (domain->iop.mode & DEV_ENTRY_MODE_MASK)
1566 << DEV_ENTRY_MODE_SHIFT;
1567
1568 pte_root |= DTE_FLAG_IR | DTE_FLAG_IW | DTE_FLAG_V;
1569
1570 /*
1571 * When SNP is enabled, Only set TV bit when IOMMU
1572 * page translation is in use.
1573 */
1574 if (!amd_iommu_snp_en || (domain->id != 0))
1575 pte_root |= DTE_FLAG_TV;
1576
1577 flags = dev_table[devid].data[1];
1578
1579 if (ats)
1580 flags |= DTE_FLAG_IOTLB;
1581
1582 if (ppr) {
1583 if (iommu_feature(iommu, FEATURE_EPHSUP))
1584 pte_root |= 1ULL << DEV_ENTRY_PPR;
1585 }
1586
1587 if (domain->flags & PD_IOMMUV2_MASK) {
1588 u64 gcr3 = iommu_virt_to_phys(domain->gcr3_tbl);
1589 u64 glx = domain->glx;
1590 u64 tmp;
1591
1592 pte_root |= DTE_FLAG_GV;
1593 pte_root |= (glx & DTE_GLX_MASK) << DTE_GLX_SHIFT;
1594
1595 /* First mask out possible old values for GCR3 table */
1596 tmp = DTE_GCR3_VAL_B(~0ULL) << DTE_GCR3_SHIFT_B;
1597 flags &= ~tmp;
1598
1599 tmp = DTE_GCR3_VAL_C(~0ULL) << DTE_GCR3_SHIFT_C;
1600 flags &= ~tmp;
1601
1602 /* Encode GCR3 table into DTE */
1603 tmp = DTE_GCR3_VAL_A(gcr3) << DTE_GCR3_SHIFT_A;
1604 pte_root |= tmp;
1605
1606 tmp = DTE_GCR3_VAL_B(gcr3) << DTE_GCR3_SHIFT_B;
1607 flags |= tmp;
1608
1609 tmp = DTE_GCR3_VAL_C(gcr3) << DTE_GCR3_SHIFT_C;
1610 flags |= tmp;
1611
1612 if (domain->flags & PD_GIOV_MASK)
1613 pte_root |= DTE_FLAG_GIOV;
1614 }
1615
1616 flags &= ~DEV_DOMID_MASK;
1617 flags |= domain->id;
1618
1619 old_domid = dev_table[devid].data[1] & DEV_DOMID_MASK;
1620 dev_table[devid].data[1] = flags;
1621 dev_table[devid].data[0] = pte_root;
1622
1623 /*
1624 * A kdump kernel might be replacing a domain ID that was copied from
1625 * the previous kernel--if so, it needs to flush the translation cache
1626 * entries for the old domain ID that is being overwritten
1627 */
1628 if (old_domid) {
1629 amd_iommu_flush_tlb_domid(iommu, old_domid);
1630 }
1631 }
1632
clear_dte_entry(struct amd_iommu * iommu,u16 devid)1633 static void clear_dte_entry(struct amd_iommu *iommu, u16 devid)
1634 {
1635 struct dev_table_entry *dev_table = get_dev_table(iommu);
1636
1637 /* remove entry from the device table seen by the hardware */
1638 dev_table[devid].data[0] = DTE_FLAG_V;
1639
1640 if (!amd_iommu_snp_en)
1641 dev_table[devid].data[0] |= DTE_FLAG_TV;
1642
1643 dev_table[devid].data[1] &= DTE_FLAG_MASK;
1644
1645 amd_iommu_apply_erratum_63(iommu, devid);
1646 }
1647
do_attach(struct iommu_dev_data * dev_data,struct protection_domain * domain)1648 static void do_attach(struct iommu_dev_data *dev_data,
1649 struct protection_domain *domain)
1650 {
1651 struct amd_iommu *iommu;
1652 bool ats;
1653
1654 iommu = rlookup_amd_iommu(dev_data->dev);
1655 if (!iommu)
1656 return;
1657 ats = dev_data->ats.enabled;
1658
1659 /* Update data structures */
1660 dev_data->domain = domain;
1661 list_add(&dev_data->list, &domain->dev_list);
1662
1663 /* Do reference counting */
1664 domain->dev_iommu[iommu->index] += 1;
1665 domain->dev_cnt += 1;
1666
1667 /* Update device table */
1668 set_dte_entry(iommu, dev_data->devid, domain,
1669 ats, dev_data->iommu_v2);
1670 clone_aliases(iommu, dev_data->dev);
1671
1672 device_flush_dte(dev_data);
1673 }
1674
do_detach(struct iommu_dev_data * dev_data)1675 static void do_detach(struct iommu_dev_data *dev_data)
1676 {
1677 struct protection_domain *domain = dev_data->domain;
1678 struct amd_iommu *iommu;
1679
1680 iommu = rlookup_amd_iommu(dev_data->dev);
1681 if (!iommu)
1682 return;
1683
1684 /* Update data structures */
1685 dev_data->domain = NULL;
1686 list_del(&dev_data->list);
1687 clear_dte_entry(iommu, dev_data->devid);
1688 clone_aliases(iommu, dev_data->dev);
1689
1690 /* Flush the DTE entry */
1691 device_flush_dte(dev_data);
1692
1693 /* Flush IOTLB */
1694 amd_iommu_domain_flush_tlb_pde(domain);
1695
1696 /* Wait for the flushes to finish */
1697 amd_iommu_domain_flush_complete(domain);
1698
1699 /* decrease reference counters - needs to happen after the flushes */
1700 domain->dev_iommu[iommu->index] -= 1;
1701 domain->dev_cnt -= 1;
1702 }
1703
pdev_iommuv2_disable(struct pci_dev * pdev)1704 static void pdev_iommuv2_disable(struct pci_dev *pdev)
1705 {
1706 pci_disable_ats(pdev);
1707 pci_disable_pri(pdev);
1708 pci_disable_pasid(pdev);
1709 }
1710
pdev_pri_ats_enable(struct pci_dev * pdev)1711 static int pdev_pri_ats_enable(struct pci_dev *pdev)
1712 {
1713 int ret;
1714
1715 /* Only allow access to user-accessible pages */
1716 ret = pci_enable_pasid(pdev, 0);
1717 if (ret)
1718 return ret;
1719
1720 /* First reset the PRI state of the device */
1721 ret = pci_reset_pri(pdev);
1722 if (ret)
1723 goto out_err_pasid;
1724
1725 /* Enable PRI */
1726 /* FIXME: Hardcode number of outstanding requests for now */
1727 ret = pci_enable_pri(pdev, 32);
1728 if (ret)
1729 goto out_err_pasid;
1730
1731 ret = pci_enable_ats(pdev, PAGE_SHIFT);
1732 if (ret)
1733 goto out_err_pri;
1734
1735 return 0;
1736
1737 out_err_pri:
1738 pci_disable_pri(pdev);
1739
1740 out_err_pasid:
1741 pci_disable_pasid(pdev);
1742
1743 return ret;
1744 }
1745
1746 /*
1747 * If a device is not yet associated with a domain, this function makes the
1748 * device visible in the domain
1749 */
attach_device(struct device * dev,struct protection_domain * domain)1750 static int attach_device(struct device *dev,
1751 struct protection_domain *domain)
1752 {
1753 struct iommu_dev_data *dev_data;
1754 struct pci_dev *pdev;
1755 unsigned long flags;
1756 int ret;
1757
1758 spin_lock_irqsave(&domain->lock, flags);
1759
1760 dev_data = dev_iommu_priv_get(dev);
1761
1762 spin_lock(&dev_data->lock);
1763
1764 ret = -EBUSY;
1765 if (dev_data->domain != NULL)
1766 goto out;
1767
1768 if (!dev_is_pci(dev))
1769 goto skip_ats_check;
1770
1771 pdev = to_pci_dev(dev);
1772 if (domain->flags & PD_IOMMUV2_MASK) {
1773 struct iommu_domain *def_domain = iommu_get_dma_domain(dev);
1774
1775 ret = -EINVAL;
1776
1777 /*
1778 * In case of using AMD_IOMMU_V1 page table mode and the device
1779 * is enabling for PPR/ATS support (using v2 table),
1780 * we need to make sure that the domain type is identity map.
1781 */
1782 if ((amd_iommu_pgtable == AMD_IOMMU_V1) &&
1783 def_domain->type != IOMMU_DOMAIN_IDENTITY) {
1784 goto out;
1785 }
1786
1787 if (dev_data->iommu_v2) {
1788 if (pdev_pri_ats_enable(pdev) != 0)
1789 goto out;
1790
1791 dev_data->ats.enabled = true;
1792 dev_data->ats.qdep = pci_ats_queue_depth(pdev);
1793 dev_data->pri_tlp = pci_prg_resp_pasid_required(pdev);
1794 }
1795 } else if (amd_iommu_iotlb_sup &&
1796 pci_enable_ats(pdev, PAGE_SHIFT) == 0) {
1797 dev_data->ats.enabled = true;
1798 dev_data->ats.qdep = pci_ats_queue_depth(pdev);
1799 }
1800
1801 skip_ats_check:
1802 ret = 0;
1803
1804 do_attach(dev_data, domain);
1805
1806 /*
1807 * We might boot into a crash-kernel here. The crashed kernel
1808 * left the caches in the IOMMU dirty. So we have to flush
1809 * here to evict all dirty stuff.
1810 */
1811 amd_iommu_domain_flush_tlb_pde(domain);
1812
1813 amd_iommu_domain_flush_complete(domain);
1814
1815 out:
1816 spin_unlock(&dev_data->lock);
1817
1818 spin_unlock_irqrestore(&domain->lock, flags);
1819
1820 return ret;
1821 }
1822
1823 /*
1824 * Removes a device from a protection domain (with devtable_lock held)
1825 */
detach_device(struct device * dev)1826 static void detach_device(struct device *dev)
1827 {
1828 struct protection_domain *domain;
1829 struct iommu_dev_data *dev_data;
1830 unsigned long flags;
1831
1832 dev_data = dev_iommu_priv_get(dev);
1833 domain = dev_data->domain;
1834
1835 spin_lock_irqsave(&domain->lock, flags);
1836
1837 spin_lock(&dev_data->lock);
1838
1839 /*
1840 * First check if the device is still attached. It might already
1841 * be detached from its domain because the generic
1842 * iommu_detach_group code detached it and we try again here in
1843 * our alias handling.
1844 */
1845 if (WARN_ON(!dev_data->domain))
1846 goto out;
1847
1848 do_detach(dev_data);
1849
1850 if (!dev_is_pci(dev))
1851 goto out;
1852
1853 if (domain->flags & PD_IOMMUV2_MASK && dev_data->iommu_v2)
1854 pdev_iommuv2_disable(to_pci_dev(dev));
1855 else if (dev_data->ats.enabled)
1856 pci_disable_ats(to_pci_dev(dev));
1857
1858 dev_data->ats.enabled = false;
1859
1860 out:
1861 spin_unlock(&dev_data->lock);
1862
1863 spin_unlock_irqrestore(&domain->lock, flags);
1864 }
1865
amd_iommu_probe_device(struct device * dev)1866 static struct iommu_device *amd_iommu_probe_device(struct device *dev)
1867 {
1868 struct iommu_device *iommu_dev;
1869 struct amd_iommu *iommu;
1870 int ret;
1871
1872 if (!check_device(dev))
1873 return ERR_PTR(-ENODEV);
1874
1875 iommu = rlookup_amd_iommu(dev);
1876 if (!iommu)
1877 return ERR_PTR(-ENODEV);
1878
1879 /* Not registered yet? */
1880 if (!iommu->iommu.ops)
1881 return ERR_PTR(-ENODEV);
1882
1883 if (dev_iommu_priv_get(dev))
1884 return &iommu->iommu;
1885
1886 ret = iommu_init_device(iommu, dev);
1887 if (ret) {
1888 if (ret != -ENOTSUPP)
1889 dev_err(dev, "Failed to initialize - trying to proceed anyway\n");
1890 iommu_dev = ERR_PTR(ret);
1891 iommu_ignore_device(iommu, dev);
1892 } else {
1893 amd_iommu_set_pci_msi_domain(dev, iommu);
1894 iommu_dev = &iommu->iommu;
1895 }
1896
1897 iommu_completion_wait(iommu);
1898
1899 return iommu_dev;
1900 }
1901
amd_iommu_probe_finalize(struct device * dev)1902 static void amd_iommu_probe_finalize(struct device *dev)
1903 {
1904 /* Domains are initialized for this device - have a look what we ended up with */
1905 set_dma_ops(dev, NULL);
1906 iommu_setup_dma_ops(dev, 0, U64_MAX);
1907 }
1908
amd_iommu_release_device(struct device * dev)1909 static void amd_iommu_release_device(struct device *dev)
1910 {
1911 struct amd_iommu *iommu;
1912
1913 if (!check_device(dev))
1914 return;
1915
1916 iommu = rlookup_amd_iommu(dev);
1917 if (!iommu)
1918 return;
1919
1920 amd_iommu_uninit_device(dev);
1921 iommu_completion_wait(iommu);
1922 }
1923
amd_iommu_device_group(struct device * dev)1924 static struct iommu_group *amd_iommu_device_group(struct device *dev)
1925 {
1926 if (dev_is_pci(dev))
1927 return pci_device_group(dev);
1928
1929 return acpihid_device_group(dev);
1930 }
1931
1932 /*****************************************************************************
1933 *
1934 * The next functions belong to the dma_ops mapping/unmapping code.
1935 *
1936 *****************************************************************************/
1937
update_device_table(struct protection_domain * domain)1938 static void update_device_table(struct protection_domain *domain)
1939 {
1940 struct iommu_dev_data *dev_data;
1941
1942 list_for_each_entry(dev_data, &domain->dev_list, list) {
1943 struct amd_iommu *iommu = rlookup_amd_iommu(dev_data->dev);
1944
1945 if (!iommu)
1946 continue;
1947 set_dte_entry(iommu, dev_data->devid, domain,
1948 dev_data->ats.enabled, dev_data->iommu_v2);
1949 clone_aliases(iommu, dev_data->dev);
1950 }
1951 }
1952
amd_iommu_update_and_flush_device_table(struct protection_domain * domain)1953 void amd_iommu_update_and_flush_device_table(struct protection_domain *domain)
1954 {
1955 update_device_table(domain);
1956 domain_flush_devices(domain);
1957 }
1958
amd_iommu_domain_update(struct protection_domain * domain)1959 void amd_iommu_domain_update(struct protection_domain *domain)
1960 {
1961 /* Update device table */
1962 amd_iommu_update_and_flush_device_table(domain);
1963
1964 /* Flush domain TLB(s) and wait for completion */
1965 amd_iommu_domain_flush_tlb_pde(domain);
1966 amd_iommu_domain_flush_complete(domain);
1967 }
1968
1969 /*****************************************************************************
1970 *
1971 * The following functions belong to the exported interface of AMD IOMMU
1972 *
1973 * This interface allows access to lower level functions of the IOMMU
1974 * like protection domain handling and assignement of devices to domains
1975 * which is not possible with the dma_ops interface.
1976 *
1977 *****************************************************************************/
1978
cleanup_domain(struct protection_domain * domain)1979 static void cleanup_domain(struct protection_domain *domain)
1980 {
1981 struct iommu_dev_data *entry;
1982 unsigned long flags;
1983
1984 spin_lock_irqsave(&domain->lock, flags);
1985
1986 while (!list_empty(&domain->dev_list)) {
1987 entry = list_first_entry(&domain->dev_list,
1988 struct iommu_dev_data, list);
1989 BUG_ON(!entry->domain);
1990 do_detach(entry);
1991 }
1992
1993 spin_unlock_irqrestore(&domain->lock, flags);
1994 }
1995
protection_domain_free(struct protection_domain * domain)1996 static void protection_domain_free(struct protection_domain *domain)
1997 {
1998 if (!domain)
1999 return;
2000
2001 if (domain->iop.pgtbl_cfg.tlb)
2002 free_io_pgtable_ops(&domain->iop.iop.ops);
2003
2004 if (domain->id)
2005 domain_id_free(domain->id);
2006
2007 kfree(domain);
2008 }
2009
protection_domain_init_v1(struct protection_domain * domain,int mode)2010 static int protection_domain_init_v1(struct protection_domain *domain, int mode)
2011 {
2012 u64 *pt_root = NULL;
2013
2014 BUG_ON(mode < PAGE_MODE_NONE || mode > PAGE_MODE_6_LEVEL);
2015
2016 spin_lock_init(&domain->lock);
2017 domain->id = domain_id_alloc();
2018 if (!domain->id)
2019 return -ENOMEM;
2020 INIT_LIST_HEAD(&domain->dev_list);
2021
2022 if (mode != PAGE_MODE_NONE) {
2023 pt_root = (void *)get_zeroed_page(GFP_KERNEL);
2024 if (!pt_root) {
2025 domain_id_free(domain->id);
2026 return -ENOMEM;
2027 }
2028 }
2029
2030 amd_iommu_domain_set_pgtable(domain, pt_root, mode);
2031
2032 return 0;
2033 }
2034
protection_domain_init_v2(struct protection_domain * domain)2035 static int protection_domain_init_v2(struct protection_domain *domain)
2036 {
2037 spin_lock_init(&domain->lock);
2038 domain->id = domain_id_alloc();
2039 if (!domain->id)
2040 return -ENOMEM;
2041 INIT_LIST_HEAD(&domain->dev_list);
2042
2043 domain->flags |= PD_GIOV_MASK;
2044
2045 domain->domain.pgsize_bitmap = AMD_IOMMU_PGSIZES_V2;
2046
2047 if (domain_enable_v2(domain, 1)) {
2048 domain_id_free(domain->id);
2049 return -ENOMEM;
2050 }
2051
2052 return 0;
2053 }
2054
protection_domain_alloc(unsigned int type)2055 static struct protection_domain *protection_domain_alloc(unsigned int type)
2056 {
2057 struct io_pgtable_ops *pgtbl_ops;
2058 struct protection_domain *domain;
2059 int pgtable;
2060 int mode = DEFAULT_PGTABLE_LEVEL;
2061 int ret;
2062
2063 /*
2064 * Force IOMMU v1 page table when iommu=pt and
2065 * when allocating domain for pass-through devices.
2066 */
2067 if (type == IOMMU_DOMAIN_IDENTITY) {
2068 pgtable = AMD_IOMMU_V1;
2069 mode = PAGE_MODE_NONE;
2070 } else if (type == IOMMU_DOMAIN_UNMANAGED) {
2071 pgtable = AMD_IOMMU_V1;
2072 } else if (type == IOMMU_DOMAIN_DMA || type == IOMMU_DOMAIN_DMA_FQ) {
2073 pgtable = amd_iommu_pgtable;
2074 } else {
2075 return NULL;
2076 }
2077
2078 domain = kzalloc(sizeof(*domain), GFP_KERNEL);
2079 if (!domain)
2080 return NULL;
2081
2082 switch (pgtable) {
2083 case AMD_IOMMU_V1:
2084 ret = protection_domain_init_v1(domain, mode);
2085 break;
2086 case AMD_IOMMU_V2:
2087 ret = protection_domain_init_v2(domain);
2088 break;
2089 default:
2090 ret = -EINVAL;
2091 }
2092
2093 if (ret)
2094 goto out_err;
2095
2096 pgtbl_ops = alloc_io_pgtable_ops(pgtable, &domain->iop.pgtbl_cfg, domain);
2097 if (!pgtbl_ops) {
2098 domain_id_free(domain->id);
2099 goto out_err;
2100 }
2101
2102 return domain;
2103 out_err:
2104 kfree(domain);
2105 return NULL;
2106 }
2107
dma_max_address(void)2108 static inline u64 dma_max_address(void)
2109 {
2110 if (amd_iommu_pgtable == AMD_IOMMU_V1)
2111 return ~0ULL;
2112
2113 /* V2 with 4 level page table */
2114 return ((1ULL << PM_LEVEL_SHIFT(PAGE_MODE_4_LEVEL)) - 1);
2115 }
2116
amd_iommu_domain_alloc(unsigned type)2117 static struct iommu_domain *amd_iommu_domain_alloc(unsigned type)
2118 {
2119 struct protection_domain *domain;
2120
2121 /*
2122 * Since DTE[Mode]=0 is prohibited on SNP-enabled system,
2123 * default to use IOMMU_DOMAIN_DMA[_FQ].
2124 */
2125 if (amd_iommu_snp_en && (type == IOMMU_DOMAIN_IDENTITY))
2126 return NULL;
2127
2128 domain = protection_domain_alloc(type);
2129 if (!domain)
2130 return NULL;
2131
2132 domain->domain.geometry.aperture_start = 0;
2133 domain->domain.geometry.aperture_end = dma_max_address();
2134 domain->domain.geometry.force_aperture = true;
2135
2136 return &domain->domain;
2137 }
2138
amd_iommu_domain_free(struct iommu_domain * dom)2139 static void amd_iommu_domain_free(struct iommu_domain *dom)
2140 {
2141 struct protection_domain *domain;
2142
2143 domain = to_pdomain(dom);
2144
2145 if (domain->dev_cnt > 0)
2146 cleanup_domain(domain);
2147
2148 BUG_ON(domain->dev_cnt != 0);
2149
2150 if (!dom)
2151 return;
2152
2153 if (domain->flags & PD_IOMMUV2_MASK)
2154 free_gcr3_table(domain);
2155
2156 protection_domain_free(domain);
2157 }
2158
amd_iommu_detach_device(struct iommu_domain * dom,struct device * dev)2159 static void amd_iommu_detach_device(struct iommu_domain *dom,
2160 struct device *dev)
2161 {
2162 struct iommu_dev_data *dev_data = dev_iommu_priv_get(dev);
2163 struct amd_iommu *iommu;
2164
2165 if (!check_device(dev))
2166 return;
2167
2168 if (dev_data->domain != NULL)
2169 detach_device(dev);
2170
2171 iommu = rlookup_amd_iommu(dev);
2172 if (!iommu)
2173 return;
2174
2175 #ifdef CONFIG_IRQ_REMAP
2176 if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir) &&
2177 (dom->type == IOMMU_DOMAIN_UNMANAGED))
2178 dev_data->use_vapic = 0;
2179 #endif
2180
2181 iommu_completion_wait(iommu);
2182 }
2183
amd_iommu_attach_device(struct iommu_domain * dom,struct device * dev)2184 static int amd_iommu_attach_device(struct iommu_domain *dom,
2185 struct device *dev)
2186 {
2187 struct protection_domain *domain = to_pdomain(dom);
2188 struct iommu_dev_data *dev_data;
2189 struct amd_iommu *iommu;
2190 int ret;
2191
2192 if (!check_device(dev))
2193 return -EINVAL;
2194
2195 dev_data = dev_iommu_priv_get(dev);
2196 dev_data->defer_attach = false;
2197
2198 iommu = rlookup_amd_iommu(dev);
2199 if (!iommu)
2200 return -EINVAL;
2201
2202 if (dev_data->domain)
2203 detach_device(dev);
2204
2205 ret = attach_device(dev, domain);
2206
2207 #ifdef CONFIG_IRQ_REMAP
2208 if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir)) {
2209 if (dom->type == IOMMU_DOMAIN_UNMANAGED)
2210 dev_data->use_vapic = 1;
2211 else
2212 dev_data->use_vapic = 0;
2213 }
2214 #endif
2215
2216 iommu_completion_wait(iommu);
2217
2218 return ret;
2219 }
2220
amd_iommu_iotlb_sync_map(struct iommu_domain * dom,unsigned long iova,size_t size)2221 static void amd_iommu_iotlb_sync_map(struct iommu_domain *dom,
2222 unsigned long iova, size_t size)
2223 {
2224 struct protection_domain *domain = to_pdomain(dom);
2225 struct io_pgtable_ops *ops = &domain->iop.iop.ops;
2226
2227 if (ops->map_pages)
2228 domain_flush_np_cache(domain, iova, size);
2229 }
2230
amd_iommu_map_pages(struct iommu_domain * dom,unsigned long iova,phys_addr_t paddr,size_t pgsize,size_t pgcount,int iommu_prot,gfp_t gfp,size_t * mapped)2231 static int amd_iommu_map_pages(struct iommu_domain *dom, unsigned long iova,
2232 phys_addr_t paddr, size_t pgsize, size_t pgcount,
2233 int iommu_prot, gfp_t gfp, size_t *mapped)
2234 {
2235 struct protection_domain *domain = to_pdomain(dom);
2236 struct io_pgtable_ops *ops = &domain->iop.iop.ops;
2237 int prot = 0;
2238 int ret = -EINVAL;
2239
2240 if ((amd_iommu_pgtable == AMD_IOMMU_V1) &&
2241 (domain->iop.mode == PAGE_MODE_NONE))
2242 return -EINVAL;
2243
2244 if (iommu_prot & IOMMU_READ)
2245 prot |= IOMMU_PROT_IR;
2246 if (iommu_prot & IOMMU_WRITE)
2247 prot |= IOMMU_PROT_IW;
2248
2249 if (ops->map_pages) {
2250 ret = ops->map_pages(ops, iova, paddr, pgsize,
2251 pgcount, prot, gfp, mapped);
2252 }
2253
2254 return ret;
2255 }
2256
amd_iommu_iotlb_gather_add_page(struct iommu_domain * domain,struct iommu_iotlb_gather * gather,unsigned long iova,size_t size)2257 static void amd_iommu_iotlb_gather_add_page(struct iommu_domain *domain,
2258 struct iommu_iotlb_gather *gather,
2259 unsigned long iova, size_t size)
2260 {
2261 /*
2262 * AMD's IOMMU can flush as many pages as necessary in a single flush.
2263 * Unless we run in a virtual machine, which can be inferred according
2264 * to whether "non-present cache" is on, it is probably best to prefer
2265 * (potentially) too extensive TLB flushing (i.e., more misses) over
2266 * mutliple TLB flushes (i.e., more flushes). For virtual machines the
2267 * hypervisor needs to synchronize the host IOMMU PTEs with those of
2268 * the guest, and the trade-off is different: unnecessary TLB flushes
2269 * should be avoided.
2270 */
2271 if (amd_iommu_np_cache &&
2272 iommu_iotlb_gather_is_disjoint(gather, iova, size))
2273 iommu_iotlb_sync(domain, gather);
2274
2275 iommu_iotlb_gather_add_range(gather, iova, size);
2276 }
2277
amd_iommu_unmap_pages(struct iommu_domain * dom,unsigned long iova,size_t pgsize,size_t pgcount,struct iommu_iotlb_gather * gather)2278 static size_t amd_iommu_unmap_pages(struct iommu_domain *dom, unsigned long iova,
2279 size_t pgsize, size_t pgcount,
2280 struct iommu_iotlb_gather *gather)
2281 {
2282 struct protection_domain *domain = to_pdomain(dom);
2283 struct io_pgtable_ops *ops = &domain->iop.iop.ops;
2284 size_t r;
2285
2286 if ((amd_iommu_pgtable == AMD_IOMMU_V1) &&
2287 (domain->iop.mode == PAGE_MODE_NONE))
2288 return 0;
2289
2290 r = (ops->unmap_pages) ? ops->unmap_pages(ops, iova, pgsize, pgcount, NULL) : 0;
2291
2292 if (r)
2293 amd_iommu_iotlb_gather_add_page(dom, gather, iova, r);
2294
2295 return r;
2296 }
2297
amd_iommu_iova_to_phys(struct iommu_domain * dom,dma_addr_t iova)2298 static phys_addr_t amd_iommu_iova_to_phys(struct iommu_domain *dom,
2299 dma_addr_t iova)
2300 {
2301 struct protection_domain *domain = to_pdomain(dom);
2302 struct io_pgtable_ops *ops = &domain->iop.iop.ops;
2303
2304 return ops->iova_to_phys(ops, iova);
2305 }
2306
amd_iommu_capable(struct device * dev,enum iommu_cap cap)2307 static bool amd_iommu_capable(struct device *dev, enum iommu_cap cap)
2308 {
2309 switch (cap) {
2310 case IOMMU_CAP_CACHE_COHERENCY:
2311 return true;
2312 case IOMMU_CAP_INTR_REMAP:
2313 return (irq_remapping_enabled == 1);
2314 case IOMMU_CAP_NOEXEC:
2315 return false;
2316 case IOMMU_CAP_PRE_BOOT_PROTECTION:
2317 return amdr_ivrs_remap_support;
2318 default:
2319 break;
2320 }
2321
2322 return false;
2323 }
2324
amd_iommu_get_resv_regions(struct device * dev,struct list_head * head)2325 static void amd_iommu_get_resv_regions(struct device *dev,
2326 struct list_head *head)
2327 {
2328 struct iommu_resv_region *region;
2329 struct unity_map_entry *entry;
2330 struct amd_iommu *iommu;
2331 struct amd_iommu_pci_seg *pci_seg;
2332 int devid, sbdf;
2333
2334 sbdf = get_device_sbdf_id(dev);
2335 if (sbdf < 0)
2336 return;
2337
2338 devid = PCI_SBDF_TO_DEVID(sbdf);
2339 iommu = rlookup_amd_iommu(dev);
2340 if (!iommu)
2341 return;
2342 pci_seg = iommu->pci_seg;
2343
2344 list_for_each_entry(entry, &pci_seg->unity_map, list) {
2345 int type, prot = 0;
2346 size_t length;
2347
2348 if (devid < entry->devid_start || devid > entry->devid_end)
2349 continue;
2350
2351 type = IOMMU_RESV_DIRECT;
2352 length = entry->address_end - entry->address_start;
2353 if (entry->prot & IOMMU_PROT_IR)
2354 prot |= IOMMU_READ;
2355 if (entry->prot & IOMMU_PROT_IW)
2356 prot |= IOMMU_WRITE;
2357 if (entry->prot & IOMMU_UNITY_MAP_FLAG_EXCL_RANGE)
2358 /* Exclusion range */
2359 type = IOMMU_RESV_RESERVED;
2360
2361 region = iommu_alloc_resv_region(entry->address_start,
2362 length, prot, type,
2363 GFP_KERNEL);
2364 if (!region) {
2365 dev_err(dev, "Out of memory allocating dm-regions\n");
2366 return;
2367 }
2368 list_add_tail(®ion->list, head);
2369 }
2370
2371 region = iommu_alloc_resv_region(MSI_RANGE_START,
2372 MSI_RANGE_END - MSI_RANGE_START + 1,
2373 0, IOMMU_RESV_MSI, GFP_KERNEL);
2374 if (!region)
2375 return;
2376 list_add_tail(®ion->list, head);
2377
2378 region = iommu_alloc_resv_region(HT_RANGE_START,
2379 HT_RANGE_END - HT_RANGE_START + 1,
2380 0, IOMMU_RESV_RESERVED, GFP_KERNEL);
2381 if (!region)
2382 return;
2383 list_add_tail(®ion->list, head);
2384 }
2385
amd_iommu_is_attach_deferred(struct device * dev)2386 bool amd_iommu_is_attach_deferred(struct device *dev)
2387 {
2388 struct iommu_dev_data *dev_data = dev_iommu_priv_get(dev);
2389
2390 return dev_data->defer_attach;
2391 }
2392 EXPORT_SYMBOL_GPL(amd_iommu_is_attach_deferred);
2393
amd_iommu_flush_iotlb_all(struct iommu_domain * domain)2394 static void amd_iommu_flush_iotlb_all(struct iommu_domain *domain)
2395 {
2396 struct protection_domain *dom = to_pdomain(domain);
2397 unsigned long flags;
2398
2399 spin_lock_irqsave(&dom->lock, flags);
2400 amd_iommu_domain_flush_tlb_pde(dom);
2401 amd_iommu_domain_flush_complete(dom);
2402 spin_unlock_irqrestore(&dom->lock, flags);
2403 }
2404
amd_iommu_iotlb_sync(struct iommu_domain * domain,struct iommu_iotlb_gather * gather)2405 static void amd_iommu_iotlb_sync(struct iommu_domain *domain,
2406 struct iommu_iotlb_gather *gather)
2407 {
2408 struct protection_domain *dom = to_pdomain(domain);
2409 unsigned long flags;
2410
2411 spin_lock_irqsave(&dom->lock, flags);
2412 domain_flush_pages(dom, gather->start, gather->end - gather->start + 1, 1);
2413 amd_iommu_domain_flush_complete(dom);
2414 spin_unlock_irqrestore(&dom->lock, flags);
2415 }
2416
amd_iommu_def_domain_type(struct device * dev)2417 static int amd_iommu_def_domain_type(struct device *dev)
2418 {
2419 struct iommu_dev_data *dev_data;
2420
2421 dev_data = dev_iommu_priv_get(dev);
2422 if (!dev_data)
2423 return 0;
2424
2425 /*
2426 * Do not identity map IOMMUv2 capable devices when:
2427 * - memory encryption is active, because some of those devices
2428 * (AMD GPUs) don't have the encryption bit in their DMA-mask
2429 * and require remapping.
2430 * - SNP is enabled, because it prohibits DTE[Mode]=0.
2431 */
2432 if (dev_data->iommu_v2 &&
2433 !cc_platform_has(CC_ATTR_MEM_ENCRYPT) &&
2434 !amd_iommu_snp_en) {
2435 return IOMMU_DOMAIN_IDENTITY;
2436 }
2437
2438 return 0;
2439 }
2440
amd_iommu_enforce_cache_coherency(struct iommu_domain * domain)2441 static bool amd_iommu_enforce_cache_coherency(struct iommu_domain *domain)
2442 {
2443 /* IOMMU_PTE_FC is always set */
2444 return true;
2445 }
2446
2447 const struct iommu_ops amd_iommu_ops = {
2448 .capable = amd_iommu_capable,
2449 .domain_alloc = amd_iommu_domain_alloc,
2450 .probe_device = amd_iommu_probe_device,
2451 .release_device = amd_iommu_release_device,
2452 .probe_finalize = amd_iommu_probe_finalize,
2453 .device_group = amd_iommu_device_group,
2454 .get_resv_regions = amd_iommu_get_resv_regions,
2455 .is_attach_deferred = amd_iommu_is_attach_deferred,
2456 .pgsize_bitmap = AMD_IOMMU_PGSIZES,
2457 .def_domain_type = amd_iommu_def_domain_type,
2458 .default_domain_ops = &(const struct iommu_domain_ops) {
2459 .attach_dev = amd_iommu_attach_device,
2460 .detach_dev = amd_iommu_detach_device,
2461 .map_pages = amd_iommu_map_pages,
2462 .unmap_pages = amd_iommu_unmap_pages,
2463 .iotlb_sync_map = amd_iommu_iotlb_sync_map,
2464 .iova_to_phys = amd_iommu_iova_to_phys,
2465 .flush_iotlb_all = amd_iommu_flush_iotlb_all,
2466 .iotlb_sync = amd_iommu_iotlb_sync,
2467 .free = amd_iommu_domain_free,
2468 .enforce_cache_coherency = amd_iommu_enforce_cache_coherency,
2469 }
2470 };
2471
2472 /*****************************************************************************
2473 *
2474 * The next functions do a basic initialization of IOMMU for pass through
2475 * mode
2476 *
2477 * In passthrough mode the IOMMU is initialized and enabled but not used for
2478 * DMA-API translation.
2479 *
2480 *****************************************************************************/
2481
2482 /* IOMMUv2 specific functions */
amd_iommu_register_ppr_notifier(struct notifier_block * nb)2483 int amd_iommu_register_ppr_notifier(struct notifier_block *nb)
2484 {
2485 return atomic_notifier_chain_register(&ppr_notifier, nb);
2486 }
2487 EXPORT_SYMBOL(amd_iommu_register_ppr_notifier);
2488
amd_iommu_unregister_ppr_notifier(struct notifier_block * nb)2489 int amd_iommu_unregister_ppr_notifier(struct notifier_block *nb)
2490 {
2491 return atomic_notifier_chain_unregister(&ppr_notifier, nb);
2492 }
2493 EXPORT_SYMBOL(amd_iommu_unregister_ppr_notifier);
2494
amd_iommu_domain_direct_map(struct iommu_domain * dom)2495 void amd_iommu_domain_direct_map(struct iommu_domain *dom)
2496 {
2497 struct protection_domain *domain = to_pdomain(dom);
2498 unsigned long flags;
2499
2500 spin_lock_irqsave(&domain->lock, flags);
2501
2502 if (domain->iop.pgtbl_cfg.tlb)
2503 free_io_pgtable_ops(&domain->iop.iop.ops);
2504
2505 spin_unlock_irqrestore(&domain->lock, flags);
2506 }
2507 EXPORT_SYMBOL(amd_iommu_domain_direct_map);
2508
2509 /* Note: This function expects iommu_domain->lock to be held prior calling the function. */
domain_enable_v2(struct protection_domain * domain,int pasids)2510 static int domain_enable_v2(struct protection_domain *domain, int pasids)
2511 {
2512 int levels;
2513
2514 /* Number of GCR3 table levels required */
2515 for (levels = 0; (pasids - 1) & ~0x1ff; pasids >>= 9)
2516 levels += 1;
2517
2518 if (levels > amd_iommu_max_glx_val)
2519 return -EINVAL;
2520
2521 domain->gcr3_tbl = (void *)get_zeroed_page(GFP_ATOMIC);
2522 if (domain->gcr3_tbl == NULL)
2523 return -ENOMEM;
2524
2525 domain->glx = levels;
2526 domain->flags |= PD_IOMMUV2_MASK;
2527
2528 amd_iommu_domain_update(domain);
2529
2530 return 0;
2531 }
2532
amd_iommu_domain_enable_v2(struct iommu_domain * dom,int pasids)2533 int amd_iommu_domain_enable_v2(struct iommu_domain *dom, int pasids)
2534 {
2535 struct protection_domain *pdom = to_pdomain(dom);
2536 unsigned long flags;
2537 int ret;
2538
2539 spin_lock_irqsave(&pdom->lock, flags);
2540
2541 /*
2542 * Save us all sanity checks whether devices already in the
2543 * domain support IOMMUv2. Just force that the domain has no
2544 * devices attached when it is switched into IOMMUv2 mode.
2545 */
2546 ret = -EBUSY;
2547 if (pdom->dev_cnt > 0 || pdom->flags & PD_IOMMUV2_MASK)
2548 goto out;
2549
2550 if (!pdom->gcr3_tbl)
2551 ret = domain_enable_v2(pdom, pasids);
2552
2553 out:
2554 spin_unlock_irqrestore(&pdom->lock, flags);
2555 return ret;
2556 }
2557 EXPORT_SYMBOL(amd_iommu_domain_enable_v2);
2558
__flush_pasid(struct protection_domain * domain,u32 pasid,u64 address,bool size)2559 static int __flush_pasid(struct protection_domain *domain, u32 pasid,
2560 u64 address, bool size)
2561 {
2562 struct iommu_dev_data *dev_data;
2563 struct iommu_cmd cmd;
2564 int i, ret;
2565
2566 if (!(domain->flags & PD_IOMMUV2_MASK))
2567 return -EINVAL;
2568
2569 build_inv_iommu_pasid(&cmd, domain->id, pasid, address, size);
2570
2571 /*
2572 * IOMMU TLB needs to be flushed before Device TLB to
2573 * prevent device TLB refill from IOMMU TLB
2574 */
2575 for (i = 0; i < amd_iommu_get_num_iommus(); ++i) {
2576 if (domain->dev_iommu[i] == 0)
2577 continue;
2578
2579 ret = iommu_queue_command(amd_iommus[i], &cmd);
2580 if (ret != 0)
2581 goto out;
2582 }
2583
2584 /* Wait until IOMMU TLB flushes are complete */
2585 amd_iommu_domain_flush_complete(domain);
2586
2587 /* Now flush device TLBs */
2588 list_for_each_entry(dev_data, &domain->dev_list, list) {
2589 struct amd_iommu *iommu;
2590 int qdep;
2591
2592 /*
2593 There might be non-IOMMUv2 capable devices in an IOMMUv2
2594 * domain.
2595 */
2596 if (!dev_data->ats.enabled)
2597 continue;
2598
2599 qdep = dev_data->ats.qdep;
2600 iommu = rlookup_amd_iommu(dev_data->dev);
2601 if (!iommu)
2602 continue;
2603 build_inv_iotlb_pasid(&cmd, dev_data->devid, pasid,
2604 qdep, address, size);
2605
2606 ret = iommu_queue_command(iommu, &cmd);
2607 if (ret != 0)
2608 goto out;
2609 }
2610
2611 /* Wait until all device TLBs are flushed */
2612 amd_iommu_domain_flush_complete(domain);
2613
2614 ret = 0;
2615
2616 out:
2617
2618 return ret;
2619 }
2620
__amd_iommu_flush_page(struct protection_domain * domain,u32 pasid,u64 address)2621 static int __amd_iommu_flush_page(struct protection_domain *domain, u32 pasid,
2622 u64 address)
2623 {
2624 return __flush_pasid(domain, pasid, address, false);
2625 }
2626
amd_iommu_flush_page(struct iommu_domain * dom,u32 pasid,u64 address)2627 int amd_iommu_flush_page(struct iommu_domain *dom, u32 pasid,
2628 u64 address)
2629 {
2630 struct protection_domain *domain = to_pdomain(dom);
2631 unsigned long flags;
2632 int ret;
2633
2634 spin_lock_irqsave(&domain->lock, flags);
2635 ret = __amd_iommu_flush_page(domain, pasid, address);
2636 spin_unlock_irqrestore(&domain->lock, flags);
2637
2638 return ret;
2639 }
2640 EXPORT_SYMBOL(amd_iommu_flush_page);
2641
__amd_iommu_flush_tlb(struct protection_domain * domain,u32 pasid)2642 static int __amd_iommu_flush_tlb(struct protection_domain *domain, u32 pasid)
2643 {
2644 return __flush_pasid(domain, pasid, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
2645 true);
2646 }
2647
amd_iommu_flush_tlb(struct iommu_domain * dom,u32 pasid)2648 int amd_iommu_flush_tlb(struct iommu_domain *dom, u32 pasid)
2649 {
2650 struct protection_domain *domain = to_pdomain(dom);
2651 unsigned long flags;
2652 int ret;
2653
2654 spin_lock_irqsave(&domain->lock, flags);
2655 ret = __amd_iommu_flush_tlb(domain, pasid);
2656 spin_unlock_irqrestore(&domain->lock, flags);
2657
2658 return ret;
2659 }
2660 EXPORT_SYMBOL(amd_iommu_flush_tlb);
2661
__get_gcr3_pte(u64 * root,int level,u32 pasid,bool alloc)2662 static u64 *__get_gcr3_pte(u64 *root, int level, u32 pasid, bool alloc)
2663 {
2664 int index;
2665 u64 *pte;
2666
2667 while (true) {
2668
2669 index = (pasid >> (9 * level)) & 0x1ff;
2670 pte = &root[index];
2671
2672 if (level == 0)
2673 break;
2674
2675 if (!(*pte & GCR3_VALID)) {
2676 if (!alloc)
2677 return NULL;
2678
2679 root = (void *)get_zeroed_page(GFP_ATOMIC);
2680 if (root == NULL)
2681 return NULL;
2682
2683 *pte = iommu_virt_to_phys(root) | GCR3_VALID;
2684 }
2685
2686 root = iommu_phys_to_virt(*pte & PAGE_MASK);
2687
2688 level -= 1;
2689 }
2690
2691 return pte;
2692 }
2693
__set_gcr3(struct protection_domain * domain,u32 pasid,unsigned long cr3)2694 static int __set_gcr3(struct protection_domain *domain, u32 pasid,
2695 unsigned long cr3)
2696 {
2697 u64 *pte;
2698
2699 if (domain->iop.mode != PAGE_MODE_NONE)
2700 return -EINVAL;
2701
2702 pte = __get_gcr3_pte(domain->gcr3_tbl, domain->glx, pasid, true);
2703 if (pte == NULL)
2704 return -ENOMEM;
2705
2706 *pte = (cr3 & PAGE_MASK) | GCR3_VALID;
2707
2708 return __amd_iommu_flush_tlb(domain, pasid);
2709 }
2710
__clear_gcr3(struct protection_domain * domain,u32 pasid)2711 static int __clear_gcr3(struct protection_domain *domain, u32 pasid)
2712 {
2713 u64 *pte;
2714
2715 if (domain->iop.mode != PAGE_MODE_NONE)
2716 return -EINVAL;
2717
2718 pte = __get_gcr3_pte(domain->gcr3_tbl, domain->glx, pasid, false);
2719 if (pte == NULL)
2720 return 0;
2721
2722 *pte = 0;
2723
2724 return __amd_iommu_flush_tlb(domain, pasid);
2725 }
2726
amd_iommu_domain_set_gcr3(struct iommu_domain * dom,u32 pasid,unsigned long cr3)2727 int amd_iommu_domain_set_gcr3(struct iommu_domain *dom, u32 pasid,
2728 unsigned long cr3)
2729 {
2730 struct protection_domain *domain = to_pdomain(dom);
2731 unsigned long flags;
2732 int ret;
2733
2734 spin_lock_irqsave(&domain->lock, flags);
2735 ret = __set_gcr3(domain, pasid, cr3);
2736 spin_unlock_irqrestore(&domain->lock, flags);
2737
2738 return ret;
2739 }
2740 EXPORT_SYMBOL(amd_iommu_domain_set_gcr3);
2741
amd_iommu_domain_clear_gcr3(struct iommu_domain * dom,u32 pasid)2742 int amd_iommu_domain_clear_gcr3(struct iommu_domain *dom, u32 pasid)
2743 {
2744 struct protection_domain *domain = to_pdomain(dom);
2745 unsigned long flags;
2746 int ret;
2747
2748 spin_lock_irqsave(&domain->lock, flags);
2749 ret = __clear_gcr3(domain, pasid);
2750 spin_unlock_irqrestore(&domain->lock, flags);
2751
2752 return ret;
2753 }
2754 EXPORT_SYMBOL(amd_iommu_domain_clear_gcr3);
2755
amd_iommu_complete_ppr(struct pci_dev * pdev,u32 pasid,int status,int tag)2756 int amd_iommu_complete_ppr(struct pci_dev *pdev, u32 pasid,
2757 int status, int tag)
2758 {
2759 struct iommu_dev_data *dev_data;
2760 struct amd_iommu *iommu;
2761 struct iommu_cmd cmd;
2762
2763 dev_data = dev_iommu_priv_get(&pdev->dev);
2764 iommu = rlookup_amd_iommu(&pdev->dev);
2765 if (!iommu)
2766 return -ENODEV;
2767
2768 build_complete_ppr(&cmd, dev_data->devid, pasid, status,
2769 tag, dev_data->pri_tlp);
2770
2771 return iommu_queue_command(iommu, &cmd);
2772 }
2773 EXPORT_SYMBOL(amd_iommu_complete_ppr);
2774
amd_iommu_device_info(struct pci_dev * pdev,struct amd_iommu_device_info * info)2775 int amd_iommu_device_info(struct pci_dev *pdev,
2776 struct amd_iommu_device_info *info)
2777 {
2778 int max_pasids;
2779 int pos;
2780
2781 if (pdev == NULL || info == NULL)
2782 return -EINVAL;
2783
2784 if (!amd_iommu_v2_supported())
2785 return -EINVAL;
2786
2787 memset(info, 0, sizeof(*info));
2788
2789 if (pci_ats_supported(pdev))
2790 info->flags |= AMD_IOMMU_DEVICE_FLAG_ATS_SUP;
2791
2792 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
2793 if (pos)
2794 info->flags |= AMD_IOMMU_DEVICE_FLAG_PRI_SUP;
2795
2796 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PASID);
2797 if (pos) {
2798 int features;
2799
2800 max_pasids = 1 << (9 * (amd_iommu_max_glx_val + 1));
2801 max_pasids = min(max_pasids, (1 << 20));
2802
2803 info->flags |= AMD_IOMMU_DEVICE_FLAG_PASID_SUP;
2804 info->max_pasids = min(pci_max_pasids(pdev), max_pasids);
2805
2806 features = pci_pasid_features(pdev);
2807 if (features & PCI_PASID_CAP_EXEC)
2808 info->flags |= AMD_IOMMU_DEVICE_FLAG_EXEC_SUP;
2809 if (features & PCI_PASID_CAP_PRIV)
2810 info->flags |= AMD_IOMMU_DEVICE_FLAG_PRIV_SUP;
2811 }
2812
2813 return 0;
2814 }
2815 EXPORT_SYMBOL(amd_iommu_device_info);
2816
2817 #ifdef CONFIG_IRQ_REMAP
2818
2819 /*****************************************************************************
2820 *
2821 * Interrupt Remapping Implementation
2822 *
2823 *****************************************************************************/
2824
2825 static struct irq_chip amd_ir_chip;
2826 static DEFINE_SPINLOCK(iommu_table_lock);
2827
set_dte_irq_entry(struct amd_iommu * iommu,u16 devid,struct irq_remap_table * table)2828 static void set_dte_irq_entry(struct amd_iommu *iommu, u16 devid,
2829 struct irq_remap_table *table)
2830 {
2831 u64 dte;
2832 struct dev_table_entry *dev_table = get_dev_table(iommu);
2833
2834 dte = dev_table[devid].data[2];
2835 dte &= ~DTE_IRQ_PHYS_ADDR_MASK;
2836 dte |= iommu_virt_to_phys(table->table);
2837 dte |= DTE_IRQ_REMAP_INTCTL;
2838 dte |= DTE_INTTABLEN;
2839 dte |= DTE_IRQ_REMAP_ENABLE;
2840
2841 dev_table[devid].data[2] = dte;
2842 }
2843
get_irq_table(struct amd_iommu * iommu,u16 devid)2844 static struct irq_remap_table *get_irq_table(struct amd_iommu *iommu, u16 devid)
2845 {
2846 struct irq_remap_table *table;
2847 struct amd_iommu_pci_seg *pci_seg = iommu->pci_seg;
2848
2849 if (WARN_ONCE(!pci_seg->rlookup_table[devid],
2850 "%s: no iommu for devid %x:%x\n",
2851 __func__, pci_seg->id, devid))
2852 return NULL;
2853
2854 table = pci_seg->irq_lookup_table[devid];
2855 if (WARN_ONCE(!table, "%s: no table for devid %x:%x\n",
2856 __func__, pci_seg->id, devid))
2857 return NULL;
2858
2859 return table;
2860 }
2861
__alloc_irq_table(void)2862 static struct irq_remap_table *__alloc_irq_table(void)
2863 {
2864 struct irq_remap_table *table;
2865
2866 table = kzalloc(sizeof(*table), GFP_KERNEL);
2867 if (!table)
2868 return NULL;
2869
2870 table->table = kmem_cache_alloc(amd_iommu_irq_cache, GFP_KERNEL);
2871 if (!table->table) {
2872 kfree(table);
2873 return NULL;
2874 }
2875 raw_spin_lock_init(&table->lock);
2876
2877 if (!AMD_IOMMU_GUEST_IR_GA(amd_iommu_guest_ir))
2878 memset(table->table, 0,
2879 MAX_IRQS_PER_TABLE * sizeof(u32));
2880 else
2881 memset(table->table, 0,
2882 (MAX_IRQS_PER_TABLE * (sizeof(u64) * 2)));
2883 return table;
2884 }
2885
set_remap_table_entry(struct amd_iommu * iommu,u16 devid,struct irq_remap_table * table)2886 static void set_remap_table_entry(struct amd_iommu *iommu, u16 devid,
2887 struct irq_remap_table *table)
2888 {
2889 struct amd_iommu_pci_seg *pci_seg = iommu->pci_seg;
2890
2891 pci_seg->irq_lookup_table[devid] = table;
2892 set_dte_irq_entry(iommu, devid, table);
2893 iommu_flush_dte(iommu, devid);
2894 }
2895
set_remap_table_entry_alias(struct pci_dev * pdev,u16 alias,void * data)2896 static int set_remap_table_entry_alias(struct pci_dev *pdev, u16 alias,
2897 void *data)
2898 {
2899 struct irq_remap_table *table = data;
2900 struct amd_iommu_pci_seg *pci_seg;
2901 struct amd_iommu *iommu = rlookup_amd_iommu(&pdev->dev);
2902
2903 if (!iommu)
2904 return -EINVAL;
2905
2906 pci_seg = iommu->pci_seg;
2907 pci_seg->irq_lookup_table[alias] = table;
2908 set_dte_irq_entry(iommu, alias, table);
2909 iommu_flush_dte(pci_seg->rlookup_table[alias], alias);
2910
2911 return 0;
2912 }
2913
alloc_irq_table(struct amd_iommu * iommu,u16 devid,struct pci_dev * pdev)2914 static struct irq_remap_table *alloc_irq_table(struct amd_iommu *iommu,
2915 u16 devid, struct pci_dev *pdev)
2916 {
2917 struct irq_remap_table *table = NULL;
2918 struct irq_remap_table *new_table = NULL;
2919 struct amd_iommu_pci_seg *pci_seg;
2920 unsigned long flags;
2921 u16 alias;
2922
2923 spin_lock_irqsave(&iommu_table_lock, flags);
2924
2925 pci_seg = iommu->pci_seg;
2926 table = pci_seg->irq_lookup_table[devid];
2927 if (table)
2928 goto out_unlock;
2929
2930 alias = pci_seg->alias_table[devid];
2931 table = pci_seg->irq_lookup_table[alias];
2932 if (table) {
2933 set_remap_table_entry(iommu, devid, table);
2934 goto out_wait;
2935 }
2936 spin_unlock_irqrestore(&iommu_table_lock, flags);
2937
2938 /* Nothing there yet, allocate new irq remapping table */
2939 new_table = __alloc_irq_table();
2940 if (!new_table)
2941 return NULL;
2942
2943 spin_lock_irqsave(&iommu_table_lock, flags);
2944
2945 table = pci_seg->irq_lookup_table[devid];
2946 if (table)
2947 goto out_unlock;
2948
2949 table = pci_seg->irq_lookup_table[alias];
2950 if (table) {
2951 set_remap_table_entry(iommu, devid, table);
2952 goto out_wait;
2953 }
2954
2955 table = new_table;
2956 new_table = NULL;
2957
2958 if (pdev)
2959 pci_for_each_dma_alias(pdev, set_remap_table_entry_alias,
2960 table);
2961 else
2962 set_remap_table_entry(iommu, devid, table);
2963
2964 if (devid != alias)
2965 set_remap_table_entry(iommu, alias, table);
2966
2967 out_wait:
2968 iommu_completion_wait(iommu);
2969
2970 out_unlock:
2971 spin_unlock_irqrestore(&iommu_table_lock, flags);
2972
2973 if (new_table) {
2974 kmem_cache_free(amd_iommu_irq_cache, new_table->table);
2975 kfree(new_table);
2976 }
2977 return table;
2978 }
2979
alloc_irq_index(struct amd_iommu * iommu,u16 devid,int count,bool align,struct pci_dev * pdev)2980 static int alloc_irq_index(struct amd_iommu *iommu, u16 devid, int count,
2981 bool align, struct pci_dev *pdev)
2982 {
2983 struct irq_remap_table *table;
2984 int index, c, alignment = 1;
2985 unsigned long flags;
2986
2987 table = alloc_irq_table(iommu, devid, pdev);
2988 if (!table)
2989 return -ENODEV;
2990
2991 if (align)
2992 alignment = roundup_pow_of_two(count);
2993
2994 raw_spin_lock_irqsave(&table->lock, flags);
2995
2996 /* Scan table for free entries */
2997 for (index = ALIGN(table->min_index, alignment), c = 0;
2998 index < MAX_IRQS_PER_TABLE;) {
2999 if (!iommu->irte_ops->is_allocated(table, index)) {
3000 c += 1;
3001 } else {
3002 c = 0;
3003 index = ALIGN(index + 1, alignment);
3004 continue;
3005 }
3006
3007 if (c == count) {
3008 for (; c != 0; --c)
3009 iommu->irte_ops->set_allocated(table, index - c + 1);
3010
3011 index -= count - 1;
3012 goto out;
3013 }
3014
3015 index++;
3016 }
3017
3018 index = -ENOSPC;
3019
3020 out:
3021 raw_spin_unlock_irqrestore(&table->lock, flags);
3022
3023 return index;
3024 }
3025
modify_irte_ga(struct amd_iommu * iommu,u16 devid,int index,struct irte_ga * irte,struct amd_ir_data * data)3026 static int modify_irte_ga(struct amd_iommu *iommu, u16 devid, int index,
3027 struct irte_ga *irte, struct amd_ir_data *data)
3028 {
3029 bool ret;
3030 struct irq_remap_table *table;
3031 unsigned long flags;
3032 struct irte_ga *entry;
3033
3034 table = get_irq_table(iommu, devid);
3035 if (!table)
3036 return -ENOMEM;
3037
3038 raw_spin_lock_irqsave(&table->lock, flags);
3039
3040 entry = (struct irte_ga *)table->table;
3041 entry = &entry[index];
3042
3043 ret = cmpxchg_double(&entry->lo.val, &entry->hi.val,
3044 entry->lo.val, entry->hi.val,
3045 irte->lo.val, irte->hi.val);
3046 /*
3047 * We use cmpxchg16 to atomically update the 128-bit IRTE,
3048 * and it cannot be updated by the hardware or other processors
3049 * behind us, so the return value of cmpxchg16 should be the
3050 * same as the old value.
3051 */
3052 WARN_ON(!ret);
3053
3054 if (data)
3055 data->ref = entry;
3056
3057 raw_spin_unlock_irqrestore(&table->lock, flags);
3058
3059 iommu_flush_irt(iommu, devid);
3060 iommu_completion_wait(iommu);
3061
3062 return 0;
3063 }
3064
modify_irte(struct amd_iommu * iommu,u16 devid,int index,union irte * irte)3065 static int modify_irte(struct amd_iommu *iommu,
3066 u16 devid, int index, union irte *irte)
3067 {
3068 struct irq_remap_table *table;
3069 unsigned long flags;
3070
3071 table = get_irq_table(iommu, devid);
3072 if (!table)
3073 return -ENOMEM;
3074
3075 raw_spin_lock_irqsave(&table->lock, flags);
3076 table->table[index] = irte->val;
3077 raw_spin_unlock_irqrestore(&table->lock, flags);
3078
3079 iommu_flush_irt(iommu, devid);
3080 iommu_completion_wait(iommu);
3081
3082 return 0;
3083 }
3084
free_irte(struct amd_iommu * iommu,u16 devid,int index)3085 static void free_irte(struct amd_iommu *iommu, u16 devid, int index)
3086 {
3087 struct irq_remap_table *table;
3088 unsigned long flags;
3089
3090 table = get_irq_table(iommu, devid);
3091 if (!table)
3092 return;
3093
3094 raw_spin_lock_irqsave(&table->lock, flags);
3095 iommu->irte_ops->clear_allocated(table, index);
3096 raw_spin_unlock_irqrestore(&table->lock, flags);
3097
3098 iommu_flush_irt(iommu, devid);
3099 iommu_completion_wait(iommu);
3100 }
3101
irte_prepare(void * entry,u32 delivery_mode,bool dest_mode,u8 vector,u32 dest_apicid,int devid)3102 static void irte_prepare(void *entry,
3103 u32 delivery_mode, bool dest_mode,
3104 u8 vector, u32 dest_apicid, int devid)
3105 {
3106 union irte *irte = (union irte *) entry;
3107
3108 irte->val = 0;
3109 irte->fields.vector = vector;
3110 irte->fields.int_type = delivery_mode;
3111 irte->fields.destination = dest_apicid;
3112 irte->fields.dm = dest_mode;
3113 irte->fields.valid = 1;
3114 }
3115
irte_ga_prepare(void * entry,u32 delivery_mode,bool dest_mode,u8 vector,u32 dest_apicid,int devid)3116 static void irte_ga_prepare(void *entry,
3117 u32 delivery_mode, bool dest_mode,
3118 u8 vector, u32 dest_apicid, int devid)
3119 {
3120 struct irte_ga *irte = (struct irte_ga *) entry;
3121
3122 irte->lo.val = 0;
3123 irte->hi.val = 0;
3124 irte->lo.fields_remap.int_type = delivery_mode;
3125 irte->lo.fields_remap.dm = dest_mode;
3126 irte->hi.fields.vector = vector;
3127 irte->lo.fields_remap.destination = APICID_TO_IRTE_DEST_LO(dest_apicid);
3128 irte->hi.fields.destination = APICID_TO_IRTE_DEST_HI(dest_apicid);
3129 irte->lo.fields_remap.valid = 1;
3130 }
3131
irte_activate(struct amd_iommu * iommu,void * entry,u16 devid,u16 index)3132 static void irte_activate(struct amd_iommu *iommu, void *entry, u16 devid, u16 index)
3133 {
3134 union irte *irte = (union irte *) entry;
3135
3136 irte->fields.valid = 1;
3137 modify_irte(iommu, devid, index, irte);
3138 }
3139
irte_ga_activate(struct amd_iommu * iommu,void * entry,u16 devid,u16 index)3140 static void irte_ga_activate(struct amd_iommu *iommu, void *entry, u16 devid, u16 index)
3141 {
3142 struct irte_ga *irte = (struct irte_ga *) entry;
3143
3144 irte->lo.fields_remap.valid = 1;
3145 modify_irte_ga(iommu, devid, index, irte, NULL);
3146 }
3147
irte_deactivate(struct amd_iommu * iommu,void * entry,u16 devid,u16 index)3148 static void irte_deactivate(struct amd_iommu *iommu, void *entry, u16 devid, u16 index)
3149 {
3150 union irte *irte = (union irte *) entry;
3151
3152 irte->fields.valid = 0;
3153 modify_irte(iommu, devid, index, irte);
3154 }
3155
irte_ga_deactivate(struct amd_iommu * iommu,void * entry,u16 devid,u16 index)3156 static void irte_ga_deactivate(struct amd_iommu *iommu, void *entry, u16 devid, u16 index)
3157 {
3158 struct irte_ga *irte = (struct irte_ga *) entry;
3159
3160 irte->lo.fields_remap.valid = 0;
3161 modify_irte_ga(iommu, devid, index, irte, NULL);
3162 }
3163
irte_set_affinity(struct amd_iommu * iommu,void * entry,u16 devid,u16 index,u8 vector,u32 dest_apicid)3164 static void irte_set_affinity(struct amd_iommu *iommu, void *entry, u16 devid, u16 index,
3165 u8 vector, u32 dest_apicid)
3166 {
3167 union irte *irte = (union irte *) entry;
3168
3169 irte->fields.vector = vector;
3170 irte->fields.destination = dest_apicid;
3171 modify_irte(iommu, devid, index, irte);
3172 }
3173
irte_ga_set_affinity(struct amd_iommu * iommu,void * entry,u16 devid,u16 index,u8 vector,u32 dest_apicid)3174 static void irte_ga_set_affinity(struct amd_iommu *iommu, void *entry, u16 devid, u16 index,
3175 u8 vector, u32 dest_apicid)
3176 {
3177 struct irte_ga *irte = (struct irte_ga *) entry;
3178
3179 if (!irte->lo.fields_remap.guest_mode) {
3180 irte->hi.fields.vector = vector;
3181 irte->lo.fields_remap.destination =
3182 APICID_TO_IRTE_DEST_LO(dest_apicid);
3183 irte->hi.fields.destination =
3184 APICID_TO_IRTE_DEST_HI(dest_apicid);
3185 modify_irte_ga(iommu, devid, index, irte, NULL);
3186 }
3187 }
3188
3189 #define IRTE_ALLOCATED (~1U)
irte_set_allocated(struct irq_remap_table * table,int index)3190 static void irte_set_allocated(struct irq_remap_table *table, int index)
3191 {
3192 table->table[index] = IRTE_ALLOCATED;
3193 }
3194
irte_ga_set_allocated(struct irq_remap_table * table,int index)3195 static void irte_ga_set_allocated(struct irq_remap_table *table, int index)
3196 {
3197 struct irte_ga *ptr = (struct irte_ga *)table->table;
3198 struct irte_ga *irte = &ptr[index];
3199
3200 memset(&irte->lo.val, 0, sizeof(u64));
3201 memset(&irte->hi.val, 0, sizeof(u64));
3202 irte->hi.fields.vector = 0xff;
3203 }
3204
irte_is_allocated(struct irq_remap_table * table,int index)3205 static bool irte_is_allocated(struct irq_remap_table *table, int index)
3206 {
3207 union irte *ptr = (union irte *)table->table;
3208 union irte *irte = &ptr[index];
3209
3210 return irte->val != 0;
3211 }
3212
irte_ga_is_allocated(struct irq_remap_table * table,int index)3213 static bool irte_ga_is_allocated(struct irq_remap_table *table, int index)
3214 {
3215 struct irte_ga *ptr = (struct irte_ga *)table->table;
3216 struct irte_ga *irte = &ptr[index];
3217
3218 return irte->hi.fields.vector != 0;
3219 }
3220
irte_clear_allocated(struct irq_remap_table * table,int index)3221 static void irte_clear_allocated(struct irq_remap_table *table, int index)
3222 {
3223 table->table[index] = 0;
3224 }
3225
irte_ga_clear_allocated(struct irq_remap_table * table,int index)3226 static void irte_ga_clear_allocated(struct irq_remap_table *table, int index)
3227 {
3228 struct irte_ga *ptr = (struct irte_ga *)table->table;
3229 struct irte_ga *irte = &ptr[index];
3230
3231 memset(&irte->lo.val, 0, sizeof(u64));
3232 memset(&irte->hi.val, 0, sizeof(u64));
3233 }
3234
get_devid(struct irq_alloc_info * info)3235 static int get_devid(struct irq_alloc_info *info)
3236 {
3237 switch (info->type) {
3238 case X86_IRQ_ALLOC_TYPE_IOAPIC:
3239 return get_ioapic_devid(info->devid);
3240 case X86_IRQ_ALLOC_TYPE_HPET:
3241 return get_hpet_devid(info->devid);
3242 case X86_IRQ_ALLOC_TYPE_PCI_MSI:
3243 case X86_IRQ_ALLOC_TYPE_PCI_MSIX:
3244 return get_device_sbdf_id(msi_desc_to_dev(info->desc));
3245 default:
3246 WARN_ON_ONCE(1);
3247 return -1;
3248 }
3249 }
3250
3251 struct irq_remap_ops amd_iommu_irq_ops = {
3252 .prepare = amd_iommu_prepare,
3253 .enable = amd_iommu_enable,
3254 .disable = amd_iommu_disable,
3255 .reenable = amd_iommu_reenable,
3256 .enable_faulting = amd_iommu_enable_faulting,
3257 };
3258
fill_msi_msg(struct msi_msg * msg,u32 index)3259 static void fill_msi_msg(struct msi_msg *msg, u32 index)
3260 {
3261 msg->data = index;
3262 msg->address_lo = 0;
3263 msg->arch_addr_lo.base_address = X86_MSI_BASE_ADDRESS_LOW;
3264 msg->address_hi = X86_MSI_BASE_ADDRESS_HIGH;
3265 }
3266
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)3267 static void irq_remapping_prepare_irte(struct amd_ir_data *data,
3268 struct irq_cfg *irq_cfg,
3269 struct irq_alloc_info *info,
3270 int devid, int index, int sub_handle)
3271 {
3272 struct irq_2_irte *irte_info = &data->irq_2_irte;
3273 struct amd_iommu *iommu = data->iommu;
3274
3275 if (!iommu)
3276 return;
3277
3278 data->irq_2_irte.devid = devid;
3279 data->irq_2_irte.index = index + sub_handle;
3280 iommu->irte_ops->prepare(data->entry, apic->delivery_mode,
3281 apic->dest_mode_logical, irq_cfg->vector,
3282 irq_cfg->dest_apicid, devid);
3283
3284 switch (info->type) {
3285 case X86_IRQ_ALLOC_TYPE_IOAPIC:
3286 case X86_IRQ_ALLOC_TYPE_HPET:
3287 case X86_IRQ_ALLOC_TYPE_PCI_MSI:
3288 case X86_IRQ_ALLOC_TYPE_PCI_MSIX:
3289 fill_msi_msg(&data->msi_entry, irte_info->index);
3290 break;
3291
3292 default:
3293 BUG_ON(1);
3294 break;
3295 }
3296 }
3297
3298 struct amd_irte_ops irte_32_ops = {
3299 .prepare = irte_prepare,
3300 .activate = irte_activate,
3301 .deactivate = irte_deactivate,
3302 .set_affinity = irte_set_affinity,
3303 .set_allocated = irte_set_allocated,
3304 .is_allocated = irte_is_allocated,
3305 .clear_allocated = irte_clear_allocated,
3306 };
3307
3308 struct amd_irte_ops irte_128_ops = {
3309 .prepare = irte_ga_prepare,
3310 .activate = irte_ga_activate,
3311 .deactivate = irte_ga_deactivate,
3312 .set_affinity = irte_ga_set_affinity,
3313 .set_allocated = irte_ga_set_allocated,
3314 .is_allocated = irte_ga_is_allocated,
3315 .clear_allocated = irte_ga_clear_allocated,
3316 };
3317
irq_remapping_alloc(struct irq_domain * domain,unsigned int virq,unsigned int nr_irqs,void * arg)3318 static int irq_remapping_alloc(struct irq_domain *domain, unsigned int virq,
3319 unsigned int nr_irqs, void *arg)
3320 {
3321 struct irq_alloc_info *info = arg;
3322 struct irq_data *irq_data;
3323 struct amd_ir_data *data = NULL;
3324 struct amd_iommu *iommu;
3325 struct irq_cfg *cfg;
3326 int i, ret, devid, seg, sbdf;
3327 int index;
3328
3329 if (!info)
3330 return -EINVAL;
3331 if (nr_irqs > 1 && info->type != X86_IRQ_ALLOC_TYPE_PCI_MSI &&
3332 info->type != X86_IRQ_ALLOC_TYPE_PCI_MSIX)
3333 return -EINVAL;
3334
3335 /*
3336 * With IRQ remapping enabled, don't need contiguous CPU vectors
3337 * to support multiple MSI interrupts.
3338 */
3339 if (info->type == X86_IRQ_ALLOC_TYPE_PCI_MSI)
3340 info->flags &= ~X86_IRQ_ALLOC_CONTIGUOUS_VECTORS;
3341
3342 sbdf = get_devid(info);
3343 if (sbdf < 0)
3344 return -EINVAL;
3345
3346 seg = PCI_SBDF_TO_SEGID(sbdf);
3347 devid = PCI_SBDF_TO_DEVID(sbdf);
3348 iommu = __rlookup_amd_iommu(seg, devid);
3349 if (!iommu)
3350 return -EINVAL;
3351
3352 ret = irq_domain_alloc_irqs_parent(domain, virq, nr_irqs, arg);
3353 if (ret < 0)
3354 return ret;
3355
3356 if (info->type == X86_IRQ_ALLOC_TYPE_IOAPIC) {
3357 struct irq_remap_table *table;
3358
3359 table = alloc_irq_table(iommu, devid, NULL);
3360 if (table) {
3361 if (!table->min_index) {
3362 /*
3363 * Keep the first 32 indexes free for IOAPIC
3364 * interrupts.
3365 */
3366 table->min_index = 32;
3367 for (i = 0; i < 32; ++i)
3368 iommu->irte_ops->set_allocated(table, i);
3369 }
3370 WARN_ON(table->min_index != 32);
3371 index = info->ioapic.pin;
3372 } else {
3373 index = -ENOMEM;
3374 }
3375 } else if (info->type == X86_IRQ_ALLOC_TYPE_PCI_MSI ||
3376 info->type == X86_IRQ_ALLOC_TYPE_PCI_MSIX) {
3377 bool align = (info->type == X86_IRQ_ALLOC_TYPE_PCI_MSI);
3378
3379 index = alloc_irq_index(iommu, devid, nr_irqs, align,
3380 msi_desc_to_pci_dev(info->desc));
3381 } else {
3382 index = alloc_irq_index(iommu, devid, nr_irqs, false, NULL);
3383 }
3384
3385 if (index < 0) {
3386 pr_warn("Failed to allocate IRTE\n");
3387 ret = index;
3388 goto out_free_parent;
3389 }
3390
3391 for (i = 0; i < nr_irqs; i++) {
3392 irq_data = irq_domain_get_irq_data(domain, virq + i);
3393 cfg = irq_data ? irqd_cfg(irq_data) : NULL;
3394 if (!cfg) {
3395 ret = -EINVAL;
3396 goto out_free_data;
3397 }
3398
3399 ret = -ENOMEM;
3400 data = kzalloc(sizeof(*data), GFP_KERNEL);
3401 if (!data)
3402 goto out_free_data;
3403
3404 if (!AMD_IOMMU_GUEST_IR_GA(amd_iommu_guest_ir))
3405 data->entry = kzalloc(sizeof(union irte), GFP_KERNEL);
3406 else
3407 data->entry = kzalloc(sizeof(struct irte_ga),
3408 GFP_KERNEL);
3409 if (!data->entry) {
3410 kfree(data);
3411 goto out_free_data;
3412 }
3413
3414 data->iommu = iommu;
3415 irq_data->hwirq = (devid << 16) + i;
3416 irq_data->chip_data = data;
3417 irq_data->chip = &amd_ir_chip;
3418 irq_remapping_prepare_irte(data, cfg, info, devid, index, i);
3419 irq_set_status_flags(virq + i, IRQ_MOVE_PCNTXT);
3420 }
3421
3422 return 0;
3423
3424 out_free_data:
3425 for (i--; i >= 0; i--) {
3426 irq_data = irq_domain_get_irq_data(domain, virq + i);
3427 if (irq_data)
3428 kfree(irq_data->chip_data);
3429 }
3430 for (i = 0; i < nr_irqs; i++)
3431 free_irte(iommu, devid, index + i);
3432 out_free_parent:
3433 irq_domain_free_irqs_common(domain, virq, nr_irqs);
3434 return ret;
3435 }
3436
irq_remapping_free(struct irq_domain * domain,unsigned int virq,unsigned int nr_irqs)3437 static void irq_remapping_free(struct irq_domain *domain, unsigned int virq,
3438 unsigned int nr_irqs)
3439 {
3440 struct irq_2_irte *irte_info;
3441 struct irq_data *irq_data;
3442 struct amd_ir_data *data;
3443 int i;
3444
3445 for (i = 0; i < nr_irqs; i++) {
3446 irq_data = irq_domain_get_irq_data(domain, virq + i);
3447 if (irq_data && irq_data->chip_data) {
3448 data = irq_data->chip_data;
3449 irte_info = &data->irq_2_irte;
3450 free_irte(data->iommu, irte_info->devid, irte_info->index);
3451 kfree(data->entry);
3452 kfree(data);
3453 }
3454 }
3455 irq_domain_free_irqs_common(domain, virq, nr_irqs);
3456 }
3457
3458 static void amd_ir_update_irte(struct irq_data *irqd, struct amd_iommu *iommu,
3459 struct amd_ir_data *ir_data,
3460 struct irq_2_irte *irte_info,
3461 struct irq_cfg *cfg);
3462
irq_remapping_activate(struct irq_domain * domain,struct irq_data * irq_data,bool reserve)3463 static int irq_remapping_activate(struct irq_domain *domain,
3464 struct irq_data *irq_data, bool reserve)
3465 {
3466 struct amd_ir_data *data = irq_data->chip_data;
3467 struct irq_2_irte *irte_info = &data->irq_2_irte;
3468 struct amd_iommu *iommu = data->iommu;
3469 struct irq_cfg *cfg = irqd_cfg(irq_data);
3470
3471 if (!iommu)
3472 return 0;
3473
3474 iommu->irte_ops->activate(iommu, data->entry, irte_info->devid,
3475 irte_info->index);
3476 amd_ir_update_irte(irq_data, iommu, data, irte_info, cfg);
3477 return 0;
3478 }
3479
irq_remapping_deactivate(struct irq_domain * domain,struct irq_data * irq_data)3480 static void irq_remapping_deactivate(struct irq_domain *domain,
3481 struct irq_data *irq_data)
3482 {
3483 struct amd_ir_data *data = irq_data->chip_data;
3484 struct irq_2_irte *irte_info = &data->irq_2_irte;
3485 struct amd_iommu *iommu = data->iommu;
3486
3487 if (iommu)
3488 iommu->irte_ops->deactivate(iommu, data->entry, irte_info->devid,
3489 irte_info->index);
3490 }
3491
irq_remapping_select(struct irq_domain * d,struct irq_fwspec * fwspec,enum irq_domain_bus_token bus_token)3492 static int irq_remapping_select(struct irq_domain *d, struct irq_fwspec *fwspec,
3493 enum irq_domain_bus_token bus_token)
3494 {
3495 struct amd_iommu *iommu;
3496 int devid = -1;
3497
3498 if (!amd_iommu_irq_remap)
3499 return 0;
3500
3501 if (x86_fwspec_is_ioapic(fwspec))
3502 devid = get_ioapic_devid(fwspec->param[0]);
3503 else if (x86_fwspec_is_hpet(fwspec))
3504 devid = get_hpet_devid(fwspec->param[0]);
3505
3506 if (devid < 0)
3507 return 0;
3508 iommu = __rlookup_amd_iommu((devid >> 16), (devid & 0xffff));
3509
3510 return iommu && iommu->ir_domain == d;
3511 }
3512
3513 static const struct irq_domain_ops amd_ir_domain_ops = {
3514 .select = irq_remapping_select,
3515 .alloc = irq_remapping_alloc,
3516 .free = irq_remapping_free,
3517 .activate = irq_remapping_activate,
3518 .deactivate = irq_remapping_deactivate,
3519 };
3520
amd_iommu_activate_guest_mode(void * data)3521 int amd_iommu_activate_guest_mode(void *data)
3522 {
3523 struct amd_ir_data *ir_data = (struct amd_ir_data *)data;
3524 struct irte_ga *entry = (struct irte_ga *) ir_data->entry;
3525 u64 valid;
3526
3527 if (!AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir) || !entry)
3528 return 0;
3529
3530 valid = entry->lo.fields_vapic.valid;
3531
3532 entry->lo.val = 0;
3533 entry->hi.val = 0;
3534
3535 entry->lo.fields_vapic.valid = valid;
3536 entry->lo.fields_vapic.guest_mode = 1;
3537 entry->lo.fields_vapic.ga_log_intr = 1;
3538 entry->hi.fields.ga_root_ptr = ir_data->ga_root_ptr;
3539 entry->hi.fields.vector = ir_data->ga_vector;
3540 entry->lo.fields_vapic.ga_tag = ir_data->ga_tag;
3541
3542 return modify_irte_ga(ir_data->iommu, ir_data->irq_2_irte.devid,
3543 ir_data->irq_2_irte.index, entry, ir_data);
3544 }
3545 EXPORT_SYMBOL(amd_iommu_activate_guest_mode);
3546
amd_iommu_deactivate_guest_mode(void * data)3547 int amd_iommu_deactivate_guest_mode(void *data)
3548 {
3549 struct amd_ir_data *ir_data = (struct amd_ir_data *)data;
3550 struct irte_ga *entry = (struct irte_ga *) ir_data->entry;
3551 struct irq_cfg *cfg = ir_data->cfg;
3552 u64 valid;
3553
3554 if (!AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir) ||
3555 !entry || !entry->lo.fields_vapic.guest_mode)
3556 return 0;
3557
3558 valid = entry->lo.fields_remap.valid;
3559
3560 entry->lo.val = 0;
3561 entry->hi.val = 0;
3562
3563 entry->lo.fields_remap.valid = valid;
3564 entry->lo.fields_remap.dm = apic->dest_mode_logical;
3565 entry->lo.fields_remap.int_type = apic->delivery_mode;
3566 entry->hi.fields.vector = cfg->vector;
3567 entry->lo.fields_remap.destination =
3568 APICID_TO_IRTE_DEST_LO(cfg->dest_apicid);
3569 entry->hi.fields.destination =
3570 APICID_TO_IRTE_DEST_HI(cfg->dest_apicid);
3571
3572 return modify_irte_ga(ir_data->iommu, ir_data->irq_2_irte.devid,
3573 ir_data->irq_2_irte.index, entry, ir_data);
3574 }
3575 EXPORT_SYMBOL(amd_iommu_deactivate_guest_mode);
3576
amd_ir_set_vcpu_affinity(struct irq_data * data,void * vcpu_info)3577 static int amd_ir_set_vcpu_affinity(struct irq_data *data, void *vcpu_info)
3578 {
3579 int ret;
3580 struct amd_iommu_pi_data *pi_data = vcpu_info;
3581 struct vcpu_data *vcpu_pi_info = pi_data->vcpu_data;
3582 struct amd_ir_data *ir_data = data->chip_data;
3583 struct irq_2_irte *irte_info = &ir_data->irq_2_irte;
3584 struct iommu_dev_data *dev_data;
3585
3586 if (ir_data->iommu == NULL)
3587 return -EINVAL;
3588
3589 dev_data = search_dev_data(ir_data->iommu, irte_info->devid);
3590
3591 /* Note:
3592 * This device has never been set up for guest mode.
3593 * we should not modify the IRTE
3594 */
3595 if (!dev_data || !dev_data->use_vapic)
3596 return 0;
3597
3598 ir_data->cfg = irqd_cfg(data);
3599 pi_data->ir_data = ir_data;
3600
3601 /* Note:
3602 * SVM tries to set up for VAPIC mode, but we are in
3603 * legacy mode. So, we force legacy mode instead.
3604 */
3605 if (!AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir)) {
3606 pr_debug("%s: Fall back to using intr legacy remap\n",
3607 __func__);
3608 pi_data->is_guest_mode = false;
3609 }
3610
3611 pi_data->prev_ga_tag = ir_data->cached_ga_tag;
3612 if (pi_data->is_guest_mode) {
3613 ir_data->ga_root_ptr = (pi_data->base >> 12);
3614 ir_data->ga_vector = vcpu_pi_info->vector;
3615 ir_data->ga_tag = pi_data->ga_tag;
3616 ret = amd_iommu_activate_guest_mode(ir_data);
3617 if (!ret)
3618 ir_data->cached_ga_tag = pi_data->ga_tag;
3619 } else {
3620 ret = amd_iommu_deactivate_guest_mode(ir_data);
3621
3622 /*
3623 * This communicates the ga_tag back to the caller
3624 * so that it can do all the necessary clean up.
3625 */
3626 if (!ret)
3627 ir_data->cached_ga_tag = 0;
3628 }
3629
3630 return ret;
3631 }
3632
3633
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)3634 static void amd_ir_update_irte(struct irq_data *irqd, struct amd_iommu *iommu,
3635 struct amd_ir_data *ir_data,
3636 struct irq_2_irte *irte_info,
3637 struct irq_cfg *cfg)
3638 {
3639
3640 /*
3641 * Atomically updates the IRTE with the new destination, vector
3642 * and flushes the interrupt entry cache.
3643 */
3644 iommu->irte_ops->set_affinity(iommu, ir_data->entry, irte_info->devid,
3645 irte_info->index, cfg->vector,
3646 cfg->dest_apicid);
3647 }
3648
amd_ir_set_affinity(struct irq_data * data,const struct cpumask * mask,bool force)3649 static int amd_ir_set_affinity(struct irq_data *data,
3650 const struct cpumask *mask, bool force)
3651 {
3652 struct amd_ir_data *ir_data = data->chip_data;
3653 struct irq_2_irte *irte_info = &ir_data->irq_2_irte;
3654 struct irq_cfg *cfg = irqd_cfg(data);
3655 struct irq_data *parent = data->parent_data;
3656 struct amd_iommu *iommu = ir_data->iommu;
3657 int ret;
3658
3659 if (!iommu)
3660 return -ENODEV;
3661
3662 ret = parent->chip->irq_set_affinity(parent, mask, force);
3663 if (ret < 0 || ret == IRQ_SET_MASK_OK_DONE)
3664 return ret;
3665
3666 amd_ir_update_irte(data, iommu, ir_data, irte_info, cfg);
3667 /*
3668 * After this point, all the interrupts will start arriving
3669 * at the new destination. So, time to cleanup the previous
3670 * vector allocation.
3671 */
3672 send_cleanup_vector(cfg);
3673
3674 return IRQ_SET_MASK_OK_DONE;
3675 }
3676
ir_compose_msi_msg(struct irq_data * irq_data,struct msi_msg * msg)3677 static void ir_compose_msi_msg(struct irq_data *irq_data, struct msi_msg *msg)
3678 {
3679 struct amd_ir_data *ir_data = irq_data->chip_data;
3680
3681 *msg = ir_data->msi_entry;
3682 }
3683
3684 static struct irq_chip amd_ir_chip = {
3685 .name = "AMD-IR",
3686 .irq_ack = apic_ack_irq,
3687 .irq_set_affinity = amd_ir_set_affinity,
3688 .irq_set_vcpu_affinity = amd_ir_set_vcpu_affinity,
3689 .irq_compose_msi_msg = ir_compose_msi_msg,
3690 };
3691
amd_iommu_create_irq_domain(struct amd_iommu * iommu)3692 int amd_iommu_create_irq_domain(struct amd_iommu *iommu)
3693 {
3694 struct fwnode_handle *fn;
3695
3696 fn = irq_domain_alloc_named_id_fwnode("AMD-IR", iommu->index);
3697 if (!fn)
3698 return -ENOMEM;
3699 iommu->ir_domain = irq_domain_create_tree(fn, &amd_ir_domain_ops, iommu);
3700 if (!iommu->ir_domain) {
3701 irq_domain_free_fwnode(fn);
3702 return -ENOMEM;
3703 }
3704
3705 iommu->ir_domain->parent = arch_get_ir_parent_domain();
3706 iommu->msi_domain = arch_create_remap_msi_irq_domain(iommu->ir_domain,
3707 "AMD-IR-MSI",
3708 iommu->index);
3709 return 0;
3710 }
3711
amd_iommu_update_ga(int cpu,bool is_run,void * data)3712 int amd_iommu_update_ga(int cpu, bool is_run, void *data)
3713 {
3714 unsigned long flags;
3715 struct amd_iommu *iommu;
3716 struct irq_remap_table *table;
3717 struct amd_ir_data *ir_data = (struct amd_ir_data *)data;
3718 int devid = ir_data->irq_2_irte.devid;
3719 struct irte_ga *entry = (struct irte_ga *) ir_data->entry;
3720 struct irte_ga *ref = (struct irte_ga *) ir_data->ref;
3721
3722 if (!AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir) ||
3723 !ref || !entry || !entry->lo.fields_vapic.guest_mode)
3724 return 0;
3725
3726 iommu = ir_data->iommu;
3727 if (!iommu)
3728 return -ENODEV;
3729
3730 table = get_irq_table(iommu, devid);
3731 if (!table)
3732 return -ENODEV;
3733
3734 raw_spin_lock_irqsave(&table->lock, flags);
3735
3736 if (ref->lo.fields_vapic.guest_mode) {
3737 if (cpu >= 0) {
3738 ref->lo.fields_vapic.destination =
3739 APICID_TO_IRTE_DEST_LO(cpu);
3740 ref->hi.fields.destination =
3741 APICID_TO_IRTE_DEST_HI(cpu);
3742 }
3743 ref->lo.fields_vapic.is_run = is_run;
3744 barrier();
3745 }
3746
3747 raw_spin_unlock_irqrestore(&table->lock, flags);
3748
3749 iommu_flush_irt(iommu, devid);
3750 iommu_completion_wait(iommu);
3751 return 0;
3752 }
3753 EXPORT_SYMBOL(amd_iommu_update_ga);
3754 #endif
3755