xref: /OK3568_Linux_fs/kernel/drivers/gpu/host1x/bus.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Copyright (C) 2012 Avionic Design GmbH
4*4882a593Smuzhiyun  * Copyright (C) 2012-2013, NVIDIA Corporation
5*4882a593Smuzhiyun  */
6*4882a593Smuzhiyun 
7*4882a593Smuzhiyun #include <linux/debugfs.h>
8*4882a593Smuzhiyun #include <linux/host1x.h>
9*4882a593Smuzhiyun #include <linux/of.h>
10*4882a593Smuzhiyun #include <linux/seq_file.h>
11*4882a593Smuzhiyun #include <linux/slab.h>
12*4882a593Smuzhiyun #include <linux/of_device.h>
13*4882a593Smuzhiyun 
14*4882a593Smuzhiyun #include "bus.h"
15*4882a593Smuzhiyun #include "dev.h"
16*4882a593Smuzhiyun 
17*4882a593Smuzhiyun static DEFINE_MUTEX(clients_lock);
18*4882a593Smuzhiyun static LIST_HEAD(clients);
19*4882a593Smuzhiyun 
20*4882a593Smuzhiyun static DEFINE_MUTEX(drivers_lock);
21*4882a593Smuzhiyun static LIST_HEAD(drivers);
22*4882a593Smuzhiyun 
23*4882a593Smuzhiyun static DEFINE_MUTEX(devices_lock);
24*4882a593Smuzhiyun static LIST_HEAD(devices);
25*4882a593Smuzhiyun 
26*4882a593Smuzhiyun struct host1x_subdev {
27*4882a593Smuzhiyun 	struct host1x_client *client;
28*4882a593Smuzhiyun 	struct device_node *np;
29*4882a593Smuzhiyun 	struct list_head list;
30*4882a593Smuzhiyun };
31*4882a593Smuzhiyun 
32*4882a593Smuzhiyun /**
33*4882a593Smuzhiyun  * host1x_subdev_add() - add a new subdevice with an associated device node
34*4882a593Smuzhiyun  * @device: host1x device to add the subdevice to
35*4882a593Smuzhiyun  * @np: device node
36*4882a593Smuzhiyun  */
host1x_subdev_add(struct host1x_device * device,struct host1x_driver * driver,struct device_node * np)37*4882a593Smuzhiyun static int host1x_subdev_add(struct host1x_device *device,
38*4882a593Smuzhiyun 			     struct host1x_driver *driver,
39*4882a593Smuzhiyun 			     struct device_node *np)
40*4882a593Smuzhiyun {
41*4882a593Smuzhiyun 	struct host1x_subdev *subdev;
42*4882a593Smuzhiyun 	struct device_node *child;
43*4882a593Smuzhiyun 	int err;
44*4882a593Smuzhiyun 
45*4882a593Smuzhiyun 	subdev = kzalloc(sizeof(*subdev), GFP_KERNEL);
46*4882a593Smuzhiyun 	if (!subdev)
47*4882a593Smuzhiyun 		return -ENOMEM;
48*4882a593Smuzhiyun 
49*4882a593Smuzhiyun 	INIT_LIST_HEAD(&subdev->list);
50*4882a593Smuzhiyun 	subdev->np = of_node_get(np);
51*4882a593Smuzhiyun 
52*4882a593Smuzhiyun 	mutex_lock(&device->subdevs_lock);
53*4882a593Smuzhiyun 	list_add_tail(&subdev->list, &device->subdevs);
54*4882a593Smuzhiyun 	mutex_unlock(&device->subdevs_lock);
55*4882a593Smuzhiyun 
56*4882a593Smuzhiyun 	/* recursively add children */
57*4882a593Smuzhiyun 	for_each_child_of_node(np, child) {
58*4882a593Smuzhiyun 		if (of_match_node(driver->subdevs, child) &&
59*4882a593Smuzhiyun 		    of_device_is_available(child)) {
60*4882a593Smuzhiyun 			err = host1x_subdev_add(device, driver, child);
61*4882a593Smuzhiyun 			if (err < 0) {
62*4882a593Smuzhiyun 				/* XXX cleanup? */
63*4882a593Smuzhiyun 				of_node_put(child);
64*4882a593Smuzhiyun 				return err;
65*4882a593Smuzhiyun 			}
66*4882a593Smuzhiyun 		}
67*4882a593Smuzhiyun 	}
68*4882a593Smuzhiyun 
69*4882a593Smuzhiyun 	return 0;
70*4882a593Smuzhiyun }
71*4882a593Smuzhiyun 
72*4882a593Smuzhiyun /**
73*4882a593Smuzhiyun  * host1x_subdev_del() - remove subdevice
74*4882a593Smuzhiyun  * @subdev: subdevice to remove
75*4882a593Smuzhiyun  */
host1x_subdev_del(struct host1x_subdev * subdev)76*4882a593Smuzhiyun static void host1x_subdev_del(struct host1x_subdev *subdev)
77*4882a593Smuzhiyun {
78*4882a593Smuzhiyun 	list_del(&subdev->list);
79*4882a593Smuzhiyun 	of_node_put(subdev->np);
80*4882a593Smuzhiyun 	kfree(subdev);
81*4882a593Smuzhiyun }
82*4882a593Smuzhiyun 
83*4882a593Smuzhiyun /**
84*4882a593Smuzhiyun  * host1x_device_parse_dt() - scan device tree and add matching subdevices
85*4882a593Smuzhiyun  * @device: host1x logical device
86*4882a593Smuzhiyun  * @driver: host1x driver
87*4882a593Smuzhiyun  */
host1x_device_parse_dt(struct host1x_device * device,struct host1x_driver * driver)88*4882a593Smuzhiyun static int host1x_device_parse_dt(struct host1x_device *device,
89*4882a593Smuzhiyun 				  struct host1x_driver *driver)
90*4882a593Smuzhiyun {
91*4882a593Smuzhiyun 	struct device_node *np;
92*4882a593Smuzhiyun 	int err;
93*4882a593Smuzhiyun 
94*4882a593Smuzhiyun 	for_each_child_of_node(device->dev.parent->of_node, np) {
95*4882a593Smuzhiyun 		if (of_match_node(driver->subdevs, np) &&
96*4882a593Smuzhiyun 		    of_device_is_available(np)) {
97*4882a593Smuzhiyun 			err = host1x_subdev_add(device, driver, np);
98*4882a593Smuzhiyun 			if (err < 0) {
99*4882a593Smuzhiyun 				of_node_put(np);
100*4882a593Smuzhiyun 				return err;
101*4882a593Smuzhiyun 			}
102*4882a593Smuzhiyun 		}
103*4882a593Smuzhiyun 	}
104*4882a593Smuzhiyun 
105*4882a593Smuzhiyun 	return 0;
106*4882a593Smuzhiyun }
107*4882a593Smuzhiyun 
host1x_subdev_register(struct host1x_device * device,struct host1x_subdev * subdev,struct host1x_client * client)108*4882a593Smuzhiyun static void host1x_subdev_register(struct host1x_device *device,
109*4882a593Smuzhiyun 				   struct host1x_subdev *subdev,
110*4882a593Smuzhiyun 				   struct host1x_client *client)
111*4882a593Smuzhiyun {
112*4882a593Smuzhiyun 	int err;
113*4882a593Smuzhiyun 
114*4882a593Smuzhiyun 	/*
115*4882a593Smuzhiyun 	 * Move the subdevice to the list of active (registered) subdevices
116*4882a593Smuzhiyun 	 * and associate it with a client. At the same time, associate the
117*4882a593Smuzhiyun 	 * client with its parent device.
118*4882a593Smuzhiyun 	 */
119*4882a593Smuzhiyun 	mutex_lock(&device->subdevs_lock);
120*4882a593Smuzhiyun 	mutex_lock(&device->clients_lock);
121*4882a593Smuzhiyun 	list_move_tail(&client->list, &device->clients);
122*4882a593Smuzhiyun 	list_move_tail(&subdev->list, &device->active);
123*4882a593Smuzhiyun 	client->host = &device->dev;
124*4882a593Smuzhiyun 	subdev->client = client;
125*4882a593Smuzhiyun 	mutex_unlock(&device->clients_lock);
126*4882a593Smuzhiyun 	mutex_unlock(&device->subdevs_lock);
127*4882a593Smuzhiyun 
128*4882a593Smuzhiyun 	if (list_empty(&device->subdevs)) {
129*4882a593Smuzhiyun 		err = device_add(&device->dev);
130*4882a593Smuzhiyun 		if (err < 0)
131*4882a593Smuzhiyun 			dev_err(&device->dev, "failed to add: %d\n", err);
132*4882a593Smuzhiyun 		else
133*4882a593Smuzhiyun 			device->registered = true;
134*4882a593Smuzhiyun 	}
135*4882a593Smuzhiyun }
136*4882a593Smuzhiyun 
__host1x_subdev_unregister(struct host1x_device * device,struct host1x_subdev * subdev)137*4882a593Smuzhiyun static void __host1x_subdev_unregister(struct host1x_device *device,
138*4882a593Smuzhiyun 				       struct host1x_subdev *subdev)
139*4882a593Smuzhiyun {
140*4882a593Smuzhiyun 	struct host1x_client *client = subdev->client;
141*4882a593Smuzhiyun 
142*4882a593Smuzhiyun 	/*
143*4882a593Smuzhiyun 	 * If all subdevices have been activated, we're about to remove the
144*4882a593Smuzhiyun 	 * first active subdevice, so unload the driver first.
145*4882a593Smuzhiyun 	 */
146*4882a593Smuzhiyun 	if (list_empty(&device->subdevs)) {
147*4882a593Smuzhiyun 		if (device->registered) {
148*4882a593Smuzhiyun 			device->registered = false;
149*4882a593Smuzhiyun 			device_del(&device->dev);
150*4882a593Smuzhiyun 		}
151*4882a593Smuzhiyun 	}
152*4882a593Smuzhiyun 
153*4882a593Smuzhiyun 	/*
154*4882a593Smuzhiyun 	 * Move the subdevice back to the list of idle subdevices and remove
155*4882a593Smuzhiyun 	 * it from list of clients.
156*4882a593Smuzhiyun 	 */
157*4882a593Smuzhiyun 	mutex_lock(&device->clients_lock);
158*4882a593Smuzhiyun 	subdev->client = NULL;
159*4882a593Smuzhiyun 	client->host = NULL;
160*4882a593Smuzhiyun 	list_move_tail(&subdev->list, &device->subdevs);
161*4882a593Smuzhiyun 	/*
162*4882a593Smuzhiyun 	 * XXX: Perhaps don't do this here, but rather explicitly remove it
163*4882a593Smuzhiyun 	 * when the device is about to be deleted.
164*4882a593Smuzhiyun 	 *
165*4882a593Smuzhiyun 	 * This is somewhat complicated by the fact that this function is
166*4882a593Smuzhiyun 	 * used to remove the subdevice when a client is unregistered but
167*4882a593Smuzhiyun 	 * also when the composite device is about to be removed.
168*4882a593Smuzhiyun 	 */
169*4882a593Smuzhiyun 	list_del_init(&client->list);
170*4882a593Smuzhiyun 	mutex_unlock(&device->clients_lock);
171*4882a593Smuzhiyun }
172*4882a593Smuzhiyun 
host1x_subdev_unregister(struct host1x_device * device,struct host1x_subdev * subdev)173*4882a593Smuzhiyun static void host1x_subdev_unregister(struct host1x_device *device,
174*4882a593Smuzhiyun 				     struct host1x_subdev *subdev)
175*4882a593Smuzhiyun {
176*4882a593Smuzhiyun 	mutex_lock(&device->subdevs_lock);
177*4882a593Smuzhiyun 	__host1x_subdev_unregister(device, subdev);
178*4882a593Smuzhiyun 	mutex_unlock(&device->subdevs_lock);
179*4882a593Smuzhiyun }
180*4882a593Smuzhiyun 
181*4882a593Smuzhiyun /**
182*4882a593Smuzhiyun  * host1x_device_init() - initialize a host1x logical device
183*4882a593Smuzhiyun  * @device: host1x logical device
184*4882a593Smuzhiyun  *
185*4882a593Smuzhiyun  * The driver for the host1x logical device can call this during execution of
186*4882a593Smuzhiyun  * its &host1x_driver.probe implementation to initialize each of its clients.
187*4882a593Smuzhiyun  * The client drivers access the subsystem specific driver data using the
188*4882a593Smuzhiyun  * &host1x_client.parent field and driver data associated with it (usually by
189*4882a593Smuzhiyun  * calling dev_get_drvdata()).
190*4882a593Smuzhiyun  */
host1x_device_init(struct host1x_device * device)191*4882a593Smuzhiyun int host1x_device_init(struct host1x_device *device)
192*4882a593Smuzhiyun {
193*4882a593Smuzhiyun 	struct host1x_client *client;
194*4882a593Smuzhiyun 	int err;
195*4882a593Smuzhiyun 
196*4882a593Smuzhiyun 	mutex_lock(&device->clients_lock);
197*4882a593Smuzhiyun 
198*4882a593Smuzhiyun 	list_for_each_entry(client, &device->clients, list) {
199*4882a593Smuzhiyun 		if (client->ops && client->ops->init) {
200*4882a593Smuzhiyun 			err = client->ops->init(client);
201*4882a593Smuzhiyun 			if (err < 0) {
202*4882a593Smuzhiyun 				dev_err(&device->dev,
203*4882a593Smuzhiyun 					"failed to initialize %s: %d\n",
204*4882a593Smuzhiyun 					dev_name(client->dev), err);
205*4882a593Smuzhiyun 				goto teardown;
206*4882a593Smuzhiyun 			}
207*4882a593Smuzhiyun 		}
208*4882a593Smuzhiyun 	}
209*4882a593Smuzhiyun 
210*4882a593Smuzhiyun 	mutex_unlock(&device->clients_lock);
211*4882a593Smuzhiyun 
212*4882a593Smuzhiyun 	return 0;
213*4882a593Smuzhiyun 
214*4882a593Smuzhiyun teardown:
215*4882a593Smuzhiyun 	list_for_each_entry_continue_reverse(client, &device->clients, list)
216*4882a593Smuzhiyun 		if (client->ops->exit)
217*4882a593Smuzhiyun 			client->ops->exit(client);
218*4882a593Smuzhiyun 
219*4882a593Smuzhiyun 	mutex_unlock(&device->clients_lock);
220*4882a593Smuzhiyun 	return err;
221*4882a593Smuzhiyun }
222*4882a593Smuzhiyun EXPORT_SYMBOL(host1x_device_init);
223*4882a593Smuzhiyun 
224*4882a593Smuzhiyun /**
225*4882a593Smuzhiyun  * host1x_device_exit() - uninitialize host1x logical device
226*4882a593Smuzhiyun  * @device: host1x logical device
227*4882a593Smuzhiyun  *
228*4882a593Smuzhiyun  * When the driver for a host1x logical device is unloaded, it can call this
229*4882a593Smuzhiyun  * function to tear down each of its clients. Typically this is done after a
230*4882a593Smuzhiyun  * subsystem-specific data structure is removed and the functionality can no
231*4882a593Smuzhiyun  * longer be used.
232*4882a593Smuzhiyun  */
host1x_device_exit(struct host1x_device * device)233*4882a593Smuzhiyun int host1x_device_exit(struct host1x_device *device)
234*4882a593Smuzhiyun {
235*4882a593Smuzhiyun 	struct host1x_client *client;
236*4882a593Smuzhiyun 	int err;
237*4882a593Smuzhiyun 
238*4882a593Smuzhiyun 	mutex_lock(&device->clients_lock);
239*4882a593Smuzhiyun 
240*4882a593Smuzhiyun 	list_for_each_entry_reverse(client, &device->clients, list) {
241*4882a593Smuzhiyun 		if (client->ops && client->ops->exit) {
242*4882a593Smuzhiyun 			err = client->ops->exit(client);
243*4882a593Smuzhiyun 			if (err < 0) {
244*4882a593Smuzhiyun 				dev_err(&device->dev,
245*4882a593Smuzhiyun 					"failed to cleanup %s: %d\n",
246*4882a593Smuzhiyun 					dev_name(client->dev), err);
247*4882a593Smuzhiyun 				mutex_unlock(&device->clients_lock);
248*4882a593Smuzhiyun 				return err;
249*4882a593Smuzhiyun 			}
250*4882a593Smuzhiyun 		}
251*4882a593Smuzhiyun 	}
252*4882a593Smuzhiyun 
253*4882a593Smuzhiyun 	mutex_unlock(&device->clients_lock);
254*4882a593Smuzhiyun 
255*4882a593Smuzhiyun 	return 0;
256*4882a593Smuzhiyun }
257*4882a593Smuzhiyun EXPORT_SYMBOL(host1x_device_exit);
258*4882a593Smuzhiyun 
host1x_add_client(struct host1x * host1x,struct host1x_client * client)259*4882a593Smuzhiyun static int host1x_add_client(struct host1x *host1x,
260*4882a593Smuzhiyun 			     struct host1x_client *client)
261*4882a593Smuzhiyun {
262*4882a593Smuzhiyun 	struct host1x_device *device;
263*4882a593Smuzhiyun 	struct host1x_subdev *subdev;
264*4882a593Smuzhiyun 
265*4882a593Smuzhiyun 	mutex_lock(&host1x->devices_lock);
266*4882a593Smuzhiyun 
267*4882a593Smuzhiyun 	list_for_each_entry(device, &host1x->devices, list) {
268*4882a593Smuzhiyun 		list_for_each_entry(subdev, &device->subdevs, list) {
269*4882a593Smuzhiyun 			if (subdev->np == client->dev->of_node) {
270*4882a593Smuzhiyun 				host1x_subdev_register(device, subdev, client);
271*4882a593Smuzhiyun 				mutex_unlock(&host1x->devices_lock);
272*4882a593Smuzhiyun 				return 0;
273*4882a593Smuzhiyun 			}
274*4882a593Smuzhiyun 		}
275*4882a593Smuzhiyun 	}
276*4882a593Smuzhiyun 
277*4882a593Smuzhiyun 	mutex_unlock(&host1x->devices_lock);
278*4882a593Smuzhiyun 	return -ENODEV;
279*4882a593Smuzhiyun }
280*4882a593Smuzhiyun 
host1x_del_client(struct host1x * host1x,struct host1x_client * client)281*4882a593Smuzhiyun static int host1x_del_client(struct host1x *host1x,
282*4882a593Smuzhiyun 			     struct host1x_client *client)
283*4882a593Smuzhiyun {
284*4882a593Smuzhiyun 	struct host1x_device *device, *dt;
285*4882a593Smuzhiyun 	struct host1x_subdev *subdev;
286*4882a593Smuzhiyun 
287*4882a593Smuzhiyun 	mutex_lock(&host1x->devices_lock);
288*4882a593Smuzhiyun 
289*4882a593Smuzhiyun 	list_for_each_entry_safe(device, dt, &host1x->devices, list) {
290*4882a593Smuzhiyun 		list_for_each_entry(subdev, &device->active, list) {
291*4882a593Smuzhiyun 			if (subdev->client == client) {
292*4882a593Smuzhiyun 				host1x_subdev_unregister(device, subdev);
293*4882a593Smuzhiyun 				mutex_unlock(&host1x->devices_lock);
294*4882a593Smuzhiyun 				return 0;
295*4882a593Smuzhiyun 			}
296*4882a593Smuzhiyun 		}
297*4882a593Smuzhiyun 	}
298*4882a593Smuzhiyun 
299*4882a593Smuzhiyun 	mutex_unlock(&host1x->devices_lock);
300*4882a593Smuzhiyun 	return -ENODEV;
301*4882a593Smuzhiyun }
302*4882a593Smuzhiyun 
host1x_device_match(struct device * dev,struct device_driver * drv)303*4882a593Smuzhiyun static int host1x_device_match(struct device *dev, struct device_driver *drv)
304*4882a593Smuzhiyun {
305*4882a593Smuzhiyun 	return strcmp(dev_name(dev), drv->name) == 0;
306*4882a593Smuzhiyun }
307*4882a593Smuzhiyun 
host1x_device_uevent(struct device * dev,struct kobj_uevent_env * env)308*4882a593Smuzhiyun static int host1x_device_uevent(struct device *dev,
309*4882a593Smuzhiyun 				struct kobj_uevent_env *env)
310*4882a593Smuzhiyun {
311*4882a593Smuzhiyun 	struct device_node *np = dev->parent->of_node;
312*4882a593Smuzhiyun 	unsigned int count = 0;
313*4882a593Smuzhiyun 	struct property *p;
314*4882a593Smuzhiyun 	const char *compat;
315*4882a593Smuzhiyun 
316*4882a593Smuzhiyun 	/*
317*4882a593Smuzhiyun 	 * This duplicates most of of_device_uevent(), but the latter cannot
318*4882a593Smuzhiyun 	 * be called from modules and operates on dev->of_node, which is not
319*4882a593Smuzhiyun 	 * available in this case.
320*4882a593Smuzhiyun 	 *
321*4882a593Smuzhiyun 	 * Note that this is really only needed for backwards compatibility
322*4882a593Smuzhiyun 	 * with libdrm, which parses this information from sysfs and will
323*4882a593Smuzhiyun 	 * fail if it can't find the OF_FULLNAME, specifically.
324*4882a593Smuzhiyun 	 */
325*4882a593Smuzhiyun 	add_uevent_var(env, "OF_NAME=%pOFn", np);
326*4882a593Smuzhiyun 	add_uevent_var(env, "OF_FULLNAME=%pOF", np);
327*4882a593Smuzhiyun 
328*4882a593Smuzhiyun 	of_property_for_each_string(np, "compatible", p, compat) {
329*4882a593Smuzhiyun 		add_uevent_var(env, "OF_COMPATIBLE_%u=%s", count, compat);
330*4882a593Smuzhiyun 		count++;
331*4882a593Smuzhiyun 	}
332*4882a593Smuzhiyun 
333*4882a593Smuzhiyun 	add_uevent_var(env, "OF_COMPATIBLE_N=%u", count);
334*4882a593Smuzhiyun 
335*4882a593Smuzhiyun 	return 0;
336*4882a593Smuzhiyun }
337*4882a593Smuzhiyun 
host1x_dma_configure(struct device * dev)338*4882a593Smuzhiyun static int host1x_dma_configure(struct device *dev)
339*4882a593Smuzhiyun {
340*4882a593Smuzhiyun 	return of_dma_configure(dev, dev->of_node, true);
341*4882a593Smuzhiyun }
342*4882a593Smuzhiyun 
343*4882a593Smuzhiyun static const struct dev_pm_ops host1x_device_pm_ops = {
344*4882a593Smuzhiyun 	.suspend = pm_generic_suspend,
345*4882a593Smuzhiyun 	.resume = pm_generic_resume,
346*4882a593Smuzhiyun 	.freeze = pm_generic_freeze,
347*4882a593Smuzhiyun 	.thaw = pm_generic_thaw,
348*4882a593Smuzhiyun 	.poweroff = pm_generic_poweroff,
349*4882a593Smuzhiyun 	.restore = pm_generic_restore,
350*4882a593Smuzhiyun };
351*4882a593Smuzhiyun 
352*4882a593Smuzhiyun struct bus_type host1x_bus_type = {
353*4882a593Smuzhiyun 	.name = "host1x",
354*4882a593Smuzhiyun 	.match = host1x_device_match,
355*4882a593Smuzhiyun 	.uevent = host1x_device_uevent,
356*4882a593Smuzhiyun 	.dma_configure = host1x_dma_configure,
357*4882a593Smuzhiyun 	.pm = &host1x_device_pm_ops,
358*4882a593Smuzhiyun };
359*4882a593Smuzhiyun 
__host1x_device_del(struct host1x_device * device)360*4882a593Smuzhiyun static void __host1x_device_del(struct host1x_device *device)
361*4882a593Smuzhiyun {
362*4882a593Smuzhiyun 	struct host1x_subdev *subdev, *sd;
363*4882a593Smuzhiyun 	struct host1x_client *client, *cl;
364*4882a593Smuzhiyun 
365*4882a593Smuzhiyun 	mutex_lock(&device->subdevs_lock);
366*4882a593Smuzhiyun 
367*4882a593Smuzhiyun 	/* unregister subdevices */
368*4882a593Smuzhiyun 	list_for_each_entry_safe(subdev, sd, &device->active, list) {
369*4882a593Smuzhiyun 		/*
370*4882a593Smuzhiyun 		 * host1x_subdev_unregister() will remove the client from
371*4882a593Smuzhiyun 		 * any lists, so we'll need to manually add it back to the
372*4882a593Smuzhiyun 		 * list of idle clients.
373*4882a593Smuzhiyun 		 *
374*4882a593Smuzhiyun 		 * XXX: Alternatively, perhaps don't remove the client from
375*4882a593Smuzhiyun 		 * any lists in host1x_subdev_unregister() and instead do
376*4882a593Smuzhiyun 		 * that explicitly from host1x_unregister_client()?
377*4882a593Smuzhiyun 		 */
378*4882a593Smuzhiyun 		client = subdev->client;
379*4882a593Smuzhiyun 
380*4882a593Smuzhiyun 		__host1x_subdev_unregister(device, subdev);
381*4882a593Smuzhiyun 
382*4882a593Smuzhiyun 		/* add the client to the list of idle clients */
383*4882a593Smuzhiyun 		mutex_lock(&clients_lock);
384*4882a593Smuzhiyun 		list_add_tail(&client->list, &clients);
385*4882a593Smuzhiyun 		mutex_unlock(&clients_lock);
386*4882a593Smuzhiyun 	}
387*4882a593Smuzhiyun 
388*4882a593Smuzhiyun 	/* remove subdevices */
389*4882a593Smuzhiyun 	list_for_each_entry_safe(subdev, sd, &device->subdevs, list)
390*4882a593Smuzhiyun 		host1x_subdev_del(subdev);
391*4882a593Smuzhiyun 
392*4882a593Smuzhiyun 	mutex_unlock(&device->subdevs_lock);
393*4882a593Smuzhiyun 
394*4882a593Smuzhiyun 	/* move clients to idle list */
395*4882a593Smuzhiyun 	mutex_lock(&clients_lock);
396*4882a593Smuzhiyun 	mutex_lock(&device->clients_lock);
397*4882a593Smuzhiyun 
398*4882a593Smuzhiyun 	list_for_each_entry_safe(client, cl, &device->clients, list)
399*4882a593Smuzhiyun 		list_move_tail(&client->list, &clients);
400*4882a593Smuzhiyun 
401*4882a593Smuzhiyun 	mutex_unlock(&device->clients_lock);
402*4882a593Smuzhiyun 	mutex_unlock(&clients_lock);
403*4882a593Smuzhiyun 
404*4882a593Smuzhiyun 	/* finally remove the device */
405*4882a593Smuzhiyun 	list_del_init(&device->list);
406*4882a593Smuzhiyun }
407*4882a593Smuzhiyun 
host1x_device_release(struct device * dev)408*4882a593Smuzhiyun static void host1x_device_release(struct device *dev)
409*4882a593Smuzhiyun {
410*4882a593Smuzhiyun 	struct host1x_device *device = to_host1x_device(dev);
411*4882a593Smuzhiyun 
412*4882a593Smuzhiyun 	__host1x_device_del(device);
413*4882a593Smuzhiyun 	kfree(device);
414*4882a593Smuzhiyun }
415*4882a593Smuzhiyun 
host1x_device_add(struct host1x * host1x,struct host1x_driver * driver)416*4882a593Smuzhiyun static int host1x_device_add(struct host1x *host1x,
417*4882a593Smuzhiyun 			     struct host1x_driver *driver)
418*4882a593Smuzhiyun {
419*4882a593Smuzhiyun 	struct host1x_client *client, *tmp;
420*4882a593Smuzhiyun 	struct host1x_subdev *subdev;
421*4882a593Smuzhiyun 	struct host1x_device *device;
422*4882a593Smuzhiyun 	int err;
423*4882a593Smuzhiyun 
424*4882a593Smuzhiyun 	device = kzalloc(sizeof(*device), GFP_KERNEL);
425*4882a593Smuzhiyun 	if (!device)
426*4882a593Smuzhiyun 		return -ENOMEM;
427*4882a593Smuzhiyun 
428*4882a593Smuzhiyun 	device_initialize(&device->dev);
429*4882a593Smuzhiyun 
430*4882a593Smuzhiyun 	mutex_init(&device->subdevs_lock);
431*4882a593Smuzhiyun 	INIT_LIST_HEAD(&device->subdevs);
432*4882a593Smuzhiyun 	INIT_LIST_HEAD(&device->active);
433*4882a593Smuzhiyun 	mutex_init(&device->clients_lock);
434*4882a593Smuzhiyun 	INIT_LIST_HEAD(&device->clients);
435*4882a593Smuzhiyun 	INIT_LIST_HEAD(&device->list);
436*4882a593Smuzhiyun 	device->driver = driver;
437*4882a593Smuzhiyun 
438*4882a593Smuzhiyun 	device->dev.coherent_dma_mask = host1x->dev->coherent_dma_mask;
439*4882a593Smuzhiyun 	device->dev.dma_mask = &device->dev.coherent_dma_mask;
440*4882a593Smuzhiyun 	dev_set_name(&device->dev, "%s", driver->driver.name);
441*4882a593Smuzhiyun 	device->dev.release = host1x_device_release;
442*4882a593Smuzhiyun 	device->dev.bus = &host1x_bus_type;
443*4882a593Smuzhiyun 	device->dev.parent = host1x->dev;
444*4882a593Smuzhiyun 
445*4882a593Smuzhiyun 	of_dma_configure(&device->dev, host1x->dev->of_node, true);
446*4882a593Smuzhiyun 
447*4882a593Smuzhiyun 	device->dev.dma_parms = &device->dma_parms;
448*4882a593Smuzhiyun 	dma_set_max_seg_size(&device->dev, UINT_MAX);
449*4882a593Smuzhiyun 
450*4882a593Smuzhiyun 	err = host1x_device_parse_dt(device, driver);
451*4882a593Smuzhiyun 	if (err < 0) {
452*4882a593Smuzhiyun 		kfree(device);
453*4882a593Smuzhiyun 		return err;
454*4882a593Smuzhiyun 	}
455*4882a593Smuzhiyun 
456*4882a593Smuzhiyun 	list_add_tail(&device->list, &host1x->devices);
457*4882a593Smuzhiyun 
458*4882a593Smuzhiyun 	mutex_lock(&clients_lock);
459*4882a593Smuzhiyun 
460*4882a593Smuzhiyun 	list_for_each_entry_safe(client, tmp, &clients, list) {
461*4882a593Smuzhiyun 		list_for_each_entry(subdev, &device->subdevs, list) {
462*4882a593Smuzhiyun 			if (subdev->np == client->dev->of_node) {
463*4882a593Smuzhiyun 				host1x_subdev_register(device, subdev, client);
464*4882a593Smuzhiyun 				break;
465*4882a593Smuzhiyun 			}
466*4882a593Smuzhiyun 		}
467*4882a593Smuzhiyun 	}
468*4882a593Smuzhiyun 
469*4882a593Smuzhiyun 	mutex_unlock(&clients_lock);
470*4882a593Smuzhiyun 
471*4882a593Smuzhiyun 	return 0;
472*4882a593Smuzhiyun }
473*4882a593Smuzhiyun 
474*4882a593Smuzhiyun /*
475*4882a593Smuzhiyun  * Removes a device by first unregistering any subdevices and then removing
476*4882a593Smuzhiyun  * itself from the list of devices.
477*4882a593Smuzhiyun  *
478*4882a593Smuzhiyun  * This function must be called with the host1x->devices_lock held.
479*4882a593Smuzhiyun  */
host1x_device_del(struct host1x * host1x,struct host1x_device * device)480*4882a593Smuzhiyun static void host1x_device_del(struct host1x *host1x,
481*4882a593Smuzhiyun 			      struct host1x_device *device)
482*4882a593Smuzhiyun {
483*4882a593Smuzhiyun 	if (device->registered) {
484*4882a593Smuzhiyun 		device->registered = false;
485*4882a593Smuzhiyun 		device_del(&device->dev);
486*4882a593Smuzhiyun 	}
487*4882a593Smuzhiyun 
488*4882a593Smuzhiyun 	put_device(&device->dev);
489*4882a593Smuzhiyun }
490*4882a593Smuzhiyun 
host1x_attach_driver(struct host1x * host1x,struct host1x_driver * driver)491*4882a593Smuzhiyun static void host1x_attach_driver(struct host1x *host1x,
492*4882a593Smuzhiyun 				 struct host1x_driver *driver)
493*4882a593Smuzhiyun {
494*4882a593Smuzhiyun 	struct host1x_device *device;
495*4882a593Smuzhiyun 	int err;
496*4882a593Smuzhiyun 
497*4882a593Smuzhiyun 	mutex_lock(&host1x->devices_lock);
498*4882a593Smuzhiyun 
499*4882a593Smuzhiyun 	list_for_each_entry(device, &host1x->devices, list) {
500*4882a593Smuzhiyun 		if (device->driver == driver) {
501*4882a593Smuzhiyun 			mutex_unlock(&host1x->devices_lock);
502*4882a593Smuzhiyun 			return;
503*4882a593Smuzhiyun 		}
504*4882a593Smuzhiyun 	}
505*4882a593Smuzhiyun 
506*4882a593Smuzhiyun 	err = host1x_device_add(host1x, driver);
507*4882a593Smuzhiyun 	if (err < 0)
508*4882a593Smuzhiyun 		dev_err(host1x->dev, "failed to allocate device: %d\n", err);
509*4882a593Smuzhiyun 
510*4882a593Smuzhiyun 	mutex_unlock(&host1x->devices_lock);
511*4882a593Smuzhiyun }
512*4882a593Smuzhiyun 
host1x_detach_driver(struct host1x * host1x,struct host1x_driver * driver)513*4882a593Smuzhiyun static void host1x_detach_driver(struct host1x *host1x,
514*4882a593Smuzhiyun 				 struct host1x_driver *driver)
515*4882a593Smuzhiyun {
516*4882a593Smuzhiyun 	struct host1x_device *device, *tmp;
517*4882a593Smuzhiyun 
518*4882a593Smuzhiyun 	mutex_lock(&host1x->devices_lock);
519*4882a593Smuzhiyun 
520*4882a593Smuzhiyun 	list_for_each_entry_safe(device, tmp, &host1x->devices, list)
521*4882a593Smuzhiyun 		if (device->driver == driver)
522*4882a593Smuzhiyun 			host1x_device_del(host1x, device);
523*4882a593Smuzhiyun 
524*4882a593Smuzhiyun 	mutex_unlock(&host1x->devices_lock);
525*4882a593Smuzhiyun }
526*4882a593Smuzhiyun 
host1x_devices_show(struct seq_file * s,void * data)527*4882a593Smuzhiyun static int host1x_devices_show(struct seq_file *s, void *data)
528*4882a593Smuzhiyun {
529*4882a593Smuzhiyun 	struct host1x *host1x = s->private;
530*4882a593Smuzhiyun 	struct host1x_device *device;
531*4882a593Smuzhiyun 
532*4882a593Smuzhiyun 	mutex_lock(&host1x->devices_lock);
533*4882a593Smuzhiyun 
534*4882a593Smuzhiyun 	list_for_each_entry(device, &host1x->devices, list) {
535*4882a593Smuzhiyun 		struct host1x_subdev *subdev;
536*4882a593Smuzhiyun 
537*4882a593Smuzhiyun 		seq_printf(s, "%s\n", dev_name(&device->dev));
538*4882a593Smuzhiyun 
539*4882a593Smuzhiyun 		mutex_lock(&device->subdevs_lock);
540*4882a593Smuzhiyun 
541*4882a593Smuzhiyun 		list_for_each_entry(subdev, &device->active, list)
542*4882a593Smuzhiyun 			seq_printf(s, "  %pOFf: %s\n", subdev->np,
543*4882a593Smuzhiyun 				   dev_name(subdev->client->dev));
544*4882a593Smuzhiyun 
545*4882a593Smuzhiyun 		list_for_each_entry(subdev, &device->subdevs, list)
546*4882a593Smuzhiyun 			seq_printf(s, "  %pOFf:\n", subdev->np);
547*4882a593Smuzhiyun 
548*4882a593Smuzhiyun 		mutex_unlock(&device->subdevs_lock);
549*4882a593Smuzhiyun 	}
550*4882a593Smuzhiyun 
551*4882a593Smuzhiyun 	mutex_unlock(&host1x->devices_lock);
552*4882a593Smuzhiyun 
553*4882a593Smuzhiyun 	return 0;
554*4882a593Smuzhiyun }
555*4882a593Smuzhiyun DEFINE_SHOW_ATTRIBUTE(host1x_devices);
556*4882a593Smuzhiyun 
557*4882a593Smuzhiyun /**
558*4882a593Smuzhiyun  * host1x_register() - register a host1x controller
559*4882a593Smuzhiyun  * @host1x: host1x controller
560*4882a593Smuzhiyun  *
561*4882a593Smuzhiyun  * The host1x controller driver uses this to register a host1x controller with
562*4882a593Smuzhiyun  * the infrastructure. Note that all Tegra SoC generations have only ever come
563*4882a593Smuzhiyun  * with a single host1x instance, so this function is somewhat academic.
564*4882a593Smuzhiyun  */
host1x_register(struct host1x * host1x)565*4882a593Smuzhiyun int host1x_register(struct host1x *host1x)
566*4882a593Smuzhiyun {
567*4882a593Smuzhiyun 	struct host1x_driver *driver;
568*4882a593Smuzhiyun 
569*4882a593Smuzhiyun 	mutex_lock(&devices_lock);
570*4882a593Smuzhiyun 	list_add_tail(&host1x->list, &devices);
571*4882a593Smuzhiyun 	mutex_unlock(&devices_lock);
572*4882a593Smuzhiyun 
573*4882a593Smuzhiyun 	mutex_lock(&drivers_lock);
574*4882a593Smuzhiyun 
575*4882a593Smuzhiyun 	list_for_each_entry(driver, &drivers, list)
576*4882a593Smuzhiyun 		host1x_attach_driver(host1x, driver);
577*4882a593Smuzhiyun 
578*4882a593Smuzhiyun 	mutex_unlock(&drivers_lock);
579*4882a593Smuzhiyun 
580*4882a593Smuzhiyun 	debugfs_create_file("devices", S_IRUGO, host1x->debugfs, host1x,
581*4882a593Smuzhiyun 			    &host1x_devices_fops);
582*4882a593Smuzhiyun 
583*4882a593Smuzhiyun 	return 0;
584*4882a593Smuzhiyun }
585*4882a593Smuzhiyun 
586*4882a593Smuzhiyun /**
587*4882a593Smuzhiyun  * host1x_unregister() - unregister a host1x controller
588*4882a593Smuzhiyun  * @host1x: host1x controller
589*4882a593Smuzhiyun  *
590*4882a593Smuzhiyun  * The host1x controller driver uses this to remove a host1x controller from
591*4882a593Smuzhiyun  * the infrastructure.
592*4882a593Smuzhiyun  */
host1x_unregister(struct host1x * host1x)593*4882a593Smuzhiyun int host1x_unregister(struct host1x *host1x)
594*4882a593Smuzhiyun {
595*4882a593Smuzhiyun 	struct host1x_driver *driver;
596*4882a593Smuzhiyun 
597*4882a593Smuzhiyun 	mutex_lock(&drivers_lock);
598*4882a593Smuzhiyun 
599*4882a593Smuzhiyun 	list_for_each_entry(driver, &drivers, list)
600*4882a593Smuzhiyun 		host1x_detach_driver(host1x, driver);
601*4882a593Smuzhiyun 
602*4882a593Smuzhiyun 	mutex_unlock(&drivers_lock);
603*4882a593Smuzhiyun 
604*4882a593Smuzhiyun 	mutex_lock(&devices_lock);
605*4882a593Smuzhiyun 	list_del_init(&host1x->list);
606*4882a593Smuzhiyun 	mutex_unlock(&devices_lock);
607*4882a593Smuzhiyun 
608*4882a593Smuzhiyun 	return 0;
609*4882a593Smuzhiyun }
610*4882a593Smuzhiyun 
host1x_device_probe(struct device * dev)611*4882a593Smuzhiyun static int host1x_device_probe(struct device *dev)
612*4882a593Smuzhiyun {
613*4882a593Smuzhiyun 	struct host1x_driver *driver = to_host1x_driver(dev->driver);
614*4882a593Smuzhiyun 	struct host1x_device *device = to_host1x_device(dev);
615*4882a593Smuzhiyun 
616*4882a593Smuzhiyun 	if (driver->probe)
617*4882a593Smuzhiyun 		return driver->probe(device);
618*4882a593Smuzhiyun 
619*4882a593Smuzhiyun 	return 0;
620*4882a593Smuzhiyun }
621*4882a593Smuzhiyun 
host1x_device_remove(struct device * dev)622*4882a593Smuzhiyun static int host1x_device_remove(struct device *dev)
623*4882a593Smuzhiyun {
624*4882a593Smuzhiyun 	struct host1x_driver *driver = to_host1x_driver(dev->driver);
625*4882a593Smuzhiyun 	struct host1x_device *device = to_host1x_device(dev);
626*4882a593Smuzhiyun 
627*4882a593Smuzhiyun 	if (driver->remove)
628*4882a593Smuzhiyun 		return driver->remove(device);
629*4882a593Smuzhiyun 
630*4882a593Smuzhiyun 	return 0;
631*4882a593Smuzhiyun }
632*4882a593Smuzhiyun 
host1x_device_shutdown(struct device * dev)633*4882a593Smuzhiyun static void host1x_device_shutdown(struct device *dev)
634*4882a593Smuzhiyun {
635*4882a593Smuzhiyun 	struct host1x_driver *driver = to_host1x_driver(dev->driver);
636*4882a593Smuzhiyun 	struct host1x_device *device = to_host1x_device(dev);
637*4882a593Smuzhiyun 
638*4882a593Smuzhiyun 	if (driver->shutdown)
639*4882a593Smuzhiyun 		driver->shutdown(device);
640*4882a593Smuzhiyun }
641*4882a593Smuzhiyun 
642*4882a593Smuzhiyun /**
643*4882a593Smuzhiyun  * host1x_driver_register_full() - register a host1x driver
644*4882a593Smuzhiyun  * @driver: host1x driver
645*4882a593Smuzhiyun  * @owner: owner module
646*4882a593Smuzhiyun  *
647*4882a593Smuzhiyun  * Drivers for host1x logical devices call this function to register a driver
648*4882a593Smuzhiyun  * with the infrastructure. Note that since these drive logical devices, the
649*4882a593Smuzhiyun  * registration of the driver actually triggers tho logical device creation.
650*4882a593Smuzhiyun  * A logical device will be created for each host1x instance.
651*4882a593Smuzhiyun  */
host1x_driver_register_full(struct host1x_driver * driver,struct module * owner)652*4882a593Smuzhiyun int host1x_driver_register_full(struct host1x_driver *driver,
653*4882a593Smuzhiyun 				struct module *owner)
654*4882a593Smuzhiyun {
655*4882a593Smuzhiyun 	struct host1x *host1x;
656*4882a593Smuzhiyun 
657*4882a593Smuzhiyun 	INIT_LIST_HEAD(&driver->list);
658*4882a593Smuzhiyun 
659*4882a593Smuzhiyun 	mutex_lock(&drivers_lock);
660*4882a593Smuzhiyun 	list_add_tail(&driver->list, &drivers);
661*4882a593Smuzhiyun 	mutex_unlock(&drivers_lock);
662*4882a593Smuzhiyun 
663*4882a593Smuzhiyun 	mutex_lock(&devices_lock);
664*4882a593Smuzhiyun 
665*4882a593Smuzhiyun 	list_for_each_entry(host1x, &devices, list)
666*4882a593Smuzhiyun 		host1x_attach_driver(host1x, driver);
667*4882a593Smuzhiyun 
668*4882a593Smuzhiyun 	mutex_unlock(&devices_lock);
669*4882a593Smuzhiyun 
670*4882a593Smuzhiyun 	driver->driver.bus = &host1x_bus_type;
671*4882a593Smuzhiyun 	driver->driver.owner = owner;
672*4882a593Smuzhiyun 	driver->driver.probe = host1x_device_probe;
673*4882a593Smuzhiyun 	driver->driver.remove = host1x_device_remove;
674*4882a593Smuzhiyun 	driver->driver.shutdown = host1x_device_shutdown;
675*4882a593Smuzhiyun 
676*4882a593Smuzhiyun 	return driver_register(&driver->driver);
677*4882a593Smuzhiyun }
678*4882a593Smuzhiyun EXPORT_SYMBOL(host1x_driver_register_full);
679*4882a593Smuzhiyun 
680*4882a593Smuzhiyun /**
681*4882a593Smuzhiyun  * host1x_driver_unregister() - unregister a host1x driver
682*4882a593Smuzhiyun  * @driver: host1x driver
683*4882a593Smuzhiyun  *
684*4882a593Smuzhiyun  * Unbinds the driver from each of the host1x logical devices that it is
685*4882a593Smuzhiyun  * bound to, effectively removing the subsystem devices that they represent.
686*4882a593Smuzhiyun  */
host1x_driver_unregister(struct host1x_driver * driver)687*4882a593Smuzhiyun void host1x_driver_unregister(struct host1x_driver *driver)
688*4882a593Smuzhiyun {
689*4882a593Smuzhiyun 	struct host1x *host1x;
690*4882a593Smuzhiyun 
691*4882a593Smuzhiyun 	driver_unregister(&driver->driver);
692*4882a593Smuzhiyun 
693*4882a593Smuzhiyun 	mutex_lock(&devices_lock);
694*4882a593Smuzhiyun 
695*4882a593Smuzhiyun 	list_for_each_entry(host1x, &devices, list)
696*4882a593Smuzhiyun 		host1x_detach_driver(host1x, driver);
697*4882a593Smuzhiyun 
698*4882a593Smuzhiyun 	mutex_unlock(&devices_lock);
699*4882a593Smuzhiyun 
700*4882a593Smuzhiyun 	mutex_lock(&drivers_lock);
701*4882a593Smuzhiyun 	list_del_init(&driver->list);
702*4882a593Smuzhiyun 	mutex_unlock(&drivers_lock);
703*4882a593Smuzhiyun }
704*4882a593Smuzhiyun EXPORT_SYMBOL(host1x_driver_unregister);
705*4882a593Smuzhiyun 
706*4882a593Smuzhiyun /**
707*4882a593Smuzhiyun  * __host1x_client_init() - initialize a host1x client
708*4882a593Smuzhiyun  * @client: host1x client
709*4882a593Smuzhiyun  * @key: lock class key for the client-specific mutex
710*4882a593Smuzhiyun  */
__host1x_client_init(struct host1x_client * client,struct lock_class_key * key)711*4882a593Smuzhiyun void __host1x_client_init(struct host1x_client *client, struct lock_class_key *key)
712*4882a593Smuzhiyun {
713*4882a593Smuzhiyun 	INIT_LIST_HEAD(&client->list);
714*4882a593Smuzhiyun 	__mutex_init(&client->lock, "host1x client lock", key);
715*4882a593Smuzhiyun 	client->usecount = 0;
716*4882a593Smuzhiyun }
717*4882a593Smuzhiyun EXPORT_SYMBOL(__host1x_client_init);
718*4882a593Smuzhiyun 
719*4882a593Smuzhiyun /**
720*4882a593Smuzhiyun  * host1x_client_exit() - uninitialize a host1x client
721*4882a593Smuzhiyun  * @client: host1x client
722*4882a593Smuzhiyun  */
host1x_client_exit(struct host1x_client * client)723*4882a593Smuzhiyun void host1x_client_exit(struct host1x_client *client)
724*4882a593Smuzhiyun {
725*4882a593Smuzhiyun 	mutex_destroy(&client->lock);
726*4882a593Smuzhiyun }
727*4882a593Smuzhiyun EXPORT_SYMBOL(host1x_client_exit);
728*4882a593Smuzhiyun 
729*4882a593Smuzhiyun /**
730*4882a593Smuzhiyun  * __host1x_client_register() - register a host1x client
731*4882a593Smuzhiyun  * @client: host1x client
732*4882a593Smuzhiyun  * @key: lock class key for the client-specific mutex
733*4882a593Smuzhiyun  *
734*4882a593Smuzhiyun  * Registers a host1x client with each host1x controller instance. Note that
735*4882a593Smuzhiyun  * each client will only match their parent host1x controller and will only be
736*4882a593Smuzhiyun  * associated with that instance. Once all clients have been registered with
737*4882a593Smuzhiyun  * their parent host1x controller, the infrastructure will set up the logical
738*4882a593Smuzhiyun  * device and call host1x_device_init(), which will in turn call each client's
739*4882a593Smuzhiyun  * &host1x_client_ops.init implementation.
740*4882a593Smuzhiyun  */
__host1x_client_register(struct host1x_client * client)741*4882a593Smuzhiyun int __host1x_client_register(struct host1x_client *client)
742*4882a593Smuzhiyun {
743*4882a593Smuzhiyun 	struct host1x *host1x;
744*4882a593Smuzhiyun 	int err;
745*4882a593Smuzhiyun 
746*4882a593Smuzhiyun 	mutex_lock(&devices_lock);
747*4882a593Smuzhiyun 
748*4882a593Smuzhiyun 	list_for_each_entry(host1x, &devices, list) {
749*4882a593Smuzhiyun 		err = host1x_add_client(host1x, client);
750*4882a593Smuzhiyun 		if (!err) {
751*4882a593Smuzhiyun 			mutex_unlock(&devices_lock);
752*4882a593Smuzhiyun 			return 0;
753*4882a593Smuzhiyun 		}
754*4882a593Smuzhiyun 	}
755*4882a593Smuzhiyun 
756*4882a593Smuzhiyun 	mutex_unlock(&devices_lock);
757*4882a593Smuzhiyun 
758*4882a593Smuzhiyun 	mutex_lock(&clients_lock);
759*4882a593Smuzhiyun 	list_add_tail(&client->list, &clients);
760*4882a593Smuzhiyun 	mutex_unlock(&clients_lock);
761*4882a593Smuzhiyun 
762*4882a593Smuzhiyun 	return 0;
763*4882a593Smuzhiyun }
764*4882a593Smuzhiyun EXPORT_SYMBOL(__host1x_client_register);
765*4882a593Smuzhiyun 
766*4882a593Smuzhiyun /**
767*4882a593Smuzhiyun  * host1x_client_unregister() - unregister a host1x client
768*4882a593Smuzhiyun  * @client: host1x client
769*4882a593Smuzhiyun  *
770*4882a593Smuzhiyun  * Removes a host1x client from its host1x controller instance. If a logical
771*4882a593Smuzhiyun  * device has already been initialized, it will be torn down.
772*4882a593Smuzhiyun  */
host1x_client_unregister(struct host1x_client * client)773*4882a593Smuzhiyun int host1x_client_unregister(struct host1x_client *client)
774*4882a593Smuzhiyun {
775*4882a593Smuzhiyun 	struct host1x_client *c;
776*4882a593Smuzhiyun 	struct host1x *host1x;
777*4882a593Smuzhiyun 	int err;
778*4882a593Smuzhiyun 
779*4882a593Smuzhiyun 	mutex_lock(&devices_lock);
780*4882a593Smuzhiyun 
781*4882a593Smuzhiyun 	list_for_each_entry(host1x, &devices, list) {
782*4882a593Smuzhiyun 		err = host1x_del_client(host1x, client);
783*4882a593Smuzhiyun 		if (!err) {
784*4882a593Smuzhiyun 			mutex_unlock(&devices_lock);
785*4882a593Smuzhiyun 			return 0;
786*4882a593Smuzhiyun 		}
787*4882a593Smuzhiyun 	}
788*4882a593Smuzhiyun 
789*4882a593Smuzhiyun 	mutex_unlock(&devices_lock);
790*4882a593Smuzhiyun 	mutex_lock(&clients_lock);
791*4882a593Smuzhiyun 
792*4882a593Smuzhiyun 	list_for_each_entry(c, &clients, list) {
793*4882a593Smuzhiyun 		if (c == client) {
794*4882a593Smuzhiyun 			list_del_init(&c->list);
795*4882a593Smuzhiyun 			break;
796*4882a593Smuzhiyun 		}
797*4882a593Smuzhiyun 	}
798*4882a593Smuzhiyun 
799*4882a593Smuzhiyun 	mutex_unlock(&clients_lock);
800*4882a593Smuzhiyun 
801*4882a593Smuzhiyun 	return 0;
802*4882a593Smuzhiyun }
803*4882a593Smuzhiyun EXPORT_SYMBOL(host1x_client_unregister);
804*4882a593Smuzhiyun 
host1x_client_suspend(struct host1x_client * client)805*4882a593Smuzhiyun int host1x_client_suspend(struct host1x_client *client)
806*4882a593Smuzhiyun {
807*4882a593Smuzhiyun 	int err = 0;
808*4882a593Smuzhiyun 
809*4882a593Smuzhiyun 	mutex_lock(&client->lock);
810*4882a593Smuzhiyun 
811*4882a593Smuzhiyun 	if (client->usecount == 1) {
812*4882a593Smuzhiyun 		if (client->ops && client->ops->suspend) {
813*4882a593Smuzhiyun 			err = client->ops->suspend(client);
814*4882a593Smuzhiyun 			if (err < 0)
815*4882a593Smuzhiyun 				goto unlock;
816*4882a593Smuzhiyun 		}
817*4882a593Smuzhiyun 	}
818*4882a593Smuzhiyun 
819*4882a593Smuzhiyun 	client->usecount--;
820*4882a593Smuzhiyun 	dev_dbg(client->dev, "use count: %u\n", client->usecount);
821*4882a593Smuzhiyun 
822*4882a593Smuzhiyun 	if (client->parent) {
823*4882a593Smuzhiyun 		err = host1x_client_suspend(client->parent);
824*4882a593Smuzhiyun 		if (err < 0)
825*4882a593Smuzhiyun 			goto resume;
826*4882a593Smuzhiyun 	}
827*4882a593Smuzhiyun 
828*4882a593Smuzhiyun 	goto unlock;
829*4882a593Smuzhiyun 
830*4882a593Smuzhiyun resume:
831*4882a593Smuzhiyun 	if (client->usecount == 0)
832*4882a593Smuzhiyun 		if (client->ops && client->ops->resume)
833*4882a593Smuzhiyun 			client->ops->resume(client);
834*4882a593Smuzhiyun 
835*4882a593Smuzhiyun 	client->usecount++;
836*4882a593Smuzhiyun unlock:
837*4882a593Smuzhiyun 	mutex_unlock(&client->lock);
838*4882a593Smuzhiyun 	return err;
839*4882a593Smuzhiyun }
840*4882a593Smuzhiyun EXPORT_SYMBOL(host1x_client_suspend);
841*4882a593Smuzhiyun 
host1x_client_resume(struct host1x_client * client)842*4882a593Smuzhiyun int host1x_client_resume(struct host1x_client *client)
843*4882a593Smuzhiyun {
844*4882a593Smuzhiyun 	int err = 0;
845*4882a593Smuzhiyun 
846*4882a593Smuzhiyun 	mutex_lock(&client->lock);
847*4882a593Smuzhiyun 
848*4882a593Smuzhiyun 	if (client->parent) {
849*4882a593Smuzhiyun 		err = host1x_client_resume(client->parent);
850*4882a593Smuzhiyun 		if (err < 0)
851*4882a593Smuzhiyun 			goto unlock;
852*4882a593Smuzhiyun 	}
853*4882a593Smuzhiyun 
854*4882a593Smuzhiyun 	if (client->usecount == 0) {
855*4882a593Smuzhiyun 		if (client->ops && client->ops->resume) {
856*4882a593Smuzhiyun 			err = client->ops->resume(client);
857*4882a593Smuzhiyun 			if (err < 0)
858*4882a593Smuzhiyun 				goto suspend;
859*4882a593Smuzhiyun 		}
860*4882a593Smuzhiyun 	}
861*4882a593Smuzhiyun 
862*4882a593Smuzhiyun 	client->usecount++;
863*4882a593Smuzhiyun 	dev_dbg(client->dev, "use count: %u\n", client->usecount);
864*4882a593Smuzhiyun 
865*4882a593Smuzhiyun 	goto unlock;
866*4882a593Smuzhiyun 
867*4882a593Smuzhiyun suspend:
868*4882a593Smuzhiyun 	if (client->parent)
869*4882a593Smuzhiyun 		host1x_client_suspend(client->parent);
870*4882a593Smuzhiyun unlock:
871*4882a593Smuzhiyun 	mutex_unlock(&client->lock);
872*4882a593Smuzhiyun 	return err;
873*4882a593Smuzhiyun }
874*4882a593Smuzhiyun EXPORT_SYMBOL(host1x_client_resume);
875