xref: /freebsd/sys/dev/bhnd/bhnd_erom.c (revision 076ad2f8)
1 /*-
2  * Copyright (c) 2016 Landon Fuller <landonf@FreeBSD.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/kobj.h>
35 
36 #include <dev/bhnd/bhndvar.h>
37 #include <dev/bhnd/bhnd_erom.h>
38 
39 /**
40  * Allocate and return a new device enumeration table parser.
41  *
42  * @param cls		The parser class for which an instance will be
43  *			allocated.
44  * @param parent	The parent device from which EROM resources should
45  *			be allocated.
46  * @param rid		The resource ID to be used when allocating EROM
47  *			resources.
48  * @param cid		The device's chip identifier.
49  *
50  * @retval non-NULL	success
51  * @retval NULL		if an error occured allocating or initializing the
52  *			EROM parser.
53  */
54 bhnd_erom_t *
55 bhnd_erom_alloc(bhnd_erom_class_t *cls, const struct bhnd_chipid *cid,
56     device_t parent, int rid)
57 {
58 	bhnd_erom_t	*erom;
59 	int		 error;
60 
61 	erom = (bhnd_erom_t *)kobj_create((kobj_class_t)cls, M_BHND,
62 	    M_WAITOK|M_ZERO);
63 
64 	if ((error = BHND_EROM_INIT(erom, cid, parent, rid))) {
65 		printf("error initializing %s parser at %#jx with "
66 		    "rid %d: %d\n", cls->name, (uintmax_t)cid->enum_addr, rid,
67 		     error);
68 
69 		kobj_delete((kobj_t)erom, M_BHND);
70 		return (NULL);
71 	}
72 
73 	return (erom);
74 }
75 
76 /**
77  * Perform static initialization of aa device enumeration table parser using
78  * the provided bus space tag and handle.
79  *
80  * This may be used to initialize a caller-allocated erom instance state
81  * during early boot, prior to malloc availability.
82  *
83  * @param cls		The parser class for which an instance will be
84  *			allocated.
85  * @param erom		The erom parser instance to initialize.
86  * @param esize		The total available number of bytes allocated for
87  *			@p erom. If this is less than is required by @p cls,
88  *			ENOMEM will be returned.
89  * @param cid		The device's chip identifier.
90  * @param bst		Bus space tag.
91  * @param bsh		Bus space handle mapping the device enumeration
92  *			space.
93  *
94  * @retval 0		success
95  * @retval ENOMEM	if @p esize is smaller than required by @p cls.
96  * @retval non-zero	if an error occurs initializing the EROM parser,
97  *			a regular unix error code will be returned.
98  */
99 int
100 bhnd_erom_init_static(bhnd_erom_class_t *cls, bhnd_erom_t *erom, size_t esize,
101     const struct bhnd_chipid *cid, bus_space_tag_t bst, bus_space_handle_t bsh)
102 {
103 	kobj_class_t	kcls;
104 
105 	kcls = (kobj_class_t)cls;
106 
107 	/* Verify allocation size */
108 	if (kcls->size > esize)
109 		return (ENOMEM);
110 
111 	/* Perform instance initialization */
112 	kobj_init_static((kobj_t)erom, kcls);
113 	return (BHND_EROM_INIT_STATIC(erom, cid, bst, bsh));
114 }
115 
116 /**
117  * Release any resources held by a @p erom parser previously
118  * initialized via bhnd_erom_init_static().
119  *
120  * @param	erom	An erom parser instance previously initialized via
121  *			bhnd_erom_init_static().
122  */
123 void
124 bhnd_erom_fini_static(bhnd_erom_t *erom)
125 {
126 	return (BHND_EROM_FINI(erom));
127 }
128 
129 /**
130  * Release all resources held by a @p erom parser previously
131  * allocated via bhnd_erom_alloc().
132  *
133  * @param	erom	An erom parser instance previously allocated via
134  *			bhnd_erom_alloc().
135  */
136 void
137 bhnd_erom_free(bhnd_erom_t *erom)
138 {
139 	BHND_EROM_FINI(erom);
140 	kobj_delete((kobj_t)erom, M_BHND);
141 }
142