141f93af9SSandeep Nair /* 241f93af9SSandeep Nair * Keystone Queue Manager subsystem driver 341f93af9SSandeep Nair * 441f93af9SSandeep Nair * Copyright (C) 2014 Texas Instruments Incorporated - http://www.ti.com 541f93af9SSandeep Nair * Authors: Sandeep Nair <sandeep_n@ti.com> 641f93af9SSandeep Nair * Cyril Chemparathy <cyril@ti.com> 741f93af9SSandeep Nair * Santosh Shilimkar <santosh.shilimkar@ti.com> 841f93af9SSandeep Nair * 941f93af9SSandeep Nair * This program is free software; you can redistribute it and/or 1041f93af9SSandeep Nair * modify it under the terms of the GNU General Public License 1141f93af9SSandeep Nair * version 2 as published by the Free Software Foundation. 1241f93af9SSandeep Nair * 1341f93af9SSandeep Nair * This program is distributed in the hope that it will be useful, but 1441f93af9SSandeep Nair * WITHOUT ANY WARRANTY; without even the implied warranty of 1541f93af9SSandeep Nair * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 1641f93af9SSandeep Nair * General Public License for more details. 1741f93af9SSandeep Nair */ 1841f93af9SSandeep Nair 1941f93af9SSandeep Nair #include <linux/debugfs.h> 20*6e03f653SMurali Karicheri #include <linux/dma-mapping.h> 21*6e03f653SMurali Karicheri #include <linux/firmware.h> 22*6e03f653SMurali Karicheri #include <linux/interrupt.h> 23*6e03f653SMurali Karicheri #include <linux/io.h> 24*6e03f653SMurali Karicheri #include <linux/module.h> 25*6e03f653SMurali Karicheri #include <linux/of_address.h> 26*6e03f653SMurali Karicheri #include <linux/of_device.h> 27*6e03f653SMurali Karicheri #include <linux/of_irq.h> 28*6e03f653SMurali Karicheri #include <linux/pm_runtime.h> 29*6e03f653SMurali Karicheri #include <linux/slab.h> 3041f93af9SSandeep Nair #include <linux/soc/ti/knav_qmss.h> 3141f93af9SSandeep Nair 3241f93af9SSandeep Nair #include "knav_qmss.h" 3341f93af9SSandeep Nair 3441f93af9SSandeep Nair static struct knav_device *kdev; 3541f93af9SSandeep Nair static DEFINE_MUTEX(knav_dev_lock); 3641f93af9SSandeep Nair 3741f93af9SSandeep Nair /* Queue manager register indices in DTS */ 3841f93af9SSandeep Nair #define KNAV_QUEUE_PEEK_REG_INDEX 0 3941f93af9SSandeep Nair #define KNAV_QUEUE_STATUS_REG_INDEX 1 4041f93af9SSandeep Nair #define KNAV_QUEUE_CONFIG_REG_INDEX 2 4141f93af9SSandeep Nair #define KNAV_QUEUE_REGION_REG_INDEX 3 4241f93af9SSandeep Nair #define KNAV_QUEUE_PUSH_REG_INDEX 4 4341f93af9SSandeep Nair #define KNAV_QUEUE_POP_REG_INDEX 5 4441f93af9SSandeep Nair 4541f93af9SSandeep Nair /* PDSP register indices in DTS */ 4641f93af9SSandeep Nair #define KNAV_QUEUE_PDSP_IRAM_REG_INDEX 0 4741f93af9SSandeep Nair #define KNAV_QUEUE_PDSP_REGS_REG_INDEX 1 4841f93af9SSandeep Nair #define KNAV_QUEUE_PDSP_INTD_REG_INDEX 2 4941f93af9SSandeep Nair #define KNAV_QUEUE_PDSP_CMD_REG_INDEX 3 5041f93af9SSandeep Nair 5141f93af9SSandeep Nair #define knav_queue_idx_to_inst(kdev, idx) \ 5241f93af9SSandeep Nair (kdev->instances + (idx << kdev->inst_shift)) 5341f93af9SSandeep Nair 5441f93af9SSandeep Nair #define for_each_handle_rcu(qh, inst) \ 5541f93af9SSandeep Nair list_for_each_entry_rcu(qh, &inst->handles, list) 5641f93af9SSandeep Nair 5741f93af9SSandeep Nair #define for_each_instance(idx, inst, kdev) \ 5841f93af9SSandeep Nair for (idx = 0, inst = kdev->instances; \ 5941f93af9SSandeep Nair idx < (kdev)->num_queues_in_use; \ 6041f93af9SSandeep Nair idx++, inst = knav_queue_idx_to_inst(kdev, idx)) 6141f93af9SSandeep Nair 6296ee19beSMurali Karicheri /* All firmware file names end up here. List the firmware file names below. 6396ee19beSMurali Karicheri * Newest followed by older ones. Search is done from start of the array 6496ee19beSMurali Karicheri * until a firmware file is found. 6596ee19beSMurali Karicheri */ 6696ee19beSMurali Karicheri const char *knav_acc_firmwares[] = {"ks2_qmss_pdsp_acc48.bin"}; 6796ee19beSMurali Karicheri 6841f93af9SSandeep Nair /** 6941f93af9SSandeep Nair * knav_queue_notify: qmss queue notfier call 7041f93af9SSandeep Nair * 7141f93af9SSandeep Nair * @inst: qmss queue instance like accumulator 7241f93af9SSandeep Nair */ 7341f93af9SSandeep Nair void knav_queue_notify(struct knav_queue_inst *inst) 7441f93af9SSandeep Nair { 7541f93af9SSandeep Nair struct knav_queue *qh; 7641f93af9SSandeep Nair 7741f93af9SSandeep Nair if (!inst) 7841f93af9SSandeep Nair return; 7941f93af9SSandeep Nair 8041f93af9SSandeep Nair rcu_read_lock(); 8141f93af9SSandeep Nair for_each_handle_rcu(qh, inst) { 8241f93af9SSandeep Nair if (atomic_read(&qh->notifier_enabled) <= 0) 8341f93af9SSandeep Nair continue; 8441f93af9SSandeep Nair if (WARN_ON(!qh->notifier_fn)) 8541f93af9SSandeep Nair continue; 8641f93af9SSandeep Nair atomic_inc(&qh->stats.notifies); 8741f93af9SSandeep Nair qh->notifier_fn(qh->notifier_fn_arg); 8841f93af9SSandeep Nair } 8941f93af9SSandeep Nair rcu_read_unlock(); 9041f93af9SSandeep Nair } 9141f93af9SSandeep Nair EXPORT_SYMBOL_GPL(knav_queue_notify); 9241f93af9SSandeep Nair 9341f93af9SSandeep Nair static irqreturn_t knav_queue_int_handler(int irq, void *_instdata) 9441f93af9SSandeep Nair { 9541f93af9SSandeep Nair struct knav_queue_inst *inst = _instdata; 9641f93af9SSandeep Nair 9741f93af9SSandeep Nair knav_queue_notify(inst); 9841f93af9SSandeep Nair return IRQ_HANDLED; 9941f93af9SSandeep Nair } 10041f93af9SSandeep Nair 10141f93af9SSandeep Nair static int knav_queue_setup_irq(struct knav_range_info *range, 10241f93af9SSandeep Nair struct knav_queue_inst *inst) 10341f93af9SSandeep Nair { 10441f93af9SSandeep Nair unsigned queue = inst->id - range->queue_base; 10541f93af9SSandeep Nair unsigned long cpu_map; 10641f93af9SSandeep Nair int ret = 0, irq; 10741f93af9SSandeep Nair 10841f93af9SSandeep Nair if (range->flags & RANGE_HAS_IRQ) { 10941f93af9SSandeep Nair irq = range->irqs[queue].irq; 11041f93af9SSandeep Nair cpu_map = range->irqs[queue].cpu_map; 11141f93af9SSandeep Nair ret = request_irq(irq, knav_queue_int_handler, 0, 11241f93af9SSandeep Nair inst->irq_name, inst); 11341f93af9SSandeep Nair if (ret) 11441f93af9SSandeep Nair return ret; 11541f93af9SSandeep Nair disable_irq(irq); 11641f93af9SSandeep Nair if (cpu_map) { 11741f93af9SSandeep Nair ret = irq_set_affinity_hint(irq, to_cpumask(&cpu_map)); 11841f93af9SSandeep Nair if (ret) { 11941f93af9SSandeep Nair dev_warn(range->kdev->dev, 12041f93af9SSandeep Nair "Failed to set IRQ affinity\n"); 12141f93af9SSandeep Nair return ret; 12241f93af9SSandeep Nair } 12341f93af9SSandeep Nair } 12441f93af9SSandeep Nair } 12541f93af9SSandeep Nair return ret; 12641f93af9SSandeep Nair } 12741f93af9SSandeep Nair 12841f93af9SSandeep Nair static void knav_queue_free_irq(struct knav_queue_inst *inst) 12941f93af9SSandeep Nair { 13041f93af9SSandeep Nair struct knav_range_info *range = inst->range; 13141f93af9SSandeep Nair unsigned queue = inst->id - inst->range->queue_base; 13241f93af9SSandeep Nair int irq; 13341f93af9SSandeep Nair 13441f93af9SSandeep Nair if (range->flags & RANGE_HAS_IRQ) { 13541f93af9SSandeep Nair irq = range->irqs[queue].irq; 13641f93af9SSandeep Nair irq_set_affinity_hint(irq, NULL); 13741f93af9SSandeep Nair free_irq(irq, inst); 13841f93af9SSandeep Nair } 13941f93af9SSandeep Nair } 14041f93af9SSandeep Nair 14141f93af9SSandeep Nair static inline bool knav_queue_is_busy(struct knav_queue_inst *inst) 14241f93af9SSandeep Nair { 14341f93af9SSandeep Nair return !list_empty(&inst->handles); 14441f93af9SSandeep Nair } 14541f93af9SSandeep Nair 14641f93af9SSandeep Nair static inline bool knav_queue_is_reserved(struct knav_queue_inst *inst) 14741f93af9SSandeep Nair { 14841f93af9SSandeep Nair return inst->range->flags & RANGE_RESERVED; 14941f93af9SSandeep Nair } 15041f93af9SSandeep Nair 15141f93af9SSandeep Nair static inline bool knav_queue_is_shared(struct knav_queue_inst *inst) 15241f93af9SSandeep Nair { 15341f93af9SSandeep Nair struct knav_queue *tmp; 15441f93af9SSandeep Nair 15541f93af9SSandeep Nair rcu_read_lock(); 15641f93af9SSandeep Nair for_each_handle_rcu(tmp, inst) { 15741f93af9SSandeep Nair if (tmp->flags & KNAV_QUEUE_SHARED) { 15841f93af9SSandeep Nair rcu_read_unlock(); 15941f93af9SSandeep Nair return true; 16041f93af9SSandeep Nair } 16141f93af9SSandeep Nair } 16241f93af9SSandeep Nair rcu_read_unlock(); 16341f93af9SSandeep Nair return false; 16441f93af9SSandeep Nair } 16541f93af9SSandeep Nair 16641f93af9SSandeep Nair static inline bool knav_queue_match_type(struct knav_queue_inst *inst, 16741f93af9SSandeep Nair unsigned type) 16841f93af9SSandeep Nair { 16941f93af9SSandeep Nair if ((type == KNAV_QUEUE_QPEND) && 17041f93af9SSandeep Nair (inst->range->flags & RANGE_HAS_IRQ)) { 17141f93af9SSandeep Nair return true; 17241f93af9SSandeep Nair } else if ((type == KNAV_QUEUE_ACC) && 17341f93af9SSandeep Nair (inst->range->flags & RANGE_HAS_ACCUMULATOR)) { 17441f93af9SSandeep Nair return true; 17541f93af9SSandeep Nair } else if ((type == KNAV_QUEUE_GP) && 17641f93af9SSandeep Nair !(inst->range->flags & 17741f93af9SSandeep Nair (RANGE_HAS_ACCUMULATOR | RANGE_HAS_IRQ))) { 17841f93af9SSandeep Nair return true; 17941f93af9SSandeep Nair } 18041f93af9SSandeep Nair return false; 18141f93af9SSandeep Nair } 18241f93af9SSandeep Nair 18341f93af9SSandeep Nair static inline struct knav_queue_inst * 18441f93af9SSandeep Nair knav_queue_match_id_to_inst(struct knav_device *kdev, unsigned id) 18541f93af9SSandeep Nair { 18641f93af9SSandeep Nair struct knav_queue_inst *inst; 18741f93af9SSandeep Nair int idx; 18841f93af9SSandeep Nair 18941f93af9SSandeep Nair for_each_instance(idx, inst, kdev) { 19041f93af9SSandeep Nair if (inst->id == id) 19141f93af9SSandeep Nair return inst; 19241f93af9SSandeep Nair } 19341f93af9SSandeep Nair return NULL; 19441f93af9SSandeep Nair } 19541f93af9SSandeep Nair 19641f93af9SSandeep Nair static inline struct knav_queue_inst *knav_queue_find_by_id(int id) 19741f93af9SSandeep Nair { 19841f93af9SSandeep Nair if (kdev->base_id <= id && 19941f93af9SSandeep Nair kdev->base_id + kdev->num_queues > id) { 20041f93af9SSandeep Nair id -= kdev->base_id; 20141f93af9SSandeep Nair return knav_queue_match_id_to_inst(kdev, id); 20241f93af9SSandeep Nair } 20341f93af9SSandeep Nair return NULL; 20441f93af9SSandeep Nair } 20541f93af9SSandeep Nair 20641f93af9SSandeep Nair static struct knav_queue *__knav_queue_open(struct knav_queue_inst *inst, 20741f93af9SSandeep Nair const char *name, unsigned flags) 20841f93af9SSandeep Nair { 20941f93af9SSandeep Nair struct knav_queue *qh; 21041f93af9SSandeep Nair unsigned id; 21141f93af9SSandeep Nair int ret = 0; 21241f93af9SSandeep Nair 21341f93af9SSandeep Nair qh = devm_kzalloc(inst->kdev->dev, sizeof(*qh), GFP_KERNEL); 21441f93af9SSandeep Nair if (!qh) 21541f93af9SSandeep Nair return ERR_PTR(-ENOMEM); 21641f93af9SSandeep Nair 21741f93af9SSandeep Nair qh->flags = flags; 21841f93af9SSandeep Nair qh->inst = inst; 21941f93af9SSandeep Nair id = inst->id - inst->qmgr->start_queue; 22041f93af9SSandeep Nair qh->reg_push = &inst->qmgr->reg_push[id]; 22141f93af9SSandeep Nair qh->reg_pop = &inst->qmgr->reg_pop[id]; 22241f93af9SSandeep Nair qh->reg_peek = &inst->qmgr->reg_peek[id]; 22341f93af9SSandeep Nair 22441f93af9SSandeep Nair /* first opener? */ 22541f93af9SSandeep Nair if (!knav_queue_is_busy(inst)) { 22641f93af9SSandeep Nair struct knav_range_info *range = inst->range; 22741f93af9SSandeep Nair 22841f93af9SSandeep Nair inst->name = kstrndup(name, KNAV_NAME_SIZE, GFP_KERNEL); 22941f93af9SSandeep Nair if (range->ops && range->ops->open_queue) 23041f93af9SSandeep Nair ret = range->ops->open_queue(range, inst, flags); 23141f93af9SSandeep Nair 23241f93af9SSandeep Nair if (ret) { 23341f93af9SSandeep Nair devm_kfree(inst->kdev->dev, qh); 23441f93af9SSandeep Nair return ERR_PTR(ret); 23541f93af9SSandeep Nair } 23641f93af9SSandeep Nair } 23741f93af9SSandeep Nair list_add_tail_rcu(&qh->list, &inst->handles); 23841f93af9SSandeep Nair return qh; 23941f93af9SSandeep Nair } 24041f93af9SSandeep Nair 24141f93af9SSandeep Nair static struct knav_queue * 24241f93af9SSandeep Nair knav_queue_open_by_id(const char *name, unsigned id, unsigned flags) 24341f93af9SSandeep Nair { 24441f93af9SSandeep Nair struct knav_queue_inst *inst; 24541f93af9SSandeep Nair struct knav_queue *qh; 24641f93af9SSandeep Nair 24741f93af9SSandeep Nair mutex_lock(&knav_dev_lock); 24841f93af9SSandeep Nair 24941f93af9SSandeep Nair qh = ERR_PTR(-ENODEV); 25041f93af9SSandeep Nair inst = knav_queue_find_by_id(id); 25141f93af9SSandeep Nair if (!inst) 25241f93af9SSandeep Nair goto unlock_ret; 25341f93af9SSandeep Nair 25441f93af9SSandeep Nair qh = ERR_PTR(-EEXIST); 25541f93af9SSandeep Nair if (!(flags & KNAV_QUEUE_SHARED) && knav_queue_is_busy(inst)) 25641f93af9SSandeep Nair goto unlock_ret; 25741f93af9SSandeep Nair 25841f93af9SSandeep Nair qh = ERR_PTR(-EBUSY); 25941f93af9SSandeep Nair if ((flags & KNAV_QUEUE_SHARED) && 26041f93af9SSandeep Nair (knav_queue_is_busy(inst) && !knav_queue_is_shared(inst))) 26141f93af9SSandeep Nair goto unlock_ret; 26241f93af9SSandeep Nair 26341f93af9SSandeep Nair qh = __knav_queue_open(inst, name, flags); 26441f93af9SSandeep Nair 26541f93af9SSandeep Nair unlock_ret: 26641f93af9SSandeep Nair mutex_unlock(&knav_dev_lock); 26741f93af9SSandeep Nair 26841f93af9SSandeep Nair return qh; 26941f93af9SSandeep Nair } 27041f93af9SSandeep Nair 27141f93af9SSandeep Nair static struct knav_queue *knav_queue_open_by_type(const char *name, 27241f93af9SSandeep Nair unsigned type, unsigned flags) 27341f93af9SSandeep Nair { 27441f93af9SSandeep Nair struct knav_queue_inst *inst; 27541f93af9SSandeep Nair struct knav_queue *qh = ERR_PTR(-EINVAL); 27641f93af9SSandeep Nair int idx; 27741f93af9SSandeep Nair 27841f93af9SSandeep Nair mutex_lock(&knav_dev_lock); 27941f93af9SSandeep Nair 28041f93af9SSandeep Nair for_each_instance(idx, inst, kdev) { 28141f93af9SSandeep Nair if (knav_queue_is_reserved(inst)) 28241f93af9SSandeep Nair continue; 28341f93af9SSandeep Nair if (!knav_queue_match_type(inst, type)) 28441f93af9SSandeep Nair continue; 28541f93af9SSandeep Nair if (knav_queue_is_busy(inst)) 28641f93af9SSandeep Nair continue; 28741f93af9SSandeep Nair qh = __knav_queue_open(inst, name, flags); 28841f93af9SSandeep Nair goto unlock_ret; 28941f93af9SSandeep Nair } 29041f93af9SSandeep Nair 29141f93af9SSandeep Nair unlock_ret: 29241f93af9SSandeep Nair mutex_unlock(&knav_dev_lock); 29341f93af9SSandeep Nair return qh; 29441f93af9SSandeep Nair } 29541f93af9SSandeep Nair 29641f93af9SSandeep Nair static void knav_queue_set_notify(struct knav_queue_inst *inst, bool enabled) 29741f93af9SSandeep Nair { 29841f93af9SSandeep Nair struct knav_range_info *range = inst->range; 29941f93af9SSandeep Nair 30041f93af9SSandeep Nair if (range->ops && range->ops->set_notify) 30141f93af9SSandeep Nair range->ops->set_notify(range, inst, enabled); 30241f93af9SSandeep Nair } 30341f93af9SSandeep Nair 30441f93af9SSandeep Nair static int knav_queue_enable_notifier(struct knav_queue *qh) 30541f93af9SSandeep Nair { 30641f93af9SSandeep Nair struct knav_queue_inst *inst = qh->inst; 30741f93af9SSandeep Nair bool first; 30841f93af9SSandeep Nair 30941f93af9SSandeep Nair if (WARN_ON(!qh->notifier_fn)) 31041f93af9SSandeep Nair return -EINVAL; 31141f93af9SSandeep Nair 31241f93af9SSandeep Nair /* Adjust the per handle notifier count */ 31341f93af9SSandeep Nair first = (atomic_inc_return(&qh->notifier_enabled) == 1); 31441f93af9SSandeep Nair if (!first) 31541f93af9SSandeep Nair return 0; /* nothing to do */ 31641f93af9SSandeep Nair 31741f93af9SSandeep Nair /* Now adjust the per instance notifier count */ 31841f93af9SSandeep Nair first = (atomic_inc_return(&inst->num_notifiers) == 1); 31941f93af9SSandeep Nair if (first) 32041f93af9SSandeep Nair knav_queue_set_notify(inst, true); 32141f93af9SSandeep Nair 32241f93af9SSandeep Nair return 0; 32341f93af9SSandeep Nair } 32441f93af9SSandeep Nair 32541f93af9SSandeep Nair static int knav_queue_disable_notifier(struct knav_queue *qh) 32641f93af9SSandeep Nair { 32741f93af9SSandeep Nair struct knav_queue_inst *inst = qh->inst; 32841f93af9SSandeep Nair bool last; 32941f93af9SSandeep Nair 33041f93af9SSandeep Nair last = (atomic_dec_return(&qh->notifier_enabled) == 0); 33141f93af9SSandeep Nair if (!last) 33241f93af9SSandeep Nair return 0; /* nothing to do */ 33341f93af9SSandeep Nair 33441f93af9SSandeep Nair last = (atomic_dec_return(&inst->num_notifiers) == 0); 33541f93af9SSandeep Nair if (last) 33641f93af9SSandeep Nair knav_queue_set_notify(inst, false); 33741f93af9SSandeep Nair 33841f93af9SSandeep Nair return 0; 33941f93af9SSandeep Nair } 34041f93af9SSandeep Nair 34141f93af9SSandeep Nair static int knav_queue_set_notifier(struct knav_queue *qh, 34241f93af9SSandeep Nair struct knav_queue_notify_config *cfg) 34341f93af9SSandeep Nair { 34441f93af9SSandeep Nair knav_queue_notify_fn old_fn = qh->notifier_fn; 34541f93af9SSandeep Nair 34641f93af9SSandeep Nair if (!cfg) 34741f93af9SSandeep Nair return -EINVAL; 34841f93af9SSandeep Nair 34941f93af9SSandeep Nair if (!(qh->inst->range->flags & (RANGE_HAS_ACCUMULATOR | RANGE_HAS_IRQ))) 35041f93af9SSandeep Nair return -ENOTSUPP; 35141f93af9SSandeep Nair 35241f93af9SSandeep Nair if (!cfg->fn && old_fn) 35341f93af9SSandeep Nair knav_queue_disable_notifier(qh); 35441f93af9SSandeep Nair 35541f93af9SSandeep Nair qh->notifier_fn = cfg->fn; 35641f93af9SSandeep Nair qh->notifier_fn_arg = cfg->fn_arg; 35741f93af9SSandeep Nair 35841f93af9SSandeep Nair if (cfg->fn && !old_fn) 35941f93af9SSandeep Nair knav_queue_enable_notifier(qh); 36041f93af9SSandeep Nair 36141f93af9SSandeep Nair return 0; 36241f93af9SSandeep Nair } 36341f93af9SSandeep Nair 36441f93af9SSandeep Nair static int knav_gp_set_notify(struct knav_range_info *range, 36541f93af9SSandeep Nair struct knav_queue_inst *inst, 36641f93af9SSandeep Nair bool enabled) 36741f93af9SSandeep Nair { 36841f93af9SSandeep Nair unsigned queue; 36941f93af9SSandeep Nair 37041f93af9SSandeep Nair if (range->flags & RANGE_HAS_IRQ) { 37141f93af9SSandeep Nair queue = inst->id - range->queue_base; 37241f93af9SSandeep Nair if (enabled) 37341f93af9SSandeep Nair enable_irq(range->irqs[queue].irq); 37441f93af9SSandeep Nair else 37541f93af9SSandeep Nair disable_irq_nosync(range->irqs[queue].irq); 37641f93af9SSandeep Nair } 37741f93af9SSandeep Nair return 0; 37841f93af9SSandeep Nair } 37941f93af9SSandeep Nair 38041f93af9SSandeep Nair static int knav_gp_open_queue(struct knav_range_info *range, 38141f93af9SSandeep Nair struct knav_queue_inst *inst, unsigned flags) 38241f93af9SSandeep Nair { 38341f93af9SSandeep Nair return knav_queue_setup_irq(range, inst); 38441f93af9SSandeep Nair } 38541f93af9SSandeep Nair 38641f93af9SSandeep Nair static int knav_gp_close_queue(struct knav_range_info *range, 38741f93af9SSandeep Nair struct knav_queue_inst *inst) 38841f93af9SSandeep Nair { 38941f93af9SSandeep Nair knav_queue_free_irq(inst); 39041f93af9SSandeep Nair return 0; 39141f93af9SSandeep Nair } 39241f93af9SSandeep Nair 39341f93af9SSandeep Nair struct knav_range_ops knav_gp_range_ops = { 39441f93af9SSandeep Nair .set_notify = knav_gp_set_notify, 39541f93af9SSandeep Nair .open_queue = knav_gp_open_queue, 39641f93af9SSandeep Nair .close_queue = knav_gp_close_queue, 39741f93af9SSandeep Nair }; 39841f93af9SSandeep Nair 39941f93af9SSandeep Nair 40041f93af9SSandeep Nair static int knav_queue_get_count(void *qhandle) 40141f93af9SSandeep Nair { 40241f93af9SSandeep Nair struct knav_queue *qh = qhandle; 40341f93af9SSandeep Nair struct knav_queue_inst *inst = qh->inst; 40441f93af9SSandeep Nair 40541f93af9SSandeep Nair return readl_relaxed(&qh->reg_peek[0].entry_count) + 40641f93af9SSandeep Nair atomic_read(&inst->desc_count); 40741f93af9SSandeep Nair } 40841f93af9SSandeep Nair 40941f93af9SSandeep Nair static void knav_queue_debug_show_instance(struct seq_file *s, 41041f93af9SSandeep Nair struct knav_queue_inst *inst) 41141f93af9SSandeep Nair { 41241f93af9SSandeep Nair struct knav_device *kdev = inst->kdev; 41341f93af9SSandeep Nair struct knav_queue *qh; 41441f93af9SSandeep Nair 41541f93af9SSandeep Nair if (!knav_queue_is_busy(inst)) 41641f93af9SSandeep Nair return; 41741f93af9SSandeep Nair 41841f93af9SSandeep Nair seq_printf(s, "\tqueue id %d (%s)\n", 41941f93af9SSandeep Nair kdev->base_id + inst->id, inst->name); 42041f93af9SSandeep Nair for_each_handle_rcu(qh, inst) { 42141f93af9SSandeep Nair seq_printf(s, "\t\thandle %p: ", qh); 42241f93af9SSandeep Nair seq_printf(s, "pushes %8d, ", 42341f93af9SSandeep Nair atomic_read(&qh->stats.pushes)); 42441f93af9SSandeep Nair seq_printf(s, "pops %8d, ", 42541f93af9SSandeep Nair atomic_read(&qh->stats.pops)); 42641f93af9SSandeep Nair seq_printf(s, "count %8d, ", 42741f93af9SSandeep Nair knav_queue_get_count(qh)); 42841f93af9SSandeep Nair seq_printf(s, "notifies %8d, ", 42941f93af9SSandeep Nair atomic_read(&qh->stats.notifies)); 43041f93af9SSandeep Nair seq_printf(s, "push errors %8d, ", 43141f93af9SSandeep Nair atomic_read(&qh->stats.push_errors)); 43241f93af9SSandeep Nair seq_printf(s, "pop errors %8d\n", 43341f93af9SSandeep Nair atomic_read(&qh->stats.pop_errors)); 43441f93af9SSandeep Nair } 43541f93af9SSandeep Nair } 43641f93af9SSandeep Nair 43741f93af9SSandeep Nair static int knav_queue_debug_show(struct seq_file *s, void *v) 43841f93af9SSandeep Nair { 43941f93af9SSandeep Nair struct knav_queue_inst *inst; 44041f93af9SSandeep Nair int idx; 44141f93af9SSandeep Nair 44241f93af9SSandeep Nair mutex_lock(&knav_dev_lock); 44341f93af9SSandeep Nair seq_printf(s, "%s: %u-%u\n", 44441f93af9SSandeep Nair dev_name(kdev->dev), kdev->base_id, 44541f93af9SSandeep Nair kdev->base_id + kdev->num_queues - 1); 44641f93af9SSandeep Nair for_each_instance(idx, inst, kdev) 44741f93af9SSandeep Nair knav_queue_debug_show_instance(s, inst); 44841f93af9SSandeep Nair mutex_unlock(&knav_dev_lock); 44941f93af9SSandeep Nair 45041f93af9SSandeep Nair return 0; 45141f93af9SSandeep Nair } 45241f93af9SSandeep Nair 45341f93af9SSandeep Nair static int knav_queue_debug_open(struct inode *inode, struct file *file) 45441f93af9SSandeep Nair { 45541f93af9SSandeep Nair return single_open(file, knav_queue_debug_show, NULL); 45641f93af9SSandeep Nair } 45741f93af9SSandeep Nair 45841f93af9SSandeep Nair static const struct file_operations knav_queue_debug_ops = { 45941f93af9SSandeep Nair .open = knav_queue_debug_open, 46041f93af9SSandeep Nair .read = seq_read, 46141f93af9SSandeep Nair .llseek = seq_lseek, 46241f93af9SSandeep Nair .release = single_release, 46341f93af9SSandeep Nair }; 46441f93af9SSandeep Nair 46541f93af9SSandeep Nair static inline int knav_queue_pdsp_wait(u32 * __iomem addr, unsigned timeout, 46641f93af9SSandeep Nair u32 flags) 46741f93af9SSandeep Nair { 46841f93af9SSandeep Nair unsigned long end; 46941f93af9SSandeep Nair u32 val = 0; 47041f93af9SSandeep Nair 47141f93af9SSandeep Nair end = jiffies + msecs_to_jiffies(timeout); 47241f93af9SSandeep Nair while (time_after(end, jiffies)) { 47341f93af9SSandeep Nair val = readl_relaxed(addr); 47441f93af9SSandeep Nair if (flags) 47541f93af9SSandeep Nair val &= flags; 47641f93af9SSandeep Nair if (!val) 47741f93af9SSandeep Nair break; 47841f93af9SSandeep Nair cpu_relax(); 47941f93af9SSandeep Nair } 48041f93af9SSandeep Nair return val ? -ETIMEDOUT : 0; 48141f93af9SSandeep Nair } 48241f93af9SSandeep Nair 48341f93af9SSandeep Nair 48441f93af9SSandeep Nair static int knav_queue_flush(struct knav_queue *qh) 48541f93af9SSandeep Nair { 48641f93af9SSandeep Nair struct knav_queue_inst *inst = qh->inst; 48741f93af9SSandeep Nair unsigned id = inst->id - inst->qmgr->start_queue; 48841f93af9SSandeep Nair 48941f93af9SSandeep Nair atomic_set(&inst->desc_count, 0); 49041f93af9SSandeep Nair writel_relaxed(0, &inst->qmgr->reg_push[id].ptr_size_thresh); 49141f93af9SSandeep Nair return 0; 49241f93af9SSandeep Nair } 49341f93af9SSandeep Nair 49441f93af9SSandeep Nair /** 49541f93af9SSandeep Nair * knav_queue_open() - open a hardware queue 49641f93af9SSandeep Nair * @name - name to give the queue handle 49741f93af9SSandeep Nair * @id - desired queue number if any or specifes the type 49841f93af9SSandeep Nair * of queue 49941f93af9SSandeep Nair * @flags - the following flags are applicable to queues: 50041f93af9SSandeep Nair * KNAV_QUEUE_SHARED - allow the queue to be shared. Queues are 50141f93af9SSandeep Nair * exclusive by default. 50241f93af9SSandeep Nair * Subsequent attempts to open a shared queue should 50341f93af9SSandeep Nair * also have this flag. 50441f93af9SSandeep Nair * 50541f93af9SSandeep Nair * Returns a handle to the open hardware queue if successful. Use IS_ERR() 50641f93af9SSandeep Nair * to check the returned value for error codes. 50741f93af9SSandeep Nair */ 50841f93af9SSandeep Nair void *knav_queue_open(const char *name, unsigned id, 50941f93af9SSandeep Nair unsigned flags) 51041f93af9SSandeep Nair { 51141f93af9SSandeep Nair struct knav_queue *qh = ERR_PTR(-EINVAL); 51241f93af9SSandeep Nair 51341f93af9SSandeep Nair switch (id) { 51441f93af9SSandeep Nair case KNAV_QUEUE_QPEND: 51541f93af9SSandeep Nair case KNAV_QUEUE_ACC: 51641f93af9SSandeep Nair case KNAV_QUEUE_GP: 51741f93af9SSandeep Nair qh = knav_queue_open_by_type(name, id, flags); 51841f93af9SSandeep Nair break; 51941f93af9SSandeep Nair 52041f93af9SSandeep Nair default: 52141f93af9SSandeep Nair qh = knav_queue_open_by_id(name, id, flags); 52241f93af9SSandeep Nair break; 52341f93af9SSandeep Nair } 52441f93af9SSandeep Nair return qh; 52541f93af9SSandeep Nair } 52641f93af9SSandeep Nair EXPORT_SYMBOL_GPL(knav_queue_open); 52741f93af9SSandeep Nair 52841f93af9SSandeep Nair /** 52941f93af9SSandeep Nair * knav_queue_close() - close a hardware queue handle 53041f93af9SSandeep Nair * @qh - handle to close 53141f93af9SSandeep Nair */ 53241f93af9SSandeep Nair void knav_queue_close(void *qhandle) 53341f93af9SSandeep Nair { 53441f93af9SSandeep Nair struct knav_queue *qh = qhandle; 53541f93af9SSandeep Nair struct knav_queue_inst *inst = qh->inst; 53641f93af9SSandeep Nair 53741f93af9SSandeep Nair while (atomic_read(&qh->notifier_enabled) > 0) 53841f93af9SSandeep Nair knav_queue_disable_notifier(qh); 53941f93af9SSandeep Nair 54041f93af9SSandeep Nair mutex_lock(&knav_dev_lock); 54141f93af9SSandeep Nair list_del_rcu(&qh->list); 54241f93af9SSandeep Nair mutex_unlock(&knav_dev_lock); 54341f93af9SSandeep Nair synchronize_rcu(); 54441f93af9SSandeep Nair if (!knav_queue_is_busy(inst)) { 54541f93af9SSandeep Nair struct knav_range_info *range = inst->range; 54641f93af9SSandeep Nair 54741f93af9SSandeep Nair if (range->ops && range->ops->close_queue) 54841f93af9SSandeep Nair range->ops->close_queue(range, inst); 54941f93af9SSandeep Nair } 55041f93af9SSandeep Nair devm_kfree(inst->kdev->dev, qh); 55141f93af9SSandeep Nair } 55241f93af9SSandeep Nair EXPORT_SYMBOL_GPL(knav_queue_close); 55341f93af9SSandeep Nair 55441f93af9SSandeep Nair /** 55541f93af9SSandeep Nair * knav_queue_device_control() - Perform control operations on a queue 55641f93af9SSandeep Nair * @qh - queue handle 55741f93af9SSandeep Nair * @cmd - control commands 55841f93af9SSandeep Nair * @arg - command argument 55941f93af9SSandeep Nair * 56041f93af9SSandeep Nair * Returns 0 on success, errno otherwise. 56141f93af9SSandeep Nair */ 56241f93af9SSandeep Nair int knav_queue_device_control(void *qhandle, enum knav_queue_ctrl_cmd cmd, 56341f93af9SSandeep Nair unsigned long arg) 56441f93af9SSandeep Nair { 56541f93af9SSandeep Nair struct knav_queue *qh = qhandle; 56641f93af9SSandeep Nair struct knav_queue_notify_config *cfg; 56741f93af9SSandeep Nair int ret; 56841f93af9SSandeep Nair 56941f93af9SSandeep Nair switch ((int)cmd) { 57041f93af9SSandeep Nair case KNAV_QUEUE_GET_ID: 57141f93af9SSandeep Nair ret = qh->inst->kdev->base_id + qh->inst->id; 57241f93af9SSandeep Nair break; 57341f93af9SSandeep Nair 57441f93af9SSandeep Nair case KNAV_QUEUE_FLUSH: 57541f93af9SSandeep Nair ret = knav_queue_flush(qh); 57641f93af9SSandeep Nair break; 57741f93af9SSandeep Nair 57841f93af9SSandeep Nair case KNAV_QUEUE_SET_NOTIFIER: 57941f93af9SSandeep Nair cfg = (void *)arg; 58041f93af9SSandeep Nair ret = knav_queue_set_notifier(qh, cfg); 58141f93af9SSandeep Nair break; 58241f93af9SSandeep Nair 58341f93af9SSandeep Nair case KNAV_QUEUE_ENABLE_NOTIFY: 58441f93af9SSandeep Nair ret = knav_queue_enable_notifier(qh); 58541f93af9SSandeep Nair break; 58641f93af9SSandeep Nair 58741f93af9SSandeep Nair case KNAV_QUEUE_DISABLE_NOTIFY: 58841f93af9SSandeep Nair ret = knav_queue_disable_notifier(qh); 58941f93af9SSandeep Nair break; 59041f93af9SSandeep Nair 59141f93af9SSandeep Nair case KNAV_QUEUE_GET_COUNT: 59241f93af9SSandeep Nair ret = knav_queue_get_count(qh); 59341f93af9SSandeep Nair break; 59441f93af9SSandeep Nair 59541f93af9SSandeep Nair default: 59641f93af9SSandeep Nair ret = -ENOTSUPP; 59741f93af9SSandeep Nair break; 59841f93af9SSandeep Nair } 59941f93af9SSandeep Nair return ret; 60041f93af9SSandeep Nair } 60141f93af9SSandeep Nair EXPORT_SYMBOL_GPL(knav_queue_device_control); 60241f93af9SSandeep Nair 60341f93af9SSandeep Nair 60441f93af9SSandeep Nair 60541f93af9SSandeep Nair /** 60641f93af9SSandeep Nair * knav_queue_push() - push data (or descriptor) to the tail of a queue 60741f93af9SSandeep Nair * @qh - hardware queue handle 60841f93af9SSandeep Nair * @data - data to push 60941f93af9SSandeep Nair * @size - size of data to push 61041f93af9SSandeep Nair * @flags - can be used to pass additional information 61141f93af9SSandeep Nair * 61241f93af9SSandeep Nair * Returns 0 on success, errno otherwise. 61341f93af9SSandeep Nair */ 61441f93af9SSandeep Nair int knav_queue_push(void *qhandle, dma_addr_t dma, 61541f93af9SSandeep Nair unsigned size, unsigned flags) 61641f93af9SSandeep Nair { 61741f93af9SSandeep Nair struct knav_queue *qh = qhandle; 61841f93af9SSandeep Nair u32 val; 61941f93af9SSandeep Nair 62041f93af9SSandeep Nair val = (u32)dma | ((size / 16) - 1); 62141f93af9SSandeep Nair writel_relaxed(val, &qh->reg_push[0].ptr_size_thresh); 62241f93af9SSandeep Nair 62341f93af9SSandeep Nair atomic_inc(&qh->stats.pushes); 62441f93af9SSandeep Nair return 0; 62541f93af9SSandeep Nair } 62697f39af4SMurali Karicheri EXPORT_SYMBOL_GPL(knav_queue_push); 62741f93af9SSandeep Nair 62841f93af9SSandeep Nair /** 62941f93af9SSandeep Nair * knav_queue_pop() - pop data (or descriptor) from the head of a queue 63041f93af9SSandeep Nair * @qh - hardware queue handle 63141f93af9SSandeep Nair * @size - (optional) size of the data pop'ed. 63241f93af9SSandeep Nair * 63341f93af9SSandeep Nair * Returns a DMA address on success, 0 on failure. 63441f93af9SSandeep Nair */ 63541f93af9SSandeep Nair dma_addr_t knav_queue_pop(void *qhandle, unsigned *size) 63641f93af9SSandeep Nair { 63741f93af9SSandeep Nair struct knav_queue *qh = qhandle; 63841f93af9SSandeep Nair struct knav_queue_inst *inst = qh->inst; 63941f93af9SSandeep Nair dma_addr_t dma; 64041f93af9SSandeep Nair u32 val, idx; 64141f93af9SSandeep Nair 64241f93af9SSandeep Nair /* are we accumulated? */ 64341f93af9SSandeep Nair if (inst->descs) { 64441f93af9SSandeep Nair if (unlikely(atomic_dec_return(&inst->desc_count) < 0)) { 64541f93af9SSandeep Nair atomic_inc(&inst->desc_count); 64641f93af9SSandeep Nair return 0; 64741f93af9SSandeep Nair } 64841f93af9SSandeep Nair idx = atomic_inc_return(&inst->desc_head); 64941f93af9SSandeep Nair idx &= ACC_DESCS_MASK; 65041f93af9SSandeep Nair val = inst->descs[idx]; 65141f93af9SSandeep Nair } else { 65241f93af9SSandeep Nair val = readl_relaxed(&qh->reg_pop[0].ptr_size_thresh); 65341f93af9SSandeep Nair if (unlikely(!val)) 65441f93af9SSandeep Nair return 0; 65541f93af9SSandeep Nair } 65641f93af9SSandeep Nair 65741f93af9SSandeep Nair dma = val & DESC_PTR_MASK; 65841f93af9SSandeep Nair if (size) 65941f93af9SSandeep Nair *size = ((val & DESC_SIZE_MASK) + 1) * 16; 66041f93af9SSandeep Nair 66141f93af9SSandeep Nair atomic_inc(&qh->stats.pops); 66241f93af9SSandeep Nair return dma; 66341f93af9SSandeep Nair } 66497f39af4SMurali Karicheri EXPORT_SYMBOL_GPL(knav_queue_pop); 66541f93af9SSandeep Nair 66641f93af9SSandeep Nair /* carve out descriptors and push into queue */ 66741f93af9SSandeep Nair static void kdesc_fill_pool(struct knav_pool *pool) 66841f93af9SSandeep Nair { 66941f93af9SSandeep Nair struct knav_region *region; 67041f93af9SSandeep Nair int i; 67141f93af9SSandeep Nair 67241f93af9SSandeep Nair region = pool->region; 67341f93af9SSandeep Nair pool->desc_size = region->desc_size; 67441f93af9SSandeep Nair for (i = 0; i < pool->num_desc; i++) { 67541f93af9SSandeep Nair int index = pool->region_offset + i; 67641f93af9SSandeep Nair dma_addr_t dma_addr; 67741f93af9SSandeep Nair unsigned dma_size; 67841f93af9SSandeep Nair dma_addr = region->dma_start + (region->desc_size * index); 67941f93af9SSandeep Nair dma_size = ALIGN(pool->desc_size, SMP_CACHE_BYTES); 68041f93af9SSandeep Nair dma_sync_single_for_device(pool->dev, dma_addr, dma_size, 68141f93af9SSandeep Nair DMA_TO_DEVICE); 68241f93af9SSandeep Nair knav_queue_push(pool->queue, dma_addr, dma_size, 0); 68341f93af9SSandeep Nair } 68441f93af9SSandeep Nair } 68541f93af9SSandeep Nair 68641f93af9SSandeep Nair /* pop out descriptors and close the queue */ 68741f93af9SSandeep Nair static void kdesc_empty_pool(struct knav_pool *pool) 68841f93af9SSandeep Nair { 68941f93af9SSandeep Nair dma_addr_t dma; 69041f93af9SSandeep Nair unsigned size; 69141f93af9SSandeep Nair void *desc; 69241f93af9SSandeep Nair int i; 69341f93af9SSandeep Nair 69441f93af9SSandeep Nair if (!pool->queue) 69541f93af9SSandeep Nair return; 69641f93af9SSandeep Nair 69741f93af9SSandeep Nair for (i = 0;; i++) { 69841f93af9SSandeep Nair dma = knav_queue_pop(pool->queue, &size); 69941f93af9SSandeep Nair if (!dma) 70041f93af9SSandeep Nair break; 70141f93af9SSandeep Nair desc = knav_pool_desc_dma_to_virt(pool, dma); 70241f93af9SSandeep Nair if (!desc) { 70341f93af9SSandeep Nair dev_dbg(pool->kdev->dev, 70441f93af9SSandeep Nair "couldn't unmap desc, continuing\n"); 70541f93af9SSandeep Nair continue; 70641f93af9SSandeep Nair } 70741f93af9SSandeep Nair } 70841f93af9SSandeep Nair WARN_ON(i != pool->num_desc); 70941f93af9SSandeep Nair knav_queue_close(pool->queue); 71041f93af9SSandeep Nair } 71141f93af9SSandeep Nair 71241f93af9SSandeep Nair 71341f93af9SSandeep Nair /* Get the DMA address of a descriptor */ 71441f93af9SSandeep Nair dma_addr_t knav_pool_desc_virt_to_dma(void *ph, void *virt) 71541f93af9SSandeep Nair { 71641f93af9SSandeep Nair struct knav_pool *pool = ph; 71741f93af9SSandeep Nair return pool->region->dma_start + (virt - pool->region->virt_start); 71841f93af9SSandeep Nair } 71997f39af4SMurali Karicheri EXPORT_SYMBOL_GPL(knav_pool_desc_virt_to_dma); 72041f93af9SSandeep Nair 72141f93af9SSandeep Nair void *knav_pool_desc_dma_to_virt(void *ph, dma_addr_t dma) 72241f93af9SSandeep Nair { 72341f93af9SSandeep Nair struct knav_pool *pool = ph; 72441f93af9SSandeep Nair return pool->region->virt_start + (dma - pool->region->dma_start); 72541f93af9SSandeep Nair } 72697f39af4SMurali Karicheri EXPORT_SYMBOL_GPL(knav_pool_desc_dma_to_virt); 72741f93af9SSandeep Nair 72841f93af9SSandeep Nair /** 72941f93af9SSandeep Nair * knav_pool_create() - Create a pool of descriptors 73041f93af9SSandeep Nair * @name - name to give the pool handle 73141f93af9SSandeep Nair * @num_desc - numbers of descriptors in the pool 73241f93af9SSandeep Nair * @region_id - QMSS region id from which the descriptors are to be 73341f93af9SSandeep Nair * allocated. 73441f93af9SSandeep Nair * 73541f93af9SSandeep Nair * Returns a pool handle on success. 73641f93af9SSandeep Nair * Use IS_ERR_OR_NULL() to identify error values on return. 73741f93af9SSandeep Nair */ 73841f93af9SSandeep Nair void *knav_pool_create(const char *name, 73941f93af9SSandeep Nair int num_desc, int region_id) 74041f93af9SSandeep Nair { 74141f93af9SSandeep Nair struct knav_region *reg_itr, *region = NULL; 74241f93af9SSandeep Nair struct knav_pool *pool, *pi; 74341f93af9SSandeep Nair struct list_head *node; 74441f93af9SSandeep Nair unsigned last_offset; 74541f93af9SSandeep Nair bool slot_found; 74641f93af9SSandeep Nair int ret; 74741f93af9SSandeep Nair 74841f93af9SSandeep Nair if (!kdev->dev) 74941f93af9SSandeep Nair return ERR_PTR(-ENODEV); 75041f93af9SSandeep Nair 75141f93af9SSandeep Nair pool = devm_kzalloc(kdev->dev, sizeof(*pool), GFP_KERNEL); 75241f93af9SSandeep Nair if (!pool) { 75341f93af9SSandeep Nair dev_err(kdev->dev, "out of memory allocating pool\n"); 75441f93af9SSandeep Nair return ERR_PTR(-ENOMEM); 75541f93af9SSandeep Nair } 75641f93af9SSandeep Nair 75741f93af9SSandeep Nair for_each_region(kdev, reg_itr) { 75841f93af9SSandeep Nair if (reg_itr->id != region_id) 75941f93af9SSandeep Nair continue; 76041f93af9SSandeep Nair region = reg_itr; 76141f93af9SSandeep Nair break; 76241f93af9SSandeep Nair } 76341f93af9SSandeep Nair 76441f93af9SSandeep Nair if (!region) { 76541f93af9SSandeep Nair dev_err(kdev->dev, "region-id(%d) not found\n", region_id); 76641f93af9SSandeep Nair ret = -EINVAL; 76741f93af9SSandeep Nair goto err; 76841f93af9SSandeep Nair } 76941f93af9SSandeep Nair 77041f93af9SSandeep Nair pool->queue = knav_queue_open(name, KNAV_QUEUE_GP, 0); 77141f93af9SSandeep Nair if (IS_ERR_OR_NULL(pool->queue)) { 77241f93af9SSandeep Nair dev_err(kdev->dev, 77341f93af9SSandeep Nair "failed to open queue for pool(%s), error %ld\n", 77441f93af9SSandeep Nair name, PTR_ERR(pool->queue)); 77541f93af9SSandeep Nair ret = PTR_ERR(pool->queue); 77641f93af9SSandeep Nair goto err; 77741f93af9SSandeep Nair } 77841f93af9SSandeep Nair 77941f93af9SSandeep Nair pool->name = kstrndup(name, KNAV_NAME_SIZE, GFP_KERNEL); 78041f93af9SSandeep Nair pool->kdev = kdev; 78141f93af9SSandeep Nair pool->dev = kdev->dev; 78241f93af9SSandeep Nair 78341f93af9SSandeep Nair mutex_lock(&knav_dev_lock); 78441f93af9SSandeep Nair 78541f93af9SSandeep Nair if (num_desc > (region->num_desc - region->used_desc)) { 78641f93af9SSandeep Nair dev_err(kdev->dev, "out of descs in region(%d) for pool(%s)\n", 78741f93af9SSandeep Nair region_id, name); 78841f93af9SSandeep Nair ret = -ENOMEM; 789ea6d4c07SAxel Lin goto err_unlock; 79041f93af9SSandeep Nair } 79141f93af9SSandeep Nair 79241f93af9SSandeep Nair /* Region maintains a sorted (by region offset) list of pools 79341f93af9SSandeep Nair * use the first free slot which is large enough to accomodate 79441f93af9SSandeep Nair * the request 79541f93af9SSandeep Nair */ 79641f93af9SSandeep Nair last_offset = 0; 79741f93af9SSandeep Nair slot_found = false; 79841f93af9SSandeep Nair node = ®ion->pools; 79941f93af9SSandeep Nair list_for_each_entry(pi, ®ion->pools, region_inst) { 80041f93af9SSandeep Nair if ((pi->region_offset - last_offset) >= num_desc) { 80141f93af9SSandeep Nair slot_found = true; 80241f93af9SSandeep Nair break; 80341f93af9SSandeep Nair } 80441f93af9SSandeep Nair last_offset = pi->region_offset + pi->num_desc; 80541f93af9SSandeep Nair } 80641f93af9SSandeep Nair node = &pi->region_inst; 80741f93af9SSandeep Nair 80841f93af9SSandeep Nair if (slot_found) { 80941f93af9SSandeep Nair pool->region = region; 81041f93af9SSandeep Nair pool->num_desc = num_desc; 81141f93af9SSandeep Nair pool->region_offset = last_offset; 81241f93af9SSandeep Nair region->used_desc += num_desc; 81341f93af9SSandeep Nair list_add_tail(&pool->list, &kdev->pools); 81441f93af9SSandeep Nair list_add_tail(&pool->region_inst, node); 81541f93af9SSandeep Nair } else { 81641f93af9SSandeep Nair dev_err(kdev->dev, "pool(%s) create failed: fragmented desc pool in region(%d)\n", 81741f93af9SSandeep Nair name, region_id); 81841f93af9SSandeep Nair ret = -ENOMEM; 819ea6d4c07SAxel Lin goto err_unlock; 82041f93af9SSandeep Nair } 82141f93af9SSandeep Nair 82241f93af9SSandeep Nair mutex_unlock(&knav_dev_lock); 82341f93af9SSandeep Nair kdesc_fill_pool(pool); 82441f93af9SSandeep Nair return pool; 82541f93af9SSandeep Nair 826ea6d4c07SAxel Lin err_unlock: 82741f93af9SSandeep Nair mutex_unlock(&knav_dev_lock); 828ea6d4c07SAxel Lin err: 82941f93af9SSandeep Nair kfree(pool->name); 83041f93af9SSandeep Nair devm_kfree(kdev->dev, pool); 83141f93af9SSandeep Nair return ERR_PTR(ret); 83241f93af9SSandeep Nair } 83341f93af9SSandeep Nair EXPORT_SYMBOL_GPL(knav_pool_create); 83441f93af9SSandeep Nair 83541f93af9SSandeep Nair /** 83641f93af9SSandeep Nair * knav_pool_destroy() - Free a pool of descriptors 83741f93af9SSandeep Nair * @pool - pool handle 83841f93af9SSandeep Nair */ 83941f93af9SSandeep Nair void knav_pool_destroy(void *ph) 84041f93af9SSandeep Nair { 84141f93af9SSandeep Nair struct knav_pool *pool = ph; 84241f93af9SSandeep Nair 84341f93af9SSandeep Nair if (!pool) 84441f93af9SSandeep Nair return; 84541f93af9SSandeep Nair 84641f93af9SSandeep Nair if (!pool->region) 84741f93af9SSandeep Nair return; 84841f93af9SSandeep Nair 84941f93af9SSandeep Nair kdesc_empty_pool(pool); 85041f93af9SSandeep Nair mutex_lock(&knav_dev_lock); 85141f93af9SSandeep Nair 85241f93af9SSandeep Nair pool->region->used_desc -= pool->num_desc; 85341f93af9SSandeep Nair list_del(&pool->region_inst); 85441f93af9SSandeep Nair list_del(&pool->list); 85541f93af9SSandeep Nair 85641f93af9SSandeep Nair mutex_unlock(&knav_dev_lock); 85741f93af9SSandeep Nair kfree(pool->name); 85841f93af9SSandeep Nair devm_kfree(kdev->dev, pool); 85941f93af9SSandeep Nair } 86041f93af9SSandeep Nair EXPORT_SYMBOL_GPL(knav_pool_destroy); 86141f93af9SSandeep Nair 86241f93af9SSandeep Nair 86341f93af9SSandeep Nair /** 86441f93af9SSandeep Nair * knav_pool_desc_get() - Get a descriptor from the pool 86541f93af9SSandeep Nair * @pool - pool handle 86641f93af9SSandeep Nair * 86741f93af9SSandeep Nair * Returns descriptor from the pool. 86841f93af9SSandeep Nair */ 86941f93af9SSandeep Nair void *knav_pool_desc_get(void *ph) 87041f93af9SSandeep Nair { 87141f93af9SSandeep Nair struct knav_pool *pool = ph; 87241f93af9SSandeep Nair dma_addr_t dma; 87341f93af9SSandeep Nair unsigned size; 87441f93af9SSandeep Nair void *data; 87541f93af9SSandeep Nair 87641f93af9SSandeep Nair dma = knav_queue_pop(pool->queue, &size); 87741f93af9SSandeep Nair if (unlikely(!dma)) 87841f93af9SSandeep Nair return ERR_PTR(-ENOMEM); 87941f93af9SSandeep Nair data = knav_pool_desc_dma_to_virt(pool, dma); 88041f93af9SSandeep Nair return data; 88141f93af9SSandeep Nair } 88297f39af4SMurali Karicheri EXPORT_SYMBOL_GPL(knav_pool_desc_get); 88341f93af9SSandeep Nair 88441f93af9SSandeep Nair /** 88541f93af9SSandeep Nair * knav_pool_desc_put() - return a descriptor to the pool 88641f93af9SSandeep Nair * @pool - pool handle 88741f93af9SSandeep Nair */ 88841f93af9SSandeep Nair void knav_pool_desc_put(void *ph, void *desc) 88941f93af9SSandeep Nair { 89041f93af9SSandeep Nair struct knav_pool *pool = ph; 89141f93af9SSandeep Nair dma_addr_t dma; 89241f93af9SSandeep Nair dma = knav_pool_desc_virt_to_dma(pool, desc); 89341f93af9SSandeep Nair knav_queue_push(pool->queue, dma, pool->region->desc_size, 0); 89441f93af9SSandeep Nair } 89597f39af4SMurali Karicheri EXPORT_SYMBOL_GPL(knav_pool_desc_put); 89641f93af9SSandeep Nair 89741f93af9SSandeep Nair /** 89841f93af9SSandeep Nair * knav_pool_desc_map() - Map descriptor for DMA transfer 89941f93af9SSandeep Nair * @pool - pool handle 90041f93af9SSandeep Nair * @desc - address of descriptor to map 90141f93af9SSandeep Nair * @size - size of descriptor to map 90241f93af9SSandeep Nair * @dma - DMA address return pointer 90341f93af9SSandeep Nair * @dma_sz - adjusted return pointer 90441f93af9SSandeep Nair * 90541f93af9SSandeep Nair * Returns 0 on success, errno otherwise. 90641f93af9SSandeep Nair */ 90741f93af9SSandeep Nair int knav_pool_desc_map(void *ph, void *desc, unsigned size, 90841f93af9SSandeep Nair dma_addr_t *dma, unsigned *dma_sz) 90941f93af9SSandeep Nair { 91041f93af9SSandeep Nair struct knav_pool *pool = ph; 91141f93af9SSandeep Nair *dma = knav_pool_desc_virt_to_dma(pool, desc); 91241f93af9SSandeep Nair size = min(size, pool->region->desc_size); 91341f93af9SSandeep Nair size = ALIGN(size, SMP_CACHE_BYTES); 91441f93af9SSandeep Nair *dma_sz = size; 91541f93af9SSandeep Nair dma_sync_single_for_device(pool->dev, *dma, size, DMA_TO_DEVICE); 91641f93af9SSandeep Nair 91741f93af9SSandeep Nair /* Ensure the descriptor reaches to the memory */ 91841f93af9SSandeep Nair __iowmb(); 91941f93af9SSandeep Nair 92041f93af9SSandeep Nair return 0; 92141f93af9SSandeep Nair } 92297f39af4SMurali Karicheri EXPORT_SYMBOL_GPL(knav_pool_desc_map); 92341f93af9SSandeep Nair 92441f93af9SSandeep Nair /** 92541f93af9SSandeep Nair * knav_pool_desc_unmap() - Unmap descriptor after DMA transfer 92641f93af9SSandeep Nair * @pool - pool handle 92741f93af9SSandeep Nair * @dma - DMA address of descriptor to unmap 92841f93af9SSandeep Nair * @dma_sz - size of descriptor to unmap 92941f93af9SSandeep Nair * 93041f93af9SSandeep Nair * Returns descriptor address on success, Use IS_ERR_OR_NULL() to identify 93141f93af9SSandeep Nair * error values on return. 93241f93af9SSandeep Nair */ 93341f93af9SSandeep Nair void *knav_pool_desc_unmap(void *ph, dma_addr_t dma, unsigned dma_sz) 93441f93af9SSandeep Nair { 93541f93af9SSandeep Nair struct knav_pool *pool = ph; 93641f93af9SSandeep Nair unsigned desc_sz; 93741f93af9SSandeep Nair void *desc; 93841f93af9SSandeep Nair 93941f93af9SSandeep Nair desc_sz = min(dma_sz, pool->region->desc_size); 94041f93af9SSandeep Nair desc = knav_pool_desc_dma_to_virt(pool, dma); 94141f93af9SSandeep Nair dma_sync_single_for_cpu(pool->dev, dma, desc_sz, DMA_FROM_DEVICE); 94241f93af9SSandeep Nair prefetch(desc); 94341f93af9SSandeep Nair return desc; 94441f93af9SSandeep Nair } 94597f39af4SMurali Karicheri EXPORT_SYMBOL_GPL(knav_pool_desc_unmap); 94641f93af9SSandeep Nair 94741f93af9SSandeep Nair /** 94841f93af9SSandeep Nair * knav_pool_count() - Get the number of descriptors in pool. 94941f93af9SSandeep Nair * @pool - pool handle 95041f93af9SSandeep Nair * Returns number of elements in the pool. 95141f93af9SSandeep Nair */ 95241f93af9SSandeep Nair int knav_pool_count(void *ph) 95341f93af9SSandeep Nair { 95441f93af9SSandeep Nair struct knav_pool *pool = ph; 95541f93af9SSandeep Nair return knav_queue_get_count(pool->queue); 95641f93af9SSandeep Nair } 95797f39af4SMurali Karicheri EXPORT_SYMBOL_GPL(knav_pool_count); 95841f93af9SSandeep Nair 95941f93af9SSandeep Nair static void knav_queue_setup_region(struct knav_device *kdev, 96041f93af9SSandeep Nair struct knav_region *region) 96141f93af9SSandeep Nair { 96241f93af9SSandeep Nair unsigned hw_num_desc, hw_desc_size, size; 96341f93af9SSandeep Nair struct knav_reg_region __iomem *regs; 96441f93af9SSandeep Nair struct knav_qmgr_info *qmgr; 96541f93af9SSandeep Nair struct knav_pool *pool; 96641f93af9SSandeep Nair int id = region->id; 96741f93af9SSandeep Nair struct page *page; 96841f93af9SSandeep Nair 96941f93af9SSandeep Nair /* unused region? */ 97041f93af9SSandeep Nair if (!region->num_desc) { 97141f93af9SSandeep Nair dev_warn(kdev->dev, "unused region %s\n", region->name); 97241f93af9SSandeep Nair return; 97341f93af9SSandeep Nair } 97441f93af9SSandeep Nair 97541f93af9SSandeep Nair /* get hardware descriptor value */ 97641f93af9SSandeep Nair hw_num_desc = ilog2(region->num_desc - 1) + 1; 97741f93af9SSandeep Nair 97841f93af9SSandeep Nair /* did we force fit ourselves into nothingness? */ 97941f93af9SSandeep Nair if (region->num_desc < 32) { 98041f93af9SSandeep Nair region->num_desc = 0; 98141f93af9SSandeep Nair dev_warn(kdev->dev, "too few descriptors in region %s\n", 98241f93af9SSandeep Nair region->name); 98341f93af9SSandeep Nair return; 98441f93af9SSandeep Nair } 98541f93af9SSandeep Nair 98641f93af9SSandeep Nair size = region->num_desc * region->desc_size; 98741f93af9SSandeep Nair region->virt_start = alloc_pages_exact(size, GFP_KERNEL | GFP_DMA | 98841f93af9SSandeep Nair GFP_DMA32); 98941f93af9SSandeep Nair if (!region->virt_start) { 99041f93af9SSandeep Nair region->num_desc = 0; 99141f93af9SSandeep Nair dev_err(kdev->dev, "memory alloc failed for region %s\n", 99241f93af9SSandeep Nair region->name); 99341f93af9SSandeep Nair return; 99441f93af9SSandeep Nair } 99541f93af9SSandeep Nair region->virt_end = region->virt_start + size; 99641f93af9SSandeep Nair page = virt_to_page(region->virt_start); 99741f93af9SSandeep Nair 99841f93af9SSandeep Nair region->dma_start = dma_map_page(kdev->dev, page, 0, size, 99941f93af9SSandeep Nair DMA_BIDIRECTIONAL); 100041f93af9SSandeep Nair if (dma_mapping_error(kdev->dev, region->dma_start)) { 100141f93af9SSandeep Nair dev_err(kdev->dev, "dma map failed for region %s\n", 100241f93af9SSandeep Nair region->name); 100341f93af9SSandeep Nair goto fail; 100441f93af9SSandeep Nair } 100541f93af9SSandeep Nair region->dma_end = region->dma_start + size; 100641f93af9SSandeep Nair 100741f93af9SSandeep Nair pool = devm_kzalloc(kdev->dev, sizeof(*pool), GFP_KERNEL); 100841f93af9SSandeep Nair if (!pool) { 100941f93af9SSandeep Nair dev_err(kdev->dev, "out of memory allocating dummy pool\n"); 101041f93af9SSandeep Nair goto fail; 101141f93af9SSandeep Nair } 101241f93af9SSandeep Nair pool->num_desc = 0; 101341f93af9SSandeep Nair pool->region_offset = region->num_desc; 101441f93af9SSandeep Nair list_add(&pool->region_inst, ®ion->pools); 101541f93af9SSandeep Nair 101641f93af9SSandeep Nair dev_dbg(kdev->dev, 1017cc0336ecSArnd Bergmann "region %s (%d): size:%d, link:%d@%d, dma:%pad-%pad, virt:%p-%p\n", 101841f93af9SSandeep Nair region->name, id, region->desc_size, region->num_desc, 1019cc0336ecSArnd Bergmann region->link_index, ®ion->dma_start, ®ion->dma_end, 102041f93af9SSandeep Nair region->virt_start, region->virt_end); 102141f93af9SSandeep Nair 102241f93af9SSandeep Nair hw_desc_size = (region->desc_size / 16) - 1; 102341f93af9SSandeep Nair hw_num_desc -= 5; 102441f93af9SSandeep Nair 102541f93af9SSandeep Nair for_each_qmgr(kdev, qmgr) { 102641f93af9SSandeep Nair regs = qmgr->reg_region + id; 1027cc0336ecSArnd Bergmann writel_relaxed((u32)region->dma_start, ®s->base); 102841f93af9SSandeep Nair writel_relaxed(region->link_index, ®s->start_index); 102941f93af9SSandeep Nair writel_relaxed(hw_desc_size << 16 | hw_num_desc, 103041f93af9SSandeep Nair ®s->size_count); 103141f93af9SSandeep Nair } 103241f93af9SSandeep Nair return; 103341f93af9SSandeep Nair 103441f93af9SSandeep Nair fail: 103541f93af9SSandeep Nair if (region->dma_start) 103641f93af9SSandeep Nair dma_unmap_page(kdev->dev, region->dma_start, size, 103741f93af9SSandeep Nair DMA_BIDIRECTIONAL); 103841f93af9SSandeep Nair if (region->virt_start) 103941f93af9SSandeep Nair free_pages_exact(region->virt_start, size); 104041f93af9SSandeep Nair region->num_desc = 0; 104141f93af9SSandeep Nair return; 104241f93af9SSandeep Nair } 104341f93af9SSandeep Nair 104441f93af9SSandeep Nair static const char *knav_queue_find_name(struct device_node *node) 104541f93af9SSandeep Nair { 104641f93af9SSandeep Nair const char *name; 104741f93af9SSandeep Nair 104841f93af9SSandeep Nair if (of_property_read_string(node, "label", &name) < 0) 104941f93af9SSandeep Nair name = node->name; 105041f93af9SSandeep Nair if (!name) 105141f93af9SSandeep Nair name = "unknown"; 105241f93af9SSandeep Nair return name; 105341f93af9SSandeep Nair } 105441f93af9SSandeep Nair 105541f93af9SSandeep Nair static int knav_queue_setup_regions(struct knav_device *kdev, 105641f93af9SSandeep Nair struct device_node *regions) 105741f93af9SSandeep Nair { 105841f93af9SSandeep Nair struct device *dev = kdev->dev; 105941f93af9SSandeep Nair struct knav_region *region; 106041f93af9SSandeep Nair struct device_node *child; 106141f93af9SSandeep Nair u32 temp[2]; 106241f93af9SSandeep Nair int ret; 106341f93af9SSandeep Nair 106441f93af9SSandeep Nair for_each_child_of_node(regions, child) { 106541f93af9SSandeep Nair region = devm_kzalloc(dev, sizeof(*region), GFP_KERNEL); 106641f93af9SSandeep Nair if (!region) { 106741f93af9SSandeep Nair dev_err(dev, "out of memory allocating region\n"); 106841f93af9SSandeep Nair return -ENOMEM; 106941f93af9SSandeep Nair } 107041f93af9SSandeep Nair 107141f93af9SSandeep Nair region->name = knav_queue_find_name(child); 107241f93af9SSandeep Nair of_property_read_u32(child, "id", ®ion->id); 107341f93af9SSandeep Nair ret = of_property_read_u32_array(child, "region-spec", temp, 2); 107441f93af9SSandeep Nair if (!ret) { 107541f93af9SSandeep Nair region->num_desc = temp[0]; 107641f93af9SSandeep Nair region->desc_size = temp[1]; 107741f93af9SSandeep Nair } else { 107841f93af9SSandeep Nair dev_err(dev, "invalid region info %s\n", region->name); 107941f93af9SSandeep Nair devm_kfree(dev, region); 108041f93af9SSandeep Nair continue; 108141f93af9SSandeep Nair } 108241f93af9SSandeep Nair 108341f93af9SSandeep Nair if (!of_get_property(child, "link-index", NULL)) { 108441f93af9SSandeep Nair dev_err(dev, "No link info for %s\n", region->name); 108541f93af9SSandeep Nair devm_kfree(dev, region); 108641f93af9SSandeep Nair continue; 108741f93af9SSandeep Nair } 108841f93af9SSandeep Nair ret = of_property_read_u32(child, "link-index", 108941f93af9SSandeep Nair ®ion->link_index); 109041f93af9SSandeep Nair if (ret) { 109141f93af9SSandeep Nair dev_err(dev, "link index not found for %s\n", 109241f93af9SSandeep Nair region->name); 109341f93af9SSandeep Nair devm_kfree(dev, region); 109441f93af9SSandeep Nair continue; 109541f93af9SSandeep Nair } 109641f93af9SSandeep Nair 109741f93af9SSandeep Nair INIT_LIST_HEAD(®ion->pools); 109841f93af9SSandeep Nair list_add_tail(®ion->list, &kdev->regions); 109941f93af9SSandeep Nair } 110041f93af9SSandeep Nair if (list_empty(&kdev->regions)) { 110141f93af9SSandeep Nair dev_err(dev, "no valid region information found\n"); 110241f93af9SSandeep Nair return -ENODEV; 110341f93af9SSandeep Nair } 110441f93af9SSandeep Nair 110541f93af9SSandeep Nair /* Next, we run through the regions and set things up */ 110641f93af9SSandeep Nair for_each_region(kdev, region) 110741f93af9SSandeep Nair knav_queue_setup_region(kdev, region); 110841f93af9SSandeep Nair 110941f93af9SSandeep Nair return 0; 111041f93af9SSandeep Nair } 111141f93af9SSandeep Nair 111241f93af9SSandeep Nair static int knav_get_link_ram(struct knav_device *kdev, 111341f93af9SSandeep Nair const char *name, 111441f93af9SSandeep Nair struct knav_link_ram_block *block) 111541f93af9SSandeep Nair { 111641f93af9SSandeep Nair struct platform_device *pdev = to_platform_device(kdev->dev); 111741f93af9SSandeep Nair struct device_node *node = pdev->dev.of_node; 111841f93af9SSandeep Nair u32 temp[2]; 111941f93af9SSandeep Nair 112041f93af9SSandeep Nair /* 112141f93af9SSandeep Nair * Note: link ram resources are specified in "entry" sized units. In 112241f93af9SSandeep Nair * reality, although entries are ~40bits in hardware, we treat them as 112341f93af9SSandeep Nair * 64-bit entities here. 112441f93af9SSandeep Nair * 112541f93af9SSandeep Nair * For example, to specify the internal link ram for Keystone-I class 112641f93af9SSandeep Nair * devices, we would set the linkram0 resource to 0x80000-0x83fff. 112741f93af9SSandeep Nair * 112841f93af9SSandeep Nair * This gets a bit weird when other link rams are used. For example, 112941f93af9SSandeep Nair * if the range specified is 0x0c000000-0x0c003fff (i.e., 16K entries 113041f93af9SSandeep Nair * in MSMC SRAM), the actual memory used is 0x0c000000-0x0c020000, 113141f93af9SSandeep Nair * which accounts for 64-bits per entry, for 16K entries. 113241f93af9SSandeep Nair */ 113341f93af9SSandeep Nair if (!of_property_read_u32_array(node, name , temp, 2)) { 113441f93af9SSandeep Nair if (temp[0]) { 113541f93af9SSandeep Nair /* 113641f93af9SSandeep Nair * queue_base specified => using internal or onchip 113741f93af9SSandeep Nair * link ram WARNING - we do not "reserve" this block 113841f93af9SSandeep Nair */ 1139cc0336ecSArnd Bergmann block->dma = (dma_addr_t)temp[0]; 114041f93af9SSandeep Nair block->virt = NULL; 114141f93af9SSandeep Nair block->size = temp[1]; 114241f93af9SSandeep Nair } else { 114341f93af9SSandeep Nair block->size = temp[1]; 114441f93af9SSandeep Nair /* queue_base not specific => allocate requested size */ 114541f93af9SSandeep Nair block->virt = dmam_alloc_coherent(kdev->dev, 1146cc0336ecSArnd Bergmann 8 * block->size, &block->dma, 114741f93af9SSandeep Nair GFP_KERNEL); 114841f93af9SSandeep Nair if (!block->virt) { 114941f93af9SSandeep Nair dev_err(kdev->dev, "failed to alloc linkram\n"); 115041f93af9SSandeep Nair return -ENOMEM; 115141f93af9SSandeep Nair } 115241f93af9SSandeep Nair } 115341f93af9SSandeep Nair } else { 115441f93af9SSandeep Nair return -ENODEV; 115541f93af9SSandeep Nair } 115641f93af9SSandeep Nair return 0; 115741f93af9SSandeep Nair } 115841f93af9SSandeep Nair 115941f93af9SSandeep Nair static int knav_queue_setup_link_ram(struct knav_device *kdev) 116041f93af9SSandeep Nair { 116141f93af9SSandeep Nair struct knav_link_ram_block *block; 116241f93af9SSandeep Nair struct knav_qmgr_info *qmgr; 116341f93af9SSandeep Nair 116441f93af9SSandeep Nair for_each_qmgr(kdev, qmgr) { 116541f93af9SSandeep Nair block = &kdev->link_rams[0]; 1166cc0336ecSArnd Bergmann dev_dbg(kdev->dev, "linkram0: dma:%pad, virt:%p, size:%x\n", 1167cc0336ecSArnd Bergmann &block->dma, block->virt, block->size); 1168cc0336ecSArnd Bergmann writel_relaxed((u32)block->dma, &qmgr->reg_config->link_ram_base0); 116941f93af9SSandeep Nair writel_relaxed(block->size, &qmgr->reg_config->link_ram_size0); 117041f93af9SSandeep Nair 117141f93af9SSandeep Nair block++; 117241f93af9SSandeep Nair if (!block->size) 1173f7f2bccdSMichal Morawiec continue; 117441f93af9SSandeep Nair 1175cc0336ecSArnd Bergmann dev_dbg(kdev->dev, "linkram1: dma:%pad, virt:%p, size:%x\n", 1176cc0336ecSArnd Bergmann &block->dma, block->virt, block->size); 1177cc0336ecSArnd Bergmann writel_relaxed(block->dma, &qmgr->reg_config->link_ram_base1); 117841f93af9SSandeep Nair } 117941f93af9SSandeep Nair 118041f93af9SSandeep Nair return 0; 118141f93af9SSandeep Nair } 118241f93af9SSandeep Nair 118341f93af9SSandeep Nair static int knav_setup_queue_range(struct knav_device *kdev, 118441f93af9SSandeep Nair struct device_node *node) 118541f93af9SSandeep Nair { 118641f93af9SSandeep Nair struct device *dev = kdev->dev; 118741f93af9SSandeep Nair struct knav_range_info *range; 118841f93af9SSandeep Nair struct knav_qmgr_info *qmgr; 118941f93af9SSandeep Nair u32 temp[2], start, end, id, index; 119041f93af9SSandeep Nair int ret, i; 119141f93af9SSandeep Nair 119241f93af9SSandeep Nair range = devm_kzalloc(dev, sizeof(*range), GFP_KERNEL); 119341f93af9SSandeep Nair if (!range) { 119441f93af9SSandeep Nair dev_err(dev, "out of memory allocating range\n"); 119541f93af9SSandeep Nair return -ENOMEM; 119641f93af9SSandeep Nair } 119741f93af9SSandeep Nair 119841f93af9SSandeep Nair range->kdev = kdev; 119941f93af9SSandeep Nair range->name = knav_queue_find_name(node); 120041f93af9SSandeep Nair ret = of_property_read_u32_array(node, "qrange", temp, 2); 120141f93af9SSandeep Nair if (!ret) { 120241f93af9SSandeep Nair range->queue_base = temp[0] - kdev->base_id; 120341f93af9SSandeep Nair range->num_queues = temp[1]; 120441f93af9SSandeep Nair } else { 120541f93af9SSandeep Nair dev_err(dev, "invalid queue range %s\n", range->name); 120641f93af9SSandeep Nair devm_kfree(dev, range); 120741f93af9SSandeep Nair return -EINVAL; 120841f93af9SSandeep Nair } 120941f93af9SSandeep Nair 121041f93af9SSandeep Nair for (i = 0; i < RANGE_MAX_IRQS; i++) { 121141f93af9SSandeep Nair struct of_phandle_args oirq; 121241f93af9SSandeep Nair 121341f93af9SSandeep Nair if (of_irq_parse_one(node, i, &oirq)) 121441f93af9SSandeep Nair break; 121541f93af9SSandeep Nair 121641f93af9SSandeep Nair range->irqs[i].irq = irq_create_of_mapping(&oirq); 121741f93af9SSandeep Nair if (range->irqs[i].irq == IRQ_NONE) 121841f93af9SSandeep Nair break; 121941f93af9SSandeep Nair 122041f93af9SSandeep Nair range->num_irqs++; 122141f93af9SSandeep Nair 1222920f8bf7SGrygorii Strashko if (IS_ENABLED(CONFIG_SMP) && oirq.args_count == 3) 122341f93af9SSandeep Nair range->irqs[i].cpu_map = 122441f93af9SSandeep Nair (oirq.args[2] & 0x0000ff00) >> 8; 122541f93af9SSandeep Nair } 122641f93af9SSandeep Nair 122741f93af9SSandeep Nair range->num_irqs = min(range->num_irqs, range->num_queues); 122841f93af9SSandeep Nair if (range->num_irqs) 122941f93af9SSandeep Nair range->flags |= RANGE_HAS_IRQ; 123041f93af9SSandeep Nair 123141f93af9SSandeep Nair if (of_get_property(node, "qalloc-by-id", NULL)) 123241f93af9SSandeep Nair range->flags |= RANGE_RESERVED; 123341f93af9SSandeep Nair 123441f93af9SSandeep Nair if (of_get_property(node, "accumulator", NULL)) { 123541f93af9SSandeep Nair ret = knav_init_acc_range(kdev, node, range); 123641f93af9SSandeep Nair if (ret < 0) { 123741f93af9SSandeep Nair devm_kfree(dev, range); 123841f93af9SSandeep Nair return ret; 123941f93af9SSandeep Nair } 124041f93af9SSandeep Nair } else { 124141f93af9SSandeep Nair range->ops = &knav_gp_range_ops; 124241f93af9SSandeep Nair } 124341f93af9SSandeep Nair 124441f93af9SSandeep Nair /* set threshold to 1, and flush out the queues */ 124541f93af9SSandeep Nair for_each_qmgr(kdev, qmgr) { 124641f93af9SSandeep Nair start = max(qmgr->start_queue, range->queue_base); 124741f93af9SSandeep Nair end = min(qmgr->start_queue + qmgr->num_queues, 124841f93af9SSandeep Nair range->queue_base + range->num_queues); 124941f93af9SSandeep Nair for (id = start; id < end; id++) { 125041f93af9SSandeep Nair index = id - qmgr->start_queue; 125141f93af9SSandeep Nair writel_relaxed(THRESH_GTE | 1, 125241f93af9SSandeep Nair &qmgr->reg_peek[index].ptr_size_thresh); 125341f93af9SSandeep Nair writel_relaxed(0, 125441f93af9SSandeep Nair &qmgr->reg_push[index].ptr_size_thresh); 125541f93af9SSandeep Nair } 125641f93af9SSandeep Nair } 125741f93af9SSandeep Nair 125841f93af9SSandeep Nair list_add_tail(&range->list, &kdev->queue_ranges); 125941f93af9SSandeep Nair dev_dbg(dev, "added range %s: %d-%d, %d irqs%s%s%s\n", 126041f93af9SSandeep Nair range->name, range->queue_base, 126141f93af9SSandeep Nair range->queue_base + range->num_queues - 1, 126241f93af9SSandeep Nair range->num_irqs, 126341f93af9SSandeep Nair (range->flags & RANGE_HAS_IRQ) ? ", has irq" : "", 126441f93af9SSandeep Nair (range->flags & RANGE_RESERVED) ? ", reserved" : "", 126541f93af9SSandeep Nair (range->flags & RANGE_HAS_ACCUMULATOR) ? ", acc" : ""); 126641f93af9SSandeep Nair kdev->num_queues_in_use += range->num_queues; 126741f93af9SSandeep Nair return 0; 126841f93af9SSandeep Nair } 126941f93af9SSandeep Nair 127041f93af9SSandeep Nair static int knav_setup_queue_pools(struct knav_device *kdev, 127141f93af9SSandeep Nair struct device_node *queue_pools) 127241f93af9SSandeep Nair { 127341f93af9SSandeep Nair struct device_node *type, *range; 127441f93af9SSandeep Nair int ret; 127541f93af9SSandeep Nair 127641f93af9SSandeep Nair for_each_child_of_node(queue_pools, type) { 127741f93af9SSandeep Nair for_each_child_of_node(type, range) { 127841f93af9SSandeep Nair ret = knav_setup_queue_range(kdev, range); 127941f93af9SSandeep Nair /* return value ignored, we init the rest... */ 128041f93af9SSandeep Nair } 128141f93af9SSandeep Nair } 128241f93af9SSandeep Nair 128341f93af9SSandeep Nair /* ... and barf if they all failed! */ 128441f93af9SSandeep Nair if (list_empty(&kdev->queue_ranges)) { 128541f93af9SSandeep Nair dev_err(kdev->dev, "no valid queue range found\n"); 128641f93af9SSandeep Nair return -ENODEV; 128741f93af9SSandeep Nair } 128841f93af9SSandeep Nair return 0; 128941f93af9SSandeep Nair } 129041f93af9SSandeep Nair 129141f93af9SSandeep Nair static void knav_free_queue_range(struct knav_device *kdev, 129241f93af9SSandeep Nair struct knav_range_info *range) 129341f93af9SSandeep Nair { 129441f93af9SSandeep Nair if (range->ops && range->ops->free_range) 129541f93af9SSandeep Nair range->ops->free_range(range); 129641f93af9SSandeep Nair list_del(&range->list); 129741f93af9SSandeep Nair devm_kfree(kdev->dev, range); 129841f93af9SSandeep Nair } 129941f93af9SSandeep Nair 130041f93af9SSandeep Nair static void knav_free_queue_ranges(struct knav_device *kdev) 130141f93af9SSandeep Nair { 130241f93af9SSandeep Nair struct knav_range_info *range; 130341f93af9SSandeep Nair 130441f93af9SSandeep Nair for (;;) { 130541f93af9SSandeep Nair range = first_queue_range(kdev); 130641f93af9SSandeep Nair if (!range) 130741f93af9SSandeep Nair break; 130841f93af9SSandeep Nair knav_free_queue_range(kdev, range); 130941f93af9SSandeep Nair } 131041f93af9SSandeep Nair } 131141f93af9SSandeep Nair 131241f93af9SSandeep Nair static void knav_queue_free_regions(struct knav_device *kdev) 131341f93af9SSandeep Nair { 131441f93af9SSandeep Nair struct knav_region *region; 1315148bb043SAxel Lin struct knav_pool *pool, *tmp; 131641f93af9SSandeep Nair unsigned size; 131741f93af9SSandeep Nair 131841f93af9SSandeep Nair for (;;) { 131941f93af9SSandeep Nair region = first_region(kdev); 132041f93af9SSandeep Nair if (!region) 132141f93af9SSandeep Nair break; 1322148bb043SAxel Lin list_for_each_entry_safe(pool, tmp, ®ion->pools, region_inst) 132341f93af9SSandeep Nair knav_pool_destroy(pool); 132441f93af9SSandeep Nair 132541f93af9SSandeep Nair size = region->virt_end - region->virt_start; 132641f93af9SSandeep Nair if (size) 132741f93af9SSandeep Nair free_pages_exact(region->virt_start, size); 132841f93af9SSandeep Nair list_del(®ion->list); 132941f93af9SSandeep Nair devm_kfree(kdev->dev, region); 133041f93af9SSandeep Nair } 133141f93af9SSandeep Nair } 133241f93af9SSandeep Nair 133341f93af9SSandeep Nair static void __iomem *knav_queue_map_reg(struct knav_device *kdev, 133441f93af9SSandeep Nair struct device_node *node, int index) 133541f93af9SSandeep Nair { 133641f93af9SSandeep Nair struct resource res; 133741f93af9SSandeep Nair void __iomem *regs; 133841f93af9SSandeep Nair int ret; 133941f93af9SSandeep Nair 134041f93af9SSandeep Nair ret = of_address_to_resource(node, index, &res); 134141f93af9SSandeep Nair if (ret) { 134241f93af9SSandeep Nair dev_err(kdev->dev, "Can't translate of node(%s) address for index(%d)\n", 134341f93af9SSandeep Nair node->name, index); 134441f93af9SSandeep Nair return ERR_PTR(ret); 134541f93af9SSandeep Nair } 134641f93af9SSandeep Nair 134741f93af9SSandeep Nair regs = devm_ioremap_resource(kdev->dev, &res); 134841f93af9SSandeep Nair if (IS_ERR(regs)) 134941f93af9SSandeep Nair dev_err(kdev->dev, "Failed to map register base for index(%d) node(%s)\n", 135041f93af9SSandeep Nair index, node->name); 135141f93af9SSandeep Nair return regs; 135241f93af9SSandeep Nair } 135341f93af9SSandeep Nair 135441f93af9SSandeep Nair static int knav_queue_init_qmgrs(struct knav_device *kdev, 135541f93af9SSandeep Nair struct device_node *qmgrs) 135641f93af9SSandeep Nair { 135741f93af9SSandeep Nair struct device *dev = kdev->dev; 135841f93af9SSandeep Nair struct knav_qmgr_info *qmgr; 135941f93af9SSandeep Nair struct device_node *child; 136041f93af9SSandeep Nair u32 temp[2]; 136141f93af9SSandeep Nair int ret; 136241f93af9SSandeep Nair 136341f93af9SSandeep Nair for_each_child_of_node(qmgrs, child) { 136441f93af9SSandeep Nair qmgr = devm_kzalloc(dev, sizeof(*qmgr), GFP_KERNEL); 136541f93af9SSandeep Nair if (!qmgr) { 136641f93af9SSandeep Nair dev_err(dev, "out of memory allocating qmgr\n"); 136741f93af9SSandeep Nair return -ENOMEM; 136841f93af9SSandeep Nair } 136941f93af9SSandeep Nair 137041f93af9SSandeep Nair ret = of_property_read_u32_array(child, "managed-queues", 137141f93af9SSandeep Nair temp, 2); 137241f93af9SSandeep Nair if (!ret) { 137341f93af9SSandeep Nair qmgr->start_queue = temp[0]; 137441f93af9SSandeep Nair qmgr->num_queues = temp[1]; 137541f93af9SSandeep Nair } else { 137641f93af9SSandeep Nair dev_err(dev, "invalid qmgr queue range\n"); 137741f93af9SSandeep Nair devm_kfree(dev, qmgr); 137841f93af9SSandeep Nair continue; 137941f93af9SSandeep Nair } 138041f93af9SSandeep Nair 138141f93af9SSandeep Nair dev_info(dev, "qmgr start queue %d, number of queues %d\n", 138241f93af9SSandeep Nair qmgr->start_queue, qmgr->num_queues); 138341f93af9SSandeep Nair 138441f93af9SSandeep Nair qmgr->reg_peek = 138541f93af9SSandeep Nair knav_queue_map_reg(kdev, child, 138641f93af9SSandeep Nair KNAV_QUEUE_PEEK_REG_INDEX); 138741f93af9SSandeep Nair qmgr->reg_status = 138841f93af9SSandeep Nair knav_queue_map_reg(kdev, child, 138941f93af9SSandeep Nair KNAV_QUEUE_STATUS_REG_INDEX); 139041f93af9SSandeep Nair qmgr->reg_config = 139141f93af9SSandeep Nair knav_queue_map_reg(kdev, child, 139241f93af9SSandeep Nair KNAV_QUEUE_CONFIG_REG_INDEX); 139341f93af9SSandeep Nair qmgr->reg_region = 139441f93af9SSandeep Nair knav_queue_map_reg(kdev, child, 139541f93af9SSandeep Nair KNAV_QUEUE_REGION_REG_INDEX); 139641f93af9SSandeep Nair qmgr->reg_push = 139741f93af9SSandeep Nair knav_queue_map_reg(kdev, child, 139841f93af9SSandeep Nair KNAV_QUEUE_PUSH_REG_INDEX); 139941f93af9SSandeep Nair qmgr->reg_pop = 140041f93af9SSandeep Nair knav_queue_map_reg(kdev, child, 140141f93af9SSandeep Nair KNAV_QUEUE_POP_REG_INDEX); 140241f93af9SSandeep Nair 140341f93af9SSandeep Nair if (IS_ERR(qmgr->reg_peek) || IS_ERR(qmgr->reg_status) || 140441f93af9SSandeep Nair IS_ERR(qmgr->reg_config) || IS_ERR(qmgr->reg_region) || 140541f93af9SSandeep Nair IS_ERR(qmgr->reg_push) || IS_ERR(qmgr->reg_pop)) { 140641f93af9SSandeep Nair dev_err(dev, "failed to map qmgr regs\n"); 140741f93af9SSandeep Nair if (!IS_ERR(qmgr->reg_peek)) 140841f93af9SSandeep Nair devm_iounmap(dev, qmgr->reg_peek); 140941f93af9SSandeep Nair if (!IS_ERR(qmgr->reg_status)) 141041f93af9SSandeep Nair devm_iounmap(dev, qmgr->reg_status); 141141f93af9SSandeep Nair if (!IS_ERR(qmgr->reg_config)) 141241f93af9SSandeep Nair devm_iounmap(dev, qmgr->reg_config); 141341f93af9SSandeep Nair if (!IS_ERR(qmgr->reg_region)) 141441f93af9SSandeep Nair devm_iounmap(dev, qmgr->reg_region); 141541f93af9SSandeep Nair if (!IS_ERR(qmgr->reg_push)) 141641f93af9SSandeep Nair devm_iounmap(dev, qmgr->reg_push); 141741f93af9SSandeep Nair if (!IS_ERR(qmgr->reg_pop)) 141841f93af9SSandeep Nair devm_iounmap(dev, qmgr->reg_pop); 141941f93af9SSandeep Nair devm_kfree(dev, qmgr); 142041f93af9SSandeep Nair continue; 142141f93af9SSandeep Nair } 142241f93af9SSandeep Nair 142341f93af9SSandeep Nair list_add_tail(&qmgr->list, &kdev->qmgrs); 142441f93af9SSandeep 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", 142541f93af9SSandeep Nair qmgr->start_queue, qmgr->num_queues, 142641f93af9SSandeep Nair qmgr->reg_peek, qmgr->reg_status, 142741f93af9SSandeep Nair qmgr->reg_config, qmgr->reg_region, 142841f93af9SSandeep Nair qmgr->reg_push, qmgr->reg_pop); 142941f93af9SSandeep Nair } 143041f93af9SSandeep Nair return 0; 143141f93af9SSandeep Nair } 143241f93af9SSandeep Nair 143341f93af9SSandeep Nair static int knav_queue_init_pdsps(struct knav_device *kdev, 143441f93af9SSandeep Nair struct device_node *pdsps) 143541f93af9SSandeep Nair { 143641f93af9SSandeep Nair struct device *dev = kdev->dev; 143741f93af9SSandeep Nair struct knav_pdsp_info *pdsp; 143841f93af9SSandeep Nair struct device_node *child; 143941f93af9SSandeep Nair 144041f93af9SSandeep Nair for_each_child_of_node(pdsps, child) { 144141f93af9SSandeep Nair pdsp = devm_kzalloc(dev, sizeof(*pdsp), GFP_KERNEL); 144241f93af9SSandeep Nair if (!pdsp) { 144341f93af9SSandeep Nair dev_err(dev, "out of memory allocating pdsp\n"); 144441f93af9SSandeep Nair return -ENOMEM; 144541f93af9SSandeep Nair } 144641f93af9SSandeep Nair pdsp->name = knav_queue_find_name(child); 144741f93af9SSandeep Nair pdsp->iram = 144841f93af9SSandeep Nair knav_queue_map_reg(kdev, child, 144941f93af9SSandeep Nair KNAV_QUEUE_PDSP_IRAM_REG_INDEX); 145041f93af9SSandeep Nair pdsp->regs = 145141f93af9SSandeep Nair knav_queue_map_reg(kdev, child, 145241f93af9SSandeep Nair KNAV_QUEUE_PDSP_REGS_REG_INDEX); 145341f93af9SSandeep Nair pdsp->intd = 145441f93af9SSandeep Nair knav_queue_map_reg(kdev, child, 145541f93af9SSandeep Nair KNAV_QUEUE_PDSP_INTD_REG_INDEX); 145641f93af9SSandeep Nair pdsp->command = 145741f93af9SSandeep Nair knav_queue_map_reg(kdev, child, 145841f93af9SSandeep Nair KNAV_QUEUE_PDSP_CMD_REG_INDEX); 145941f93af9SSandeep Nair 146041f93af9SSandeep Nair if (IS_ERR(pdsp->command) || IS_ERR(pdsp->iram) || 146141f93af9SSandeep Nair IS_ERR(pdsp->regs) || IS_ERR(pdsp->intd)) { 146241f93af9SSandeep Nair dev_err(dev, "failed to map pdsp %s regs\n", 146341f93af9SSandeep Nair pdsp->name); 146441f93af9SSandeep Nair if (!IS_ERR(pdsp->command)) 146541f93af9SSandeep Nair devm_iounmap(dev, pdsp->command); 146641f93af9SSandeep Nair if (!IS_ERR(pdsp->iram)) 146741f93af9SSandeep Nair devm_iounmap(dev, pdsp->iram); 146841f93af9SSandeep Nair if (!IS_ERR(pdsp->regs)) 146941f93af9SSandeep Nair devm_iounmap(dev, pdsp->regs); 147041f93af9SSandeep Nair if (!IS_ERR(pdsp->intd)) 147141f93af9SSandeep Nair devm_iounmap(dev, pdsp->intd); 147241f93af9SSandeep Nair devm_kfree(dev, pdsp); 147341f93af9SSandeep Nair continue; 147441f93af9SSandeep Nair } 147541f93af9SSandeep Nair of_property_read_u32(child, "id", &pdsp->id); 147641f93af9SSandeep Nair list_add_tail(&pdsp->list, &kdev->pdsps); 147796ee19beSMurali Karicheri dev_dbg(dev, "added pdsp %s: command %p, iram %p, regs %p, intd %p\n", 147841f93af9SSandeep Nair pdsp->name, pdsp->command, pdsp->iram, pdsp->regs, 147996ee19beSMurali Karicheri pdsp->intd); 148041f93af9SSandeep Nair } 148141f93af9SSandeep Nair return 0; 148241f93af9SSandeep Nair } 148341f93af9SSandeep Nair 148441f93af9SSandeep Nair static int knav_queue_stop_pdsp(struct knav_device *kdev, 148541f93af9SSandeep Nair struct knav_pdsp_info *pdsp) 148641f93af9SSandeep Nair { 148741f93af9SSandeep Nair u32 val, timeout = 1000; 148841f93af9SSandeep Nair int ret; 148941f93af9SSandeep Nair 149041f93af9SSandeep Nair val = readl_relaxed(&pdsp->regs->control) & ~PDSP_CTRL_ENABLE; 149141f93af9SSandeep Nair writel_relaxed(val, &pdsp->regs->control); 149241f93af9SSandeep Nair ret = knav_queue_pdsp_wait(&pdsp->regs->control, timeout, 149341f93af9SSandeep Nair PDSP_CTRL_RUNNING); 149441f93af9SSandeep Nair if (ret < 0) { 149541f93af9SSandeep Nair dev_err(kdev->dev, "timed out on pdsp %s stop\n", pdsp->name); 149641f93af9SSandeep Nair return ret; 149741f93af9SSandeep Nair } 149804501690SMurali Karicheri pdsp->loaded = false; 149904501690SMurali Karicheri pdsp->started = false; 150041f93af9SSandeep Nair return 0; 150141f93af9SSandeep Nair } 150241f93af9SSandeep Nair 150341f93af9SSandeep Nair static int knav_queue_load_pdsp(struct knav_device *kdev, 150441f93af9SSandeep Nair struct knav_pdsp_info *pdsp) 150541f93af9SSandeep Nair { 150641f93af9SSandeep Nair int i, ret, fwlen; 150741f93af9SSandeep Nair const struct firmware *fw; 150896ee19beSMurali Karicheri bool found = false; 150941f93af9SSandeep Nair u32 *fwdata; 151041f93af9SSandeep Nair 151196ee19beSMurali Karicheri for (i = 0; i < ARRAY_SIZE(knav_acc_firmwares); i++) { 151296ee19beSMurali Karicheri if (knav_acc_firmwares[i]) { 15136af1a073SMurali Karicheri ret = request_firmware_direct(&fw, 151496ee19beSMurali Karicheri knav_acc_firmwares[i], 151596ee19beSMurali Karicheri kdev->dev); 151696ee19beSMurali Karicheri if (!ret) { 151796ee19beSMurali Karicheri found = true; 151896ee19beSMurali Karicheri break; 151941f93af9SSandeep Nair } 152096ee19beSMurali Karicheri } 152196ee19beSMurali Karicheri } 152296ee19beSMurali Karicheri 152396ee19beSMurali Karicheri if (!found) { 152496ee19beSMurali Karicheri dev_err(kdev->dev, "failed to get firmware for pdsp\n"); 152596ee19beSMurali Karicheri return -ENODEV; 152696ee19beSMurali Karicheri } 152796ee19beSMurali Karicheri 152896ee19beSMurali Karicheri dev_info(kdev->dev, "firmware file %s downloaded for PDSP\n", 152996ee19beSMurali Karicheri knav_acc_firmwares[i]); 153096ee19beSMurali Karicheri 153141f93af9SSandeep Nair writel_relaxed(pdsp->id + 1, pdsp->command + 0x18); 153241f93af9SSandeep Nair /* download the firmware */ 153341f93af9SSandeep Nair fwdata = (u32 *)fw->data; 153441f93af9SSandeep Nair fwlen = (fw->size + sizeof(u32) - 1) / sizeof(u32); 153541f93af9SSandeep Nair for (i = 0; i < fwlen; i++) 153641f93af9SSandeep Nair writel_relaxed(be32_to_cpu(fwdata[i]), pdsp->iram + i); 153741f93af9SSandeep Nair 153841f93af9SSandeep Nair release_firmware(fw); 153941f93af9SSandeep Nair return 0; 154041f93af9SSandeep Nair } 154141f93af9SSandeep Nair 154241f93af9SSandeep Nair static int knav_queue_start_pdsp(struct knav_device *kdev, 154341f93af9SSandeep Nair struct knav_pdsp_info *pdsp) 154441f93af9SSandeep Nair { 154541f93af9SSandeep Nair u32 val, timeout = 1000; 154641f93af9SSandeep Nair int ret; 154741f93af9SSandeep Nair 154841f93af9SSandeep Nair /* write a command for sync */ 154941f93af9SSandeep Nair writel_relaxed(0xffffffff, pdsp->command); 155041f93af9SSandeep Nair while (readl_relaxed(pdsp->command) != 0xffffffff) 155141f93af9SSandeep Nair cpu_relax(); 155241f93af9SSandeep Nair 155341f93af9SSandeep Nair /* soft reset the PDSP */ 155441f93af9SSandeep Nair val = readl_relaxed(&pdsp->regs->control); 155541f93af9SSandeep Nair val &= ~(PDSP_CTRL_PC_MASK | PDSP_CTRL_SOFT_RESET); 155641f93af9SSandeep Nair writel_relaxed(val, &pdsp->regs->control); 155741f93af9SSandeep Nair 155841f93af9SSandeep Nair /* enable pdsp */ 155941f93af9SSandeep Nair val = readl_relaxed(&pdsp->regs->control) | PDSP_CTRL_ENABLE; 156041f93af9SSandeep Nair writel_relaxed(val, &pdsp->regs->control); 156141f93af9SSandeep Nair 156241f93af9SSandeep Nair /* wait for command register to clear */ 156341f93af9SSandeep Nair ret = knav_queue_pdsp_wait(pdsp->command, timeout, 0); 156441f93af9SSandeep Nair if (ret < 0) { 156541f93af9SSandeep Nair dev_err(kdev->dev, 156641f93af9SSandeep Nair "timed out on pdsp %s command register wait\n", 156741f93af9SSandeep Nair pdsp->name); 156841f93af9SSandeep Nair return ret; 156941f93af9SSandeep Nair } 157041f93af9SSandeep Nair return 0; 157141f93af9SSandeep Nair } 157241f93af9SSandeep Nair 157341f93af9SSandeep Nair static void knav_queue_stop_pdsps(struct knav_device *kdev) 157441f93af9SSandeep Nair { 157541f93af9SSandeep Nair struct knav_pdsp_info *pdsp; 157641f93af9SSandeep Nair 157741f93af9SSandeep Nair /* disable all pdsps */ 157841f93af9SSandeep Nair for_each_pdsp(kdev, pdsp) 157941f93af9SSandeep Nair knav_queue_stop_pdsp(kdev, pdsp); 158041f93af9SSandeep Nair } 158141f93af9SSandeep Nair 158241f93af9SSandeep Nair static int knav_queue_start_pdsps(struct knav_device *kdev) 158341f93af9SSandeep Nair { 158441f93af9SSandeep Nair struct knav_pdsp_info *pdsp; 158541f93af9SSandeep Nair int ret; 158641f93af9SSandeep Nair 158741f93af9SSandeep Nair knav_queue_stop_pdsps(kdev); 158804501690SMurali Karicheri /* now load them all. We return success even if pdsp 158904501690SMurali Karicheri * is not loaded as acc channels are optional on having 159004501690SMurali Karicheri * firmware availability in the system. We set the loaded 159104501690SMurali Karicheri * and stated flag and when initialize the acc range, check 159204501690SMurali Karicheri * it and init the range only if pdsp is started. 159304501690SMurali Karicheri */ 159441f93af9SSandeep Nair for_each_pdsp(kdev, pdsp) { 159541f93af9SSandeep Nair ret = knav_queue_load_pdsp(kdev, pdsp); 159604501690SMurali Karicheri if (!ret) 159704501690SMurali Karicheri pdsp->loaded = true; 159841f93af9SSandeep Nair } 159941f93af9SSandeep Nair 160041f93af9SSandeep Nair for_each_pdsp(kdev, pdsp) { 160104501690SMurali Karicheri if (pdsp->loaded) { 160241f93af9SSandeep Nair ret = knav_queue_start_pdsp(kdev, pdsp); 160304501690SMurali Karicheri if (!ret) 160404501690SMurali Karicheri pdsp->started = true; 160504501690SMurali Karicheri } 160641f93af9SSandeep Nair } 160741f93af9SSandeep Nair return 0; 160841f93af9SSandeep Nair } 160941f93af9SSandeep Nair 161041f93af9SSandeep Nair static inline struct knav_qmgr_info *knav_find_qmgr(unsigned id) 161141f93af9SSandeep Nair { 161241f93af9SSandeep Nair struct knav_qmgr_info *qmgr; 161341f93af9SSandeep Nair 161441f93af9SSandeep Nair for_each_qmgr(kdev, qmgr) { 161541f93af9SSandeep Nair if ((id >= qmgr->start_queue) && 161641f93af9SSandeep Nair (id < qmgr->start_queue + qmgr->num_queues)) 161741f93af9SSandeep Nair return qmgr; 161841f93af9SSandeep Nair } 161941f93af9SSandeep Nair return NULL; 162041f93af9SSandeep Nair } 162141f93af9SSandeep Nair 162241f93af9SSandeep Nair static int knav_queue_init_queue(struct knav_device *kdev, 162341f93af9SSandeep Nair struct knav_range_info *range, 162441f93af9SSandeep Nair struct knav_queue_inst *inst, 162541f93af9SSandeep Nair unsigned id) 162641f93af9SSandeep Nair { 162741f93af9SSandeep Nair char irq_name[KNAV_NAME_SIZE]; 162841f93af9SSandeep Nair inst->qmgr = knav_find_qmgr(id); 162941f93af9SSandeep Nair if (!inst->qmgr) 163041f93af9SSandeep Nair return -1; 163141f93af9SSandeep Nair 163241f93af9SSandeep Nair INIT_LIST_HEAD(&inst->handles); 163341f93af9SSandeep Nair inst->kdev = kdev; 163441f93af9SSandeep Nair inst->range = range; 163541f93af9SSandeep Nair inst->irq_num = -1; 163641f93af9SSandeep Nair inst->id = id; 163741f93af9SSandeep Nair scnprintf(irq_name, sizeof(irq_name), "hwqueue-%d", id); 163841f93af9SSandeep Nair inst->irq_name = kstrndup(irq_name, sizeof(irq_name), GFP_KERNEL); 163941f93af9SSandeep Nair 164041f93af9SSandeep Nair if (range->ops && range->ops->init_queue) 164141f93af9SSandeep Nair return range->ops->init_queue(range, inst); 164241f93af9SSandeep Nair else 164341f93af9SSandeep Nair return 0; 164441f93af9SSandeep Nair } 164541f93af9SSandeep Nair 164641f93af9SSandeep Nair static int knav_queue_init_queues(struct knav_device *kdev) 164741f93af9SSandeep Nair { 164841f93af9SSandeep Nair struct knav_range_info *range; 164941f93af9SSandeep Nair int size, id, base_idx; 165041f93af9SSandeep Nair int idx = 0, ret = 0; 165141f93af9SSandeep Nair 165241f93af9SSandeep Nair /* how much do we need for instance data? */ 165341f93af9SSandeep Nair size = sizeof(struct knav_queue_inst); 165441f93af9SSandeep Nair 165541f93af9SSandeep Nair /* round this up to a power of 2, keep the index to instance 165641f93af9SSandeep Nair * arithmetic fast. 165741f93af9SSandeep Nair * */ 165841f93af9SSandeep Nair kdev->inst_shift = order_base_2(size); 165941f93af9SSandeep Nair size = (1 << kdev->inst_shift) * kdev->num_queues_in_use; 166041f93af9SSandeep Nair kdev->instances = devm_kzalloc(kdev->dev, size, GFP_KERNEL); 166141f93af9SSandeep Nair if (!kdev->instances) 166239179cb5SAxel Lin return -ENOMEM; 166341f93af9SSandeep Nair 166441f93af9SSandeep Nair for_each_queue_range(kdev, range) { 166541f93af9SSandeep Nair if (range->ops && range->ops->init_range) 166641f93af9SSandeep Nair range->ops->init_range(range); 166741f93af9SSandeep Nair base_idx = idx; 166841f93af9SSandeep Nair for (id = range->queue_base; 166941f93af9SSandeep Nair id < range->queue_base + range->num_queues; id++, idx++) { 167041f93af9SSandeep Nair ret = knav_queue_init_queue(kdev, range, 167141f93af9SSandeep Nair knav_queue_idx_to_inst(kdev, idx), id); 167241f93af9SSandeep Nair if (ret < 0) 167341f93af9SSandeep Nair return ret; 167441f93af9SSandeep Nair } 167541f93af9SSandeep Nair range->queue_base_inst = 167641f93af9SSandeep Nair knav_queue_idx_to_inst(kdev, base_idx); 167741f93af9SSandeep Nair } 167841f93af9SSandeep Nair return 0; 167941f93af9SSandeep Nair } 168041f93af9SSandeep Nair 168141f93af9SSandeep Nair static int knav_queue_probe(struct platform_device *pdev) 168241f93af9SSandeep Nair { 168341f93af9SSandeep Nair struct device_node *node = pdev->dev.of_node; 168441f93af9SSandeep Nair struct device_node *qmgrs, *queue_pools, *regions, *pdsps; 168541f93af9SSandeep Nair struct device *dev = &pdev->dev; 168641f93af9SSandeep Nair u32 temp[2]; 168741f93af9SSandeep Nair int ret; 168841f93af9SSandeep Nair 168941f93af9SSandeep Nair if (!node) { 169041f93af9SSandeep Nair dev_err(dev, "device tree info unavailable\n"); 169141f93af9SSandeep Nair return -ENODEV; 169241f93af9SSandeep Nair } 169341f93af9SSandeep Nair 169441f93af9SSandeep Nair kdev = devm_kzalloc(dev, sizeof(struct knav_device), GFP_KERNEL); 169541f93af9SSandeep Nair if (!kdev) { 169641f93af9SSandeep Nair dev_err(dev, "memory allocation failed\n"); 169741f93af9SSandeep Nair return -ENOMEM; 169841f93af9SSandeep Nair } 169941f93af9SSandeep Nair 170041f93af9SSandeep Nair platform_set_drvdata(pdev, kdev); 170141f93af9SSandeep Nair kdev->dev = dev; 170241f93af9SSandeep Nair INIT_LIST_HEAD(&kdev->queue_ranges); 170341f93af9SSandeep Nair INIT_LIST_HEAD(&kdev->qmgrs); 170441f93af9SSandeep Nair INIT_LIST_HEAD(&kdev->pools); 170541f93af9SSandeep Nair INIT_LIST_HEAD(&kdev->regions); 170641f93af9SSandeep Nair INIT_LIST_HEAD(&kdev->pdsps); 170741f93af9SSandeep Nair 170841f93af9SSandeep Nair pm_runtime_enable(&pdev->dev); 170941f93af9SSandeep Nair ret = pm_runtime_get_sync(&pdev->dev); 171041f93af9SSandeep Nair if (ret < 0) { 171141f93af9SSandeep Nair dev_err(dev, "Failed to enable QMSS\n"); 171241f93af9SSandeep Nair return ret; 171341f93af9SSandeep Nair } 171441f93af9SSandeep Nair 171541f93af9SSandeep Nair if (of_property_read_u32_array(node, "queue-range", temp, 2)) { 171641f93af9SSandeep Nair dev_err(dev, "queue-range not specified\n"); 171741f93af9SSandeep Nair ret = -ENODEV; 171841f93af9SSandeep Nair goto err; 171941f93af9SSandeep Nair } 172041f93af9SSandeep Nair kdev->base_id = temp[0]; 172141f93af9SSandeep Nair kdev->num_queues = temp[1]; 172241f93af9SSandeep Nair 172341f93af9SSandeep Nair /* Initialize queue managers using device tree configuration */ 172441f93af9SSandeep Nair qmgrs = of_get_child_by_name(node, "qmgrs"); 172541f93af9SSandeep Nair if (!qmgrs) { 172641f93af9SSandeep Nair dev_err(dev, "queue manager info not specified\n"); 172741f93af9SSandeep Nair ret = -ENODEV; 172841f93af9SSandeep Nair goto err; 172941f93af9SSandeep Nair } 173041f93af9SSandeep Nair ret = knav_queue_init_qmgrs(kdev, qmgrs); 173141f93af9SSandeep Nair of_node_put(qmgrs); 173241f93af9SSandeep Nair if (ret) 173341f93af9SSandeep Nair goto err; 173441f93af9SSandeep Nair 173541f93af9SSandeep Nair /* get pdsp configuration values from device tree */ 173641f93af9SSandeep Nair pdsps = of_get_child_by_name(node, "pdsps"); 173741f93af9SSandeep Nair if (pdsps) { 173841f93af9SSandeep Nair ret = knav_queue_init_pdsps(kdev, pdsps); 173941f93af9SSandeep Nair if (ret) 174041f93af9SSandeep Nair goto err; 174141f93af9SSandeep Nair 174241f93af9SSandeep Nair ret = knav_queue_start_pdsps(kdev); 174341f93af9SSandeep Nair if (ret) 174441f93af9SSandeep Nair goto err; 174541f93af9SSandeep Nair } 174641f93af9SSandeep Nair of_node_put(pdsps); 174741f93af9SSandeep Nair 174841f93af9SSandeep Nair /* get usable queue range values from device tree */ 174941f93af9SSandeep Nair queue_pools = of_get_child_by_name(node, "queue-pools"); 175041f93af9SSandeep Nair if (!queue_pools) { 175141f93af9SSandeep Nair dev_err(dev, "queue-pools not specified\n"); 175241f93af9SSandeep Nair ret = -ENODEV; 175341f93af9SSandeep Nair goto err; 175441f93af9SSandeep Nair } 175541f93af9SSandeep Nair ret = knav_setup_queue_pools(kdev, queue_pools); 175641f93af9SSandeep Nair of_node_put(queue_pools); 175741f93af9SSandeep Nair if (ret) 175841f93af9SSandeep Nair goto err; 175941f93af9SSandeep Nair 176041f93af9SSandeep Nair ret = knav_get_link_ram(kdev, "linkram0", &kdev->link_rams[0]); 176141f93af9SSandeep Nair if (ret) { 176241f93af9SSandeep Nair dev_err(kdev->dev, "could not setup linking ram\n"); 176341f93af9SSandeep Nair goto err; 176441f93af9SSandeep Nair } 176541f93af9SSandeep Nair 176641f93af9SSandeep Nair ret = knav_get_link_ram(kdev, "linkram1", &kdev->link_rams[1]); 176741f93af9SSandeep Nair if (ret) { 176841f93af9SSandeep Nair /* 176941f93af9SSandeep Nair * nothing really, we have one linking ram already, so we just 177041f93af9SSandeep Nair * live within our means 177141f93af9SSandeep Nair */ 177241f93af9SSandeep Nair } 177341f93af9SSandeep Nair 177441f93af9SSandeep Nair ret = knav_queue_setup_link_ram(kdev); 177541f93af9SSandeep Nair if (ret) 177641f93af9SSandeep Nair goto err; 177741f93af9SSandeep Nair 177841f93af9SSandeep Nair regions = of_get_child_by_name(node, "descriptor-regions"); 177941f93af9SSandeep Nair if (!regions) { 178041f93af9SSandeep Nair dev_err(dev, "descriptor-regions not specified\n"); 178141f93af9SSandeep Nair goto err; 178241f93af9SSandeep Nair } 178341f93af9SSandeep Nair ret = knav_queue_setup_regions(kdev, regions); 178441f93af9SSandeep Nair of_node_put(regions); 178541f93af9SSandeep Nair if (ret) 178641f93af9SSandeep Nair goto err; 178741f93af9SSandeep Nair 178841f93af9SSandeep Nair ret = knav_queue_init_queues(kdev); 178941f93af9SSandeep Nair if (ret < 0) { 179041f93af9SSandeep Nair dev_err(dev, "hwqueue initialization failed\n"); 179141f93af9SSandeep Nair goto err; 179241f93af9SSandeep Nair } 179341f93af9SSandeep Nair 179441f93af9SSandeep Nair debugfs_create_file("qmss", S_IFREG | S_IRUGO, NULL, NULL, 179541f93af9SSandeep Nair &knav_queue_debug_ops); 179641f93af9SSandeep Nair return 0; 179741f93af9SSandeep Nair 179841f93af9SSandeep Nair err: 179941f93af9SSandeep Nair knav_queue_stop_pdsps(kdev); 180041f93af9SSandeep Nair knav_queue_free_regions(kdev); 180141f93af9SSandeep Nair knav_free_queue_ranges(kdev); 180241f93af9SSandeep Nair pm_runtime_put_sync(&pdev->dev); 180341f93af9SSandeep Nair pm_runtime_disable(&pdev->dev); 180441f93af9SSandeep Nair return ret; 180541f93af9SSandeep Nair } 180641f93af9SSandeep Nair 180741f93af9SSandeep Nair static int knav_queue_remove(struct platform_device *pdev) 180841f93af9SSandeep Nair { 180941f93af9SSandeep Nair /* TODO: Free resources */ 181041f93af9SSandeep Nair pm_runtime_put_sync(&pdev->dev); 181141f93af9SSandeep Nair pm_runtime_disable(&pdev->dev); 181241f93af9SSandeep Nair return 0; 181341f93af9SSandeep Nair } 181441f93af9SSandeep Nair 181541f93af9SSandeep Nair /* Match table for of_platform binding */ 181641f93af9SSandeep Nair static struct of_device_id keystone_qmss_of_match[] = { 181741f93af9SSandeep Nair { .compatible = "ti,keystone-navigator-qmss", }, 181841f93af9SSandeep Nair {}, 181941f93af9SSandeep Nair }; 182041f93af9SSandeep Nair MODULE_DEVICE_TABLE(of, keystone_qmss_of_match); 182141f93af9SSandeep Nair 182241f93af9SSandeep Nair static struct platform_driver keystone_qmss_driver = { 182341f93af9SSandeep Nair .probe = knav_queue_probe, 182441f93af9SSandeep Nair .remove = knav_queue_remove, 182541f93af9SSandeep Nair .driver = { 182641f93af9SSandeep Nair .name = "keystone-navigator-qmss", 182741f93af9SSandeep Nair .of_match_table = keystone_qmss_of_match, 182841f93af9SSandeep Nair }, 182941f93af9SSandeep Nair }; 183041f93af9SSandeep Nair module_platform_driver(keystone_qmss_driver); 183141f93af9SSandeep Nair 183241f93af9SSandeep Nair MODULE_LICENSE("GPL v2"); 183341f93af9SSandeep Nair MODULE_DESCRIPTION("TI QMSS driver for Keystone SOCs"); 183441f93af9SSandeep Nair MODULE_AUTHOR("Sandeep Nair <sandeep_n@ti.com>"); 183541f93af9SSandeep Nair MODULE_AUTHOR("Santosh Shilimkar <santosh.shilimkar@ti.com>"); 1836