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