Home | History | Annotate | Line # | Download | only in pci
pci_machdep.c revision 1.29
      1 /* $NetBSD: pci_machdep.c,v 1.29 2021/06/19 16:59:07 thorpej Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2020 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jason R. Thorpe.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  * Copyright (c) 1995, 1996 Carnegie-Mellon University.
     34  * All rights reserved.
     35  *
     36  * Author: Chris G. Demetriou
     37  *
     38  * Permission to use, copy, modify and distribute this software and
     39  * its documentation is hereby granted, provided that both the copyright
     40  * notice and this permission notice appear in all copies of the
     41  * software, derivative works or modified versions, and any portions
     42  * thereof, and that both notices appear in supporting documentation.
     43  *
     44  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
     45  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
     46  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
     47  *
     48  * Carnegie Mellon requests users of this software to return to
     49  *
     50  *  Software Distribution Coordinator  or  Software.Distribution (at) CS.CMU.EDU
     51  *  School of Computer Science
     52  *  Carnegie Mellon University
     53  *  Pittsburgh PA 15213-3890
     54  *
     55  * any improvements or extensions that they make and grant Carnegie the
     56  * rights to redistribute these changes.
     57  */
     58 
     59 /*
     60  * Machine-specific functions for PCI autoconfiguration.
     61  */
     62 
     63 #include <sys/cdefs.h>			/* RCS ID & Copyright macro defns */
     64 
     65 __KERNEL_RCSID(0, "$NetBSD: pci_machdep.c,v 1.29 2021/06/19 16:59:07 thorpej Exp $");
     66 
     67 #include <sys/types.h>
     68 #include <sys/param.h>
     69 #include <sys/time.h>
     70 #include <sys/systm.h>
     71 #include <sys/errno.h>
     72 #include <sys/device.h>
     73 #include <sys/cpu.h>
     74 
     75 #include <dev/isa/isavar.h>
     76 #include <dev/pci/pcireg.h>
     77 #include <dev/pci/pcivar.h>
     78 #include <dev/pci/pcidevs.h>
     79 
     80 #include "vga_pci.h"
     81 #if NVGA_PCI
     82 #include <dev/ic/mc6845reg.h>
     83 #include <dev/ic/pcdisplayvar.h>
     84 #include <dev/pci/vga_pcivar.h>
     85 #endif
     86 
     87 #include "tga.h"
     88 #if NTGA
     89 #include <dev/pci/tgavar.h>
     90 #endif
     91 
     92 #include <machine/rpb.h>
     93 
     94 void
     95 pci_display_console(bus_space_tag_t iot, bus_space_tag_t memt, pci_chipset_tag_t pc, int bus, int device, int function)
     96 {
     97 #if NVGA_PCI || NTGA
     98 	pcitag_t tag;
     99 	pcireg_t id;
    100 	int match, nmatch;
    101 #endif
    102 #if NVGA_PCI
    103 	pcireg_t class;
    104 #endif
    105 	int (*fn)(bus_space_tag_t, bus_space_tag_t, pci_chipset_tag_t,
    106 	    int, int, int);
    107 
    108 #if NVGA_PCI || NTGA
    109 	tag = pci_make_tag(pc, bus, device, function);
    110 	id = pci_conf_read(pc, tag, PCI_ID_REG);
    111 	if (id == 0 || id == 0xffffffff)
    112 		panic("pci_display_console: no device at %d/%d/%d",
    113 		    bus, device, function);
    114 #  if NVGA_PCI
    115 	class = pci_conf_read(pc, tag, PCI_CLASS_REG);
    116 #  endif
    117 
    118 	match = 0;
    119 #endif
    120 	fn = NULL;
    121 
    122 #if NVGA_PCI
    123 	nmatch = DEVICE_IS_VGA_PCI(class, id);
    124 	if (nmatch > match) {
    125 		match = nmatch;
    126 		fn = vga_pci_cnattach;
    127 	}
    128 #endif
    129 #if NTGA
    130 	nmatch = DEVICE_IS_TGA(class, id);
    131 	if (nmatch > match)
    132 		nmatch = tga_cnmatch(iot, memt, pc, tag);
    133 	if (nmatch > match) {
    134 		match = nmatch;
    135 		fn = tga_cnattach;
    136 	}
    137 #endif
    138 
    139 	if (fn != NULL)
    140 		(*fn)(iot, memt, pc, bus, device, function);
    141 	else
    142 		panic("pci_display_console: unconfigured device at %d/%d/%d",
    143 		    bus, device, function);
    144 }
    145 
    146 void
    147 device_pci_register(device_t dev, void *aux)
    148 {
    149 	struct pci_attach_args *pa = aux;
    150 	struct ctb *ctb;
    151 	prop_dictionary_t dict;
    152 
    153 	/* set properties for PCI framebuffers */
    154 	ctb = (struct ctb *)(((char *)hwrpb) + hwrpb->rpb_ctb_off);
    155 	if (PCI_CLASS(pa->pa_class) == PCI_CLASS_DISPLAY &&
    156 	    ctb->ctb_term_type == CTB_GRAPHICS) {
    157 		/* XXX should consider multiple displays? */
    158 		dict = device_properties(dev);
    159 		prop_dictionary_set_bool(dict, "is_console", true);
    160 	}
    161 }
    162 
    163 void
    164 alpha_pci_intr_init(void *core, bus_space_tag_t iot, bus_space_tag_t memt,
    165     pci_chipset_tag_t pc)
    166 {
    167 	__link_set_decl(alpha_pci_intr_impls, struct alpha_pci_intr_impl);
    168 	struct alpha_pci_intr_impl * const *impl;
    169 
    170 	__link_set_foreach(impl, alpha_pci_intr_impls) {
    171 		if ((*impl)->systype == cputype) {
    172 			(*impl)->intr_init(core, iot, memt, pc);
    173 			return;
    174 		}
    175 	}
    176 	panic("%s: unknown systype %d", __func__, cputype);
    177 }
    178 
    179 int
    180 alpha_pci_generic_intr_map(const struct pci_attach_args * const pa,
    181     pci_intr_handle_t * const ihp)
    182 {
    183 	pcitag_t const bustag = pa->pa_intrtag;
    184 	int const buspin = pa->pa_intrpin;
    185 	int const line = pa->pa_intrline;
    186 	pci_chipset_tag_t const pc = pa->pa_pc;
    187 	int bus, device, function;
    188 
    189 	if (buspin == 0) {
    190 		/* No IRQ used. */
    191 		return 1;
    192 	}
    193 	if (buspin < 0 || buspin > 4) {
    194 		printf("%s: bad interrupt pin %d\n", __func__, buspin);
    195 		return 1;
    196 	}
    197 
    198 	pci_decompose_tag(pc, bustag, &bus, &device, &function);
    199 
    200 	/*
    201 	 * The console firmware places the interrupt mapping in the "line"
    202 	 * value.  A valaue of (char)-1 indicates there is no mapping.
    203 	 */
    204 	if (line == 0xff) {
    205 		printf("%s: no mapping for %d/%d/%d\n", __func__,
    206 		    bus, device, function);
    207 		return 1;
    208 	}
    209 
    210 	if (line < 0 || line >= pc->pc_nirq) {
    211 		printf("%s: bad line %d for %d/%d/%d\n", __func__,
    212 		    line, bus, device, function);
    213 		return 1;
    214 	}
    215 
    216 	alpha_pci_intr_handle_init(ihp, line, 0);
    217 	return 0;
    218 }
    219 
    220 const char *
    221 alpha_pci_generic_intr_string(pci_chipset_tag_t const pc,
    222     pci_intr_handle_t const ih, char * const buf, size_t const len)
    223 {
    224 	const u_int irq = alpha_pci_intr_handle_get_irq(&ih);
    225 
    226 	KASSERT(irq < pc->pc_nirq);
    227 
    228 	snprintf(buf, len, "%s %u", pc->pc_intr_desc, irq);
    229 	return buf;
    230 }
    231 
    232 const struct evcnt *
    233 alpha_pci_generic_intr_evcnt(pci_chipset_tag_t const pc,
    234     pci_intr_handle_t const ih)
    235 {
    236 	const u_int irq = alpha_pci_intr_handle_get_irq(&ih);
    237 
    238 	KASSERT(irq < pc->pc_nirq);
    239 
    240 	return alpha_shared_intr_evcnt(pc->pc_shared_intrs, irq);
    241 }
    242 
    243 static struct cpu_info *
    244 alpha_pci_generic_intr_select_cpu(pci_chipset_tag_t const pc, u_int const irq,
    245     u_int const flags)
    246 {
    247 	struct cpu_info *ci, *best_ci;
    248 	CPU_INFO_ITERATOR cii;
    249 
    250 	KASSERT(mutex_owned(&cpu_lock));
    251 
    252 	/*
    253 	 * If the back-end didn't tell us where we can route, then
    254 	 * they all go to the primry CPU.
    255 	 */
    256 	if (pc->pc_eligible_cpus == 0) {
    257 		return &cpu_info_primary;
    258 	}
    259 
    260 	/*
    261 	 * If the interrupt already has a CPU assigned, keep on using it,
    262 	 * unless the CPU has become ineligible.
    263 	 */
    264 	ci = alpha_shared_intr_get_cpu(pc->pc_shared_intrs, irq);
    265 	if (ci != NULL) {
    266 		if ((ci->ci_schedstate.spc_flags & SPCF_NOINTR) == 0 ||
    267 		    CPU_IS_PRIMARY(ci)) {
    268 			return ci;
    269 		}
    270 	}
    271 
    272 	/*
    273 	 * Pick the CPU with the fewest handlers.
    274 	 */
    275 	best_ci = NULL;
    276 	for (CPU_INFO_FOREACH(cii, ci)) {
    277 		if ((pc->pc_eligible_cpus & __BIT(ci->ci_cpuid)) == 0) {
    278 			/* This CPU is not eligible in hardware. */
    279 			continue;
    280 		}
    281 		if (ci->ci_schedstate.spc_flags & SPCF_NOINTR) {
    282 			/* This CPU is not eligible in software. */
    283 			continue;
    284 		}
    285 		if (best_ci == NULL ||
    286 		    ci->ci_nintrhand < best_ci->ci_nintrhand) {
    287 			best_ci = ci;
    288 		}
    289 	}
    290 
    291 	/* If we found one, cool... */
    292 	if (best_ci != NULL) {
    293 		return best_ci;
    294 	}
    295 
    296 	/* ...if not, well I guess we'll just fall back on the primary. */
    297 	return &cpu_info_primary;
    298 }
    299 
    300 void *
    301 alpha_pci_generic_intr_establish(pci_chipset_tag_t const pc,
    302     pci_intr_handle_t const ih, int const level,
    303     int (*func)(void *), void *arg)
    304 {
    305 	const u_int irq = alpha_pci_intr_handle_get_irq(&ih);
    306 	const u_int flags = alpha_pci_intr_handle_get_flags(&ih);
    307 	void *cookie;
    308 
    309 	KASSERT(irq < pc->pc_nirq);
    310 
    311 	cookie = alpha_shared_intr_alloc_intrhand(pc->pc_shared_intrs,
    312 	    irq, IST_LEVEL, level, flags, func, arg, pc->pc_intr_desc);
    313 
    314 	if (cookie == NULL)
    315 		return NULL;
    316 
    317 	mutex_enter(&cpu_lock);
    318 
    319 	struct cpu_info *target_ci =
    320 	    alpha_pci_generic_intr_select_cpu(pc, irq, flags);
    321 	struct cpu_info *current_ci =
    322 	    alpha_shared_intr_get_cpu(pc->pc_shared_intrs, irq);
    323 
    324 	const bool first_handler =
    325 	    ! alpha_shared_intr_isactive(pc->pc_shared_intrs, irq);
    326 
    327 	/*
    328 	 * If this is the first handler on this interrupt, or if the
    329 	 * target CPU has changed, then program the route if the
    330 	 * hardware supports it.
    331 	 */
    332 	if (first_handler || target_ci != current_ci) {
    333 		alpha_shared_intr_set_cpu(pc->pc_shared_intrs, irq, target_ci);
    334 		if (pc->pc_intr_set_affinity != NULL) {
    335 			pc->pc_intr_set_affinity(pc, irq, target_ci);
    336 		}
    337 	}
    338 
    339 	if (! alpha_shared_intr_link(pc->pc_shared_intrs, cookie,
    340 				     pc->pc_intr_desc)) {
    341 		mutex_exit(&cpu_lock);
    342 		alpha_shared_intr_free_intrhand(cookie);
    343 		return NULL;
    344 	}
    345 
    346 	if (first_handler) {
    347 		scb_set(pc->pc_vecbase + SCB_IDXTOVEC(irq),
    348 		    alpha_pci_generic_iointr, pc);
    349 		pc->pc_intr_enable(pc, irq);
    350 	}
    351 
    352 	mutex_exit(&cpu_lock);
    353 
    354 	return cookie;
    355 }
    356 
    357 void
    358 alpha_pci_generic_intr_disestablish(pci_chipset_tag_t const pc,
    359     void * const cookie)
    360 {
    361 	struct alpha_shared_intrhand * const ih = cookie;
    362 	const u_int irq = ih->ih_num;
    363 
    364 	mutex_enter(&cpu_lock);
    365 
    366 	if (alpha_shared_intr_firstactive(pc->pc_shared_intrs, irq)) {
    367 		pc->pc_intr_disable(pc, irq);
    368 		alpha_shared_intr_set_dfltsharetype(pc->pc_shared_intrs,
    369 		    irq, IST_NONE);
    370 		scb_free(pc->pc_vecbase + SCB_IDXTOVEC(irq));
    371 	}
    372 
    373 	alpha_shared_intr_unlink(pc->pc_shared_intrs, cookie, pc->pc_intr_desc);
    374 
    375 	mutex_exit(&cpu_lock);
    376 
    377 	alpha_shared_intr_free_intrhand(cookie);
    378 }
    379 
    380 void
    381 alpha_pci_generic_iointr(void * const arg, unsigned long const vec)
    382 {
    383 	pci_chipset_tag_t const pc = arg;
    384 	const u_int irq = SCB_VECTOIDX(vec - pc->pc_vecbase);
    385 
    386 	if (!alpha_shared_intr_dispatch(pc->pc_shared_intrs, irq)) {
    387 		alpha_shared_intr_stray(pc->pc_shared_intrs, irq,
    388 		    pc->pc_intr_desc);
    389 		if (ALPHA_SHARED_INTR_DISABLE(pc->pc_shared_intrs, irq)) {
    390 			pc->pc_intr_disable(pc, irq);
    391 		}
    392 	} else {
    393 		alpha_shared_intr_reset_strays(pc->pc_shared_intrs, irq);
    394 	}
    395 }
    396 
    397 void
    398 alpha_pci_generic_intr_redistribute(pci_chipset_tag_t const pc)
    399 {
    400 	struct cpu_info *current_ci, *new_ci;
    401 	unsigned int irq;
    402 
    403 	KASSERT(mutex_owned(&cpu_lock));
    404 	KASSERT(mp_online);
    405 
    406 	/* If we can't set affinity, then there's nothing to do. */
    407 	if (pc->pc_eligible_cpus == 0 || pc->pc_intr_set_affinity == NULL) {
    408 		return;
    409 	}
    410 
    411 	/*
    412 	 * Look at each IRQ, and allocate a new CPU for each IRQ
    413 	 * that's being serviced by a now-shielded CPU.
    414 	 */
    415 	for (irq = 0; irq < pc->pc_nirq; irq++) {
    416 		current_ci =
    417 		    alpha_shared_intr_get_cpu(pc->pc_shared_intrs, irq);
    418 		if (current_ci == NULL ||
    419 		    (current_ci->ci_schedstate.spc_flags & SPCF_NOINTR) == 0) {
    420 			continue;
    421 		}
    422 
    423 		new_ci = alpha_pci_generic_intr_select_cpu(pc, irq, 0);
    424 		if (new_ci == current_ci) {
    425 			/* Can't shield this one. */
    426 			continue;
    427 		}
    428 
    429 		alpha_shared_intr_set_cpu(pc->pc_shared_intrs, irq, new_ci);
    430 		pc->pc_intr_set_affinity(pc, irq, new_ci);
    431 	}
    432 
    433 	/* XXX should now re-balance */
    434 }
    435 
    436 #define	ALPHA_PCI_INTR_HANDLE_IRQ	__BITS(0,31)
    437 #define	ALPHA_PCI_INTR_HANDLE_FLAGS	__BITS(32,63)
    438 
    439 void
    440 alpha_pci_intr_handle_init(pci_intr_handle_t * const ihp, u_int const irq,
    441     u_int const flags)
    442 {
    443 	ihp->value = __SHIFTIN(irq, ALPHA_PCI_INTR_HANDLE_IRQ) |
    444 	    __SHIFTIN(flags, ALPHA_PCI_INTR_HANDLE_FLAGS);
    445 }
    446 
    447 void
    448 alpha_pci_intr_handle_set_irq(pci_intr_handle_t * const ihp, u_int const irq)
    449 {
    450 	ihp->value = (ihp->value & ALPHA_PCI_INTR_HANDLE_FLAGS) |
    451 	    __SHIFTIN(irq, ALPHA_PCI_INTR_HANDLE_IRQ);
    452 }
    453 
    454 u_int
    455 alpha_pci_intr_handle_get_irq(const pci_intr_handle_t * const ihp)
    456 {
    457 	return __SHIFTOUT(ihp->value, ALPHA_PCI_INTR_HANDLE_IRQ);
    458 }
    459 
    460 void
    461 alpha_pci_intr_handle_set_flags(pci_intr_handle_t * const ihp,
    462     u_int const flags)
    463 {
    464 	ihp->value = (ihp->value & ALPHA_PCI_INTR_HANDLE_IRQ) |
    465 	    __SHIFTIN(flags, ALPHA_PCI_INTR_HANDLE_FLAGS);
    466 }
    467 
    468 u_int
    469 alpha_pci_intr_handle_get_flags(const pci_intr_handle_t * const ihp)
    470 {
    471 	return __SHIFTOUT(ihp->value, ALPHA_PCI_INTR_HANDLE_FLAGS);
    472 }
    473 
    474 /*
    475  * MI PCI back-end entry points.
    476  */
    477 
    478 void
    479 pci_attach_hook(device_t const parent, device_t const self,
    480     struct pcibus_attach_args * const pba)
    481 {
    482 	pci_chipset_tag_t const pc = pba->pba_pc;
    483 
    484 	KASSERT(pc->pc_attach_hook != NULL);
    485 	pc->pc_attach_hook(parent, self, pba);
    486 }
    487 
    488 int
    489 pci_bus_maxdevs(pci_chipset_tag_t const pc, int const busno)
    490 {
    491 	KASSERT(pc->pc_bus_maxdevs != NULL);
    492 	return pc->pc_bus_maxdevs(pc->pc_conf_v, busno);
    493 }
    494 
    495 pcitag_t
    496 pci_make_tag(pci_chipset_tag_t const pc, int const bus, int const dev,
    497     int const func)
    498 {
    499 	KASSERT(pc->pc_make_tag != NULL);
    500 	return pc->pc_make_tag(pc->pc_conf_v, bus, dev, func);
    501 }
    502 
    503 void
    504 pci_decompose_tag(pci_chipset_tag_t const pc, pcitag_t const tag,
    505     int * const busp, int * const devp, int * const funcp)
    506 {
    507 	KASSERT(pc->pc_decompose_tag != NULL);
    508 	pc->pc_decompose_tag(pc->pc_conf_v, tag, busp, devp, funcp);
    509 }
    510 
    511 pcireg_t
    512 pci_conf_read(pci_chipset_tag_t const pc, pcitag_t const tag, int const reg)
    513 {
    514 	KASSERT(pc->pc_conf_read != NULL);
    515 	return pc->pc_conf_read(pc->pc_conf_v, tag, reg);
    516 }
    517 
    518 void
    519 pci_conf_write(pci_chipset_tag_t const pc, pcitag_t const tag, int const reg,
    520     pcireg_t const val)
    521 {
    522 	KASSERT(pc->pc_conf_write != NULL);
    523 	pc->pc_conf_write(pc->pc_conf_v, tag, reg, val);
    524 }
    525 
    526 int
    527 pci_intr_map(const struct pci_attach_args * const pa,
    528     pci_intr_handle_t * const ihp)
    529 {
    530 	pci_chipset_tag_t const pc = pa->pa_pc;
    531 
    532 	KASSERT(pc->pc_intr_map != NULL);
    533 	return pc->pc_intr_map(pa, ihp);
    534 }
    535 
    536 const char *
    537 pci_intr_string(pci_chipset_tag_t const pc, pci_intr_handle_t const ih,
    538     char * const buf, size_t const len)
    539 {
    540 	KASSERT(pc->pc_intr_string != NULL);
    541 	return pc->pc_intr_string(pc, ih, buf, len);
    542 }
    543 
    544 const struct evcnt *
    545 pci_intr_evcnt(pci_chipset_tag_t const pc, pci_intr_handle_t const ih)
    546 {
    547 	KASSERT(pc->pc_intr_evcnt != NULL);
    548 	return pc->pc_intr_evcnt(pc, ih);
    549 }
    550 
    551 void *
    552 pci_intr_establish(pci_chipset_tag_t const pc, pci_intr_handle_t const ih,
    553     int const ipl, int (*func)(void *), void *arg)
    554 {
    555 	KASSERT(pc->pc_intr_establish != NULL);
    556 	return pc->pc_intr_establish(pc, ih, ipl, func, arg);
    557 }
    558 
    559 void
    560 pci_intr_disestablish(pci_chipset_tag_t const pc, void * const cookie)
    561 {
    562 	KASSERT(pc->pc_intr_disestablish != NULL);
    563 	pc->pc_intr_disestablish(pc, cookie);
    564 }
    565 
    566 int
    567 pci_intr_setattr(pci_chipset_tag_t const pc __unused,
    568     pci_intr_handle_t * const ihp, int const attr, uint64_t const data)
    569 {
    570 	u_int flags = alpha_pci_intr_handle_get_flags(ihp);
    571 
    572 	switch (attr) {
    573 	case PCI_INTR_MPSAFE:
    574 		if (data)
    575 			flags |= ALPHA_INTR_MPSAFE;
    576 		else
    577 			flags &= ~ALPHA_INTR_MPSAFE;
    578 		break;
    579 
    580 	default:
    581 		return ENODEV;
    582 	}
    583 
    584 	alpha_pci_intr_handle_set_flags(ihp, flags);
    585 	return 0;
    586 }
    587