Home | History | Annotate | Line # | Download | only in pci
      1 /*	$NetBSD: agpvar.h,v 1.24 2026/06/21 18:38:35 andvar Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2000 Doug Rabson
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  * SUCH DAMAGE.
     27  *
     28  *	$FreeBSD: src/sys/pci/agppriv.h,v 1.3 2000/07/12 10:13:04 dfr Exp $
     29  */
     30 
     31 #ifndef _PCI_AGPVAR_H_
     32 #define _PCI_AGPVAR_H_
     33 
     34 #include <sys/mallocvar.h>
     35 #include <sys/mutex.h>
     36 
     37 #include <dev/pci/pcivar.h>
     38 
     39 struct agpbus_attach_args {
     40 	char	*_apa_busname; /* XXX placeholder */
     41 	struct pci_attach_args apa_pci_args;
     42 };
     43 
     44 /*
     45  * The AGP chipset can be acquired by user or kernel code. If the
     46  * chipset has already been acquired, it cannot be acquired by another
     47  * user until the previous user has released it.
     48  */
     49 enum agp_acquire_state {
     50 	AGP_ACQUIRE_FREE,
     51 	AGP_ACQUIRE_USER,
     52 	AGP_ACQUIRE_KERNEL
     53 };
     54 
     55 /*
     56  * This structure is used to query the state of the AGP system.
     57  */
     58 struct agp_info {
     59 	u_int32_t	ai_mode;
     60 	bus_addr_t	ai_aperture_base;
     61 	bus_size_t	ai_aperture_size;
     62 	vsize_t		ai_memory_allowed;
     63 	vsize_t		ai_memory_used;
     64 	u_int32_t	ai_devid;
     65 };
     66 
     67 struct agp_memory_info {
     68 	vsize_t		ami_size;	/* size in bytes */
     69 	bus_addr_t	ami_physical;	/* bogus hack for i810 */
     70 	off_t		ami_offset;	/* page offset if bound */
     71 	int		ami_is_bound;	/* non-zero if bound */
     72 };
     73 
     74 #ifdef AGP_DEBUG
     75 #define AGP_DPF(x) do {			\
     76     printf("agp: ");				\
     77     printf x;				\
     78 } while (0)
     79 #else
     80 #define AGP_DPF(x)
     81 #endif
     82 
     83 #define AGPUNIT(x)	minor(x)
     84 
     85 /*
     86  * Data structure to describe an AGP memory allocation.
     87  */
     88 TAILQ_HEAD(agp_memory_list, agp_memory);
     89 struct agp_memory {
     90 	TAILQ_ENTRY(agp_memory) am_link;	/* wiring for the tailq */
     91 	int		am_id;			/* unique id for block */
     92 	vsize_t		am_size;		/* number of bytes allocated */
     93 	int		am_type;		/* chipset specific type */
     94 	off_t		am_offset;		/* page offset if bound */
     95 	int		am_is_bound;		/* non-zero if bound */
     96 	bus_addr_t	  am_physical;
     97 	void *		  am_virtual;
     98 	bus_dmamap_t	  am_dmamap;
     99 	bus_dma_segment_t *am_dmaseg;
    100 	int		  am_nseg;
    101 };
    102 
    103 struct agp_softc;
    104 
    105 struct agp_methods {
    106 	u_int32_t (*get_aperture)(struct agp_softc *);
    107 	int (*set_aperture)(struct agp_softc *, u_int32_t);
    108 	int (*bind_page)(struct agp_softc *, off_t, bus_addr_t);
    109 	int (*unbind_page)(struct agp_softc *, off_t);
    110 	void (*flush_tlb)(struct agp_softc *);
    111 	int (*enable)(struct agp_softc *, u_int32_t mode);
    112 	struct agp_memory *(*alloc_memory)(struct agp_softc *, int, vsize_t);
    113 	int (*free_memory)(struct agp_softc *, struct agp_memory *);
    114 	int (*bind_memory)(struct agp_softc *, struct agp_memory *, off_t);
    115 	int (*unbind_memory)(struct agp_softc *, struct agp_memory *);
    116 };
    117 
    118 #define AGP_GET_APERTURE(sc)	 ((sc)->as_methods->get_aperture(sc))
    119 #define AGP_SET_APERTURE(sc,a)	 ((sc)->as_methods->set_aperture((sc),(a)))
    120 #define AGP_BIND_PAGE(sc,o,p)	 ((sc)->as_methods->bind_page((sc),(o),(p)))
    121 #define AGP_UNBIND_PAGE(sc,o)	 ((sc)->as_methods->unbind_page((sc), (o)))
    122 #define AGP_FLUSH_TLB(sc)	 ((sc)->as_methods->flush_tlb(sc))
    123 #define AGP_ENABLE(sc,m)	 ((sc)->as_methods->enable((sc),(m)))
    124 #define AGP_ALLOC_MEMORY(sc,t,s) ((sc)->as_methods->alloc_memory((sc),(t),(s)))
    125 #define AGP_FREE_MEMORY(sc,m)	 ((sc)->as_methods->free_memory((sc),(m)))
    126 #define AGP_BIND_MEMORY(sc,m,o)	 ((sc)->as_methods->bind_memory((sc),(m),(o)))
    127 #define AGP_UNBIND_MEMORY(sc,m)	 ((sc)->as_methods->unbind_memory((sc),(m)))
    128 
    129 /*
    130  * All chipset drivers must have this at the start of their softc.
    131  */
    132 struct agp_softc {
    133 	device_t		as_dev;
    134 	bus_space_tag_t		as_apt;
    135 	int			as_capoff;
    136 	bus_addr_t		as_apaddr;
    137 	bus_size_t		as_apsize;
    138 	int			as_apflags;
    139 	bus_dma_tag_t		as_dmat;
    140 	u_int32_t		as_maxmem;	/* allocation upper bound */
    141 	u_int32_t		as_allocated;	/* amount allocated */
    142 	enum agp_acquire_state	as_state;
    143 	struct agp_memory_list	as_memory;	/* list of allocated memory */
    144 	int			as_nextid;	/* next memory block id */
    145 	int			as_isopen;	/* user device is open */
    146 #if 0
    147 	dev_t			as_devnode;	/* from make_dev */
    148 #endif
    149 	kmutex_t		as_mtx;		/* mutex for access to GATT */
    150 	struct agp_methods	*as_methods;	/* chipset-dependent API */
    151 	void			*as_chipc;	/* chipset-dependent state */
    152 	pci_chipset_tag_t	as_pc;
    153 	pcitag_t		as_tag;
    154 	pcireg_t		as_id;
    155 };
    156 
    157 struct agp_gatt {
    158 	u_int32_t	  ag_entries;
    159 	u_int32_t        *ag_virtual;
    160 	bus_addr_t	  ag_physical;
    161 	bus_dmamap_t	  ag_dmamap;
    162 	bus_dma_segment_t ag_dmaseg;
    163 	size_t		  ag_size;
    164 };
    165 
    166 int agpbusprint(void *, const char *);
    167 
    168 /*
    169  * Functions private to the AGP code.
    170  */
    171 void agp_flush_cache(void);
    172 int agp_find_caps(pci_chipset_tag_t, pcitag_t);
    173 int agp_map_aperture(struct pci_attach_args *, struct agp_softc *, int);
    174 struct agp_gatt *agp_alloc_gatt(struct agp_softc *);
    175 void agp_free_gatt(struct agp_softc *, struct agp_gatt *);
    176 int agp_generic_attach(struct agp_softc *);
    177 int agp_generic_detach(struct agp_softc *);
    178 int agp_generic_enable(struct agp_softc *, u_int32_t);
    179 struct agp_memory *agp_generic_alloc_memory(struct agp_softc *, int, vsize_t);
    180 int agp_generic_free_memory(struct agp_softc *, struct agp_memory *);
    181 int agp_generic_bind_memory(struct agp_softc *, struct agp_memory *, off_t);
    182 int agp_generic_bind_memory_bounded(struct agp_softc *, struct agp_memory *,
    183 	off_t, off_t, off_t);
    184 int agp_generic_unbind_memory(struct agp_softc *, struct agp_memory *);
    185 
    186 /* The vendor has already been matched when these functions are called */
    187 int agp_amd_match(const struct pci_attach_args *);
    188 int agp_amd64_match(const struct pci_attach_args *);
    189 
    190 int agp_ali_attach(device_t, device_t, void *);
    191 int agp_amd_attach(device_t, device_t, void *);
    192 int agp_apple_attach(device_t, device_t, void *);
    193 int agp_i810_attach(device_t, device_t, void *);
    194 int agp_intel_attach(device_t, device_t, void *);
    195 int agp_sis_attach(device_t, device_t, void *);
    196 int agp_nvidia_attach(device_t, device_t, void *);
    197 int agp_via_attach(device_t, device_t, void *);
    198 int agp_amd64_attach(device_t, device_t, void *);
    199 
    200 int agp_alloc_dmamem(bus_dma_tag_t, size_t, int, bus_dmamap_t *, void **,
    201 		     bus_addr_t *, bus_dma_segment_t *, int, int *);
    202 void agp_free_dmamem(bus_dma_tag_t, size_t, bus_dmamap_t, void *,
    203 		     bus_dma_segment_t *, int) ;
    204 
    205 MALLOC_DECLARE(M_AGP);
    206 
    207 /*
    208  * Kernel API
    209  */
    210 /*
    211  * Find the AGP device and return it.
    212  */
    213 void *agp_find_device(int);
    214 
    215 /*
    216  * Return the current owner of the AGP chipset.
    217  */
    218 enum agp_acquire_state agp_state(void *);
    219 
    220 /*
    221  * Query the state of the AGP system.
    222  */
    223 void agp_get_info(void *, struct agp_info *);
    224 
    225 /*
    226  * Acquire the AGP chipset for use by the kernel. Returns EBUSY if the
    227  * AGP chipset is already acquired by another user.
    228  */
    229 int agp_acquire(void *);
    230 
    231 /*
    232  * Release the AGP chipset.
    233  */
    234 int agp_release(void *);
    235 
    236 /*
    237  * Enable the agp hardware with the relevant mode. The mode bits are
    238  * defined in <dev/pci/agpreg.h>
    239  */
    240 int agp_enable(void *, u_int32_t);
    241 
    242 /*
    243  * Allocate physical memory suitable for mapping into the AGP
    244  * aperture.  The value returned is an opaque handle which can be
    245  * passed to agp_bind(), agp_unbind() or agp_deallocate().
    246  */
    247 void *agp_alloc_memory(void *, int, vsize_t);
    248 
    249 /*
    250  * Free memory which was allocated with agp_allocate().
    251  */
    252 void agp_free_memory(void *, void *);
    253 
    254 /*
    255  * Bind memory allocated with agp_allocate() at a given offset within
    256  * the AGP aperture. Returns EINVAL if the memory is already bound or
    257  * the offset is not at an AGP page boundary.
    258  */
    259 int agp_bind_memory(void *, void *, off_t);
    260 
    261 /*
    262  * Unbind memory from the AGP aperture. Returns EINVAL if the memory
    263  * is not bound.
    264  */
    265 int agp_unbind_memory(void *, void *);
    266 
    267 /*
    268  * Retrieve information about a memory block allocated with
    269  * agp_alloc_memory().
    270  */
    271 void agp_memory_info(void *, void *, struct agp_memory_info *);
    272 
    273 /*
    274  * XXX horrible hack to allow drm code to use our mapping
    275  * of VGA chip registers
    276  */
    277 int agp_i810_borrow(bus_addr_t, bus_size_t, bus_space_handle_t *);
    278 
    279 #endif /* !_PCI_AGPPRIV_H_ */
    280