1ad8694baSJoerg Roedel /* SPDX-License-Identifier: GPL-2.0-only */
2ad8694baSJoerg Roedel /*
3ad8694baSJoerg Roedel * Copyright (C) 2009-2010 Advanced Micro Devices, Inc.
4ad8694baSJoerg Roedel * Author: Joerg Roedel <jroedel@suse.de>
5ad8694baSJoerg Roedel */
6ad8694baSJoerg Roedel
7ad8694baSJoerg Roedel #ifndef AMD_IOMMU_H
8ad8694baSJoerg Roedel #define AMD_IOMMU_H
9ad8694baSJoerg Roedel
10ad8694baSJoerg Roedel #include <linux/iommu.h>
11ad8694baSJoerg Roedel
12ad8694baSJoerg Roedel #include "amd_iommu_types.h"
13ad8694baSJoerg Roedel
1478db2985SVasant Hegde irqreturn_t amd_iommu_int_thread(int irq, void *data);
15*2379f348SVasant Hegde irqreturn_t amd_iommu_int_thread_evtlog(int irq, void *data);
16*2379f348SVasant Hegde irqreturn_t amd_iommu_int_thread_pprlog(int irq, void *data);
17*2379f348SVasant Hegde irqreturn_t amd_iommu_int_thread_galog(int irq, void *data);
1878db2985SVasant Hegde irqreturn_t amd_iommu_int_handler(int irq, void *data);
1978db2985SVasant Hegde void amd_iommu_apply_erratum_63(struct amd_iommu *iommu, u16 devid);
2078db2985SVasant Hegde void amd_iommu_restart_event_logging(struct amd_iommu *iommu);
21a7a33407SJoerg Roedel void amd_iommu_restart_ga_log(struct amd_iommu *iommu);
22274c2218SVasant Hegde void amd_iommu_restart_ppr_log(struct amd_iommu *iommu);
2378db2985SVasant Hegde void amd_iommu_set_rlookup_table(struct amd_iommu *iommu, u16 devid);
24ad8694baSJoerg Roedel
25ad8694baSJoerg Roedel #ifdef CONFIG_AMD_IOMMU_DEBUGFS
26ad8694baSJoerg Roedel void amd_iommu_debugfs_setup(struct amd_iommu *iommu);
27ad8694baSJoerg Roedel #else
amd_iommu_debugfs_setup(struct amd_iommu * iommu)28ad8694baSJoerg Roedel static inline void amd_iommu_debugfs_setup(struct amd_iommu *iommu) {}
29ad8694baSJoerg Roedel #endif
30ad8694baSJoerg Roedel
31ad8694baSJoerg Roedel /* Needed for interrupt remapping */
3278db2985SVasant Hegde int amd_iommu_prepare(void);
3378db2985SVasant Hegde int amd_iommu_enable(void);
3478db2985SVasant Hegde void amd_iommu_disable(void);
3578db2985SVasant Hegde int amd_iommu_reenable(int mode);
3678db2985SVasant Hegde int amd_iommu_enable_faulting(void);
37ad8694baSJoerg Roedel extern int amd_iommu_guest_ir;
3889c9a09cSSuravee Suthikulpanit extern enum io_pgtable_fmt amd_iommu_pgtable;
39f5944964SVasant Hegde extern int amd_iommu_gpt_level;
40ad8694baSJoerg Roedel
41ad8694baSJoerg Roedel /* IOMMUv2 specific functions */
42ad8694baSJoerg Roedel struct iommu_domain;
43ad8694baSJoerg Roedel
4478db2985SVasant Hegde bool amd_iommu_v2_supported(void);
4578db2985SVasant Hegde struct amd_iommu *get_amd_iommu(unsigned int idx);
4678db2985SVasant Hegde u8 amd_iommu_pc_get_max_banks(unsigned int idx);
4778db2985SVasant Hegde bool amd_iommu_pc_supported(void);
4878db2985SVasant Hegde u8 amd_iommu_pc_get_max_counters(unsigned int idx);
4978db2985SVasant Hegde int amd_iommu_pc_get_reg(struct amd_iommu *iommu, u8 bank, u8 cntr,
509bdc2ef6SKrzysztof Kozlowski u8 fxn, u64 *value);
5178db2985SVasant Hegde int amd_iommu_pc_set_reg(struct amd_iommu *iommu, u8 bank, u8 cntr,
529bdc2ef6SKrzysztof Kozlowski u8 fxn, u64 *value);
539bdc2ef6SKrzysztof Kozlowski
5478db2985SVasant Hegde int amd_iommu_register_ppr_notifier(struct notifier_block *nb);
5578db2985SVasant Hegde int amd_iommu_unregister_ppr_notifier(struct notifier_block *nb);
5678db2985SVasant Hegde void amd_iommu_domain_direct_map(struct iommu_domain *dom);
5778db2985SVasant Hegde int amd_iommu_domain_enable_v2(struct iommu_domain *dom, int pasids);
5878db2985SVasant Hegde int amd_iommu_flush_page(struct iommu_domain *dom, u32 pasid, u64 address);
5978db2985SVasant Hegde void amd_iommu_update_and_flush_device_table(struct protection_domain *domain);
6078db2985SVasant Hegde void amd_iommu_domain_update(struct protection_domain *domain);
6178db2985SVasant Hegde void amd_iommu_domain_flush_complete(struct protection_domain *domain);
6278db2985SVasant Hegde void amd_iommu_domain_flush_tlb_pde(struct protection_domain *domain);
6378db2985SVasant Hegde int amd_iommu_flush_tlb(struct iommu_domain *dom, u32 pasid);
6478db2985SVasant Hegde int amd_iommu_domain_set_gcr3(struct iommu_domain *dom, u32 pasid,
65ad8694baSJoerg Roedel unsigned long cr3);
6678db2985SVasant Hegde int amd_iommu_domain_clear_gcr3(struct iommu_domain *dom, u32 pasid);
67ad8694baSJoerg Roedel
68ad8694baSJoerg Roedel #ifdef CONFIG_IRQ_REMAP
6978db2985SVasant Hegde int amd_iommu_create_irq_domain(struct amd_iommu *iommu);
70ad8694baSJoerg Roedel #else
amd_iommu_create_irq_domain(struct amd_iommu * iommu)71ad8694baSJoerg Roedel static inline int amd_iommu_create_irq_domain(struct amd_iommu *iommu)
72ad8694baSJoerg Roedel {
73ad8694baSJoerg Roedel return 0;
74ad8694baSJoerg Roedel }
75ad8694baSJoerg Roedel #endif
76ad8694baSJoerg Roedel
77ad8694baSJoerg Roedel #define PPR_SUCCESS 0x0
78ad8694baSJoerg Roedel #define PPR_INVALID 0x1
79ad8694baSJoerg Roedel #define PPR_FAILURE 0xf
80ad8694baSJoerg Roedel
8178db2985SVasant Hegde int amd_iommu_complete_ppr(struct pci_dev *pdev, u32 pasid,
82ad8694baSJoerg Roedel int status, int tag);
83ad8694baSJoerg Roedel
is_rd890_iommu(struct pci_dev * pdev)84ad8694baSJoerg Roedel static inline bool is_rd890_iommu(struct pci_dev *pdev)
85ad8694baSJoerg Roedel {
86ad8694baSJoerg Roedel return (pdev->vendor == PCI_VENDOR_ID_ATI) &&
87ad8694baSJoerg Roedel (pdev->device == PCI_DEVICE_ID_RD890_IOMMU);
88ad8694baSJoerg Roedel }
89ad8694baSJoerg Roedel
iommu_feature(struct amd_iommu * iommu,u64 mask)90a44092e3SSuravee Suthikulpanit static inline bool iommu_feature(struct amd_iommu *iommu, u64 mask)
91ad8694baSJoerg Roedel {
92a44092e3SSuravee Suthikulpanit return !!(iommu->features & mask);
93ad8694baSJoerg Roedel }
94ad8694baSJoerg Roedel
iommu_virt_to_phys(void * vaddr)95ad8694baSJoerg Roedel static inline u64 iommu_virt_to_phys(void *vaddr)
96ad8694baSJoerg Roedel {
97ad8694baSJoerg Roedel return (u64)__sme_set(virt_to_phys(vaddr));
98ad8694baSJoerg Roedel }
99ad8694baSJoerg Roedel
iommu_phys_to_virt(unsigned long paddr)100ad8694baSJoerg Roedel static inline void *iommu_phys_to_virt(unsigned long paddr)
101ad8694baSJoerg Roedel {
102ad8694baSJoerg Roedel return phys_to_virt(__sme_clr(paddr));
103ad8694baSJoerg Roedel }
104ad8694baSJoerg Roedel
1058c112a6bSSuravee Suthikulpanit static inline
amd_iommu_domain_set_pt_root(struct protection_domain * domain,u64 root)1068c112a6bSSuravee Suthikulpanit void amd_iommu_domain_set_pt_root(struct protection_domain *domain, u64 root)
1078c112a6bSSuravee Suthikulpanit {
108d2272ec7SSuravee Suthikulpanit atomic64_set(&domain->iop.pt_root, root);
1096eedb59cSSuravee Suthikulpanit domain->iop.root = (u64 *)(root & PAGE_MASK);
1106eedb59cSSuravee Suthikulpanit domain->iop.mode = root & 7; /* lowest 3 bits encode pgtable mode */
1118c112a6bSSuravee Suthikulpanit }
1128c112a6bSSuravee Suthikulpanit
1138c112a6bSSuravee Suthikulpanit static inline
amd_iommu_domain_clr_pt_root(struct protection_domain * domain)1148c112a6bSSuravee Suthikulpanit void amd_iommu_domain_clr_pt_root(struct protection_domain *domain)
1158c112a6bSSuravee Suthikulpanit {
1168c112a6bSSuravee Suthikulpanit amd_iommu_domain_set_pt_root(domain, 0);
1178c112a6bSSuravee Suthikulpanit }
1188c112a6bSSuravee Suthikulpanit
get_pci_sbdf_id(struct pci_dev * pdev)119bf87972cSSuravee Suthikulpanit static inline int get_pci_sbdf_id(struct pci_dev *pdev)
120bf87972cSSuravee Suthikulpanit {
121bf87972cSSuravee Suthikulpanit int seg = pci_domain_nr(pdev->bus);
122bf87972cSSuravee Suthikulpanit u16 devid = pci_dev_id(pdev);
123bf87972cSSuravee Suthikulpanit
124bf87972cSSuravee Suthikulpanit return PCI_SEG_DEVID_TO_SBDF(seg, devid);
125bf87972cSSuravee Suthikulpanit }
1268c112a6bSSuravee Suthikulpanit
alloc_pgtable_page(int nid,gfp_t gfp)1270d571dcbSVasant Hegde static inline void *alloc_pgtable_page(int nid, gfp_t gfp)
1280d571dcbSVasant Hegde {
1290d571dcbSVasant Hegde struct page *page;
1300d571dcbSVasant Hegde
1310d571dcbSVasant Hegde page = alloc_pages_node(nid, gfp | __GFP_ZERO, 0);
1320d571dcbSVasant Hegde return page ? page_address(page) : NULL;
1330d571dcbSVasant Hegde }
1340d571dcbSVasant Hegde
13578db2985SVasant Hegde bool translation_pre_enabled(struct amd_iommu *iommu);
13678db2985SVasant Hegde bool amd_iommu_is_attach_deferred(struct device *dev);
13778db2985SVasant Hegde int __init add_special_device(u8 type, u8 id, u32 *devid, bool cmd_line);
138ad8694baSJoerg Roedel
139ad8694baSJoerg Roedel #ifdef CONFIG_DMI
140ad8694baSJoerg Roedel void amd_iommu_apply_ivrs_quirks(void);
141ad8694baSJoerg Roedel #else
amd_iommu_apply_ivrs_quirks(void)142e7fc2383SJoerg Roedel static inline void amd_iommu_apply_ivrs_quirks(void) { }
143ad8694baSJoerg Roedel #endif
144ad8694baSJoerg Roedel
14578db2985SVasant Hegde void amd_iommu_domain_set_pgtable(struct protection_domain *domain,
14618954252SSuravee Suthikulpanit u64 *root, int mode);
14778db2985SVasant Hegde struct dev_table_entry *get_dev_table(struct amd_iommu *iommu);
1489dd299d8SSuravee Suthikulpanit
1499dd299d8SSuravee Suthikulpanit extern u64 amd_iommu_efr;
1509dd299d8SSuravee Suthikulpanit extern u64 amd_iommu_efr2;
151fb2accadSBrijesh Singh
152fb2accadSBrijesh Singh extern bool amd_iommu_snp_en;
153ad8694baSJoerg Roedel #endif
154