Sudeep Dutt | aa27bad | 2013-09-05 16:42:06 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Intel MIC Platform Software Stack (MPSS) |
| 3 | * |
| 4 | * Copyright(c) 2013 Intel Corporation. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License, version 2, as |
| 8 | * published by the Free Software Foundation. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, but |
| 11 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | * General Public License for more details. |
| 14 | * |
| 15 | * The full GNU General Public License is included in this distribution in |
| 16 | * the file called "COPYING". |
| 17 | * |
| 18 | * Disclaimer: The codes contained in these modules may be specific to |
| 19 | * the Intel Software Development Platform codenamed: Knights Ferry, and |
| 20 | * the Intel product codenamed: Knights Corner, and are not backward |
| 21 | * compatible with other Intel products. Additionally, Intel will NOT |
| 22 | * support the codes or instruction set in future products. |
| 23 | * |
| 24 | * Intel MIC Card driver. |
| 25 | * |
| 26 | */ |
| 27 | #include <linux/module.h> |
| 28 | #include <linux/pci.h> |
| 29 | #include <linux/interrupt.h> |
| 30 | #include <linux/reboot.h> |
| 31 | |
| 32 | #include <linux/mic_common.h> |
Sudeep Dutt | 4aa7996 | 2013-09-27 09:49:42 -0700 | [diff] [blame^] | 33 | #include "../common/mic_dev.h" |
Sudeep Dutt | aa27bad | 2013-09-05 16:42:06 -0700 | [diff] [blame] | 34 | #include "mic_device.h" |
Ashutosh Dixit | 2141c7c | 2013-09-05 16:42:28 -0700 | [diff] [blame] | 35 | #include "mic_virtio.h" |
Sudeep Dutt | aa27bad | 2013-09-05 16:42:06 -0700 | [diff] [blame] | 36 | |
| 37 | static struct mic_driver *g_drv; |
| 38 | static struct mic_irq *shutdown_cookie; |
| 39 | |
| 40 | static void mic_notify_host(u8 state) |
| 41 | { |
| 42 | struct mic_driver *mdrv = g_drv; |
| 43 | struct mic_bootparam __iomem *bootparam = mdrv->dp; |
| 44 | |
| 45 | iowrite8(state, &bootparam->shutdown_status); |
| 46 | dev_dbg(mdrv->dev, "%s %d system_state %d\n", |
| 47 | __func__, __LINE__, state); |
| 48 | mic_send_intr(&mdrv->mdev, ioread8(&bootparam->c2h_shutdown_db)); |
| 49 | } |
| 50 | |
| 51 | static int mic_panic_event(struct notifier_block *this, unsigned long event, |
| 52 | void *ptr) |
| 53 | { |
| 54 | struct mic_driver *mdrv = g_drv; |
| 55 | struct mic_bootparam __iomem *bootparam = mdrv->dp; |
| 56 | |
| 57 | iowrite8(-1, &bootparam->h2c_config_db); |
| 58 | iowrite8(-1, &bootparam->h2c_shutdown_db); |
| 59 | mic_notify_host(MIC_CRASHED); |
| 60 | return NOTIFY_DONE; |
| 61 | } |
| 62 | |
| 63 | static struct notifier_block mic_panic = { |
| 64 | .notifier_call = mic_panic_event, |
| 65 | }; |
| 66 | |
| 67 | static irqreturn_t mic_shutdown_isr(int irq, void *data) |
| 68 | { |
| 69 | struct mic_driver *mdrv = g_drv; |
| 70 | struct mic_bootparam __iomem *bootparam = mdrv->dp; |
| 71 | |
| 72 | mic_ack_interrupt(&g_drv->mdev); |
| 73 | if (ioread8(&bootparam->shutdown_card)) |
| 74 | orderly_poweroff(true); |
| 75 | return IRQ_HANDLED; |
| 76 | } |
| 77 | |
| 78 | static int mic_shutdown_init(void) |
| 79 | { |
| 80 | int rc = 0; |
| 81 | struct mic_driver *mdrv = g_drv; |
| 82 | struct mic_bootparam __iomem *bootparam = mdrv->dp; |
| 83 | int shutdown_db; |
| 84 | |
| 85 | shutdown_db = mic_next_card_db(); |
| 86 | shutdown_cookie = mic_request_card_irq(mic_shutdown_isr, |
| 87 | "Shutdown", mdrv, shutdown_db); |
| 88 | if (IS_ERR(shutdown_cookie)) |
| 89 | rc = PTR_ERR(shutdown_cookie); |
| 90 | else |
| 91 | iowrite8(shutdown_db, &bootparam->h2c_shutdown_db); |
| 92 | return rc; |
| 93 | } |
| 94 | |
| 95 | static void mic_shutdown_uninit(void) |
| 96 | { |
| 97 | struct mic_driver *mdrv = g_drv; |
| 98 | struct mic_bootparam __iomem *bootparam = mdrv->dp; |
| 99 | |
| 100 | iowrite8(-1, &bootparam->h2c_shutdown_db); |
| 101 | mic_free_card_irq(shutdown_cookie, mdrv); |
| 102 | } |
| 103 | |
| 104 | static int __init mic_dp_init(void) |
| 105 | { |
| 106 | struct mic_driver *mdrv = g_drv; |
| 107 | struct mic_device *mdev = &mdrv->mdev; |
| 108 | struct mic_bootparam __iomem *bootparam; |
| 109 | u64 lo, hi, dp_dma_addr; |
| 110 | u32 magic; |
| 111 | |
| 112 | lo = mic_read_spad(&mdrv->mdev, MIC_DPLO_SPAD); |
| 113 | hi = mic_read_spad(&mdrv->mdev, MIC_DPHI_SPAD); |
| 114 | |
| 115 | dp_dma_addr = lo | (hi << 32); |
| 116 | mdrv->dp = mic_card_map(mdev, dp_dma_addr, MIC_DP_SIZE); |
| 117 | if (!mdrv->dp) { |
| 118 | dev_err(mdrv->dev, "Cannot remap Aperture BAR\n"); |
| 119 | return -ENOMEM; |
| 120 | } |
| 121 | bootparam = mdrv->dp; |
| 122 | magic = ioread32(&bootparam->magic); |
| 123 | if (MIC_MAGIC != magic) { |
| 124 | dev_err(mdrv->dev, "bootparam magic mismatch 0x%x\n", magic); |
| 125 | return -EIO; |
| 126 | } |
| 127 | return 0; |
| 128 | } |
| 129 | |
| 130 | /* Uninitialize the device page */ |
| 131 | static void mic_dp_uninit(void) |
| 132 | { |
| 133 | mic_card_unmap(&g_drv->mdev, g_drv->dp); |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * mic_request_card_irq - request an irq. |
| 138 | * |
| 139 | * @func: The callback function that handles the interrupt. |
| 140 | * @name: The ASCII name of the callee requesting the irq. |
| 141 | * @data: private data that is returned back when calling the |
| 142 | * function handler. |
| 143 | * @index: The doorbell index of the requester. |
| 144 | * |
| 145 | * returns: The cookie that is transparent to the caller. Passed |
| 146 | * back when calling mic_free_irq. An appropriate error code |
| 147 | * is returned on failure. Caller needs to use IS_ERR(return_val) |
| 148 | * to check for failure and PTR_ERR(return_val) to obtained the |
| 149 | * error code. |
| 150 | * |
| 151 | */ |
| 152 | struct mic_irq *mic_request_card_irq(irqreturn_t (*func)(int irq, void *data), |
| 153 | const char *name, void *data, int index) |
| 154 | { |
| 155 | int rc = 0; |
| 156 | unsigned long cookie; |
| 157 | struct mic_driver *mdrv = g_drv; |
| 158 | |
| 159 | rc = request_irq(mic_db_to_irq(mdrv, index), func, |
| 160 | 0, name, data); |
| 161 | if (rc) { |
| 162 | dev_err(mdrv->dev, "request_irq failed rc = %d\n", rc); |
| 163 | goto err; |
| 164 | } |
| 165 | mdrv->irq_info.irq_usage_count[index]++; |
| 166 | cookie = index; |
| 167 | return (struct mic_irq *)cookie; |
| 168 | err: |
| 169 | return ERR_PTR(rc); |
| 170 | |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * mic_free_card_irq - free irq. |
| 175 | * |
| 176 | * @cookie: cookie obtained during a successful call to mic_request_irq |
| 177 | * @data: private data specified by the calling function during the |
| 178 | * mic_request_irq |
| 179 | * |
| 180 | * returns: none. |
| 181 | */ |
| 182 | void mic_free_card_irq(struct mic_irq *cookie, void *data) |
| 183 | { |
| 184 | int index; |
| 185 | struct mic_driver *mdrv = g_drv; |
| 186 | |
| 187 | index = (unsigned long)cookie & 0xFFFFU; |
| 188 | free_irq(mic_db_to_irq(mdrv, index), data); |
| 189 | mdrv->irq_info.irq_usage_count[index]--; |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * mic_next_card_db - Get the doorbell with minimum usage count. |
| 194 | * |
| 195 | * Returns the irq index. |
| 196 | */ |
| 197 | int mic_next_card_db(void) |
| 198 | { |
| 199 | int i; |
| 200 | int index = 0; |
| 201 | struct mic_driver *mdrv = g_drv; |
| 202 | |
| 203 | for (i = 0; i < mdrv->intr_info.num_intr; i++) { |
| 204 | if (mdrv->irq_info.irq_usage_count[i] < |
| 205 | mdrv->irq_info.irq_usage_count[index]) |
| 206 | index = i; |
| 207 | } |
| 208 | |
| 209 | return index; |
| 210 | } |
| 211 | |
| 212 | /** |
| 213 | * mic_init_irq - Initialize irq information. |
| 214 | * |
| 215 | * Returns 0 in success. Appropriate error code on failure. |
| 216 | */ |
| 217 | static int mic_init_irq(void) |
| 218 | { |
| 219 | struct mic_driver *mdrv = g_drv; |
| 220 | |
| 221 | mdrv->irq_info.irq_usage_count = kzalloc((sizeof(u32) * |
| 222 | mdrv->intr_info.num_intr), |
| 223 | GFP_KERNEL); |
| 224 | if (!mdrv->irq_info.irq_usage_count) |
| 225 | return -ENOMEM; |
| 226 | return 0; |
| 227 | } |
| 228 | |
| 229 | /** |
| 230 | * mic_uninit_irq - Uninitialize irq information. |
| 231 | * |
| 232 | * None. |
| 233 | */ |
| 234 | static void mic_uninit_irq(void) |
| 235 | { |
| 236 | struct mic_driver *mdrv = g_drv; |
| 237 | |
| 238 | kfree(mdrv->irq_info.irq_usage_count); |
| 239 | } |
| 240 | |
| 241 | /* |
| 242 | * mic_driver_init - MIC driver initialization tasks. |
| 243 | * |
| 244 | * Returns 0 in success. Appropriate error code on failure. |
| 245 | */ |
| 246 | int __init mic_driver_init(struct mic_driver *mdrv) |
| 247 | { |
| 248 | int rc; |
| 249 | |
| 250 | g_drv = mdrv; |
| 251 | /* |
| 252 | * Unloading the card module is not supported. The MIC card module |
| 253 | * handles fundamental operations like host/card initiated shutdowns |
| 254 | * and informing the host about card crashes and cannot be unloaded. |
| 255 | */ |
| 256 | if (!try_module_get(mdrv->dev->driver->owner)) { |
| 257 | rc = -ENODEV; |
| 258 | goto done; |
| 259 | } |
| 260 | rc = mic_dp_init(); |
| 261 | if (rc) |
| 262 | goto put; |
| 263 | rc = mic_init_irq(); |
| 264 | if (rc) |
| 265 | goto dp_uninit; |
| 266 | rc = mic_shutdown_init(); |
| 267 | if (rc) |
| 268 | goto irq_uninit; |
Ashutosh Dixit | 2141c7c | 2013-09-05 16:42:28 -0700 | [diff] [blame] | 269 | rc = mic_devices_init(mdrv); |
| 270 | if (rc) |
| 271 | goto shutdown_uninit; |
Sudeep Dutt | aa27bad | 2013-09-05 16:42:06 -0700 | [diff] [blame] | 272 | mic_create_card_debug_dir(mdrv); |
| 273 | atomic_notifier_chain_register(&panic_notifier_list, &mic_panic); |
| 274 | done: |
| 275 | return rc; |
Ashutosh Dixit | 2141c7c | 2013-09-05 16:42:28 -0700 | [diff] [blame] | 276 | shutdown_uninit: |
| 277 | mic_shutdown_uninit(); |
Sudeep Dutt | aa27bad | 2013-09-05 16:42:06 -0700 | [diff] [blame] | 278 | irq_uninit: |
| 279 | mic_uninit_irq(); |
| 280 | dp_uninit: |
| 281 | mic_dp_uninit(); |
| 282 | put: |
| 283 | module_put(mdrv->dev->driver->owner); |
| 284 | return rc; |
| 285 | } |
| 286 | |
| 287 | /* |
| 288 | * mic_driver_uninit - MIC driver uninitialization tasks. |
| 289 | * |
| 290 | * Returns None |
| 291 | */ |
| 292 | void mic_driver_uninit(struct mic_driver *mdrv) |
| 293 | { |
| 294 | mic_delete_card_debug_dir(mdrv); |
Ashutosh Dixit | 2141c7c | 2013-09-05 16:42:28 -0700 | [diff] [blame] | 295 | mic_devices_uninit(mdrv); |
Sudeep Dutt | aa27bad | 2013-09-05 16:42:06 -0700 | [diff] [blame] | 296 | /* |
| 297 | * Inform the host about the shutdown status i.e. poweroff/restart etc. |
| 298 | * The module cannot be unloaded so the only code path to call |
| 299 | * mic_devices_uninit(..) is the shutdown callback. |
| 300 | */ |
| 301 | mic_notify_host(system_state); |
| 302 | mic_shutdown_uninit(); |
| 303 | mic_uninit_irq(); |
| 304 | mic_dp_uninit(); |
| 305 | module_put(mdrv->dev->driver->owner); |
| 306 | } |