1f72301f7179965cba5ec4e57ca175286ba76f27
[dragonfly.git] / sys / bus / pci / i386 / pci_cfgreg.c
1 /*-
2  * Copyright (c) 1997, Stefan Esser <se@kfreebsd.org>
3  * Copyright (c) 2000, Michael Smith <msmith@kfreebsd.org>
4  * Copyright (c) 2000, BSDi
5  * Copyright (c) 2004, Scott Long <scottl@kfreebsd.org>
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice unmodified, this list of conditions, and the following
13  *    disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  *
29  * $FreeBSD: src/sys/i386/pci/pci_cfgreg.c,v 1.124.2.3 2009/05/04 21:04:29 jhb
30  */
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/bus.h>
35 #include <sys/kernel.h>
36 #include <sys/lock.h>
37 #include <sys/malloc.h>
38 #include <sys/thread2.h>
39 #include <sys/spinlock.h>
40 #include <sys/spinlock2.h>
41 #include <sys/queue.h>
42 #include <bus/pci/pcivar.h>
43 #include <bus/pci/pcireg.h>
44 #include "pci_cfgreg.h"
45 #include <machine/pc/bios.h>
46
47 #include <vm/vm.h>
48 #include <vm/vm_param.h>
49 #include <vm/vm_kern.h>
50 #include <vm/vm_extern.h>
51 #include <vm/pmap.h>
52 #include <machine/pmap.h>
53
54 #if defined(__DragonFly__)
55 #define mtx_init(a, b, c, d) spin_init(a)
56 #define mtx_lock_spin(a) spin_lock(a)
57 #define mtx_unlock_spin(a) spin_unlock(a)
58 #endif
59
60 #define PRVERB(a) do {                                                  \
61         if (bootverbose)                                                \
62                 kprintf a ;                                             \
63 } while(0)
64
65 #define PCIE_CACHE 8
66 struct pcie_cfg_elem {
67         TAILQ_ENTRY(pcie_cfg_elem)      elem;
68         vm_offset_t     vapage;
69         vm_paddr_t      papage;
70 };
71
72 enum {
73         CFGMECH_NONE = 0,
74         CFGMECH_1,
75         CFGMECH_2,
76         CFGMECH_PCIE,
77 };
78
79 static TAILQ_HEAD(pcie_cfg_list, pcie_cfg_elem) pcie_list[MAXCPU];
80 static uint64_t pcie_base;
81 static int pcie_minbus, pcie_maxbus;
82 static uint32_t pcie_badslots;
83 static int cfgmech;
84 static int devmax;
85 #if defined(__DragonFly__)
86 static struct spinlock pcicfg_mtx;
87 #else
88 static struct mtx pcicfg_mtx;
89 #endif
90 static int mcfg_enable = 0;
91
92 TUNABLE_INT("hw.pci.mcfg", &mcfg_enable);
93
94 static uint32_t pci_docfgregread(int bus, int slot, int func, int reg, int bytes);
95 static int      pcireg_cfgread(int bus, int slot, int func, int reg, int bytes);
96 static void     pcireg_cfgwrite(int bus, int slot, int func, int reg, int data, int bytes);
97 static int      pcireg_cfgopen(void);
98
99 static int      pciereg_cfgread(int bus, unsigned slot, unsigned func,
100                    unsigned reg, unsigned bytes);
101 static void     pciereg_cfgwrite(int bus, unsigned slot, unsigned func,
102                    unsigned reg, int data, unsigned bytes);
103
104
105
106 /*
107  * Some BIOS writers seem to want to ignore the spec and put
108  * 0 in the intline rather than 255 to indicate none.  Some use
109  * numbers in the range 128-254 to indicate something strange and
110  * apparently undocumented anywhere.  Assume these are completely bogus
111  * and map them to 255, which means "none".
112  */
113 static __inline int 
114 pci_i386_map_intline(int line)
115 {
116         if (line == 0 || line >= 128)
117                 return (PCI_INVALID_IRQ);
118         return (line);
119 }
120
121 static u_int16_t
122 pcibios_get_version(void)
123 {
124         struct bios_regs args;
125
126         if (PCIbios.ventry == 0) {
127                 PRVERB(("pcibios: No call entry point\n"));
128                 return (0);
129         }
130         args.eax = PCIBIOS_BIOS_PRESENT;
131         if (bios32(&args, PCIbios.ventry, GSEL(GCODE_SEL, SEL_KPL))) {
132                 PRVERB(("pcibios: BIOS_PRESENT call failed\n"));
133                 return (0);
134         }
135         if (args.edx != 0x20494350) {
136                 PRVERB(("pcibios: BIOS_PRESENT didn't return 'PCI ' in edx\n"));
137                 return (0);
138         }
139         return (args.ebx & 0xffff);
140 }
141
142 /* 
143  * Initialise access to PCI configuration space 
144  */
145 int
146 pci_cfgregopen(void)
147 {
148         static int              opened = 0;
149         uint64_t                pciebar;
150         u_int16_t               vid, did;
151         u_int16_t               v;
152         if (opened)
153                 return (1);
154
155         if (cfgmech == CFGMECH_NONE && pcireg_cfgopen() == 0)
156                 return (0);
157
158         v = pcibios_get_version();
159         if (v > 0)
160                 PRVERB(("pcibios: BIOS version %x.%02x\n", (v & 0xff00) >> 8,
161                     v & 0xff));
162         mtx_init(&pcicfg_mtx, "pcicfg", NULL, MTX_SPIN);
163         opened = 1;
164
165         /* $PIR requires PCI BIOS 2.10 or greater. */
166         if (v >= 0x0210)
167                 pci_pir_open();
168
169         if (cfgmech == CFGMECH_PCIE)
170                 return (1);
171
172         /*
173          * Grope around in the PCI config space to see if this is a
174          * chipset that is capable of doing memory-mapped config cycles.
175          * This also implies that it can do PCIe extended config cycles.
176          */
177
178         /* Check for supported chipsets */
179         vid = pci_cfgregread(0, 0, 0, PCIR_VENDOR, 2);
180         did = pci_cfgregread(0, 0, 0, PCIR_DEVICE, 2);
181         switch (vid) {
182         case 0x8086:
183                 switch (did) {
184                 case 0x3590:
185                 case 0x3592:
186                         /* Intel 7520 or 7320 */
187                         pciebar = pci_cfgregread(0, 0, 0, 0xce, 2) << 16;
188                         pcie_cfgregopen(pciebar, 0, 255);
189                         break;
190                 case 0x2580:
191                 case 0x2584:
192                 case 0x2590:
193                         /* Intel 915, 925, or 915GM */
194                         pciebar = pci_cfgregread(0, 0, 0, 0x48, 4);
195                         pcie_cfgregopen(pciebar, 0, 255);
196                         break;
197                 }
198         }
199
200         return(1);
201 }
202
203 static uint32_t
204 pci_docfgregread(int bus, int slot, int func, int reg, int bytes)
205 {
206
207         if (cfgmech == CFGMECH_PCIE &&
208             (bus >= pcie_minbus && bus <= pcie_maxbus) &&
209             (bus != 0 || !(1 << slot & pcie_badslots)))
210                 return (pciereg_cfgread(bus, slot, func, reg, bytes));
211         else
212                 return (pcireg_cfgread(bus, slot, func, reg, bytes));
213 }
214
215 /* 
216  * Read configuration space register
217  */
218 u_int32_t
219 pci_cfgregread(int bus, int slot, int func, int reg, int bytes)
220 {
221         uint32_t line;
222
223         /*
224          * Some BIOS writers seem to want to ignore the spec and put
225          * 0 in the intline rather than 255 to indicate none.  The rest of
226          * the code uses 255 as an invalid IRQ.
227          */
228         if (reg == PCIR_INTLINE && bytes == 1) {
229                 line = pci_docfgregread(bus, slot, func, PCIR_INTLINE, 1);
230                 return (pci_i386_map_intline(line));
231         }
232         return (pci_docfgregread(bus, slot, func, reg, bytes));
233 }
234
235 /* 
236  * Write configuration space register 
237  */
238 void
239 pci_cfgregwrite(int bus, int slot, int func, int reg, u_int32_t data, int bytes)
240 {
241
242         if (cfgmech == CFGMECH_PCIE &&
243             (bus >= pcie_minbus && bus <= pcie_maxbus) &&
244             (bus != 0 || !(1 << slot & pcie_badslots)))
245                 pciereg_cfgwrite(bus, slot, func, reg, data, bytes);
246         else
247                 pcireg_cfgwrite(bus, slot, func, reg, data, bytes);
248 }
249
250 /* 
251  * Configuration space access using direct register operations
252  */
253
254 /* enable configuration space accesses and return data port address */
255 static int
256 pci_cfgenable(unsigned bus, unsigned slot, unsigned func, int reg, int bytes)
257 {
258         int dataport = 0;
259
260 #ifdef XBOX
261         if (arch_i386_is_xbox) {
262                 /*
263                  * The Xbox MCPX chipset is a derivative of the nForce 1
264                  * chipset. It almost has the same bus layout; some devices
265                  * cannot be used, because they have been removed.
266                  */
267
268                 /*
269                  * Devices 00:00.1 and 00:00.2 used to be memory controllers on
270                  * the nForce chipset, but on the Xbox, using them will lockup
271                  * the chipset.
272                  */
273                 if (bus == 0 && slot == 0 && (func == 1 || func == 2))
274                         return dataport;
275                 
276                 /*
277                  * Bus 1 only contains a VGA controller at 01:00.0. When you try
278                  * to probe beyond that device, you only get garbage, which
279                  * could cause lockups.
280                  */
281                 if (bus == 1 && (slot != 0 || func != 0))
282                         return dataport;
283                 
284                 /*
285                  * Bus 2 used to contain the AGP controller, but the Xbox MCPX
286                  * doesn't have one. Probing it can cause lockups.
287                  */
288                 if (bus >= 2)
289                         return dataport;
290         }
291 #endif
292
293         if (bus <= PCI_BUSMAX
294             && slot < devmax
295             && func <= PCI_FUNCMAX
296             && reg <= PCI_REGMAX
297             && bytes != 3
298             && (unsigned) bytes <= 4
299             && (reg & (bytes - 1)) == 0) {
300                 switch (cfgmech) {
301                 case CFGMECH_PCIE:
302                 case CFGMECH_1:
303                         outl(CONF1_ADDR_PORT, (1 << 31)
304                             | (bus << 16) | (slot << 11) 
305                             | (func << 8) | (reg & ~0x03));
306                         dataport = CONF1_DATA_PORT + (reg & 0x03);
307                         break;
308                 case CFGMECH_2:
309                         outb(CONF2_ENABLE_PORT, 0xf0 | (func << 1));
310                         outb(CONF2_FORWARD_PORT, bus);
311                         dataport = 0xc000 | (slot << 8) | reg;
312                         break;
313                 }
314         }
315         return (dataport);
316 }
317
318 /* disable configuration space accesses */
319 static void
320 pci_cfgdisable(void)
321 {
322         switch (cfgmech) {
323         case CFGMECH_PCIE:
324         case CFGMECH_1:
325                 /*
326                  * Do nothing for the config mechanism 1 case.
327                  * Writing a 0 to the address port can apparently
328                  * confuse some bridges and cause spurious
329                  * access failures.
330                  */
331                 break;
332         case CFGMECH_2:
333                 outb(CONF2_ENABLE_PORT, 0);
334                 break;
335         }
336 }
337
338 static int
339 pcireg_cfgread(int bus, int slot, int func, int reg, int bytes)
340 {
341         int data = -1;
342         int port;
343
344         mtx_lock_spin(&pcicfg_mtx);
345         port = pci_cfgenable(bus, slot, func, reg, bytes);
346         if (port != 0) {
347                 switch (bytes) {
348                 case 1:
349                         data = inb(port);
350                         break;
351                 case 2:
352                         data = inw(port);
353                         break;
354                 case 4:
355                         data = inl(port);
356                         break;
357                 }
358                 pci_cfgdisable();
359         }
360         mtx_unlock_spin(&pcicfg_mtx);
361         return (data);
362 }
363
364 static void
365 pcireg_cfgwrite(int bus, int slot, int func, int reg, int data, int bytes)
366 {
367         int port;
368
369         mtx_lock_spin(&pcicfg_mtx);
370         port = pci_cfgenable(bus, slot, func, reg, bytes);
371         if (port != 0) {
372                 switch (bytes) {
373                 case 1:
374                         outb(port, data);
375                         break;
376                 case 2:
377                         outw(port, data);
378                         break;
379                 case 4:
380                         outl(port, data);
381                         break;
382                 }
383                 pci_cfgdisable();
384         }
385         mtx_unlock_spin(&pcicfg_mtx);
386 }
387
388 /* check whether the configuration mechanism has been correctly identified */
389 static int
390 pci_cfgcheck(int maxdev)
391 {
392         uint32_t id, class;
393         uint8_t header;
394         uint8_t device;
395         int port;
396
397         if (bootverbose) 
398                 kprintf("pci_cfgcheck:\tdevice ");
399
400         for (device = 0; device < maxdev; device++) {
401                 if (bootverbose) 
402                         kprintf("%d ", device);
403
404                 port = pci_cfgenable(0, device, 0, 0, 4);
405                 id = inl(port);
406                 if (id == 0 || id == 0xffffffff)
407                         continue;
408
409                 port = pci_cfgenable(0, device, 0, 8, 4);
410                 class = inl(port) >> 8;
411                 if (bootverbose)
412                         kprintf("[class=%06x] ", class);
413                 if (class == 0 || (class & 0xf870ff) != 0)
414                         continue;
415
416                 port = pci_cfgenable(0, device, 0, 14, 1);
417                 header = inb(port);
418                 if (bootverbose)
419                         kprintf("[hdr=%02x] ", header);
420                 if ((header & 0x7e) != 0)
421                         continue;
422
423                 if (bootverbose)
424                         kprintf("is there (id=%08x)\n", id);
425
426                 pci_cfgdisable();
427                 return (1);
428         }
429         if (bootverbose) 
430                 kprintf("-- nothing found\n");
431
432         pci_cfgdisable();
433         return (0);
434 }
435
436 static int
437 pcireg_cfgopen(void)
438 {
439         uint32_t mode1res, oldval1;
440         uint8_t mode2res, oldval2;
441
442         /* Check for type #1 first. */
443         oldval1 = inl(CONF1_ADDR_PORT);
444
445         if (bootverbose) {
446                 kprintf("pci_open(1):\tmode 1 addr port (0x0cf8) is 0x%08x\n",
447                     oldval1);
448         }
449
450         cfgmech = CFGMECH_1;
451         devmax = 32;
452
453         outl(CONF1_ADDR_PORT, CONF1_ENABLE_CHK);
454         DELAY(1);
455         mode1res = inl(CONF1_ADDR_PORT);
456         outl(CONF1_ADDR_PORT, oldval1);
457
458         if (bootverbose)
459                 kprintf("pci_open(1a):\tmode1res=0x%08x (0x%08lx)\n",  mode1res,
460                     CONF1_ENABLE_CHK);
461
462         if (mode1res) {
463                 if (pci_cfgcheck(32)) 
464                         return (cfgmech);
465         }
466
467         outl(CONF1_ADDR_PORT, CONF1_ENABLE_CHK1);
468         mode1res = inl(CONF1_ADDR_PORT);
469         outl(CONF1_ADDR_PORT, oldval1);
470
471         if (bootverbose)
472                 kprintf("pci_open(1b):\tmode1res=0x%08x (0x%08lx)\n",  mode1res,
473                     CONF1_ENABLE_CHK1);
474
475         if ((mode1res & CONF1_ENABLE_MSK1) == CONF1_ENABLE_RES1) {
476                 if (pci_cfgcheck(32)) 
477                         return (cfgmech);
478         }
479
480         /* Type #1 didn't work, so try type #2. */
481         oldval2 = inb(CONF2_ENABLE_PORT);
482
483         if (bootverbose) {
484                 kprintf("pci_open(2):\tmode 2 enable port (0x0cf8) is 0x%02x\n",
485                     oldval2);
486         }
487
488         if ((oldval2 & 0xf0) == 0) {
489
490                 cfgmech = CFGMECH_2;
491                 devmax = 16;
492
493                 outb(CONF2_ENABLE_PORT, CONF2_ENABLE_CHK);
494                 mode2res = inb(CONF2_ENABLE_PORT);
495                 outb(CONF2_ENABLE_PORT, oldval2);
496
497                 if (bootverbose)
498                         kprintf("pci_open(2a):\tmode2res=0x%02x (0x%02x)\n",
499                             mode2res, CONF2_ENABLE_CHK);
500
501                 if (mode2res == CONF2_ENABLE_RES) {
502                         if (bootverbose)
503                                 kprintf("pci_open(2a):\tnow trying mechanism 2\n");
504
505                         if (pci_cfgcheck(16)) 
506                                 return (cfgmech);
507                 }
508         }
509
510         /* Nothing worked, so punt. */
511         cfgmech = CFGMECH_NONE;
512         devmax = 0;
513         return (cfgmech);
514 }
515
516 int
517 pcie_cfgregopen(uint64_t base, uint8_t minbus, uint8_t maxbus)
518 {
519 #ifdef PCIE_CFG_MECH
520         struct pcie_cfg_list *pcielist;
521         struct pcie_cfg_elem *pcie_array, *elem;
522 #ifdef SMP
523         struct pcpu *pc;
524 #endif
525         vm_offset_t va;
526         uint32_t val1, val2;
527         int i, slot;
528
529         if (!mcfg_enable)
530                 return (0);
531
532         if (minbus != 0)
533                 return (0);
534
535         if (base >= 0x100000000) {
536                 if (bootverbose)
537                         kprintf(
538                 "PCI: Memory Mapped PCI configuration area base 0x%jx too high\n",
539                         (uintmax_t)base);
540                 return (0);
541         }
542
543         if (bootverbose)
544                 kprintf("PCIe: Memory Mapped configuration base @ 0x%jx\n",
545                         (uintmax_t)base);
546
547 #ifdef SMP
548         SLIST_FOREACH(pc, &cpuhead, pc_allcpu)
549 #endif
550         {
551
552                 pcie_array = kmalloc(sizeof(struct pcie_cfg_elem) * PCIE_CACHE,
553                     M_DEVBUF, M_NOWAIT);
554                 if (pcie_array == NULL)
555                         return (0);
556
557                 va = kmem_alloc_nofault(&kernel_map, PCIE_CACHE * PAGE_SIZE,
558                                         PAGE_SIZE);
559                 if (va == 0) {
560                         kfree(pcie_array, M_DEVBUF);
561                         return (0);
562                 }
563
564 #ifdef SMP
565                 pcielist = &pcie_list[pc->pc_cpuid];
566 #else
567                 pcielist = &pcie_list[0];
568 #endif
569                 TAILQ_INIT(pcielist);
570                 for (i = 0; i < PCIE_CACHE; i++) {
571                         elem = &pcie_array[i];
572                         elem->vapage = va + (i * PAGE_SIZE);
573                         elem->papage = 0;
574                         TAILQ_INSERT_HEAD(pcielist, elem, elem);
575                 }
576         }
577
578         pcie_base = base;
579         pcie_minbus = minbus;
580         pcie_maxbus = maxbus;
581         cfgmech = CFGMECH_PCIE;
582         devmax = 32;
583
584         /*
585          * On some AMD systems, some of the devices on bus 0 are
586          * inaccessible using memory-mapped PCI config access.  Walk
587          * bus 0 looking for such devices.  For these devices, we will
588          * fall back to using type 1 config access instead.
589          */
590         if (pci_cfgregopen() != 0) {
591                 for (slot = 0; slot < 32; slot++) {
592                         val1 = pcireg_cfgread(0, slot, 0, 0, 4);
593                         if (val1 == 0xffffffff)
594                                 continue;
595
596                         val2 = pciereg_cfgread(0, slot, 0, 0, 4);
597                         if (val2 != val1)
598                                 pcie_badslots |= (1 << slot);
599                 }
600         }
601
602         return (1);
603 #else   /* !PCIE_CFG_MECH */
604         return (0);
605 #endif  /* PCIE_CFG_MECH */
606 }
607
608 #define PCIE_PADDR(bar, reg, bus, slot, func)   \
609         ((bar)                          |       \
610         (((bus) & 0xff) << 20)          |       \
611         (((slot) & 0x1f) << 15)         |       \
612         (((func) & 0x7) << 12)          |       \
613         ((reg) & 0xfff))
614
615 /*
616  * Find an element in the cache that matches the physical page desired, or
617  * create a new mapping from the least recently used element.
618  * A very simple LRU algorithm is used here, does it need to be more
619  * efficient?
620  */
621 static __inline struct pcie_cfg_elem *
622 pciereg_findelem(vm_paddr_t papage)
623 {
624         struct pcie_cfg_list *pcielist;
625         struct pcie_cfg_elem *elem;
626         pcielist = &pcie_list[mycpuid];
627         TAILQ_FOREACH(elem, pcielist, elem) {
628                 if (elem->papage == papage)
629                         break;
630         }
631
632         if (elem == NULL) {
633                 elem = TAILQ_LAST(pcielist, pcie_cfg_list);
634                 if (elem->papage != 0) {
635                         pmap_kremove(elem->vapage);
636                         cpu_invlpg(&elem->vapage);
637                 }
638                 pmap_kenter(elem->vapage, papage);
639                 elem->papage = papage;
640         }
641
642         if (elem != TAILQ_FIRST(pcielist)) {
643                 TAILQ_REMOVE(pcielist, elem, elem);
644                 TAILQ_INSERT_HEAD(pcielist, elem, elem);
645         }
646         return (elem);
647 }
648
649 static int
650 pciereg_cfgread(int bus, unsigned slot, unsigned func, unsigned reg,
651         unsigned bytes)
652 {
653         struct pcie_cfg_elem *elem;
654         volatile vm_offset_t va;
655         vm_paddr_t pa, papage;
656         int data = -1;
657
658         if (bus < pcie_minbus || bus > pcie_maxbus || slot >= 32 ||
659             func > PCI_FUNCMAX || reg >= 0x1000 || bytes > 4 || bytes == 3)
660                 return (-1);
661
662         crit_enter();
663         pa = PCIE_PADDR(pcie_base, reg, bus, slot, func);
664         papage = pa & ~PAGE_MASK;
665         elem = pciereg_findelem(papage);
666         va = elem->vapage | (pa & PAGE_MASK);
667
668         switch (bytes) {
669         case 4:
670                 data = *(volatile uint32_t *)(va);
671                 break;
672         case 2:
673                 data = *(volatile uint16_t *)(va);
674                 break;
675         case 1:
676                 data = *(volatile uint8_t *)(va);
677                 break;
678         }
679
680         crit_exit();
681         return (data);
682 }
683
684 static void
685 pciereg_cfgwrite(int bus, unsigned slot, unsigned func, unsigned reg, int data, unsigned bytes)
686 {
687         struct pcie_cfg_elem *elem;
688         volatile vm_offset_t va;
689         vm_paddr_t pa, papage;
690
691         crit_enter();
692         pa = PCIE_PADDR(pcie_base, reg, bus, slot, func);
693         papage = pa & ~PAGE_MASK;
694         elem = pciereg_findelem(papage);
695         va = elem->vapage | (pa & PAGE_MASK);
696
697         switch (bytes) {
698         case 4:
699                 *(volatile uint32_t *)(va) = data;
700                 break;
701         case 2:
702                 *(volatile uint16_t *)(va) = data;
703                 break;
704         case 1:
705                 *(volatile uint8_t *)(va) = data;
706                 break;
707         }
708
709         crit_exit();
710 }