paul@123 | 1 | /* |
paul@123 | 2 | * Test DMA transfers. |
paul@123 | 3 | * |
paul@123 | 4 | * Copyright (C) 2018, 2020, 2021 Paul Boddie <paul@boddie.org.uk> |
paul@123 | 5 | * |
paul@123 | 6 | * This program is free software; you can redistribute it and/or |
paul@123 | 7 | * modify it under the terms of the GNU General Public License as |
paul@123 | 8 | * published by the Free Software Foundation; either version 2 of |
paul@123 | 9 | * the License, or (at your option) any later version. |
paul@123 | 10 | * |
paul@123 | 11 | * This program is distributed in the hope that it will be useful, |
paul@123 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@123 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@123 | 14 | * GNU General Public License for more details. |
paul@123 | 15 | * |
paul@123 | 16 | * You should have received a copy of the GNU General Public License |
paul@123 | 17 | * along with this program; if not, write to the Free Software |
paul@123 | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@123 | 19 | * Boston, MA 02110-1301, USA |
paul@123 | 20 | */ |
paul@123 | 21 | |
paul@123 | 22 | #include <l4/devices/cpm-jz4730.h> |
paul@123 | 23 | #include <l4/devices/dma-jz4730.h> |
paul@123 | 24 | #include <l4/devices/memory.h> |
paul@123 | 25 | |
paul@123 | 26 | #include <l4/re/c/util/cap_alloc.h> |
paul@123 | 27 | #include <l4/re/c/dataspace.h> |
paul@123 | 28 | #include <l4/re/c/mem_alloc.h> |
paul@123 | 29 | #include <l4/re/c/rm.h> |
paul@123 | 30 | |
paul@123 | 31 | #include <l4/sys/factory.h> |
paul@123 | 32 | #include <l4/sys/icu.h> |
paul@123 | 33 | #include <l4/sys/irq.h> |
paul@123 | 34 | #include <l4/sys/rcv_endpoint.h> |
paul@123 | 35 | |
paul@123 | 36 | #include <stdio.h> |
paul@123 | 37 | #include <string.h> |
paul@123 | 38 | #include <unistd.h> |
paul@123 | 39 | |
paul@123 | 40 | |
paul@123 | 41 | |
paul@123 | 42 | /* Device and resource discovery. */ |
paul@123 | 43 | |
paul@123 | 44 | static long item_in_range(long start, long end, long index) |
paul@123 | 45 | { |
paul@123 | 46 | if (start < end) |
paul@123 | 47 | return start + index; |
paul@123 | 48 | else |
paul@123 | 49 | return start - index; |
paul@123 | 50 | } |
paul@123 | 51 | |
paul@123 | 52 | |
paul@123 | 53 | |
paul@123 | 54 | int main(void) |
paul@123 | 55 | { |
paul@123 | 56 | void *cpm; |
paul@123 | 57 | void *dma, *dma0; |
paul@123 | 58 | |
paul@123 | 59 | /* Allocate memory to test transfers. */ |
paul@123 | 60 | |
paul@123 | 61 | l4_cap_idx_t ds0_mem, ds1_mem; |
paul@123 | 62 | l4_size_t ds0_size = L4_PAGESIZE, ds0_psize, ds1_size = L4_PAGESIZE, ds1_psize; |
paul@123 | 63 | l4_addr_t ds0_addr, ds0_paddr, ds1_addr, ds1_paddr; |
paul@123 | 64 | |
paul@123 | 65 | ds0_mem = l4re_util_cap_alloc(); |
paul@123 | 66 | ds1_mem = l4re_util_cap_alloc(); |
paul@123 | 67 | |
paul@123 | 68 | if (l4_is_invalid_cap(ds0_mem) || l4_is_invalid_cap(ds1_mem)) |
paul@123 | 69 | { |
paul@123 | 70 | printf("Could not allocate memory capabilities.\n"); |
paul@123 | 71 | return 1; |
paul@123 | 72 | } |
paul@123 | 73 | |
paul@123 | 74 | if (l4re_ma_alloc_align(ds0_size, ds0_mem, L4RE_MA_CONTINUOUS | L4RE_MA_PINNED, 8) || |
paul@123 | 75 | l4re_ma_alloc_align(ds1_size, ds1_mem, L4RE_MA_CONTINUOUS | L4RE_MA_PINNED, 8)) |
paul@123 | 76 | { |
paul@123 | 77 | printf("Could not allocate memory.\n"); |
paul@123 | 78 | return 1; |
paul@123 | 79 | } |
paul@123 | 80 | |
paul@123 | 81 | if (l4re_rm_attach((void **) &ds0_addr, ds0_size, |
paul@123 | 82 | L4RE_RM_SEARCH_ADDR | L4RE_RM_EAGER_MAP, |
paul@123 | 83 | ds0_mem, 0, L4_PAGESHIFT) || |
paul@123 | 84 | l4re_rm_attach((void **) &ds1_addr, ds1_size, |
paul@123 | 85 | L4RE_RM_SEARCH_ADDR | L4RE_RM_EAGER_MAP, |
paul@123 | 86 | ds1_mem, 0, L4_PAGESHIFT)) |
paul@123 | 87 | { |
paul@123 | 88 | printf("Could not map memory.\n"); |
paul@123 | 89 | return 1; |
paul@123 | 90 | } |
paul@123 | 91 | |
paul@123 | 92 | if (l4re_ds_phys(ds0_mem, 0, &ds0_paddr, &ds0_psize) || |
paul@123 | 93 | l4re_ds_phys(ds1_mem, 0, &ds1_paddr, &ds1_psize)) |
paul@123 | 94 | { |
paul@123 | 95 | printf("Could not get physical addresses for memory.\n"); |
paul@123 | 96 | return 1; |
paul@123 | 97 | } |
paul@123 | 98 | |
paul@123 | 99 | /* Fill the allocated memory. */ |
paul@123 | 100 | |
paul@123 | 101 | memset((void *) ds0_addr, 0, ds0_size); |
paul@123 | 102 | memset((void *) ds1_addr, 0, ds1_size); |
paul@123 | 103 | |
paul@123 | 104 | sprintf((char *) ds0_addr, "The quick brown fox jumped over the lazy dog.\n"); |
paul@123 | 105 | |
paul@123 | 106 | /* Interrupts. */ |
paul@123 | 107 | |
paul@123 | 108 | l4_uint32_t dma_irq_start = 0, dma_irq_end = 0; |
paul@123 | 109 | l4_cap_idx_t icucap, irq0cap; |
paul@123 | 110 | |
paul@123 | 111 | /* Obtain resource details describing the interrupt for DMA channel 0. */ |
paul@123 | 112 | |
paul@123 | 113 | printf("Access IRQ...\n"); |
paul@123 | 114 | |
paul@123 | 115 | if (get_irq("jz4730-dma", &dma_irq_start, &dma_irq_end) < 0) |
paul@123 | 116 | return 1; |
paul@123 | 117 | |
paul@123 | 118 | printf("IRQ range at %d...%d.\n", dma_irq_start, dma_irq_end); |
paul@123 | 119 | |
paul@123 | 120 | /* Obtain capabilities for the interrupt controller and an interrupt. */ |
paul@123 | 121 | |
paul@123 | 122 | irq0cap = l4re_util_cap_alloc(); |
paul@123 | 123 | icucap = l4re_env_get_cap("icu"); |
paul@123 | 124 | |
paul@123 | 125 | if (l4_is_invalid_cap(icucap)) |
paul@123 | 126 | { |
paul@123 | 127 | printf("No 'icu' capability available in the virtual bus.\n"); |
paul@123 | 128 | return 1; |
paul@123 | 129 | } |
paul@123 | 130 | |
paul@123 | 131 | if (l4_is_invalid_cap(irq0cap)) |
paul@123 | 132 | { |
paul@123 | 133 | printf("Capabilities not available for interrupts.\n"); |
paul@123 | 134 | return 1; |
paul@123 | 135 | } |
paul@123 | 136 | |
paul@123 | 137 | /* Create interrupt objects. */ |
paul@123 | 138 | |
paul@123 | 139 | long err; |
paul@123 | 140 | |
paul@123 | 141 | err = l4_error(l4_factory_create_irq(l4re_global_env->factory, irq0cap)); |
paul@123 | 142 | |
paul@123 | 143 | if (err) |
paul@123 | 144 | { |
paul@123 | 145 | printf("Could not create IRQ object: %lx\n", err); |
paul@123 | 146 | return 1; |
paul@123 | 147 | } |
paul@123 | 148 | |
paul@123 | 149 | /* Bind interrupt objects to IRQ numbers. */ |
paul@123 | 150 | |
paul@123 | 151 | err = l4_error(l4_icu_bind(icucap, |
paul@123 | 152 | item_in_range(dma_irq_start, dma_irq_end, 0), |
paul@123 | 153 | irq0cap)); |
paul@123 | 154 | |
paul@123 | 155 | if (err) |
paul@123 | 156 | { |
paul@123 | 157 | printf("Could not bind IRQ to the ICU: %ld\n", err); |
paul@123 | 158 | return 1; |
paul@123 | 159 | } |
paul@123 | 160 | |
paul@123 | 161 | /* Attach ourselves to the interrupt handler. */ |
paul@123 | 162 | |
paul@123 | 163 | err = l4_error(l4_rcv_ep_bind_thread(irq0cap, l4re_env()->main_thread, 0)); |
paul@123 | 164 | |
paul@123 | 165 | if (err) |
paul@123 | 166 | { |
paul@123 | 167 | printf("Could not attach to IRQs: %ld\n", err); |
paul@123 | 168 | return 1; |
paul@123 | 169 | } |
paul@123 | 170 | |
paul@123 | 171 | /* Peripheral memory. */ |
paul@123 | 172 | |
paul@123 | 173 | l4_addr_t cpm_base = 0, cpm_base_end = 0; |
paul@123 | 174 | l4_addr_t dma_base = 0, dma_base_end = 0; |
paul@123 | 175 | |
paul@123 | 176 | /* Obtain resource details describing I/O memory. */ |
paul@123 | 177 | |
paul@123 | 178 | printf("Access CPM...\n"); |
paul@123 | 179 | |
paul@123 | 180 | if (get_memory("jz4730-cpm", &cpm_base, &cpm_base_end) < 0) |
paul@123 | 181 | return 1; |
paul@123 | 182 | |
paul@123 | 183 | printf("CPM at 0x%lx...0x%lx.\n", cpm_base, cpm_base_end); |
paul@123 | 184 | |
paul@123 | 185 | printf("Access DMA...\n"); |
paul@123 | 186 | |
paul@123 | 187 | if (get_memory("jz4730-dma", &dma_base, &dma_base_end) < 0) |
paul@123 | 188 | return 1; |
paul@123 | 189 | |
paul@123 | 190 | printf("DMA at 0x%lx...0x%lx.\n", dma_base, dma_base_end); |
paul@123 | 191 | |
paul@123 | 192 | /* Obtain CPM and DMA references. */ |
paul@123 | 193 | |
paul@123 | 194 | cpm = jz4730_cpm_init(cpm_base); |
paul@123 | 195 | dma = jz4730_dma_init(dma_base, dma_base_end, cpm); |
paul@123 | 196 | dma0 = jz4730_dma_get_channel(dma, 0, irq0cap); |
paul@123 | 197 | |
paul@123 | 198 | /* Enable DMA. */ |
paul@123 | 199 | |
paul@123 | 200 | printf("Enable DMA...\n"); |
paul@123 | 201 | |
paul@123 | 202 | jz4730_dma_enable(dma); |
paul@123 | 203 | |
paul@123 | 204 | /* Transfer data between the allocated memory regions. */ |
paul@123 | 205 | |
paul@123 | 206 | printf("Transfer from %lx to %lx...\n", ds0_paddr, ds1_paddr); |
paul@123 | 207 | |
paul@123 | 208 | unsigned int ntransferred = jz4730_dma_transfer(dma0, (uint32_t) ds0_paddr, |
paul@123 | 209 | (uint32_t) ds1_paddr, |
paul@123 | 210 | L4_PAGESIZE / 4, |
paul@123 | 211 | Dma_trans_unit_size_32_bit, |
paul@123 | 212 | Dma_request_auto); |
paul@123 | 213 | |
paul@123 | 214 | printf("Transferred: %d\n", ntransferred); |
paul@123 | 215 | printf("Source: %s\n", (char *) ds0_addr); |
paul@123 | 216 | printf("Destination: %s\n", (char *) ds1_addr); |
paul@123 | 217 | |
paul@123 | 218 | /* Detach from the interrupt. */ |
paul@123 | 219 | |
paul@123 | 220 | err = l4_error(l4_irq_detach(irq0cap)); |
paul@123 | 221 | |
paul@123 | 222 | if (err) |
paul@123 | 223 | printf("Error detaching from IRQ objects: %ld\n", err); |
paul@123 | 224 | |
paul@123 | 225 | return 0; |
paul@123 | 226 | } |