Merge tag 'io_uring-5.11-2021-01-16' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / vme / vme.c
blob54d7963c10782fed64d0b5a7147b52037da2c123
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * VME Bridge Framework
5 * Author: Martyn Welch <martyn.welch@ge.com>
6 * Copyright 2008 GE Intelligent Platforms Embedded Systems, Inc.
8 * Based on work by Tom Armistead and Ajit Prem
9 * Copyright 2004 Motorola Inc.
12 #include <linux/init.h>
13 #include <linux/export.h>
14 #include <linux/mm.h>
15 #include <linux/types.h>
16 #include <linux/kernel.h>
17 #include <linux/errno.h>
18 #include <linux/pci.h>
19 #include <linux/poll.h>
20 #include <linux/highmem.h>
21 #include <linux/interrupt.h>
22 #include <linux/pagemap.h>
23 #include <linux/device.h>
24 #include <linux/dma-mapping.h>
25 #include <linux/syscalls.h>
26 #include <linux/mutex.h>
27 #include <linux/spinlock.h>
28 #include <linux/slab.h>
29 #include <linux/vme.h>
31 #include "vme_bridge.h"
33 /* Bitmask and list of registered buses both protected by common mutex */
34 static unsigned int vme_bus_numbers;
35 static LIST_HEAD(vme_bus_list);
36 static DEFINE_MUTEX(vme_buses_lock);
38 static int __init vme_init(void);
40 static struct vme_dev *dev_to_vme_dev(struct device *dev)
42 return container_of(dev, struct vme_dev, dev);
46 * Find the bridge that the resource is associated with.
48 static struct vme_bridge *find_bridge(struct vme_resource *resource)
50 /* Get list to search */
51 switch (resource->type) {
52 case VME_MASTER:
53 return list_entry(resource->entry, struct vme_master_resource,
54 list)->parent;
55 case VME_SLAVE:
56 return list_entry(resource->entry, struct vme_slave_resource,
57 list)->parent;
58 case VME_DMA:
59 return list_entry(resource->entry, struct vme_dma_resource,
60 list)->parent;
61 case VME_LM:
62 return list_entry(resource->entry, struct vme_lm_resource,
63 list)->parent;
64 default:
65 printk(KERN_ERR "Unknown resource type\n");
66 return NULL;
70 /**
71 * vme_alloc_consistent - Allocate contiguous memory.
72 * @resource: Pointer to VME resource.
73 * @size: Size of allocation required.
74 * @dma: Pointer to variable to store physical address of allocation.
76 * Allocate a contiguous block of memory for use by the driver. This is used to
77 * create the buffers for the slave windows.
79 * Return: Virtual address of allocation on success, NULL on failure.
81 void *vme_alloc_consistent(struct vme_resource *resource, size_t size,
82 dma_addr_t *dma)
84 struct vme_bridge *bridge;
86 if (!resource) {
87 printk(KERN_ERR "No resource\n");
88 return NULL;
91 bridge = find_bridge(resource);
92 if (!bridge) {
93 printk(KERN_ERR "Can't find bridge\n");
94 return NULL;
97 if (!bridge->parent) {
98 printk(KERN_ERR "Dev entry NULL for bridge %s\n", bridge->name);
99 return NULL;
102 if (!bridge->alloc_consistent) {
103 printk(KERN_ERR "alloc_consistent not supported by bridge %s\n",
104 bridge->name);
105 return NULL;
108 return bridge->alloc_consistent(bridge->parent, size, dma);
110 EXPORT_SYMBOL(vme_alloc_consistent);
113 * vme_free_consistent - Free previously allocated memory.
114 * @resource: Pointer to VME resource.
115 * @size: Size of allocation to free.
116 * @vaddr: Virtual address of allocation.
117 * @dma: Physical address of allocation.
119 * Free previously allocated block of contiguous memory.
121 void vme_free_consistent(struct vme_resource *resource, size_t size,
122 void *vaddr, dma_addr_t dma)
124 struct vme_bridge *bridge;
126 if (!resource) {
127 printk(KERN_ERR "No resource\n");
128 return;
131 bridge = find_bridge(resource);
132 if (!bridge) {
133 printk(KERN_ERR "Can't find bridge\n");
134 return;
137 if (!bridge->parent) {
138 printk(KERN_ERR "Dev entry NULL for bridge %s\n", bridge->name);
139 return;
142 if (!bridge->free_consistent) {
143 printk(KERN_ERR "free_consistent not supported by bridge %s\n",
144 bridge->name);
145 return;
148 bridge->free_consistent(bridge->parent, size, vaddr, dma);
150 EXPORT_SYMBOL(vme_free_consistent);
153 * vme_get_size - Helper function returning size of a VME window
154 * @resource: Pointer to VME slave or master resource.
156 * Determine the size of the VME window provided. This is a helper
157 * function, wrappering the call to vme_master_get or vme_slave_get
158 * depending on the type of window resource handed to it.
160 * Return: Size of the window on success, zero on failure.
162 size_t vme_get_size(struct vme_resource *resource)
164 int enabled, retval;
165 unsigned long long base, size;
166 dma_addr_t buf_base;
167 u32 aspace, cycle, dwidth;
169 switch (resource->type) {
170 case VME_MASTER:
171 retval = vme_master_get(resource, &enabled, &base, &size,
172 &aspace, &cycle, &dwidth);
173 if (retval)
174 return 0;
176 return size;
177 case VME_SLAVE:
178 retval = vme_slave_get(resource, &enabled, &base, &size,
179 &buf_base, &aspace, &cycle);
180 if (retval)
181 return 0;
183 return size;
184 case VME_DMA:
185 return 0;
186 default:
187 printk(KERN_ERR "Unknown resource type\n");
188 return 0;
191 EXPORT_SYMBOL(vme_get_size);
193 int vme_check_window(u32 aspace, unsigned long long vme_base,
194 unsigned long long size)
196 int retval = 0;
198 if (vme_base + size < size)
199 return -EINVAL;
201 switch (aspace) {
202 case VME_A16:
203 if (vme_base + size > VME_A16_MAX)
204 retval = -EFAULT;
205 break;
206 case VME_A24:
207 if (vme_base + size > VME_A24_MAX)
208 retval = -EFAULT;
209 break;
210 case VME_A32:
211 if (vme_base + size > VME_A32_MAX)
212 retval = -EFAULT;
213 break;
214 case VME_A64:
215 /* The VME_A64_MAX limit is actually U64_MAX + 1 */
216 break;
217 case VME_CRCSR:
218 if (vme_base + size > VME_CRCSR_MAX)
219 retval = -EFAULT;
220 break;
221 case VME_USER1:
222 case VME_USER2:
223 case VME_USER3:
224 case VME_USER4:
225 /* User Defined */
226 break;
227 default:
228 printk(KERN_ERR "Invalid address space\n");
229 retval = -EINVAL;
230 break;
233 return retval;
235 EXPORT_SYMBOL(vme_check_window);
237 static u32 vme_get_aspace(int am)
239 switch (am) {
240 case 0x29:
241 case 0x2D:
242 return VME_A16;
243 case 0x38:
244 case 0x39:
245 case 0x3A:
246 case 0x3B:
247 case 0x3C:
248 case 0x3D:
249 case 0x3E:
250 case 0x3F:
251 return VME_A24;
252 case 0x8:
253 case 0x9:
254 case 0xA:
255 case 0xB:
256 case 0xC:
257 case 0xD:
258 case 0xE:
259 case 0xF:
260 return VME_A32;
261 case 0x0:
262 case 0x1:
263 case 0x3:
264 return VME_A64;
267 return 0;
271 * vme_slave_request - Request a VME slave window resource.
272 * @vdev: Pointer to VME device struct vme_dev assigned to driver instance.
273 * @address: Required VME address space.
274 * @cycle: Required VME data transfer cycle type.
276 * Request use of a VME window resource capable of being set for the requested
277 * address space and data transfer cycle.
279 * Return: Pointer to VME resource on success, NULL on failure.
281 struct vme_resource *vme_slave_request(struct vme_dev *vdev, u32 address,
282 u32 cycle)
284 struct vme_bridge *bridge;
285 struct list_head *slave_pos = NULL;
286 struct vme_slave_resource *allocated_image = NULL;
287 struct vme_slave_resource *slave_image = NULL;
288 struct vme_resource *resource = NULL;
290 bridge = vdev->bridge;
291 if (!bridge) {
292 printk(KERN_ERR "Can't find VME bus\n");
293 goto err_bus;
296 /* Loop through slave resources */
297 list_for_each(slave_pos, &bridge->slave_resources) {
298 slave_image = list_entry(slave_pos,
299 struct vme_slave_resource, list);
301 if (!slave_image) {
302 printk(KERN_ERR "Registered NULL Slave resource\n");
303 continue;
306 /* Find an unlocked and compatible image */
307 mutex_lock(&slave_image->mtx);
308 if (((slave_image->address_attr & address) == address) &&
309 ((slave_image->cycle_attr & cycle) == cycle) &&
310 (slave_image->locked == 0)) {
312 slave_image->locked = 1;
313 mutex_unlock(&slave_image->mtx);
314 allocated_image = slave_image;
315 break;
317 mutex_unlock(&slave_image->mtx);
320 /* No free image */
321 if (!allocated_image)
322 goto err_image;
324 resource = kmalloc(sizeof(*resource), GFP_KERNEL);
325 if (!resource)
326 goto err_alloc;
328 resource->type = VME_SLAVE;
329 resource->entry = &allocated_image->list;
331 return resource;
333 err_alloc:
334 /* Unlock image */
335 mutex_lock(&slave_image->mtx);
336 slave_image->locked = 0;
337 mutex_unlock(&slave_image->mtx);
338 err_image:
339 err_bus:
340 return NULL;
342 EXPORT_SYMBOL(vme_slave_request);
345 * vme_slave_set - Set VME slave window configuration.
346 * @resource: Pointer to VME slave resource.
347 * @enabled: State to which the window should be configured.
348 * @vme_base: Base address for the window.
349 * @size: Size of the VME window.
350 * @buf_base: Based address of buffer used to provide VME slave window storage.
351 * @aspace: VME address space for the VME window.
352 * @cycle: VME data transfer cycle type for the VME window.
354 * Set configuration for provided VME slave window.
356 * Return: Zero on success, -EINVAL if operation is not supported on this
357 * device, if an invalid resource has been provided or invalid
358 * attributes are provided. Hardware specific errors may also be
359 * returned.
361 int vme_slave_set(struct vme_resource *resource, int enabled,
362 unsigned long long vme_base, unsigned long long size,
363 dma_addr_t buf_base, u32 aspace, u32 cycle)
365 struct vme_bridge *bridge = find_bridge(resource);
366 struct vme_slave_resource *image;
367 int retval;
369 if (resource->type != VME_SLAVE) {
370 printk(KERN_ERR "Not a slave resource\n");
371 return -EINVAL;
374 image = list_entry(resource->entry, struct vme_slave_resource, list);
376 if (!bridge->slave_set) {
377 printk(KERN_ERR "Function not supported\n");
378 return -ENOSYS;
381 if (!(((image->address_attr & aspace) == aspace) &&
382 ((image->cycle_attr & cycle) == cycle))) {
383 printk(KERN_ERR "Invalid attributes\n");
384 return -EINVAL;
387 retval = vme_check_window(aspace, vme_base, size);
388 if (retval)
389 return retval;
391 return bridge->slave_set(image, enabled, vme_base, size, buf_base,
392 aspace, cycle);
394 EXPORT_SYMBOL(vme_slave_set);
397 * vme_slave_get - Retrieve VME slave window configuration.
398 * @resource: Pointer to VME slave resource.
399 * @enabled: Pointer to variable for storing state.
400 * @vme_base: Pointer to variable for storing window base address.
401 * @size: Pointer to variable for storing window size.
402 * @buf_base: Pointer to variable for storing slave buffer base address.
403 * @aspace: Pointer to variable for storing VME address space.
404 * @cycle: Pointer to variable for storing VME data transfer cycle type.
406 * Return configuration for provided VME slave window.
408 * Return: Zero on success, -EINVAL if operation is not supported on this
409 * device or if an invalid resource has been provided.
411 int vme_slave_get(struct vme_resource *resource, int *enabled,
412 unsigned long long *vme_base, unsigned long long *size,
413 dma_addr_t *buf_base, u32 *aspace, u32 *cycle)
415 struct vme_bridge *bridge = find_bridge(resource);
416 struct vme_slave_resource *image;
418 if (resource->type != VME_SLAVE) {
419 printk(KERN_ERR "Not a slave resource\n");
420 return -EINVAL;
423 image = list_entry(resource->entry, struct vme_slave_resource, list);
425 if (!bridge->slave_get) {
426 printk(KERN_ERR "vme_slave_get not supported\n");
427 return -EINVAL;
430 return bridge->slave_get(image, enabled, vme_base, size, buf_base,
431 aspace, cycle);
433 EXPORT_SYMBOL(vme_slave_get);
436 * vme_slave_free - Free VME slave window
437 * @resource: Pointer to VME slave resource.
439 * Free the provided slave resource so that it may be reallocated.
441 void vme_slave_free(struct vme_resource *resource)
443 struct vme_slave_resource *slave_image;
445 if (resource->type != VME_SLAVE) {
446 printk(KERN_ERR "Not a slave resource\n");
447 return;
450 slave_image = list_entry(resource->entry, struct vme_slave_resource,
451 list);
452 if (!slave_image) {
453 printk(KERN_ERR "Can't find slave resource\n");
454 return;
457 /* Unlock image */
458 mutex_lock(&slave_image->mtx);
459 if (slave_image->locked == 0)
460 printk(KERN_ERR "Image is already free\n");
462 slave_image->locked = 0;
463 mutex_unlock(&slave_image->mtx);
465 /* Free up resource memory */
466 kfree(resource);
468 EXPORT_SYMBOL(vme_slave_free);
471 * vme_master_request - Request a VME master window resource.
472 * @vdev: Pointer to VME device struct vme_dev assigned to driver instance.
473 * @address: Required VME address space.
474 * @cycle: Required VME data transfer cycle type.
475 * @dwidth: Required VME data transfer width.
477 * Request use of a VME window resource capable of being set for the requested
478 * address space, data transfer cycle and width.
480 * Return: Pointer to VME resource on success, NULL on failure.
482 struct vme_resource *vme_master_request(struct vme_dev *vdev, u32 address,
483 u32 cycle, u32 dwidth)
485 struct vme_bridge *bridge;
486 struct list_head *master_pos = NULL;
487 struct vme_master_resource *allocated_image = NULL;
488 struct vme_master_resource *master_image = NULL;
489 struct vme_resource *resource = NULL;
491 bridge = vdev->bridge;
492 if (!bridge) {
493 printk(KERN_ERR "Can't find VME bus\n");
494 goto err_bus;
497 /* Loop through master resources */
498 list_for_each(master_pos, &bridge->master_resources) {
499 master_image = list_entry(master_pos,
500 struct vme_master_resource, list);
502 if (!master_image) {
503 printk(KERN_WARNING "Registered NULL master resource\n");
504 continue;
507 /* Find an unlocked and compatible image */
508 spin_lock(&master_image->lock);
509 if (((master_image->address_attr & address) == address) &&
510 ((master_image->cycle_attr & cycle) == cycle) &&
511 ((master_image->width_attr & dwidth) == dwidth) &&
512 (master_image->locked == 0)) {
514 master_image->locked = 1;
515 spin_unlock(&master_image->lock);
516 allocated_image = master_image;
517 break;
519 spin_unlock(&master_image->lock);
522 /* Check to see if we found a resource */
523 if (!allocated_image) {
524 printk(KERN_ERR "Can't find a suitable resource\n");
525 goto err_image;
528 resource = kmalloc(sizeof(*resource), GFP_KERNEL);
529 if (!resource)
530 goto err_alloc;
532 resource->type = VME_MASTER;
533 resource->entry = &allocated_image->list;
535 return resource;
537 err_alloc:
538 /* Unlock image */
539 spin_lock(&master_image->lock);
540 master_image->locked = 0;
541 spin_unlock(&master_image->lock);
542 err_image:
543 err_bus:
544 return NULL;
546 EXPORT_SYMBOL(vme_master_request);
549 * vme_master_set - Set VME master window configuration.
550 * @resource: Pointer to VME master resource.
551 * @enabled: State to which the window should be configured.
552 * @vme_base: Base address for the window.
553 * @size: Size of the VME window.
554 * @aspace: VME address space for the VME window.
555 * @cycle: VME data transfer cycle type for the VME window.
556 * @dwidth: VME data transfer width for the VME window.
558 * Set configuration for provided VME master window.
560 * Return: Zero on success, -EINVAL if operation is not supported on this
561 * device, if an invalid resource has been provided or invalid
562 * attributes are provided. Hardware specific errors may also be
563 * returned.
565 int vme_master_set(struct vme_resource *resource, int enabled,
566 unsigned long long vme_base, unsigned long long size, u32 aspace,
567 u32 cycle, u32 dwidth)
569 struct vme_bridge *bridge = find_bridge(resource);
570 struct vme_master_resource *image;
571 int retval;
573 if (resource->type != VME_MASTER) {
574 printk(KERN_ERR "Not a master resource\n");
575 return -EINVAL;
578 image = list_entry(resource->entry, struct vme_master_resource, list);
580 if (!bridge->master_set) {
581 printk(KERN_WARNING "vme_master_set not supported\n");
582 return -EINVAL;
585 if (!(((image->address_attr & aspace) == aspace) &&
586 ((image->cycle_attr & cycle) == cycle) &&
587 ((image->width_attr & dwidth) == dwidth))) {
588 printk(KERN_WARNING "Invalid attributes\n");
589 return -EINVAL;
592 retval = vme_check_window(aspace, vme_base, size);
593 if (retval)
594 return retval;
596 return bridge->master_set(image, enabled, vme_base, size, aspace,
597 cycle, dwidth);
599 EXPORT_SYMBOL(vme_master_set);
602 * vme_master_get - Retrieve VME master window configuration.
603 * @resource: Pointer to VME master resource.
604 * @enabled: Pointer to variable for storing state.
605 * @vme_base: Pointer to variable for storing window base address.
606 * @size: Pointer to variable for storing window size.
607 * @aspace: Pointer to variable for storing VME address space.
608 * @cycle: Pointer to variable for storing VME data transfer cycle type.
609 * @dwidth: Pointer to variable for storing VME data transfer width.
611 * Return configuration for provided VME master window.
613 * Return: Zero on success, -EINVAL if operation is not supported on this
614 * device or if an invalid resource has been provided.
616 int vme_master_get(struct vme_resource *resource, int *enabled,
617 unsigned long long *vme_base, unsigned long long *size, u32 *aspace,
618 u32 *cycle, u32 *dwidth)
620 struct vme_bridge *bridge = find_bridge(resource);
621 struct vme_master_resource *image;
623 if (resource->type != VME_MASTER) {
624 printk(KERN_ERR "Not a master resource\n");
625 return -EINVAL;
628 image = list_entry(resource->entry, struct vme_master_resource, list);
630 if (!bridge->master_get) {
631 printk(KERN_WARNING "%s not supported\n", __func__);
632 return -EINVAL;
635 return bridge->master_get(image, enabled, vme_base, size, aspace,
636 cycle, dwidth);
638 EXPORT_SYMBOL(vme_master_get);
641 * vme_master_read - Read data from VME space into a buffer.
642 * @resource: Pointer to VME master resource.
643 * @buf: Pointer to buffer where data should be transferred.
644 * @count: Number of bytes to transfer.
645 * @offset: Offset into VME master window at which to start transfer.
647 * Perform read of count bytes of data from location on VME bus which maps into
648 * the VME master window at offset to buf.
650 * Return: Number of bytes read, -EINVAL if resource is not a VME master
651 * resource or read operation is not supported. -EFAULT returned if
652 * invalid offset is provided. Hardware specific errors may also be
653 * returned.
655 ssize_t vme_master_read(struct vme_resource *resource, void *buf, size_t count,
656 loff_t offset)
658 struct vme_bridge *bridge = find_bridge(resource);
659 struct vme_master_resource *image;
660 size_t length;
662 if (!bridge->master_read) {
663 printk(KERN_WARNING "Reading from resource not supported\n");
664 return -EINVAL;
667 if (resource->type != VME_MASTER) {
668 printk(KERN_ERR "Not a master resource\n");
669 return -EINVAL;
672 image = list_entry(resource->entry, struct vme_master_resource, list);
674 length = vme_get_size(resource);
676 if (offset > length) {
677 printk(KERN_WARNING "Invalid Offset\n");
678 return -EFAULT;
681 if ((offset + count) > length)
682 count = length - offset;
684 return bridge->master_read(image, buf, count, offset);
687 EXPORT_SYMBOL(vme_master_read);
690 * vme_master_write - Write data out to VME space from a buffer.
691 * @resource: Pointer to VME master resource.
692 * @buf: Pointer to buffer holding data to transfer.
693 * @count: Number of bytes to transfer.
694 * @offset: Offset into VME master window at which to start transfer.
696 * Perform write of count bytes of data from buf to location on VME bus which
697 * maps into the VME master window at offset.
699 * Return: Number of bytes written, -EINVAL if resource is not a VME master
700 * resource or write operation is not supported. -EFAULT returned if
701 * invalid offset is provided. Hardware specific errors may also be
702 * returned.
704 ssize_t vme_master_write(struct vme_resource *resource, void *buf,
705 size_t count, loff_t offset)
707 struct vme_bridge *bridge = find_bridge(resource);
708 struct vme_master_resource *image;
709 size_t length;
711 if (!bridge->master_write) {
712 printk(KERN_WARNING "Writing to resource not supported\n");
713 return -EINVAL;
716 if (resource->type != VME_MASTER) {
717 printk(KERN_ERR "Not a master resource\n");
718 return -EINVAL;
721 image = list_entry(resource->entry, struct vme_master_resource, list);
723 length = vme_get_size(resource);
725 if (offset > length) {
726 printk(KERN_WARNING "Invalid Offset\n");
727 return -EFAULT;
730 if ((offset + count) > length)
731 count = length - offset;
733 return bridge->master_write(image, buf, count, offset);
735 EXPORT_SYMBOL(vme_master_write);
738 * vme_master_rmw - Perform read-modify-write cycle.
739 * @resource: Pointer to VME master resource.
740 * @mask: Bits to be compared and swapped in operation.
741 * @compare: Bits to be compared with data read from offset.
742 * @swap: Bits to be swapped in data read from offset.
743 * @offset: Offset into VME master window at which to perform operation.
745 * Perform read-modify-write cycle on provided location:
746 * - Location on VME bus is read.
747 * - Bits selected by mask are compared with compare.
748 * - Where a selected bit matches that in compare and are selected in swap,
749 * the bit is swapped.
750 * - Result written back to location on VME bus.
752 * Return: Bytes written on success, -EINVAL if resource is not a VME master
753 * resource or RMW operation is not supported. Hardware specific
754 * errors may also be returned.
756 unsigned int vme_master_rmw(struct vme_resource *resource, unsigned int mask,
757 unsigned int compare, unsigned int swap, loff_t offset)
759 struct vme_bridge *bridge = find_bridge(resource);
760 struct vme_master_resource *image;
762 if (!bridge->master_rmw) {
763 printk(KERN_WARNING "Writing to resource not supported\n");
764 return -EINVAL;
767 if (resource->type != VME_MASTER) {
768 printk(KERN_ERR "Not a master resource\n");
769 return -EINVAL;
772 image = list_entry(resource->entry, struct vme_master_resource, list);
774 return bridge->master_rmw(image, mask, compare, swap, offset);
776 EXPORT_SYMBOL(vme_master_rmw);
779 * vme_master_mmap - Mmap region of VME master window.
780 * @resource: Pointer to VME master resource.
781 * @vma: Pointer to definition of user mapping.
783 * Memory map a region of the VME master window into user space.
785 * Return: Zero on success, -EINVAL if resource is not a VME master
786 * resource or -EFAULT if map exceeds window size. Other generic mmap
787 * errors may also be returned.
789 int vme_master_mmap(struct vme_resource *resource, struct vm_area_struct *vma)
791 struct vme_master_resource *image;
792 phys_addr_t phys_addr;
793 unsigned long vma_size;
795 if (resource->type != VME_MASTER) {
796 pr_err("Not a master resource\n");
797 return -EINVAL;
800 image = list_entry(resource->entry, struct vme_master_resource, list);
801 phys_addr = image->bus_resource.start + (vma->vm_pgoff << PAGE_SHIFT);
802 vma_size = vma->vm_end - vma->vm_start;
804 if (phys_addr + vma_size > image->bus_resource.end + 1) {
805 pr_err("Map size cannot exceed the window size\n");
806 return -EFAULT;
809 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
811 return vm_iomap_memory(vma, phys_addr, vma->vm_end - vma->vm_start);
813 EXPORT_SYMBOL(vme_master_mmap);
816 * vme_master_free - Free VME master window
817 * @resource: Pointer to VME master resource.
819 * Free the provided master resource so that it may be reallocated.
821 void vme_master_free(struct vme_resource *resource)
823 struct vme_master_resource *master_image;
825 if (resource->type != VME_MASTER) {
826 printk(KERN_ERR "Not a master resource\n");
827 return;
830 master_image = list_entry(resource->entry, struct vme_master_resource,
831 list);
832 if (!master_image) {
833 printk(KERN_ERR "Can't find master resource\n");
834 return;
837 /* Unlock image */
838 spin_lock(&master_image->lock);
839 if (master_image->locked == 0)
840 printk(KERN_ERR "Image is already free\n");
842 master_image->locked = 0;
843 spin_unlock(&master_image->lock);
845 /* Free up resource memory */
846 kfree(resource);
848 EXPORT_SYMBOL(vme_master_free);
851 * vme_dma_request - Request a DMA controller.
852 * @vdev: Pointer to VME device struct vme_dev assigned to driver instance.
853 * @route: Required src/destination combination.
855 * Request a VME DMA controller with capability to perform transfers bewteen
856 * requested source/destination combination.
858 * Return: Pointer to VME DMA resource on success, NULL on failure.
860 struct vme_resource *vme_dma_request(struct vme_dev *vdev, u32 route)
862 struct vme_bridge *bridge;
863 struct list_head *dma_pos = NULL;
864 struct vme_dma_resource *allocated_ctrlr = NULL;
865 struct vme_dma_resource *dma_ctrlr = NULL;
866 struct vme_resource *resource = NULL;
868 /* XXX Not checking resource attributes */
869 printk(KERN_ERR "No VME resource Attribute tests done\n");
871 bridge = vdev->bridge;
872 if (!bridge) {
873 printk(KERN_ERR "Can't find VME bus\n");
874 goto err_bus;
877 /* Loop through DMA resources */
878 list_for_each(dma_pos, &bridge->dma_resources) {
879 dma_ctrlr = list_entry(dma_pos,
880 struct vme_dma_resource, list);
881 if (!dma_ctrlr) {
882 printk(KERN_ERR "Registered NULL DMA resource\n");
883 continue;
886 /* Find an unlocked and compatible controller */
887 mutex_lock(&dma_ctrlr->mtx);
888 if (((dma_ctrlr->route_attr & route) == route) &&
889 (dma_ctrlr->locked == 0)) {
891 dma_ctrlr->locked = 1;
892 mutex_unlock(&dma_ctrlr->mtx);
893 allocated_ctrlr = dma_ctrlr;
894 break;
896 mutex_unlock(&dma_ctrlr->mtx);
899 /* Check to see if we found a resource */
900 if (!allocated_ctrlr)
901 goto err_ctrlr;
903 resource = kmalloc(sizeof(*resource), GFP_KERNEL);
904 if (!resource)
905 goto err_alloc;
907 resource->type = VME_DMA;
908 resource->entry = &allocated_ctrlr->list;
910 return resource;
912 err_alloc:
913 /* Unlock image */
914 mutex_lock(&dma_ctrlr->mtx);
915 dma_ctrlr->locked = 0;
916 mutex_unlock(&dma_ctrlr->mtx);
917 err_ctrlr:
918 err_bus:
919 return NULL;
921 EXPORT_SYMBOL(vme_dma_request);
924 * vme_new_dma_list - Create new VME DMA list.
925 * @resource: Pointer to VME DMA resource.
927 * Create a new VME DMA list. It is the responsibility of the user to free
928 * the list once it is no longer required with vme_dma_list_free().
930 * Return: Pointer to new VME DMA list, NULL on allocation failure or invalid
931 * VME DMA resource.
933 struct vme_dma_list *vme_new_dma_list(struct vme_resource *resource)
935 struct vme_dma_list *dma_list;
937 if (resource->type != VME_DMA) {
938 printk(KERN_ERR "Not a DMA resource\n");
939 return NULL;
942 dma_list = kmalloc(sizeof(*dma_list), GFP_KERNEL);
943 if (!dma_list)
944 return NULL;
946 INIT_LIST_HEAD(&dma_list->entries);
947 dma_list->parent = list_entry(resource->entry,
948 struct vme_dma_resource,
949 list);
950 mutex_init(&dma_list->mtx);
952 return dma_list;
954 EXPORT_SYMBOL(vme_new_dma_list);
957 * vme_dma_pattern_attribute - Create "Pattern" type VME DMA list attribute.
958 * @pattern: Value to use used as pattern
959 * @type: Type of pattern to be written.
961 * Create VME DMA list attribute for pattern generation. It is the
962 * responsibility of the user to free used attributes using
963 * vme_dma_free_attribute().
965 * Return: Pointer to VME DMA attribute, NULL on failure.
967 struct vme_dma_attr *vme_dma_pattern_attribute(u32 pattern, u32 type)
969 struct vme_dma_attr *attributes;
970 struct vme_dma_pattern *pattern_attr;
972 attributes = kmalloc(sizeof(*attributes), GFP_KERNEL);
973 if (!attributes)
974 goto err_attr;
976 pattern_attr = kmalloc(sizeof(*pattern_attr), GFP_KERNEL);
977 if (!pattern_attr)
978 goto err_pat;
980 attributes->type = VME_DMA_PATTERN;
981 attributes->private = (void *)pattern_attr;
983 pattern_attr->pattern = pattern;
984 pattern_attr->type = type;
986 return attributes;
988 err_pat:
989 kfree(attributes);
990 err_attr:
991 return NULL;
993 EXPORT_SYMBOL(vme_dma_pattern_attribute);
996 * vme_dma_pci_attribute - Create "PCI" type VME DMA list attribute.
997 * @address: PCI base address for DMA transfer.
999 * Create VME DMA list attribute pointing to a location on PCI for DMA
1000 * transfers. It is the responsibility of the user to free used attributes
1001 * using vme_dma_free_attribute().
1003 * Return: Pointer to VME DMA attribute, NULL on failure.
1005 struct vme_dma_attr *vme_dma_pci_attribute(dma_addr_t address)
1007 struct vme_dma_attr *attributes;
1008 struct vme_dma_pci *pci_attr;
1010 /* XXX Run some sanity checks here */
1012 attributes = kmalloc(sizeof(*attributes), GFP_KERNEL);
1013 if (!attributes)
1014 goto err_attr;
1016 pci_attr = kmalloc(sizeof(*pci_attr), GFP_KERNEL);
1017 if (!pci_attr)
1018 goto err_pci;
1020 attributes->type = VME_DMA_PCI;
1021 attributes->private = (void *)pci_attr;
1023 pci_attr->address = address;
1025 return attributes;
1027 err_pci:
1028 kfree(attributes);
1029 err_attr:
1030 return NULL;
1032 EXPORT_SYMBOL(vme_dma_pci_attribute);
1035 * vme_dma_vme_attribute - Create "VME" type VME DMA list attribute.
1036 * @address: VME base address for DMA transfer.
1037 * @aspace: VME address space to use for DMA transfer.
1038 * @cycle: VME bus cycle to use for DMA transfer.
1039 * @dwidth: VME data width to use for DMA transfer.
1041 * Create VME DMA list attribute pointing to a location on the VME bus for DMA
1042 * transfers. It is the responsibility of the user to free used attributes
1043 * using vme_dma_free_attribute().
1045 * Return: Pointer to VME DMA attribute, NULL on failure.
1047 struct vme_dma_attr *vme_dma_vme_attribute(unsigned long long address,
1048 u32 aspace, u32 cycle, u32 dwidth)
1050 struct vme_dma_attr *attributes;
1051 struct vme_dma_vme *vme_attr;
1053 attributes = kmalloc(sizeof(*attributes), GFP_KERNEL);
1054 if (!attributes)
1055 goto err_attr;
1057 vme_attr = kmalloc(sizeof(*vme_attr), GFP_KERNEL);
1058 if (!vme_attr)
1059 goto err_vme;
1061 attributes->type = VME_DMA_VME;
1062 attributes->private = (void *)vme_attr;
1064 vme_attr->address = address;
1065 vme_attr->aspace = aspace;
1066 vme_attr->cycle = cycle;
1067 vme_attr->dwidth = dwidth;
1069 return attributes;
1071 err_vme:
1072 kfree(attributes);
1073 err_attr:
1074 return NULL;
1076 EXPORT_SYMBOL(vme_dma_vme_attribute);
1079 * vme_dma_free_attribute - Free DMA list attribute.
1080 * @attributes: Pointer to DMA list attribute.
1082 * Free VME DMA list attribute. VME DMA list attributes can be safely freed
1083 * once vme_dma_list_add() has returned.
1085 void vme_dma_free_attribute(struct vme_dma_attr *attributes)
1087 kfree(attributes->private);
1088 kfree(attributes);
1090 EXPORT_SYMBOL(vme_dma_free_attribute);
1093 * vme_dma_list_add - Add enty to a VME DMA list.
1094 * @list: Pointer to VME list.
1095 * @src: Pointer to DMA list attribute to use as source.
1096 * @dest: Pointer to DMA list attribute to use as destination.
1097 * @count: Number of bytes to transfer.
1099 * Add an entry to the provided VME DMA list. Entry requires pointers to source
1100 * and destination DMA attributes and a count.
1102 * Please note, the attributes supported as source and destinations for
1103 * transfers are hardware dependent.
1105 * Return: Zero on success, -EINVAL if operation is not supported on this
1106 * device or if the link list has already been submitted for execution.
1107 * Hardware specific errors also possible.
1109 int vme_dma_list_add(struct vme_dma_list *list, struct vme_dma_attr *src,
1110 struct vme_dma_attr *dest, size_t count)
1112 struct vme_bridge *bridge = list->parent->parent;
1113 int retval;
1115 if (!bridge->dma_list_add) {
1116 printk(KERN_WARNING "Link List DMA generation not supported\n");
1117 return -EINVAL;
1120 if (!mutex_trylock(&list->mtx)) {
1121 printk(KERN_ERR "Link List already submitted\n");
1122 return -EINVAL;
1125 retval = bridge->dma_list_add(list, src, dest, count);
1127 mutex_unlock(&list->mtx);
1129 return retval;
1131 EXPORT_SYMBOL(vme_dma_list_add);
1134 * vme_dma_list_exec - Queue a VME DMA list for execution.
1135 * @list: Pointer to VME list.
1137 * Queue the provided VME DMA list for execution. The call will return once the
1138 * list has been executed.
1140 * Return: Zero on success, -EINVAL if operation is not supported on this
1141 * device. Hardware specific errors also possible.
1143 int vme_dma_list_exec(struct vme_dma_list *list)
1145 struct vme_bridge *bridge = list->parent->parent;
1146 int retval;
1148 if (!bridge->dma_list_exec) {
1149 printk(KERN_ERR "Link List DMA execution not supported\n");
1150 return -EINVAL;
1153 mutex_lock(&list->mtx);
1155 retval = bridge->dma_list_exec(list);
1157 mutex_unlock(&list->mtx);
1159 return retval;
1161 EXPORT_SYMBOL(vme_dma_list_exec);
1164 * vme_dma_list_free - Free a VME DMA list.
1165 * @list: Pointer to VME list.
1167 * Free the provided DMA list and all its entries.
1169 * Return: Zero on success, -EINVAL on invalid VME resource, -EBUSY if resource
1170 * is still in use. Hardware specific errors also possible.
1172 int vme_dma_list_free(struct vme_dma_list *list)
1174 struct vme_bridge *bridge = list->parent->parent;
1175 int retval;
1177 if (!bridge->dma_list_empty) {
1178 printk(KERN_WARNING "Emptying of Link Lists not supported\n");
1179 return -EINVAL;
1182 if (!mutex_trylock(&list->mtx)) {
1183 printk(KERN_ERR "Link List in use\n");
1184 return -EBUSY;
1188 * Empty out all of the entries from the DMA list. We need to go to the
1189 * low level driver as DMA entries are driver specific.
1191 retval = bridge->dma_list_empty(list);
1192 if (retval) {
1193 printk(KERN_ERR "Unable to empty link-list entries\n");
1194 mutex_unlock(&list->mtx);
1195 return retval;
1197 mutex_unlock(&list->mtx);
1198 kfree(list);
1200 return retval;
1202 EXPORT_SYMBOL(vme_dma_list_free);
1205 * vme_dma_free - Free a VME DMA resource.
1206 * @resource: Pointer to VME DMA resource.
1208 * Free the provided DMA resource so that it may be reallocated.
1210 * Return: Zero on success, -EINVAL on invalid VME resource, -EBUSY if resource
1211 * is still active.
1213 int vme_dma_free(struct vme_resource *resource)
1215 struct vme_dma_resource *ctrlr;
1217 if (resource->type != VME_DMA) {
1218 printk(KERN_ERR "Not a DMA resource\n");
1219 return -EINVAL;
1222 ctrlr = list_entry(resource->entry, struct vme_dma_resource, list);
1224 if (!mutex_trylock(&ctrlr->mtx)) {
1225 printk(KERN_ERR "Resource busy, can't free\n");
1226 return -EBUSY;
1229 if (!(list_empty(&ctrlr->pending) && list_empty(&ctrlr->running))) {
1230 printk(KERN_WARNING "Resource still processing transfers\n");
1231 mutex_unlock(&ctrlr->mtx);
1232 return -EBUSY;
1235 ctrlr->locked = 0;
1237 mutex_unlock(&ctrlr->mtx);
1239 kfree(resource);
1241 return 0;
1243 EXPORT_SYMBOL(vme_dma_free);
1245 void vme_bus_error_handler(struct vme_bridge *bridge,
1246 unsigned long long address, int am)
1248 struct list_head *handler_pos = NULL;
1249 struct vme_error_handler *handler;
1250 int handler_triggered = 0;
1251 u32 aspace = vme_get_aspace(am);
1253 list_for_each(handler_pos, &bridge->vme_error_handlers) {
1254 handler = list_entry(handler_pos, struct vme_error_handler,
1255 list);
1256 if ((aspace == handler->aspace) &&
1257 (address >= handler->start) &&
1258 (address < handler->end)) {
1259 if (!handler->num_errors)
1260 handler->first_error = address;
1261 if (handler->num_errors != UINT_MAX)
1262 handler->num_errors++;
1263 handler_triggered = 1;
1267 if (!handler_triggered)
1268 dev_err(bridge->parent,
1269 "Unhandled VME access error at address 0x%llx\n",
1270 address);
1272 EXPORT_SYMBOL(vme_bus_error_handler);
1274 struct vme_error_handler *vme_register_error_handler(
1275 struct vme_bridge *bridge, u32 aspace,
1276 unsigned long long address, size_t len)
1278 struct vme_error_handler *handler;
1280 handler = kmalloc(sizeof(*handler), GFP_ATOMIC);
1281 if (!handler)
1282 return NULL;
1284 handler->aspace = aspace;
1285 handler->start = address;
1286 handler->end = address + len;
1287 handler->num_errors = 0;
1288 handler->first_error = 0;
1289 list_add_tail(&handler->list, &bridge->vme_error_handlers);
1291 return handler;
1293 EXPORT_SYMBOL(vme_register_error_handler);
1295 void vme_unregister_error_handler(struct vme_error_handler *handler)
1297 list_del(&handler->list);
1298 kfree(handler);
1300 EXPORT_SYMBOL(vme_unregister_error_handler);
1302 void vme_irq_handler(struct vme_bridge *bridge, int level, int statid)
1304 void (*call)(int, int, void *);
1305 void *priv_data;
1307 call = bridge->irq[level - 1].callback[statid].func;
1308 priv_data = bridge->irq[level - 1].callback[statid].priv_data;
1309 if (call)
1310 call(level, statid, priv_data);
1311 else
1312 printk(KERN_WARNING "Spurious VME interrupt, level:%x, vector:%x\n",
1313 level, statid);
1315 EXPORT_SYMBOL(vme_irq_handler);
1318 * vme_irq_request - Request a specific VME interrupt.
1319 * @vdev: Pointer to VME device struct vme_dev assigned to driver instance.
1320 * @level: Interrupt priority being requested.
1321 * @statid: Interrupt vector being requested.
1322 * @callback: Pointer to callback function called when VME interrupt/vector
1323 * received.
1324 * @priv_data: Generic pointer that will be passed to the callback function.
1326 * Request callback to be attached as a handler for VME interrupts with provided
1327 * level and statid.
1329 * Return: Zero on success, -EINVAL on invalid vme device, level or if the
1330 * function is not supported, -EBUSY if the level/statid combination is
1331 * already in use. Hardware specific errors also possible.
1333 int vme_irq_request(struct vme_dev *vdev, int level, int statid,
1334 void (*callback)(int, int, void *),
1335 void *priv_data)
1337 struct vme_bridge *bridge;
1339 bridge = vdev->bridge;
1340 if (!bridge) {
1341 printk(KERN_ERR "Can't find VME bus\n");
1342 return -EINVAL;
1345 if ((level < 1) || (level > 7)) {
1346 printk(KERN_ERR "Invalid interrupt level\n");
1347 return -EINVAL;
1350 if (!bridge->irq_set) {
1351 printk(KERN_ERR "Configuring interrupts not supported\n");
1352 return -EINVAL;
1355 mutex_lock(&bridge->irq_mtx);
1357 if (bridge->irq[level - 1].callback[statid].func) {
1358 mutex_unlock(&bridge->irq_mtx);
1359 printk(KERN_WARNING "VME Interrupt already taken\n");
1360 return -EBUSY;
1363 bridge->irq[level - 1].count++;
1364 bridge->irq[level - 1].callback[statid].priv_data = priv_data;
1365 bridge->irq[level - 1].callback[statid].func = callback;
1367 /* Enable IRQ level */
1368 bridge->irq_set(bridge, level, 1, 1);
1370 mutex_unlock(&bridge->irq_mtx);
1372 return 0;
1374 EXPORT_SYMBOL(vme_irq_request);
1377 * vme_irq_free - Free a VME interrupt.
1378 * @vdev: Pointer to VME device struct vme_dev assigned to driver instance.
1379 * @level: Interrupt priority of interrupt being freed.
1380 * @statid: Interrupt vector of interrupt being freed.
1382 * Remove previously attached callback from VME interrupt priority/vector.
1384 void vme_irq_free(struct vme_dev *vdev, int level, int statid)
1386 struct vme_bridge *bridge;
1388 bridge = vdev->bridge;
1389 if (!bridge) {
1390 printk(KERN_ERR "Can't find VME bus\n");
1391 return;
1394 if ((level < 1) || (level > 7)) {
1395 printk(KERN_ERR "Invalid interrupt level\n");
1396 return;
1399 if (!bridge->irq_set) {
1400 printk(KERN_ERR "Configuring interrupts not supported\n");
1401 return;
1404 mutex_lock(&bridge->irq_mtx);
1406 bridge->irq[level - 1].count--;
1408 /* Disable IRQ level if no more interrupts attached at this level*/
1409 if (bridge->irq[level - 1].count == 0)
1410 bridge->irq_set(bridge, level, 0, 1);
1412 bridge->irq[level - 1].callback[statid].func = NULL;
1413 bridge->irq[level - 1].callback[statid].priv_data = NULL;
1415 mutex_unlock(&bridge->irq_mtx);
1417 EXPORT_SYMBOL(vme_irq_free);
1420 * vme_irq_generate - Generate VME interrupt.
1421 * @vdev: Pointer to VME device struct vme_dev assigned to driver instance.
1422 * @level: Interrupt priority at which to assert the interrupt.
1423 * @statid: Interrupt vector to associate with the interrupt.
1425 * Generate a VME interrupt of the provided level and with the provided
1426 * statid.
1428 * Return: Zero on success, -EINVAL on invalid vme device, level or if the
1429 * function is not supported. Hardware specific errors also possible.
1431 int vme_irq_generate(struct vme_dev *vdev, int level, int statid)
1433 struct vme_bridge *bridge;
1435 bridge = vdev->bridge;
1436 if (!bridge) {
1437 printk(KERN_ERR "Can't find VME bus\n");
1438 return -EINVAL;
1441 if ((level < 1) || (level > 7)) {
1442 printk(KERN_WARNING "Invalid interrupt level\n");
1443 return -EINVAL;
1446 if (!bridge->irq_generate) {
1447 printk(KERN_WARNING "Interrupt generation not supported\n");
1448 return -EINVAL;
1451 return bridge->irq_generate(bridge, level, statid);
1453 EXPORT_SYMBOL(vme_irq_generate);
1456 * vme_lm_request - Request a VME location monitor
1457 * @vdev: Pointer to VME device struct vme_dev assigned to driver instance.
1459 * Allocate a location monitor resource to the driver. A location monitor
1460 * allows the driver to monitor accesses to a contiguous number of
1461 * addresses on the VME bus.
1463 * Return: Pointer to a VME resource on success or NULL on failure.
1465 struct vme_resource *vme_lm_request(struct vme_dev *vdev)
1467 struct vme_bridge *bridge;
1468 struct list_head *lm_pos = NULL;
1469 struct vme_lm_resource *allocated_lm = NULL;
1470 struct vme_lm_resource *lm = NULL;
1471 struct vme_resource *resource = NULL;
1473 bridge = vdev->bridge;
1474 if (!bridge) {
1475 printk(KERN_ERR "Can't find VME bus\n");
1476 goto err_bus;
1479 /* Loop through LM resources */
1480 list_for_each(lm_pos, &bridge->lm_resources) {
1481 lm = list_entry(lm_pos,
1482 struct vme_lm_resource, list);
1483 if (!lm) {
1484 printk(KERN_ERR "Registered NULL Location Monitor resource\n");
1485 continue;
1488 /* Find an unlocked controller */
1489 mutex_lock(&lm->mtx);
1490 if (lm->locked == 0) {
1491 lm->locked = 1;
1492 mutex_unlock(&lm->mtx);
1493 allocated_lm = lm;
1494 break;
1496 mutex_unlock(&lm->mtx);
1499 /* Check to see if we found a resource */
1500 if (!allocated_lm)
1501 goto err_lm;
1503 resource = kmalloc(sizeof(*resource), GFP_KERNEL);
1504 if (!resource)
1505 goto err_alloc;
1507 resource->type = VME_LM;
1508 resource->entry = &allocated_lm->list;
1510 return resource;
1512 err_alloc:
1513 /* Unlock image */
1514 mutex_lock(&lm->mtx);
1515 lm->locked = 0;
1516 mutex_unlock(&lm->mtx);
1517 err_lm:
1518 err_bus:
1519 return NULL;
1521 EXPORT_SYMBOL(vme_lm_request);
1524 * vme_lm_count - Determine number of VME Addresses monitored
1525 * @resource: Pointer to VME location monitor resource.
1527 * The number of contiguous addresses monitored is hardware dependent.
1528 * Return the number of contiguous addresses monitored by the
1529 * location monitor.
1531 * Return: Count of addresses monitored or -EINVAL when provided with an
1532 * invalid location monitor resource.
1534 int vme_lm_count(struct vme_resource *resource)
1536 struct vme_lm_resource *lm;
1538 if (resource->type != VME_LM) {
1539 printk(KERN_ERR "Not a Location Monitor resource\n");
1540 return -EINVAL;
1543 lm = list_entry(resource->entry, struct vme_lm_resource, list);
1545 return lm->monitors;
1547 EXPORT_SYMBOL(vme_lm_count);
1550 * vme_lm_set - Configure location monitor
1551 * @resource: Pointer to VME location monitor resource.
1552 * @lm_base: Base address to monitor.
1553 * @aspace: VME address space to monitor.
1554 * @cycle: VME bus cycle type to monitor.
1556 * Set the base address, address space and cycle type of accesses to be
1557 * monitored by the location monitor.
1559 * Return: Zero on success, -EINVAL when provided with an invalid location
1560 * monitor resource or function is not supported. Hardware specific
1561 * errors may also be returned.
1563 int vme_lm_set(struct vme_resource *resource, unsigned long long lm_base,
1564 u32 aspace, u32 cycle)
1566 struct vme_bridge *bridge = find_bridge(resource);
1567 struct vme_lm_resource *lm;
1569 if (resource->type != VME_LM) {
1570 printk(KERN_ERR "Not a Location Monitor resource\n");
1571 return -EINVAL;
1574 lm = list_entry(resource->entry, struct vme_lm_resource, list);
1576 if (!bridge->lm_set) {
1577 printk(KERN_ERR "vme_lm_set not supported\n");
1578 return -EINVAL;
1581 return bridge->lm_set(lm, lm_base, aspace, cycle);
1583 EXPORT_SYMBOL(vme_lm_set);
1586 * vme_lm_get - Retrieve location monitor settings
1587 * @resource: Pointer to VME location monitor resource.
1588 * @lm_base: Pointer used to output the base address monitored.
1589 * @aspace: Pointer used to output the address space monitored.
1590 * @cycle: Pointer used to output the VME bus cycle type monitored.
1592 * Retrieve the base address, address space and cycle type of accesses to
1593 * be monitored by the location monitor.
1595 * Return: Zero on success, -EINVAL when provided with an invalid location
1596 * monitor resource or function is not supported. Hardware specific
1597 * errors may also be returned.
1599 int vme_lm_get(struct vme_resource *resource, unsigned long long *lm_base,
1600 u32 *aspace, u32 *cycle)
1602 struct vme_bridge *bridge = find_bridge(resource);
1603 struct vme_lm_resource *lm;
1605 if (resource->type != VME_LM) {
1606 printk(KERN_ERR "Not a Location Monitor resource\n");
1607 return -EINVAL;
1610 lm = list_entry(resource->entry, struct vme_lm_resource, list);
1612 if (!bridge->lm_get) {
1613 printk(KERN_ERR "vme_lm_get not supported\n");
1614 return -EINVAL;
1617 return bridge->lm_get(lm, lm_base, aspace, cycle);
1619 EXPORT_SYMBOL(vme_lm_get);
1622 * vme_lm_attach - Provide callback for location monitor address
1623 * @resource: Pointer to VME location monitor resource.
1624 * @monitor: Offset to which callback should be attached.
1625 * @callback: Pointer to callback function called when triggered.
1626 * @data: Generic pointer that will be passed to the callback function.
1628 * Attach a callback to the specificed offset into the location monitors
1629 * monitored addresses. A generic pointer is provided to allow data to be
1630 * passed to the callback when called.
1632 * Return: Zero on success, -EINVAL when provided with an invalid location
1633 * monitor resource or function is not supported. Hardware specific
1634 * errors may also be returned.
1636 int vme_lm_attach(struct vme_resource *resource, int monitor,
1637 void (*callback)(void *), void *data)
1639 struct vme_bridge *bridge = find_bridge(resource);
1640 struct vme_lm_resource *lm;
1642 if (resource->type != VME_LM) {
1643 printk(KERN_ERR "Not a Location Monitor resource\n");
1644 return -EINVAL;
1647 lm = list_entry(resource->entry, struct vme_lm_resource, list);
1649 if (!bridge->lm_attach) {
1650 printk(KERN_ERR "vme_lm_attach not supported\n");
1651 return -EINVAL;
1654 return bridge->lm_attach(lm, monitor, callback, data);
1656 EXPORT_SYMBOL(vme_lm_attach);
1659 * vme_lm_detach - Remove callback for location monitor address
1660 * @resource: Pointer to VME location monitor resource.
1661 * @monitor: Offset to which callback should be removed.
1663 * Remove the callback associated with the specificed offset into the
1664 * location monitors monitored addresses.
1666 * Return: Zero on success, -EINVAL when provided with an invalid location
1667 * monitor resource or function is not supported. Hardware specific
1668 * errors may also be returned.
1670 int vme_lm_detach(struct vme_resource *resource, int monitor)
1672 struct vme_bridge *bridge = find_bridge(resource);
1673 struct vme_lm_resource *lm;
1675 if (resource->type != VME_LM) {
1676 printk(KERN_ERR "Not a Location Monitor resource\n");
1677 return -EINVAL;
1680 lm = list_entry(resource->entry, struct vme_lm_resource, list);
1682 if (!bridge->lm_detach) {
1683 printk(KERN_ERR "vme_lm_detach not supported\n");
1684 return -EINVAL;
1687 return bridge->lm_detach(lm, monitor);
1689 EXPORT_SYMBOL(vme_lm_detach);
1692 * vme_lm_free - Free allocated VME location monitor
1693 * @resource: Pointer to VME location monitor resource.
1695 * Free allocation of a VME location monitor.
1697 * WARNING: This function currently expects that any callbacks that have
1698 * been attached to the location monitor have been removed.
1700 * Return: Zero on success, -EINVAL when provided with an invalid location
1701 * monitor resource.
1703 void vme_lm_free(struct vme_resource *resource)
1705 struct vme_lm_resource *lm;
1707 if (resource->type != VME_LM) {
1708 printk(KERN_ERR "Not a Location Monitor resource\n");
1709 return;
1712 lm = list_entry(resource->entry, struct vme_lm_resource, list);
1714 mutex_lock(&lm->mtx);
1716 /* XXX
1717 * Check to see that there aren't any callbacks still attached, if
1718 * there are we should probably be detaching them!
1721 lm->locked = 0;
1723 mutex_unlock(&lm->mtx);
1725 kfree(resource);
1727 EXPORT_SYMBOL(vme_lm_free);
1730 * vme_slot_num - Retrieve slot ID
1731 * @vdev: Pointer to VME device struct vme_dev assigned to driver instance.
1733 * Retrieve the slot ID associated with the provided VME device.
1735 * Return: The slot ID on success, -EINVAL if VME bridge cannot be determined
1736 * or the function is not supported. Hardware specific errors may also
1737 * be returned.
1739 int vme_slot_num(struct vme_dev *vdev)
1741 struct vme_bridge *bridge;
1743 bridge = vdev->bridge;
1744 if (!bridge) {
1745 printk(KERN_ERR "Can't find VME bus\n");
1746 return -EINVAL;
1749 if (!bridge->slot_get) {
1750 printk(KERN_WARNING "vme_slot_num not supported\n");
1751 return -EINVAL;
1754 return bridge->slot_get(bridge);
1756 EXPORT_SYMBOL(vme_slot_num);
1759 * vme_bus_num - Retrieve bus number
1760 * @vdev: Pointer to VME device struct vme_dev assigned to driver instance.
1762 * Retrieve the bus enumeration associated with the provided VME device.
1764 * Return: The bus number on success, -EINVAL if VME bridge cannot be
1765 * determined.
1767 int vme_bus_num(struct vme_dev *vdev)
1769 struct vme_bridge *bridge;
1771 bridge = vdev->bridge;
1772 if (!bridge) {
1773 pr_err("Can't find VME bus\n");
1774 return -EINVAL;
1777 return bridge->num;
1779 EXPORT_SYMBOL(vme_bus_num);
1781 /* - Bridge Registration --------------------------------------------------- */
1783 static void vme_dev_release(struct device *dev)
1785 kfree(dev_to_vme_dev(dev));
1788 /* Common bridge initialization */
1789 struct vme_bridge *vme_init_bridge(struct vme_bridge *bridge)
1791 INIT_LIST_HEAD(&bridge->vme_error_handlers);
1792 INIT_LIST_HEAD(&bridge->master_resources);
1793 INIT_LIST_HEAD(&bridge->slave_resources);
1794 INIT_LIST_HEAD(&bridge->dma_resources);
1795 INIT_LIST_HEAD(&bridge->lm_resources);
1796 mutex_init(&bridge->irq_mtx);
1798 return bridge;
1800 EXPORT_SYMBOL(vme_init_bridge);
1802 int vme_register_bridge(struct vme_bridge *bridge)
1804 int i;
1805 int ret = -1;
1807 mutex_lock(&vme_buses_lock);
1808 for (i = 0; i < sizeof(vme_bus_numbers) * 8; i++) {
1809 if ((vme_bus_numbers & (1 << i)) == 0) {
1810 vme_bus_numbers |= (1 << i);
1811 bridge->num = i;
1812 INIT_LIST_HEAD(&bridge->devices);
1813 list_add_tail(&bridge->bus_list, &vme_bus_list);
1814 ret = 0;
1815 break;
1818 mutex_unlock(&vme_buses_lock);
1820 return ret;
1822 EXPORT_SYMBOL(vme_register_bridge);
1824 void vme_unregister_bridge(struct vme_bridge *bridge)
1826 struct vme_dev *vdev;
1827 struct vme_dev *tmp;
1829 mutex_lock(&vme_buses_lock);
1830 vme_bus_numbers &= ~(1 << bridge->num);
1831 list_for_each_entry_safe(vdev, tmp, &bridge->devices, bridge_list) {
1832 list_del(&vdev->drv_list);
1833 list_del(&vdev->bridge_list);
1834 device_unregister(&vdev->dev);
1836 list_del(&bridge->bus_list);
1837 mutex_unlock(&vme_buses_lock);
1839 EXPORT_SYMBOL(vme_unregister_bridge);
1841 /* - Driver Registration --------------------------------------------------- */
1843 static int __vme_register_driver_bus(struct vme_driver *drv,
1844 struct vme_bridge *bridge, unsigned int ndevs)
1846 int err;
1847 unsigned int i;
1848 struct vme_dev *vdev;
1849 struct vme_dev *tmp;
1851 for (i = 0; i < ndevs; i++) {
1852 vdev = kzalloc(sizeof(*vdev), GFP_KERNEL);
1853 if (!vdev) {
1854 err = -ENOMEM;
1855 goto err_devalloc;
1857 vdev->num = i;
1858 vdev->bridge = bridge;
1859 vdev->dev.platform_data = drv;
1860 vdev->dev.release = vme_dev_release;
1861 vdev->dev.parent = bridge->parent;
1862 vdev->dev.bus = &vme_bus_type;
1863 dev_set_name(&vdev->dev, "%s.%u-%u", drv->name, bridge->num,
1864 vdev->num);
1866 err = device_register(&vdev->dev);
1867 if (err)
1868 goto err_reg;
1870 if (vdev->dev.platform_data) {
1871 list_add_tail(&vdev->drv_list, &drv->devices);
1872 list_add_tail(&vdev->bridge_list, &bridge->devices);
1873 } else
1874 device_unregister(&vdev->dev);
1876 return 0;
1878 err_reg:
1879 put_device(&vdev->dev);
1880 err_devalloc:
1881 list_for_each_entry_safe(vdev, tmp, &drv->devices, drv_list) {
1882 list_del(&vdev->drv_list);
1883 list_del(&vdev->bridge_list);
1884 device_unregister(&vdev->dev);
1886 return err;
1889 static int __vme_register_driver(struct vme_driver *drv, unsigned int ndevs)
1891 struct vme_bridge *bridge;
1892 int err = 0;
1894 mutex_lock(&vme_buses_lock);
1895 list_for_each_entry(bridge, &vme_bus_list, bus_list) {
1897 * This cannot cause trouble as we already have vme_buses_lock
1898 * and if the bridge is removed, it will have to go through
1899 * vme_unregister_bridge() to do it (which calls remove() on
1900 * the bridge which in turn tries to acquire vme_buses_lock and
1901 * will have to wait).
1903 err = __vme_register_driver_bus(drv, bridge, ndevs);
1904 if (err)
1905 break;
1907 mutex_unlock(&vme_buses_lock);
1908 return err;
1912 * vme_register_driver - Register a VME driver
1913 * @drv: Pointer to VME driver structure to register.
1914 * @ndevs: Maximum number of devices to allow to be enumerated.
1916 * Register a VME device driver with the VME subsystem.
1918 * Return: Zero on success, error value on registration failure.
1920 int vme_register_driver(struct vme_driver *drv, unsigned int ndevs)
1922 int err;
1924 drv->driver.name = drv->name;
1925 drv->driver.bus = &vme_bus_type;
1926 INIT_LIST_HEAD(&drv->devices);
1928 err = driver_register(&drv->driver);
1929 if (err)
1930 return err;
1932 err = __vme_register_driver(drv, ndevs);
1933 if (err)
1934 driver_unregister(&drv->driver);
1936 return err;
1938 EXPORT_SYMBOL(vme_register_driver);
1941 * vme_unregister_driver - Unregister a VME driver
1942 * @drv: Pointer to VME driver structure to unregister.
1944 * Unregister a VME device driver from the VME subsystem.
1946 void vme_unregister_driver(struct vme_driver *drv)
1948 struct vme_dev *dev, *dev_tmp;
1950 mutex_lock(&vme_buses_lock);
1951 list_for_each_entry_safe(dev, dev_tmp, &drv->devices, drv_list) {
1952 list_del(&dev->drv_list);
1953 list_del(&dev->bridge_list);
1954 device_unregister(&dev->dev);
1956 mutex_unlock(&vme_buses_lock);
1958 driver_unregister(&drv->driver);
1960 EXPORT_SYMBOL(vme_unregister_driver);
1962 /* - Bus Registration ------------------------------------------------------ */
1964 static int vme_bus_match(struct device *dev, struct device_driver *drv)
1966 struct vme_driver *vme_drv;
1968 vme_drv = container_of(drv, struct vme_driver, driver);
1970 if (dev->platform_data == vme_drv) {
1971 struct vme_dev *vdev = dev_to_vme_dev(dev);
1973 if (vme_drv->match && vme_drv->match(vdev))
1974 return 1;
1976 dev->platform_data = NULL;
1978 return 0;
1981 static int vme_bus_probe(struct device *dev)
1983 struct vme_driver *driver;
1984 struct vme_dev *vdev = dev_to_vme_dev(dev);
1986 driver = dev->platform_data;
1987 if (driver->probe)
1988 return driver->probe(vdev);
1990 return -ENODEV;
1993 static int vme_bus_remove(struct device *dev)
1995 struct vme_driver *driver;
1996 struct vme_dev *vdev = dev_to_vme_dev(dev);
1998 driver = dev->platform_data;
1999 if (driver->remove)
2000 return driver->remove(vdev);
2002 return -ENODEV;
2005 struct bus_type vme_bus_type = {
2006 .name = "vme",
2007 .match = vme_bus_match,
2008 .probe = vme_bus_probe,
2009 .remove = vme_bus_remove,
2011 EXPORT_SYMBOL(vme_bus_type);
2013 static int __init vme_init(void)
2015 return bus_register(&vme_bus_type);
2017 subsys_initcall(vme_init);