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