pxafb: lcsr1 is unused without CONFIG_FB_PXA_OVERLAY
[safe/jmp/linux-2.6] / drivers / lguest / lguest_device.c
index 07f57a5..df44d96 100644 (file)
@@ -1,10 +1,10 @@
 /*P:050 Lguest guests use a very simple method to describe devices.  It's a
- * series of device descriptors contained just above the top of normal
+ * series of device descriptors contained just above the top of normal Guest
  * memory.
  *
  * We use the standard "virtio" device infrastructure, which provides us with a
  * console, a network and a block driver.  Each one expects some configuration
- * information and a "virtqueue" mechanism to send and receive data. :*/
+ * information and a "virtqueue" or two to send and receive data. :*/
 #include <linux/init.h>
 #include <linux/bootmem.h>
 #include <linux/lguest_launcher.h>
 /* The pointer to our (page) of device descriptions. */
 static void *lguest_devices;
 
-/* Unique numbering for lguest devices. */
-static unsigned int dev_index;
-
 /* For Guests, device memory can be used as normal memory, so we cast away the
  * __iomem to quieten sparse. */
 static inline void *lguest_map(unsigned long phys_addr, unsigned long pages)
 {
-       return (__force void *)ioremap(phys_addr, PAGE_SIZE*pages);
+       return (__force void *)ioremap_cache(phys_addr, PAGE_SIZE*pages);
 }
 
 static inline void lguest_unmap(void *addr)
@@ -47,14 +44,14 @@ struct lguest_device {
 /* Since the virtio infrastructure hands us a pointer to the virtio_device all
  * the time, it helps to have a curt macro to get a pointer to the struct
  * lguest_device it's enclosed in.  */
-#define to_lgdev(vdev) container_of(vdev, struct lguest_device, vdev)
+#define to_lgdev(vd) container_of(vd, struct lguest_device, vdev)
 
 /*D:130
  * Device configurations
  *
  * The configuration information for a device consists of one or more
- * virtqueues, a feature bitmaks, and some configuration bytes.  The
- * configuration bytes don't really matter to us: the Launcher set them up, and
+ * virtqueues, a feature bitmap, and some configuration bytes.  The
+ * configuration bytes don't really matter to us: the Launcher sets them up, and
  * the driver will look at them during setup.
  *
  * A convenient routine to return the device's virtqueue config array:
@@ -85,27 +82,46 @@ static unsigned desc_size(const struct lguest_device_desc *desc)
                + desc->config_len;
 }
 
-/* This tests (and acknowleges) a feature bit. */
-static bool lg_feature(struct virtio_device *vdev, unsigned fbit)
+/* This gets the device's feature bits. */
+static u32 lg_get_features(struct virtio_device *vdev)
 {
+       unsigned int i;
+       u32 features = 0;
        struct lguest_device_desc *desc = to_lgdev(vdev)->desc;
-       u8 *features;
-
-       /* Obviously if they ask for a feature off the end of our feature
-        * bitmap, it's not set. */
-       if (fbit / 8 > desc->feature_len)
-               return false;
-
-       /* The feature bitmap comes after the virtqueues. */
-       features = lg_features(desc);
-       if (!(features[fbit / 8] & (1 << (fbit % 8))))
-               return false;
-
-       /* We set the matching bit in the other half of the bitmap to tell the
-        * Host we want to use this feature.  We don't use this yet, but we
-        * could in future. */
-       features[desc->feature_len + fbit / 8] |= (1 << (fbit % 8));
-       return true;
+       u8 *in_features = lg_features(desc);
+
+       /* We do this the slow but generic way. */
+       for (i = 0; i < min(desc->feature_len * 8, 32); i++)
+               if (in_features[i / 8] & (1 << (i % 8)))
+                       features |= (1 << i);
+
+       return features;
+}
+
+/* The virtio core takes the features the Host offers, and copies the
+ * ones supported by the driver into the vdev->features array.  Once
+ * that's all sorted out, this routine is called so we can tell the
+ * Host which features we understand and accept. */
+static void lg_finalize_features(struct virtio_device *vdev)
+{
+       unsigned int i, bits;
+       struct lguest_device_desc *desc = to_lgdev(vdev)->desc;
+       /* Second half of bitmap is features we accept. */
+       u8 *out_features = lg_features(desc) + desc->feature_len;
+
+       /* Give virtio_ring a chance to accept features. */
+       vring_transport_features(vdev);
+
+       /* The vdev->feature array is a Linux bitmask: this isn't the
+        * same as a the simple array of bits used by lguest devices
+        * for features.  So we do this slow, manual conversion which is
+        * completely general. */
+       memset(out_features, 0, desc->feature_len);
+       bits = min_t(unsigned, desc->feature_len, sizeof(vdev->features)) * 8;
+       for (i = 0; i < bits; i++) {
+               if (test_bit(i, vdev->features))
+                       out_features[i / 8] |= (1 << (i % 8));
+       }
 }
 
 /* Once they've found a field, getting a copy of it is easy. */
@@ -137,9 +153,26 @@ static u8 lg_get_status(struct virtio_device *vdev)
        return to_lgdev(vdev)->desc->status;
 }
 
