xref: /linux/drivers/gpu/host1x/context.c (revision 5b0764a53235d3c77e1ed674e836e1b3fe1a4b1f)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2021, NVIDIA Corporation.
4  */
5 
6 #include <linux/completion.h>
7 #include <linux/device.h>
8 #include <linux/host1x_context_bus.h>
9 #include <linux/kref.h>
10 #include <linux/list.h>
11 #include <linux/moduleparam.h>
12 #include <linux/of.h>
13 #include <linux/of_device.h>
14 #include <linux/pid.h>
15 #include <linux/slab.h>
16 
17 #include "context.h"
18 #include "dev.h"
19 
20 static bool static_context_alloc;
21 module_param(static_context_alloc, bool, 0644);
22 MODULE_PARM_DESC(static_context_alloc, "If enabled, memory contexts are allocated immediately on channel open and cannot be relinquished while a channel is open");
23 
24 static void host1x_memory_context_release(struct device *dev)
25 {
26 	/* context device is freed in host1x_memory_context_list_free() */
27 }
28 
29 int host1x_memory_context_list_init(struct host1x *host1x)
30 {
31 	struct host1x_memory_context_list *cdl = &host1x->context_list;
32 	struct device_node *node = host1x->dev->of_node;
33 	struct host1x_hw_memory_context *ctx;
34 	unsigned int devs, i;
35 	int err;
36 
37 	cdl->devs = NULL;
38 	cdl->len = 0;
39 	mutex_init(&cdl->lock);
40 	INIT_LIST_HEAD(&cdl->waiters);
41 
42 	err = of_property_count_u32_elems(node, "iommu-map");
43 	if (err < 0)
44 		return 0;
45 
46 	devs = 0;
47 
48 	for (i = 0; i < err / 4; i++) {
49 		u32 length;
50 
51 		of_property_read_u32_index(node, "iommu-map", i * 4 + 3, &length);
52 		devs += length;
53 	}
54 
55 	cdl->len = devs;
56 	cdl->devs = kzalloc_objs(*cdl->devs, cdl->len);
57 	if (!cdl->devs)
58 		return -ENOMEM;
59 
60 	for (i = 0; i < cdl->len; i++) {
61 		ctx = &cdl->devs[i];
62 
63 		ctx->host = host1x;
64 		INIT_LIST_HEAD(&ctx->owners);
65 
66 		device_initialize(&ctx->dev);
67 
68 		/*
69 		 * Due to an issue with T194 NVENC, only 38 bits can be used.
70 		 * Anyway, 256GiB of IOVA ought to be enough for anyone.
71 		 */
72 		ctx->dma_mask = DMA_BIT_MASK(38);
73 		ctx->dev.dma_mask = &ctx->dma_mask;
74 		ctx->dev.coherent_dma_mask = ctx->dma_mask;
75 		dev_set_name(&ctx->dev, "host1x-ctx.%d", i);
76 		ctx->dev.bus = &host1x_context_device_bus_type;
77 		ctx->dev.parent = host1x->dev;
78 		ctx->dev.release = host1x_memory_context_release;
79 
80 		ctx->dev.dma_parms = &ctx->dma_parms;
81 		dma_set_max_seg_size(&ctx->dev, UINT_MAX);
82 
83 		err = device_add(&ctx->dev);
84 		if (err) {
85 			dev_err(host1x->dev, "could not add context device %d: %d\n", i, err);
86 			put_device(&ctx->dev);
87 			goto unreg_devices;
88 		}
89 
90 		err = of_dma_configure_id(&ctx->dev, node, true, &i);
91 		if (err) {
92 			dev_err(host1x->dev, "IOMMU configuration failed for context device %d: %d\n",
93 				i, err);
94 			device_unregister(&ctx->dev);
95 			goto unreg_devices;
96 		}
97 
98 		if (!tegra_dev_iommu_get_stream_id(&ctx->dev, &ctx->stream_id) ||
99 		    !device_iommu_mapped(&ctx->dev)) {
100 			dev_err(host1x->dev, "Context device %d has no IOMMU!\n", i);
101 			device_unregister(&ctx->dev);
102 
103 			/*
104 			 * This means that if IOMMU is disabled but context devices
105 			 * are defined in the device tree, Host1x will fail to probe.
106 			 * That's probably OK in this time and age.
107 			 */
108 			err = -EINVAL;
109 
110 			goto unreg_devices;
111 		}
112 	}
113 
114 	return 0;
115 
116 unreg_devices:
117 	while (i--)
118 		device_unregister(&cdl->devs[i].dev);
119 
120 	kfree(cdl->devs);
121 	cdl->devs = NULL;
122 	cdl->len = 0;
123 
124 	return err;
125 }
126 
127 void host1x_memory_context_list_free(struct host1x_memory_context_list *cdl)
128 {
129 	unsigned int i;
130 
131 	for (i = 0; i < cdl->len; i++)
132 		device_unregister(&cdl->devs[i].dev);
133 
134 	kfree(cdl->devs);
135 	cdl->len = 0;
136 }
137 
138 static bool hw_usable_for_dev(struct host1x_hw_memory_context *hw, struct device *dev)
139 {
140 	return hw->dev.iommu->iommu_dev == dev->iommu->iommu_dev;
141 }
142 
143 static struct host1x_hw_memory_context *
144 host1x_memory_context_alloc_hw_locked(struct host1x *host1x, struct device *dev,
145 				      struct pid *pid)
146 {
147 	struct host1x_memory_context_list *cdl = &host1x->context_list;
148 	struct host1x_hw_memory_context *free = NULL, *can_steal = NULL;
149 	struct host1x_memory_context *ctx;
150 	int i;
151 
152 	if (!cdl->len)
153 		return ERR_PTR(-EOPNOTSUPP);
154 
155 	for (i = 0; i < cdl->len; i++) {
156 		struct host1x_hw_memory_context *cd = &cdl->devs[i];
157 
158 		if (!hw_usable_for_dev(cd, dev))
159 			continue;
160 
161 		if (cd->owner == pid) {
162 			refcount_inc(&cd->ref);
163 			return cd;
164 		} else if (!cd->owner && !free) {
165 			free = cd;
166 		} else if (refcount_read(&cd->active) == 0) {
167 			can_steal = cd;
168 		}
169 	}
170 
171 	if (free)
172 		goto found;
173 
174 	/* Steal */
175 
176 	if (!can_steal || static_context_alloc)
177 		return ERR_PTR(-EBUSY);
178 
179 	list_for_each_entry(ctx, &can_steal->owners, entry) {
180 		struct host1x_context_mapping *mapping;
181 
182 		ctx->hw = NULL;
183 
184 		list_for_each_entry(mapping, &ctx->mappings, entry) {
185 			host1x_bo_unpin(mapping->mapping);
186 			mapping->mapping = NULL;
187 		}
188 	}
189 
190 	put_pid(can_steal->owner);
191 
192 	free = can_steal;
193 
194 found:
195 	refcount_set(&free->ref, 1);
196 	free->owner = get_pid(pid);
197 	INIT_LIST_HEAD(&free->owners);
198 
199 	return free;
200 }
201 
202 static void host1x_memory_context_hw_put(struct host1x_hw_memory_context *cd)
203 {
204 	if (refcount_dec_and_test(&cd->ref)) {
205 		put_pid(cd->owner);
206 		cd->owner = NULL;
207 	}
208 }
209 
210 /**
211  * host1x_memory_context_alloc() - allocate a logical memory context
212  * @host1x: host1x instance
213  * @dev: engine device that will own the context
214  * @pid: PID (TGID) of the process the context is allocated for
215  *
216  * Allocate a logical memory context. The context has no backing hardware memory
217  * context until host1x_memory_context_active() is called unless static context
218  * allocation is enabled.
219  *
220  * Return: pointer to the new context, or an ERR_PTR() on failure.
221  * ERR_PTR(-EOPNOTSUPP) is returned if the platform has no hardware memory
222  * contexts.
223  */
224 struct host1x_memory_context *host1x_memory_context_alloc(struct host1x *host1x,
225 							  struct device *dev,
226 							  struct pid *pid)
227 {
228 	struct host1x_memory_context_list *cdl = &host1x->context_list;
229 	struct host1x_memory_context *ctx;
230 	int err;
231 
232 	if (!cdl->len)
233 		return ERR_PTR(-EOPNOTSUPP);
234 
235 	ctx = kzalloc_obj(*ctx, GFP_KERNEL);
236 	if (!ctx)
237 		return ERR_PTR(-ENOMEM);
238 
239 	ctx->host = host1x;
240 	ctx->dev = dev;
241 	ctx->pid = get_pid(pid);
242 
243 	refcount_set(&ctx->ref, 1);
244 	INIT_LIST_HEAD(&ctx->mappings);
245 
246 	if (static_context_alloc) {
247 		err = host1x_memory_context_active(ctx);
248 		if (err) {
249 			put_pid(ctx->pid);
250 			kfree(ctx);
251 			return ERR_PTR(err);
252 		}
253 
254 		ctx->static_alloc = true;
255 	}
256 
257 	return ctx;
258 }
259 EXPORT_SYMBOL_GPL(host1x_memory_context_alloc);
260 
261 struct hw_alloc_waiter {
262 	struct completion wait; /* Completion to wait for free hw context */
263 	struct list_head entry;
264 	struct device *dev;
265 };
266 
267 /**
268  * host1x_memory_context_active() - mark a memory context as in use
269  * @ctx: memory context
270  *
271  * Bind a hardware memory context to @ctx and map all of its stored mappings,
272  * allocating or stealing a hardware context from an idle context if needed.
273  * While the context is active its hardware context cannot be stolen away.
274  *
275  * Each successful call must be balanced by exactly one
276  * host1x_memory_context_inactive() call.
277  *
278  * May sleep, and may block until a hardware context becomes available unless
279  * static allocation is in use. Must be called in process context.
280  *
281  * Return: 0 on success or a negative error code. -EBUSY is returned if no
282  * hardware context could be obtained and static context allocation is enabled.
283  */
284 int host1x_memory_context_active(struct host1x_memory_context *ctx)
285 {
286 	struct host1x_memory_context_list *cdl = &ctx->host->context_list;
287 	struct host1x_context_mapping *mapping;
288 	struct host1x_hw_memory_context *hw;
289 	struct hw_alloc_waiter waiter;
290 	bool retrying = false;
291 	int err = 0;
292 
293 	mutex_lock(&cdl->lock);
294 
295 retry:
296 	if (ctx->hw)
297 		goto have_hw;
298 
299 	hw = host1x_memory_context_alloc_hw_locked(ctx->host, ctx->dev, ctx->pid);
300 	if (PTR_ERR(hw) == -EBUSY) {
301 		/* All contexts busy. Wait for free context. */
302 		if (static_context_alloc) {
303 			dev_warn(ctx->dev, "%s: all memory contexts are busy\n", current->comm);
304 			err = -EBUSY;
305 			goto unlock;
306 		}
307 		if (!retrying)
308 			dev_warn(ctx->dev, "%s: all memory contexts are busy, waiting\n",
309 				 current->comm);
310 
311 		init_completion(&waiter.wait);
312 		waiter.dev = ctx->dev;
313 		list_add(&waiter.entry, &cdl->waiters);
314 
315 		mutex_unlock(&cdl->lock);
316 		err = wait_for_completion_interruptible(&waiter.wait);
317 		mutex_lock(&cdl->lock);
318 
319 		list_del(&waiter.entry);
320 		if (err)
321 			goto unlock;
322 
323 		retrying = true;
324 		goto retry;
325 	}
326 	if (IS_ERR(hw)) {
327 		err = PTR_ERR(hw);
328 		goto unlock;
329 	}
330 
331 	ctx->hw = hw;
332 	list_add(&ctx->entry, &hw->owners);
333 
334 	list_for_each_entry(mapping, &ctx->mappings, entry) {
335 		mapping->mapping = host1x_bo_pin(&hw->dev, mapping->bo,
336 						 mapping->direction, NULL);
337 		if (IS_ERR(mapping->mapping)) {
338 			err = PTR_ERR(mapping->mapping);
339 			mapping->mapping = NULL;
340 			goto unpin;
341 		}
342 	}
343 
344 have_hw:
345 	/* We're just counting active users. No need to do anything on 0->1 transition.*/
346 	if (!refcount_inc_not_zero(&ctx->hw->active))
347 		refcount_set(&ctx->hw->active, 1);
348 
349 	mutex_unlock(&cdl->lock);
350 
351 	return 0;
352 
353 unpin:
354 	list_for_each_entry(mapping, &ctx->mappings, entry) {
355 		if (mapping->mapping) {
356 			host1x_bo_unpin(mapping->mapping);
357 			mapping->mapping = NULL;
358 		}
359 	}
360 
361 	host1x_memory_context_hw_put(ctx->hw);
362 	list_del(&ctx->entry);
363 	ctx->hw = NULL;
364 unlock:
365 	mutex_unlock(&cdl->lock);
366 	return err;
367 }
368 EXPORT_SYMBOL_GPL(host1x_memory_context_active);
369 
370 /**
371  * host1x_memory_context_map() - add a buffer mapping to a memory context
372  * @ctx: memory context
373  * @bo: buffer object to map
374  * @direction: DMA direction of the mapping
375  *
376  * Register a mapping of @bo on @ctx. If @ctx currently has a hardware context
377  * bound, the buffer is mapped immediately; otherwise it is pinned the next time
378  * the context is made active. The mapping is unmapped and freed by
379  * host1x_memory_context_unmap().
380  *
381  * Return: pointer to the mapping, or an ERR_PTR() on failure.
382  */
383 struct host1x_context_mapping *host1x_memory_context_map(struct host1x_memory_context *ctx,
384 							 struct host1x_bo *bo,
385 							 enum dma_data_direction direction)
386 {
387 	struct host1x_memory_context_list *cdl = &ctx->host->context_list;
388 	struct host1x_context_mapping *m;
389 	struct host1x_bo_mapping *bo_m;
390 
391 	m = kzalloc_obj(*m, GFP_KERNEL);
392 	if (!m)
393 		return ERR_PTR(-ENOMEM);
394 
395 	m->host = ctx->host;
396 	m->bo = bo;
397 	m->direction = direction;
398 
399 	mutex_lock(&cdl->lock);
400 
401 	if (ctx->hw) {
402 		bo_m = host1x_bo_pin(&ctx->hw->dev, bo, direction, NULL);
403 		if (IS_ERR(bo_m)) {
404 			mutex_unlock(&cdl->lock);
405 			kfree(m);
406 
407 			return ERR_CAST(bo_m);
408 		}
409 
410 		m->mapping = bo_m;
411 	}
412 
413 	list_add(&m->entry, &ctx->mappings);
414 
415 	mutex_unlock(&cdl->lock);
416 
417 	return m;
418 }
419 EXPORT_SYMBOL_GPL(host1x_memory_context_map);
420 
421 /**
422  * host1x_memory_context_unmap() - remove a buffer mapping from a memory context
423  * @m: mapping previously returned by host1x_memory_context_map()
424  *
425  * Unmap the buffer if it is currently mapped, and free the mapping.
426  */
427 void host1x_memory_context_unmap(struct host1x_context_mapping *m)
428 {
429 	struct host1x_memory_context_list *cdl = &m->host->context_list;
430 
431 	mutex_lock(&cdl->lock);
432 
433 	list_del(&m->entry);
434 
435 	mutex_unlock(&cdl->lock);
436 
437 	if (m->mapping)
438 		host1x_bo_unpin(m->mapping);
439 
440 	kfree(m);
441 }
442 EXPORT_SYMBOL_GPL(host1x_memory_context_unmap);
443 
444 static void host1x_memory_context_inactive_locked(struct host1x_memory_context *ctx)
445 {
446 	struct host1x_memory_context_list *cdl = &ctx->host->context_list;
447 	struct hw_alloc_waiter *waiter;
448 
449 	if (refcount_dec_and_test(&ctx->hw->active)) {
450 		/* Hardware context becomes eligible for stealing */
451 		list_for_each_entry(waiter, &cdl->waiters, entry) {
452 			if (!hw_usable_for_dev(ctx->hw, waiter->dev))
453 				continue;
454 
455 			complete(&waiter->wait);
456 
457 			/*
458 			 * Need to wake up all waiters -- there could be multiple from
459 			 * the same process that can use the same freed hardware context.
460 			 */
461 		}
462 	}
463 }
464 
465 /**
466  * host1x_memory_context_inactive() - mark a memory context as no longer in use
467  * @ctx: memory context
468  *
469  * Drop one active reference taken by host1x_memory_context_active(). When the
470  * last active reference is dropped, the backing hardware context becomes
471  * eligible to be stolen by another context, and any waiters are woken.
472  *
473  * Must be called exactly once for each successful
474  * host1x_memory_context_active().
475  */
476 void host1x_memory_context_inactive(struct host1x_memory_context *ctx)
477 {
478 	struct host1x_memory_context_list *cdl = &ctx->host->context_list;
479 
480 	mutex_lock(&cdl->lock);
481 
482 	host1x_memory_context_inactive_locked(ctx);
483 
484 	mutex_unlock(&cdl->lock);
485 }
486 EXPORT_SYMBOL_GPL(host1x_memory_context_inactive);
487 
488 /**
489  * host1x_memory_context_get() - take a reference to a memory context
490  * @ctx: memory context
491  */
492 void host1x_memory_context_get(struct host1x_memory_context *ctx)
493 {
494 	refcount_inc(&ctx->ref);
495 }
496 EXPORT_SYMBOL_GPL(host1x_memory_context_get);
497 
498 /**
499  * host1x_memory_context_put() - release a reference to a memory context
500  * @ctx: memory context
501  *
502  * Drop a reference taken by host1x_memory_context_alloc() or
503  * host1x_memory_context_get(). When the last reference is dropped the context
504  * is detached from its hardware context and freed.
505  */
506 void host1x_memory_context_put(struct host1x_memory_context *ctx)
507 {
508 	struct host1x_memory_context_list *cdl = &ctx->host->context_list;
509 
510 	if (refcount_dec_and_mutex_lock(&ctx->ref, &cdl->lock)) {
511 		if (ctx->static_alloc)
512 			host1x_memory_context_inactive_locked(ctx);
513 
514 		if (ctx->hw) {
515 			list_del(&ctx->entry);
516 
517 			host1x_memory_context_hw_put(ctx->hw);
518 			ctx->hw = NULL;
519 
520 			WARN_ON(!list_empty(&ctx->mappings));
521 		}
522 
523 		put_pid(ctx->pid);
524 		mutex_unlock(&cdl->lock);
525 		kfree(ctx);
526 	}
527 }
528 EXPORT_SYMBOL_GPL(host1x_memory_context_put);
529