ofw_bus_subr.c revision 277098
1189251Ssam/*-
2189251Ssam * Copyright (c) 2001 - 2003 by Thomas Moestl <tmm@FreeBSD.org>.
3189251Ssam * Copyright (c) 2005 Marius Strobl <marius@FreeBSD.org>
4189251Ssam * All rights reserved.
5252726Srpaulo *
6252726Srpaulo * Redistribution and use in source and binary forms, with or without
7189251Ssam * modification, are permitted provided that the following conditions
8189251Ssam * are met:
9189251Ssam * 1. Redistributions of source code must retain the above copyright
10189251Ssam *    notice, this list of conditions, and the following disclaimer,
11189251Ssam *    without modification, immediately at the beginning of the file.
12189251Ssam * 2. Redistributions in binary form must reproduce the above copyright
13189251Ssam *    notice, this list of conditions and the following disclaimer in
14189251Ssam *    the documentation and/or other materials provided with the
15189251Ssam *    distribution.
16189251Ssam *
17189251Ssam * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18189251Ssam * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19189251Ssam * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20189251Ssam * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
21189251Ssam * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22189251Ssam * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23189251Ssam * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24189251Ssam * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25189251Ssam * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26189251Ssam * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27189251Ssam * SUCH DAMAGE.
28189251Ssam */
29189251Ssam
30189251Ssam#include <sys/cdefs.h>
31189251Ssam__FBSDID("$FreeBSD: head/sys/dev/ofw/ofw_bus_subr.c 277098 2015-01-13 00:00:09Z zbb $");
32189251Ssam
33189251Ssam#include "opt_platform.h"
34189251Ssam#include <sys/param.h>
35189251Ssam#include <sys/systm.h>
36189251Ssam#include <sys/bus.h>
37189251Ssam#include <sys/errno.h>
38189251Ssam#include <sys/libkern.h>
39189251Ssam
40189251Ssam#include <machine/resource.h>
41189251Ssam
42189251Ssam#include <dev/ofw/ofw_bus.h>
43189251Ssam#include <dev/ofw/ofw_bus_subr.h>
44189251Ssam#include <dev/ofw/openfirm.h>
45189251Ssam
46189251Ssam#include "ofw_bus_if.h"
47189251Ssam
48189251Ssamint
49189251Ssamofw_bus_gen_setup_devinfo(struct ofw_bus_devinfo *obd, phandle_t node)
50189251Ssam{
51189251Ssam
52189251Ssam	if (obd == NULL)
53189251Ssam		return (ENOMEM);
54189251Ssam	/* The 'name' property is considered mandatory. */
55189251Ssam	if ((OF_getprop_alloc(node, "name", 1, (void **)&obd->obd_name)) == -1)
56189251Ssam		return (EINVAL);
57189251Ssam	OF_getprop_alloc(node, "compatible", 1, (void **)&obd->obd_compat);
58189251Ssam	OF_getprop_alloc(node, "device_type", 1, (void **)&obd->obd_type);
59189251Ssam	OF_getprop_alloc(node, "model", 1, (void **)&obd->obd_model);
60189251Ssam	OF_getprop_alloc(node, "status", 1, (void **)&obd->obd_status);
61189251Ssam	obd->obd_node = node;
62189251Ssam	return (0);
63189251Ssam}
64189251Ssam
65189251Ssamvoid
66189251Ssamofw_bus_gen_destroy_devinfo(struct ofw_bus_devinfo *obd)
67189251Ssam{
68189251Ssam
69189251Ssam	if (obd == NULL)
70189251Ssam		return;
71189251Ssam	if (obd->obd_compat != NULL)
72189251Ssam		free(obd->obd_compat, M_OFWPROP);
73	if (obd->obd_model != NULL)
74		free(obd->obd_model, M_OFWPROP);
75	if (obd->obd_name != NULL)
76		free(obd->obd_name, M_OFWPROP);
77	if (obd->obd_type != NULL)
78		free(obd->obd_type, M_OFWPROP);
79	if (obd->obd_status != NULL)
80		free(obd->obd_status, M_OFWPROP);
81}
82
83int
84ofw_bus_gen_child_pnpinfo_str(device_t cbdev, device_t child, char *buf,
85    size_t buflen)
86{
87
88	if (ofw_bus_get_name(child) != NULL) {
89		strlcat(buf, "name=", buflen);
90		strlcat(buf, ofw_bus_get_name(child), buflen);
91	}
92
93	if (ofw_bus_get_compat(child) != NULL) {
94		strlcat(buf, " compat=", buflen);
95		strlcat(buf, ofw_bus_get_compat(child), buflen);
96	}
97	return (0);
98};
99
100const char *
101ofw_bus_gen_get_compat(device_t bus, device_t dev)
102{
103	const struct ofw_bus_devinfo *obd;
104
105	obd = OFW_BUS_GET_DEVINFO(bus, dev);
106	if (obd == NULL)
107		return (NULL);
108	return (obd->obd_compat);
109}
110
111const char *
112ofw_bus_gen_get_model(device_t bus, device_t dev)
113{
114	const struct ofw_bus_devinfo *obd;
115
116	obd = OFW_BUS_GET_DEVINFO(bus, dev);
117	if (obd == NULL)
118		return (NULL);
119	return (obd->obd_model);
120}
121
122const char *
123ofw_bus_gen_get_name(device_t bus, device_t dev)
124{
125	const struct ofw_bus_devinfo *obd;
126
127	obd = OFW_BUS_GET_DEVINFO(bus, dev);
128	if (obd == NULL)
129		return (NULL);
130	return (obd->obd_name);
131}
132
133phandle_t
134ofw_bus_gen_get_node(device_t bus, device_t dev)
135{
136	const struct ofw_bus_devinfo *obd;
137
138	obd = OFW_BUS_GET_DEVINFO(bus, dev);
139	if (obd == NULL)
140		return (0);
141	return (obd->obd_node);
142}
143
144const char *
145ofw_bus_gen_get_type(device_t bus, device_t dev)
146{
147	const struct ofw_bus_devinfo *obd;
148
149	obd = OFW_BUS_GET_DEVINFO(bus, dev);
150	if (obd == NULL)
151		return (NULL);
152	return (obd->obd_type);
153}
154
155const char *
156ofw_bus_get_status(device_t dev)
157{
158	const struct ofw_bus_devinfo *obd;
159
160	obd = OFW_BUS_GET_DEVINFO(device_get_parent(dev), dev);
161	if (obd == NULL)
162		return (NULL);
163
164	return (obd->obd_status);
165}
166
167int
168ofw_bus_status_okay(device_t dev)
169{
170	const char *status;
171
172	status = ofw_bus_get_status(dev);
173	if (status == NULL || strcmp(status, "okay") == 0)
174		return (1);
175
176	return (0);
177}
178
179int
180ofw_bus_is_compatible(device_t dev, const char *onecompat)
181{
182	phandle_t node;
183	const char *compat;
184	int len, onelen, l;
185
186	if ((compat = ofw_bus_get_compat(dev)) == NULL)
187		return (0);
188
189	if ((node = ofw_bus_get_node(dev)) == -1)
190		return (0);
191
192	/* Get total 'compatible' prop len */
193	if ((len = OF_getproplen(node, "compatible")) <= 0)
194		return (0);
195
196	onelen = strlen(onecompat);
197
198	while (len > 0) {
199		if (strlen(compat) == onelen &&
200		    strncasecmp(compat, onecompat, onelen) == 0)
201			/* Found it. */
202			return (1);
203
204		/* Slide to the next sub-string. */
205		l = strlen(compat) + 1;
206		compat += l;
207		len -= l;
208	}
209	return (0);
210}
211
212int
213ofw_bus_is_compatible_strict(device_t dev, const char *compatible)
214{
215	const char *compat;
216	size_t len;
217
218	if ((compat = ofw_bus_get_compat(dev)) == NULL)
219		return (0);
220
221	len = strlen(compatible);
222	if (strlen(compat) == len &&
223	    strncasecmp(compat, compatible, len) == 0)
224		return (1);
225
226	return (0);
227}
228
229const struct ofw_compat_data *
230ofw_bus_search_compatible(device_t dev, const struct ofw_compat_data *compat)
231{
232
233	if (compat == NULL)
234		return NULL;
235
236	for (; compat->ocd_str != NULL; ++compat) {
237		if (ofw_bus_is_compatible(dev, compat->ocd_str))
238			break;
239	}
240
241	return (compat);
242}
243
244int
245ofw_bus_has_prop(device_t dev, const char *propname)
246{
247	phandle_t node;
248
249	if ((node = ofw_bus_get_node(dev)) == -1)
250		return (0);
251
252	return (OF_hasprop(node, propname));
253}
254
255void
256ofw_bus_setup_iinfo(phandle_t node, struct ofw_bus_iinfo *ii, int intrsz)
257{
258	pcell_t addrc;
259	int msksz;
260
261	if (OF_getencprop(node, "#address-cells", &addrc, sizeof(addrc)) == -1)
262		addrc = 2;
263	ii->opi_addrc = addrc * sizeof(pcell_t);
264
265	ii->opi_imapsz = OF_getencprop_alloc(node, "interrupt-map", 1,
266	    (void **)&ii->opi_imap);
267	if (ii->opi_imapsz > 0) {
268		msksz = OF_getencprop_alloc(node, "interrupt-map-mask", 1,
269		    (void **)&ii->opi_imapmsk);
270		/*
271		 * Failure to get the mask is ignored; a full mask is used
272		 * then.  We barf on bad mask sizes, however.
273		 */
274		if (msksz != -1 && msksz != ii->opi_addrc + intrsz)
275			panic("ofw_bus_setup_iinfo: bad interrupt-map-mask "
276			    "property!");
277	}
278}
279
280int
281ofw_bus_lookup_imap(phandle_t node, struct ofw_bus_iinfo *ii, void *reg,
282    int regsz, void *pintr, int pintrsz, void *mintr, int mintrsz,
283    phandle_t *iparent)
284{
285	uint8_t maskbuf[regsz + pintrsz];
286	int rv;
287
288	if (ii->opi_imapsz <= 0)
289		return (0);
290	KASSERT(regsz >= ii->opi_addrc,
291	    ("ofw_bus_lookup_imap: register size too small: %d < %d",
292		regsz, ii->opi_addrc));
293	if (node != -1) {
294		rv = OF_getencprop(node, "reg", reg, regsz);
295		if (rv < regsz)
296			panic("ofw_bus_lookup_imap: cannot get reg property");
297	}
298	return (ofw_bus_search_intrmap(pintr, pintrsz, reg, ii->opi_addrc,
299	    ii->opi_imap, ii->opi_imapsz, ii->opi_imapmsk, maskbuf, mintr,
300	    mintrsz, iparent));
301}
302
303/*
304 * Map an interrupt using the firmware reg, interrupt-map and
305 * interrupt-map-mask properties.
306 * The interrupt property to be mapped must be of size intrsz, and pointed to
307 * by intr.  The regs property of the node for which the mapping is done must
308 * be passed as regs. This property is an array of register specifications;
309 * the size of the address part of such a specification must be passed as
310 * physsz.  Only the first element of the property is used.
311 * imap and imapsz hold the interrupt mask and it's size.
312 * imapmsk is a pointer to the interrupt-map-mask property, which must have
313 * a size of physsz + intrsz; it may be NULL, in which case a full mask is
314 * assumed.
315 * maskbuf must point to a buffer of length physsz + intrsz.
316 * The interrupt is returned in result, which must point to a buffer of length
317 * rintrsz (which gives the expected size of the mapped interrupt).
318 * Returns number of cells in the interrupt if a mapping was found, 0 otherwise.
319 */
320int
321ofw_bus_search_intrmap(void *intr, int intrsz, void *regs, int physsz,
322    void *imap, int imapsz, void *imapmsk, void *maskbuf, void *result,
323    int rintrsz, phandle_t *iparent)
324{
325	phandle_t parent;
326	uint8_t *ref = maskbuf;
327	uint8_t *uiintr = intr;
328	uint8_t *uiregs = regs;
329	uint8_t *uiimapmsk = imapmsk;
330	uint8_t *mptr;
331	pcell_t pintrsz;
332	int i, rsz, tsz;
333
334	rsz = -1;
335	if (imapmsk != NULL) {
336		for (i = 0; i < physsz; i++)
337			ref[i] = uiregs[i] & uiimapmsk[i];
338		for (i = 0; i < intrsz; i++)
339			ref[physsz + i] = uiintr[i] & uiimapmsk[physsz + i];
340	} else {
341		bcopy(regs, ref, physsz);
342		bcopy(intr, ref + physsz, intrsz);
343	}
344
345	mptr = imap;
346	i = imapsz;
347	while (i > 0) {
348		bcopy(mptr + physsz + intrsz, &parent, sizeof(parent));
349		if (OF_searchencprop(OF_node_from_xref(parent),
350		    "#interrupt-cells", &pintrsz, sizeof(pintrsz)) == -1)
351			pintrsz = 1;	/* default */
352		pintrsz *= sizeof(pcell_t);
353
354		/* Compute the map stride size. */
355		tsz = physsz + intrsz + sizeof(phandle_t) + pintrsz;
356		KASSERT(i >= tsz, ("ofw_bus_search_intrmap: truncated map"));
357
358		if (bcmp(ref, mptr, physsz + intrsz) == 0) {
359			bcopy(mptr + physsz + intrsz + sizeof(parent),
360			    result, MIN(rintrsz, pintrsz));
361
362			if (iparent != NULL)
363				*iparent = parent;
364			return (pintrsz/sizeof(pcell_t));
365		}
366		mptr += tsz;
367		i -= tsz;
368	}
369	return (0);
370}
371
372int
373ofw_bus_reg_to_rl(device_t dev, phandle_t node, pcell_t acells, pcell_t scells,
374    struct resource_list *rl)
375{
376	uint64_t phys, size;
377	ssize_t i, j, rid, nreg, ret;
378	uint32_t *reg;
379	char *name;
380
381	/*
382	 * This may be just redundant when having ofw_bus_devinfo
383	 * but makes this routine independent of it.
384	 */
385	ret = OF_getencprop_alloc(node, "name", sizeof(*name), (void **)&name);
386	if (ret == -1)
387		name = NULL;
388
389	ret = OF_getencprop_alloc(node, "reg", sizeof(*reg), (void **)&reg);
390	nreg = (ret == -1) ? 0 : ret;
391
392	if (nreg % (acells + scells) != 0) {
393		if (bootverbose)
394			device_printf(dev, "Malformed reg property on <%s>\n",
395			    (name == NULL) ? "unknown" : name);
396		nreg = 0;
397	}
398
399	for (i = 0, rid = 0; i < nreg; i += acells + scells, rid++) {
400		phys = size = 0;
401		for (j = 0; j < acells; j++) {
402			phys <<= 32;
403			phys |= reg[i + j];
404		}
405		for (j = 0; j < scells; j++) {
406			size <<= 32;
407			size |= reg[i + acells + j];
408		}
409		/* Skip the dummy reg property of glue devices like ssm(4). */
410		if (size != 0)
411			resource_list_add(rl, SYS_RES_MEMORY, rid,
412			    phys, phys + size - 1, size);
413	}
414	free(name, M_OFWPROP);
415	free(reg, M_OFWPROP);
416
417	return (0);
418}
419
420int
421ofw_bus_intr_to_rl(device_t dev, phandle_t node, struct resource_list *rl)
422{
423	phandle_t iparent;
424	uint32_t icells, *intr;
425	int err, i, irqnum, nintr, rid;
426	boolean_t extended;
427
428	nintr = OF_getencprop_alloc(node, "interrupts",  sizeof(*intr),
429	    (void **)&intr);
430	if (nintr > 0) {
431		if (OF_searchencprop(node, "interrupt-parent", &iparent,
432		    sizeof(iparent)) == -1) {
433			for (iparent = node; iparent != 0;
434			    iparent = OF_parent(node)) {
435				if (OF_hasprop(iparent, "interrupt-controller"))
436					break;
437			}
438			if (iparent == 0) {
439				device_printf(dev, "No interrupt-parent found, "
440				    "assuming direct parent\n");
441				iparent = OF_parent(node);
442			}
443			iparent = OF_xref_from_node(iparent);
444		}
445		if (OF_searchencprop(OF_node_from_xref(iparent),
446		    "#interrupt-cells", &icells, sizeof(icells)) == -1) {
447			device_printf(dev, "Missing #interrupt-cells "
448			    "property, assuming <1>\n");
449			icells = 1;
450		}
451		if (icells < 1 || icells > nintr) {
452			device_printf(dev, "Invalid #interrupt-cells property "
453			    "value <%d>, assuming <1>\n", icells);
454			icells = 1;
455		}
456		extended = false;
457	} else {
458		nintr = OF_getencprop_alloc(node, "interrupts-extended",
459		    sizeof(*intr), (void **)&intr);
460		if (nintr <= 0)
461			return (0);
462		extended = true;
463	}
464	err = 0;
465	rid = 0;
466	for (i = 0; i < nintr; i += icells) {
467		if (extended) {
468			iparent = intr[i++];
469			if (OF_searchencprop(OF_node_from_xref(iparent),
470			    "#interrupt-cells", &icells, sizeof(icells)) == -1) {
471				device_printf(dev, "Missing #interrupt-cells "
472				    "property\n");
473				err = ENOENT;
474				break;
475			}
476			if (icells < 1 || (i + icells) > nintr) {
477				device_printf(dev, "Invalid #interrupt-cells "
478				    "property value <%d>\n", icells);
479				err = ERANGE;
480				break;
481			}
482		}
483		irqnum = ofw_bus_map_intr(dev, iparent, icells, &intr[i]);
484		resource_list_add(rl, SYS_RES_IRQ, rid++, irqnum, irqnum, 1);
485	}
486	free(intr, M_OFWPROP);
487	return (err);
488}
489
490