blob: 0529b14073176005c160e7d72396580f170e7146 [file] [log] [blame]
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +11001/*
2 * Mailbox Daemon LPC Helpers
3 *
4 * Copyright 2016 IBM
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 */
19
20#define _GNU_SOURCE
21#include <assert.h>
22#include <errno.h>
23#include <fcntl.h>
24#include <getopt.h>
25#include <limits.h>
26#include <poll.h>
27#include <stdbool.h>
28#include <stdint.h>
29#include <stdio.h>
30#include <stdlib.h>
31#include <string.h>
32#include <syslog.h>
33#include <signal.h>
34#include <sys/ioctl.h>
35#include <sys/mman.h>
36#include <sys/stat.h>
37#include <sys/timerfd.h>
38#include <sys/types.h>
39#include <time.h>
40#include <unistd.h>
41#include <inttypes.h>
42
43#include "mbox.h"
44#include "common.h"
45#include "mboxd_lpc.h"
46#include "mboxd_flash.h"
47#include <linux/aspeed-lpc-ctrl.h>
48
49#define LPC_CTRL_PATH "/dev/aspeed-lpc-ctrl"
50
Andrew Jeffery07a76842017-04-12 14:09:06 +093051int __init_lpc_dev(struct mbox_context *context, const char *path)
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +110052{
53 struct aspeed_lpc_ctrl_mapping map = {
54 .window_type = ASPEED_LPC_CTRL_WINDOW_MEMORY,
55 .window_id = 0, /* There's only one */
56 .flags = 0,
57 .addr = 0,
58 .offset = 0,
59 .size = 0
60 };
61 int fd;
62
63 /* Open LPC Device */
Suraj Jitindar Singh28519592017-04-27 14:48:58 +100064 MSG_DBG("Opening %s\n", path);
Andrew Jeffery07a76842017-04-12 14:09:06 +093065 fd = open(path, O_RDWR | O_SYNC);
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +110066 if (fd < 0) {
67 MSG_ERR("Couldn't open %s with flags O_RDWR: %s\n",
Andrew Jeffery07a76842017-04-12 14:09:06 +093068 path, strerror(errno));
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +110069 return -errno;
70 }
71
72 context->fds[LPC_CTRL_FD].fd = fd;
73
74 /* Find Size of Reserved Memory Region */
Suraj Jitindar Singh28519592017-04-27 14:48:58 +100075 MSG_DBG("Getting buffer size...\n");
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +110076 if (ioctl(fd, ASPEED_LPC_CTRL_IOCTL_GET_SIZE, &map) < 0) {
77 MSG_ERR("Couldn't get lpc control buffer size: %s\n",
78 strerror(errno));
79 return -errno;
80 }
81
82 context->mem_size = map.size;
83 /* Map at the top of the 28-bit LPC firmware address space-0 */
84 context->lpc_base = 0x0FFFFFFF & -context->mem_size;
Suraj Jitindar Singh28519592017-04-27 14:48:58 +100085
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +110086 /* mmap the Reserved Memory Region */
Suraj Jitindar Singh28519592017-04-27 14:48:58 +100087 MSG_DBG("Mapping in 0x%.8x bytes of %s\n", context->mem_size, path);
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +110088 context->mem = mmap(NULL, context->mem_size, PROT_READ | PROT_WRITE,
89 MAP_SHARED, fd, 0);
90 if (context->mem == MAP_FAILED) {
Andrew Jeffery093d65a2017-04-24 12:34:56 +093091 MSG_ERR("Failed to map %s: %s\n", path, strerror(errno));
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +110092 return -errno;
93 }
94
95 return 0;
96}
97
Andrew Jeffery07a76842017-04-12 14:09:06 +093098int init_lpc_dev(struct mbox_context *context)
99{
100 return __init_lpc_dev(context, LPC_CTRL_PATH);
101}
102
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100103void free_lpc_dev(struct mbox_context *context)
104{
105 if (context->mem) {
106 munmap(context->mem, context->mem_size);
107 }
108 close(context->fds[LPC_CTRL_FD].fd);
109}
110
111/*
112 * point_to_flash() - Point the lpc bus mapping to the actual flash device
113 * @context: The mbox context pointer
114 *
115 * Return: 0 on success otherwise negative error code
116 */
117int point_to_flash(struct mbox_context *context)
118{
119 struct aspeed_lpc_ctrl_mapping map = {
120 .window_type = ASPEED_LPC_CTRL_WINDOW_FLASH,
121 .window_id = 0, /* Theres only one */
122 .flags = 0,
123 /*
124 * The mask is because the top nibble is the host LPC FW space,
125 * we want space 0.
126 */
127 .addr = 0x0FFFFFFF & -context->flash_size,
128 .offset = 0,
129 .size = context->flash_size
130 };
131
132 if (context->state & MAPS_FLASH) {
133 return 0; /* LPC Bus already points to flash */
134 }
135 /* Don't let the host access flash while we're suspended */
136 if (context->state & STATE_SUSPENDED) {
137 MSG_ERR("Can't point lpc mapping to flash while suspended\n");
138 return -MBOX_R_PARAM_ERROR;
139 }
140
Suraj Jitindar Singh28519592017-04-27 14:48:58 +1000141 MSG_INFO("Pointing HOST LPC bus at the flash\n");
142 MSG_INFO("Assuming %dMB of flash: HOST LPC 0x%08x\n",
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100143 context->flash_size >> 20, map.addr);
144
145 if (ioctl(context->fds[LPC_CTRL_FD].fd, ASPEED_LPC_CTRL_IOCTL_MAP, &map)
146 == -1) {
147 MSG_ERR("Failed to point the LPC BUS at the actual flash: %s\n",
148 strerror(errno));
149 return -MBOX_R_SYSTEM_ERROR;
150 }
151
152 context->state = ACTIVE_MAPS_FLASH;
153 /*
154 * Since the host now has access to the flash it can change it out from
155 * under us
156 */
157 return set_flash_bytemap(context, 0, context->flash_size, FLASH_DIRTY);
158}
159
160/*
161 * point_to_memory() - Point the lpc bus mapping to the reserved memory region
162 * @context: The mbox context pointer
163 *
164 * Return: 0 on success otherwise negative error code
165 */
166int point_to_memory(struct mbox_context *context)
167{
168 struct aspeed_lpc_ctrl_mapping map = {
169 .window_type = ASPEED_LPC_CTRL_WINDOW_MEMORY,
170 .window_id = 0, /* There's only one */
171 .flags = 0,
172 .addr = context->lpc_base,
173 .offset = 0,
174 .size = context->mem_size
175 };
176
177 if (context->state & MAPS_MEM) {
178 return 0; /* LPC Bus already points to reserved memory area */
179 }
180
Suraj Jitindar Singh28519592017-04-27 14:48:58 +1000181 MSG_INFO("Pointing HOST LPC bus at memory region %p of size 0x%.8x\n",
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100182 context->mem, context->mem_size);
Suraj Jitindar Singh28519592017-04-27 14:48:58 +1000183 MSG_INFO("LPC address 0x%.8x\n", map.addr);
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100184
185 if (ioctl(context->fds[LPC_CTRL_FD].fd, ASPEED_LPC_CTRL_IOCTL_MAP,
186 &map)) {
187 MSG_ERR("Failed to point the LPC BUS to memory: %s\n",
188 strerror(errno));
189 return -MBOX_R_SYSTEM_ERROR;
190 }
191
192 /* LPC now maps memory (keep suspended state) */
193 context->state = MAPS_MEM | (context->state & STATE_SUSPENDED);
194
195 return 0;
196}