18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
28c2ecf20Sopenharmony_ci#ifndef _ASM_S390_PCI_DMA_H
38c2ecf20Sopenharmony_ci#define _ASM_S390_PCI_DMA_H
48c2ecf20Sopenharmony_ci
58c2ecf20Sopenharmony_ci/* I/O Translation Anchor (IOTA) */
68c2ecf20Sopenharmony_cienum zpci_ioat_dtype {
78c2ecf20Sopenharmony_ci	ZPCI_IOTA_STO = 0,
88c2ecf20Sopenharmony_ci	ZPCI_IOTA_RTTO = 1,
98c2ecf20Sopenharmony_ci	ZPCI_IOTA_RSTO = 2,
108c2ecf20Sopenharmony_ci	ZPCI_IOTA_RFTO = 3,
118c2ecf20Sopenharmony_ci	ZPCI_IOTA_PFAA = 4,
128c2ecf20Sopenharmony_ci	ZPCI_IOTA_IOPFAA = 5,
138c2ecf20Sopenharmony_ci	ZPCI_IOTA_IOPTO = 7
148c2ecf20Sopenharmony_ci};
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci#define ZPCI_IOTA_IOT_ENABLED		0x800UL
178c2ecf20Sopenharmony_ci#define ZPCI_IOTA_DT_ST			(ZPCI_IOTA_STO	<< 2)
188c2ecf20Sopenharmony_ci#define ZPCI_IOTA_DT_RT			(ZPCI_IOTA_RTTO << 2)
198c2ecf20Sopenharmony_ci#define ZPCI_IOTA_DT_RS			(ZPCI_IOTA_RSTO << 2)
208c2ecf20Sopenharmony_ci#define ZPCI_IOTA_DT_RF			(ZPCI_IOTA_RFTO << 2)
218c2ecf20Sopenharmony_ci#define ZPCI_IOTA_DT_PF			(ZPCI_IOTA_PFAA << 2)
228c2ecf20Sopenharmony_ci#define ZPCI_IOTA_FS_4K			0
238c2ecf20Sopenharmony_ci#define ZPCI_IOTA_FS_1M			1
248c2ecf20Sopenharmony_ci#define ZPCI_IOTA_FS_2G			2
258c2ecf20Sopenharmony_ci#define ZPCI_KEY			(PAGE_DEFAULT_KEY << 5)
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ci#define ZPCI_TABLE_SIZE_RT	(1UL << 42)
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ci#define ZPCI_IOTA_STO_FLAG	(ZPCI_IOTA_IOT_ENABLED | ZPCI_KEY | ZPCI_IOTA_DT_ST)
308c2ecf20Sopenharmony_ci#define ZPCI_IOTA_RTTO_FLAG	(ZPCI_IOTA_IOT_ENABLED | ZPCI_KEY | ZPCI_IOTA_DT_RT)
318c2ecf20Sopenharmony_ci#define ZPCI_IOTA_RSTO_FLAG	(ZPCI_IOTA_IOT_ENABLED | ZPCI_KEY | ZPCI_IOTA_DT_RS)
328c2ecf20Sopenharmony_ci#define ZPCI_IOTA_RFTO_FLAG	(ZPCI_IOTA_IOT_ENABLED | ZPCI_KEY | ZPCI_IOTA_DT_RF)
338c2ecf20Sopenharmony_ci#define ZPCI_IOTA_RFAA_FLAG	(ZPCI_IOTA_IOT_ENABLED | ZPCI_KEY | ZPCI_IOTA_DT_PF | ZPCI_IOTA_FS_2G)
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci/* I/O Region and segment tables */
368c2ecf20Sopenharmony_ci#define ZPCI_INDEX_MASK			0x7ffUL
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ci#define ZPCI_TABLE_TYPE_MASK		0xc
398c2ecf20Sopenharmony_ci#define ZPCI_TABLE_TYPE_RFX		0xc
408c2ecf20Sopenharmony_ci#define ZPCI_TABLE_TYPE_RSX		0x8
418c2ecf20Sopenharmony_ci#define ZPCI_TABLE_TYPE_RTX		0x4
428c2ecf20Sopenharmony_ci#define ZPCI_TABLE_TYPE_SX		0x0
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci#define ZPCI_TABLE_LEN_RFX		0x3
458c2ecf20Sopenharmony_ci#define ZPCI_TABLE_LEN_RSX		0x3
468c2ecf20Sopenharmony_ci#define ZPCI_TABLE_LEN_RTX		0x3
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci#define ZPCI_TABLE_OFFSET_MASK		0xc0
498c2ecf20Sopenharmony_ci#define ZPCI_TABLE_SIZE			0x4000
508c2ecf20Sopenharmony_ci#define ZPCI_TABLE_ALIGN		ZPCI_TABLE_SIZE
518c2ecf20Sopenharmony_ci#define ZPCI_TABLE_ENTRY_SIZE		(sizeof(unsigned long))
528c2ecf20Sopenharmony_ci#define ZPCI_TABLE_ENTRIES		(ZPCI_TABLE_SIZE / ZPCI_TABLE_ENTRY_SIZE)
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci#define ZPCI_TABLE_BITS			11
558c2ecf20Sopenharmony_ci#define ZPCI_PT_BITS			8
568c2ecf20Sopenharmony_ci#define ZPCI_ST_SHIFT			(ZPCI_PT_BITS + PAGE_SHIFT)
578c2ecf20Sopenharmony_ci#define ZPCI_RT_SHIFT			(ZPCI_ST_SHIFT + ZPCI_TABLE_BITS)
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ci#define ZPCI_RTE_FLAG_MASK		0x3fffUL
608c2ecf20Sopenharmony_ci#define ZPCI_RTE_ADDR_MASK		(~ZPCI_RTE_FLAG_MASK)
618c2ecf20Sopenharmony_ci#define ZPCI_STE_FLAG_MASK		0x7ffUL
628c2ecf20Sopenharmony_ci#define ZPCI_STE_ADDR_MASK		(~ZPCI_STE_FLAG_MASK)
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci/* I/O Page tables */
658c2ecf20Sopenharmony_ci#define ZPCI_PTE_VALID_MASK		0x400
668c2ecf20Sopenharmony_ci#define ZPCI_PTE_INVALID		0x400
678c2ecf20Sopenharmony_ci#define ZPCI_PTE_VALID			0x000
688c2ecf20Sopenharmony_ci#define ZPCI_PT_SIZE			0x800
698c2ecf20Sopenharmony_ci#define ZPCI_PT_ALIGN			ZPCI_PT_SIZE
708c2ecf20Sopenharmony_ci#define ZPCI_PT_ENTRIES			(ZPCI_PT_SIZE / ZPCI_TABLE_ENTRY_SIZE)
718c2ecf20Sopenharmony_ci#define ZPCI_PT_MASK			(ZPCI_PT_ENTRIES - 1)
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ci#define ZPCI_PTE_FLAG_MASK		0xfffUL
748c2ecf20Sopenharmony_ci#define ZPCI_PTE_ADDR_MASK		(~ZPCI_PTE_FLAG_MASK)
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ci/* Shared bits */
778c2ecf20Sopenharmony_ci#define ZPCI_TABLE_VALID		0x00
788c2ecf20Sopenharmony_ci#define ZPCI_TABLE_INVALID		0x20
798c2ecf20Sopenharmony_ci#define ZPCI_TABLE_PROTECTED		0x200
808c2ecf20Sopenharmony_ci#define ZPCI_TABLE_UNPROTECTED		0x000
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_ci#define ZPCI_TABLE_VALID_MASK		0x20
838c2ecf20Sopenharmony_ci#define ZPCI_TABLE_PROT_MASK		0x200
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_cistatic inline unsigned int calc_rtx(dma_addr_t ptr)
868c2ecf20Sopenharmony_ci{
878c2ecf20Sopenharmony_ci	return ((unsigned long) ptr >> ZPCI_RT_SHIFT) & ZPCI_INDEX_MASK;
888c2ecf20Sopenharmony_ci}
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_cistatic inline unsigned int calc_sx(dma_addr_t ptr)
918c2ecf20Sopenharmony_ci{
928c2ecf20Sopenharmony_ci	return ((unsigned long) ptr >> ZPCI_ST_SHIFT) & ZPCI_INDEX_MASK;
938c2ecf20Sopenharmony_ci}
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_cistatic inline unsigned int calc_px(dma_addr_t ptr)
968c2ecf20Sopenharmony_ci{
978c2ecf20Sopenharmony_ci	return ((unsigned long) ptr >> PAGE_SHIFT) & ZPCI_PT_MASK;
988c2ecf20Sopenharmony_ci}
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_cistatic inline void set_pt_pfaa(unsigned long *entry, void *pfaa)
1018c2ecf20Sopenharmony_ci{
1028c2ecf20Sopenharmony_ci	*entry &= ZPCI_PTE_FLAG_MASK;
1038c2ecf20Sopenharmony_ci	*entry |= ((unsigned long) pfaa & ZPCI_PTE_ADDR_MASK);
1048c2ecf20Sopenharmony_ci}
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_cistatic inline void set_rt_sto(unsigned long *entry, void *sto)
1078c2ecf20Sopenharmony_ci{
1088c2ecf20Sopenharmony_ci	*entry &= ZPCI_RTE_FLAG_MASK;
1098c2ecf20Sopenharmony_ci	*entry |= ((unsigned long) sto & ZPCI_RTE_ADDR_MASK);
1108c2ecf20Sopenharmony_ci	*entry |= ZPCI_TABLE_TYPE_RTX;
1118c2ecf20Sopenharmony_ci}
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_cistatic inline void set_st_pto(unsigned long *entry, void *pto)
1148c2ecf20Sopenharmony_ci{
1158c2ecf20Sopenharmony_ci	*entry &= ZPCI_STE_FLAG_MASK;
1168c2ecf20Sopenharmony_ci	*entry |= ((unsigned long) pto & ZPCI_STE_ADDR_MASK);
1178c2ecf20Sopenharmony_ci	*entry |= ZPCI_TABLE_TYPE_SX;
1188c2ecf20Sopenharmony_ci}
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_cistatic inline void validate_rt_entry(unsigned long *entry)
1218c2ecf20Sopenharmony_ci{
1228c2ecf20Sopenharmony_ci	*entry &= ~ZPCI_TABLE_VALID_MASK;
1238c2ecf20Sopenharmony_ci	*entry &= ~ZPCI_TABLE_OFFSET_MASK;
1248c2ecf20Sopenharmony_ci	*entry |= ZPCI_TABLE_VALID;
1258c2ecf20Sopenharmony_ci	*entry |= ZPCI_TABLE_LEN_RTX;
1268c2ecf20Sopenharmony_ci}
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_cistatic inline void validate_st_entry(unsigned long *entry)
1298c2ecf20Sopenharmony_ci{
1308c2ecf20Sopenharmony_ci	*entry &= ~ZPCI_TABLE_VALID_MASK;
1318c2ecf20Sopenharmony_ci	*entry |= ZPCI_TABLE_VALID;
1328c2ecf20Sopenharmony_ci}
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_cistatic inline void invalidate_pt_entry(unsigned long *entry)
1358c2ecf20Sopenharmony_ci{
1368c2ecf20Sopenharmony_ci	WARN_ON_ONCE((*entry & ZPCI_PTE_VALID_MASK) == ZPCI_PTE_INVALID);
1378c2ecf20Sopenharmony_ci	*entry &= ~ZPCI_PTE_VALID_MASK;
1388c2ecf20Sopenharmony_ci	*entry |= ZPCI_PTE_INVALID;
1398c2ecf20Sopenharmony_ci}
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_cistatic inline void validate_pt_entry(unsigned long *entry)
1428c2ecf20Sopenharmony_ci{
1438c2ecf20Sopenharmony_ci	WARN_ON_ONCE((*entry & ZPCI_PTE_VALID_MASK) == ZPCI_PTE_VALID);
1448c2ecf20Sopenharmony_ci	*entry &= ~ZPCI_PTE_VALID_MASK;
1458c2ecf20Sopenharmony_ci	*entry |= ZPCI_PTE_VALID;
1468c2ecf20Sopenharmony_ci}
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_cistatic inline void entry_set_protected(unsigned long *entry)
1498c2ecf20Sopenharmony_ci{
1508c2ecf20Sopenharmony_ci	*entry &= ~ZPCI_TABLE_PROT_MASK;
1518c2ecf20Sopenharmony_ci	*entry |= ZPCI_TABLE_PROTECTED;
1528c2ecf20Sopenharmony_ci}
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_cistatic inline void entry_clr_protected(unsigned long *entry)
1558c2ecf20Sopenharmony_ci{
1568c2ecf20Sopenharmony_ci	*entry &= ~ZPCI_TABLE_PROT_MASK;
1578c2ecf20Sopenharmony_ci	*entry |= ZPCI_TABLE_UNPROTECTED;
1588c2ecf20Sopenharmony_ci}
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_cistatic inline int reg_entry_isvalid(unsigned long entry)
1618c2ecf20Sopenharmony_ci{
1628c2ecf20Sopenharmony_ci	return (entry & ZPCI_TABLE_VALID_MASK) == ZPCI_TABLE_VALID;
1638c2ecf20Sopenharmony_ci}
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_cistatic inline int pt_entry_isvalid(unsigned long entry)
1668c2ecf20Sopenharmony_ci{
1678c2ecf20Sopenharmony_ci	return (entry & ZPCI_PTE_VALID_MASK) == ZPCI_PTE_VALID;
1688c2ecf20Sopenharmony_ci}
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_cistatic inline unsigned long *get_rt_sto(unsigned long entry)
1718c2ecf20Sopenharmony_ci{
1728c2ecf20Sopenharmony_ci	return ((entry & ZPCI_TABLE_TYPE_MASK) == ZPCI_TABLE_TYPE_RTX)
1738c2ecf20Sopenharmony_ci		? (unsigned long *) (entry & ZPCI_RTE_ADDR_MASK)
1748c2ecf20Sopenharmony_ci		: NULL;
1758c2ecf20Sopenharmony_ci}
1768c2ecf20Sopenharmony_ci
1778c2ecf20Sopenharmony_cistatic inline unsigned long *get_st_pto(unsigned long entry)
1788c2ecf20Sopenharmony_ci{
1798c2ecf20Sopenharmony_ci	return ((entry & ZPCI_TABLE_TYPE_MASK) == ZPCI_TABLE_TYPE_SX)
1808c2ecf20Sopenharmony_ci		? (unsigned long *) (entry & ZPCI_STE_ADDR_MASK)
1818c2ecf20Sopenharmony_ci		: NULL;
1828c2ecf20Sopenharmony_ci}
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_ci/* Prototypes */
1858c2ecf20Sopenharmony_ciint zpci_dma_init_device(struct zpci_dev *);
1868c2ecf20Sopenharmony_civoid zpci_dma_exit_device(struct zpci_dev *);
1878c2ecf20Sopenharmony_civoid dma_free_seg_table(unsigned long);
1888c2ecf20Sopenharmony_ciunsigned long *dma_alloc_cpu_table(void);
1898c2ecf20Sopenharmony_civoid dma_cleanup_tables(unsigned long *);
1908c2ecf20Sopenharmony_ciunsigned long *dma_walk_cpu_trans(unsigned long *rto, dma_addr_t dma_addr);
1918c2ecf20Sopenharmony_civoid dma_update_cpu_trans(unsigned long *entry, void *page_addr, int flags);
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_ciextern const struct dma_map_ops s390_pci_dma_ops;
1948c2ecf20Sopenharmony_ci
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_ci#endif
197