xref: /dpdk/drivers/bus/fslmc/mc/mc_sys.c (revision 06cce2876a37ac6161652bd121ce1ff03d52cce5)
1131a75b6SHemant Agrawal /* SPDX-License-Identifier: (BSD-3-Clause OR GPL-2.0)
23af733baSHemant Agrawal  *
33af733baSHemant Agrawal  * Copyright 2013-2015 Freescale Semiconductor Inc.
479711846SRohit Raj  * Copyright 2017,2022 NXP
53af733baSHemant Agrawal  *
63af733baSHemant Agrawal  */
73af733baSHemant Agrawal #include <fsl_mc_sys.h>
83af733baSHemant Agrawal #include <fsl_mc_cmd.h>
93af733baSHemant Agrawal 
103af733baSHemant Agrawal #include <rte_spinlock.h>
1179711846SRohit Raj #include <rte_cycles.h>
123af733baSHemant Agrawal 
133af733baSHemant Agrawal /** User space framework uses MC Portal in shared mode. Following change
143af733baSHemant Agrawal  * introduces lock in MC FLIB
153af733baSHemant Agrawal  */
163af733baSHemant Agrawal 
173af733baSHemant Agrawal /**
183af733baSHemant Agrawal  * A static spinlock initializer.
193af733baSHemant Agrawal  */
203af733baSHemant Agrawal static rte_spinlock_t mc_portal_lock = RTE_SPINLOCK_INITIALIZER;
213af733baSHemant Agrawal 
mc_status_to_error(enum mc_cmd_status status)223af733baSHemant Agrawal static int mc_status_to_error(enum mc_cmd_status status)
233af733baSHemant Agrawal {
243af733baSHemant Agrawal 	switch (status) {
253af733baSHemant Agrawal 	case MC_CMD_STATUS_OK:
263af733baSHemant Agrawal 		return 0;
273af733baSHemant Agrawal 	case MC_CMD_STATUS_AUTH_ERR:
283af733baSHemant Agrawal 		return -EACCES; /* Token error */
293af733baSHemant Agrawal 	case MC_CMD_STATUS_NO_PRIVILEGE:
303af733baSHemant Agrawal 		return -EPERM; /* Permission denied */
313af733baSHemant Agrawal 	case MC_CMD_STATUS_DMA_ERR:
323af733baSHemant Agrawal 		return -EIO; /* Input/Output error */
333af733baSHemant Agrawal 	case MC_CMD_STATUS_CONFIG_ERR:
343af733baSHemant Agrawal 		return -EINVAL; /* Device not configured */
353af733baSHemant Agrawal 	case MC_CMD_STATUS_TIMEOUT:
363af733baSHemant Agrawal 		return -ETIMEDOUT; /* Operation timed out */
373af733baSHemant Agrawal 	case MC_CMD_STATUS_NO_RESOURCE:
383af733baSHemant Agrawal 		return -ENAVAIL; /* Resource temporarily unavailable */
393af733baSHemant Agrawal 	case MC_CMD_STATUS_NO_MEMORY:
403af733baSHemant Agrawal 		return -ENOMEM; /* Cannot allocate memory */
413af733baSHemant Agrawal 	case MC_CMD_STATUS_BUSY:
423af733baSHemant Agrawal 		return -EBUSY; /* Device busy */
433af733baSHemant Agrawal 	case MC_CMD_STATUS_UNSUPPORTED_OP:
443af733baSHemant Agrawal 		return -ENOTSUP; /* Operation not supported by device */
453af733baSHemant Agrawal 	case MC_CMD_STATUS_INVALID_STATE:
463af733baSHemant Agrawal 		return -ENODEV; /* Invalid device state */
473af733baSHemant Agrawal 	default:
483af733baSHemant Agrawal 		break;
493af733baSHemant Agrawal 	}
503af733baSHemant Agrawal 
513af733baSHemant Agrawal 	/* Not expected to reach here */
523af733baSHemant Agrawal 	return -EINVAL;
533af733baSHemant Agrawal }
543af733baSHemant Agrawal 
mc_send_command(struct fsl_mc_io * mc_io,struct mc_command * cmd)553af733baSHemant Agrawal int mc_send_command(struct fsl_mc_io *mc_io, struct mc_command *cmd)
563af733baSHemant Agrawal {
573af733baSHemant Agrawal 	enum mc_cmd_status status;
5879711846SRohit Raj 	uint64_t response, start_time, total_time, time_to_wait;
593af733baSHemant Agrawal 
603af733baSHemant Agrawal 	if (!mc_io || !mc_io->regs)
613af733baSHemant Agrawal 		return -EACCES;
623af733baSHemant Agrawal 
633af733baSHemant Agrawal 	/* --- Call lock function here in case portal is shared --- */
643af733baSHemant Agrawal 	rte_spinlock_lock(&mc_portal_lock);
653af733baSHemant Agrawal 
663af733baSHemant Agrawal 	mc_write_command(mc_io->regs, cmd);
673af733baSHemant Agrawal 
6879711846SRohit Raj 	/* Wait for one second. rte_get_timer_hz() returns frequency of CPU */
6979711846SRohit Raj 	time_to_wait = rte_get_timer_hz();
7079711846SRohit Raj 	total_time = 0;
7179711846SRohit Raj 	start_time = rte_get_timer_cycles();
7279711846SRohit Raj 
733af733baSHemant Agrawal 	/* Spin until status changes */
743af733baSHemant Agrawal 	do {
7516bbc98aSShreyansh Jain 		response = ioread64(mc_io->regs);
7616bbc98aSShreyansh Jain 		status = mc_cmd_read_status((struct mc_command *)&response);
7779711846SRohit Raj 		total_time = rte_get_timer_cycles() - start_time;
7879711846SRohit Raj 	} while (status == MC_CMD_STATUS_READY && total_time <= time_to_wait);
793af733baSHemant Agrawal 
80*06cce287SAlvaro Karsz 	if (status == MC_CMD_STATUS_READY) {
81*06cce287SAlvaro Karsz 		rte_spinlock_unlock(&mc_portal_lock);
82*06cce287SAlvaro Karsz 
8379711846SRohit Raj 		return mc_status_to_error(MC_CMD_STATUS_TIMEOUT);
84*06cce287SAlvaro Karsz 	}
853af733baSHemant Agrawal 
863af733baSHemant Agrawal 	/* Read the response back into the command buffer */
873af733baSHemant Agrawal 	mc_read_response(mc_io->regs, cmd);
883af733baSHemant Agrawal 
893af733baSHemant Agrawal 	/* --- Call unlock function here in case portal is shared --- */
903af733baSHemant Agrawal 	rte_spinlock_unlock(&mc_portal_lock);
913af733baSHemant Agrawal 
923af733baSHemant Agrawal 	return mc_status_to_error(status);
933af733baSHemant Agrawal }
94