blob: 0cc4e0a96535232205ae3ec751f09e6ed29f765f [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>
35#include "ipmi_kcs.h"
36#include "chip.h"
37
38/* 4 bit encoding */
39static u8 ipmi_revision_major = 0x1;
40static u8 ipmi_revision_minor = 0x0;
41
42static int ipmi_get_device_id(struct device *dev, struct ipmi_devid_rsp *rsp)
43{
44 int ret;
45
46 ret = ipmi_kcs_message(dev->path.pnp.port, IPMI_NETFN_APPLICATION, 0,
47 IPMI_BMC_GET_DEVICE_ID, NULL, 0, (u8 *)rsp,
48 sizeof(*rsp));
49 if (ret < sizeof(struct ipmi_rsp) || rsp->resp.completion_code) {
50 printk(BIOS_ERR, "IPMI: %s command failed (ret=%d resp=0x%x)\n",
51 __func__, ret, rsp->resp.completion_code);
52 return 1;
53 }
54 if (ret != sizeof(*rsp)) {
55 printk(BIOS_ERR, "IPMI: %s response truncated\n", __func__);
56 return 1;
57 }
58 return 0;
59}
60
61static void ipmi_kcs_init(struct device *dev)
62{
63 struct ipmi_devid_rsp rsp;
64 uint32_t man_id = 0, prod_id = 0;
65
66 if (!dev->enabled)
67 return;
68
69 /* Get IPMI version for ACPI and SMBIOS */
70 if (!ipmi_get_device_id(dev, &rsp)) {
71 ipmi_revision_minor = IPMI_IPMI_VERSION_MINOR(rsp.ipmi_version);
72 ipmi_revision_major = IPMI_IPMI_VERSION_MAJOR(rsp.ipmi_version);
73
74 memcpy(&man_id, rsp.manufacturer_id,
75 sizeof(rsp.manufacturer_id));
76
77 memcpy(&prod_id, rsp.product_id, sizeof(rsp.product_id));
78
79 printk(BIOS_INFO, "IPMI: Found man_id 0x%06x, prod_id 0x%04x\n",
80 man_id, prod_id);
81
82 printk(BIOS_INFO, "IPMI: Version %01x.%01x\n",
83 ipmi_revision_major, ipmi_revision_minor);
84 } else {
85 /* Don't write tables if communication failed */
86 dev->enabled = 0;
87 }
88}
89
90#if CONFIG(HAVE_ACPI_TABLES)
91static uint32_t uid_cnt = 0;
92
93static unsigned long
94ipmi_write_acpi_tables(struct device *dev, unsigned long current,
95 struct acpi_rsdp *rsdp)
96{
97 struct drivers_ipmi_config *conf = NULL;
98 struct acpi_spmi *spmi;
99 s8 gpe_interrupt = -1;
100 u32 apic_interrupt = 0;
101 acpi_addr_t addr = {
102 .space_id = ACPI_ADDRESS_SPACE_IO,
103 .access_size = ACPI_ACCESS_SIZE_BYTE_ACCESS,
104 .addrl = dev->path.pnp.port,
105 };
106
107 current = ALIGN_UP(current, 8);
108 printk(BIOS_DEBUG, "ACPI: * SPMI at %lx\n", current);
109 spmi = (struct acpi_spmi *)current;
110
111 if (dev->chip_info)
112 conf = dev->chip_info;
113
114 if (conf) {
115 if (conf->have_gpe)
116 gpe_interrupt = conf->gpe_interrupt;
117 if (conf->have_apic)
118 apic_interrupt = conf->apic_interrupt;
119 }
120
121 /* Use command to get UID from ipmi_ssdt */
122 acpi_create_ipmi(dev, spmi, (ipmi_revision_major << 8) |
123 (ipmi_revision_minor << 4), &addr,
124 IPMI_INTERFACE_KCS, gpe_interrupt, apic_interrupt,
125 dev->command);
126
127 acpi_add_table(rsdp, spmi);
128
129 current += spmi->header.length;
130
131 return current;
132}
133
134static void ipmi_ssdt(struct device *dev)
135{
136 const char *scope = acpi_device_scope(dev);
137 struct drivers_ipmi_config *conf = NULL;
138
139 if (!scope) {
140 printk(BIOS_ERR, "IPMI: Missing ACPI scope for %s\n",
141 dev_path(dev));
142 return;
143 }
144
145 if (dev->chip_info)
146 conf = dev->chip_info;
147
148 /* Use command to pass UID to ipmi_write_acpi_tables */
149 dev->command = uid_cnt++;
150
151 /* write SPMI device */
152 acpigen_write_scope(scope);
153 acpigen_write_device("SPMI");
154 acpigen_write_name_string("_HID", "IPI0001");
155 acpigen_write_name_string("_STR", "IPMI_KCS");
156 acpigen_write_name_byte("_UID", dev->command);
157 acpigen_write_STA(0xf);
158 acpigen_write_name("_CRS");
159 acpigen_write_resourcetemplate_header();
160 acpigen_write_io16(dev->path.pnp.port, dev->path.pnp.port, 1, 2, 1);
161
162 if (conf) {
163 // FIXME: is that correct?
164 if (conf->have_apic)
165 acpigen_write_irq(1 << conf->apic_interrupt);
166 }
167
168 acpigen_write_resourcetemplate_footer();
169
170 acpigen_write_method("_IFT", 0);
171 acpigen_write_return_byte(1); // KCS
172 acpigen_pop_len();
173
174 acpigen_write_method("_SRV", 0);
175 acpigen_write_return_integer((ipmi_revision_major << 8) |
176 (ipmi_revision_minor << 4));
177 acpigen_pop_len();
178
179 acpigen_pop_len(); /* pop device */
180 acpigen_pop_len(); /* pop scope */
181}
182#endif
183
184#if CONFIG(GENERATE_SMBIOS_TABLES)
185static int ipmi_smbios_data(struct device *dev, int *handle,
186 unsigned long *current)
187{
188 struct drivers_ipmi_config *conf = NULL;
189 u8 nv_storage = 0xff;
190 u8 i2c_address = 0;
191 int len = 0;
192
193 if (dev->chip_info)
194 conf = dev->chip_info;
195
196 if (conf) {
197 if (conf->have_nv_storage)
198 nv_storage = conf->nv_storage_device_address;
199 i2c_address = conf->bmc_i2c_address;
200 }
201
202 // add IPMI Device Information
203 len += smbios_write_type38(
204 current, handle,
205 SMBIOS_BMC_INTERFACE_KCS,
206 ipmi_revision_minor | (ipmi_revision_major << 4),
207 i2c_address, // I2C address
208 nv_storage, // NV storage
209 dev->path.pnp.port | 1, // IO interface
210 0,
211 0); // no IRQ
212
213 return len;
214}
215#endif
216
217static void ipmi_set_resources(struct device *dev)
218{
219 struct resource *res;
220
221 for (res = dev->resource_list; res; res = res->next) {
222 if (!(res->flags & IORESOURCE_ASSIGNED))
223 continue;
224
225 res->flags |= IORESOURCE_STORED;
226 report_resource_stored(dev, res, "");
227 }
228}
229
230static void ipmi_read_resources(struct device *dev)
231{
232 struct resource *res = new_resource(dev, 0);
233 res->base = dev->path.pnp.port;
234 res->size = 2;
235 res->flags = IORESOURCE_IO | IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
236}
237
238static struct device_operations ops = {
239 .read_resources = ipmi_read_resources,
240 .set_resources = ipmi_set_resources,
241 .enable_resources = DEVICE_NOOP,
242 .init = ipmi_kcs_init,
243#if CONFIG(HAVE_ACPI_TABLES)
244 .write_acpi_tables = ipmi_write_acpi_tables,
245 .acpi_fill_ssdt_generator = ipmi_ssdt,
246#endif
247#if CONFIG(GENERATE_SMBIOS_TABLES)
248 .get_smbios_data = ipmi_smbios_data,
249#endif
250};
251
252static void enable_dev(struct device *dev)
253{
254 if (dev->path.type != DEVICE_PATH_PNP)
255 printk(BIOS_ERR, "%s: Unsupported device type\n",
256 dev_path(dev));
257 else if (dev->path.pnp.port & 1)
258 printk(BIOS_ERR, "%s: Base address needs to be aligned to 2\n",
259 dev_path(dev));
260 else
261 dev->ops = &ops;
262}
263
264struct chip_operations drivers_ipmi_ops = {
265 CHIP_NAME("IPMI KCS")
266 .enable_dev = enable_dev,
267};