Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 776dc384 authored by Thierry Reding's avatar Thierry Reding
Browse files

drm/tegra: Move subdevice infrastructure to host1x



The Tegra DRM driver currently uses some infrastructure to defer the DRM
core initialization until all required devices have registered. The same
infrastructure can potentially be used by any other driver that requires
more than a single sub-device of the host1x module.

Make the infrastructure more generic and keep only the DRM specific code
in the DRM part of the driver. Eventually this will make it easy to move
the DRM driver part back to the DRM subsystem.

Signed-off-by: default avatarThierry Reding <treding@nvidia.com>
parent 35d747a8
Loading
Loading
Loading
Loading
+2 −0
Original line number Original line Diff line number Diff line
ccflags-y = -Idrivers/gpu/host1x
ccflags-y = -Idrivers/gpu/host1x


host1x-y = \
host1x-y = \
	bus.o \
	syncpt.o \
	syncpt.o \
	dev.o \
	dev.o \
	intr.o \
	intr.o \
@@ -17,4 +18,5 @@ host1x-$(CONFIG_DRM_TEGRA) += drm/drm.o drm/fb.o drm/dc.o
host1x-$(CONFIG_DRM_TEGRA) += drm/output.o drm/rgb.o drm/hdmi.o
host1x-$(CONFIG_DRM_TEGRA) += drm/output.o drm/rgb.o drm/hdmi.o
host1x-$(CONFIG_DRM_TEGRA) += drm/gem.o
host1x-$(CONFIG_DRM_TEGRA) += drm/gem.o
host1x-$(CONFIG_DRM_TEGRA) += drm/gr2d.o
host1x-$(CONFIG_DRM_TEGRA) += drm/gr2d.o
host1x-$(CONFIG_DRM_TEGRA) += drm/bus.o
obj-$(CONFIG_TEGRA_HOST1X) += host1x.o
obj-$(CONFIG_TEGRA_HOST1X) += host1x.o
+550 −0
Original line number Original line Diff line number Diff line
/*
 * Copyright (C) 2012 Avionic Design GmbH
 * Copyright (C) 2012-2013, NVIDIA Corporation
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms and conditions of the GNU General Public License,
 * version 2, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <linux/host1x.h>
#include <linux/of.h>
#include <linux/slab.h>

#include "dev.h"

static DEFINE_MUTEX(clients_lock);
static LIST_HEAD(clients);

static DEFINE_MUTEX(drivers_lock);
static LIST_HEAD(drivers);

static DEFINE_MUTEX(devices_lock);
static LIST_HEAD(devices);

struct host1x_subdev {
	struct host1x_client *client;
	struct device_node *np;
	struct list_head list;
};

/**
 * host1x_subdev_add() - add a new subdevice with an associated device node
 */
static int host1x_subdev_add(struct host1x_device *device,
			     struct device_node *np)
{
	struct host1x_subdev *subdev;

	subdev = kzalloc(sizeof(*subdev), GFP_KERNEL);
	if (!subdev)
		return -ENOMEM;

	INIT_LIST_HEAD(&subdev->list);
	subdev->np = of_node_get(np);

	mutex_lock(&device->subdevs_lock);
	list_add_tail(&subdev->list, &device->subdevs);
	mutex_unlock(&device->subdevs_lock);

	return 0;
}

/**
 * host1x_subdev_del() - remove subdevice
 */
static void host1x_subdev_del(struct host1x_subdev *subdev)
{
	list_del(&subdev->list);
	of_node_put(subdev->np);
	kfree(subdev);
}

/**
 * host1x_device_parse_dt() - scan device tree and add matching subdevices
 */
static int host1x_device_parse_dt(struct host1x_device *device)
{
	struct device_node *np;
	int err;

	for_each_child_of_node(device->dev.parent->of_node, np) {
		if (of_match_node(device->driver->subdevs, np) &&
		    of_device_is_available(np)) {
			err = host1x_subdev_add(device, np);
			if (err < 0)
				return err;
		}
	}

	return 0;
}

static void host1x_subdev_register(struct host1x_device *device,
				   struct host1x_subdev *subdev,
				   struct host1x_client *client)
{
	int err;

	/*
	 * Move the subdevice to the list of active (registered) subdevices
	 * and associate it with a client. At the same time, associate the
	 * client with its parent device.
	 */
	mutex_lock(&device->subdevs_lock);
	mutex_lock(&device->clients_lock);
	list_move_tail(&client->list, &device->clients);
	list_move_tail(&subdev->list, &device->active);
	client->parent = &device->dev;
	subdev->client = client;
	mutex_unlock(&device->clients_lock);
	mutex_unlock(&device->subdevs_lock);

