blob: d4a5620c62c11b4f2069b18a68886803ac2f6c14 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass5a822e12015-03-05 12:25:29 -07002/*
3 * PCI emulation device which swaps the case of text
4 *
5 * Copyright (c) 2014 Google, Inc
6 * Written by Simon Glass <sjg@chromium.org>
Simon Glass5a822e12015-03-05 12:25:29 -07007 */
8
Simon Glass5a822e12015-03-05 12:25:29 -07009#include <dm.h>
Simon Glass07a3b232015-05-04 11:31:08 -060010#include <errno.h>
Simon Glass0f2af882020-05-10 11:40:05 -060011#include <log.h>
Simon Glass5a822e12015-03-05 12:25:29 -070012#include <pci.h>
13#include <asm/test.h>
14#include <linux/ctype.h>
15
16/**
Simon Glassb75b15b2020-12-03 16:55:23 -070017 * struct swap_case_plat - platform data for this device
Simon Glass5a822e12015-03-05 12:25:29 -070018 *
19 * @command: Current PCI command value
20 * @bar: Current base address values
21 */
Simon Glassb75b15b2020-12-03 16:55:23 -070022struct swap_case_plat {
Simon Glass5a822e12015-03-05 12:25:29 -070023 u16 command;
Simon Glassff7dd582018-06-12 00:05:02 -060024 u32 bar[6];
Simon Glass5a822e12015-03-05 12:25:29 -070025};
26
Simon Glass5a822e12015-03-05 12:25:29 -070027enum {
28 MEM_TEXT_SIZE = 0x100,
29};
30
31enum swap_case_op {
32 OP_TO_LOWER,
33 OP_TO_UPPER,
34 OP_SWAP,
35};
36
37static struct pci_bar {
38 int type;
39 u32 size;
40} barinfo[] = {
41 { PCI_BASE_ADDRESS_SPACE_IO, 1 },
42 { PCI_BASE_ADDRESS_MEM_TYPE_32, MEM_TEXT_SIZE },
43 { 0, 0 },
44 { 0, 0 },
45 { 0, 0 },
46 { 0, 0 },
47};
48
49struct swap_case_priv {
50 enum swap_case_op op;
51 char mem_text[MEM_TEXT_SIZE];
52};
53
Simon Glass2a311e82020-01-27 08:49:37 -070054static int sandbox_swap_case_use_ea(const struct udevice *dev)
Alex Margineanf1274432019-06-07 11:24:24 +030055{
Simon Glassa7ece582020-12-19 10:40:14 -070056 return !!ofnode_get_property(dev_ofnode(dev), "use-ea", NULL);
Alex Margineanf1274432019-06-07 11:24:24 +030057}
58
59/* Please keep these macros in sync with ea_regs below */
60#define PCI_CAP_ID_EA_SIZE (sizeof(ea_regs) + 4)
61#define PCI_CAP_ID_EA_ENTRY_CNT 4
62/* Hardcoded EA structure, excluding 1st DW. */
63static const u32 ea_regs[] = {
64 /* BEI=0, ES=2, BAR0 32b Base + 32b MaxOffset, I/O space */
65 (2 << 8) | 2,
66 PCI_CAP_EA_BASE_LO0,
67 0,
68 /* BEI=1, ES=2, BAR1 32b Base + 32b MaxOffset */
69 (1 << 4) | 2,
70 PCI_CAP_EA_BASE_LO1,
71 MEM_TEXT_SIZE - 1,
72 /* BEI=2, ES=3, BAR2 64b Base + 32b MaxOffset */
73 (2 << 4) | 3,
74 PCI_CAP_EA_BASE_LO2 | PCI_EA_IS_64,
75 PCI_CAP_EA_SIZE_LO,
76 PCI_CAP_EA_BASE_HI2,
77 /* BEI=4, ES=4, BAR4 64b Base + 64b MaxOffset */
78 (4 << 4) | 4,
79 PCI_CAP_EA_BASE_LO4 | PCI_EA_IS_64,
80 PCI_CAP_EA_SIZE_LO | PCI_EA_IS_64,
81 PCI_CAP_EA_BASE_HI4,
82 PCI_CAP_EA_SIZE_HI,
83};
84
Simon Glass2a311e82020-01-27 08:49:37 -070085static int sandbox_swap_case_read_ea(const struct udevice *emul, uint offset,
Alex Margineanf1274432019-06-07 11:24:24 +030086 ulong *valuep, enum pci_size_t size)
87{
88 u32 reg;
89
90 offset = offset - PCI_CAP_ID_EA_OFFSET - 4;
91 reg = ea_regs[offset >> 2];
92 reg >>= (offset % 4) * 8;
93
94 *valuep = reg;
95 return 0;
96}
97
Simon Glass2a311e82020-01-27 08:49:37 -070098static int sandbox_swap_case_read_config(const struct udevice *emul,
99 uint offset, ulong *valuep,
100 enum pci_size_t size)
Simon Glass5a822e12015-03-05 12:25:29 -0700101{
Simon Glassb75b15b2020-12-03 16:55:23 -0700102 struct swap_case_plat *plat = dev_get_plat(emul);
Simon Glass5a822e12015-03-05 12:25:29 -0700103
Alex Margineanf1274432019-06-07 11:24:24 +0300104 /*
105 * The content of the EA capability structure is handled elsewhere to
106 * keep the switch/case below sane
107 */
108 if (offset > PCI_CAP_ID_EA_OFFSET + PCI_CAP_LIST_NEXT &&
109 offset < PCI_CAP_ID_EA_OFFSET + PCI_CAP_ID_EA_SIZE)
110 return sandbox_swap_case_read_ea(emul, offset, valuep, size);
111
Simon Glass5a822e12015-03-05 12:25:29 -0700112 switch (offset) {
113 case PCI_COMMAND:
114 *valuep = plat->command;
115 break;
116 case PCI_HEADER_TYPE:
117 *valuep = 0;
118 break;
119 case PCI_VENDOR_ID:
120 *valuep = SANDBOX_PCI_VENDOR_ID;
121 break;
122 case PCI_DEVICE_ID:
Simon Glass21c8f1a2019-09-25 08:56:01 -0600123 *valuep = SANDBOX_PCI_SWAP_CASE_EMUL_ID;
Simon Glass5a822e12015-03-05 12:25:29 -0700124 break;
125 case PCI_CLASS_DEVICE:
126 if (size == PCI_SIZE_8) {
127 *valuep = SANDBOX_PCI_CLASS_SUB_CODE;
128 } else {
129 *valuep = (SANDBOX_PCI_CLASS_CODE << 8) |
130 SANDBOX_PCI_CLASS_SUB_CODE;
131 }
132 break;
133 case PCI_CLASS_CODE:
134 *valuep = SANDBOX_PCI_CLASS_CODE;
135 break;
136 case PCI_BASE_ADDRESS_0:
137 case PCI_BASE_ADDRESS_1:
138 case PCI_BASE_ADDRESS_2:
139 case PCI_BASE_ADDRESS_3:
140 case PCI_BASE_ADDRESS_4:
141 case PCI_BASE_ADDRESS_5: {
142 int barnum;
Simon Glass72231f72019-09-25 08:56:42 -0600143 u32 *bar;
Simon Glass5a822e12015-03-05 12:25:29 -0700144
Simon Glass130d7ff2019-09-25 08:56:06 -0600145 barnum = pci_offset_to_barnum(offset);
Simon Glass5a822e12015-03-05 12:25:29 -0700146 bar = &plat->bar[barnum];
147
Simon Glass72231f72019-09-25 08:56:42 -0600148 *valuep = sandbox_pci_read_bar(*bar, barinfo[barnum].type,
149 barinfo[barnum].size);
Simon Glass5a822e12015-03-05 12:25:29 -0700150 break;
151 }
Bin Mengd74d3122018-08-03 01:14:53 -0700152 case PCI_CAPABILITY_LIST:
153 *valuep = PCI_CAP_ID_PM_OFFSET;
154 break;
155 case PCI_CAP_ID_PM_OFFSET:
156 *valuep = (PCI_CAP_ID_EXP_OFFSET << 8) | PCI_CAP_ID_PM;
157 break;
Bin Mengb59b3692018-10-15 02:21:22 -0700158 case PCI_CAP_ID_PM_OFFSET + PCI_CAP_LIST_NEXT:
159 *valuep = PCI_CAP_ID_EXP_OFFSET;
160 break;
Bin Mengd74d3122018-08-03 01:14:53 -0700161 case PCI_CAP_ID_EXP_OFFSET:
162 *valuep = (PCI_CAP_ID_MSIX_OFFSET << 8) | PCI_CAP_ID_EXP;
163 break;
Bin Mengb59b3692018-10-15 02:21:22 -0700164 case PCI_CAP_ID_EXP_OFFSET + PCI_CAP_LIST_NEXT:
165 *valuep = PCI_CAP_ID_MSIX_OFFSET;
166 break;
Stephen Carlson710862d2023-03-10 11:07:14 -0800167 case PCI_CAP_ID_EXP_OFFSET + PCI_EXP_DEVCAP:
168 *valuep = PCI_EXP_DEVCAP_PAYLOAD_256B;
169 break;
Bin Mengd74d3122018-08-03 01:14:53 -0700170 case PCI_CAP_ID_MSIX_OFFSET:
Alex Margineanf1274432019-06-07 11:24:24 +0300171 if (sandbox_swap_case_use_ea(emul))
172 *valuep = (PCI_CAP_ID_EA_OFFSET << 8) | PCI_CAP_ID_MSIX;
173 else
174 *valuep = PCI_CAP_ID_MSIX;
Bin Mengd74d3122018-08-03 01:14:53 -0700175 break;
Bin Mengb59b3692018-10-15 02:21:22 -0700176 case PCI_CAP_ID_MSIX_OFFSET + PCI_CAP_LIST_NEXT:
Alex Margineanf1274432019-06-07 11:24:24 +0300177 if (sandbox_swap_case_use_ea(emul))
178 *valuep = PCI_CAP_ID_EA_OFFSET;
179 else
180 *valuep = 0;
181 break;
182 case PCI_CAP_ID_EA_OFFSET:
183 *valuep = (PCI_CAP_ID_EA_ENTRY_CNT << 16) | PCI_CAP_ID_EA;
184 break;
185 case PCI_CAP_ID_EA_OFFSET + PCI_CAP_LIST_NEXT:
Bin Mengb59b3692018-10-15 02:21:22 -0700186 *valuep = 0;
187 break;
Bin Mengd74d3122018-08-03 01:14:53 -0700188 case PCI_EXT_CAP_ID_ERR_OFFSET:
189 *valuep = (PCI_EXT_CAP_ID_VC_OFFSET << 20) | PCI_EXT_CAP_ID_ERR;
190 break;
191 case PCI_EXT_CAP_ID_VC_OFFSET:
192 *valuep = (PCI_EXT_CAP_ID_DSN_OFFSET << 20) | PCI_EXT_CAP_ID_VC;
193 break;
194 case PCI_EXT_CAP_ID_DSN_OFFSET:
195 *valuep = PCI_EXT_CAP_ID_DSN;
196 break;
Simon Glass5a822e12015-03-05 12:25:29 -0700197 }
198
199 return 0;
200}
201
202static int sandbox_swap_case_write_config(struct udevice *emul, uint offset,
203 ulong value, enum pci_size_t size)
204{
Simon Glassb75b15b2020-12-03 16:55:23 -0700205 struct swap_case_plat *plat = dev_get_plat(emul);
Simon Glass5a822e12015-03-05 12:25:29 -0700206
207 switch (offset) {
208 case PCI_COMMAND:
209 plat->command = value;
210 break;
211 case PCI_BASE_ADDRESS_0:
212 case PCI_BASE_ADDRESS_1: {
213 int barnum;
214 u32 *bar;
215
Simon Glass130d7ff2019-09-25 08:56:06 -0600216 barnum = pci_offset_to_barnum(offset);
Simon Glass5a822e12015-03-05 12:25:29 -0700217 bar = &plat->bar[barnum];
218
219 debug("w bar %d=%lx\n", barnum, value);
220 *bar = value;
Bin Meng5b87baf2018-08-03 01:14:40 -0700221 /* space indicator (bit#0) is read-only */
222 *bar |= barinfo[barnum].type;
Simon Glass5a822e12015-03-05 12:25:29 -0700223 break;
224 }
225 }
226
227 return 0;
228}
229
230static int sandbox_swap_case_find_bar(struct udevice *emul, unsigned int addr,
231 int *barnump, unsigned int *offsetp)
232{
Simon Glassb75b15b2020-12-03 16:55:23 -0700233 struct swap_case_plat *plat = dev_get_plat(emul);
Simon Glass5a822e12015-03-05 12:25:29 -0700234 int barnum;
235
236 for (barnum = 0; barnum < ARRAY_SIZE(barinfo); barnum++) {
237 unsigned int size = barinfo[barnum].size;
Bin Meng5b87baf2018-08-03 01:14:40 -0700238 u32 base = plat->bar[barnum] & ~PCI_BASE_ADDRESS_SPACE;
Simon Glass5a822e12015-03-05 12:25:29 -0700239
Bin Meng5b87baf2018-08-03 01:14:40 -0700240 if (addr >= base && addr < base + size) {
Simon Glass5a822e12015-03-05 12:25:29 -0700241 *barnump = barnum;
Bin Meng5b87baf2018-08-03 01:14:40 -0700242 *offsetp = addr - base;
Simon Glass5a822e12015-03-05 12:25:29 -0700243 return 0;
244 }
245 }
246 *barnump = -1;
247
248 return -ENOENT;
249}
250
251static void sandbox_swap_case_do_op(enum swap_case_op op, char *str, int len)
252{
253 for (; len > 0; len--, str++) {
254 switch (op) {
255 case OP_TO_UPPER:
256 *str = toupper(*str);
257 break;
258 case OP_TO_LOWER:
259 *str = tolower(*str);
260 break;
261 case OP_SWAP:
262 if (isupper(*str))
263 *str = tolower(*str);
264 else
265 *str = toupper(*str);
266 break;
267 }
268 }
269}
270
Simon Glass81ae7782019-09-25 08:56:03 -0600271static int sandbox_swap_case_read_io(struct udevice *dev, unsigned int addr,
272 ulong *valuep, enum pci_size_t size)
Simon Glass5a822e12015-03-05 12:25:29 -0700273{
274 struct swap_case_priv *priv = dev_get_priv(dev);
275 unsigned int offset;
276 int barnum;
277 int ret;
278
279 ret = sandbox_swap_case_find_bar(dev, addr, &barnum, &offset);
280 if (ret)
281 return ret;
282
283 if (barnum == 0 && offset == 0)
284 *valuep = (*valuep & ~0xff) | priv->op;
285
286 return 0;
287}
288
Simon Glass81ae7782019-09-25 08:56:03 -0600289static int sandbox_swap_case_write_io(struct udevice *dev, unsigned int addr,
290 ulong value, enum pci_size_t size)
Simon Glass5a822e12015-03-05 12:25:29 -0700291{
292 struct swap_case_priv *priv = dev_get_priv(dev);
293 unsigned int offset;
294 int barnum;
295 int ret;
296
297 ret = sandbox_swap_case_find_bar(dev, addr, &barnum, &offset);
298 if (ret)
299 return ret;
300 if (barnum == 0 && offset == 0)
301 priv->op = value;
302
303 return 0;
304}
305
Alex Margineanf1274432019-06-07 11:24:24 +0300306static int pci_ea_bar2_magic = PCI_EA_BAR2_MAGIC;
Alex Margineanf1274432019-06-07 11:24:24 +0300307
Simon Glass5a822e12015-03-05 12:25:29 -0700308static int sandbox_swap_case_map_physmem(struct udevice *dev,
309 phys_addr_t addr, unsigned long *lenp, void **ptrp)
310{
311 struct swap_case_priv *priv = dev_get_priv(dev);
312 unsigned int offset, avail;
313 int barnum;
314 int ret;
315
Alex Margineanf1274432019-06-07 11:24:24 +0300316 if (sandbox_swap_case_use_ea(dev)) {
317 /*
318 * only support mapping base address in EA test for now, we
319 * don't handle mapping an offset inside a BAR. Seems good
320 * enough for the current test.
321 */
322 switch (addr) {
323 case (phys_addr_t)PCI_CAP_EA_BASE_LO0:
324 *ptrp = &priv->op;
325 *lenp = 4;
326 break;
327 case (phys_addr_t)PCI_CAP_EA_BASE_LO1:
328 *ptrp = priv->mem_text;
329 *lenp = barinfo[1].size - 1;
330 break;
331 case (phys_addr_t)((PCI_CAP_EA_BASE_HI2 << 32) |
332 PCI_CAP_EA_BASE_LO2):
333 *ptrp = &pci_ea_bar2_magic;
334 *lenp = PCI_CAP_EA_SIZE_LO;
335 break;
Simon Glassc40a98c2021-07-18 14:14:23 -0600336#ifdef CONFIG_HOST_64BIT
337 /*
338 * This cannot be work on a 32-bit machine since *lenp is ulong
339 * which is 32-bits, but it needs to have a 64-bit value
340 * assigned
341 */
Alex Margineanf1274432019-06-07 11:24:24 +0300342 case (phys_addr_t)((PCI_CAP_EA_BASE_HI4 << 32) |
Simon Glassc40a98c2021-07-18 14:14:23 -0600343 PCI_CAP_EA_BASE_LO4): {
344 static int pci_ea_bar4_magic = PCI_EA_BAR4_MAGIC;
345
Alex Margineanf1274432019-06-07 11:24:24 +0300346 *ptrp = &pci_ea_bar4_magic;
347 *lenp = (PCI_CAP_EA_SIZE_HI << 32) |
348 PCI_CAP_EA_SIZE_LO;
349 break;
Simon Glassc40a98c2021-07-18 14:14:23 -0600350 }
351#endif
Alex Margineanf1274432019-06-07 11:24:24 +0300352 default:
353 return -ENOENT;
354 }
355 return 0;
356 }
357
Simon Glass5a822e12015-03-05 12:25:29 -0700358 ret = sandbox_swap_case_find_bar(dev, addr, &barnum, &offset);
359 if (ret)
360 return ret;
Alex Margineanf1274432019-06-07 11:24:24 +0300361
Simon Glass5a822e12015-03-05 12:25:29 -0700362 if (barnum == 1) {
363 *ptrp = priv->mem_text + offset;
364 avail = barinfo[1].size - offset;
365 if (avail > barinfo[1].size)
366 *lenp = 0;
367 else
368 *lenp = min(*lenp, (ulong)avail);
369
370 return 0;
371 }
372
373 return -ENOENT;
374}
375
376static int sandbox_swap_case_unmap_physmem(struct udevice *dev,
377 const void *vaddr, unsigned long len)
378{
379 struct swap_case_priv *priv = dev_get_priv(dev);
380
381 sandbox_swap_case_do_op(priv->op, (void *)vaddr, len);
382
383 return 0;
384}
385
Simon Glass81ae7782019-09-25 08:56:03 -0600386static struct dm_pci_emul_ops sandbox_swap_case_emul_ops = {
Simon Glass5a822e12015-03-05 12:25:29 -0700387 .read_config = sandbox_swap_case_read_config,
388 .write_config = sandbox_swap_case_write_config,
389 .read_io = sandbox_swap_case_read_io,
390 .write_io = sandbox_swap_case_write_io,
391 .map_physmem = sandbox_swap_case_map_physmem,
392 .unmap_physmem = sandbox_swap_case_unmap_physmem,
393};
394
395static const struct udevice_id sandbox_swap_case_ids[] = {
396 { .compatible = "sandbox,swap-case" },
397 { }
398};
399
400U_BOOT_DRIVER(sandbox_swap_case_emul) = {
401 .name = "sandbox_swap_case_emul",
402 .id = UCLASS_PCI_EMUL,
403 .of_match = sandbox_swap_case_ids,
404 .ops = &sandbox_swap_case_emul_ops,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700405 .priv_auto = sizeof(struct swap_case_priv),
Simon Glassb75b15b2020-12-03 16:55:23 -0700406 .plat_auto = sizeof(struct swap_case_plat),
Simon Glass5a822e12015-03-05 12:25:29 -0700407};
Bin Mengc69ae412018-08-03 01:14:46 -0700408
409static struct pci_device_id sandbox_swap_case_supported[] = {
Simon Glass21c8f1a2019-09-25 08:56:01 -0600410 { PCI_VDEVICE(SANDBOX, SANDBOX_PCI_SWAP_CASE_EMUL_ID),
411 SWAP_CASE_DRV_DATA },
Bin Mengc69ae412018-08-03 01:14:46 -0700412 {},
413};
414
415U_BOOT_PCI_DEVICE(sandbox_swap_case_emul, sandbox_swap_case_supported);