1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
|
/* IBM_PROLOG_BEGIN_TAG */
/* This is an automatically generated prolog. */
/* */
/* $Source: src/usr/mbox/mbox_dma_buffer.C $ */
/* */
/* IBM CONFIDENTIAL */
/* */
/* COPYRIGHT International Business Machines Corp. 2012 */
/* */
/* p1 */
/* */
/* Object Code Only (OCO) source materials */
/* Licensed Internal Code Source Materials */
/* IBM HostBoot Licensed Internal Code */
/* */
/* The source code for this program is not published or otherwise */
/* divested of its trade secrets, irrespective of what has been */
/* deposited with the U.S. Copyright Office. */
/* */
/* Origin: 30 */
/* */
/* IBM_PROLOG_END_TAG */
#include "mbox_dma_buffer.H"
#include <stdlib.h>
#include <assert.h>
#include <util/align.H>
#include <trace/interface.H>
#include <kernel/pagemgr.H>
#define ALIGN_DMAPAGE(u) (((u) + (VmmManager::MBOX_DMA_PAGESIZE-1)) & \
~(VmmManager::MBOX_DMA_PAGESIZE-1))
using namespace MBOX;
// Defined in mboxdd.C
extern trace_desc_t * g_trac_mbox;
DmaBuffer::DmaBuffer() :
iv_head(NULL),
iv_dir(makeMask(VmmManager::MBOX_DMA_PAGES))
{
iv_head = reinterpret_cast<void*>(VmmManager::MBOX_DMA_ADDR);
iv_phys_head = mm_virt_to_phys(iv_head);
}
DmaBuffer::~DmaBuffer()
{
}
void DmaBuffer::release(void * i_buffer, size_t i_size)
{
if(!i_buffer)
{
return;
}
// Make sure this buffer falls inside the DMA space
// If not then it's not a DMA buffer - exit.
if(i_buffer < iv_head ||
i_buffer >= (static_cast<uint8_t*>(iv_head) +
(VmmManager::MBOX_DMA_PAGES *
VmmManager::MBOX_DMA_PAGESIZE)))
{
TRACDCOMP(g_trac_mbox,
ERR_MRK"MBOX DMA buffer address %p out of range",
i_buffer
);
return;
}
// Calculate the # of chunks
size_t chunks = ALIGN_DMAPAGE(i_size)/VmmManager::MBOX_DMA_PAGESIZE;
uint64_t offset =
(static_cast<uint8_t *>(i_buffer) - static_cast<uint8_t *>(iv_head)) /
VmmManager::MBOX_DMA_PAGESIZE;
// makeMask will assert if chunks > total possible # of chunks in the dir
uint64_t mask = makeMask(chunks);
mask >>= offset;
iv_dir |= mask;
TRACDCOMP(g_trac_mbox,"MBOX DMA free dir: %016lx",iv_dir);
}
void DmaBuffer::addBuffers(uint64_t i_map)
{
iv_dir |= i_map;
TRACDCOMP(g_trac_mbox,"MBOXDMA addBuffers. dir: %016lx",iv_dir);
}
void * DmaBuffer::getBuffer(uint64_t & io_size)
{
// Note: Due to this check, makeMask() will never assert while trying to
// make a mask for the requested size. Instead, getBuffer will just return
// NULL if the requested size is larger than the total possible DMA buffer
// space.
if(io_size == 0 ||
io_size > (VmmManager::MBOX_DMA_PAGES * VmmManager::MBOX_DMA_PAGESIZE))
{
io_size = 0;
return NULL;
}
void * r_addr = NULL;
size_t chunks = ALIGN_DMAPAGE(io_size)/VmmManager::MBOX_DMA_PAGESIZE;
size_t start_page = 0;
size_t shift_count = (MAX_MASK_SIZE + 1) - chunks;
uint64_t mask = makeMask(chunks);
io_size = 0;
// look for a contiguous block of DMA space.
// If shift_count goes to zero, the request could not be granted.
while(shift_count)
{
if((mask & iv_dir) == mask)
{
break;
}
mask >>= 1;
++start_page;
--shift_count;
}
if(shift_count)
{
iv_dir &= ~mask;
io_size = mask;
uint64_t offset = start_page * VmmManager::MBOX_DMA_PAGESIZE;
r_addr = static_cast<void*>(static_cast<uint8_t*>(iv_head) + offset);
}
TRACDCOMP(g_trac_mbox,"MBOX DMA allocate dir: %016lx",iv_dir);
return r_addr;
}
uint64_t DmaBuffer::makeMask(uint64_t i_size)
{
assert(i_size <= MAX_MASK_SIZE);
uint64_t mask = 0;
// For some reason (1ul << 64) returns 1, not zero
// The math function pow() converts things to float then back again - bad
if(i_size < MAX_MASK_SIZE)
{
mask = 1ul << i_size;
}
mask -= 1;
mask <<= MAX_MASK_SIZE - i_size;
return mask;
}
|