sps_dma.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2011-2013, 2015, 2017-2019, 2021, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2022 Qualcomm Innovation Center, Inc. All rights reserved.
  5. */
  6. /* BAM-DMA Manager. */
  7. #ifdef CONFIG_SPS_SUPPORT_BAMDMA
  8. #include <linux/export.h>
  9. #include <linux/memory.h>
  10. #include "spsi.h"
  11. #include "bam.h"
  12. #include "sps_bam.h"
  13. #include "sps_core.h"
  14. /**
  15. * registers
  16. */
  17. #define DMA_ENBL (0x00000000)
  18. #ifdef CONFIG_SPS_SUPPORT_NDP_BAM
  19. #define DMA_REVISION (0x00000004)
  20. #define DMA_CONFIG (0x00000008)
  21. #define DMA_CHNL_CONFIG(n) (0x00001000 + 4096 * (n))
  22. #else
  23. #define DMA_CHNL_CONFIG(n) (0x00000004 + 4 * (n))
  24. #define DMA_CONFIG (0x00000040)
  25. #endif
  26. /**
  27. * masks
  28. */
  29. /* DMA_CHNL_confign */
  30. #ifdef CONFIG_SPS_SUPPORT_NDP_BAM
  31. #define DMA_CHNL_PRODUCER_PIPE_ENABLED 0x40000
  32. #define DMA_CHNL_CONSUMER_PIPE_ENABLED 0x20000
  33. #endif
  34. #define DMA_CHNL_HALT_DONE 0x10000
  35. #define DMA_CHNL_HALT 0x1000
  36. #define DMA_CHNL_ENABLE 0x100
  37. #define DMA_CHNL_ACT_THRESH 0x30
  38. #define DMA_CHNL_WEIGHT 0x7
  39. /* DMA_CONFIG */
  40. #define TESTBUS_SELECT 0x3
  41. /**
  42. *
  43. * Write register with debug info.
  44. *
  45. * @base - bam base virtual address.
  46. * @offset - register offset.
  47. * @val - value to write.
  48. *
  49. */
  50. static inline void dma_write_reg(void *base, u32 offset, u32 val)
  51. {
  52. iowrite32(val, base + offset);
  53. SPS_DBG(sps, "sps:bamdma: write reg 0x%x w_val 0x%x\n", offset, val);
  54. }
  55. /**
  56. * Write register masked field with debug info.
  57. *
  58. * @base - bam base virtual address.
  59. * @offset - register offset.
  60. * @mask - register bitmask.
  61. * @val - value to write.
  62. *
  63. */
  64. static inline void dma_write_reg_field(void *base, u32 offset,
  65. const u32 mask, u32 val)
  66. {
  67. unsigned long lmask = mask;
  68. u32 shift = find_first_bit(&lmask, 32);
  69. u32 tmp = ioread32(base + offset);
  70. tmp &= ~mask; /* clear written bits */
  71. val = tmp | (val << shift);
  72. iowrite32(val, base + offset);
  73. SPS_DBG(sps, "sps:bamdma: write reg 0x%x w_val 0x%x\n", offset, val);
  74. }
  75. /* Round max number of pipes to nearest multiple of 2 */
  76. #define DMA_MAX_PIPES ((BAM_MAX_PIPES / 2) * 2)
  77. /* Maximum number of BAM-DMAs supported */
  78. #define MAX_BAM_DMA_DEVICES 1
  79. /* Maximum number of BAMs that will be registered */
  80. #define MAX_BAM_DMA_BAMS 1
  81. /* Pipe enable check values */
  82. #define DMA_PIPES_STATE_DIFF 0
  83. #define DMA_PIPES_BOTH_DISABLED 1
  84. #define DMA_PIPES_BOTH_ENABLED 2
  85. /* Even pipe is tx/dest/input/write, odd pipe is rx/src/output/read */
  86. #define DMA_PIPE_IS_DEST(p) (((p) & 1) == 0)
  87. #define DMA_PIPE_IS_SRC(p) (((p) & 1) != 0)
  88. /* BAM DMA pipe state */
  89. enum bamdma_pipe_state {
  90. PIPE_INACTIVE = 0,
  91. PIPE_ACTIVE
  92. };
  93. /* BAM DMA channel state */
  94. enum bamdma_chan_state {
  95. DMA_CHAN_STATE_FREE = 0,
  96. DMA_CHAN_STATE_ALLOC_EXT, /* Client allocation */
  97. DMA_CHAN_STATE_ALLOC_INT /* Internal (resource mgr) allocation */
  98. };
  99. struct bamdma_chan {
  100. /* Allocation state */
  101. enum bamdma_chan_state state;
  102. /* BAM DMA channel configuration parameters */
  103. u32 threshold;
  104. enum sps_dma_priority priority;
  105. /* HWIO channel configuration parameters */
  106. enum bam_dma_thresh_dma thresh;
  107. enum bam_dma_weight_dma weight;
  108. };
  109. /* BAM DMA device state */
  110. struct bamdma_device {
  111. /* BAM-DMA device state */
  112. int enabled;
  113. int local;
  114. /* BAM device state */
  115. struct sps_bam *bam;
  116. /* BAM handle, for deregistration */
  117. unsigned long h;
  118. /* BAM DMA device virtual mapping */
  119. void *virt_addr;
  120. int virtual_mapped;
  121. phys_addr_t phys_addr;
  122. void *hwio;
  123. /* BAM DMA pipe/channel state */
  124. u32 num_pipes;
  125. enum bamdma_pipe_state pipes[DMA_MAX_PIPES];
  126. struct bamdma_chan chans[DMA_MAX_PIPES / 2];
  127. };
  128. /* BAM-DMA devices */
  129. static struct bamdma_device bam_dma_dev[MAX_BAM_DMA_DEVICES];
  130. static struct mutex bam_dma_lock;
  131. /*
  132. * The BAM DMA module registers all BAMs in the BSP properties, but only
  133. * uses the first BAM-DMA device for allocations. References to the others
  134. * are stored in the following data array.
  135. */
  136. static int num_bams;
  137. static unsigned long bam_handles[MAX_BAM_DMA_BAMS];
  138. /**
  139. * Find BAM-DMA device
  140. *
  141. * This function finds the BAM-DMA device associated with the BAM handle.
  142. *
  143. * @h - BAM handle
  144. *
  145. * @return - pointer to BAM-DMA device, or NULL on error
  146. *
  147. */
  148. static struct bamdma_device *sps_dma_find_device(unsigned long h)
  149. {
  150. return &bam_dma_dev[0];
  151. }
  152. /**
  153. * BAM DMA device enable
  154. *
  155. * This function enables a BAM DMA device and the associated BAM.
  156. *
  157. * @dev - pointer to BAM DMA device context
  158. *
  159. * @return 0 on success, negative value on error
  160. *
  161. */
  162. static int sps_dma_device_enable(struct bamdma_device *dev)
  163. {
  164. if (dev->enabled)
  165. return 0;
  166. /*
  167. * If the BAM-DMA device is locally controlled then enable BAM-DMA
  168. * device
  169. */
  170. if (dev->local)
  171. dma_write_reg(dev->virt_addr, DMA_ENBL, 1);
  172. /* Enable BAM device */
  173. if (sps_bam_enable(dev->bam)) {
  174. SPS_ERR(sps, "sps:Failed to enable BAM DMA's BAM: %pa\n",
  175. &dev->phys_addr);
  176. return SPS_ERROR;
  177. }
  178. dev->enabled = true;
  179. return 0;
  180. }
  181. /**
  182. * BAM DMA device enable
  183. *
  184. * This function initializes a BAM DMA device.
  185. *
  186. * @dev - pointer to BAM DMA device context
  187. *
  188. * @return 0 on success, negative value on error
  189. *
  190. */
  191. static int sps_dma_device_disable(struct bamdma_device *dev)
  192. {
  193. u32 pipe_index;
  194. if (!dev->enabled)
  195. return 0;
  196. /* Do not disable if channels active */
  197. for (pipe_index = 0; pipe_index < dev->num_pipes; pipe_index++) {
  198. if (dev->pipes[pipe_index] != PIPE_INACTIVE)
  199. break;
  200. }
  201. if (pipe_index < dev->num_pipes) {
  202. SPS_ERR(sps,
  203. "sps:Fail to disable BAM-DMA %pa:channels are active\n",
  204. &dev->phys_addr);
  205. return SPS_ERROR;
  206. }
  207. dev->enabled = false;
  208. /* Disable BAM device */
  209. if (sps_bam_disable(dev->bam)) {
  210. SPS_ERR(sps,
  211. "sps:Fail to disable BAM-DMA BAM:%pa\n",
  212. &dev->phys_addr);
  213. return SPS_ERROR;
  214. }
  215. /* Is the BAM-DMA device locally controlled? */
  216. if (dev->local)
  217. /* Disable BAM-DMA device */
  218. dma_write_reg(dev->virt_addr, DMA_ENBL, 0);
  219. return 0;
  220. }
  221. /**
  222. * Initialize BAM DMA device
  223. *
  224. */
  225. int sps_dma_device_init(unsigned long h)
  226. {
  227. struct bamdma_device *dev;
  228. struct sps_bam_props *props;
  229. int result = SPS_ERROR;
  230. mutex_lock(&bam_dma_lock);
  231. /* Find a free BAM-DMA device slot */
  232. dev = NULL;
  233. if (bam_dma_dev[0].bam != NULL) {
  234. SPS_ERR(sps,
  235. "sps:%s:BAM-DMA BAM device is already initialized\n",
  236. __func__);
  237. goto exit_err;
  238. } else {
  239. dev = &bam_dma_dev[0];
  240. }
  241. /* Record BAM */
  242. memset(dev, 0, sizeof(*dev));
  243. dev->h = h;
  244. dev->bam = sps_h2bam(h);
  245. if (dev->bam == NULL) {
  246. SPS_ERR(sps,
  247. "sps:%s:BAM-DMA BAM device is not found from the handle\n",
  248. __func__);
  249. goto exit_err;
  250. }
  251. /* Map the BAM DMA device into virtual space, if necessary */
  252. props = &dev->bam->props;
  253. dev->phys_addr = props->periph_phys_addr;
  254. if (props->periph_virt_addr != NULL) {
  255. dev->virt_addr = props->periph_virt_addr;
  256. dev->virtual_mapped = false;
  257. } else {
  258. if (props->periph_virt_size == 0) {
  259. SPS_ERR(sps,
  260. "sps:Unable to map BAM DMA IO memory: %pa %x\n",
  261. &dev->phys_addr, props->periph_virt_size);
  262. goto exit_err;
  263. }
  264. dev->virt_addr = ioremap(dev->phys_addr,
  265. props->periph_virt_size);
  266. if (dev->virt_addr == NULL) {
  267. SPS_ERR(sps,
  268. "sps:Unable to map BAM DMA IO memory: %pa %x\n",
  269. &dev->phys_addr, props->periph_virt_size);
  270. goto exit_err;
  271. }
  272. dev->virtual_mapped = true;
  273. }
  274. dev->hwio = (void *) dev->virt_addr;
  275. /* Is the BAM-DMA device locally controlled? */
  276. if ((props->manage & SPS_BAM_MGR_DEVICE_REMOTE) == 0) {
  277. SPS_DBG3(sps, "sps:BAM-DMA is controlled locally: %pa\n",
  278. &dev->phys_addr);
  279. dev->local = true;
  280. } else {
  281. SPS_DBG3(sps, "sps:BAM-DMA is controlled remotely: %pa\n",
  282. &dev->phys_addr);
  283. dev->local = false;
  284. }
  285. /*
  286. * Enable the BAM DMA and determine the number of pipes/channels.
  287. * Leave the BAM-DMA enabled, since it is always a shared device.
  288. */
  289. if (sps_dma_device_enable(dev))
  290. goto exit_err;
  291. dev->num_pipes = dev->bam->props.num_pipes;
  292. result = 0;
  293. exit_err:
  294. if (result) {
  295. if (dev != NULL) {
  296. if (dev->virtual_mapped)
  297. iounmap(dev->virt_addr);
  298. dev->bam = NULL;
  299. }
  300. }
  301. mutex_unlock(&bam_dma_lock);
  302. return result;
  303. }
  304. /**
  305. * De-initialize BAM DMA device
  306. *
  307. */
  308. int sps_dma_device_de_init(unsigned long h)
  309. {
  310. struct bamdma_device *dev;
  311. u32 pipe_index;
  312. u32 chan;
  313. int result = 0;
  314. mutex_lock(&bam_dma_lock);
  315. dev = sps_dma_find_device(h);
  316. if (dev == NULL) {
  317. SPS_ERR(sps, "sps:BAM-DMA: not registered: %pK\n", (void *)h);
  318. result = SPS_ERROR;
  319. goto exit_err;
  320. }
  321. /* Check for channel leaks */
  322. for (chan = 0; chan < dev->num_pipes / 2; chan++) {
  323. if (dev->chans[chan].state != DMA_CHAN_STATE_FREE) {
  324. SPS_ERR(sps, "sps:BAM-DMA: channel not free: %d\n",
  325. chan);
  326. result = SPS_ERROR;
  327. dev->chans[chan].state = DMA_CHAN_STATE_FREE;
  328. }
  329. }
  330. for (pipe_index = 0; pipe_index < dev->num_pipes; pipe_index++) {
  331. if (dev->pipes[pipe_index] != PIPE_INACTIVE) {
  332. SPS_ERR(sps, "sps:BAM-DMA: pipe not inactive: %d\n",
  333. pipe_index);
  334. result = SPS_ERROR;
  335. dev->pipes[pipe_index] = PIPE_INACTIVE;
  336. }
  337. }
  338. /* Disable BAM and BAM-DMA */
  339. if (sps_dma_device_disable(dev))
  340. result = SPS_ERROR;
  341. dev->h = BAM_HANDLE_INVALID;
  342. dev->bam = NULL;
  343. if (dev->virtual_mapped)
  344. iounmap(dev->virt_addr);
  345. exit_err:
  346. mutex_unlock(&bam_dma_lock);
  347. return result;
  348. }
  349. /**
  350. * Initialize BAM DMA module
  351. *
  352. */
  353. int sps_dma_init(const struct sps_bam_props *bam_props)
  354. {
  355. struct sps_bam_props props;
  356. const struct sps_bam_props *bam_reg;
  357. unsigned long h;
  358. /* Init local data */
  359. memset(&bam_dma_dev, 0, sizeof(bam_dma_dev));
  360. num_bams = 0;
  361. memset(bam_handles, 0, sizeof(bam_handles));
  362. /* Create a mutex to control access to the BAM-DMA devices */
  363. mutex_init(&bam_dma_lock);
  364. /* Are there any BAM DMA devices? */
  365. if (bam_props == NULL)
  366. return 0;
  367. /*
  368. * Registers all BAMs in the BSP properties, but only uses the first
  369. * BAM-DMA device for allocations.
  370. */
  371. if (bam_props->phys_addr) {
  372. /* Force multi-EE option for all BAM-DMAs */
  373. bam_reg = bam_props;
  374. if ((bam_props->options & SPS_BAM_OPT_BAMDMA) &&
  375. (bam_props->manage & SPS_BAM_MGR_MULTI_EE) == 0) {
  376. SPS_DBG(sps,
  377. "sps:Setting multi-EE options for BAM-DMA: %pa\n",
  378. &bam_props->phys_addr);
  379. props = *bam_props;
  380. props.manage |= SPS_BAM_MGR_MULTI_EE;
  381. bam_reg = &props;
  382. }
  383. /* Register the BAM */
  384. if (sps_register_bam_device(bam_reg, &h)) {
  385. SPS_ERR(sps,
  386. "sps:Fail to register BAM-DMA BAM device: phys %pa\n",
  387. &bam_props->phys_addr);
  388. return SPS_ERROR;
  389. }
  390. /* Record the BAM so that it may be deregistered later */
  391. if (num_bams < MAX_BAM_DMA_BAMS) {
  392. bam_handles[num_bams] = h;
  393. num_bams++;
  394. } else {
  395. SPS_ERR(sps, "sps:BAM-DMA: BAM limit exceeded: %d\n",
  396. num_bams);
  397. return SPS_ERROR;
  398. }
  399. } else {
  400. SPS_ERR(sps,
  401. "sps:%s:BAM-DMA phys_addr is zero\n",
  402. __func__);
  403. return SPS_ERROR;
  404. }
  405. return 0;
  406. }
  407. /**
  408. * De-initialize BAM DMA module
  409. *
  410. */
  411. void sps_dma_de_init(void)
  412. {
  413. int n;
  414. /* De-initialize the BAM devices */
  415. for (n = 0; n < num_bams; n++)
  416. sps_deregister_bam_device(bam_handles[n]);
  417. /* Clear local data */
  418. memset(&bam_dma_dev, 0, sizeof(bam_dma_dev));
  419. num_bams = 0;
  420. memset(bam_handles, 0, sizeof(bam_handles));
  421. }
  422. /**
  423. * Allocate a BAM DMA channel
  424. *
  425. */
  426. int sps_alloc_dma_chan(const struct sps_alloc_dma_chan *alloc,
  427. struct sps_dma_chan *chan_info)
  428. {
  429. struct bamdma_device *dev;
  430. struct bamdma_chan *chan;
  431. u32 pipe_index;
  432. enum bam_dma_thresh_dma thresh = (enum bam_dma_thresh_dma) 0;
  433. enum bam_dma_weight_dma weight = (enum bam_dma_weight_dma) 0;
  434. int result = SPS_ERROR;
  435. if (alloc == NULL || chan_info == NULL) {
  436. SPS_ERR(sps,
  437. "sps:%s:invalid parameters\n", __func__);
  438. return SPS_ERROR;
  439. }
  440. /* Translate threshold and priority to hwio values */
  441. if (alloc->threshold != SPS_DMA_THRESHOLD_DEFAULT) {
  442. if (alloc->threshold >= 512)
  443. thresh = BAM_DMA_THRESH_512;
  444. else if (alloc->threshold >= 256)
  445. thresh = BAM_DMA_THRESH_256;
  446. else if (alloc->threshold >= 128)
  447. thresh = BAM_DMA_THRESH_128;
  448. else
  449. thresh = BAM_DMA_THRESH_64;
  450. }
  451. weight = alloc->priority;
  452. if ((u32)alloc->priority > (u32)BAM_DMA_WEIGHT_HIGH) {
  453. SPS_ERR(sps, "sps:BAM-DMA: invalid priority: %x\n",
  454. alloc->priority);
  455. return SPS_ERROR;
  456. }
  457. mutex_lock(&bam_dma_lock);
  458. dev = sps_dma_find_device(alloc->dev);
  459. if (dev == NULL) {
  460. SPS_ERR(sps, "sps:BAM-DMA: invalid BAM handle: %pK\n",
  461. (void *)alloc->dev);
  462. goto exit_err;
  463. }
  464. /* Search for a free set of pipes */
  465. for (pipe_index = 0, chan = dev->chans;
  466. pipe_index < dev->num_pipes; pipe_index += 2, chan++) {
  467. if (chan->state == DMA_CHAN_STATE_FREE) {
  468. /* Just check pipes for safety */
  469. if (dev->pipes[pipe_index] != PIPE_INACTIVE ||
  470. dev->pipes[pipe_index + 1] != PIPE_INACTIVE) {
  471. SPS_ERR(sps,
  472. "sps:BAM-DMA: channel %d state error:%d %d\n",
  473. pipe_index / 2, dev->pipes[pipe_index],
  474. dev->pipes[pipe_index + 1]);
  475. goto exit_err;
  476. }
  477. break; /* Found free pipe */
  478. }
  479. }
  480. if (pipe_index >= dev->num_pipes) {
  481. SPS_ERR(sps, "sps:BAM-DMA: no free channel num_pipes = %d\n",
  482. dev->num_pipes);
  483. goto exit_err;
  484. }
  485. chan->state = DMA_CHAN_STATE_ALLOC_EXT;
  486. /* Store config values for use when pipes are activated */
  487. chan = &dev->chans[pipe_index / 2];
  488. chan->threshold = alloc->threshold;
  489. chan->thresh = thresh;
  490. chan->priority = alloc->priority;
  491. chan->weight = weight;
  492. SPS_DBG3(sps, "sps:%s. pipe %d\n", __func__, pipe_index);
  493. /* Report allocated pipes to client */
  494. chan_info->dev = dev->h;
  495. /* Dest/input/write pipex */
  496. chan_info->dest_pipe_index = pipe_index;
  497. /* Source/output/read pipe */
  498. chan_info->src_pipe_index = pipe_index + 1;
  499. result = 0;
  500. exit_err:
  501. mutex_unlock(&bam_dma_lock);
  502. return result;
  503. }
  504. EXPORT_SYMBOL(sps_alloc_dma_chan);
  505. /**
  506. * Free a BAM DMA channel
  507. *
  508. */
  509. int sps_free_dma_chan(struct sps_dma_chan *chan)
  510. {
  511. struct bamdma_device *dev;
  512. u32 pipe_index;
  513. int result = 0;
  514. if (chan == NULL) {
  515. SPS_ERR(sps,
  516. "sps:%s:chan is NULL\n", __func__);
  517. return SPS_ERROR;
  518. }
  519. mutex_lock(&bam_dma_lock);
  520. dev = sps_dma_find_device(chan->dev);
  521. if (dev == NULL) {
  522. SPS_ERR(sps, "sps:BAM-DMA: invalid BAM handle: %pK\n",
  523. (void *)chan->dev);
  524. result = SPS_ERROR;
  525. goto exit_err;
  526. }
  527. /* Verify the pipe indices */
  528. pipe_index = chan->dest_pipe_index;
  529. if (pipe_index >= dev->num_pipes || ((pipe_index & 1)) ||
  530. (pipe_index + 1) != chan->src_pipe_index) {
  531. SPS_ERR(sps,
  532. "sps:%s. Invalid pipe indices num_pipes=%d dest=%d src=%d\n",
  533. __func__, dev->num_pipes,
  534. chan->dest_pipe_index,
  535. chan->src_pipe_index);
  536. result = SPS_ERROR;
  537. goto exit_err;
  538. }
  539. /* Are both pipes inactive? */
  540. if (dev->chans[pipe_index / 2].state != DMA_CHAN_STATE_ALLOC_EXT ||
  541. dev->pipes[pipe_index] != PIPE_INACTIVE ||
  542. dev->pipes[pipe_index + 1] != PIPE_INACTIVE) {
  543. SPS_ERR(sps,
  544. "sps:BAM-DMA: attempt to free active chan %d: %d %d\n",
  545. pipe_index / 2, dev->pipes[pipe_index],
  546. dev->pipes[pipe_index + 1]);
  547. result = SPS_ERROR;
  548. goto exit_err;
  549. }
  550. /* Free the channel */
  551. dev->chans[pipe_index / 2].state = DMA_CHAN_STATE_FREE;
  552. exit_err:
  553. mutex_unlock(&bam_dma_lock);
  554. return result;
  555. }
  556. EXPORT_SYMBOL(sps_free_dma_chan);
  557. /**
  558. * Activate a BAM DMA pipe
  559. *
  560. * This function activates a BAM DMA pipe.
  561. *
  562. * @dev - pointer to BAM-DMA device descriptor
  563. *
  564. * @pipe_index - pipe index
  565. *
  566. * @return 0 on success, negative value on error
  567. *
  568. */
  569. static u32 sps_dma_check_pipes(struct bamdma_device *dev, u32 pipe_index)
  570. {
  571. u32 pipe_in;
  572. u32 pipe_out;
  573. int enabled_in;
  574. int enabled_out;
  575. u32 check;
  576. pipe_in = pipe_index & ~1;
  577. pipe_out = pipe_in + 1;
  578. enabled_in = bam_pipe_is_enabled(&dev->bam->base, pipe_in);
  579. enabled_out = bam_pipe_is_enabled(&dev->bam->base, pipe_out);
  580. if (!enabled_in && !enabled_out)
  581. check = DMA_PIPES_BOTH_DISABLED;
  582. else if (enabled_in && enabled_out)
  583. check = DMA_PIPES_BOTH_ENABLED;
  584. else
  585. check = DMA_PIPES_STATE_DIFF;
  586. return check;
  587. }
  588. /**
  589. * Allocate a BAM DMA pipe
  590. *
  591. */
  592. int sps_dma_pipe_alloc(void *bam_arg, u32 pipe_index, enum sps_mode dir)
  593. {
  594. struct sps_bam *bam = bam_arg;
  595. struct bamdma_device *dev;
  596. struct bamdma_chan *chan;
  597. u32 channel;
  598. int result = SPS_ERROR;
  599. if (bam == NULL) {
  600. SPS_ERR(sps, "%s\n", "sps:BAM context is NULL");
  601. return SPS_ERROR;
  602. }
  603. /* Check pipe direction */
  604. if ((DMA_PIPE_IS_DEST(pipe_index) && dir != SPS_MODE_DEST) ||
  605. (DMA_PIPE_IS_SRC(pipe_index) && dir != SPS_MODE_SRC)) {
  606. SPS_ERR(sps, "sps:BAM-DMA: wrong dir for BAM %pa pipe %d\n",
  607. &bam->props.phys_addr, pipe_index);
  608. return SPS_ERROR;
  609. }
  610. mutex_lock(&bam_dma_lock);
  611. dev = sps_dma_find_device((unsigned long) bam);
  612. if (dev == NULL) {
  613. SPS_ERR(sps, "sps:BAM-DMA: invalid BAM: %pa\n",
  614. &bam->props.phys_addr);
  615. goto exit_err;
  616. }
  617. if (pipe_index >= dev->num_pipes) {
  618. SPS_ERR(sps, "sps:BAM-DMA: BAM %pa invalid pipe: %d\n",
  619. &bam->props.phys_addr, pipe_index);
  620. goto exit_err;
  621. }
  622. if (dev->pipes[pipe_index] != PIPE_INACTIVE) {
  623. SPS_ERR(sps, "sps:BAM-DMA: BAM %pa pipe %d already active\n",
  624. &bam->props.phys_addr, pipe_index);
  625. goto exit_err;
  626. }
  627. /* Mark pipe active */
  628. dev->pipes[pipe_index] = PIPE_ACTIVE;
  629. /* If channel is not allocated, make an internal allocation */
  630. channel = pipe_index / 2;
  631. chan = &dev->chans[channel];
  632. if (chan->state != DMA_CHAN_STATE_ALLOC_EXT &&
  633. chan->state != DMA_CHAN_STATE_ALLOC_INT) {
  634. chan->state = DMA_CHAN_STATE_ALLOC_INT;
  635. }
  636. result = 0;
  637. exit_err:
  638. mutex_unlock(&bam_dma_lock);
  639. return result;
  640. }
  641. /**
  642. * Enable a BAM DMA pipe
  643. *
  644. */
  645. int sps_dma_pipe_enable(void *bam_arg, u32 pipe_index)
  646. {
  647. struct sps_bam *bam = bam_arg;
  648. struct bamdma_device *dev;
  649. struct bamdma_chan *chan;
  650. u32 channel;
  651. int result = SPS_ERROR;
  652. SPS_DBG3(sps, "sps:%s pipe %d\n", __func__, pipe_index);
  653. mutex_lock(&bam_dma_lock);
  654. dev = sps_dma_find_device((unsigned long) bam);
  655. if (dev == NULL) {
  656. SPS_ERR(sps, "sps:%s:BAM-DMA: invalid BAM\n", __func__);
  657. goto exit_err;
  658. }
  659. if (pipe_index >= dev->num_pipes) {
  660. SPS_ERR(sps, "sps:BAM-DMA: BAM %pa invalid pipe: %d\n",
  661. &bam->props.phys_addr, pipe_index);
  662. goto exit_err;
  663. }
  664. if (dev->pipes[pipe_index] != PIPE_ACTIVE) {
  665. SPS_ERR(sps, "sps:BAM-DMA: BAM %pa pipe %d not active\n",
  666. &bam->props.phys_addr, pipe_index);
  667. goto exit_err;
  668. }
  669. /*
  670. * The channel must be enabled when the dest/input/write pipe
  671. * is enabled
  672. */
  673. if (DMA_PIPE_IS_DEST(pipe_index)) {
  674. /* Configure and enable the channel */
  675. channel = pipe_index / 2;
  676. chan = &dev->chans[channel];
  677. if (chan->threshold != SPS_DMA_THRESHOLD_DEFAULT)
  678. dma_write_reg_field(dev->virt_addr,
  679. DMA_CHNL_CONFIG(channel),
  680. DMA_CHNL_ACT_THRESH,
  681. chan->thresh);
  682. if (chan->priority != SPS_DMA_PRI_DEFAULT)
  683. dma_write_reg_field(dev->virt_addr,
  684. DMA_CHNL_CONFIG(channel),
  685. DMA_CHNL_WEIGHT,
  686. chan->weight);
  687. dma_write_reg_field(dev->virt_addr,
  688. DMA_CHNL_CONFIG(channel),
  689. DMA_CHNL_ENABLE, 1);
  690. }
  691. result = 0;
  692. exit_err:
  693. mutex_unlock(&bam_dma_lock);
  694. return result;
  695. }
  696. /**
  697. * Deactivate a BAM DMA pipe
  698. *
  699. * This function deactivates a BAM DMA pipe.
  700. *
  701. * @dev - pointer to BAM-DMA device descriptor
  702. *
  703. * @bam - pointer to BAM device descriptor
  704. *
  705. * @pipe_index - pipe index
  706. *
  707. * @return 0 on success, negative value on error
  708. *
  709. */
  710. static int sps_dma_deactivate_pipe_atomic(struct bamdma_device *dev,
  711. struct sps_bam *bam,
  712. u32 pipe_index)
  713. {
  714. u32 channel;
  715. if (dev->bam != bam)
  716. return SPS_ERROR;
  717. if (pipe_index >= dev->num_pipes)
  718. return SPS_ERROR;
  719. if (dev->pipes[pipe_index] != PIPE_ACTIVE)
  720. return SPS_ERROR; /* Pipe is not active */
  721. SPS_DBG3(sps, "sps:BAM-DMA: deactivate pipe %d\n", pipe_index);
  722. /* Mark pipe inactive */
  723. dev->pipes[pipe_index] = PIPE_INACTIVE;
  724. /*
  725. * Channel must be reset when either pipe is disabled, so just always
  726. * reset regardless of other pipe's state
  727. */
  728. channel = pipe_index / 2;
  729. dma_write_reg_field(dev->virt_addr, DMA_CHNL_CONFIG(channel),
  730. DMA_CHNL_ENABLE, 0);
  731. /* If the peer pipe is also inactive, reset the channel */
  732. if (sps_dma_check_pipes(dev, pipe_index) == DMA_PIPES_BOTH_DISABLED) {
  733. /* Free channel if allocated internally */
  734. if (dev->chans[channel].state == DMA_CHAN_STATE_ALLOC_INT)
  735. dev->chans[channel].state = DMA_CHAN_STATE_FREE;
  736. }
  737. return 0;
  738. }
  739. /**
  740. * Free a BAM DMA pipe
  741. *
  742. */
  743. int sps_dma_pipe_free(void *bam_arg, u32 pipe_index)
  744. {
  745. struct bamdma_device *dev;
  746. struct sps_bam *bam = bam_arg;
  747. int result;
  748. mutex_lock(&bam_dma_lock);
  749. dev = sps_dma_find_device((unsigned long) bam);
  750. if (dev == NULL) {
  751. SPS_ERR(sps, "sps:%s:BAM-DMA: invalid BAM\n", __func__);
  752. result = SPS_ERROR;
  753. goto exit_err;
  754. }
  755. result = sps_dma_deactivate_pipe_atomic(dev, bam, pipe_index);
  756. exit_err:
  757. mutex_unlock(&bam_dma_lock);
  758. return result;
  759. }
  760. /**
  761. * Get the BAM handle for BAM-DMA.
  762. *
  763. * The BAM handle should be use as source/destination in the sps_connect().
  764. *
  765. * @return bam handle on success, zero on error
  766. */
  767. unsigned long sps_dma_get_bam_handle(void)
  768. {
  769. return (unsigned long)bam_dma_dev[0].bam;
  770. }
  771. EXPORT_SYMBOL(sps_dma_get_bam_handle);
  772. /**
  773. * Free the BAM handle for BAM-DMA.
  774. *
  775. */
  776. void sps_dma_free_bam_handle(unsigned long h)
  777. {
  778. }
  779. EXPORT_SYMBOL(sps_dma_free_bam_handle);
  780. #endif /* CONFIG_SPS_SUPPORT_BAMDMA */