Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
/*	$NetBSD: sun2.c,v 1.11 2009/12/11 18:42:05 tsutsui Exp $	*/

/*-
 * Copyright (c) 1998 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Gordon W. Ross and Matthew Fredette.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

/*
 * Standalone functions specific to the Sun2.
 */

/* Need to avoid conflicts on these: */
#define get_pte sun2_get_pte
#define set_pte sun2_set_pte
#define get_segmap sun2_get_segmap
#define set_segmap sun2_set_segmap

/*
 * We need to get the sun2 NBSG definition, even if we're 
 * building this with a different sun68k target.
 */
#include <arch/sun2/include/pmap.h>

#include <sys/param.h>
#include <machine/idprom.h>
#include <machine/mon.h>

#include <arch/sun2/include/pte.h>
#include <arch/sun2/sun2/control.h>
#ifdef notyet
#include <arch/sun3/sun3/vme.h>
#else
#define VME16_BASE MBIO_BASE
#define VME16_MASK MBIO_MASK
#endif
#include <arch/sun2/sun2/mbmem.h>
#include <arch/sun2/sun2/mbio.h>

#include <stand.h>

#include "libsa.h"
#include "dvma.h"
#include "saio.h"	/* enum MAPTYPES */

#define OBIO_MASK 0xFFFFFF

u_int	get_pte(vaddr_t);
void	set_pte(vaddr_t, u_int);
void	dvma2_init(void);
char *	dvma2_alloc(int);
void	dvma2_free(char *, int);
char *	dvma2_mapin(char *, int);
void	dvma2_mapout(char *, int);
char *	dev2_mapin(int, u_long, int);

struct mapinfo {
	int maptype;
	int pgtype;
	u_int base;
	u_int mask;
};

#ifdef	notyet
struct mapinfo
sun2_mapinfo[MAP__NTYPES] = {
	/* On-board memory, I/O */
	{ MAP_MAINMEM,   PGT_OBMEM,   0,          ~0 },
	{ MAP_OBIO,      PGT_OBIO,    0,          OBIO_MASK },
	/* Multibus memory, I/O */
	{ MAP_MBMEM,     PGT_MBMEM, MBMEM_BASE, MBMEM_MASK },
	{ MAP_MBIO,      PGT_MBIO,  MBIO_BASE, MBIO_MASK },
	/* VME A16 */
	{ MAP_VME16A16D, PGT_VME_D16, VME16_BASE, VME16_MASK },
	{ MAP_VME16A32D, 0, 0, 0 },
	/* VME A24 */
	{ MAP_VME24A16D, 0, 0, 0 },
	{ MAP_VME24A32D, 0, 0, 0 },
	/* VME A32 */
	{ MAP_VME32A16D, 0, 0, 0 },
	{ MAP_VME32A32D, 0, 0, 0 },
};
#endif

/* The virtual address we will use for PROM device mappings. */
int sun2_devmap = SUN3_MONSHORTSEG;

char *
dev2_mapin(int maptype, u_long physaddr, int length)
{
#ifdef	notyet
	u_int i, pa, pte, pgva, va;

	if ((sun2_devmap + length) > SUN3_MONSHORTPAGE)
		panic("dev2_mapin: length=%d", length);

	for (i = 0; i < MAP__NTYPES; i++)
		if (sun2_mapinfo[i].maptype == maptype)
			goto found;
	panic("dev2_mapin: bad maptype");
found:

	if (physaddr & ~(sun2_mapinfo[i].mask))
		panic("dev2_mapin: bad address");
	pa = sun2_mapinfo[i].base += physaddr;

	pte = PA_PGNUM(pa) | PG_PERM |
		sun2_mapinfo[i].pgtype;

	va = pgva = sun2_devmap;
	do {
		set_pte(pgva, pte);
		pgva += NBPG;
		pte += 1;
		length -= NBPG;
	} while (length > 0);
	sun2_devmap = pgva;
	va += (physaddr & PGOFSET);

#ifdef	DEBUG_PROM
	if (debug)
		printf("dev2_mapin: va=0x%x pte=0x%x\n",
			   va, get_pte(va));
#endif
	return ((char*)va);
#else
	panic("dev2_mapin");
	return(NULL);
#endif
}

/*****************************************************************
 * DVMA support
 */

/*
 * The easiest way to deal with the need for DVMA mappings is to
 * create a DVMA alias mapping of the entire address range used by
 * the boot program.  That way, dvma_mapin can just compute the
 * DVMA alias address, and dvma_mapout does nothing.
 *
 * Note that this assumes that standalone programs will do I/O
 * operations only within range (SA_MIN_VA .. SA_MAX_VA) checked.
 */

#define DVMA_BASE 0x00f00000
#define DVMA_MAPLEN  0x38000	/* 256K - 32K (save MONSHORTSEG) */

#define SA_MIN_VA	0x220000
#define SA_MAX_VA	(SA_MIN_VA + DVMA_MAPLEN)

/* This points to the end of the free DVMA space. */
u_int dvma2_end = DVMA_BASE + DVMA_MAPLEN;

