xref: /openbsd/sys/arch/i386/pci/agp_machdep.c (revision 4bdff4be)
1 /*	$OpenBSD: agp_machdep.c,v 1.21 2023/01/30 10:49:05 jsg Exp $	*/
2 
3 /*
4  * Copyright (c) 2008 - 2009 Owain G. Ainsworth <oga@openbsd.org>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 /*
19  * Copyright (c) 2002 Michael Shalayeff
20  * All rights reserved.
21  *
22  * Redistribution and use in source and binary forms, with or without
23  * modification, are permitted provided that the following conditions
24  * are met:
25  * 1. Redistributions of source code must retain the above copyright
26  *    notice, this list of conditions and the following disclaimer.
27  * 2. Redistributions in binary form must reproduce the above copyright
28  *    notice, this list of conditions and the following disclaimer in the
29  *    documentation and/or other materials provided with the distribution.
30  *
31  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
32  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
33  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
34  * IN NO EVENT SHALL THE AUTHOR OR HIS RELATIVES BE LIABLE FOR ANY DIRECT,
35  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
36  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
37  * SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
41  * THE POSSIBILITY OF SUCH DAMAGE.
42  */
43 
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/malloc.h>
47 
48 #include <dev/pci/pcivar.h>
49 #include <dev/pci/pcireg.h>
50 #include <dev/pci/agpvar.h>
51 
52 #include <uvm/uvm_extern.h>
53 
54 #include <machine/cpufunc.h>
55 #include <machine/bus.h>
56 #include <machine/pmap.h>
57 
58 void
59 agp_flush_cache(void)
60 {
61 	wbinvd();
62 }
63 
64 void
65 agp_flush_cache_range(vaddr_t va, vsize_t sz)
66 {
67 	pmap_flush_cache(va, sz);
68 }
69 
70 struct agp_map {
71 	bus_space_tag_t	bst;
72 	bus_addr_t	addr;
73 	bus_size_t	size;
74 	int		flags;
75 	vaddr_t		va;
76 };
77 
78 extern struct extent	*iomem_ex;
79 
80 int
81 agp_init_map(bus_space_tag_t tag, bus_addr_t address, bus_size_t size,
82     int flags, struct agp_map **mapp)
83 {
84 	struct agp_map *map;
85 	int error;
86 
87 	KASSERT(tag == I386_BUS_SPACE_MEM);
88 
89 	/*
90 	 * We grab the extent out of the bus region ourselves
91 	 * so we don't need to do these allocations every time.
92 	 */
93 	error = extent_alloc_region(iomem_ex, address, size,
94 	    EX_NOWAIT | EX_MALLOCOK);
95 	if (error)
96 		return (error);
97 
98 	map = malloc(sizeof(*map), M_AGP, M_WAITOK | M_CANFAIL);
99 	if (map == NULL)
100 		return (ENOMEM);
101 
102 	map->bst = tag;
103 	map->addr = address;
104 	map->size = size;
105 	map->flags = flags;
106 
107 	map->va = (vaddr_t)km_alloc(PAGE_SIZE, &kv_any, &kp_none, &kd_waitok);
108 	if (map->va == 0) {
109 		free(map, M_AGP, sizeof(*map));
110 		return (ENOMEM);
111 	}
112 
113 	*mapp = map;
114 	return (0);
115 }
116 
117 void
118 agp_destroy_map(struct agp_map *map)
119 {
120 	if (extent_free(iomem_ex, map->addr, map->size,
121 	    EX_NOWAIT | EX_MALLOCOK ))
122 		printf("%s: can't free region\n",__func__);
123 	km_free((void *)map->va, PAGE_SIZE, &kv_any, &kp_none);
124 	free(map, M_AGP, sizeof(*map));
125 }
126 
127 
128 int
129 agp_map_subregion(struct agp_map *map, bus_size_t offset, bus_size_t size,
130     bus_space_handle_t *bshp)
131 {
132 	return (_bus_space_map(map->bst, map->addr + offset, size,
133 	    map->flags, bshp));
134 }
135 
136 void
137 agp_unmap_subregion(struct agp_map *map, bus_space_handle_t bsh,
138     bus_size_t size)
139 {
140 	return (_bus_space_unmap(map->bst, bsh, size, NULL));
141 }
142 
143 void
144 agp_map_atomic(struct agp_map *map, bus_size_t offset,
145     bus_space_handle_t *bshp)
146 {
147 	int pmap_flags = PMAP_NOCACHE;
148 	paddr_t pa;
149 
150 	KASSERT((offset & PGOFSET) == 0);
151 
152 	if (map->flags & BUS_SPACE_MAP_CACHEABLE)
153 		pmap_flags = 0;
154 	else if (map->flags & BUS_SPACE_MAP_PREFETCHABLE)
155 		pmap_flags = PMAP_WC;
156 
157 	pa = bus_space_mmap(map->bst, map->addr, offset, 0, 0);
158 	pmap_kenter_pa(map->va, pa | pmap_flags, PROT_READ | PROT_WRITE);
159 	pmap_update(pmap_kernel());
160 
161 	*bshp = (bus_space_handle_t)map->va;
162 }
163 
164 void
165 agp_unmap_atomic(struct agp_map *map, bus_space_handle_t bsh)
166 {
167 	KASSERT(bsh == (bus_space_handle_t)map->va);
168 
169 	pmap_kremove(map->va, PAGE_SIZE);
170 	pmap_update(pmap_kernel());
171 }
172