interplane.Get Omega
Read in your language

aienux

Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.

Brought by drakestapleton

branch main Branches Tags History

aienux / native / kernel / tests / test_pi5_net.c

715 lines · 31807 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0
2 * Copyright 2026 Drake Stapleton (AIEN / aien-dev)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/* test_pi5_net.c -- Pi 5 network lane cut 2 (dev/pi5_net.c) over FAKE registers: the inbound
18 * window plan and its register sequence, the RP1 bus-master quiesce, the outbound window decode,
19 * the RP1 clock decisions, the PHY reset line sequence, the GEM MDIO read and the PHY ID decode.
20 * Hosted: this proves the decisions and the order of register writes, NOT the hardware. Run under
21 * ASan/UBSan and with each CK_NET_MUTANT_* (make pi5-net-mutants); every mutant must make it FAIL. */
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25#include "ck_test.h"
26#include "pi5_net.h"
27
28#define MiB (1ull << 20)
29#define GiB (1ull << 30)
30
31/* ===================== fake root complex ===================== */
32struct frc {
33 uint32_t regs[0xa000 / 4];
34 uint32_t cmd_status; /* RP1 config dword 1: status[31:16] | command[15:0] */
35 uint32_t cmd_stuck_bme; /* 1: BME bit ignores clears */
36 uint32_t hi_stuck; /* 1: window 0 HI register ignores writes */
37 uint32_t w3_stuck; /* 1: window 3 cannot be switched off */
38 uint32_t index;
39 /* write log for window 0 */
40 uint32_t nwr, order_violations;
41 uint32_t lo_writes[16], nlo;
42 uint32_t cfg_writes[8], ncfg;
43};
44static struct frc fr;
45
46static uint32_t fr_rd(void *c, uint32_t off)
47{
48 struct frc *f = c;
49 if (off == CK_PI5PCIE_EXT_CFG_INDEX)
50 return f->index;
51 if (off >= 0x8000 && off < 0x9000) {
52 if (f->index != 0x00100000u)
53 return 0xffffffffu;
54 if (off - 0x8000 == CK_PI5NET_PCI_COMMAND)
55 return f->cmd_status;
56 return 0;
57 }
58 return f->regs[off / 4];
59}
60
61static void fr_wr(void *c, uint32_t off, uint32_t v)
62{
63 struct frc *f = c;
64 f->nwr++;
65 if (off == CK_PI5PCIE_EXT_CFG_INDEX) {
66 f->index = v;
67 return;
68 }
69 if (off >= 0x8000 && off < 0x9000) {
70 if (f->index == 0x00100000u && off - 0x8000 == CK_PI5NET_PCI_COMMAND) {
71 uint32_t status = f->cmd_status >> 16;
72 if (f->ncfg < 8)
73 f->cfg_writes[f->ncfg] = v;
74 f->ncfg++;
75 status &= ~(v >> 16); /* RW1C */
76 f->cmd_status = (status << 16) | (v & 0xffffu);
77 if (f->cmd_stuck_bme)
78 f->cmd_status |= CK_PI5NET_CMD_BME;
79 }
80 return;
81 }
82 if (off == CK_PI5NET_IB0_LO) {
83 if (f->nlo < 16)
84 f->lo_writes[f->nlo] = v;
85 f->nlo++;
86 }
87 if (off == CK_PI5NET_IB0_HI || off == CK_PI5NET_IB0_REMAP_LO || off == CK_PI5NET_IB0_REMAP_HI) {
88 if (f->regs[CK_PI5NET_IB0_LO / 4] & 0x1fu)
89 f->order_violations++; /* a half of an ENABLED window changed under traffic */
90 if (off == CK_PI5NET_IB0_HI && f->hi_stuck)
91 return;
92 if (off == CK_PI5NET_IB0_REMAP_LO)
93 v &= 0xfffff001u;
94 if (off == CK_PI5NET_IB0_REMAP_HI)
95 v &= 0xffu;
96 }
97 if (off == 0x40d4u + 2 * 8u && f->w3_stuck) /* window 3 */
98 return;
99 f->regs[off / 4] = v;
100}
101static struct ck_pi5pcie_io fio = {fr_rd, fr_wr, &fr};
102
103static void fr_fresh(void)
104{
105 memset(&fr, 0, sizeof fr);
106 fr.cmd_status = (0x0010u << 16) | 0x0006u; /* status: capability list; command: MEM + BME */
107 /* the firmware's window 0: bus 0 -> cpu 0, 64 GiB (size code 21), remap on */
108 fr.regs[CK_PI5NET_IB0_LO / 4] = 21;
109 fr.regs[CK_PI5NET_IB0_HI / 4] = 0;
110 fr.regs[CK_PI5NET_IB0_REMAP_LO / 4] = 1;
111 fr.regs[CK_PI5NET_IB0_REMAP_HI / 4] = 0;
112}
113
114static void t_size_code(void)
115{
116 CHECK(ck_pi5net_ib_size_code(4096) == 0x1c);
117 CHECK(ck_pi5net_ib_size_code(32768) == 0x1f);
118 CHECK(ck_pi5net_ib_size_code(65536) == 1);
119 CHECK(ck_pi5net_ib_size_code(2 * MiB) == 6);
120 CHECK(ck_pi5net_ib_size_code(64 * GiB) == 21);
121 CHECK(ck_pi5net_ib_size_code(0) == 0);
122 CHECK(ck_pi5net_ib_size_code(3 * MiB) == 0); /* not a power of two */
123 CHECK(ck_pi5net_ib_size_code(2048) == 0); /* below 4 KiB */
124 CHECK(ck_pi5net_ib_size_code(128 * GiB) == 0); /* above 64 GiB */
125 /* agrees with the decoder cut 1 uses to read the registers back */
126 for (uint32_t lg = 12; lg <= 36; lg++) {
127 uint64_t sz = 0;
128 CHECK(ck_pi5pcie_ib_size_decode(ck_pi5net_ib_size_code(1ull << lg), &sz) == 1 && sz == (1ull << lg));
129 }
130}
131
132static void t_plan(void)
133{
134 struct ck_pi5net_ibprog p;
135 /* the real Pi run: bounce cpu 0x4400000 size 0x200000 */
136 CHECK(ck_pi5net_ib_plan(0x4400000ull, 0x200000ull, &p) == 0);
137 CHECK(p.bus_base == 0x4400000ull && p.cpu_base == 0x4400000ull && p.size == 0x200000ull);
138 CHECK(p.size_code == 6 && p.extra_bytes == 0);
139 CHECK(p.lo == 0x04400006u && p.hi == 0 && p.remap_lo == 0x04400001u && p.remap_hi == 0);
140 /* not a power of two: rounds up, the extra is reported */
141 CHECK(ck_pi5net_ib_plan(0x4400000ull, 0x180000ull, &p) == 0 && p.size == 0x200000ull && p.extra_bytes == 0x80000ull);
142 /* a region that straddles an aligned block of its own size must get the next size up */
143 CHECK(ck_pi5net_ib_plan(0x4500000ull, 0x200000ull, &p) == 0);
144 CHECK(p.size == 0x400000ull && p.cpu_base == 0x4400000ull && p.extra_bytes == 0x200000ull);
145 CHECK(p.cpu_base <= 0x4500000ull && p.cpu_base + p.size >= 0x4700000ull);
146 /* high address: the upper halves carry the bits above 32 */
147 CHECK(ck_pi5net_ib_plan(0x880000000ull, 0x200000ull, &p) == 0);
148 CHECK(p.hi == 8 && p.remap_hi == 8 && p.lo == (0x80000000u | 6) && p.remap_lo == 0x80000001u);
149 /* small region: 4 KiB window */
150 CHECK(ck_pi5net_ib_plan(0x10000ull, 0x1000ull, &p) == 0 && p.size == 0x1000ull && p.size_code == 0x1c);
151 /* refusals */
152 CHECK(ck_pi5net_ib_plan(0x4400000ull, 0, &p) == CK_PI5NET_E_ARG);
153 CHECK(ck_pi5net_ib_plan(UINT64_MAX - 10, 0x1000, &p) == CK_PI5NET_E_ARG);
154 CHECK(ck_pi5net_ib_plan(0, 128 * GiB, &p) == CK_PI5NET_E_RANGE);
155 CHECK(ck_pi5net_ib_plan((1ull << 40) - 0x1000, 0x2000, &p) == CK_PI5NET_E_RANGE); /* passes 40 bits */
156 CHECK(ck_pi5net_ib_plan(0x4400000ull, 0x200000ull, NULL) == CK_PI5NET_E_ARG);
157 /* property: the window always holds the region, is power-of-two sized, aligned, and minimal */
158 srand(12345);
159 for (int i = 0; i < 20000; i++) {
160 uint64_t b = ((uint64_t)rand() << 12) % (1ull << 38);
161 uint64_t s = ((uint64_t)(rand() % 0x400) + 1) << (12 + rand() % 14);
162 if (ck_pi5net_ib_plan(b, s, &p) != 0)
163 continue;
164 CHECK(p.size && (p.size & (p.size - 1)) == 0 && (p.cpu_base & (p.size - 1)) == 0);
165 CHECK(p.cpu_base <= b && p.cpu_base + p.size >= b + s);
166 CHECK(p.extra_bytes == p.size - s);
167 if (p.size > 4096) { /* minimality: the half-size aligned block around b does not hold the region */
168 uint64_t h = p.size / 2, hb = b & ~(h - 1);
169 CHECK(hb + h < b + s);
170 }
171 CHECK(p.bus_base == p.cpu_base);
172 }
173}
174
175static void t_plan_then_registers_then_verdict(void)
176{
177 struct ck_pi5net_ibprog p;
178 struct ck_pi5net_ibsaved sv;
179 struct ck_pi5pcie_ibwin w[CK_PI5PCIE_IB_WINDOWS];
180 struct ck_pi5pcie_dmav dv;
181 uint32_t nd = 99;
182 fr_fresh();
183 for (uint32_t i = 0; i < CK_PI5PCIE_IB_WINDOWS; i++)
184 CHECK(ck_pi5pcie_ib_read(&fio, i, &w[i]) == 0);
185 CHECK(ck_pi5pcie_ib_verdict(w, CK_PI5PCIE_IB_WINDOWS, 0, 0x4400000ull, 0x200000ull, &dv) == 0);
186 CHECK(dv.verdict == CK_PI5PCIE_DMA_WIDER && dv.reach_bytes == 64 * GiB); /* the cut 1 result */
187 CHECK(ck_pi5net_confine_label(&dv, NULL) == CK_PI5NET_CONF_NOT_CONFINED);
188 CHECK(ck_pi5net_ib_plan(0x4400000ull, 0x200000ull, &p) == 0);
189 CHECK(ck_pi5net_ib_apply(&fio, &p, &sv, &nd) == 0 && nd == 0);
190 for (uint32_t i = 0; i < CK_PI5PCIE_IB_WINDOWS; i++)
191 CHECK(ck_pi5pcie_ib_read(&fio, i, &w[i]) == 0);
192 CHECK(w[0].enabled && w[0].size_valid && w[0].remap_en && w[0].size == 0x200000ull);
193 CHECK(w[0].bus_base == 0x4400000ull && w[0].cpu_base == 0x4400000ull);
194 CHECK(ck_pi5pcie_ib_verdict(w, CK_PI5PCIE_IB_WINDOWS, 0, 0x4400000ull, 0x200000ull, &dv) == 0);
195 CHECK(dv.verdict == CK_PI5PCIE_DMA_CONFINED && dv.reach_bytes == 0x200000ull && dv.outside_bytes == 0);
196 CHECK(ck_pi5net_confine_label(&dv, &p) == CK_PI5NET_CONF_CONFINED);
197 /* a region that needed a bigger block: PARTIAL with exactly the plan's extra, not CONFINED */
198 fr_fresh();
199 CHECK(ck_pi5net_ib_plan(0x4500000ull, 0x200000ull, &p) == 0);
200 CHECK(ck_pi5net_ib_apply(&fio, &p, &sv, &nd) == 0);
201 for (uint32_t i = 0; i < CK_PI5PCIE_IB_WINDOWS; i++)
202 CHECK(ck_pi5pcie_ib_read(&fio, i, &w[i]) == 0);
203 CHECK(ck_pi5pcie_ib_verdict(w, CK_PI5PCIE_IB_WINDOWS, 0, 0x4500000ull, 0x200000ull, &dv) == 0);
204 CHECK(dv.verdict == CK_PI5PCIE_DMA_WIDER && dv.outside_bytes == 0x200000ull && dv.inside_bytes == 0x200000ull);
205 CHECK(ck_pi5net_confine_label(&dv, &p) == CK_PI5NET_CONF_PARTIAL);
206 CHECK(ck_pi5net_confine_label(&dv, NULL) == CK_PI5NET_CONF_NOT_CONFINED); /* without the plan it is just wide */
207 /* the other verdicts map to BROKEN / UNKNOWN */
208 dv.verdict = CK_PI5PCIE_DMA_MISS;
209 CHECK(ck_pi5net_confine_label(&dv, &p) == CK_PI5NET_CONF_BROKEN);
210 dv.verdict = CK_PI5PCIE_DMA_UNKNOWN;
211 CHECK(ck_pi5net_confine_label(&dv, &p) == CK_PI5NET_CONF_UNKNOWN);
212 CHECK(strcmp(ck_pi5net_confine_name(CK_PI5NET_CONF_PARTIAL), "PARTIAL") == 0);
213 CHECK(strcmp(ck_pi5net_confine_name(CK_PI5NET_CONF_CONFINED), "CONFINED") == 0);
214}
215
216static void t_apply_order(void)
217{
218 struct ck_pi5net_ibprog p;
219 struct ck_pi5net_ibsaved sv;
220 uint32_t nd = 0;
221 fr_fresh();
222 CHECK(ck_pi5net_ib_plan(0x4400000ull, 0x200000ull, &p) == 0);
223 CHECK(ck_pi5net_ib_apply(&fio, &p, &sv, &nd) == 0);
224 CHECK(fr.nlo == 2); /* off, then on */
225 CHECK((fr.lo_writes[0] & 0x1f) == 0); /* window is OFF before its halves change */
226 CHECK(fr.lo_writes[fr.nlo - 1] == p.lo); /* the size code (enable) is the last write to LO */
227 CHECK(fr.order_violations == 0);
228 CHECK(sv.lo == 21 && sv.hi == 0 && sv.remap_lo == 1); /* firmware window saved for the restore */
229 /* other enabled windows are switched off */
230 fr_fresh();
231 fr.regs[(0x40d4u) / 4] = 0x10000000u | 6; /* window 1 */
232 fr.regs[(0x40d4u + 3 * 8u) / 4] = 0x20000000u | 1; /* window 4 */
233 CHECK(ck_pi5net_ib_apply(&fio, &p, &sv, &nd) == 0 && nd == 2);
234 CHECK((fr.regs[0x40d4u / 4] & 0x1f) == 0 && (fr.regs[(0x40d4u + 24u) / 4] & 0x1f) == 0);
235 CHECK(fr.regs[0x40d4u / 4] >> 5 == 0x10000000u >> 5); /* base bits kept, only the size code cleared */
236}
237
238static void t_apply_failure_restores(void)
239{
240 struct ck_pi5net_ibprog p;
241 struct ck_pi5net_ibsaved sv;
242 uint32_t nd = 0;
243 CHECK(ck_pi5net_ib_plan(0x4400000ull, 0x200000ull, &p) == 0);
244 /* hardware ignores the HI write: readback does not match, firmware window is put back */
245 fr_fresh();
246 fr.hi_stuck = 1;
247 /* make the plan need a non-zero HI so the stuck register matters */
248 CHECK(ck_pi5net_ib_plan(0x880000000ull, 0x200000ull, &p) == 0);
249 CHECK(ck_pi5net_ib_apply(&fio, &p, &sv, &nd) == CK_PI5NET_E_READBACK);
250 CHECK(fr.regs[CK_PI5NET_IB0_LO / 4] == 21 && fr.regs[CK_PI5NET_IB0_HI / 4] == 0 &&
251 fr.regs[CK_PI5NET_IB0_REMAP_LO / 4] == 1 && fr.regs[CK_PI5NET_IB0_REMAP_HI / 4] == 0);
252 /* another window that refuses to switch off: not confined, so refuse and restore */
253 fr_fresh();
254 fr.regs[(0x40d4u + 2 * 8u) / 4] = 0x30000000u | 7; /* window 3 */
255 fr.w3_stuck = 1;
256 CHECK(ck_pi5net_ib_plan(0x4400000ull, 0x200000ull, &p) == 0);
257 CHECK(ck_pi5net_ib_apply(&fio, &p, &sv, &nd) == CK_PI5NET_E_READBACK);
258 CHECK(fr.regs[CK_PI5NET_IB0_LO / 4] == 21);
259 /* arguments */
260 CHECK(ck_pi5net_ib_apply(NULL, &p, &sv, &nd) == CK_PI5NET_E_ARG);
261 memset(&p, 0, sizeof p);
262 CHECK(ck_pi5net_ib_apply(&fio, &p, &sv, &nd) == CK_PI5NET_E_ARG); /* a plan with no size code */
263}
264
265static void t_quiesce(void)
266{
267 uint16_t saved = 0xdead;
268 fr_fresh();
269 CHECK(ck_pi5net_rp1_quiesce(&fio, &saved) == 0 && saved == 0x0006);
270 CHECK((fr.cmd_status & 0xffffu) == 0x0002); /* BME off, MEM kept */
271 CHECK((fr.cmd_status >> 16) == 0x0010); /* status bits not cleared by writing zeros */
272 CHECK(fr.ncfg == 1 && (fr.cfg_writes[0] >> 16) == 0); /* the write carried no status ones */
273 CHECK(fr.index == 0x00100000u);
274 fr_fresh();
275 fr.cmd_stuck_bme = 1;
276 CHECK(ck_pi5net_rp1_quiesce(&fio, &saved) == CK_PI5NET_E_QUIESCE);
277 CHECK(ck_pi5net_rp1_quiesce(&fio, NULL) == CK_PI5NET_E_ARG);
278 /* config write addressing */
279 fr_fresh();
280 CHECK(ck_pi5net_cfg_write32(&fio, 1, 0, 0, 4, 0x2) == 0);
281 CHECK(ck_pi5net_cfg_write32(&fio, 2, 0, 0, 4, 0) == CK_PI5NET_E_ARG);
282 CHECK(ck_pi5net_cfg_write32(&fio, 1, 1, 0, 4, 0) == CK_PI5NET_E_ARG);
283 CHECK(ck_pi5net_cfg_write32(&fio, 1, 0, 0, 5, 0) == CK_PI5NET_E_ARG);
284 CHECK(ck_pi5net_cfg_write32(&fio, 1, 0, 0, 4096, 0) == CK_PI5NET_E_ARG);
285 CHECK(ck_pi5net_cfg_write32(&fio, 0, 1, 0, 4, 0) == CK_PI5NET_E_ARG);
286}
287
288static void t_outbound(void)
289{
290 struct ck_pi5net_obwin w;
291 /* the values edk2 PcieSetupOutboundWindow writes for CPU 0x1f00000000, 1 GiB, bus 0xc0000000 */
292 uint64_t cpu_mb = 0x1f00000000ull / MiB, lim_mb = (0x1f00000000ull + GiB - 1) / MiB;
293 fr_fresh();
294 fr.regs[CK_PI5NET_OB0_BASE_LIMIT / 4] = (uint32_t)((cpu_mb << 4) | (lim_mb << 20));
295 fr.regs[CK_PI5NET_OB0_BASE_HI / 4] = (uint32_t)(cpu_mb >> 12);
296 fr.regs[CK_PI5NET_OB0_LIMIT_HI / 4] = (uint32_t)(lim_mb >> 12);
297 fr.regs[CK_PI5NET_OB0_BUS_LO / 4] = 0xc0000000u;
298 fr.regs[CK_PI5NET_OB0_BUS_HI / 4] = 0;
299 CHECK(ck_pi5net_ob_read(&fio, &w) == 0 && w.valid);
300 CHECK(w.cpu_base == 0x1f00000000ull && w.bus_base == 0xc0000000ull && w.size == GiB);
301 CHECK(ck_pi5net_bus_to_cpu(&w, 0xc0000000ull, 0x108000) == 0x1f00000000ull);
302 CHECK(ck_pi5net_bus_to_cpu(&w, 0xc0400000ull, 0x10000) == 0x1f00400000ull);
303 CHECK(ck_pi5net_bus_to_cpu(&w, 0xbfffffffull, 0x10) == 0); /* below the window */
304 CHECK(ck_pi5net_bus_to_cpu(&w, 0xc0000000ull + GiB - 0x10, 0x20) == 0); /* runs past the end */
305 CHECK(ck_pi5net_bus_to_cpu(&w, 0xc0000000ull + GiB - 0x10, 0x10) != 0); /* exactly to the end */
306 CHECK(ck_pi5net_bus_to_cpu(&w, 0xc0000000ull, 2 * GiB) == 0); /* longer than the window */
307 CHECK(ck_pi5net_bus_to_cpu(&w, 0xc0000000ull, 0) == 0);
308 CHECK(ck_pi5net_bus_to_cpu(NULL, 0xc0000000ull, 0x10) == 0);
309 /* limit below base: not valid */
310 fr.regs[CK_PI5NET_OB0_BASE_LIMIT / 4] = (uint32_t)((cpu_mb << 4) | ((lim_mb - 0x1000) << 20));
311 fr.regs[CK_PI5NET_OB0_LIMIT_HI / 4] = 0x1e;
312 CHECK(ck_pi5net_ob_read(&fio, &w) == 0 && !w.valid);
313 CHECK(ck_pi5net_bus_to_cpu(&w, 0xc0000000ull, 0x10) == 0);
314}
315
316/* ===================== fake RP1 BAR1 ===================== */
317#define NOFF 0x108000u
318struct frp {
319 uint32_t r[NOFF / 4];
320 uint32_t wr_count[NOFF / 4];
321 uint64_t t_us; /* fake time */
322 uint32_t order[256], nord;
323 int enable_sticks; /* clock ENABLE bit holds */
324 /* GPIO32 pin model */
325 int pin_forced; /* 1: ext fault, pin reads 0 whatever we drive */
326 uint64_t low_us; /* total time the pin was driven low */
327 int driven_low_before_oe, drove_low;
328 uint64_t low_since;
329 int low_now;
330 /* GEM / MDIO model */
331 uint32_t gem_busy_polls; /* idle stays 0 for this many NSR reads after a MAN write */
332 uint32_t busy_left;
333 int phy_addr; /* -1 none */
334 uint32_t phy_ready_polls; /* PHY returns 0xffff until this many reads happened */
335 uint32_t phy_reads;
336 uint16_t phy_regs[32];
337 int never_idle;
338 int bad_frame_seen;
339 uint32_t last_man;
340 int stuck_ncfg;
341};
342static struct frp fp;
343
344static int pin_level(void)
345{
346 uint32_t oe = fp.r[CK_RP1_RIO1_OE / 4] & CK_RP1_GPIO_PIN_BIT, out = fp.r[CK_RP1_RIO1_OUT / 4] & CK_RP1_GPIO_PIN_BIT;
347 if (fp.pin_forced)
348 return 0;
349 if (oe)
350 return out ? 1 : 0;
351 return 1; /* pulled up */
352}
353
354static void pin_track(void)
355{
356 int lv = pin_level();
357 if (!lv && !fp.low_now) {
358 fp.low_now = 1;
359 fp.low_since = fp.t_us;
360 fp.drove_low = 1;
361 if (!(fp.r[CK_RP1_RIO1_OE / 4] & CK_RP1_GPIO_PIN_BIT))
362 fp.driven_low_before_oe = 1;
363 } else if (lv && fp.low_now) {
364 fp.low_now = 0;
365 fp.low_us += fp.t_us - fp.low_since;
366 }
367}
368
369static uint32_t rp_rd(void *c, uint32_t off)
370{
371 (void)c;
372 if (off == CK_RP1_RIO1_IN)
373 return pin_level() ? CK_RP1_GPIO_PIN_BIT : 0;
374 if (off == CK_RP1_ETH + CK_GEM_NSR) {
375 if (fp.never_idle)
376 return 0;
377 if (fp.busy_left) {
378 fp.busy_left--;
379 return 0;
380 }
381 return CK_GEM_NSR_IDLE;
382 }
383 if (off == CK_RP1_ETH + CK_GEM_MAN) {
384 /* stale until the transfer finished; the model keeps the data only for a correct read frame */
385 if (fp.busy_left)
386 return (fp.r[off / 4] & 0xffff0000u) | 0xdeadu;
387 return fp.r[off / 4];
388 }
389 return fp.r[off / 4];
390}
391
392static void rp_wr(void *c, uint32_t off, uint32_t v)
393{
394 (void)c;
395 if (fp.nord < 256)
396 fp.order[fp.nord++] = off;
397 fp.wr_count[off / 4]++;
398 if (off >= CK_RP1_RIO1_OUT + CK_RP1_ATOMIC_SET && off < CK_RP1_RIO1_OUT + CK_RP1_ATOMIC_SET + 0x10) {
399 uint32_t base = off - CK_RP1_ATOMIC_SET;
400 fp.r[base / 4] |= v;
401 pin_track();
402 return;
403 }
404 if (off >= CK_RP1_RIO1_OUT + CK_RP1_ATOMIC_CLR && off < CK_RP1_RIO1_OUT + CK_RP1_ATOMIC_CLR + 0x10) {
405 uint32_t base = off - CK_RP1_ATOMIC_CLR;
406 fp.r[base / 4] &= ~v;
407 pin_track();
408 return;
409 }
410 if (off == CK_RP1_CLOCKS + CK_RP1_CLK_ETH_CTRL || off == CK_RP1_CLOCKS + CK_RP1_CLK_TSU_CTRL) {
411 if (!fp.enable_sticks)
412 v &= ~(uint32_t)CK_RP1_CLK_ENABLE;
413 }
414 if (off == CK_RP1_ETH + CK_GEM_NCFGR && fp.stuck_ncfg)
415 return;
416 if (off == CK_RP1_ETH + CK_GEM_MAN) {
417 uint32_t phy = (v >> 23) & 31u, reg = (v >> 18) & 31u;
418 fp.last_man = v;
419 if ((v >> 30) != 1 || ((v >> 28) & 3u) != 2 || ((v >> 16) & 3u) != 2) {
420 fp.bad_frame_seen = 1; /* not a clause 22 read: the model returns nothing useful */
421 fp.r[off / 4] = v & 0xffff0000u;
422 } else {
423 uint16_t d = 0xffff;
424 if ((int)phy == fp.phy_addr) {
425 fp.phy_reads++;
426 if (fp.phy_reads > fp.phy_ready_polls)
427 d = fp.phy_regs[reg];
428 }
429 fp.r[off / 4] = (v & 0xffff0000u) | d;
430 }
431 fp.busy_left = fp.gem_busy_polls;
432 return;
433 }
434 fp.r[off / 4] = v;
435 if (off == CK_RP1_GPIO32_PAD || off == CK_RP1_GPIO32_CTRL)
436 pin_track();
437}
438
439static void rp_delay(void *c, uint32_t us)
440{
441 (void)c;
442 fp.t_us += us;
443}
444static struct ck_rp1_io rio = {rp_rd, rp_wr, rp_delay, &fp};
445
446static void rp_fresh(void)
447{
448 memset(&fp, 0, sizeof fp);
449 fp.phy_addr = -1;
450 /* clock block as left by a cold boot: PLL locked, secondary running, ETH/TSU off */
451 fp.r[(CK_RP1_PLL_SYS + CK_RP1_PLL_CS) / 4] = 0x80000001u;
452 fp.r[(CK_RP1_PLL_SYS + CK_RP1_PLL_FBDIV_INT) / 4] = 20;
453 fp.r[(CK_RP1_PLL_SYS + CK_RP1_PLL_PRIM) / 4] = (5u << 16) | (1u << 12);
454 fp.r[(CK_RP1_PLL_SYS + CK_RP1_PLL_SEC) / 4] = 0x80000000u | (8u << 8);
455 fp.r[(CK_RP1_CLOCKS + CK_RP1_CLK_SYS_CTRL) / 4] = 2;
456 fp.r[(CK_RP1_CLOCKS + CK_RP1_CLK_SYS_DIV) / 4] = 1;
457 fp.r[(CK_RP1_CLOCKS + CK_RP1_CLK_SYS_SEL) / 4] = 4;
458 fp.enable_sticks = 1;
459}
460
461static void t_clock_rates(void)
462{
463 struct ck_rp1_clk_regs r;
464 rp_fresh();
465 ck_rp1_clk_read(&rio, &r);
466 CHECK(ck_rp1_pll_core_hz(&r) == 1000000000u);
467 CHECK(ck_rp1_pll_sec_hz(&r) == 125000000u);
468 CHECK(ck_rp1_pll_pri_hz(&r) == 200000000u);
469 CHECK(ck_rp1_clk_sys_hz(&r) == 200000000u);
470 CHECK(ck_rp1_clk_eth_hz(&r) == 0 && ck_rp1_clk_tsu_hz(&r) == 0);
471 r.pll_cs = 1; /* unlocked */
472 CHECK(ck_rp1_pll_core_hz(&r) == 0 && ck_rp1_pll_sec_hz(&r) == 0 && ck_rp1_pll_pri_hz(&r) == 0);
473 rp_fresh();
474 ck_rp1_clk_read(&rio, &r);
475 r.pll_sec |= 1u << 16; /* divider held in reset */
476 CHECK(ck_rp1_pll_sec_hz(&r) == 0);
477 r.pll_sec = 0x80000000u | (12u << 8); /* divide by 12 */
478 CHECK(ck_rp1_pll_sec_hz(&r) == 83333334u);
479 r.pll_sec = 0x80000000u | (3u << 8); /* invalid field value defaults to 19 */
480 CHECK(ck_rp1_pll_sec_hz(&r) == 52631579u);
481 r.pll_sec = (8u << 8); /* divider not implemented (IMPL bit clear) */
482 CHECK(ck_rp1_pll_sec_hz(&r) == 0);
483 /* clk_sys on xosc: sel bit 0 */
484 ck_rp1_clk_read(&rio, &r);
485 r.sys_sel = 1;
486 CHECK(ck_rp1_clk_sys_hz(&r) == 50000000u);
487}
488
489static void t_clock_decide(void)
490{
491 struct ck_rp1_clk_regs r;
492 struct ck_rp1_clk_plan p;
493 rp_fresh();
494 ck_rp1_clk_read(&rio, &r);
495 ck_rp1_clk_decide(&r, &p);
496 CHECK(p.eth == CK_RP1_CLK_TO_ENABLE && p.eth_div == 1 && (p.eth_ctrl & CK_RP1_CLK_ENABLE) &&
497 (p.eth_ctrl & CK_RP1_CLK_AUXSRC_MASK) == 0);
498 CHECK(p.tsu == CK_RP1_CLK_TO_ENABLE && p.tsu_div == 1 && (p.tsu_ctrl & CK_RP1_CLK_ENABLE));
499 /* a clock that is already on is left exactly as found, even at an odd rate */
500 r.eth_ctrl = CK_RP1_CLK_ENABLE | (1u << CK_RP1_CLK_AUXSRC_SHIFT);
501 r.eth_div = 2;
502 ck_rp1_clk_decide(&r, &p);
503 CHECK(p.eth == CK_RP1_CLK_ALREADY_ON && p.eth_div == 0 && p.eth_ctrl == 0);
504 CHECK(ck_rp1_clk_eth_hz(&r) == 100000000u); /* pll_sys (200 MHz) / 2: reported, not changed */
505 r.tsu_ctrl = CK_RP1_CLK_ENABLE;
506 r.tsu_div = 1;
507 ck_rp1_clk_decide(&r, &p);
508 CHECK(p.tsu == CK_RP1_CLK_ALREADY_ON && ck_rp1_clk_tsu_hz(&r) == 50000000u);
509 /* PLL not locked: ETH blocked, TSU (from xosc) is not */
510 ck_rp1_clk_read(&rio, &r);
511 r.pll_cs = 1;
512 ck_rp1_clk_decide(&r, &p);
513 CHECK(p.eth == CK_RP1_CLK_BLOCKED && p.tsu == CK_RP1_CLK_TO_ENABLE);
514 /* secondary in reset */
515 ck_rp1_clk_read(&rio, &r);
516 r.pll_sec |= 1u << 16;
517 ck_rp1_clk_decide(&r, &p);
518 CHECK(p.eth == CK_RP1_CLK_BLOCKED);
519 /* secondary not an exact multiple of 125 MHz (1000/9): refuse rather than run ETH at a wrong rate */
520 ck_rp1_clk_read(&rio, &r);
521 r.pll_sec = 0x80000000u | (9u << 8);
522 ck_rp1_clk_decide(&r, &p);
523 CHECK(p.eth == CK_RP1_CLK_BLOCKED);
524 ck_rp1_clk_read(&rio, &r);
525 r.pll_fbdiv_int = 30; /* core 1.5 GHz, sec 187.5 MHz: ratio 1 but not exact */
526 ck_rp1_clk_decide(&r, &p);
527 CHECK(p.eth == CK_RP1_CLK_BLOCKED);
528 /* a faster secondary that divides exactly: divider = ratio (1000 MHz core / 8 = 125; make core 2000/8 = 250 -> 2) */
529 ck_rp1_clk_read(&rio, &r);
530 r.pll_fbdiv_int = 40;
531 ck_rp1_clk_decide(&r, &p);
532 CHECK(p.eth == CK_RP1_CLK_TO_ENABLE && p.eth_div == 2);
533 /* and a secondary slower than 125 MHz is refused (ratio 0) */
534 ck_rp1_clk_read(&rio, &r);
535 r.pll_sec = 0x80000000u | (16u << 8); /* 1000/16 = 62.5 MHz */
536 ck_rp1_clk_decide(&r, &p);
537 CHECK(p.eth == CK_RP1_CLK_BLOCKED);
538}
539
540static void t_clock_apply(void)
541{
542 struct ck_rp1_clk_regs r;
543 struct ck_rp1_clk_plan p;
544 uint32_t eth_ctrl = (CK_RP1_CLOCKS + CK_RP1_CLK_ETH_CTRL) / 4, eth_div = (CK_RP1_CLOCKS + CK_RP1_CLK_ETH_DIV) / 4;
545 uint32_t tsu_ctrl = (CK_RP1_CLOCKS + CK_RP1_CLK_TSU_CTRL) / 4;
546 rp_fresh();
547 ck_rp1_clk_read(&rio, &r);
548 ck_rp1_clk_decide(&r, &p);
549 CHECK(ck_rp1_clk_apply(&rio, &p) == 0);
550 ck_rp1_clk_read(&rio, &r);
551 CHECK(ck_rp1_clk_eth_hz(&r) == 125000000u && ck_rp1_clk_tsu_hz(&r) == 50000000u);
552 /* order: divider, then source with the clock still off, then the enable */
553 CHECK(fp.wr_count[eth_div] == 1 && fp.wr_count[eth_ctrl] == 2);
554 {
555 int i_div = -1, i_c1 = -1, i_c2 = -1;
556 for (uint32_t i = 0; i < fp.nord; i++) {
557 if (fp.order[i] == CK_RP1_CLOCKS + CK_RP1_CLK_ETH_DIV)
558 i_div = (int)i;
559 if (fp.order[i] == CK_RP1_CLOCKS + CK_RP1_CLK_ETH_CTRL) {
560 if (i_c1 < 0)
561 i_c1 = (int)i;
562 else
563 i_c2 = (int)i;
564 }
565 }
566 CHECK(i_div >= 0 && i_div < i_c1 && i_c1 < i_c2);
567 }
568 /* already-on clocks get no write at all */
569 rp_fresh();
570 fp.r[eth_ctrl] = CK_RP1_CLK_ENABLE;
571 fp.r[eth_div] = 1;
572 fp.r[tsu_ctrl] = CK_RP1_CLK_ENABLE | 0x20;
573 fp.r[(CK_RP1_CLOCKS + CK_RP1_CLK_TSU_DIV) / 4] = 1;
574 ck_rp1_clk_read(&rio, &r);
575 ck_rp1_clk_decide(&r, &p);
576 fp.nord = 0;
577 memset(fp.wr_count, 0, sizeof fp.wr_count);
578 CHECK(ck_rp1_clk_apply(&rio, &p) == 0);
579 CHECK(fp.nord == 0 && fp.r[eth_ctrl] == CK_RP1_CLK_ENABLE);
580 /* enable does not stick: reported */
581 rp_fresh();
582 fp.enable_sticks = 0;
583 ck_rp1_clk_read(&rio, &r);
584 ck_rp1_clk_decide(&r, &p);
585 CHECK(ck_rp1_clk_apply(&rio, &p) == CK_PI5NET_E_READBACK);
586}
587
588static void t_phy_reset(void)
589{
590 struct ck_rp1_phyreset pr;
591 rp_fresh();
592 fp.r[CK_RP1_GPIO32_CTRL / 4] = 0x1f | (1u << 20); /* NONE, with an interrupt-enable bit set */
593 fp.r[CK_RP1_GPIO32_PAD / 4] = CK_RP1_PAD_OUT_DISABLE | 0x1;
594 CHECK(ck_rp1_phy_reset(&rio, &pr) == 0);
595 CHECK(fp.drove_low == 1); /* reset was really asserted */
596 CHECK(fp.driven_low_before_oe == 0); /* the pin never went low before we owned it */
597 CHECK(fp.low_us >= (uint64_t)CK_RP1_PHY_RESET_MS * 1000u); /* held for the dts reset time */
598 CHECK(fp.low_us < 1000u * 1000u);
599 CHECK(fp.t_us >= (uint64_t)(CK_RP1_PHY_RESET_MS + CK_RP1_PHY_SETTLE_MS) * 1000u);
600 CHECK(pin_level() == 1); /* released at the end */
601 CHECK((fp.r[CK_RP1_RIO1_OE / 4] & CK_RP1_GPIO_PIN_BIT) != 0);
602 CHECK((fp.r[CK_RP1_GPIO32_CTRL / 4] & 0x1f) == CK_RP1_FSEL_GPIO && (fp.r[CK_RP1_GPIO32_CTRL / 4] & (1u << 20)));
603 CHECK((fp.r[CK_RP1_GPIO32_PAD / 4] & CK_RP1_PAD_IN_ENABLE) && !(fp.r[CK_RP1_GPIO32_PAD / 4] & CK_RP1_PAD_OUT_DISABLE));
604 CHECK((pr.in_low & CK_RP1_GPIO_PIN_BIT) == 0 && (pr.in_high & CK_RP1_GPIO_PIN_BIT) != 0);
605 CHECK(pr.ctrl_before == (0x1f | (1u << 20)) && (pr.ctrl_after & 0x1f) == 5);
606 /* the register addresses (bank 1 pin 4): see pinctrl-rp1.c bank table */
607 CHECK(CK_RP1_GPIO32_CTRL == 0xd4024u && CK_RP1_GPIO32_PAD == 0xf4014u && CK_RP1_RIO1_OUT == 0xe4000u);
608 /* a pin stuck low (external fault) is reported, not ignored */
609 rp_fresh();
610 fp.pin_forced = 1;
611 CHECK(ck_rp1_phy_reset(&rio, &pr) == CK_PI5NET_E_READBACK);
612}
613
614static void gem_phy(uint32_t polls_busy, int addr, uint32_t ready_polls)
615{
616 rp_fresh();
617 fp.gem_busy_polls = polls_busy;
618 fp.phy_addr = addr;
619 fp.phy_ready_polls = ready_polls;
620 fp.phy_regs[2] = 0x600d;
621 fp.phy_regs[3] = 0x84a2;
622 fp.r[(CK_RP1_ETH + CK_GEM_NCR) / 4] = 0x00000003u;
623 fp.r[(CK_RP1_ETH + CK_GEM_NCFGR) / 4] = 0x00000800u | (1u << 18);
624}
625
626static void t_mdio(void)
627{
628 uint16_t v = 0;
629 struct ck_phy_id id;
630 uint32_t att = 0;
631 int hit = 0;
632 CHECK(ck_gem_mdio_frame_read(1, 2) == 0x608a0000u);
633 CHECK(ck_gem_mdio_frame_read(31, 31) == (0x60000000u | (31u << 23) | (31u << 18) | 0x20000u));
634 CHECK(ck_gem_mdc_div_code(0) == 7 && ck_gem_mdc_div_code(20000000) == 0 && ck_gem_mdc_div_code(20000001) == 1 &&
635 ck_gem_mdc_div_code(80000000) == 2 && ck_gem_mdc_div_code(120000000) == 3 &&
636 ck_gem_mdc_div_code(160000000) == 4 && ck_gem_mdc_div_code(200000000) == 5 &&
637 ck_gem_mdc_div_code(240000000) == 5 && ck_gem_mdc_div_code(320000000) == 6 &&
638 ck_gem_mdc_div_code(400000000) == 7);
639 /* 200 MHz pclk / 96 = 2.08 MHz MDC, under the 2.5 MHz limit */
640 CHECK(200000000u / 96u <= 2500000u);
641 gem_phy(3, 1, 0);
642 CHECK(ck_gem_mdio_read(&rio, 1, 2, &v) == 0 && v == 0x600d);
643 CHECK(ck_gem_mdio_read(&rio, 1, 3, &v) == 0 && v == 0x84a2);
644 CHECK(fp.bad_frame_seen == 0 && fp.last_man == ck_gem_mdio_frame_read(1, 3));
645 CHECK(ck_gem_mdio_read(&rio, 5, 2, &v) == 0 && v == 0xffff); /* nothing at address 5 */
646 CHECK(ck_gem_mdio_read(&rio, 32, 2, &v) == CK_PI5NET_E_ARG && ck_gem_mdio_read(&rio, 1, 32, &v) == CK_PI5NET_E_ARG);
647 CHECK(ck_gem_mdio_read(&rio, 1, 2, NULL) == CK_PI5NET_E_ARG);
648 /* the management logic never goes idle: bounded wait, then timeout (no hang) */
649 gem_phy(0, 1, 0);
650 fp.never_idle = 1;
651 CHECK(ck_gem_mdio_read(&rio, 1, 2, &v) == CK_PI5NET_E_TIMEOUT);
652 CHECK(fp.t_us >= (uint64_t)CK_GEM_MDIO_POLLS * CK_GEM_MDIO_POLL_US);
653 /* enable: MPE set, other NCR bits kept, divider replaced and other NCFGR bits kept */
654 gem_phy(0, 1, 0);
655 fp.r[(CK_RP1_ETH + CK_GEM_NCR) / 4] = 0x00000003u;
656 CHECK(ck_gem_mdio_enable(&rio, 200000000u) == 0);
657 CHECK(fp.r[(CK_RP1_ETH + CK_GEM_NCR) / 4] == (0x3u | CK_GEM_NCR_MPE));
658 CHECK(fp.r[(CK_RP1_ETH + CK_GEM_NCFGR) / 4] == (0x800u | (5u << 18)));
659 gem_phy(0, 1, 0);
660 fp.stuck_ncfg = 1;
661 CHECK(ck_gem_mdio_enable(&rio, 200000000u) == CK_PI5NET_E_READBACK);
662 /* find: the PHY answers only after a few polls (still in reset): retries, then reports attempts */
663 gem_phy(1, 1, 6); /* the first 6 register reads return 0xffff: 3 attempts of 2 reads fail, the 4th works */
664 CHECK(ck_phy_find(&rio, 1, 20, 25, &id, &att, &hit) == 0);
665 CHECK(att == 4 && id.id == 0x600d84a2u && id.cls == CK_PHY_BCM54213PE);
666 /* never answers at 1, but something answers at 7: reported for the diagnosis */
667 gem_phy(0, 7, 0);
668 CHECK(ck_phy_find(&rio, 1, 3, 1, &id, &att, &hit) == CK_PI5NET_E_TIMEOUT && att == 3 && hit == 7);
669 CHECK(id.cls == CK_PHY_ABSENT);
670 gem_phy(0, -1, 0);
671 CHECK(ck_phy_find(&rio, 1, 2, 1, &id, &att, &hit) == CK_PI5NET_E_TIMEOUT && hit == -1);
672 CHECK(ck_phy_find(&rio, 1, 0, 1, &id, &att, &hit) == CK_PI5NET_E_ARG);
673}
674
675static void t_phy_id(void)
676{
677 struct ck_phy_id d;
678 /* BCM54213PE: Linux PHY_ID_BCM54213PE 0x600d84a2; OUI 18-C0-86 is "Broadcom" in the IEEE registry */
679 ck_phy_id_decode(0x600d, 0x84a2, &d);
680 CHECK(d.id == 0x600d84a2u && d.oui == 0x18c086u && d.model == 0x0a && d.rev == 2 && d.cls == CK_PHY_BCM54213PE);
681 /* BCM54210E (0x600d84a0) is Broadcom but NOT this chip: Linux matches the whole 32 bits */
682 ck_phy_id_decode(0x600d, 0x84a0, &d);
683 CHECK(d.cls == CK_PHY_BROADCOM_OTHER && d.oui == 0x18c086u);
684 ck_phy_id_decode(0x600d, 0x84a3, &d); /* a later revision of 54213PE is not exact either */
685 CHECK(d.cls == CK_PHY_BROADCOM_OTHER);
686 /* independent vector: Realtek RTL8211 0x001cc916, OUI 00-E0-4C */
687 ck_phy_id_decode(0x001c, 0xc916, &d);
688 CHECK(d.oui == 0x00e04cu && d.cls == CK_PHY_OTHER);
689 /* independent vector: Broadcom BCM5421 0x002060e0, OUI 00-10-18 */
690 ck_phy_id_decode(0x0020, 0x60e0, &d);
691 CHECK(d.oui == 0x001018u && d.cls == CK_PHY_BROADCOM_OTHER);
692 ck_phy_id_decode(0xffff, 0xffff, &d);
693 CHECK(d.cls == CK_PHY_ABSENT);
694 ck_phy_id_decode(0x0000, 0x0000, &d);
695 CHECK(d.cls == CK_PHY_ABSENT);
696 CHECK(strcmp(ck_phy_class_name(CK_PHY_BCM54213PE), "BCM54213PE") == 0);
697}
698
699int main(void)
700{
701 t_size_code();
702 t_plan();
703 t_plan_then_registers_then_verdict();
704 t_apply_order();
705 t_apply_failure_restores();
706 t_quiesce();
707 t_outbound();
708 t_clock_rates();
709 t_clock_decide();
710 t_clock_apply();
711 t_phy_reset();
712 t_mdio();
713 t_phy_id();
714 return ck_t_verdict("test_pi5_net");
715}