void 
dvma2_init(void)
{
	int segva, dmava, sme;

	segva = SA_MIN_VA;
	dmava = DVMA_BASE;

	while (segva < SA_MAX_VA) {
		sme = get_segmap(segva);
		set_segmap(dmava, sme);
		segva += NBSG;
		dmava += NBSG;
	}
}

/* Convert a local address to a DVMA address. */
char *
dvma2_mapin(char *addr, int len)
{
	int va = (int)addr;

	/* Make sure the address is in the DVMA map. */
	if ((va < SA_MIN_VA) || (va >= SA_MAX_VA))
		panic("dvma2_mapin: 0x%x outside 0x%x..0x%x", 
		    va, SA_MIN_VA, SA_MAX_VA);

	va -= SA_MIN_VA;
	va += DVMA_BASE;

	return ((char *) va);
}

/* Destroy a DVMA address alias. */
void
dvma2_mapout(char *addr, int len)
{
	int va = (int)addr;

	/* Make sure the address is in the DVMA map. */
	if ((va < DVMA_BASE) || (va >= (DVMA_BASE + DVMA_MAPLEN)))
		panic("dvma2_mapout");
}

char *
dvma2_alloc(int len)
{
	len = m68k_round_page(len);
	dvma2_end -= len;
	return((char*)dvma2_end);
}

void
dvma2_free(char *dvma, int len)
{
	/* not worth the trouble */
}

/*****************************************************************
 * Control space stuff...
 */

u_int
get_pte(vaddr_t va)
{
	u_int pte;

	pte = get_control_word(CONTROL_ADDR_BUILD(PGMAP_BASE, va));
	if (pte & PG_VALID) {
		/* 
		 * This clears bit 30 (the kernel readable bit, which
		 * should always be set), bit 28 (which should always
		 * be set) and bit 26 (the user writable bit, which we
		 * always have tracking the kernel writable bit).  In
		 * the protection, this leaves bit 29 (the kernel
		 * writable bit) and bit 27 (the user readable bit).
		 * See pte2.h for more about this hack.
		 */
		pte &= ~(0x54000000);
		/*
		 * Flip bit 27 (the user readable bit) to become bit
		 * 27 (the PG_SYSTEM bit).
		 */
		pte ^= (PG_SYSTEM);
	}
	return (pte);
}

void
set_pte(vaddr_t va, u_int pte)
{
	if (pte & PG_VALID) {
		/* Clear bit 26 (the user writable bit).  */
		pte &= (~0x04000000);
		/*
		 * Flip bit 27 (the PG_SYSTEM bit) to become bit 27
		 * (the user readable bit).
		 */
		pte ^= (PG_SYSTEM);
		/*
		 * Always set bits 30 (the kernel readable bit) and
		 * bit 28, and set bit 26 (the user writable bit) iff
		 * bit 29 (the kernel writable bit) is set *and* bit
		 * 27 (the user readable bit) is set.  This latter bit
		 * of logic is expressed in the bizarre second term
		 * below, chosen because it needs no branches.
		 */
#if (PG_WRITE >> 2) != PG_SYSTEM
#error	"PG_WRITE and PG_SYSTEM definitions don't match!"
#endif
		pte |= 0x50000000
		    | ((((pte & PG_WRITE) >> 2) & pte) >> 1);
	}
	set_control_word(CONTROL_ADDR_BUILD(PGMAP_BASE, va), pte);
}

int
get_segmap(vaddr_t va)
{
	va = CONTROL_ADDR_BUILD(SEGMAP_BASE, va);
	return (get_control_byte(va));
}

void
set_segmap(vaddr_t va, int sme)
{
	va = CONTROL_ADDR_BUILD(SEGMAP_BASE, va);
	set_control_byte(va, sme);
}

/*
 * Copy the IDPROM contents into the passed buffer.
 * The caller (idprom.c) will do the checksum.
 */
void
sun2_getidprom(u_char *dst)
{
	vaddr_t src;	/* control space address */
	int len, x;

	src = IDPROM_BASE;
	len = sizeof(struct idprom);
	do {
		x = get_control_byte(src);
		src += NBPG;
		*dst++ = x;
	} while (--len > 0);
}

/*****************************************************************
 * Init our function pointers, etc.
 */

