sps_rm.c 22 KB

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