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