blob: 1901ee6a709f6d400c07c434486312383d6226bb [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 Tauner8e656542016-03-06 22:32:16 +000036#if !(IS_LINUX || IS_MACOSX || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__) || defined(__DJGPP__) || defined(__LIBPAYLOAD__) || defined(__sun) || defined(__gnu_hurd__))
Stefan Taunerb0eee9b2015-01-10 09:32:50 +000037#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__))
Stefan Tauner8e656542016-03-06 22:32:16 +000042#define USE_IOPERM (defined(__gnu_hurd__))
43
44#if USE_IOPERM
45#include <sys/io.h>
46#endif
Stefan Tauner95b4b6d2013-07-13 20:55:33 +000047
48#if IS_X86 && USE_DEV_IO
Carl-Daniel Hailfingerfb0828f2010-02-12 19:35:25 +000049int io_fd;
50#endif
51
Peter Lemenkov62829662012-12-29 19:26:55 +000052/* Prevent reordering and/or merging of reads/writes to hardware.
53 * Such reordering and/or merging would break device accesses which depend on the exact access order.
54 */
55static inline void sync_primitive(void)
Carl-Daniel Hailfingerd6bb8282012-07-21 17:27:08 +000056{
Stefan Taunerfb2d77c2015-02-10 08:03:10 +000057/* This is not needed for...
58 * - x86: uses uncached accesses which have a strongly ordered memory model.
59 * - MIPS: uses uncached accesses in mode 2 on /dev/mem which has also a strongly ordered memory model.
60 * - ARM: uses a strongly ordered memory model for device memories.
61 *
62 * See also https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/Documentation/memory-barriers.txt
Peter Lemenkov62829662012-12-29 19:26:55 +000063 */
Stefan Taunerb0eee9b2015-01-10 09:32:50 +000064#if IS_PPC // cf. http://lxr.free-electrons.com/source/arch/powerpc/include/asm/barrier.h
Peter Lemenkov62829662012-12-29 19:26:55 +000065 asm("eieio" : : : "memory");
Stefan Taunerfb2d77c2015-02-10 08:03:10 +000066#elif IS_SPARC
67#if defined(__sparc_v9__) || defined(__sparcv9)
68 /* Sparc V9 CPUs support three different memory orderings that range from x86-like TSO to PowerPC-like
69 * RMO. The modes can be switched at runtime thus to make sure we maintain the right order of access we
70 * use the strongest hardware memory barriers that exist on Sparc V9. */
71 asm volatile ("membar #Sync" ::: "memory");
72#elif defined(__sparc_v8__) || defined(__sparcv8)
73 /* On SPARC V8 there is no RMO just PSO and that does not apply to I/O accesses... but if V8 code is run
74 * on V9 CPUs it might apply... or not... we issue a write barrier anyway. That's the most suitable
75 * operation in the V8 instruction set anyway. If you know better then please tell us. */
76 asm volatile ("stbar");
77#else
78 #error Unknown and/or unsupported SPARC instruction set version detected.
79#endif
Peter Lemenkov62829662012-12-29 19:26:55 +000080#endif
81}
82
Carl-Daniel Hailfinger82258682013-01-08 22:49:12 +000083#if IS_X86 && !(defined(__DJGPP__) || defined(__LIBPAYLOAD__))
Peter Lemenkov62829662012-12-29 19:26:55 +000084static int release_io_perms(void *p)
85{
Carl-Daniel Hailfinger82258682013-01-08 22:49:12 +000086#if defined (__sun)
Carl-Daniel Hailfingerd6bb8282012-07-21 17:27:08 +000087 sysi86(SI86V86, V86SC_IOPL, 0);
Stefan Tauner95b4b6d2013-07-13 20:55:33 +000088#elif USE_DEV_IO
Carl-Daniel Hailfingerd6bb8282012-07-21 17:27:08 +000089 close(io_fd);
Stefan Tauner8e656542016-03-06 22:32:16 +000090#elif USE_IOPERM
91 ioperm(0, 65536, 0);
Stefan Tauner95b4b6d2013-07-13 20:55:33 +000092#elif USE_IOPL
Carl-Daniel Hailfingerd6bb8282012-07-21 17:27:08 +000093 iopl(0);
94#endif
Carl-Daniel Hailfingerd6bb8282012-07-21 17:27:08 +000095 return 0;
96}
Carl-Daniel Hailfinger82258682013-01-08 22:49:12 +000097#endif
Carl-Daniel Hailfingerd6bb8282012-07-21 17:27:08 +000098
99/* Get I/O permissions with automatic permission release on shutdown. */
100int rget_io_perms(void)
Carl-Daniel Hailfingerfb0828f2010-02-12 19:35:25 +0000101{
Carl-Daniel Hailfinger82258682013-01-08 22:49:12 +0000102#if IS_X86 && !(defined(__DJGPP__) || defined(__LIBPAYLOAD__))
103#if defined (__sun)
Carl-Daniel Hailfingerfb0828f2010-02-12 19:35:25 +0000104 if (sysi86(SI86V86, V86SC_IOPL, PS_IOPL) != 0) {
Stefan Tauner95b4b6d2013-07-13 20:55:33 +0000105#elif USE_DEV_IO
Carl-Daniel Hailfingerfb0828f2010-02-12 19:35:25 +0000106 if ((io_fd = open("/dev/io", O_RDWR)) < 0) {
Stefan Tauner8e656542016-03-06 22:32:16 +0000107#elif USE_IOPERM
108 if (ioperm(0, 65536, 1) != 0) {
Stefan Tauner95b4b6d2013-07-13 20:55:33 +0000109#elif USE_IOPL
Carl-Daniel Hailfingerfb0828f2010-02-12 19:35:25 +0000110 if (iopl(3) != 0) {
111#endif
Stefan Tauner95b4b6d2013-07-13 20:55:33 +0000112 msg_perr("ERROR: Could not get I/O privileges (%s).\n", strerror(errno));
113 msg_perr("You need to be root.\n");
Carl-Daniel Hailfingerb63b0672010-07-02 17:12:50 +0000114#if defined (__OpenBSD__)
Stefan Tauner95b4b6d2013-07-13 20:55:33 +0000115 msg_perr("If you are root already please set securelevel=-1 in /etc/rc.securelevel and\n"
116 "reboot, or reboot into single user mode.\n");
117#elif defined(__NetBSD__)
118 msg_perr("If you are root already please reboot into single user mode or make sure\n"
119 "that your kernel configuration has the option INSECURE enabled.\n");
Carl-Daniel Hailfingerb63b0672010-07-02 17:12:50 +0000120#endif
Carl-Daniel Hailfingerd6bb8282012-07-21 17:27:08 +0000121 return 1;
122 } else {
123 register_shutdown(release_io_perms, NULL);
Carl-Daniel Hailfingerfb0828f2010-02-12 19:35:25 +0000124 }
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000125#else
Carl-Daniel Hailfinger82258682013-01-08 22:49:12 +0000126 /* DJGPP and libpayload environments have full PCI port I/O permissions by default. */
127 /* PCI port I/O support is unimplemented on PPC/MIPS and unavailable on ARM. */
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000128#endif
Peter Lemenkov62829662012-12-29 19:26:55 +0000129 return 0;
130}
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000131
Carl-Daniel Hailfingerfb0828f2010-02-12 19:35:25 +0000132void mmio_writeb(uint8_t val, void *addr)
133{
134 *(volatile uint8_t *) addr = val;
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000135 sync_primitive();
Carl-Daniel Hailfingerfb0828f2010-02-12 19:35:25 +0000136}
137
138void mmio_writew(uint16_t val, void *addr)
139{
140 *(volatile uint16_t *) addr = val;
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000141 sync_primitive();
Carl-Daniel Hailfingerfb0828f2010-02-12 19:35:25 +0000142}
143
144void mmio_writel(uint32_t val, void *addr)
145{
146 *(volatile uint32_t *) addr = val;
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000147 sync_primitive();
Carl-Daniel Hailfingerfb0828f2010-02-12 19:35:25 +0000148}
149
150uint8_t mmio_readb(void *addr)
151{
152 return *(volatile uint8_t *) addr;
153}
154
155uint16_t mmio_readw(void *addr)
156{
157 return *(volatile uint16_t *) addr;
158}
159
160uint32_t mmio_readl(void *addr)
161{
162 return *(volatile uint32_t *) addr;
163}
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000164
Carl-Daniel Hailfingerccd71c22012-03-01 22:38:27 +0000165void mmio_readn(void *addr, uint8_t *buf, size_t len)
166{
167 memcpy(buf, addr, len);
168 return;
169}
170
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000171void mmio_le_writeb(uint8_t val, void *addr)
172{
173 mmio_writeb(cpu_to_le8(val), addr);
174}
175
176void mmio_le_writew(uint16_t val, void *addr)
177{
178 mmio_writew(cpu_to_le16(val), addr);
179}
180
181void mmio_le_writel(uint32_t val, void *addr)
182{
183 mmio_writel(cpu_to_le32(val), addr);
184}
185
186uint8_t mmio_le_readb(void *addr)
187{
188 return le_to_cpu8(mmio_readb(addr));
189}
190
191uint16_t mmio_le_readw(void *addr)
192{
193 return le_to_cpu16(mmio_readw(addr));
194}
195
196uint32_t mmio_le_readl(void *addr)
197{
198 return le_to_cpu32(mmio_readl(addr));
199}
Carl-Daniel Hailfinger54ce73a2011-05-03 21:49:41 +0000200
201enum mmio_write_type {
202 mmio_write_type_b,
203 mmio_write_type_w,
204 mmio_write_type_l,
205};
206
207struct undo_mmio_write_data {
208 void *addr;
209 int reg;
210 enum mmio_write_type type;
211 union {
212 uint8_t bdata;
213 uint16_t wdata;
214 uint32_t ldata;
215 };
216};
217
David Hendricks8bb20212011-06-14 01:35:36 +0000218int undo_mmio_write(void *p)
Carl-Daniel Hailfinger54ce73a2011-05-03 21:49:41 +0000219{
220 struct undo_mmio_write_data *data = p;
221 msg_pdbg("Restoring MMIO space at %p\n", data->addr);
222 switch (data->type) {
223 case mmio_write_type_b:
224 mmio_writeb(data->bdata, data->addr);
225 break;
226 case mmio_write_type_w:
227 mmio_writew(data->wdata, data->addr);
228 break;
229 case mmio_write_type_l:
230 mmio_writel(data->ldata, data->addr);
231 break;
232 }
233 /* p was allocated in register_undo_mmio_write. */
234 free(p);
David Hendricks8bb20212011-06-14 01:35:36 +0000235 return 0;
Carl-Daniel Hailfinger54ce73a2011-05-03 21:49:41 +0000236}
237
238#define register_undo_mmio_write(a, c) \
239{ \
240 struct undo_mmio_write_data *undo_mmio_write_data; \
241 undo_mmio_write_data = malloc(sizeof(struct undo_mmio_write_data)); \
Stefan Tauner269de352011-07-12 22:35:21 +0000242 if (!undo_mmio_write_data) { \
243 msg_gerr("Out of memory!\n"); \
244 exit(1); \
245 } \
Carl-Daniel Hailfinger54ce73a2011-05-03 21:49:41 +0000246 undo_mmio_write_data->addr = a; \
247 undo_mmio_write_data->type = mmio_write_type_##c; \
248 undo_mmio_write_data->c##data = mmio_read##c(a); \
249 register_shutdown(undo_mmio_write, undo_mmio_write_data); \
250}
251
252#define register_undo_mmio_writeb(a) register_undo_mmio_write(a, b)
253#define register_undo_mmio_writew(a) register_undo_mmio_write(a, w)
254#define register_undo_mmio_writel(a) register_undo_mmio_write(a, l)
255
256void rmmio_writeb(uint8_t val, void *addr)
257{
258 register_undo_mmio_writeb(addr);
259 mmio_writeb(val, addr);
260}
261
262void rmmio_writew(uint16_t val, void *addr)
263{
264 register_undo_mmio_writew(addr);
265 mmio_writew(val, addr);
266}
267
268void rmmio_writel(uint32_t val, void *addr)
269{
270 register_undo_mmio_writel(addr);
271 mmio_writel(val, addr);
272}
273
274void rmmio_le_writeb(uint8_t val, void *addr)
275{
276 register_undo_mmio_writeb(addr);
277 mmio_le_writeb(val, addr);
278}
279
280void rmmio_le_writew(uint16_t val, void *addr)
281{
282 register_undo_mmio_writew(addr);
283 mmio_le_writew(val, addr);
284}
285
286void rmmio_le_writel(uint32_t val, void *addr)
287{
288 register_undo_mmio_writel(addr);
289 mmio_le_writel(val, addr);
290}
291
292void rmmio_valb(void *addr)
293{
294 register_undo_mmio_writeb(addr);
295}
296
297void rmmio_valw(void *addr)
298{
299 register_undo_mmio_writew(addr);
300}
301
302void rmmio_vall(void *addr)
303{
304 register_undo_mmio_writel(addr);
305}