David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 1 | /* mdesc.c: Sun4V machine description handling. |
| 2 | * |
David S. Miller | 4a28333 | 2008-02-13 19:22:23 -0800 | [diff] [blame] | 3 | * Copyright (C) 2007, 2008 David S. Miller <davem@davemloft.net> |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 4 | */ |
| 5 | #include <linux/kernel.h> |
| 6 | #include <linux/types.h> |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 7 | #include <linux/memblock.h> |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 8 | #include <linux/log2.h> |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 9 | #include <linux/list.h> |
| 10 | #include <linux/slab.h> |
David S. Miller | 8f3fff2 | 2007-07-14 00:54:55 -0700 | [diff] [blame] | 11 | #include <linux/mm.h> |
David S. Miller | 0fdb7f9 | 2007-08-15 21:02:23 -0700 | [diff] [blame] | 12 | #include <linux/miscdevice.h> |
David S. Miller | adfe67d | 2009-12-11 01:16:46 -0800 | [diff] [blame] | 13 | #include <linux/bootmem.h> |
Paul Gortmaker | 7b64db6 | 2011-07-18 15:57:46 -0400 | [diff] [blame] | 14 | #include <linux/export.h> |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 15 | |
Sam Ravnborg | 6e6ab2e | 2008-12-26 15:33:07 -0800 | [diff] [blame] | 16 | #include <asm/cpudata.h> |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 17 | #include <asm/hypervisor.h> |
| 18 | #include <asm/mdesc.h> |
| 19 | #include <asm/prom.h> |
Paul Gortmaker | cd66bc4 | 2011-09-19 21:15:28 -0400 | [diff] [blame] | 20 | #include <asm/uaccess.h> |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 21 | #include <asm/oplib.h> |
| 22 | #include <asm/smp.h> |
| 23 | |
| 24 | /* Unlike the OBP device tree, the machine description is a full-on |
| 25 | * DAG. An arbitrary number of ARCs are possible from one |
| 26 | * node to other nodes and thus we can't use the OBP device_node |
| 27 | * data structure to represent these nodes inside of the kernel. |
| 28 | * |
| 29 | * Actually, it isn't even a DAG, because there are back pointers |
| 30 | * which create cycles in the graph. |
| 31 | * |
| 32 | * mdesc_hdr and mdesc_elem describe the layout of the data structure |
| 33 | * we get from the Hypervisor. |
| 34 | */ |
| 35 | struct mdesc_hdr { |
| 36 | u32 version; /* Transport version */ |
| 37 | u32 node_sz; /* node block size */ |
| 38 | u32 name_sz; /* name block size */ |
| 39 | u32 data_sz; /* data block size */ |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 40 | } __attribute__((aligned(16))); |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 41 | |
| 42 | struct mdesc_elem { |
| 43 | u8 tag; |
| 44 | #define MD_LIST_END 0x00 |
| 45 | #define MD_NODE 0x4e |
| 46 | #define MD_NODE_END 0x45 |
| 47 | #define MD_NOOP 0x20 |
| 48 | #define MD_PROP_ARC 0x61 |
| 49 | #define MD_PROP_VAL 0x76 |
| 50 | #define MD_PROP_STR 0x73 |
| 51 | #define MD_PROP_DATA 0x64 |
| 52 | u8 name_len; |
| 53 | u16 resv; |
| 54 | u32 name_offset; |
| 55 | union { |
| 56 | struct { |
| 57 | u32 data_len; |
| 58 | u32 data_offset; |
| 59 | } data; |
| 60 | u64 val; |
| 61 | } d; |
| 62 | }; |
| 63 | |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 64 | struct mdesc_mem_ops { |
| 65 | struct mdesc_handle *(*alloc)(unsigned int mdesc_size); |
| 66 | void (*free)(struct mdesc_handle *handle); |
| 67 | }; |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 68 | |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 69 | struct mdesc_handle { |
| 70 | struct list_head list; |
| 71 | struct mdesc_mem_ops *mops; |
| 72 | void *self_base; |
| 73 | atomic_t refcnt; |
| 74 | unsigned int handle_size; |
| 75 | struct mdesc_hdr mdesc; |
| 76 | }; |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 77 | |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 78 | static void mdesc_handle_init(struct mdesc_handle *hp, |
| 79 | unsigned int handle_size, |
| 80 | void *base) |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 81 | { |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 82 | BUG_ON(((unsigned long)&hp->mdesc) & (16UL - 1)); |
| 83 | |
| 84 | memset(hp, 0, handle_size); |
| 85 | INIT_LIST_HEAD(&hp->list); |
| 86 | hp->self_base = base; |
| 87 | atomic_set(&hp->refcnt, 1); |
| 88 | hp->handle_size = handle_size; |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 91 | static struct mdesc_handle * __init mdesc_memblock_alloc(unsigned int mdesc_size) |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 92 | { |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 93 | unsigned int handle_size, alloc_size; |
David S. Miller | 4a28333 | 2008-02-13 19:22:23 -0800 | [diff] [blame] | 94 | struct mdesc_handle *hp; |
| 95 | unsigned long paddr; |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 96 | |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 97 | handle_size = (sizeof(struct mdesc_handle) - |
| 98 | sizeof(struct mdesc_hdr) + |
| 99 | mdesc_size); |
| 100 | alloc_size = PAGE_ALIGN(handle_size); |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 101 | |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 102 | paddr = memblock_alloc(alloc_size, PAGE_SIZE); |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 103 | |
David S. Miller | 4a28333 | 2008-02-13 19:22:23 -0800 | [diff] [blame] | 104 | hp = NULL; |
| 105 | if (paddr) { |
| 106 | hp = __va(paddr); |
| 107 | mdesc_handle_init(hp, handle_size, hp); |
| 108 | } |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 109 | return hp; |
| 110 | } |
| 111 | |
David S. Miller | 3628aa0 | 2011-03-30 17:37:56 -0700 | [diff] [blame] | 112 | static void __init mdesc_memblock_free(struct mdesc_handle *hp) |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 113 | { |
David S. Miller | adfe67d | 2009-12-11 01:16:46 -0800 | [diff] [blame] | 114 | unsigned int alloc_size; |
| 115 | unsigned long start; |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 116 | |
| 117 | BUG_ON(atomic_read(&hp->refcnt) != 0); |
| 118 | BUG_ON(!list_empty(&hp->list)); |
| 119 | |
David S. Miller | adfe67d | 2009-12-11 01:16:46 -0800 | [diff] [blame] | 120 | alloc_size = PAGE_ALIGN(hp->handle_size); |
| 121 | start = __pa(hp); |
| 122 | free_bootmem_late(start, alloc_size); |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 123 | } |
| 124 | |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 125 | static struct mdesc_mem_ops memblock_mdesc_ops = { |
| 126 | .alloc = mdesc_memblock_alloc, |
| 127 | .free = mdesc_memblock_free, |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 128 | }; |
| 129 | |
| 130 | static struct mdesc_handle *mdesc_kmalloc(unsigned int mdesc_size) |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 131 | { |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 132 | unsigned int handle_size; |
Michal Hocko | f91e8d6 | 2015-04-14 15:47:11 -0700 | [diff] [blame] | 133 | struct mdesc_handle *hp; |
| 134 | unsigned long addr; |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 135 | void *base; |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 136 | |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 137 | handle_size = (sizeof(struct mdesc_handle) - |
| 138 | sizeof(struct mdesc_hdr) + |
| 139 | mdesc_size); |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 140 | |
Michal Hocko | f91e8d6 | 2015-04-14 15:47:11 -0700 | [diff] [blame] | 141 | /* |
| 142 | * Allocation has to succeed because mdesc update would be missed |
| 143 | * and such events are not retransmitted. |
| 144 | */ |
David S. Miller | 920c3ed | 2007-07-17 21:37:35 -0700 | [diff] [blame] | 145 | base = kmalloc(handle_size + 15, GFP_KERNEL | __GFP_NOFAIL); |
Michal Hocko | f91e8d6 | 2015-04-14 15:47:11 -0700 | [diff] [blame] | 146 | addr = (unsigned long)base; |
| 147 | addr = (addr + 15UL) & ~15UL; |
| 148 | hp = (struct mdesc_handle *) addr; |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 149 | |
Michal Hocko | f91e8d6 | 2015-04-14 15:47:11 -0700 | [diff] [blame] | 150 | mdesc_handle_init(hp, handle_size, base); |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 151 | |
Michal Hocko | f91e8d6 | 2015-04-14 15:47:11 -0700 | [diff] [blame] | 152 | return hp; |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 153 | } |
| 154 | |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 155 | static void mdesc_kfree(struct mdesc_handle *hp) |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 156 | { |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 157 | BUG_ON(atomic_read(&hp->refcnt) != 0); |
| 158 | BUG_ON(!list_empty(&hp->list)); |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 159 | |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 160 | kfree(hp->self_base); |
| 161 | } |
| 162 | |
| 163 | static struct mdesc_mem_ops kmalloc_mdesc_memops = { |
| 164 | .alloc = mdesc_kmalloc, |
| 165 | .free = mdesc_kfree, |
| 166 | }; |
| 167 | |
| 168 | static struct mdesc_handle *mdesc_alloc(unsigned int mdesc_size, |
| 169 | struct mdesc_mem_ops *mops) |
| 170 | { |
| 171 | struct mdesc_handle *hp = mops->alloc(mdesc_size); |
| 172 | |
| 173 | if (hp) |
| 174 | hp->mops = mops; |
| 175 | |
| 176 | return hp; |
| 177 | } |
| 178 | |
| 179 | static void mdesc_free(struct mdesc_handle *hp) |
| 180 | { |
| 181 | hp->mops->free(hp); |
| 182 | } |
| 183 | |
| 184 | static struct mdesc_handle *cur_mdesc; |
| 185 | static LIST_HEAD(mdesc_zombie_list); |
| 186 | static DEFINE_SPINLOCK(mdesc_lock); |
| 187 | |
| 188 | struct mdesc_handle *mdesc_grab(void) |
| 189 | { |
| 190 | struct mdesc_handle *hp; |
| 191 | unsigned long flags; |
| 192 | |
| 193 | spin_lock_irqsave(&mdesc_lock, flags); |
| 194 | hp = cur_mdesc; |
| 195 | if (hp) |
| 196 | atomic_inc(&hp->refcnt); |
| 197 | spin_unlock_irqrestore(&mdesc_lock, flags); |
| 198 | |
| 199 | return hp; |
| 200 | } |
| 201 | EXPORT_SYMBOL(mdesc_grab); |
| 202 | |
| 203 | void mdesc_release(struct mdesc_handle *hp) |
| 204 | { |
| 205 | unsigned long flags; |
| 206 | |
| 207 | spin_lock_irqsave(&mdesc_lock, flags); |
| 208 | if (atomic_dec_and_test(&hp->refcnt)) { |
| 209 | list_del_init(&hp->list); |
| 210 | hp->mops->free(hp); |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 211 | } |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 212 | spin_unlock_irqrestore(&mdesc_lock, flags); |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 213 | } |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 214 | EXPORT_SYMBOL(mdesc_release); |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 215 | |
David S. Miller | 920c3ed | 2007-07-17 21:37:35 -0700 | [diff] [blame] | 216 | static DEFINE_MUTEX(mdesc_mutex); |
| 217 | static struct mdesc_notifier_client *client_list; |
| 218 | |
| 219 | void mdesc_register_notifier(struct mdesc_notifier_client *client) |
| 220 | { |
| 221 | u64 node; |
| 222 | |
| 223 | mutex_lock(&mdesc_mutex); |
| 224 | client->next = client_list; |
| 225 | client_list = client; |
| 226 | |
| 227 | mdesc_for_each_node_by_name(cur_mdesc, node, client->node_name) |
| 228 | client->add(cur_mdesc, node); |
| 229 | |
| 230 | mutex_unlock(&mdesc_mutex); |
| 231 | } |
| 232 | |
David S. Miller | 91ba3c2 | 2007-07-18 15:15:45 -0700 | [diff] [blame] | 233 | static const u64 *parent_cfg_handle(struct mdesc_handle *hp, u64 node) |
| 234 | { |
| 235 | const u64 *id; |
| 236 | u64 a; |
| 237 | |
| 238 | id = NULL; |
| 239 | mdesc_for_each_arc(a, hp, node, MDESC_ARC_TYPE_BACK) { |
| 240 | u64 target; |
| 241 | |
| 242 | target = mdesc_arc_target(hp, a); |
| 243 | id = mdesc_get_property(hp, target, |
| 244 | "cfg-handle", NULL); |
| 245 | if (id) |
| 246 | break; |
| 247 | } |
| 248 | |
| 249 | return id; |
| 250 | } |
| 251 | |
David S. Miller | 920c3ed | 2007-07-17 21:37:35 -0700 | [diff] [blame] | 252 | /* Run 'func' on nodes which are in A but not in B. */ |
| 253 | static void invoke_on_missing(const char *name, |
| 254 | struct mdesc_handle *a, |
| 255 | struct mdesc_handle *b, |
| 256 | void (*func)(struct mdesc_handle *, u64)) |
| 257 | { |
| 258 | u64 node; |
| 259 | |
| 260 | mdesc_for_each_node_by_name(a, node, name) { |
David S. Miller | 91ba3c2 | 2007-07-18 15:15:45 -0700 | [diff] [blame] | 261 | int found = 0, is_vdc_port = 0; |
| 262 | const char *name_prop; |
| 263 | const u64 *id; |
David S. Miller | 920c3ed | 2007-07-17 21:37:35 -0700 | [diff] [blame] | 264 | u64 fnode; |
| 265 | |
David S. Miller | 91ba3c2 | 2007-07-18 15:15:45 -0700 | [diff] [blame] | 266 | name_prop = mdesc_get_property(a, node, "name", NULL); |
| 267 | if (name_prop && !strcmp(name_prop, "vdc-port")) { |
| 268 | is_vdc_port = 1; |
| 269 | id = parent_cfg_handle(a, node); |
| 270 | } else |
| 271 | id = mdesc_get_property(a, node, "id", NULL); |
| 272 | |
| 273 | if (!id) { |
| 274 | printk(KERN_ERR "MD: Cannot find ID for %s node.\n", |
| 275 | (name_prop ? name_prop : name)); |
| 276 | continue; |
| 277 | } |
| 278 | |
David S. Miller | 920c3ed | 2007-07-17 21:37:35 -0700 | [diff] [blame] | 279 | mdesc_for_each_node_by_name(b, fnode, name) { |
David S. Miller | 91ba3c2 | 2007-07-18 15:15:45 -0700 | [diff] [blame] | 280 | const u64 *fid; |
| 281 | |
| 282 | if (is_vdc_port) { |
| 283 | name_prop = mdesc_get_property(b, fnode, |
| 284 | "name", NULL); |
| 285 | if (!name_prop || |
| 286 | strcmp(name_prop, "vdc-port")) |
| 287 | continue; |
| 288 | fid = parent_cfg_handle(b, fnode); |
| 289 | if (!fid) { |
| 290 | printk(KERN_ERR "MD: Cannot find ID " |
| 291 | "for vdc-port node.\n"); |
| 292 | continue; |
| 293 | } |
| 294 | } else |
| 295 | fid = mdesc_get_property(b, fnode, |
| 296 | "id", NULL); |
David S. Miller | 920c3ed | 2007-07-17 21:37:35 -0700 | [diff] [blame] | 297 | |
| 298 | if (*id == *fid) { |
| 299 | found = 1; |
| 300 | break; |
| 301 | } |
| 302 | } |
| 303 | if (!found) |
| 304 | func(a, node); |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | static void notify_one(struct mdesc_notifier_client *p, |
| 309 | struct mdesc_handle *old_hp, |
| 310 | struct mdesc_handle *new_hp) |
| 311 | { |
| 312 | invoke_on_missing(p->node_name, old_hp, new_hp, p->remove); |
| 313 | invoke_on_missing(p->node_name, new_hp, old_hp, p->add); |
| 314 | } |
| 315 | |
| 316 | static void mdesc_notify_clients(struct mdesc_handle *old_hp, |
| 317 | struct mdesc_handle *new_hp) |
| 318 | { |
| 319 | struct mdesc_notifier_client *p = client_list; |
| 320 | |
| 321 | while (p) { |
| 322 | notify_one(p, old_hp, new_hp); |
| 323 | p = p->next; |
| 324 | } |
| 325 | } |
| 326 | |
David S. Miller | 778feeb | 2007-07-16 16:50:36 -0700 | [diff] [blame] | 327 | void mdesc_update(void) |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 328 | { |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 329 | unsigned long len, real_len, status; |
| 330 | struct mdesc_handle *hp, *orig_hp; |
| 331 | unsigned long flags; |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 332 | |
David S. Miller | 920c3ed | 2007-07-17 21:37:35 -0700 | [diff] [blame] | 333 | mutex_lock(&mdesc_mutex); |
| 334 | |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 335 | (void) sun4v_mach_desc(0UL, 0UL, &len); |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 336 | |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 337 | hp = mdesc_alloc(len, &kmalloc_mdesc_memops); |
| 338 | if (!hp) { |
| 339 | printk(KERN_ERR "MD: mdesc alloc fails\n"); |
David S. Miller | 920c3ed | 2007-07-17 21:37:35 -0700 | [diff] [blame] | 340 | goto out; |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 341 | } |
| 342 | |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 343 | status = sun4v_mach_desc(__pa(&hp->mdesc), len, &real_len); |
| 344 | if (status != HV_EOK || real_len > len) { |
| 345 | printk(KERN_ERR "MD: mdesc reread fails with %lu\n", |
| 346 | status); |
| 347 | atomic_dec(&hp->refcnt); |
| 348 | mdesc_free(hp); |
David S. Miller | 920c3ed | 2007-07-17 21:37:35 -0700 | [diff] [blame] | 349 | goto out; |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 350 | } |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 351 | |
| 352 | spin_lock_irqsave(&mdesc_lock, flags); |
| 353 | orig_hp = cur_mdesc; |
| 354 | cur_mdesc = hp; |
David S. Miller | 920c3ed | 2007-07-17 21:37:35 -0700 | [diff] [blame] | 355 | spin_unlock_irqrestore(&mdesc_lock, flags); |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 356 | |
David S. Miller | 920c3ed | 2007-07-17 21:37:35 -0700 | [diff] [blame] | 357 | mdesc_notify_clients(orig_hp, hp); |
| 358 | |
| 359 | spin_lock_irqsave(&mdesc_lock, flags); |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 360 | if (atomic_dec_and_test(&orig_hp->refcnt)) |
| 361 | mdesc_free(orig_hp); |
| 362 | else |
| 363 | list_add(&orig_hp->list, &mdesc_zombie_list); |
| 364 | spin_unlock_irqrestore(&mdesc_lock, flags); |
David S. Miller | 920c3ed | 2007-07-17 21:37:35 -0700 | [diff] [blame] | 365 | |
| 366 | out: |
| 367 | mutex_unlock(&mdesc_mutex); |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 368 | } |
| 369 | |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 370 | static struct mdesc_elem *node_block(struct mdesc_hdr *mdesc) |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 371 | { |
| 372 | return (struct mdesc_elem *) (mdesc + 1); |
| 373 | } |
| 374 | |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 375 | static void *name_block(struct mdesc_hdr *mdesc) |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 376 | { |
| 377 | return ((void *) node_block(mdesc)) + mdesc->node_sz; |
| 378 | } |
| 379 | |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 380 | static void *data_block(struct mdesc_hdr *mdesc) |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 381 | { |
| 382 | return ((void *) name_block(mdesc)) + mdesc->name_sz; |
| 383 | } |
| 384 | |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 385 | u64 mdesc_node_by_name(struct mdesc_handle *hp, |
| 386 | u64 from_node, const char *name) |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 387 | { |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 388 | struct mdesc_elem *ep = node_block(&hp->mdesc); |
| 389 | const char *names = name_block(&hp->mdesc); |
| 390 | u64 last_node = hp->mdesc.node_sz / 16; |
| 391 | u64 ret; |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 392 | |
David S. Miller | 778feeb | 2007-07-16 16:50:36 -0700 | [diff] [blame] | 393 | if (from_node == MDESC_NODE_NULL) { |
| 394 | ret = from_node = 0; |
| 395 | } else if (from_node >= last_node) { |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 396 | return MDESC_NODE_NULL; |
David S. Miller | 778feeb | 2007-07-16 16:50:36 -0700 | [diff] [blame] | 397 | } else { |
| 398 | ret = ep[from_node].d.val; |
| 399 | } |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 400 | |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 401 | while (ret < last_node) { |
| 402 | if (ep[ret].tag != MD_NODE) |
| 403 | return MDESC_NODE_NULL; |
| 404 | if (!strcmp(names + ep[ret].name_offset, name)) |
| 405 | break; |
| 406 | ret = ep[ret].d.val; |
| 407 | } |
| 408 | if (ret >= last_node) |
| 409 | ret = MDESC_NODE_NULL; |
| 410 | return ret; |
| 411 | } |
| 412 | EXPORT_SYMBOL(mdesc_node_by_name); |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 413 | |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 414 | const void *mdesc_get_property(struct mdesc_handle *hp, u64 node, |
| 415 | const char *name, int *lenp) |
| 416 | { |
| 417 | const char *names = name_block(&hp->mdesc); |
| 418 | u64 last_node = hp->mdesc.node_sz / 16; |
| 419 | void *data = data_block(&hp->mdesc); |
| 420 | struct mdesc_elem *ep; |
| 421 | |
| 422 | if (node == MDESC_NODE_NULL || node >= last_node) |
| 423 | return NULL; |
| 424 | |
| 425 | ep = node_block(&hp->mdesc) + node; |
| 426 | ep++; |
| 427 | for (; ep->tag != MD_NODE_END; ep++) { |
| 428 | void *val = NULL; |
| 429 | int len = 0; |
| 430 | |
| 431 | switch (ep->tag) { |
| 432 | case MD_PROP_VAL: |
| 433 | val = &ep->d.val; |
| 434 | len = 8; |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 435 | break; |
| 436 | |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 437 | case MD_PROP_STR: |
| 438 | case MD_PROP_DATA: |
| 439 | val = data + ep->d.data.data_offset; |
| 440 | len = ep->d.data.data_len; |
| 441 | break; |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 442 | |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 443 | default: |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 444 | break; |
| 445 | } |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 446 | if (!val) |
| 447 | continue; |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 448 | |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 449 | if (!strcmp(names + ep->name_offset, name)) { |
| 450 | if (lenp) |
| 451 | *lenp = len; |
| 452 | return val; |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 453 | } |
| 454 | } |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 455 | |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 456 | return NULL; |
| 457 | } |
| 458 | EXPORT_SYMBOL(mdesc_get_property); |
| 459 | |
| 460 | u64 mdesc_next_arc(struct mdesc_handle *hp, u64 from, const char *arc_type) |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 461 | { |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 462 | struct mdesc_elem *ep, *base = node_block(&hp->mdesc); |
| 463 | const char *names = name_block(&hp->mdesc); |
| 464 | u64 last_node = hp->mdesc.node_sz / 16; |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 465 | |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 466 | if (from == MDESC_NODE_NULL || from >= last_node) |
| 467 | return MDESC_NODE_NULL; |
| 468 | |
| 469 | ep = base + from; |
| 470 | |
| 471 | ep++; |
| 472 | for (; ep->tag != MD_NODE_END; ep++) { |
| 473 | if (ep->tag != MD_PROP_ARC) |
| 474 | continue; |
| 475 | |
| 476 | if (strcmp(names + ep->name_offset, arc_type)) |
| 477 | continue; |
| 478 | |
| 479 | return ep - base; |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 480 | } |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 481 | |
| 482 | return MDESC_NODE_NULL; |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 483 | } |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 484 | EXPORT_SYMBOL(mdesc_next_arc); |
| 485 | |
| 486 | u64 mdesc_arc_target(struct mdesc_handle *hp, u64 arc) |
| 487 | { |
| 488 | struct mdesc_elem *ep, *base = node_block(&hp->mdesc); |
| 489 | |
| 490 | ep = base + arc; |
| 491 | |
| 492 | return ep->d.val; |
| 493 | } |
| 494 | EXPORT_SYMBOL(mdesc_arc_target); |
| 495 | |
| 496 | const char *mdesc_node_name(struct mdesc_handle *hp, u64 node) |
| 497 | { |
| 498 | struct mdesc_elem *ep, *base = node_block(&hp->mdesc); |
| 499 | const char *names = name_block(&hp->mdesc); |
| 500 | u64 last_node = hp->mdesc.node_sz / 16; |
| 501 | |
| 502 | if (node == MDESC_NODE_NULL || node >= last_node) |
| 503 | return NULL; |
| 504 | |
| 505 | ep = base + node; |
| 506 | if (ep->tag != MD_NODE) |
| 507 | return NULL; |
| 508 | |
| 509 | return names + ep->name_offset; |
| 510 | } |
| 511 | EXPORT_SYMBOL(mdesc_node_name); |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 512 | |
David S. Miller | 961f65f | 2011-08-05 02:38:27 -0700 | [diff] [blame] | 513 | static u64 max_cpus = 64; |
| 514 | |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 515 | static void __init report_platform_properties(void) |
| 516 | { |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 517 | struct mdesc_handle *hp = mdesc_grab(); |
| 518 | u64 pn = mdesc_node_by_name(hp, MDESC_NODE_NULL, "platform"); |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 519 | const char *s; |
| 520 | const u64 *v; |
| 521 | |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 522 | if (pn == MDESC_NODE_NULL) { |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 523 | prom_printf("No platform node in machine-description.\n"); |
| 524 | prom_halt(); |
| 525 | } |
| 526 | |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 527 | s = mdesc_get_property(hp, pn, "banner-name", NULL); |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 528 | printk("PLATFORM: banner-name [%s]\n", s); |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 529 | s = mdesc_get_property(hp, pn, "name", NULL); |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 530 | printk("PLATFORM: name [%s]\n", s); |
| 531 | |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 532 | v = mdesc_get_property(hp, pn, "hostid", NULL); |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 533 | if (v) |
Sam Ravnborg | 9018113 | 2009-01-06 13:19:28 -0800 | [diff] [blame] | 534 | printk("PLATFORM: hostid [%08llx]\n", *v); |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 535 | v = mdesc_get_property(hp, pn, "serial#", NULL); |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 536 | if (v) |
Sam Ravnborg | 9018113 | 2009-01-06 13:19:28 -0800 | [diff] [blame] | 537 | printk("PLATFORM: serial# [%08llx]\n", *v); |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 538 | v = mdesc_get_property(hp, pn, "stick-frequency", NULL); |
Sam Ravnborg | 9018113 | 2009-01-06 13:19:28 -0800 | [diff] [blame] | 539 | printk("PLATFORM: stick-frequency [%08llx]\n", *v); |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 540 | v = mdesc_get_property(hp, pn, "mac-address", NULL); |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 541 | if (v) |
Sam Ravnborg | 9018113 | 2009-01-06 13:19:28 -0800 | [diff] [blame] | 542 | printk("PLATFORM: mac-address [%llx]\n", *v); |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 543 | v = mdesc_get_property(hp, pn, "watchdog-resolution", NULL); |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 544 | if (v) |
Sam Ravnborg | 9018113 | 2009-01-06 13:19:28 -0800 | [diff] [blame] | 545 | printk("PLATFORM: watchdog-resolution [%llu ms]\n", *v); |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 546 | v = mdesc_get_property(hp, pn, "watchdog-max-timeout", NULL); |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 547 | if (v) |
Sam Ravnborg | 9018113 | 2009-01-06 13:19:28 -0800 | [diff] [blame] | 548 | printk("PLATFORM: watchdog-max-timeout [%llu ms]\n", *v); |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 549 | v = mdesc_get_property(hp, pn, "max-cpus", NULL); |
David S. Miller | 961f65f | 2011-08-05 02:38:27 -0700 | [diff] [blame] | 550 | if (v) { |
| 551 | max_cpus = *v; |
| 552 | printk("PLATFORM: max-cpus [%llu]\n", max_cpus); |
| 553 | } |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 554 | |
David S. Miller | 4f0234f | 2007-07-13 16:03:42 -0700 | [diff] [blame] | 555 | #ifdef CONFIG_SMP |
| 556 | { |
| 557 | int max_cpu, i; |
| 558 | |
| 559 | if (v) { |
| 560 | max_cpu = *v; |
| 561 | if (max_cpu > NR_CPUS) |
| 562 | max_cpu = NR_CPUS; |
| 563 | } else { |
| 564 | max_cpu = NR_CPUS; |
| 565 | } |
| 566 | for (i = 0; i < max_cpu; i++) |
Rusty Russell | 8922907 | 2009-03-16 14:40:23 +1030 | [diff] [blame] | 567 | set_cpu_possible(i, true); |
David S. Miller | 4f0234f | 2007-07-13 16:03:42 -0700 | [diff] [blame] | 568 | } |
| 569 | #endif |
| 570 | |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 571 | mdesc_release(hp); |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 572 | } |
| 573 | |
Paul Gortmaker | 2066aad | 2013-06-17 15:43:14 -0400 | [diff] [blame] | 574 | static void fill_in_one_cache(cpuinfo_sparc *c, struct mdesc_handle *hp, u64 mp) |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 575 | { |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 576 | const u64 *level = mdesc_get_property(hp, mp, "level", NULL); |
| 577 | const u64 *size = mdesc_get_property(hp, mp, "size", NULL); |
| 578 | const u64 *line_size = mdesc_get_property(hp, mp, "line-size", NULL); |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 579 | const char *type; |
| 580 | int type_len; |
| 581 | |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 582 | type = mdesc_get_property(hp, mp, "type", &type_len); |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 583 | |
| 584 | switch (*level) { |
| 585 | case 1: |
David S. Miller | 46bcea7 | 2007-08-07 18:46:36 -0700 | [diff] [blame] | 586 | if (of_find_in_proplist(type, "instn", type_len)) { |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 587 | c->icache_size = *size; |
| 588 | c->icache_line_size = *line_size; |
David S. Miller | 46bcea7 | 2007-08-07 18:46:36 -0700 | [diff] [blame] | 589 | } else if (of_find_in_proplist(type, "data", type_len)) { |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 590 | c->dcache_size = *size; |
| 591 | c->dcache_line_size = *line_size; |
| 592 | } |
| 593 | break; |
| 594 | |
| 595 | case 2: |
| 596 | c->ecache_size = *size; |
| 597 | c->ecache_line_size = *line_size; |
| 598 | break; |
| 599 | |
| 600 | default: |
| 601 | break; |
| 602 | } |
| 603 | |
| 604 | if (*level == 1) { |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 605 | u64 a; |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 606 | |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 607 | mdesc_for_each_arc(a, hp, mp, MDESC_ARC_TYPE_FWD) { |
| 608 | u64 target = mdesc_arc_target(hp, a); |
| 609 | const char *name = mdesc_node_name(hp, target); |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 610 | |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 611 | if (!strcmp(name, "cache")) |
| 612 | fill_in_one_cache(c, hp, target); |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 613 | } |
| 614 | } |
| 615 | } |
| 616 | |
chris hyser | acc455c | 2015-04-22 12:28:31 -0400 | [diff] [blame] | 617 | static void find_back_node_value(struct mdesc_handle *hp, u64 node, |
| 618 | char *srch_val, |
| 619 | void (*func)(struct mdesc_handle *, u64, int), |
| 620 | u64 val, int depth) |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 621 | { |
chris hyser | acc455c | 2015-04-22 12:28:31 -0400 | [diff] [blame] | 622 | u64 arc; |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 623 | |
chris hyser | acc455c | 2015-04-22 12:28:31 -0400 | [diff] [blame] | 624 | /* Since we have an estimate of recursion depth, do a sanity check. */ |
| 625 | if (depth == 0) |
| 626 | return; |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 627 | |
chris hyser | acc455c | 2015-04-22 12:28:31 -0400 | [diff] [blame] | 628 | mdesc_for_each_arc(arc, hp, node, MDESC_ARC_TYPE_BACK) { |
| 629 | u64 n = mdesc_arc_target(hp, arc); |
| 630 | const char *name = mdesc_node_name(hp, n); |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 631 | |
chris hyser | acc455c | 2015-04-22 12:28:31 -0400 | [diff] [blame] | 632 | if (!strcmp(srch_val, name)) |
| 633 | (*func)(hp, n, val); |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 634 | |
chris hyser | acc455c | 2015-04-22 12:28:31 -0400 | [diff] [blame] | 635 | find_back_node_value(hp, n, srch_val, func, val, depth-1); |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 636 | } |
| 637 | } |
| 638 | |
chris hyser | acc455c | 2015-04-22 12:28:31 -0400 | [diff] [blame] | 639 | static void __mark_core_id(struct mdesc_handle *hp, u64 node, |
| 640 | int core_id) |
| 641 | { |
| 642 | const u64 *id = mdesc_get_property(hp, node, "id", NULL); |
| 643 | |
| 644 | if (*id < num_possible_cpus()) |
| 645 | cpu_data(*id).core_id = core_id; |
| 646 | } |
| 647 | |
Atish Patra | d624716 | 2016-10-19 18:33:29 -0600 | [diff] [blame] | 648 | static void __mark_max_cache_id(struct mdesc_handle *hp, u64 node, |
| 649 | int max_cache_id) |
chris hyser | acc455c | 2015-04-22 12:28:31 -0400 | [diff] [blame] | 650 | { |
| 651 | const u64 *id = mdesc_get_property(hp, node, "id", NULL); |
| 652 | |
Atish Patra | d624716 | 2016-10-19 18:33:29 -0600 | [diff] [blame] | 653 | if (*id < num_possible_cpus()) { |
| 654 | cpu_data(*id).max_cache_id = max_cache_id; |
| 655 | |
| 656 | /** |
| 657 | * On systems without explicit socket descriptions socket |
| 658 | * is max_cache_id |
| 659 | */ |
| 660 | cpu_data(*id).sock_id = max_cache_id; |
| 661 | } |
chris hyser | acc455c | 2015-04-22 12:28:31 -0400 | [diff] [blame] | 662 | } |
| 663 | |
| 664 | static void mark_core_ids(struct mdesc_handle *hp, u64 mp, |
| 665 | int core_id) |
| 666 | { |
| 667 | find_back_node_value(hp, mp, "cpu", __mark_core_id, core_id, 10); |
| 668 | } |
| 669 | |
Atish Patra | d624716 | 2016-10-19 18:33:29 -0600 | [diff] [blame] | 670 | static void mark_max_cache_ids(struct mdesc_handle *hp, u64 mp, |
| 671 | int max_cache_id) |
chris hyser | acc455c | 2015-04-22 12:28:31 -0400 | [diff] [blame] | 672 | { |
Atish Patra | d624716 | 2016-10-19 18:33:29 -0600 | [diff] [blame] | 673 | find_back_node_value(hp, mp, "cpu", __mark_max_cache_id, |
| 674 | max_cache_id, 10); |
chris hyser | acc455c | 2015-04-22 12:28:31 -0400 | [diff] [blame] | 675 | } |
| 676 | |
Paul Gortmaker | 2066aad | 2013-06-17 15:43:14 -0400 | [diff] [blame] | 677 | static void set_core_ids(struct mdesc_handle *hp) |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 678 | { |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 679 | int idx; |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 680 | u64 mp; |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 681 | |
| 682 | idx = 1; |
chris hyser | acc455c | 2015-04-22 12:28:31 -0400 | [diff] [blame] | 683 | |
| 684 | /* Identify unique cores by looking for cpus backpointed to by |
| 685 | * level 1 instruction caches. |
| 686 | */ |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 687 | mdesc_for_each_node_by_name(hp, mp, "cache") { |
| 688 | const u64 *level; |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 689 | const char *type; |
| 690 | int len; |
| 691 | |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 692 | level = mdesc_get_property(hp, mp, "level", NULL); |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 693 | if (*level != 1) |
| 694 | continue; |
| 695 | |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 696 | type = mdesc_get_property(hp, mp, "type", &len); |
David S. Miller | 46bcea7 | 2007-08-07 18:46:36 -0700 | [diff] [blame] | 697 | if (!of_find_in_proplist(type, "instn", len)) |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 698 | continue; |
| 699 | |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 700 | mark_core_ids(hp, mp, idx); |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 701 | idx++; |
| 702 | } |
| 703 | } |
| 704 | |
Atish Patra | d624716 | 2016-10-19 18:33:29 -0600 | [diff] [blame] | 705 | static int set_max_cache_ids_by_cache(struct mdesc_handle *hp, int level) |
chris hyser | acc455c | 2015-04-22 12:28:31 -0400 | [diff] [blame] | 706 | { |
| 707 | u64 mp; |
| 708 | int idx = 1; |
| 709 | int fnd = 0; |
| 710 | |
Atish Patra | d624716 | 2016-10-19 18:33:29 -0600 | [diff] [blame] | 711 | /** |
| 712 | * Identify unique highest level of shared cache by looking for cpus |
| 713 | * backpointed to by shared level N caches. |
chris hyser | acc455c | 2015-04-22 12:28:31 -0400 | [diff] [blame] | 714 | */ |
| 715 | mdesc_for_each_node_by_name(hp, mp, "cache") { |
| 716 | const u64 *cur_lvl; |
| 717 | |
| 718 | cur_lvl = mdesc_get_property(hp, mp, "level", NULL); |
| 719 | if (*cur_lvl != level) |
| 720 | continue; |
Atish Patra | d624716 | 2016-10-19 18:33:29 -0600 | [diff] [blame] | 721 | mark_max_cache_ids(hp, mp, idx); |
chris hyser | acc455c | 2015-04-22 12:28:31 -0400 | [diff] [blame] | 722 | idx++; |
| 723 | fnd = 1; |
| 724 | } |
| 725 | return fnd; |
| 726 | } |
| 727 | |
| 728 | static void set_sock_ids_by_socket(struct mdesc_handle *hp, u64 mp) |
| 729 | { |
| 730 | int idx = 1; |
| 731 | |
| 732 | mdesc_for_each_node_by_name(hp, mp, "socket") { |
| 733 | u64 a; |
| 734 | |
| 735 | mdesc_for_each_arc(a, hp, mp, MDESC_ARC_TYPE_FWD) { |
| 736 | u64 t = mdesc_arc_target(hp, a); |
| 737 | const char *name; |
| 738 | const u64 *id; |
| 739 | |
| 740 | name = mdesc_node_name(hp, t); |
| 741 | if (strcmp(name, "cpu")) |
| 742 | continue; |
| 743 | |
| 744 | id = mdesc_get_property(hp, t, "id", NULL); |
| 745 | if (*id < num_possible_cpus()) |
| 746 | cpu_data(*id).sock_id = idx; |
| 747 | } |
| 748 | idx++; |
| 749 | } |
| 750 | } |
| 751 | |
| 752 | static void set_sock_ids(struct mdesc_handle *hp) |
| 753 | { |
| 754 | u64 mp; |
| 755 | |
Atish Patra | d624716 | 2016-10-19 18:33:29 -0600 | [diff] [blame] | 756 | /** |
| 757 | * Find the highest level of shared cache which pre-T7 is also |
| 758 | * the socket. |
chris hyser | acc455c | 2015-04-22 12:28:31 -0400 | [diff] [blame] | 759 | */ |
Atish Patra | d624716 | 2016-10-19 18:33:29 -0600 | [diff] [blame] | 760 | if (!set_max_cache_ids_by_cache(hp, 3)) |
| 761 | set_max_cache_ids_by_cache(hp, 2); |
| 762 | |
| 763 | /* If machine description exposes sockets data use it.*/ |
chris hyser | acc455c | 2015-04-22 12:28:31 -0400 | [diff] [blame] | 764 | mp = mdesc_node_by_name(hp, MDESC_NODE_NULL, "sockets"); |
| 765 | if (mp != MDESC_NODE_NULL) |
Atish Patra | d624716 | 2016-10-19 18:33:29 -0600 | [diff] [blame] | 766 | set_sock_ids_by_socket(hp, mp); |
chris hyser | acc455c | 2015-04-22 12:28:31 -0400 | [diff] [blame] | 767 | } |
| 768 | |
Paul Gortmaker | 2066aad | 2013-06-17 15:43:14 -0400 | [diff] [blame] | 769 | static void mark_proc_ids(struct mdesc_handle *hp, u64 mp, int proc_id) |
David S. Miller | f78eae2 | 2007-06-04 17:01:39 -0700 | [diff] [blame] | 770 | { |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 771 | u64 a; |
David S. Miller | f78eae2 | 2007-06-04 17:01:39 -0700 | [diff] [blame] | 772 | |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 773 | mdesc_for_each_arc(a, hp, mp, MDESC_ARC_TYPE_BACK) { |
| 774 | u64 t = mdesc_arc_target(hp, a); |
| 775 | const char *name; |
David S. Miller | f78eae2 | 2007-06-04 17:01:39 -0700 | [diff] [blame] | 776 | const u64 *id; |
| 777 | |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 778 | name = mdesc_node_name(hp, t); |
| 779 | if (strcmp(name, "cpu")) |
David S. Miller | f78eae2 | 2007-06-04 17:01:39 -0700 | [diff] [blame] | 780 | continue; |
| 781 | |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 782 | id = mdesc_get_property(hp, t, "id", NULL); |
David S. Miller | f78eae2 | 2007-06-04 17:01:39 -0700 | [diff] [blame] | 783 | if (*id < NR_CPUS) |
| 784 | cpu_data(*id).proc_id = proc_id; |
| 785 | } |
| 786 | } |
| 787 | |
Paul Gortmaker | 2066aad | 2013-06-17 15:43:14 -0400 | [diff] [blame] | 788 | static void __set_proc_ids(struct mdesc_handle *hp, const char *exec_unit_name) |
David S. Miller | f78eae2 | 2007-06-04 17:01:39 -0700 | [diff] [blame] | 789 | { |
David S. Miller | f78eae2 | 2007-06-04 17:01:39 -0700 | [diff] [blame] | 790 | int idx; |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 791 | u64 mp; |
David S. Miller | f78eae2 | 2007-06-04 17:01:39 -0700 | [diff] [blame] | 792 | |
| 793 | idx = 0; |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 794 | mdesc_for_each_node_by_name(hp, mp, exec_unit_name) { |
David S. Miller | f78eae2 | 2007-06-04 17:01:39 -0700 | [diff] [blame] | 795 | const char *type; |
| 796 | int len; |
| 797 | |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 798 | type = mdesc_get_property(hp, mp, "type", &len); |
David S. Miller | 46bcea7 | 2007-08-07 18:46:36 -0700 | [diff] [blame] | 799 | if (!of_find_in_proplist(type, "int", len) && |
| 800 | !of_find_in_proplist(type, "integer", len)) |
David S. Miller | f78eae2 | 2007-06-04 17:01:39 -0700 | [diff] [blame] | 801 | continue; |
| 802 | |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 803 | mark_proc_ids(hp, mp, idx); |
David S. Miller | f78eae2 | 2007-06-04 17:01:39 -0700 | [diff] [blame] | 804 | idx++; |
| 805 | } |
| 806 | } |
| 807 | |
Paul Gortmaker | 2066aad | 2013-06-17 15:43:14 -0400 | [diff] [blame] | 808 | static void set_proc_ids(struct mdesc_handle *hp) |
David S. Miller | f78eae2 | 2007-06-04 17:01:39 -0700 | [diff] [blame] | 809 | { |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 810 | __set_proc_ids(hp, "exec_unit"); |
| 811 | __set_proc_ids(hp, "exec-unit"); |
David S. Miller | f78eae2 | 2007-06-04 17:01:39 -0700 | [diff] [blame] | 812 | } |
| 813 | |
Paul Gortmaker | 2066aad | 2013-06-17 15:43:14 -0400 | [diff] [blame] | 814 | static void get_one_mondo_bits(const u64 *p, unsigned int *mask, |
| 815 | unsigned long def, unsigned long max) |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 816 | { |
| 817 | u64 val; |
| 818 | |
| 819 | if (!p) |
| 820 | goto use_default; |
| 821 | val = *p; |
| 822 | |
| 823 | if (!val || val >= 64) |
| 824 | goto use_default; |
| 825 | |
David S. Miller | 961f65f | 2011-08-05 02:38:27 -0700 | [diff] [blame] | 826 | if (val > max) |
| 827 | val = max; |
| 828 | |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 829 | *mask = ((1U << val) * 64U) - 1U; |
| 830 | return; |
| 831 | |
| 832 | use_default: |
| 833 | *mask = ((1U << def) * 64U) - 1U; |
| 834 | } |
| 835 | |
Paul Gortmaker | 2066aad | 2013-06-17 15:43:14 -0400 | [diff] [blame] | 836 | static void get_mondo_data(struct mdesc_handle *hp, u64 mp, |
| 837 | struct trap_per_cpu *tb) |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 838 | { |
David S. Miller | 961f65f | 2011-08-05 02:38:27 -0700 | [diff] [blame] | 839 | static int printed; |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 840 | const u64 *val; |
| 841 | |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 842 | val = mdesc_get_property(hp, mp, "q-cpu-mondo-#bits", NULL); |
David S. Miller | 961f65f | 2011-08-05 02:38:27 -0700 | [diff] [blame] | 843 | get_one_mondo_bits(val, &tb->cpu_mondo_qmask, 7, ilog2(max_cpus * 2)); |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 844 | |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 845 | val = mdesc_get_property(hp, mp, "q-dev-mondo-#bits", NULL); |
David S. Miller | 961f65f | 2011-08-05 02:38:27 -0700 | [diff] [blame] | 846 | get_one_mondo_bits(val, &tb->dev_mondo_qmask, 7, 8); |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 847 | |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 848 | val = mdesc_get_property(hp, mp, "q-resumable-#bits", NULL); |
David S. Miller | 961f65f | 2011-08-05 02:38:27 -0700 | [diff] [blame] | 849 | get_one_mondo_bits(val, &tb->resum_qmask, 6, 7); |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 850 | |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 851 | val = mdesc_get_property(hp, mp, "q-nonresumable-#bits", NULL); |
David S. Miller | 961f65f | 2011-08-05 02:38:27 -0700 | [diff] [blame] | 852 | get_one_mondo_bits(val, &tb->nonresum_qmask, 2, 2); |
| 853 | if (!printed++) { |
| 854 | pr_info("SUN4V: Mondo queue sizes " |
| 855 | "[cpu(%u) dev(%u) r(%u) nr(%u)]\n", |
| 856 | tb->cpu_mondo_qmask + 1, |
| 857 | tb->dev_mondo_qmask + 1, |
| 858 | tb->resum_qmask + 1, |
| 859 | tb->nonresum_qmask + 1); |
| 860 | } |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 861 | } |
| 862 | |
Paul Gortmaker | 2066aad | 2013-06-17 15:43:14 -0400 | [diff] [blame] | 863 | static void *mdesc_iterate_over_cpus(void *(*func)(struct mdesc_handle *, u64, int, void *), void *arg, cpumask_t *mask) |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 864 | { |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 865 | struct mdesc_handle *hp = mdesc_grab(); |
David S. Miller | 5052f52 | 2009-04-08 19:55:47 -0700 | [diff] [blame] | 866 | void *ret = NULL; |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 867 | u64 mp; |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 868 | |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 869 | mdesc_for_each_node_by_name(hp, mp, "cpu") { |
| 870 | const u64 *id = mdesc_get_property(hp, mp, "id", NULL); |
David S. Miller | 5052f52 | 2009-04-08 19:55:47 -0700 | [diff] [blame] | 871 | int cpuid = *id; |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 872 | |
| 873 | #ifdef CONFIG_SMP |
David S. Miller | 8a177c4 | 2007-09-16 14:45:06 -0700 | [diff] [blame] | 874 | if (cpuid >= NR_CPUS) { |
| 875 | printk(KERN_WARNING "Ignoring CPU %d which is " |
| 876 | ">= NR_CPUS (%d)\n", |
| 877 | cpuid, NR_CPUS); |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 878 | continue; |
David S. Miller | 8a177c4 | 2007-09-16 14:45:06 -0700 | [diff] [blame] | 879 | } |
KOSAKI Motohiro | fb1fece | 2011-05-16 13:38:07 -0700 | [diff] [blame] | 880 | if (!cpumask_test_cpu(cpuid, mask)) |
David S. Miller | 4f0234f | 2007-07-13 16:03:42 -0700 | [diff] [blame] | 881 | continue; |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 882 | #endif |
| 883 | |
David S. Miller | 5052f52 | 2009-04-08 19:55:47 -0700 | [diff] [blame] | 884 | ret = func(hp, mp, cpuid, arg); |
| 885 | if (ret) |
| 886 | goto out; |
| 887 | } |
| 888 | out: |
| 889 | mdesc_release(hp); |
| 890 | return ret; |
| 891 | } |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 892 | |
Paul Gortmaker | 2066aad | 2013-06-17 15:43:14 -0400 | [diff] [blame] | 893 | static void *record_one_cpu(struct mdesc_handle *hp, u64 mp, int cpuid, |
| 894 | void *arg) |
David S. Miller | 5052f52 | 2009-04-08 19:55:47 -0700 | [diff] [blame] | 895 | { |
| 896 | ncpus_probed++; |
| 897 | #ifdef CONFIG_SMP |
| 898 | set_cpu_present(cpuid, true); |
| 899 | #endif |
| 900 | return NULL; |
| 901 | } |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 902 | |
Paul Gortmaker | 2066aad | 2013-06-17 15:43:14 -0400 | [diff] [blame] | 903 | void mdesc_populate_present_mask(cpumask_t *mask) |
David S. Miller | 5052f52 | 2009-04-08 19:55:47 -0700 | [diff] [blame] | 904 | { |
| 905 | if (tlb_type != hypervisor) |
| 906 | return; |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 907 | |
David S. Miller | 5052f52 | 2009-04-08 19:55:47 -0700 | [diff] [blame] | 908 | ncpus_probed = 0; |
| 909 | mdesc_iterate_over_cpus(record_one_cpu, NULL, mask); |
| 910 | } |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 911 | |
David S. Miller | ce33fdc | 2012-09-06 19:01:25 -0700 | [diff] [blame] | 912 | static void * __init check_one_pgsz(struct mdesc_handle *hp, u64 mp, int cpuid, void *arg) |
| 913 | { |
| 914 | const u64 *pgsz_prop = mdesc_get_property(hp, mp, "mmu-page-size-list", NULL); |
| 915 | unsigned long *pgsz_mask = arg; |
| 916 | u64 val; |
| 917 | |
| 918 | val = (HV_PGSZ_MASK_8K | HV_PGSZ_MASK_64K | |
| 919 | HV_PGSZ_MASK_512K | HV_PGSZ_MASK_4MB); |
| 920 | if (pgsz_prop) |
| 921 | val = *pgsz_prop; |
| 922 | |
| 923 | if (!*pgsz_mask) |
| 924 | *pgsz_mask = val; |
| 925 | else |
| 926 | *pgsz_mask &= val; |
| 927 | return NULL; |
| 928 | } |
| 929 | |
| 930 | void __init mdesc_get_page_sizes(cpumask_t *mask, unsigned long *pgsz_mask) |
| 931 | { |
| 932 | *pgsz_mask = 0; |
| 933 | mdesc_iterate_over_cpus(check_one_pgsz, pgsz_mask, mask); |
| 934 | } |
| 935 | |
Paul Gortmaker | 2066aad | 2013-06-17 15:43:14 -0400 | [diff] [blame] | 936 | static void *fill_in_one_cpu(struct mdesc_handle *hp, u64 mp, int cpuid, |
| 937 | void *arg) |
David S. Miller | 5052f52 | 2009-04-08 19:55:47 -0700 | [diff] [blame] | 938 | { |
| 939 | const u64 *cfreq = mdesc_get_property(hp, mp, "clock-frequency", NULL); |
| 940 | struct trap_per_cpu *tb; |
| 941 | cpuinfo_sparc *c; |
| 942 | u64 a; |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 943 | |
David S. Miller | 5052f52 | 2009-04-08 19:55:47 -0700 | [diff] [blame] | 944 | #ifndef CONFIG_SMP |
| 945 | /* On uniprocessor we only want the values for the |
| 946 | * real physical cpu the kernel booted onto, however |
| 947 | * cpu_data() only has one entry at index 0. |
| 948 | */ |
| 949 | if (cpuid != real_hard_smp_processor_id()) |
| 950 | return NULL; |
| 951 | cpuid = 0; |
| 952 | #endif |
| 953 | |
| 954 | c = &cpu_data(cpuid); |
| 955 | c->clock_tick = *cfreq; |
| 956 | |
| 957 | tb = &trap_block[cpuid]; |
| 958 | get_mondo_data(hp, mp, tb); |
| 959 | |
| 960 | mdesc_for_each_arc(a, hp, mp, MDESC_ARC_TYPE_FWD) { |
| 961 | u64 j, t = mdesc_arc_target(hp, a); |
| 962 | const char *t_name; |
| 963 | |
| 964 | t_name = mdesc_node_name(hp, t); |
| 965 | if (!strcmp(t_name, "cache")) { |
| 966 | fill_in_one_cache(c, hp, t); |
| 967 | continue; |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 968 | } |
| 969 | |
David S. Miller | 5052f52 | 2009-04-08 19:55:47 -0700 | [diff] [blame] | 970 | mdesc_for_each_arc(j, hp, t, MDESC_ARC_TYPE_FWD) { |
| 971 | u64 n = mdesc_arc_target(hp, j); |
| 972 | const char *n_name; |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 973 | |
David S. Miller | 5052f52 | 2009-04-08 19:55:47 -0700 | [diff] [blame] | 974 | n_name = mdesc_node_name(hp, n); |
| 975 | if (!strcmp(n_name, "cache")) |
| 976 | fill_in_one_cache(c, hp, n); |
| 977 | } |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 978 | } |
| 979 | |
David S. Miller | 5052f52 | 2009-04-08 19:55:47 -0700 | [diff] [blame] | 980 | c->core_id = 0; |
| 981 | c->proc_id = -1; |
| 982 | |
| 983 | return NULL; |
| 984 | } |
| 985 | |
Paul Gortmaker | 2066aad | 2013-06-17 15:43:14 -0400 | [diff] [blame] | 986 | void mdesc_fill_in_cpu_data(cpumask_t *mask) |
David S. Miller | 5052f52 | 2009-04-08 19:55:47 -0700 | [diff] [blame] | 987 | { |
| 988 | struct mdesc_handle *hp; |
| 989 | |
David S. Miller | a209450 | 2009-04-01 03:15:11 -0700 | [diff] [blame] | 990 | mdesc_iterate_over_cpus(fill_in_one_cpu, NULL, mask); |
David S. Miller | 5052f52 | 2009-04-08 19:55:47 -0700 | [diff] [blame] | 991 | |
David S. Miller | 5052f52 | 2009-04-08 19:55:47 -0700 | [diff] [blame] | 992 | hp = mdesc_grab(); |
| 993 | |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 994 | set_core_ids(hp); |
| 995 | set_proc_ids(hp); |
chris hyser | acc455c | 2015-04-22 12:28:31 -0400 | [diff] [blame] | 996 | set_sock_ids(hp); |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 997 | |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 998 | mdesc_release(hp); |
David S. Miller | 5052f52 | 2009-04-08 19:55:47 -0700 | [diff] [blame] | 999 | |
| 1000 | smp_fill_in_sib_core_maps(); |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 1001 | } |
| 1002 | |
Khalid Aziz | 07d6692 | 2014-07-18 09:15:40 -0600 | [diff] [blame] | 1003 | /* mdesc_open() - Grab a reference to mdesc_handle when /dev/mdesc is |
| 1004 | * opened. Hold this reference until /dev/mdesc is closed to ensure |
| 1005 | * mdesc data structure is not released underneath us. Store the |
| 1006 | * pointer to mdesc structure in private_data for read and seek to use |
| 1007 | */ |
| 1008 | static int mdesc_open(struct inode *inode, struct file *file) |
David S. Miller | 0fdb7f9 | 2007-08-15 21:02:23 -0700 | [diff] [blame] | 1009 | { |
| 1010 | struct mdesc_handle *hp = mdesc_grab(); |
David S. Miller | 0fdb7f9 | 2007-08-15 21:02:23 -0700 | [diff] [blame] | 1011 | |
| 1012 | if (!hp) |
| 1013 | return -ENODEV; |
| 1014 | |
Khalid Aziz | 07d6692 | 2014-07-18 09:15:40 -0600 | [diff] [blame] | 1015 | file->private_data = hp; |
David S. Miller | 0fdb7f9 | 2007-08-15 21:02:23 -0700 | [diff] [blame] | 1016 | |
Khalid Aziz | 07d6692 | 2014-07-18 09:15:40 -0600 | [diff] [blame] | 1017 | return 0; |
| 1018 | } |
| 1019 | |
| 1020 | static ssize_t mdesc_read(struct file *file, char __user *buf, |
| 1021 | size_t len, loff_t *offp) |
| 1022 | { |
| 1023 | struct mdesc_handle *hp = file->private_data; |
| 1024 | unsigned char *mdesc; |
| 1025 | int bytes_left, count = len; |
| 1026 | |
| 1027 | if (*offp >= hp->handle_size) |
| 1028 | return 0; |
| 1029 | |
| 1030 | bytes_left = hp->handle_size - *offp; |
| 1031 | if (count > bytes_left) |
| 1032 | count = bytes_left; |
| 1033 | |
| 1034 | mdesc = (unsigned char *)&hp->mdesc; |
| 1035 | mdesc += *offp; |
| 1036 | if (!copy_to_user(buf, mdesc, count)) { |
| 1037 | *offp += count; |
| 1038 | return count; |
| 1039 | } else { |
| 1040 | return -EFAULT; |
| 1041 | } |
| 1042 | } |
| 1043 | |
| 1044 | static loff_t mdesc_llseek(struct file *file, loff_t offset, int whence) |
| 1045 | { |
Al Viro | b25472f | 2015-12-05 22:04:48 -0500 | [diff] [blame] | 1046 | struct mdesc_handle *hp = file->private_data; |
Khalid Aziz | 07d6692 | 2014-07-18 09:15:40 -0600 | [diff] [blame] | 1047 | |
Al Viro | b25472f | 2015-12-05 22:04:48 -0500 | [diff] [blame] | 1048 | return no_seek_end_llseek_size(file, offset, whence, hp->handle_size); |
Khalid Aziz | 07d6692 | 2014-07-18 09:15:40 -0600 | [diff] [blame] | 1049 | } |
| 1050 | |
| 1051 | /* mdesc_close() - /dev/mdesc is being closed, release the reference to |
| 1052 | * mdesc structure. |
| 1053 | */ |
| 1054 | static int mdesc_close(struct inode *inode, struct file *file) |
| 1055 | { |
| 1056 | mdesc_release(file->private_data); |
| 1057 | return 0; |
David S. Miller | 0fdb7f9 | 2007-08-15 21:02:23 -0700 | [diff] [blame] | 1058 | } |
| 1059 | |
| 1060 | static const struct file_operations mdesc_fops = { |
Khalid Aziz | 07d6692 | 2014-07-18 09:15:40 -0600 | [diff] [blame] | 1061 | .open = mdesc_open, |
| 1062 | .read = mdesc_read, |
| 1063 | .llseek = mdesc_llseek, |
| 1064 | .release = mdesc_close, |
| 1065 | .owner = THIS_MODULE, |
David S. Miller | 0fdb7f9 | 2007-08-15 21:02:23 -0700 | [diff] [blame] | 1066 | }; |
| 1067 | |
| 1068 | static struct miscdevice mdesc_misc = { |
| 1069 | .minor = MISC_DYNAMIC_MINOR, |
| 1070 | .name = "mdesc", |
| 1071 | .fops = &mdesc_fops, |
| 1072 | }; |
| 1073 | |
| 1074 | static int __init mdesc_misc_init(void) |
| 1075 | { |
| 1076 | return misc_register(&mdesc_misc); |
| 1077 | } |
| 1078 | |
| 1079 | __initcall(mdesc_misc_init); |
| 1080 | |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 1081 | void __init sun4v_mdesc_init(void) |
| 1082 | { |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 1083 | struct mdesc_handle *hp; |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 1084 | unsigned long len, real_len, status; |
| 1085 | |
| 1086 | (void) sun4v_mach_desc(0UL, 0UL, &len); |
| 1087 | |
| 1088 | printk("MDESC: Size is %lu bytes.\n", len); |
| 1089 | |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 1090 | hp = mdesc_alloc(len, &memblock_mdesc_ops); |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 1091 | if (hp == NULL) { |
| 1092 | prom_printf("MDESC: alloc of %lu bytes failed.\n", len); |
| 1093 | prom_halt(); |
| 1094 | } |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 1095 | |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 1096 | status = sun4v_mach_desc(__pa(&hp->mdesc), len, &real_len); |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 1097 | if (status != HV_EOK || real_len > len) { |
| 1098 | prom_printf("sun4v_mach_desc fails, err(%lu), " |
| 1099 | "len(%lu), real_len(%lu)\n", |
| 1100 | status, len, real_len); |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 1101 | mdesc_free(hp); |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 1102 | prom_halt(); |
| 1103 | } |
| 1104 | |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 1105 | cur_mdesc = hp; |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 1106 | |
| 1107 | report_platform_properties(); |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 1108 | } |