xref: /freebsd/sys/x86/isa/isa.c (revision 2dd1bdf1834c53d048d3d9a7079b45afea5cecd7)
132580301SAttilio Rao /*-
232580301SAttilio Rao  * Copyright (c) 1998 Doug Rabson
332580301SAttilio Rao  * All rights reserved.
432580301SAttilio Rao  *
532580301SAttilio Rao  * Redistribution and use in source and binary forms, with or without
632580301SAttilio Rao  * modification, are permitted provided that the following conditions
732580301SAttilio Rao  * are met:
832580301SAttilio Rao  * 1. Redistributions of source code must retain the above copyright
932580301SAttilio Rao  *    notice, this list of conditions and the following disclaimer.
1032580301SAttilio Rao  * 2. Redistributions in binary form must reproduce the above copyright
1132580301SAttilio Rao  *    notice, this list of conditions and the following disclaimer in the
1232580301SAttilio Rao  *    documentation and/or other materials provided with the distribution.
1332580301SAttilio Rao  *
1432580301SAttilio Rao  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1532580301SAttilio Rao  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1632580301SAttilio Rao  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1732580301SAttilio Rao  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1832580301SAttilio Rao  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1932580301SAttilio Rao  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2032580301SAttilio Rao  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2132580301SAttilio Rao  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2232580301SAttilio Rao  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2332580301SAttilio Rao  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2432580301SAttilio Rao  * SUCH DAMAGE.
2532580301SAttilio Rao  */
2632580301SAttilio Rao 
2732580301SAttilio Rao #include <sys/cdefs.h>
2832580301SAttilio Rao __FBSDID("$FreeBSD$");
2932580301SAttilio Rao 
3032580301SAttilio Rao /*-
3132580301SAttilio Rao  * Modifications for Intel architecture by Garrett A. Wollman.
3232580301SAttilio Rao  * Copyright 1998 Massachusetts Institute of Technology
3332580301SAttilio Rao  *
3432580301SAttilio Rao  * Permission to use, copy, modify, and distribute this software and
3532580301SAttilio Rao  * its documentation for any purpose and without fee is hereby
3632580301SAttilio Rao  * granted, provided that both the above copyright notice and this
3732580301SAttilio Rao  * permission notice appear in all copies, that both the above
3832580301SAttilio Rao  * copyright notice and this permission notice appear in all
3932580301SAttilio Rao  * supporting documentation, and that the name of M.I.T. not be used
4032580301SAttilio Rao  * in advertising or publicity pertaining to distribution of the
4132580301SAttilio Rao  * software without specific, written prior permission.  M.I.T. makes
4232580301SAttilio Rao  * no representations about the suitability of this software for any
4332580301SAttilio Rao  * purpose.  It is provided "as is" without express or implied
4432580301SAttilio Rao  * warranty.
4532580301SAttilio Rao  *
4632580301SAttilio Rao  * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''.  M.I.T. DISCLAIMS
4732580301SAttilio Rao  * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
4832580301SAttilio Rao  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
4932580301SAttilio Rao  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
5032580301SAttilio Rao  * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
5132580301SAttilio Rao  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
5232580301SAttilio Rao  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
5332580301SAttilio Rao  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
5432580301SAttilio Rao  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
5532580301SAttilio Rao  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
5632580301SAttilio Rao  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5732580301SAttilio Rao  * SUCH DAMAGE.
5832580301SAttilio Rao  */
5932580301SAttilio Rao 
6032580301SAttilio Rao #include <sys/param.h>
6132580301SAttilio Rao #include <sys/bus.h>
6232580301SAttilio Rao #include <sys/kernel.h>
6332580301SAttilio Rao #include <sys/malloc.h>
6432580301SAttilio Rao #include <sys/module.h>
6532580301SAttilio Rao #include <machine/bus.h>
6632580301SAttilio Rao #include <sys/rman.h>
6732580301SAttilio Rao #ifdef PC98
6832580301SAttilio Rao #include <sys/systm.h>
6932580301SAttilio Rao #endif
7032580301SAttilio Rao 
7132580301SAttilio Rao #include <machine/resource.h>
7232580301SAttilio Rao 
7332580301SAttilio Rao #include <isa/isavar.h>
7432580301SAttilio Rao #include <isa/isa_common.h>
7532580301SAttilio Rao 
7632580301SAttilio Rao void
7732580301SAttilio Rao isa_init(device_t dev)
7832580301SAttilio Rao {
7932580301SAttilio Rao }
8032580301SAttilio Rao 
8132580301SAttilio Rao /*
8232580301SAttilio Rao  * This implementation simply passes the request up to the parent
8332580301SAttilio Rao  * bus, which in our case is the special i386 nexus, substituting any
8432580301SAttilio Rao  * configured values if the caller defaulted.  We can get away with
8532580301SAttilio Rao  * this because there is no special mapping for ISA resources on an Intel
8632580301SAttilio Rao  * platform.  When porting this code to another architecture, it may be
8732580301SAttilio Rao  * necessary to interpose a mapping layer here.
8832580301SAttilio Rao  */
8932580301SAttilio Rao struct resource *
9032580301SAttilio Rao isa_alloc_resource(device_t bus, device_t child, int type, int *rid,
91*2dd1bdf1SJustin Hibbits 		   rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
9232580301SAttilio Rao {
9332580301SAttilio Rao 	/*
9432580301SAttilio Rao 	 * Consider adding a resource definition.
9532580301SAttilio Rao 	 */
9632580301SAttilio Rao 	int passthrough = (device_get_parent(child) != bus);
9732580301SAttilio Rao 	int isdefault = (start == 0UL && end == ~0UL);
9832580301SAttilio Rao 	struct isa_device* idev = DEVTOISA(child);
9932580301SAttilio Rao 	struct resource_list *rl = &idev->id_resources;
10032580301SAttilio Rao 	struct resource_list_entry *rle;
10132580301SAttilio Rao 
10232580301SAttilio Rao 	if (!passthrough && !isdefault) {
10332580301SAttilio Rao 		rle = resource_list_find(rl, type, *rid);
10432580301SAttilio Rao 		if (!rle) {
10532580301SAttilio Rao 			if (*rid < 0)
10632580301SAttilio Rao 				return 0;
10732580301SAttilio Rao 			switch (type) {
10832580301SAttilio Rao 			case SYS_RES_IRQ:
10932580301SAttilio Rao 				if (*rid >= ISA_NIRQ)
11032580301SAttilio Rao 					return 0;
11132580301SAttilio Rao 				break;
11232580301SAttilio Rao 			case SYS_RES_DRQ:
11332580301SAttilio Rao 				if (*rid >= ISA_NDRQ)
11432580301SAttilio Rao 					return 0;
11532580301SAttilio Rao 				break;
11632580301SAttilio Rao 			case SYS_RES_MEMORY:
11732580301SAttilio Rao 				if (*rid >= ISA_NMEM)
11832580301SAttilio Rao 					return 0;
11932580301SAttilio Rao 				break;
12032580301SAttilio Rao 			case SYS_RES_IOPORT:
12132580301SAttilio Rao 				if (*rid >= ISA_NPORT)
12232580301SAttilio Rao 					return 0;
12332580301SAttilio Rao 				break;
12432580301SAttilio Rao 			default:
12532580301SAttilio Rao 				return 0;
12632580301SAttilio Rao 			}
12732580301SAttilio Rao 			resource_list_add(rl, type, *rid, start, end, count);
12832580301SAttilio Rao 		}
12932580301SAttilio Rao 	}
13032580301SAttilio Rao 
13132580301SAttilio Rao 	return resource_list_alloc(rl, bus, child, type, rid,
13232580301SAttilio Rao 				   start, end, count, flags);
13332580301SAttilio Rao }
13432580301SAttilio Rao 
13532580301SAttilio Rao #ifdef PC98
13632580301SAttilio Rao /*
13732580301SAttilio Rao  * Indirection support.  The type of bus_space_handle_t is
13832580301SAttilio Rao  * defined in sys/i386/include/bus_pc98.h.
13932580301SAttilio Rao  */
14032580301SAttilio Rao struct resource *
14132580301SAttilio Rao isa_alloc_resourcev(device_t child, int type, int *rid,
14232580301SAttilio Rao 		    bus_addr_t *res, bus_size_t count, u_int flags)
14332580301SAttilio Rao {
14432580301SAttilio Rao 	struct isa_device* idev = DEVTOISA(child);
14532580301SAttilio Rao 	struct resource_list *rl = &idev->id_resources;
14632580301SAttilio Rao 
14732580301SAttilio Rao 	device_t	bus = device_get_parent(child);
14832580301SAttilio Rao 	bus_addr_t	start;
14932580301SAttilio Rao 	bus_space_handle_t bh;
15032580301SAttilio Rao 	struct resource *re;
15132580301SAttilio Rao 	struct resource	**bsre;
15232580301SAttilio Rao 	int		i, j, k, linear_cnt, ressz, bsrid;
15332580301SAttilio Rao 
15432580301SAttilio Rao 	start = bus_get_resource_start(child, type, *rid);
15532580301SAttilio Rao 
15632580301SAttilio Rao 	linear_cnt = count;
15732580301SAttilio Rao 	ressz = 1;
15832580301SAttilio Rao 	for (i = 1; i < count; ++i) {
15932580301SAttilio Rao 		if (res[i] != res[i - 1] + 1) {
16032580301SAttilio Rao 			if (i < linear_cnt)
16132580301SAttilio Rao 				linear_cnt = i;
16232580301SAttilio Rao 			++ressz;
16332580301SAttilio Rao 		}
16432580301SAttilio Rao 	}
16532580301SAttilio Rao 
16632580301SAttilio Rao 	re = isa_alloc_resource(bus, child, type, rid,
16732580301SAttilio Rao 				start + res[0], start + res[linear_cnt - 1],
16832580301SAttilio Rao 				linear_cnt, flags);
16932580301SAttilio Rao 	if (re == NULL)
17032580301SAttilio Rao 		return NULL;
17132580301SAttilio Rao 
17232580301SAttilio Rao 	bsre = malloc(sizeof (struct resource *) * ressz, M_DEVBUF, M_NOWAIT);
17332580301SAttilio Rao 	if (bsre == NULL) {
17432580301SAttilio Rao 		resource_list_release(rl, bus, child, type, *rid, re);
17532580301SAttilio Rao 		return NULL;
17632580301SAttilio Rao 	}
17732580301SAttilio Rao 	bsre[0] = re;
17832580301SAttilio Rao 
17932580301SAttilio Rao 	for (i = linear_cnt, k = 1; i < count; i = j, k++) {
18032580301SAttilio Rao 		for (j = i + 1; j < count; j++) {
18132580301SAttilio Rao 			if (res[j] != res[j - 1] + 1)
18232580301SAttilio Rao 				break;
18332580301SAttilio Rao 		}
18432580301SAttilio Rao 		bsrid = *rid + k;
18532580301SAttilio Rao 		bsre[k] = isa_alloc_resource(bus, child, type, &bsrid,
18632580301SAttilio Rao 			start + res[i], start + res[j - 1], j - i, flags);
18732580301SAttilio Rao 		if (bsre[k] == NULL) {
18832580301SAttilio Rao 			for (k--; k >= 0; k--)
18932580301SAttilio Rao 				resource_list_release(rl, bus, child, type,
19032580301SAttilio Rao 						      *rid + k, bsre[k]);
19132580301SAttilio Rao 			free(bsre, M_DEVBUF);
19232580301SAttilio Rao 			return NULL;
19332580301SAttilio Rao 		}
19432580301SAttilio Rao 	}
19532580301SAttilio Rao 
19632580301SAttilio Rao 	bh = rman_get_bushandle(re);
19732580301SAttilio Rao 	bh->bsh_res = bsre;
19832580301SAttilio Rao 	bh->bsh_ressz = ressz;
19932580301SAttilio Rao 
20032580301SAttilio Rao 	return re;
20132580301SAttilio Rao }
20232580301SAttilio Rao 
20332580301SAttilio Rao int
20432580301SAttilio Rao isa_load_resourcev(struct resource *re, bus_addr_t *res, bus_size_t count)
20532580301SAttilio Rao {
20632580301SAttilio Rao 
20732580301SAttilio Rao 	return bus_space_map_load(rman_get_bustag(re), rman_get_bushandle(re),
20832580301SAttilio Rao 				  count, res, 0);
20932580301SAttilio Rao }
21032580301SAttilio Rao #endif	/* PC98 */
21132580301SAttilio Rao 
21232580301SAttilio Rao int
21332580301SAttilio Rao isa_release_resource(device_t bus, device_t child, int type, int rid,
21432580301SAttilio Rao 		     struct resource *r)
21532580301SAttilio Rao {
21632580301SAttilio Rao 	struct isa_device* idev = DEVTOISA(child);
21732580301SAttilio Rao 	struct resource_list *rl = &idev->id_resources;
21832580301SAttilio Rao #ifdef PC98
21932580301SAttilio Rao 	/*
22032580301SAttilio Rao 	 * Indirection support.  The type of bus_space_handle_t is
22132580301SAttilio Rao 	 * defined in sys/i386/include/bus_pc98.h.
22232580301SAttilio Rao 	 */
22332580301SAttilio Rao 	int	i;
22432580301SAttilio Rao 	bus_space_handle_t bh;
22532580301SAttilio Rao 
22632580301SAttilio Rao 	if (type == SYS_RES_MEMORY || type == SYS_RES_IOPORT) {
22732580301SAttilio Rao 		bh = rman_get_bushandle(r);
22832580301SAttilio Rao 		if (bh != NULL) {
22932580301SAttilio Rao 			for (i = 1; i < bh->bsh_ressz; i++)
23032580301SAttilio Rao 				resource_list_release(rl, bus, child, type,
23132580301SAttilio Rao 						      rid + i, bh->bsh_res[i]);
23232580301SAttilio Rao 			if (bh->bsh_res != NULL)
23332580301SAttilio Rao 				free(bh->bsh_res, M_DEVBUF);
23432580301SAttilio Rao 		}
23532580301SAttilio Rao 	}
23632580301SAttilio Rao #endif
23732580301SAttilio Rao 	return resource_list_release(rl, bus, child, type, rid, r);
23832580301SAttilio Rao }
23932580301SAttilio Rao 
24032580301SAttilio Rao /*
24132580301SAttilio Rao  * On this platform, isa can also attach to the legacy bus.
24232580301SAttilio Rao  */
24332580301SAttilio Rao DRIVER_MODULE(isa, legacy, isa_driver, isa_devclass, 0, 0);
24479cd455eSRoger Pau Monné 
24579cd455eSRoger Pau Monné /*
24679cd455eSRoger Pau Monné  * Attach the ISA bus to the xenpv bus in order to get syscons.
24779cd455eSRoger Pau Monné  */
24879cd455eSRoger Pau Monné DRIVER_MODULE(isa, xenpv, isa_driver, isa_devclass, 0, 0);
249