Home | History | Annotate | Line # | Download | only in pci
pci_machdep.c revision 1.27
      1 /* $NetBSD: pci_machdep.c,v 1.27 2020/09/26 02:46:28 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.27 2020/09/26 02:46:28 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 int
    164 alpha_pci_generic_intr_map(const struct pci_attach_args * const pa,
    165     pci_intr_handle_t * const ihp)
    166 {
    167 	pcitag_t const bustag = pa->pa_intrtag;
    168 	int const buspin = pa->pa_intrpin;
    169 	int const line = pa->pa_intrline;
    170 	pci_chipset_tag_t const pc = pa->pa_pc;
    171 	int bus, device, function;
    172 
    173 	if (buspin == 0) {
    174 		/* No IRQ used. */
    175 		return 1;
    176 	}
    177 	if (buspin < 0 || buspin > 4) {
    178 		printf("%s: bad interrupt pin %d\n", __func__, buspin);
    179 		return 1;
    180 	}
    181 
    182 	pci_decompose_tag(pc, bustag, &bus, &device, &function);
    183 
    184 	/*
    185 	 * The console firmware places the interrupt mapping in the "line"
    186 	 * value.  A valaue of (char)-1 indicates there is no mapping.
    187 	 */
    188 	if (line == 0xff) {
    189 		printf("%s: no mapping for %d/%d/%d\n", __func__,
    190 		    bus, device, function);
    191 		return 1;
    192 	}
    193 
    194 	if (line < 0 || line >= pc->pc_nirq) {
    195 		printf("%s: bad line %d for %d/%d/%d\n", __func__,
    196 		    line, bus, device, function);
    197 		return 1;
    198 	}
    199 
    200 	alpha_pci_intr_handle_init(ihp, line, 0);
    201 	return 0;
    202 }
    203 
    204 const char *
    205 alpha_pci_generic_intr_string(pci_chipset_tag_t const pc,
    206     pci_intr_handle_t const ih, char * const buf, size_t const len)
    207 {
    208 	const u_int irq = alpha_pci_intr_handle_get_irq(&ih);
    209 
    210 	KASSERT(irq < pc->pc_nirq);
    211 
    212 	snprintf(buf, len, "%s %u", pc->pc_intr_desc, irq);
    213 	return buf;
    214 }
    215 
    216 const struct evcnt *
    217 alpha_pci_generic_intr_evcnt(pci_chipset_tag_t const pc,
    218     pci_intr_handle_t const ih)
    219 {
    220 	const u_int irq = alpha_pci_intr_handle_get_irq(&ih);
    221 
    222 	KASSERT(irq < pc->pc_nirq);
    223 
    224 	return alpha_shared_intr_evcnt(pc->pc_shared_intrs, irq);
    225 }
    226 
    227 static struct cpu_info *
    228 alpha_pci_generic_intr_select_cpu(pci_chipset_tag_t const pc, u_int const irq,
    229     u_int const flags)
    230 {
    231 	struct cpu_info *ci, *best_ci;
    232 	CPU_INFO_ITERATOR cii;
    233 
    234 	KASSERT(mutex_owned(&cpu_lock));
    235 
    236 	/*
    237 	 * If the back-end didn't tell us where we can route, then
    238 	 * they all go to the primry CPU.
    239 	 */
    240 	if (pc->pc_eligible_cpus == 0) {
    241 		return &cpu_info_primary;
    242 	}
    243 
    244 	/*
    245 	 * If the interrupt already has a CPU assigned, keep on using it,
    246 	 * unless the CPU has become ineligible.
    247 	 */
    248 	ci = alpha_shared_intr_get_cpu(pc->pc_shared_intrs, irq);
    249 	if (ci != NULL) {
    250 		if ((ci->ci_schedstate.spc_flags & SPCF_NOINTR) == 0 ||
    251 		    CPU_IS_PRIMARY(ci)) {
    252 			return ci;
    253 		}
    254 	}
    255 
    256 	/*
    257 	 * Pick the CPU with the fewest handlers.
    258 	 */
    259 	best_ci = NULL;
    260 	for (CPU_INFO_FOREACH(cii, ci)) {
    261 		if ((pc->pc_eligible_cpus & __BIT(ci->ci_cpuid)) == 0) {
    262 			/* This CPU is not eligible in hardware. */
    263 			continue;
    264 		}
    265 		if (ci->ci_schedstate.spc_flags & SPCF_NOINTR) {
    266 			/* This CPU is not eligible in software. */
    267 			continue;
    268 		}
    269 		if (best_ci == NULL ||
    270 		    ci->ci_nintrhand < best_ci->ci_nintrhand) {
    271 			best_ci = ci;
    272 		}
    273 	}
    274 
    275 	/* If we found one, cool... */
    276 	if (best_ci != NULL) {
    277 		return best_ci;
    278 	}
    279 
    280 	/* ...if not, well I guess we'll just fall back on the primary. */
    281 	return &cpu_info_primary;
    282 }
    283 
    284 void *
    285 alpha_pci_generic_intr_establish(pci_chipset_tag_t const pc,
    286     pci_intr_handle_t const ih, int const level,
    287     int (*func)(void *), void *arg)
    288 {
    289 	const u_int irq = alpha_pci_intr_handle_get_irq(&ih);
    290 	const u_int flags = alpha_pci_intr_handle_get_flags(&ih);
    291 	void *cookie;
    292 
    293 	KASSERT(irq < pc->pc_nirq);
    294 
    295 	cookie = alpha_shared_intr_alloc_intrhand(pc->pc_shared_intrs,
    296 	    irq, IST_LEVEL, level, flags, func, arg, pc->pc_intr_desc);
    297 
    298 	if (cookie == NULL)
    299 		return NULL;
    300 
    301 	mutex_enter(&cpu_lock);
    302 
    303 	struct cpu_info *target_ci =
    304 	    alpha_pci_generic_intr_select_cpu(pc, irq, flags);
    305 	struct cpu_info *current_ci =
    306 	    alpha_shared_intr_get_cpu(pc->pc_shared_intrs, irq);
    307 
    308 	const bool first_handler =
    309 	    ! alpha_shared_intr_isactive(pc->pc_shared_intrs, irq);
    310 
    311 	/*
    312 	 * If this is the first handler on this interrupt, or if the
    313 	 * target CPU has changed, then program the route if the
    314 	 * hardware supports it.
    315 	 */
    316 	if (first_handler || target_ci != current_ci) {
    317 		alpha_shared_intr_set_cpu(pc->pc_shared_intrs, irq, target_ci);
    318 		if (pc->pc_intr_set_affinity != NULL) {
    319 			pc->pc_intr_set_affinity(pc, irq, target_ci);
    320 		}
    321 	}
    322 
    323 	if (! alpha_shared_intr_link(pc->pc_shared_intrs, cookie,
    324 				     pc->pc_intr_desc)) {
    325 		mutex_exit(&cpu_lock);
    326 		alpha_shared_intr_free_intrhand(cookie);
    327 		return NULL;
    328 	}
    329 
    330 	if (first_handler) {
    331 		scb_set(pc->pc_vecbase + SCB_IDXTOVEC(irq),
    332 		    alpha_pci_generic_iointr, pc);
    333 		pc->pc_intr_enable(pc, irq);
    334 	}
    335 
    336 	mutex_exit(&cpu_lock);
    337 
    338 	return cookie;
    339 }
    340 
    341 void
    342 alpha_pci_generic_intr_disestablish(pci_chipset_tag_t const pc,
    343     void * const cookie)
    344 {
    345 	struct alpha_shared_intrhand * const ih = cookie;
    346 	const u_int irq = ih->ih_num;
    347 
    348 	mutex_enter(&cpu_lock);
    349 
    350 	if (alpha_shared_intr_firstactive(pc->pc_shared_intrs, irq)) {
    351 		pc->pc_intr_disable(pc, irq);
    352 		alpha_shared_intr_set_dfltsharetype(pc->pc_shared_intrs,
    353 		    irq, IST_NONE);
    354 		scb_free(pc->pc_vecbase + SCB_IDXTOVEC(irq));
    355 	}
    356 
    357 	alpha_shared_intr_unlink(pc->pc_shared_intrs, cookie, pc->pc_intr_desc);
    358 
    359 	mutex_exit(&cpu_lock);
    360 
    361 	alpha_shared_intr_free_intrhand(cookie);
    362 }
    363 
    364 void
    365 alpha_pci_generic_iointr(void * const arg, unsigned long const vec)
    366 {
    367 	pci_chipset_tag_t const pc = arg;
    368 	const u_int irq = SCB_VECTOIDX(vec - pc->pc_vecbase);
    369 
    370 	if (!alpha_shared_intr_dispatch(pc->pc_shared_intrs, irq)) {
    371 		alpha_shared_intr_stray(pc->pc_shared_intrs, irq,
    372 		    pc->pc_intr_desc);
    373 		if (ALPHA_SHARED_INTR_DISABLE(pc->pc_shared_intrs, irq)) {
    374 			pc->pc_intr_disable(pc, irq);
    375 		}
    376 	} else {
    377 		alpha_shared_intr_reset_strays(pc->pc_shared_intrs, irq);
    378 	}
    379 }
    380 
    381 #define	ALPHA_PCI_INTR_HANDLE_IRQ	__BITS(0,31)
    382 #define	ALPHA_PCI_INTR_HANDLE_FLAGS	__BITS(32,63)
    383 
    384 void
    385 alpha_pci_intr_handle_init(pci_intr_handle_t * const ihp, u_int const irq,
    386     u_int const flags)
    387 {
    388 	ihp->value = __SHIFTIN(irq, ALPHA_PCI_INTR_HANDLE_IRQ) |
    389 	    __SHIFTIN(flags, ALPHA_PCI_INTR_HANDLE_FLAGS);
    390 }
    391 
    392 void
    393 alpha_pci_intr_handle_set_irq(pci_intr_handle_t * const ihp, u_int const irq)
    394 {
    395 	ihp->value = (ihp->value & ALPHA_PCI_INTR_HANDLE_FLAGS) |
    396 	    __SHIFTIN(irq, ALPHA_PCI_INTR_HANDLE_IRQ);
    397 }
    398 
    399 u_int
    400 alpha_pci_intr_handle_get_irq(const pci_intr_handle_t * const ihp)
    401 {
    402 	return __SHIFTOUT(ihp->value, ALPHA_PCI_INTR_HANDLE_IRQ);
    403 }
    404 
    405 void
    406 alpha_pci_intr_handle_set_flags(pci_intr_handle_t * const ihp,
    407     u_int const flags)
    408 {
    409 	ihp->value = (ihp->value & ALPHA_PCI_INTR_HANDLE_IRQ) |
    410 	    __SHIFTIN(flags, ALPHA_PCI_INTR_HANDLE_FLAGS);
    411 }
    412 
    413 u_int
    414 alpha_pci_intr_handle_get_flags(const pci_intr_handle_t * const ihp)
    415 {
    416 	return __SHIFTOUT(ihp->value, ALPHA_PCI_INTR_HANDLE_FLAGS);
    417 }
    418 
    419 /*
    420  * MI PCI back-end entry points.
    421  */
    422 
    423 void
    424 pci_attach_hook(device_t const parent, device_t const self,
    425     struct pcibus_attach_args * const pba)
    426 {
    427 	pci_chipset_tag_t const pc = pba->pba_pc;
    428 
    429 	KASSERT(pc->pc_attach_hook != NULL);
    430 	pc->pc_attach_hook(parent, self, pba);
    431 }
    432 
    433 int
    434 pci_bus_maxdevs(pci_chipset_tag_t const pc, int const busno)
    435 {
    436 	KASSERT(pc->pc_bus_maxdevs != NULL);
    437 	return pc->pc_bus_maxdevs(pc->pc_conf_v, busno);
    438 }
    439 
    440 pcitag_t
    441 pci_make_tag(pci_chipset_tag_t const pc, int const bus, int const dev,
    442     int const func)
    443 {
    444 	KASSERT(pc->pc_make_tag != NULL);
    445 	return pc->pc_make_tag(pc->pc_conf_v, bus, dev, func);
    446 }
    447 
    448 void
    449 pci_decompose_tag(pci_chipset_tag_t const pc, pcitag_t const tag,
    450     int * const busp, int * const devp, int * const funcp)
    451 {
    452 	KASSERT(pc->pc_decompose_tag != NULL);
    453 	pc->pc_decompose_tag(pc->pc_conf_v, tag, busp, devp, funcp);
    454 }
    455 
    456 pcireg_t
    457 pci_conf_read(pci_chipset_tag_t const pc, pcitag_t const tag, int const reg)
    458 {
    459 	KASSERT(pc->pc_conf_read != NULL);
    460 	return pc->pc_conf_read(pc->pc_conf_v, tag, reg);
    461 }
    462 
    463 void
    464 pci_conf_write(pci_chipset_tag_t const pc, pcitag_t const tag, int const reg,
    465     pcireg_t const val)
    466 {
    467 	KASSERT(pc->pc_conf_write != NULL);
    468 	pc->pc_conf_write(pc->pc_conf_v, tag, reg, val);
    469 }
    470 
    471 int
    472 pci_intr_map(const struct pci_attach_args * const pa,
    473     pci_intr_handle_t * const ihp)
    474 {
    475 	pci_chipset_tag_t const pc = pa->pa_pc;
    476 
    477 	KASSERT(pc->pc_intr_map != NULL);
    478 	return pc->pc_intr_map(pa, ihp);
    479 }
    480 
    481 const char *
    482 pci_intr_string(pci_chipset_tag_t const pc, pci_intr_handle_t const ih,
    483     char * const buf, size_t const len)
    484 {
    485 	KASSERT(pc->pc_intr_string != NULL);
    486 	return pc->pc_intr_string(pc, ih, buf, len);
    487 }
    488 
    489 const struct evcnt *
    490 pci_intr_evcnt(pci_chipset_tag_t const pc, pci_intr_handle_t const ih)
    491 {
    492 	KASSERT(pc->pc_intr_evcnt != NULL);
    493 	return pc->pc_intr_evcnt(pc, ih);
    494 }
    495 
    496 void *
    497 pci_intr_establish(pci_chipset_tag_t const pc, pci_intr_handle_t const ih,
    498     int const ipl, int (*func)(void *), void *arg)
    499 {
    500 	KASSERT(pc->pc_intr_establish != NULL);
    501 	return pc->pc_intr_establish(pc, ih, ipl, func, arg);
    502 }
    503 
    504 void
    505 pci_intr_disestablish(pci_chipset_tag_t const pc, void * const cookie)
    506 {
    507 	KASSERT(pc->pc_intr_disestablish != NULL);
    508 	pc->pc_intr_disestablish(pc, cookie);
    509 }
    510 
    511 int
    512 pci_intr_setattr(pci_chipset_tag_t const pc __unused,
    513     pci_intr_handle_t * const ihp, int const attr, uint64_t const data)
    514 {
    515 	u_int flags = alpha_pci_intr_handle_get_flags(ihp);
    516 
    517 	switch (attr) {
    518 	case PCI_INTR_MPSAFE:
    519 		if (data)
    520 			flags |= ALPHA_INTR_MPSAFE;
    521 		else
    522 			flags &= ~ALPHA_INTR_MPSAFE;
    523 		break;
    524 
    525 	default:
    526 		return ENODEV;
    527 	}
    528 
    529 	alpha_pci_intr_handle_set_flags(ihp, flags);
    530 	return 0;
    531 }
    532