/*
 * For booting, the PROM in fredette's Sun 2/120 doesn't map 
 * much main memory, and what is mapped is mapped strangely.  
 * Low virtual memory is mapped like:
 *
 * 0x000000 - 0x0bffff virtual -> 0x000000 - 0x0bffff physical
 * 0x0c0000 - 0x0fffff virtual -> invalid
 * 0x100000 - 0x13ffff virtual -> 0x0c0000 - 0x0fffff physical
 * 0x200800 - 0x3fffff virtual -> 0x200800 - 0x3fffff physical
 *
 * I think the SunOS authors wanted to load kernels starting at
 * physical zero, and assumed that kernels would be less
 * than 768K (0x0c0000) long.  Also, the PROM maps physical
 * 0x0c0000 - 0x0fffff into DVMA space, so we can't take the
 * easy road and just add more mappings to use that physical
 * memory while loading (the PROM might do DMA there).
 *
 * What we do, then, is assume a 4MB machine (you'll really
 * need that to run NetBSD at all anyways), and we map two
 * chunks of physical and virtual space:
 *
 * 0x400000 - 0x4bffff virtual -> 0x000000 - 0x0bffff physical
 * 0x4c0000 - 0x600000 virtual -> 0x2c0000 - 0x3fffff physical
 *
 * And then we load starting at virtual 0x400000.  We will do 
 * all of this mapping just by copying PMEGs.
 *
 * After the load is done, but before we enter the kernel, we're
 * done with the PROM, so we copy the part of the kernel that
 * got loaded at physical 0x2c0000 down to physical 0x0c0000.
 * This can't just be a PMEG copy; we've actually got to move
 * bytes in physical memory.
 *
 * These two chunks of physical and virtual space are defined
 * in macros below.  Some of the macros are only for completeness:
 */
#define MEM_CHUNK0_SIZE			(0x0c0000)
#define MEM_CHUNK0_LOAD_PHYS		(0x000000)
#define MEM_CHUNK0_LOAD_VIRT		(0x400000)
#define MEM_CHUNK0_LOAD_VIRT_PROM	MEM_CHUNK0_LOAD_PHYS
#define MEM_CHUNK0_COPY_PHYS		MEM_CHUNK0_LOAD_PHYS
#define MEM_CHUNK0_COPY_VIRT		MEM_CHUNK0_COPY_PHYS

#define MEM_CHUNK1_SIZE			(0x140000)
#define MEM_CHUNK1_LOAD_PHYS		(0x2c0000)
#define MEM_CHUNK1_LOAD_VIRT		(MEM_CHUNK0_LOAD_VIRT + MEM_CHUNK0_SIZE)
#define MEM_CHUNK1_LOAD_VIRT_PROM	MEM_CHUNK1_LOAD_PHYS
#define MEM_CHUNK1_COPY_PHYS		(MEM_CHUNK0_LOAD_PHYS + MEM_CHUNK0_SIZE)
#define MEM_CHUNK1_COPY_VIRT		MEM_CHUNK1_COPY_PHYS

/* Maps memory for loading. */
u_long 
sun2_map_mem_load(void)
{
	vaddr_t off;

	/* Map chunk zero for loading. */
	for(off = 0; off < MEM_CHUNK0_SIZE; off += NBSG)
		set_segmap(MEM_CHUNK0_LOAD_VIRT + off,
			   get_segmap(MEM_CHUNK0_LOAD_VIRT_PROM + off));

	/* Map chunk one for loading. */
	for(off = 0; off < MEM_CHUNK1_SIZE; off += NBSG)
		set_segmap(MEM_CHUNK1_LOAD_VIRT + off,
			   get_segmap(MEM_CHUNK1_LOAD_VIRT_PROM + off));

	/* Tell our caller where in virtual space to load. */
	return MEM_CHUNK0_LOAD_VIRT;
}

/* Remaps memory for running. */
void *
sun2_map_mem_run(void *entry)
{
	vaddr_t off, off_end;
	int sme;
	u_int pte;

	/* Chunk zero is already mapped and copied. */

	/* Chunk one needs to be mapped and copied. */
	pte = (get_pte(0) & ~PG_FRAME);
	for(off = 0; off < MEM_CHUNK1_SIZE; ) {

		/*
		 * We use the PMEG immediately before the
		 * segment we're copying in the PROM virtual
		 * mapping of the chunk.  If this is the first
		 * segment, this is the PMEG the PROM used to
		 * map 0x2b8000 virtual to 0x2b8000 physical,
		 * which I'll assume is unused.  For the second
		 * and subsequent segments, this will be the
		 * PMEG used to map the previous segment, which
		 * is now (since we already copied it) unused.
		 */
		sme = get_segmap((MEM_CHUNK1_LOAD_VIRT_PROM + off) - NBSG);
		set_segmap(MEM_CHUNK1_COPY_VIRT + off, sme);

		/* Set the PTEs in this new PMEG. */
		for(off_end = off + NBSG; off < off_end; off += NBPG)
			set_pte(MEM_CHUNK1_COPY_VIRT + off, 
				pte | PA_PGNUM(MEM_CHUNK1_COPY_PHYS + off));
		
		/* Copy this segment. */
		memcpy((void *)(MEM_CHUNK1_COPY_VIRT + (off - NBSG)),
		       (void *)(MEM_CHUNK1_LOAD_VIRT + (off - NBSG)),
		       NBSG);
	}
		
	/* Tell our caller where in virtual space to enter. */
	return ((char *)entry) - MEM_CHUNK0_LOAD_VIRT;
}

void 
sun2_init(void)
{
	/* Set the function pointers. */
	dev_mapin_p   = dev2_mapin;
	dvma_alloc_p  = dvma2_alloc;
	dvma_free_p   = dvma2_free;
	dvma_mapin_p  = dvma2_mapin;
	dvma_mapout_p = dvma2_mapout;
       
	/* Prepare DVMA segment. */
	dvma2_init();
}