pcibr.c
上传用户:lgb322
上传日期:2013-02-24
资源大小:30529k
文件大小:285k
源码类别:

嵌入式Linux

开发平台:

Unix_Linux

  1. /* $Id$
  2.  *
  3.  * This file is subject to the terms and conditions of the GNU General Public
  4.  * License.  See the file "COPYING" in the main directory of this archive
  5.  * for more details.
  6.  *
  7.  * Copyright (C) 1992 - 1997, 2000 Silicon Graphics, Inc.
  8.  * Copyright (C) 2000 by Colin Ngam
  9.  */
  10. #ifdef BRINGUP
  11. int NeedXbridgeSwap = 0;
  12. #endif
  13. #include <linux/types.h>
  14. #include <linux/config.h>
  15. #include <linux/slab.h>
  16. #include <linux/module.h>
  17. #include <asm/sn/sgi.h>
  18. #include <asm/sn/addrs.h>
  19. #include <asm/sn/arch.h>
  20. #include <asm/sn/iograph.h>
  21. #include <asm/sn/invent.h>
  22. #include <asm/sn/hcl.h>
  23. #include <asm/sn/labelcl.h>
  24. #include <asm/sn/xtalk/xwidget.h>
  25. #include <asm/sn/pci/bridge.h>
  26. #include <asm/sn/pci/pciio.h>
  27. #include <asm/sn/pci/pcibr.h>
  28. #include <asm/sn/pci/pcibr_private.h>
  29. #include <asm/sn/pci/pci_defs.h>
  30. #include <asm/sn/prio.h>
  31. #include <asm/sn/ioerror_handling.h>
  32. #include <asm/sn/xtalk/xbow.h>
  33. #include <asm/sn/ioc3.h>
  34. #include <asm/sn/eeprom.h>
  35. #include <asm/sn/sn1/bedrock.h>
  36. #include <asm/sn/sn_private.h>
  37. #if defined(CONFIG_SGI_IP35) || defined(CONFIG_IA64_SGI_SN1) || defined(CONFIG_IA64_GENERIC)
  38. #include <asm/sn/sn1/hubio.h>
  39. #include <asm/sn/sn1/hubio_next.h>
  40. #endif
  41. #ifdef __ia64
  42. #define rmallocmap atemapalloc
  43. #define rmfreemap atemapfree
  44. #define rmfree atefree
  45. #define rmalloc atealloc
  46. #endif
  47. #undef PCIBR_ATE_DEBUG
  48. #if defined(BRINGUP)
  49. #if 0
  50. #define DEBUG 1  /* To avoid lots of bad printk() formats leave off */
  51. #endif
  52. #define PCI_DEBUG 1
  53. #define ATTACH_DEBUG 1
  54. #define PCIBR_SOFT_LIST 1
  55. #endif
  56. #ifndef LOCAL
  57. #define LOCAL static
  58. #endif
  59. /*
  60.  * Macros related to the Lucent USS 302/312 usb timeout workaround.  It
  61.  * appears that if the lucent part can get into a retry loop if it sees a
  62.  * DAC on the bus during a pio read retry.  The loop is broken after about
  63.  * 1ms, so we need to set up bridges holding this part to allow at least
  64.  * 1ms for pio.
  65.  */
  66. #define USS302_TIMEOUT_WAR
  67. #ifdef USS302_TIMEOUT_WAR
  68. #include <asm/sn/io.h>
  69. #define LUCENT_USBHC_VENDOR_ID_NUM 0x11c1
  70. #define LUCENT_USBHC302_DEVICE_ID_NUM 0x5801
  71. #define LUCENT_USBHC312_DEVICE_ID_NUM 0x5802
  72. #define USS302_BRIDGE_TIMEOUT_HLD 4
  73. #endif
  74. #define PCIBR_LLP_CONTROL_WAR
  75. #if defined (PCIBR_LLP_CONTROL_WAR)
  76. int                     pcibr_llp_control_war_cnt;
  77. #endif /* PCIBR_LLP_CONTROL_WAR */
  78. #define NEWAf(ptr,n,f) (ptr = kmem_zalloc((n)*sizeof (*(ptr)), (f&PCIIO_NOSLEEP)?KM_NOSLEEP:KM_SLEEP))
  79. #define NEWA(ptr,n) (ptr = kmem_zalloc((n)*sizeof (*(ptr)), KM_SLEEP))
  80. #define DELA(ptr,n) (kfree(ptr))
  81. #define NEWf(ptr,f) NEWAf(ptr,1,f)
  82. #define NEW(ptr) NEWA(ptr,1)
  83. #define DEL(ptr) DELA(ptr,1)
  84. int                     pcibr_devflag = D_MP;
  85. #ifdef LATER
  86. #define F(s,n) { 1l<<(s),-(s), n }
  87. struct reg_desc         bridge_int_status_desc[] =
  88. {
  89.     F(31, "MULTI_ERR"),
  90.     F(30, "PMU_ESIZE_EFAULT"),
  91.     F(29, "UNEXPECTED_RESP"),
  92.     F(28, "BAD_XRESP_PACKET"),
  93.     F(27, "BAD_XREQ_PACKET"),
  94.     F(26, "RESP_XTALK_ERROR"),
  95.     F(25, "REQ_XTALK_ERROR"),
  96.     F(24, "INVALID_ADDRESS"),
  97.     F(23, "UNSUPPORTED_XOP"),
  98.     F(22, "XREQ_FIFO_OFLOW"),
  99.     F(21, "LLP_REC_SNERROR"),
  100.     F(20, "LLP_REC_CBERROR"),
  101.     F(19, "LLP_RCTY"),
  102.     F(18, "LLP_TX_RETRY"),
  103.     F(17, "LLP_TCTY"),
  104.     F(16, "SSRAM_PERR"),
  105.     F(15, "PCI_ABORT"),
  106.     F(14, "PCI_PARITY"),
  107.     F(13, "PCI_SERR"),
  108.     F(12, "PCI_PERR"),
  109.     F(11, "PCI_MASTER_TOUT"),
  110.     F(10, "PCI_RETRY_CNT"),
  111.     F(9, "XREAD_REQ_TOUT"),
  112.     F(8, "GIO_BENABLE_ERR"),
  113.     F(7, "INT7"),
  114.     F(6, "INT6"),
  115.     F(5, "INT5"),
  116.     F(4, "INT4"),
  117.     F(3, "INT3"),
  118.     F(2, "INT2"),
  119.     F(1, "INT1"),
  120.     F(0, "INT0"),
  121.     {0}
  122. };
  123. struct reg_values       space_v[] =
  124. {
  125.     {PCIIO_SPACE_NONE, "none"},
  126.     {PCIIO_SPACE_ROM, "ROM"},
  127.     {PCIIO_SPACE_IO, "I/O"},
  128.     {PCIIO_SPACE_MEM, "MEM"},
  129.     {PCIIO_SPACE_MEM32, "MEM(32)"},
  130.     {PCIIO_SPACE_MEM64, "MEM(64)"},
  131.     {PCIIO_SPACE_CFG, "CFG"},
  132.     {PCIIO_SPACE_WIN(0), "WIN(0)"},
  133.     {PCIIO_SPACE_WIN(1), "WIN(1)"},
  134.     {PCIIO_SPACE_WIN(2), "WIN(2)"},
  135.     {PCIIO_SPACE_WIN(3), "WIN(3)"},
  136.     {PCIIO_SPACE_WIN(4), "WIN(4)"},
  137.     {PCIIO_SPACE_WIN(5), "WIN(5)"},
  138.     {PCIIO_SPACE_BAD, "BAD"},
  139.     {0}
  140. };
  141. struct reg_desc         space_desc[] =
  142. {
  143.     {0xFF, 0, "space", 0, space_v},
  144.     {0}
  145. };
  146. #if DEBUG
  147. #define device_desc device_bits
  148. LOCAL struct reg_desc   device_bits[] =
  149. {
  150.     {BRIDGE_DEV_ERR_LOCK_EN, 0, "ERR_LOCK_EN"},
  151.     {BRIDGE_DEV_PAGE_CHK_DIS, 0, "PAGE_CHK_DIS"},
  152.     {BRIDGE_DEV_FORCE_PCI_PAR, 0, "FORCE_PCI_PAR"},
  153.     {BRIDGE_DEV_VIRTUAL_EN, 0, "VIRTUAL_EN"},
  154.     {BRIDGE_DEV_PMU_WRGA_EN, 0, "PMU_WRGA_EN"},
  155.     {BRIDGE_DEV_DIR_WRGA_EN, 0, "DIR_WRGA_EN"},
  156.     {BRIDGE_DEV_DEV_SIZE, 0, "DEV_SIZE"},
  157.     {BRIDGE_DEV_RT, 0, "RT"},
  158.     {BRIDGE_DEV_SWAP_PMU, 0, "SWAP_PMU"},
  159.     {BRIDGE_DEV_SWAP_DIR, 0, "SWAP_DIR"},
  160.     {BRIDGE_DEV_PREF, 0, "PREF"},
  161.     {BRIDGE_DEV_PRECISE, 0, "PRECISE"},
  162.     {BRIDGE_DEV_COH, 0, "COH"},
  163.     {BRIDGE_DEV_BARRIER, 0, "BARRIER"},
  164.     {BRIDGE_DEV_GBR, 0, "GBR"},
  165.     {BRIDGE_DEV_DEV_SWAP, 0, "DEV_SWAP"},
  166.     {BRIDGE_DEV_DEV_IO_MEM, 0, "DEV_IO_MEM"},
  167.     {BRIDGE_DEV_OFF_MASK, BRIDGE_DEV_OFF_ADDR_SHFT, "DEV_OFF", "%x"},
  168.     {0}
  169. };
  170. #endif /* DEBUG */
  171. #ifdef SUPPORT_PRINTING_R_FORMAT
  172. LOCAL struct reg_values xio_cmd_pactyp[] =
  173. {
  174.     {0x0, "RdReq"},
  175.     {0x1, "RdResp"},
  176.     {0x2, "WrReqWithResp"},
  177.     {0x3, "WrResp"},
  178.     {0x4, "WrReqNoResp"},
  179.     {0x5, "Reserved(5)"},
  180.     {0x6, "FetchAndOp"},
  181.     {0x7, "Reserved(7)"},
  182.     {0x8, "StoreAndOp"},
  183.     {0x9, "Reserved(9)"},
  184.     {0xa, "Reserved(a)"},
  185.     {0xb, "Reserved(b)"},
  186.     {0xc, "Reserved(c)"},
  187.     {0xd, "Reserved(d)"},
  188.     {0xe, "SpecialReq"},
  189.     {0xf, "SpecialResp"},
  190.     {0}
  191. };
  192. LOCAL struct reg_desc   xio_cmd_bits[] =
  193. {
  194.     {WIDGET_DIDN, -28, "DIDN", "%x"},
  195.     {WIDGET_SIDN, -24, "SIDN", "%x"},
  196.     {WIDGET_PACTYP, -20, "PACTYP", 0, xio_cmd_pactyp},
  197.     {WIDGET_TNUM, -15, "TNUM", "%x"},
  198.     {WIDGET_COHERENT, 0, "COHERENT"},
  199.     {WIDGET_DS, 0, "DS"},
  200.     {WIDGET_GBR, 0, "GBR"},
  201.     {WIDGET_VBPM, 0, "VBPM"},
  202.     {WIDGET_ERROR, 0, "ERROR"},
  203.     {WIDGET_BARRIER, 0, "BARRIER"},
  204.     {0}
  205. };
  206. #endif /* SUPPORT_PRINTING_R_FORMAT */
  207. #if PCIBR_FREEZE_TIME || PCIBR_ATE_DEBUG
  208. LOCAL struct reg_desc   ate_bits[] =
  209. {
  210.     {0xFFFF000000000000ull, -48, "RMF", "%x"},
  211.     {~(IOPGSIZE - 1) & /* may trim off some low bits */
  212.      0x0000FFFFFFFFF000ull, 0, "XIO", "%x"},
  213.     {0x0000000000000F00ull, -8, "port", "%x"},
  214.     {0x0000000000000010ull, 0, "Barrier"},
  215.     {0x0000000000000008ull, 0, "Prefetch"},
  216.     {0x0000000000000004ull, 0, "Precise"},
  217.     {0x0000000000000002ull, 0, "Coherent"},
  218.     {0x0000000000000001ull, 0, "Valid"},
  219.     {0}
  220. };
  221. #endif
  222. #if PCIBR_ATE_DEBUG
  223. LOCAL struct reg_values ssram_sizes[] =
  224. {
  225.     {BRIDGE_CTRL_SSRAM_512K, "512k"},
  226.     {BRIDGE_CTRL_SSRAM_128K, "128k"},
  227.     {BRIDGE_CTRL_SSRAM_64K, "64k"},
  228.     {BRIDGE_CTRL_SSRAM_1K, "1k"},
  229.     {0}
  230. };
  231. LOCAL struct reg_desc   control_bits[] =
  232. {
  233.     {BRIDGE_CTRL_FLASH_WR_EN, 0, "FLASH_WR_EN"},
  234.     {BRIDGE_CTRL_EN_CLK50, 0, "EN_CLK50"},
  235.     {BRIDGE_CTRL_EN_CLK40, 0, "EN_CLK40"},
  236.     {BRIDGE_CTRL_EN_CLK33, 0, "EN_CLK33"},
  237.     {BRIDGE_CTRL_RST_MASK, -24, "RST", "%x"},
  238.     {BRIDGE_CTRL_IO_SWAP, 0, "IO_SWAP"},
  239.     {BRIDGE_CTRL_MEM_SWAP, 0, "MEM_SWAP"},
  240.     {BRIDGE_CTRL_PAGE_SIZE, 0, "PAGE_SIZE"},
  241.     {BRIDGE_CTRL_SS_PAR_BAD, 0, "SS_PAR_BAD"},
  242.     {BRIDGE_CTRL_SS_PAR_EN, 0, "SS_PAR_EN"},
  243.     {BRIDGE_CTRL_SSRAM_SIZE_MASK, 0, "SSRAM_SIZE", 0, ssram_sizes},
  244.     {BRIDGE_CTRL_F_BAD_PKT, 0, "F_BAD_PKT"},
  245.     {BRIDGE_CTRL_LLP_XBAR_CRD_MASK, -12, "LLP_XBAR_CRD", "%d"},
  246.     {BRIDGE_CTRL_CLR_RLLP_CNT, 0, "CLR_RLLP_CNT"},
  247.     {BRIDGE_CTRL_CLR_TLLP_CNT, 0, "CLR_TLLP_CNT"},
  248.     {BRIDGE_CTRL_SYS_END, 0, "SYS_END"},
  249.     {BRIDGE_CTRL_MAX_TRANS_MASK, -4, "MAX_TRANS", "%d"},
  250.     {BRIDGE_CTRL_WIDGET_ID_MASK, 0, "WIDGET_ID", "%x"},
  251.     {0}
  252. };
  253. #endif
  254. #endif /* LATER */
  255. /* kbrick widgetnum-to-bus layout */
  256. int p_busnum[MAX_PORT_NUM] = {                  /* widget#      */
  257.         0, 0, 0, 0, 0, 0, 0, 0,                 /* 0x0 - 0x7    */
  258.         2,                                      /* 0x8          */
  259.         1,                                      /* 0x9          */
  260.         0, 0,                                   /* 0xa - 0xb    */
  261.         5,                                      /* 0xc          */
  262.         6,                                      /* 0xd          */
  263.         4,                                      /* 0xe          */
  264.         3,                                      /* 0xf          */
  265. };
  266. /*
  267.  * Additional PIO spaces per slot are
  268.  * recorded in this structure.
  269.  */
  270. struct pciio_piospace_s {
  271.     pciio_piospace_t        next; /* another space for this device */
  272.     char                    free; /* 1 if free, 0 if in use               */
  273.     pciio_space_t           space; /* Which space is in use                */
  274.     iopaddr_t               start; /* Starting address of the PIO space    */
  275.     size_t                  count; /* size of PIO space                    */
  276. };
  277. /* Use io spin locks. This ensures that all the PIO writes from a particular
  278.  * CPU to a particular IO device are synched before the start of the next
  279.  * set of PIO operations to the same device.
  280.  */
  281. #define pcibr_lock(pcibr_soft) io_splock(&pcibr_soft->bs_lock)
  282. #define pcibr_unlock(pcibr_soft,s) io_spunlock(&pcibr_soft->bs_lock,s)
  283. #if PCIBR_SOFT_LIST
  284. typedef struct pcibr_list_s *pcibr_list_p;
  285. struct pcibr_list_s {
  286.     pcibr_list_p            bl_next;
  287.     pcibr_soft_t            bl_soft;
  288.     devfs_handle_t            bl_vhdl;
  289. };
  290. pcibr_list_p            pcibr_list = 0;
  291. #endif
  292. typedef volatile unsigned *cfg_p;
  293. typedef volatile bridgereg_t *reg_p;
  294. #define INFO_LBL_PCIBR_ASIC_REV "_pcibr_asic_rev"
  295. #define PCIBR_D64_BASE_UNSET (0xFFFFFFFFFFFFFFFF)
  296. #define PCIBR_D32_BASE_UNSET (0xFFFFFFFF)
  297. #define PCIBR_VALID_SLOT(s) (s < 8)
  298. #ifdef SN_XXX
  299. extern int      hub_device_flags_set(devfs_handle_t       widget_dev,
  300.                                      hub_widget_flags_t flags);
  301. #endif
  302. extern pciio_dmamap_t get_free_pciio_dmamap(devfs_handle_t);
  303. /*
  304.  * This is the file operation table for the pcibr driver.
  305.  * As each of the functions are implemented, put the 
  306.  * appropriate function name below.
  307.  */
  308. struct file_operations pcibr_fops = {
  309.         owner:  THIS_MODULE,
  310.         llseek: NULL,
  311.         read: NULL,
  312.         write: NULL,
  313.         readdir: NULL,
  314.         poll: NULL,
  315.         ioctl: NULL,
  316.         mmap: NULL,
  317.         open: NULL,
  318.         flush: NULL,
  319.         release: NULL,
  320.         fsync: NULL,
  321.         fasync: NULL,
  322.         lock: NULL,
  323.         readv: NULL,
  324.         writev: NULL
  325. };
  326. extern devfs_handle_t hwgraph_root;
  327. extern graph_error_t hwgraph_vertex_unref(devfs_handle_t vhdl);
  328. extern int cap_able(uint64_t x);
  329. extern uint64_t rmalloc(struct map *mp, size_t size);
  330. extern void rmfree(struct map *mp, size_t size, uint64_t a);
  331. extern int hwgraph_vertex_name_get(devfs_handle_t vhdl, char *buf, uint buflen);
  332. extern long atoi(register char *p);
  333. extern void *swap_ptr(void **loc, void *new);
  334. extern char *dev_to_name(devfs_handle_t dev, char *buf, uint buflen);
  335. extern cnodeid_t nodevertex_to_cnodeid(devfs_handle_t vhdl);
  336. extern graph_error_t hwgraph_edge_remove(devfs_handle_t from, char *name, devfs_handle_t *toptr);
  337. extern struct map *rmallocmap(uint64_t mapsiz);
  338. extern void rmfreemap(struct map *mp);
  339. extern int compare_and_swap_ptr(void **location, void *old_ptr, void *new_ptr);
  340. extern int io_path_map_widget(devfs_handle_t vertex);
  341. /* =====================================================================
  342.  *    Function Table of Contents
  343.  *
  344.  *      The order of functions in this file has stopped
  345.  *      making much sense. We might want to take a look
  346.  *      at it some time and bring back some sanity, or
  347.  *      perhaps bust this file into smaller chunks.
  348.  */
  349. LOCAL void              do_pcibr_rrb_clear(bridge_t *, int);
  350. LOCAL void              do_pcibr_rrb_flush(bridge_t *, int);
  351. LOCAL int               do_pcibr_rrb_count_valid(bridge_t *, pciio_slot_t);
  352. LOCAL int               do_pcibr_rrb_count_avail(bridge_t *, pciio_slot_t);
  353. LOCAL int               do_pcibr_rrb_alloc(bridge_t *, pciio_slot_t, int);
  354. LOCAL int               do_pcibr_rrb_free(bridge_t *, pciio_slot_t, int);
  355. LOCAL void              do_pcibr_rrb_autoalloc(pcibr_soft_t, int, int);
  356. int pcibr_wrb_flush(devfs_handle_t);
  357. int                     pcibr_rrb_alloc(devfs_handle_t, int *, int *);
  358. int                     pcibr_rrb_check(devfs_handle_t, int *, int *, int *, int *);
  359. int                     pcibr_alloc_all_rrbs(devfs_handle_t, int, int, int, int, int, int, int, int, int);
  360. void                    pcibr_rrb_flush(devfs_handle_t);
  361. LOCAL int               pcibr_try_set_device(pcibr_soft_t, pciio_slot_t, unsigned, bridgereg_t);
  362. void                    pcibr_release_device(pcibr_soft_t, pciio_slot_t, bridgereg_t);
  363. LOCAL void              pcibr_clearwidint(bridge_t *);
  364. LOCAL void              pcibr_setwidint(xtalk_intr_t);
  365. LOCAL int               pcibr_probe_slot(bridge_t *, cfg_p, unsigned *);
  366. void                    pcibr_init(void);
  367. int                     pcibr_attach(devfs_handle_t);
  368. int pcibr_detach(devfs_handle_t);
  369. int                     pcibr_open(devfs_handle_t *, int, int, cred_t *);
  370. int                     pcibr_close(devfs_handle_t, int, int, cred_t *);
  371. int                     pcibr_map(devfs_handle_t, vhandl_t *, off_t, size_t, uint);
  372. int                     pcibr_unmap(devfs_handle_t, vhandl_t *);
  373. int                     pcibr_ioctl(devfs_handle_t, int, void *, int, struct cred *, int *);
  374. void                    pcibr_freeblock_sub(iopaddr_t *, iopaddr_t *, iopaddr_t, size_t);
  375. LOCAL int               pcibr_init_ext_ate_ram(bridge_t *);
  376. LOCAL int               pcibr_ate_alloc(pcibr_soft_t, int);
  377. LOCAL void              pcibr_ate_free(pcibr_soft_t, int, int);
  378. LOCAL pcibr_info_t      pcibr_info_get(devfs_handle_t);
  379. LOCAL pcibr_info_t      pcibr_device_info_new(pcibr_soft_t, pciio_slot_t, pciio_function_t, pciio_vendor_id_t, pciio_device_id_t);
  380. LOCAL void pcibr_device_info_free(devfs_handle_t, pciio_slot_t);
  381. LOCAL iopaddr_t         pcibr_addr_pci_to_xio(devfs_handle_t, pciio_slot_t, pciio_space_t, iopaddr_t, size_t, unsigned);
  382. pcibr_piomap_t          pcibr_piomap_alloc(devfs_handle_t, device_desc_t, pciio_space_t, iopaddr_t, size_t, size_t, unsigned);
  383. void                    pcibr_piomap_free(pcibr_piomap_t);
  384. caddr_t                 pcibr_piomap_addr(pcibr_piomap_t, iopaddr_t, size_t);
  385. void                    pcibr_piomap_done(pcibr_piomap_t);
  386. caddr_t                 pcibr_piotrans_addr(devfs_handle_t, device_desc_t, pciio_space_t, iopaddr_t, size_t, unsigned);
  387. iopaddr_t               pcibr_piospace_alloc(devfs_handle_t, device_desc_t, pciio_space_t, size_t, size_t);
  388. void                    pcibr_piospace_free(devfs_handle_t, pciio_space_t, iopaddr_t, size_t);
  389. LOCAL iopaddr_t         pcibr_flags_to_d64(unsigned, pcibr_soft_t);
  390. LOCAL bridge_ate_t      pcibr_flags_to_ate(unsigned);
  391. pcibr_dmamap_t          pcibr_dmamap_alloc(devfs_handle_t, device_desc_t, size_t, unsigned);
  392. void                    pcibr_dmamap_free(pcibr_dmamap_t);
  393. LOCAL bridge_ate_p      pcibr_ate_addr(pcibr_soft_t, int);
  394. LOCAL iopaddr_t         pcibr_addr_xio_to_pci(pcibr_soft_t, iopaddr_t, size_t);
  395. iopaddr_t               pcibr_dmamap_addr(pcibr_dmamap_t, paddr_t, size_t);
  396. alenlist_t              pcibr_dmamap_list(pcibr_dmamap_t, alenlist_t, unsigned);
  397. void                    pcibr_dmamap_done(pcibr_dmamap_t);
  398. cnodeid_t pcibr_get_dmatrans_node(devfs_handle_t);
  399. iopaddr_t               pcibr_dmatrans_addr(devfs_handle_t, device_desc_t, paddr_t, size_t, unsigned);
  400. alenlist_t              pcibr_dmatrans_list(devfs_handle_t, device_desc_t, alenlist_t, unsigned);
  401. void                    pcibr_dmamap_drain(pcibr_dmamap_t);
  402. void                    pcibr_dmaaddr_drain(devfs_handle_t, paddr_t, size_t);
  403. void                    pcibr_dmalist_drain(devfs_handle_t, alenlist_t);
  404. iopaddr_t               pcibr_dmamap_pciaddr_get(pcibr_dmamap_t);
  405. static unsigned pcibr_intr_bits(pciio_info_t info, pciio_intr_line_t lines);
  406. pcibr_intr_t            pcibr_intr_alloc(devfs_handle_t, device_desc_t, pciio_intr_line_t, devfs_handle_t);
  407. void                    pcibr_intr_free(pcibr_intr_t);
  408. LOCAL void              pcibr_setpciint(xtalk_intr_t);
  409. int                     pcibr_intr_connect(pcibr_intr_t, intr_func_t, intr_arg_t, void *);
  410. void                    pcibr_intr_disconnect(pcibr_intr_t);
  411. devfs_handle_t            pcibr_intr_cpu_get(pcibr_intr_t);
  412. void                    pcibr_xintr_preset(void *, int, xwidgetnum_t, iopaddr_t, xtalk_intr_vector_t);
  413. void                    pcibr_intr_func(intr_arg_t);
  414. LOCAL void              print_bridge_errcmd(uint32_t, char *);
  415. void                    pcibr_error_dump(pcibr_soft_t);
  416. uint32_t              pcibr_errintr_group(uint32_t);
  417. LOCAL void pcibr_pioerr_check(pcibr_soft_t);
  418. LOCAL void              pcibr_error_intr_handler(intr_arg_t);
  419. LOCAL int               pcibr_addr_toslot(pcibr_soft_t, iopaddr_t, pciio_space_t *, iopaddr_t *, pciio_function_t *);
  420. LOCAL void              pcibr_error_cleanup(pcibr_soft_t, int);
  421. void                    pcibr_device_disable(pcibr_soft_t, int);
  422. LOCAL int               pcibr_pioerror(pcibr_soft_t, int, ioerror_mode_t, ioerror_t *);
  423. int                     pcibr_dmard_error(pcibr_soft_t, int, ioerror_mode_t, ioerror_t *);
  424. int                     pcibr_dmawr_error(pcibr_soft_t, int, ioerror_mode_t, ioerror_t *);
  425. LOCAL int               pcibr_error_handler(error_handler_arg_t, int, ioerror_mode_t, ioerror_t *);
  426. int                     pcibr_error_devenable(devfs_handle_t, int);
  427. void                    pcibr_provider_startup(devfs_handle_t);
  428. void                    pcibr_provider_shutdown(devfs_handle_t);
  429. int                     pcibr_reset(devfs_handle_t);
  430. pciio_endian_t          pcibr_endian_set(devfs_handle_t, pciio_endian_t, pciio_endian_t);
  431. int                     pcibr_priority_bits_set(pcibr_soft_t, pciio_slot_t, pciio_priority_t);
  432. pciio_priority_t        pcibr_priority_set(devfs_handle_t, pciio_priority_t);
  433. int                     pcibr_device_flags_set(devfs_handle_t, pcibr_device_flags_t);
  434. LOCAL cfg_p             pcibr_config_addr(devfs_handle_t, unsigned);
  435. uint64_t                pcibr_config_get(devfs_handle_t, unsigned, unsigned);
  436. LOCAL uint64_t          do_pcibr_config_get(cfg_p, unsigned, unsigned);
  437. void                    pcibr_config_set(devfs_handle_t, unsigned, unsigned, uint64_t);
  438. LOCAL void              do_pcibr_config_set(cfg_p, unsigned, unsigned, uint64_t);
  439. LOCAL pcibr_hints_t     pcibr_hints_get(devfs_handle_t, int);
  440. void                    pcibr_hints_fix_rrbs(devfs_handle_t);
  441. void                    pcibr_hints_dualslot(devfs_handle_t, pciio_slot_t, pciio_slot_t);
  442. void pcibr_hints_intr_bits(devfs_handle_t, pcibr_intr_bits_f *);
  443. void                    pcibr_set_rrb_callback(devfs_handle_t, rrb_alloc_funct_t);
  444. void                    pcibr_hints_handsoff(devfs_handle_t);
  445. void                    pcibr_hints_subdevs(devfs_handle_t, pciio_slot_t, ulong);
  446. LOCAL int pcibr_slot_info_init(devfs_handle_t,pciio_slot_t);
  447. LOCAL int pcibr_slot_info_free(devfs_handle_t,pciio_slot_t);
  448. #ifdef LATER
  449. LOCAL int         pcibr_slot_info_return(pcibr_soft_t, pciio_slot_t,
  450.                                                pcibr_slot_info_resp_t);
  451. LOCAL void        pcibr_slot_func_info_return(pcibr_info_h, int,
  452.                                                     pcibr_slot_func_info_resp_t);
  453. #endif /* LATER */
  454. LOCAL int pcibr_slot_addr_space_init(devfs_handle_t,pciio_slot_t);
  455. LOCAL int pcibr_slot_device_init(devfs_handle_t, pciio_slot_t);
  456. LOCAL int pcibr_slot_guest_info_init(devfs_handle_t,pciio_slot_t);
  457. LOCAL int pcibr_slot_initial_rrb_alloc(devfs_handle_t,pciio_slot_t);
  458. LOCAL int pcibr_slot_call_device_attach(devfs_handle_t,
  459.       pciio_slot_t, int);
  460. LOCAL int pcibr_slot_call_device_detach(devfs_handle_t,
  461.       pciio_slot_t, int);
  462. LOCAL int               pcibr_slot_detach(devfs_handle_t, pciio_slot_t, int);
  463. LOCAL int  pcibr_is_slot_sys_critical(devfs_handle_t, pciio_slot_t);
  464. #ifdef LATER
  465. LOCAL int pcibr_slot_query(devfs_handle_t, pcibr_slot_info_req_t);
  466. #endif
  467. /* =====================================================================
  468.  *    RRB management
  469.  */
  470. #define LSBIT(word) ((word) &~ ((word)-1))
  471. #define PCIBR_RRB_SLOT_VIRTUAL 8
  472. LOCAL void
  473. do_pcibr_rrb_clear(bridge_t *bridge, int rrb)
  474. {
  475.     bridgereg_t             status;
  476.     /* bridge_lock must be held;
  477.      * this RRB must be disabled.
  478.      */
  479.     /* wait until RRB has no outstanduing XIO packets. */
  480.     while ((status = bridge->b_resp_status) & BRIDGE_RRB_INUSE(rrb)) {
  481. ; /* XXX- beats on bridge. bad idea? */
  482.     }
  483.     /* if the RRB has data, drain it. */
  484.     if (status & BRIDGE_RRB_VALID(rrb)) {
  485. bridge->b_resp_clear = BRIDGE_RRB_CLEAR(rrb);
  486. /* wait until RRB is no longer valid. */
  487. while ((status = bridge->b_resp_status) & BRIDGE_RRB_VALID(rrb)) {
  488.     ; /* XXX- beats on bridge. bad idea? */
  489. }
  490.     }
  491. }
  492. LOCAL void
  493. do_pcibr_rrb_flush(bridge_t *bridge, int rrbn)
  494. {
  495.     reg_p                   rrbp = &bridge->b_rrb_map[rrbn & 1].reg;
  496.     bridgereg_t             rrbv;
  497.     int                     shft = 4 * (rrbn >> 1);
  498.     unsigned                ebit = BRIDGE_RRB_EN << shft;
  499.     rrbv = *rrbp;
  500.     if (rrbv & ebit)
  501. *rrbp = rrbv & ~ebit;
  502.     do_pcibr_rrb_clear(bridge, rrbn);
  503.     if (rrbv & ebit)
  504. *rrbp = rrbv;
  505. }
  506. /*
  507.  *    pcibr_rrb_count_valid: count how many RRBs are
  508.  *      marked valid for the specified PCI slot on this
  509.  *      bridge.
  510.  *
  511.  *      NOTE: The "slot" parameter for all pcibr_rrb
  512.  *      management routines must include the "virtual"
  513.  *      bit; when manageing both the normal and the
  514.  *      virtual channel, separate calls to these
  515.  *      routines must be made. To denote the virtual
  516.  *      channel, add PCIBR_RRB_SLOT_VIRTUAL to the slot
  517.  *      number.
  518.  *
  519.  *      IMPL NOTE: The obvious algorithm is to iterate
  520.  *      through the RRB fields, incrementing a count if
  521.  *      the RRB is valid and matches the slot. However,
  522.  *      it is much simpler to use an algorithm derived
  523.  *      from the "partitioned add" idea. First, XOR in a
  524.  *      pattern such that the fields that match this
  525.  *      slot come up "all ones" and all other fields
  526.  *      have zeros in the mismatching bits. Then AND
  527.  *      together the bits in the field, so we end up
  528.  *      with one bit turned on for each field that
  529.  *      matched. Now we need to count these bits. This
  530.  *      can be done either with a series of shift/add
  531.  *      instructions or by using "tmp % 15"; I expect
  532.  *      that the cascaded shift/add will be faster.
  533.  */
  534. LOCAL int
  535. do_pcibr_rrb_count_valid(bridge_t *bridge,
  536.  pciio_slot_t slot)
  537. {
  538.     bridgereg_t             tmp;
  539.     tmp = bridge->b_rrb_map[slot & 1].reg;
  540.     tmp ^= 0x11111111 * (7 - slot / 2);
  541.     tmp &= (0xCCCCCCCC & tmp) >> 2;
  542.     tmp &= (0x22222222 & tmp) >> 1;
  543.     tmp += tmp >> 4;
  544.     tmp += tmp >> 8;
  545.     tmp += tmp >> 16;
  546.     return tmp & 15;
  547. }
  548. /*
  549.  *    do_pcibr_rrb_count_avail: count how many RRBs are
  550.  *      available to be allocated for the specified slot.
  551.  *
  552.  *      IMPL NOTE: similar to the above, except we are
  553.  *      just counting how many fields have the valid bit
  554.  *      turned off.
  555.  */
  556. LOCAL int
  557. do_pcibr_rrb_count_avail(bridge_t *bridge,
  558.  pciio_slot_t slot)
  559. {
  560.     bridgereg_t             tmp;
  561.     tmp = bridge->b_rrb_map[slot & 1].reg;
  562.     tmp = (0x88888888 & ~tmp) >> 3;
  563.     tmp += tmp >> 4;
  564.     tmp += tmp >> 8;
  565.     tmp += tmp >> 16;
  566.     return tmp & 15;
  567. }
  568. /*
  569.  *    do_pcibr_rrb_alloc: allocate some additional RRBs
  570.  *      for the specified slot. Returns -1 if there were
  571.  *      insufficient free RRBs to satisfy the request,
  572.  *      or 0 if the request was fulfilled.
  573.  *
  574.  *      Note that if a request can be partially filled,
  575.  *      it will be, even if we return failure.
  576.  *
  577.  *      IMPL NOTE: again we avoid iterating across all
  578.  *      the RRBs; instead, we form up a word containing
  579.  *      one bit for each free RRB, then peel the bits
  580.  *      off from the low end.
  581.  */
  582. LOCAL int
  583. do_pcibr_rrb_alloc(bridge_t *bridge,
  584.    pciio_slot_t slot,
  585.    int more)
  586. {
  587.     int                     rv = 0;
  588.     bridgereg_t             reg, tmp, bit;
  589.     reg = bridge->b_rrb_map[slot & 1].reg;
  590.     tmp = (0x88888888 & ~reg) >> 3;
  591.     while (more-- > 0) {
  592. bit = LSBIT(tmp);
  593. if (!bit) {
  594.     rv = -1;
  595.     break;
  596. }
  597. tmp &= ~bit;
  598. reg = ((reg & ~(bit * 15)) | (bit * (8 + slot / 2)));
  599.     }
  600.     bridge->b_rrb_map[slot & 1].reg = reg;
  601.     return rv;
  602. }
  603. /*
  604.  *    do_pcibr_rrb_free: release some of the RRBs that
  605.  *      have been allocated for the specified
  606.  *      slot. Returns zero for success, or negative if
  607.  *      it was unable to free that many RRBs.
  608.  *
  609.  *      IMPL NOTE: We form up a bit for each RRB
  610.  *      allocated to the slot, aligned with the VALID
  611.  *      bitfield this time; then we peel bits off one at
  612.  *      a time, releasing the corresponding RRB.
  613.  */
  614. LOCAL int
  615. do_pcibr_rrb_free(bridge_t *bridge,
  616.   pciio_slot_t slot,
  617.   int less)
  618. {
  619.     int                     rv = 0;
  620.     bridgereg_t             reg, tmp, clr, bit;
  621.     int                     i;
  622.     clr = 0;
  623.     reg = bridge->b_rrb_map[slot & 1].reg;
  624.     /* This needs to be done otherwise the rrb's on the virtual channel
  625.      * for this slot won't be freed !!
  626.      */
  627.     tmp = reg & 0xbbbbbbbb;
  628.     tmp ^= (0x11111111 * (7 - slot / 2));
  629.     tmp &= (0x33333333 & tmp) << 2;
  630.     tmp &= (0x44444444 & tmp) << 1;
  631.     while (less-- > 0) {
  632. bit = LSBIT(tmp);
  633. if (!bit) {
  634.     rv = -1;
  635.     break;
  636. }
  637. tmp &= ~bit;
  638. reg &= ~bit;
  639. clr |= bit;
  640.     }
  641.     bridge->b_rrb_map[slot & 1].reg = reg;
  642.     for (i = 0; i < 8; i++)
  643. if (clr & (8 << (4 * i)))
  644.     do_pcibr_rrb_clear(bridge, (2 * i) + (slot & 1));
  645.     return rv;
  646. }
  647. LOCAL void
  648. do_pcibr_rrb_autoalloc(pcibr_soft_t pcibr_soft,
  649.        int slot,
  650.        int more_rrbs)
  651. {
  652.     bridge_t               *bridge = pcibr_soft->bs_base;
  653.     int                     got;
  654.     for (got = 0; got < more_rrbs; ++got) {
  655. if (pcibr_soft->bs_rrb_res[slot & 7] > 0)
  656.     pcibr_soft->bs_rrb_res[slot & 7]--;
  657. else if (pcibr_soft->bs_rrb_avail[slot & 1] > 0)
  658.     pcibr_soft->bs_rrb_avail[slot & 1]--;
  659. else
  660.     break;
  661. if (do_pcibr_rrb_alloc(bridge, slot, 1) < 0)
  662.     break;
  663. #if PCIBR_RRB_DEBUG
  664. printk( "do_pcibr_rrb_autoalloc: add one to slot %d%sn",
  665. slot & 7, slot & 8 ? "v" : "");
  666. #endif
  667. pcibr_soft->bs_rrb_valid[slot]++;
  668.     }
  669. #if PCIBR_RRB_DEBUG
  670.     printk("%s: %d+%d free RRBs. Allocation list:n", pcibr_soft->bs_name,
  671.     pcibr_soft->bs_rrb_avail[0],
  672.     pcibr_soft->bs_rrb_avail[1]);
  673.     for (slot = 0; slot < 8; ++slot)
  674. printk("t%d+%d+%d",
  675. 0xFFF & pcibr_soft->bs_rrb_valid[slot],
  676. 0xFFF & pcibr_soft->bs_rrb_valid[slot + PCIBR_RRB_SLOT_VIRTUAL],
  677. pcibr_soft->bs_rrb_res[slot]);
  678. printk("n");
  679. #endif
  680. }
  681. /*
  682.  * Device driver interface to flush the write buffers for a specified
  683.  * device hanging off the bridge.
  684.  */
  685. int
  686. pcibr_wrb_flush(devfs_handle_t pconn_vhdl)
  687. {
  688.     pciio_info_t            pciio_info = pciio_info_get(pconn_vhdl);
  689.     pciio_slot_t            pciio_slot = pciio_info_slot_get(pciio_info);
  690.     pcibr_soft_t            pcibr_soft = (pcibr_soft_t) pciio_info_mfast_get(pciio_info);
  691.     bridge_t               *bridge = pcibr_soft->bs_base;
  692.     volatile bridgereg_t   *wrb_flush;
  693.     wrb_flush = &(bridge->b_wr_req_buf[pciio_slot].reg);
  694.     while (*wrb_flush);
  695.     return(0);
  696. }
  697. /*
  698.  * Device driver interface to request RRBs for a specified device
  699.  * hanging off a Bridge.  The driver requests the total number of
  700.  * RRBs it would like for the normal channel (vchan0) and for the
  701.  * "virtual channel" (vchan1).  The actual number allocated to each
  702.  * channel is returned.
  703.  *
  704.  * If we cannot allocate at least one RRB to a channel that needs
  705.  * at least one, return -1 (failure).  Otherwise, satisfy the request
  706.  * as best we can and return 0.
  707.  */
  708. int
  709. pcibr_rrb_alloc(devfs_handle_t pconn_vhdl,
  710. int *count_vchan0,
  711. int *count_vchan1)
  712. {
  713.     pciio_info_t            pciio_info = pciio_info_get(pconn_vhdl);
  714.     pciio_slot_t            pciio_slot = pciio_info_slot_get(pciio_info);
  715.     pcibr_soft_t            pcibr_soft = (pcibr_soft_t) pciio_info_mfast_get(pciio_info);
  716.     bridge_t               *bridge = pcibr_soft->bs_base;
  717.     int                     desired_vchan0;
  718.     int                     desired_vchan1;
  719.     int                     orig_vchan0;
  720.     int                     orig_vchan1;
  721.     int                     delta_vchan0;
  722.     int                     delta_vchan1;
  723.     int                     final_vchan0;
  724.     int                     final_vchan1;
  725.     int                     avail_rrbs;
  726.     unsigned long           s;
  727.     int                     error;
  728.     /*
  729.      * TBD: temper request with admin info about RRB allocation,
  730.      * and according to demand from other devices on this Bridge.
  731.      *
  732.      * One way of doing this would be to allocate two RRBs
  733.      * for each device on the bus, before any drivers start
  734.      * asking for extras. This has the weakness that one
  735.      * driver might not give back an "extra" RRB until after
  736.      * another driver has already failed to get one that
  737.      * it wanted.
  738.      */
  739.     s = pcibr_lock(pcibr_soft);
  740.     /* How many RRBs do we own? */
  741.     orig_vchan0 = pcibr_soft->bs_rrb_valid[pciio_slot];
  742.     orig_vchan1 = pcibr_soft->bs_rrb_valid[pciio_slot + PCIBR_RRB_SLOT_VIRTUAL];
  743.     /* How many RRBs do we want? */
  744.     desired_vchan0 = count_vchan0 ? *count_vchan0 : orig_vchan0;
  745.     desired_vchan1 = count_vchan1 ? *count_vchan1 : orig_vchan1;
  746.     /* How many RRBs are free? */
  747.     avail_rrbs = pcibr_soft->bs_rrb_avail[pciio_slot & 1]
  748. + pcibr_soft->bs_rrb_res[pciio_slot];
  749.     /* Figure desired deltas */
  750.     delta_vchan0 = desired_vchan0 - orig_vchan0;
  751.     delta_vchan1 = desired_vchan1 - orig_vchan1;
  752.     /* Trim back deltas to something
  753.      * that we can actually meet, by
  754.      * decreasing the ending allocation
  755.      * for whichever channel wants
  756.      * more RRBs. If both want the same
  757.      * number, cut the second channel.
  758.      * NOTE: do not change the allocation for
  759.      * a channel that was passed as NULL.
  760.      */
  761.     while ((delta_vchan0 + delta_vchan1) > avail_rrbs) {
  762. if (count_vchan0 &&
  763.     (!count_vchan1 ||
  764.      ((orig_vchan0 + delta_vchan0) >
  765.       (orig_vchan1 + delta_vchan1))))
  766.     delta_vchan0--;
  767. else
  768.     delta_vchan1--;
  769.     }
  770.     /* Figure final RRB allocations
  771.      */
  772.     final_vchan0 = orig_vchan0 + delta_vchan0;
  773.     final_vchan1 = orig_vchan1 + delta_vchan1;
  774.     /* If either channel wants RRBs but our actions
  775.      * would leave it with none, declare an error,
  776.      * but DO NOT change any RRB allocations.
  777.      */
  778.     if ((desired_vchan0 && !final_vchan0) ||
  779. (desired_vchan1 && !final_vchan1)) {
  780. error = -1;
  781.     } else {
  782. /* Commit the allocations: free, then alloc.
  783.  */
  784. if (delta_vchan0 < 0)
  785.     (void) do_pcibr_rrb_free(bridge, pciio_slot, -delta_vchan0);
  786. if (delta_vchan1 < 0)
  787.     (void) do_pcibr_rrb_free(bridge, PCIBR_RRB_SLOT_VIRTUAL + pciio_slot, -delta_vchan1);
  788. if (delta_vchan0 > 0)
  789.     (void) do_pcibr_rrb_alloc(bridge, pciio_slot, delta_vchan0);
  790. if (delta_vchan1 > 0)
  791.     (void) do_pcibr_rrb_alloc(bridge, PCIBR_RRB_SLOT_VIRTUAL + pciio_slot, delta_vchan1);
  792. /* Return final values to caller.
  793.  */
  794. if (count_vchan0)
  795.     *count_vchan0 = final_vchan0;
  796. if (count_vchan1)
  797.     *count_vchan1 = final_vchan1;
  798. /* prevent automatic changes to this slot's RRBs
  799.  */
  800. pcibr_soft->bs_rrb_fixed |= 1 << pciio_slot;
  801. /* Track the actual allocations, release
  802.  * any further reservations, and update the
  803.  * number of available RRBs.
  804.  */
  805. pcibr_soft->bs_rrb_valid[pciio_slot] = final_vchan0;
  806. pcibr_soft->bs_rrb_valid[pciio_slot + PCIBR_RRB_SLOT_VIRTUAL] = final_vchan1;
  807. pcibr_soft->bs_rrb_avail[pciio_slot & 1] =
  808.     pcibr_soft->bs_rrb_avail[pciio_slot & 1]
  809.     + pcibr_soft->bs_rrb_res[pciio_slot]
  810.     - delta_vchan0
  811.     - delta_vchan1;
  812. pcibr_soft->bs_rrb_res[pciio_slot] = 0;
  813. #if PCIBR_RRB_DEBUG
  814. printk("pcibr_rrb_alloc: slot %d set to %d+%d; %d+%d freen",
  815. pciio_slot, final_vchan0, final_vchan1,
  816. pcibr_soft->bs_rrb_avail[0],
  817. pcibr_soft->bs_rrb_avail[1]);
  818. for (pciio_slot = 0; pciio_slot < 8; ++pciio_slot)
  819.     printk("t%d+%d+%d",
  820.     0xFFF & pcibr_soft->bs_rrb_valid[pciio_slot],
  821.     0xFFF & pcibr_soft->bs_rrb_valid[pciio_slot + PCIBR_RRB_SLOT_VIRTUAL],
  822.     pcibr_soft->bs_rrb_res[pciio_slot]);
  823. printk("n");
  824. #endif
  825. error = 0;
  826.     }
  827.     pcibr_unlock(pcibr_soft, s);
  828.     return error;
  829. }
  830. /*
  831.  * Device driver interface to check the current state
  832.  * of the RRB allocations.
  833.  *
  834.  *   pconn_vhdl is your PCI connection point (specifies which
  835.  *      PCI bus and which slot).
  836.  *
  837.  *   count_vchan0 points to where to return the number of RRBs
  838.  *      assigned to the primary DMA channel, used by all DMA
  839.  *      that does not explicitly ask for the alternate virtual
  840.  *      channel.
  841.  *
  842.  *   count_vchan1 points to where to return the number of RRBs
  843.  *      assigned to the secondary DMA channel, used when
  844.  *      PCIBR_VCHAN1 and PCIIO_DMA_A64 are specified.
  845.  *
  846.  *   count_reserved points to where to return the number of RRBs
  847.  *      that have been automatically reserved for your device at
  848.  *      startup, but which have not been assigned to a
  849.  *      channel. RRBs must be assigned to a channel to be used;
  850.  *      this can be done either with an explicit pcibr_rrb_alloc
  851.  *      call, or automatically by the infrastructure when a DMA
  852.  *      translation is constructed. Any call to pcibr_rrb_alloc
  853.  *      will release any unassigned reserved RRBs back to the
  854.  *      free pool.
  855.  *
  856.  *   count_pool points to where to return the number of RRBs
  857.  *      that are currently unassigned and unreserved. This
  858.  *      number can (and will) change as other drivers make calls
  859.  *      to pcibr_rrb_alloc, or automatically allocate RRBs for
  860.  *      DMA beyond their initial reservation.
  861.  *
  862.  * NULL may be passed for any of the return value pointers
  863.  * the caller is not interested in.
  864.  *
  865.  * The return value is "0" if all went well, or "-1" if
  866.  * there is a problem. Additionally, if the wrong vertex
  867.  * is passed in, one of the subsidiary support functions
  868.  * could panic with a "bad pciio fingerprint."
  869.  */
  870. int
  871. pcibr_rrb_check(devfs_handle_t pconn_vhdl,
  872. int *count_vchan0,
  873. int *count_vchan1,
  874. int *count_reserved,
  875. int *count_pool)
  876. {
  877.     pciio_info_t            pciio_info;
  878.     pciio_slot_t            pciio_slot;
  879.     pcibr_soft_t            pcibr_soft;
  880.     unsigned long           s;
  881.     int                     error = -1;
  882.     if ((pciio_info = pciio_info_get(pconn_vhdl)) &&
  883. (pcibr_soft = (pcibr_soft_t) pciio_info_mfast_get(pciio_info)) &&
  884. ((pciio_slot = pciio_info_slot_get(pciio_info)) < 8)) {
  885. s = pcibr_lock(pcibr_soft);
  886. if (count_vchan0)
  887.     *count_vchan0 =
  888. pcibr_soft->bs_rrb_valid[pciio_slot];
  889. if (count_vchan1)
  890.     *count_vchan1 =
  891. pcibr_soft->bs_rrb_valid[pciio_slot + PCIBR_RRB_SLOT_VIRTUAL];
  892. if (count_reserved)
  893.     *count_reserved =
  894. pcibr_soft->bs_rrb_res[pciio_slot];
  895. if (count_pool)
  896.     *count_pool =
  897. pcibr_soft->bs_rrb_avail[pciio_slot & 1];
  898. error = 0;
  899. pcibr_unlock(pcibr_soft, s);
  900.     }
  901.     return error;
  902. }
  903. /* pcibr_alloc_all_rrbs allocates all the rrbs available in the quantities
  904.  * requested for each of the devies.  The evn_odd argument indicates whether
  905.  * allcoation for the odd or even rrbs is requested and next group of four pairse
  906.  * are the amount to assign to each device (they should sum to <= 8) and
  907.  * whether to set the viritual bit for that device (1 indictaes yes, 0 indicates no)
  908.  * the devices in order are either 0, 2, 4, 6 or 1, 3, 5, 7
  909.  * if even_odd is even we alloc even rrbs else we allocate odd rrbs
  910.  * returns 0 if no errors else returns -1
  911.  */
  912. int
  913. pcibr_alloc_all_rrbs(devfs_handle_t vhdl, int even_odd,
  914.      int dev_1_rrbs, int virt1, int dev_2_rrbs, int virt2,
  915.      int dev_3_rrbs, int virt3, int dev_4_rrbs, int virt4)
  916. {
  917.     devfs_handle_t            pcibr_vhdl;
  918.     pcibr_soft_t            pcibr_soft = NULL;
  919.     bridge_t               *bridge = NULL;
  920.     uint32_t              rrb_setting = 0;
  921.     int                     rrb_shift = 7;
  922.     uint32_t              cur_rrb;
  923.     int                     dev_rrbs[4];
  924.     int                     virt[4];
  925.     int                     i, j;
  926.     unsigned long           s;
  927.     if (GRAPH_SUCCESS ==
  928. hwgraph_traverse(vhdl, EDGE_LBL_PCI, &pcibr_vhdl)) {
  929. pcibr_soft = pcibr_soft_get(pcibr_vhdl);
  930. if (pcibr_soft)
  931.     bridge = pcibr_soft->bs_base;
  932. hwgraph_vertex_unref(pcibr_vhdl);
  933.     }
  934.     if (bridge == NULL)
  935. bridge = (bridge_t *) xtalk_piotrans_addr
  936.     (vhdl, NULL, 0, sizeof(bridge_t), 0);
  937.     even_odd &= 1;
  938.     dev_rrbs[0] = dev_1_rrbs;
  939.     dev_rrbs[1] = dev_2_rrbs;
  940.     dev_rrbs[2] = dev_3_rrbs;
  941.     dev_rrbs[3] = dev_4_rrbs;
  942.     virt[0] = virt1;
  943.     virt[1] = virt2;
  944.     virt[2] = virt3;
  945.     virt[3] = virt4;
  946.     if ((dev_1_rrbs + dev_2_rrbs + dev_3_rrbs + dev_4_rrbs) > 8) {
  947. return -1;
  948.     }
  949.     if ((dev_1_rrbs < 0) || (dev_2_rrbs < 0) || (dev_3_rrbs < 0) || (dev_4_rrbs < 0)) {
  950. return -1;
  951.     }
  952.     /* walk through rrbs */
  953.     for (i = 0; i < 4; i++) {
  954. if (virt[i]) {
  955.     cur_rrb = i | 0xc;
  956.     cur_rrb = cur_rrb << (rrb_shift * 4);
  957.     rrb_shift--;
  958.     rrb_setting = rrb_setting | cur_rrb;
  959.     dev_rrbs[i] = dev_rrbs[i] - 1;
  960. }
  961. for (j = 0; j < dev_rrbs[i]; j++) {
  962.     cur_rrb = i | 0x8;
  963.     cur_rrb = cur_rrb << (rrb_shift * 4);
  964.     rrb_shift--;
  965.     rrb_setting = rrb_setting | cur_rrb;
  966. }
  967.     }
  968.     if (pcibr_soft)
  969. s = pcibr_lock(pcibr_soft);
  970.     bridge->b_rrb_map[even_odd].reg = rrb_setting;
  971.     if (pcibr_soft) {
  972. pcibr_soft->bs_rrb_fixed |= 0x55 << even_odd;
  973. /* since we've "FIXED" the allocations
  974.  * for these slots, we probably can dispense
  975.  * with tracking avail/res/valid data, but
  976.  * keeping it up to date helps debugging.
  977.  */
  978. pcibr_soft->bs_rrb_avail[even_odd] =
  979.     8 - (dev_1_rrbs + dev_2_rrbs + dev_3_rrbs + dev_4_rrbs);
  980. pcibr_soft->bs_rrb_res[even_odd + 0] = 0;
  981. pcibr_soft->bs_rrb_res[even_odd + 2] = 0;
  982. pcibr_soft->bs_rrb_res[even_odd + 4] = 0;
  983. pcibr_soft->bs_rrb_res[even_odd + 6] = 0;
  984. pcibr_soft->bs_rrb_valid[even_odd + 0] = dev_1_rrbs - virt1;
  985. pcibr_soft->bs_rrb_valid[even_odd + 2] = dev_2_rrbs - virt2;
  986. pcibr_soft->bs_rrb_valid[even_odd + 4] = dev_3_rrbs - virt3;
  987. pcibr_soft->bs_rrb_valid[even_odd + 6] = dev_4_rrbs - virt4;
  988. pcibr_soft->bs_rrb_valid[even_odd + 0 + PCIBR_RRB_SLOT_VIRTUAL] = virt1;
  989. pcibr_soft->bs_rrb_valid[even_odd + 2 + PCIBR_RRB_SLOT_VIRTUAL] = virt2;
  990. pcibr_soft->bs_rrb_valid[even_odd + 4 + PCIBR_RRB_SLOT_VIRTUAL] = virt3;
  991. pcibr_soft->bs_rrb_valid[even_odd + 6 + PCIBR_RRB_SLOT_VIRTUAL] = virt4;
  992. pcibr_unlock(pcibr_soft, s);
  993.     }
  994.     return 0;
  995. }
  996. /*
  997.  *    pcibr_rrb_flush: chase down all the RRBs assigned
  998.  *      to the specified connection point, and flush
  999.  *      them.
  1000.  */
  1001. void
  1002. pcibr_rrb_flush(devfs_handle_t pconn_vhdl)
  1003. {
  1004.     pciio_info_t            pciio_info = pciio_info_get(pconn_vhdl);
  1005.     pcibr_soft_t            pcibr_soft = (pcibr_soft_t) pciio_info_mfast_get(pciio_info);
  1006.     pciio_slot_t            pciio_slot = pciio_info_slot_get(pciio_info);
  1007.     bridge_t               *bridge = pcibr_soft->bs_base;
  1008.     unsigned long           s;
  1009.     reg_p                   rrbp;
  1010.     unsigned                rrbm;
  1011.     int                     i;
  1012.     int                     rrbn;
  1013.     unsigned                sval;
  1014.     unsigned                mask;
  1015.     sval = BRIDGE_RRB_EN | (pciio_slot >> 1);
  1016.     mask = BRIDGE_RRB_EN | BRIDGE_RRB_PDEV;
  1017.     rrbn = pciio_slot & 1;
  1018.     rrbp = &bridge->b_rrb_map[rrbn].reg;
  1019.     s = pcibr_lock(pcibr_soft);
  1020.     rrbm = *rrbp;
  1021.     for (i = 0; i < 8; ++i) {
  1022. if ((rrbm & mask) == sval)
  1023.     do_pcibr_rrb_flush(bridge, rrbn);
  1024. rrbm >>= 4;
  1025. rrbn += 2;
  1026.     }
  1027.     pcibr_unlock(pcibr_soft, s);
  1028. }
  1029. /* =====================================================================
  1030.  *    Device(x) register management
  1031.  */
  1032. /* pcibr_try_set_device: attempt to modify Device(x)
  1033.  * for the specified slot on the specified bridge
  1034.  * as requested in flags, limited to the specified
  1035.  * bits. Returns which BRIDGE bits were in conflict,
  1036.  * or ZERO if everything went OK.
  1037.  *
  1038.  * Caller MUST hold pcibr_lock when calling this function.
  1039.  */
  1040. LOCAL int
  1041. pcibr_try_set_device(pcibr_soft_t pcibr_soft,
  1042.      pciio_slot_t slot,
  1043.      unsigned flags,
  1044.      bridgereg_t mask)
  1045. {
  1046.     bridge_t               *bridge;
  1047.     pcibr_soft_slot_t       slotp;
  1048.     bridgereg_t             old;
  1049.     bridgereg_t             new;
  1050.     bridgereg_t             chg;
  1051.     bridgereg_t             bad;
  1052.     bridgereg_t             badpmu;
  1053.     bridgereg_t             badd32;
  1054.     bridgereg_t             badd64;
  1055.     bridgereg_t             fix;
  1056.     unsigned long           s;
  1057.     bridgereg_t             xmask;
  1058.     xmask = mask;
  1059.     if (pcibr_soft->bs_xbridge) {
  1060.      if (mask == BRIDGE_DEV_PMU_BITS)
  1061. xmask = XBRIDGE_DEV_PMU_BITS;
  1062. if (mask == BRIDGE_DEV_D64_BITS)
  1063. xmask = XBRIDGE_DEV_D64_BITS;
  1064.     }
  1065.     slotp = &pcibr_soft->bs_slot[slot];
  1066.     s = pcibr_lock(pcibr_soft);
  1067.     bridge = pcibr_soft->bs_base;
  1068.     old = slotp->bss_device;
  1069.     /* figure out what the desired
  1070.      * Device(x) bits are based on
  1071.      * the flags specified.
  1072.      */
  1073.     new = old;
  1074.     /* Currently, we inherit anything that
  1075.      * the new caller has not specified in
  1076.      * one way or another, unless we take
  1077.      * action here to not inherit.
  1078.      *
  1079.      * This is needed for the "swap" stuff,
  1080.      * since it could have been set via
  1081.      * pcibr_endian_set -- altho note that
  1082.      * any explicit PCIBR_BYTE_STREAM or
  1083.      * PCIBR_WORD_VALUES will freely override
  1084.      * the effect of that call (and vice
  1085.      * versa, no protection either way).
  1086.      *
  1087.      * I want to get rid of pcibr_endian_set
  1088.      * in favor of tracking DMA endianness
  1089.      * using the flags specified when DMA
  1090.      * channels are created.
  1091.      */
  1092. #define BRIDGE_DEV_WRGA_BITS (BRIDGE_DEV_PMU_WRGA_EN | BRIDGE_DEV_DIR_WRGA_EN)
  1093. #define BRIDGE_DEV_SWAP_BITS (BRIDGE_DEV_SWAP_PMU | BRIDGE_DEV_SWAP_DIR)
  1094.     /* Do not use Barrier, Write Gather,
  1095.      * or Prefetch unless asked.
  1096.      * Leave everything else as it
  1097.      * was from the last time.
  1098.      */
  1099.     new = new
  1100. & ~BRIDGE_DEV_BARRIER
  1101. & ~BRIDGE_DEV_WRGA_BITS
  1102. & ~BRIDGE_DEV_PREF
  1103. ;
  1104.     /* Generic macro flags
  1105.      */
  1106.     if (flags & PCIIO_DMA_DATA) {
  1107. new = (new
  1108.             & ~BRIDGE_DEV_BARRIER)      /* barrier off */
  1109.             | BRIDGE_DEV_PREF;          /* prefetch on */
  1110.     }
  1111.     if (flags & PCIIO_DMA_CMD) {
  1112.         new = ((new
  1113.             & ~BRIDGE_DEV_PREF)         /* prefetch off */
  1114.             & ~BRIDGE_DEV_WRGA_BITS)    /* write gather off */
  1115.             | BRIDGE_DEV_BARRIER;       /* barrier on */
  1116.     }
  1117.     /* Generic detail flags
  1118.      */
  1119.     if (flags & PCIIO_WRITE_GATHER)
  1120. new |= BRIDGE_DEV_WRGA_BITS;
  1121.     if (flags & PCIIO_NOWRITE_GATHER)
  1122. new &= ~BRIDGE_DEV_WRGA_BITS;
  1123.     if (flags & PCIIO_PREFETCH)
  1124. new |= BRIDGE_DEV_PREF;
  1125.     if (flags & PCIIO_NOPREFETCH)
  1126. new &= ~BRIDGE_DEV_PREF;
  1127.     if (flags & PCIBR_WRITE_GATHER)
  1128. new |= BRIDGE_DEV_WRGA_BITS;
  1129.     if (flags & PCIBR_NOWRITE_GATHER)
  1130. new &= ~BRIDGE_DEV_WRGA_BITS;
  1131.     if (flags & PCIIO_BYTE_STREAM)
  1132. new |= (pcibr_soft->bs_xbridge) ? 
  1133. BRIDGE_DEV_SWAP_DIR : BRIDGE_DEV_SWAP_BITS;
  1134.     if (flags & PCIIO_WORD_VALUES)
  1135. new &= (pcibr_soft->bs_xbridge) ? 
  1136. ~BRIDGE_DEV_SWAP_DIR : ~BRIDGE_DEV_SWAP_BITS;
  1137.     /* Provider-specific flags
  1138.      */
  1139.     if (flags & PCIBR_PREFETCH)
  1140. new |= BRIDGE_DEV_PREF;
  1141.     if (flags & PCIBR_NOPREFETCH)
  1142. new &= ~BRIDGE_DEV_PREF;
  1143.     if (flags & PCIBR_PRECISE)
  1144. new |= BRIDGE_DEV_PRECISE;
  1145.     if (flags & PCIBR_NOPRECISE)
  1146. new &= ~BRIDGE_DEV_PRECISE;
  1147.     if (flags & PCIBR_BARRIER)
  1148. new |= BRIDGE_DEV_BARRIER;
  1149.     if (flags & PCIBR_NOBARRIER)
  1150. new &= ~BRIDGE_DEV_BARRIER;
  1151.     if (flags & PCIBR_64BIT)
  1152. new |= BRIDGE_DEV_DEV_SIZE;
  1153.     if (flags & PCIBR_NO64BIT)
  1154. new &= ~BRIDGE_DEV_DEV_SIZE;
  1155.     chg = old ^ new; /* what are we changing, */
  1156.     chg &= xmask; /* of the interesting bits */
  1157.     if (chg) {
  1158. badd32 = slotp->bss_d32_uctr ? (BRIDGE_DEV_D32_BITS & chg) : 0;
  1159. if (pcibr_soft->bs_xbridge) {
  1160. badpmu = slotp->bss_pmu_uctr ? (XBRIDGE_DEV_PMU_BITS & chg) : 0;
  1161. badd64 = slotp->bss_d64_uctr ? (XBRIDGE_DEV_D64_BITS & chg) : 0;
  1162. } else {
  1163. badpmu = slotp->bss_pmu_uctr ? (BRIDGE_DEV_PMU_BITS & chg) : 0;
  1164. badd64 = slotp->bss_d64_uctr ? (BRIDGE_DEV_D64_BITS & chg) : 0;
  1165. }
  1166. bad = badpmu | badd32 | badd64;
  1167. if (bad) {
  1168.     /* some conflicts can be resolved by
  1169.      * forcing the bit on. this may cause
  1170.      * some performance degredation in
  1171.      * the stream(s) that want the bit off,
  1172.      * but the alternative is not allowing
  1173.      * the new stream at all.
  1174.      */
  1175.             if ( (fix = bad & (BRIDGE_DEV_PRECISE |
  1176.                              BRIDGE_DEV_BARRIER)) ){
  1177. bad &= ~fix;
  1178. /* don't change these bits if
  1179.  * they are already set in "old"
  1180.  */
  1181. chg &= ~(fix & old);
  1182.     }
  1183.     /* some conflicts can be resolved by
  1184.      * forcing the bit off. this may cause
  1185.      * some performance degredation in
  1186.      * the stream(s) that want the bit on,
  1187.      * but the alternative is not allowing
  1188.      * the new stream at all.
  1189.      */
  1190.     if ( (fix = bad & (BRIDGE_DEV_WRGA_BITS |
  1191.      BRIDGE_DEV_PREF)) ) {
  1192. bad &= ~fix;
  1193. /* don't change these bits if
  1194.  * we wanted to turn them on.
  1195.  */
  1196. chg &= ~(fix & new);
  1197.     }
  1198.     /* conflicts in other bits mean
  1199.      * we can not establish this DMA
  1200.      * channel while the other(s) are
  1201.      * still present.
  1202.      */
  1203.     if (bad) {
  1204. pcibr_unlock(pcibr_soft, s);
  1205. #if (DEBUG && PCIBR_DEV_DEBUG)
  1206. printk("pcibr_try_set_device: mod blocked by %Rn", bad, device_bits);
  1207. #endif
  1208. return bad;
  1209.     }
  1210. }
  1211.     }
  1212.     if (mask == BRIDGE_DEV_PMU_BITS)
  1213. slotp->bss_pmu_uctr++;
  1214.     if (mask == BRIDGE_DEV_D32_BITS)
  1215. slotp->bss_d32_uctr++;
  1216.     if (mask == BRIDGE_DEV_D64_BITS)
  1217. slotp->bss_d64_uctr++;
  1218.     /* the value we want to write is the
  1219.      * original value, with the bits for
  1220.      * our selected changes flipped, and
  1221.      * with any disabled features turned off.
  1222.      */
  1223.     new = old ^ chg; /* only change what we want to change */
  1224.     if (slotp->bss_device == new) {
  1225. pcibr_unlock(pcibr_soft, s);
  1226. return 0;
  1227.     }
  1228.     bridge->b_device[slot].reg = new;
  1229.     slotp->bss_device = new;
  1230.     bridge->b_wid_tflush; /* wait until Bridge PIO complete */
  1231.     pcibr_unlock(pcibr_soft, s);
  1232. #if DEBUG && PCIBR_DEV_DEBUG
  1233.     printk("pcibr Device(%d): 0x%pn", slot, bridge->b_device[slot].reg);
  1234. #endif
  1235.     return 0;
  1236. }
  1237. void
  1238. pcibr_release_device(pcibr_soft_t pcibr_soft,
  1239.      pciio_slot_t slot,
  1240.      bridgereg_t mask)
  1241. {
  1242.     pcibr_soft_slot_t       slotp;
  1243.     unsigned long           s;
  1244.     slotp = &pcibr_soft->bs_slot[slot];
  1245.     s = pcibr_lock(pcibr_soft);
  1246.     if (mask == BRIDGE_DEV_PMU_BITS)
  1247. slotp->bss_pmu_uctr--;
  1248.     if (mask == BRIDGE_DEV_D32_BITS)
  1249. slotp->bss_d32_uctr--;
  1250.     if (mask == BRIDGE_DEV_D64_BITS)
  1251. slotp->bss_d64_uctr--;
  1252.     pcibr_unlock(pcibr_soft, s);
  1253. }
  1254. /*
  1255.  * flush write gather buffer for slot
  1256.  */
  1257. LOCAL void
  1258. pcibr_device_write_gather_flush(pcibr_soft_t pcibr_soft,
  1259.               pciio_slot_t slot)
  1260. {
  1261.     bridge_t               *bridge;
  1262.     unsigned long          s;
  1263.     volatile uint32_t     wrf;
  1264.     s = pcibr_lock(pcibr_soft);
  1265.     bridge = pcibr_soft->bs_base;
  1266.     wrf = bridge->b_wr_req_buf[slot].reg;
  1267.     pcibr_unlock(pcibr_soft, s);
  1268. }
  1269. /* =====================================================================
  1270.  *    Bridge (pcibr) "Device Driver" entry points
  1271.  */
  1272. /*
  1273.  * pcibr_probe_slot: read a config space word
  1274.  * while trapping any errors; reutrn zero if
  1275.  * all went OK, or nonzero if there was an error.
  1276.  * The value read, if any, is passed back
  1277.  * through the valp parameter.
  1278.  */
  1279. LOCAL int
  1280. pcibr_probe_slot(bridge_t *bridge,
  1281.  cfg_p cfg,
  1282.  unsigned *valp)
  1283. {
  1284.     int                     rv;
  1285.     bridgereg_t             old_enable, new_enable;
  1286.     int badaddr_val(volatile void *, int, volatile void *);
  1287.     old_enable = bridge->b_int_enable;
  1288.     new_enable = old_enable & ~BRIDGE_IMR_PCI_MST_TIMEOUT;
  1289.     bridge->b_int_enable = new_enable;
  1290. /*
  1291.  * The xbridge doesn't clear b_err_int_view unless
  1292.  * multi-err is cleared...
  1293.  */
  1294. if (is_xbridge(bridge))
  1295.     if (bridge->b_err_int_view & BRIDGE_ISR_PCI_MST_TIMEOUT) {
  1296. bridge->b_int_rst_stat = BRIDGE_IRR_MULTI_CLR;
  1297.     }
  1298.     if (bridge->b_int_status & BRIDGE_IRR_PCI_GRP) {
  1299. bridge->b_int_rst_stat = BRIDGE_IRR_PCI_GRP_CLR;
  1300. (void) bridge->b_wid_tflush; /* flushbus */
  1301.     }
  1302.     rv = badaddr_val((void *) cfg, 4, valp);
  1303. /*
  1304.  * The xbridge doesn't set master timeout in b_int_status
  1305.  * here.  Fortunately it's in error_interrupt_view.
  1306.  */
  1307. if (is_xbridge(bridge))
  1308.     if (bridge->b_err_int_view & BRIDGE_ISR_PCI_MST_TIMEOUT) {
  1309. bridge->b_int_rst_stat = BRIDGE_IRR_MULTI_CLR;
  1310. rv = 1; /* unoccupied slot */
  1311.     }
  1312.     bridge->b_int_enable = old_enable;
  1313.     bridge->b_wid_tflush; /* wait until Bridge PIO complete */
  1314.     return rv;
  1315. }
  1316. /*
  1317.  *    pcibr_init: called once during system startup or
  1318.  *      when a loadable driver is loaded.
  1319.  *
  1320.  *      The driver_register function should normally
  1321.  *      be in _reg, not _init.  But the pcibr driver is
  1322.  *      required by devinit before the _reg routines
  1323.  *      are called, so this is an exception.
  1324.  */
  1325. void
  1326. pcibr_init(void)
  1327. {
  1328. #if DEBUG && ATTACH_DEBUG
  1329.     printk("pcibr_initn");
  1330. #endif
  1331.     xwidget_driver_register(XBRIDGE_WIDGET_PART_NUM,
  1332.     XBRIDGE_WIDGET_MFGR_NUM,
  1333.     "pcibr_",
  1334.     0);
  1335.     xwidget_driver_register(BRIDGE_WIDGET_PART_NUM,
  1336.     BRIDGE_WIDGET_MFGR_NUM,
  1337.     "pcibr_",
  1338.     0);
  1339. }
  1340. /*
  1341.  * open/close mmap/munmap interface would be used by processes
  1342.  * that plan to map the PCI bridge, and muck around with the
  1343.  * registers. This is dangerous to do, and will be allowed
  1344.  * to a select brand of programs. Typically these are
  1345.  * diagnostics programs, or some user level commands we may
  1346.  * write to do some weird things.
  1347.  * To start with expect them to have root priveleges.
  1348.  * We will ask for more later.
  1349.  */
  1350. /* ARGSUSED */
  1351. int
  1352. pcibr_open(devfs_handle_t *devp, int oflag, int otyp, cred_t *credp)
  1353. {
  1354.     return 0;
  1355. }
  1356. /*ARGSUSED */
  1357. int
  1358. pcibr_close(devfs_handle_t dev, int oflag, int otyp, cred_t *crp)
  1359. {
  1360.     return 0;
  1361. }
  1362. /*ARGSUSED */
  1363. int
  1364. pcibr_map(devfs_handle_t dev, vhandl_t *vt, off_t off, size_t len, uint prot)
  1365. {
  1366.     int                     error;
  1367.     devfs_handle_t            vhdl = dev_to_vhdl(dev);
  1368.     devfs_handle_t            pcibr_vhdl = hwgraph_connectpt_get(vhdl);
  1369.     pcibr_soft_t            pcibr_soft = pcibr_soft_get(pcibr_vhdl);
  1370.     bridge_t               *bridge = pcibr_soft->bs_base;
  1371.     hwgraph_vertex_unref(pcibr_vhdl);
  1372.     ASSERT(pcibr_soft);
  1373.     len = ctob(btoc(len)); /* Make len page aligned */
  1374.     error = v_mapphys(vt, (void *) ((__psunsigned_t) bridge + off), len);
  1375.     /*
  1376.      * If the offset being mapped corresponds to the flash prom
  1377.      * base, and if the mapping succeeds, and if the user
  1378.      * has requested the protections to be WRITE, enable the
  1379.      * flash prom to be written.
  1380.      *
  1381.      * XXX- deprecate this in favor of using the
  1382.      * real flash driver ...
  1383.      */
  1384.     if (!error &&
  1385. ((off == BRIDGE_EXTERNAL_FLASH) ||
  1386.  (len > BRIDGE_EXTERNAL_FLASH))) {
  1387. int                     s;
  1388. /*
  1389.  * ensure that we write and read without any interruption.
  1390.  * The read following the write is required for the Bridge war
  1391.  */
  1392. s = splhi();
  1393. bridge->b_wid_control |= BRIDGE_CTRL_FLASH_WR_EN;
  1394. bridge->b_wid_control; /* inval addr bug war */
  1395. splx(s);
  1396.     }
  1397.     return error;
  1398. }
  1399. /*ARGSUSED */
  1400. int
  1401. pcibr_unmap(devfs_handle_t dev, vhandl_t *vt)
  1402. {
  1403.     devfs_handle_t            pcibr_vhdl = hwgraph_connectpt_get((devfs_handle_t) dev);
  1404.     pcibr_soft_t            pcibr_soft = pcibr_soft_get(pcibr_vhdl);
  1405.     bridge_t               *bridge = pcibr_soft->bs_base;
  1406.     hwgraph_vertex_unref(pcibr_vhdl);
  1407.     /*
  1408.      * If flashprom write was enabled, disable it, as
  1409.      * this is the last unmap.
  1410.      */
  1411.     if (bridge->b_wid_control & BRIDGE_CTRL_FLASH_WR_EN) {
  1412. int                     s;
  1413. /*
  1414.  * ensure that we write and read without any interruption.
  1415.  * The read following the write is required for the Bridge war
  1416.  */
  1417. s = splhi();
  1418. bridge->b_wid_control &= ~BRIDGE_CTRL_FLASH_WR_EN;
  1419. bridge->b_wid_control; /* inval addr bug war */
  1420. splx(s);
  1421.     }
  1422.     return 0;
  1423. }
  1424. /* This is special case code used by grio. There are plans to make
  1425.  * this a bit more general in the future, but till then this should
  1426.  * be sufficient.
  1427.  */
  1428. pciio_slot_t
  1429. pcibr_device_slot_get(devfs_handle_t dev_vhdl)
  1430. {
  1431.     char                    devname[MAXDEVNAME];
  1432.     devfs_handle_t            tdev;
  1433.     pciio_info_t            pciio_info;
  1434.     pciio_slot_t            slot = PCIIO_SLOT_NONE;
  1435.     vertex_to_name(dev_vhdl, devname, MAXDEVNAME);
  1436.     /* run back along the canonical path
  1437.      * until we find a PCI connection point.
  1438.      */
  1439.     tdev = hwgraph_connectpt_get(dev_vhdl);
  1440.     while (tdev != GRAPH_VERTEX_NONE) {
  1441. pciio_info = pciio_info_chk(tdev);
  1442. if (pciio_info) {
  1443.     slot = pciio_info_slot_get(pciio_info);
  1444.     break;
  1445. }
  1446. hwgraph_vertex_unref(tdev);
  1447. tdev = hwgraph_connectpt_get(tdev);
  1448.     }
  1449.     hwgraph_vertex_unref(tdev);
  1450.     return slot;
  1451. }
  1452. /*==========================================================================
  1453.  * BRIDGE PCI SLOT RELATED IOCTLs
  1454.  */
  1455. char *pci_space_name[] = {"NONE", 
  1456.   "ROM",
  1457.   "IO",
  1458.   "",
  1459.   "MEM",
  1460.   "MEM32",
  1461.   "MEM64",
  1462.   "CFG",
  1463.   "WIN0",
  1464.   "WIN1",
  1465.   "WIN2",
  1466.   "WIN3",
  1467.   "WIN4",
  1468.   "WIN5",
  1469.   "",
  1470.   "BAD"};
  1471. #ifdef LATER
  1472. void
  1473. pcibr_slot_func_info_return(pcibr_info_h pcibr_infoh,
  1474.                             int func,
  1475.                             pcibr_slot_func_info_resp_t funcp)
  1476. {
  1477.     pcibr_info_t                 pcibr_info = pcibr_infoh[func];
  1478.     int                          win;
  1479.     funcp->resp_f_status = 0;
  1480.     if (!pcibr_info) {
  1481.         return;
  1482.     }
  1483.     funcp->resp_f_status |= FUNC_IS_VALID;
  1484. #ifdef SUPPORT_PRINTING_V_FORMAT
  1485.     sprintf(funcp->resp_f_slot_name, "%v", pcibr_info->f_vertex);
  1486. #else
  1487.     sprintf(funcp->resp_f_slot_name, "%x", pcibr_info->f_vertex);
  1488. #endif
  1489.     if(is_sys_critical_vertex(pcibr_info->f_vertex)) {
  1490.         funcp->resp_f_status |= FUNC_IS_SYS_CRITICAL;
  1491.     }
  1492.     funcp->resp_f_bus = pcibr_info->f_bus;
  1493.     funcp->resp_f_slot = pcibr_info->f_slot;
  1494.     funcp->resp_f_func = pcibr_info->f_func;
  1495. #ifdef SUPPORT_PRINTING_V_FORMAT
  1496.     sprintf(funcp->resp_f_master_name, "%v", pcibr_info->f_master);
  1497. #else
  1498.     sprintf(funcp->resp_f_master_name, "%x", pcibr_info->f_master);
  1499. #endif
  1500.     funcp->resp_f_pops = pcibr_info->f_pops;
  1501.     funcp->resp_f_efunc = pcibr_info->f_efunc;
  1502.     funcp->resp_f_einfo = pcibr_info->f_einfo;
  1503.     funcp->resp_f_vendor = pcibr_info->f_vendor;
  1504.     funcp->resp_f_device = pcibr_info->f_device;
  1505.     for(win = 0 ; win < 6 ; win++) {
  1506.         funcp->resp_f_window[win].resp_w_base =
  1507.                                   pcibr_info->f_window[win].w_base;
  1508.         funcp->resp_f_window[win].resp_w_size =
  1509.                                   pcibr_info->f_window[win].w_size;
  1510.         sprintf(funcp->resp_f_window[win].resp_w_space,
  1511.                 "%s",
  1512.                 pci_space_name[pcibr_info->f_window[win].w_space]);
  1513.     }
  1514.     funcp->resp_f_rbase = pcibr_info->f_rbase;
  1515.     funcp->resp_f_rsize = pcibr_info->f_rsize;
  1516.     for (win = 0 ; win < 4; win++) {
  1517.         funcp->resp_f_ibit[win] = pcibr_info->f_ibit[win];
  1518.     }
  1519.     funcp->resp_f_att_det_error = pcibr_info->f_att_det_error;
  1520. }
  1521. int
  1522. pcibr_slot_info_return(pcibr_soft_t             pcibr_soft,
  1523.                        pciio_slot_t             slot,
  1524.                        pcibr_slot_info_resp_t   respp)
  1525. {
  1526.     pcibr_soft_slot_t            pss;
  1527.     int                          func;
  1528.     bridge_t                    *bridge = pcibr_soft->bs_base;
  1529.     reg_p                        b_respp;
  1530.     pcibr_slot_info_resp_t       slotp;
  1531.     pcibr_slot_func_info_resp_t  funcp;
  1532.     slotp = kmem_zalloc(sizeof(*slotp), KM_SLEEP);
  1533.     if (slotp == NULL) {
  1534.         return(ENOMEM);
  1535.     }
  1536.     pss = &pcibr_soft->bs_slot[slot];
  1537.     
  1538.     printk("nPCI INFRASTRUCTURAL INFO FOR SLOT %dnn", slot);
  1539.     slotp->resp_has_host = pss->has_host;
  1540.     slotp->resp_host_slot = pss->host_slot;
  1541. #ifdef SUPPORT_PRINTING_V_FORMAT
  1542.     sprintf(slotp->resp_slot_conn_name, "%v", pss->slot_conn);
  1543. #else
  1544.     sprintf(slotp->resp_slot_conn_name, "%x", pss->slot_conn);
  1545. #endif
  1546.     slotp->resp_slot_status = pss->slot_status;
  1547.     slotp->resp_l1_bus_num = io_path_map_widget(pcibr_soft->bs_vhdl);
  1548.     if (is_sys_critical_vertex(pss->slot_conn)) {
  1549.         slotp->resp_slot_status |= SLOT_IS_SYS_CRITICAL;
  1550.     }
  1551.     slotp->resp_bss_ninfo = pss->bss_ninfo;
  1552.     for (func = 0; func < pss->bss_ninfo; func++) {
  1553.         funcp = &(slotp->resp_func[func]);
  1554.         pcibr_slot_func_info_return(pss->bss_infos, func, funcp);
  1555.     }
  1556.     sprintf(slotp->resp_bss_devio_bssd_space, "%s",
  1557.             pci_space_name[pss->bss_devio.bssd_space]);
  1558.     slotp->resp_bss_devio_bssd_base = pss->bss_devio.bssd_base;
  1559.     slotp->resp_bss_device = pss->bss_device;
  1560.     slotp->resp_bss_pmu_uctr = pss->bss_pmu_uctr;
  1561.     slotp->resp_bss_d32_uctr = pss->bss_d32_uctr;
  1562.     slotp->resp_bss_d64_uctr = pss->bss_d64_uctr;
  1563.     slotp->resp_bss_d64_base = pss->bss_d64_base;
  1564.     slotp->resp_bss_d64_flags = pss->bss_d64_flags;
  1565.     slotp->resp_bss_d32_base = pss->bss_d32_base;
  1566.     slotp->resp_bss_d32_flags = pss->bss_d32_flags;
  1567.     slotp->resp_bss_ext_ates_active = atomic_read(&pss->bss_ext_ates_active);
  1568.     slotp->resp_bss_cmd_pointer = pss->bss_cmd_pointer;
  1569.     slotp->resp_bss_cmd_shadow = pss->bss_cmd_shadow;
  1570.     slotp->resp_bs_rrb_valid = pcibr_soft->bs_rrb_valid[slot];
  1571.     slotp->resp_bs_rrb_valid_v = pcibr_soft->bs_rrb_valid[slot +
  1572.                                                       PCIBR_RRB_SLOT_VIRTUAL];
  1573.     slotp->resp_bs_rrb_res = pcibr_soft->bs_rrb_res[slot];
  1574.     if (slot & 1) {
  1575.         b_respp = &bridge->b_odd_resp;
  1576.     } else {
  1577.         b_respp = &bridge->b_even_resp;
  1578.     }
  1579.     slotp->resp_b_resp = *b_respp;
  1580.     slotp->resp_b_int_device = bridge->b_int_device;
  1581.     slotp->resp_b_int_enable = bridge->b_int_enable;
  1582.     slotp->resp_b_int_host = bridge->b_int_addr[slot].addr;
  1583.     if (COPYOUT(slotp, respp, sizeof(*respp))) {
  1584.         return(EFAULT);
  1585.     }
  1586.     kmem_free(slotp, sizeof(*slotp));
  1587.     return(0);
  1588. }
  1589. /*
  1590.  * pcibr_slot_query
  1591.  * Return information about the PCI slot maintained by the infrastructure.
  1592.  * Information is requested in the request structure.
  1593.  *
  1594.  *      Information returned in the response structure:
  1595.  * Slot hwgraph name
  1596.  * Vendor/Device info
  1597.  * Base register info
  1598.  * Interrupt mapping from device pins to the bridge pins
  1599.  * Devio register
  1600.  * Software RRB info
  1601.  * RRB register info
  1602.  * Host/Gues info
  1603.  * PCI Bus #,slot #, function #
  1604.  * Slot provider hwgraph name
  1605.  * Provider Functions
  1606.  * Error handler
  1607.  * DMA mapping usage counters
  1608.  * DMA direct translation info
  1609.  * External SSRAM workaround info
  1610.  */
  1611. int
  1612. pcibr_slot_query(devfs_handle_t pcibr_vhdl, pcibr_slot_info_req_t reqp)
  1613. {
  1614.     pcibr_soft_t            pcibr_soft = pcibr_soft_get(pcibr_vhdl);
  1615.     pciio_slot_t            slot = reqp->req_slot;
  1616.     pciio_slot_t            tmp_slot;
  1617.     pcibr_slot_info_resp_t  respp = (pcibr_slot_info_resp_t) reqp->req_respp;
  1618.     int                     size = reqp->req_size;
  1619.     int                     error;
  1620.     /* Make sure that we are dealing with a bridge device vertex */
  1621.     if (!pcibr_soft) {
  1622.         return(EINVAL);
  1623.     }
  1624.     /* Make sure that we have a valid PCI slot number or PCIIO_SLOT_NONE */
  1625.     if ((!PCIBR_VALID_SLOT(slot)) && (slot != PCIIO_SLOT_NONE)) {
  1626.         return(EINVAL);
  1627.     }
  1628. #ifdef LATER
  1629.     /* Do not allow a query of a slot in a shoehorn */
  1630.     if(nic_vertex_info_match(pcibr_soft->bs_conn, XTALK_PCI_PART_NUM)) {
  1631.        return(EPERM);
  1632.     }
  1633. #endif
  1634.     /* Return information for the requested PCI slot */
  1635.     if (slot != PCIIO_SLOT_NONE) {
  1636.         if (size < sizeof(*respp)) {
  1637.             return(EINVAL);
  1638.         }
  1639.         /* Acquire read access to the slot */
  1640.         mrlock(pcibr_soft->bs_slot[slot].slot_lock, MR_ACCESS, PZERO);
  1641.         error = pcibr_slot_info_return(pcibr_soft, slot, respp);
  1642.         /* Release the slot lock */
  1643.         mrunlock(pcibr_soft->bs_slot[slot].slot_lock);
  1644.         return(error);
  1645.     }
  1646.     /* Return information for all the slots */
  1647.     for (tmp_slot = 0; tmp_slot < 8; tmp_slot++) {
  1648.         if (size < sizeof(*respp)) {
  1649.             return(EINVAL);
  1650.         }
  1651.         /* Acquire read access to the slot */
  1652.         mrlock(pcibr_soft->bs_slot[tmp_slot].slot_lock, MR_ACCESS, PZERO);
  1653.         error = pcibr_slot_info_return(pcibr_soft, tmp_slot, respp);
  1654.         /* Release the slot lock */
  1655.         mrunlock(pcibr_soft->bs_slot[tmp_slot].slot_lock);
  1656.         if (error) {
  1657.             return(error);
  1658.         }
  1659.         ++respp;
  1660.         size -= sizeof(*respp);
  1661.     }
  1662.     return(error);
  1663. }
  1664. #endif /* LATER */
  1665. /*ARGSUSED */
  1666. int
  1667. pcibr_ioctl(devfs_handle_t dev,
  1668.     int cmd,
  1669.     void *arg,
  1670.     int flag,
  1671.     struct cred *cr,
  1672.     int *rvalp)
  1673. {
  1674.     devfs_handle_t            pcibr_vhdl = hwgraph_connectpt_get((devfs_handle_t)dev);
  1675. #ifdef LATER
  1676.     pcibr_soft_t            pcibr_soft = pcibr_soft_get(pcibr_vhdl);
  1677. #endif
  1678.     int                     error = 0;
  1679.     hwgraph_vertex_unref(pcibr_vhdl);
  1680.     switch (cmd) {
  1681. #ifdef LATER
  1682.     case GIOCSETBW:
  1683. {
  1684.     grio_ioctl_info_t       info;
  1685.     pciio_slot_t            slot = 0;
  1686.     if (!cap_able((uint64_t)CAP_DEVICE_MGT)) {
  1687. error = EPERM;
  1688. break;
  1689.     }
  1690.     if (COPYIN(arg, &info, sizeof(grio_ioctl_info_t))) {
  1691. error = EFAULT;
  1692. break;
  1693.     }
  1694. #ifdef GRIO_DEBUG
  1695.     printk("pcibr:: prev_vhdl: %d reqbw: %lldn",
  1696.     info.prev_vhdl, info.reqbw);
  1697. #endif /* GRIO_DEBUG */
  1698.     if ((slot = pcibr_device_slot_get(info.prev_vhdl)) ==
  1699. PCIIO_SLOT_NONE) {
  1700. error = EIO;
  1701. break;
  1702.     }
  1703.     if (info.reqbw)
  1704. pcibr_priority_bits_set(pcibr_soft, slot, PCI_PRIO_HIGH);
  1705.     break;
  1706. }
  1707.     case GIOCRELEASEBW:
  1708. {
  1709.     grio_ioctl_info_t       info;
  1710.     pciio_slot_t            slot = 0;
  1711.     if (!cap_able(CAP_DEVICE_MGT)) {
  1712. error = EPERM;
  1713. break;
  1714.     }
  1715.     if (COPYIN(arg, &info, sizeof(grio_ioctl_info_t))) {
  1716. error = EFAULT;
  1717. break;
  1718.     }
  1719. #ifdef GRIO_DEBUG
  1720.     printk("pcibr:: prev_vhdl: %d reqbw: %lldn",
  1721.     info.prev_vhdl, info.reqbw);
  1722. #endif /* GRIO_DEBUG */
  1723.     if ((slot = pcibr_device_slot_get(info.prev_vhdl)) ==
  1724. PCIIO_SLOT_NONE) {
  1725. error = EIO;
  1726. break;
  1727.     }
  1728.     if (info.reqbw)
  1729. pcibr_priority_bits_set(pcibr_soft, slot, PCI_PRIO_LOW);
  1730.     break;
  1731. }
  1732.     case PCIBR_SLOT_POWERUP:
  1733. {
  1734.     pciio_slot_t slot;
  1735.     if (!cap_able(CAP_DEVICE_MGT)) {
  1736. error = EPERM;
  1737. break;
  1738.     }
  1739.     slot = (pciio_slot_t)(uint64_t)arg;
  1740.     error = pcibr_slot_powerup(pcibr_vhdl,slot);
  1741.     break;
  1742. }
  1743.     case PCIBR_SLOT_SHUTDOWN:
  1744.     if (!cap_able(CAP_DEVICE_MGT)) {
  1745. error = EPERM;
  1746. break;
  1747.     }
  1748.     slot = (pciio_slot_t)(uint64_t)arg;
  1749.     error = pcibr_slot_powerup(pcibr_vhdl,slot);
  1750.     break;
  1751. }
  1752.     case PCIBR_SLOT_QUERY:
  1753. {
  1754.     struct pcibr_slot_info_req_s        req;
  1755.     if (!cap_able(CAP_DEVICE_MGT)) {
  1756. error = EPERM;
  1757. break;
  1758.     }
  1759.             if (COPYIN(arg, &req, sizeof(req))) {
  1760.                 error = EFAULT;
  1761.                 break;
  1762.             }
  1763.             error = pcibr_slot_query(pcibr_vhdl, &req);
  1764.     break;
  1765. }
  1766. #endif /* LATER */
  1767.     default:
  1768. break;
  1769.     }
  1770.     return error;
  1771. }
  1772. void
  1773. pcibr_freeblock_sub(iopaddr_t *free_basep,
  1774.     iopaddr_t *free_lastp,
  1775.     iopaddr_t base,
  1776.     size_t size)
  1777. {
  1778.     iopaddr_t               free_base = *free_basep;
  1779.     iopaddr_t               free_last = *free_lastp;
  1780.     iopaddr_t               last = base + size - 1;
  1781.     if ((last < free_base) || (base > free_last)); /* free block outside arena */
  1782.     else if ((base <= free_base) && (last >= free_last))
  1783. /* free block contains entire arena */
  1784. *free_basep = *free_lastp = 0;
  1785.     else if (base <= free_base)
  1786. /* free block is head of arena */
  1787. *free_basep = last + 1;
  1788.     else if (last >= free_last)
  1789. /* free block is tail of arena */
  1790. *free_lastp = base - 1;
  1791.     /*
  1792.      * We are left with two regions: the free area
  1793.      * in the arena "below" the block, and the free
  1794.      * area in the arena "above" the block. Keep
  1795.      * the one that is bigger.
  1796.      */
  1797.     else if ((base - free_base) > (free_last - last))
  1798. *free_lastp = base - 1; /* keep lower chunk */
  1799.     else
  1800. *free_basep = last + 1; /* keep upper chunk */
  1801. }
  1802. /* Convert from ssram_bits in control register to number of SSRAM entries */
  1803. #define ATE_NUM_ENTRIES(n) _ate_info[n]
  1804. /* Possible choices for number of ATE entries in Bridge's SSRAM */
  1805. LOCAL int               _ate_info[] =
  1806. {
  1807.     0, /* 0 entries */
  1808.     8 * 1024, /* 8K entries */
  1809.     16 * 1024, /* 16K entries */
  1810.     64 * 1024 /* 64K entries */
  1811. };
  1812. #define ATE_NUM_SIZES (sizeof(_ate_info) / sizeof(int))
  1813. #define ATE_PROBE_VALUE 0x0123456789abcdefULL
  1814. /*
  1815.  * Determine the size of this bridge's external mapping SSRAM, and set
  1816.  * the control register appropriately to reflect this size, and initialize
  1817.  * the external SSRAM.
  1818.  */
  1819. LOCAL int
  1820. pcibr_init_ext_ate_ram(bridge_t *bridge)
  1821. {
  1822.     int                     largest_working_size = 0;
  1823.     int                     num_entries, entry;
  1824.     int                     i, j;
  1825.     bridgereg_t             old_enable, new_enable;
  1826.     int                     s;
  1827.     /* Probe SSRAM to determine its size. */
  1828.     old_enable = bridge->b_int_enable;
  1829.     new_enable = old_enable & ~BRIDGE_IMR_PCI_MST_TIMEOUT;
  1830.     bridge->b_int_enable = new_enable;
  1831.     for (i = 1; i < ATE_NUM_SIZES; i++) {
  1832. /* Try writing a value */
  1833. bridge->b_ext_ate_ram[ATE_NUM_ENTRIES(i) - 1] = ATE_PROBE_VALUE;
  1834. /* Guard against wrap */
  1835. for (j = 1; j < i; j++)
  1836.     bridge->b_ext_ate_ram[ATE_NUM_ENTRIES(j) - 1] = 0;
  1837. /* See if value was written */
  1838. if (bridge->b_ext_ate_ram[ATE_NUM_ENTRIES(i) - 1] == ATE_PROBE_VALUE)
  1839.     largest_working_size = i;
  1840.     }
  1841.     bridge->b_int_enable = old_enable;
  1842.     bridge->b_wid_tflush; /* wait until Bridge PIO complete */
  1843.     /*
  1844.      * ensure that we write and read without any interruption.
  1845.      * The read following the write is required for the Bridge war
  1846.      */
  1847.     s = splhi();
  1848.     bridge->b_wid_control = (bridge->b_wid_control
  1849. & ~BRIDGE_CTRL_SSRAM_SIZE_MASK)
  1850. | BRIDGE_CTRL_SSRAM_SIZE(largest_working_size);
  1851.     bridge->b_wid_control; /* inval addr bug war */
  1852.     splx(s);
  1853.     num_entries = ATE_NUM_ENTRIES(largest_working_size);
  1854. #if PCIBR_ATE_DEBUG
  1855.     if (num_entries)
  1856. printk("bridge at 0x%x: clearing %d external ATEsn", bridge, num_entries);
  1857.     else
  1858. printk("bridge at 0x%x: no externa9422l ATE RAM foundn", bridge);
  1859. #endif
  1860.     /* Initialize external mapping entries */
  1861.     for (entry = 0; entry < num_entries; entry++)
  1862. bridge->b_ext_ate_ram[entry] = 0;
  1863.     return (num_entries);
  1864. }
  1865. /*
  1866.  * Allocate "count" contiguous Bridge Address Translation Entries
  1867.  * on the specified bridge to be used for PCI to XTALK mappings.
  1868.  * Indices in rm map range from 1..num_entries.  Indicies returned
  1869.  * to caller range from 0..num_entries-1.
  1870.  *
  1871.  * Return the start index on success, -1 on failure.
  1872.  */
  1873. LOCAL int
  1874. pcibr_ate_alloc(pcibr_soft_t pcibr_soft, int count)
  1875. {
  1876.     int                     index = 0;
  1877.     index = (int) rmalloc(pcibr_soft->bs_int_ate_map, (size_t) count);
  1878. /* printk("Colin: pcibr_ate_alloc - index %d count %d n", index, count); */
  1879.     if (!index && pcibr_soft->bs_ext_ate_map)
  1880. index = (int) rmalloc(pcibr_soft->bs_ext_ate_map, (size_t) count);
  1881.     /* rmalloc manages resources in the 1..n
  1882.      * range, with 0 being failure.
  1883.      * pcibr_ate_alloc manages resources
  1884.      * in the 0..n-1 range, with -1 being failure.
  1885.      */
  1886.     return index - 1;
  1887. }
  1888. LOCAL void
  1889. pcibr_ate_free(pcibr_soft_t pcibr_soft, int index, int count)
  1890. /* Who says there's no such thing as a free meal? :-) */
  1891. {
  1892.     /* note the "+1" since rmalloc handles 1..n but
  1893.      * we start counting ATEs at zero.
  1894.      */
  1895. /* printk("Colin: pcibr_ate_free - index %d count %dn", index, count); */
  1896.     rmfree((index < pcibr_soft->bs_int_ate_size)
  1897.    ? pcibr_soft->bs_int_ate_map
  1898.    : pcibr_soft->bs_ext_ate_map,
  1899.    count, index + 1);
  1900. }
  1901. LOCAL pcibr_info_t
  1902. pcibr_info_get(devfs_handle_t vhdl)
  1903. {
  1904.     return (pcibr_info_t) pciio_info_get(vhdl);
  1905. }
  1906. pcibr_info_t
  1907. pcibr_device_info_new(
  1908.  pcibr_soft_t pcibr_soft,
  1909.  pciio_slot_t slot,
  1910.  pciio_function_t rfunc,
  1911.  pciio_vendor_id_t vendor,
  1912.  pciio_device_id_t device)
  1913. {
  1914.     pcibr_info_t            pcibr_info;
  1915.     pciio_function_t        func;
  1916.     int                     ibit;
  1917.     func = (rfunc == PCIIO_FUNC_NONE) ? 0 : rfunc;
  1918.     NEW(pcibr_info);
  1919.     pciio_device_info_new(&pcibr_info->f_c,
  1920.   pcibr_soft->bs_vhdl,
  1921.   slot, rfunc,
  1922.   vendor, device);
  1923.     if (slot != PCIIO_SLOT_NONE) {
  1924. /*
  1925.  * Currently favored mapping from PCI
  1926.  * slot number and INTA/B/C/D to Bridge
  1927.  * PCI Interrupt Bit Number:
  1928.  *
  1929.  *     SLOT     A B C D
  1930.  *      0       0 4 0 4
  1931.  *      1       1 5 1 5
  1932.  *      2       2 6 2 6
  1933.  *      3       3 7 3 7
  1934.  *      4       4 0 4 0
  1935.  *      5       5 1 5 1
  1936.  *      6       6 2 6 2
  1937.  *      7       7 3 7 3
  1938.  *
  1939.  * XXX- allow pcibr_hints to override default
  1940.  * XXX- allow ADMIN to override pcibr_hints
  1941.  */
  1942. for (ibit = 0; ibit < 4; ++ibit)
  1943.     pcibr_info->f_ibit[ibit] =
  1944. (slot + 4 * ibit) & 7;
  1945. /*
  1946.  * Record the info in the sparse func info space.
  1947.  */
  1948. if (func < pcibr_soft->bs_slot[slot].bss_ninfo)
  1949.     pcibr_soft->bs_slot[slot].bss_infos[func] = pcibr_info;
  1950.     }
  1951.     return pcibr_info;
  1952. }
  1953. void
  1954. pcibr_device_info_free(devfs_handle_t pcibr_vhdl, pciio_slot_t slot)
  1955. {
  1956.     pcibr_soft_t pcibr_soft = pcibr_soft_get(pcibr_vhdl);
  1957.     pcibr_info_t pcibr_info;
  1958.     pciio_function_t func;
  1959.     pcibr_soft_slot_t slotp = &pcibr_soft->bs_slot[slot];
  1960.     int nfunc = slotp->bss_ninfo;
  1961.     for (func = 0; func < nfunc; func++) {
  1962. pcibr_info = slotp->bss_infos[func];
  1963. if (!pcibr_info) 
  1964.     continue;
  1965. slotp->bss_infos[func] = 0;
  1966. pciio_device_info_unregister(pcibr_vhdl, &pcibr_info->f_c);
  1967. pciio_device_info_free(&pcibr_info->f_c);
  1968. DEL(pcibr_info);
  1969.     }
  1970.     /* Clear the DEVIO(x) for this slot */
  1971.     slotp->bss_devio.bssd_space = PCIIO_SPACE_NONE;
  1972.     slotp->bss_devio.bssd_base = PCIBR_D32_BASE_UNSET;
  1973.     slotp->bss_device  = 0;
  1974.     
  1975.     /* Reset the mapping usage counters */
  1976.     slotp->bss_pmu_uctr = 0;
  1977.     slotp->bss_d32_uctr = 0;
  1978.     slotp->bss_d64_uctr = 0;
  1979.     /* Clear the Direct translation info */
  1980.     slotp->bss_d64_base = PCIBR_D64_BASE_UNSET;
  1981.     slotp->bss_d64_flags = 0;
  1982.     slotp->bss_d32_base = PCIBR_D32_BASE_UNSET;
  1983.     slotp->bss_d32_flags = 0;
  1984.     /* Clear out shadow info necessary for the external SSRAM workaround */
  1985.     slotp->bss_ext_ates_active = ATOMIC_INIT(0);
  1986.     slotp->bss_cmd_pointer = 0;
  1987.     slotp->bss_cmd_shadow = 0;
  1988. }
  1989. /* 
  1990.  * PCI_ADDR_SPACE_LIMITS_LOAD
  1991.  * Gets the current values of 
  1992.  * pci io base, 
  1993.  * pci io last,
  1994.  * pci low memory base,
  1995.  * pci low memory last,
  1996.  * pci high memory base,
  1997.  *  pci high memory last
  1998.  */
  1999. #define PCI_ADDR_SPACE_LIMITS_LOAD()
  2000.     pci_io_fb = pcibr_soft->bs_spinfo.pci_io_base;
  2001.     pci_io_fl = pcibr_soft->bs_spinfo.pci_io_last;
  2002.     pci_lo_fb = pcibr_soft->bs_spinfo.pci_swin_base;
  2003.     pci_lo_fl = pcibr_soft->bs_spinfo.pci_swin_last;
  2004.     pci_hi_fb = pcibr_soft->bs_spinfo.pci_mem_base;
  2005.     pci_hi_fl = pcibr_soft->bs_spinfo.pci_mem_last;
  2006. /*
  2007.  * PCI_ADDR_SPACE_LIMITS_STORE
  2008.  * Sets the current values of
  2009.  * pci io base, 
  2010.  * pci io last,
  2011.  * pci low memory base,
  2012.  * pci low memory last,
  2013.  * pci high memory base,
  2014.  *  pci high memory last
  2015.  */
  2016. #define PCI_ADDR_SPACE_LIMITS_STORE()
  2017.     pcibr_soft->bs_spinfo.pci_io_base = pci_io_fb;
  2018.     pcibr_soft->bs_spinfo.pci_io_last = pci_io_fl;
  2019.     pcibr_soft->bs_spinfo.pci_swin_base = pci_lo_fb;
  2020.     pcibr_soft->bs_spinfo.pci_swin_last = pci_lo_fl;
  2021.     pcibr_soft->bs_spinfo.pci_mem_base = pci_hi_fb;
  2022.     pcibr_soft->bs_spinfo.pci_mem_last = pci_hi_fl;
  2023. #define PCI_ADDR_SPACE_LIMITS_PRINT()
  2024.     printf("+++++++++++++++++++++++n"
  2025.    "IO base 0x%x last 0x%xn"
  2026.    "SWIN base 0x%x last 0x%xn"
  2027.    "MEM base 0x%x last 0x%xn"
  2028.    "+++++++++++++++++++++++n",
  2029.    pcibr_soft->bs_spinfo.pci_io_base,
  2030.    pcibr_soft->bs_spinfo.pci_io_last,
  2031.    pcibr_soft->bs_spinfo.pci_swin_base,
  2032.    pcibr_soft->bs_spinfo.pci_swin_last,
  2033.    pcibr_soft->bs_spinfo.pci_mem_base,
  2034.    pcibr_soft->bs_spinfo.pci_mem_last);
  2035. /*
  2036.  * pcibr_slot_info_init
  2037.  * Probe for this slot and see if it is populated.
  2038.  * If it is populated initialize the generic PCI infrastructural
  2039.  *  information associated with this particular PCI device.
  2040.  */
  2041. int
  2042. pcibr_slot_info_init(devfs_handle_t  pcibr_vhdl,
  2043.      pciio_slot_t  slot)
  2044. {
  2045.     pcibr_soft_t     pcibr_soft;
  2046.     pcibr_info_h     pcibr_infoh;
  2047.     pcibr_info_t     pcibr_info;
  2048.     bridge_t    *bridge;
  2049.     cfg_p                   cfgw;
  2050.     unsigned                idword;
  2051.     unsigned                pfail;
  2052.     unsigned                idwords[8];
  2053.     pciio_vendor_id_t       vendor;
  2054.     pciio_device_id_t       device;
  2055.     unsigned                htype;
  2056. #if !defined(CONFIG_IA64_SGI_SN1)
  2057.     int     nbars;
  2058. #endif
  2059.     cfg_p                   wptr;
  2060.     int                     win;
  2061.     pciio_space_t           space;
  2062.     iopaddr_t     pci_io_fb, pci_io_fl;
  2063.     iopaddr_t     pci_lo_fb,  pci_lo_fl;
  2064.     iopaddr_t     pci_hi_fb,  pci_hi_fl;
  2065.     int     nfunc;
  2066.     pciio_function_t     rfunc;
  2067.     int     func;
  2068.     devfs_handle_t     conn_vhdl;
  2069.     pcibr_soft_slot_t     slotp;
  2070.     
  2071.     /* Get the basic software information required to proceed */
  2072.     pcibr_soft = pcibr_soft_get(pcibr_vhdl);
  2073.     if (!pcibr_soft)
  2074. return(EINVAL);
  2075.     bridge = pcibr_soft->bs_base;
  2076.     if (!PCIBR_VALID_SLOT(slot))
  2077. return(EINVAL);
  2078.     /* If we have a host slot (eg:- IOC3 has 2 PCI slots and the initialization
  2079.      * is done by the host slot then we are done.
  2080.      */
  2081.     if (pcibr_soft->bs_slot[slot].has_host) {
  2082. return(0);    
  2083.     }
  2084.     /* Check for a slot with any system critical functions */
  2085.     if (pcibr_is_slot_sys_critical(pcibr_vhdl, slot))
  2086.         return(EPERM);
  2087.     /* Load the current values of allocated PCI address spaces */
  2088.     PCI_ADDR_SPACE_LIMITS_LOAD();
  2089.     
  2090.     /* Try to read the device-id/vendor-id from the config space */
  2091.     cfgw = bridge->b_type0_cfg_dev[slot].l;
  2092.     if (pcibr_probe_slot(bridge, cfgw, &idword)) 
  2093. return(ENODEV);
  2094.     slotp = &pcibr_soft->bs_slot[slot];
  2095.     slotp->slot_status |= SLOT_POWER_UP;
  2096.     vendor = 0xFFFF & idword;
  2097.     /* If the vendor id is not valid then the slot is not populated
  2098.      * and we are done.
  2099.      */
  2100.     if (vendor == 0xFFFF) 
  2101. return(ENODEV);
  2102.     
  2103.     device = 0xFFFF & (idword >> 16);
  2104.     htype = do_pcibr_config_get(cfgw, PCI_CFG_HEADER_TYPE, 1);
  2105.     nfunc = 1;
  2106.     rfunc = PCIIO_FUNC_NONE;
  2107.     pfail = 0;
  2108.     /* NOTE: if a card claims to be multifunction
  2109.      * but only responds to config space 0, treat
  2110.      * it as a unifunction card.
  2111.      */
  2112.     if (htype & 0x80) { /* MULTIFUNCTION */
  2113. for (func = 1; func < 8; ++func) {
  2114.     cfgw = bridge->b_type0_cfg_dev[slot].f[func].l;
  2115.     if (pcibr_probe_slot(bridge, cfgw, &idwords[func])) {
  2116. pfail |= 1 << func;
  2117. continue;
  2118.     }
  2119.     vendor = 0xFFFF & idwords[func];
  2120.     if (vendor == 0xFFFF) {
  2121. pfail |= 1 << func;
  2122. continue;
  2123.     }
  2124.     nfunc = func + 1;
  2125.     rfunc = 0;
  2126. }
  2127. cfgw = bridge->b_type0_cfg_dev[slot].l;
  2128.     }
  2129.     NEWA(pcibr_infoh, nfunc);
  2130.     
  2131.     pcibr_soft->bs_slot[slot].bss_ninfo = nfunc;
  2132.     pcibr_soft->bs_slot[slot].bss_infos = pcibr_infoh;
  2133.     for (func = 0; func < nfunc; ++func) {
  2134. unsigned                cmd_reg;
  2135. if (func) {
  2136.     if (pfail & (1 << func))
  2137. continue;
  2138.     
  2139.     idword = idwords[func];
  2140.     cfgw = bridge->b_type0_cfg_dev[slot].f[func].l;
  2141.     
  2142.     device = 0xFFFF & (idword >> 16);
  2143.     htype = do_pcibr_config_get(cfgw, PCI_CFG_HEADER_TYPE, 1);
  2144.     rfunc = func;
  2145. }
  2146. htype &= 0x7f;
  2147. if (htype != 0x00) {
  2148.     PRINT_WARNING("%s pcibr: pci slot %d func %d has strange header type 0x%xn",
  2149.     pcibr_soft->bs_name, slot, func, htype);
  2150. #if defined(CONFIG_IA64_SGI_SN1)
  2151.     continue;
  2152. #else
  2153.     nbars = 2;
  2154. } else {
  2155.     nbars = PCI_CFG_BASE_ADDRS;
  2156. #endif
  2157. }
  2158. #if DEBUG && ATTACH_DEBUG
  2159. PRINT_NOTICE( 
  2160. "%s pcibr: pci slot %d func %d: vendor 0x%x device 0x%x",
  2161. pcibr_soft->bs_name, slot, func, vendor, device);
  2162. #endif
  2163. pcibr_info = pcibr_device_info_new
  2164.     (pcibr_soft, slot, rfunc, vendor, device);
  2165. conn_vhdl = pciio_device_info_register(pcibr_vhdl, &pcibr_info->f_c);
  2166. if (func == 0)
  2167.     slotp->slot_conn = conn_vhdl;
  2168. cmd_reg = cfgw[PCI_CFG_COMMAND / 4];
  2169. wptr = cfgw + PCI_CFG_BASE_ADDR_0 / 4;
  2170. #if defined(CONFIG_IA64_SGI_SN1)
  2171. for (win = 0; win < PCI_CFG_BASE_ADDRS; ++win)
  2172. #else
  2173. for (win = 0; win < nbars; ++win)
  2174. #endif
  2175. {
  2176.     iopaddr_t               base, mask, code;
  2177.     size_t                  size;
  2178.     /*
  2179.      * GET THE BASE & SIZE OF THIS WINDOW:
  2180.      *
  2181.      * The low two or four bits of the BASE register
  2182.      * determines which address space we are in; the
  2183.      * rest is a base address. BASE registers
  2184.      * determine windows that are power-of-two sized
  2185.      * and naturally aligned, so we can get the size
  2186.      * of a window by writing all-ones to the
  2187.      * register, reading it back, and seeing which
  2188.      * bits are used for decode; the least
  2189.      * significant nonzero bit is also the size of
  2190.      * the window.
  2191.      *
  2192.      * WARNING: someone may already have allocated
  2193.      * some PCI space to this window, and in fact
  2194.      * PIO may be in process at this very moment
  2195.      * from another processor (or even from this
  2196.      * one, if we get interrupted)! So, if the BASE
  2197.      * already has a nonzero address, be generous
  2198.      * and use the LSBit of that address as the
  2199.      * size; this could overstate the window size.
  2200.      * Usually, when one card is set up, all are set
  2201.      * up; so, since we don't bitch about
  2202.      * overlapping windows, we are ok.
  2203.      *
  2204.      * UNFORTUNATELY, some cards do not clear their
  2205.      * BASE registers on reset. I have two heuristics
  2206.      * that can detect such cards: first, if the
  2207.      * decode enable is turned off for the space
  2208.      * that the window uses, we can disregard the
  2209.      * initial value. second, if the address is
  2210.      * outside the range that we use, we can disregard
  2211.      * it as well.
  2212.      *
  2213.      * This is looking very PCI generic. Except for
  2214.      * knowing how many slots and where their config
  2215.      * spaces are, this window loop and the next one
  2216.      * could probably be shared with other PCI host
  2217.      * adapters. It would be interesting to see if
  2218.      * this could be pushed up into pciio, when we
  2219.      * start supporting more PCI providers.
  2220.      */
  2221. #ifdef LITTLE_ENDIAN
  2222.     base = wptr[((win*4)^4)/4];
  2223. #else
  2224.     base = wptr[win];
  2225. #endif
  2226.     if (base & PCI_BA_IO_SPACE) {
  2227. /* BASE is in I/O space. */
  2228. space = PCIIO_SPACE_IO;
  2229. mask = -4;
  2230. code = base & 3;
  2231. base = base & mask;
  2232. if (base == 0) {
  2233.     ; /* not assigned */
  2234. } else if (!(cmd_reg & PCI_CMD_IO_SPACE)) {
  2235.     base = 0; /* decode not enabled */
  2236. }
  2237.     } else {
  2238. /* BASE is in MEM space. */
  2239. space = PCIIO_SPACE_MEM;
  2240. mask = -16;
  2241. code = base & PCI_BA_MEM_LOCATION; /* extract BAR type */
  2242. base = base & mask;
  2243. if (base == 0) {
  2244.     ; /* not assigned */
  2245. } else if (!(cmd_reg & PCI_CMD_MEM_SPACE)) {
  2246.     base = 0; /* decode not enabled */
  2247. } else if (base & 0xC0000000) {
  2248.     base = 0; /* outside permissable range */
  2249. } else if ((code == PCI_BA_MEM_64BIT) &&
  2250. #ifdef LITTLE_ENDIAN
  2251.    (wptr[(((win + 1)*4)^4)/4] != 0)) {
  2252. #else 
  2253.    (wptr[win + 1] != 0)) {
  2254. #endif /* LITTLE_ENDIAN */
  2255.     base = 0; /* outside permissable range */
  2256. }
  2257.     }
  2258.     if (base != 0) { /* estimate size */
  2259. size = base & -base;
  2260.     } else { /* calculate size */
  2261. #ifdef LITTLE_ENDIAN
  2262. wptr[((win*4)^4)/4] = ~0; /* turn on all bits */
  2263. size = wptr[((win*4)^4)/4]; /* get stored bits */
  2264. #else 
  2265. wptr[win] = ~0; /* turn on all bits */
  2266. size = wptr[win]; /* get stored bits */
  2267. #endif /* LITTLE_ENDIAN */
  2268. size &= mask; /* keep addr */
  2269. size &= -size; /* keep lsbit */
  2270. if (size == 0)
  2271.     continue;
  2272.     }
  2273.     pcibr_info->f_window[win].w_space = space;
  2274.     pcibr_info->f_window[win].w_base = base;
  2275.     pcibr_info->f_window[win].w_size = size;
  2276.     /*
  2277.      * If this window already has PCI space
  2278.      * allocated for it, "subtract" that space from
  2279.      * our running freeblocks. Don't worry about
  2280.      * overlaps in existing allocated windows; we
  2281.      * may be overstating their sizes anyway.
  2282.      */
  2283.     if (base && size) {
  2284. if (space == PCIIO_SPACE_IO) {
  2285.     pcibr_freeblock_sub(&pci_io_fb,
  2286. &pci_io_fl,
  2287. base, size);
  2288. } else {
  2289.     pcibr_freeblock_sub(&pci_lo_fb,
  2290. &pci_lo_fl,
  2291. base, size);
  2292.     pcibr_freeblock_sub(&pci_hi_fb,
  2293. &pci_hi_fl,
  2294. base, size);
  2295. }
  2296.     }
  2297. #if defined(IOC3_VENDOR_ID_NUM) && defined(IOC3_DEVICE_ID_NUM)
  2298.     /*
  2299.      * IOC3 BASE_ADDR* BUG WORKAROUND
  2300.      *
  2301.      
  2302.      * If we write to BASE1 on the IOC3, the
  2303.      * data in BASE0 is replaced. The
  2304.      * original workaround was to remember
  2305.      * the value of BASE0 and restore it
  2306.      * when we ran off the end of the BASE
  2307.      * registers; however, a later
  2308.      * workaround was added (I think it was
  2309.      * rev 1.44) to avoid setting up
  2310.      * anything but BASE0, with the comment
  2311.      * that writing all ones to BASE1 set
  2312.      * the enable-parity-error test feature
  2313.      * in IOC3's SCR bit 14.
  2314.      *
  2315.      * So, unless we defer doing any PCI
  2316.      * space allocation until drivers
  2317.      * attach, and set up a way for drivers
  2318.      * (the IOC3 in paricular) to tell us
  2319.      * generically to keep our hands off
  2320.      * BASE registers, we gotta "know" about
  2321.      * the IOC3 here.
  2322.      *
  2323.      * Too bad the PCI folks didn't reserve the
  2324.      * all-zero value for 'no BASE here' (it is a
  2325.      * valid code for an uninitialized BASE in
  2326.      * 32-bit PCI memory space).
  2327.      */
  2328.     
  2329.     if ((vendor == IOC3_VENDOR_ID_NUM) &&
  2330. (device == IOC3_DEVICE_ID_NUM))
  2331. break;
  2332. #endif
  2333.     if (code == PCI_BA_MEM_64BIT) {
  2334. win++; /* skip upper half */
  2335. #ifdef LITTLE_ENDIAN
  2336. wptr[((win*4)^4)/4] = 0; /* which must be zero */
  2337. #else 
  2338. wptr[win] = 0; /* which must be zero */
  2339. #endif /* LITTLE_ENDIAN */
  2340.     }
  2341. } /* next win */
  2342.     } /* next func */
  2343.     /* Store back the values for allocated PCI address spaces */
  2344.     PCI_ADDR_SPACE_LIMITS_STORE();
  2345.     return(0);
  2346. }
  2347. /*
  2348.  * pcibr_slot_info_free
  2349.  * Remove all the PCI infrastructural information associated
  2350.  *  with a particular PCI device.
  2351.  */
  2352. int
  2353. pcibr_slot_info_free(devfs_handle_t pcibr_vhdl,
  2354.                      pciio_slot_t slot)
  2355. {
  2356.     pcibr_soft_t pcibr_soft;
  2357.     pcibr_info_h pcibr_infoh;
  2358.     int nfunc;
  2359.     pcibr_soft = pcibr_soft_get(pcibr_vhdl);
  2360.     if (!pcibr_soft || !PCIBR_VALID_SLOT(slot))
  2361. return(EINVAL);
  2362. #if !defined(CONFIG_IA64_SGI_SN1)
  2363.     /* Clean out all the base registers */
  2364.     bridge = pcibr_soft->bs_base;
  2365.     cfgw = bridge->b_type0_cfg_dev[slot].l;
  2366.     wptr = cfgw + PCI_CFG_BASE_ADDR_0 / 4;
  2367.     
  2368.     for (win = 0; win < PCI_CFG_BASE_ADDRS; ++win) 
  2369. #ifdef LITTLE_ENDIAN
  2370. wptr[((win*4)^4)/4] = 0;
  2371. #else
  2372. wptr[win] = 0;
  2373. #endif  /* LITTLE_ENDIAN */
  2374. #endif /* !CONFIG_IA64_SGI_SN1 */
  2375.     nfunc = pcibr_soft->bs_slot[slot].bss_ninfo;
  2376.     pcibr_device_info_free(pcibr_vhdl, slot);
  2377.     pcibr_infoh = pcibr_soft->bs_slot[slot].bss_infos;
  2378.     DELA(pcibr_infoh,nfunc);
  2379.     pcibr_soft->bs_slot[slot].bss_ninfo = 0;
  2380.     return(0);
  2381. }
  2382. int as_debug = 0;
  2383. /*
  2384.  * pcibr_slot_addr_space_init
  2385.  * Reserve chunks of PCI address space as required by 
  2386.  *  the base registers in the card.
  2387.  */
  2388. int
  2389. pcibr_slot_addr_space_init(devfs_handle_t pcibr_vhdl,
  2390.    pciio_slot_t slot)
  2391. {
  2392.     pcibr_soft_t pcibr_soft;
  2393.     pcibr_info_h pcibr_infoh;
  2394.     pcibr_info_t pcibr_info;
  2395.     bridge_t *bridge;
  2396.     iopaddr_t pci_io_fb, pci_io_fl;
  2397.     iopaddr_t pci_lo_fb, pci_lo_fl;
  2398.     iopaddr_t pci_hi_fb, pci_hi_fl;
  2399.     size_t              align;
  2400.     iopaddr_t           mask;
  2401.     int      nbars;
  2402.     int         nfunc;
  2403.     int func;
  2404.     int win;
  2405.     pcibr_soft = pcibr_soft_get(pcibr_vhdl);
  2406.     if (!pcibr_soft || !PCIBR_VALID_SLOT(slot))
  2407. return(EINVAL);
  2408.     bridge = pcibr_soft->bs_base;
  2409.     /* Get the current values for the allocated PCI address spaces */
  2410.     PCI_ADDR_SPACE_LIMITS_LOAD();
  2411.     if (as_debug)
  2412. #ifdef LATER
  2413.     PCI_ADDR_SPACE_LIMITS_PRINT();
  2414. #endif
  2415.     /* allocate address space,
  2416.      * for windows that have not been
  2417.      * previously assigned.
  2418.      */
  2419.     if (pcibr_soft->bs_slot[slot].has_host) {
  2420. return(0);
  2421.     }
  2422.     nfunc = pcibr_soft->bs_slot[slot].bss_ninfo;
  2423.     if (nfunc < 1)
  2424. return(EINVAL);
  2425.     pcibr_infoh = pcibr_soft->bs_slot[slot].bss_infos;
  2426.     if (!pcibr_infoh)
  2427. return(EINVAL);
  2428.     /*
  2429.      * Try to make the DevIO windows not
  2430.      * overlap by pushing the "io" and "hi"
  2431.      * allocation areas up to the next one
  2432.      * or two megabyte bound. This also
  2433.      * keeps them from being zero.
  2434.      *
  2435.      * DO NOT do this with "pci_lo" since
  2436.      * the entire "lo" area is only a
  2437.      * megabyte, total ...
  2438.      */
  2439.     align = (slot < 2) ? 0x200000 : 0x100000;
  2440.     mask = -align;
  2441.     pci_io_fb = (pci_io_fb + align - 1) & mask;
  2442.     pci_hi_fb = (pci_hi_fb + align - 1) & mask;
  2443.     for (func = 0; func < nfunc; ++func) {
  2444. cfg_p                   cfgw;
  2445. cfg_p                   wptr;
  2446. pciio_space_t           space;
  2447. iopaddr_t               base;
  2448. size_t                  size;
  2449. cfg_p                   pci_cfg_cmd_reg_p;
  2450. unsigned                pci_cfg_cmd_reg;
  2451. unsigned                pci_cfg_cmd_reg_add = 0;
  2452. pcibr_info = pcibr_infoh[func];
  2453. if (!pcibr_info)
  2454.     continue;
  2455. if (pcibr_info->f_vendor == PCIIO_VENDOR_ID_NONE)
  2456.     continue;
  2457. cfgw = bridge->b_type0_cfg_dev[slot].f[func].l;
  2458. wptr = cfgw + PCI_CFG_BASE_ADDR_0 / 4;
  2459. #if defined(CONFIG_IA64_SGI_SN1)
  2460. nbars = PCI_CFG_BASE_ADDRS;
  2461. #else
  2462. if ((do_pcibr_config_get(cfgw, PCI_CFG_HEADER_TYPE, 1) & 0x7f) != 0)
  2463.     nbars = 2;
  2464. else
  2465.     nbars = PCI_CFG_BASE_ADDRS;
  2466. #endif
  2467. for (win = 0; win < nbars; ++win) {
  2468.     space = pcibr_info->f_window[win].w_space;
  2469.     base = pcibr_info->f_window[win].w_base;
  2470.     size = pcibr_info->f_window[win].w_size;
  2471.     
  2472.     if (size < 1)
  2473. continue;
  2474.     if (base >= size) {
  2475. #if DEBUG && PCI_DEBUG
  2476. printk("pcibr: slot %d func %d window %d is in %d[0x%x..0x%x], alloc by promn",
  2477. slot, func, win, space, base, base + size - 1);
  2478. #endif
  2479. continue; /* already allocated */
  2480.     }
  2481.     align = size; /* ie. 0x00001000 */
  2482.     if (align < _PAGESZ)
  2483. align = _PAGESZ; /* ie. 0x00004000 */
  2484.     mask = -align; /* ie. 0xFFFFC000 */
  2485.     switch (space) {
  2486.     case PCIIO_SPACE_IO:
  2487. base = (pci_io_fb + align - 1) & mask;
  2488. if ((base + size) > pci_io_fl) {
  2489.     base = 0;
  2490.     break;
  2491. }
  2492. pci_io_fb = base + size;
  2493. break;
  2494.     case PCIIO_SPACE_MEM:
  2495. #ifdef LITTLE_ENDIAN
  2496. if ((wptr[((win*4)^4)/4] & PCI_BA_MEM_LOCATION) ==
  2497. #else
  2498. if ((wptr[win] & PCI_BA_MEM_LOCATION) ==
  2499. #endif  /* LITTLE_ENDIAN */
  2500.     PCI_BA_MEM_1MEG) {
  2501.     /* allocate from 20-bit PCI space */
  2502.     base = (pci_lo_fb + align - 1) & mask;
  2503.     if ((base + size) > pci_lo_fl) {
  2504. base = 0;
  2505. break;
  2506.     }
  2507.     pci_lo_fb = base + size;
  2508. } else {
  2509.     /* allocate from 32-bit or 64-bit PCI space */
  2510.     base = (pci_hi_fb + align - 1) & mask;
  2511.     if ((base + size) > pci_hi_fl) {
  2512. base = 0;
  2513. break;
  2514.     }
  2515.     pci_hi_fb = base + size;
  2516. }
  2517. break;
  2518.     default:
  2519. base = 0;
  2520. #if DEBUG && PCI_DEBUG
  2521. printk("pcibr: slot %d window %d had bad space code %dn",
  2522. slot, win, space);
  2523. #endif
  2524.     }
  2525.     pcibr_info->f_window[win].w_base = base;
  2526. #ifdef LITTLE_ENDIAN
  2527.     wptr[((win*4)^4)/4] = base;
  2528. #if DEBUG && PCI_DEBUG
  2529. printk("Setting base address 0x%p base 0x%xn", &(wptr[((win*4)^4)/4]), base);
  2530. #endif
  2531. #else
  2532.     wptr[win] = base;
  2533. #endif  /* LITTLE_ENDIAN */
  2534. #if DEBUG && PCI_DEBUG
  2535.     if (base >= size)
  2536. printk("pcibr: slot %d func %d window %d is in %d [0x%x..0x%x], alloc by pcibrn",
  2537. slot, func, win, space, base, base + size - 1);
  2538.     else
  2539. printk("pcibr: slot %d func %d window %d, unable to alloc 0x%x in 0x%pn",
  2540. slot, func, win, size, space);
  2541. #endif
  2542. } /* next base */
  2543. /*
  2544.  * Allocate space for the EXPANSION ROM
  2545.  * NOTE: DO NOT DO THIS ON AN IOC3,
  2546.  * as it blows the system away.
  2547.  */
  2548. base = size = 0;
  2549. if ((pcibr_soft->bs_slot[slot].bss_vendor_id != IOC3_VENDOR_ID_NUM) ||
  2550.     (pcibr_soft->bs_slot[slot].bss_device_id != IOC3_DEVICE_ID_NUM)) {
  2551.     wptr = cfgw + PCI_EXPANSION_ROM / 4;
  2552. #ifdef LITTLE_ENDIAN
  2553.     wptr[1] = 0xFFFFF000;
  2554.     mask = wptr[1];
  2555. #else
  2556.     *wptr = 0xFFFFF000;
  2557.     mask = *wptr;
  2558. #endif  /* LITTLE_ENDIAN */
  2559.     if (mask & 0xFFFFF000) {
  2560. size = mask & -mask;
  2561. align = size;
  2562. if (align < _PAGESZ)
  2563.     align = _PAGESZ;
  2564. mask = -align;
  2565. base = (pci_hi_fb + align - 1) & mask;
  2566. if ((base + size) > pci_hi_fl)
  2567.     base = size = 0;
  2568. else {
  2569.     pci_hi_fb = base + size;
  2570. #ifdef LITTLE_ENDIAN
  2571.     wptr[1] = base;
  2572. #else
  2573.     *wptr = base;
  2574. #endif  /* LITTLE_ENDIAN */
  2575. #if DEBUG && PCI_DEBUG
  2576.     printk("%s/%d ROM in 0x%lx..0x%lx (alloc by pcibr)n",
  2577.     pcibr_soft->bs_name, slot,
  2578.     base, base + size - 1);
  2579. #endif
  2580. }
  2581.     }
  2582. }
  2583. pcibr_info->f_rbase = base;
  2584. pcibr_info->f_rsize = size;
  2585. /*
  2586.  * if necessary, update the board's
  2587.  * command register to enable decoding
  2588.  * in the windows we added.
  2589.  *
  2590.  * There are some bits we always want to
  2591.  * be sure are set.
  2592.  */
  2593. pci_cfg_cmd_reg_add |= PCI_CMD_IO_SPACE;
  2594. /*
  2595.  * The Adaptec 1160 FC Controller WAR #767995:
  2596.  * The part incorrectly ignores the upper 32 bits of a 64 bit
  2597.  * address when decoding references to it's registers so to
  2598.  * keep it from responding to a bus cycle that it shouldn't
  2599.  * we only use I/O space to get at it's registers.  Don't
  2600.  * enable memory space accesses on that PCI device.
  2601.  */
  2602. #define FCADP_VENDID 0x9004 /* Adaptec Vendor ID from fcadp.h */
  2603. #define FCADP_DEVID 0x1160  /* Adaptec 1160 Device ID from fcadp.h */
  2604. if ((pcibr_info->f_vendor != FCADP_VENDID) ||
  2605.     (pcibr_info->f_device != FCADP_DEVID))
  2606.     pci_cfg_cmd_reg_add |= PCI_CMD_MEM_SPACE;
  2607. pci_cfg_cmd_reg_add |= PCI_CMD_BUS_MASTER;
  2608. pci_cfg_cmd_reg_p = cfgw + PCI_CFG_COMMAND / 4;
  2609. pci_cfg_cmd_reg = *pci_cfg_cmd_reg_p;
  2610. #if PCI_FBBE /* XXX- check here to see if dev can do fast-back-to-back */
  2611. if (!((pci_cfg_cmd_reg >> 16) & PCI_STAT_F_BK_BK_CAP))
  2612.     fast_back_to_back_enable = 0;
  2613. #endif
  2614. pci_cfg_cmd_reg &= 0xFFFF;
  2615. if (pci_cfg_cmd_reg_add & ~pci_cfg_cmd_reg)
  2616.     *pci_cfg_cmd_reg_p = pci_cfg_cmd_reg | pci_cfg_cmd_reg_add;
  2617.     } /* next func */
  2618.     /* Now that we have allocated new chunks of PCI address spaces to this
  2619.      * card we need to update the bookkeeping values which indicate
  2620.      * the current PCI address space allocations.
  2621.      */
  2622.     PCI_ADDR_SPACE_LIMITS_STORE();
  2623.     return(0);
  2624. }
  2625. /*
  2626.  * pcibr_slot_device_init
  2627.  *  Setup the device register in the bridge for this PCI slot.
  2628.  */
  2629. int
  2630. pcibr_slot_device_init(devfs_handle_t pcibr_vhdl,
  2631.        pciio_slot_t slot)
  2632. {
  2633.     pcibr_soft_t  pcibr_soft;
  2634.     bridge_t *bridge;
  2635.     bridgereg_t  devreg;
  2636.     pcibr_soft = pcibr_soft_get(pcibr_vhdl);
  2637.     if (!pcibr_soft || !PCIBR_VALID_SLOT(slot))
  2638. return(EINVAL);
  2639.     bridge = pcibr_soft->bs_base;
  2640.     /*
  2641.      * Adjustments to Device(x)
  2642.      * and init of bss_device shadow
  2643.      */
  2644.     devreg = bridge->b_device[slot].reg;
  2645.     devreg &= ~BRIDGE_DEV_PAGE_CHK_DIS;
  2646.     devreg |= BRIDGE_DEV_COH | BRIDGE_DEV_VIRTUAL_EN;
  2647. #ifdef LITTLE_ENDIAN
  2648.     devreg |= BRIDGE_DEV_DEV_SWAP;
  2649. #endif
  2650.     pcibr_soft->bs_slot[slot].bss_device = devreg;
  2651.     bridge->b_device[slot].reg = devreg;
  2652. #if DEBUG && PCI_DEBUG
  2653. printk("pcibr Device(%d): 0x%lxn", slot, bridge->b_device[slot].reg);
  2654. #endif
  2655. #if DEBUG && PCI_DEBUG
  2656.     printk("pcibr: PCI space allocation done.n");
  2657. #endif
  2658.     return(0);
  2659. }
  2660. /*
  2661.  * pcibr_slot_guest_info_init
  2662.  * Setup the host/guest relations for a PCI slot.
  2663.  */
  2664. int
  2665. pcibr_slot_guest_info_init(devfs_handle_t pcibr_vhdl,
  2666.    pciio_slot_t slot)
  2667. {
  2668.     pcibr_soft_t pcibr_soft;
  2669.     pcibr_info_h pcibr_infoh;
  2670.     pcibr_info_t pcibr_info;
  2671.     pcibr_soft_slot_t slotp;
  2672.     pcibr_soft = pcibr_soft_get(pcibr_vhdl);
  2673.     if (!pcibr_soft || !PCIBR_VALID_SLOT(slot))
  2674. return(EINVAL);
  2675.     slotp = &pcibr_soft->bs_slot[slot];
  2676.     /* create info and verticies for guest slots;
  2677.      * for compatibilitiy macros, create info
  2678.      * for even unpopulated slots (but do not
  2679.      * build verticies for them).
  2680.      */
  2681.     if (pcibr_soft->bs_slot[slot].bss_ninfo < 1) {
  2682. NEWA(pcibr_infoh, 1);
  2683. pcibr_soft->bs_slot[slot].bss_ninfo = 1;
  2684. pcibr_soft->bs_slot[slot].bss_infos = pcibr_infoh;
  2685. pcibr_info = pcibr_device_info_new
  2686.     (pcibr_soft, slot, PCIIO_FUNC_NONE,
  2687.      PCIIO_VENDOR_ID_NONE, PCIIO_DEVICE_ID_NONE);
  2688. if (pcibr_soft->bs_slot[slot].has_host) {
  2689.     slotp->slot_conn = pciio_device_info_register
  2690. (pcibr_vhdl, &pcibr_info->f_c);
  2691. }
  2692.     }
  2693.     /* generate host/guest relations
  2694.      */
  2695.     if (pcibr_soft->bs_slot[slot].has_host) {
  2696. int  host = pcibr_soft->bs_slot[slot].host_slot;
  2697. pcibr_soft_slot_t host_slotp = &pcibr_soft->bs_slot[host];
  2698. hwgraph_edge_add(slotp->slot_conn,
  2699.  host_slotp->slot_conn,
  2700.  EDGE_LBL_HOST);
  2701. /* XXX- only gives us one guest edge per
  2702.  * host. If/when we have a host with more than
  2703.  * one guest, we will need to figure out how
  2704.  * the host finds all its guests, and sorts
  2705.  * out which one is which.
  2706.  */
  2707. hwgraph_edge_add(host_slotp->slot_conn,
  2708.  slotp->slot_conn,
  2709.  EDGE_LBL_GUEST);
  2710.     }
  2711.     return(0);
  2712. }
  2713. /*
  2714.  * pcibr_slot_initial_rrb_alloc
  2715.  * Allocate a default number of rrbs for this slot on 
  2716.  *  the two channels.  This is dictated by the rrb allocation
  2717.  *  strategy routine defined per platform.
  2718.  */
  2719. int
  2720. pcibr_slot_initial_rrb_alloc(devfs_handle_t pcibr_vhdl,
  2721.      pciio_slot_t slot)
  2722. {
  2723.     pcibr_soft_t pcibr_soft;
  2724.     pcibr_info_h pcibr_infoh;
  2725.     pcibr_info_t pcibr_info;
  2726.     bridge_t *bridge;
  2727.     int                 c0, c1;
  2728.     int r;
  2729.     pcibr_soft = pcibr_soft_get(pcibr_vhdl);
  2730.     if (!pcibr_soft || !PCIBR_VALID_SLOT(slot))
  2731. return(EINVAL);
  2732.     bridge = pcibr_soft->bs_base;
  2733.     /* How may RRBs are on this slot?
  2734.      */
  2735.     c0 = do_pcibr_rrb_count_valid(bridge, slot);
  2736.     c1 = do_pcibr_rrb_count_valid(bridge, slot + PCIBR_RRB_SLOT_VIRTUAL);
  2737. #if PCIBR_RRB_DEBUG
  2738.     printk("pcibr_attach: slot %d started with %d+%dn", slot, c0, c1);
  2739. #endif
  2740.     /* Do we really need any?
  2741.      */
  2742.     pcibr_infoh = pcibr_soft->bs_slot[slot].bss_infos;
  2743.     pcibr_info = pcibr_infoh[0];
  2744.     if ((pcibr_info->f_vendor == PCIIO_VENDOR_ID_NONE) &&
  2745. !pcibr_soft->bs_slot[slot].has_host) {
  2746. if (c0 > 0)
  2747.     do_pcibr_rrb_free(bridge, slot, c0);
  2748. if (c1 > 0)
  2749.     do_pcibr_rrb_free(bridge, slot + PCIBR_RRB_SLOT_VIRTUAL, c1);
  2750. pcibr_soft->bs_rrb_valid[slot] = 0x1000;
  2751. pcibr_soft->bs_rrb_valid[slot + PCIBR_RRB_SLOT_VIRTUAL] = 0x1000;
  2752. return(ENODEV);
  2753.     }
  2754.     pcibr_soft->bs_rrb_avail[slot & 1] -= c0 + c1;
  2755.     pcibr_soft->bs_rrb_valid[slot] = c0;
  2756.     pcibr_soft->bs_rrb_valid[slot + PCIBR_RRB_SLOT_VIRTUAL] = c1;
  2757.     pcibr_soft->bs_rrb_avail[0] = do_pcibr_rrb_count_avail(bridge, 0);
  2758.     pcibr_soft->bs_rrb_avail[1] = do_pcibr_rrb_count_avail(bridge, 1);
  2759.     r = 3 - (c0 + c1);
  2760.     if (r > 0) {
  2761. pcibr_soft->bs_rrb_res[slot] = r;
  2762. pcibr_soft->bs_rrb_avail[slot & 1] -= r;
  2763.     }
  2764. #if PCIBR_RRB_DEBUG
  2765.     printk("t%d+%d+%d",
  2766.     0xFFF & pcibr_soft->bs_rrb_valid[slot],
  2767.     0xFFF & pcibr_soft->bs_rrb_valid[slot + PCIBR_RRB_SLOT_VIRTUAL],
  2768.     pcibr_soft->bs_rrb_res[slot]);
  2769.     printk("n");
  2770. #endif
  2771.     return(0);
  2772. }
  2773. /*
  2774.  * pcibr_slot_call_device_attach
  2775.  * This calls the associated driver attach routine for the PCI
  2776.  *  card in this slot.
  2777.  */
  2778. int
  2779. pcibr_slot_call_device_attach(devfs_handle_t pcibr_vhdl,
  2780.       pciio_slot_t slot,
  2781.       int          drv_flags)
  2782. {
  2783.     pcibr_soft_t pcibr_soft;
  2784.     pcibr_info_h pcibr_infoh;
  2785.     pcibr_info_t pcibr_info;
  2786.     async_attach_t aa = NULL;
  2787.     int func;
  2788.     devfs_handle_t xconn_vhdl,conn_vhdl;
  2789.     int nfunc;
  2790.     int                 error_func;
  2791.     int                 error_slot = 0;
  2792.     int                 error = ENODEV;
  2793.     pcibr_soft = pcibr_soft_get(pcibr_vhdl);
  2794.     if (!pcibr_soft || !PCIBR_VALID_SLOT(slot))
  2795. return(EINVAL);
  2796.     if (pcibr_soft->bs_slot[slot].has_host) {
  2797.         return(EPERM);
  2798.     }
  2799.     
  2800.     xconn_vhdl = pcibr_soft->bs_conn;
  2801.     aa = async_attach_get_info(xconn_vhdl);
  2802.     nfunc = pcibr_soft->bs_slot[slot].bss_ninfo;
  2803.     pcibr_infoh = pcibr_soft->bs_slot[slot].bss_infos;
  2804.     for (func = 0; func < nfunc; ++func) {
  2805. pcibr_info = pcibr_infoh[func];
  2806. if (!pcibr_info)
  2807.     continue;
  2808. if (pcibr_info->f_vendor == PCIIO_VENDOR_ID_NONE)
  2809.     continue;
  2810. conn_vhdl = pcibr_info->f_vertex;
  2811. /* If the PCI device has been disabled in the prom,
  2812.  * do not set it up for driver attach. NOTE: usrpci
  2813.  * and pciba will not "see" this connection point!
  2814.  */
  2815. if (device_admin_info_get(conn_vhdl, ADMIN_LBL_DISABLED)) {
  2816. #ifdef SUPPORT_PRINTING_V_FORMAT
  2817.     PRINT_WARNING("pcibr_slot_call_device_attach: %v disabledn", 
  2818.     conn_vhdl);
  2819. #endif
  2820.     continue;
  2821. }
  2822. #ifdef LATER
  2823. /*
  2824.  * Activate if and when we support cdl.
  2825.  */
  2826. if (aa)
  2827.     async_attach_add_info(conn_vhdl, aa);
  2828. #endif /* LATER */
  2829. error_func = pciio_device_attach(conn_vhdl, drv_flags);
  2830.         pcibr_info->f_att_det_error = error_func;
  2831. if (error_func)
  2832.     error_slot = error_func;
  2833.         error = error_slot;
  2834.     } /* next func */
  2835.     if (error) {
  2836. if ((error != ENODEV) && (error != EUNATCH))
  2837.     pcibr_soft->bs_slot[slot].slot_status |= SLOT_STARTUP_INCMPLT;
  2838.     } else {
  2839.         pcibr_soft->bs_slot[slot].slot_status |= SLOT_STARTUP_CMPLT;
  2840.     }
  2841.         
  2842.     return(error);