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