1 /* 2 * spu management operations for of based platforms 3 * 4 * (C) Copyright IBM Deutschland Entwicklung GmbH 2005 5 * Copyright 2006 Sony Corp. 6 * (C) Copyright 2007 TOSHIBA CORPORATION 7 * 8 * This program is free software; you can redistribute it and/or modify 9 * it under the terms of the GNU General Public License as published by 10 * the Free Software Foundation; version 2 of the License. 11 * 12 * This program is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * GNU General Public License for more details. 16 * 17 * You should have received a copy of the GNU General Public License along 18 * with this program; if not, write to the Free Software Foundation, Inc., 19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 20 */ 21 22 #include <linux/interrupt.h> 23 #include <linux/list.h> 24 #include <linux/export.h> 25 #include <linux/ptrace.h> 26 #include <linux/wait.h> 27 #include <linux/mm.h> 28 #include <linux/io.h> 29 #include <linux/mutex.h> 30 #include <linux/device.h> 31 32 #include <asm/spu.h> 33 #include <asm/spu_priv1.h> 34 #include <asm/firmware.h> 35 #include <asm/prom.h> 36 37 #include "spufs/spufs.h" 38 #include "interrupt.h" 39 40 struct device_node *spu_devnode(struct spu *spu) 41 { 42 return spu->devnode; 43 } 44 45 EXPORT_SYMBOL_GPL(spu_devnode); 46 47 static u64 __init find_spu_unit_number(struct device_node *spe) 48 { 49 const unsigned int *prop; 50 int proplen; 51 52 /* new device trees should provide the physical-id attribute */ 53 prop = of_get_property(spe, "physical-id", &proplen); 54 if (proplen == 4) 55 return (u64)*prop; 56 57 /* celleb device tree provides the unit-id */ 58 prop = of_get_property(spe, "unit-id", &proplen); 59 if (proplen == 4) 60 return (u64)*prop; 61 62 /* legacy device trees provide the id in the reg attribute */ 63 prop = of_get_property(spe, "reg", &proplen); 64 if (proplen == 4) 65 return (u64)*prop; 66 67 return 0; 68 } 69 70 static void spu_unmap(struct spu *spu) 71 { 72 if (!firmware_has_feature(FW_FEATURE_LPAR)) 73 iounmap(spu->priv1); 74 iounmap(spu->priv2); 75 iounmap(spu->problem); 76 iounmap((__force u8 __iomem *)spu->local_store); 77 } 78 79 static int __init spu_map_interrupts_old(struct spu *spu, 80 struct device_node *np) 81 { 82 unsigned int isrc; 83 const u32 *tmp; 84 int nid; 85 86 /* Get the interrupt source unit from the device-tree */ 87 tmp = of_get_property(np, "isrc", NULL); 88 if (!tmp) 89 return -ENODEV; 90 isrc = tmp[0]; 91 92 tmp = of_get_property(np->parent->parent, "node-id", NULL); 93 if (!tmp) { 94 printk(KERN_WARNING "%s: can't find node-id\n", __func__); 95 nid = spu->node; 96 } else 97 nid = tmp[0]; 98 99 /* Add the node number */ 100 isrc |= nid << IIC_IRQ_NODE_SHIFT; 101 102 /* Now map interrupts of all 3 classes */ 103 spu->irqs[0] = irq_create_mapping(NULL, IIC_IRQ_CLASS_0 | isrc); 104 spu->irqs[1] = irq_create_mapping(NULL, IIC_IRQ_CLASS_1 | isrc); 105 spu->irqs[2] = irq_create_mapping(NULL, IIC_IRQ_CLASS_2 | isrc); 106 107 /* Right now, we only fail if class 2 failed */ 108 if (!spu->irqs[2]) 109 return -EINVAL; 110 111 return 0; 112 } 113 114 static void __iomem * __init spu_map_prop_old(struct spu *spu, 115 struct device_node *n, 116 const char *name) 117 { 118 const struct address_prop { 119 unsigned long address; 120 unsigned int len; 121 } __attribute__((packed)) *prop; 122 int proplen; 123 124 prop = of_get_property(n, name, &proplen); 125 if (prop == NULL || proplen != sizeof (struct address_prop)) 126 return NULL; 127 128 return ioremap(prop->address, prop->len); 129 } 130 131 static int __init spu_map_device_old(struct spu *spu) 132 { 133 struct device_node *node = spu->devnode; 134 const char *prop; 135 int ret; 136 137 ret = -ENODEV; 138 spu->name = of_get_property(node, "name", NULL); 139 if (!spu->name) 140 goto out; 141 142 prop = of_get_property(node, "local-store", NULL); 143 if (!prop) 144 goto out; 145 spu->local_store_phys = *(unsigned long *)prop; 146 147 /* we use local store as ram, not io memory */ 148 spu->local_store = (void __force *) 149 spu_map_prop_old(spu, node, "local-store"); 150 if (!spu->local_store) 151 goto out; 152 153 prop = of_get_property(node, "problem", NULL); 154 if (!prop) 155 goto out_unmap; 156 spu->problem_phys = *(unsigned long *)prop; 157 158 spu->problem = spu_map_prop_old(spu, node, "problem"); 159 if (!spu->problem) 160 goto out_unmap; 161 162 spu->priv2 = spu_map_prop_old(spu, node, "priv2"); 163 if (!spu->priv2) 164 goto out_unmap; 165 166 if (!firmware_has_feature(FW_FEATURE_LPAR)) { 167 spu->priv1 = spu_map_prop_old(spu, node, "priv1"); 168 if (!spu->priv1) 169 goto out_unmap; 170 } 171 172 ret = 0; 173 goto out; 174 175 out_unmap: 176 spu_unmap(spu); 177 out: 178 return ret; 179 } 180 181 static int __init spu_map_interrupts(struct spu *spu, struct device_node *np) 182 { 183 int i; 184 185 for (i=0; i < 3; i++) { 186 spu->irqs[i] = irq_of_parse_and_map(np, i); 187 if (!spu->irqs[i]) 188 goto err; 189 } 190 return 0; 191 192 err: 193 pr_debug("failed to map irq %x for spu %s\n", i, spu->name); 194 for (; i >= 0; i--) { 195 if (spu->irqs[i]) 196 irq_dispose_mapping(spu->irqs[i]); 197 } 198 return -EINVAL; 199 } 200 201 static int spu_map_resource(struct spu *spu, int nr, 202 void __iomem** virt, unsigned long *phys) 203 { 204 struct device_node *np = spu->devnode; 205 struct resource resource = { }; 206 unsigned long len; 207 int ret; 208 209 ret = of_address_to_resource(np, nr, &resource); 210 if (ret) 211 return ret; 212 if (phys) 213 *phys = resource.start; 214 len = resource_size(&resource); 215 *virt = ioremap(resource.start, len); 216 if (!*virt) 217 return -EINVAL; 218 return 0; 219 } 220 221 static int __init spu_map_device(struct spu *spu) 222 { 223 struct device_node *np = spu->devnode; 224 int ret = -ENODEV; 225 226 spu->name = of_get_property(np, "name", NULL); 227 if (!spu->name) 228 goto out; 229 230 ret = spu_map_resource(spu, 0, (void __iomem**)&spu->local_store, 231 &spu->local_store_phys); 232 if (ret) { 233 pr_debug("spu_new: failed to map %pOF resource 0\n", 234 np); 235 goto out; 236 } 237 ret = spu_map_resource(spu, 1, (void __iomem**)&spu->problem, 238 &spu->problem_phys); 239 if (ret) { 240 pr_debug("spu_new: failed to map %pOF resource 1\n", 241 np); 242 goto out_unmap; 243 } 244 ret = spu_map_resource(spu, 2, (void __iomem**)&spu->priv2, NULL); 245 if (ret) { 246 pr_debug("spu_new: failed to map %pOF resource 2\n", 247 np); 248 goto out_unmap; 249 } 250 if (!firmware_has_feature(FW_FEATURE_LPAR)) 251 ret = spu_map_resource(spu, 3, 252 (void __iomem**)&spu->priv1, NULL); 253 if (ret) { 254 pr_debug("spu_new: failed to map %pOF resource 3\n", 255 np); 256 goto out_unmap; 257 } 258 pr_debug("spu_new: %pOF maps:\n", np); 259 pr_debug(" local store : 0x%016lx -> 0x%p\n", 260 spu->local_store_phys, spu->local_store); 261 pr_debug(" problem state : 0x%016lx -> 0x%p\n", 262 spu->problem_phys, spu->problem); 263 pr_debug(" priv2 : 0x%p\n", spu->priv2); 264 pr_debug(" priv1 : 0x%p\n", spu->priv1); 265 266 return 0; 267 268 out_unmap: 269 spu_unmap(spu); 270 out: 271 pr_debug("failed to map spe %s: %d\n", spu->name, ret); 272 return ret; 273 } 274 275 static int __init of_enumerate_spus(int (*fn)(void *data)) 276 { 277 int ret; 278 struct device_node *node; 279 unsigned int n = 0; 280 281 ret = -ENODEV; 282 for_each_node_by_type(node, "spe") { 283 ret = fn(node); 284 if (ret) { 285 printk(KERN_WARNING "%s: Error initializing %pOFn\n", 286 __func__, node); 287 of_node_put(node); 288 break; 289 } 290 n++; 291 } 292 return ret ? ret : n; 293 } 294 295 static int __init of_create_spu(struct spu *spu, void *data) 296 { 297 int ret; 298 struct device_node *spe = (struct device_node *)data; 299 static int legacy_map = 0, legacy_irq = 0; 300 301 spu->devnode = of_node_get(spe); 302 spu->spe_id = find_spu_unit_number(spe); 303 304 spu->node = of_node_to_nid(spe); 305 if (spu->node >= MAX_NUMNODES) { 306 printk(KERN_WARNING "SPE %pOF on node %d ignored," 307 " node number too big\n", spe, spu->node); 308 printk(KERN_WARNING "Check if CONFIG_NUMA is enabled.\n"); 309 ret = -ENODEV; 310 goto out; 311 } 312 313 ret = spu_map_device(spu); 314 if (ret) { 315 if (!legacy_map) { 316 legacy_map = 1; 317 printk(KERN_WARNING "%s: Legacy device tree found, " 318 "trying to map old style\n", __func__); 319 } 320 ret = spu_map_device_old(spu); 321 if (ret) { 322 printk(KERN_ERR "Unable to map %s\n", 323 spu->name); 324 goto out; 325 } 326 } 327 328 ret = spu_map_interrupts(spu, spe); 329 if (ret) { 330 if (!legacy_irq) { 331 legacy_irq = 1; 332 printk(KERN_WARNING "%s: Legacy device tree found, " 333 "trying old style irq\n", __func__); 334 } 335 ret = spu_map_interrupts_old(spu, spe); 336 if (ret) { 337 printk(KERN_ERR "%s: could not map interrupts\n", 338 spu->name); 339 goto out_unmap; 340 } 341 } 342 343 pr_debug("Using SPE %s %p %p %p %p %d\n", spu->name, 344 spu->local_store, spu->problem, spu->priv1, 345 spu->priv2, spu->number); 346 goto out; 347 348 out_unmap: 349 spu_unmap(spu); 350 out: 351 return ret; 352 } 353 354 static int of_destroy_spu(struct spu *spu) 355 { 356 spu_unmap(spu); 357 of_node_put(spu->devnode); 358 return 0; 359 } 360 361 static void enable_spu_by_master_run(struct spu_context *ctx) 362 { 363 ctx->ops->master_start(ctx); 364 } 365 366 static void disable_spu_by_master_run(struct spu_context *ctx) 367 { 368 ctx->ops->master_stop(ctx); 369 } 370 371 /* Hardcoded affinity idxs for qs20 */ 372 #define QS20_SPES_PER_BE 8 373 static int qs20_reg_idxs[QS20_SPES_PER_BE] = { 0, 2, 4, 6, 7, 5, 3, 1 }; 374 static int qs20_reg_memory[QS20_SPES_PER_BE] = { 1, 1, 0, 0, 0, 0, 0, 0 }; 375 376 static struct spu *spu_lookup_reg(int node, u32 reg) 377 { 378 struct spu *spu; 379 const u32 *spu_reg; 380 381 list_for_each_entry(spu, &cbe_spu_info[node].spus, cbe_list) { 382 spu_reg = of_get_property(spu_devnode(spu), "reg", NULL); 383 if (*spu_reg == reg) 384 return spu; 385 } 386 return NULL; 387 } 388 389 static void init_affinity_qs20_harcoded(void) 390 { 391 int node, i; 392 struct spu *last_spu, *spu; 393 u32 reg; 394 395 for (node = 0; node < MAX_NUMNODES; node++) { 396 last_spu = NULL; 397 for (i = 0; i < QS20_SPES_PER_BE; i++) { 398 reg = qs20_reg_idxs[i]; 399 spu = spu_lookup_reg(node, reg); 400 if (!spu) 401 continue; 402 spu->has_mem_affinity = qs20_reg_memory[reg]; 403 if (last_spu) 404 list_add_tail(&spu->aff_list, 405 &last_spu->aff_list); 406 last_spu = spu; 407 } 408 } 409 } 410 411 static int of_has_vicinity(void) 412 { 413 struct device_node *dn; 414 415 for_each_node_by_type(dn, "spe") { 416 if (of_find_property(dn, "vicinity", NULL)) { 417 of_node_put(dn); 418 return 1; 419 } 420 } 421 return 0; 422 } 423 424 static struct spu *devnode_spu(int cbe, struct device_node *dn) 425 { 426 struct spu *spu; 427 428 list_for_each_entry(spu, &cbe_spu_info[cbe].spus, cbe_list) 429 if (spu_devnode(spu) == dn) 430 return spu; 431 return NULL; 432 } 433 434 static struct spu * 435 neighbour_spu(int cbe, struct device_node *target, struct device_node *avoid) 436 { 437 struct spu *spu; 438 struct device_node *spu_dn; 439 const phandle *vic_handles; 440 int lenp, i; 441 442 list_for_each_entry(spu, &cbe_spu_info[cbe].spus, cbe_list) { 443 spu_dn = spu_devnode(spu); 444 if (spu_dn == avoid) 445 continue; 446 vic_handles = of_get_property(spu_dn, "vicinity", &lenp); 447 for (i=0; i < (lenp / sizeof(phandle)); i++) { 448 if (vic_handles[i] == target->phandle) 449 return spu; 450 } 451 } 452 return NULL; 453 } 454 455 static void init_affinity_node(int cbe) 456 { 457 struct spu *spu, *last_spu; 458 struct device_node *vic_dn, *last_spu_dn; 459 phandle avoid_ph; 460 const phandle *vic_handles; 461 int lenp, i, added; 462 463 last_spu = list_first_entry(&cbe_spu_info[cbe].spus, struct spu, 464 cbe_list); 465 avoid_ph = 0; 466 for (added = 1; added < cbe_spu_info[cbe].n_spus; added++) { 467 last_spu_dn = spu_devnode(last_spu); 468 vic_handles = of_get_property(last_spu_dn, "vicinity", &lenp); 469 470 /* 471 * Walk through each phandle in vicinity property of the spu 472 * (tipically two vicinity phandles per spe node) 473 */ 474 for (i = 0; i < (lenp / sizeof(phandle)); i++) { 475 if (vic_handles[i] == avoid_ph) 476 continue; 477 478 vic_dn = of_find_node_by_phandle(vic_handles[i]); 479 if (!vic_dn) 480 continue; 481 482 if (of_node_name_eq(vic_dn, "spe") ) { 483 spu = devnode_spu(cbe, vic_dn); 484 avoid_ph = last_spu_dn->phandle; 485 } else { 486 /* 487 * "mic-tm" and "bif0" nodes do not have 488 * vicinity property. So we need to find the 489 * spe which has vic_dn as neighbour, but 490 * skipping the one we came from (last_spu_dn) 491 */ 492 spu = neighbour_spu(cbe, vic_dn, last_spu_dn); 493 if (!spu) 494 continue; 495 if (of_node_name_eq(vic_dn, "mic-tm")) { 496 last_spu->has_mem_affinity = 1; 497 spu->has_mem_affinity = 1; 498 } 499 avoid_ph = vic_dn->phandle; 500 } 501 502 list_add_tail(&spu->aff_list, &last_spu->aff_list); 503 last_spu = spu; 504 break; 505 } 506 } 507 } 508 509 static void init_affinity_fw(void) 510 { 511 int cbe; 512 513 for (cbe = 0; cbe < MAX_NUMNODES; cbe++) 514 init_affinity_node(cbe); 515 } 516 517 static int __init init_affinity(void) 518 { 519 if (of_has_vicinity()) { 520 init_affinity_fw(); 521 } else { 522 if (of_machine_is_compatible("IBM,CPBW-1.0")) 523 init_affinity_qs20_harcoded(); 524 else 525 printk("No affinity configuration found\n"); 526 } 527 528 return 0; 529 } 530 531 const struct spu_management_ops spu_management_of_ops = { 532 .enumerate_spus = of_enumerate_spus, 533 .create_spu = of_create_spu, 534 .destroy_spu = of_destroy_spu, 535 .enable_spu = enable_spu_by_master_run, 536 .disable_spu = disable_spu_by_master_run, 537 .init_affinity = init_affinity, 538 }; 539