Andrew Jeffery | 4fe996c | 2018-02-27 12:16:48 +1030 | [diff] [blame] | 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // Copyright (C) 2018 IBM Corp. |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 3 | |
| 4 | #define _GNU_SOURCE |
| 5 | #include <assert.h> |
| 6 | #include <errno.h> |
| 7 | #include <fcntl.h> |
| 8 | #include <getopt.h> |
| 9 | #include <limits.h> |
| 10 | #include <poll.h> |
| 11 | #include <stdbool.h> |
| 12 | #include <stdint.h> |
| 13 | #include <stdio.h> |
| 14 | #include <stdlib.h> |
| 15 | #include <string.h> |
| 16 | #include <syslog.h> |
| 17 | #include <signal.h> |
| 18 | #include <sys/ioctl.h> |
| 19 | #include <sys/mman.h> |
| 20 | #include <sys/stat.h> |
| 21 | #include <sys/timerfd.h> |
| 22 | #include <sys/types.h> |
| 23 | #include <time.h> |
| 24 | #include <unistd.h> |
| 25 | #include <inttypes.h> |
| 26 | |
Andrew Jeffery | 26558db | 2018-08-10 00:22:38 +0930 | [diff] [blame] | 27 | #include "mboxd.h" |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 28 | #include "common.h" |
Andrew Jeffery | cd18611 | 2018-08-08 10:47:55 +0930 | [diff] [blame] | 29 | #include "lpc.h" |
Evan Lojewski | f1e547c | 2019-03-14 14:34:33 +1030 | [diff] [blame] | 30 | #include "backend.h" |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 31 | #include <linux/aspeed-lpc-ctrl.h> |
| 32 | |
| 33 | #define LPC_CTRL_PATH "/dev/aspeed-lpc-ctrl" |
| 34 | |
Andrew Jeffery | cb9b210 | 2018-08-08 16:31:07 +0930 | [diff] [blame] | 35 | int __lpc_dev_init(struct mbox_context *context, const char *path) |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 36 | { |
| 37 | struct aspeed_lpc_ctrl_mapping map = { |
| 38 | .window_type = ASPEED_LPC_CTRL_WINDOW_MEMORY, |
| 39 | .window_id = 0, /* There's only one */ |
| 40 | .flags = 0, |
| 41 | .addr = 0, |
| 42 | .offset = 0, |
| 43 | .size = 0 |
| 44 | }; |
| 45 | int fd; |
| 46 | |
| 47 | /* Open LPC Device */ |
Suraj Jitindar Singh | 2851959 | 2017-04-27 14:48:58 +1000 | [diff] [blame] | 48 | MSG_DBG("Opening %s\n", path); |
Andrew Jeffery | 07a7684 | 2017-04-12 14:09:06 +0930 | [diff] [blame] | 49 | fd = open(path, O_RDWR | O_SYNC); |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 50 | if (fd < 0) { |
| 51 | MSG_ERR("Couldn't open %s with flags O_RDWR: %s\n", |
Andrew Jeffery | 07a7684 | 2017-04-12 14:09:06 +0930 | [diff] [blame] | 52 | path, strerror(errno)); |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 53 | return -errno; |
| 54 | } |
| 55 | |
| 56 | context->fds[LPC_CTRL_FD].fd = fd; |
| 57 | |
| 58 | /* Find Size of Reserved Memory Region */ |
Suraj Jitindar Singh | 2851959 | 2017-04-27 14:48:58 +1000 | [diff] [blame] | 59 | MSG_DBG("Getting buffer size...\n"); |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 60 | if (ioctl(fd, ASPEED_LPC_CTRL_IOCTL_GET_SIZE, &map) < 0) { |
| 61 | MSG_ERR("Couldn't get lpc control buffer size: %s\n", |
| 62 | strerror(errno)); |
| 63 | return -errno; |
| 64 | } |
| 65 | |
| 66 | context->mem_size = map.size; |
| 67 | /* Map at the top of the 28-bit LPC firmware address space-0 */ |
| 68 | context->lpc_base = 0x0FFFFFFF & -context->mem_size; |
Suraj Jitindar Singh | 2851959 | 2017-04-27 14:48:58 +1000 | [diff] [blame] | 69 | |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 70 | /* mmap the Reserved Memory Region */ |
Suraj Jitindar Singh | 2851959 | 2017-04-27 14:48:58 +1000 | [diff] [blame] | 71 | MSG_DBG("Mapping in 0x%.8x bytes of %s\n", context->mem_size, path); |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 72 | context->mem = mmap(NULL, context->mem_size, PROT_READ | PROT_WRITE, |
| 73 | MAP_SHARED, fd, 0); |
| 74 | if (context->mem == MAP_FAILED) { |
Andrew Jeffery | 093d65a | 2017-04-24 12:34:56 +0930 | [diff] [blame] | 75 | MSG_ERR("Failed to map %s: %s\n", path, strerror(errno)); |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 76 | return -errno; |
| 77 | } |
| 78 | |
| 79 | return 0; |
| 80 | } |
| 81 | |
Andrew Jeffery | cb9b210 | 2018-08-08 16:31:07 +0930 | [diff] [blame] | 82 | int lpc_dev_init(struct mbox_context *context) |
Andrew Jeffery | 07a7684 | 2017-04-12 14:09:06 +0930 | [diff] [blame] | 83 | { |
Andrew Jeffery | cb9b210 | 2018-08-08 16:31:07 +0930 | [diff] [blame] | 84 | return __lpc_dev_init(context, LPC_CTRL_PATH); |
Andrew Jeffery | 07a7684 | 2017-04-12 14:09:06 +0930 | [diff] [blame] | 85 | } |
| 86 | |
Andrew Jeffery | 2e2df28 | 2018-08-08 16:32:22 +0930 | [diff] [blame] | 87 | void lpc_dev_free(struct mbox_context *context) |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 88 | { |
| 89 | if (context->mem) { |
| 90 | munmap(context->mem, context->mem_size); |
| 91 | } |
| 92 | close(context->fds[LPC_CTRL_FD].fd); |
| 93 | } |
| 94 | |
| 95 | /* |
Andrew Jeffery | ec0f230 | 2018-08-08 16:33:27 +0930 | [diff] [blame] | 96 | * lpc_map_flash() - Point the lpc bus mapping to the actual flash device |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 97 | * @context: The mbox context pointer |
| 98 | * |
| 99 | * Return: 0 on success otherwise negative error code |
| 100 | */ |
Andrew Jeffery | ec0f230 | 2018-08-08 16:33:27 +0930 | [diff] [blame] | 101 | int lpc_map_flash(struct mbox_context *context) |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 102 | { |
| 103 | struct aspeed_lpc_ctrl_mapping map = { |
| 104 | .window_type = ASPEED_LPC_CTRL_WINDOW_FLASH, |
| 105 | .window_id = 0, /* Theres only one */ |
| 106 | .flags = 0, |
| 107 | /* |
| 108 | * The mask is because the top nibble is the host LPC FW space, |
| 109 | * we want space 0. |
| 110 | */ |
Evan Lojewski | f1e547c | 2019-03-14 14:34:33 +1030 | [diff] [blame] | 111 | .addr = 0x0FFFFFFF & -context->backend.flash_size, |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 112 | .offset = 0, |
Evan Lojewski | f1e547c | 2019-03-14 14:34:33 +1030 | [diff] [blame] | 113 | .size = context->backend.flash_size |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 114 | }; |
| 115 | |
| 116 | if (context->state & MAPS_FLASH) { |
| 117 | return 0; /* LPC Bus already points to flash */ |
| 118 | } |
| 119 | /* Don't let the host access flash while we're suspended */ |
| 120 | if (context->state & STATE_SUSPENDED) { |
| 121 | MSG_ERR("Can't point lpc mapping to flash while suspended\n"); |
Andrew Jeffery | 8eab215 | 2018-08-09 23:47:29 +0930 | [diff] [blame] | 122 | return -EBUSY; |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 123 | } |
| 124 | |
Suraj Jitindar Singh | 2851959 | 2017-04-27 14:48:58 +1000 | [diff] [blame] | 125 | MSG_INFO("Pointing HOST LPC bus at the flash\n"); |
| 126 | MSG_INFO("Assuming %dMB of flash: HOST LPC 0x%08x\n", |
Evan Lojewski | f1e547c | 2019-03-14 14:34:33 +1030 | [diff] [blame] | 127 | context->backend.flash_size >> 20, map.addr); |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 128 | |
| 129 | if (ioctl(context->fds[LPC_CTRL_FD].fd, ASPEED_LPC_CTRL_IOCTL_MAP, &map) |
| 130 | == -1) { |
| 131 | MSG_ERR("Failed to point the LPC BUS at the actual flash: %s\n", |
| 132 | strerror(errno)); |
Andrew Jeffery | 8eab215 | 2018-08-09 23:47:29 +0930 | [diff] [blame] | 133 | return -errno; |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | context->state = ACTIVE_MAPS_FLASH; |
| 137 | /* |
| 138 | * Since the host now has access to the flash it can change it out from |
| 139 | * under us |
| 140 | */ |
Andrew Jeffery | 0297e5b | 2019-03-14 16:36:27 +1030 | [diff] [blame] | 141 | return backend_set_bytemap(&context->backend, 0, |
| 142 | context->backend.flash_size, FLASH_DIRTY); |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | /* |
Andrew Jeffery | 17079d1 | 2018-08-08 16:35:09 +0930 | [diff] [blame] | 146 | * lpc_map_memory() - Point the lpc bus mapping to the reserved memory region |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 147 | * @context: The mbox context pointer |
| 148 | * |
| 149 | * Return: 0 on success otherwise negative error code |
| 150 | */ |
Andrew Jeffery | 17079d1 | 2018-08-08 16:35:09 +0930 | [diff] [blame] | 151 | int lpc_map_memory(struct mbox_context *context) |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 152 | { |
| 153 | struct aspeed_lpc_ctrl_mapping map = { |
| 154 | .window_type = ASPEED_LPC_CTRL_WINDOW_MEMORY, |
| 155 | .window_id = 0, /* There's only one */ |
| 156 | .flags = 0, |
| 157 | .addr = context->lpc_base, |
| 158 | .offset = 0, |
| 159 | .size = context->mem_size |
| 160 | }; |
| 161 | |
| 162 | if (context->state & MAPS_MEM) { |
| 163 | return 0; /* LPC Bus already points to reserved memory area */ |
| 164 | } |
| 165 | |
Suraj Jitindar Singh | 2851959 | 2017-04-27 14:48:58 +1000 | [diff] [blame] | 166 | MSG_INFO("Pointing HOST LPC bus at memory region %p of size 0x%.8x\n", |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 167 | context->mem, context->mem_size); |
Suraj Jitindar Singh | 2851959 | 2017-04-27 14:48:58 +1000 | [diff] [blame] | 168 | MSG_INFO("LPC address 0x%.8x\n", map.addr); |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 169 | |
| 170 | if (ioctl(context->fds[LPC_CTRL_FD].fd, ASPEED_LPC_CTRL_IOCTL_MAP, |
| 171 | &map)) { |
| 172 | MSG_ERR("Failed to point the LPC BUS to memory: %s\n", |
| 173 | strerror(errno)); |
Andrew Jeffery | 1e531af | 2018-08-07 13:32:57 +0930 | [diff] [blame] | 174 | return -errno; |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | /* LPC now maps memory (keep suspended state) */ |
| 178 | context->state = MAPS_MEM | (context->state & STATE_SUSPENDED); |
| 179 | |
| 180 | return 0; |
| 181 | } |