blob: 5efdf9d2e8f17f8bed6b0c1454f4ea7b93aa48d0 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001/* SPDX-License-Identifier: GPL-2.0+ */
Kumar Gala6d7bfa82008-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 Goldschmidt8890e7d2019-01-26 22:13:04 +01007#include <asm/u-boot.h>
8
Kumar Gala6d7bfa82008-02-27 21:51:47 -06009/*
10 * Logical memory blocks.
11 *
12 * Copyright (C) 2001 Peter Bergner, IBM Corp.
Kumar Gala6d7bfa82008-02-27 21:51:47 -060013 */
14
Patrick Delaunay71cc9c52021-03-10 10:16:31 +010015/**
Patrick Delaunaye11c9082021-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 Delaunay71cc9c52021-03-10 10:16:31 +010026 * struct lmb_property - Description of one region.
27 *
Heinrich Schuchardte6923f02021-11-14 08:43:07 +010028 * @base: Base address of the region.
29 * @size: Size of the region
30 * @flags: memory region attributes
Patrick Delaunay71cc9c52021-03-10 10:16:31 +010031 */
Kumar Gala6d7bfa82008-02-27 21:51:47 -060032struct lmb_property {
Becky Bruced26d67c2008-06-09 20:37:18 -050033 phys_addr_t base;
34 phys_size_t size;
Patrick Delaunaye11c9082021-05-07 14:50:29 +020035 enum lmb_flags flags;
Kumar Gala6d7bfa82008-02-27 21:51:47 -060036};
37
Patrick Delaunay71cc9c52021-03-10 10:16:31 +010038/**
39 * struct lmb_region - Description of a set of region.
40 *
41 * @cnt: Number of regions.
42 * @max: Size of the region array, max value of cnt.
43 * @region: Array of the region properties
44 */
Kumar Gala6d7bfa82008-02-27 21:51:47 -060045struct lmb_region {
46 unsigned long cnt;
Patrick Delaunay8c69fc22021-03-10 10:16:27 +010047 unsigned long max;
Patrick Delaunay71cc9c52021-03-10 10:16:31 +010048#if IS_ENABLED(CONFIG_LMB_USE_MAX_REGIONS)
Patrick Delaunay8b829302021-03-10 10:16:29 +010049 struct lmb_property region[CONFIG_LMB_MAX_REGIONS];
Patrick Delaunay71cc9c52021-03-10 10:16:31 +010050#else
51 struct lmb_property *region;
52#endif
Kumar Gala6d7bfa82008-02-27 21:51:47 -060053};
54
Patrick Delaunay71cc9c52021-03-10 10:16:31 +010055/**
56 * struct lmb - Logical memory block handle.
57 *
58 * Clients provide storage for Logical memory block (lmb) handles.
59 * The content of the structure is managed by the lmb library.
60 * A lmb struct is initialized by lmb_init() functions.
61 * The lmb struct is passed to all other lmb APIs.
62 *
63 * @memory: Description of memory regions.
64 * @reserved: Description of reserved regions.
65 * @memory_regions: Array of the memory regions (statically allocated)
66 * @reserved_regions: Array of the reserved regions (statically allocated)
67 */
Kumar Gala6d7bfa82008-02-27 21:51:47 -060068struct lmb {
69 struct lmb_region memory;
70 struct lmb_region reserved;
Patrick Delaunay71cc9c52021-03-10 10:16:31 +010071#if !IS_ENABLED(CONFIG_LMB_USE_MAX_REGIONS)
72 struct lmb_property memory_regions[CONFIG_LMB_MEMORY_REGIONS];
73 struct lmb_property reserved_regions[CONFIG_LMB_RESERVED_REGIONS];
74#endif
Kumar Gala6d7bfa82008-02-27 21:51:47 -060075};
76
Kumar Gala6d7bfa82008-02-27 21:51:47 -060077extern void lmb_init(struct lmb *lmb);
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +090078extern void lmb_init_and_reserve(struct lmb *lmb, struct bd_info *bd,
79 void *fdt_blob);
Simon Goldschmidt8890e7d2019-01-26 22:13:04 +010080extern void lmb_init_and_reserve_range(struct lmb *lmb, phys_addr_t base,
81 phys_size_t size, void *fdt_blob);
Becky Bruced26d67c2008-06-09 20:37:18 -050082extern long lmb_add(struct lmb *lmb, phys_addr_t base, phys_size_t size);
83extern long lmb_reserve(struct lmb *lmb, phys_addr_t base, phys_size_t size);
Patrick Delaunaye11c9082021-05-07 14:50:29 +020084/**
85 * lmb_reserve_flags - Reserve one region with a specific flags bitfield.
86 *
Heinrich Schuchardte6923f02021-11-14 08:43:07 +010087 * @lmb: the logical memory block struct
88 * @base: base address of the memory region
89 * @size: size of the memory region
90 * @flags: flags for the memory region
91 * Return: 0 if OK, > 0 for coalesced region or a negative error code.
Patrick Delaunaye11c9082021-05-07 14:50:29 +020092 */
93long lmb_reserve_flags(struct lmb *lmb, phys_addr_t base,
94 phys_size_t size, enum lmb_flags flags);
Becky Bruced26d67c2008-06-09 20:37:18 -050095extern phys_addr_t lmb_alloc(struct lmb *lmb, phys_size_t size, ulong align);
96extern phys_addr_t lmb_alloc_base(struct lmb *lmb, phys_size_t size, ulong align,
97 phys_addr_t max_addr);
98extern phys_addr_t __lmb_alloc_base(struct lmb *lmb, phys_size_t size, ulong align,
99 phys_addr_t max_addr);
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100100extern phys_addr_t lmb_alloc_addr(struct lmb *lmb, phys_addr_t base,
101 phys_size_t size);
Simon Goldschmidt7510a562019-01-21 20:29:55 +0100102extern phys_size_t lmb_get_free_size(struct lmb *lmb, phys_addr_t addr);
Becky Bruced26d67c2008-06-09 20:37:18 -0500103extern int lmb_is_reserved(struct lmb *lmb, phys_addr_t addr);
Patrick Delaunaydb2c9aa2021-05-07 14:50:30 +0200104/**
105 * lmb_is_reserved_flags - test if tha address is in reserved region with a bitfield flag
106 *
Heinrich Schuchardte6923f02021-11-14 08:43:07 +0100107 * @lmb: the logical memory block struct
108 * @addr: address to be tested
109 * @flags: flags bitfied to be tested
110 * Return: if not reserved or reserved without the requested flag else 1
Patrick Delaunaydb2c9aa2021-05-07 14:50:30 +0200111 */
112int lmb_is_reserved_flags(struct lmb *lmb, phys_addr_t addr, int flags);
Andy Fleming09d2a712008-07-07 14:24:39 -0500113extern long lmb_free(struct lmb *lmb, phys_addr_t base, phys_size_t size);
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600114
115extern void lmb_dump_all(struct lmb *lmb);
Tero Kristo97c418b2020-07-20 11:10:45 +0300116extern void lmb_dump_all_force(struct lmb *lmb);
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600117
Becky Bruced26d67c2008-06-09 20:37:18 -0500118static inline phys_size_t
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600119lmb_size_bytes(struct lmb_region *type, unsigned long region_nr)
120{
121 return type->region[region_nr].size;
122}
Mike Frysingera0dadf82009-11-03 11:35:59 -0500123
124void board_lmb_reserve(struct lmb *lmb);
125void arch_lmb_reserve(struct lmb *lmb);
Marek Vasuta2eec022021-09-10 22:47:09 +0200126void arch_lmb_reserve_generic(struct lmb *lmb, ulong sp, ulong end, ulong align);
Mike Frysingera0dadf82009-11-03 11:35:59 -0500127
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600128#endif /* __KERNEL__ */
129
130#endif /* _LINUX_LMB_H */