	/*
	 * When all subdevices have been registered, the composite device is
	 * ready to be probed.
	 */
	if (list_empty(&device->subdevs)) {
		err = device->driver->probe(device);
		if (err < 0)
			dev_err(&device->dev, "probe failed: %d\n", err);
	}
}

static void __host1x_subdev_unregister(struct host1x_device *device,
				       struct host1x_subdev *subdev)
{
	struct host1x_client *client = subdev->client;
	int err;

	/*
	 * If all subdevices have been activated, we're about to remove the
	 * first active subdevice, so unload the driver first.
	 */
	if (list_empty(&device->subdevs)) {
		err = device->driver->remove(device);
		if (err < 0)
			dev_err(&device->dev, "remove failed: %d\n", err);
	}

	/*
	 * Move the subdevice back to the list of idle subdevices and remove
	 * it from list of clients.
	 */
	mutex_lock(&device->clients_lock);
	subdev->client = NULL;
	client->parent = NULL;
	list_move_tail(&subdev->list, &device->subdevs);
	/*
	 * XXX: Perhaps don't do this here, but rather explicitly remove it
	 * when the device is about to be deleted.
	 *
	 * This is somewhat complicated by the fact that this function is
	 * used to remove the subdevice when a client is unregistered but
	 * also when the composite device is about to be removed.
	 */
	list_del_init(&client->list);
	mutex_unlock(&device->clients_lock);
}

static void host1x_subdev_unregister(struct host1x_device *device,
				     struct host1x_subdev *subdev)
{
	mutex_lock(&device->subdevs_lock);
	__host1x_subdev_unregister(device, subdev);
	mutex_unlock(&device->subdevs_lock);
}

int host1x_device_init(struct host1x_device *device)
{
	struct host1x_client *client;
	int err;

	mutex_lock(&device->clients_lock);

	list_for_each_entry(client, &device->clients, list) {
		if (client->ops && client->ops->init) {
			err = client->ops->init(client);
			if (err < 0) {
				dev_err(&device->dev,
					"failed to initialize %s: %d\n",
					dev_name(client->dev), err);
				mutex_unlock(&device->clients_lock);
				return err;
			}
		}
	}

	mutex_unlock(&device->clients_lock);

	return 0;
}

int host1x_device_exit(struct host1x_device *device)
{
	struct host1x_client *client;
	int err;

	mutex_lock(&device->clients_lock);

	list_for_each_entry_reverse(client, &device->clients, list) {
		if (client->ops && client->ops->exit) {
			err = client->ops->exit(client);
			if (err < 0) {
				dev_err(&device->dev,
					"failed to cleanup %s: %d\n",
					dev_name(client->dev), err);
				mutex_unlock(&device->clients_lock);
				return err;
			}
		}
	}

	mutex_unlock(&device->clients_lock);

	return 0;
}

static int host1x_register_client(struct host1x *host1x,
				  struct host1x_client *client)
{
	struct host1x_device *device;
	struct host1x_subdev *subdev;

	mutex_lock(&host1x->devices_lock);

	list_for_each_entry(device, &host1x->devices, list) {
		list_for_each_entry(subdev, &device->subdevs, list) {
			if (subdev->np == client->dev->of_node) {
				host1x_subdev_register(device, subdev, client);
				mutex_unlock(&host1x->devices_lock);
				return 0;
			}
		}
	}

	mutex_unlock(&host1x->devices_lock);
	return -ENODEV;
}

static int host1x_unregister_client(struct host1x *host1x,
				    struct host1x_client *client)
{
	struct host1x_device *device, *dt;
	struct host1x_subdev *subdev;

	mutex_lock(&host1x->devices_lock);

	list_for_each_entry_safe(device, dt, &host1x->devices, list) {
		list_for_each_entry(subdev, &device->active, list) {
			if (subdev->client == client) {
				host1x_subdev_unregister(device, subdev);
				mutex_unlock(&host1x->devices_lock);
				return 0;
			}
		}
	}

	mutex_unlock(&host1x->devices_lock);
	return -ENODEV;
}

struct bus_type host1x_bus_type = {
	.name = "host1x",
};

int host1x_bus_init(void)
{
	return bus_register(&host1x_bus_type);
}

void host1x_bus_exit(void)
{
	bus_unregister(&host1x_bus_type);
}

static void host1x_device_release(struct device *dev)
{
	struct host1x_device *device = to_host1x_device(dev);

	kfree(device);
}

static int host1x_device_add(struct host1x *host1x,
			     struct host1x_driver *driver)
{
	struct host1x_client *client, *tmp;
	struct host1x_subdev *subdev;
	struct host1x_device *device;
	int err;

