]> git.proxmox.com Git - mirror_ubuntu-focal-kernel.git/blob - arch/sparc/kernel/vio.c
Input: wm97xx: add new AC97 bus support
[mirror_ubuntu-focal-kernel.git] / arch / sparc / kernel / vio.c
1 /* vio.c: Virtual I/O channel devices probing infrastructure.
2 *
3 * Copyright (c) 2003-2005 IBM Corp.
4 * Dave Engebretsen engebret@us.ibm.com
5 * Santiago Leon santil@us.ibm.com
6 * Hollis Blanchard <hollisb@us.ibm.com>
7 * Stephen Rothwell
8 *
9 * Adapted to sparc64 by David S. Miller davem@davemloft.net
10 */
11
12 #include <linux/kernel.h>
13 #include <linux/slab.h>
14 #include <linux/irq.h>
15 #include <linux/export.h>
16 #include <linux/init.h>
17
18 #include <asm/mdesc.h>
19 #include <asm/vio.h>
20
21 static const struct vio_device_id *vio_match_device(
22 const struct vio_device_id *matches,
23 const struct vio_dev *dev)
24 {
25 const char *type, *compat;
26 int len;
27
28 type = dev->type;
29 compat = dev->compat;
30 len = dev->compat_len;
31
32 while (matches->type[0] || matches->compat[0]) {
33 int match = 1;
34 if (matches->type[0])
35 match &= !strcmp(matches->type, type);
36
37 if (matches->compat[0]) {
38 match &= len &&
39 of_find_in_proplist(compat, matches->compat, len);
40 }
41 if (match)
42 return matches;
43 matches++;
44 }
45 return NULL;
46 }
47
48 static int vio_hotplug(struct device *dev, struct kobj_uevent_env *env)
49 {
50 const struct vio_dev *vio_dev = to_vio_dev(dev);
51
52 add_uevent_var(env, "MODALIAS=vio:T%sS%s", vio_dev->type, vio_dev->compat);
53 return 0;
54 }
55
56 static int vio_bus_match(struct device *dev, struct device_driver *drv)
57 {
58 struct vio_dev *vio_dev = to_vio_dev(dev);
59 struct vio_driver *vio_drv = to_vio_driver(drv);
60 const struct vio_device_id *matches = vio_drv->id_table;
61
62 if (!matches)
63 return 0;
64
65 return vio_match_device(matches, vio_dev) != NULL;
66 }
67
68 static int vio_device_probe(struct device *dev)
69 {
70 struct vio_dev *vdev = to_vio_dev(dev);
71 struct vio_driver *drv = to_vio_driver(dev->driver);
72 const struct vio_device_id *id;
73
74 if (!drv->probe)
75 return -ENODEV;
76
77 id = vio_match_device(drv->id_table, vdev);
78 if (!id)
79 return -ENODEV;
80
81 /* alloc irqs (unless the driver specified not to) */
82 if (!drv->no_irq) {
83 if (vdev->tx_irq == 0 && vdev->tx_ino != ~0UL)
84 vdev->tx_irq = sun4v_build_virq(vdev->cdev_handle,
85 vdev->tx_ino);
86
87 if (vdev->rx_irq == 0 && vdev->rx_ino != ~0UL)
88 vdev->rx_irq = sun4v_build_virq(vdev->cdev_handle,
89 vdev->rx_ino);
90 }
91
92 return drv->probe(vdev, id);
93 }
94
95 static int vio_device_remove(struct device *dev)
96 {
97 struct vio_dev *vdev = to_vio_dev(dev);
98 struct vio_driver *drv = to_vio_driver(dev->driver);
99
100 if (drv->remove) {
101 /*
102 * Ideally, we would remove/deallocate tx/rx virqs
103 * here - however, there are currently no support
104 * routines to do so at the moment. TBD
105 */
106
107 return drv->remove(vdev);
108 }
109
110 return 1;
111 }
112
113 static ssize_t devspec_show(struct device *dev,
114 struct device_attribute *attr, char *buf)
115 {
116 struct vio_dev *vdev = to_vio_dev(dev);
117 const char *str = "none";
118
119 if (!strcmp(vdev->type, "vnet-port"))
120 str = "vnet";
121 else if (!strcmp(vdev->type, "vdc-port"))
122 str = "vdisk";
123
124 return sprintf(buf, "%s\n", str);
125 }
126 static DEVICE_ATTR_RO(devspec);
127
128 static ssize_t type_show(struct device *dev,
129 struct device_attribute *attr, char *buf)
130 {
131 struct vio_dev *vdev = to_vio_dev(dev);
132 return sprintf(buf, "%s\n", vdev->type);
133 }
134 static DEVICE_ATTR_RO(type);
135
136 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
137 char *buf)
138 {
139 const struct vio_dev *vdev = to_vio_dev(dev);
140
141 return sprintf(buf, "vio:T%sS%s\n", vdev->type, vdev->compat);
142 }
143 static DEVICE_ATTR_RO(modalias);
144
145 static struct attribute *vio_dev_attrs[] = {
146 &dev_attr_devspec.attr,
147 &dev_attr_type.attr,
148 &dev_attr_modalias.attr,
149 NULL,
150 };
151 ATTRIBUTE_GROUPS(vio_dev);
152
153 static struct bus_type vio_bus_type = {
154 .name = "vio",
155 .dev_groups = vio_dev_groups,
156 .uevent = vio_hotplug,
157 .match = vio_bus_match,
158 .probe = vio_device_probe,
159 .remove = vio_device_remove,
160 };
161
162 int __vio_register_driver(struct vio_driver *viodrv, struct module *owner,
163 const char *mod_name)
164 {
165 viodrv->driver.bus = &vio_bus_type;
166 viodrv->driver.name = viodrv->name;
167 viodrv->driver.owner = owner;
168 viodrv->driver.mod_name = mod_name;
169
170 return driver_register(&viodrv->driver);
171 }
172 EXPORT_SYMBOL(__vio_register_driver);
173
174 void vio_unregister_driver(struct vio_driver *viodrv)
175 {
176 driver_unregister(&viodrv->driver);
177 }
178 EXPORT_SYMBOL(vio_unregister_driver);
179
180 static void vio_dev_release(struct device *dev)
181 {
182 kfree(to_vio_dev(dev));
183 }
184
185 static ssize_t
186 show_pciobppath_attr(struct device *dev, struct device_attribute *attr,
187 char *buf)
188 {
189 struct vio_dev *vdev;
190 struct device_node *dp;
191
192 vdev = to_vio_dev(dev);
193 dp = vdev->dp;
194
195 return snprintf (buf, PAGE_SIZE, "%s\n", dp->full_name);
196 }
197
198 static DEVICE_ATTR(obppath, S_IRUSR | S_IRGRP | S_IROTH,
199 show_pciobppath_attr, NULL);
200
201 static struct device_node *cdev_node;
202
203 static struct vio_dev *root_vdev;
204 static u64 cdev_cfg_handle;
205
206 static const u64 *vio_cfg_handle(struct mdesc_handle *hp, u64 node)
207 {
208 const u64 *cfg_handle = NULL;
209 u64 a;
210
211 mdesc_for_each_arc(a, hp, node, MDESC_ARC_TYPE_BACK) {
212 u64 target;
213
214 target = mdesc_arc_target(hp, a);
215 cfg_handle = mdesc_get_property(hp, target,
216 "cfg-handle", NULL);
217 if (cfg_handle)
218 break;
219 }
220
221 return cfg_handle;
222 }
223
224 /**
225 * vio_vdev_node() - Find VDEV node in MD
226 * @hp: Handle to the MD
227 * @vdev: Pointer to VDEV
228 *
229 * Find the node in the current MD which matches the given vio_dev. This
230 * must be done dynamically since the node value can change if the MD
231 * is updated.
232 *
233 * NOTE: the MD must be locked, using mdesc_grab(), when calling this routine
234 *
235 * Return: The VDEV node in MDESC
236 */
237 u64 vio_vdev_node(struct mdesc_handle *hp, struct vio_dev *vdev)
238 {
239 u64 node;
240
241 if (vdev == NULL)
242 return MDESC_NODE_NULL;
243
244 node = mdesc_get_node(hp, (const char *)vdev->node_name,
245 &vdev->md_node_info);
246
247 return node;
248 }
249 EXPORT_SYMBOL(vio_vdev_node);
250
251 static void vio_fill_channel_info(struct mdesc_handle *hp, u64 mp,
252 struct vio_dev *vdev)
253 {
254 u64 a;
255
256 vdev->tx_ino = ~0UL;
257 vdev->rx_ino = ~0UL;
258 vdev->channel_id = ~0UL;
259 mdesc_for_each_arc(a, hp, mp, MDESC_ARC_TYPE_FWD) {
260 const u64 *chan_id;
261 const u64 *irq;
262 u64 target;
263
264 target = mdesc_arc_target(hp, a);
265
266 irq = mdesc_get_property(hp, target, "tx-ino", NULL);
267 if (irq)
268 vdev->tx_ino = *irq;
269
270 irq = mdesc_get_property(hp, target, "rx-ino", NULL);
271 if (irq)
272 vdev->rx_ino = *irq;
273
274 chan_id = mdesc_get_property(hp, target, "id", NULL);
275 if (chan_id)
276 vdev->channel_id = *chan_id;
277 }
278
279 vdev->cdev_handle = cdev_cfg_handle;
280 }
281
282 int vio_set_intr(unsigned long dev_ino, int state)
283 {
284 int err;
285
286 err = sun4v_vintr_set_valid(cdev_cfg_handle, dev_ino, state);
287 return err;
288 }
289 EXPORT_SYMBOL(vio_set_intr);
290
291 static struct vio_dev *vio_create_one(struct mdesc_handle *hp, u64 mp,
292 const char *node_name,
293 struct device *parent)
294 {
295 const char *type, *compat;
296 struct device_node *dp;
297 struct vio_dev *vdev;
298 int err, tlen, clen;
299 const u64 *id, *cfg_handle;
300
301 type = mdesc_get_property(hp, mp, "device-type", &tlen);
302 if (!type) {
303 type = mdesc_get_property(hp, mp, "name", &tlen);
304 if (!type) {
305 type = mdesc_node_name(hp, mp);
306 tlen = strlen(type) + 1;
307 }
308 }
309 if (tlen > VIO_MAX_TYPE_LEN || strlen(type) >= VIO_MAX_TYPE_LEN) {
310 printk(KERN_ERR "VIO: Type string [%s] is too long.\n",
311 type);
312 return NULL;
313 }
314
315 id = mdesc_get_property(hp, mp, "id", NULL);
316
317 cfg_handle = vio_cfg_handle(hp, mp);
318
319 compat = mdesc_get_property(hp, mp, "device-type", &clen);
320 if (!compat) {
321 clen = 0;
322 } else if (clen > VIO_MAX_COMPAT_LEN) {
323 printk(KERN_ERR "VIO: Compat len %d for [%s] is too long.\n",
324 clen, type);
325 return NULL;
326 }
327
328 vdev = kzalloc(sizeof(*vdev), GFP_KERNEL);
329 if (!vdev) {
330 printk(KERN_ERR "VIO: Could not allocate vio_dev\n");
331 return NULL;
332 }
333
334 vdev->mp = mp;
335 memcpy(vdev->type, type, tlen);
336 if (compat)
337 memcpy(vdev->compat, compat, clen);
338 else
339 memset(vdev->compat, 0, sizeof(vdev->compat));
340 vdev->compat_len = clen;
341
342 vdev->port_id = ~0UL;
343 vdev->tx_irq = 0;
344 vdev->rx_irq = 0;
345
346 vio_fill_channel_info(hp, mp, vdev);
347
348 if (!id) {
349 dev_set_name(&vdev->dev, "%s", type);
350 vdev->dev_no = ~(u64)0;
351 } else if (!cfg_handle) {
352 dev_set_name(&vdev->dev, "%s-%llu", type, *id);
353 vdev->dev_no = *id;
354 } else {
355 dev_set_name(&vdev->dev, "%s-%llu-%llu", type,
356 *cfg_handle, *id);
357 vdev->dev_no = *cfg_handle;
358 vdev->port_id = *id;
359 }
360
361 vdev->dev.parent = parent;
362 vdev->dev.bus = &vio_bus_type;
363 vdev->dev.release = vio_dev_release;
364
365 if (parent == NULL) {
366 dp = cdev_node;
367 } else if (to_vio_dev(parent) == root_vdev) {
368 dp = of_get_next_child(cdev_node, NULL);
369 while (dp) {
370 if (!strcmp(dp->type, type))
371 break;
372
373 dp = of_get_next_child(cdev_node, dp);
374 }
375 } else {
376 dp = to_vio_dev(parent)->dp;
377 }
378 vdev->dp = dp;
379
380 /*
381 * node_name is NULL for the parent/channel-devices node and
382 * the parent doesn't require the MD node info.
383 */
384 if (node_name != NULL) {
385 (void) snprintf(vdev->node_name, VIO_MAX_NAME_LEN, "%s",
386 node_name);
387
388 err = mdesc_get_node_info(hp, mp, node_name,
389 &vdev->md_node_info);
390 if (err) {
391 pr_err("VIO: Could not get MD node info %s, err=%d\n",
392 dev_name(&vdev->dev), err);
393 kfree(vdev);
394 return NULL;
395 }
396 }
397
398 pr_info("VIO: Adding device %s (tx_ino = %llx, rx_ino = %llx)\n",
399 dev_name(&vdev->dev), vdev->tx_ino, vdev->rx_ino);
400
401 err = device_register(&vdev->dev);
402 if (err) {
403 printk(KERN_ERR "VIO: Could not register device %s, err=%d\n",
404 dev_name(&vdev->dev), err);
405 kfree(vdev);
406 return NULL;
407 }
408 if (vdev->dp)
409 err = sysfs_create_file(&vdev->dev.kobj,
410 &dev_attr_obppath.attr);
411
412 return vdev;
413 }
414
415 static void vio_add(struct mdesc_handle *hp, u64 node,
416 const char *node_name)
417 {
418 (void) vio_create_one(hp, node, node_name, &root_vdev->dev);
419 }
420
421 struct vio_remove_node_data {
422 struct mdesc_handle *hp;
423 u64 node;
424 };
425
426 static int vio_md_node_match(struct device *dev, void *arg)
427 {
428 struct vio_dev *vdev = to_vio_dev(dev);
429 struct vio_remove_node_data *node_data;
430 u64 node;
431
432 node_data = (struct vio_remove_node_data *)arg;
433
434 node = vio_vdev_node(node_data->hp, vdev);
435
436 if (node == node_data->node)
437 return 1;
438 else
439 return 0;
440 }
441
442 static void vio_remove(struct mdesc_handle *hp, u64 node, const char *node_name)
443 {
444 struct vio_remove_node_data node_data;
445 struct device *dev;
446
447 node_data.hp = hp;
448 node_data.node = node;
449
450 dev = device_find_child(&root_vdev->dev, (void *)&node_data,
451 vio_md_node_match);
452 if (dev) {
453 printk(KERN_INFO "VIO: Removing device %s\n", dev_name(dev));
454
455 device_unregister(dev);
456 put_device(dev);
457 } else {
458 pr_err("VIO: %s node not found in MDESC\n", node_name);
459 }
460 }
461
462 static struct mdesc_notifier_client vio_device_notifier = {
463 .add = vio_add,
464 .remove = vio_remove,
465 .node_name = "virtual-device-port",
466 };
467
468 /* We are only interested in domain service ports under the
469 * "domain-services" node. On control nodes there is another port
470 * under "openboot" that we should not mess with as aparently that is
471 * reserved exclusively for OBP use.
472 */
473 static void vio_add_ds(struct mdesc_handle *hp, u64 node,
474 const char *node_name)
475 {
476 int found;
477 u64 a;
478
479 found = 0;
480 mdesc_for_each_arc(a, hp, node, MDESC_ARC_TYPE_BACK) {
481 u64 target = mdesc_arc_target(hp, a);
482 const char *name = mdesc_node_name(hp, target);
483
484 if (!strcmp(name, "domain-services")) {
485 found = 1;
486 break;
487 }
488 }
489
490 if (found)
491 (void) vio_create_one(hp, node, node_name, &root_vdev->dev);
492 }
493
494 static struct mdesc_notifier_client vio_ds_notifier = {
495 .add = vio_add_ds,
496 .remove = vio_remove,
497 .node_name = "domain-services-port",
498 };
499
500 static const char *channel_devices_node = "channel-devices";
501 static const char *channel_devices_compat = "SUNW,sun4v-channel-devices";
502 static const char *cfg_handle_prop = "cfg-handle";
503
504 static int __init vio_init(void)
505 {
506 struct mdesc_handle *hp;
507 const char *compat;
508 const u64 *cfg_handle;
509 int err, len;
510 u64 root;
511
512 err = bus_register(&vio_bus_type);
513 if (err) {
514 printk(KERN_ERR "VIO: Could not register bus type err=%d\n",
515 err);
516 return err;
517 }
518
519 hp = mdesc_grab();
520 if (!hp)
521 return 0;
522
523 root = mdesc_node_by_name(hp, MDESC_NODE_NULL, channel_devices_node);
524 if (root == MDESC_NODE_NULL) {
525 printk(KERN_INFO "VIO: No channel-devices MDESC node.\n");
526 mdesc_release(hp);
527 return 0;
528 }
529
530 cdev_node = of_find_node_by_name(NULL, "channel-devices");
531 err = -ENODEV;
532 if (!cdev_node) {
533 printk(KERN_INFO "VIO: No channel-devices OBP node.\n");
534 goto out_release;
535 }
536
537 compat = mdesc_get_property(hp, root, "compatible", &len);
538 if (!compat) {
539 printk(KERN_ERR "VIO: Channel devices lacks compatible "
540 "property\n");
541 goto out_release;
542 }
543 if (!of_find_in_proplist(compat, channel_devices_compat, len)) {
544 printk(KERN_ERR "VIO: Channel devices node lacks (%s) "
545 "compat entry.\n", channel_devices_compat);
546 goto out_release;
547 }
548
549 cfg_handle = mdesc_get_property(hp, root, cfg_handle_prop, NULL);
550 if (!cfg_handle) {
551 printk(KERN_ERR "VIO: Channel devices lacks %s property\n",
552 cfg_handle_prop);
553 goto out_release;
554 }
555
556 cdev_cfg_handle = *cfg_handle;
557
558 root_vdev = vio_create_one(hp, root, NULL, NULL);
559 err = -ENODEV;
560 if (!root_vdev) {
561 printk(KERN_ERR "VIO: Could not create root device.\n");
562 goto out_release;
563 }
564
565 mdesc_register_notifier(&vio_device_notifier);
566 mdesc_register_notifier(&vio_ds_notifier);
567
568 mdesc_release(hp);
569
570 return err;
571
572 out_release:
573 mdesc_release(hp);
574 return err;
575 }
576
577 postcore_initcall(vio_init);