1 /* $NetBSD: amdnb_misc.c,v 1.3 2018/12/12 23:35:04 is Exp $ */ 2 /* 3 * Copyright (c) 2012 The NetBSD Foundation, Inc. 4 * All rights reserved. 5 * 6 * This code is derived from software contributed to The NetBSD Foundation 7 * by Christoph Egger. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 19 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 20 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 22 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28 * POSSIBILITY OF SUCH DAMAGE. 29 */ 30 31 #include <sys/cdefs.h> 32 __KERNEL_RCSID(0, "$NetBSD: amdnb_misc.c,v 1.3 2018/12/12 23:35:04 is Exp $"); 33 34 #include <sys/param.h> 35 #include <sys/device.h> 36 37 #include <dev/pci/pcireg.h> 38 #include <dev/pci/pcivar.h> 39 #include <dev/pci/pcidevs.h> 40 41 static int amdnb_misc_match(device_t, cfdata_t match, void *); 42 static void amdnb_misc_attach(device_t, device_t, void *); 43 static int amdnb_misc_detach(device_t, int); 44 static int amdnb_misc_rescan(device_t, const char *, const int *); 45 static void amdnb_misc_childdet(device_t, device_t); 46 47 struct amdnb_misc_softc { 48 struct pci_attach_args sc_pa; 49 }; 50 51 CFATTACH_DECL3_NEW(amdnb_misc, sizeof(struct amdnb_misc_softc), 52 amdnb_misc_match, amdnb_misc_attach, amdnb_misc_detach, NULL, 53 amdnb_misc_rescan, amdnb_misc_childdet, DVF_DETACH_SHUTDOWN); 54 55 56 static int 57 amdnb_misc_match(device_t parent, cfdata_t match, void *aux) 58 { 59 struct pci_attach_args *pa = aux; 60 61 if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_AMD) 62 return 0; 63 64 switch (PCI_PRODUCT(pa->pa_id)) { 65 case PCI_PRODUCT_AMD_AMD64_MISC: 66 case PCI_PRODUCT_AMD_AMD64_F10_MISC: 67 case PCI_PRODUCT_AMD_AMD64_F11_MISC: 68 case PCI_PRODUCT_AMD_F14_NB: /* Family 12h, too */ 69 case PCI_PRODUCT_AMD_F15_MISC: 70 case PCI_PRODUCT_AMD_F16_NB: 71 case PCI_PRODUCT_AMD_F16_30_NB: 72 break; 73 default: 74 return 0; 75 } 76 77 return 2; /* supercede pchb(4) */ 78 } 79 80 static int 81 amdnb_misc_search(device_t parent, cfdata_t cf, const int *locs, void *aux) 82 { 83 device_t dev; 84 deviter_t di; 85 bool attach; 86 87 if (!config_match(parent, cf, aux)) 88 return 0; 89 90 attach = true; 91 92 /* Figure out if found child 'cf' is already attached. 93 * No need to attach it twice. 94 */ 95 96 /* XXX: I only want to iterate over the children of *this* device. 97 * Can we introduce a 98 * deviter_first_child(&di, parent, DEVITER_F_LEAVES_ONLY) 99 * or even better, can we introduce a query function that returns 100 * if a child is already attached? 101 */ 102 for (dev = deviter_first(&di, DEVITER_F_LEAVES_FIRST); dev != NULL; 103 dev = deviter_next(&di)) 104 { 105 if (device_parent(dev) != parent) 106 continue; 107 if (device_is_a(dev, cf->cf_name)) { 108 attach = false; 109 break; 110 } 111 } 112 deviter_release(&di); 113 114 if (!attach) 115 return 0; 116 117 config_attach_loc(parent, cf, locs, aux, NULL); 118 119 return 0; 120 } 121 122 static void 123 amdnb_misc_attach(device_t parent, device_t self, void *aux) 124 { 125 struct amdnb_misc_softc *sc = device_private(self); 126 struct pci_attach_args *pa = aux; 127 128 sc->sc_pa = *pa; 129 130 aprint_naive("\n"); 131 aprint_normal(": AMD NB Misc Configuration\n"); 132 133 if (!pmf_device_register(self, NULL, NULL)) 134 aprint_error_dev(self, "couldn't establish power handler\n"); 135 136 config_search_loc(amdnb_misc_search, self, "amdnb_miscbus", 137 NULL, &sc->sc_pa); 138 139 return; 140 } 141 142 static int 143 amdnb_misc_detach(device_t self, int flags) 144 { 145 int rv; 146 147 rv = config_detach_children(self, flags); 148 if (rv != 0) 149 return rv; 150 151 pmf_device_deregister(self); 152 return rv; 153 } 154 155 static int 156 amdnb_misc_rescan(device_t self, const char *ifattr, const int *locators) 157 { 158 struct amdnb_misc_softc *sc = device_private(self); 159 160 if (!ifattr_match(ifattr, "amdnb_miscbus")) 161 return 0; 162 163 config_search_loc(amdnb_misc_search, self, ifattr, 164 locators, &sc->sc_pa); 165 166 return 0; 167 } 168 169 static void 170 amdnb_misc_childdet(device_t self, device_t child) 171 { 172 return; 173 } 174