	device = kzalloc(sizeof(*device), GFP_KERNEL);
	if (!device)
		return -ENOMEM;

	mutex_init(&device->subdevs_lock);
	INIT_LIST_HEAD(&device->subdevs);
	INIT_LIST_HEAD(&device->active);
	mutex_init(&device->clients_lock);
	INIT_LIST_HEAD(&device->clients);
	INIT_LIST_HEAD(&device->list);
	device->driver = driver;

	device->dev.coherent_dma_mask = host1x->dev->coherent_dma_mask;
	device->dev.dma_mask = &device->dev.coherent_dma_mask;
	device->dev.release = host1x_device_release;
	dev_set_name(&device->dev, driver->name);
	device->dev.bus = &host1x_bus_type;
	device->dev.parent = host1x->dev;

	err = device_register(&device->dev);
	if (err < 0)
		return err;

	err = host1x_device_parse_dt(device);
	if (err < 0) {
		device_unregister(&device->dev);
		return err;
	}

	mutex_lock(&host1x->devices_lock);
	list_add_tail(&device->list, &host1x->devices);
	mutex_unlock(&host1x->devices_lock);

	mutex_lock(&clients_lock);

	list_for_each_entry_safe(client, tmp, &clients, list) {
		list_for_each_entry(subdev, &device->subdevs, list) {
			if (subdev->np == client->dev->of_node) {
				host1x_subdev_register(device, subdev, client);
				break;
			}
		}
	}

	mutex_unlock(&clients_lock);

	return 0;
}

/*
 * Removes a device by first unregistering any subdevices and then removing
 * itself from the list of devices.
 *
 * This function must be called with the host1x->devices_lock held.
 */
static void host1x_device_del(struct host1x *host1x,
			      struct host1x_device *device)
{
	struct host1x_subdev *subdev, *sd;
	struct host1x_client *client, *cl;

	mutex_lock(&device->subdevs_lock);

	/* unregister subdevices */
	list_for_each_entry_safe(subdev, sd, &device->active, list) {
		/*
		 * host1x_subdev_unregister() will remove the client from
		 * any lists, so we'll need to manually add it back to the
		 * list of idle clients.
		 *
		 * XXX: Alternatively, perhaps don't remove the client from
		 * any lists in host1x_subdev_unregister() and instead do
		 * that explicitly from host1x_unregister_client()?
		 */
		client = subdev->client;

		__host1x_subdev_unregister(device, subdev);

		/* add the client to the list of idle clients */
		mutex_lock(&clients_lock);
		list_add_tail(&client->list, &clients);
		mutex_unlock(&clients_lock);
	}

	/* remove subdevices */
	list_for_each_entry_safe(subdev, sd, &device->subdevs, list)
		host1x_subdev_del(subdev);

	mutex_unlock(&device->subdevs_lock);

	/* move clients to idle list */
	mutex_lock(&clients_lock);
	mutex_lock(&device->clients_lock);

	list_for_each_entry_safe(client, cl, &device->clients, list)
		list_move_tail(&client->list, &clients);

	mutex_unlock(&device->clients_lock);
	mutex_unlock(&clients_lock);

	/* finally remove the device */
	list_del_init(&device->list);
	device_unregister(&device->dev);
}

static void host1x_attach_driver(struct host1x *host1x,
				 struct host1x_driver *driver)
{
	struct host1x_device *device;
	int err;

	mutex_lock(&host1x->devices_lock);

	list_for_each_entry(device, &host1x->devices, list) {
		if (device->driver == driver) {
			mutex_unlock(&host1x->devices_lock);
			return;
		}
	}

	mutex_unlock(&host1x->devices_lock);

	err = host1x_device_add(host1x, driver);
	if (err < 0)
		dev_err(host1x->dev, "failed to allocate device: %d\n", err);
}

static void host1x_detach_driver(struct host1x *host1x,
				 struct host1x_driver *driver)
{
	struct host1x_device *device, *tmp;

	mutex_lock(&host1x->devices_lock);

	list_for_each_entry_safe(device, tmp, &host1x->devices, list)
		if (device->driver == driver)
			host1x_device_del(host1x, device);

	mutex_unlock(&host1x->devices_lock);
}

int host1x_register(struct host1x *host1x)
{
	struct host1x_driver *driver;

	mutex_lock(&devices_lock);
	list_add_tail(&host1x->list, &devices);
	mutex_unlock(&devices_lock);

	mutex_lock(&drivers_lock);

	list_for_each_entry(driver, &drivers, list)
		host1x_attach_driver(host1x, driver);

	mutex_unlock(&drivers_lock);

	return 0;
}

