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)
96 if (pci_find_cap(dev, PCIY_AGP, &capreg) != 0)
102 * Find an AGP display device (if any).
105 agp_find_display(void)
107 devclass_t pci = devclass_find("pci");
108 device_t bus, dev = 0;
110 int busnum, numkids, i;
112 for (busnum = 0; busnum < devclass_get_maxunit(pci); busnum++) {
113 bus = devclass_get_device(pci, busnum);
116 if (device_get_children(bus, &kids, &numkids) != 0)
118 for (i = 0; i < numkids; i++) {
120 if (pci_get_class(dev) == PCIC_DISPLAY
121 && pci_get_subclass(dev) == PCIS_DISPLAY_VGA)
122 if (agp_find_caps(dev)) {
135 agp_alloc_gatt(device_t dev)
137 u_int32_t apsize = AGP_GET_APERTURE(dev);
138 u_int32_t entries = apsize >> AGP_PAGE_SHIFT;
139 struct agp_gatt *gatt;
143 "allocating GATT for aperture of size %dM\n",
144 apsize / (1024*1024));
147 device_printf(dev, "bad aperture size\n");
151 gatt = malloc(sizeof(struct agp_gatt), M_AGP, M_NOWAIT);
155 gatt->ag_entries = entries;
156 gatt->ag_virtual = (void *)kmem_alloc_contig(entries *
157 sizeof(u_int32_t), M_NOWAIT | M_ZERO, 0, ~0, PAGE_SIZE, 0,
158 VM_MEMATTR_WRITE_COMBINING);
159 if (!gatt->ag_virtual) {
161 device_printf(dev, "contiguous allocation failed\n");
165 gatt->ag_physical = vtophys((vm_offset_t) gatt->ag_virtual);
171 agp_free_gatt(struct agp_gatt *gatt)
173 kmem_free((vm_offset_t)gatt->ag_virtual, gatt->ag_entries *
178 static u_int agp_max[][2] = {
189 #define AGP_MAX_SIZE nitems(agp_max)
192 * Sets the PCI resource which represents the AGP aperture.
194 * If not called, the default AGP aperture resource of AGP_APBASE will
195 * be used. Must be called before agp_generic_attach().
198 agp_set_aperture_resource(device_t dev, int rid)
200 struct agp_softc *sc = device_get_softc(dev);
202 sc->as_aperture_rid = rid;
206 agp_generic_attach(device_t dev)
208 struct agp_softc *sc = device_get_softc(dev);
213 * Find and map the aperture, RF_SHAREABLE for DRM but not RF_ACTIVE
214 * because the kernel doesn't need to map it.
217 if (sc->as_aperture_rid != -1) {
218 if (sc->as_aperture_rid == 0)
219 sc->as_aperture_rid = AGP_APBASE;
221 sc->as_aperture = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
222 &sc->as_aperture_rid, RF_SHAREABLE);
223 if (!sc->as_aperture)
228 * Work out an upper bound for agp memory allocation. This
229 * uses a heurisitc table from the Linux driver.
231 memsize = ptoa(realmem) >> 20;
232 for (i = 0; i < AGP_MAX_SIZE; i++) {
233 if (memsize <= agp_max[i][0])
236 if (i == AGP_MAX_SIZE)
237 i = AGP_MAX_SIZE - 1;
238 sc->as_maxmem = agp_max[i][1] << 20U;
241 * The lock is used to prevent re-entry to
242 * agp_generic_bind_memory() since that function can sleep.
244 mtx_init(&sc->as_lock, "agp lock", NULL, MTX_DEF);
247 * Initialise stuff for the userland device.
249 agp_devclass = devclass_find("agp");
250 TAILQ_INIT(&sc->as_memory);
253 sc->as_devnode = make_dev(&agp_cdevsw,
254 0, UID_ROOT, GID_WHEEL, 0600, "agpgart");
255 sc->as_devnode->si_drv1 = dev;
261 agp_free_cdev(device_t dev)
263 struct agp_softc *sc = device_get_softc(dev);
265 destroy_dev(sc->as_devnode);
269 agp_free_res(device_t dev)
271 struct agp_softc *sc = device_get_softc(dev);
273 if (sc->as_aperture != NULL)
274 bus_release_resource(dev, SYS_RES_MEMORY, sc->as_aperture_rid,
276 mtx_destroy(&sc->as_lock);
280 agp_generic_detach(device_t dev)
289 * Default AGP aperture size detection which simply returns the size of
290 * the aperture's PCI resource.
293 agp_generic_get_aperture(device_t dev)
295 struct agp_softc *sc = device_get_softc(dev);
297 return rman_get_size(sc->as_aperture);
301 * Default AGP aperture size setting function, which simply doesn't allow
302 * changes to resource size.
305 agp_generic_set_aperture(device_t dev, u_int32_t aperture)
307 u_int32_t current_aperture;
309 current_aperture = AGP_GET_APERTURE(dev);
310 if (current_aperture != aperture)
317 * This does the enable logic for v3, with the same topology
318 * restrictions as in place for v2 -- one bus, one device on the bus.
321 agp_v3_enable(device_t dev, device_t mdev, u_int32_t mode)
323 u_int32_t tstatus, mstatus;
325 int rq, sba, fw, rate, arqsz, cal;
327 tstatus = pci_read_config(dev, agp_find_caps(dev) + AGP_STATUS, 4);
328 mstatus = pci_read_config(mdev, agp_find_caps(mdev) + AGP_STATUS, 4);
330 /* Set RQ to the min of mode, tstatus and mstatus */
331 rq = AGP_MODE_GET_RQ(mode);
332 if (AGP_MODE_GET_RQ(tstatus) < rq)
333 rq = AGP_MODE_GET_RQ(tstatus);
334 if (AGP_MODE_GET_RQ(mstatus) < rq)
335 rq = AGP_MODE_GET_RQ(mstatus);
338 * ARQSZ - Set the value to the maximum one.
339 * Don't allow the mode register to override values.
341 arqsz = AGP_MODE_GET_ARQSZ(mode);
342 if (AGP_MODE_GET_ARQSZ(tstatus) > rq)
343 rq = AGP_MODE_GET_ARQSZ(tstatus);
344 if (AGP_MODE_GET_ARQSZ(mstatus) > rq)
345 rq = AGP_MODE_GET_ARQSZ(mstatus);
347 /* Calibration cycle - don't allow override by mode register */
348 cal = AGP_MODE_GET_CAL(tstatus);
349 if (AGP_MODE_GET_CAL(mstatus) < cal)
350 cal = AGP_MODE_GET_CAL(mstatus);
352 /* SBA must be supported for AGP v3. */
355 /* Set FW if all three support it. */
356 fw = (AGP_MODE_GET_FW(tstatus)
357 & AGP_MODE_GET_FW(mstatus)
358 & AGP_MODE_GET_FW(mode));
360 /* Figure out the max rate */
361 rate = (AGP_MODE_GET_RATE(tstatus)
362 & AGP_MODE_GET_RATE(mstatus)
363 & AGP_MODE_GET_RATE(mode));
364 if (rate & AGP_MODE_V3_RATE_8x)
365 rate = AGP_MODE_V3_RATE_8x;
367 rate = AGP_MODE_V3_RATE_4x;
369 device_printf(dev, "Setting AGP v3 mode %d\n", rate * 4);
371 pci_write_config(dev, agp_find_caps(dev) + AGP_COMMAND, 0, 4);
373 /* Construct the new mode word and tell the hardware */
375 command = AGP_MODE_SET_RQ(0, rq);
376 command = AGP_MODE_SET_ARQSZ(command, arqsz);
377 command = AGP_MODE_SET_CAL(command, cal);
378 command = AGP_MODE_SET_SBA(command, sba);
379 command = AGP_MODE_SET_FW(command, fw);
380 command = AGP_MODE_SET_RATE(command, rate);
381 command = AGP_MODE_SET_MODE_3(command, 1);
382 command = AGP_MODE_SET_AGP(command, 1);
383 pci_write_config(dev, agp_find_caps(dev) + AGP_COMMAND, command, 4);
384 pci_write_config(mdev, agp_find_caps(mdev) + AGP_COMMAND, command, 4);
390 agp_v2_enable(device_t dev, device_t mdev, u_int32_t mode)
392 u_int32_t tstatus, mstatus;
394 int rq, sba, fw, rate;
396 tstatus = pci_read_config(dev, agp_find_caps(dev) + AGP_STATUS, 4);
397 mstatus = pci_read_config(mdev, agp_find_caps(mdev) + AGP_STATUS, 4);
399 /* Set RQ to the min of mode, tstatus and mstatus */
400 rq = AGP_MODE_GET_RQ(mode);
401 if (AGP_MODE_GET_RQ(tstatus) < rq)
402 rq = AGP_MODE_GET_RQ(tstatus);
403 if (AGP_MODE_GET_RQ(mstatus) < rq)
404 rq = AGP_MODE_GET_RQ(mstatus);
406 /* Set SBA if all three can deal with SBA */
407 sba = (AGP_MODE_GET_SBA(tstatus)
408 & AGP_MODE_GET_SBA(mstatus)
409 & AGP_MODE_GET_SBA(mode));
412 fw = (AGP_MODE_GET_FW(tstatus)
413 & AGP_MODE_GET_FW(mstatus)
414 & AGP_MODE_GET_FW(mode));
416 /* Figure out the max rate */
417 rate = (AGP_MODE_GET_RATE(tstatus)
418 & AGP_MODE_GET_RATE(mstatus)
419 & AGP_MODE_GET_RATE(mode));
420 if (rate & AGP_MODE_V2_RATE_4x)
421 rate = AGP_MODE_V2_RATE_4x;
422 else if (rate & AGP_MODE_V2_RATE_2x)
423 rate = AGP_MODE_V2_RATE_2x;
425 rate = AGP_MODE_V2_RATE_1x;
427 device_printf(dev, "Setting AGP v2 mode %d\n", rate);
429 /* Construct the new mode word and tell the hardware */
431 command = AGP_MODE_SET_RQ(0, rq);
432 command = AGP_MODE_SET_SBA(command, sba);
433 command = AGP_MODE_SET_FW(command, fw);
434 command = AGP_MODE_SET_RATE(command, rate);
435 command = AGP_MODE_SET_AGP(command, 1);
436 pci_write_config(dev, agp_find_caps(dev) + AGP_COMMAND, command, 4);
437 pci_write_config(mdev, agp_find_caps(mdev) + AGP_COMMAND, command, 4);
443 agp_generic_enable(device_t dev, u_int32_t mode)
445 device_t mdev = agp_find_display();
446 u_int32_t tstatus, mstatus;
449 AGP_DPF("can't find display\n");
453 tstatus = pci_read_config(dev, agp_find_caps(dev) + AGP_STATUS, 4);
454 mstatus = pci_read_config(mdev, agp_find_caps(mdev) + AGP_STATUS, 4);
457 * Check display and bridge for AGP v3 support. AGP v3 allows
458 * more variety in topology than v2, e.g. multiple AGP devices
459 * attached to one bridge, or multiple AGP bridges in one
460 * system. This doesn't attempt to address those situations,
461 * but should work fine for a classic single AGP slot system
464 if (AGP_MODE_GET_MODE_3(mode) &&
465 AGP_MODE_GET_MODE_3(tstatus) &&
466 AGP_MODE_GET_MODE_3(mstatus))
467 return (agp_v3_enable(dev, mdev, mode));
469 return (agp_v2_enable(dev, mdev, mode));
473 agp_generic_alloc_memory(device_t dev, int type, vm_size_t size)
475 struct agp_softc *sc = device_get_softc(dev);
476 struct agp_memory *mem;
478 if ((size & (AGP_PAGE_SIZE - 1)) != 0)
481 if (size > sc->as_maxmem - sc->as_allocated)
485 printf("agp_generic_alloc_memory: unsupported type %d\n",
490 mem = malloc(sizeof *mem, M_AGP, M_WAITOK);
491 mem->am_id = sc->as_nextid++;
494 mem->am_obj = vm_object_allocate(OBJT_DEFAULT, atop(round_page(size)));
495 mem->am_physical = 0;
497 mem->am_is_bound = 0;
498 TAILQ_INSERT_TAIL(&sc->as_memory, mem, am_link);
499 sc->as_allocated += size;
505 agp_generic_free_memory(device_t dev, struct agp_memory *mem)
507 struct agp_softc *sc = device_get_softc(dev);
509 if (mem->am_is_bound)
512 sc->as_allocated -= mem->am_size;
513 TAILQ_REMOVE(&sc->as_memory, mem, am_link);
514 vm_object_deallocate(mem->am_obj);
520 agp_generic_bind_memory(device_t dev, struct agp_memory *mem,
523 struct agp_softc *sc = device_get_softc(dev);
528 /* Do some sanity checks first. */
529 if ((offset & (AGP_PAGE_SIZE - 1)) != 0 ||
530 offset + mem->am_size > AGP_GET_APERTURE(dev)) {
531 device_printf(dev, "binding memory at bad offset %#x\n",
537 * Allocate the pages early, before acquiring the lock,
538 * because vm_page_grab() may sleep and we can't hold a mutex
541 VM_OBJECT_WLOCK(mem->am_obj);
542 for (i = 0; i < mem->am_size; i += PAGE_SIZE) {
544 * Find a page from the object and wire it
545 * down. This page will be mapped using one or more
546 * entries in the GATT (assuming that PAGE_SIZE >=
547 * AGP_PAGE_SIZE. If this is the first call to bind,
548 * the pages will be allocated and zeroed.
550 m = vm_page_grab(mem->am_obj, OFF_TO_IDX(i),
551 VM_ALLOC_WIRED | VM_ALLOC_ZERO);
552 AGP_DPF("found page pa=%#jx\n", (uintmax_t)VM_PAGE_TO_PHYS(m));
554 VM_OBJECT_WUNLOCK(mem->am_obj);
556 mtx_lock(&sc->as_lock);
558 if (mem->am_is_bound) {
559 device_printf(dev, "memory already bound\n");
561 VM_OBJECT_WLOCK(mem->am_obj);
567 * Bind the individual pages and flush the chipset's
570 VM_OBJECT_WLOCK(mem->am_obj);
571 for (i = 0; i < mem->am_size; i += PAGE_SIZE) {
572 m = vm_page_lookup(mem->am_obj, OFF_TO_IDX(i));
575 * Install entries in the GATT, making sure that if
576 * AGP_PAGE_SIZE < PAGE_SIZE and mem->am_size is not
577 * aligned to PAGE_SIZE, we don't modify too many GATT
580 for (j = 0; j < PAGE_SIZE && i + j < mem->am_size;
581 j += AGP_PAGE_SIZE) {
582 vm_offset_t pa = VM_PAGE_TO_PHYS(m) + j;
583 AGP_DPF("binding offset %#jx to pa %#jx\n",
584 (uintmax_t)offset + i + j, (uintmax_t)pa);
585 error = AGP_BIND_PAGE(dev, offset + i + j, pa);
588 * Bail out. Reverse all the mappings
589 * and unwire the pages.
591 for (k = 0; k < i + j; k += AGP_PAGE_SIZE)
592 AGP_UNBIND_PAGE(dev, offset + k);
598 VM_OBJECT_WUNLOCK(mem->am_obj);
601 * Make sure the chipset gets the new mappings.
605 mem->am_offset = offset;
606 mem->am_is_bound = 1;
608 mtx_unlock(&sc->as_lock);
612 mtx_unlock(&sc->as_lock);
613 VM_OBJECT_ASSERT_WLOCKED(mem->am_obj);
614 for (k = 0; k < mem->am_size; k += PAGE_SIZE) {
615 m = vm_page_lookup(mem->am_obj, OFF_TO_IDX(k));
618 vm_page_unwire(m, PQ_INACTIVE);
620 VM_OBJECT_WUNLOCK(mem->am_obj);
626 agp_generic_unbind_memory(device_t dev, struct agp_memory *mem)
628 struct agp_softc *sc = device_get_softc(dev);
632 mtx_lock(&sc->as_lock);
634 if (!mem->am_is_bound) {
635 device_printf(dev, "memory is not bound\n");
636 mtx_unlock(&sc->as_lock);
641 * Unbind the individual pages and flush the chipset's
642 * TLB. Unwire the pages so they can be swapped.
644 for (i = 0; i < mem->am_size; i += AGP_PAGE_SIZE)
645 AGP_UNBIND_PAGE(dev, mem->am_offset + i);
649 VM_OBJECT_WLOCK(mem->am_obj);
650 for (i = 0; i < mem->am_size; i += PAGE_SIZE) {
651 m = vm_page_lookup(mem->am_obj, atop(i));
652 vm_page_unwire(m, PQ_INACTIVE);
654 VM_OBJECT_WUNLOCK(mem->am_obj);
657 mem->am_is_bound = 0;
659 mtx_unlock(&sc->as_lock);
664 /* Helper functions for implementing user/kernel api */
667 agp_acquire_helper(device_t dev, enum agp_acquire_state state)
669 struct agp_softc *sc = device_get_softc(dev);
671 if (sc->as_state != AGP_ACQUIRE_FREE)
673 sc->as_state = state;
679 agp_release_helper(device_t dev, enum agp_acquire_state state)
681 struct agp_softc *sc = device_get_softc(dev);
683 if (sc->as_state == AGP_ACQUIRE_FREE)
686 if (sc->as_state != state)
689 sc->as_state = AGP_ACQUIRE_FREE;
693 static struct agp_memory *
694 agp_find_memory(device_t dev, int id)
696 struct agp_softc *sc = device_get_softc(dev);
697 struct agp_memory *mem;
699 AGP_DPF("searching for memory block %d\n", id);
700 TAILQ_FOREACH(mem, &sc->as_memory, am_link) {
701 AGP_DPF("considering memory block %d\n", mem->am_id);
702 if (mem->am_id == id)
708 /* Implementation of the userland ioctl api */
711 agp_info_user(device_t dev, agp_info *info)
713 struct agp_softc *sc = device_get_softc(dev);
715 bzero(info, sizeof *info);
716 info->bridge_id = pci_get_devid(dev);
718 pci_read_config(dev, agp_find_caps(dev) + AGP_STATUS, 4);
720 info->aper_base = rman_get_start(sc->as_aperture);
723 info->aper_size = AGP_GET_APERTURE(dev) >> 20;
724 info->pg_total = info->pg_system = sc->as_maxmem >> AGP_PAGE_SHIFT;
725 info->pg_used = sc->as_allocated >> AGP_PAGE_SHIFT;
731 agp_setup_user(device_t dev, agp_setup *setup)
733 return AGP_ENABLE(dev, setup->agp_mode);
737 agp_allocate_user(device_t dev, agp_allocate *alloc)
739 struct agp_memory *mem;
741 mem = AGP_ALLOC_MEMORY(dev,
743 alloc->pg_count << AGP_PAGE_SHIFT);
745 alloc->key = mem->am_id;
746 alloc->physical = mem->am_physical;
754 agp_deallocate_user(device_t dev, int id)
756 struct agp_memory *mem = agp_find_memory(dev, id);
759 AGP_FREE_MEMORY(dev, mem);
767 agp_bind_user(device_t dev, agp_bind *bind)
769 struct agp_memory *mem = agp_find_memory(dev, bind->key);
774 return AGP_BIND_MEMORY(dev, mem, bind->pg_start << AGP_PAGE_SHIFT);
778 agp_unbind_user(device_t dev, agp_unbind *unbind)
780 struct agp_memory *mem = agp_find_memory(dev, unbind->key);
785 return AGP_UNBIND_MEMORY(dev, mem);
789 agp_chipset_flush(device_t dev)
792 return (AGP_CHIPSET_FLUSH(dev));
796 agp_open(struct cdev *kdev, int oflags, int devtype, struct thread *td)
798 device_t dev = kdev->si_drv1;
799 struct agp_softc *sc = device_get_softc(dev);
801 if (!sc->as_isopen) {
810 agp_close(struct cdev *kdev, int fflag, int devtype, struct thread *td)
812 device_t dev = kdev->si_drv1;
813 struct agp_softc *sc = device_get_softc(dev);
814 struct agp_memory *mem;
817 * Clear the GATT and force release on last close
819 while ((mem = TAILQ_FIRST(&sc->as_memory)) != NULL) {
820 if (mem->am_is_bound)
821 AGP_UNBIND_MEMORY(dev, mem);
822 AGP_FREE_MEMORY(dev, mem);
824 if (sc->as_state == AGP_ACQUIRE_USER)
825 agp_release_helper(dev, AGP_ACQUIRE_USER);
833 agp_ioctl(struct cdev *kdev, u_long cmd, caddr_t data, int fflag, struct thread *td)
835 device_t dev = kdev->si_drv1;
839 return agp_info_user(dev, (agp_info *) data);
842 return agp_acquire_helper(dev, AGP_ACQUIRE_USER);
845 return agp_release_helper(dev, AGP_ACQUIRE_USER);
848 return agp_setup_user(dev, (agp_setup *)data);
850 case AGPIOC_ALLOCATE:
851 return agp_allocate_user(dev, (agp_allocate *)data);
853 case AGPIOC_DEALLOCATE:
854 return agp_deallocate_user(dev, *(int *) data);
857 return agp_bind_user(dev, (agp_bind *)data);
860 return agp_unbind_user(dev, (agp_unbind *)data);
862 case AGPIOC_CHIPSET_FLUSH:
863 return agp_chipset_flush(dev);
870 agp_mmap(struct cdev *kdev, vm_ooffset_t offset, vm_paddr_t *paddr,
871 int prot, vm_memattr_t *memattr)
873 device_t dev = kdev->si_drv1;
874 struct agp_softc *sc = device_get_softc(dev);
876 if (offset > AGP_GET_APERTURE(dev))
878 if (sc->as_aperture == NULL)
880 *paddr = rman_get_start(sc->as_aperture) + offset;
884 /* Implementation of the kernel api */
889 device_t *children, child;
894 if (devclass_get_devices(agp_devclass, &children, &count) != 0)
897 for (i = 0; i < count; i++) {
898 if (device_is_attached(children[i])) {
903 free(children, M_TEMP);
907 enum agp_acquire_state
908 agp_state(device_t dev)
910 struct agp_softc *sc = device_get_softc(dev);
915 agp_get_info(device_t dev, struct agp_info *info)
917 struct agp_softc *sc = device_get_softc(dev);
920 pci_read_config(dev, agp_find_caps(dev) + AGP_STATUS, 4);
921 if (sc->as_aperture != NULL)
922 info->ai_aperture_base = rman_get_start(sc->as_aperture);
924 info->ai_aperture_base = 0;
925 info->ai_aperture_size = AGP_GET_APERTURE(dev);
926 info->ai_memory_allowed = sc->as_maxmem;
927 info->ai_memory_used = sc->as_allocated;
931 agp_acquire(device_t dev)
933 return agp_acquire_helper(dev, AGP_ACQUIRE_KERNEL);
937 agp_release(device_t dev)
939 return agp_release_helper(dev, AGP_ACQUIRE_KERNEL);
943 agp_enable(device_t dev, u_int32_t mode)
945 return AGP_ENABLE(dev, mode);
948 void *agp_alloc_memory(device_t dev, int type, vm_size_t bytes)
950 return (void *) AGP_ALLOC_MEMORY(dev, type, bytes);
953 void agp_free_memory(device_t dev, void *handle)
955 struct agp_memory *mem = (struct agp_memory *) handle;
956 AGP_FREE_MEMORY(dev, mem);
959 int agp_bind_memory(device_t dev, void *handle, vm_offset_t offset)
961 struct agp_memory *mem = (struct agp_memory *) handle;
962 return AGP_BIND_MEMORY(dev, mem, offset);
965 int agp_unbind_memory(device_t dev, void *handle)
967 struct agp_memory *mem = (struct agp_memory *) handle;
968 return AGP_UNBIND_MEMORY(dev, mem);
971 void agp_memory_info(device_t dev, void *handle, struct
974 struct agp_memory *mem = (struct agp_memory *) handle;
976 mi->ami_size = mem->am_size;
977 mi->ami_physical = mem->am_physical;
978 mi->ami_offset = mem->am_offset;
979 mi->ami_is_bound = mem->am_is_bound;
983 agp_bind_pages(device_t dev, vm_page_t *pages, vm_size_t size,
986 struct agp_softc *sc;
987 vm_offset_t i, j, k, pa;
991 if ((size & (AGP_PAGE_SIZE - 1)) != 0 ||
992 (offset & (AGP_PAGE_SIZE - 1)) != 0)
995 sc = device_get_softc(dev);
997 mtx_lock(&sc->as_lock);
998 for (i = 0; i < size; i += PAGE_SIZE) {
999 m = pages[OFF_TO_IDX(i)];
1000 KASSERT(vm_page_wired(m),
1001 ("agp_bind_pages: page %p hasn't been wired", m));
1004 * Install entries in the GATT, making sure that if
1005 * AGP_PAGE_SIZE < PAGE_SIZE and size is not
1006 * aligned to PAGE_SIZE, we don't modify too many GATT
1009 for (j = 0; j < PAGE_SIZE && i + j < size; j += AGP_PAGE_SIZE) {
1010 pa = VM_PAGE_TO_PHYS(m) + j;
1011 AGP_DPF("binding offset %#jx to pa %#jx\n",
1012 (uintmax_t)offset + i + j, (uintmax_t)pa);
1013 error = AGP_BIND_PAGE(dev, offset + i + j, pa);
1016 * Bail out. Reverse all the mappings.
1018 for (k = 0; k < i + j; k += AGP_PAGE_SIZE)
1019 AGP_UNBIND_PAGE(dev, offset + k);
1021 mtx_unlock(&sc->as_lock);
1029 mtx_unlock(&sc->as_lock);
1034 agp_unbind_pages(device_t dev, vm_size_t size, vm_offset_t offset)
1036 struct agp_softc *sc;
1039 if ((size & (AGP_PAGE_SIZE - 1)) != 0 ||
1040 (offset & (AGP_PAGE_SIZE - 1)) != 0)
1043 sc = device_get_softc(dev);
1045 mtx_lock(&sc->as_lock);
1046 for (i = 0; i < size; i += AGP_PAGE_SIZE)
1047 AGP_UNBIND_PAGE(dev, offset + i);
1051 mtx_unlock(&sc->as_lock);