paul@0 | 1 | /* |
paul@79 | 2 | * Memory and resource allocation utility functions. |
paul@0 | 3 | * |
paul@142 | 4 | * Copyright (C) 2018, 2019, 2023 Paul Boddie <paul@boddie.org.uk> |
paul@0 | 5 | * |
paul@0 | 6 | * This program is free software; you can redistribute it and/or |
paul@0 | 7 | * modify it under the terms of the GNU General Public License as |
paul@0 | 8 | * published by the Free Software Foundation; either version 2 of |
paul@0 | 9 | * the License, or (at your option) any later version. |
paul@0 | 10 | * |
paul@0 | 11 | * This program is distributed in the hope that it will be useful, |
paul@0 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@0 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@0 | 14 | * GNU General Public License for more details. |
paul@0 | 15 | * |
paul@0 | 16 | * You should have received a copy of the GNU General Public License |
paul@0 | 17 | * along with this program; if not, write to the Free Software |
paul@0 | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@0 | 19 | * Boston, MA 02110-1301, USA |
paul@0 | 20 | */ |
paul@0 | 21 | |
paul@0 | 22 | #include <l4/io/io.h> |
paul@0 | 23 | #include <l4/re/env.h> |
paul@0 | 24 | #include <l4/re/c/mem_alloc.h> |
paul@0 | 25 | #include <l4/re/c/util/cap_alloc.h> |
paul@0 | 26 | #include <l4/util/util.h> |
paul@0 | 27 | #include <l4/vbus/vbus.h> |
paul@0 | 28 | |
paul@0 | 29 | #include "memory.h" |
paul@0 | 30 | |
paul@152 | 31 | |
paul@152 | 32 | |
paul@0 | 33 | int get_device(char const *hid, l4io_device_handle_t *dh, l4io_resource_handle_t *rh) |
paul@0 | 34 | { |
paul@0 | 35 | return l4io_lookup_device(hid, dh, 0, rh); |
paul@0 | 36 | } |
paul@0 | 37 | |
paul@79 | 38 | int get_irq(char const *hid, l4_uint32_t *start, l4_uint32_t *end) |
paul@79 | 39 | { |
paul@79 | 40 | l4io_device_handle_t dh; |
paul@79 | 41 | l4io_resource_handle_t rh; |
paul@79 | 42 | l4io_resource_t res; |
paul@79 | 43 | int result; |
paul@79 | 44 | |
paul@79 | 45 | result = get_device(hid, &dh, &rh); |
paul@79 | 46 | |
paul@79 | 47 | if (result < 0) |
paul@79 | 48 | return result; |
paul@79 | 49 | |
paul@142 | 50 | result = get_resource(dh, &res, L4VBUS_RESOURCE_IRQ); |
paul@79 | 51 | |
paul@79 | 52 | if (result) |
paul@79 | 53 | return result; |
paul@79 | 54 | |
paul@79 | 55 | *start = res.start; |
paul@79 | 56 | *end = res.end; |
paul@79 | 57 | |
paul@79 | 58 | return result; |
paul@79 | 59 | } |
paul@79 | 60 | |
paul@0 | 61 | int get_resource(l4io_device_handle_t dh, l4io_resource_t *res, |
paul@142 | 62 | enum l4vbus_resource_type_t type) |
paul@0 | 63 | { |
paul@0 | 64 | int current = 0, result = 0; |
paul@0 | 65 | l4_cap_idx_t vbus = l4re_env_get_cap("vbus"); |
paul@0 | 66 | |
paul@0 | 67 | do |
paul@0 | 68 | { |
paul@0 | 69 | result = l4vbus_get_resource(vbus, dh, current, res); |
paul@0 | 70 | current++; |
paul@0 | 71 | } |
paul@0 | 72 | while ((!result) && (res->type != type)); |
paul@0 | 73 | |
paul@0 | 74 | return result; |
paul@0 | 75 | } |
paul@0 | 76 | |
paul@0 | 77 | int get_memory(char const *hid, l4_addr_t *start, l4_addr_t *end) |
paul@0 | 78 | { |
paul@202 | 79 | l4_addr_t phys_start, phys_end; |
paul@202 | 80 | |
paul@202 | 81 | return get_memory_complete(hid, start, end, &phys_start, &phys_end); |
paul@202 | 82 | } |
paul@202 | 83 | |
paul@202 | 84 | int get_memory_complete(char const *hid, l4_addr_t *start, l4_addr_t *end, |
paul@202 | 85 | l4_addr_t *phys_start, l4_addr_t *phys_end) |
paul@202 | 86 | { |
paul@0 | 87 | l4io_device_handle_t dh; |
paul@0 | 88 | l4io_resource_handle_t rh; |
paul@0 | 89 | l4io_resource_t res; |
paul@0 | 90 | int result; |
paul@0 | 91 | |
paul@0 | 92 | result = get_device(hid, &dh, &rh); |
paul@0 | 93 | |
paul@0 | 94 | if (result < 0) |
paul@0 | 95 | return result; |
paul@0 | 96 | |
paul@142 | 97 | result = get_resource(dh, &res, L4VBUS_RESOURCE_MEM); |
paul@0 | 98 | |
paul@0 | 99 | if (result) |
paul@0 | 100 | return result; |
paul@0 | 101 | |
paul@0 | 102 | result = l4io_request_iomem(res.start, res.end - res.start + 1, |
paul@0 | 103 | L4IO_MEM_NONCACHED, start); |
paul@0 | 104 | |
paul@0 | 105 | if (result) |
paul@0 | 106 | return result; |
paul@0 | 107 | |
paul@0 | 108 | *end = *start + (res.end - res.start + 1); |
paul@202 | 109 | *phys_start = res.start; |
paul@202 | 110 | *phys_end = res.end; |
paul@0 | 111 | |
paul@0 | 112 | return 0; |
paul@0 | 113 | } |
paul@142 | 114 | |
paul@142 | 115 | static int _find_resource(l4vbus_device_handle_t device, |
paul@142 | 116 | l4vbus_resource_t *resource, |
paul@142 | 117 | enum l4vbus_resource_type_t type, |
paul@142 | 118 | l4vbus_device_t info) |
paul@142 | 119 | { |
paul@142 | 120 | l4_cap_idx_t vbus = l4re_env_get_cap("vbus"); |
paul@142 | 121 | |
paul@142 | 122 | for (unsigned int i = 0; i < info.num_resources; i++) |
paul@142 | 123 | { |
paul@142 | 124 | int result = l4vbus_get_resource(vbus, device, i, resource); |
paul@142 | 125 | |
paul@142 | 126 | if (!result && (resource->type == type)) |
paul@142 | 127 | return 1; |
paul@142 | 128 | } |
paul@142 | 129 | |
paul@142 | 130 | return 0; |
paul@142 | 131 | } |
paul@142 | 132 | |
paul@142 | 133 | int find_resource(l4vbus_device_handle_t *device, l4vbus_resource_t *resource, |
paul@142 | 134 | enum l4vbus_resource_type_t type) |
paul@142 | 135 | { |
paul@142 | 136 | l4_cap_idx_t vbus = l4re_env_get_cap("vbus"); |
paul@142 | 137 | l4vbus_device_t info; |
paul@142 | 138 | |
paul@142 | 139 | /* Search within devices only, not within the top-level of the bus itself. */ |
paul@142 | 140 | |
paul@142 | 141 | while (!l4vbus_get_next_device(vbus, L4VBUS_ROOT_BUS, device, |
paul@142 | 142 | L4VBUS_MAX_DEPTH, &info)) |
paul@142 | 143 | { |
paul@142 | 144 | if (_find_resource(*device, resource, type, info)) |
paul@142 | 145 | return 1; |
paul@142 | 146 | } |
paul@142 | 147 | |
paul@142 | 148 | return 0; |
paul@142 | 149 | } |