1.\" Copyright (c) 2015-2016 Landon Fuller <landonf@FreeBSD.org> 2.\" Copyright (c) 2017 The FreeBSD Foundation 3.\" All rights reserved. 4.\" 5.\" Portions of this documentation were written by Landon Fuller 6.\" under sponsorship from the FreeBSD Foundation. 7.\" 8.\" Redistribution and use in source and binary forms, with or without 9.\" modification, are permitted provided that the following conditions 10.\" are met: 11.\" 1. Redistributions of source code must retain the above copyright 12.\" notice, this list of conditions and the following disclaimer. 13.\" 2. Redistributions in binary form must reproduce the above copyright 14.\" notice, this list of conditions and the following disclaimer in the 15.\" documentation and/or other materials provided with the distribution. 16.\" 17.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27.\" SUCH DAMAGE. 28.\" 29.\" $FreeBSD$ 30.\" 31.Dd November 9, 2017 32.Dt BHND_EROM 9 33.Os 34.Sh NAME 35.Nm bhnd_erom , 36.Nm bhnd_erom_alloc , 37.Nm bhnd_erom_dump , 38.Nm bhnd_erom_fini_static , 39.Nm bhnd_erom_free , 40.Nm bhnd_erom_free_core_table , 41.Nm bhnd_erom_get_core_table , 42.Nm bhnd_erom_init_static , 43.Nm bhnd_erom_io , 44.Nm bhnd_erom_io_fini , 45.Nm bhnd_erom_io_map , 46.Nm bhnd_erom_io_read , 47.Nm bhnd_erom_iobus_init , 48.Nm bhnd_erom_iores_new , 49.Nm bhnd_erom_lookup_core , 50.Nm bhnd_erom_lookup_core_addr , 51.Nm bhnd_erom_probe , 52.Nm bhnd_erom_probe_driver_classes 53.Nd BHND device enumeration table parsing 54.Sh SYNOPSIS 55.In dev/bhnd/bhnd.h 56.In dev/bhnd/bhnd_erom.h 57.\" 58.Vt typedef struct bhnd_erom bhnd_erom_t ; 59.Vt typedef struct kobj_class bhnd_erom_class_t ; 60.Vt typedef struct bhnd_erom_static bhnd_erom_static_t ; 61.Ft int 62.Fo bhnd_erom_probe 63.Fa "bhnd_erom_class_t *cls" 64.Fa "struct bhnd_erom_io *eio" 65.Fa "const struct bhnd_chipid *hint" 66.Fa "struct bhnd_chipid *cid" 67.Fc 68.Ft bhnd_erom_class_t * 69.Fo bhnd_erom_probe_driver_classes 70.Fa "devclass_t bus_devclass" 71.Fa "struct bhnd_erom_io *eio" 72.Fa "const struct bhnd_chipid *hint" 73.Fa "struct bhnd_chipid *cid" 74.Fc 75.Ft bhnd_erom_t * 76.Fo bhnd_erom_alloc 77.Fa "bhnd_erom_class_t *cls" 78.Fa "const struct bhnd_chipid *cid" 79.Fa "struct bhnd_erom_io *eio" 80.Fc 81.Ft void 82.Fo bhnd_erom_free 83.Fa "bhnd_erom_t *erom" 84.Fc 85.Ft int 86.Fo bhnd_erom_init_static 87.Fa "bhnd_erom_class_t *cls" 88.Fa "bhnd_erom_t *erom" 89.Fa "size_t esize" 90.Fa "const struct bhnd_chipid *cid" 91.Fa "struct bhnd_erom_io *eio" 92.Fc 93.Ft void 94.Fo bhnd_erom_fini_static 95.Fa "bhnd_erom_t *erom" 96.Fc 97.Ft int 98.Fo bhnd_erom_dump 99.Fa "bhnd_erom_t *erom" 100.Fc 101.Ft int 102.Fo bhnd_erom_get_core_table 103.Fa "bhnd_erom_t *erom" 104.Fa "struct bhnd_core_info **cores" 105.Fa "u_int *num_cores" 106.Fc 107.Ft void 108.Fo bhnd_erom_free_core_table 109.Fa "bhnd_erom_t *erom" 110.Fa "struct bhnd_core_info *cores" 111.Fc 112.Ft int 113.Fo bhnd_erom_lookup_core 114.Fa "bhnd_erom_t *erom" 115.Fa "const struct bhnd_core_match *desc" 116.Fa "struct bhnd_core_info *core" 117.Fc 118.Ft int 119.Fo bhnd_erom_lookup_core_addr 120.Fa "bhnd_erom_t *erom" 121.Fa "const struct bhnd_core_match *desc" 122.Fa "bhnd_port_type type" 123.Fa "u_int port" 124.Fa "u_int region" 125.Fa "struct bhnd_core_info *core" 126.Fa "bhnd_addr_t *addr" 127.Fa "bhnd_size_t *size" 128.Fc 129.\" 130.Ss Bus Space I/O 131.Ft struct bhnd_erom_io * 132.Fo bhnd_erom_iores_new 133.Fa "device_t dev" 134.Fa "int rid" 135.Fc 136.Ft int 137.Fo bhnd_erom_iobus_init 138.Fa "struct bhnd_erom_iobus *iobus" 139.Fa "bhnd_addr_t addr" 140.Fa "bhnd_size_t size" 141.Fa "bus_space_tag_t bst" 142.Fa "bus_space_handle_t bsh" 143.Fc 144.Ft void 145.Fo bhnd_erom_io_fini 146.Fa "struct bhnd_erom_io *eio" 147.Fc 148.Ft int 149.Fo bhnd_erom_io_map 150.Fa "struct bhnd_erom_io *eio" 151.Fa "bhnd_addr_t addr" 152.Fa "bhnd_size_t size" 153.Fc 154.Ft uint32_t 155.Fo bhnd_erom_io_read 156.Fa "struct bhnd_erom_io *eio" 157.Fa "bhnd_size_t offset" 158.Fa "u_int width" 159.Fc 160.In dev/bhnd/bhnd_eromvar.h 161.Bd -literal 162struct bhnd_erom_io { 163 bhnd_erom_io_map_t *map; 164 bhnd_erom_io_read_t *read; 165 bhnd_erom_io_fini_t *fini; 166}; 167.Ed 168.Ft typedef int 169.Fo \*(lpbhnd_erom_io_map_t\*(rp 170.Fa "struct bhnd_erom_io *eio" 171.Fa "bhnd_addr_t addr" 172.Fa "bhnd_size_t size" 173.Fc 174.Ft typedef uint32_t 175.Fo \*(lpbhnd_erom_io_read_t\*(rp 176.Fa "struct bhnd_erom_io *eio" 177.Fa "bhnd_size_t offset" 178.Fa "u_int width" 179.Fc 180.Ft typedef void 181.Fo "\*(lpbhnd_erom_io_fini_t\*(rp 182.Fa "struct bhnd_erom_io *eio" 183.Fc 184.\" 185.Sh DESCRIPTION 186The 187.Nm 188framework provides a common parser interface to the BHND device enumeration 189table formats supported by 190.Xr bhnd 4 191bus drivers. 192.Pp 193The 194.Fn bhnd_erom_probe 195function is used to identify a 196.Xr bhnd 4 197bus device and determine whether the erom class 198.Fa cls 199is capable of parsing its device enumeration table. 200If successful, the probed chip identification is written to the location 201pointed to by 202.Fa cid . 203.Pp 204A pointer to a bus I/O instance mapping the device registers of the first 205hardware core must be provided using the 206.Fa eio 207argument. 208The registers can be mapped using 209.Xr bhnd_erom_io_map 9 . 210.Pp 211On devices that do not provide standard 212.Xr bhnd_chipc 4 213chip identification registers via the first hardware core, a pointer to chip 214information for the device must be specified using the 215.Fa hint 216argument. 217Otherwise, the 218.Fa hint 219argument should be 220.Dv NULL . 221.Pp 222The 223.Fn bhnd_erom_probe_driver_classes 224function is a convenience wrapper for 225.Fn bhnd_erom_probe . 226This function will iterate over all drivers instances in the device class 227.Fa bus_devclass , 228using 229.Xr bhnd_driver_get_erom_class 9 230to fetch each driver's erom class and probe the hardware core mapped by 231.Fa eio . 232A pointer to the erom class with the highest probe priority is returned on 233success. 234If there are no successful probe results from the erom classes, 235.Dv NULL 236is returned. 237.Pp 238The 239.Fn bhnd_erom_alloc 240function allocates and returns a new parser instance of the device enumeration 241class 242.Fa cls 243for the chip identified by 244.Fa cid , 245using the bus I/O instance 246.Fa eio 247to map and read the device table. 248On success, the returned 249.Vt bhnd_erom_t 250assumes ownership of 251.Fa eio . 252.Pp 253The 254.Fn bhnd_erom_free 255function releases all resources held by an erom parser successfully allocated 256using 257.Fn bhnd_erom_alloc . 258.Pp 259Clients can manage the allocation of memory themselves with 260.Fn bhnd_erom_init_static . 261This is useful in cases like performing device enumeration before 262.Xr malloc 9 263initialization. 264.Fn bhnd_erom_init_static 265is called with 266.Fa erom 267set to a pointer to the memory for the instance, and the total available bytes 268in 269.Fa esize . 270.Pp 271The 272.Vt bhnd_erom_static 273structure is large enough to statically allocate any supported parser class 274instance state. 275Pointers to a 276.Vt bhnd_erom_static 277structure can be cast to 278.Vt bhnd_erom_t . 279.Pp 280The 281.Fn bhnd_erom_fini_static 282function releases all resources held by an erom parser successfully 283initialized using 284.Fn bhnd_erom_init_static . 285.Pp 286The 287.Fn bhnd_erom_dump 288function enumerates and prints all device table entries in 289.Fa erom . 290.Pp 291The 292.Fn bhnd_erom_get_core_table 293function enumerates all device table entries in 294.Fa erom , 295returning a table of core information structures in 296.Fa cores 297and the count in 298.Fa num_cores . 299The memory allocated for the table must be freed using 300.Fn bhnd_erom_free_core_table . 301.Pp 302The 303.Fn bhnd_erom_free_core_table 304function frees any memory allocated in a previous call to 305.Fn bhnd_erom_get_core_table . 306.Pp 307The 308.Fn bhnd_erom_lookup_core 309function locates the first device table entry in 310.Fa erom 311that matches core match descriptor 312.Fa desc , 313writing the core information of the matching entry to 314.Fa core . 315.Pp 316The 317.Fn bhnd_erom_lookup_core_addr 318function locates the first device table entry in 319.Fa erom 320that matches core match descriptor 321.Fa desc , 322fetching the base address and size of the memory region 323.Fa region 324mapped to the port 325.Fa port 326of type 327.Fa type . 328On success, the core information of the matching entry is written to 329.Fa core , 330the base address of the port region is written to 331.Fa addr , 332and the total size of the port region is written to 333.Fa size . 334If the core information is not desired, set 335.Fa core 336to 337.Dv NULL . 338.Ss Bus Space I/O 339The 340.Vt bhnd_erom_io 341structure provides a set of I/O callbacks used by 342.Nm 343to map and read the device enumeration table. 344Clients may either use the existing 345.Fn bhnd_erom_iores_new 346or 347.Fn bhnd_erom_iobus_init 348functions to allocate a bus I/O instance, or implement the 349.Vt bhnd_erom_io 350callbacks directly. 351.Pp 352The 353.Vt bhnd_erom_io 354structure contains these required fields: 355.Bl -tag -width "read" -offset indent 356.It Fa map 357A function implementing 358.Fn bhnd_erom_io_map . 359.It Fa read 360A function implementing 361.Fn bhnd_erom_io_read . 362.It Fa fini 363A function implementing 364.Fn bhnd_erom_io_fini . 365.El 366.Pp 367The 368.Fn bhnd_erom_iores_new 369function allocates and returns a new bus I/O instance that will perform mapping 370by using 371.Xr bhnd_alloc_resource 9 372to allocate 373.Dv SYS_RES_MEMORY 374bus resources on demand from the device 375.Fa dev 376using a resource ID of 377.Fa rid . 378.Pp 379The 380.Fn bhnd_erom_iobus_init 381function initializes a caller-allocated bus I/O instance 382.Fa iobus 383that will perform bus I/O using the bus space tag 384.Fa bst 385and handle 386.Fa bsh . 387The base address and total size mapped by 388.Fa bsh 389should be specified using the 390.Fa addr 391and 392.Fa size 393arguments. 394.Pp 395The 396.Fn bhnd_erom_io_fini 397function frees all resources held by the bus I/O instance 398.Fa eio . 399.Pp 400The 401.Fn bhnd_erom_io_map 402function is used to request that the bus I/O instance 403.Fa eio 404map 405.Xr bhnd 4 406bus space at bus address 407.Fa addr 408with a mapping of size 409.Fa size . 410.Pp 411The 412.Fn bhnd_erom_io_read 413function is used to read a data item of 414.Fa width 415bytes from the bus I/O instance 416.Fa eio 417at 418.Fa offset , 419relative to the bus address previously mapped using 420.Fn bhnd_erom_io_map . 421.Pp 422The 423.Fa width 424must be one of 1, 2, or 4 bytes. 425.Sh RETURN VALUES 426The 427.Fn bhnd_erom_probe 428function returns a standard 429.Xr DEVICE_PROBE 9 430result. 431.Pp 432A return value equal to or less than zero indicates success. 433Values greater than zero indicates an error, and will be an appropriate error 434code. 435For values less than or equal to zero, the erom class returning the highest 436value should be used to parse the erom table. 437.Er ENXIO 438is returned if the device is not supported by the parser. 439.Pp 440The 441.Fn bhnd_erom_probe_driver_classes 442function returns a pointer to the probed 443.Vt bhnd_erom_class_t 444instance on success, a null pointer otherwise. 445.Pp 446The 447.Fn bhnd_erom_alloc 448function returns a pointer to 449.Vt bhnd_erom_t 450on success, or 451.Dv NULL 452if an error occurred allocating or initializing the EROM parser. 453.Pp 454The 455.Fn bhnd_erom_init_static 456function returns 0 on success, 457.Er ENOMEM 458if the allocation size is smaller than required by the erom class, or 459an appropriate error code if initialization otherwise fails. 460.Pp 461The 462.Fn bhnd_erom_lookup_core 463function returns 0 on success, 464.Er ENOENT 465if no matching core is found, or an appropriate error code if parsing the device 466table otherwise fails. 467.Pp 468The 469.Fn bhnd_erom_dump , 470.Fn bhnd_erom_get_core_table , 471.Fn bhnd_erom_iobus_init , 472.Fn bhnd_erom_io_map , 473functions return 0 on success, otherwise an appropriate error code is returned. 474.Sh SEE ALSO 475.Xr bhnd 4 , 476.Xr bhnd 9 , 477.Xr bhnd_alloc_resource 9 , 478.Xr bhnd_driver_get_erom_class 9 , 479.Xr bus_space 9 480.Sh AUTHORS 481.An -nosplit 482The 483.Nm 484framework and this manual page were written by 485.An Landon Fuller Aq Mt landonf@FreeBSD.org . 486