xref: /linux/drivers/soc/ti/knav_qmss_queue.c (revision a88f66d4a8668e66afa8eb4099b4515e0729b52a)
11802d0beSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
241f93af9SSandeep Nair /*
341f93af9SSandeep Nair  * Keystone Queue Manager subsystem driver
441f93af9SSandeep Nair  *
541f93af9SSandeep Nair  * Copyright (C) 2014 Texas Instruments Incorporated - http://www.ti.com
641f93af9SSandeep Nair  * Authors:	Sandeep Nair <sandeep_n@ti.com>
741f93af9SSandeep Nair  *		Cyril Chemparathy <cyril@ti.com>
841f93af9SSandeep Nair  *		Santosh Shilimkar <santosh.shilimkar@ti.com>
941f93af9SSandeep Nair  */
1041f93af9SSandeep Nair 
1141f93af9SSandeep Nair #include <linux/debugfs.h>
126e03f653SMurali Karicheri #include <linux/dma-mapping.h>
136e03f653SMurali Karicheri #include <linux/firmware.h>
146e03f653SMurali Karicheri #include <linux/interrupt.h>
156e03f653SMurali Karicheri #include <linux/io.h>
166e03f653SMurali Karicheri #include <linux/module.h>
176e03f653SMurali Karicheri #include <linux/of_address.h>
186e03f653SMurali Karicheri #include <linux/of_device.h>
196e03f653SMurali Karicheri #include <linux/of_irq.h>
206e03f653SMurali Karicheri #include <linux/pm_runtime.h>
216e03f653SMurali Karicheri #include <linux/slab.h>
2241f93af9SSandeep Nair #include <linux/soc/ti/knav_qmss.h>
2341f93af9SSandeep Nair 
2441f93af9SSandeep Nair #include "knav_qmss.h"
2541f93af9SSandeep Nair 
2641f93af9SSandeep Nair static struct knav_device *kdev;
2741f93af9SSandeep Nair static DEFINE_MUTEX(knav_dev_lock);
28feec214aSAmol Grover #define knav_dev_lock_held() \
29feec214aSAmol Grover 	lockdep_is_held(&knav_dev_lock)
3041f93af9SSandeep Nair 
3141f93af9SSandeep Nair /* Queue manager register indices in DTS */
3241f93af9SSandeep Nair #define KNAV_QUEUE_PEEK_REG_INDEX	0
3341f93af9SSandeep Nair #define KNAV_QUEUE_STATUS_REG_INDEX	1
3441f93af9SSandeep Nair #define KNAV_QUEUE_CONFIG_REG_INDEX	2
3541f93af9SSandeep Nair #define KNAV_QUEUE_REGION_REG_INDEX	3
3641f93af9SSandeep Nair #define KNAV_QUEUE_PUSH_REG_INDEX	4
3741f93af9SSandeep Nair #define KNAV_QUEUE_POP_REG_INDEX	5
3841f93af9SSandeep Nair 
39350601b4SMurali Karicheri /* Queue manager register indices in DTS for QMSS in K2G NAVSS.
40350601b4SMurali Karicheri  * There are no status and vbusm push registers on this version
41350601b4SMurali Karicheri  * of QMSS. Push registers are same as pop, So all indices above 1
42350601b4SMurali Karicheri  * are to be re-defined
43350601b4SMurali Karicheri  */
44350601b4SMurali Karicheri #define KNAV_L_QUEUE_CONFIG_REG_INDEX	1
45350601b4SMurali Karicheri #define KNAV_L_QUEUE_REGION_REG_INDEX	2
46350601b4SMurali Karicheri #define KNAV_L_QUEUE_PUSH_REG_INDEX	3
47350601b4SMurali Karicheri 
4841f93af9SSandeep Nair /* PDSP register indices in DTS */
4941f93af9SSandeep Nair #define KNAV_QUEUE_PDSP_IRAM_REG_INDEX	0
5041f93af9SSandeep Nair #define KNAV_QUEUE_PDSP_REGS_REG_INDEX	1
5141f93af9SSandeep Nair #define KNAV_QUEUE_PDSP_INTD_REG_INDEX	2
5241f93af9SSandeep Nair #define KNAV_QUEUE_PDSP_CMD_REG_INDEX	3
5341f93af9SSandeep Nair 
5441f93af9SSandeep Nair #define knav_queue_idx_to_inst(kdev, idx)			\
5541f93af9SSandeep Nair 	(kdev->instances + (idx << kdev->inst_shift))
5641f93af9SSandeep Nair 
5741f93af9SSandeep Nair #define for_each_handle_rcu(qh, inst)				\
58feec214aSAmol Grover 	list_for_each_entry_rcu(qh, &inst->handles, list,	\
59feec214aSAmol Grover 				knav_dev_lock_held())
6041f93af9SSandeep Nair 
6141f93af9SSandeep Nair #define for_each_instance(idx, inst, kdev)		\
6241f93af9SSandeep Nair 	for (idx = 0, inst = kdev->instances;		\
6341f93af9SSandeep Nair 	     idx < (kdev)->num_queues_in_use;			\
6441f93af9SSandeep Nair 	     idx++, inst = knav_queue_idx_to_inst(kdev, idx))
6541f93af9SSandeep Nair 
6696ee19beSMurali Karicheri /* All firmware file names end up here. List the firmware file names below.
6796ee19beSMurali Karicheri  * Newest followed by older ones. Search is done from start of the array
6896ee19beSMurali Karicheri  * until a firmware file is found.
6996ee19beSMurali Karicheri  */
7096ee19beSMurali Karicheri const char *knav_acc_firmwares[] = {"ks2_qmss_pdsp_acc48.bin"};
7196ee19beSMurali Karicheri 
72a2dd6877SMurali Karicheri static bool device_ready;
73a2dd6877SMurali Karicheri bool knav_qmss_device_ready(void)
74a2dd6877SMurali Karicheri {
75a2dd6877SMurali Karicheri 	return device_ready;
76a2dd6877SMurali Karicheri }
77a2dd6877SMurali Karicheri EXPORT_SYMBOL_GPL(knav_qmss_device_ready);
78a2dd6877SMurali Karicheri 
7941f93af9SSandeep Nair /**
8041f93af9SSandeep Nair  * knav_queue_notify: qmss queue notfier call
8141f93af9SSandeep Nair  *
82e8ebf411SLee Jones  * @inst:		- qmss queue instance like accumulator
8341f93af9SSandeep Nair  */
8441f93af9SSandeep Nair void knav_queue_notify(struct knav_queue_inst *inst)
8541f93af9SSandeep Nair {
8641f93af9SSandeep Nair 	struct knav_queue *qh;
8741f93af9SSandeep Nair 
8841f93af9SSandeep Nair 	if (!inst)
8941f93af9SSandeep Nair 		return;
9041f93af9SSandeep Nair 
9141f93af9SSandeep Nair 	rcu_read_lock();
9241f93af9SSandeep Nair 	for_each_handle_rcu(qh, inst) {
9341f93af9SSandeep Nair 		if (atomic_read(&qh->notifier_enabled) <= 0)
9441f93af9SSandeep Nair 			continue;
9541f93af9SSandeep Nair 		if (WARN_ON(!qh->notifier_fn))
9641f93af9SSandeep Nair 			continue;
97bc3acbb8SVasyl Gomonovych 		this_cpu_inc(qh->stats->notifies);
9841f93af9SSandeep Nair 		qh->notifier_fn(qh->notifier_fn_arg);
9941f93af9SSandeep Nair 	}
10041f93af9SSandeep Nair 	rcu_read_unlock();
10141f93af9SSandeep Nair }
10241f93af9SSandeep Nair EXPORT_SYMBOL_GPL(knav_queue_notify);
10341f93af9SSandeep Nair 
10441f93af9SSandeep Nair static irqreturn_t knav_queue_int_handler(int irq, void *_instdata)
10541f93af9SSandeep Nair {
10641f93af9SSandeep Nair 	struct knav_queue_inst *inst = _instdata;
10741f93af9SSandeep Nair 
10841f93af9SSandeep Nair 	knav_queue_notify(inst);
10941f93af9SSandeep Nair 	return IRQ_HANDLED;
11041f93af9SSandeep Nair }
11141f93af9SSandeep Nair 
11241f93af9SSandeep Nair static int knav_queue_setup_irq(struct knav_range_info *range,
11341f93af9SSandeep Nair 			  struct knav_queue_inst *inst)
11441f93af9SSandeep Nair {
11541f93af9SSandeep Nair 	unsigned queue = inst->id - range->queue_base;
11641f93af9SSandeep Nair 	int ret = 0, irq;
11741f93af9SSandeep Nair 
11841f93af9SSandeep Nair 	if (range->flags & RANGE_HAS_IRQ) {
11941f93af9SSandeep Nair 		irq = range->irqs[queue].irq;
12041f93af9SSandeep Nair 		ret = request_irq(irq, knav_queue_int_handler, 0,
12141f93af9SSandeep Nair 					inst->irq_name, inst);
12241f93af9SSandeep Nair 		if (ret)
12341f93af9SSandeep Nair 			return ret;
12441f93af9SSandeep Nair 		disable_irq(irq);
125832ad0e3SMarc Zyngier 		if (range->irqs[queue].cpu_mask) {
126832ad0e3SMarc Zyngier 			ret = irq_set_affinity_hint(irq, range->irqs[queue].cpu_mask);
12741f93af9SSandeep Nair 			if (ret) {
12841f93af9SSandeep Nair 				dev_warn(range->kdev->dev,
12941f93af9SSandeep Nair 					 "Failed to set IRQ affinity\n");
13041f93af9SSandeep Nair 				return ret;
13141f93af9SSandeep Nair 			}
13241f93af9SSandeep Nair 		}
13341f93af9SSandeep Nair 	}
13441f93af9SSandeep Nair 	return ret;
13541f93af9SSandeep Nair }
13641f93af9SSandeep Nair 
13741f93af9SSandeep Nair static void knav_queue_free_irq(struct knav_queue_inst *inst)
13841f93af9SSandeep Nair {
13941f93af9SSandeep Nair 	struct knav_range_info *range = inst->range;
14041f93af9SSandeep Nair 	unsigned queue = inst->id - inst->range->queue_base;
14141f93af9SSandeep Nair 	int irq;
14241f93af9SSandeep Nair 
14341f93af9SSandeep Nair 	if (range->flags & RANGE_HAS_IRQ) {
14441f93af9SSandeep Nair 		irq = range->irqs[queue].irq;
14541f93af9SSandeep Nair 		irq_set_affinity_hint(irq, NULL);
14641f93af9SSandeep Nair 		free_irq(irq, inst);
14741f93af9SSandeep Nair 	}
14841f93af9SSandeep Nair }
14941f93af9SSandeep Nair 
15041f93af9SSandeep Nair static inline bool knav_queue_is_busy(struct knav_queue_inst *inst)
15141f93af9SSandeep Nair {
15241f93af9SSandeep Nair 	return !list_empty(&inst->handles);
15341f93af9SSandeep Nair }
15441f93af9SSandeep Nair 
15541f93af9SSandeep Nair static inline bool knav_queue_is_reserved(struct knav_queue_inst *inst)
15641f93af9SSandeep Nair {
15741f93af9SSandeep Nair 	return inst->range->flags & RANGE_RESERVED;
15841f93af9SSandeep Nair }
15941f93af9SSandeep Nair 
16041f93af9SSandeep Nair static inline bool knav_queue_is_shared(struct knav_queue_inst *inst)
16141f93af9SSandeep Nair {
16241f93af9SSandeep Nair 	struct knav_queue *tmp;
16341f93af9SSandeep Nair 
16441f93af9SSandeep Nair 	rcu_read_lock();
16541f93af9SSandeep Nair 	for_each_handle_rcu(tmp, inst) {
16641f93af9SSandeep Nair 		if (tmp->flags & KNAV_QUEUE_SHARED) {
16741f93af9SSandeep Nair 			rcu_read_unlock();
16841f93af9SSandeep Nair 			return true;
16941f93af9SSandeep Nair 		}
17041f93af9SSandeep Nair 	}
17141f93af9SSandeep Nair 	rcu_read_unlock();
17241f93af9SSandeep Nair 	return false;
17341f93af9SSandeep Nair }
17441f93af9SSandeep Nair 
17541f93af9SSandeep Nair static inline bool knav_queue_match_type(struct knav_queue_inst *inst,
17641f93af9SSandeep Nair 						unsigned type)
17741f93af9SSandeep Nair {
17841f93af9SSandeep Nair 	if ((type == KNAV_QUEUE_QPEND) &&
17941f93af9SSandeep Nair 	    (inst->range->flags & RANGE_HAS_IRQ)) {
18041f93af9SSandeep Nair 		return true;
18141f93af9SSandeep Nair 	} else if ((type == KNAV_QUEUE_ACC) &&
18241f93af9SSandeep Nair 		(inst->range->flags & RANGE_HAS_ACCUMULATOR)) {
18341f93af9SSandeep Nair 		return true;
18441f93af9SSandeep Nair 	} else if ((type == KNAV_QUEUE_GP) &&
18541f93af9SSandeep Nair 		!(inst->range->flags &
18641f93af9SSandeep Nair 			(RANGE_HAS_ACCUMULATOR | RANGE_HAS_IRQ))) {
18741f93af9SSandeep Nair 		return true;
18841f93af9SSandeep Nair 	}
18941f93af9SSandeep Nair 	return false;
19041f93af9SSandeep Nair }
19141f93af9SSandeep Nair 
19241f93af9SSandeep Nair static inline struct knav_queue_inst *
19341f93af9SSandeep Nair knav_queue_match_id_to_inst(struct knav_device *kdev, unsigned id)
19441f93af9SSandeep Nair {
19541f93af9SSandeep Nair 	struct knav_queue_inst *inst;
19641f93af9SSandeep Nair 	int idx;
19741f93af9SSandeep Nair 
19841f93af9SSandeep Nair 	for_each_instance(idx, inst, kdev) {
19941f93af9SSandeep Nair 		if (inst->id == id)
20041f93af9SSandeep Nair 			return inst;
20141f93af9SSandeep Nair 	}
20241f93af9SSandeep Nair 	return NULL;
20341f93af9SSandeep Nair }
20441f93af9SSandeep Nair 
20541f93af9SSandeep Nair static inline struct knav_queue_inst *knav_queue_find_by_id(int id)
20641f93af9SSandeep Nair {
20741f93af9SSandeep Nair 	if (kdev->base_id <= id &&
20841f93af9SSandeep Nair 	    kdev->base_id + kdev->num_queues > id) {
20941f93af9SSandeep Nair 		id -= kdev->base_id;
21041f93af9SSandeep Nair 		return knav_queue_match_id_to_inst(kdev, id);
21141f93af9SSandeep Nair 	}
21241f93af9SSandeep Nair 	return NULL;
21341f93af9SSandeep Nair }
21441f93af9SSandeep Nair 
21541f93af9SSandeep Nair static struct knav_queue *__knav_queue_open(struct knav_queue_inst *inst,
21641f93af9SSandeep Nair 				      const char *name, unsigned flags)
21741f93af9SSandeep Nair {
21841f93af9SSandeep Nair 	struct knav_queue *qh;
21941f93af9SSandeep Nair 	unsigned id;
22041f93af9SSandeep Nair 	int ret = 0;
22141f93af9SSandeep Nair 
22241f93af9SSandeep Nair 	qh = devm_kzalloc(inst->kdev->dev, sizeof(*qh), GFP_KERNEL);
22341f93af9SSandeep Nair 	if (!qh)
22441f93af9SSandeep Nair 		return ERR_PTR(-ENOMEM);
22541f93af9SSandeep Nair 
226bc3acbb8SVasyl Gomonovych 	qh->stats = alloc_percpu(struct knav_queue_stats);
227bc3acbb8SVasyl Gomonovych 	if (!qh->stats) {
228bc3acbb8SVasyl Gomonovych 		ret = -ENOMEM;
229bc3acbb8SVasyl Gomonovych 		goto err;
230bc3acbb8SVasyl Gomonovych 	}
231bc3acbb8SVasyl Gomonovych 
23241f93af9SSandeep Nair 	qh->flags = flags;
23341f93af9SSandeep Nair 	qh->inst = inst;
23441f93af9SSandeep Nair 	id = inst->id - inst->qmgr->start_queue;
23541f93af9SSandeep Nair 	qh->reg_push = &inst->qmgr->reg_push[id];
23641f93af9SSandeep Nair 	qh->reg_pop = &inst->qmgr->reg_pop[id];
23741f93af9SSandeep Nair 	qh->reg_peek = &inst->qmgr->reg_peek[id];
23841f93af9SSandeep Nair 
23941f93af9SSandeep Nair 	/* first opener? */
24041f93af9SSandeep Nair 	if (!knav_queue_is_busy(inst)) {
24141f93af9SSandeep Nair 		struct knav_range_info *range = inst->range;
24241f93af9SSandeep Nair 
243aefc5818SMa Shimiao 		inst->name = kstrndup(name, KNAV_NAME_SIZE - 1, GFP_KERNEL);
24441f93af9SSandeep Nair 		if (range->ops && range->ops->open_queue)
24541f93af9SSandeep Nair 			ret = range->ops->open_queue(range, inst, flags);
24641f93af9SSandeep Nair 
247bc3acbb8SVasyl Gomonovych 		if (ret)
248bc3acbb8SVasyl Gomonovych 			goto err;
24941f93af9SSandeep Nair 	}
25041f93af9SSandeep Nair 	list_add_tail_rcu(&qh->list, &inst->handles);
25141f93af9SSandeep Nair 	return qh;
252bc3acbb8SVasyl Gomonovych 
253bc3acbb8SVasyl Gomonovych err:
254bc3acbb8SVasyl Gomonovych 	if (qh->stats)
255bc3acbb8SVasyl Gomonovych 		free_percpu(qh->stats);
256bc3acbb8SVasyl Gomonovych 	devm_kfree(inst->kdev->dev, qh);
257bc3acbb8SVasyl Gomonovych 	return ERR_PTR(ret);
25841f93af9SSandeep Nair }
25941f93af9SSandeep Nair 
26041f93af9SSandeep Nair static struct knav_queue *
26141f93af9SSandeep Nair knav_queue_open_by_id(const char *name, unsigned id, unsigned flags)
26241f93af9SSandeep Nair {
26341f93af9SSandeep Nair 	struct knav_queue_inst *inst;
26441f93af9SSandeep Nair 	struct knav_queue *qh;
26541f93af9SSandeep Nair 
26641f93af9SSandeep Nair 	mutex_lock(&knav_dev_lock);
26741f93af9SSandeep Nair 
26841f93af9SSandeep Nair 	qh = ERR_PTR(-ENODEV);
26941f93af9SSandeep Nair 	inst = knav_queue_find_by_id(id);
27041f93af9SSandeep Nair 	if (!inst)
27141f93af9SSandeep Nair 		goto unlock_ret;
27241f93af9SSandeep Nair 
27341f93af9SSandeep Nair 	qh = ERR_PTR(-EEXIST);
27441f93af9SSandeep Nair 	if (!(flags & KNAV_QUEUE_SHARED) && knav_queue_is_busy(inst))
27541f93af9SSandeep Nair 		goto unlock_ret;
27641f93af9SSandeep Nair 
27741f93af9SSandeep Nair 	qh = ERR_PTR(-EBUSY);
27841f93af9SSandeep Nair 	if ((flags & KNAV_QUEUE_SHARED) &&
27941f93af9SSandeep Nair 	    (knav_queue_is_busy(inst) && !knav_queue_is_shared(inst)))
28041f93af9SSandeep Nair 		goto unlock_ret;
28141f93af9SSandeep Nair 
28241f93af9SSandeep Nair 	qh = __knav_queue_open(inst, name, flags);
28341f93af9SSandeep Nair 
28441f93af9SSandeep Nair unlock_ret:
28541f93af9SSandeep Nair 	mutex_unlock(&knav_dev_lock);
28641f93af9SSandeep Nair 
28741f93af9SSandeep Nair 	return qh;
28841f93af9SSandeep Nair }
28941f93af9SSandeep Nair 
29041f93af9SSandeep Nair static struct knav_queue *knav_queue_open_by_type(const char *name,
29141f93af9SSandeep Nair 						unsigned type, unsigned flags)
29241f93af9SSandeep Nair {
29341f93af9SSandeep Nair 	struct knav_queue_inst *inst;
29441f93af9SSandeep Nair 	struct knav_queue *qh = ERR_PTR(-EINVAL);
29541f93af9SSandeep Nair 	int idx;
29641f93af9SSandeep Nair 
29741f93af9SSandeep Nair 	mutex_lock(&knav_dev_lock);
29841f93af9SSandeep Nair 
29941f93af9SSandeep Nair 	for_each_instance(idx, inst, kdev) {
30041f93af9SSandeep Nair 		if (knav_queue_is_reserved(inst))
30141f93af9SSandeep Nair 			continue;
30241f93af9SSandeep Nair 		if (!knav_queue_match_type(inst, type))
30341f93af9SSandeep Nair 			continue;
30441f93af9SSandeep Nair 		if (knav_queue_is_busy(inst))
30541f93af9SSandeep Nair 			continue;
30641f93af9SSandeep Nair 		qh = __knav_queue_open(inst, name, flags);
30741f93af9SSandeep Nair 		goto unlock_ret;
30841f93af9SSandeep Nair 	}
30941f93af9SSandeep Nair 
31041f93af9SSandeep Nair unlock_ret:
31141f93af9SSandeep Nair 	mutex_unlock(&knav_dev_lock);
31241f93af9SSandeep Nair 	return qh;
31341f93af9SSandeep Nair }
31441f93af9SSandeep Nair 
31541f93af9SSandeep Nair static void knav_queue_set_notify(struct knav_queue_inst *inst, bool enabled)
31641f93af9SSandeep Nair {
31741f93af9SSandeep Nair 	struct knav_range_info *range = inst->range;
31841f93af9SSandeep Nair 
31941f93af9SSandeep Nair 	if (range->ops && range->ops->set_notify)
32041f93af9SSandeep Nair 		range->ops->set_notify(range, inst, enabled);
32141f93af9SSandeep Nair }
32241f93af9SSandeep Nair 
32341f93af9SSandeep Nair static int knav_queue_enable_notifier(struct knav_queue *qh)
32441f93af9SSandeep Nair {
32541f93af9SSandeep Nair 	struct knav_queue_inst *inst = qh->inst;
32641f93af9SSandeep Nair 	bool first;
32741f93af9SSandeep Nair 
32841f93af9SSandeep Nair 	if (WARN_ON(!qh->notifier_fn))
32941f93af9SSandeep Nair 		return -EINVAL;
33041f93af9SSandeep Nair 
33141f93af9SSandeep Nair 	/* Adjust the per handle notifier count */
33241f93af9SSandeep Nair 	first = (atomic_inc_return(&qh->notifier_enabled) == 1);
33341f93af9SSandeep Nair 	if (!first)
33441f93af9SSandeep Nair 		return 0; /* nothing to do */
33541f93af9SSandeep Nair 
33641f93af9SSandeep Nair 	/* Now adjust the per instance notifier count */
33741f93af9SSandeep Nair 	first = (atomic_inc_return(&inst->num_notifiers) == 1);
33841f93af9SSandeep Nair 	if (first)
33941f93af9SSandeep Nair 		knav_queue_set_notify(inst, true);
34041f93af9SSandeep Nair 
34141f93af9SSandeep Nair 	return 0;
34241f93af9SSandeep Nair }
34341f93af9SSandeep Nair 
34441f93af9SSandeep Nair static int knav_queue_disable_notifier(struct knav_queue *qh)
34541f93af9SSandeep Nair {
34641f93af9SSandeep Nair 	struct knav_queue_inst *inst = qh->inst;
34741f93af9SSandeep Nair 	bool last;
34841f93af9SSandeep Nair 
34941f93af9SSandeep Nair 	last = (atomic_dec_return(&qh->notifier_enabled) == 0);
35041f93af9SSandeep Nair 	if (!last)
35141f93af9SSandeep Nair 		return 0; /* nothing to do */
35241f93af9SSandeep Nair 
35341f93af9SSandeep Nair 	last = (atomic_dec_return(&inst->num_notifiers) == 0);
35441f93af9SSandeep Nair 	if (last)
35541f93af9SSandeep Nair 		knav_queue_set_notify(inst, false);
35641f93af9SSandeep Nair 
35741f93af9SSandeep Nair 	return 0;
35841f93af9SSandeep Nair }
35941f93af9SSandeep Nair 
36041f93af9SSandeep Nair static int knav_queue_set_notifier(struct knav_queue *qh,
36141f93af9SSandeep Nair 				struct knav_queue_notify_config *cfg)
36241f93af9SSandeep Nair {
36341f93af9SSandeep Nair 	knav_queue_notify_fn old_fn = qh->notifier_fn;
36441f93af9SSandeep Nair 
36541f93af9SSandeep Nair 	if (!cfg)
36641f93af9SSandeep Nair 		return -EINVAL;
36741f93af9SSandeep Nair 
36841f93af9SSandeep Nair 	if (!(qh->inst->range->flags & (RANGE_HAS_ACCUMULATOR | RANGE_HAS_IRQ)))
36941f93af9SSandeep Nair 		return -ENOTSUPP;
37041f93af9SSandeep Nair 
37141f93af9SSandeep Nair 	if (!cfg->fn && old_fn)
37241f93af9SSandeep Nair 		knav_queue_disable_notifier(qh);
37341f93af9SSandeep Nair 
37441f93af9SSandeep Nair 	qh->notifier_fn = cfg->fn;
37541f93af9SSandeep Nair 	qh->notifier_fn_arg = cfg->fn_arg;
37641f93af9SSandeep Nair 
37741f93af9SSandeep Nair 	if (cfg->fn && !old_fn)
37841f93af9SSandeep Nair 		knav_queue_enable_notifier(qh);
37941f93af9SSandeep Nair 
38041f93af9SSandeep Nair 	return 0;
38141f93af9SSandeep Nair }
38241f93af9SSandeep Nair 
38341f93af9SSandeep Nair static int knav_gp_set_notify(struct knav_range_info *range,
38441f93af9SSandeep Nair 			       struct knav_queue_inst *inst,
38541f93af9SSandeep Nair 			       bool enabled)
38641f93af9SSandeep Nair {
38741f93af9SSandeep Nair 	unsigned queue;
38841f93af9SSandeep Nair 
38941f93af9SSandeep Nair 	if (range->flags & RANGE_HAS_IRQ) {
39041f93af9SSandeep Nair 		queue = inst->id - range->queue_base;
39141f93af9SSandeep Nair 		if (enabled)
39241f93af9SSandeep Nair 			enable_irq(range->irqs[queue].irq);
39341f93af9SSandeep Nair 		else
39441f93af9SSandeep Nair 			disable_irq_nosync(range->irqs[queue].irq);
39541f93af9SSandeep Nair 	}
39641f93af9SSandeep Nair 	return 0;
39741f93af9SSandeep Nair }
39841f93af9SSandeep Nair 
39941f93af9SSandeep Nair static int knav_gp_open_queue(struct knav_range_info *range,
40041f93af9SSandeep Nair 				struct knav_queue_inst *inst, unsigned flags)
40141f93af9SSandeep Nair {
40241f93af9SSandeep Nair 	return knav_queue_setup_irq(range, inst);
40341f93af9SSandeep Nair }
40441f93af9SSandeep Nair 
40541f93af9SSandeep Nair static int knav_gp_close_queue(struct knav_range_info *range,
40641f93af9SSandeep Nair 				struct knav_queue_inst *inst)
40741f93af9SSandeep Nair {
40841f93af9SSandeep Nair 	knav_queue_free_irq(inst);
40941f93af9SSandeep Nair 	return 0;
41041f93af9SSandeep Nair }
41141f93af9SSandeep Nair 
412b8b38a8eSSamuel Zou static struct knav_range_ops knav_gp_range_ops = {
41341f93af9SSandeep Nair 	.set_notify	= knav_gp_set_notify,
41441f93af9SSandeep Nair 	.open_queue	= knav_gp_open_queue,
41541f93af9SSandeep Nair 	.close_queue	= knav_gp_close_queue,
41641f93af9SSandeep Nair };
41741f93af9SSandeep Nair 
41841f93af9SSandeep Nair 
41941f93af9SSandeep Nair static int knav_queue_get_count(void *qhandle)
42041f93af9SSandeep Nair {
42141f93af9SSandeep Nair 	struct knav_queue *qh = qhandle;
42241f93af9SSandeep Nair 	struct knav_queue_inst *inst = qh->inst;
42341f93af9SSandeep Nair 
42441f93af9SSandeep Nair 	return readl_relaxed(&qh->reg_peek[0].entry_count) +
42541f93af9SSandeep Nair 		atomic_read(&inst->desc_count);
42641f93af9SSandeep Nair }
42741f93af9SSandeep Nair 
42841f93af9SSandeep Nair static void knav_queue_debug_show_instance(struct seq_file *s,
42941f93af9SSandeep Nair 					struct knav_queue_inst *inst)
43041f93af9SSandeep Nair {
43141f93af9SSandeep Nair 	struct knav_device *kdev = inst->kdev;
43241f93af9SSandeep Nair 	struct knav_queue *qh;
433bc3acbb8SVasyl Gomonovych 	int cpu = 0;
434bc3acbb8SVasyl Gomonovych 	int pushes = 0;
435bc3acbb8SVasyl Gomonovych 	int pops = 0;
436bc3acbb8SVasyl Gomonovych 	int push_errors = 0;
437bc3acbb8SVasyl Gomonovych 	int pop_errors = 0;
438bc3acbb8SVasyl Gomonovych 	int notifies = 0;
43941f93af9SSandeep Nair 
44041f93af9SSandeep Nair 	if (!knav_queue_is_busy(inst))
44141f93af9SSandeep Nair 		return;
44241f93af9SSandeep Nair 
44341f93af9SSandeep Nair 	seq_printf(s, "\tqueue id %d (%s)\n",
44441f93af9SSandeep Nair 		   kdev->base_id + inst->id, inst->name);
44541f93af9SSandeep Nair 	for_each_handle_rcu(qh, inst) {
446bc3acbb8SVasyl Gomonovych 		for_each_possible_cpu(cpu) {
447bc3acbb8SVasyl Gomonovych 			pushes += per_cpu_ptr(qh->stats, cpu)->pushes;
448bc3acbb8SVasyl Gomonovych 			pops += per_cpu_ptr(qh->stats, cpu)->pops;
449bc3acbb8SVasyl Gomonovych 			push_errors += per_cpu_ptr(qh->stats, cpu)->push_errors;
450bc3acbb8SVasyl Gomonovych 			pop_errors += per_cpu_ptr(qh->stats, cpu)->pop_errors;
451bc3acbb8SVasyl Gomonovych 			notifies += per_cpu_ptr(qh->stats, cpu)->notifies;
452bc3acbb8SVasyl Gomonovych 		}
453bc3acbb8SVasyl Gomonovych 
454bc3acbb8SVasyl Gomonovych 		seq_printf(s, "\t\thandle %p: pushes %8d, pops %8d, count %8d, notifies %8d, push errors %8d, pop errors %8d\n",
455bc3acbb8SVasyl Gomonovych 				qh,
456bc3acbb8SVasyl Gomonovych 				pushes,
457bc3acbb8SVasyl Gomonovych 				pops,
458bc3acbb8SVasyl Gomonovych 				knav_queue_get_count(qh),
459bc3acbb8SVasyl Gomonovych 				notifies,
460bc3acbb8SVasyl Gomonovych 				push_errors,
461bc3acbb8SVasyl Gomonovych 				pop_errors);
46241f93af9SSandeep Nair 	}
46341f93af9SSandeep Nair }
46441f93af9SSandeep Nair 
46541f93af9SSandeep Nair static int knav_queue_debug_show(struct seq_file *s, void *v)
46641f93af9SSandeep Nair {
46741f93af9SSandeep Nair 	struct knav_queue_inst *inst;
46841f93af9SSandeep Nair 	int idx;
46941f93af9SSandeep Nair 
47041f93af9SSandeep Nair 	mutex_lock(&knav_dev_lock);
47141f93af9SSandeep Nair 	seq_printf(s, "%s: %u-%u\n",
47241f93af9SSandeep Nair 		   dev_name(kdev->dev), kdev->base_id,
47341f93af9SSandeep Nair 		   kdev->base_id + kdev->num_queues - 1);
47441f93af9SSandeep Nair 	for_each_instance(idx, inst, kdev)
47541f93af9SSandeep Nair 		knav_queue_debug_show_instance(s, inst);
47641f93af9SSandeep Nair 	mutex_unlock(&knav_dev_lock);
47741f93af9SSandeep Nair 
47841f93af9SSandeep Nair 	return 0;
47941f93af9SSandeep Nair }
48041f93af9SSandeep Nair 
48174e0e43aSQinglang Miao DEFINE_SHOW_ATTRIBUTE(knav_queue_debug);
48241f93af9SSandeep Nair 
48341f93af9SSandeep Nair static inline int knav_queue_pdsp_wait(u32 * __iomem addr, unsigned timeout,
48441f93af9SSandeep Nair 					u32 flags)
48541f93af9SSandeep Nair {
48641f93af9SSandeep Nair 	unsigned long end;
48741f93af9SSandeep Nair 	u32 val = 0;
48841f93af9SSandeep Nair 
48941f93af9SSandeep Nair 	end = jiffies + msecs_to_jiffies(timeout);
49041f93af9SSandeep Nair 	while (time_after(end, jiffies)) {
49141f93af9SSandeep Nair 		val = readl_relaxed(addr);
49241f93af9SSandeep Nair 		if (flags)
49341f93af9SSandeep Nair 			val &= flags;
49441f93af9SSandeep Nair 		if (!val)
49541f93af9SSandeep Nair 			break;
49641f93af9SSandeep Nair 		cpu_relax();
49741f93af9SSandeep Nair 	}
49841f93af9SSandeep Nair 	return val ? -ETIMEDOUT : 0;
49941f93af9SSandeep Nair }
50041f93af9SSandeep Nair 
50141f93af9SSandeep Nair 
50241f93af9SSandeep Nair static int knav_queue_flush(struct knav_queue *qh)
50341f93af9SSandeep Nair {
50441f93af9SSandeep Nair 	struct knav_queue_inst *inst = qh->inst;
50541f93af9SSandeep Nair 	unsigned id = inst->id - inst->qmgr->start_queue;
50641f93af9SSandeep Nair 
50741f93af9SSandeep Nair 	atomic_set(&inst->desc_count, 0);
50841f93af9SSandeep Nair 	writel_relaxed(0, &inst->qmgr->reg_push[id].ptr_size_thresh);
50941f93af9SSandeep Nair 	return 0;
51041f93af9SSandeep Nair }
51141f93af9SSandeep Nair 
51241f93af9SSandeep Nair /**
51341f93af9SSandeep Nair  * knav_queue_open()	- open a hardware queue
514e8ebf411SLee Jones  * @name:		- name to give the queue handle
515e8ebf411SLee Jones  * @id:			- desired queue number if any or specifes the type
51641f93af9SSandeep Nair  *			  of queue
517e8ebf411SLee Jones  * @flags:		- the following flags are applicable to queues:
51841f93af9SSandeep Nair  *	KNAV_QUEUE_SHARED - allow the queue to be shared. Queues are
51941f93af9SSandeep Nair  *			     exclusive by default.
52041f93af9SSandeep Nair  *			     Subsequent attempts to open a shared queue should
52141f93af9SSandeep Nair  *			     also have this flag.
52241f93af9SSandeep Nair  *
52341f93af9SSandeep Nair  * Returns a handle to the open hardware queue if successful. Use IS_ERR()
52441f93af9SSandeep Nair  * to check the returned value for error codes.
52541f93af9SSandeep Nair  */
52641f93af9SSandeep Nair void *knav_queue_open(const char *name, unsigned id,
52741f93af9SSandeep Nair 					unsigned flags)
52841f93af9SSandeep Nair {
52941f93af9SSandeep Nair 	struct knav_queue *qh = ERR_PTR(-EINVAL);
53041f93af9SSandeep Nair 
53141f93af9SSandeep Nair 	switch (id) {
53241f93af9SSandeep Nair 	case KNAV_QUEUE_QPEND:
53341f93af9SSandeep Nair 	case KNAV_QUEUE_ACC:
53441f93af9SSandeep Nair 	case KNAV_QUEUE_GP:
53541f93af9SSandeep Nair 		qh = knav_queue_open_by_type(name, id, flags);
53641f93af9SSandeep Nair 		break;
53741f93af9SSandeep Nair 
53841f93af9SSandeep Nair 	default:
53941f93af9SSandeep Nair 		qh = knav_queue_open_by_id(name, id, flags);
54041f93af9SSandeep Nair 		break;
54141f93af9SSandeep Nair 	}
54241f93af9SSandeep Nair 	return qh;
54341f93af9SSandeep Nair }
54441f93af9SSandeep Nair EXPORT_SYMBOL_GPL(knav_queue_open);
54541f93af9SSandeep Nair 
54641f93af9SSandeep Nair /**
54741f93af9SSandeep Nair  * knav_queue_close()	- close a hardware queue handle
548e8ebf411SLee Jones  * @qhandle:		- handle to close
54941f93af9SSandeep Nair  */
55041f93af9SSandeep Nair void knav_queue_close(void *qhandle)
55141f93af9SSandeep Nair {
55241f93af9SSandeep Nair 	struct knav_queue *qh = qhandle;
55341f93af9SSandeep Nair 	struct knav_queue_inst *inst = qh->inst;
55441f93af9SSandeep Nair 
55541f93af9SSandeep Nair 	while (atomic_read(&qh->notifier_enabled) > 0)
55641f93af9SSandeep Nair 		knav_queue_disable_notifier(qh);
55741f93af9SSandeep Nair 
55841f93af9SSandeep Nair 	mutex_lock(&knav_dev_lock);
55941f93af9SSandeep Nair 	list_del_rcu(&qh->list);
56041f93af9SSandeep Nair 	mutex_unlock(&knav_dev_lock);
56141f93af9SSandeep Nair 	synchronize_rcu();
56241f93af9SSandeep Nair 	if (!knav_queue_is_busy(inst)) {
56341f93af9SSandeep Nair 		struct knav_range_info *range = inst->range;
56441f93af9SSandeep Nair 
56541f93af9SSandeep Nair 		if (range->ops && range->ops->close_queue)
56641f93af9SSandeep Nair 			range->ops->close_queue(range, inst);
56741f93af9SSandeep Nair 	}
568bc3acbb8SVasyl Gomonovych 	free_percpu(qh->stats);
56941f93af9SSandeep Nair 	devm_kfree(inst->kdev->dev, qh);
57041f93af9SSandeep Nair }
57141f93af9SSandeep Nair EXPORT_SYMBOL_GPL(knav_queue_close);
57241f93af9SSandeep Nair 
57341f93af9SSandeep Nair /**
57441f93af9SSandeep Nair  * knav_queue_device_control()	- Perform control operations on a queue
575e8ebf411SLee Jones  * @qhandle:			- queue handle
576e8ebf411SLee Jones  * @cmd:			- control commands
577e8ebf411SLee Jones  * @arg:			- command argument
57841f93af9SSandeep Nair  *
57941f93af9SSandeep Nair  * Returns 0 on success, errno otherwise.
58041f93af9SSandeep Nair  */
58141f93af9SSandeep Nair int knav_queue_device_control(void *qhandle, enum knav_queue_ctrl_cmd cmd,
58241f93af9SSandeep Nair 				unsigned long arg)
58341f93af9SSandeep Nair {
58441f93af9SSandeep Nair 	struct knav_queue *qh = qhandle;
58541f93af9SSandeep Nair 	struct knav_queue_notify_config *cfg;
58641f93af9SSandeep Nair 	int ret;
58741f93af9SSandeep Nair 
58841f93af9SSandeep Nair 	switch ((int)cmd) {
58941f93af9SSandeep Nair 	case KNAV_QUEUE_GET_ID:
59041f93af9SSandeep Nair 		ret = qh->inst->kdev->base_id + qh->inst->id;
59141f93af9SSandeep Nair 		break;
59241f93af9SSandeep Nair 
59341f93af9SSandeep Nair 	case KNAV_QUEUE_FLUSH:
59441f93af9SSandeep Nair 		ret = knav_queue_flush(qh);
59541f93af9SSandeep Nair 		break;
59641f93af9SSandeep Nair 
59741f93af9SSandeep Nair 	case KNAV_QUEUE_SET_NOTIFIER:
59841f93af9SSandeep Nair 		cfg = (void *)arg;
59941f93af9SSandeep Nair 		ret = knav_queue_set_notifier(qh, cfg);
60041f93af9SSandeep Nair 		break;
60141f93af9SSandeep Nair 
60241f93af9SSandeep Nair 	case KNAV_QUEUE_ENABLE_NOTIFY:
60341f93af9SSandeep Nair 		ret = knav_queue_enable_notifier(qh);
60441f93af9SSandeep Nair 		break;
60541f93af9SSandeep Nair 
60641f93af9SSandeep Nair 	case KNAV_QUEUE_DISABLE_NOTIFY:
60741f93af9SSandeep Nair 		ret = knav_queue_disable_notifier(qh);
60841f93af9SSandeep Nair 		break;
60941f93af9SSandeep Nair 
61041f93af9SSandeep Nair 	case KNAV_QUEUE_GET_COUNT:
61141f93af9SSandeep Nair 		ret = knav_queue_get_count(qh);
61241f93af9SSandeep Nair 		break;
61341f93af9SSandeep Nair 
61441f93af9SSandeep Nair 	default:
61541f93af9SSandeep Nair 		ret = -ENOTSUPP;
61641f93af9SSandeep Nair 		break;
61741f93af9SSandeep Nair 	}
61841f93af9SSandeep Nair 	return ret;
61941f93af9SSandeep Nair }
62041f93af9SSandeep Nair EXPORT_SYMBOL_GPL(knav_queue_device_control);
62141f93af9SSandeep Nair 
62241f93af9SSandeep Nair 
62341f93af9SSandeep Nair 
62441f93af9SSandeep Nair /**
62541f93af9SSandeep Nair  * knav_queue_push()	- push data (or descriptor) to the tail of a queue
626e8ebf411SLee Jones  * @qhandle:		- hardware queue handle
627e8ebf411SLee Jones  * @dma:		- DMA data to push
628e8ebf411SLee Jones  * @size:		- size of data to push
629e8ebf411SLee Jones  * @flags:		- can be used to pass additional information
63041f93af9SSandeep Nair  *
63141f93af9SSandeep Nair  * Returns 0 on success, errno otherwise.
63241f93af9SSandeep Nair  */
63341f93af9SSandeep Nair int knav_queue_push(void *qhandle, dma_addr_t dma,
63441f93af9SSandeep Nair 					unsigned size, unsigned flags)
63541f93af9SSandeep Nair {
63641f93af9SSandeep Nair 	struct knav_queue *qh = qhandle;
63741f93af9SSandeep Nair 	u32 val;
63841f93af9SSandeep Nair 
63941f93af9SSandeep Nair 	val = (u32)dma | ((size / 16) - 1);
64041f93af9SSandeep Nair 	writel_relaxed(val, &qh->reg_push[0].ptr_size_thresh);
64141f93af9SSandeep Nair 
642bc3acbb8SVasyl Gomonovych 	this_cpu_inc(qh->stats->pushes);
64341f93af9SSandeep Nair 	return 0;
64441f93af9SSandeep Nair }
64597f39af4SMurali Karicheri EXPORT_SYMBOL_GPL(knav_queue_push);
64641f93af9SSandeep Nair 
64741f93af9SSandeep Nair /**
64841f93af9SSandeep Nair  * knav_queue_pop()	- pop data (or descriptor) from the head of a queue
649e8ebf411SLee Jones  * @qhandle:		- hardware queue handle
650e8ebf411SLee Jones  * @size:		- (optional) size of the data pop'ed.
65141f93af9SSandeep Nair  *
65241f93af9SSandeep Nair  * Returns a DMA address on success, 0 on failure.
65341f93af9SSandeep Nair  */
65441f93af9SSandeep Nair dma_addr_t knav_queue_pop(void *qhandle, unsigned *size)
65541f93af9SSandeep Nair {
65641f93af9SSandeep Nair 	struct knav_queue *qh = qhandle;
65741f93af9SSandeep Nair 	struct knav_queue_inst *inst = qh->inst;
65841f93af9SSandeep Nair 	dma_addr_t dma;
65941f93af9SSandeep Nair 	u32 val, idx;
66041f93af9SSandeep Nair 
66141f93af9SSandeep Nair 	/* are we accumulated? */
66241f93af9SSandeep Nair 	if (inst->descs) {
66341f93af9SSandeep Nair 		if (unlikely(atomic_dec_return(&inst->desc_count) < 0)) {
66441f93af9SSandeep Nair 			atomic_inc(&inst->desc_count);
66541f93af9SSandeep Nair 			return 0;
66641f93af9SSandeep Nair 		}
66741f93af9SSandeep Nair 		idx  = atomic_inc_return(&inst->desc_head);
66841f93af9SSandeep Nair 		idx &= ACC_DESCS_MASK;
66941f93af9SSandeep Nair 		val = inst->descs[idx];
67041f93af9SSandeep Nair 	} else {
67141f93af9SSandeep Nair 		val = readl_relaxed(&qh->reg_pop[0].ptr_size_thresh);
67241f93af9SSandeep Nair 		if (unlikely(!val))
67341f93af9SSandeep Nair 			return 0;
67441f93af9SSandeep Nair 	}
67541f93af9SSandeep Nair 
67641f93af9SSandeep Nair 	dma = val & DESC_PTR_MASK;
67741f93af9SSandeep Nair 	if (size)
67841f93af9SSandeep Nair 		*size = ((val & DESC_SIZE_MASK) + 1) * 16;
67941f93af9SSandeep Nair 
680bc3acbb8SVasyl Gomonovych 	this_cpu_inc(qh->stats->pops);
68141f93af9SSandeep Nair 	return dma;
68241f93af9SSandeep Nair }
68397f39af4SMurali Karicheri EXPORT_SYMBOL_GPL(knav_queue_pop);
68441f93af9SSandeep Nair 
68541f93af9SSandeep Nair /* carve out descriptors and push into queue */
68641f93af9SSandeep Nair static void kdesc_fill_pool(struct knav_pool *pool)
68741f93af9SSandeep Nair {
68841f93af9SSandeep Nair 	struct knav_region *region;
68941f93af9SSandeep Nair 	int i;
69041f93af9SSandeep Nair 
69141f93af9SSandeep Nair 	region = pool->region;
69241f93af9SSandeep Nair 	pool->desc_size = region->desc_size;
69341f93af9SSandeep Nair 	for (i = 0; i < pool->num_desc; i++) {
69441f93af9SSandeep Nair 		int index = pool->region_offset + i;
69541f93af9SSandeep Nair 		dma_addr_t dma_addr;
69641f93af9SSandeep Nair 		unsigned dma_size;
69741f93af9SSandeep Nair 		dma_addr = region->dma_start + (region->desc_size * index);
69841f93af9SSandeep Nair 		dma_size = ALIGN(pool->desc_size, SMP_CACHE_BYTES);
69941f93af9SSandeep Nair 		dma_sync_single_for_device(pool->dev, dma_addr, dma_size,
70041f93af9SSandeep Nair 					   DMA_TO_DEVICE);
70141f93af9SSandeep Nair 		knav_queue_push(pool->queue, dma_addr, dma_size, 0);
70241f93af9SSandeep Nair 	}
70341f93af9SSandeep Nair }
70441f93af9SSandeep Nair 
70541f93af9SSandeep Nair /* pop out descriptors and close the queue */
70641f93af9SSandeep Nair static void kdesc_empty_pool(struct knav_pool *pool)
70741f93af9SSandeep Nair {
70841f93af9SSandeep Nair 	dma_addr_t dma;
70941f93af9SSandeep Nair 	unsigned size;
71041f93af9SSandeep Nair 	void *desc;
71141f93af9SSandeep Nair 	int i;
71241f93af9SSandeep Nair 
71341f93af9SSandeep Nair 	if (!pool->queue)
71441f93af9SSandeep Nair 		return;
71541f93af9SSandeep Nair 
71641f93af9SSandeep Nair 	for (i = 0;; i++) {
71741f93af9SSandeep Nair 		dma = knav_queue_pop(pool->queue, &size);
71841f93af9SSandeep Nair 		if (!dma)
71941f93af9SSandeep Nair 			break;
72041f93af9SSandeep Nair 		desc = knav_pool_desc_dma_to_virt(pool, dma);
72141f93af9SSandeep Nair 		if (!desc) {
72241f93af9SSandeep Nair 			dev_dbg(pool->kdev->dev,
72341f93af9SSandeep Nair 				"couldn't unmap desc, continuing\n");
72441f93af9SSandeep Nair 			continue;
72541f93af9SSandeep Nair 		}
72641f93af9SSandeep Nair 	}
72741f93af9SSandeep Nair 	WARN_ON(i != pool->num_desc);
72841f93af9SSandeep Nair 	knav_queue_close(pool->queue);
72941f93af9SSandeep Nair }
73041f93af9SSandeep Nair 
73141f93af9SSandeep Nair 
73241f93af9SSandeep Nair /* Get the DMA address of a descriptor */
73341f93af9SSandeep Nair dma_addr_t knav_pool_desc_virt_to_dma(void *ph, void *virt)
73441f93af9SSandeep Nair {
73541f93af9SSandeep Nair 	struct knav_pool *pool = ph;
73641f93af9SSandeep Nair 	return pool->region->dma_start + (virt - pool->region->virt_start);
73741f93af9SSandeep Nair }
73897f39af4SMurali Karicheri EXPORT_SYMBOL_GPL(knav_pool_desc_virt_to_dma);
73941f93af9SSandeep Nair 
74041f93af9SSandeep Nair void *knav_pool_desc_dma_to_virt(void *ph, dma_addr_t dma)
74141f93af9SSandeep Nair {
74241f93af9SSandeep Nair 	struct knav_pool *pool = ph;
74341f93af9SSandeep Nair 	return pool->region->virt_start + (dma - pool->region->dma_start);
74441f93af9SSandeep Nair }
74597f39af4SMurali Karicheri EXPORT_SYMBOL_GPL(knav_pool_desc_dma_to_virt);
74641f93af9SSandeep Nair 
74741f93af9SSandeep Nair /**
74841f93af9SSandeep Nair  * knav_pool_create()	- Create a pool of descriptors
749e8ebf411SLee Jones  * @name:		- name to give the pool handle
750e8ebf411SLee Jones  * @num_desc:		- numbers of descriptors in the pool
751e8ebf411SLee Jones  * @region_id:		- QMSS region id from which the descriptors are to be
75241f93af9SSandeep Nair  *			  allocated.
75341f93af9SSandeep Nair  *
75441f93af9SSandeep Nair  * Returns a pool handle on success.
75541f93af9SSandeep Nair  * Use IS_ERR_OR_NULL() to identify error values on return.
75641f93af9SSandeep Nair  */
75741f93af9SSandeep Nair void *knav_pool_create(const char *name,
75841f93af9SSandeep Nair 					int num_desc, int region_id)
75941f93af9SSandeep Nair {
76041f93af9SSandeep Nair 	struct knav_region *reg_itr, *region = NULL;
76141f93af9SSandeep Nair 	struct knav_pool *pool, *pi;
76241f93af9SSandeep Nair 	struct list_head *node;
76341f93af9SSandeep Nair 	unsigned last_offset;
76441f93af9SSandeep Nair 	bool slot_found;
76541f93af9SSandeep Nair 	int ret;
76641f93af9SSandeep Nair 
7674459398bSKeerthy 	if (!kdev)
7684459398bSKeerthy 		return ERR_PTR(-EPROBE_DEFER);
7694459398bSKeerthy 
77041f93af9SSandeep Nair 	if (!kdev->dev)
77141f93af9SSandeep Nair 		return ERR_PTR(-ENODEV);
77241f93af9SSandeep Nair 
77341f93af9SSandeep Nair 	pool = devm_kzalloc(kdev->dev, sizeof(*pool), GFP_KERNEL);
77441f93af9SSandeep Nair 	if (!pool) {
77541f93af9SSandeep Nair 		dev_err(kdev->dev, "out of memory allocating pool\n");
77641f93af9SSandeep Nair 		return ERR_PTR(-ENOMEM);
77741f93af9SSandeep Nair 	}
77841f93af9SSandeep Nair 
77941f93af9SSandeep Nair 	for_each_region(kdev, reg_itr) {
78041f93af9SSandeep Nair 		if (reg_itr->id != region_id)
78141f93af9SSandeep Nair 			continue;
78241f93af9SSandeep Nair 		region = reg_itr;
78341f93af9SSandeep Nair 		break;
78441f93af9SSandeep Nair 	}
78541f93af9SSandeep Nair 
78641f93af9SSandeep Nair 	if (!region) {
78741f93af9SSandeep Nair 		dev_err(kdev->dev, "region-id(%d) not found\n", region_id);
78841f93af9SSandeep Nair 		ret = -EINVAL;
78941f93af9SSandeep Nair 		goto err;
79041f93af9SSandeep Nair 	}
79141f93af9SSandeep Nair 
79241f93af9SSandeep Nair 	pool->queue = knav_queue_open(name, KNAV_QUEUE_GP, 0);
79341f93af9SSandeep Nair 	if (IS_ERR_OR_NULL(pool->queue)) {
79441f93af9SSandeep Nair 		dev_err(kdev->dev,
79541f93af9SSandeep Nair 			"failed to open queue for pool(%s), error %ld\n",
79641f93af9SSandeep Nair 			name, PTR_ERR(pool->queue));
79741f93af9SSandeep Nair 		ret = PTR_ERR(pool->queue);
79841f93af9SSandeep Nair 		goto err;
79941f93af9SSandeep Nair 	}
80041f93af9SSandeep Nair 
801aefc5818SMa Shimiao 	pool->name = kstrndup(name, KNAV_NAME_SIZE - 1, GFP_KERNEL);
80241f93af9SSandeep Nair 	pool->kdev = kdev;
80341f93af9SSandeep Nair 	pool->dev = kdev->dev;
80441f93af9SSandeep Nair 
80541f93af9SSandeep Nair 	mutex_lock(&knav_dev_lock);
80641f93af9SSandeep Nair 
80741f93af9SSandeep Nair 	if (num_desc > (region->num_desc - region->used_desc)) {
80841f93af9SSandeep Nair 		dev_err(kdev->dev, "out of descs in region(%d) for pool(%s)\n",
80941f93af9SSandeep Nair 			region_id, name);
81041f93af9SSandeep Nair 		ret = -ENOMEM;
811ea6d4c07SAxel Lin 		goto err_unlock;
81241f93af9SSandeep Nair 	}
81341f93af9SSandeep Nair 
81441f93af9SSandeep Nair 	/* Region maintains a sorted (by region offset) list of pools
81541f93af9SSandeep Nair 	 * use the first free slot which is large enough to accomodate
81641f93af9SSandeep Nair 	 * the request
81741f93af9SSandeep Nair 	 */
81841f93af9SSandeep Nair 	last_offset = 0;
81941f93af9SSandeep Nair 	slot_found = false;
82041f93af9SSandeep Nair 	node = &region->pools;
82141f93af9SSandeep Nair 	list_for_each_entry(pi, &region->pools, region_inst) {
82241f93af9SSandeep Nair 		if ((pi->region_offset - last_offset) >= num_desc) {
82341f93af9SSandeep Nair 			slot_found = true;
82441f93af9SSandeep Nair 			break;
82541f93af9SSandeep Nair 		}
82641f93af9SSandeep Nair 		last_offset = pi->region_offset + pi->num_desc;
82741f93af9SSandeep Nair 	}
82841f93af9SSandeep Nair 	node = &pi->region_inst;
82941f93af9SSandeep Nair 
83041f93af9SSandeep Nair 	if (slot_found) {
83141f93af9SSandeep Nair 		pool->region = region;
83241f93af9SSandeep Nair 		pool->num_desc = num_desc;
83341f93af9SSandeep Nair 		pool->region_offset = last_offset;
83441f93af9SSandeep Nair 		region->used_desc += num_desc;
83541f93af9SSandeep Nair 		list_add_tail(&pool->list, &kdev->pools);
83641f93af9SSandeep Nair 		list_add_tail(&pool->region_inst, node);
83741f93af9SSandeep Nair 	} else {
83841f93af9SSandeep Nair 		dev_err(kdev->dev, "pool(%s) create failed: fragmented desc pool in region(%d)\n",
83941f93af9SSandeep Nair 			name, region_id);
84041f93af9SSandeep Nair 		ret = -ENOMEM;
841ea6d4c07SAxel Lin 		goto err_unlock;
84241f93af9SSandeep Nair 	}
84341f93af9SSandeep Nair 
84441f93af9SSandeep Nair 	mutex_unlock(&knav_dev_lock);
84541f93af9SSandeep Nair 	kdesc_fill_pool(pool);
84641f93af9SSandeep Nair 	return pool;
84741f93af9SSandeep Nair 
848ea6d4c07SAxel Lin err_unlock:
84941f93af9SSandeep Nair 	mutex_unlock(&knav_dev_lock);
850ea6d4c07SAxel Lin err:
85141f93af9SSandeep Nair 	kfree(pool->name);
85241f93af9SSandeep Nair 	devm_kfree(kdev->dev, pool);
85341f93af9SSandeep Nair 	return ERR_PTR(ret);
85441f93af9SSandeep Nair }
85541f93af9SSandeep Nair EXPORT_SYMBOL_GPL(knav_pool_create);
85641f93af9SSandeep Nair 
85741f93af9SSandeep Nair /**
85841f93af9SSandeep Nair  * knav_pool_destroy()	- Free a pool of descriptors
859e8ebf411SLee Jones  * @ph:		- pool handle
86041f93af9SSandeep Nair  */
86141f93af9SSandeep Nair void knav_pool_destroy(void *ph)
86241f93af9SSandeep Nair {
86341f93af9SSandeep Nair 	struct knav_pool *pool = ph;
86441f93af9SSandeep Nair 
86541f93af9SSandeep Nair 	if (!pool)
86641f93af9SSandeep Nair 		return;
86741f93af9SSandeep Nair 
86841f93af9SSandeep Nair 	if (!pool->region)
86941f93af9SSandeep Nair 		return;
87041f93af9SSandeep Nair 
87141f93af9SSandeep Nair 	kdesc_empty_pool(pool);
87241f93af9SSandeep Nair 	mutex_lock(&knav_dev_lock);
87341f93af9SSandeep Nair 
87441f93af9SSandeep Nair 	pool->region->used_desc -= pool->num_desc;
87541f93af9SSandeep Nair 	list_del(&pool->region_inst);
87641f93af9SSandeep Nair 	list_del(&pool->list);
87741f93af9SSandeep Nair 
87841f93af9SSandeep Nair 	mutex_unlock(&knav_dev_lock);
87941f93af9SSandeep Nair 	kfree(pool->name);
88041f93af9SSandeep Nair 	devm_kfree(kdev->dev, pool);
88141f93af9SSandeep Nair }
88241f93af9SSandeep Nair EXPORT_SYMBOL_GPL(knav_pool_destroy);
88341f93af9SSandeep Nair 
88441f93af9SSandeep Nair 
88541f93af9SSandeep Nair /**
88641f93af9SSandeep Nair  * knav_pool_desc_get()	- Get a descriptor from the pool
887e8ebf411SLee Jones  * @ph:		- pool handle
88841f93af9SSandeep Nair  *
88941f93af9SSandeep Nair  * Returns descriptor from the pool.
89041f93af9SSandeep Nair  */
89141f93af9SSandeep Nair void *knav_pool_desc_get(void *ph)
89241f93af9SSandeep Nair {
89341f93af9SSandeep Nair 	struct knav_pool *pool = ph;
89441f93af9SSandeep Nair 	dma_addr_t dma;
89541f93af9SSandeep Nair 	unsigned size;
89641f93af9SSandeep Nair 	void *data;
89741f93af9SSandeep Nair 
89841f93af9SSandeep Nair 	dma = knav_queue_pop(pool->queue, &size);
89941f93af9SSandeep Nair 	if (unlikely(!dma))
90041f93af9SSandeep Nair 		return ERR_PTR(-ENOMEM);
90141f93af9SSandeep Nair 	data = knav_pool_desc_dma_to_virt(pool, dma);
90241f93af9SSandeep Nair 	return data;
90341f93af9SSandeep Nair }
90497f39af4SMurali Karicheri EXPORT_SYMBOL_GPL(knav_pool_desc_get);
90541f93af9SSandeep Nair 
90641f93af9SSandeep Nair /**
90741f93af9SSandeep Nair  * knav_pool_desc_put()	- return a descriptor to the pool
908e8ebf411SLee Jones  * @ph:		- pool handle
909e8ebf411SLee Jones  * @desc:	- virtual address
91041f93af9SSandeep Nair  */
91141f93af9SSandeep Nair void knav_pool_desc_put(void *ph, void *desc)
91241f93af9SSandeep Nair {
91341f93af9SSandeep Nair 	struct knav_pool *pool = ph;
91441f93af9SSandeep Nair 	dma_addr_t dma;
91541f93af9SSandeep Nair 	dma = knav_pool_desc_virt_to_dma(pool, desc);
91641f93af9SSandeep Nair 	knav_queue_push(pool->queue, dma, pool->region->desc_size, 0);
91741f93af9SSandeep Nair }
91897f39af4SMurali Karicheri EXPORT_SYMBOL_GPL(knav_pool_desc_put);
91941f93af9SSandeep Nair 
92041f93af9SSandeep Nair /**
92141f93af9SSandeep Nair  * knav_pool_desc_map()	- Map descriptor for DMA transfer
922e8ebf411SLee Jones  * @ph:				- pool handle
923e8ebf411SLee Jones  * @desc:			- address of descriptor to map
924e8ebf411SLee Jones  * @size:			- size of descriptor to map
925e8ebf411SLee Jones  * @dma:			- DMA address return pointer
926e8ebf411SLee Jones  * @dma_sz:			- adjusted return pointer
92741f93af9SSandeep Nair  *
92841f93af9SSandeep Nair  * Returns 0 on success, errno otherwise.
92941f93af9SSandeep Nair  */
93041f93af9SSandeep Nair int knav_pool_desc_map(void *ph, void *desc, unsigned size,
93141f93af9SSandeep Nair 					dma_addr_t *dma, unsigned *dma_sz)
93241f93af9SSandeep Nair {
93341f93af9SSandeep Nair 	struct knav_pool *pool = ph;
93441f93af9SSandeep Nair 	*dma = knav_pool_desc_virt_to_dma(pool, desc);
93541f93af9SSandeep Nair 	size = min(size, pool->region->desc_size);
93641f93af9SSandeep Nair 	size = ALIGN(size, SMP_CACHE_BYTES);
93741f93af9SSandeep Nair 	*dma_sz = size;
93841f93af9SSandeep Nair 	dma_sync_single_for_device(pool->dev, *dma, size, DMA_TO_DEVICE);
93941f93af9SSandeep Nair 
94041f93af9SSandeep Nair 	/* Ensure the descriptor reaches to the memory */
94141f93af9SSandeep Nair 	__iowmb();
94241f93af9SSandeep Nair 
94341f93af9SSandeep Nair 	return 0;
94441f93af9SSandeep Nair }
94597f39af4SMurali Karicheri EXPORT_SYMBOL_GPL(knav_pool_desc_map);
94641f93af9SSandeep Nair 
94741f93af9SSandeep Nair /**
94841f93af9SSandeep Nair  * knav_pool_desc_unmap()	- Unmap descriptor after DMA transfer
949e8ebf411SLee Jones  * @ph:				- pool handle
950e8ebf411SLee Jones  * @dma:			- DMA address of descriptor to unmap
951e8ebf411SLee Jones  * @dma_sz:			- size of descriptor to unmap
95241f93af9SSandeep Nair  *
95341f93af9SSandeep Nair  * Returns descriptor address on success, Use IS_ERR_OR_NULL() to identify
95441f93af9SSandeep Nair  * error values on return.
95541f93af9SSandeep Nair  */
95641f93af9SSandeep Nair void *knav_pool_desc_unmap(void *ph, dma_addr_t dma, unsigned dma_sz)
95741f93af9SSandeep Nair {
95841f93af9SSandeep Nair 	struct knav_pool *pool = ph;
95941f93af9SSandeep Nair 	unsigned desc_sz;
96041f93af9SSandeep Nair 	void *desc;
96141f93af9SSandeep Nair 
96241f93af9SSandeep Nair 	desc_sz = min(dma_sz, pool->region->desc_size);
96341f93af9SSandeep Nair 	desc = knav_pool_desc_dma_to_virt(pool, dma);
96441f93af9SSandeep Nair 	dma_sync_single_for_cpu(pool->dev, dma, desc_sz, DMA_FROM_DEVICE);
96541f93af9SSandeep Nair 	prefetch(desc);
96641f93af9SSandeep Nair 	return desc;
96741f93af9SSandeep Nair }
96897f39af4SMurali Karicheri EXPORT_SYMBOL_GPL(knav_pool_desc_unmap);
96941f93af9SSandeep Nair 
97041f93af9SSandeep Nair /**
97141f93af9SSandeep Nair  * knav_pool_count()	- Get the number of descriptors in pool.
972e8ebf411SLee Jones  * @ph:			- pool handle
97341f93af9SSandeep Nair  * Returns number of elements in the pool.
97441f93af9SSandeep Nair  */
97541f93af9SSandeep Nair int knav_pool_count(void *ph)
97641f93af9SSandeep Nair {
97741f93af9SSandeep Nair 	struct knav_pool *pool = ph;
97841f93af9SSandeep Nair 	return knav_queue_get_count(pool->queue);
97941f93af9SSandeep Nair }
98097f39af4SMurali Karicheri EXPORT_SYMBOL_GPL(knav_pool_count);
98141f93af9SSandeep Nair 
98241f93af9SSandeep Nair static void knav_queue_setup_region(struct knav_device *kdev,
98341f93af9SSandeep Nair 					struct knav_region *region)
98441f93af9SSandeep Nair {
98541f93af9SSandeep Nair 	unsigned hw_num_desc, hw_desc_size, size;
98641f93af9SSandeep Nair 	struct knav_reg_region __iomem  *regs;
98741f93af9SSandeep Nair 	struct knav_qmgr_info *qmgr;
98841f93af9SSandeep Nair 	struct knav_pool *pool;
98941f93af9SSandeep Nair 	int id = region->id;
99041f93af9SSandeep Nair 	struct page *page;
99141f93af9SSandeep Nair 
99241f93af9SSandeep Nair 	/* unused region? */
99341f93af9SSandeep Nair 	if (!region->num_desc) {
99441f93af9SSandeep Nair 		dev_warn(kdev->dev, "unused region %s\n", region->name);
99541f93af9SSandeep Nair 		return;
99641f93af9SSandeep Nair 	}
99741f93af9SSandeep Nair 
99841f93af9SSandeep Nair 	/* get hardware descriptor value */
99941f93af9SSandeep Nair 	hw_num_desc = ilog2(region->num_desc - 1) + 1;
100041f93af9SSandeep Nair 
100141f93af9SSandeep Nair 	/* did we force fit ourselves into nothingness? */
100241f93af9SSandeep Nair 	if (region->num_desc < 32) {
100341f93af9SSandeep Nair 		region->num_desc = 0;
100441f93af9SSandeep Nair 		dev_warn(kdev->dev, "too few descriptors in region %s\n",
100541f93af9SSandeep Nair 			 region->name);
100641f93af9SSandeep Nair 		return;
100741f93af9SSandeep Nair 	}
100841f93af9SSandeep Nair 
100941f93af9SSandeep Nair 	size = region->num_desc * region->desc_size;
101041f93af9SSandeep Nair 	region->virt_start = alloc_pages_exact(size, GFP_KERNEL | GFP_DMA |
101141f93af9SSandeep Nair 						GFP_DMA32);
101241f93af9SSandeep Nair 	if (!region->virt_start) {
101341f93af9SSandeep Nair 		region->num_desc = 0;
101441f93af9SSandeep Nair 		dev_err(kdev->dev, "memory alloc failed for region %s\n",
101541f93af9SSandeep Nair 			region->name);
101641f93af9SSandeep Nair 		return;
101741f93af9SSandeep Nair 	}
101841f93af9SSandeep Nair 	region->virt_end = region->virt_start + size;
101941f93af9SSandeep Nair 	page = virt_to_page(region->virt_start);
102041f93af9SSandeep Nair 
102141f93af9SSandeep Nair 	region->dma_start = dma_map_page(kdev->dev, page, 0, size,
102241f93af9SSandeep Nair 					 DMA_BIDIRECTIONAL);
102341f93af9SSandeep Nair 	if (dma_mapping_error(kdev->dev, region->dma_start)) {
102441f93af9SSandeep Nair 		dev_err(kdev->dev, "dma map failed for region %s\n",
102541f93af9SSandeep Nair 			region->name);
102641f93af9SSandeep Nair 		goto fail;
102741f93af9SSandeep Nair 	}
102841f93af9SSandeep Nair 	region->dma_end = region->dma_start + size;
102941f93af9SSandeep Nair 
103041f93af9SSandeep Nair 	pool = devm_kzalloc(kdev->dev, sizeof(*pool), GFP_KERNEL);
103141f93af9SSandeep Nair 	if (!pool) {
103241f93af9SSandeep Nair 		dev_err(kdev->dev, "out of memory allocating dummy pool\n");
103341f93af9SSandeep Nair 		goto fail;
103441f93af9SSandeep Nair 	}
103541f93af9SSandeep Nair 	pool->num_desc = 0;
103641f93af9SSandeep Nair 	pool->region_offset = region->num_desc;
103741f93af9SSandeep Nair 	list_add(&pool->region_inst, &region->pools);
103841f93af9SSandeep Nair 
103941f93af9SSandeep Nair 	dev_dbg(kdev->dev,
1040cc0336ecSArnd Bergmann 		"region %s (%d): size:%d, link:%d@%d, dma:%pad-%pad, virt:%p-%p\n",
104141f93af9SSandeep Nair 		region->name, id, region->desc_size, region->num_desc,
1042cc0336ecSArnd Bergmann 		region->link_index, &region->dma_start, &region->dma_end,
104341f93af9SSandeep Nair 		region->virt_start, region->virt_end);
104441f93af9SSandeep Nair 
104541f93af9SSandeep Nair 	hw_desc_size = (region->desc_size / 16) - 1;
104641f93af9SSandeep Nair 	hw_num_desc -= 5;
104741f93af9SSandeep Nair 
104841f93af9SSandeep Nair 	for_each_qmgr(kdev, qmgr) {
104941f93af9SSandeep Nair 		regs = qmgr->reg_region + id;
1050cc0336ecSArnd Bergmann 		writel_relaxed((u32)region->dma_start, &regs->base);
105141f93af9SSandeep Nair 		writel_relaxed(region->link_index, &regs->start_index);
105241f93af9SSandeep Nair 		writel_relaxed(hw_desc_size << 16 | hw_num_desc,
105341f93af9SSandeep Nair 			       &regs->size_count);
105441f93af9SSandeep Nair 	}
105541f93af9SSandeep Nair 	return;
105641f93af9SSandeep Nair 
105741f93af9SSandeep Nair fail:
105841f93af9SSandeep Nair 	if (region->dma_start)
105941f93af9SSandeep Nair 		dma_unmap_page(kdev->dev, region->dma_start, size,
106041f93af9SSandeep Nair 				DMA_BIDIRECTIONAL);
106141f93af9SSandeep Nair 	if (region->virt_start)
106241f93af9SSandeep Nair 		free_pages_exact(region->virt_start, size);
106341f93af9SSandeep Nair 	region->num_desc = 0;
106441f93af9SSandeep Nair 	return;
106541f93af9SSandeep Nair }
106641f93af9SSandeep Nair 
106741f93af9SSandeep Nair static const char *knav_queue_find_name(struct device_node *node)
106841f93af9SSandeep Nair {
106941f93af9SSandeep Nair 	const char *name;
107041f93af9SSandeep Nair 
107141f93af9SSandeep Nair 	if (of_property_read_string(node, "label", &name) < 0)
107241f93af9SSandeep Nair 		name = node->name;
107341f93af9SSandeep Nair 	if (!name)
107441f93af9SSandeep Nair 		name = "unknown";
107541f93af9SSandeep Nair 	return name;
107641f93af9SSandeep Nair }
107741f93af9SSandeep Nair 
107841f93af9SSandeep Nair static int knav_queue_setup_regions(struct knav_device *kdev,
107941f93af9SSandeep Nair 					struct device_node *regions)
108041f93af9SSandeep Nair {
108141f93af9SSandeep Nair 	struct device *dev = kdev->dev;
108241f93af9SSandeep Nair 	struct knav_region *region;
108341f93af9SSandeep Nair 	struct device_node *child;
108441f93af9SSandeep Nair 	u32 temp[2];
108541f93af9SSandeep Nair 	int ret;
108641f93af9SSandeep Nair 
108741f93af9SSandeep Nair 	for_each_child_of_node(regions, child) {
108841f93af9SSandeep Nair 		region = devm_kzalloc(dev, sizeof(*region), GFP_KERNEL);
108941f93af9SSandeep Nair 		if (!region) {
1090*a88f66d4SVasyl Gomonovych 			of_node_put(child);
109141f93af9SSandeep Nair 			dev_err(dev, "out of memory allocating region\n");
109241f93af9SSandeep Nair 			return -ENOMEM;
109341f93af9SSandeep Nair 		}
109441f93af9SSandeep Nair 
109541f93af9SSandeep Nair 		region->name = knav_queue_find_name(child);
109641f93af9SSandeep Nair 		of_property_read_u32(child, "id", &region->id);
109741f93af9SSandeep Nair 		ret = of_property_read_u32_array(child, "region-spec", temp, 2);
109841f93af9SSandeep Nair 		if (!ret) {
109941f93af9SSandeep Nair 			region->num_desc  = temp[0];
110041f93af9SSandeep Nair 			region->desc_size = temp[1];
110141f93af9SSandeep Nair 		} else {
110241f93af9SSandeep Nair 			dev_err(dev, "invalid region info %s\n", region->name);
110341f93af9SSandeep Nair 			devm_kfree(dev, region);
110441f93af9SSandeep Nair 			continue;
110541f93af9SSandeep Nair 		}
110641f93af9SSandeep Nair 
110741f93af9SSandeep Nair 		if (!of_get_property(child, "link-index", NULL)) {
110841f93af9SSandeep Nair 			dev_err(dev, "No link info for %s\n", region->name);
110941f93af9SSandeep Nair 			devm_kfree(dev, region);
111041f93af9SSandeep Nair 			continue;
111141f93af9SSandeep Nair 		}
111241f93af9SSandeep Nair 		ret = of_property_read_u32(child, "link-index",
111341f93af9SSandeep Nair 					   &region->link_index);
111441f93af9SSandeep Nair 		if (ret) {
111541f93af9SSandeep Nair 			dev_err(dev, "link index not found for %s\n",
111641f93af9SSandeep Nair 				region->name);
111741f93af9SSandeep Nair 			devm_kfree(dev, region);
111841f93af9SSandeep Nair 			continue;
111941f93af9SSandeep Nair 		}
112041f93af9SSandeep Nair 
112141f93af9SSandeep Nair 		INIT_LIST_HEAD(&region->pools);
112241f93af9SSandeep Nair 		list_add_tail(&region->list, &kdev->regions);
112341f93af9SSandeep Nair 	}
112441f93af9SSandeep Nair 	if (list_empty(&kdev->regions)) {
112541f93af9SSandeep Nair 		dev_err(dev, "no valid region information found\n");
112641f93af9SSandeep Nair 		return -ENODEV;
112741f93af9SSandeep Nair 	}
112841f93af9SSandeep Nair 
112941f93af9SSandeep Nair 	/* Next, we run through the regions and set things up */
113041f93af9SSandeep Nair 	for_each_region(kdev, region)
113141f93af9SSandeep Nair 		knav_queue_setup_region(kdev, region);
113241f93af9SSandeep Nair 
113341f93af9SSandeep Nair 	return 0;
113441f93af9SSandeep Nair }
113541f93af9SSandeep Nair 
113641f93af9SSandeep Nair static int knav_get_link_ram(struct knav_device *kdev,
113741f93af9SSandeep Nair 				       const char *name,
113841f93af9SSandeep Nair 				       struct knav_link_ram_block *block)
113941f93af9SSandeep Nair {
114041f93af9SSandeep Nair 	struct platform_device *pdev = to_platform_device(kdev->dev);
114141f93af9SSandeep Nair 	struct device_node *node = pdev->dev.of_node;
114241f93af9SSandeep Nair 	u32 temp[2];
114341f93af9SSandeep Nair 
114441f93af9SSandeep Nair 	/*
114541f93af9SSandeep Nair 	 * Note: link ram resources are specified in "entry" sized units. In
114641f93af9SSandeep Nair 	 * reality, although entries are ~40bits in hardware, we treat them as
114741f93af9SSandeep Nair 	 * 64-bit entities here.
114841f93af9SSandeep Nair 	 *
114941f93af9SSandeep Nair 	 * For example, to specify the internal link ram for Keystone-I class
115041f93af9SSandeep Nair 	 * devices, we would set the linkram0 resource to 0x80000-0x83fff.
115141f93af9SSandeep Nair 	 *
115241f93af9SSandeep Nair 	 * This gets a bit weird when other link rams are used.  For example,
115341f93af9SSandeep Nair 	 * if the range specified is 0x0c000000-0x0c003fff (i.e., 16K entries
115441f93af9SSandeep Nair 	 * in MSMC SRAM), the actual memory used is 0x0c000000-0x0c020000,
115541f93af9SSandeep Nair 	 * which accounts for 64-bits per entry, for 16K entries.
115641f93af9SSandeep Nair 	 */
115741f93af9SSandeep Nair 	if (!of_property_read_u32_array(node, name , temp, 2)) {
115841f93af9SSandeep Nair 		if (temp[0]) {
115941f93af9SSandeep Nair 			/*
116041f93af9SSandeep Nair 			 * queue_base specified => using internal or onchip
116141f93af9SSandeep Nair 			 * link ram WARNING - we do not "reserve" this block
116241f93af9SSandeep Nair 			 */
1163cc0336ecSArnd Bergmann 			block->dma = (dma_addr_t)temp[0];
116441f93af9SSandeep Nair 			block->virt = NULL;
116541f93af9SSandeep Nair 			block->size = temp[1];
116641f93af9SSandeep Nair 		} else {
116741f93af9SSandeep Nair 			block->size = temp[1];
116841f93af9SSandeep Nair 			/* queue_base not specific => allocate requested size */
116941f93af9SSandeep Nair 			block->virt = dmam_alloc_coherent(kdev->dev,
1170cc0336ecSArnd Bergmann 						  8 * block->size, &block->dma,
117141f93af9SSandeep Nair 						  GFP_KERNEL);
117241f93af9SSandeep Nair 			if (!block->virt) {
117341f93af9SSandeep Nair 				dev_err(kdev->dev, "failed to alloc linkram\n");
117441f93af9SSandeep Nair 				return -ENOMEM;
117541f93af9SSandeep Nair 			}
117641f93af9SSandeep Nair 		}
117741f93af9SSandeep Nair 	} else {
117841f93af9SSandeep Nair 		return -ENODEV;
117941f93af9SSandeep Nair 	}
118041f93af9SSandeep Nair 	return 0;
118141f93af9SSandeep Nair }
118241f93af9SSandeep Nair 
118341f93af9SSandeep Nair static int knav_queue_setup_link_ram(struct knav_device *kdev)
118441f93af9SSandeep Nair {
118541f93af9SSandeep Nair 	struct knav_link_ram_block *block;
118641f93af9SSandeep Nair 	struct knav_qmgr_info *qmgr;
118741f93af9SSandeep Nair 
118841f93af9SSandeep Nair 	for_each_qmgr(kdev, qmgr) {
118941f93af9SSandeep Nair 		block = &kdev->link_rams[0];
1190cc0336ecSArnd Bergmann 		dev_dbg(kdev->dev, "linkram0: dma:%pad, virt:%p, size:%x\n",
1191cc0336ecSArnd Bergmann 			&block->dma, block->virt, block->size);
1192cc0336ecSArnd Bergmann 		writel_relaxed((u32)block->dma, &qmgr->reg_config->link_ram_base0);
1193350601b4SMurali Karicheri 		if (kdev->version == QMSS_66AK2G)
1194350601b4SMurali Karicheri 			writel_relaxed(block->size,
1195350601b4SMurali Karicheri 				       &qmgr->reg_config->link_ram_size0);
1196350601b4SMurali Karicheri 		else
1197350601b4SMurali Karicheri 			writel_relaxed(block->size - 1,
1198350601b4SMurali Karicheri 				       &qmgr->reg_config->link_ram_size0);
119941f93af9SSandeep Nair 		block++;
120041f93af9SSandeep Nair 		if (!block->size)
1201f7f2bccdSMichal Morawiec 			continue;
120241f93af9SSandeep Nair 
1203cc0336ecSArnd Bergmann 		dev_dbg(kdev->dev, "linkram1: dma:%pad, virt:%p, size:%x\n",
1204cc0336ecSArnd Bergmann 			&block->dma, block->virt, block->size);
1205cc0336ecSArnd Bergmann 		writel_relaxed(block->dma, &qmgr->reg_config->link_ram_base1);
120641f93af9SSandeep Nair 	}
120741f93af9SSandeep Nair 
120841f93af9SSandeep Nair 	return 0;
120941f93af9SSandeep Nair }
121041f93af9SSandeep Nair 
121141f93af9SSandeep Nair static int knav_setup_queue_range(struct knav_device *kdev,
121241f93af9SSandeep Nair 					struct device_node *node)
121341f93af9SSandeep Nair {
121441f93af9SSandeep Nair 	struct device *dev = kdev->dev;
121541f93af9SSandeep Nair 	struct knav_range_info *range;
121641f93af9SSandeep Nair 	struct knav_qmgr_info *qmgr;
121741f93af9SSandeep Nair 	u32 temp[2], start, end, id, index;
121841f93af9SSandeep Nair 	int ret, i;
121941f93af9SSandeep Nair 
122041f93af9SSandeep Nair 	range = devm_kzalloc(dev, sizeof(*range), GFP_KERNEL);
122141f93af9SSandeep Nair 	if (!range) {
122241f93af9SSandeep Nair 		dev_err(dev, "out of memory allocating range\n");
122341f93af9SSandeep Nair 		return -ENOMEM;
122441f93af9SSandeep Nair 	}
122541f93af9SSandeep Nair 
122641f93af9SSandeep Nair 	range->kdev = kdev;
122741f93af9SSandeep Nair 	range->name = knav_queue_find_name(node);
122841f93af9SSandeep Nair 	ret = of_property_read_u32_array(node, "qrange", temp, 2);
122941f93af9SSandeep Nair 	if (!ret) {
123041f93af9SSandeep Nair 		range->queue_base = temp[0] - kdev->base_id;
123141f93af9SSandeep Nair 		range->num_queues = temp[1];
123241f93af9SSandeep Nair 	} else {
123341f93af9SSandeep Nair 		dev_err(dev, "invalid queue range %s\n", range->name);
123441f93af9SSandeep Nair 		devm_kfree(dev, range);
123541f93af9SSandeep Nair 		return -EINVAL;
123641f93af9SSandeep Nair 	}
123741f93af9SSandeep Nair 
123841f93af9SSandeep Nair 	for (i = 0; i < RANGE_MAX_IRQS; i++) {
123941f93af9SSandeep Nair 		struct of_phandle_args oirq;
124041f93af9SSandeep Nair 
124141f93af9SSandeep Nair 		if (of_irq_parse_one(node, i, &oirq))
124241f93af9SSandeep Nair 			break;
124341f93af9SSandeep Nair 
124441f93af9SSandeep Nair 		range->irqs[i].irq = irq_create_of_mapping(&oirq);
124541f93af9SSandeep Nair 		if (range->irqs[i].irq == IRQ_NONE)
124641f93af9SSandeep Nair 			break;
124741f93af9SSandeep Nair 
124841f93af9SSandeep Nair 		range->num_irqs++;
124941f93af9SSandeep Nair 
1250832ad0e3SMarc Zyngier 		if (IS_ENABLED(CONFIG_SMP) && oirq.args_count == 3) {
1251832ad0e3SMarc Zyngier 			unsigned long mask;
1252832ad0e3SMarc Zyngier 			int bit;
1253832ad0e3SMarc Zyngier 
1254832ad0e3SMarc Zyngier 			range->irqs[i].cpu_mask = devm_kzalloc(dev,
1255832ad0e3SMarc Zyngier 							       cpumask_size(), GFP_KERNEL);
1256832ad0e3SMarc Zyngier 			if (!range->irqs[i].cpu_mask)
1257832ad0e3SMarc Zyngier 				return -ENOMEM;
1258832ad0e3SMarc Zyngier 
1259832ad0e3SMarc Zyngier 			mask = (oirq.args[2] & 0x0000ff00) >> 8;
1260832ad0e3SMarc Zyngier 			for_each_set_bit(bit, &mask, BITS_PER_LONG)
1261832ad0e3SMarc Zyngier 				cpumask_set_cpu(bit, range->irqs[i].cpu_mask);
1262832ad0e3SMarc Zyngier 		}
126341f93af9SSandeep Nair 	}
126441f93af9SSandeep Nair 
126541f93af9SSandeep Nair 	range->num_irqs = min(range->num_irqs, range->num_queues);
126641f93af9SSandeep Nair 	if (range->num_irqs)
126741f93af9SSandeep Nair 		range->flags |= RANGE_HAS_IRQ;
126841f93af9SSandeep Nair 
126941f93af9SSandeep Nair 	if (of_get_property(node, "qalloc-by-id", NULL))
127041f93af9SSandeep Nair 		range->flags |= RANGE_RESERVED;
127141f93af9SSandeep Nair 
127241f93af9SSandeep Nair 	if (of_get_property(node, "accumulator", NULL)) {
127341f93af9SSandeep Nair 		ret = knav_init_acc_range(kdev, node, range);
127441f93af9SSandeep Nair 		if (ret < 0) {
127541f93af9SSandeep Nair 			devm_kfree(dev, range);
127641f93af9SSandeep Nair 			return ret;
127741f93af9SSandeep Nair 		}
127841f93af9SSandeep Nair 	} else {
127941f93af9SSandeep Nair 		range->ops = &knav_gp_range_ops;
128041f93af9SSandeep Nair 	}
128141f93af9SSandeep Nair 
128241f93af9SSandeep Nair 	/* set threshold to 1, and flush out the queues */
128341f93af9SSandeep Nair 	for_each_qmgr(kdev, qmgr) {
128441f93af9SSandeep Nair 		start = max(qmgr->start_queue, range->queue_base);
128541f93af9SSandeep Nair 		end   = min(qmgr->start_queue + qmgr->num_queues,
128641f93af9SSandeep Nair 			    range->queue_base + range->num_queues);
128741f93af9SSandeep Nair 		for (id = start; id < end; id++) {
128841f93af9SSandeep Nair 			index = id - qmgr->start_queue;
128941f93af9SSandeep Nair 			writel_relaxed(THRESH_GTE | 1,
129041f93af9SSandeep Nair 				       &qmgr->reg_peek[index].ptr_size_thresh);
129141f93af9SSandeep Nair 			writel_relaxed(0,
129241f93af9SSandeep Nair 				       &qmgr->reg_push[index].ptr_size_thresh);
129341f93af9SSandeep Nair 		}
129441f93af9SSandeep Nair 	}
129541f93af9SSandeep Nair 
129641f93af9SSandeep Nair 	list_add_tail(&range->list, &kdev->queue_ranges);
129741f93af9SSandeep Nair 	dev_dbg(dev, "added range %s: %d-%d, %d irqs%s%s%s\n",
129841f93af9SSandeep Nair 		range->name, range->queue_base,
129941f93af9SSandeep Nair 		range->queue_base + range->num_queues - 1,
130041f93af9SSandeep Nair 		range->num_irqs,
130141f93af9SSandeep Nair 		(range->flags & RANGE_HAS_IRQ) ? ", has irq" : "",
130241f93af9SSandeep Nair 		(range->flags & RANGE_RESERVED) ? ", reserved" : "",
130341f93af9SSandeep Nair 		(range->flags & RANGE_HAS_ACCUMULATOR) ? ", acc" : "");
130441f93af9SSandeep Nair 	kdev->num_queues_in_use += range->num_queues;
130541f93af9SSandeep Nair 	return 0;
130641f93af9SSandeep Nair }
130741f93af9SSandeep Nair 
130841f93af9SSandeep Nair static int knav_setup_queue_pools(struct knav_device *kdev,
130941f93af9SSandeep Nair 				   struct device_node *queue_pools)
131041f93af9SSandeep Nair {
131141f93af9SSandeep Nair 	struct device_node *type, *range;
131241f93af9SSandeep Nair 
131341f93af9SSandeep Nair 	for_each_child_of_node(queue_pools, type) {
131441f93af9SSandeep Nair 		for_each_child_of_node(type, range) {
131541f93af9SSandeep Nair 			/* return value ignored, we init the rest... */
1316e7250109SLee Jones 			knav_setup_queue_range(kdev, range);
131741f93af9SSandeep Nair 		}
131841f93af9SSandeep Nair 	}
131941f93af9SSandeep Nair 
132041f93af9SSandeep Nair 	/* ... and barf if they all failed! */
132141f93af9SSandeep Nair 	if (list_empty(&kdev->queue_ranges)) {
132241f93af9SSandeep Nair 		dev_err(kdev->dev, "no valid queue range found\n");
132341f93af9SSandeep Nair 		return -ENODEV;
132441f93af9SSandeep Nair 	}
132541f93af9SSandeep Nair 	return 0;
132641f93af9SSandeep Nair }
132741f93af9SSandeep Nair 
132841f93af9SSandeep Nair static void knav_free_queue_range(struct knav_device *kdev,
132941f93af9SSandeep Nair 				  struct knav_range_info *range)
133041f93af9SSandeep Nair {
133141f93af9SSandeep Nair 	if (range->ops && range->ops->free_range)
133241f93af9SSandeep Nair 		range->ops->free_range(range);
133341f93af9SSandeep Nair 	list_del(&range->list);
133441f93af9SSandeep Nair 	devm_kfree(kdev->dev, range);
133541f93af9SSandeep Nair }
133641f93af9SSandeep Nair 
133741f93af9SSandeep Nair static void knav_free_queue_ranges(struct knav_device *kdev)
133841f93af9SSandeep Nair {
133941f93af9SSandeep Nair 	struct knav_range_info *range;
134041f93af9SSandeep Nair 
134141f93af9SSandeep Nair 	for (;;) {
134241f93af9SSandeep Nair 		range = first_queue_range(kdev);
134341f93af9SSandeep Nair 		if (!range)
134441f93af9SSandeep Nair 			break;
134541f93af9SSandeep Nair 		knav_free_queue_range(kdev, range);
134641f93af9SSandeep Nair 	}
134741f93af9SSandeep Nair }
134841f93af9SSandeep Nair 
134941f93af9SSandeep Nair static void knav_queue_free_regions(struct knav_device *kdev)
135041f93af9SSandeep Nair {
135141f93af9SSandeep Nair 	struct knav_region *region;
1352148bb043SAxel Lin 	struct knav_pool *pool, *tmp;
135341f93af9SSandeep Nair 	unsigned size;
135441f93af9SSandeep Nair 
135541f93af9SSandeep Nair 	for (;;) {
135641f93af9SSandeep Nair 		region = first_region(kdev);
135741f93af9SSandeep Nair 		if (!region)
135841f93af9SSandeep Nair 			break;
1359148bb043SAxel Lin 		list_for_each_entry_safe(pool, tmp, &region->pools, region_inst)
136041f93af9SSandeep Nair 			knav_pool_destroy(pool);
136141f93af9SSandeep Nair 
136241f93af9SSandeep Nair 		size = region->virt_end - region->virt_start;
136341f93af9SSandeep Nair 		if (size)
136441f93af9SSandeep Nair 			free_pages_exact(region->virt_start, size);
136541f93af9SSandeep Nair 		list_del(&region->list);
136641f93af9SSandeep Nair 		devm_kfree(kdev->dev, region);
136741f93af9SSandeep Nair 	}
136841f93af9SSandeep Nair }
136941f93af9SSandeep Nair 
137041f93af9SSandeep Nair static void __iomem *knav_queue_map_reg(struct knav_device *kdev,
137141f93af9SSandeep Nair 					struct device_node *node, int index)
137241f93af9SSandeep Nair {
137341f93af9SSandeep Nair 	struct resource res;
137441f93af9SSandeep Nair 	void __iomem *regs;
137541f93af9SSandeep Nair 	int ret;
137641f93af9SSandeep Nair 
137741f93af9SSandeep Nair 	ret = of_address_to_resource(node, index, &res);
137841f93af9SSandeep Nair 	if (ret) {
1379dc37a252SRob Herring 		dev_err(kdev->dev, "Can't translate of node(%pOFn) address for index(%d)\n",
1380dc37a252SRob Herring 			node, index);
138141f93af9SSandeep Nair 		return ERR_PTR(ret);
138241f93af9SSandeep Nair 	}
138341f93af9SSandeep Nair 
138441f93af9SSandeep Nair 	regs = devm_ioremap_resource(kdev->dev, &res);
138541f93af9SSandeep Nair 	if (IS_ERR(regs))
1386dc37a252SRob Herring 		dev_err(kdev->dev, "Failed to map register base for index(%d) node(%pOFn)\n",
1387dc37a252SRob Herring 			index, node);
138841f93af9SSandeep Nair 	return regs;
138941f93af9SSandeep Nair }
139041f93af9SSandeep Nair 
139141f93af9SSandeep Nair static int knav_queue_init_qmgrs(struct knav_device *kdev,
139241f93af9SSandeep Nair 					struct device_node *qmgrs)
139341f93af9SSandeep Nair {
139441f93af9SSandeep Nair 	struct device *dev = kdev->dev;
139541f93af9SSandeep Nair 	struct knav_qmgr_info *qmgr;
139641f93af9SSandeep Nair 	struct device_node *child;
139741f93af9SSandeep Nair 	u32 temp[2];
139841f93af9SSandeep Nair 	int ret;
139941f93af9SSandeep Nair 
140041f93af9SSandeep Nair 	for_each_child_of_node(qmgrs, child) {
140141f93af9SSandeep Nair 		qmgr = devm_kzalloc(dev, sizeof(*qmgr), GFP_KERNEL);
140241f93af9SSandeep Nair 		if (!qmgr) {
1403*a88f66d4SVasyl Gomonovych 			of_node_put(child);
140441f93af9SSandeep Nair 			dev_err(dev, "out of memory allocating qmgr\n");
140541f93af9SSandeep Nair 			return -ENOMEM;
140641f93af9SSandeep Nair 		}
140741f93af9SSandeep Nair 
140841f93af9SSandeep Nair 		ret = of_property_read_u32_array(child, "managed-queues",
140941f93af9SSandeep Nair 						 temp, 2);
141041f93af9SSandeep Nair 		if (!ret) {
141141f93af9SSandeep Nair 			qmgr->start_queue = temp[0];
141241f93af9SSandeep Nair 			qmgr->num_queues = temp[1];
141341f93af9SSandeep Nair 		} else {
141441f93af9SSandeep Nair 			dev_err(dev, "invalid qmgr queue range\n");
141541f93af9SSandeep Nair 			devm_kfree(dev, qmgr);
141641f93af9SSandeep Nair 			continue;
141741f93af9SSandeep Nair 		}
141841f93af9SSandeep Nair 
141941f93af9SSandeep Nair 		dev_info(dev, "qmgr start queue %d, number of queues %d\n",
142041f93af9SSandeep Nair 			 qmgr->start_queue, qmgr->num_queues);
142141f93af9SSandeep Nair 
142241f93af9SSandeep Nair 		qmgr->reg_peek =
142341f93af9SSandeep Nair 			knav_queue_map_reg(kdev, child,
142441f93af9SSandeep Nair 					   KNAV_QUEUE_PEEK_REG_INDEX);
1425350601b4SMurali Karicheri 
1426350601b4SMurali Karicheri 		if (kdev->version == QMSS) {
142741f93af9SSandeep Nair 			qmgr->reg_status =
142841f93af9SSandeep Nair 				knav_queue_map_reg(kdev, child,
142941f93af9SSandeep Nair 						   KNAV_QUEUE_STATUS_REG_INDEX);
1430350601b4SMurali Karicheri 		}
1431350601b4SMurali Karicheri 
143241f93af9SSandeep Nair 		qmgr->reg_config =
143341f93af9SSandeep Nair 			knav_queue_map_reg(kdev, child,
1434350601b4SMurali Karicheri 					   (kdev->version == QMSS_66AK2G) ?
1435350601b4SMurali Karicheri 					   KNAV_L_QUEUE_CONFIG_REG_INDEX :
143641f93af9SSandeep Nair 					   KNAV_QUEUE_CONFIG_REG_INDEX);
143741f93af9SSandeep Nair 		qmgr->reg_region =
143841f93af9SSandeep Nair 			knav_queue_map_reg(kdev, child,
1439350601b4SMurali Karicheri 					   (kdev->version == QMSS_66AK2G) ?
1440350601b4SMurali Karicheri 					   KNAV_L_QUEUE_REGION_REG_INDEX :
144141f93af9SSandeep Nair 					   KNAV_QUEUE_REGION_REG_INDEX);
1442350601b4SMurali Karicheri 
144341f93af9SSandeep Nair 		qmgr->reg_push =
144441f93af9SSandeep Nair 			knav_queue_map_reg(kdev, child,
1445350601b4SMurali Karicheri 					   (kdev->version == QMSS_66AK2G) ?
1446350601b4SMurali Karicheri 					    KNAV_L_QUEUE_PUSH_REG_INDEX :
144741f93af9SSandeep Nair 					    KNAV_QUEUE_PUSH_REG_INDEX);
1448350601b4SMurali Karicheri 
1449350601b4SMurali Karicheri 		if (kdev->version == QMSS) {
145041f93af9SSandeep Nair 			qmgr->reg_pop =
145141f93af9SSandeep Nair 				knav_queue_map_reg(kdev, child,
145241f93af9SSandeep Nair 						   KNAV_QUEUE_POP_REG_INDEX);
1453350601b4SMurali Karicheri 		}
145441f93af9SSandeep Nair 
1455350601b4SMurali Karicheri 		if (IS_ERR(qmgr->reg_peek) ||
1456350601b4SMurali Karicheri 		    ((kdev->version == QMSS) &&
1457350601b4SMurali Karicheri 		    (IS_ERR(qmgr->reg_status) || IS_ERR(qmgr->reg_pop))) ||
145841f93af9SSandeep Nair 		    IS_ERR(qmgr->reg_config) || IS_ERR(qmgr->reg_region) ||
1459350601b4SMurali Karicheri 		    IS_ERR(qmgr->reg_push)) {
146041f93af9SSandeep Nair 			dev_err(dev, "failed to map qmgr regs\n");
1461350601b4SMurali Karicheri 			if (kdev->version == QMSS) {
146241f93af9SSandeep Nair 				if (!IS_ERR(qmgr->reg_status))
146341f93af9SSandeep Nair 					devm_iounmap(dev, qmgr->reg_status);
1464350601b4SMurali Karicheri 				if (!IS_ERR(qmgr->reg_pop))
1465350601b4SMurali Karicheri 					devm_iounmap(dev, qmgr->reg_pop);
1466350601b4SMurali Karicheri 			}
1467350601b4SMurali Karicheri 			if (!IS_ERR(qmgr->reg_peek))
1468350601b4SMurali Karicheri 				devm_iounmap(dev, qmgr->reg_peek);
146941f93af9SSandeep Nair 			if (!IS_ERR(qmgr->reg_config))
147041f93af9SSandeep Nair 				devm_iounmap(dev, qmgr->reg_config);
147141f93af9SSandeep Nair 			if (!IS_ERR(qmgr->reg_region))
147241f93af9SSandeep Nair 				devm_iounmap(dev, qmgr->reg_region);
147341f93af9SSandeep Nair 			if (!IS_ERR(qmgr->reg_push))
147441f93af9SSandeep Nair 				devm_iounmap(dev, qmgr->reg_push);
147541f93af9SSandeep Nair 			devm_kfree(dev, qmgr);
147641f93af9SSandeep Nair 			continue;
147741f93af9SSandeep Nair 		}
147841f93af9SSandeep Nair 
1479350601b4SMurali Karicheri 		/* Use same push register for pop as well */
1480350601b4SMurali Karicheri 		if (kdev->version == QMSS_66AK2G)
1481350601b4SMurali Karicheri 			qmgr->reg_pop = qmgr->reg_push;
1482350601b4SMurali Karicheri 
148341f93af9SSandeep Nair 		list_add_tail(&qmgr->list, &kdev->qmgrs);
148441f93af9SSandeep Nair 		dev_info(dev, "added qmgr start queue %d, num of queues %d, reg_peek %p, reg_status %p, reg_config %p, reg_region %p, reg_push %p, reg_pop %p\n",
148541f93af9SSandeep Nair 			 qmgr->start_queue, qmgr->num_queues,
148641f93af9SSandeep Nair 			 qmgr->reg_peek, qmgr->reg_status,
148741f93af9SSandeep Nair 			 qmgr->reg_config, qmgr->reg_region,
148841f93af9SSandeep Nair 			 qmgr->reg_push, qmgr->reg_pop);
148941f93af9SSandeep Nair 	}
149041f93af9SSandeep Nair 	return 0;
149141f93af9SSandeep Nair }
149241f93af9SSandeep Nair 
149341f93af9SSandeep Nair static int knav_queue_init_pdsps(struct knav_device *kdev,
149441f93af9SSandeep Nair 					struct device_node *pdsps)
149541f93af9SSandeep Nair {
149641f93af9SSandeep Nair 	struct device *dev = kdev->dev;
149741f93af9SSandeep Nair 	struct knav_pdsp_info *pdsp;
149841f93af9SSandeep Nair 	struct device_node *child;
149941f93af9SSandeep Nair 
150041f93af9SSandeep Nair 	for_each_child_of_node(pdsps, child) {
150141f93af9SSandeep Nair 		pdsp = devm_kzalloc(dev, sizeof(*pdsp), GFP_KERNEL);
150241f93af9SSandeep Nair 		if (!pdsp) {
1503*a88f66d4SVasyl Gomonovych 			of_node_put(child);
150441f93af9SSandeep Nair 			dev_err(dev, "out of memory allocating pdsp\n");
150541f93af9SSandeep Nair 			return -ENOMEM;
150641f93af9SSandeep Nair 		}
150741f93af9SSandeep Nair 		pdsp->name = knav_queue_find_name(child);
150841f93af9SSandeep Nair 		pdsp->iram =
150941f93af9SSandeep Nair 			knav_queue_map_reg(kdev, child,
151041f93af9SSandeep Nair 					   KNAV_QUEUE_PDSP_IRAM_REG_INDEX);
151141f93af9SSandeep Nair 		pdsp->regs =
151241f93af9SSandeep Nair 			knav_queue_map_reg(kdev, child,
151341f93af9SSandeep Nair 					   KNAV_QUEUE_PDSP_REGS_REG_INDEX);
151441f93af9SSandeep Nair 		pdsp->intd =
151541f93af9SSandeep Nair 			knav_queue_map_reg(kdev, child,
151641f93af9SSandeep Nair 					   KNAV_QUEUE_PDSP_INTD_REG_INDEX);
151741f93af9SSandeep Nair 		pdsp->command =
151841f93af9SSandeep Nair 			knav_queue_map_reg(kdev, child,
151941f93af9SSandeep Nair 					   KNAV_QUEUE_PDSP_CMD_REG_INDEX);
152041f93af9SSandeep Nair 
152141f93af9SSandeep Nair 		if (IS_ERR(pdsp->command) || IS_ERR(pdsp->iram) ||
152241f93af9SSandeep Nair 		    IS_ERR(pdsp->regs) || IS_ERR(pdsp->intd)) {
152341f93af9SSandeep Nair 			dev_err(dev, "failed to map pdsp %s regs\n",
152441f93af9SSandeep Nair 				pdsp->name);
152541f93af9SSandeep Nair 			if (!IS_ERR(pdsp->command))
152641f93af9SSandeep Nair 				devm_iounmap(dev, pdsp->command);
152741f93af9SSandeep Nair 			if (!IS_ERR(pdsp->iram))
152841f93af9SSandeep Nair 				devm_iounmap(dev, pdsp->iram);
152941f93af9SSandeep Nair 			if (!IS_ERR(pdsp->regs))
153041f93af9SSandeep Nair 				devm_iounmap(dev, pdsp->regs);
153141f93af9SSandeep Nair 			if (!IS_ERR(pdsp->intd))
153241f93af9SSandeep Nair 				devm_iounmap(dev, pdsp->intd);
153341f93af9SSandeep Nair 			devm_kfree(dev, pdsp);
153441f93af9SSandeep Nair 			continue;
153541f93af9SSandeep Nair 		}
153641f93af9SSandeep Nair 		of_property_read_u32(child, "id", &pdsp->id);
153741f93af9SSandeep Nair 		list_add_tail(&pdsp->list, &kdev->pdsps);
153896ee19beSMurali Karicheri 		dev_dbg(dev, "added pdsp %s: command %p, iram %p, regs %p, intd %p\n",
153941f93af9SSandeep Nair 			pdsp->name, pdsp->command, pdsp->iram, pdsp->regs,
154096ee19beSMurali Karicheri 			pdsp->intd);
154141f93af9SSandeep Nair 	}
154241f93af9SSandeep Nair 	return 0;
154341f93af9SSandeep Nair }
154441f93af9SSandeep Nair 
154541f93af9SSandeep Nair static int knav_queue_stop_pdsp(struct knav_device *kdev,
154641f93af9SSandeep Nair 			  struct knav_pdsp_info *pdsp)
154741f93af9SSandeep Nair {
154841f93af9SSandeep Nair 	u32 val, timeout = 1000;
154941f93af9SSandeep Nair 	int ret;
155041f93af9SSandeep Nair 
155141f93af9SSandeep Nair 	val = readl_relaxed(&pdsp->regs->control) & ~PDSP_CTRL_ENABLE;
155241f93af9SSandeep Nair 	writel_relaxed(val, &pdsp->regs->control);
155341f93af9SSandeep Nair 	ret = knav_queue_pdsp_wait(&pdsp->regs->control, timeout,
155441f93af9SSandeep Nair 					PDSP_CTRL_RUNNING);
155541f93af9SSandeep Nair 	if (ret < 0) {
155641f93af9SSandeep Nair 		dev_err(kdev->dev, "timed out on pdsp %s stop\n", pdsp->name);
155741f93af9SSandeep Nair 		return ret;
155841f93af9SSandeep Nair 	}
155904501690SMurali Karicheri 	pdsp->loaded = false;
156004501690SMurali Karicheri 	pdsp->started = false;
156141f93af9SSandeep Nair 	return 0;
156241f93af9SSandeep Nair }
156341f93af9SSandeep Nair 
156441f93af9SSandeep Nair static int knav_queue_load_pdsp(struct knav_device *kdev,
156541f93af9SSandeep Nair 			  struct knav_pdsp_info *pdsp)
156641f93af9SSandeep Nair {
156741f93af9SSandeep Nair 	int i, ret, fwlen;
156841f93af9SSandeep Nair 	const struct firmware *fw;
156996ee19beSMurali Karicheri 	bool found = false;
157041f93af9SSandeep Nair 	u32 *fwdata;
157141f93af9SSandeep Nair 
157296ee19beSMurali Karicheri 	for (i = 0; i < ARRAY_SIZE(knav_acc_firmwares); i++) {
157396ee19beSMurali Karicheri 		if (knav_acc_firmwares[i]) {
15746af1a073SMurali Karicheri 			ret = request_firmware_direct(&fw,
157596ee19beSMurali Karicheri 						      knav_acc_firmwares[i],
157696ee19beSMurali Karicheri 						      kdev->dev);
157796ee19beSMurali Karicheri 			if (!ret) {
157896ee19beSMurali Karicheri 				found = true;
157996ee19beSMurali Karicheri 				break;
158041f93af9SSandeep Nair 			}
158196ee19beSMurali Karicheri 		}
158296ee19beSMurali Karicheri 	}
158396ee19beSMurali Karicheri 
158496ee19beSMurali Karicheri 	if (!found) {
158596ee19beSMurali Karicheri 		dev_err(kdev->dev, "failed to get firmware for pdsp\n");
158696ee19beSMurali Karicheri 		return -ENODEV;
158796ee19beSMurali Karicheri 	}
158896ee19beSMurali Karicheri 
158996ee19beSMurali Karicheri 	dev_info(kdev->dev, "firmware file %s downloaded for PDSP\n",
159096ee19beSMurali Karicheri 		 knav_acc_firmwares[i]);
159196ee19beSMurali Karicheri 
159241f93af9SSandeep Nair 	writel_relaxed(pdsp->id + 1, pdsp->command + 0x18);
159341f93af9SSandeep Nair 	/* download the firmware */
159441f93af9SSandeep Nair 	fwdata = (u32 *)fw->data;
159541f93af9SSandeep Nair 	fwlen = (fw->size + sizeof(u32) - 1) / sizeof(u32);
159641f93af9SSandeep Nair 	for (i = 0; i < fwlen; i++)
159741f93af9SSandeep Nair 		writel_relaxed(be32_to_cpu(fwdata[i]), pdsp->iram + i);
159841f93af9SSandeep Nair 
159941f93af9SSandeep Nair 	release_firmware(fw);
160041f93af9SSandeep Nair 	return 0;
160141f93af9SSandeep Nair }
160241f93af9SSandeep Nair 
160341f93af9SSandeep Nair static int knav_queue_start_pdsp(struct knav_device *kdev,
160441f93af9SSandeep Nair 			   struct knav_pdsp_info *pdsp)
160541f93af9SSandeep Nair {
160641f93af9SSandeep Nair 	u32 val, timeout = 1000;
160741f93af9SSandeep Nair 	int ret;
160841f93af9SSandeep Nair 
160941f93af9SSandeep Nair 	/* write a command for sync */
161041f93af9SSandeep Nair 	writel_relaxed(0xffffffff, pdsp->command);
161141f93af9SSandeep Nair 	while (readl_relaxed(pdsp->command) != 0xffffffff)
161241f93af9SSandeep Nair 		cpu_relax();
161341f93af9SSandeep Nair 
161441f93af9SSandeep Nair 	/* soft reset the PDSP */
161541f93af9SSandeep Nair 	val  = readl_relaxed(&pdsp->regs->control);
161641f93af9SSandeep Nair 	val &= ~(PDSP_CTRL_PC_MASK | PDSP_CTRL_SOFT_RESET);
161741f93af9SSandeep Nair 	writel_relaxed(val, &pdsp->regs->control);
161841f93af9SSandeep Nair 
161941f93af9SSandeep Nair 	/* enable pdsp */
162041f93af9SSandeep Nair 	val = readl_relaxed(&pdsp->regs->control) | PDSP_CTRL_ENABLE;
162141f93af9SSandeep Nair 	writel_relaxed(val, &pdsp->regs->control);
162241f93af9SSandeep Nair 
162341f93af9SSandeep Nair 	/* wait for command register to clear */
162441f93af9SSandeep Nair 	ret = knav_queue_pdsp_wait(pdsp->command, timeout, 0);
162541f93af9SSandeep Nair 	if (ret < 0) {
162641f93af9SSandeep Nair 		dev_err(kdev->dev,
162741f93af9SSandeep Nair 			"timed out on pdsp %s command register wait\n",
162841f93af9SSandeep Nair 			pdsp->name);
162941f93af9SSandeep Nair 		return ret;
163041f93af9SSandeep Nair 	}
163141f93af9SSandeep Nair 	return 0;
163241f93af9SSandeep Nair }
163341f93af9SSandeep Nair 
163441f93af9SSandeep Nair static void knav_queue_stop_pdsps(struct knav_device *kdev)
163541f93af9SSandeep Nair {
163641f93af9SSandeep Nair 	struct knav_pdsp_info *pdsp;
163741f93af9SSandeep Nair 
163841f93af9SSandeep Nair 	/* disable all pdsps */
163941f93af9SSandeep Nair 	for_each_pdsp(kdev, pdsp)
164041f93af9SSandeep Nair 		knav_queue_stop_pdsp(kdev, pdsp);
164141f93af9SSandeep Nair }
164241f93af9SSandeep Nair 
164341f93af9SSandeep Nair static int knav_queue_start_pdsps(struct knav_device *kdev)
164441f93af9SSandeep Nair {
164541f93af9SSandeep Nair 	struct knav_pdsp_info *pdsp;
164641f93af9SSandeep Nair 	int ret;
164741f93af9SSandeep Nair 
164841f93af9SSandeep Nair 	knav_queue_stop_pdsps(kdev);
164904501690SMurali Karicheri 	/* now load them all. We return success even if pdsp
165004501690SMurali Karicheri 	 * is not loaded as acc channels are optional on having
165104501690SMurali Karicheri 	 * firmware availability in the system. We set the loaded
165204501690SMurali Karicheri 	 * and stated flag and when initialize the acc range, check
165304501690SMurali Karicheri 	 * it and init the range only if pdsp is started.
165404501690SMurali Karicheri 	 */
165541f93af9SSandeep Nair 	for_each_pdsp(kdev, pdsp) {
165641f93af9SSandeep Nair 		ret = knav_queue_load_pdsp(kdev, pdsp);
165704501690SMurali Karicheri 		if (!ret)
165804501690SMurali Karicheri 			pdsp->loaded = true;
165941f93af9SSandeep Nair 	}
166041f93af9SSandeep Nair 
166141f93af9SSandeep Nair 	for_each_pdsp(kdev, pdsp) {
166204501690SMurali Karicheri 		if (pdsp->loaded) {
166341f93af9SSandeep Nair 			ret = knav_queue_start_pdsp(kdev, pdsp);
166404501690SMurali Karicheri 			if (!ret)
166504501690SMurali Karicheri 				pdsp->started = true;
166604501690SMurali Karicheri 		}
166741f93af9SSandeep Nair 	}
166841f93af9SSandeep Nair 	return 0;
166941f93af9SSandeep Nair }
167041f93af9SSandeep Nair 
167141f93af9SSandeep Nair static inline struct knav_qmgr_info *knav_find_qmgr(unsigned id)
167241f93af9SSandeep Nair {
167341f93af9SSandeep Nair 	struct knav_qmgr_info *qmgr;
167441f93af9SSandeep Nair 
167541f93af9SSandeep Nair 	for_each_qmgr(kdev, qmgr) {
167641f93af9SSandeep Nair 		if ((id >= qmgr->start_queue) &&
167741f93af9SSandeep Nair 		    (id < qmgr->start_queue + qmgr->num_queues))
167841f93af9SSandeep Nair 			return qmgr;
167941f93af9SSandeep Nair 	}
168041f93af9SSandeep Nair 	return NULL;
168141f93af9SSandeep Nair }
168241f93af9SSandeep Nair 
168341f93af9SSandeep Nair static int knav_queue_init_queue(struct knav_device *kdev,
168441f93af9SSandeep Nair 					struct knav_range_info *range,
168541f93af9SSandeep Nair 					struct knav_queue_inst *inst,
168641f93af9SSandeep Nair 					unsigned id)
168741f93af9SSandeep Nair {
168841f93af9SSandeep Nair 	char irq_name[KNAV_NAME_SIZE];
168941f93af9SSandeep Nair 	inst->qmgr = knav_find_qmgr(id);
169041f93af9SSandeep Nair 	if (!inst->qmgr)
169141f93af9SSandeep Nair 		return -1;
169241f93af9SSandeep Nair 
169341f93af9SSandeep Nair 	INIT_LIST_HEAD(&inst->handles);
169441f93af9SSandeep Nair 	inst->kdev = kdev;
169541f93af9SSandeep Nair 	inst->range = range;
169641f93af9SSandeep Nair 	inst->irq_num = -1;
169741f93af9SSandeep Nair 	inst->id = id;
169841f93af9SSandeep Nair 	scnprintf(irq_name, sizeof(irq_name), "hwqueue-%d", id);
169941f93af9SSandeep Nair 	inst->irq_name = kstrndup(irq_name, sizeof(irq_name), GFP_KERNEL);
170041f93af9SSandeep Nair 
170141f93af9SSandeep Nair 	if (range->ops && range->ops->init_queue)
170241f93af9SSandeep Nair 		return range->ops->init_queue(range, inst);
170341f93af9SSandeep Nair 	else
170441f93af9SSandeep Nair 		return 0;
170541f93af9SSandeep Nair }
170641f93af9SSandeep Nair 
170741f93af9SSandeep Nair static int knav_queue_init_queues(struct knav_device *kdev)
170841f93af9SSandeep Nair {
170941f93af9SSandeep Nair 	struct knav_range_info *range;
171041f93af9SSandeep Nair 	int size, id, base_idx;
171141f93af9SSandeep Nair 	int idx = 0, ret = 0;
171241f93af9SSandeep Nair 
171341f93af9SSandeep Nair 	/* how much do we need for instance data? */
171441f93af9SSandeep Nair 	size = sizeof(struct knav_queue_inst);
171541f93af9SSandeep Nair 
171641f93af9SSandeep Nair 	/* round this up to a power of 2, keep the index to instance
171741f93af9SSandeep Nair 	 * arithmetic fast.
171841f93af9SSandeep Nair 	 * */
171941f93af9SSandeep Nair 	kdev->inst_shift = order_base_2(size);
172041f93af9SSandeep Nair 	size = (1 << kdev->inst_shift) * kdev->num_queues_in_use;
172141f93af9SSandeep Nair 	kdev->instances = devm_kzalloc(kdev->dev, size, GFP_KERNEL);
172241f93af9SSandeep Nair 	if (!kdev->instances)
172339179cb5SAxel Lin 		return -ENOMEM;
172441f93af9SSandeep Nair 
172541f93af9SSandeep Nair 	for_each_queue_range(kdev, range) {
172641f93af9SSandeep Nair 		if (range->ops && range->ops->init_range)
172741f93af9SSandeep Nair 			range->ops->init_range(range);
172841f93af9SSandeep Nair 		base_idx = idx;
172941f93af9SSandeep Nair 		for (id = range->queue_base;
173041f93af9SSandeep Nair 		     id < range->queue_base + range->num_queues; id++, idx++) {
173141f93af9SSandeep Nair 			ret = knav_queue_init_queue(kdev, range,
173241f93af9SSandeep Nair 					knav_queue_idx_to_inst(kdev, idx), id);
173341f93af9SSandeep Nair 			if (ret < 0)
173441f93af9SSandeep Nair 				return ret;
173541f93af9SSandeep Nair 		}
173641f93af9SSandeep Nair 		range->queue_base_inst =
173741f93af9SSandeep Nair 			knav_queue_idx_to_inst(kdev, base_idx);
173841f93af9SSandeep Nair 	}
173941f93af9SSandeep Nair 	return 0;
174041f93af9SSandeep Nair }
174141f93af9SSandeep Nair 
1742350601b4SMurali Karicheri /* Match table for of_platform binding */
1743350601b4SMurali Karicheri static const struct of_device_id keystone_qmss_of_match[] = {
1744350601b4SMurali Karicheri 	{
1745350601b4SMurali Karicheri 		.compatible = "ti,keystone-navigator-qmss",
1746350601b4SMurali Karicheri 	},
1747350601b4SMurali Karicheri 	{
1748350601b4SMurali Karicheri 		.compatible = "ti,66ak2g-navss-qm",
1749350601b4SMurali Karicheri 		.data	= (void *)QMSS_66AK2G,
1750350601b4SMurali Karicheri 	},
1751350601b4SMurali Karicheri 	{},
1752350601b4SMurali Karicheri };
1753350601b4SMurali Karicheri MODULE_DEVICE_TABLE(of, keystone_qmss_of_match);
1754350601b4SMurali Karicheri 
175541f93af9SSandeep Nair static int knav_queue_probe(struct platform_device *pdev)
175641f93af9SSandeep Nair {
175741f93af9SSandeep Nair 	struct device_node *node = pdev->dev.of_node;
175841f93af9SSandeep Nair 	struct device_node *qmgrs, *queue_pools, *regions, *pdsps;
1759350601b4SMurali Karicheri 	const struct of_device_id *match;
176041f93af9SSandeep Nair 	struct device *dev = &pdev->dev;
176141f93af9SSandeep Nair 	u32 temp[2];
176241f93af9SSandeep Nair 	int ret;
176341f93af9SSandeep Nair 
176441f93af9SSandeep Nair 	if (!node) {
176541f93af9SSandeep Nair 		dev_err(dev, "device tree info unavailable\n");
176641f93af9SSandeep Nair 		return -ENODEV;
176741f93af9SSandeep Nair 	}
176841f93af9SSandeep Nair 
176941f93af9SSandeep Nair 	kdev = devm_kzalloc(dev, sizeof(struct knav_device), GFP_KERNEL);
177041f93af9SSandeep Nair 	if (!kdev) {
177141f93af9SSandeep Nair 		dev_err(dev, "memory allocation failed\n");
177241f93af9SSandeep Nair 		return -ENOMEM;
177341f93af9SSandeep Nair 	}
177441f93af9SSandeep Nair 
1775350601b4SMurali Karicheri 	match = of_match_device(of_match_ptr(keystone_qmss_of_match), dev);
1776350601b4SMurali Karicheri 	if (match && match->data)
1777350601b4SMurali Karicheri 		kdev->version = QMSS_66AK2G;
1778350601b4SMurali Karicheri 
177941f93af9SSandeep Nair 	platform_set_drvdata(pdev, kdev);
178041f93af9SSandeep Nair 	kdev->dev = dev;
178141f93af9SSandeep Nair 	INIT_LIST_HEAD(&kdev->queue_ranges);
178241f93af9SSandeep Nair 	INIT_LIST_HEAD(&kdev->qmgrs);
178341f93af9SSandeep Nair 	INIT_LIST_HEAD(&kdev->pools);
178441f93af9SSandeep Nair 	INIT_LIST_HEAD(&kdev->regions);
178541f93af9SSandeep Nair 	INIT_LIST_HEAD(&kdev->pdsps);
178641f93af9SSandeep Nair 
178741f93af9SSandeep Nair 	pm_runtime_enable(&pdev->dev);
178841f93af9SSandeep Nair 	ret = pm_runtime_get_sync(&pdev->dev);
178941f93af9SSandeep Nair 	if (ret < 0) {
1790ec868484SZhang Qilong 		pm_runtime_put_noidle(&pdev->dev);
179141f93af9SSandeep Nair 		dev_err(dev, "Failed to enable QMSS\n");
179241f93af9SSandeep Nair 		return ret;
179341f93af9SSandeep Nair 	}
179441f93af9SSandeep Nair 
179541f93af9SSandeep Nair 	if (of_property_read_u32_array(node, "queue-range", temp, 2)) {
179641f93af9SSandeep Nair 		dev_err(dev, "queue-range not specified\n");
179741f93af9SSandeep Nair 		ret = -ENODEV;
179841f93af9SSandeep Nair 		goto err;
179941f93af9SSandeep Nair 	}
180041f93af9SSandeep Nair 	kdev->base_id    = temp[0];
180141f93af9SSandeep Nair 	kdev->num_queues = temp[1];
180241f93af9SSandeep Nair 
180341f93af9SSandeep Nair 	/* Initialize queue managers using device tree configuration */
180441f93af9SSandeep Nair 	qmgrs =  of_get_child_by_name(node, "qmgrs");
180541f93af9SSandeep Nair 	if (!qmgrs) {
180641f93af9SSandeep Nair 		dev_err(dev, "queue manager info not specified\n");
180741f93af9SSandeep Nair 		ret = -ENODEV;
180841f93af9SSandeep Nair 		goto err;
180941f93af9SSandeep Nair 	}
181041f93af9SSandeep Nair 	ret = knav_queue_init_qmgrs(kdev, qmgrs);
181141f93af9SSandeep Nair 	of_node_put(qmgrs);
181241f93af9SSandeep Nair 	if (ret)
181341f93af9SSandeep Nair 		goto err;
181441f93af9SSandeep Nair 
181541f93af9SSandeep Nair 	/* get pdsp configuration values from device tree */
181641f93af9SSandeep Nair 	pdsps =  of_get_child_by_name(node, "pdsps");
181741f93af9SSandeep Nair 	if (pdsps) {
181841f93af9SSandeep Nair 		ret = knav_queue_init_pdsps(kdev, pdsps);
181941f93af9SSandeep Nair 		if (ret)
182041f93af9SSandeep Nair 			goto err;
182141f93af9SSandeep Nair 
182241f93af9SSandeep Nair 		ret = knav_queue_start_pdsps(kdev);
182341f93af9SSandeep Nair 		if (ret)
182441f93af9SSandeep Nair 			goto err;
182541f93af9SSandeep Nair 	}
182641f93af9SSandeep Nair 	of_node_put(pdsps);
182741f93af9SSandeep Nair 
182841f93af9SSandeep Nair 	/* get usable queue range values from device tree */
182941f93af9SSandeep Nair 	queue_pools = of_get_child_by_name(node, "queue-pools");
183041f93af9SSandeep Nair 	if (!queue_pools) {
183141f93af9SSandeep Nair 		dev_err(dev, "queue-pools not specified\n");
183241f93af9SSandeep Nair 		ret = -ENODEV;
183341f93af9SSandeep Nair 		goto err;
183441f93af9SSandeep Nair 	}
183541f93af9SSandeep Nair 	ret = knav_setup_queue_pools(kdev, queue_pools);
183641f93af9SSandeep Nair 	of_node_put(queue_pools);
183741f93af9SSandeep Nair 	if (ret)
183841f93af9SSandeep Nair 		goto err;
183941f93af9SSandeep Nair 
184041f93af9SSandeep Nair 	ret = knav_get_link_ram(kdev, "linkram0", &kdev->link_rams[0]);
184141f93af9SSandeep Nair 	if (ret) {
184241f93af9SSandeep Nair 		dev_err(kdev->dev, "could not setup linking ram\n");
184341f93af9SSandeep Nair 		goto err;
184441f93af9SSandeep Nair 	}
184541f93af9SSandeep Nair 
184641f93af9SSandeep Nair 	ret = knav_get_link_ram(kdev, "linkram1", &kdev->link_rams[1]);
184741f93af9SSandeep Nair 	if (ret) {
184841f93af9SSandeep Nair 		/*
184941f93af9SSandeep Nair 		 * nothing really, we have one linking ram already, so we just
185041f93af9SSandeep Nair 		 * live within our means
185141f93af9SSandeep Nair 		 */
185241f93af9SSandeep Nair 	}
185341f93af9SSandeep Nair 
185441f93af9SSandeep Nair 	ret = knav_queue_setup_link_ram(kdev);
185541f93af9SSandeep Nair 	if (ret)
185641f93af9SSandeep Nair 		goto err;
185741f93af9SSandeep Nair 
185841f93af9SSandeep Nair 	regions = of_get_child_by_name(node, "descriptor-regions");
185941f93af9SSandeep Nair 	if (!regions) {
186041f93af9SSandeep Nair 		dev_err(dev, "descriptor-regions not specified\n");
18614cba398fSZhihao Cheng 		ret = -ENODEV;
186241f93af9SSandeep Nair 		goto err;
186341f93af9SSandeep Nair 	}
186441f93af9SSandeep Nair 	ret = knav_queue_setup_regions(kdev, regions);
186541f93af9SSandeep Nair 	of_node_put(regions);
186641f93af9SSandeep Nair 	if (ret)
186741f93af9SSandeep Nair 		goto err;
186841f93af9SSandeep Nair 
186941f93af9SSandeep Nair 	ret = knav_queue_init_queues(kdev);
187041f93af9SSandeep Nair 	if (ret < 0) {
187141f93af9SSandeep Nair 		dev_err(dev, "hwqueue initialization failed\n");
187241f93af9SSandeep Nair 		goto err;
187341f93af9SSandeep Nair 	}
187441f93af9SSandeep Nair 
187541f93af9SSandeep Nair 	debugfs_create_file("qmss", S_IFREG | S_IRUGO, NULL, NULL,
187674e0e43aSQinglang Miao 			    &knav_queue_debug_fops);
1877a2dd6877SMurali Karicheri 	device_ready = true;
187841f93af9SSandeep Nair 	return 0;
187941f93af9SSandeep Nair 
188041f93af9SSandeep Nair err:
188141f93af9SSandeep Nair 	knav_queue_stop_pdsps(kdev);
188241f93af9SSandeep Nair 	knav_queue_free_regions(kdev);
188341f93af9SSandeep Nair 	knav_free_queue_ranges(kdev);
188441f93af9SSandeep Nair 	pm_runtime_put_sync(&pdev->dev);
188541f93af9SSandeep Nair 	pm_runtime_disable(&pdev->dev);
188641f93af9SSandeep Nair 	return ret;
188741f93af9SSandeep Nair }
188841f93af9SSandeep Nair 
188941f93af9SSandeep Nair static int knav_queue_remove(struct platform_device *pdev)
189041f93af9SSandeep Nair {
189141f93af9SSandeep Nair 	/* TODO: Free resources */
189241f93af9SSandeep Nair 	pm_runtime_put_sync(&pdev->dev);
189341f93af9SSandeep Nair 	pm_runtime_disable(&pdev->dev);
189441f93af9SSandeep Nair 	return 0;
189541f93af9SSandeep Nair }
189641f93af9SSandeep Nair 
189741f93af9SSandeep Nair static struct platform_driver keystone_qmss_driver = {
189841f93af9SSandeep Nair 	.probe		= knav_queue_probe,
189941f93af9SSandeep Nair 	.remove		= knav_queue_remove,
190041f93af9SSandeep Nair 	.driver		= {
190141f93af9SSandeep Nair 		.name	= "keystone-navigator-qmss",
190241f93af9SSandeep Nair 		.of_match_table = keystone_qmss_of_match,
190341f93af9SSandeep Nair 	},
190441f93af9SSandeep Nair };
190541f93af9SSandeep Nair module_platform_driver(keystone_qmss_driver);
190641f93af9SSandeep Nair 
190741f93af9SSandeep Nair MODULE_LICENSE("GPL v2");
190841f93af9SSandeep Nair MODULE_DESCRIPTION("TI QMSS driver for Keystone SOCs");
190941f93af9SSandeep Nair MODULE_AUTHOR("Sandeep Nair <sandeep_n@ti.com>");
191041f93af9SSandeep Nair MODULE_AUTHOR("Santosh Shilimkar <santosh.shilimkar@ti.com>");
1911