Subversion Repositories Kolibri OS

Rev

Rev 948 | Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1.  
  2. #include "types.h"
  3. #include "link.h"
  4.  
  5. #include <stdio.h>
  6. #include <malloc.h>
  7. #include <memory.h>
  8.  
  9. #include "pci.h"
  10. #include "agp.h"
  11.  
  12. #include "syscall.h"
  13.  
  14.  
  15. agp_t *bridge;
  16.  
  17.  
  18. int __stdcall srv_agp(ioctl_t *io);
  19.  
  20.  
  21. u32_t __stdcall drvEntry(int action)
  22. {
  23.     u32_t retval;
  24.  
  25.     int i;
  26.  
  27.     if(action != 1)
  28.         return 0;
  29.  
  30.     if(!dbg_open("/rd/1/drivers/agp.log"))
  31.     {
  32.         printf("Can't open /rd/1/drivers/agp.log\nExit\n");
  33.         return 0;
  34.     }
  35.  
  36.     if( FindPciDevice() == 0)
  37.     {
  38.         dbgprintf("Device not found\n");
  39.         return 0;
  40.     };
  41.  
  42.     return 0;
  43.  
  44. //    retval = RegService("AGP", srv_2d);
  45. //    dbgprintf("reg service %s as: %x\n", "HDRAW", retval);
  46.  
  47. //    return retval;
  48. };
  49.  
  50.  
  51. #include "pci.inc"
  52.  
  53. static void intel_8xx_tlbflush(void *mem)
  54. {
  55.     u32_t temp;
  56.  
  57.     temp = pciReadLong(bridge->PciTag, INTEL_AGPCTRL);
  58.     pciWriteLong(bridge->PciTag, INTEL_AGPCTRL, temp & ~(1 << 7));
  59.     temp = pciReadLong(bridge->PciTag, INTEL_AGPCTRL);
  60.     pciWriteLong(bridge->PciTag, INTEL_AGPCTRL, temp | (1 << 7));
  61. }
  62.  
  63.  
  64. static aper_size_t intel_8xx_sizes[7] =
  65. {
  66.     { 256, 65536, 64,  0 },
  67.     { 128, 32768, 32, 32 },
  68.     {  64, 16384, 16, 48 },
  69.     {  32,  8192,  8, 56 },
  70.     {  16,  4096,  4, 60 },
  71.     {   8,  2048,  2, 62 },
  72.     {   4,  1024,  1, 63 }
  73. };
  74.  
  75.  
  76.  
  77. static int intel_845_configure()
  78. {
  79.     u32_t temp;
  80.     u8_t  temp2;
  81.     aper_size_t *current_size;
  82.  
  83.     current_size = bridge->current_size;
  84.  
  85.         /* aperture size */
  86.     pciWriteByte(bridge->PciTag, INTEL_APSIZE, current_size->size_value);
  87.  
  88.     dbgprintf("INTEL_APSIZE %d\n", current_size->size_value );
  89.  
  90.     if (bridge->apbase_config != 0)
  91.     {
  92.         pciWriteLong(bridge->PciTag, AGP_APBASE, bridge->apbase_config);
  93.     }
  94.     else
  95.     {
  96.                 /* address to map to */
  97.         temp = pciReadLong(bridge->PciTag, AGP_APBASE);
  98.         bridge->gart_addr = (temp & PCI_MAP_MEMORY_ADDRESS_MASK);
  99.         bridge->apbase_config = temp;
  100.         }
  101.  
  102.     dbgprintf("AGP_APBASE %x\n", temp );
  103.  
  104.         /* attbase - aperture base */
  105.     pciWriteLong(bridge->PciTag, INTEL_ATTBASE, bridge->gatt_dma);
  106.  
  107.         /* agpctrl */
  108.     pciWriteLong(bridge->PciTag, INTEL_AGPCTRL, 0x0000);
  109.  
  110.         /* agpm */
  111.     temp2 = pciReadByte(bridge->PciTag, INTEL_I845_AGPM);
  112.     pciWriteByte(bridge->PciTag, INTEL_I845_AGPM, temp2 | (1 << 1));
  113.         /* clear any possible error conditions */
  114.     pciWriteWord(bridge->PciTag, INTEL_I845_ERRSTS, 0x001c);
  115.         return 0;
  116. }
  117.  
  118.  
  119. int agp_generic_create_gatt_table()
  120. {
  121.     count_t pages;
  122.  
  123.     pages = bridge->current_size->pages_count;
  124.  
  125.     if( bridge->gatt_dma = AllocPages(pages))
  126.     {
  127.         if(bridge->gatt_table =
  128.            (u32_t*)MapIoMem((void*)bridge->gatt_dma,
  129.                             pages<<12, PG_SW+PG_NOCACHE))
  130.         {
  131.             dbgprintf("gatt map %x at %x %d pages\n",bridge->gatt_dma ,
  132.                        bridge->gatt_table, pages);
  133.  
  134.         /* AK: bogus, should encode addresses > 4GB */
  135.  
  136.             u32_t volatile *table = bridge->gatt_table;
  137.  
  138.             count_t count = bridge->current_size->num_entries;
  139.  
  140.             while(count--) {            /* FIXME memset */
  141.                 addr_t tmp;
  142.  
  143.                 *table = 0;
  144.                 table++;
  145.             }
  146.             return 1;
  147.         };
  148.     };
  149.     dbgprintf("unable to get memory for "
  150.               "graphics translation table.\n");
  151.         return 0;
  152. }
  153.  
  154.  
  155. static int __intel_8xx_fetch_size(u8_t temp)
  156. {
  157.         int i;
  158.     aper_size_t *values;
  159.  
  160.     values = bridge->aperture_sizes;
  161.  
  162.     values = intel_8xx_sizes;
  163.  
  164.     for (i = 0; i < 7; i++)
  165.     {
  166.         if (temp == values[i].size_value)
  167.         {
  168.             bridge->previous_size =
  169.                 bridge->current_size = (void *) (values + i);
  170.             bridge->aperture_size_idx = i;
  171.                         return values[i].size;
  172.                 }
  173.         }
  174.         return 0;
  175. }
  176.  
  177. static int intel_8xx_fetch_size(void)
  178. {
  179.     u8_t temp;
  180.  
  181.     temp = pciReadByte(bridge->PciTag, INTEL_APSIZE);
  182.         return __intel_8xx_fetch_size(temp);
  183. }
  184.  
  185.  
  186. int agp_bind_memory(addr_t agp_addr, addr_t dma_addr, size_t size)
  187. {
  188.         int ret_val;
  189.     count_t count;
  190.  
  191. //    if (curr == NULL)
  192. //        return -EINVAL;
  193.  
  194. //    if (curr->is_bound == TRUE) {
  195. //        printk(KERN_INFO PFX "memory %p is already bound!\n", curr);
  196. //        return -EINVAL;
  197. //    }
  198. //    if (curr->is_flushed == FALSE) {
  199. //        curr->bridge->driver->cache_flush();
  200. //        curr->is_flushed = TRUE;
  201. //    }
  202. //    ret_val = curr->bridge->driver->insert_memory(curr, pg_start, curr->type);
  203.  
  204.     u32_t volatile *table = &bridge->gatt_table[agp_addr>>12];
  205.  
  206.     count = size >> 12;
  207.  
  208.     dma_addr |= 0x00000017;
  209.  
  210.     while(count--)
  211.     {
  212.         *table = dma_addr;
  213.         table++;
  214.         dma_addr+=4096;
  215.     }
  216.  
  217.     bridge->tlb_flush(NULL);
  218.  
  219. //    if (ret_val != 0)
  220. //        return ret_val;
  221.  
  222. //    curr->is_bound = TRUE;
  223. //    curr->pg_start = pg_start;
  224.         return 0;
  225. }
  226.  
  227. void get_agp_version(agp_t *bridge)
  228. {
  229.     u32_t ncapid;
  230.  
  231.         /* Exit early if already set by errata workarounds. */
  232.         if (bridge->major_version != 0)
  233.                 return;
  234.  
  235.     ncapid = pciReadLong(bridge->PciTag, bridge->capndx);
  236.         bridge->major_version = (ncapid >> AGP_MAJOR_VERSION_SHIFT) & 0xf;
  237.         bridge->minor_version = (ncapid >> AGP_MINOR_VERSION_SHIFT) & 0xf;
  238. }
  239.  
  240. static void agp_v2_parse_one(u32_t *requested_mode, u32_t *bridge_agpstat, u32_t *vga_agpstat)
  241. {
  242.     u32_t tmp;
  243.  
  244.         if (*requested_mode & AGP2_RESERVED_MASK) {
  245.         dbgprintf("reserved bits set (%x) in mode 0x%x. Fixed.\n",
  246.                         *requested_mode & AGP2_RESERVED_MASK, *requested_mode);
  247.                 *requested_mode &= ~AGP2_RESERVED_MASK;
  248.         }
  249.  
  250.         /* Check the speed bits make sense. Only one should be set. */
  251.         tmp = *requested_mode & 7;
  252.         switch (tmp) {
  253.                 case 0:
  254.             dbgprintf("Setting to x1 mode.\n");
  255.                         *requested_mode |= AGPSTAT2_1X;
  256.                         break;
  257.                 case 1:
  258.                 case 2:
  259.                         break;
  260.                 case 3:
  261.                         *requested_mode &= ~(AGPSTAT2_1X);      /* rate=2 */
  262.                         break;
  263.                 case 4:
  264.                         break;
  265.                 case 5:
  266.                 case 6:
  267.                 case 7:
  268.                         *requested_mode &= ~(AGPSTAT2_1X|AGPSTAT2_2X); /* rate=4*/
  269.                         break;
  270.         }
  271.  
  272.         /* disable SBA if it's not supported */
  273.         if (!((*bridge_agpstat & AGPSTAT_SBA) && (*vga_agpstat & AGPSTAT_SBA) && (*requested_mode & AGPSTAT_SBA)))
  274.                 *bridge_agpstat &= ~AGPSTAT_SBA;
  275.  
  276.         /* Set rate */
  277.         if (!((*bridge_agpstat & AGPSTAT2_4X) && (*vga_agpstat & AGPSTAT2_4X) && (*requested_mode & AGPSTAT2_4X)))
  278.                 *bridge_agpstat &= ~AGPSTAT2_4X;
  279.  
  280.         if (!((*bridge_agpstat & AGPSTAT2_2X) && (*vga_agpstat & AGPSTAT2_2X) && (*requested_mode & AGPSTAT2_2X)))
  281.                 *bridge_agpstat &= ~AGPSTAT2_2X;
  282.  
  283.         if (!((*bridge_agpstat & AGPSTAT2_1X) && (*vga_agpstat & AGPSTAT2_1X) && (*requested_mode & AGPSTAT2_1X)))
  284.                 *bridge_agpstat &= ~AGPSTAT2_1X;
  285.  
  286.         /* Now we know what mode it should be, clear out the unwanted bits. */
  287.         if (*bridge_agpstat & AGPSTAT2_4X)
  288.                 *bridge_agpstat &= ~(AGPSTAT2_1X | AGPSTAT2_2X);        /* 4X */
  289.  
  290.         if (*bridge_agpstat & AGPSTAT2_2X)
  291.                 *bridge_agpstat &= ~(AGPSTAT2_1X | AGPSTAT2_4X);        /* 2X */
  292.  
  293.         if (*bridge_agpstat & AGPSTAT2_1X)
  294.                 *bridge_agpstat &= ~(AGPSTAT2_2X | AGPSTAT2_4X);        /* 1X */
  295.  
  296.         /* Apply any errata. */
  297.     if (bridge->flags & AGP_ERRATA_FASTWRITES)
  298.                 *bridge_agpstat &= ~AGPSTAT_FW;
  299.  
  300.     if (bridge->flags & AGP_ERRATA_SBA)
  301.                 *bridge_agpstat &= ~AGPSTAT_SBA;
  302.  
  303.     if (bridge->flags & AGP_ERRATA_1X) {
  304.                 *bridge_agpstat &= ~(AGPSTAT2_2X | AGPSTAT2_4X);
  305.                 *bridge_agpstat |= AGPSTAT2_1X;
  306.         }
  307.  
  308.         /* If we've dropped down to 1X, disable fast writes. */
  309.         if (*bridge_agpstat & AGPSTAT2_1X)
  310.                 *bridge_agpstat &= ~AGPSTAT_FW;
  311. }
  312.  
  313.  
  314. static void agp_v3_parse_one(u32_t *requested_mode,
  315.                              u32_t *bridge_agpstat,
  316.                              u32_t *vga_agpstat)
  317. {
  318.     u32_t origbridge = *bridge_agpstat, origvga = *vga_agpstat;
  319.     u32_t tmp;
  320.  
  321.     if (*requested_mode & AGP3_RESERVED_MASK)
  322.     {
  323.         dbgprintf("reserved bits set (%x) in mode 0x%x. Fixed.\n",
  324.                         *requested_mode & AGP3_RESERVED_MASK, *requested_mode);
  325.                 *requested_mode &= ~AGP3_RESERVED_MASK;
  326.         }
  327.  
  328.         /* Check the speed bits make sense. */
  329.         tmp = *requested_mode & 7;
  330.         if (tmp == 0) {
  331.         dbgprintf("Setting to AGP3 x4 mode.\n");
  332.                 *requested_mode |= AGPSTAT3_4X;
  333.         }
  334.         if (tmp >= 3) {
  335.         dbgprintf("Setting to AGP3 x8 mode.\n");
  336.                 *requested_mode = (*requested_mode & ~7) | AGPSTAT3_8X;
  337.         }
  338.  
  339.         /* ARQSZ - Set the value to the maximum one.
  340.          * Don't allow the mode register to override values. */
  341.         *bridge_agpstat = ((*bridge_agpstat & ~AGPSTAT_ARQSZ) |
  342.         max_t(u32_t,(*bridge_agpstat & AGPSTAT_ARQSZ),(*vga_agpstat & AGPSTAT_ARQSZ)));
  343.  
  344.         /* Calibration cycle.
  345.          * Don't allow the mode register to override values. */
  346.         *bridge_agpstat = ((*bridge_agpstat & ~AGPSTAT_CAL_MASK) |
  347.         min_t(u32_t,(*bridge_agpstat & AGPSTAT_CAL_MASK),(*vga_agpstat & AGPSTAT_CAL_MASK)));
  348.  
  349.         /* SBA *must* be supported for AGP v3 */
  350.         *bridge_agpstat |= AGPSTAT_SBA;
  351.  
  352.         /*
  353.          * Set speed.
  354.          * Check for invalid speeds. This can happen when applications
  355.          * written before the AGP 3.0 standard pass AGP2.x modes to AGP3 hardware
  356.          */
  357.         if (*requested_mode & AGPSTAT_MODE_3_0) {
  358.                 /*
  359.                  * Caller hasn't a clue what it is doing. Bridge is in 3.0 mode,
  360.                  * have been passed a 3.0 mode, but with 2.x speed bits set.
  361.                  * AGP2.x 4x -> AGP3.0 4x.
  362.                  */
  363.                 if (*requested_mode & AGPSTAT2_4X) {
  364.             dbgprintf("broken AGP3 flags (%x). Fixed.\n", *requested_mode);
  365.                         *requested_mode &= ~AGPSTAT2_4X;
  366.                         *requested_mode |= AGPSTAT3_4X;
  367.                 }
  368.         } else {
  369.                 /*
  370.                  * The caller doesn't know what they are doing. We are in 3.0 mode,
  371.                  * but have been passed an AGP 2.x mode.
  372.                  * Convert AGP 1x,2x,4x -> AGP 3.0 4x.
  373.                  */
  374.         dbgprintf("broken AGP2 flags (%x) in AGP3 mode. Fixed.\n",*requested_mode);
  375.                 *requested_mode &= ~(AGPSTAT2_4X | AGPSTAT2_2X | AGPSTAT2_1X);
  376.                 *requested_mode |= AGPSTAT3_4X;
  377.         }
  378.  
  379.         if (*requested_mode & AGPSTAT3_8X) {
  380.                 if (!(*bridge_agpstat & AGPSTAT3_8X)) {
  381.                         *bridge_agpstat &= ~(AGPSTAT3_8X | AGPSTAT3_RSVD);
  382.                         *bridge_agpstat |= AGPSTAT3_4X;
  383.             dbgprintf("requested AGPx8 but bridge not capable.\n");
  384.                         return;
  385.                 }
  386.                 if (!(*vga_agpstat & AGPSTAT3_8X)) {
  387.                         *bridge_agpstat &= ~(AGPSTAT3_8X | AGPSTAT3_RSVD);
  388.                         *bridge_agpstat |= AGPSTAT3_4X;
  389.             dbgprintf("requested AGPx8 but graphic card not capable.\n");
  390.                         return;
  391.                 }
  392.                 /* All set, bridge & device can do AGP x8*/
  393.                 *bridge_agpstat &= ~(AGPSTAT3_4X | AGPSTAT3_RSVD);
  394.                 goto done;
  395.  
  396.         } else {
  397.  
  398.                 /*
  399.                  * If we didn't specify AGPx8, we can only do x4.
  400.                  * If the hardware can't do x4, we're up shit creek, and never
  401.                  *  should have got this far.
  402.                  */
  403.                 *bridge_agpstat &= ~(AGPSTAT3_8X | AGPSTAT3_RSVD);
  404.                 if ((*bridge_agpstat & AGPSTAT3_4X) && (*vga_agpstat & AGPSTAT3_4X))
  405.                         *bridge_agpstat |= AGPSTAT3_4X;
  406.                 else {
  407.             dbgprintf("Badness. Don't know which AGP mode to set. "
  408.                                                         "[bridge_agpstat:%x vga_agpstat:%x fell back to:- bridge_agpstat:%x vga_agpstat:%x]\n",
  409.                                                         origbridge, origvga, *bridge_agpstat, *vga_agpstat);
  410.                         if (!(*bridge_agpstat & AGPSTAT3_4X))
  411.                 dbgprintf("Bridge couldn't do AGP x4.\n");
  412.                         if (!(*vga_agpstat & AGPSTAT3_4X))
  413.                 dbgprintf("Graphic card couldn't do AGP x4.\n");
  414.                         return;
  415.                 }
  416.         }
  417.  
  418. done:
  419.         /* Apply any errata. */
  420.     if (bridge->flags & AGP_ERRATA_FASTWRITES)
  421.                 *bridge_agpstat &= ~AGPSTAT_FW;
  422.  
  423.     if (bridge->flags & AGP_ERRATA_SBA)
  424.                 *bridge_agpstat &= ~AGPSTAT_SBA;
  425.  
  426.     if (bridge->flags & AGP_ERRATA_1X) {
  427.                 *bridge_agpstat &= ~(AGPSTAT2_2X | AGPSTAT2_4X);
  428.                 *bridge_agpstat |= AGPSTAT2_1X;
  429.         }
  430. }
  431.  
  432.  
  433. u32_t agp_collect_device_status(agp_t *bridge, u32_t requested_mode,
  434.                                 u32_t bridge_agpstat)
  435. {
  436.     PCITAG  vgaTag;
  437.     u32_t   vga_agpstat;
  438.     int     cap_ptr;
  439.  
  440.     for (;;)
  441.     {
  442.         vgaTag = pci_find_class(PCI_CLASS_DISPLAY_VGA);
  443.         if (vgaTag == -1)
  444.         {
  445.             dbgprintf("Couldn't find an AGP VGA controller.\n");
  446.                         return 0;
  447.                 }
  448.         cap_ptr = pci_find_capability(vgaTag, PCI_CAP_ID_AGP);
  449.                 if (cap_ptr)
  450.                         break;
  451.         }
  452.  
  453.         /*
  454.          * Ok, here we have a AGP device. Disable impossible
  455.          * settings, and adjust the readqueue to the minimum.
  456.          */
  457.     vga_agpstat = pciReadLong(vgaTag, cap_ptr+PCI_AGP_STATUS);
  458.  
  459.         /* adjust RQ depth */
  460.         bridge_agpstat = ((bridge_agpstat & ~AGPSTAT_RQ_DEPTH) |
  461.          min_t(u32_t, (requested_mode & AGPSTAT_RQ_DEPTH),
  462.          min_t(u32_t, (bridge_agpstat & AGPSTAT_RQ_DEPTH), (vga_agpstat & AGPSTAT_RQ_DEPTH))));
  463.  
  464.         /* disable FW if it's not supported */
  465.         if (!((bridge_agpstat & AGPSTAT_FW) &&
  466.           (vga_agpstat & AGPSTAT_FW) &&
  467.           (requested_mode & AGPSTAT_FW)))
  468.                 bridge_agpstat &= ~AGPSTAT_FW;
  469.  
  470.         /* Check to see if we are operating in 3.0 mode */
  471.     if (bridge->mode & AGPSTAT_MODE_3_0)
  472.                 agp_v3_parse_one(&requested_mode, &bridge_agpstat, &vga_agpstat);
  473.         else
  474.                 agp_v2_parse_one(&requested_mode, &bridge_agpstat, &vga_agpstat);
  475.  
  476.         return bridge_agpstat;
  477. }
  478.  
  479.  
  480. void agp_device_command(u32_t bridge_agpstat, int agp_v3)
  481. {
  482.     PCITAG device = 0;
  483.         int mode;
  484.  
  485.         mode = bridge_agpstat & 0x7;
  486.         if (agp_v3)
  487.                 mode *= 4;
  488.  
  489.     for_each_pci_dev(device)
  490.     {
  491.         int agp = pci_find_capability(device, PCI_CAP_ID_AGP);
  492.                 if (!agp)
  493.                         continue;
  494.  
  495.         dbgprintf("Putting AGP V%d device at into %dx mode\n",
  496.                 agp_v3 ? 3 : 2, mode);
  497.         pciWriteLong(device, agp + PCI_AGP_COMMAND, bridge_agpstat);
  498.         }
  499. }
  500.  
  501.  
  502. struct agp_3_5_dev
  503. {
  504.     link_t link;
  505.     int    capndx;
  506.     u32_t  maxbw;
  507.     PCITAG tag;
  508. };
  509.  
  510.  
  511. /*
  512.  * Fully configure and enable an AGP 3.0 host bridge and all the devices
  513.  * lying behind it.
  514.  */
  515. int agp_3_5_enable(agp_t *bridge)
  516. {
  517.     u8_t   mcapndx;
  518.     u32_t  isoch, arqsz;
  519.     u32_t  tstatus, mstatus, ncapid;
  520.     u32_t  mmajor;
  521.     u16_t  mpstat;
  522.  
  523.     link_t dev_list;
  524.  
  525.     struct agp_3_5_dev *cur, *pos;
  526.  
  527.         unsigned int ndevs = 0;
  528.     PCITAG dev = 0;
  529.         int ret = 0;
  530.  
  531.         /* Extract some power-on defaults from the target */
  532.     tstatus = pciReadLong(bridge->PciTag, bridge->capndx+AGPSTAT);
  533.         isoch     = (tstatus >> 17) & 0x1;
  534.         if (isoch == 0) /* isoch xfers not available, bail out. */
  535.         return -1;
  536.  
  537.         arqsz     = (tstatus >> 13) & 0x7;
  538.  
  539.     list_initialize(&dev_list);
  540.  
  541.         /* Find all AGP devices, and add them to dev_list. */
  542.     for_each_pci_dev(dev)
  543.     {
  544.         u16_t devclass;
  545.  
  546.                 mcapndx = pci_find_capability(dev, PCI_CAP_ID_AGP);
  547.                 if (mcapndx == 0)
  548.                         continue;
  549.  
  550.         devclass = pciReadWord(dev, 0x0A);
  551.  
  552.         switch (devclass & 0xff00)
  553.         {
  554.                         case 0x0600:    /* Bridge */
  555.                                 /* Skip bridges. We should call this function for each one. */
  556.                                 continue;
  557.  
  558.                         case 0x0001:    /* Unclassified device */
  559.                                 /* Don't know what this is, but log it for investigation. */
  560.                                 if (mcapndx != 0) {
  561.                     dbgprintf("Wacky, found unclassified AGP device.\n");
  562.                                 }
  563.                                 continue;
  564.  
  565.                         case 0x0300:    /* Display controller */
  566.                         case 0x0400:    /* Multimedia controller */
  567.                 if((cur = malloc(sizeof(*cur))) == NULL)
  568.                 {
  569.                     ret = -1;
  570.                                         goto free_and_exit;
  571.                                 }
  572.                 cur->tag = dev;
  573.                 list_prepend(&cur->link, &dev_list);
  574.                                 ndevs++;
  575.                                 continue;
  576.  
  577.                         default:
  578.                                 continue;
  579.                 }
  580.         }
  581.  
  582.         /*
  583.          * Take an initial pass through the devices lying behind our host
  584.          * bridge.  Make sure each one is actually an AGP 3.0 device, otherwise
  585.          * exit with an error message.  Along the way store the AGP 3.0
  586.          * cap_ptr for each device
  587.          */
  588.  
  589.     cur = (struct agp_3_5_dev*)dev_list.next;
  590.  
  591.     while(&cur->link != &dev_list)
  592.     {
  593.         dev = cur->tag;
  594.  
  595.         mpstat = pciReadWord(dev, PCI_STATUS);
  596.                 if ((mpstat & PCI_STATUS_CAP_LIST) == 0)
  597.                         continue;
  598.  
  599.         mcapndx = pciReadByte(dev, PCI_CAPABILITY_LIST);
  600.                 if (mcapndx != 0) {
  601.                         do {
  602.                 ncapid = pciReadLong(dev, mcapndx);
  603.                                 if ((ncapid & 0xff) != 2)
  604.                                         mcapndx = (ncapid >> 8) & 0xff;
  605.                         }
  606.                         while (((ncapid & 0xff) != 2) && (mcapndx != 0));
  607.                 }
  608.  
  609.                 if (mcapndx == 0) {
  610.             dbgprintf("woah!  Non-AGP device "
  611.                                 "found on the secondary bus of an AGP 3.5 bridge!\n");
  612.             ret = -1;
  613.                         goto free_and_exit;
  614.                 }
  615.  
  616.                 mmajor = (ncapid >> AGP_MAJOR_VERSION_SHIFT) & 0xf;
  617.                 if (mmajor < 3) {
  618.             dbgprintf("woah!  AGP 2.0 device "
  619.                                 "found on the secondary bus of an AGP 3.5 "
  620.                                 "bridge operating with AGP 3.0 electricals!\n");
  621.             ret = -1;
  622.                         goto free_and_exit;
  623.                 }
  624.  
  625.                 cur->capndx = mcapndx;
  626.  
  627.         mstatus = pciReadLong(dev, cur->capndx+AGPSTAT);
  628.  
  629.                 if (((mstatus >> 3) & 0x1) == 0) {
  630.             dbgprintf("woah!  AGP 3.x device "
  631.                                 "not operating in AGP 3.x mode found on the "
  632.                                 "secondary bus of an AGP 3.5 bridge operating "
  633.                                 "with AGP 3.0 electricals!\n");
  634.             ret = -1;
  635.                         goto free_and_exit;
  636.                 }
  637.         cur = (struct agp_3_5_dev*)cur->link.next;
  638.         }
  639.  
  640.         /*
  641.          * Call functions to divide target resources amongst the AGP 3.0
  642.          * masters.  This process is dramatically different depending on
  643.          * whether isochronous transfers are supported.
  644.          */
  645.         if (isoch) {
  646.         ret = agp_3_5_isochronous_node_enable(bridge, &dev_list, ndevs);
  647.                 if (ret) {
  648.             dbgprintf("Something bad happened setting "
  649.                       "up isochronous xfers.  Falling back to "
  650.                       "non-isochronous xfer mode.\n");
  651.                 } else {
  652.                         goto free_and_exit;
  653.                 }
  654.         }
  655.         agp_3_5_nonisochronous_node_enable(bridge, dev_list, ndevs);
  656.  
  657. free_and_exit:
  658.         /* Be sure to free the dev_list */
  659.     for (pos = (struct agp_3_5_dev*)dev_list.next; &pos->link != &dev_list; )
  660.     {
  661.         cur = pos;
  662.  
  663.         pos = (struct agp_3_5_dev*)pos->link.next;
  664.         free(cur);
  665.         }
  666.  
  667. get_out:
  668.         return ret;
  669. }
  670.  
  671.  
  672. void agp_generic_enable(u32_t requested_mode)
  673. {
  674.     u32_t bridge_agpstat, temp;
  675.  
  676.     get_agp_version(bridge);
  677.  
  678.     dbgprintf("Found an AGP %d.%d compliant device.\n",
  679.            bridge->major_version, bridge->minor_version);
  680.  
  681.     bridge_agpstat = pciReadLong(bridge->PciTag,
  682.                      bridge->capndx + PCI_AGP_STATUS);
  683.  
  684.     bridge_agpstat = agp_collect_device_status(bridge, requested_mode, bridge_agpstat);
  685.         if (bridge_agpstat == 0)
  686.                 /* Something bad happened. FIXME: Return error code? */
  687.                 return;
  688.  
  689.         bridge_agpstat |= AGPSTAT_AGP_ENABLE;
  690.  
  691.         /* Do AGP version specific frobbing. */
  692.     if (bridge->major_version >= 3)
  693.     {
  694.         if (bridge->mode & AGPSTAT_MODE_3_0)
  695.         {
  696.                         /* If we have 3.5, we can do the isoch stuff. */
  697.             if (bridge->minor_version >= 5)
  698.                 agp_3_5_enable(bridge);
  699.             agp_device_command(bridge_agpstat, TRUE);
  700.                         return;
  701.         }
  702.         else
  703.         {
  704.                     /* Disable calibration cycle in RX91<1> when not in AGP3.0 mode of operation.*/
  705.                     bridge_agpstat &= ~(7<<10) ;
  706.             temp = pciReadLong(bridge->PciTag, bridge->capndx+AGPCTRL);
  707.                     temp |= (1<<9);
  708.             pciWriteLong(bridge->PciTag, bridge->capndx+AGPCTRL, temp);
  709.  
  710.             dbgprintf("Device is in legacy mode,"
  711.                       " falling back to 2.x\n");
  712.                 }
  713.         }
  714.  
  715.         /* AGP v<3 */
  716.     agp_device_command(bridge_agpstat, FALSE);
  717. }
  718.  
  719.  
  720. static agp_t intel_845_driver =
  721. {
  722.     .aperture_sizes     = intel_8xx_sizes,
  723. //    .size_type          = U8_APER_SIZE,
  724. //    .num_aperture_sizes = 7,
  725.     .configure          = intel_845_configure,
  726.     .fetch_size         = intel_8xx_fetch_size,
  727. //    .cleanup            = intel_8xx_cleanup,
  728.     .tlb_flush          = intel_8xx_tlbflush,
  729. //    .mask_memory        = agp_generic_mask_memory,
  730. //    .masks              = intel_generic_masks,
  731. //    .agp_enable         = agp_generic_enable,
  732. //    .cache_flush        = global_cache_flush,
  733.     .create_gatt_table  = agp_generic_create_gatt_table,
  734. //    .free_gatt_table    = agp_generic_free_gatt_table,
  735. //    .insert_memory      = agp_generic_insert_memory,
  736. //    .remove_memory      = agp_generic_remove_memory,
  737. //    .alloc_by_type      = agp_generic_alloc_by_type,
  738. //    .free_by_type       = agp_generic_free_by_type,
  739. //    .agp_alloc_page     = agp_generic_alloc_page,
  740. //    .agp_destroy_page   = agp_generic_destroy_page,
  741. };
  742.  
  743. int init_bridge(PCITAG pciTag)
  744. {
  745.     size_t size_value;
  746.  
  747.     bridge = &intel_845_driver;
  748.  
  749.     bridge->PciTag = pciTag;
  750.  
  751.     bridge->capndx = pci_find_capability(pciTag, PCI_CAP_ID_AGP);
  752.  
  753.     size_value = bridge->fetch_size();
  754.  
  755.     if (size_value == 0) {
  756.         dbgprintf("unable to determine aperture size.\n");
  757.         return 0;
  758.     };
  759.  
  760.     dbgprintf("fetch size = %x\n", size_value);
  761.  
  762.     if( bridge->create_gatt_table() )
  763.     {
  764.         bridge->configure();
  765.         return 1;
  766.     }
  767.     return 0;
  768. }
  769.  
  770.  
  771. #include "detect.inc"
  772.