source: mainline/uspace/lib/usb/src/dma_buffer.c@ da9d6ca

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since da9d6ca was 98893ede, checked in by Ondřej Hlavatý <aearsis@…>, 8 years ago

libusb: dma_buffers optimization

  • Property mode set to 100644
File size: 3.5 KB
Line 
1/*
2 * Copyright (c) 2017 Ondrej Hlavaty <aearsis@eideo.cz>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28/** @addtogroup libusb
29 * @{
30 */
31/** @file
32 */
33
34#include <align.h>
35#include <as.h>
36#include <ddi.h>
37#include <stddef.h>
38
39#include "usb/dma_buffer.h"
40
41const dma_policy_t dma_policy_default = {
42 .use64 = false,
43 .alignment = PAGE_SIZE,
44};
45
46/**
47 * The routine of allocating a DMA buffer. Inlined to force optimization for the
48 * default policy.
49 */
50static inline int dma_buffer_alloc_internal(dma_buffer_t *db,
51 size_t size, const dma_policy_t *policy)
52{
53 assert(db);
54
55 if (policy->alignment > PAGE_SIZE)
56 return EINVAL;
57
58 const size_t aligned_size = ALIGN_UP(size, policy->alignment);
59 const size_t real_size = ALIGN_UP(aligned_size, PAGE_SIZE);
60 const uintptr_t flags = policy->use64 ? 0 : DMAMEM_4GiB;
61
62 uintptr_t phys;
63 void *address = AS_AREA_ANY;
64
65 const int ret = dmamem_map_anonymous(real_size,
66 flags, AS_AREA_READ | AS_AREA_WRITE, 0,
67 &phys, &address);
68
69 if (ret == EOK) {
70 db->virt = address;
71 db->phys = phys;
72 }
73 return ret;
74}
75
76/**
77 * Allocate a DMA buffer.
78 *
79 * @param[in] db dma_buffer_t structure to fill
80 * @param[in] size Size of the required memory space
81 * @param[in] policy dma_policy_t structure to guide
82 * @return Error code.
83 */
84int dma_buffer_alloc_policy(dma_buffer_t *db, size_t size,
85 const dma_policy_t *policy)
86{
87 return dma_buffer_alloc_internal(db, size, policy);
88}
89
90/**
91 * Allocate a DMA buffer using the default policy.
92 *
93 * @param[in] db dma_buffer_t structure to fill
94 * @param[in] size Size of the required memory space
95 * @return Error code.
96 */
97int dma_buffer_alloc(dma_buffer_t *db, size_t size)
98{
99 return dma_buffer_alloc_internal(db, size, &dma_policy_default);
100}
101
102
103/**
104 * Free a DMA buffer.
105 *
106 * @param[in] db dma_buffer_t structure buffer of which will be freed
107 */
108void dma_buffer_free(dma_buffer_t *db)
109{
110 if (db->virt) {
111 dmamem_unmap_anonymous(db->virt);
112 db->virt = NULL;
113 db->phys = 0;
114 }
115}
116
117/**
118 * Convert a pointer inside a buffer to physical address.
119 *
120 * @param[in] db Buffer at which virt is pointing
121 * @param[in] virt Pointer somewhere inside db
122 */
123uintptr_t dma_buffer_phys(const dma_buffer_t *db, void *virt)
124{
125 return db->phys + (virt - db->virt);
126}
127
128/**
129 * @}
130 */
Note: See TracBrowser for help on using the repository browser.