few bugfixes, some features, starting 2.4 port
[warm.git] / warm.c
CommitLineData
198a1649 1/*
2 * wARM - exporting ARM processor specific privileged services to userspace
3 * userspace part
4 *
5 * Copyright (c) GraÅžvydas "notaz" Ignotas, 2009
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * * Neither the name of the organization nor the
15 * names of its contributors may be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30#include <stdio.h>
31#include <stdlib.h>
32#include <string.h>
33#include <sys/types.h>
34#include <sys/stat.h>
35#include <unistd.h>
36#include <fcntl.h>
37#include <sys/ioctl.h>
38#include <sys/utsname.h>
39#include <errno.h>
40
41#define WARM_CODE
42#include "warm.h"
43#include "sys_cacheflush.h"
44
45static int warm_fd = -1;
46
47int warm_init(void)
48{
49 struct utsname unm;
50 char buff[128];
51
52 warm_fd = open("/proc/warm", O_RDWR);
53 if (warm_fd >= 0)
54 return 0;
55
56 memset(&unm, 0, sizeof(unm));
57 uname(&unm);
8d04105a 58 snprintf(buff, sizeof(buff), "/sbin/insmod warm_%s.ko verbose=1", unm.release);
198a1649 59
60 /* try to insmod */
61 system(buff);
62 warm_fd = open("/proc/warm", O_RDWR);
63 if (warm_fd >= 0)
64 return 0;
65
66 fprintf(stderr, "wARM: can't init, acting as sys_cacheflush wrapper\n");
67 return -1;
68}
69
70void warm_finish(void)
71{
72 if (warm_fd >= 0)
73 close(warm_fd);
74 system("rmmod warm");
75}
76
77int warm_cache_op_range(int op, void *addr, unsigned long size)
78{
79 struct warm_cache_op wop;
80 int ret;
81
82 if (warm_fd < 0) {
83 sys_cacheflush(addr, (char *)addr + size);
84 return -1;
85 }
86
87 wop.ops = op;
88 wop.addr = (unsigned long)addr;
89 wop.size = size;
90
91 ret = ioctl(warm_fd, WARMC_CACHE_OP, &wop);
92 if (ret != 0) {
93 perror("WARMC_CACHE_OP failed");
94 return -1;
95 }
96
97 return 0;
98}
99
100int warm_cache_op_all(int op)
101{
102 return warm_cache_op_range(op, NULL, (size_t)-1);
103}
104
105int warm_change_cb_range(int cb, int is_set, void *addr, unsigned long size)
106{
107 struct warm_change_cb ccb;
108 int ret;
109
110 if (warm_fd < 0)
111 return -1;
112
113 ccb.addr = (unsigned long)addr;
114 ccb.size = size;
115 ccb.cb = cb;
116 ccb.is_set = is_set;
117
118 ret = ioctl(warm_fd, WARMC_CHANGE_CB, &ccb);
119 if (ret != 0) {
120 perror("WARMC_CHANGE_CB failed");
121 return -1;
122 }
123
124 return 0;
125}
126
127int warm_change_cb_upper(int cb, int is_set)
128{
129 return warm_change_cb_range(cb, is_set, 0, 0);
130}
131
132unsigned long warm_virt2phys(const void *ptr)
133{
134 unsigned long ptrio;
135 int ret;
136
137 ptrio = (unsigned long)ptr;
138 ret = ioctl(warm_fd, WARMC_VIRT2PHYS, &ptrio);
139 if (ret != 0) {
140 perror("WARMC_VIRT2PHYS failed");
141 return (unsigned long)-1;
142 }
143
144 return ptrio;
145}
146