1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1991-1994, by Sun Microsystems, Inc.
24*7c478bd9Sstevel@tonic-gate  * All rights reserved.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate #include <sys/promif.h>
30*7c478bd9Sstevel@tonic-gate #include <sys/promimpl.h>
31*7c478bd9Sstevel@tonic-gate 
32*7c478bd9Sstevel@tonic-gate /*
33*7c478bd9Sstevel@tonic-gate  * This allocator has SMCC-OBP-like semantics associated with it.
34*7c478bd9Sstevel@tonic-gate  * Specifically, the alignment value specifies both a physical
35*7c478bd9Sstevel@tonic-gate  * and virtual alignment. If virthint is zero, a suitable virt
36*7c478bd9Sstevel@tonic-gate  * is chosen. In either case, align is not ignored.
37*7c478bd9Sstevel@tonic-gate  *
38*7c478bd9Sstevel@tonic-gate  * This routine returns NULL on failure.
39*7c478bd9Sstevel@tonic-gate  * This routine is suitable for (the given semantics) machines with
40*7c478bd9Sstevel@tonic-gate  * a 2-cell physical address.
41*7c478bd9Sstevel@tonic-gate  *
42*7c478bd9Sstevel@tonic-gate  * Memory allocated with prom_alloc can be freed with prom_free.
43*7c478bd9Sstevel@tonic-gate  *
44*7c478bd9Sstevel@tonic-gate  * The generic allocator is prom_malloc.
45*7c478bd9Sstevel@tonic-gate  *
46*7c478bd9Sstevel@tonic-gate  */
47*7c478bd9Sstevel@tonic-gate 
48*7c478bd9Sstevel@tonic-gate caddr_t
prom_alloc(caddr_t virthint,size_t size,u_int align)49*7c478bd9Sstevel@tonic-gate prom_alloc(caddr_t virthint, size_t size, u_int align)
50*7c478bd9Sstevel@tonic-gate {
51*7c478bd9Sstevel@tonic-gate 
52*7c478bd9Sstevel@tonic-gate 	caddr_t virt = virthint;
53*7c478bd9Sstevel@tonic-gate 	unsigned long long physaddr;
54*7c478bd9Sstevel@tonic-gate 
55*7c478bd9Sstevel@tonic-gate 	if (align == 0)
56*7c478bd9Sstevel@tonic-gate 		align = (u_int)1;
57*7c478bd9Sstevel@tonic-gate 
58*7c478bd9Sstevel@tonic-gate 	/*
59*7c478bd9Sstevel@tonic-gate 	 * First, allocate or claim the virtual address space.
60*7c478bd9Sstevel@tonic-gate 	 * In either case, after this code, "virt" is the chosen address.
61*7c478bd9Sstevel@tonic-gate 	 */
62*7c478bd9Sstevel@tonic-gate 	if (virthint == 0) {
63*7c478bd9Sstevel@tonic-gate 		virt = prom_allocate_virt(align, size);
64*7c478bd9Sstevel@tonic-gate 		if (virt == (caddr_t)-1)
65*7c478bd9Sstevel@tonic-gate 			return ((caddr_t)0);
66*7c478bd9Sstevel@tonic-gate 	} else {
67*7c478bd9Sstevel@tonic-gate 		if (prom_claim_virt(size, virthint) == (caddr_t)-1)
68*7c478bd9Sstevel@tonic-gate 			return ((caddr_t)0);
69*7c478bd9Sstevel@tonic-gate 	}
70*7c478bd9Sstevel@tonic-gate 
71*7c478bd9Sstevel@tonic-gate 	/*
72*7c478bd9Sstevel@tonic-gate 	 * Next, allocate the physical address space, at the specified
73*7c478bd9Sstevel@tonic-gate 	 * physical alignment (or 1 byte alignment, if none specified)
74*7c478bd9Sstevel@tonic-gate 	 */
75*7c478bd9Sstevel@tonic-gate 
76*7c478bd9Sstevel@tonic-gate 	if (prom_allocate_phys(size, align, &physaddr) == -1) {
77*7c478bd9Sstevel@tonic-gate 
78*7c478bd9Sstevel@tonic-gate 		/*
79*7c478bd9Sstevel@tonic-gate 		 * Request failed, free virtual address space and return.
80*7c478bd9Sstevel@tonic-gate 		 */
81*7c478bd9Sstevel@tonic-gate 		prom_free_virt(size, virt);
82*7c478bd9Sstevel@tonic-gate 		return ((caddr_t)0);
83*7c478bd9Sstevel@tonic-gate 	}
84*7c478bd9Sstevel@tonic-gate 
85*7c478bd9Sstevel@tonic-gate 	/*
86*7c478bd9Sstevel@tonic-gate 	 * Next, create a mapping from the physical to virtual address,
87*7c478bd9Sstevel@tonic-gate 	 * using a default "mode".
88*7c478bd9Sstevel@tonic-gate 	 */
89*7c478bd9Sstevel@tonic-gate 
90*7c478bd9Sstevel@tonic-gate 	if (prom_map_phys(-1, size, virt, physaddr) == -1)  {
91*7c478bd9Sstevel@tonic-gate 
92*7c478bd9Sstevel@tonic-gate 		/*
93*7c478bd9Sstevel@tonic-gate 		 * The call failed; release the physical and virtual
94*7c478bd9Sstevel@tonic-gate 		 * addresses allocated or claimed, and return.
95*7c478bd9Sstevel@tonic-gate 		 */
96*7c478bd9Sstevel@tonic-gate 
97*7c478bd9Sstevel@tonic-gate 		prom_free_virt(size, virt);
98*7c478bd9Sstevel@tonic-gate 		prom_free_phys(size, physaddr);
99*7c478bd9Sstevel@tonic-gate 		return ((caddr_t)0);
100*7c478bd9Sstevel@tonic-gate 	}
101*7c478bd9Sstevel@tonic-gate 	return (virt);
102*7c478bd9Sstevel@tonic-gate }
103*7c478bd9Sstevel@tonic-gate 
104*7c478bd9Sstevel@tonic-gate /*
105*7c478bd9Sstevel@tonic-gate  * This is the generic client interface to "claim" memory.
106*7c478bd9Sstevel@tonic-gate  * These two routines belong in the common directory.
107*7c478bd9Sstevel@tonic-gate  */
108*7c478bd9Sstevel@tonic-gate caddr_t
prom_malloc(caddr_t virt,size_t size,u_int align)109*7c478bd9Sstevel@tonic-gate prom_malloc(caddr_t virt, size_t size, u_int align)
110*7c478bd9Sstevel@tonic-gate {
111*7c478bd9Sstevel@tonic-gate 	cell_t ci[7];
112*7c478bd9Sstevel@tonic-gate 	int rv;
113*7c478bd9Sstevel@tonic-gate 
114*7c478bd9Sstevel@tonic-gate 	ci[0] = p1275_ptr2cell("claim");	/* Service name */
115*7c478bd9Sstevel@tonic-gate 	ci[1] = (cell_t)3;			/* #argument cells */
116*7c478bd9Sstevel@tonic-gate 	ci[2] = (cell_t)1;			/* #result cells */
117*7c478bd9Sstevel@tonic-gate 	ci[3] = p1275_ptr2cell(virt);		/* Arg1: virt */
118*7c478bd9Sstevel@tonic-gate 	ci[4] = p1275_size2cell(size);		/* Arg2: size */
119*7c478bd9Sstevel@tonic-gate 	ci[5] = p1275_uint2cell(align);		/* Arg3: align */
120*7c478bd9Sstevel@tonic-gate 
121*7c478bd9Sstevel@tonic-gate 	promif_preprom();
122*7c478bd9Sstevel@tonic-gate 	rv = p1275_cif_handler(&ci);
123*7c478bd9Sstevel@tonic-gate 	promif_postprom();
124*7c478bd9Sstevel@tonic-gate 
125*7c478bd9Sstevel@tonic-gate 	if (rv == 0)
126*7c478bd9Sstevel@tonic-gate 		return ((caddr_t)p1275_cell2ptr(ci[6])); /* Res1: base */
127*7c478bd9Sstevel@tonic-gate 	return ((caddr_t)-1);
128*7c478bd9Sstevel@tonic-gate }
129*7c478bd9Sstevel@tonic-gate 
130*7c478bd9Sstevel@tonic-gate 
131*7c478bd9Sstevel@tonic-gate void
prom_free(caddr_t virt,size_t size)132*7c478bd9Sstevel@tonic-gate prom_free(caddr_t virt, size_t size)
133*7c478bd9Sstevel@tonic-gate {
134*7c478bd9Sstevel@tonic-gate 	cell_t ci[5];
135*7c478bd9Sstevel@tonic-gate 
136*7c478bd9Sstevel@tonic-gate 	ci[0] = p1275_ptr2cell("release");	/* Service name */
137*7c478bd9Sstevel@tonic-gate 	ci[1] = (cell_t)2;			/* #argument cells */
138*7c478bd9Sstevel@tonic-gate 	ci[2] = (cell_t)0;			/* #result cells */
139*7c478bd9Sstevel@tonic-gate 	ci[3] = p1275_ptr2cell(virt);		/* Arg1: virt */
140*7c478bd9Sstevel@tonic-gate 	ci[4] = p1275_size2cell(size);		/* Arg2: size */
141*7c478bd9Sstevel@tonic-gate 
142*7c478bd9Sstevel@tonic-gate 	promif_preprom();
143*7c478bd9Sstevel@tonic-gate 	(void) p1275_cif_handler(&ci);
144*7c478bd9Sstevel@tonic-gate 	promif_postprom();
145*7c478bd9Sstevel@tonic-gate }
146