1/*-
2 * Copyright (c) 2015-2016 Landon Fuller <landon@landonf.org>
3 * Copyright (c) 2017 The FreeBSD Foundation
4 * All rights reserved.
5 *
6 * Portions of this software were developed by Landon Fuller
7 * under sponsorship from the FreeBSD Foundation.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 *    notice, this list of conditions and the following disclaimer,
14 *    without modification.
15 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
16 *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
17 *    redistribution must be conditioned upon including a substantially
18 *    similar Disclaimer requirement for further binary redistribution.
19 *
20 * NO WARRANTY
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
24 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
25 * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
26 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
29 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
31 * THE POSSIBILITY OF SUCH DAMAGES.
32 *
33 */
34
35#ifndef _BHND_BHNDBVAR_H_
36#define _BHND_BHNDBVAR_H_
37
38#include <sys/param.h>
39#include <sys/bus.h>
40#include <sys/kernel.h>
41#include <sys/lock.h>
42#include <sys/malloc.h>
43#include <sys/mutex.h>
44#include <sys/rman.h>
45
46#include <dev/bhnd/bhndvar.h>
47#include "bhndb.h"
48
49#include "bhndb_if.h"
50
51/*
52 * Definitions shared by bhndb(4) driver implementations.
53 */
54
55DECLARE_CLASS(bhndb_driver);
56
57/* forward declarations */
58struct bhndb_intr_isrc;
59struct bhndb_resources;
60struct bhndb_host_resources;
61
62int				 bhndb_attach(device_t dev,
63				     struct bhnd_chipid *cid,
64				     struct bhnd_core_info *cores, u_int ncores,
65				     struct bhnd_core_info *bridge_core,
66				     bhnd_erom_class_t *erom_class);
67
68int				 bhndb_generic_probe(device_t dev);
69int				 bhndb_generic_detach(device_t dev);
70int				 bhndb_generic_suspend(device_t dev);
71int				 bhndb_generic_resume(device_t dev);
72int				 bhndb_generic_init_full_config(device_t dev,
73				     device_t child,
74				     const struct bhndb_hw_priority *hw_prio_table);
75
76int				 bhnd_generic_br_suspend_child(device_t dev,
77				     device_t child);
78int				 bhnd_generic_br_resume_child(device_t dev,
79				     device_t child);
80
81int				 bhndb_find_hostb_core(
82				     struct bhnd_core_info *cores, u_int ncores,
83				     bhnd_devclass_t bridge_devclass,
84				     struct bhnd_core_info *core);
85
86struct bhndb_intr_isrc		*bhndb_alloc_intr_isrc(device_t owner, int rid,
87				     rman_res_t start, rman_res_t end,
88				     rman_res_t count, u_int flags);
89void				 bhndb_free_intr_isrc(
90				     struct bhndb_intr_isrc *isrc);
91
92int				 bhndb_alloc_host_resources(
93				     struct bhndb_host_resources **resources,
94				     device_t dev, device_t parent_dev,
95				     const struct bhndb_hwcfg *hwcfg);
96
97void				 bhndb_release_host_resources(
98				     struct bhndb_host_resources *resources);
99struct resource			*bhndb_host_resource_for_range(
100				     struct bhndb_host_resources *resources,
101				     int type, rman_res_t start,
102				     rman_res_t count);
103struct resource			*bhndb_host_resource_for_regwin(
104				     struct bhndb_host_resources *resources,
105				     const struct bhndb_regwin *win);
106
107size_t				 bhndb_regwin_count(
108				     const struct bhndb_regwin *table,
109				     bhndb_regwin_type_t type);
110
111const struct bhndb_regwin	*bhndb_regwin_find_type(
112				     const struct bhndb_regwin *table,
113				     bhndb_regwin_type_t type,
114				     bus_size_t min_size);
115
116const struct bhndb_regwin	*bhndb_regwin_find_core(
117				     const struct bhndb_regwin *table,
118				     bhnd_devclass_t class, int unit,
119				     bhnd_port_type port_type, u_int port,
120				     u_int region, bus_size_t offset,
121				     bus_size_t min_size);
122
123const struct bhndb_regwin	*bhndb_regwin_find_best(
124				     const struct bhndb_regwin *table,
125				     bhnd_devclass_t class, int unit,
126				     bhnd_port_type port_type, u_int port,
127				     u_int region, bus_size_t offset,
128				     bus_size_t min_size);
129
130bool				 bhndb_regwin_match_core(
131				     const struct bhndb_regwin *regw,
132				     struct bhnd_core_info *core);
133
134/**
135 * bhndb child address space. Children either operate in the bridged
136 * SoC address space, or within the address space mapped to the host
137 * device (e.g. the PCI BAR(s)).
138 */
139typedef enum {
140	BHNDB_ADDRSPACE_BRIDGED,	/**< bridged (SoC) address space */
141	BHNDB_ADDRSPACE_NATIVE		/**< host address space */
142} bhndb_addrspace;
143
144/** bhndb child instance state */
145struct bhndb_devinfo {
146	bhndb_addrspace		addrspace;	/**< child address space. */
147	struct resource_list    resources;	/**< child resources. */
148};
149
150/**
151 * Host interrupt source to which bridged interrupts may be routed.
152 */
153struct bhndb_intr_isrc {
154	device_t	 is_owner;	/**< host device (e.g. the pci device). */
155	struct resource	*is_res;	/**< irq resource */
156	int		 is_rid;	/**< irq resource ID */
157};
158
159/**
160 * Host resources allocated for a bridge hardware configuration.
161 */
162struct bhndb_host_resources {
163	device_t			 owner;			/**< device owning the allocated resources */
164	const struct bhndb_hwcfg	*cfg;			/**< bridge hardware configuration */
165	struct resource_spec		*resource_specs;	/**< resource specification table */
166	struct resource			**resources;		/**< allocated resource table */
167	bus_dma_tag_t			*dma_tags;		/**< DMA tags for all hwcfg DMA translations, or NULL
168								     if DMA is not supported */
169	size_t				 num_dma_tags;		/**< DMA tag count */
170};
171
172/**
173 * bhndb driver instance state. Must be first member of all subclass
174 * softc structures.
175 */
176struct bhndb_softc {
177	device_t			 dev;		/**< bridge device */
178	struct bhnd_chipid		 chipid;	/**< chip identification */
179	struct bhnd_core_info		 bridge_core;	/**< bridge core info */
180
181	device_t			 parent_dev;	/**< parent device */
182	device_t			 bus_dev;	/**< child bhnd(4) bus */
183
184	struct bhnd_service_registry	 services;	/**< local service registry */
185
186	struct mtx			 sc_mtx;	/**< resource lock. */
187	struct bhndb_resources		*bus_res;	/**< bus resource state */
188	STAILQ_HEAD(,bhndb_intr_handler) bus_intrs;	/**< attached child interrupt handlers */
189};
190
191#endif /* _BHND_BHNDBVAR_H_ */
192