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 /* Unique numbering for mbus devices. */
29 static DEFINE_IDA(mbus_index_ida
);
31 static ssize_t
device_show(struct device
*d
,
32 struct device_attribute
*attr
, char *buf
)
34 struct mbus_device
*dev
= dev_to_mbus(d
);
35 return sprintf(buf
, "0x%04x\n", dev
->id
.device
);
37 static DEVICE_ATTR_RO(device
);
39 static ssize_t
vendor_show(struct device
*d
,
40 struct device_attribute
*attr
, char *buf
)
42 struct mbus_device
*dev
= dev_to_mbus(d
);
43 return sprintf(buf
, "0x%04x\n", dev
->id
.vendor
);
45 static DEVICE_ATTR_RO(vendor
);
47 static ssize_t
modalias_show(struct device
*d
,
48 struct device_attribute
*attr
, char *buf
)
50 struct mbus_device
*dev
= dev_to_mbus(d
);
51 return sprintf(buf
, "mbus:d%08Xv%08X\n",
52 dev
->id
.device
, dev
->id
.vendor
);
54 static DEVICE_ATTR_RO(modalias
);
56 static struct attribute
*mbus_dev_attrs
[] = {
57 &dev_attr_device
.attr
,
58 &dev_attr_vendor
.attr
,
59 &dev_attr_modalias
.attr
,
62 ATTRIBUTE_GROUPS(mbus_dev
);
64 static inline int mbus_id_match(const struct mbus_device
*dev
,
65 const struct mbus_device_id
*id
)
67 if (id
->device
!= dev
->id
.device
&& id
->device
!= MBUS_DEV_ANY_ID
)
70 return id
->vendor
== MBUS_DEV_ANY_ID
|| id
->vendor
== dev
->id
.vendor
;
74 * This looks through all the IDs a driver claims to support. If any of them
75 * match, we return 1 and the kernel will call mbus_dev_probe().
77 static int mbus_dev_match(struct device
*dv
, struct device_driver
*dr
)
80 struct mbus_device
*dev
= dev_to_mbus(dv
);
81 const struct mbus_device_id
*ids
;
83 ids
= drv_to_mbus(dr
)->id_table
;
84 for (i
= 0; ids
[i
].device
; i
++)
85 if (mbus_id_match(dev
, &ids
[i
]))
90 static int mbus_uevent(struct device
*dv
, struct kobj_uevent_env
*env
)
92 struct mbus_device
*dev
= dev_to_mbus(dv
);
94 return add_uevent_var(env
, "MODALIAS=mbus:d%08Xv%08X",
95 dev
->id
.device
, dev
->id
.vendor
);
98 static int mbus_dev_probe(struct device
*d
)
101 struct mbus_device
*dev
= dev_to_mbus(d
);
102 struct mbus_driver
*drv
= drv_to_mbus(dev
->dev
.driver
);
104 err
= drv
->probe(dev
);
111 static int mbus_dev_remove(struct device
*d
)
113 struct mbus_device
*dev
= dev_to_mbus(d
);
114 struct mbus_driver
*drv
= drv_to_mbus(dev
->dev
.driver
);
120 static struct bus_type mic_bus
= {
122 .match
= mbus_dev_match
,
123 .dev_groups
= mbus_dev_groups
,
124 .uevent
= mbus_uevent
,
125 .probe
= mbus_dev_probe
,
126 .remove
= mbus_dev_remove
,
129 int mbus_register_driver(struct mbus_driver
*driver
)
131 driver
->driver
.bus
= &mic_bus
;
132 return driver_register(&driver
->driver
);
134 EXPORT_SYMBOL_GPL(mbus_register_driver
);
136 void mbus_unregister_driver(struct mbus_driver
*driver
)
138 driver_unregister(&driver
->driver
);
140 EXPORT_SYMBOL_GPL(mbus_unregister_driver
);
142 static void mbus_release_dev(struct device
*d
)
144 struct mbus_device
*mbdev
= dev_to_mbus(d
);
149 mbus_register_device(struct device
*pdev
, int id
, struct dma_map_ops
*dma_ops
,
150 struct mbus_hw_ops
*hw_ops
, void __iomem
*mmio_va
)
153 struct mbus_device
*mbdev
;
155 mbdev
= kzalloc(sizeof(*mbdev
), GFP_KERNEL
);
157 return ERR_PTR(-ENOMEM
);
159 mbdev
->mmio_va
= mmio_va
;
160 mbdev
->dev
.parent
= pdev
;
161 mbdev
->id
.device
= id
;
162 mbdev
->id
.vendor
= MBUS_DEV_ANY_ID
;
163 mbdev
->dev
.archdata
.dma_ops
= dma_ops
;
164 mbdev
->dev
.dma_mask
= &mbdev
->dev
.coherent_dma_mask
;
165 dma_set_mask(&mbdev
->dev
, DMA_BIT_MASK(64));
166 mbdev
->dev
.release
= mbus_release_dev
;
167 mbdev
->hw_ops
= hw_ops
;
168 mbdev
->dev
.bus
= &mic_bus
;
170 /* Assign a unique device index and hence name. */
171 ret
= ida_simple_get(&mbus_index_ida
, 0, 0, GFP_KERNEL
);
176 dev_set_name(&mbdev
->dev
, "mbus-dev%u", mbdev
->index
);
178 * device_register() causes the bus infrastructure to look for a
181 ret
= device_register(&mbdev
->dev
);
186 ida_simple_remove(&mbus_index_ida
, mbdev
->index
);
191 EXPORT_SYMBOL_GPL(mbus_register_device
);
193 void mbus_unregister_device(struct mbus_device
*mbdev
)
195 int index
= mbdev
->index
; /* save for after device release */
197 device_unregister(&mbdev
->dev
);
198 ida_simple_remove(&mbus_index_ida
, index
);
200 EXPORT_SYMBOL_GPL(mbus_unregister_device
);
202 static int __init
mbus_init(void)
204 return bus_register(&mic_bus
);
207 static void __exit
mbus_exit(void)
209 bus_unregister(&mic_bus
);
210 ida_destroy(&mbus_index_ida
);
213 core_initcall(mbus_init
);
214 module_exit(mbus_exit
);
216 MODULE_AUTHOR("Intel Corporation");
217 MODULE_DESCRIPTION("Intel(R) MIC Bus driver");
218 MODULE_LICENSE("GPL v2");