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 | * |
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@0 | 31 | int get_device(char const *hid, l4io_device_handle_t *dh, l4io_resource_handle_t *rh) |
paul@0 | 32 | { |
paul@0 | 33 | return l4io_lookup_device(hid, dh, 0, rh); |
paul@0 | 34 | } |
paul@0 | 35 | |
paul@0 | 36 | int get_resource(l4io_device_handle_t dh, l4io_resource_t *res, |
paul@0 | 37 | enum l4io_resource_types_t type) |
paul@0 | 38 | { |
paul@0 | 39 | int current = 0, result = 0; |
paul@0 | 40 | l4_cap_idx_t vbus = l4re_env_get_cap("vbus"); |
paul@0 | 41 | |
paul@0 | 42 | do |
paul@0 | 43 | { |
paul@0 | 44 | result = l4vbus_get_resource(vbus, dh, current, res); |
paul@0 | 45 | current++; |
paul@0 | 46 | } |
paul@0 | 47 | while ((!result) && (res->type != type)); |
paul@0 | 48 | |
paul@0 | 49 | return result; |
paul@0 | 50 | } |
paul@0 | 51 | |
paul@0 | 52 | int get_memory(char const *hid, l4_addr_t *start, l4_addr_t *end) |
paul@0 | 53 | { |
paul@0 | 54 | l4io_device_handle_t dh; |
paul@0 | 55 | l4io_resource_handle_t rh; |
paul@0 | 56 | l4io_resource_t res; |
paul@0 | 57 | int result; |
paul@0 | 58 | |
paul@0 | 59 | result = get_device(hid, &dh, &rh); |
paul@0 | 60 | |
paul@0 | 61 | if (result < 0) |
paul@0 | 62 | return result; |
paul@0 | 63 | |
paul@0 | 64 | result = get_resource(dh, &res, L4IO_RESOURCE_MEM); |
paul@0 | 65 | |
paul@0 | 66 | if (result) |
paul@0 | 67 | return result; |
paul@0 | 68 | |
paul@0 | 69 | result = l4io_request_iomem(res.start, res.end - res.start + 1, |
paul@0 | 70 | L4IO_MEM_NONCACHED, start); |
paul@0 | 71 | |
paul@0 | 72 | if (result) |
paul@0 | 73 | return result; |
paul@0 | 74 | |
paul@0 | 75 | *end = *start + (res.end - res.start + 1); |
paul@0 | 76 | |
paul@0 | 77 | return 0; |
paul@0 | 78 | } |