blob: 67d56227f0917cabe033766cce1ad51c0c915d3a [file] [log] [blame]
Andrew Jeffery4fe996c2018-02-27 12:16:48 +10301/* SPDX-License-Identifier: Apache-2.0 */
2/* Copyright (C) 2018 IBM Corp. */
Cyril Bur314929b2016-10-14 15:55:16 +11003
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +11004#ifndef MBOX_H
5#define MBOX_H
Cyril Bur314929b2016-10-14 15:55:16 +11006
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +11007#include <mtd/mtd-abi.h>
8#include <systemd/sd-bus.h>
Andrew Jeffery8ecbdb52017-04-10 16:26:08 +09309#include <poll.h>
10#include <stdbool.h>
Andrew Jeffery53c21aa2018-03-26 11:56:16 +103011#include "vpnor/mboxd_pnor_partition_table.h"
Cyril Bur314929b2016-10-14 15:55:16 +110012
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +110013enum api_version {
14 API_VERSION_INVAL = 0,
15 API_VERSION_1 = 1,
16 API_VERSION_2 = 2
Cyril Bur314929b2016-10-14 15:55:16 +110017};
18
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +110019#define API_MIN_VERSION API_VERSION_1
20#define API_MAX_VERSION API_VERSION_2
21
22#define THIS_NAME "Mailbox Daemon"
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +110023
24/* Command Values */
25#define MBOX_C_RESET_STATE 0x01
26#define MBOX_C_GET_MBOX_INFO 0x02
27#define MBOX_C_GET_FLASH_INFO 0x03
28#define MBOX_C_READ_WINDOW 0x04
29#define MBOX_C_CLOSE_WINDOW 0x05
30#define MBOX_C_WRITE_WINDOW 0x06
31#define MBOX_C_WRITE_DIRTY 0x07
32#define MBOX_C_WRITE_FLUSH 0x08
33#define MBOX_C_ACK 0x09
34#define MBOX_C_WRITE_ERASE 0x0a
35#define NUM_MBOX_CMDS MBOX_C_WRITE_ERASE
36
37/* Response Values */
38#define MBOX_R_SUCCESS 0x01
39#define MBOX_R_PARAM_ERROR 0x02
40#define MBOX_R_WRITE_ERROR 0x03
41#define MBOX_R_SYSTEM_ERROR 0x04
42#define MBOX_R_TIMEOUT 0x05
43#define MBOX_R_BUSY 0x06
44#define MBOX_R_WINDOW_ERROR 0x07
Andrew Jeffery55dede62017-04-24 16:13:06 +093045#define MBOX_R_SEQ_ERROR 0x08
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +110046
47/* Argument Flags */
48#define FLAGS_NONE 0x00
49#define FLAGS_SHORT_LIFETIME 0x01
50
51/* BMC Event Notification */
52#define BMC_EVENT_REBOOT 0x01
53#define BMC_EVENT_WINDOW_RESET 0x02
54#define BMC_EVENT_ACK_MASK (BMC_EVENT_REBOOT | \
55 BMC_EVENT_WINDOW_RESET)
56#define BMC_EVENT_FLASH_CTRL_LOST 0x40
57#define BMC_EVENT_DAEMON_READY 0x80
58#define BMC_EVENT_V1_MASK BMC_EVENT_REBOOT
59#define BMC_EVENT_V2_MASK (BMC_EVENT_REBOOT | \
60 BMC_EVENT_WINDOW_RESET | \
61 BMC_EVENT_FLASH_CTRL_LOST | \
62 BMC_EVENT_DAEMON_READY)
63
64/* MBOX Registers */
65#define MBOX_HOST_PATH "/dev/aspeed-mbox"
66#define MBOX_HOST_TIMEOUT_SEC 1
67#define MBOX_ARGS_BYTES 11
68#define MBOX_REG_BYTES 16
69#define MBOX_HOST_EVENT 14
70#define MBOX_BMC_EVENT 15
71
72#define BLOCK_SIZE_SHIFT_V1 12 /* 4K */
73
74/* Window Dirty/Erase bytemap masks */
75#define WINDOW_CLEAN 0x00
76#define WINDOW_DIRTY 0x01
77#define WINDOW_ERASED 0x02
78
79/* Put polled file descriptors first */
80#define DBUS_FD 0
81#define MBOX_FD 1
82#define SIG_FD 2
83#define POLL_FDS 3 /* Number of FDs we poll on */
84#define LPC_CTRL_FD 3
85#define MTD_FD 4
86#define TOTAL_FDS 5
87
88#define MAPS_FLASH (1 << 0)
89#define MAPS_MEM (1 << 1)
90#define STATE_SUSPENDED (1 << 7)
91enum mbox_state {
92 /* Still Initing */
93 UNINITIALISED = 0,
94 /* Active and LPC Maps Flash */
95 ACTIVE_MAPS_FLASH = MAPS_FLASH,
96 /* Suspended and LPC Maps Flash */
97 SUSPEND_MAPS_FLASH = STATE_SUSPENDED | MAPS_FLASH,
98 /* Active and LPC Maps Memory */
99 ACTIVE_MAPS_MEM = MAPS_MEM,
100 /* Suspended and LPC Maps Memory */
101 SUSPEND_MAPS_MEM = STATE_SUSPENDED | MAPS_MEM
Cyril Bur314929b2016-10-14 15:55:16 +1100102};
103
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100104#define FLASH_OFFSET_UNINIT 0xFFFFFFFF
Cyril Bur314929b2016-10-14 15:55:16 +1100105
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100106struct window_context {
107 void *mem; /* Portion of Reserved Memory Region */
108 uint32_t flash_offset; /* Flash area the window maps (bytes) */
109 uint32_t size; /* Window Size (bytes) power-of-2 */
110 uint8_t *dirty_bmap; /* Bytemap of the dirty/erased state */
111 uint32_t age; /* Used for LRU eviction scheme */
112};
113
114struct window_list {
115 uint32_t num;
116 uint32_t max_age;
117 uint32_t default_size;
118 struct window_context *window;
119};
120
121struct mbox_context {
122/* System State */
123 enum mbox_state state;
124 enum api_version version;
125 struct pollfd fds[TOTAL_FDS];
126 sd_bus *bus;
127 bool terminate;
128 uint8_t bmc_events;
Andrew Jeffery55dede62017-04-24 16:13:06 +0930129 uint8_t prev_seq;
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100130
131/* Window State */
132 /* The window list struct containing all current "windows" */
133 struct window_list windows;
134 /* The window the host is currently pointed at */
135 struct window_context *current;
136 /* Is the current window a write one */
137 bool current_is_write;
138
139/* Memory & Flash State */
140 /* Reserved Memory Region */
141 void *mem;
142 /* Reserved Mem Size (bytes) */
143 uint32_t mem_size;
144 /* LPC Bus Base Address (bytes) */
145 uint32_t lpc_base;
146 /* Flash size from command line (bytes) */
147 uint32_t flash_size;
148 /* Bytemap of the erased state of the entire flash */
149 uint8_t *flash_bmap;
150 /* Erase size (as a shift) */
151 uint32_t erase_size_shift;
152 /* Block size (as a shift) */
153 uint32_t block_size_shift;
154 /* Actual Flash Info */
155 struct mtd_info_user mtd_info;
Deepak Kodihallib6a446f2017-04-29 13:01:49 -0500156#ifdef VIRTUAL_PNOR_ENABLED
157 /* Virtual PNOR partition table */
158 struct vpnor_partition_table *vpnor;
Ratan Gupta8441a392017-05-05 21:42:53 +0530159 struct vpnor_partition_paths paths;
Deepak Kodihallib6a446f2017-04-29 13:01:49 -0500160#endif
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100161};
162
163#endif /* MBOX_H */