1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/pkg/landfall-examples/msc_block_server/msc_block_server.cc Tue Feb 27 17:27:25 2024 +0100
1.3 @@ -0,0 +1,264 @@
1.4 +/*
1.5 + * A dataspace server exposing regions of a MMC/SD card.
1.6 + *
1.7 + * Copyright (C) 2020, 2021, 2023, 2024 Paul Boddie <paul@boddie.org.uk>
1.8 + *
1.9 + * This program is free software; you can redistribute it and/or
1.10 + * modify it under the terms of the GNU General Public License as
1.11 + * published by the Free Software Foundation; either version 2 of
1.12 + * the License, or (at your option) any later version.
1.13 + *
1.14 + * This program is distributed in the hope that it will be useful,
1.15 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.16 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.17 + * GNU General Public License for more details.
1.18 + *
1.19 + * You should have received a copy of the GNU General Public License
1.20 + * along with this program; if not, write to the Free Software
1.21 + * Foundation, Inc., 51 Franklin Street, Fifth Floor,
1.22 + * Boston, MA 02110-1301, USA
1.23 + */
1.24 +
1.25 +#include <l4/devices/cpm-x1600.h>
1.26 +#include <l4/devices/dma.h>
1.27 +#include <l4/devices/dma-x1600.h>
1.28 +#include <l4/devices/memory.h>
1.29 +#include <l4/devices/msc-x1600.h>
1.30 +
1.31 +#include <l4/re/c/util/cap_alloc.h>
1.32 +#include <l4/sys/debugger.h>
1.33 +#include <l4/sys/err.h>
1.34 +#include <l4/sys/factory.h>
1.35 +#include <l4/sys/icu.h>
1.36 +#include <l4/sys/ipc.h>
1.37 +#include <l4/sys/irq.h>
1.38 +
1.39 +#include <ipc/thread.h>
1.40 +
1.41 +#include <stdio.h>
1.42 +#include <stdlib.h>
1.43 +
1.44 +#include <mem/memory_incremental.h>
1.45 +#include <fsserver/page_queue_shared.h>
1.46 +#include <fsserver/pages.h>
1.47 +#include <resource/resource_server.h>
1.48 +#include <systypes/env.h>
1.49 +
1.50 +#include "msc_region_opener.h"
1.51 +#include "msc_region_operations.h"
1.52 +
1.53 +
1.54 +
1.55 +/* Common configuration. */
1.56 +
1.57 +static l4_cap_idx_t icucap;
1.58 +
1.59 +
1.60 +
1.61 +/* Device and resource discovery. */
1.62 +
1.63 +static long item_in_range(long start, long end, long index)
1.64 +{
1.65 + if (start < end)
1.66 + return start + index;
1.67 + else if (start > end)
1.68 + return start - index;
1.69 + else
1.70 + return start;
1.71 +}
1.72 +
1.73 +
1.74 +
1.75 +/* Common functions. */
1.76 +
1.77 +static int init_irq(int num, l4_cap_idx_t irq, l4_uint32_t start, l4_uint32_t end)
1.78 +{
1.79 + /* Create interrupt object. */
1.80 +
1.81 + long err = l4_error(l4_factory_create_irq(l4re_global_env->factory, irq));
1.82 +
1.83 + if (err)
1.84 + {
1.85 + printf("Could not create IRQ object: %ld\n", err);
1.86 + return 1;
1.87 + }
1.88 +
1.89 + /* Bind interrupt objects to IRQ numbers. */
1.90 +
1.91 + err = l4_error(l4_icu_bind(icucap,
1.92 + item_in_range(start, end, num),
1.93 + irq));
1.94 +
1.95 + if (err)
1.96 + {
1.97 + printf("Could not bind IRQ to the ICU: %ld\n", err);
1.98 + return 1;
1.99 + }
1.100 +
1.101 + return 0;
1.102 +}
1.103 +
1.104 +
1.105 +
1.106 +/* Peripheral resources. */
1.107 +
1.108 +static Cpm_x1600_chip *cpm;
1.109 +static Dma_x1600_chip *dma;
1.110 +static Msc_x1600_chip *msc;
1.111 +
1.112 +/* Obtain an abstraction for the memory card. */
1.113 +
1.114 +static MscRegionOperations *get_msc_region_operations(int msc_channel_num,
1.115 + int dma_channel_num,
1.116 + int card)
1.117 +{
1.118 + l4_addr_t cpm_base = 0, cpm_base_end = 0;
1.119 + l4_addr_t dma_base = 0, dma_base_end = 0;
1.120 + l4_addr_t msc_base = 0, msc_base_end = 0;
1.121 + l4_addr_t msc_phys_base = 0, msc_phys_base_end = 0;
1.122 + l4_uint32_t dma_irq_start = 0, dma_irq_end = 0;
1.123 + l4_uint32_t msc_irq_start = 0, msc_irq_end = 0;
1.124 +
1.125 + icucap = l4re_env_get_cap("icu");
1.126 +
1.127 + /* Obtain resource details describing I/O memory. */
1.128 +
1.129 + if (get_memory("x1600-cpm", &cpm_base, &cpm_base_end) < 0)
1.130 + return NULL;
1.131 +
1.132 + cpm = new Cpm_x1600_chip(cpm_base);
1.133 +
1.134 + if (get_memory("x1600-dma", &dma_base, &dma_base_end) < 0)
1.135 + return NULL;
1.136 +
1.137 + dma = new Dma_x1600_chip(dma_base, dma_base_end, cpm);
1.138 +
1.139 + if (get_irq("x1600-dma", &dma_irq_start, &dma_irq_end) < 0)
1.140 + return NULL;
1.141 +
1.142 + l4_cap_idx_t dma_irq = l4re_util_cap_alloc();
1.143 +
1.144 + if (init_irq(0, dma_irq, dma_irq_start, dma_irq_end))
1.145 + return NULL;
1.146 +
1.147 + dma->enable();
1.148 +
1.149 + if (get_memory_complete("x1600-msc", &msc_base, &msc_base_end,
1.150 + &msc_phys_base, &msc_phys_base_end) < 0)
1.151 + return NULL;
1.152 +
1.153 + msc = new Msc_x1600_chip(msc_phys_base, msc_base, msc_base_end, cpm);
1.154 +
1.155 + if (get_irq("x1600-msc", &msc_irq_start, &msc_irq_end) < 0)
1.156 + return NULL;
1.157 +
1.158 + l4_cap_idx_t msc_irq = l4re_util_cap_alloc();
1.159 +
1.160 + if (init_irq(msc_channel_num, msc_irq, msc_irq_start, msc_irq_end))
1.161 + return NULL;
1.162 +
1.163 + Dma_x1600_channel *dma_channel = dma->get_channel(dma_channel_num, dma_irq);
1.164 + Msc_channel *msc_channel = msc->get_channel(msc_channel_num, msc_irq, dma_channel);
1.165 +
1.166 + msc_channel->enable();
1.167 +
1.168 + if (card >= msc_channel->num_cards())
1.169 + return NULL;
1.170 +
1.171 + struct dma_region region;
1.172 +
1.173 + if (get_dma_region(512, 12, ®ion))
1.174 + return NULL;
1.175 +
1.176 + return new MscRegionOperations(msc_channel, card, region);
1.177 +}
1.178 +
1.179 +
1.180 +
1.181 +/* Default number of pages for files. */
1.182 +
1.183 +const unsigned int MEMORY_PAGES = 20;
1.184 +
1.185 +
1.186 +
1.187 +/* Server program. */
1.188 +
1.189 +int main(int argc, char *argv[])
1.190 +{
1.191 + l4_debugger_set_object_name(l4re_env()->main_thread, "block_server");
1.192 + long err;
1.193 +
1.194 + /* Introduce concurrency control. */
1.195 +
1.196 + err = ipc_thread_init();
1.197 +
1.198 + if (err)
1.199 + {
1.200 + printf("Initialisation error: %s\n", l4sys_errtostr(err));
1.201 + return 1;
1.202 + }
1.203 +
1.204 + if (argc < 4)
1.205 + {
1.206 + printf("Need a MSC channel/peripheral number, a DMA channel number, " \
1.207 + "and a card number.\n\n" \
1.208 + "A number of memory pages can be indicated for the use of the " \
1.209 + "server.\n\n" \
1.210 + "A named capability from the environment can be specified.\n");
1.211 + return 1;
1.212 + }
1.213 +
1.214 + int msc_channel_num = atoi(argv[1]);
1.215 + int dma_channel_num = atoi(argv[2]);
1.216 + int card = atoi(argv[3]);
1.217 +
1.218 + unsigned int memory_pages = MEMORY_PAGES;
1.219 +
1.220 + if (argc > 4)
1.221 + memory_pages = atoi(argv[4]);
1.222 +
1.223 + const char *server_name = (argc > 5) ? argv[5] : ENV_FILESYSTEM_SERVER_NAME;
1.224 +
1.225 + /* Obtain a DMA space for associating allocated memory with physical
1.226 + addresses. */
1.227 +
1.228 + l4_cap_idx_t dma_space;
1.229 +
1.230 + err = get_dma_space(&dma_space);
1.231 +
1.232 + if (err)
1.233 + {
1.234 + printf("Could not obtain DMA space: %s\n", l4sys_errtostr(err));
1.235 + return 1;
1.236 + }
1.237 +
1.238 + MscRegionOperations *ops = get_msc_region_operations(msc_channel_num,
1.239 + dma_channel_num,
1.240 + card);
1.241 +
1.242 + if (ops == NULL)
1.243 + {
1.244 + printf("Could not access memory card peripheral.\n");
1.245 + return 1;
1.246 + }
1.247 +
1.248 + /* Some memory plus infrastructure. */
1.249 +
1.250 + MemoryIncremental mem(memory_pages, PAGE_SIZE, dma_space);
1.251 + PageQueueShared queue;
1.252 + Pages pages(&mem, &queue);
1.253 + ResourceRegistry registry(&pages);
1.254 + MscRegionOpener opener(®istry, ops);
1.255 +
1.256 + /* Register a server associating it with the given object. */
1.257 +
1.258 + ResourceServer server(&opener);
1.259 + server.bind(server_name);
1.260 +
1.261 + printf("Starting server using %d pages...\n", memory_pages);
1.262 + server.start();
1.263 +
1.264 + return 0;
1.265 +}
1.266 +
1.267 +// vim: tabstop=2 expandtab shiftwidth=2