blob: d920cc7da630f101e95f915d15d07e8641a884cf [file] [log] [blame]
Cyril Burc85e34d2016-11-15 11:50:41 +11001/* Copyright 2016 IBM
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 *
15 */
16
17#include <assert.h>
18#include <errno.h>
19#include <fcntl.h>
20#include <getopt.h>
21#include <limits.h>
22#include <poll.h>
23#include <stdbool.h>
24#include <stdint.h>
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28#include <syslog.h>
29#include <sys/ioctl.h>
30#include <sys/mman.h>
31#include <sys/stat.h>
32#include <sys/timerfd.h>
33#include <sys/types.h>
34#include <time.h>
35#include <unistd.h>
Andrew Jeffery78210b92017-01-13 13:06:09 +103036#include <inttypes.h>
Cyril Burc85e34d2016-11-15 11:50:41 +110037
38#include <mtd/mtd-abi.h>
39
40#include <linux/aspeed-lpc-ctrl.h>
41
42#include "mbox.h"
43#include "common.h"
44
45#define LPC_CTRL_PATH "/dev/aspeed-lpc-ctrl"
46
47#define MBOX_FD 0
48#define LPC_CTRL_FD 1
49#define MTD_FD 2
50#define TOTAL_FDS 3
51
52#define ALIGN_UP(_v, _a) (((_v) + (_a) - 1) & ~((_a) - 1))
53
54#define MSG_OUT(f_, ...) do { if (verbosity != MBOX_LOG_NONE) { mbox_log(LOG_INFO, f_, ##__VA_ARGS__); } } while(0)
55#define MSG_ERR(f_, ...) do { if (verbosity != MBOX_LOG_NONE) { mbox_log(LOG_ERR, f_, ##__VA_ARGS__); } } while(0)
56
57#define BOOT_HICR7 0x30000e00U
58#define BOOT_HICR8 0xfe0001ffU
59
60struct mbox_context {
61 struct pollfd fds[TOTAL_FDS];
62 void *lpc_mem;
63 uint32_t base;
64 uint32_t size;
65 uint32_t pgsize;
66 bool dirty;
67 uint32_t dirtybase;
68 uint32_t dirtysize;
69 struct mtd_info_user mtd_info;
70};
71
72static int running = 1;
73
Cyril Bur46233672017-01-16 13:33:26 +110074static int point_to_flash(struct mbox_context *context)
Cyril Burc85e34d2016-11-15 11:50:41 +110075{
Cyril Bur46233672017-01-16 13:33:26 +110076 struct aspeed_lpc_ctrl_mapping map;
77 int r = 0;
78
Cyril Burc85e34d2016-11-15 11:50:41 +110079 /*
Cyril Bur46233672017-01-16 13:33:26 +110080 * Point it to the real flash for sanity.
Cyril Burc85e34d2016-11-15 11:50:41 +110081 *
Cyril Bur46233672017-01-16 13:33:26 +110082 * This function assumes 32MB of flash which means that that
83 * hostboot expects flash to be at 0x0e000000 - 0x0fffffff on the
84 * LPC bus. If the machine actually has 64MB of flash then the
85 * map.addr should be 0x0c000000. TODO
Cyril Burc85e34d2016-11-15 11:50:41 +110086 *
87 * Until hostboot learns how to talk to this daemon this hardcode will
88 * get hostboot going. Furthermore, when hostboot does learn to talk
89 * then this mapping is unnecessary and this code should be removed.
90 */
91
Cyril Bur46233672017-01-16 13:33:26 +110092 map.addr = 0x0e000000;
93 map.size = 0x02000000; /* 32MB */
94 map.offset = 0;
95 map.window_type = ASPEED_LPC_CTRL_WINDOW_FLASH;
96 map.window_id = 0; /* Theres only one */
Cyril Burc85e34d2016-11-15 11:50:41 +110097
98 MSG_OUT("Pointing HOST LPC bus at the actual flash\n");
Cyril Bur46233672017-01-16 13:33:26 +110099 MSG_OUT("Assuming 32MB of flash: HOST LPC 0x%08x\n", map.addr);
Cyril Burc85e34d2016-11-15 11:50:41 +1100100
Cyril Bur46233672017-01-16 13:33:26 +1100101 if (ioctl(-context->fds[LPC_CTRL_FD].fd, ASPEED_LPC_CTRL_IOCTL_MAP, &map) == -1) {
102 r = -errno;
103 MSG_ERR("Couldn't MAP the host LPC bus to the platform flash\n");
104 }
105
Cyril Burc85e34d2016-11-15 11:50:41 +1100106 return r;
107}
108
109static int flash_write(struct mbox_context *context, uint32_t pos, uint32_t len)
110{
111 int rc;
112 struct erase_info_user erase_info = {
113 .start = pos,
114 };
115
116 assert(context);
117
118 erase_info.length = ALIGN_UP(len, context->mtd_info.erasesize);
119
120 MSG_OUT("Erasing 0x%08x for 0x%08x (aligned: 0x%08x)\n", pos, len, erase_info.length);
121 if (ioctl(-context->fds[MTD_FD].fd, MEMERASE, &erase_info) == -1) {
122 MSG_ERR("Couldn't MEMERASE ioctl, flash write lost: %s\n", strerror(errno));
123 return -1;
124 }
125
126 if (lseek(-context->fds[MTD_FD].fd, pos, SEEK_SET) == (off_t) -1) {
127 MSG_ERR("Couldn't seek to 0x%08x into MTD, flash write lost: %s\n", pos, strerror(errno));
128 return -1;
129 }
130
131 while (erase_info.length) {
132 rc = write(-context->fds[MTD_FD].fd, context->lpc_mem + pos, erase_info.length);
133 if (rc == -1) {
134 MSG_ERR("Couldn't write to flash! Flash write lost: %s\n", strerror(errno));
135 return -1;
136 }
137 erase_info.length -= rc;
138 pos += rc;
139 }
140
141 return 0;
142}
143
144/* TODO: Add come consistency around the daemon exiting and either
145 * way, ensuring it responds.
146 * I'm in favour of an approach where it does its best to stay alive
147 * and keep talking, the hacky prototype was written the other way.
148 * This function is now inconsistent
149 */
150static int dispatch_mbox(struct mbox_context *context)
151{
152 int r = 0;
153 int len;
154 off_t pos;
155 uint8_t byte;
156 union mbox_regs resp, req = { 0 };
157 uint16_t sizepg, basepg, dirtypg;
158 uint32_t dirtycount;
159 struct aspeed_lpc_ctrl_mapping map;
160
161 assert(context);
162
163 map.addr = context->base;
164 map.size = context->size;
165 map.offset = 0;
166 map.window_type = ASPEED_LPC_CTRL_WINDOW_MEMORY;
167 map.window_id = 0; /* Theres only one */
168
169 MSG_OUT("Dispatched to mbox\n");
170 r = read(context->fds[MBOX_FD].fd, &req, sizeof(req.raw));
171 if (r < 0) {
172 r = -errno;
173 MSG_ERR("Couldn't read: %s\n", strerror(errno));
174 goto out;
175 }
176 if (r < sizeof(req.msg)) {
177 MSG_ERR("Short read: %d expecting %zu\n", r, sizeof(req.msg));
178 r = -1;
179 goto out;
180 }
181
182 /* We are NOT going to update the last two 'status' bytes */
183 memcpy(&resp, &req, sizeof(req.msg));
184
185 sizepg = context->size >> context->pgsize;
186 basepg = context->base >> context->pgsize;
187 MSG_OUT("Got data in with command %d\n", req.msg.command);
188 switch (req.msg.command) {
189 case MBOX_C_RESET_STATE:
190 /* Called by early hostboot? TODO */
191 resp.msg.response = MBOX_R_SUCCESS;
Cyril Bur46233672017-01-16 13:33:26 +1100192 r = point_to_flash(context);
Cyril Burc85e34d2016-11-15 11:50:41 +1100193 if (r) {
194 resp.msg.response = MBOX_R_SYSTEM_ERROR;
195 MSG_ERR("Couldn't point the LPC BUS back to actual flash\n");
196 }
197 break;
198 case MBOX_C_GET_MBOX_INFO:
199 /* TODO Freak if data.data[0] isn't 1 */
200 resp.msg.data[0] = 1;
201 put_u16(&resp.msg.data[1], sizepg);
202 put_u16(&resp.msg.data[3], sizepg);
203 resp.msg.response = MBOX_R_SUCCESS;
204 /* Wow that can't stay negated thats horrible */
205 MSG_OUT("LPC_CTRL_IOCTL_MAP to 0x%08x for 0x%08x\n", map.addr, map.size);
206 r = ioctl(-context->fds[LPC_CTRL_FD].fd,
207 ASPEED_LPC_CTRL_IOCTL_MAP, &map);
208 if (r < 0) {
209 r = -errno;
210 resp.msg.response = MBOX_R_SYSTEM_ERROR;
211 MSG_ERR("Couldn't MAP ioctl(): %s\n", strerror(errno));
212 }
213 break;
214 case MBOX_C_GET_FLASH_INFO:
215 put_u32(&resp.msg.data[0], context->mtd_info.size);
216 put_u32(&resp.msg.data[4], context->mtd_info.erasesize);
217 resp.msg.response = MBOX_R_SUCCESS;
218 break;
219 case MBOX_C_READ_WINDOW:
220 /*
221 * We could probably play tricks with LPC mapping.
222 * That would require kernel involvement.
223 * We could also always copy the relevant flash part to
224 * context->base even if it turns out that offset is in
225 * the window...
226 * This approach is easiest.
227 */
Andrew Jeffery78210b92017-01-13 13:06:09 +1030228 if (context->dirty) {
229 r = read(-context->fds[MTD_FD].fd, context->lpc_mem, context->size);
230 if (r != context->size) {
231 MSG_ERR("Short read: %d expecting %"PRIu32"\n", r, context->size);
232 goto out;
233 }
234 }
Cyril Burc85e34d2016-11-15 11:50:41 +1100235 basepg += get_u16(&req.msg.data[0]);
236 put_u16(&resp.msg.data[0], basepg);
237 resp.msg.response = MBOX_R_SUCCESS;
238 context->dirty = false;
239 break;
240 case MBOX_C_CLOSE_WINDOW:
241 context->dirty = true;
242 break;
243 case MBOX_C_WRITE_WINDOW:
244 basepg += get_u16(&req.msg.data[0]);
245 put_u16(&resp.msg.data[0], basepg);
246 resp.msg.response = MBOX_R_SUCCESS;
247 context->dirtybase = basepg << context->pgsize;
248 break;
249 /* Optimise these later */
250 case MBOX_C_WRITE_DIRTY:
251 case MBOX_C_WRITE_FENCE:
252 dirtypg = get_u16(&req.msg.data[0]);
253 dirtycount = get_u32(&req.msg.data[2]);
254 if (dirtycount == 0) {
255 resp.msg.response = MBOX_R_PARAM_ERROR;
256 break;
257 }
258 /*
259 * dirtypg is actually offset within window so we probs
260 * need to know if the window isn't at zero
261 */
262 if (flash_write(context, dirtypg << context->pgsize, dirtycount) != 0) {
263 resp.msg.response = MBOX_R_WRITE_ERROR;
264 break;
265 }
266 resp.msg.response = MBOX_R_SUCCESS;
267 break;
268 case MBOX_C_ACK:
269 resp.msg.response = MBOX_R_SUCCESS;
270 pos = lseek(context->fds[MBOX_FD].fd, MBOX_BMC_BYTE, SEEK_SET);
271 if (pos != MBOX_BMC_BYTE) {
272 r = -errno;
273 MSG_ERR("Couldn't lseek() to byte %d: %s\n", MBOX_BMC_BYTE,
274 strerror(errno));
275 }
276 /*
277 * NAND what is in the hardware and the request.
278 * This prevents the host being able to SET bits, it can
279 * only request set ones be cleared.
280 */
281 byte = ~(req.msg.data[0] & req.raw[MBOX_BMC_BYTE]);
282 len = write(context->fds[MBOX_FD].fd, &byte, 1);
283 if (len != 1) {
284 r = -errno;
285 MSG_ERR("Couldn't write to BMC status reg: %s\n",
286 strerror(errno));
287 }
288 pos = lseek(context->fds[MBOX_FD].fd, 0, SEEK_SET);
289 if (pos != 0) {
290 r = -errno;
291 MSG_ERR("Couldn't reset MBOX offset to zero\n");
292 }
293 break;
294 case MBOX_C_COMPLETED_COMMANDS:
295 /* This implementation always completes before responding */
296 resp.msg.data[0] = 0;
297 resp.msg.response = MBOX_R_SUCCESS;
298 break;
299 default:
300 MSG_ERR("UNKNOWN MBOX COMMAND\n");
301 resp.msg.response = MBOX_R_PARAM_ERROR;
302 r = -1;
303 }
304
305 MSG_OUT("Writing response to MBOX regs\n");
306 len = write(context->fds[MBOX_FD].fd, &resp, sizeof(resp.msg));
307 if (len < sizeof(resp.msg)) {
308 r = -errno;
309 MSG_ERR("Didn't write the full response\n");
310 }
311
312out:
313 return r;
314}
315
316static void usage(const char *name)
317{
318 fprintf(stderr, "Usage %s [ -v[v] | --syslog ]\n", name);
319 fprintf(stderr, "\t--verbose\t Be [more] verbose\n");
320 fprintf(stderr, "\t--syslog\t Log output to syslog (pointless without -v)\n\n");
321}
322
323int main(int argc, char *argv[])
324{
325 struct mbox_context *context;
326 const char *name = argv[0];
327 char *pnor_filename = NULL;
328 int opt, polled, r, i;
329 struct aspeed_lpc_ctrl_mapping map;
330
331 static const struct option long_options[] = {
332 { "verbose", no_argument, 0, 'v' },
333 { "syslog", no_argument, 0, 's' },
Andrew Jefferyd7a15b82017-01-13 13:08:59 +1030334 { 0, 0, 0, 0 }
Cyril Burc85e34d2016-11-15 11:50:41 +1100335 };
336
337 context = calloc(1, sizeof(*context));
338 for (i = 0; i < TOTAL_FDS; i++)
339 context->fds[i].fd = -1;
340
341 mbox_vlog = &mbox_log_console;
342 while ((opt = getopt_long(argc, argv, "v", long_options, NULL)) != -1) {
343 switch (opt) {
344 case 0:
345 break;
346 case 'v':
347 verbosity++;
348 break;
349 case 's':
350 /* Avoid a double openlog() */
351 if (mbox_vlog != &vsyslog) {
352 openlog(PREFIX, LOG_ODELAY, LOG_DAEMON);
353 mbox_vlog = &vsyslog;
354 }
355 break;
356 default:
357 usage(name);
358 exit(EXIT_FAILURE);
359 }
360 }
361
362 if (verbosity == MBOX_LOG_VERBOSE)
363 MSG_OUT("Verbose logging\n");
364
365 if (verbosity == MBOX_LOG_DEBUG)
366 MSG_OUT("Debug logging\n");
367
368 MSG_OUT("Starting\n");
369
370 MSG_OUT("Opening %s\n", MBOX_HOST_PATH);
371 context->fds[MBOX_FD].fd = open(MBOX_HOST_PATH, O_RDWR | O_NONBLOCK);
372 if (context->fds[MBOX_FD].fd < 0) {
373 r = -errno;
374 MSG_ERR("Couldn't open %s with flags O_RDWR: %s\n",
375 MBOX_HOST_PATH, strerror(errno));
376 goto finish;
377 }
378
379 MSG_OUT("Opening %s\n", LPC_CTRL_PATH);
380 context->fds[LPC_CTRL_FD].fd = open(LPC_CTRL_PATH, O_RDWR | O_SYNC);
381 if (context->fds[LPC_CTRL_FD].fd < 0) {
382 r = -errno;
383 MSG_ERR("Couldn't open %s with flags O_RDWR: %s\n",
384 LPC_CTRL_PATH, strerror(errno));
385 goto finish;
386 }
387
388 MSG_OUT("Getting buffer size...\n");
389 /* This may become more variable in the future */
390 context->pgsize = 12; /* 4K */
391 map.window_type = ASPEED_LPC_CTRL_WINDOW_MEMORY;
392 map.window_id = 0; /* Theres only one */
393 if (ioctl(context->fds[LPC_CTRL_FD].fd, ASPEED_LPC_CTRL_IOCTL_GET_SIZE,
394 &map) < 0) {
395 r = -errno;
396 MSG_OUT("fail\n");
397 MSG_ERR("Couldn't get lpc control buffer size: %s\n", strerror(-r));
398 goto finish;
399 }
400 /* And strip the first nibble, LPC access speciality */
401 context->size = map.size;
402 context->base = -context->size & 0x0FFFFFFF;
403
404 /* READ THE COMMENT AT THE START OF THIS FUNCTION! */
Cyril Bur46233672017-01-16 13:33:26 +1100405 r = point_to_flash(context);
Cyril Burc85e34d2016-11-15 11:50:41 +1100406 if (r) {
407 MSG_ERR("Failed to point the LPC BUS at the actual flash: %s\n",
408 strerror(-r));
409 goto finish;
410 }
411
412 MSG_OUT("Mapping %s for %u\n", LPC_CTRL_PATH, context->size);
413 context->lpc_mem = mmap(NULL, context->size, PROT_READ | PROT_WRITE, MAP_SHARED,
414 context->fds[LPC_CTRL_FD].fd, 0);
415 if (context->lpc_mem == MAP_FAILED) {
416 r = -errno;
417 MSG_ERR("Didn't manage to mmap %s: %s\n", LPC_CTRL_PATH, strerror(errno));
418 goto finish;
419 }
420
421 pnor_filename = get_dev_mtd();
422 if (!pnor_filename) {
423 MSG_ERR("Couldn't find the PNOR /dev/mtd partition\n");
424 r = -1;
425 goto finish;
426 }
427
428 MSG_OUT("Opening %s\n", pnor_filename);
429 context->fds[MTD_FD].fd = open(pnor_filename, O_RDWR);
430 if (context->fds[MTD_FD].fd < 0) {
431 r = -errno;
432 MSG_ERR("Couldn't open %s with flags O_RDWR: %s\n",
433 pnor_filename, strerror(errno));
434 goto finish;
435 }
436
437 if (ioctl(context->fds[MTD_FD].fd, MEMGETINFO, &context->mtd_info) == -1) {
438 MSG_ERR("Couldn't get information about MTD: %s\n", strerror(errno));
439 return -1;
440 }
441
442 /*
443 * Copy flash into RAM early, same time.
444 * The kernel has created the LPC->AHB mapping also, which means
445 * flash should work.
446 * Ideally we tell the kernel whats up and when to do stuff...
447 */
448 MSG_OUT("Loading flash into ram at %p for 0x%08x bytes\n",
449 context->lpc_mem, context->size);
450 r = read(context->fds[MTD_FD].fd, context->lpc_mem, context->size);
451 if (r != context->size) {
452 MSG_ERR("Couldn't copy mtd into ram: %d\n", r);
453 goto finish;
454 }
455
456 context->fds[MBOX_FD].events = POLLIN;
457 /* Ignore in poll() */
458 context->fds[LPC_CTRL_FD].fd = -context->fds[LPC_CTRL_FD].fd;
459 context->fds[MTD_FD].fd = -context->fds[MTD_FD].fd;
460
Cyril Burd8f6d7a2017-01-10 18:11:16 +1100461 /* Test the single write facility by setting all the regs to 0xFF */
462 MSG_OUT("Setting all MBOX regs to 0xff individually...\n");
463 for (i = 0; i < MBOX_REG_BYTES; i++) {
464 uint8_t byte = 0xff;
465 off_t pos;
466 int len;
467
468 pos = lseek(context->fds[MBOX_FD].fd, i, SEEK_SET);
469 if (pos != i) {
470 MSG_ERR("Couldn't lseek() to byte %d: %s\n", i,
471 strerror(errno));
472 break;
473 }
474 len = write(context->fds[MBOX_FD].fd, &byte, 1);
475 if (len != 1) {
476 MSG_ERR("Couldn't write MBOX reg %d: %s\n", i,
477 strerror(errno));
478 break;
479 }
480 }
481 if (lseek(context->fds[MBOX_FD].fd, 0, SEEK_SET) != 0) {
482 r = -errno;
483 MSG_ERR("Couldn't reset MBOX pos to zero\n");
484 goto finish;
485 }
486
Cyril Burc85e34d2016-11-15 11:50:41 +1100487 MSG_OUT("Entering polling loop\n");
488 while (running) {
489 polled = poll(context->fds, TOTAL_FDS, 1000);
490 if (polled == 0)
491 continue;
492 if (polled < 0) {
493 r = -errno;
494 MSG_ERR("Error from poll(): %s\n", strerror(errno));
495 break;
496 }
497 r = dispatch_mbox(context);
498 if (r < 0) {
499 MSG_ERR("Error handling MBOX event: %s\n", strerror(-r));
500 break;
501 }
502 }
503
504 MSG_OUT("Exiting\n");
505
506 /* Unnegate so we can close it */
507 context->fds[LPC_CTRL_FD].fd = -context->fds[LPC_CTRL_FD].fd;
508 context->fds[MTD_FD].fd = -context->fds[MTD_FD].fd;
509
510finish:
511 if (context->lpc_mem)
512 munmap(context->lpc_mem, context->size);
513
514 free(pnor_filename);
515 close(context->fds[MTD_FD].fd);
516 close(context->fds[LPC_CTRL_FD].fd);
517 close(context->fds[MBOX_FD].fd);
518 free(context);
519
520 return r;
521}
522