162306a36Sopenharmony_ci/*
262306a36Sopenharmony_ci * Copyright 2015 Advanced Micro Devices, Inc.
362306a36Sopenharmony_ci *
462306a36Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
562306a36Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
662306a36Sopenharmony_ci * to deal in the Software without restriction, including without limitation
762306a36Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
862306a36Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
962306a36Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
1062306a36Sopenharmony_ci *
1162306a36Sopenharmony_ci * The above copyright notice and this permission notice shall be included in
1262306a36Sopenharmony_ci * all copies or substantial portions of the Software.
1362306a36Sopenharmony_ci *
1462306a36Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1562306a36Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1662306a36Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
1762306a36Sopenharmony_ci * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
1862306a36Sopenharmony_ci * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
1962306a36Sopenharmony_ci * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
2062306a36Sopenharmony_ci * OTHER DEALINGS IN THE SOFTWARE.
2162306a36Sopenharmony_ci *
2262306a36Sopenharmony_ci *
2362306a36Sopenharmony_ci */
2462306a36Sopenharmony_ci#ifndef _CGS_COMMON_H
2562306a36Sopenharmony_ci#define _CGS_COMMON_H
2662306a36Sopenharmony_ci
2762306a36Sopenharmony_ci#include "amd_shared.h"
2862306a36Sopenharmony_ci
2962306a36Sopenharmony_cistruct cgs_device;
3062306a36Sopenharmony_ci
3162306a36Sopenharmony_ci/**
3262306a36Sopenharmony_ci * enum cgs_ind_reg - Indirect register spaces
3362306a36Sopenharmony_ci */
3462306a36Sopenharmony_cienum cgs_ind_reg {
3562306a36Sopenharmony_ci	CGS_IND_REG__PCIE,
3662306a36Sopenharmony_ci	CGS_IND_REG__SMC,
3762306a36Sopenharmony_ci	CGS_IND_REG__UVD_CTX,
3862306a36Sopenharmony_ci	CGS_IND_REG__DIDT,
3962306a36Sopenharmony_ci	CGS_IND_REG_GC_CAC,
4062306a36Sopenharmony_ci	CGS_IND_REG_SE_CAC,
4162306a36Sopenharmony_ci	CGS_IND_REG__AUDIO_ENDPT
4262306a36Sopenharmony_ci};
4362306a36Sopenharmony_ci
4462306a36Sopenharmony_ci/*
4562306a36Sopenharmony_ci * enum cgs_ucode_id - Firmware types for different IPs
4662306a36Sopenharmony_ci */
4762306a36Sopenharmony_cienum cgs_ucode_id {
4862306a36Sopenharmony_ci	CGS_UCODE_ID_SMU = 0,
4962306a36Sopenharmony_ci	CGS_UCODE_ID_SMU_SK,
5062306a36Sopenharmony_ci	CGS_UCODE_ID_SDMA0,
5162306a36Sopenharmony_ci	CGS_UCODE_ID_SDMA1,
5262306a36Sopenharmony_ci	CGS_UCODE_ID_CP_CE,
5362306a36Sopenharmony_ci	CGS_UCODE_ID_CP_PFP,
5462306a36Sopenharmony_ci	CGS_UCODE_ID_CP_ME,
5562306a36Sopenharmony_ci	CGS_UCODE_ID_CP_MEC,
5662306a36Sopenharmony_ci	CGS_UCODE_ID_CP_MEC_JT1,
5762306a36Sopenharmony_ci	CGS_UCODE_ID_CP_MEC_JT2,
5862306a36Sopenharmony_ci	CGS_UCODE_ID_GMCON_RENG,
5962306a36Sopenharmony_ci	CGS_UCODE_ID_RLC_G,
6062306a36Sopenharmony_ci	CGS_UCODE_ID_STORAGE,
6162306a36Sopenharmony_ci	CGS_UCODE_ID_MAXIMUM,
6262306a36Sopenharmony_ci};
6362306a36Sopenharmony_ci
6462306a36Sopenharmony_ci/**
6562306a36Sopenharmony_ci * struct cgs_firmware_info - Firmware information
6662306a36Sopenharmony_ci */
6762306a36Sopenharmony_cistruct cgs_firmware_info {
6862306a36Sopenharmony_ci	uint16_t		version;
6962306a36Sopenharmony_ci	uint16_t		fw_version;
7062306a36Sopenharmony_ci	uint16_t		feature_version;
7162306a36Sopenharmony_ci	uint32_t		image_size;
7262306a36Sopenharmony_ci	uint64_t		mc_addr;
7362306a36Sopenharmony_ci
7462306a36Sopenharmony_ci	/* only for smc firmware */
7562306a36Sopenharmony_ci	uint32_t		ucode_start_address;
7662306a36Sopenharmony_ci
7762306a36Sopenharmony_ci	void			*kptr;
7862306a36Sopenharmony_ci	bool			is_kicker;
7962306a36Sopenharmony_ci};
8062306a36Sopenharmony_ci
8162306a36Sopenharmony_citypedef unsigned long cgs_handle_t;
8262306a36Sopenharmony_ci
8362306a36Sopenharmony_ci/**
8462306a36Sopenharmony_ci * cgs_read_register() - Read an MMIO register
8562306a36Sopenharmony_ci * @cgs_device:	opaque device handle
8662306a36Sopenharmony_ci * @offset:	register offset
8762306a36Sopenharmony_ci *
8862306a36Sopenharmony_ci * Return:  register value
8962306a36Sopenharmony_ci */
9062306a36Sopenharmony_citypedef uint32_t (*cgs_read_register_t)(struct cgs_device *cgs_device, unsigned offset);
9162306a36Sopenharmony_ci
9262306a36Sopenharmony_ci/**
9362306a36Sopenharmony_ci * cgs_write_register() - Write an MMIO register
9462306a36Sopenharmony_ci * @cgs_device:	opaque device handle
9562306a36Sopenharmony_ci * @offset:	register offset
9662306a36Sopenharmony_ci * @value:	register value
9762306a36Sopenharmony_ci */
9862306a36Sopenharmony_citypedef void (*cgs_write_register_t)(struct cgs_device *cgs_device, unsigned offset,
9962306a36Sopenharmony_ci				     uint32_t value);
10062306a36Sopenharmony_ci
10162306a36Sopenharmony_ci/**
10262306a36Sopenharmony_ci * cgs_read_ind_register() - Read an indirect register
10362306a36Sopenharmony_ci * @cgs_device:	opaque device handle
10462306a36Sopenharmony_ci * @offset:	register offset
10562306a36Sopenharmony_ci *
10662306a36Sopenharmony_ci * Return:  register value
10762306a36Sopenharmony_ci */
10862306a36Sopenharmony_citypedef uint32_t (*cgs_read_ind_register_t)(struct cgs_device *cgs_device, enum cgs_ind_reg space,
10962306a36Sopenharmony_ci					    unsigned index);
11062306a36Sopenharmony_ci
11162306a36Sopenharmony_ci/**
11262306a36Sopenharmony_ci * cgs_write_ind_register() - Write an indirect register
11362306a36Sopenharmony_ci * @cgs_device:	opaque device handle
11462306a36Sopenharmony_ci * @offset:	register offset
11562306a36Sopenharmony_ci * @value:	register value
11662306a36Sopenharmony_ci */
11762306a36Sopenharmony_citypedef void (*cgs_write_ind_register_t)(struct cgs_device *cgs_device, enum cgs_ind_reg space,
11862306a36Sopenharmony_ci					 unsigned index, uint32_t value);
11962306a36Sopenharmony_ci
12062306a36Sopenharmony_ci#define CGS_REG_FIELD_SHIFT(reg, field) reg##__##field##__SHIFT
12162306a36Sopenharmony_ci#define CGS_REG_FIELD_MASK(reg, field) reg##__##field##_MASK
12262306a36Sopenharmony_ci
12362306a36Sopenharmony_ci#define CGS_REG_SET_FIELD(orig_val, reg, field, field_val)			\
12462306a36Sopenharmony_ci	(((orig_val) & ~CGS_REG_FIELD_MASK(reg, field)) |			\
12562306a36Sopenharmony_ci	 (CGS_REG_FIELD_MASK(reg, field) & ((field_val) << CGS_REG_FIELD_SHIFT(reg, field))))
12662306a36Sopenharmony_ci
12762306a36Sopenharmony_ci#define CGS_REG_GET_FIELD(value, reg, field)				\
12862306a36Sopenharmony_ci	(((value) & CGS_REG_FIELD_MASK(reg, field)) >> CGS_REG_FIELD_SHIFT(reg, field))
12962306a36Sopenharmony_ci
13062306a36Sopenharmony_ci#define CGS_WREG32_FIELD(device, reg, field, val)	\
13162306a36Sopenharmony_ci	cgs_write_register(device, mm##reg, (cgs_read_register(device, mm##reg) & ~CGS_REG_FIELD_MASK(reg, field)) | (val) << CGS_REG_FIELD_SHIFT(reg, field))
13262306a36Sopenharmony_ci
13362306a36Sopenharmony_ci#define CGS_WREG32_FIELD_IND(device, space, reg, field, val)	\
13462306a36Sopenharmony_ci	cgs_write_ind_register(device, space, ix##reg, (cgs_read_ind_register(device, space, ix##reg) & ~CGS_REG_FIELD_MASK(reg, field)) | (val) << CGS_REG_FIELD_SHIFT(reg, field))
13562306a36Sopenharmony_ci
13662306a36Sopenharmony_citypedef int (*cgs_get_firmware_info)(struct cgs_device *cgs_device,
13762306a36Sopenharmony_ci				     enum cgs_ucode_id type,
13862306a36Sopenharmony_ci				     struct cgs_firmware_info *info);
13962306a36Sopenharmony_ci
14062306a36Sopenharmony_cistruct cgs_ops {
14162306a36Sopenharmony_ci	/* MMIO access */
14262306a36Sopenharmony_ci	cgs_read_register_t read_register;
14362306a36Sopenharmony_ci	cgs_write_register_t write_register;
14462306a36Sopenharmony_ci	cgs_read_ind_register_t read_ind_register;
14562306a36Sopenharmony_ci	cgs_write_ind_register_t write_ind_register;
14662306a36Sopenharmony_ci	/* Firmware Info */
14762306a36Sopenharmony_ci	cgs_get_firmware_info get_firmware_info;
14862306a36Sopenharmony_ci};
14962306a36Sopenharmony_ci
15062306a36Sopenharmony_cistruct cgs_os_ops; /* To be define in OS-specific CGS header */
15162306a36Sopenharmony_ci
15262306a36Sopenharmony_cistruct cgs_device
15362306a36Sopenharmony_ci{
15462306a36Sopenharmony_ci	const struct cgs_ops *ops;
15562306a36Sopenharmony_ci	/* to be embedded at the start of driver private structure */
15662306a36Sopenharmony_ci};
15762306a36Sopenharmony_ci
15862306a36Sopenharmony_ci/* Convenience macros that make CGS indirect function calls look like
15962306a36Sopenharmony_ci * normal function calls */
16062306a36Sopenharmony_ci#define CGS_CALL(func,dev,...) \
16162306a36Sopenharmony_ci	(((struct cgs_device *)dev)->ops->func(dev, ##__VA_ARGS__))
16262306a36Sopenharmony_ci#define CGS_OS_CALL(func,dev,...) \
16362306a36Sopenharmony_ci	(((struct cgs_device *)dev)->os_ops->func(dev, ##__VA_ARGS__))
16462306a36Sopenharmony_ci
16562306a36Sopenharmony_ci#define cgs_read_register(dev,offset)		\
16662306a36Sopenharmony_ci	CGS_CALL(read_register,dev,offset)
16762306a36Sopenharmony_ci#define cgs_write_register(dev,offset,value)		\
16862306a36Sopenharmony_ci	CGS_CALL(write_register,dev,offset,value)
16962306a36Sopenharmony_ci#define cgs_read_ind_register(dev,space,index)		\
17062306a36Sopenharmony_ci	CGS_CALL(read_ind_register,dev,space,index)
17162306a36Sopenharmony_ci#define cgs_write_ind_register(dev,space,index,value)		\
17262306a36Sopenharmony_ci	CGS_CALL(write_ind_register,dev,space,index,value)
17362306a36Sopenharmony_ci
17462306a36Sopenharmony_ci#define cgs_get_firmware_info(dev, type, info)	\
17562306a36Sopenharmony_ci	CGS_CALL(get_firmware_info, dev, type, info)
17662306a36Sopenharmony_ci
17762306a36Sopenharmony_ci#endif /* _CGS_COMMON_H */
178