-static void lg_set_status(struct virtio_device *vdev, u8 status)
+/* To notify on status updates, we (ab)use the NOTIFY hypercall, with the
+ * descriptor address of the device.  A zero status means "reset". */
+static void set_status(struct virtio_device *vdev, u8 status)
 {
+       unsigned long offset = (void *)to_lgdev(vdev)->desc - lguest_devices;
+
+       /* We set the status. */
        to_lgdev(vdev)->desc->status = status;
+       kvm_hypercall1(LHCALL_NOTIFY, (max_pfn << PAGE_SHIFT) + offset);
+}
+
+static void lg_set_status(struct virtio_device *vdev, u8 status)
+{
+       BUG_ON(!status);
+       set_status(vdev, status);
+}
+
+static void lg_reset(struct virtio_device *vdev)
+{
+       set_status(vdev, 0);
 }
 
 /*
@@ -168,7 +201,7 @@ struct lguest_vq_info
 };
 
 /* When the virtio_ring code wants to prod the Host, it calls us here and we
- * make a hypercall.  We hand the page number of the virtqueue so the Host
+ * make a hypercall.  We hand the physical address of the virtqueue so the Host
  * knows which virtqueue we're talking about. */
 static void lg_notify(struct virtqueue *vq)
 {
@@ -176,9 +209,12 @@ static void lg_notify(struct virtqueue *vq)
         * virtqueue structure. */
        struct lguest_vq_info *lvq = vq->priv;
 
-       hcall(LHCALL_NOTIFY, lvq->config.pfn << PAGE_SHIFT, 0, 0);
+       kvm_hypercall1(LHCALL_NOTIFY, lvq->config.pfn << PAGE_SHIFT);
 }
 
