paul@0 | 1 | /* |
paul@0 | 2 | * Memory allocation utility functions. |
paul@0 | 3 | * |
paul@0 | 4 | * Copyright (C) 2018 Paul Boddie <paul@boddie.org.uk> |
paul@0 | 5 | * Subject to other copyrights, being derived from the existing L4Re |
paul@0 | 6 | * LCD driver implementations. |
paul@0 | 7 | * |
paul@0 | 8 | * This file is part of TUD:OS and distributed under the terms of the |
paul@0 | 9 | * GNU General Public License 2. |
paul@0 | 10 | * Please see the COPYING-GPL-2 file for details. |
paul@0 | 11 | */ |
paul@0 | 12 | |
paul@0 | 13 | #include <stdio.h> |
paul@0 | 14 | |
paul@0 | 15 | #include "memory.h" |
paul@0 | 16 | #include <l4/io/io.h> |
paul@0 | 17 | #include <l4/re/env.h> |
paul@0 | 18 | #include <l4/re/c/mem_alloc.h> |
paul@0 | 19 | #include <l4/re/c/util/cap_alloc.h> |
paul@0 | 20 | #include <l4/util/util.h> |
paul@0 | 21 | #include <l4/vbus/vbus.h> |
paul@0 | 22 | |
paul@0 | 23 | /* Internal memory allocation/mapping functions. */ |
paul@0 | 24 | |
paul@0 | 25 | static char const *resource_type(enum l4io_resource_types_t type) |
paul@0 | 26 | { |
paul@0 | 27 | switch (type) |
paul@0 | 28 | { |
paul@0 | 29 | case L4VBUS_RESOURCE_INVALID: |
paul@0 | 30 | return "INVALID"; |
paul@0 | 31 | |
paul@0 | 32 | case L4VBUS_RESOURCE_IRQ: |
paul@0 | 33 | return "IRQ"; |
paul@0 | 34 | |
paul@0 | 35 | case L4VBUS_RESOURCE_MEM: |
paul@0 | 36 | return "MEMORY"; |
paul@0 | 37 | |
paul@0 | 38 | default: |
paul@0 | 39 | return "OTHER"; |
paul@0 | 40 | } |
paul@0 | 41 | } |
paul@0 | 42 | |
paul@0 | 43 | int get_device(char const *hid, l4io_device_handle_t *dh, l4io_resource_handle_t *rh) |
paul@0 | 44 | { |
paul@0 | 45 | int result = l4io_lookup_device(hid, dh, 0, rh); |
paul@0 | 46 | |
paul@0 | 47 | if (result < 0) |
paul@0 | 48 | printf("Could not access '%s': %s\n", hid, result == -L4_ENOENT ? "no such device" : "no device"); |
paul@0 | 49 | |
paul@0 | 50 | return result; |
paul@0 | 51 | } |
paul@0 | 52 | |
paul@0 | 53 | int get_resource(l4io_device_handle_t dh, l4io_resource_t *res, |
paul@0 | 54 | enum l4io_resource_types_t type) |
paul@0 | 55 | { |
paul@0 | 56 | int current = 0, result = 0; |
paul@0 | 57 | l4_cap_idx_t vbus = l4re_env_get_cap("vbus"); |
paul@0 | 58 | |
paul@0 | 59 | do |
paul@0 | 60 | { |
paul@0 | 61 | result = l4vbus_get_resource(vbus, dh, current, res); |
paul@0 | 62 | |
paul@0 | 63 | if (result) |
paul@0 | 64 | printf("Could not access resource of type %s.\n", resource_type(type)); |
paul@0 | 65 | else |
paul@0 | 66 | printf("Resource %d: type %s, start=%lx, end=%lx\n", res->id, |
paul@0 | 67 | resource_type(res->type), res->start, res->end); |
paul@0 | 68 | |
paul@0 | 69 | current++; |
paul@0 | 70 | } |
paul@0 | 71 | while ((!result) && (res->type != type)); |
paul@0 | 72 | |
paul@0 | 73 | return result; |
paul@0 | 74 | } |
paul@0 | 75 | |
paul@0 | 76 | int get_memory(char const *hid, l4_addr_t *start, l4_addr_t *end) |
paul@0 | 77 | { |
paul@0 | 78 | l4io_device_handle_t dh; |
paul@0 | 79 | l4io_resource_handle_t rh; |
paul@0 | 80 | l4io_resource_t res; |
paul@0 | 81 | int result; |
paul@0 | 82 | |
paul@0 | 83 | result = get_device(hid, &dh, &rh); |
paul@0 | 84 | |
paul@0 | 85 | if (result < 0) |
paul@0 | 86 | return result; |
paul@0 | 87 | |
paul@0 | 88 | result = get_resource(dh, &res, L4IO_RESOURCE_MEM); |
paul@0 | 89 | |
paul@0 | 90 | if (result) |
paul@0 | 91 | return result; |
paul@0 | 92 | |
paul@0 | 93 | if ((result = l4io_request_iomem(res.start, res.end - res.start + 1, |
paul@0 | 94 | L4IO_MEM_NONCACHED, start))) |
paul@0 | 95 | { |
paul@0 | 96 | printf("Could not get address for '%s'.\n", hid); |
paul@0 | 97 | return result; |
paul@0 | 98 | } |
paul@0 | 99 | |
paul@0 | 100 | printf("Resource at 0x%lx...0x%lx.\n", res.start, res.end); |
paul@0 | 101 | |
paul@0 | 102 | *end = *start + (res.end - res.start + 1); |
paul@0 | 103 | |
paul@0 | 104 | return 0; |
paul@0 | 105 | } |