1*1991Sheppo /*
2*1991Sheppo * CDDL HEADER START
3*1991Sheppo *
4*1991Sheppo * The contents of this file are subject to the terms of the
5*1991Sheppo * Common Development and Distribution License (the "License").
6*1991Sheppo * You may not use this file except in compliance with the License.
7*1991Sheppo *
8*1991Sheppo * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*1991Sheppo * or http://www.opensolaris.org/os/licensing.
10*1991Sheppo * See the License for the specific language governing permissions
11*1991Sheppo * and limitations under the License.
12*1991Sheppo *
13*1991Sheppo * When distributing Covered Code, include this CDDL HEADER in each
14*1991Sheppo * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*1991Sheppo * If applicable, add the following below this CDDL HEADER, with the
16*1991Sheppo * fields enclosed by brackets "[]" replaced with your own identifying
17*1991Sheppo * information: Portions Copyright [yyyy] [name of copyright owner]
18*1991Sheppo *
19*1991Sheppo * CDDL HEADER END
20*1991Sheppo */
21*1991Sheppo
22*1991Sheppo /*
23*1991Sheppo * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
24*1991Sheppo * Use is subject to license terms.
25*1991Sheppo */
26*1991Sheppo
27*1991Sheppo #pragma ident "%Z%%M% %I% %E% SMI"
28*1991Sheppo
29*1991Sheppo #include <sys/types.h>
30*1991Sheppo #include <sys/machsystm.h>
31*1991Sheppo #include <sys/machparam.h>
32*1991Sheppo #include <sys/cmn_err.h>
33*1991Sheppo #include <sys/cpuvar.h>
34*1991Sheppo #include <sys/note.h>
35*1991Sheppo #include <sys/hypervisor_api.h>
36*1991Sheppo #include <sys/lpad.h>
37*1991Sheppo
38*1991Sheppo typedef struct {
39*1991Sheppo uint64_t inuse;
40*1991Sheppo uint64_t buf[LPAD_SIZE / sizeof (uint64_t)];
41*1991Sheppo } lpad_t;
42*1991Sheppo
43*1991Sheppo /*
44*1991Sheppo * A global pool of landing pad memory. Currently, CPUs are only
45*1991Sheppo * brought into the system one at a time, so the pool is only a
46*1991Sheppo * single landing pad. In the future, it may be desirable to bring
47*1991Sheppo * CPUs into the systems in parallel. At that time, the size of
48*1991Sheppo * the pool can be increased by changing the pool size constant.
49*1991Sheppo */
50*1991Sheppo #define LPAD_POOL_SIZE 1
51*1991Sheppo
52*1991Sheppo static lpad_t lpad_pool[LPAD_POOL_SIZE];
53*1991Sheppo
54*1991Sheppo #ifdef DEBUG
55*1991Sheppo static int lpad_dbg = 0;
56*1991Sheppo
57*1991Sheppo #define LPAD_DBG if (lpad_dbg) printf
58*1991Sheppo #define LPAD_DUMP_DATA lpad_dump_data
59*1991Sheppo
60*1991Sheppo static void lpad_dump_data(uint64_t *lpd_start, uint64_t *lpd_end);
61*1991Sheppo
62*1991Sheppo #else /* DEBUG */
63*1991Sheppo
64*1991Sheppo #define LPAD_DBG _NOTE(CONSTCOND) if (0) printf
65*1991Sheppo #define LPAD_DUMP_DATA
66*1991Sheppo #endif /* DEBUG */
67*1991Sheppo
68*1991Sheppo extern void mach_cpu_startup(uint64_t rabase, uint64_t memsize);
69*1991Sheppo extern void mach_cpu_startup_end(void);
70*1991Sheppo extern int promif_in_cif(void);
71*1991Sheppo
72*1991Sheppo static lpad_t *lpad_alloc(void);
73*1991Sheppo
74*1991Sheppo uint64_t *
lpad_setup(int cpuid,uint64_t pc,uint64_t arg)75*1991Sheppo lpad_setup(int cpuid, uint64_t pc, uint64_t arg)
76*1991Sheppo {
77*1991Sheppo lpad_t *lpp;
78*1991Sheppo uint64_t textsz;
79*1991Sheppo uint64_t datasz;
80*1991Sheppo lpad_data_t *lpd;
81*1991Sheppo lpad_map_t *lpm;
82*1991Sheppo
83*1991Sheppo /* external parameters */
84*1991Sheppo extern caddr_t textva;
85*1991Sheppo extern caddr_t datava;
86*1991Sheppo extern tte_t ktext_tte;
87*1991Sheppo extern tte_t kdata_tte;
88*1991Sheppo extern caddr_t mmu_fault_status_area;
89*1991Sheppo
90*1991Sheppo LPAD_DBG("lpad_setup...\n");
91*1991Sheppo
92*1991Sheppo if ((cpuid < 0) || (cpuid > NCPU)) {
93*1991Sheppo cmn_err(CE_PANIC, "lpad_setup: invalid cpuid");
94*1991Sheppo }
95*1991Sheppo
96*1991Sheppo /* allocate our landing pad */
97*1991Sheppo if ((lpp = lpad_alloc()) == NULL) {
98*1991Sheppo cmn_err(CE_PANIC, "lpad_setup: unable to allocate lpad");
99*1991Sheppo }
100*1991Sheppo
101*1991Sheppo /* calculate the size of our text */
102*1991Sheppo textsz = (uint64_t)mach_cpu_startup_end - (uint64_t)mach_cpu_startup;
103*1991Sheppo
104*1991Sheppo LPAD_DBG("lpad textsz=%ld\n", textsz);
105*1991Sheppo
106*1991Sheppo ASSERT(textsz <= LPAD_TEXT_SIZE);
107*1991Sheppo
108*1991Sheppo /* copy over text section */
109*1991Sheppo bcopy((void *)mach_cpu_startup, lpp->buf, textsz);
110*1991Sheppo
111*1991Sheppo lpd = (lpad_data_t *)(((caddr_t)lpp->buf) + LPAD_TEXT_SIZE);
112*1991Sheppo lpm = (lpad_map_t *)lpd->map;
113*1991Sheppo
114*1991Sheppo ASSERT(mmu_fault_status_area);
115*1991Sheppo
116*1991Sheppo bzero(lpd, LPAD_TEXT_SIZE);
117*1991Sheppo lpd->magic = LPAD_MAGIC_VAL;
118*1991Sheppo lpd->inuse = &(lpp->inuse);
119*1991Sheppo lpd->mmfsa_ra = va_to_pa(mmu_fault_status_area) + (MMFSA_SIZE * cpuid);
120*1991Sheppo lpd->pc = pc;
121*1991Sheppo lpd->arg = arg;
122*1991Sheppo
123*1991Sheppo /*
124*1991Sheppo * List of mappings:
125*1991Sheppo *
126*1991Sheppo * - permanent inst/data mapping for kernel text
127*1991Sheppo * - permanent data mapping for kernel data
128*1991Sheppo * - non-permanent inst mapping for kernel data,
129*1991Sheppo * required for landing pad text
130*1991Sheppo */
131*1991Sheppo lpd->nmap = 3;
132*1991Sheppo
133*1991Sheppo /* verify the lpad has enough room for the data */
134*1991Sheppo datasz = sizeof (lpad_data_t);
135*1991Sheppo datasz += (lpd->nmap - 1) * sizeof (lpad_map_t);
136*1991Sheppo
137*1991Sheppo ASSERT(datasz <= LPAD_DATA_SIZE);
138*1991Sheppo
139*1991Sheppo /*
140*1991Sheppo * Kernel Text Mapping
141*1991Sheppo */
142*1991Sheppo lpm->va = (uint64_t)textva;
143*1991Sheppo lpm->tte = ktext_tte;
144*1991Sheppo lpm->flag_mmuflags = (MAP_ITLB | MAP_DTLB);
145*1991Sheppo lpm->flag_perm = 1;
146*1991Sheppo lpm++;
147*1991Sheppo
148*1991Sheppo /*
149*1991Sheppo * Kernel Data Mapping
150*1991Sheppo */
151*1991Sheppo lpm->va = (uint64_t)datava;
152*1991Sheppo lpm->tte = kdata_tte;
153*1991Sheppo lpm->flag_mmuflags = MAP_DTLB;
154*1991Sheppo lpm->flag_perm = 1;
155*1991Sheppo lpm++;
156*1991Sheppo
157*1991Sheppo /*
158*1991Sheppo * Landing Pad Text Mapping
159*1991Sheppo *
160*1991Sheppo * Because this mapping should not be permanent,
161*1991Sheppo * the permanent mapping above cannot be used.
162*1991Sheppo */
163*1991Sheppo lpm->va = (uint64_t)datava;
164*1991Sheppo lpm->tte = kdata_tte;
165*1991Sheppo lpm->flag_mmuflags = MAP_ITLB;
166*1991Sheppo lpm->flag_perm = 0;
167*1991Sheppo lpm++;
168*1991Sheppo
169*1991Sheppo ASSERT(((uint64_t)lpm - (uint64_t)lpd) == datasz);
170*1991Sheppo
171*1991Sheppo LPAD_DBG("copied %ld bytes of data into lpad\n", datasz);
172*1991Sheppo
173*1991Sheppo LPAD_DUMP_DATA((uint64_t *)lpd, (uint64_t *)lpm);
174*1991Sheppo
175*1991Sheppo return (lpp->buf);
176*1991Sheppo }
177*1991Sheppo
178*1991Sheppo static lpad_t *
lpad_alloc(void)179*1991Sheppo lpad_alloc(void)
180*1991Sheppo {
181*1991Sheppo int idx;
182*1991Sheppo
183*1991Sheppo /*
184*1991Sheppo * No locking is required for the global lpad pool since
185*1991Sheppo * it should only be accessed while in the CIF which is
186*1991Sheppo * single threaded. If this assumption changes, locking
187*1991Sheppo * would be required.
188*1991Sheppo */
189*1991Sheppo ASSERT(promif_in_cif());
190*1991Sheppo
191*1991Sheppo /*
192*1991Sheppo * Wait until an lpad buffer becomes available.
193*1991Sheppo */
194*1991Sheppo for (;;) {
195*1991Sheppo LPAD_DBG("checking lpad pool:\n");
196*1991Sheppo
197*1991Sheppo /* walk the lpad buffer array */
198*1991Sheppo for (idx = 0; idx < LPAD_POOL_SIZE; idx++) {
199*1991Sheppo
200*1991Sheppo LPAD_DBG("\tchecking lpad_pool[%d]\n", idx);
201*1991Sheppo
202*1991Sheppo if (lpad_pool[idx].inuse == 0) {
203*1991Sheppo LPAD_DBG("found empty lpad (%d)\n", idx);
204*1991Sheppo
205*1991Sheppo /* mark the buffer as busy */
206*1991Sheppo lpad_pool[idx].inuse = 1;
207*1991Sheppo
208*1991Sheppo return (&lpad_pool[idx]);
209*1991Sheppo }
210*1991Sheppo }
211*1991Sheppo }
212*1991Sheppo }
213*1991Sheppo
214*1991Sheppo #ifdef DEBUG
215*1991Sheppo static void
lpad_dump_data(uint64_t * lpd_start,uint64_t * lpd_end)216*1991Sheppo lpad_dump_data(uint64_t *lpd_start, uint64_t *lpd_end)
217*1991Sheppo {
218*1991Sheppo uint64_t *lp;
219*1991Sheppo uint_t offset = 0;
220*1991Sheppo
221*1991Sheppo if (lpad_dbg == 0)
222*1991Sheppo return;
223*1991Sheppo
224*1991Sheppo printf("lpad data:\n");
225*1991Sheppo
226*1991Sheppo for (lp = lpd_start; lp < lpd_end; lp++) {
227*1991Sheppo printf("\t0x%02x 0x%016lx\n", offset, *lp);
228*1991Sheppo offset += sizeof (uint64_t);
229*1991Sheppo }
230*1991Sheppo }
231*1991Sheppo #endif /* DEBUG */
232