IB/hfi1: Add ASIC resource reservation functions
authorDean Luick <dean.luick@intel.com>
Sat, 5 Mar 2016 16:49:50 +0000 (08:49 -0800)
committerDoug Ledford <dledford@redhat.com>
Thu, 17 Mar 2016 19:55:13 +0000 (15:55 -0400)
The ASIC block is a shared hardware resource between two devices
on the chip.  Add functions to acquire and release these resources
in a way that is safe for both multiple users on the same OS
and multiple users on different OSes, while holding the hardware
mutex as little as possible.

Reservations are noted in a scratch register in the shared region.
There are two types of reservations: per-HFI dynamic and permanent.

Reviewed-by: Mitko Haralanov <mitko.haralanov@intel.com>
Reviewed-by: Easwar Hariharan <easwar.hariharan@intel.com>
Signed-off-by: Dean Luick <dean.luick@intel.com>
Signed-off-by: Jubin John <jubin.john@intel.com>
Signed-off-by: Doug Ledford <dledford@redhat.com>
drivers/staging/rdma/hfi1/chip.c
drivers/staging/rdma/hfi1/chip.h
drivers/staging/rdma/hfi1/firmware.c

index 686cadf449b95d15f12ce50798fd869b19c74147..98ebee4aac22ab1d76fc2a2618a19c2c7199555f 100644 (file)
@@ -13368,6 +13368,7 @@ static void init_chip(struct hfi1_devdata *dd)
         */
        write_csr(dd, ASIC_QSFP1_OUT, 0x1f);
        write_csr(dd, ASIC_QSFP2_OUT, 0x1f);
+       init_chip_resources(dd);
 }
 
 static void init_early_variables(struct hfi1_devdata *dd)
@@ -13794,6 +13795,7 @@ void hfi1_start_cleanup(struct hfi1_devdata *dd)
        free_cntrs(dd);
        free_rcverr(dd);
        clean_up_interrupts(dd);
+       finish_chip_resources(dd);
 }
 
 #define HFI_BASE_GUID(dev) \
index e9a41ed396427499f6f7e4b92edf1f5e191adec4..dc684bc11d273819814f85eae4bd914bfdc4f32b 100644 (file)
@@ -639,6 +639,36 @@ int load_firmware(struct hfi1_devdata *dd);
 void dispose_firmware(void);
 int acquire_hw_mutex(struct hfi1_devdata *dd);
 void release_hw_mutex(struct hfi1_devdata *dd);
+
+/*
+ * Bitmask of dynamic access for ASIC block chip resources.  Each HFI has its
+ * own range of bits for the resource so it can clear its own bits on
+ * starting and exiting.  If either HFI has the resource bit set, the
+ * resource is in use.  The separate bit ranges are:
+ *     HFI0 bits  7:0
+ *     HFI1 bits 15:8
+ */
+#define CR_SBUS  0x01  /* SBUS, THERM, and PCIE registers */
+#define CR_EPROM 0x02  /* EEP, GPIO registers */
+#define CR_I2C1  0x04  /* QSFP1_OE register */
+#define CR_I2C2  0x08  /* QSFP2_OE register */
+#define CR_DYN_SHIFT 8 /* dynamic flag shift */
+#define CR_DYN_MASK  ((1ull << CR_DYN_SHIFT) - 1)
+
+/*
+ * Bitmask of static ASIC states these are outside of the dynamic ASIC
+ * block chip resources above.  These are to be set once and never cleared.
+ * Must be holding the SBus dynamic flag when setting.
+ */
+#define CR_THERM_INIT  0x010000
+
+int acquire_chip_resource(struct hfi1_devdata *dd, u32 resource, u32 mswait);
+void release_chip_resource(struct hfi1_devdata *dd, u32 resource);
+bool check_chip_resource(struct hfi1_devdata *dd, u32 resource,
+                        const char *func);
+void init_chip_resources(struct hfi1_devdata *dd);
+void finish_chip_resources(struct hfi1_devdata *dd);
+
 void fabric_serdes_reset(struct hfi1_devdata *dd);
 int read_8051_data(struct hfi1_devdata *dd, u32 addr, u32 len, u64 *result);
 
index ca4e48988b7073b7bb6d53cd19cae99bd2307388..140dd8646cd0d94b562c6537958518fde51fbf95 100644 (file)
@@ -1385,6 +1385,193 @@ void release_hw_mutex(struct hfi1_devdata *dd)
        write_csr(dd, ASIC_CFG_MUTEX, 0);
 }
 
