xref: /titanic_41/usr/src/uts/sun4u/io/pci/pcisch.c (revision 25cf1a301a396c38e8adf52c15f537b80d2483f7)
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 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  * Schizo specifics implementation:
31  *	interrupt mapping register
32  *	PBM configuration
33  *	ECC and PBM error handling
34  *	Iommu mapping handling
35  *	Streaming Cache flushing
36  */
37 
38 #include <sys/types.h>
39 #include <sys/kmem.h>
40 #include <sys/sysmacros.h>
41 #include <sys/async.h>
42 #include <sys/ivintr.h>
43 #include <sys/systm.h>
44 #include <sys/intr.h>
45 #include <sys/machsystm.h>	/* lddphys() */
46 #include <sys/machsystm.h>	/* lddphys, intr_dist_add */
47 #include <sys/iommutsb.h>
48 #include <sys/promif.h>		/* prom_printf */
49 #include <sys/map.h>
50 #include <sys/ddi.h>
51 #include <sys/sunddi.h>
52 #include <sys/sunndi.h>
53 #include <sys/spl.h>
54 #include <sys/fm/util.h>
55 #include <sys/ddi_impldefs.h>
56 #include <sys/fm/protocol.h>
57 #include <sys/fm/io/sun4upci.h>
58 #include <sys/fm/io/ddi.h>
59 #include <sys/fm/io/pci.h>
60 #include <sys/pci/pci_obj.h>
61 #include <sys/pci/pcisch.h>
62 #include <sys/pci/pcisch_asm.h>
63 #include <sys/x_call.h>		/* XCALL_PIL */
64 
65 /*LINTLIBRARY*/
66 
67 extern uint8_t ldstub(uint8_t *);
68 
69 #define	IOMMU_CTX_BITMAP_SIZE	(1 << (12 - 3))
70 static void iommu_ctx_free(iommu_t *);
71 static int iommu_tlb_scrub(iommu_t *, int);
72 static uint32_t pci_identity_init(pci_t *);
73 
74 static void pci_cb_clear_error(cb_t *, cb_errstate_t *);
75 static void pci_clear_error(pci_t *, pbm_errstate_t *);
76 static uint32_t pci_identity_init(pci_t *pci_p);
77 static int pci_intr_setup(pci_t *pci_p);
78 static void iommu_ereport_post(dev_info_t *, uint64_t, pbm_errstate_t *);
79 static void cb_ereport_post(dev_info_t *, uint64_t, cb_errstate_t *);
80 static void pcix_ereport_post(dev_info_t *, uint64_t, pbm_errstate_t *);
81 static void pci_format_ecc_addr(dev_info_t *dip, uint64_t *afar,
82 		ecc_region_t region);
83 static void pci_pbm_errstate_get(pci_t *pci_p, pbm_errstate_t *pbm_err_p);
84 static void tm_vmem_free(ddi_dma_impl_t *mp, iommu_t *iommu_p,
85 		dvma_addr_t dvma_pg, int npages);
86 
87 static int pcix_ma_behind_bridge(pbm_errstate_t *pbm_err_p);
88 
89 static pci_ksinfo_t	*pci_name_kstat;
90 static pci_ksinfo_t	*saf_name_kstat;
91 
92 extern void pcix_set_cmd_reg(dev_info_t *child, uint16_t value);
93 
94 /* called by pci_attach() DDI_ATTACH to initialize pci objects */
95 int
96 pci_obj_setup(pci_t *pci_p)
97 {
98 	pci_common_t *cmn_p;
99 	uint32_t chip_id = pci_identity_init(pci_p);
100 	uint32_t cmn_id = PCI_CMN_ID(ID_CHIP_TYPE(chip_id), pci_p->pci_id);
101 	int ret;
102 
103 	/* Perform allocations first to avoid delicate unwinding. */
104 	if (pci_alloc_tsb(pci_p) != DDI_SUCCESS)
105 		return (DDI_FAILURE);
106 
107 	mutex_enter(&pci_global_mutex);
108 	cmn_p = get_pci_common_soft_state(cmn_id);
109 	if (cmn_p == NULL) {
110 		if (alloc_pci_common_soft_state(cmn_id) != DDI_SUCCESS) {
111 			mutex_exit(&pci_global_mutex);
112 			pci_free_tsb(pci_p);
113 			return (DDI_FAILURE);
114 		}
115 		cmn_p = get_pci_common_soft_state(cmn_id);
116 		cmn_p->pci_common_id = cmn_id;
117 		cmn_p->pci_common_tsb_cookie = IOMMU_TSB_COOKIE_NONE;
118 	}
119 
120 	ASSERT((pci_p->pci_side == 0) || (pci_p->pci_side == 1));
121 	if (cmn_p->pci_p[pci_p->pci_side]) {
122 		/* second side attach */
123 		pci_p->pci_side = PCI_OTHER_SIDE(pci_p->pci_side);
124 		ASSERT(cmn_p->pci_p[pci_p->pci_side] == NULL);
125 	}
126 
127 	cmn_p->pci_p[pci_p->pci_side] = pci_p;
128 	pci_p->pci_common_p = cmn_p;
129 
130 	if (cmn_p->pci_common_refcnt == 0)
131 		cmn_p->pci_chip_id = chip_id;
132 
133 	ib_create(pci_p);
134 
135 	/*
136 	 * The initialization of cb internal interrupts depends on ib
137 	 */
138 	if (cmn_p->pci_common_refcnt == 0) {
139 		cb_create(pci_p);
140 		cmn_p->pci_common_cb_p = pci_p->pci_cb_p;
141 	} else
142 		pci_p->pci_cb_p = cmn_p->pci_common_cb_p;
143 
144 	iommu_create(pci_p);
145 
146 	if (cmn_p->pci_common_refcnt == 0) {
147 		ecc_create(pci_p);
148 		cmn_p->pci_common_ecc_p = pci_p->pci_ecc_p;
149 	} else
150 		pci_p->pci_ecc_p = cmn_p->pci_common_ecc_p;
151 
152 	pbm_create(pci_p);
153 	sc_create(pci_p);
154 
155 	pci_fm_create(pci_p);
156 
157 	if ((ret = pci_intr_setup(pci_p)) != DDI_SUCCESS)
158 		goto done;
159 
160 	pci_kstat_create(pci_p);
161 
162 	cmn_p->pci_common_attachcnt++;
163 	cmn_p->pci_common_refcnt++;
164 done:
165 	mutex_exit(&pci_global_mutex);
166 	if (ret != DDI_SUCCESS)
167 		cmn_err(CE_WARN, "pci_obj_setup failed %x", ret);
168 	return (ret);
169 }
170 
171 /* called by pci_detach() DDI_DETACH to destroy pci objects */
172 void
173 pci_obj_destroy(pci_t *pci_p)
174 {
175 	pci_common_t *cmn_p;
176 	mutex_enter(&pci_global_mutex);
177 
178 	cmn_p = pci_p->pci_common_p;
179 	cmn_p->pci_common_refcnt--;
180 	cmn_p->pci_common_attachcnt--;
181 
182 	pci_kstat_destroy(pci_p);
183 
184 	/* schizo non-shared objects */
185 	pci_fm_destroy(pci_p);
186 
187 	sc_destroy(pci_p);
188 	pbm_destroy(pci_p);
189 	iommu_destroy(pci_p);
190 	ib_destroy(pci_p);
191 
192 	if (cmn_p->pci_common_refcnt != 0) {
193 		pci_intr_teardown(pci_p);
194 		cmn_p->pci_p[pci_p->pci_side] = NULL;
195 		mutex_exit(&pci_global_mutex);
196 		return;
197 	}
198 
199 	/* schizo shared objects - uses cmn_p, must be destroyed before cmn */
200 	ecc_destroy(pci_p);
201 	cb_destroy(pci_p);
202 
203 	free_pci_common_soft_state(cmn_p->pci_common_id);
204 	pci_intr_teardown(pci_p);
205 	mutex_exit(&pci_global_mutex);
206 }
207 
208 /* called by pci_attach() DDI_RESUME to (re)initialize pci objects */
209 void
210 pci_obj_resume(pci_t *pci_p)
211 {
212 	pci_common_t *cmn_p = pci_p->pci_common_p;
213 
214 	mutex_enter(&pci_global_mutex);
215 
216 	ib_configure(pci_p->pci_ib_p);
217 	iommu_configure(pci_p->pci_iommu_p);
218 
219 	if (cmn_p->pci_common_attachcnt == 0)
220 		ecc_configure(pci_p);
221 
222 	ib_resume(pci_p->pci_ib_p);
223 
224 	pbm_configure(pci_p->pci_pbm_p);
225 	sc_configure(pci_p->pci_sc_p);
226 
227 	if (cmn_p->pci_common_attachcnt == 0)
228 		cb_resume(pci_p->pci_cb_p);
229 
230 	pbm_resume(pci_p->pci_pbm_p);
231 
232 	cmn_p->pci_common_attachcnt++;
233 	mutex_exit(&pci_global_mutex);
234 }
235 
236 /* called by pci_detach() DDI_SUSPEND to suspend pci objects */
237 void
238 pci_obj_suspend(pci_t *pci_p)
239 {
240 	mutex_enter(&pci_global_mutex);
241 
242 	pbm_suspend(pci_p->pci_pbm_p);
243 	ib_suspend(pci_p->pci_ib_p);
244 
245 	if (!--pci_p->pci_common_p->pci_common_attachcnt)
246 		cb_suspend(pci_p->pci_cb_p);
247 
248 	mutex_exit(&pci_global_mutex);
249 }
250 
251 /*
252  * add an additional 0x35 or 0x36 ino interrupt on platforms don't have them
253  * This routine has multiple places that assumes interrupt takes one cell
254  * each and cell size is same as integer size.
255  */
256 static int
257 pci_intr_setup(pci_t *pci_p)
258 {
259 	dev_info_t *dip = pci_p->pci_dip;
260 	pbm_t *pbm_p = pci_p->pci_pbm_p;
261 	cb_t *cb_p = pci_p->pci_cb_p;
262 	uint32_t *intr_buf, *new_intr_buf;
263 	int intr_len, intr_cnt, ret;
264 
265 	if (ddi_getlongprop(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
266 		"interrupts", (caddr_t)&intr_buf, &intr_len) != DDI_SUCCESS)
267 		cmn_err(CE_PANIC, "%s%d: no interrupts property\n",
268 			ddi_driver_name(dip), ddi_get_instance(dip));
269 
270 	intr_cnt = BYTES_TO_1275_CELLS(intr_len);
271 	if (intr_cnt < CBNINTR_CDMA)	/* CBNINTR_CDMA is 0 based */
272 		cmn_err(CE_PANIC, "%s%d: <%d interrupts", ddi_driver_name(dip),
273 			ddi_get_instance(dip), CBNINTR_CDMA);
274 
275 	if (intr_cnt == CBNINTR_CDMA)
276 		intr_cnt++;
277 
278 	new_intr_buf = kmem_alloc(CELLS_1275_TO_BYTES(intr_cnt), KM_SLEEP);
279 	bcopy(intr_buf, new_intr_buf, intr_len);
280 	kmem_free(intr_buf, intr_len);
281 
282 	new_intr_buf[CBNINTR_CDMA] = PBM_CDMA_INO_BASE + pci_p->pci_side;
283 	pci_p->pci_inos = new_intr_buf;
284 	pci_p->pci_inos_len = CELLS_1275_TO_BYTES(intr_cnt);
285 
286 	if (ndi_prop_update_int_array(DDI_DEV_T_NONE, dip, "interrupts",
287 		(int *)new_intr_buf, intr_cnt))
288 		cmn_err(CE_PANIC, "%s%d: cannot update interrupts property\n",
289 			ddi_driver_name(dip), ddi_get_instance(dip));
290 
291 	if (pci_p->pci_common_p->pci_common_refcnt == 0) {
292 		cb_p->cb_no_of_inos = intr_cnt;
293 		if (ret = cb_register_intr(pci_p))
294 			goto teardown;
295 		if (ret = ecc_register_intr(pci_p))
296 			goto teardown;
297 
298 		intr_dist_add(cb_intr_dist, cb_p);
299 		cb_enable_intr(pci_p);
300 		ecc_enable_intr(pci_p);
301 	}
302 
303 	if (CHIP_TYPE(pci_p) != PCI_CHIP_SCHIZO)
304 		pbm_p->pbm_sync_ino = pci_p->pci_inos[CBNINTR_PBM];
305 	if (ret = pbm_register_intr(pbm_p)) {
306 		if (pci_p->pci_common_p->pci_common_refcnt == 0)
307 			intr_dist_rem(cb_intr_dist, cb_p);
308 		goto teardown;
309 	}
310 	intr_dist_add(pbm_intr_dist, pbm_p);
311 	ib_intr_enable(pci_p, pci_p->pci_inos[CBNINTR_PBM]);
312 	ib_intr_enable(pci_p, pci_p->pci_inos[CBNINTR_CDMA]);
313 
314 	intr_dist_add_weighted(ib_intr_dist_all, pci_p->pci_ib_p);
315 	return (DDI_SUCCESS);
316 teardown:
317 	pci_intr_teardown(pci_p);
318 	return (ret);
319 }
320 
321 uint64_t
322 pci_sc_configure(pci_t *pci_p)
323 {
324 	int instance;
325 	dev_info_t *dip = pci_p->pci_dip;
326 
327 	instance = ddi_get_instance(dip);
328 	if ((pci_xmits_sc_max_prf & (1 << instance)) &&
329 	    (CHIP_TYPE(pci_p) == PCI_CHIP_XMITS))
330 		return (XMITS_SC_MAX_PRF);
331 	else
332 		return (0);
333 }
334 
335 static void
336 pci_schizo_cdma_sync(pbm_t *pbm_p)
337 {
338 	pci_t *pci_p = pbm_p->pbm_pci_p;
339 	hrtime_t start_time;
340 	volatile uint64_t *clr_p = ib_clear_intr_reg_addr(pci_p->pci_ib_p,
341 		pci_p->pci_inos[CBNINTR_CDMA]);
342 	uint32_t fail_cnt = pci_cdma_intr_count;
343 
344 	mutex_enter(&pbm_p->pbm_sync_mutex);
345 #ifdef PBM_CDMA_DEBUG
346 	pbm_p->pbm_cdma_req_cnt++;
347 #endif /* PBM_CDMA_DEBUG */
348 	pbm_p->pbm_cdma_flag = PBM_CDMA_PEND;
349 	IB_INO_INTR_TRIG(clr_p);
350 wait:
351 	start_time = gethrtime();
352 	while (pbm_p->pbm_cdma_flag != PBM_CDMA_DONE) {
353 		if (gethrtime() - start_time <= pci_cdma_intr_timeout)
354 			continue;
355 		if (--fail_cnt > 0)
356 			goto wait;
357 		if (pbm_p->pbm_cdma_flag == PBM_CDMA_DONE)
358 			break;
359 		cmn_err(CE_PANIC, "%s (%s): consistent dma sync timeout",
360 		    pbm_p->pbm_nameinst_str, pbm_p->pbm_nameaddr_str);
361 	}
362 #ifdef PBM_CDMA_DEBUG
363 	if (pbm_p->pbm_cdma_flag != PBM_CDMA_DONE)
364 		pbm_p->pbm_cdma_to_cnt++;
365 	else {
366 		start_time = gethrtime() - start_time;
367 		pbm_p->pbm_cdma_success_cnt++;
368 		pbm_p->pbm_cdma_latency_sum += start_time;
369 		if (start_time > pbm_p->pbm_cdma_latency_max)
370 			pbm_p->pbm_cdma_latency_max = start_time;
371 	}
372 #endif /* PBM_CDMA_DEBUG */
373 	mutex_exit(&pbm_p->pbm_sync_mutex);
374 }
375 
376 #if !defined(lint)
377 #include <sys/cpuvar.h>
378 #endif
379 
380 #define	SYNC_HW_BUSY(pa, mask)	(lddphysio(pa) & (mask))
381 
382 /*
383  * Consistent DMA Sync/Flush
384  *
385  * XMITS and Tomatillo use multi-threaded sync/flush register.
386  * Called from interrupt wrapper: the associated ino is used to index
387  *	the distinctive register bit.
388  * Called from pci_dma_sync(): the bit belongs to PBM is shared
389  *	for all calls from pci_dma_sync(). Xmits requires serialization
390  *	while Tomatillo does not.
391  */
392 void
393 pci_pbm_dma_sync(pbm_t *pbm_p, ib_ino_t ino)
394 {
395 	pci_t *pci_p = pbm_p->pbm_pci_p;
396 	hrtime_t start_time;
397 	uint64_t ino_mask, sync_reg_pa;
398 	volatile uint64_t flag_val;
399 	uint32_t locked, chip_type = CHIP_TYPE(pci_p);
400 	int	i;
401 
402 	if (chip_type == PCI_CHIP_SCHIZO) {
403 		pci_schizo_cdma_sync(pbm_p);
404 		return;
405 	}
406 
407 	sync_reg_pa = pbm_p->pbm_sync_reg_pa;
408 
409 	locked = 0;
410 	if (((chip_type == PCI_CHIP_XMITS) && (ino == pbm_p->pbm_sync_ino)) ||
411 	    pci_sync_lock) {
412 		locked = 1;
413 		mutex_enter(&pbm_p->pbm_sync_mutex);
414 	}
415 	ino_mask = 1ull << ino;
416 	stdphysio(sync_reg_pa, ino_mask);
417 
418 	for (i = 0; i < 5; i++) {
419 		if ((flag_val = SYNC_HW_BUSY(sync_reg_pa, ino_mask)) == 0)
420 			goto done;
421 	}
422 
423 	start_time = gethrtime();
424 	for (; (flag_val = SYNC_HW_BUSY(sync_reg_pa, ino_mask)) != 0; i++) {
425 		if (gethrtime() - start_time > pci_sync_buf_timeout)
426 			break;
427 	}
428 
429 	if (flag_val && SYNC_HW_BUSY(sync_reg_pa, ino_mask) && !panicstr)
430 		cmn_err(CE_PANIC, "%s: pbm dma sync %lx,%lx timeout!",
431 			pbm_p->pbm_nameaddr_str, sync_reg_pa, flag_val);
432 done:
433 	/* optional: stdphysio(sync_reg_pa - 8, ino_mask); */
434 	if (locked)
435 		mutex_exit(&pbm_p->pbm_sync_mutex);
436 
437 	if (tomatillo_store_store_wrka) {
438 #if !defined(lint)
439 		kpreempt_disable();
440 #endif
441 		tomatillo_store_store_order();
442 #if !defined(lint)
443 		kpreempt_enable();
444 #endif
445 	}
446 
447 }
448 
449 /*ARGSUSED*/
450 void
451 pci_fix_ranges(pci_ranges_t *rng_p, int rng_entries)
452 {
453 }
454 
455 /*
456  * map_pci_registers
457  *
458  * This function is called from the attach routine to map the registers
459  * accessed by this driver.
460  *
461  * used by: pci_attach()
462  *
463  * return value: DDI_FAILURE on failure
464  */
465 int
466 map_pci_registers(pci_t *pci_p, dev_info_t *dip)
467 {
468 	ddi_device_acc_attr_t attr;
469 	int len;
470 
471 	attr.devacc_attr_version = DDI_DEVICE_ATTR_V0;
472 	attr.devacc_attr_dataorder = DDI_STRICTORDER_ACC;
473 
474 	attr.devacc_attr_endian_flags = DDI_NEVERSWAP_ACC;
475 	/*
476 	 * Register set 0 is PCI CSR Base
477 	 */
478 	if (ddi_regs_map_setup(dip, 0, &pci_p->pci_address[0], 0, 0,
479 	    &attr, &pci_p->pci_ac[0]) != DDI_SUCCESS) {
480 		len = 0;
481 		goto fail;
482 	}
483 	/*
484 	 * Register set 1 is Schizo CSR Base
485 	 */
486 	if (ddi_regs_map_setup(dip, 1, &pci_p->pci_address[1], 0, 0,
487 	    &attr, &pci_p->pci_ac[1]) != DDI_SUCCESS) {
488 		len = 1;
489 		goto fail;
490 	}
491 
492 	/*
493 	 * The third register set contains the bridge's configuration
494 	 * header.  This header is at the very beginning of the bridge's
495 	 * configuration space.  This space has litte-endian byte order.
496 	 */
497 	attr.devacc_attr_endian_flags = DDI_STRUCTURE_LE_ACC;
498 	if (ddi_regs_map_setup(dip, 2, &pci_p->pci_address[2], 0,
499 	    PCI_CONF_HDR_SIZE, &attr, &pci_p->pci_ac[2]) != DDI_SUCCESS) {
500 		len = 2;
501 		goto fail;
502 	}
503 
504 	if (ddi_getproplen(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
505 	    "reg", &len) || (len / sizeof (pci_nexus_regspec_t) < 4))
506 		goto done;
507 
508 	/*
509 	 * The optional fourth register bank points to the
510 	 * interrupt concentrator registers.
511 	 */
512 	attr.devacc_attr_endian_flags = DDI_NEVERSWAP_ACC;
513 	if (ddi_regs_map_setup(dip, 3, &pci_p->pci_address[3], 0,
514 	    0, &attr, &pci_p->pci_ac[3]) != DDI_SUCCESS) {
515 		len = 3;
516 		goto fail;
517 	}
518 
519 done:
520 	DEBUG4(DBG_ATTACH, dip, "address (%p,%p,%p,%p)\n",
521 	    pci_p->pci_address[0], pci_p->pci_address[1],
522 	    pci_p->pci_address[2], pci_p->pci_address[3]);
523 
524 	return (DDI_SUCCESS);
525 
526 
527 fail:
528 	cmn_err(CE_WARN, "%s%d: unable to map reg entry %d\n",
529 		ddi_driver_name(dip), ddi_get_instance(dip), len);
530 	for (; len--; ddi_regs_map_free(&pci_p->pci_ac[len]));
531 	return (DDI_FAILURE);
532 }
533 
534 /*
535  * unmap_pci_registers:
536  *
537  * This routine unmap the registers mapped by map_pci_registers.
538  *
539  * used by: pci_detach()
540  *
541  * return value: none
542  */
543 void
544 unmap_pci_registers(pci_t *pci_p)
545 {
546 	int i;
547 
548 	for (i = 0; i < 4; i++) {
549 		if (pci_p->pci_ac[i])
550 			ddi_regs_map_free(&pci_p->pci_ac[i]);
551 	}
552 }
553 
554 uint64_t
555 ib_get_map_reg(ib_mondo_t mondo, uint32_t cpu_id)
556 {
557 	uint32_t agent_id;
558 	uint32_t node_id;
559 
560 	/* ensure that cpu_id is only 10 bits. */
561 	ASSERT((cpu_id & ~0x3ff) == 0);
562 
563 	agent_id = cpu_id & 0x1f;
564 	node_id = (cpu_id >> 5) & 0x1f;
565 
566 	return ((mondo) | (agent_id << COMMON_INTR_MAP_REG_TID_SHIFT) |
567 	    (node_id << SCHIZO_INTR_MAP_REG_NID_SHIFT) |
568 	    COMMON_INTR_MAP_REG_VALID);
569 }
570 
571 uint32_t
572 ib_map_reg_get_cpu(volatile uint64_t reg)
573 {
574 	return (((reg & COMMON_INTR_MAP_REG_TID) >>
575 		COMMON_INTR_MAP_REG_TID_SHIFT) |
576 			((reg & SCHIZO_INTR_MAP_REG_NID) >>
577 			(SCHIZO_INTR_MAP_REG_NID_SHIFT-5)));
578 }
579 
580 uint64_t *
581 ib_intr_map_reg_addr(ib_t *ib_p, ib_ino_t ino)
582 {
583 	/*
584 	 * Schizo maps all interrupts in one contiguous area.
585 	 * (PCI_CSRBase + 0x00.1000 + INO * 8).
586 	 */
587 	return ((uint64_t *)(ib_p->ib_intr_map_regs) + (ino & 0x3f));
588 }
589 
590 uint64_t *
591 ib_clear_intr_reg_addr(ib_t *ib_p, ib_ino_t ino)	/* XXX - needs work */
592 {
593 	/*
594 	 * Schizo maps clear intr. registers in contiguous area.
595 	 * (PCI_CSRBase + 0x00.1400 + INO * 8).
596 	 */
597 	return ((uint64_t *)(ib_p->ib_slot_clear_intr_regs) + (ino & 0x3f));
598 }
599 
600 /*
601  * schizo does not have mapping register per slot, so no sharing
602  * is done.
603  */
604 /*ARGSUSED*/
605 void
606 ib_ino_map_reg_share(ib_t *ib_p, ib_ino_t ino, ib_ino_info_t *ino_p)
607 {
608 }
609 
610 /*
611  * return true if there are interrupts using this mapping register
612  */
613 /*ARGSUSED*/
614 int
615 ib_ino_map_reg_unshare(ib_t *ib_p, ib_ino_t ino, ib_ino_info_t *ino_p)
616 {
617 	return (ino_p->ino_ih_size);
618 }
619 
620 void
621 pci_pbm_intr_dist(pbm_t *pbm_p)
622 {
623 	pci_t *pci_p = pbm_p->pbm_pci_p;
624 	ib_t *ib_p = pci_p->pci_ib_p;
625 	ib_ino_t ino = IB_MONDO_TO_INO(pci_p->pci_inos[CBNINTR_CDMA]);
626 
627 	mutex_enter(&pbm_p->pbm_sync_mutex);
628 	ib_intr_dist_nintr(ib_p, ino, ib_intr_map_reg_addr(ib_p, ino));
629 	mutex_exit(&pbm_p->pbm_sync_mutex);
630 }
631 
632 uint32_t
633 pci_xlate_intr(dev_info_t *dip, dev_info_t *rdip, ib_t *ib_p, uint32_t intr)
634 {
635 	return (IB_INO_TO_MONDO(ib_p, intr));
636 }
637 
638 
639 /*
640  * Return the cpuid to to be used for an ino.  We have no special cpu
641  * assignment constraints for this nexus, so just call intr_dist_cpuid().
642  */
643 /* ARGSUSED */
644 uint32_t
645 pci_intr_dist_cpuid(ib_t *ib_p, ib_ino_info_t *ino_p)
646 {
647 	return (intr_dist_cpuid());
648 }
649 
650 void
651 pci_cb_teardown(pci_t *pci_p)
652 {
653 	cb_t 	*cb_p = pci_p->pci_cb_p;
654 	uint32_t mondo;
655 
656 	if (!pci_buserr_interrupt)
657 		return;
658 
659 	mondo = ((pci_p->pci_cb_p->cb_ign  << PCI_INO_BITS) |
660 	    pci_p->pci_inos[CBNINTR_BUS_ERROR]);
661 	mondo = CB_MONDO_TO_XMONDO(pci_p->pci_cb_p, mondo);
662 
663 	cb_disable_nintr(cb_p, CBNINTR_BUS_ERROR, IB_INTR_WAIT);
664 	rem_ivintr(mondo, NULL);
665 }
666 
667 int
668 cb_register_intr(pci_t *pci_p)
669 {
670 	uint32_t mondo;
671 
672 	if (!pci_buserr_interrupt)
673 		return (DDI_SUCCESS);
674 
675 	mondo = ((pci_p->pci_cb_p->cb_ign << PCI_INO_BITS) |
676 	    pci_p->pci_inos[CBNINTR_BUS_ERROR]);
677 	mondo = CB_MONDO_TO_XMONDO(pci_p->pci_cb_p, mondo);
678 
679 	VERIFY(add_ivintr(mondo, pci_pil[CBNINTR_BUS_ERROR],
680 	    cb_buserr_intr, (caddr_t)pci_p->pci_cb_p, NULL) == 0);
681 
682 	return (PCI_ATTACH_RETCODE(PCI_CB_OBJ, PCI_OBJ_INTR_ADD, DDI_SUCCESS));
683 }
684 
685 void
686 cb_enable_intr(pci_t *pci_p)
687 {
688 	if (pci_buserr_interrupt)
689 		cb_enable_nintr(pci_p, CBNINTR_BUS_ERROR);
690 }
691 
692 uint64_t
693 cb_ino_to_map_pa(cb_t *cb_p, ib_ino_t ino)
694 {
695 	return (cb_p->cb_map_pa + (ino << 3));
696 }
697 
698 uint64_t
699 cb_ino_to_clr_pa(cb_t *cb_p, ib_ino_t ino)
700 {
701 	return (cb_p->cb_clr_pa + (ino << 3));
702 }
703 
704 /*
705  * Useful on psycho only.
706  */
707 int
708 cb_remove_xintr(pci_t *pci_p, dev_info_t *dip, dev_info_t *rdip, ib_ino_t ino,
709 ib_mondo_t mondo)
710 {
711 	return (DDI_FAILURE);
712 }
713 
714 void
715 pbm_configure(pbm_t *pbm_p)
716 {
717 	pci_t *pci_p = pbm_p->pbm_pci_p;
718 	dev_info_t *dip = pbm_p->pbm_pci_p->pci_dip;
719 	int instance = ddi_get_instance(dip);
720 	uint64_t l;
721 	uint64_t mask = 1ll << instance;
722 	ushort_t s = 0;
723 
724 	l = *pbm_p->pbm_ctrl_reg;	/* save control register state */
725 	DEBUG1(DBG_ATTACH, dip, "pbm_configure: ctrl reg=%llx\n", l);
726 
727 	/*
728 	 * See if any SERR# signals are asserted.  We'll clear them later.
729 	 */
730 	if (l & COMMON_PCI_CTRL_SERR)
731 		cmn_err(CE_WARN, "%s%d: SERR asserted on pci bus\n",
732 		    ddi_driver_name(dip), instance);
733 
734 	/*
735 	 * Determine if PCI bus is running at 33 or 66 mhz.
736 	 */
737 	if (l & COMMON_PCI_CTRL_SPEED)
738 		pbm_p->pbm_speed = PBM_SPEED_66MHZ;
739 	else
740 		pbm_p->pbm_speed = PBM_SPEED_33MHZ;
741 	DEBUG1(DBG_ATTACH, dip, "pbm_configure: %d mhz\n",
742 	    pbm_p->pbm_speed  == PBM_SPEED_66MHZ ? 66 : 33);
743 
744 	if (pci_set_dto_value & mask) {
745 		l &= ~(3ull << SCHIZO_PCI_CTRL_PTO_SHIFT);
746 		l |= pci_dto_value << SCHIZO_PCI_CTRL_PTO_SHIFT;
747 	} else if (PCI_CHIP_ID(pci_p) >= TOMATILLO_VER_21) {
748 		l |= (3ull << SCHIZO_PCI_CTRL_PTO_SHIFT);
749 	}
750 
751 	/*
752 	 * Enable error interrupts.
753 	 */
754 	if (pci_error_intr_enable & mask)
755 		l |= SCHIZO_PCI_CTRL_ERR_INT_EN;
756 	else
757 		l &= ~SCHIZO_PCI_CTRL_ERR_INT_EN;
758 
759 	/*
760 	 * Enable pci streaming byte errors and error interrupts.
761 	 */
762 	if (pci_sbh_error_intr_enable & mask)
763 		l |= SCHIZO_PCI_CTRL_SBH_INT_EN;
764 	else
765 		l &= ~SCHIZO_PCI_CTRL_SBH_INT_EN;
766 
767 	/*
768 	 * Enable pci discard timeout error interrupt.
769 	 */
770 	if (pci_mmu_error_intr_enable & mask)
771 		l |= SCHIZO_PCI_CTRL_MMU_INT_EN;
772 	else
773 		l &= ~SCHIZO_PCI_CTRL_MMU_INT_EN;
774 
775 	/*
776 	 * Enable PCI-X error interrupts.
777 	 */
778 	if (CHIP_TYPE(pci_p) == PCI_CHIP_XMITS) {
779 
780 		if (xmits_error_intr_enable & mask)
781 			l |= XMITS_PCI_CTRL_X_ERRINT_EN;
782 		else
783 			l &= ~XMITS_PCI_CTRL_X_ERRINT_EN;
784 		/*
785 		 * Panic if older XMITS hardware is found.
786 		 */
787 		if (*pbm_p->pbm_ctrl_reg & XMITS_PCI_CTRL_X_MODE)
788 			if (PCI_CHIP_ID(pci_p) <= XMITS_VER_10)
789 				cmn_err(CE_PANIC, "%s (%s): PCIX mode "
790 				"unsupported on XMITS version %d\n",
791 				    pbm_p->pbm_nameinst_str,
792 				    pbm_p->pbm_nameaddr_str, CHIP_VER(pci_p));
793 
794 		if (xmits_perr_recov_int_enable) {
795 			if (PCI_CHIP_ID(pci_p) >= XMITS_VER_30) {
796 				uint64_t pcix_err;
797 				/*
798 				 * Enable interrupt on PERR
799 				 */
800 				pcix_err = *pbm_p->pbm_pcix_err_stat_reg;
801 				pcix_err |= XMITS_PCIX_STAT_PERR_RECOV_INT_EN;
802 				pcix_err &= ~XMITS_PCIX_STAT_SERR_ON_PERR;
803 				*pbm_p->pbm_pcix_err_stat_reg = pcix_err;
804 			}
805 		}
806 
807 		/*
808 		 * Enable parity error detection on internal memories
809 		 */
810 		*pbm_p->pbm_pci_ped_ctrl = 0x3fff;
811 	}
812 
813 	/*
814 	 * Enable/disable bus parking.
815 	 */
816 	if ((pci_bus_parking_enable & mask) &&
817 	    !ddi_prop_exists(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
818 	    "no-bus-parking"))
819 		l |= SCHIZO_PCI_CTRL_ARB_PARK;
820 	else
821 		l &= ~SCHIZO_PCI_CTRL_ARB_PARK;
822 
823 	/*
824 	 * Enable arbitration.
825 	 */
826 	l |= PCI_CHIP_ID(pci_p) == XMITS_VER_10 ? XMITS10_PCI_CTRL_ARB_EN_MASK :
827 		SCHIZO_PCI_CTRL_ARB_EN_MASK;
828 
829 	/*
830 	 * Make sure SERR is clear
831 	 */
832 	l |= COMMON_PCI_CTRL_SERR;
833 
834 
835 	/*
836 	 * Enable DTO interrupt, if desired.
837 	 */
838 
839 	if (PCI_CHIP_ID(pci_p) <= TOMATILLO_VER_20 || (pci_dto_intr_enable &
840 	    mask))
841 		l |=	 (TOMATILLO_PCI_CTRL_DTO_INT_EN);
842 	else
843 		l &=	 ~(TOMATILLO_PCI_CTRL_DTO_INT_EN);
844 
845 	l |= TOMATILLO_PCI_CTRL_PEN_RD_MLTPL |
846 		TOMATILLO_PCI_CTRL_PEN_RD_ONE |
847 		TOMATILLO_PCI_CTRL_PEN_RD_LINE;
848 
849 	/*
850 	 * Now finally write the control register with the appropriate value.
851 	 */
852 	DEBUG1(DBG_ATTACH, dip, "pbm_configure: ctrl reg=%llx\n", l);
853 	*pbm_p->pbm_ctrl_reg = l;
854 
855 	/*
856 	 * Enable IO Prefetch on Tomatillo
857 	 */
858 	if (CHIP_TYPE(pci_p) == PCI_CHIP_TOMATILLO) {
859 		volatile uint64_t *ioc_csr_p = pbm_p->pbm_ctrl_reg +
860 			((TOMATILLO_IOC_CSR_OFF -
861 			SCHIZO_PCI_CTRL_REG_OFFSET) >> 3);
862 		*ioc_csr_p = TOMATILLO_WRT_PEN |
863 			(1 << TOMATILLO_POFFSET_SHIFT) |
864 			TOMATILLO_C_PEN_RD_MLTPL |
865 			TOMATILLO_C_PEN_RD_ONE |
866 			TOMATILLO_C_PEN_RD_LINE;
867 	}
868 
869 	/*
870 	 * Allow DMA write parity errors to generate an interrupt.
871 	 * This is implemented on Schizo 2.5 and greater and XMITS 3.0
872 	 * and greater.  Setting this on earlier versions of XMITS 3.0
873 	 * has no affect.
874 	 */
875 	if (((CHIP_TYPE(pci_p) == PCI_CHIP_SCHIZO) &&
876 	    PCI_CHIP_ID(pci_p) >= SCHIZO_VER_25) ||
877 	    (CHIP_TYPE(pci_p) == PCI_CHIP_XMITS)) {
878 		volatile uint64_t *pbm_icd = pbm_p->pbm_ctrl_reg +
879 		    ((SCHIZO_PERF_PCI_ICD_OFFSET -
880 		    SCHIZO_PCI_CTRL_REG_OFFSET) >> 3);
881 
882 		*pbm_icd |= SCHIZO_PERF_PCI_ICD_DMAW_PARITY_INT_ENABLE;
883 	}
884 
885 	/*
886 	 * Clear any PBM errors.
887 	 */
888 	l = (SCHIZO_PCI_AFSR_E_MASK << SCHIZO_PCI_AFSR_PE_SHIFT) |
889 		(SCHIZO_PCI_AFSR_E_MASK << SCHIZO_PCI_AFSR_SE_SHIFT);
890 	*pbm_p->pbm_async_flt_status_reg = l;
891 
892 	/*
893 	 * Allow the diag register to be set based upon variable that
894 	 * can be configured via /etc/system.
895 	 */
896 	l = *pbm_p->pbm_diag_reg;
897 	DEBUG1(DBG_ATTACH, dip, "pbm_configure: PCI diag reg=%llx\n", l);
898 
899 	/*
900 	 * Enable/disable retry limit.
901 	 */
902 	if (pci_retry_disable & mask)
903 		l |= COMMON_PCI_DIAG_DIS_RETRY;
904 	else
905 		l &= ~COMMON_PCI_DIAG_DIS_RETRY;
906 
907 	/*
908 	 * Enable/disable DMA write/interrupt synchronization.
909 	 */
910 	if (pci_intsync_disable & mask)
911 		l |= COMMON_PCI_DIAG_DIS_INTSYNC;
912 	else
913 		l &= ~COMMON_PCI_DIAG_DIS_INTSYNC;
914 
915 	/*
916 	 * Enable/disable retry arbitration priority.
917 	 */
918 	if (pci_enable_retry_arb & mask)
919 		l &= ~SCHIZO_PCI_DIAG_DIS_RTRY_ARB;
920 	else
921 		l |= SCHIZO_PCI_DIAG_DIS_RTRY_ARB;
922 
923 	DEBUG1(DBG_ATTACH, dip, "pbm_configure: PCI diag reg=%llx\n", l);
924 	*pbm_p->pbm_diag_reg = l;
925 
926 	/*
927 	 * Enable SERR# and parity reporting via command register.
928 	 */
929 	s = pci_perr_enable & mask ? PCI_COMM_PARITY_DETECT : 0;
930 	s |= pci_serr_enable & mask ? PCI_COMM_SERR_ENABLE : 0;
931 
932 	DEBUG1(DBG_ATTACH, dip, "pbm_configure: conf command reg=%x\n", s);
933 	pbm_p->pbm_config_header->ch_command_reg = s;
934 
935 	/*
936 	 * Clear error bits in configuration status register.
937 	 */
938 	s = PCI_STAT_PERROR | PCI_STAT_S_PERROR |
939 		PCI_STAT_R_MAST_AB | PCI_STAT_R_TARG_AB |
940 		PCI_STAT_S_TARG_AB | PCI_STAT_S_PERROR;
941 	DEBUG1(DBG_ATTACH, dip, "pbm_configure: conf status reg=%x\n", s);
942 	pbm_p->pbm_config_header->ch_status_reg = s;
943 
944 	/*
945 	 * The current versions of the obp are suppose to set the latency
946 	 * timer register but do not.  Bug 1234181 is open against this
947 	 * problem.  Until this bug is fixed we check to see if the obp
948 	 * has attempted to set the latency timer register by checking
949 	 * for the existence of a "latency-timer" property.
950 	 */
951 	if (pci_set_latency_timer_register) {
952 		DEBUG1(DBG_ATTACH, dip,
953 		    "pbm_configure: set schizo latency timer to %x\n",
954 			pci_latency_timer);
955 		pbm_p->pbm_config_header->ch_latency_timer_reg =
956 			pci_latency_timer;
957 	}
958 
959 	(void) ndi_prop_update_int(DDI_DEV_T_ANY, dip, "latency-timer",
960 		(int)pbm_p->pbm_config_header->ch_latency_timer_reg);
961 
962 	/*
963 	 * Adjust xmits_upper_retry_counter if set in /etc/system
964 	 *
965 	 * NOTE: current implementation resets UPPR_RTRY counter for
966 	 * _all_ XMITS' PBMs and does not support tuning per PBM.
967 	 */
968 	if (CHIP_TYPE(pci_p) == PCI_CHIP_XMITS) {
969 		uint_t xurc = xmits_upper_retry_counter &
970 		    XMITS_UPPER_RETRY_MASK;
971 
972 		if (xurc) {
973 			*pbm_p->pbm_upper_retry_counter_reg = (uint64_t)xurc;
974 			DEBUG1(DBG_ATTACH, dip, "pbm_configure: Setting XMITS"
975 			    " uppr_rtry counter = 0x%lx\n",
976 			    *pbm_p->pbm_upper_retry_counter_reg);
977 		}
978 	}
979 }
980 
981 uint_t
982 pbm_disable_pci_errors(pbm_t *pbm_p)
983 {
984 	pci_t *pci_p = pbm_p->pbm_pci_p;
985 	ib_t *ib_p = pci_p->pci_ib_p;
986 
987 	/*
988 	 * Disable error and streaming byte hole interrupts via the
989 	 * PBM control register.
990 	 */
991 	*pbm_p->pbm_ctrl_reg &=
992 		~(SCHIZO_PCI_CTRL_ERR_INT_EN | SCHIZO_PCI_CTRL_SBH_INT_EN |
993 		SCHIZO_PCI_CTRL_MMU_INT_EN);
994 
995 	/*
996 	 * Disable error interrupts via the interrupt mapping register.
997 	 */
998 	ib_intr_disable(ib_p, pci_p->pci_inos[CBNINTR_PBM], IB_INTR_NOWAIT);
999 	return (BF_NONE);
1000 }
1001 
1002 /*
1003  * Layout of the dvma context bucket bitmap entry:
1004  *
1005  *	63 - 56		55 - 0
1006  *	8-bit lock	56-bit, each represent one context
1007  *	DCB_LOCK_BITS	DCB_BMAP_BITS
1008  */
1009 #define	DCB_LOCK_BITS	8
1010 #define	DCB_BMAP_BITS	(64 - DCB_LOCK_BITS)
1011 
1012 dvma_context_t
1013 pci_iommu_get_dvma_context(iommu_t *iommu_p, dvma_addr_t dvma_pg_index)
1014 {
1015 	dvma_context_t ctx;
1016 	int i = (dvma_pg_index >> 6) & 0x1f;	/* 5 bit index within bucket */
1017 	uint64_t ctx_mask, test = 1ull << i;
1018 	uint32_t bucket_no = dvma_pg_index & 0x3f;
1019 	uint64_t *bucket_ptr = iommu_p->iommu_ctx_bitmap + bucket_no;
1020 
1021 	uint32_t spl = ddi_enter_critical();	/* block interrupts */
1022 	if (ldstub((uint8_t *)bucket_ptr)) {	/* try lock */
1023 		ddi_exit_critical(spl);		/* unblock interrupt */
1024 		pci_iommu_ctx_lock_failure++;
1025 		return (0);
1026 	}
1027 
1028 	/* clear lock bits */
1029 	ctx_mask = (*bucket_ptr << DCB_LOCK_BITS) >> DCB_LOCK_BITS;
1030 	ASSERT(*bucket_ptr >> DCB_BMAP_BITS == 0xff);
1031 	ASSERT(ctx_mask >> DCB_BMAP_BITS == 0);
1032 
1033 	if (ctx_mask & test)			/* quick check i bit */
1034 		for (i = 0, test = 1ull; test & ctx_mask; test <<= 1, i++);
1035 	if (i < DCB_BMAP_BITS)
1036 		ctx_mask |= test;
1037 	*bucket_ptr = ctx_mask;			/* unlock */
1038 	ddi_exit_critical(spl);			/* unblock interrupts */
1039 
1040 	ctx = i < DCB_BMAP_BITS ? (bucket_no << 6) | i : 0;
1041 	DEBUG3(DBG_DMA_MAP, iommu_p->iommu_pci_p->pci_dip,
1042 		"get_dvma_context: ctx_mask=0x%x.%x ctx=0x%x\n",
1043 		(uint32_t)(ctx_mask >> 32), (uint32_t)ctx_mask, ctx);
1044 	return (ctx);
1045 }
1046 
1047 void
1048 pci_iommu_free_dvma_context(iommu_t *iommu_p, dvma_context_t ctx)
1049 {
1050 	uint64_t ctx_mask;
1051 	uint32_t spl, bucket_no = ctx >> 6;
1052 	int bit_no = ctx & 0x3f;
1053 	uint64_t *bucket_ptr = iommu_p->iommu_ctx_bitmap + bucket_no;
1054 
1055 	DEBUG1(DBG_DMA_MAP, iommu_p->iommu_pci_p->pci_dip,
1056 		"free_dvma_context: ctx=0x%x\n", ctx);
1057 
1058 	spl = ddi_enter_critical();			/* block interrupts */
1059 	while (ldstub((uint8_t *)bucket_ptr));		/* spin lock */
1060 	ctx_mask = (*bucket_ptr << DCB_LOCK_BITS) >> DCB_LOCK_BITS;
1061 							/* clear lock bits */
1062 	ASSERT(ctx_mask & (1ull << bit_no));
1063 	*bucket_ptr = ctx_mask ^ (1ull << bit_no);	/* clear & unlock */
1064 	ddi_exit_critical(spl);				/* unblock interrupt */
1065 }
1066 
1067 int
1068 pci_sc_ctx_inv(dev_info_t *dip, sc_t *sc_p, ddi_dma_impl_t *mp)
1069 {
1070 	dvma_context_t ctx = MP2CTX(mp);
1071 	volatile uint64_t *reg_addr = sc_p->sc_ctx_match_reg + ctx;
1072 	uint64_t matchreg;
1073 
1074 	if (!*reg_addr) {
1075 		DEBUG1(DBG_SC, dip, "ctx=%x no match\n", ctx);
1076 		return (DDI_SUCCESS);
1077 	}
1078 
1079 	*sc_p->sc_ctx_invl_reg = ctx;	/* 1st flush write */
1080 	matchreg = *reg_addr;		/* re-fetch after 1st flush */
1081 	if (!matchreg)
1082 		return (DDI_SUCCESS);
1083 
1084 	matchreg = (matchreg << SC_ENT_SHIFT) >> SC_ENT_SHIFT;	/* low 16-bit */
1085 	do {
1086 		if (matchreg & 1)
1087 			*sc_p->sc_ctx_invl_reg = ctx;
1088 		matchreg >>= 1;
1089 	} while (matchreg);
1090 
1091 	if (pci_ctx_no_compat || !*reg_addr)	/* compat: active ctx flush */
1092 		return (DDI_SUCCESS);
1093 
1094 	pci_ctx_unsuccess_count++;
1095 	if (pci_ctx_flush_warn)
1096 		cmn_err(pci_ctx_flush_warn, "%s%d: ctx flush unsuccessful\n",
1097 			NAMEINST(dip));
1098 	return (DDI_FAILURE);
1099 }
1100 
1101 void
1102 pci_cb_setup(pci_t *pci_p)
1103 {
1104 	dev_info_t *dip = pci_p->pci_dip;
1105 	cb_t *cb_p = pci_p->pci_cb_p;
1106 	uint64_t pa;
1107 	uint32_t chip_id = PCI_CHIP_ID(pci_p);
1108 	DEBUG1(DBG_ATTACH, dip, "cb_create: chip id %d\n", chip_id);
1109 
1110 	if (CHIP_TYPE(pci_p) == PCI_CHIP_TOMATILLO) {
1111 		if ((!tm_mtlb_gc_manual) &&
1112 		    (PCI_CHIP_ID(pci_p) <= TOMATILLO_VER_24))
1113 			tm_mtlb_gc = 1;
1114 
1115 		if (PCI_CHIP_ID(pci_p) <= TOMATILLO_VER_23) {
1116 			extern int ignore_invalid_vecintr;
1117 			ignore_invalid_vecintr = 1;
1118 			tomatillo_store_store_wrka = 1;
1119 			tomatillo_disallow_bypass = 1;
1120 			if (pci_spurintr_msgs == PCI_SPURINTR_MSG_DEFAULT)
1121 				pci_spurintr_msgs = 0;
1122 		}
1123 	}
1124 
1125 	if (chip_id == TOMATILLO_VER_20 || chip_id == TOMATILLO_VER_21)
1126 		cmn_err(CE_WARN, "Unsupported Tomatillo rev (%x)", chip_id);
1127 
1128 	if (chip_id < SCHIZO_VER_23)
1129 		pci_ctx_no_active_flush = 1;
1130 
1131 	cb_p->cb_node_id = PCI_ID_TO_NODEID(pci_p->pci_id);
1132 	cb_p->cb_ign	 = PCI_ID_TO_IGN(pci_p->pci_id);
1133 
1134 	/*
1135 	 * schizo control status reg bank is on the 2nd "reg" property entry
1136 	 * interrupt mapping/clear/state regs are on the 1st "reg" entry.
1137 	 *
1138 	 * ALL internal interrupts except pbm interrupts are shared by both
1139 	 * sides, 1st-side-attached is used as *the* owner.
1140 	 */
1141 	pa = (uint64_t)hat_getpfnum(kas.a_hat, pci_p->pci_address[1]);
1142 	cb_p->cb_base_pa = pa << MMU_PAGESHIFT;
1143 
1144 	pa = pci_p->pci_address[3] ?
1145 		(uint64_t)hat_getpfnum(kas.a_hat, pci_p->pci_address[3]) : 0;
1146 	cb_p->cb_icbase_pa = (pa == PFN_INVALID) ? 0 : pa << MMU_PAGESHIFT;
1147 
1148 	pa = (uint64_t)hat_getpfnum(kas.a_hat, pci_p->pci_address[0])
1149 		<< MMU_PAGESHIFT;
1150 	cb_p->cb_map_pa = pa + SCHIZO_IB_INTR_MAP_REG_OFFSET;
1151 	cb_p->cb_clr_pa = pa + SCHIZO_IB_CLEAR_INTR_REG_OFFSET;
1152 	cb_p->cb_obsta_pa = pa + COMMON_IB_OBIO_INTR_STATE_DIAG_REG;
1153 }
1154 
1155 void
1156 pci_ecc_setup(ecc_t *ecc_p)
1157 {
1158 	ecc_p->ecc_ue.ecc_errpndg_mask = SCHIZO_ECC_UE_AFSR_ERRPNDG;
1159 	ecc_p->ecc_ue.ecc_offset_mask = SCHIZO_ECC_UE_AFSR_QW_OFFSET;
1160 	ecc_p->ecc_ue.ecc_offset_shift = SCHIZO_ECC_UE_AFSR_QW_OFFSET_SHIFT;
1161 	ecc_p->ecc_ue.ecc_size_log2 = 4;
1162 
1163 	ecc_p->ecc_ce.ecc_errpndg_mask = SCHIZO_ECC_CE_AFSR_ERRPNDG;
1164 	ecc_p->ecc_ce.ecc_offset_mask = SCHIZO_ECC_CE_AFSR_QW_OFFSET;
1165 	ecc_p->ecc_ce.ecc_offset_shift = SCHIZO_ECC_CE_AFSR_QW_OFFSET_SHIFT;
1166 	ecc_p->ecc_ce.ecc_size_log2 = 4;
1167 }
1168 
1169 ushort_t
1170 pci_ecc_get_synd(uint64_t afsr)
1171 {
1172 	return ((ushort_t)((afsr & SCHIZO_ECC_CE_AFSR_SYND) >>
1173 	    SCHIZO_ECC_CE_AFSR_SYND_SHIFT));
1174 }
1175 
1176 /*
1177  * overwrite dvma end address (only on virtual-dma systems)
1178  * initialize tsb size
1179  * reset context bits
1180  * return: IOMMU CSR bank base address (VA)
1181  */
1182 
1183 uintptr_t
1184 pci_iommu_setup(iommu_t *iommu_p)
1185 {
1186 	pci_dvma_range_prop_t *dvma_prop;
1187 	int dvma_prop_len;
1188 
1189 	uintptr_t a;
1190 	pci_t *pci_p = iommu_p->iommu_pci_p;
1191 	dev_info_t *dip = pci_p->pci_dip;
1192 	uint_t tsb_size = iommu_tsb_cookie_to_size(pci_p->pci_tsb_cookie);
1193 	uint_t tsb_size_prop;
1194 
1195 	/*
1196 	 * Initializations for Tomatillo's micro TLB bug. errata #82
1197 	 */
1198 	if (tm_mtlb_gc) {
1199 		iommu_p->iommu_mtlb_nreq = 0;
1200 		iommu_p->iommu_mtlb_npgs = 0;
1201 		iommu_p->iommu_mtlb_maxpgs = tm_mtlb_maxpgs;
1202 		iommu_p->iommu_mtlb_req_p = (dvma_unbind_req_t *)
1203 		    kmem_zalloc(sizeof (dvma_unbind_req_t) *
1204 		    (tm_mtlb_maxpgs + 1), KM_SLEEP);
1205 		mutex_init(&iommu_p->iommu_mtlb_lock, NULL, MUTEX_DRIVER, NULL);
1206 	}
1207 
1208 	if (ddi_getlongprop(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
1209 		"virtual-dma", (caddr_t)&dvma_prop, &dvma_prop_len) !=
1210 		DDI_PROP_SUCCESS)
1211 		goto tsb_done;
1212 
1213 	if (dvma_prop_len != sizeof (pci_dvma_range_prop_t)) {
1214 		cmn_err(CE_WARN, "%s%d: invalid virtual-dma property",
1215 			ddi_driver_name(dip), ddi_get_instance(dip));
1216 		goto tsb_end;
1217 	}
1218 	iommu_p->iommu_dvma_end = dvma_prop->dvma_base +
1219 		(dvma_prop->dvma_len - 1);
1220 	tsb_size_prop = IOMMU_BTOP(dvma_prop->dvma_len) * sizeof (uint64_t);
1221 	tsb_size = MIN(tsb_size_prop, tsb_size);
1222 tsb_end:
1223 	kmem_free(dvma_prop, dvma_prop_len);
1224 tsb_done:
1225 	iommu_p->iommu_tsb_size = iommu_tsb_size_encode(tsb_size);
1226 	iommu_p->iommu_ctx_bitmap =
1227 		kmem_zalloc(IOMMU_CTX_BITMAP_SIZE, KM_SLEEP);
1228 	*iommu_p->iommu_ctx_bitmap = 1ull;	/* reserve context 0 */
1229 
1230 	/*
1231 	 * Determine the virtual address of the register block
1232 	 * containing the iommu control registers and determine
1233 	 * the virtual address of schizo specific iommu registers.
1234 	 */
1235 	a = (uintptr_t)pci_p->pci_address[0];
1236 	iommu_p->iommu_flush_ctx_reg =
1237 		(uint64_t *)(a + SCHIZO_IOMMU_FLUSH_CTX_REG_OFFSET);
1238 	if (CHIP_TYPE(pci_p) == PCI_CHIP_TOMATILLO)
1239 		iommu_p->iommu_tfar_reg =
1240 			(uint64_t *)(a + TOMATILLO_IOMMU_ERR_TFAR_OFFSET);
1241 	return (a);	/* PCICSRBase */
1242 }
1243 
1244 void
1245 pci_iommu_teardown(iommu_t *iommu_p)
1246 {
1247 	if (pci_use_contexts)
1248 		iommu_ctx_free(iommu_p);
1249 	if (iommu_p->iommu_mtlb_req_p) {
1250 		kmem_free(iommu_p->iommu_mtlb_req_p,
1251 		    sizeof (dvma_unbind_req_t) * (tm_mtlb_maxpgs + 1));
1252 		mutex_destroy(&iommu_p->iommu_mtlb_lock);
1253 		iommu_p->iommu_mtlb_req_p = NULL;
1254 		iommu_p->iommu_mtlb_nreq = 0;
1255 		iommu_p->iommu_mtlb_npgs = iommu_p->iommu_mtlb_maxpgs = 0;
1256 	}
1257 }
1258 
1259 uintptr_t
1260 get_pbm_reg_base(pci_t *pci_p)
1261 {
1262 	return ((uintptr_t)
1263 		(pci_p->pci_address[0] + SCHIZO_PCI_CTRL_REG_OFFSET));
1264 }
1265 
1266 /* ARGSUSED */
1267 static boolean_t
1268 pci_pbm_panic_callb(void *arg, int code)
1269 {
1270 	pbm_t *pbm_p = (pbm_t *)arg;
1271 	volatile uint64_t *ctrl_reg_p;
1272 
1273 	if (pbm_p->pbm_quiesce_count > 0) {
1274 		ctrl_reg_p = pbm_p->pbm_ctrl_reg;
1275 		*ctrl_reg_p = pbm_p->pbm_saved_ctrl_reg;
1276 	}
1277 
1278 	return (B_TRUE);
1279 }
1280 
1281 static boolean_t
1282 pci_pbm_debug_callb(void *arg, int code)
1283 {
1284 	pbm_t *pbm_p = (pbm_t *)arg;
1285 	volatile uint64_t *ctrl_reg_p;
1286 	uint64_t ctrl_reg;
1287 
1288 	if (pbm_p->pbm_quiesce_count > 0) {
1289 		ctrl_reg_p = pbm_p->pbm_ctrl_reg;
1290 		if (code == 0) {
1291 			*ctrl_reg_p = pbm_p->pbm_saved_ctrl_reg;
1292 		} else {
1293 			ctrl_reg = pbm_p->pbm_saved_ctrl_reg;
1294 			ctrl_reg &= ~(SCHIZO_PCI_CTRL_ARB_EN_MASK |
1295 			    SCHIZO_PCI_CTRL_ARB_PARK);
1296 			*ctrl_reg_p = ctrl_reg;
1297 		}
1298 	}
1299 
1300 	return (B_TRUE);
1301 }
1302 
1303 void
1304 pci_pbm_setup(pbm_t *pbm_p)
1305 {
1306 	pci_t *pci_p = pbm_p->pbm_pci_p;
1307 	caddr_t a = pci_p->pci_address[0]; /* PBM block base VA */
1308 	uint64_t pa = va_to_pa(a);
1309 	extern int segkmem_reloc;
1310 
1311 	mutex_init(&pbm_p->pbm_sync_mutex, NULL, MUTEX_DRIVER,
1312 	    (void *)ipltospl(XCALL_PIL));
1313 
1314 	pbm_p->pbm_config_header = (config_header_t *)pci_p->pci_address[2];
1315 	pbm_p->pbm_ctrl_reg = (uint64_t *)(a + SCHIZO_PCI_CTRL_REG_OFFSET);
1316 	pbm_p->pbm_diag_reg = (uint64_t *)(a + SCHIZO_PCI_DIAG_REG_OFFSET);
1317 	pbm_p->pbm_async_flt_status_reg =
1318 		(uint64_t *)(a + SCHIZO_PCI_ASYNC_FLT_STATUS_REG_OFFSET);
1319 	pbm_p->pbm_async_flt_addr_reg =
1320 		(uint64_t *)(a + SCHIZO_PCI_ASYNC_FLT_ADDR_REG_OFFSET);
1321 	pbm_p->pbm_estar_reg = (uint64_t *)(a + SCHIZO_PCI_ESTAR_REG_OFFSET);
1322 	pbm_p->pbm_pcix_err_stat_reg = (uint64_t *)(a +
1323 	    XMITS_PCI_X_ERROR_STATUS_REG_OFFSET);
1324 	pbm_p->pbm_pci_ped_ctrl = (uint64_t *)(a +
1325 	    XMITS_PARITY_DETECT_REG_OFFSET);
1326 
1327 	/*
1328 	 * Create a property to indicate that this node supports DVMA
1329 	 * page relocation.
1330 	 */
1331 	if (CHIP_TYPE(pci_p) != PCI_CHIP_TOMATILLO && segkmem_reloc != 0) {
1332 		pci_dvma_remap_enabled = 1;
1333 		(void) ndi_prop_create_boolean(DDI_DEV_T_NONE,
1334 		    pci_p->pci_dip, "dvma-remap-supported");
1335 	}
1336 
1337 	/*
1338 	 * Register a panic callback so we can unquiesce this bus
1339 	 * if it has been placed in the quiesced state.
1340 	 */
1341 	pbm_p->pbm_panic_cb_id = callb_add(pci_pbm_panic_callb,
1342 	    (void *)pbm_p, CB_CL_PANIC, "pci_panic");
1343 	pbm_p->pbm_debug_cb_id = callb_add(pci_pbm_panic_callb,
1344 	    (void *)pbm_p, CB_CL_ENTER_DEBUGGER, "pci_debug_enter");
1345 
1346 	if (CHIP_TYPE(pci_p) != PCI_CHIP_SCHIZO)
1347 		goto non_schizo;
1348 
1349 	if (PCI_CHIP_ID(pci_p) >= SCHIZO_VER_23) {
1350 
1351 		pbm_p->pbm_sync_reg_pa = pa + SCHIZO_PBM_DMA_SYNC_REG_OFFSET;
1352 
1353 		/*
1354 		 * This is a software workaround to fix schizo hardware bug.
1355 		 * Create a boolean property and its existence means consistent
1356 		 * dma sync should not be done while in prom. The usb polled
1357 		 * code (OHCI,EHCI) will check for this property and will not
1358 		 * do dma sync if this property exist.
1359 		 */
1360 		(void) ndi_prop_create_boolean(DDI_DEV_T_NONE,
1361 		    pci_p->pci_dip, "no-prom-cdma-sync");
1362 	}
1363 	return;
1364 non_schizo:
1365 	if (CHIP_TYPE(pci_p) == PCI_CHIP_TOMATILLO) {
1366 		pci_dvma_sync_before_unmap = 1;
1367 		pa = pci_p->pci_cb_p->cb_icbase_pa;
1368 	}
1369 	if (CHIP_TYPE(pci_p) == PCI_CHIP_XMITS)
1370 		pbm_p->pbm_upper_retry_counter_reg =
1371 		    (uint64_t *)(a + XMITS_UPPER_RETRY_COUNTER_REG_OFFSET);
1372 
1373 	pbm_p->pbm_sync_reg_pa = pa + PBM_DMA_SYNC_PEND_REG_OFFSET;
1374 }
1375 
1376 void
1377 pci_pbm_teardown(pbm_t *pbm_p)
1378 {
1379 	(void) callb_delete(pbm_p->pbm_panic_cb_id);
1380 	(void) callb_delete(pbm_p->pbm_debug_cb_id);
1381 }
1382 
1383 uintptr_t
1384 pci_ib_setup(ib_t *ib_p)
1385 {
1386 	/*
1387 	 * Determine virtual addresses of bridge specific registers,
1388 	 */
1389 	pci_t *pci_p = ib_p->ib_pci_p;
1390 	uintptr_t a = (uintptr_t)pci_p->pci_address[0];
1391 
1392 	ib_p->ib_ign = PCI_ID_TO_IGN(pci_p->pci_id);
1393 	ib_p->ib_max_ino = SCHIZO_MAX_INO;
1394 	ib_p->ib_slot_intr_map_regs = a + SCHIZO_IB_SLOT_INTR_MAP_REG_OFFSET;
1395 	ib_p->ib_intr_map_regs = a + SCHIZO_IB_INTR_MAP_REG_OFFSET;
1396 	ib_p->ib_slot_clear_intr_regs =
1397 		a + SCHIZO_IB_CLEAR_INTR_REG_OFFSET;
1398 	return (a);
1399 }
1400 
1401 void
1402 pci_sc_setup(sc_t *sc_p)
1403 {
1404 	pci_t *pci_p = sc_p->sc_pci_p;
1405 	uintptr_t a;
1406 
1407 	/*
1408 	 * Determine the virtual addresses of the stream cache
1409 	 * control/status and flush registers.
1410 	 */
1411 	a = (uintptr_t)pci_p->pci_address[0];	/* PCICSRBase */
1412 	sc_p->sc_ctrl_reg = (uint64_t *)(a + SCHIZO_SC_CTRL_REG_OFFSET);
1413 	sc_p->sc_invl_reg = (uint64_t *)(a + SCHIZO_SC_INVL_REG_OFFSET);
1414 	sc_p->sc_sync_reg = (uint64_t *)(a + SCHIZO_SC_SYNC_REG_OFFSET);
1415 	sc_p->sc_ctx_invl_reg = (uint64_t *)(a + SCHIZO_SC_CTX_INVL_REG_OFFSET);
1416 	sc_p->sc_ctx_match_reg =
1417 		(uint64_t *)(a + SCHIZO_SC_CTX_MATCH_REG_OFFSET);
1418 
1419 	/*
1420 	 * Determine the virtual addresses of the streaming cache
1421 	 * diagnostic access registers.
1422 	 */
1423 	sc_p->sc_data_diag_acc = (uint64_t *)(a + SCHIZO_SC_DATA_DIAG_OFFSET);
1424 	sc_p->sc_tag_diag_acc = (uint64_t *)(a + SCHIZO_SC_TAG_DIAG_OFFSET);
1425 	sc_p->sc_ltag_diag_acc = (uint64_t *)(a + SCHIZO_SC_LTAG_DIAG_OFFSET);
1426 }
1427 
1428 /*ARGSUSED*/
1429 int
1430 pci_get_numproxy(dev_info_t *dip)
1431 {
1432 	/*
1433 	 * Schizo does not support interrupt proxies.
1434 	 */
1435 	return (0);
1436 }
1437 
1438 /*
1439  * pcisch error handling 101:
1440  *
1441  * The various functions below are responsible for error handling. Given
1442  * a particular error, they must gather the appropriate state, report all
1443  * errors with correct payload, and attempt recovery where ever possible.
1444  *
1445  * Recovery in the context of this driver is being able notify a leaf device
1446  * of the failed transaction. This leaf device may either be the master or
1447  * target for this transaction and may have already received an error
1448  * notification via a PCI interrupt. Notification is done via DMA and access
1449  * handles. If we capture an address for the transaction then we can map it
1450  * to a handle(if the leaf device is fma-compliant) and fault the handle as
1451  * well as call the device driver registered callback.
1452  *
1453  * The hardware can either interrupt or trap upon detection of an error, in
1454  * some rare cases it also causes a fatal reset.
1455  *
1456  * cb_buserr_intr() is responsible for handling control block
1457  * errors(errors which stem from the host bus side of the bridge). Since
1458  * we support multiple chips and host bus standards, cb_buserr_intr will
1459  * call a bus specific error handler to report and handle the detected
1460  * error. Since this error can either affect or orginate from either of the
1461  * two PCI busses which are connected to the bridge, we need to call
1462  * pci_pbm_err_handler() for each bus as well to report their errors. We
1463  * also need to gather possible errors which have been detected by their
1464  * compliant children(via ndi_fm_handler_dispatch()).
1465  *
1466  * pbm_error_intr() and ecc_intr() are responsible for PCI Block Module
1467  * errors(generic PCI + bridge specific) and ECC errors, respectively. They
1468  * are common between pcisch and pcipsy and therefore exist in pci_pbm.c and
1469  * pci_ecc.c. To support error handling certain chip specific handlers
1470  * must exist and they are defined below.
1471  *
1472  * cpu_deferred_error() and cpu_async_error(), handle the traps that may
1473  * have originated from IO space. They call into the registered IO callbacks
1474  * to report and handle errors that may have caused the trap.
1475  *
1476  * pci_pbm_err_handler() is called by pbm_error_intr() or pci_err_callback()
1477  * (generic fma callback for pcipsy/pcisch, pci_fm.c). pci_err_callback() is
1478  * called when the CPU has trapped because of a possible IO error(TO/BERR/UE).
1479  * It will call pci_pbm_err_handler() to report and handle all PCI/PBM/IOMMU
1480  * related errors which are detected by the chip.
1481  *
1482  * pci_pbm_err_handler() calls a generic interface pbm_afsr_report()(pci_pbm.c)
1483  * to report the pbm specific errors and attempt to map the failed address
1484  * (if captured) to a device instance. pbm_afsr_report() calls a chip specific
1485  * interface to interpret the afsr bits pci_pbm_classify()(pcisch.c/pcipsy.c).
1486  * pci_pbm_err_handler() also calls iommu_err_handler() to handle IOMMU related
1487  * errors.
1488  *
1489  * iommu_err_handler() can recover from most errors, as long as the requesting
1490  * device is notified and the iommu can be flushed. If an IOMMU error occurs
1491  * due to a UE then it will be passed on to the ecc_err_handler() for
1492  * subsequent handling.
1493  *
1494  * ecc_err_handler()(pci_ecc.c) also calls a chip specific interface to
1495  * interpret the afsr, pci_ecc_classify(). ecc_err_handler() also calls
1496  * pci_pbm_err_handler() to report any pbm errors detected.
1497  *
1498  * To make sure that the trap code and the interrupt code are not going
1499  * to step on each others toes we have a per chip pci_fm_mutex. This also
1500  * makes it necessary for us to be caution while we are at a high PIL, so
1501  * that we do not cause a subsequent trap that causes us to hang.
1502  *
1503  * The attempt to commonize code was meant to keep in line with the current
1504  * pci driver implementation and it was not meant to confuse. If you are
1505  * confused then don't worry, I was too.
1506  *
1507  */
1508 static void
1509 pci_cb_errstate_get(cb_t *cb_p, cb_errstate_t *cb_err_p)
1510 {
1511 	uint64_t pa = cb_p->cb_base_pa;
1512 	int	i;
1513 
1514 	bzero(cb_err_p, sizeof (cb_errstate_t));
1515 
1516 	ASSERT(MUTEX_HELD(&cb_p->cb_pci_cmn_p->pci_fm_mutex));
1517 
1518 	cb_err_p->cb_bridge_type = PCI_BRIDGE_TYPE(cb_p->cb_pci_cmn_p);
1519 
1520 	cb_err_p->cb_csr = lddphysio(pa + SCHIZO_CB_CSR_OFFSET);
1521 	cb_err_p->cb_err = lddphysio(pa + SCHIZO_CB_ERRCTRL_OFFSET);
1522 	cb_err_p->cb_intr = lddphysio(pa + SCHIZO_CB_INTCTRL_OFFSET);
1523 	cb_err_p->cb_elog = lddphysio(pa + SCHIZO_CB_ERRLOG_OFFSET);
1524 	cb_err_p->cb_ecc = lddphysio(pa + SCHIZO_CB_ECCCTRL_OFFSET);
1525 	cb_err_p->cb_ue_afsr = lddphysio(pa + SCHIZO_CB_UEAFSR_OFFSET);
1526 	cb_err_p->cb_ue_afar = lddphysio(pa + SCHIZO_CB_UEAFAR_OFFSET);
1527 	cb_err_p->cb_ce_afsr = lddphysio(pa + SCHIZO_CB_CEAFSR_OFFSET);
1528 	cb_err_p->cb_ce_afar = lddphysio(pa + SCHIZO_CB_CEAFAR_OFFSET);
1529 
1530 	if ((CB_CHIP_TYPE((cb_t *)cb_p)) == PCI_CHIP_XMITS) {
1531 		cb_err_p->cb_first_elog = lddphysio(pa +
1532 				XMITS_CB_FIRST_ERROR_LOG);
1533 		cb_err_p->cb_first_eaddr = lddphysio(pa +
1534 				XMITS_CB_FIRST_ERROR_ADDR);
1535 		cb_err_p->cb_leaf_status = lddphysio(pa +
1536 				XMITS_CB_FIRST_ERROR_ADDR);
1537 	}
1538 
1539 	/* Gather PBM state information for both sides of this chip */
1540 	for (i = 0; i < 2; i++) {
1541 		if (cb_p->cb_pci_cmn_p->pci_p[i] == NULL)
1542 			continue;
1543 		pci_pbm_errstate_get(((cb_t *)cb_p)->cb_pci_cmn_p->
1544 					    pci_p[i], &cb_err_p->cb_pbm[i]);
1545 	}
1546 }
1547 
1548 static void
1549 pci_cb_clear_error(cb_t *cb_p, cb_errstate_t *cb_err_p)
1550 {
1551 	uint64_t pa = ((cb_t *)cb_p)->cb_base_pa;
1552 
1553 	stdphysio(pa + SCHIZO_CB_ERRLOG_OFFSET, cb_err_p->cb_elog);
1554 }
1555 
1556 static cb_fm_err_t safari_err_tbl[] = {
1557 	SAFARI_BAD_CMD,		SCHIZO_CB_ELOG_BAD_CMD,		CB_FATAL,
1558 	SAFARI_SSM_DIS,		SCHIZO_CB_ELOG_SSM_DIS,		CB_FATAL,
1559 	SAFARI_BAD_CMD_PCIA, 	SCHIZO_CB_ELOG_BAD_CMD_PCIA,	CB_FATAL,
1560 	SAFARI_BAD_CMD_PCIB, 	SCHIZO_CB_ELOG_BAD_CMD_PCIB,	CB_FATAL,
1561 	SAFARI_PAR_ERR_INT_PCIB, XMITS_CB_ELOG_PAR_ERR_INT_PCIB, CB_FATAL,
1562 	SAFARI_PAR_ERR_INT_PCIA, XMITS_CB_ELOG_PAR_ERR_INT_PCIA, CB_FATAL,
1563 	SAFARI_PAR_ERR_INT_SAF,	XMITS_CB_ELOG_PAR_ERR_INT_SAF,	CB_FATAL,
1564 	SAFARI_PLL_ERR_PCIB,	XMITS_CB_ELOG_PLL_ERR_PCIB,	CB_FATAL,
1565 	SAFARI_PLL_ERR_PCIA,	XMITS_CB_ELOG_PLL_ERR_PCIA,	CB_FATAL,
1566 	SAFARI_PLL_ERR_SAF,	XMITS_CB_ELOG_PLL_ERR_SAF,	CB_FATAL,
1567 	SAFARI_SAF_CIQ_TO,	SCHIZO_CB_ELOG_SAF_CIQ_TO,	CB_FATAL,
1568 	SAFARI_SAF_LPQ_TO,	SCHIZO_CB_ELOG_SAF_LPQ_TO,	CB_FATAL,
1569 	SAFARI_SAF_SFPQ_TO,	SCHIZO_CB_ELOG_SAF_SFPQ_TO,	CB_FATAL,
1570 	SAFARI_APERR,		SCHIZO_CB_ELOG_ADDR_PAR_ERR,	CB_FATAL,
1571 	SAFARI_UNMAP_ERR,	SCHIZO_CB_ELOG_UNMAP_ERR,	CB_FATAL,
1572 	SAFARI_BUS_ERR,		SCHIZO_CB_ELOG_BUS_ERR,		CB_FATAL,
1573 	SAFARI_TO_ERR,		SCHIZO_CB_ELOG_TO_ERR,		CB_FATAL,
1574 	SAFARI_DSTAT_ERR,	SCHIZO_CB_ELOG_DSTAT_ERR,	CB_FATAL,
1575 	SAFARI_SAF_UFPQ_TO,	SCHIZO_CB_ELOG_SAF_UFPQ_TO,	CB_FATAL,
1576 	SAFARI_CPU0_PAR_SINGLE,	SCHIZO_CB_ELOG_CPU0_PAR_SINGLE,	CB_FATAL,
1577 	SAFARI_CPU0_PAR_BIDI,	SCHIZO_CB_ELOG_CPU0_PAR_BIDI,	CB_FATAL,
1578 	SAFARI_CPU1_PAR_SINGLE,	SCHIZO_CB_ELOG_CPU1_PAR_SINGLE,	CB_FATAL,
1579 	SAFARI_CPU1_PAR_BIDI,	SCHIZO_CB_ELOG_CPU1_PAR_BIDI,	CB_FATAL,
1580 	NULL,			NULL,				NULL,
1581 };
1582 
1583 /*
1584  * Function used to handle and log Safari bus errors.
1585  */
1586 static int
1587 safari_err_handler(dev_info_t *dip, uint64_t fme_ena,
1588 		cb_errstate_t *cb_err_p)
1589 {
1590 	int	i;
1591 	int	fatal = 0;
1592 	pci_t *pci_p = get_pci_soft_state(ddi_get_instance(dip));
1593 	pci_common_t *cmn_p = pci_p->pci_common_p;
1594 
1595 	ASSERT(MUTEX_HELD(&cmn_p->pci_fm_mutex));
1596 
1597 	for (i = 0; safari_err_tbl[i].cb_err_class != NULL; i++) {
1598 		if (cb_err_p->cb_elog & safari_err_tbl[i].cb_reg_bit) {
1599 			cb_err_p->cb_err_class = safari_err_tbl[i].cb_err_class;
1600 			cb_ereport_post(dip, fme_ena, cb_err_p);
1601 			fatal += safari_err_tbl[i].cb_fatal;
1602 		}
1603 	}
1604 
1605 	if (fatal)
1606 		return (DDI_FM_FATAL);
1607 	return (DDI_FM_OK);
1608 
1609 }
1610 
1611 /*
1612  * Check pbm va log register for captured errant address, and fail handle
1613  * if in per device cache.
1614  * Called from jbus_err_handler.
1615  */
1616 static int
1617 jbus_check_va_log(cb_t *cb_p, uint64_t fme_ena,
1618     cb_errstate_t *cb_err_p)
1619 {
1620 	int i;
1621 	int ret = DDI_FM_FATAL;
1622 	pci_common_t *cmn_p = cb_p->cb_pci_cmn_p;
1623 
1624 	ASSERT(MUTEX_HELD(&cmn_p->pci_fm_mutex));
1625 	/*
1626 	 * Check VA log register for address associated with error,
1627 	 * if no address is registered then return failure
1628 	 */
1629 	for (i = 0; i < 2; i++) {
1630 
1631 		if (cb_p->cb_pci_cmn_p->pci_p[i] == NULL)
1632 			continue;
1633 		/*
1634 		 * Look up and fault handle associated with
1635 		 * logged DMA address
1636 		 */
1637 		if (cb_err_p->cb_pbm[i].pbm_va_log) {
1638 			ret = pci_handle_lookup(cb_p->cb_pci_cmn_p->pci_p[i]->
1639 					pci_dip, DMA_HANDLE, fme_ena,
1640 					(void *)&cb_err_p->cb_pbm[i].
1641 					pbm_va_log);
1642 			if (ret == DDI_FM_NONFATAL)
1643 				break;
1644 		}
1645 	}
1646 	return (ret);
1647 }
1648 
1649 static cb_fm_err_t jbus_err_tbl[] = {
1650 	JBUS_APERR,		SCHIZO_CB_ELOG_ADDR_PAR_ERR,	CB_FATAL,
1651 	JBUS_PWR_DATA_PERR,	TOMATILLO_CB_ELOG_WR_DATA_PAR_ERR, CB_FATAL,
1652 	JBUS_DRD_DATA_PERR,	TOMATILLO_CB_ELOG_RD_DATA_PAR_ERR, CB_NONFATAL,
1653 	JBUS_CTL_PERR,		TOMATILLO_CB_ELOG_CTL_PAR_ERR,	CB_FATAL,
1654 	JBUS_ILL_BYTE_EN,	TOMATILLO_CB_ELOG_ILL_BYTE_EN,	CB_FATAL,
1655 	JBUS_ILL_COH_IN,	TOMATILLO_CB_ELOG_ILL_COH_IN,	CB_FATAL,
1656 	JBUS_SNOOP_ERR_RD,	TOMATILLO_CB_ELOG_SNOOP_ERR_RD,	CB_FATAL,
1657 	JBUS_SNOOP_ERR_RDS,	TOMATILLO_CB_ELOG_SNOOP_ERR_RDS, CB_FATAL,
1658 	JBUS_SNOOP_ERR_RDSA,	TOMATILLO_CB_ELOG_SNOOP_ERR_RDSA, CB_FATAL,
1659 	JBUS_SNOOP_ERR_OWN,	TOMATILLO_CB_ELOG_SNOOP_ERR_OWN, CB_FATAL,
1660 	JBUS_SNOOP_ERR_RDO,	TOMATILLO_CB_ELOG_SNOOP_ERR_RDO, CB_FATAL,
1661 	JBUS_SNOOP_ERR_PCI,	TOMATILLO_CB_ELOG_SNOOP_ERR_PCI, CB_FATAL,
1662 	JBUS_SNOOP_ERR_GR,	TOMATILLO_CB_ELOG_SNOOP_ERR_GR,	CB_FATAL,
1663 	JBUS_SNOOP_ERR,		TOMATILLO_CB_ELOG_SNOOP_ERR,	CB_FATAL,
1664 	JBUS_BAD_CMD,		SCHIZO_CB_ELOG_BAD_CMD,		CB_FATAL,
1665 	JBUS_UNMAP_ERR,		SCHIZO_CB_ELOG_UNMAP_ERR,	CB_NONFATAL,
1666 	JBUS_TO_EXP_ERR,	TOMATILLO_CB_ELOG_TO_EXP_ERR,	CB_NONFATAL,
1667 	JBUS_TO_ERR,		SCHIZO_CB_ELOG_TO_ERR,		CB_NONFATAL,
1668 	JBUS_BUS_ERR,		SCHIZO_CB_ELOG_BUS_ERR,		CB_NONFATAL,
1669 	NULL,			NULL,				NULL,
1670 };
1671 
1672 /*
1673  * Function used to handle and log Jbus errors.
1674  */
1675 static int
1676 jbus_err_handler(dev_info_t *dip, uint64_t fme_ena,
1677     cb_errstate_t *cb_err_p)
1678 {
1679 	int	fatal = 0;
1680 	int	nonfatal = 0;
1681 	int	i;
1682 	pci_t	*pci_p = get_pci_soft_state(ddi_get_instance(dip));
1683 	cb_t	*cb_p = pci_p->pci_cb_p;
1684 
1685 	ASSERT(MUTEX_HELD(&pci_p->pci_common_p->pci_fm_mutex));
1686 
1687 	for (i = 0; jbus_err_tbl[i].cb_err_class != NULL; i++) {
1688 		if (!(cb_err_p->cb_elog & jbus_err_tbl[i].cb_reg_bit))
1689 			continue;
1690 		cb_err_p->cb_err_class = jbus_err_tbl[i].cb_err_class;
1691 		if (jbus_err_tbl[i].cb_fatal) {
1692 			fatal += jbus_err_tbl[i].cb_fatal;
1693 			continue;
1694 		}
1695 		if (jbus_check_va_log(cb_p, fme_ena, cb_err_p)
1696 				!= DDI_FM_NONFATAL) {
1697 			fatal++;
1698 		}
1699 		cb_ereport_post(dip, fme_ena, cb_err_p);
1700 	}
1701 
1702 	return (fatal ? DDI_FM_FATAL : (nonfatal ? DDI_FM_NONFATAL :
1703 				DDI_FM_OK));
1704 }
1705 
1706 /*
1707  * Control Block error interrupt handler.
1708  */
1709 uint_t
1710 cb_buserr_intr(caddr_t a)
1711 {
1712 	cb_t *cb_p = (cb_t *)a;
1713 	pci_common_t *cmn_p = cb_p->cb_pci_cmn_p;
1714 	pci_t *pci_p = cmn_p->pci_p[0];
1715 	cb_errstate_t cb_err;
1716 	ddi_fm_error_t derr;
1717 	int ret = DDI_FM_FATAL;
1718 	int i;
1719 
1720 	if (pci_p == NULL)
1721 		pci_p = cmn_p->pci_p[1];
1722 
1723 	bzero(&derr, sizeof (ddi_fm_error_t));
1724 	derr.fme_version = DDI_FME_VERSION;
1725 	derr.fme_ena = fm_ena_generate(0, FM_ENA_FMT1);
1726 
1727 	mutex_enter(&cmn_p->pci_fm_mutex);
1728 
1729 	pci_cb_errstate_get(cb_p, &cb_err);
1730 
1731 	if (CB_CHIP_TYPE(cb_p) == PCI_CHIP_TOMATILLO)
1732 		ret = jbus_err_handler(pci_p->pci_dip, derr.fme_ena, &cb_err);
1733 	else if ((CB_CHIP_TYPE(cb_p) == PCI_CHIP_SCHIZO) ||
1734 			(CB_CHIP_TYPE(cb_p) == PCI_CHIP_XMITS))
1735 		ret = safari_err_handler(pci_p->pci_dip, derr.fme_ena,
1736 		    &cb_err);
1737 
1738 	/*
1739 	 * Check for related errors in PBM and IOMMU. The IOMMU could cause
1740 	 * a timeout on the jbus due to an IOMMU miss, so we need to check and
1741 	 * log the IOMMU error registers.
1742 	 */
1743 	for (i = 0; i < 2; i++) {
1744 		if (cmn_p->pci_p[i] == NULL)
1745 			continue;
1746 		if (pci_pbm_err_handler(cmn_p->pci_p[i]->pci_dip, &derr,
1747 		    (void *)cmn_p->pci_p[i], PCI_CB_CALL) == DDI_FM_FATAL)
1748 			ret = DDI_FM_FATAL;
1749 	}
1750 
1751 	/* Cleanup and reset error bits */
1752 	(void) pci_cb_clear_error(cb_p, &cb_err);
1753 	mutex_exit(&cmn_p->pci_fm_mutex);
1754 
1755 	if (ret == DDI_FM_FATAL) {
1756 		fm_panic("Fatal System Bus Error has occurred\n");
1757 	}
1758 
1759 	return (DDI_INTR_CLAIMED);
1760 }
1761 
1762 static ecc_fm_err_t ecc_err_tbl[] = {
1763 	PCI_ECC_PIO_UE, COMMON_ECC_UE_AFSR_E_PIO, CBNINTR_UE,
1764 	PBM_PRIMARY, SCHIZO_ECC_AFAR_PIOW_UPA64S, SCH_REG_UPA,
1765 	ACC_HANDLE,
1766 
1767 	PCI_ECC_PIO_UE, COMMON_ECC_UE_AFSR_E_PIO, CBNINTR_UE,
1768 	PBM_PRIMARY, SCHIZO_ECC_AFAR_PIOW_PCIA_REG, SCH_REG_PCIA_REG,
1769 	ACC_HANDLE,
1770 
1771 	PCI_ECC_PIO_UE, COMMON_ECC_UE_AFSR_E_PIO, CBNINTR_UE,
1772 	PBM_PRIMARY, SCHIZO_ECC_AFAR_PIOW_PCIA_MEM, SCH_REG_PCIA_MEM,
1773 	ACC_HANDLE,
1774 
1775 	PCI_ECC_PIO_UE, COMMON_ECC_UE_AFSR_E_PIO, CBNINTR_UE,
1776 	PBM_PRIMARY, SCHIZO_ECC_AFAR_PIOW_PCIA_CFGIO, SCH_REG_PCIA_CFGIO,
1777 	ACC_HANDLE,
1778 
1779 	PCI_ECC_PIO_UE, COMMON_ECC_UE_AFSR_E_PIO, CBNINTR_UE,
1780 	PBM_PRIMARY, SCHIZO_ECC_AFAR_PIOW_PCIB_REG, SCH_REG_PCIB_REG,
1781 	ACC_HANDLE,
1782 
1783 	PCI_ECC_PIO_UE, COMMON_ECC_UE_AFSR_E_PIO, CBNINTR_UE,
1784 	PBM_PRIMARY, SCHIZO_ECC_AFAR_PIOW_PCIB_MEM, SCH_REG_PCIB_MEM,
1785 	ACC_HANDLE,
1786 
1787 	PCI_ECC_PIO_UE, COMMON_ECC_UE_AFSR_E_PIO, CBNINTR_UE,
1788 	PBM_PRIMARY, SCHIZO_ECC_AFAR_PIOW_PCIB_CFGIO, SCH_REG_PCIB_CFGIO,
1789 	ACC_HANDLE,
1790 
1791 	PCI_ECC_PIO_UE, COMMON_ECC_UE_AFSR_E_PIO, CBNINTR_UE,
1792 	PBM_PRIMARY, SCHIZO_ECC_AFAR_PIOW_SAFARI_REGS, SCH_REG_SAFARI_REGS,
1793 	ACC_HANDLE,
1794 
1795 	PCI_ECC_SEC_PIO_UE, COMMON_ECC_UE_AFSR_E_PIO,  CBNINTR_UE,
1796 	PBM_SECONDARY, NULL, NULL, ACC_HANDLE,
1797 
1798 	PCI_ECC_PIO_CE, COMMON_ECC_UE_AFSR_E_PIO,  CBNINTR_CE,
1799 	PBM_PRIMARY, NULL, NULL, ACC_HANDLE,
1800 
1801 	PCI_ECC_SEC_PIO_CE, COMMON_ECC_UE_AFSR_E_PIO,  CBNINTR_CE,
1802 	PBM_SECONDARY, NULL, NULL, ACC_HANDLE,
1803 
1804 	PCI_ECC_DRD_UE, COMMON_ECC_UE_AFSR_E_DRD, CBNINTR_UE,
1805 	PBM_PRIMARY, NULL, NULL, DMA_HANDLE,
1806 
1807 	PCI_ECC_SEC_DRD_UE, COMMON_ECC_UE_AFSR_E_DRD, CBNINTR_UE,
1808 	PBM_SECONDARY, NULL, NULL, DMA_HANDLE,
1809 
1810 	PCI_ECC_DRD_CE, COMMON_ECC_UE_AFSR_E_DRD, CBNINTR_CE,
1811 	PBM_PRIMARY, NULL, NULL, DMA_HANDLE,
1812 
1813 	PCI_ECC_SEC_DRD_CE, COMMON_ECC_UE_AFSR_E_DRD, CBNINTR_CE,
1814 	PBM_SECONDARY, NULL, NULL, DMA_HANDLE,
1815 
1816 	PCI_ECC_DWR_UE, COMMON_ECC_UE_AFSR_E_DWR, CBNINTR_UE,
1817 	PBM_PRIMARY, NULL, NULL, DMA_HANDLE,
1818 
1819 	PCI_ECC_SEC_DWR_UE, COMMON_ECC_UE_AFSR_E_DWR, CBNINTR_UE,
1820 	PBM_SECONDARY, NULL, NULL, DMA_HANDLE,
1821 
1822 	PCI_ECC_DWR_CE, COMMON_ECC_UE_AFSR_E_DWR, CBNINTR_CE,
1823 	PBM_PRIMARY, NULL, NULL, DMA_HANDLE,
1824 
1825 	PCI_ECC_SEC_DWR_CE, COMMON_ECC_UE_AFSR_E_DWR, CBNINTR_CE,
1826 	PBM_SECONDARY, NULL, NULL, DMA_HANDLE,
1827 
1828 	NULL, NULL, NULL, NULL, NULL, NULL,
1829 };
1830 
1831 /*
1832  * pci_ecc_classify, called by ecc_handler to classify ecc errors
1833  * and determine if we should panic or not.
1834  */
1835 void
1836 pci_ecc_classify(uint64_t err, ecc_errstate_t *ecc_err_p)
1837 {
1838 	struct async_flt *ecc_p = &ecc_err_p->ecc_aflt;
1839 	uint64_t region, afar = ecc_p->flt_addr;
1840 	int i, j, ret = 0;
1841 	int flag, fatal = 0;
1842 	pci_common_t *cmn_p = ecc_err_p->ecc_ii_p.ecc_p->ecc_pci_cmn_p;
1843 	pci_t *pci_p = cmn_p->pci_p[0];
1844 
1845 	ASSERT(MUTEX_HELD(&cmn_p->pci_fm_mutex));
1846 
1847 	ecc_err_p->ecc_bridge_type = PCI_BRIDGE_TYPE(cmn_p);
1848 
1849 	if (pci_p == NULL)
1850 		pci_p = cmn_p->pci_p[1];
1851 
1852 	ecc_err_p->ecc_ctrl = lddphysio(ecc_err_p->ecc_ii_p.ecc_p->ecc_csr_pa);
1853 	ecc_err_p->ecc_err_addr = afar;
1854 	region = afar & SCHIZO_ECC_AFAR_PIOW_MASK;
1855 
1856 	for (i = 0; ecc_err_tbl[i].ecc_err_class != NULL; i++) {
1857 		if (!(err & ecc_err_tbl[i].ecc_reg_bit) ||
1858 			(ecc_err_p->ecc_ii_p.ecc_type !=
1859 			    ecc_err_tbl[i].ecc_type) ||
1860 			(ecc_err_p->ecc_pri != ecc_err_tbl[i].ecc_pri))
1861 			continue;
1862 
1863 		ecc_p->flt_erpt_class = ecc_err_tbl[i].ecc_err_class;
1864 		flag = ecc_err_tbl[i].ecc_flag;
1865 
1866 		if (!ecc_err_tbl[i].ecc_pri ||
1867 				(ecc_err_tbl[i].ecc_type == CBNINTR_CE)) {
1868 			fatal += (ecc_err_tbl[i].ecc_type == CBNINTR_UE) ?
1869 				1 : 0;
1870 			break;
1871 		}
1872 
1873 		if (flag == ACC_HANDLE &&
1874 			(region & ecc_err_tbl[i].ecc_region_bits)) {
1875 			ecc_err_p->ecc_region = ecc_err_tbl[i].ecc_region;
1876 			pci_format_ecc_addr(pci_p->pci_dip,
1877 					&ecc_err_p->ecc_err_addr,
1878 					ecc_err_p->ecc_region);
1879 		}
1880 
1881 		/*
1882 		 * Lookup and fault errant handle
1883 		 */
1884 		for (j = 0; j < 2; ++j) {
1885 			ret = DDI_FM_UNKNOWN;
1886 			if (cmn_p->pci_p[j] == NULL)
1887 				continue;
1888 			ret = pci_handle_lookup(cmn_p->pci_p[j]->pci_dip,
1889 					flag, ecc_err_p->ecc_ena,
1890 					(void *)&ecc_err_p->ecc_err_addr);
1891 			if (ret == DDI_FM_NONFATAL) {
1892 				fatal = 0;
1893 				break;
1894 			} else
1895 				fatal++;
1896 		}
1897 		break;
1898 	}
1899 
1900 	if (fatal)
1901 		ecc_p->flt_panic = 1;
1902 	else if (flag != ACC_HANDLE)
1903 		ecc_err_p->ecc_pg_ret = 1;
1904 }
1905 
1906 /*
1907  * Tables to define PCI-X Split Completion errors
1908  */
1909 
1910 pcix_err_msg_rec_t pcix_completer_errs[] = {
1911 	{PCIX_CPLT_OUT_OF_RANGE,	"pcix", "oor"	},
1912 };
1913 
1914 pcix_err_tbl_t pcix_split_errs_tbl[] = {
1915 	{PCIX_CLASS_CPLT,
1916 		sizeof (pcix_completer_errs)/sizeof (pcix_err_msg_rec_t),
1917 		pcix_completer_errs		},
1918 };
1919 
1920 /*
1921  * Tables for the PCI-X error status messages
1922  */
1923 pcix_err_msg_rec_t pcix_stat_errs[] = {
1924 	{XMITS_PCIX_STAT_SC_DSCRD,	"pcix", "discard"  	},
1925 	{XMITS_PCIX_STAT_SC_TTO,	"xmits.pbmx", "tato" 	},
1926 	{XMITS_PCIX_STAT_SMMU,		"xmits.pbmx", "stmmu"	},
1927 	{XMITS_PCIX_STAT_SDSTAT,	"xmits.pbmx", "stdst"	},
1928 	{XMITS_PCIX_STAT_CMMU,		"xmits.pbmx", "cnmmu"	},
1929 	{XMITS_PCIX_STAT_CDSTAT,	"xmits.pbmx", "cndst"	}
1930 };
1931 
1932 pcix_err_tbl_t pcix_stat_errs_tbl =
1933 	{PCIX_NO_CLASS,
1934 		sizeof (pcix_stat_errs)/sizeof (pcix_err_msg_rec_t),
1935 		pcix_stat_errs		};
1936 
1937 
1938 /*
1939  * walk thru a table of error messages, printing as appropriate
1940  *
1941  * t - the table of messages to parse
1942  * err - the error to match against
1943  * multi - flag, sometimes multiple error bits may be set/desired
1944  */
1945 static int
1946 pcix_lookup_err_msgs(dev_info_t *dip, uint64_t ena, pcix_err_tbl_t t,
1947 		pbm_errstate_t *pbm_err_p)
1948 {
1949 	uint32_t err_bits  = pbm_err_p->pbm_err & XMITS_PCIX_MSG_INDEX_MASK;
1950 	int nerr = 0;
1951 	int j;
1952 	char buf[FM_MAX_CLASS];
1953 
1954 	for (j = 0; j < t.err_rec_num; j++)  {
1955 		uint32_t msg_key = t.err_msg_tbl[j].msg_key;
1956 		if (pbm_err_p->pbm_multi ? !(err_bits & msg_key) : err_bits
1957 				!= msg_key)
1958 			continue;
1959 
1960 		(void) snprintf(buf, FM_MAX_CLASS, "%s.%s%s",
1961 		    t.err_msg_tbl[j].msg_class,
1962 		    pbm_err_p->pbm_pri ? "" : PCIX_SECONDARY,
1963 		    t.err_msg_tbl[j].msg_str);
1964 
1965 		pbm_err_p->pbm_err_class = buf;
1966 		pcix_ereport_post(dip, ena, pbm_err_p);
1967 		nerr++;
1968 	}
1969 	return (nerr ? DDI_FM_FATAL : DDI_FM_OK);
1970 }
1971 
1972 /*
1973  * Decodes primary(bit 27-24) or secondary(bit 15-12) PCI-X split
1974  * completion error message class and index in PBM AFSR.
1975  */
1976 static void
1977 pcix_log_split_err(dev_info_t *dip, uint64_t ena, pbm_errstate_t *pbm_err_p)
1978 {
1979 	uint32_t class  = pbm_err_p->pbm_err & XMITS_PCIX_MSG_CLASS_MASK;
1980 	uint32_t num_classes = sizeof (pcix_split_errs_tbl) /
1981 	    sizeof (struct pcix_err_tbl);
1982 	int i;
1983 
1984 	for (i = 0; i < num_classes; i++) {
1985 		if (class == pcix_split_errs_tbl[i].err_class) {
1986 			pbm_err_p->pbm_multi = PCIX_SINGLE_ERR;
1987 			(void) pcix_lookup_err_msgs(dip, ena,
1988 			    pcix_split_errs_tbl[i], pbm_err_p);
1989 			break;
1990 		}
1991 	}
1992 }
1993 
1994 /*
1995  * Report PBM PCI-X Error Status Register if in PCI-X mode
1996  *
1997  * Once a PCI-X fault tree is constructed, the code below may need to
1998  * change.
1999  */
2000 static int
2001 pcix_log_pbm(pci_t *pci_p, uint64_t ena, pbm_errstate_t *pbm_err_p)
2002 {
2003 	int fatal = 0;
2004 	int nonfatal = 0;
2005 	uint32_t e;
2006 
2007 	ASSERT(MUTEX_HELD(&pci_p->pci_common_p->pci_fm_mutex));
2008 
2009 	DEBUG3(DBG_ERR_INTR, pci_p->pci_dip, "pcix_log_pbm: chip_type=%d "
2010 	    "ctr_stat=%lx afsr = 0x%lx", CHIP_TYPE(pci_p),
2011 	    pbm_err_p->pbm_ctl_stat, pbm_err_p->pbm_afsr);
2012 
2013 	if (!(CHIP_TYPE(pci_p) == PCI_CHIP_XMITS) ||
2014 	    !(pbm_err_p->pbm_ctl_stat & XMITS_PCI_CTRL_X_MODE))
2015 		return (DDI_FM_OK);
2016 
2017 	if (pbm_err_p->pbm_afsr & XMITS_PCI_X_AFSR_P_SC_ERR) {
2018 		pbm_err_p->pbm_err = PBM_AFSR_TO_PRISPLIT(pbm_err_p->pbm_afsr);
2019 		pbm_err_p->pbm_pri = PBM_PRIMARY;
2020 		pcix_log_split_err(pci_p->pci_dip, ena, pbm_err_p);
2021 		nonfatal++;
2022 	}
2023 	if (pbm_err_p->pbm_afsr & XMITS_PCI_X_AFSR_S_SC_ERR) {
2024 		pbm_err_p->pbm_err = PBM_AFSR_TO_PRISPLIT(pbm_err_p->pbm_afsr);
2025 		pbm_err_p->pbm_pri = PBM_PRIMARY;
2026 		pcix_log_split_err(pci_p->pci_dip, ena, pbm_err_p);
2027 		nonfatal++;
2028 	}
2029 
2030 	e = PBM_PCIX_TO_PRIERR(pbm_err_p->pbm_pcix_stat);
2031 	if (e) {
2032 		pbm_err_p->pbm_pri = PBM_PRIMARY;
2033 		pbm_err_p->pbm_err = e;
2034 		pbm_err_p->pbm_multi = PCIX_MULTI_ERR;
2035 		if (pcix_lookup_err_msgs(pci_p->pci_dip, ena,
2036 		    pcix_stat_errs_tbl, pbm_err_p) == DDI_FM_FATAL)
2037 			fatal++;
2038 		else
2039 			nonfatal++;
2040 	}
2041 
2042 	e = PBM_PCIX_TO_SECERR(pbm_err_p->pbm_pcix_stat);
2043 	if (e) {
2044 		pbm_err_p->pbm_pri = PBM_SECONDARY;
2045 		pbm_err_p->pbm_err = e;
2046 		pbm_err_p->pbm_multi = PCIX_MULTI_ERR;
2047 		if (pcix_lookup_err_msgs(pci_p->pci_dip, ena,
2048 		    pcix_stat_errs_tbl, pbm_err_p) == DDI_FM_FATAL)
2049 			fatal++;
2050 		else
2051 			nonfatal++;
2052 	}
2053 
2054 	if (!fatal && !nonfatal)
2055 		return (DDI_FM_OK);
2056 	else if (fatal)
2057 		return (DDI_FM_FATAL);
2058 	return (DDI_FM_NONFATAL);
2059 }
2060 
2061 static pbm_fm_err_t pbm_err_tbl[] = {
2062 	PCI_MA,			SCHIZO_PCI_AFSR_E_MA,	PBM_PRIMARY,
2063 	FM_LOG_PCI,	PCI_TARG_MA,
2064 
2065 	PCI_SEC_MA,		SCHIZO_PCI_AFSR_E_MA,	PBM_SECONDARY,
2066 	FM_LOG_PBM,	NULL,
2067 
2068 	PCI_REC_TA,		SCHIZO_PCI_AFSR_E_TA,	PBM_PRIMARY,
2069 	FM_LOG_PCI,	PCI_TARG_REC_TA,
2070 
2071 	PCI_SEC_REC_TA,		SCHIZO_PCI_AFSR_E_TA,	PBM_SECONDARY,
2072 	FM_LOG_PBM,	NULL,
2073 
2074 	PCI_PBM_RETRY,		SCHIZO_PCI_AFSR_E_RTRY,	PBM_PRIMARY,
2075 	FM_LOG_PBM,	PCI_PBM_TARG_RETRY,
2076 
2077 	PCI_SEC_PBM_RETRY,	SCHIZO_PCI_AFSR_E_RTRY,	PBM_SECONDARY,
2078 	FM_LOG_PBM,	NULL,
2079 
2080 	PCI_MDPE,		SCHIZO_PCI_AFSR_E_PERR,	PBM_PRIMARY,
2081 	FM_LOG_PCI,	PCI_TARG_MDPE,
2082 
2083 	PCI_SEC_MDPE,		SCHIZO_PCI_AFSR_E_PERR,	PBM_SECONDARY,
2084 	FM_LOG_PBM,	NULL,
2085 
2086 	PCI_PBM_TTO,		SCHIZO_PCI_AFSR_E_TTO,	PBM_PRIMARY,
2087 	FM_LOG_PBM,	PCI_PBM_TARG_TTO,
2088 
2089 	PCI_SEC_PBM_TTO,	SCHIZO_PCI_AFSR_E_TTO,	PBM_SECONDARY,
2090 	FM_LOG_PBM,	NULL,
2091 
2092 	PCI_SCH_BUS_UNUSABLE_ERR, SCHIZO_PCI_AFSR_E_UNUSABLE, PBM_PRIMARY,
2093 	FM_LOG_PBM,	NULL,
2094 
2095 	PCI_SEC_SCH_BUS_UNUSABLE_ERR, SCHIZO_PCI_AFSR_E_UNUSABLE, PBM_SECONDARY,
2096 	FM_LOG_PBM,	NULL,
2097 
2098 	NULL,			NULL,			NULL,
2099 	NULL,		NULL,
2100 };
2101 
2102 
2103 /*
2104  * pci_pbm_classify, called by pbm_afsr_report to classify piow afsr.
2105  */
2106 int
2107 pci_pbm_classify(pbm_errstate_t *pbm_err_p)
2108 {
2109 	uint32_t err;
2110 	int nerr = 0;
2111 	int i;
2112 
2113 	err = pbm_err_p->pbm_pri ? PBM_AFSR_TO_PRIERR(pbm_err_p->pbm_afsr):
2114 		PBM_AFSR_TO_SECERR(pbm_err_p->pbm_afsr);
2115 
2116 	for (i = 0; pbm_err_tbl[i].pbm_err_class != NULL; i++) {
2117 		if ((err & pbm_err_tbl[i].pbm_reg_bit) &&
2118 		    (pbm_err_p->pbm_pri == pbm_err_tbl[i].pbm_pri)) {
2119 			if (pbm_err_tbl[i].pbm_flag == FM_LOG_PCI)
2120 				pbm_err_p->pbm_pci.pci_err_class =
2121 					pbm_err_tbl[i].pbm_err_class;
2122 			else
2123 				pbm_err_p->pbm_err_class =
2124 				    pbm_err_tbl[i].pbm_err_class;
2125 
2126 			pbm_err_p->pbm_terr_class =
2127 			    pbm_err_tbl[i].pbm_terr_class;
2128 			pbm_err_p->pbm_log = pbm_err_tbl[i].pbm_flag;
2129 			nerr++;
2130 			break;
2131 		}
2132 	}
2133 
2134 	return (nerr);
2135 }
2136 
2137 /*
2138  * Function used to handle and log IOMMU errors. Called by pci_pbm_err_handler,
2139  * with pci_fm_mutex held.
2140  */
2141 static int
2142 iommu_err_handler(dev_info_t *dip, uint64_t ena, pbm_errstate_t *pbm_err_p)
2143 {
2144 	pci_t *pci_p = get_pci_soft_state(ddi_get_instance(dip));
2145 	iommu_t *iommu_p = pci_p->pci_iommu_p;
2146 	ecc_t *ecc_p = pci_p->pci_ecc_p;
2147 	uint64_t stat;
2148 	ushort_t ta_signalled;
2149 	int err = 0;
2150 	int fatal = 0;
2151 	int nonfatal = 0;
2152 	int ret;
2153 
2154 	ASSERT(MUTEX_HELD(&ecc_p->ecc_pci_cmn_p->pci_fm_mutex));
2155 	if (!((stat = *iommu_p->iommu_ctrl_reg) & TOMATILLO_IOMMU_ERR)) {
2156 		pbm_err_p->pbm_err_class = PCI_SCH_MMU_ERR;
2157 		iommu_ereport_post(dip, ena, pbm_err_p);
2158 		return (DDI_FM_NONFATAL);
2159 	}
2160 
2161 	/*
2162 	 * Need to make sure a Target Abort was signalled to the device if
2163 	 * we have any hope of recovering. Tomatillo does not send a TA for
2164 	 * DMA Writes that result in a Translation Error, thus fooling the
2165 	 * device into believing everything is as it expects. Ignorance
2166 	 * is bliss, but knowledge is power.
2167 	 */
2168 	ta_signalled = pbm_err_p->pbm_pci.pci_cfg_stat &
2169 		PCI_STAT_S_TARG_AB;
2170 
2171 	if (stat & TOMATILLO_IOMMU_ERR_ILLTSBTBW) {
2172 		pbm_err_p->pbm_err_class = PCI_TOM_MMU_BAD_TSBTBW;
2173 		err = 1;
2174 		iommu_ereport_post(dip, ena, pbm_err_p);
2175 		if (!ta_signalled)
2176 			fatal++;
2177 		else
2178 			nonfatal++;
2179 	}
2180 
2181 	if (stat & TOMATILLO_IOMMU_ERR_BAD_VA) {
2182 		pbm_err_p->pbm_err_class = PCI_TOM_MMU_BAD_VA;
2183 		err = 1;
2184 		iommu_ereport_post(dip, ena, pbm_err_p);
2185 		if (!ta_signalled)
2186 			fatal++;
2187 		else
2188 			nonfatal++;
2189 	}
2190 
2191 	if (!err) {
2192 		stat = ((stat & TOMATILLO_IOMMU_ERRSTS) >>
2193 		    TOMATILLO_IOMMU_ERRSTS_SHIFT);
2194 		switch (stat) {
2195 		case TOMATILLO_IOMMU_PROTECTION_ERR:
2196 			pbm_err_p->pbm_err_class = PCI_TOM_MMU_PROT_ERR;
2197 			iommu_ereport_post(dip, ena, pbm_err_p);
2198 			fatal++;
2199 			break;
2200 		case TOMATILLO_IOMMU_INVALID_ERR:
2201 			pbm_err_p->pbm_err_class = PCI_TOM_MMU_INVAL_ERR;
2202 			/*
2203 			 * Fault the address in iommu_tfar
2204 			 * register to inform target driver of error
2205 			 */
2206 			ret = pci_handle_lookup(pci_p->pci_dip, DMA_HANDLE,
2207 				ena, (void *)&pbm_err_p->pbm_iommu.iommu_tfar);
2208 
2209 			if (ret == DDI_FM_NONFATAL)
2210 				if (ta_signalled)
2211 					nonfatal++;
2212 				else
2213 					fatal++;
2214 			else
2215 				fatal++;
2216 			iommu_ereport_post(dip, ena, pbm_err_p);
2217 			break;
2218 		case TOMATILLO_IOMMU_TIMEOUT_ERR:
2219 			pbm_err_p->pbm_err_class = PCI_TOM_MMU_TO_ERR;
2220 			fatal++;
2221 			iommu_ereport_post(dip, ena, pbm_err_p);
2222 			break;
2223 		case TOMATILLO_IOMMU_ECC_ERR:
2224 			pbm_err_p->pbm_err_class = PCI_TOM_MMU_UE;
2225 			iommu_ereport_post(dip, ena, pbm_err_p);
2226 			break;
2227 		}
2228 	}
2229 
2230 	if (fatal)
2231 		return (DDI_FM_FATAL);
2232 	else if (nonfatal)
2233 		return (DDI_FM_NONFATAL);
2234 
2235 	return (DDI_FM_OK);
2236 }
2237 
2238 int
2239 pci_check_error(pci_t *pci_p)
2240 {
2241 	pbm_t *pbm_p = pci_p->pci_pbm_p;
2242 	uint16_t pci_cfg_stat;
2243 	uint64_t pbm_ctl_stat, pbm_afsr, pbm_pcix_stat;
2244 	caddr_t a = pci_p->pci_address[0];
2245 	uint64_t *pbm_pcix_stat_reg;
2246 
2247 	ASSERT(MUTEX_HELD(&pci_p->pci_common_p->pci_fm_mutex));
2248 
2249 	pci_cfg_stat = pbm_p->pbm_config_header->ch_status_reg;
2250 	pbm_ctl_stat = *pbm_p->pbm_ctrl_reg;
2251 	pbm_afsr = *pbm_p->pbm_async_flt_status_reg;
2252 
2253 	if ((pci_cfg_stat & (PCI_STAT_S_PERROR | PCI_STAT_S_TARG_AB |
2254 				PCI_STAT_R_TARG_AB | PCI_STAT_R_MAST_AB |
2255 				PCI_STAT_S_SYSERR | PCI_STAT_PERROR)) ||
2256 			(pbm_ctl_stat & (SCHIZO_PCI_CTRL_BUS_UNUSABLE |
2257 				TOMATILLO_PCI_CTRL_PCI_DTO_ERR |
2258 				SCHIZO_PCI_CTRL_PCI_TTO_ERR |
2259 				SCHIZO_PCI_CTRL_PCI_RTRY_ERR |
2260 				SCHIZO_PCI_CTRL_PCI_MMU_ERR |
2261 				COMMON_PCI_CTRL_SBH_ERR |
2262 				COMMON_PCI_CTRL_SERR)) ||
2263 			(PBM_AFSR_TO_PRIERR(pbm_afsr)))
2264 		return (1);
2265 
2266 	if ((CHIP_TYPE(pci_p) == PCI_CHIP_XMITS) &&
2267 			(pbm_ctl_stat & XMITS_PCI_CTRL_X_MODE)) {
2268 
2269 		pbm_pcix_stat_reg = (uint64_t *)(a +
2270 		    XMITS_PCI_X_ERROR_STATUS_REG_OFFSET);
2271 
2272 		pbm_pcix_stat = *pbm_pcix_stat_reg;
2273 
2274 		if (PBM_PCIX_TO_PRIERR(pbm_pcix_stat))
2275 			return (1);
2276 
2277 		if (pbm_pcix_stat & XMITS_PCIX_STAT_PERR_RECOV_INT)
2278 			return (1);
2279 	}
2280 
2281 	return (0);
2282 
2283 }
2284 
2285 static pbm_fm_err_t pci_pbm_err_tbl[] = {
2286 	PCI_PBM_RETRY,			SCHIZO_PCI_CTRL_PCI_RTRY_ERR,
2287 	NULL,	PBM_NONFATAL,	PCI_PBM_TARG_RETRY,
2288 
2289 	PCI_PBM_TTO,			SCHIZO_PCI_CTRL_PCI_TTO_ERR,
2290 	NULL,	PBM_NONFATAL,	PCI_PBM_TARG_TTO,
2291 
2292 	PCI_SCH_BUS_UNUSABLE_ERR,	SCHIZO_PCI_CTRL_BUS_UNUSABLE,
2293 	NULL,	PBM_NONFATAL,	NULL,
2294 
2295 	NULL,				NULL,
2296 	NULL,	NULL,		NULL
2297 };
2298 
2299 /*
2300  * Function used to log all PCI/PBM/IOMMU errors found in the system.
2301  * It is called by the pbm_error_intr as well as the pci_err_callback(trap
2302  * callback). To protect access we hold the pci_fm_mutex when calling
2303  * this function.
2304  */
2305 int
2306 pci_pbm_err_handler(dev_info_t *dip, ddi_fm_error_t *derr,
2307 		const void *impl_data, int caller)
2308 {
2309 	int fatal = 0;
2310 	int nonfatal = 0;
2311 	int unknown = 0;
2312 	int rserr = 0;
2313 	uint32_t prierr, secerr;
2314 	pbm_errstate_t pbm_err;
2315 	char buf[FM_MAX_CLASS];
2316 	pci_t *pci_p = (pci_t *)impl_data;
2317 	pbm_t *pbm_p = pci_p->pci_pbm_p;
2318 	pci_target_err_t tgt_err;
2319 	int i, ret = 0;
2320 
2321 	ASSERT(MUTEX_HELD(&pci_p->pci_common_p->pci_fm_mutex));
2322 	pci_pbm_errstate_get(pci_p, &pbm_err);
2323 
2324 	derr->fme_ena = derr->fme_ena ? derr->fme_ena :
2325 	    fm_ena_generate(0, FM_ENA_FMT1);
2326 
2327 	prierr = PBM_AFSR_TO_PRIERR(pbm_err.pbm_afsr);
2328 	secerr = PBM_AFSR_TO_SECERR(pbm_err.pbm_afsr);
2329 
2330 	if (derr->fme_flag == DDI_FM_ERR_EXPECTED) {
2331 		if (caller == PCI_TRAP_CALL) {
2332 			/*
2333 			 * For ddi_caut_get treat all events as nonfatal.
2334 			 * The trampoline will set err_ena = 0, err_status =
2335 			 * NONFATAL. We only really call this function so that
2336 			 * pci_clear_error() and ndi_fm_handler_dispatch() will
2337 			 * get called.
2338 			 */
2339 			derr->fme_status = DDI_FM_NONFATAL;
2340 			nonfatal++;
2341 			goto done;
2342 		} else {
2343 			/*
2344 			 * For ddi_caut_put treat all events as nonfatal. Here
2345 			 * we have the handle and can call ndi_fm_acc_err_set().
2346 			 */
2347 			derr->fme_status = DDI_FM_NONFATAL;
2348 			ndi_fm_acc_err_set(pbm_p->pbm_excl_handle, derr);
2349 			nonfatal++;
2350 			goto done;
2351 		}
2352 	} else if (derr->fme_flag == DDI_FM_ERR_PEEK) {
2353 		/*
2354 		 * For ddi_peek treat all events as nonfatal. We only
2355 		 * really call this function so that pci_clear_error()
2356 		 * and ndi_fm_handler_dispatch() will get called.
2357 		 */
2358 		nonfatal++;
2359 		goto done;
2360 	} else if (derr->fme_flag == DDI_FM_ERR_POKE) {
2361 		/*
2362 		 * For ddi_poke we can treat as nonfatal if the
2363 		 * following conditions are met :
2364 		 * 1. Make sure only primary error is MA/TA
2365 		 * 2. Make sure no secondary error bits set
2366 		 * 3. check pci config header stat reg to see MA/TA is
2367 		 *    logged. We cannot verify only MA/TA is recorded
2368 		 *    since it gets much more complicated when a
2369 		 *    PCI-to-PCI bridge is present.
2370 		 */
2371 		if ((prierr == SCHIZO_PCI_AFSR_E_MA) && !secerr &&
2372 		    (pbm_err.pbm_pci.pci_cfg_stat & PCI_STAT_R_MAST_AB)) {
2373 			nonfatal++;
2374 			goto done;
2375 		} else if ((*pbm_p->pbm_ctrl_reg & XMITS_PCI_CTRL_X_MODE) &&
2376 		    pcix_ma_behind_bridge(&pbm_err)) {
2377 			/*
2378 			 * MAs behind a PCI-X bridge get sent back to
2379 			 * the host as a Split Completion Error Message.
2380 			 * We handle this the same as the above check.
2381 			 */
2382 			nonfatal++;
2383 			goto done;
2384 		}
2385 		if ((prierr == SCHIZO_PCI_AFSR_E_TA) && !secerr &&
2386 		    (pbm_err.pbm_pci.pci_cfg_stat & PCI_STAT_R_TARG_AB)) {
2387 			nonfatal++;
2388 			goto done;
2389 		}
2390 	}
2391 
2392 	DEBUG2(DBG_ERR_INTR, dip, "pci_pbm_err_handler: prierr=0x%x "
2393 	    "secerr=0x%x", prierr, secerr);
2394 
2395 	if (prierr || secerr) {
2396 		ret = pbm_afsr_report(dip, derr->fme_ena, &pbm_err);
2397 		if (ret == DDI_FM_FATAL)
2398 			fatal++;
2399 		else
2400 			nonfatal++;
2401 	}
2402 	if ((ret = pcix_log_pbm(pci_p, derr->fme_ena, &pbm_err))
2403 			== DDI_FM_FATAL)
2404 		fatal++;
2405 	else if (ret == DDI_FM_NONFATAL)
2406 		nonfatal++;
2407 
2408 	if ((ret = pci_cfg_report(dip, derr, &pbm_err.pbm_pci, caller, prierr))
2409 			== DDI_FM_FATAL)
2410 		fatal++;
2411 	else if (ret == DDI_FM_NONFATAL)
2412 		nonfatal++;
2413 
2414 	for (i = 0; pci_pbm_err_tbl[i].pbm_err_class != NULL; i++) {
2415 		if ((pbm_err.pbm_ctl_stat & pci_pbm_err_tbl[i].pbm_reg_bit) &&
2416 		    !prierr) {
2417 			pbm_err.pbm_err_class =
2418 				pci_pbm_err_tbl[i].pbm_err_class;
2419 			pbm_ereport_post(dip, derr->fme_ena, &pbm_err);
2420 			if (pci_pbm_err_tbl[i].pbm_flag)
2421 				fatal++;
2422 			else
2423 				nonfatal++;
2424 			if (caller == PCI_TRAP_CALL &&
2425 			    pci_pbm_err_tbl[i].pbm_terr_class) {
2426 				tgt_err.tgt_err_ena = derr->fme_ena;
2427 				tgt_err.tgt_err_class =
2428 				    pci_pbm_err_tbl[i].pbm_terr_class;
2429 				tgt_err.tgt_bridge_type =
2430 				    pbm_err.pbm_bridge_type;
2431 				tgt_err.tgt_err_addr =
2432 				    (uint64_t)derr->fme_bus_specific;
2433 				errorq_dispatch(pci_target_queue,
2434 				    (void *)&tgt_err, sizeof (pci_target_err_t),
2435 				    ERRORQ_ASYNC);
2436 			}
2437 		}
2438 	}
2439 
2440 	if ((pbm_err.pbm_ctl_stat & COMMON_PCI_CTRL_SBH_ERR) &&
2441 	    (CHIP_TYPE(pci_p) != PCI_CHIP_TOMATILLO)) {
2442 		pbm_err.pbm_err_class = PCI_SCH_SBH;
2443 		pbm_ereport_post(dip, derr->fme_ena, &pbm_err);
2444 		if (pci_panic_on_sbh_errors)
2445 			fatal++;
2446 		else
2447 			nonfatal++;
2448 	}
2449 
2450 	/*
2451 	 * PBM Received System Error - During any transaction, or
2452 	 * at any point on the bus, some device may detect a critical
2453 	 * error and signal a system error to the system.
2454 	 */
2455 	if (pbm_err.pbm_ctl_stat & COMMON_PCI_CTRL_SERR) {
2456 		/*
2457 		 * may be expected (master abort from pci-pci bridge during
2458 		 * poke will generate SERR)
2459 		 */
2460 		if (derr->fme_flag != DDI_FM_ERR_POKE) {
2461 			DEBUG1(DBG_ERR_INTR, dip, "pci_pbm_err_handler: "
2462 			    "ereport_post: %s", buf);
2463 			(void) snprintf(buf, FM_MAX_CLASS, "%s.%s",
2464 				PCI_ERROR_SUBCLASS, PCI_REC_SERR);
2465 			ddi_fm_ereport_post(dip, buf, derr->fme_ena,
2466 			    DDI_NOSLEEP, FM_VERSION, DATA_TYPE_UINT8, 0,
2467 			    PCI_CONFIG_STATUS, DATA_TYPE_UINT16,
2468 			    pbm_err.pbm_pci.pci_cfg_stat, PCI_CONFIG_COMMAND,
2469 			    DATA_TYPE_UINT16, pbm_err.pbm_pci.pci_cfg_comm,
2470 			    PCI_PA, DATA_TYPE_UINT64, (uint64_t)0, NULL);
2471 		}
2472 		rserr++;
2473 	}
2474 
2475 	/*
2476 	 * PCI Retry Timeout - Device fails to retry deferred
2477 	 * transaction within timeout. Only Tomatillo
2478 	 */
2479 	if (pbm_err.pbm_ctl_stat & TOMATILLO_PCI_CTRL_PCI_DTO_ERR) {
2480 		if (pci_dto_fault_warn == CE_PANIC)
2481 			fatal++;
2482 		else
2483 			nonfatal++;
2484 
2485 		(void) snprintf(buf, FM_MAX_CLASS, "%s.%s",
2486 			PCI_ERROR_SUBCLASS, PCI_DTO);
2487 		ddi_fm_ereport_post(dip, buf, derr->fme_ena, DDI_NOSLEEP,
2488 		    FM_VERSION, DATA_TYPE_UINT8, 0,
2489 		    PCI_CONFIG_STATUS, DATA_TYPE_UINT16,
2490 		    pbm_err.pbm_pci.pci_cfg_stat,
2491 		    PCI_CONFIG_COMMAND, DATA_TYPE_UINT16,
2492 		    pbm_err.pbm_pci.pci_cfg_comm,
2493 		    PCI_PA, DATA_TYPE_UINT64, (uint64_t)0, NULL);
2494 	}
2495 
2496 	/*
2497 	 * PBM Detected Data Parity Error - DPE detected during a DMA Write
2498 	 * or PIO Read. Later case is taken care of by cpu_deferred_error
2499 	 * and sent here to be logged.
2500 	 */
2501 	if ((pbm_err.pbm_pci.pci_cfg_stat & PCI_STAT_PERROR) &&
2502 			!(pbm_err.pbm_pci.pci_cfg_stat & PCI_STAT_S_SYSERR)) {
2503 		/*
2504 		 * If we have an address then fault
2505 		 * it, if not probe for errant device
2506 		 */
2507 		ret = DDI_FM_FATAL;
2508 		if (caller != PCI_TRAP_CALL) {
2509 			if (pbm_err.pbm_va_log)
2510 				ret = pci_handle_lookup(dip, DMA_HANDLE,
2511 						derr->fme_ena,
2512 						(void *)&pbm_err.pbm_va_log);
2513 			if (ret == DDI_FM_NONFATAL)
2514 				nonfatal++;
2515 			else
2516 				fatal++;
2517 		} else
2518 			nonfatal++;
2519 
2520 	}
2521 
2522 	/* PBM Detected IOMMU Error */
2523 	if (pbm_err.pbm_ctl_stat & SCHIZO_PCI_CTRL_PCI_MMU_ERR) {
2524 		if (iommu_err_handler(dip, derr->fme_ena, &pbm_err)
2525 				== DDI_FM_FATAL)
2526 			fatal++;
2527 		else
2528 			nonfatal++;
2529 	}
2530 
2531 done:
2532 	ret = ndi_fm_handler_dispatch(dip, NULL, derr);
2533 	if (ret == DDI_FM_FATAL) {
2534 		fatal++;
2535 	} else if (ret == DDI_FM_NONFATAL) {
2536 		nonfatal++;
2537 	} else if (ret == DDI_FM_UNKNOWN) {
2538 		unknown++;
2539 	}
2540 
2541 	/*
2542 	 * RSERR not claimed as nonfatal by a child is considered fatal
2543 	 */
2544 	if (rserr && ret != DDI_FM_NONFATAL)
2545 		fatal++;
2546 
2547 	/* Cleanup and reset error bits */
2548 	pci_clear_error(pci_p, &pbm_err);
2549 
2550 	return (fatal ? DDI_FM_FATAL : (nonfatal ? DDI_FM_NONFATAL :
2551 	    (unknown ? DDI_FM_UNKNOWN : DDI_FM_OK)));
2552 }
2553 
2554 /*
2555  * Function returns TRUE if a Primary error is Split Completion Error
2556  * that indicates a Master Abort occured behind a PCI-X bridge.
2557  * This function should only be called for busses running in PCI-X mode.
2558  */
2559 static int
2560 pcix_ma_behind_bridge(pbm_errstate_t *pbm_err_p)
2561 {
2562 	uint64_t msg;
2563 
2564 	if (pbm_err_p->pbm_afsr & XMITS_PCI_X_AFSR_S_SC_ERR)
2565 		return (0);
2566 
2567 	if (pbm_err_p->pbm_afsr & XMITS_PCI_X_AFSR_P_SC_ERR) {
2568 		msg = (pbm_err_p->pbm_afsr >> XMITS_PCI_X_P_MSG_SHIFT) &
2569 		    XMITS_PCIX_MSG_MASK;
2570 		if (msg & PCIX_CLASS_BRIDGE)
2571 			if (msg & PCIX_BRIDGE_MASTER_ABORT) {
2572 				return (1);
2573 			}
2574 	}
2575 
2576 	return (0);
2577 }
2578 
2579 /*
2580  * Function used to gather PBM/PCI/IOMMU error state for the
2581  * pci_pbm_err_handler and the cb_buserr_intr. This function must be
2582  * called while pci_fm_mutex is held.
2583  */
2584 static void
2585 pci_pbm_errstate_get(pci_t *pci_p, pbm_errstate_t *pbm_err_p)
2586 {
2587 	pbm_t *pbm_p = pci_p->pci_pbm_p;
2588 	iommu_t *iommu_p = pci_p->pci_iommu_p;
2589 	caddr_t a = pci_p->pci_address[0];
2590 	uint64_t *pbm_pcix_stat_reg;
2591 
2592 	ASSERT(MUTEX_HELD(&pci_p->pci_common_p->pci_fm_mutex));
2593 	bzero(pbm_err_p, sizeof (pbm_errstate_t));
2594 
2595 	/*
2596 	 * Capture all pbm error state for later logging
2597 	 */
2598 	pbm_err_p->pbm_bridge_type = PCI_BRIDGE_TYPE(pci_p->pci_common_p);
2599 
2600 	pbm_err_p->pbm_pci.pci_cfg_stat =
2601 		pbm_p->pbm_config_header->ch_status_reg;
2602 	pbm_err_p->pbm_ctl_stat = *pbm_p->pbm_ctrl_reg;
2603 	pbm_err_p->pbm_afsr = *pbm_p->pbm_async_flt_status_reg;
2604 	pbm_err_p->pbm_afar = *pbm_p->pbm_async_flt_addr_reg;
2605 	pbm_err_p->pbm_iommu.iommu_stat = *iommu_p->iommu_ctrl_reg;
2606 	pbm_err_p->pbm_pci.pci_cfg_comm =
2607 		pbm_p->pbm_config_header->ch_command_reg;
2608 	pbm_err_p->pbm_pci.pci_pa = *pbm_p->pbm_async_flt_addr_reg;
2609 
2610 	/*
2611 	 * Record errant slot for Xmits and Schizo
2612 	 * Not stored in Tomatillo
2613 	 */
2614 	if (CHIP_TYPE(pci_p) == PCI_CHIP_XMITS ||
2615 			CHIP_TYPE(pci_p) == PCI_CHIP_SCHIZO) {
2616 		pbm_err_p->pbm_err_sl = (pbm_err_p->pbm_ctl_stat &
2617 				SCHIZO_PCI_CTRL_ERR_SLOT) >>
2618 			SCHIZO_PCI_CTRL_ERR_SLOT_SHIFT;
2619 
2620 		/*
2621 		 * The bit 51 on XMITS rev1.0 is same as
2622 		 * SCHIZO_PCI_CTRL_ERR_SLOT_LOCK on schizo2.3. But
2623 		 * this bit needs to be cleared to be able to latch
2624 		 * the slot info on next fault.
2625 		 * But in XMITS Rev2.0, this bit indicates a DMA Write
2626 		 * Parity error.
2627 		 */
2628 		if (pbm_err_p->pbm_ctl_stat & XMITS_PCI_CTRL_DMA_WR_PERR) {
2629 			if ((PCI_CHIP_ID(pci_p) == XMITS_VER_10) ||
2630 				(PCI_CHIP_ID(pci_p) <= SCHIZO_VER_23)) {
2631 				/*
2632 				 * top 32 bits are W1C and we just want to
2633 				 * clear SLOT_LOCK. Leave bottom 32 bits
2634 				 * unchanged
2635 				 */
2636 				*pbm_p->pbm_ctrl_reg =
2637 					pbm_err_p->pbm_ctl_stat &
2638 					(SCHIZO_PCI_CTRL_ERR_SLOT_LOCK |
2639 					0xffffffff);
2640 				pbm_err_p->pbm_ctl_stat =
2641 					*pbm_p->pbm_ctrl_reg;
2642 			}
2643 		}
2644 	}
2645 
2646 	/*
2647 	 * Tomatillo specific registers
2648 	 */
2649 	if (CHIP_TYPE(pci_p) == PCI_CHIP_TOMATILLO) {
2650 		pbm_err_p->pbm_va_log = (uint64_t)va_to_pa(
2651 		    (void *)(uintptr_t)*(a + TOMATILLO_TGT_ERR_VALOG_OFFSET));
2652 		pbm_err_p->pbm_iommu.iommu_tfar = *iommu_p->iommu_tfar_reg;
2653 	}
2654 
2655 	/*
2656 	 * Xmits PCI-X register
2657 	 */
2658 	if ((CHIP_TYPE(pci_p) == PCI_CHIP_XMITS) &&
2659 			(pbm_err_p->pbm_ctl_stat & XMITS_PCI_CTRL_X_MODE)) {
2660 
2661 		pbm_pcix_stat_reg = (uint64_t *)(a +
2662 		    XMITS_PCI_X_ERROR_STATUS_REG_OFFSET);
2663 
2664 		pbm_err_p->pbm_pcix_stat = *pbm_pcix_stat_reg;
2665 		pbm_err_p->pbm_pcix_pfar = pbm_err_p->pbm_pcix_stat &
2666 				XMITS_PCI_X_STATUS_PFAR_MASK;
2667 	}
2668 }
2669 
2670 /*
2671  * Function used to clear PBM/PCI/IOMMU error state after error handling
2672  * is complete. Only clearing error bits which have been logged. Called by
2673  * pci_pbm_err_handler and pci_bus_exit.
2674  */
2675 static void
2676 pci_clear_error(pci_t *pci_p, pbm_errstate_t *pbm_err_p)
2677 {
2678 	pbm_t *pbm_p = pci_p->pci_pbm_p;
2679 	iommu_t *iommu_p = pci_p->pci_iommu_p;
2680 
2681 	ASSERT(MUTEX_HELD(&pbm_p->pbm_pci_p->pci_common_p->pci_fm_mutex));
2682 
2683 	if (*pbm_p->pbm_ctrl_reg & SCHIZO_PCI_CTRL_PCI_MMU_ERR) {
2684 		iommu_tlb_scrub(pci_p->pci_iommu_p, 1);
2685 	}
2686 	pbm_p->pbm_config_header->ch_status_reg =
2687 		pbm_err_p->pbm_pci.pci_cfg_stat;
2688 	*pbm_p->pbm_ctrl_reg = pbm_err_p->pbm_ctl_stat;
2689 	*pbm_p->pbm_async_flt_status_reg = pbm_err_p->pbm_afsr;
2690 	*iommu_p->iommu_ctrl_reg = pbm_err_p->pbm_iommu.iommu_stat;
2691 }
2692 
2693 void
2694 pbm_clear_error(pbm_t *pbm_p)
2695 {
2696 	uint64_t pbm_afsr, pbm_ctl_stat;
2697 
2698 	/*
2699 	 * for poke() support - called from POKE_FLUSH. Spin waiting
2700 	 * for MA, TA or SERR to be cleared by a pbm_error_intr().
2701 	 * We have to wait for SERR too in case the device is beyond
2702 	 * a pci-pci bridge.
2703 	 */
2704 	pbm_ctl_stat = *pbm_p->pbm_ctrl_reg;
2705 	pbm_afsr = *pbm_p->pbm_async_flt_status_reg;
2706 	while (((pbm_afsr >> SCHIZO_PCI_AFSR_PE_SHIFT) &
2707 	    (SCHIZO_PCI_AFSR_E_MA | SCHIZO_PCI_AFSR_E_TA)) ||
2708 	    (pbm_ctl_stat & COMMON_PCI_CTRL_SERR)) {
2709 		pbm_ctl_stat = *pbm_p->pbm_ctrl_reg;
2710 		pbm_afsr = *pbm_p->pbm_async_flt_status_reg;
2711 	}
2712 }
2713 
2714 /*
2715  * Function used to convert the 32 bit captured PCI error address
2716  * to the full Safari or Jbus address. This is so we can look this address
2717  * up in our handle caches.
2718  */
2719 void
2720 pci_format_addr(dev_info_t *dip, uint64_t *afar, uint64_t afsr)
2721 {
2722 	pci_t *pci_p = get_pci_soft_state(ddi_get_instance(dip));
2723 	pci_ranges_t *io_range, *mem_range;
2724 	uint64_t err_pa = 0;
2725 
2726 	if (afsr & SCHIZO_PCI_AFSR_CONF_SPACE) {
2727 		err_pa |= pci_p->pci_ranges->parent_high;
2728 		err_pa = err_pa << 32;
2729 		err_pa |= pci_p->pci_ranges->parent_low;
2730 	} else if (afsr & SCHIZO_PCI_AFSR_IO_SPACE) {
2731 		io_range = pci_p->pci_ranges + 1;
2732 		err_pa |= io_range->parent_high;
2733 		err_pa = err_pa << 32;
2734 		err_pa |= io_range->parent_low;
2735 	} else if (afsr & SCHIZO_PCI_AFSR_MEM_SPACE) {
2736 		mem_range = pci_p->pci_ranges + 2;
2737 		err_pa |= mem_range->parent_high;
2738 		err_pa = err_pa << 32;
2739 		err_pa |= mem_range->parent_low;
2740 	}
2741 	*afar |= err_pa;
2742 }
2743 
2744 static ecc_format_t ecc_format_tbl[] = {
2745 	SCH_REG_UPA,		NULL,				NULL,
2746 	SCH_REG_PCIA_REG,	SCHIZO_PCI_AFSR_CONF_SPACE,	PCI_SIDEA,
2747 	SCH_REG_PCIA_MEM,	SCHIZO_PCI_AFSR_MEM_SPACE,	PCI_SIDEA,
2748 	SCH_REG_PCIA_CFGIO,	SCHIZO_PCI_AFSR_IO_SPACE,	PCI_SIDEA,
2749 	SCH_REG_PCIB_REG,	SCHIZO_PCI_AFSR_CONF_SPACE,	PCI_SIDEB,
2750 	SCH_REG_PCIB_MEM,	SCHIZO_PCI_AFSR_MEM_SPACE,	PCI_SIDEB,
2751 	SCH_REG_PCIB_CFGIO,	SCHIZO_PCI_AFSR_IO_SPACE,	PCI_SIDEB,
2752 	SCH_REG_SAFARI_REGS,	NULL,				NULL,
2753 	NULL,			NULL,				NULL,
2754 };
2755 
2756 /*
2757  * Function used to convert the 32 bit PIO address captured for a
2758  * Safari Bus UE(during PIO Rd/Wr) to a full Safari Bus Address.
2759  */
2760 static void
2761 pci_format_ecc_addr(dev_info_t *dip, uint64_t *afar, ecc_region_t region)
2762 {
2763 	pci_t *pci_p = get_pci_soft_state(ddi_get_instance(dip));
2764 	pci_common_t *cmn_p = pci_p->pci_common_p;
2765 	cb_t *cb_p = pci_p->pci_cb_p;
2766 	int i, pci_side = 0;
2767 	int swap = 0;
2768 	uint64_t pa = cb_p->cb_base_pa;
2769 	uint64_t flag, schizo_base, pci_csr_base;
2770 
2771 	if (pci_p == NULL)
2772 		return;
2773 
2774 	pci_csr_base = va_to_pa(pci_p->pci_address[0]);
2775 
2776 	/*
2777 	 * Using the csr_base address to determine which side
2778 	 * we are on.
2779 	 */
2780 	if (pci_csr_base & PCI_SIDE_ADDR_MASK)
2781 		pci_side = 1;
2782 	else
2783 		pci_side = 0;
2784 
2785 	schizo_base = pa - PBM_CTRL_OFFSET;
2786 
2787 	for (i = 0; ecc_format_tbl[i].ecc_region != NULL; i++) {
2788 		if (region == ecc_format_tbl[i].ecc_region) {
2789 			flag = ecc_format_tbl[i].ecc_space;
2790 			if (ecc_format_tbl[i].ecc_side != pci_side)
2791 				swap = 1;
2792 			if (region == SCH_REG_SAFARI_REGS)
2793 				*afar |= schizo_base;
2794 			break;
2795 		}
2796 	}
2797 
2798 	if (swap) {
2799 		pci_p = cmn_p->pci_p[PCI_OTHER_SIDE(pci_p->pci_side)];
2800 
2801 		if (pci_p == NULL)
2802 			return;
2803 	}
2804 	pci_format_addr(pci_p->pci_dip, afar, flag);
2805 }
2806 
2807 /*
2808  * Function used to post control block specific ereports.
2809  */
2810 static void
2811 cb_ereport_post(dev_info_t *dip, uint64_t ena, cb_errstate_t *cb_err)
2812 {
2813 	pci_t *pci_p = get_pci_soft_state(ddi_get_instance(dip));
2814 	char buf[FM_MAX_CLASS], dev_path[MAXPATHLEN], *ptr;
2815 	struct i_ddi_fmhdl *fmhdl = DEVI(dip)->devi_fmhdl;
2816 	nvlist_t *ereport, *detector;
2817 	errorq_elem_t *eqep;
2818 	nv_alloc_t *nva;
2819 
2820 	DEBUG1(DBG_ATTACH, dip, "cb_ereport_post: elog 0x%lx",
2821 	    cb_err->cb_elog);
2822 
2823 	/*
2824 	 * We do not use ddi_fm_ereport_post because we need to set a
2825 	 * special detector here. Since we do not have a device path for
2826 	 * the bridge chip we use what we think it should be to aid in
2827 	 * diagnosis.
2828 	 */
2829 	(void) snprintf(buf, FM_MAX_CLASS, "%s.%s.%s", DDI_IO_CLASS,
2830 	    cb_err->cb_bridge_type, cb_err->cb_err_class);
2831 
2832 	ena = ena ? ena : fm_ena_generate(0, FM_ENA_FMT1);
2833 
2834 	eqep = errorq_reserve(fmhdl->fh_errorq);
2835 	if (eqep == NULL)
2836 		return;
2837 
2838 	ereport = errorq_elem_nvl(fmhdl->fh_errorq, eqep);
2839 	nva = errorq_elem_nva(fmhdl->fh_errorq, eqep);
2840 	detector = fm_nvlist_create(nva);
2841 
2842 	ASSERT(ereport);
2843 	ASSERT(nva);
2844 	ASSERT(detector);
2845 
2846 	ddi_pathname(dip, dev_path);
2847 	ptr = strrchr(dev_path, (int)',');
2848 
2849 	if (ptr)
2850 		*ptr = '\0';
2851 
2852 	fm_fmri_dev_set(detector, FM_DEV_SCHEME_VERSION, NULL, dev_path, NULL);
2853 
2854 	DEBUG1(DBG_ERR_INTR, dip, "cb_ereport_post: ereport_set: %s", buf);
2855 
2856 	if (CHIP_TYPE(pci_p) == PCI_CHIP_SCHIZO ||
2857 	    CHIP_TYPE(pci_p) == PCI_CHIP_XMITS) {
2858 		fm_ereport_set(ereport, FM_EREPORT_VERSION, buf, ena, detector,
2859 		    SAFARI_CSR, DATA_TYPE_UINT64, cb_err->cb_csr,
2860 		    SAFARI_ERR, DATA_TYPE_UINT64, cb_err->cb_err,
2861 		    SAFARI_INTR, DATA_TYPE_UINT64, cb_err->cb_intr,
2862 		    SAFARI_ELOG, DATA_TYPE_UINT64, cb_err->cb_elog,
2863 		    SAFARI_PCR, DATA_TYPE_UINT64, cb_err->cb_pcr,
2864 		    NULL);
2865 	} else if (CHIP_TYPE(pci_p) == PCI_CHIP_TOMATILLO) {
2866 		fm_ereport_set(ereport, FM_EREPORT_VERSION, buf, ena, detector,
2867 		    JBUS_CSR, DATA_TYPE_UINT64, cb_err->cb_csr,
2868 		    JBUS_ERR, DATA_TYPE_UINT64, cb_err->cb_err,
2869 		    JBUS_INTR, DATA_TYPE_UINT64, cb_err->cb_intr,
2870 		    JBUS_ELOG, DATA_TYPE_UINT64, cb_err->cb_elog,
2871 		    JBUS_PCR, DATA_TYPE_UINT64, cb_err->cb_pcr,
2872 		    NULL);
2873 	}
2874 	errorq_commit(fmhdl->fh_errorq, eqep, ERRORQ_ASYNC);
2875 }
2876 
2877 /*
2878  * Function used to post IOMMU specific ereports.
2879  */
2880 static void
2881 iommu_ereport_post(dev_info_t *dip, uint64_t ena, pbm_errstate_t *pbm_err)
2882 {
2883 	char buf[FM_MAX_CLASS];
2884 
2885 	(void) snprintf(buf, FM_MAX_CLASS, "%s.%s",
2886 		    pbm_err->pbm_bridge_type, pbm_err->pbm_err_class);
2887 
2888 	ena = ena ? ena : fm_ena_generate(0, FM_ENA_FMT1);
2889 
2890 	DEBUG1(DBG_ERR_INTR, dip, "iommu_ereport_post: ereport_set: %s", buf);
2891 
2892 	ddi_fm_ereport_post(dip, buf, ena, DDI_NOSLEEP,
2893 	    FM_VERSION, DATA_TYPE_UINT8, 0,
2894 	    PCI_CONFIG_STATUS, DATA_TYPE_UINT16, pbm_err->pbm_pci.pci_cfg_stat,
2895 	    PCI_CONFIG_COMMAND, DATA_TYPE_UINT16, pbm_err->pbm_pci.pci_cfg_comm,
2896 	    PCI_PBM_CSR, DATA_TYPE_UINT64, pbm_err->pbm_ctl_stat,
2897 	    PCI_PBM_IOMMU_CTRL, DATA_TYPE_UINT64, pbm_err->pbm_iommu.iommu_stat,
2898 	    PCI_PBM_IOMMU_TFAR, DATA_TYPE_UINT64, pbm_err->pbm_iommu.iommu_tfar,
2899 	    PCI_PBM_SLOT, DATA_TYPE_UINT64, pbm_err->pbm_err_sl,
2900 	    PCI_PBM_VALOG, DATA_TYPE_UINT64, pbm_err->pbm_va_log,
2901 	    NULL);
2902 }
2903 
2904 /*
2905  * Function used to post PCI-X generic ereports.
2906  * This function needs to be fixed once the Fault Boundary Analysis
2907  * for PCI-X is conducted. The payload should be made more generic.
2908  */
2909 static void
2910 pcix_ereport_post(dev_info_t *dip, uint64_t ena, pbm_errstate_t *pbm_err)
2911 {
2912 	char buf[FM_MAX_CLASS];
2913 
2914 	ena = ena ? ena : fm_ena_generate(0, FM_ENA_FMT1);
2915 
2916 	DEBUG1(DBG_ERR_INTR, dip, "pcix_ereport_post: ereport_post: %s", buf);
2917 
2918 	ddi_fm_ereport_post(dip, pbm_err->pbm_err_class, ena, DDI_NOSLEEP,
2919 	    FM_VERSION, DATA_TYPE_UINT8, 0,
2920 	    PCI_CONFIG_STATUS, DATA_TYPE_UINT16, pbm_err->pbm_pci.pci_cfg_stat,
2921 	    PCI_CONFIG_COMMAND, DATA_TYPE_UINT16, pbm_err->pbm_pci.pci_cfg_comm,
2922 	    PCI_PBM_CSR, DATA_TYPE_UINT64, pbm_err->pbm_ctl_stat,
2923 	    PCI_PBM_AFSR, DATA_TYPE_UINT64, pbm_err->pbm_afsr,
2924 	    PCI_PBM_AFAR, DATA_TYPE_UINT64, pbm_err->pbm_afar,
2925 	    PCI_PBM_SLOT, DATA_TYPE_UINT64, pbm_err->pbm_err_sl,
2926 	    PCIX_STAT, DATA_TYPE_UINT64, pbm_err->pbm_pcix_stat,
2927 	    PCIX_PFAR, DATA_TYPE_UINT32, pbm_err->pbm_pcix_pfar,
2928 	    NULL);
2929 }
2930 
2931 static void
2932 iommu_ctx_free(iommu_t *iommu_p)
2933 {
2934 	kmem_free(iommu_p->iommu_ctx_bitmap, IOMMU_CTX_BITMAP_SIZE);
2935 }
2936 
2937 /*
2938  * iommu_tlb_scrub():
2939  *	Exam TLB entries through TLB diagnostic registers and look for errors.
2940  *	scrub = 1 : cleanup all error bits in tlb, called in FAULT_RESET case
2941  *	scrub = 0 : log all error conditions to console, FAULT_LOG case
2942  *	In both cases, it returns number of errors found in tlb entries.
2943  */
2944 static int
2945 iommu_tlb_scrub(iommu_t *iommu_p, int scrub)
2946 {
2947 	int i, nerr = 0;
2948 	dev_info_t *dip = iommu_p->iommu_pci_p->pci_dip;
2949 	char *neg = "not ";
2950 
2951 	uint64_t base = (uint64_t)iommu_p->iommu_ctrl_reg -
2952 		COMMON_IOMMU_CTRL_REG_OFFSET;
2953 
2954 	volatile uint64_t *tlb_tag = (volatile uint64_t *)
2955 		(base + COMMON_IOMMU_TLB_TAG_DIAG_ACC_OFFSET);
2956 	volatile uint64_t *tlb_data = (volatile uint64_t *)
2957 		(base + COMMON_IOMMU_TLB_DATA_DIAG_ACC_OFFSET);
2958 	for (i = 0; i < IOMMU_TLB_ENTRIES; i++) {
2959 		uint64_t tag = tlb_tag[i];
2960 		uint64_t data = tlb_data[i];
2961 		uint32_t errstat;
2962 		iopfn_t pfn;
2963 
2964 		if (!(tag & TLBTAG_ERR_BIT))
2965 			continue;
2966 
2967 		pfn = (iopfn_t)(data & TLBDATA_MEMPA_BITS);
2968 		errstat = (uint32_t)
2969 			((tag & TLBTAG_ERRSTAT_BITS) >> TLBTAG_ERRSTAT_SHIFT);
2970 		if (errstat == TLBTAG_ERRSTAT_INVALID) {
2971 			if (scrub)
2972 				tlb_tag[i] = tlb_data[i] = 0ull;
2973 		} else
2974 			nerr++;
2975 
2976 		if (scrub)
2977 			continue;
2978 
2979 		cmn_err(CE_CONT, "%s%d: Error %x on IOMMU TLB entry %x:\n"
2980 		"\tContext=%lx %sWritable %sStreamable\n"
2981 		"\tPCI Page Size=%sk Address in page %lx\n",
2982 			ddi_driver_name(dip), ddi_get_instance(dip), errstat, i,
2983 			(tag & TLBTAG_CONTEXT_BITS) >> TLBTAG_CONTEXT_SHIFT,
2984 			(tag & TLBTAG_WRITABLE_BIT) ? "" : neg,
2985 			(tag & TLBTAG_STREAM_BIT) ? "" : neg,
2986 			(tag & TLBTAG_PGSIZE_BIT) ? "64" : "8",
2987 			(tag & TLBTAG_PCIVPN_BITS) << 13);
2988 		cmn_err(CE_CONT, "Memory: %sValid %sCacheable Page Frame=%lx\n",
2989 			(data & TLBDATA_VALID_BIT) ? "" : neg,
2990 			(data & TLBDATA_CACHE_BIT) ? "" : neg, pfn);
2991 	}
2992 	return (nerr);
2993 }
2994 
2995 /*
2996  * pci_iommu_disp: calculates the displacement needed in tomatillo's
2997  *	iommu control register and modifies the control value template
2998  *	from caller. It also clears any error status bit that are new
2999  *	in tomatillo.
3000  * return value: an 8-bit mask to enable corresponding 512 MB segments
3001  *	suitable for tomatillo's target address register.
3002  *	0x00: no programming is needed, use existing value from prom
3003  *	0x60: use segment 5 and 6 to form a 1GB dvma range
3004  */
3005 static uint64_t
3006 pci_iommu_disp(iommu_t *iommu_p, uint64_t *ctl_p)
3007 {
3008 	uint64_t ctl_old;
3009 	if (CHIP_TYPE(iommu_p->iommu_pci_p) != PCI_CHIP_TOMATILLO)
3010 		return (0);
3011 
3012 	ctl_old = *iommu_p->iommu_ctrl_reg;
3013 	/* iommu ctrl reg error bits are W1C */
3014 	if (ctl_old >> TOMATIILO_IOMMU_ERR_REG_SHIFT) {
3015 		cmn_err(CE_WARN, "Tomatillo iommu err: %lx", ctl_old);
3016 		*ctl_p |= (ctl_old >> TOMATIILO_IOMMU_ERR_REG_SHIFT)
3017 		    << TOMATIILO_IOMMU_ERR_REG_SHIFT;
3018 	}
3019 
3020 	if (iommu_p->iommu_tsb_size != TOMATILLO_IOMMU_TSB_MAX)
3021 		return (0);
3022 
3023 	/* Tomatillo 2.0 and later, and 1GB DVMA range */
3024 	*ctl_p |= 1 << TOMATILLO_IOMMU_SEG_DISP_SHIFT;
3025 	return (3 << (iommu_p->iommu_dvma_base >> (32 - 3)));
3026 }
3027 
3028 void
3029 pci_iommu_config(iommu_t *iommu_p, uint64_t iommu_ctl, uint64_t cfgpa)
3030 {
3031 	uintptr_t pbm_regbase = get_pbm_reg_base(iommu_p->iommu_pci_p);
3032 	volatile uint64_t *pbm_csr_p = (volatile uint64_t *)pbm_regbase;
3033 	volatile uint64_t *tgt_space_p = (volatile uint64_t *)(pbm_regbase |
3034 		(TOMATILLO_TGT_ADDR_SPACE_OFFSET - SCHIZO_PCI_CTRL_REG_OFFSET));
3035 	volatile uint64_t pbm_ctl = *pbm_csr_p;
3036 
3037 	volatile uint64_t *iommu_ctl_p = iommu_p->iommu_ctrl_reg;
3038 	volatile uint64_t tsb_bar_val = iommu_p->iommu_tsb_paddr;
3039 	volatile uint64_t *tsb_bar_p = iommu_p->iommu_tsb_base_addr_reg;
3040 	uint64_t mask = pci_iommu_disp(iommu_p, &iommu_ctl);
3041 
3042 	DEBUG2(DBG_ATTACH, iommu_p->iommu_pci_p->pci_dip,
3043 		"\npci_iommu_config: pbm_csr_p=%llx pbm_ctl=%llx",
3044 		pbm_csr_p, pbm_ctl);
3045 	DEBUG2(DBG_ATTACH|DBG_CONT, iommu_p->iommu_pci_p->pci_dip,
3046 		"\n\tiommu_ctl_p=%llx iommu_ctl=%llx",
3047 		iommu_ctl_p, iommu_ctl);
3048 	DEBUG4(DBG_ATTACH|DBG_CONT, iommu_p->iommu_pci_p->pci_dip,
3049 		"\n\tcfgpa=%llx tgt_space_p=%llx mask=%x tsb=%llx\n",
3050 		cfgpa, tgt_space_p, mask, tsb_bar_val);
3051 
3052 	if (!cfgpa)
3053 		goto reprog;
3054 
3055 	/* disable PBM arbiters - turn off bits 0-7 */
3056 	*pbm_csr_p = (pbm_ctl >> 8) << 8;
3057 
3058 	/*
3059 	 * For non-XMITS, flush any previous writes. This is only
3060 	 * necessary for host bridges that may have a USB keywboard
3061 	 * attached.  XMITS does not.
3062 	 */
3063 	if (!(CHIP_TYPE(iommu_p->iommu_pci_p) == PCI_CHIP_XMITS))
3064 		(void) ldphysio(cfgpa);
3065 
3066 reprog:
3067 	if (mask)
3068 		*tgt_space_p = mask;
3069 
3070 	*tsb_bar_p = tsb_bar_val;
3071 	*iommu_ctl_p = iommu_ctl;
3072 
3073 	*pbm_csr_p = pbm_ctl;	/* re-enable bus arbitration */
3074 	pbm_ctl = *pbm_csr_p;	/* flush all prev writes */
3075 }
3076 
3077 
3078 int
3079 pci_get_portid(dev_info_t *dip)
3080 {
3081 	return (ddi_getprop(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
3082 	    "portid", -1));
3083 }
3084 
3085 /*
3086  * Schizo Safari Performance Events.
3087  */
3088 pci_kev_mask_t
3089 schizo_saf_events[] = {
3090 	{"saf_bus_cycles", 0x1},	{"saf_pause_asserted_cycles", 0x2},
3091 	{"saf_frn_coherent_cmds", 0x3},	{"saf_frn_coherent_hits", 0x4},
3092 	{"saf_my_coherent_cmds", 0x5},	{"saf_my_coherent_hits", 0x6},
3093 	{"saf_frn_io_cmds", 0x7}, 	{"saf_frn_io_hits", 0x8},
3094 	{"merge_buffer", 0x9}, 		{"interrupts", 0xa},
3095 	{"csr_pios", 0xc}, 		{"upa_pios", 0xd},
3096 	{"pcia_pios", 0xe}, 		{"pcib_pios", 0xf},
3097 	{"saf_pause_seen_cycles", 0x11}, 	{"dvma_reads", 0x12},
3098 	{"dvma_writes", 0x13},		{"saf_orq_full_cycles", 0x14},
3099 	{"saf_data_in_cycles", 0x15},	{"saf_data_out_cycles", 0x16},
3100 	{"clear_pic", 0x1f}
3101 };
3102 
3103 
3104 /*
3105  * Schizo PCI Performance Events.
3106  */
3107 pci_kev_mask_t
3108 schizo_pci_events[] = {
3109 	{"dvma_stream_rd", 0x0}, 	{"dvma_stream_wr", 0x1},
3110 	{"dvma_const_rd", 0x2},		{"dvma_const_wr", 0x3},
3111 	{"dvma_stream_buf_mis", 0x4},	{"dvma_cycles", 0x5},
3112 	{"dvma_wd_xfr", 0x6},		{"pio_cycles", 0x7},
3113 	{"dvma_tlb_misses", 0x10},	{"interrupts", 0x11},
3114 	{"saf_inter_nack", 0x12},	{"pio_reads", 0x13},
3115 	{"pio_writes", 0x14},		{"dvma_rd_buf_timeout", 0x15},
3116 	{"dvma_rd_rtry_stc", 0x16},	{"dvma_wr_rtry_stc", 0x17},
3117 	{"dvma_rd_rtry_nonstc", 0x18},	{"dvma_wr_rtry_nonstc", 0x19},
3118 	{"E*_slow_transitions", 0x1a},	{"E*_slow_cycles_per_64", 0x1b},
3119 	{"clear_pic", 0x1f}
3120 };
3121 
3122 
3123 /*
3124  * Create the picN kstats for the pci
3125  * and safari events.
3126  */
3127 void
3128 pci_kstat_init()
3129 {
3130 	pci_name_kstat = (pci_ksinfo_t *)kmem_alloc(sizeof (pci_ksinfo_t),
3131 		KM_NOSLEEP);
3132 
3133 	if (pci_name_kstat == NULL) {
3134 		cmn_err(CE_WARN, "pcisch : no space for kstat\n");
3135 	} else {
3136 		pci_name_kstat->pic_no_evs =
3137 			sizeof (schizo_pci_events) / sizeof (pci_kev_mask_t);
3138 		pci_name_kstat->pic_shift[0] = SCHIZO_SHIFT_PIC0;
3139 		pci_name_kstat->pic_shift[1] = SCHIZO_SHIFT_PIC1;
3140 		pci_create_name_kstat("pcis",
3141 			pci_name_kstat, schizo_pci_events);
3142 	}
3143 
3144 	saf_name_kstat = (pci_ksinfo_t *)kmem_alloc(sizeof (pci_ksinfo_t),
3145 		KM_NOSLEEP);
3146 	if (saf_name_kstat == NULL) {
3147 		cmn_err(CE_WARN, "pcisch : no space for kstat\n");
3148 	} else {
3149 		saf_name_kstat->pic_no_evs =
3150 			sizeof (schizo_saf_events) / sizeof (pci_kev_mask_t);
3151 		saf_name_kstat->pic_shift[0] = SCHIZO_SHIFT_PIC0;
3152 		saf_name_kstat->pic_shift[1] = SCHIZO_SHIFT_PIC1;
3153 		pci_create_name_kstat("saf", saf_name_kstat, schizo_saf_events);
3154 	}
3155 }
3156 
3157 void
3158 pci_kstat_fini()
3159 {
3160 	if (pci_name_kstat != NULL) {
3161 		pci_delete_name_kstat(pci_name_kstat);
3162 		kmem_free(pci_name_kstat, sizeof (pci_ksinfo_t));
3163 		pci_name_kstat = NULL;
3164 	}
3165 
3166 	if (saf_name_kstat != NULL) {
3167 		pci_delete_name_kstat(saf_name_kstat);
3168 		kmem_free(saf_name_kstat, sizeof (pci_ksinfo_t));
3169 		saf_name_kstat = NULL;
3170 	}
3171 }
3172 
3173 /*
3174  * Create 'counters' kstat for pci events.
3175  */
3176 void
3177 pci_add_pci_kstat(pci_t *pci_p)
3178 {
3179 	pci_cntr_addr_t *cntr_addr_p = &pci_p->pci_ks_addr;
3180 	uintptr_t regbase = (uintptr_t)pci_p->pci_address[0];
3181 
3182 	cntr_addr_p->pcr_addr = (uint64_t *)
3183 		(regbase + SCHIZO_PERF_PCI_PCR_OFFSET);
3184 	cntr_addr_p->pic_addr = (uint64_t *)
3185 		(regbase + SCHIZO_PERF_PCI_PIC_OFFSET);
3186 
3187 	pci_p->pci_ksp = pci_create_cntr_kstat(pci_p, "pcis",
3188 		NUM_OF_PICS, pci_cntr_kstat_update, cntr_addr_p);
3189 
3190 	if (pci_p->pci_ksp == NULL) {
3191 		cmn_err(CE_WARN, "pcisch : cannot create counter kstat");
3192 	}
3193 }
3194 
3195 void
3196 pci_rem_pci_kstat(pci_t *pci_p)
3197 {
3198 	if (pci_p->pci_ksp != NULL)
3199 		kstat_delete(pci_p->pci_ksp);
3200 	pci_p->pci_ksp = NULL;
3201 }
3202 
3203 void
3204 pci_add_upstream_kstat(pci_t *pci_p)
3205 {
3206 	pci_common_t	*cmn_p = pci_p->pci_common_p;
3207 	pci_cntr_pa_t	*cntr_pa_p = &cmn_p->pci_cmn_uks_pa;
3208 	uint64_t regbase = va_to_pa(pci_p->pci_address[1]);
3209 
3210 	cntr_pa_p->pcr_pa =
3211 		regbase + SCHIZO_PERF_SAF_PCR_OFFSET;
3212 	cntr_pa_p->pic_pa =
3213 		regbase + SCHIZO_PERF_SAF_PIC_OFFSET;
3214 
3215 	cmn_p->pci_common_uksp = pci_create_cntr_kstat(pci_p, "saf",
3216 		NUM_OF_PICS, pci_cntr_kstat_pa_update, cntr_pa_p);
3217 }
3218 
3219 /*
3220  * Extract the drivers binding name to identify which chip
3221  * we're binding to.  Whenever a new bus bridge is created, the driver alias
3222  * entry should be added here to identify the device if needed.  If a device
3223  * isn't added, the identity defaults to PCI_CHIP_UNIDENTIFIED.
3224  */
3225 static uint32_t
3226 pci_identity_init(pci_t *pci_p)
3227 {
3228 	dev_info_t *dip = pci_p->pci_dip;
3229 	char *name = ddi_binding_name(dip);
3230 	uint32_t ver = ddi_prop_get_int(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
3231 		"version#", 0);
3232 
3233 	if (strcmp(name, "pci108e,a801") == 0)
3234 		return (CHIP_ID(PCI_CHIP_TOMATILLO, ver, 0x00));
3235 
3236 	if (strcmp(name, "pci108e,8001") == 0)
3237 		return (CHIP_ID(PCI_CHIP_SCHIZO, ver, 0x00));
3238 
3239 	if (strcmp(name, "pci108e,8002") == 0) {
3240 		uint32_t mod_rev = ddi_prop_get_int(DDI_DEV_T_ANY, dip,
3241 			DDI_PROP_DONTPASS, "module-revision#", 0);
3242 		return (CHIP_ID(PCI_CHIP_XMITS, ver, mod_rev));
3243 	}
3244 
3245 	cmn_err(CE_WARN, "%s%d: Unknown PCI Host bridge %s %x\n",
3246 		ddi_driver_name(dip), ddi_get_instance(dip), name, ver);
3247 
3248 	return (PCI_CHIP_UNIDENTIFIED);
3249 }
3250 
3251 /*
3252  * Setup a physical pointer to one leaf config space area. This
3253  * is used in several places in order to do a dummy read which
3254  * guarantees the nexus (and not a bus master) has gained control
3255  * of the bus.
3256  */
3257 static void
3258 pci_setup_cfgpa(pci_t *pci_p)
3259 {
3260 	dev_info_t *dip = pci_p->pci_dip;
3261 	dev_info_t *cdip;
3262 	pbm_t *pbm_p = pci_p->pci_pbm_p;
3263 	uint64_t cfgpa = pci_get_cfg_pabase(pci_p);
3264 	uint32_t *reg_p;
3265 	int reg_len;
3266 
3267 	for (cdip = ddi_get_child(dip); cdip != NULL;
3268 	    cdip = ddi_get_next_sibling(cdip)) {
3269 		if (ddi_getlongprop(DDI_DEV_T_ANY, cdip, DDI_PROP_DONTPASS,
3270 		    "reg", (caddr_t)&reg_p, &reg_len) != DDI_PROP_SUCCESS)
3271 			continue;
3272 		cfgpa += (*reg_p) & (PCI_CONF_ADDR_MASK ^ PCI_REG_REG_M);
3273 		kmem_free(reg_p, reg_len);
3274 		break;
3275 	}
3276 	pbm_p->pbm_anychild_cfgpa = cfgpa;
3277 }
3278 
3279 void
3280 pci_post_init_child(pci_t *pci_p, dev_info_t *child)
3281 {
3282 	volatile uint64_t *ctrl_reg_p;
3283 	pbm_t *pbm_p = pci_p->pci_pbm_p;
3284 
3285 	pci_setup_cfgpa(pci_p);
3286 
3287 	/*
3288 	 * This is a hack for skyhawk/casinni combination to address
3289 	 * hardware problems between the request and grant signals which
3290 	 * causes a bus hang.  One workaround, which is applied here,
3291 	 * is to disable bus parking if the child contains the property
3292 	 * pci-req-removal.  Note that if the bus is quiesced we must mask
3293 	 * off the parking bit in the saved control registers, since the
3294 	 * quiesce operation temporarily turns off PCI bus parking.
3295 	 */
3296 	if (ddi_prop_exists(DDI_DEV_T_ANY, child, DDI_PROP_DONTPASS,
3297 		"pci-req-removal") == 1) {
3298 
3299 		if (pbm_p->pbm_quiesce_count > 0) {
3300 			pbm_p->pbm_saved_ctrl_reg &= ~SCHIZO_PCI_CTRL_ARB_PARK;
3301 		} else {
3302 			ctrl_reg_p = pbm_p->pbm_ctrl_reg;
3303 			*ctrl_reg_p &= ~SCHIZO_PCI_CTRL_ARB_PARK;
3304 		}
3305 	}
3306 
3307 	if (CHIP_TYPE(pci_p) == PCI_CHIP_XMITS) {
3308 		if (*pbm_p->pbm_ctrl_reg & XMITS_PCI_CTRL_X_MODE) {
3309 			int value;
3310 
3311 			/*
3312 			 * Due to a XMITS bug, we need to set the outstanding
3313 			 * split transactions to 1 for all PCI-X functions
3314 			 * behind the leaf.
3315 			 */
3316 			value = (xmits_max_transactions << 4) |
3317 			    (xmits_max_read_bytes << 2);
3318 
3319 			DEBUG1(DBG_INIT_CLD, child, "Turning on XMITS NCPQ "
3320 			    "Workaround: value = %x\n", value);
3321 
3322 			pcix_set_cmd_reg(child, value);
3323 
3324 			(void) ndi_prop_update_int(DDI_DEV_T_NONE,
3325 			    child, "pcix-update-cmd-reg", value);
3326 		}
3327 	}
3328 }
3329 
3330 void
3331 pci_post_uninit_child(pci_t *pci_p)
3332 {
3333 	pci_setup_cfgpa(pci_p);
3334 }
3335 
3336 static int
3337 pci_tom_nbintr_op(pci_t *pci_p, uint32_t inum, intrfunc f, caddr_t arg,
3338     int flag)
3339 {
3340 	uint32_t ino = pci_p->pci_inos[inum];
3341 	uint32_t mondo = IB_INO_TO_NBMONDO(pci_p->pci_ib_p, ino);
3342 	int ret = DDI_SUCCESS;
3343 
3344 	mondo = CB_MONDO_TO_XMONDO(pci_p->pci_cb_p, mondo); /* no op on tom */
3345 
3346 	switch (flag) {
3347 	case PCI_OBJ_INTR_ADD:
3348 		VERIFY(add_ivintr(mondo, pci_pil[inum], f, arg, NULL) == 0);
3349 		break;
3350 	case PCI_OBJ_INTR_REMOVE:
3351 		rem_ivintr(mondo, NULL);
3352 		break;
3353 	default:
3354 		ret = DDI_FAILURE;
3355 		break;
3356 	}
3357 
3358 	return (ret);
3359 }
3360 
3361 int
3362 pci_ecc_add_intr(pci_t *pci_p, int inum, ecc_intr_info_t *eii_p)
3363 {
3364 	uint32_t mondo;
3365 	int	r;
3366 
3367 	mondo = ((pci_p->pci_cb_p->cb_ign << PCI_INO_BITS) |
3368 	    pci_p->pci_inos[inum]);
3369 	mondo = CB_MONDO_TO_XMONDO(pci_p->pci_cb_p, mondo);
3370 
3371 	VERIFY(add_ivintr(mondo, pci_pil[inum], ecc_intr,
3372 	    (caddr_t)eii_p, NULL) == 0);
3373 
3374 	if (CHIP_TYPE(pci_p) != PCI_CHIP_TOMATILLO)
3375 		return (PCI_ATTACH_RETCODE(PCI_ECC_OBJ, PCI_OBJ_INTR_ADD,
3376 		    DDI_SUCCESS));
3377 
3378 	r = pci_tom_nbintr_op(pci_p, inum, ecc_intr,
3379 	    (caddr_t)eii_p, PCI_OBJ_INTR_ADD);
3380 	return (PCI_ATTACH_RETCODE(PCI_ECC_OBJ, PCI_OBJ_INTR_ADD, r));
3381 }
3382 
3383 void
3384 pci_ecc_rem_intr(pci_t *pci_p, int inum, ecc_intr_info_t *eii_p)
3385 {
3386 	uint32_t mondo;
3387 
3388 	mondo = ((pci_p->pci_cb_p->cb_ign << PCI_INO_BITS) |
3389 	    pci_p->pci_inos[inum]);
3390 	mondo = CB_MONDO_TO_XMONDO(pci_p->pci_cb_p, mondo);
3391 
3392 	rem_ivintr(mondo, NULL);
3393 
3394 	if (CHIP_TYPE(pci_p) == PCI_CHIP_TOMATILLO)
3395 		pci_tom_nbintr_op(pci_p, inum, ecc_intr,
3396 			(caddr_t)eii_p, PCI_OBJ_INTR_REMOVE);
3397 }
3398 
3399 static uint_t
3400 pci_pbm_cdma_intr(caddr_t a)
3401 {
3402 	pbm_t *pbm_p = (pbm_t *)a;
3403 	pbm_p->pbm_cdma_flag = PBM_CDMA_DONE;
3404 #ifdef PBM_CDMA_DEBUG
3405 	pbm_p->pbm_cdma_intr_cnt++;
3406 #endif /* PBM_CDMA_DEBUG */
3407 	return (DDI_INTR_CLAIMED);
3408 }
3409 
3410 int
3411 pci_pbm_add_intr(pci_t *pci_p)
3412 {
3413 	uint32_t mondo;
3414 
3415 	mondo = IB_INO_TO_MONDO(pci_p->pci_ib_p, pci_p->pci_inos[CBNINTR_CDMA]);
3416 	mondo = CB_MONDO_TO_XMONDO(pci_p->pci_cb_p, mondo);
3417 
3418 	VERIFY(add_ivintr(mondo, pci_pil[CBNINTR_CDMA],
3419 	    pci_pbm_cdma_intr, (caddr_t)pci_p->pci_pbm_p, NULL) == 0);
3420 
3421 	return (DDI_SUCCESS);
3422 }
3423 
3424 void
3425 pci_pbm_rem_intr(pci_t *pci_p)
3426 {
3427 	ib_t		*ib_p = pci_p->pci_ib_p;
3428 	uint32_t	mondo;
3429 
3430 	mondo = IB_INO_TO_MONDO(pci_p->pci_ib_p, pci_p->pci_inos[CBNINTR_CDMA]);
3431 	mondo = CB_MONDO_TO_XMONDO(pci_p->pci_cb_p, mondo);
3432 
3433 	ib_intr_disable(ib_p, pci_p->pci_inos[CBNINTR_CDMA], IB_INTR_NOWAIT);
3434 	rem_ivintr(mondo, NULL);
3435 }
3436 
3437 void
3438 pci_pbm_suspend(pci_t *pci_p)
3439 {
3440 	pbm_t		*pbm_p = pci_p->pci_pbm_p;
3441 	ib_ino_t	ino = pci_p->pci_inos[CBNINTR_CDMA];
3442 
3443 	/* Save CDMA interrupt state */
3444 	pbm_p->pbm_cdma_imr_save = *ib_intr_map_reg_addr(pci_p->pci_ib_p, ino);
3445 }
3446 
3447 void
3448 pci_pbm_resume(pci_t *pci_p)
3449 {
3450 	pbm_t		*pbm_p = pci_p->pci_pbm_p;
3451 	ib_ino_t	ino = pci_p->pci_inos[CBNINTR_CDMA];
3452 
3453 	/* Restore CDMA interrupt state */
3454 	*ib_intr_map_reg_addr(pci_p->pci_ib_p, ino) = pbm_p->pbm_cdma_imr_save;
3455 }
3456 
3457 /*
3458  * pci_bus_quiesce
3459  *
3460  * This function is called as the corresponding control ops routine
3461  * to a DDI_CTLOPS_QUIESCE command.  Its mission is to halt all DMA
3462  * activity on the bus by disabling arbitration/parking.
3463  */
3464 int
3465 pci_bus_quiesce(pci_t *pci_p, dev_info_t *dip, void *result)
3466 {
3467 	volatile uint64_t *ctrl_reg_p;
3468 	volatile uint64_t ctrl_reg;
3469 	pbm_t *pbm_p;
3470 
3471 	pbm_p = pci_p->pci_pbm_p;
3472 	ctrl_reg_p = pbm_p->pbm_ctrl_reg;
3473 
3474 	if (pbm_p->pbm_quiesce_count++ == 0) {
3475 
3476 		DEBUG0(DBG_PWR, dip, "quiescing bus\n");
3477 
3478 		ctrl_reg = *ctrl_reg_p;
3479 		pbm_p->pbm_saved_ctrl_reg = ctrl_reg;
3480 		ctrl_reg &= ~(SCHIZO_PCI_CTRL_ARB_EN_MASK |
3481 		    SCHIZO_PCI_CTRL_ARB_PARK);
3482 		*ctrl_reg_p = ctrl_reg;
3483 #ifdef	DEBUG
3484 		ctrl_reg = *ctrl_reg_p;
3485 		if ((ctrl_reg & (SCHIZO_PCI_CTRL_ARB_EN_MASK |
3486 		    SCHIZO_PCI_CTRL_ARB_PARK)) != 0)
3487 			panic("ctrl_reg didn't quiesce: 0x%lx\n", ctrl_reg);
3488 #endif
3489 		if (pbm_p->pbm_anychild_cfgpa)
3490 			(void) ldphysio(pbm_p->pbm_anychild_cfgpa);
3491 	}
3492 
3493 	return (DDI_SUCCESS);
3494 }
3495 
3496 /*
3497  * pci_bus_unquiesce
3498  *
3499  * This function is called as the corresponding control ops routine
3500  * to a DDI_CTLOPS_UNQUIESCE command.  Its mission is to resume paused
3501  * DMA activity on the bus by re-enabling arbitration (and maybe parking).
3502  */
3503 int
3504 pci_bus_unquiesce(pci_t *pci_p, dev_info_t *dip, void *result)
3505 {
3506 	volatile uint64_t *ctrl_reg_p;
3507 	pbm_t *pbm_p;
3508 #ifdef	DEBUG
3509 	volatile uint64_t ctrl_reg;
3510 #endif
3511 
3512 	pbm_p = pci_p->pci_pbm_p;
3513 	ctrl_reg_p = pbm_p->pbm_ctrl_reg;
3514 
3515 	ASSERT(pbm_p->pbm_quiesce_count > 0);
3516 	if (--pbm_p->pbm_quiesce_count == 0) {
3517 		*ctrl_reg_p = pbm_p->pbm_saved_ctrl_reg;
3518 #ifdef	DEBUG
3519 		ctrl_reg = *ctrl_reg_p;
3520 		if ((ctrl_reg & (SCHIZO_PCI_CTRL_ARB_EN_MASK |
3521 		    SCHIZO_PCI_CTRL_ARB_PARK)) == 0)
3522 			panic("ctrl_reg didn't unquiesce: 0x%lx\n", ctrl_reg);
3523 #endif
3524 	}
3525 
3526 	return (DDI_SUCCESS);
3527 }
3528 
3529 static void
3530 tm_vmem_free(ddi_dma_impl_t *mp, iommu_t *iommu_p, dvma_addr_t dvma_pg,
3531 	int npages)
3532 {
3533 	uint32_t dur_max, dur_base;
3534 	dvma_unbind_req_t *req_p, *req_max_p;
3535 	dvma_unbind_req_t *req_base_p = iommu_p->iommu_mtlb_req_p;
3536 	uint32_t tlb_vpn[IOMMU_TLB_ENTRIES];
3537 	caddr_t reg_base;
3538 	volatile uint64_t *tag_p;
3539 	int i, preserv_count = 0;
3540 
3541 	mutex_enter(&iommu_p->iommu_mtlb_lock);
3542 
3543 	iommu_p->iommu_mtlb_npgs += npages;
3544 	req_max_p = req_base_p + iommu_p->iommu_mtlb_nreq++;
3545 	req_max_p->dur_npg = npages;
3546 	req_max_p->dur_base = dvma_pg;
3547 	req_max_p->dur_flags = mp->dmai_flags & DMAI_FLAGS_VMEMCACHE;
3548 
3549 
3550 	if (iommu_p->iommu_mtlb_npgs <= iommu_p->iommu_mtlb_maxpgs)
3551 		goto done;
3552 
3553 	/* read TLB */
3554 	reg_base = iommu_p->iommu_pci_p->pci_address[0];
3555 	tag_p = (volatile uint64_t *)
3556 	    (reg_base + COMMON_IOMMU_TLB_TAG_DIAG_ACC_OFFSET);
3557 
3558 	for (i = 0; i < IOMMU_TLB_ENTRIES; i++)
3559 		tlb_vpn[i] = tag_p[i] & SCHIZO_VPN_MASK;
3560 
3561 	/* for each request search the TLB for a matching address */
3562 	for (req_p = req_base_p; req_p <= req_max_p; req_p++) {
3563 		dur_base = req_p->dur_base;
3564 		dur_max = req_p->dur_base + req_p->dur_npg;
3565 
3566 		for (i = 0; i < IOMMU_TLB_ENTRIES; i++) {
3567 			uint_t vpn = tlb_vpn[i];
3568 			if (vpn >= dur_base && vpn < dur_max)
3569 				break;
3570 		}
3571 		if (i >= IOMMU_TLB_ENTRIES) {
3572 			pci_vmem_do_free(iommu_p,
3573 			    (void *)IOMMU_PTOB(req_p->dur_base),
3574 			    req_p->dur_npg, req_p->dur_flags);
3575 			iommu_p->iommu_mtlb_npgs -= req_p->dur_npg;
3576 			continue;
3577 		}
3578 		/* if an empty slot exists */
3579 		if ((req_p - req_base_p) != preserv_count)
3580 			*(req_base_p + preserv_count) = *req_p;
3581 		preserv_count++;
3582 	}
3583 
3584 	iommu_p->iommu_mtlb_nreq = preserv_count;
3585 done:
3586 	mutex_exit(&iommu_p->iommu_mtlb_lock);
3587 }
3588 
3589 void
3590 pci_vmem_free(iommu_t *iommu_p, ddi_dma_impl_t *mp, void *dvma_addr,
3591     size_t npages)
3592 {
3593 	if (tm_mtlb_gc)
3594 		tm_vmem_free(mp, iommu_p,
3595 		    (dvma_addr_t)IOMMU_BTOP((dvma_addr_t)dvma_addr), npages);
3596 	else
3597 		pci_vmem_do_free(iommu_p, dvma_addr, npages,
3598 		    (mp->dmai_flags & DMAI_FLAGS_VMEMCACHE));
3599 }
3600