[Nouveau] [PATCH 3/9] pci: implement generic code for PCIe speed change

Karol Herbst
2015-10-12T20:27:44Z

drm/nouveau/include/nvkm/subdev/pci.h |  12 +++
 drm/nouveau/nvkm/subdev/pci/Kbuild    |   1 +
 drm/nouveau/nvkm/subdev/pci/base.c    |   5 ++
 drm/nouveau/nvkm/subdev/pci/pcie.c    | 159 ++++++++++++++++++++++++++++++++++
 drm/nouveau/nvkm/subdev/pci/priv.h    |  12 +++
 5 files changed, 189 insertions(+)
 create mode 100644 drm/nouveau/nvkm/subdev/pci/pcie.c

diff --git a/drm/nouveau/include/nvkm/subdev/pci.h
b/drm/nouveau/include/nvkm/subdev/pci.h
index 17fe7b7..e195736 100644
--- a/drm/nouveau/include/nvkm/subdev/pci.h
+++ b/drm/nouveau/include/nvkm/subdev/pci.h
@@ -2,6 +2,12 @@
 #define __NVKM_PCI_H__
 #include <core/subdev.h>

+enum nvkm_pcie_speed {
+	NVKM_PCIE_SPEED_2_5,
+	NVKM_PCIE_SPEED_5_0,
+	NVKM_PCIE_SPEED_8_0,
+};
+
 struct nvkm_pci {
 	const struct nvkm_pci_func *func;
 	struct nvkm_subdev subdev;
@@ -18,6 +24,11 @@ struct nvkm_pci {
 		bool acquired;
 	} agp;

+	struct {
+		enum nvkm_pcie_speed last_speed;
+		u8 last_width;
+	} pcie;
+
 	bool msi;
 };

@@ -26,6 +37,7 @@ void nvkm_pci_wr08(struct nvkm_pci *, u16 addr, u8 data);
 void nvkm_pci_wr32(struct nvkm_pci *, u16 addr, u32 data);
 u32 nvkm_pci_mask(struct nvkm_pci *, u16 addr, u32 mask, u32 value);
 void nvkm_pci_rom_shadow(struct nvkm_pci *, bool shadow);
+int nvkm_pci_set_pcie_link(struct nvkm_pci *, enum nvkm_pcie_speed, u8 width);

 int nv04_pci_new(struct nvkm_device *, int, struct nvkm_pci **);
 int nv40_pci_new(struct nvkm_device *, int, struct nvkm_pci **);
diff --git a/drm/nouveau/nvkm/subdev/pci/Kbuild
b/drm/nouveau/nvkm/subdev/pci/Kbuild
index 724afd4..3c2519f 100644
--- a/drm/nouveau/nvkm/subdev/pci/Kbuild
+++ b/drm/nouveau/nvkm/subdev/pci/Kbuild
@@ -1,5 +1,6 @@
 nvkm-y += nvkm/subdev/pci/agp.o
 nvkm-y += nvkm/subdev/pci/base.o
+nvkm-y += nvkm/subdev/pci/pcie.o
 nvkm-y += nvkm/subdev/pci/nv04.o
 nvkm-y += nvkm/subdev/pci/nv40.o
 nvkm-y += nvkm/subdev/pci/nv46.o
diff --git a/drm/nouveau/nvkm/subdev/pci/base.c
b/drm/nouveau/nvkm/subdev/pci/base.c
index d671dcf..3895ab2 100644
--- a/drm/nouveau/nvkm/subdev/pci/base.c
+++ b/drm/nouveau/nvkm/subdev/pci/base.c
@@ -117,6 +117,9 @@ nvkm_pci_init(struct nvkm_subdev *subdev)
 		ret = nvkm_agp_init(pci);
 		if (ret)
 			return ret;
+	} else {
+		if (pci_is_pcie(pci->pdev))
+			nvkm_pci_pcie_init(pci);
 	}

 	if (pci->func->init)
