1/*-
2 * Copyright (c) 2001 Benno Rice
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY Benno Rice ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27#include <sys/cdefs.h>
28__FBSDID("$FreeBSD$");
29
30#include <sys/param.h>
31#include <sys/types.h>
32
33#include <stand.h>
34
35#include "libofw.h"
36#include "openfirm.h"
37
38static void		*heap_base = 0;
39static unsigned int	heap_size = 0;
40
41struct ofw_mapping {
42        vm_offset_t     va;
43        int             len;
44        vm_offset_t     pa;
45        int             mode;
46};
47
48struct ofw_mapping2 {
49        vm_offset_t     va;
50        int             len;
51        vm_offset_t     pa_hi;
52        vm_offset_t     pa_lo;
53        int             mode;
54};
55
56void
57ofw_memmap(int acells)
58{
59	struct		ofw_mapping *mapptr;
60	struct		ofw_mapping2 *mapptr2;
61        phandle_t	mmup;
62        int		nmapping, i;
63        u_char		mappings[256 * sizeof(struct ofw_mapping2)];
64        char		lbuf[80];
65
66	mmup = OF_instance_to_package(mmu);
67
68	bzero(mappings, sizeof(mappings));
69
70	nmapping = OF_getprop(mmup, "translations", mappings, sizeof(mappings));
71	if (nmapping == -1) {
72		printf("Could not get memory map (%d)\n",
73		    nmapping);
74		return;
75	}
76
77	pager_open();
78	if (acells == 1) {
79		nmapping /= sizeof(struct ofw_mapping);
80		mapptr = (struct ofw_mapping *) mappings;
81
82		printf("%17s\t%17s\t%8s\t%6s\n", "Virtual Range",
83		    "Physical Range", "#Pages", "Mode");
84
85		for (i = 0; i < nmapping; i++) {
86			sprintf(lbuf, "%08x-%08x\t%08x-%08x\t%8d\t%6x\n",
87				mapptr[i].va,
88				mapptr[i].va + mapptr[i].len,
89				mapptr[i].pa,
90				mapptr[i].pa + mapptr[i].len,
91				mapptr[i].len / 0x1000,
92				mapptr[i].mode);
93			if (pager_output(lbuf))
94				break;
95		}
96	} else {
97		nmapping /= sizeof(struct ofw_mapping2);
98		mapptr2 = (struct ofw_mapping2 *) mappings;
99
100		printf("%17s\t%17s\t%8s\t%6s\n", "Virtual Range",
101		       "Physical Range", "#Pages", "Mode");
102
103		for (i = 0; i < nmapping; i++) {
104			sprintf(lbuf, "%08x-%08x\t%08x-%08x\t%8d\t%6x\n",
105				mapptr2[i].va,
106				mapptr2[i].va + mapptr2[i].len,
107				mapptr2[i].pa_lo,
108				mapptr2[i].pa_lo + mapptr2[i].len,
109				mapptr2[i].len / 0x1000,
110				mapptr2[i].mode);
111			if (pager_output(lbuf))
112				break;
113		}
114	}
115	pager_close();
116}
117
118void *
119ofw_alloc_heap(unsigned int size)
120{
121	phandle_t	memoryp, root;
122	cell_t		available[4];
123	cell_t		acells;
124
125	root = OF_finddevice("/");
126	acells = 1;
127	OF_getprop(root, "#address-cells", &acells, sizeof(acells));
128
129	memoryp = OF_instance_to_package(memory);
130	OF_getprop(memoryp, "available", available, sizeof(available));
131
132	heap_base = OF_claim((void *)available[acells-1], size,
133	    sizeof(register_t));
134
135	if (heap_base != (void *)-1) {
136		heap_size = size;
137	}
138
139	return (heap_base);
140}
141
142void
143ofw_release_heap(void)
144{
145	OF_release(heap_base, heap_size);
146}
147