+/* return the given resource bit(s) as a mask for the given HFI */
+static inline u64 resource_mask(u32 hfi1_id, u32 resource)
+{
+       return ((u64)resource) << (hfi1_id ? CR_DYN_SHIFT : 0);
+}
+
+static void fail_mutex_acquire_message(struct hfi1_devdata *dd,
+                                      const char *func)
+{
+       dd_dev_err(dd,
+                  "%s: hardware mutex stuck - suggest rebooting the machine\n",
+                  func);
+}
+
+/*
+ * Acquire access to a chip resource.
+ *
+ * Return 0 on success, -EBUSY if resource busy, -EIO if mutex acquire failed.
+ */
+static int __acquire_chip_resource(struct hfi1_devdata *dd, u32 resource)
+{
+       u64 scratch0, all_bits, my_bit;
+       int ret;
+
+       if (resource & CR_DYN_MASK) {
+               /* a dynamic resource is in use if either HFI has set the bit */
+               all_bits = resource_mask(0, resource) |
+                                               resource_mask(1, resource);
+               my_bit = resource_mask(dd->hfi1_id, resource);
+       } else {
+               /* non-dynamic resources are not split between HFIs */
+               all_bits = resource;
+               my_bit = resource;
+       }
+
+       /* lock against other callers within the driver wanting a resource */
+       mutex_lock(&dd->asic_data->asic_resource_mutex);
+
+       ret = acquire_hw_mutex(dd);
+       if (ret) {
+               fail_mutex_acquire_message(dd, __func__);
+               ret = -EIO;
+               goto done;
+       }
+
+       scratch0 = read_csr(dd, ASIC_CFG_SCRATCH);
+       if (scratch0 & all_bits) {
+               ret = -EBUSY;
+       } else {
+               write_csr(dd, ASIC_CFG_SCRATCH, scratch0 | my_bit);
+               /* force write to be visible to other HFI on another OS */
+               (void)read_csr(dd, ASIC_CFG_SCRATCH);
+       }
+
+       release_hw_mutex(dd);
+
+done:
+       mutex_unlock(&dd->asic_data->asic_resource_mutex);
+       return ret;
+}
+
+/*
+ * Acquire access to a chip resource, wait up to mswait milliseconds for
+ * the resource to become available.
+ *
+ * Return 0 on success, -EBUSY if busy (even after wait), -EIO if mutex
+ * acquire failed.
+ */
+int acquire_chip_resource(struct hfi1_devdata *dd, u32 resource, u32 mswait)
+{
+       unsigned long timeout;
+       int ret;
+
+       timeout = jiffies + msecs_to_jiffies(mswait);
+       while (1) {
+               ret = __acquire_chip_resource(dd, resource);
+               if (ret != -EBUSY)
+                       return ret;
+               /* resource is busy, check our timeout */
+               if (time_after_eq(jiffies, timeout))
+                       return -EBUSY;
+               usleep_range(80, 120);  /* arbitrary delay */
+       }
+}
+
+/*
+ * Release access to a chip resource
+ */
+void release_chip_resource(struct hfi1_devdata *dd, u32 resource)
+{
+       u64 scratch0, bit;
+
+       /* only dynamic resources should ever be cleared */
+       if (!(resource & CR_DYN_MASK)) {
+               dd_dev_err(dd, "%s: invalid resource 0x%x\n", __func__,
+                          resource);
+               return;
+       }
+       bit = resource_mask(dd->hfi1_id, resource);
+
+       /* lock against other callers within the driver wanting a resource */
+       mutex_lock(&dd->asic_data->asic_resource_mutex);
+
+       if (acquire_hw_mutex(dd)) {
+               fail_mutex_acquire_message(dd, __func__);
+               goto done;
+       }
+
+       scratch0 = read_csr(dd, ASIC_CFG_SCRATCH);
+       if ((scratch0 & bit) != 0) {
+               scratch0 &= ~bit;
+               write_csr(dd, ASIC_CFG_SCRATCH, scratch0);
+               /* force write to be visible to other HFI on another OS */
+               (void)read_csr(dd, ASIC_CFG_SCRATCH);
+       } else {
+               dd_dev_warn(dd, "%s: id %d, resource 0x%x: bit not set\n",
+                           __func__, dd->hfi1_id, resource);
+       }
+
+       release_hw_mutex(dd);
+
+done:
+       mutex_unlock(&dd->asic_data->asic_resource_mutex);
+}
+
+/*
+ * Return true if resource is set, false otherwise.  Print a warning
+ * if not set and a function is supplied.
+ */
+bool check_chip_resource(struct hfi1_devdata *dd, u32 resource,
+                        const char *func)
+{
+       u64 scratch0, bit;
+
+       if (resource & CR_DYN_MASK)
+               bit = resource_mask(dd->hfi1_id, resource);
+       else
+               bit = resource;
+
+       scratch0 = read_csr(dd, ASIC_CFG_SCRATCH);
+       if ((scratch0 & bit) == 0) {
+               if (func)
+                       dd_dev_warn(dd,
+                                   "%s: id %d, resource 0x%x, not acquired!\n",
+                                   func, dd->hfi1_id, resource);
+               return false;
+       }
+       return true;
+}
+
+static void clear_chip_resources(struct hfi1_devdata *dd, const char *func)
+{
+       u64 scratch0;
+
+       /* lock against other callers within the driver wanting a resource */
+       mutex_lock(&dd->asic_data->asic_resource_mutex);
+
+       if (acquire_hw_mutex(dd)) {
+               fail_mutex_acquire_message(dd, func);
+               goto done;
+       }
+
+       /* clear all dynamic access bits for this HFI */
+       scratch0 = read_csr(dd, ASIC_CFG_SCRATCH);
+       scratch0 &= ~resource_mask(dd->hfi1_id, CR_DYN_MASK);
+       write_csr(dd, ASIC_CFG_SCRATCH, scratch0);
+       /* force write to be visible to other HFI on another OS */
+       (void)read_csr(dd, ASIC_CFG_SCRATCH);
+
+       release_hw_mutex(dd);
+
+done:
+       mutex_unlock(&dd->asic_data->asic_resource_mutex);
+}
+
+void init_chip_resources(struct hfi1_devdata *dd)
+{
+       /* clear any holds left by us */
+       clear_chip_resources(dd, __func__);
+}
+
+void finish_chip_resources(struct hfi1_devdata *dd)
+{
+       /* clear any holds left by us */
+       clear_chip_resources(dd, __func__);
+}
+
 void set_sbus_fast_mode(struct hfi1_devdata *dd)
 {
        write_csr(dd, ASIC_CFG_SBUS_EXECUTE,
This page took 0.03293 seconds and 5 git commands to generate.