2 * Copyright (c) 2015-2016 Landon Fuller <landon@landonf.org>
3 * Copyright (c) 2017 The FreeBSD Foundation
6 * Portions of this software were developed by Landon Fuller
7 * under sponsorship from the FreeBSD Foundation.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer,
14 * without modification.
15 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
16 * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
17 * redistribution must be conditioned upon including a substantially
18 * similar Disclaimer requirement for further binary redistribution.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
24 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
25 * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
26 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
29 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
31 * THE POSSIBILITY OF SUCH DAMAGES.
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
37 #include <sys/param.h>
39 #include <sys/kernel.h>
40 #include <sys/limits.h>
41 #include <sys/systm.h>
43 #include <machine/bus.h>
44 #include <machine/resource.h>
46 #include <dev/bhnd/bhndvar.h>
51 static int siba_register_interrupts(device_t dev, device_t child,
52 struct siba_devinfo *dinfo);
53 static int siba_append_dinfo_region(struct siba_devinfo *dinfo,
54 uint8_t addridx, uint32_t base, uint32_t size,
55 uint32_t bus_reserved);
58 * Map a siba(4) OCP vendor code to its corresponding JEDEC JEP-106 vendor
61 * @param ocp_vendor An OCP vendor code.
62 * @return The BHND_MFGID constant corresponding to @p ocp_vendor, or
63 * BHND_MFGID_INVALID if the OCP vendor is unknown.
66 siba_get_bhnd_mfgid(uint16_t ocp_vendor)
70 return (BHND_MFGID_BCM);
72 return (BHND_MFGID_INVALID);
77 * Allocate and return a new empty device info structure.
79 * @param bus The requesting bus device.
81 * @retval NULL if allocation failed.
84 siba_alloc_dinfo(device_t bus)
86 struct siba_devinfo *dinfo;
88 dinfo = malloc(sizeof(struct siba_devinfo), M_BHND, M_NOWAIT|M_ZERO);
92 for (u_int i = 0; i < nitems(dinfo->cfg); i++) {
93 dinfo->cfg[i] = ((struct siba_cfg_block){
98 dinfo->cfg_res[i] = NULL;
99 dinfo->cfg_rid[i] = -1;
102 resource_list_init(&dinfo->resources);
104 dinfo->pmu_state = SIBA_PMU_NONE;
106 dinfo->intr = (struct siba_intr) {
115 * Initialize a device info structure previously allocated via
116 * siba_alloc_dinfo, copying the provided core id.
118 * @param dev The requesting bus device.
119 * @param child The siba child device.
120 * @param dinfo The device info instance.
121 * @param core Device core info.
124 * @retval non-zero initialization failed.
127 siba_init_dinfo(device_t dev, device_t child, struct siba_devinfo *dinfo,
128 const struct siba_core_id *core_id)
132 dinfo->core_id = *core_id;
134 /* Register all address space mappings */
135 for (uint8_t i = 0; i < core_id->num_admatch; i++) {
136 uint32_t bus_reserved;
138 /* If this is the device's core/enumeration addrespace,
139 * reserve the Sonics configuration register blocks for the
142 if (i == SIBA_CORE_ADDRSPACE)
143 bus_reserved = core_id->num_cfg_blocks * SIBA_CFG_SIZE;
145 /* Append the region info */
146 error = siba_append_dinfo_region(dinfo, i,
147 core_id->admatch[i].am_base, core_id->admatch[i].am_size,
153 /* Register all interrupt(s) */
154 if ((error = siba_register_interrupts(dev, child, dinfo)))
162 * Register and map all interrupts for @p dinfo.
164 * @param dev The siba bus device.
165 * @param child The siba child device.
166 * @param dinfo The device info instance on which to register all interrupt
170 siba_register_interrupts(device_t dev, device_t child,
171 struct siba_devinfo *dinfo)
175 /* Is backplane interrupt distribution enabled for this core? */
176 if (!dinfo->core_id.intr_en)
179 /* Have one interrupt */
180 dinfo->intr.mapped = false;
182 dinfo->intr.rid = -1;
184 /* Map the interrupt */
185 error = BHND_BUS_MAP_INTR(dev, child, 0 /* single intr is always 0 */,
188 device_printf(dev, "failed mapping interrupt line for core %u: "
189 "%d\n", dinfo->core_id.core_info.core_idx, error);
192 dinfo->intr.mapped = true;
194 /* Update the resource list */
195 dinfo->intr.rid = resource_list_add_next(&dinfo->resources, SYS_RES_IRQ,
196 dinfo->intr.irq, dinfo->intr.irq, 1);
202 * Map an addrspace index to its corresponding bhnd(4) BHND_PORT_DEVICE port
205 * @param addrspace Address space index.
208 siba_addrspace_device_port(u_int addrspace)
210 /* The first addrspace is always mapped to device0; the remainder
211 * are mapped to device1 */
219 * Map an addrspace index to its corresponding bhnd(4) BHND_PORT_DEVICE port
222 * @param addrspace Address space index.
225 siba_addrspace_device_region(u_int addrspace)
227 /* The first addrspace is always mapped to device0.0; the remainder
228 * are mapped to device1.0 + (n - 1) */
232 return (addrspace - 1);
236 * Map an config block index to its corresponding bhnd(4) BHND_PORT_AGENT port
239 * @param cfg Config block index.
242 siba_cfg_agent_port(u_int cfg)
249 * Map an config block index to its corresponding bhnd(4) BHND_PORT_AGENT port
252 * @param cfg Config block index.
255 siba_cfg_agent_region(u_int cfg)
257 /* Always agent0.<idx> */
262 * Return the number of bhnd(4) ports to advertise for the given
263 * @p core_id and @p port_type.
265 * Refer to the siba_addrspace_index() and siba_cfg_index() functions for
266 * information on siba's mapping of bhnd(4) port and region identifiers.
268 * @param core_id The siba core info.
269 * @param port_type The bhnd(4) port type.
272 siba_port_count(struct siba_core_id *core_id, bhnd_port_type port_type)
275 case BHND_PORT_DEVICE:
276 /* 0, 1, or 2 ports */
277 return (min(core_id->num_admatch, 2));
279 case BHND_PORT_AGENT:
280 /* One agent port maps all configuration blocks */
281 if (core_id->num_cfg_blocks > 0)
284 /* Do not advertise an agent port if there are no configuration
294 * Return true if @p port of @p port_type is defined by @p core_id, false
297 * @param core_id The siba core info.
298 * @param port_type The bhnd(4) port type.
299 * @param port The bhnd(4) port number.
302 siba_is_port_valid(struct siba_core_id *core_id, bhnd_port_type port_type,
305 /* Verify the index against the port count */
306 if (siba_port_count(core_id, port_type) <= port)
313 * Return the number of bhnd(4) regions to advertise for @p core_id on the
314 * @p port of @p port_type.
316 * @param core_id The siba core info.
317 * @param port_type The bhnd(4) port type.
320 siba_port_region_count(struct siba_core_id *core_id, bhnd_port_type port_type,
323 /* The port must exist */
324 if (!siba_is_port_valid(core_id, port_type, port))
328 case BHND_PORT_DEVICE:
329 /* The first address space, if any, is mapped to device0.0 */
331 return (min(core_id->num_admatch, 1));
333 /* All remaining address spaces are mapped to device0.(n - 1) */
334 if (port == 1 && core_id->num_admatch >= 2)
335 return (core_id->num_admatch - 1);
339 case BHND_PORT_AGENT:
340 /* All config blocks are mapped to a single port */
342 return (core_id->num_cfg_blocks);
350 /* Validated above */
351 panic("siba_is_port_valid() returned true for unknown %s.%u port",
352 bhnd_port_type_name(port_type), port);
357 * Map a bhnd(4) type/port/region triplet to its associated config block index,
360 * We map config registers to port/region identifiers as follows:
362 * [port].[region] [cfg register block]
366 * @param port_type The bhnd(4) port type.
367 * @param port The bhnd(4) port number.
368 * @param region The bhnd(4) port region.
369 * @param addridx On success, the corresponding addrspace index.
372 * @retval ENOENT if the given type/port/region cannot be mapped to a
373 * siba config register block.
376 siba_cfg_index(struct siba_core_id *core_id, bhnd_port_type port_type,
377 u_int port, u_int region, u_int *cfgidx)
379 /* Config blocks are mapped to agent ports */
380 if (port_type != BHND_PORT_AGENT)
383 /* Port must be valid */
384 if (!siba_is_port_valid(core_id, port_type, port))
387 if (region >= core_id->num_cfg_blocks)
390 if (region >= SIBA_MAX_CFG)
399 * Map an bhnd(4) type/port/region triplet to its associated config block
402 * The only supported port type is BHND_PORT_DEVICE.
404 * @param dinfo The device info to search for a matching address space.
405 * @param type The bhnd(4) port type.
406 * @param port The bhnd(4) port number.
407 * @param region The bhnd(4) port region.
409 struct siba_cfg_block *
410 siba_find_cfg_block(struct siba_devinfo *dinfo, bhnd_port_type type, u_int port,
416 /* Map to addrspace index */
417 error = siba_cfg_index(&dinfo->core_id, type, port, region, &cfgidx);
422 return (&dinfo->cfg[cfgidx]);
426 * Map a bhnd(4) type/port/region triplet to its associated address space
429 * For compatibility with bcma(4), we map address spaces to port/region
430 * identifiers as follows:
432 * [port.region] [admatch index]
438 * @param core_id The siba core info.
439 * @param port_type The bhnd(4) port type.
440 * @param port The bhnd(4) port number.
441 * @param region The bhnd(4) port region.
442 * @param addridx On success, the corresponding addrspace index.
445 * @retval ENOENT if the given type/port/region cannot be mapped to a
446 * siba address space.
449 siba_addrspace_index(struct siba_core_id *core_id, bhnd_port_type port_type,
450 u_int port, u_int region, u_int *addridx)
454 /* Address spaces are always device ports */
455 if (port_type != BHND_PORT_DEVICE)
458 /* Port must be valid */
459 if (!siba_is_port_valid(core_id, port_type, port))
469 if (idx >= core_id->num_admatch)
478 * Map an bhnd(4) type/port/region triplet to its associated address space
481 * The only supported port type is BHND_PORT_DEVICE.
483 * @param dinfo The device info to search for a matching address space.
484 * @param type The bhnd(4) port type.
485 * @param port The bhnd(4) port number.
486 * @param region The bhnd(4) port region.
488 struct siba_addrspace *
489 siba_find_addrspace(struct siba_devinfo *dinfo, bhnd_port_type type, u_int port,
495 /* Map to addrspace index */
496 error = siba_addrspace_index(&dinfo->core_id, type, port, region,
502 if (addridx >= SIBA_MAX_ADDRSPACE)
505 return (&dinfo->addrspace[addridx]);
509 * Append an address space entry to @p dinfo.
511 * @param dinfo The device info entry to update.
512 * @param addridx The address space index.
513 * @param base The mapping's base address.
514 * @param size The mapping size.
515 * @param bus_reserved Number of bytes to reserve in @p size for bus use
516 * when registering the resource list entry. This is used to reserve bus
517 * access to the core's SIBA_CFG* register blocks.
520 * @retval non-zero An error occurred appending the entry.
523 siba_append_dinfo_region(struct siba_devinfo *dinfo, uint8_t addridx,
524 uint32_t base, uint32_t size, uint32_t bus_reserved)
526 struct siba_addrspace *sa;
529 /* Verify that base + size will not overflow */
530 if (size > 0 && UINT32_MAX - (size - 1) < base)
533 /* Verify that size - bus_reserved will not underflow */
534 if (size < bus_reserved)
537 /* Must not be 0-length */
541 /* Must not exceed addrspace array size */
542 if (addridx >= nitems(dinfo->addrspace))
545 /* Initialize new addrspace entry */
546 sa = &dinfo->addrspace[addridx];
549 sa->sa_bus_reserved = bus_reserved;
551 /* Populate the resource list */
552 r_size = size - bus_reserved;
553 sa->sa_rid = resource_list_add_next(&dinfo->resources, SYS_RES_MEMORY,
554 base, base + (r_size - 1), r_size);
560 * Deallocate the given device info structure and any associated resources.
562 * @param dev The requesting bus device.
563 * @param child The siba child device.
564 * @param dinfo Device info associated with @p child to be deallocated.
567 siba_free_dinfo(device_t dev, device_t child, struct siba_devinfo *dinfo)
569 resource_list_free(&dinfo->resources);
571 /* Free all mapped configuration blocks */
572 for (u_int i = 0; i < nitems(dinfo->cfg); i++) {
573 if (dinfo->cfg_res[i] == NULL)
576 bhnd_release_resource(dev, SYS_RES_MEMORY, dinfo->cfg_rid[i],
579 dinfo->cfg_res[i] = NULL;
580 dinfo->cfg_rid[i] = -1;
583 /* Unmap the core's interrupt */
584 if (dinfo->core_id.intr_en && dinfo->intr.mapped) {
585 BHND_BUS_UNMAP_INTR(dev, child, dinfo->intr.irq);
586 dinfo->intr.mapped = false;
593 * Return the core-enumeration-relative offset for the @p addrspace
594 * SIBA_R0_ADMATCH* register.
596 * @param addrspace The address space index.
598 * @retval non-zero success
599 * @retval 0 the given @p addrspace index is not supported.
602 siba_admatch_offset(uint8_t addrspace)
606 return SB0_REG_ABS(SIBA_CFG0_ADMATCH0);
608 return SB0_REG_ABS(SIBA_CFG0_ADMATCH1);
610 return SB0_REG_ABS(SIBA_CFG0_ADMATCH2);
612 return SB0_REG_ABS(SIBA_CFG0_ADMATCH3);
619 * Parse a SIBA_R0_ADMATCH* register.
621 * @param addrspace The address space index.
622 * @param am The address match register value to be parsed.
623 * @param[out] admatch The parsed address match descriptor
626 * @retval non-zero a parse error occurred.
629 siba_parse_admatch(uint32_t am, struct siba_admatch *admatch)
633 /* Extract the base address and size */
634 am_type = SIBA_REG_GET(am, AM_TYPE);
637 /* Type 0 entries are always enabled, and do not support
638 * negative matching */
639 admatch->am_base = am & SIBA_AM_BASE0_MASK;
640 admatch->am_size = 1 << (SIBA_REG_GET(am, AM_ADINT0) + 1);
641 admatch->am_enabled = true;
642 admatch->am_negative = false;
645 admatch->am_base = am & SIBA_AM_BASE1_MASK;
646 admatch->am_size = 1 << (SIBA_REG_GET(am, AM_ADINT1) + 1);
647 admatch->am_enabled = ((am & SIBA_AM_ADEN) != 0);
648 admatch->am_negative = ((am & SIBA_AM_ADNEG) != 0);
651 admatch->am_base = am & SIBA_AM_BASE2_MASK;
652 admatch->am_size = 1 << (SIBA_REG_GET(am, AM_ADINT2) + 1);
653 admatch->am_enabled = ((am & SIBA_AM_ADEN) != 0);
654 admatch->am_negative = ((am & SIBA_AM_ADNEG) != 0);
664 * Write @p value to @p dev's CFG0 target/initiator state register, performing
665 * required read-back and waiting for completion.
667 * @param dev The siba(4) child device.
668 * @param reg The CFG0 state register to write (e.g. SIBA_CFG0_TMSTATELOW,
670 * @param value The value to write to @p reg.
671 * @param mask The mask of bits to be included from @p value.
674 siba_write_target_state(device_t dev, struct siba_devinfo *dinfo,
675 bus_size_t reg, uint32_t value, uint32_t mask)
677 struct bhnd_resource *r;
680 r = dinfo->cfg_res[0];
682 KASSERT(r != NULL, ("%s missing CFG0 mapping",
683 device_get_nameunit(dev)));
684 KASSERT(reg <= SIBA_CFG_SIZE-4, ("%s invalid CFG0 register offset %#jx",
685 device_get_nameunit(dev), (uintmax_t)reg));
687 rval = bhnd_bus_read_4(r, reg);
689 rval |= (value & mask);
691 bhnd_bus_write_4(r, reg, rval);
692 bhnd_bus_read_4(r, reg); /* read-back */
697 * Spin for up to @p usec waiting for @p dev's CFG0 target/initiator state
698 * register value to be equal to @p value after applying @p mask bits to both
701 * @param dev The siba(4) child device to wait on.
702 * @param dinfo The @p dev's device info
703 * @param reg The state register to read (e.g. SIBA_CFG0_TMSTATEHIGH,
705 * @param value The value against which @p reg will be compared.
706 * @param mask The mask to be applied when comparing @p value with @p reg.
707 * @param usec The maximum number of microseconds to wait for completion.
709 * @retval 0 if SIBA_TMH_BUSY is cleared prior to the @p usec timeout.
710 * @retval ENODEV if SIBA_CFG0 is not mapped by @p dinfo.
711 * @retval ETIMEDOUT if a timeout occurs.
714 siba_wait_target_state(device_t dev, struct siba_devinfo *dinfo, bus_size_t reg,
715 uint32_t value, uint32_t mask, u_int usec)
717 struct bhnd_resource *r;
720 if ((r = dinfo->cfg_res[0]) == NULL)
724 for (int i = 0; i < usec; i += 10) {
725 rval = bhnd_bus_read_4(r, reg);
726 if ((rval & mask) == value)