18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright(c) 2018 Intel Corporation.
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci */
68c2ecf20Sopenharmony_ci#include "iowait.h"
78c2ecf20Sopenharmony_ci#include "trace_iowait.h"
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci/* 1 priority == 16 starve_cnt */
108c2ecf20Sopenharmony_ci#define IOWAIT_PRIORITY_STARVE_SHIFT 4
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_civoid iowait_set_flag(struct iowait *wait, u32 flag)
138c2ecf20Sopenharmony_ci{
148c2ecf20Sopenharmony_ci	trace_hfi1_iowait_set(wait, flag);
158c2ecf20Sopenharmony_ci	set_bit(flag, &wait->flags);
168c2ecf20Sopenharmony_ci}
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_cibool iowait_flag_set(struct iowait *wait, u32 flag)
198c2ecf20Sopenharmony_ci{
208c2ecf20Sopenharmony_ci	return test_bit(flag, &wait->flags);
218c2ecf20Sopenharmony_ci}
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ciinline void iowait_clear_flag(struct iowait *wait, u32 flag)
248c2ecf20Sopenharmony_ci{
258c2ecf20Sopenharmony_ci	trace_hfi1_iowait_clear(wait, flag);
268c2ecf20Sopenharmony_ci	clear_bit(flag, &wait->flags);
278c2ecf20Sopenharmony_ci}
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ci/**
308c2ecf20Sopenharmony_ci * iowait_init() - initialize wait structure
318c2ecf20Sopenharmony_ci * @wait: wait struct to initialize
328c2ecf20Sopenharmony_ci * @tx_limit: limit for overflow queuing
338c2ecf20Sopenharmony_ci * @func: restart function for workqueue
348c2ecf20Sopenharmony_ci * @sleep: sleep function for no space
358c2ecf20Sopenharmony_ci * @resume: wakeup function for no space
368c2ecf20Sopenharmony_ci *
378c2ecf20Sopenharmony_ci * This function initializes the iowait
388c2ecf20Sopenharmony_ci * structure embedded in the QP or PQ.
398c2ecf20Sopenharmony_ci *
408c2ecf20Sopenharmony_ci */
418c2ecf20Sopenharmony_civoid iowait_init(struct iowait *wait, u32 tx_limit,
428c2ecf20Sopenharmony_ci		 void (*func)(struct work_struct *work),
438c2ecf20Sopenharmony_ci		 void (*tidfunc)(struct work_struct *work),
448c2ecf20Sopenharmony_ci		 int (*sleep)(struct sdma_engine *sde,
458c2ecf20Sopenharmony_ci			      struct iowait_work *wait,
468c2ecf20Sopenharmony_ci			      struct sdma_txreq *tx,
478c2ecf20Sopenharmony_ci			      uint seq,
488c2ecf20Sopenharmony_ci			      bool pkts_sent),
498c2ecf20Sopenharmony_ci		 void (*wakeup)(struct iowait *wait, int reason),
508c2ecf20Sopenharmony_ci		 void (*sdma_drained)(struct iowait *wait),
518c2ecf20Sopenharmony_ci		 void (*init_priority)(struct iowait *wait))
528c2ecf20Sopenharmony_ci{
538c2ecf20Sopenharmony_ci	int i;
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci	wait->count = 0;
568c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&wait->list);
578c2ecf20Sopenharmony_ci	init_waitqueue_head(&wait->wait_dma);
588c2ecf20Sopenharmony_ci	init_waitqueue_head(&wait->wait_pio);
598c2ecf20Sopenharmony_ci	atomic_set(&wait->sdma_busy, 0);
608c2ecf20Sopenharmony_ci	atomic_set(&wait->pio_busy, 0);
618c2ecf20Sopenharmony_ci	wait->tx_limit = tx_limit;
628c2ecf20Sopenharmony_ci	wait->sleep = sleep;
638c2ecf20Sopenharmony_ci	wait->wakeup = wakeup;
648c2ecf20Sopenharmony_ci	wait->sdma_drained = sdma_drained;
658c2ecf20Sopenharmony_ci	wait->init_priority = init_priority;
668c2ecf20Sopenharmony_ci	wait->flags = 0;
678c2ecf20Sopenharmony_ci	for (i = 0; i < IOWAIT_SES; i++) {
688c2ecf20Sopenharmony_ci		wait->wait[i].iow = wait;
698c2ecf20Sopenharmony_ci		INIT_LIST_HEAD(&wait->wait[i].tx_head);
708c2ecf20Sopenharmony_ci		if (i == IOWAIT_IB_SE)
718c2ecf20Sopenharmony_ci			INIT_WORK(&wait->wait[i].iowork, func);
728c2ecf20Sopenharmony_ci		else
738c2ecf20Sopenharmony_ci			INIT_WORK(&wait->wait[i].iowork, tidfunc);
748c2ecf20Sopenharmony_ci	}
758c2ecf20Sopenharmony_ci}
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ci/**
788c2ecf20Sopenharmony_ci * iowait_cancel_work - cancel all work in iowait
798c2ecf20Sopenharmony_ci * @w: the iowait struct
808c2ecf20Sopenharmony_ci */
818c2ecf20Sopenharmony_civoid iowait_cancel_work(struct iowait *w)
828c2ecf20Sopenharmony_ci{
838c2ecf20Sopenharmony_ci	cancel_work_sync(&iowait_get_ib_work(w)->iowork);
848c2ecf20Sopenharmony_ci	/* Make sure that the iowork for TID RDMA is used */
858c2ecf20Sopenharmony_ci	if (iowait_get_tid_work(w)->iowork.func)
868c2ecf20Sopenharmony_ci		cancel_work_sync(&iowait_get_tid_work(w)->iowork);
878c2ecf20Sopenharmony_ci}
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ci/**
908c2ecf20Sopenharmony_ci * iowait_set_work_flag - set work flag based on leg
918c2ecf20Sopenharmony_ci * @w - the iowait work struct
928c2ecf20Sopenharmony_ci */
938c2ecf20Sopenharmony_ciint iowait_set_work_flag(struct iowait_work *w)
948c2ecf20Sopenharmony_ci{
958c2ecf20Sopenharmony_ci	if (w == &w->iow->wait[IOWAIT_IB_SE]) {
968c2ecf20Sopenharmony_ci		iowait_set_flag(w->iow, IOWAIT_PENDING_IB);
978c2ecf20Sopenharmony_ci		return IOWAIT_IB_SE;
988c2ecf20Sopenharmony_ci	}
998c2ecf20Sopenharmony_ci	iowait_set_flag(w->iow, IOWAIT_PENDING_TID);
1008c2ecf20Sopenharmony_ci	return IOWAIT_TID_SE;
1018c2ecf20Sopenharmony_ci}
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ci/**
1048c2ecf20Sopenharmony_ci * iowait_priority_update_top - update the top priority entry
1058c2ecf20Sopenharmony_ci * @w: the iowait struct
1068c2ecf20Sopenharmony_ci * @top: a pointer to the top priority entry
1078c2ecf20Sopenharmony_ci * @idx: the index of the current iowait in an array
1088c2ecf20Sopenharmony_ci * @top_idx: the array index for the iowait entry that has the top priority
1098c2ecf20Sopenharmony_ci *
1108c2ecf20Sopenharmony_ci * This function is called to compare the priority of a given
1118c2ecf20Sopenharmony_ci * iowait with the given top priority entry. The top index will
1128c2ecf20Sopenharmony_ci * be returned.
1138c2ecf20Sopenharmony_ci */
1148c2ecf20Sopenharmony_ciuint iowait_priority_update_top(struct iowait *w,
1158c2ecf20Sopenharmony_ci				struct iowait *top,
1168c2ecf20Sopenharmony_ci				uint idx, uint top_idx)
1178c2ecf20Sopenharmony_ci{
1188c2ecf20Sopenharmony_ci	u8 cnt, tcnt;
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ci	/* Convert priority into starve_cnt and compare the total.*/
1218c2ecf20Sopenharmony_ci	cnt = (w->priority << IOWAIT_PRIORITY_STARVE_SHIFT) + w->starved_cnt;
1228c2ecf20Sopenharmony_ci	tcnt = (top->priority << IOWAIT_PRIORITY_STARVE_SHIFT) +
1238c2ecf20Sopenharmony_ci		top->starved_cnt;
1248c2ecf20Sopenharmony_ci	if (cnt > tcnt)
1258c2ecf20Sopenharmony_ci		return idx;
1268c2ecf20Sopenharmony_ci	else
1278c2ecf20Sopenharmony_ci		return top_idx;
1288c2ecf20Sopenharmony_ci}
129