summaryrefslogtreecommitdiff
path: root/libcpu/arm/armv6/vfp.c
blob: 478c0603d7848b339da3c9b2d573fb9f9c160c45 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
 * File      : vfp.c
 * This file is part of RT-Thread RTOS
 * COPYRIGHT (C) 2006, RT-Thread Develop Team
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Change Logs:
 * Date           Author       Notes
 * 2014-11-07     weety      first version
 */

#include <rthw.h>
#include <rtthread.h>
#include "vfp.h"

#ifdef RT_USING_VFP

rt_uint32_t vfpregs_offset = offsetof(struct rt_thread, vfpregs);

struct vfp_context *last_vfp_context = RT_NULL;

int vfp_switch(rt_uint32_t cmd, struct rt_thread *thread)
{
	rt_uint32_t fpexc;
	//rt_kprintf("%s:%d, %x\n", __func__, cmd, thread);

	switch (cmd) {
		case THREAD_INIT:
		{
			struct vfp_context *vfp = &thread->vfpregs;

			rt_memset(vfp, 0, sizeof(struct vfp_context));
			vfp->fpexc = FPEXC_EN;
			vfp->fpscr = FPSCR_RN;

			if (last_vfp_context == vfp)
				last_vfp_context = RT_NULL;
			vmsr(FPEXC, vmrs(FPEXC) & ~FPEXC_EN);
			break;
		}
		case THREAD_EXIT:
		{
				/* release case: Per-thread VFP cleanup. */
			struct vfp_context *vfp = &thread->vfpregs;

			if (last_vfp_context == vfp)
				last_vfp_context = RT_NULL;
			break;
		}
		default:
			break;
	}

	return 0;
}

int vfp_thread_init(struct rt_thread *thread)
{
	return vfp_switch(THREAD_INIT, thread);
}


int vfp_thread_exit(struct rt_thread *thread)
{
	return vfp_switch(THREAD_EXIT, thread);
}

rt_uint32_t read_vfp_regs(rt_uint32_t *buf)
{
	rt_uint32_t value;
	rt_uint32_t length;
	asm  volatile ("vldmia  %0!, {d0-d15}"
			:
			:"r"(buf)
			:"cc");
	length = 32;
#ifdef RT_USING_VFPv3
	asm  volatile ("vmrs  %0, mvfr0"
			:"=r"(value)
			:);
	if ((value & MVFR0_A_SIMD_MASK) == 2)
	{
		asm  volatile ("vldmia  %0!, {d16-d31}"
				:
				:"r"(buf)
				:"cc");
		length = 64;
	}
	else
	{
		length = 32;
	}
#endif

	return length;
}

void vfp_exception(rt_uint32_t instruction, rt_uint32_t fpexc)
{
	int i = 0;
	int length = 0;
	rt_uint32_t fpscr, fpsid;
#ifdef RT_USING_VFPv3
	unsigned long long vfp_regs[32];
#else
	unsigned long long vfp_regs[16];
#endif

	rt_kprintf("VFP: exception: instruction %08x fpexc %08x\n", instruction, fpexc);
	fpsid = vmrs(FPSID);
	fpscr = vmrs(FPSCR);
	rt_kprintf("VFP: exception: fpsid %08x fpscr %08x\n", fpsid, fpscr);

	length = read_vfp_regs((rt_uint32_t *)vfp_regs);
	rt_kprintf("VFP: exception registers: {s0~s%d}\n", length - 1);
	for(i = 0; i < length; i++)
	{
		if (i && !(i & 0x3))
			rt_kprintf("\n");
		rt_kprintf("0x%08x ", ((rt_uint32_t *)vfp_regs)[i]);
	}
	rt_kprintf("\n");
	
}

void vfp_init(void)
{
	int ret = 0;
	unsigned int value;
	asm  volatile ("mrc p15, 0, %0, c1, c0, 2"
			:"=r"(value)
			:);
	value |= 0xf00000;/*enable CP10, CP11 user access*/
	asm volatile("mcr p15, 0, %0, c1, c0, 2"
			:
			:"r"(value));
#if 0
	asm volatile("fmrx %0, fpexc"
			:"=r"(value));
	value |=(1<<30);
	asm volatile("fmxr fpexc, %0"
			:
			:"r"(value));
#endif
}


#endif