xref: /freebsd/sys/dev/bhnd/bcma/bcma_subr.c (revision ca942ab4b2b665e5375cc72afc4930d3b33b14f9)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2015-2016 Landon Fuller <landon@landonf.org>
5  * Copyright (c) 2017 The FreeBSD Foundation
6  * All rights reserved.
7  *
8  * Portions of this software were developed by Landon Fuller
9  * under sponsorship from the FreeBSD Foundation.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
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.
21  *
22  * NO WARRANTY
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.
34  */
35 
36 #include <sys/param.h>
37 #include <sys/bus.h>
38 #include <sys/kernel.h>
39 #include <sys/limits.h>
40 #include <sys/systm.h>
41 
42 #include <machine/bus.h>
43 #include <machine/resource.h>
44 
45 #include <dev/bhnd/bhndvar.h>
46 
47 #include "bcma_dmp.h"
48 
49 #include "bcmavar.h"
50 
51 /* Return the resource ID for a device's agent register allocation */
52 #define	BCMA_AGENT_RID(_dinfo)	\
53     (BCMA_AGENT_RID_BASE + BCMA_DINFO_COREIDX(_dinfo))
54 
55  /**
56  * Allocate and initialize new core config structure.
57  *
58  * @param core_index Core index on the bus.
59  * @param core_unit Core unit number.
60  * @param vendor Core designer.
61  * @param device Core identifier (e.g. part number).
62  * @param hwrev Core revision.
63  */
64 struct bcma_corecfg *
bcma_alloc_corecfg(u_int core_index,int core_unit,uint16_t vendor,uint16_t device,uint8_t hwrev)65 bcma_alloc_corecfg(u_int core_index, int core_unit, uint16_t vendor,
66     uint16_t device, uint8_t hwrev)
67 {
68 	struct bcma_corecfg *cfg;
69 
70 	cfg = malloc(sizeof(*cfg), M_BHND, M_NOWAIT);
71 	if (cfg == NULL)
72 		return NULL;
73 
74 	cfg->core_info = (struct bhnd_core_info) {
75 		.vendor = vendor,
76 		.device = device,
77 		.hwrev = hwrev,
78 		.core_idx = core_index,
79 		.unit = core_unit
80 	};
81 
82 	STAILQ_INIT(&cfg->master_ports);
83 	cfg->num_master_ports = 0;
84 
85 	STAILQ_INIT(&cfg->dev_ports);
86 	cfg->num_dev_ports = 0;
87 
88 	STAILQ_INIT(&cfg->bridge_ports);
89 	cfg->num_bridge_ports = 0;
90 
91 	STAILQ_INIT(&cfg->wrapper_ports);
92 	cfg->num_wrapper_ports = 0;
93 
94 	return (cfg);
95 }
96 
97 /**
98  * Deallocate the given core config and any associated resources.
99  *
100  * @param corecfg Core info to be deallocated.
101  */
102 void
bcma_free_corecfg(struct bcma_corecfg * corecfg)103 bcma_free_corecfg(struct bcma_corecfg *corecfg)
104 {
105 	struct bcma_mport *mport, *mnext;
106 	struct bcma_sport *sport, *snext;
107 
108 	STAILQ_FOREACH_SAFE(mport, &corecfg->master_ports, mp_link, mnext) {
109 		free(mport, M_BHND);
110 	}
111 
112 	STAILQ_FOREACH_SAFE(sport, &corecfg->dev_ports, sp_link, snext) {
113 		bcma_free_sport(sport);
114 	}
115 
116 	STAILQ_FOREACH_SAFE(sport, &corecfg->bridge_ports, sp_link, snext) {
117 		bcma_free_sport(sport);
118 	}
119 
120 	STAILQ_FOREACH_SAFE(sport, &corecfg->wrapper_ports, sp_link, snext) {
121 		bcma_free_sport(sport);
122 	}
123 
124 	free(corecfg, M_BHND);
125 }
126 
127 /**
128  * Return the @p cfg port list for @p type.
129  *
130  * @param cfg The core configuration.
131  * @param type The requested port type.
132  */
133 struct bcma_sport_list *
bcma_corecfg_get_port_list(struct bcma_corecfg * cfg,bhnd_port_type type)134 bcma_corecfg_get_port_list(struct bcma_corecfg *cfg, bhnd_port_type type)
135 {
136 	switch (type) {
137 	case BHND_PORT_DEVICE:
138 		return (&cfg->dev_ports);
139 		break;
140 	case BHND_PORT_BRIDGE:
141 		return (&cfg->bridge_ports);
142 		break;
143 	case BHND_PORT_AGENT:
144 		return (&cfg->wrapper_ports);
145 		break;
146 	default:
147 		return (NULL);
148 	}
149 }
150 
151 /**
152  * Populate the resource list and bcma_map RIDs using the maps defined on
153  * @p ports.
154  *
155  * @param bus The requesting bus device.
156  * @param dinfo The device info instance to be initialized.
157  * @param ports The set of ports to be enumerated
158  */
159 static void
bcma_dinfo_init_port_resource_info(device_t bus,struct bcma_devinfo * dinfo,struct bcma_sport_list * ports)160 bcma_dinfo_init_port_resource_info(device_t bus, struct bcma_devinfo *dinfo,
161     struct bcma_sport_list *ports)
162 {
163 	struct bcma_map		*map;
164 	struct bcma_sport	*port;
165 	bhnd_addr_t		 end;
166 
167 	STAILQ_FOREACH(port, ports, sp_link) {
168 		STAILQ_FOREACH(map, &port->sp_maps, m_link) {
169 			/*
170 			 * Create the corresponding device resource list entry.
171 			 *
172 			 * We necessarily skip registration if the region's
173 			 * device memory range is not representable via
174 			 * rman_res_t.
175 			 *
176 			 * When rman_res_t is migrated to uintmax_t, any
177 			 * range should be representable.
178 			 */
179 			end = map->m_base + map->m_size;
180 			if (map->m_base <= RM_MAX_END && end <= RM_MAX_END) {
181 				map->m_rid = resource_list_add_next(
182 				    &dinfo->resources, SYS_RES_MEMORY,
183 				    map->m_base, end, map->m_size);
184 			} else if (bootverbose) {
185 				device_printf(bus,
186 				    "core%u %s%u.%u: region %llx-%llx extends "
187 				        "beyond supported addressable range\n",
188 				    dinfo->corecfg->core_info.core_idx,
189 				    bhnd_port_type_name(port->sp_type),
190 				    port->sp_num, map->m_region_num,
191 				    (unsigned long long) map->m_base,
192 				    (unsigned long long) end);
193 			}
194 		}
195 	}
196 }
197 
198 /**
199  * Allocate the per-core agent register block for a device info structure.
200  *
201  * If an agent0.0 region is not defined on @p dinfo, the device info
202  * agent resource is set to NULL and 0 is returned.
203  *
204  * @param bus The requesting bus device.
205  * @param child The bcma child device.
206  * @param dinfo The device info associated with @p child
207  *
208  * @retval 0 success
209  * @retval non-zero resource allocation failed.
210  */
211 static int
bcma_dinfo_init_agent(device_t bus,device_t child,struct bcma_devinfo * dinfo)212 bcma_dinfo_init_agent(device_t bus, device_t child, struct bcma_devinfo *dinfo)
213 {
214 	bhnd_addr_t	addr;
215 	bhnd_size_t	size;
216 	rman_res_t	r_start, r_count, r_end;
217 	int		error, rid_agent;
218 
219 	KASSERT(dinfo->res_agent == NULL, ("double allocation of agent"));
220 
221 	/* Verify that the agent register block exists and is
222 	 * mappable */
223 	if (bhnd_get_port_rid(child, BHND_PORT_AGENT, 0, 0) == -1)
224 		return (0);	/* nothing to do */
225 
226 	/* Fetch the address of the agent register block */
227 	error = bhnd_get_region_addr(child, BHND_PORT_AGENT, 0, 0,
228 	    &addr, &size);
229 	if (error) {
230 		device_printf(bus, "failed fetching agent register block "
231 		    "address for core %u\n", BCMA_DINFO_COREIDX(dinfo));
232 		return (error);
233 	}
234 
235 	/* Allocate the resource */
236 	r_start = addr;
237 	r_count = size;
238 	r_end = r_start + r_count - 1;
239 
240 	rid_agent = BCMA_AGENT_RID(dinfo);
241 	dinfo->res_agent = BHND_BUS_ALLOC_RESOURCE(bus, bus, SYS_RES_MEMORY,
242 	    rid_agent, r_start, r_end, r_count, RF_ACTIVE|RF_SHAREABLE);
243 	if (dinfo->res_agent == NULL) {
244 		device_printf(bus, "failed allocating agent register block for "
245 		    "core %u\n", BCMA_DINFO_COREIDX(dinfo));
246 		return (ENXIO);
247 	}
248 
249 	return (0);
250 }
251 
252 /**
253  * Populate the list of interrupts for a device info structure
254  * previously initialized via bcma_dinfo_alloc_agent().
255  *
256  * If an agent0.0 region is not mapped on @p dinfo, the OOB interrupt bank is
257  * assumed to be unavailable and 0 is returned.
258  *
259  * @param bus The requesting bus device.
260  * @param dinfo The device info instance to be initialized.
261  */
262 static int
bcma_dinfo_init_intrs(device_t bus,device_t child,struct bcma_devinfo * dinfo)263 bcma_dinfo_init_intrs(device_t bus, device_t child,
264     struct bcma_devinfo *dinfo)
265 {
266 	uint32_t dmpcfg, oobw;
267 
268 	/* Agent block must be mapped */
269 	if (dinfo->res_agent == NULL)
270 		return (0);
271 
272 	/* Agent must support OOB */
273 	dmpcfg = bhnd_bus_read_4(dinfo->res_agent, BCMA_DMP_CONFIG);
274 	if (!BCMA_DMP_GET_FLAG(dmpcfg, BCMA_DMP_CFG_OOB))
275 		return (0);
276 
277 	/* Fetch width of the OOB interrupt bank */
278 	oobw = bhnd_bus_read_4(dinfo->res_agent,
279 	     BCMA_DMP_OOB_OUTWIDTH(BCMA_OOB_BANK_INTR));
280 	if (oobw >= BCMA_OOB_NUM_SEL) {
281 		device_printf(bus, "ignoring invalid OOBOUTWIDTH for core %u: "
282 		    "%#x\n", BCMA_DINFO_COREIDX(dinfo), oobw);
283 		return (0);
284 	}
285 
286 	/* Fetch OOBSEL busline values and populate list of interrupt
287 	 * descriptors */
288 	for (uint32_t sel = 0; sel < oobw; sel++) {
289 		struct bcma_intr	*intr;
290 		uint32_t		 selout;
291 		uint8_t			 line;
292 
293 		if (dinfo->num_intrs == UINT_MAX)
294 			return (ENOMEM);
295 
296 		selout = bhnd_bus_read_4(dinfo->res_agent, BCMA_DMP_OOBSELOUT(
297 		    BCMA_OOB_BANK_INTR, sel));
298 
299 		line = (selout >> BCMA_DMP_OOBSEL_SHIFT(sel)) &
300 		    BCMA_DMP_OOBSEL_BUSLINE_MASK;
301 
302 		intr = bcma_alloc_intr(BCMA_OOB_BANK_INTR, sel, line);
303 		if (intr == NULL) {
304 			device_printf(bus, "failed allocating interrupt "
305 			    "descriptor %#x for core %u\n", sel,
306 			    BCMA_DINFO_COREIDX(dinfo));
307 			return (ENOMEM);
308 		}
309 
310 		STAILQ_INSERT_HEAD(&dinfo->intrs, intr, i_link);
311 		dinfo->num_intrs++;
312 	}
313 
314 	return (0);
315 }
316 
317 /**
318  * Allocate and return a new empty device info structure.
319  *
320  * @param bus The requesting bus device.
321  *
322  * @retval NULL if allocation failed.
323  */
324 struct bcma_devinfo *
bcma_alloc_dinfo(device_t bus)325 bcma_alloc_dinfo(device_t bus)
326 {
327 	struct bcma_devinfo *dinfo;
328 
329 	dinfo = malloc(sizeof(struct bcma_devinfo), M_BHND, M_NOWAIT|M_ZERO);
330 	if (dinfo == NULL)
331 		return (NULL);
332 
333 	dinfo->corecfg = NULL;
334 	dinfo->res_agent = NULL;
335 
336 	STAILQ_INIT(&dinfo->intrs);
337 	dinfo->num_intrs = 0;
338 
339 	resource_list_init(&dinfo->resources);
340 
341 	return (dinfo);
342 }
343 
344 /**
345  * Initialize a device info structure previously allocated via
346  * bcma_alloc_dinfo, assuming ownership of the provided core
347  * configuration.
348  *
349  * @param bus The requesting bus device.
350  * @param child The bcma child device.
351  * @param dinfo The device info associated with @p child
352  * @param corecfg Device core configuration; ownership of this value
353  * will be assumed by @p dinfo.
354  *
355  * @retval 0 success
356  * @retval non-zero initialization failed.
357  */
358 int
bcma_init_dinfo(device_t bus,device_t child,struct bcma_devinfo * dinfo,struct bcma_corecfg * corecfg)359 bcma_init_dinfo(device_t bus, device_t child, struct bcma_devinfo *dinfo,
360     struct bcma_corecfg *corecfg)
361 {
362 	struct bcma_intr	*intr;
363 	int			 error;
364 
365 	KASSERT(dinfo->corecfg == NULL, ("dinfo previously initialized"));
366 
367 	/* Save core configuration value */
368 	dinfo->corecfg = corecfg;
369 
370 	/* The device ports must always be initialized first to ensure that
371 	 * rid 0 maps to the first device port */
372 	bcma_dinfo_init_port_resource_info(bus, dinfo, &corecfg->dev_ports);
373 	bcma_dinfo_init_port_resource_info(bus, dinfo, &corecfg->bridge_ports);
374 	bcma_dinfo_init_port_resource_info(bus, dinfo, &corecfg->wrapper_ports);
375 
376 	/* Now that we've defined the port resources, we can map the device's
377 	 * agent registers (if any) */
378 	if ((error = bcma_dinfo_init_agent(bus, child, dinfo)))
379 		goto failed;
380 
381 	/* With agent registers mapped, we can populate the device's interrupt
382 	 * descriptors */
383 	if ((error = bcma_dinfo_init_intrs(bus, child, dinfo)))
384 		goto failed;
385 
386 	/* Finally, map the interrupt descriptors */
387 	STAILQ_FOREACH(intr, &dinfo->intrs, i_link) {
388 		/* Already mapped? */
389 		if (intr->i_mapped)
390 			continue;
391 
392 		/* Map the interrupt */
393 		error = BHND_BUS_MAP_INTR(bus, child, intr->i_sel,
394 		    &intr->i_irq);
395 		if (error) {
396 			device_printf(bus, "failed mapping interrupt line %u "
397 			    "for core %u: %d\n", intr->i_sel,
398 			    BCMA_DINFO_COREIDX(dinfo), error);
399 			goto failed;
400 		}
401 
402 		intr->i_mapped = true;
403 
404 		/* Add to resource list */
405 		intr->i_rid = resource_list_add_next(&dinfo->resources,
406 		    SYS_RES_IRQ, intr->i_irq, intr->i_irq, 1);
407 	}
408 
409 	return (0);
410 
411 failed:
412 	/* Owned by the caller on failure */
413 	dinfo->corecfg = NULL;
414 
415 	return (error);
416 }
417 
418 /**
419  * Deallocate the given device info structure and any associated resources.
420  *
421  * @param bus The requesting bus device.
422  * @param dinfo Device info to be deallocated.
423  */
424 void
bcma_free_dinfo(device_t bus,device_t child,struct bcma_devinfo * dinfo)425 bcma_free_dinfo(device_t bus, device_t child, struct bcma_devinfo *dinfo)
426 {
427 	struct bcma_intr *intr, *inext;
428 
429 	resource_list_free(&dinfo->resources);
430 
431 	if (dinfo->corecfg != NULL)
432 		bcma_free_corecfg(dinfo->corecfg);
433 
434 	/* Release agent resource, if any */
435 	if (dinfo->res_agent != NULL) {
436 		bhnd_release_resource(bus, dinfo->res_agent);
437 	}
438 
439 	/* Clean up interrupt descriptors */
440 	STAILQ_FOREACH_SAFE(intr, &dinfo->intrs, i_link, inext) {
441 		STAILQ_REMOVE(&dinfo->intrs, intr, bcma_intr, i_link);
442 
443 		/* Release our IRQ mapping */
444 		if (intr->i_mapped) {
445 			BHND_BUS_UNMAP_INTR(bus, child, intr->i_irq);
446 			intr->i_mapped = false;
447 		}
448 
449 		bcma_free_intr(intr);
450 	}
451 
452 	free(dinfo, M_BHND);
453 }
454 
455 /**
456  * Allocate and initialize a new interrupt descriptor.
457  *
458  * @param bank OOB bank.
459  * @param sel OOB selector.
460  * @param line OOB bus line.
461  */
462 struct bcma_intr *
bcma_alloc_intr(uint8_t bank,uint8_t sel,uint8_t line)463 bcma_alloc_intr(uint8_t bank, uint8_t sel, uint8_t line)
464 {
465 	struct bcma_intr *intr;
466 
467 	if (bank >= BCMA_OOB_NUM_BANKS)
468 		return (NULL);
469 
470 	if (sel >= BCMA_OOB_NUM_SEL)
471 		return (NULL);
472 
473 	if (line >= BCMA_OOB_NUM_BUSLINES)
474 		return (NULL);
475 
476 	intr = malloc(sizeof(*intr), M_BHND, M_NOWAIT);
477 	if (intr == NULL)
478 		return (NULL);
479 
480 	intr->i_bank = bank;
481 	intr->i_sel = sel;
482 	intr->i_busline = line;
483 	intr->i_mapped = false;
484 	intr->i_irq = 0;
485 
486 	return (intr);
487 }
488 
489 /**
490  * Deallocate all resources associated with the given interrupt descriptor.
491  *
492  * @param intr Interrupt descriptor to be deallocated.
493  */
494 void
bcma_free_intr(struct bcma_intr * intr)495 bcma_free_intr(struct bcma_intr *intr)
496 {
497 	KASSERT(!intr->i_mapped, ("interrupt %u still mapped", intr->i_sel));
498 
499 	free(intr, M_BHND);
500 }
501 
502 /**
503  * Allocate and initialize new slave port descriptor.
504  *
505  * @param port_num Per-core port number.
506  * @param port_type Port type.
507  */
508 struct bcma_sport *
bcma_alloc_sport(bcma_pid_t port_num,bhnd_port_type port_type)509 bcma_alloc_sport(bcma_pid_t port_num, bhnd_port_type port_type)
510 {
511 	struct bcma_sport *sport;
512 
513 	sport = malloc(sizeof(struct bcma_sport), M_BHND, M_NOWAIT);
514 	if (sport == NULL)
515 		return NULL;
516 
517 	sport->sp_num = port_num;
518 	sport->sp_type = port_type;
519 	sport->sp_num_maps = 0;
520 	STAILQ_INIT(&sport->sp_maps);
521 
522 	return sport;
523 }
524 
525 /**
526  * Deallocate all resources associated with the given port descriptor.
527  *
528  * @param sport Port descriptor to be deallocated.
529  */
530 void
bcma_free_sport(struct bcma_sport * sport)531 bcma_free_sport(struct bcma_sport *sport) {
532 	struct bcma_map *map, *mapnext;
533 
534 	STAILQ_FOREACH_SAFE(map, &sport->sp_maps, m_link, mapnext) {
535 		free(map, M_BHND);
536 	}
537 
538 	free(sport, M_BHND);
539 }
540 
541 /**
542  * Given a bcma(4) child's device info, spin waiting for the device's DMP
543  * resetstatus register to clear.
544  *
545  * @param child The bcma(4) child device.
546  * @param dinfo The @p child device info.
547  *
548  * @retval 0 success
549  * @retval ENODEV if @p dinfo does not map an agent register resource.
550  * @retval ETIMEDOUT if timeout occurs
551  */
552 int
bcma_dmp_wait_reset(device_t child,struct bcma_devinfo * dinfo)553 bcma_dmp_wait_reset(device_t child, struct bcma_devinfo *dinfo)
554 {
555 	uint32_t rst;
556 
557 	if (dinfo->res_agent == NULL)
558 		return (ENODEV);
559 
560 	/* 300us should be long enough, but there are references to this
561 	 * requiring up to 10ms when performing reset of an 80211 core
562 	 * after a MAC PSM microcode watchdog event. */
563 	for (int i = 0; i < 10000; i += 10) {
564 		rst = bhnd_bus_read_4(dinfo->res_agent, BCMA_DMP_RESETSTATUS);
565 		if (rst == 0)
566 			return (0);
567 
568 		DELAY(10);
569 	}
570 
571 	device_printf(child, "BCMA_DMP_RESETSTATUS timeout\n");
572 	return (ETIMEDOUT);
573 }
574 
575 /**
576  * Set the bcma(4) child's DMP resetctrl register value, and then wait
577  * for all backplane operations to complete.
578  *
579  * @param child The bcma(4) child device.
580  * @param dinfo The @p child device info.
581  * @param value The new ioctrl value to set.
582  *
583  * @retval 0 success
584  * @retval ENODEV if @p dinfo does not map an agent register resource.
585  * @retval ETIMEDOUT if timeout occurs waiting for reset completion
586  */
587 int
bcma_dmp_write_reset(device_t child,struct bcma_devinfo * dinfo,uint32_t value)588 bcma_dmp_write_reset(device_t child, struct bcma_devinfo *dinfo, uint32_t value)
589 {
590 	uint32_t rst;
591 
592 	if (dinfo->res_agent == NULL)
593 		return (ENODEV);
594 
595 	/* Already in requested reset state? */
596 	rst = bhnd_bus_read_4(dinfo->res_agent, BCMA_DMP_RESETCTRL);
597 	if (rst == value)
598 		return (0);
599 
600 	bhnd_bus_write_4(dinfo->res_agent, BCMA_DMP_RESETCTRL, value);
601 	bhnd_bus_read_4(dinfo->res_agent, BCMA_DMP_RESETCTRL); /* read-back */
602 	DELAY(10);
603 
604 	return (bcma_dmp_wait_reset(child, dinfo));
605 }
606