int host1x_unregister(struct host1x *host1x)
{
	struct host1x_driver *driver;

	mutex_lock(&drivers_lock);

	list_for_each_entry(driver, &drivers, list)
		host1x_detach_driver(host1x, driver);

	mutex_unlock(&drivers_lock);

	mutex_lock(&devices_lock);
	list_del_init(&host1x->list);
	mutex_unlock(&devices_lock);

	return 0;
}

int host1x_driver_register(struct host1x_driver *driver)
{
	struct host1x *host1x;

	INIT_LIST_HEAD(&driver->list);

	mutex_lock(&drivers_lock);
	list_add_tail(&driver->list, &drivers);
	mutex_unlock(&drivers_lock);

	mutex_lock(&devices_lock);

	list_for_each_entry(host1x, &devices, list)
		host1x_attach_driver(host1x, driver);

	mutex_unlock(&devices_lock);

	return 0;
}
EXPORT_SYMBOL(host1x_driver_register);

void host1x_driver_unregister(struct host1x_driver *driver)
{
	mutex_lock(&drivers_lock);
	list_del_init(&driver->list);
	mutex_unlock(&drivers_lock);
}
EXPORT_SYMBOL(host1x_driver_unregister);

int host1x_client_register(struct host1x_client *client)
{
	struct host1x *host1x;
	int err;

	mutex_lock(&devices_lock);

	list_for_each_entry(host1x, &devices, list) {
		err = host1x_register_client(host1x, client);
		if (!err) {
			mutex_unlock(&devices_lock);
			return 0;
		}
	}

	mutex_unlock(&devices_lock);

	mutex_lock(&clients_lock);
	list_add_tail(&client->list, &clients);
	mutex_unlock(&clients_lock);

	return 0;
}
EXPORT_SYMBOL(host1x_client_register);

int host1x_client_unregister(struct host1x_client *client)
{
	struct host1x_client *c;
	struct host1x *host1x;
	int err;

	mutex_lock(&devices_lock);

	list_for_each_entry(host1x, &devices, list) {
		err = host1x_unregister_client(host1x, client);
		if (!err) {
			mutex_unlock(&devices_lock);
			return 0;
		}
	}

	mutex_unlock(&devices_lock);
	mutex_lock(&clients_lock);

	list_for_each_entry(c, &clients, list) {
		if (c == client) {
			list_del_init(&c->list);
			break;
		}
	}

	mutex_unlock(&clients_lock);

	return 0;
}
EXPORT_SYMBOL(host1x_client_unregister);
+9 −15
Original line number Original line Diff line number Diff line
/*
/*
 * Copyright (c) 2013, NVIDIA Corporation.
 * Copyright (C) 2012 Avionic Design GmbH
 * Copyright (C) 2012-2013, NVIDIA Corporation
 *
 *
 * This program is free software; you can redistribute it and/or modify it
 * This program is free software; you can redistribute it and/or modify it
 * under the terms and conditions of the GNU General Public License,
 * under the terms and conditions of the GNU General Public License,
@@ -14,22 +15,15 @@
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 */


#ifndef HOST1X_CLIENT_H
#ifndef HOST1X_BUS_H
#define HOST1X_CLIENT_H
#define HOST1X_BUS_H


struct device;
struct host1x;
struct platform_device;


#ifdef CONFIG_DRM_TEGRA
int host1x_bus_init(void);
int tegra_drm_alloc(struct platform_device *pdev);
void host1x_bus_exit(void);
#else
static inline int tegra_drm_alloc(struct platform_device *pdev)
{
	return 0;
}
#endif


void host1x_set_drm_data(struct device *dev, void *data);
int host1x_register(struct host1x *host1x);
void *host1x_get_drm_data(struct device *dev);
int host1x_unregister(struct host1x *host1x);


#endif
#endif
+17 −43
Original line number Original line Diff line number Diff line
@@ -27,24 +27,12 @@
#define CREATE_TRACE_POINTS
#define CREATE_TRACE_POINTS
#include <trace/events/host1x.h>
#include <trace/events/host1x.h>


#include "bus.h"
#include "dev.h"
#include "dev.h"
#include "intr.h"
#include "intr.h"
#include "channel.h"
#include "channel.h"
#include "debug.h"
#include "debug.h"
#include "hw/host1x01.h"
#include "hw/host1x01.h"
#include "host1x_client.h"

void host1x_set_drm_data(struct device *dev, void *data)
{
	struct host1x *host1x = dev_get_drvdata(dev);
	host1x->drm_data = data;
}

