Zephyr API Documentation  2.7.0-rc2
A Scalable Open Source RTOS
device_mmio.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2020 Intel Corporation.
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 *
6 * Definitions and helper macros for managing driver memory-mapped
7 * input/output (MMIO) regions appropriately in either RAM or ROM.
8 *
9 * In most cases drivers will just want to include device.h, but
10 * including this separately may be needed for arch-level driver code
11 * which uses the DEVICE_MMIO_TOPLEVEL variants and including the
12 * main device.h would introduce header dependency loops due to that
13 * header's reliance on kernel.h.
14 */
15#ifndef ZEPHYR_INCLUDE_SYS_DEVICE_MMIO_H
16#define ZEPHYR_INCLUDE_SYS_DEVICE_MMIO_H
17
18#include <toolchain/common.h>
19#include <linker/sections.h>
20
27/* Storing MMIO addresses in RAM is a system-wide decision based on
28 * configuration. This is just used to simplify some other definitions.
29 *
30 * If we have an MMU enabled, all physical MMIO regions must be mapped into
31 * the kernel's virtual address space at runtime, this is a hard requirement.
32 *
33 * If we have PCIE enabled, this does mean that non-PCIE drivers may waste
34 * a bit of RAM, but systems with PCI express are not RAM constrained.
35 */
36#if defined(CONFIG_MMU) || defined(CONFIG_PCIE)
37#define DEVICE_MMIO_IS_IN_RAM
38#endif
39
40#ifndef _ASMLANGUAGE
41#include <stdint.h>
42#include <stddef.h>
43#include <sys/mem_manage.h>
44#include <sys/sys_io.h>
45
46#ifdef DEVICE_MMIO_IS_IN_RAM
47/* Store the physical address and size from DTS, we'll memory
48 * map into the virtual address space at runtime. This is not applicable
49 * to PCIe devices, which must query the bus for BAR information.
50 */
51struct z_device_mmio_rom {
53 uintptr_t phys_addr;
54
56 size_t size;
57};
58
59#define Z_DEVICE_MMIO_ROM_INITIALIZER(node_id) \
60 { \
61 .phys_addr = DT_REG_ADDR(node_id), \
62 .size = DT_REG_SIZE(node_id) \
63 }
64
85__boot_func
86static inline void device_map(mm_reg_t *virt_addr, uintptr_t phys_addr,
87 size_t size, uint32_t flags)
88{
89#ifdef CONFIG_MMU
90 /* Pass along flags and add that we want supervisor mode
91 * read-write access.
92 */
93 z_phys_map((uint8_t **)virt_addr, phys_addr, size,
95#else
96 ARG_UNUSED(size);
97 ARG_UNUSED(flags);
98
99 *virt_addr = phys_addr;
100#endif /* CONFIG_MMU */
101}
102#else
103/* No MMU or PCIe. Just store the address from DTS and treat as a linear
104 * address
105 */
106struct z_device_mmio_rom {
108 mm_reg_t addr;
109};
110
111#define Z_DEVICE_MMIO_ROM_INITIALIZER(node_id) \
112 { \
113 .addr = DT_REG_ADDR(node_id) \
114 }
115#endif /* DEVICE_MMIO_IS_IN_RAM */
116#endif /* !_ASMLANGUAGE */
156#ifdef DEVICE_MMIO_IS_IN_RAM
157#define DEVICE_MMIO_RAM mm_reg_t _mmio
158#else
159#define DEVICE_MMIO_RAM
160#endif
161
162#ifdef DEVICE_MMIO_IS_IN_RAM
175#define DEVICE_MMIO_RAM_PTR(device) (mm_reg_t *)((device)->data)
176#endif /* DEVICE_MMIO_IS_IN_RAM */
177
206#define DEVICE_MMIO_ROM struct z_device_mmio_rom _mmio
207
219#define DEVICE_MMIO_ROM_PTR(dev) \
220 ((struct z_device_mmio_rom *)((dev)->config))
221
242#define DEVICE_MMIO_ROM_INIT(node_id) \
243 ._mmio = Z_DEVICE_MMIO_ROM_INITIALIZER(node_id)
244
261#ifdef DEVICE_MMIO_IS_IN_RAM
262#define DEVICE_MMIO_MAP(dev, flags) \
263 device_map(DEVICE_MMIO_RAM_PTR(dev), \
264 DEVICE_MMIO_ROM_PTR(dev)->phys_addr, \
265 DEVICE_MMIO_ROM_PTR(dev)->size, \
266 (flags))
267#else
268#define DEVICE_MMIO_MAP(dev, flags) do { } while (0)
269#endif
270
290#ifdef DEVICE_MMIO_IS_IN_RAM
291#define DEVICE_MMIO_GET(dev) (*DEVICE_MMIO_RAM_PTR(dev))
292#else
293#define DEVICE_MMIO_GET(dev) (DEVICE_MMIO_ROM_PTR(dev)->addr)
294#endif
337#ifdef DEVICE_MMIO_IS_IN_RAM
338#define DEVICE_MMIO_NAMED_RAM(name) mm_reg_t name
339#else
340#define DEVICE_MMIO_NAMED_RAM(name)
341#endif /* DEVICE_MMIO_IS_IN_RAM */
342
343#ifdef DEVICE_MMIO_IS_IN_RAM
356#define DEVICE_MMIO_NAMED_RAM_PTR(dev, name) \
357 (&(DEV_DATA(dev)->name))
358#endif /* DEVICE_MMIO_IS_IN_RAM */
359
393#define DEVICE_MMIO_NAMED_ROM(name) struct z_device_mmio_rom name
394
409#define DEVICE_MMIO_NAMED_ROM_PTR(dev, name) (&(DEV_CFG(dev)->name))
410
435#define DEVICE_MMIO_NAMED_ROM_INIT(name, node_id) \
436 .name = Z_DEVICE_MMIO_ROM_INITIALIZER(node_id)
437
466#ifdef DEVICE_MMIO_IS_IN_RAM
467#define DEVICE_MMIO_NAMED_MAP(dev, name, flags) \
468 device_map(DEVICE_MMIO_NAMED_RAM_PTR((dev), name), \
469 (DEVICE_MMIO_NAMED_ROM_PTR((dev), name)->phys_addr), \
470 (DEVICE_MMIO_NAMED_ROM_PTR((dev), name)->size), \
471 (flags))
472#else
473#define DEVICE_MMIO_NAMED_MAP(dev, name, flags) do { } while (0)
474#endif
475
497#ifdef DEVICE_MMIO_IS_IN_RAM
498#define DEVICE_MMIO_NAMED_GET(dev, name) \
499 (*DEVICE_MMIO_NAMED_RAM_PTR((dev), name))
500#else
501#define DEVICE_MMIO_NAMED_GET(dev, name) \
502 ((DEVICE_MMIO_NAMED_ROM_PTR((dev), name))->addr)
503#endif /* DEVICE_MMIO_IS_IN_RAM */
504
524 #define Z_TOPLEVEL_ROM_NAME(name) _CONCAT(z_mmio_rom__, name)
525 #define Z_TOPLEVEL_RAM_NAME(name) _CONCAT(z_mmio_ram__, name)
526
542#ifdef DEVICE_MMIO_IS_IN_RAM
543#define DEVICE_MMIO_TOPLEVEL(name, node_id) \
544 __pinned_bss \
545 mm_reg_t Z_TOPLEVEL_RAM_NAME(name); \
546 __pinned_rodata \
547 const struct z_device_mmio_rom Z_TOPLEVEL_ROM_NAME(name) = \
548 Z_DEVICE_MMIO_ROM_INITIALIZER(node_id)
549#else
550#define DEVICE_MMIO_TOPLEVEL(name, node_id) \
551 __pinned_rodata \
552 const struct z_device_mmio_rom Z_TOPLEVEL_ROM_NAME(name) = \
553 Z_DEVICE_MMIO_ROM_INITIALIZER(node_id)
554#endif /* DEVICE_MMIO_IS_IN_RAM */
555
570#ifdef DEVICE_MMIO_IS_IN_RAM
571#define DEVICE_MMIO_TOPLEVEL_DECLARE(name) \
572 extern mm_reg_t Z_TOPLEVEL_RAM_NAME(name); \
573 extern const struct z_device_mmio_rom Z_TOPLEVEL_ROM_NAME(name)
574#else
575#define DEVICE_MMIO_TOPLEVEL_DECLARE(name) \
576 extern const struct z_device_mmio_rom Z_TOPLEVEL_ROM_NAME(name)
577#endif /* DEVICE_MMIO_IS_IN_RAM */
578
593#ifdef DEVICE_MMIO_IS_IN_RAM
594#define DEVICE_MMIO_TOPLEVEL_STATIC(name, node_id) \
595 __pinned_bss \
596 static mm_reg_t Z_TOPLEVEL_RAM_NAME(name); \
597 __pinned_rodata \
598 static const struct z_device_mmio_rom Z_TOPLEVEL_ROM_NAME(name) = \
599 Z_DEVICE_MMIO_ROM_INITIALIZER(node_id)
600#else
601#define DEVICE_MMIO_TOPLEVEL_STATIC(name, node_id) \
602 __pinned_rodata \
603 static const struct z_device_mmio_rom Z_TOPLEVEL_ROM_NAME(name) = \
604 Z_DEVICE_MMIO_ROM_INITIALIZER(node_id)
605#endif /* DEVICE_MMIO_IS_IN_RAM */
606
607#ifdef DEVICE_MMIO_IS_IN_RAM
617#define DEVICE_MMIO_TOPLEVEL_RAM_PTR(name) &Z_TOPLEVEL_RAM_NAME(name)
618#endif /* DEVICE_MMIO_IS_IN_RAM */
619
628#define DEVICE_MMIO_TOPLEVEL_ROM_PTR(name) &Z_TOPLEVEL_ROM_NAME(name)
629
651#ifdef DEVICE_MMIO_IS_IN_RAM
652#define DEVICE_MMIO_TOPLEVEL_MAP(name, flags) \
653 device_map(&Z_TOPLEVEL_RAM_NAME(name), \
654 Z_TOPLEVEL_ROM_NAME(name).phys_addr, \
655 Z_TOPLEVEL_ROM_NAME(name).size, flags)
656#else
657#define DEVICE_MMIO_TOPLEVEL_MAP(name, flags) do { } while (0)
658#endif
659
670#ifdef DEVICE_MMIO_IS_IN_RAM
671#define DEVICE_MMIO_TOPLEVEL_GET(name) \
672 ((mm_reg_t)Z_TOPLEVEL_RAM_NAME(name))
673#else
674#define DEVICE_MMIO_TOPLEVEL_GET(name) \
675 ((mm_reg_t)Z_TOPLEVEL_ROM_NAME(name).addr)
676#endif
679#endif /* ZEPHYR_INCLUDE_SYS_DEVICE_MMIO_H */
Common toolchain abstraction.
static __boot_func void device_map(mm_reg_t *virt_addr, uintptr_t phys_addr, size_t size, uint32_t flags)
Definition: device_mmio.h:86
flags
Definition: http_parser.h:131
#define K_MEM_PERM_RW
Definition: mem_manage.h:34
Definitions of various linker Sections.
__UINT32_TYPE__ uint32_t
Definition: stdint.h:60
__UINT8_TYPE__ uint8_t
Definition: stdint.h:58
__UINTPTR_TYPE__ uintptr_t
Definition: stdint.h:75
uintptr_t mm_reg_t
Definition: sys_io.h:20