blob: c18a1104bb9d61e82d89945a5e07ccf68932c78c [file] [log] [blame]
Carl-Daniel Hailfingerfb0828f2010-02-12 19:35:25 +00001/*
2 * This file is part of the flashrom project.
3 *
4 * Copyright (C) 2009,2010 Carl-Daniel Hailfinger
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21#include <stdint.h>
22#include <string.h>
23#include <stdlib.h>
Carl-Daniel Hailfingerdcef67e2010-06-21 23:20:15 +000024#include <sys/types.h>
Patrick Georgia9095a92010-09-30 17:03:32 +000025#if !defined (__DJGPP__) && !defined(__LIBPAYLOAD__)
Carl-Daniel Hailfinger831e8f42010-05-30 22:24:40 +000026#include <unistd.h>
Carl-Daniel Hailfingerfb0828f2010-02-12 19:35:25 +000027#include <fcntl.h>
Patrick Georgia9095a92010-09-30 17:03:32 +000028#endif
29#if !defined (__DJGPP__)
Carl-Daniel Hailfingerfb0828f2010-02-12 19:35:25 +000030#include <errno.h>
Carl-Daniel Hailfingerdcef67e2010-06-21 23:20:15 +000031#endif
Carl-Daniel Hailfingerfb0828f2010-02-12 19:35:25 +000032#include "flash.h"
Patrick Georgi32508eb2012-07-20 20:35:14 +000033#include "hwaccess.h"
Carl-Daniel Hailfingerfb0828f2010-02-12 19:35:25 +000034
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +000035#if defined(__i386__) || defined(__x86_64__)
36
37/* sync primitive is not needed because x86 uses uncached accesses
38 * which have a strongly ordered memory model.
39 */
40static inline void sync_primitive(void)
41{
42}
43
Carl-Daniel Hailfingera5eecda2012-02-25 22:50:21 +000044#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
Carl-Daniel Hailfingerfb0828f2010-02-12 19:35:25 +000045int io_fd;
46#endif
47
Carl-Daniel Hailfingerd6bb8282012-07-21 17:27:08 +000048int release_io_perms(void *p)
49{
50#if defined(__DJGPP__) || defined(__LIBPAYLOAD__)
51#else
52#if defined (__sun) && (defined(__i386) || defined(__amd64))
53 sysi86(SI86V86, V86SC_IOPL, 0);
54#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined (__DragonFly__)
55 close(io_fd);
56#else
57 iopl(0);
58#endif
59#endif
60 return 0;
61}
62
63/* Get I/O permissions with automatic permission release on shutdown. */
64int rget_io_perms(void)
Carl-Daniel Hailfingerfb0828f2010-02-12 19:35:25 +000065{
Patrick Georgia9095a92010-09-30 17:03:32 +000066#if defined(__DJGPP__) || defined(__LIBPAYLOAD__)
Carl-Daniel Hailfingerdcef67e2010-06-21 23:20:15 +000067 /* We have full permissions by default. */
Carl-Daniel Hailfingerd6bb8282012-07-21 17:27:08 +000068 return 0;
Carl-Daniel Hailfingerdcef67e2010-06-21 23:20:15 +000069#else
Carl-Daniel Hailfingerfb0828f2010-02-12 19:35:25 +000070#if defined (__sun) && (defined(__i386) || defined(__amd64))
71 if (sysi86(SI86V86, V86SC_IOPL, PS_IOPL) != 0) {
Carl-Daniel Hailfingera5eecda2012-02-25 22:50:21 +000072#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined (__DragonFly__)
Carl-Daniel Hailfingerfb0828f2010-02-12 19:35:25 +000073 if ((io_fd = open("/dev/io", O_RDWR)) < 0) {
Rudolf Marek03ae5c12010-03-16 23:59:19 +000074#else
Carl-Daniel Hailfingerfb0828f2010-02-12 19:35:25 +000075 if (iopl(3) != 0) {
76#endif
Sean Nelson316a29f2010-05-07 20:09:04 +000077 msg_perr("ERROR: Could not get I/O privileges (%s).\n"
Carl-Daniel Hailfingerfb0828f2010-02-12 19:35:25 +000078 "You need to be root.\n", strerror(errno));
Carl-Daniel Hailfingerb63b0672010-07-02 17:12:50 +000079#if defined (__OpenBSD__)
80 msg_perr("Please set securelevel=-1 in /etc/rc.securelevel "
81 "and reboot, or reboot into \n");
82 msg_perr("single user mode.\n");
83#endif
Carl-Daniel Hailfingerd6bb8282012-07-21 17:27:08 +000084 return 1;
85 } else {
86 register_shutdown(release_io_perms, NULL);
Carl-Daniel Hailfingerfb0828f2010-02-12 19:35:25 +000087 }
Carl-Daniel Hailfingerd6bb8282012-07-21 17:27:08 +000088 return 0;
Carl-Daniel Hailfingerfb0828f2010-02-12 19:35:25 +000089#endif
90}
91
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +000092#elif defined(__powerpc__) || defined(__powerpc64__) || defined(__ppc__) || defined(__ppc64__)
93
94static inline void sync_primitive(void)
95{
96 /* Prevent reordering and/or merging of reads/writes to hardware.
97 * Such reordering and/or merging would break device accesses which
98 * depend on the exact access order.
99 */
100 asm("eieio" : : : "memory");
101}
102
103/* PCI port I/O is not yet implemented on PowerPC. */
Carl-Daniel Hailfingerd6bb8282012-07-21 17:27:08 +0000104int rget_io_perms(void)
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000105{
Carl-Daniel Hailfingerd6bb8282012-07-21 17:27:08 +0000106 return 0;
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000107}
108
109#elif defined (__mips) || defined (__mips__) || defined (_mips) || defined (mips)
110
111/* sync primitive is not needed because /dev/mem on MIPS uses uncached accesses
112 * in mode 2 which has a strongly ordered memory model.
113 */
114static inline void sync_primitive(void)
115{
116}
117
118/* PCI port I/O is not yet implemented on MIPS. */
Carl-Daniel Hailfingerd6bb8282012-07-21 17:27:08 +0000119int rget_io_perms(void)
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000120{
Carl-Daniel Hailfingerd6bb8282012-07-21 17:27:08 +0000121 return 0;
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000122}
123
David Hendricksb286da72012-02-13 00:35:35 +0000124#elif defined (__arm__)
125
126static inline void sync_primitive(void)
127{
128}
129
Carl-Daniel Hailfingerd6bb8282012-07-21 17:27:08 +0000130int rget_io_perms(void)
David Hendricksb286da72012-02-13 00:35:35 +0000131{
Carl-Daniel Hailfingerd6bb8282012-07-21 17:27:08 +0000132 return 0;
David Hendricksb286da72012-02-13 00:35:35 +0000133}
134
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000135#else
136
137#error Unknown architecture
138
139#endif
140
Carl-Daniel Hailfingerfb0828f2010-02-12 19:35:25 +0000141void mmio_writeb(uint8_t val, void *addr)
142{
143 *(volatile uint8_t *) addr = val;
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000144 sync_primitive();
Carl-Daniel Hailfingerfb0828f2010-02-12 19:35:25 +0000145}
146
147void mmio_writew(uint16_t val, void *addr)
148{
149 *(volatile uint16_t *) addr = val;
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000150 sync_primitive();
Carl-Daniel Hailfingerfb0828f2010-02-12 19:35:25 +0000151}
152
153void mmio_writel(uint32_t val, void *addr)
154{
155 *(volatile uint32_t *) addr = val;
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000156 sync_primitive();
Carl-Daniel Hailfingerfb0828f2010-02-12 19:35:25 +0000157}
158
159uint8_t mmio_readb(void *addr)
160{
161 return *(volatile uint8_t *) addr;
162}
163
164uint16_t mmio_readw(void *addr)
165{
166 return *(volatile uint16_t *) addr;
167}
168
169uint32_t mmio_readl(void *addr)
170{
171 return *(volatile uint32_t *) addr;
172}
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000173
Carl-Daniel Hailfingerccd71c22012-03-01 22:38:27 +0000174void mmio_readn(void *addr, uint8_t *buf, size_t len)
175{
176 memcpy(buf, addr, len);
177 return;
178}
179
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000180void mmio_le_writeb(uint8_t val, void *addr)
181{
182 mmio_writeb(cpu_to_le8(val), addr);
183}
184
185void mmio_le_writew(uint16_t val, void *addr)
186{
187 mmio_writew(cpu_to_le16(val), addr);
188}
189
190void mmio_le_writel(uint32_t val, void *addr)
191{
192 mmio_writel(cpu_to_le32(val), addr);
193}
194
195uint8_t mmio_le_readb(void *addr)
196{
197 return le_to_cpu8(mmio_readb(addr));
198}
199
200uint16_t mmio_le_readw(void *addr)
201{
202 return le_to_cpu16(mmio_readw(addr));
203}
204
205uint32_t mmio_le_readl(void *addr)
206{
207 return le_to_cpu32(mmio_readl(addr));
208}
Carl-Daniel Hailfinger54ce73a2011-05-03 21:49:41 +0000209
210enum mmio_write_type {
211 mmio_write_type_b,
212 mmio_write_type_w,
213 mmio_write_type_l,
214};
215
216struct undo_mmio_write_data {
217 void *addr;
218 int reg;
219 enum mmio_write_type type;
220 union {
221 uint8_t bdata;
222 uint16_t wdata;
223 uint32_t ldata;
224 };
225};
226
David Hendricks8bb20212011-06-14 01:35:36 +0000227int undo_mmio_write(void *p)
Carl-Daniel Hailfinger54ce73a2011-05-03 21:49:41 +0000228{
229 struct undo_mmio_write_data *data = p;
230 msg_pdbg("Restoring MMIO space at %p\n", data->addr);
231 switch (data->type) {
232 case mmio_write_type_b:
233 mmio_writeb(data->bdata, data->addr);
234 break;
235 case mmio_write_type_w:
236 mmio_writew(data->wdata, data->addr);
237 break;
238 case mmio_write_type_l:
239 mmio_writel(data->ldata, data->addr);
240 break;
241 }
242 /* p was allocated in register_undo_mmio_write. */
243 free(p);
David Hendricks8bb20212011-06-14 01:35:36 +0000244 return 0;
Carl-Daniel Hailfinger54ce73a2011-05-03 21:49:41 +0000245}
246
247#define register_undo_mmio_write(a, c) \
248{ \
249 struct undo_mmio_write_data *undo_mmio_write_data; \
250 undo_mmio_write_data = malloc(sizeof(struct undo_mmio_write_data)); \
Stefan Tauner269de352011-07-12 22:35:21 +0000251 if (!undo_mmio_write_data) { \
252 msg_gerr("Out of memory!\n"); \
253 exit(1); \
254 } \
Carl-Daniel Hailfinger54ce73a2011-05-03 21:49:41 +0000255 undo_mmio_write_data->addr = a; \
256 undo_mmio_write_data->type = mmio_write_type_##c; \
257 undo_mmio_write_data->c##data = mmio_read##c(a); \
258 register_shutdown(undo_mmio_write, undo_mmio_write_data); \
259}
260
261#define register_undo_mmio_writeb(a) register_undo_mmio_write(a, b)
262#define register_undo_mmio_writew(a) register_undo_mmio_write(a, w)
263#define register_undo_mmio_writel(a) register_undo_mmio_write(a, l)
264
265void rmmio_writeb(uint8_t val, void *addr)
266{
267 register_undo_mmio_writeb(addr);
268 mmio_writeb(val, addr);
269}
270
271void rmmio_writew(uint16_t val, void *addr)
272{
273 register_undo_mmio_writew(addr);
274 mmio_writew(val, addr);
275}
276
277void rmmio_writel(uint32_t val, void *addr)
278{
279 register_undo_mmio_writel(addr);
280 mmio_writel(val, addr);
281}
282
283void rmmio_le_writeb(uint8_t val, void *addr)
284{
285 register_undo_mmio_writeb(addr);
286 mmio_le_writeb(val, addr);
287}
288
289void rmmio_le_writew(uint16_t val, void *addr)
290{
291 register_undo_mmio_writew(addr);
292 mmio_le_writew(val, addr);
293}
294
295void rmmio_le_writel(uint32_t val, void *addr)
296{
297 register_undo_mmio_writel(addr);
298 mmio_le_writel(val, addr);
299}
300
301void rmmio_valb(void *addr)
302{
303 register_undo_mmio_writeb(addr);
304}
305
306void rmmio_valw(void *addr)
307{
308 register_undo_mmio_writew(addr);
309}
310
311void rmmio_vall(void *addr)
312{
313 register_undo_mmio_writel(addr);
314}