1/*
2 * Copyright (c) 2004-2007 Marcus Overhagen <marcus@overhagen.de>
3 *
4 * Permission is hereby granted, free of charge, to any person
5 * obtaining a copy of this software and associated documentation
6 * files (the "Software"), to deal in the Software without restriction,
7 * including without limitation the rights to use, copy, modify,
8 * merge, publish, distribute, sublicense, and/or sell copies of
9 * the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25#include <KernelExport.h>
26#include <Errors.h>
27#include <OS.h>
28#include <string.h>
29#include "util.h"
30
31#define TRACE_UTIL
32#ifdef TRACE_UTIL
33  #define TRACE dprintf
34#else
35  #define TRACE(a...)
36#endif
37
38
39area_id
40map_mem(void **virt, void *phy, size_t size, uint32 protection,
41	const char *name)
42{
43	uint32 offset;
44	void *phyadr;
45	void *mapadr;
46	area_id area;
47
48	TRACE("mapping physical address %p with %ld bytes for %s\n", phy, size,
49		name);
50
51	offset = (uint32)phy & (B_PAGE_SIZE - 1);
52	phyadr = (char *)phy - offset;
53	size = ROUNDUP(size + offset, B_PAGE_SIZE);
54	area = map_physical_memory(name, (addr_t)phyadr, size,
55		B_ANY_KERNEL_BLOCK_ADDRESS, protection, &mapadr);
56	if (area < B_OK) {
57		TRACE("mapping '%s' failed, error 0x%lx (%s)\n", name, area, strerror(area));
58		return area;
59	}
60
61	*virt = (char *)mapadr + offset;
62
63	TRACE("physical = %p, virtual = %p, offset = %ld, phyadr = %p, mapadr = %p, size = %ld, area = 0x%08lx\n",
64		phy, *virt, offset, phyadr, mapadr, size, area);
65
66	return area;
67}
68
69
70area_id
71alloc_mem(void **virt, void **phy, size_t size, uint32 protection,
72	const char *name)
73{
74// TODO: phy should be phys_addr_t*!
75	physical_entry pe;
76	void * virtadr;
77	area_id areaid;
78	status_t rv;
79
80	TRACE("allocating %ld bytes for %s\n", size, name);
81
82	size = ROUNDUP(size, B_PAGE_SIZE);
83	areaid = create_area(name, &virtadr, B_ANY_KERNEL_ADDRESS, size,
84		B_32_BIT_CONTIGUOUS, protection);
85		// TODO: The rest of the code doesn't deal correctly with physical
86		// addresses > 4 GB, so we have to force 32 bit addresses here.
87	if (areaid < B_OK) {
88		TRACE("couldn't allocate area %s\n", name);
89		return B_ERROR;
90	}
91	rv = get_memory_map(virtadr, size, &pe, 1);
92	if (rv < B_OK) {
93		delete_area(areaid);
94		TRACE("couldn't map %s\n", name);
95		return B_ERROR;
96	}
97	if (virt)
98		*virt = virtadr;
99	if (phy)
100		*phy = (void*)(addr_t)pe.address;
101	TRACE("area = %ld, size = %ld, virt = %p, phy = %" B_PRIxPHYSADDR "\n", areaid, size, virtadr, pe.address);
102	return areaid;
103}
104