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