a55b36308efd279bf7c9a42155c20e741f57eb6b
[processor-sdk/open-amp.git] / lib / rpmsg / rpmsg_core.c
1 /*
2  * Copyright (c) 2014, Mentor Graphics Corporation
3  * All rights reserved.
4  * Copyright (c) 2015 Xilinx, Inc. All rights reserved.
5  * Copyright (c) 2016 Freescale Semiconductor, Inc. All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright notice,
11  *    this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright notice,
13  *    this list of conditions and the following disclaimer in the documentation
14  *    and/or other materials provided with the distribution.
15  * 3. Neither the name of Mentor Graphics Corporation nor the names of its
16  *    contributors may be used to endorse or promote products derived from this
17  *    software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
32 /**************************************************************************
33  * FILE NAME
34  *
35  *       rpmsg_core.c
36  *
37  * COMPONENT
38  *
39  *       OpenAMP
40  *
41  * DESCRIPTION
42  *
43  * This file provides the core functionality of RPMSG messaging part like
44  * message parsing ,Rx/Tx callbacks handling , channel creation/deletion
45  * and address management.
46  *
47  *
48  **************************************************************************/
49 #include <string.h>
50 #include "openamp/rpmsg.h"
51 #include "metal/utilities.h"
52 #include "metal/io.h"
53 #include "metal/cache.h"
54 #include "metal/alloc.h"
55 #include "metal/cpu.h"
57 /* Internal functions */
58 static void rpmsg_rx_callback(struct virtqueue *vq);
59 static void rpmsg_tx_callback(struct virtqueue *vq);
61 /**
62  * rpmsg_start_ipc
63  *
64  * This function creates communication links(virtqueues) for remote device
65  * and notifies it to start IPC.
66  *
67  * @param rdev - remote device handle
68  *
69  * @return - status of function execution
70  *
71  */
72 int rpmsg_start_ipc(struct remote_device *rdev)
73 {
74         struct virtio_device *virt_dev;
75         struct rpmsg_endpoint *ns_ept;
76         void (*callback[2]) (struct virtqueue * vq);
77         const char *vq_names[2];
78         unsigned long dev_features;
79         int status;
80         struct virtqueue *vqs[2];
81         int i;
83         virt_dev = &rdev->virt_dev;
85         /* Initialize names and callbacks based on the device role */
86         if (rdev->role == RPMSG_MASTER) {
87                 vq_names[0] = "tx_vq";
88                 vq_names[1] = "rx_vq";
89                 callback[0] = rpmsg_tx_callback;
90                 callback[1] = rpmsg_rx_callback;
91         } else {
92                 vq_names[0] = "rx_vq";
93                 vq_names[1] = "tx_vq";
94                 callback[0] = rpmsg_rx_callback;
95                 callback[1] = rpmsg_tx_callback;
96         }
98         /* Create virtqueues for remote device */
99         status = virt_dev->func->create_virtqueues(virt_dev, 0,
100                                                    RPMSG_MAX_VQ_PER_RDEV,
101                                                    vq_names, callback,
102                                                    RPMSG_NULL);
103         if (status != RPMSG_SUCCESS) {
104                 return status;
105         }
107         dev_features = virt_dev->func->get_features(virt_dev);
109         /*
110          * Create name service announcement endpoint if device supports name
111          * service announcement feature.
112          */
113         if ((dev_features & (1 << VIRTIO_RPMSG_F_NS))) {
114                 rdev->support_ns = RPMSG_TRUE;
115                 ns_ept = _create_endpoint(rdev, rpmsg_ns_callback, rdev,
116                                           RPMSG_NS_EPT_ADDR);
117                 if (!ns_ept) {
118                         return RPMSG_ERR_NO_MEM;
119                 }
120         }
122         /* Initialize notifications for vring. */
123         if (rdev->role == RPMSG_MASTER) {
124                 vqs[0] = rdev->tvq;
125                 vqs[1] = rdev->rvq;
126         } else {
127                 vqs[0] = rdev->rvq;
128                 vqs[1] = rdev->tvq;
129         }
130         for (i = 0; i <= 1; i++) {
131                 status = hil_enable_vring_notifications(i, vqs[i]);
132                 if (status != RPMSG_SUCCESS) {
133                         return status;
134                 }
135         }
137         if (rdev->role == RPMSG_MASTER) {
138                 virt_dev->func->set_status(virt_dev,
139                         VIRTIO_CONFIG_STATUS_DRIVER_OK);
140                 status = rpmsg_rdev_notify(rdev);
141         }
143         return status;
146 /**
147  * _rpmsg_create_channel
148  *
149  * Creates new rpmsg channel with the given parameters.
150  *
151  * @param rdev - pointer to remote device which contains the channel
152  * @param name - name of the device
153  * @param src  - source address for the rpmsg channel
154  * @param dst  - destination address for the rpmsg channel
155  *
156  * @return - pointer to new rpmsg channel
157  *
158  */
159 struct rpmsg_channel *_rpmsg_create_channel(struct remote_device *rdev,
160                                             char *name, unsigned long src,
161                                             unsigned long dst)
163         struct rpmsg_channel *rp_chnl;
165         rp_chnl = metal_allocate_memory(sizeof(struct rpmsg_channel));
166         if (rp_chnl) {
167                 memset(rp_chnl, 0x00, sizeof(struct rpmsg_channel));
168                 strncpy(rp_chnl->name, name, sizeof(rp_chnl->name));
169                 rp_chnl->src = src;
170                 rp_chnl->dst = dst;
171                 rp_chnl->rdev = rdev;
172                 /* Place channel on channels list */
173                 metal_mutex_acquire(&rdev->lock);
174                 metal_list_add_tail(&rdev->rp_channels, &rp_chnl->node);
175                 metal_mutex_release(&rdev->lock);
176         }
178         return rp_chnl;
181 /**
182  * _rpmsg_delete_channel
183  *
184  * Deletes given rpmsg channel.
185  *
186  * @param rp_chnl -  pointer to rpmsg channel to delete
187  *
188  * return - none
189  */
190 void _rpmsg_delete_channel(struct rpmsg_channel *rp_chnl)
192         if (rp_chnl) {
193                 metal_mutex_acquire(&rp_chnl->rdev->lock);
194                 metal_list_del(&rp_chnl->node);
195                 metal_mutex_release(&rp_chnl->rdev->lock);
196                 metal_free_memory(rp_chnl);
197         }
200 /**
201  * _create_endpoint
202  *
203  * This function creates rpmsg endpoint.
204  *
205  * @param rdev    - pointer to remote device
206  * @param cb      - Rx completion call back
207  * @param priv    - private data
208  * @param addr    - endpoint src address
209  *
210  * @return - pointer to endpoint control block
211  *
212  */
213 struct rpmsg_endpoint *_create_endpoint(struct remote_device *rdev,
214                                         rpmsg_rx_cb_t cb, void *priv,
215                                         unsigned long addr)
218         struct rpmsg_endpoint *rp_ept;
219         int status = RPMSG_SUCCESS;
221         rp_ept = metal_allocate_memory(sizeof(struct rpmsg_endpoint));
222         if (!rp_ept) {
223                 return RPMSG_NULL;
224         }
225         memset(rp_ept, 0x00, sizeof(struct rpmsg_endpoint));
227         metal_mutex_acquire(&rdev->lock);
229         if (addr != RPMSG_ADDR_ANY) {
230                 /*
231                  * Application has requested a particular src address for endpoint,
232                  * first check if address is available.
233                  */
234                 if (!rpmsg_is_address_set
235                     (rdev->bitmap, RPMSG_ADDR_BMP_SIZE, addr)) {
236                         /* Mark the address as used in the address bitmap. */
237                         rpmsg_set_address(rdev->bitmap, RPMSG_ADDR_BMP_SIZE,
238                                           addr);
240                 } else {
241                         status = RPMSG_ERR_DEV_ADDR;
242                 }
243         } else {
244                 addr = rpmsg_get_address(rdev->bitmap, RPMSG_ADDR_BMP_SIZE);
245                 if ((int)addr < 0) {
246                         status = RPMSG_ERR_DEV_ADDR;
247                 }
248         }
250         /* Do cleanup in case of error and return */
251         if (RPMSG_SUCCESS != status) {
252                 metal_free_memory(rp_ept);
253                 metal_mutex_release(&rdev->lock);
254                 return RPMSG_NULL;
255         }
257         rp_ept->addr = addr;
258         rp_ept->cb = cb;
259         rp_ept->priv = priv;
261         metal_list_add_tail(&rdev->rp_endpoints, &rp_ept->node);
263         metal_mutex_release(&rdev->lock);
265         return rp_ept;
268 /**
269  * rpmsg_destroy_ept
270  *
271  * This function deletes rpmsg endpoint and performs cleanup.
272  *
273  * @param rdev   - pointer to remote device
274  * @param rp_ept - pointer to endpoint to destroy
275  *
276  */
277 void _destroy_endpoint(struct remote_device *rdev,
278                        struct rpmsg_endpoint *rp_ept)
280         metal_mutex_acquire(&rdev->lock);
281         rpmsg_release_address(rdev->bitmap, RPMSG_ADDR_BMP_SIZE,
282                               rp_ept->addr);
283         metal_list_del(&rp_ept->node);
284         metal_mutex_release(&rdev->lock);
285         /* free node and rp_ept */
286         metal_free_memory(rp_ept);
289 /**
290  * rpmsg_send_ns_message
291  *
292  * Sends name service announcement to remote device
293  *
294  * @param rdev    - pointer to remote device
295  * @param rp_chnl - pointer to rpmsg channel
296  * @param flags   - Channel creation/deletion flags
297  *
298  */
299 int rpmsg_send_ns_message(struct remote_device *rdev,
300                            struct rpmsg_channel *rp_chnl, unsigned long flags)
303         struct rpmsg_hdr *rp_hdr;
304         struct rpmsg_ns_msg *ns_msg;
305         unsigned short idx;
306         unsigned long len;
308         metal_mutex_acquire(&rdev->lock);
310         /* Get Tx buffer. */
311         rp_hdr = (struct rpmsg_hdr *)rpmsg_get_tx_buffer(rdev, &len, &idx);
312         if (!rp_hdr) {
313                 metal_mutex_release(&rdev->lock);
314                 return -RPMSG_ERR_NO_BUFF;
315         }
317         /* Fill out name service data. */
318         rp_hdr->dst = RPMSG_NS_EPT_ADDR;
319         rp_hdr->len = sizeof(struct rpmsg_ns_msg);
320         ns_msg = (struct rpmsg_ns_msg *) RPMSG_LOCATE_DATA(rp_hdr);
321         strncpy(ns_msg->name, rp_chnl->name, sizeof(rp_chnl->name));
322         ns_msg->flags = flags;
323         ns_msg->addr = rp_chnl->src;
325         /* Place the buffer on virtqueue. */
326         rpmsg_enqueue_buffer(rdev, rp_hdr, len, idx);
328         /* Notify the other side that it has data to process. */
329         virtqueue_kick(rdev->tvq);
331         metal_mutex_release(&rdev->lock);
332         return RPMSG_SUCCESS;
335 /**
336  * rpmsg_enqueue_buffers
337  *
338  * Places buffer on the virtqueue for consumption by the other side.
339  *
340  * @param rdev   - pointer to remote core
341  * @param buffer - buffer pointer
342  * @param len    - buffer length
343  * @idx          - buffer index
344  *
345  * @return - status of function execution
346  *
347  */
348 int rpmsg_enqueue_buffer(struct remote_device *rdev, void *buffer,
349                          unsigned long len, unsigned short idx)
351         int status;
352         struct metal_sg sg;
353         struct metal_io_region *io;
355         io = rdev->proc->sh_buff.io;
356         if (io) {
357                 if (! (io->mem_flags & METAL_UNCACHED))
358                         metal_cache_flush(buffer, (unsigned int)len);
359         }
360         if (rdev->role == RPMSG_REMOTE) {
361                 /* Initialize buffer node */
362                 sg.virt = buffer;
363                 sg.len = len;
364                 sg.io = io;
365                 status = virtqueue_add_buffer(rdev->tvq, &sg, 0, 1, buffer);
366         } else {
367                 (void)sg;
368                 status = virtqueue_add_consumed_buffer(rdev->tvq, idx, len);
369         }
371         return status;
374 /**
375  * rpmsg_return_buffer
376  *
377  * Places the used buffer back on the virtqueue.
378  *
379  * @param rdev   - pointer to remote core
380  * @param buffer - buffer pointer
381  * @param len    - buffer length
382  * @param idx    - buffer index
383  *
384  */
385 void rpmsg_return_buffer(struct remote_device *rdev, void *buffer,
386                          unsigned long len, unsigned short idx)
388         struct metal_sg sg;
390         if (rdev->role == RPMSG_REMOTE) {
391                 /* Initialize buffer node */
392                 sg.virt = buffer;
393                 sg.len = len;
394                 sg.io = rdev->proc->sh_buff.io;
395                 virtqueue_add_buffer(rdev->rvq, &sg, 0, 1, buffer);
396         } else {
397                 (void)sg;
398                 virtqueue_add_consumed_buffer(rdev->rvq, idx, len);
399         }
402 /**
403  * rpmsg_get_tx_buffer
404  *
405  * Provides buffer to transmit messages.
406  *
407  * @param rdev - pointer to remote device
408  * @param len  - length of returned buffer
409  * @param idx  - buffer index
410  *
411  * return - pointer to buffer.
412  */
413 void *rpmsg_get_tx_buffer(struct remote_device *rdev, unsigned long *len,
414                           unsigned short *idx)
416         void *data;
418         if (rdev->role == RPMSG_REMOTE) {
419                 data = virtqueue_get_buffer(rdev->tvq, (uint32_t *) len, idx);
420                 if (data == RPMSG_NULL) {
421                         data = sh_mem_get_buffer(rdev->mem_pool);
422                         *len = RPMSG_BUFFER_SIZE;
423                 }
424         } else {
425                 data =
426                     virtqueue_get_available_buffer(rdev->tvq, idx,
427                                                    (uint32_t *) len);
428         }
429         return data;
432 /**
433  * rpmsg_get_rx_buffer
434  *
435  * Retrieves the received buffer from the virtqueue.
436  *
437  * @param rdev - pointer to remote device
438  * @param len  - size of received buffer
439  * @param idx  - index of buffer
440  *
441  * @return - pointer to received buffer
442  *
443  */
444 void *rpmsg_get_rx_buffer(struct remote_device *rdev, unsigned long *len,
445                           unsigned short *idx)
448         void *data;
449         if (rdev->role == RPMSG_REMOTE) {
450                 data = virtqueue_get_buffer(rdev->rvq, (uint32_t *) len, idx);
451         } else {
452                 data =
453                     virtqueue_get_available_buffer(rdev->rvq, idx,
454                                                    (uint32_t *) len);
455         }
456         if (data) {
457                 struct metal_io_region *io;
458                 io = rdev->proc->sh_buff.io;
459                 if (io) {
460                         if (! (io->mem_flags & METAL_UNCACHED))
461                                 metal_cache_invalidate(data,
462                                         (unsigned int)(*len));
463                 }
464         }
466         return data;
469 /**
470  * rpmsg_free_buffer
471  *
472  * Frees the allocated buffers.
473  *
474  * @param rdev   - pointer to remote device
475  * @param buffer - pointer to buffer to free
476  *
477  */
478 void rpmsg_free_buffer(struct remote_device *rdev, void *buffer)
480         if (rdev->role == RPMSG_REMOTE) {
481                 sh_mem_free_buffer(buffer, rdev->mem_pool);
482         }
485 /**
486  * rpmsg_tx_callback
487  *
488  * Tx callback function.
489  *
490  * @param vq - pointer to virtqueue on which Tx is has been
491  *             completed.
492  *
493  */
494 static void rpmsg_tx_callback(struct virtqueue *vq)
496         struct remote_device *rdev;
497         struct virtio_device *vdev;
498         struct rpmsg_channel *rp_chnl;
499         struct metal_list *node;
501         vdev = (struct virtio_device *)vq->vq_dev;
502         rdev = (struct remote_device *)vdev;
504         /* Check if the remote device is master. */
505         if (rdev->role == RPMSG_MASTER) {
507                 /* Notification is received from the master. Now the remote(us) can
508                  * performs one of two operations;
509                  *
510                  * a. If name service announcement is supported then it will send NS message.
511                  *    else
512                  * b. It will update the channel state to active so that further communication
513                  *    can take place.
514                  */
515                 metal_list_for_each(&rdev->rp_channels, node) {
516                         rp_chnl = metal_container_of(node,
517                                 struct rpmsg_channel, node);
519                         if (rp_chnl->state == RPMSG_CHNL_STATE_IDLE) {
521                                 if (rdev->support_ns) {
522                                         if (rpmsg_send_ns_message(rdev, rp_chnl,
523                                                       RPMSG_NS_CREATE) ==
524                                                 RPMSG_SUCCESS)
525                                                 rp_chnl->state =
526                                                         RPMSG_CHNL_STATE_NS;
527                                 } else {
528                                         rp_chnl->state =
529                                             RPMSG_CHNL_STATE_ACTIVE;
530                                 }
532                         }
534                 }
535         }
538 /**
539  * rpmsg_rx_callback
540  *
541  * Rx callback function.
542  *
543  * @param vq - pointer to virtqueue on which messages is received
544  *
545  */
546 void rpmsg_rx_callback(struct virtqueue *vq)
548         struct remote_device *rdev;
549         struct virtio_device *vdev;
550         struct rpmsg_channel *rp_chnl;
551         struct rpmsg_endpoint *rp_ept;
552         struct rpmsg_hdr *rp_hdr;
553         struct rpmsg_hdr_reserved *reserved;
554         struct metal_list *node;
555         unsigned long len;
556         unsigned short idx;
558         vdev = (struct virtio_device *)vq->vq_dev;
559         rdev = (struct remote_device *)vdev;
561         if (rdev->role == RPMSG_MASTER) {
562                 metal_list_for_each(&rdev->rp_channels, node) {
563                         rp_chnl = metal_container_of(node,
564                                 struct rpmsg_channel, node);
565                         if (rp_chnl->state == RPMSG_CHNL_STATE_IDLE) {
566                                 if (rdev->support_ns) {
567                                         if (rpmsg_send_ns_message(rdev, rp_chnl,
568                                                       RPMSG_NS_CREATE) ==
569                                                 RPMSG_SUCCESS)
570                                                 rp_chnl->state =
571                                                         RPMSG_CHNL_STATE_NS;
572                                 } else {
573                                         rp_chnl->state = RPMSG_CHNL_STATE_ACTIVE;
574                                 }
575                                 return;
576                         }
577                 }
578         }
580         metal_mutex_acquire(&rdev->lock);
582         /* Process the received data from remote node */
583         rp_hdr = (struct rpmsg_hdr *)rpmsg_get_rx_buffer(rdev, &len, &idx);
585         metal_mutex_release(&rdev->lock);
587         while (rp_hdr) {
589                 /* Get the channel node from the remote device channels list. */
590                 metal_mutex_acquire(&rdev->lock);
591                 rp_ept = rpmsg_rdev_get_endpoint_from_addr(rdev, rp_hdr->dst);
592                 metal_mutex_release(&rdev->lock);
594                 if (!rp_ept)
595                         /* Fatal error no endpoint for the given dst addr. */
596                         return;
598                 rp_chnl = rp_ept->rp_chnl;
600                 if ((rp_chnl) && (rp_chnl->state == RPMSG_CHNL_STATE_NS)) {
601                         /* First message from RPMSG Master, update channel
602                          * destination address and state */
603                         rp_chnl->dst = rp_hdr->src;
604                         rp_chnl->state = RPMSG_CHNL_STATE_ACTIVE;
606                         /* Notify channel creation to application */
607                         if (rdev->channel_created) {
608                                 rdev->channel_created(rp_chnl);
609                         }
610                 } else {
611                         rp_ept->cb(rp_chnl, (void *)RPMSG_LOCATE_DATA(rp_hdr), rp_hdr->len,
612                                    rp_ept->priv, rp_hdr->src);
613                 }
615                 metal_mutex_acquire(&rdev->lock);
617                 /* Check whether callback wants to hold buffer */
618                 if (rp_hdr->reserved & RPMSG_BUF_HELD)
619                 {
620                         /* 'rp_hdr->reserved' field is now used as storage for
621                          * 'idx' to release buffer later */
622                         reserved = (struct rpmsg_hdr_reserved*)&rp_hdr->reserved;
623                         reserved->idx = (uint16_t)idx;
624                 } else {
625                         /* Return used buffers. */
626                         rpmsg_return_buffer(rdev, rp_hdr, len, idx);
627                 }
629                 rp_hdr =
630                     (struct rpmsg_hdr *)rpmsg_get_rx_buffer(rdev, &len, &idx);
631                 metal_mutex_release(&rdev->lock);
632         }
635 /**
636  * rpmsg_ns_callback
637  *
638  * This callback handles name service announcement from the remote device
639  * and creates/deletes rpmsg channels.
640  *
641  * @param server_chnl - pointer to server channel control block.
642  * @param data        - pointer to received messages
643  * @param len         - length of received data
644  * @param priv        - any private data
645  * @param src         - source address
646  *
647  * @return - none
648  */
649 void rpmsg_ns_callback(struct rpmsg_channel *server_chnl, void *data, int len,
650                        void *priv, unsigned long src)
652         struct remote_device *rdev;
653         struct rpmsg_channel *rp_chnl;
654         struct rpmsg_ns_msg *ns_msg;
656         (void)server_chnl;
657         (void)src;
659         rdev = (struct remote_device *)priv;
661         //FIXME: This assumes same name string size for channel name both on master
662         //and remote. If this is not the case then we will have to parse the
663         //message contents.
665         ns_msg = (struct rpmsg_ns_msg *)data;
666         ns_msg->name[len - 1] = '\0';
668         if (ns_msg->flags & RPMSG_NS_DESTROY) {
669                 metal_mutex_acquire(&rdev->lock);
670                 rp_chnl = rpmsg_rdev_get_chnl_from_id(rdev, ns_msg->name);
671                 metal_mutex_release(&rdev->lock);
672                 if (rp_chnl) {
673                         if (rdev->channel_destroyed) {
674                                 rdev->channel_destroyed(rp_chnl);
675                         }
676                         rpmsg_destroy_ept(rp_chnl->rp_ept);
677                         _rpmsg_delete_channel(rp_chnl);
678                 }
679         } else {
680                 rp_chnl =
681                     _rpmsg_create_channel(rdev, ns_msg->name, 0x00,
682                                           ns_msg->addr);
683                 if (rp_chnl) {
684                         rp_chnl->state = RPMSG_CHNL_STATE_ACTIVE;
685                         /* Create default endpoint for channel */
686                         rp_chnl->rp_ept =
687                             rpmsg_create_ept(rp_chnl, rdev->default_cb, rdev,
688                                              RPMSG_ADDR_ANY);
689                         if (rp_chnl->rp_ept) {
690                                 rp_chnl->src = rp_chnl->rp_ept->addr;
691                                 /*
692                                  * Echo back the NS message to remote in order to
693                                  * complete the connection stage. Remote will know the endpoint
694                                  * address from this point onward which will enable it to send
695                                  * message without waiting for any application level message from
696                                  * master.
697                                  */
698                                 rpmsg_send(rp_chnl, data, len);
699                                 if (rdev->channel_created) {
700                                         rdev->channel_created(rp_chnl);
701                                 }
702                         }
703                 }
704         }
707 /**
708  * rpmsg_get_address
709  *
710  * This function provides unique 32 bit address.
711  *
712  * @param bitmap - bit map for addresses
713  * @param size   - size of bitmap
714  *
715  * return - a unique address
716  */
717 int rpmsg_get_address(unsigned long *bitmap, int size)
719         int addr = -1;
720         int i, tmp32;
722         /* Find first available buffer */
723         for (i = 0; i < size; i++) {
724                 tmp32 = get_first_zero_bit(bitmap[i]);
726                 if (tmp32 < 32) {
727                         addr = tmp32 + (i*32);
728                         bitmap[i] |= (1 << tmp32);
729                         break;
730                 }
731         }
733         return addr;
736 /**
737  * rpmsg_release_address
738  *
739  * Frees the given address.
740  *
741  * @param bitmap - bit map for addresses
742  * @param size   - size of bitmap
743  * @param addr   - address to free
744  *
745  * return - none
746  */
747 int rpmsg_release_address(unsigned long *bitmap, int size, int addr)
749         unsigned int i, j;
750         unsigned long mask = 1;
752         if (addr >= size * 32)
753                 return -1;
755         /* Mark the addr as available */
756         i = addr / 32;
757         j = addr % 32;
759         mask = mask << j;
760         bitmap[i] = bitmap[i] & (~mask);
762         return RPMSG_SUCCESS;
765 /**
766  * rpmsg_is_address_set
767  *
768  * Checks whether address is used or free.
769  *
770  * @param bitmap - bit map for addresses
771  * @param size   - size of bitmap
772  * @param addr   - address to free
773  *
774  * return - TRUE/FALSE
775  */
776 int rpmsg_is_address_set(unsigned long *bitmap, int size, int addr)
778         int i, j;
779         unsigned long mask = 1;
781         if (addr >= size * 32)
782                 return -1;
784         /* Mark the id as available */
785         i = addr / 32;
786         j = addr % 32;
787         mask = mask << j;
789         return (bitmap[i] & mask);
792 /**
793  * rpmsg_set_address
794  *
795  * Marks the address as consumed.
796  *
797  * @param bitmap - bit map for addresses
798  * @param size   - size of bitmap
799  * @param addr   - address to free
800  *
801  * return - none
802  */
803 int rpmsg_set_address(unsigned long *bitmap, int size, int addr)
805         int i, j;
806         unsigned long mask = 1;
808         if (addr >= size * 32)
809                 return -1;
811         /* Mark the id as available */
812         i = addr / 32;
813         j = addr % 32;
814         mask = mask << j;
815         bitmap[i] |= mask;
817         return RPMSG_SUCCESS;