162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Copyright (C) 2022-2023, Advanced Micro Devices, Inc.
462306a36Sopenharmony_ci */
562306a36Sopenharmony_ci
662306a36Sopenharmony_ci#include <linux/module.h>
762306a36Sopenharmony_ci
862306a36Sopenharmony_ci#include "mcdi.h"
962306a36Sopenharmony_ci#include "mcdi_functions.h"
1062306a36Sopenharmony_ci
1162306a36Sopenharmony_ciint cdx_mcdi_get_num_buses(struct cdx_mcdi *cdx)
1262306a36Sopenharmony_ci{
1362306a36Sopenharmony_ci	MCDI_DECLARE_BUF(outbuf, MC_CMD_CDX_BUS_ENUM_BUSES_OUT_LEN);
1462306a36Sopenharmony_ci	size_t outlen;
1562306a36Sopenharmony_ci	int ret;
1662306a36Sopenharmony_ci
1762306a36Sopenharmony_ci	ret = cdx_mcdi_rpc(cdx, MC_CMD_CDX_BUS_ENUM_BUSES, NULL, 0,
1862306a36Sopenharmony_ci			   outbuf, sizeof(outbuf), &outlen);
1962306a36Sopenharmony_ci	if (ret)
2062306a36Sopenharmony_ci		return ret;
2162306a36Sopenharmony_ci
2262306a36Sopenharmony_ci	if (outlen != MC_CMD_CDX_BUS_ENUM_BUSES_OUT_LEN)
2362306a36Sopenharmony_ci		return -EIO;
2462306a36Sopenharmony_ci
2562306a36Sopenharmony_ci	return MCDI_DWORD(outbuf, CDX_BUS_ENUM_BUSES_OUT_BUS_COUNT);
2662306a36Sopenharmony_ci}
2762306a36Sopenharmony_ci
2862306a36Sopenharmony_ciint cdx_mcdi_get_num_devs(struct cdx_mcdi *cdx, int bus_num)
2962306a36Sopenharmony_ci{
3062306a36Sopenharmony_ci	MCDI_DECLARE_BUF(outbuf, MC_CMD_CDX_BUS_ENUM_DEVICES_OUT_LEN);
3162306a36Sopenharmony_ci	MCDI_DECLARE_BUF(inbuf, MC_CMD_CDX_BUS_ENUM_DEVICES_IN_LEN);
3262306a36Sopenharmony_ci	size_t outlen;
3362306a36Sopenharmony_ci	int ret;
3462306a36Sopenharmony_ci
3562306a36Sopenharmony_ci	MCDI_SET_DWORD(inbuf, CDX_BUS_ENUM_DEVICES_IN_BUS, bus_num);
3662306a36Sopenharmony_ci
3762306a36Sopenharmony_ci	ret = cdx_mcdi_rpc(cdx, MC_CMD_CDX_BUS_ENUM_DEVICES, inbuf, sizeof(inbuf),
3862306a36Sopenharmony_ci			   outbuf, sizeof(outbuf), &outlen);
3962306a36Sopenharmony_ci	if (ret)
4062306a36Sopenharmony_ci		return ret;
4162306a36Sopenharmony_ci
4262306a36Sopenharmony_ci	if (outlen != MC_CMD_CDX_BUS_ENUM_DEVICES_OUT_LEN)
4362306a36Sopenharmony_ci		return -EIO;
4462306a36Sopenharmony_ci
4562306a36Sopenharmony_ci	return MCDI_DWORD(outbuf, CDX_BUS_ENUM_DEVICES_OUT_DEVICE_COUNT);
4662306a36Sopenharmony_ci}
4762306a36Sopenharmony_ci
4862306a36Sopenharmony_ciint cdx_mcdi_get_dev_config(struct cdx_mcdi *cdx,
4962306a36Sopenharmony_ci			    u8 bus_num, u8 dev_num,
5062306a36Sopenharmony_ci			    struct cdx_dev_params *dev_params)
5162306a36Sopenharmony_ci{
5262306a36Sopenharmony_ci	MCDI_DECLARE_BUF(outbuf, MC_CMD_CDX_BUS_GET_DEVICE_CONFIG_OUT_LEN);
5362306a36Sopenharmony_ci	MCDI_DECLARE_BUF(inbuf, MC_CMD_CDX_BUS_GET_DEVICE_CONFIG_IN_LEN);
5462306a36Sopenharmony_ci	struct resource *res = &dev_params->res[0];
5562306a36Sopenharmony_ci	size_t outlen;
5662306a36Sopenharmony_ci	u32 req_id;
5762306a36Sopenharmony_ci	int ret;
5862306a36Sopenharmony_ci
5962306a36Sopenharmony_ci	MCDI_SET_DWORD(inbuf, CDX_BUS_GET_DEVICE_CONFIG_IN_BUS, bus_num);
6062306a36Sopenharmony_ci	MCDI_SET_DWORD(inbuf, CDX_BUS_GET_DEVICE_CONFIG_IN_DEVICE, dev_num);
6162306a36Sopenharmony_ci
6262306a36Sopenharmony_ci	ret = cdx_mcdi_rpc(cdx, MC_CMD_CDX_BUS_GET_DEVICE_CONFIG, inbuf, sizeof(inbuf),
6362306a36Sopenharmony_ci			   outbuf, sizeof(outbuf), &outlen);
6462306a36Sopenharmony_ci	if (ret)
6562306a36Sopenharmony_ci		return ret;
6662306a36Sopenharmony_ci
6762306a36Sopenharmony_ci	if (outlen != MC_CMD_CDX_BUS_GET_DEVICE_CONFIG_OUT_LEN)
6862306a36Sopenharmony_ci		return -EIO;
6962306a36Sopenharmony_ci
7062306a36Sopenharmony_ci	dev_params->bus_num = bus_num;
7162306a36Sopenharmony_ci	dev_params->dev_num = dev_num;
7262306a36Sopenharmony_ci
7362306a36Sopenharmony_ci	req_id = MCDI_DWORD(outbuf, CDX_BUS_GET_DEVICE_CONFIG_OUT_REQUESTER_ID);
7462306a36Sopenharmony_ci	dev_params->req_id = req_id;
7562306a36Sopenharmony_ci
7662306a36Sopenharmony_ci	dev_params->res_count = 0;
7762306a36Sopenharmony_ci	if (MCDI_QWORD(outbuf, CDX_BUS_GET_DEVICE_CONFIG_OUT_MMIO_REGION0_SIZE) != 0) {
7862306a36Sopenharmony_ci		res[dev_params->res_count].start =
7962306a36Sopenharmony_ci			MCDI_QWORD(outbuf, CDX_BUS_GET_DEVICE_CONFIG_OUT_MMIO_REGION0_BASE);
8062306a36Sopenharmony_ci		res[dev_params->res_count].end =
8162306a36Sopenharmony_ci			MCDI_QWORD(outbuf, CDX_BUS_GET_DEVICE_CONFIG_OUT_MMIO_REGION0_BASE) +
8262306a36Sopenharmony_ci				   MCDI_QWORD(outbuf,
8362306a36Sopenharmony_ci					      CDX_BUS_GET_DEVICE_CONFIG_OUT_MMIO_REGION0_SIZE) - 1;
8462306a36Sopenharmony_ci		res[dev_params->res_count].flags = IORESOURCE_MEM;
8562306a36Sopenharmony_ci		dev_params->res_count++;
8662306a36Sopenharmony_ci	}
8762306a36Sopenharmony_ci
8862306a36Sopenharmony_ci	if (MCDI_QWORD(outbuf, CDX_BUS_GET_DEVICE_CONFIG_OUT_MMIO_REGION1_SIZE) != 0) {
8962306a36Sopenharmony_ci		res[dev_params->res_count].start =
9062306a36Sopenharmony_ci			MCDI_QWORD(outbuf, CDX_BUS_GET_DEVICE_CONFIG_OUT_MMIO_REGION1_BASE);
9162306a36Sopenharmony_ci		res[dev_params->res_count].end =
9262306a36Sopenharmony_ci			MCDI_QWORD(outbuf, CDX_BUS_GET_DEVICE_CONFIG_OUT_MMIO_REGION1_BASE) +
9362306a36Sopenharmony_ci				   MCDI_QWORD(outbuf,
9462306a36Sopenharmony_ci					      CDX_BUS_GET_DEVICE_CONFIG_OUT_MMIO_REGION1_SIZE) - 1;
9562306a36Sopenharmony_ci		res[dev_params->res_count].flags = IORESOURCE_MEM;
9662306a36Sopenharmony_ci		dev_params->res_count++;
9762306a36Sopenharmony_ci	}
9862306a36Sopenharmony_ci
9962306a36Sopenharmony_ci	if (MCDI_QWORD(outbuf, CDX_BUS_GET_DEVICE_CONFIG_OUT_MMIO_REGION2_SIZE) != 0) {
10062306a36Sopenharmony_ci		res[dev_params->res_count].start =
10162306a36Sopenharmony_ci			MCDI_QWORD(outbuf, CDX_BUS_GET_DEVICE_CONFIG_OUT_MMIO_REGION2_BASE);
10262306a36Sopenharmony_ci		res[dev_params->res_count].end =
10362306a36Sopenharmony_ci			MCDI_QWORD(outbuf, CDX_BUS_GET_DEVICE_CONFIG_OUT_MMIO_REGION2_BASE) +
10462306a36Sopenharmony_ci				   MCDI_QWORD(outbuf,
10562306a36Sopenharmony_ci					      CDX_BUS_GET_DEVICE_CONFIG_OUT_MMIO_REGION2_SIZE) - 1;
10662306a36Sopenharmony_ci		res[dev_params->res_count].flags = IORESOURCE_MEM;
10762306a36Sopenharmony_ci		dev_params->res_count++;
10862306a36Sopenharmony_ci	}
10962306a36Sopenharmony_ci
11062306a36Sopenharmony_ci	if (MCDI_QWORD(outbuf, CDX_BUS_GET_DEVICE_CONFIG_OUT_MMIO_REGION3_SIZE) != 0) {
11162306a36Sopenharmony_ci		res[dev_params->res_count].start =
11262306a36Sopenharmony_ci			MCDI_QWORD(outbuf, CDX_BUS_GET_DEVICE_CONFIG_OUT_MMIO_REGION3_BASE);
11362306a36Sopenharmony_ci		res[dev_params->res_count].end =
11462306a36Sopenharmony_ci			MCDI_QWORD(outbuf, CDX_BUS_GET_DEVICE_CONFIG_OUT_MMIO_REGION3_BASE) +
11562306a36Sopenharmony_ci				   MCDI_QWORD(outbuf,
11662306a36Sopenharmony_ci					      CDX_BUS_GET_DEVICE_CONFIG_OUT_MMIO_REGION3_SIZE) - 1;
11762306a36Sopenharmony_ci		res[dev_params->res_count].flags = IORESOURCE_MEM;
11862306a36Sopenharmony_ci		dev_params->res_count++;
11962306a36Sopenharmony_ci	}
12062306a36Sopenharmony_ci
12162306a36Sopenharmony_ci	dev_params->vendor = MCDI_WORD(outbuf, CDX_BUS_GET_DEVICE_CONFIG_OUT_VENDOR_ID);
12262306a36Sopenharmony_ci	dev_params->device = MCDI_WORD(outbuf, CDX_BUS_GET_DEVICE_CONFIG_OUT_DEVICE_ID);
12362306a36Sopenharmony_ci
12462306a36Sopenharmony_ci	return 0;
12562306a36Sopenharmony_ci}
12662306a36Sopenharmony_ci
12762306a36Sopenharmony_ciint cdx_mcdi_reset_device(struct cdx_mcdi *cdx, u8 bus_num, u8 dev_num)
12862306a36Sopenharmony_ci{
12962306a36Sopenharmony_ci	MCDI_DECLARE_BUF(inbuf, MC_CMD_CDX_DEVICE_RESET_IN_LEN);
13062306a36Sopenharmony_ci	int ret;
13162306a36Sopenharmony_ci
13262306a36Sopenharmony_ci	MCDI_SET_DWORD(inbuf, CDX_DEVICE_RESET_IN_BUS, bus_num);
13362306a36Sopenharmony_ci	MCDI_SET_DWORD(inbuf, CDX_DEVICE_RESET_IN_DEVICE, dev_num);
13462306a36Sopenharmony_ci
13562306a36Sopenharmony_ci	ret = cdx_mcdi_rpc(cdx, MC_CMD_CDX_DEVICE_RESET, inbuf, sizeof(inbuf),
13662306a36Sopenharmony_ci			   NULL, 0, NULL);
13762306a36Sopenharmony_ci
13862306a36Sopenharmony_ci	return ret;
13962306a36Sopenharmony_ci}
140