From 7ba791b2a72d128b793790f92352b1e8dac04ce6 Mon Sep 17 00:00:00 2001
From: Patrick Bellasi <patrick.bellasi@arm.com>
Date: Tue, 15 Sep 2015 10:21:25 +0100
Subject: [PATCH 1/6] hotplug: make hotplug probing more robust

In general it makes not sense to hotplug out all the CPUs of a system, thus
ususally CPU0 is configured as not hot/plugggable.
Definitively, on a single core system it does not make sense to hotpolug out
the only available CPU.

This patch switch to usage of CPU1 for hotplug support probing, which is the
really first one for which enabling hotplug could be useful.

Signed-off-by: Patrick Bellasi <patrick.bellasi@arm.com>
---
 devlib/module/hotplug.py | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/devlib/module/hotplug.py b/devlib/module/hotplug.py
index d89ce07..8ae238e 100644
--- a/devlib/module/hotplug.py
+++ b/devlib/module/hotplug.py
@@ -8,7 +8,10 @@ class HotplugModule(Module):
 
     @classmethod
     def probe(cls, target):  # pylint: disable=arguments-differ
-        path = cls._cpu_path(target, 0)
+        # If a system has just 1 CPU, it makes not sense to hotplug it.
+        # If a system has more than 1 CPU, CPU0 could be configured to be not
+        # hotpluggable. Thus, check for hotplug support by looking at CPU1
+        path = cls._cpu_path(target, 1)
         return target.file_exists(path) and target.is_rooted
 
     @classmethod
@@ -30,6 +33,8 @@ class HotplugModule(Module):
 
     def hotplug(self, cpu, online):
         path = self._cpu_path(self.target, cpu)
+        if not self.target.file_exists(path):
+            return
         value = 1 if online else 0
         self.target.write_value(path, value)
 

From b19d76d4b065408f4bd6a8d7c197f64f581168ef Mon Sep 17 00:00:00 2001
From: Patrick Bellasi <patrick.bellasi@arm.com>
Date: Tue, 15 Sep 2015 10:25:15 +0100
Subject: [PATCH 2/6] cpufreq: fix probe method to support Intel machine

Recent Intel machines uses the CPUFreq pstate driver, which does not create
the standard "/sys/devices/system/cpu/cpufreq" folder usually created by
other (mostly ARM platform) drivers.

This patch fixes the probe method to check for the propert pstate driver
being available in order to enabled the CPUFreq module.

Signed-off-by: Patrick Bellasi <patrick.bellasi@arm.com>
---
 devlib/module/cpufreq.py | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/devlib/module/cpufreq.py b/devlib/module/cpufreq.py
index 2c7dc36..9aeebe8 100644
--- a/devlib/module/cpufreq.py
+++ b/devlib/module/cpufreq.py
@@ -30,6 +30,8 @@ class CpufreqModule(Module):
     @staticmethod
     def probe(target):
         path = '/sys/devices/system/cpu/cpufreq'
+        if target.abi == 'x86_64':
+            path = '/sys/devices/system/cpu/intel_pstate'
         return target.file_exists(path)
 
     def __init__(self, target):

From cbe80da3a113cc4325feb3348e69790d6dafdd8f Mon Sep 17 00:00:00 2001
From: Patrick Bellasi <patrick.bellasi@arm.com>
Date: Thu, 1 Oct 2015 17:59:12 +0100
Subject: [PATCH 3/6] cpufreq: make probing more robust

On some machines, when a different CPUFreq policy could be configured
for each CPU, there is not a top-level 'cpufreq' folder exposed
at top level but just per-CPU ones.

This patch makes the probing for CPUFreq support more robust by checking
on all the supported paths.

Signed-off-by: Patrick Bellasi <patrick.bellasi@arm.com>
---
 devlib/module/cpufreq.py | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)

diff --git a/devlib/module/cpufreq.py b/devlib/module/cpufreq.py
index 9aeebe8..a41cff2 100644
--- a/devlib/module/cpufreq.py
+++ b/devlib/module/cpufreq.py
@@ -29,9 +29,20 @@ class CpufreqModule(Module):
 
     @staticmethod
     def probe(target):
-        path = '/sys/devices/system/cpu/cpufreq'
+
+        # x86 with Intel P-State driver
         if target.abi == 'x86_64':
             path = '/sys/devices/system/cpu/intel_pstate'
