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> |
David S. Miller | 4a28333 | 2008-02-13 19:22:23 -0800 | [diff] [blame] | 7 | #include <linux/lmb.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 | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 13 | |
| 14 | #include <asm/hypervisor.h> |
| 15 | #include <asm/mdesc.h> |
| 16 | #include <asm/prom.h> |
| 17 | #include <asm/oplib.h> |
| 18 | #include <asm/smp.h> |
| 19 | |
| 20 | /* Unlike the OBP device tree, the machine description is a full-on |
| 21 | * DAG. An arbitrary number of ARCs are possible from one |
| 22 | * node to other nodes and thus we can't use the OBP device_node |
| 23 | * data structure to represent these nodes inside of the kernel. |
| 24 | * |
| 25 | * Actually, it isn't even a DAG, because there are back pointers |
| 26 | * which create cycles in the graph. |
| 27 | * |
| 28 | * mdesc_hdr and mdesc_elem describe the layout of the data structure |
| 29 | * we get from the Hypervisor. |
| 30 | */ |
| 31 | struct mdesc_hdr { |
| 32 | u32 version; /* Transport version */ |
| 33 | u32 node_sz; /* node block size */ |
| 34 | u32 name_sz; /* name block size */ |
| 35 | u32 data_sz; /* data block size */ |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 36 | } __attribute__((aligned(16))); |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 37 | |
| 38 | struct mdesc_elem { |
| 39 | u8 tag; |
| 40 | #define MD_LIST_END 0x00 |
| 41 | #define MD_NODE 0x4e |
| 42 | #define MD_NODE_END 0x45 |
| 43 | #define MD_NOOP 0x20 |
| 44 | #define MD_PROP_ARC 0x61 |
| 45 | #define MD_PROP_VAL 0x76 |
| 46 | #define MD_PROP_STR 0x73 |
| 47 | #define MD_PROP_DATA 0x64 |
| 48 | u8 name_len; |
| 49 | u16 resv; |
| 50 | u32 name_offset; |
| 51 | union { |
| 52 | struct { |
| 53 | u32 data_len; |
| 54 | u32 data_offset; |
| 55 | } data; |
| 56 | u64 val; |
| 57 | } d; |
| 58 | }; |
| 59 | |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 60 | struct mdesc_mem_ops { |
| 61 | struct mdesc_handle *(*alloc)(unsigned int mdesc_size); |
| 62 | void (*free)(struct mdesc_handle *handle); |
| 63 | }; |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 64 | |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 65 | struct mdesc_handle { |
| 66 | struct list_head list; |
| 67 | struct mdesc_mem_ops *mops; |
| 68 | void *self_base; |
| 69 | atomic_t refcnt; |
| 70 | unsigned int handle_size; |
| 71 | struct mdesc_hdr mdesc; |
| 72 | }; |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 73 | |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 74 | static void mdesc_handle_init(struct mdesc_handle *hp, |
| 75 | unsigned int handle_size, |
| 76 | void *base) |
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 | BUG_ON(((unsigned long)&hp->mdesc) & (16UL - 1)); |
| 79 | |
| 80 | memset(hp, 0, handle_size); |
| 81 | INIT_LIST_HEAD(&hp->list); |
| 82 | hp->self_base = base; |
| 83 | atomic_set(&hp->refcnt, 1); |
| 84 | hp->handle_size = handle_size; |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 85 | } |
| 86 | |
David S. Miller | 4a28333 | 2008-02-13 19:22:23 -0800 | [diff] [blame] | 87 | static struct mdesc_handle * __init mdesc_lmb_alloc(unsigned int mdesc_size) |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 88 | { |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 89 | unsigned int handle_size, alloc_size; |
David S. Miller | 4a28333 | 2008-02-13 19:22:23 -0800 | [diff] [blame] | 90 | struct mdesc_handle *hp; |
| 91 | unsigned long paddr; |
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 | handle_size = (sizeof(struct mdesc_handle) - |
| 94 | sizeof(struct mdesc_hdr) + |
| 95 | mdesc_size); |
| 96 | alloc_size = PAGE_ALIGN(handle_size); |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 97 | |
David S. Miller | 4a28333 | 2008-02-13 19:22:23 -0800 | [diff] [blame] | 98 | paddr = lmb_alloc(alloc_size, PAGE_SIZE); |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 99 | |
David S. Miller | 4a28333 | 2008-02-13 19:22:23 -0800 | [diff] [blame] | 100 | hp = NULL; |
| 101 | if (paddr) { |
| 102 | hp = __va(paddr); |
| 103 | mdesc_handle_init(hp, handle_size, hp); |
| 104 | } |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 105 | return hp; |
| 106 | } |
| 107 | |
David S. Miller | 4a28333 | 2008-02-13 19:22:23 -0800 | [diff] [blame] | 108 | static void mdesc_lmb_free(struct mdesc_handle *hp) |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 109 | { |
| 110 | unsigned int alloc_size, handle_size = hp->handle_size; |
| 111 | unsigned long start, end; |
| 112 | |
| 113 | BUG_ON(atomic_read(&hp->refcnt) != 0); |
| 114 | BUG_ON(!list_empty(&hp->list)); |
| 115 | |
| 116 | alloc_size = PAGE_ALIGN(handle_size); |
| 117 | |
| 118 | start = (unsigned long) hp; |
| 119 | end = start + alloc_size; |
| 120 | |
| 121 | while (start < end) { |
| 122 | struct page *p; |
| 123 | |
| 124 | p = virt_to_page(start); |
| 125 | ClearPageReserved(p); |
| 126 | __free_page(p); |
| 127 | start += PAGE_SIZE; |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 128 | } |
| 129 | } |
| 130 | |
David S. Miller | 4a28333 | 2008-02-13 19:22:23 -0800 | [diff] [blame] | 131 | static struct mdesc_mem_ops lmb_mdesc_ops = { |
| 132 | .alloc = mdesc_lmb_alloc, |
| 133 | .free = mdesc_lmb_free, |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 134 | }; |
| 135 | |
| 136 | static struct mdesc_handle *mdesc_kmalloc(unsigned int mdesc_size) |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 137 | { |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 138 | unsigned int handle_size; |
| 139 | void *base; |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 140 | |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 141 | handle_size = (sizeof(struct mdesc_handle) - |
| 142 | sizeof(struct mdesc_hdr) + |
| 143 | mdesc_size); |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 144 | |
David S. Miller | 920c3ed | 2007-07-17 21:37:35 -0700 | [diff] [blame] | 145 | base = kmalloc(handle_size + 15, GFP_KERNEL | __GFP_NOFAIL); |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 146 | if (base) { |
| 147 | struct mdesc_handle *hp; |
| 148 | unsigned long addr; |
| 149 | |
| 150 | addr = (unsigned long)base; |
| 151 | addr = (addr + 15UL) & ~15UL; |
| 152 | hp = (struct mdesc_handle *) addr; |
| 153 | |
| 154 | mdesc_handle_init(hp, handle_size, base); |
| 155 | return 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 | |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 158 | return NULL; |
| 159 | } |
| 160 | |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 161 | static void mdesc_kfree(struct mdesc_handle *hp) |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 162 | { |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 163 | BUG_ON(atomic_read(&hp->refcnt) != 0); |
| 164 | BUG_ON(!list_empty(&hp->list)); |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 165 | |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 166 | kfree(hp->self_base); |
| 167 | } |
| 168 | |
| 169 | static struct mdesc_mem_ops kmalloc_mdesc_memops = { |
| 170 | .alloc = mdesc_kmalloc, |
| 171 | .free = mdesc_kfree, |
| 172 | }; |
| 173 | |
| 174 | static struct mdesc_handle *mdesc_alloc(unsigned int mdesc_size, |
| 175 | struct mdesc_mem_ops *mops) |
| 176 | { |
| 177 | struct mdesc_handle *hp = mops->alloc(mdesc_size); |
| 178 | |
| 179 | if (hp) |
| 180 | hp->mops = mops; |
| 181 | |
| 182 | return hp; |
| 183 | } |
| 184 | |
| 185 | static void mdesc_free(struct mdesc_handle *hp) |
| 186 | { |
| 187 | hp->mops->free(hp); |
| 188 | } |
| 189 | |
| 190 | static struct mdesc_handle *cur_mdesc; |
| 191 | static LIST_HEAD(mdesc_zombie_list); |
| 192 | static DEFINE_SPINLOCK(mdesc_lock); |
| 193 | |
| 194 | struct mdesc_handle *mdesc_grab(void) |
| 195 | { |
| 196 | struct mdesc_handle *hp; |
| 197 | unsigned long flags; |
| 198 | |
| 199 | spin_lock_irqsave(&mdesc_lock, flags); |
| 200 | hp = cur_mdesc; |
| 201 | if (hp) |
| 202 | atomic_inc(&hp->refcnt); |
| 203 | spin_unlock_irqrestore(&mdesc_lock, flags); |
| 204 | |
| 205 | return hp; |
| 206 | } |
| 207 | EXPORT_SYMBOL(mdesc_grab); |
| 208 | |
| 209 | void mdesc_release(struct mdesc_handle *hp) |
| 210 | { |
| 211 | unsigned long flags; |
| 212 | |
| 213 | spin_lock_irqsave(&mdesc_lock, flags); |
| 214 | if (atomic_dec_and_test(&hp->refcnt)) { |
| 215 | list_del_init(&hp->list); |
| 216 | hp->mops->free(hp); |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 217 | } |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 218 | spin_unlock_irqrestore(&mdesc_lock, flags); |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 219 | } |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 220 | EXPORT_SYMBOL(mdesc_release); |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 221 | |
David S. Miller | 920c3ed | 2007-07-17 21:37:35 -0700 | [diff] [blame] | 222 | static DEFINE_MUTEX(mdesc_mutex); |
| 223 | static struct mdesc_notifier_client *client_list; |
| 224 | |
| 225 | void mdesc_register_notifier(struct mdesc_notifier_client *client) |
| 226 | { |
| 227 | u64 node; |
| 228 | |
| 229 | mutex_lock(&mdesc_mutex); |
| 230 | client->next = client_list; |
| 231 | client_list = client; |
| 232 | |
| 233 | mdesc_for_each_node_by_name(cur_mdesc, node, client->node_name) |
| 234 | client->add(cur_mdesc, node); |
| 235 | |
| 236 | mutex_unlock(&mdesc_mutex); |
| 237 | } |
| 238 | |
David S. Miller | 91ba3c2 | 2007-07-18 15:15:45 -0700 | [diff] [blame] | 239 | static const u64 *parent_cfg_handle(struct mdesc_handle *hp, u64 node) |
| 240 | { |
| 241 | const u64 *id; |
| 242 | u64 a; |
| 243 | |
| 244 | id = NULL; |
| 245 | mdesc_for_each_arc(a, hp, node, MDESC_ARC_TYPE_BACK) { |
| 246 | u64 target; |
| 247 | |
| 248 | target = mdesc_arc_target(hp, a); |
| 249 | id = mdesc_get_property(hp, target, |
| 250 | "cfg-handle", NULL); |
| 251 | if (id) |
| 252 | break; |
| 253 | } |
| 254 | |
| 255 | return id; |
| 256 | } |
| 257 | |
David S. Miller | 920c3ed | 2007-07-17 21:37:35 -0700 | [diff] [blame] | 258 | /* Run 'func' on nodes which are in A but not in B. */ |
| 259 | static void invoke_on_missing(const char *name, |
| 260 | struct mdesc_handle *a, |
| 261 | struct mdesc_handle *b, |
| 262 | void (*func)(struct mdesc_handle *, u64)) |
| 263 | { |
| 264 | u64 node; |
| 265 | |
| 266 | mdesc_for_each_node_by_name(a, node, name) { |
David S. Miller | 91ba3c2 | 2007-07-18 15:15:45 -0700 | [diff] [blame] | 267 | int found = 0, is_vdc_port = 0; |
| 268 | const char *name_prop; |
| 269 | const u64 *id; |
David S. Miller | 920c3ed | 2007-07-17 21:37:35 -0700 | [diff] [blame] | 270 | u64 fnode; |
| 271 | |
David S. Miller | 91ba3c2 | 2007-07-18 15:15:45 -0700 | [diff] [blame] | 272 | name_prop = mdesc_get_property(a, node, "name", NULL); |
| 273 | if (name_prop && !strcmp(name_prop, "vdc-port")) { |
| 274 | is_vdc_port = 1; |
| 275 | id = parent_cfg_handle(a, node); |
| 276 | } else |
| 277 | id = mdesc_get_property(a, node, "id", NULL); |
| 278 | |
| 279 | if (!id) { |
| 280 | printk(KERN_ERR "MD: Cannot find ID for %s node.\n", |
| 281 | (name_prop ? name_prop : name)); |
| 282 | continue; |
| 283 | } |
| 284 | |
David S. Miller | 920c3ed | 2007-07-17 21:37:35 -0700 | [diff] [blame] | 285 | mdesc_for_each_node_by_name(b, fnode, name) { |
David S. Miller | 91ba3c2 | 2007-07-18 15:15:45 -0700 | [diff] [blame] | 286 | const u64 *fid; |
| 287 | |
| 288 | if (is_vdc_port) { |
| 289 | name_prop = mdesc_get_property(b, fnode, |
| 290 | "name", NULL); |
| 291 | if (!name_prop || |
| 292 | strcmp(name_prop, "vdc-port")) |
| 293 | continue; |
| 294 | fid = parent_cfg_handle(b, fnode); |
| 295 | if (!fid) { |
| 296 | printk(KERN_ERR "MD: Cannot find ID " |
| 297 | "for vdc-port node.\n"); |
| 298 | continue; |
| 299 | } |
| 300 | } else |
| 301 | fid = mdesc_get_property(b, fnode, |
| 302 | "id", NULL); |
David S. Miller | 920c3ed | 2007-07-17 21:37:35 -0700 | [diff] [blame] | 303 | |
| 304 | if (*id == *fid) { |
| 305 | found = 1; |
| 306 | break; |
| 307 | } |
| 308 | } |
| 309 | if (!found) |
| 310 | func(a, node); |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | static void notify_one(struct mdesc_notifier_client *p, |
| 315 | struct mdesc_handle *old_hp, |
| 316 | struct mdesc_handle *new_hp) |
| 317 | { |
| 318 | invoke_on_missing(p->node_name, old_hp, new_hp, p->remove); |
| 319 | invoke_on_missing(p->node_name, new_hp, old_hp, p->add); |
| 320 | } |
| 321 | |
| 322 | static void mdesc_notify_clients(struct mdesc_handle *old_hp, |
| 323 | struct mdesc_handle *new_hp) |
| 324 | { |
| 325 | struct mdesc_notifier_client *p = client_list; |
| 326 | |
| 327 | while (p) { |
| 328 | notify_one(p, old_hp, new_hp); |
| 329 | p = p->next; |
| 330 | } |
| 331 | } |
| 332 | |
David S. Miller | 778feeb4 | 2007-07-16 16:50:36 -0700 | [diff] [blame] | 333 | void mdesc_update(void) |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 334 | { |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 335 | unsigned long len, real_len, status; |
| 336 | struct mdesc_handle *hp, *orig_hp; |
| 337 | unsigned long flags; |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 338 | |
David S. Miller | 920c3ed | 2007-07-17 21:37:35 -0700 | [diff] [blame] | 339 | mutex_lock(&mdesc_mutex); |
| 340 | |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 341 | (void) sun4v_mach_desc(0UL, 0UL, &len); |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 342 | |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 343 | hp = mdesc_alloc(len, &kmalloc_mdesc_memops); |
| 344 | if (!hp) { |
| 345 | printk(KERN_ERR "MD: mdesc alloc fails\n"); |
David S. Miller | 920c3ed | 2007-07-17 21:37:35 -0700 | [diff] [blame] | 346 | goto out; |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 347 | } |
| 348 | |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 349 | status = sun4v_mach_desc(__pa(&hp->mdesc), len, &real_len); |
| 350 | if (status != HV_EOK || real_len > len) { |
| 351 | printk(KERN_ERR "MD: mdesc reread fails with %lu\n", |
| 352 | status); |
| 353 | atomic_dec(&hp->refcnt); |
| 354 | mdesc_free(hp); |
David S. Miller | 920c3ed | 2007-07-17 21:37:35 -0700 | [diff] [blame] | 355 | goto out; |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 356 | } |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 357 | |
| 358 | spin_lock_irqsave(&mdesc_lock, flags); |
| 359 | orig_hp = cur_mdesc; |
| 360 | cur_mdesc = hp; |
David S. Miller | 920c3ed | 2007-07-17 21:37:35 -0700 | [diff] [blame] | 361 | spin_unlock_irqrestore(&mdesc_lock, flags); |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 362 | |
David S. Miller | 920c3ed | 2007-07-17 21:37:35 -0700 | [diff] [blame] | 363 | mdesc_notify_clients(orig_hp, hp); |
| 364 | |
| 365 | spin_lock_irqsave(&mdesc_lock, flags); |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 366 | if (atomic_dec_and_test(&orig_hp->refcnt)) |
| 367 | mdesc_free(orig_hp); |
| 368 | else |
| 369 | list_add(&orig_hp->list, &mdesc_zombie_list); |
| 370 | spin_unlock_irqrestore(&mdesc_lock, flags); |
David S. Miller | 920c3ed | 2007-07-17 21:37:35 -0700 | [diff] [blame] | 371 | |
| 372 | out: |
| 373 | mutex_unlock(&mdesc_mutex); |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 374 | } |
| 375 | |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 376 | static struct mdesc_elem *node_block(struct mdesc_hdr *mdesc) |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 377 | { |
| 378 | return (struct mdesc_elem *) (mdesc + 1); |
| 379 | } |
| 380 | |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 381 | static void *name_block(struct mdesc_hdr *mdesc) |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 382 | { |
| 383 | return ((void *) node_block(mdesc)) + mdesc->node_sz; |
| 384 | } |
| 385 | |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 386 | static void *data_block(struct mdesc_hdr *mdesc) |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 387 | { |
| 388 | return ((void *) name_block(mdesc)) + mdesc->name_sz; |
| 389 | } |
| 390 | |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 391 | u64 mdesc_node_by_name(struct mdesc_handle *hp, |
| 392 | u64 from_node, const char *name) |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 393 | { |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 394 | struct mdesc_elem *ep = node_block(&hp->mdesc); |
| 395 | const char *names = name_block(&hp->mdesc); |
| 396 | u64 last_node = hp->mdesc.node_sz / 16; |
| 397 | u64 ret; |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 398 | |
David S. Miller | 778feeb4 | 2007-07-16 16:50:36 -0700 | [diff] [blame] | 399 | if (from_node == MDESC_NODE_NULL) { |
| 400 | ret = from_node = 0; |
| 401 | } else if (from_node >= last_node) { |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 402 | return MDESC_NODE_NULL; |
David S. Miller | 778feeb4 | 2007-07-16 16:50:36 -0700 | [diff] [blame] | 403 | } else { |
| 404 | ret = ep[from_node].d.val; |
| 405 | } |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 406 | |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 407 | while (ret < last_node) { |
| 408 | if (ep[ret].tag != MD_NODE) |
| 409 | return MDESC_NODE_NULL; |
| 410 | if (!strcmp(names + ep[ret].name_offset, name)) |
| 411 | break; |
| 412 | ret = ep[ret].d.val; |
| 413 | } |
| 414 | if (ret >= last_node) |
| 415 | ret = MDESC_NODE_NULL; |
| 416 | return ret; |
| 417 | } |
| 418 | EXPORT_SYMBOL(mdesc_node_by_name); |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 419 | |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 420 | const void *mdesc_get_property(struct mdesc_handle *hp, u64 node, |
| 421 | const char *name, int *lenp) |
| 422 | { |
| 423 | const char *names = name_block(&hp->mdesc); |
| 424 | u64 last_node = hp->mdesc.node_sz / 16; |
| 425 | void *data = data_block(&hp->mdesc); |
| 426 | struct mdesc_elem *ep; |
| 427 | |
| 428 | if (node == MDESC_NODE_NULL || node >= last_node) |
| 429 | return NULL; |
| 430 | |
| 431 | ep = node_block(&hp->mdesc) + node; |
| 432 | ep++; |
| 433 | for (; ep->tag != MD_NODE_END; ep++) { |
| 434 | void *val = NULL; |
| 435 | int len = 0; |
| 436 | |
| 437 | switch (ep->tag) { |
| 438 | case MD_PROP_VAL: |
| 439 | val = &ep->d.val; |
| 440 | len = 8; |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 441 | break; |
| 442 | |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 443 | case MD_PROP_STR: |
| 444 | case MD_PROP_DATA: |
| 445 | val = data + ep->d.data.data_offset; |
| 446 | len = ep->d.data.data_len; |
| 447 | break; |
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 | default: |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 450 | break; |
| 451 | } |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 452 | if (!val) |
| 453 | continue; |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 454 | |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 455 | if (!strcmp(names + ep->name_offset, name)) { |
| 456 | if (lenp) |
| 457 | *lenp = len; |
| 458 | return val; |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 459 | } |
| 460 | } |
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 | return NULL; |
| 463 | } |
| 464 | EXPORT_SYMBOL(mdesc_get_property); |
| 465 | |
| 466 | 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] | 467 | { |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 468 | struct mdesc_elem *ep, *base = node_block(&hp->mdesc); |
| 469 | const char *names = name_block(&hp->mdesc); |
| 470 | u64 last_node = hp->mdesc.node_sz / 16; |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 471 | |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 472 | if (from == MDESC_NODE_NULL || from >= last_node) |
| 473 | return MDESC_NODE_NULL; |
| 474 | |
| 475 | ep = base + from; |
| 476 | |
| 477 | ep++; |
| 478 | for (; ep->tag != MD_NODE_END; ep++) { |
| 479 | if (ep->tag != MD_PROP_ARC) |
| 480 | continue; |
| 481 | |
| 482 | if (strcmp(names + ep->name_offset, arc_type)) |
| 483 | continue; |
| 484 | |
| 485 | return ep - base; |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 486 | } |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 487 | |
| 488 | return MDESC_NODE_NULL; |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 489 | } |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 490 | EXPORT_SYMBOL(mdesc_next_arc); |
| 491 | |
| 492 | u64 mdesc_arc_target(struct mdesc_handle *hp, u64 arc) |
| 493 | { |
| 494 | struct mdesc_elem *ep, *base = node_block(&hp->mdesc); |
| 495 | |
| 496 | ep = base + arc; |
| 497 | |
| 498 | return ep->d.val; |
| 499 | } |
| 500 | EXPORT_SYMBOL(mdesc_arc_target); |
| 501 | |
| 502 | const char *mdesc_node_name(struct mdesc_handle *hp, u64 node) |
| 503 | { |
| 504 | struct mdesc_elem *ep, *base = node_block(&hp->mdesc); |
| 505 | const char *names = name_block(&hp->mdesc); |
| 506 | u64 last_node = hp->mdesc.node_sz / 16; |
| 507 | |
| 508 | if (node == MDESC_NODE_NULL || node >= last_node) |
| 509 | return NULL; |
| 510 | |
| 511 | ep = base + node; |
| 512 | if (ep->tag != MD_NODE) |
| 513 | return NULL; |
| 514 | |
| 515 | return names + ep->name_offset; |
| 516 | } |
| 517 | EXPORT_SYMBOL(mdesc_node_name); |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 518 | |
| 519 | static void __init report_platform_properties(void) |
| 520 | { |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 521 | struct mdesc_handle *hp = mdesc_grab(); |
| 522 | u64 pn = mdesc_node_by_name(hp, MDESC_NODE_NULL, "platform"); |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 523 | const char *s; |
| 524 | const u64 *v; |
| 525 | |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 526 | if (pn == MDESC_NODE_NULL) { |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 527 | prom_printf("No platform node in machine-description.\n"); |
| 528 | prom_halt(); |
| 529 | } |
| 530 | |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 531 | s = mdesc_get_property(hp, pn, "banner-name", NULL); |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 532 | printk("PLATFORM: banner-name [%s]\n", s); |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 533 | s = mdesc_get_property(hp, pn, "name", NULL); |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 534 | printk("PLATFORM: name [%s]\n", s); |
| 535 | |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 536 | v = mdesc_get_property(hp, pn, "hostid", NULL); |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 537 | if (v) |
| 538 | printk("PLATFORM: hostid [%08lx]\n", *v); |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 539 | v = mdesc_get_property(hp, pn, "serial#", NULL); |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 540 | if (v) |
| 541 | printk("PLATFORM: serial# [%08lx]\n", *v); |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 542 | v = mdesc_get_property(hp, pn, "stick-frequency", NULL); |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 543 | printk("PLATFORM: stick-frequency [%08lx]\n", *v); |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 544 | v = mdesc_get_property(hp, pn, "mac-address", NULL); |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 545 | if (v) |
| 546 | printk("PLATFORM: mac-address [%lx]\n", *v); |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 547 | v = mdesc_get_property(hp, pn, "watchdog-resolution", NULL); |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 548 | if (v) |
| 549 | printk("PLATFORM: watchdog-resolution [%lu ms]\n", *v); |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 550 | v = mdesc_get_property(hp, pn, "watchdog-max-timeout", NULL); |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 551 | if (v) |
| 552 | printk("PLATFORM: watchdog-max-timeout [%lu ms]\n", *v); |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 553 | v = mdesc_get_property(hp, pn, "max-cpus", NULL); |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 554 | if (v) |
| 555 | printk("PLATFORM: max-cpus [%lu]\n", *v); |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 556 | |
David S. Miller | 4f0234f | 2007-07-13 16:03:42 -0700 | [diff] [blame] | 557 | #ifdef CONFIG_SMP |
| 558 | { |
| 559 | int max_cpu, i; |
| 560 | |
| 561 | if (v) { |
| 562 | max_cpu = *v; |
| 563 | if (max_cpu > NR_CPUS) |
| 564 | max_cpu = NR_CPUS; |
| 565 | } else { |
| 566 | max_cpu = NR_CPUS; |
| 567 | } |
| 568 | for (i = 0; i < max_cpu; i++) |
| 569 | cpu_set(i, cpu_possible_map); |
| 570 | } |
| 571 | #endif |
| 572 | |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 573 | mdesc_release(hp); |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 574 | } |
| 575 | |
David S. Miller | 4f0234f | 2007-07-13 16:03:42 -0700 | [diff] [blame] | 576 | static void __devinit fill_in_one_cache(cpuinfo_sparc *c, |
| 577 | struct mdesc_handle *hp, |
| 578 | u64 mp) |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 579 | { |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 580 | const u64 *level = mdesc_get_property(hp, mp, "level", NULL); |
| 581 | const u64 *size = mdesc_get_property(hp, mp, "size", NULL); |
| 582 | 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] | 583 | const char *type; |
| 584 | int type_len; |
| 585 | |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 586 | type = mdesc_get_property(hp, mp, "type", &type_len); |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 587 | |
| 588 | switch (*level) { |
| 589 | case 1: |
David S. Miller | 46bcea7 | 2007-08-07 18:46:36 -0700 | [diff] [blame] | 590 | if (of_find_in_proplist(type, "instn", type_len)) { |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 591 | c->icache_size = *size; |
| 592 | c->icache_line_size = *line_size; |
David S. Miller | 46bcea7 | 2007-08-07 18:46:36 -0700 | [diff] [blame] | 593 | } else if (of_find_in_proplist(type, "data", type_len)) { |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 594 | c->dcache_size = *size; |
| 595 | c->dcache_line_size = *line_size; |
| 596 | } |
| 597 | break; |
| 598 | |
| 599 | case 2: |
| 600 | c->ecache_size = *size; |
| 601 | c->ecache_line_size = *line_size; |
| 602 | break; |
| 603 | |
| 604 | default: |
| 605 | break; |
| 606 | } |
| 607 | |
| 608 | if (*level == 1) { |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 609 | u64 a; |
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 | mdesc_for_each_arc(a, hp, mp, MDESC_ARC_TYPE_FWD) { |
| 612 | u64 target = mdesc_arc_target(hp, a); |
| 613 | const char *name = mdesc_node_name(hp, target); |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 614 | |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 615 | if (!strcmp(name, "cache")) |
| 616 | fill_in_one_cache(c, hp, target); |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 617 | } |
| 618 | } |
| 619 | } |
| 620 | |
David S. Miller | 4f0234f | 2007-07-13 16:03:42 -0700 | [diff] [blame] | 621 | static void __devinit mark_core_ids(struct mdesc_handle *hp, u64 mp, |
| 622 | int core_id) |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 623 | { |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 624 | u64 a; |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 625 | |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 626 | mdesc_for_each_arc(a, hp, mp, MDESC_ARC_TYPE_BACK) { |
| 627 | u64 t = mdesc_arc_target(hp, a); |
| 628 | const char *name; |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 629 | const u64 *id; |
| 630 | |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 631 | name = mdesc_node_name(hp, t); |
| 632 | if (!strcmp(name, "cpu")) { |
| 633 | id = mdesc_get_property(hp, t, "id", NULL); |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 634 | if (*id < NR_CPUS) |
| 635 | cpu_data(*id).core_id = core_id; |
| 636 | } else { |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 637 | u64 j; |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 638 | |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 639 | mdesc_for_each_arc(j, hp, t, MDESC_ARC_TYPE_BACK) { |
| 640 | u64 n = mdesc_arc_target(hp, j); |
| 641 | const char *n_name; |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 642 | |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 643 | n_name = mdesc_node_name(hp, n); |
| 644 | if (strcmp(n_name, "cpu")) |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 645 | continue; |
| 646 | |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 647 | id = mdesc_get_property(hp, n, "id", NULL); |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 648 | if (*id < NR_CPUS) |
| 649 | cpu_data(*id).core_id = core_id; |
| 650 | } |
| 651 | } |
| 652 | } |
| 653 | } |
| 654 | |
David S. Miller | 4f0234f | 2007-07-13 16:03:42 -0700 | [diff] [blame] | 655 | static void __devinit set_core_ids(struct mdesc_handle *hp) |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 656 | { |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 657 | int idx; |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 658 | u64 mp; |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 659 | |
| 660 | idx = 1; |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 661 | mdesc_for_each_node_by_name(hp, mp, "cache") { |
| 662 | const u64 *level; |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 663 | const char *type; |
| 664 | int len; |
| 665 | |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 666 | level = mdesc_get_property(hp, mp, "level", NULL); |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 667 | if (*level != 1) |
| 668 | continue; |
| 669 | |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 670 | type = mdesc_get_property(hp, mp, "type", &len); |
David S. Miller | 46bcea7 | 2007-08-07 18:46:36 -0700 | [diff] [blame] | 671 | if (!of_find_in_proplist(type, "instn", len)) |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 672 | continue; |
| 673 | |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 674 | mark_core_ids(hp, mp, idx); |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 675 | |
| 676 | idx++; |
| 677 | } |
| 678 | } |
| 679 | |
David S. Miller | 4f0234f | 2007-07-13 16:03:42 -0700 | [diff] [blame] | 680 | static void __devinit mark_proc_ids(struct mdesc_handle *hp, u64 mp, |
| 681 | int proc_id) |
David S. Miller | f78eae2 | 2007-06-04 17:01:39 -0700 | [diff] [blame] | 682 | { |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 683 | u64 a; |
David S. Miller | f78eae2 | 2007-06-04 17:01:39 -0700 | [diff] [blame] | 684 | |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 685 | mdesc_for_each_arc(a, hp, mp, MDESC_ARC_TYPE_BACK) { |
| 686 | u64 t = mdesc_arc_target(hp, a); |
| 687 | const char *name; |
David S. Miller | f78eae2 | 2007-06-04 17:01:39 -0700 | [diff] [blame] | 688 | const u64 *id; |
| 689 | |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 690 | name = mdesc_node_name(hp, t); |
| 691 | if (strcmp(name, "cpu")) |
David S. Miller | f78eae2 | 2007-06-04 17:01:39 -0700 | [diff] [blame] | 692 | continue; |
| 693 | |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 694 | id = mdesc_get_property(hp, t, "id", NULL); |
David S. Miller | f78eae2 | 2007-06-04 17:01:39 -0700 | [diff] [blame] | 695 | if (*id < NR_CPUS) |
| 696 | cpu_data(*id).proc_id = proc_id; |
| 697 | } |
| 698 | } |
| 699 | |
David S. Miller | 4f0234f | 2007-07-13 16:03:42 -0700 | [diff] [blame] | 700 | static void __devinit __set_proc_ids(struct mdesc_handle *hp, |
| 701 | const char *exec_unit_name) |
David S. Miller | f78eae2 | 2007-06-04 17:01:39 -0700 | [diff] [blame] | 702 | { |
David S. Miller | f78eae2 | 2007-06-04 17:01:39 -0700 | [diff] [blame] | 703 | int idx; |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 704 | u64 mp; |
David S. Miller | f78eae2 | 2007-06-04 17:01:39 -0700 | [diff] [blame] | 705 | |
| 706 | idx = 0; |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 707 | mdesc_for_each_node_by_name(hp, mp, exec_unit_name) { |
David S. Miller | f78eae2 | 2007-06-04 17:01:39 -0700 | [diff] [blame] | 708 | const char *type; |
| 709 | int len; |
| 710 | |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 711 | type = mdesc_get_property(hp, mp, "type", &len); |
David S. Miller | 46bcea7 | 2007-08-07 18:46:36 -0700 | [diff] [blame] | 712 | if (!of_find_in_proplist(type, "int", len) && |
| 713 | !of_find_in_proplist(type, "integer", len)) |
David S. Miller | f78eae2 | 2007-06-04 17:01:39 -0700 | [diff] [blame] | 714 | continue; |
| 715 | |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 716 | mark_proc_ids(hp, mp, idx); |
David S. Miller | f78eae2 | 2007-06-04 17:01:39 -0700 | [diff] [blame] | 717 | |
| 718 | idx++; |
| 719 | } |
| 720 | } |
| 721 | |
David S. Miller | 4f0234f | 2007-07-13 16:03:42 -0700 | [diff] [blame] | 722 | static void __devinit set_proc_ids(struct mdesc_handle *hp) |
David S. Miller | f78eae2 | 2007-06-04 17:01:39 -0700 | [diff] [blame] | 723 | { |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 724 | __set_proc_ids(hp, "exec_unit"); |
| 725 | __set_proc_ids(hp, "exec-unit"); |
David S. Miller | f78eae2 | 2007-06-04 17:01:39 -0700 | [diff] [blame] | 726 | } |
| 727 | |
David S. Miller | 4f0234f | 2007-07-13 16:03:42 -0700 | [diff] [blame] | 728 | static void __devinit get_one_mondo_bits(const u64 *p, unsigned int *mask, |
| 729 | unsigned char def) |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 730 | { |
| 731 | u64 val; |
| 732 | |
| 733 | if (!p) |
| 734 | goto use_default; |
| 735 | val = *p; |
| 736 | |
| 737 | if (!val || val >= 64) |
| 738 | goto use_default; |
| 739 | |
| 740 | *mask = ((1U << val) * 64U) - 1U; |
| 741 | return; |
| 742 | |
| 743 | use_default: |
| 744 | *mask = ((1U << def) * 64U) - 1U; |
| 745 | } |
| 746 | |
David S. Miller | 4f0234f | 2007-07-13 16:03:42 -0700 | [diff] [blame] | 747 | static void __devinit get_mondo_data(struct mdesc_handle *hp, u64 mp, |
| 748 | struct trap_per_cpu *tb) |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 749 | { |
| 750 | const u64 *val; |
| 751 | |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 752 | val = mdesc_get_property(hp, mp, "q-cpu-mondo-#bits", NULL); |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 753 | get_one_mondo_bits(val, &tb->cpu_mondo_qmask, 7); |
| 754 | |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 755 | val = mdesc_get_property(hp, mp, "q-dev-mondo-#bits", NULL); |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 756 | get_one_mondo_bits(val, &tb->dev_mondo_qmask, 7); |
| 757 | |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 758 | val = mdesc_get_property(hp, mp, "q-resumable-#bits", NULL); |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 759 | get_one_mondo_bits(val, &tb->resum_qmask, 6); |
| 760 | |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 761 | val = mdesc_get_property(hp, mp, "q-nonresumable-#bits", NULL); |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 762 | get_one_mondo_bits(val, &tb->nonresum_qmask, 2); |
| 763 | } |
| 764 | |
Sam Ravnborg | 7769bd1 | 2008-02-24 19:47:51 -0800 | [diff] [blame] | 765 | void __cpuinit mdesc_fill_in_cpu_data(cpumask_t mask) |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 766 | { |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 767 | struct mdesc_handle *hp = mdesc_grab(); |
| 768 | u64 mp; |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 769 | |
| 770 | ncpus_probed = 0; |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 771 | mdesc_for_each_node_by_name(hp, mp, "cpu") { |
| 772 | const u64 *id = mdesc_get_property(hp, mp, "id", NULL); |
| 773 | const u64 *cfreq = mdesc_get_property(hp, mp, "clock-frequency", NULL); |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 774 | struct trap_per_cpu *tb; |
| 775 | cpuinfo_sparc *c; |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 776 | int cpuid; |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 777 | u64 a; |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 778 | |
| 779 | ncpus_probed++; |
| 780 | |
| 781 | cpuid = *id; |
| 782 | |
| 783 | #ifdef CONFIG_SMP |
David S. Miller | 8a177c4 | 2007-09-16 14:45:06 -0700 | [diff] [blame] | 784 | if (cpuid >= NR_CPUS) { |
| 785 | printk(KERN_WARNING "Ignoring CPU %d which is " |
| 786 | ">= NR_CPUS (%d)\n", |
| 787 | cpuid, NR_CPUS); |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 788 | continue; |
David S. Miller | 8a177c4 | 2007-09-16 14:45:06 -0700 | [diff] [blame] | 789 | } |
David S. Miller | 4f0234f | 2007-07-13 16:03:42 -0700 | [diff] [blame] | 790 | if (!cpu_isset(cpuid, mask)) |
| 791 | continue; |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 792 | #else |
| 793 | /* On uniprocessor we only want the values for the |
| 794 | * real physical cpu the kernel booted onto, however |
| 795 | * cpu_data() only has one entry at index 0. |
| 796 | */ |
| 797 | if (cpuid != real_hard_smp_processor_id()) |
| 798 | continue; |
| 799 | cpuid = 0; |
| 800 | #endif |
| 801 | |
| 802 | c = &cpu_data(cpuid); |
| 803 | c->clock_tick = *cfreq; |
| 804 | |
| 805 | tb = &trap_block[cpuid]; |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 806 | get_mondo_data(hp, mp, tb); |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 807 | |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 808 | mdesc_for_each_arc(a, hp, mp, MDESC_ARC_TYPE_FWD) { |
| 809 | u64 j, t = mdesc_arc_target(hp, a); |
| 810 | const char *t_name; |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 811 | |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 812 | t_name = mdesc_node_name(hp, t); |
| 813 | if (!strcmp(t_name, "cache")) { |
| 814 | fill_in_one_cache(c, hp, t); |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 815 | continue; |
| 816 | } |
| 817 | |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 818 | mdesc_for_each_arc(j, hp, t, MDESC_ARC_TYPE_FWD) { |
| 819 | u64 n = mdesc_arc_target(hp, j); |
| 820 | const char *n_name; |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 821 | |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 822 | n_name = mdesc_node_name(hp, n); |
| 823 | if (!strcmp(n_name, "cache")) |
| 824 | fill_in_one_cache(c, hp, n); |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 825 | } |
| 826 | } |
| 827 | |
| 828 | #ifdef CONFIG_SMP |
| 829 | cpu_set(cpuid, cpu_present_map); |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 830 | #endif |
| 831 | |
| 832 | c->core_id = 0; |
David S. Miller | f78eae2 | 2007-06-04 17:01:39 -0700 | [diff] [blame] | 833 | c->proc_id = -1; |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 834 | } |
| 835 | |
David S. Miller | a2f9f6b | 2007-06-04 21:48:33 -0700 | [diff] [blame] | 836 | #ifdef CONFIG_SMP |
| 837 | sparc64_multi_core = 1; |
| 838 | #endif |
| 839 | |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 840 | set_core_ids(hp); |
| 841 | set_proc_ids(hp); |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 842 | |
| 843 | smp_fill_in_sib_core_maps(); |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 844 | |
| 845 | mdesc_release(hp); |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 846 | } |
| 847 | |
David S. Miller | 0fdb7f9 | 2007-08-15 21:02:23 -0700 | [diff] [blame] | 848 | static ssize_t mdesc_read(struct file *file, char __user *buf, |
| 849 | size_t len, loff_t *offp) |
| 850 | { |
| 851 | struct mdesc_handle *hp = mdesc_grab(); |
| 852 | int err; |
| 853 | |
| 854 | if (!hp) |
| 855 | return -ENODEV; |
| 856 | |
| 857 | err = hp->handle_size; |
| 858 | if (len < hp->handle_size) |
| 859 | err = -EMSGSIZE; |
| 860 | else if (copy_to_user(buf, &hp->mdesc, hp->handle_size)) |
| 861 | err = -EFAULT; |
| 862 | mdesc_release(hp); |
| 863 | |
| 864 | return err; |
| 865 | } |
| 866 | |
| 867 | static const struct file_operations mdesc_fops = { |
| 868 | .read = mdesc_read, |
| 869 | .owner = THIS_MODULE, |
| 870 | }; |
| 871 | |
| 872 | static struct miscdevice mdesc_misc = { |
| 873 | .minor = MISC_DYNAMIC_MINOR, |
| 874 | .name = "mdesc", |
| 875 | .fops = &mdesc_fops, |
| 876 | }; |
| 877 | |
| 878 | static int __init mdesc_misc_init(void) |
| 879 | { |
| 880 | return misc_register(&mdesc_misc); |
| 881 | } |
| 882 | |
| 883 | __initcall(mdesc_misc_init); |
| 884 | |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 885 | void __init sun4v_mdesc_init(void) |
| 886 | { |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 887 | struct mdesc_handle *hp; |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 888 | unsigned long len, real_len, status; |
David S. Miller | 4f0234f | 2007-07-13 16:03:42 -0700 | [diff] [blame] | 889 | cpumask_t mask; |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 890 | |
| 891 | (void) sun4v_mach_desc(0UL, 0UL, &len); |
| 892 | |
| 893 | printk("MDESC: Size is %lu bytes.\n", len); |
| 894 | |
David S. Miller | 4a28333 | 2008-02-13 19:22:23 -0800 | [diff] [blame] | 895 | hp = mdesc_alloc(len, &lmb_mdesc_ops); |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 896 | if (hp == NULL) { |
| 897 | prom_printf("MDESC: alloc of %lu bytes failed.\n", len); |
| 898 | prom_halt(); |
| 899 | } |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 900 | |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 901 | status = sun4v_mach_desc(__pa(&hp->mdesc), len, &real_len); |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 902 | if (status != HV_EOK || real_len > len) { |
| 903 | prom_printf("sun4v_mach_desc fails, err(%lu), " |
| 904 | "len(%lu), real_len(%lu)\n", |
| 905 | status, len, real_len); |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 906 | mdesc_free(hp); |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 907 | prom_halt(); |
| 908 | } |
| 909 | |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 910 | cur_mdesc = hp; |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 911 | |
| 912 | report_platform_properties(); |
David S. Miller | 4f0234f | 2007-07-13 16:03:42 -0700 | [diff] [blame] | 913 | |
| 914 | cpus_setall(mask); |
| 915 | mdesc_fill_in_cpu_data(mask); |
David S. Miller | 5cbc307 | 2007-05-25 15:49:59 -0700 | [diff] [blame] | 916 | } |