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