2 * Intel MIC Platform Software Stack (MPSS)
4 * Copyright(c) 2013 Intel Corporation.
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.
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.
15 * The full GNU General Public License is included in this distribution in
16 * the file called "COPYING".
18 * Intel MIC Host driver.
21 #include <linux/delay.h>
22 #include <linux/firmware.h>
23 #include <linux/pci.h>
24 #include <linux/kmod.h>
25 #include <linux/mic_common.h>
26 #include <linux/mic_bus.h>
27 #include "../bus/scif_bus.h"
28 #include "../common/mic_dev.h"
29 #include "mic_device.h"
31 #include "mic_virtio.h"
33 static inline struct mic_device *scdev_to_mdev(struct scif_hw_dev *scdev)
35 return dev_get_drvdata(scdev->dev.parent);
38 static void *__mic_dma_alloc(struct device *dev, size_t size,
39 dma_addr_t *dma_handle, gfp_t gfp,
40 struct dma_attrs *attrs)
42 struct scif_hw_dev *scdev = dev_get_drvdata(dev);
43 struct mic_device *mdev = scdev_to_mdev(scdev);
45 void *va = kmalloc(size, gfp);
48 tmp = mic_map_single(mdev, va, size);
49 if (dma_mapping_error(dev, tmp)) {
59 static void __mic_dma_free(struct device *dev, size_t size, void *vaddr,
60 dma_addr_t dma_handle, struct dma_attrs *attrs)
62 struct scif_hw_dev *scdev = dev_get_drvdata(dev);
63 struct mic_device *mdev = scdev_to_mdev(scdev);
65 mic_unmap_single(mdev, dma_handle, size);
70 __mic_dma_map_page(struct device *dev, struct page *page, unsigned long offset,
71 size_t size, enum dma_data_direction dir,
72 struct dma_attrs *attrs)
74 void *va = phys_to_virt(page_to_phys(page)) + offset;
75 struct scif_hw_dev *scdev = dev_get_drvdata(dev);
76 struct mic_device *mdev = scdev_to_mdev(scdev);
78 return mic_map_single(mdev, va, size);
82 __mic_dma_unmap_page(struct device *dev, dma_addr_t dma_addr,
83 size_t size, enum dma_data_direction dir,
84 struct dma_attrs *attrs)
86 struct scif_hw_dev *scdev = dev_get_drvdata(dev);
87 struct mic_device *mdev = scdev_to_mdev(scdev);
89 mic_unmap_single(mdev, dma_addr, size);
92 static int __mic_dma_map_sg(struct device *dev, struct scatterlist *sg,
93 int nents, enum dma_data_direction dir,
94 struct dma_attrs *attrs)
96 struct scif_hw_dev *scdev = dev_get_drvdata(dev);
97 struct mic_device *mdev = scdev_to_mdev(scdev);
98 struct scatterlist *s;
102 ret = dma_map_sg(&mdev->pdev->dev, sg, nents, dir);
106 for_each_sg(sg, s, nents, i) {
107 da = mic_map(mdev, sg_dma_address(s) + s->offset, s->length);
110 sg_dma_address(s) = da;
114 for_each_sg(sg, s, i, j) {
115 mic_unmap(mdev, sg_dma_address(s), s->length);
116 sg_dma_address(s) = mic_to_dma_addr(mdev, sg_dma_address(s));
118 dma_unmap_sg(&mdev->pdev->dev, sg, nents, dir);
122 static void __mic_dma_unmap_sg(struct device *dev,
123 struct scatterlist *sg, int nents,
124 enum dma_data_direction dir,
125 struct dma_attrs *attrs)
127 struct scif_hw_dev *scdev = dev_get_drvdata(dev);
128 struct mic_device *mdev = scdev_to_mdev(scdev);
129 struct scatterlist *s;
133 for_each_sg(sg, s, nents, i) {
134 da = mic_to_dma_addr(mdev, sg_dma_address(s));
135 mic_unmap(mdev, sg_dma_address(s), s->length);
136 sg_dma_address(s) = da;
138 dma_unmap_sg(&mdev->pdev->dev, sg, nents, dir);
141 static struct dma_map_ops __mic_dma_ops = {
142 .alloc = __mic_dma_alloc,
143 .free = __mic_dma_free,
144 .map_page = __mic_dma_map_page,
145 .unmap_page = __mic_dma_unmap_page,
146 .map_sg = __mic_dma_map_sg,
147 .unmap_sg = __mic_dma_unmap_sg,
150 static struct mic_irq *
151 ___mic_request_irq(struct scif_hw_dev *scdev,
152 irqreturn_t (*func)(int irq, void *data),
156 struct mic_device *mdev = scdev_to_mdev(scdev);
158 return mic_request_threaded_irq(mdev, func, NULL, name, data,
163 ___mic_free_irq(struct scif_hw_dev *scdev,
164 struct mic_irq *cookie, void *data)
166 struct mic_device *mdev = scdev_to_mdev(scdev);
168 return mic_free_irq(mdev, cookie, data);
171 static void ___mic_ack_interrupt(struct scif_hw_dev *scdev, int num)
173 struct mic_device *mdev = scdev_to_mdev(scdev);
175 mdev->ops->intr_workarounds(mdev);
178 static int ___mic_next_db(struct scif_hw_dev *scdev)
180 struct mic_device *mdev = scdev_to_mdev(scdev);
182 return mic_next_db(mdev);
185 static void ___mic_send_intr(struct scif_hw_dev *scdev, int db)
187 struct mic_device *mdev = scdev_to_mdev(scdev);
189 mdev->ops->send_intr(mdev, db);
192 static void __iomem *___mic_ioremap(struct scif_hw_dev *scdev,
193 phys_addr_t pa, size_t len)
195 struct mic_device *mdev = scdev_to_mdev(scdev);
197 return mdev->aper.va + pa;
200 static void ___mic_iounmap(struct scif_hw_dev *scdev, void __iomem *va)
205 static struct scif_hw_ops scif_hw_ops = {
206 .request_irq = ___mic_request_irq,
207 .free_irq = ___mic_free_irq,
208 .ack_interrupt = ___mic_ack_interrupt,
209 .next_db = ___mic_next_db,
210 .send_intr = ___mic_send_intr,
211 .ioremap = ___mic_ioremap,
212 .iounmap = ___mic_iounmap,
215 static inline struct mic_device *mbdev_to_mdev(struct mbus_device *mbdev)
217 return dev_get_drvdata(mbdev->dev.parent);
221 mic_dma_map_page(struct device *dev, struct page *page,
222 unsigned long offset, size_t size, enum dma_data_direction dir,
223 struct dma_attrs *attrs)
225 void *va = phys_to_virt(page_to_phys(page)) + offset;
226 struct mic_device *mdev = dev_get_drvdata(dev->parent);
228 return mic_map_single(mdev, va, size);
232 mic_dma_unmap_page(struct device *dev, dma_addr_t dma_addr,
233 size_t size, enum dma_data_direction dir,
234 struct dma_attrs *attrs)
236 struct mic_device *mdev = dev_get_drvdata(dev->parent);
237 mic_unmap_single(mdev, dma_addr, size);
240 static struct dma_map_ops mic_dma_ops = {
241 .map_page = mic_dma_map_page,
242 .unmap_page = mic_dma_unmap_page,
245 static struct mic_irq *
246 _mic_request_threaded_irq(struct mbus_device *mbdev,
247 irq_handler_t handler, irq_handler_t thread_fn,
248 const char *name, void *data, int intr_src)
250 return mic_request_threaded_irq(mbdev_to_mdev(mbdev), handler,
251 thread_fn, name, data,
252 intr_src, MIC_INTR_DMA);
255 static void _mic_free_irq(struct mbus_device *mbdev,
256 struct mic_irq *cookie, void *data)
258 return mic_free_irq(mbdev_to_mdev(mbdev), cookie, data);
261 static void _mic_ack_interrupt(struct mbus_device *mbdev, int num)
263 struct mic_device *mdev = mbdev_to_mdev(mbdev);
264 mdev->ops->intr_workarounds(mdev);
267 static struct mbus_hw_ops mbus_hw_ops = {
268 .request_threaded_irq = _mic_request_threaded_irq,
269 .free_irq = _mic_free_irq,
270 .ack_interrupt = _mic_ack_interrupt,
273 /* Initialize the MIC bootparams */
274 void mic_bootparam_init(struct mic_device *mdev)
276 struct mic_bootparam *bootparam = mdev->dp;
278 bootparam->magic = cpu_to_le32(MIC_MAGIC);
279 bootparam->h2c_config_db = -1;
280 bootparam->node_id = mdev->id + 1;
281 bootparam->scif_host_dma_addr = 0x0;
282 bootparam->scif_card_dma_addr = 0x0;
283 bootparam->c2h_scif_db = -1;
284 bootparam->h2c_scif_db = -1;
287 static inline struct mic_device *cosmdev_to_mdev(struct cosm_device *cdev)
289 return dev_get_drvdata(cdev->dev.parent);
292 static void _mic_reset(struct cosm_device *cdev)
294 struct mic_device *mdev = cosmdev_to_mdev(cdev);
296 mdev->ops->reset_fw_ready(mdev);
297 mdev->ops->reset(mdev);
300 static bool _mic_ready(struct cosm_device *cdev)
302 struct mic_device *mdev = cosmdev_to_mdev(cdev);
304 return mdev->ops->is_fw_ready(mdev);
308 * mic_request_dma_chans - Request DMA channels
309 * @mdev: pointer to mic_device instance
311 * returns number of DMA channels acquired
313 static int mic_request_dma_chans(struct mic_device *mdev)
316 struct dma_chan *chan;
318 request_module("mic_x100_dma");
320 dma_cap_set(DMA_MEMCPY, mask);
323 chan = dma_request_channel(mask, mdev->ops->dma_filter,
326 mdev->dma_ch[mdev->num_dma_ch++] = chan;
327 if (mdev->num_dma_ch >= MIC_MAX_DMA_CHAN)
331 dev_info(&mdev->pdev->dev, "DMA channels # %d\n", mdev->num_dma_ch);
332 return mdev->num_dma_ch;
336 * mic_free_dma_chans - release DMA channels
337 * @mdev: pointer to mic_device instance
341 static void mic_free_dma_chans(struct mic_device *mdev)
345 for (i = 0; i < mdev->num_dma_ch; i++) {
346 dma_release_channel(mdev->dma_ch[i]);
347 mdev->dma_ch[i] = NULL;
349 mdev->num_dma_ch = 0;
353 * _mic_start - Start the MIC.
354 * @cdev: pointer to cosm_device instance
355 * @id: MIC device id/index provided by COSM used in other drivers like SCIF
357 * This function prepares an MIC for boot and initiates boot.
358 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
360 * For all cosm_hw_ops the caller holds a mutex to ensure serialization.
362 static int _mic_start(struct cosm_device *cdev, int id)
364 struct mic_device *mdev = cosmdev_to_mdev(cdev);
367 mic_bootparam_init(mdev);
368 mdev->dma_mbdev = mbus_register_device(&mdev->pdev->dev,
369 MBUS_DEV_DMA_HOST, &mic_dma_ops,
370 &mbus_hw_ops, id, mdev->mmio.va);
371 if (IS_ERR(mdev->dma_mbdev)) {
372 rc = PTR_ERR(mdev->dma_mbdev);
375 if (!mic_request_dma_chans(mdev)) {
379 mdev->scdev = scif_register_device(&mdev->pdev->dev, MIC_SCIF_DEV,
380 &__mic_dma_ops, &scif_hw_ops,
381 id + 1, 0, &mdev->mmio,
382 &mdev->aper, mdev->dp, NULL,
383 mdev->dma_ch, mdev->num_dma_ch,
385 if (IS_ERR(mdev->scdev)) {
386 rc = PTR_ERR(mdev->scdev);
390 rc = mdev->ops->load_mic_fw(mdev, NULL);
393 mic_smpt_restore(mdev);
394 mic_intr_restore(mdev);
395 mdev->intr_ops->enable_interrupts(mdev);
396 mdev->ops->write_spad(mdev, MIC_DPLO_SPAD, mdev->dp_dma_addr);
397 mdev->ops->write_spad(mdev, MIC_DPHI_SPAD, mdev->dp_dma_addr >> 32);
398 mdev->ops->send_firmware_intr(mdev);
401 scif_unregister_device(mdev->scdev);
403 mic_free_dma_chans(mdev);
405 mbus_unregister_device(mdev->dma_mbdev);
411 * _mic_stop - Prepare the MIC for reset and trigger reset.
412 * @cdev: pointer to cosm_device instance
413 * @force: force a MIC to reset even if it is already offline.
417 static void _mic_stop(struct cosm_device *cdev, bool force)
419 struct mic_device *mdev = cosmdev_to_mdev(cdev);
422 * Since SCIF handles card shutdown and reset (using COSM), it will
423 * will be the first to be registered and the last to be
426 mic_virtio_reset_devices(mdev);
427 scif_unregister_device(mdev->scdev);
428 mic_free_dma_chans(mdev);
429 mbus_unregister_device(mdev->dma_mbdev);
430 mic_bootparam_init(mdev);
433 static ssize_t _mic_family(struct cosm_device *cdev, char *buf)
435 struct mic_device *mdev = cosmdev_to_mdev(cdev);
436 static const char *family[MIC_FAMILY_LAST] = { "x100", "Unknown" };
438 return scnprintf(buf, PAGE_SIZE, "%s\n", family[mdev->family]);
441 static ssize_t _mic_stepping(struct cosm_device *cdev, char *buf)
443 struct mic_device *mdev = cosmdev_to_mdev(cdev);
444 const char *string = "??";
446 switch (mdev->stepping) {
462 return scnprintf(buf, PAGE_SIZE, "%s\n", string);
465 static struct mic_mw *_mic_aper(struct cosm_device *cdev)
467 struct mic_device *mdev = cosmdev_to_mdev(cdev);
472 struct cosm_hw_ops cosm_hw_ops = {
474 .force_reset = _mic_reset,
479 .family = _mic_family,
480 .stepping = _mic_stepping,