xref: /netbsd/sys/arch/hpc/hpc/platid.c (revision a9f4c28a)
1*a9f4c28aSskrll /*	$NetBSD: platid.c,v 1.11 2006/05/10 06:24:02 skrll Exp $	*/
26be22c0cSuch 
36be22c0cSuch /*-
46be22c0cSuch  * Copyright (c) 1999-2001
56be22c0cSuch  *         Shin Takemura and PocketBSD Project. All rights reserved.
66be22c0cSuch  *
76be22c0cSuch  * Redistribution and use in source and binary forms, with or without
86be22c0cSuch  * modification, are permitted provided that the following conditions
96be22c0cSuch  * are met:
106be22c0cSuch  * 1. Redistributions of source code must retain the above copyright
116be22c0cSuch  *    notice, this list of conditions and the following disclaimer.
126be22c0cSuch  * 2. Redistributions in binary form must reproduce the above copyright
136be22c0cSuch  *    notice, this list of conditions and the following disclaimer in the
146be22c0cSuch  *    documentation and/or other materials provided with the distribution.
156be22c0cSuch  * 3. All advertising materials mentioning features or use of this software
166be22c0cSuch  *    must display the following acknowledgement:
176be22c0cSuch  *	This product includes software developed by the PocketBSD project
186be22c0cSuch  *	and its contributors.
196be22c0cSuch  * 4. Neither the name of the project nor the names of its contributors
206be22c0cSuch  *    may be used to endorse or promote products derived from this software
216be22c0cSuch  *    without specific prior written permission.
226be22c0cSuch  *
236be22c0cSuch  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
246be22c0cSuch  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
256be22c0cSuch  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
266be22c0cSuch  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
276be22c0cSuch  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
286be22c0cSuch  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
296be22c0cSuch  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
306be22c0cSuch  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
316be22c0cSuch  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
326be22c0cSuch  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
336be22c0cSuch  * SUCH DAMAGE.
346be22c0cSuch  *
356be22c0cSuch  */
366be22c0cSuch 
370c82163cSlukem #include <sys/cdefs.h>
385f834996Suwe #if !defined(_WIN32)	/* XXX: hpcboot.exe */
39*a9f4c28aSskrll __KERNEL_RCSID(0, "$NetBSD: platid.c,v 1.11 2006/05/10 06:24:02 skrll Exp $");
405f834996Suwe #endif
410c82163cSlukem 
426be22c0cSuch #include <sys/types.h>
436be22c0cSuch #include <sys/param.h>
446be22c0cSuch #include <sys/systm.h>
456be22c0cSuch 
466be22c0cSuch #include <machine/platid.h>
476be22c0cSuch 
486be22c0cSuch platid_t platid_unknown = {{ PLATID_UNKNOWN, PLATID_UNKNOWN }};
496be22c0cSuch platid_t platid_wild = {{ PLATID_WILD, PLATID_WILD }};
506be22c0cSuch platid_t platid = {{ PLATID_UNKNOWN, PLATID_UNKNOWN }};
516be22c0cSuch 
526be22c0cSuch void
platid_ntoh(platid_t * pid)536be22c0cSuch platid_ntoh(platid_t *pid)
546be22c0cSuch {
5569ed2175Such 
566be22c0cSuch 	pid->dw.dw0 = ntohl(pid->dw.dw0);
576be22c0cSuch 	pid->dw.dw1 = ntohl(pid->dw.dw1);
586be22c0cSuch }
596be22c0cSuch 
606be22c0cSuch void
platid_hton(platid_t * pid)616be22c0cSuch platid_hton(platid_t *pid)
626be22c0cSuch {
6369ed2175Such 
646be22c0cSuch 	pid->dw.dw0 = htonl(pid->dw.dw0);
656be22c0cSuch 	pid->dw.dw1 = htonl(pid->dw.dw1);
666be22c0cSuch }
676be22c0cSuch 
680d1e222eSuwe #ifdef PLATID_TEST
696be22c0cSuch void
platid_dump(const char * name,void * pxx)700d1e222eSuwe platid_dump(const char *name, void* pxx)
716be22c0cSuch {
726be22c0cSuch 	int i;
736be22c0cSuch 	unsigned char* p = (unsigned char*)pxx;
7469ed2175Such 
756be22c0cSuch 	printf("%14s: ", name);
766be22c0cSuch 
776be22c0cSuch 	for (i = 0; i < 8; i++) {
786be22c0cSuch 		printf("%02x", p[i]);
796be22c0cSuch 	}
806be22c0cSuch 	printf("\n");
816be22c0cSuch }
820d1e222eSuwe #endif
836be22c0cSuch 
846be22c0cSuch int
platid_match(platid_t * pid,platid_mask_t * mask)85e7b97196Suwe platid_match(platid_t *pid, platid_mask_t *mask)
866be22c0cSuch {
8769ed2175Such 
88e7b97196Suwe 	return (platid_match_sub(pid, mask, 0));
896be22c0cSuch }
906be22c0cSuch 
916be22c0cSuch int
platid_match_sub(platid_t * pid,platid_mask_t * mask,int unknown_is_match)92e7b97196Suwe platid_match_sub(platid_t *pid, platid_mask_t *mask, int unknown_is_match)
936be22c0cSuch {
946be22c0cSuch 	int match_count;
956be22c0cSuch 
966be22c0cSuch #define PLATID_MATCH(mbr)				\
97e7b97196Suwe 	if (pid->s.mbr != mask->s.mbr &&		\
986be22c0cSuch 	    mask->s.mbr != platid_wild.s.mbr &&		\
99e7b97196Suwe 	    !(pid->s.mbr == platid_unknown.s.mbr &&	\
1006be22c0cSuch 	     unknown_is_match)) {			\
1016be22c0cSuch 		return (0);				\
102e7b97196Suwe 	} else if (pid->s.mbr == mask->s.mbr) {		\
1036be22c0cSuch 		match_count++;				\
1046be22c0cSuch 	}
1056be22c0cSuch 
1066be22c0cSuch 	match_count = 1;
1076be22c0cSuch 	PLATID_MATCH(cpu_submodel);
1086be22c0cSuch 	PLATID_MATCH(cpu_model);
1096be22c0cSuch 	PLATID_MATCH(cpu_series);
1106be22c0cSuch 	PLATID_MATCH(cpu_arch);
1116be22c0cSuch 
1126be22c0cSuch 	PLATID_MATCH(submodel);
1136be22c0cSuch 	PLATID_MATCH(model);
1146be22c0cSuch 	PLATID_MATCH(series);
1156be22c0cSuch 	PLATID_MATCH(vendor);
1166be22c0cSuch 
1176be22c0cSuch 	return (match_count);
1186be22c0cSuch 
1196be22c0cSuch #undef PLATID_MATCH
1206be22c0cSuch }
1216be22c0cSuch 
12273c9c64fSuwe const tchar *
platid_name(platid_t * pid)123e7b97196Suwe platid_name(platid_t *pid)
1246be22c0cSuch {
125ca599650Stakemura 	struct platid_name *match;
1266be22c0cSuch 
127e7b97196Suwe 	match = platid_search(pid,
128ca599650Stakemura 	    platid_name_table, platid_name_table_size,
129ca599650Stakemura 	    sizeof(struct platid_name));
130ca599650Stakemura 
13173c9c64fSuwe 	return ((match != NULL) ? match->name : TEXT("UNKNOWN"));
1326be22c0cSuch }
1336be22c0cSuch 
1346be22c0cSuch struct platid_data *
platid_search_data(platid_t * pid,struct platid_data * datap)135e7b97196Suwe platid_search_data(platid_t *pid, struct platid_data *datap)
1366be22c0cSuch {
1376be22c0cSuch 
138e7b97196Suwe 	while (datap->mask != NULL && !platid_match(pid, datap->mask))
1396be22c0cSuch 		datap++;
1406be22c0cSuch 	if (datap->mask == NULL && datap->data == NULL)
14169ed2175Such 		return (NULL);
14269ed2175Such 
14369ed2175Such 	return (datap);
1446be22c0cSuch }
145ca599650Stakemura 
146ca599650Stakemura void *
platid_search(platid_t * pid,void * base,int nmemb,int size)147e7b97196Suwe platid_search(platid_t *pid, void *base, int nmemb, int size)
148ca599650Stakemura {
149ca599650Stakemura 	int i, match_level, res;
150ca599650Stakemura 	void *match;
151ca599650Stakemura 
152ca599650Stakemura 	match_level = 0;
153ca599650Stakemura 	match = NULL;
154ca599650Stakemura 	for (i = 0; i < nmemb; i++) {
155e7b97196Suwe 		res = platid_match(pid, *(platid_mask_t**)base);
156ca599650Stakemura 		if (match_level < res) {
157ca599650Stakemura 			match_level = res;
158ca599650Stakemura 			match = base;
159ca599650Stakemura 		}
160*a9f4c28aSskrll 		base = (char *)base + size;
161ca599650Stakemura 	}
162ca599650Stakemura 
163ca599650Stakemura 	return (match);
164ca599650Stakemura }
165