162306a36Sopenharmony_ci/*
262306a36Sopenharmony_ci * Copyright (C) 2019  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
1262306a36Sopenharmony_ci * in all copies or substantial portions of the Software.
1362306a36Sopenharmony_ci *
1462306a36Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
1562306a36Sopenharmony_ci * OR 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) BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
1862306a36Sopenharmony_ci * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
1962306a36Sopenharmony_ci * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2062306a36Sopenharmony_ci */
2162306a36Sopenharmony_ci#ifndef __AMDGPU_UMC_H__
2262306a36Sopenharmony_ci#define __AMDGPU_UMC_H__
2362306a36Sopenharmony_ci#include "amdgpu_ras.h"
2462306a36Sopenharmony_ci
2562306a36Sopenharmony_ci/*
2662306a36Sopenharmony_ci * (addr / 256) * 4096, the higher 26 bits in ErrorAddr
2762306a36Sopenharmony_ci * is the index of 4KB block
2862306a36Sopenharmony_ci */
2962306a36Sopenharmony_ci#define ADDR_OF_4KB_BLOCK(addr)			(((addr) & ~0xffULL) << 4)
3062306a36Sopenharmony_ci/*
3162306a36Sopenharmony_ci * (addr / 256) * 8192, the higher 26 bits in ErrorAddr
3262306a36Sopenharmony_ci * is the index of 8KB block
3362306a36Sopenharmony_ci */
3462306a36Sopenharmony_ci#define ADDR_OF_8KB_BLOCK(addr)			(((addr) & ~0xffULL) << 5)
3562306a36Sopenharmony_ci/* channel index is the index of 256B block */
3662306a36Sopenharmony_ci#define ADDR_OF_256B_BLOCK(channel_index)	((channel_index) << 8)
3762306a36Sopenharmony_ci/* offset in 256B block */
3862306a36Sopenharmony_ci#define OFFSET_IN_256B_BLOCK(addr)		((addr) & 0xffULL)
3962306a36Sopenharmony_ci
4062306a36Sopenharmony_ci#define LOOP_UMC_INST(umc_inst) for ((umc_inst) = 0; (umc_inst) < adev->umc.umc_inst_num; (umc_inst)++)
4162306a36Sopenharmony_ci#define LOOP_UMC_CH_INST(ch_inst) for ((ch_inst) = 0; (ch_inst) < adev->umc.channel_inst_num; (ch_inst)++)
4262306a36Sopenharmony_ci#define LOOP_UMC_INST_AND_CH(umc_inst, ch_inst) LOOP_UMC_INST((umc_inst)) LOOP_UMC_CH_INST((ch_inst))
4362306a36Sopenharmony_ci
4462306a36Sopenharmony_ci#define LOOP_UMC_NODE_INST(node_inst) \
4562306a36Sopenharmony_ci		for_each_set_bit((node_inst), &(adev->umc.active_mask), adev->umc.node_inst_num)
4662306a36Sopenharmony_ci
4762306a36Sopenharmony_ci#define LOOP_UMC_EACH_NODE_INST_AND_CH(node_inst, umc_inst, ch_inst) \
4862306a36Sopenharmony_ci		LOOP_UMC_NODE_INST((node_inst)) LOOP_UMC_INST_AND_CH((umc_inst), (ch_inst))
4962306a36Sopenharmony_ci
5062306a36Sopenharmony_ci
5162306a36Sopenharmony_citypedef int (*umc_func)(struct amdgpu_device *adev, uint32_t node_inst,
5262306a36Sopenharmony_ci			uint32_t umc_inst, uint32_t ch_inst, void *data);
5362306a36Sopenharmony_ci
5462306a36Sopenharmony_cistruct amdgpu_umc_ras {
5562306a36Sopenharmony_ci	struct amdgpu_ras_block_object ras_block;
5662306a36Sopenharmony_ci	void (*err_cnt_init)(struct amdgpu_device *adev);
5762306a36Sopenharmony_ci	bool (*query_ras_poison_mode)(struct amdgpu_device *adev);
5862306a36Sopenharmony_ci	void (*ecc_info_query_ras_error_count)(struct amdgpu_device *adev,
5962306a36Sopenharmony_ci				      void *ras_error_status);
6062306a36Sopenharmony_ci	void (*ecc_info_query_ras_error_address)(struct amdgpu_device *adev,
6162306a36Sopenharmony_ci					void *ras_error_status);
6262306a36Sopenharmony_ci	/* support different eeprom table version for different asic */
6362306a36Sopenharmony_ci	void (*set_eeprom_table_version)(struct amdgpu_ras_eeprom_table_header *hdr);
6462306a36Sopenharmony_ci};
6562306a36Sopenharmony_ci
6662306a36Sopenharmony_cistruct amdgpu_umc_funcs {
6762306a36Sopenharmony_ci	void (*init_registers)(struct amdgpu_device *adev);
6862306a36Sopenharmony_ci};
6962306a36Sopenharmony_ci
7062306a36Sopenharmony_cistruct amdgpu_umc {
7162306a36Sopenharmony_ci	/* max error count in one ras query call */
7262306a36Sopenharmony_ci	uint32_t max_ras_err_cnt_per_query;
7362306a36Sopenharmony_ci	/* number of umc channel instance with memory map register access */
7462306a36Sopenharmony_ci	uint32_t channel_inst_num;
7562306a36Sopenharmony_ci	/* number of umc instance with memory map register access */
7662306a36Sopenharmony_ci	uint32_t umc_inst_num;
7762306a36Sopenharmony_ci
7862306a36Sopenharmony_ci	/* Total number of umc node instance including harvest one */
7962306a36Sopenharmony_ci	uint32_t node_inst_num;
8062306a36Sopenharmony_ci
8162306a36Sopenharmony_ci	/* UMC regiser per channel offset */
8262306a36Sopenharmony_ci	uint32_t channel_offs;
8362306a36Sopenharmony_ci	/* how many pages are retired in one UE */
8462306a36Sopenharmony_ci	uint32_t retire_unit;
8562306a36Sopenharmony_ci	/* channel index table of interleaved memory */
8662306a36Sopenharmony_ci	const uint32_t *channel_idx_tbl;
8762306a36Sopenharmony_ci	struct ras_common_if *ras_if;
8862306a36Sopenharmony_ci
8962306a36Sopenharmony_ci	const struct amdgpu_umc_funcs *funcs;
9062306a36Sopenharmony_ci	struct amdgpu_umc_ras *ras;
9162306a36Sopenharmony_ci
9262306a36Sopenharmony_ci	/* active mask for umc node instance */
9362306a36Sopenharmony_ci	unsigned long active_mask;
9462306a36Sopenharmony_ci};
9562306a36Sopenharmony_ci
9662306a36Sopenharmony_ciint amdgpu_umc_ras_sw_init(struct amdgpu_device *adev);
9762306a36Sopenharmony_ciint amdgpu_umc_ras_late_init(struct amdgpu_device *adev, struct ras_common_if *ras_block);
9862306a36Sopenharmony_ciint amdgpu_umc_poison_handler(struct amdgpu_device *adev, bool reset);
9962306a36Sopenharmony_ciint amdgpu_umc_process_ecc_irq(struct amdgpu_device *adev,
10062306a36Sopenharmony_ci		struct amdgpu_irq_src *source,
10162306a36Sopenharmony_ci		struct amdgpu_iv_entry *entry);
10262306a36Sopenharmony_civoid amdgpu_umc_fill_error_record(struct ras_err_data *err_data,
10362306a36Sopenharmony_ci		uint64_t err_addr,
10462306a36Sopenharmony_ci		uint64_t retired_page,
10562306a36Sopenharmony_ci		uint32_t channel_index,
10662306a36Sopenharmony_ci		uint32_t umc_inst);
10762306a36Sopenharmony_ci
10862306a36Sopenharmony_ciint amdgpu_umc_process_ras_data_cb(struct amdgpu_device *adev,
10962306a36Sopenharmony_ci		void *ras_error_status,
11062306a36Sopenharmony_ci		struct amdgpu_iv_entry *entry);
11162306a36Sopenharmony_ciint amdgpu_umc_page_retirement_mca(struct amdgpu_device *adev,
11262306a36Sopenharmony_ci			uint64_t err_addr, uint32_t ch_inst, uint32_t umc_inst);
11362306a36Sopenharmony_ci
11462306a36Sopenharmony_ciint amdgpu_umc_loop_channels(struct amdgpu_device *adev,
11562306a36Sopenharmony_ci			umc_func func, void *data);
11662306a36Sopenharmony_ci#endif
117