Changeset 1e472ee in mainline for uspace/lib/virtio/virtio.c


Ignore:
Timestamp:
2018-06-28T19:23:42Z (7 years ago)
Author:
Jakub Jermar <jakub@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
d3b2ffa
Parents:
331d024
Message:

Move DMA buffers allocation functions to libvirtio

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/virtio/virtio.c

    r331d024 r1e472ee  
    3939#include <libarch/barrier.h>
    4040
     41/** Allocate DMA buffers
     42 *
     43 * @param buffers[in]  Number of buffers to allocate.
     44 * @param size[in]     Size of each buffer.
     45 * @param write[in]    True if the buffers are writable by the driver, false
     46 *                     otherwise.
     47 * @param buf[out]     Output array holding virtual addresses of the allocated
     48 *                     buffers.
     49 * @param buf_p[out]   Output array holding physical addresses of the allocated
     50 *                     buffers.
     51 *
     52 * The buffers can be deallocated by virtio_net_teardown_bufs().
     53 *
     54 * @return  EOK on success or error code.
     55 */
     56errno_t virtio_setup_dma_bufs(unsigned int buffers, size_t size,
     57    bool write, void *buf[], uintptr_t buf_p[])
     58{
     59        /*
     60         * Allocate all buffers at once in one large chunk.
     61         */
     62        void *virt = AS_AREA_ANY;
     63        uintptr_t phys;
     64        errno_t rc = dmamem_map_anonymous(buffers * size, 0,
     65            write ? AS_AREA_WRITE : AS_AREA_READ, 0, &phys, &virt);
     66        if (rc != EOK)
     67                return rc;
     68
     69        ddf_msg(LVL_NOTE, "DMA buffers: %p-%p", virt, virt + buffers * size);
     70
     71        /*
     72         * Calculate addresses of the individual buffers for easy access.
     73         */
     74        for (unsigned i = 0; i < buffers; i++) {
     75                buf[i] = virt + i * size;
     76                buf_p[i] = phys + i * size;
     77        }
     78
     79        return EOK;
     80}
     81
     82/** Deallocate DMA buffers
     83 *
     84 * @param buf[in]  Array holding the virtual addresses of the DMA buffers
     85 *                 previously allocated by virtio_net_setup_bufs().
     86 */
     87extern void virtio_teardown_dma_bufs(void *buf[])
     88{
     89        if (buf[0]) {
     90                dmamem_unmap_anonymous(buf[0]);
     91                buf[0] = NULL;
     92        }
     93}
     94
    4195void virtio_virtq_desc_set(virtio_dev_t *vdev, uint16_t num, uint16_t descno,
    4296    uint64_t addr, uint32_t len, uint16_t flags, uint16_t next)
Note: See TracChangeset for help on using the changeset viewer.