1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*
28  * sun4u specific DDI implementation
29  */
30 #include <sys/bootconf.h>
31 #include <sys/conf.h>
32 #include <sys/ddi_subrdefs.h>
33 #include <sys/ethernet.h>
34 #include <sys/idprom.h>
35 #include <sys/machsystm.h>
36 #include <sys/promif.h>
37 #include <sys/prom_plat.h>
38 #include <sys/sunndi.h>
39 #include <sys/systeminfo.h>
40 #include <sys/fpu/fpusystm.h>
41 #include <sys/vm.h>
42 #include <sys/fs/dv_node.h>
43 #include <sys/fs/snode.h>
44 
45 /*
46  * Favored drivers of this implementation
47  * architecture.  These drivers MUST be present for
48  * the system to boot at all.
49  */
50 char *impl_module_list[] = {
51 	"rootnex",
52 	"options",
53 	"sad",		/* Referenced via init_tbl[] */
54 	"pseudo",
55 	"clone",
56 	"scsi_vhci",
57 	(char *)0
58 };
59 
60 /*
61  * These strings passed to not_serviced in locore.s
62  */
63 const char busname_ovec[] = "onboard ";
64 const char busname_svec[] = "SBus ";
65 const char busname_vec[] = "";
66 
67 
68 static uint64_t *intr_map_reg[32];
69 
70 /*
71  * Forward declarations
72  */
73 static int getlongprop_buf();
74 static int get_boardnum(int nid, dev_info_t *par);
75 
76 /*
77  * Check the status of the device node passed as an argument.
78  *
79  *	if ((status is OKAY) || (status is DISABLED))
80  *		return DDI_SUCCESS
81  *	else
82  *		print a warning and return DDI_FAILURE
83  */
84 /*ARGSUSED*/
85 int
check_status(int id,char * buf,dev_info_t * parent)86 check_status(int id, char *buf, dev_info_t *parent)
87 {
88 	char status_buf[64];
89 	char devtype_buf[OBP_MAXPROPNAME];
90 	char board_buf[32];
91 	char path[OBP_MAXPATHLEN];
92 	int boardnum;
93 	int retval = DDI_FAILURE;
94 	extern int status_okay(int, char *, int);
95 
96 	/*
97 	 * is the status okay?
98 	 */
99 	if (status_okay(id, status_buf, sizeof (status_buf)))
100 		return (DDI_SUCCESS);
101 
102 	/*
103 	 * a status property indicating bad memory will be associated
104 	 * with a node which has a "device_type" property with a value of
105 	 * "memory-controller". in this situation, return DDI_SUCCESS
106 	 */
107 	if (getlongprop_buf(id, OBP_DEVICETYPE, devtype_buf,
108 	    sizeof (devtype_buf)) > 0) {
109 		if (strcmp(devtype_buf, "memory-controller") == 0)
110 			retval = DDI_SUCCESS;
111 	}
112 
113 	/*
114 	 * get the full OBP pathname of this node
115 	 */
116 	if (prom_phandle_to_path((phandle_t)id, path, sizeof (path)) < 0)
117 		cmn_err(CE_WARN, "prom_phandle_to_path(%d) failed", id);
118 
119 	/*
120 	 * get the board number, if one exists
121 	 */
122 	if ((boardnum = get_boardnum(id, parent)) >= 0)
123 		(void) sprintf(board_buf, " on board %d", boardnum);
124 	else
125 		board_buf[0] = '\0';
126 
127 	/*
128 	 * print the status property information
129 	 */
130 	cmn_err(CE_WARN, "status '%s' for '%s'%s",
131 	    status_buf, path, board_buf);
132 	return (retval);
133 }
134 
135 /*
136  * determine the board number associated with this nodeid
137  */
138 static int
get_boardnum(int nid,dev_info_t * par)139 get_boardnum(int nid, dev_info_t *par)
140 {
141 	int board_num;
142 
143 	if (prom_getprop((pnode_t)nid, OBP_BOARDNUM,
144 	    (caddr_t)&board_num) != -1)
145 		return (board_num);
146 
147 	/*
148 	 * Look at current node and up the parent chain
149 	 * till we find a node with an OBP_BOARDNUM.
150 	 */
151 	while (par) {
152 		nid = ddi_get_nodeid(par);
153 
154 		if (prom_getprop((pnode_t)nid, OBP_BOARDNUM,
155 		    (caddr_t)&board_num) != -1)
156 			return (board_num);
157 
158 		par = ddi_get_parent(par);
159 	}
160 	return (-1);
161 }
162 
163 /*
164  * Note that this routine does not take into account the endianness
165  * of the host or the device (or PROM) when retrieving properties.
166  */
167 static int
getlongprop_buf(int id,char * name,char * buf,int maxlen)168 getlongprop_buf(int id, char *name, char *buf, int maxlen)
169 {
170 	int size;
171 
172 	size = prom_getproplen((pnode_t)id, name);
173 	if (size <= 0 || (size > maxlen - 1))
174 		return (-1);
175 
176 	if (-1 == prom_getprop((pnode_t)id, name, buf))
177 		return (-1);
178 
179 	/*
180 	 * Workaround for bugid 1085575 - OBP may return a "name" property
181 	 * without null terminating the string with '\0'.  When this occurs,
182 	 * append a '\0' and return (size + 1).
183 	 */
184 	if (strcmp("name", name) == 0) {
185 		if (buf[size - 1] != '\0') {
186 			buf[size] = '\0';
187 			size += 1;
188 		}
189 	}
190 
191 	return (size);
192 }
193 
194 /*
195  * Routines to set/get UPA slave only device interrupt mapping registers.
196  * set_intr_mapping_reg() is called by the UPA master to register the address
197  * of an interrupt mapping register. The upa id is that of the master. If
198  * this routine is called on behalf of a slave device, the framework
199  * determines the upa id of the slave based on that supplied by the master.
200  *
201  * get_intr_mapping_reg() is called by the UPA nexus driver on behalf
202  * of a child device to get and program the interrupt mapping register of
203  * one of it's child nodes.  It uses the upa id of the child device to
204  * index into a table of mapping registers.  If the routine is called on
205  * behalf of a slave device and the mapping register has not been set,
206  * the framework determines the devinfo node of the corresponding master
207  * nexus which owns the mapping register of the slave and installs that
208  * driver.  The device driver which owns the mapping register must call
209  * set_intr_mapping_reg() in its attach routine to register the slaves
210  * mapping register with the system.
211  */
212 void
set_intr_mapping_reg(int upaid,uint64_t * addr,int slave)213 set_intr_mapping_reg(int upaid, uint64_t *addr, int slave)
214 {
215 	int affin_upaid;
216 
217 	/* For UPA master devices, set the mapping reg addr and we're done */
218 	if (slave == 0) {
219 		intr_map_reg[upaid] = addr;
220 		return;
221 	}
222 
223 	/*
224 	 * If we get here, we're adding an entry for a UPA slave only device.
225 	 * The UPA id of the device which has affinity with that requesting,
226 	 * will be the device with the same UPA id minus the slave number.
227 	 * If the affin_upaid is negative, silently return to the caller.
228 	 */
229 	if ((affin_upaid = upaid - slave) < 0)
230 		return;
231 
232 	/*
233 	 * Load the address of the mapping register in the correct slot
234 	 * for the slave device.
235 	 */
236 	intr_map_reg[affin_upaid] = addr;
237 }
238 
239 uint64_t *
get_intr_mapping_reg(int upaid,int slave)240 get_intr_mapping_reg(int upaid, int slave)
241 {
242 	int affin_upaid;
243 	dev_info_t *affin_dip;
244 	uint64_t *addr = intr_map_reg[upaid];
245 
246 	/* If we're a UPA master, or we have a valid mapping register. */
247 	if (!slave || addr != NULL)
248 		return (addr);
249 
250 	/*
251 	 * We only get here if we're a UPA slave only device whose interrupt
252 	 * mapping register has not been set.
253 	 * We need to try and install the nexus whose physical address
254 	 * space is where the slaves mapping register resides.  They
255 	 * should call set_intr_mapping_reg() in their xxattach() to register
256 	 * the mapping register with the system.
257 	 */
258 
259 	/*
260 	 * We don't know if a single- or multi-interrupt proxy is fielding
261 	 * our UPA slave interrupt, we must check both cases.
262 	 * Start out by assuming the multi-interrupt case.
263 	 * We assume that single- and multi- interrupters are not
264 	 * overlapping in UPA portid space.
265 	 */
266 
267 	affin_upaid = upaid | 3;
268 
269 	/*
270 	 * We start looking for the multi-interrupter affinity node.
271 	 * We know it's ONLY a child of the root node since the root
272 	 * node defines UPA space.
273 	 */
274 	for (affin_dip = ddi_get_child(ddi_root_node()); affin_dip;
275 	    affin_dip = ddi_get_next_sibling(affin_dip))
276 		if (ddi_prop_get_int(DDI_DEV_T_ANY, affin_dip,
277 		    DDI_PROP_DONTPASS, "upa-portid", -1) == affin_upaid)
278 			break;
279 
280 	if (affin_dip) {
281 		if (i_ddi_attach_node_hierarchy(affin_dip) == DDI_SUCCESS) {
282 			/* try again to get the mapping register. */
283 			addr = intr_map_reg[upaid];
284 		}
285 	}
286 
287 	/*
288 	 * If we still don't have a mapping register try single -interrupter
289 	 * case.
290 	 */
291 	if (addr == NULL) {
292 
293 		affin_upaid = upaid | 1;
294 
295 		for (affin_dip = ddi_get_child(ddi_root_node()); affin_dip;
296 		    affin_dip = ddi_get_next_sibling(affin_dip))
297 			if (ddi_prop_get_int(DDI_DEV_T_ANY, affin_dip,
298 			    DDI_PROP_DONTPASS, "upa-portid", -1) == affin_upaid)
299 				break;
300 
301 		if (affin_dip) {
302 			if (i_ddi_attach_node_hierarchy(affin_dip)
303 			    == DDI_SUCCESS) {
304 				/* try again to get the mapping register. */
305 				addr = intr_map_reg[upaid];
306 			}
307 		}
308 	}
309 	return (addr);
310 }
311 
312 
313 static struct upa_dma_pfns {
314 	pfn_t hipfn;
315 	pfn_t lopfn;
316 } upa_dma_pfn_array[MAX_UPA];
317 
318 static int upa_dma_pfn_ndx = 0;
319 
320 /*
321  * Certain UPA busses cannot accept dma transactions from any other source
322  * except for memory due to livelock conditions in their hardware. (e.g. sbus
323  * and PCI). These routines allow devices or busses on the UPA to register
324  * a physical address block within it's own register space where DMA can be
325  * performed.  Currently, the FFB is the only such device which supports
326  * device DMA on the UPA.
327  */
328 void
pf_set_dmacapable(pfn_t hipfn,pfn_t lopfn)329 pf_set_dmacapable(pfn_t hipfn, pfn_t lopfn)
330 {
331 	int i = upa_dma_pfn_ndx;
332 
333 	upa_dma_pfn_ndx++;
334 
335 	upa_dma_pfn_array[i].hipfn = hipfn;
336 	upa_dma_pfn_array[i].lopfn = lopfn;
337 }
338 
339 void
pf_unset_dmacapable(pfn_t pfn)340 pf_unset_dmacapable(pfn_t pfn)
341 {
342 	int i;
343 
344 	for (i = 0; i < upa_dma_pfn_ndx; i++) {
345 		if (pfn <= upa_dma_pfn_array[i].hipfn &&
346 		    pfn >= upa_dma_pfn_array[i].lopfn) {
347 			upa_dma_pfn_array[i].hipfn =
348 			    upa_dma_pfn_array[upa_dma_pfn_ndx - 1].hipfn;
349 			upa_dma_pfn_array[i].lopfn =
350 			    upa_dma_pfn_array[upa_dma_pfn_ndx - 1].lopfn;
351 			upa_dma_pfn_ndx--;
352 			break;
353 		}
354 	}
355 }
356 
357 /*
358  * This routine should only be called using a pfn that is known to reside
359  * in IO space.  The function pf_is_memory() can be used to determine this.
360  */
361 int
pf_is_dmacapable(pfn_t pfn)362 pf_is_dmacapable(pfn_t pfn)
363 {
364 	int i, j;
365 
366 	/* If the caller passed in a memory pfn, return true. */
367 	if (pf_is_memory(pfn))
368 		return (1);
369 
370 	for (i = upa_dma_pfn_ndx, j = 0; j < i; j++)
371 		if (pfn <= upa_dma_pfn_array[j].hipfn &&
372 		    pfn >= upa_dma_pfn_array[j].lopfn)
373 			return (1);
374 
375 	return (0);
376 }
377 
378 
379 /*
380  * Find cpu_id corresponding to the dip of a CPU device node
381  */
382 int
dip_to_cpu_id(dev_info_t * dip,processorid_t * cpu_id)383 dip_to_cpu_id(dev_info_t *dip, processorid_t *cpu_id)
384 {
385 	pnode_t		nodeid;
386 	int		i;
387 
388 	nodeid = (pnode_t)ddi_get_nodeid(dip);
389 	for (i = 0; i < NCPU; i++) {
390 		if (cpunodes[i].nodeid == nodeid) {
391 			*cpu_id = i;
392 			return (DDI_SUCCESS);
393 		}
394 	}
395 	return (DDI_FAILURE);
396 }
397 
398 /* ARGSUSED */
399 void
translate_devid(dev_info_t * dip)400 translate_devid(dev_info_t *dip)
401 {
402 }
403