+/* An extern declaration inside a C file is bad form.  Don't do it. */
+extern void lguest_setup_irq(unsigned int irq);
+
 /* This routine finds the first virtqueue described in the configuration of
  * this device and sets it up.
  *
@@ -188,10 +224,11 @@ static void lg_notify(struct virtqueue *vq)
  * allocate its own pages and tell the Host where they are, but for lguest it's
  * simpler for the Host to simply tell us where the pages are.
  *
- * So we provide devices with a "find virtqueue and set it up" function. */
+ * So we provide drivers with a "find the Nth virtqueue and set it up"
+ * function. */
 static struct virtqueue *lg_find_vq(struct virtio_device *vdev,
                                    unsigned index,
-                                   bool (*callback)(struct virtqueue *vq))
+                                   void (*callback)(struct virtqueue *vq))
 {
        struct lguest_device *ldev = to_lgdev(vdev);
        struct lguest_vq_info *lvq;
@@ -216,7 +253,7 @@ static struct virtqueue *lg_find_vq(struct virtio_device *vdev,
        /* Figure out how many pages the ring will take, and map that memory */
        lvq->pages = lguest_map((unsigned long)lvq->config.pfn << PAGE_SHIFT,
                                DIV_ROUND_UP(vring_size(lvq->config.num,
-                                                       PAGE_SIZE),
+                                                       LGUEST_VRING_ALIGN),
                                             PAGE_SIZE));
        if (!lvq->pages) {
                err = -ENOMEM;
@@ -225,20 +262,23 @@ static struct virtqueue *lg_find_vq(struct virtio_device *vdev,
 
        /* OK, tell virtio_ring.c to set up a virtqueue now we know its size
         * and we've got a pointer to its pages. */
-       vq = vring_new_virtqueue(lvq->config.num, vdev, lvq->pages,
-                                lg_notify, callback);
+       vq = vring_new_virtqueue(lvq->config.num, LGUEST_VRING_ALIGN,
+                                vdev, lvq->pages, lg_notify, callback);
        if (!vq) {
                err = -ENOMEM;
                goto unmap;
        }
 
+       /* Make sure the interrupt is allocated. */
+       lguest_setup_irq(lvq->config.irq);
+
        /* Tell the interrupt for this virtqueue to go to the virtio_ring
         * interrupt handler. */
        /* FIXME: We used to have a flag for the Host to tell us we could use
         * the interrupt as a source of randomness: it'd be nice to have that
         * back.. */
        err = request_irq(lvq->config.irq, vring_interrupt, IRQF_SHARED,
-                         vdev->dev.bus_id, vq);
+                         dev_name(&vdev->dev), vq);
        if (err)
                goto destroy_vring;
 
@@ -274,21 +314,20 @@ static void lg_del_vq(struct virtqueue *vq)
 
 /* The ops structure which hooks everything together. */
 static struct virtio_config_ops lguest_config_ops = {
-       .feature = lg_feature,
+       .get_features = lg_get_features,
+       .finalize_features = lg_finalize_features,
        .get = lg_get,
        .set = lg_set,
        .get_status = lg_get_status,
        .set_status = lg_set_status,
+       .reset = lg_reset,
        .find_vq = lg_find_vq,
        .del_vq = lg_del_vq,
 };
 
 /* The root device for the lguest virtio devices.  This makes them appear as
  * /sys/devices/lguest/0,1,2 not /sys/devices/0,1,2. */
-static struct device lguest_root = {
-       .parent = NULL,
-       .bus_id = "lguest",
-};
+static struct device *lguest_root;
 
 /*D:120 This is the core of the lguest bus: actually adding a new device.
  * It's a separate function because it's neater that way, and because an
@@ -298,8 +337,10 @@ static struct device lguest_root = {
  * As Andrew Tridgell says, "Untested code is buggy code".
  *
  * It's worth reading this carefully: we start with a pointer to the new device
- * descriptor in the "lguest_devices" page. */
-static void add_lguest_device(struct lguest_device_desc *d)
+ * descriptor in the "lguest_devices" page, and the offset into the device
+ * descriptor page so we can uniquely identify it if things go badly wrong. */
+static void add_lguest_device(struct lguest_device_desc *d,
+                             unsigned int offset)
 {
        struct lguest_device *ldev;
 
@@ -307,18 +348,14 @@ static void add_lguest_device(struct lguest_device_desc *d)
         * it. */
        ldev = kzalloc(sizeof(*ldev), GFP_KERNEL);
        if (!ldev) {
-               printk(KERN_EMERG "Cannot allocate lguest dev %u\n",
-                      dev_index++);
+               printk(KERN_EMERG "Cannot allocate lguest dev %u type %u\n",
+                      offset, d->type);
                return;
        }
 
        /* This devices' parent is the lguest/ dir. */
-       ldev->vdev.dev.parent = &lguest_root;
+       ldev->vdev.dev.parent = lguest_root;
        /* We have a unique device index thanks to the dev_index counter. */
-       ldev->vdev.index = dev_index++;
-       /* The device type comes straight from the descriptor.  There's also a
-        * device vendor field in the virtio_device struct, which we leave as
-        * 0. */
        ldev->vdev.id.device = d->type;
        /* We have a simple set of routines for querying the device's
         * configuration information and setting its status. */
@@ -330,8 +367,8 @@ static void add_lguest_device(struct lguest_device_desc *d)
         * virtio_device and calls device_register().  This makes the bus
         * infrastructure look for a matching driver. */
        if (register_virtio_device(&ldev->vdev) != 0) {
-               printk(KERN_ERR "Failed to register lguest device %u\n",
-                      ldev->vdev.index);
+               printk(KERN_ERR "Failed to register lguest dev %u type %u\n",
+                      offset, d->type);
                kfree(ldev);
        }
 }
@@ -352,7 +389,7 @@ static void scan_devices(void)
                        break;
 
                printk("Device at %i has size %u\n", i, desc_size(d));
-               add_lguest_device(d);
+               add_lguest_device(d, i);
        }
 }
 
@@ -373,7 +410,8 @@ static int __init lguest_devices_init(void)
        if (strcmp(pv_info.name, "lguest") != 0)
                return 0;
 
-       if (device_register(&lguest_root) != 0)
+       lguest_root = root_device_register("lguest");
+       if (IS_ERR(lguest_root))
                panic("Could not register lguest root");
 
        /* Devices are in a single page above top of "normal" mem */