sps_rm.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2011-2015, 2017-2019, 2021, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2022 Qualcomm Innovation Center, Inc. All rights reserved.
  5. */
  6. /* Resource management for the SPS device driver. */
  7. #include <linux/types.h>
  8. #include <linux/kernel.h>
  9. #include <linux/mutex.h>
  10. #include <linux/list.h>
  11. #include <linux/slab.h>
  12. #include <linux/memory.h>
  13. #include <linux/interrupt.h>
  14. #include "spsi.h"
  15. #include "sps_core.h"
  16. /* Max BAM FIFO sizes */
  17. #define SPSRM_MAX_DESC_FIFO_SIZE 0xffff
  18. #define SPSRM_MAX_DATA_FIFO_SIZE 0xffff
  19. /* Connection control struct pointer */
  20. static struct sps_rm *sps_rm;
  21. /**
  22. * Initialize resource manager module
  23. */
  24. int sps_rm_init(struct sps_rm *rm, u32 options)
  25. {
  26. /* Set the resource manager state struct pointer */
  27. sps_rm = rm;
  28. /* Initialize the state struct */
  29. INIT_LIST_HEAD(&sps_rm->connections_q);
  30. mutex_init(&sps_rm->lock);
  31. return 0;
  32. }
  33. /**
  34. * Initialize client state context
  35. *
  36. */
  37. void sps_rm_config_init(struct sps_connect *connect)
  38. {
  39. memset(connect, SPSRM_CLEAR, sizeof(*connect));
  40. }
  41. /**
  42. * Remove reference to connection mapping
  43. *
  44. * This function removes a reference from a connection mapping struct.
  45. *
  46. * @map - pointer to connection mapping struct
  47. *
  48. */
  49. static void sps_rm_remove_ref(struct sps_connection *map)
  50. {
  51. /* Free this connection */
  52. map->refs--;
  53. if (map->refs <= 0) {
  54. if (map->client_src != NULL || map->client_dest != NULL)
  55. SPS_ERR(sps,
  56. "sps:%s:Failed to allocate connection struct\n",
  57. __func__);
  58. list_del(&map->list);
  59. kfree(map);
  60. }
  61. }
  62. /**
  63. * Compare map to connect parameters
  64. *
  65. * This function compares client connect parameters to an allocated
  66. * connection mapping.
  67. *
  68. * @pipe - client context for SPS connection end point
  69. *
  70. * @return - true if match, false otherwise
  71. *
  72. */
  73. static int sps_rm_map_match(const struct sps_connect *cfg,
  74. const struct sps_connection *map)
  75. {
  76. if (cfg->source != map->src.dev ||
  77. cfg->destination != map->dest.dev)
  78. return false;
  79. if (cfg->src_pipe_index != SPSRM_CLEAR &&
  80. cfg->src_pipe_index != map->src.pipe_index)
  81. return false;
  82. if (cfg->dest_pipe_index != SPSRM_CLEAR &&
  83. cfg->dest_pipe_index != map->dest.pipe_index)
  84. return false;
  85. if (cfg->config != map->config)
  86. return false;
  87. if (cfg->desc.size != SPSRM_CLEAR) {
  88. if (cfg->desc.size != map->desc.size)
  89. return false;
  90. if (cfg->desc.phys_base != (SPSRM_CLEAR|SPSRM_ADDR_CLR) &&
  91. cfg->desc.base != (void *)(SPSRM_CLEAR|SPSRM_ADDR_CLR) &&
  92. (cfg->desc.phys_base != map->desc.phys_base ||
  93. cfg->desc.base != map->desc.base)) {
  94. return false;
  95. }
  96. }
  97. if (cfg->data.size != SPSRM_CLEAR) {
  98. if (cfg->data.size != map->data.size)
  99. return false;
  100. if (cfg->data.phys_base != (SPSRM_CLEAR|SPSRM_ADDR_CLR) &&
  101. cfg->data.base != (void *)(SPSRM_CLEAR|SPSRM_ADDR_CLR) &&
  102. (cfg->data.phys_base != map->data.phys_base ||
  103. cfg->data.base != map->data.base))
  104. return false;
  105. }
  106. return true;
  107. }
  108. /**
  109. * Find unconnected mapping
  110. *
  111. * This function finds an allocated a connection mapping.
  112. *
  113. * @pipe - client context for SPS connection end point
  114. *
  115. * @return - pointer to allocated connection mapping, or NULL if not found
  116. *
  117. */
  118. static struct sps_connection *find_unconnected(struct sps_pipe *pipe)
  119. {
  120. struct sps_connect *cfg = &pipe->connect;
  121. struct sps_connection *map;
  122. /* Has this connection already been allocated? */
  123. list_for_each_entry(map, &sps_rm->connections_q, list) {
  124. if (sps_rm_map_match(cfg, map))
  125. if ((cfg->mode == SPS_MODE_SRC
  126. && map->client_src == NULL)
  127. || (cfg->mode != SPS_MODE_SRC
  128. && map->client_dest == NULL))
  129. return map; /* Found */
  130. }
  131. return NULL; /* Not Found */
  132. }
  133. /**
  134. * Assign connection to client
  135. *
  136. * This function assigns a connection to a client.
  137. *
  138. * @pipe - client context for SPS connection end point
  139. *
  140. * @map - connection mapping
  141. *
  142. * @return 0 on success, negative value on error
  143. *
  144. */
  145. static int sps_rm_assign(struct sps_pipe *pipe,
  146. struct sps_connection *map)
  147. {
  148. struct sps_connect *cfg = &pipe->connect;
  149. unsigned long desc_iova = 0;
  150. unsigned long data_iova = 0;
  151. /* Check ownership and BAM */
  152. if ((cfg->mode == SPS_MODE_SRC && map->client_src != NULL) ||
  153. (cfg->mode != SPS_MODE_SRC && map->client_dest != NULL)) {
  154. SPS_ERR(sps,
  155. "sps:%s:The end point is already connected\n",
  156. __func__);
  157. return SPS_ERROR;
  158. }
  159. /* Check whether this end point is a BAM (not memory) */
  160. if ((cfg->mode == SPS_MODE_SRC && map->src.bam == NULL) ||
  161. (cfg->mode != SPS_MODE_SRC && map->dest.bam == NULL)) {
  162. SPS_ERR(sps, "sps:%s:The end point is empty\n", __func__);
  163. return SPS_ERROR;
  164. }
  165. /* Record the connection assignment */
  166. if (cfg->mode == SPS_MODE_SRC) {
  167. map->client_src = pipe;
  168. pipe->bam = map->src.bam;
  169. pipe->pipe_index = map->src.pipe_index;
  170. if (pipe->connect.event_thresh != SPSRM_CLEAR)
  171. map->src.event_threshold = pipe->connect.event_thresh;
  172. if (pipe->connect.lock_group != SPSRM_CLEAR)
  173. map->src.lock_group = pipe->connect.lock_group;
  174. } else {
  175. map->client_dest = pipe;
  176. pipe->bam = map->dest.bam;
  177. pipe->pipe_index = map->dest.pipe_index;
  178. if (pipe->connect.event_thresh != SPSRM_CLEAR)
  179. map->dest.event_threshold =
  180. pipe->connect.event_thresh;
  181. if (pipe->connect.lock_group != SPSRM_CLEAR)
  182. map->dest.lock_group = pipe->connect.lock_group;
  183. }
  184. pipe->map = map;
  185. SPS_DBG(pipe->bam, "sps:%s.bam %pa.pipe_index=%d\n",
  186. __func__, BAM_ID(pipe->bam), pipe->pipe_index);
  187. /* Copy parameters to client connect state */
  188. pipe->connect.src_pipe_index = map->src.pipe_index;
  189. pipe->connect.dest_pipe_index = map->dest.pipe_index;
  190. /*
  191. * The below assignment to connect.desc and connect.data will
  192. * overwrite the previous values given by the first client
  193. * in a BAM-to-BAM connection. Prevent that since the IOVAs
  194. * may be different for the same physical buffers if the
  195. * BAMs use different SMMUs.
  196. */
  197. if (pipe->bam->props.options & SPS_BAM_SMMU_EN) {
  198. desc_iova = pipe->connect.desc.iova;
  199. data_iova = pipe->connect.data.iova;
  200. }
  201. pipe->connect.desc = map->desc;
  202. pipe->connect.data = map->data;
  203. if (pipe->bam->props.options & SPS_BAM_SMMU_EN) {
  204. pipe->connect.desc.iova = desc_iova;
  205. pipe->connect.data.iova = data_iova;
  206. }
  207. pipe->client_state = SPS_STATE_ALLOCATE;
  208. return 0;
  209. }
  210. /**
  211. * Free connection mapping resources
  212. *
  213. * This function frees a connection mapping resources.
  214. *
  215. * @pipe - client context for SPS connection end point
  216. *
  217. */
  218. static void sps_rm_free_map_rsrc(struct sps_connection *map)
  219. {
  220. struct sps_bam *bam;
  221. if (map->client_src != NULL || map->client_dest != NULL)
  222. return;
  223. if (map->alloc_src_pipe != SPS_BAM_PIPE_INVALID) {
  224. bam = map->src.bam;
  225. sps_bam_pipe_free(bam, map->src.pipe_index);
  226. /* Is this a BAM-DMA pipe? */
  227. #ifdef CONFIG_SPS_SUPPORT_BAMDMA
  228. if ((bam->props.options & SPS_BAM_OPT_BAMDMA))
  229. /* Deallocate and free the BAM-DMA channel */
  230. sps_dma_pipe_free(bam, map->src.pipe_index);
  231. #endif
  232. map->alloc_src_pipe = SPS_BAM_PIPE_INVALID;
  233. map->src.pipe_index = SPS_BAM_PIPE_INVALID;
  234. }
  235. if (map->alloc_dest_pipe != SPS_BAM_PIPE_INVALID) {
  236. bam = map->dest.bam;
  237. sps_bam_pipe_free(bam, map->dest.pipe_index);
  238. /* Is this a BAM-DMA pipe? */
  239. #ifdef CONFIG_SPS_SUPPORT_BAMDMA
  240. if ((bam->props.options & SPS_BAM_OPT_BAMDMA)) {
  241. /* Deallocate the BAM-DMA channel */
  242. sps_dma_pipe_free(bam, map->dest.pipe_index);
  243. }
  244. #endif
  245. map->alloc_dest_pipe = SPS_BAM_PIPE_INVALID;
  246. map->dest.pipe_index = SPS_BAM_PIPE_INVALID;
  247. }
  248. if (map->alloc_desc_base != SPS_ADDR_INVALID) {
  249. sps_mem_free_io(map->alloc_desc_base, map->desc.size);
  250. map->alloc_desc_base = SPS_ADDR_INVALID;
  251. map->desc.phys_base = SPS_ADDR_INVALID;
  252. }
  253. if (map->alloc_data_base != SPS_ADDR_INVALID) {
  254. sps_mem_free_io(map->alloc_data_base, map->data.size);
  255. map->alloc_data_base = SPS_ADDR_INVALID;
  256. map->data.phys_base = SPS_ADDR_INVALID;
  257. }
  258. }
  259. /**
  260. * Init connection mapping from client connect
  261. *
  262. * This function initializes a connection mapping from the client's
  263. * connect parameters.
  264. *
  265. * @map - connection mapping struct
  266. *
  267. * @cfg - client connect parameters
  268. *
  269. * @return - pointer to allocated connection mapping, or NULL on error
  270. *
  271. */
  272. static void sps_rm_init_map(struct sps_connection *map,
  273. const struct sps_connect *cfg)
  274. {
  275. /* Clear the connection mapping struct */
  276. memset(map, 0, sizeof(*map));
  277. map->desc.phys_base = SPS_ADDR_INVALID;
  278. map->data.phys_base = SPS_ADDR_INVALID;
  279. map->alloc_desc_base = SPS_ADDR_INVALID;
  280. map->alloc_data_base = SPS_ADDR_INVALID;
  281. map->alloc_src_pipe = SPS_BAM_PIPE_INVALID;
  282. map->alloc_dest_pipe = SPS_BAM_PIPE_INVALID;
  283. /* Copy client required parameters */
  284. map->src.dev = cfg->source;
  285. map->dest.dev = cfg->destination;
  286. map->desc.size = cfg->desc.size;
  287. map->data.size = cfg->data.size;
  288. map->config = cfg->config;
  289. /* Did client specify descriptor FIFO? */
  290. if (map->desc.size != SPSRM_CLEAR &&
  291. cfg->desc.phys_base != (SPSRM_CLEAR|SPSRM_ADDR_CLR) &&
  292. cfg->desc.base != (void *)(SPSRM_CLEAR|SPSRM_ADDR_CLR))
  293. map->desc = cfg->desc;
  294. /* Did client specify data FIFO? */
  295. if (map->data.size != SPSRM_CLEAR &&
  296. cfg->data.phys_base != (SPSRM_CLEAR|SPSRM_ADDR_CLR) &&
  297. cfg->data.base != (void *)(SPSRM_CLEAR|SPSRM_ADDR_CLR))
  298. map->data = cfg->data;
  299. /* Did client specify source pipe? */
  300. if (cfg->src_pipe_index != SPSRM_CLEAR)
  301. map->src.pipe_index = cfg->src_pipe_index;
  302. else
  303. map->src.pipe_index = SPS_BAM_PIPE_INVALID;
  304. /* Did client specify destination pipe? */
  305. if (cfg->dest_pipe_index != SPSRM_CLEAR)
  306. map->dest.pipe_index = cfg->dest_pipe_index;
  307. else
  308. map->dest.pipe_index = SPS_BAM_PIPE_INVALID;
  309. }
  310. /**
  311. * Create a new connection mapping
  312. *
  313. * This function creates a new connection mapping.
  314. *
  315. * @pipe - client context for SPS connection end point
  316. *
  317. * @return - pointer to allocated connection mapping, or NULL on error
  318. *
  319. */
  320. static struct sps_connection *sps_rm_create(struct sps_pipe *pipe)
  321. {
  322. struct sps_connection *map;
  323. struct sps_bam *bam;
  324. u32 desc_size;
  325. u32 data_size;
  326. enum sps_mode dir;
  327. int success = false;
  328. /* Allocate new connection */
  329. map = kzalloc(sizeof(*map), GFP_KERNEL);
  330. if (map == NULL) {
  331. SPS_ERR(sps,
  332. "sps:%s:Failed to allocate connection struct\n",
  333. __func__);
  334. return NULL;
  335. }
  336. /* Initialize connection struct */
  337. sps_rm_init_map(map, &pipe->connect);
  338. dir = pipe->connect.mode;
  339. /* Use a do/while() loop to avoid a "goto" */
  340. success = false;
  341. /* Get BAMs */
  342. map->src.bam = sps_h2bam(map->src.dev);
  343. if (map->src.bam == NULL) {
  344. if (map->src.dev != SPS_DEV_HANDLE_MEM) {
  345. SPS_ERR(sps, "sps:Invalid BAM handle: %pK\n",
  346. (void *)(&map->src.dev));
  347. goto exit_err;
  348. }
  349. map->src.pipe_index = SPS_BAM_PIPE_INVALID;
  350. }
  351. if (!(pipe->connect.options & SPS_O_DUMMY_PEER)) {
  352. map->dest.bam = sps_h2bam(map->dest.dev);
  353. if (map->dest.bam == NULL) {
  354. if (map->dest.dev != SPS_DEV_HANDLE_MEM) {
  355. SPS_ERR(sps,
  356. "sps:Invalid BAM handle: %pK",
  357. (void *)(&map->dest.dev));
  358. goto exit_err;
  359. }
  360. map->dest.pipe_index = SPS_BAM_PIPE_INVALID;
  361. }
  362. }
  363. /* Check the BAM device for the pipe */
  364. if ((dir == SPS_MODE_SRC && map->src.bam == NULL) ||
  365. (dir != SPS_MODE_SRC && map->dest.bam == NULL)) {
  366. SPS_ERR(sps, "sps:Invalid BAM endpt: dir %d src %pK dest %pK\n",
  367. dir, (void *)(&map->src.dev), (void *)(&map->dest.dev));
  368. goto exit_err;
  369. }
  370. /* Allocate pipes and copy BAM parameters */
  371. if (map->src.bam != NULL) {
  372. /* Allocate the pipe */
  373. bam = map->src.bam;
  374. map->alloc_src_pipe = sps_bam_pipe_alloc(bam,
  375. map->src.pipe_index);
  376. if (map->alloc_src_pipe == SPS_BAM_PIPE_INVALID)
  377. goto exit_err;
  378. map->src.pipe_index = map->alloc_src_pipe;
  379. /* Is this a BAM-DMA pipe? */
  380. #ifdef CONFIG_SPS_SUPPORT_BAMDMA
  381. if ((bam->props.options & SPS_BAM_OPT_BAMDMA)) {
  382. int rc;
  383. /* Allocate the BAM-DMA channel */
  384. rc = sps_dma_pipe_alloc(bam, map->src.pipe_index,
  385. SPS_MODE_SRC);
  386. if (rc) {
  387. SPS_ERR(bam,
  388. "sps:Failed to alloc BAM-DMA pipe: %d\n",
  389. map->src.pipe_index);
  390. goto exit_err;
  391. }
  392. }
  393. #endif
  394. map->src.bam_phys = bam->props.phys_addr;
  395. map->src.event_threshold = bam->props.event_threshold;
  396. }
  397. if (map->dest.bam != NULL) {
  398. /* Allocate the pipe */
  399. bam = map->dest.bam;
  400. map->alloc_dest_pipe = sps_bam_pipe_alloc(bam,
  401. map->dest.pipe_index);
  402. if (map->alloc_dest_pipe == SPS_BAM_PIPE_INVALID)
  403. goto exit_err;
  404. map->dest.pipe_index = map->alloc_dest_pipe;
  405. /* Is this a BAM-DMA pipe? */
  406. #ifdef CONFIG_SPS_SUPPORT_BAMDMA
  407. if ((bam->props.options & SPS_BAM_OPT_BAMDMA)) {
  408. int rc;
  409. /* Allocate the BAM-DMA channel */
  410. rc = sps_dma_pipe_alloc(bam, map->dest.pipe_index,
  411. SPS_MODE_DEST);
  412. if (rc) {
  413. SPS_ERR(bam,
  414. "sps:Failed to alloc BAM-DMA pipe: %d\n",
  415. map->dest.pipe_index);
  416. goto exit_err;
  417. }
  418. }
  419. #endif
  420. map->dest.bam_phys = bam->props.phys_addr;
  421. map->dest.event_threshold =
  422. bam->props.event_threshold;
  423. }
  424. /* Get default FIFO sizes */
  425. desc_size = 0;
  426. data_size = 0;
  427. if (map->src.bam != NULL) {
  428. bam = map->src.bam;
  429. desc_size = bam->props.desc_size;
  430. data_size = bam->props.data_size;
  431. }
  432. if (map->dest.bam != NULL) {
  433. bam = map->dest.bam;
  434. if (bam->props.desc_size > desc_size)
  435. desc_size = bam->props.desc_size;
  436. if (bam->props.data_size > data_size)
  437. data_size = bam->props.data_size;
  438. }
  439. /* Set FIFO sizes */
  440. if (map->desc.size == SPSRM_CLEAR)
  441. map->desc.size = desc_size;
  442. if (map->src.bam != NULL && map->dest.bam != NULL) {
  443. /* BAM-to-BAM requires data FIFO */
  444. if (map->data.size == SPSRM_CLEAR)
  445. map->data.size = data_size;
  446. } else {
  447. if (!(pipe->connect.options & SPS_O_DUMMY_PEER))
  448. map->data.size = 0;
  449. }
  450. if (map->desc.size > SPSRM_MAX_DESC_FIFO_SIZE) {
  451. SPS_ERR(sps, "sps:Invalid desc FIFO size: 0x%x\n",
  452. map->desc.size);
  453. goto exit_err;
  454. }
  455. if (map->src.bam != NULL && map->dest.bam != NULL &&
  456. map->data.size > SPSRM_MAX_DATA_FIFO_SIZE) {
  457. SPS_ERR(sps, "sps:Invalid data FIFO size: 0x%x\n",
  458. map->data.size);
  459. goto exit_err;
  460. }
  461. /* Allocate descriptor FIFO if necessary */
  462. if (map->desc.size && map->desc.phys_base == SPS_ADDR_INVALID) {
  463. map->alloc_desc_base = sps_mem_alloc_io(map->desc.size);
  464. if (map->alloc_desc_base == SPS_ADDR_INVALID) {
  465. SPS_ERR(sps, "sps:I/O memory allocation failure:0x%x\n",
  466. map->desc.size);
  467. goto exit_err;
  468. }
  469. map->desc.phys_base = map->alloc_desc_base;
  470. map->desc.base = spsi_get_mem_ptr(map->desc.phys_base);
  471. if (map->desc.base == NULL) {
  472. SPS_ERR(sps,
  473. "sps:Cannot get virt addr for I/O buffer:%pa\n",
  474. &map->desc.phys_base);
  475. goto exit_err;
  476. }
  477. }
  478. /* Allocate data FIFO if necessary */
  479. if (map->data.size && map->data.phys_base == SPS_ADDR_INVALID) {
  480. map->alloc_data_base = sps_mem_alloc_io(map->data.size);
  481. if (map->alloc_data_base == SPS_ADDR_INVALID) {
  482. SPS_ERR(sps, "sps:I/O memory allocation failure:0x%x\n",
  483. map->data.size);
  484. goto exit_err;
  485. }
  486. map->data.phys_base = map->alloc_data_base;
  487. map->data.base = spsi_get_mem_ptr(map->data.phys_base);
  488. if (map->data.base == NULL) {
  489. SPS_ERR(sps,
  490. "sps:Cannot get virt addr for I/O buffer:%pa\n",
  491. &map->data.phys_base);
  492. goto exit_err;
  493. }
  494. }
  495. /* Attempt to assign this connection to the client */
  496. if (sps_rm_assign(pipe, map)) {
  497. SPS_ERR(sps,
  498. "sps:%s:failed to assign a connection to the client\n",
  499. __func__);
  500. goto exit_err;
  501. }
  502. /* Initialization was successful */
  503. success = true;
  504. exit_err:
  505. /* If initialization failed, free resources */
  506. if (!success) {
  507. sps_rm_free_map_rsrc(map);
  508. kfree(map);
  509. return NULL;
  510. }
  511. return map;
  512. }
  513. /**
  514. * Free connection mapping
  515. *
  516. * This function frees a connection mapping.
  517. *
  518. * @pipe - client context for SPS connection end point
  519. *
  520. * @return 0 on success, negative value on error
  521. *
  522. */
  523. static int sps_rm_free(struct sps_pipe *pipe)
  524. {
  525. struct sps_connection *map = (void *)pipe->map;
  526. struct sps_connect *cfg = &pipe->connect;
  527. mutex_lock(&sps_rm->lock);
  528. /* Free this connection */
  529. if (cfg->mode == SPS_MODE_SRC)
  530. map->client_src = NULL;
  531. else
  532. map->client_dest = NULL;
  533. pipe->map = NULL;
  534. pipe->client_state = SPS_STATE_DISCONNECT;
  535. sps_rm_free_map_rsrc(map);
  536. sps_rm_remove_ref(map);
  537. mutex_unlock(&sps_rm->lock);
  538. return 0;
  539. }
  540. /**
  541. * Allocate an SPS connection end point
  542. *
  543. * This function allocates resources and initializes a BAM connection.
  544. *
  545. * @pipe - client context for SPS connection end point
  546. *
  547. * @return 0 on success, negative value on error
  548. *
  549. */
  550. static int sps_rm_alloc(struct sps_pipe *pipe)
  551. {
  552. struct sps_connection *map;
  553. int result = SPS_ERROR;
  554. if (pipe->connect.sps_reserved != SPSRM_CLEAR) {
  555. /*
  556. * Client did not call sps_get_config() to init
  557. * struct sps_connect, so only use legacy members.
  558. */
  559. unsigned long source = pipe->connect.source;
  560. unsigned long destination = pipe->connect.destination;
  561. enum sps_mode mode = pipe->connect.mode;
  562. u32 config = pipe->connect.config;
  563. memset(&pipe->connect, SPSRM_CLEAR,
  564. sizeof(pipe->connect));
  565. pipe->connect.source = source;
  566. pipe->connect.destination = destination;
  567. pipe->connect.mode = mode;
  568. pipe->connect.config = config;
  569. }
  570. if (pipe->connect.config == SPSRM_CLEAR)
  571. pipe->connect.config = SPS_CONFIG_DEFAULT;
  572. /*
  573. * If configuration is not default, then client is specifying a
  574. * connection mapping. Find a matching mapping, or fail.
  575. * If a match is found, the client's Connect struct will be updated
  576. * with all the mapping's values.
  577. */
  578. if (pipe->connect.config != SPS_CONFIG_DEFAULT) {
  579. if (sps_map_find(&pipe->connect)) {
  580. SPS_ERR(sps,
  581. "sps:%s:Failed to find connection mapping\n",
  582. __func__);
  583. return SPS_ERROR;
  584. }
  585. }
  586. mutex_lock(&sps_rm->lock);
  587. /* Check client state */
  588. if (IS_SPS_STATE_OK(pipe)) {
  589. SPS_ERR(sps,
  590. "sps:%s:Client connection already allocated\n",
  591. __func__);
  592. goto exit_err;
  593. }
  594. /* Are the connection resources already allocated? */
  595. map = find_unconnected(pipe);
  596. if (map != NULL) {
  597. /* Attempt to assign this connection to the client */
  598. if (sps_rm_assign(pipe, map))
  599. /* Assignment failed, so must allocate new */
  600. map = NULL;
  601. }
  602. /* Allocate a new connection if necessary */
  603. if (map == NULL) {
  604. map = sps_rm_create(pipe);
  605. if (map == NULL) {
  606. SPS_ERR(sps,
  607. "sps:%s:Failed to allocate connection\n",
  608. __func__);
  609. goto exit_err;
  610. }
  611. list_add_tail(&map->list, &sps_rm->connections_q);
  612. }
  613. /* Add the connection to the allocated queue */
  614. map->refs++;
  615. /* Initialization was successful */
  616. result = 0;
  617. exit_err:
  618. mutex_unlock(&sps_rm->lock);
  619. if (result)
  620. return SPS_ERROR;
  621. return 0;
  622. }
  623. /**
  624. * Disconnect an SPS connection end point
  625. *
  626. * This function frees resources and de-initializes a BAM connection.
  627. *
  628. * @pipe - client context for SPS connection end point
  629. *
  630. * @return 0 on success, negative value on error
  631. *
  632. */
  633. static int sps_rm_disconnect(struct sps_pipe *pipe)
  634. {
  635. sps_rm_free(pipe);
  636. return 0;
  637. }
  638. /**
  639. * Process connection state change
  640. *
  641. * This function processes a connection state change.
  642. *
  643. * @pipe - pointer to client context
  644. *
  645. * @state - new state for connection
  646. *
  647. * @return 0 on success, negative value on error
  648. *
  649. */
  650. int sps_rm_state_change(struct sps_pipe *pipe, u32 state)
  651. {
  652. int auto_enable = false;
  653. int result;
  654. /* Allocate the pipe */
  655. if (pipe->client_state == SPS_STATE_DISCONNECT &&
  656. state == SPS_STATE_ALLOCATE) {
  657. if (sps_rm_alloc(pipe)) {
  658. SPS_ERR(pipe->bam,
  659. "sps:Fail to allocate resource for BAM 0x%pK pipe %d\n",
  660. pipe->bam, pipe->pipe_index);
  661. return SPS_ERROR;
  662. }
  663. }
  664. /* Configure the pipe */
  665. if (pipe->client_state == SPS_STATE_ALLOCATE &&
  666. state == SPS_STATE_CONNECT) {
  667. /* Connect the BAM pipe */
  668. struct sps_bam_connect_param params;
  669. memset(&params, 0, sizeof(params));
  670. params.mode = pipe->connect.mode;
  671. if (pipe->connect.options != SPSRM_CLEAR) {
  672. params.options = pipe->connect.options;
  673. params.irq_gen_addr = pipe->connect.irq_gen_addr;
  674. params.irq_gen_data = pipe->connect.irq_gen_data;
  675. }
  676. result = sps_bam_pipe_connect(pipe, &params);
  677. if (result) {
  678. SPS_ERR(pipe->bam,
  679. "sps:Failed to connect BAM 0x%pK pipe %d\n",
  680. pipe->bam, pipe->pipe_index);
  681. return SPS_ERROR;
  682. }
  683. pipe->client_state = SPS_STATE_CONNECT;
  684. /* Set auto-enable for system-mode connections */
  685. if (pipe->connect.source == SPS_DEV_HANDLE_MEM ||
  686. pipe->connect.destination == SPS_DEV_HANDLE_MEM) {
  687. if (pipe->map->desc.size != 0 &&
  688. pipe->map->desc.phys_base != SPS_ADDR_INVALID)
  689. auto_enable = true;
  690. }
  691. }
  692. /* Enable the pipe data flow */
  693. if (pipe->client_state == SPS_STATE_CONNECT &&
  694. !(state == SPS_STATE_DISABLE
  695. || state == SPS_STATE_DISCONNECT)
  696. && (state == SPS_STATE_ENABLE || auto_enable
  697. || (pipe->connect.options & SPS_O_AUTO_ENABLE))) {
  698. result = sps_bam_pipe_enable(pipe->bam, pipe->pipe_index);
  699. if (result) {
  700. SPS_ERR(pipe->bam,
  701. "sps:Failed to set BAM %pa pipe %d flow on\n",
  702. &pipe->bam->props.phys_addr,
  703. pipe->pipe_index);
  704. return SPS_ERROR;
  705. }
  706. /* Is this a BAM-DMA pipe? */
  707. #ifdef CONFIG_SPS_SUPPORT_BAMDMA
  708. if ((pipe->bam->props.options & SPS_BAM_OPT_BAMDMA)) {
  709. /* Activate the BAM-DMA channel */
  710. result = sps_dma_pipe_enable(pipe->bam,
  711. pipe->pipe_index);
  712. if (result) {
  713. SPS_ERR(pipe->bam,
  714. "sps:Failed to activate BAM-DMA pipe: %d\n",
  715. pipe->pipe_index);
  716. return SPS_ERROR;
  717. }
  718. }
  719. #endif
  720. pipe->client_state = SPS_STATE_ENABLE;
  721. }
  722. /* Disable the pipe data flow */
  723. if (pipe->client_state == SPS_STATE_ENABLE &&
  724. (state == SPS_STATE_DISABLE || state == SPS_STATE_DISCONNECT)) {
  725. result = sps_bam_pipe_disable(pipe->bam, pipe->pipe_index);
  726. if (result) {
  727. SPS_ERR(pipe->bam,
  728. "sps:Failed to set BAM %pa pipe %d flow off\n",
  729. &pipe->bam->props.phys_addr,
  730. pipe->pipe_index);
  731. return SPS_ERROR;
  732. }
  733. pipe->client_state = SPS_STATE_CONNECT;
  734. }
  735. /* Disconnect the BAM pipe */
  736. if (pipe->client_state == SPS_STATE_CONNECT &&
  737. state == SPS_STATE_DISCONNECT) {
  738. struct sps_connection *map;
  739. struct sps_bam *bam = pipe->bam;
  740. unsigned long flags;
  741. u32 pipe_index;
  742. if (pipe->connect.mode == SPS_MODE_SRC)
  743. pipe_index = pipe->map->src.pipe_index;
  744. else
  745. pipe_index = pipe->map->dest.pipe_index;
  746. if (bam->props.irq > 0)
  747. synchronize_irq(bam->props.irq);
  748. spin_lock_irqsave(&bam->isr_lock, flags);
  749. pipe->disconnecting = true;
  750. spin_unlock_irqrestore(&bam->isr_lock, flags);
  751. result = sps_bam_pipe_disconnect(pipe->bam, pipe_index);
  752. if (result) {
  753. SPS_ERR(pipe->bam,
  754. "sps:Failed to disconnect BAM %pa pipe %d\n",
  755. &pipe->bam->props.phys_addr,
  756. pipe->pipe_index);
  757. return SPS_ERROR;
  758. }
  759. /* Clear map state */
  760. map = (void *)pipe->map;
  761. if (pipe->connect.mode == SPS_MODE_SRC)
  762. map->client_src = NULL;
  763. else if (pipe->connect.mode == SPS_MODE_DEST)
  764. map->client_dest = NULL;
  765. sps_rm_disconnect(pipe);
  766. /* Clear the client state */
  767. pipe->map = NULL;
  768. pipe->bam = NULL;
  769. pipe->client_state = SPS_STATE_DISCONNECT;
  770. }
  771. return 0;
  772. }