/*      $NetBSD: bus_defs.h,v 1.6 2021/01/21 09:50:37 kre Exp $ */

/*-
* Copyright (c) 1996, 1997, 1998, 2001 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
* NASA Ames Research Center.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
*    notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
*    notice, this list of conditions and the following disclaimer in the
*    documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/

/*
* Copyright (c) 1996 Charles M. Hannum.  All rights reserved.
* Copyright (c) 1996 Christopher G. Demetriou.  All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
*    notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
*    notice, this list of conditions and the following disclaimer in the
*    documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
*    must display the following acknowledgement:
*      This product includes software developed by Christopher G. Demetriou
*      for the NetBSD Project.
* 4. The name of the author may not be used to endorse or promote products
*    derived from this software without specific prior written permission
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef _X86_BUS_H_
#define _X86_BUS_H_

#ifdef _KERNEL_OPT
#include "opt_kasan.h"
#include "opt_kmsan.h"
#endif

#include <x86/busdefs.h>

#ifdef BUS_SPACE_DEBUG
#define BUS_SPACE_ALIGNED_ADDRESS(p, t)                         \
       ((((u_long)(p)) & (sizeof(t)-1)) == 0)
#define BUS_SPACE_ALIGNED_POINTER(p, t) BUS_SPACE_ALIGNED_ADDRESS(p, t)
#else
#define BUS_SPACE_ALIGNED_POINTER(p, t) ALIGNED_POINTER(p, t)
#endif /* BUS_SPACE_DEBUG */

/*
* Bus address and size types
*/
typedef paddr_t bus_addr_t;
typedef size_t bus_size_t;

#define PRIxBUSADDR     PRIxPADDR
#define PRIxBUSSIZE     "zx"
#define PRIuBUSSIZE     "zu"

struct bus_space_tag;
typedef struct bus_space_tag *bus_space_tag_t;

struct bus_space_tag {
       int                                     bst_type;
       bus_space_tag_t                         bst_super;
       /* bst_present: bitmap indicating overrides present (1) in *this* tag,
        * bst_exists: bitmap indicating overrides present (1) in *this* tag
        * or in an ancestor's tag (follow bst_super to ancestors)
        */
       uint64_t                                bst_present;
       uint64_t                                bst_exists;
       const struct bus_space_overrides        *bst_ov;
       void                                    *bst_ctx;
};

typedef vaddr_t bus_space_handle_t;

#define PRIxBSH         PRIxVADDR

typedef struct x86_bus_dma_tag          *bus_dma_tag_t;
typedef struct x86_bus_dmamap           *bus_dmamap_t;

#define BUS_DMA_TAG_VALID(__t)    ((__t) != NULL)

/*
*      bus_dma_segment_t
*
*      Describes a single contiguous DMA transaction.  Values
*      are suitable for programming into DMA registers.
*/
typedef struct x86_bus_dma_segment {
       bus_addr_t      ds_addr;        /* DMA address */
       bus_size_t      ds_len;         /* length of transfer */
} bus_dma_segment_t;

/*
*      bus_dmamap_t
*
*      Describes a DMA mapping.  XXX exposing structure to LKM.
*/
struct x86_bus_dmamap {
       /*
        * PRIVATE MEMBERS: not for use by machine-independent code.
        */
       bus_size_t      _dm_size;       /* largest DMA transfer mappable */
       int             _dm_segcnt;     /* number of segs this map can map */
       bus_size_t      _dm_maxmaxsegsz; /* fixed largest possible segment */
       bus_size_t      _dm_boundary;   /* don't cross this */
       bus_addr_t      _dm_bounce_thresh; /* bounce threshold; see tag */
       int             _dm_flags;      /* misc. flags */

       void            *_dm_cookie;    /* cookie for bus-specific functions */

       /*
        * PUBLIC MEMBERS: these are used by machine-independent code.
        */
#if defined(KASAN) || defined(KMSAN)
       void            *dm_buf;
       bus_size_t      dm_buflen;
       int             dm_buftype;
#endif
       bus_size_t      dm_maxsegsz;    /* largest possible segment */
       bus_size_t      dm_mapsize;     /* size of the mapping */
       int             dm_nsegs;       /* # valid segments in mapping */
       bus_dma_segment_t dm_segs[1];   /* segments; variable length */
};

#endif /* _X86_BUS_H_ */