blob: 3c5c302dba41b8a39dfc75a0969bc025749c9b3d [file] [log] [blame]
Sudeep Duttaa27bad2013-09-05 16:42:06 -07001/*
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>
33#include "../common/mic_device.h"
34#include "mic_device.h"
35
36static struct mic_driver *g_drv;
37static struct mic_irq *shutdown_cookie;
38
39static void mic_notify_host(u8 state)
40{
41 struct mic_driver *mdrv = g_drv;
42 struct mic_bootparam __iomem *bootparam = mdrv->dp;
43
44 iowrite8(state, &bootparam->shutdown_status);
45 dev_dbg(mdrv->dev, "%s %d system_state %d\n",
46 __func__, __LINE__, state);
47 mic_send_intr(&mdrv->mdev, ioread8(&bootparam->c2h_shutdown_db));
48}
49
50static int mic_panic_event(struct notifier_block *this, unsigned long event,
51 void *ptr)
52{
53 struct mic_driver *mdrv = g_drv;
54 struct mic_bootparam __iomem *bootparam = mdrv->dp;
55
56 iowrite8(-1, &bootparam->h2c_config_db);
57 iowrite8(-1, &bootparam->h2c_shutdown_db);
58 mic_notify_host(MIC_CRASHED);
59 return NOTIFY_DONE;
60}
61
62static struct notifier_block mic_panic = {
63 .notifier_call = mic_panic_event,
64};
65
66static irqreturn_t mic_shutdown_isr(int irq, void *data)
67{
68 struct mic_driver *mdrv = g_drv;
69 struct mic_bootparam __iomem *bootparam = mdrv->dp;
70
71 mic_ack_interrupt(&g_drv->mdev);
72 if (ioread8(&bootparam->shutdown_card))
73 orderly_poweroff(true);
74 return IRQ_HANDLED;
75}
76
77static int mic_shutdown_init(void)
78{
79 int rc = 0;
80 struct mic_driver *mdrv = g_drv;
81 struct mic_bootparam __iomem *bootparam = mdrv->dp;
82 int shutdown_db;
83
84 shutdown_db = mic_next_card_db();
85 shutdown_cookie = mic_request_card_irq(mic_shutdown_isr,
86 "Shutdown", mdrv, shutdown_db);
87 if (IS_ERR(shutdown_cookie))
88 rc = PTR_ERR(shutdown_cookie);
89 else
90 iowrite8(shutdown_db, &bootparam->h2c_shutdown_db);
91 return rc;
92}
93
94static void mic_shutdown_uninit(void)
95{
96 struct mic_driver *mdrv = g_drv;
97 struct mic_bootparam __iomem *bootparam = mdrv->dp;
98
99 iowrite8(-1, &bootparam->h2c_shutdown_db);
100 mic_free_card_irq(shutdown_cookie, mdrv);
101}
102
103static int __init mic_dp_init(void)
104{
105 struct mic_driver *mdrv = g_drv;
106 struct mic_device *mdev = &mdrv->mdev;
107 struct mic_bootparam __iomem *bootparam;
108 u64 lo, hi, dp_dma_addr;
109 u32 magic;
110
111 lo = mic_read_spad(&mdrv->mdev, MIC_DPLO_SPAD);
112 hi = mic_read_spad(&mdrv->mdev, MIC_DPHI_SPAD);
113
114 dp_dma_addr = lo | (hi << 32);
115 mdrv->dp = mic_card_map(mdev, dp_dma_addr, MIC_DP_SIZE);
116 if (!mdrv->dp) {
117 dev_err(mdrv->dev, "Cannot remap Aperture BAR\n");
118 return -ENOMEM;
119 }
120 bootparam = mdrv->dp;
121 magic = ioread32(&bootparam->magic);
122 if (MIC_MAGIC != magic) {
123 dev_err(mdrv->dev, "bootparam magic mismatch 0x%x\n", magic);
124 return -EIO;
125 }
126 return 0;
127}
128
129/* Uninitialize the device page */
130static void mic_dp_uninit(void)
131{
132 mic_card_unmap(&g_drv->mdev, g_drv->dp);
133}
134
135/**
136 * mic_request_card_irq - request an irq.
137 *
138 * @func: The callback function that handles the interrupt.
139 * @name: The ASCII name of the callee requesting the irq.
140 * @data: private data that is returned back when calling the
141 * function handler.
142 * @index: The doorbell index of the requester.
143 *
144 * returns: The cookie that is transparent to the caller. Passed
145 * back when calling mic_free_irq. An appropriate error code
146 * is returned on failure. Caller needs to use IS_ERR(return_val)
147 * to check for failure and PTR_ERR(return_val) to obtained the
148 * error code.
149 *
150 */
151struct mic_irq *mic_request_card_irq(irqreturn_t (*func)(int irq, void *data),
152 const char *name, void *data, int index)
153{
154 int rc = 0;
155 unsigned long cookie;
156 struct mic_driver *mdrv = g_drv;
157
158 rc = request_irq(mic_db_to_irq(mdrv, index), func,
159 0, name, data);
160 if (rc) {
161 dev_err(mdrv->dev, "request_irq failed rc = %d\n", rc);
162 goto err;
163 }
164 mdrv->irq_info.irq_usage_count[index]++;
165 cookie = index;
166 return (struct mic_irq *)cookie;
167err:
168 return ERR_PTR(rc);
169
170}
171
172/**
173 * mic_free_card_irq - free irq.
174 *
175 * @cookie: cookie obtained during a successful call to mic_request_irq
176 * @data: private data specified by the calling function during the
177 * mic_request_irq
178 *
179 * returns: none.
180 */
181void mic_free_card_irq(struct mic_irq *cookie, void *data)
182{
183 int index;
184 struct mic_driver *mdrv = g_drv;
185
186 index = (unsigned long)cookie & 0xFFFFU;
187 free_irq(mic_db_to_irq(mdrv, index), data);
188 mdrv->irq_info.irq_usage_count[index]--;
189}
190
191/**
192 * mic_next_card_db - Get the doorbell with minimum usage count.
193 *
194 * Returns the irq index.
195 */
196int mic_next_card_db(void)
197{
198 int i;
199 int index = 0;
200 struct mic_driver *mdrv = g_drv;
201
202 for (i = 0; i < mdrv->intr_info.num_intr; i++) {
203 if (mdrv->irq_info.irq_usage_count[i] <
204 mdrv->irq_info.irq_usage_count[index])
205 index = i;
206 }
207
208 return index;
209}
210
211/**
212 * mic_init_irq - Initialize irq information.
213 *
214 * Returns 0 in success. Appropriate error code on failure.
215 */
216static int mic_init_irq(void)
217{
218 struct mic_driver *mdrv = g_drv;
219
220 mdrv->irq_info.irq_usage_count = kzalloc((sizeof(u32) *
221 mdrv->intr_info.num_intr),
222 GFP_KERNEL);
223 if (!mdrv->irq_info.irq_usage_count)
224 return -ENOMEM;
225 return 0;
226}
227
228/**
229 * mic_uninit_irq - Uninitialize irq information.
230 *
231 * None.
232 */
233static void mic_uninit_irq(void)
234{
235 struct mic_driver *mdrv = g_drv;
236
237 kfree(mdrv->irq_info.irq_usage_count);
238}
239
240/*
241 * mic_driver_init - MIC driver initialization tasks.
242 *
243 * Returns 0 in success. Appropriate error code on failure.
244 */
245int __init mic_driver_init(struct mic_driver *mdrv)
246{
247 int rc;
248
249 g_drv = mdrv;
250 /*
251 * Unloading the card module is not supported. The MIC card module
252 * handles fundamental operations like host/card initiated shutdowns
253 * and informing the host about card crashes and cannot be unloaded.
254 */
255 if (!try_module_get(mdrv->dev->driver->owner)) {
256 rc = -ENODEV;
257 goto done;
258 }
259 rc = mic_dp_init();
260 if (rc)
261 goto put;
262 rc = mic_init_irq();
263 if (rc)
264 goto dp_uninit;
265 rc = mic_shutdown_init();
266 if (rc)
267 goto irq_uninit;
268 mic_create_card_debug_dir(mdrv);
269 atomic_notifier_chain_register(&panic_notifier_list, &mic_panic);
270done:
271 return rc;
272irq_uninit:
273 mic_uninit_irq();
274dp_uninit:
275 mic_dp_uninit();
276put:
277 module_put(mdrv->dev->driver->owner);
278 return rc;
279}
280
281/*
282 * mic_driver_uninit - MIC driver uninitialization tasks.
283 *
284 * Returns None
285 */
286void mic_driver_uninit(struct mic_driver *mdrv)
287{
288 mic_delete_card_debug_dir(mdrv);
289 /*
290 * Inform the host about the shutdown status i.e. poweroff/restart etc.
291 * The module cannot be unloaded so the only code path to call
292 * mic_devices_uninit(..) is the shutdown callback.
293 */
294 mic_notify_host(system_state);
295 mic_shutdown_uninit();
296 mic_uninit_irq();
297 mic_dp_uninit();
298 module_put(mdrv->dev->driver->owner);
299}