xref: /freebsd/sys/dev/bhnd/bhndb/bhndbvar.h (revision 3c4ba5f55438f7afd4f4b0b56f88f2bb505fd6a6)
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  * $FreeBSD$
34  */
35 
36 #ifndef _BHND_BHNDBVAR_H_
37 #define _BHND_BHNDBVAR_H_
38 
39 #include <sys/param.h>
40 #include <sys/bus.h>
41 #include <sys/kernel.h>
42 #include <sys/lock.h>
43 #include <sys/malloc.h>
44 #include <sys/mutex.h>
45 #include <sys/rman.h>
46 
47 #include <dev/bhnd/bhndvar.h>
48 #include "bhndb.h"
49 
50 #include "bhndb_if.h"
51 
52 /*
53  * Definitions shared by bhndb(4) driver implementations.
54  */
55 
56 DECLARE_CLASS(bhndb_driver);
57 
58 /* forward declarations */
59 struct bhndb_intr_isrc;
60 struct bhndb_resources;
61 struct bhndb_host_resources;
62 
63 int				 bhndb_attach(device_t dev,
64 				     struct bhnd_chipid *cid,
65 				     struct bhnd_core_info *cores, u_int ncores,
66 				     struct bhnd_core_info *bridge_core,
67 				     bhnd_erom_class_t *erom_class);
68 
69 int				 bhndb_generic_probe(device_t dev);
70 int				 bhndb_generic_detach(device_t dev);
71 int				 bhndb_generic_suspend(device_t dev);
72 int				 bhndb_generic_resume(device_t dev);
73 int				 bhndb_generic_init_full_config(device_t dev,
74 				     device_t child,
75 				     const struct bhndb_hw_priority *hw_prio_table);
76 
77 int				 bhnd_generic_br_suspend_child(device_t dev,
78 				     device_t child);
79 int				 bhnd_generic_br_resume_child(device_t dev,
80 				     device_t child);
81 
82 int				 bhndb_find_hostb_core(
83 				     struct bhnd_core_info *cores, u_int ncores,
84 				     bhnd_devclass_t bridge_devclass,
85 				     struct bhnd_core_info *core);
86 
87 struct bhndb_intr_isrc		*bhndb_alloc_intr_isrc(device_t owner, int rid,
88 				     rman_res_t start, rman_res_t end,
89 				     rman_res_t count, u_int flags);
90 void				 bhndb_free_intr_isrc(
91 				     struct bhndb_intr_isrc *isrc);
92 
93 int				 bhndb_alloc_host_resources(
94 				     struct bhndb_host_resources **resources,
95 				     device_t dev, device_t parent_dev,
96 				     const struct bhndb_hwcfg *hwcfg);
97 
98 void				 bhndb_release_host_resources(
99 				     struct bhndb_host_resources *resources);
100 struct resource			*bhndb_host_resource_for_range(
101 				     struct bhndb_host_resources *resources,
102 				     int type, rman_res_t start,
103 				     rman_res_t count);
104 struct resource			*bhndb_host_resource_for_regwin(
105 				     struct bhndb_host_resources *resources,
106 				     const struct bhndb_regwin *win);
107 
108 size_t				 bhndb_regwin_count(
109 				     const struct bhndb_regwin *table,
110 				     bhndb_regwin_type_t type);
111 
112 const struct bhndb_regwin	*bhndb_regwin_find_type(
113 				     const struct bhndb_regwin *table,
114 				     bhndb_regwin_type_t type,
115 				     bus_size_t min_size);
116 
117 const struct bhndb_regwin	*bhndb_regwin_find_core(
118 				     const struct bhndb_regwin *table,
119 				     bhnd_devclass_t class, int unit,
120 				     bhnd_port_type port_type, u_int port,
121 				     u_int region, bus_size_t offset,
122 				     bus_size_t min_size);
123 
124 const struct bhndb_regwin	*bhndb_regwin_find_best(
125 				     const struct bhndb_regwin *table,
126 				     bhnd_devclass_t class, int unit,
127 				     bhnd_port_type port_type, u_int port,
128 				     u_int region, bus_size_t offset,
129 				     bus_size_t min_size);
130 
131 bool				 bhndb_regwin_match_core(
132 				     const struct bhndb_regwin *regw,
133 				     struct bhnd_core_info *core);
134 
135 /**
136  * bhndb child address space. Children either operate in the bridged
137  * SoC address space, or within the address space mapped to the host
138  * device (e.g. the PCI BAR(s)).
139  */
140 typedef enum {
141 	BHNDB_ADDRSPACE_BRIDGED,	/**< bridged (SoC) address space */
142 	BHNDB_ADDRSPACE_NATIVE		/**< host address space */
143 } bhndb_addrspace;
144 
145 /** bhndb child instance state */
146 struct bhndb_devinfo {
147 	bhndb_addrspace		addrspace;	/**< child address space. */
148 	struct resource_list    resources;	/**< child resources. */
149 };
150 
151 /**
152  * Host interrupt source to which bridged interrupts may be routed.
153  */
154 struct bhndb_intr_isrc {
155 	device_t	 is_owner;	/**< host device (e.g. the pci device). */
156 	struct resource	*is_res;	/**< irq resource */
157 	int		 is_rid;	/**< irq resource ID */
158 };
159 
160 /**
161  * Host resources allocated for a bridge hardware configuration.
162  */
163 struct bhndb_host_resources {
164 	device_t			 owner;			/**< device owning the allocated resources */
165 	const struct bhndb_hwcfg	*cfg;			/**< bridge hardware configuration */
166 	struct resource_spec		*resource_specs;	/**< resource specification table */
167 	struct resource			**resources;		/**< allocated resource table */
168 	bus_dma_tag_t			*dma_tags;		/**< DMA tags for all hwcfg DMA translations, or NULL
169 								     if DMA is not supported */
170 	size_t				 num_dma_tags;		/**< DMA tag count */
171 };
172 
173 /**
174  * bhndb driver instance state. Must be first member of all subclass
175  * softc structures.
176  */
177 struct bhndb_softc {
178 	device_t			 dev;		/**< bridge device */
179 	struct bhnd_chipid		 chipid;	/**< chip identification */
180 	struct bhnd_core_info		 bridge_core;	/**< bridge core info */
181 
182 	device_t			 parent_dev;	/**< parent device */
183 	device_t			 bus_dev;	/**< child bhnd(4) bus */
184 
185 	struct bhnd_service_registry	 services;	/**< local service registry */
186 
187 	struct mtx			 sc_mtx;	/**< resource lock. */
188 	struct bhndb_resources		*bus_res;	/**< bus resource state */
189 	STAILQ_HEAD(,bhndb_intr_handler) bus_intrs;	/**< attached child interrupt handlers */
190 };
191 
192 #endif /* _BHND_BHNDBVAR_H_ */
193