qipeng.zha | 0a8b835 | 2015-06-27 00:32:15 +0800 | [diff] [blame] | 1 | /* |
| 2 | * intel_pmc_ipc.c: Driver for the Intel PMC IPC mechanism |
| 3 | * |
| 4 | * (C) Copyright 2014-2015 Intel Corporation |
| 5 | * |
| 6 | * This driver is based on Intel SCU IPC driver(intel_scu_opc.c) by |
| 7 | * Sreedhara DS <sreedhara.ds@intel.com> |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or |
| 10 | * modify it under the terms of the GNU General Public License |
| 11 | * as published by the Free Software Foundation; version 2 |
| 12 | * of the License. |
| 13 | * |
| 14 | * PMC running in ARC processor communicates with other entity running in IA |
| 15 | * core through IPC mechanism which in turn messaging between IA core ad PMC. |
| 16 | */ |
| 17 | |
| 18 | #include <linux/module.h> |
| 19 | #include <linux/delay.h> |
| 20 | #include <linux/errno.h> |
| 21 | #include <linux/init.h> |
| 22 | #include <linux/device.h> |
| 23 | #include <linux/pm.h> |
| 24 | #include <linux/pci.h> |
| 25 | #include <linux/platform_device.h> |
| 26 | #include <linux/interrupt.h> |
| 27 | #include <linux/pm_qos.h> |
| 28 | #include <linux/kernel.h> |
| 29 | #include <linux/bitops.h> |
| 30 | #include <linux/sched.h> |
| 31 | #include <linux/atomic.h> |
| 32 | #include <linux/notifier.h> |
| 33 | #include <linux/suspend.h> |
| 34 | #include <linux/acpi.h> |
Shanth Murthy | 76062b4 | 2017-02-13 04:02:52 -0800 | [diff] [blame] | 35 | #include <linux/io-64-nonatomic-lo-hi.h> |
| 36 | |
qipeng.zha | 0a8b835 | 2015-06-27 00:32:15 +0800 | [diff] [blame] | 37 | #include <asm/intel_pmc_ipc.h> |
Shanth Murthy | 76062b4 | 2017-02-13 04:02:52 -0800 | [diff] [blame] | 38 | |
Matt Fleming | 420b54d | 2015-08-06 13:46:24 +0100 | [diff] [blame] | 39 | #include <linux/platform_data/itco_wdt.h> |
qipeng.zha | 0a8b835 | 2015-06-27 00:32:15 +0800 | [diff] [blame] | 40 | |
| 41 | /* |
| 42 | * IPC registers |
| 43 | * The IA write to IPC_CMD command register triggers an interrupt to the ARC, |
| 44 | * The ARC handles the interrupt and services it, writing optional data to |
| 45 | * the IPC1 registers, updates the IPC_STS response register with the status. |
| 46 | */ |
| 47 | #define IPC_CMD 0x0 |
| 48 | #define IPC_CMD_MSI 0x100 |
| 49 | #define IPC_CMD_SIZE 16 |
| 50 | #define IPC_CMD_SUBCMD 12 |
| 51 | #define IPC_STATUS 0x04 |
| 52 | #define IPC_STATUS_IRQ 0x4 |
| 53 | #define IPC_STATUS_ERR 0x2 |
| 54 | #define IPC_STATUS_BUSY 0x1 |
| 55 | #define IPC_SPTR 0x08 |
| 56 | #define IPC_DPTR 0x0C |
| 57 | #define IPC_WRITE_BUFFER 0x80 |
| 58 | #define IPC_READ_BUFFER 0x90 |
| 59 | |
Shanth Murthy | 76062b4 | 2017-02-13 04:02:52 -0800 | [diff] [blame] | 60 | /* PMC Global Control Registers */ |
| 61 | #define GCR_TELEM_DEEP_S0IX_OFFSET 0x1078 |
| 62 | #define GCR_TELEM_SHLW_S0IX_OFFSET 0x1080 |
| 63 | |
| 64 | /* Residency with clock rate at 19.2MHz to usecs */ |
| 65 | #define S0IX_RESIDENCY_IN_USECS(d, s) \ |
| 66 | ({ \ |
| 67 | u64 result = 10ull * ((d) + (s)); \ |
| 68 | do_div(result, 192); \ |
| 69 | result; \ |
| 70 | }) |
| 71 | |
qipeng.zha | 0a8b835 | 2015-06-27 00:32:15 +0800 | [diff] [blame] | 72 | /* |
| 73 | * 16-byte buffer for sending data associated with IPC command. |
| 74 | */ |
| 75 | #define IPC_DATA_BUFFER_SIZE 16 |
| 76 | |
| 77 | #define IPC_LOOP_CNT 3000000 |
| 78 | #define IPC_MAX_SEC 3 |
| 79 | |
| 80 | #define IPC_TRIGGER_MODE_IRQ true |
| 81 | |
| 82 | /* exported resources from IFWI */ |
| 83 | #define PLAT_RESOURCE_IPC_INDEX 0 |
| 84 | #define PLAT_RESOURCE_IPC_SIZE 0x1000 |
Kuppuswamy Sathyanarayanan | e6749c8 | 2017-04-09 15:00:16 -0700 | [diff] [blame] | 85 | #define PLAT_RESOURCE_GCR_OFFSET 0x1000 |
Shanth Murthy | 76062b4 | 2017-02-13 04:02:52 -0800 | [diff] [blame] | 86 | #define PLAT_RESOURCE_GCR_SIZE 0x1000 |
Qipeng Zha | 8cc7fb4 | 2015-12-11 22:44:59 +0800 | [diff] [blame] | 87 | #define PLAT_RESOURCE_BIOS_DATA_INDEX 1 |
| 88 | #define PLAT_RESOURCE_BIOS_IFACE_INDEX 2 |
Souvik Kumar Chakravarty | 48c1917 | 2016-01-12 16:02:54 +0530 | [diff] [blame] | 89 | #define PLAT_RESOURCE_TELEM_SSRAM_INDEX 3 |
Qipeng Zha | 8cc7fb4 | 2015-12-11 22:44:59 +0800 | [diff] [blame] | 90 | #define PLAT_RESOURCE_ISP_DATA_INDEX 4 |
| 91 | #define PLAT_RESOURCE_ISP_IFACE_INDEX 5 |
| 92 | #define PLAT_RESOURCE_GTD_DATA_INDEX 6 |
| 93 | #define PLAT_RESOURCE_GTD_IFACE_INDEX 7 |
qipeng.zha | 0a8b835 | 2015-06-27 00:32:15 +0800 | [diff] [blame] | 94 | #define PLAT_RESOURCE_ACPI_IO_INDEX 0 |
| 95 | |
| 96 | /* |
| 97 | * BIOS does not create an ACPI device for each PMC function, |
| 98 | * but exports multiple resources from one ACPI device(IPC) for |
| 99 | * multiple functions. This driver is responsible to create a |
| 100 | * platform device and to export resources for those functions. |
| 101 | */ |
| 102 | #define TCO_DEVICE_NAME "iTCO_wdt" |
Yong, Jonathan | 334da2d | 2016-06-17 00:33:32 +0000 | [diff] [blame] | 103 | #define SMI_EN_OFFSET 0x40 |
qipeng.zha | 0a8b835 | 2015-06-27 00:32:15 +0800 | [diff] [blame] | 104 | #define SMI_EN_SIZE 4 |
| 105 | #define TCO_BASE_OFFSET 0x60 |
| 106 | #define TCO_REGS_SIZE 16 |
| 107 | #define PUNIT_DEVICE_NAME "intel_punit_ipc" |
Souvik Kumar Chakravarty | 48c1917 | 2016-01-12 16:02:54 +0530 | [diff] [blame] | 108 | #define TELEMETRY_DEVICE_NAME "intel_telemetry" |
| 109 | #define TELEM_SSRAM_SIZE 240 |
| 110 | #define TELEM_PMC_SSRAM_OFFSET 0x1B00 |
| 111 | #define TELEM_PUNIT_SSRAM_OFFSET 0x1A00 |
Yong, Jonathan | 334da2d | 2016-06-17 00:33:32 +0000 | [diff] [blame] | 112 | #define TCO_PMC_OFFSET 0x8 |
| 113 | #define TCO_PMC_SIZE 0x4 |
qipeng.zha | 0a8b835 | 2015-06-27 00:32:15 +0800 | [diff] [blame] | 114 | |
qipeng.zha | 0a8b835 | 2015-06-27 00:32:15 +0800 | [diff] [blame] | 115 | static struct intel_pmc_ipc_dev { |
| 116 | struct device *dev; |
| 117 | void __iomem *ipc_base; |
| 118 | bool irq_mode; |
| 119 | int irq; |
| 120 | int cmd; |
| 121 | struct completion cmd_complete; |
| 122 | |
| 123 | /* The following PMC BARs share the same ACPI device with the IPC */ |
qipeng.zha | b78fb51 | 2015-07-07 00:04:45 +0800 | [diff] [blame] | 124 | resource_size_t acpi_io_base; |
qipeng.zha | 0a8b835 | 2015-06-27 00:32:15 +0800 | [diff] [blame] | 125 | int acpi_io_size; |
| 126 | struct platform_device *tco_dev; |
| 127 | |
| 128 | /* gcr */ |
qipeng.zha | b78fb51 | 2015-07-07 00:04:45 +0800 | [diff] [blame] | 129 | resource_size_t gcr_base; |
qipeng.zha | 0a8b835 | 2015-06-27 00:32:15 +0800 | [diff] [blame] | 130 | int gcr_size; |
Kuppuswamy Sathyanarayanan | 4967020 | 2017-04-09 15:00:17 -0700 | [diff] [blame^] | 131 | void __iomem *gcr_mem_base; |
Shanth Murthy | 76062b4 | 2017-02-13 04:02:52 -0800 | [diff] [blame] | 132 | bool has_gcr_regs; |
qipeng.zha | 0a8b835 | 2015-06-27 00:32:15 +0800 | [diff] [blame] | 133 | |
| 134 | /* punit */ |
qipeng.zha | 0a8b835 | 2015-06-27 00:32:15 +0800 | [diff] [blame] | 135 | struct platform_device *punit_dev; |
Souvik Kumar Chakravarty | 48c1917 | 2016-01-12 16:02:54 +0530 | [diff] [blame] | 136 | |
| 137 | /* Telemetry */ |
| 138 | resource_size_t telem_pmc_ssram_base; |
| 139 | resource_size_t telem_punit_ssram_base; |
| 140 | int telem_pmc_ssram_size; |
| 141 | int telem_punit_ssram_size; |
| 142 | u8 telem_res_inval; |
| 143 | struct platform_device *telemetry_dev; |
qipeng.zha | 0a8b835 | 2015-06-27 00:32:15 +0800 | [diff] [blame] | 144 | } ipcdev; |
| 145 | |
| 146 | static char *ipc_err_sources[] = { |
| 147 | [IPC_ERR_NONE] = |
| 148 | "no error", |
| 149 | [IPC_ERR_CMD_NOT_SUPPORTED] = |
| 150 | "command not supported", |
| 151 | [IPC_ERR_CMD_NOT_SERVICED] = |
| 152 | "command not serviced", |
| 153 | [IPC_ERR_UNABLE_TO_SERVICE] = |
| 154 | "unable to service", |
| 155 | [IPC_ERR_CMD_INVALID] = |
| 156 | "command invalid", |
| 157 | [IPC_ERR_CMD_FAILED] = |
| 158 | "command failed", |
| 159 | [IPC_ERR_EMSECURITY] = |
| 160 | "Invalid Battery", |
| 161 | [IPC_ERR_UNSIGNEDKERNEL] = |
| 162 | "Unsigned kernel", |
| 163 | }; |
| 164 | |
| 165 | /* Prevent concurrent calls to the PMC */ |
| 166 | static DEFINE_MUTEX(ipclock); |
| 167 | |
| 168 | static inline void ipc_send_command(u32 cmd) |
| 169 | { |
| 170 | ipcdev.cmd = cmd; |
| 171 | if (ipcdev.irq_mode) { |
| 172 | reinit_completion(&ipcdev.cmd_complete); |
| 173 | cmd |= IPC_CMD_MSI; |
| 174 | } |
| 175 | writel(cmd, ipcdev.ipc_base + IPC_CMD); |
| 176 | } |
| 177 | |
| 178 | static inline u32 ipc_read_status(void) |
| 179 | { |
| 180 | return readl(ipcdev.ipc_base + IPC_STATUS); |
| 181 | } |
| 182 | |
| 183 | static inline void ipc_data_writel(u32 data, u32 offset) |
| 184 | { |
| 185 | writel(data, ipcdev.ipc_base + IPC_WRITE_BUFFER + offset); |
| 186 | } |
| 187 | |
| 188 | static inline u8 ipc_data_readb(u32 offset) |
| 189 | { |
| 190 | return readb(ipcdev.ipc_base + IPC_READ_BUFFER + offset); |
| 191 | } |
| 192 | |
| 193 | static inline u32 ipc_data_readl(u32 offset) |
| 194 | { |
| 195 | return readl(ipcdev.ipc_base + IPC_READ_BUFFER + offset); |
| 196 | } |
| 197 | |
Shanth Murthy | 76062b4 | 2017-02-13 04:02:52 -0800 | [diff] [blame] | 198 | static inline u64 gcr_data_readq(u32 offset) |
| 199 | { |
| 200 | return readq(ipcdev.ipc_base + offset); |
| 201 | } |
| 202 | |
Kuppuswamy Sathyanarayanan | 4967020 | 2017-04-09 15:00:17 -0700 | [diff] [blame^] | 203 | static inline int is_gcr_valid(u32 offset) |
| 204 | { |
| 205 | if (!ipcdev.has_gcr_regs) |
| 206 | return -EACCES; |
| 207 | |
| 208 | if (offset > PLAT_RESOURCE_GCR_SIZE) |
| 209 | return -EINVAL; |
| 210 | |
| 211 | return 0; |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * intel_pmc_gcr_read() - Read PMC GCR register |
| 216 | * @offset: offset of GCR register from GCR address base |
| 217 | * @data: data pointer for storing the register output |
| 218 | * |
| 219 | * Reads the PMC GCR register of given offset. |
| 220 | * |
| 221 | * Return: negative value on error or 0 on success. |
| 222 | */ |
| 223 | int intel_pmc_gcr_read(u32 offset, u32 *data) |
| 224 | { |
| 225 | int ret; |
| 226 | |
| 227 | mutex_lock(&ipclock); |
| 228 | |
| 229 | ret = is_gcr_valid(offset); |
| 230 | if (ret < 0) { |
| 231 | mutex_unlock(&ipclock); |
| 232 | return ret; |
| 233 | } |
| 234 | |
| 235 | *data = readl(ipcdev.gcr_mem_base + offset); |
| 236 | |
| 237 | mutex_unlock(&ipclock); |
| 238 | |
| 239 | return 0; |
| 240 | } |
| 241 | EXPORT_SYMBOL_GPL(intel_pmc_gcr_read); |
| 242 | |
| 243 | /** |
| 244 | * intel_pmc_gcr_write() - Write PMC GCR register |
| 245 | * @offset: offset of GCR register from GCR address base |
| 246 | * @data: register update value |
| 247 | * |
| 248 | * Writes the PMC GCR register of given offset with given |
| 249 | * value. |
| 250 | * |
| 251 | * Return: negative value on error or 0 on success. |
| 252 | */ |
| 253 | int intel_pmc_gcr_write(u32 offset, u32 data) |
| 254 | { |
| 255 | int ret; |
| 256 | |
| 257 | mutex_lock(&ipclock); |
| 258 | |
| 259 | ret = is_gcr_valid(offset); |
| 260 | if (ret < 0) { |
| 261 | mutex_unlock(&ipclock); |
| 262 | return ret; |
| 263 | } |
| 264 | |
| 265 | writel(data, ipcdev.gcr_mem_base + offset); |
| 266 | |
| 267 | mutex_unlock(&ipclock); |
| 268 | |
| 269 | return 0; |
| 270 | } |
| 271 | EXPORT_SYMBOL_GPL(intel_pmc_gcr_write); |
| 272 | |
| 273 | /** |
| 274 | * intel_pmc_gcr_update() - Update PMC GCR register bits |
| 275 | * @offset: offset of GCR register from GCR address base |
| 276 | * @mask: bit mask for update operation |
| 277 | * @val: update value |
| 278 | * |
| 279 | * Updates the bits of given GCR register as specified by |
| 280 | * @mask and @val. |
| 281 | * |
| 282 | * Return: negative value on error or 0 on success. |
| 283 | */ |
| 284 | int intel_pmc_gcr_update(u32 offset, u32 mask, u32 val) |
| 285 | { |
| 286 | u32 new_val; |
| 287 | int ret = 0; |
| 288 | |
| 289 | mutex_lock(&ipclock); |
| 290 | |
| 291 | ret = is_gcr_valid(offset); |
| 292 | if (ret < 0) |
| 293 | goto gcr_ipc_unlock; |
| 294 | |
| 295 | new_val = readl(ipcdev.gcr_mem_base + offset); |
| 296 | |
| 297 | new_val &= ~mask; |
| 298 | new_val |= val & mask; |
| 299 | |
| 300 | writel(new_val, ipcdev.gcr_mem_base + offset); |
| 301 | |
| 302 | new_val = readl(ipcdev.gcr_mem_base + offset); |
| 303 | |
| 304 | /* check whether the bit update is successful */ |
| 305 | if ((new_val & mask) != (val & mask)) { |
| 306 | ret = -EIO; |
| 307 | goto gcr_ipc_unlock; |
| 308 | } |
| 309 | |
| 310 | gcr_ipc_unlock: |
| 311 | mutex_unlock(&ipclock); |
| 312 | return ret; |
| 313 | } |
| 314 | EXPORT_SYMBOL_GPL(intel_pmc_gcr_update); |
| 315 | |
qipeng.zha | 0a8b835 | 2015-06-27 00:32:15 +0800 | [diff] [blame] | 316 | static int intel_pmc_ipc_check_status(void) |
| 317 | { |
| 318 | int status; |
| 319 | int ret = 0; |
| 320 | |
| 321 | if (ipcdev.irq_mode) { |
| 322 | if (0 == wait_for_completion_timeout( |
| 323 | &ipcdev.cmd_complete, IPC_MAX_SEC * HZ)) |
| 324 | ret = -ETIMEDOUT; |
| 325 | } else { |
| 326 | int loop_count = IPC_LOOP_CNT; |
| 327 | |
| 328 | while ((ipc_read_status() & IPC_STATUS_BUSY) && --loop_count) |
| 329 | udelay(1); |
| 330 | if (loop_count == 0) |
| 331 | ret = -ETIMEDOUT; |
| 332 | } |
| 333 | |
| 334 | status = ipc_read_status(); |
| 335 | if (ret == -ETIMEDOUT) { |
| 336 | dev_err(ipcdev.dev, |
| 337 | "IPC timed out, TS=0x%x, CMD=0x%x\n", |
| 338 | status, ipcdev.cmd); |
| 339 | return ret; |
| 340 | } |
| 341 | |
| 342 | if (status & IPC_STATUS_ERR) { |
| 343 | int i; |
| 344 | |
| 345 | ret = -EIO; |
| 346 | i = (status >> IPC_CMD_SIZE) & 0xFF; |
| 347 | if (i < ARRAY_SIZE(ipc_err_sources)) |
| 348 | dev_err(ipcdev.dev, |
| 349 | "IPC failed: %s, STS=0x%x, CMD=0x%x\n", |
| 350 | ipc_err_sources[i], status, ipcdev.cmd); |
| 351 | else |
| 352 | dev_err(ipcdev.dev, |
| 353 | "IPC failed: unknown, STS=0x%x, CMD=0x%x\n", |
| 354 | status, ipcdev.cmd); |
| 355 | if ((i == IPC_ERR_UNSIGNEDKERNEL) || (i == IPC_ERR_EMSECURITY)) |
| 356 | ret = -EACCES; |
| 357 | } |
| 358 | |
| 359 | return ret; |
| 360 | } |
| 361 | |
qipeng.zha | 0294100 | 2015-07-09 00:14:15 +0800 | [diff] [blame] | 362 | /** |
| 363 | * intel_pmc_ipc_simple_command() - Simple IPC command |
| 364 | * @cmd: IPC command code. |
| 365 | * @sub: IPC command sub type. |
| 366 | * |
| 367 | * Send a simple IPC command to PMC when don't need to specify |
| 368 | * input/output data and source/dest pointers. |
| 369 | * |
| 370 | * Return: an IPC error code or 0 on success. |
qipeng.zha | 0a8b835 | 2015-06-27 00:32:15 +0800 | [diff] [blame] | 371 | */ |
| 372 | int intel_pmc_ipc_simple_command(int cmd, int sub) |
| 373 | { |
| 374 | int ret; |
| 375 | |
| 376 | mutex_lock(&ipclock); |
| 377 | if (ipcdev.dev == NULL) { |
| 378 | mutex_unlock(&ipclock); |
| 379 | return -ENODEV; |
| 380 | } |
| 381 | ipc_send_command(sub << IPC_CMD_SUBCMD | cmd); |
| 382 | ret = intel_pmc_ipc_check_status(); |
| 383 | mutex_unlock(&ipclock); |
| 384 | |
| 385 | return ret; |
| 386 | } |
| 387 | EXPORT_SYMBOL_GPL(intel_pmc_ipc_simple_command); |
| 388 | |
qipeng.zha | 0294100 | 2015-07-09 00:14:15 +0800 | [diff] [blame] | 389 | /** |
| 390 | * intel_pmc_ipc_raw_cmd() - IPC command with data and pointers |
| 391 | * @cmd: IPC command code. |
| 392 | * @sub: IPC command sub type. |
| 393 | * @in: input data of this IPC command. |
| 394 | * @inlen: input data length in bytes. |
| 395 | * @out: output data of this IPC command. |
| 396 | * @outlen: output data length in dwords. |
| 397 | * @sptr: data writing to SPTR register. |
| 398 | * @dptr: data writing to DPTR register. |
| 399 | * |
| 400 | * Send an IPC command to PMC with input/output data and source/dest pointers. |
| 401 | * |
| 402 | * Return: an IPC error code or 0 on success. |
qipeng.zha | 0a8b835 | 2015-06-27 00:32:15 +0800 | [diff] [blame] | 403 | */ |
| 404 | int intel_pmc_ipc_raw_cmd(u32 cmd, u32 sub, u8 *in, u32 inlen, u32 *out, |
| 405 | u32 outlen, u32 dptr, u32 sptr) |
| 406 | { |
| 407 | u32 wbuf[4] = { 0 }; |
| 408 | int ret; |
| 409 | int i; |
| 410 | |
| 411 | if (inlen > IPC_DATA_BUFFER_SIZE || outlen > IPC_DATA_BUFFER_SIZE / 4) |
| 412 | return -EINVAL; |
| 413 | |
| 414 | mutex_lock(&ipclock); |
| 415 | if (ipcdev.dev == NULL) { |
| 416 | mutex_unlock(&ipclock); |
| 417 | return -ENODEV; |
| 418 | } |
| 419 | memcpy(wbuf, in, inlen); |
| 420 | writel(dptr, ipcdev.ipc_base + IPC_DPTR); |
| 421 | writel(sptr, ipcdev.ipc_base + IPC_SPTR); |
| 422 | /* The input data register is 32bit register and inlen is in Byte */ |
| 423 | for (i = 0; i < ((inlen + 3) / 4); i++) |
| 424 | ipc_data_writel(wbuf[i], 4 * i); |
| 425 | ipc_send_command((inlen << IPC_CMD_SIZE) | |
| 426 | (sub << IPC_CMD_SUBCMD) | cmd); |
| 427 | ret = intel_pmc_ipc_check_status(); |
| 428 | if (!ret) { |
| 429 | /* out is read from 32bit register and outlen is in 32bit */ |
| 430 | for (i = 0; i < outlen; i++) |
| 431 | *out++ = ipc_data_readl(4 * i); |
| 432 | } |
| 433 | mutex_unlock(&ipclock); |
| 434 | |
| 435 | return ret; |
| 436 | } |
| 437 | EXPORT_SYMBOL_GPL(intel_pmc_ipc_raw_cmd); |
| 438 | |
qipeng.zha | 0294100 | 2015-07-09 00:14:15 +0800 | [diff] [blame] | 439 | /** |
| 440 | * intel_pmc_ipc_command() - IPC command with input/output data |
| 441 | * @cmd: IPC command code. |
| 442 | * @sub: IPC command sub type. |
| 443 | * @in: input data of this IPC command. |
| 444 | * @inlen: input data length in bytes. |
| 445 | * @out: output data of this IPC command. |
| 446 | * @outlen: output data length in dwords. |
| 447 | * |
| 448 | * Send an IPC command to PMC with input/output data. |
| 449 | * |
| 450 | * Return: an IPC error code or 0 on success. |
qipeng.zha | 0a8b835 | 2015-06-27 00:32:15 +0800 | [diff] [blame] | 451 | */ |
| 452 | int intel_pmc_ipc_command(u32 cmd, u32 sub, u8 *in, u32 inlen, |
| 453 | u32 *out, u32 outlen) |
| 454 | { |
| 455 | return intel_pmc_ipc_raw_cmd(cmd, sub, in, inlen, out, outlen, 0, 0); |
| 456 | } |
| 457 | EXPORT_SYMBOL_GPL(intel_pmc_ipc_command); |
| 458 | |
| 459 | static irqreturn_t ioc(int irq, void *dev_id) |
| 460 | { |
| 461 | int status; |
| 462 | |
| 463 | if (ipcdev.irq_mode) { |
| 464 | status = ipc_read_status(); |
| 465 | writel(status | IPC_STATUS_IRQ, ipcdev.ipc_base + IPC_STATUS); |
| 466 | } |
| 467 | complete(&ipcdev.cmd_complete); |
| 468 | |
| 469 | return IRQ_HANDLED; |
| 470 | } |
| 471 | |
| 472 | static int ipc_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id) |
| 473 | { |
| 474 | resource_size_t pci_resource; |
| 475 | int ret; |
| 476 | int len; |
| 477 | |
| 478 | ipcdev.dev = &pci_dev_get(pdev)->dev; |
| 479 | ipcdev.irq_mode = IPC_TRIGGER_MODE_IRQ; |
| 480 | |
| 481 | ret = pci_enable_device(pdev); |
| 482 | if (ret) |
| 483 | return ret; |
| 484 | |
| 485 | ret = pci_request_regions(pdev, "intel_pmc_ipc"); |
| 486 | if (ret) |
| 487 | return ret; |
| 488 | |
| 489 | pci_resource = pci_resource_start(pdev, 0); |
| 490 | len = pci_resource_len(pdev, 0); |
| 491 | if (!pci_resource || !len) { |
| 492 | dev_err(&pdev->dev, "Failed to get resource\n"); |
| 493 | return -ENOMEM; |
| 494 | } |
| 495 | |
| 496 | init_completion(&ipcdev.cmd_complete); |
| 497 | |
| 498 | if (request_irq(pdev->irq, ioc, 0, "intel_pmc_ipc", &ipcdev)) { |
| 499 | dev_err(&pdev->dev, "Failed to request irq\n"); |
| 500 | return -EBUSY; |
| 501 | } |
| 502 | |
| 503 | ipcdev.ipc_base = ioremap_nocache(pci_resource, len); |
| 504 | if (!ipcdev.ipc_base) { |
| 505 | dev_err(&pdev->dev, "Failed to ioremap ipc base\n"); |
| 506 | free_irq(pdev->irq, &ipcdev); |
| 507 | ret = -ENOMEM; |
| 508 | } |
| 509 | |
| 510 | return ret; |
| 511 | } |
| 512 | |
| 513 | static void ipc_pci_remove(struct pci_dev *pdev) |
| 514 | { |
| 515 | free_irq(pdev->irq, &ipcdev); |
| 516 | pci_release_regions(pdev); |
| 517 | pci_dev_put(pdev); |
| 518 | iounmap(ipcdev.ipc_base); |
| 519 | ipcdev.dev = NULL; |
| 520 | } |
| 521 | |
| 522 | static const struct pci_device_id ipc_pci_ids[] = { |
| 523 | {PCI_VDEVICE(INTEL, 0x0a94), 0}, |
| 524 | {PCI_VDEVICE(INTEL, 0x1a94), 0}, |
Rajneesh Bhardwaj | 23e775d | 2017-02-13 16:11:47 +0530 | [diff] [blame] | 525 | {PCI_VDEVICE(INTEL, 0x5a94), 0}, |
qipeng.zha | 0a8b835 | 2015-06-27 00:32:15 +0800 | [diff] [blame] | 526 | { 0,} |
| 527 | }; |
| 528 | MODULE_DEVICE_TABLE(pci, ipc_pci_ids); |
| 529 | |
| 530 | static struct pci_driver ipc_pci_driver = { |
| 531 | .name = "intel_pmc_ipc", |
| 532 | .id_table = ipc_pci_ids, |
| 533 | .probe = ipc_pci_probe, |
| 534 | .remove = ipc_pci_remove, |
| 535 | }; |
| 536 | |
| 537 | static ssize_t intel_pmc_ipc_simple_cmd_store(struct device *dev, |
| 538 | struct device_attribute *attr, |
| 539 | const char *buf, size_t count) |
| 540 | { |
| 541 | int subcmd; |
| 542 | int cmd; |
| 543 | int ret; |
| 544 | |
| 545 | ret = sscanf(buf, "%d %d", &cmd, &subcmd); |
| 546 | if (ret != 2) { |
| 547 | dev_err(dev, "Error args\n"); |
| 548 | return -EINVAL; |
| 549 | } |
| 550 | |
| 551 | ret = intel_pmc_ipc_simple_command(cmd, subcmd); |
| 552 | if (ret) { |
| 553 | dev_err(dev, "command %d error with %d\n", cmd, ret); |
| 554 | return ret; |
| 555 | } |
| 556 | return (ssize_t)count; |
| 557 | } |
| 558 | |
| 559 | static ssize_t intel_pmc_ipc_northpeak_store(struct device *dev, |
| 560 | struct device_attribute *attr, |
| 561 | const char *buf, size_t count) |
| 562 | { |
| 563 | unsigned long val; |
| 564 | int subcmd; |
| 565 | int ret; |
| 566 | |
| 567 | if (kstrtoul(buf, 0, &val)) |
| 568 | return -EINVAL; |
| 569 | |
| 570 | if (val) |
| 571 | subcmd = 1; |
| 572 | else |
| 573 | subcmd = 0; |
| 574 | ret = intel_pmc_ipc_simple_command(PMC_IPC_NORTHPEAK_CTRL, subcmd); |
| 575 | if (ret) { |
| 576 | dev_err(dev, "command north %d error with %d\n", subcmd, ret); |
| 577 | return ret; |
| 578 | } |
| 579 | return (ssize_t)count; |
| 580 | } |
| 581 | |
| 582 | static DEVICE_ATTR(simplecmd, S_IWUSR, |
| 583 | NULL, intel_pmc_ipc_simple_cmd_store); |
| 584 | static DEVICE_ATTR(northpeak, S_IWUSR, |
| 585 | NULL, intel_pmc_ipc_northpeak_store); |
| 586 | |
| 587 | static struct attribute *intel_ipc_attrs[] = { |
| 588 | &dev_attr_northpeak.attr, |
| 589 | &dev_attr_simplecmd.attr, |
| 590 | NULL |
| 591 | }; |
| 592 | |
| 593 | static const struct attribute_group intel_ipc_group = { |
| 594 | .attrs = intel_ipc_attrs, |
| 595 | }; |
| 596 | |
Qipeng Zha | 8cc7fb4 | 2015-12-11 22:44:59 +0800 | [diff] [blame] | 597 | static struct resource punit_res_array[] = { |
| 598 | /* Punit BIOS */ |
| 599 | { |
| 600 | .flags = IORESOURCE_MEM, |
| 601 | }, |
| 602 | { |
| 603 | .flags = IORESOURCE_MEM, |
| 604 | }, |
| 605 | /* Punit ISP */ |
| 606 | { |
| 607 | .flags = IORESOURCE_MEM, |
| 608 | }, |
| 609 | { |
| 610 | .flags = IORESOURCE_MEM, |
| 611 | }, |
| 612 | /* Punit GTD */ |
qipeng.zha | 0a8b835 | 2015-06-27 00:32:15 +0800 | [diff] [blame] | 613 | { |
| 614 | .flags = IORESOURCE_MEM, |
| 615 | }, |
| 616 | { |
| 617 | .flags = IORESOURCE_MEM, |
| 618 | }, |
| 619 | }; |
| 620 | |
| 621 | #define TCO_RESOURCE_ACPI_IO 0 |
| 622 | #define TCO_RESOURCE_SMI_EN_IO 1 |
| 623 | #define TCO_RESOURCE_GCR_MEM 2 |
| 624 | static struct resource tco_res[] = { |
| 625 | /* ACPI - TCO */ |
| 626 | { |
| 627 | .flags = IORESOURCE_IO, |
| 628 | }, |
| 629 | /* ACPI - SMI */ |
| 630 | { |
| 631 | .flags = IORESOURCE_IO, |
| 632 | }, |
| 633 | /* GCS */ |
| 634 | { |
| 635 | .flags = IORESOURCE_MEM, |
| 636 | }, |
| 637 | }; |
| 638 | |
Matt Fleming | 420b54d | 2015-08-06 13:46:24 +0100 | [diff] [blame] | 639 | static struct itco_wdt_platform_data tco_info = { |
qipeng.zha | 0a8b835 | 2015-06-27 00:32:15 +0800 | [diff] [blame] | 640 | .name = "Apollo Lake SoC", |
Yong, Jonathan | 334da2d | 2016-06-17 00:33:32 +0000 | [diff] [blame] | 641 | .version = 5, |
qipeng.zha | 0a8b835 | 2015-06-27 00:32:15 +0800 | [diff] [blame] | 642 | }; |
| 643 | |
Souvik Kumar Chakravarty | 48c1917 | 2016-01-12 16:02:54 +0530 | [diff] [blame] | 644 | #define TELEMETRY_RESOURCE_PUNIT_SSRAM 0 |
| 645 | #define TELEMETRY_RESOURCE_PMC_SSRAM 1 |
| 646 | static struct resource telemetry_res[] = { |
| 647 | /*Telemetry*/ |
| 648 | { |
| 649 | .flags = IORESOURCE_MEM, |
| 650 | }, |
| 651 | { |
| 652 | .flags = IORESOURCE_MEM, |
| 653 | }, |
| 654 | }; |
| 655 | |
qipeng.zha | 0a8b835 | 2015-06-27 00:32:15 +0800 | [diff] [blame] | 656 | static int ipc_create_punit_device(void) |
| 657 | { |
| 658 | struct platform_device *pdev; |
Axel Lin | ea1a76b | 2016-09-24 11:54:08 +0800 | [diff] [blame] | 659 | const struct platform_device_info pdevinfo = { |
| 660 | .parent = ipcdev.dev, |
| 661 | .name = PUNIT_DEVICE_NAME, |
| 662 | .id = -1, |
| 663 | .res = punit_res_array, |
| 664 | .num_res = ARRAY_SIZE(punit_res_array), |
| 665 | }; |
qipeng.zha | 0a8b835 | 2015-06-27 00:32:15 +0800 | [diff] [blame] | 666 | |
Axel Lin | ea1a76b | 2016-09-24 11:54:08 +0800 | [diff] [blame] | 667 | pdev = platform_device_register_full(&pdevinfo); |
| 668 | if (IS_ERR(pdev)) |
| 669 | return PTR_ERR(pdev); |
qipeng.zha | 0a8b835 | 2015-06-27 00:32:15 +0800 | [diff] [blame] | 670 | |
qipeng.zha | 0a8b835 | 2015-06-27 00:32:15 +0800 | [diff] [blame] | 671 | ipcdev.punit_dev = pdev; |
| 672 | |
| 673 | return 0; |
qipeng.zha | 0a8b835 | 2015-06-27 00:32:15 +0800 | [diff] [blame] | 674 | } |
| 675 | |
| 676 | static int ipc_create_tco_device(void) |
| 677 | { |
| 678 | struct platform_device *pdev; |
| 679 | struct resource *res; |
Axel Lin | ea1a76b | 2016-09-24 11:54:08 +0800 | [diff] [blame] | 680 | const struct platform_device_info pdevinfo = { |
| 681 | .parent = ipcdev.dev, |
| 682 | .name = TCO_DEVICE_NAME, |
| 683 | .id = -1, |
| 684 | .res = tco_res, |
| 685 | .num_res = ARRAY_SIZE(tco_res), |
| 686 | .data = &tco_info, |
| 687 | .size_data = sizeof(tco_info), |
| 688 | }; |
qipeng.zha | 0a8b835 | 2015-06-27 00:32:15 +0800 | [diff] [blame] | 689 | |
| 690 | res = tco_res + TCO_RESOURCE_ACPI_IO; |
qipeng.zha | b78fb51 | 2015-07-07 00:04:45 +0800 | [diff] [blame] | 691 | res->start = ipcdev.acpi_io_base + TCO_BASE_OFFSET; |
qipeng.zha | 0a8b835 | 2015-06-27 00:32:15 +0800 | [diff] [blame] | 692 | res->end = res->start + TCO_REGS_SIZE - 1; |
| 693 | |
| 694 | res = tco_res + TCO_RESOURCE_SMI_EN_IO; |
qipeng.zha | b78fb51 | 2015-07-07 00:04:45 +0800 | [diff] [blame] | 695 | res->start = ipcdev.acpi_io_base + SMI_EN_OFFSET; |
qipeng.zha | 0a8b835 | 2015-06-27 00:32:15 +0800 | [diff] [blame] | 696 | res->end = res->start + SMI_EN_SIZE - 1; |
| 697 | |
| 698 | res = tco_res + TCO_RESOURCE_GCR_MEM; |
Yong, Jonathan | 334da2d | 2016-06-17 00:33:32 +0000 | [diff] [blame] | 699 | res->start = ipcdev.gcr_base + TCO_PMC_OFFSET; |
| 700 | res->end = res->start + TCO_PMC_SIZE - 1; |
qipeng.zha | 0a8b835 | 2015-06-27 00:32:15 +0800 | [diff] [blame] | 701 | |
Axel Lin | ea1a76b | 2016-09-24 11:54:08 +0800 | [diff] [blame] | 702 | pdev = platform_device_register_full(&pdevinfo); |
| 703 | if (IS_ERR(pdev)) |
| 704 | return PTR_ERR(pdev); |
qipeng.zha | 0a8b835 | 2015-06-27 00:32:15 +0800 | [diff] [blame] | 705 | |
qipeng.zha | 0a8b835 | 2015-06-27 00:32:15 +0800 | [diff] [blame] | 706 | ipcdev.tco_dev = pdev; |
| 707 | |
| 708 | return 0; |
qipeng.zha | 0a8b835 | 2015-06-27 00:32:15 +0800 | [diff] [blame] | 709 | } |
| 710 | |
Souvik Kumar Chakravarty | 48c1917 | 2016-01-12 16:02:54 +0530 | [diff] [blame] | 711 | static int ipc_create_telemetry_device(void) |
| 712 | { |
| 713 | struct platform_device *pdev; |
| 714 | struct resource *res; |
Axel Lin | ea1a76b | 2016-09-24 11:54:08 +0800 | [diff] [blame] | 715 | const struct platform_device_info pdevinfo = { |
| 716 | .parent = ipcdev.dev, |
| 717 | .name = TELEMETRY_DEVICE_NAME, |
| 718 | .id = -1, |
| 719 | .res = telemetry_res, |
| 720 | .num_res = ARRAY_SIZE(telemetry_res), |
| 721 | }; |
Souvik Kumar Chakravarty | 48c1917 | 2016-01-12 16:02:54 +0530 | [diff] [blame] | 722 | |
| 723 | res = telemetry_res + TELEMETRY_RESOURCE_PUNIT_SSRAM; |
| 724 | res->start = ipcdev.telem_punit_ssram_base; |
| 725 | res->end = res->start + ipcdev.telem_punit_ssram_size - 1; |
| 726 | |
| 727 | res = telemetry_res + TELEMETRY_RESOURCE_PMC_SSRAM; |
| 728 | res->start = ipcdev.telem_pmc_ssram_base; |
| 729 | res->end = res->start + ipcdev.telem_pmc_ssram_size - 1; |
| 730 | |
Axel Lin | ea1a76b | 2016-09-24 11:54:08 +0800 | [diff] [blame] | 731 | pdev = platform_device_register_full(&pdevinfo); |
| 732 | if (IS_ERR(pdev)) |
| 733 | return PTR_ERR(pdev); |
Souvik Kumar Chakravarty | 48c1917 | 2016-01-12 16:02:54 +0530 | [diff] [blame] | 734 | |
Souvik Kumar Chakravarty | 48c1917 | 2016-01-12 16:02:54 +0530 | [diff] [blame] | 735 | ipcdev.telemetry_dev = pdev; |
| 736 | |
| 737 | return 0; |
Souvik Kumar Chakravarty | 48c1917 | 2016-01-12 16:02:54 +0530 | [diff] [blame] | 738 | } |
| 739 | |
qipeng.zha | 0a8b835 | 2015-06-27 00:32:15 +0800 | [diff] [blame] | 740 | static int ipc_create_pmc_devices(void) |
| 741 | { |
| 742 | int ret; |
| 743 | |
Mika Westerberg | bba6529 | 2016-09-20 15:30:54 +0300 | [diff] [blame] | 744 | /* If we have ACPI based watchdog use that instead */ |
| 745 | if (!acpi_has_watchdog()) { |
| 746 | ret = ipc_create_tco_device(); |
| 747 | if (ret) { |
| 748 | dev_err(ipcdev.dev, "Failed to add tco platform device\n"); |
| 749 | return ret; |
| 750 | } |
qipeng.zha | 0a8b835 | 2015-06-27 00:32:15 +0800 | [diff] [blame] | 751 | } |
Mika Westerberg | bba6529 | 2016-09-20 15:30:54 +0300 | [diff] [blame] | 752 | |
qipeng.zha | 0a8b835 | 2015-06-27 00:32:15 +0800 | [diff] [blame] | 753 | ret = ipc_create_punit_device(); |
| 754 | if (ret) { |
| 755 | dev_err(ipcdev.dev, "Failed to add punit platform device\n"); |
| 756 | platform_device_unregister(ipcdev.tco_dev); |
| 757 | } |
Souvik Kumar Chakravarty | 48c1917 | 2016-01-12 16:02:54 +0530 | [diff] [blame] | 758 | |
| 759 | if (!ipcdev.telem_res_inval) { |
| 760 | ret = ipc_create_telemetry_device(); |
| 761 | if (ret) |
| 762 | dev_warn(ipcdev.dev, |
| 763 | "Failed to add telemetry platform device\n"); |
| 764 | } |
| 765 | |
qipeng.zha | 0a8b835 | 2015-06-27 00:32:15 +0800 | [diff] [blame] | 766 | return ret; |
| 767 | } |
| 768 | |
| 769 | static int ipc_plat_get_res(struct platform_device *pdev) |
| 770 | { |
Qipeng Zha | 8cc7fb4 | 2015-12-11 22:44:59 +0800 | [diff] [blame] | 771 | struct resource *res, *punit_res; |
qipeng.zha | 0a8b835 | 2015-06-27 00:32:15 +0800 | [diff] [blame] | 772 | void __iomem *addr; |
| 773 | int size; |
| 774 | |
| 775 | res = platform_get_resource(pdev, IORESOURCE_IO, |
| 776 | PLAT_RESOURCE_ACPI_IO_INDEX); |
| 777 | if (!res) { |
| 778 | dev_err(&pdev->dev, "Failed to get io resource\n"); |
| 779 | return -ENXIO; |
| 780 | } |
| 781 | size = resource_size(res); |
qipeng.zha | b78fb51 | 2015-07-07 00:04:45 +0800 | [diff] [blame] | 782 | ipcdev.acpi_io_base = res->start; |
qipeng.zha | 0a8b835 | 2015-06-27 00:32:15 +0800 | [diff] [blame] | 783 | ipcdev.acpi_io_size = size; |
Qipeng Zha | 8cc7fb4 | 2015-12-11 22:44:59 +0800 | [diff] [blame] | 784 | dev_info(&pdev->dev, "io res: %pR\n", res); |
qipeng.zha | 0a8b835 | 2015-06-27 00:32:15 +0800 | [diff] [blame] | 785 | |
Qipeng Zha | 8cc7fb4 | 2015-12-11 22:44:59 +0800 | [diff] [blame] | 786 | punit_res = punit_res_array; |
Aubrey Li | 5d07163 | 2016-03-31 14:28:09 -0500 | [diff] [blame] | 787 | /* This is index 0 to cover BIOS data register */ |
qipeng.zha | 0a8b835 | 2015-06-27 00:32:15 +0800 | [diff] [blame] | 788 | res = platform_get_resource(pdev, IORESOURCE_MEM, |
Qipeng Zha | 8cc7fb4 | 2015-12-11 22:44:59 +0800 | [diff] [blame] | 789 | PLAT_RESOURCE_BIOS_DATA_INDEX); |
qipeng.zha | 0a8b835 | 2015-06-27 00:32:15 +0800 | [diff] [blame] | 790 | if (!res) { |
Qipeng Zha | 8cc7fb4 | 2015-12-11 22:44:59 +0800 | [diff] [blame] | 791 | dev_err(&pdev->dev, "Failed to get res of punit BIOS data\n"); |
qipeng.zha | 0a8b835 | 2015-06-27 00:32:15 +0800 | [diff] [blame] | 792 | return -ENXIO; |
| 793 | } |
Qipeng Zha | 8cc7fb4 | 2015-12-11 22:44:59 +0800 | [diff] [blame] | 794 | *punit_res = *res; |
| 795 | dev_info(&pdev->dev, "punit BIOS data res: %pR\n", res); |
qipeng.zha | 0a8b835 | 2015-06-27 00:32:15 +0800 | [diff] [blame] | 796 | |
Aubrey Li | 5d07163 | 2016-03-31 14:28:09 -0500 | [diff] [blame] | 797 | /* This is index 1 to cover BIOS interface register */ |
qipeng.zha | 0a8b835 | 2015-06-27 00:32:15 +0800 | [diff] [blame] | 798 | res = platform_get_resource(pdev, IORESOURCE_MEM, |
Qipeng Zha | 8cc7fb4 | 2015-12-11 22:44:59 +0800 | [diff] [blame] | 799 | PLAT_RESOURCE_BIOS_IFACE_INDEX); |
qipeng.zha | 0a8b835 | 2015-06-27 00:32:15 +0800 | [diff] [blame] | 800 | if (!res) { |
Qipeng Zha | 8cc7fb4 | 2015-12-11 22:44:59 +0800 | [diff] [blame] | 801 | dev_err(&pdev->dev, "Failed to get res of punit BIOS iface\n"); |
qipeng.zha | 0a8b835 | 2015-06-27 00:32:15 +0800 | [diff] [blame] | 802 | return -ENXIO; |
| 803 | } |
Qipeng Zha | 8cc7fb4 | 2015-12-11 22:44:59 +0800 | [diff] [blame] | 804 | *++punit_res = *res; |
| 805 | dev_info(&pdev->dev, "punit BIOS interface res: %pR\n", res); |
| 806 | |
Aubrey Li | 5d07163 | 2016-03-31 14:28:09 -0500 | [diff] [blame] | 807 | /* This is index 2 to cover ISP data register, optional */ |
Qipeng Zha | 8cc7fb4 | 2015-12-11 22:44:59 +0800 | [diff] [blame] | 808 | res = platform_get_resource(pdev, IORESOURCE_MEM, |
| 809 | PLAT_RESOURCE_ISP_DATA_INDEX); |
Aubrey Li | 5d07163 | 2016-03-31 14:28:09 -0500 | [diff] [blame] | 810 | ++punit_res; |
| 811 | if (res) { |
| 812 | *punit_res = *res; |
| 813 | dev_info(&pdev->dev, "punit ISP data res: %pR\n", res); |
Qipeng Zha | 8cc7fb4 | 2015-12-11 22:44:59 +0800 | [diff] [blame] | 814 | } |
Qipeng Zha | 8cc7fb4 | 2015-12-11 22:44:59 +0800 | [diff] [blame] | 815 | |
Aubrey Li | 5d07163 | 2016-03-31 14:28:09 -0500 | [diff] [blame] | 816 | /* This is index 3 to cover ISP interface register, optional */ |
Qipeng Zha | 8cc7fb4 | 2015-12-11 22:44:59 +0800 | [diff] [blame] | 817 | res = platform_get_resource(pdev, IORESOURCE_MEM, |
| 818 | PLAT_RESOURCE_ISP_IFACE_INDEX); |
Aubrey Li | 5d07163 | 2016-03-31 14:28:09 -0500 | [diff] [blame] | 819 | ++punit_res; |
| 820 | if (res) { |
| 821 | *punit_res = *res; |
| 822 | dev_info(&pdev->dev, "punit ISP interface res: %pR\n", res); |
Qipeng Zha | 8cc7fb4 | 2015-12-11 22:44:59 +0800 | [diff] [blame] | 823 | } |
Qipeng Zha | 8cc7fb4 | 2015-12-11 22:44:59 +0800 | [diff] [blame] | 824 | |
Aubrey Li | 5d07163 | 2016-03-31 14:28:09 -0500 | [diff] [blame] | 825 | /* This is index 4 to cover GTD data register, optional */ |
Qipeng Zha | 8cc7fb4 | 2015-12-11 22:44:59 +0800 | [diff] [blame] | 826 | res = platform_get_resource(pdev, IORESOURCE_MEM, |
| 827 | PLAT_RESOURCE_GTD_DATA_INDEX); |
Aubrey Li | 5d07163 | 2016-03-31 14:28:09 -0500 | [diff] [blame] | 828 | ++punit_res; |
| 829 | if (res) { |
| 830 | *punit_res = *res; |
| 831 | dev_info(&pdev->dev, "punit GTD data res: %pR\n", res); |
Qipeng Zha | 8cc7fb4 | 2015-12-11 22:44:59 +0800 | [diff] [blame] | 832 | } |
Qipeng Zha | 8cc7fb4 | 2015-12-11 22:44:59 +0800 | [diff] [blame] | 833 | |
Aubrey Li | 5d07163 | 2016-03-31 14:28:09 -0500 | [diff] [blame] | 834 | /* This is index 5 to cover GTD interface register, optional */ |
Qipeng Zha | 8cc7fb4 | 2015-12-11 22:44:59 +0800 | [diff] [blame] | 835 | res = platform_get_resource(pdev, IORESOURCE_MEM, |
| 836 | PLAT_RESOURCE_GTD_IFACE_INDEX); |
Aubrey Li | 5d07163 | 2016-03-31 14:28:09 -0500 | [diff] [blame] | 837 | ++punit_res; |
| 838 | if (res) { |
| 839 | *punit_res = *res; |
| 840 | dev_info(&pdev->dev, "punit GTD interface res: %pR\n", res); |
Qipeng Zha | 8cc7fb4 | 2015-12-11 22:44:59 +0800 | [diff] [blame] | 841 | } |
qipeng.zha | 0a8b835 | 2015-06-27 00:32:15 +0800 | [diff] [blame] | 842 | |
| 843 | res = platform_get_resource(pdev, IORESOURCE_MEM, |
| 844 | PLAT_RESOURCE_IPC_INDEX); |
| 845 | if (!res) { |
| 846 | dev_err(&pdev->dev, "Failed to get ipc resource\n"); |
| 847 | return -ENXIO; |
| 848 | } |
Shanth Murthy | 76062b4 | 2017-02-13 04:02:52 -0800 | [diff] [blame] | 849 | size = PLAT_RESOURCE_IPC_SIZE + PLAT_RESOURCE_GCR_SIZE; |
| 850 | |
qipeng.zha | 0a8b835 | 2015-06-27 00:32:15 +0800 | [diff] [blame] | 851 | if (!request_mem_region(res->start, size, pdev->name)) { |
| 852 | dev_err(&pdev->dev, "Failed to request ipc resource\n"); |
| 853 | return -EBUSY; |
| 854 | } |
| 855 | addr = ioremap_nocache(res->start, size); |
| 856 | if (!addr) { |
| 857 | dev_err(&pdev->dev, "I/O memory remapping failed\n"); |
| 858 | release_mem_region(res->start, size); |
| 859 | return -ENOMEM; |
| 860 | } |
| 861 | ipcdev.ipc_base = addr; |
| 862 | |
Qipeng Zha | 1a2f25d | 2016-02-18 02:03:37 +0800 | [diff] [blame] | 863 | ipcdev.gcr_base = res->start + PLAT_RESOURCE_GCR_OFFSET; |
Kuppuswamy Sathyanarayanan | 4967020 | 2017-04-09 15:00:17 -0700 | [diff] [blame^] | 864 | ipcdev.gcr_mem_base = addr + PLAT_RESOURCE_GCR_OFFSET; |
qipeng.zha | 0a8b835 | 2015-06-27 00:32:15 +0800 | [diff] [blame] | 865 | ipcdev.gcr_size = PLAT_RESOURCE_GCR_SIZE; |
Qipeng Zha | 8cc7fb4 | 2015-12-11 22:44:59 +0800 | [diff] [blame] | 866 | dev_info(&pdev->dev, "ipc res: %pR\n", res); |
qipeng.zha | 0a8b835 | 2015-06-27 00:32:15 +0800 | [diff] [blame] | 867 | |
Souvik Kumar Chakravarty | 48c1917 | 2016-01-12 16:02:54 +0530 | [diff] [blame] | 868 | ipcdev.telem_res_inval = 0; |
| 869 | res = platform_get_resource(pdev, IORESOURCE_MEM, |
| 870 | PLAT_RESOURCE_TELEM_SSRAM_INDEX); |
| 871 | if (!res) { |
| 872 | dev_err(&pdev->dev, "Failed to get telemetry ssram resource\n"); |
| 873 | ipcdev.telem_res_inval = 1; |
| 874 | } else { |
| 875 | ipcdev.telem_punit_ssram_base = res->start + |
| 876 | TELEM_PUNIT_SSRAM_OFFSET; |
| 877 | ipcdev.telem_punit_ssram_size = TELEM_SSRAM_SIZE; |
| 878 | ipcdev.telem_pmc_ssram_base = res->start + |
| 879 | TELEM_PMC_SSRAM_OFFSET; |
| 880 | ipcdev.telem_pmc_ssram_size = TELEM_SSRAM_SIZE; |
| 881 | dev_info(&pdev->dev, "telemetry ssram res: %pR\n", res); |
| 882 | } |
| 883 | |
qipeng.zha | 0a8b835 | 2015-06-27 00:32:15 +0800 | [diff] [blame] | 884 | return 0; |
| 885 | } |
| 886 | |
Shanth Murthy | 76062b4 | 2017-02-13 04:02:52 -0800 | [diff] [blame] | 887 | /** |
| 888 | * intel_pmc_s0ix_counter_read() - Read S0ix residency. |
| 889 | * @data: Out param that contains current S0ix residency count. |
| 890 | * |
| 891 | * Return: an error code or 0 on success. |
| 892 | */ |
| 893 | int intel_pmc_s0ix_counter_read(u64 *data) |
| 894 | { |
| 895 | u64 deep, shlw; |
| 896 | |
| 897 | if (!ipcdev.has_gcr_regs) |
| 898 | return -EACCES; |
| 899 | |
| 900 | deep = gcr_data_readq(GCR_TELEM_DEEP_S0IX_OFFSET); |
| 901 | shlw = gcr_data_readq(GCR_TELEM_SHLW_S0IX_OFFSET); |
| 902 | |
| 903 | *data = S0IX_RESIDENCY_IN_USECS(deep, shlw); |
| 904 | |
| 905 | return 0; |
| 906 | } |
| 907 | EXPORT_SYMBOL_GPL(intel_pmc_s0ix_counter_read); |
| 908 | |
qipeng.zha | 0a8b835 | 2015-06-27 00:32:15 +0800 | [diff] [blame] | 909 | #ifdef CONFIG_ACPI |
| 910 | static const struct acpi_device_id ipc_acpi_ids[] = { |
| 911 | { "INT34D2", 0}, |
| 912 | { } |
| 913 | }; |
| 914 | MODULE_DEVICE_TABLE(acpi, ipc_acpi_ids); |
| 915 | #endif |
| 916 | |
| 917 | static int ipc_plat_probe(struct platform_device *pdev) |
| 918 | { |
| 919 | struct resource *res; |
| 920 | int ret; |
| 921 | |
| 922 | ipcdev.dev = &pdev->dev; |
| 923 | ipcdev.irq_mode = IPC_TRIGGER_MODE_IRQ; |
| 924 | init_completion(&ipcdev.cmd_complete); |
| 925 | |
| 926 | ipcdev.irq = platform_get_irq(pdev, 0); |
| 927 | if (ipcdev.irq < 0) { |
| 928 | dev_err(&pdev->dev, "Failed to get irq\n"); |
| 929 | return -EINVAL; |
| 930 | } |
| 931 | |
| 932 | ret = ipc_plat_get_res(pdev); |
| 933 | if (ret) { |
| 934 | dev_err(&pdev->dev, "Failed to request resource\n"); |
| 935 | return ret; |
| 936 | } |
| 937 | |
| 938 | ret = ipc_create_pmc_devices(); |
| 939 | if (ret) { |
| 940 | dev_err(&pdev->dev, "Failed to create pmc devices\n"); |
| 941 | goto err_device; |
| 942 | } |
| 943 | |
Qipeng Zha | 1f1ae99 | 2016-02-18 02:03:38 +0800 | [diff] [blame] | 944 | if (request_irq(ipcdev.irq, ioc, IRQF_NO_SUSPEND, |
| 945 | "intel_pmc_ipc", &ipcdev)) { |
qipeng.zha | 0a8b835 | 2015-06-27 00:32:15 +0800 | [diff] [blame] | 946 | dev_err(&pdev->dev, "Failed to request irq\n"); |
| 947 | ret = -EBUSY; |
| 948 | goto err_irq; |
| 949 | } |
| 950 | |
| 951 | ret = sysfs_create_group(&pdev->dev.kobj, &intel_ipc_group); |
| 952 | if (ret) { |
| 953 | dev_err(&pdev->dev, "Failed to create sysfs group %d\n", |
| 954 | ret); |
| 955 | goto err_sys; |
| 956 | } |
| 957 | |
Shanth Murthy | 76062b4 | 2017-02-13 04:02:52 -0800 | [diff] [blame] | 958 | ipcdev.has_gcr_regs = true; |
| 959 | |
qipeng.zha | 0a8b835 | 2015-06-27 00:32:15 +0800 | [diff] [blame] | 960 | return 0; |
| 961 | err_sys: |
| 962 | free_irq(ipcdev.irq, &ipcdev); |
| 963 | err_irq: |
| 964 | platform_device_unregister(ipcdev.tco_dev); |
| 965 | platform_device_unregister(ipcdev.punit_dev); |
Souvik Kumar Chakravarty | 48c1917 | 2016-01-12 16:02:54 +0530 | [diff] [blame] | 966 | platform_device_unregister(ipcdev.telemetry_dev); |
qipeng.zha | 0a8b835 | 2015-06-27 00:32:15 +0800 | [diff] [blame] | 967 | err_device: |
| 968 | iounmap(ipcdev.ipc_base); |
| 969 | res = platform_get_resource(pdev, IORESOURCE_MEM, |
| 970 | PLAT_RESOURCE_IPC_INDEX); |
Shanth Murthy | 76062b4 | 2017-02-13 04:02:52 -0800 | [diff] [blame] | 971 | if (res) { |
| 972 | release_mem_region(res->start, |
| 973 | PLAT_RESOURCE_IPC_SIZE + |
| 974 | PLAT_RESOURCE_GCR_SIZE); |
| 975 | } |
qipeng.zha | 0a8b835 | 2015-06-27 00:32:15 +0800 | [diff] [blame] | 976 | return ret; |
| 977 | } |
| 978 | |
| 979 | static int ipc_plat_remove(struct platform_device *pdev) |
| 980 | { |
| 981 | struct resource *res; |
| 982 | |
| 983 | sysfs_remove_group(&pdev->dev.kobj, &intel_ipc_group); |
| 984 | free_irq(ipcdev.irq, &ipcdev); |
| 985 | platform_device_unregister(ipcdev.tco_dev); |
| 986 | platform_device_unregister(ipcdev.punit_dev); |
Souvik Kumar Chakravarty | 48c1917 | 2016-01-12 16:02:54 +0530 | [diff] [blame] | 987 | platform_device_unregister(ipcdev.telemetry_dev); |
qipeng.zha | 0a8b835 | 2015-06-27 00:32:15 +0800 | [diff] [blame] | 988 | iounmap(ipcdev.ipc_base); |
| 989 | res = platform_get_resource(pdev, IORESOURCE_MEM, |
| 990 | PLAT_RESOURCE_IPC_INDEX); |
Shanth Murthy | 76062b4 | 2017-02-13 04:02:52 -0800 | [diff] [blame] | 991 | if (res) { |
| 992 | release_mem_region(res->start, |
| 993 | PLAT_RESOURCE_IPC_SIZE + |
| 994 | PLAT_RESOURCE_GCR_SIZE); |
| 995 | } |
qipeng.zha | 0a8b835 | 2015-06-27 00:32:15 +0800 | [diff] [blame] | 996 | ipcdev.dev = NULL; |
| 997 | return 0; |
| 998 | } |
| 999 | |
| 1000 | static struct platform_driver ipc_plat_driver = { |
| 1001 | .remove = ipc_plat_remove, |
| 1002 | .probe = ipc_plat_probe, |
| 1003 | .driver = { |
| 1004 | .name = "pmc-ipc-plat", |
| 1005 | .acpi_match_table = ACPI_PTR(ipc_acpi_ids), |
| 1006 | }, |
| 1007 | }; |
| 1008 | |
| 1009 | static int __init intel_pmc_ipc_init(void) |
| 1010 | { |
| 1011 | int ret; |
| 1012 | |
| 1013 | ret = platform_driver_register(&ipc_plat_driver); |
| 1014 | if (ret) { |
| 1015 | pr_err("Failed to register PMC ipc platform driver\n"); |
| 1016 | return ret; |
| 1017 | } |
| 1018 | ret = pci_register_driver(&ipc_pci_driver); |
| 1019 | if (ret) { |
| 1020 | pr_err("Failed to register PMC ipc pci driver\n"); |
| 1021 | platform_driver_unregister(&ipc_plat_driver); |
| 1022 | return ret; |
| 1023 | } |
| 1024 | return ret; |
| 1025 | } |
| 1026 | |
| 1027 | static void __exit intel_pmc_ipc_exit(void) |
| 1028 | { |
| 1029 | pci_unregister_driver(&ipc_pci_driver); |
| 1030 | platform_driver_unregister(&ipc_plat_driver); |
| 1031 | } |
| 1032 | |
| 1033 | MODULE_AUTHOR("Zha Qipeng <qipeng.zha@intel.com>"); |
| 1034 | MODULE_DESCRIPTION("Intel PMC IPC driver"); |
| 1035 | MODULE_LICENSE("GPL"); |
| 1036 | |
| 1037 | /* Some modules are dependent on this, so init earlier */ |
| 1038 | fs_initcall(intel_pmc_ipc_init); |
| 1039 | module_exit(intel_pmc_ipc_exit); |