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

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

usb: dma_buffer check policy function

  • Property mode set to 100644
File size: 4.9 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 .flags = DMA_POLICY_F_4GiB | DMA_POLICY_F_CONTIGUOUS,
43};
44
45/**
46 * The routine of allocating a DMA buffer. Inlined to force optimization for the
47 * default policy.
48 *
49 * FIXME: We ignore the non-presence of contiguous flag, for now.
50 */
51static inline int dma_buffer_alloc_internal(dma_buffer_t *db,
52 size_t size, const dma_policy_t *policy)
53{
54 assert(db);
55
56 const size_t real_size = ALIGN_UP(size, PAGE_SIZE);
57 const bool need_4gib = !!(policy->flags & DMA_POLICY_F_4GiB);
58
59 const uintptr_t flags = need_4gib ? DMAMEM_4GiB : 0;
60
61 uintptr_t phys;
62 void *address = AS_AREA_ANY;
63
64 const int ret = dmamem_map_anonymous(real_size,
65 flags, AS_AREA_READ | AS_AREA_WRITE, 0,
66 &phys, &address);
67
68 if (ret == EOK) {
69 db->virt = address;
70 db->phys = phys;
71 }
72 return ret;
73}
74
75/**
76 * Allocate a DMA buffer.
77 *
78 * @param[in] db dma_buffer_t structure to fill
79 * @param[in] size Size of the required memory space
80 * @param[in] policy dma_policy_t structure to guide
81 * @return Error code.
82 */
83int dma_buffer_alloc_policy(dma_buffer_t *db, size_t size,
84 const dma_policy_t *policy)
85{
86 return dma_buffer_alloc_internal(db, size, policy);
87}
88
89/**
90 * Allocate a DMA buffer using the default policy.
91 *
92 * @param[in] db dma_buffer_t structure to fill
93 * @param[in] size Size of the required memory space
94 * @return Error code.
95 */
96int dma_buffer_alloc(dma_buffer_t *db, size_t size)
97{
98 return dma_buffer_alloc_internal(db, size, &dma_policy_default);
99}
100
101
102/**
103 * Free a DMA buffer.
104 *
105 * @param[in] db dma_buffer_t structure buffer of which will be freed
106 */
107void dma_buffer_free(dma_buffer_t *db)
108{
109 if (db->virt) {
110 dmamem_unmap_anonymous(db->virt);
111 db->virt = NULL;
112 db->phys = 0;
113 }
114}
115
116/**
117 * Convert a pointer inside a buffer to physical address.
118 *
119 * @param[in] db Buffer at which virt is pointing
120 * @param[in] virt Pointer somewhere inside db
121 */
122uintptr_t dma_buffer_phys(const dma_buffer_t *db, void *virt)
123{
124 return db->phys + (virt - db->virt);
125}
126
127/**
128 * Check whether a memory area is compatible with a policy.
129 *
130 * Useful to skip copying, if the buffer is already ready to be given to
131 * hardware.
132 */
133bool dma_buffer_check_policy(const void *buffer, size_t size, dma_policy_t *policy)
134{
135 /* Buffer must be always page aligned */
136 if (((uintptr_t) buffer) % PAGE_SIZE)
137 goto violated;
138
139 const bool check_4gib = !!(policy->flags & DMA_POLICY_F_4GiB);
140 const bool check_contiguous = !!(policy->flags & DMA_POLICY_F_CONTIGUOUS);
141
142 /*
143 * For these conditions, we need to walk through pages and check
144 * physical address of each one
145 */
146 if (check_contiguous || check_4gib) {
147 const void * virt = buffer;
148 uintptr_t phys;
149
150 /* Get the mapping of the first page */
151 if (as_get_physical_mapping(virt, &phys))
152 goto error;
153
154 /* First page can already break 4GiB condition */
155 if (check_4gib && (phys & DMAMEM_4GiB) != 0)
156 goto violated;
157
158 while (size <= PAGE_SIZE) {
159 /* Move to the next page */
160 virt += PAGE_SIZE;
161 size -= PAGE_SIZE;
162
163 uintptr_t last_phys = phys;
164 if (as_get_physical_mapping(virt, &phys))
165 goto error;
166
167 if (check_contiguous && (phys - last_phys) != PAGE_SIZE)
168 goto violated;
169
170 if (check_4gib && (phys & DMAMEM_4GiB) != 0)
171 goto violated;
172 }
173 }
174
175 /* All checks passed */
176 return true;
177
178violated:
179error:
180 return false;
181}
182
183/**
184 * @}
185 */
Note: See TracBrowser for help on using the repository browser.