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 |
|
---|
42 | dma_policy_t dma_policy_default = {
|
---|
43 | .use64 = false,
|
---|
44 | .alignment = PAGE_SIZE,
|
---|
45 | };
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * Allocate a DMA buffer.
|
---|
49 | *
|
---|
50 | * @param[in] db dma_buffer_t structure to fill
|
---|
51 | * @param[in] policy dma_policy_t structure to guide
|
---|
52 | * @param[in] size Size of the required memory space
|
---|
53 | * @return Error code.
|
---|
54 | */
|
---|
55 | int dma_buffer_alloc_policy(dma_buffer_t *db, size_t size, dma_policy_t policy)
|
---|
56 | {
|
---|
57 | assert(db);
|
---|
58 |
|
---|
59 | if (policy.alignment > PAGE_SIZE)
|
---|
60 | return EINVAL;
|
---|
61 |
|
---|
62 | const size_t aligned_size = ALIGN_UP(size, policy.alignment);
|
---|
63 | const size_t real_size = ALIGN_UP(aligned_size, PAGE_SIZE);
|
---|
64 | const uintptr_t flags = policy.use64 ? 0 : DMAMEM_4GiB;
|
---|
65 |
|
---|
66 | uintptr_t phys;
|
---|
67 | void *address = AS_AREA_ANY;
|
---|
68 |
|
---|
69 | const int ret = dmamem_map_anonymous(real_size,
|
---|
70 | flags, AS_AREA_READ | AS_AREA_WRITE, 0,
|
---|
71 | &phys, &address);
|
---|
72 |
|
---|
73 | if (ret == EOK) {
|
---|
74 | /* Poison, accessing it should be enough to make sure
|
---|
75 | * the location is mapped, but poison works better */
|
---|
76 | memset(address, 0x5, real_size);
|
---|
77 | db->virt = address;
|
---|
78 | db->phys = phys;
|
---|
79 | }
|
---|
80 | return ret;
|
---|
81 | }
|
---|
82 |
|
---|
83 | /**
|
---|
84 | * Allocate a DMA buffer using the default policy.
|
---|
85 | *
|
---|
86 | * @param[in] db dma_buffer_t structure to fill
|
---|
87 | * @param[in] size Size of the required memory space
|
---|
88 | * @return Error code.
|
---|
89 | */
|
---|
90 | int dma_buffer_alloc(dma_buffer_t *db, size_t size)
|
---|
91 | {
|
---|
92 | return dma_buffer_alloc_policy(db, size, dma_policy_default);
|
---|
93 | }
|
---|
94 |
|
---|
95 |
|
---|
96 | /**
|
---|
97 | * Free a DMA buffer.
|
---|
98 | *
|
---|
99 | * @param[in] db dma_buffer_t structure buffer of which will be freed
|
---|
100 | */
|
---|
101 | void dma_buffer_free(dma_buffer_t *db)
|
---|
102 | {
|
---|
103 | if (db->virt) {
|
---|
104 | dmamem_unmap_anonymous(db->virt);
|
---|
105 | db->virt = NULL;
|
---|
106 | db->phys = 0;
|
---|
107 | }
|
---|
108 | }
|
---|
109 |
|
---|
110 | /**
|
---|
111 | * Convert a pointer inside a buffer to physical address.
|
---|
112 | *
|
---|
113 | * @param[in] db Buffer at which virt is pointing
|
---|
114 | * @param[in] virt Pointer somewhere inside db
|
---|
115 | */
|
---|
116 | uintptr_t dma_buffer_phys(const dma_buffer_t *db, void *virt)
|
---|
117 | {
|
---|
118 | return db->phys + (virt - db->virt);
|
---|
119 | }
|
---|
120 |
|
---|
121 | /**
|
---|
122 | * @}
|
---|
123 | */
|
---|