3 * This file is subject to the terms and conditions of the GNU General Public
4 * License. See the file "COPYING" in the main directory of this archive
7 * Copyright (C) 2001-2003 Silicon Graphics, Inc. All rights reserved.
10 #include <linux/types.h>
11 #include <linux/slab.h>
12 #include <linux/module.h>
13 #include <asm/sn/sgi.h>
14 #include <asm/sn/sn_cpuid.h>
15 #include <asm/sn/addrs.h>
16 #include <asm/sn/arch.h>
17 #include <asm/sn/iograph.h>
18 #include <asm/sn/invent.h>
19 #include <asm/sn/hcl.h>
20 #include <asm/sn/labelcl.h>
21 #include <asm/sn/xtalk/xwidget.h>
22 #include <asm/sn/pci/bridge.h>
23 #include <asm/sn/pci/pciio.h>
24 #include <asm/sn/pci/pcibr.h>
25 #include <asm/sn/pci/pcibr_private.h>
26 #include <asm/sn/pci/pci_defs.h>
27 #include <asm/sn/prio.h>
28 #include <asm/sn/xtalk/xbow.h>
29 #include <asm/sn/ioc3.h>
30 #include <asm/sn/io.h>
31 #include <asm/sn/sn_private.h>
35 compare_and_swap_ptr(void **location, void *old_ptr, void *new_ptr)
37 /* FIXME - compare_and_swap_ptr NOT ATOMIC */
38 if (*location == old_ptr) {
47 unsigned pcibr_intr_bits(pciio_info_t info, pciio_intr_line_t lines, int nslots);
48 pcibr_intr_t pcibr_intr_alloc(vertex_hdl_t, device_desc_t, pciio_intr_line_t, vertex_hdl_t);
49 void pcibr_intr_free(pcibr_intr_t);
50 void pcibr_setpciint(xtalk_intr_t);
51 int pcibr_intr_connect(pcibr_intr_t, intr_func_t, intr_arg_t);
52 void pcibr_intr_disconnect(pcibr_intr_t);
54 vertex_hdl_t pcibr_intr_cpu_get(pcibr_intr_t);
55 void pcibr_xintr_preset(void *, int, xwidgetnum_t, iopaddr_t, xtalk_intr_vector_t);
56 void pcibr_intr_func(intr_arg_t);
58 extern pcibr_info_t pcibr_info_get(vertex_hdl_t);
60 /* =====================================================================
61 * INTERRUPT MANAGEMENT
65 pcibr_intr_bits(pciio_info_t info,
66 pciio_intr_line_t lines, int nslots)
68 pciio_slot_t slot = PCIBR_INFO_SLOT_GET_INT(info);
72 * Currently favored mapping from PCI
73 * slot number and INTA/B/C/D to Bridge
74 * PCI Interrupt Bit Number:
88 if (lines & (PCIIO_INTR_LINE_A| PCIIO_INTR_LINE_C))
90 if (lines & (PCIIO_INTR_LINE_B| PCIIO_INTR_LINE_D))
91 bbits |= 1 << (slot ^ 4);
98 * Get the next wrapper pointer queued in the interrupt circular buffer.
101 pcibr_wrap_get(pcibr_intr_cbuf_t cbuf)
103 pcibr_intr_wrap_t wrap;
105 if (cbuf->ib_in == cbuf->ib_out)
106 panic( "pcibr intr circular buffer empty, cbuf=0x%p, ib_in=ib_out=%d\n",
107 (void *)cbuf, cbuf->ib_out);
109 wrap = cbuf->ib_cbuf[cbuf->ib_out++];
110 cbuf->ib_out = cbuf->ib_out % IBUFSIZE;
115 * Queue a wrapper pointer in the interrupt circular buffer.
118 pcibr_wrap_put(pcibr_intr_wrap_t wrap, pcibr_intr_cbuf_t cbuf)
123 * Multiple CPUs could be executing this code simultaneously
124 * if a handler has registered multiple interrupt lines and
125 * the interrupts are directed to different CPUs.
127 spin_lock(&cbuf->ib_lock);
128 in = (cbuf->ib_in + 1) % IBUFSIZE;
129 if (in == cbuf->ib_out)
130 panic( "pcibr intr circular buffer full, cbuf=0x%p, ib_in=%d\n",
131 (void *)cbuf, cbuf->ib_in);
133 cbuf->ib_cbuf[cbuf->ib_in] = wrap;
135 spin_unlock(&cbuf->ib_lock);
140 * On SN systems there is a race condition between a PIO read response
141 * and DMA's. In rare cases, the read response may beat the DMA, causing
142 * the driver to think that data in memory is complete and meaningful.
143 * This code eliminates that race.
144 * This routine is called by the PIO read routines after doing the read.
145 * This routine then forces a fake interrupt on another line, which
146 * is logically associated with the slot that the PIO is addressed to.
147 * (see sn_dma_flush_init() )
148 * It then spins while watching the memory location that the interrupt
149 * is targetted to. When the interrupt response arrives, we are sure
150 * that the DMA has landed in memory and it is safe for the driver
154 extern struct sn_flush_nasid_entry flush_nasid_list[MAX_NASIDS];
157 sn_dma_flush(unsigned long addr) {
160 volatile struct sn_flush_device_list *p;
165 nasid = NASID_GET(addr);
166 wid_num = SWIN_WIDGETNUM(addr);
167 bwin = BWIN_WINDOWNUM(addr);
169 if (flush_nasid_list[nasid].widget_p == NULL) return;
174 wid_num = ((flush_nasid_list[nasid].iio_itte1) >> 8) & 0xf;
177 wid_num = ((flush_nasid_list[nasid].iio_itte2) >> 8) & 0xf;
180 wid_num = ((flush_nasid_list[nasid].iio_itte3) >> 8) & 0xf;
183 wid_num = ((flush_nasid_list[nasid].iio_itte4) >> 8) & 0xf;
186 wid_num = ((flush_nasid_list[nasid].iio_itte5) >> 8) & 0xf;
189 wid_num = ((flush_nasid_list[nasid].iio_itte6) >> 8) & 0xf;
192 wid_num = ((flush_nasid_list[nasid].iio_itte7) >> 8) & 0xf;
196 if (flush_nasid_list[nasid].widget_p == NULL) return;
197 if (flush_nasid_list[nasid].widget_p[wid_num] == NULL) return;
198 p = &flush_nasid_list[nasid].widget_p[wid_num][0];
200 // find a matching BAR
202 for (i=0; i<DEV_PER_WIDGET;i++) {
203 for (j=0; j<PCI_ROM_RESOURCE;j++) {
204 if (p->bar_list[j].start == 0) break;
205 if (addr >= p->bar_list[j].start && addr <= p->bar_list[j].end) break;
207 if (j < PCI_ROM_RESOURCE && p->bar_list[j].start != 0) break;
211 // if no matching BAR, return without doing anything.
213 if (i == DEV_PER_WIDGET) return;
215 spin_lock_irqsave(&p->flush_lock, flags);
219 // force an interrupt.
221 *(bridgereg_t *)(p->force_int_addr) = 1;
223 // wait for the interrupt to come back.
225 while (p->flush_addr != 0x10f);
227 // okay, everything is synched up.
228 spin_unlock_irqrestore(&p->flush_lock, flags);
233 EXPORT_SYMBOL(sn_dma_flush);
236 * There are end cases where a deadlock can occur if interrupt
237 * processing completes and the Bridge b_int_status bit is still set.
239 * One scenerio is if a second PCI interrupt occurs within 60ns of
240 * the previous interrupt being cleared. In this case the Bridge
241 * does not detect the transition, the Bridge b_int_status bit
242 * remains set, and because no transition was detected no interrupt
243 * packet is sent to the Hub/Heart.
245 * A second scenerio is possible when a b_int_status bit is being
246 * shared by multiple devices:
247 * Device #1 generates interrupt
248 * Bridge b_int_status bit set
249 * Device #2 generates interrupt
250 * interrupt processing begins
251 * ISR for device #1 runs and
253 * Device #1 generates interrupt
254 * ISR for device #2 runs and
256 * (b_int_status bit still set)
257 * interrupt processing completes
259 * Interrupt processing is now complete, but an interrupt is still
260 * outstanding for Device #1. But because there was no transition of
261 * the b_int_status bit, no interrupt packet will be generated and
262 * a deadlock will occur.
264 * To avoid these deadlock situations, this function is used
265 * to check if a specific Bridge b_int_status bit is set, and if so,
266 * cause the setting of the corresponding interrupt bit.
268 * On a XBridge (SN1) and PIC (SN2), we do this by writing the appropriate Bridge Force
269 * Interrupt register.
272 pcibr_force_interrupt(pcibr_intr_t intr)
276 pcibr_soft_t pcibr_soft = intr->bi_soft;
277 bridge_t *bridge = pcibr_soft->bs_base;
279 bits = intr->bi_ibits;
280 for (bit = 0; bit < 8; bit++) {
281 if (bits & (1 << bit)) {
283 PCIBR_DEBUG((PCIBR_DEBUG_INTR, pcibr_soft->bs_vhdl,
284 "pcibr_force_interrupt: bit=0x%x\n", bit));
286 if (IS_XBRIDGE_OR_PIC_SOFT(pcibr_soft)) {
287 bridge->b_force_pin[bit].intr = 1;
295 pcibr_intr_alloc(vertex_hdl_t pconn_vhdl,
296 device_desc_t dev_desc,
297 pciio_intr_line_t lines,
298 vertex_hdl_t owner_dev)
300 pcibr_info_t pcibr_info = pcibr_info_get(pconn_vhdl);
301 pciio_slot_t pciio_slot = PCIBR_INFO_SLOT_GET_INT(pcibr_info);
302 pcibr_soft_t pcibr_soft = (pcibr_soft_t) pcibr_info->f_mfast;
303 vertex_hdl_t xconn_vhdl = pcibr_soft->bs_conn;
304 bridge_t *bridge = pcibr_soft->bs_base;
307 xtalk_intr_t *xtalk_intr_p;
308 pcibr_intr_t *pcibr_intr_p;
309 pcibr_intr_list_t *intr_list_p;
311 unsigned pcibr_int_bits;
312 unsigned pcibr_int_bit;
313 xtalk_intr_t xtalk_intr = (xtalk_intr_t)0;
315 pcibr_intr_t pcibr_intr;
316 pcibr_intr_list_t intr_entry;
317 pcibr_intr_list_t intr_list;
321 PCIBR_DEBUG_ALWAYS((PCIBR_DEBUG_INTR_ALLOC, pconn_vhdl,
322 "pcibr_intr_alloc: %s%s%s%s%s\n",
323 !(lines & 15) ? " No INTs?" : "",
324 lines & 1 ? " INTA" : "",
325 lines & 2 ? " INTB" : "",
326 lines & 4 ? " INTC" : "",
327 lines & 8 ? " INTD" : ""));
333 pcibr_intr->bi_dev = pconn_vhdl;
334 pcibr_intr->bi_lines = lines;
335 pcibr_intr->bi_soft = pcibr_soft;
336 pcibr_intr->bi_ibits = 0; /* bits will be added below */
337 pcibr_intr->bi_func = 0; /* unset until connect */
338 pcibr_intr->bi_arg = 0; /* unset until connect */
339 pcibr_intr->bi_flags = is_threaded ? 0 : PCIIO_INTR_NOTHREAD;
340 pcibr_intr->bi_mustruncpu = CPU_NONE;
341 pcibr_intr->bi_ibuf.ib_in = 0;
342 pcibr_intr->bi_ibuf.ib_out = 0;
343 spin_lock_init(&pcibr_intr->bi_ibuf.ib_lock);
344 pcibr_int_bits = pcibr_soft->bs_intr_bits((pciio_info_t)pcibr_info, lines,
345 PCIBR_NUM_SLOTS(pcibr_soft));
349 * For each PCI interrupt line requested, figure
350 * out which Bridge PCI Interrupt Line it maps
351 * to, and make sure there are xtalk resources
354 PCIBR_DEBUG_ALWAYS((PCIBR_DEBUG_INTR_ALLOC, pconn_vhdl,
355 "pcibr_intr_alloc: pcibr_int_bits: 0x%x\n", pcibr_int_bits));
356 for (pcibr_int_bit = 0; pcibr_int_bit < 8; pcibr_int_bit ++) {
357 if (pcibr_int_bits & (1 << pcibr_int_bit)) {
358 xtalk_intr_p = &pcibr_soft->bs_intr[pcibr_int_bit].bsi_xtalk_intr;
360 xtalk_intr = *xtalk_intr_p;
362 if (xtalk_intr == NULL) {
364 * This xtalk_intr_alloc is constrained for two reasons:
365 * 1) Normal interrupts and error interrupts need to be delivered
366 * through a single xtalk target widget so that there aren't any
367 * ordering problems with DMA, completion interrupts, and error
368 * interrupts. (Use of xconn_vhdl forces this.)
370 * 2) On SN1, addressing constraints on SN1 and Bridge force
371 * us to use a single PI number for all interrupts from a
372 * single Bridge. (SN1-specific code forces this).
376 * All code dealing with threaded PCI interrupt handlers
377 * is located at the pcibr level. Because of this,
378 * we always want the lower layers (hub/heart_intr_alloc,
379 * intr_level_connect) to treat us as non-threaded so we
380 * don't set up a duplicate threaded environment. We make
381 * this happen by calling a special xtalk interface.
383 xtalk_intr = xtalk_intr_alloc_nothd(xconn_vhdl, dev_desc,
386 PCIBR_DEBUG_ALWAYS((PCIBR_DEBUG_INTR_ALLOC, pconn_vhdl,
387 "pcibr_intr_alloc: xtalk_intr=0x%x\n", xtalk_intr));
389 /* both an assert and a runtime check on this:
390 * we need to check in non-DEBUG kernels, and
391 * the ASSERT gets us more information when
392 * we use DEBUG kernels.
394 ASSERT(xtalk_intr != NULL);
395 if (xtalk_intr == NULL) {
396 /* it is quite possible that our
397 * xtalk_intr_alloc failed because
398 * someone else got there first,
399 * and we can find their results
402 if (!*xtalk_intr_p) {
403 #ifdef SUPPORT_PRINTING_V_FORMAT
405 "pcibr_intr_alloc %v: unable to get xtalk interrupt resources",
409 "pcibr_intr_alloc 0x%p: unable to get xtalk interrupt resources",
412 /* yes, we leak resources here. */
415 } else if (compare_and_swap_ptr((void **) xtalk_intr_p, NULL, xtalk_intr)) {
417 * now tell the bridge which slot is
418 * using this interrupt line.
420 int_dev = bridge->b_int_device;
421 int_dev &= ~BRIDGE_INT_DEV_MASK(pcibr_int_bit);
422 int_dev |= pciio_slot << BRIDGE_INT_DEV_SHFT(pcibr_int_bit);
423 bridge->b_int_device = int_dev; /* XXXMP */
425 PCIBR_DEBUG_ALWAYS((PCIBR_DEBUG_INTR_ALLOC, pconn_vhdl,
426 "bridge intr bit %d clears my wrb\n",
429 /* someone else got one allocated first;
430 * free the one we just created, and
431 * retrieve the one they allocated.
433 xtalk_intr_free(xtalk_intr);
434 xtalk_intr = *xtalk_intr_p;
436 /* once xtalk_intr is set, we never clear it,
437 * so if the CAS fails above, this condition
438 * can "never happen" ...
442 "pcibr_intr_alloc %v: unable to set xtalk interrupt resources",
444 /* yes, we leak resources here. */
451 pcibr_intr->bi_ibits |= 1 << pcibr_int_bit;
454 intr_entry->il_next = NULL;
455 intr_entry->il_intr = pcibr_intr;
456 intr_entry->il_wrbf = &(bridge->b_wr_req_buf[pciio_slot].reg);
458 &pcibr_soft->bs_intr[pcibr_int_bit].bsi_pcibr_intr_wrap.iw_list;
460 PCIBR_DEBUG_ALWAYS((PCIBR_DEBUG_INTR_ALLOC, pconn_vhdl,
461 "Bridge bit 0x%x wrap=0x%x\n", pcibr_int_bit,
462 pcibr_soft->bs_intr[pcibr_int_bit].bsi_pcibr_intr_wrap));
464 if (compare_and_swap_ptr((void **) intr_list_p, NULL, intr_entry)) {
465 /* we are the first interrupt on this bridge bit.
467 PCIBR_DEBUG_ALWAYS((PCIBR_DEBUG_INTR_ALLOC, pconn_vhdl,
468 "INT 0x%x (bridge bit %d) allocated [FIRST]\n",
469 pcibr_int_bits, pcibr_int_bit));
472 intr_list = *intr_list_p;
473 pcibr_intr_p = &intr_list->il_intr;
474 if (compare_and_swap_ptr((void **) pcibr_intr_p, NULL, pcibr_intr)) {
475 /* first entry on list was erased,
476 * and we replaced it, so we
477 * don't need our intr_entry.
480 PCIBR_DEBUG_ALWAYS((PCIBR_DEBUG_INTR_ALLOC, pconn_vhdl,
481 "INT 0x%x (bridge bit %d) replaces erased first\n",
482 pcibr_int_bits, pcibr_int_bit));
485 intr_list_p = &intr_list->il_next;
486 if (compare_and_swap_ptr((void **) intr_list_p, NULL, intr_entry)) {
487 /* we are the new second interrupt on this bit.
489 pcibr_soft->bs_intr[pcibr_int_bit].bsi_pcibr_intr_wrap.iw_shared = 1;
490 PCIBR_DEBUG_ALWAYS((PCIBR_DEBUG_INTR_ALLOC, pconn_vhdl,
491 "INT 0x%x (bridge bit %d) is new SECOND\n",
492 pcibr_int_bits, pcibr_int_bit));
496 pcibr_intr_p = &intr_list->il_intr;
497 if (compare_and_swap_ptr((void **) pcibr_intr_p, NULL, pcibr_intr)) {
498 /* an entry on list was erased,
499 * and we replaced it, so we
500 * don't need our intr_entry.
504 PCIBR_DEBUG_ALWAYS((PCIBR_DEBUG_INTR_ALLOC, pconn_vhdl,
505 "INT 0x%x (bridge bit %d) replaces erase Nth\n",
506 pcibr_int_bits, pcibr_int_bit));
509 intr_list_p = &intr_list->il_next;
510 if (compare_and_swap_ptr((void **) intr_list_p, NULL, intr_entry)) {
511 /* entry appended to share list
513 PCIBR_DEBUG_ALWAYS((PCIBR_DEBUG_INTR_ALLOC, pconn_vhdl,
514 "INT 0x%x (bridge bit %d) is new Nth\n",
515 pcibr_int_bits, pcibr_int_bit));
518 /* step to next record in chain
520 intr_list = *intr_list_p;
525 #if DEBUG && INTR_DEBUG
526 printk("%v pcibr_intr_alloc complete\n", pconn_vhdl);
528 hub_intr = (hub_intr_t)xtalk_intr;
529 pcibr_intr->bi_irq = hub_intr->i_bit;
530 pcibr_intr->bi_cpu = hub_intr->i_cpuid;
536 pcibr_intr_free(pcibr_intr_t pcibr_intr)
538 unsigned pcibr_int_bits = pcibr_intr->bi_ibits;
539 pcibr_soft_t pcibr_soft = pcibr_intr->bi_soft;
540 unsigned pcibr_int_bit;
541 pcibr_intr_list_t intr_list;
543 xtalk_intr_t *xtalk_intrp;
545 for (pcibr_int_bit = 0; pcibr_int_bit < 8; pcibr_int_bit++) {
546 if (pcibr_int_bits & (1 << pcibr_int_bit)) {
548 pcibr_soft->bs_intr[pcibr_int_bit].bsi_pcibr_intr_wrap.iw_list;
550 intr_list = intr_list->il_next)
551 if (compare_and_swap_ptr((void **) &intr_list->il_intr,
555 PCIBR_DEBUG_ALWAYS((PCIBR_DEBUG_INTR_ALLOC,
557 "pcibr_intr_free: cleared hdlr from bit 0x%x\n",
560 /* If this interrupt line is not being shared between multiple
561 * devices release the xtalk interrupt resources.
564 pcibr_soft->bs_intr[pcibr_int_bit].bsi_pcibr_intr_wrap.iw_shared;
565 xtalk_intrp = &pcibr_soft->bs_intr[pcibr_int_bit].bsi_xtalk_intr;
567 if ((!intr_shared) && (*xtalk_intrp)) {
569 bridge_t *bridge = pcibr_soft->bs_base;
572 xtalk_intr_free(*xtalk_intrp);
575 /* Clear the PCI device interrupt to bridge interrupt pin
578 int_dev = bridge->b_int_device;
579 int_dev &= ~BRIDGE_INT_DEV_MASK(pcibr_int_bit);
580 bridge->b_int_device = int_dev;
589 pcibr_setpciint(xtalk_intr_t xtalk_intr)
592 xtalk_intr_vector_t vect;
597 addr = xtalk_intr_addr_get(xtalk_intr);
598 vect = xtalk_intr_vector_get(xtalk_intr);
599 vhdl = xtalk_intr_dev_get(xtalk_intr);
600 bridge = (bridge_t *)xtalk_piotrans_addr(vhdl, 0, 0, sizeof(bridge_t), 0);
602 int_addr = (picreg_t *)xtalk_intr_sfarg_get(xtalk_intr);
603 *int_addr = ((PIC_INT_ADDR_FLD & ((uint64_t)vect << 48)) |
604 (PIC_INT_ADDR_HOST & addr));
609 pcibr_intr_connect(pcibr_intr_t pcibr_intr, intr_func_t intr_func, intr_arg_t intr_arg)
611 pcibr_soft_t pcibr_soft = pcibr_intr->bi_soft;
612 bridge_t *bridge = pcibr_soft->bs_base;
613 unsigned pcibr_int_bits = pcibr_intr->bi_ibits;
614 unsigned pcibr_int_bit;
618 if (pcibr_intr == NULL)
621 PCIBR_DEBUG_ALWAYS((PCIBR_DEBUG_INTR_ALLOC, pcibr_intr->bi_dev,
622 "pcibr_intr_connect: intr_func=0x%x\n",
625 pcibr_intr->bi_func = intr_func;
626 pcibr_intr->bi_arg = intr_arg;
627 *((volatile unsigned *)&pcibr_intr->bi_flags) |= PCIIO_INTR_CONNECTED;
630 * For each PCI interrupt line requested, figure
631 * out which Bridge PCI Interrupt Line it maps
632 * to, and make sure there are xtalk resources
635 for (pcibr_int_bit = 0; pcibr_int_bit < 8; pcibr_int_bit++)
636 if (pcibr_int_bits & (1 << pcibr_int_bit)) {
637 pcibr_intr_wrap_t intr_wrap;
638 xtalk_intr_t xtalk_intr;
641 xtalk_intr = pcibr_soft->bs_intr[pcibr_int_bit].bsi_xtalk_intr;
642 intr_wrap = &pcibr_soft->bs_intr[pcibr_int_bit].bsi_pcibr_intr_wrap;
645 * If this interrupt line is being shared and the connect has
646 * already been done, no need to do it again.
648 if (pcibr_soft->bs_intr[pcibr_int_bit].bsi_pcibr_intr_wrap.iw_connected)
653 * Use the pcibr wrapper function to handle all Bridge interrupts
654 * regardless of whether the interrupt line is shared or not.
656 if (IS_PIC_SOFT(pcibr_soft))
657 int_addr = (void *)&(bridge->p_int_addr_64[pcibr_int_bit]);
659 int_addr = (void *)&(bridge->b_int_addr[pcibr_int_bit].addr);
661 xtalk_intr_connect(xtalk_intr, pcibr_intr_func, (intr_arg_t) intr_wrap,
662 (xtalk_intr_setfunc_t) pcibr_setpciint,
665 pcibr_soft->bs_intr[pcibr_int_bit].bsi_pcibr_intr_wrap.iw_connected = 1;
667 PCIBR_DEBUG_ALWAYS((PCIBR_DEBUG_INTR_ALLOC, pcibr_intr->bi_dev,
668 "pcibr_setpciint: int_addr=0x%x, *int_addr=0x%x, "
669 "pcibr_int_bit=0x%x\n", int_addr,
670 *(picreg_t *)int_addr,
674 /* PIC WAR. PV# 854697
675 * On PIC we must write 64-bit MMRs with 64-bit stores
677 s = pcibr_lock(pcibr_soft);
678 if (IS_PIC_SOFT(pcibr_soft) &&
679 PCIBR_WAR_ENABLED(PV854697, pcibr_soft)) {
680 int_enable = bridge->p_int_enable_64;
681 int_enable |= pcibr_int_bits;
682 bridge->p_int_enable_64 = int_enable;
684 bridgereg_t int_enable;
686 int_enable = bridge->b_int_enable;
687 int_enable |= pcibr_int_bits;
688 bridge->b_int_enable = int_enable;
690 bridge->b_wid_tflush; /* wait until Bridge PIO complete */
691 pcibr_unlock(pcibr_soft, s);
698 pcibr_intr_disconnect(pcibr_intr_t pcibr_intr)
700 pcibr_soft_t pcibr_soft = pcibr_intr->bi_soft;
701 bridge_t *bridge = pcibr_soft->bs_base;
702 unsigned pcibr_int_bits = pcibr_intr->bi_ibits;
703 unsigned pcibr_int_bit;
704 pcibr_intr_wrap_t intr_wrap;
708 /* Stop calling the function. Now.
710 *((volatile unsigned *)&pcibr_intr->bi_flags) &= ~PCIIO_INTR_CONNECTED;
711 pcibr_intr->bi_func = 0;
712 pcibr_intr->bi_arg = 0;
714 * For each PCI interrupt line requested, figure
715 * out which Bridge PCI Interrupt Line it maps
716 * to, and disconnect the interrupt.
719 /* don't disable interrupts for lines that
720 * are shared between devices.
722 for (pcibr_int_bit = 0; pcibr_int_bit < 8; pcibr_int_bit++)
723 if ((pcibr_int_bits & (1 << pcibr_int_bit)) &&
724 (pcibr_soft->bs_intr[pcibr_int_bit].bsi_pcibr_intr_wrap.iw_shared))
725 pcibr_int_bits &= ~(1 << pcibr_int_bit);
729 /* PIC WAR. PV# 854697
730 * On PIC we must write 64-bit MMRs with 64-bit stores
732 s = pcibr_lock(pcibr_soft);
733 if (IS_PIC_SOFT(pcibr_soft) && PCIBR_WAR_ENABLED(PV854697, pcibr_soft)) {
734 int_enable = bridge->p_int_enable_64;
735 int_enable &= ~pcibr_int_bits;
736 bridge->p_int_enable_64 = int_enable;
738 int_enable = (uint64_t)bridge->b_int_enable;
739 int_enable &= ~pcibr_int_bits;
740 bridge->b_int_enable = (bridgereg_t)int_enable;
742 bridge->b_wid_tflush; /* wait until Bridge PIO complete */
743 pcibr_unlock(pcibr_soft, s);
745 PCIBR_DEBUG_ALWAYS((PCIBR_DEBUG_INTR_ALLOC, pcibr_intr->bi_dev,
746 "pcibr_intr_disconnect: disabled int_bits=0x%x\n",
749 for (pcibr_int_bit = 0; pcibr_int_bit < 8; pcibr_int_bit++)
750 if (pcibr_int_bits & (1 << pcibr_int_bit)) {
753 /* if the interrupt line is now shared,
754 * do not disconnect it.
756 if (pcibr_soft->bs_intr[pcibr_int_bit].bsi_pcibr_intr_wrap.iw_shared)
759 xtalk_intr_disconnect(pcibr_soft->bs_intr[pcibr_int_bit].bsi_xtalk_intr);
760 pcibr_soft->bs_intr[pcibr_int_bit].bsi_pcibr_intr_wrap.iw_connected = 0;
762 PCIBR_DEBUG_ALWAYS((PCIBR_DEBUG_INTR_ALLOC, pcibr_intr->bi_dev,
763 "pcibr_intr_disconnect: disconnect int_bits=0x%x\n",
766 /* if we are sharing the interrupt line,
767 * connect us up; this closes the hole
768 * where the another pcibr_intr_alloc()
769 * was in progress as we disconnected.
771 if (!pcibr_soft->bs_intr[pcibr_int_bit].bsi_pcibr_intr_wrap.iw_shared)
774 intr_wrap = &pcibr_soft->bs_intr[pcibr_int_bit].bsi_pcibr_intr_wrap;
775 if (!pcibr_soft->bs_intr[pcibr_int_bit].bsi_pcibr_intr_wrap.iw_shared)
778 if (IS_PIC_SOFT(pcibr_soft))
779 int_addr = (void *)&(bridge->p_int_addr_64[pcibr_int_bit]);
781 int_addr = (void *)&(bridge->b_int_addr[pcibr_int_bit].addr);
783 xtalk_intr_connect(pcibr_soft->bs_intr[pcibr_int_bit].bsi_xtalk_intr,
784 pcibr_intr_func, (intr_arg_t) intr_wrap,
785 (xtalk_intr_setfunc_t)pcibr_setpciint,
786 (void *)(long)pcibr_int_bit);
787 PCIBR_DEBUG_ALWAYS((PCIBR_DEBUG_INTR_ALLOC, pcibr_intr->bi_dev,
788 "pcibr_intr_disconnect: now-sharing int_bits=0x%x\n",
795 pcibr_intr_cpu_get(pcibr_intr_t pcibr_intr)
797 pcibr_soft_t pcibr_soft = pcibr_intr->bi_soft;
798 unsigned pcibr_int_bits = pcibr_intr->bi_ibits;
799 unsigned pcibr_int_bit;
801 for (pcibr_int_bit = 0; pcibr_int_bit < 8; pcibr_int_bit++)
802 if (pcibr_int_bits & (1 << pcibr_int_bit))
803 return xtalk_intr_cpu_get(pcibr_soft->bs_intr[pcibr_int_bit].bsi_xtalk_intr);
807 /* =====================================================================
811 pcibr_clearwidint(bridge_t *bridge)
813 bridge->b_wid_int_upper = 0;
814 bridge->b_wid_int_lower = 0;
819 pcibr_setwidint(xtalk_intr_t intr)
821 xwidgetnum_t targ = xtalk_intr_target_get(intr);
822 iopaddr_t addr = xtalk_intr_addr_get(intr);
823 xtalk_intr_vector_t vect = xtalk_intr_vector_get(intr);
824 widgetreg_t NEW_b_wid_int_upper, NEW_b_wid_int_lower;
825 widgetreg_t OLD_b_wid_int_upper, OLD_b_wid_int_lower;
827 bridge_t *bridge = (bridge_t *)xtalk_intr_sfarg_get(intr);
829 NEW_b_wid_int_upper = ( (0x000F0000 & (targ << 16)) |
830 XTALK_ADDR_TO_UPPER(addr));
831 NEW_b_wid_int_lower = XTALK_ADDR_TO_LOWER(addr);
833 OLD_b_wid_int_upper = bridge->b_wid_int_upper;
834 OLD_b_wid_int_lower = bridge->b_wid_int_lower;
836 /* Verify that all interrupts from this Bridge are using a single PI */
837 if ((OLD_b_wid_int_upper != 0) && (OLD_b_wid_int_lower != 0)) {
839 * Once set, these registers shouldn't change; they should
840 * be set multiple times with the same values.
842 * If we're attempting to change these registers, it means
843 * that our heuristics for allocating interrupts in a way
844 * appropriate for IP35 have failed, and the admin needs to
845 * explicitly direct some interrupts (or we need to make the
846 * heuristics more clever).
848 * In practice, we hope this doesn't happen very often, if
851 if ((OLD_b_wid_int_upper != NEW_b_wid_int_upper) ||
852 (OLD_b_wid_int_lower != NEW_b_wid_int_lower)) {
853 printk(KERN_WARNING "Interrupt allocation is too complex.\n");
854 printk(KERN_WARNING "Use explicit administrative interrupt targetting.\n");
855 printk(KERN_WARNING "bridge=0x%lx targ=0x%x\n", (unsigned long)bridge, targ);
856 printk(KERN_WARNING "NEW=0x%x/0x%x OLD=0x%x/0x%x\n",
857 NEW_b_wid_int_upper, NEW_b_wid_int_lower,
858 OLD_b_wid_int_upper, OLD_b_wid_int_lower);
859 panic("PCI Bridge interrupt targetting error\n");
863 bridge->b_wid_int_upper = NEW_b_wid_int_upper;
864 bridge->b_wid_int_lower = NEW_b_wid_int_lower;
865 bridge->b_int_host_err = vect;
870 * pcibr_intr_preset: called during mlreset time
871 * if the platform specific code needs to route
872 * one of the Bridge's xtalk interrupts before the
873 * xtalk infrastructure is available.
876 pcibr_xintr_preset(void *which_widget,
877 int which_widget_intr,
880 xtalk_intr_vector_t vect)
882 bridge_t *bridge = (bridge_t *) which_widget;
884 if (which_widget_intr == -1) {
885 /* bridge widget error interrupt */
886 bridge->b_wid_int_upper = ( (0x000F0000 & (targ << 16)) |
887 XTALK_ADDR_TO_UPPER(addr));
888 bridge->b_wid_int_lower = XTALK_ADDR_TO_LOWER(addr);
889 bridge->b_int_host_err = vect;
890 printk("pcibr_xintr_preset: b_wid_int_upper 0x%lx b_wid_int_lower 0x%lx b_int_host_err 0x%x\n",
891 ( (0x000F0000 & (targ << 16)) | XTALK_ADDR_TO_UPPER(addr)),
892 XTALK_ADDR_TO_LOWER(addr), vect);
894 /* turn on all interrupts except
895 * the PCI interrupt requests,
898 bridge->b_int_enable |= ~BRIDGE_IMR_INT_MSK;
901 /* routing a PCI device interrupt.
902 * targ and low 38 bits of addr must
903 * be the same as the already set
904 * value for the widget error interrupt.
906 bridge->b_int_addr[which_widget_intr].addr =
907 ((BRIDGE_INT_ADDR_HOST & (addr >> 30)) |
908 (BRIDGE_INT_ADDR_FLD & vect));
910 * now bridge can let it through;
911 * NB: still should be blocked at
912 * xtalk provider end, until the service
915 bridge->b_int_enable |= 1 << vect;
917 bridge->b_wid_tflush; /* wait until Bridge PIO complete */
924 * This is the pcibr interrupt "wrapper" function that is called,
925 * in interrupt context, to initiate the interrupt handler(s) registered
926 * (via pcibr_intr_alloc/connect) for the occurring interrupt. Non-threaded
927 * handlers will be called directly, and threaded handlers will have their
931 pcibr_intr_func(intr_arg_t arg)
933 pcibr_intr_wrap_t wrap = (pcibr_intr_wrap_t) arg;
937 pcibr_intr_list_t list;
939 int do_nonthreaded = 1;
942 pcibr_soft_t pcibr_soft = wrap->iw_soft;
943 bridge_t *bridge = pcibr_soft->bs_base;
944 uint64_t p_enable = pcibr_soft->bs_int_enable;
945 int bit = wrap->iw_ibit;
949 * Early attempt at a workaround for the runaway
950 * interrupt problem. Briefly disable the enable bit for
953 if (IS_PIC_SOFT(pcibr_soft) &&
954 PCIBR_WAR_ENABLED(PV855272, pcibr_soft)) {
957 /* disable-enable interrupts for this bridge pin */
959 p_enable &= ~(1 << bit);
960 s = pcibr_lock(pcibr_soft);
961 bridge->p_int_enable_64 = p_enable;
962 p_enable |= (1 << bit);
963 bridge->p_int_enable_64 = p_enable;
964 pcibr_unlock(pcibr_soft, s);
968 * If any handler is still running from a previous interrupt
969 * just return. If there's a need to call the handler(s) again,
970 * another interrupt will be generated either by the device or by
971 * pcibr_force_interrupt().
974 if (wrap->iw_hdlrcnt) {
979 * Call all interrupt handlers registered.
980 * First, the pcibr_intrd threads for any threaded handlers will be
981 * awoken, then any non-threaded handlers will be called sequentially.
985 while (do_nonthreaded) {
986 for (list = wrap->iw_list; list != NULL; list = list->il_next) {
987 if ((intr = list->il_intr) && (intr->bi_flags & PCIIO_INTR_CONNECTED)) {
990 * This device may have initiated write
991 * requests since the bridge last saw
992 * an edge on this interrupt input; flushing
993 * the buffer prior to invoking the handler
994 * should help but may not be sufficient if we
995 * get more requests after the flush, followed
996 * by the card deciding it wants service, before
997 * the interrupt handler checks to see if things need
1000 * There is a similar race condition if
1001 * an interrupt handler loops around and
1002 * notices further service is required.
1003 * Perhaps we need to have an explicit
1004 * call that interrupt handlers need to
1005 * do between noticing that DMA to memory
1006 * has completed, but before observing the
1007 * contents of memory?
1010 if ((do_nonthreaded) && (!is_threaded)) {
1011 /* Non-threaded - Call the interrupt handler at interrupt level */
1012 /* Only need to flush write buffers if sharing */
1014 if ((wrap->iw_shared) && (wrbf = list->il_wrbf)) {
1015 if ((x = *wrbf)) /* write request buffer flush */
1016 #ifdef SUPPORT_PRINTING_V_FORMAT
1017 printk(KERN_ALERT "pcibr_intr_func %v: \n"
1018 "write buffer flush failed, wrbf=0x%x\n",
1019 list->il_intr->bi_dev, wrbf);
1021 printk(KERN_ALERT "pcibr_intr_func %p: \n"
1022 "write buffer flush failed, wrbf=0x%lx\n",
1023 (void *)list->il_intr->bi_dev, (long) wrbf);
1026 func = intr->bi_func;
1036 * If the non-threaded handler was the last to complete,
1037 * (i.e., no threaded handlers still running) force an
1038 * interrupt to avoid a potential deadlock situation.
1040 if (wrap->iw_hdlrcnt == 0) {
1041 pcibr_force_interrupt((pcibr_intr_t) wrap);
1045 /* If there were no handlers,
1046 * disable the interrupt and return.
1047 * It will get enabled again after
1048 * a handler is connected.
1049 * If we don't do this, we would
1050 * sit here and spin through the
1054 pcibr_soft_t pcibr_soft = wrap->iw_soft;
1055 bridge_t *bridge = pcibr_soft->bs_base;
1056 bridgereg_t int_enable;
1057 bridgereg_t mask = 1 << wrap->iw_ibit;
1060 /* PIC BRINUGP WAR (PV# 854697):
1061 * On PIC we must write 64-bit MMRs with 64-bit stores
1063 s = pcibr_lock(pcibr_soft);
1064 if (IS_PIC_SOFT(pcibr_soft) &&
1065 PCIBR_WAR_ENABLED(PV854697, pcibr_soft)) {
1066 int_enable = bridge->p_int_enable_64;
1067 int_enable &= ~mask;
1068 bridge->p_int_enable_64 = int_enable;
1070 int_enable = (uint64_t)bridge->b_int_enable;
1071 int_enable &= ~mask;
1072 bridge->b_int_enable = (bridgereg_t)int_enable;
1074 bridge->b_wid_tflush; /* wait until Bridge PIO complete */
1075 pcibr_unlock(pcibr_soft, s);