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