@@ -160,6 +163,8 @@ nvkm_pci_new_(const struct nvkm_pci_func *func, struct
nvkm_device *device,
 	pci->func = func;
 	pci->pdev = device->func->pci(device)->pdev;
 	pci->irq = -1;
+	pci->pcie.last_speed = -1;
+	pci->pcie.last_width = -1;

 	if (device->type == NVKM_DEVICE_AGP)
 		nvkm_agp_ctor(pci);
diff --git a/drm/nouveau/nvkm/subdev/pci/pcie.c
b/drm/nouveau/nvkm/subdev/pci/pcie.c
new file mode 100644
index 0000000..05e9c55
--- /dev/null
+++ b/drm/nouveau/nvkm/subdev/pci/pcie.c
@@ -0,0 +1,159 @@
+/*
+ * Copyright 2015 Karol Herbst
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the
"Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
+ * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ *
+ * Authors: Karol Herbst <git at karolherbst.de>
+ */
+#include "priv.h"
+
+static char *pcie_speed_strings[] = {
+	"2.5GT/s",
+	"5.0GT/s",
+	"8.0GT/s",
+};
+
+static enum nvkm_pcie_speed
+pci_bus_speed_to_nvkm_pcie_speed(enum pci_bus_speed speed)
+{
+	switch (speed) {
+	case PCIE_SPEED_2_5GT:
+		return NVKM_PCIE_SPEED_2_5;
+	case PCIE_SPEED_5_0GT:
+		return NVKM_PCIE_SPEED_5_0;
+	case PCIE_SPEED_8_0GT:
+		return NVKM_PCIE_SPEED_8_0;
+	default:
+		if (speed == 0x17) // 0x16 is 8_0, assume 0x17 will be 16_0 for now
+			return NVKM_PCIE_SPEED_8_0;
+		return -1;
+	}
+}
+
+static s8
+nvkm_pci_get_pcie_version(struct nvkm_pci *pci)
+{
+	if (!pci_is_pcie(pci->pdev))
+		return -ENODEV;
+
+	if (!pci->func->get_pcie_version)
+		return -ENOSYS;
+
+	return pci->func->get_pcie_version(pci);
+}
+
+static s8
+nvkm_pci_raise_pcie_version(struct nvkm_pci *pci)
+{
+	int ret, supported;
+
+	if (!pci_is_pcie(pci->pdev))
+		return -ENODEV;
+
+	if (!pci->func->set_pcie_version ||
!pci->func->get_pcie_version_supported)
+		return -ENOSYS;
+
+	ret = nvkm_pci_get_pcie_version(pci);
+	supported = pci->func->get_pcie_version_supported(pci);
+	if (ret == 1 && ret < supported) {
+		nvkm_debug(&pci->subdev, "raising version\n");
+		pci->func->set_pcie_version(pci, supported);
+		ret = nvkm_pci_get_pcie_version(pci);
+	}
+	return ret >= supported ? ret : -EINVAL;
+}
+
+int
+nvkm_pci_pcie_init(struct nvkm_pci *pci)
+{
+	struct nvkm_subdev *subdev = &pci->subdev;
+	int ret;
+
+	if (!pci_is_pcie(pci->pdev))
+		return -ENODEV;
+
+	// raise pcie version first
+	ret = nvkm_pci_raise_pcie_version(pci);
+	if (ret <= 0)
+		nvkm_error(subdev, "couldn't raise version: %i\n", ret);
+
+	if (pci->func->pcie_speed_init)
+		pci->func->pcie_speed_init(pci);
+
+	nvkm_info(subdev, "pcie version: %i\n",
nvkm_pci_get_pcie_version(pci));
+	if (pci->func->pcie_max_speed)
+		nvkm_info(subdev, "pcie max speed: %s\n",
pcie_speed_strings[pci->func->pcie_max_speed(pci)]);
+
+	if (pci->pcie.last_speed != -1)
+		nvkm_pci_set_pcie_link(pci, pci->pcie.last_speed,
pci->pcie.last_width);
+
+	return 0;
+}
+
+int
+nvkm_pci_set_pcie_link(struct nvkm_pci * pci, enum nvkm_pcie_speed req_speed,
u8 req_width)
+{
+	struct nvkm_subdev *subdev = &pci->subdev;
+	enum nvkm_pcie_speed sys_cur_speed, sys_max_speed;
+	struct pci_bus *pbus = pci->pdev->bus;
+	int ret;
+
+	if (!pci_is_pcie(pci->pdev))
+		return -ENODEV;
+
+	if (!pci->func->pcie_speed_set || !pci->func->get_pcie_version ||
!pci->func->pcie_max_speed || !pci->func->pcie_current_speed)
+		return -ENOSYS;
+
+	nvkm_debug(subdev, "pcie speed %s requested\n",
pcie_speed_strings[req_speed]);
+
+	if (pci->func->get_pcie_version(pci) < 2) {
+		nvkm_error(subdev, "can't change link speed, "
+				"because current version too low\n");
+		return -ENODEV;
+	}
+
+	sys_cur_speed = pci->func->pcie_current_speed(pci);
+	sys_max_speed = min(pci_bus_speed_to_nvkm_pcie_speed(pbus->max_bus_speed),
pci->func->pcie_max_speed(pci));
+
+	nvkm_debug(subdev, "bus current speed: %s max speed: %s\n",
+			pcie_speed_strings[sys_cur_speed], pcie_speed_strings[sys_max_speed]);
+
+	if (req_speed > sys_max_speed) {
+		req_speed = sys_max_speed;
+		nvkm_warn(subdev, "bus or card not fast enough, dropping "
+				"requested speed to %s", pcie_speed_strings[req_speed]);
+	}
+
+	pci->pcie.last_speed = req_speed;
+	pci->pcie.last_width = req_width;
+
+	if (req_speed == sys_cur_speed) {
+		nvkm_debug(subdev, "requested speed matches current speed
already\n");
+		return req_speed;
+	}
+
+	nvkm_debug(subdev, "set link to speed: %s width: x%i\n",
+			pcie_speed_strings[req_speed], req_width);
+	ret = pci->func->pcie_speed_set(pci, req_speed, req_width);
+
+	if (ret < 0)
+		nvkm_error(subdev, "setting link failed with ret: %i\n", ret);
+
+	return ret;
+}
diff --git a/drm/nouveau/nvkm/subdev/pci/priv.h
b/drm/nouveau/nvkm/subdev/pci/priv.h
index cf46d38..11c266f 100644
--- a/drm/nouveau/nvkm/subdev/pci/priv.h
+++ b/drm/nouveau/nvkm/subdev/pci/priv.h
@@ -12,6 +12,16 @@ struct nvkm_pci_func {
 	void (*wr08)(struct nvkm_pci *, u16 addr, u8 data);
 	void (*wr32)(struct nvkm_pci *, u16 addr, u32 data);
 	void (*msi_rearm)(struct nvkm_pci *);
+
+	int (*pcie_speed_init)(struct nvkm_pci *);
+	int (*pcie_speed_set)(struct nvkm_pci *, enum nvkm_pcie_speed, u8);
+
+	enum nvkm_pcie_speed (*pcie_max_speed)(struct nvkm_pci *);
+	enum nvkm_pcie_speed (*pcie_current_speed)(struct nvkm_pci *);
+
+	void (*set_pcie_version)(struct nvkm_pci *, u8);
+	u8 (*get_pcie_version)(struct nvkm_pci *);
+	u8 (*get_pcie_version_supported)(struct nvkm_pci *);
 };

 u32 nv40_pci_rd32(struct nvkm_pci *, u16);
@@ -22,4 +32,6 @@ void nv40_pci_msi_rearm(struct nvkm_pci *);
 void nv46_pci_msi_rearm(struct nvkm_pci *);

 void g84_pci_init(struct nvkm_pci *pci);
+
+int nvkm_pci_pcie_init(struct nvkm_pci *pci);
 #endif
--
2.6.1