xref: /linux/drivers/gpu/drm/etnaviv/etnaviv_drv.c (revision 2c1ed907520c50326b8f604907a8478b27881a2e)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2015-2018 Etnaviv Project
4  */
5 
6 #include <linux/component.h>
7 #include <linux/dma-mapping.h>
8 #include <linux/module.h>
9 #include <linux/of.h>
10 #include <linux/of_device.h>
11 #include <linux/platform_device.h>
12 #include <linux/uaccess.h>
13 
14 #include <drm/drm_debugfs.h>
15 #include <drm/drm_drv.h>
16 #include <drm/drm_file.h>
17 #include <drm/drm_ioctl.h>
18 #include <drm/drm_of.h>
19 #include <drm/drm_prime.h>
20 
21 #include "etnaviv_cmdbuf.h"
22 #include "etnaviv_drv.h"
23 #include "etnaviv_gpu.h"
24 #include "etnaviv_gem.h"
25 #include "etnaviv_mmu.h"
26 #include "etnaviv_perfmon.h"
27 
28 /*
29  * DRM operations:
30  */
31 
etnaviv_of_first_available_node(void)32 static struct device_node *etnaviv_of_first_available_node(void)
33 {
34 	struct device_node *np;
35 
36 	for_each_compatible_node(np, NULL, "vivante,gc") {
37 		if (of_device_is_available(np))
38 			return np;
39 	}
40 
41 	return NULL;
42 }
43 
load_gpu(struct drm_device * dev)44 static void load_gpu(struct drm_device *dev)
45 {
46 	struct etnaviv_drm_private *priv = dev->dev_private;
47 	unsigned int i;
48 
49 	for (i = 0; i < ETNA_MAX_PIPES; i++) {
50 		struct etnaviv_gpu *g = priv->gpu[i];
51 
52 		if (g) {
53 			int ret;
54 
55 			ret = etnaviv_gpu_init(g);
56 			if (ret)
57 				priv->gpu[i] = NULL;
58 		}
59 	}
60 }
61 
etnaviv_open(struct drm_device * dev,struct drm_file * file)62 static int etnaviv_open(struct drm_device *dev, struct drm_file *file)
63 {
64 	struct etnaviv_drm_private *priv = dev->dev_private;
65 	struct etnaviv_file_private *ctx;
66 	int ret, i;
67 
68 	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
69 	if (!ctx)
70 		return -ENOMEM;
71 
72 	ret = xa_alloc_cyclic(&priv->active_contexts, &ctx->id, ctx,
73 			      xa_limit_32b, &priv->next_context_id, GFP_KERNEL);
74 	if (ret < 0)
75 		goto out_free;
76 
77 	ctx->mmu = etnaviv_iommu_context_init(priv->mmu_global,
78 					      priv->cmdbuf_suballoc);
79 	if (!ctx->mmu) {
80 		ret = -ENOMEM;
81 		goto out_free;
82 	}
83 
84 	for (i = 0; i < ETNA_MAX_PIPES; i++) {
85 		struct etnaviv_gpu *gpu = priv->gpu[i];
86 		struct drm_gpu_scheduler *sched;
87 
88 		if (gpu) {
89 			sched = &gpu->sched;
90 			drm_sched_entity_init(&ctx->sched_entity[i],
91 					      DRM_SCHED_PRIORITY_NORMAL, &sched,
92 					      1, NULL);
93 		}
94 	}
95 
96 	file->driver_priv = ctx;
97 
98 	return 0;
99 
100 out_free:
101 	kfree(ctx);
102 	return ret;
103 }
104 
etnaviv_postclose(struct drm_device * dev,struct drm_file * file)105 static void etnaviv_postclose(struct drm_device *dev, struct drm_file *file)
106 {
107 	struct etnaviv_drm_private *priv = dev->dev_private;
108 	struct etnaviv_file_private *ctx = file->driver_priv;
109 	unsigned int i;
110 
111 	for (i = 0; i < ETNA_MAX_PIPES; i++) {
112 		struct etnaviv_gpu *gpu = priv->gpu[i];
113 
114 		if (gpu)
115 			drm_sched_entity_destroy(&ctx->sched_entity[i]);
116 	}
117 
118 	etnaviv_iommu_context_put(ctx->mmu);
119 
120 	xa_erase(&priv->active_contexts, ctx->id);
121 
122 	kfree(ctx);
123 }
124 
125 /*
126  * DRM debugfs:
127  */
128 
129 #ifdef CONFIG_DEBUG_FS
etnaviv_gem_show(struct drm_device * dev,struct seq_file * m)130 static int etnaviv_gem_show(struct drm_device *dev, struct seq_file *m)
131 {
132 	struct etnaviv_drm_private *priv = dev->dev_private;
133 
134 	etnaviv_gem_describe_objects(priv, m);
135 
136 	return 0;
137 }
138 
etnaviv_mm_show(struct drm_device * dev,struct seq_file * m)139 static int etnaviv_mm_show(struct drm_device *dev, struct seq_file *m)
140 {
141 	struct drm_printer p = drm_seq_file_printer(m);
142 
143 	read_lock(&dev->vma_offset_manager->vm_lock);
144 	drm_mm_print(&dev->vma_offset_manager->vm_addr_space_mm, &p);
145 	read_unlock(&dev->vma_offset_manager->vm_lock);
146 
147 	return 0;
148 }
149 
etnaviv_mmu_show(struct etnaviv_gpu * gpu,struct seq_file * m)150 static int etnaviv_mmu_show(struct etnaviv_gpu *gpu, struct seq_file *m)
151 {
152 	struct drm_printer p = drm_seq_file_printer(m);
153 	struct etnaviv_iommu_context *mmu_context;
154 
155 	seq_printf(m, "Active Objects (%s):\n", dev_name(gpu->dev));
156 
157 	/*
158 	 * Lock the GPU to avoid a MMU context switch just now and elevate
159 	 * the refcount of the current context to avoid it disappearing from
160 	 * under our feet.
161 	 */
162 	mutex_lock(&gpu->lock);
163 	mmu_context = gpu->mmu_context;
164 	if (mmu_context)
165 		etnaviv_iommu_context_get(mmu_context);
166 	mutex_unlock(&gpu->lock);
167 
168 	if (!mmu_context)
169 		return 0;
170 
171 	mutex_lock(&mmu_context->lock);
172 	drm_mm_print(&mmu_context->mm, &p);
173 	mutex_unlock(&mmu_context->lock);
174 
175 	etnaviv_iommu_context_put(mmu_context);
176 
177 	return 0;
178 }
179 
etnaviv_buffer_dump(struct etnaviv_gpu * gpu,struct seq_file * m)180 static void etnaviv_buffer_dump(struct etnaviv_gpu *gpu, struct seq_file *m)
181 {
182 	struct etnaviv_cmdbuf *buf = &gpu->buffer;
183 	u32 size = buf->size;
184 	u32 *ptr = buf->vaddr;
185 	u32 i;
186 
187 	seq_printf(m, "virt %p - phys 0x%llx - free 0x%08x\n",
188 			buf->vaddr, (u64)etnaviv_cmdbuf_get_pa(buf),
189 			size - buf->user_size);
190 
191 	for (i = 0; i < size / 4; i++) {
192 		if (i && !(i % 4))
193 			seq_puts(m, "\n");
194 		if (i % 4 == 0)
195 			seq_printf(m, "\t0x%p: ", ptr + i);
196 		seq_printf(m, "%08x ", *(ptr + i));
197 	}
198 	seq_puts(m, "\n");
199 }
200 
etnaviv_ring_show(struct etnaviv_gpu * gpu,struct seq_file * m)201 static int etnaviv_ring_show(struct etnaviv_gpu *gpu, struct seq_file *m)
202 {
203 	seq_printf(m, "Ring Buffer (%s): ", dev_name(gpu->dev));
204 
205 	mutex_lock(&gpu->lock);
206 	etnaviv_buffer_dump(gpu, m);
207 	mutex_unlock(&gpu->lock);
208 
209 	return 0;
210 }
211 
show_unlocked(struct seq_file * m,void * arg)212 static int show_unlocked(struct seq_file *m, void *arg)
213 {
214 	struct drm_info_node *node = (struct drm_info_node *) m->private;
215 	struct drm_device *dev = node->minor->dev;
216 	int (*show)(struct drm_device *dev, struct seq_file *m) =
217 			node->info_ent->data;
218 
219 	return show(dev, m);
220 }
221 
show_each_gpu(struct seq_file * m,void * arg)222 static int show_each_gpu(struct seq_file *m, void *arg)
223 {
224 	struct drm_info_node *node = (struct drm_info_node *) m->private;
225 	struct drm_device *dev = node->minor->dev;
226 	struct etnaviv_drm_private *priv = dev->dev_private;
227 	struct etnaviv_gpu *gpu;
228 	int (*show)(struct etnaviv_gpu *gpu, struct seq_file *m) =
229 			node->info_ent->data;
230 	unsigned int i;
231 	int ret = 0;
232 
233 	for (i = 0; i < ETNA_MAX_PIPES; i++) {
234 		gpu = priv->gpu[i];
235 		if (!gpu)
236 			continue;
237 
238 		ret = show(gpu, m);
239 		if (ret < 0)
240 			break;
241 	}
242 
243 	return ret;
244 }
245 
246 static struct drm_info_list etnaviv_debugfs_list[] = {
247 	{"gpu", show_each_gpu, 0, etnaviv_gpu_debugfs},
248 	{"gem", show_unlocked, 0, etnaviv_gem_show},
249 	{ "mm", show_unlocked, 0, etnaviv_mm_show },
250 	{"mmu", show_each_gpu, 0, etnaviv_mmu_show},
251 	{"ring", show_each_gpu, 0, etnaviv_ring_show},
252 };
253 
etnaviv_debugfs_init(struct drm_minor * minor)254 static void etnaviv_debugfs_init(struct drm_minor *minor)
255 {
256 	drm_debugfs_create_files(etnaviv_debugfs_list,
257 				 ARRAY_SIZE(etnaviv_debugfs_list),
258 				 minor->debugfs_root, minor);
259 }
260 #endif
261 
262 /*
263  * DRM ioctls:
264  */
265 
etnaviv_ioctl_get_param(struct drm_device * dev,void * data,struct drm_file * file)266 static int etnaviv_ioctl_get_param(struct drm_device *dev, void *data,
267 		struct drm_file *file)
268 {
269 	struct etnaviv_drm_private *priv = dev->dev_private;
270 	struct drm_etnaviv_param *args = data;
271 	struct etnaviv_gpu *gpu;
272 
273 	if (args->pipe >= ETNA_MAX_PIPES)
274 		return -EINVAL;
275 
276 	gpu = priv->gpu[args->pipe];
277 	if (!gpu)
278 		return -ENXIO;
279 
280 	return etnaviv_gpu_get_param(gpu, args->param, &args->value);
281 }
282 
etnaviv_ioctl_gem_new(struct drm_device * dev,void * data,struct drm_file * file)283 static int etnaviv_ioctl_gem_new(struct drm_device *dev, void *data,
284 		struct drm_file *file)
285 {
286 	struct drm_etnaviv_gem_new *args = data;
287 
288 	if (args->flags & ~(ETNA_BO_CACHED | ETNA_BO_WC | ETNA_BO_UNCACHED |
289 			    ETNA_BO_FORCE_MMU))
290 		return -EINVAL;
291 
292 	return etnaviv_gem_new_handle(dev, file, args->size,
293 			args->flags, &args->handle);
294 }
295 
etnaviv_ioctl_gem_cpu_prep(struct drm_device * dev,void * data,struct drm_file * file)296 static int etnaviv_ioctl_gem_cpu_prep(struct drm_device *dev, void *data,
297 		struct drm_file *file)
298 {
299 	struct drm_etnaviv_gem_cpu_prep *args = data;
300 	struct drm_gem_object *obj;
301 	int ret;
302 
303 	if (args->op & ~(ETNA_PREP_READ | ETNA_PREP_WRITE | ETNA_PREP_NOSYNC))
304 		return -EINVAL;
305 
306 	obj = drm_gem_object_lookup(file, args->handle);
307 	if (!obj)
308 		return -ENOENT;
309 
310 	ret = etnaviv_gem_cpu_prep(obj, args->op, &args->timeout);
311 
312 	drm_gem_object_put(obj);
313 
314 	return ret;
315 }
316 
etnaviv_ioctl_gem_cpu_fini(struct drm_device * dev,void * data,struct drm_file * file)317 static int etnaviv_ioctl_gem_cpu_fini(struct drm_device *dev, void *data,
318 		struct drm_file *file)
319 {
320 	struct drm_etnaviv_gem_cpu_fini *args = data;
321 	struct drm_gem_object *obj;
322 	int ret;
323 
324 	if (args->flags)
325 		return -EINVAL;
326 
327 	obj = drm_gem_object_lookup(file, args->handle);
328 	if (!obj)
329 		return -ENOENT;
330 
331 	ret = etnaviv_gem_cpu_fini(obj);
332 
333 	drm_gem_object_put(obj);
334 
335 	return ret;
336 }
337 
etnaviv_ioctl_gem_info(struct drm_device * dev,void * data,struct drm_file * file)338 static int etnaviv_ioctl_gem_info(struct drm_device *dev, void *data,
339 		struct drm_file *file)
340 {
341 	struct drm_etnaviv_gem_info *args = data;
342 	struct drm_gem_object *obj;
343 	int ret;
344 
345 	if (args->pad)
346 		return -EINVAL;
347 
348 	obj = drm_gem_object_lookup(file, args->handle);
349 	if (!obj)
350 		return -ENOENT;
351 
352 	ret = etnaviv_gem_mmap_offset(obj, &args->offset);
353 	drm_gem_object_put(obj);
354 
355 	return ret;
356 }
357 
etnaviv_ioctl_wait_fence(struct drm_device * dev,void * data,struct drm_file * file)358 static int etnaviv_ioctl_wait_fence(struct drm_device *dev, void *data,
359 		struct drm_file *file)
360 {
361 	struct drm_etnaviv_wait_fence *args = data;
362 	struct etnaviv_drm_private *priv = dev->dev_private;
363 	struct drm_etnaviv_timespec *timeout = &args->timeout;
364 	struct etnaviv_gpu *gpu;
365 
366 	if (args->flags & ~(ETNA_WAIT_NONBLOCK))
367 		return -EINVAL;
368 
369 	if (args->pipe >= ETNA_MAX_PIPES)
370 		return -EINVAL;
371 
372 	gpu = priv->gpu[args->pipe];
373 	if (!gpu)
374 		return -ENXIO;
375 
376 	if (args->flags & ETNA_WAIT_NONBLOCK)
377 		timeout = NULL;
378 
379 	return etnaviv_gpu_wait_fence_interruptible(gpu, args->fence,
380 						    timeout);
381 }
382 
etnaviv_ioctl_gem_userptr(struct drm_device * dev,void * data,struct drm_file * file)383 static int etnaviv_ioctl_gem_userptr(struct drm_device *dev, void *data,
384 	struct drm_file *file)
385 {
386 	struct drm_etnaviv_gem_userptr *args = data;
387 
388 	if (args->flags & ~(ETNA_USERPTR_READ|ETNA_USERPTR_WRITE) ||
389 	    args->flags == 0)
390 		return -EINVAL;
391 
392 	if (offset_in_page(args->user_ptr | args->user_size) ||
393 	    (uintptr_t)args->user_ptr != args->user_ptr ||
394 	    (u32)args->user_size != args->user_size ||
395 	    args->user_ptr & ~PAGE_MASK)
396 		return -EINVAL;
397 
398 	if (!access_ok((void __user *)(unsigned long)args->user_ptr,
399 		       args->user_size))
400 		return -EFAULT;
401 
402 	return etnaviv_gem_new_userptr(dev, file, args->user_ptr,
403 				       args->user_size, args->flags,
404 				       &args->handle);
405 }
406 
etnaviv_ioctl_gem_wait(struct drm_device * dev,void * data,struct drm_file * file)407 static int etnaviv_ioctl_gem_wait(struct drm_device *dev, void *data,
408 	struct drm_file *file)
409 {
410 	struct etnaviv_drm_private *priv = dev->dev_private;
411 	struct drm_etnaviv_gem_wait *args = data;
412 	struct drm_etnaviv_timespec *timeout = &args->timeout;
413 	struct drm_gem_object *obj;
414 	struct etnaviv_gpu *gpu;
415 	int ret;
416 
417 	if (args->flags & ~(ETNA_WAIT_NONBLOCK))
418 		return -EINVAL;
419 
420 	if (args->pipe >= ETNA_MAX_PIPES)
421 		return -EINVAL;
422 
423 	gpu = priv->gpu[args->pipe];
424 	if (!gpu)
425 		return -ENXIO;
426 
427 	obj = drm_gem_object_lookup(file, args->handle);
428 	if (!obj)
429 		return -ENOENT;
430 
431 	if (args->flags & ETNA_WAIT_NONBLOCK)
432 		timeout = NULL;
433 
434 	ret = etnaviv_gem_wait_bo(gpu, obj, timeout);
435 
436 	drm_gem_object_put(obj);
437 
438 	return ret;
439 }
440 
etnaviv_ioctl_pm_query_dom(struct drm_device * dev,void * data,struct drm_file * file)441 static int etnaviv_ioctl_pm_query_dom(struct drm_device *dev, void *data,
442 	struct drm_file *file)
443 {
444 	struct etnaviv_drm_private *priv = dev->dev_private;
445 	struct drm_etnaviv_pm_domain *args = data;
446 	struct etnaviv_gpu *gpu;
447 
448 	if (args->pipe >= ETNA_MAX_PIPES)
449 		return -EINVAL;
450 
451 	gpu = priv->gpu[args->pipe];
452 	if (!gpu)
453 		return -ENXIO;
454 
455 	return etnaviv_pm_query_dom(gpu, args);
456 }
457 
etnaviv_ioctl_pm_query_sig(struct drm_device * dev,void * data,struct drm_file * file)458 static int etnaviv_ioctl_pm_query_sig(struct drm_device *dev, void *data,
459 	struct drm_file *file)
460 {
461 	struct etnaviv_drm_private *priv = dev->dev_private;
462 	struct drm_etnaviv_pm_signal *args = data;
463 	struct etnaviv_gpu *gpu;
464 
465 	if (args->pipe >= ETNA_MAX_PIPES)
466 		return -EINVAL;
467 
468 	gpu = priv->gpu[args->pipe];
469 	if (!gpu)
470 		return -ENXIO;
471 
472 	return etnaviv_pm_query_sig(gpu, args);
473 }
474 
475 static const struct drm_ioctl_desc etnaviv_ioctls[] = {
476 #define ETNA_IOCTL(n, func, flags) \
477 	DRM_IOCTL_DEF_DRV(ETNAVIV_##n, etnaviv_ioctl_##func, flags)
478 	ETNA_IOCTL(GET_PARAM,    get_param,    DRM_RENDER_ALLOW),
479 	ETNA_IOCTL(GEM_NEW,      gem_new,      DRM_RENDER_ALLOW),
480 	ETNA_IOCTL(GEM_INFO,     gem_info,     DRM_RENDER_ALLOW),
481 	ETNA_IOCTL(GEM_CPU_PREP, gem_cpu_prep, DRM_RENDER_ALLOW),
482 	ETNA_IOCTL(GEM_CPU_FINI, gem_cpu_fini, DRM_RENDER_ALLOW),
483 	ETNA_IOCTL(GEM_SUBMIT,   gem_submit,   DRM_RENDER_ALLOW),
484 	ETNA_IOCTL(WAIT_FENCE,   wait_fence,   DRM_RENDER_ALLOW),
485 	ETNA_IOCTL(GEM_USERPTR,  gem_userptr,  DRM_RENDER_ALLOW),
486 	ETNA_IOCTL(GEM_WAIT,     gem_wait,     DRM_RENDER_ALLOW),
487 	ETNA_IOCTL(PM_QUERY_DOM, pm_query_dom, DRM_RENDER_ALLOW),
488 	ETNA_IOCTL(PM_QUERY_SIG, pm_query_sig, DRM_RENDER_ALLOW),
489 };
490 
etnaviv_show_fdinfo(struct drm_printer * p,struct drm_file * file)491 static void etnaviv_show_fdinfo(struct drm_printer *p, struct drm_file *file)
492 {
493 	drm_show_memory_stats(p, file);
494 }
495 
496 static const struct file_operations fops = {
497 	.owner = THIS_MODULE,
498 	DRM_GEM_FOPS,
499 	.show_fdinfo = drm_show_fdinfo,
500 };
501 
502 static const struct drm_driver etnaviv_drm_driver = {
503 	.driver_features    = DRIVER_GEM | DRIVER_RENDER,
504 	.open               = etnaviv_open,
505 	.postclose           = etnaviv_postclose,
506 	.gem_prime_import_sg_table = etnaviv_gem_prime_import_sg_table,
507 #ifdef CONFIG_DEBUG_FS
508 	.debugfs_init       = etnaviv_debugfs_init,
509 #endif
510 	.show_fdinfo        = etnaviv_show_fdinfo,
511 	.ioctls             = etnaviv_ioctls,
512 	.num_ioctls         = DRM_ETNAVIV_NUM_IOCTLS,
513 	.fops               = &fops,
514 	.name               = "etnaviv",
515 	.desc               = "etnaviv DRM",
516 	.major              = 1,
517 	.minor              = 4,
518 };
519 
520 /*
521  * Platform driver:
522  */
etnaviv_bind(struct device * dev)523 static int etnaviv_bind(struct device *dev)
524 {
525 	struct etnaviv_drm_private *priv;
526 	struct drm_device *drm;
527 	int ret;
528 
529 	drm = drm_dev_alloc(&etnaviv_drm_driver, dev);
530 	if (IS_ERR(drm))
531 		return PTR_ERR(drm);
532 
533 	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
534 	if (!priv) {
535 		dev_err(dev, "failed to allocate private data\n");
536 		ret = -ENOMEM;
537 		goto out_put;
538 	}
539 	drm->dev_private = priv;
540 
541 	dma_set_max_seg_size(dev, SZ_2G);
542 
543 	xa_init_flags(&priv->active_contexts, XA_FLAGS_ALLOC);
544 
545 	mutex_init(&priv->gem_lock);
546 	INIT_LIST_HEAD(&priv->gem_list);
547 	priv->num_gpus = 0;
548 	priv->shm_gfp_mask = GFP_HIGHUSER | __GFP_RETRY_MAYFAIL | __GFP_NOWARN;
549 
550 	/*
551 	 * If the GPU is part of a system with DMA addressing limitations,
552 	 * request pages for our SHM backend buffers from the DMA32 zone to
553 	 * hopefully avoid performance killing SWIOTLB bounce buffering.
554 	 */
555 	if (dma_addressing_limited(dev)) {
556 		priv->shm_gfp_mask |= GFP_DMA32;
557 		priv->shm_gfp_mask &= ~__GFP_HIGHMEM;
558 	}
559 
560 	priv->cmdbuf_suballoc = etnaviv_cmdbuf_suballoc_new(drm->dev);
561 	if (IS_ERR(priv->cmdbuf_suballoc)) {
562 		dev_err(drm->dev, "Failed to create cmdbuf suballocator\n");
563 		ret = PTR_ERR(priv->cmdbuf_suballoc);
564 		goto out_free_priv;
565 	}
566 
567 	dev_set_drvdata(dev, drm);
568 
569 	ret = component_bind_all(dev, drm);
570 	if (ret < 0)
571 		goto out_destroy_suballoc;
572 
573 	load_gpu(drm);
574 
575 	ret = drm_dev_register(drm, 0);
576 	if (ret)
577 		goto out_unbind;
578 
579 	return 0;
580 
581 out_unbind:
582 	component_unbind_all(dev, drm);
583 out_destroy_suballoc:
584 	etnaviv_cmdbuf_suballoc_destroy(priv->cmdbuf_suballoc);
585 out_free_priv:
586 	mutex_destroy(&priv->gem_lock);
587 	kfree(priv);
588 out_put:
589 	drm_dev_put(drm);
590 
591 	return ret;
592 }
593 
etnaviv_unbind(struct device * dev)594 static void etnaviv_unbind(struct device *dev)
595 {
596 	struct drm_device *drm = dev_get_drvdata(dev);
597 	struct etnaviv_drm_private *priv = drm->dev_private;
598 
599 	drm_dev_unregister(drm);
600 
601 	component_unbind_all(dev, drm);
602 
603 	etnaviv_cmdbuf_suballoc_destroy(priv->cmdbuf_suballoc);
604 
605 	xa_destroy(&priv->active_contexts);
606 
607 	drm->dev_private = NULL;
608 	kfree(priv);
609 
610 	drm_dev_put(drm);
611 }
612 
613 static const struct component_master_ops etnaviv_master_ops = {
614 	.bind = etnaviv_bind,
615 	.unbind = etnaviv_unbind,
616 };
617 
etnaviv_pdev_probe(struct platform_device * pdev)618 static int etnaviv_pdev_probe(struct platform_device *pdev)
619 {
620 	struct device *dev = &pdev->dev;
621 	struct device_node *first_node = NULL;
622 	struct component_match *match = NULL;
623 
624 	if (!dev->platform_data) {
625 		struct device_node *core_node;
626 
627 		for_each_compatible_node(core_node, NULL, "vivante,gc") {
628 			if (!of_device_is_available(core_node))
629 				continue;
630 
631 			drm_of_component_match_add(dev, &match,
632 						   component_compare_of, core_node);
633 		}
634 	} else {
635 		char **names = dev->platform_data;
636 		unsigned i;
637 
638 		for (i = 0; names[i]; i++)
639 			component_match_add(dev, &match, component_compare_dev_name, names[i]);
640 	}
641 
642 	/*
643 	 * PTA and MTLB can have 40 bit base addresses, but
644 	 * unfortunately, an entry in the MTLB can only point to a
645 	 * 32 bit base address of a STLB. Moreover, to initialize the
646 	 * MMU we need a command buffer with a 32 bit address because
647 	 * without an MMU there is only an indentity mapping between
648 	 * the internal 32 bit addresses and the bus addresses.
649 	 *
650 	 * To make things easy, we set the dma_coherent_mask to 32
651 	 * bit to make sure we are allocating the command buffers and
652 	 * TLBs in the lower 4 GiB address space.
653 	 */
654 	if (dma_set_mask(dev, DMA_BIT_MASK(40)) ||
655 	    dma_set_coherent_mask(dev, DMA_BIT_MASK(32))) {
656 		dev_dbg(dev, "No suitable DMA available\n");
657 		return -ENODEV;
658 	}
659 
660 	/*
661 	 * Apply the same DMA configuration to the virtual etnaviv
662 	 * device as the GPU we found. This assumes that all Vivante
663 	 * GPUs in the system share the same DMA constraints.
664 	 */
665 	first_node = etnaviv_of_first_available_node();
666 	if (first_node) {
667 		of_dma_configure(dev, first_node, true);
668 		of_node_put(first_node);
669 	}
670 
671 	return component_master_add_with_match(dev, &etnaviv_master_ops, match);
672 }
673 
etnaviv_pdev_remove(struct platform_device * pdev)674 static void etnaviv_pdev_remove(struct platform_device *pdev)
675 {
676 	component_master_del(&pdev->dev, &etnaviv_master_ops);
677 }
678 
679 static struct platform_driver etnaviv_platform_driver = {
680 	.probe      = etnaviv_pdev_probe,
681 	.remove     = etnaviv_pdev_remove,
682 	.driver     = {
683 		.name   = "etnaviv",
684 	},
685 };
686 
etnaviv_create_platform_device(const char * name,struct platform_device ** ppdev)687 static int etnaviv_create_platform_device(const char *name,
688 					  struct platform_device **ppdev)
689 {
690 	struct platform_device *pdev;
691 	int ret;
692 
693 	pdev = platform_device_alloc(name, PLATFORM_DEVID_NONE);
694 	if (!pdev)
695 		return -ENOMEM;
696 
697 	ret = platform_device_add(pdev);
698 	if (ret) {
699 		platform_device_put(pdev);
700 		return ret;
701 	}
702 
703 	*ppdev = pdev;
704 
705 	return 0;
706 }
707 
etnaviv_destroy_platform_device(struct platform_device ** ppdev)708 static void etnaviv_destroy_platform_device(struct platform_device **ppdev)
709 {
710 	struct platform_device *pdev = *ppdev;
711 
712 	if (!pdev)
713 		return;
714 
715 	platform_device_unregister(pdev);
716 
717 	*ppdev = NULL;
718 }
719 
720 static struct platform_device *etnaviv_drm;
721 
etnaviv_init(void)722 static int __init etnaviv_init(void)
723 {
724 	int ret;
725 	struct device_node *np;
726 
727 	etnaviv_validate_init();
728 
729 	ret = platform_driver_register(&etnaviv_gpu_driver);
730 	if (ret != 0)
731 		return ret;
732 
733 	ret = platform_driver_register(&etnaviv_platform_driver);
734 	if (ret != 0)
735 		goto unregister_gpu_driver;
736 
737 	/*
738 	 * If the DT contains at least one available GPU device, instantiate
739 	 * the DRM platform device.
740 	 */
741 	np = etnaviv_of_first_available_node();
742 	if (np) {
743 		of_node_put(np);
744 
745 		ret = etnaviv_create_platform_device("etnaviv", &etnaviv_drm);
746 		if (ret)
747 			goto unregister_platform_driver;
748 	}
749 
750 	return 0;
751 
752 unregister_platform_driver:
753 	platform_driver_unregister(&etnaviv_platform_driver);
754 unregister_gpu_driver:
755 	platform_driver_unregister(&etnaviv_gpu_driver);
756 	return ret;
757 }
758 module_init(etnaviv_init);
759 
etnaviv_exit(void)760 static void __exit etnaviv_exit(void)
761 {
762 	etnaviv_destroy_platform_device(&etnaviv_drm);
763 	platform_driver_unregister(&etnaviv_platform_driver);
764 	platform_driver_unregister(&etnaviv_gpu_driver);
765 }
766 module_exit(etnaviv_exit);
767 
768 MODULE_AUTHOR("Christian Gmeiner <christian.gmeiner@gmail.com>");
769 MODULE_AUTHOR("Russell King <rmk+kernel@armlinux.org.uk>");
770 MODULE_AUTHOR("Lucas Stach <l.stach@pengutronix.de>");
771 MODULE_DESCRIPTION("etnaviv DRM Driver");
772 MODULE_LICENSE("GPL v2");
773 MODULE_ALIAS("platform:etnaviv");
774