blob: 21102bb74ec6062912803ea2729ddd36d1d7df80 [file] [log] [blame]
Patrick Rudolphffbc3b52019-06-06 15:45:51 +02001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2019 9elements Agency GmbH
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; version 2 of
9 * the License.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * Place in devicetree.cb:
17 *
18 * chip drivers/ipmi
19 * device pnp ca2.0 on end # IPMI KCS
20 * end
21 */
22
23#include <console/console.h>
24#include <device/device.h>
25#include <device/pnp.h>
26#if CONFIG(HAVE_ACPI_TABLES)
27#include <arch/acpi.h>
28#include <arch/acpigen.h>
29#endif
30#if CONFIG(GENERATE_SMBIOS_TABLES)
31#include <smbios.h>
32#endif
33#include <version.h>
34#include <delay.h>
Patrick Rudolph3d41a132019-07-22 16:31:35 +020035#include <timer.h>
Patrick Rudolphffbc3b52019-06-06 15:45:51 +020036#include "ipmi_kcs.h"
37#include "chip.h"
38
39/* 4 bit encoding */
40static u8 ipmi_revision_major = 0x1;
41static u8 ipmi_revision_minor = 0x0;
42
43static int ipmi_get_device_id(struct device *dev, struct ipmi_devid_rsp *rsp)
44{
45 int ret;
46
47 ret = ipmi_kcs_message(dev->path.pnp.port, IPMI_NETFN_APPLICATION, 0,
48 IPMI_BMC_GET_DEVICE_ID, NULL, 0, (u8 *)rsp,
49 sizeof(*rsp));
50 if (ret < sizeof(struct ipmi_rsp) || rsp->resp.completion_code) {
51 printk(BIOS_ERR, "IPMI: %s command failed (ret=%d resp=0x%x)\n",
52 __func__, ret, rsp->resp.completion_code);
53 return 1;
54 }
55 if (ret != sizeof(*rsp)) {
56 printk(BIOS_ERR, "IPMI: %s response truncated\n", __func__);
57 return 1;
58 }
59 return 0;
60}
61
62static void ipmi_kcs_init(struct device *dev)
63{
64 struct ipmi_devid_rsp rsp;
65 uint32_t man_id = 0, prod_id = 0;
Patrick Rudolph3d41a132019-07-22 16:31:35 +020066 struct drivers_ipmi_config *conf = NULL;
Patrick Rudolphffbc3b52019-06-06 15:45:51 +020067
68 if (!dev->enabled)
69 return;
70
Patrick Rudolph3d41a132019-07-22 16:31:35 +020071 printk(BIOS_DEBUG, "IPMI: PNP KCS 0x%x\n", dev->path.pnp.port);
72
73 if (dev->chip_info)
74 conf = dev->chip_info;
75
Patrick Rudolphffbc3b52019-06-06 15:45:51 +020076 /* Get IPMI version for ACPI and SMBIOS */
Patrick Rudolph3d41a132019-07-22 16:31:35 +020077 if (conf && conf->wait_for_bmc && conf->bmc_boot_timeout) {
78 struct stopwatch sw;
79 stopwatch_init_msecs_expire(&sw, conf->bmc_boot_timeout * 1000);
80 printk(BIOS_DEBUG, "IPMI: Waiting for BMC...\n");
81
82 while (!stopwatch_expired(&sw)) {
83 if (inb(dev->path.pnp.port) != 0xff)
84 break;
85 mdelay(100);
86 }
87 if (stopwatch_expired(&sw)) {
88 printk(BIOS_INFO, "IPMI: Waiting for BMC timed out\n");
89 /* Don't write tables if communication failed */
90 dev->enabled = 0;
91 return;
92 }
93 }
94
Patrick Rudolphffbc3b52019-06-06 15:45:51 +020095 if (!ipmi_get_device_id(dev, &rsp)) {
Patrick Rudolph3d41a132019-07-22 16:31:35 +020096 /* Queried the IPMI revision from BMC */
Patrick Rudolphffbc3b52019-06-06 15:45:51 +020097 ipmi_revision_minor = IPMI_IPMI_VERSION_MINOR(rsp.ipmi_version);
98 ipmi_revision_major = IPMI_IPMI_VERSION_MAJOR(rsp.ipmi_version);
99
100 memcpy(&man_id, rsp.manufacturer_id,
101 sizeof(rsp.manufacturer_id));
102
103 memcpy(&prod_id, rsp.product_id, sizeof(rsp.product_id));
104
105 printk(BIOS_INFO, "IPMI: Found man_id 0x%06x, prod_id 0x%04x\n",
106 man_id, prod_id);
107
108 printk(BIOS_INFO, "IPMI: Version %01x.%01x\n",
109 ipmi_revision_major, ipmi_revision_minor);
110 } else {
111 /* Don't write tables if communication failed */
112 dev->enabled = 0;
113 }
114}
115
116#if CONFIG(HAVE_ACPI_TABLES)
117static uint32_t uid_cnt = 0;
118
119static unsigned long
120ipmi_write_acpi_tables(struct device *dev, unsigned long current,
121 struct acpi_rsdp *rsdp)
122{
123 struct drivers_ipmi_config *conf = NULL;
124 struct acpi_spmi *spmi;
125 s8 gpe_interrupt = -1;
126 u32 apic_interrupt = 0;
127 acpi_addr_t addr = {
128 .space_id = ACPI_ADDRESS_SPACE_IO,
129 .access_size = ACPI_ACCESS_SIZE_BYTE_ACCESS,
130 .addrl = dev->path.pnp.port,
131 };
132
133 current = ALIGN_UP(current, 8);
134 printk(BIOS_DEBUG, "ACPI: * SPMI at %lx\n", current);
135 spmi = (struct acpi_spmi *)current;
136
137 if (dev->chip_info)
138 conf = dev->chip_info;
139
140 if (conf) {
141 if (conf->have_gpe)
142 gpe_interrupt = conf->gpe_interrupt;
143 if (conf->have_apic)
144 apic_interrupt = conf->apic_interrupt;
145 }
146
147 /* Use command to get UID from ipmi_ssdt */
148 acpi_create_ipmi(dev, spmi, (ipmi_revision_major << 8) |
149 (ipmi_revision_minor << 4), &addr,
150 IPMI_INTERFACE_KCS, gpe_interrupt, apic_interrupt,
151 dev->command);
152
153 acpi_add_table(rsdp, spmi);
154
155 current += spmi->header.length;
156
157 return current;
158}
159
160static void ipmi_ssdt(struct device *dev)
161{
162 const char *scope = acpi_device_scope(dev);
163 struct drivers_ipmi_config *conf = NULL;
164
165 if (!scope) {
166 printk(BIOS_ERR, "IPMI: Missing ACPI scope for %s\n",
167 dev_path(dev));
168 return;
169 }
170
171 if (dev->chip_info)
172 conf = dev->chip_info;
173
174 /* Use command to pass UID to ipmi_write_acpi_tables */
175 dev->command = uid_cnt++;
176
177 /* write SPMI device */
178 acpigen_write_scope(scope);
179 acpigen_write_device("SPMI");
180 acpigen_write_name_string("_HID", "IPI0001");
181 acpigen_write_name_string("_STR", "IPMI_KCS");
182 acpigen_write_name_byte("_UID", dev->command);
183 acpigen_write_STA(0xf);
184 acpigen_write_name("_CRS");
185 acpigen_write_resourcetemplate_header();
186 acpigen_write_io16(dev->path.pnp.port, dev->path.pnp.port, 1, 2, 1);
187
188 if (conf) {
189 // FIXME: is that correct?
190 if (conf->have_apic)
191 acpigen_write_irq(1 << conf->apic_interrupt);
192 }
193
194 acpigen_write_resourcetemplate_footer();
195
196 acpigen_write_method("_IFT", 0);
197 acpigen_write_return_byte(1); // KCS
198 acpigen_pop_len();
199
200 acpigen_write_method("_SRV", 0);
201 acpigen_write_return_integer((ipmi_revision_major << 8) |
202 (ipmi_revision_minor << 4));
203 acpigen_pop_len();
204
205 acpigen_pop_len(); /* pop device */
206 acpigen_pop_len(); /* pop scope */
207}
208#endif
209
210#if CONFIG(GENERATE_SMBIOS_TABLES)
211static int ipmi_smbios_data(struct device *dev, int *handle,
212 unsigned long *current)
213{
214 struct drivers_ipmi_config *conf = NULL;
215 u8 nv_storage = 0xff;
216 u8 i2c_address = 0;
217 int len = 0;
218
219 if (dev->chip_info)
220 conf = dev->chip_info;
221
222 if (conf) {
223 if (conf->have_nv_storage)
224 nv_storage = conf->nv_storage_device_address;
225 i2c_address = conf->bmc_i2c_address;
226 }
227
228 // add IPMI Device Information
229 len += smbios_write_type38(
230 current, handle,
231 SMBIOS_BMC_INTERFACE_KCS,
232 ipmi_revision_minor | (ipmi_revision_major << 4),
233 i2c_address, // I2C address
234 nv_storage, // NV storage
235 dev->path.pnp.port | 1, // IO interface
236 0,
237 0); // no IRQ
238
239 return len;
240}
241#endif
242
243static void ipmi_set_resources(struct device *dev)
244{
245 struct resource *res;
246
247 for (res = dev->resource_list; res; res = res->next) {
248 if (!(res->flags & IORESOURCE_ASSIGNED))
249 continue;
250
251 res->flags |= IORESOURCE_STORED;
252 report_resource_stored(dev, res, "");
253 }
254}
255
256static void ipmi_read_resources(struct device *dev)
257{
258 struct resource *res = new_resource(dev, 0);
259 res->base = dev->path.pnp.port;
260 res->size = 2;
261 res->flags = IORESOURCE_IO | IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
262}
263
264static struct device_operations ops = {
265 .read_resources = ipmi_read_resources,
266 .set_resources = ipmi_set_resources,
267 .enable_resources = DEVICE_NOOP,
268 .init = ipmi_kcs_init,
269#if CONFIG(HAVE_ACPI_TABLES)
270 .write_acpi_tables = ipmi_write_acpi_tables,
271 .acpi_fill_ssdt_generator = ipmi_ssdt,
272#endif
273#if CONFIG(GENERATE_SMBIOS_TABLES)
274 .get_smbios_data = ipmi_smbios_data,
275#endif
276};
277
278static void enable_dev(struct device *dev)
279{
280 if (dev->path.type != DEVICE_PATH_PNP)
281 printk(BIOS_ERR, "%s: Unsupported device type\n",
282 dev_path(dev));
283 else if (dev->path.pnp.port & 1)
284 printk(BIOS_ERR, "%s: Base address needs to be aligned to 2\n",
285 dev_path(dev));
286 else
287 dev->ops = &ops;
288}
289
290struct chip_operations drivers_ipmi_ops = {
291 CHIP_NAME("IPMI KCS")
292 .enable_dev = enable_dev,
293};