100}; 101 102static devclass_t pcib_devclass; 103 104DEFINE_CLASS_1(pcib, apb_driver, apb_methods, sizeof(struct apb_softc), 105 pcib_driver); 106EARLY_DRIVER_MODULE(apb, pci, apb_driver, pcib_devclass, 0, 0, BUS_PASS_BUS); 107MODULE_DEPEND(apb, pci, 1, 1, 1); 108 109/* APB specific registers */ 110#define APBR_IOMAP 0xde 111#define APBR_MEMMAP 0xdf 112 113/* Definitions for the mapping registers */ 114#define APB_IO_SCALE 0x200000 115#define APB_MEM_SCALE 0x20000000 116 117/* 118 * Generic device interface 119 */ 120static int 121apb_probe(device_t dev) 122{ 123 124 if (pci_get_vendor(dev) == 0x108e && /* Sun */ 125 pci_get_device(dev) == 0x5000) { /* APB */ 126 device_set_desc(dev, "APB PCI-PCI bridge"); 127 return (0); 128 } 129 return (ENXIO); 130} 131 132static void 133apb_map_print(uint8_t map, u_long scale) 134{ 135 int i, first; 136 137 for (first = 1, i = 0; i < 8; i++) { 138 if ((map & (1 << i)) != 0) { 139 printf("%s0x%lx-0x%lx", first ? "" : ", ", 140 i * scale, (i + 1) * scale - 1); 141 first = 0; 142 } 143 } 144} 145 146static int 147apb_checkrange(uint8_t map, u_long scale, u_long start, u_long end) 148{ 149 int i, ei; 150 151 i = start / scale; 152 ei = end / scale; 153 if (i > 7 || ei > 7) 154 return (0); 155 for (; i <= ei; i++) 156 if ((map & (1 << i)) == 0) 157 return (0); 158 return (1); 159} 160 161static int 162apb_attach(device_t dev) 163{ 164 struct apb_softc *sc; 165 struct sysctl_ctx_list *sctx; 166 struct sysctl_oid *soid; 167 168 sc = device_get_softc(dev); 169 170 /* 171 * Get current bridge configuration. 172 */ 173 sc->sc_bsc.ops_pcib_sc.domain = pci_get_domain(dev); 174 sc->sc_bsc.ops_pcib_sc.secstat = 175 pci_read_config(dev, PCIR_SECSTAT_1, 2); 176 sc->sc_bsc.ops_pcib_sc.command = 177 pci_read_config(dev, PCIR_COMMAND, 2); 178 sc->sc_bsc.ops_pcib_sc.pribus = 179 pci_read_config(dev, PCIR_PRIBUS_1, 1); 180 sc->sc_bsc.ops_pcib_sc.secbus = 181 pci_read_config(dev, PCIR_SECBUS_1, 1); 182 sc->sc_bsc.ops_pcib_sc.subbus = 183 pci_read_config(dev, PCIR_SUBBUS_1, 1); 184 sc->sc_bsc.ops_pcib_sc.bridgectl = 185 pci_read_config(dev, PCIR_BRIDGECTL_1, 2); 186 sc->sc_bsc.ops_pcib_sc.seclat = 187 pci_read_config(dev, PCIR_SECLAT_1, 1); 188 sc->sc_iomap = pci_read_config(dev, APBR_IOMAP, 1); 189 sc->sc_memmap = pci_read_config(dev, APBR_MEMMAP, 1); 190 191 /* 192 * Setup SYSCTL reporting nodes. 193 */ 194 sctx = device_get_sysctl_ctx(dev); 195 soid = device_get_sysctl_tree(dev); 196 SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "domain", 197 CTLFLAG_RD, &sc->sc_bsc.ops_pcib_sc.domain, 0, 198 "Domain number"); 199 SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "pribus", 200 CTLFLAG_RD, &sc->sc_bsc.ops_pcib_sc.pribus, 0, 201 "Primary bus number"); 202 SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "secbus", 203 CTLFLAG_RD, &sc->sc_bsc.ops_pcib_sc.secbus, 0, 204 "Secondary bus number"); 205 SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "subbus", 206 CTLFLAG_RD, &sc->sc_bsc.ops_pcib_sc.subbus, 0, 207 "Subordinate bus number"); 208 209 ofw_pcib_gen_setup(dev); 210 211 if (bootverbose) { 212 device_printf(dev, " domain %d\n", 213 sc->sc_bsc.ops_pcib_sc.domain); 214 device_printf(dev, " secondary bus %d\n", 215 sc->sc_bsc.ops_pcib_sc.secbus); 216 device_printf(dev, " subordinate bus %d\n", 217 sc->sc_bsc.ops_pcib_sc.subbus); 218 device_printf(dev, " I/O decode "); 219 apb_map_print(sc->sc_iomap, APB_IO_SCALE); 220 printf("\n"); 221 device_printf(dev, " memory decode "); 222 apb_map_print(sc->sc_memmap, APB_MEM_SCALE); 223 printf("\n"); 224 } 225 226 device_add_child(dev, "pci", -1); 227 return (bus_generic_attach(dev)); 228} 229 230/* 231 * We have to trap resource allocation requests and ensure that the bridge 232 * is set up to, or capable of handling them. 233 */ 234static struct resource * 235apb_alloc_resource(device_t dev, device_t child, int type, int *rid, 236 u_long start, u_long end, u_long count, u_int flags) 237{ 238 struct apb_softc *sc; 239 240 sc = device_get_softc(dev); 241 242 /* 243 * If this is a "default" allocation against this rid, we can't work 244 * out where it's coming from (we should actually never see these) so 245 * we just have to punt. 246 */ 247 if (start == 0 && end == ~0) { 248 device_printf(dev, "can't decode default resource id %d for " 249 "%s, bypassing\n", *rid, device_get_nameunit(child)); 250 goto passup; 251 } 252 253 /* 254 * Fail the allocation for this range if it's not supported. 255 * XXX we should probably just fix up the bridge decode and 256 * soldier on. 257 */ 258 switch (type) { 259 case SYS_RES_IOPORT: 260 if (!apb_checkrange(sc->sc_iomap, APB_IO_SCALE, start, end)) { 261 device_printf(dev, "device %s requested unsupported " 262 "I/O range 0x%lx-0x%lx\n", 263 device_get_nameunit(child), start, end); 264 return (NULL); 265 } 266 if (bootverbose) 267 device_printf(sc->sc_bsc.ops_pcib_sc.dev, "device " 268 "%s requested decoded I/O range 0x%lx-0x%lx\n", 269 device_get_nameunit(child), start, end); 270 break; 271 case SYS_RES_MEMORY: 272 if (!apb_checkrange(sc->sc_memmap, APB_MEM_SCALE, start, 273 end)) { 274 device_printf(dev, "device %s requested unsupported " 275 "memory range 0x%lx-0x%lx\n", 276 device_get_nameunit(child), start, end); 277 return (NULL); 278 } 279 if (bootverbose) 280 device_printf(sc->sc_bsc.ops_pcib_sc.dev, "device " 281 "%s requested decoded memory range 0x%lx-0x%lx\n", 282 device_get_nameunit(child), start, end); 283 break; 284 } 285 286 passup: 287 /* 288 * Bridge is OK decoding this resource, so pass it up. 289 */ 290 return (bus_generic_alloc_resource(dev, child, type, rid, start, end, 291 count, flags)); 292} 293 294static int 295apb_adjust_resource(device_t dev, device_t child, int type, 296 struct resource *r, u_long start, u_long end) 297{ 298 struct apb_softc *sc; 299 300 sc = device_get_softc(dev); 301 switch (type) { 302 case SYS_RES_IOPORT: 303 if (!apb_checkrange(sc->sc_iomap, APB_IO_SCALE, start, end)) 304 return (ENXIO); 305 break; 306 case SYS_RES_MEMORY: 307 if (!apb_checkrange(sc->sc_memmap, APB_MEM_SCALE, start, end)) 308 return (ENXIO); 309 break; 310 } 311 return (bus_generic_adjust_resource(dev, child, type, r, start, end)); 312}
| 100}; 101 102static devclass_t pcib_devclass; 103 104DEFINE_CLASS_1(pcib, apb_driver, apb_methods, sizeof(struct apb_softc), 105 pcib_driver); 106EARLY_DRIVER_MODULE(apb, pci, apb_driver, pcib_devclass, 0, 0, BUS_PASS_BUS); 107MODULE_DEPEND(apb, pci, 1, 1, 1); 108 109/* APB specific registers */ 110#define APBR_IOMAP 0xde 111#define APBR_MEMMAP 0xdf 112 113/* Definitions for the mapping registers */ 114#define APB_IO_SCALE 0x200000 115#define APB_MEM_SCALE 0x20000000 116 117/* 118 * Generic device interface 119 */ 120static int 121apb_probe(device_t dev) 122{ 123 124 if (pci_get_vendor(dev) == 0x108e && /* Sun */ 125 pci_get_device(dev) == 0x5000) { /* APB */ 126 device_set_desc(dev, "APB PCI-PCI bridge"); 127 return (0); 128 } 129 return (ENXIO); 130} 131 132static void 133apb_map_print(uint8_t map, u_long scale) 134{ 135 int i, first; 136 137 for (first = 1, i = 0; i < 8; i++) { 138 if ((map & (1 << i)) != 0) { 139 printf("%s0x%lx-0x%lx", first ? "" : ", ", 140 i * scale, (i + 1) * scale - 1); 141 first = 0; 142 } 143 } 144} 145 146static int 147apb_checkrange(uint8_t map, u_long scale, u_long start, u_long end) 148{ 149 int i, ei; 150 151 i = start / scale; 152 ei = end / scale; 153 if (i > 7 || ei > 7) 154 return (0); 155 for (; i <= ei; i++) 156 if ((map & (1 << i)) == 0) 157 return (0); 158 return (1); 159} 160 161static int 162apb_attach(device_t dev) 163{ 164 struct apb_softc *sc; 165 struct sysctl_ctx_list *sctx; 166 struct sysctl_oid *soid; 167 168 sc = device_get_softc(dev); 169 170 /* 171 * Get current bridge configuration. 172 */ 173 sc->sc_bsc.ops_pcib_sc.domain = pci_get_domain(dev); 174 sc->sc_bsc.ops_pcib_sc.secstat = 175 pci_read_config(dev, PCIR_SECSTAT_1, 2); 176 sc->sc_bsc.ops_pcib_sc.command = 177 pci_read_config(dev, PCIR_COMMAND, 2); 178 sc->sc_bsc.ops_pcib_sc.pribus = 179 pci_read_config(dev, PCIR_PRIBUS_1, 1); 180 sc->sc_bsc.ops_pcib_sc.secbus = 181 pci_read_config(dev, PCIR_SECBUS_1, 1); 182 sc->sc_bsc.ops_pcib_sc.subbus = 183 pci_read_config(dev, PCIR_SUBBUS_1, 1); 184 sc->sc_bsc.ops_pcib_sc.bridgectl = 185 pci_read_config(dev, PCIR_BRIDGECTL_1, 2); 186 sc->sc_bsc.ops_pcib_sc.seclat = 187 pci_read_config(dev, PCIR_SECLAT_1, 1); 188 sc->sc_iomap = pci_read_config(dev, APBR_IOMAP, 1); 189 sc->sc_memmap = pci_read_config(dev, APBR_MEMMAP, 1); 190 191 /* 192 * Setup SYSCTL reporting nodes. 193 */ 194 sctx = device_get_sysctl_ctx(dev); 195 soid = device_get_sysctl_tree(dev); 196 SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "domain", 197 CTLFLAG_RD, &sc->sc_bsc.ops_pcib_sc.domain, 0, 198 "Domain number"); 199 SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "pribus", 200 CTLFLAG_RD, &sc->sc_bsc.ops_pcib_sc.pribus, 0, 201 "Primary bus number"); 202 SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "secbus", 203 CTLFLAG_RD, &sc->sc_bsc.ops_pcib_sc.secbus, 0, 204 "Secondary bus number"); 205 SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "subbus", 206 CTLFLAG_RD, &sc->sc_bsc.ops_pcib_sc.subbus, 0, 207 "Subordinate bus number"); 208 209 ofw_pcib_gen_setup(dev); 210 211 if (bootverbose) { 212 device_printf(dev, " domain %d\n", 213 sc->sc_bsc.ops_pcib_sc.domain); 214 device_printf(dev, " secondary bus %d\n", 215 sc->sc_bsc.ops_pcib_sc.secbus); 216 device_printf(dev, " subordinate bus %d\n", 217 sc->sc_bsc.ops_pcib_sc.subbus); 218 device_printf(dev, " I/O decode "); 219 apb_map_print(sc->sc_iomap, APB_IO_SCALE); 220 printf("\n"); 221 device_printf(dev, " memory decode "); 222 apb_map_print(sc->sc_memmap, APB_MEM_SCALE); 223 printf("\n"); 224 } 225 226 device_add_child(dev, "pci", -1); 227 return (bus_generic_attach(dev)); 228} 229 230/* 231 * We have to trap resource allocation requests and ensure that the bridge 232 * is set up to, or capable of handling them. 233 */ 234static struct resource * 235apb_alloc_resource(device_t dev, device_t child, int type, int *rid, 236 u_long start, u_long end, u_long count, u_int flags) 237{ 238 struct apb_softc *sc; 239 240 sc = device_get_softc(dev); 241 242 /* 243 * If this is a "default" allocation against this rid, we can't work 244 * out where it's coming from (we should actually never see these) so 245 * we just have to punt. 246 */ 247 if (start == 0 && end == ~0) { 248 device_printf(dev, "can't decode default resource id %d for " 249 "%s, bypassing\n", *rid, device_get_nameunit(child)); 250 goto passup; 251 } 252 253 /* 254 * Fail the allocation for this range if it's not supported. 255 * XXX we should probably just fix up the bridge decode and 256 * soldier on. 257 */ 258 switch (type) { 259 case SYS_RES_IOPORT: 260 if (!apb_checkrange(sc->sc_iomap, APB_IO_SCALE, start, end)) { 261 device_printf(dev, "device %s requested unsupported " 262 "I/O range 0x%lx-0x%lx\n", 263 device_get_nameunit(child), start, end); 264 return (NULL); 265 } 266 if (bootverbose) 267 device_printf(sc->sc_bsc.ops_pcib_sc.dev, "device " 268 "%s requested decoded I/O range 0x%lx-0x%lx\n", 269 device_get_nameunit(child), start, end); 270 break; 271 case SYS_RES_MEMORY: 272 if (!apb_checkrange(sc->sc_memmap, APB_MEM_SCALE, start, 273 end)) { 274 device_printf(dev, "device %s requested unsupported " 275 "memory range 0x%lx-0x%lx\n", 276 device_get_nameunit(child), start, end); 277 return (NULL); 278 } 279 if (bootverbose) 280 device_printf(sc->sc_bsc.ops_pcib_sc.dev, "device " 281 "%s requested decoded memory range 0x%lx-0x%lx\n", 282 device_get_nameunit(child), start, end); 283 break; 284 } 285 286 passup: 287 /* 288 * Bridge is OK decoding this resource, so pass it up. 289 */ 290 return (bus_generic_alloc_resource(dev, child, type, rid, start, end, 291 count, flags)); 292} 293 294static int 295apb_adjust_resource(device_t dev, device_t child, int type, 296 struct resource *r, u_long start, u_long end) 297{ 298 struct apb_softc *sc; 299 300 sc = device_get_softc(dev); 301 switch (type) { 302 case SYS_RES_IOPORT: 303 if (!apb_checkrange(sc->sc_iomap, APB_IO_SCALE, start, end)) 304 return (ENXIO); 305 break; 306 case SYS_RES_MEMORY: 307 if (!apb_checkrange(sc->sc_memmap, APB_MEM_SCALE, start, end)) 308 return (ENXIO); 309 break; 310 } 311 return (bus_generic_adjust_resource(dev, child, type, r, start, end)); 312}
|