1/*	$OpenBSD: memprobe.c,v 1.57 2016/06/10 18:36:06 jcs Exp $	*/
2
3/*
4 * Copyright (c) 1997-1999 Michael Shalayeff
5 * Copyright (c) 1997-1999 Tobias Weingartner
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 */
30
31#include <sys/param.h>
32#include <machine/biosvar.h>
33#include <dev/isa/isareg.h>
34#include <stand/boot/bootarg.h>
35#include "libsa.h"
36
37u_int cnvmem, extmem;		/* XXX - compatibility */
38
39bios_memmap_t bios_memmap[64];	/* This is easier */
40#ifndef EFIBOOT
41/*
42 * Check gateA20
43 *
44 * A sanity check.
45 */
46static __inline int
47checkA20(void)
48{
49	register char *p = (char *)0x100000;
50	register char *q = (char *)0x000000;
51	int st;
52
53	/* Simple check */
54	if (*p != *q)
55		return 1;
56
57	/* Complex check */
58	*p = ~(*p);
59	st = (*p != *q);
60	*p = ~(*p);
61
62	return st;
63}
64
65/*
66 * BIOS int 15, AX=E820
67 *
68 * This is the "preferred" method.
69 */
70static __inline bios_memmap_t *
71bios_E820(bios_memmap_t *mp)
72{
73	int rc, off = 0, sig, gotcha = 0;
74
75	do {
76		BIOS_regs.biosr_es = ((u_int)(mp) >> 4);
77		__asm volatile(DOINT(0x15) "; setc %b1"
78		    : "=a" (sig), "=d" (rc), "=b" (off)
79		    : "0" (0xE820), "1" (0x534d4150), "b" (off),
80		      "c" (sizeof(*mp)), "D" (((u_int)mp) & 0xf)
81		    : "cc", "memory");
82		off = BIOS_regs.biosr_bx;
83
84		if (rc & 0xff || sig != 0x534d4150)
85			break;
86		gotcha++;
87		if (!mp->type)
88			mp->type = BIOS_MAP_RES;
89		mp++;
90	} while (off);
91
92	if (!gotcha)
93		return NULL;
94#ifdef DEBUG
95	printf("0x15[E820] ");
96#endif
97	return mp;
98}
99
100/*
101 * BIOS int 15, AX=8800
102 *
103 * Only used if int 15, AX=E801 does not work.
104 * Machines with this are restricted to 64MB.
105 */
106static __inline bios_memmap_t *
107bios_8800(bios_memmap_t *mp)
108{
109	int rc, mem;
110
111	__asm volatile(DOINT(0x15) "; setc %b0"
112	    : "=c" (rc), "=a" (mem) : "a" (0x8800));
113
114	if (rc & 0xff)
115		return NULL;
116#ifdef DEBUG
117	printf("0x15[8800] ");
118#endif
119	/* Fill out a BIOS_MAP */
120	mp->addr = 1024 * 1024;		/* 1MB */
121	mp->size = (mem & 0xffff) * 1024;
122	mp->type = BIOS_MAP_FREE;
123
124	return ++mp;
125}
126
127/*
128 * BIOS int 0x12 Get Conventional Memory
129 *
130 * Only used if int 15, AX=E820 does not work.
131 */
132static __inline bios_memmap_t *
133bios_int12(bios_memmap_t *mp)
134{
135	int mem;
136#ifdef DEBUG
137	printf("0x12 ");
138#endif
139	__asm volatile(DOINT(0x12) : "=a" (mem) :: "%ecx", "%edx", "cc");
140
141	/* Fill out a bios_memmap_t */
142	mp->addr = 0;
143	mp->size = (mem & 0xffff) * 1024;
144	mp->type = BIOS_MAP_FREE;
145
146	return ++mp;
147}
148
149
150/*
151 * addrprobe(kloc): Probe memory at address kloc * 1024.
152 *
153 * This is a hack, but it seems to work ok.  Maybe this is
154 * the *real* way that you are supposed to do probing???
155 *
156 * Modify the original a bit.  We write everything first, and
157 * then test for the values.  This should croak on machines that
158 * return values just written on non-existent memory...
159 *
160 * BTW: These machines are pretty broken IMHO.
161 *
162 * XXX - Does not detect aliased memory.
163 */
164const u_int addrprobe_pat[] = {
165	0x00000000, 0xFFFFFFFF,
166	0x01010101, 0x10101010,
167	0x55555555, 0xCCCCCCCC
168};
169static int
170addrprobe(u_int kloc)
171{
172	volatile u_int *loc;
173	register u_int i, ret = 0;
174	u_int save[nitems(addrprobe_pat)];
175
176	/* Get location */
177	loc = (int *)(intptr_t)(kloc * 1024);
178
179	save[0] = *loc;
180	/* Probe address */
181	for (i = 0; i < nitems(addrprobe_pat); i++) {
182		*loc = addrprobe_pat[i];
183		if (*loc != addrprobe_pat[i])
184			ret++;
185	}
186	*loc = save[0];
187
188	if (!ret) {
189		/* Write address */
190		for (i = 0; i < nitems(addrprobe_pat); i++) {
191			save[i] = loc[i];
192			loc[i] = addrprobe_pat[i];
193		}
194
195		/* Read address */
196		for (i = 0; i < nitems(addrprobe_pat); i++) {
197			if (loc[i] != addrprobe_pat[i])
198				ret++;
199			loc[i] = save[i];
200		}
201	}
202
203	return ret;
204}
205
206/*
207 * Probe for all extended memory.
208 *
209 * This is only used as a last resort.  If we resort to this
210 * routine, we are getting pretty desperate.  Hopefully nobody
211 * has to rely on this after all the work above.
212 *
213 * XXX - Does not detect aliased memory.
214 * XXX - Could be destructive, as it does write.
215 */
216static __inline bios_memmap_t *
217badprobe(bios_memmap_t *mp)
218{
219	u_int64_t ram;
220#ifdef DEBUG
221	printf("scan ");
222#endif
223	/*
224	 * probe extended memory
225	 *
226	 * There is no need to do this in assembly language.  This is
227	 * much easier to debug in C anyways.
228	 */
229	for (ram = 1024; ram < 512 * 1024; ram += 4)
230		if (addrprobe(ram))
231			break;
232
233	mp->addr = 1024 * 1024;
234	mp->size = (ram - 1024) * 1024;
235	mp->type = BIOS_MAP_FREE;
236
237	return ++mp;
238}
239
240bios_memmap_t bios_memmap[64];	/* This is easier */
241
242void
243memprobe(void)
244{
245	bios_memmap_t *pm = bios_memmap, *im;
246
247#ifdef DEBUG
248	printf(" mem(");
249#else
250	printf(" mem[");
251#endif
252
253	if ((pm = bios_E820(bios_memmap)) == NULL) {
254		im = bios_int12(bios_memmap);
255		pm = bios_8800(im);
256		if (pm == NULL)
257			pm = badprobe(im);
258		if (pm == NULL) {
259			printf(" No Extended memory detected.");
260			pm = im;
261		}
262	}
263	pm->type = BIOS_MAP_END;
264
265	/* XXX - gotta peephole optimize the list */
266
267	/* Remove APM needed RAM */
268	apmfixmem();
269
270#ifdef DEBUG
271	printf(")[");
272#endif
273
274	/* XXX - Compatibility, remove later (smpprobe() relies on it) */
275	extmem = cnvmem = 0;
276	for (im = bios_memmap; im->type != BIOS_MAP_END; im++) {
277		/* Count only "good" memory chunks 12K and up in size */
278		if ((im->type == BIOS_MAP_FREE) && (im->size >= 12 * 1024)) {
279			if (im->size > 1024 * 1024)
280				printf("%uM ", (u_int)(im->size /
281				    (1024 * 1024)));
282			else
283				printf("%uK ", (u_int)im->size / 1024);
284
285			/*
286			 * Compute compatibility values:
287			 * cnvmem -- is the upper boundary of conventional
288			 *	memory (below IOM_BEGIN (=640k))
289			 * extmem -- is the size of the contiguous extended
290			 *	memory segment starting at 1M
291			 *
292			 * We ignore "good" memory in the 640K-1M hole.
293			 * We drop "machine {cnvmem,extmem}" commands.
294			 */
295			if (im->addr < IOM_BEGIN)
296				cnvmem = max(cnvmem,
297				    im->addr + im->size) / 1024;
298			if (im->addr >= IOM_END)
299				extmem += im->size / 1024;
300		}
301	}
302
303	/* Check if gate A20 is on */
304	printf("a20=o%s] ", checkA20()? "n" : "ff!");
305}
306#endif
307
308void
309dump_biosmem(bios_memmap_t *tm)
310{
311	register bios_memmap_t *p;
312	register u_int total = 0;
313
314	if (tm == NULL)
315		tm = bios_memmap;
316
317	for (p = tm; p->type != BIOS_MAP_END; p++) {
318		printf("Region %ld: type %u at 0x%llx for %uKB\n",
319		    (long)(p - tm), p->type, p->addr,
320		    (u_int)(p->size / 1024));
321
322		if (p->type == BIOS_MAP_FREE)
323			total += p->size / 1024;
324	}
325
326	printf("Low ram: %dKB  High ram: %dKB\n", cnvmem, extmem);
327	printf("Total free memory: %uKB\n", total);
328}
329
330int
331mem_limit(long long ml)
332{
333	register bios_memmap_t *p;
334
335	for (p = bios_memmap; p->type != BIOS_MAP_END; p++) {
336		register int64_t sp = p->addr, ep = p->addr + p->size;
337
338		if (p->type != BIOS_MAP_FREE)
339			continue;
340
341		/* Wholly above limit, nuke it */
342		if ((sp >= ml) && (ep >= ml)) {
343			bcopy (p + 1, p, (char *)bios_memmap +
344			       sizeof(bios_memmap) - (char *)p);
345		} else if ((sp < ml) && (ep >= ml)) {
346			p->size -= (ep - ml);
347		}
348	}
349	return 0;
350}
351
352int
353mem_delete(long long sa, long long ea)
354{
355	register bios_memmap_t *p;
356
357	for (p = bios_memmap; p->type != BIOS_MAP_END; p++) {
358		if (p->type == BIOS_MAP_FREE) {
359			register int64_t sp = p->addr, ep = p->addr + p->size;
360
361			/* can we eat it as a whole? */
362			if ((sa - sp) <= PAGE_SIZE && (ep - ea) <= PAGE_SIZE) {
363				bcopy(p + 1, p, (char *)bios_memmap +
364				    sizeof(bios_memmap) - (char *)p);
365				break;
366			/* eat head or legs */
367			} else if (sa <= sp && sp < ea) {
368				p->addr = ea;
369				p->size = ep - ea;
370				break;
371			} else if (sa < ep && ep <= ea) {
372				p->size = sa - sp;
373				break;
374			} else if (sp < sa && ea < ep) {
375				/* bite in half */
376				bcopy(p, p + 1, (char *)bios_memmap +
377				    sizeof(bios_memmap) - (char *)p -
378				    sizeof(bios_memmap[0]));
379				p[1].addr = ea;
380				p[1].size = ep - ea;
381				p->size = sa - sp;
382				break;
383			}
384		}
385	}
386	return 0;
387}
388
389int
390mem_add(long long sa, long long ea)
391{
392	register bios_memmap_t *p;
393
394	for (p = bios_memmap; p->type != BIOS_MAP_END; p++) {
395		if (p->type == BIOS_MAP_FREE) {
396			register int64_t sp = p->addr, ep = p->addr + p->size;
397
398			/* is it already there? */
399			if (sp <= sa && ea <= ep) {
400				break;
401			/* join head or legs */
402			} else if (sa < sp && sp <= ea) {
403				p->addr = sa;
404				p->size = ep - sa;
405				break;
406			} else if (sa <= ep && ep < ea) {
407				p->size = ea - sp;
408				break;
409			} else if (ea < sp) {
410				/* insert before */
411				bcopy(p, p + 1, (char *)bios_memmap +
412				    sizeof(bios_memmap) - (char *)(p - 1));
413				p->addr = sa;
414				p->size = ea - sa;
415				break;
416			}
417		}
418	}
419
420	/* meaning add new item at the end of the list */
421	if (p->type == BIOS_MAP_END) {
422		p[1] = p[0];
423		p->type = BIOS_MAP_FREE;
424		p->addr = sa;
425		p->size = ea - sa;
426	}
427
428	return 0;
429}
430
431void
432mem_pass(void)
433{
434	bios_memmap_t *p;
435
436	for (p = bios_memmap; p->type != BIOS_MAP_END; p++)
437		;
438	addbootarg(BOOTARG_MEMMAP, (p - bios_memmap + 1) * sizeof *bios_memmap,
439	    bios_memmap);
440}
441