etnaviv_drv.c 18 KB

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