2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2000 Doug Rabson
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/malloc.h>
37 #include <sys/kernel.h>
38 #include <sys/module.h>
41 #include <sys/ioccom.h>
42 #include <sys/agpio.h>
44 #include <sys/mutex.h>
46 #include <sys/rwlock.h>
48 #include <dev/agp/agppriv.h>
49 #include <dev/agp/agpvar.h>
50 #include <dev/agp/agpreg.h>
51 #include <dev/pci/pcivar.h>
52 #include <dev/pci/pcireg.h>
55 #include <vm/vm_extern.h>
56 #include <vm/vm_kern.h>
57 #include <vm/vm_param.h>
58 #include <vm/vm_object.h>
59 #include <vm/vm_page.h>
60 #include <vm/vm_pageout.h>
63 #include <machine/bus.h>
64 #include <machine/resource.h>
67 MODULE_VERSION(agp, 1);
69 MALLOC_DEFINE(M_AGP, "agp", "AGP data structures");
72 static d_open_t agp_open;
73 static d_close_t agp_close;
74 static d_ioctl_t agp_ioctl;
75 static d_mmap_t agp_mmap;
77 static struct cdevsw agp_cdevsw = {
78 .d_version = D_VERSION,
79 .d_flags = D_NEEDGIANT,
87 static devclass_t agp_devclass;
89 /* Helper functions for implementing chipset mini drivers. */
92 agp_find_caps(device_t dev)
97 if (pci_find_cap(dev, PCIY_AGP, &capreg) != 0)
103 * Find an AGP display device (if any).
106 agp_find_display(void)
108 devclass_t pci = devclass_find("pci");
109 device_t bus, dev = 0;
111 int busnum, numkids, i;
113 for (busnum = 0; busnum < devclass_get_maxunit(pci); busnum++) {
114 bus = devclass_get_device(pci, busnum);
117 if (device_get_children(bus, &kids, &numkids) != 0)
119 for (i = 0; i < numkids; i++) {
121 if (pci_get_class(dev) == PCIC_DISPLAY
122 && pci_get_subclass(dev) == PCIS_DISPLAY_VGA)
123 if (agp_find_caps(dev)) {
136 agp_alloc_gatt(device_t dev)
138 u_int32_t apsize = AGP_GET_APERTURE(dev);
139 u_int32_t entries = apsize >> AGP_PAGE_SHIFT;
140 struct agp_gatt *gatt;
144 "allocating GATT for aperture of size %dM\n",
145 apsize / (1024*1024));
148 device_printf(dev, "bad aperture size\n");
152 gatt = malloc(sizeof(struct agp_gatt), M_AGP, M_NOWAIT);
156 gatt->ag_entries = entries;
157 gatt->ag_virtual = (void *)kmem_alloc_contig(entries *
158 sizeof(u_int32_t), M_NOWAIT | M_ZERO, 0, ~0, PAGE_SIZE, 0,
159 VM_MEMATTR_WRITE_COMBINING);
160 if (!gatt->ag_virtual) {
162 device_printf(dev, "contiguous allocation failed\n");
166 gatt->ag_physical = vtophys((vm_offset_t) gatt->ag_virtual);
172 agp_free_gatt(struct agp_gatt *gatt)
174 kmem_free(kernel_arena, (vm_offset_t)gatt->ag_virtual,
175 gatt->ag_entries * sizeof(u_int32_t));
179 static u_int agp_max[][2] = {
190 #define AGP_MAX_SIZE nitems(agp_max)
193 * Sets the PCI resource which represents the AGP aperture.
195 * If not called, the default AGP aperture resource of AGP_APBASE will
196 * be used. Must be called before agp_generic_attach().
199 agp_set_aperture_resource(device_t dev, int rid)
201 struct agp_softc *sc = device_get_softc(dev);
203 sc->as_aperture_rid = rid;
207 agp_generic_attach(device_t dev)
209 struct agp_softc *sc = device_get_softc(dev);
214 * Find and map the aperture, RF_SHAREABLE for DRM but not RF_ACTIVE
215 * because the kernel doesn't need to map it.
218 if (sc->as_aperture_rid != -1) {
219 if (sc->as_aperture_rid == 0)
220 sc->as_aperture_rid = AGP_APBASE;
222 sc->as_aperture = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
223 &sc->as_aperture_rid, RF_SHAREABLE);
224 if (!sc->as_aperture)
229 * Work out an upper bound for agp memory allocation. This
230 * uses a heurisitc table from the Linux driver.
232 memsize = ptoa(realmem) >> 20;
233 for (i = 0; i < AGP_MAX_SIZE; i++) {
234 if (memsize <= agp_max[i][0])
237 if (i == AGP_MAX_SIZE)
238 i = AGP_MAX_SIZE - 1;
239 sc->as_maxmem = agp_max[i][1] << 20U;
242 * The lock is used to prevent re-entry to
243 * agp_generic_bind_memory() since that function can sleep.
245 mtx_init(&sc->as_lock, "agp lock", NULL, MTX_DEF);
248 * Initialise stuff for the userland device.
250 agp_devclass = devclass_find("agp");
251 TAILQ_INIT(&sc->as_memory);
254 sc->as_devnode = make_dev(&agp_cdevsw,
255 0, UID_ROOT, GID_WHEEL, 0600, "agpgart");
256 sc->as_devnode->si_drv1 = dev;
262 agp_free_cdev(device_t dev)
264 struct agp_softc *sc = device_get_softc(dev);
266 destroy_dev(sc->as_devnode);
270 agp_free_res(device_t dev)
272 struct agp_softc *sc = device_get_softc(dev);
274 if (sc->as_aperture != NULL)
275 bus_release_resource(dev, SYS_RES_MEMORY, sc->as_aperture_rid,
277 mtx_destroy(&sc->as_lock);
281 agp_generic_detach(device_t dev)
290 * Default AGP aperture size detection which simply returns the size of
291 * the aperture's PCI resource.
294 agp_generic_get_aperture(device_t dev)
296 struct agp_softc *sc = device_get_softc(dev);
298 return rman_get_size(sc->as_aperture);
302 * Default AGP aperture size setting function, which simply doesn't allow
303 * changes to resource size.
306 agp_generic_set_aperture(device_t dev, u_int32_t aperture)
308 u_int32_t current_aperture;
310 current_aperture = AGP_GET_APERTURE(dev);
311 if (current_aperture != aperture)
318 * This does the enable logic for v3, with the same topology
319 * restrictions as in place for v2 -- one bus, one device on the bus.
322 agp_v3_enable(device_t dev, device_t mdev, u_int32_t mode)
324 u_int32_t tstatus, mstatus;
326 int rq, sba, fw, rate, arqsz, cal;
328 tstatus = pci_read_config(dev, agp_find_caps(dev) + AGP_STATUS, 4);
329 mstatus = pci_read_config(mdev, agp_find_caps(mdev) + AGP_STATUS, 4);
331 /* Set RQ to the min of mode, tstatus and mstatus */
332 rq = AGP_MODE_GET_RQ(mode);
333 if (AGP_MODE_GET_RQ(tstatus) < rq)
334 rq = AGP_MODE_GET_RQ(tstatus);
335 if (AGP_MODE_GET_RQ(mstatus) < rq)
336 rq = AGP_MODE_GET_RQ(mstatus);
339 * ARQSZ - Set the value to the maximum one.
340 * Don't allow the mode register to override values.
342 arqsz = AGP_MODE_GET_ARQSZ(mode);
343 if (AGP_MODE_GET_ARQSZ(tstatus) > rq)
344 rq = AGP_MODE_GET_ARQSZ(tstatus);
345 if (AGP_MODE_GET_ARQSZ(mstatus) > rq)
346 rq = AGP_MODE_GET_ARQSZ(mstatus);
348 /* Calibration cycle - don't allow override by mode register */
349 cal = AGP_MODE_GET_CAL(tstatus);
350 if (AGP_MODE_GET_CAL(mstatus) < cal)
351 cal = AGP_MODE_GET_CAL(mstatus);
353 /* SBA must be supported for AGP v3. */
356 /* Set FW if all three support it. */
357 fw = (AGP_MODE_GET_FW(tstatus)
358 & AGP_MODE_GET_FW(mstatus)
359 & AGP_MODE_GET_FW(mode));
361 /* Figure out the max rate */
362 rate = (AGP_MODE_GET_RATE(tstatus)
363 & AGP_MODE_GET_RATE(mstatus)
364 & AGP_MODE_GET_RATE(mode));
365 if (rate & AGP_MODE_V3_RATE_8x)
366 rate = AGP_MODE_V3_RATE_8x;
368 rate = AGP_MODE_V3_RATE_4x;
370 device_printf(dev, "Setting AGP v3 mode %d\n", rate * 4);
372 pci_write_config(dev, agp_find_caps(dev) + AGP_COMMAND, 0, 4);
374 /* Construct the new mode word and tell the hardware */
376 command = AGP_MODE_SET_RQ(0, rq);
377 command = AGP_MODE_SET_ARQSZ(command, arqsz);
378 command = AGP_MODE_SET_CAL(command, cal);
379 command = AGP_MODE_SET_SBA(command, sba);
380 command = AGP_MODE_SET_FW(command, fw);
381 command = AGP_MODE_SET_RATE(command, rate);
382 command = AGP_MODE_SET_MODE_3(command, 1);
383 command = AGP_MODE_SET_AGP(command, 1);
384 pci_write_config(dev, agp_find_caps(dev) + AGP_COMMAND, command, 4);
385 pci_write_config(mdev, agp_find_caps(mdev) + AGP_COMMAND, command, 4);
391 agp_v2_enable(device_t dev, device_t mdev, u_int32_t mode)
393 u_int32_t tstatus, mstatus;
395 int rq, sba, fw, rate;
397 tstatus = pci_read_config(dev, agp_find_caps(dev) + AGP_STATUS, 4);
398 mstatus = pci_read_config(mdev, agp_find_caps(mdev) + AGP_STATUS, 4);
400 /* Set RQ to the min of mode, tstatus and mstatus */
401 rq = AGP_MODE_GET_RQ(mode);
402 if (AGP_MODE_GET_RQ(tstatus) < rq)
403 rq = AGP_MODE_GET_RQ(tstatus);
404 if (AGP_MODE_GET_RQ(mstatus) < rq)
405 rq = AGP_MODE_GET_RQ(mstatus);
407 /* Set SBA if all three can deal with SBA */
408 sba = (AGP_MODE_GET_SBA(tstatus)
409 & AGP_MODE_GET_SBA(mstatus)
410 & AGP_MODE_GET_SBA(mode));
413 fw = (AGP_MODE_GET_FW(tstatus)
414 & AGP_MODE_GET_FW(mstatus)
415 & AGP_MODE_GET_FW(mode));
417 /* Figure out the max rate */
418 rate = (AGP_MODE_GET_RATE(tstatus)
419 & AGP_MODE_GET_RATE(mstatus)
420 & AGP_MODE_GET_RATE(mode));
421 if (rate & AGP_MODE_V2_RATE_4x)
422 rate = AGP_MODE_V2_RATE_4x;
423 else if (rate & AGP_MODE_V2_RATE_2x)
424 rate = AGP_MODE_V2_RATE_2x;
426 rate = AGP_MODE_V2_RATE_1x;
428 device_printf(dev, "Setting AGP v2 mode %d\n", rate);
430 /* Construct the new mode word and tell the hardware */
432 command = AGP_MODE_SET_RQ(0, rq);
433 command = AGP_MODE_SET_SBA(command, sba);
434 command = AGP_MODE_SET_FW(command, fw);
435 command = AGP_MODE_SET_RATE(command, rate);
436 command = AGP_MODE_SET_AGP(command, 1);
437 pci_write_config(dev, agp_find_caps(dev) + AGP_COMMAND, command, 4);
438 pci_write_config(mdev, agp_find_caps(mdev) + AGP_COMMAND, command, 4);
444 agp_generic_enable(device_t dev, u_int32_t mode)
446 device_t mdev = agp_find_display();
447 u_int32_t tstatus, mstatus;
450 AGP_DPF("can't find display\n");
454 tstatus = pci_read_config(dev, agp_find_caps(dev) + AGP_STATUS, 4);
455 mstatus = pci_read_config(mdev, agp_find_caps(mdev) + AGP_STATUS, 4);
458 * Check display and bridge for AGP v3 support. AGP v3 allows
459 * more variety in topology than v2, e.g. multiple AGP devices
460 * attached to one bridge, or multiple AGP bridges in one
461 * system. This doesn't attempt to address those situations,
462 * but should work fine for a classic single AGP slot system
465 if (AGP_MODE_GET_MODE_3(mode) &&
466 AGP_MODE_GET_MODE_3(tstatus) &&
467 AGP_MODE_GET_MODE_3(mstatus))
468 return (agp_v3_enable(dev, mdev, mode));
470 return (agp_v2_enable(dev, mdev, mode));
474 agp_generic_alloc_memory(device_t dev, int type, vm_size_t size)
476 struct agp_softc *sc = device_get_softc(dev);
477 struct agp_memory *mem;
479 if ((size & (AGP_PAGE_SIZE - 1)) != 0)
482 if (size > sc->as_maxmem - sc->as_allocated)
486 printf("agp_generic_alloc_memory: unsupported type %d\n",
491 mem = malloc(sizeof *mem, M_AGP, M_WAITOK);
492 mem->am_id = sc->as_nextid++;
495 mem->am_obj = vm_object_allocate(OBJT_DEFAULT, atop(round_page(size)));
496 mem->am_physical = 0;
498 mem->am_is_bound = 0;
499 TAILQ_INSERT_TAIL(&sc->as_memory, mem, am_link);
500 sc->as_allocated += size;
506 agp_generic_free_memory(device_t dev, struct agp_memory *mem)
508 struct agp_softc *sc = device_get_softc(dev);
510 if (mem->am_is_bound)
513 sc->as_allocated -= mem->am_size;
514 TAILQ_REMOVE(&sc->as_memory, mem, am_link);
515 vm_object_deallocate(mem->am_obj);
521 agp_generic_bind_memory(device_t dev, struct agp_memory *mem,
524 struct agp_softc *sc = device_get_softc(dev);
529 /* Do some sanity checks first. */
530 if ((offset & (AGP_PAGE_SIZE - 1)) != 0 ||
531 offset + mem->am_size > AGP_GET_APERTURE(dev)) {
532 device_printf(dev, "binding memory at bad offset %#x\n",
538 * Allocate the pages early, before acquiring the lock,
539 * because vm_page_grab() may sleep and we can't hold a mutex
542 VM_OBJECT_WLOCK(mem->am_obj);
543 for (i = 0; i < mem->am_size; i += PAGE_SIZE) {
545 * Find a page from the object and wire it
546 * down. This page will be mapped using one or more
547 * entries in the GATT (assuming that PAGE_SIZE >=
548 * AGP_PAGE_SIZE. If this is the first call to bind,
549 * the pages will be allocated and zeroed.
551 m = vm_page_grab(mem->am_obj, OFF_TO_IDX(i),
552 VM_ALLOC_WIRED | VM_ALLOC_ZERO);
553 AGP_DPF("found page pa=%#jx\n", (uintmax_t)VM_PAGE_TO_PHYS(m));
555 VM_OBJECT_WUNLOCK(mem->am_obj);
557 mtx_lock(&sc->as_lock);
559 if (mem->am_is_bound) {
560 device_printf(dev, "memory already bound\n");
562 VM_OBJECT_WLOCK(mem->am_obj);
568 * Bind the individual pages and flush the chipset's
571 VM_OBJECT_WLOCK(mem->am_obj);
572 for (i = 0; i < mem->am_size; i += PAGE_SIZE) {
573 m = vm_page_lookup(mem->am_obj, OFF_TO_IDX(i));
576 * Install entries in the GATT, making sure that if
577 * AGP_PAGE_SIZE < PAGE_SIZE and mem->am_size is not
578 * aligned to PAGE_SIZE, we don't modify too many GATT
581 for (j = 0; j < PAGE_SIZE && i + j < mem->am_size;
582 j += AGP_PAGE_SIZE) {
583 vm_offset_t pa = VM_PAGE_TO_PHYS(m) + j;
584 AGP_DPF("binding offset %#jx to pa %#jx\n",
585 (uintmax_t)offset + i + j, (uintmax_t)pa);
586 error = AGP_BIND_PAGE(dev, offset + i + j, pa);
589 * Bail out. Reverse all the mappings
590 * and unwire the pages.
592 for (k = 0; k < i + j; k += AGP_PAGE_SIZE)
593 AGP_UNBIND_PAGE(dev, offset + k);
599 VM_OBJECT_WUNLOCK(mem->am_obj);
602 * Make sure the chipset gets the new mappings.
606 mem->am_offset = offset;
607 mem->am_is_bound = 1;
609 mtx_unlock(&sc->as_lock);
613 mtx_unlock(&sc->as_lock);
614 VM_OBJECT_ASSERT_WLOCKED(mem->am_obj);
615 for (k = 0; k < mem->am_size; k += PAGE_SIZE) {
616 m = vm_page_lookup(mem->am_obj, OFF_TO_IDX(k));
620 vm_page_unwire(m, PQ_INACTIVE);
623 VM_OBJECT_WUNLOCK(mem->am_obj);
629 agp_generic_unbind_memory(device_t dev, struct agp_memory *mem)
631 struct agp_softc *sc = device_get_softc(dev);
635 mtx_lock(&sc->as_lock);
637 if (!mem->am_is_bound) {
638 device_printf(dev, "memory is not bound\n");
639 mtx_unlock(&sc->as_lock);
645 * Unbind the individual pages and flush the chipset's
646 * TLB. Unwire the pages so they can be swapped.
648 for (i = 0; i < mem->am_size; i += AGP_PAGE_SIZE)
649 AGP_UNBIND_PAGE(dev, mem->am_offset + i);
653 VM_OBJECT_WLOCK(mem->am_obj);
654 for (i = 0; i < mem->am_size; i += PAGE_SIZE) {
655 m = vm_page_lookup(mem->am_obj, atop(i));
657 vm_page_unwire(m, PQ_INACTIVE);
660 VM_OBJECT_WUNLOCK(mem->am_obj);
663 mem->am_is_bound = 0;
665 mtx_unlock(&sc->as_lock);
670 /* Helper functions for implementing user/kernel api */
673 agp_acquire_helper(device_t dev, enum agp_acquire_state state)
675 struct agp_softc *sc = device_get_softc(dev);
677 if (sc->as_state != AGP_ACQUIRE_FREE)
679 sc->as_state = state;
685 agp_release_helper(device_t dev, enum agp_acquire_state state)
687 struct agp_softc *sc = device_get_softc(dev);
689 if (sc->as_state == AGP_ACQUIRE_FREE)
692 if (sc->as_state != state)
695 sc->as_state = AGP_ACQUIRE_FREE;
699 static struct agp_memory *
700 agp_find_memory(device_t dev, int id)
702 struct agp_softc *sc = device_get_softc(dev);
703 struct agp_memory *mem;
705 AGP_DPF("searching for memory block %d\n", id);
706 TAILQ_FOREACH(mem, &sc->as_memory, am_link) {
707 AGP_DPF("considering memory block %d\n", mem->am_id);
708 if (mem->am_id == id)
714 /* Implementation of the userland ioctl api */
717 agp_info_user(device_t dev, agp_info *info)
719 struct agp_softc *sc = device_get_softc(dev);
721 bzero(info, sizeof *info);
722 info->bridge_id = pci_get_devid(dev);
724 pci_read_config(dev, agp_find_caps(dev) + AGP_STATUS, 4);
726 info->aper_base = rman_get_start(sc->as_aperture);
729 info->aper_size = AGP_GET_APERTURE(dev) >> 20;
730 info->pg_total = info->pg_system = sc->as_maxmem >> AGP_PAGE_SHIFT;
731 info->pg_used = sc->as_allocated >> AGP_PAGE_SHIFT;
737 agp_setup_user(device_t dev, agp_setup *setup)
739 return AGP_ENABLE(dev, setup->agp_mode);
743 agp_allocate_user(device_t dev, agp_allocate *alloc)
745 struct agp_memory *mem;
747 mem = AGP_ALLOC_MEMORY(dev,
749 alloc->pg_count << AGP_PAGE_SHIFT);
751 alloc->key = mem->am_id;
752 alloc->physical = mem->am_physical;
760 agp_deallocate_user(device_t dev, int id)
762 struct agp_memory *mem = agp_find_memory(dev, id);
765 AGP_FREE_MEMORY(dev, mem);
773 agp_bind_user(device_t dev, agp_bind *bind)
775 struct agp_memory *mem = agp_find_memory(dev, bind->key);
780 return AGP_BIND_MEMORY(dev, mem, bind->pg_start << AGP_PAGE_SHIFT);
784 agp_unbind_user(device_t dev, agp_unbind *unbind)
786 struct agp_memory *mem = agp_find_memory(dev, unbind->key);
791 return AGP_UNBIND_MEMORY(dev, mem);
795 agp_chipset_flush(device_t dev)
798 return (AGP_CHIPSET_FLUSH(dev));
802 agp_open(struct cdev *kdev, int oflags, int devtype, struct thread *td)
804 device_t dev = kdev->si_drv1;
805 struct agp_softc *sc = device_get_softc(dev);
807 if (!sc->as_isopen) {
816 agp_close(struct cdev *kdev, int fflag, int devtype, struct thread *td)
818 device_t dev = kdev->si_drv1;
819 struct agp_softc *sc = device_get_softc(dev);
820 struct agp_memory *mem;
823 * Clear the GATT and force release on last close
825 while ((mem = TAILQ_FIRST(&sc->as_memory)) != NULL) {
826 if (mem->am_is_bound)
827 AGP_UNBIND_MEMORY(dev, mem);
828 AGP_FREE_MEMORY(dev, mem);
830 if (sc->as_state == AGP_ACQUIRE_USER)
831 agp_release_helper(dev, AGP_ACQUIRE_USER);
839 agp_ioctl(struct cdev *kdev, u_long cmd, caddr_t data, int fflag, struct thread *td)
841 device_t dev = kdev->si_drv1;
845 return agp_info_user(dev, (agp_info *) data);
848 return agp_acquire_helper(dev, AGP_ACQUIRE_USER);
851 return agp_release_helper(dev, AGP_ACQUIRE_USER);
854 return agp_setup_user(dev, (agp_setup *)data);
856 case AGPIOC_ALLOCATE:
857 return agp_allocate_user(dev, (agp_allocate *)data);
859 case AGPIOC_DEALLOCATE:
860 return agp_deallocate_user(dev, *(int *) data);
863 return agp_bind_user(dev, (agp_bind *)data);
866 return agp_unbind_user(dev, (agp_unbind *)data);
868 case AGPIOC_CHIPSET_FLUSH:
869 return agp_chipset_flush(dev);
876 agp_mmap(struct cdev *kdev, vm_ooffset_t offset, vm_paddr_t *paddr,
877 int prot, vm_memattr_t *memattr)
879 device_t dev = kdev->si_drv1;
880 struct agp_softc *sc = device_get_softc(dev);
882 if (offset > AGP_GET_APERTURE(dev))
884 if (sc->as_aperture == NULL)
886 *paddr = rman_get_start(sc->as_aperture) + offset;
890 /* Implementation of the kernel api */
895 device_t *children, child;
900 if (devclass_get_devices(agp_devclass, &children, &count) != 0)
903 for (i = 0; i < count; i++) {
904 if (device_is_attached(children[i])) {
909 free(children, M_TEMP);
913 enum agp_acquire_state
914 agp_state(device_t dev)
916 struct agp_softc *sc = device_get_softc(dev);
921 agp_get_info(device_t dev, struct agp_info *info)
923 struct agp_softc *sc = device_get_softc(dev);
926 pci_read_config(dev, agp_find_caps(dev) + AGP_STATUS, 4);
927 if (sc->as_aperture != NULL)
928 info->ai_aperture_base = rman_get_start(sc->as_aperture);
930 info->ai_aperture_base = 0;
931 info->ai_aperture_size = AGP_GET_APERTURE(dev);
932 info->ai_memory_allowed = sc->as_maxmem;
933 info->ai_memory_used = sc->as_allocated;
937 agp_acquire(device_t dev)
939 return agp_acquire_helper(dev, AGP_ACQUIRE_KERNEL);
943 agp_release(device_t dev)
945 return agp_release_helper(dev, AGP_ACQUIRE_KERNEL);
949 agp_enable(device_t dev, u_int32_t mode)
951 return AGP_ENABLE(dev, mode);
954 void *agp_alloc_memory(device_t dev, int type, vm_size_t bytes)
956 return (void *) AGP_ALLOC_MEMORY(dev, type, bytes);
959 void agp_free_memory(device_t dev, void *handle)
961 struct agp_memory *mem = (struct agp_memory *) handle;
962 AGP_FREE_MEMORY(dev, mem);
965 int agp_bind_memory(device_t dev, void *handle, vm_offset_t offset)
967 struct agp_memory *mem = (struct agp_memory *) handle;
968 return AGP_BIND_MEMORY(dev, mem, offset);
971 int agp_unbind_memory(device_t dev, void *handle)
973 struct agp_memory *mem = (struct agp_memory *) handle;
974 return AGP_UNBIND_MEMORY(dev, mem);
977 void agp_memory_info(device_t dev, void *handle, struct
980 struct agp_memory *mem = (struct agp_memory *) handle;
982 mi->ami_size = mem->am_size;
983 mi->ami_physical = mem->am_physical;
984 mi->ami_offset = mem->am_offset;
985 mi->ami_is_bound = mem->am_is_bound;
989 agp_bind_pages(device_t dev, vm_page_t *pages, vm_size_t size,
992 struct agp_softc *sc;
993 vm_offset_t i, j, k, pa;
997 if ((size & (AGP_PAGE_SIZE - 1)) != 0 ||
998 (offset & (AGP_PAGE_SIZE - 1)) != 0)
1001 sc = device_get_softc(dev);
1003 mtx_lock(&sc->as_lock);
1004 for (i = 0; i < size; i += PAGE_SIZE) {
1005 m = pages[OFF_TO_IDX(i)];
1006 KASSERT(m->wire_count > 0,
1007 ("agp_bind_pages: page %p hasn't been wired", m));
1010 * Install entries in the GATT, making sure that if
1011 * AGP_PAGE_SIZE < PAGE_SIZE and size is not
1012 * aligned to PAGE_SIZE, we don't modify too many GATT
1015 for (j = 0; j < PAGE_SIZE && i + j < size; j += AGP_PAGE_SIZE) {
1016 pa = VM_PAGE_TO_PHYS(m) + j;
1017 AGP_DPF("binding offset %#jx to pa %#jx\n",
1018 (uintmax_t)offset + i + j, (uintmax_t)pa);
1019 error = AGP_BIND_PAGE(dev, offset + i + j, pa);
1022 * Bail out. Reverse all the mappings.
1024 for (k = 0; k < i + j; k += AGP_PAGE_SIZE)
1025 AGP_UNBIND_PAGE(dev, offset + k);
1027 mtx_unlock(&sc->as_lock);
1035 mtx_unlock(&sc->as_lock);
1040 agp_unbind_pages(device_t dev, vm_size_t size, vm_offset_t offset)
1042 struct agp_softc *sc;
1045 if ((size & (AGP_PAGE_SIZE - 1)) != 0 ||
1046 (offset & (AGP_PAGE_SIZE - 1)) != 0)
1049 sc = device_get_softc(dev);
1051 mtx_lock(&sc->as_lock);
1052 for (i = 0; i < size; i += AGP_PAGE_SIZE)
1053 AGP_UNBIND_PAGE(dev, offset + i);
1057 mtx_unlock(&sc->as_lock);