1*68f185ccSZachary Leaf /*-
2*68f185ccSZachary Leaf * SPDX-License-Identifier: BSD-2-Clause
3*68f185ccSZachary Leaf *
4*68f185ccSZachary Leaf * Copyright (c) 2024 Arm Ltd
5*68f185ccSZachary Leaf * Copyright (c) 2022 The FreeBSD Foundation
6*68f185ccSZachary Leaf *
7*68f185ccSZachary Leaf * Portions of this software were developed by Andrew Turner under sponsorship
8*68f185ccSZachary Leaf * from the FreeBSD Foundation.
9*68f185ccSZachary Leaf *
10*68f185ccSZachary Leaf * Redistribution and use in source and binary forms, with or without
11*68f185ccSZachary Leaf * modification, are permitted provided that the following conditions
12*68f185ccSZachary Leaf * are met:
13*68f185ccSZachary Leaf * 1. Redistributions of source code must retain the above copyright
14*68f185ccSZachary Leaf * notice, this list of conditions and the following disclaimer.
15*68f185ccSZachary Leaf * 2. Redistributions in binary form must reproduce the above copyright
16*68f185ccSZachary Leaf * notice, this list of conditions and the following disclaimer in the
17*68f185ccSZachary Leaf * documentation and/or other materials provided with the distribution.
18*68f185ccSZachary Leaf *
19*68f185ccSZachary Leaf * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20*68f185ccSZachary Leaf * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21*68f185ccSZachary Leaf * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22*68f185ccSZachary Leaf * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23*68f185ccSZachary Leaf * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24*68f185ccSZachary Leaf * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25*68f185ccSZachary Leaf * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26*68f185ccSZachary Leaf * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27*68f185ccSZachary Leaf * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28*68f185ccSZachary Leaf * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29*68f185ccSZachary Leaf * SUCH DAMAGE.
30*68f185ccSZachary Leaf */
31*68f185ccSZachary Leaf
32*68f185ccSZachary Leaf #ifndef _ARM64_ARM_SPE_DEV_H_
33*68f185ccSZachary Leaf #define _ARM64_ARM_SPE_DEV_H_
34*68f185ccSZachary Leaf
35*68f185ccSZachary Leaf #include <sys/mutex.h>
36*68f185ccSZachary Leaf #include <sys/taskqueue.h>
37*68f185ccSZachary Leaf
38*68f185ccSZachary Leaf #include <vm/vm.h>
39*68f185ccSZachary Leaf
40*68f185ccSZachary Leaf #include <arm64/spe/arm_spe.h>
41*68f185ccSZachary Leaf
42*68f185ccSZachary Leaf #include <dev/hwt/hwt_context.h>
43*68f185ccSZachary Leaf
44*68f185ccSZachary Leaf #define ARM_SPE_DEBUG
45*68f185ccSZachary Leaf #undef ARM_SPE_DEBUG
46*68f185ccSZachary Leaf
47*68f185ccSZachary Leaf #ifdef ARM_SPE_DEBUG
48*68f185ccSZachary Leaf #define dprintf(fmt, ...) printf(fmt, ##__VA_ARGS__)
49*68f185ccSZachary Leaf #else
50*68f185ccSZachary Leaf #define dprintf(fmt, ...)
51*68f185ccSZachary Leaf #endif
52*68f185ccSZachary Leaf
53*68f185ccSZachary Leaf DECLARE_CLASS(arm_spe_driver);
54*68f185ccSZachary Leaf
55*68f185ccSZachary Leaf struct cdev;
56*68f185ccSZachary Leaf struct resource;
57*68f185ccSZachary Leaf
58*68f185ccSZachary Leaf extern bool arm64_pid_in_contextidr;
59*68f185ccSZachary Leaf
60*68f185ccSZachary Leaf int spe_register(device_t dev);
61*68f185ccSZachary Leaf void arm_spe_disable(void *arg __unused);
62*68f185ccSZachary Leaf int spe_backend_disable_smp(struct hwt_context *ctx);
63*68f185ccSZachary Leaf void arm_spe_send_buffer(void *arg, int pending __unused);
64*68f185ccSZachary Leaf
65*68f185ccSZachary Leaf /*
66*68f185ccSZachary Leaf PSB CSYNC is a Profiling Synchronization Barrier encoded in the hint space
67*68f185ccSZachary Leaf * so it is a NOP on earlier architecture.
68*68f185ccSZachary Leaf */
69*68f185ccSZachary Leaf #define psb_csync() __asm __volatile("hint #17" ::: "memory");
70*68f185ccSZachary Leaf
71*68f185ccSZachary Leaf struct arm_spe_softc {
72*68f185ccSZachary Leaf device_t dev;
73*68f185ccSZachary Leaf
74*68f185ccSZachary Leaf struct resource *sc_irq_res;
75*68f185ccSZachary Leaf void *sc_irq_cookie;
76*68f185ccSZachary Leaf struct cdev *sc_cdev;
77*68f185ccSZachary Leaf struct mtx sc_lock;
78*68f185ccSZachary Leaf struct task task;
79*68f185ccSZachary Leaf
80*68f185ccSZachary Leaf int64_t sc_pmsidr;
81*68f185ccSZachary Leaf int kqueue_fd;
82*68f185ccSZachary Leaf struct thread *hwt_td;
83*68f185ccSZachary Leaf struct arm_spe_info *spe_info;
84*68f185ccSZachary Leaf struct hwt_context *ctx;
85*68f185ccSZachary Leaf STAILQ_HEAD(, arm_spe_queue) pending;
86*68f185ccSZachary Leaf uint64_t npending;
87*68f185ccSZachary Leaf
88*68f185ccSZachary Leaf uint64_t pmbidr;
89*68f185ccSZachary Leaf uint64_t pmsidr;
90*68f185ccSZachary Leaf
91*68f185ccSZachary Leaf uint16_t kva_align;
92*68f185ccSZachary Leaf };
93*68f185ccSZachary Leaf
94*68f185ccSZachary Leaf struct arm_spe_buf_info {
95*68f185ccSZachary Leaf struct arm_spe_info *info;
96*68f185ccSZachary Leaf uint64_t pmbptr;
97*68f185ccSZachary Leaf uint8_t buf_idx : 1;
98*68f185ccSZachary Leaf bool buf_svc : 1;
99*68f185ccSZachary Leaf bool buf_wait : 1;
100*68f185ccSZachary Leaf bool partial_rec : 1;
101*68f185ccSZachary Leaf };
102*68f185ccSZachary Leaf
103*68f185ccSZachary Leaf struct arm_spe_info {
104*68f185ccSZachary Leaf int ident; /* tid or cpu_id */
105*68f185ccSZachary Leaf struct mtx lock;
106*68f185ccSZachary Leaf struct arm_spe_softc *sc;
107*68f185ccSZachary Leaf struct task task[2];
108*68f185ccSZachary Leaf bool enabled : 1;
109*68f185ccSZachary Leaf
110*68f185ccSZachary Leaf /* buffer is split in half as a ping-pong buffer */
111*68f185ccSZachary Leaf vm_object_t bufobj;
112*68f185ccSZachary Leaf vm_offset_t kvaddr;
113*68f185ccSZachary Leaf size_t buf_size;
114*68f185ccSZachary Leaf uint8_t buf_idx : 1; /* 0 = first half of buf, 1 = 2nd half */
115*68f185ccSZachary Leaf struct arm_spe_buf_info buf_info[2];
116*68f185ccSZachary Leaf
117*68f185ccSZachary Leaf /* config */
118*68f185ccSZachary Leaf enum arm_spe_profiling_level level;
119*68f185ccSZachary Leaf enum arm_spe_ctx_field ctx_field;
120*68f185ccSZachary Leaf /* filters */
121*68f185ccSZachary Leaf uint64_t pmsfcr;
122*68f185ccSZachary Leaf uint64_t pmsevfr;
123*68f185ccSZachary Leaf uint64_t pmslatfr;
124*68f185ccSZachary Leaf /* interval */
125*68f185ccSZachary Leaf uint64_t pmsirr;
126*68f185ccSZachary Leaf uint64_t pmsicr;
127*68f185ccSZachary Leaf /* control */
128*68f185ccSZachary Leaf uint64_t pmscr;
129*68f185ccSZachary Leaf };
130*68f185ccSZachary Leaf
131*68f185ccSZachary Leaf struct arm_spe_queue {
132*68f185ccSZachary Leaf int ident;
133*68f185ccSZachary Leaf u_int buf_idx : 1;
134*68f185ccSZachary Leaf bool partial_rec : 1;
135*68f185ccSZachary Leaf bool final_buf : 1;
136*68f185ccSZachary Leaf vm_offset_t offset;
137*68f185ccSZachary Leaf STAILQ_ENTRY(arm_spe_queue) next;
138*68f185ccSZachary Leaf };
139*68f185ccSZachary Leaf
buf_start_addr(u_int buf_idx,struct arm_spe_info * info)140*68f185ccSZachary Leaf static inline vm_offset_t buf_start_addr(u_int buf_idx, struct arm_spe_info *info)
141*68f185ccSZachary Leaf {
142*68f185ccSZachary Leaf vm_offset_t addr;
143*68f185ccSZachary Leaf if (buf_idx == 0)
144*68f185ccSZachary Leaf addr = info->kvaddr;
145*68f185ccSZachary Leaf if (buf_idx == 1)
146*68f185ccSZachary Leaf addr = info->kvaddr + (info->buf_size/2);
147*68f185ccSZachary Leaf
148*68f185ccSZachary Leaf return (addr);
149*68f185ccSZachary Leaf }
150*68f185ccSZachary Leaf
buf_end_addr(u_int buf_idx,struct arm_spe_info * info)151*68f185ccSZachary Leaf static inline vm_offset_t buf_end_addr(u_int buf_idx, struct arm_spe_info *info)
152*68f185ccSZachary Leaf {
153*68f185ccSZachary Leaf vm_offset_t addr;
154*68f185ccSZachary Leaf if (buf_idx == 0)
155*68f185ccSZachary Leaf addr = info->kvaddr + (info->buf_size/2);
156*68f185ccSZachary Leaf if (buf_idx == 1)
157*68f185ccSZachary Leaf addr = info->kvaddr + info->buf_size;
158*68f185ccSZachary Leaf
159*68f185ccSZachary Leaf return (addr);
160*68f185ccSZachary Leaf }
161*68f185ccSZachary Leaf
162*68f185ccSZachary Leaf #endif /* _ARM64_ARM_SPE_DEV_H_ */
163