xref: /freebsd-13-stable/sys/dev/agp/agp.c (revision 3bc80996974a61a4223eae4c1ccd47b6ee32a48a)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
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 
29 #include <sys/cdefs.h>
30 #include "opt_agp.h"
31 
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/malloc.h>
35 #include <sys/kernel.h>
36 #include <sys/module.h>
37 #include <sys/bus.h>
38 #include <sys/conf.h>
39 #include <sys/ioccom.h>
40 #include <sys/agpio.h>
41 #include <sys/lock.h>
42 #include <sys/mutex.h>
43 #include <sys/proc.h>
44 #include <sys/rwlock.h>
45 
46 #include <dev/agp/agppriv.h>
47 #include <dev/agp/agpvar.h>
48 #include <dev/agp/agpreg.h>
49 #include <dev/pci/pcivar.h>
50 #include <dev/pci/pcireg.h>
51 
52 #include <vm/vm.h>
53 #include <vm/vm_extern.h>
54 #include <vm/vm_kern.h>
55 #include <vm/vm_param.h>
56 #include <vm/vm_object.h>
57 #include <vm/vm_page.h>
58 #include <vm/vm_pageout.h>
59 #include <vm/pmap.h>
60 
61 #include <machine/bus.h>
62 #include <machine/resource.h>
63 #include <sys/rman.h>
64 
65 MODULE_VERSION(agp, 1);
66 
67 MALLOC_DEFINE(M_AGP, "agp", "AGP data structures");
68 
69 				/* agp_drv.c */
70 static d_open_t agp_open;
71 static d_close_t agp_close;
72 static d_ioctl_t agp_ioctl;
73 static d_mmap_t agp_mmap;
74 
75 static struct cdevsw agp_cdevsw = {
76 	.d_version =	D_VERSION,
77 	.d_flags =	D_NEEDGIANT,
78 	.d_open =	agp_open,
79 	.d_close =	agp_close,
80 	.d_ioctl =	agp_ioctl,
81 	.d_mmap =	agp_mmap,
82 	.d_name =	"agp",
83 };
84 
85 static devclass_t agp_devclass;
86 
87 /* Helper functions for implementing chipset mini drivers. */
88 
89 u_int8_t
agp_find_caps(device_t dev)90 agp_find_caps(device_t dev)
91 {
92 	int capreg;
93 
94 	if (pci_find_cap(dev, PCIY_AGP, &capreg) != 0)
95 		capreg = 0;
96 	return (capreg);
97 }
98 
99 /*
100  * Find an AGP display device (if any).
101  */
102 static device_t
agp_find_display(void)103 agp_find_display(void)
104 {
105 	devclass_t pci = devclass_find("pci");
106 	device_t bus, dev = 0;
107 	device_t *kids;
108 	int busnum, numkids, i;
109 
110 	for (busnum = 0; busnum < devclass_get_maxunit(pci); busnum++) {
111 		bus = devclass_get_device(pci, busnum);
112 		if (!bus)
113 			continue;
114 		if (device_get_children(bus, &kids, &numkids) != 0)
115 			continue;
116 		for (i = 0; i < numkids; i++) {
117 			dev = kids[i];
118 			if (pci_get_class(dev) == PCIC_DISPLAY
119 			    && pci_get_subclass(dev) == PCIS_DISPLAY_VGA)
120 				if (agp_find_caps(dev)) {
121 					free(kids, M_TEMP);
122 					return dev;
123 				}
124 
125 		}
126 		free(kids, M_TEMP);
127 	}
128 
129 	return 0;
130 }
131 
132 struct agp_gatt *
agp_alloc_gatt(device_t dev)133 agp_alloc_gatt(device_t dev)
134 {
135 	u_int32_t apsize = AGP_GET_APERTURE(dev);
136 	u_int32_t entries = apsize >> AGP_PAGE_SHIFT;
137 	struct agp_gatt *gatt;
138 
139 	if (bootverbose)
140 		device_printf(dev,
141 			      "allocating GATT for aperture of size %dM\n",
142 			      apsize / (1024*1024));
143 
144 	if (entries == 0) {
145 		device_printf(dev, "bad aperture size\n");
146 		return NULL;
147 	}
148 
149 	gatt = malloc(sizeof(struct agp_gatt), M_AGP, M_NOWAIT);
150 	if (!gatt)
151 		return 0;
152 
153 	gatt->ag_entries = entries;
154 	gatt->ag_virtual = (void *)kmem_alloc_contig(entries *
155 	    sizeof(u_int32_t), M_NOWAIT | M_ZERO, 0, ~0, PAGE_SIZE, 0,
156 	    VM_MEMATTR_WRITE_COMBINING);
157 	if (!gatt->ag_virtual) {
158 		if (bootverbose)
159 			device_printf(dev, "contiguous allocation failed\n");
160 		free(gatt, M_AGP);
161 		return 0;
162 	}
163 	gatt->ag_physical = vtophys((vm_offset_t) gatt->ag_virtual);
164 
165 	return gatt;
166 }
167 
168 void
agp_free_gatt(struct agp_gatt * gatt)169 agp_free_gatt(struct agp_gatt *gatt)
170 {
171 	kmem_free((vm_offset_t)gatt->ag_virtual, gatt->ag_entries *
172 	    sizeof(u_int32_t));
173 	free(gatt, M_AGP);
174 }
175 
176 static u_int agp_max[][2] = {
177 	{0,	0},
178 	{32,	4},
179 	{64,	28},
180 	{128,	96},
181 	{256,	204},
182 	{512,	440},
183 	{1024,	942},
184 	{2048,	1920},
185 	{4096,	3932}
186 };
187 #define	AGP_MAX_SIZE	nitems(agp_max)
188 
189 /**
190  * Sets the PCI resource which represents the AGP aperture.
191  *
192  * If not called, the default AGP aperture resource of AGP_APBASE will
193  * be used.  Must be called before agp_generic_attach().
194  */
195 void
agp_set_aperture_resource(device_t dev,int rid)196 agp_set_aperture_resource(device_t dev, int rid)
197 {
198 	struct agp_softc *sc = device_get_softc(dev);
199 
200 	sc->as_aperture_rid = rid;
201 }
202 
203 int
agp_generic_attach(device_t dev)204 agp_generic_attach(device_t dev)
205 {
206 	struct agp_softc *sc = device_get_softc(dev);
207 	int i;
208 	u_int memsize;
209 
210 	/*
211 	 * Find and map the aperture, RF_SHAREABLE for DRM but not RF_ACTIVE
212 	 * because the kernel doesn't need to map it.
213 	 */
214 
215 	if (sc->as_aperture_rid != -1) {
216 		if (sc->as_aperture_rid == 0)
217 			sc->as_aperture_rid = AGP_APBASE;
218 
219 		sc->as_aperture = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
220 		    &sc->as_aperture_rid, RF_SHAREABLE);
221 		if (!sc->as_aperture)
222 			return ENOMEM;
223 	}
224 
225 	/*
226 	 * Work out an upper bound for agp memory allocation. This
227 	 * uses a heurisitc table from the Linux driver.
228 	 */
229 	memsize = ptoa(realmem) >> 20;
230 	for (i = 0; i < AGP_MAX_SIZE; i++) {
231 		if (memsize <= agp_max[i][0])
232 			break;
233 	}
234 	if (i == AGP_MAX_SIZE)
235 		i = AGP_MAX_SIZE - 1;
236 	sc->as_maxmem = agp_max[i][1] << 20U;
237 
238 	/*
239 	 * The lock is used to prevent re-entry to
240 	 * agp_generic_bind_memory() since that function can sleep.
241 	 */
242 	mtx_init(&sc->as_lock, "agp lock", NULL, MTX_DEF);
243 
244 	/*
245 	 * Initialise stuff for the userland device.
246 	 */
247 	agp_devclass = devclass_find("agp");
248 	TAILQ_INIT(&sc->as_memory);
249 	sc->as_nextid = 1;
250 
251 	sc->as_devnode = make_dev(&agp_cdevsw,
252 	    0, UID_ROOT, GID_WHEEL, 0600, "agpgart");
253 	sc->as_devnode->si_drv1 = dev;
254 
255 	return 0;
256 }
257 
258 void
agp_free_cdev(device_t dev)259 agp_free_cdev(device_t dev)
260 {
261 	struct agp_softc *sc = device_get_softc(dev);
262 
263 	destroy_dev(sc->as_devnode);
264 }
265 
266 void
agp_free_res(device_t dev)267 agp_free_res(device_t dev)
268 {
269 	struct agp_softc *sc = device_get_softc(dev);
270 
271 	if (sc->as_aperture != NULL)
272 		bus_release_resource(dev, SYS_RES_MEMORY, sc->as_aperture_rid,
273 		    sc->as_aperture);
274 	mtx_destroy(&sc->as_lock);
275 }
276 
277 int
agp_generic_detach(device_t dev)278 agp_generic_detach(device_t dev)
279 {
280 
281 	agp_free_cdev(dev);
282 	agp_free_res(dev);
283 	return 0;
284 }
285 
286 /**
287  * Default AGP aperture size detection which simply returns the size of
288  * the aperture's PCI resource.
289  */
290 u_int32_t
agp_generic_get_aperture(device_t dev)291 agp_generic_get_aperture(device_t dev)
292 {
293 	struct agp_softc *sc = device_get_softc(dev);
294 
295 	return rman_get_size(sc->as_aperture);
296 }
297 
298 /**
299  * Default AGP aperture size setting function, which simply doesn't allow
300  * changes to resource size.
301  */
302 int
agp_generic_set_aperture(device_t dev,u_int32_t aperture)303 agp_generic_set_aperture(device_t dev, u_int32_t aperture)
304 {
305 	u_int32_t current_aperture;
306 
307 	current_aperture = AGP_GET_APERTURE(dev);
308 	if (current_aperture != aperture)
309 		return EINVAL;
310 	else
311 		return 0;
312 }
313 
314 /*
315  * This does the enable logic for v3, with the same topology
316  * restrictions as in place for v2 -- one bus, one device on the bus.
317  */
318 static int
agp_v3_enable(device_t dev,device_t mdev,u_int32_t mode)319 agp_v3_enable(device_t dev, device_t mdev, u_int32_t mode)
320 {
321 	u_int32_t tstatus, mstatus;
322 	u_int32_t command;
323 	int rq, sba, fw, rate, arqsz, cal;
324 
325 	tstatus = pci_read_config(dev, agp_find_caps(dev) + AGP_STATUS, 4);
326 	mstatus = pci_read_config(mdev, agp_find_caps(mdev) + AGP_STATUS, 4);
327 
328 	/* Set RQ to the min of mode, tstatus and mstatus */
329 	rq = AGP_MODE_GET_RQ(mode);
330 	if (AGP_MODE_GET_RQ(tstatus) < rq)
331 		rq = AGP_MODE_GET_RQ(tstatus);
332 	if (AGP_MODE_GET_RQ(mstatus) < rq)
333 		rq = AGP_MODE_GET_RQ(mstatus);
334 
335 	/*
336 	 * ARQSZ - Set the value to the maximum one.
337 	 * Don't allow the mode register to override values.
338 	 */
339 	arqsz = AGP_MODE_GET_ARQSZ(mode);
340 	if (AGP_MODE_GET_ARQSZ(tstatus) > rq)
341 		rq = AGP_MODE_GET_ARQSZ(tstatus);
342 	if (AGP_MODE_GET_ARQSZ(mstatus) > rq)
343 		rq = AGP_MODE_GET_ARQSZ(mstatus);
344 
345 	/* Calibration cycle - don't allow override by mode register */
346 	cal = AGP_MODE_GET_CAL(tstatus);
347 	if (AGP_MODE_GET_CAL(mstatus) < cal)
348 		cal = AGP_MODE_GET_CAL(mstatus);
349 
350 	/* SBA must be supported for AGP v3. */
351 	sba = 1;
352 
353 	/* Set FW if all three support it. */
354 	fw = (AGP_MODE_GET_FW(tstatus)
355 	       & AGP_MODE_GET_FW(mstatus)
356 	       & AGP_MODE_GET_FW(mode));
357 
358 	/* Figure out the max rate */
359 	rate = (AGP_MODE_GET_RATE(tstatus)
360 		& AGP_MODE_GET_RATE(mstatus)
361 		& AGP_MODE_GET_RATE(mode));
362 	if (rate & AGP_MODE_V3_RATE_8x)
363 		rate = AGP_MODE_V3_RATE_8x;
364 	else
365 		rate = AGP_MODE_V3_RATE_4x;
366 	if (bootverbose)
367 		device_printf(dev, "Setting AGP v3 mode %d\n", rate * 4);
368 
369 	pci_write_config(dev, agp_find_caps(dev) + AGP_COMMAND, 0, 4);
370 
371 	/* Construct the new mode word and tell the hardware */
372 	command = 0;
373 	command = AGP_MODE_SET_RQ(0, rq);
374 	command = AGP_MODE_SET_ARQSZ(command, arqsz);
375 	command = AGP_MODE_SET_CAL(command, cal);
376 	command = AGP_MODE_SET_SBA(command, sba);
377 	command = AGP_MODE_SET_FW(command, fw);
378 	command = AGP_MODE_SET_RATE(command, rate);
379 	command = AGP_MODE_SET_MODE_3(command, 1);
380 	command = AGP_MODE_SET_AGP(command, 1);
381 	pci_write_config(dev, agp_find_caps(dev) + AGP_COMMAND, command, 4);
382 	pci_write_config(mdev, agp_find_caps(mdev) + AGP_COMMAND, command, 4);
383 
384 	return 0;
385 }
386 
387 static int
agp_v2_enable(device_t dev,device_t mdev,u_int32_t mode)388 agp_v2_enable(device_t dev, device_t mdev, u_int32_t mode)
389 {
390 	u_int32_t tstatus, mstatus;
391 	u_int32_t command;
392 	int rq, sba, fw, rate;
393 
394 	tstatus = pci_read_config(dev, agp_find_caps(dev) + AGP_STATUS, 4);
395 	mstatus = pci_read_config(mdev, agp_find_caps(mdev) + AGP_STATUS, 4);
396 
397 	/* Set RQ to the min of mode, tstatus and mstatus */
398 	rq = AGP_MODE_GET_RQ(mode);
399 	if (AGP_MODE_GET_RQ(tstatus) < rq)
400 		rq = AGP_MODE_GET_RQ(tstatus);
401 	if (AGP_MODE_GET_RQ(mstatus) < rq)
402 		rq = AGP_MODE_GET_RQ(mstatus);
403 
404 	/* Set SBA if all three can deal with SBA */
405 	sba = (AGP_MODE_GET_SBA(tstatus)
406 	       & AGP_MODE_GET_SBA(mstatus)
407 	       & AGP_MODE_GET_SBA(mode));
408 
409 	/* Similar for FW */
410 	fw = (AGP_MODE_GET_FW(tstatus)
411 	       & AGP_MODE_GET_FW(mstatus)
412 	       & AGP_MODE_GET_FW(mode));
413 
414 	/* Figure out the max rate */
415 	rate = (AGP_MODE_GET_RATE(tstatus)
416 		& AGP_MODE_GET_RATE(mstatus)
417 		& AGP_MODE_GET_RATE(mode));
418 	if (rate & AGP_MODE_V2_RATE_4x)
419 		rate = AGP_MODE_V2_RATE_4x;
420 	else if (rate & AGP_MODE_V2_RATE_2x)
421 		rate = AGP_MODE_V2_RATE_2x;
422 	else
423 		rate = AGP_MODE_V2_RATE_1x;
424 	if (bootverbose)
425 		device_printf(dev, "Setting AGP v2 mode %d\n", rate);
426 
427 	/* Construct the new mode word and tell the hardware */
428 	command = 0;
429 	command = AGP_MODE_SET_RQ(0, rq);
430 	command = AGP_MODE_SET_SBA(command, sba);
431 	command = AGP_MODE_SET_FW(command, fw);
432 	command = AGP_MODE_SET_RATE(command, rate);
433 	command = AGP_MODE_SET_AGP(command, 1);
434 	pci_write_config(dev, agp_find_caps(dev) + AGP_COMMAND, command, 4);
435 	pci_write_config(mdev, agp_find_caps(mdev) + AGP_COMMAND, command, 4);
436 
437 	return 0;
438 }
439 
440 int
agp_generic_enable(device_t dev,u_int32_t mode)441 agp_generic_enable(device_t dev, u_int32_t mode)
442 {
443 	device_t mdev = agp_find_display();
444 	u_int32_t tstatus, mstatus;
445 
446 	if (!mdev) {
447 		AGP_DPF("can't find display\n");
448 		return ENXIO;
449 	}
450 
451 	tstatus = pci_read_config(dev, agp_find_caps(dev) + AGP_STATUS, 4);
452 	mstatus = pci_read_config(mdev, agp_find_caps(mdev) + AGP_STATUS, 4);
453 
454 	/*
455 	 * Check display and bridge for AGP v3 support.  AGP v3 allows
456 	 * more variety in topology than v2, e.g. multiple AGP devices
457 	 * attached to one bridge, or multiple AGP bridges in one
458 	 * system.  This doesn't attempt to address those situations,
459 	 * but should work fine for a classic single AGP slot system
460 	 * with AGP v3.
461 	 */
462 	if (AGP_MODE_GET_MODE_3(mode) &&
463 	    AGP_MODE_GET_MODE_3(tstatus) &&
464 	    AGP_MODE_GET_MODE_3(mstatus))
465 		return (agp_v3_enable(dev, mdev, mode));
466 	else
467 		return (agp_v2_enable(dev, mdev, mode));
468 }
469 
470 struct agp_memory *
agp_generic_alloc_memory(device_t dev,int type,vm_size_t size)471 agp_generic_alloc_memory(device_t dev, int type, vm_size_t size)
472 {
473 	struct agp_softc *sc = device_get_softc(dev);
474 	struct agp_memory *mem;
475 
476 	if ((size & (AGP_PAGE_SIZE - 1)) != 0)
477 		return 0;
478 
479 	if (size > sc->as_maxmem - sc->as_allocated)
480 		return 0;
481 
482 	if (type != 0) {
483 		printf("agp_generic_alloc_memory: unsupported type %d\n",
484 		       type);
485 		return 0;
486 	}
487 
488 	mem = malloc(sizeof *mem, M_AGP, M_WAITOK);
489 	mem->am_id = sc->as_nextid++;
490 	mem->am_size = size;
491 	mem->am_type = 0;
492 	mem->am_obj = vm_object_allocate(OBJT_SWAP, atop(round_page(size)));
493 	mem->am_physical = 0;
494 	mem->am_offset = 0;
495 	mem->am_is_bound = 0;
496 	TAILQ_INSERT_TAIL(&sc->as_memory, mem, am_link);
497 	sc->as_allocated += size;
498 
499 	return mem;
500 }
501 
502 int
agp_generic_free_memory(device_t dev,struct agp_memory * mem)503 agp_generic_free_memory(device_t dev, struct agp_memory *mem)
504 {
505 	struct agp_softc *sc = device_get_softc(dev);
506 
507 	if (mem->am_is_bound)
508 		return EBUSY;
509 
510 	sc->as_allocated -= mem->am_size;
511 	TAILQ_REMOVE(&sc->as_memory, mem, am_link);
512 	vm_object_deallocate(mem->am_obj);
513 	free(mem, M_AGP);
514 	return 0;
515 }
516 
517 int
agp_generic_bind_memory(device_t dev,struct agp_memory * mem,vm_offset_t offset)518 agp_generic_bind_memory(device_t dev, struct agp_memory *mem,
519 			vm_offset_t offset)
520 {
521 	struct agp_softc *sc = device_get_softc(dev);
522 	vm_offset_t i, j, k;
523 	vm_page_t m;
524 	int error;
525 
526 	/* Do some sanity checks first. */
527 	if ((offset & (AGP_PAGE_SIZE - 1)) != 0 ||
528 	    offset + mem->am_size > AGP_GET_APERTURE(dev)) {
529 		device_printf(dev, "binding memory at bad offset %#x\n",
530 		    (int)offset);
531 		return EINVAL;
532 	}
533 
534 	/*
535 	 * Allocate the pages early, before acquiring the lock,
536 	 * because vm_page_grab() may sleep and we can't hold a mutex
537 	 * while sleeping.
538 	 */
539 	VM_OBJECT_WLOCK(mem->am_obj);
540 	for (i = 0; i < mem->am_size; i += PAGE_SIZE) {
541 		/*
542 		 * Find a page from the object and wire it
543 		 * down. This page will be mapped using one or more
544 		 * entries in the GATT (assuming that PAGE_SIZE >=
545 		 * AGP_PAGE_SIZE. If this is the first call to bind,
546 		 * the pages will be allocated and zeroed.
547 		 */
548 		m = vm_page_grab(mem->am_obj, OFF_TO_IDX(i),
549 		    VM_ALLOC_WIRED | VM_ALLOC_ZERO);
550 		AGP_DPF("found page pa=%#jx\n", (uintmax_t)VM_PAGE_TO_PHYS(m));
551 	}
552 	VM_OBJECT_WUNLOCK(mem->am_obj);
553 
554 	mtx_lock(&sc->as_lock);
555 
556 	if (mem->am_is_bound) {
557 		device_printf(dev, "memory already bound\n");
558 		error = EINVAL;
559 		VM_OBJECT_WLOCK(mem->am_obj);
560 		i = 0;
561 		goto bad;
562 	}
563 
564 	/*
565 	 * Bind the individual pages and flush the chipset's
566 	 * TLB.
567 	 */
568 	VM_OBJECT_WLOCK(mem->am_obj);
569 	for (i = 0; i < mem->am_size; i += PAGE_SIZE) {
570 		m = vm_page_lookup(mem->am_obj, OFF_TO_IDX(i));
571 
572 		/*
573 		 * Install entries in the GATT, making sure that if
574 		 * AGP_PAGE_SIZE < PAGE_SIZE and mem->am_size is not
575 		 * aligned to PAGE_SIZE, we don't modify too many GATT
576 		 * entries.
577 		 */
578 		for (j = 0; j < PAGE_SIZE && i + j < mem->am_size;
579 		     j += AGP_PAGE_SIZE) {
580 			vm_offset_t pa = VM_PAGE_TO_PHYS(m) + j;
581 			AGP_DPF("binding offset %#jx to pa %#jx\n",
582 				(uintmax_t)offset + i + j, (uintmax_t)pa);
583 			error = AGP_BIND_PAGE(dev, offset + i + j, pa);
584 			if (error) {
585 				/*
586 				 * Bail out. Reverse all the mappings
587 				 * and unwire the pages.
588 				 */
589 				for (k = 0; k < i + j; k += AGP_PAGE_SIZE)
590 					AGP_UNBIND_PAGE(dev, offset + k);
591 				goto bad;
592 			}
593 		}
594 		vm_page_xunbusy(m);
595 	}
596 	VM_OBJECT_WUNLOCK(mem->am_obj);
597 
598 	/*
599 	 * Make sure the chipset gets the new mappings.
600 	 */
601 	AGP_FLUSH_TLB(dev);
602 
603 	mem->am_offset = offset;
604 	mem->am_is_bound = 1;
605 
606 	mtx_unlock(&sc->as_lock);
607 
608 	return 0;
609 bad:
610 	mtx_unlock(&sc->as_lock);
611 	VM_OBJECT_ASSERT_WLOCKED(mem->am_obj);
612 	for (k = 0; k < mem->am_size; k += PAGE_SIZE) {
613 		m = vm_page_lookup(mem->am_obj, OFF_TO_IDX(k));
614 		if (k >= i)
615 			vm_page_xunbusy(m);
616 		vm_page_unwire(m, PQ_INACTIVE);
617 	}
618 	VM_OBJECT_WUNLOCK(mem->am_obj);
619 
620 	return error;
621 }
622 
623 int
agp_generic_unbind_memory(device_t dev,struct agp_memory * mem)624 agp_generic_unbind_memory(device_t dev, struct agp_memory *mem)
625 {
626 	struct agp_softc *sc = device_get_softc(dev);
627 	vm_page_t m;
628 	int i;
629 
630 	mtx_lock(&sc->as_lock);
631 
632 	if (!mem->am_is_bound) {
633 		device_printf(dev, "memory is not bound\n");
634 		mtx_unlock(&sc->as_lock);
635 		return EINVAL;
636 	}
637 
638 	/*
639 	 * Unbind the individual pages and flush the chipset's
640 	 * TLB. Unwire the pages so they can be swapped.
641 	 */
642 	for (i = 0; i < mem->am_size; i += AGP_PAGE_SIZE)
643 		AGP_UNBIND_PAGE(dev, mem->am_offset + i);
644 
645 	AGP_FLUSH_TLB(dev);
646 
647 	VM_OBJECT_WLOCK(mem->am_obj);
648 	for (i = 0; i < mem->am_size; i += PAGE_SIZE) {
649 		m = vm_page_lookup(mem->am_obj, atop(i));
650 		vm_page_unwire(m, PQ_INACTIVE);
651 	}
652 	VM_OBJECT_WUNLOCK(mem->am_obj);
653 
654 	mem->am_offset = 0;
655 	mem->am_is_bound = 0;
656 
657 	mtx_unlock(&sc->as_lock);
658 
659 	return 0;
660 }
661 
662 /* Helper functions for implementing user/kernel api */
663 
664 static int
agp_acquire_helper(device_t dev,enum agp_acquire_state state)665 agp_acquire_helper(device_t dev, enum agp_acquire_state state)
666 {
667 	struct agp_softc *sc = device_get_softc(dev);
668 
669 	if (sc->as_state != AGP_ACQUIRE_FREE)
670 		return EBUSY;
671 	sc->as_state = state;
672 
673 	return 0;
674 }
675 
676 static int
agp_release_helper(device_t dev,enum agp_acquire_state state)677 agp_release_helper(device_t dev, enum agp_acquire_state state)
678 {
679 	struct agp_softc *sc = device_get_softc(dev);
680 
681 	if (sc->as_state == AGP_ACQUIRE_FREE)
682 		return 0;
683 
684 	if (sc->as_state != state)
685 		return EBUSY;
686 
687 	sc->as_state = AGP_ACQUIRE_FREE;
688 	return 0;
689 }
690 
691 static struct agp_memory *
agp_find_memory(device_t dev,int id)692 agp_find_memory(device_t dev, int id)
693 {
694 	struct agp_softc *sc = device_get_softc(dev);
695 	struct agp_memory *mem;
696 
697 	AGP_DPF("searching for memory block %d\n", id);
698 	TAILQ_FOREACH(mem, &sc->as_memory, am_link) {
699 		AGP_DPF("considering memory block %d\n", mem->am_id);
700 		if (mem->am_id == id)
701 			return mem;
702 	}
703 	return 0;
704 }
705 
706 /* Implementation of the userland ioctl api */
707 
708 static int
agp_info_user(device_t dev,agp_info * info)709 agp_info_user(device_t dev, agp_info *info)
710 {
711 	struct agp_softc *sc = device_get_softc(dev);
712 
713 	bzero(info, sizeof *info);
714 	info->bridge_id = pci_get_devid(dev);
715 	info->agp_mode =
716 	    pci_read_config(dev, agp_find_caps(dev) + AGP_STATUS, 4);
717 	if (sc->as_aperture)
718 		info->aper_base = rman_get_start(sc->as_aperture);
719 	else
720 		info->aper_base = 0;
721 	info->aper_size = AGP_GET_APERTURE(dev) >> 20;
722 	info->pg_total = info->pg_system = sc->as_maxmem >> AGP_PAGE_SHIFT;
723 	info->pg_used = sc->as_allocated >> AGP_PAGE_SHIFT;
724 
725 	return 0;
726 }
727 
728 static int
agp_setup_user(device_t dev,agp_setup * setup)729 agp_setup_user(device_t dev, agp_setup *setup)
730 {
731 	return AGP_ENABLE(dev, setup->agp_mode);
732 }
733 
734 static int
agp_allocate_user(device_t dev,agp_allocate * alloc)735 agp_allocate_user(device_t dev, agp_allocate *alloc)
736 {
737 	struct agp_memory *mem;
738 
739 	mem = AGP_ALLOC_MEMORY(dev,
740 			       alloc->type,
741 			       alloc->pg_count << AGP_PAGE_SHIFT);
742 	if (mem) {
743 		alloc->key = mem->am_id;
744 		alloc->physical = mem->am_physical;
745 		return 0;
746 	} else {
747 		return ENOMEM;
748 	}
749 }
750 
751 static int
agp_deallocate_user(device_t dev,int id)752 agp_deallocate_user(device_t dev, int id)
753 {
754 	struct agp_memory *mem = agp_find_memory(dev, id);
755 
756 	if (mem) {
757 		AGP_FREE_MEMORY(dev, mem);
758 		return 0;
759 	} else {
760 		return ENOENT;
761 	}
762 }
763 
764 static int
agp_bind_user(device_t dev,agp_bind * bind)765 agp_bind_user(device_t dev, agp_bind *bind)
766 {
767 	struct agp_memory *mem = agp_find_memory(dev, bind->key);
768 
769 	if (!mem)
770 		return ENOENT;
771 
772 	return AGP_BIND_MEMORY(dev, mem, bind->pg_start << AGP_PAGE_SHIFT);
773 }
774 
775 static int
agp_unbind_user(device_t dev,agp_unbind * unbind)776 agp_unbind_user(device_t dev, agp_unbind *unbind)
777 {
778 	struct agp_memory *mem = agp_find_memory(dev, unbind->key);
779 
780 	if (!mem)
781 		return ENOENT;
782 
783 	return AGP_UNBIND_MEMORY(dev, mem);
784 }
785 
786 static int
agp_chipset_flush(device_t dev)787 agp_chipset_flush(device_t dev)
788 {
789 
790 	return (AGP_CHIPSET_FLUSH(dev));
791 }
792 
793 static int
agp_open(struct cdev * kdev,int oflags,int devtype,struct thread * td)794 agp_open(struct cdev *kdev, int oflags, int devtype, struct thread *td)
795 {
796 	device_t dev = kdev->si_drv1;
797 	struct agp_softc *sc = device_get_softc(dev);
798 
799 	if (!sc->as_isopen) {
800 		sc->as_isopen = 1;
801 		device_busy(dev);
802 	}
803 
804 	return 0;
805 }
806 
807 static int
agp_close(struct cdev * kdev,int fflag,int devtype,struct thread * td)808 agp_close(struct cdev *kdev, int fflag, int devtype, struct thread *td)
809 {
810 	device_t dev = kdev->si_drv1;
811 	struct agp_softc *sc = device_get_softc(dev);
812 	struct agp_memory *mem;
813 
814 	/*
815 	 * Clear the GATT and force release on last close
816 	 */
817 	while ((mem = TAILQ_FIRST(&sc->as_memory)) != NULL) {
818 		if (mem->am_is_bound)
819 			AGP_UNBIND_MEMORY(dev, mem);
820 		AGP_FREE_MEMORY(dev, mem);
821 	}
822 	if (sc->as_state == AGP_ACQUIRE_USER)
823 		agp_release_helper(dev, AGP_ACQUIRE_USER);
824 	sc->as_isopen = 0;
825 	device_unbusy(dev);
826 
827 	return 0;
828 }
829 
830 static int
agp_ioctl(struct cdev * kdev,u_long cmd,caddr_t data,int fflag,struct thread * td)831 agp_ioctl(struct cdev *kdev, u_long cmd, caddr_t data, int fflag, struct thread *td)
832 {
833 	device_t dev = kdev->si_drv1;
834 
835 	switch (cmd) {
836 	case AGPIOC_INFO:
837 		return agp_info_user(dev, (agp_info *) data);
838 
839 	case AGPIOC_ACQUIRE:
840 		return agp_acquire_helper(dev, AGP_ACQUIRE_USER);
841 
842 	case AGPIOC_RELEASE:
843 		return agp_release_helper(dev, AGP_ACQUIRE_USER);
844 
845 	case AGPIOC_SETUP:
846 		return agp_setup_user(dev, (agp_setup *)data);
847 
848 	case AGPIOC_ALLOCATE:
849 		return agp_allocate_user(dev, (agp_allocate *)data);
850 
851 	case AGPIOC_DEALLOCATE:
852 		return agp_deallocate_user(dev, *(int *) data);
853 
854 	case AGPIOC_BIND:
855 		return agp_bind_user(dev, (agp_bind *)data);
856 
857 	case AGPIOC_UNBIND:
858 		return agp_unbind_user(dev, (agp_unbind *)data);
859 
860 	case AGPIOC_CHIPSET_FLUSH:
861 		return agp_chipset_flush(dev);
862 	}
863 
864 	return EINVAL;
865 }
866 
867 static int
agp_mmap(struct cdev * kdev,vm_ooffset_t offset,vm_paddr_t * paddr,int prot,vm_memattr_t * memattr)868 agp_mmap(struct cdev *kdev, vm_ooffset_t offset, vm_paddr_t *paddr,
869     int prot, vm_memattr_t *memattr)
870 {
871 	device_t dev = kdev->si_drv1;
872 	struct agp_softc *sc = device_get_softc(dev);
873 
874 	if (offset > AGP_GET_APERTURE(dev))
875 		return -1;
876 	if (sc->as_aperture == NULL)
877 		return -1;
878 	*paddr = rman_get_start(sc->as_aperture) + offset;
879 	return 0;
880 }
881 
882 /* Implementation of the kernel api */
883 
884 device_t
agp_find_device(void)885 agp_find_device(void)
886 {
887 	device_t *children, child;
888 	int i, count;
889 
890 	if (!agp_devclass)
891 		return NULL;
892 	if (devclass_get_devices(agp_devclass, &children, &count) != 0)
893 		return NULL;
894 	child = NULL;
895 	for (i = 0; i < count; i++) {
896 		if (device_is_attached(children[i])) {
897 			child = children[i];
898 			break;
899 		}
900 	}
901 	free(children, M_TEMP);
902 	return child;
903 }
904 
905 enum agp_acquire_state
agp_state(device_t dev)906 agp_state(device_t dev)
907 {
908 	struct agp_softc *sc = device_get_softc(dev);
909 	return sc->as_state;
910 }
911 
912 void
agp_get_info(device_t dev,struct agp_info * info)913 agp_get_info(device_t dev, struct agp_info *info)
914 {
915 	struct agp_softc *sc = device_get_softc(dev);
916 
917 	info->ai_mode =
918 		pci_read_config(dev, agp_find_caps(dev) + AGP_STATUS, 4);
919 	if (sc->as_aperture != NULL)
920 		info->ai_aperture_base = rman_get_start(sc->as_aperture);
921 	else
922 		info->ai_aperture_base = 0;
923 	info->ai_aperture_size = AGP_GET_APERTURE(dev);
924 	info->ai_memory_allowed = sc->as_maxmem;
925 	info->ai_memory_used = sc->as_allocated;
926 }
927 
928 int
agp_acquire(device_t dev)929 agp_acquire(device_t dev)
930 {
931 	return agp_acquire_helper(dev, AGP_ACQUIRE_KERNEL);
932 }
933 
934 int
agp_release(device_t dev)935 agp_release(device_t dev)
936 {
937 	return agp_release_helper(dev, AGP_ACQUIRE_KERNEL);
938 }
939 
940 int
agp_enable(device_t dev,u_int32_t mode)941 agp_enable(device_t dev, u_int32_t mode)
942 {
943 	return AGP_ENABLE(dev, mode);
944 }
945 
agp_alloc_memory(device_t dev,int type,vm_size_t bytes)946 void *agp_alloc_memory(device_t dev, int type, vm_size_t bytes)
947 {
948 	return  (void *) AGP_ALLOC_MEMORY(dev, type, bytes);
949 }
950 
agp_free_memory(device_t dev,void * handle)951 void agp_free_memory(device_t dev, void *handle)
952 {
953 	struct agp_memory *mem = (struct agp_memory *) handle;
954 	AGP_FREE_MEMORY(dev, mem);
955 }
956 
agp_bind_memory(device_t dev,void * handle,vm_offset_t offset)957 int agp_bind_memory(device_t dev, void *handle, vm_offset_t offset)
958 {
959 	struct agp_memory *mem = (struct agp_memory *) handle;
960 	return AGP_BIND_MEMORY(dev, mem, offset);
961 }
962 
agp_unbind_memory(device_t dev,void * handle)963 int agp_unbind_memory(device_t dev, void *handle)
964 {
965 	struct agp_memory *mem = (struct agp_memory *) handle;
966 	return AGP_UNBIND_MEMORY(dev, mem);
967 }
968 
agp_memory_info(device_t dev,void * handle,struct agp_memory_info * mi)969 void agp_memory_info(device_t dev, void *handle, struct
970 		     agp_memory_info *mi)
971 {
972 	struct agp_memory *mem = (struct agp_memory *) handle;
973 
974 	mi->ami_size = mem->am_size;
975 	mi->ami_physical = mem->am_physical;
976 	mi->ami_offset = mem->am_offset;
977 	mi->ami_is_bound = mem->am_is_bound;
978 }
979 
980 int
agp_bind_pages(device_t dev,vm_page_t * pages,vm_size_t size,vm_offset_t offset)981 agp_bind_pages(device_t dev, vm_page_t *pages, vm_size_t size,
982     vm_offset_t offset)
983 {
984 	struct agp_softc *sc;
985 	vm_offset_t i, j, k, pa;
986 	vm_page_t m;
987 	int error;
988 
989 	if ((size & (AGP_PAGE_SIZE - 1)) != 0 ||
990 	    (offset & (AGP_PAGE_SIZE - 1)) != 0)
991 		return (EINVAL);
992 
993 	sc = device_get_softc(dev);
994 
995 	mtx_lock(&sc->as_lock);
996 	for (i = 0; i < size; i += PAGE_SIZE) {
997 		m = pages[OFF_TO_IDX(i)];
998 		KASSERT(vm_page_wired(m),
999 		    ("agp_bind_pages: page %p hasn't been wired", m));
1000 
1001 		/*
1002 		 * Install entries in the GATT, making sure that if
1003 		 * AGP_PAGE_SIZE < PAGE_SIZE and size is not
1004 		 * aligned to PAGE_SIZE, we don't modify too many GATT
1005 		 * entries.
1006 		 */
1007 		for (j = 0; j < PAGE_SIZE && i + j < size; j += AGP_PAGE_SIZE) {
1008 			pa = VM_PAGE_TO_PHYS(m) + j;
1009 			AGP_DPF("binding offset %#jx to pa %#jx\n",
1010 				(uintmax_t)offset + i + j, (uintmax_t)pa);
1011 			error = AGP_BIND_PAGE(dev, offset + i + j, pa);
1012 			if (error) {
1013 				/*
1014 				 * Bail out. Reverse all the mappings.
1015 				 */
1016 				for (k = 0; k < i + j; k += AGP_PAGE_SIZE)
1017 					AGP_UNBIND_PAGE(dev, offset + k);
1018 
1019 				mtx_unlock(&sc->as_lock);
1020 				return (error);
1021 			}
1022 		}
1023 	}
1024 
1025 	AGP_FLUSH_TLB(dev);
1026 
1027 	mtx_unlock(&sc->as_lock);
1028 	return (0);
1029 }
1030 
1031 int
agp_unbind_pages(device_t dev,vm_size_t size,vm_offset_t offset)1032 agp_unbind_pages(device_t dev, vm_size_t size, vm_offset_t offset)
1033 {
1034 	struct agp_softc *sc;
1035 	vm_offset_t i;
1036 
1037 	if ((size & (AGP_PAGE_SIZE - 1)) != 0 ||
1038 	    (offset & (AGP_PAGE_SIZE - 1)) != 0)
1039 		return (EINVAL);
1040 
1041 	sc = device_get_softc(dev);
1042 
1043 	mtx_lock(&sc->as_lock);
1044 	for (i = 0; i < size; i += AGP_PAGE_SIZE)
1045 		AGP_UNBIND_PAGE(dev, offset + i);
1046 
1047 	AGP_FLUSH_TLB(dev);
1048 
1049 	mtx_unlock(&sc->as_lock);
1050 	return (0);
1051 }
1052