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