xref: /freebsd/sys/dev/bhnd/siba/siba_subr.c (revision d96700a6da2afa88607fbd7405ade439424d10d9)
1 /*-
2  * Copyright (c) 2015 Landon Fuller <landon@landonf.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer,
10  *    without modification.
11  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12  *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
13  *    redistribution must be conditioned upon including a substantially
14  *    similar Disclaimer requirement for further binary redistribution.
15  *
16  * NO WARRANTY
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19  * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
20  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21  * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
22  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
27  * THE POSSIBILITY OF SUCH DAMAGES.
28  */
29 
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32 
33 #include <sys/param.h>
34 #include <sys/bus.h>
35 #include <sys/kernel.h>
36 #include <sys/limits.h>
37 #include <sys/systm.h>
38 
39 #include <machine/bus.h>
40 #include <machine/resource.h>
41 
42 #include <dev/bhnd/bhndvar.h>
43 
44 #include "sibareg.h"
45 #include "sibavar.h"
46 
47 /**
48  * Map a siba(4) OCP vendor code to its corresponding JEDEC JEP-106 vendor
49  * code.
50  *
51  * @param ocp_vendor An OCP vendor code.
52  * @return The BHND_MFGID constant corresponding to @p ocp_vendor, or
53  * BHND_MFGID_INVALID if the OCP vendor is unknown.
54  */
55 uint16_t
56 siba_get_bhnd_mfgid(uint16_t ocp_vendor)
57 {
58 	switch (ocp_vendor) {
59 	case OCP_VENDOR_BCM:
60 		return (BHND_MFGID_BCM);
61 	default:
62 		return (BHND_MFGID_INVALID);
63 	}
64 }
65 
66 /**
67  * Parse the SIBA_IDH_* fields from the per-core identification
68  * registers, returning a siba_core_id representation.
69  *
70  * @param idhigh The SIBA_R0_IDHIGH register.
71  * @param idlow The SIBA_R0_IDLOW register.
72  * @param core_id The core id (index) to include in the result.
73  * @param unit The unit number to include in the result.
74  */
75 struct siba_core_id
76 siba_parse_core_id(uint32_t idhigh, uint32_t idlow, u_int core_idx, int unit)
77 {
78 
79 	uint16_t	ocp_vendor;
80 	uint8_t		sonics_rev;
81 	uint8_t		num_addrspace;
82 	uint8_t		num_cfg;
83 
84 	ocp_vendor = SIBA_REG_GET(idhigh, IDH_VENDOR);
85 	sonics_rev = SIBA_REG_GET(idlow, IDL_SBREV);
86 	num_addrspace = SIBA_REG_GET(idlow, IDL_NRADDR) + 1 /* + enum block */;
87 
88 	/* Determine the number of sonics config register blocks */
89 	num_cfg = SIBA_CFG_NUM_2_2;
90 	if (sonics_rev >= SIBA_IDL_SBREV_2_3)
91 		num_cfg = SIBA_CFG_NUM_2_3;
92 
93 	return (struct siba_core_id) {
94 		.core_info	= {
95 			.vendor	= siba_get_bhnd_mfgid(ocp_vendor),
96 			.device	= SIBA_REG_GET(idhigh, IDH_DEVICE),
97 			.hwrev	= SIBA_IDH_CORE_REV(idhigh),
98 			.core_idx = core_idx,
99 			.unit	= unit
100 		},
101 		.sonics_vendor	= ocp_vendor,
102 		.sonics_rev	= sonics_rev,
103 		.num_addrspace	= num_addrspace,
104 		.num_cfg_blocks	= num_cfg
105 	};
106 }
107 
108 /**
109  * Allocate and return a new empty device info structure.
110  *
111  * @param bus The requesting bus device.
112  *
113  * @retval NULL if allocation failed.
114  */
115 struct siba_devinfo *
116 siba_alloc_dinfo(device_t bus)
117 {
118 	struct siba_devinfo *dinfo;
119 
120 	dinfo = malloc(sizeof(struct siba_devinfo), M_BHND, M_NOWAIT|M_ZERO);
121 	if (dinfo == NULL)
122 		return NULL;
123 
124 	for (u_int i = 0; i < nitems(dinfo->cfg); i++) {
125 		dinfo->cfg[i] = NULL;
126 		dinfo->cfg_rid[i] = -1;
127 	}
128 
129 	resource_list_init(&dinfo->resources);
130 
131 	return dinfo;
132 }
133 
134 /**
135  * Initialize a device info structure previously allocated via
136  * siba_alloc_dinfo, copying the provided core id.
137  *
138  * @param dev The requesting bus device.
139  * @param dinfo The device info instance.
140  * @param core Device core info.
141  *
142  * @retval 0 success
143  * @retval non-zero initialization failed.
144  */
145 int
146 siba_init_dinfo(device_t dev, struct siba_devinfo *dinfo,
147     const struct siba_core_id *core_id)
148 {
149 	dinfo->core_id = *core_id;
150 	return (0);
151 }
152 
153 /**
154  * Map an addrspace index to its corresponding bhnd(4) port number.
155  *
156  * @param addrspace Address space index.
157  */
158 u_int
159 siba_addrspace_port(u_int addrspace)
160 {
161 	/* The first addrspace is always mapped to device0; the remainder
162 	 * are mapped to device1 */
163 	if (addrspace == 0)
164 		return (0);
165 	else
166 		return (1);
167 }
168 
169 /**
170  * Map an addrspace index to its corresponding bhnd(4) region number.
171  *
172  * @param addrspace Address space index.
173  */
174 u_int
175 siba_addrspace_region(u_int addrspace)
176 {
177 	/* The first addrspace is always mapped to device0.0; the remainder
178 	 * are mapped to device1.0 + (n - 1) */
179 	if (addrspace == 0)
180 		return (0);
181 	else
182 		return (addrspace - 1);
183 }
184 
185 /**
186  * Return the number of bhnd(4) ports to advertise for the given
187  * @p dinfo.
188  *
189  * @param dinfo The device info to query.
190  */
191 u_int
192 siba_addrspace_port_count(struct siba_devinfo *dinfo)
193 {
194 	/* 0, 1, or 2 ports */
195 	return min(dinfo->core_id.num_addrspace, 2);
196 }
197 
198 /**
199  * Return the number of bhnd(4) regions to advertise on @p port
200  * given the provided @p num_addrspace address space count.
201  *
202  * @param num_addrspace The number of core-mapped siba(4) Sonics/OCP address
203  * spaces.
204  */
205 u_int
206 siba_addrspace_region_count(struct siba_devinfo *dinfo, u_int port)
207 {
208 	u_int num_addrspace = dinfo->core_id.num_addrspace;
209 
210 	/* The first address space, if any, is mapped to device0.0 */
211 	if (port == 0)
212 		return (min(num_addrspace, 1));
213 
214 	/* All remaining address spaces are mapped to device0.(n - 1) */
215 	if (port == 1 && num_addrspace >= 2)
216 		return (num_addrspace - 1);
217 
218 	/* No region mapping */
219 	return (0);
220 }
221 
222 /**
223  * Return true if @p port is defined on @p dinfo, false otherwise.
224  *
225  * Refer to the siba_find_addrspace() function for information on siba's
226  * mapping of bhnd(4) port and region identifiers.
227  *
228  * @param dinfo The device info to verify the port against.
229  * @param type The bhnd(4) port type.
230  * @param port The bhnd(4) port number.
231  */
232 bool
233 siba_is_port_valid(struct siba_devinfo *dinfo, bhnd_port_type type, u_int port)
234 {
235 	/* Only device ports are supported */
236 	if (type != BHND_PORT_DEVICE)
237 		return (false);
238 
239 	/* Verify the index against the port count */
240 	if (siba_addrspace_port_count(dinfo) <= port)
241 		return (false);
242 
243 	return (true);
244 }
245 
246 /**
247  * Map an bhnd(4) type/port/region triplet to its associated address space
248  * entry, if any.
249  *
250  * For compatibility with bcma(4), we map address spaces to port/region
251  * identifiers as follows:
252  *
253  * 	[port]		[addrspace]
254  * 	device0.0	0
255  * 	device1.0	1
256  * 	device1.1	2
257  * 	device1.2	3
258  *
259  * The only supported port type is BHND_PORT_DEVICE.
260  *
261  * @param dinfo The device info to search for a matching address space.
262  * @param type The bhnd(4) port type.
263  * @param port The bhnd(4) port number.
264  * @param region The bhnd(4) port region.
265  */
266 struct siba_addrspace *
267 siba_find_addrspace(struct siba_devinfo *dinfo, bhnd_port_type type, u_int port,
268     u_int region)
269 {
270 	u_int			 addridx;
271 
272 	if (!siba_is_port_valid(dinfo, type, port))
273 		return (NULL);
274 
275 	if (port == 0)
276 		addridx = region;
277 	else if (port == 1)
278 		addridx = region + 1;
279 	else
280 		return (NULL);
281 
282 	/* Out of range? */
283 	if (addridx >= dinfo->core_id.num_addrspace)
284 		return (NULL);
285 
286 	/* Found */
287 	return (&dinfo->addrspace[addridx]);
288 }
289 
290 /**
291  * Append an address space entry to @p dinfo.
292  *
293  * @param dinfo The device info entry to update.
294  * @param addridx The address space index.
295  * @param base The mapping's base address.
296  * @param size The mapping size.
297  * @param bus_reserved Number of bytes to reserve in @p size for bus use
298  * when registering the resource list entry. This is used to reserve bus
299  * access to the core's SIBA_CFG* register blocks.
300  *
301  * @retval 0 success
302  * @retval non-zero An error occurred appending the entry.
303  */
304 int
305 siba_append_dinfo_region(struct siba_devinfo *dinfo, uint8_t addridx,
306     uint32_t base, uint32_t size, uint32_t bus_reserved)
307 {
308 	struct siba_addrspace	*sa;
309 	rman_res_t		 r_size;
310 
311 	/* Verify that base + size will not overflow */
312 	if (size > 0 && UINT32_MAX - (size - 1) < base)
313 		return (ERANGE);
314 
315 	/* Verify that size - bus_reserved will not underflow */
316 	if (size < bus_reserved)
317 		return (ERANGE);
318 
319 	/* Must not be 0-length */
320 	if (size == 0)
321 		return (EINVAL);
322 
323 	/* Must not exceed addrspace array size */
324 	if (addridx >= nitems(dinfo->addrspace))
325 		return (EINVAL);
326 
327 	/* Initialize new addrspace entry */
328 	sa = &dinfo->addrspace[addridx];
329 	sa->sa_base = base;
330 	sa->sa_size = size;
331 	sa->sa_bus_reserved = bus_reserved;
332 
333 	/* Populate the resource list */
334 	r_size = size - bus_reserved;
335 	sa->sa_rid = resource_list_add_next(&dinfo->resources, SYS_RES_MEMORY,
336 	    base, base + (r_size - 1), r_size);
337 
338 	return (0);
339 }
340 
341 /**
342  * Deallocate the given device info structure and any associated resources.
343  *
344  * @param dev The requesting bus device.
345  * @param dinfo Device info to be deallocated.
346  */
347 void
348 siba_free_dinfo(device_t dev, struct siba_devinfo *dinfo)
349 {
350 	resource_list_free(&dinfo->resources);
351 
352 	/* Free all mapped configuration blocks */
353 	for (u_int i = 0; i < nitems(dinfo->cfg); i++) {
354 		if (dinfo->cfg[i] == NULL)
355 			continue;
356 
357 		bhnd_release_resource(dev, SYS_RES_MEMORY, dinfo->cfg_rid[i],
358 		    dinfo->cfg[i]);
359 
360 		dinfo->cfg[i] = NULL;
361 		dinfo->cfg_rid[i] = -1;
362 	}
363 
364 	free(dinfo, M_BHND);
365 }
366 
367 /**
368  * Return the core-enumeration-relative offset for the @p addrspace
369  * SIBA_R0_ADMATCH* register.
370  *
371  * @param addrspace The address space index.
372  *
373  * @retval non-zero success
374  * @retval 0 the given @p addrspace index is not supported.
375  */
376 u_int
377 siba_admatch_offset(uint8_t addrspace)
378 {
379 	switch (addrspace) {
380 	case 0:
381 		return SB0_REG_ABS(SIBA_CFG0_ADMATCH0);
382 	case 1:
383 		return SB0_REG_ABS(SIBA_CFG0_ADMATCH1);
384 	case 2:
385 		return SB0_REG_ABS(SIBA_CFG0_ADMATCH2);
386 	case 3:
387 		return SB0_REG_ABS(SIBA_CFG0_ADMATCH3);
388 	default:
389 		return (0);
390 	}
391 }
392 
393 /**
394  * Parse a SIBA_R0_ADMATCH* register.
395  *
396  * @param addrspace The address space index.
397  * @param am The address match register value to be parsed.
398  * @param[out] addr The parsed address.
399  * @param[out] size The parsed size.
400  *
401  * @retval 0 success
402  * @retval non-zero a parse error occurred.
403  */
404 int
405 siba_parse_admatch(uint32_t am, uint32_t *addr, uint32_t *size)
406 {
407 	u_int		am_type;
408 
409 	/* Negative encoding is not supported. This is not used on any
410 	 * currently known devices*/
411 	if (am & SIBA_AM_ADNEG)
412 		return (EINVAL);
413 
414 	/* Extract the base address and size */
415 	am_type = SIBA_REG_GET(am, AM_TYPE);
416 	switch (am_type) {
417 	case 0:
418 		*addr = am & SIBA_AM_BASE0_MASK;
419 		*size = 1 << (SIBA_REG_GET(am, AM_ADINT0) + 1);
420 		break;
421 	case 1:
422 		*addr = am & SIBA_AM_BASE1_MASK;
423 		*size = 1 << (SIBA_REG_GET(am, AM_ADINT1) + 1);
424 		break;
425 	case 2:
426 		*addr = am & SIBA_AM_BASE2_MASK;
427 		*size = 1 << (SIBA_REG_GET(am, AM_ADINT2) + 1);
428 		break;
429 	default:
430 		return (EINVAL);
431 	}
432 
433 	return (0);
434 }
435