1/*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2015 Landon Fuller <landon@landonf.org>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer,
12 *    without modification.
13 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
14 *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
15 *    redistribution must be conditioned upon including a substantially
16 *    similar Disclaimer requirement for further binary redistribution.
17 *
18 * NO WARRANTY
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
22 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
23 * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
24 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
27 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
29 * THE POSSIBILITY OF SUCH DAMAGES.
30 *
31 * $FreeBSD$
32 */
33
34#ifndef _BHND_CORES_PMU_BHND_PMUVAR_H_
35#define _BHND_CORES_PMU_BHND_PMUVAR_H_
36
37#include <sys/types.h>
38#include <sys/rman.h>
39
40#include "bhnd_pmu.h"
41
42struct bhnd_pmu_query;
43struct bhnd_pmu_io;
44
45DECLARE_CLASS(bhnd_pmu_driver);
46extern devclass_t bhnd_pmu_devclass;
47
48int		bhnd_pmu_probe(device_t dev);
49int		bhnd_pmu_attach(device_t dev, struct bhnd_resource *res);
50int		bhnd_pmu_detach(device_t dev);
51int		bhnd_pmu_suspend(device_t dev);
52int		bhnd_pmu_resume(device_t dev);
53
54int		bhnd_pmu_query_init(struct bhnd_pmu_query *query, device_t dev,
55		    struct bhnd_chipid id, const struct bhnd_pmu_io *io,
56		    void *ctx);
57void		bhnd_pmu_query_fini(struct bhnd_pmu_query *query);
58
59uint32_t	bhnd_pmu_si_clock(struct bhnd_pmu_query *sc);
60uint32_t	bhnd_pmu_cpu_clock(struct bhnd_pmu_query *sc);
61uint32_t	bhnd_pmu_mem_clock(struct bhnd_pmu_query *sc);
62uint32_t	bhnd_pmu_alp_clock(struct bhnd_pmu_query *sc);
63uint32_t	bhnd_pmu_ilp_clock(struct bhnd_pmu_query *sc);
64
65/**
66 * PMU read-only query support.
67 *
68 * Provides support for querying PMU information prior to availability of
69 * the bhnd(4) bus.
70 */
71struct bhnd_pmu_query {
72	device_t			 dev;		/**< owning device, or NULL */
73	struct bhnd_chipid		 cid;		/**< chip identification */
74	uint32_t			 caps;		/**< pmu capability flags. */
75
76	const struct bhnd_pmu_io	*io;		/**< I/O operations */
77	void				*io_ctx;	/**< I/O callback context */
78
79	uint32_t			 ilp_cps;	/**< measured ILP cycles per second, or 0 */
80};
81
82/**
83 * PMU abstract I/O operations.
84 */
85struct bhnd_pmu_io {
86	/* Read 4 bytes from PMU @p reg */
87	uint32_t	(*rd4)(bus_size_t reg, void *ctx);
88
89	/* Read 4 bytes to PMU @p reg */
90	void		(*wr4)(bus_size_t reg, uint32_t val, void *ctx);
91
92	/* Read ChipCommon's CHIP_ST register */
93	uint32_t	(*rd_chipst)(void *ctx);
94};
95
96/**
97 * bhnd_pmu driver instance state.
98 */
99struct bhnd_pmu_softc {
100	device_t			 dev;
101	uint32_t			 caps;		/**< pmu capability flags. */
102	struct bhnd_chipid		 cid;		/**< chip identification */
103
104	struct bhnd_pmu_query		 query;		/**< query instance */
105
106	struct bhnd_board_info		 board;		/**< board identification */
107	device_t			 chipc_dev;	/**< chipcommon device */
108
109	struct bhnd_resource		*res;		/**< pmu register block. */
110	int				 rid;		/**< pmu register RID */
111	struct bhnd_core_clkctl		*clkctl;	/**< pmu clkctl register */
112
113	struct mtx			 mtx;		/**< state mutex */
114
115	/* For compatibility with bhnd_pmu_query APIs and the shared
116	 * BHND_PMU_(READ|WRITE) macros. */
117	const struct bhnd_pmu_io	*io;
118	void				*io_ctx;
119
120};
121
122#define	BPMU_LOCK_INIT(sc) \
123    mtx_init(&(sc)->mtx, device_get_nameunit((sc)->dev), NULL, MTX_DEF)
124#define	BPMU_LOCK(sc)			mtx_lock(&(sc)->mtx)
125#define	BPMU_UNLOCK(sc)			mtx_unlock(&(sc)->mtx)
126#define	BPMU_LOCK_ASSERT(sc, what)	mtx_assert(&(sc)->mtx, what)
127#define	BPMU_LOCK_DESTROY(sc)		mtx_destroy(&(sc)->mtx)
128
129#endif /* _BHND_CORES_PMU_BHND_PMUVAR_H_ */
130