pcibr_dvr.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:126k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /*
  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) 2001-2002 Silicon Graphics, Inc. All rights reserved.
  8.  */
  9. #include <linux/types.h>
  10. #include <linux/slab.h>
  11. #include <linux/module.h>
  12. #include <asm/sn/sgi.h>
  13. #include <asm/sn/sn_cpuid.h>
  14. #include <asm/sn/addrs.h>
  15. #include <asm/sn/arch.h>
  16. #include <asm/sn/iograph.h>
  17. #include <asm/sn/invent.h>
  18. #include <asm/sn/hcl.h>
  19. #include <asm/sn/labelcl.h>
  20. #include <asm/sn/xtalk/xwidget.h>
  21. #include <asm/sn/pci/bridge.h>
  22. #include <asm/sn/pci/pciio.h>
  23. #include <asm/sn/pci/pcibr.h>
  24. #include <asm/sn/pci/pcibr_private.h>
  25. #include <asm/sn/pci/pci_defs.h>
  26. #include <asm/sn/prio.h>
  27. #include <asm/sn/xtalk/xbow.h>
  28. #include <asm/sn/ioc3.h>
  29. #include <asm/sn/eeprom.h>
  30. #include <asm/sn/io.h>
  31. #include <asm/sn/sn_private.h>
  32. #ifdef __ia64
  33. #define rmallocmap atemapalloc
  34. #define rmfreemap atemapfree
  35. #define rmfree atefree
  36. #define rmalloc atealloc
  37. #endif
  38. /*
  39.  * Macros related to the Lucent USS 302/312 usb timeout workaround.  It
  40.  * appears that if the lucent part can get into a retry loop if it sees a
  41.  * DAC on the bus during a pio read retry.  The loop is broken after about
  42.  * 1ms, so we need to set up bridges holding this part to allow at least
  43.  * 1ms for pio.
  44.  */
  45. #define USS302_TIMEOUT_WAR
  46. #ifdef USS302_TIMEOUT_WAR
  47. #define LUCENT_USBHC_VENDOR_ID_NUM 0x11c1
  48. #define LUCENT_USBHC302_DEVICE_ID_NUM 0x5801
  49. #define LUCENT_USBHC312_DEVICE_ID_NUM 0x5802
  50. #define USS302_BRIDGE_TIMEOUT_HLD 4
  51. #endif
  52. int                     pcibr_devflag = D_MP;
  53. /*
  54.  * This is the file operation table for the pcibr driver.
  55.  * As each of the functions are implemented, put the
  56.  * appropriate function name below.
  57.  */
  58. struct file_operations pcibr_fops = {
  59. owner:  THIS_MODULE,
  60. llseek: NULL,
  61. read: NULL,
  62. write: NULL,
  63. readdir: NULL,
  64. poll: NULL,
  65. ioctl: NULL,
  66. mmap: NULL,
  67. open: NULL,
  68. flush: NULL,
  69. release: NULL,
  70. fsync: NULL,
  71. fasync: NULL,
  72. lock: NULL,
  73. readv: NULL,
  74. writev: NULL
  75. };
  76. #ifdef LATER
  77. #if PCIBR_ATE_DEBUG
  78. static struct reg_values ssram_sizes[] =
  79. {
  80.     {BRIDGE_CTRL_SSRAM_512K, "512k"},
  81.     {BRIDGE_CTRL_SSRAM_128K, "128k"},
  82.     {BRIDGE_CTRL_SSRAM_64K, "64k"},
  83.     {BRIDGE_CTRL_SSRAM_1K, "1k"},
  84.     {0}
  85. };
  86. static struct reg_desc   control_bits[] =
  87. {
  88.     {BRIDGE_CTRL_FLASH_WR_EN, 0, "FLASH_WR_EN"},
  89.     {BRIDGE_CTRL_EN_CLK50, 0, "EN_CLK50"},
  90.     {BRIDGE_CTRL_EN_CLK40, 0, "EN_CLK40"},
  91.     {BRIDGE_CTRL_EN_CLK33, 0, "EN_CLK33"},
  92.     {BRIDGE_CTRL_RST_MASK, -24, "RST", "%x"},
  93.     {BRIDGE_CTRL_IO_SWAP, 0, "IO_SWAP"},
  94.     {BRIDGE_CTRL_MEM_SWAP, 0, "MEM_SWAP"},
  95.     {BRIDGE_CTRL_PAGE_SIZE, 0, "PAGE_SIZE"},
  96.     {BRIDGE_CTRL_SS_PAR_BAD, 0, "SS_PAR_BAD"},
  97.     {BRIDGE_CTRL_SS_PAR_EN, 0, "SS_PAR_EN"},
  98.     {BRIDGE_CTRL_SSRAM_SIZE_MASK, 0, "SSRAM_SIZE", 0, ssram_sizes},
  99.     {BRIDGE_CTRL_F_BAD_PKT, 0, "F_BAD_PKT"},
  100.     {BRIDGE_CTRL_LLP_XBAR_CRD_MASK, -12, "LLP_XBAR_CRD", "%d"},
  101.     {BRIDGE_CTRL_CLR_RLLP_CNT, 0, "CLR_RLLP_CNT"},
  102.     {BRIDGE_CTRL_CLR_TLLP_CNT, 0, "CLR_TLLP_CNT"},
  103.     {BRIDGE_CTRL_SYS_END, 0, "SYS_END"},
  104.     {BRIDGE_CTRL_BUS_SPEED_MASK, -4, "BUS_SPEED", "%d"},
  105.     {BRIDGE_CTRL_WIDGET_ID_MASK, 0, "WIDGET_ID", "%x"},
  106.     {0}
  107. };
  108. #endif
  109. #endif /* LATER */
  110. /* kbrick widgetnum-to-bus layout */
  111. int p_busnum[MAX_PORT_NUM] = {                  /* widget#      */
  112.         0, 0, 0, 0, 0, 0, 0, 0,                 /* 0x0 - 0x7    */
  113.         2,                                      /* 0x8          */
  114.         1,                                      /* 0x9          */
  115.         0, 0,                                   /* 0xa - 0xb    */
  116.         5,                                      /* 0xc          */
  117.         6,                                      /* 0xd          */
  118.         4,                                      /* 0xe          */
  119.         3,                                      /* 0xf          */
  120. };
  121. /*
  122.  * Additional PIO spaces per slot are
  123.  * recorded in this structure.
  124.  */
  125. struct pciio_piospace_s {
  126.     pciio_piospace_t        next;       /* another space for this device */
  127.     char                    free;       /* 1 if free, 0 if in use               */
  128.     pciio_space_t           space;      /* Which space is in use                */
  129.     iopaddr_t               start;      /* Starting address of the PIO space    */
  130.     size_t                  count;      /* size of PIO space                    */
  131. };
  132. #if PCIBR_SOFT_LIST
  133. pcibr_list_p            pcibr_list = 0;
  134. #endif
  135. extern int              hwgraph_vertex_name_get(devfs_handle_t vhdl, char *buf, uint buflen);
  136. extern int              hub_device_flags_set(devfs_handle_t widget_dev, hub_widget_flags_t flags);
  137. extern long             atoi(register char *p);
  138. extern cnodeid_t        nodevertex_to_cnodeid(devfs_handle_t vhdl);
  139. extern void             *swap_ptr(void **loc, void *new);
  140. extern char             *dev_to_name(devfs_handle_t dev, char *buf, uint buflen);
  141. extern struct map       *atemapalloc(uint64_t);
  142. extern void             atefree(struct map *, size_t, uint64_t);
  143. extern void             atemapfree(struct map *);
  144. extern pciio_dmamap_t   get_free_pciio_dmamap(devfs_handle_t);
  145. extern void free_pciio_dmamap(pcibr_dmamap_t);
  146. #define ATE_WRITE()    ate_write(ate_ptr, ate_count, ate)
  147. #if PCIBR_FREEZE_TIME
  148. #define ATE_FREEZE() s = ate_freeze(pcibr_dmamap, &freeze_time, cmd_regs)
  149. #else
  150. #define ATE_FREEZE() s = ate_freeze(pcibr_dmamap, cmd_regs)
  151. #endif /* PCIBR_FREEZE_TIME */
  152. #if PCIBR_FREEZE_TIME
  153. #define ATE_THAW() ate_thaw(pcibr_dmamap, ate_index, ate, ate_total, freeze_time, cmd_regs, s)
  154. #else
  155. #define ATE_THAW() ate_thaw(pcibr_dmamap, ate_index, cmd_regs, s)
  156. #endif
  157. /* =====================================================================
  158.  *    Function Table of Contents
  159.  *
  160.  *      The order of functions in this file has stopped
  161.  *      making much sense. We might want to take a look
  162.  *      at it some time and bring back some sanity, or
  163.  *      perhaps bust this file into smaller chunks.
  164.  */
  165. extern void              do_pcibr_rrb_clear(bridge_t *, int);
  166. extern void              do_pcibr_rrb_flush(bridge_t *, int);
  167. extern int               do_pcibr_rrb_count_valid(bridge_t *, pciio_slot_t);
  168. extern int               do_pcibr_rrb_count_avail(bridge_t *, pciio_slot_t);
  169. extern int               do_pcibr_rrb_alloc(bridge_t *, pciio_slot_t, int);
  170. extern int               do_pcibr_rrb_free(bridge_t *, pciio_slot_t, int);
  171. extern void              do_pcibr_rrb_autoalloc(pcibr_soft_t, int, int);
  172. extern int    pcibr_wrb_flush(devfs_handle_t);
  173. extern int               pcibr_rrb_alloc(devfs_handle_t, int *, int *);
  174. extern int               pcibr_rrb_check(devfs_handle_t, int *, int *, int *, int *);
  175. extern int               pcibr_alloc_all_rrbs(devfs_handle_t, int, int, int, int, int, int, int, int, int);
  176. extern void              pcibr_rrb_flush(devfs_handle_t);
  177. static int                pcibr_try_set_device(pcibr_soft_t, pciio_slot_t, unsigned, bridgereg_t);
  178. void                     pcibr_release_device(pcibr_soft_t, pciio_slot_t, bridgereg_t);
  179. extern void              pcibr_clearwidint(bridge_t *);
  180. extern void              pcibr_setwidint(xtalk_intr_t);
  181. void                     pcibr_init(void);
  182. int                      pcibr_attach(devfs_handle_t);
  183. int  pcibr_detach(devfs_handle_t);
  184. int                      pcibr_open(devfs_handle_t *, int, int, cred_t *);
  185. int                      pcibr_close(devfs_handle_t, int, int, cred_t *);
  186. int                      pcibr_map(devfs_handle_t, vhandl_t *, off_t, size_t, uint);
  187. int                      pcibr_unmap(devfs_handle_t, vhandl_t *);
  188. int                      pcibr_ioctl(devfs_handle_t, int, void *, int, struct cred *, int *);
  189. void                     pcibr_freeblock_sub(iopaddr_t *, iopaddr_t *, iopaddr_t, size_t);
  190. extern int               pcibr_init_ext_ate_ram(bridge_t *);
  191. extern int               pcibr_ate_alloc(pcibr_soft_t, int);
  192. extern void              pcibr_ate_free(pcibr_soft_t, int, int);
  193. extern unsigned          ate_freeze(pcibr_dmamap_t pcibr_dmamap,
  194. #if PCIBR_FREEZE_TIME
  195.      unsigned *freeze_time_ptr,
  196. #endif
  197. unsigned *cmd_regs);
  198. extern void              ate_write(bridge_ate_p ate_ptr, int ate_count, bridge_ate_t ate);
  199. extern void              ate_thaw(pcibr_dmamap_t pcibr_dmamap, int ate_index,
  200. #if PCIBR_FREEZE_TIME
  201.   bridge_ate_t ate,
  202.   int ate_total,
  203.   unsigned freeze_time_start,
  204. #endif
  205.   unsigned *cmd_regs,
  206.   unsigned s);
  207. pcibr_info_t             pcibr_info_get(devfs_handle_t);
  208. static iopaddr_t         pcibr_addr_pci_to_xio(devfs_handle_t, pciio_slot_t, pciio_space_t, iopaddr_t, size_t, unsigned);
  209. pcibr_piomap_t          pcibr_piomap_alloc(devfs_handle_t, device_desc_t, pciio_space_t, iopaddr_t, size_t, size_t, unsigned);
  210. void                    pcibr_piomap_free(pcibr_piomap_t);
  211. caddr_t                 pcibr_piomap_addr(pcibr_piomap_t, iopaddr_t, size_t);
  212. void                    pcibr_piomap_done(pcibr_piomap_t);
  213. caddr_t                 pcibr_piotrans_addr(devfs_handle_t, device_desc_t, pciio_space_t, iopaddr_t, size_t, unsigned);
  214. iopaddr_t               pcibr_piospace_alloc(devfs_handle_t, device_desc_t, pciio_space_t, size_t, size_t);
  215. void                    pcibr_piospace_free(devfs_handle_t, pciio_space_t, iopaddr_t, size_t);
  216. static iopaddr_t         pcibr_flags_to_d64(unsigned, pcibr_soft_t);
  217. extern bridge_ate_t     pcibr_flags_to_ate(unsigned);
  218. pcibr_dmamap_t          pcibr_dmamap_alloc(devfs_handle_t, device_desc_t, size_t, unsigned);
  219. void                    pcibr_dmamap_free(pcibr_dmamap_t);
  220. extern bridge_ate_p     pcibr_ate_addr(pcibr_soft_t, int);
  221. static iopaddr_t         pcibr_addr_xio_to_pci(pcibr_soft_t, iopaddr_t, size_t);
  222. iopaddr_t               pcibr_dmamap_addr(pcibr_dmamap_t, paddr_t, size_t);
  223. alenlist_t              pcibr_dmamap_list(pcibr_dmamap_t, alenlist_t, unsigned);
  224. void                    pcibr_dmamap_done(pcibr_dmamap_t);
  225. cnodeid_t pcibr_get_dmatrans_node(devfs_handle_t);
  226. iopaddr_t               pcibr_dmatrans_addr(devfs_handle_t, device_desc_t, paddr_t, size_t, unsigned);
  227. alenlist_t              pcibr_dmatrans_list(devfs_handle_t, device_desc_t, alenlist_t, unsigned);
  228. void                    pcibr_dmamap_drain(pcibr_dmamap_t);
  229. void                    pcibr_dmaaddr_drain(devfs_handle_t, paddr_t, size_t);
  230. void                    pcibr_dmalist_drain(devfs_handle_t, alenlist_t);
  231. iopaddr_t               pcibr_dmamap_pciaddr_get(pcibr_dmamap_t);
  232. extern unsigned pcibr_intr_bits(pciio_info_t info, pciio_intr_line_t lines);
  233. extern pcibr_intr_t     pcibr_intr_alloc(devfs_handle_t, device_desc_t, pciio_intr_line_t, devfs_handle_t);
  234. extern void             pcibr_intr_free(pcibr_intr_t);
  235. extern void             pcibr_setpciint(xtalk_intr_t);
  236. extern int              pcibr_intr_connect(pcibr_intr_t);
  237. extern void             pcibr_intr_disconnect(pcibr_intr_t);
  238. extern devfs_handle_t     pcibr_intr_cpu_get(pcibr_intr_t);
  239. extern void             pcibr_xintr_preset(void *, int, xwidgetnum_t, iopaddr_t, xtalk_intr_vector_t);
  240. extern void             pcibr_intr_func(intr_arg_t);
  241. extern void             print_bridge_errcmd(uint32_t, char *);
  242. extern void             pcibr_error_dump(pcibr_soft_t);
  243. extern uint32_t         pcibr_errintr_group(uint32_t);
  244. extern void         pcibr_pioerr_check(pcibr_soft_t);
  245. extern void             pcibr_error_intr_handler(intr_arg_t);
  246. extern int              pcibr_addr_toslot(pcibr_soft_t, iopaddr_t, pciio_space_t *, iopaddr_t *, pciio_function_t *);
  247. extern void             pcibr_error_cleanup(pcibr_soft_t, int);
  248. extern void                    pcibr_device_disable(pcibr_soft_t, int);
  249. extern int              pcibr_pioerror(pcibr_soft_t, int, ioerror_mode_t, ioerror_t *);
  250. extern int              pcibr_dmard_error(pcibr_soft_t, int, ioerror_mode_t, ioerror_t *);
  251. extern int              pcibr_dmawr_error(pcibr_soft_t, int, ioerror_mode_t, ioerror_t *);
  252. extern int              pcibr_error_handler(error_handler_arg_t, int, ioerror_mode_t, ioerror_t *);
  253. extern int              pcibr_error_devenable(devfs_handle_t, int);
  254. void                    pcibr_provider_startup(devfs_handle_t);
  255. void                    pcibr_provider_shutdown(devfs_handle_t);
  256. int                     pcibr_reset(devfs_handle_t);
  257. pciio_endian_t          pcibr_endian_set(devfs_handle_t, pciio_endian_t, pciio_endian_t);
  258. int                     pcibr_priority_bits_set(pcibr_soft_t, pciio_slot_t, pciio_priority_t);
  259. pciio_priority_t        pcibr_priority_set(devfs_handle_t, pciio_priority_t);
  260. int                     pcibr_device_flags_set(devfs_handle_t, pcibr_device_flags_t);
  261. extern cfg_p            pcibr_config_addr(devfs_handle_t, unsigned);
  262. extern uint64_t         pcibr_config_get(devfs_handle_t, unsigned, unsigned);
  263. extern void             pcibr_config_set(devfs_handle_t, unsigned, unsigned, uint64_t);
  264. extern void             do_pcibr_config_set(cfg_p, unsigned, unsigned, uint64_t);
  265. extern pcibr_hints_t    pcibr_hints_get(devfs_handle_t, int);
  266. extern void             pcibr_hints_fix_rrbs(devfs_handle_t);
  267. extern void             pcibr_hints_dualslot(devfs_handle_t, pciio_slot_t, pciio_slot_t);
  268. extern void   pcibr_hints_intr_bits(devfs_handle_t, pcibr_intr_bits_f *);
  269. extern void             pcibr_set_rrb_callback(devfs_handle_t, rrb_alloc_funct_t);
  270. extern void             pcibr_hints_handsoff(devfs_handle_t);
  271. extern void             pcibr_hints_subdevs(devfs_handle_t, pciio_slot_t, uint64_t);
  272. #ifdef BRIDGE_B_DATACORR_WAR
  273. extern int              ql_bridge_rev_b_war(devfs_handle_t);
  274. extern int              bridge_rev_b_data_check_disable;
  275. char                   *rev_b_datacorr_warning =
  276. "***************************** WARNING! ******************************n";
  277. char                   *rev_b_datacorr_mesg =
  278. "UNRECOVERABLE IO LINK ERROR. CONTACT SERVICE PROVIDERn";
  279. #endif
  280. extern int pcibr_slot_reset(devfs_handle_t,pciio_slot_t);
  281. extern int pcibr_slot_info_init(devfs_handle_t,pciio_slot_t);
  282. extern int pcibr_slot_info_free(devfs_handle_t,pciio_slot_t);
  283. extern int pcibr_slot_addr_space_init(devfs_handle_t,pciio_slot_t);
  284. extern int pcibr_slot_device_init(devfs_handle_t, pciio_slot_t);
  285. extern int pcibr_slot_guest_info_init(devfs_handle_t,pciio_slot_t);
  286. extern int pcibr_slot_call_device_attach(devfs_handle_t, pciio_slot_t, int);
  287. extern int pcibr_slot_call_device_detach(devfs_handle_t, pciio_slot_t, int);
  288. extern int              pcibr_slot_attach(devfs_handle_t, pciio_slot_t, int, char *, int *);
  289. extern int              pcibr_slot_detach(devfs_handle_t, pciio_slot_t, int);
  290. extern int  pcibr_is_slot_sys_critical(devfs_handle_t, pciio_slot_t);
  291. #ifdef LATER
  292. extern int pcibr_slot_startup(devfs_handle_t, pcibr_slot_req_t);
  293. extern int pcibr_slot_shutdown(devfs_handle_t, pcibr_slot_req_t);
  294. extern int pcibr_slot_query(devfs_handle_t, pcibr_slot_req_t);
  295. #endif
  296. extern int pcibr_slot_initial_rrb_alloc(devfs_handle_t, pciio_slot_t);
  297. extern int pcibr_initial_rrb(devfs_handle_t, pciio_slot_t, pciio_slot_t);
  298. /* =====================================================================
  299.  *    Device(x) register management
  300.  */
  301. /* pcibr_try_set_device: attempt to modify Device(x)
  302.  * for the specified slot on the specified bridge
  303.  * as requested in flags, limited to the specified
  304.  * bits. Returns which BRIDGE bits were in conflict,
  305.  * or ZERO if everything went OK.
  306.  *
  307.  * Caller MUST hold pcibr_lock when calling this function.
  308.  */
  309. static int
  310. pcibr_try_set_device(pcibr_soft_t pcibr_soft,
  311.      pciio_slot_t slot,
  312.      unsigned flags,
  313.      bridgereg_t mask)
  314. {
  315.     bridge_t               *bridge;
  316.     pcibr_soft_slot_t       slotp;
  317.     bridgereg_t             old;
  318.     bridgereg_t             new;
  319.     bridgereg_t             chg;
  320.     bridgereg_t             bad;
  321.     bridgereg_t             badpmu;
  322.     bridgereg_t             badd32;
  323.     bridgereg_t             badd64;
  324.     bridgereg_t             fix;
  325.     unsigned long           s;
  326.     bridgereg_t             xmask;
  327.     xmask = mask;
  328.     if (pcibr_soft->bs_xbridge) {
  329.      if (mask == BRIDGE_DEV_PMU_BITS)
  330. xmask = XBRIDGE_DEV_PMU_BITS;
  331. if (mask == BRIDGE_DEV_D64_BITS)
  332. xmask = XBRIDGE_DEV_D64_BITS;
  333.     }
  334.     slotp = &pcibr_soft->bs_slot[slot];
  335.     s = pcibr_lock(pcibr_soft);
  336.     bridge = pcibr_soft->bs_base;
  337.     old = slotp->bss_device;
  338.     /* figure out what the desired
  339.      * Device(x) bits are based on
  340.      * the flags specified.
  341.      */
  342.     new = old;
  343.     /* Currently, we inherit anything that
  344.      * the new caller has not specified in
  345.      * one way or another, unless we take
  346.      * action here to not inherit.
  347.      *
  348.      * This is needed for the "swap" stuff,
  349.      * since it could have been set via
  350.      * pcibr_endian_set -- altho note that
  351.      * any explicit PCIBR_BYTE_STREAM or
  352.      * PCIBR_WORD_VALUES will freely override
  353.      * the effect of that call (and vice
  354.      * versa, no protection either way).
  355.      *
  356.      * I want to get rid of pcibr_endian_set
  357.      * in favor of tracking DMA endianness
  358.      * using the flags specified when DMA
  359.      * channels are created.
  360.      */
  361. #define BRIDGE_DEV_WRGA_BITS (BRIDGE_DEV_PMU_WRGA_EN | BRIDGE_DEV_DIR_WRGA_EN)
  362. #define BRIDGE_DEV_SWAP_BITS (BRIDGE_DEV_SWAP_PMU | BRIDGE_DEV_SWAP_DIR)
  363.     /* Do not use Barrier, Write Gather,
  364.      * or Prefetch unless asked.
  365.      * Leave everything else as it
  366.      * was from the last time.
  367.      */
  368.     new = new
  369. & ~BRIDGE_DEV_BARRIER
  370. & ~BRIDGE_DEV_WRGA_BITS
  371. & ~BRIDGE_DEV_PREF
  372. ;
  373.     /* Generic macro flags
  374.      */
  375.     if (flags & PCIIO_DMA_DATA) {
  376. new = (new
  377.             & ~BRIDGE_DEV_BARRIER)      /* barrier off */
  378.             | BRIDGE_DEV_PREF;          /* prefetch on */
  379.     }
  380.     if (flags & PCIIO_DMA_CMD) {
  381.         new = ((new
  382.             & ~BRIDGE_DEV_PREF)         /* prefetch off */
  383.             & ~BRIDGE_DEV_WRGA_BITS)    /* write gather off */
  384.             | BRIDGE_DEV_BARRIER;       /* barrier on */
  385.     }
  386.     /* Generic detail flags
  387.      */
  388.     if (flags & PCIIO_WRITE_GATHER)
  389. new |= BRIDGE_DEV_WRGA_BITS;
  390.     if (flags & PCIIO_NOWRITE_GATHER)
  391. new &= ~BRIDGE_DEV_WRGA_BITS;
  392.     if (flags & PCIIO_PREFETCH)
  393. new |= BRIDGE_DEV_PREF;
  394.     if (flags & PCIIO_NOPREFETCH)
  395. new &= ~BRIDGE_DEV_PREF;
  396.     if (flags & PCIBR_WRITE_GATHER)
  397. new |= BRIDGE_DEV_WRGA_BITS;
  398.     if (flags & PCIBR_NOWRITE_GATHER)
  399. new &= ~BRIDGE_DEV_WRGA_BITS;
  400.     if (flags & PCIIO_BYTE_STREAM)
  401. new |= (pcibr_soft->bs_xbridge) ? 
  402. BRIDGE_DEV_SWAP_DIR : BRIDGE_DEV_SWAP_BITS;
  403.     if (flags & PCIIO_WORD_VALUES)
  404. new &= (pcibr_soft->bs_xbridge) ? 
  405. ~BRIDGE_DEV_SWAP_DIR : ~BRIDGE_DEV_SWAP_BITS;
  406.     /* Provider-specific flags
  407.      */
  408.     if (flags & PCIBR_PREFETCH)
  409. new |= BRIDGE_DEV_PREF;
  410.     if (flags & PCIBR_NOPREFETCH)
  411. new &= ~BRIDGE_DEV_PREF;
  412.     if (flags & PCIBR_PRECISE)
  413. new |= BRIDGE_DEV_PRECISE;
  414.     if (flags & PCIBR_NOPRECISE)
  415. new &= ~BRIDGE_DEV_PRECISE;
  416.     if (flags & PCIBR_BARRIER)
  417. new |= BRIDGE_DEV_BARRIER;
  418.     if (flags & PCIBR_NOBARRIER)
  419. new &= ~BRIDGE_DEV_BARRIER;
  420.     if (flags & PCIBR_64BIT)
  421. new |= BRIDGE_DEV_DEV_SIZE;
  422.     if (flags & PCIBR_NO64BIT)
  423. new &= ~BRIDGE_DEV_DEV_SIZE;
  424.     chg = old ^ new; /* what are we changing, */
  425.     chg &= xmask; /* of the interesting bits */
  426.     if (chg) {
  427. badd32 = slotp->bss_d32_uctr ? (BRIDGE_DEV_D32_BITS & chg) : 0;
  428. if (pcibr_soft->bs_xbridge) {
  429. badpmu = slotp->bss_pmu_uctr ? (XBRIDGE_DEV_PMU_BITS & chg) : 0;
  430. badd64 = slotp->bss_d64_uctr ? (XBRIDGE_DEV_D64_BITS & chg) : 0;
  431. } else {
  432. badpmu = slotp->bss_pmu_uctr ? (BRIDGE_DEV_PMU_BITS & chg) : 0;
  433. badd64 = slotp->bss_d64_uctr ? (BRIDGE_DEV_D64_BITS & chg) : 0;
  434. }
  435. bad = badpmu | badd32 | badd64;
  436. if (bad) {
  437.     /* some conflicts can be resolved by
  438.      * forcing the bit on. this may cause
  439.      * some performance degredation in
  440.      * the stream(s) that want the bit off,
  441.      * but the alternative is not allowing
  442.      * the new stream at all.
  443.      */
  444.             if ( (fix = bad & (BRIDGE_DEV_PRECISE |
  445.                              BRIDGE_DEV_BARRIER)) ){
  446. bad &= ~fix;
  447. /* don't change these bits if
  448.  * they are already set in "old"
  449.  */
  450. chg &= ~(fix & old);
  451.     }
  452.     /* some conflicts can be resolved by
  453.      * forcing the bit off. this may cause
  454.      * some performance degredation in
  455.      * the stream(s) that want the bit on,
  456.      * but the alternative is not allowing
  457.      * the new stream at all.
  458.      */
  459.     if ( (fix = bad & (BRIDGE_DEV_WRGA_BITS |
  460.      BRIDGE_DEV_PREF)) ) {
  461. bad &= ~fix;
  462. /* don't change these bits if
  463.  * we wanted to turn them on.
  464.  */
  465. chg &= ~(fix & new);
  466.     }
  467.     /* conflicts in other bits mean
  468.      * we can not establish this DMA
  469.      * channel while the other(s) are
  470.      * still present.
  471.      */
  472.     if (bad) {
  473. pcibr_unlock(pcibr_soft, s);
  474. #if (DEBUG && PCIBR_DEV_DEBUG)
  475. printk("pcibr_try_set_device: mod blocked by %Rn", bad, device_bits);
  476. #endif
  477. return bad;
  478.     }
  479. }
  480.     }
  481.     if (mask == BRIDGE_DEV_PMU_BITS)
  482. slotp->bss_pmu_uctr++;
  483.     if (mask == BRIDGE_DEV_D32_BITS)
  484. slotp->bss_d32_uctr++;
  485.     if (mask == BRIDGE_DEV_D64_BITS)
  486. slotp->bss_d64_uctr++;
  487.     /* the value we want to write is the
  488.      * original value, with the bits for
  489.      * our selected changes flipped, and
  490.      * with any disabled features turned off.
  491.      */
  492.     new = old ^ chg; /* only change what we want to change */
  493.     if (slotp->bss_device == new) {
  494. pcibr_unlock(pcibr_soft, s);
  495. return 0;
  496.     }
  497.     bridge->b_device[slot].reg = new;
  498.     slotp->bss_device = new;
  499.     bridge->b_wid_tflush; /* wait until Bridge PIO complete */
  500.     pcibr_unlock(pcibr_soft, s);
  501. #if DEBUG && PCIBR_DEV_DEBUG
  502.     printk("pcibr Device(%d): 0x%pn", slot, bridge->b_device[slot].reg);
  503. #endif
  504.     return 0;
  505. }
  506. void
  507. pcibr_release_device(pcibr_soft_t pcibr_soft,
  508.      pciio_slot_t slot,
  509.      bridgereg_t mask)
  510. {
  511.     pcibr_soft_slot_t       slotp;
  512.     unsigned long           s;
  513.     slotp = &pcibr_soft->bs_slot[slot];
  514.     s = pcibr_lock(pcibr_soft);
  515.     if (mask == BRIDGE_DEV_PMU_BITS)
  516. slotp->bss_pmu_uctr--;
  517.     if (mask == BRIDGE_DEV_D32_BITS)
  518. slotp->bss_d32_uctr--;
  519.     if (mask == BRIDGE_DEV_D64_BITS)
  520. slotp->bss_d64_uctr--;
  521.     pcibr_unlock(pcibr_soft, s);
  522. }
  523. /*
  524.  * flush write gather buffer for slot
  525.  */
  526. static void
  527. pcibr_device_write_gather_flush(pcibr_soft_t pcibr_soft,
  528.               pciio_slot_t slot)
  529. {
  530.     bridge_t               *bridge;
  531.     unsigned long          s;
  532.     volatile uint32_t     wrf;
  533.     s = pcibr_lock(pcibr_soft);
  534.     bridge = pcibr_soft->bs_base;
  535.     wrf = bridge->b_wr_req_buf[slot].reg;
  536.     pcibr_unlock(pcibr_soft, s);
  537. }
  538. /* =====================================================================
  539.  *    Bridge (pcibr) "Device Driver" entry points
  540.  */
  541. /*
  542.  *    pcibr_init: called once during system startup or
  543.  *      when a loadable driver is loaded.
  544.  *
  545.  *      The driver_register function should normally
  546.  *      be in _reg, not _init.  But the pcibr driver is
  547.  *      required by devinit before the _reg routines
  548.  *      are called, so this is an exception.
  549.  */
  550. void
  551. pcibr_init(void)
  552. {
  553. #if DEBUG && ATTACH_DEBUG
  554.     printk("pcibr_initn");
  555. #endif
  556.     xwidget_driver_register(XBRIDGE_WIDGET_PART_NUM,
  557.     XBRIDGE_WIDGET_MFGR_NUM,
  558.     "pcibr_",
  559.     0);
  560.     xwidget_driver_register(BRIDGE_WIDGET_PART_NUM,
  561.     BRIDGE_WIDGET_MFGR_NUM,
  562.     "pcibr_",
  563.     0);
  564. }
  565. /*
  566.  * open/close mmap/munmap interface would be used by processes
  567.  * that plan to map the PCI bridge, and muck around with the
  568.  * registers. This is dangerous to do, and will be allowed
  569.  * to a select brand of programs. Typically these are
  570.  * diagnostics programs, or some user level commands we may
  571.  * write to do some weird things.
  572.  * To start with expect them to have root priveleges.
  573.  * We will ask for more later.
  574.  */
  575. /* ARGSUSED */
  576. int
  577. pcibr_open(devfs_handle_t *devp, int oflag, int otyp, cred_t *credp)
  578. {
  579.     return 0;
  580. }
  581. /*ARGSUSED */
  582. int
  583. pcibr_close(devfs_handle_t dev, int oflag, int otyp, cred_t *crp)
  584. {
  585.     return 0;
  586. }
  587. /*ARGSUSED */
  588. int
  589. pcibr_map(devfs_handle_t dev, vhandl_t *vt, off_t off, size_t len, uint prot)
  590. {
  591.     int                     error;
  592.     devfs_handle_t            vhdl = dev_to_vhdl(dev);
  593.     devfs_handle_t            pcibr_vhdl = hwgraph_connectpt_get(vhdl);
  594.     pcibr_soft_t            pcibr_soft = pcibr_soft_get(pcibr_vhdl);
  595.     bridge_t               *bridge = pcibr_soft->bs_base;
  596.     hwgraph_vertex_unref(pcibr_vhdl);
  597.     ASSERT(pcibr_soft);
  598.     len = ctob(btoc(len)); /* Make len page aligned */
  599.     error = v_mapphys(vt, (void *) ((__psunsigned_t) bridge + off), len);
  600.     /*
  601.      * If the offset being mapped corresponds to the flash prom
  602.      * base, and if the mapping succeeds, and if the user
  603.      * has requested the protections to be WRITE, enable the
  604.      * flash prom to be written.
  605.      *
  606.      * XXX- deprecate this in favor of using the
  607.      * real flash driver ...
  608.      */
  609.     if (!error &&
  610. ((off == BRIDGE_EXTERNAL_FLASH) ||
  611.  (len > BRIDGE_EXTERNAL_FLASH))) {
  612. int                     s;
  613. /*
  614.  * ensure that we write and read without any interruption.
  615.  * The read following the write is required for the Bridge war
  616.  */
  617. s = splhi();
  618. bridge->b_wid_control |= BRIDGE_CTRL_FLASH_WR_EN;
  619. bridge->b_wid_control; /* inval addr bug war */
  620. splx(s);
  621.     }
  622.     return error;
  623. }
  624. /*ARGSUSED */
  625. int
  626. pcibr_unmap(devfs_handle_t dev, vhandl_t *vt)
  627. {
  628.     devfs_handle_t            pcibr_vhdl = hwgraph_connectpt_get((devfs_handle_t) dev);
  629.     pcibr_soft_t            pcibr_soft = pcibr_soft_get(pcibr_vhdl);
  630.     bridge_t               *bridge = pcibr_soft->bs_base;
  631.     hwgraph_vertex_unref(pcibr_vhdl);
  632.     /*
  633.      * If flashprom write was enabled, disable it, as
  634.      * this is the last unmap.
  635.      */
  636.     if (bridge->b_wid_control & BRIDGE_CTRL_FLASH_WR_EN) {
  637. int                     s;
  638. /*
  639.  * ensure that we write and read without any interruption.
  640.  * The read following the write is required for the Bridge war
  641.  */
  642. s = splhi();
  643. bridge->b_wid_control &= ~BRIDGE_CTRL_FLASH_WR_EN;
  644. bridge->b_wid_control; /* inval addr bug war */
  645. splx(s);
  646.     }
  647.     return 0;
  648. }
  649. /* This is special case code used by grio. There are plans to make
  650.  * this a bit more general in the future, but till then this should
  651.  * be sufficient.
  652.  */
  653. pciio_slot_t
  654. pcibr_device_slot_get(devfs_handle_t dev_vhdl)
  655. {
  656.     char                    devname[MAXDEVNAME];
  657.     devfs_handle_t            tdev;
  658.     pciio_info_t            pciio_info;
  659.     pciio_slot_t            slot = PCIIO_SLOT_NONE;
  660.     vertex_to_name(dev_vhdl, devname, MAXDEVNAME);
  661.     /* run back along the canonical path
  662.      * until we find a PCI connection point.
  663.      */
  664.     tdev = hwgraph_connectpt_get(dev_vhdl);
  665.     while (tdev != GRAPH_VERTEX_NONE) {
  666. pciio_info = pciio_info_chk(tdev);
  667. if (pciio_info) {
  668.     slot = pciio_info_slot_get(pciio_info);
  669.     break;
  670. }
  671. hwgraph_vertex_unref(tdev);
  672. tdev = hwgraph_connectpt_get(tdev);
  673.     }
  674.     hwgraph_vertex_unref(tdev);
  675.     return slot;
  676. }
  677. /*ARGSUSED */
  678. int
  679. pcibr_ioctl(devfs_handle_t dev,
  680.     int cmd,
  681.     void *arg,
  682.     int flag,
  683.     struct cred *cr,
  684.     int *rvalp)
  685. {
  686.     devfs_handle_t            pcibr_vhdl = hwgraph_connectpt_get((devfs_handle_t)dev);
  687. #ifdef LATER
  688.     pcibr_soft_t            pcibr_soft = pcibr_soft_get(pcibr_vhdl);
  689. #endif
  690.     int                     error = 0;
  691.     hwgraph_vertex_unref(pcibr_vhdl);
  692.     switch (cmd) {
  693. #ifdef LATER
  694.     case GIOCSETBW:
  695. {
  696.     grio_ioctl_info_t       info;
  697.     pciio_slot_t            slot = 0;
  698.     if (!cap_able((uint64_t)CAP_DEVICE_MGT)) {
  699. error = EPERM;
  700. break;
  701.     }
  702.     if (COPYIN(arg, &info, sizeof(grio_ioctl_info_t))) {
  703. error = EFAULT;
  704. break;
  705.     }
  706. #ifdef GRIO_DEBUG
  707.     printk("pcibr:: prev_vhdl: %d reqbw: %lldn",
  708.     info.prev_vhdl, info.reqbw);
  709. #endif /* GRIO_DEBUG */
  710.     if ((slot = pcibr_device_slot_get(info.prev_vhdl)) ==
  711. PCIIO_SLOT_NONE) {
  712. error = EIO;
  713. break;
  714.     }
  715.     if (info.reqbw)
  716. pcibr_priority_bits_set(pcibr_soft, slot, PCI_PRIO_HIGH);
  717.     break;
  718. }
  719.     case GIOCRELEASEBW:
  720. {
  721.     grio_ioctl_info_t       info;
  722.     pciio_slot_t            slot = 0;
  723.     if (!cap_able(CAP_DEVICE_MGT)) {
  724. error = EPERM;
  725. break;
  726.     }
  727.     if (COPYIN(arg, &info, sizeof(grio_ioctl_info_t))) {
  728. error = EFAULT;
  729. break;
  730.     }
  731. #ifdef GRIO_DEBUG
  732.     printk("pcibr:: prev_vhdl: %d reqbw: %lldn",
  733.     info.prev_vhdl, info.reqbw);
  734. #endif /* GRIO_DEBUG */
  735.     if ((slot = pcibr_device_slot_get(info.prev_vhdl)) ==
  736. PCIIO_SLOT_NONE) {
  737. error = EIO;
  738. break;
  739.     }
  740.     if (info.reqbw)
  741. pcibr_priority_bits_set(pcibr_soft, slot, PCI_PRIO_LOW);
  742.     break;
  743. }
  744.     case PCIBR_SLOT_STARTUP:
  745. {
  746.     struct pcibr_slot_req_s        req;
  747.     if (!cap_able(CAP_DEVICE_MGT)) {
  748. error = EPERM;
  749. break;
  750.     }
  751.             if (COPYIN(arg, &req, sizeof(req))) {
  752.                 error = EFAULT;
  753.                 break;
  754.             }
  755.     error = pcibr_slot_startup(pcibr_vhdl, &req);
  756.     break;
  757. }
  758.     case PCIBR_SLOT_SHUTDOWN:
  759. {
  760.     struct pcibr_slot_req_s        req;
  761.     if (!cap_able(CAP_DEVICE_MGT)) {
  762. error = EPERM;
  763. break;
  764.     }
  765.             if (COPYIN(arg, &req, sizeof(req))) {
  766.                 error = EFAULT;
  767.                 break;
  768.             }
  769.     error = pcibr_slot_shutdown(pcibr_vhdl, &req);
  770.     break;
  771. }
  772.     case PCIBR_SLOT_QUERY:
  773. {
  774.     struct pcibr_slot_req_s        req;
  775.     if (!cap_able(CAP_DEVICE_MGT)) {
  776. error = EPERM;
  777. break;
  778.     }
  779.             if (COPYIN(arg, &req, sizeof(req))) {
  780.                 error = EFAULT;
  781.                 break;
  782.             }
  783.             error = pcibr_slot_query(pcibr_vhdl, &req);
  784.     break;
  785. }
  786. #endif /* LATER */
  787.     default:
  788. break;
  789.     }
  790.     return error;
  791. }
  792. void
  793. pcibr_freeblock_sub(iopaddr_t *free_basep,
  794.     iopaddr_t *free_lastp,
  795.     iopaddr_t base,
  796.     size_t size)
  797. {
  798.     iopaddr_t               free_base = *free_basep;
  799.     iopaddr_t               free_last = *free_lastp;
  800.     iopaddr_t               last = base + size - 1;
  801.     if ((last < free_base) || (base > free_last)); /* free block outside arena */
  802.     else if ((base <= free_base) && (last >= free_last))
  803. /* free block contains entire arena */
  804. *free_basep = *free_lastp = 0;
  805.     else if (base <= free_base)
  806. /* free block is head of arena */
  807. *free_basep = last + 1;
  808.     else if (last >= free_last)
  809. /* free block is tail of arena */
  810. *free_lastp = base - 1;
  811.     /*
  812.      * We are left with two regions: the free area
  813.      * in the arena "below" the block, and the free
  814.      * area in the arena "above" the block. Keep
  815.      * the one that is bigger.
  816.      */
  817.     else if ((base - free_base) > (free_last - last))
  818. *free_lastp = base - 1; /* keep lower chunk */
  819.     else
  820. *free_basep = last + 1; /* keep upper chunk */
  821. }
  822. pcibr_info_t
  823. pcibr_info_get(devfs_handle_t vhdl)
  824. {
  825.     return (pcibr_info_t) pciio_info_get(vhdl);
  826. }
  827. pcibr_info_t
  828. pcibr_device_info_new(
  829.  pcibr_soft_t pcibr_soft,
  830.  pciio_slot_t slot,
  831.  pciio_function_t rfunc,
  832.  pciio_vendor_id_t vendor,
  833.  pciio_device_id_t device)
  834. {
  835.     pcibr_info_t            pcibr_info;
  836.     pciio_function_t        func;
  837.     int                     ibit;
  838.     func = (rfunc == PCIIO_FUNC_NONE) ? 0 : rfunc;
  839.     NEW(pcibr_info);
  840.     pciio_device_info_new(&pcibr_info->f_c,
  841.   pcibr_soft->bs_vhdl,
  842.   slot, rfunc,
  843.   vendor, device);
  844. /* pfg - this is new ..... */
  845.     /* Set PCI bus number */
  846.     pcibr_info->f_bus = io_path_map_widget(pcibr_soft->bs_vhdl);
  847.     if (slot != PCIIO_SLOT_NONE) {
  848. /*
  849.  * Currently favored mapping from PCI
  850.  * slot number and INTA/B/C/D to Bridge
  851.  * PCI Interrupt Bit Number:
  852.  *
  853.  *     SLOT     A B C D
  854.  *      0       0 4 0 4
  855.  *      1       1 5 1 5
  856.  *      2       2 6 2 6
  857.  *      3       3 7 3 7
  858.  *      4       4 0 4 0
  859.  *      5       5 1 5 1
  860.  *      6       6 2 6 2
  861.  *      7       7 3 7 3
  862.  *
  863.  * XXX- allow pcibr_hints to override default
  864.  * XXX- allow ADMIN to override pcibr_hints
  865.  */
  866. for (ibit = 0; ibit < 4; ++ibit)
  867.     pcibr_info->f_ibit[ibit] =
  868. (slot + 4 * ibit) & 7;
  869. /*
  870.  * Record the info in the sparse func info space.
  871.  */
  872. if (func < pcibr_soft->bs_slot[slot].bss_ninfo)
  873.     pcibr_soft->bs_slot[slot].bss_infos[func] = pcibr_info;
  874.     }
  875.     return pcibr_info;
  876. }
  877. /* FIXME:  for now this is needed by both pcibr.c and
  878.  * pcibr_slot.c.  Need to find a better way, the least
  879.  * of which would be to move it to pcibr_private.h
  880.  */
  881. /*
  882.  * PCI_ADDR_SPACE_LIMITS_STORE
  883.  * Sets the current values of
  884.  * pci io base, 
  885.  * pci io last,
  886.  * pci low memory base,
  887.  * pci low memory last,
  888.  * pci high memory base,
  889.  *  pci high memory last
  890.  */
  891. #define PCI_ADDR_SPACE_LIMITS_STORE()
  892.     pcibr_soft->bs_spinfo.pci_io_base = pci_io_fb;
  893.     pcibr_soft->bs_spinfo.pci_io_last = pci_io_fl;
  894.     pcibr_soft->bs_spinfo.pci_swin_base = pci_lo_fb;
  895.     pcibr_soft->bs_spinfo.pci_swin_last = pci_lo_fl;
  896.     pcibr_soft->bs_spinfo.pci_mem_base = pci_hi_fb;
  897.     pcibr_soft->bs_spinfo.pci_mem_last = pci_hi_fl;
  898. /*
  899.  * pcibr_device_unregister
  900.  * This frees up any hardware resources reserved for this PCI device
  901.  *  and removes any PCI infrastructural information setup for it.
  902.  * This is usually used at the time of shutting down of the PCI card.
  903.  */
  904. int
  905. pcibr_device_unregister(devfs_handle_t pconn_vhdl)
  906. {
  907.     pciio_info_t  pciio_info;
  908.     devfs_handle_t  pcibr_vhdl;
  909.     pciio_slot_t  slot;
  910.     pcibr_soft_t  pcibr_soft;
  911.     bridge_t *bridge;
  912.     int                  count_vchan0, count_vchan1;
  913.     unsigned             s;
  914.     int  error_call;
  915.     int  error = 0;
  916.     pciio_info = pciio_info_get(pconn_vhdl);
  917.     pcibr_vhdl = pciio_info_master_get(pciio_info);
  918.     slot = pciio_info_slot_get(pciio_info);
  919.     pcibr_soft = pcibr_soft_get(pcibr_vhdl);
  920.     bridge = pcibr_soft->bs_base;
  921.     /* Clear all the hardware xtalk resources for this device */
  922.     xtalk_widgetdev_shutdown(pcibr_soft->bs_conn, slot);
  923.     /* Flush all the rrbs */
  924.     pcibr_rrb_flush(pconn_vhdl);
  925.     /*
  926.      * If the RRB configuration for this slot has changed, set it 
  927.      * back to the boot-time default
  928.      */
  929.     if (pcibr_soft->bs_rrb_valid_dflt[slot] >= 0) {
  930.         s = pcibr_lock(pcibr_soft);
  931.         /* Free the rrbs allocated to this slot */
  932.         error_call = do_pcibr_rrb_free(bridge, slot, 
  933.                        pcibr_soft->bs_rrb_valid[slot] +
  934.                        pcibr_soft->bs_rrb_valid[slot + 
  935.                                        PCIBR_RRB_SLOT_VIRTUAL]);
  936.         if (error_call)
  937.             error = ERANGE;
  938.    
  939.          pcibr_soft->bs_rrb_res[slot] = pcibr_soft->bs_rrb_res[slot] +
  940.                                         pcibr_soft->bs_rrb_valid[slot] +
  941.                                         pcibr_soft->bs_rrb_valid[slot +
  942.                                         PCIBR_RRB_SLOT_VIRTUAL];
  943.         count_vchan0 = pcibr_soft->bs_rrb_valid_dflt[slot];
  944.         count_vchan1 = pcibr_soft->bs_rrb_valid_dflt[slot +
  945.                                                      PCIBR_RRB_SLOT_VIRTUAL];
  946.         pcibr_unlock(pcibr_soft, s);
  947.         pcibr_rrb_alloc(pconn_vhdl, &count_vchan0, &count_vchan1);
  948.     }
  949.     /* Flush the write buffers !! */
  950.     error_call = pcibr_wrb_flush(pconn_vhdl);
  951.     if (error_call)
  952.         error = error_call;
  953.     /* Clear the information specific to the slot */
  954.     error_call = pcibr_slot_info_free(pcibr_vhdl, slot);
  955.     if (error_call)
  956.         error = error_call;
  957.     return(error);
  958.     
  959. }
  960. /*
  961.  * pcibr_driver_reg_callback
  962.  *      CDL will call this function for each device found in the PCI
  963.  *      registry that matches the vendor/device IDs supported by 
  964.  *      the driver being registered.  The device's connection vertex
  965.  *      and the driver's attach function return status enable the
  966.  *      slot's device status to be set.
  967.  */
  968. void
  969. pcibr_driver_reg_callback(devfs_handle_t pconn_vhdl,
  970.   int key1, int key2, int error)
  971. {
  972.     pciio_info_t  pciio_info;
  973.     pcibr_info_t         pcibr_info;
  974.     devfs_handle_t  pcibr_vhdl;
  975.     pciio_slot_t  slot;
  976.     pcibr_soft_t  pcibr_soft;
  977.     /* Do not set slot status for vendor/device ID wildcard drivers */
  978.     if ((key1 == -1) || (key2 == -1))
  979.         return;
  980.     pciio_info = pciio_info_get(pconn_vhdl);
  981.     pcibr_info = pcibr_info_get(pconn_vhdl);
  982.     pcibr_vhdl = pciio_info_master_get(pciio_info);
  983.     slot = pciio_info_slot_get(pciio_info);
  984.     pcibr_soft = pcibr_soft_get(pcibr_vhdl);
  985.     /* This may be a loadable driver so lock out any pciconfig actions */
  986.     mrlock(pcibr_soft->bs_bus_lock, MR_UPDATE, PZERO);
  987.     pcibr_info->f_att_det_error = error;
  988.     pcibr_soft->bs_slot[slot].slot_status &= ~SLOT_STATUS_MASK;
  989.     if (error) {
  990.         pcibr_soft->bs_slot[slot].slot_status |= SLOT_STARTUP_INCMPLT;
  991.     } else {
  992.         pcibr_soft->bs_slot[slot].slot_status |= SLOT_STARTUP_CMPLT;
  993.     }
  994.         
  995.     /* Release the bus lock */
  996.     mrunlock(pcibr_soft->bs_bus_lock);
  997. }
  998. /*
  999.  * pcibr_driver_unreg_callback
  1000.  *      CDL will call this function for each device found in the PCI
  1001.  *      registry that matches the vendor/device IDs supported by 
  1002.  *      the driver being unregistered.  The device's connection vertex
  1003.  *      and the driver's detach function return status enable the
  1004.  *      slot's device status to be set.
  1005.  */
  1006. void
  1007. pcibr_driver_unreg_callback(devfs_handle_t pconn_vhdl, 
  1008.                             int key1, int key2, int error)
  1009. {
  1010.     pciio_info_t  pciio_info;
  1011.     pcibr_info_t         pcibr_info;
  1012.     devfs_handle_t  pcibr_vhdl;
  1013.     pciio_slot_t  slot;
  1014.     pcibr_soft_t  pcibr_soft;
  1015.     /* Do not set slot status for vendor/device ID wildcard drivers */
  1016.     if ((key1 == -1) || (key2 == -1))
  1017.         return;
  1018.     pciio_info = pciio_info_get(pconn_vhdl);
  1019.     pcibr_info = pcibr_info_get(pconn_vhdl);
  1020.     pcibr_vhdl = pciio_info_master_get(pciio_info);
  1021.     slot = pciio_info_slot_get(pciio_info);
  1022.     pcibr_soft = pcibr_soft_get(pcibr_vhdl);
  1023.     /* This may be a loadable driver so lock out any pciconfig actions */
  1024.     mrlock(pcibr_soft->bs_bus_lock, MR_UPDATE, PZERO);
  1025.     pcibr_info->f_att_det_error = error;
  1026.     pcibr_soft->bs_slot[slot].slot_status &= ~SLOT_STATUS_MASK;
  1027.     if (error) {
  1028.         pcibr_soft->bs_slot[slot].slot_status |= SLOT_SHUTDOWN_INCMPLT;
  1029.     } else {
  1030.         pcibr_soft->bs_slot[slot].slot_status |= SLOT_SHUTDOWN_CMPLT;
  1031.     }
  1032.         
  1033.     /* Release the bus lock */
  1034.     mrunlock(pcibr_soft->bs_bus_lock);
  1035. }
  1036. /* 
  1037.  * build a convenience link path in the
  1038.  * form of ".../<iobrick>/bus/<busnum>"
  1039.  * 
  1040.  * returns 1 on success, 0 otherwise
  1041.  *
  1042.  * depends on hwgraph separator == '/'
  1043.  */
  1044. int
  1045. pcibr_bus_cnvlink(devfs_handle_t f_c, int slot)
  1046. {
  1047.         char dst[MAXDEVNAME];
  1048. char *dp = dst;
  1049.         char *cp, *xp;
  1050.         int widgetnum;
  1051.         char pcibus[8];
  1052. devfs_handle_t nvtx, svtx;
  1053. int rv;
  1054. #if DEBUG
  1055. printk("pcibr_bus_cnvlink: slot= %d f_c= %pn", 
  1056. slot, f_c);
  1057. {
  1058. int pos;
  1059. char dname[256];
  1060. pos = devfs_generate_path(f_c, dname, 256);
  1061. printk("%s : path= %sn", __FUNCTION__, &dname[pos]);
  1062. }
  1063. #endif
  1064. if (GRAPH_SUCCESS != hwgraph_vertex_name_get(f_c, dst, MAXDEVNAME))
  1065. return 0;
  1066. /* dst example == /hw/module/001c02/Pbrick/xtalk/8/pci/direct */
  1067. /* find the widget number */
  1068. xp = strstr(dst, "/"EDGE_LBL_XTALK"/");
  1069. if (xp == NULL)
  1070. return 0;
  1071. widgetnum = atoi(xp+7);
  1072. if (widgetnum < XBOW_PORT_8 || widgetnum > XBOW_PORT_F)
  1073. return 0;
  1074. /* remove "/pci/direct" from path */
  1075. cp = strstr(dst, "/" EDGE_LBL_PCI "/" "direct");
  1076. if (cp == NULL)
  1077. return 0;
  1078. *cp = (char)NULL;
  1079. /* get the vertex for the widget */
  1080. if (GRAPH_SUCCESS != hwgraph_traverse(NULL, dp, &svtx))
  1081. return 0;
  1082. *xp = (char)NULL; /* remove "/xtalk/..." from path */
  1083. /* dst example now == /hw/module/001c02/Pbrick */
  1084. /* get the bus number */
  1085.         strcat(dst, "/bus");
  1086.         sprintf(pcibus, "%d", p_busnum[widgetnum]);
  1087. /* link to bus to widget */
  1088. rv = hwgraph_path_add(NULL, dp, &nvtx);
  1089. if (GRAPH_SUCCESS == rv)
  1090. rv = hwgraph_edge_add(nvtx, svtx, pcibus);
  1091. return (rv == GRAPH_SUCCESS);
  1092. }
  1093. /*
  1094.  *    pcibr_attach: called every time the crosstalk
  1095.  *      infrastructure is asked to initialize a widget
  1096.  *      that matches the part number we handed to the
  1097.  *      registration routine above.
  1098.  */
  1099. /*ARGSUSED */
  1100. int
  1101. pcibr_attach(devfs_handle_t xconn_vhdl)
  1102. {
  1103.     /* REFERENCED */
  1104.     graph_error_t           rc;
  1105.     devfs_handle_t            pcibr_vhdl;
  1106.     devfs_handle_t            ctlr_vhdl;
  1107.     bridge_t               *bridge = NULL;
  1108.     bridgereg_t             id;
  1109.     int                     rev;
  1110.     pcibr_soft_t            pcibr_soft;
  1111.     pcibr_info_t            pcibr_info;
  1112.     xwidget_info_t          info;
  1113.     xtalk_intr_t            xtalk_intr;
  1114.     device_desc_t           dev_desc = (device_desc_t)0;
  1115.     int                     slot;
  1116.     int                     ibit;
  1117.     devfs_handle_t            noslot_conn;
  1118.     char                    devnm[MAXDEVNAME], *s;
  1119.     pcibr_hints_t           pcibr_hints;
  1120.     bridgereg_t             b_int_enable;
  1121.     unsigned                rrb_fixed = 0;
  1122.     iopaddr_t               pci_io_fb, pci_io_fl;
  1123.     iopaddr_t               pci_lo_fb, pci_lo_fl;
  1124.     iopaddr_t               pci_hi_fb, pci_hi_fl;
  1125.     int                     spl_level;
  1126. #ifdef LATER
  1127.     char     *nicinfo = (char *)0;
  1128. #endif
  1129. #if PCI_FBBE
  1130.     int                     fast_back_to_back_enable;
  1131. #endif
  1132.     l1sc_t     *scp;
  1133.     nasid_t     nasid;
  1134.     async_attach_t          aa = NULL;
  1135.     aa = async_attach_get_info(xconn_vhdl);
  1136. #if DEBUG && ATTACH_DEBUG
  1137.     printk("pcibr_attach: xconn_vhdl=  %pn", xconn_vhdl);
  1138.     {
  1139. int pos;
  1140. char dname[256];
  1141. pos = devfs_generate_path(xconn_vhdl, dname, 256);
  1142. printk("%s : path= %s n", __FUNCTION__, &dname[pos]);
  1143.     }
  1144. #endif
  1145.     /* Setup the PRB for the bridge in CONVEYOR BELT
  1146.      * mode. PRBs are setup in default FIRE-AND-FORGET
  1147.      * mode during the initialization.
  1148.      */
  1149.     hub_device_flags_set(xconn_vhdl, HUB_PIO_CONVEYOR);
  1150.     bridge = (bridge_t *)
  1151. xtalk_piotrans_addr(xconn_vhdl, NULL,
  1152.     0, sizeof(bridge_t), 0);
  1153.     /*
  1154.      * Create the vertex for the PCI bus, which we
  1155.      * will also use to hold the pcibr_soft and
  1156.      * which will be the "master" vertex for all the
  1157.      * pciio connection points we will hang off it.
  1158.      * This needs to happen before we call nic_bridge_vertex_info
  1159.      * as we are some of the *_vmc functions need access to the edges.
  1160.      *
  1161.      * Opening this vertex will provide access to
  1162.      * the Bridge registers themselves.
  1163.      */
  1164.     rc = hwgraph_path_add(xconn_vhdl, EDGE_LBL_PCI, &pcibr_vhdl);
  1165.     ASSERT(rc == GRAPH_SUCCESS);
  1166.     ctlr_vhdl = NULL;
  1167.     ctlr_vhdl = hwgraph_register(pcibr_vhdl, EDGE_LBL_CONTROLLER,
  1168.                 0, DEVFS_FL_AUTO_DEVNUM,
  1169.                 0, 0,
  1170.                 S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP, 0, 0,
  1171.                 &pcibr_fops, NULL);
  1172.     ASSERT(ctlr_vhdl != NULL);
  1173.     /*
  1174.      * decode the nic, and hang its stuff off our
  1175.      * connection point where other drivers can get
  1176.      * at it.
  1177.      */
  1178. #ifdef LATER
  1179.     nicinfo = BRIDGE_VERTEX_MFG_INFO(xconn_vhdl, (nic_data_t) & bridge->b_nic);
  1180. #endif
  1181.     /*
  1182.      * Get the hint structure; if some NIC callback
  1183.      * marked this vertex as "hands-off" then we
  1184.      * just return here, before doing anything else.
  1185.      */
  1186.     pcibr_hints = pcibr_hints_get(xconn_vhdl, 0);
  1187.     if (pcibr_hints && pcibr_hints->ph_hands_off)
  1188. return -1; /* generic operations disabled */
  1189.     id = bridge->b_wid_id;
  1190.     rev = XWIDGET_PART_REV_NUM(id);
  1191.     hwgraph_info_add_LBL(pcibr_vhdl, INFO_LBL_PCIBR_ASIC_REV, (arbitrary_info_t) rev);
  1192.     /*
  1193.      * allocate soft state structure, fill in some
  1194.      * fields, and hook it up to our vertex.
  1195.      */
  1196.     NEW(pcibr_soft);
  1197.     BZERO(pcibr_soft, sizeof *pcibr_soft);
  1198.     pcibr_soft_set(pcibr_vhdl, pcibr_soft);
  1199.     pcibr_soft->bs_conn = xconn_vhdl;
  1200.     pcibr_soft->bs_vhdl = pcibr_vhdl;
  1201.     pcibr_soft->bs_base = bridge;
  1202.     pcibr_soft->bs_rev_num = rev;
  1203.     pcibr_soft->bs_intr_bits = pcibr_intr_bits;
  1204.     if (is_xbridge(bridge)) {
  1205. pcibr_soft->bs_int_ate_size = XBRIDGE_INTERNAL_ATES;
  1206. pcibr_soft->bs_xbridge = 1;
  1207.     } else {
  1208. pcibr_soft->bs_int_ate_size = BRIDGE_INTERNAL_ATES;
  1209. pcibr_soft->bs_xbridge = 0;
  1210.     }
  1211.     nasid = NASID_GET(bridge);
  1212.     scp = &NODEPDA( NASID_TO_COMPACT_NODEID(nasid) )->module->elsc;
  1213.     pcibr_soft->bs_l1sc = scp;
  1214.     pcibr_soft->bs_moduleid = iobrick_module_get(scp);
  1215.     pcibr_soft->bsi_err_intr = 0;
  1216.     /* Bridges up through REV C
  1217.      * are unable to set the direct
  1218.      * byteswappers to BYTE_STREAM.
  1219.      */
  1220.     if (pcibr_soft->bs_rev_num <= BRIDGE_PART_REV_C) {
  1221. pcibr_soft->bs_pio_end_io = PCIIO_WORD_VALUES;
  1222. pcibr_soft->bs_pio_end_mem = PCIIO_WORD_VALUES;
  1223.     }
  1224. #if PCIBR_SOFT_LIST
  1225.     {
  1226. pcibr_list_p            self;
  1227. NEW(self);
  1228. self->bl_soft = pcibr_soft;
  1229. self->bl_vhdl = pcibr_vhdl;
  1230. self->bl_next = pcibr_list;
  1231. self->bl_next = swap_ptr((void **) &pcibr_list, (void *)self);
  1232.     }
  1233. #endif
  1234.     /*
  1235.      * get the name of this bridge vertex and keep the info. Use this
  1236.      * only where it is really needed now: like error interrupts.
  1237.      */
  1238.     s = dev_to_name(pcibr_vhdl, devnm, MAXDEVNAME);
  1239.     pcibr_soft->bs_name = kmalloc(strlen(s) + 1, GFP_KERNEL);
  1240.     strcpy(pcibr_soft->bs_name, s);
  1241. #if SHOW_REVS || DEBUG
  1242. #if !DEBUG
  1243.     if (kdebug)
  1244. #endif
  1245. printk("%sBridge ASIC: rev %s (code=0x%x) at %sn",
  1246. is_xbridge(bridge) ? "X" : "",
  1247. (rev == BRIDGE_PART_REV_A) ? "A" :
  1248. (rev == BRIDGE_PART_REV_B) ? "B" :
  1249. (rev == BRIDGE_PART_REV_C) ? "C" :
  1250. (rev == BRIDGE_PART_REV_D) ? "D" :
  1251. (rev == XBRIDGE_PART_REV_A) ? "A" :
  1252. (rev == XBRIDGE_PART_REV_B) ? "B" :
  1253. "unknown",
  1254. rev, pcibr_soft->bs_name);
  1255. #endif
  1256.     info = xwidget_info_get(xconn_vhdl);
  1257.     pcibr_soft->bs_xid = xwidget_info_id_get(info);
  1258.     pcibr_soft->bs_master = xwidget_info_master_get(info);
  1259.     pcibr_soft->bs_mxid = xwidget_info_masterid_get(info);
  1260.     /*
  1261.      * Init bridge lock.
  1262.      */
  1263.     spin_lock_init(&pcibr_soft->bs_lock);
  1264.     /*
  1265.      * If we have one, process the hints structure.
  1266.      */
  1267.     if (pcibr_hints) {
  1268. rrb_fixed = pcibr_hints->ph_rrb_fixed;
  1269. pcibr_soft->bs_rrb_fixed = rrb_fixed;
  1270. if (pcibr_hints->ph_intr_bits)
  1271.     pcibr_soft->bs_intr_bits = pcibr_hints->ph_intr_bits;
  1272. for (slot = 0; slot < 8; ++slot) {
  1273.     int                     hslot = pcibr_hints->ph_host_slot[slot] - 1;
  1274.     if (hslot < 0) {
  1275. pcibr_soft->bs_slot[slot].host_slot = slot;
  1276.     } else {
  1277. pcibr_soft->bs_slot[slot].has_host = 1;
  1278. pcibr_soft->bs_slot[slot].host_slot = hslot;
  1279.     }
  1280. }
  1281.     }
  1282.     /*
  1283.      * set up initial values for state fields
  1284.      */
  1285.     for (slot = 0; slot < 8; ++slot) {
  1286. pcibr_soft->bs_slot[slot].bss_devio.bssd_space = PCIIO_SPACE_NONE;
  1287. pcibr_soft->bs_slot[slot].bss_d64_base = PCIBR_D64_BASE_UNSET;
  1288. pcibr_soft->bs_slot[slot].bss_d32_base = PCIBR_D32_BASE_UNSET;
  1289. pcibr_soft->bs_slot[slot].bss_ext_ates_active = ATOMIC_INIT(0);
  1290.     }
  1291.     for (ibit = 0; ibit < 8; ++ibit) {
  1292. pcibr_soft->bs_intr[ibit].bsi_xtalk_intr = 0;
  1293. pcibr_soft->bs_intr[ibit].bsi_pcibr_intr_wrap.iw_soft = pcibr_soft;
  1294. pcibr_soft->bs_intr[ibit].bsi_pcibr_intr_wrap.iw_list = NULL;
  1295. pcibr_soft->bs_intr[ibit].bsi_pcibr_intr_wrap.iw_stat = 
  1296. &(bridge->b_int_status);
  1297. pcibr_soft->bs_intr[ibit].bsi_pcibr_intr_wrap.iw_hdlrcnt = 0;
  1298. pcibr_soft->bs_intr[ibit].bsi_pcibr_intr_wrap.iw_shared = 0;
  1299. pcibr_soft->bs_intr[ibit].bsi_pcibr_intr_wrap.iw_connected = 0;
  1300.     }
  1301.     /*
  1302.      * Initialize various Bridge registers.
  1303.      */
  1304.     /*
  1305.      * On pre-Rev.D bridges, set the PCI_RETRY_CNT
  1306.      * to zero to avoid dropping stores. (#475347)
  1307.      */
  1308.     if (rev < BRIDGE_PART_REV_D)
  1309. bridge->b_bus_timeout &= ~BRIDGE_BUS_PCI_RETRY_MASK;
  1310.     /*
  1311.      * Clear all pending interrupts.
  1312.      */
  1313.     bridge->b_int_rst_stat = (BRIDGE_IRR_ALL_CLR);
  1314.     /*
  1315.      * Until otherwise set up,
  1316.      * assume all interrupts are
  1317.      * from slot 7.
  1318.      */
  1319.     bridge->b_int_device = (uint32_t) 0xffffffff;
  1320.     {
  1321. bridgereg_t             dirmap;
  1322. paddr_t                 paddr;
  1323. iopaddr_t               xbase;
  1324. xwidgetnum_t            xport;
  1325. iopaddr_t               offset;
  1326. int                     num_entries = 0;
  1327. int                     entry;
  1328. cnodeid_t cnodeid;
  1329. nasid_t nasid;
  1330. /* Set the Bridge's 32-bit PCI to XTalk
  1331.  * Direct Map register to the most useful
  1332.  * value we can determine.  Note that we
  1333.  * must use a single xid for all of:
  1334.  *      direct-mapped 32-bit DMA accesses
  1335.  *      direct-mapped 64-bit DMA accesses
  1336.  *      DMA accesses through the PMU
  1337.  *      interrupts
  1338.  * This is the only way to guarantee that
  1339.  * completion interrupts will reach a CPU
  1340.  * after all DMA data has reached memory.
  1341.  * (Of course, there may be a few special
  1342.  * drivers/controlers that explicitly manage
  1343.  * this ordering problem.)
  1344.  */
  1345. cnodeid = 0;  /* default node id */
  1346. nasid = COMPACT_TO_NASID_NODEID(cnodeid);
  1347. paddr = NODE_OFFSET(nasid) + 0;
  1348. /* currently, we just assume that if we ask
  1349.  * for a DMA mapping to "zero" the XIO
  1350.  * host will transmute this into a request
  1351.  * for the lowest hunk of memory.
  1352.  */
  1353. xbase = xtalk_dmatrans_addr(xconn_vhdl, 0,
  1354.     paddr, _PAGESZ, 0);
  1355. if (xbase != XIO_NOWHERE) {
  1356.     if (XIO_PACKED(xbase)) {
  1357. xport = XIO_PORT(xbase);
  1358. xbase = XIO_ADDR(xbase);
  1359.     } else
  1360. xport = pcibr_soft->bs_mxid;
  1361.     offset = xbase & ((1ull << BRIDGE_DIRMAP_OFF_ADDRSHFT) - 1ull);
  1362.     xbase >>= BRIDGE_DIRMAP_OFF_ADDRSHFT;
  1363.     dirmap = xport << BRIDGE_DIRMAP_W_ID_SHFT;
  1364.     if (xbase)
  1365. dirmap |= BRIDGE_DIRMAP_OFF & xbase;
  1366.     else if (offset >= (512 << 20))
  1367. dirmap |= BRIDGE_DIRMAP_ADD512;
  1368.     bridge->b_dir_map = dirmap;
  1369. }
  1370. /*
  1371.  * Set bridge's idea of page size according to the system's
  1372.  * idea of "IO page size".  TBD: The idea of IO page size
  1373.  * should really go away.
  1374.  */
  1375. /*
  1376.  * ensure that we write and read without any interruption.
  1377.  * The read following the write is required for the Bridge war
  1378.  */
  1379. spl_level = splhi();
  1380. #if IOPGSIZE == 4096
  1381. bridge->b_wid_control &= ~BRIDGE_CTRL_PAGE_SIZE;
  1382. #elif IOPGSIZE == 16384
  1383. bridge->b_wid_control |= BRIDGE_CTRL_PAGE_SIZE;
  1384. #else
  1385. <<<Unable to deal with IOPGSIZE >>>;
  1386. #endif
  1387. bridge->b_wid_control; /* inval addr bug war */
  1388. splx(spl_level);
  1389. /* Initialize internal mapping entries */
  1390. for (entry = 0; entry < pcibr_soft->bs_int_ate_size; entry++) {
  1391.     bridge->b_int_ate_ram[entry].wr = 0;
  1392. }
  1393. /*
  1394.  * Determine if there's external mapping SSRAM on this
  1395.  * bridge.  Set up Bridge control register appropriately,
  1396.  * inititlize SSRAM, and set software up to manage RAM
  1397.  * entries as an allocatable resource.
  1398.  *
  1399.  * Currently, we just use the rm* routines to manage ATE
  1400.  * allocation.  We should probably replace this with a
  1401.  * Best Fit allocator.
  1402.  *
  1403.  * For now, if we have external SSRAM, avoid using
  1404.  * the internal ssram: we can't turn PREFETCH on
  1405.  * when we use the internal SSRAM; and besides,
  1406.  * this also guarantees that no allocation will
  1407.  * straddle the internal/external line, so we
  1408.  * can increment ATE write addresses rather than
  1409.  * recomparing against BRIDGE_INTERNAL_ATES every
  1410.  * time.
  1411.  */
  1412. if (is_xbridge(bridge))
  1413. num_entries = 0;
  1414. else
  1415. num_entries = pcibr_init_ext_ate_ram(bridge);
  1416. /* we always have 128 ATEs (512 for Xbridge) inside the chip
  1417.  * even if disabled for debugging.
  1418.  */
  1419. pcibr_soft->bs_int_ate_map = rmallocmap(pcibr_soft->bs_int_ate_size);
  1420. pcibr_ate_free(pcibr_soft, 0, pcibr_soft->bs_int_ate_size);
  1421. #if PCIBR_ATE_DEBUG
  1422. printk("pcibr_attach: %d INTERNAL ATEsn", pcibr_soft->bs_int_ate_size);
  1423. #endif
  1424. if (num_entries > pcibr_soft->bs_int_ate_size) {
  1425. #if PCIBR_ATE_NOTBOTH /* for debug -- forces us to use external ates */
  1426.     printk("pcibr_attach: disabling internal ATEs.n");
  1427.     pcibr_ate_alloc(pcibr_soft, pcibr_soft->bs_int_ate_size);
  1428. #endif
  1429.     pcibr_soft->bs_ext_ate_map = rmallocmap(num_entries);
  1430.     pcibr_ate_free(pcibr_soft, pcibr_soft->bs_int_ate_size,
  1431.    num_entries - pcibr_soft->bs_int_ate_size);
  1432. #if PCIBR_ATE_DEBUG
  1433.     printk("pcibr_attach: %d EXTERNAL ATEsn",
  1434.     num_entries - pcibr_soft->bs_int_ate_size);
  1435. #endif
  1436. }
  1437.     }
  1438.     {
  1439. bridgereg_t             dirmap;
  1440. iopaddr_t               xbase;
  1441. /*
  1442.  * now figure the *real* xtalk base address
  1443.  * that dirmap sends us to.
  1444.  */
  1445. dirmap = bridge->b_dir_map;
  1446. if (dirmap & BRIDGE_DIRMAP_OFF)
  1447.     xbase = (iopaddr_t)(dirmap & BRIDGE_DIRMAP_OFF)
  1448. << BRIDGE_DIRMAP_OFF_ADDRSHFT;
  1449. else if (dirmap & BRIDGE_DIRMAP_ADD512)
  1450.     xbase = 512 << 20;
  1451. else
  1452.     xbase = 0;
  1453. pcibr_soft->bs_dir_xbase = xbase;
  1454. /* it is entirely possible that we may, at this
  1455.  * point, have our dirmap pointing somewhere
  1456.  * other than our "master" port.
  1457.  */
  1458. pcibr_soft->bs_dir_xport =
  1459.     (dirmap & BRIDGE_DIRMAP_W_ID) >> BRIDGE_DIRMAP_W_ID_SHFT;
  1460.     }
  1461.     /* pcibr sources an error interrupt;
  1462.      * figure out where to send it.
  1463.      *
  1464.      * If any interrupts are enabled in bridge,
  1465.      * then the prom set us up and our interrupt
  1466.      * has already been reconnected in mlreset
  1467.      * above.
  1468.      *
  1469.      * Need to set the D_INTR_ISERR flag
  1470.      * in the dev_desc used for allocating the
  1471.      * error interrupt, so our interrupt will
  1472.      * be properly routed and prioritized.
  1473.      *
  1474.      * If our crosstalk provider wants to
  1475.      * fix widget error interrupts to specific
  1476.      * destinations, D_INTR_ISERR is how it
  1477.      * knows to do this.
  1478.      */
  1479.     xtalk_intr = xtalk_intr_alloc(xconn_vhdl, dev_desc, pcibr_vhdl);
  1480.     ASSERT(xtalk_intr != NULL);
  1481.     pcibr_soft->bsi_err_intr = xtalk_intr;
  1482.     /*
  1483.      * On IP35 with XBridge, we do some extra checks in pcibr_setwidint
  1484.      * in order to work around some addressing limitations.  In order
  1485.      * for that fire wall to work properly, we need to make sure we
  1486.      * start from a known clean state.
  1487.      */
  1488.     pcibr_clearwidint(bridge);
  1489.     xtalk_intr_connect(xtalk_intr, (xtalk_intr_setfunc_t)pcibr_setwidint, (void *)bridge);
  1490.     /*
  1491.      * now we can start handling error interrupts;
  1492.      * enable all of them.
  1493.      * NOTE: some PCI ints may already be enabled.
  1494.      */
  1495.     b_int_enable = bridge->b_int_enable | BRIDGE_ISR_ERRORS;
  1496.     bridge->b_int_enable = b_int_enable;
  1497.     bridge->b_int_mode = 0; /* do not send "clear interrupt" packets */
  1498.     bridge->b_wid_tflush; /* wait until Bridge PIO complete */
  1499.     /*
  1500.      * Depending on the rev of bridge, disable certain features.
  1501.      * Easiest way seems to be to force the PCIBR_NOwhatever
  1502.      * flag to be on for all DMA calls, which overrides any
  1503.      * PCIBR_whatever flag or even the setting of whatever
  1504.      * from the PCIIO_DMA_class flags (or even from the other
  1505.      * PCIBR flags, since NO overrides YES).
  1506.      */
  1507.     pcibr_soft->bs_dma_flags = 0;
  1508.     /* PREFETCH:
  1509.      * Always completely disabled for REV.A;
  1510.      * at "pcibr_prefetch_enable_rev", anyone
  1511.      * asking for PCIIO_PREFETCH gets it.
  1512.      * Between these two points, you have to ask
  1513.      * for PCIBR_PREFETCH, which promises that
  1514.      * your driver knows about known Bridge WARs.
  1515.      */
  1516.     if (pcibr_soft->bs_rev_num < BRIDGE_PART_REV_B)
  1517. pcibr_soft->bs_dma_flags |= PCIBR_NOPREFETCH;
  1518.     else if (pcibr_soft->bs_rev_num < 
  1519. (BRIDGE_WIDGET_PART_NUM << 4 | pcibr_prefetch_enable_rev))
  1520. pcibr_soft->bs_dma_flags |= PCIIO_NOPREFETCH;
  1521.     /* WRITE_GATHER:
  1522.      * Disabled up to but not including the
  1523.      * rev number in pcibr_wg_enable_rev. There
  1524.      * is no "WAR range" as with prefetch.
  1525.      */
  1526.     if (pcibr_soft->bs_rev_num < 
  1527. (BRIDGE_WIDGET_PART_NUM << 4 | pcibr_wg_enable_rev))
  1528. pcibr_soft->bs_dma_flags |= PCIBR_NOWRITE_GATHER;
  1529.     pciio_provider_register(pcibr_vhdl, &pcibr_provider);
  1530.     pciio_provider_startup(pcibr_vhdl);
  1531.     pci_io_fb = 0x00000004; /* I/O FreeBlock Base */
  1532.     pci_io_fl = 0xFFFFFFFF; /* I/O FreeBlock Last */
  1533.     pci_lo_fb = 0x00000010; /* Low Memory FreeBlock Base */
  1534.     pci_lo_fl = 0x001FFFFF; /* Low Memory FreeBlock Last */
  1535.     pci_hi_fb = 0x00200000; /* High Memory FreeBlock Base */
  1536.     pci_hi_fl = 0x3FFFFFFF; /* High Memory FreeBlock Last */
  1537.     PCI_ADDR_SPACE_LIMITS_STORE();
  1538.     /* build "no-slot" connection point
  1539.      */
  1540.     pcibr_info = pcibr_device_info_new
  1541. (pcibr_soft, PCIIO_SLOT_NONE, PCIIO_FUNC_NONE,
  1542.  PCIIO_VENDOR_ID_NONE, PCIIO_DEVICE_ID_NONE);
  1543.     noslot_conn = pciio_device_info_register
  1544. (pcibr_vhdl, &pcibr_info->f_c);
  1545.     /* Remember the no slot connection point info for tearing it
  1546.      * down during detach.
  1547.      */
  1548.     pcibr_soft->bs_noslot_conn = noslot_conn;
  1549.     pcibr_soft->bs_noslot_info = pcibr_info;
  1550. #if PCI_FBBE
  1551.     fast_back_to_back_enable = 1;
  1552. #endif
  1553. #if PCI_FBBE
  1554.     if (fast_back_to_back_enable) {
  1555. /*
  1556.  * All devices on the bus are capable of fast back to back, so
  1557.  * we need to set the fast back to back bit in all devices on
  1558.  * the bus that are capable of doing such accesses.
  1559.  */
  1560.     }
  1561. #endif
  1562. #ifdef LATER
  1563.     /* If the bridge has been reset then there is no need to reset
  1564.      * the individual PCI slots.
  1565.      */
  1566.     for (slot = 0; slot < 8; ++slot)  
  1567. /* Reset all the slots */
  1568. (void)pcibr_slot_reset(pcibr_vhdl, slot);
  1569. #endif
  1570.     for (slot = 0; slot < 8; ++slot)
  1571. /* Find out what is out there */
  1572. (void)pcibr_slot_info_init(pcibr_vhdl,slot);
  1573.     for (slot = 0; slot < 8; ++slot)  
  1574. /* Set up the address space for this slot in the pci land */
  1575. (void)pcibr_slot_addr_space_init(pcibr_vhdl,slot);
  1576.     for (slot = 0; slot < 8; ++slot)  
  1577. /* Setup the device register */
  1578. (void)pcibr_slot_device_init(pcibr_vhdl, slot);
  1579.     for (slot = 0; slot < 8; ++slot)  
  1580. /* Setup host/guest relations */
  1581. (void)pcibr_slot_guest_info_init(pcibr_vhdl,slot);
  1582.     for (slot = 0; slot < 8; ++slot)  
  1583. /* Initial RRB management */
  1584. (void)pcibr_slot_initial_rrb_alloc(pcibr_vhdl,slot);
  1585.     /* driver attach routines should be called out from generic linux code */
  1586.     for (slot = 0; slot < 8; ++slot)  
  1587. /* Call the device attach */
  1588. (void)pcibr_slot_call_device_attach(pcibr_vhdl, slot, 0);
  1589.     /*
  1590.      * Each Pbrick PCI bus only has slots 1 and 2.   Similarly for
  1591.      * widget 0xe on Ibricks.  Allocate RRB's accordingly.
  1592.      */
  1593.     if (pcibr_soft->bs_moduleid > 0) {
  1594. switch (MODULE_GET_BTCHAR(pcibr_soft->bs_moduleid)) {
  1595. case 'p': /* Pbrick */
  1596. do_pcibr_rrb_autoalloc(pcibr_soft, 1, 8);
  1597. do_pcibr_rrb_autoalloc(pcibr_soft, 2, 8);
  1598. break;
  1599. case 'i': /* Ibrick */
  1600.    /* port 0xe on the Ibrick only has slots 1 and 2 */
  1601. if (pcibr_soft->bs_xid == 0xe) {
  1602. do_pcibr_rrb_autoalloc(pcibr_soft, 1, 8);
  1603. do_pcibr_rrb_autoalloc(pcibr_soft, 2, 8);
  1604. }
  1605. else {
  1606.      /* allocate one RRB for the serial port */
  1607. do_pcibr_rrb_autoalloc(pcibr_soft, 0, 1);
  1608. }
  1609. break;
  1610. } /* switch */
  1611.     }
  1612. #ifdef LATER
  1613.     if (strstr(nicinfo, XTALK_PCI_PART_NUM)) {
  1614. do_pcibr_rrb_autoalloc(pcibr_soft, 1, 8);
  1615. #if PCIBR_RRB_DEBUG
  1616. printf("nnFound XTALK_PCI (030-1275) at %vn", xconn_vhdl);
  1617. printf("pcibr_attach: %v Shoebox RRB MANAGEMENT: %d+%d freen",
  1618. pcibr_vhdl,
  1619. pcibr_soft->bs_rrb_avail[0],
  1620. pcibr_soft->bs_rrb_avail[1]);
  1621. for (slot = 0; slot < 8; ++slot)
  1622.     printf("t%d+%d+%d",
  1623.     0xFFF & pcibr_soft->bs_rrb_valid[slot],
  1624.     0xFFF & pcibr_soft->bs_rrb_valid[slot + PCIBR_RRB_SLOT_VIRTUAL],
  1625.     pcibr_soft->bs_rrb_res[slot]);
  1626. printf("n");
  1627. #endif
  1628.     }
  1629. #else
  1630. FIXME("pcibr_attach: Call do_pcibr_rrb_autoalloc nicinfon");
  1631. #endif
  1632.     if (aa)
  1633.     async_attach_add_info(noslot_conn, aa);
  1634.     pciio_device_attach(noslot_conn, 0);
  1635.     /* 
  1636.      * Tear down pointer to async attach info -- async threads for
  1637.      * bridge's descendants may be running but the bridge's work is done.
  1638.      */
  1639.     if (aa)
  1640.     async_attach_del_info(xconn_vhdl);
  1641.     return 0;
  1642. }
  1643. /*
  1644.  * pcibr_detach:
  1645.  * Detach the bridge device from the hwgraph after cleaning out all the 
  1646.  * underlying vertices.
  1647.  */
  1648. int
  1649. pcibr_detach(devfs_handle_t xconn)
  1650. {
  1651.     pciio_slot_t slot;
  1652.     devfs_handle_t pcibr_vhdl;
  1653.     pcibr_soft_t pcibr_soft;
  1654.     bridge_t *bridge;
  1655.     /* Get the bridge vertex from its xtalk connection point */
  1656.     if (hwgraph_traverse(xconn, EDGE_LBL_PCI, &pcibr_vhdl) != GRAPH_SUCCESS)
  1657. return(1);
  1658.     pcibr_soft = pcibr_soft_get(pcibr_vhdl);
  1659.     bridge = pcibr_soft->bs_base;
  1660.     /* Disable the interrupts from the bridge */
  1661.     bridge->b_int_enable = 0;
  1662.     /* Detach all the PCI devices talking to this bridge */
  1663.     for(slot = 0; slot < 8; slot++) {
  1664. #ifdef DEBUG
  1665. printk("pcibr_device_detach called for %p/%dn",
  1666. pcibr_vhdl,slot);
  1667. #endif
  1668. pcibr_slot_detach(pcibr_vhdl, slot, 0);
  1669.     }
  1670.     /* Unregister the no-slot connection point */
  1671.     pciio_device_info_unregister(pcibr_vhdl,
  1672.  &(pcibr_soft->bs_noslot_info->f_c));
  1673.     spin_lock_destroy(&pcibr_soft->bs_lock);
  1674.     kfree(pcibr_soft->bs_name);
  1675.     
  1676.     /* Error handler gets unregistered when the widget info is 
  1677.      * cleaned 
  1678.      */
  1679.     /* Free the soft ATE maps */
  1680.     if (pcibr_soft->bs_int_ate_map)
  1681. rmfreemap(pcibr_soft->bs_int_ate_map);
  1682.     if (pcibr_soft->bs_ext_ate_map)
  1683. rmfreemap(pcibr_soft->bs_ext_ate_map);
  1684.     /* Disconnect the error interrupt and free the xtalk resources 
  1685.      * associated with it.
  1686.      */
  1687.     xtalk_intr_disconnect(pcibr_soft->bsi_err_intr);
  1688.     xtalk_intr_free(pcibr_soft->bsi_err_intr);
  1689.     /* Clear the software state maintained by the bridge driver for this
  1690.      * bridge.
  1691.      */
  1692.     DEL(pcibr_soft);
  1693.     /* Remove the Bridge revision labelled info */
  1694.     (void)hwgraph_info_remove_LBL(pcibr_vhdl, INFO_LBL_PCIBR_ASIC_REV, NULL);
  1695.     /* Remove the character device associated with this bridge */
  1696.     (void)hwgraph_edge_remove(pcibr_vhdl, EDGE_LBL_CONTROLLER, NULL);
  1697.     /* Remove the PCI bridge vertex */
  1698.     (void)hwgraph_edge_remove(xconn, EDGE_LBL_PCI, NULL);
  1699.     return(0);
  1700. }
  1701. int
  1702. pcibr_asic_rev(devfs_handle_t pconn_vhdl)
  1703. {
  1704.     devfs_handle_t            pcibr_vhdl;
  1705.     arbitrary_info_t        ainfo;
  1706.     if (GRAPH_SUCCESS !=
  1707. hwgraph_traverse(pconn_vhdl, EDGE_LBL_MASTER, &pcibr_vhdl))
  1708. return -1;
  1709.     if (GRAPH_SUCCESS !=
  1710. hwgraph_info_get_LBL(pcibr_vhdl, INFO_LBL_PCIBR_ASIC_REV, &ainfo))
  1711. return -1;
  1712.     return (int) ainfo;
  1713. }
  1714. int
  1715. pcibr_write_gather_flush(devfs_handle_t pconn_vhdl)
  1716. {
  1717.     pciio_info_t  pciio_info = pciio_info_get(pconn_vhdl);
  1718.     pcibr_soft_t  pcibr_soft = (pcibr_soft_t) pciio_info_mfast_get(pciio_info);
  1719.     pciio_slot_t  slot;
  1720.     slot = pciio_info_slot_get(pciio_info);
  1721.     pcibr_device_write_gather_flush(pcibr_soft, slot);
  1722.     return 0;
  1723. }
  1724. /* =====================================================================
  1725.  *    PIO MANAGEMENT
  1726.  */
  1727. static iopaddr_t
  1728. pcibr_addr_pci_to_xio(devfs_handle_t pconn_vhdl,
  1729.       pciio_slot_t slot,
  1730.       pciio_space_t space,
  1731.       iopaddr_t pci_addr,
  1732.       size_t req_size,
  1733.       unsigned flags)
  1734. {
  1735.     pcibr_info_t            pcibr_info = pcibr_info_get(pconn_vhdl);
  1736.     pciio_info_t            pciio_info = &pcibr_info->f_c;
  1737.     pcibr_soft_t            pcibr_soft = (pcibr_soft_t) pciio_info_mfast_get(pciio_info);
  1738.     bridge_t               *bridge = pcibr_soft->bs_base;
  1739.     unsigned                bar; /* which BASE reg on device is decoding */
  1740.     iopaddr_t               xio_addr = XIO_NOWHERE;
  1741.     pciio_space_t           wspace; /* which space device is decoding */
  1742.     iopaddr_t               wbase; /* base of device decode on PCI */
  1743.     size_t                  wsize; /* size of device decode on PCI */
  1744.     int                     try; /* DevIO(x) window scanning order control */
  1745.     int                     win; /* which DevIO(x) window is being used */
  1746.     pciio_space_t           mspace; /* target space for devio(x) register */
  1747.     iopaddr_t               mbase; /* base of devio(x) mapped area on PCI */
  1748.     size_t                  msize; /* size of devio(x) mapped area on PCI */
  1749.     size_t                  mmask; /* addr bits stored in Device(x) */
  1750.     unsigned long           s;
  1751.     s = pcibr_lock(pcibr_soft);
  1752.     if (pcibr_soft->bs_slot[slot].has_host) {
  1753. slot = pcibr_soft->bs_slot[slot].host_slot;
  1754. pcibr_info = pcibr_soft->bs_slot[slot].bss_infos[0];
  1755.     }
  1756.     if (space == PCIIO_SPACE_NONE)
  1757. goto done;
  1758.     if (space == PCIIO_SPACE_CFG) {
  1759. /*
  1760.  * Usually, the first mapping
  1761.  * established to a PCI device
  1762.  * is to its config space.
  1763.  *
  1764.  * In any case, we definitely
  1765.  * do NOT need to worry about
  1766.  * PCI BASE registers, and
  1767.  * MUST NOT attempt to point
  1768.  * the DevIO(x) window at
  1769.  * this access ...
  1770.  */
  1771. if (((flags & PCIIO_BYTE_STREAM) == 0) &&
  1772.     ((pci_addr + req_size) <= BRIDGE_TYPE0_CFG_FUNC_OFF))
  1773.     xio_addr = pci_addr + BRIDGE_TYPE0_CFG_DEV(slot);
  1774. goto done;
  1775.     }
  1776.     if (space == PCIIO_SPACE_ROM) {
  1777. /* PIO to the Expansion Rom.
  1778.  * Driver is responsible for
  1779.  * enabling and disabling
  1780.  * decodes properly.
  1781.  */
  1782. wbase = pcibr_info->f_rbase;
  1783. wsize = pcibr_info->f_rsize;
  1784. /*
  1785.  * While the driver should know better
  1786.  * than to attempt to map more space
  1787.  * than the device is decoding, he might
  1788.  * do it; better to bail out here.
  1789.  */
  1790. if ((pci_addr + req_size) > wsize)
  1791.     goto done;
  1792. pci_addr += wbase;
  1793. space = PCIIO_SPACE_MEM;
  1794.     }
  1795.     /*
  1796.      * reduce window mappings to raw
  1797.      * space mappings (maybe allocating
  1798.      * windows), and try for DevIO(x)
  1799.      * usage (setting it if it is available).
  1800.      */
  1801.     bar = space - PCIIO_SPACE_WIN0;
  1802.     if (bar < 6) {
  1803. wspace = pcibr_info->f_window[bar].w_space;
  1804. if (wspace == PCIIO_SPACE_NONE)
  1805.     goto done;
  1806. /* get PCI base and size */
  1807. wbase = pcibr_info->f_window[bar].w_base;
  1808. wsize = pcibr_info->f_window[bar].w_size;
  1809. /*
  1810.  * While the driver should know better
  1811.  * than to attempt to map more space
  1812.  * than the device is decoding, he might
  1813.  * do it; better to bail out here.
  1814.  */
  1815. if ((pci_addr + req_size) > wsize)
  1816.     goto done;
  1817. /* shift from window relative to
  1818.  * decoded space relative.
  1819.  */
  1820. pci_addr += wbase;
  1821. space = wspace;
  1822.     } else
  1823. bar = -1;
  1824.     /* Scan all the DevIO(x) windows twice looking for one
  1825.      * that can satisfy our request. The first time through,
  1826.      * only look at assigned windows; the second time, also
  1827.      * look at PCIIO_SPACE_NONE windows. Arrange the order
  1828.      * so we always look at our own window first.
  1829.      *
  1830.      * We will not attempt to satisfy a single request
  1831.      * by concatinating multiple windows.
  1832.      */
  1833.     for (try = 0; try < 16; ++try) {
  1834. bridgereg_t             devreg;
  1835. unsigned                offset;
  1836. win = (try + slot) % 8;
  1837. /* If this DevIO(x) mapping area can provide
  1838.  * a mapping to this address, use it.
  1839.  */
  1840. msize = (win < 2) ? 0x200000 : 0x100000;
  1841. mmask = -msize;
  1842. if (space != PCIIO_SPACE_IO)
  1843.     mmask &= 0x3FFFFFFF;
  1844. offset = pci_addr & (msize - 1);
  1845. /* If this window can't possibly handle that request,
  1846.  * go on to the next window.
  1847.  */
  1848. if (((pci_addr & (msize - 1)) + req_size) > msize)
  1849.     continue;
  1850. devreg = pcibr_soft->bs_slot[win].bss_device;
  1851. /* Is this window "nailed down"?
  1852.  * If not, maybe we can use it.
  1853.  * (only check this the second time through)
  1854.  */
  1855. mspace = pcibr_soft->bs_slot[win].bss_devio.bssd_space;
  1856. if ((try > 7) && (mspace == PCIIO_SPACE_NONE)) {
  1857.     /* If this is the primary DevIO(x) window
  1858.      * for some other device, skip it.
  1859.      */
  1860.     if ((win != slot) &&
  1861. (PCIIO_VENDOR_ID_NONE !=
  1862.  pcibr_soft->bs_slot[win].bss_vendor_id))
  1863. continue;
  1864.     /* It's a free window, and we fit in it.
  1865.      * Set up Device(win) to our taste.
  1866.      */
  1867.     mbase = pci_addr & mmask;
  1868.     /* check that we would really get from
  1869.      * here to there.
  1870.      */
  1871.     if ((mbase | offset) != pci_addr)
  1872. continue;
  1873.     devreg &= ~BRIDGE_DEV_OFF_MASK;
  1874.     if (space != PCIIO_SPACE_IO)
  1875. devreg |= BRIDGE_DEV_DEV_IO_MEM;
  1876.     else
  1877. devreg &= ~BRIDGE_DEV_DEV_IO_MEM;
  1878.     devreg |= (mbase >> 20) & BRIDGE_DEV_OFF_MASK;
  1879.     /* default is WORD_VALUES.
  1880.      * if you specify both,
  1881.      * operation is undefined.
  1882.      */
  1883.     if (flags & PCIIO_BYTE_STREAM)
  1884. devreg |= BRIDGE_DEV_DEV_SWAP;
  1885.     else
  1886. devreg &= ~BRIDGE_DEV_DEV_SWAP;
  1887.     if (pcibr_soft->bs_slot[win].bss_device != devreg) {
  1888. bridge->b_device[win].reg = devreg;
  1889. pcibr_soft->bs_slot[win].bss_device = devreg;
  1890. bridge->b_wid_tflush; /* wait until Bridge PIO complete */
  1891. #if DEBUG && PCI_DEBUG
  1892. printk("pcibr Device(%d): 0x%lxn", win, bridge->b_device[win].reg);
  1893. #endif
  1894.     }
  1895.     pcibr_soft->bs_slot[win].bss_devio.bssd_space = space;
  1896.     pcibr_soft->bs_slot[win].bss_devio.bssd_base = mbase;
  1897.     xio_addr = BRIDGE_DEVIO(win) + (pci_addr - mbase);
  1898. #if DEBUG && PCI_DEBUG
  1899.     printk("%s LINE %d map to space %d space desc 0x%x[%lx..%lx] for slot %d allocates DevIO(%d) devreg 0x%xn", 
  1900.     __FUNCTION__, __LINE__, space, space_desc,
  1901.     pci_addr, pci_addr + req_size - 1,
  1902.     slot, win, devreg);
  1903. #endif
  1904.     goto done;
  1905. } /* endif DevIO(x) not pointed */
  1906. mbase = pcibr_soft->bs_slot[win].bss_devio.bssd_base;
  1907. /* Now check for request incompat with DevIO(x)
  1908.  */
  1909. if ((mspace != space) ||
  1910.     (pci_addr < mbase) ||
  1911.     ((pci_addr + req_size) > (mbase + msize)) ||
  1912.     ((flags & PCIIO_BYTE_STREAM) && !(devreg & BRIDGE_DEV_DEV_SWAP)) ||
  1913.     (!(flags & PCIIO_BYTE_STREAM) && (devreg & BRIDGE_DEV_DEV_SWAP)))
  1914.     continue;
  1915. /* DevIO(x) window is pointed at PCI space
  1916.  * that includes our target. Calculate the
  1917.  * final XIO address, release the lock and
  1918.  * return.
  1919.  */
  1920. xio_addr = BRIDGE_DEVIO(win) + (pci_addr - mbase);
  1921. #if DEBUG && PCI_DEBUG
  1922. printk("%s LINE %d map to space %d [0x%p..0x%p] for slot %d uses DevIO(%d)n",
  1923. __FUNCTION__, __LINE__, space,  pci_addr, pci_addr + req_size - 1, slot, win);
  1924. #endif
  1925. goto done;
  1926.     }
  1927.     switch (space) {
  1928. /*
  1929.  * Accesses to device decode
  1930.  * areas that do a not fit
  1931.  * within the DevIO(x) space are
  1932.  * modified to be accesses via
  1933.  * the direct mapping areas.
  1934.  *
  1935.  * If necessary, drivers can
  1936.  * explicitly ask for mappings
  1937.  * into these address spaces,
  1938.  * but this should never be needed.
  1939.  */
  1940.     case PCIIO_SPACE_MEM: /* "mem space" */
  1941.     case PCIIO_SPACE_MEM32: /* "mem, use 32-bit-wide bus" */
  1942. if ((pci_addr + BRIDGE_PCI_MEM32_BASE + req_size - 1) <=
  1943.     BRIDGE_PCI_MEM32_LIMIT)
  1944.     xio_addr = pci_addr + BRIDGE_PCI_MEM32_BASE;
  1945. break;
  1946.     case PCIIO_SPACE_MEM64: /* "mem, use 64-bit-wide bus" */
  1947. if ((pci_addr + BRIDGE_PCI_MEM64_BASE + req_size - 1) <=
  1948.     BRIDGE_PCI_MEM64_LIMIT)
  1949.     xio_addr = pci_addr + BRIDGE_PCI_MEM64_BASE;
  1950. break;
  1951.     case PCIIO_SPACE_IO: /* "i/o space" */
  1952. /* Bridge Hardware Bug WAR #482741:
  1953.  * The 4G area that maps directly from
  1954.  * XIO space to PCI I/O space is busted
  1955.  * until Bridge Rev D.
  1956.  */
  1957. if ((pcibr_soft->bs_rev_num > BRIDGE_PART_REV_C) &&
  1958.     ((pci_addr + BRIDGE_PCI_IO_BASE + req_size - 1) <=
  1959.      BRIDGE_PCI_IO_LIMIT))
  1960.     xio_addr = pci_addr + BRIDGE_PCI_IO_BASE;
  1961. break;
  1962.     }
  1963.     /* Check that "Direct PIO" byteswapping matches,
  1964.      * try to change it if it does not.
  1965.      */
  1966.     if (xio_addr != XIO_NOWHERE) {
  1967. unsigned                bst; /* nonzero to set bytestream */
  1968. unsigned               *bfp; /* addr of record of how swapper is set */
  1969. unsigned                swb; /* which control bit to mung */
  1970. unsigned                bfo; /* current swapper setting */
  1971. unsigned                bfn; /* desired swapper setting */
  1972. bfp = ((space == PCIIO_SPACE_IO)
  1973.        ? (&pcibr_soft->bs_pio_end_io)
  1974.        : (&pcibr_soft->bs_pio_end_mem));
  1975. bfo = *bfp;
  1976. bst = flags & PCIIO_BYTE_STREAM;
  1977. bfn = bst ? PCIIO_BYTE_STREAM : PCIIO_WORD_VALUES;
  1978. if (bfn == bfo) { /* we already match. */
  1979.     ;
  1980. } else if (bfo != 0) { /* we have a conflict. */
  1981. #if DEBUG && PCI_DEBUG
  1982.     printk("pcibr_addr_pci_to_xio: swap conflict in space %d , was%s%s, want%s%sn",
  1983.     space, 
  1984.     bfo & PCIIO_BYTE_STREAM ? " BYTE_STREAM" : "",
  1985.     bfo & PCIIO_WORD_VALUES ? " WORD_VALUES" : "",
  1986.     bfn & PCIIO_BYTE_STREAM ? " BYTE_STREAM" : "",
  1987.     bfn & PCIIO_WORD_VALUES ? " WORD_VALUES" : "");
  1988. #endif
  1989.     xio_addr = XIO_NOWHERE;
  1990. } else { /* OK to make the change. */
  1991.     bridgereg_t             octl, nctl;
  1992.     swb = (space == PCIIO_SPACE_IO) ? BRIDGE_CTRL_IO_SWAP : BRIDGE_CTRL_MEM_SWAP;
  1993.     octl = bridge->b_wid_control;
  1994.     nctl = bst ? octl | swb : octl & ~swb;
  1995.     if (octl != nctl) /* make the change if any */
  1996. bridge->b_wid_control = nctl;
  1997.     *bfp = bfn; /* record the assignment */
  1998. #if DEBUG && PCI_DEBUG
  1999.     printk("pcibr_addr_pci_to_xio: swap for space %d  set to%s%sn",
  2000.     space, 
  2001.     bfn & PCIIO_BYTE_STREAM ? " BYTE_STREAM" : "",
  2002.     bfn & PCIIO_WORD_VALUES ? " WORD_VALUES" : "");
  2003. #endif
  2004. }
  2005.     }
  2006.   done:
  2007.     pcibr_unlock(pcibr_soft, s);
  2008.     return xio_addr;
  2009. }
  2010. /*ARGSUSED6 */
  2011. pcibr_piomap_t
  2012. pcibr_piomap_alloc(devfs_handle_t pconn_vhdl,
  2013.    device_desc_t dev_desc,
  2014.    pciio_space_t space,
  2015.    iopaddr_t pci_addr,
  2016.    size_t req_size,
  2017.    size_t req_size_max,
  2018.    unsigned flags)
  2019. {
  2020.     pcibr_info_t     pcibr_info = pcibr_info_get(pconn_vhdl);
  2021.     pciio_info_t            pciio_info = &pcibr_info->f_c;
  2022.     pciio_slot_t            pciio_slot = pciio_info_slot_get(pciio_info);
  2023.     pcibr_soft_t            pcibr_soft = (pcibr_soft_t) pciio_info_mfast_get(pciio_info);
  2024.     devfs_handle_t            xconn_vhdl = pcibr_soft->bs_conn;
  2025.     pcibr_piomap_t         *mapptr;
  2026.     pcibr_piomap_t          maplist;
  2027.     pcibr_piomap_t          pcibr_piomap;
  2028.     iopaddr_t               xio_addr;
  2029.     xtalk_piomap_t          xtalk_piomap;
  2030.     unsigned long           s;
  2031.     /* Make sure that the req sizes are non-zero */
  2032.     if ((req_size < 1) || (req_size_max < 1))
  2033. return NULL;
  2034.     /*
  2035.      * Code to translate slot/space/addr
  2036.      * into xio_addr is common between
  2037.      * this routine and pcibr_piotrans_addr.
  2038.      */
  2039.     xio_addr = pcibr_addr_pci_to_xio(pconn_vhdl, pciio_slot, space, pci_addr, req_size, flags);
  2040.     if (xio_addr == XIO_NOWHERE)
  2041. return NULL;
  2042.     /* Check the piomap list to see if there is already an allocated
  2043.      * piomap entry but not in use. If so use that one. Otherwise
  2044.      * allocate a new piomap entry and add it to the piomap list
  2045.      */
  2046.     mapptr = &(pcibr_info->f_piomap);
  2047.     s = pcibr_lock(pcibr_soft);
  2048.     for (pcibr_piomap = *mapptr;
  2049.  pcibr_piomap != NULL;
  2050.  pcibr_piomap = pcibr_piomap->bp_next) {
  2051. if (pcibr_piomap->bp_mapsz == 0)
  2052.     break;
  2053.     }
  2054.     if (pcibr_piomap)
  2055. mapptr = NULL;
  2056.     else {
  2057. pcibr_unlock(pcibr_soft, s);
  2058. NEW(pcibr_piomap);
  2059.     }
  2060.     pcibr_piomap->bp_dev = pconn_vhdl;
  2061.     pcibr_piomap->bp_slot = pciio_slot;
  2062.     pcibr_piomap->bp_flags = flags;
  2063.     pcibr_piomap->bp_space = space;
  2064.     pcibr_piomap->bp_pciaddr = pci_addr;
  2065.     pcibr_piomap->bp_mapsz = req_size;
  2066.     pcibr_piomap->bp_soft = pcibr_soft;
  2067.     pcibr_piomap->bp_toc[0] = ATOMIC_INIT(0);
  2068.     if (mapptr) {
  2069. s = pcibr_lock(pcibr_soft);
  2070. maplist = *mapptr;
  2071. pcibr_piomap->bp_next = maplist;
  2072. *mapptr = pcibr_piomap;
  2073.     }
  2074.     pcibr_unlock(pcibr_soft, s);
  2075.     if (pcibr_piomap) {
  2076. xtalk_piomap =
  2077.     xtalk_piomap_alloc(xconn_vhdl, 0,
  2078.        xio_addr,
  2079.        req_size, req_size_max,
  2080.        flags & PIOMAP_FLAGS);
  2081. if (xtalk_piomap) {
  2082.     pcibr_piomap->bp_xtalk_addr = xio_addr;
  2083.     pcibr_piomap->bp_xtalk_pio = xtalk_piomap;
  2084. } else {
  2085.     pcibr_piomap->bp_mapsz = 0;
  2086.     pcibr_piomap = 0;
  2087. }
  2088.     }
  2089.     return pcibr_piomap;
  2090. }
  2091. /*ARGSUSED */
  2092. void
  2093. pcibr_piomap_free(pcibr_piomap_t pcibr_piomap)
  2094. {
  2095.     xtalk_piomap_free(pcibr_piomap->bp_xtalk_pio);
  2096.     pcibr_piomap->bp_xtalk_pio = 0;
  2097.     pcibr_piomap->bp_mapsz = 0;
  2098. }
  2099. /*ARGSUSED */
  2100. caddr_t
  2101. pcibr_piomap_addr(pcibr_piomap_t pcibr_piomap,
  2102.   iopaddr_t pci_addr,
  2103.   size_t req_size)
  2104. {
  2105.     return xtalk_piomap_addr(pcibr_piomap->bp_xtalk_pio,
  2106.      pcibr_piomap->bp_xtalk_addr +
  2107.      pci_addr - pcibr_piomap->bp_pciaddr,
  2108.      req_size);
  2109. }
  2110. /*ARGSUSED */
  2111. void
  2112. pcibr_piomap_done(pcibr_piomap_t pcibr_piomap)
  2113. {
  2114.     xtalk_piomap_done(pcibr_piomap->bp_xtalk_pio);
  2115. }
  2116. /*ARGSUSED */
  2117. caddr_t
  2118. pcibr_piotrans_addr(devfs_handle_t pconn_vhdl,
  2119.     device_desc_t dev_desc,
  2120.     pciio_space_t space,
  2121.     iopaddr_t pci_addr,
  2122.     size_t req_size,
  2123.     unsigned flags)
  2124. {
  2125.     pciio_info_t            pciio_info = pciio_info_get(pconn_vhdl);
  2126.     pciio_slot_t            pciio_slot = pciio_info_slot_get(pciio_info);
  2127.     pcibr_soft_t            pcibr_soft = (pcibr_soft_t) pciio_info_mfast_get(pciio_info);
  2128.     devfs_handle_t            xconn_vhdl = pcibr_soft->bs_conn;
  2129.     iopaddr_t               xio_addr;
  2130.     xio_addr = pcibr_addr_pci_to_xio(pconn_vhdl, pciio_slot, space, pci_addr, req_size, flags);
  2131.     if (xio_addr == XIO_NOWHERE)
  2132. return NULL;
  2133.     return xtalk_piotrans_addr(xconn_vhdl, 0, xio_addr, req_size, flags & PIOMAP_FLAGS);
  2134. }
  2135. /*
  2136.  * PIO Space allocation and management.
  2137.  *      Allocate and Manage the PCI PIO space (mem and io space)
  2138.  *      This routine is pretty simplistic at this time, and
  2139.  *      does pretty trivial management of allocation and freeing..
  2140.  *      The current scheme is prone for fragmentation..
  2141.  *      Change the scheme to use bitmaps.
  2142.  */
  2143. /*ARGSUSED */
  2144. iopaddr_t
  2145. pcibr_piospace_alloc(devfs_handle_t pconn_vhdl,
  2146.      device_desc_t dev_desc,
  2147.      pciio_space_t space,
  2148.      size_t req_size,
  2149.      size_t alignment)
  2150. {
  2151.     pcibr_info_t            pcibr_info = pcibr_info_get(pconn_vhdl);
  2152.     pciio_info_t            pciio_info = &pcibr_info->f_c;
  2153.     pcibr_soft_t            pcibr_soft = (pcibr_soft_t) pciio_info_mfast_get(pciio_info);
  2154.     pciio_piospace_t        piosp;
  2155.     unsigned long           s;
  2156.     iopaddr_t              *pciaddr, *pcilast;
  2157.     iopaddr_t               start_addr;
  2158.     size_t                  align_mask;
  2159.     /*
  2160.      * Check for proper alignment
  2161.      */
  2162.     ASSERT(alignment >= NBPP);
  2163.     ASSERT((alignment & (alignment - 1)) == 0);
  2164.     align_mask = alignment - 1;
  2165.     s = pcibr_lock(pcibr_soft);
  2166.     /*
  2167.      * First look if a previously allocated chunk exists.
  2168.      */
  2169.     if ((piosp = pcibr_info->f_piospace)) {
  2170. /*
  2171.  * Look through the list for a right sized free chunk.
  2172.  */
  2173. do {
  2174.     if (piosp->free &&
  2175. (piosp->space == space) &&
  2176. (piosp->count >= req_size) &&
  2177. !(piosp->start & align_mask)) {
  2178. piosp->free = 0;
  2179. pcibr_unlock(pcibr_soft, s);
  2180. return piosp->start;
  2181.     }
  2182.     piosp = piosp->next;
  2183. } while (piosp);
  2184.     }
  2185.     ASSERT(!piosp);
  2186.     switch (space) {
  2187.     case PCIIO_SPACE_IO:
  2188. pciaddr = &pcibr_soft->bs_spinfo.pci_io_base;
  2189. pcilast = &pcibr_soft->bs_spinfo.pci_io_last;
  2190. break;
  2191.     case PCIIO_SPACE_MEM:
  2192.     case PCIIO_SPACE_MEM32:
  2193. pciaddr = &pcibr_soft->bs_spinfo.pci_mem_base;
  2194. pcilast = &pcibr_soft->bs_spinfo.pci_mem_last;
  2195. break;
  2196.     default:
  2197. ASSERT(0);
  2198. pcibr_unlock(pcibr_soft, s);
  2199. return 0;
  2200.     }
  2201.     start_addr = *pciaddr;
  2202.     /*
  2203.      * Align start_addr.
  2204.      */
  2205.     if (start_addr & align_mask)
  2206. start_addr = (start_addr + align_mask) & ~align_mask;
  2207.     if ((start_addr + req_size) > *pcilast) {
  2208. /*
  2209.  * If too big a request, reject it.
  2210.  */
  2211. pcibr_unlock(pcibr_soft, s);
  2212. return 0;
  2213.     }
  2214.     *pciaddr = (start_addr + req_size);
  2215.     NEW(piosp);
  2216.     piosp->free = 0;
  2217.     piosp->space = space;
  2218.     piosp->start = start_addr;
  2219.     piosp->count = req_size;
  2220.     piosp->next = pcibr_info->f_piospace;
  2221.     pcibr_info->f_piospace = piosp;
  2222.     pcibr_unlock(pcibr_soft, s);
  2223.     return start_addr;
  2224. }
  2225. /*ARGSUSED */
  2226. void
  2227. pcibr_piospace_free(devfs_handle_t pconn_vhdl,
  2228.     pciio_space_t space,
  2229.     iopaddr_t pciaddr,
  2230.     size_t req_size)
  2231. {
  2232.     pcibr_info_t            pcibr_info = pcibr_info_get(pconn_vhdl);
  2233.     pcibr_soft_t            pcibr_soft = (pcibr_soft_t) pcibr_info->f_mfast;
  2234.     pciio_piospace_t        piosp;
  2235.     unsigned long           s;
  2236.     char                    name[1024];
  2237.     /*
  2238.      * Look through the bridge data structures for the pciio_piospace_t
  2239.      * structure corresponding to  'pciaddr'
  2240.      */
  2241.     s = pcibr_lock(pcibr_soft);
  2242.     piosp = pcibr_info->f_piospace;
  2243.     while (piosp) {
  2244. /*
  2245.  * Piospace free can only be for the complete
  2246.  * chunk and not parts of it..
  2247.  */
  2248. if (piosp->start == pciaddr) {
  2249.     if (piosp->count == req_size)
  2250. break;
  2251.     /*
  2252.      * Improper size passed for freeing..
  2253.      * Print a message and break;
  2254.      */
  2255.     hwgraph_vertex_name_get(pconn_vhdl, name, 1024);
  2256.     printk(KERN_WARNING  "pcibr_piospace_free: error");
  2257.     printk(KERN_WARNING  "Device %s freeing size (0x%lx) different than allocated (0x%lx)",
  2258. name, req_size, piosp->count);
  2259.     printk(KERN_WARNING  "Freeing 0x%lx instead", piosp->count);
  2260.     break;
  2261. }
  2262. piosp = piosp->next;
  2263.     }
  2264.     if (!piosp) {
  2265. printk(KERN_WARNING  
  2266. "pcibr_piospace_free: Address 0x%lx size 0x%lx - No matchn",
  2267. pciaddr, req_size);
  2268. pcibr_unlock(pcibr_soft, s);
  2269. return;
  2270.     }
  2271.     piosp->free = 1;
  2272.     pcibr_unlock(pcibr_soft, s);
  2273.     return;
  2274. }
  2275. /* =====================================================================
  2276.  *    DMA MANAGEMENT
  2277.  *
  2278.  *      The Bridge ASIC provides three methods of doing
  2279.  *      DMA: via a "direct map" register available in
  2280.  *      32-bit PCI space (which selects a contiguous 2G
  2281.  *      address space on some other widget), via
  2282.  *      "direct" addressing via 64-bit PCI space (all
  2283.  *      destination information comes from the PCI
  2284.  *      address, including transfer attributes), and via
  2285.  *      a "mapped" region that allows a bunch of
  2286.  *      different small mappings to be established with
  2287.  *      the PMU.
  2288.  *
  2289.  *      For efficiency, we most prefer to use the 32-bit
  2290.  *      direct mapping facility, since it requires no
  2291.  *      resource allocations. The advantage of using the
  2292.  *      PMU over the 64-bit direct is that single-cycle
  2293.  *      PCI addressing can be used; the advantage of
  2294.  *      using 64-bit direct over PMU addressing is that
  2295.  *      we do not have to allocate entries in the PMU.
  2296.  */
  2297. /*
  2298.  * Convert PCI-generic software flags and Bridge-specific software flags
  2299.  * into Bridge-specific Direct Map attribute bits.
  2300.  */
  2301. static iopaddr_t
  2302. pcibr_flags_to_d64(unsigned flags, pcibr_soft_t pcibr_soft)
  2303. {
  2304.     iopaddr_t               attributes = 0;
  2305.     /* Sanity check: Bridge only allows use of VCHAN1 via 64-bit addrs */
  2306. #ifdef LATER
  2307.     ASSERT_ALWAYS(!(flags & PCIBR_VCHAN1) || (flags & PCIIO_DMA_A64));
  2308. #endif
  2309.     /* Generic macro flags
  2310.      */
  2311.     if (flags & PCIIO_DMA_DATA) { /* standard data channel */
  2312. attributes &= ~PCI64_ATTR_BAR; /* no barrier bit */
  2313. attributes |= PCI64_ATTR_PREF; /* prefetch on */
  2314.     }
  2315.     if (flags & PCIIO_DMA_CMD) { /* standard command channel */
  2316. attributes |= PCI64_ATTR_BAR; /* barrier bit on */
  2317. attributes &= ~PCI64_ATTR_PREF; /* disable prefetch */
  2318.     }
  2319.     /* Generic detail flags
  2320.      */
  2321.     if (flags & PCIIO_PREFETCH)
  2322. attributes |= PCI64_ATTR_PREF;
  2323.     if (flags & PCIIO_NOPREFETCH)
  2324. attributes &= ~PCI64_ATTR_PREF;
  2325.     /* the swap bit is in the address attributes for xbridge */
  2326.     if (pcibr_soft->bs_xbridge) {
  2327.      if (flags & PCIIO_BYTE_STREAM)
  2328.          attributes |= PCI64_ATTR_SWAP;
  2329.      if (flags & PCIIO_WORD_VALUES)
  2330.          attributes &= ~PCI64_ATTR_SWAP;
  2331.     }
  2332.     /* Provider-specific flags
  2333.      */
  2334.     if (flags & PCIBR_BARRIER)
  2335. attributes |= PCI64_ATTR_BAR;
  2336.     if (flags & PCIBR_NOBARRIER)
  2337. attributes &= ~PCI64_ATTR_BAR;
  2338.     if (flags & PCIBR_PREFETCH)
  2339. attributes |= PCI64_ATTR_PREF;
  2340.     if (flags & PCIBR_NOPREFETCH)
  2341. attributes &= ~PCI64_ATTR_PREF;
  2342.     if (flags & PCIBR_PRECISE)
  2343. attributes |= PCI64_ATTR_PREC;
  2344.     if (flags & PCIBR_NOPRECISE)
  2345. attributes &= ~PCI64_ATTR_PREC;
  2346.     if (flags & PCIBR_VCHAN1)
  2347. attributes |= PCI64_ATTR_VIRTUAL;
  2348.     if (flags & PCIBR_VCHAN0)
  2349. attributes &= ~PCI64_ATTR_VIRTUAL;
  2350.     return (attributes);
  2351. }
  2352. /*ARGSUSED */
  2353. pcibr_dmamap_t
  2354. pcibr_dmamap_alloc(devfs_handle_t pconn_vhdl,
  2355.    device_desc_t dev_desc,
  2356.    size_t req_size_max,
  2357.    unsigned flags)
  2358. {
  2359.     pciio_info_t            pciio_info = pciio_info_get(pconn_vhdl);
  2360.     pcibr_soft_t            pcibr_soft = (pcibr_soft_t) pciio_info_mfast_get(pciio_info);
  2361.     devfs_handle_t            xconn_vhdl = pcibr_soft->bs_conn;
  2362.     pciio_slot_t            slot;
  2363.     xwidgetnum_t            xio_port;
  2364.     xtalk_dmamap_t          xtalk_dmamap;
  2365.     pcibr_dmamap_t          pcibr_dmamap;
  2366.     int                     ate_count;
  2367.     int                     ate_index;
  2368.     /* merge in forced flags */
  2369.     flags |= pcibr_soft->bs_dma_flags;
  2370.     /*
  2371.      * On SNIA64, these maps are pre-allocated because pcibr_dmamap_alloc()
  2372.      * can be called within an interrupt thread.
  2373.      */
  2374.     pcibr_dmamap = (pcibr_dmamap_t)get_free_pciio_dmamap(pcibr_soft->bs_vhdl);
  2375.     if (!pcibr_dmamap)
  2376. return 0;
  2377.     xtalk_dmamap = xtalk_dmamap_alloc(xconn_vhdl, dev_desc, req_size_max,
  2378.       flags & DMAMAP_FLAGS);
  2379.     if (!xtalk_dmamap) {
  2380. #if PCIBR_ATE_DEBUG
  2381. printk("pcibr_attach: xtalk_dmamap_alloc failedn");
  2382. #endif
  2383. free_pciio_dmamap(pcibr_dmamap);
  2384. return 0;
  2385.     }
  2386.     xio_port = pcibr_soft->bs_mxid;
  2387.     slot = pciio_info_slot_get(pciio_info);
  2388.     pcibr_dmamap->bd_dev = pconn_vhdl;
  2389.     pcibr_dmamap->bd_slot = slot;
  2390.     pcibr_dmamap->bd_soft = pcibr_soft;
  2391.     pcibr_dmamap->bd_xtalk = xtalk_dmamap;
  2392.     pcibr_dmamap->bd_max_size = req_size_max;
  2393.     pcibr_dmamap->bd_xio_port = xio_port;
  2394.     if (flags & PCIIO_DMA_A64) {
  2395. if (!pcibr_try_set_device(pcibr_soft, slot, flags, BRIDGE_DEV_D64_BITS)) {
  2396.     iopaddr_t               pci_addr;
  2397.     int                     have_rrbs;
  2398.     int                     min_rrbs;
  2399.     /* Device is capable of A64 operations,
  2400.      * and the attributes of the DMA are
  2401.      * consistant with any previous DMA
  2402.      * mappings using shared resources.
  2403.      */
  2404.     pci_addr = pcibr_flags_to_d64(flags, pcibr_soft);
  2405.     pcibr_dmamap->bd_flags = flags;
  2406.     pcibr_dmamap->bd_xio_addr = 0;
  2407.     pcibr_dmamap->bd_pci_addr = pci_addr;
  2408.     /* Make sure we have an RRB (or two).
  2409.      */
  2410.     if (!(pcibr_soft->bs_rrb_fixed & (1 << slot))) {
  2411. if (flags & PCIBR_VCHAN1)
  2412.     slot += PCIBR_RRB_SLOT_VIRTUAL;
  2413. have_rrbs = pcibr_soft->bs_rrb_valid[slot];
  2414. if (have_rrbs < 2) {
  2415.     if (pci_addr & PCI64_ATTR_PREF)
  2416. min_rrbs = 2;
  2417.     else
  2418. min_rrbs = 1;
  2419.     if (have_rrbs < min_rrbs)
  2420. do_pcibr_rrb_autoalloc(pcibr_soft, slot, min_rrbs - have_rrbs);
  2421. }
  2422.     }
  2423. #if PCIBR_ATE_DEBUG
  2424.     printk("pcibr_dmamap_alloc: using direct64n");
  2425. #endif
  2426.     return pcibr_dmamap;
  2427. }
  2428. #if PCIBR_ATE_DEBUG
  2429. printk("pcibr_dmamap_alloc: unable to use direct64n");
  2430. #endif
  2431. flags &= ~PCIIO_DMA_A64;
  2432.     }
  2433.     if (flags & PCIIO_FIXED) {
  2434. /* warning: mappings may fail later,
  2435.  * if direct32 can't get to the address.
  2436.  */
  2437. if (!pcibr_try_set_device(pcibr_soft, slot, flags, BRIDGE_DEV_D32_BITS)) {
  2438.     /* User desires DIRECT A32 operations,
  2439.      * and the attributes of the DMA are
  2440.      * consistant with any previous DMA
  2441.      * mappings using shared resources.
  2442.      * Mapping calls may fail if target
  2443.      * is outside the direct32 range.
  2444.      */
  2445. #if PCIBR_ATE_DEBUG
  2446.     printk("pcibr_dmamap_alloc: using direct32n");
  2447. #endif
  2448.     pcibr_dmamap->bd_flags = flags;
  2449.     pcibr_dmamap->bd_xio_addr = pcibr_soft->bs_dir_xbase;
  2450.     pcibr_dmamap->bd_pci_addr = PCI32_DIRECT_BASE;
  2451.     return pcibr_dmamap;
  2452. }
  2453. #if PCIBR_ATE_DEBUG
  2454. printk("pcibr_dmamap_alloc: unable to use direct32n");
  2455. #endif
  2456. /* If the user demands FIXED and we can't
  2457.  * give it to him, fail.
  2458.  */
  2459. xtalk_dmamap_free(xtalk_dmamap);
  2460. free_pciio_dmamap(pcibr_dmamap);
  2461. return 0;
  2462.     }
  2463.     /*
  2464.      * Allocate Address Translation Entries from the mapping RAM.
  2465.      * Unless the PCIBR_NO_ATE_ROUNDUP flag is specified,
  2466.      * the maximum number of ATEs is based on the worst-case
  2467.      * scenario, where the requested target is in the
  2468.      * last byte of an ATE; thus, mapping IOPGSIZE+2
  2469.      * does end up requiring three ATEs.
  2470.      */
  2471.     if (!(flags & PCIBR_NO_ATE_ROUNDUP)) {
  2472. ate_count = IOPG((IOPGSIZE - 1) /* worst case start offset */
  2473.      +req_size_max /* max mapping bytes */
  2474.      - 1) + 1; /* round UP */
  2475.     } else { /* assume requested target is page aligned */
  2476. ate_count = IOPG(req_size_max   /* max mapping bytes */
  2477.      - 1) + 1; /* round UP */
  2478.     }
  2479.     ate_index = pcibr_ate_alloc(pcibr_soft, ate_count);
  2480.     if (ate_index != -1) {
  2481. if (!pcibr_try_set_device(pcibr_soft, slot, flags, BRIDGE_DEV_PMU_BITS)) {
  2482.     bridge_ate_t            ate_proto;
  2483.     int                     have_rrbs;
  2484.     int                     min_rrbs;
  2485. #if PCIBR_ATE_DEBUG
  2486.     printk("pcibr_dmamap_alloc: using PMUn");
  2487. #endif
  2488.     ate_proto = pcibr_flags_to_ate(flags);
  2489.     pcibr_dmamap->bd_flags = flags;
  2490.     pcibr_dmamap->bd_pci_addr =
  2491. PCI32_MAPPED_BASE + IOPGSIZE * ate_index;
  2492.     /*
  2493.      * for xbridge the byte-swap bit == bit 29 of PCI address
  2494.      */
  2495.     if (pcibr_soft->bs_xbridge) {
  2496.     if (flags & PCIIO_BYTE_STREAM)
  2497.     ATE_SWAP_ON(pcibr_dmamap->bd_pci_addr);
  2498.     /*
  2499.      * If swap was set in bss_device in pcibr_endian_set()
  2500.      * we need to change the address bit.
  2501.      */
  2502.     if (pcibr_soft->bs_slot[slot].bss_device & 
  2503. BRIDGE_DEV_SWAP_PMU)
  2504.     ATE_SWAP_ON(pcibr_dmamap->bd_pci_addr);
  2505.     if (flags & PCIIO_WORD_VALUES)
  2506.     ATE_SWAP_OFF(pcibr_dmamap->bd_pci_addr);
  2507.     }
  2508.     pcibr_dmamap->bd_xio_addr = 0;
  2509.     pcibr_dmamap->bd_ate_ptr = pcibr_ate_addr(pcibr_soft, ate_index);
  2510.     pcibr_dmamap->bd_ate_index = ate_index;
  2511.     pcibr_dmamap->bd_ate_count = ate_count;
  2512.     pcibr_dmamap->bd_ate_proto = ate_proto;
  2513.     /* Make sure we have an RRB (or two).
  2514.      */
  2515.     if (!(pcibr_soft->bs_rrb_fixed & (1 << slot))) {
  2516. have_rrbs = pcibr_soft->bs_rrb_valid[slot];
  2517. if (have_rrbs < 2) {
  2518.     if (ate_proto & ATE_PREF)
  2519. min_rrbs = 2;
  2520.     else
  2521. min_rrbs = 1;
  2522.     if (have_rrbs < min_rrbs)
  2523. do_pcibr_rrb_autoalloc(pcibr_soft, slot, min_rrbs - have_rrbs);
  2524. }
  2525.     }
  2526.     if (ate_index >= pcibr_soft->bs_int_ate_size && 
  2527. !pcibr_soft->bs_xbridge) {
  2528. bridge_t               *bridge = pcibr_soft->bs_base;
  2529. volatile unsigned      *cmd_regp;
  2530. unsigned                cmd_reg;
  2531. unsigned long           s;
  2532. pcibr_dmamap->bd_flags |= PCIBR_DMAMAP_SSRAM;
  2533. s = pcibr_lock(pcibr_soft);
  2534. cmd_regp = &(bridge->
  2535.      b_type0_cfg_dev[slot].
  2536.      l[PCI_CFG_COMMAND / 4]);
  2537. cmd_reg = *cmd_regp;
  2538. pcibr_soft->bs_slot[slot].bss_cmd_pointer = cmd_regp;
  2539. pcibr_soft->bs_slot[slot].bss_cmd_shadow = cmd_reg;
  2540. pcibr_unlock(pcibr_soft, s);
  2541.     }
  2542.     return pcibr_dmamap;
  2543. }
  2544. #if PCIBR_ATE_DEBUG
  2545. printk("pcibr_dmamap_alloc: unable to use PMUn");
  2546. #endif
  2547. pcibr_ate_free(pcibr_soft, ate_index, ate_count);
  2548.     }
  2549.     /* total failure: sorry, you just can't
  2550.      * get from here to there that way.
  2551.      */
  2552. #if PCIBR_ATE_DEBUG
  2553.     printk("pcibr_dmamap_alloc: complete failure.n");
  2554. #endif
  2555.     xtalk_dmamap_free(xtalk_dmamap);
  2556.     free_pciio_dmamap(pcibr_dmamap);
  2557.     return 0;
  2558. }
  2559. /*ARGSUSED */
  2560. void
  2561. pcibr_dmamap_free(pcibr_dmamap_t pcibr_dmamap)
  2562. {
  2563.     pcibr_soft_t            pcibr_soft = pcibr_dmamap->bd_soft;
  2564.     pciio_slot_t            slot = pcibr_dmamap->bd_slot;
  2565.     unsigned                flags = pcibr_dmamap->bd_flags;
  2566.     /* Make sure that bss_ext_ates_active
  2567.      * is properly kept up to date.
  2568.      */
  2569.     if (PCIBR_DMAMAP_BUSY & flags)
  2570. if (PCIBR_DMAMAP_SSRAM & flags)
  2571.     atomic_dec(&(pcibr_soft->bs_slot[slot]. bss_ext_ates_active));
  2572.     xtalk_dmamap_free(pcibr_dmamap->bd_xtalk);
  2573.     if (pcibr_dmamap->bd_flags & PCIIO_DMA_A64) {
  2574. pcibr_release_device(pcibr_soft, slot, BRIDGE_DEV_D64_BITS);
  2575.     }
  2576.     if (pcibr_dmamap->bd_ate_count) {
  2577. pcibr_ate_free(pcibr_dmamap->bd_soft,
  2578.        pcibr_dmamap->bd_ate_index,
  2579.        pcibr_dmamap->bd_ate_count);
  2580. pcibr_release_device(pcibr_soft, slot, BRIDGE_DEV_PMU_BITS);
  2581.     }
  2582.     free_pciio_dmamap(pcibr_dmamap);
  2583. }
  2584. /*
  2585.  *    pcibr_addr_xio_to_pci: given a PIO range, hand
  2586.  *      back the corresponding base PCI MEM address;
  2587.  *      this is used to short-circuit DMA requests that
  2588.  *      loop back onto this PCI bus.
  2589.  */
  2590. static iopaddr_t
  2591. pcibr_addr_xio_to_pci(pcibr_soft_t soft,
  2592.       iopaddr_t xio_addr,
  2593.       size_t req_size)
  2594. {
  2595.     iopaddr_t               xio_lim = xio_addr + req_size - 1;
  2596.     iopaddr_t               pci_addr;
  2597.     pciio_slot_t            slot;
  2598.     if ((xio_addr >= BRIDGE_PCI_MEM32_BASE) &&
  2599. (xio_lim <= BRIDGE_PCI_MEM32_LIMIT)) {
  2600. pci_addr = xio_addr - BRIDGE_PCI_MEM32_BASE;
  2601. return pci_addr;
  2602.     }
  2603.     if ((xio_addr >= BRIDGE_PCI_MEM64_BASE) &&
  2604. (xio_lim <= BRIDGE_PCI_MEM64_LIMIT)) {
  2605. pci_addr = xio_addr - BRIDGE_PCI_MEM64_BASE;
  2606. return pci_addr;
  2607.     }
  2608.     for (slot = 0; slot < 8; ++slot)
  2609. if ((xio_addr >= BRIDGE_DEVIO(slot)) &&
  2610.     (xio_lim < BRIDGE_DEVIO(slot + 1))) {
  2611.     bridgereg_t             dev;
  2612.     dev = soft->bs_slot[slot].bss_device;
  2613.     pci_addr = dev & BRIDGE_DEV_OFF_MASK;
  2614.     pci_addr <<= BRIDGE_DEV_OFF_ADDR_SHFT;
  2615.     pci_addr += xio_addr - BRIDGE_DEVIO(slot);
  2616.     return (dev & BRIDGE_DEV_DEV_IO_MEM) ? pci_addr : PCI_NOWHERE;
  2617. }
  2618.     return 0;
  2619. }
  2620. /*ARGSUSED */
  2621. iopaddr_t
  2622. pcibr_dmamap_addr(pcibr_dmamap_t pcibr_dmamap,
  2623.   paddr_t paddr,
  2624.   size_t req_size)
  2625. {
  2626.     pcibr_soft_t            pcibr_soft;
  2627.     iopaddr_t               xio_addr;
  2628.     xwidgetnum_t            xio_port;
  2629.     iopaddr_t               pci_addr;
  2630.     unsigned                flags;
  2631.     ASSERT(pcibr_dmamap != NULL);
  2632.     ASSERT(req_size > 0);
  2633.     ASSERT(req_size <= pcibr_dmamap->bd_max_size);
  2634.     pcibr_soft = pcibr_dmamap->bd_soft;
  2635.     flags = pcibr_dmamap->bd_flags;
  2636.     xio_addr = xtalk_dmamap_addr(pcibr_dmamap->bd_xtalk, paddr, req_size);
  2637.     if (XIO_PACKED(xio_addr)) {
  2638. xio_port = XIO_PORT(xio_addr);
  2639. xio_addr = XIO_ADDR(xio_addr);
  2640.     } else
  2641. xio_port = pcibr_dmamap->bd_xio_port;
  2642.     /* If this DMA is to an address that
  2643.      * refers back to this Bridge chip,
  2644.      * reduce it back to the correct
  2645.      * PCI MEM address.
  2646.      */
  2647.     if (xio_port == pcibr_soft->bs_xid) {
  2648. pci_addr = pcibr_addr_xio_to_pci(pcibr_soft, xio_addr, req_size);
  2649.     } else if (flags & PCIIO_DMA_A64) {
  2650. /* A64 DMA:
  2651.  * always use 64-bit direct mapping,
  2652.  * which always works.
  2653.  * Device(x) was set up during
  2654.  * dmamap allocation.
  2655.  */
  2656. /* attributes are already bundled up into bd_pci_addr.
  2657.  */
  2658. pci_addr = pcibr_dmamap->bd_pci_addr
  2659.     | ((uint64_t) xio_port << PCI64_ATTR_TARG_SHFT)
  2660.     | xio_addr;
  2661. /* Bridge Hardware WAR #482836:
  2662.  * If the transfer is not cache aligned
  2663.  * and the Bridge Rev is <= B, force
  2664.  * prefetch to be off.
  2665.  */
  2666. if (flags & PCIBR_NOPREFETCH)
  2667.     pci_addr &= ~PCI64_ATTR_PREF;
  2668. #if DEBUG && PCIBR_DMA_DEBUG
  2669. printk("pcibr_dmamap_addr (direct64):n"
  2670. "twanted paddr [0x%x..0x%x]n"
  2671. "tXIO port 0x%x offset 0x%xn"
  2672. "treturning PCI 0x%xn",
  2673. paddr, paddr + req_size - 1,
  2674. xio_port, xio_addr, pci_addr);
  2675. #endif
  2676.     } else if (flags & PCIIO_FIXED) {
  2677. /* A32 direct DMA:
  2678.  * always use 32-bit direct mapping,
  2679.  * which may fail.
  2680.  * Device(x) was set up during
  2681.  * dmamap allocation.
  2682.  */
  2683. if (xio_port != pcibr_soft->bs_dir_xport)
  2684.     pci_addr = 0; /* wrong DIDN */
  2685. else if (xio_addr < pcibr_dmamap->bd_xio_addr)
  2686.     pci_addr = 0; /* out of range */
  2687. else if ((xio_addr + req_size) >
  2688.  (pcibr_dmamap->bd_xio_addr + BRIDGE_DMA_DIRECT_SIZE))
  2689.     pci_addr = 0; /* out of range */
  2690. else
  2691.     pci_addr = pcibr_dmamap->bd_pci_addr +
  2692. xio_addr - pcibr_dmamap->bd_xio_addr;
  2693. #if DEBUG && PCIBR_DMA_DEBUG
  2694. printk("pcibr_dmamap_addr (direct32):n"
  2695. "twanted paddr [0x%x..0x%x]n"
  2696. "tXIO port 0x%x offset 0x%xn"
  2697. "treturning PCI 0x%xn",
  2698. paddr, paddr + req_size - 1,
  2699. xio_port, xio_addr, pci_addr);
  2700. #endif
  2701.     } else {
  2702. bridge_t               *bridge = pcibr_soft->bs_base;
  2703. iopaddr_t               offset = IOPGOFF(xio_addr);
  2704. bridge_ate_t            ate_proto = pcibr_dmamap->bd_ate_proto;
  2705. int                     ate_count = IOPG(offset + req_size - 1) + 1;
  2706. int                     ate_index = pcibr_dmamap->bd_ate_index;
  2707. unsigned                cmd_regs[8];
  2708. unsigned                s;
  2709. #if PCIBR_FREEZE_TIME
  2710. int                     ate_total = ate_count;
  2711. unsigned                freeze_time;
  2712. #endif
  2713. #if PCIBR_ATE_DEBUG
  2714. bridge_ate_t            ate_cmp;
  2715. bridge_ate_p            ate_cptr;
  2716. unsigned                ate_lo, ate_hi;
  2717. int                     ate_bad = 0;
  2718. int                     ate_rbc = 0;
  2719. #endif
  2720. bridge_ate_p            ate_ptr = pcibr_dmamap->bd_ate_ptr;
  2721. bridge_ate_t            ate;
  2722. /* Bridge Hardware WAR #482836:
  2723.  * If the transfer is not cache aligned
  2724.  * and the Bridge Rev is <= B, force
  2725.  * prefetch to be off.
  2726.  */
  2727. if (flags & PCIBR_NOPREFETCH)
  2728.     ate_proto &= ~ATE_PREF;
  2729. ate = ate_proto
  2730.     | (xio_port << ATE_TIDSHIFT)
  2731.     | (xio_addr - offset);
  2732. pci_addr = pcibr_dmamap->bd_pci_addr + offset;
  2733. /* Fill in our mapping registers
  2734.  * with the appropriate xtalk data,
  2735.  * and hand back the PCI address.
  2736.  */
  2737. ASSERT(ate_count > 0);
  2738. if (ate_count <= pcibr_dmamap->bd_ate_count) {
  2739. ATE_FREEZE();
  2740. ATE_WRITE();
  2741. ATE_THAW();
  2742. bridge->b_wid_tflush; /* wait until Bridge PIO complete */
  2743. } else {
  2744. /* The number of ATE's required is greater than the number
  2745.  * allocated for this map. One way this can happen is if
  2746.  * pcibr_dmamap_alloc() was called with the PCIBR_NO_ATE_ROUNDUP
  2747.  * flag, and then when that map is used (right now), the
  2748.  * target address tells us we really did need to roundup.
  2749.  * The other possibility is that the map is just plain too
  2750.  * small to handle the requested target area.
  2751.  */
  2752. #if PCIBR_ATE_DEBUG
  2753. printk(KERN_WARNING "pcibr_dmamap_addr :n"
  2754. "twanted paddr [0x%x..0x%x]n"
  2755. "tate_count 0x%x bd_ate_count 0x%xn"
  2756. "tATE's required > number allocatedn",
  2757. paddr, paddr + req_size - 1,
  2758. ate_count, pcibr_dmamap->bd_ate_count);
  2759. #endif
  2760. pci_addr = 0;
  2761. }
  2762.     }
  2763.     return pci_addr;
  2764. }
  2765. /*ARGSUSED */
  2766. alenlist_t
  2767. pcibr_dmamap_list(pcibr_dmamap_t pcibr_dmamap,
  2768.   alenlist_t palenlist,
  2769.   unsigned flags)
  2770. {
  2771.     pcibr_soft_t            pcibr_soft;
  2772.     bridge_t               *bridge=NULL;
  2773.     unsigned                al_flags = (flags & PCIIO_NOSLEEP) ? AL_NOSLEEP : 0;
  2774.     int                     inplace = flags & PCIIO_INPLACE;
  2775.     alenlist_t              pciio_alenlist = 0;
  2776.     alenlist_t              xtalk_alenlist;
  2777.     size_t                  length;
  2778.     iopaddr_t               offset;
  2779.     unsigned                direct64;
  2780.     int                     ate_index = 0;
  2781.     int                     ate_count = 0;
  2782.     int                     ate_total = 0;
  2783.     bridge_ate_p            ate_ptr = (bridge_ate_p)0;
  2784.     bridge_ate_t            ate_proto = (bridge_ate_t)0;
  2785.     bridge_ate_t            ate_prev;
  2786.     bridge_ate_t            ate;
  2787.     alenaddr_t              xio_addr;
  2788.     xwidgetnum_t            xio_port;
  2789.     iopaddr_t               pci_addr;
  2790.     alenaddr_t              new_addr;
  2791.     unsigned                cmd_regs[8];
  2792.     unsigned                s = 0;
  2793. #if PCIBR_FREEZE_TIME
  2794.     unsigned                freeze_time;
  2795. #endif
  2796.     int     ate_freeze_done = 0; /* To pair ATE_THAW
  2797.  * with an ATE_FREEZE
  2798.  */
  2799.     pcibr_soft = pcibr_dmamap->bd_soft;
  2800.     xtalk_alenlist = xtalk_dmamap_list(pcibr_dmamap->bd_xtalk, palenlist,
  2801.        flags & DMAMAP_FLAGS);
  2802.     if (!xtalk_alenlist)
  2803. goto fail;
  2804.     alenlist_cursor_init(xtalk_alenlist, 0, NULL);
  2805.     if (inplace) {
  2806. pciio_alenlist = xtalk_alenlist;
  2807.     } else {
  2808. pciio_alenlist = alenlist_create(al_flags);
  2809. if (!pciio_alenlist)
  2810.     goto fail;
  2811.     }
  2812.     direct64 = pcibr_dmamap->bd_flags & PCIIO_DMA_A64;
  2813.     if (!direct64) {
  2814. bridge = pcibr_soft->bs_base;
  2815. ate_ptr = pcibr_dmamap->bd_ate_ptr;
  2816. ate_index = pcibr_dmamap->bd_ate_index;
  2817. ate_proto = pcibr_dmamap->bd_ate_proto;
  2818. ATE_FREEZE();
  2819. ate_freeze_done = 1; /* Remember that we need to do an ATE_THAW */
  2820.     }
  2821.     pci_addr = pcibr_dmamap->bd_pci_addr;
  2822.     ate_prev = 0; /* matches no valid ATEs */
  2823.     while (ALENLIST_SUCCESS ==
  2824.    alenlist_get(xtalk_alenlist, NULL, 0,
  2825. &xio_addr, &length, al_flags)) {
  2826. if (XIO_PACKED(xio_addr)) {
  2827.     xio_port = XIO_PORT(xio_addr);
  2828.     xio_addr = XIO_ADDR(xio_addr);
  2829. } else
  2830.     xio_port = pcibr_dmamap->bd_xio_port;
  2831. if (xio_port == pcibr_soft->bs_xid) {
  2832.     new_addr = pcibr_addr_xio_to_pci(pcibr_soft, xio_addr, length);
  2833.     if (new_addr == PCI_NOWHERE)
  2834. goto fail;
  2835. } else if (direct64) {
  2836.     new_addr = pci_addr | xio_addr
  2837. | ((uint64_t) xio_port << PCI64_ATTR_TARG_SHFT);
  2838.     /* Bridge Hardware WAR #482836:
  2839.      * If the transfer is not cache aligned
  2840.      * and the Bridge Rev is <= B, force
  2841.      * prefetch to be off.
  2842.      */
  2843.     if (flags & PCIBR_NOPREFETCH)
  2844. new_addr &= ~PCI64_ATTR_PREF;
  2845. } else {
  2846.     /* calculate the ate value for
  2847.      * the first address. If it
  2848.      * matches the previous
  2849.      * ATE written (ie. we had
  2850.      * multiple blocks in the
  2851.      * same IOPG), then back up
  2852.      * and reuse that ATE.
  2853.      *
  2854.      * We are NOT going to
  2855.      * aggressively try to
  2856.      * reuse any other ATEs.
  2857.      */
  2858.     offset = IOPGOFF(xio_addr);
  2859.     ate = ate_proto
  2860. | (xio_port << ATE_TIDSHIFT)
  2861. | (xio_addr - offset);
  2862.     if (ate == ate_prev) {
  2863. #if PCIBR_ATE_DEBUG
  2864. printk("pcibr_dmamap_list: ATE sharen");
  2865. #endif
  2866. ate_ptr--;
  2867. ate_index--;
  2868. pci_addr -= IOPGSIZE;
  2869.     }
  2870.     new_addr = pci_addr + offset;
  2871.     /* Fill in the hardware ATEs
  2872.      * that contain this block.
  2873.      */
  2874.     ate_count = IOPG(offset + length - 1) + 1;
  2875.     ate_total += ate_count;
  2876.     /* Ensure that this map contains enough ATE's */
  2877.     if (ate_total > pcibr_dmamap->bd_ate_count) {
  2878. #if PCIBR_ATE_DEBUG
  2879. printk(KERN_WARNING "pcibr_dmamap_list :n"
  2880. "twanted xio_addr [0x%x..0x%x]n"
  2881. "tate_total 0x%x bd_ate_count 0x%xn"
  2882. "tATE's required > number allocatedn",
  2883. xio_addr, xio_addr + length - 1,
  2884. ate_total, pcibr_dmamap->bd_ate_count);
  2885. #endif
  2886. goto fail;
  2887.     }
  2888.     ATE_WRITE();
  2889.     ate_index += ate_count;
  2890.     ate_ptr += ate_count;
  2891.     ate_count <<= IOPFNSHIFT;
  2892.     ate += ate_count;
  2893.     pci_addr += ate_count;
  2894. }
  2895. /* write the PCI DMA address
  2896.  * out to the scatter-gather list.
  2897.  */
  2898. if (inplace) {
  2899.     if (ALENLIST_SUCCESS !=
  2900. alenlist_replace(pciio_alenlist, NULL,
  2901.  &new_addr, &length, al_flags))
  2902. goto fail;
  2903. } else {
  2904.     if (ALENLIST_SUCCESS !=
  2905. alenlist_append(pciio_alenlist,
  2906. new_addr, length, al_flags))
  2907. goto fail;
  2908. }
  2909.     }
  2910.     if (!inplace)
  2911. alenlist_done(xtalk_alenlist);
  2912.     /* Reset the internal cursor of the alenlist to be returned back
  2913.      * to the caller.
  2914.      */
  2915.     alenlist_cursor_init(pciio_alenlist, 0, NULL);
  2916.     /* In case an ATE_FREEZE was done do the ATE_THAW to unroll all the
  2917.      * changes that ATE_FREEZE has done to implement the external SSRAM
  2918.      * bug workaround.
  2919.      */
  2920.     if (ate_freeze_done) {
  2921. ATE_THAW();
  2922. bridge->b_wid_tflush; /* wait until Bridge PIO complete */
  2923.     }
  2924.     return pciio_alenlist;
  2925.   fail:
  2926.     /* There are various points of failure after doing an ATE_FREEZE
  2927.      * We need to do an ATE_THAW. Otherwise the ATEs are locked forever.
  2928.      * The decision to do an ATE_THAW needs to be based on whether a
  2929.      * an ATE_FREEZE was done before.
  2930.      */
  2931.     if (ate_freeze_done) {
  2932. ATE_THAW();
  2933. bridge->b_wid_tflush;
  2934.     }
  2935.     if (pciio_alenlist && !inplace)
  2936. alenlist_destroy(pciio_alenlist);
  2937.     return 0;
  2938. }
  2939. /*ARGSUSED */
  2940. void
  2941. pcibr_dmamap_done(pcibr_dmamap_t pcibr_dmamap)
  2942. {
  2943.     /*
  2944.      * We could go through and invalidate ATEs here;
  2945.      * for performance reasons, we don't.
  2946.      * We also don't enforce the strict alternation
  2947.      * between _addr/_list and _done, but Hub does.
  2948.      */
  2949.     if (pcibr_dmamap->bd_flags & PCIBR_DMAMAP_BUSY) {
  2950. pcibr_dmamap->bd_flags &= ~PCIBR_DMAMAP_BUSY;
  2951. if (pcibr_dmamap->bd_flags & PCIBR_DMAMAP_SSRAM)
  2952.     atomic_dec(&(pcibr_dmamap->bd_soft->bs_slot[pcibr_dmamap->bd_slot]. bss_ext_ates_active));
  2953.     }
  2954.     xtalk_dmamap_done(pcibr_dmamap->bd_xtalk);
  2955. }
  2956. /*
  2957.  * For each bridge, the DIR_OFF value in the Direct Mapping Register
  2958.  * determines the PCI to Crosstalk memory mapping to be used for all
  2959.  * 32-bit Direct Mapping memory accesses. This mapping can be to any
  2960.  * node in the system. This function will return that compact node id.
  2961.  */
  2962. /*ARGSUSED */
  2963. cnodeid_t
  2964. pcibr_get_dmatrans_node(devfs_handle_t pconn_vhdl)
  2965. {
  2966. pciio_info_t pciio_info = pciio_info_get(pconn_vhdl);
  2967. pcibr_soft_t pcibr_soft = (pcibr_soft_t) pciio_info_mfast_get(pciio_info);
  2968. return(NASID_TO_COMPACT_NODEID(NASID_GET(pcibr_soft->bs_dir_xbase)));
  2969. }
  2970. /*ARGSUSED */
  2971. iopaddr_t
  2972. pcibr_dmatrans_addr(devfs_handle_t pconn_vhdl,
  2973.     device_desc_t dev_desc,
  2974.     paddr_t paddr,
  2975.     size_t req_size,
  2976.     unsigned flags)
  2977. {
  2978.     pciio_info_t            pciio_info = pciio_info_get(pconn_vhdl);
  2979.     pcibr_soft_t            pcibr_soft = (pcibr_soft_t) pciio_info_mfast_get(pciio_info);
  2980.     devfs_handle_t            xconn_vhdl = pcibr_soft->bs_conn;
  2981.     pciio_slot_t            pciio_slot = pciio_info_slot_get(pciio_info);
  2982.     pcibr_soft_slot_t       slotp = &pcibr_soft->bs_slot[pciio_slot];
  2983.     xwidgetnum_t            xio_port;
  2984.     iopaddr_t               xio_addr;
  2985.     iopaddr_t               pci_addr;
  2986.     int                     have_rrbs;
  2987.     int                     min_rrbs;
  2988.     /* merge in forced flags */
  2989.     flags |= pcibr_soft->bs_dma_flags;
  2990.     xio_addr = xtalk_dmatrans_addr(xconn_vhdl, 0, paddr, req_size,
  2991.    flags & DMAMAP_FLAGS);
  2992.     if (!xio_addr) {
  2993. #if PCIBR_DMA_DEBUG
  2994. printk("pcibr_dmatrans_addr:n"
  2995. "tpciio connection point %vn"
  2996. "txtalk connection point %vn"
  2997. "twanted paddr [0x%x..0x%x]n"
  2998. "txtalk_dmatrans_addr returned 0x%xn",
  2999. pconn_vhdl, xconn_vhdl,
  3000. paddr, paddr + req_size - 1,
  3001. xio_addr);
  3002. #endif
  3003. return 0;
  3004.     }
  3005.     /*
  3006.      * find which XIO port this goes to.
  3007.      */
  3008.     if (XIO_PACKED(xio_addr)) {
  3009. if (xio_addr == XIO_NOWHERE) {
  3010. #if PCIBR_DMA_DEBUG
  3011.     printk("pcibr_dmatrans_addr:n"
  3012.     "tpciio connection point %vn"
  3013.     "txtalk connection point %vn"
  3014.     "twanted paddr [0x%x..0x%x]n"
  3015.     "txtalk_dmatrans_addr returned 0x%xn",
  3016.     pconn_vhdl, xconn_vhdl,
  3017.     paddr, paddr + req_size - 1,
  3018.     xio_addr);
  3019. #endif
  3020.     return 0;
  3021. }
  3022. xio_port = XIO_PORT(xio_addr);
  3023. xio_addr = XIO_ADDR(xio_addr);
  3024.     } else
  3025. xio_port = pcibr_soft->bs_mxid;
  3026.     /*
  3027.      * If this DMA comes back to us,
  3028.      * return the PCI MEM address on
  3029.      * which it would land, or NULL
  3030.      * if the target is something
  3031.      * on bridge other than PCI MEM.
  3032.      */
  3033.     if (xio_port == pcibr_soft->bs_xid) {
  3034. pci_addr = pcibr_addr_xio_to_pci(pcibr_soft, xio_addr, req_size);
  3035. return pci_addr;
  3036.     }
  3037.     /* If the caller can use A64, try to
  3038.      * satisfy the request with the 64-bit
  3039.      * direct map. This can fail if the
  3040.      * configuration bits in Device(x)
  3041.      * conflict with our flags.
  3042.      */
  3043.     if (flags & PCIIO_DMA_A64) {
  3044. pci_addr = slotp->bss_d64_base;
  3045. if (!(flags & PCIBR_VCHAN1))
  3046.     flags |= PCIBR_VCHAN0;
  3047. if ((pci_addr != PCIBR_D64_BASE_UNSET) &&
  3048.     (flags == slotp->bss_d64_flags)) {
  3049. #ifdef CONFIG_IA64_SGI_SN2
  3050.     pci_addr |= (PHYS_TO_DMA(xio_addr))
  3051. | ((uint64_t) xio_port << PCI64_ATTR_TARG_SHFT);
  3052. #else
  3053.     pci_addr |=  xio_addr
  3054. | ((uint64_t) xio_port << PCI64_ATTR_TARG_SHFT);
  3055. #endif
  3056. #if DEBUG && PCIBR_DMA_DEBUG
  3057. #if HWG_PERF_CHECK
  3058.     if (xio_addr != 0x20000000)
  3059. #endif
  3060. printk("pcibr_dmatrans_addr: [reuse]n"
  3061. "tpciio connection point %vn"
  3062. "txtalk connection point %vn"
  3063. "twanted paddr [0x%x..0x%x]n"
  3064. "txtalk_dmatrans_addr returned 0x%xn"
  3065. "tdirect 64bit address is 0x%xn",
  3066. pconn_vhdl, xconn_vhdl,
  3067. paddr, paddr + req_size - 1,
  3068. xio_addr, pci_addr);
  3069. #endif
  3070.     return (pci_addr);
  3071. }
  3072. if (!pcibr_try_set_device(pcibr_soft, pciio_slot, flags, BRIDGE_DEV_D64_BITS)) {
  3073.     pci_addr = pcibr_flags_to_d64(flags, pcibr_soft);
  3074.     slotp->bss_d64_flags = flags;
  3075.     slotp->bss_d64_base = pci_addr;
  3076. #ifdef CONFIG_IA64_SGI_SN2
  3077.     pci_addr |= (PHYS_TO_DMA(xio_addr))
  3078. | ((uint64_t) xio_port << PCI64_ATTR_TARG_SHFT);
  3079. #else
  3080.             pci_addr |= xio_addr
  3081. | ((uint64_t) xio_port << PCI64_ATTR_TARG_SHFT);
  3082. #endif
  3083.     /* Make sure we have an RRB (or two).
  3084.      */
  3085.     if (!(pcibr_soft->bs_rrb_fixed & (1 << pciio_slot))) {
  3086. if (flags & PCIBR_VCHAN1)
  3087.     pciio_slot += PCIBR_RRB_SLOT_VIRTUAL;
  3088. have_rrbs = pcibr_soft->bs_rrb_valid[pciio_slot];
  3089. if (have_rrbs < 2) {
  3090.     if (pci_addr & PCI64_ATTR_PREF)
  3091. min_rrbs = 2;
  3092.     else
  3093. min_rrbs = 1;
  3094.     if (have_rrbs < min_rrbs)
  3095. do_pcibr_rrb_autoalloc(pcibr_soft, pciio_slot, min_rrbs - have_rrbs);
  3096. }
  3097.     }
  3098. #if PCIBR_DMA_DEBUG
  3099. #if HWG_PERF_CHECK
  3100.     if (xio_addr != 0x20000000)
  3101. #endif
  3102. printk("pcibr_dmatrans_addr:n"
  3103. "tpciio connection point %vn"
  3104. "txtalk connection point %vn"
  3105. "twanted paddr [0x%x..0x%x]n"
  3106. "txtalk_dmatrans_addr returned 0x%xn"
  3107. "tdirect 64bit address is 0x%xn"
  3108. "tnew flags: 0x%xn",
  3109. pconn_vhdl, xconn_vhdl,
  3110. paddr, paddr + req_size - 1,
  3111. xio_addr, pci_addr, (uint64_t) flags);
  3112. #endif
  3113.     return (pci_addr);
  3114. }
  3115. /* our flags conflict with Device(x).
  3116.  */
  3117. flags = flags
  3118.     & ~PCIIO_DMA_A64
  3119.     & ~PCIBR_VCHAN0
  3120.     ;
  3121. #if PCIBR_DMA_DEBUG
  3122. printk("pcibr_dmatrans_addr:n"
  3123. "tpciio connection point %vn"
  3124. "txtalk connection point %vn"
  3125. "twanted paddr [0x%x..0x%x]n"
  3126. "txtalk_dmatrans_addr returned 0x%xn"
  3127. "tUnable to set Device(x) bits for Direct-64n",
  3128. pconn_vhdl, xconn_vhdl,
  3129. paddr, paddr + req_size - 1,
  3130. xio_addr);
  3131. #endif
  3132.     }
  3133.     /* Try to satisfy the request with the 32-bit direct
  3134.      * map. This can fail if the configuration bits in
  3135.      * Device(x) conflict with our flags, or if the
  3136.      * target address is outside where DIR_OFF points.
  3137.      */
  3138.     {
  3139. size_t                  map_size = 1ULL << 31;
  3140. iopaddr_t               xio_base = pcibr_soft->bs_dir_xbase;
  3141. iopaddr_t               offset = xio_addr - xio_base;
  3142. iopaddr_t               endoff = req_size + offset;
  3143. if ((req_size > map_size) ||
  3144.     (xio_addr < xio_base) ||
  3145.     (xio_port != pcibr_soft->bs_dir_xport) ||
  3146.     (endoff > map_size)) {
  3147. #if PCIBR_DMA_DEBUG
  3148.     printk("pcibr_dmatrans_addr:n"
  3149.     "tpciio connection point %vn"
  3150.     "txtalk connection point %vn"
  3151.     "twanted paddr [0x%x..0x%x]n"
  3152.     "txtalk_dmatrans_addr returned 0x%xn"
  3153.     "txio region outside direct32 targetn",
  3154.     pconn_vhdl, xconn_vhdl,
  3155.     paddr, paddr + req_size - 1,
  3156.     xio_addr);
  3157. #endif
  3158. } else {
  3159.     pci_addr = slotp->bss_d32_base;
  3160.     if ((pci_addr != PCIBR_D32_BASE_UNSET) &&
  3161. (flags == slotp->bss_d32_flags)) {
  3162. pci_addr |= offset;
  3163. #if DEBUG && PCIBR_DMA_DEBUG
  3164. printk("pcibr_dmatrans_addr: [reuse]n"
  3165. "tpciio connection point %vn"
  3166. "txtalk connection point %vn"
  3167. "twanted paddr [0x%x..0x%x]n"
  3168. "txtalk_dmatrans_addr returned 0x%xn"
  3169. "tmapped via direct32 offset 0x%xn"
  3170. "twill DMA via pci addr 0x%xn",
  3171. pconn_vhdl, xconn_vhdl,
  3172. paddr, paddr + req_size - 1,
  3173. xio_addr, offset, pci_addr);
  3174. #endif
  3175. return (pci_addr);
  3176.     }
  3177.     if (!pcibr_try_set_device(pcibr_soft, pciio_slot, flags, BRIDGE_DEV_D32_BITS)) {
  3178. pci_addr = PCI32_DIRECT_BASE;
  3179. slotp->bss_d32_flags = flags;
  3180. slotp->bss_d32_base = pci_addr;
  3181. pci_addr |= offset;
  3182. /* Make sure we have an RRB (or two).
  3183.  */
  3184. if (!(pcibr_soft->bs_rrb_fixed & (1 << pciio_slot))) {
  3185.     have_rrbs = pcibr_soft->bs_rrb_valid[pciio_slot];
  3186.     if (have_rrbs < 2) {
  3187. if (slotp->bss_device & BRIDGE_DEV_PREF)
  3188.     min_rrbs = 2;
  3189. else
  3190.     min_rrbs = 1;
  3191. if (have_rrbs < min_rrbs)
  3192.     do_pcibr_rrb_autoalloc(pcibr_soft, pciio_slot, min_rrbs - have_rrbs);
  3193.     }
  3194. }
  3195. #if PCIBR_DMA_DEBUG
  3196. #if HWG_PERF_CHECK
  3197. if (xio_addr != 0x20000000)
  3198. #endif
  3199.     printk("pcibr_dmatrans_addr:n"
  3200.     "tpciio connection point %vn"
  3201.     "txtalk connection point %vn"
  3202.     "twanted paddr [0x%x..0x%x]n"
  3203.     "txtalk_dmatrans_addr returned 0x%xn"
  3204.     "tmapped via direct32 offset 0x%xn"
  3205.     "twill DMA via pci addr 0x%xn"
  3206.     "tnew flags: 0x%xn",
  3207.     pconn_vhdl, xconn_vhdl,
  3208.     paddr, paddr + req_size - 1,
  3209.     xio_addr, offset, pci_addr, (uint64_t) flags);
  3210. #endif
  3211. return (pci_addr);
  3212.     }
  3213.     /* our flags conflict with Device(x).
  3214.      */
  3215. #if PCIBR_DMA_DEBUG
  3216.     printk("pcibr_dmatrans_addr:n"
  3217.     "tpciio connection point %vn"
  3218.     "txtalk connection point %vn"
  3219.     "twanted paddr [0x%x..0x%x]n"
  3220.     "txtalk_dmatrans_addr returned 0x%xn"
  3221.     "tUnable to set Device(x) bits for Direct-32n",
  3222.     pconn_vhdl, xconn_vhdl,
  3223.     paddr, paddr + req_size - 1,
  3224.     xio_addr);
  3225. #endif
  3226. }
  3227.     }
  3228. #if PCIBR_DMA_DEBUG
  3229.     printk("pcibr_dmatrans_addr:n"
  3230.     "tpciio connection point %vn"
  3231.     "txtalk connection point %vn"
  3232.     "twanted paddr [0x%x..0x%x]n"
  3233.     "txtalk_dmatrans_addr returned 0x%xn"
  3234.     "tno acceptable PCI address found or constructablen",
  3235.     pconn_vhdl, xconn_vhdl,
  3236.     paddr, paddr + req_size - 1,
  3237.     xio_addr);
  3238. #endif
  3239.     return 0;
  3240. }
  3241. /*ARGSUSED */
  3242. alenlist_t
  3243. pcibr_dmatrans_list(devfs_handle_t pconn_vhdl,
  3244.     device_desc_t dev_desc,
  3245.     alenlist_t palenlist,
  3246.     unsigned flags)
  3247. {
  3248.     pciio_info_t            pciio_info = pciio_info_get(pconn_vhdl);
  3249.     pcibr_soft_t            pcibr_soft = (pcibr_soft_t) pciio_info_mfast_get(pciio_info);
  3250.     devfs_handle_t            xconn_vhdl = pcibr_soft->bs_conn;
  3251.     pciio_slot_t            pciio_slot = pciio_info_slot_get(pciio_info);
  3252.     pcibr_soft_slot_t       slotp = &pcibr_soft->bs_slot[pciio_slot];
  3253.     xwidgetnum_t            xio_port;
  3254.     alenlist_t              pciio_alenlist = 0;
  3255.     alenlist_t              xtalk_alenlist = 0;
  3256.     int                     inplace;
  3257.     unsigned                direct64;
  3258.     unsigned                al_flags;
  3259.     iopaddr_t               xio_base;
  3260.     alenaddr_t              xio_addr;
  3261.     size_t                  xio_size;
  3262.     size_t                  map_size;
  3263.     iopaddr_t               pci_base;
  3264.     alenaddr_t              pci_addr;
  3265.     unsigned                relbits = 0;
  3266.     /* merge in forced flags */
  3267.     flags |= pcibr_soft->bs_dma_flags;
  3268.     inplace = flags & PCIIO_INPLACE;
  3269.     direct64 = flags & PCIIO_DMA_A64;
  3270.     al_flags = (flags & PCIIO_NOSLEEP) ? AL_NOSLEEP : 0;
  3271.     if (direct64) {
  3272. map_size = 1ull << 48;
  3273. xio_base = 0;
  3274. pci_base = slotp->bss_d64_base;
  3275. if ((pci_base != PCIBR_D64_BASE_UNSET) &&
  3276.     (flags == slotp->bss_d64_flags)) {
  3277.     /* reuse previous base info */
  3278. } else if (pcibr_try_set_device(pcibr_soft, pciio_slot, flags, BRIDGE_DEV_D64_BITS) < 0) {
  3279.     /* DMA configuration conflict */
  3280.     goto fail;
  3281. } else {
  3282.     relbits = BRIDGE_DEV_D64_BITS;
  3283.     pci_base =
  3284. pcibr_flags_to_d64(flags, pcibr_soft);
  3285. }
  3286.     } else {
  3287. xio_base = pcibr_soft->bs_dir_xbase;
  3288. map_size = 1ull << 31;
  3289. pci_base = slotp->bss_d32_base;
  3290. if ((pci_base != PCIBR_D32_BASE_UNSET) &&
  3291.     (flags == slotp->bss_d32_flags)) {
  3292.     /* reuse previous base info */
  3293. } else if (pcibr_try_set_device(pcibr_soft, pciio_slot, flags, BRIDGE_DEV_D32_BITS) < 0) {
  3294.     /* DMA configuration conflict */
  3295.     goto fail;
  3296. } else {
  3297.     relbits = BRIDGE_DEV_D32_BITS;
  3298.     pci_base = PCI32_DIRECT_BASE;
  3299. }
  3300.     }
  3301.     xtalk_alenlist = xtalk_dmatrans_list(xconn_vhdl, 0, palenlist,
  3302.  flags & DMAMAP_FLAGS);
  3303.     if (!xtalk_alenlist)
  3304. goto fail;
  3305.     alenlist_cursor_init(xtalk_alenlist, 0, NULL);
  3306.     if (inplace) {
  3307. pciio_alenlist = xtalk_alenlist;
  3308.     } else {
  3309. pciio_alenlist = alenlist_create(al_flags);
  3310. if (!pciio_alenlist)
  3311.     goto fail;
  3312.     }
  3313.     while (ALENLIST_SUCCESS ==
  3314.    alenlist_get(xtalk_alenlist, NULL, 0,
  3315. &xio_addr, &xio_size, al_flags)) {
  3316. /*
  3317.  * find which XIO port this goes to.
  3318.  */
  3319. if (XIO_PACKED(xio_addr)) {
  3320.     if (xio_addr == XIO_NOWHERE) {
  3321. #if PCIBR_DMA_DEBUG
  3322. printk("pcibr_dmatrans_addr:n"
  3323. "tpciio connection point %vn"
  3324. "txtalk connection point %vn"
  3325. "twanted paddr [0x%x..0x%x]n"
  3326. "txtalk_dmatrans_addr returned 0x%xn",
  3327. pconn_vhdl, xconn_vhdl,
  3328. paddr, paddr + req_size - 1,
  3329. xio_addr);
  3330. #endif
  3331. return 0;
  3332.     }
  3333.     xio_port = XIO_PORT(xio_addr);
  3334.     xio_addr = XIO_ADDR(xio_addr);
  3335. } else
  3336.     xio_port = pcibr_soft->bs_mxid;
  3337. /*
  3338.  * If this DMA comes back to us,
  3339.  * return the PCI MEM address on
  3340.  * which it would land, or NULL
  3341.  * if the target is something
  3342.  * on bridge other than PCI MEM.
  3343.  */
  3344. if (xio_port == pcibr_soft->bs_xid) {
  3345.     pci_addr = pcibr_addr_xio_to_pci(pcibr_soft, xio_addr, xio_size);
  3346.     if ( (pci_addr == (alenaddr_t)NULL) )
  3347. goto fail;
  3348. } else if (direct64) {
  3349.     ASSERT(xio_port != 0);
  3350.     pci_addr = pci_base | xio_addr
  3351. | ((uint64_t) xio_port << PCI64_ATTR_TARG_SHFT);
  3352. } else {
  3353.     iopaddr_t               offset = xio_addr - xio_base;
  3354.     iopaddr_t               endoff = xio_size + offset;
  3355.     if ((xio_size > map_size) ||
  3356. (xio_addr < xio_base) ||
  3357. (xio_port != pcibr_soft->bs_dir_xport) ||
  3358. (endoff > map_size))
  3359. goto fail;
  3360.     pci_addr = pci_base + (xio_addr - xio_base);
  3361. }
  3362. /* write the PCI DMA address
  3363.  * out to the scatter-gather list.
  3364.  */
  3365. if (inplace) {
  3366.     if (ALENLIST_SUCCESS !=
  3367. alenlist_replace(pciio_alenlist, NULL,
  3368.  &pci_addr, &xio_size, al_flags))
  3369. goto fail;
  3370. } else {
  3371.     if (ALENLIST_SUCCESS !=
  3372. alenlist_append(pciio_alenlist,
  3373. pci_addr, xio_size, al_flags))
  3374. goto fail;
  3375. }
  3376.     }
  3377.     if (relbits) {
  3378. if (direct64) {
  3379.     slotp->bss_d64_flags = flags;
  3380.     slotp->bss_d64_base = pci_base;
  3381. } else {
  3382.     slotp->bss_d32_flags = flags;
  3383.     slotp->bss_d32_base = pci_base;
  3384. }
  3385.     }
  3386.     if (!inplace)
  3387. alenlist_done(xtalk_alenlist);
  3388.     /* Reset the internal cursor of the alenlist to be returned back
  3389.      * to the caller.
  3390.      */
  3391.     alenlist_cursor_init(pciio_alenlist, 0, NULL);
  3392.     return pciio_alenlist;
  3393.   fail:
  3394.     if (relbits)
  3395. pcibr_release_device(pcibr_soft, pciio_slot, relbits);
  3396.     if (pciio_alenlist && !inplace)
  3397. alenlist_destroy(pciio_alenlist);
  3398.     return 0;
  3399. }
  3400. void
  3401. pcibr_dmamap_drain(pcibr_dmamap_t map)
  3402. {
  3403.     xtalk_dmamap_drain(map->bd_xtalk);
  3404. }
  3405. void
  3406. pcibr_dmaaddr_drain(devfs_handle_t pconn_vhdl,
  3407.     paddr_t paddr,
  3408.     size_t bytes)
  3409. {
  3410.     pciio_info_t            pciio_info = pciio_info_get(pconn_vhdl);
  3411.     pcibr_soft_t            pcibr_soft = (pcibr_soft_t) pciio_info_mfast_get(pciio_info);
  3412.     devfs_handle_t            xconn_vhdl = pcibr_soft->bs_conn;
  3413.     xtalk_dmaaddr_drain(xconn_vhdl, paddr, bytes);
  3414. }
  3415. void
  3416. pcibr_dmalist_drain(devfs_handle_t pconn_vhdl,
  3417.     alenlist_t list)
  3418. {
  3419.     pciio_info_t            pciio_info = pciio_info_get(pconn_vhdl);
  3420.     pcibr_soft_t            pcibr_soft = (pcibr_soft_t) pciio_info_mfast_get(pciio_info);
  3421.     devfs_handle_t            xconn_vhdl = pcibr_soft->bs_conn;
  3422.     xtalk_dmalist_drain(xconn_vhdl, list);
  3423. }
  3424. /*
  3425.  * Get the starting PCIbus address out of the given DMA map.
  3426.  * This function is supposed to be used by a close friend of PCI bridge
  3427.  * since it relies on the fact that the starting address of the map is fixed at
  3428.  * the allocation time in the current implementation of PCI bridge.
  3429.  */
  3430. iopaddr_t
  3431. pcibr_dmamap_pciaddr_get(pcibr_dmamap_t pcibr_dmamap)
  3432. {
  3433.     return (pcibr_dmamap->bd_pci_addr);
  3434. }
  3435. /* =====================================================================
  3436.  *    CONFIGURATION MANAGEMENT
  3437.  */
  3438. /*ARGSUSED */
  3439. void
  3440. pcibr_provider_startup(devfs_handle_t pcibr)
  3441. {
  3442. }
  3443. /*ARGSUSED */
  3444. void
  3445. pcibr_provider_shutdown(devfs_handle_t pcibr)
  3446. {
  3447. }
  3448. int
  3449. pcibr_reset(devfs_handle_t conn)
  3450. {
  3451.     pciio_info_t            pciio_info = pciio_info_get(conn);
  3452.     pciio_slot_t            pciio_slot = pciio_info_slot_get(pciio_info);
  3453.     pcibr_soft_t            pcibr_soft = (pcibr_soft_t) pciio_info_mfast_get(pciio_info);
  3454.     bridge_t               *bridge = pcibr_soft->bs_base;
  3455.     bridgereg_t             ctlreg;
  3456.     unsigned                cfgctl[8];
  3457.     unsigned long           s;
  3458.     int                     f, nf;
  3459.     pcibr_info_h            pcibr_infoh;
  3460.     pcibr_info_t            pcibr_info;
  3461.     int                     win;
  3462.     if (pcibr_soft->bs_slot[pciio_slot].has_host) {
  3463. pciio_slot = pcibr_soft->bs_slot[pciio_slot].host_slot;
  3464. pcibr_info = pcibr_soft->bs_slot[pciio_slot].bss_infos[0];
  3465.     }
  3466.     if (pciio_slot < 4) {
  3467. s = pcibr_lock(pcibr_soft);
  3468. nf = pcibr_soft->bs_slot[pciio_slot].bss_ninfo;
  3469. pcibr_infoh = pcibr_soft->bs_slot[pciio_slot].bss_infos;
  3470. for (f = 0; f < nf; ++f)
  3471.     if (pcibr_infoh[f])
  3472. cfgctl[f] = bridge->b_type0_cfg_dev[pciio_slot].f[f].l[PCI_CFG_COMMAND / 4];
  3473. ctlreg = bridge->b_wid_control;
  3474. bridge->b_wid_control = ctlreg | BRIDGE_CTRL_RST(pciio_slot);
  3475. /* XXX delay? */
  3476. bridge->b_wid_control = ctlreg;
  3477. /* XXX delay? */
  3478. for (f = 0; f < nf; ++f)
  3479.     if ((pcibr_info = pcibr_infoh[f]))
  3480. for (win = 0; win < 6; ++win)
  3481.     if (pcibr_info->f_window[win].w_base != 0)
  3482. bridge->b_type0_cfg_dev[pciio_slot].f[f].l[PCI_CFG_BASE_ADDR(win) / 4] =
  3483.     pcibr_info->f_window[win].w_base;
  3484. for (f = 0; f < nf; ++f)
  3485.     if (pcibr_infoh[f])
  3486. bridge->b_type0_cfg_dev[pciio_slot].f[f].l[PCI_CFG_COMMAND / 4] = cfgctl[f];
  3487. pcibr_unlock(pcibr_soft, s);
  3488. return 0;
  3489.     }
  3490. #ifdef SUPPORT_PRINTING_V_FORMAT
  3491.     printk(KERN_WARNING   "%v: pcibr_reset unimplemented for slot %dn",
  3492.     conn, pciio_slot);
  3493. #endif
  3494.     return -1;
  3495. }
  3496. pciio_endian_t
  3497. pcibr_endian_set(devfs_handle_t pconn_vhdl,
  3498.  pciio_endian_t device_end,
  3499.  pciio_endian_t desired_end)
  3500. {
  3501.     pciio_info_t            pciio_info = pciio_info_get(pconn_vhdl);
  3502.     pciio_slot_t            pciio_slot = pciio_info_slot_get(pciio_info);
  3503.     pcibr_soft_t            pcibr_soft = (pcibr_soft_t) pciio_info_mfast_get(pciio_info);
  3504.     bridgereg_t             devreg;
  3505.     unsigned long           s;
  3506.     /*
  3507.      * Bridge supports hardware swapping; so we can always
  3508.      * arrange for the caller's desired endianness.
  3509.      */
  3510.     s = pcibr_lock(pcibr_soft);
  3511.     devreg = pcibr_soft->bs_slot[pciio_slot].bss_device;
  3512.     if (device_end != desired_end)
  3513. devreg |= BRIDGE_DEV_SWAP_BITS;
  3514.     else
  3515. devreg &= ~BRIDGE_DEV_SWAP_BITS;
  3516.     /* NOTE- if we ever put SWAP bits
  3517.      * onto the disabled list, we will
  3518.      * have to change the logic here.
  3519.      */
  3520.     if (pcibr_soft->bs_slot[pciio_slot].bss_device != devreg) {
  3521. bridge_t               *bridge = pcibr_soft->bs_base;
  3522. bridge->b_device[pciio_slot].reg = devreg;
  3523. pcibr_soft->bs_slot[pciio_slot].bss_device = devreg;
  3524. bridge->b_wid_tflush; /* wait until Bridge PIO complete */
  3525.     }
  3526.     pcibr_unlock(pcibr_soft, s);
  3527. #if DEBUG && PCIBR_DEV_DEBUG
  3528.     printk("pcibr Device(%d): 0x%pn", pciio_slot, bridge->b_device[pciio_slot].reg);
  3529. #endif
  3530.     return desired_end;
  3531. }
  3532. /* This (re)sets the GBR and REALTIME bits and also keeps track of how
  3533.  * many sets are outstanding. Reset succeeds only if the number of outstanding
  3534.  * sets == 1.
  3535.  */
  3536. int
  3537. pcibr_priority_bits_set(pcibr_soft_t pcibr_soft,
  3538. pciio_slot_t pciio_slot,
  3539. pciio_priority_t device_prio)
  3540. {
  3541.     unsigned long           s;
  3542.     int                    *counter;
  3543.     bridgereg_t             rtbits = 0;
  3544.     bridgereg_t             devreg;
  3545.     int                     rc = PRIO_SUCCESS;
  3546.     /* in dual-slot configurations, the host and the
  3547.      * guest have separate DMA resources, so they
  3548.      * have separate requirements for priority bits.
  3549.      */
  3550.     counter = &(pcibr_soft->bs_slot[pciio_slot].bss_pri_uctr);
  3551.     /*
  3552.      * Bridge supports PCI notions of LOW and HIGH priority
  3553.      * arbitration rings via a "REAL_TIME" bit in the per-device
  3554.      * Bridge register. The "GBR" bit controls access to the GBR
  3555.      * ring on the xbow. These two bits are (re)set together.
  3556.      *
  3557.      * XXX- Bug in Rev B Bridge Si:
  3558.      * Symptom: Prefetcher starts operating incorrectly. This happens
  3559.      * due to corruption of the address storage ram in the prefetcher
  3560.      * when a non-real time PCI request is pulled and a real-time one is
  3561.      * put in it's place. Workaround: Use only a single arbitration ring
  3562.      * on PCI bus. GBR and RR can still be uniquely used per
  3563.      * device. NETLIST MERGE DONE, WILL BE FIXED IN REV C.
  3564.      */
  3565.     if (pcibr_soft->bs_rev_num != BRIDGE_PART_REV_B)
  3566. rtbits |= BRIDGE_DEV_RT;
  3567.     /* NOTE- if we ever put DEV_RT or DEV_GBR on
  3568.      * the disabled list, we will have to take
  3569.      * it into account here.
  3570.      */
  3571.     s = pcibr_lock(pcibr_soft);
  3572.     devreg = pcibr_soft->bs_slot[pciio_slot].bss_device;
  3573.     if (device_prio == PCI_PRIO_HIGH) {
  3574. if ((++*counter == 1)) {
  3575.     if (rtbits)
  3576. devreg |= rtbits;
  3577.     else
  3578. rc = PRIO_FAIL;
  3579. }
  3580.     } else if (device_prio == PCI_PRIO_LOW) {
  3581. if (*counter <= 0)
  3582.     rc = PRIO_FAIL;
  3583. else if (--*counter == 0)
  3584.     if (rtbits)
  3585. devreg &= ~rtbits;
  3586.     }
  3587.     if (pcibr_soft->bs_slot[pciio_slot].bss_device != devreg) {
  3588. bridge_t               *bridge = pcibr_soft->bs_base;
  3589. bridge->b_device[pciio_slot].reg = devreg;
  3590. pcibr_soft->bs_slot[pciio_slot].bss_device = devreg;
  3591. bridge->b_wid_tflush; /* wait until Bridge PIO complete */
  3592.     }
  3593.     pcibr_unlock(pcibr_soft, s);
  3594.     return rc;
  3595. }
  3596. pciio_priority_t
  3597. pcibr_priority_set(devfs_handle_t pconn_vhdl,
  3598.    pciio_priority_t device_prio)
  3599. {
  3600.     pciio_info_t            pciio_info = pciio_info_get(pconn_vhdl);
  3601.     pciio_slot_t            pciio_slot = pciio_info_slot_get(pciio_info);
  3602.     pcibr_soft_t            pcibr_soft = (pcibr_soft_t) pciio_info_mfast_get(pciio_info);
  3603.     (void) pcibr_priority_bits_set(pcibr_soft, pciio_slot, device_prio);
  3604.     return device_prio;
  3605. }
  3606. /*
  3607.  * Interfaces to allow special (e.g. SGI) drivers to set/clear
  3608.  * Bridge-specific device flags.  Many flags are modified through
  3609.  * PCI-generic interfaces; we don't allow them to be directly
  3610.  * manipulated here.  Only flags that at this point seem pretty
  3611.  * Bridge-specific can be set through these special interfaces.
  3612.  * We may add more flags as the need arises, or remove flags and
  3613.  * create PCI-generic interfaces as the need arises.
  3614.  *
  3615.  * Returns 0 on failure, 1 on success
  3616.  */
  3617. int
  3618. pcibr_device_flags_set(devfs_handle_t pconn_vhdl,
  3619.        pcibr_device_flags_t flags)
  3620. {
  3621.     pciio_info_t            pciio_info = pciio_info_get(pconn_vhdl);
  3622.     pciio_slot_t            pciio_slot = pciio_info_slot_get(pciio_info);
  3623.     pcibr_soft_t            pcibr_soft = (pcibr_soft_t) pciio_info_mfast_get(pciio_info);
  3624.     bridgereg_t             set = 0;
  3625.     bridgereg_t             clr = 0;
  3626.     ASSERT((flags & PCIBR_DEVICE_FLAGS) == flags);
  3627.     if (flags & PCIBR_WRITE_GATHER)
  3628. set |= BRIDGE_DEV_PMU_WRGA_EN;
  3629.     if (flags & PCIBR_NOWRITE_GATHER)
  3630. clr |= BRIDGE_DEV_PMU_WRGA_EN;
  3631.     if (flags & PCIBR_WRITE_GATHER)
  3632. set |= BRIDGE_DEV_DIR_WRGA_EN;
  3633.     if (flags & PCIBR_NOWRITE_GATHER)
  3634. clr |= BRIDGE_DEV_DIR_WRGA_EN;
  3635.     if (flags & PCIBR_PREFETCH)
  3636. set |= BRIDGE_DEV_PREF;
  3637.     if (flags & PCIBR_NOPREFETCH)
  3638. clr |= BRIDGE_DEV_PREF;
  3639.     if (flags & PCIBR_PRECISE)
  3640. set |= BRIDGE_DEV_PRECISE;
  3641.     if (flags & PCIBR_NOPRECISE)
  3642. clr |= BRIDGE_DEV_PRECISE;
  3643.     if (flags & PCIBR_BARRIER)
  3644. set |= BRIDGE_DEV_BARRIER;
  3645.     if (flags & PCIBR_NOBARRIER)
  3646. clr |= BRIDGE_DEV_BARRIER;
  3647.     if (flags & PCIBR_64BIT)
  3648. set |= BRIDGE_DEV_DEV_SIZE;
  3649.     if (flags & PCIBR_NO64BIT)
  3650. clr |= BRIDGE_DEV_DEV_SIZE;
  3651.     if (set || clr) {
  3652. bridgereg_t             devreg;
  3653. unsigned long           s;
  3654. s = pcibr_lock(pcibr_soft);
  3655. devreg = pcibr_soft->bs_slot[pciio_slot].bss_device;
  3656. devreg = (devreg & ~clr) | set;
  3657. if (pcibr_soft->bs_slot[pciio_slot].bss_device != devreg) {
  3658.     bridge_t               *bridge = pcibr_soft->bs_base;
  3659.     bridge->b_device[pciio_slot].reg = devreg;
  3660.     pcibr_soft->bs_slot[pciio_slot].bss_device = devreg;
  3661.     bridge->b_wid_tflush; /* wait until Bridge PIO complete */
  3662. }
  3663. pcibr_unlock(pcibr_soft, s);
  3664. #if DEBUG && PCIBR_DEV_DEBUG
  3665. printk("pcibr Device(%d): %Rn", pciio_slot, bridge->b_device[pciio_slot].regbridge->b_device[pciio_slot].reg, device_bits);
  3666. #endif
  3667.     }
  3668.     return (1);
  3669. }
  3670. pciio_provider_t        pcibr_provider =
  3671. {
  3672.     (pciio_piomap_alloc_f *) pcibr_piomap_alloc,
  3673.     (pciio_piomap_free_f *) pcibr_piomap_free,
  3674.     (pciio_piomap_addr_f *) pcibr_piomap_addr,
  3675.     (pciio_piomap_done_f *) pcibr_piomap_done,
  3676.     (pciio_piotrans_addr_f *) pcibr_piotrans_addr,
  3677.     (pciio_piospace_alloc_f *) pcibr_piospace_alloc,
  3678.     (pciio_piospace_free_f *) pcibr_piospace_free,
  3679.     (pciio_dmamap_alloc_f *) pcibr_dmamap_alloc,
  3680.     (pciio_dmamap_free_f *) pcibr_dmamap_free,
  3681.     (pciio_dmamap_addr_f *) pcibr_dmamap_addr,
  3682.     (pciio_dmamap_list_f *) pcibr_dmamap_list,
  3683.     (pciio_dmamap_done_f *) pcibr_dmamap_done,
  3684.     (pciio_dmatrans_addr_f *) pcibr_dmatrans_addr,
  3685.     (pciio_dmatrans_list_f *) pcibr_dmatrans_list,
  3686.     (pciio_dmamap_drain_f *) pcibr_dmamap_drain,
  3687.     (pciio_dmaaddr_drain_f *) pcibr_dmaaddr_drain,
  3688.     (pciio_dmalist_drain_f *) pcibr_dmalist_drain,
  3689.     (pciio_intr_alloc_f *) pcibr_intr_alloc,
  3690.     (pciio_intr_free_f *) pcibr_intr_free,
  3691.     (pciio_intr_connect_f *) pcibr_intr_connect,
  3692.     (pciio_intr_disconnect_f *) pcibr_intr_disconnect,
  3693.     (pciio_intr_cpu_get_f *) pcibr_intr_cpu_get,
  3694.     (pciio_provider_startup_f *) pcibr_provider_startup,
  3695.     (pciio_provider_shutdown_f *) pcibr_provider_shutdown,
  3696.     (pciio_reset_f *) pcibr_reset,
  3697.     (pciio_write_gather_flush_f *) pcibr_write_gather_flush,
  3698.     (pciio_endian_set_f *) pcibr_endian_set,
  3699.     (pciio_priority_set_f *) pcibr_priority_set,
  3700.     (pciio_config_get_f *) pcibr_config_get,
  3701.     (pciio_config_set_f *) pcibr_config_set,
  3702.     (pciio_error_devenable_f *) 0,
  3703.     (pciio_error_extract_f *) 0,
  3704. #ifdef LATER
  3705.     (pciio_driver_reg_callback_f *) pcibr_driver_reg_callback,
  3706.     (pciio_driver_unreg_callback_f *) pcibr_driver_unreg_callback,
  3707. #else
  3708.     (pciio_driver_reg_callback_f *) 0,
  3709.     (pciio_driver_unreg_callback_f *) 0,
  3710. #endif
  3711.     (pciio_device_unregister_f  *) pcibr_device_unregister,
  3712.     (pciio_dma_enabled_f *) pcibr_dma_enabled,
  3713. };
  3714. int
  3715. pcibr_dma_enabled(devfs_handle_t pconn_vhdl)
  3716. {
  3717.     pciio_info_t            pciio_info = pciio_info_get(pconn_vhdl);
  3718.     pcibr_soft_t            pcibr_soft = (pcibr_soft_t) pciio_info_mfast_get(pciio_info);
  3719.     return xtalk_dma_enabled(pcibr_soft->bs_conn);
  3720. }