paul@202 | 1 | /* |
paul@202 | 2 | * DMA-related memory allocation utility functions. |
paul@202 | 3 | * |
paul@264 | 4 | * Copyright (C) 2023, 2024 Paul Boddie <paul@boddie.org.uk> |
paul@202 | 5 | * |
paul@202 | 6 | * This program is free software; you can redistribute it and/or |
paul@202 | 7 | * modify it under the terms of the GNU General Public License as |
paul@202 | 8 | * published by the Free Software Foundation; either version 2 of |
paul@202 | 9 | * the License, or (at your option) any later version. |
paul@202 | 10 | * |
paul@202 | 11 | * This program is distributed in the hope that it will be useful, |
paul@202 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@202 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@202 | 14 | * GNU General Public License for more details. |
paul@202 | 15 | * |
paul@202 | 16 | * You should have received a copy of the GNU General Public License |
paul@202 | 17 | * along with this program; if not, write to the Free Software |
paul@202 | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@202 | 19 | * Boston, MA 02110-1301, USA |
paul@202 | 20 | */ |
paul@202 | 21 | |
paul@202 | 22 | #include <l4/re/c/dataspace.h> |
paul@202 | 23 | #include <l4/re/c/dma_space.h> |
paul@202 | 24 | #include <l4/re/c/mem_alloc.h> |
paul@202 | 25 | #include <l4/re/env.h> |
paul@202 | 26 | #include <l4/re/protocols.h> |
paul@202 | 27 | #include <l4/sys/factory.h> |
paul@202 | 28 | #include <l4/sys/types.h> |
paul@202 | 29 | #include <l4/vbus/vbus.h> |
paul@202 | 30 | |
paul@202 | 31 | #include <ipc/cap_alloc.h> |
paul@202 | 32 | #include <ipc/mem_ipc.h> |
paul@202 | 33 | |
paul@202 | 34 | #include "dma.h" |
paul@202 | 35 | #include "memory.h" |
paul@202 | 36 | |
paul@202 | 37 | |
paul@202 | 38 | |
paul@267 | 39 | // Obtain a DMA space to which memory regions may be associated. |
paul@202 | 40 | |
paul@267 | 41 | long get_dma_space(l4_cap_idx_t *dma) |
paul@267 | 42 | { |
paul@267 | 43 | l4_cap_idx_t vbus = l4re_env_get_cap("vbus"); |
paul@202 | 44 | |
paul@202 | 45 | if (l4_is_invalid_cap(vbus)) |
paul@202 | 46 | return -L4_ENOENT; |
paul@202 | 47 | |
paul@267 | 48 | // Obtain capabilities for the DMA region and the vbus. |
paul@267 | 49 | |
paul@267 | 50 | *dma = ipc_cap_alloc(); |
paul@267 | 51 | |
paul@267 | 52 | if (l4_is_invalid_cap(*dma)) |
paul@267 | 53 | return -L4_ENOENT; |
paul@267 | 54 | |
paul@202 | 55 | // Create the DMA space. |
paul@202 | 56 | |
paul@267 | 57 | if (l4_error(l4_factory_create(l4re_env()->mem_alloc, L4RE_PROTO_DMA_SPACE, |
paul@267 | 58 | *dma))) |
paul@202 | 59 | return -L4_ENOMEM; |
paul@202 | 60 | |
paul@202 | 61 | // Find the DMA domain and assign the DMA space. |
paul@202 | 62 | |
paul@202 | 63 | l4vbus_device_handle_t device = L4VBUS_NULL; |
paul@202 | 64 | l4vbus_resource_t dma_resource; |
paul@202 | 65 | |
paul@202 | 66 | if (!find_resource(&device, &dma_resource, L4VBUS_RESOURCE_DMA_DOMAIN)) |
paul@202 | 67 | return -L4_ENOENT; |
paul@202 | 68 | |
paul@202 | 69 | if (l4vbus_assign_dma_domain(vbus, dma_resource.start, |
paul@202 | 70 | L4VBUS_DMAD_BIND | L4VBUS_DMAD_L4RE_DMA_SPACE, |
paul@267 | 71 | *dma)) |
paul@202 | 72 | return -L4_ENOMEM; |
paul@202 | 73 | |
paul@267 | 74 | return L4_EOK; |
paul@267 | 75 | } |
paul@267 | 76 | |
paul@267 | 77 | // Obtain a memory region associated with the given DMA space. |
paul@267 | 78 | |
paul@267 | 79 | long get_dma_region_for_space(unsigned long size, int align, l4_cap_idx_t dma, |
paul@267 | 80 | struct dma_region *region) |
paul@267 | 81 | { |
paul@202 | 82 | // Allocate memory at the given alignment. |
paul@202 | 83 | |
paul@202 | 84 | const l4_size_t alloc_flags = L4RE_MA_CONTINUOUS | L4RE_MA_PINNED; |
paul@202 | 85 | const l4re_rm_flags_t attach_flags = L4RE_RM_F_SEARCH_ADDR | L4RE_RM_F_RW; |
paul@202 | 86 | |
paul@267 | 87 | region->align = 0; |
paul@267 | 88 | region->paddr = 0; |
paul@267 | 89 | region->size = 0; |
paul@267 | 90 | region->vaddr = 0; |
paul@202 | 91 | |
paul@267 | 92 | // Map the allocated memory, obtaining a virtual address. |
paul@202 | 93 | |
paul@264 | 94 | if (ipc_new_dataspace(size, alloc_flags, align, ®ion->mem)) |
paul@202 | 95 | return -L4_ENOMEM; |
paul@202 | 96 | |
paul@267 | 97 | if (ipc_attach_dataspace_align(region->mem, size, attach_flags, align, |
paul@267 | 98 | (void **) ®ion->vaddr)) |
paul@202 | 99 | return -L4_ENOMEM; |
paul@202 | 100 | |
paul@202 | 101 | // Obtain a physical address. |
paul@202 | 102 | |
paul@202 | 103 | l4_size_t size_out = size; |
paul@202 | 104 | |
paul@264 | 105 | if (l4re_dma_space_map(dma, region->mem | L4_CAP_FPAGE_RW, 0, &size_out, 0, |
paul@264 | 106 | L4RE_DMA_SPACE_TO_DEVICE, ®ion->paddr)) |
paul@202 | 107 | return -L4_ENOMEM; |
paul@202 | 108 | |
paul@202 | 109 | // Test the mapped region size. |
paul@202 | 110 | |
paul@267 | 111 | if (size_out < size) |
paul@202 | 112 | return -L4_ENOMEM; |
paul@202 | 113 | |
paul@267 | 114 | region->align = align; |
paul@264 | 115 | region->size = size_out; |
paul@264 | 116 | |
paul@202 | 117 | return L4_EOK; |
paul@202 | 118 | } |
paul@267 | 119 | |
paul@267 | 120 | // Obtain a memory region associated with its own DMA space. |
paul@267 | 121 | |
paul@267 | 122 | long get_dma_region(unsigned long size, int align, struct dma_region *region) |
paul@267 | 123 | { |
paul@267 | 124 | l4_cap_idx_t dma; |
paul@267 | 125 | long err = get_dma_space(&dma); |
paul@267 | 126 | |
paul@267 | 127 | if (err) |
paul@267 | 128 | return err; |
paul@267 | 129 | |
paul@267 | 130 | return get_dma_region_for_space(size, align, dma, region); |
paul@267 | 131 | } |