1 /* Realtek RTL8169 Family Driver 2 * Copyright (C) 2004 Marcus Overhagen <marcus@overhagen.de>. All rights reserved. 3 * 4 * Permission to use, copy, modify and distribute this software and its 5 * documentation for any purpose and without fee is hereby granted, provided 6 * that the above copyright notice appear in all copies, and that both the 7 * copyright notice and this permission notice appear in supporting documentation. 8 * 9 * Marcus Overhagen makes no representations about the suitability of this software 10 * for any purpose. It is provided "as is" without express or implied warranty. 11 * 12 * MARCUS OVERHAGEN DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING 13 * ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL MARCUS 14 * OVERHAGEN BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY 15 * DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN 17 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 18 */ 19 #include <Errors.h> 20 #include <OS.h> 21 #include <string.h> 22 23 //#define DEBUG 24 25 #include "fwdebug.h" 26 #include "util.h" 27 28 29 static inline uint32 30 round_to_pagesize(uint32 size) 31 { 32 return (size + B_PAGE_SIZE - 1) & ~(B_PAGE_SIZE - 1); 33 } 34 35 36 area_id 37 alloc_mem(void **virt, void **phy, size_t size, uint32 protection, 38 const char *name) 39 { 40 // TODO: phy should be phys_addr_t*! 41 physical_entry pe; 42 void *virtadr; 43 area_id area; 44 status_t rv; 45 46 TRACE("allocating %ld bytes for %s\n", size, name); 47 48 size = round_to_pagesize(size); 49 area = create_area(name, &virtadr, B_ANY_KERNEL_ADDRESS, size, B_CONTIGUOUS, 50 protection); 51 if (area < B_OK) { 52 ERROR("couldn't allocate area %s\n", name); 53 return B_ERROR; 54 } 55 rv = get_memory_map(virtadr, size, &pe, 1); 56 if (rv < B_OK) { 57 delete_area(area); 58 ERROR("couldn't get mapping for %s\n", name); 59 return B_ERROR; 60 } 61 memset(virtadr, 0, size); 62 if (virt) 63 *virt = virtadr; 64 if (phy) 65 *phy = (void*)(addr_t)pe.address; 66 TRACE("area = %ld, size = %ld, virt = %p, phy = %p\n", area, size, virtadr, 67 pe.address); 68 return area; 69 } 70 71 72 area_id 73 map_mem(void **virt, void *phy, size_t size, uint32 protection, 74 const char *name) 75 { 76 uint32 offset; 77 void *phyadr; 78 void *mapadr; 79 area_id area; 80 81 TRACE("mapping physical address %p with %ld bytes for %s\n", phy, size, 82 name); 83 84 offset = (uint32)phy & (B_PAGE_SIZE - 1); 85 phyadr = (char *)phy - offset; 86 size = round_to_pagesize(size + offset); 87 area = map_physical_memory(name, (addr_t)phyadr, size, 88 B_ANY_KERNEL_BLOCK_ADDRESS, protection, &mapadr); 89 if (area < B_OK) { 90 ERROR("mapping '%s' failed, error 0x%lx (%s)\n", name, area, 91 strerror(area)); 92 return area; 93 } 94 95 *virt = (char *)mapadr + offset; 96 97 TRACE("physical = %p, virtual = %p, offset = %ld, phyadr = %p, mapadr = " 98 "%p, size = %ld, area = 0x%08lx\n", phy, *virt, offset, phyadr, mapadr, 99 size, area); 100 101 return area; 102 } 103