home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / lxapi32.zip / PCI / pci.c < prev    next >
C/C++ Source or Header  |  2002-04-26  |  27KB  |  968 lines

  1. /* $Id: pci.c,v 1.2 2002/04/26 23:09:35 smilcke Exp $ */
  2.  
  3. /*
  4.  * pci.c
  5.  * Autor:               Stefan Milcke
  6.  * Erstellt am:         25.10.2001
  7.  * Letzte Aenderung am: 09.04.2002
  8.  *
  9. */
  10.  
  11. #include <linux/kernel.h>
  12. #include <linux/init.h>
  13. #include <linux/poll.h>
  14. #include <asm/uaccess.h>
  15. #include <asm/hardirq.h>
  16. #include <asm/io.h>
  17. #include <asm/delay.h>
  18. #include <linux/pci.h>
  19. #include <linux/slab.h>
  20. #include <linux/module.h>
  21.  
  22. #ifndef FAR
  23. #define FAR_LDEFOS2
  24. #endif
  25. #include <ldefos2.h>
  26. #ifdef FAR_LDEFOS2
  27. #undef FAR
  28. #undef FAR_LDEFOS2
  29. #endif
  30.  
  31. #define INCL_NOPMAPI
  32. #include <os2.h>
  33. #include <lxrmcall.h>
  34.  
  35. #define LINUX
  36. #include <stacktoflat.h>
  37.  
  38. #define PCI_CONFIG_ADDRESS 0xCF8
  39. #define PCI_CONFIG_DATA    0xCFC
  40. #define PCI_CONFIG_ENABLE  0x80000000
  41.  
  42. LIST_HEAD(pci_root_buses);
  43. LIST_HEAD(pci_devices);
  44.  
  45. LIST_HEAD(pci_drivers);
  46. extern void dev_probe_lock(void);
  47. extern void dev_probe_unlock(void);
  48.  
  49. #ifdef TARGET_OS2
  50. extern int dopcifixups;
  51. extern int dopcienable;
  52. extern int dopcisetmaster;
  53. extern int dopcisetpowerstate;
  54. extern int dopciupdateresource;
  55. MODULE_PARM_LIST_HEAD(pcidrv_parms)
  56. MODULE_PARM(dopcifixups,"i")
  57. MODULE_PARM(dopcienable,"i")
  58. MODULE_PARM(dopcisetmaster,"i")
  59. MODULE_PARM(dopcisetpowerstate,"i")
  60. MODULE_PARM(dopciupdateresource,"i")
  61. MODULE_PARM_LIST_TAIL(pcidrv_parms)
  62. #endif
  63.  
  64. //----------------------------- pci_enable_device ------------------------------
  65. int pci_enable_device(struct pci_dev *dev)
  66. {
  67.  int err;
  68. #ifdef TARGET_OS2
  69.  if(0==dopcienable)
  70.   return 0;
  71. #endif
  72.  pci_set_power_state(dev,0);
  73.  if((err=pcibios_enable_device(dev))<0)
  74.   return err;
  75.  return 0;
  76. }
  77.  
  78. //----------------------------- pci_disable_device -----------------------------
  79. void pci_disable_device(struct pci_dev *dev)
  80. {
  81.  u16 pci_command;
  82.  pci_read_config_word(dev,PCI_COMMAND,&pci_command);
  83.  if(pci_command & PCI_COMMAND_MASTER)
  84.  {
  85.   pci_command&=~PCI_COMMAND_MASTER;
  86. #ifdef TARGET_OS2
  87.   if(0!=dopcienable)
  88. #endif
  89.   pci_write_config_word(dev,PCI_COMMAND,pci_command);
  90.  }
  91. }
  92.  
  93. //------------------------------ pci_match_device ------------------------------
  94. const struct pci_device_id *pci_match_device(const struct pci_device_id *ids
  95.                                              ,const struct pci_dev *dev)
  96. {
  97.  while(ids->vendor||ids->subvendor||ids->class_mask)
  98.  {
  99.   if((ids->vendor==PCI_ANY_ID || ids->vendor==dev->vendor) &&
  100.      (ids->device==PCI_ANY_ID || ids->device==dev->device) &&
  101.      (ids->subvendor==PCI_ANY_ID || ids->subvendor==dev->subsystem_vendor) &&
  102.      (ids->subdevice==PCI_ANY_ID || ids->subdevice==dev->subsystem_device) &&
  103.      !((ids->pciclass ^ dev->pciclass) & ids->class_mask))
  104.    return ids;
  105.   ids++;
  106.  }
  107.  return NULL;
  108. }
  109.  
  110. #ifdef TARGET_OS2
  111. unsigned long OS2_pci_announce_device(struct pci_driver *drv,struct pci_dev *dev);
  112. #endif
  113. //---------------------------- pci_announce_device -----------------------------
  114. static int pci_announce_device(struct pci_driver *drv,struct pci_dev *dev)
  115. {
  116.  const struct pci_device_id *id;
  117.  int ret=0;
  118.  if(drv->id_table)
  119.  {
  120.   id=pci_match_device(drv->id_table,dev);
  121.   if(!id)
  122.   {
  123.    ret=0;
  124.    goto out;
  125.   }
  126.  }
  127.  else
  128.   id=NULL;
  129.  dev_probe_lock();
  130.  if(drv->probe(dev,id)>=0)
  131.  {
  132.   dev->driver=drv;
  133. #ifdef TARGET_OS2
  134.   OS2_pci_announce_device(drv,dev);
  135. #endif
  136.   ret=1;
  137.  }
  138.  dev_probe_unlock();
  139. out:
  140.  return ret;
  141. }
  142.  
  143. #ifdef TARGET_OS2
  144. unsigned long OS2_pci_register_driver(struct pci_driver *drv);
  145. unsigned long OS2_pci_unregister_driver(struct pci_driver *drv);
  146. #endif
  147. //---------------------------- pci_register_driver -----------------------------
  148. int pci_register_driver(struct pci_driver *drv)
  149. {
  150.  struct pci_dev *dev;
  151.  int count=0;
  152.  list_add_tail(&drv->node,&pci_drivers);
  153. #ifdef TARGET_OS2
  154.  OS2_pci_register_driver(drv);
  155. #endif
  156.  pci_for_each_dev(dev)
  157.  {
  158.   if(!pci_dev_driver(dev))
  159.    count+=pci_announce_device(drv,dev);
  160.  }
  161.  return count;
  162. }
  163.  
  164. //--------------------------- pci_unregister_driver ----------------------------
  165. void pci_unregister_driver(struct pci_driver *drv)
  166. {
  167.  struct pci_dev *dev;
  168.  list_del(&drv->node);
  169.  pci_for_each_dev(dev)
  170.  {
  171.   if(dev->driver==drv)
  172.   {
  173.    if(drv->remove)
  174.     drv->remove(dev);
  175.    dev->driver=NULL;
  176.   }
  177.  }
  178. #ifdef TARGET_OS2
  179.  OS2_pci_unregister_driver(drv);
  180. #endif
  181. }
  182.  
  183. //----------------------- pci_announce_device_to_drivers -----------------------
  184. void pci_announce_device_to_drivers(struct pci_dev *dev)
  185. {
  186.  struct list_head *ln;
  187.  for(ln=pci_drivers.next;ln!=&pci_drivers;ln=ln->next)
  188.  {
  189.   struct pci_driver *drv=list_entry(ln,struct pci_driver,node);
  190.   if(drv->remove && pci_announce_device(drv,dev))
  191.    break;
  192.  }
  193.  // run_sbin_hotplug(dev,TRUE);
  194. }
  195.  
  196. //----------------------------- pci_insert_device ------------------------------
  197. void pci_insert_device(struct pci_dev *dev,struct pci_bus *bus)
  198. {
  199.  list_add_tail(&dev->bus_list,&bus->devices);
  200.  list_add_tail(&dev->global_list,&pci_devices);
  201.  pci_announce_device_to_drivers(dev);
  202. }
  203.  
  204. //----------------------------- pci_free_resources -----------------------------
  205. static void pci_free_resources(struct pci_dev *dev)
  206. {
  207.  int i;
  208.  for(i=0;i<PCI_NUM_RESOURCES;i++)
  209.  {
  210.   struct resource *res=dev->resource+i;
  211.   if(res->parent)
  212.    release_resource(res);
  213.  }
  214. }
  215.  
  216. //----------------------------- pci_remove_device ------------------------------
  217. void pci_remove_device(struct pci_dev *dev)
  218. {
  219.  if(dev->driver)
  220.  {
  221.   if(dev->driver->remove)
  222.    dev->driver->remove(dev);
  223.   dev->driver=NULL;
  224.  }
  225.  list_del(&dev->bus_list);
  226.  list_del(&dev->global_list);
  227.  pci_free_resources(dev);
  228.  // run_sbin_hotplug(dev,FALSE);
  229. }
  230.  
  231. static struct pci_driver pci_compat_driver={0};
  232.  
  233. //------------------------------- pci_dev_driver -------------------------------
  234. struct pci_driver *pci_dev_driver(const struct pci_dev *dev)
  235. {
  236.  if(dev->driver)
  237.   return dev->driver;
  238.  else
  239.  {
  240.   int i;
  241.   for(i=0;i<=PCI_ROM_RESOURCE;i++)
  242.    if(dev->resource[i].flags & IORESOURCE_BUSY)
  243.    {
  244.     pci_compat_driver.name="compat";
  245.     return &pci_compat_driver;
  246.    }
  247.  }
  248.  return NULL;
  249. }
  250.  
  251. //------------------------------- pci_find_slot --------------------------------
  252. struct pci_dev *pci_find_slot(unsigned int bus,unsigned int devfn)
  253. {
  254.  struct pci_dev *dev;
  255.  pci_for_each_dev(dev)
  256.  {
  257.   if(dev->bus->number==bus && dev->devfn==devfn)
  258.    return dev;
  259.  }
  260.  return NULL;
  261. }
  262.  
  263. //------------------------------ pci_find_subsys -------------------------------
  264. struct pci_dev *pci_find_subsys(unsigned int vendor,unsigned int device
  265.                                 ,unsigned int ss_vendor,unsigned int ss_device
  266.                                 ,const struct pci_dev *from)
  267. {
  268.  struct list_head *n=from ? from->global_list.next : pci_devices.next;
  269.  while(n!=&pci_devices)
  270.  {
  271.   struct pci_dev *dev=pci_dev_g(n);
  272.   if((vendor==PCI_ANY_ID || dev->vendor==vendor) &&
  273.      (device==PCI_ANY_ID || dev->device==device) &&
  274.      (ss_vendor==PCI_ANY_ID || dev->subsystem_vendor==ss_vendor) &&
  275.      (ss_device==PCI_ANY_ID || dev->subsystem_device==ss_device))
  276.    return dev;
  277.   n=n->next;
  278.  }
  279.  return NULL;
  280. }
  281.  
  282. //------------------------------ pci_find_device -------------------------------
  283. struct pci_dev *pci_find_device(unsigned int vendor,unsigned int device
  284.                                 ,const struct pci_dev *from)
  285. {
  286.  return pci_find_subsys(vendor,device,PCI_ANY_ID,PCI_ANY_ID,from);
  287. }
  288.  
  289. //------------------------------- pci_find_class -------------------------------
  290. struct pci_dev *pci_find_class(unsigned int pciclass, const struct pci_dev *from)
  291. {
  292.  struct list_head *n = from ? from->global_list.next : pci_devices.next;
  293.  while (n != &pci_devices)
  294.  {
  295.   struct pci_dev *dev = pci_dev_g(n);
  296.   if (dev->pciclass == pciclass)
  297.    return dev;
  298.   n = n->next;
  299.  }
  300.  return NULL;
  301. }
  302.  
  303. //---------------------------- pci_find_capability -----------------------------
  304. int pci_find_capability(struct pci_dev *dev, int cap)
  305. {
  306.  u16 status;
  307.  u8 pos, id;
  308.  int ttl = 48;
  309.  pci_read_config_word(dev, PCI_STATUS, &status);
  310.  if (!(status & PCI_STATUS_CAP_LIST))
  311.   return 0;
  312.  switch (dev->hdr_type)
  313.  {
  314.   case PCI_HEADER_TYPE_NORMAL:
  315.   case PCI_HEADER_TYPE_BRIDGE:
  316.    pci_read_config_byte(dev, PCI_CAPABILITY_LIST, &pos);
  317.    break;
  318.   case PCI_HEADER_TYPE_CARDBUS:
  319.    pci_read_config_byte(dev, PCI_CB_CAPABILITY_LIST, &pos);
  320.    break;
  321.   default:
  322.    return 0;
  323.   }
  324.  while (ttl-- && pos >= 0x40)
  325.  {
  326.   pos &= ~3;
  327.   pci_read_config_byte(dev, pos + PCI_CAP_LIST_ID, &id);
  328.   if (id == 0xff)
  329.    break;
  330.   if (id == cap)
  331.    return pos;
  332.   pci_read_config_byte(dev, pos + PCI_CAP_LIST_NEXT, &pos);
  333.  }
  334.  return 0;
  335. }
  336.  
  337. //-------------------------- pci_find_parent_resource --------------------------
  338. struct resource *pci_find_parent_resource(const struct pci_dev *dev
  339.                                           ,struct resource *res)
  340. {
  341.  const struct pci_bus *bus=dev->bus;
  342.  int i;
  343.  struct resource *best=NULL;
  344.  for(i=0;i<4;i++)
  345.  {
  346.   struct resource *r=bus->resource[i];
  347.   if(!r)
  348.    continue;
  349.   if(res->start && !(res->start>=r->start && res->end<=r->end))
  350.    continue;
  351.   if((res->flags^r->flags)&(IORESOURCE_IO | IORESOURCE_MEM))
  352.    continue;
  353.   if(!((res->flags^r->flags)&IORESOURCE_PREFETCH))
  354.    return r;
  355.   if((res->flags&IORESOURCE_PREFETCH)&&!(r->flags&IORESOURCE_PREFETCH))
  356.    best=r;
  357.  }
  358.  return best;
  359. }
  360.  
  361. //---------------------------- pci_set_power_state -----------------------------
  362. int pci_set_power_state(struct pci_dev *dev,int state)
  363. {
  364.  int pm;
  365.  u16 pmcsr;
  366. #ifdef TARGET_OS2
  367.  if(0==dopcisetpowerstate)
  368.   return 0;
  369. #endif
  370.  if(state>3)
  371.   state=3;
  372.  if(state>0 && dev->current_state>state)
  373.   return -EINVAL;
  374.  else if(dev->current_state==state)
  375.   return 0;
  376.  pm=pci_find_capability(dev,PCI_CAP_ID_PM);
  377.  if(!pm)
  378.   return -EIO;
  379.  if(state==1 || state==2)
  380.  {
  381.   u16 pmc;
  382.   pci_read_config_word(dev,pm+PCI_PM_PMC,&pmc);
  383.   if(state==1 && !(pmc&PCI_PM_CAP_D1))
  384.    return -EIO;
  385.   else if(state==2 && !(pmc&PCI_PM_CAP_D2))
  386.    return -EIO;
  387.  }
  388.  if(dev->current_state>=3)
  389.   pmcsr=0;
  390.  else
  391.  {
  392.   pci_read_config_word(dev,pm+PCI_PM_CTRL,&pmcsr);
  393.   pmcsr&=~PCI_PM_CTRL_STATE_MASK;
  394.   pmcsr|=state;
  395.  }
  396.  pci_write_config_word(dev,pm+PCI_PM_CTRL,pmcsr);
  397.  if(state==3 || dev->current_state==3)
  398.  {
  399.   // Incomplete (SM)
  400. //  set_current_state(TASK_UNINTERRUPTIBLE);
  401.   schedule_timeout(HZ/100);
  402.  }
  403.  else if(state==2 || dev->current_state==2)
  404.   udelay(200);
  405.  dev->current_state=state;
  406.  return 0;
  407. }
  408.  
  409. #define CONFIG_CMD(dev, where)   (0x80000000 | (dev->bus->number << 16) | (dev->devfn << 8) | (where & ~3))
  410. //---------------------------- pci_read_config_byte ----------------------------
  411. int pci_read_config_byte(struct pci_dev *dev, int where, u8 *value)
  412. {
  413.     outl(CONFIG_CMD(dev,where), 0xCF8);
  414.     *value = inb(0xCFC + (where&3));
  415.     return PCIBIOS_SUCCESSFUL;
  416. }
  417.  
  418. //---------------------------- pci_read_config_word ----------------------------
  419. int pci_read_config_word(struct pci_dev *dev, int where, u16 *value)
  420. {
  421.     outl(CONFIG_CMD(dev,where), 0xCF8);
  422.     *value = inw(0xCFC + (where&2));
  423.     return PCIBIOS_SUCCESSFUL;
  424. }
  425.  
  426. //--------------------------- pci_read_config_dword ----------------------------
  427. int pci_read_config_dword(struct pci_dev *dev, int where, u32 *value)
  428. {
  429.     outl(CONFIG_CMD(dev,where), 0xCF8);
  430.     *value = inl(0xCFC);
  431.     return PCIBIOS_SUCCESSFUL;
  432. }
  433.  
  434. //--------------------------- pci_write_config_byte ----------------------------
  435. int pci_write_config_byte(struct pci_dev *dev, int where, u8 value)
  436. {
  437.     outl(CONFIG_CMD(dev,where), 0xCF8);
  438.     outb(value, 0xCFC + (where&3));
  439.     return PCIBIOS_SUCCESSFUL;
  440. }
  441.  
  442. //--------------------------- pci_write_config_word ----------------------------
  443. int pci_write_config_word(struct pci_dev *dev, int where, u16 value)
  444. {
  445.     outl(CONFIG_CMD(dev,where), 0xCF8);
  446.     outw(value, 0xCFC + (where&2));
  447.     return PCIBIOS_SUCCESSFUL;
  448. }
  449.  
  450. //--------------------------- pci_write_config_dword ---------------------------
  451. int pci_write_config_dword(struct pci_dev *dev, int where, u32 value)
  452. {
  453.     outl(CONFIG_CMD(dev,where), 0xCF8);
  454.     outl(value, 0xCFC);
  455.     return PCIBIOS_SUCCESSFUL;
  456. }
  457.  
  458. //-------------------------- pci_calc_resource_flags ---------------------------
  459. static __inline__ unsigned int pci_calc_resource_flags(unsigned int flags)
  460. {
  461.  if(flags&PCI_BASE_ADDRESS_SPACE_IO)
  462.   return IORESOURCE_IO;
  463.  if(flags&PCI_BASE_ADDRESS_MEM_PREFETCH)
  464.   return IORESOURCE_MEM | IORESOURCE_PREFETCH;
  465.  return IORESOURCE_MEM;
  466. }
  467.  
  468. //---------------------------------- pci_size ----------------------------------
  469. static u32 pci_size(u32 base,unsigned long mask)
  470. {
  471.  u32 size=mask&base; // find the significant bits
  472.  size=size&~(size-1); // get the lowest of them to find the decode size
  473.  return size-1; // extend=size-1
  474. }
  475.  
  476. //------------------------------- pci_set_master -------------------------------
  477. void pci_set_master(struct pci_dev *dev)
  478. {
  479.  u16 cmd;
  480.  pci_read_config_word(dev,PCI_COMMAND,&cmd);
  481.  if(!(cmd&PCI_COMMAND_MASTER))
  482.  {
  483. //  DBG("PCI: Enabling bus mastering for device %s\n",dev->slot_name);
  484.   cmd|=PCI_COMMAND_MASTER;
  485. #ifdef TARGET_OS2
  486.   if(0!=dopcisetmaster)
  487. #endif
  488.   pci_write_config_word(dev,PCI_COMMAND,cmd);
  489.  }
  490.  pcibios_set_master(dev);
  491. }
  492.  
  493. //------------------------------- pci_read_bases -------------------------------
  494. static void pci_read_bases(struct pci_dev *dev,unsigned int howmany,int rom)
  495. {
  496.  unsigned int pos,reg,next;
  497.  u32 l,sz;
  498.  struct resource *res;
  499.  for(pos=0;pos<howmany;pos=next)
  500.  {
  501.   next=pos+1;
  502.   res=&dev->resource[pos];
  503.   res->name=dev->name;
  504.   reg=PCI_BASE_ADDRESS_0 + (pos << 2);
  505.   pci_read_config_dword(dev,reg,&l);
  506.   pci_write_config_dword(dev,reg,~0);
  507.   pci_read_config_dword(dev,reg,&sz);
  508.   pci_write_config_dword(dev,reg,l);
  509.   if(!sz || sz==0xffffffff)
  510.    continue;
  511.   if(l==0xffffffff)
  512.    l=0;
  513.   if((l&PCI_BASE_ADDRESS_SPACE)==PCI_BASE_ADDRESS_SPACE_MEMORY)
  514.   {
  515.    res->start=l&PCI_BASE_ADDRESS_MEM_MASK;
  516.    sz=pci_size(sz,PCI_BASE_ADDRESS_MEM_MASK);
  517.   }
  518.   else
  519.   {
  520.    res->start=l&PCI_BASE_ADDRESS_IO_MASK;
  521.    sz=pci_size(sz,PCI_BASE_ADDRESS_IO_MASK & 0xffff);
  522.   }
  523.   res->end=res->start+(unsigned long)sz;
  524.   res->flags|=(l&0xf) | pci_calc_resource_flags(l);
  525.   if((l&(PCI_BASE_ADDRESS_SPACE | PCI_BASE_ADDRESS_MEM_TYPE_MASK))
  526.      ==(PCI_BASE_ADDRESS_SPACE_MEMORY | PCI_BASE_ADDRESS_MEM_TYPE_64))
  527.   {
  528.    pci_read_config_dword(dev,reg+4,&l);
  529.    next++;
  530. #if BITS_PER_LONG==64
  531.    res->start|=((unsigned long)l)<<32;
  532.    res->end=res->start+sz;
  533.    pci_write_config_dword(dev,reg+4,~0);
  534.    pci_read_config_dword(dev,reg+4,&sz);
  535.    pci_write_config_dword(dev,reg+4,l);
  536.    if(~sz)
  537.     res->end=res->start+0xffffffff+(((unsigned long)~sz)<<32);
  538. #else
  539.    if(l)
  540.    {
  541.     res->start=0;
  542.     res->flags=0;
  543.     continue;
  544.    }
  545. #endif
  546.   }
  547.  }
  548.  if(rom)
  549.  {
  550.   dev->rom_base_reg=rom;
  551.   res=&dev->resource[PCI_ROM_RESOURCE];
  552.   pci_read_config_dword(dev,rom,&l);
  553.   pci_write_config_dword(dev,rom,~PCI_ROM_ADDRESS_ENABLE);
  554.   pci_read_config_dword(dev,rom,&sz);
  555.   pci_write_config_dword(dev,rom,l);
  556.   if(l==0xffffffff)
  557.    l=0;
  558.   if(sz && sz!=0xffffffff)
  559.   {
  560.    res->flags=(l&PCI_ROM_ADDRESS_ENABLE)
  561.                | IORESOURCE_MEM | IORESOURCE_PREFETCH | IORESOURCE_READONLY | IORESOURCE_CACHEABLE;
  562.    res->start=l&PCI_ROM_ADDRESS_MASK;
  563.    sz=pci_size(sz,PCI_ROM_ADDRESS_MASK);
  564.    res->end=res->start+(unsigned long)sz;
  565.   }
  566.   res->name=dev->name;
  567.  }
  568. }
  569.  
  570. //--------------------------- pci_read_bridge_bases ----------------------------
  571. void __init pci_read_bridge_bases(struct pci_bus *child)
  572. {
  573.  struct pci_dev *dev=child->self;
  574.  u8 io_base_lo,io_limit_lo;
  575.  u16 mem_base_lo,mem_limit_lo;
  576.  unsigned long base,limit;
  577.  struct resource *res;
  578.  int i;
  579.  if(!dev)
  580.   return;
  581.  for(i=0;i<3;i++)
  582.   child->resource[i]=&dev->resource[PCI_BRIDGE_RESOURCES+i];
  583.  res=child->resource[0];
  584.  pci_read_config_byte(dev,PCI_IO_BASE,&io_base_lo);
  585.  pci_read_config_byte(dev,PCI_IO_LIMIT,&io_limit_lo);
  586.  base=(io_base_lo & PCI_IO_RANGE_MASK)<<8;
  587.  limit=(io_limit_lo & PCI_IO_RANGE_MASK)<<8;
  588.  if((base&PCI_IO_RANGE_TYPE_MASK)==PCI_IO_RANGE_TYPE_32)
  589.  {
  590.   u16 io_base_hi,io_limit_hi;
  591.   pci_read_config_word(dev,PCI_IO_BASE_UPPER16,&io_base_hi);
  592.   pci_read_config_word(dev,PCI_IO_LIMIT_UPPER16,&io_limit_hi);
  593.   base|=(io_base_hi<<16);
  594.   limit|=(io_limit_hi<<16);
  595.  }
  596.  if(base && base <= limit)
  597.  {
  598.   res->flags=(io_base_lo & PCI_IO_RANGE_TYPE_MASK) | IORESOURCE_IO;
  599.   res->start=base;
  600.   res->end=limit+0xfff;
  601.   res->name=child->name; // ??? (SM)
  602.  }
  603.  else
  604.  {
  605.   // Ugh. We don't know enough about this bridge. Just assume
  606.   // that it's entirely transparent
  607.   CPK(printk(KERN_ERR "Unknown bridge resource %d: assuming transparent\n",0));
  608.   child->resource[0]=child->parent->resource[0];
  609.  }
  610.  res=child->resource[1];
  611.  pci_read_config_word(dev,PCI_MEMORY_BASE,&mem_base_lo);
  612.  pci_read_config_word(dev,PCI_MEMORY_LIMIT,&mem_limit_lo);
  613.  base=(mem_base_lo & PCI_MEMORY_RANGE_MASK)<<16;
  614.  limit=(mem_limit_lo & PCI_MEMORY_RANGE_MASK)<<16;
  615.  if(base && base <= limit)
  616.  {
  617.   res->flags=(mem_base_lo & PCI_MEMORY_RANGE_TYPE_MASK) | IORESOURCE_MEM;
  618.   res->start=base;
  619.   res->end=limit+0xfffff;
  620.   res->name=child->name; // ??? (SM)
  621.  }
  622.  else
  623.  {
  624.   // Ugh. We don't know enough about this bridge. Just assume
  625.   // that it's entirely transparent
  626.   CPK(printk(KERN_ERR "Unknown bridge resource %d: assuming transparent\n",1));
  627.   child->resource[1]=child->parent->resource[1];
  628.  }
  629.  res=child->resource[2];
  630.  pci_read_config_word(dev,PCI_PREF_MEMORY_BASE,&mem_base_lo);
  631.  pci_read_config_word(dev,PCI_PREF_MEMORY_LIMIT,&mem_limit_lo);
  632.  base=(mem_base_lo & PCI_PREF_RANGE_MASK)<<16;
  633.  limit=(mem_limit_lo & PCI_PREF_RANGE_MASK)<<16;
  634.  if((mem_base_lo & PCI_PREF_RANGE_TYPE_MASK)==PCI_PREF_RANGE_TYPE_64)
  635.  {
  636.   u32 mem_base_hi,mem_limit_hi;
  637.   pci_read_config_dword(dev,PCI_PREF_BASE_UPPER32,&mem_base_hi);
  638.   pci_read_config_dword(dev,PCI_PREF_LIMIT_UPPER32,&mem_limit_hi);
  639. #if BITS_PER_LONG==64
  640.   base |=((long)mem_base_hi)<<32;
  641.   limit |=((long)mem_limit_hi)<<32;
  642. #else
  643.   if(mem_base_hi || mem_limit_hi)
  644.   {
  645.    CPK(printk(KERN_ERR "PCI: Unable to handle 64-bit address space for %s\n",child->name));
  646.    return;
  647.   }
  648. #endif
  649.  }
  650.  if(base & base <= limit)
  651.  {
  652.   res->flags=(mem_base_lo & PCI_MEMORY_RANGE_TYPE_MASK) | IORESOURCE_MEM | IORESOURCE_PREFETCH;
  653.   res->start=base;
  654.   res->end=limit+0xfffff;
  655.   res->name=child->name; // ??? (SM)
  656.  }
  657.  else
  658.  {
  659.   // Ugh. We don't know enough about this bridge. Just assume
  660.   // that it's entirely transparent
  661.   CPK(printk(KERN_ERR "Unknown bridge resource %d: assuming transparent\n",2));
  662.   child->resource[2]=child->parent->resource[2];
  663.  }
  664. }
  665.  
  666. //------------------------------- pci_alloc_bus --------------------------------
  667. static struct pci_bus *pci_alloc_bus(void)
  668. {
  669.  struct pci_bus *b;
  670.  b=kmalloc(sizeof(*b),GFP_KERNEL);
  671.  if(b)
  672.  {
  673.   memset(b,0,sizeof(*b));
  674.   INIT_LIST_HEAD(&b->children);
  675.   INIT_LIST_HEAD(&b->devices);
  676.  }
  677.  return b;
  678. }
  679.  
  680. //------------------------------ pci_add_new_bus -------------------------------
  681. static struct pci_bus * __init pci_add_new_bus(struct pci_bus *parent
  682.                                                ,struct pci_dev *dev
  683.                                                ,int busnr)
  684. {
  685.  struct pci_bus *child;
  686.  int i;
  687.  child=pci_alloc_bus();
  688.  list_add_tail(&child->node,&parent->children);
  689.  child->self=dev;
  690.  dev->subordinate=child;
  691.  child->parent=parent;
  692.  child->ops=parent->ops;
  693.  child->sysdata=parent->sysdata;
  694.  child->number=child->secondary=busnr;
  695.  child->primary=parent->secondary;
  696.  child->subordinate=0xff;
  697.  for(i=0;i<4;i++)
  698.   child->resource[i]=&dev->resource[PCI_BRIDGE_RESOURCES+i];
  699.  return child;
  700. }
  701.  
  702. static unsigned int __init pci_do_scan_bus(struct pci_bus *bus);
  703.  
  704. //------------------------------ pci_scan_bridge -------------------------------
  705. static int __init pci_scan_bridge(struct pci_bus *bus,struct pci_dev *dev
  706.                                   ,int maxb,int pass)
  707. {
  708.  unsigned int buses;
  709.  unsigned short cr;
  710.  struct pci_bus *child;
  711.  int is_cardbus=(dev->hdr_type==PCI_HEADER_TYPE_CARDBUS);
  712.  pci_read_config_dword(dev,PCI_PRIMARY_BUS,&buses);
  713.  if((buses&0xffff00) && !pcibios_assign_all_busses())
  714.  {
  715.   if(pass)
  716.    return maxb;
  717.   child=(struct pci_bus *)pci_add_new_bus(bus,dev,0);
  718.   child->primary=buses&0xFF;
  719.   child->secondary=(buses>>8)&0xFF;
  720.   child->subordinate=(buses>>16)&0xFF;
  721.   child->number=child->secondary;
  722.   if(!is_cardbus)
  723.   {
  724.    unsigned int cmax=pci_do_scan_bus(child);
  725.    if(cmax>maxb)
  726.     maxb=cmax;
  727.   }
  728.   else
  729.   {
  730.    unsigned int cmax=child->subordinate;
  731.    if(cmax>maxb)
  732.     maxb=cmax;
  733.   }
  734.  }
  735.  else
  736.  {
  737.   if(!pass)
  738.    return maxb;
  739.   pci_read_config_word(dev,PCI_COMMAND,&cr);
  740.   pci_write_config_word(dev,PCI_COMMAND,0x0000);
  741.   pci_write_config_word(dev,PCI_COMMAND,0xFFFF);
  742.   child=(struct pci_bus *)pci_add_new_bus(bus,dev,++maxb);
  743.   buses=(buses&0xff000000)
  744.         |((unsigned int)(child->primary)<<0)
  745.         |((unsigned int)(child->secondary)<<8)
  746.         |((unsigned int)(child->subordinate)<<16);
  747.   pci_write_config_dword(dev,PCI_PRIMARY_BUS,buses);
  748.   if(!is_cardbus)
  749.    maxb=pci_do_scan_bus(child);
  750.   else
  751.    maxb+=3;
  752.   child->subordinate=maxb;
  753.   pci_write_config_byte(dev,PCI_SUBORDINATE_BUS,maxb);
  754.   pci_write_config_word(dev,PCI_COMMAND,cr);
  755.  }
  756.  sprintf(child->name,(is_cardbus ? "PCI CardBus #%02x" : "PCI Bus #%02x"),child->number);
  757.  return maxb;
  758. }
  759.  
  760. //-------------------------------- pci_read_irq --------------------------------
  761. static void pci_read_irq(struct pci_dev *dev)
  762. {
  763.  unsigned char irq;
  764.  pci_read_config_byte(dev,PCI_INTERRUPT_PIN,&irq);
  765.  if(irq)
  766.   pci_read_config_byte(dev,PCI_INTERRUPT_LINE,&irq);
  767.  dev->irq=irq;
  768. }
  769.  
  770. //------------------------------ pci_setup_device ------------------------------
  771. int pci_setup_device(struct pci_dev *dev)
  772. {
  773.  u32 dclass;
  774.  sprintf(dev->slot_name,"%02x:%02x.%d"
  775.          ,dev->bus->number
  776.          ,PCI_SLOT(dev->devfn)
  777.          ,PCI_FUNC(dev->devfn));
  778.  sprintf(dev->name,"PCI device %04x:%04x"
  779.          ,dev->vendor
  780.          ,dev->device);
  781.  pci_read_config_dword(dev,PCI_CLASS_REVISION,&dclass);
  782.  dclass >>=8;
  783.  dev->pciclass=dclass;
  784.  dclass >>=8;
  785.  dev->current_state=4;
  786.  switch(dev->hdr_type)
  787.  {
  788.   case PCI_HEADER_TYPE_NORMAL:
  789.    if(dclass==PCI_CLASS_BRIDGE_PCI)
  790.     goto bad;
  791.    pci_read_irq(dev);
  792.    pci_read_bases(dev,6,PCI_ROM_ADDRESS);
  793.    pci_read_config_word(dev,PCI_SUBSYSTEM_VENDOR_ID,&dev->subsystem_vendor);
  794.    pci_read_config_word(dev,PCI_SUBSYSTEM_ID,&dev->subsystem_device);
  795.    break;
  796.   case PCI_HEADER_TYPE_BRIDGE:
  797.    if(dclass!=PCI_CLASS_BRIDGE_PCI)
  798.     goto bad;
  799.    pci_read_bases(dev,2,PCI_ROM_ADDRESS1);
  800.    break;
  801.   case PCI_HEADER_TYPE_CARDBUS:
  802.    if(dclass!=PCI_CLASS_BRIDGE_CARDBUS)
  803.     goto bad;
  804.    pci_read_irq(dev);
  805.    pci_read_bases(dev,1,0);
  806.    pci_read_config_word(dev,PCI_SUBSYSTEM_VENDOR_ID,&dev->subsystem_vendor);
  807.    pci_read_config_word(dev,PCI_SUBSYSTEM_ID,&dev->subsystem_device);
  808.    break;
  809.   default:
  810.    return -1;
  811.   bad:
  812.    dev->pciclass=PCI_CLASS_NOT_DEFINED;
  813.  }
  814.  return 0;
  815. }
  816.  
  817. //------------------------------ pci_scan_device -------------------------------
  818. static struct pci_dev *pci_scan_device(struct pci_dev *temp)
  819. {
  820.  struct pci_dev *dev;
  821.  u32 l;
  822.  if(pci_read_config_dword(temp,PCI_VENDOR_ID,&l))
  823.   return NULL;
  824.  if(l == 0xffffffff || l == 0x00000000 || l == 0x0000ffff || l == 0xffff0000)
  825.   return NULL;
  826.  dev=kmalloc(sizeof(*dev),GFP_KERNEL);
  827.  if(!dev)
  828.   return NULL;
  829.  memcpy(dev,temp,sizeof(*dev));
  830.  dev->vendor=l&0xffff;
  831.  dev->device=(l>>16)&0xffff;
  832.  // Assume 32-bit PCI; let 64-bit PCI cards (which are far rarer)
  833.  // set this higher, assuming the system even supports it;
  834.  dev->dma_mask=0xffffffff;
  835.  if(pci_setup_device(dev)<0)
  836.  {
  837.   kfree(dev);
  838.   dev=NULL;
  839.  }
  840. #ifdef TARGET_OS2
  841.  dev->rm_subadapter=kmalloc(sizeof(struct lxrm_subadapter),0);
  842.  memset(dev->rm_subadapter,0,sizeof(struct lxrm_subadapter));
  843.  dev->rm_subdevice=kmalloc(sizeof(struct lxrm_subdevice),0);
  844.  memset(dev->rm_subdevice,0,sizeof(struct lxrm_subdevice));
  845. #endif
  846.  return dev;
  847. }
  848.  
  849. //------------------------------- pci_scan_slot --------------------------------
  850. struct pci_dev *pci_scan_slot(struct pci_dev *temp)
  851. {
  852.  struct pci_bus *bus=temp->bus;
  853.  struct pci_dev *dev;
  854.  struct pci_dev *first_dev=NULL;
  855.  int func=0;
  856.  int is_multi=0;
  857.  u8 hdr_type;
  858.  for(func=0;func<8;func++,temp->devfn++)
  859.  {
  860.   if(func&&!is_multi)
  861.    continue;
  862.   if(pci_read_config_byte(temp,PCI_HEADER_TYPE,&hdr_type))
  863.    continue;
  864.   temp->hdr_type=hdr_type&0x7f;
  865.   dev=pci_scan_device(temp);
  866.   if(!dev)
  867.    continue;
  868.   pci_name_device(dev);
  869.   if(!func)
  870.   {
  871.    is_multi=hdr_type&0x80;
  872.    first_dev=dev;
  873.   }
  874.   list_add_tail(&dev->global_list,&pci_devices);
  875.   list_add_tail(&dev->bus_list,&bus->devices);
  876.   pci_fixup_device(PCI_FIXUP_HEADER,dev);
  877.  }
  878.  return first_dev;
  879. }
  880.  
  881. //------------------------------ pci_do_scan_bus -------------------------------
  882. static unsigned int __init pci_do_scan_bus(struct pci_bus *bus)
  883. {
  884.  unsigned int devfn,maxb,pass;
  885.  struct list_head *ln;
  886.  struct pci_dev *dev,dev0;
  887.  maxb=bus->secondary;
  888.  memset(&dev0,0,sizeof(dev0));
  889.  dev0.bus=bus;
  890.  dev0.sysdata=bus->sysdata;
  891.  for(devfn=0;devfn<0x100;devfn+=8)
  892.  {
  893.   dev0.devfn=devfn;
  894.   pci_scan_slot(&dev0);
  895.  }
  896.  pcibios_fixup_bus(bus);
  897.  for(pass=0;pass<2;pass++)
  898.   for(ln=bus->devices.next;ln!=&bus->devices;ln=ln->next)
  899.   {
  900.    dev=pci_dev_b(ln);
  901.    if(dev->hdr_type==PCI_HEADER_TYPE_BRIDGE || dev->hdr_type==PCI_HEADER_TYPE_CARDBUS)
  902.     maxb=pci_scan_bridge(bus,dev,maxb,pass);
  903.   }
  904.  // (SM)
  905.  return maxb;
  906. }
  907.  
  908. //------------------------------- pci_bus_exists -------------------------------
  909. int pci_bus_exists(const struct list_head *list,int nr)
  910. {
  911.  const struct list_head *l;
  912.  for(l=list->next;l!=list;l=l->next)
  913.  {
  914.   const struct pci_bus *b=pci_bus_b(l);
  915.   if(b->number==nr || pci_bus_exists(&b->children,nr))
  916.    return 1;
  917.  }
  918.  return 0;
  919. }
  920.  
  921. //--------------------------- pci_alloc_primary_bus ----------------------------
  922. struct pci_bus *pci_alloc_primary_bus(int bus)
  923. {
  924.  struct pci_bus *b;
  925.  if(pci_bus_exists(&pci_root_buses,bus))
  926.  {
  927.   return NULL;
  928.  }
  929.  b=pci_alloc_bus();
  930.  list_add_tail(&b->node,&pci_root_buses);
  931.  b->number=b->secondary=bus;
  932.  b->resource[0]=&ioport_resource;
  933.  b->resource[1]=&iomem_resource;
  934.  return b;
  935. }
  936.  
  937. //-------------------------------- pci_scan_bus --------------------------------
  938. struct pci_bus *pci_scan_bus(int bus,struct pci_ops *ops,void *sysdata)
  939. {
  940.  struct pci_bus *b=pci_alloc_primary_bus(bus);
  941.  if(b)
  942.  {
  943.   b->sysdata=sysdata;
  944.   b->ops=ops;
  945.   b->subordinate=pci_do_scan_bus(b);
  946.  }
  947.  return b;
  948. }
  949.  
  950. #ifdef TARGET_OS2
  951. void pci_name_device_free(void);
  952. #endif
  953.  
  954. //---------------------------------- pci_init ----------------------------------
  955. void pci_init(void)
  956. {
  957.  struct pci_dev *dev;
  958.  pcibios_init();
  959.  pci_for_each_dev(dev)
  960.   pci_fixup_device(PCI_FIXUP_FINAL,dev);
  961.  #ifdef CONFIG_PM
  962.  pm_register(PM_PCI_DEV,0,pci_pm_callback);
  963.  #endif
  964.  #ifdef TARGET_OS2
  965.  pci_name_device_free();
  966.  #endif
  967. }
  968.