^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /* IBM POWER Barrier Synchronization Register Driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Copyright IBM Corporation 2008
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Author: Sonny Rao <sonnyrao@us.ibm.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/of.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/of_address.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/of_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/of_platform.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/cdev.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/list.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <asm/io.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) This driver exposes a special register which can be used for fast
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) synchronization across a large SMP machine. The hardware is exposed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) as an array of bytes where each process will write to one of the bytes to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) indicate it has finished the current stage and this update is broadcast to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) all processors without having to bounce a cacheline between them. In
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) POWER5 and POWER6 there is one of these registers per SMP, but it is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) presented in two forms; first, it is given as a whole and then as a number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) of smaller registers which alias to parts of the single whole register.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) This can potentially allow multiple groups of processes to each have their
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) own private synchronization device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) Note that this hardware *must* be written to using *only* single byte writes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) It may be read using 1, 2, 4, or 8 byte loads which must be aligned since
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) this region is treated as cache-inhibited processes should also use a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) full sync before and after writing to the BSR to ensure all stores and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) the BSR update have made it to all chips in the system
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) /* This is arbitrary number, up to Power6 it's been 17 or fewer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) #define BSR_MAX_DEVS (32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) struct bsr_dev {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) u64 bsr_addr; /* Real address */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) u64 bsr_len; /* length of mem region we can map */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) unsigned bsr_bytes; /* size of the BSR reg itself */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) unsigned bsr_stride; /* interval at which BSR repeats in the page */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) unsigned bsr_type; /* maps to enum below */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) unsigned bsr_num; /* bsr id number for its type */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) int bsr_minor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) struct list_head bsr_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) dev_t bsr_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) struct cdev bsr_cdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) struct device *bsr_device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) char bsr_name[32];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) static unsigned total_bsr_devs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) static struct list_head bsr_devs = LIST_HEAD_INIT(bsr_devs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) static struct class *bsr_class;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) static int bsr_major;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) enum {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) BSR_8 = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) BSR_16 = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) BSR_64 = 2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) BSR_128 = 3,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) BSR_4096 = 4,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) BSR_UNKNOWN = 5,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) BSR_MAX = 6,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) static unsigned bsr_types[BSR_MAX];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) bsr_size_show(struct device *dev, struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) struct bsr_dev *bsr_dev = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) return sprintf(buf, "%u\n", bsr_dev->bsr_bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) static DEVICE_ATTR_RO(bsr_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) bsr_stride_show(struct device *dev, struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) struct bsr_dev *bsr_dev = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) return sprintf(buf, "%u\n", bsr_dev->bsr_stride);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) static DEVICE_ATTR_RO(bsr_stride);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) bsr_length_show(struct device *dev, struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) struct bsr_dev *bsr_dev = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) return sprintf(buf, "%llu\n", bsr_dev->bsr_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) static DEVICE_ATTR_RO(bsr_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) static struct attribute *bsr_dev_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) &dev_attr_bsr_size.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) &dev_attr_bsr_stride.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) &dev_attr_bsr_length.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) ATTRIBUTE_GROUPS(bsr_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) static int bsr_mmap(struct file *filp, struct vm_area_struct *vma)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) unsigned long size = vma->vm_end - vma->vm_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) struct bsr_dev *dev = filp->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) /* check for the case of a small BSR device and map one 4k page for it*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) if (dev->bsr_len < PAGE_SIZE && size == PAGE_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) ret = remap_4k_pfn(vma, vma->vm_start, dev->bsr_addr >> 12,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) vma->vm_page_prot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) else if (size <= dev->bsr_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) ret = io_remap_pfn_range(vma, vma->vm_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) dev->bsr_addr >> PAGE_SHIFT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) size, vma->vm_page_prot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) return -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) static int bsr_open(struct inode *inode, struct file *filp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) struct cdev *cdev = inode->i_cdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) struct bsr_dev *dev = container_of(cdev, struct bsr_dev, bsr_cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) filp->private_data = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) static const struct file_operations bsr_fops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) .owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) .mmap = bsr_mmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) .open = bsr_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) .llseek = noop_llseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) static void bsr_cleanup_devs(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) struct bsr_dev *cur, *n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) list_for_each_entry_safe(cur, n, &bsr_devs, bsr_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) if (cur->bsr_device) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) cdev_del(&cur->bsr_cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) device_del(cur->bsr_device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) list_del(&cur->bsr_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) kfree(cur);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) static int bsr_add_node(struct device_node *bn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) int bsr_stride_len, bsr_bytes_len, num_bsr_devs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) const u32 *bsr_stride;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) const u32 *bsr_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) unsigned i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) int ret = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) bsr_stride = of_get_property(bn, "ibm,lock-stride", &bsr_stride_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) bsr_bytes = of_get_property(bn, "ibm,#lock-bytes", &bsr_bytes_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) if (!bsr_stride || !bsr_bytes ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) (bsr_stride_len != bsr_bytes_len)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) printk(KERN_ERR "bsr of-node has missing/incorrect property\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) num_bsr_devs = bsr_bytes_len / sizeof(u32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) for (i = 0 ; i < num_bsr_devs; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) struct bsr_dev *cur = kzalloc(sizeof(struct bsr_dev),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) struct resource res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) int result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) if (!cur) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) printk(KERN_ERR "Unable to alloc bsr dev\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) result = of_address_to_resource(bn, i, &res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) if (result < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) printk(KERN_ERR "bsr of-node has invalid reg property, skipping\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) kfree(cur);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) cur->bsr_minor = i + total_bsr_devs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) cur->bsr_addr = res.start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) cur->bsr_len = resource_size(&res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) cur->bsr_bytes = bsr_bytes[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) cur->bsr_stride = bsr_stride[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) cur->bsr_dev = MKDEV(bsr_major, i + total_bsr_devs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) /* if we have a bsr_len of > 4k and less then PAGE_SIZE (64k pages) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) /* we can only map 4k of it, so only advertise the 4k in sysfs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) if (cur->bsr_len > 4096 && cur->bsr_len < PAGE_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) cur->bsr_len = 4096;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) switch(cur->bsr_bytes) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) case 8:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) cur->bsr_type = BSR_8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) case 16:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) cur->bsr_type = BSR_16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) case 64:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) cur->bsr_type = BSR_64;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) case 128:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) cur->bsr_type = BSR_128;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) case 4096:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) cur->bsr_type = BSR_4096;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) cur->bsr_type = BSR_UNKNOWN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) cur->bsr_num = bsr_types[cur->bsr_type];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) snprintf(cur->bsr_name, 32, "bsr%d_%d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) cur->bsr_bytes, cur->bsr_num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) cdev_init(&cur->bsr_cdev, &bsr_fops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) result = cdev_add(&cur->bsr_cdev, cur->bsr_dev, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) if (result) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) kfree(cur);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) cur->bsr_device = device_create(bsr_class, NULL, cur->bsr_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) cur, "%s", cur->bsr_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) if (IS_ERR(cur->bsr_device)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) printk(KERN_ERR "device_create failed for %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) cur->bsr_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) cdev_del(&cur->bsr_cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) kfree(cur);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) bsr_types[cur->bsr_type] = cur->bsr_num + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) list_add_tail(&cur->bsr_list, &bsr_devs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) total_bsr_devs += num_bsr_devs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) out_err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) bsr_cleanup_devs();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) static int bsr_create_devs(struct device_node *bn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) while (bn) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) ret = bsr_add_node(bn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) of_node_put(bn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) bn = of_find_compatible_node(bn, NULL, "ibm,bsr");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) static int __init bsr_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) struct device_node *np;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) dev_t bsr_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) int ret = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) np = of_find_compatible_node(NULL, NULL, "ibm,bsr");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) if (!np)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) bsr_class = class_create(THIS_MODULE, "bsr");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) if (IS_ERR(bsr_class)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) printk(KERN_ERR "class_create() failed for bsr_class\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) ret = PTR_ERR(bsr_class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) goto out_err_1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) bsr_class->dev_groups = bsr_dev_groups;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) ret = alloc_chrdev_region(&bsr_dev, 0, BSR_MAX_DEVS, "bsr");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) bsr_major = MAJOR(bsr_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) printk(KERN_ERR "alloc_chrdev_region() failed for bsr\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) goto out_err_2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) ret = bsr_create_devs(np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) np = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) goto out_err_3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) out_err_3:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) unregister_chrdev_region(bsr_dev, BSR_MAX_DEVS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) out_err_2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) class_destroy(bsr_class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) out_err_1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) of_node_put(np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) out_err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) static void __exit bsr_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) bsr_cleanup_devs();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) if (bsr_class)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) class_destroy(bsr_class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) if (bsr_major)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) unregister_chrdev_region(MKDEV(bsr_major, 0), BSR_MAX_DEVS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) module_init(bsr_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) module_exit(bsr_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) MODULE_AUTHOR("Sonny Rao <sonnyrao@us.ibm.com>");