void *host1x_get_drm_data(struct device *dev)
{
	struct host1x *host1x = dev_get_drvdata(dev);
	return host1x ? host1x->drm_data : NULL;
}


void host1x_sync_writel(struct host1x *host1x, u32 v, u32 r)
void host1x_sync_writel(struct host1x *host1x, u32 v, u32 r)
{
{
@@ -114,6 +102,9 @@ static int host1x_probe(struct platform_device *pdev)
	if (!host)
	if (!host)
		return -ENOMEM;
		return -ENOMEM;


	mutex_init(&host->devices_lock);
	INIT_LIST_HEAD(&host->devices);
	INIT_LIST_HEAD(&host->list);
	host->dev = &pdev->dev;
	host->dev = &pdev->dev;
	host->info = id->data;
	host->info = id->data;


@@ -163,10 +154,14 @@ static int host1x_probe(struct platform_device *pdev)


	host1x_debug_init(host);
	host1x_debug_init(host);


	tegra_drm_alloc(pdev);
	err = host1x_register(host);
	if (err < 0)
		goto fail_deinit_intr;


	return 0;
	return 0;


fail_deinit_intr:
	host1x_intr_deinit(host);
fail_deinit_syncpt:
fail_deinit_syncpt:
	host1x_syncpt_deinit(host);
	host1x_syncpt_deinit(host);
	return err;
	return err;
@@ -176,6 +171,7 @@ static int host1x_remove(struct platform_device *pdev)
{
{
	struct host1x *host = platform_get_drvdata(pdev);
	struct host1x *host = platform_get_drvdata(pdev);


	host1x_unregister(host);
	host1x_intr_deinit(host);
	host1x_intr_deinit(host);
	host1x_syncpt_deinit(host);
	host1x_syncpt_deinit(host);
	clk_disable_unprepare(host->clk);
	clk_disable_unprepare(host->clk);
@@ -196,46 +192,24 @@ static int __init tegra_host1x_init(void)
{
{
	int err;
	int err;


	err = platform_driver_register(&tegra_host1x_driver);
	err = host1x_bus_init();
	if (err < 0)
	if (err < 0)
		return err;
		return err;


#ifdef CONFIG_DRM_TEGRA
	err = platform_driver_register(&tegra_host1x_driver);
	err = platform_driver_register(&tegra_dc_driver);
	if (err < 0) {
	if (err < 0)
		host1x_bus_exit();
		goto unregister_host1x;
		return err;

	}
	err = platform_driver_register(&tegra_hdmi_driver);
	if (err < 0)
		goto unregister_dc;

	err = platform_driver_register(&tegra_gr2d_driver);
	if (err < 0)
		goto unregister_hdmi;
#endif


	return 0;
	return 0;

#ifdef CONFIG_DRM_TEGRA
unregister_hdmi:
	platform_driver_unregister(&tegra_hdmi_driver);
unregister_dc:
	platform_driver_unregister(&tegra_dc_driver);
unregister_host1x:
	platform_driver_unregister(&tegra_host1x_driver);
	return err;
#endif
}
}
module_init(tegra_host1x_init);
module_init(tegra_host1x_init);


static void __exit tegra_host1x_exit(void)
static void __exit tegra_host1x_exit(void)
{
{
#ifdef CONFIG_DRM_TEGRA
	platform_driver_unregister(&tegra_gr2d_driver);
	platform_driver_unregister(&tegra_hdmi_driver);
	platform_driver_unregister(&tegra_dc_driver);
#endif
	platform_driver_unregister(&tegra_host1x_driver);
	platform_driver_unregister(&tegra_host1x_driver);
	host1x_bus_exit();
}
}
module_exit(tegra_host1x_exit);
module_exit(tegra_host1x_exit);


+4 −5
Original line number Original line Diff line number Diff line
@@ -125,7 +125,10 @@ struct host1x {


	struct dentry *debugfs;
	struct dentry *debugfs;


	void *drm_data;
	struct mutex devices_lock;
	struct list_head devices;

	struct list_head list;
};
};


void host1x_sync_writel(struct host1x *host1x, u32 r, u32 v);
void host1x_sync_writel(struct host1x *host1x, u32 r, u32 v);
@@ -301,8 +304,4 @@ static inline void host1x_hw_show_mlocks(struct host1x *host, struct output *o)
	host->debug_op->show_mlocks(host, o);
	host->debug_op->show_mlocks(host, o);
}
}


extern struct platform_driver tegra_dc_driver;
extern struct platform_driver tegra_hdmi_driver;
extern struct platform_driver tegra_gr2d_driver;

#endif
#endif
Loading