| 1 | /* |
| 2 | * Dynamic reconfiguration memory support |
| 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 | #define pr_fmt(fmt) "drmem: " fmt |
| 13 | |
| 14 | #include <linux/kernel.h> |
| 15 | #include <linux/of.h> |
| 16 | #include <linux/of_fdt.h> |
| 17 | #include <linux/memblock.h> |
| 18 | #include <asm/prom.h> |
| 19 | #include <asm/drmem.h> |
| 20 | |
| 21 | static struct drmem_lmb_info __drmem_info; |
| 22 | struct drmem_lmb_info *drmem_info = &__drmem_info; |
| 23 | |
| 24 | u64 drmem_lmb_memory_max(void) |
| 25 | { |
| 26 | struct drmem_lmb *last_lmb; |
| 27 | |
| 28 | last_lmb = &drmem_info->lmbs[drmem_info->n_lmbs - 1]; |
| 29 | return last_lmb->base_addr + drmem_lmb_size(); |
| 30 | } |
| 31 | |
| 32 | static void __init read_drconf_v1_cell(struct drmem_lmb *lmb, |
| 33 | const __be32 **prop) |
| 34 | { |
| 35 | const __be32 *p = *prop; |
| 36 | |
| 37 | lmb->base_addr = dt_mem_next_cell(dt_root_addr_cells, &p); |
| 38 | lmb->drc_index = of_read_number(p++, 1); |
| 39 | |
| 40 | p++; /* skip reserved field */ |
| 41 | |
| 42 | lmb->aa_index = of_read_number(p++, 1); |
| 43 | lmb->flags = of_read_number(p++, 1); |
| 44 | |
| 45 | *prop = p; |
| 46 | } |
| 47 | |
| 48 | static void __init __walk_drmem_v1_lmbs(const __be32 *prop, const __be32 *usm, |
| 49 | void (*func)(struct drmem_lmb *, const __be32 **)) |
| 50 | { |
| 51 | struct drmem_lmb lmb; |
| 52 | u32 i, n_lmbs; |
| 53 | |
| 54 | n_lmbs = of_read_number(prop++, 1); |
| 55 | |
| 56 | for (i = 0; i < n_lmbs; i++) { |
| 57 | read_drconf_v1_cell(&lmb, &prop); |
| 58 | func(&lmb, &usm); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | #ifdef CONFIG_PPC_PSERIES |
| 63 | void __init walk_drmem_lmbs_early(unsigned long node, |
| 64 | void (*func)(struct drmem_lmb *, const __be32 **)) |
| 65 | { |
| 66 | const __be32 *prop, *usm; |
| 67 | int len; |
| 68 | |
| 69 | prop = of_get_flat_dt_prop(node, "ibm,lmb-size", &len); |
| 70 | if (!prop || len < dt_root_size_cells * sizeof(__be32)) |
| 71 | return; |
| 72 | |
| 73 | drmem_info->lmb_size = dt_mem_next_cell(dt_root_size_cells, &prop); |
| 74 | |
| 75 | usm = of_get_flat_dt_prop(node, "linux,drconf-usable-memory", &len); |
| 76 | |
| 77 | prop = of_get_flat_dt_prop(node, "ibm,dynamic-memory", &len); |
| 78 | if (prop) |
| 79 | __walk_drmem_v1_lmbs(prop, usm, func); |
| 80 | |
| 81 | memblock_dump_all(); |
| 82 | } |
| 83 | |
| 84 | #endif |
| 85 | |
| 86 | static int __init init_drmem_lmb_size(struct device_node *dn) |
| 87 | { |
| 88 | const __be32 *prop; |
| 89 | int len; |
| 90 | |
| 91 | if (drmem_info->lmb_size) |
| 92 | return 0; |
| 93 | |
| 94 | prop = of_get_property(dn, "ibm,lmb-size", &len); |
| 95 | if (!prop || len < dt_root_size_cells * sizeof(__be32)) { |
| 96 | pr_info("Could not determine LMB size\n"); |
| 97 | return -1; |
| 98 | } |
| 99 | |
| 100 | drmem_info->lmb_size = dt_mem_next_cell(dt_root_size_cells, &prop); |
| 101 | return 0; |
| 102 | } |
| 103 | |
| 104 | /* |
| 105 | * Returns the property linux,drconf-usable-memory if |
| 106 | * it exists (the property exists only in kexec/kdump kernels, |
| 107 | * added by kexec-tools) |
| 108 | */ |
| 109 | static const __be32 *of_get_usable_memory(struct device_node *dn) |
| 110 | { |
| 111 | const __be32 *prop; |
| 112 | u32 len; |
| 113 | |
| 114 | prop = of_get_property(dn, "linux,drconf-usable-memory", &len); |
| 115 | if (!prop || len < sizeof(unsigned int)) |
| 116 | return NULL; |
| 117 | |
| 118 | return prop; |
| 119 | } |
| 120 | |
| 121 | void __init walk_drmem_lmbs(struct device_node *dn, |
| 122 | void (*func)(struct drmem_lmb *, const __be32 **)) |
| 123 | { |
| 124 | const __be32 *prop, *usm; |
| 125 | |
| 126 | if (init_drmem_lmb_size(dn)) |
| 127 | return; |
| 128 | |
| 129 | usm = of_get_usable_memory(dn); |
| 130 | |
| 131 | prop = of_get_property(dn, "ibm,dynamic-memory", NULL); |
| 132 | if (prop) |
| 133 | __walk_drmem_v1_lmbs(prop, usm, func); |
| 134 | } |
| 135 | |
| 136 | static void __init init_drmem_v1_lmbs(const __be32 *prop) |
| 137 | { |
| 138 | struct drmem_lmb *lmb; |
| 139 | |
| 140 | drmem_info->n_lmbs = of_read_number(prop++, 1); |
| 141 | |
| 142 | drmem_info->lmbs = kcalloc(drmem_info->n_lmbs, sizeof(*lmb), |
| 143 | GFP_KERNEL); |
| 144 | if (!drmem_info->lmbs) |
| 145 | return; |
| 146 | |
| 147 | for_each_drmem_lmb(lmb) |
| 148 | read_drconf_v1_cell(lmb, &prop); |
| 149 | } |
| 150 | |
| 151 | static int __init drmem_init(void) |
| 152 | { |
| 153 | struct device_node *dn; |
| 154 | const __be32 *prop; |
| 155 | |
| 156 | dn = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory"); |
| 157 | if (!dn) { |
| 158 | pr_info("No dynamic reconfiguration memory found\n"); |
| 159 | return 0; |
| 160 | } |
| 161 | |
| 162 | if (init_drmem_lmb_size(dn)) { |
| 163 | of_node_put(dn); |
| 164 | return 0; |
| 165 | } |
| 166 | |
| 167 | prop = of_get_property(dn, "ibm,dynamic-memory", NULL); |
| 168 | if (prop) |
| 169 | init_drmem_v1_lmbs(prop); |
| 170 | |
| 171 | of_node_put(dn); |
| 172 | return 0; |
| 173 | } |
| 174 | late_initcall(drmem_init); |