xref: /linux/drivers/gpu/host1x/context.c (revision d639d9fa162aadec1ae9980c4dcf6e50bd2f8290)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2021, NVIDIA Corporation.
4  */
5 
6 #include <linux/device.h>
7 #include <linux/host1x_context_bus.h>
8 #include <linux/kref.h>
9 #include <linux/of.h>
10 #include <linux/of_device.h>
11 #include <linux/pid.h>
12 #include <linux/slab.h>
13 
14 #include "context.h"
15 #include "dev.h"
16 
17 static void host1x_memory_context_release(struct device *dev)
18 {
19 	/* context device is freed in host1x_memory_context_list_free() */
20 }
21 
22 int host1x_memory_context_list_init(struct host1x *host1x)
23 {
24 	struct host1x_memory_context_list *cdl = &host1x->context_list;
25 	struct device_node *node = host1x->dev->of_node;
26 	struct host1x_memory_context *ctx;
27 	unsigned int i;
28 	int err;
29 
30 	cdl->devs = NULL;
31 	cdl->len = 0;
32 	mutex_init(&cdl->lock);
33 
34 	err = of_property_count_u32_elems(node, "iommu-map");
35 	if (err < 0)
36 		return 0;
37 
38 	cdl->len = err / 4;
39 	cdl->devs = kzalloc_objs(*cdl->devs, cdl->len);
40 	if (!cdl->devs)
41 		return -ENOMEM;
42 
43 	for (i = 0; i < cdl->len; i++) {
44 		ctx = &cdl->devs[i];
45 
46 		ctx->host = host1x;
47 
48 		device_initialize(&ctx->dev);
49 
50 		/*
51 		 * Due to an issue with T194 NVENC, only 38 bits can be used.
52 		 * Anyway, 256GiB of IOVA ought to be enough for anyone.
53 		 */
54 		ctx->dma_mask = DMA_BIT_MASK(38);
55 		ctx->dev.dma_mask = &ctx->dma_mask;
56 		ctx->dev.coherent_dma_mask = ctx->dma_mask;
57 		dev_set_name(&ctx->dev, "host1x-ctx.%d", i);
58 		ctx->dev.bus = &host1x_context_device_bus_type;
59 		ctx->dev.parent = host1x->dev;
60 		ctx->dev.release = host1x_memory_context_release;
61 
62 		ctx->dev.dma_parms = &ctx->dma_parms;
63 		dma_set_max_seg_size(&ctx->dev, UINT_MAX);
64 
65 		err = device_add(&ctx->dev);
66 		if (err) {
67 			dev_err(host1x->dev, "could not add context device %d: %d\n", i, err);
68 			put_device(&ctx->dev);
69 			goto unreg_devices;
70 		}
71 
72 		err = of_dma_configure_id(&ctx->dev, node, true, &i);
73 		if (err) {
74 			dev_err(host1x->dev, "IOMMU configuration failed for context device %d: %d\n",
75 				i, err);
76 			device_unregister(&ctx->dev);
77 			goto unreg_devices;
78 		}
79 
80 		if (!tegra_dev_iommu_get_stream_id(&ctx->dev, &ctx->stream_id) ||
81 		    !device_iommu_mapped(&ctx->dev)) {
82 			dev_err(host1x->dev, "Context device %d has no IOMMU!\n", i);
83 			device_unregister(&ctx->dev);
84 
85 			/*
86 			 * This means that if IOMMU is disabled but context devices
87 			 * are defined in the device tree, Host1x will fail to probe.
88 			 * That's probably OK in this time and age.
89 			 */
90 			err = -EINVAL;
91 
92 			goto unreg_devices;
93 		}
94 	}
95 
96 	return 0;
97 
98 unreg_devices:
99 	while (i--)
100 		device_unregister(&cdl->devs[i].dev);
101 
102 	kfree(cdl->devs);
103 	cdl->devs = NULL;
104 	cdl->len = 0;
105 
106 	return err;
107 }
108 
109 void host1x_memory_context_list_free(struct host1x_memory_context_list *cdl)
110 {
111 	unsigned int i;
112 
113 	for (i = 0; i < cdl->len; i++)
114 		device_unregister(&cdl->devs[i].dev);
115 
116 	kfree(cdl->devs);
117 	cdl->len = 0;
118 }
119 
120 struct host1x_memory_context *host1x_memory_context_alloc(struct host1x *host1x,
121 							  struct device *dev,
122 							  struct pid *pid)
123 {
124 	struct host1x_memory_context_list *cdl = &host1x->context_list;
125 	struct host1x_memory_context *free = NULL;
126 	int i;
127 
128 	if (!cdl->len)
129 		return ERR_PTR(-EOPNOTSUPP);
130 
131 	mutex_lock(&cdl->lock);
132 
133 	for (i = 0; i < cdl->len; i++) {
134 		struct host1x_memory_context *cd = &cdl->devs[i];
135 
136 		if (cd->dev.iommu->iommu_dev != dev->iommu->iommu_dev)
137 			continue;
138 
139 		if (cd->owner == pid) {
140 			refcount_inc(&cd->ref);
141 			mutex_unlock(&cdl->lock);
142 			return cd;
143 		} else if (!cd->owner && !free) {
144 			free = cd;
145 		}
146 	}
147 
148 	if (!free) {
149 		mutex_unlock(&cdl->lock);
150 		return ERR_PTR(-EBUSY);
151 	}
152 
153 	refcount_set(&free->ref, 1);
154 	free->owner = get_pid(pid);
155 
156 	mutex_unlock(&cdl->lock);
157 
158 	return free;
159 }
160 EXPORT_SYMBOL_GPL(host1x_memory_context_alloc);
161 
162 void host1x_memory_context_get(struct host1x_memory_context *cd)
163 {
164 	refcount_inc(&cd->ref);
165 }
166 EXPORT_SYMBOL_GPL(host1x_memory_context_get);
167 
168 void host1x_memory_context_put(struct host1x_memory_context *cd)
169 {
170 	struct host1x_memory_context_list *cdl = &cd->host->context_list;
171 
172 	if (refcount_dec_and_mutex_lock(&cd->ref, &cdl->lock)) {
173 		put_pid(cd->owner);
174 		cd->owner = NULL;
175 		mutex_unlock(&cdl->lock);
176 	}
177 }
178 EXPORT_SYMBOL_GPL(host1x_memory_context_put);
179