xref: /linux/drivers/soc/ti/knav_qmss_queue.c (revision 832ad0e3da4510fd17f98804abe512ea9a747035)
141f93af9SSandeep Nair /*
241f93af9SSandeep Nair  * Keystone Queue Manager subsystem driver
341f93af9SSandeep Nair  *
441f93af9SSandeep Nair  * Copyright (C) 2014 Texas Instruments Incorporated - http://www.ti.com
541f93af9SSandeep Nair  * Authors:	Sandeep Nair <sandeep_n@ti.com>
641f93af9SSandeep Nair  *		Cyril Chemparathy <cyril@ti.com>
741f93af9SSandeep Nair  *		Santosh Shilimkar <santosh.shilimkar@ti.com>
841f93af9SSandeep Nair  *
941f93af9SSandeep Nair  * This program is free software; you can redistribute it and/or
1041f93af9SSandeep Nair  * modify it under the terms of the GNU General Public License
1141f93af9SSandeep Nair  * version 2 as published by the Free Software Foundation.
1241f93af9SSandeep Nair  *
1341f93af9SSandeep Nair  * This program is distributed in the hope that it will be useful, but
1441f93af9SSandeep Nair  * WITHOUT ANY WARRANTY; without even the implied warranty of
1541f93af9SSandeep Nair  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
1641f93af9SSandeep Nair  * General Public License for more details.
1741f93af9SSandeep Nair  */
1841f93af9SSandeep Nair 
1941f93af9SSandeep Nair #include <linux/debugfs.h>
206e03f653SMurali Karicheri #include <linux/dma-mapping.h>
216e03f653SMurali Karicheri #include <linux/firmware.h>
226e03f653SMurali Karicheri #include <linux/interrupt.h>
236e03f653SMurali Karicheri #include <linux/io.h>
246e03f653SMurali Karicheri #include <linux/module.h>
256e03f653SMurali Karicheri #include <linux/of_address.h>
266e03f653SMurali Karicheri #include <linux/of_device.h>
276e03f653SMurali Karicheri #include <linux/of_irq.h>
286e03f653SMurali Karicheri #include <linux/pm_runtime.h>
296e03f653SMurali Karicheri #include <linux/slab.h>
3041f93af9SSandeep Nair #include <linux/soc/ti/knav_qmss.h>
3141f93af9SSandeep Nair 
3241f93af9SSandeep Nair #include "knav_qmss.h"
3341f93af9SSandeep Nair 
3441f93af9SSandeep Nair static struct knav_device *kdev;
3541f93af9SSandeep Nair static DEFINE_MUTEX(knav_dev_lock);
3641f93af9SSandeep Nair 
3741f93af9SSandeep Nair /* Queue manager register indices in DTS */
3841f93af9SSandeep Nair #define KNAV_QUEUE_PEEK_REG_INDEX	0
3941f93af9SSandeep Nair #define KNAV_QUEUE_STATUS_REG_INDEX	1
4041f93af9SSandeep Nair #define KNAV_QUEUE_CONFIG_REG_INDEX	2
4141f93af9SSandeep Nair #define KNAV_QUEUE_REGION_REG_INDEX	3
4241f93af9SSandeep Nair #define KNAV_QUEUE_PUSH_REG_INDEX	4
4341f93af9SSandeep Nair #define KNAV_QUEUE_POP_REG_INDEX	5
4441f93af9SSandeep Nair 
45350601b4SMurali Karicheri /* Queue manager register indices in DTS for QMSS in K2G NAVSS.
46350601b4SMurali Karicheri  * There are no status and vbusm push registers on this version
47350601b4SMurali Karicheri  * of QMSS. Push registers are same as pop, So all indices above 1
48350601b4SMurali Karicheri  * are to be re-defined
49350601b4SMurali Karicheri  */
50350601b4SMurali Karicheri #define KNAV_L_QUEUE_CONFIG_REG_INDEX	1
51350601b4SMurali Karicheri #define KNAV_L_QUEUE_REGION_REG_INDEX	2
52350601b4SMurali Karicheri #define KNAV_L_QUEUE_PUSH_REG_INDEX	3
53350601b4SMurali Karicheri 
5441f93af9SSandeep Nair /* PDSP register indices in DTS */
5541f93af9SSandeep Nair #define KNAV_QUEUE_PDSP_IRAM_REG_INDEX	0
5641f93af9SSandeep Nair #define KNAV_QUEUE_PDSP_REGS_REG_INDEX	1
5741f93af9SSandeep Nair #define KNAV_QUEUE_PDSP_INTD_REG_INDEX	2
5841f93af9SSandeep Nair #define KNAV_QUEUE_PDSP_CMD_REG_INDEX	3
5941f93af9SSandeep Nair 
6041f93af9SSandeep Nair #define knav_queue_idx_to_inst(kdev, idx)			\
6141f93af9SSandeep Nair 	(kdev->instances + (idx << kdev->inst_shift))
6241f93af9SSandeep Nair 
6341f93af9SSandeep Nair #define for_each_handle_rcu(qh, inst)			\
6441f93af9SSandeep Nair 	list_for_each_entry_rcu(qh, &inst->handles, list)
6541f93af9SSandeep Nair 
6641f93af9SSandeep Nair #define for_each_instance(idx, inst, kdev)		\
6741f93af9SSandeep Nair 	for (idx = 0, inst = kdev->instances;		\
6841f93af9SSandeep Nair 	     idx < (kdev)->num_queues_in_use;			\
6941f93af9SSandeep Nair 	     idx++, inst = knav_queue_idx_to_inst(kdev, idx))
7041f93af9SSandeep Nair 
7196ee19beSMurali Karicheri /* All firmware file names end up here. List the firmware file names below.
7296ee19beSMurali Karicheri  * Newest followed by older ones. Search is done from start of the array
7396ee19beSMurali Karicheri  * until a firmware file is found.
7496ee19beSMurali Karicheri  */
7596ee19beSMurali Karicheri const char *knav_acc_firmwares[] = {"ks2_qmss_pdsp_acc48.bin"};
7696ee19beSMurali Karicheri 
77a2dd6877SMurali Karicheri static bool device_ready;
78a2dd6877SMurali Karicheri bool knav_qmss_device_ready(void)
79a2dd6877SMurali Karicheri {
80a2dd6877SMurali Karicheri 	return device_ready;
81a2dd6877SMurali Karicheri }
82a2dd6877SMurali Karicheri EXPORT_SYMBOL_GPL(knav_qmss_device_ready);
83a2dd6877SMurali Karicheri 
8441f93af9SSandeep Nair /**
8541f93af9SSandeep Nair  * knav_queue_notify: qmss queue notfier call
8641f93af9SSandeep Nair  *
8741f93af9SSandeep Nair  * @inst:		qmss queue instance like accumulator
8841f93af9SSandeep Nair  */
8941f93af9SSandeep Nair void knav_queue_notify(struct knav_queue_inst *inst)
9041f93af9SSandeep Nair {
9141f93af9SSandeep Nair 	struct knav_queue *qh;
9241f93af9SSandeep Nair 
9341f93af9SSandeep Nair 	if (!inst)
9441f93af9SSandeep Nair 		return;
9541f93af9SSandeep Nair 
9641f93af9SSandeep Nair 	rcu_read_lock();
9741f93af9SSandeep Nair 	for_each_handle_rcu(qh, inst) {
9841f93af9SSandeep Nair 		if (atomic_read(&qh->notifier_enabled) <= 0)
9941f93af9SSandeep Nair 			continue;
10041f93af9SSandeep Nair 		if (WARN_ON(!qh->notifier_fn))
10141f93af9SSandeep Nair 			continue;
102bc3acbb8SVasyl Gomonovych 		this_cpu_inc(qh->stats->notifies);
10341f93af9SSandeep Nair 		qh->notifier_fn(qh->notifier_fn_arg);
10441f93af9SSandeep Nair 	}
10541f93af9SSandeep Nair 	rcu_read_unlock();
10641f93af9SSandeep Nair }
10741f93af9SSandeep Nair EXPORT_SYMBOL_GPL(knav_queue_notify);
10841f93af9SSandeep Nair 
10941f93af9SSandeep Nair static irqreturn_t knav_queue_int_handler(int irq, void *_instdata)
11041f93af9SSandeep Nair {
11141f93af9SSandeep Nair 	struct knav_queue_inst *inst = _instdata;
11241f93af9SSandeep Nair 
11341f93af9SSandeep Nair 	knav_queue_notify(inst);
11441f93af9SSandeep Nair 	return IRQ_HANDLED;
11541f93af9SSandeep Nair }
11641f93af9SSandeep Nair 
11741f93af9SSandeep Nair static int knav_queue_setup_irq(struct knav_range_info *range,
11841f93af9SSandeep Nair 			  struct knav_queue_inst *inst)
11941f93af9SSandeep Nair {
12041f93af9SSandeep Nair 	unsigned queue = inst->id - range->queue_base;
12141f93af9SSandeep Nair 	int ret = 0, irq;
12241f93af9SSandeep Nair 
12341f93af9SSandeep Nair 	if (range->flags & RANGE_HAS_IRQ) {
12441f93af9SSandeep Nair 		irq = range->irqs[queue].irq;
12541f93af9SSandeep Nair 		ret = request_irq(irq, knav_queue_int_handler, 0,
12641f93af9SSandeep Nair 					inst->irq_name, inst);
12741f93af9SSandeep Nair 		if (ret)
12841f93af9SSandeep Nair 			return ret;
12941f93af9SSandeep Nair 		disable_irq(irq);
130*832ad0e3SMarc Zyngier 		if (range->irqs[queue].cpu_mask) {
131*832ad0e3SMarc Zyngier 			ret = irq_set_affinity_hint(irq, range->irqs[queue].cpu_mask);
13241f93af9SSandeep Nair 			if (ret) {
13341f93af9SSandeep Nair 				dev_warn(range->kdev->dev,
13441f93af9SSandeep Nair 					 "Failed to set IRQ affinity\n");
13541f93af9SSandeep Nair 				return ret;
13641f93af9SSandeep Nair 			}
13741f93af9SSandeep Nair 		}
13841f93af9SSandeep Nair 	}
13941f93af9SSandeep Nair 	return ret;
14041f93af9SSandeep Nair }
14141f93af9SSandeep Nair 
14241f93af9SSandeep Nair static void knav_queue_free_irq(struct knav_queue_inst *inst)
14341f93af9SSandeep Nair {
14441f93af9SSandeep Nair 	struct knav_range_info *range = inst->range;
14541f93af9SSandeep Nair 	unsigned queue = inst->id - inst->range->queue_base;
14641f93af9SSandeep Nair 	int irq;
14741f93af9SSandeep Nair 
14841f93af9SSandeep Nair 	if (range->flags & RANGE_HAS_IRQ) {
14941f93af9SSandeep Nair 		irq = range->irqs[queue].irq;
15041f93af9SSandeep Nair 		irq_set_affinity_hint(irq, NULL);
15141f93af9SSandeep Nair 		free_irq(irq, inst);
15241f93af9SSandeep Nair 	}
15341f93af9SSandeep Nair }
15441f93af9SSandeep Nair 
15541f93af9SSandeep Nair static inline bool knav_queue_is_busy(struct knav_queue_inst *inst)
15641f93af9SSandeep Nair {
15741f93af9SSandeep Nair 	return !list_empty(&inst->handles);
15841f93af9SSandeep Nair }
15941f93af9SSandeep Nair 
16041f93af9SSandeep Nair static inline bool knav_queue_is_reserved(struct knav_queue_inst *inst)
16141f93af9SSandeep Nair {
16241f93af9SSandeep Nair 	return inst->range->flags & RANGE_RESERVED;
16341f93af9SSandeep Nair }
16441f93af9SSandeep Nair 
16541f93af9SSandeep Nair static inline bool knav_queue_is_shared(struct knav_queue_inst *inst)
16641f93af9SSandeep Nair {
16741f93af9SSandeep Nair 	struct knav_queue *tmp;
16841f93af9SSandeep Nair 
16941f93af9SSandeep Nair 	rcu_read_lock();
17041f93af9SSandeep Nair 	for_each_handle_rcu(tmp, inst) {
17141f93af9SSandeep Nair 		if (tmp->flags & KNAV_QUEUE_SHARED) {
17241f93af9SSandeep Nair 			rcu_read_unlock();
17341f93af9SSandeep Nair 			return true;
17441f93af9SSandeep Nair 		}
17541f93af9SSandeep Nair 	}
17641f93af9SSandeep Nair 	rcu_read_unlock();
17741f93af9SSandeep Nair 	return false;
17841f93af9SSandeep Nair }
17941f93af9SSandeep Nair 
18041f93af9SSandeep Nair static inline bool knav_queue_match_type(struct knav_queue_inst *inst,
18141f93af9SSandeep Nair 						unsigned type)
18241f93af9SSandeep Nair {
18341f93af9SSandeep Nair 	if ((type == KNAV_QUEUE_QPEND) &&
18441f93af9SSandeep Nair 	    (inst->range->flags & RANGE_HAS_IRQ)) {
18541f93af9SSandeep Nair 		return true;
18641f93af9SSandeep Nair 	} else if ((type == KNAV_QUEUE_ACC) &&
18741f93af9SSandeep Nair 		(inst->range->flags & RANGE_HAS_ACCUMULATOR)) {
18841f93af9SSandeep Nair 		return true;
18941f93af9SSandeep Nair 	} else if ((type == KNAV_QUEUE_GP) &&
19041f93af9SSandeep Nair 		!(inst->range->flags &
19141f93af9SSandeep Nair 			(RANGE_HAS_ACCUMULATOR | RANGE_HAS_IRQ))) {
19241f93af9SSandeep Nair 		return true;
19341f93af9SSandeep Nair 	}
19441f93af9SSandeep Nair 	return false;
19541f93af9SSandeep Nair }
19641f93af9SSandeep Nair 
19741f93af9SSandeep Nair static inline struct knav_queue_inst *
19841f93af9SSandeep Nair knav_queue_match_id_to_inst(struct knav_device *kdev, unsigned id)
19941f93af9SSandeep Nair {
20041f93af9SSandeep Nair 	struct knav_queue_inst *inst;
20141f93af9SSandeep Nair 	int idx;
20241f93af9SSandeep Nair 
20341f93af9SSandeep Nair 	for_each_instance(idx, inst, kdev) {
20441f93af9SSandeep Nair 		if (inst->id == id)
20541f93af9SSandeep Nair 			return inst;
20641f93af9SSandeep Nair 	}
20741f93af9SSandeep Nair 	return NULL;
20841f93af9SSandeep Nair }
20941f93af9SSandeep Nair 
21041f93af9SSandeep Nair static inline struct knav_queue_inst *knav_queue_find_by_id(int id)
21141f93af9SSandeep Nair {
21241f93af9SSandeep Nair 	if (kdev->base_id <= id &&
21341f93af9SSandeep Nair 	    kdev->base_id + kdev->num_queues > id) {
21441f93af9SSandeep Nair 		id -= kdev->base_id;
21541f93af9SSandeep Nair 		return knav_queue_match_id_to_inst(kdev, id);
21641f93af9SSandeep Nair 	}
21741f93af9SSandeep Nair 	return NULL;
21841f93af9SSandeep Nair }
21941f93af9SSandeep Nair 
22041f93af9SSandeep Nair static struct knav_queue *__knav_queue_open(struct knav_queue_inst *inst,
22141f93af9SSandeep Nair 				      const char *name, unsigned flags)
22241f93af9SSandeep Nair {
22341f93af9SSandeep Nair 	struct knav_queue *qh;
22441f93af9SSandeep Nair 	unsigned id;
22541f93af9SSandeep Nair 	int ret = 0;
22641f93af9SSandeep Nair 
22741f93af9SSandeep Nair 	qh = devm_kzalloc(inst->kdev->dev, sizeof(*qh), GFP_KERNEL);
22841f93af9SSandeep Nair 	if (!qh)
22941f93af9SSandeep Nair 		return ERR_PTR(-ENOMEM);
23041f93af9SSandeep Nair 
231bc3acbb8SVasyl Gomonovych 	qh->stats = alloc_percpu(struct knav_queue_stats);
232bc3acbb8SVasyl Gomonovych 	if (!qh->stats) {
233bc3acbb8SVasyl Gomonovych 		ret = -ENOMEM;
234bc3acbb8SVasyl Gomonovych 		goto err;
235bc3acbb8SVasyl Gomonovych 	}
236bc3acbb8SVasyl Gomonovych 
23741f93af9SSandeep Nair 	qh->flags = flags;
23841f93af9SSandeep Nair 	qh->inst = inst;
23941f93af9SSandeep Nair 	id = inst->id - inst->qmgr->start_queue;
24041f93af9SSandeep Nair 	qh->reg_push = &inst->qmgr->reg_push[id];
24141f93af9SSandeep Nair 	qh->reg_pop = &inst->qmgr->reg_pop[id];
24241f93af9SSandeep Nair 	qh->reg_peek = &inst->qmgr->reg_peek[id];
24341f93af9SSandeep Nair 
24441f93af9SSandeep Nair 	/* first opener? */
24541f93af9SSandeep Nair 	if (!knav_queue_is_busy(inst)) {
24641f93af9SSandeep Nair 		struct knav_range_info *range = inst->range;
24741f93af9SSandeep Nair 
248aefc5818SMa Shimiao 		inst->name = kstrndup(name, KNAV_NAME_SIZE - 1, GFP_KERNEL);
24941f93af9SSandeep Nair 		if (range->ops && range->ops->open_queue)
25041f93af9SSandeep Nair 			ret = range->ops->open_queue(range, inst, flags);
25141f93af9SSandeep Nair 
252bc3acbb8SVasyl Gomonovych 		if (ret)
253bc3acbb8SVasyl Gomonovych 			goto err;
25441f93af9SSandeep Nair 	}
25541f93af9SSandeep Nair 	list_add_tail_rcu(&qh->list, &inst->handles);
25641f93af9SSandeep Nair 	return qh;
257bc3acbb8SVasyl Gomonovych 
258bc3acbb8SVasyl Gomonovych err:
259bc3acbb8SVasyl Gomonovych 	if (qh->stats)
260bc3acbb8SVasyl Gomonovych 		free_percpu(qh->stats);
261bc3acbb8SVasyl Gomonovych 	devm_kfree(inst->kdev->dev, qh);
262bc3acbb8SVasyl Gomonovych 	return ERR_PTR(ret);
26341f93af9SSandeep Nair }
26441f93af9SSandeep Nair 
26541f93af9SSandeep Nair static struct knav_queue *
26641f93af9SSandeep Nair knav_queue_open_by_id(const char *name, unsigned id, unsigned flags)
26741f93af9SSandeep Nair {
26841f93af9SSandeep Nair 	struct knav_queue_inst *inst;
26941f93af9SSandeep Nair 	struct knav_queue *qh;
27041f93af9SSandeep Nair 
27141f93af9SSandeep Nair 	mutex_lock(&knav_dev_lock);
27241f93af9SSandeep Nair 
27341f93af9SSandeep Nair 	qh = ERR_PTR(-ENODEV);
27441f93af9SSandeep Nair 	inst = knav_queue_find_by_id(id);
27541f93af9SSandeep Nair 	if (!inst)
27641f93af9SSandeep Nair 		goto unlock_ret;
27741f93af9SSandeep Nair 
27841f93af9SSandeep Nair 	qh = ERR_PTR(-EEXIST);
27941f93af9SSandeep Nair 	if (!(flags & KNAV_QUEUE_SHARED) && knav_queue_is_busy(inst))
28041f93af9SSandeep Nair 		goto unlock_ret;
28141f93af9SSandeep Nair 
28241f93af9SSandeep Nair 	qh = ERR_PTR(-EBUSY);
28341f93af9SSandeep Nair 	if ((flags & KNAV_QUEUE_SHARED) &&
28441f93af9SSandeep Nair 	    (knav_queue_is_busy(inst) && !knav_queue_is_shared(inst)))
28541f93af9SSandeep Nair 		goto unlock_ret;
28641f93af9SSandeep Nair 
28741f93af9SSandeep Nair 	qh = __knav_queue_open(inst, name, flags);
28841f93af9SSandeep Nair 
28941f93af9SSandeep Nair unlock_ret:
29041f93af9SSandeep Nair 	mutex_unlock(&knav_dev_lock);
29141f93af9SSandeep Nair 
29241f93af9SSandeep Nair 	return qh;
29341f93af9SSandeep Nair }
29441f93af9SSandeep Nair 
29541f93af9SSandeep Nair static struct knav_queue *knav_queue_open_by_type(const char *name,
29641f93af9SSandeep Nair 						unsigned type, unsigned flags)
29741f93af9SSandeep Nair {
29841f93af9SSandeep Nair 	struct knav_queue_inst *inst;
29941f93af9SSandeep Nair 	struct knav_queue *qh = ERR_PTR(-EINVAL);
30041f93af9SSandeep Nair 	int idx;
30141f93af9SSandeep Nair 
30241f93af9SSandeep Nair 	mutex_lock(&knav_dev_lock);
30341f93af9SSandeep Nair 
30441f93af9SSandeep Nair 	for_each_instance(idx, inst, kdev) {
30541f93af9SSandeep Nair 		if (knav_queue_is_reserved(inst))
30641f93af9SSandeep Nair 			continue;
30741f93af9SSandeep Nair 		if (!knav_queue_match_type(inst, type))
30841f93af9SSandeep Nair 			continue;
30941f93af9SSandeep Nair 		if (knav_queue_is_busy(inst))
31041f93af9SSandeep Nair 			continue;
31141f93af9SSandeep Nair 		qh = __knav_queue_open(inst, name, flags);
31241f93af9SSandeep Nair 		goto unlock_ret;
31341f93af9SSandeep Nair 	}
31441f93af9SSandeep Nair 
31541f93af9SSandeep Nair unlock_ret:
31641f93af9SSandeep Nair 	mutex_unlock(&knav_dev_lock);
31741f93af9SSandeep Nair 	return qh;
31841f93af9SSandeep Nair }
31941f93af9SSandeep Nair 
32041f93af9SSandeep Nair static void knav_queue_set_notify(struct knav_queue_inst *inst, bool enabled)
32141f93af9SSandeep Nair {
32241f93af9SSandeep Nair 	struct knav_range_info *range = inst->range;
32341f93af9SSandeep Nair 
32441f93af9SSandeep Nair 	if (range->ops && range->ops->set_notify)
32541f93af9SSandeep Nair 		range->ops->set_notify(range, inst, enabled);
32641f93af9SSandeep Nair }
32741f93af9SSandeep Nair 
32841f93af9SSandeep Nair static int knav_queue_enable_notifier(struct knav_queue *qh)
32941f93af9SSandeep Nair {
33041f93af9SSandeep Nair 	struct knav_queue_inst *inst = qh->inst;
33141f93af9SSandeep Nair 	bool first;
33241f93af9SSandeep Nair 
33341f93af9SSandeep Nair 	if (WARN_ON(!qh->notifier_fn))
33441f93af9SSandeep Nair 		return -EINVAL;
33541f93af9SSandeep Nair 
33641f93af9SSandeep Nair 	/* Adjust the per handle notifier count */
33741f93af9SSandeep Nair 	first = (atomic_inc_return(&qh->notifier_enabled) == 1);
33841f93af9SSandeep Nair 	if (!first)
33941f93af9SSandeep Nair 		return 0; /* nothing to do */
34041f93af9SSandeep Nair 
34141f93af9SSandeep Nair 	/* Now adjust the per instance notifier count */
34241f93af9SSandeep Nair 	first = (atomic_inc_return(&inst->num_notifiers) == 1);
34341f93af9SSandeep Nair 	if (first)
34441f93af9SSandeep Nair 		knav_queue_set_notify(inst, true);
34541f93af9SSandeep Nair 
34641f93af9SSandeep Nair 	return 0;
34741f93af9SSandeep Nair }
34841f93af9SSandeep Nair 
34941f93af9SSandeep Nair static int knav_queue_disable_notifier(struct knav_queue *qh)
35041f93af9SSandeep Nair {
35141f93af9SSandeep Nair 	struct knav_queue_inst *inst = qh->inst;
35241f93af9SSandeep Nair 	bool last;
35341f93af9SSandeep Nair 
35441f93af9SSandeep Nair 	last = (atomic_dec_return(&qh->notifier_enabled) == 0);
35541f93af9SSandeep Nair 	if (!last)
35641f93af9SSandeep Nair 		return 0; /* nothing to do */
35741f93af9SSandeep Nair 
35841f93af9SSandeep Nair 	last = (atomic_dec_return(&inst->num_notifiers) == 0);
35941f93af9SSandeep Nair 	if (last)
36041f93af9SSandeep Nair 		knav_queue_set_notify(inst, false);
36141f93af9SSandeep Nair 
36241f93af9SSandeep Nair 	return 0;
36341f93af9SSandeep Nair }
36441f93af9SSandeep Nair 
36541f93af9SSandeep Nair static int knav_queue_set_notifier(struct knav_queue *qh,
36641f93af9SSandeep Nair 				struct knav_queue_notify_config *cfg)
36741f93af9SSandeep Nair {
36841f93af9SSandeep Nair 	knav_queue_notify_fn old_fn = qh->notifier_fn;
36941f93af9SSandeep Nair 
37041f93af9SSandeep Nair 	if (!cfg)
37141f93af9SSandeep Nair 		return -EINVAL;
37241f93af9SSandeep Nair 
37341f93af9SSandeep Nair 	if (!(qh->inst->range->flags & (RANGE_HAS_ACCUMULATOR | RANGE_HAS_IRQ)))
37441f93af9SSandeep Nair 		return -ENOTSUPP;
37541f93af9SSandeep Nair 
37641f93af9SSandeep Nair 	if (!cfg->fn && old_fn)
37741f93af9SSandeep Nair 		knav_queue_disable_notifier(qh);
37841f93af9SSandeep Nair 
37941f93af9SSandeep Nair 	qh->notifier_fn = cfg->fn;
38041f93af9SSandeep Nair 	qh->notifier_fn_arg = cfg->fn_arg;
38141f93af9SSandeep Nair 
38241f93af9SSandeep Nair 	if (cfg->fn && !old_fn)
38341f93af9SSandeep Nair 		knav_queue_enable_notifier(qh);
38441f93af9SSandeep Nair 
38541f93af9SSandeep Nair 	return 0;
38641f93af9SSandeep Nair }
38741f93af9SSandeep Nair 
38841f93af9SSandeep Nair static int knav_gp_set_notify(struct knav_range_info *range,
38941f93af9SSandeep Nair 			       struct knav_queue_inst *inst,
39041f93af9SSandeep Nair 			       bool enabled)
39141f93af9SSandeep Nair {
39241f93af9SSandeep Nair 	unsigned queue;
39341f93af9SSandeep Nair 
39441f93af9SSandeep Nair 	if (range->flags & RANGE_HAS_IRQ) {
39541f93af9SSandeep Nair 		queue = inst->id - range->queue_base;
39641f93af9SSandeep Nair 		if (enabled)
39741f93af9SSandeep Nair 			enable_irq(range->irqs[queue].irq);
39841f93af9SSandeep Nair 		else
39941f93af9SSandeep Nair 			disable_irq_nosync(range->irqs[queue].irq);
40041f93af9SSandeep Nair 	}
40141f93af9SSandeep Nair 	return 0;
40241f93af9SSandeep Nair }
40341f93af9SSandeep Nair 
40441f93af9SSandeep Nair static int knav_gp_open_queue(struct knav_range_info *range,
40541f93af9SSandeep Nair 				struct knav_queue_inst *inst, unsigned flags)
40641f93af9SSandeep Nair {
40741f93af9SSandeep Nair 	return knav_queue_setup_irq(range, inst);
40841f93af9SSandeep Nair }
40941f93af9SSandeep Nair 
41041f93af9SSandeep Nair static int knav_gp_close_queue(struct knav_range_info *range,
41141f93af9SSandeep Nair 				struct knav_queue_inst *inst)
41241f93af9SSandeep Nair {
41341f93af9SSandeep Nair 	knav_queue_free_irq(inst);
41441f93af9SSandeep Nair 	return 0;
41541f93af9SSandeep Nair }
41641f93af9SSandeep Nair 
41741f93af9SSandeep Nair struct knav_range_ops knav_gp_range_ops = {
41841f93af9SSandeep Nair 	.set_notify	= knav_gp_set_notify,
41941f93af9SSandeep Nair 	.open_queue	= knav_gp_open_queue,
42041f93af9SSandeep Nair 	.close_queue	= knav_gp_close_queue,
42141f93af9SSandeep Nair };
42241f93af9SSandeep Nair 
42341f93af9SSandeep Nair 
42441f93af9SSandeep Nair static int knav_queue_get_count(void *qhandle)
42541f93af9SSandeep Nair {
42641f93af9SSandeep Nair 	struct knav_queue *qh = qhandle;
42741f93af9SSandeep Nair 	struct knav_queue_inst *inst = qh->inst;
42841f93af9SSandeep Nair 
42941f93af9SSandeep Nair 	return readl_relaxed(&qh->reg_peek[0].entry_count) +
43041f93af9SSandeep Nair 		atomic_read(&inst->desc_count);
43141f93af9SSandeep Nair }
43241f93af9SSandeep Nair 
43341f93af9SSandeep Nair static void knav_queue_debug_show_instance(struct seq_file *s,
43441f93af9SSandeep Nair 					struct knav_queue_inst *inst)
43541f93af9SSandeep Nair {
43641f93af9SSandeep Nair 	struct knav_device *kdev = inst->kdev;
43741f93af9SSandeep Nair 	struct knav_queue *qh;
438bc3acbb8SVasyl Gomonovych 	int cpu = 0;
439bc3acbb8SVasyl Gomonovych 	int pushes = 0;
440bc3acbb8SVasyl Gomonovych 	int pops = 0;
441bc3acbb8SVasyl Gomonovych 	int push_errors = 0;
442bc3acbb8SVasyl Gomonovych 	int pop_errors = 0;
443bc3acbb8SVasyl Gomonovych 	int notifies = 0;
44441f93af9SSandeep Nair 
44541f93af9SSandeep Nair 	if (!knav_queue_is_busy(inst))
44641f93af9SSandeep Nair 		return;
44741f93af9SSandeep Nair 
44841f93af9SSandeep Nair 	seq_printf(s, "\tqueue id %d (%s)\n",
44941f93af9SSandeep Nair 		   kdev->base_id + inst->id, inst->name);
45041f93af9SSandeep Nair 	for_each_handle_rcu(qh, inst) {
451bc3acbb8SVasyl Gomonovych 		for_each_possible_cpu(cpu) {
452bc3acbb8SVasyl Gomonovych 			pushes += per_cpu_ptr(qh->stats, cpu)->pushes;
453bc3acbb8SVasyl Gomonovych 			pops += per_cpu_ptr(qh->stats, cpu)->pops;
454bc3acbb8SVasyl Gomonovych 			push_errors += per_cpu_ptr(qh->stats, cpu)->push_errors;
455bc3acbb8SVasyl Gomonovych 			pop_errors += per_cpu_ptr(qh->stats, cpu)->pop_errors;
456bc3acbb8SVasyl Gomonovych 			notifies += per_cpu_ptr(qh->stats, cpu)->notifies;
457bc3acbb8SVasyl Gomonovych 		}
458bc3acbb8SVasyl Gomonovych 
459bc3acbb8SVasyl Gomonovych 		seq_printf(s, "\t\thandle %p: pushes %8d, pops %8d, count %8d, notifies %8d, push errors %8d, pop errors %8d\n",
460bc3acbb8SVasyl Gomonovych 				qh,
461bc3acbb8SVasyl Gomonovych 				pushes,
462bc3acbb8SVasyl Gomonovych 				pops,
463bc3acbb8SVasyl Gomonovych 				knav_queue_get_count(qh),
464bc3acbb8SVasyl Gomonovych 				notifies,
465bc3acbb8SVasyl Gomonovych 				push_errors,
466bc3acbb8SVasyl Gomonovych 				pop_errors);
46741f93af9SSandeep Nair 	}
46841f93af9SSandeep Nair }
46941f93af9SSandeep Nair 
47041f93af9SSandeep Nair static int knav_queue_debug_show(struct seq_file *s, void *v)
47141f93af9SSandeep Nair {
47241f93af9SSandeep Nair 	struct knav_queue_inst *inst;
47341f93af9SSandeep Nair 	int idx;
47441f93af9SSandeep Nair 
47541f93af9SSandeep Nair 	mutex_lock(&knav_dev_lock);
47641f93af9SSandeep Nair 	seq_printf(s, "%s: %u-%u\n",
47741f93af9SSandeep Nair 		   dev_name(kdev->dev), kdev->base_id,
47841f93af9SSandeep Nair 		   kdev->base_id + kdev->num_queues - 1);
47941f93af9SSandeep Nair 	for_each_instance(idx, inst, kdev)
48041f93af9SSandeep Nair 		knav_queue_debug_show_instance(s, inst);
48141f93af9SSandeep Nair 	mutex_unlock(&knav_dev_lock);
48241f93af9SSandeep Nair 
48341f93af9SSandeep Nair 	return 0;
48441f93af9SSandeep Nair }
48541f93af9SSandeep Nair 
48641f93af9SSandeep Nair static int knav_queue_debug_open(struct inode *inode, struct file *file)
48741f93af9SSandeep Nair {
48841f93af9SSandeep Nair 	return single_open(file, knav_queue_debug_show, NULL);
48941f93af9SSandeep Nair }
49041f93af9SSandeep Nair 
49141f93af9SSandeep Nair static const struct file_operations knav_queue_debug_ops = {
49241f93af9SSandeep Nair 	.open		= knav_queue_debug_open,
49341f93af9SSandeep Nair 	.read		= seq_read,
49441f93af9SSandeep Nair 	.llseek		= seq_lseek,
49541f93af9SSandeep Nair 	.release	= single_release,
49641f93af9SSandeep Nair };
49741f93af9SSandeep Nair 
49841f93af9SSandeep Nair static inline int knav_queue_pdsp_wait(u32 * __iomem addr, unsigned timeout,
49941f93af9SSandeep Nair 					u32 flags)
50041f93af9SSandeep Nair {
50141f93af9SSandeep Nair 	unsigned long end;
50241f93af9SSandeep Nair 	u32 val = 0;
50341f93af9SSandeep Nair 
50441f93af9SSandeep Nair 	end = jiffies + msecs_to_jiffies(timeout);
50541f93af9SSandeep Nair 	while (time_after(end, jiffies)) {
50641f93af9SSandeep Nair 		val = readl_relaxed(addr);
50741f93af9SSandeep Nair 		if (flags)
50841f93af9SSandeep Nair 			val &= flags;
50941f93af9SSandeep Nair 		if (!val)
51041f93af9SSandeep Nair 			break;
51141f93af9SSandeep Nair 		cpu_relax();
51241f93af9SSandeep Nair 	}
51341f93af9SSandeep Nair 	return val ? -ETIMEDOUT : 0;
51441f93af9SSandeep Nair }
51541f93af9SSandeep Nair 
51641f93af9SSandeep Nair 
51741f93af9SSandeep Nair static int knav_queue_flush(struct knav_queue *qh)
51841f93af9SSandeep Nair {
51941f93af9SSandeep Nair 	struct knav_queue_inst *inst = qh->inst;
52041f93af9SSandeep Nair 	unsigned id = inst->id - inst->qmgr->start_queue;
52141f93af9SSandeep Nair 
52241f93af9SSandeep Nair 	atomic_set(&inst->desc_count, 0);
52341f93af9SSandeep Nair 	writel_relaxed(0, &inst->qmgr->reg_push[id].ptr_size_thresh);
52441f93af9SSandeep Nair 	return 0;
52541f93af9SSandeep Nair }
52641f93af9SSandeep Nair 
52741f93af9SSandeep Nair /**
52841f93af9SSandeep Nair  * knav_queue_open()	- open a hardware queue
52941f93af9SSandeep Nair  * @name		- name to give the queue handle
53041f93af9SSandeep Nair  * @id			- desired queue number if any or specifes the type
53141f93af9SSandeep Nair  *			  of queue
53241f93af9SSandeep Nair  * @flags		- the following flags are applicable to queues:
53341f93af9SSandeep Nair  *	KNAV_QUEUE_SHARED - allow the queue to be shared. Queues are
53441f93af9SSandeep Nair  *			     exclusive by default.
53541f93af9SSandeep Nair  *			     Subsequent attempts to open a shared queue should
53641f93af9SSandeep Nair  *			     also have this flag.
53741f93af9SSandeep Nair  *
53841f93af9SSandeep Nair  * Returns a handle to the open hardware queue if successful. Use IS_ERR()
53941f93af9SSandeep Nair  * to check the returned value for error codes.
54041f93af9SSandeep Nair  */
54141f93af9SSandeep Nair void *knav_queue_open(const char *name, unsigned id,
54241f93af9SSandeep Nair 					unsigned flags)
54341f93af9SSandeep Nair {
54441f93af9SSandeep Nair 	struct knav_queue *qh = ERR_PTR(-EINVAL);
54541f93af9SSandeep Nair 
54641f93af9SSandeep Nair 	switch (id) {
54741f93af9SSandeep Nair 	case KNAV_QUEUE_QPEND:
54841f93af9SSandeep Nair 	case KNAV_QUEUE_ACC:
54941f93af9SSandeep Nair 	case KNAV_QUEUE_GP:
55041f93af9SSandeep Nair 		qh = knav_queue_open_by_type(name, id, flags);
55141f93af9SSandeep Nair 		break;
55241f93af9SSandeep Nair 
55341f93af9SSandeep Nair 	default:
55441f93af9SSandeep Nair 		qh = knav_queue_open_by_id(name, id, flags);
55541f93af9SSandeep Nair 		break;
55641f93af9SSandeep Nair 	}
55741f93af9SSandeep Nair 	return qh;
55841f93af9SSandeep Nair }
55941f93af9SSandeep Nair EXPORT_SYMBOL_GPL(knav_queue_open);
56041f93af9SSandeep Nair 
56141f93af9SSandeep Nair /**
56241f93af9SSandeep Nair  * knav_queue_close()	- close a hardware queue handle
56341f93af9SSandeep Nair  * @qh			- handle to close
56441f93af9SSandeep Nair  */
56541f93af9SSandeep Nair void knav_queue_close(void *qhandle)
56641f93af9SSandeep Nair {
56741f93af9SSandeep Nair 	struct knav_queue *qh = qhandle;
56841f93af9SSandeep Nair 	struct knav_queue_inst *inst = qh->inst;
56941f93af9SSandeep Nair 
57041f93af9SSandeep Nair 	while (atomic_read(&qh->notifier_enabled) > 0)
57141f93af9SSandeep Nair 		knav_queue_disable_notifier(qh);
57241f93af9SSandeep Nair 
57341f93af9SSandeep Nair 	mutex_lock(&knav_dev_lock);
57441f93af9SSandeep Nair 	list_del_rcu(&qh->list);
57541f93af9SSandeep Nair 	mutex_unlock(&knav_dev_lock);
57641f93af9SSandeep Nair 	synchronize_rcu();
57741f93af9SSandeep Nair 	if (!knav_queue_is_busy(inst)) {
57841f93af9SSandeep Nair 		struct knav_range_info *range = inst->range;
57941f93af9SSandeep Nair 
58041f93af9SSandeep Nair 		if (range->ops && range->ops->close_queue)
58141f93af9SSandeep Nair 			range->ops->close_queue(range, inst);
58241f93af9SSandeep Nair 	}
583bc3acbb8SVasyl Gomonovych 	free_percpu(qh->stats);
58441f93af9SSandeep Nair 	devm_kfree(inst->kdev->dev, qh);
58541f93af9SSandeep Nair }
58641f93af9SSandeep Nair EXPORT_SYMBOL_GPL(knav_queue_close);
58741f93af9SSandeep Nair 
58841f93af9SSandeep Nair /**
58941f93af9SSandeep Nair  * knav_queue_device_control()	- Perform control operations on a queue
59041f93af9SSandeep Nair  * @qh				- queue handle
59141f93af9SSandeep Nair  * @cmd				- control commands
59241f93af9SSandeep Nair  * @arg				- command argument
59341f93af9SSandeep Nair  *
59441f93af9SSandeep Nair  * Returns 0 on success, errno otherwise.
59541f93af9SSandeep Nair  */
59641f93af9SSandeep Nair int knav_queue_device_control(void *qhandle, enum knav_queue_ctrl_cmd cmd,
59741f93af9SSandeep Nair 				unsigned long arg)
59841f93af9SSandeep Nair {
59941f93af9SSandeep Nair 	struct knav_queue *qh = qhandle;
60041f93af9SSandeep Nair 	struct knav_queue_notify_config *cfg;
60141f93af9SSandeep Nair 	int ret;
60241f93af9SSandeep Nair 
60341f93af9SSandeep Nair 	switch ((int)cmd) {
60441f93af9SSandeep Nair 	case KNAV_QUEUE_GET_ID:
60541f93af9SSandeep Nair 		ret = qh->inst->kdev->base_id + qh->inst->id;
60641f93af9SSandeep Nair 		break;
60741f93af9SSandeep Nair 
60841f93af9SSandeep Nair 	case KNAV_QUEUE_FLUSH:
60941f93af9SSandeep Nair 		ret = knav_queue_flush(qh);
61041f93af9SSandeep Nair 		break;
61141f93af9SSandeep Nair 
61241f93af9SSandeep Nair 	case KNAV_QUEUE_SET_NOTIFIER:
61341f93af9SSandeep Nair 		cfg = (void *)arg;
61441f93af9SSandeep Nair 		ret = knav_queue_set_notifier(qh, cfg);
61541f93af9SSandeep Nair 		break;
61641f93af9SSandeep Nair 
61741f93af9SSandeep Nair 	case KNAV_QUEUE_ENABLE_NOTIFY:
61841f93af9SSandeep Nair 		ret = knav_queue_enable_notifier(qh);
61941f93af9SSandeep Nair 		break;
62041f93af9SSandeep Nair 
62141f93af9SSandeep Nair 	case KNAV_QUEUE_DISABLE_NOTIFY:
62241f93af9SSandeep Nair 		ret = knav_queue_disable_notifier(qh);
62341f93af9SSandeep Nair 		break;
62441f93af9SSandeep Nair 
62541f93af9SSandeep Nair 	case KNAV_QUEUE_GET_COUNT:
62641f93af9SSandeep Nair 		ret = knav_queue_get_count(qh);
62741f93af9SSandeep Nair 		break;
62841f93af9SSandeep Nair 
62941f93af9SSandeep Nair 	default:
63041f93af9SSandeep Nair 		ret = -ENOTSUPP;
63141f93af9SSandeep Nair 		break;
63241f93af9SSandeep Nair 	}
63341f93af9SSandeep Nair 	return ret;
63441f93af9SSandeep Nair }
63541f93af9SSandeep Nair EXPORT_SYMBOL_GPL(knav_queue_device_control);
63641f93af9SSandeep Nair 
63741f93af9SSandeep Nair 
63841f93af9SSandeep Nair 
63941f93af9SSandeep Nair /**
64041f93af9SSandeep Nair  * knav_queue_push()	- push data (or descriptor) to the tail of a queue
64141f93af9SSandeep Nair  * @qh			- hardware queue handle
64241f93af9SSandeep Nair  * @data		- data to push
64341f93af9SSandeep Nair  * @size		- size of data to push
64441f93af9SSandeep Nair  * @flags		- can be used to pass additional information
64541f93af9SSandeep Nair  *
64641f93af9SSandeep Nair  * Returns 0 on success, errno otherwise.
64741f93af9SSandeep Nair  */
64841f93af9SSandeep Nair int knav_queue_push(void *qhandle, dma_addr_t dma,
64941f93af9SSandeep Nair 					unsigned size, unsigned flags)
65041f93af9SSandeep Nair {
65141f93af9SSandeep Nair 	struct knav_queue *qh = qhandle;
65241f93af9SSandeep Nair 	u32 val;
65341f93af9SSandeep Nair 
65441f93af9SSandeep Nair 	val = (u32)dma | ((size / 16) - 1);
65541f93af9SSandeep Nair 	writel_relaxed(val, &qh->reg_push[0].ptr_size_thresh);
65641f93af9SSandeep Nair 
657bc3acbb8SVasyl Gomonovych 	this_cpu_inc(qh->stats->pushes);
65841f93af9SSandeep Nair 	return 0;
65941f93af9SSandeep Nair }
66097f39af4SMurali Karicheri EXPORT_SYMBOL_GPL(knav_queue_push);
66141f93af9SSandeep Nair 
66241f93af9SSandeep Nair /**
66341f93af9SSandeep Nair  * knav_queue_pop()	- pop data (or descriptor) from the head of a queue
66441f93af9SSandeep Nair  * @qh			- hardware queue handle
66541f93af9SSandeep Nair  * @size		- (optional) size of the data pop'ed.
66641f93af9SSandeep Nair  *
66741f93af9SSandeep Nair  * Returns a DMA address on success, 0 on failure.
66841f93af9SSandeep Nair  */
66941f93af9SSandeep Nair dma_addr_t knav_queue_pop(void *qhandle, unsigned *size)
67041f93af9SSandeep Nair {
67141f93af9SSandeep Nair 	struct knav_queue *qh = qhandle;
67241f93af9SSandeep Nair 	struct knav_queue_inst *inst = qh->inst;
67341f93af9SSandeep Nair 	dma_addr_t dma;
67441f93af9SSandeep Nair 	u32 val, idx;
67541f93af9SSandeep Nair 
67641f93af9SSandeep Nair 	/* are we accumulated? */
67741f93af9SSandeep Nair 	if (inst->descs) {
67841f93af9SSandeep Nair 		if (unlikely(atomic_dec_return(&inst->desc_count) < 0)) {
67941f93af9SSandeep Nair 			atomic_inc(&inst->desc_count);
68041f93af9SSandeep Nair 			return 0;
68141f93af9SSandeep Nair 		}
68241f93af9SSandeep Nair 		idx  = atomic_inc_return(&inst->desc_head);
68341f93af9SSandeep Nair 		idx &= ACC_DESCS_MASK;
68441f93af9SSandeep Nair 		val = inst->descs[idx];
68541f93af9SSandeep Nair 	} else {
68641f93af9SSandeep Nair 		val = readl_relaxed(&qh->reg_pop[0].ptr_size_thresh);
68741f93af9SSandeep Nair 		if (unlikely(!val))
68841f93af9SSandeep Nair 			return 0;
68941f93af9SSandeep Nair 	}
69041f93af9SSandeep Nair 
69141f93af9SSandeep Nair 	dma = val & DESC_PTR_MASK;
69241f93af9SSandeep Nair 	if (size)
69341f93af9SSandeep Nair 		*size = ((val & DESC_SIZE_MASK) + 1) * 16;
69441f93af9SSandeep Nair 
695bc3acbb8SVasyl Gomonovych 	this_cpu_inc(qh->stats->pops);
69641f93af9SSandeep Nair 	return dma;
69741f93af9SSandeep Nair }
69897f39af4SMurali Karicheri EXPORT_SYMBOL_GPL(knav_queue_pop);
69941f93af9SSandeep Nair 
70041f93af9SSandeep Nair /* carve out descriptors and push into queue */
70141f93af9SSandeep Nair static void kdesc_fill_pool(struct knav_pool *pool)
70241f93af9SSandeep Nair {
70341f93af9SSandeep Nair 	struct knav_region *region;
70441f93af9SSandeep Nair 	int i;
70541f93af9SSandeep Nair 
70641f93af9SSandeep Nair 	region = pool->region;
70741f93af9SSandeep Nair 	pool->desc_size = region->desc_size;
70841f93af9SSandeep Nair 	for (i = 0; i < pool->num_desc; i++) {
70941f93af9SSandeep Nair 		int index = pool->region_offset + i;
71041f93af9SSandeep Nair 		dma_addr_t dma_addr;
71141f93af9SSandeep Nair 		unsigned dma_size;
71241f93af9SSandeep Nair 		dma_addr = region->dma_start + (region->desc_size * index);
71341f93af9SSandeep Nair 		dma_size = ALIGN(pool->desc_size, SMP_CACHE_BYTES);
71441f93af9SSandeep Nair 		dma_sync_single_for_device(pool->dev, dma_addr, dma_size,
71541f93af9SSandeep Nair 					   DMA_TO_DEVICE);
71641f93af9SSandeep Nair 		knav_queue_push(pool->queue, dma_addr, dma_size, 0);
71741f93af9SSandeep Nair 	}
71841f93af9SSandeep Nair }
71941f93af9SSandeep Nair 
72041f93af9SSandeep Nair /* pop out descriptors and close the queue */
72141f93af9SSandeep Nair static void kdesc_empty_pool(struct knav_pool *pool)
72241f93af9SSandeep Nair {
72341f93af9SSandeep Nair 	dma_addr_t dma;
72441f93af9SSandeep Nair 	unsigned size;
72541f93af9SSandeep Nair 	void *desc;
72641f93af9SSandeep Nair 	int i;
72741f93af9SSandeep Nair 
72841f93af9SSandeep Nair 	if (!pool->queue)
72941f93af9SSandeep Nair 		return;
73041f93af9SSandeep Nair 
73141f93af9SSandeep Nair 	for (i = 0;; i++) {
73241f93af9SSandeep Nair 		dma = knav_queue_pop(pool->queue, &size);
73341f93af9SSandeep Nair 		if (!dma)
73441f93af9SSandeep Nair 			break;
73541f93af9SSandeep Nair 		desc = knav_pool_desc_dma_to_virt(pool, dma);
73641f93af9SSandeep Nair 		if (!desc) {
73741f93af9SSandeep Nair 			dev_dbg(pool->kdev->dev,
73841f93af9SSandeep Nair 				"couldn't unmap desc, continuing\n");
73941f93af9SSandeep Nair 			continue;
74041f93af9SSandeep Nair 		}
74141f93af9SSandeep Nair 	}
74241f93af9SSandeep Nair 	WARN_ON(i != pool->num_desc);
74341f93af9SSandeep Nair 	knav_queue_close(pool->queue);
74441f93af9SSandeep Nair }
74541f93af9SSandeep Nair 
74641f93af9SSandeep Nair 
74741f93af9SSandeep Nair /* Get the DMA address of a descriptor */
74841f93af9SSandeep Nair dma_addr_t knav_pool_desc_virt_to_dma(void *ph, void *virt)
74941f93af9SSandeep Nair {
75041f93af9SSandeep Nair 	struct knav_pool *pool = ph;
75141f93af9SSandeep Nair 	return pool->region->dma_start + (virt - pool->region->virt_start);
75241f93af9SSandeep Nair }
75397f39af4SMurali Karicheri EXPORT_SYMBOL_GPL(knav_pool_desc_virt_to_dma);
75441f93af9SSandeep Nair 
75541f93af9SSandeep Nair void *knav_pool_desc_dma_to_virt(void *ph, dma_addr_t dma)
75641f93af9SSandeep Nair {
75741f93af9SSandeep Nair 	struct knav_pool *pool = ph;
75841f93af9SSandeep Nair 	return pool->region->virt_start + (dma - pool->region->dma_start);
75941f93af9SSandeep Nair }
76097f39af4SMurali Karicheri EXPORT_SYMBOL_GPL(knav_pool_desc_dma_to_virt);
76141f93af9SSandeep Nair 
76241f93af9SSandeep Nair /**
76341f93af9SSandeep Nair  * knav_pool_create()	- Create a pool of descriptors
76441f93af9SSandeep Nair  * @name		- name to give the pool handle
76541f93af9SSandeep Nair  * @num_desc		- numbers of descriptors in the pool
76641f93af9SSandeep Nair  * @region_id		- QMSS region id from which the descriptors are to be
76741f93af9SSandeep Nair  *			  allocated.
76841f93af9SSandeep Nair  *
76941f93af9SSandeep Nair  * Returns a pool handle on success.
77041f93af9SSandeep Nair  * Use IS_ERR_OR_NULL() to identify error values on return.
77141f93af9SSandeep Nair  */
77241f93af9SSandeep Nair void *knav_pool_create(const char *name,
77341f93af9SSandeep Nair 					int num_desc, int region_id)
77441f93af9SSandeep Nair {
77541f93af9SSandeep Nair 	struct knav_region *reg_itr, *region = NULL;
77641f93af9SSandeep Nair 	struct knav_pool *pool, *pi;
77741f93af9SSandeep Nair 	struct list_head *node;
77841f93af9SSandeep Nair 	unsigned last_offset;
77941f93af9SSandeep Nair 	bool slot_found;
78041f93af9SSandeep Nair 	int ret;
78141f93af9SSandeep Nair 
7824459398bSKeerthy 	if (!kdev)
7834459398bSKeerthy 		return ERR_PTR(-EPROBE_DEFER);
7844459398bSKeerthy 
78541f93af9SSandeep Nair 	if (!kdev->dev)
78641f93af9SSandeep Nair 		return ERR_PTR(-ENODEV);
78741f93af9SSandeep Nair 
78841f93af9SSandeep Nair 	pool = devm_kzalloc(kdev->dev, sizeof(*pool), GFP_KERNEL);
78941f93af9SSandeep Nair 	if (!pool) {
79041f93af9SSandeep Nair 		dev_err(kdev->dev, "out of memory allocating pool\n");
79141f93af9SSandeep Nair 		return ERR_PTR(-ENOMEM);
79241f93af9SSandeep Nair 	}
79341f93af9SSandeep Nair 
79441f93af9SSandeep Nair 	for_each_region(kdev, reg_itr) {
79541f93af9SSandeep Nair 		if (reg_itr->id != region_id)
79641f93af9SSandeep Nair 			continue;
79741f93af9SSandeep Nair 		region = reg_itr;
79841f93af9SSandeep Nair 		break;
79941f93af9SSandeep Nair 	}
80041f93af9SSandeep Nair 
80141f93af9SSandeep Nair 	if (!region) {
80241f93af9SSandeep Nair 		dev_err(kdev->dev, "region-id(%d) not found\n", region_id);
80341f93af9SSandeep Nair 		ret = -EINVAL;
80441f93af9SSandeep Nair 		goto err;
80541f93af9SSandeep Nair 	}
80641f93af9SSandeep Nair 
80741f93af9SSandeep Nair 	pool->queue = knav_queue_open(name, KNAV_QUEUE_GP, 0);
80841f93af9SSandeep Nair 	if (IS_ERR_OR_NULL(pool->queue)) {
80941f93af9SSandeep Nair 		dev_err(kdev->dev,
81041f93af9SSandeep Nair 			"failed to open queue for pool(%s), error %ld\n",
81141f93af9SSandeep Nair 			name, PTR_ERR(pool->queue));
81241f93af9SSandeep Nair 		ret = PTR_ERR(pool->queue);
81341f93af9SSandeep Nair 		goto err;
81441f93af9SSandeep Nair 	}
81541f93af9SSandeep Nair 
816aefc5818SMa Shimiao 	pool->name = kstrndup(name, KNAV_NAME_SIZE - 1, GFP_KERNEL);
81741f93af9SSandeep Nair 	pool->kdev = kdev;
81841f93af9SSandeep Nair 	pool->dev = kdev->dev;
81941f93af9SSandeep Nair 
82041f93af9SSandeep Nair 	mutex_lock(&knav_dev_lock);
82141f93af9SSandeep Nair 
82241f93af9SSandeep Nair 	if (num_desc > (region->num_desc - region->used_desc)) {
82341f93af9SSandeep Nair 		dev_err(kdev->dev, "out of descs in region(%d) for pool(%s)\n",
82441f93af9SSandeep Nair 			region_id, name);
82541f93af9SSandeep Nair 		ret = -ENOMEM;
826ea6d4c07SAxel Lin 		goto err_unlock;
82741f93af9SSandeep Nair 	}
82841f93af9SSandeep Nair 
82941f93af9SSandeep Nair 	/* Region maintains a sorted (by region offset) list of pools
83041f93af9SSandeep Nair 	 * use the first free slot which is large enough to accomodate
83141f93af9SSandeep Nair 	 * the request
83241f93af9SSandeep Nair 	 */
83341f93af9SSandeep Nair 	last_offset = 0;
83441f93af9SSandeep Nair 	slot_found = false;
83541f93af9SSandeep Nair 	node = &region->pools;
83641f93af9SSandeep Nair 	list_for_each_entry(pi, &region->pools, region_inst) {
83741f93af9SSandeep Nair 		if ((pi->region_offset - last_offset) >= num_desc) {
83841f93af9SSandeep Nair 			slot_found = true;
83941f93af9SSandeep Nair 			break;
84041f93af9SSandeep Nair 		}
84141f93af9SSandeep Nair 		last_offset = pi->region_offset + pi->num_desc;
84241f93af9SSandeep Nair 	}
84341f93af9SSandeep Nair 	node = &pi->region_inst;
84441f93af9SSandeep Nair 
84541f93af9SSandeep Nair 	if (slot_found) {
84641f93af9SSandeep Nair 		pool->region = region;
84741f93af9SSandeep Nair 		pool->num_desc = num_desc;
84841f93af9SSandeep Nair 		pool->region_offset = last_offset;
84941f93af9SSandeep Nair 		region->used_desc += num_desc;
85041f93af9SSandeep Nair 		list_add_tail(&pool->list, &kdev->pools);
85141f93af9SSandeep Nair 		list_add_tail(&pool->region_inst, node);
85241f93af9SSandeep Nair 	} else {
85341f93af9SSandeep Nair 		dev_err(kdev->dev, "pool(%s) create failed: fragmented desc pool in region(%d)\n",
85441f93af9SSandeep Nair 			name, region_id);
85541f93af9SSandeep Nair 		ret = -ENOMEM;
856ea6d4c07SAxel Lin 		goto err_unlock;
85741f93af9SSandeep Nair 	}
85841f93af9SSandeep Nair 
85941f93af9SSandeep Nair 	mutex_unlock(&knav_dev_lock);
86041f93af9SSandeep Nair 	kdesc_fill_pool(pool);
86141f93af9SSandeep Nair 	return pool;
86241f93af9SSandeep Nair 
863ea6d4c07SAxel Lin err_unlock:
86441f93af9SSandeep Nair 	mutex_unlock(&knav_dev_lock);
865ea6d4c07SAxel Lin err:
86641f93af9SSandeep Nair 	kfree(pool->name);
86741f93af9SSandeep Nair 	devm_kfree(kdev->dev, pool);
86841f93af9SSandeep Nair 	return ERR_PTR(ret);
86941f93af9SSandeep Nair }
87041f93af9SSandeep Nair EXPORT_SYMBOL_GPL(knav_pool_create);
87141f93af9SSandeep Nair 
87241f93af9SSandeep Nair /**
87341f93af9SSandeep Nair  * knav_pool_destroy()	- Free a pool of descriptors
87441f93af9SSandeep Nair  * @pool		- pool handle
87541f93af9SSandeep Nair  */
87641f93af9SSandeep Nair void knav_pool_destroy(void *ph)
87741f93af9SSandeep Nair {
87841f93af9SSandeep Nair 	struct knav_pool *pool = ph;
87941f93af9SSandeep Nair 
88041f93af9SSandeep Nair 	if (!pool)
88141f93af9SSandeep Nair 		return;
88241f93af9SSandeep Nair 
88341f93af9SSandeep Nair 	if (!pool->region)
88441f93af9SSandeep Nair 		return;
88541f93af9SSandeep Nair 
88641f93af9SSandeep Nair 	kdesc_empty_pool(pool);
88741f93af9SSandeep Nair 	mutex_lock(&knav_dev_lock);
88841f93af9SSandeep Nair 
88941f93af9SSandeep Nair 	pool->region->used_desc -= pool->num_desc;
89041f93af9SSandeep Nair 	list_del(&pool->region_inst);
89141f93af9SSandeep Nair 	list_del(&pool->list);
89241f93af9SSandeep Nair 
89341f93af9SSandeep Nair 	mutex_unlock(&knav_dev_lock);
89441f93af9SSandeep Nair 	kfree(pool->name);
89541f93af9SSandeep Nair 	devm_kfree(kdev->dev, pool);
89641f93af9SSandeep Nair }
89741f93af9SSandeep Nair EXPORT_SYMBOL_GPL(knav_pool_destroy);
89841f93af9SSandeep Nair 
89941f93af9SSandeep Nair 
90041f93af9SSandeep Nair /**
90141f93af9SSandeep Nair  * knav_pool_desc_get()	- Get a descriptor from the pool
90241f93af9SSandeep Nair  * @pool			- pool handle
90341f93af9SSandeep Nair  *
90441f93af9SSandeep Nair  * Returns descriptor from the pool.
90541f93af9SSandeep Nair  */
90641f93af9SSandeep Nair void *knav_pool_desc_get(void *ph)
90741f93af9SSandeep Nair {
90841f93af9SSandeep Nair 	struct knav_pool *pool = ph;
90941f93af9SSandeep Nair 	dma_addr_t dma;
91041f93af9SSandeep Nair 	unsigned size;
91141f93af9SSandeep Nair 	void *data;
91241f93af9SSandeep Nair 
91341f93af9SSandeep Nair 	dma = knav_queue_pop(pool->queue, &size);
91441f93af9SSandeep Nair 	if (unlikely(!dma))
91541f93af9SSandeep Nair 		return ERR_PTR(-ENOMEM);
91641f93af9SSandeep Nair 	data = knav_pool_desc_dma_to_virt(pool, dma);
91741f93af9SSandeep Nair 	return data;
91841f93af9SSandeep Nair }
91997f39af4SMurali Karicheri EXPORT_SYMBOL_GPL(knav_pool_desc_get);
92041f93af9SSandeep Nair 
92141f93af9SSandeep Nair /**
92241f93af9SSandeep Nair  * knav_pool_desc_put()	- return a descriptor to the pool
92341f93af9SSandeep Nair  * @pool			- pool handle
92441f93af9SSandeep Nair  */
92541f93af9SSandeep Nair void knav_pool_desc_put(void *ph, void *desc)
92641f93af9SSandeep Nair {
92741f93af9SSandeep Nair 	struct knav_pool *pool = ph;
92841f93af9SSandeep Nair 	dma_addr_t dma;
92941f93af9SSandeep Nair 	dma = knav_pool_desc_virt_to_dma(pool, desc);
93041f93af9SSandeep Nair 	knav_queue_push(pool->queue, dma, pool->region->desc_size, 0);
93141f93af9SSandeep Nair }
93297f39af4SMurali Karicheri EXPORT_SYMBOL_GPL(knav_pool_desc_put);
93341f93af9SSandeep Nair 
93441f93af9SSandeep Nair /**
93541f93af9SSandeep Nair  * knav_pool_desc_map()	- Map descriptor for DMA transfer
93641f93af9SSandeep Nair  * @pool			- pool handle
93741f93af9SSandeep Nair  * @desc			- address of descriptor to map
93841f93af9SSandeep Nair  * @size			- size of descriptor to map
93941f93af9SSandeep Nair  * @dma				- DMA address return pointer
94041f93af9SSandeep Nair  * @dma_sz			- adjusted return pointer
94141f93af9SSandeep Nair  *
94241f93af9SSandeep Nair  * Returns 0 on success, errno otherwise.
94341f93af9SSandeep Nair  */
94441f93af9SSandeep Nair int knav_pool_desc_map(void *ph, void *desc, unsigned size,
94541f93af9SSandeep Nair 					dma_addr_t *dma, unsigned *dma_sz)
94641f93af9SSandeep Nair {
94741f93af9SSandeep Nair 	struct knav_pool *pool = ph;
94841f93af9SSandeep Nair 	*dma = knav_pool_desc_virt_to_dma(pool, desc);
94941f93af9SSandeep Nair 	size = min(size, pool->region->desc_size);
95041f93af9SSandeep Nair 	size = ALIGN(size, SMP_CACHE_BYTES);
95141f93af9SSandeep Nair 	*dma_sz = size;
95241f93af9SSandeep Nair 	dma_sync_single_for_device(pool->dev, *dma, size, DMA_TO_DEVICE);
95341f93af9SSandeep Nair 
95441f93af9SSandeep Nair 	/* Ensure the descriptor reaches to the memory */
95541f93af9SSandeep Nair 	__iowmb();
95641f93af9SSandeep Nair 
95741f93af9SSandeep Nair 	return 0;
95841f93af9SSandeep Nair }
95997f39af4SMurali Karicheri EXPORT_SYMBOL_GPL(knav_pool_desc_map);
96041f93af9SSandeep Nair 
96141f93af9SSandeep Nair /**
96241f93af9SSandeep Nair  * knav_pool_desc_unmap()	- Unmap descriptor after DMA transfer
96341f93af9SSandeep Nair  * @pool			- pool handle
96441f93af9SSandeep Nair  * @dma				- DMA address of descriptor to unmap
96541f93af9SSandeep Nair  * @dma_sz			- size of descriptor to unmap
96641f93af9SSandeep Nair  *
96741f93af9SSandeep Nair  * Returns descriptor address on success, Use IS_ERR_OR_NULL() to identify
96841f93af9SSandeep Nair  * error values on return.
96941f93af9SSandeep Nair  */
97041f93af9SSandeep Nair void *knav_pool_desc_unmap(void *ph, dma_addr_t dma, unsigned dma_sz)
97141f93af9SSandeep Nair {
97241f93af9SSandeep Nair 	struct knav_pool *pool = ph;
97341f93af9SSandeep Nair 	unsigned desc_sz;
97441f93af9SSandeep Nair 	void *desc;
97541f93af9SSandeep Nair 
97641f93af9SSandeep Nair 	desc_sz = min(dma_sz, pool->region->desc_size);
97741f93af9SSandeep Nair 	desc = knav_pool_desc_dma_to_virt(pool, dma);
97841f93af9SSandeep Nair 	dma_sync_single_for_cpu(pool->dev, dma, desc_sz, DMA_FROM_DEVICE);
97941f93af9SSandeep Nair 	prefetch(desc);
98041f93af9SSandeep Nair 	return desc;
98141f93af9SSandeep Nair }
98297f39af4SMurali Karicheri EXPORT_SYMBOL_GPL(knav_pool_desc_unmap);
98341f93af9SSandeep Nair 
98441f93af9SSandeep Nair /**
98541f93af9SSandeep Nair  * knav_pool_count()	- Get the number of descriptors in pool.
98641f93af9SSandeep Nair  * @pool		- pool handle
98741f93af9SSandeep Nair  * Returns number of elements in the pool.
98841f93af9SSandeep Nair  */
98941f93af9SSandeep Nair int knav_pool_count(void *ph)
99041f93af9SSandeep Nair {
99141f93af9SSandeep Nair 	struct knav_pool *pool = ph;
99241f93af9SSandeep Nair 	return knav_queue_get_count(pool->queue);
99341f93af9SSandeep Nair }
99497f39af4SMurali Karicheri EXPORT_SYMBOL_GPL(knav_pool_count);
99541f93af9SSandeep Nair 
99641f93af9SSandeep Nair static void knav_queue_setup_region(struct knav_device *kdev,
99741f93af9SSandeep Nair 					struct knav_region *region)
99841f93af9SSandeep Nair {
99941f93af9SSandeep Nair 	unsigned hw_num_desc, hw_desc_size, size;
100041f93af9SSandeep Nair 	struct knav_reg_region __iomem  *regs;
100141f93af9SSandeep Nair 	struct knav_qmgr_info *qmgr;
100241f93af9SSandeep Nair 	struct knav_pool *pool;
100341f93af9SSandeep Nair 	int id = region->id;
100441f93af9SSandeep Nair 	struct page *page;
100541f93af9SSandeep Nair 
100641f93af9SSandeep Nair 	/* unused region? */
100741f93af9SSandeep Nair 	if (!region->num_desc) {
100841f93af9SSandeep Nair 		dev_warn(kdev->dev, "unused region %s\n", region->name);
100941f93af9SSandeep Nair 		return;
101041f93af9SSandeep Nair 	}
101141f93af9SSandeep Nair 
101241f93af9SSandeep Nair 	/* get hardware descriptor value */
101341f93af9SSandeep Nair 	hw_num_desc = ilog2(region->num_desc - 1) + 1;
101441f93af9SSandeep Nair 
101541f93af9SSandeep Nair 	/* did we force fit ourselves into nothingness? */
101641f93af9SSandeep Nair 	if (region->num_desc < 32) {
101741f93af9SSandeep Nair 		region->num_desc = 0;
101841f93af9SSandeep Nair 		dev_warn(kdev->dev, "too few descriptors in region %s\n",
101941f93af9SSandeep Nair 			 region->name);
102041f93af9SSandeep Nair 		return;
102141f93af9SSandeep Nair 	}
102241f93af9SSandeep Nair 
102341f93af9SSandeep Nair 	size = region->num_desc * region->desc_size;
102441f93af9SSandeep Nair 	region->virt_start = alloc_pages_exact(size, GFP_KERNEL | GFP_DMA |
102541f93af9SSandeep Nair 						GFP_DMA32);
102641f93af9SSandeep Nair 	if (!region->virt_start) {
102741f93af9SSandeep Nair 		region->num_desc = 0;
102841f93af9SSandeep Nair 		dev_err(kdev->dev, "memory alloc failed for region %s\n",
102941f93af9SSandeep Nair 			region->name);
103041f93af9SSandeep Nair 		return;
103141f93af9SSandeep Nair 	}
103241f93af9SSandeep Nair 	region->virt_end = region->virt_start + size;
103341f93af9SSandeep Nair 	page = virt_to_page(region->virt_start);
103441f93af9SSandeep Nair 
103541f93af9SSandeep Nair 	region->dma_start = dma_map_page(kdev->dev, page, 0, size,
103641f93af9SSandeep Nair 					 DMA_BIDIRECTIONAL);
103741f93af9SSandeep Nair 	if (dma_mapping_error(kdev->dev, region->dma_start)) {
103841f93af9SSandeep Nair 		dev_err(kdev->dev, "dma map failed for region %s\n",
103941f93af9SSandeep Nair 			region->name);
104041f93af9SSandeep Nair 		goto fail;
104141f93af9SSandeep Nair 	}
104241f93af9SSandeep Nair 	region->dma_end = region->dma_start + size;
104341f93af9SSandeep Nair 
104441f93af9SSandeep Nair 	pool = devm_kzalloc(kdev->dev, sizeof(*pool), GFP_KERNEL);
104541f93af9SSandeep Nair 	if (!pool) {
104641f93af9SSandeep Nair 		dev_err(kdev->dev, "out of memory allocating dummy pool\n");
104741f93af9SSandeep Nair 		goto fail;
104841f93af9SSandeep Nair 	}
104941f93af9SSandeep Nair 	pool->num_desc = 0;
105041f93af9SSandeep Nair 	pool->region_offset = region->num_desc;
105141f93af9SSandeep Nair 	list_add(&pool->region_inst, &region->pools);
105241f93af9SSandeep Nair 
105341f93af9SSandeep Nair 	dev_dbg(kdev->dev,
1054cc0336ecSArnd Bergmann 		"region %s (%d): size:%d, link:%d@%d, dma:%pad-%pad, virt:%p-%p\n",
105541f93af9SSandeep Nair 		region->name, id, region->desc_size, region->num_desc,
1056cc0336ecSArnd Bergmann 		region->link_index, &region->dma_start, &region->dma_end,
105741f93af9SSandeep Nair 		region->virt_start, region->virt_end);
105841f93af9SSandeep Nair 
105941f93af9SSandeep Nair 	hw_desc_size = (region->desc_size / 16) - 1;
106041f93af9SSandeep Nair 	hw_num_desc -= 5;
106141f93af9SSandeep Nair 
106241f93af9SSandeep Nair 	for_each_qmgr(kdev, qmgr) {
106341f93af9SSandeep Nair 		regs = qmgr->reg_region + id;
1064cc0336ecSArnd Bergmann 		writel_relaxed((u32)region->dma_start, &regs->base);
106541f93af9SSandeep Nair 		writel_relaxed(region->link_index, &regs->start_index);
106641f93af9SSandeep Nair 		writel_relaxed(hw_desc_size << 16 | hw_num_desc,
106741f93af9SSandeep Nair 			       &regs->size_count);
106841f93af9SSandeep Nair 	}
106941f93af9SSandeep Nair 	return;
107041f93af9SSandeep Nair 
107141f93af9SSandeep Nair fail:
107241f93af9SSandeep Nair 	if (region->dma_start)
107341f93af9SSandeep Nair 		dma_unmap_page(kdev->dev, region->dma_start, size,
107441f93af9SSandeep Nair 				DMA_BIDIRECTIONAL);
107541f93af9SSandeep Nair 	if (region->virt_start)
107641f93af9SSandeep Nair 		free_pages_exact(region->virt_start, size);
107741f93af9SSandeep Nair 	region->num_desc = 0;
107841f93af9SSandeep Nair 	return;
107941f93af9SSandeep Nair }
108041f93af9SSandeep Nair 
108141f93af9SSandeep Nair static const char *knav_queue_find_name(struct device_node *node)
108241f93af9SSandeep Nair {
108341f93af9SSandeep Nair 	const char *name;
108441f93af9SSandeep Nair 
108541f93af9SSandeep Nair 	if (of_property_read_string(node, "label", &name) < 0)
108641f93af9SSandeep Nair 		name = node->name;
108741f93af9SSandeep Nair 	if (!name)
108841f93af9SSandeep Nair 		name = "unknown";
108941f93af9SSandeep Nair 	return name;
109041f93af9SSandeep Nair }
109141f93af9SSandeep Nair 
109241f93af9SSandeep Nair static int knav_queue_setup_regions(struct knav_device *kdev,
109341f93af9SSandeep Nair 					struct device_node *regions)
109441f93af9SSandeep Nair {
109541f93af9SSandeep Nair 	struct device *dev = kdev->dev;
109641f93af9SSandeep Nair 	struct knav_region *region;
109741f93af9SSandeep Nair 	struct device_node *child;
109841f93af9SSandeep Nair 	u32 temp[2];
109941f93af9SSandeep Nair 	int ret;
110041f93af9SSandeep Nair 
110141f93af9SSandeep Nair 	for_each_child_of_node(regions, child) {
110241f93af9SSandeep Nair 		region = devm_kzalloc(dev, sizeof(*region), GFP_KERNEL);
110341f93af9SSandeep Nair 		if (!region) {
110441f93af9SSandeep Nair 			dev_err(dev, "out of memory allocating region\n");
110541f93af9SSandeep Nair 			return -ENOMEM;
110641f93af9SSandeep Nair 		}
110741f93af9SSandeep Nair 
110841f93af9SSandeep Nair 		region->name = knav_queue_find_name(child);
110941f93af9SSandeep Nair 		of_property_read_u32(child, "id", &region->id);
111041f93af9SSandeep Nair 		ret = of_property_read_u32_array(child, "region-spec", temp, 2);
111141f93af9SSandeep Nair 		if (!ret) {
111241f93af9SSandeep Nair 			region->num_desc  = temp[0];
111341f93af9SSandeep Nair 			region->desc_size = temp[1];
111441f93af9SSandeep Nair 		} else {
111541f93af9SSandeep Nair 			dev_err(dev, "invalid region info %s\n", region->name);
111641f93af9SSandeep Nair 			devm_kfree(dev, region);
111741f93af9SSandeep Nair 			continue;
111841f93af9SSandeep Nair 		}
111941f93af9SSandeep Nair 
112041f93af9SSandeep Nair 		if (!of_get_property(child, "link-index", NULL)) {
112141f93af9SSandeep Nair 			dev_err(dev, "No link info for %s\n", region->name);
112241f93af9SSandeep Nair 			devm_kfree(dev, region);
112341f93af9SSandeep Nair 			continue;
112441f93af9SSandeep Nair 		}
112541f93af9SSandeep Nair 		ret = of_property_read_u32(child, "link-index",
112641f93af9SSandeep Nair 					   &region->link_index);
112741f93af9SSandeep Nair 		if (ret) {
112841f93af9SSandeep Nair 			dev_err(dev, "link index not found for %s\n",
112941f93af9SSandeep Nair 				region->name);
113041f93af9SSandeep Nair 			devm_kfree(dev, region);
113141f93af9SSandeep Nair 			continue;
113241f93af9SSandeep Nair 		}
113341f93af9SSandeep Nair 
113441f93af9SSandeep Nair 		INIT_LIST_HEAD(&region->pools);
113541f93af9SSandeep Nair 		list_add_tail(&region->list, &kdev->regions);
113641f93af9SSandeep Nair 	}
113741f93af9SSandeep Nair 	if (list_empty(&kdev->regions)) {
113841f93af9SSandeep Nair 		dev_err(dev, "no valid region information found\n");
113941f93af9SSandeep Nair 		return -ENODEV;
114041f93af9SSandeep Nair 	}
114141f93af9SSandeep Nair 
114241f93af9SSandeep Nair 	/* Next, we run through the regions and set things up */
114341f93af9SSandeep Nair 	for_each_region(kdev, region)
114441f93af9SSandeep Nair 		knav_queue_setup_region(kdev, region);
114541f93af9SSandeep Nair 
114641f93af9SSandeep Nair 	return 0;
114741f93af9SSandeep Nair }
114841f93af9SSandeep Nair 
114941f93af9SSandeep Nair static int knav_get_link_ram(struct knav_device *kdev,
115041f93af9SSandeep Nair 				       const char *name,
115141f93af9SSandeep Nair 				       struct knav_link_ram_block *block)
115241f93af9SSandeep Nair {
115341f93af9SSandeep Nair 	struct platform_device *pdev = to_platform_device(kdev->dev);
115441f93af9SSandeep Nair 	struct device_node *node = pdev->dev.of_node;
115541f93af9SSandeep Nair 	u32 temp[2];
115641f93af9SSandeep Nair 
115741f93af9SSandeep Nair 	/*
115841f93af9SSandeep Nair 	 * Note: link ram resources are specified in "entry" sized units. In
115941f93af9SSandeep Nair 	 * reality, although entries are ~40bits in hardware, we treat them as
116041f93af9SSandeep Nair 	 * 64-bit entities here.
116141f93af9SSandeep Nair 	 *
116241f93af9SSandeep Nair 	 * For example, to specify the internal link ram for Keystone-I class
116341f93af9SSandeep Nair 	 * devices, we would set the linkram0 resource to 0x80000-0x83fff.
116441f93af9SSandeep Nair 	 *
116541f93af9SSandeep Nair 	 * This gets a bit weird when other link rams are used.  For example,
116641f93af9SSandeep Nair 	 * if the range specified is 0x0c000000-0x0c003fff (i.e., 16K entries
116741f93af9SSandeep Nair 	 * in MSMC SRAM), the actual memory used is 0x0c000000-0x0c020000,
116841f93af9SSandeep Nair 	 * which accounts for 64-bits per entry, for 16K entries.
116941f93af9SSandeep Nair 	 */
117041f93af9SSandeep Nair 	if (!of_property_read_u32_array(node, name , temp, 2)) {
117141f93af9SSandeep Nair 		if (temp[0]) {
117241f93af9SSandeep Nair 			/*
117341f93af9SSandeep Nair 			 * queue_base specified => using internal or onchip
117441f93af9SSandeep Nair 			 * link ram WARNING - we do not "reserve" this block
117541f93af9SSandeep Nair 			 */
1176cc0336ecSArnd Bergmann 			block->dma = (dma_addr_t)temp[0];
117741f93af9SSandeep Nair 			block->virt = NULL;
117841f93af9SSandeep Nair 			block->size = temp[1];
117941f93af9SSandeep Nair 		} else {
118041f93af9SSandeep Nair 			block->size = temp[1];
118141f93af9SSandeep Nair 			/* queue_base not specific => allocate requested size */
118241f93af9SSandeep Nair 			block->virt = dmam_alloc_coherent(kdev->dev,
1183cc0336ecSArnd Bergmann 						  8 * block->size, &block->dma,
118441f93af9SSandeep Nair 						  GFP_KERNEL);
118541f93af9SSandeep Nair 			if (!block->virt) {
118641f93af9SSandeep Nair 				dev_err(kdev->dev, "failed to alloc linkram\n");
118741f93af9SSandeep Nair 				return -ENOMEM;
118841f93af9SSandeep Nair 			}
118941f93af9SSandeep Nair 		}
119041f93af9SSandeep Nair 	} else {
119141f93af9SSandeep Nair 		return -ENODEV;
119241f93af9SSandeep Nair 	}
119341f93af9SSandeep Nair 	return 0;
119441f93af9SSandeep Nair }
119541f93af9SSandeep Nair 
119641f93af9SSandeep Nair static int knav_queue_setup_link_ram(struct knav_device *kdev)
119741f93af9SSandeep Nair {
119841f93af9SSandeep Nair 	struct knav_link_ram_block *block;
119941f93af9SSandeep Nair 	struct knav_qmgr_info *qmgr;
120041f93af9SSandeep Nair 
120141f93af9SSandeep Nair 	for_each_qmgr(kdev, qmgr) {
120241f93af9SSandeep Nair 		block = &kdev->link_rams[0];
1203cc0336ecSArnd Bergmann 		dev_dbg(kdev->dev, "linkram0: dma:%pad, virt:%p, size:%x\n",
1204cc0336ecSArnd Bergmann 			&block->dma, block->virt, block->size);
1205cc0336ecSArnd Bergmann 		writel_relaxed((u32)block->dma, &qmgr->reg_config->link_ram_base0);
1206350601b4SMurali Karicheri 		if (kdev->version == QMSS_66AK2G)
1207350601b4SMurali Karicheri 			writel_relaxed(block->size,
1208350601b4SMurali Karicheri 				       &qmgr->reg_config->link_ram_size0);
1209350601b4SMurali Karicheri 		else
1210350601b4SMurali Karicheri 			writel_relaxed(block->size - 1,
1211350601b4SMurali Karicheri 				       &qmgr->reg_config->link_ram_size0);
121241f93af9SSandeep Nair 		block++;
121341f93af9SSandeep Nair 		if (!block->size)
1214f7f2bccdSMichal Morawiec 			continue;
121541f93af9SSandeep Nair 
1216cc0336ecSArnd Bergmann 		dev_dbg(kdev->dev, "linkram1: dma:%pad, virt:%p, size:%x\n",
1217cc0336ecSArnd Bergmann 			&block->dma, block->virt, block->size);
1218cc0336ecSArnd Bergmann 		writel_relaxed(block->dma, &qmgr->reg_config->link_ram_base1);
121941f93af9SSandeep Nair 	}
122041f93af9SSandeep Nair 
122141f93af9SSandeep Nair 	return 0;
122241f93af9SSandeep Nair }
122341f93af9SSandeep Nair 
122441f93af9SSandeep Nair static int knav_setup_queue_range(struct knav_device *kdev,
122541f93af9SSandeep Nair 					struct device_node *node)
122641f93af9SSandeep Nair {
122741f93af9SSandeep Nair 	struct device *dev = kdev->dev;
122841f93af9SSandeep Nair 	struct knav_range_info *range;
122941f93af9SSandeep Nair 	struct knav_qmgr_info *qmgr;
123041f93af9SSandeep Nair 	u32 temp[2], start, end, id, index;
123141f93af9SSandeep Nair 	int ret, i;
123241f93af9SSandeep Nair 
123341f93af9SSandeep Nair 	range = devm_kzalloc(dev, sizeof(*range), GFP_KERNEL);
123441f93af9SSandeep Nair 	if (!range) {
123541f93af9SSandeep Nair 		dev_err(dev, "out of memory allocating range\n");
123641f93af9SSandeep Nair 		return -ENOMEM;
123741f93af9SSandeep Nair 	}
123841f93af9SSandeep Nair 
123941f93af9SSandeep Nair 	range->kdev = kdev;
124041f93af9SSandeep Nair 	range->name = knav_queue_find_name(node);
124141f93af9SSandeep Nair 	ret = of_property_read_u32_array(node, "qrange", temp, 2);
124241f93af9SSandeep Nair 	if (!ret) {
124341f93af9SSandeep Nair 		range->queue_base = temp[0] - kdev->base_id;
124441f93af9SSandeep Nair 		range->num_queues = temp[1];
124541f93af9SSandeep Nair 	} else {
124641f93af9SSandeep Nair 		dev_err(dev, "invalid queue range %s\n", range->name);
124741f93af9SSandeep Nair 		devm_kfree(dev, range);
124841f93af9SSandeep Nair 		return -EINVAL;
124941f93af9SSandeep Nair 	}
125041f93af9SSandeep Nair 
125141f93af9SSandeep Nair 	for (i = 0; i < RANGE_MAX_IRQS; i++) {
125241f93af9SSandeep Nair 		struct of_phandle_args oirq;
125341f93af9SSandeep Nair 
125441f93af9SSandeep Nair 		if (of_irq_parse_one(node, i, &oirq))
125541f93af9SSandeep Nair 			break;
125641f93af9SSandeep Nair 
125741f93af9SSandeep Nair 		range->irqs[i].irq = irq_create_of_mapping(&oirq);
125841f93af9SSandeep Nair 		if (range->irqs[i].irq == IRQ_NONE)
125941f93af9SSandeep Nair 			break;
126041f93af9SSandeep Nair 
126141f93af9SSandeep Nair 		range->num_irqs++;
126241f93af9SSandeep Nair 
1263*832ad0e3SMarc Zyngier 		if (IS_ENABLED(CONFIG_SMP) && oirq.args_count == 3) {
1264*832ad0e3SMarc Zyngier 			unsigned long mask;
1265*832ad0e3SMarc Zyngier 			int bit;
1266*832ad0e3SMarc Zyngier 
1267*832ad0e3SMarc Zyngier 			range->irqs[i].cpu_mask = devm_kzalloc(dev,
1268*832ad0e3SMarc Zyngier 							       cpumask_size(), GFP_KERNEL);
1269*832ad0e3SMarc Zyngier 			if (!range->irqs[i].cpu_mask)
1270*832ad0e3SMarc Zyngier 				return -ENOMEM;
1271*832ad0e3SMarc Zyngier 
1272*832ad0e3SMarc Zyngier 			mask = (oirq.args[2] & 0x0000ff00) >> 8;
1273*832ad0e3SMarc Zyngier 			for_each_set_bit(bit, &mask, BITS_PER_LONG)
1274*832ad0e3SMarc Zyngier 				cpumask_set_cpu(bit, range->irqs[i].cpu_mask);
1275*832ad0e3SMarc Zyngier 		}
127641f93af9SSandeep Nair 	}
127741f93af9SSandeep Nair 
127841f93af9SSandeep Nair 	range->num_irqs = min(range->num_irqs, range->num_queues);
127941f93af9SSandeep Nair 	if (range->num_irqs)
128041f93af9SSandeep Nair 		range->flags |= RANGE_HAS_IRQ;
128141f93af9SSandeep Nair 
128241f93af9SSandeep Nair 	if (of_get_property(node, "qalloc-by-id", NULL))
128341f93af9SSandeep Nair 		range->flags |= RANGE_RESERVED;
128441f93af9SSandeep Nair 
128541f93af9SSandeep Nair 	if (of_get_property(node, "accumulator", NULL)) {
128641f93af9SSandeep Nair 		ret = knav_init_acc_range(kdev, node, range);
128741f93af9SSandeep Nair 		if (ret < 0) {
128841f93af9SSandeep Nair 			devm_kfree(dev, range);
128941f93af9SSandeep Nair 			return ret;
129041f93af9SSandeep Nair 		}
129141f93af9SSandeep Nair 	} else {
129241f93af9SSandeep Nair 		range->ops = &knav_gp_range_ops;
129341f93af9SSandeep Nair 	}
129441f93af9SSandeep Nair 
129541f93af9SSandeep Nair 	/* set threshold to 1, and flush out the queues */
129641f93af9SSandeep Nair 	for_each_qmgr(kdev, qmgr) {
129741f93af9SSandeep Nair 		start = max(qmgr->start_queue, range->queue_base);
129841f93af9SSandeep Nair 		end   = min(qmgr->start_queue + qmgr->num_queues,
129941f93af9SSandeep Nair 			    range->queue_base + range->num_queues);
130041f93af9SSandeep Nair 		for (id = start; id < end; id++) {
130141f93af9SSandeep Nair 			index = id - qmgr->start_queue;
130241f93af9SSandeep Nair 			writel_relaxed(THRESH_GTE | 1,
130341f93af9SSandeep Nair 				       &qmgr->reg_peek[index].ptr_size_thresh);
130441f93af9SSandeep Nair 			writel_relaxed(0,
130541f93af9SSandeep Nair 				       &qmgr->reg_push[index].ptr_size_thresh);
130641f93af9SSandeep Nair 		}
130741f93af9SSandeep Nair 	}
130841f93af9SSandeep Nair 
130941f93af9SSandeep Nair 	list_add_tail(&range->list, &kdev->queue_ranges);
131041f93af9SSandeep Nair 	dev_dbg(dev, "added range %s: %d-%d, %d irqs%s%s%s\n",
131141f93af9SSandeep Nair 		range->name, range->queue_base,
131241f93af9SSandeep Nair 		range->queue_base + range->num_queues - 1,
131341f93af9SSandeep Nair 		range->num_irqs,
131441f93af9SSandeep Nair 		(range->flags & RANGE_HAS_IRQ) ? ", has irq" : "",
131541f93af9SSandeep Nair 		(range->flags & RANGE_RESERVED) ? ", reserved" : "",
131641f93af9SSandeep Nair 		(range->flags & RANGE_HAS_ACCUMULATOR) ? ", acc" : "");
131741f93af9SSandeep Nair 	kdev->num_queues_in_use += range->num_queues;
131841f93af9SSandeep Nair 	return 0;
131941f93af9SSandeep Nair }
132041f93af9SSandeep Nair 
132141f93af9SSandeep Nair static int knav_setup_queue_pools(struct knav_device *kdev,
132241f93af9SSandeep Nair 				   struct device_node *queue_pools)
132341f93af9SSandeep Nair {
132441f93af9SSandeep Nair 	struct device_node *type, *range;
132541f93af9SSandeep Nair 	int ret;
132641f93af9SSandeep Nair 
132741f93af9SSandeep Nair 	for_each_child_of_node(queue_pools, type) {
132841f93af9SSandeep Nair 		for_each_child_of_node(type, range) {
132941f93af9SSandeep Nair 			ret = knav_setup_queue_range(kdev, range);
133041f93af9SSandeep Nair 			/* return value ignored, we init the rest... */
133141f93af9SSandeep Nair 		}
133241f93af9SSandeep Nair 	}
133341f93af9SSandeep Nair 
133441f93af9SSandeep Nair 	/* ... and barf if they all failed! */
133541f93af9SSandeep Nair 	if (list_empty(&kdev->queue_ranges)) {
133641f93af9SSandeep Nair 		dev_err(kdev->dev, "no valid queue range found\n");
133741f93af9SSandeep Nair 		return -ENODEV;
133841f93af9SSandeep Nair 	}
133941f93af9SSandeep Nair 	return 0;
134041f93af9SSandeep Nair }
134141f93af9SSandeep Nair 
134241f93af9SSandeep Nair static void knav_free_queue_range(struct knav_device *kdev,
134341f93af9SSandeep Nair 				  struct knav_range_info *range)
134441f93af9SSandeep Nair {
134541f93af9SSandeep Nair 	if (range->ops && range->ops->free_range)
134641f93af9SSandeep Nair 		range->ops->free_range(range);
134741f93af9SSandeep Nair 	list_del(&range->list);
134841f93af9SSandeep Nair 	devm_kfree(kdev->dev, range);
134941f93af9SSandeep Nair }
135041f93af9SSandeep Nair 
135141f93af9SSandeep Nair static void knav_free_queue_ranges(struct knav_device *kdev)
135241f93af9SSandeep Nair {
135341f93af9SSandeep Nair 	struct knav_range_info *range;
135441f93af9SSandeep Nair 
135541f93af9SSandeep Nair 	for (;;) {
135641f93af9SSandeep Nair 		range = first_queue_range(kdev);
135741f93af9SSandeep Nair 		if (!range)
135841f93af9SSandeep Nair 			break;
135941f93af9SSandeep Nair 		knav_free_queue_range(kdev, range);
136041f93af9SSandeep Nair 	}
136141f93af9SSandeep Nair }
136241f93af9SSandeep Nair 
136341f93af9SSandeep Nair static void knav_queue_free_regions(struct knav_device *kdev)
136441f93af9SSandeep Nair {
136541f93af9SSandeep Nair 	struct knav_region *region;
1366148bb043SAxel Lin 	struct knav_pool *pool, *tmp;
136741f93af9SSandeep Nair 	unsigned size;
136841f93af9SSandeep Nair 
136941f93af9SSandeep Nair 	for (;;) {
137041f93af9SSandeep Nair 		region = first_region(kdev);
137141f93af9SSandeep Nair 		if (!region)
137241f93af9SSandeep Nair 			break;
1373148bb043SAxel Lin 		list_for_each_entry_safe(pool, tmp, &region->pools, region_inst)
137441f93af9SSandeep Nair 			knav_pool_destroy(pool);
137541f93af9SSandeep Nair 
137641f93af9SSandeep Nair 		size = region->virt_end - region->virt_start;
137741f93af9SSandeep Nair 		if (size)
137841f93af9SSandeep Nair 			free_pages_exact(region->virt_start, size);
137941f93af9SSandeep Nair 		list_del(&region->list);
138041f93af9SSandeep Nair 		devm_kfree(kdev->dev, region);
138141f93af9SSandeep Nair 	}
138241f93af9SSandeep Nair }
138341f93af9SSandeep Nair 
138441f93af9SSandeep Nair static void __iomem *knav_queue_map_reg(struct knav_device *kdev,
138541f93af9SSandeep Nair 					struct device_node *node, int index)
138641f93af9SSandeep Nair {
138741f93af9SSandeep Nair 	struct resource res;
138841f93af9SSandeep Nair 	void __iomem *regs;
138941f93af9SSandeep Nair 	int ret;
139041f93af9SSandeep Nair 
139141f93af9SSandeep Nair 	ret = of_address_to_resource(node, index, &res);
139241f93af9SSandeep Nair 	if (ret) {
1393dc37a252SRob Herring 		dev_err(kdev->dev, "Can't translate of node(%pOFn) address for index(%d)\n",
1394dc37a252SRob Herring 			node, index);
139541f93af9SSandeep Nair 		return ERR_PTR(ret);
139641f93af9SSandeep Nair 	}
139741f93af9SSandeep Nair 
139841f93af9SSandeep Nair 	regs = devm_ioremap_resource(kdev->dev, &res);
139941f93af9SSandeep Nair 	if (IS_ERR(regs))
1400dc37a252SRob Herring 		dev_err(kdev->dev, "Failed to map register base for index(%d) node(%pOFn)\n",
1401dc37a252SRob Herring 			index, node);
140241f93af9SSandeep Nair 	return regs;
140341f93af9SSandeep Nair }
140441f93af9SSandeep Nair 
140541f93af9SSandeep Nair static int knav_queue_init_qmgrs(struct knav_device *kdev,
140641f93af9SSandeep Nair 					struct device_node *qmgrs)
140741f93af9SSandeep Nair {
140841f93af9SSandeep Nair 	struct device *dev = kdev->dev;
140941f93af9SSandeep Nair 	struct knav_qmgr_info *qmgr;
141041f93af9SSandeep Nair 	struct device_node *child;
141141f93af9SSandeep Nair 	u32 temp[2];
141241f93af9SSandeep Nair 	int ret;
141341f93af9SSandeep Nair 
141441f93af9SSandeep Nair 	for_each_child_of_node(qmgrs, child) {
141541f93af9SSandeep Nair 		qmgr = devm_kzalloc(dev, sizeof(*qmgr), GFP_KERNEL);
141641f93af9SSandeep Nair 		if (!qmgr) {
141741f93af9SSandeep Nair 			dev_err(dev, "out of memory allocating qmgr\n");
141841f93af9SSandeep Nair 			return -ENOMEM;
141941f93af9SSandeep Nair 		}
142041f93af9SSandeep Nair 
142141f93af9SSandeep Nair 		ret = of_property_read_u32_array(child, "managed-queues",
142241f93af9SSandeep Nair 						 temp, 2);
142341f93af9SSandeep Nair 		if (!ret) {
142441f93af9SSandeep Nair 			qmgr->start_queue = temp[0];
142541f93af9SSandeep Nair 			qmgr->num_queues = temp[1];
142641f93af9SSandeep Nair 		} else {
142741f93af9SSandeep Nair 			dev_err(dev, "invalid qmgr queue range\n");
142841f93af9SSandeep Nair 			devm_kfree(dev, qmgr);
142941f93af9SSandeep Nair 			continue;
143041f93af9SSandeep Nair 		}
143141f93af9SSandeep Nair 
143241f93af9SSandeep Nair 		dev_info(dev, "qmgr start queue %d, number of queues %d\n",
143341f93af9SSandeep Nair 			 qmgr->start_queue, qmgr->num_queues);
143441f93af9SSandeep Nair 
143541f93af9SSandeep Nair 		qmgr->reg_peek =
143641f93af9SSandeep Nair 			knav_queue_map_reg(kdev, child,
143741f93af9SSandeep Nair 					   KNAV_QUEUE_PEEK_REG_INDEX);
1438350601b4SMurali Karicheri 
1439350601b4SMurali Karicheri 		if (kdev->version == QMSS) {
144041f93af9SSandeep Nair 			qmgr->reg_status =
144141f93af9SSandeep Nair 				knav_queue_map_reg(kdev, child,
144241f93af9SSandeep Nair 						   KNAV_QUEUE_STATUS_REG_INDEX);
1443350601b4SMurali Karicheri 		}
1444350601b4SMurali Karicheri 
144541f93af9SSandeep Nair 		qmgr->reg_config =
144641f93af9SSandeep Nair 			knav_queue_map_reg(kdev, child,
1447350601b4SMurali Karicheri 					   (kdev->version == QMSS_66AK2G) ?
1448350601b4SMurali Karicheri 					   KNAV_L_QUEUE_CONFIG_REG_INDEX :
144941f93af9SSandeep Nair 					   KNAV_QUEUE_CONFIG_REG_INDEX);
145041f93af9SSandeep Nair 		qmgr->reg_region =
145141f93af9SSandeep Nair 			knav_queue_map_reg(kdev, child,
1452350601b4SMurali Karicheri 					   (kdev->version == QMSS_66AK2G) ?
1453350601b4SMurali Karicheri 					   KNAV_L_QUEUE_REGION_REG_INDEX :
145441f93af9SSandeep Nair 					   KNAV_QUEUE_REGION_REG_INDEX);
1455350601b4SMurali Karicheri 
145641f93af9SSandeep Nair 		qmgr->reg_push =
145741f93af9SSandeep Nair 			knav_queue_map_reg(kdev, child,
1458350601b4SMurali Karicheri 					   (kdev->version == QMSS_66AK2G) ?
1459350601b4SMurali Karicheri 					    KNAV_L_QUEUE_PUSH_REG_INDEX :
146041f93af9SSandeep Nair 					    KNAV_QUEUE_PUSH_REG_INDEX);
1461350601b4SMurali Karicheri 
1462350601b4SMurali Karicheri 		if (kdev->version == QMSS) {
146341f93af9SSandeep Nair 			qmgr->reg_pop =
146441f93af9SSandeep Nair 				knav_queue_map_reg(kdev, child,
146541f93af9SSandeep Nair 						   KNAV_QUEUE_POP_REG_INDEX);
1466350601b4SMurali Karicheri 		}
146741f93af9SSandeep Nair 
1468350601b4SMurali Karicheri 		if (IS_ERR(qmgr->reg_peek) ||
1469350601b4SMurali Karicheri 		    ((kdev->version == QMSS) &&
1470350601b4SMurali Karicheri 		    (IS_ERR(qmgr->reg_status) || IS_ERR(qmgr->reg_pop))) ||
147141f93af9SSandeep Nair 		    IS_ERR(qmgr->reg_config) || IS_ERR(qmgr->reg_region) ||
1472350601b4SMurali Karicheri 		    IS_ERR(qmgr->reg_push)) {
147341f93af9SSandeep Nair 			dev_err(dev, "failed to map qmgr regs\n");
1474350601b4SMurali Karicheri 			if (kdev->version == QMSS) {
147541f93af9SSandeep Nair 				if (!IS_ERR(qmgr->reg_status))
147641f93af9SSandeep Nair 					devm_iounmap(dev, qmgr->reg_status);
1477350601b4SMurali Karicheri 				if (!IS_ERR(qmgr->reg_pop))
1478350601b4SMurali Karicheri 					devm_iounmap(dev, qmgr->reg_pop);
1479350601b4SMurali Karicheri 			}
1480350601b4SMurali Karicheri 			if (!IS_ERR(qmgr->reg_peek))
1481350601b4SMurali Karicheri 				devm_iounmap(dev, qmgr->reg_peek);
148241f93af9SSandeep Nair 			if (!IS_ERR(qmgr->reg_config))
148341f93af9SSandeep Nair 				devm_iounmap(dev, qmgr->reg_config);
148441f93af9SSandeep Nair 			if (!IS_ERR(qmgr->reg_region))
148541f93af9SSandeep Nair 				devm_iounmap(dev, qmgr->reg_region);
148641f93af9SSandeep Nair 			if (!IS_ERR(qmgr->reg_push))
148741f93af9SSandeep Nair 				devm_iounmap(dev, qmgr->reg_push);
148841f93af9SSandeep Nair 			devm_kfree(dev, qmgr);
148941f93af9SSandeep Nair 			continue;
149041f93af9SSandeep Nair 		}
149141f93af9SSandeep Nair 
1492350601b4SMurali Karicheri 		/* Use same push register for pop as well */
1493350601b4SMurali Karicheri 		if (kdev->version == QMSS_66AK2G)
1494350601b4SMurali Karicheri 			qmgr->reg_pop = qmgr->reg_push;
1495350601b4SMurali Karicheri 
149641f93af9SSandeep Nair 		list_add_tail(&qmgr->list, &kdev->qmgrs);
149741f93af9SSandeep 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",
149841f93af9SSandeep Nair 			 qmgr->start_queue, qmgr->num_queues,
149941f93af9SSandeep Nair 			 qmgr->reg_peek, qmgr->reg_status,
150041f93af9SSandeep Nair 			 qmgr->reg_config, qmgr->reg_region,
150141f93af9SSandeep Nair 			 qmgr->reg_push, qmgr->reg_pop);
150241f93af9SSandeep Nair 	}
150341f93af9SSandeep Nair 	return 0;
150441f93af9SSandeep Nair }
150541f93af9SSandeep Nair 
150641f93af9SSandeep Nair static int knav_queue_init_pdsps(struct knav_device *kdev,
150741f93af9SSandeep Nair 					struct device_node *pdsps)
150841f93af9SSandeep Nair {
150941f93af9SSandeep Nair 	struct device *dev = kdev->dev;
151041f93af9SSandeep Nair 	struct knav_pdsp_info *pdsp;
151141f93af9SSandeep Nair 	struct device_node *child;
151241f93af9SSandeep Nair 
151341f93af9SSandeep Nair 	for_each_child_of_node(pdsps, child) {
151441f93af9SSandeep Nair 		pdsp = devm_kzalloc(dev, sizeof(*pdsp), GFP_KERNEL);
151541f93af9SSandeep Nair 		if (!pdsp) {
151641f93af9SSandeep Nair 			dev_err(dev, "out of memory allocating pdsp\n");
151741f93af9SSandeep Nair 			return -ENOMEM;
151841f93af9SSandeep Nair 		}
151941f93af9SSandeep Nair 		pdsp->name = knav_queue_find_name(child);
152041f93af9SSandeep Nair 		pdsp->iram =
152141f93af9SSandeep Nair 			knav_queue_map_reg(kdev, child,
152241f93af9SSandeep Nair 					   KNAV_QUEUE_PDSP_IRAM_REG_INDEX);
152341f93af9SSandeep Nair 		pdsp->regs =
152441f93af9SSandeep Nair 			knav_queue_map_reg(kdev, child,
152541f93af9SSandeep Nair 					   KNAV_QUEUE_PDSP_REGS_REG_INDEX);
152641f93af9SSandeep Nair 		pdsp->intd =
152741f93af9SSandeep Nair 			knav_queue_map_reg(kdev, child,
152841f93af9SSandeep Nair 					   KNAV_QUEUE_PDSP_INTD_REG_INDEX);
152941f93af9SSandeep Nair 		pdsp->command =
153041f93af9SSandeep Nair 			knav_queue_map_reg(kdev, child,
153141f93af9SSandeep Nair 					   KNAV_QUEUE_PDSP_CMD_REG_INDEX);
153241f93af9SSandeep Nair 
153341f93af9SSandeep Nair 		if (IS_ERR(pdsp->command) || IS_ERR(pdsp->iram) ||
153441f93af9SSandeep Nair 		    IS_ERR(pdsp->regs) || IS_ERR(pdsp->intd)) {
153541f93af9SSandeep Nair 			dev_err(dev, "failed to map pdsp %s regs\n",
153641f93af9SSandeep Nair 				pdsp->name);
153741f93af9SSandeep Nair 			if (!IS_ERR(pdsp->command))
153841f93af9SSandeep Nair 				devm_iounmap(dev, pdsp->command);
153941f93af9SSandeep Nair 			if (!IS_ERR(pdsp->iram))
154041f93af9SSandeep Nair 				devm_iounmap(dev, pdsp->iram);
154141f93af9SSandeep Nair 			if (!IS_ERR(pdsp->regs))
154241f93af9SSandeep Nair 				devm_iounmap(dev, pdsp->regs);
154341f93af9SSandeep Nair 			if (!IS_ERR(pdsp->intd))
154441f93af9SSandeep Nair 				devm_iounmap(dev, pdsp->intd);
154541f93af9SSandeep Nair 			devm_kfree(dev, pdsp);
154641f93af9SSandeep Nair 			continue;
154741f93af9SSandeep Nair 		}
154841f93af9SSandeep Nair 		of_property_read_u32(child, "id", &pdsp->id);
154941f93af9SSandeep Nair 		list_add_tail(&pdsp->list, &kdev->pdsps);
155096ee19beSMurali Karicheri 		dev_dbg(dev, "added pdsp %s: command %p, iram %p, regs %p, intd %p\n",
155141f93af9SSandeep Nair 			pdsp->name, pdsp->command, pdsp->iram, pdsp->regs,
155296ee19beSMurali Karicheri 			pdsp->intd);
155341f93af9SSandeep Nair 	}
155441f93af9SSandeep Nair 	return 0;
155541f93af9SSandeep Nair }
155641f93af9SSandeep Nair 
155741f93af9SSandeep Nair static int knav_queue_stop_pdsp(struct knav_device *kdev,
155841f93af9SSandeep Nair 			  struct knav_pdsp_info *pdsp)
155941f93af9SSandeep Nair {
156041f93af9SSandeep Nair 	u32 val, timeout = 1000;
156141f93af9SSandeep Nair 	int ret;
156241f93af9SSandeep Nair 
156341f93af9SSandeep Nair 	val = readl_relaxed(&pdsp->regs->control) & ~PDSP_CTRL_ENABLE;
156441f93af9SSandeep Nair 	writel_relaxed(val, &pdsp->regs->control);
156541f93af9SSandeep Nair 	ret = knav_queue_pdsp_wait(&pdsp->regs->control, timeout,
156641f93af9SSandeep Nair 					PDSP_CTRL_RUNNING);
156741f93af9SSandeep Nair 	if (ret < 0) {
156841f93af9SSandeep Nair 		dev_err(kdev->dev, "timed out on pdsp %s stop\n", pdsp->name);
156941f93af9SSandeep Nair 		return ret;
157041f93af9SSandeep Nair 	}
157104501690SMurali Karicheri 	pdsp->loaded = false;
157204501690SMurali Karicheri 	pdsp->started = false;
157341f93af9SSandeep Nair 	return 0;
157441f93af9SSandeep Nair }
157541f93af9SSandeep Nair 
157641f93af9SSandeep Nair static int knav_queue_load_pdsp(struct knav_device *kdev,
157741f93af9SSandeep Nair 			  struct knav_pdsp_info *pdsp)
157841f93af9SSandeep Nair {
157941f93af9SSandeep Nair 	int i, ret, fwlen;
158041f93af9SSandeep Nair 	const struct firmware *fw;
158196ee19beSMurali Karicheri 	bool found = false;
158241f93af9SSandeep Nair 	u32 *fwdata;
158341f93af9SSandeep Nair 
158496ee19beSMurali Karicheri 	for (i = 0; i < ARRAY_SIZE(knav_acc_firmwares); i++) {
158596ee19beSMurali Karicheri 		if (knav_acc_firmwares[i]) {
15866af1a073SMurali Karicheri 			ret = request_firmware_direct(&fw,
158796ee19beSMurali Karicheri 						      knav_acc_firmwares[i],
158896ee19beSMurali Karicheri 						      kdev->dev);
158996ee19beSMurali Karicheri 			if (!ret) {
159096ee19beSMurali Karicheri 				found = true;
159196ee19beSMurali Karicheri 				break;
159241f93af9SSandeep Nair 			}
159396ee19beSMurali Karicheri 		}
159496ee19beSMurali Karicheri 	}
159596ee19beSMurali Karicheri 
159696ee19beSMurali Karicheri 	if (!found) {
159796ee19beSMurali Karicheri 		dev_err(kdev->dev, "failed to get firmware for pdsp\n");
159896ee19beSMurali Karicheri 		return -ENODEV;
159996ee19beSMurali Karicheri 	}
160096ee19beSMurali Karicheri 
160196ee19beSMurali Karicheri 	dev_info(kdev->dev, "firmware file %s downloaded for PDSP\n",
160296ee19beSMurali Karicheri 		 knav_acc_firmwares[i]);
160396ee19beSMurali Karicheri 
160441f93af9SSandeep Nair 	writel_relaxed(pdsp->id + 1, pdsp->command + 0x18);
160541f93af9SSandeep Nair 	/* download the firmware */
160641f93af9SSandeep Nair 	fwdata = (u32 *)fw->data;
160741f93af9SSandeep Nair 	fwlen = (fw->size + sizeof(u32) - 1) / sizeof(u32);
160841f93af9SSandeep Nair 	for (i = 0; i < fwlen; i++)
160941f93af9SSandeep Nair 		writel_relaxed(be32_to_cpu(fwdata[i]), pdsp->iram + i);
161041f93af9SSandeep Nair 
161141f93af9SSandeep Nair 	release_firmware(fw);
161241f93af9SSandeep Nair 	return 0;
161341f93af9SSandeep Nair }
161441f93af9SSandeep Nair 
161541f93af9SSandeep Nair static int knav_queue_start_pdsp(struct knav_device *kdev,
161641f93af9SSandeep Nair 			   struct knav_pdsp_info *pdsp)
161741f93af9SSandeep Nair {
161841f93af9SSandeep Nair 	u32 val, timeout = 1000;
161941f93af9SSandeep Nair 	int ret;
162041f93af9SSandeep Nair 
162141f93af9SSandeep Nair 	/* write a command for sync */
162241f93af9SSandeep Nair 	writel_relaxed(0xffffffff, pdsp->command);
162341f93af9SSandeep Nair 	while (readl_relaxed(pdsp->command) != 0xffffffff)
162441f93af9SSandeep Nair 		cpu_relax();
162541f93af9SSandeep Nair 
162641f93af9SSandeep Nair 	/* soft reset the PDSP */
162741f93af9SSandeep Nair 	val  = readl_relaxed(&pdsp->regs->control);
162841f93af9SSandeep Nair 	val &= ~(PDSP_CTRL_PC_MASK | PDSP_CTRL_SOFT_RESET);
162941f93af9SSandeep Nair 	writel_relaxed(val, &pdsp->regs->control);
163041f93af9SSandeep Nair 
163141f93af9SSandeep Nair 	/* enable pdsp */
163241f93af9SSandeep Nair 	val = readl_relaxed(&pdsp->regs->control) | PDSP_CTRL_ENABLE;
163341f93af9SSandeep Nair 	writel_relaxed(val, &pdsp->regs->control);
163441f93af9SSandeep Nair 
163541f93af9SSandeep Nair 	/* wait for command register to clear */
163641f93af9SSandeep Nair 	ret = knav_queue_pdsp_wait(pdsp->command, timeout, 0);
163741f93af9SSandeep Nair 	if (ret < 0) {
163841f93af9SSandeep Nair 		dev_err(kdev->dev,
163941f93af9SSandeep Nair 			"timed out on pdsp %s command register wait\n",
164041f93af9SSandeep Nair 			pdsp->name);
164141f93af9SSandeep Nair 		return ret;
164241f93af9SSandeep Nair 	}
164341f93af9SSandeep Nair 	return 0;
164441f93af9SSandeep Nair }
164541f93af9SSandeep Nair 
164641f93af9SSandeep Nair static void knav_queue_stop_pdsps(struct knav_device *kdev)
164741f93af9SSandeep Nair {
164841f93af9SSandeep Nair 	struct knav_pdsp_info *pdsp;
164941f93af9SSandeep Nair 
165041f93af9SSandeep Nair 	/* disable all pdsps */
165141f93af9SSandeep Nair 	for_each_pdsp(kdev, pdsp)
165241f93af9SSandeep Nair 		knav_queue_stop_pdsp(kdev, pdsp);
165341f93af9SSandeep Nair }
165441f93af9SSandeep Nair 
165541f93af9SSandeep Nair static int knav_queue_start_pdsps(struct knav_device *kdev)
165641f93af9SSandeep Nair {
165741f93af9SSandeep Nair 	struct knav_pdsp_info *pdsp;
165841f93af9SSandeep Nair 	int ret;
165941f93af9SSandeep Nair 
166041f93af9SSandeep Nair 	knav_queue_stop_pdsps(kdev);
166104501690SMurali Karicheri 	/* now load them all. We return success even if pdsp
166204501690SMurali Karicheri 	 * is not loaded as acc channels are optional on having
166304501690SMurali Karicheri 	 * firmware availability in the system. We set the loaded
166404501690SMurali Karicheri 	 * and stated flag and when initialize the acc range, check
166504501690SMurali Karicheri 	 * it and init the range only if pdsp is started.
166604501690SMurali Karicheri 	 */
166741f93af9SSandeep Nair 	for_each_pdsp(kdev, pdsp) {
166841f93af9SSandeep Nair 		ret = knav_queue_load_pdsp(kdev, pdsp);
166904501690SMurali Karicheri 		if (!ret)
167004501690SMurali Karicheri 			pdsp->loaded = true;
167141f93af9SSandeep Nair 	}
167241f93af9SSandeep Nair 
167341f93af9SSandeep Nair 	for_each_pdsp(kdev, pdsp) {
167404501690SMurali Karicheri 		if (pdsp->loaded) {
167541f93af9SSandeep Nair 			ret = knav_queue_start_pdsp(kdev, pdsp);
167604501690SMurali Karicheri 			if (!ret)
167704501690SMurali Karicheri 				pdsp->started = true;
167804501690SMurali Karicheri 		}
167941f93af9SSandeep Nair 	}
168041f93af9SSandeep Nair 	return 0;
168141f93af9SSandeep Nair }
168241f93af9SSandeep Nair 
168341f93af9SSandeep Nair static inline struct knav_qmgr_info *knav_find_qmgr(unsigned id)
168441f93af9SSandeep Nair {
168541f93af9SSandeep Nair 	struct knav_qmgr_info *qmgr;
168641f93af9SSandeep Nair 
168741f93af9SSandeep Nair 	for_each_qmgr(kdev, qmgr) {
168841f93af9SSandeep Nair 		if ((id >= qmgr->start_queue) &&
168941f93af9SSandeep Nair 		    (id < qmgr->start_queue + qmgr->num_queues))
169041f93af9SSandeep Nair 			return qmgr;
169141f93af9SSandeep Nair 	}
169241f93af9SSandeep Nair 	return NULL;
169341f93af9SSandeep Nair }
169441f93af9SSandeep Nair 
169541f93af9SSandeep Nair static int knav_queue_init_queue(struct knav_device *kdev,
169641f93af9SSandeep Nair 					struct knav_range_info *range,
169741f93af9SSandeep Nair 					struct knav_queue_inst *inst,
169841f93af9SSandeep Nair 					unsigned id)
169941f93af9SSandeep Nair {
170041f93af9SSandeep Nair 	char irq_name[KNAV_NAME_SIZE];
170141f93af9SSandeep Nair 	inst->qmgr = knav_find_qmgr(id);
170241f93af9SSandeep Nair 	if (!inst->qmgr)
170341f93af9SSandeep Nair 		return -1;
170441f93af9SSandeep Nair 
170541f93af9SSandeep Nair 	INIT_LIST_HEAD(&inst->handles);
170641f93af9SSandeep Nair 	inst->kdev = kdev;
170741f93af9SSandeep Nair 	inst->range = range;
170841f93af9SSandeep Nair 	inst->irq_num = -1;
170941f93af9SSandeep Nair 	inst->id = id;
171041f93af9SSandeep Nair 	scnprintf(irq_name, sizeof(irq_name), "hwqueue-%d", id);
171141f93af9SSandeep Nair 	inst->irq_name = kstrndup(irq_name, sizeof(irq_name), GFP_KERNEL);
171241f93af9SSandeep Nair 
171341f93af9SSandeep Nair 	if (range->ops && range->ops->init_queue)
171441f93af9SSandeep Nair 		return range->ops->init_queue(range, inst);
171541f93af9SSandeep Nair 	else
171641f93af9SSandeep Nair 		return 0;
171741f93af9SSandeep Nair }
171841f93af9SSandeep Nair 
171941f93af9SSandeep Nair static int knav_queue_init_queues(struct knav_device *kdev)
172041f93af9SSandeep Nair {
172141f93af9SSandeep Nair 	struct knav_range_info *range;
172241f93af9SSandeep Nair 	int size, id, base_idx;
172341f93af9SSandeep Nair 	int idx = 0, ret = 0;
172441f93af9SSandeep Nair 
172541f93af9SSandeep Nair 	/* how much do we need for instance data? */
172641f93af9SSandeep Nair 	size = sizeof(struct knav_queue_inst);
172741f93af9SSandeep Nair 
172841f93af9SSandeep Nair 	/* round this up to a power of 2, keep the index to instance
172941f93af9SSandeep Nair 	 * arithmetic fast.
173041f93af9SSandeep Nair 	 * */
173141f93af9SSandeep Nair 	kdev->inst_shift = order_base_2(size);
173241f93af9SSandeep Nair 	size = (1 << kdev->inst_shift) * kdev->num_queues_in_use;
173341f93af9SSandeep Nair 	kdev->instances = devm_kzalloc(kdev->dev, size, GFP_KERNEL);
173441f93af9SSandeep Nair 	if (!kdev->instances)
173539179cb5SAxel Lin 		return -ENOMEM;
173641f93af9SSandeep Nair 
173741f93af9SSandeep Nair 	for_each_queue_range(kdev, range) {
173841f93af9SSandeep Nair 		if (range->ops && range->ops->init_range)
173941f93af9SSandeep Nair 			range->ops->init_range(range);
174041f93af9SSandeep Nair 		base_idx = idx;
174141f93af9SSandeep Nair 		for (id = range->queue_base;
174241f93af9SSandeep Nair 		     id < range->queue_base + range->num_queues; id++, idx++) {
174341f93af9SSandeep Nair 			ret = knav_queue_init_queue(kdev, range,
174441f93af9SSandeep Nair 					knav_queue_idx_to_inst(kdev, idx), id);
174541f93af9SSandeep Nair 			if (ret < 0)
174641f93af9SSandeep Nair 				return ret;
174741f93af9SSandeep Nair 		}
174841f93af9SSandeep Nair 		range->queue_base_inst =
174941f93af9SSandeep Nair 			knav_queue_idx_to_inst(kdev, base_idx);
175041f93af9SSandeep Nair 	}
175141f93af9SSandeep Nair 	return 0;
175241f93af9SSandeep Nair }
175341f93af9SSandeep Nair 
1754350601b4SMurali Karicheri /* Match table for of_platform binding */
1755350601b4SMurali Karicheri static const struct of_device_id keystone_qmss_of_match[] = {
1756350601b4SMurali Karicheri 	{
1757350601b4SMurali Karicheri 		.compatible = "ti,keystone-navigator-qmss",
1758350601b4SMurali Karicheri 	},
1759350601b4SMurali Karicheri 	{
1760350601b4SMurali Karicheri 		.compatible = "ti,66ak2g-navss-qm",
1761350601b4SMurali Karicheri 		.data	= (void *)QMSS_66AK2G,
1762350601b4SMurali Karicheri 	},
1763350601b4SMurali Karicheri 	{},
1764350601b4SMurali Karicheri };
1765350601b4SMurali Karicheri MODULE_DEVICE_TABLE(of, keystone_qmss_of_match);
1766350601b4SMurali Karicheri 
176741f93af9SSandeep Nair static int knav_queue_probe(struct platform_device *pdev)
176841f93af9SSandeep Nair {
176941f93af9SSandeep Nair 	struct device_node *node = pdev->dev.of_node;
177041f93af9SSandeep Nair 	struct device_node *qmgrs, *queue_pools, *regions, *pdsps;
1771350601b4SMurali Karicheri 	const struct of_device_id *match;
177241f93af9SSandeep Nair 	struct device *dev = &pdev->dev;
177341f93af9SSandeep Nair 	u32 temp[2];
177441f93af9SSandeep Nair 	int ret;
177541f93af9SSandeep Nair 
177641f93af9SSandeep Nair 	if (!node) {
177741f93af9SSandeep Nair 		dev_err(dev, "device tree info unavailable\n");
177841f93af9SSandeep Nair 		return -ENODEV;
177941f93af9SSandeep Nair 	}
178041f93af9SSandeep Nair 
178141f93af9SSandeep Nair 	kdev = devm_kzalloc(dev, sizeof(struct knav_device), GFP_KERNEL);
178241f93af9SSandeep Nair 	if (!kdev) {
178341f93af9SSandeep Nair 		dev_err(dev, "memory allocation failed\n");
178441f93af9SSandeep Nair 		return -ENOMEM;
178541f93af9SSandeep Nair 	}
178641f93af9SSandeep Nair 
1787350601b4SMurali Karicheri 	match = of_match_device(of_match_ptr(keystone_qmss_of_match), dev);
1788350601b4SMurali Karicheri 	if (match && match->data)
1789350601b4SMurali Karicheri 		kdev->version = QMSS_66AK2G;
1790350601b4SMurali Karicheri 
179141f93af9SSandeep Nair 	platform_set_drvdata(pdev, kdev);
179241f93af9SSandeep Nair 	kdev->dev = dev;
179341f93af9SSandeep Nair 	INIT_LIST_HEAD(&kdev->queue_ranges);
179441f93af9SSandeep Nair 	INIT_LIST_HEAD(&kdev->qmgrs);
179541f93af9SSandeep Nair 	INIT_LIST_HEAD(&kdev->pools);
179641f93af9SSandeep Nair 	INIT_LIST_HEAD(&kdev->regions);
179741f93af9SSandeep Nair 	INIT_LIST_HEAD(&kdev->pdsps);
179841f93af9SSandeep Nair 
179941f93af9SSandeep Nair 	pm_runtime_enable(&pdev->dev);
180041f93af9SSandeep Nair 	ret = pm_runtime_get_sync(&pdev->dev);
180141f93af9SSandeep Nair 	if (ret < 0) {
180241f93af9SSandeep Nair 		dev_err(dev, "Failed to enable QMSS\n");
180341f93af9SSandeep Nair 		return ret;
180441f93af9SSandeep Nair 	}
180541f93af9SSandeep Nair 
180641f93af9SSandeep Nair 	if (of_property_read_u32_array(node, "queue-range", temp, 2)) {
180741f93af9SSandeep Nair 		dev_err(dev, "queue-range not specified\n");
180841f93af9SSandeep Nair 		ret = -ENODEV;
180941f93af9SSandeep Nair 		goto err;
181041f93af9SSandeep Nair 	}
181141f93af9SSandeep Nair 	kdev->base_id    = temp[0];
181241f93af9SSandeep Nair 	kdev->num_queues = temp[1];
181341f93af9SSandeep Nair 
181441f93af9SSandeep Nair 	/* Initialize queue managers using device tree configuration */
181541f93af9SSandeep Nair 	qmgrs =  of_get_child_by_name(node, "qmgrs");
181641f93af9SSandeep Nair 	if (!qmgrs) {
181741f93af9SSandeep Nair 		dev_err(dev, "queue manager info not specified\n");
181841f93af9SSandeep Nair 		ret = -ENODEV;
181941f93af9SSandeep Nair 		goto err;
182041f93af9SSandeep Nair 	}
182141f93af9SSandeep Nair 	ret = knav_queue_init_qmgrs(kdev, qmgrs);
182241f93af9SSandeep Nair 	of_node_put(qmgrs);
182341f93af9SSandeep Nair 	if (ret)
182441f93af9SSandeep Nair 		goto err;
182541f93af9SSandeep Nair 
182641f93af9SSandeep Nair 	/* get pdsp configuration values from device tree */
182741f93af9SSandeep Nair 	pdsps =  of_get_child_by_name(node, "pdsps");
182841f93af9SSandeep Nair 	if (pdsps) {
182941f93af9SSandeep Nair 		ret = knav_queue_init_pdsps(kdev, pdsps);
183041f93af9SSandeep Nair 		if (ret)
183141f93af9SSandeep Nair 			goto err;
183241f93af9SSandeep Nair 
183341f93af9SSandeep Nair 		ret = knav_queue_start_pdsps(kdev);
183441f93af9SSandeep Nair 		if (ret)
183541f93af9SSandeep Nair 			goto err;
183641f93af9SSandeep Nair 	}
183741f93af9SSandeep Nair 	of_node_put(pdsps);
183841f93af9SSandeep Nair 
183941f93af9SSandeep Nair 	/* get usable queue range values from device tree */
184041f93af9SSandeep Nair 	queue_pools = of_get_child_by_name(node, "queue-pools");
184141f93af9SSandeep Nair 	if (!queue_pools) {
184241f93af9SSandeep Nair 		dev_err(dev, "queue-pools not specified\n");
184341f93af9SSandeep Nair 		ret = -ENODEV;
184441f93af9SSandeep Nair 		goto err;
184541f93af9SSandeep Nair 	}
184641f93af9SSandeep Nair 	ret = knav_setup_queue_pools(kdev, queue_pools);
184741f93af9SSandeep Nair 	of_node_put(queue_pools);
184841f93af9SSandeep Nair 	if (ret)
184941f93af9SSandeep Nair 		goto err;
185041f93af9SSandeep Nair 
185141f93af9SSandeep Nair 	ret = knav_get_link_ram(kdev, "linkram0", &kdev->link_rams[0]);
185241f93af9SSandeep Nair 	if (ret) {
185341f93af9SSandeep Nair 		dev_err(kdev->dev, "could not setup linking ram\n");
185441f93af9SSandeep Nair 		goto err;
185541f93af9SSandeep Nair 	}
185641f93af9SSandeep Nair 
185741f93af9SSandeep Nair 	ret = knav_get_link_ram(kdev, "linkram1", &kdev->link_rams[1]);
185841f93af9SSandeep Nair 	if (ret) {
185941f93af9SSandeep Nair 		/*
186041f93af9SSandeep Nair 		 * nothing really, we have one linking ram already, so we just
186141f93af9SSandeep Nair 		 * live within our means
186241f93af9SSandeep Nair 		 */
186341f93af9SSandeep Nair 	}
186441f93af9SSandeep Nair 
186541f93af9SSandeep Nair 	ret = knav_queue_setup_link_ram(kdev);
186641f93af9SSandeep Nair 	if (ret)
186741f93af9SSandeep Nair 		goto err;
186841f93af9SSandeep Nair 
186941f93af9SSandeep Nair 	regions =  of_get_child_by_name(node, "descriptor-regions");
187041f93af9SSandeep Nair 	if (!regions) {
187141f93af9SSandeep Nair 		dev_err(dev, "descriptor-regions not specified\n");
187241f93af9SSandeep Nair 		goto err;
187341f93af9SSandeep Nair 	}
187441f93af9SSandeep Nair 	ret = knav_queue_setup_regions(kdev, regions);
187541f93af9SSandeep Nair 	of_node_put(regions);
187641f93af9SSandeep Nair 	if (ret)
187741f93af9SSandeep Nair 		goto err;
187841f93af9SSandeep Nair 
187941f93af9SSandeep Nair 	ret = knav_queue_init_queues(kdev);
188041f93af9SSandeep Nair 	if (ret < 0) {
188141f93af9SSandeep Nair 		dev_err(dev, "hwqueue initialization failed\n");
188241f93af9SSandeep Nair 		goto err;
188341f93af9SSandeep Nair 	}
188441f93af9SSandeep Nair 
188541f93af9SSandeep Nair 	debugfs_create_file("qmss", S_IFREG | S_IRUGO, NULL, NULL,
188641f93af9SSandeep Nair 			    &knav_queue_debug_ops);
1887a2dd6877SMurali Karicheri 	device_ready = true;
188841f93af9SSandeep Nair 	return 0;
188941f93af9SSandeep Nair 
189041f93af9SSandeep Nair err:
189141f93af9SSandeep Nair 	knav_queue_stop_pdsps(kdev);
189241f93af9SSandeep Nair 	knav_queue_free_regions(kdev);
189341f93af9SSandeep Nair 	knav_free_queue_ranges(kdev);
189441f93af9SSandeep Nair 	pm_runtime_put_sync(&pdev->dev);
189541f93af9SSandeep Nair 	pm_runtime_disable(&pdev->dev);
189641f93af9SSandeep Nair 	return ret;
189741f93af9SSandeep Nair }
189841f93af9SSandeep Nair 
189941f93af9SSandeep Nair static int knav_queue_remove(struct platform_device *pdev)
190041f93af9SSandeep Nair {
190141f93af9SSandeep Nair 	/* TODO: Free resources */
190241f93af9SSandeep Nair 	pm_runtime_put_sync(&pdev->dev);
190341f93af9SSandeep Nair 	pm_runtime_disable(&pdev->dev);
190441f93af9SSandeep Nair 	return 0;
190541f93af9SSandeep Nair }
190641f93af9SSandeep Nair 
190741f93af9SSandeep Nair static struct platform_driver keystone_qmss_driver = {
190841f93af9SSandeep Nair 	.probe		= knav_queue_probe,
190941f93af9SSandeep Nair 	.remove		= knav_queue_remove,
191041f93af9SSandeep Nair 	.driver		= {
191141f93af9SSandeep Nair 		.name	= "keystone-navigator-qmss",
191241f93af9SSandeep Nair 		.of_match_table = keystone_qmss_of_match,
191341f93af9SSandeep Nair 	},
191441f93af9SSandeep Nair };
191541f93af9SSandeep Nair module_platform_driver(keystone_qmss_driver);
191641f93af9SSandeep Nair 
191741f93af9SSandeep Nair MODULE_LICENSE("GPL v2");
191841f93af9SSandeep Nair MODULE_DESCRIPTION("TI QMSS driver for Keystone SOCs");
191941f93af9SSandeep Nair MODULE_AUTHOR("Sandeep Nair <sandeep_n@ti.com>");
192041f93af9SSandeep Nair MODULE_AUTHOR("Santosh Shilimkar <santosh.shilimkar@ti.com>");
1921