xref: /netbsd-src/sys/arch/powerpc/booke/dev/cpunode.c (revision c7fb772b85b2b5d4cfb282f868f454b4701534fd)
1*c7fb772bSthorpej /*	$NetBSD: cpunode.c,v 1.8 2021/08/07 16:19:02 thorpej Exp $	*/
2b8ea2c8cSmatt /*-
3b8ea2c8cSmatt  * Copyright (c) 2010, 2011 The NetBSD Foundation, Inc.
4b8ea2c8cSmatt  * All rights reserved.
5b8ea2c8cSmatt  *
6b8ea2c8cSmatt  * This code is derived from software contributed to The NetBSD Foundation
7b8ea2c8cSmatt  * by Raytheon BBN Technologies Corp and Defense Advanced Research Projects
8b8ea2c8cSmatt  * Agency and which was developed by Matt Thomas of 3am Software Foundry.
9b8ea2c8cSmatt  *
10b8ea2c8cSmatt  * This material is based upon work supported by the Defense Advanced Research
11b8ea2c8cSmatt  * Projects Agency and Space and Naval Warfare Systems Center, Pacific, under
12b8ea2c8cSmatt  * Contract No. N66001-09-C-2073.
13b8ea2c8cSmatt  * Approved for Public Release, Distribution Unlimited
14b8ea2c8cSmatt  *
15b8ea2c8cSmatt  * Redistribution and use in source and binary forms, with or without
16b8ea2c8cSmatt  * modification, are permitted provided that the following conditions
17b8ea2c8cSmatt  * are met:
18b8ea2c8cSmatt  * 1. Redistributions of source code must retain the above copyright
19b8ea2c8cSmatt  *    notice, this list of conditions and the following disclaimer.
20b8ea2c8cSmatt  * 2. Redistributions in binary form must reproduce the above copyright
21b8ea2c8cSmatt  *    notice, this list of conditions and the following disclaimer in the
22b8ea2c8cSmatt  *    documentation and/or other materials provided with the distribution.
23b8ea2c8cSmatt  *
24b8ea2c8cSmatt  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
25b8ea2c8cSmatt  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
26b8ea2c8cSmatt  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
27b8ea2c8cSmatt  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
28b8ea2c8cSmatt  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29b8ea2c8cSmatt  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30b8ea2c8cSmatt  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31b8ea2c8cSmatt  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32b8ea2c8cSmatt  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33b8ea2c8cSmatt  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34b8ea2c8cSmatt  * POSSIBILITY OF SUCH DAMAGE.
35b8ea2c8cSmatt  */
36b8ea2c8cSmatt 
37b8ea2c8cSmatt #include <sys/cdefs.h>
38*c7fb772bSthorpej __KERNEL_RCSID(0, "$NetBSD: cpunode.c,v 1.8 2021/08/07 16:19:02 thorpej Exp $");
39b8ea2c8cSmatt 
4016031f7dSrin #include "ioconf.h"
41b8ea2c8cSmatt 
42b8ea2c8cSmatt #include <sys/param.h>
43b8ea2c8cSmatt #include <sys/device.h>
44b8ea2c8cSmatt #include <sys/cpu.h>
45b8ea2c8cSmatt 
46b8ea2c8cSmatt #include <powerpc/booke/cpuvar.h>
47b8ea2c8cSmatt 
48b8ea2c8cSmatt static int cpunode_match(device_t, cfdata_t, void *);
49b8ea2c8cSmatt static void cpunode_attach(device_t, device_t, void *);
50b8ea2c8cSmatt 
51b8ea2c8cSmatt CFATTACH_DECL_NEW(cpunode, sizeof(struct cpunode_softc),
52b8ea2c8cSmatt     cpunode_match, cpunode_attach, NULL, NULL);
53b8ea2c8cSmatt 
54b8ea2c8cSmatt static u_int nodes;
55b8ea2c8cSmatt 
56b8ea2c8cSmatt static int
cpunode_match(device_t parent,cfdata_t cf,void * aux)57b8ea2c8cSmatt cpunode_match(device_t parent, cfdata_t cf, void *aux)
58b8ea2c8cSmatt {
59b8ea2c8cSmatt 	struct mainbus_attach_args * const ma = aux;
60b8ea2c8cSmatt 	if (strcmp(ma->ma_name, cpunode_cd.cd_name) != 0)
61b8ea2c8cSmatt 		return 0;
62b8ea2c8cSmatt 
63b8ea2c8cSmatt 	if (ma->ma_node > 8 || (nodes & (1 << ma->ma_node)))
64b8ea2c8cSmatt 		return 0;
65b8ea2c8cSmatt 
66b8ea2c8cSmatt 	return 1;
67b8ea2c8cSmatt }
68b8ea2c8cSmatt 
69b8ea2c8cSmatt static int
cpunode_print(void * aux,const char * pnp)70b8ea2c8cSmatt cpunode_print(void *aux, const char *pnp)
71b8ea2c8cSmatt {
72b8ea2c8cSmatt 	struct cpunode_attach_args *cna = aux;
73b8ea2c8cSmatt 
74b8ea2c8cSmatt 	if (pnp)
75b8ea2c8cSmatt #if 0
76b8ea2c8cSmatt 		return QUIET;
77b8ea2c8cSmatt #else
78b8ea2c8cSmatt 		aprint_normal("%s at %s", cna->cna_locs.cnl_name, pnp);
79b8ea2c8cSmatt #endif
80b8ea2c8cSmatt 
81b8ea2c8cSmatt 	if (cna->cna_locs.cnl_instance != 0)
82b8ea2c8cSmatt 		aprint_normal(" instance %d", cna->cna_locs.cnl_instance);
83b8ea2c8cSmatt 
84b8ea2c8cSmatt 	return UNCONF;
85b8ea2c8cSmatt }
86b8ea2c8cSmatt 
87b8ea2c8cSmatt static void
cpunode_attach(device_t parent,device_t self,void * aux)88b8ea2c8cSmatt cpunode_attach(device_t parent, device_t self, void *aux)
89b8ea2c8cSmatt {
90b8ea2c8cSmatt 	const struct cpunode_locators *cnl = cpu_md_ops.md_cpunode_locs;
91b8ea2c8cSmatt 	struct cpunode_softc * const sc = device_private(self);
92b8ea2c8cSmatt 	struct mainbus_attach_args * const ma = aux;
93b8ea2c8cSmatt 	struct cpunode_attach_args cna;
94b8ea2c8cSmatt 
95b8ea2c8cSmatt 	sc->sc_dev = self;
96b8ea2c8cSmatt 
97b8ea2c8cSmatt 	aprint_normal("\n");
98b8ea2c8cSmatt 	aprint_normal_dev(self,
99b8ea2c8cSmatt 	    "%"PRIu64"KB/%"PRIu64"B %"PRIu64"-banked %"PRIu64"-way unified L2 cache\n",
100b8ea2c8cSmatt 	    board_info_get_number("l2-cache-size") / 1024,
101b8ea2c8cSmatt 	    board_info_get_number("l2-cache-line-size"),
102b8ea2c8cSmatt 	    board_info_get_number("l2-cache-banks"),
103b8ea2c8cSmatt 	    board_info_get_number("l2-cache-ways"));
104b8ea2c8cSmatt 
105b8ea2c8cSmatt 	nodes |= 1 << ma->ma_node;
106b8ea2c8cSmatt 
107ccada5e8Smatt 	const uint16_t my_id = board_info_get_number("my-id");
108ccada5e8Smatt 
109ccada5e8Smatt 	for (u_int childmask = 1; cnl->cnl_name != NULL; cnl++) {
110ccada5e8Smatt 		bool inclusive = true;
111ccada5e8Smatt 		bool found = (cnl->cnl_ids[0] == 0);
112b887a107Smatt 
113b887a107Smatt #if DEBUG > 1
114b887a107Smatt 		aprint_normal_dev(self, "dev=%s[%u], addr=%x@%x",
115b887a107Smatt 		    cnl->cnl_name, cnl->cnl_instance, cnl->cnl_size,
116b887a107Smatt 		    cnl->cnl_addr);
117b887a107Smatt 		if (cnl->cnl_nintr > 0) {
118b887a107Smatt 			aprint_normal(", intrs=%u", cnl->cnl_intrs[0]);
119b887a107Smatt 			for (u_int i = 1; i < cnl->cnl_nintr; i++)
120b887a107Smatt 				aprint_normal(",%u", cnl->cnl_intrs[i]);
121b887a107Smatt 		}
122b887a107Smatt 		aprint_normal("\n");
123b887a107Smatt #endif
124b887a107Smatt 
125ccada5e8Smatt 		for (u_int i = 0;
126ccada5e8Smatt 		     !found
127ccada5e8Smatt 		     && i < __arraycount(cnl->cnl_ids)
128ccada5e8Smatt 		     && cnl->cnl_ids[i] != 0;
129ccada5e8Smatt 		     i++) {
130ccada5e8Smatt 			if (cnl->cnl_ids[i] == 0xffff) {
131ccada5e8Smatt 				inclusive = false;
132ccada5e8Smatt 				continue;
133ccada5e8Smatt 			}
134ccada5e8Smatt 			found = (cnl->cnl_ids[i] == my_id);
135ccada5e8Smatt 		}
136ccada5e8Smatt 		/*
137ccada5e8Smatt 		 * found & inclusive == match
138ccada5e8Smatt 		 * !found & !inclusive == match
139ccada5e8Smatt 		 * found & !inclusive == no match
140ccada5e8Smatt 		 * !found & inclusive == no match
141ccada5e8Smatt 		 * therefore
142ccada5e8Smatt 		 * found ^ inclusive = no match
143ccada5e8Smatt 		 * so
144ccada5e8Smatt 		 * !(found ^ inclusive) = match
145ccada5e8Smatt 		 */
146ccada5e8Smatt 		if (found ^ inclusive)
147ccada5e8Smatt 			continue;
148ccada5e8Smatt 
149b8ea2c8cSmatt 		cna.cna_busname = "cpunode";
150b8ea2c8cSmatt 		cna.cna_memt = ma->ma_memt;
151cac052afSmatt 		cna.cna_le_memt = ma->ma_le_memt;
152b8ea2c8cSmatt 		cna.cna_dmat = ma->ma_dmat;
153b8ea2c8cSmatt 		cna.cna_childmask = childmask;
154b8ea2c8cSmatt 		cna.cna_locs = *cnl;
155b8ea2c8cSmatt 
1562685996bSthorpej 		config_found(self, &cna, cpunode_print,
157*c7fb772bSthorpej 		    CFARGS(.iattr = "cpunode"));
158ccada5e8Smatt 		childmask <<= 1;
159b8ea2c8cSmatt 	}
160b8ea2c8cSmatt 	/*
161b8ea2c8cSmatt 	 * Anything MD left to do?
162b8ea2c8cSmatt 	 */
163b8ea2c8cSmatt 	if (cpu_md_ops.md_cpunode_attach != NULL)
164b8ea2c8cSmatt 		(*cpu_md_ops.md_cpunode_attach)(parent, self, aux);
165b8ea2c8cSmatt }
166b8ea2c8cSmatt 
167b8ea2c8cSmatt static int cpu_match(device_t, cfdata_t, void *);
168b8ea2c8cSmatt static void cpu_attach(device_t, device_t, void *);
169b8ea2c8cSmatt 
170b8ea2c8cSmatt CFATTACH_DECL_NEW(cpu, 0,
171b8ea2c8cSmatt     cpu_match, cpu_attach, NULL, NULL);
172b8ea2c8cSmatt 
173b8ea2c8cSmatt static int
cpu_match(device_t parent,cfdata_t cf,void * aux)174b8ea2c8cSmatt cpu_match(device_t parent, cfdata_t cf, void *aux)
175b8ea2c8cSmatt {
176b8ea2c8cSmatt 	struct cpunode_softc * const psc = device_private(parent);
177b8ea2c8cSmatt 	struct cpunode_attach_args * const cna = aux;
178b8ea2c8cSmatt 
179b8ea2c8cSmatt 	if (strcmp(cna->cna_locs.cnl_name, cpu_cd.cd_name) != 0)
180b8ea2c8cSmatt 		return 0;
181b8ea2c8cSmatt 
182b8ea2c8cSmatt 	if (psc->sc_children & cna->cna_childmask)
183b8ea2c8cSmatt 		return 0;
184b8ea2c8cSmatt 
185b8ea2c8cSmatt 	return 1;
186b8ea2c8cSmatt }
187b8ea2c8cSmatt 
188b8ea2c8cSmatt static void
cpu_attach(device_t parent,device_t self,void * aux)189b8ea2c8cSmatt cpu_attach(device_t parent, device_t self, void *aux)
190b8ea2c8cSmatt {
191b8ea2c8cSmatt 	struct cpunode_softc * const psc = device_private(parent);
192b8ea2c8cSmatt 	struct cpunode_attach_args * const cna = aux;
193b8ea2c8cSmatt 
194b8ea2c8cSmatt 	psc->sc_children |= cna->cna_childmask;
195b8ea2c8cSmatt 
196b8ea2c8cSmatt 	aprint_normal("\n");
197b8ea2c8cSmatt 
198b8ea2c8cSmatt 	(*cpu_md_ops.md_cpu_attach)(self, cna->cna_locs.cnl_instance);
199b8ea2c8cSmatt }
200