GNU Linux-libre 4.19.245-gnu1
[releases.git] / arch / powerpc / include / asm / drmem.h
1 /*
2  * drmem.h: Power specific logical memory block representation
3  *
4  * Copyright 2017 IBM Corporation
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version
9  * 2 of the License, or (at your option) any later version.
10  */
11
12 #ifndef _ASM_POWERPC_LMB_H
13 #define _ASM_POWERPC_LMB_H
14
15 #include <linux/sched.h>
16
17 struct drmem_lmb {
18         u64     base_addr;
19         u32     drc_index;
20         u32     aa_index;
21         u32     flags;
22 };
23
24 struct drmem_lmb_info {
25         struct drmem_lmb        *lmbs;
26         int                     n_lmbs;
27         u64                     lmb_size;
28 };
29
30 extern struct drmem_lmb_info *drmem_info;
31
32 static inline struct drmem_lmb *drmem_lmb_next(struct drmem_lmb *lmb,
33                                                const struct drmem_lmb *start)
34 {
35         /*
36          * DLPAR code paths can take several milliseconds per element
37          * when interacting with firmware. Ensure that we don't
38          * unfairly monopolize the CPU.
39          */
40         if (((++lmb - start) % 16) == 0)
41                 cond_resched();
42
43         return lmb;
44 }
45
46 #define for_each_drmem_lmb_in_range(lmb, start, end)            \
47         for ((lmb) = (start); (lmb) < (end); lmb = drmem_lmb_next(lmb, start))
48
49 #define for_each_drmem_lmb(lmb)                                 \
50         for_each_drmem_lmb_in_range((lmb),                      \
51                 &drmem_info->lmbs[0],                           \
52                 &drmem_info->lmbs[drmem_info->n_lmbs])
53
54 /*
55  * The of_drconf_cell_v1 struct defines the layout of the LMB data
56  * specified in the ibm,dynamic-memory device tree property.
57  * The property itself is a 32-bit value specifying the number of
58  * LMBs followed by an array of of_drconf_cell_v1 entries, one
59  * per LMB.
60  */
61 struct of_drconf_cell_v1 {
62         __be64  base_addr;
63         __be32  drc_index;
64         __be32  reserved;
65         __be32  aa_index;
66         __be32  flags;
67 };
68
69 /*
70  * Version 2 of the ibm,dynamic-memory property is defined as a
71  * 32-bit value specifying the number of LMB sets followed by an
72  * array of of_drconf_cell_v2 entries, one per LMB set.
73  */
74 struct of_drconf_cell_v2 {
75         u32     seq_lmbs;
76         u64     base_addr;
77         u32     drc_index;
78         u32     aa_index;
79         u32     flags;
80 } __packed;
81
82 #define DRCONF_MEM_ASSIGNED     0x00000008
83 #define DRCONF_MEM_AI_INVALID   0x00000040
84 #define DRCONF_MEM_RESERVED     0x00000080
85
86 static inline u64 drmem_lmb_size(void)
87 {
88         return drmem_info->lmb_size;
89 }
90
91 #define DRMEM_LMB_RESERVED      0x80000000
92
93 static inline void drmem_mark_lmb_reserved(struct drmem_lmb *lmb)
94 {
95         lmb->flags |= DRMEM_LMB_RESERVED;
96 }
97
98 static inline void drmem_remove_lmb_reservation(struct drmem_lmb *lmb)
99 {
100         lmb->flags &= ~DRMEM_LMB_RESERVED;
101 }
102
103 static inline bool drmem_lmb_reserved(struct drmem_lmb *lmb)
104 {
105         return lmb->flags & DRMEM_LMB_RESERVED;
106 }
107
108 u64 drmem_lmb_memory_max(void);
109 void __init walk_drmem_lmbs(struct device_node *dn,
110                         void (*func)(struct drmem_lmb *, const __be32 **));
111 int drmem_update_dt(void);
112
113 #ifdef CONFIG_PPC_PSERIES
114 void __init walk_drmem_lmbs_early(unsigned long node,
115                         void (*func)(struct drmem_lmb *, const __be32 **));
116 #endif
117
118 static inline void invalidate_lmb_associativity_index(struct drmem_lmb *lmb)
119 {
120         lmb->aa_index = 0xffffffff;
121 }
122
123 #endif /* _ASM_POWERPC_LMB_H */