1139738Simp/*-
284619Sbenno * Copyright (c) 2001 Benno Rice
384619Sbenno * All rights reserved.
484619Sbenno *
584619Sbenno * Redistribution and use in source and binary forms, with or without
684619Sbenno * modification, are permitted provided that the following conditions
784619Sbenno * are met:
884619Sbenno * 1. Redistributions of source code must retain the above copyright
984619Sbenno *    notice, this list of conditions and the following disclaimer.
1084619Sbenno * 2. Redistributions in binary form must reproduce the above copyright
1184619Sbenno *    notice, this list of conditions and the following disclaimer in the
1284619Sbenno *    documentation and/or other materials provided with the distribution.
1384619Sbenno *
1484619Sbenno * THIS SOFTWARE IS PROVIDED BY Benno Rice ``AS IS'' AND
1584619Sbenno * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1684619Sbenno * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1784619Sbenno * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
1884619Sbenno * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1984619Sbenno * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2084619Sbenno * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2184619Sbenno * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2284619Sbenno * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2384619Sbenno * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2484619Sbenno * SUCH DAMAGE.
2584619Sbenno */
2684619Sbenno
27124140Sobrien#include <sys/cdefs.h>
28124140Sobrien__FBSDID("$FreeBSD$");
29124140Sobrien
3084619Sbenno#include <sys/param.h>
3184619Sbenno#include <sys/types.h>
3284619Sbenno
3384619Sbenno#include <stand.h>
3484619Sbenno
3584619Sbenno#include "libofw.h"
3684619Sbenno#include "openfirm.h"
3784619Sbenno
3884619Sbennostatic void		*heap_base = 0;
3984619Sbennostatic unsigned int	heap_size = 0;
4084619Sbenno
4184619Sbennostruct ofw_mapping {
4284619Sbenno        vm_offset_t     va;
4384619Sbenno        int             len;
4484619Sbenno        vm_offset_t     pa;
4584619Sbenno        int             mode;
4684619Sbenno};
4784619Sbenno
48132996Sgrehanstruct ofw_mapping2 {
49132996Sgrehan        vm_offset_t     va;
50132996Sgrehan        int             len;
51132996Sgrehan        vm_offset_t     pa_hi;
52132996Sgrehan        vm_offset_t     pa_lo;
53132996Sgrehan        int             mode;
54132996Sgrehan};
55132996Sgrehan
5684619Sbennovoid
57132996Sgrehanofw_memmap(int acells)
5884619Sbenno{
59132996Sgrehan	struct		ofw_mapping *mapptr;
60132996Sgrehan	struct		ofw_mapping2 *mapptr2;
61132996Sgrehan        phandle_t	mmup;
62132996Sgrehan        int		nmapping, i;
63132996Sgrehan        u_char		mappings[256 * sizeof(struct ofw_mapping2)];
64132996Sgrehan        char		lbuf[80];
65132996Sgrehan
66132996Sgrehan	mmup = OF_instance_to_package(mmu);
67132996Sgrehan
68132996Sgrehan	bzero(mappings, sizeof(mappings));
69132996Sgrehan
70132996Sgrehan	nmapping = OF_getprop(mmup, "translations", mappings, sizeof(mappings));
7184619Sbenno	if (nmapping == -1) {
7284619Sbenno		printf("Could not get memory map (%d)\n",
7384619Sbenno		    nmapping);
7484619Sbenno		return;
7584619Sbenno	}
7684619Sbenno
77132996Sgrehan	pager_open();
78132996Sgrehan	if (acells == 1) {
79132996Sgrehan		nmapping /= sizeof(struct ofw_mapping);
80132996Sgrehan		mapptr = (struct ofw_mapping *) mappings;
8184619Sbenno
82132996Sgrehan		printf("%17s\t%17s\t%8s\t%6s\n", "Virtual Range",
83132996Sgrehan		    "Physical Range", "#Pages", "Mode");
84132996Sgrehan
85132996Sgrehan		for (i = 0; i < nmapping; i++) {
86132996Sgrehan			sprintf(lbuf, "%08x-%08x\t%08x-%08x\t%8d\t%6x\n",
87132996Sgrehan				mapptr[i].va,
88132996Sgrehan				mapptr[i].va + mapptr[i].len,
89132996Sgrehan				mapptr[i].pa,
90132996Sgrehan				mapptr[i].pa + mapptr[i].len,
91132996Sgrehan				mapptr[i].len / 0x1000,
92132996Sgrehan				mapptr[i].mode);
93132996Sgrehan			if (pager_output(lbuf))
94132996Sgrehan				break;
95132996Sgrehan		}
96132996Sgrehan	} else {
97132996Sgrehan		nmapping /= sizeof(struct ofw_mapping2);
98132996Sgrehan		mapptr2 = (struct ofw_mapping2 *) mappings;
99132996Sgrehan
100132996Sgrehan		printf("%17s\t%17s\t%8s\t%6s\n", "Virtual Range",
101132996Sgrehan		       "Physical Range", "#Pages", "Mode");
102132996Sgrehan
103132996Sgrehan		for (i = 0; i < nmapping; i++) {
104132996Sgrehan			sprintf(lbuf, "%08x-%08x\t%08x-%08x\t%8d\t%6x\n",
105132996Sgrehan				mapptr2[i].va,
106132996Sgrehan				mapptr2[i].va + mapptr2[i].len,
107132996Sgrehan				mapptr2[i].pa_lo,
108132996Sgrehan				mapptr2[i].pa_lo + mapptr2[i].len,
109132996Sgrehan				mapptr2[i].len / 0x1000,
110132996Sgrehan				mapptr2[i].mode);
111132996Sgrehan			if (pager_output(lbuf))
112132996Sgrehan				break;
113132996Sgrehan		}
11484619Sbenno	}
115132996Sgrehan	pager_close();
11684619Sbenno}
11784619Sbenno
11884619Sbennovoid *
11984619Sbennoofw_alloc_heap(unsigned int size)
12084619Sbenno{
121214493Snwhitehorn	phandle_t	memoryp, root;
122214493Snwhitehorn	cell_t		available[4];
123214493Snwhitehorn	cell_t		acells;
12484619Sbenno
125214493Snwhitehorn	root = OF_finddevice("/");
126214493Snwhitehorn	acells = 1;
127214493Snwhitehorn	OF_getprop(root, "#address-cells", &acells, sizeof(acells));
128214493Snwhitehorn
129100318Sbenno	memoryp = OF_instance_to_package(memory);
130214493Snwhitehorn	OF_getprop(memoryp, "available", available, sizeof(available));
13184619Sbenno
132214493Snwhitehorn	heap_base = OF_claim((void *)available[acells-1], size,
133214493Snwhitehorn	    sizeof(register_t));
13484619Sbenno
13584975Srobert	if (heap_base != (void *)-1) {
13684619Sbenno		heap_size = size;
13784619Sbenno	}
13884619Sbenno
13984619Sbenno	return (heap_base);
14084619Sbenno}
14184619Sbenno
14284619Sbennovoid
14384619Sbennoofw_release_heap(void)
14484619Sbenno{
14584619Sbenno	OF_release(heap_base, heap_size);
14684619Sbenno}
147