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