2 * This file is subject to the terms and conditions of the GNU General Public
3 * License. See the file "COPYING" in the main directory of this archive
6 * Copyright (C) 2004, 2005 MIPS Technologies, Inc. All rights reserved.
7 * Copyright (C) 2013 Imagination Technologies Ltd.
9 #include <linux/kernel.h>
10 #include <linux/device.h>
12 #include <linux/slab.h>
13 #include <linux/export.h>
15 #include <asm/mipsregs.h>
16 #include <asm/mipsmtregs.h>
17 #include <asm/mips_mt.h>
22 /* The number of TCs and VPEs physically available on the core */
23 static int hw_tcs
, hw_vpes
;
25 /* We are prepared so configure and start the VPE... */
26 int vpe_run(struct vpe
*v
)
28 unsigned long flags
, val
, dmt_flag
;
29 struct vpe_notifications
*notifier
;
30 unsigned int vpeflags
;
33 /* check we are the Master VPE */
34 local_irq_save(flags
);
35 val
= read_c0_vpeconf0();
36 if (!(val
& VPECONF0_MVP
)) {
37 pr_warn("VPE loader: only Master VPE's are able to config MT\n");
38 local_irq_restore(flags
);
46 if (list_empty(&v
->tc
)) {
49 local_irq_restore(flags
);
51 pr_warn("VPE loader: No TC's associated with VPE %d\n",
57 t
= list_first_entry(&v
->tc
, struct tc
, tc
);
59 /* Put MVPE's into 'configuration state' */
60 set_c0_mvpcontrol(MVPCONTROL_VPC
);
64 /* should check it is halted, and not activated */
65 if ((read_tc_c0_tcstatus() & TCSTATUS_A
) ||
66 !(read_tc_c0_tchalt() & TCHALT_H
)) {
69 local_irq_restore(flags
);
71 pr_warn("VPE loader: TC %d is already active!\n",
78 * Write the address we want it to start running from in the TCPC
81 write_tc_c0_tcrestart((unsigned long)v
->__start
);
82 write_tc_c0_tccontext((unsigned long)0);
85 * Mark the TC as activated, not interrupt exempt and not dynamically
88 val
= read_tc_c0_tcstatus();
89 val
= (val
& ~(TCSTATUS_DA
| TCSTATUS_IXMT
)) | TCSTATUS_A
;
90 write_tc_c0_tcstatus(val
);
92 write_tc_c0_tchalt(read_tc_c0_tchalt() & ~TCHALT_H
);
95 * We don't pass the memsize here, so VPE programs need to be
96 * compiled with DFLT_STACK_SIZE and DFLT_HEAP_SIZE defined.
103 * bind the TC to VPE 1 as late as possible so we only have the final
104 * VPE registers to set up, and so an EJTAG probe can trigger on it
106 write_tc_c0_tcbind((read_tc_c0_tcbind() & ~TCBIND_CURVPE
) | 1);
108 write_vpe_c0_vpeconf0(read_vpe_c0_vpeconf0() & ~(VPECONF0_VPA
));
110 back_to_back_c0_hazard();
112 /* Set up the XTC bit in vpeconf0 to point at our tc */
113 write_vpe_c0_vpeconf0((read_vpe_c0_vpeconf0() & ~(VPECONF0_XTC
))
114 | (t
->index
<< VPECONF0_XTC_SHIFT
));
116 back_to_back_c0_hazard();
118 /* enable this VPE */
119 write_vpe_c0_vpeconf0(read_vpe_c0_vpeconf0() | VPECONF0_VPA
);
121 /* clear out any left overs from a previous program */
122 write_vpe_c0_status(0);
123 write_vpe_c0_cause(0);
125 /* take system out of configuration state */
126 clear_c0_mvpcontrol(MVPCONTROL_VPC
);
129 * SMVP kernels manage VPE enable independently, but uniprocessor
130 * kernels need to turn it on, even if that wasn't the pre-dvpe() state.
138 local_irq_restore(flags
);
140 list_for_each_entry(notifier
, &v
->notify
, list
)
141 notifier
->start(VPE_MODULE_MINOR
);
146 void cleanup_tc(struct tc
*tc
)
149 unsigned int mtflags
, vpflags
;
152 local_irq_save(flags
);
155 /* Put MVPE's into 'configuration state' */
156 set_c0_mvpcontrol(MVPCONTROL_VPC
);
159 tmp
= read_tc_c0_tcstatus();
161 /* mark not allocated and not dynamically allocatable */
162 tmp
&= ~(TCSTATUS_A
| TCSTATUS_DA
);
163 tmp
|= TCSTATUS_IXMT
; /* interrupt exempt */
164 write_tc_c0_tcstatus(tmp
);
166 write_tc_c0_tchalt(TCHALT_H
);
169 clear_c0_mvpcontrol(MVPCONTROL_VPC
);
172 local_irq_restore(flags
);
175 /* module wrapper entry points */
177 void *vpe_alloc(void)
183 for (i
= 1; i
< MAX_VPES
; i
++) {
186 v
->state
= VPE_STATE_INUSE
;
192 EXPORT_SYMBOL(vpe_alloc
);
194 /* start running from here */
195 int vpe_start(void *vpe
, unsigned long start
)
202 EXPORT_SYMBOL(vpe_start
);
204 /* halt it for now */
205 int vpe_stop(void *vpe
)
209 unsigned int evpe_flags
;
213 t
= list_entry(v
->tc
.next
, struct tc
, tc
);
216 write_vpe_c0_vpeconf0(read_vpe_c0_vpeconf0() & ~VPECONF0_VPA
);
223 EXPORT_SYMBOL(vpe_stop
);
225 /* I've done with it thank you */
226 int vpe_free(void *vpe
)
230 unsigned int evpe_flags
;
232 t
= list_entry(v
->tc
.next
, struct tc
, tc
);
238 /* Put MVPE's into 'configuration state' */
239 set_c0_mvpcontrol(MVPCONTROL_VPC
);
242 write_vpe_c0_vpeconf0(read_vpe_c0_vpeconf0() & ~VPECONF0_VPA
);
245 write_tc_c0_tchalt(TCHALT_H
);
248 /* mark the TC unallocated */
249 write_tc_c0_tcstatus(read_tc_c0_tcstatus() & ~TCSTATUS_A
);
251 v
->state
= VPE_STATE_UNUSED
;
253 clear_c0_mvpcontrol(MVPCONTROL_VPC
);
258 EXPORT_SYMBOL(vpe_free
);
260 static ssize_t
store_kill(struct device
*dev
, struct device_attribute
*attr
,
261 const char *buf
, size_t len
)
263 struct vpe
*vpe
= get_vpe(aprp_cpu_index());
264 struct vpe_notifications
*notifier
;
266 list_for_each_entry(notifier
, &vpe
->notify
, list
)
267 notifier
->stop(aprp_cpu_index());
269 release_progmem(vpe
->load_addr
);
270 cleanup_tc(get_tc(aprp_cpu_index()));
276 static DEVICE_ATTR(kill
, S_IWUSR
, NULL
, store_kill
);
278 static ssize_t
ntcs_show(struct device
*cd
, struct device_attribute
*attr
,
281 struct vpe
*vpe
= get_vpe(aprp_cpu_index());
283 return sprintf(buf
, "%d\n", vpe
->ntcs
);
286 static ssize_t
ntcs_store(struct device
*dev
, struct device_attribute
*attr
,
287 const char *buf
, size_t len
)
289 struct vpe
*vpe
= get_vpe(aprp_cpu_index());
293 ret
= kstrtoul(buf
, 0, &new);
297 if (new == 0 || new > (hw_tcs
- aprp_cpu_index()))
304 static DEVICE_ATTR_RW(ntcs
);
306 static struct attribute
*vpe_attrs
[] = {
311 ATTRIBUTE_GROUPS(vpe
);
313 static void vpe_device_release(struct device
*cd
)
317 static struct class vpe_class
= {
319 .dev_release
= vpe_device_release
,
320 .dev_groups
= vpe_groups
,
323 static struct device vpe_device
;
325 int __init
vpe_module_init(void)
327 unsigned int mtflags
, vpflags
;
328 unsigned long flags
, val
;
329 struct vpe
*v
= NULL
;
333 if (!cpu_has_mipsmt
) {
334 pr_warn("VPE loader: not a MIPS MT capable processor\n");
339 pr_warn("No VPEs reserved for AP/SP, not initialize VPE loader\n"
340 "Pass maxvpes=<n> argument as kernel argument\n");
345 if (aprp_cpu_index() == 0) {
346 pr_warn("No TCs reserved for AP/SP, not initialize VPE loader\n"
347 "Pass maxtcs=<n> argument as kernel argument\n");
352 major
= register_chrdev(0, VPE_MODULE_NAME
, &vpe_fops
);
354 pr_warn("VPE loader: unable to register character device\n");
358 err
= class_register(&vpe_class
);
360 pr_err("vpe_class registration failed\n");
364 device_initialize(&vpe_device
);
365 vpe_device
.class = &vpe_class
;
366 vpe_device
.parent
= NULL
;
367 dev_set_name(&vpe_device
, "vpe1");
368 vpe_device
.devt
= MKDEV(major
, VPE_MODULE_MINOR
);
369 err
= device_add(&vpe_device
);
371 pr_err("Adding vpe_device failed\n");
375 local_irq_save(flags
);
379 /* Put MVPE's into 'configuration state' */
380 set_c0_mvpcontrol(MVPCONTROL_VPC
);
382 val
= read_c0_mvpconf0();
383 hw_tcs
= (val
& MVPCONF0_PTC
) + 1;
384 hw_vpes
= ((val
& MVPCONF0_PVPE
) >> MVPCONF0_PVPE_SHIFT
) + 1;
386 for (tc
= aprp_cpu_index(); tc
< hw_tcs
; tc
++) {
388 * Must re-enable multithreading temporarily or in case we
389 * reschedule send IPIs or similar we might hang.
391 clear_c0_mvpcontrol(MVPCONTROL_VPC
);
394 local_irq_restore(flags
);
401 local_irq_save(flags
);
404 set_c0_mvpcontrol(MVPCONTROL_VPC
);
412 pr_warn("VPE: unable to allocate VPE\n");
416 v
->ntcs
= hw_tcs
- aprp_cpu_index();
418 /* add the tc to the list of this vpe's tc's. */
419 list_add(&t
->tc
, &v
->tc
);
421 /* deactivate all but vpe0 */
422 if (tc
>= aprp_cpu_index()) {
423 unsigned long tmp
= read_vpe_c0_vpeconf0();
425 tmp
&= ~VPECONF0_VPA
;
429 write_vpe_c0_vpeconf0(tmp
);
432 /* disable multi-threading with TC's */
433 write_vpe_c0_vpecontrol(read_vpe_c0_vpecontrol() &
436 if (tc
>= vpelimit
) {
438 * Set config to be the same as vpe0,
439 * particularly kseg0 coherency alg
441 write_vpe_c0_config(read_c0_config());
446 t
->pvpe
= v
; /* set the parent vpe */
448 if (tc
>= aprp_cpu_index()) {
454 * A TC that is bound to any other VPE gets bound to
455 * VPE0, ideally I'd like to make it homeless but it
456 * doesn't appear to let me bind a TC to a non-existent
457 * VPE. Which is perfectly reasonable.
459 * The (un)bound state is visible to an EJTAG probe so
462 tmp
= read_tc_c0_tcbind();
463 if (tmp
& TCBIND_CURVPE
) {
464 /* tc is bound >vpe0 */
465 write_tc_c0_tcbind(tmp
& ~TCBIND_CURVPE
);
467 t
->pvpe
= get_vpe(0); /* set the parent vpe */
471 write_tc_c0_tchalt(TCHALT_H
);
474 tmp
= read_tc_c0_tcstatus();
476 /* mark not activated and not dynamically allocatable */
477 tmp
&= ~(TCSTATUS_A
| TCSTATUS_DA
);
478 tmp
|= TCSTATUS_IXMT
; /* interrupt exempt */
479 write_tc_c0_tcstatus(tmp
);
484 /* release config state */
485 clear_c0_mvpcontrol(MVPCONTROL_VPC
);
489 local_irq_restore(flags
);
494 device_del(&vpe_device
);
497 put_device(&vpe_device
);
498 class_unregister(&vpe_class
);
501 unregister_chrdev(major
, VPE_MODULE_NAME
);
506 void __exit
vpe_module_exit(void)
510 device_unregister(&vpe_device
);
511 class_unregister(&vpe_class
);
512 unregister_chrdev(major
, VPE_MODULE_NAME
);
514 /* No locking needed here */
515 list_for_each_entry_safe(v
, n
, &vpecontrol
.vpe_list
, list
) {
516 if (v
->state
!= VPE_STATE_UNUSED
)