#include     <stdio.h>
    #include     <stdlib.h>
    #include     <unistd.h
    #include     <sys/ioccom.h>
    #include     <sys/types.h>
    #include     <fcntl.h>
    #include     <errno.h>
    #include     <sys/mman.h>
    #include     <sys/agpgart.h>
    #define AGP_PAGE_SIZE             4096
    int main(int argc, char *argv[])
    {
               int fd, ret;
               agp_allocate_t alloc;
               agp_bind_t bindinfo;
               agp_info_t agpinfo;
               agp_setup_t modesetup;
               int *p = NULL;
               off_t mapoff;
               size_t maplen;
               if((fd = open(AGP_DEVICE, O_RDWR))== -1) {
                           printf("open AGP_DEVICE error with %d\n", errno);\
                           exit(-1);
               }
               printf("device opened\n");
               ret = ioctl(fd, AGPIOC_INFO, &agpinfo);
               if(ret == -1) {
                           printf("Get info error %d\n", errno);
                           exit(-1);
               }
               printf("AGPSTAT is %x\n", agpinfo.agpi_mode);
               printf("APBASE is %x\n", agpinfo.agpi_aperbase);
               printf("APSIZE is %dMB\n", agpinfo.agpi_apersize);
               printf("pg_total is %d\n", agpinfo.agpi_pgtotal);
               ret = ioctl(fd, AGPIOC_ACQUIRE);
               if(ret == -1) {
                           printf(" Acquire GART error %d\n", errno);
                           exit(-1);
               }
               modesetup.agps_mode = agpinfo.agpi_mode;
            ret = ioctl(fd, AGPIOC_SETUP, &modesetup);
            if(ret == -1) {
                       printf("set up AGP mode error\n", errno);
                       exit(-1);
            }
            printf("Please input the number of pages you want to allocate\n");
            scanf("%d", &alloc.agpa_pgcount);
            alloc.agpa_type = AGP_NORMAL;
            ret = ioctl(fd, AGPIOC_ALLOCATE, &alloc);
            if(ret == -1) {
                       printf("Allocate memory error %d\n", errno);
                       exit(-1);
            }
            printf("Please input the aperture page offset to bind\n");
            scanf("%d", &bindinfo.agpb_pgstart);
            bindinfo.agpb_key = alloc.agpa_key;
            ret = ioctl(fd, AGPIOC_BIND, &bindinfo);
            if(ret == -1) {
                       printf("Bind error %d\n", errno);
                       exit(-1);
            }
            printf("Bind successful\n");
            /*
              * Now gart aperture space from (bindinfo.agpb_pgstart) to
              * (bindinfo.agpb_pgstart + alloc.agpa_pgcount) can be used for
              * AGP graphics transactions
              */
            ...
            /*
              * mmap can allow user processes to store graphics data
              * to the aperture space
              */
            maplen = alloc.agpa_pgcount * AGP_PAGE_SIZE;
            mapoff = bindinfo.agpb_pgstart * AGP_PAGE_SIZE;
            p = (int *)mmap((caddr_t)0, maplen, (PROT_READ | PROT_WRITE),
                               MAP_SHARED, fd, mapoff);
            if (p == MAP_FAILED) {
                       printf("Mmap error %d\n", errno);
                       exit(-1);
            }
            printf("Mmap successful\n");
                 /*
                  * When user processes finish access to the aperture space,
                  * unmap the memory range
                  */
                 munmap((void *)p, maplen);
                 ...
                 /*
                  * After finishing AGP transactions, the resources can be freed
                  * step by step or simply by close device.
                  */
                 ret = ioctl(fd, AGPIOC_DEALLOCATE, alloc.agpa_key);
                 if(ret == -1) {
                         printf(" Deallocate memory error %d\n", errno);
                         exit(-1);
                 }
                 ret = ioctl(fd, AGPIOC_RELEASE);
                 if(ret == -1) {
                         printf(" Release GART error %d\n", errno);
                         exit(-1);
                 }
                 close(fd);
}

Reply via email to