blob: 5b18c32e38629373bd5ddf8c12d5f47cd35ec099 [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
Peter Lemenkov62829662012-12-29 19:26:55 +000021#define IS_X86 (defined(__i386__) || defined(__x86_64__) || defined(__amd64__))
22#define IS_MIPS (defined (__mips) || defined (__mips__) || defined (__MIPS__) || defined (mips))
23#define IS_PPC (defined(__powerpc__) || defined(__powerpc64__) || defined(__ppc__) || defined(__ppc64__))
24#define IS_ARM (defined (__arm__) || defined (_ARM))
25#if !(IS_X86 || IS_MIPS || IS_PPC || IS_ARM)
26#error Unknown architecture
27#endif
28
Peter Lemenkov62829662012-12-29 19:26:55 +000029#define IS_LINUX (defined(__gnu_linux__) || defined(__linux__))
Stefan Taunere038e902013-02-04 04:38:42 +000030#define IS_MACOSX (defined(__APPLE__) && defined(__MACH__))
Stefan Tauner95b4b6d2013-07-13 20:55:33 +000031#if !(IS_LINUX || IS_MACOSX || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__) || defined(__DJGPP__) || defined(__LIBPAYLOAD__) || defined(__sun))
Peter Lemenkov62829662012-12-29 19:26:55 +000032#error "Unknown operating system"
33#endif
34
Carl-Daniel Hailfingerfb0828f2010-02-12 19:35:25 +000035#include <stdint.h>
36#include <string.h>
37#include <stdlib.h>
Carl-Daniel Hailfinger11990da2013-07-13 23:21:05 +000038#include <errno.h>
Carl-Daniel Hailfingerdcef67e2010-06-21 23:20:15 +000039#include <sys/types.h>
Patrick Georgia9095a92010-09-30 17:03:32 +000040#if !defined (__DJGPP__) && !defined(__LIBPAYLOAD__)
Carl-Daniel Hailfinger11990da2013-07-13 23:21:05 +000041/* No file access needed/possible to get hardware access permissions. */
Carl-Daniel Hailfinger831e8f42010-05-30 22:24:40 +000042#include <unistd.h>
Carl-Daniel Hailfingerfb0828f2010-02-12 19:35:25 +000043#include <fcntl.h>
Patrick Georgia9095a92010-09-30 17:03:32 +000044#endif
Carl-Daniel Hailfingerfb0828f2010-02-12 19:35:25 +000045#include "flash.h"
Patrick Georgi32508eb2012-07-20 20:35:14 +000046#include "hwaccess.h"
Carl-Daniel Hailfingerfb0828f2010-02-12 19:35:25 +000047
Stefan Tauner95b4b6d2013-07-13 20:55:33 +000048#define USE_IOPL (IS_LINUX || IS_MACOSX || defined(__NetBSD__) || defined(__OpenBSD__))
49#define USE_DEV_IO (defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__))
50
51#if IS_X86 && USE_DEV_IO
Carl-Daniel Hailfingerfb0828f2010-02-12 19:35:25 +000052int io_fd;
53#endif
54
Peter Lemenkov62829662012-12-29 19:26:55 +000055/* Prevent reordering and/or merging of reads/writes to hardware.
56 * Such reordering and/or merging would break device accesses which depend on the exact access order.
57 */
58static inline void sync_primitive(void)
Carl-Daniel Hailfingerd6bb8282012-07-21 17:27:08 +000059{
Peter Lemenkov62829662012-12-29 19:26:55 +000060/* This is needed only on PowerPC because...
61 * - x86 uses uncached accesses which have a strongly ordered memory model and
62 * - MIPS uses uncached accesses in mode 2 on /dev/mem which has also a strongly ordered memory model
63 * - ARM uses a strongly ordered memory model for device memories.
64 */
65#if IS_PPC
66 asm("eieio" : : : "memory");
67#endif
68}
69
Carl-Daniel Hailfinger82258682013-01-08 22:49:12 +000070#if IS_X86 && !(defined(__DJGPP__) || defined(__LIBPAYLOAD__))
Peter Lemenkov62829662012-12-29 19:26:55 +000071static int release_io_perms(void *p)
72{
Carl-Daniel Hailfinger82258682013-01-08 22:49:12 +000073#if defined (__sun)
Carl-Daniel Hailfingerd6bb8282012-07-21 17:27:08 +000074 sysi86(SI86V86, V86SC_IOPL, 0);
Stefan Tauner95b4b6d2013-07-13 20:55:33 +000075#elif USE_DEV_IO
Carl-Daniel Hailfingerd6bb8282012-07-21 17:27:08 +000076 close(io_fd);
Stefan Tauner95b4b6d2013-07-13 20:55:33 +000077#elif USE_IOPL
Carl-Daniel Hailfingerd6bb8282012-07-21 17:27:08 +000078 iopl(0);
79#endif
Carl-Daniel Hailfingerd6bb8282012-07-21 17:27:08 +000080 return 0;
81}
Carl-Daniel Hailfinger82258682013-01-08 22:49:12 +000082#endif
Carl-Daniel Hailfingerd6bb8282012-07-21 17:27:08 +000083
84/* Get I/O permissions with automatic permission release on shutdown. */
85int rget_io_perms(void)
Carl-Daniel Hailfingerfb0828f2010-02-12 19:35:25 +000086{
Carl-Daniel Hailfinger82258682013-01-08 22:49:12 +000087#if IS_X86 && !(defined(__DJGPP__) || defined(__LIBPAYLOAD__))
88#if defined (__sun)
Carl-Daniel Hailfingerfb0828f2010-02-12 19:35:25 +000089 if (sysi86(SI86V86, V86SC_IOPL, PS_IOPL) != 0) {
Stefan Tauner95b4b6d2013-07-13 20:55:33 +000090#elif USE_DEV_IO
Carl-Daniel Hailfingerfb0828f2010-02-12 19:35:25 +000091 if ((io_fd = open("/dev/io", O_RDWR)) < 0) {
Stefan Tauner95b4b6d2013-07-13 20:55:33 +000092#elif USE_IOPL
Carl-Daniel Hailfingerfb0828f2010-02-12 19:35:25 +000093 if (iopl(3) != 0) {
94#endif
Stefan Tauner95b4b6d2013-07-13 20:55:33 +000095 msg_perr("ERROR: Could not get I/O privileges (%s).\n", strerror(errno));
96 msg_perr("You need to be root.\n");
Carl-Daniel Hailfingerb63b0672010-07-02 17:12:50 +000097#if defined (__OpenBSD__)
Stefan Tauner95b4b6d2013-07-13 20:55:33 +000098 msg_perr("If you are root already please set securelevel=-1 in /etc/rc.securelevel and\n"
99 "reboot, or reboot into single user mode.\n");
100#elif defined(__NetBSD__)
101 msg_perr("If you are root already please reboot into single user mode or make sure\n"
102 "that your kernel configuration has the option INSECURE enabled.\n");
Carl-Daniel Hailfingerb63b0672010-07-02 17:12:50 +0000103#endif
Carl-Daniel Hailfingerd6bb8282012-07-21 17:27:08 +0000104 return 1;
105 } else {
106 register_shutdown(release_io_perms, NULL);
Carl-Daniel Hailfingerfb0828f2010-02-12 19:35:25 +0000107 }
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000108#else
Carl-Daniel Hailfinger82258682013-01-08 22:49:12 +0000109 /* DJGPP and libpayload environments have full PCI port I/O permissions by default. */
110 /* PCI port I/O support is unimplemented on PPC/MIPS and unavailable on ARM. */
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000111#endif
Peter Lemenkov62829662012-12-29 19:26:55 +0000112 return 0;
113}
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000114
Carl-Daniel Hailfingerfb0828f2010-02-12 19:35:25 +0000115void mmio_writeb(uint8_t val, void *addr)
116{
117 *(volatile uint8_t *) addr = val;
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000118 sync_primitive();
Carl-Daniel Hailfingerfb0828f2010-02-12 19:35:25 +0000119}
120
121void mmio_writew(uint16_t val, void *addr)
122{
123 *(volatile uint16_t *) addr = val;
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000124 sync_primitive();
Carl-Daniel Hailfingerfb0828f2010-02-12 19:35:25 +0000125}
126
127void mmio_writel(uint32_t val, void *addr)
128{
129 *(volatile uint32_t *) addr = val;
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000130 sync_primitive();
Carl-Daniel Hailfingerfb0828f2010-02-12 19:35:25 +0000131}
132
133uint8_t mmio_readb(void *addr)
134{
135 return *(volatile uint8_t *) addr;
136}
137
138uint16_t mmio_readw(void *addr)
139{
140 return *(volatile uint16_t *) addr;
141}
142
143uint32_t mmio_readl(void *addr)
144{
145 return *(volatile uint32_t *) addr;
146}
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000147
Carl-Daniel Hailfingerccd71c22012-03-01 22:38:27 +0000148void mmio_readn(void *addr, uint8_t *buf, size_t len)
149{
150 memcpy(buf, addr, len);
151 return;
152}
153
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000154void mmio_le_writeb(uint8_t val, void *addr)
155{
156 mmio_writeb(cpu_to_le8(val), addr);
157}
158
159void mmio_le_writew(uint16_t val, void *addr)
160{
161 mmio_writew(cpu_to_le16(val), addr);
162}
163
164void mmio_le_writel(uint32_t val, void *addr)
165{
166 mmio_writel(cpu_to_le32(val), addr);
167}
168
169uint8_t mmio_le_readb(void *addr)
170{
171 return le_to_cpu8(mmio_readb(addr));
172}
173
174uint16_t mmio_le_readw(void *addr)
175{
176 return le_to_cpu16(mmio_readw(addr));
177}
178
179uint32_t mmio_le_readl(void *addr)
180{
181 return le_to_cpu32(mmio_readl(addr));
182}
Carl-Daniel Hailfinger54ce73a2011-05-03 21:49:41 +0000183
184enum mmio_write_type {
185 mmio_write_type_b,
186 mmio_write_type_w,
187 mmio_write_type_l,
188};
189
190struct undo_mmio_write_data {
191 void *addr;
192 int reg;
193 enum mmio_write_type type;
194 union {
195 uint8_t bdata;
196 uint16_t wdata;
197 uint32_t ldata;
198 };
199};
200
David Hendricks8bb20212011-06-14 01:35:36 +0000201int undo_mmio_write(void *p)
Carl-Daniel Hailfinger54ce73a2011-05-03 21:49:41 +0000202{
203 struct undo_mmio_write_data *data = p;
204 msg_pdbg("Restoring MMIO space at %p\n", data->addr);
205 switch (data->type) {
206 case mmio_write_type_b:
207 mmio_writeb(data->bdata, data->addr);
208 break;
209 case mmio_write_type_w:
210 mmio_writew(data->wdata, data->addr);
211 break;
212 case mmio_write_type_l:
213 mmio_writel(data->ldata, data->addr);
214 break;
215 }
216 /* p was allocated in register_undo_mmio_write. */
217 free(p);
David Hendricks8bb20212011-06-14 01:35:36 +0000218 return 0;
Carl-Daniel Hailfinger54ce73a2011-05-03 21:49:41 +0000219}
220
221#define register_undo_mmio_write(a, c) \
222{ \
223 struct undo_mmio_write_data *undo_mmio_write_data; \
224 undo_mmio_write_data = malloc(sizeof(struct undo_mmio_write_data)); \
Stefan Tauner269de352011-07-12 22:35:21 +0000225 if (!undo_mmio_write_data) { \
226 msg_gerr("Out of memory!\n"); \
227 exit(1); \
228 } \
Carl-Daniel Hailfinger54ce73a2011-05-03 21:49:41 +0000229 undo_mmio_write_data->addr = a; \
230 undo_mmio_write_data->type = mmio_write_type_##c; \
231 undo_mmio_write_data->c##data = mmio_read##c(a); \
232 register_shutdown(undo_mmio_write, undo_mmio_write_data); \
233}
234
235#define register_undo_mmio_writeb(a) register_undo_mmio_write(a, b)
236#define register_undo_mmio_writew(a) register_undo_mmio_write(a, w)
237#define register_undo_mmio_writel(a) register_undo_mmio_write(a, l)
238
239void rmmio_writeb(uint8_t val, void *addr)
240{
241 register_undo_mmio_writeb(addr);
242 mmio_writeb(val, addr);
243}
244
245void rmmio_writew(uint16_t val, void *addr)
246{
247 register_undo_mmio_writew(addr);
248 mmio_writew(val, addr);
249}
250
251void rmmio_writel(uint32_t val, void *addr)
252{
253 register_undo_mmio_writel(addr);
254 mmio_writel(val, addr);
255}
256
257void rmmio_le_writeb(uint8_t val, void *addr)
258{
259 register_undo_mmio_writeb(addr);
260 mmio_le_writeb(val, addr);
261}
262
263void rmmio_le_writew(uint16_t val, void *addr)
264{
265 register_undo_mmio_writew(addr);
266 mmio_le_writew(val, addr);
267}
268
269void rmmio_le_writel(uint32_t val, void *addr)
270{
271 register_undo_mmio_writel(addr);
272 mmio_le_writel(val, addr);
273}
274
275void rmmio_valb(void *addr)
276{
277 register_undo_mmio_writeb(addr);
278}
279
280void rmmio_valw(void *addr)
281{
282 register_undo_mmio_writew(addr);
283}
284
285void rmmio_vall(void *addr)
286{
287 register_undo_mmio_writel(addr);
288}