blob: be9b9539eced6df1cc8e1d6ee5ff0b35b211c2d9 [file] [log] [blame]
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +11001/*
2 * Copyright 2016 IBM
Cyril Bur314929b2016-10-14 15:55:16 +11003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +11008 * http://www.apache.org/licenses/LICENSE-2.0
Cyril Bur314929b2016-10-14 15:55:16 +11009 *
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +110010 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
Cyril Bur314929b2016-10-14 15:55:16 +110015 *
16 */
17
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +110018#ifndef MBOX_H
19#define MBOX_H
Cyril Bur314929b2016-10-14 15:55:16 +110020
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +110021#include <mtd/mtd-abi.h>
22#include <systemd/sd-bus.h>
Andrew Jeffery8ecbdb52017-04-10 16:26:08 +093023#include <poll.h>
24#include <stdbool.h>
Cyril Bur314929b2016-10-14 15:55:16 +110025
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +110026enum api_version {
27 API_VERSION_INVAL = 0,
28 API_VERSION_1 = 1,
29 API_VERSION_2 = 2
Cyril Bur314929b2016-10-14 15:55:16 +110030};
31
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +110032#define API_MIN_VERSION API_VERSION_1
33#define API_MAX_VERSION API_VERSION_2
34
35#define THIS_NAME "Mailbox Daemon"
36#define SUB_VERSION 0
37
38/* Command Values */
39#define MBOX_C_RESET_STATE 0x01
40#define MBOX_C_GET_MBOX_INFO 0x02
41#define MBOX_C_GET_FLASH_INFO 0x03
42#define MBOX_C_READ_WINDOW 0x04
43#define MBOX_C_CLOSE_WINDOW 0x05
44#define MBOX_C_WRITE_WINDOW 0x06
45#define MBOX_C_WRITE_DIRTY 0x07
46#define MBOX_C_WRITE_FLUSH 0x08
47#define MBOX_C_ACK 0x09
48#define MBOX_C_WRITE_ERASE 0x0a
49#define NUM_MBOX_CMDS MBOX_C_WRITE_ERASE
50
51/* Response Values */
52#define MBOX_R_SUCCESS 0x01
53#define MBOX_R_PARAM_ERROR 0x02
54#define MBOX_R_WRITE_ERROR 0x03
55#define MBOX_R_SYSTEM_ERROR 0x04
56#define MBOX_R_TIMEOUT 0x05
57#define MBOX_R_BUSY 0x06
58#define MBOX_R_WINDOW_ERROR 0x07
Andrew Jeffery55dede62017-04-24 16:13:06 +093059#define MBOX_R_SEQ_ERROR 0x08
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +110060
61/* Argument Flags */
62#define FLAGS_NONE 0x00
63#define FLAGS_SHORT_LIFETIME 0x01
64
65/* BMC Event Notification */
66#define BMC_EVENT_REBOOT 0x01
67#define BMC_EVENT_WINDOW_RESET 0x02
68#define BMC_EVENT_ACK_MASK (BMC_EVENT_REBOOT | \
69 BMC_EVENT_WINDOW_RESET)
70#define BMC_EVENT_FLASH_CTRL_LOST 0x40
71#define BMC_EVENT_DAEMON_READY 0x80
72#define BMC_EVENT_V1_MASK BMC_EVENT_REBOOT
73#define BMC_EVENT_V2_MASK (BMC_EVENT_REBOOT | \
74 BMC_EVENT_WINDOW_RESET | \
75 BMC_EVENT_FLASH_CTRL_LOST | \
76 BMC_EVENT_DAEMON_READY)
77
78/* MBOX Registers */
79#define MBOX_HOST_PATH "/dev/aspeed-mbox"
80#define MBOX_HOST_TIMEOUT_SEC 1
81#define MBOX_ARGS_BYTES 11
82#define MBOX_REG_BYTES 16
83#define MBOX_HOST_EVENT 14
84#define MBOX_BMC_EVENT 15
85
86#define BLOCK_SIZE_SHIFT_V1 12 /* 4K */
87
88/* Window Dirty/Erase bytemap masks */
89#define WINDOW_CLEAN 0x00
90#define WINDOW_DIRTY 0x01
91#define WINDOW_ERASED 0x02
92
93/* Put polled file descriptors first */
94#define DBUS_FD 0
95#define MBOX_FD 1
96#define SIG_FD 2
97#define POLL_FDS 3 /* Number of FDs we poll on */
98#define LPC_CTRL_FD 3
99#define MTD_FD 4
100#define TOTAL_FDS 5
101
102#define MAPS_FLASH (1 << 0)
103#define MAPS_MEM (1 << 1)
104#define STATE_SUSPENDED (1 << 7)
105enum mbox_state {
106 /* Still Initing */
107 UNINITIALISED = 0,
108 /* Active and LPC Maps Flash */
109 ACTIVE_MAPS_FLASH = MAPS_FLASH,
110 /* Suspended and LPC Maps Flash */
111 SUSPEND_MAPS_FLASH = STATE_SUSPENDED | MAPS_FLASH,
112 /* Active and LPC Maps Memory */
113 ACTIVE_MAPS_MEM = MAPS_MEM,
114 /* Suspended and LPC Maps Memory */
115 SUSPEND_MAPS_MEM = STATE_SUSPENDED | MAPS_MEM
Cyril Bur314929b2016-10-14 15:55:16 +1100116};
117
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100118#define FLASH_OFFSET_UNINIT 0xFFFFFFFF
Cyril Bur314929b2016-10-14 15:55:16 +1100119
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100120struct window_context {
121 void *mem; /* Portion of Reserved Memory Region */
122 uint32_t flash_offset; /* Flash area the window maps (bytes) */
123 uint32_t size; /* Window Size (bytes) power-of-2 */
124 uint8_t *dirty_bmap; /* Bytemap of the dirty/erased state */
125 uint32_t age; /* Used for LRU eviction scheme */
126};
127
128struct window_list {
129 uint32_t num;
130 uint32_t max_age;
131 uint32_t default_size;
132 struct window_context *window;
133};
134
135struct mbox_context {
136/* System State */
137 enum mbox_state state;
138 enum api_version version;
139 struct pollfd fds[TOTAL_FDS];
140 sd_bus *bus;
141 bool terminate;
142 uint8_t bmc_events;
Andrew Jeffery55dede62017-04-24 16:13:06 +0930143 uint8_t prev_seq;
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100144
145/* Window State */
146 /* The window list struct containing all current "windows" */
147 struct window_list windows;
148 /* The window the host is currently pointed at */
149 struct window_context *current;
150 /* Is the current window a write one */
151 bool current_is_write;
152
153/* Memory & Flash State */
154 /* Reserved Memory Region */
155 void *mem;
156 /* Reserved Mem Size (bytes) */
157 uint32_t mem_size;
158 /* LPC Bus Base Address (bytes) */
159 uint32_t lpc_base;
160 /* Flash size from command line (bytes) */
161 uint32_t flash_size;
162 /* Bytemap of the erased state of the entire flash */
163 uint8_t *flash_bmap;
164 /* Erase size (as a shift) */
165 uint32_t erase_size_shift;
166 /* Block size (as a shift) */
167 uint32_t block_size_shift;
168 /* Actual Flash Info */
169 struct mtd_info_user mtd_info;
170};
171
172#endif /* MBOX_H */