knav_qmss_queue.c 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844
  1. /*
  2. * Keystone Queue Manager subsystem driver
  3. *
  4. * Copyright (C) 2014 Texas Instruments Incorporated - http://www.ti.com
  5. * Authors: Sandeep Nair <sandeep_n@ti.com>
  6. * Cyril Chemparathy <cyril@ti.com>
  7. * Santosh Shilimkar <santosh.shilimkar@ti.com>
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * version 2 as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. */
  18. #include <linux/kernel.h>
  19. #include <linux/module.h>
  20. #include <linux/device.h>
  21. #include <linux/clk.h>
  22. #include <linux/io.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/bitops.h>
  25. #include <linux/slab.h>
  26. #include <linux/spinlock.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/dma-mapping.h>
  29. #include <linux/of.h>
  30. #include <linux/of_irq.h>
  31. #include <linux/of_device.h>
  32. #include <linux/of_address.h>
  33. #include <linux/pm_runtime.h>
  34. #include <linux/firmware.h>
  35. #include <linux/debugfs.h>
  36. #include <linux/seq_file.h>
  37. #include <linux/string.h>
  38. #include <linux/soc/ti/knav_qmss.h>
  39. #include "knav_qmss.h"
  40. static struct knav_device *kdev;
  41. static DEFINE_MUTEX(knav_dev_lock);
  42. /* Queue manager register indices in DTS */
  43. #define KNAV_QUEUE_PEEK_REG_INDEX 0
  44. #define KNAV_QUEUE_STATUS_REG_INDEX 1
  45. #define KNAV_QUEUE_CONFIG_REG_INDEX 2
  46. #define KNAV_QUEUE_REGION_REG_INDEX 3
  47. #define KNAV_QUEUE_PUSH_REG_INDEX 4
  48. #define KNAV_QUEUE_POP_REG_INDEX 5
  49. /* PDSP register indices in DTS */
  50. #define KNAV_QUEUE_PDSP_IRAM_REG_INDEX 0
  51. #define KNAV_QUEUE_PDSP_REGS_REG_INDEX 1
  52. #define KNAV_QUEUE_PDSP_INTD_REG_INDEX 2
  53. #define KNAV_QUEUE_PDSP_CMD_REG_INDEX 3
  54. #define knav_queue_idx_to_inst(kdev, idx) \
  55. (kdev->instances + (idx << kdev->inst_shift))
  56. #define for_each_handle_rcu(qh, inst) \
  57. list_for_each_entry_rcu(qh, &inst->handles, list)
  58. #define for_each_instance(idx, inst, kdev) \
  59. for (idx = 0, inst = kdev->instances; \
  60. idx < (kdev)->num_queues_in_use; \
  61. idx++, inst = knav_queue_idx_to_inst(kdev, idx))
  62. /* All firmware file names end up here. List the firmware file names below.
  63. * Newest followed by older ones. Search is done from start of the array
  64. * until a firmware file is found.
  65. */
  66. const char *knav_acc_firmwares[] = {"ks2_qmss_pdsp_acc48.bin"};
  67. /**
  68. * knav_queue_notify: qmss queue notfier call
  69. *
  70. * @inst: qmss queue instance like accumulator
  71. */
  72. void knav_queue_notify(struct knav_queue_inst *inst)
  73. {
  74. struct knav_queue *qh;
  75. if (!inst)
  76. return;
  77. rcu_read_lock();
  78. for_each_handle_rcu(qh, inst) {
  79. if (atomic_read(&qh->notifier_enabled) <= 0)
  80. continue;
  81. if (WARN_ON(!qh->notifier_fn))
  82. continue;
  83. atomic_inc(&qh->stats.notifies);
  84. qh->notifier_fn(qh->notifier_fn_arg);
  85. }
  86. rcu_read_unlock();
  87. }
  88. EXPORT_SYMBOL_GPL(knav_queue_notify);
  89. static irqreturn_t knav_queue_int_handler(int irq, void *_instdata)
  90. {
  91. struct knav_queue_inst *inst = _instdata;
  92. knav_queue_notify(inst);
  93. return IRQ_HANDLED;
  94. }
  95. static int knav_queue_setup_irq(struct knav_range_info *range,
  96. struct knav_queue_inst *inst)
  97. {
  98. unsigned queue = inst->id - range->queue_base;
  99. unsigned long cpu_map;
  100. int ret = 0, irq;
  101. if (range->flags & RANGE_HAS_IRQ) {
  102. irq = range->irqs[queue].irq;
  103. cpu_map = range->irqs[queue].cpu_map;
  104. ret = request_irq(irq, knav_queue_int_handler, 0,
  105. inst->irq_name, inst);
  106. if (ret)
  107. return ret;
  108. disable_irq(irq);
  109. if (cpu_map) {
  110. ret = irq_set_affinity_hint(irq, to_cpumask(&cpu_map));
  111. if (ret) {
  112. dev_warn(range->kdev->dev,
  113. "Failed to set IRQ affinity\n");
  114. return ret;
  115. }
  116. }
  117. }
  118. return ret;
  119. }
  120. static void knav_queue_free_irq(struct knav_queue_inst *inst)
  121. {
  122. struct knav_range_info *range = inst->range;
  123. unsigned queue = inst->id - inst->range->queue_base;
  124. int irq;
  125. if (range->flags & RANGE_HAS_IRQ) {
  126. irq = range->irqs[queue].irq;
  127. irq_set_affinity_hint(irq, NULL);
  128. free_irq(irq, inst);
  129. }
  130. }
  131. static inline bool knav_queue_is_busy(struct knav_queue_inst *inst)
  132. {
  133. return !list_empty(&inst->handles);
  134. }
  135. static inline bool knav_queue_is_reserved(struct knav_queue_inst *inst)
  136. {
  137. return inst->range->flags & RANGE_RESERVED;
  138. }
  139. static inline bool knav_queue_is_shared(struct knav_queue_inst *inst)
  140. {
  141. struct knav_queue *tmp;
  142. rcu_read_lock();
  143. for_each_handle_rcu(tmp, inst) {
  144. if (tmp->flags & KNAV_QUEUE_SHARED) {
  145. rcu_read_unlock();
  146. return true;
  147. }
  148. }
  149. rcu_read_unlock();
  150. return false;
  151. }
  152. static inline bool knav_queue_match_type(struct knav_queue_inst *inst,
  153. unsigned type)
  154. {
  155. if ((type == KNAV_QUEUE_QPEND) &&
  156. (inst->range->flags & RANGE_HAS_IRQ)) {
  157. return true;
  158. } else if ((type == KNAV_QUEUE_ACC) &&
  159. (inst->range->flags & RANGE_HAS_ACCUMULATOR)) {
  160. return true;
  161. } else if ((type == KNAV_QUEUE_GP) &&
  162. !(inst->range->flags &
  163. (RANGE_HAS_ACCUMULATOR | RANGE_HAS_IRQ))) {
  164. return true;
  165. }
  166. return false;
  167. }
  168. static inline struct knav_queue_inst *
  169. knav_queue_match_id_to_inst(struct knav_device *kdev, unsigned id)
  170. {
  171. struct knav_queue_inst *inst;
  172. int idx;
  173. for_each_instance(idx, inst, kdev) {
  174. if (inst->id == id)
  175. return inst;
  176. }
  177. return NULL;
  178. }
  179. static inline struct knav_queue_inst *knav_queue_find_by_id(int id)
  180. {
  181. if (kdev->base_id <= id &&
  182. kdev->base_id + kdev->num_queues > id) {
  183. id -= kdev->base_id;
  184. return knav_queue_match_id_to_inst(kdev, id);
  185. }
  186. return NULL;
  187. }
  188. static struct knav_queue *__knav_queue_open(struct knav_queue_inst *inst,
  189. const char *name, unsigned flags)
  190. {
  191. struct knav_queue *qh;
  192. unsigned id;
  193. int ret = 0;
  194. qh = devm_kzalloc(inst->kdev->dev, sizeof(*qh), GFP_KERNEL);
  195. if (!qh)
  196. return ERR_PTR(-ENOMEM);
  197. qh->flags = flags;
  198. qh->inst = inst;
  199. id = inst->id - inst->qmgr->start_queue;
  200. qh->reg_push = &inst->qmgr->reg_push[id];
  201. qh->reg_pop = &inst->qmgr->reg_pop[id];
  202. qh->reg_peek = &inst->qmgr->reg_peek[id];
  203. /* first opener? */
  204. if (!knav_queue_is_busy(inst)) {
  205. struct knav_range_info *range = inst->range;
  206. inst->name = kstrndup(name, KNAV_NAME_SIZE, GFP_KERNEL);
  207. if (range->ops && range->ops->open_queue)
  208. ret = range->ops->open_queue(range, inst, flags);
  209. if (ret) {
  210. devm_kfree(inst->kdev->dev, qh);
  211. return ERR_PTR(ret);
  212. }
  213. }
  214. list_add_tail_rcu(&qh->list, &inst->handles);
  215. return qh;
  216. }
  217. static struct knav_queue *
  218. knav_queue_open_by_id(const char *name, unsigned id, unsigned flags)
  219. {
  220. struct knav_queue_inst *inst;
  221. struct knav_queue *qh;
  222. mutex_lock(&knav_dev_lock);
  223. qh = ERR_PTR(-ENODEV);
  224. inst = knav_queue_find_by_id(id);
  225. if (!inst)
  226. goto unlock_ret;
  227. qh = ERR_PTR(-EEXIST);
  228. if (!(flags & KNAV_QUEUE_SHARED) && knav_queue_is_busy(inst))
  229. goto unlock_ret;
  230. qh = ERR_PTR(-EBUSY);
  231. if ((flags & KNAV_QUEUE_SHARED) &&
  232. (knav_queue_is_busy(inst) && !knav_queue_is_shared(inst)))
  233. goto unlock_ret;
  234. qh = __knav_queue_open(inst, name, flags);
  235. unlock_ret:
  236. mutex_unlock(&knav_dev_lock);
  237. return qh;
  238. }
  239. static struct knav_queue *knav_queue_open_by_type(const char *name,
  240. unsigned type, unsigned flags)
  241. {
  242. struct knav_queue_inst *inst;
  243. struct knav_queue *qh = ERR_PTR(-EINVAL);
  244. int idx;
  245. mutex_lock(&knav_dev_lock);
  246. for_each_instance(idx, inst, kdev) {
  247. if (knav_queue_is_reserved(inst))
  248. continue;
  249. if (!knav_queue_match_type(inst, type))
  250. continue;
  251. if (knav_queue_is_busy(inst))
  252. continue;
  253. qh = __knav_queue_open(inst, name, flags);
  254. goto unlock_ret;
  255. }
  256. unlock_ret:
  257. mutex_unlock(&knav_dev_lock);
  258. return qh;
  259. }
  260. static void knav_queue_set_notify(struct knav_queue_inst *inst, bool enabled)
  261. {
  262. struct knav_range_info *range = inst->range;
  263. if (range->ops && range->ops->set_notify)
  264. range->ops->set_notify(range, inst, enabled);
  265. }
  266. static int knav_queue_enable_notifier(struct knav_queue *qh)
  267. {
  268. struct knav_queue_inst *inst = qh->inst;
  269. bool first;
  270. if (WARN_ON(!qh->notifier_fn))
  271. return -EINVAL;
  272. /* Adjust the per handle notifier count */
  273. first = (atomic_inc_return(&qh->notifier_enabled) == 1);
  274. if (!first)
  275. return 0; /* nothing to do */
  276. /* Now adjust the per instance notifier count */
  277. first = (atomic_inc_return(&inst->num_notifiers) == 1);
  278. if (first)
  279. knav_queue_set_notify(inst, true);
  280. return 0;
  281. }
  282. static int knav_queue_disable_notifier(struct knav_queue *qh)
  283. {
  284. struct knav_queue_inst *inst = qh->inst;
  285. bool last;
  286. last = (atomic_dec_return(&qh->notifier_enabled) == 0);
  287. if (!last)
  288. return 0; /* nothing to do */
  289. last = (atomic_dec_return(&inst->num_notifiers) == 0);
  290. if (last)
  291. knav_queue_set_notify(inst, false);
  292. return 0;
  293. }
  294. static int knav_queue_set_notifier(struct knav_queue *qh,
  295. struct knav_queue_notify_config *cfg)
  296. {
  297. knav_queue_notify_fn old_fn = qh->notifier_fn;
  298. if (!cfg)
  299. return -EINVAL;
  300. if (!(qh->inst->range->flags & (RANGE_HAS_ACCUMULATOR | RANGE_HAS_IRQ)))
  301. return -ENOTSUPP;
  302. if (!cfg->fn && old_fn)
  303. knav_queue_disable_notifier(qh);
  304. qh->notifier_fn = cfg->fn;
  305. qh->notifier_fn_arg = cfg->fn_arg;
  306. if (cfg->fn && !old_fn)
  307. knav_queue_enable_notifier(qh);
  308. return 0;
  309. }
  310. static int knav_gp_set_notify(struct knav_range_info *range,
  311. struct knav_queue_inst *inst,
  312. bool enabled)
  313. {
  314. unsigned queue;
  315. if (range->flags & RANGE_HAS_IRQ) {
  316. queue = inst->id - range->queue_base;
  317. if (enabled)
  318. enable_irq(range->irqs[queue].irq);
  319. else
  320. disable_irq_nosync(range->irqs[queue].irq);
  321. }
  322. return 0;
  323. }
  324. static int knav_gp_open_queue(struct knav_range_info *range,
  325. struct knav_queue_inst *inst, unsigned flags)
  326. {
  327. return knav_queue_setup_irq(range, inst);
  328. }
  329. static int knav_gp_close_queue(struct knav_range_info *range,
  330. struct knav_queue_inst *inst)
  331. {
  332. knav_queue_free_irq(inst);
  333. return 0;
  334. }
  335. struct knav_range_ops knav_gp_range_ops = {
  336. .set_notify = knav_gp_set_notify,
  337. .open_queue = knav_gp_open_queue,
  338. .close_queue = knav_gp_close_queue,
  339. };
  340. static int knav_queue_get_count(void *qhandle)
  341. {
  342. struct knav_queue *qh = qhandle;
  343. struct knav_queue_inst *inst = qh->inst;
  344. return readl_relaxed(&qh->reg_peek[0].entry_count) +
  345. atomic_read(&inst->desc_count);
  346. }
  347. static void knav_queue_debug_show_instance(struct seq_file *s,
  348. struct knav_queue_inst *inst)
  349. {
  350. struct knav_device *kdev = inst->kdev;
  351. struct knav_queue *qh;
  352. if (!knav_queue_is_busy(inst))
  353. return;
  354. seq_printf(s, "\tqueue id %d (%s)\n",
  355. kdev->base_id + inst->id, inst->name);
  356. for_each_handle_rcu(qh, inst) {
  357. seq_printf(s, "\t\thandle %p: ", qh);
  358. seq_printf(s, "pushes %8d, ",
  359. atomic_read(&qh->stats.pushes));
  360. seq_printf(s, "pops %8d, ",
  361. atomic_read(&qh->stats.pops));
  362. seq_printf(s, "count %8d, ",
  363. knav_queue_get_count(qh));
  364. seq_printf(s, "notifies %8d, ",
  365. atomic_read(&qh->stats.notifies));
  366. seq_printf(s, "push errors %8d, ",
  367. atomic_read(&qh->stats.push_errors));
  368. seq_printf(s, "pop errors %8d\n",
  369. atomic_read(&qh->stats.pop_errors));
  370. }
  371. }
  372. static int knav_queue_debug_show(struct seq_file *s, void *v)
  373. {
  374. struct knav_queue_inst *inst;
  375. int idx;
  376. mutex_lock(&knav_dev_lock);
  377. seq_printf(s, "%s: %u-%u\n",
  378. dev_name(kdev->dev), kdev->base_id,
  379. kdev->base_id + kdev->num_queues - 1);
  380. for_each_instance(idx, inst, kdev)
  381. knav_queue_debug_show_instance(s, inst);
  382. mutex_unlock(&knav_dev_lock);
  383. return 0;
  384. }
  385. static int knav_queue_debug_open(struct inode *inode, struct file *file)
  386. {
  387. return single_open(file, knav_queue_debug_show, NULL);
  388. }
  389. static const struct file_operations knav_queue_debug_ops = {
  390. .open = knav_queue_debug_open,
  391. .read = seq_read,
  392. .llseek = seq_lseek,
  393. .release = single_release,
  394. };
  395. static inline int knav_queue_pdsp_wait(u32 * __iomem addr, unsigned timeout,
  396. u32 flags)
  397. {
  398. unsigned long end;
  399. u32 val = 0;
  400. end = jiffies + msecs_to_jiffies(timeout);
  401. while (time_after(end, jiffies)) {
  402. val = readl_relaxed(addr);
  403. if (flags)
  404. val &= flags;
  405. if (!val)
  406. break;
  407. cpu_relax();
  408. }
  409. return val ? -ETIMEDOUT : 0;
  410. }
  411. static int knav_queue_flush(struct knav_queue *qh)
  412. {
  413. struct knav_queue_inst *inst = qh->inst;
  414. unsigned id = inst->id - inst->qmgr->start_queue;
  415. atomic_set(&inst->desc_count, 0);
  416. writel_relaxed(0, &inst->qmgr->reg_push[id].ptr_size_thresh);
  417. return 0;
  418. }
  419. /**
  420. * knav_queue_open() - open a hardware queue
  421. * @name - name to give the queue handle
  422. * @id - desired queue number if any or specifes the type
  423. * of queue
  424. * @flags - the following flags are applicable to queues:
  425. * KNAV_QUEUE_SHARED - allow the queue to be shared. Queues are
  426. * exclusive by default.
  427. * Subsequent attempts to open a shared queue should
  428. * also have this flag.
  429. *
  430. * Returns a handle to the open hardware queue if successful. Use IS_ERR()
  431. * to check the returned value for error codes.
  432. */
  433. void *knav_queue_open(const char *name, unsigned id,
  434. unsigned flags)
  435. {
  436. struct knav_queue *qh = ERR_PTR(-EINVAL);
  437. switch (id) {
  438. case KNAV_QUEUE_QPEND:
  439. case KNAV_QUEUE_ACC:
  440. case KNAV_QUEUE_GP:
  441. qh = knav_queue_open_by_type(name, id, flags);
  442. break;
  443. default:
  444. qh = knav_queue_open_by_id(name, id, flags);
  445. break;
  446. }
  447. return qh;
  448. }
  449. EXPORT_SYMBOL_GPL(knav_queue_open);
  450. /**
  451. * knav_queue_close() - close a hardware queue handle
  452. * @qh - handle to close
  453. */
  454. void knav_queue_close(void *qhandle)
  455. {
  456. struct knav_queue *qh = qhandle;
  457. struct knav_queue_inst *inst = qh->inst;
  458. while (atomic_read(&qh->notifier_enabled) > 0)
  459. knav_queue_disable_notifier(qh);
  460. mutex_lock(&knav_dev_lock);
  461. list_del_rcu(&qh->list);
  462. mutex_unlock(&knav_dev_lock);
  463. synchronize_rcu();
  464. if (!knav_queue_is_busy(inst)) {
  465. struct knav_range_info *range = inst->range;
  466. if (range->ops && range->ops->close_queue)
  467. range->ops->close_queue(range, inst);
  468. }
  469. devm_kfree(inst->kdev->dev, qh);
  470. }
  471. EXPORT_SYMBOL_GPL(knav_queue_close);
  472. /**
  473. * knav_queue_device_control() - Perform control operations on a queue
  474. * @qh - queue handle
  475. * @cmd - control commands
  476. * @arg - command argument
  477. *
  478. * Returns 0 on success, errno otherwise.
  479. */
  480. int knav_queue_device_control(void *qhandle, enum knav_queue_ctrl_cmd cmd,
  481. unsigned long arg)
  482. {
  483. struct knav_queue *qh = qhandle;
  484. struct knav_queue_notify_config *cfg;
  485. int ret;
  486. switch ((int)cmd) {
  487. case KNAV_QUEUE_GET_ID:
  488. ret = qh->inst->kdev->base_id + qh->inst->id;
  489. break;
  490. case KNAV_QUEUE_FLUSH:
  491. ret = knav_queue_flush(qh);
  492. break;
  493. case KNAV_QUEUE_SET_NOTIFIER:
  494. cfg = (void *)arg;
  495. ret = knav_queue_set_notifier(qh, cfg);
  496. break;
  497. case KNAV_QUEUE_ENABLE_NOTIFY:
  498. ret = knav_queue_enable_notifier(qh);
  499. break;
  500. case KNAV_QUEUE_DISABLE_NOTIFY:
  501. ret = knav_queue_disable_notifier(qh);
  502. break;
  503. case KNAV_QUEUE_GET_COUNT:
  504. ret = knav_queue_get_count(qh);
  505. break;
  506. default:
  507. ret = -ENOTSUPP;
  508. break;
  509. }
  510. return ret;
  511. }
  512. EXPORT_SYMBOL_GPL(knav_queue_device_control);
  513. /**
  514. * knav_queue_push() - push data (or descriptor) to the tail of a queue
  515. * @qh - hardware queue handle
  516. * @data - data to push
  517. * @size - size of data to push
  518. * @flags - can be used to pass additional information
  519. *
  520. * Returns 0 on success, errno otherwise.
  521. */
  522. int knav_queue_push(void *qhandle, dma_addr_t dma,
  523. unsigned size, unsigned flags)
  524. {
  525. struct knav_queue *qh = qhandle;
  526. u32 val;
  527. val = (u32)dma | ((size / 16) - 1);
  528. writel_relaxed(val, &qh->reg_push[0].ptr_size_thresh);
  529. atomic_inc(&qh->stats.pushes);
  530. return 0;
  531. }
  532. EXPORT_SYMBOL_GPL(knav_queue_push);
  533. /**
  534. * knav_queue_pop() - pop data (or descriptor) from the head of a queue
  535. * @qh - hardware queue handle
  536. * @size - (optional) size of the data pop'ed.
  537. *
  538. * Returns a DMA address on success, 0 on failure.
  539. */
  540. dma_addr_t knav_queue_pop(void *qhandle, unsigned *size)
  541. {
  542. struct knav_queue *qh = qhandle;
  543. struct knav_queue_inst *inst = qh->inst;
  544. dma_addr_t dma;
  545. u32 val, idx;
  546. /* are we accumulated? */
  547. if (inst->descs) {
  548. if (unlikely(atomic_dec_return(&inst->desc_count) < 0)) {
  549. atomic_inc(&inst->desc_count);
  550. return 0;
  551. }
  552. idx = atomic_inc_return(&inst->desc_head);
  553. idx &= ACC_DESCS_MASK;
  554. val = inst->descs[idx];
  555. } else {
  556. val = readl_relaxed(&qh->reg_pop[0].ptr_size_thresh);
  557. if (unlikely(!val))
  558. return 0;
  559. }
  560. dma = val & DESC_PTR_MASK;
  561. if (size)
  562. *size = ((val & DESC_SIZE_MASK) + 1) * 16;
  563. atomic_inc(&qh->stats.pops);
  564. return dma;
  565. }
  566. EXPORT_SYMBOL_GPL(knav_queue_pop);
  567. /* carve out descriptors and push into queue */
  568. static void kdesc_fill_pool(struct knav_pool *pool)
  569. {
  570. struct knav_region *region;
  571. int i;
  572. region = pool->region;
  573. pool->desc_size = region->desc_size;
  574. for (i = 0; i < pool->num_desc; i++) {
  575. int index = pool->region_offset + i;
  576. dma_addr_t dma_addr;
  577. unsigned dma_size;
  578. dma_addr = region->dma_start + (region->desc_size * index);
  579. dma_size = ALIGN(pool->desc_size, SMP_CACHE_BYTES);
  580. dma_sync_single_for_device(pool->dev, dma_addr, dma_size,
  581. DMA_TO_DEVICE);
  582. knav_queue_push(pool->queue, dma_addr, dma_size, 0);
  583. }
  584. }
  585. /* pop out descriptors and close the queue */
  586. static void kdesc_empty_pool(struct knav_pool *pool)
  587. {
  588. dma_addr_t dma;
  589. unsigned size;
  590. void *desc;
  591. int i;
  592. if (!pool->queue)
  593. return;
  594. for (i = 0;; i++) {
  595. dma = knav_queue_pop(pool->queue, &size);
  596. if (!dma)
  597. break;
  598. desc = knav_pool_desc_dma_to_virt(pool, dma);
  599. if (!desc) {
  600. dev_dbg(pool->kdev->dev,
  601. "couldn't unmap desc, continuing\n");
  602. continue;
  603. }
  604. }
  605. WARN_ON(i != pool->num_desc);
  606. knav_queue_close(pool->queue);
  607. }
  608. /* Get the DMA address of a descriptor */
  609. dma_addr_t knav_pool_desc_virt_to_dma(void *ph, void *virt)
  610. {
  611. struct knav_pool *pool = ph;
  612. return pool->region->dma_start + (virt - pool->region->virt_start);
  613. }
  614. EXPORT_SYMBOL_GPL(knav_pool_desc_virt_to_dma);
  615. void *knav_pool_desc_dma_to_virt(void *ph, dma_addr_t dma)
  616. {
  617. struct knav_pool *pool = ph;
  618. return pool->region->virt_start + (dma - pool->region->dma_start);
  619. }
  620. EXPORT_SYMBOL_GPL(knav_pool_desc_dma_to_virt);
  621. /**
  622. * knav_pool_create() - Create a pool of descriptors
  623. * @name - name to give the pool handle
  624. * @num_desc - numbers of descriptors in the pool
  625. * @region_id - QMSS region id from which the descriptors are to be
  626. * allocated.
  627. *
  628. * Returns a pool handle on success.
  629. * Use IS_ERR_OR_NULL() to identify error values on return.
  630. */
  631. void *knav_pool_create(const char *name,
  632. int num_desc, int region_id)
  633. {
  634. struct knav_region *reg_itr, *region = NULL;
  635. struct knav_pool *pool, *pi;
  636. struct list_head *node;
  637. unsigned last_offset;
  638. bool slot_found;
  639. int ret;
  640. if (!kdev->dev)
  641. return ERR_PTR(-ENODEV);
  642. pool = devm_kzalloc(kdev->dev, sizeof(*pool), GFP_KERNEL);
  643. if (!pool) {
  644. dev_err(kdev->dev, "out of memory allocating pool\n");
  645. return ERR_PTR(-ENOMEM);
  646. }
  647. for_each_region(kdev, reg_itr) {
  648. if (reg_itr->id != region_id)
  649. continue;
  650. region = reg_itr;
  651. break;
  652. }
  653. if (!region) {
  654. dev_err(kdev->dev, "region-id(%d) not found\n", region_id);
  655. ret = -EINVAL;
  656. goto err;
  657. }
  658. pool->queue = knav_queue_open(name, KNAV_QUEUE_GP, 0);
  659. if (IS_ERR_OR_NULL(pool->queue)) {
  660. dev_err(kdev->dev,
  661. "failed to open queue for pool(%s), error %ld\n",
  662. name, PTR_ERR(pool->queue));
  663. ret = PTR_ERR(pool->queue);
  664. goto err;
  665. }
  666. pool->name = kstrndup(name, KNAV_NAME_SIZE, GFP_KERNEL);
  667. pool->kdev = kdev;
  668. pool->dev = kdev->dev;
  669. mutex_lock(&knav_dev_lock);
  670. if (num_desc > (region->num_desc - region->used_desc)) {
  671. dev_err(kdev->dev, "out of descs in region(%d) for pool(%s)\n",
  672. region_id, name);
  673. ret = -ENOMEM;
  674. goto err_unlock;
  675. }
  676. /* Region maintains a sorted (by region offset) list of pools
  677. * use the first free slot which is large enough to accomodate
  678. * the request
  679. */
  680. last_offset = 0;
  681. slot_found = false;
  682. node = &region->pools;
  683. list_for_each_entry(pi, &region->pools, region_inst) {
  684. if ((pi->region_offset - last_offset) >= num_desc) {
  685. slot_found = true;
  686. break;
  687. }
  688. last_offset = pi->region_offset + pi->num_desc;
  689. }
  690. node = &pi->region_inst;
  691. if (slot_found) {
  692. pool->region = region;
  693. pool->num_desc = num_desc;
  694. pool->region_offset = last_offset;
  695. region->used_desc += num_desc;
  696. list_add_tail(&pool->list, &kdev->pools);
  697. list_add_tail(&pool->region_inst, node);
  698. } else {
  699. dev_err(kdev->dev, "pool(%s) create failed: fragmented desc pool in region(%d)\n",
  700. name, region_id);
  701. ret = -ENOMEM;
  702. goto err_unlock;
  703. }
  704. mutex_unlock(&knav_dev_lock);
  705. kdesc_fill_pool(pool);
  706. return pool;
  707. err_unlock:
  708. mutex_unlock(&knav_dev_lock);
  709. err:
  710. kfree(pool->name);
  711. devm_kfree(kdev->dev, pool);
  712. return ERR_PTR(ret);
  713. }
  714. EXPORT_SYMBOL_GPL(knav_pool_create);
  715. /**
  716. * knav_pool_destroy() - Free a pool of descriptors
  717. * @pool - pool handle
  718. */
  719. void knav_pool_destroy(void *ph)
  720. {
  721. struct knav_pool *pool = ph;
  722. if (!pool)
  723. return;
  724. if (!pool->region)
  725. return;
  726. kdesc_empty_pool(pool);
  727. mutex_lock(&knav_dev_lock);
  728. pool->region->used_desc -= pool->num_desc;
  729. list_del(&pool->region_inst);
  730. list_del(&pool->list);
  731. mutex_unlock(&knav_dev_lock);
  732. kfree(pool->name);
  733. devm_kfree(kdev->dev, pool);
  734. }
  735. EXPORT_SYMBOL_GPL(knav_pool_destroy);
  736. /**
  737. * knav_pool_desc_get() - Get a descriptor from the pool
  738. * @pool - pool handle
  739. *
  740. * Returns descriptor from the pool.
  741. */
  742. void *knav_pool_desc_get(void *ph)
  743. {
  744. struct knav_pool *pool = ph;
  745. dma_addr_t dma;
  746. unsigned size;
  747. void *data;
  748. dma = knav_queue_pop(pool->queue, &size);
  749. if (unlikely(!dma))
  750. return ERR_PTR(-ENOMEM);
  751. data = knav_pool_desc_dma_to_virt(pool, dma);
  752. return data;
  753. }
  754. EXPORT_SYMBOL_GPL(knav_pool_desc_get);
  755. /**
  756. * knav_pool_desc_put() - return a descriptor to the pool
  757. * @pool - pool handle
  758. */
  759. void knav_pool_desc_put(void *ph, void *desc)
  760. {
  761. struct knav_pool *pool = ph;
  762. dma_addr_t dma;
  763. dma = knav_pool_desc_virt_to_dma(pool, desc);
  764. knav_queue_push(pool->queue, dma, pool->region->desc_size, 0);
  765. }
  766. EXPORT_SYMBOL_GPL(knav_pool_desc_put);
  767. /**
  768. * knav_pool_desc_map() - Map descriptor for DMA transfer
  769. * @pool - pool handle
  770. * @desc - address of descriptor to map
  771. * @size - size of descriptor to map
  772. * @dma - DMA address return pointer
  773. * @dma_sz - adjusted return pointer
  774. *
  775. * Returns 0 on success, errno otherwise.
  776. */
  777. int knav_pool_desc_map(void *ph, void *desc, unsigned size,
  778. dma_addr_t *dma, unsigned *dma_sz)
  779. {
  780. struct knav_pool *pool = ph;
  781. *dma = knav_pool_desc_virt_to_dma(pool, desc);
  782. size = min(size, pool->region->desc_size);
  783. size = ALIGN(size, SMP_CACHE_BYTES);
  784. *dma_sz = size;
  785. dma_sync_single_for_device(pool->dev, *dma, size, DMA_TO_DEVICE);
  786. /* Ensure the descriptor reaches to the memory */
  787. __iowmb();
  788. return 0;
  789. }
  790. EXPORT_SYMBOL_GPL(knav_pool_desc_map);
  791. /**
  792. * knav_pool_desc_unmap() - Unmap descriptor after DMA transfer
  793. * @pool - pool handle
  794. * @dma - DMA address of descriptor to unmap
  795. * @dma_sz - size of descriptor to unmap
  796. *
  797. * Returns descriptor address on success, Use IS_ERR_OR_NULL() to identify
  798. * error values on return.
  799. */
  800. void *knav_pool_desc_unmap(void *ph, dma_addr_t dma, unsigned dma_sz)
  801. {
  802. struct knav_pool *pool = ph;
  803. unsigned desc_sz;
  804. void *desc;
  805. desc_sz = min(dma_sz, pool->region->desc_size);
  806. desc = knav_pool_desc_dma_to_virt(pool, dma);
  807. dma_sync_single_for_cpu(pool->dev, dma, desc_sz, DMA_FROM_DEVICE);
  808. prefetch(desc);
  809. return desc;
  810. }
  811. EXPORT_SYMBOL_GPL(knav_pool_desc_unmap);
  812. /**
  813. * knav_pool_count() - Get the number of descriptors in pool.
  814. * @pool - pool handle
  815. * Returns number of elements in the pool.
  816. */
  817. int knav_pool_count(void *ph)
  818. {
  819. struct knav_pool *pool = ph;
  820. return knav_queue_get_count(pool->queue);
  821. }
  822. EXPORT_SYMBOL_GPL(knav_pool_count);
  823. static void knav_queue_setup_region(struct knav_device *kdev,
  824. struct knav_region *region)
  825. {
  826. unsigned hw_num_desc, hw_desc_size, size;
  827. struct knav_reg_region __iomem *regs;
  828. struct knav_qmgr_info *qmgr;
  829. struct knav_pool *pool;
  830. int id = region->id;
  831. struct page *page;
  832. /* unused region? */
  833. if (!region->num_desc) {
  834. dev_warn(kdev->dev, "unused region %s\n", region->name);
  835. return;
  836. }
  837. /* get hardware descriptor value */
  838. hw_num_desc = ilog2(region->num_desc - 1) + 1;
  839. /* did we force fit ourselves into nothingness? */
  840. if (region->num_desc < 32) {
  841. region->num_desc = 0;
  842. dev_warn(kdev->dev, "too few descriptors in region %s\n",
  843. region->name);
  844. return;
  845. }
  846. size = region->num_desc * region->desc_size;
  847. region->virt_start = alloc_pages_exact(size, GFP_KERNEL | GFP_DMA |
  848. GFP_DMA32);
  849. if (!region->virt_start) {
  850. region->num_desc = 0;
  851. dev_err(kdev->dev, "memory alloc failed for region %s\n",
  852. region->name);
  853. return;
  854. }
  855. region->virt_end = region->virt_start + size;
  856. page = virt_to_page(region->virt_start);
  857. region->dma_start = dma_map_page(kdev->dev, page, 0, size,
  858. DMA_BIDIRECTIONAL);
  859. if (dma_mapping_error(kdev->dev, region->dma_start)) {
  860. dev_err(kdev->dev, "dma map failed for region %s\n",
  861. region->name);
  862. goto fail;
  863. }
  864. region->dma_end = region->dma_start + size;
  865. pool = devm_kzalloc(kdev->dev, sizeof(*pool), GFP_KERNEL);
  866. if (!pool) {
  867. dev_err(kdev->dev, "out of memory allocating dummy pool\n");
  868. goto fail;
  869. }
  870. pool->num_desc = 0;
  871. pool->region_offset = region->num_desc;
  872. list_add(&pool->region_inst, &region->pools);
  873. dev_dbg(kdev->dev,
  874. "region %s (%d): size:%d, link:%d@%d, phys:%08x-%08x, virt:%p-%p\n",
  875. region->name, id, region->desc_size, region->num_desc,
  876. region->link_index, region->dma_start, region->dma_end,
  877. region->virt_start, region->virt_end);
  878. hw_desc_size = (region->desc_size / 16) - 1;
  879. hw_num_desc -= 5;
  880. for_each_qmgr(kdev, qmgr) {
  881. regs = qmgr->reg_region + id;
  882. writel_relaxed(region->dma_start, &regs->base);
  883. writel_relaxed(region->link_index, &regs->start_index);
  884. writel_relaxed(hw_desc_size << 16 | hw_num_desc,
  885. &regs->size_count);
  886. }
  887. return;
  888. fail:
  889. if (region->dma_start)
  890. dma_unmap_page(kdev->dev, region->dma_start, size,
  891. DMA_BIDIRECTIONAL);
  892. if (region->virt_start)
  893. free_pages_exact(region->virt_start, size);
  894. region->num_desc = 0;
  895. return;
  896. }
  897. static const char *knav_queue_find_name(struct device_node *node)
  898. {
  899. const char *name;
  900. if (of_property_read_string(node, "label", &name) < 0)
  901. name = node->name;
  902. if (!name)
  903. name = "unknown";
  904. return name;
  905. }
  906. static int knav_queue_setup_regions(struct knav_device *kdev,
  907. struct device_node *regions)
  908. {
  909. struct device *dev = kdev->dev;
  910. struct knav_region *region;
  911. struct device_node *child;
  912. u32 temp[2];
  913. int ret;
  914. for_each_child_of_node(regions, child) {
  915. region = devm_kzalloc(dev, sizeof(*region), GFP_KERNEL);
  916. if (!region) {
  917. dev_err(dev, "out of memory allocating region\n");
  918. return -ENOMEM;
  919. }
  920. region->name = knav_queue_find_name(child);
  921. of_property_read_u32(child, "id", &region->id);
  922. ret = of_property_read_u32_array(child, "region-spec", temp, 2);
  923. if (!ret) {
  924. region->num_desc = temp[0];
  925. region->desc_size = temp[1];
  926. } else {
  927. dev_err(dev, "invalid region info %s\n", region->name);
  928. devm_kfree(dev, region);
  929. continue;
  930. }
  931. if (!of_get_property(child, "link-index", NULL)) {
  932. dev_err(dev, "No link info for %s\n", region->name);
  933. devm_kfree(dev, region);
  934. continue;
  935. }
  936. ret = of_property_read_u32(child, "link-index",
  937. &region->link_index);
  938. if (ret) {
  939. dev_err(dev, "link index not found for %s\n",
  940. region->name);
  941. devm_kfree(dev, region);
  942. continue;
  943. }
  944. INIT_LIST_HEAD(&region->pools);
  945. list_add_tail(&region->list, &kdev->regions);
  946. }
  947. if (list_empty(&kdev->regions)) {
  948. dev_err(dev, "no valid region information found\n");
  949. return -ENODEV;
  950. }
  951. /* Next, we run through the regions and set things up */
  952. for_each_region(kdev, region)
  953. knav_queue_setup_region(kdev, region);
  954. return 0;
  955. }
  956. static int knav_get_link_ram(struct knav_device *kdev,
  957. const char *name,
  958. struct knav_link_ram_block *block)
  959. {
  960. struct platform_device *pdev = to_platform_device(kdev->dev);
  961. struct device_node *node = pdev->dev.of_node;
  962. u32 temp[2];
  963. /*
  964. * Note: link ram resources are specified in "entry" sized units. In
  965. * reality, although entries are ~40bits in hardware, we treat them as
  966. * 64-bit entities here.
  967. *
  968. * For example, to specify the internal link ram for Keystone-I class
  969. * devices, we would set the linkram0 resource to 0x80000-0x83fff.
  970. *
  971. * This gets a bit weird when other link rams are used. For example,
  972. * if the range specified is 0x0c000000-0x0c003fff (i.e., 16K entries
  973. * in MSMC SRAM), the actual memory used is 0x0c000000-0x0c020000,
  974. * which accounts for 64-bits per entry, for 16K entries.
  975. */
  976. if (!of_property_read_u32_array(node, name , temp, 2)) {
  977. if (temp[0]) {
  978. /*
  979. * queue_base specified => using internal or onchip
  980. * link ram WARNING - we do not "reserve" this block
  981. */
  982. block->phys = (dma_addr_t)temp[0];
  983. block->virt = NULL;
  984. block->size = temp[1];
  985. } else {
  986. block->size = temp[1];
  987. /* queue_base not specific => allocate requested size */
  988. block->virt = dmam_alloc_coherent(kdev->dev,
  989. 8 * block->size, &block->phys,
  990. GFP_KERNEL);
  991. if (!block->virt) {
  992. dev_err(kdev->dev, "failed to alloc linkram\n");
  993. return -ENOMEM;
  994. }
  995. }
  996. } else {
  997. return -ENODEV;
  998. }
  999. return 0;
  1000. }
  1001. static int knav_queue_setup_link_ram(struct knav_device *kdev)
  1002. {
  1003. struct knav_link_ram_block *block;
  1004. struct knav_qmgr_info *qmgr;
  1005. for_each_qmgr(kdev, qmgr) {
  1006. block = &kdev->link_rams[0];
  1007. dev_dbg(kdev->dev, "linkram0: phys:%x, virt:%p, size:%x\n",
  1008. block->phys, block->virt, block->size);
  1009. writel_relaxed(block->phys, &qmgr->reg_config->link_ram_base0);
  1010. writel_relaxed(block->size, &qmgr->reg_config->link_ram_size0);
  1011. block++;
  1012. if (!block->size)
  1013. continue;
  1014. dev_dbg(kdev->dev, "linkram1: phys:%x, virt:%p, size:%x\n",
  1015. block->phys, block->virt, block->size);
  1016. writel_relaxed(block->phys, &qmgr->reg_config->link_ram_base1);
  1017. }
  1018. return 0;
  1019. }
  1020. static int knav_setup_queue_range(struct knav_device *kdev,
  1021. struct device_node *node)
  1022. {
  1023. struct device *dev = kdev->dev;
  1024. struct knav_range_info *range;
  1025. struct knav_qmgr_info *qmgr;
  1026. u32 temp[2], start, end, id, index;
  1027. int ret, i;
  1028. range = devm_kzalloc(dev, sizeof(*range), GFP_KERNEL);
  1029. if (!range) {
  1030. dev_err(dev, "out of memory allocating range\n");
  1031. return -ENOMEM;
  1032. }
  1033. range->kdev = kdev;
  1034. range->name = knav_queue_find_name(node);
  1035. ret = of_property_read_u32_array(node, "qrange", temp, 2);
  1036. if (!ret) {
  1037. range->queue_base = temp[0] - kdev->base_id;
  1038. range->num_queues = temp[1];
  1039. } else {
  1040. dev_err(dev, "invalid queue range %s\n", range->name);
  1041. devm_kfree(dev, range);
  1042. return -EINVAL;
  1043. }
  1044. for (i = 0; i < RANGE_MAX_IRQS; i++) {
  1045. struct of_phandle_args oirq;
  1046. if (of_irq_parse_one(node, i, &oirq))
  1047. break;
  1048. range->irqs[i].irq = irq_create_of_mapping(&oirq);
  1049. if (range->irqs[i].irq == IRQ_NONE)
  1050. break;
  1051. range->num_irqs++;
  1052. if (oirq.args_count == 3)
  1053. range->irqs[i].cpu_map =
  1054. (oirq.args[2] & 0x0000ff00) >> 8;
  1055. }
  1056. range->num_irqs = min(range->num_irqs, range->num_queues);
  1057. if (range->num_irqs)
  1058. range->flags |= RANGE_HAS_IRQ;
  1059. if (of_get_property(node, "qalloc-by-id", NULL))
  1060. range->flags |= RANGE_RESERVED;
  1061. if (of_get_property(node, "accumulator", NULL)) {
  1062. ret = knav_init_acc_range(kdev, node, range);
  1063. if (ret < 0) {
  1064. devm_kfree(dev, range);
  1065. return ret;
  1066. }
  1067. } else {
  1068. range->ops = &knav_gp_range_ops;
  1069. }
  1070. /* set threshold to 1, and flush out the queues */
  1071. for_each_qmgr(kdev, qmgr) {
  1072. start = max(qmgr->start_queue, range->queue_base);
  1073. end = min(qmgr->start_queue + qmgr->num_queues,
  1074. range->queue_base + range->num_queues);
  1075. for (id = start; id < end; id++) {
  1076. index = id - qmgr->start_queue;
  1077. writel_relaxed(THRESH_GTE | 1,
  1078. &qmgr->reg_peek[index].ptr_size_thresh);
  1079. writel_relaxed(0,
  1080. &qmgr->reg_push[index].ptr_size_thresh);
  1081. }
  1082. }
  1083. list_add_tail(&range->list, &kdev->queue_ranges);
  1084. dev_dbg(dev, "added range %s: %d-%d, %d irqs%s%s%s\n",
  1085. range->name, range->queue_base,
  1086. range->queue_base + range->num_queues - 1,
  1087. range->num_irqs,
  1088. (range->flags & RANGE_HAS_IRQ) ? ", has irq" : "",
  1089. (range->flags & RANGE_RESERVED) ? ", reserved" : "",
  1090. (range->flags & RANGE_HAS_ACCUMULATOR) ? ", acc" : "");
  1091. kdev->num_queues_in_use += range->num_queues;
  1092. return 0;
  1093. }
  1094. static int knav_setup_queue_pools(struct knav_device *kdev,
  1095. struct device_node *queue_pools)
  1096. {
  1097. struct device_node *type, *range;
  1098. int ret;
  1099. for_each_child_of_node(queue_pools, type) {
  1100. for_each_child_of_node(type, range) {
  1101. ret = knav_setup_queue_range(kdev, range);
  1102. /* return value ignored, we init the rest... */
  1103. }
  1104. }
  1105. /* ... and barf if they all failed! */
  1106. if (list_empty(&kdev->queue_ranges)) {
  1107. dev_err(kdev->dev, "no valid queue range found\n");
  1108. return -ENODEV;
  1109. }
  1110. return 0;
  1111. }
  1112. static void knav_free_queue_range(struct knav_device *kdev,
  1113. struct knav_range_info *range)
  1114. {
  1115. if (range->ops && range->ops->free_range)
  1116. range->ops->free_range(range);
  1117. list_del(&range->list);
  1118. devm_kfree(kdev->dev, range);
  1119. }
  1120. static void knav_free_queue_ranges(struct knav_device *kdev)
  1121. {
  1122. struct knav_range_info *range;
  1123. for (;;) {
  1124. range = first_queue_range(kdev);
  1125. if (!range)
  1126. break;
  1127. knav_free_queue_range(kdev, range);
  1128. }
  1129. }
  1130. static void knav_queue_free_regions(struct knav_device *kdev)
  1131. {
  1132. struct knav_region *region;
  1133. struct knav_pool *pool, *tmp;
  1134. unsigned size;
  1135. for (;;) {
  1136. region = first_region(kdev);
  1137. if (!region)
  1138. break;
  1139. list_for_each_entry_safe(pool, tmp, &region->pools, region_inst)
  1140. knav_pool_destroy(pool);
  1141. size = region->virt_end - region->virt_start;
  1142. if (size)
  1143. free_pages_exact(region->virt_start, size);
  1144. list_del(&region->list);
  1145. devm_kfree(kdev->dev, region);
  1146. }
  1147. }
  1148. static void __iomem *knav_queue_map_reg(struct knav_device *kdev,
  1149. struct device_node *node, int index)
  1150. {
  1151. struct resource res;
  1152. void __iomem *regs;
  1153. int ret;
  1154. ret = of_address_to_resource(node, index, &res);
  1155. if (ret) {
  1156. dev_err(kdev->dev, "Can't translate of node(%s) address for index(%d)\n",
  1157. node->name, index);
  1158. return ERR_PTR(ret);
  1159. }
  1160. regs = devm_ioremap_resource(kdev->dev, &res);
  1161. if (IS_ERR(regs))
  1162. dev_err(kdev->dev, "Failed to map register base for index(%d) node(%s)\n",
  1163. index, node->name);
  1164. return regs;
  1165. }
  1166. static int knav_queue_init_qmgrs(struct knav_device *kdev,
  1167. struct device_node *qmgrs)
  1168. {
  1169. struct device *dev = kdev->dev;
  1170. struct knav_qmgr_info *qmgr;
  1171. struct device_node *child;
  1172. u32 temp[2];
  1173. int ret;
  1174. for_each_child_of_node(qmgrs, child) {
  1175. qmgr = devm_kzalloc(dev, sizeof(*qmgr), GFP_KERNEL);
  1176. if (!qmgr) {
  1177. dev_err(dev, "out of memory allocating qmgr\n");
  1178. return -ENOMEM;
  1179. }
  1180. ret = of_property_read_u32_array(child, "managed-queues",
  1181. temp, 2);
  1182. if (!ret) {
  1183. qmgr->start_queue = temp[0];
  1184. qmgr->num_queues = temp[1];
  1185. } else {
  1186. dev_err(dev, "invalid qmgr queue range\n");
  1187. devm_kfree(dev, qmgr);
  1188. continue;
  1189. }
  1190. dev_info(dev, "qmgr start queue %d, number of queues %d\n",
  1191. qmgr->start_queue, qmgr->num_queues);
  1192. qmgr->reg_peek =
  1193. knav_queue_map_reg(kdev, child,
  1194. KNAV_QUEUE_PEEK_REG_INDEX);
  1195. qmgr->reg_status =
  1196. knav_queue_map_reg(kdev, child,
  1197. KNAV_QUEUE_STATUS_REG_INDEX);
  1198. qmgr->reg_config =
  1199. knav_queue_map_reg(kdev, child,
  1200. KNAV_QUEUE_CONFIG_REG_INDEX);
  1201. qmgr->reg_region =
  1202. knav_queue_map_reg(kdev, child,
  1203. KNAV_QUEUE_REGION_REG_INDEX);
  1204. qmgr->reg_push =
  1205. knav_queue_map_reg(kdev, child,
  1206. KNAV_QUEUE_PUSH_REG_INDEX);
  1207. qmgr->reg_pop =
  1208. knav_queue_map_reg(kdev, child,
  1209. KNAV_QUEUE_POP_REG_INDEX);
  1210. if (IS_ERR(qmgr->reg_peek) || IS_ERR(qmgr->reg_status) ||
  1211. IS_ERR(qmgr->reg_config) || IS_ERR(qmgr->reg_region) ||
  1212. IS_ERR(qmgr->reg_push) || IS_ERR(qmgr->reg_pop)) {
  1213. dev_err(dev, "failed to map qmgr regs\n");
  1214. if (!IS_ERR(qmgr->reg_peek))
  1215. devm_iounmap(dev, qmgr->reg_peek);
  1216. if (!IS_ERR(qmgr->reg_status))
  1217. devm_iounmap(dev, qmgr->reg_status);
  1218. if (!IS_ERR(qmgr->reg_config))
  1219. devm_iounmap(dev, qmgr->reg_config);
  1220. if (!IS_ERR(qmgr->reg_region))
  1221. devm_iounmap(dev, qmgr->reg_region);
  1222. if (!IS_ERR(qmgr->reg_push))
  1223. devm_iounmap(dev, qmgr->reg_push);
  1224. if (!IS_ERR(qmgr->reg_pop))
  1225. devm_iounmap(dev, qmgr->reg_pop);
  1226. devm_kfree(dev, qmgr);
  1227. continue;
  1228. }
  1229. list_add_tail(&qmgr->list, &kdev->qmgrs);
  1230. 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",
  1231. qmgr->start_queue, qmgr->num_queues,
  1232. qmgr->reg_peek, qmgr->reg_status,
  1233. qmgr->reg_config, qmgr->reg_region,
  1234. qmgr->reg_push, qmgr->reg_pop);
  1235. }
  1236. return 0;
  1237. }
  1238. static int knav_queue_init_pdsps(struct knav_device *kdev,
  1239. struct device_node *pdsps)
  1240. {
  1241. struct device *dev = kdev->dev;
  1242. struct knav_pdsp_info *pdsp;
  1243. struct device_node *child;
  1244. for_each_child_of_node(pdsps, child) {
  1245. pdsp = devm_kzalloc(dev, sizeof(*pdsp), GFP_KERNEL);
  1246. if (!pdsp) {
  1247. dev_err(dev, "out of memory allocating pdsp\n");
  1248. return -ENOMEM;
  1249. }
  1250. pdsp->name = knav_queue_find_name(child);
  1251. pdsp->iram =
  1252. knav_queue_map_reg(kdev, child,
  1253. KNAV_QUEUE_PDSP_IRAM_REG_INDEX);
  1254. pdsp->regs =
  1255. knav_queue_map_reg(kdev, child,
  1256. KNAV_QUEUE_PDSP_REGS_REG_INDEX);
  1257. pdsp->intd =
  1258. knav_queue_map_reg(kdev, child,
  1259. KNAV_QUEUE_PDSP_INTD_REG_INDEX);
  1260. pdsp->command =
  1261. knav_queue_map_reg(kdev, child,
  1262. KNAV_QUEUE_PDSP_CMD_REG_INDEX);
  1263. if (IS_ERR(pdsp->command) || IS_ERR(pdsp->iram) ||
  1264. IS_ERR(pdsp->regs) || IS_ERR(pdsp->intd)) {
  1265. dev_err(dev, "failed to map pdsp %s regs\n",
  1266. pdsp->name);
  1267. if (!IS_ERR(pdsp->command))
  1268. devm_iounmap(dev, pdsp->command);
  1269. if (!IS_ERR(pdsp->iram))
  1270. devm_iounmap(dev, pdsp->iram);
  1271. if (!IS_ERR(pdsp->regs))
  1272. devm_iounmap(dev, pdsp->regs);
  1273. if (!IS_ERR(pdsp->intd))
  1274. devm_iounmap(dev, pdsp->intd);
  1275. devm_kfree(dev, pdsp);
  1276. continue;
  1277. }
  1278. of_property_read_u32(child, "id", &pdsp->id);
  1279. list_add_tail(&pdsp->list, &kdev->pdsps);
  1280. dev_dbg(dev, "added pdsp %s: command %p, iram %p, regs %p, intd %p\n",
  1281. pdsp->name, pdsp->command, pdsp->iram, pdsp->regs,
  1282. pdsp->intd);
  1283. }
  1284. return 0;
  1285. }
  1286. static int knav_queue_stop_pdsp(struct knav_device *kdev,
  1287. struct knav_pdsp_info *pdsp)
  1288. {
  1289. u32 val, timeout = 1000;
  1290. int ret;
  1291. val = readl_relaxed(&pdsp->regs->control) & ~PDSP_CTRL_ENABLE;
  1292. writel_relaxed(val, &pdsp->regs->control);
  1293. ret = knav_queue_pdsp_wait(&pdsp->regs->control, timeout,
  1294. PDSP_CTRL_RUNNING);
  1295. if (ret < 0) {
  1296. dev_err(kdev->dev, "timed out on pdsp %s stop\n", pdsp->name);
  1297. return ret;
  1298. }
  1299. pdsp->loaded = false;
  1300. pdsp->started = false;
  1301. return 0;
  1302. }
  1303. static int knav_queue_load_pdsp(struct knav_device *kdev,
  1304. struct knav_pdsp_info *pdsp)
  1305. {
  1306. int i, ret, fwlen;
  1307. const struct firmware *fw;
  1308. bool found = false;
  1309. u32 *fwdata;
  1310. for (i = 0; i < ARRAY_SIZE(knav_acc_firmwares); i++) {
  1311. if (knav_acc_firmwares[i]) {
  1312. ret = request_firmware_direct(&fw,
  1313. knav_acc_firmwares[i],
  1314. kdev->dev);
  1315. if (!ret) {
  1316. found = true;
  1317. break;
  1318. }
  1319. }
  1320. }
  1321. if (!found) {
  1322. dev_err(kdev->dev, "failed to get firmware for pdsp\n");
  1323. return -ENODEV;
  1324. }
  1325. dev_info(kdev->dev, "firmware file %s downloaded for PDSP\n",
  1326. knav_acc_firmwares[i]);
  1327. writel_relaxed(pdsp->id + 1, pdsp->command + 0x18);
  1328. /* download the firmware */
  1329. fwdata = (u32 *)fw->data;
  1330. fwlen = (fw->size + sizeof(u32) - 1) / sizeof(u32);
  1331. for (i = 0; i < fwlen; i++)
  1332. writel_relaxed(be32_to_cpu(fwdata[i]), pdsp->iram + i);
  1333. release_firmware(fw);
  1334. return 0;
  1335. }
  1336. static int knav_queue_start_pdsp(struct knav_device *kdev,
  1337. struct knav_pdsp_info *pdsp)
  1338. {
  1339. u32 val, timeout = 1000;
  1340. int ret;
  1341. /* write a command for sync */
  1342. writel_relaxed(0xffffffff, pdsp->command);
  1343. while (readl_relaxed(pdsp->command) != 0xffffffff)
  1344. cpu_relax();
  1345. /* soft reset the PDSP */
  1346. val = readl_relaxed(&pdsp->regs->control);
  1347. val &= ~(PDSP_CTRL_PC_MASK | PDSP_CTRL_SOFT_RESET);
  1348. writel_relaxed(val, &pdsp->regs->control);
  1349. /* enable pdsp */
  1350. val = readl_relaxed(&pdsp->regs->control) | PDSP_CTRL_ENABLE;
  1351. writel_relaxed(val, &pdsp->regs->control);
  1352. /* wait for command register to clear */
  1353. ret = knav_queue_pdsp_wait(pdsp->command, timeout, 0);
  1354. if (ret < 0) {
  1355. dev_err(kdev->dev,
  1356. "timed out on pdsp %s command register wait\n",
  1357. pdsp->name);
  1358. return ret;
  1359. }
  1360. return 0;
  1361. }
  1362. static void knav_queue_stop_pdsps(struct knav_device *kdev)
  1363. {
  1364. struct knav_pdsp_info *pdsp;
  1365. /* disable all pdsps */
  1366. for_each_pdsp(kdev, pdsp)
  1367. knav_queue_stop_pdsp(kdev, pdsp);
  1368. }
  1369. static int knav_queue_start_pdsps(struct knav_device *kdev)
  1370. {
  1371. struct knav_pdsp_info *pdsp;
  1372. int ret;
  1373. knav_queue_stop_pdsps(kdev);
  1374. /* now load them all. We return success even if pdsp
  1375. * is not loaded as acc channels are optional on having
  1376. * firmware availability in the system. We set the loaded
  1377. * and stated flag and when initialize the acc range, check
  1378. * it and init the range only if pdsp is started.
  1379. */
  1380. for_each_pdsp(kdev, pdsp) {
  1381. ret = knav_queue_load_pdsp(kdev, pdsp);
  1382. if (!ret)
  1383. pdsp->loaded = true;
  1384. }
  1385. for_each_pdsp(kdev, pdsp) {
  1386. if (pdsp->loaded) {
  1387. ret = knav_queue_start_pdsp(kdev, pdsp);
  1388. if (!ret)
  1389. pdsp->started = true;
  1390. }
  1391. }
  1392. return 0;
  1393. }
  1394. static inline struct knav_qmgr_info *knav_find_qmgr(unsigned id)
  1395. {
  1396. struct knav_qmgr_info *qmgr;
  1397. for_each_qmgr(kdev, qmgr) {
  1398. if ((id >= qmgr->start_queue) &&
  1399. (id < qmgr->start_queue + qmgr->num_queues))
  1400. return qmgr;
  1401. }
  1402. return NULL;
  1403. }
  1404. static int knav_queue_init_queue(struct knav_device *kdev,
  1405. struct knav_range_info *range,
  1406. struct knav_queue_inst *inst,
  1407. unsigned id)
  1408. {
  1409. char irq_name[KNAV_NAME_SIZE];
  1410. inst->qmgr = knav_find_qmgr(id);
  1411. if (!inst->qmgr)
  1412. return -1;
  1413. INIT_LIST_HEAD(&inst->handles);
  1414. inst->kdev = kdev;
  1415. inst->range = range;
  1416. inst->irq_num = -1;
  1417. inst->id = id;
  1418. scnprintf(irq_name, sizeof(irq_name), "hwqueue-%d", id);
  1419. inst->irq_name = kstrndup(irq_name, sizeof(irq_name), GFP_KERNEL);
  1420. if (range->ops && range->ops->init_queue)
  1421. return range->ops->init_queue(range, inst);
  1422. else
  1423. return 0;
  1424. }
  1425. static int knav_queue_init_queues(struct knav_device *kdev)
  1426. {
  1427. struct knav_range_info *range;
  1428. int size, id, base_idx;
  1429. int idx = 0, ret = 0;
  1430. /* how much do we need for instance data? */
  1431. size = sizeof(struct knav_queue_inst);
  1432. /* round this up to a power of 2, keep the index to instance
  1433. * arithmetic fast.
  1434. * */
  1435. kdev->inst_shift = order_base_2(size);
  1436. size = (1 << kdev->inst_shift) * kdev->num_queues_in_use;
  1437. kdev->instances = devm_kzalloc(kdev->dev, size, GFP_KERNEL);
  1438. if (!kdev->instances)
  1439. return -ENOMEM;
  1440. for_each_queue_range(kdev, range) {
  1441. if (range->ops && range->ops->init_range)
  1442. range->ops->init_range(range);
  1443. base_idx = idx;
  1444. for (id = range->queue_base;
  1445. id < range->queue_base + range->num_queues; id++, idx++) {
  1446. ret = knav_queue_init_queue(kdev, range,
  1447. knav_queue_idx_to_inst(kdev, idx), id);
  1448. if (ret < 0)
  1449. return ret;
  1450. }
  1451. range->queue_base_inst =
  1452. knav_queue_idx_to_inst(kdev, base_idx);
  1453. }
  1454. return 0;
  1455. }
  1456. static int knav_queue_probe(struct platform_device *pdev)
  1457. {
  1458. struct device_node *node = pdev->dev.of_node;
  1459. struct device_node *qmgrs, *queue_pools, *regions, *pdsps;
  1460. struct device *dev = &pdev->dev;
  1461. u32 temp[2];
  1462. int ret;
  1463. if (!node) {
  1464. dev_err(dev, "device tree info unavailable\n");
  1465. return -ENODEV;
  1466. }
  1467. kdev = devm_kzalloc(dev, sizeof(struct knav_device), GFP_KERNEL);
  1468. if (!kdev) {
  1469. dev_err(dev, "memory allocation failed\n");
  1470. return -ENOMEM;
  1471. }
  1472. platform_set_drvdata(pdev, kdev);
  1473. kdev->dev = dev;
  1474. INIT_LIST_HEAD(&kdev->queue_ranges);
  1475. INIT_LIST_HEAD(&kdev->qmgrs);
  1476. INIT_LIST_HEAD(&kdev->pools);
  1477. INIT_LIST_HEAD(&kdev->regions);
  1478. INIT_LIST_HEAD(&kdev->pdsps);
  1479. pm_runtime_enable(&pdev->dev);
  1480. ret = pm_runtime_get_sync(&pdev->dev);
  1481. if (ret < 0) {
  1482. dev_err(dev, "Failed to enable QMSS\n");
  1483. return ret;
  1484. }
  1485. if (of_property_read_u32_array(node, "queue-range", temp, 2)) {
  1486. dev_err(dev, "queue-range not specified\n");
  1487. ret = -ENODEV;
  1488. goto err;
  1489. }
  1490. kdev->base_id = temp[0];
  1491. kdev->num_queues = temp[1];
  1492. /* Initialize queue managers using device tree configuration */
  1493. qmgrs = of_get_child_by_name(node, "qmgrs");
  1494. if (!qmgrs) {
  1495. dev_err(dev, "queue manager info not specified\n");
  1496. ret = -ENODEV;
  1497. goto err;
  1498. }
  1499. ret = knav_queue_init_qmgrs(kdev, qmgrs);
  1500. of_node_put(qmgrs);
  1501. if (ret)
  1502. goto err;
  1503. /* get pdsp configuration values from device tree */
  1504. pdsps = of_get_child_by_name(node, "pdsps");
  1505. if (pdsps) {
  1506. ret = knav_queue_init_pdsps(kdev, pdsps);
  1507. if (ret)
  1508. goto err;
  1509. ret = knav_queue_start_pdsps(kdev);
  1510. if (ret)
  1511. goto err;
  1512. }
  1513. of_node_put(pdsps);
  1514. /* get usable queue range values from device tree */
  1515. queue_pools = of_get_child_by_name(node, "queue-pools");
  1516. if (!queue_pools) {
  1517. dev_err(dev, "queue-pools not specified\n");
  1518. ret = -ENODEV;
  1519. goto err;
  1520. }
  1521. ret = knav_setup_queue_pools(kdev, queue_pools);
  1522. of_node_put(queue_pools);
  1523. if (ret)
  1524. goto err;
  1525. ret = knav_get_link_ram(kdev, "linkram0", &kdev->link_rams[0]);
  1526. if (ret) {
  1527. dev_err(kdev->dev, "could not setup linking ram\n");
  1528. goto err;
  1529. }
  1530. ret = knav_get_link_ram(kdev, "linkram1", &kdev->link_rams[1]);
  1531. if (ret) {
  1532. /*
  1533. * nothing really, we have one linking ram already, so we just
  1534. * live within our means
  1535. */
  1536. }
  1537. ret = knav_queue_setup_link_ram(kdev);
  1538. if (ret)
  1539. goto err;
  1540. regions = of_get_child_by_name(node, "descriptor-regions");
  1541. if (!regions) {
  1542. dev_err(dev, "descriptor-regions not specified\n");
  1543. goto err;
  1544. }
  1545. ret = knav_queue_setup_regions(kdev, regions);
  1546. of_node_put(regions);
  1547. if (ret)
  1548. goto err;
  1549. ret = knav_queue_init_queues(kdev);
  1550. if (ret < 0) {
  1551. dev_err(dev, "hwqueue initialization failed\n");
  1552. goto err;
  1553. }
  1554. debugfs_create_file("qmss", S_IFREG | S_IRUGO, NULL, NULL,
  1555. &knav_queue_debug_ops);
  1556. return 0;
  1557. err:
  1558. knav_queue_stop_pdsps(kdev);
  1559. knav_queue_free_regions(kdev);
  1560. knav_free_queue_ranges(kdev);
  1561. pm_runtime_put_sync(&pdev->dev);
  1562. pm_runtime_disable(&pdev->dev);
  1563. return ret;
  1564. }
  1565. static int knav_queue_remove(struct platform_device *pdev)
  1566. {
  1567. /* TODO: Free resources */
  1568. pm_runtime_put_sync(&pdev->dev);
  1569. pm_runtime_disable(&pdev->dev);
  1570. return 0;
  1571. }
  1572. /* Match table for of_platform binding */
  1573. static struct of_device_id keystone_qmss_of_match[] = {
  1574. { .compatible = "ti,keystone-navigator-qmss", },
  1575. {},
  1576. };
  1577. MODULE_DEVICE_TABLE(of, keystone_qmss_of_match);
  1578. static struct platform_driver keystone_qmss_driver = {
  1579. .probe = knav_queue_probe,
  1580. .remove = knav_queue_remove,
  1581. .driver = {
  1582. .name = "keystone-navigator-qmss",
  1583. .of_match_table = keystone_qmss_of_match,
  1584. },
  1585. };
  1586. module_platform_driver(keystone_qmss_driver);
  1587. MODULE_LICENSE("GPL v2");
  1588. MODULE_DESCRIPTION("TI QMSS driver for Keystone SOCs");
  1589. MODULE_AUTHOR("Sandeep Nair <sandeep_n@ti.com>");
  1590. MODULE_AUTHOR("Santosh Shilimkar <santosh.shilimkar@ti.com>");