2 * Copyright (c) 2000 Doug Rabson
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
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>
39 #include <sys/ioccom.h>
40 #include <sys/agpio.h>
42 #include <sys/mutex.h>
44 #include <sys/rwlock.h>
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>
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>
61 #include <machine/bus.h>
62 #include <machine/resource.h>
65 MODULE_VERSION(agp, 1);
67 MALLOC_DEFINE(M_AGP, "agp", "AGP data structures");
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;
75 static struct cdevsw agp_cdevsw = {
76 .d_version = D_VERSION,
77 .d_flags = D_NEEDGIANT,
85 static devclass_t agp_devclass;
87 /* Helper functions for implementing chipset mini drivers. */
90 agp_find_caps(device_t dev)
95 if (pci_find_cap(dev, PCIY_AGP, &capreg) != 0)
101 * Find an AGP display device (if any).
104 agp_find_display(void)
106 devclass_t pci = devclass_find("pci");
107 device_t bus, dev = 0;
109 int busnum, numkids, i;
111 for (busnum = 0; busnum < devclass_get_maxunit(pci); busnum++) {
112 bus = devclass_get_device(pci, busnum);
115 if (device_get_children(bus, &kids, &numkids) != 0)
117 for (i = 0; i < numkids; i++) {
119 if (pci_get_class(dev) == PCIC_DISPLAY
120 && pci_get_subclass(dev) == PCIS_DISPLAY_VGA)
121 if (agp_find_caps(dev)) {
134 agp_alloc_gatt(device_t dev)
136 u_int32_t apsize = AGP_GET_APERTURE(dev);
137 u_int32_t entries = apsize >> AGP_PAGE_SHIFT;
138 struct agp_gatt *gatt;
142 "allocating GATT for aperture of size %dM\n",
143 apsize / (1024*1024));
146 device_printf(dev, "bad aperture size\n");
150 gatt = malloc(sizeof(struct agp_gatt), M_AGP, M_NOWAIT);
154 gatt->ag_entries = entries;
155 gatt->ag_virtual = (void *)kmem_alloc_contig(kernel_arena,
156 entries * sizeof(u_int32_t), M_NOWAIT | M_ZERO, 0, ~0, PAGE_SIZE,
157 0, VM_MEMATTR_WRITE_COMBINING);
158 if (!gatt->ag_virtual) {
160 device_printf(dev, "contiguous allocation failed\n");
164 gatt->ag_physical = vtophys((vm_offset_t) gatt->ag_virtual);
170 agp_free_gatt(struct agp_gatt *gatt)
172 kmem_free(kernel_arena, (vm_offset_t)gatt->ag_virtual,
173 gatt->ag_entries * sizeof(u_int32_t));
177 static u_int agp_max[][2] = {
188 #define agp_max_size (sizeof(agp_max) / sizeof(agp_max[0]))
191 * Sets the PCI resource which represents the AGP aperture.
193 * If not called, the default AGP aperture resource of AGP_APBASE will
194 * be used. Must be called before agp_generic_attach().
197 agp_set_aperture_resource(device_t dev, int rid)
199 struct agp_softc *sc = device_get_softc(dev);
201 sc->as_aperture_rid = rid;
205 agp_generic_attach(device_t dev)
207 struct agp_softc *sc = device_get_softc(dev);
212 * Find and map the aperture, RF_SHAREABLE for DRM but not RF_ACTIVE
213 * because the kernel doesn't need to map it.
216 if (sc->as_aperture_rid != -1) {
217 if (sc->as_aperture_rid == 0)
218 sc->as_aperture_rid = AGP_APBASE;
220 sc->as_aperture = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
221 &sc->as_aperture_rid, RF_SHAREABLE);
222 if (!sc->as_aperture)
227 * Work out an upper bound for agp memory allocation. This
228 * uses a heurisitc table from the Linux driver.
230 memsize = ptoa(realmem) >> 20;
231 for (i = 0; i < agp_max_size; i++) {
232 if (memsize <= agp_max[i][0])
235 if (i == agp_max_size)
236 i = agp_max_size - 1;
237 sc->as_maxmem = agp_max[i][1] << 20U;
240 * The lock is used to prevent re-entry to
241 * agp_generic_bind_memory() since that function can sleep.
243 mtx_init(&sc->as_lock, "agp lock", NULL, MTX_DEF);
246 * Initialise stuff for the userland device.
248 agp_devclass = devclass_find("agp");
249 TAILQ_INIT(&sc->as_memory);
252 sc->as_devnode = make_dev(&agp_cdevsw,
253 0, UID_ROOT, GID_WHEEL, 0600, "agpgart");
254 sc->as_devnode->si_drv1 = dev;
260 agp_free_cdev(device_t dev)
262 struct agp_softc *sc = device_get_softc(dev);
264 destroy_dev(sc->as_devnode);
268 agp_free_res(device_t dev)
270 struct agp_softc *sc = device_get_softc(dev);
272 if (sc->as_aperture != NULL)
273 bus_release_resource(dev, SYS_RES_MEMORY, sc->as_aperture_rid,
275 mtx_destroy(&sc->as_lock);
279 agp_generic_detach(device_t dev)
288 * Default AGP aperture size detection which simply returns the size of
289 * the aperture's PCI resource.
292 agp_generic_get_aperture(device_t dev)
294 struct agp_softc *sc = device_get_softc(dev);
296 return rman_get_size(sc->as_aperture);
300 * Default AGP aperture size setting function, which simply doesn't allow
301 * changes to resource size.
304 agp_generic_set_aperture(device_t dev, u_int32_t aperture)
306 u_int32_t current_aperture;
308 current_aperture = AGP_GET_APERTURE(dev);
309 if (current_aperture != aperture)
316 * This does the enable logic for v3, with the same topology
317 * restrictions as in place for v2 -- one bus, one device on the bus.
320 agp_v3_enable(device_t dev, device_t mdev, u_int32_t mode)
322 u_int32_t tstatus, mstatus;
324 int rq, sba, fw, rate, arqsz, cal;
326 tstatus = pci_read_config(dev, agp_find_caps(dev) + AGP_STATUS, 4);
327 mstatus = pci_read_config(mdev, agp_find_caps(mdev) + AGP_STATUS, 4);
329 /* Set RQ to the min of mode, tstatus and mstatus */
330 rq = AGP_MODE_GET_RQ(mode);
331 if (AGP_MODE_GET_RQ(tstatus) < rq)
332 rq = AGP_MODE_GET_RQ(tstatus);
333 if (AGP_MODE_GET_RQ(mstatus) < rq)
334 rq = AGP_MODE_GET_RQ(mstatus);
337 * ARQSZ - Set the value to the maximum one.
338 * Don't allow the mode register to override values.
340 arqsz = AGP_MODE_GET_ARQSZ(mode);
341 if (AGP_MODE_GET_ARQSZ(tstatus) > rq)
342 rq = AGP_MODE_GET_ARQSZ(tstatus);
343 if (AGP_MODE_GET_ARQSZ(mstatus) > rq)
344 rq = AGP_MODE_GET_ARQSZ(mstatus);
346 /* Calibration cycle - don't allow override by mode register */
347 cal = AGP_MODE_GET_CAL(tstatus);
348 if (AGP_MODE_GET_CAL(mstatus) < cal)
349 cal = AGP_MODE_GET_CAL(mstatus);
351 /* SBA must be supported for AGP v3. */
354 /* Set FW if all three support it. */
355 fw = (AGP_MODE_GET_FW(tstatus)
356 & AGP_MODE_GET_FW(mstatus)
357 & AGP_MODE_GET_FW(mode));
359 /* Figure out the max rate */
360 rate = (AGP_MODE_GET_RATE(tstatus)
361 & AGP_MODE_GET_RATE(mstatus)
362 & AGP_MODE_GET_RATE(mode));
363 if (rate & AGP_MODE_V3_RATE_8x)
364 rate = AGP_MODE_V3_RATE_8x;
366 rate = AGP_MODE_V3_RATE_4x;
368 device_printf(dev, "Setting AGP v3 mode %d\n", rate * 4);
370 pci_write_config(dev, agp_find_caps(dev) + AGP_COMMAND, 0, 4);
372 /* Construct the new mode word and tell the hardware */
374 command = AGP_MODE_SET_RQ(0, rq);
375 command = AGP_MODE_SET_ARQSZ(command, arqsz);
376 command = AGP_MODE_SET_CAL(command, cal);
377 command = AGP_MODE_SET_SBA(command, sba);
378 command = AGP_MODE_SET_FW(command, fw);
379 command = AGP_MODE_SET_RATE(command, rate);
380 command = AGP_MODE_SET_MODE_3(command, 1);
381 command = AGP_MODE_SET_AGP(command, 1);
382 pci_write_config(dev, agp_find_caps(dev) + AGP_COMMAND, command, 4);
383 pci_write_config(mdev, agp_find_caps(mdev) + AGP_COMMAND, command, 4);
389 agp_v2_enable(device_t dev, device_t mdev, u_int32_t mode)
391 u_int32_t tstatus, mstatus;
393 int rq, sba, fw, rate;
395 tstatus = pci_read_config(dev, agp_find_caps(dev) + AGP_STATUS, 4);
396 mstatus = pci_read_config(mdev, agp_find_caps(mdev) + AGP_STATUS, 4);
398 /* Set RQ to the min of mode, tstatus and mstatus */
399 rq = AGP_MODE_GET_RQ(mode);
400 if (AGP_MODE_GET_RQ(tstatus) < rq)
401 rq = AGP_MODE_GET_RQ(tstatus);
402 if (AGP_MODE_GET_RQ(mstatus) < rq)
403 rq = AGP_MODE_GET_RQ(mstatus);
405 /* Set SBA if all three can deal with SBA */
406 sba = (AGP_MODE_GET_SBA(tstatus)
407 & AGP_MODE_GET_SBA(mstatus)
408 & AGP_MODE_GET_SBA(mode));
411 fw = (AGP_MODE_GET_FW(tstatus)
412 & AGP_MODE_GET_FW(mstatus)
413 & AGP_MODE_GET_FW(mode));
415 /* Figure out the max rate */
416 rate = (AGP_MODE_GET_RATE(tstatus)
417 & AGP_MODE_GET_RATE(mstatus)
418 & AGP_MODE_GET_RATE(mode));
419 if (rate & AGP_MODE_V2_RATE_4x)
420 rate = AGP_MODE_V2_RATE_4x;
421 else if (rate & AGP_MODE_V2_RATE_2x)
422 rate = AGP_MODE_V2_RATE_2x;
424 rate = AGP_MODE_V2_RATE_1x;
426 device_printf(dev, "Setting AGP v2 mode %d\n", rate);
428 /* Construct the new mode word and tell the hardware */
430 command = AGP_MODE_SET_RQ(0, rq);
431 command = AGP_MODE_SET_SBA(command, sba);
432 command = AGP_MODE_SET_FW(command, fw);
433 command = AGP_MODE_SET_RATE(command, rate);
434 command = AGP_MODE_SET_AGP(command, 1);
435 pci_write_config(dev, agp_find_caps(dev) + AGP_COMMAND, command, 4);
436 pci_write_config(mdev, agp_find_caps(mdev) + AGP_COMMAND, command, 4);
442 agp_generic_enable(device_t dev, u_int32_t mode)
444 device_t mdev = agp_find_display();
445 u_int32_t tstatus, mstatus;
448 AGP_DPF("can't find display\n");
452 tstatus = pci_read_config(dev, agp_find_caps(dev) + AGP_STATUS, 4);
453 mstatus = pci_read_config(mdev, agp_find_caps(mdev) + AGP_STATUS, 4);
456 * Check display and bridge for AGP v3 support. AGP v3 allows
457 * more variety in topology than v2, e.g. multiple AGP devices
458 * attached to one bridge, or multiple AGP bridges in one
459 * system. This doesn't attempt to address those situations,
460 * but should work fine for a classic single AGP slot system
463 if (AGP_MODE_GET_MODE_3(mode) &&
464 AGP_MODE_GET_MODE_3(tstatus) &&
465 AGP_MODE_GET_MODE_3(mstatus))
466 return (agp_v3_enable(dev, mdev, mode));
468 return (agp_v2_enable(dev, mdev, mode));
472 agp_generic_alloc_memory(device_t dev, int type, vm_size_t size)
474 struct agp_softc *sc = device_get_softc(dev);
475 struct agp_memory *mem;
477 if ((size & (AGP_PAGE_SIZE - 1)) != 0)
480 if (size > sc->as_maxmem - sc->as_allocated)
484 printf("agp_generic_alloc_memory: unsupported type %d\n",
489 mem = malloc(sizeof *mem, M_AGP, M_WAITOK);
490 mem->am_id = sc->as_nextid++;
493 mem->am_obj = vm_object_allocate(OBJT_DEFAULT, atop(round_page(size)));
494 mem->am_physical = 0;
496 mem->am_is_bound = 0;
497 TAILQ_INSERT_TAIL(&sc->as_memory, mem, am_link);
498 sc->as_allocated += size;
504 agp_generic_free_memory(device_t dev, struct agp_memory *mem)
506 struct agp_softc *sc = device_get_softc(dev);
508 if (mem->am_is_bound)
511 sc->as_allocated -= mem->am_size;
512 TAILQ_REMOVE(&sc->as_memory, mem, am_link);
513 vm_object_deallocate(mem->am_obj);
519 agp_generic_bind_memory(device_t dev, struct agp_memory *mem,
522 struct agp_softc *sc = device_get_softc(dev);
527 /* Do some sanity checks first. */
528 if ((offset & (AGP_PAGE_SIZE - 1)) != 0 ||
529 offset + mem->am_size > AGP_GET_APERTURE(dev)) {
530 device_printf(dev, "binding memory at bad offset %#x\n",
536 * Allocate the pages early, before acquiring the lock,
537 * because vm_page_grab() may sleep and we can't hold a mutex
540 VM_OBJECT_WLOCK(mem->am_obj);
541 for (i = 0; i < mem->am_size; i += PAGE_SIZE) {
543 * Find a page from the object and wire it
544 * down. This page will be mapped using one or more
545 * entries in the GATT (assuming that PAGE_SIZE >=
546 * AGP_PAGE_SIZE. If this is the first call to bind,
547 * the pages will be allocated and zeroed.
549 m = vm_page_grab(mem->am_obj, OFF_TO_IDX(i),
550 VM_ALLOC_WIRED | VM_ALLOC_ZERO);
551 AGP_DPF("found page pa=%#jx\n", (uintmax_t)VM_PAGE_TO_PHYS(m));
553 VM_OBJECT_WUNLOCK(mem->am_obj);
555 mtx_lock(&sc->as_lock);
557 if (mem->am_is_bound) {
558 device_printf(dev, "memory already bound\n");
560 VM_OBJECT_WLOCK(mem->am_obj);
566 * Bind the individual pages and flush the chipset's
569 VM_OBJECT_WLOCK(mem->am_obj);
570 for (i = 0; i < mem->am_size; i += PAGE_SIZE) {
571 m = vm_page_lookup(mem->am_obj, OFF_TO_IDX(i));
574 * Install entries in the GATT, making sure that if
575 * AGP_PAGE_SIZE < PAGE_SIZE and mem->am_size is not
576 * aligned to PAGE_SIZE, we don't modify too many GATT
579 for (j = 0; j < PAGE_SIZE && i + j < mem->am_size;
580 j += AGP_PAGE_SIZE) {
581 vm_offset_t pa = VM_PAGE_TO_PHYS(m) + j;
582 AGP_DPF("binding offset %#jx to pa %#jx\n",
583 (uintmax_t)offset + i + j, (uintmax_t)pa);
584 error = AGP_BIND_PAGE(dev, offset + i + j, pa);
587 * Bail out. Reverse all the mappings
588 * and unwire the pages.
590 for (k = 0; k < i + j; k += AGP_PAGE_SIZE)
591 AGP_UNBIND_PAGE(dev, offset + k);
597 VM_OBJECT_WUNLOCK(mem->am_obj);
600 * Make sure the chipset gets the new mappings.
604 mem->am_offset = offset;
605 mem->am_is_bound = 1;
607 mtx_unlock(&sc->as_lock);
611 mtx_unlock(&sc->as_lock);
612 VM_OBJECT_ASSERT_WLOCKED(mem->am_obj);
613 for (k = 0; k < mem->am_size; k += PAGE_SIZE) {
614 m = vm_page_lookup(mem->am_obj, OFF_TO_IDX(k));
618 vm_page_unwire(m, PQ_INACTIVE);
621 VM_OBJECT_WUNLOCK(mem->am_obj);
627 agp_generic_unbind_memory(device_t dev, struct agp_memory *mem)
629 struct agp_softc *sc = device_get_softc(dev);
633 mtx_lock(&sc->as_lock);
635 if (!mem->am_is_bound) {
636 device_printf(dev, "memory is not bound\n");
637 mtx_unlock(&sc->as_lock);
643 * Unbind the individual pages and flush the chipset's
644 * TLB. Unwire the pages so they can be swapped.
646 for (i = 0; i < mem->am_size; i += AGP_PAGE_SIZE)
647 AGP_UNBIND_PAGE(dev, mem->am_offset + i);
651 VM_OBJECT_WLOCK(mem->am_obj);
652 for (i = 0; i < mem->am_size; i += PAGE_SIZE) {
653 m = vm_page_lookup(mem->am_obj, atop(i));
655 vm_page_unwire(m, PQ_INACTIVE);
658 VM_OBJECT_WUNLOCK(mem->am_obj);
661 mem->am_is_bound = 0;
663 mtx_unlock(&sc->as_lock);
668 /* Helper functions for implementing user/kernel api */
671 agp_acquire_helper(device_t dev, enum agp_acquire_state state)
673 struct agp_softc *sc = device_get_softc(dev);
675 if (sc->as_state != AGP_ACQUIRE_FREE)
677 sc->as_state = state;
683 agp_release_helper(device_t dev, enum agp_acquire_state state)
685 struct agp_softc *sc = device_get_softc(dev);
687 if (sc->as_state == AGP_ACQUIRE_FREE)
690 if (sc->as_state != state)
693 sc->as_state = AGP_ACQUIRE_FREE;
697 static struct agp_memory *
698 agp_find_memory(device_t dev, int id)
700 struct agp_softc *sc = device_get_softc(dev);
701 struct agp_memory *mem;
703 AGP_DPF("searching for memory block %d\n", id);
704 TAILQ_FOREACH(mem, &sc->as_memory, am_link) {
705 AGP_DPF("considering memory block %d\n", mem->am_id);
706 if (mem->am_id == id)
712 /* Implementation of the userland ioctl api */
715 agp_info_user(device_t dev, agp_info *info)
717 struct agp_softc *sc = device_get_softc(dev);
719 bzero(info, sizeof *info);
720 info->bridge_id = pci_get_devid(dev);
722 pci_read_config(dev, agp_find_caps(dev) + AGP_STATUS, 4);
724 info->aper_base = rman_get_start(sc->as_aperture);
727 info->aper_size = AGP_GET_APERTURE(dev) >> 20;
728 info->pg_total = info->pg_system = sc->as_maxmem >> AGP_PAGE_SHIFT;
729 info->pg_used = sc->as_allocated >> AGP_PAGE_SHIFT;
735 agp_setup_user(device_t dev, agp_setup *setup)
737 return AGP_ENABLE(dev, setup->agp_mode);
741 agp_allocate_user(device_t dev, agp_allocate *alloc)
743 struct agp_memory *mem;
745 mem = AGP_ALLOC_MEMORY(dev,
747 alloc->pg_count << AGP_PAGE_SHIFT);
749 alloc->key = mem->am_id;
750 alloc->physical = mem->am_physical;
758 agp_deallocate_user(device_t dev, int id)
760 struct agp_memory *mem = agp_find_memory(dev, id);
763 AGP_FREE_MEMORY(dev, mem);
771 agp_bind_user(device_t dev, agp_bind *bind)
773 struct agp_memory *mem = agp_find_memory(dev, bind->key);
778 return AGP_BIND_MEMORY(dev, mem, bind->pg_start << AGP_PAGE_SHIFT);
782 agp_unbind_user(device_t dev, agp_unbind *unbind)
784 struct agp_memory *mem = agp_find_memory(dev, unbind->key);
789 return AGP_UNBIND_MEMORY(dev, mem);
793 agp_chipset_flush(device_t dev)
796 return (AGP_CHIPSET_FLUSH(dev));
800 agp_open(struct cdev *kdev, int oflags, int devtype, struct thread *td)
802 device_t dev = kdev->si_drv1;
803 struct agp_softc *sc = device_get_softc(dev);
805 if (!sc->as_isopen) {
814 agp_close(struct cdev *kdev, int fflag, int devtype, struct thread *td)
816 device_t dev = kdev->si_drv1;
817 struct agp_softc *sc = device_get_softc(dev);
818 struct agp_memory *mem;
821 * Clear the GATT and force release on last close
823 while ((mem = TAILQ_FIRST(&sc->as_memory)) != 0) {
824 if (mem->am_is_bound)
825 AGP_UNBIND_MEMORY(dev, mem);
826 AGP_FREE_MEMORY(dev, mem);
828 if (sc->as_state == AGP_ACQUIRE_USER)
829 agp_release_helper(dev, AGP_ACQUIRE_USER);
837 agp_ioctl(struct cdev *kdev, u_long cmd, caddr_t data, int fflag, struct thread *td)
839 device_t dev = kdev->si_drv1;
843 return agp_info_user(dev, (agp_info *) data);
846 return agp_acquire_helper(dev, AGP_ACQUIRE_USER);
849 return agp_release_helper(dev, AGP_ACQUIRE_USER);
852 return agp_setup_user(dev, (agp_setup *)data);
854 case AGPIOC_ALLOCATE:
855 return agp_allocate_user(dev, (agp_allocate *)data);
857 case AGPIOC_DEALLOCATE:
858 return agp_deallocate_user(dev, *(int *) data);
861 return agp_bind_user(dev, (agp_bind *)data);
864 return agp_unbind_user(dev, (agp_unbind *)data);
866 case AGPIOC_CHIPSET_FLUSH:
867 return agp_chipset_flush(dev);
874 agp_mmap(struct cdev *kdev, vm_ooffset_t offset, vm_paddr_t *paddr,
875 int prot, vm_memattr_t *memattr)
877 device_t dev = kdev->si_drv1;
878 struct agp_softc *sc = device_get_softc(dev);
880 if (offset > AGP_GET_APERTURE(dev))
882 if (sc->as_aperture == NULL)
884 *paddr = rman_get_start(sc->as_aperture) + offset;
888 /* Implementation of the kernel api */
893 device_t *children, child;
898 if (devclass_get_devices(agp_devclass, &children, &count) != 0)
901 for (i = 0; i < count; i++) {
902 if (device_is_attached(children[i])) {
907 free(children, M_TEMP);
911 enum agp_acquire_state
912 agp_state(device_t dev)
914 struct agp_softc *sc = device_get_softc(dev);
919 agp_get_info(device_t dev, struct agp_info *info)
921 struct agp_softc *sc = device_get_softc(dev);
924 pci_read_config(dev, agp_find_caps(dev) + AGP_STATUS, 4);
925 if (sc->as_aperture != NULL)
926 info->ai_aperture_base = rman_get_start(sc->as_aperture);
928 info->ai_aperture_base = 0;
929 info->ai_aperture_size = AGP_GET_APERTURE(dev);
930 info->ai_memory_allowed = sc->as_maxmem;
931 info->ai_memory_used = sc->as_allocated;
935 agp_acquire(device_t dev)
937 return agp_acquire_helper(dev, AGP_ACQUIRE_KERNEL);
941 agp_release(device_t dev)
943 return agp_release_helper(dev, AGP_ACQUIRE_KERNEL);
947 agp_enable(device_t dev, u_int32_t mode)
949 return AGP_ENABLE(dev, mode);
952 void *agp_alloc_memory(device_t dev, int type, vm_size_t bytes)
954 return (void *) AGP_ALLOC_MEMORY(dev, type, bytes);
957 void agp_free_memory(device_t dev, void *handle)
959 struct agp_memory *mem = (struct agp_memory *) handle;
960 AGP_FREE_MEMORY(dev, mem);
963 int agp_bind_memory(device_t dev, void *handle, vm_offset_t offset)
965 struct agp_memory *mem = (struct agp_memory *) handle;
966 return AGP_BIND_MEMORY(dev, mem, offset);
969 int agp_unbind_memory(device_t dev, void *handle)
971 struct agp_memory *mem = (struct agp_memory *) handle;
972 return AGP_UNBIND_MEMORY(dev, mem);
975 void agp_memory_info(device_t dev, void *handle, struct
978 struct agp_memory *mem = (struct agp_memory *) handle;
980 mi->ami_size = mem->am_size;
981 mi->ami_physical = mem->am_physical;
982 mi->ami_offset = mem->am_offset;
983 mi->ami_is_bound = mem->am_is_bound;
987 agp_bind_pages(device_t dev, vm_page_t *pages, vm_size_t size,
990 struct agp_softc *sc;
991 vm_offset_t i, j, k, pa;
995 if ((size & (AGP_PAGE_SIZE - 1)) != 0 ||
996 (offset & (AGP_PAGE_SIZE - 1)) != 0)
999 sc = device_get_softc(dev);
1001 mtx_lock(&sc->as_lock);
1002 for (i = 0; i < size; i += PAGE_SIZE) {
1003 m = pages[OFF_TO_IDX(i)];
1004 KASSERT(m->wire_count > 0,
1005 ("agp_bind_pages: page %p hasn't been wired", m));
1008 * Install entries in the GATT, making sure that if
1009 * AGP_PAGE_SIZE < PAGE_SIZE and size is not
1010 * aligned to PAGE_SIZE, we don't modify too many GATT
1013 for (j = 0; j < PAGE_SIZE && i + j < size; j += AGP_PAGE_SIZE) {
1014 pa = VM_PAGE_TO_PHYS(m) + j;
1015 AGP_DPF("binding offset %#jx to pa %#jx\n",
1016 (uintmax_t)offset + i + j, (uintmax_t)pa);
1017 error = AGP_BIND_PAGE(dev, offset + i + j, pa);
1020 * Bail out. Reverse all the mappings.
1022 for (k = 0; k < i + j; k += AGP_PAGE_SIZE)
1023 AGP_UNBIND_PAGE(dev, offset + k);
1025 mtx_unlock(&sc->as_lock);
1033 mtx_unlock(&sc->as_lock);
1038 agp_unbind_pages(device_t dev, vm_size_t size, vm_offset_t offset)
1040 struct agp_softc *sc;
1043 if ((size & (AGP_PAGE_SIZE - 1)) != 0 ||
1044 (offset & (AGP_PAGE_SIZE - 1)) != 0)
1047 sc = device_get_softc(dev);
1049 mtx_lock(&sc->as_lock);
1050 for (i = 0; i < size; i += AGP_PAGE_SIZE)
1051 AGP_UNBIND_PAGE(dev, offset + i);
1055 mtx_unlock(&sc->as_lock);