1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * PS3 Platform spu routines. 4 * 5 * Copyright (C) 2006 Sony Computer Entertainment Inc. 6 * Copyright 2006 Sony Corp. 7 */ 8 9 #include <linux/kernel.h> 10 #include <linux/init.h> 11 #include <linux/slab.h> 12 #include <linux/mmzone.h> 13 #include <linux/export.h> 14 #include <linux/io.h> 15 #include <linux/mm.h> 16 #include <linux/processor.h> 17 18 #include <asm/spu.h> 19 #include <asm/spu_priv1.h> 20 #include <asm/lv1call.h> 21 #include <asm/ps3.h> 22 23 #include "../cell/spufs/spufs.h" 24 #include "platform.h" 25 26 /* spu_management_ops */ 27 28 /** 29 * enum spe_type - Type of spe to create. 30 * @SPE_TYPE_LOGICAL: Standard logical spe. 31 * 32 * For use with lv1_construct_logical_spe(). The current HV does not support 33 * any types other than those listed. 34 */ 35 36 enum spe_type { 37 SPE_TYPE_LOGICAL = 0, 38 }; 39 40 /** 41 * struct spe_shadow - logical spe shadow register area. 42 * 43 * Read-only shadow of spe registers. 44 */ 45 46 struct spe_shadow { 47 u8 padding_0140[0x0140]; 48 u64 int_status_class0_RW; /* 0x0140 */ 49 u64 int_status_class1_RW; /* 0x0148 */ 50 u64 int_status_class2_RW; /* 0x0150 */ 51 u8 padding_0158[0x0610-0x0158]; 52 u64 mfc_dsisr_RW; /* 0x0610 */ 53 u8 padding_0618[0x0620-0x0618]; 54 u64 mfc_dar_RW; /* 0x0620 */ 55 u8 padding_0628[0x0800-0x0628]; 56 u64 mfc_dsipr_R; /* 0x0800 */ 57 u8 padding_0808[0x0810-0x0808]; 58 u64 mfc_lscrr_R; /* 0x0810 */ 59 u8 padding_0818[0x0c00-0x0818]; 60 u64 mfc_cer_R; /* 0x0c00 */ 61 u8 padding_0c08[0x0f00-0x0c08]; 62 u64 spe_execution_status; /* 0x0f00 */ 63 u8 padding_0f08[0x1000-0x0f08]; 64 }; 65 66 /** 67 * enum spe_ex_state - Logical spe execution state. 68 * @SPE_EX_STATE_UNEXECUTABLE: Uninitialized. 69 * @SPE_EX_STATE_EXECUTABLE: Enabled, not ready. 70 * @SPE_EX_STATE_EXECUTED: Ready for use. 71 * 72 * The execution state (status) of the logical spe as reported in 73 * struct spe_shadow:spe_execution_status. 74 */ 75 76 enum spe_ex_state { 77 SPE_EX_STATE_UNEXECUTABLE = 0, 78 SPE_EX_STATE_EXECUTABLE = 2, 79 SPE_EX_STATE_EXECUTED = 3, 80 }; 81 82 /** 83 * struct priv1_cache - Cached values of priv1 registers. 84 * @masks[]: Array of cached spe interrupt masks, indexed by class. 85 * @sr1: Cached mfc_sr1 register. 86 * @tclass_id: Cached mfc_tclass_id register. 87 */ 88 89 struct priv1_cache { 90 u64 masks[3]; 91 u64 sr1; 92 u64 tclass_id; 93 }; 94 95 /** 96 * struct spu_pdata - Platform state variables. 97 * @spe_id: HV spe id returned by lv1_construct_logical_spe(). 98 * @resource_id: HV spe resource id returned by 99 * ps3_repository_read_spe_resource_id(). 100 * @priv2_addr: lpar address of spe priv2 area returned by 101 * lv1_construct_logical_spe(). 102 * @shadow_addr: lpar address of spe register shadow area returned by 103 * lv1_construct_logical_spe(). 104 * @shadow: Virtual (ioremap) address of spe register shadow area. 105 * @cache: Cached values of priv1 registers. 106 */ 107 108 struct spu_pdata { 109 u64 spe_id; 110 u64 resource_id; 111 u64 priv2_addr; 112 u64 shadow_addr; 113 struct spe_shadow __iomem *shadow; 114 struct priv1_cache cache; 115 }; 116 117 static struct spu_pdata *spu_pdata(struct spu *spu) 118 { 119 return spu->pdata; 120 } 121 122 #define dump_areas(_a, _b, _c, _d, _e) \ 123 _dump_areas(_a, _b, _c, _d, _e, __func__, __LINE__) 124 static void _dump_areas(unsigned int spe_id, unsigned long priv2, 125 unsigned long problem, unsigned long ls, unsigned long shadow, 126 const char* func, int line) 127 { 128 pr_debug("%s:%d: spe_id: %xh (%u)\n", func, line, spe_id, spe_id); 129 pr_debug("%s:%d: priv2: %lxh\n", func, line, priv2); 130 pr_debug("%s:%d: problem: %lxh\n", func, line, problem); 131 pr_debug("%s:%d: ls: %lxh\n", func, line, ls); 132 pr_debug("%s:%d: shadow: %lxh\n", func, line, shadow); 133 } 134 135 u64 ps3_get_spe_id(void *arg) 136 { 137 return spu_pdata(arg)->spe_id; 138 } 139 EXPORT_SYMBOL_GPL(ps3_get_spe_id); 140 141 static unsigned long __init get_vas_id(void) 142 { 143 u64 id; 144 145 lv1_get_logical_ppe_id(&id); 146 lv1_get_virtual_address_space_id_of_ppe(&id); 147 148 return id; 149 } 150 151 static int __init construct_spu(struct spu *spu) 152 { 153 int result; 154 u64 unused; 155 u64 problem_phys; 156 u64 local_store_phys; 157 158 result = lv1_construct_logical_spe(PAGE_SHIFT, PAGE_SHIFT, PAGE_SHIFT, 159 PAGE_SHIFT, PAGE_SHIFT, get_vas_id(), SPE_TYPE_LOGICAL, 160 &spu_pdata(spu)->priv2_addr, &problem_phys, 161 &local_store_phys, &unused, 162 &spu_pdata(spu)->shadow_addr, 163 &spu_pdata(spu)->spe_id); 164 spu->problem_phys = problem_phys; 165 spu->local_store_phys = local_store_phys; 166 167 if (result) { 168 pr_debug("%s:%d: lv1_construct_logical_spe failed: %s\n", 169 __func__, __LINE__, ps3_result(result)); 170 return result; 171 } 172 173 return result; 174 } 175 176 static void spu_unmap(struct spu *spu) 177 { 178 iounmap(spu->priv2); 179 iounmap(spu->problem); 180 iounmap((__force u8 __iomem *)spu->local_store); 181 iounmap(spu_pdata(spu)->shadow); 182 } 183 184 /** 185 * setup_areas - Map the spu regions into the address space. 186 * 187 * The current HV requires the spu shadow regs to be mapped with the 188 * PTE page protection bits set as read-only. 189 * 190 * Returns: %0 on success or -errno on error. 191 */ 192 193 static int __init setup_areas(struct spu *spu) 194 { 195 spu_pdata(spu)->shadow = ioremap_prot(spu_pdata(spu)->shadow_addr, 196 sizeof(struct spe_shadow), 197 pgprot_noncached_wc(PAGE_KERNEL_RO)); 198 if (!spu_pdata(spu)->shadow) { 199 pr_debug("%s:%d: ioremap shadow failed\n", __func__, __LINE__); 200 goto fail_ioremap; 201 } 202 203 spu->local_store = (__force void *)ioremap_wc(spu->local_store_phys, LS_SIZE); 204 205 if (!spu->local_store) { 206 pr_debug("%s:%d: ioremap local_store failed\n", 207 __func__, __LINE__); 208 goto fail_ioremap; 209 } 210 211 spu->problem = ioremap(spu->problem_phys, 212 sizeof(struct spu_problem)); 213 214 if (!spu->problem) { 215 pr_debug("%s:%d: ioremap problem failed\n", __func__, __LINE__); 216 goto fail_ioremap; 217 } 218 219 spu->priv2 = ioremap(spu_pdata(spu)->priv2_addr, 220 sizeof(struct spu_priv2)); 221 222 if (!spu->priv2) { 223 pr_debug("%s:%d: ioremap priv2 failed\n", __func__, __LINE__); 224 goto fail_ioremap; 225 } 226 227 dump_areas(spu_pdata(spu)->spe_id, spu_pdata(spu)->priv2_addr, 228 spu->problem_phys, spu->local_store_phys, 229 spu_pdata(spu)->shadow_addr); 230 dump_areas(spu_pdata(spu)->spe_id, (unsigned long)spu->priv2, 231 (unsigned long)spu->problem, (unsigned long)spu->local_store, 232 (unsigned long)spu_pdata(spu)->shadow); 233 234 return 0; 235 236 fail_ioremap: 237 spu_unmap(spu); 238 239 return -ENOMEM; 240 } 241 242 static int __init setup_interrupts(struct spu *spu) 243 { 244 int result; 245 246 result = ps3_spe_irq_setup(PS3_BINDING_CPU_ANY, spu_pdata(spu)->spe_id, 247 0, &spu->irqs[0]); 248 249 if (result) 250 goto fail_alloc_0; 251 252 result = ps3_spe_irq_setup(PS3_BINDING_CPU_ANY, spu_pdata(spu)->spe_id, 253 1, &spu->irqs[1]); 254 255 if (result) 256 goto fail_alloc_1; 257 258 result = ps3_spe_irq_setup(PS3_BINDING_CPU_ANY, spu_pdata(spu)->spe_id, 259 2, &spu->irqs[2]); 260 261 if (result) 262 goto fail_alloc_2; 263 264 return result; 265 266 fail_alloc_2: 267 ps3_spe_irq_destroy(spu->irqs[1]); 268 fail_alloc_1: 269 ps3_spe_irq_destroy(spu->irqs[0]); 270 fail_alloc_0: 271 spu->irqs[0] = spu->irqs[1] = spu->irqs[2] = 0; 272 return result; 273 } 274 275 static int __init enable_spu(struct spu *spu) 276 { 277 int result; 278 279 result = lv1_enable_logical_spe(spu_pdata(spu)->spe_id, 280 spu_pdata(spu)->resource_id); 281 282 if (result) { 283 pr_debug("%s:%d: lv1_enable_logical_spe failed: %s\n", 284 __func__, __LINE__, ps3_result(result)); 285 goto fail_enable; 286 } 287 288 result = setup_areas(spu); 289 290 if (result) 291 goto fail_areas; 292 293 result = setup_interrupts(spu); 294 295 if (result) 296 goto fail_interrupts; 297 298 return 0; 299 300 fail_interrupts: 301 spu_unmap(spu); 302 fail_areas: 303 lv1_disable_logical_spe(spu_pdata(spu)->spe_id, 0); 304 fail_enable: 305 return result; 306 } 307 308 static int ps3_destroy_spu(struct spu *spu) 309 { 310 int result; 311 312 pr_debug("%s:%d spu_%d\n", __func__, __LINE__, spu->number); 313 314 result = lv1_disable_logical_spe(spu_pdata(spu)->spe_id, 0); 315 BUG_ON(result); 316 317 ps3_spe_irq_destroy(spu->irqs[2]); 318 ps3_spe_irq_destroy(spu->irqs[1]); 319 ps3_spe_irq_destroy(spu->irqs[0]); 320 321 spu->irqs[0] = spu->irqs[1] = spu->irqs[2] = 0; 322 323 spu_unmap(spu); 324 325 result = lv1_destruct_logical_spe(spu_pdata(spu)->spe_id); 326 BUG_ON(result); 327 328 kfree(spu->pdata); 329 spu->pdata = NULL; 330 331 return 0; 332 } 333 334 static int __init ps3_create_spu(struct spu *spu, void *data) 335 { 336 int result; 337 338 pr_debug("%s:%d spu_%d\n", __func__, __LINE__, spu->number); 339 340 spu->pdata = kzalloc_obj(struct spu_pdata); 341 342 if (!spu->pdata) { 343 result = -ENOMEM; 344 goto fail_malloc; 345 } 346 347 spu_pdata(spu)->resource_id = (unsigned long)data; 348 349 /* Init cached reg values to HV defaults. */ 350 351 spu_pdata(spu)->cache.sr1 = 0x33; 352 353 result = construct_spu(spu); 354 355 if (result) 356 goto fail_construct; 357 358 /* For now, just go ahead and enable it. */ 359 360 result = enable_spu(spu); 361 362 if (result) 363 goto fail_enable; 364 365 while (in_be64(&spu_pdata(spu)->shadow->spe_execution_status) != 366 SPE_EX_STATE_EXECUTED) 367 cpu_relax(); 368 369 return result; 370 371 fail_enable: 372 fail_construct: 373 ps3_destroy_spu(spu); 374 fail_malloc: 375 return result; 376 } 377 378 static int __init ps3_enumerate_spus(int (*fn)(void *data)) 379 { 380 int result; 381 unsigned int num_resource_id; 382 unsigned int i; 383 384 result = ps3_repository_read_num_spu_resource_id(&num_resource_id); 385 386 pr_debug("%s:%d: num_resource_id %u\n", __func__, __LINE__, 387 num_resource_id); 388 389 /* 390 * For now, just create logical spus equal to the number 391 * of physical spus reserved for the partition. 392 */ 393 394 for (i = 0; i < num_resource_id; i++) { 395 enum ps3_spu_resource_type resource_type; 396 unsigned int resource_id; 397 398 result = ps3_repository_read_spu_resource_id(i, 399 &resource_type, &resource_id); 400 401 if (result) 402 break; 403 404 if (resource_type == PS3_SPU_RESOURCE_TYPE_EXCLUSIVE) { 405 result = fn((void*)(unsigned long)resource_id); 406 407 if (result) 408 break; 409 } 410 } 411 412 if (result) { 413 printk(KERN_WARNING "%s:%d: Error initializing spus\n", 414 __func__, __LINE__); 415 return result; 416 } 417 418 return num_resource_id; 419 } 420 421 static int ps3_init_affinity(void) 422 { 423 return 0; 424 } 425 426 /** 427 * ps3_enable_spu - Enable SPU run control. 428 * 429 * An outstanding enhancement for the PS3 would be to add a guard to check 430 * for incorrect access to the spu problem state when the spu context is 431 * disabled. This check could be implemented with a flag added to the spu 432 * context that would inhibit mapping problem state pages, and a routine 433 * to unmap spu problem state pages. When the spu is enabled with 434 * ps3_enable_spu() the flag would be set allowing pages to be mapped, 435 * and when the spu is disabled with ps3_disable_spu() the flag would be 436 * cleared and the mapped problem state pages would be unmapped. 437 */ 438 439 static void ps3_enable_spu(struct spu_context *ctx) 440 { 441 } 442 443 static void ps3_disable_spu(struct spu_context *ctx) 444 { 445 ctx->ops->runcntl_stop(ctx); 446 } 447 448 static const struct spu_management_ops spu_management_ps3_ops = { 449 .enumerate_spus = ps3_enumerate_spus, 450 .create_spu = ps3_create_spu, 451 .destroy_spu = ps3_destroy_spu, 452 .enable_spu = ps3_enable_spu, 453 .disable_spu = ps3_disable_spu, 454 .init_affinity = ps3_init_affinity, 455 }; 456 457 /* spu_priv1_ops */ 458 459 static void int_mask_and(struct spu *spu, int class, u64 mask) 460 { 461 u64 old_mask; 462 463 /* are these serialized by caller??? */ 464 old_mask = spu_int_mask_get(spu, class); 465 spu_int_mask_set(spu, class, old_mask & mask); 466 } 467 468 static void int_mask_or(struct spu *spu, int class, u64 mask) 469 { 470 u64 old_mask; 471 472 old_mask = spu_int_mask_get(spu, class); 473 spu_int_mask_set(spu, class, old_mask | mask); 474 } 475 476 static void int_mask_set(struct spu *spu, int class, u64 mask) 477 { 478 spu_pdata(spu)->cache.masks[class] = mask; 479 lv1_set_spe_interrupt_mask(spu_pdata(spu)->spe_id, class, 480 spu_pdata(spu)->cache.masks[class]); 481 } 482 483 static u64 int_mask_get(struct spu *spu, int class) 484 { 485 return spu_pdata(spu)->cache.masks[class]; 486 } 487 488 static void int_stat_clear(struct spu *spu, int class, u64 stat) 489 { 490 /* Note that MFC_DSISR will be cleared when class1[MF] is set. */ 491 492 lv1_clear_spe_interrupt_status(spu_pdata(spu)->spe_id, class, 493 stat, 0); 494 } 495 496 static u64 int_stat_get(struct spu *spu, int class) 497 { 498 u64 stat; 499 500 lv1_get_spe_interrupt_status(spu_pdata(spu)->spe_id, class, &stat); 501 return stat; 502 } 503 504 static void cpu_affinity_set(struct spu *spu, int cpu) 505 { 506 /* No support. */ 507 } 508 509 static u64 mfc_dar_get(struct spu *spu) 510 { 511 return in_be64(&spu_pdata(spu)->shadow->mfc_dar_RW); 512 } 513 514 static void mfc_dsisr_set(struct spu *spu, u64 dsisr) 515 { 516 /* Nothing to do, cleared in int_stat_clear(). */ 517 } 518 519 static u64 mfc_dsisr_get(struct spu *spu) 520 { 521 return in_be64(&spu_pdata(spu)->shadow->mfc_dsisr_RW); 522 } 523 524 static void mfc_sdr_setup(struct spu *spu) 525 { 526 /* Nothing to do. */ 527 } 528 529 static void mfc_sr1_set(struct spu *spu, u64 sr1) 530 { 531 /* Check bits allowed by HV. */ 532 533 static const u64 allowed = ~(MFC_STATE1_LOCAL_STORAGE_DECODE_MASK 534 | MFC_STATE1_PROBLEM_STATE_MASK); 535 536 BUG_ON((sr1 & allowed) != (spu_pdata(spu)->cache.sr1 & allowed)); 537 538 spu_pdata(spu)->cache.sr1 = sr1; 539 lv1_set_spe_privilege_state_area_1_register( 540 spu_pdata(spu)->spe_id, 541 offsetof(struct spu_priv1, mfc_sr1_RW), 542 spu_pdata(spu)->cache.sr1); 543 } 544 545 static u64 mfc_sr1_get(struct spu *spu) 546 { 547 return spu_pdata(spu)->cache.sr1; 548 } 549 550 static void mfc_tclass_id_set(struct spu *spu, u64 tclass_id) 551 { 552 spu_pdata(spu)->cache.tclass_id = tclass_id; 553 lv1_set_spe_privilege_state_area_1_register( 554 spu_pdata(spu)->spe_id, 555 offsetof(struct spu_priv1, mfc_tclass_id_RW), 556 spu_pdata(spu)->cache.tclass_id); 557 } 558 559 static u64 mfc_tclass_id_get(struct spu *spu) 560 { 561 return spu_pdata(spu)->cache.tclass_id; 562 } 563 564 static void tlb_invalidate(struct spu *spu) 565 { 566 /* Nothing to do. */ 567 } 568 569 static void resource_allocation_groupID_set(struct spu *spu, u64 id) 570 { 571 /* No support. */ 572 } 573 574 static u64 resource_allocation_groupID_get(struct spu *spu) 575 { 576 return 0; /* No support. */ 577 } 578 579 static void resource_allocation_enable_set(struct spu *spu, u64 enable) 580 { 581 /* No support. */ 582 } 583 584 static u64 resource_allocation_enable_get(struct spu *spu) 585 { 586 return 0; /* No support. */ 587 } 588 589 static const struct spu_priv1_ops spu_priv1_ps3_ops = { 590 .int_mask_and = int_mask_and, 591 .int_mask_or = int_mask_or, 592 .int_mask_set = int_mask_set, 593 .int_mask_get = int_mask_get, 594 .int_stat_clear = int_stat_clear, 595 .int_stat_get = int_stat_get, 596 .cpu_affinity_set = cpu_affinity_set, 597 .mfc_dar_get = mfc_dar_get, 598 .mfc_dsisr_set = mfc_dsisr_set, 599 .mfc_dsisr_get = mfc_dsisr_get, 600 .mfc_sdr_setup = mfc_sdr_setup, 601 .mfc_sr1_set = mfc_sr1_set, 602 .mfc_sr1_get = mfc_sr1_get, 603 .mfc_tclass_id_set = mfc_tclass_id_set, 604 .mfc_tclass_id_get = mfc_tclass_id_get, 605 .tlb_invalidate = tlb_invalidate, 606 .resource_allocation_groupID_set = resource_allocation_groupID_set, 607 .resource_allocation_groupID_get = resource_allocation_groupID_get, 608 .resource_allocation_enable_set = resource_allocation_enable_set, 609 .resource_allocation_enable_get = resource_allocation_enable_get, 610 }; 611 612 void ps3_spu_set_platform(void) 613 { 614 spu_priv1_ops = &spu_priv1_ps3_ops; 615 spu_management_ops = &spu_management_ps3_ops; 616 } 617