+            if target.file_exists(path):
+                return True
+
+        # Generic CPUFreq support (single policy)
+        path = '/sys/devices/system/cpu/cpufreq'
+        if target.file_exists(path):
+            return True
+
+        # Generic CPUFreq support (per CPU policy)
+        path = '/sys/devices/system/cpu/cpu0/cpufreq'
         return target.file_exists(path)
 
     def __init__(self, target):

From b83e51856d3e613bdab4dbdbc07bb64cd480e575 Mon Sep 17 00:00:00 2001
From: Patrick Bellasi <patrick.bellasi@arm.com>
Date: Mon, 12 Oct 2015 12:37:11 +0100
Subject: [PATCH 4/6] target: fix 'bl' module initialisation

When "load_default_modules" is set to False at target initialisation time
but the "bl" module is explicitly required, the initialization of this module
fails because the system as not yet been identified as a big.LITTLE.
This identification is performed by platform.update_from_target.

This patch post-pone modules initialisation at the end of the connection
method to ensure that all the required information are available at modules
probe time.

Signed-off-by: Patrick Bellasi <patrick.bellasi@arm.com>
---
 devlib/target.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/devlib/target.py b/devlib/target.py
index 9eabb7f..7559b6c 100644
--- a/devlib/target.py
+++ b/devlib/target.py
@@ -177,8 +177,8 @@ class Target(object):
         tid = id(threading.current_thread())
         self._connections[tid] = self.get_connection(timeout=timeout)
         self.busybox = self.get_installed('busybox')
-        self._update_modules('connected')
         self.platform.update_from_target(self)
+        self._update_modules('connected')
         if self.platform.big_core and self.load_default_modules:
             self._install_module(get_module('bl'))
 

From c1d7cfafccaaa931765f3c3d93ea173e2a194fab Mon Sep 17 00:00:00 2001
From: Patrick Bellasi <patrick.bellasi@arm.com>
Date: Mon, 12 Oct 2015 15:25:12 +0100
Subject: [PATCH 5/6] utils/ssh: fix pxssh import for pexpect versions >= 4.0

The pexpect module exposes a pxssh class starting from version 4.0.
This patch makes uses of the proper pxssh import statement depending on the
version of pexpect available in the host.

Signed-off-by: Patrick Bellasi <patrick.bellasi@arm.com>
---
 devlib/utils/ssh.py | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/devlib/utils/ssh.py b/devlib/utils/ssh.py
index 66e9770..5d2896b 100644
--- a/devlib/utils/ssh.py
+++ b/devlib/utils/ssh.py
@@ -23,7 +23,12 @@ import threading
 import tempfile
 import shutil
 
-import pxssh
+import pexpect
+from distutils.version import StrictVersion as V
+if V(pexpect.__version__) < V('4.0.0'):
+    import pxssh
+else:
+    from pexpect import pxssh
 from pexpect import EOF, TIMEOUT, spawn
 
 from devlib.exception import HostError, TargetError, TimeoutError

From 68e5e8386473ccfd08b180f2f81e05cd44aa1c78 Mon Sep 17 00:00:00 2001
From: Patrick Bellasi <patrick.bellasi@arm.com>
Date: Mon, 12 Oct 2015 18:31:32 +0100
Subject: [PATCH 6/6] utils/serial_port: fix fdpexpect import for pexpect
 versions >= 4.0

The pexpect module exposes a fdpexpect class starting from version 4.0.
This patch makes uses of the proper pxssh import statement depending on the
version of pexpect available in the host.

Signed-off-by: Patrick Bellasi <patrick.bellasi@arm.com>
---
 devlib/utils/serial_port.py | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/devlib/utils/serial_port.py b/devlib/utils/serial_port.py
index 62df065..d1410a4 100644
--- a/devlib/utils/serial_port.py
+++ b/devlib/utils/serial_port.py
@@ -19,7 +19,13 @@ from contextlib import contextmanager
 from logging import Logger
 
 import serial
-import fdpexpect
+
+import pexpect
+from distutils.version import StrictVersion as V
+if V(pexpect.__version__) < V('4.0.0'):
+    import fdpexpect
+else:
+    from pexpect import fdpexpect
 # Adding pexpect exceptions into this module's namespace
 from pexpect import EOF, TIMEOUT  # NOQA pylint: disable=W0611