2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2016 Landon Fuller <landonf@FreeBSD.org>
5 * Copyright (c) 2017 The FreeBSD Foundation
8 * Portions of this software were developed by Landon Fuller
9 * under sponsorship from the FreeBSD Foundation.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer,
16 * without modification.
17 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18 * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
19 * redistribution must be conditioned upon including a substantially
20 * similar Disclaimer requirement for further binary redistribution.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
26 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
27 * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
28 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
31 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
33 * THE POSSIBILITY OF SUCH DAMAGES.
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
39 #include <sys/param.h>
43 #include <machine/bus.h>
45 #include <machine/resource.h>
47 #include <dev/bhnd/bhndvar.h>
48 #include <dev/bhnd/bhnd_erom.h>
49 #include <dev/bhnd/bhnd_eromvar.h>
51 static int bhnd_erom_iores_map(struct bhnd_erom_io *eio, bhnd_addr_t addr,
53 static uint32_t bhnd_erom_iores_read(struct bhnd_erom_io *eio,
54 bhnd_size_t offset, u_int width);
55 static void bhnd_erom_iores_fini(struct bhnd_erom_io *eio);
57 static int bhnd_erom_iobus_map(struct bhnd_erom_io *eio, bhnd_addr_t addr,
59 static uint32_t bhnd_erom_iobus_read(struct bhnd_erom_io *eio,
60 bhnd_size_t offset, u_int width);
63 * An implementation of bhnd_erom_io that manages mappings via
64 * bhnd_alloc_resource() and bhnd_release_resource().
66 struct bhnd_erom_iores {
67 struct bhnd_erom_io eio;
68 device_t owner; /**< device from which we'll allocate resources */
69 int owner_rid; /**< rid to use when allocating new mappings */
70 struct bhnd_resource *mapped; /**< current mapping, or NULL */
71 int mapped_rid; /**< resource ID of current mapping, or -1 */
75 * Fetch the device enumeration parser class from all bhnd(4)-compatible drivers
76 * registered for @p bus_devclass, probe @p eio for supporting parser classes,
77 * and return the best available supporting enumeration parser class.
79 * @param bus_devclass The bus device class to be queried for
80 * bhnd(4)-compatible drivers.
81 * @param eio An erom bus I/O instance, configured with a
82 * mapping of the first bus core.
83 * @param hint Identification hint used to identify the device.
84 * If the chipset supports standard chip
85 * identification registers within the first core,
86 * this parameter should be NULL.
87 * @param[out] cid On success, the probed chip identifier.
89 * @retval non-NULL on success, the best available EROM class.
90 * @retval NULL if no erom class returned a successful probe result for
94 bhnd_erom_probe_driver_classes(devclass_t bus_devclass,
95 struct bhnd_erom_io *eio, const struct bhnd_chipid *hint,
96 struct bhnd_chipid *cid)
100 bhnd_erom_class_t *erom_cls;
101 int error, prio, result;
106 /* Fetch all available drivers */
107 error = devclass_get_drivers(bus_devclass, &drivers, &drv_count);
109 printf("error fetching bhnd(4) drivers for %s: %d\n",
110 devclass_get_name(bus_devclass), error);
114 /* Enumerate the drivers looking for the best available EROM class */
115 for (int i = 0; i < drv_count; i++) {
116 struct bhnd_chipid pcid;
117 bhnd_erom_class_t *cls;
119 /* The default implementation of BHND_BUS_GET_EROM_CLASS()
120 * returns NULL if unimplemented; this should always be safe
121 * to call on arbitrary drivers */
122 cls = bhnd_driver_get_erom_class(drivers[i]);
126 kobj_class_compile(cls);
129 result = bhnd_erom_probe(cls, eio, hint, &pcid);
131 /* The parser did not match if an error was returned */
135 /* Check for a new highest priority match */
136 if (erom_cls == NULL || result > prio) {
143 /* Terminate immediately on BUS_PROBE_SPECIFIC */
144 if (result == BUS_PROBE_SPECIFIC)
148 free(drivers, M_TEMP);
153 * Allocate and return a new device enumeration table parser.
155 * @param cls The parser class for which an instance will be
157 * @param eio The bus I/O callbacks to use when reading the device
159 * @param cid The device's chip identifier.
161 * @retval non-NULL success
162 * @retval NULL if an error occured allocating or initializing the
166 bhnd_erom_alloc(bhnd_erom_class_t *cls, const struct bhnd_chipid *cid,
167 struct bhnd_erom_io *eio)
172 erom = (bhnd_erom_t *)kobj_create((kobj_class_t)cls, M_BHND,
175 if ((error = BHND_EROM_INIT(erom, cid, eio))) {
176 printf("error initializing %s parser at %#jx: %d\n", cls->name,
177 (uintmax_t)cid->enum_addr, error);
179 kobj_delete((kobj_t)erom, M_BHND);
187 * Perform static initialization of a device enumeration table parser.
189 * This may be used to initialize a caller-allocated erom instance state
190 * during early boot, prior to malloc availability.
192 * @param cls The parser class for which an instance will be
194 * @param erom The erom parser instance to initialize.
195 * @param esize The total available number of bytes allocated for
196 * @p erom. If this is less than is required by @p cls,
197 * ENOMEM will be returned.
198 * @param cid The device's chip identifier.
199 * @param eio The bus I/O callbacks to use when reading the device
203 * @retval ENOMEM if @p esize is smaller than required by @p cls.
204 * @retval non-zero if an error occurs initializing the EROM parser,
205 * a regular unix error code will be returned.
208 bhnd_erom_init_static(bhnd_erom_class_t *cls, bhnd_erom_t *erom, size_t esize,
209 const struct bhnd_chipid *cid, struct bhnd_erom_io *eio)
213 kcls = (kobj_class_t)cls;
215 /* Verify allocation size */
216 if (kcls->size > esize)
219 /* Perform instance initialization */
220 kobj_init_static((kobj_t)erom, kcls);
221 return (BHND_EROM_INIT(erom, cid, eio));
225 * Release any resources held by a @p erom parser previously
226 * initialized via bhnd_erom_init_static().
228 * @param erom An erom parser instance previously initialized via
229 * bhnd_erom_init_static().
232 bhnd_erom_fini_static(bhnd_erom_t *erom)
234 return (BHND_EROM_FINI(erom));
238 * Release all resources held by a @p erom parser previously
239 * allocated via bhnd_erom_alloc().
241 * @param erom An erom parser instance previously allocated via
245 bhnd_erom_free(bhnd_erom_t *erom)
247 BHND_EROM_FINI(erom);
248 kobj_delete((kobj_t)erom, M_BHND);
253 * Attempt to map @p size bytes at @p addr, replacing any existing
256 * @param eio I/O instance state.
257 * @param addr The address to be mapped.
258 * @param size The number of bytes to be mapped at @p addr.
261 * @retval non-zero if mapping @p addr otherwise fails, a regular
262 * unix error code should be returned.
265 bhnd_erom_io_map(struct bhnd_erom_io *eio, bhnd_addr_t addr, bhnd_size_t size)
267 return (eio->map(eio, addr, size));
271 * Read a 1, 2, or 4 byte data item from @p eio, at the given @p offset
272 * relative to @p eio's current mapping.
274 * @param eio erom I/O callbacks
275 * @param offset read offset.
276 * @param width item width (1, 2, or 4 bytes).
279 bhnd_erom_io_read(struct bhnd_erom_io *eio, bhnd_size_t offset, u_int width)
281 return (eio->read(eio, offset, width));
285 * Free all resources held by @p eio.
288 bhnd_erom_io_fini(struct bhnd_erom_io *eio)
290 if (eio->fini != NULL)
291 return (eio->fini(eio));
295 * Allocate, initialize, and return a new I/O instance that will perform
296 * mapping by allocating SYS_RES_MEMORY resources from @p dev using @p rid.
298 * @param dev The device to pass to bhnd_alloc_resource() and
299 * bhnd_release_resource() functions.
300 * @param rid The resource ID to be used when allocating memory resources.
302 struct bhnd_erom_io *
303 bhnd_erom_iores_new(device_t dev, int rid)
305 struct bhnd_erom_iores *iores;
307 iores = malloc(sizeof(*iores), M_BHND, M_WAITOK | M_ZERO);
308 iores->eio.map = bhnd_erom_iores_map;
309 iores->eio.read = bhnd_erom_iores_read;
310 iores->eio.fini = bhnd_erom_iores_fini;
313 iores->owner_rid = rid;
314 iores->mapped = NULL;
315 iores->mapped_rid = -1;
317 return (&iores->eio);
321 bhnd_erom_iores_map(struct bhnd_erom_io *eio, bhnd_addr_t addr,
324 struct bhnd_erom_iores *iores;
326 iores = (struct bhnd_erom_iores *)eio;
328 /* Sanity check the addr/size */
332 if (BHND_ADDR_MAX - size < addr)
333 return (EINVAL); /* would overflow */
335 /* Check for an existing mapping */
337 /* If already mapped, nothing else to do */
338 if (rman_get_start(iores->mapped->res) == addr &&
339 rman_get_size(iores->mapped->res) == size)
344 /* Otherwise, we need to drop the existing mapping */
345 bhnd_release_resource(iores->owner, SYS_RES_MEMORY,
346 iores->mapped_rid, iores->mapped);
347 iores->mapped = NULL;
348 iores->mapped_rid = -1;
351 /* Try to allocate the new mapping */
352 iores->mapped_rid = iores->owner_rid;
353 iores->mapped = bhnd_alloc_resource(iores->owner, SYS_RES_MEMORY,
354 &iores->mapped_rid, addr, addr+size-1, size,
355 RF_ACTIVE|RF_SHAREABLE);
356 if (iores->mapped == NULL) {
357 iores->mapped_rid = -1;
365 bhnd_erom_iores_read(struct bhnd_erom_io *eio, bhnd_size_t offset, u_int width)
367 struct bhnd_erom_iores *iores = (struct bhnd_erom_iores *)eio;
369 if (iores->mapped == NULL)
370 panic("read with invalid mapping");
374 return (bhnd_bus_read_1(iores->mapped, offset));
376 return (bhnd_bus_read_2(iores->mapped, offset));
378 return (bhnd_bus_read_4(iores->mapped, offset));
380 panic("invalid width %u", width);
385 bhnd_erom_iores_fini(struct bhnd_erom_io *eio)
387 struct bhnd_erom_iores *iores = (struct bhnd_erom_iores *)eio;
389 /* Release any mapping */
391 bhnd_release_resource(iores->owner, SYS_RES_MEMORY,
392 iores->mapped_rid, iores->mapped);
393 iores->mapped = NULL;
394 iores->mapped_rid = -1;
401 * Initialize an I/O instance that will perform mapping directly from the
402 * given bus space tag and handle.
404 * @param iobus The I/O instance to be initialized.
405 * @param addr The base address mapped by @p bsh.
406 * @param size The total size mapped by @p bsh.
407 * @param bst Bus space tag for @p bsh.
408 * @param bsh Bus space handle mapping the full bus enumeration space.
411 * @retval non-zero if initializing @p iobus otherwise fails, a regular
412 * unix error code will be returned.
415 bhnd_erom_iobus_init(struct bhnd_erom_iobus *iobus, bhnd_addr_t addr,
416 bhnd_size_t size, bus_space_tag_t bst, bus_space_handle_t bsh)
418 iobus->eio.map = bhnd_erom_iobus_map;
419 iobus->eio.read = bhnd_erom_iobus_read;
420 iobus->eio.fini = NULL;
426 iobus->mapped = false;
432 bhnd_erom_iobus_map(struct bhnd_erom_io *eio, bhnd_addr_t addr,
435 struct bhnd_erom_iobus *iobus = (struct bhnd_erom_iobus *)eio;
437 /* Sanity check the addr/size */
441 /* addr+size must not overflow */
442 if (BHND_ADDR_MAX - size < addr)
445 /* addr/size must fit within our bus tag's mapping */
446 if (addr < iobus->addr || size > iobus->size)
449 if (iobus->size - (addr - iobus->addr) < size)
452 /* The new addr offset and size must be representible as a bus_size_t */
453 if ((addr - iobus->addr) > BUS_SPACE_MAXSIZE)
456 if (size > BUS_SPACE_MAXSIZE)
459 iobus->offset = addr - iobus->addr;
461 iobus->mapped = true;
467 bhnd_erom_iobus_read(struct bhnd_erom_io *eio, bhnd_size_t offset, u_int width)
469 struct bhnd_erom_iobus *iobus = (struct bhnd_erom_iobus *)eio;
472 panic("no active mapping");
474 if (iobus->limit < width || iobus->limit - width < offset)
475 panic("invalid offset %#jx", offset);
479 return (bus_space_read_1(iobus->bst, iobus->bsh,
480 iobus->offset + offset));
482 return (bus_space_read_2(iobus->bst, iobus->bsh,
483 iobus->offset + offset));
485 return (bus_space_read_4(iobus->bst, iobus->bsh,
486 iobus->offset + offset));
488 panic("invalid width %u", width);