blob: c90490b3cab7da0b991b62545c6575612534eedc [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
Stefan Taunerb0eee9b2015-01-10 09:32:50 +000021#include "platform.h"
Peter Lemenkov62829662012-12-29 19:26:55 +000022
Carl-Daniel Hailfingerfb0828f2010-02-12 19:35:25 +000023#include <stdint.h>
24#include <string.h>
25#include <stdlib.h>
Carl-Daniel Hailfinger11990da2013-07-13 23:21:05 +000026#include <errno.h>
Carl-Daniel Hailfingerdcef67e2010-06-21 23:20:15 +000027#include <sys/types.h>
Patrick Georgia9095a92010-09-30 17:03:32 +000028#if !defined (__DJGPP__) && !defined(__LIBPAYLOAD__)
Carl-Daniel Hailfinger11990da2013-07-13 23:21:05 +000029/* No file access needed/possible to get hardware access permissions. */
Carl-Daniel Hailfinger831e8f42010-05-30 22:24:40 +000030#include <unistd.h>
Carl-Daniel Hailfingerfb0828f2010-02-12 19:35:25 +000031#include <fcntl.h>
Patrick Georgia9095a92010-09-30 17:03:32 +000032#endif
Carl-Daniel Hailfingerfb0828f2010-02-12 19:35:25 +000033#include "flash.h"
Patrick Georgi32508eb2012-07-20 20:35:14 +000034#include "hwaccess.h"
Carl-Daniel Hailfingerfb0828f2010-02-12 19:35:25 +000035
Stefan Taunerb0eee9b2015-01-10 09:32:50 +000036#if !(IS_LINUX || IS_MACOSX || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__) || defined(__DJGPP__) || defined(__LIBPAYLOAD__) || defined(__sun))
37#error "Unknown operating system"
38#endif
39
Stefan Tauner95b4b6d2013-07-13 20:55:33 +000040#define USE_IOPL (IS_LINUX || IS_MACOSX || defined(__NetBSD__) || defined(__OpenBSD__))
41#define USE_DEV_IO (defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__))
42
43#if IS_X86 && USE_DEV_IO
Carl-Daniel Hailfingerfb0828f2010-02-12 19:35:25 +000044int io_fd;
45#endif
46
Peter Lemenkov62829662012-12-29 19:26:55 +000047/* Prevent reordering and/or merging of reads/writes to hardware.
48 * Such reordering and/or merging would break device accesses which depend on the exact access order.
49 */
50static inline void sync_primitive(void)
Carl-Daniel Hailfingerd6bb8282012-07-21 17:27:08 +000051{
Peter Lemenkov62829662012-12-29 19:26:55 +000052/* This is needed only on PowerPC because...
53 * - x86 uses uncached accesses which have a strongly ordered memory model and
54 * - MIPS uses uncached accesses in mode 2 on /dev/mem which has also a strongly ordered memory model
55 * - ARM uses a strongly ordered memory model for device memories.
56 */
Stefan Taunerb0eee9b2015-01-10 09:32:50 +000057#if IS_PPC // cf. http://lxr.free-electrons.com/source/arch/powerpc/include/asm/barrier.h
Peter Lemenkov62829662012-12-29 19:26:55 +000058 asm("eieio" : : : "memory");
59#endif
60}
61
Carl-Daniel Hailfinger82258682013-01-08 22:49:12 +000062#if IS_X86 && !(defined(__DJGPP__) || defined(__LIBPAYLOAD__))
Peter Lemenkov62829662012-12-29 19:26:55 +000063static int release_io_perms(void *p)
64{
Carl-Daniel Hailfinger82258682013-01-08 22:49:12 +000065#if defined (__sun)
Carl-Daniel Hailfingerd6bb8282012-07-21 17:27:08 +000066 sysi86(SI86V86, V86SC_IOPL, 0);
Stefan Tauner95b4b6d2013-07-13 20:55:33 +000067#elif USE_DEV_IO
Carl-Daniel Hailfingerd6bb8282012-07-21 17:27:08 +000068 close(io_fd);
Stefan Tauner95b4b6d2013-07-13 20:55:33 +000069#elif USE_IOPL
Carl-Daniel Hailfingerd6bb8282012-07-21 17:27:08 +000070 iopl(0);
71#endif
Carl-Daniel Hailfingerd6bb8282012-07-21 17:27:08 +000072 return 0;
73}
Carl-Daniel Hailfinger82258682013-01-08 22:49:12 +000074#endif
Carl-Daniel Hailfingerd6bb8282012-07-21 17:27:08 +000075
76/* Get I/O permissions with automatic permission release on shutdown. */
77int rget_io_perms(void)
Carl-Daniel Hailfingerfb0828f2010-02-12 19:35:25 +000078{
Carl-Daniel Hailfinger82258682013-01-08 22:49:12 +000079#if IS_X86 && !(defined(__DJGPP__) || defined(__LIBPAYLOAD__))
80#if defined (__sun)
Carl-Daniel Hailfingerfb0828f2010-02-12 19:35:25 +000081 if (sysi86(SI86V86, V86SC_IOPL, PS_IOPL) != 0) {
Stefan Tauner95b4b6d2013-07-13 20:55:33 +000082#elif USE_DEV_IO
Carl-Daniel Hailfingerfb0828f2010-02-12 19:35:25 +000083 if ((io_fd = open("/dev/io", O_RDWR)) < 0) {
Stefan Tauner95b4b6d2013-07-13 20:55:33 +000084#elif USE_IOPL
Carl-Daniel Hailfingerfb0828f2010-02-12 19:35:25 +000085 if (iopl(3) != 0) {
86#endif
Stefan Tauner95b4b6d2013-07-13 20:55:33 +000087 msg_perr("ERROR: Could not get I/O privileges (%s).\n", strerror(errno));
88 msg_perr("You need to be root.\n");
Carl-Daniel Hailfingerb63b0672010-07-02 17:12:50 +000089#if defined (__OpenBSD__)
Stefan Tauner95b4b6d2013-07-13 20:55:33 +000090 msg_perr("If you are root already please set securelevel=-1 in /etc/rc.securelevel and\n"
91 "reboot, or reboot into single user mode.\n");
92#elif defined(__NetBSD__)
93 msg_perr("If you are root already please reboot into single user mode or make sure\n"
94 "that your kernel configuration has the option INSECURE enabled.\n");
Carl-Daniel Hailfingerb63b0672010-07-02 17:12:50 +000095#endif
Carl-Daniel Hailfingerd6bb8282012-07-21 17:27:08 +000096 return 1;
97 } else {
98 register_shutdown(release_io_perms, NULL);
Carl-Daniel Hailfingerfb0828f2010-02-12 19:35:25 +000099 }
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000100#else
Carl-Daniel Hailfinger82258682013-01-08 22:49:12 +0000101 /* DJGPP and libpayload environments have full PCI port I/O permissions by default. */
102 /* PCI port I/O support is unimplemented on PPC/MIPS and unavailable on ARM. */
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000103#endif
Peter Lemenkov62829662012-12-29 19:26:55 +0000104 return 0;
105}
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000106
Carl-Daniel Hailfingerfb0828f2010-02-12 19:35:25 +0000107void mmio_writeb(uint8_t val, void *addr)
108{
109 *(volatile uint8_t *) addr = val;
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000110 sync_primitive();
Carl-Daniel Hailfingerfb0828f2010-02-12 19:35:25 +0000111}
112
113void mmio_writew(uint16_t val, void *addr)
114{
115 *(volatile uint16_t *) addr = val;
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000116 sync_primitive();
Carl-Daniel Hailfingerfb0828f2010-02-12 19:35:25 +0000117}
118
119void mmio_writel(uint32_t val, void *addr)
120{
121 *(volatile uint32_t *) addr = val;
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000122 sync_primitive();
Carl-Daniel Hailfingerfb0828f2010-02-12 19:35:25 +0000123}
124
125uint8_t mmio_readb(void *addr)
126{
127 return *(volatile uint8_t *) addr;
128}
129
130uint16_t mmio_readw(void *addr)
131{
132 return *(volatile uint16_t *) addr;
133}
134
135uint32_t mmio_readl(void *addr)
136{
137 return *(volatile uint32_t *) addr;
138}
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000139
Carl-Daniel Hailfingerccd71c22012-03-01 22:38:27 +0000140void mmio_readn(void *addr, uint8_t *buf, size_t len)
141{
142 memcpy(buf, addr, len);
143 return;
144}
145
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000146void mmio_le_writeb(uint8_t val, void *addr)
147{
148 mmio_writeb(cpu_to_le8(val), addr);
149}
150
151void mmio_le_writew(uint16_t val, void *addr)
152{
153 mmio_writew(cpu_to_le16(val), addr);
154}
155
156void mmio_le_writel(uint32_t val, void *addr)
157{
158 mmio_writel(cpu_to_le32(val), addr);
159}
160
161uint8_t mmio_le_readb(void *addr)
162{
163 return le_to_cpu8(mmio_readb(addr));
164}
165
166uint16_t mmio_le_readw(void *addr)
167{
168 return le_to_cpu16(mmio_readw(addr));
169}
170
171uint32_t mmio_le_readl(void *addr)
172{
173 return le_to_cpu32(mmio_readl(addr));
174}
Carl-Daniel Hailfinger54ce73a2011-05-03 21:49:41 +0000175
176enum mmio_write_type {
177 mmio_write_type_b,
178 mmio_write_type_w,
179 mmio_write_type_l,
180};
181
182struct undo_mmio_write_data {
183 void *addr;
184 int reg;
185 enum mmio_write_type type;
186 union {
187 uint8_t bdata;
188 uint16_t wdata;
189 uint32_t ldata;
190 };
191};
192
David Hendricks8bb20212011-06-14 01:35:36 +0000193int undo_mmio_write(void *p)
Carl-Daniel Hailfinger54ce73a2011-05-03 21:49:41 +0000194{
195 struct undo_mmio_write_data *data = p;
196 msg_pdbg("Restoring MMIO space at %p\n", data->addr);
197 switch (data->type) {
198 case mmio_write_type_b:
199 mmio_writeb(data->bdata, data->addr);
200 break;
201 case mmio_write_type_w:
202 mmio_writew(data->wdata, data->addr);
203 break;
204 case mmio_write_type_l:
205 mmio_writel(data->ldata, data->addr);
206 break;
207 }
208 /* p was allocated in register_undo_mmio_write. */
209 free(p);
David Hendricks8bb20212011-06-14 01:35:36 +0000210 return 0;
Carl-Daniel Hailfinger54ce73a2011-05-03 21:49:41 +0000211}
212
213#define register_undo_mmio_write(a, c) \
214{ \
215 struct undo_mmio_write_data *undo_mmio_write_data; \
216 undo_mmio_write_data = malloc(sizeof(struct undo_mmio_write_data)); \
Stefan Tauner269de352011-07-12 22:35:21 +0000217 if (!undo_mmio_write_data) { \
218 msg_gerr("Out of memory!\n"); \
219 exit(1); \
220 } \
Carl-Daniel Hailfinger54ce73a2011-05-03 21:49:41 +0000221 undo_mmio_write_data->addr = a; \
222 undo_mmio_write_data->type = mmio_write_type_##c; \
223 undo_mmio_write_data->c##data = mmio_read##c(a); \
224 register_shutdown(undo_mmio_write, undo_mmio_write_data); \
225}
226
227#define register_undo_mmio_writeb(a) register_undo_mmio_write(a, b)
228#define register_undo_mmio_writew(a) register_undo_mmio_write(a, w)
229#define register_undo_mmio_writel(a) register_undo_mmio_write(a, l)
230
231void rmmio_writeb(uint8_t val, void *addr)
232{
233 register_undo_mmio_writeb(addr);
234 mmio_writeb(val, addr);
235}
236
237void rmmio_writew(uint16_t val, void *addr)
238{
239 register_undo_mmio_writew(addr);
240 mmio_writew(val, addr);
241}
242
243void rmmio_writel(uint32_t val, void *addr)
244{
245 register_undo_mmio_writel(addr);
246 mmio_writel(val, addr);
247}
248
249void rmmio_le_writeb(uint8_t val, void *addr)
250{
251 register_undo_mmio_writeb(addr);
252 mmio_le_writeb(val, addr);
253}
254
255void rmmio_le_writew(uint16_t val, void *addr)
256{
257 register_undo_mmio_writew(addr);
258 mmio_le_writew(val, addr);
259}
260
261void rmmio_le_writel(uint32_t val, void *addr)
262{
263 register_undo_mmio_writel(addr);
264 mmio_le_writel(val, addr);
265}
266
267void rmmio_valb(void *addr)
268{
269 register_undo_mmio_writeb(addr);
270}
271
272void rmmio_valw(void *addr)
273{
274 register_undo_mmio_writew(addr);
275}
276
277void rmmio_vall(void *addr)
278{
279 register_undo_mmio_writel(addr);
280}