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