xref: /titanic_41/usr/src/uts/sun4u/io/upa64s.c (revision fd9cb95cbb2f626355a60efb9d02c5f0a33c10e6)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 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 #include <sys/types.h>
30 #include <sys/conf.h>
31 #include <sys/ddi.h>
32 #include <sys/sunddi.h>
33 #include <sys/autoconf.h>
34 #include <sys/ddi_impldefs.h>
35 #include <sys/ddi_subrdefs.h>
36 #include <sys/cmn_err.h>
37 #include <sys/errno.h>
38 #include <sys/kmem.h>
39 #include <sys/debug.h>
40 #include <sys/sysmacros.h>
41 #include <sys/spl.h>
42 #include <sys/async.h>
43 #include <sys/dvma.h>
44 #include <sys/upa64s.h>
45 #include <sys/machsystm.h>
46 
47 /*
48  * driver global data:
49  */
50 static void *per_upa64s_state;		/* soft state pointer */
51 
52 /*
53  * function prototypes for bus ops routines:
54  */
55 static int
56 upa64s_map(dev_info_t *dip, dev_info_t *rdip, ddi_map_req_t *mp,
57     off_t offset, off_t len, caddr_t *addrp);
58 static int
59 upa64s_ctlops(dev_info_t *dip, dev_info_t *rdip,
60     ddi_ctl_enum_t op, void *arg, void *result);
61 static int
62 upa64_intr_ops(dev_info_t *dip, dev_info_t *rdip, ddi_intr_op_t intr_op,
63     ddi_intr_handle_impl_t *hdlp, void *result);
64 static int
65 upa64s_add_intr_impl(dev_info_t *dip, dev_info_t *rdip,
66     ddi_intr_handle_impl_t *hdlp);
67 static int
68 upa64s_remove_intr_impl(dev_info_t *dip, dev_info_t *rdip,
69     ddi_intr_handle_impl_t *hdlp);
70 
71 /*
72  * function prototypes for dev ops routines:
73  */
74 static int upa64s_attach(dev_info_t *dip, ddi_attach_cmd_t cmd);
75 static int upa64s_detach(dev_info_t *dip, ddi_detach_cmd_t cmd);
76 static int upa64s_power(dev_info_t *dip, int component, int level);
77 
78 /*
79  * bus ops and dev ops structures:
80  */
81 static struct bus_ops upa64s_bus_ops = {
82 	BUSO_REV,
83 	upa64s_map,
84 	0,
85 	0,
86 	0,
87 	i_ddi_map_fault,
88 	ddi_no_dma_map,
89 	ddi_no_dma_allochdl,
90 	ddi_no_dma_freehdl,
91 	ddi_no_dma_bindhdl,
92 	ddi_no_dma_unbindhdl,
93 	ddi_no_dma_flush,
94 	ddi_no_dma_win,
95 	ddi_no_dma_mctl,
96 	upa64s_ctlops,
97 	ddi_bus_prop_op,
98 	0,
99 	0,
100 	0,
101 	0,
102 	0,
103 	0,
104 	0,
105 	0,
106 	0,
107 	0,
108 	0,
109 	0,
110 	upa64_intr_ops
111 };
112 
113 static struct dev_ops upa64s_ops = {
114 	DEVO_REV,
115 	0,
116 	ddi_no_info,
117 	nulldev,
118 	0,
119 	upa64s_attach,
120 	upa64s_detach,
121 	nodev,
122 	(struct cb_ops *)0,
123 	&upa64s_bus_ops,
124 	upa64s_power
125 };
126 
127 /*
128  * module definitions:
129  */
130 #include <sys/modctl.h>
131 extern struct mod_ops mod_driverops;
132 
133 static struct modldrv modldrv = {
134 	&mod_driverops, 		/* type of module */
135 	"UPA64S nexus driver %I%",	/* name of module */
136 	&upa64s_ops,			/* driver ops */
137 };
138 
139 static struct modlinkage modlinkage = {
140 	MODREV_1, (void *)&modldrv, NULL
141 };
142 
143 int
144 _init(void)
145 {
146 	int e;
147 
148 	/*
149 	 * Initialize per instance bus soft state pointer.
150 	 */
151 	if (e = ddi_soft_state_init(&per_upa64s_state,
152 	    sizeof (upa64s_devstate_t), 2))
153 		return (e);
154 	/*
155 	 * Install the module.
156 	 */
157 	if (e = mod_install(&modlinkage))
158 		ddi_soft_state_fini(&per_upa64s_state);
159 	return (e);
160 }
161 
162 int
163 _fini(void)
164 {
165 	int e = mod_remove(&modlinkage);
166 	if (e)
167 		return (e);
168 	ddi_soft_state_fini(&per_upa64s_state);
169 	return (e);
170 }
171 
172 int
173 _info(struct modinfo *modinfop)
174 {
175 	return (mod_info(&modlinkage, modinfop));
176 }
177 
178 
179 /*
180  * forward declarations:
181  */
182 static void upa64s_intrdist(void *arg);
183 static int init_child(dev_info_t *child);
184 static int report_dev(dev_info_t *dip);
185 static int get_properties(upa64s_devstate_t *upa64s_p, dev_info_t *dip);
186 static void save_state(upa64s_devstate_t *upa64s_p);
187 static void restore_state(upa64s_devstate_t *upa64s_p);
188 static int xlate_reg_prop(dev_info_t *dip, upa64s_regspec_t *upa64s_rp,
189     off_t off, off_t len, struct regspec *rp);
190 static int get_reg_set(dev_info_t *dip, dev_info_t *child, int rnumber,
191     off_t off, off_t len, struct regspec *rp);
192 static off_t get_reg_set_size(dev_info_t *child, int rnumber);
193 static uint_t get_nreg_set(dev_info_t *child);
194 
195 
196 /* device driver entry points */
197 
198 /*
199  * attach entry point:
200  */
201 static int
202 upa64s_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
203 {
204 	upa64s_devstate_t *upa64s_p;	/* per upa64s state pointer */
205 	ddi_device_acc_attr_t attr;
206 	int instance;
207 	char *pmc[] = { "NAME=Framebuffer Power", "0=Off", "1=On", NULL };
208 
209 	switch (cmd) {
210 	case DDI_ATTACH:
211 		/*
212 		 * Allocate and get the per instance soft state structure.
213 		 */
214 		instance = ddi_get_instance(dip);
215 		if (alloc_upa64s_soft_state(instance) != DDI_SUCCESS) {
216 			cmn_err(CE_WARN, "%s%d: can't allocate upa64s state",
217 			    ddi_get_name(dip), instance);
218 			return (DDI_FAILURE);
219 		}
220 		upa64s_p = get_upa64s_soft_state(instance);
221 		upa64s_p->dip = dip;
222 
223 		/*
224 		 * Get key properties of the bridge node.
225 		 */
226 		if (get_properties(upa64s_p, dip) != DDI_SUCCESS)
227 			goto fail;
228 
229 		/*
230 		 * Create "pm-components" property for the purpose of
231 		 * doing Power Management.
232 		 */
233 		if (ddi_prop_update_string_array(DDI_DEV_T_NONE, dip,
234 		    "pm-components", pmc, ((sizeof (pmc)/sizeof (char *)) - 1))
235 		    != DDI_PROP_SUCCESS) {
236 			cmn_err(CE_WARN, "%s%d: failed to create pm-components "
237 			    "property.", ddi_get_name(dip), instance);
238 			goto fail;
239 		}
240 
241 		/* Map in the UPA's registers */
242 		attr.devacc_attr_version = DDI_DEVICE_ATTR_V0;
243 		attr.devacc_attr_dataorder = DDI_STRICTORDER_ACC;
244 		attr.devacc_attr_endian_flags = DDI_NEVERSWAP_ACC;
245 		if (ddi_regs_map_setup(dip, 0,
246 		    (caddr_t *)&upa64s_p->config_base, 0, 0, &attr,
247 		    &upa64s_p->config_base_ah) != DDI_SUCCESS) {
248 			cmn_err(CE_WARN, "%s%d: failed to map reg1.",
249 			    ddi_get_name(dip), instance);
250 			goto fail;
251 		}
252 
253 		upa64s_p->upa0_config = (uint64_t *)(upa64s_p->config_base +
254 		    UPA64S_UPA0_CONFIG_OFFSET);
255 		upa64s_p->upa1_config = (uint64_t *)(upa64s_p->config_base +
256 		    UPA64S_UPA1_CONFIG_OFFSET);
257 		upa64s_p->if_config = (uint64_t *)(upa64s_p->config_base +
258 		    UPA64S_IF_CONFIG_OFFSET);
259 		upa64s_p->estar = (uint64_t *)(upa64s_p->config_base +
260 		    UPA64S_ESTAR_OFFSET);
261 
262 		if (ddi_regs_map_setup(dip, 1, (caddr_t *)&upa64s_p->imr[0],
263 		    0, 0, &attr, &upa64s_p->imr_ah[0]) != DDI_SUCCESS) {
264 			cmn_err(CE_WARN, "%s%d: failed to map reg2.",
265 			    ddi_get_name(dip), instance);
266 			goto fail1;
267 		}
268 
269 		if (ddi_regs_map_setup(dip, 2, (caddr_t *)&upa64s_p->imr[1],
270 		    0, 0, &attr, &upa64s_p->imr_ah[1]) != DDI_SUCCESS) {
271 			cmn_err(CE_WARN, "%s%d: failed to map reg3.",
272 			    ddi_get_name(dip), instance);
273 			goto fail2;
274 		}
275 
276 		/*
277 		 * Power level of a component is unknown at attach time.
278 		 * Bring the power level to what is needed for normal operation.
279 		 */
280 		upa64s_p->power_level = UPA64S_PM_UNKNOWN;
281 		if (pm_raise_power(dip, UPA64S_PM_COMP, UPA64S_PM_NORMOP) !=
282 		    DDI_SUCCESS) {
283 			cmn_err(CE_WARN, "%s%d: failed to raise the power.",
284 			    ddi_get_name(dip), instance);
285 			goto fail3;
286 		}
287 
288 		intr_dist_add(upa64s_intrdist, dip);
289 
290 		ddi_report_dev(dip);
291 		return (DDI_SUCCESS);
292 
293 	case DDI_RESUME:
294 
295 		upa64s_p = get_upa64s_soft_state(ddi_get_instance(dip));
296 		DBG(D_ATTACH, dip, "DDI_RESUME\n");
297 		restore_state(upa64s_p);
298 
299 		/*
300 		 * Power level of a component is unknown at resume time.
301 		 * Bring the power level to what it was before suspend.
302 		 */
303 		upa64s_p->power_level = UPA64S_PM_UNKNOWN;
304 		if (pm_raise_power(dip, UPA64S_PM_COMP,
305 		    upa64s_p->saved_power_level) != DDI_SUCCESS)
306 			cmn_err(CE_WARN, "%s%d: failed to change power level "
307 			    "during resume!", ddi_get_name(dip), instance);
308 
309 		return (DDI_SUCCESS);
310 
311 	default:
312 		return (DDI_FAILURE);
313 	}
314 
315 fail3:
316 	ddi_regs_map_free(&upa64s_p->imr_ah[1]);
317 fail2:
318 	ddi_regs_map_free(&upa64s_p->imr_ah[0]);
319 fail1:
320 	ddi_regs_map_free(&upa64s_p->config_base_ah);
321 fail:
322 	free_upa64s_soft_state(instance);
323 	return (DDI_FAILURE);
324 }
325 
326 
327 /*
328  * detach entry point:
329  */
330 static int
331 upa64s_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
332 {
333 	int instance = ddi_get_instance(dip);
334 	upa64s_devstate_t *upa64s_p = get_upa64s_soft_state(instance);
335 
336 	switch (cmd) {
337 	case DDI_DETACH:
338 
339 		DBG(D_DETACH, dip, "DDI_DETACH\n");
340 
341 		/*
342 		 * Power down the device.
343 		 */
344 		if (pm_lower_power(dip, UPA64S_PM_COMP, UPA64S_PM_RESET) !=
345 		    DDI_SUCCESS)
346 			DBG(D_DETACH, dip, "failed to power off!\n");
347 
348 		intr_dist_rem(upa64s_intrdist, dip);
349 
350 		ddi_regs_map_free(&upa64s_p->config_base_ah);
351 		ddi_regs_map_free(&upa64s_p->imr_ah[0]);
352 		ddi_regs_map_free(&upa64s_p->imr_ah[1]);
353 		free_upa64s_soft_state(instance);
354 		return (DDI_SUCCESS);
355 
356 	case DDI_SUSPEND:
357 
358 		DBG(D_DETACH, dip, "DDI_SUSPEND\n");
359 		save_state(upa64s_p);
360 		upa64s_p->saved_power_level = upa64s_p->power_level;
361 		return (DDI_SUCCESS);
362 	}
363 
364 	return (DDI_FAILURE);
365 }
366 
367 /*
368  * power entry point:
369  *
370  * This entry point is called by Power Management framework to
371  * reset upa bus and slow down/speed up the upa interface of
372  * Schizo chip.
373  */
374 static int
375 upa64s_power(dev_info_t *dip, int component, int level)
376 {
377 	int instance = ddi_get_instance(dip);
378 	upa64s_devstate_t *upa64s_p = get_upa64s_soft_state(instance);
379 	volatile uint64_t uint64_data;
380 
381 	DBG2(D_POWER, dip, "component=%d, level=%d\n", component, level);
382 	if (component != UPA64S_PM_COMP ||
383 	    level < UPA64S_PM_RESET || level > UPA64S_PM_NORMOP)
384 		return (DDI_FAILURE);
385 
386 	/*
387 	 * We can't set the hardware to the state that it is
388 	 * already in.  So if the power state is not known, inquire the
389 	 * state of the hardware.  If it is already in that state,
390 	 * record and return, otherwise make the state change.
391 	 */
392 	if (upa64s_p->power_level == UPA64S_PM_UNKNOWN) {
393 		uint64_data = ddi_get64(upa64s_p->config_base_ah,
394 		    upa64s_p->if_config);
395 		if ((level == UPA64S_PM_RESET &&
396 		    uint64_data == UPA64S_NOT_POK_RST_L) ||
397 		    (level == UPA64S_PM_NORMOP &&
398 		    uint64_data == UPA64S_POK_NOT_RST_L)) {
399 			upa64s_p->power_level = level;
400 			return (DDI_SUCCESS);
401 		}
402 	}
403 
404 	if (level == upa64s_p->power_level) {
405 		DBG1(D_POWER, dip, "device is already at power level %d\n",
406 		    level);
407 		return (DDI_SUCCESS);
408 	}
409 
410 
411 	if (level == UPA64S_PM_RESET) {
412 		/*
413 		 * Assert UPA64S_RESET
414 		 */
415 		ddi_put64(upa64s_p->config_base_ah, upa64s_p->if_config,
416 		    UPA64S_POK_RST_L);
417 
418 		/*
419 		 * Deassert UPA64S_POK.  Flush the store buffer.
420 		 */
421 		ddi_put64(upa64s_p->config_base_ah, upa64s_p->if_config,
422 		    UPA64S_NOT_POK_RST_L);
423 		uint64_data = ddi_get64(upa64s_p->config_base_ah,
424 		    upa64s_p->if_config);
425 
426 		/*
427 		 * Internal UPA clock to 1/2 speed
428 		 */
429 		ddi_put64(upa64s_p->config_base_ah, upa64s_p->estar,
430 		    UPA64S_1_2_SPEED);
431 
432 		/*
433 		 * Internal UPA clock to 1/64 speed.  Flush the store buffer.
434 		 */
435 		ddi_put64(upa64s_p->config_base_ah, upa64s_p->estar,
436 		    UPA64S_1_64_SPEED);
437 		uint64_data = ddi_get64(upa64s_p->config_base_ah,
438 		    upa64s_p->estar);
439 	} else {
440 		/*
441 		 * Internal UPA clock to 1/2 speed
442 		 */
443 		ddi_put64(upa64s_p->config_base_ah, upa64s_p->estar,
444 		    UPA64S_1_2_SPEED);
445 
446 		/*
447 		 * Internal UPA clock to full speed.  Flush the store buffer.
448 		 */
449 		ddi_put64(upa64s_p->config_base_ah, upa64s_p->estar,
450 		    UPA64S_FULL_SPEED);
451 		uint64_data = ddi_get64(upa64s_p->config_base_ah,
452 		    upa64s_p->estar);
453 
454 		/*
455 		 * Assert UPA64S_POK.  Flush the store buffer before
456 		 * the wait delay.
457 		 */
458 		ddi_put64(upa64s_p->config_base_ah, upa64s_p->if_config,
459 		    UPA64S_POK_RST_L);
460 		uint64_data = ddi_get64(upa64s_p->config_base_ah,
461 		    upa64s_p->if_config);
462 
463 		/*
464 		 * Delay 20 milliseconds for the signals to settle down.
465 		 */
466 		delay(drv_usectohz(20*1000));
467 
468 		/*
469 		 * Deassert UPA64S_RESET.  Flush the store buffer.
470 		 */
471 		ddi_put64(upa64s_p->config_base_ah, upa64s_p->if_config,
472 		    UPA64S_POK_NOT_RST_L);
473 		uint64_data = ddi_get64(upa64s_p->config_base_ah,
474 		    upa64s_p->if_config);
475 	}
476 	upa64s_p->power_level = level;
477 
478 	return (DDI_SUCCESS);
479 }
480 
481 /* bus driver entry points */
482 
483 /*
484  * bus map entry point:
485  *
486  * 	if map request is for an rnumber
487  *		get the corresponding regspec from device node
488  * 	build a new regspec in our parent's format
489  *	build a new map_req with the new regspec
490  *	call up the tree to complete the mapping
491  */
492 static int
493 upa64s_map(dev_info_t *dip, dev_info_t *rdip, ddi_map_req_t *mp,
494     off_t off, off_t len, caddr_t *addrp)
495 {
496 	struct regspec regspec;
497 	ddi_map_req_t p_map_request;
498 	int rnumber, rval;
499 
500 	DBG4(D_MAP, dip, "upa64s_map() mp=%x.%x addrp=%x.%08x\n",
501 	    HI32(mp), LO32(mp), HI32(addrp), LO32(addrp));
502 
503 	/*
504 	 * User level mappings are not supported yet.
505 	 */
506 	if (mp->map_flags & DDI_MF_USER_MAPPING) {
507 		DBG2(D_MAP, dip, "rdip=%s%d: no user level mappings yet!\n",
508 		    ddi_get_name(rdip), ddi_get_instance(rdip));
509 		return (DDI_ME_UNIMPLEMENTED);
510 	}
511 
512 	/*
513 	 * Now handle the mapping according to its type.
514 	 */
515 	switch (mp->map_type) {
516 	case DDI_MT_REGSPEC:
517 
518 		/*
519 		 * We assume the register specification is in PCI format.
520 		 * We must convert it into a regspec of our parent's
521 		 * and pass the request to our parent.
522 		 */
523 		DBG3(D_MAP, dip, "rdip=%s%d: REGSPEC - handlep=%x\n",
524 		    ddi_get_name(rdip), ddi_get_instance(rdip),
525 		    mp->map_handlep);
526 		rval = xlate_reg_prop(dip, (upa64s_regspec_t *)mp->map_obj.rp,
527 		    off, len, &regspec);
528 		break;
529 
530 	case DDI_MT_RNUMBER:
531 
532 		/*
533 		 * Get the "reg" property from the device node and convert
534 		 * it to our parent's format.
535 		 */
536 		DBG4(D_MAP, dip, "rdip=%s%d: rnumber=%x handlep=%x\n",
537 		    ddi_get_name(rdip), ddi_get_instance(rdip),
538 		    mp->map_obj.rnumber, mp->map_handlep);
539 		rnumber = mp->map_obj.rnumber;
540 		if (rnumber < 0)
541 			return (DDI_ME_RNUMBER_RANGE);
542 		rval = get_reg_set(dip, rdip,  rnumber, off, len, &regspec);
543 		break;
544 
545 	default:
546 		return (DDI_ME_INVAL);
547 
548 	}
549 	if (rval != DDI_SUCCESS) {
550 		DBG(D_MAP, dip, "failed on regspec\n\n");
551 		return (rval);
552 	}
553 
554 	/*
555 	 * Now we have a copy of the upa64s regspec converted to our parent's
556 	 * format.  Build a new map request based on this regspec and pass
557 	 * it to our parent.
558 	 */
559 	p_map_request = *mp;
560 	p_map_request.map_type = DDI_MT_REGSPEC;
561 	p_map_request.map_obj.rp = &regspec;
562 	rval = ddi_map(dip, &p_map_request, 0, 0, addrp);
563 	DBG3(D_MAP, dip, "ddi_map returns: rval=%x addrp=%x.%08x\n\n",
564 	    rval, HI32(*addrp), LO32(*addrp));
565 	return (rval);
566 }
567 
568 /*
569  * Translate the UPA devices interrupt property.  This is the only case I
570  * know of where the interrupts property is meaningless.  As a result, we
571  * just use UPA_BASE_INO as our interrupt value and add to it the upa port id.
572  * UPA portid is returned too.
573  */
574 #define	UPA_BASE_INO	0x2a
575 
576 static int
577 upa64s_xlate_intr(dev_info_t *rdip, int32_t safariport, uint32_t *intr)
578 {
579 	uint32_t ino = UPA_BASE_INO;
580 	int32_t portid;
581 
582 	/* Clear the ffb's interrupts property, it's meaningless */
583 	*intr = 0;
584 
585 	if ((portid = ddi_getprop(DDI_DEV_T_ANY, rdip, DDI_PROP_DONTPASS,
586 	    "upa-portid", -1)) == -1)
587 		return (-1);
588 
589 	ino += portid;
590 
591 	*intr = UPA64S_MAKE_MONDO(safariport, ino);
592 
593 	DBG5(D_A_ISPEC, rdip, "upa64s_xlate_intr: rdip=%s%d: upa portid %d "
594 	    "ino=%x mondo 0x%x\n", ddi_get_name(rdip), ddi_get_instance(rdip),
595 	    portid, ino, *intr);
596 
597 	return (portid);
598 }
599 
600 /*
601  * bus add intrspec entry point:
602  */
603 static int
604 upa64s_add_intr_impl(dev_info_t *dip, dev_info_t *rdip,
605     ddi_intr_handle_impl_t *hdlp)
606 {
607 	int upaport, instance = ddi_get_instance(dip);
608 	upa64s_devstate_t *upa64s_p = get_upa64s_soft_state(instance);
609 #ifdef DEBUG
610 	uint_t (*int_handler)(caddr_t, caddr_t) = hdlp->ih_cb_func;
611 	caddr_t int_handler_arg1 = hdlp->ih_cb_arg1;
612 #endif /* DEBUG */
613 	uint_t cpu_id;
614 	volatile uint64_t imr_data;
615 
616 	upaport = upa64s_xlate_intr(rdip, upa64s_p->safari_id,
617 	    (uint32_t *)&hdlp->ih_vector);
618 
619 	if (hdlp->ih_vector == 0)
620 		return (DDI_FAILURE);
621 
622 	DBG3(D_A_ISPEC, dip,
623 	    "rdip=%s%d - IDDI_INTR_TYPE_NORMAL, mondo=%x\n",
624 	    ddi_driver_name(rdip), ddi_get_instance(rdip), hdlp->ih_vector);
625 
626 	/*
627 	 * Make sure an interrupt handler isn't already installed.
628 	 */
629 	if (upa64s_p->ino_state[upaport] != INO_FREE) {
630 		return (DDI_FAILURE);
631 	}
632 
633 	/*
634 	 * Install the handler in the system table.
635 	 */
636 #ifdef	DEBUG
637 	DBG2(D_A_ISPEC, dip, "i_ddi_add_ivintr: hdlr=%p arg=%p\n",
638 	    int_handler, int_handler_arg1);
639 #endif
640 	if (i_ddi_add_ivintr(hdlp) != DDI_SUCCESS)
641 		return (DDI_FAILURE);
642 
643 	cpu_id = intr_dist_cpuid();
644 
645 	/*
646 	 * Enable the interrupt through its interrupt mapping register.
647 	 */
648 	imr_data = UPA64S_CPUID_TO_IMR(cpu_id);
649 	imr_data = UPA64S_GET_MAP_REG(hdlp->ih_vector, imr_data);
650 
651 	DBG4(D_A_ISPEC, dip, "IMR [upaport=%d mapping reg 0x%p] = %x.%x\n",
652 	    upaport, upa64s_p->imr[upaport], HI32(imr_data), LO32(imr_data));
653 
654 	ddi_put64(upa64s_p->imr_ah[upaport], upa64s_p->imr[upaport], imr_data);
655 	/* Read the data back to flush store buffers. */
656 	imr_data = ddi_get64(upa64s_p->imr_ah[upaport], upa64s_p->imr[upaport]);
657 	upa64s_p->ino_state[upaport] = INO_INUSE;
658 
659 	DBG(D_A_ISPEC, dip, "add_intr success!\n");
660 	return (DDI_SUCCESS);
661 }
662 
663 
664 /*
665  * bus remove intrspec entry point
666  */
667 static int
668 upa64s_remove_intr_impl(dev_info_t *dip, dev_info_t *rdip,
669     ddi_intr_handle_impl_t *hdlp)
670 {
671 	upa64s_devstate_t *upa64s_p =
672 	    get_upa64s_soft_state(ddi_get_instance(dip));
673 	int upaport;
674 #ifndef lint
675 	volatile uint64_t tmp;
676 #endif
677 
678 	/*
679 	 * Make sure the mondo is valid.
680 	 */
681 	upaport = upa64s_xlate_intr(rdip, upa64s_p->safari_id,
682 	    (uint32_t *)&hdlp->ih_vector);
683 
684 	if (hdlp->ih_vector == 0)
685 		return (DDI_FAILURE);
686 
687 	DBG3(D_R_ISPEC, dip,
688 	    "rdip=%s%d - IDDI_INTR_TYPE_NORMAL, mondo=%x\n",
689 	    ddi_driver_name(rdip), ddi_get_instance(rdip), hdlp->ih_vector);
690 
691 	if (upa64s_p->ino_state[upaport] != INO_INUSE) {
692 		return (DDI_FAILURE);
693 	}
694 
695 	/* Call up to our parent to handle the removal */
696 	i_ddi_rem_ivintr(hdlp);
697 
698 	ddi_put64(upa64s_p->imr_ah[upaport], upa64s_p->imr[upaport], 0);
699 #ifndef lint
700 	/* Flush store buffers */
701 	tmp = ddi_get64(upa64s_p->imr_ah[upaport], upa64s_p->imr[upaport]);
702 #endif
703 
704 	upa64s_p->ino_state[upaport] = INO_FREE;
705 	return (DDI_SUCCESS);
706 }
707 
708 
709 /* new intr_ops structure */
710 static int
711 upa64_intr_ops(dev_info_t *dip, dev_info_t *rdip, ddi_intr_op_t intr_op,
712     ddi_intr_handle_impl_t *hdlp, void *result)
713 {
714 	int	ret = DDI_SUCCESS;
715 
716 	switch (intr_op) {
717 	case DDI_INTROP_GETCAP:
718 		*(int *)result = DDI_INTR_FLAG_EDGE;
719 		break;
720 	case DDI_INTROP_ALLOC:
721 		*(int *)result = hdlp->ih_scratch1;
722 		break;
723 	case DDI_INTROP_FREE:
724 		break;
725 	case DDI_INTROP_GETPRI:
726 		/*
727 		 * We only have slave UPA devices so force the PIL to 5.
728 		 * this is done since all slave UPA devices have historically
729 		 * had their PILs set to 5.  Only do it if the PIL is not
730 		 * being preset.
731 		 */
732 		*(int *)result = hdlp->ih_pri ? hdlp->ih_pri : 5;
733 		break;
734 	case DDI_INTROP_SETPRI:
735 		break;
736 	case DDI_INTROP_ADDISR:
737 		ret = upa64s_add_intr_impl(dip, rdip, hdlp);
738 		break;
739 	case DDI_INTROP_REMISR:
740 		ret = upa64s_remove_intr_impl(dip, rdip, hdlp);
741 		break;
742 	case DDI_INTROP_ENABLE:
743 	case DDI_INTROP_DISABLE:
744 		break;
745 	case DDI_INTROP_NINTRS:
746 	case DDI_INTROP_NAVAIL:
747 		*(int *)result = i_ddi_get_nintrs(rdip);
748 		break;
749 	case DDI_INTROP_SETCAP:
750 	case DDI_INTROP_SETMASK:
751 	case DDI_INTROP_CLRMASK:
752 	case DDI_INTROP_GETPENDING:
753 		ret = DDI_ENOTSUP;
754 		break;
755 	case DDI_INTROP_SUPPORTED_TYPES:
756 		/* only support fixed interrupts */
757 		*(int *)result = i_ddi_get_nintrs(rdip) ?
758 		    DDI_INTR_TYPE_FIXED : 0;
759 		break;
760 	default:
761 		ret = i_ddi_intr_ops(dip, rdip, intr_op, hdlp, result);
762 		break;
763 	}
764 
765 	return (ret);
766 }
767 
768 #ifdef DEBUG
769 uint_t upa64s_debug_flags = (uint_t)0;
770 
771 extern void prom_printf(const char *, ...);
772 #endif
773 
774 /*
775  * control ops entry point:
776  *
777  * Requests handled completely:
778  *	DDI_CTLOPS_INITCHILD	see init_child() for details
779  *	DDI_CTLOPS_UNINITCHILD
780  *	DDI_CTLOPS_REPORTDEV	see report_dev() for details
781  *	DDI_CTLOPS_REGSIZE
782  *	DDI_CTLOPS_NREGS
783  *
784  * All others passed to parent.
785  */
786 static int
787 upa64s_ctlops(dev_info_t *dip, dev_info_t *rdip,
788     ddi_ctl_enum_t op, void *arg, void *result)
789 {
790 	DBG5(D_CTLOPS, dip, "dip=%x.%x rdip=%x.%x op=%x",
791 	    HI32(dip), LO32(dip), HI32(rdip), LO32(rdip), op);
792 	DBG4(D_CTLOPS|D_CONT, dip, " arg=%x.%x result=%x.%x\n",
793 	    HI32(arg), LO32(arg), HI32(result), LO32(result));
794 
795 	switch (op) {
796 	case DDI_CTLOPS_INITCHILD:
797 		DBG2(D_CTLOPS, dip, "DDI_CTLOPS_INITCHILD: rdip=%s%d\n",
798 		    ddi_get_name(rdip), ddi_get_instance(rdip));
799 		return (init_child((dev_info_t *)arg));
800 
801 	case DDI_CTLOPS_UNINITCHILD:
802 		DBG2(D_CTLOPS, dip, "DDI_CTLOPS_UNINITCHILD: rdip=%s%d\n",
803 		    ddi_get_name(rdip), ddi_get_instance(rdip));
804 		ddi_set_name_addr((dev_info_t *)arg, NULL);
805 		ddi_remove_minor_node((dev_info_t *)arg, NULL);
806 		impl_rem_dev_props((dev_info_t *)arg);
807 		return (DDI_SUCCESS);
808 
809 	case DDI_CTLOPS_REPORTDEV:
810 		DBG2(D_CTLOPS, dip, "DDI_CTLOPS_REPORTDEV: rdip=%s%d\n",
811 		    ddi_get_name(rdip), ddi_get_instance(rdip));
812 		return (report_dev(rdip));
813 
814 	case DDI_CTLOPS_REGSIZE:
815 		DBG2(D_CTLOPS, dip, "DDI_CTLOPS_REGSIZE: rdip=%s%d\n",
816 		    ddi_get_name(rdip), ddi_get_instance(rdip));
817 		*((off_t *)result) = get_reg_set_size(rdip, *((int *)arg));
818 		return (*((off_t *)result) == -1 ? DDI_FAILURE : DDI_SUCCESS);
819 
820 	case DDI_CTLOPS_NREGS:
821 		DBG2(D_CTLOPS, dip, "DDI_CTLOPS_NREGS: rdip=%s%d\n",
822 		    ddi_get_name(rdip), ddi_get_instance(rdip));
823 		*((uint_t *)result) = get_nreg_set(rdip);
824 		return (DDI_SUCCESS);
825 	}
826 
827 	/*
828 	 * Now pass the request up to our parent.
829 	 */
830 	DBG3(D_CTLOPS, dip, "passing request to parent: rdip=%s%d op=%x\n\n",
831 	    ddi_get_name(rdip), ddi_get_instance(rdip), op);
832 	return (ddi_ctlops(dip, rdip, op, arg, result));
833 }
834 
835 
836 /* support routines */
837 
838 /*
839  * get_properties
840  *
841  * This function is called from the attach routine to get the key
842  * properties of the upa64s node.
843  *
844  * used by: upa64s_attach()
845  *
846  * return value: none
847  */
848 static int
849 get_properties(upa64s_devstate_t *upa64s_p, dev_info_t *dip)
850 {
851 	int safari_id;
852 
853 	/*
854 	 * Get the device's safari id.
855 	 */
856 	safari_id = ddi_getprop(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
857 	    "portid", -1);
858 	if (safari_id == -1) {
859 		int instance = ddi_get_instance(dip);
860 		panic("%s%d: no portid property", ddi_get_name(dip), instance);
861 	}
862 	upa64s_p->safari_id = safari_id;
863 
864 	return (DDI_SUCCESS);
865 }
866 
867 
868 /*
869  * save_state
870  *
871  * This routine saves a copy of the upa64s register state.
872  *
873  * used by: upa64s_detach() on a suspend operation
874  */
875 static void
876 save_state(upa64s_devstate_t *upa64s_p)
877 {
878 	upa64s_p->imr_data[0] = ddi_get64(upa64s_p->imr_ah[0],
879 	    upa64s_p->imr[0]);
880 	upa64s_p->imr_data[1] = ddi_get64(upa64s_p->imr_ah[1],
881 	    upa64s_p->imr[1]);
882 }
883 
884 
885 /*
886  * restore_state
887  *
888  * This routine restores a copy of the upa64s register state.
889  *
890  * used by: upa64s_attach() on a resume operation
891  */
892 static void
893 restore_state(upa64s_devstate_t *upa64s_p)
894 {
895 #ifndef lint
896 	volatile uint64_t tmp;
897 #endif
898 	ddi_put64(upa64s_p->imr_ah[0], upa64s_p->imr[0],
899 	    upa64s_p->imr_data[0]);
900 	ddi_put64(upa64s_p->imr_ah[1], upa64s_p->imr[1],
901 	    upa64s_p->imr_data[1]);
902 #ifndef lint
903 	/* Flush the store buffer */
904 	tmp = ddi_get64(upa64s_p->imr_ah[0], upa64s_p->imr[0]);
905 	tmp = ddi_get64(upa64s_p->imr_ah[1], upa64s_p->imr[1]);
906 #endif
907 }
908 
909 
910 /*
911  * get_reg_set
912  *
913  * This routine will get a upa64s format regspec for a given
914  * device node and register number.
915  *
916  * used by: upa64s_map()
917  *
918  * return value:
919  *
920  *	DDI_SUCCESS		- on success
921  *	DDI_ME_INVAL		- regspec is invalid
922  *	DDI_ME_RNUMBER_RANGE	- rnumber out of range
923  */
924 static int
925 get_reg_set(dev_info_t *dip, dev_info_t *child, int rnumber,
926     off_t off, off_t len, struct regspec *rp)
927 {
928 	upa64s_regspec_t *upa64s_rp;
929 	int i, n, rval;
930 
931 	/*
932 	 * Get child device "reg" property
933 	 */
934 	if (ddi_getlongprop(DDI_DEV_T_ANY, child, DDI_PROP_DONTPASS, "reg",
935 	    (caddr_t)&upa64s_rp, &i) != DDI_SUCCESS)
936 		return (DDI_ME_RNUMBER_RANGE);
937 
938 	n = i / (int)sizeof (upa64s_regspec_t);
939 	if (rnumber >= n) {
940 		kmem_free(upa64s_rp, i);
941 		return (DDI_ME_RNUMBER_RANGE);
942 	}
943 
944 	/*
945 	 * Convert each the upa64s format register specification to
946 	 * out parent format.
947 	 */
948 	rval = xlate_reg_prop(dip, &upa64s_rp[rnumber], off, len, rp);
949 	kmem_free(upa64s_rp, i);
950 	return (rval);
951 }
952 
953 
954 /*
955  * xlate_reg_prop
956  *
957  * This routine converts a upa64s format regspec to a standard
958  * regspec containing the corresponding system address.
959  *
960  * used by: upa64s_map()
961  *
962  * return value:
963  *
964  *	DDI_SUCCESS
965  *	DDI_FAILURE	- off + len is beyond device address range
966  *	DDI_ME_INVAL	- regspec is invalid
967  */
968 static int
969 xlate_reg_prop(dev_info_t *dip, upa64s_regspec_t *child_rp, off_t off,
970     off_t len, struct regspec *rp)
971 {
972 	int n_ranges, ranges_len, i;
973 	uint64_t child_beg, child_end;
974 	upa64s_ranges_t *range_p, *rng_p;
975 
976 	DBG4(D_MAP, dip, "upa64s regspec - ((%x,%x) (%x,%x))\n",
977 	    HI32(child_rp->upa64s_phys), LO32(child_rp->upa64s_phys),
978 	    HI32(child_rp->upa64s_size), LO32(child_rp->upa64s_size));
979 	DBG2(D_MAP, dip, "upa64s xlate_reg_prp - off=%lx len=%lx\n", off, len);
980 #if 0
981 	/*
982 	 * both FFB and AFB have broken "reg" properties, all mapping
983 	 * requests are done through reg-0 with very long offsets.
984 	 * Hence this safety check is always violated.
985 	 */
986 	if (off + len > child_rp->upa64s_size) {
987 		DBG(D_MAP, dip, "upa64s xlate_reg_prp: bad off + len\n");
988 		return (DDI_FAILURE);
989 	}
990 #endif
991 	/*
992 	 * current "struct regspec" only supports 32-bit sizes.
993 	 */
994 	if (child_rp->upa64s_size >= (1ull << 32))
995 		panic("upa64s: reg size must be less than 4 Gb");
996 
997 	if (ddi_getlongprop(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
998 	    "ranges", (caddr_t)&range_p, &ranges_len) != DDI_SUCCESS) {
999 		ranges_len = 0;
1000 		cmn_err(CE_WARN, "%s%d: no ranges property",
1001 		    ddi_get_name(dip), ddi_get_instance(dip));
1002 	}
1003 
1004 	n_ranges = ranges_len / sizeof (upa64s_regspec_t);
1005 	child_beg = child_rp->upa64s_phys;
1006 #if 0
1007 	/*
1008 	 * again, this safety checking can not be performed.
1009 	 * Hack by adding a pratical max child reg bank length.
1010 	 */
1011 	child_end = child_beg + child_rp->upa64s_size;
1012 #else
1013 #define	UPA64S_MAX_CHILD_LEN	0xe000000
1014 	child_end = child_beg + UPA64S_MAX_CHILD_LEN;
1015 #endif
1016 	for (i = 0, rng_p = range_p; i < n_ranges; i++, rng_p++) {
1017 		uint64_t rng_beg = rng_p->upa64s_child;
1018 		uint64_t rng_end = rng_beg + rng_p->upa64s_size;
1019 		if ((rng_beg <= child_beg) && (rng_end >= child_end)) {
1020 			uint64_t addr = child_beg - rng_beg + off;
1021 			addr += rng_p->upa64s_parent;
1022 			rp->regspec_bustype = HI32(addr);
1023 			rp->regspec_addr = LO32(addr);
1024 			rp->regspec_size = len ? len : child_rp->upa64s_size;
1025 			break;
1026 		}
1027 	}
1028 	if (ranges_len)
1029 		kmem_free(range_p, ranges_len);
1030 	DBG4(D_MAP, dip, "regspec (%x,%x,%x) i=%x\n",
1031 	    rp->regspec_bustype, rp->regspec_addr, rp->regspec_size, i);
1032 	return (i < n_ranges? DDI_SUCCESS : DDI_ME_INVAL);
1033 }
1034 
1035 
1036 /*
1037  * report_dev
1038  *
1039  * This function is called from our control ops routine on a
1040  * DDI_CTLOPS_REPORTDEV request.
1041  */
1042 static int
1043 report_dev(dev_info_t *dip)
1044 {
1045 	if (dip == (dev_info_t *)0)
1046 		return (DDI_FAILURE);
1047 	cmn_err(CE_CONT, "?UPA64S-device: %s@%s, %s #%d\n",
1048 	    ddi_node_name(dip), ddi_get_name_addr(dip),
1049 	    ddi_major_to_name(ddi_name_to_major(ddi_get_name(dip))),
1050 	    ddi_get_instance(dip));
1051 	return (DDI_SUCCESS);
1052 }
1053 
1054 
1055 /*
1056  * init_child
1057  *
1058  * This function is called from our control ops routine on a
1059  * DDI_CTLOPS_INITCHILD request.  It builds and sets the device's
1060  * parent private data area.
1061  *
1062  * used by: upa64s_ctlops()
1063  *
1064  * return value: none
1065  */
1066 static int
1067 init_child(dev_info_t *child)
1068 {
1069 	upa64s_regspec_t *child_rp;
1070 	int i;
1071 	char addr[256];
1072 	int32_t portid;
1073 
1074 	if ((portid = ddi_getprop(DDI_DEV_T_ANY, child, DDI_PROP_DONTPASS,
1075 	    "upa-portid", -1)) == -1)
1076 		return (DDI_FAILURE);
1077 
1078 	/*
1079 	 * Set the address portion of the node name based on
1080 	 * the function and device number.
1081 	 */
1082 	if (ddi_getlongprop(DDI_DEV_T_ANY, child, DDI_PROP_DONTPASS, "reg",
1083 	    (caddr_t)&child_rp, &i) != DDI_SUCCESS) {
1084 		return (DDI_FAILURE);
1085 	}
1086 
1087 	(void) sprintf(addr, "%x,%x", portid, LO32(child_rp->upa64s_phys));
1088 	ddi_set_name_addr(child, addr);
1089 
1090 	ddi_set_parent_data(child, NULL);
1091 	kmem_free(child_rp, i);
1092 	return (DDI_SUCCESS);
1093 }
1094 
1095 
1096 /*
1097  * get_reg_set_size
1098  *
1099  * Given a dev info pointer to a child and a register number, this
1100  * routine returns the size element of that reg set property.
1101  *
1102  * used by: upa64s_ctlops() - DDI_CTLOPS_REGSIZE
1103  *
1104  * return value: size of reg set on success, -1 on error
1105  */
1106 static off_t
1107 get_reg_set_size(dev_info_t *child, int rnumber)
1108 {
1109 	upa64s_regspec_t *upa64s_rp;
1110 	uint_t size;
1111 	int i;
1112 
1113 	if (rnumber < 0)
1114 		return (-1);
1115 
1116 	/*
1117 	 * Get the reg property for the device.
1118 	 */
1119 	if (ddi_getlongprop(DDI_DEV_T_ANY, child, DDI_PROP_DONTPASS, "reg",
1120 	    (caddr_t)&upa64s_rp, &i) != DDI_SUCCESS)
1121 		return (-1);
1122 
1123 	if (rnumber >= (i / (int)sizeof (upa64s_regspec_t))) {
1124 		kmem_free(upa64s_rp, i);
1125 		return (-1);
1126 	}
1127 
1128 	/*  >4G reg size not supported */
1129 	size = (uint32_t)upa64s_rp[rnumber].upa64s_size;
1130 	kmem_free(upa64s_rp, i);
1131 	return (size);
1132 }
1133 
1134 
1135 /*
1136  * get_nreg_set
1137  *
1138  * Given a dev info pointer to a child, this routine returns the
1139  * number of sets in its "reg" property.
1140  *
1141  * used by: upa64s_ctlops() - DDI_CTLOPS_NREGS
1142  *
1143  * return value: # of reg sets on success, zero on error
1144  */
1145 static uint_t
1146 get_nreg_set(dev_info_t *child)
1147 {
1148 	upa64s_regspec_t *upa64s_rp;
1149 	int i, n;
1150 
1151 	/*
1152 	 * Get the reg property for the device.
1153 	 */
1154 	if (ddi_getlongprop(DDI_DEV_T_ANY, child, DDI_PROP_DONTPASS, "reg",
1155 	    (caddr_t)&upa64s_rp, &i) != DDI_SUCCESS)
1156 		return (0);
1157 
1158 	n =  i / (int)sizeof (upa64s_regspec_t);
1159 	kmem_free(upa64s_rp, i);
1160 	return (n);
1161 }
1162 
1163 
1164 /*
1165  * upa64s_intrdist
1166  *
1167  * The following routine is the callback function for this nexus driver
1168  * to support interrupt distribution on sun4u systems. When this
1169  * function is called by the interrupt distribution framework, it will
1170  * reprogram all the active the mondo registers.
1171  */
1172 static void
1173 upa64s_intrdist(void *arg)
1174 {
1175 	dev_info_t *dip = (dev_info_t *)arg;
1176 	int instance = ddi_get_instance(dip);
1177 	upa64s_devstate_t *upa64s_p = get_upa64s_soft_state(instance);
1178 	uint_t upaport;
1179 
1180 	for (upaport = 0; upaport < UPA64S_PORTS; upaport++) {
1181 		volatile uint64_t *imr;
1182 		volatile uint64_t imr_dat;
1183 		uint_t mondo;
1184 		uint32_t cpuid;
1185 
1186 		if (upa64s_p->ino_state[upaport] != INO_INUSE)
1187 			continue;
1188 
1189 		imr = upa64s_p->imr[upaport];
1190 		mondo = UPA64S_IMR_TO_MONDO(*imr);
1191 		cpuid = intr_dist_cpuid();
1192 		imr_dat = UPA64S_CPUID_TO_IMR(cpuid);
1193 		imr_dat = UPA64S_GET_MAP_REG(mondo, imr_dat);
1194 
1195 		/* Check and re-program cpu target if necessary */
1196 		DBG2(D_INTRDIST, dip, "mondo=%x cpuid=%x\n", mondo, cpuid);
1197 		if (UPA64S_IMR_TO_CPUID(*imr) == cpuid) {
1198 			DBG(D_INTRDIST, dip, "same cpuid\n");
1199 			continue;
1200 		}
1201 		ddi_put64(upa64s_p->imr_ah[upaport], (uint64_t *)imr, imr_dat);
1202 		imr_dat = ddi_get64(upa64s_p->imr_ah[upaport], (uint64_t *)imr);
1203 	}
1204 }
1205 
1206 
1207 #ifdef DEBUG
1208 static void
1209 upa64s_debug(uint_t flag, dev_info_t *dip, char *fmt,
1210     uintptr_t a1, uintptr_t a2, uintptr_t a3, uintptr_t a4, uintptr_t a5)
1211 {
1212 	char *s = NULL;
1213 	uint_t cont = 0;
1214 	if (flag & D_CONT) {
1215 		flag &= ~D_CONT;
1216 		cont = 1;
1217 	}
1218 	if (!(upa64s_debug_flags & flag))
1219 		return;
1220 
1221 	switch (flag) {
1222 	case D_ATTACH:		s = "attach";		break;
1223 	case D_DETACH:		s = "detach";		break;
1224 	case D_POWER:		s = "power";		break;
1225 	case D_MAP:		s = "map";		break;
1226 	case D_CTLOPS:		s = "ctlops";		break;
1227 	case D_G_ISPEC:		s = "get_intrspec";	break;
1228 	case D_A_ISPEC:		s = "add_intrspec";	break;
1229 	case D_R_ISPEC:		s = "remove_intrspec";	break;
1230 	case D_INIT_CLD:	s = "init_child";	break;
1231 	case D_INTRDIST:	s = "intrdist";		break;
1232 	}
1233 
1234 	if (s && cont == 0) {
1235 		prom_printf("%s(%d): %s: ", ddi_get_name(dip),
1236 		    ddi_get_instance(dip), s);
1237 	}
1238 	prom_printf(fmt, a1, a2, a3, a4, a5);
1239 }
1240 #endif
1241