source: mainline/uspace/lib/usbhost/src/dma_buffer.c@ 837d53d

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

usbhost: refactor include hiearchy

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