blob: 3c4afdf9f0641879873e5c2e144b50ec24503fed [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001/* SPDX-License-Identifier: GPL-2.0+ */
Kumar Gala4ed65522008-02-27 21:51:47 -06002#ifndef _LINUX_LMB_H
3#define _LINUX_LMB_H
4#ifdef __KERNEL__
5
6#include <asm/types.h>
Simon Goldschmidt9cc23232019-01-26 22:13:04 +01007#include <asm/u-boot.h>
8
Kumar Gala4ed65522008-02-27 21:51:47 -06009/*
10 * Logical memory blocks.
11 *
12 * Copyright (C) 2001 Peter Bergner, IBM Corp.
Kumar Gala4ed65522008-02-27 21:51:47 -060013 */
14
Patrick Delaunay6d665022021-03-10 10:16:31 +010015/**
Patrick Delaunay59c0ea52021-05-07 14:50:29 +020016 * enum lmb_flags - definition of memory region attributes
17 * @LMB_NONE: no special request
18 * @LMB_NOMAP: don't add to mmu configuration
19 */
20enum lmb_flags {
21 LMB_NONE = 0x0,
22 LMB_NOMAP = 0x4,
23};
24
25/**
Patrick Delaunay6d665022021-03-10 10:16:31 +010026 * struct lmb_property - Description of one region.
27 *
28 * @base: Base address of the region.
29 * @size: Size of the region
30 */
Kumar Gala4ed65522008-02-27 21:51:47 -060031struct lmb_property {
Becky Bruce391fd932008-06-09 20:37:18 -050032 phys_addr_t base;
33 phys_size_t size;
Patrick Delaunay59c0ea52021-05-07 14:50:29 +020034 enum lmb_flags flags;
Kumar Gala4ed65522008-02-27 21:51:47 -060035};
36
Patrick Delaunay6d665022021-03-10 10:16:31 +010037/**
38 * struct lmb_region - Description of a set of region.
39 *
40 * @cnt: Number of regions.
41 * @max: Size of the region array, max value of cnt.
42 * @region: Array of the region properties
43 */
Kumar Gala4ed65522008-02-27 21:51:47 -060044struct lmb_region {
45 unsigned long cnt;
Patrick Delaunay00fd8da2021-03-10 10:16:27 +010046 unsigned long max;
Patrick Delaunay6d665022021-03-10 10:16:31 +010047#if IS_ENABLED(CONFIG_LMB_USE_MAX_REGIONS)
Patrick Delaunaycb1e6192021-03-10 10:16:29 +010048 struct lmb_property region[CONFIG_LMB_MAX_REGIONS];
Patrick Delaunay6d665022021-03-10 10:16:31 +010049#else
50 struct lmb_property *region;
51#endif
Kumar Gala4ed65522008-02-27 21:51:47 -060052};
53
Patrick Delaunay6d665022021-03-10 10:16:31 +010054/**
55 * struct lmb - Logical memory block handle.
56 *
57 * Clients provide storage for Logical memory block (lmb) handles.
58 * The content of the structure is managed by the lmb library.
59 * A lmb struct is initialized by lmb_init() functions.
60 * The lmb struct is passed to all other lmb APIs.
61 *
62 * @memory: Description of memory regions.
63 * @reserved: Description of reserved regions.
64 * @memory_regions: Array of the memory regions (statically allocated)
65 * @reserved_regions: Array of the reserved regions (statically allocated)
66 */
Kumar Gala4ed65522008-02-27 21:51:47 -060067struct lmb {
68 struct lmb_region memory;
69 struct lmb_region reserved;
Patrick Delaunay6d665022021-03-10 10:16:31 +010070#if !IS_ENABLED(CONFIG_LMB_USE_MAX_REGIONS)
71 struct lmb_property memory_regions[CONFIG_LMB_MEMORY_REGIONS];
72 struct lmb_property reserved_regions[CONFIG_LMB_RESERVED_REGIONS];
73#endif
Kumar Gala4ed65522008-02-27 21:51:47 -060074};
75
Kumar Gala4ed65522008-02-27 21:51:47 -060076extern void lmb_init(struct lmb *lmb);
Masahiro Yamadab75d8dc2020-06-26 15:13:33 +090077extern void lmb_init_and_reserve(struct lmb *lmb, struct bd_info *bd,
78 void *fdt_blob);
Simon Goldschmidt9cc23232019-01-26 22:13:04 +010079extern void lmb_init_and_reserve_range(struct lmb *lmb, phys_addr_t base,
80 phys_size_t size, void *fdt_blob);
Becky Bruce391fd932008-06-09 20:37:18 -050081extern long lmb_add(struct lmb *lmb, phys_addr_t base, phys_size_t size);
82extern long lmb_reserve(struct lmb *lmb, phys_addr_t base, phys_size_t size);
Patrick Delaunay59c0ea52021-05-07 14:50:29 +020083/**
84 * lmb_reserve_flags - Reserve one region with a specific flags bitfield.
85 *
86 * @lmb the logical memory block struct
87 * @base base address of the memory region
88 * @size size of the memory region
89 * @flags flags for the memory region
90 * @return 0 if OK, > 0 for coalesced region or a negative error code.
91 */
92long lmb_reserve_flags(struct lmb *lmb, phys_addr_t base,
93 phys_size_t size, enum lmb_flags flags);
Becky Bruce391fd932008-06-09 20:37:18 -050094extern phys_addr_t lmb_alloc(struct lmb *lmb, phys_size_t size, ulong align);
95extern phys_addr_t lmb_alloc_base(struct lmb *lmb, phys_size_t size, ulong align,
96 phys_addr_t max_addr);
97extern phys_addr_t __lmb_alloc_base(struct lmb *lmb, phys_size_t size, ulong align,
98 phys_addr_t max_addr);
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +010099extern phys_addr_t lmb_alloc_addr(struct lmb *lmb, phys_addr_t base,
100 phys_size_t size);
Simon Goldschmidt65304aa2019-01-21 20:29:55 +0100101extern phys_size_t lmb_get_free_size(struct lmb *lmb, phys_addr_t addr);
Becky Bruce391fd932008-06-09 20:37:18 -0500102extern int lmb_is_reserved(struct lmb *lmb, phys_addr_t addr);
Patrick Delaunaye359a4a2021-05-07 14:50:30 +0200103/**
104 * lmb_is_reserved_flags - test if tha address is in reserved region with a bitfield flag
105 *
106 * @lmb the logical memory block struct
107 * @addr address to be tested
108 * @flags flags bitfied to be tested
109 * @return 0 if not reserved or reserved without the requested flag else 1
110 */
111int lmb_is_reserved_flags(struct lmb *lmb, phys_addr_t addr, int flags);
Andy Fleming98874ff2008-07-07 14:24:39 -0500112extern long lmb_free(struct lmb *lmb, phys_addr_t base, phys_size_t size);
Kumar Gala4ed65522008-02-27 21:51:47 -0600113
114extern void lmb_dump_all(struct lmb *lmb);
Tero Kristo9996cea2020-07-20 11:10:45 +0300115extern void lmb_dump_all_force(struct lmb *lmb);
Kumar Gala4ed65522008-02-27 21:51:47 -0600116
Becky Bruce391fd932008-06-09 20:37:18 -0500117static inline phys_size_t
Kumar Gala4ed65522008-02-27 21:51:47 -0600118lmb_size_bytes(struct lmb_region *type, unsigned long region_nr)
119{
120 return type->region[region_nr].size;
121}
Mike Frysingera16028d2009-11-03 11:35:59 -0500122
123void board_lmb_reserve(struct lmb *lmb);
124void arch_lmb_reserve(struct lmb *lmb);
125
Patrick Delaunay59c0ea52021-05-07 14:50:29 +0200126/* Low level functions */
127
128static inline bool lmb_is_nomap(struct lmb_property *m)
129{
130 return m->flags & LMB_NOMAP;
131}
132
Kumar Gala4ed65522008-02-27 21:51:47 -0600133#endif /* __KERNEL__ */
134
135#endif /* _LINUX_LMB_H */