162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-only */
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Copyright (C) 2009-2010 Advanced Micro Devices, Inc.
462306a36Sopenharmony_ci * Author: Joerg Roedel <jroedel@suse.de>
562306a36Sopenharmony_ci */
662306a36Sopenharmony_ci
762306a36Sopenharmony_ci#ifndef AMD_IOMMU_H
862306a36Sopenharmony_ci#define AMD_IOMMU_H
962306a36Sopenharmony_ci
1062306a36Sopenharmony_ci#include <linux/iommu.h>
1162306a36Sopenharmony_ci
1262306a36Sopenharmony_ci#include "amd_iommu_types.h"
1362306a36Sopenharmony_ci
1462306a36Sopenharmony_ciirqreturn_t amd_iommu_int_thread(int irq, void *data);
1562306a36Sopenharmony_ciirqreturn_t amd_iommu_int_thread_evtlog(int irq, void *data);
1662306a36Sopenharmony_ciirqreturn_t amd_iommu_int_thread_pprlog(int irq, void *data);
1762306a36Sopenharmony_ciirqreturn_t amd_iommu_int_thread_galog(int irq, void *data);
1862306a36Sopenharmony_ciirqreturn_t amd_iommu_int_handler(int irq, void *data);
1962306a36Sopenharmony_civoid amd_iommu_apply_erratum_63(struct amd_iommu *iommu, u16 devid);
2062306a36Sopenharmony_civoid amd_iommu_restart_event_logging(struct amd_iommu *iommu);
2162306a36Sopenharmony_civoid amd_iommu_restart_ga_log(struct amd_iommu *iommu);
2262306a36Sopenharmony_civoid amd_iommu_restart_ppr_log(struct amd_iommu *iommu);
2362306a36Sopenharmony_civoid amd_iommu_set_rlookup_table(struct amd_iommu *iommu, u16 devid);
2462306a36Sopenharmony_ci
2562306a36Sopenharmony_ci#ifdef CONFIG_AMD_IOMMU_DEBUGFS
2662306a36Sopenharmony_civoid amd_iommu_debugfs_setup(struct amd_iommu *iommu);
2762306a36Sopenharmony_ci#else
2862306a36Sopenharmony_cistatic inline void amd_iommu_debugfs_setup(struct amd_iommu *iommu) {}
2962306a36Sopenharmony_ci#endif
3062306a36Sopenharmony_ci
3162306a36Sopenharmony_ci/* Needed for interrupt remapping */
3262306a36Sopenharmony_ciint amd_iommu_prepare(void);
3362306a36Sopenharmony_ciint amd_iommu_enable(void);
3462306a36Sopenharmony_civoid amd_iommu_disable(void);
3562306a36Sopenharmony_ciint amd_iommu_reenable(int mode);
3662306a36Sopenharmony_ciint amd_iommu_enable_faulting(void);
3762306a36Sopenharmony_ciextern int amd_iommu_guest_ir;
3862306a36Sopenharmony_ciextern enum io_pgtable_fmt amd_iommu_pgtable;
3962306a36Sopenharmony_ciextern int amd_iommu_gpt_level;
4062306a36Sopenharmony_ci
4162306a36Sopenharmony_ci/* IOMMUv2 specific functions */
4262306a36Sopenharmony_cistruct iommu_domain;
4362306a36Sopenharmony_ci
4462306a36Sopenharmony_cibool amd_iommu_v2_supported(void);
4562306a36Sopenharmony_cistruct amd_iommu *get_amd_iommu(unsigned int idx);
4662306a36Sopenharmony_ciu8 amd_iommu_pc_get_max_banks(unsigned int idx);
4762306a36Sopenharmony_cibool amd_iommu_pc_supported(void);
4862306a36Sopenharmony_ciu8 amd_iommu_pc_get_max_counters(unsigned int idx);
4962306a36Sopenharmony_ciint amd_iommu_pc_get_reg(struct amd_iommu *iommu, u8 bank, u8 cntr,
5062306a36Sopenharmony_ci			 u8 fxn, u64 *value);
5162306a36Sopenharmony_ciint amd_iommu_pc_set_reg(struct amd_iommu *iommu, u8 bank, u8 cntr,
5262306a36Sopenharmony_ci			 u8 fxn, u64 *value);
5362306a36Sopenharmony_ci
5462306a36Sopenharmony_ciint amd_iommu_register_ppr_notifier(struct notifier_block *nb);
5562306a36Sopenharmony_ciint amd_iommu_unregister_ppr_notifier(struct notifier_block *nb);
5662306a36Sopenharmony_civoid amd_iommu_domain_direct_map(struct iommu_domain *dom);
5762306a36Sopenharmony_ciint amd_iommu_domain_enable_v2(struct iommu_domain *dom, int pasids);
5862306a36Sopenharmony_ciint amd_iommu_flush_page(struct iommu_domain *dom, u32 pasid, u64 address);
5962306a36Sopenharmony_civoid amd_iommu_update_and_flush_device_table(struct protection_domain *domain);
6062306a36Sopenharmony_civoid amd_iommu_domain_update(struct protection_domain *domain);
6162306a36Sopenharmony_civoid amd_iommu_domain_flush_complete(struct protection_domain *domain);
6262306a36Sopenharmony_civoid amd_iommu_domain_flush_tlb_pde(struct protection_domain *domain);
6362306a36Sopenharmony_ciint amd_iommu_flush_tlb(struct iommu_domain *dom, u32 pasid);
6462306a36Sopenharmony_ciint amd_iommu_domain_set_gcr3(struct iommu_domain *dom, u32 pasid,
6562306a36Sopenharmony_ci			      unsigned long cr3);
6662306a36Sopenharmony_ciint amd_iommu_domain_clear_gcr3(struct iommu_domain *dom, u32 pasid);
6762306a36Sopenharmony_ci
6862306a36Sopenharmony_ci#ifdef CONFIG_IRQ_REMAP
6962306a36Sopenharmony_ciint amd_iommu_create_irq_domain(struct amd_iommu *iommu);
7062306a36Sopenharmony_ci#else
7162306a36Sopenharmony_cistatic inline int amd_iommu_create_irq_domain(struct amd_iommu *iommu)
7262306a36Sopenharmony_ci{
7362306a36Sopenharmony_ci	return 0;
7462306a36Sopenharmony_ci}
7562306a36Sopenharmony_ci#endif
7662306a36Sopenharmony_ci
7762306a36Sopenharmony_ci#define PPR_SUCCESS			0x0
7862306a36Sopenharmony_ci#define PPR_INVALID			0x1
7962306a36Sopenharmony_ci#define PPR_FAILURE			0xf
8062306a36Sopenharmony_ci
8162306a36Sopenharmony_ciint amd_iommu_complete_ppr(struct pci_dev *pdev, u32 pasid,
8262306a36Sopenharmony_ci			   int status, int tag);
8362306a36Sopenharmony_ci
8462306a36Sopenharmony_cistatic inline bool is_rd890_iommu(struct pci_dev *pdev)
8562306a36Sopenharmony_ci{
8662306a36Sopenharmony_ci	return (pdev->vendor == PCI_VENDOR_ID_ATI) &&
8762306a36Sopenharmony_ci	       (pdev->device == PCI_DEVICE_ID_RD890_IOMMU);
8862306a36Sopenharmony_ci}
8962306a36Sopenharmony_ci
9062306a36Sopenharmony_cistatic inline bool iommu_feature(struct amd_iommu *iommu, u64 mask)
9162306a36Sopenharmony_ci{
9262306a36Sopenharmony_ci	return !!(iommu->features & mask);
9362306a36Sopenharmony_ci}
9462306a36Sopenharmony_ci
9562306a36Sopenharmony_cistatic inline u64 iommu_virt_to_phys(void *vaddr)
9662306a36Sopenharmony_ci{
9762306a36Sopenharmony_ci	return (u64)__sme_set(virt_to_phys(vaddr));
9862306a36Sopenharmony_ci}
9962306a36Sopenharmony_ci
10062306a36Sopenharmony_cistatic inline void *iommu_phys_to_virt(unsigned long paddr)
10162306a36Sopenharmony_ci{
10262306a36Sopenharmony_ci	return phys_to_virt(__sme_clr(paddr));
10362306a36Sopenharmony_ci}
10462306a36Sopenharmony_ci
10562306a36Sopenharmony_cistatic inline
10662306a36Sopenharmony_civoid amd_iommu_domain_set_pt_root(struct protection_domain *domain, u64 root)
10762306a36Sopenharmony_ci{
10862306a36Sopenharmony_ci	atomic64_set(&domain->iop.pt_root, root);
10962306a36Sopenharmony_ci	domain->iop.root = (u64 *)(root & PAGE_MASK);
11062306a36Sopenharmony_ci	domain->iop.mode = root & 7; /* lowest 3 bits encode pgtable mode */
11162306a36Sopenharmony_ci}
11262306a36Sopenharmony_ci
11362306a36Sopenharmony_cistatic inline
11462306a36Sopenharmony_civoid amd_iommu_domain_clr_pt_root(struct protection_domain *domain)
11562306a36Sopenharmony_ci{
11662306a36Sopenharmony_ci	amd_iommu_domain_set_pt_root(domain, 0);
11762306a36Sopenharmony_ci}
11862306a36Sopenharmony_ci
11962306a36Sopenharmony_cistatic inline int get_pci_sbdf_id(struct pci_dev *pdev)
12062306a36Sopenharmony_ci{
12162306a36Sopenharmony_ci	int seg = pci_domain_nr(pdev->bus);
12262306a36Sopenharmony_ci	u16 devid = pci_dev_id(pdev);
12362306a36Sopenharmony_ci
12462306a36Sopenharmony_ci	return PCI_SEG_DEVID_TO_SBDF(seg, devid);
12562306a36Sopenharmony_ci}
12662306a36Sopenharmony_ci
12762306a36Sopenharmony_cistatic inline void *alloc_pgtable_page(int nid, gfp_t gfp)
12862306a36Sopenharmony_ci{
12962306a36Sopenharmony_ci	struct page *page;
13062306a36Sopenharmony_ci
13162306a36Sopenharmony_ci	page = alloc_pages_node(nid, gfp | __GFP_ZERO, 0);
13262306a36Sopenharmony_ci	return page ? page_address(page) : NULL;
13362306a36Sopenharmony_ci}
13462306a36Sopenharmony_ci
13562306a36Sopenharmony_cibool translation_pre_enabled(struct amd_iommu *iommu);
13662306a36Sopenharmony_cibool amd_iommu_is_attach_deferred(struct device *dev);
13762306a36Sopenharmony_ciint __init add_special_device(u8 type, u8 id, u32 *devid, bool cmd_line);
13862306a36Sopenharmony_ci
13962306a36Sopenharmony_ci#ifdef CONFIG_DMI
14062306a36Sopenharmony_civoid amd_iommu_apply_ivrs_quirks(void);
14162306a36Sopenharmony_ci#else
14262306a36Sopenharmony_cistatic inline void amd_iommu_apply_ivrs_quirks(void) { }
14362306a36Sopenharmony_ci#endif
14462306a36Sopenharmony_ci
14562306a36Sopenharmony_civoid amd_iommu_domain_set_pgtable(struct protection_domain *domain,
14662306a36Sopenharmony_ci				  u64 *root, int mode);
14762306a36Sopenharmony_cistruct dev_table_entry *get_dev_table(struct amd_iommu *iommu);
14862306a36Sopenharmony_ci
14962306a36Sopenharmony_ciextern u64 amd_iommu_efr;
15062306a36Sopenharmony_ciextern u64 amd_iommu_efr2;
15162306a36Sopenharmony_ci
15262306a36Sopenharmony_ciextern bool amd_iommu_snp_en;
15362306a36Sopenharmony_ci#endif
154