Skip to content

Commit 0029d0d

Browse files
dmarionflorincoras
authored andcommitted
dev: allow drivers to specify DMA address space constraints
Drivers can optionally set dma_address_space_bits to declare their DMA address width. Verify all buffer pool physmem maps against this constraint during device initialization. Type: feature Change-Id: I9320aec523ee1cd274d073422af09465802c1da4 Signed-off-by: Damjan Marion <damarion@cisco.com>
1 parent f0ef551 commit 0029d0d

2 files changed

Lines changed: 73 additions & 0 deletions

File tree

‎src/vnet/dev/dev.c‎

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
#include <vnet/dev/dev.h>
99
#include <vnet/dev/log.h>
1010
#include <vnet/dev/counters.h>
11+
#include <vlib/buffer_funcs.h>
12+
#include <vlib/physmem_funcs.h>
1113
#include <vppinfra/unix.h>
1214
#include <dlfcn.h>
1315
#include <dirent.h>
@@ -160,6 +162,72 @@ vnet_dev_cleanup_bus_resources (vlib_main_t *vm, vnet_dev_t *dev)
160162
vec_reset_length (dev->dma_allocs);
161163
}
162164

165+
static int
166+
vnet_dev_dma_range_fits (u64 dma_start, u64 size, u64 dma_max)
167+
{
168+
return size && dma_start <= dma_max && size - 1 <= dma_max - dma_start;
169+
}
170+
171+
static vnet_dev_rv_t
172+
vnet_dev_check_dma_address_space (vlib_main_t *vm, vnet_dev_t *dev)
173+
{
174+
vnet_dev_main_t *dm = &vnet_dev_main;
175+
vnet_dev_driver_t *driver = pool_elt_at_index (dm->drivers, dev->driver_index);
176+
vlib_buffer_pool_t *buffer_pool;
177+
vlib_physmem_map_t *map;
178+
u8 dma_bits = driver->registration->device.dma_address_space_bits;
179+
u64 dma_max;
180+
u64 dma_start;
181+
u64 size;
182+
u32 i;
183+
184+
if (dma_bits == 0)
185+
return VNET_DEV_OK;
186+
if (dma_bits > 64)
187+
{
188+
log_err (dev, "invalid DMA address space size %u bits", dma_bits);
189+
return VNET_DEV_ERR_NOT_SUPPORTED;
190+
}
191+
192+
dma_max = dma_bits == 64 ? ~0ULL : (1ULL << dma_bits) - 1;
193+
194+
vec_foreach (buffer_pool, vm->buffer_main->buffer_pools)
195+
{
196+
map = vlib_physmem_get_map (vm, buffer_pool->physmem_map_index);
197+
if (dev->va_dma)
198+
{
199+
dma_start = pointer_to_uword (map->base);
200+
size = (u64) map->n_pages << map->log2_page_size;
201+
if (!vnet_dev_dma_range_fits (dma_start, size, dma_max))
202+
{
203+
log_err (
204+
dev,
205+
"VPP buffer pool %u physmem map %u DMA range at 0x%llx with size %llu exceeds "
206+
"the %u-bit device address space",
207+
buffer_pool->index, buffer_pool->physmem_map_index, dma_start, size, dma_bits);
208+
return VNET_DEV_ERR_NOT_SUPPORTED;
209+
}
210+
}
211+
else
212+
for (i = 0; i < vec_len (map->page_table); i++)
213+
{
214+
dma_start = map->page_table[i];
215+
size = 1ULL << map->log2_page_size;
216+
if (!vnet_dev_dma_range_fits (dma_start, size, dma_max))
217+
{
218+
log_err (dev,
219+
"VPP buffer pool %u physmem map %u page %u DMA range at 0x%llx with size "
220+
"%llu exceeds the %u-bit device address space",
221+
buffer_pool->index, buffer_pool->physmem_map_index, i, dma_start, size,
222+
dma_bits);
223+
return VNET_DEV_ERR_NOT_SUPPORTED;
224+
}
225+
}
226+
}
227+
228+
return VNET_DEV_OK;
229+
}
230+
163231
vnet_dev_rv_t
164232
vnet_dev_init (vlib_main_t *vm, vnet_dev_t *dev)
165233
{
@@ -172,6 +240,10 @@ vnet_dev_init (vlib_main_t *vm, vnet_dev_t *dev)
172240
if ((rv = bus->ops.device_open (vm, dev)) != VNET_DEV_OK)
173241
return rv;
174242

243+
rv = vnet_dev_check_dma_address_space (vm, dev);
244+
if (rv != VNET_DEV_OK)
245+
goto failed;
246+
175247
if (dev->ops.alloc)
176248
{
177249
rv = dev->ops.alloc (vm, dev);

‎src/vnet/dev/dev.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,7 @@ struct vnet_dev_driver_registration
496496
vnet_dev_ops_t ops;
497497
clib_arg_t *args;
498498
u16 data_sz;
499+
u8 dma_address_space_bits;
499500
} device;
500501
struct
501502
{

0 commit comments

Comments
 (0)