2 * Intel MIC Platform Software Stack (MPSS)
4 * Copyright(c) 2014 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 Bus driver.
20 * This implementation is very similar to the the virtio bus driver
21 * implementation @ drivers/virtio/virtio.c
23 #include <linux/slab.h>
24 #include <linux/module.h>
25 #include <linux/idr.h>
26 #include <linux/mic_bus.h>
28 static ssize_t
device_show(struct device
*d
,
29 struct device_attribute
*attr
, char *buf
)
31 struct mbus_device
*dev
= dev_to_mbus(d
);
32 return sprintf(buf
, "0x%04x\n", dev
->id
.device
);
34 static DEVICE_ATTR_RO(device
);
36 static ssize_t
vendor_show(struct device
*d
,
37 struct device_attribute
*attr
, char *buf
)
39 struct mbus_device
*dev
= dev_to_mbus(d
);
40 return sprintf(buf
, "0x%04x\n", dev
->id
.vendor
);
42 static DEVICE_ATTR_RO(vendor
);
44 static ssize_t
modalias_show(struct device
*d
,
45 struct device_attribute
*attr
, char *buf
)
47 struct mbus_device
*dev
= dev_to_mbus(d
);
48 return sprintf(buf
, "mbus:d%08Xv%08X\n",
49 dev
->id
.device
, dev
->id
.vendor
);
51 static DEVICE_ATTR_RO(modalias
);
53 static struct attribute
*mbus_dev_attrs
[] = {
54 &dev_attr_device
.attr
,
55 &dev_attr_vendor
.attr
,
56 &dev_attr_modalias
.attr
,
59 ATTRIBUTE_GROUPS(mbus_dev
);
61 static inline int mbus_id_match(const struct mbus_device
*dev
,
62 const struct mbus_device_id
*id
)
64 if (id
->device
!= dev
->id
.device
&& id
->device
!= MBUS_DEV_ANY_ID
)
67 return id
->vendor
== MBUS_DEV_ANY_ID
|| id
->vendor
== dev
->id
.vendor
;
71 * This looks through all the IDs a driver claims to support. If any of them
72 * match, we return 1 and the kernel will call mbus_dev_probe().
74 static int mbus_dev_match(struct device
*dv
, struct device_driver
*dr
)
77 struct mbus_device
*dev
= dev_to_mbus(dv
);
78 const struct mbus_device_id
*ids
;
80 ids
= drv_to_mbus(dr
)->id_table
;
81 for (i
= 0; ids
[i
].device
; i
++)
82 if (mbus_id_match(dev
, &ids
[i
]))
87 static int mbus_uevent(struct device
*dv
, struct kobj_uevent_env
*env
)
89 struct mbus_device
*dev
= dev_to_mbus(dv
);
91 return add_uevent_var(env
, "MODALIAS=mbus:d%08Xv%08X",
92 dev
->id
.device
, dev
->id
.vendor
);
95 static int mbus_dev_probe(struct device
*d
)
98 struct mbus_device
*dev
= dev_to_mbus(d
);
99 struct mbus_driver
*drv
= drv_to_mbus(dev
->dev
.driver
);
101 err
= drv
->probe(dev
);
108 static int mbus_dev_remove(struct device
*d
)
110 struct mbus_device
*dev
= dev_to_mbus(d
);
111 struct mbus_driver
*drv
= drv_to_mbus(dev
->dev
.driver
);
117 static struct bus_type mic_bus
= {
119 .match
= mbus_dev_match
,
120 .dev_groups
= mbus_dev_groups
,
121 .uevent
= mbus_uevent
,
122 .probe
= mbus_dev_probe
,
123 .remove
= mbus_dev_remove
,
126 int mbus_register_driver(struct mbus_driver
*driver
)
128 driver
->driver
.bus
= &mic_bus
;
129 return driver_register(&driver
->driver
);
131 EXPORT_SYMBOL_GPL(mbus_register_driver
);
133 void mbus_unregister_driver(struct mbus_driver
*driver
)
135 driver_unregister(&driver
->driver
);
137 EXPORT_SYMBOL_GPL(mbus_unregister_driver
);
139 static void mbus_release_dev(struct device
*d
)
141 struct mbus_device
*mbdev
= dev_to_mbus(d
);
146 mbus_register_device(struct device
*pdev
, int id
, const struct dma_map_ops
*dma_ops
,
147 struct mbus_hw_ops
*hw_ops
, int index
,
148 void __iomem
*mmio_va
)
151 struct mbus_device
*mbdev
;
153 mbdev
= kzalloc(sizeof(*mbdev
), GFP_KERNEL
);
155 return ERR_PTR(-ENOMEM
);
157 mbdev
->mmio_va
= mmio_va
;
158 mbdev
->dev
.parent
= pdev
;
159 mbdev
->id
.device
= id
;
160 mbdev
->id
.vendor
= MBUS_DEV_ANY_ID
;
161 mbdev
->dev
.dma_ops
= dma_ops
;
162 mbdev
->dev
.dma_mask
= &mbdev
->dev
.coherent_dma_mask
;
163 dma_set_mask(&mbdev
->dev
, DMA_BIT_MASK(64));
164 mbdev
->dev
.release
= mbus_release_dev
;
165 mbdev
->hw_ops
= hw_ops
;
166 mbdev
->dev
.bus
= &mic_bus
;
167 mbdev
->index
= index
;
168 dev_set_name(&mbdev
->dev
, "mbus-dev%u", mbdev
->index
);
170 * device_register() causes the bus infrastructure to look for a
173 ret
= device_register(&mbdev
->dev
);
178 put_device(&mbdev
->dev
);
181 EXPORT_SYMBOL_GPL(mbus_register_device
);
183 void mbus_unregister_device(struct mbus_device
*mbdev
)
185 device_unregister(&mbdev
->dev
);
187 EXPORT_SYMBOL_GPL(mbus_unregister_device
);
189 static int __init
mbus_init(void)
191 return bus_register(&mic_bus
);
194 static void __exit
mbus_exit(void)
196 bus_unregister(&mic_bus
);
199 core_initcall(mbus_init
);
200 module_exit(mbus_exit
);
202 MODULE_AUTHOR("Intel Corporation");
203 MODULE_DESCRIPTION("Intel(R) MIC Bus driver");
204 MODULE_LICENSE("GPL v2");