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